2 =head1 NAME
4 gosaTriggered.pm
6 =head1 SYNOPSIS
8 use GOSA::GosaSupportDaemon;
10 use MIME::Base64
12 =head1 DESCRIPTION
14 This module contains all GOsa-SI-client processing instructions concerning actions controllable from GOsa.
16 =head1 VERSION
18 Version 1.0
20 =head1 AUTHOR
22 Andreas Rettenberger <rettenberger at gonicus dot de>
24 =head1 FUNCTIONS
26 =cut
29 package gosaTriggered;
30 use Exporter;
31 @ISA = qw(Exporter);
32 my @events = (
33 "get_events",
34 "usr_msg",
35 "trigger_action_localboot",
36 "trigger_action_halt",
37 "trigger_action_faireboot",
38 "trigger_action_reboot",
39 "trigger_action_reinstall",
40 "trigger_action_update",
41 "trigger_action_instant_update",
42 "trigger_goto_settings_reload",
43 );
44 @EXPORT = @events;
46 use strict;
47 use warnings;
48 use GOSA::GosaSupportDaemon;
49 use MIME::Base64;
50 use File::Temp qw/ tempfile/;
52 BEGIN {}
54 END {}
56 ### Parameter declarations ###################################################
57 my $userNotification;
58 my %cfg_defaults = (
59 "ClientPackages" => {
60 "user-notification-of-admin-activities" => [\$userNotification, 'enabled'],
61 },
62 );
64 # Read config file
65 &read_configfile($main::cfg_file, %cfg_defaults);
67 ###############################################################################
68 =over
70 =item B<get_events ()>
72 =over
74 =item description
76 Reports all provided functions.
78 =item parameter
80 None.
82 =item return
84 \@events - ARRAYREF - array containing all functions
86 =back
88 =back
90 =cut
91 ###############################################################################
92 sub get_events { return \@events; }
95 ###############################################################################
96 =over
98 =item B<usr_msg ($$)>
100 =over
102 =item description
104 Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
106 =item parameter
108 $msg - STRING - complete GOsa-si message
109 $msg_hash - HASHREF - content of GOsa-si message in a hash
111 =item GOsa-si message xml content
113 <to> - STRING - username message should be deliverd to
114 <subject> - STRING - subject of the message, base64 encoded
115 <message> - STRING - message itself, base64 encoded
117 =item return
119 $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
121 =back
123 =back
125 =cut
126 ###############################################################################
127 sub usr_msg {
128 my ($msg, $msg_hash) = @_;
129 my $header = @{$msg_hash->{'header'}}[0];
130 my $source = @{$msg_hash->{'source'}}[0];
131 my $target = @{$msg_hash->{'target'}}[0];
133 my $to = @{$msg_hash->{'usr'}}[0];
134 my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
135 my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
137 my ($rand_fh, $rand_file) = tempfile( SUFFIX => '.goto_notify');
138 print $rand_fh "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
139 close $rand_fh;
141 my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' &" );
143 return
144 }
147 ###############################################################################
148 =over
150 =item B<trigger_action_localboot ($$)>
152 =over
154 =item description
156 Executes '/sbin/shutdown -r' if no user is logged in otherwise write
157 'trigger_action_localboot' to '/etc/gosa-si/event'
159 =item parameter
161 $msg - STRING - complete GOsa-si message
162 $msg_hash - HASHREF - content of GOsa-si message in a hash
164 =item GOsa-si message xml content
166 <timeout> - INTEGER - timeout to wait befor restart, default 0
168 =item return
170 Nothing.
172 =back
174 =back
176 =cut
177 ###############################################################################
178 sub trigger_action_localboot {
179 my ($msg, $msg_hash) = @_;
180 my $timeout;
182 # Invalid timeout parameter are set to 0
183 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
184 $timeout = -1;
185 } else {
186 $timeout = @{$msg_hash->{timeout}}[0];
187 }
189 # Check if user should be notificated or not
190 if ($userNotification eq "enabled") {
191 # Check logged in user
192 my @user_list = &get_logged_in_users;
193 if( @user_list >= 1 ) {
194 open(FILE, "> /etc/gosa-si/event");
195 print FILE "trigger_action_localboot\n";
196 close(FILE);
197 }
198 }
199 else {
200 system( "/sbin/shutdown -r +$timeout &" );
201 }
203 return;
204 }
207 ###############################################################################
208 =over
210 =item B<trigger_action_faireboot ($$)>
212 =over
214 =item description
216 Executes '/usr/sbin/faireboot'.
218 =item parameter
220 $msg - STRING - complete GOsa-si message
221 $msg_hash - HASHREF - content of GOsa-si message in a hash
223 =item GOsa-si message xml content
225 None.
227 =item return
229 Nothing.
231 =back
233 =back
235 =cut
236 ###############################################################################
237 sub trigger_action_faireboot {
238 my ($msg, $msg_hash) = @_;
239 &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7);
240 system("/usr/sbin/faireboot");
241 return;
242 }
245 ###############################################################################
246 =over
248 =item B<trigger_action_reboot ($$)>
250 =over
252 =item description
254 Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r' if no
255 user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
257 =item parameter
259 $msg - STRING - complete GOsa-si message
260 $msg_hash - HASHREF - content of GOsa-si message in a hash
262 =item GOsa-si message xml content
264 <timeout> - INTEGER - timeout to wait befor reboot, default 0
266 =item return
268 Nothing.
270 =back
272 =back
274 =cut
275 ###############################################################################
276 sub trigger_action_reboot {
277 my ($msg, $msg_hash) = @_;
278 my $timeout;
280 # Invalid timeout parameter are set to 0
281 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
282 $timeout = 0;
283 }
284 else {
285 $timeout = @{$msg_hash->{timeout}}[0];
286 }
288 # Check if user should be notificated or not
289 if ($userNotification eq "enabled") {
290 # Check logged in user
291 my @user_list = &get_logged_in_users;
292 if( @user_list >= 1 ) {
293 system( "/usr/bin/goto-notify reboot" );
294 open(FILE, "> /etc/gosa-si/event");
295 print FILE "reboot\n";
296 close(FILE);
297 }
298 }
299 else {
300 system( "/sbin/shutdown -r +$timeout &" );
301 }
303 return;
304 }
307 ###############################################################################
308 =over
310 =item B<trigger_action_halt ($$)>
312 =over
314 =item description
316 Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if no
317 user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
319 =item parameter
321 $msg - STRING - complete GOsa-si message
322 $msg_hash - HASHREF - content of GOsa-si message in a hash
324 =item GOsa-si message xml content
326 <timeout> - INTEGER - timeout to wait befor halt, default 0
328 =item return
330 Nothing.
332 =back
334 =back
336 =cut
337 ###############################################################################
338 sub trigger_action_halt {
339 my ($msg, $msg_hash) = @_;
340 my $timeout;
342 # Invalid timeout parameter are set to 0
343 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
344 $timeout = 0;
345 }
346 else {
347 $timeout = @{$msg_hash->{timeout}}[0];
348 }
350 # Check if user should be notificated or not
351 if ($userNotification eq "enabled") {
352 # Check logged in user
353 my @user_list = &get_logged_in_users;
354 if( @user_list >= 1 ) {
355 system( "/usr/bin/goto-notify halt" );
356 open(FILE, "> /etc/gosa-si/event");
357 print FILE "halt\n";
358 close(FILE);
359 }
360 } else {
361 system( "/sbin/shutdown -h +$timeout &" );
362 }
364 return;
365 }
368 ###############################################################################
369 =over
371 =item B<trigger_action_reinstall>
373 =over
375 =item description
377 Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no
378 user is logged in otherwise write 'install' to '/etc/gosa-si/event'
380 =item parameter
382 $msg - STRING - complete GOsa-si message
383 $msg_hash - HASHREF - content of GOsa-si message in a hash
385 =item GOsa-si message xml content
387 None.
389 =item return
391 Nothing.
393 =back
395 =back
397 =cut
398 ###############################################################################
399 sub trigger_action_reinstall {
400 my ($msg, $msg_hash) = @_;
402 # Check if user should be notificated or not
403 if ($userNotification eq "enabled") {
404 # Check logged in user
405 my @user_list = &get_logged_in_users;
406 if( @user_list >= 1 ) {
407 system( "/usr/bin/goto-notify install" );
408 open(FILE, "> /etc/gosa-si/event");
409 print FILE "install\n";
410 close(FILE);
411 }
412 } else {
413 system( "/sbin/shutdown -r now &" );
414 }
416 return;
417 }
420 ###############################################################################
421 =over
423 =item B<trigger_action_updae>
425 =over
427 =item description
429 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
431 =item parameter
433 $msg - STRING - complete GOsa-si message
434 $msg_hash - HASHREF - content of GOsa-si message in a hash
436 =item GOsa-si message xml content
438 None.
440 =item return
442 Nothing
444 =back
446 =back
448 =cut
449 ###############################################################################
450 # Backward compatibility
451 sub trigger_action_update {
452 my ($msg, $msg_hash) = @_;
454 # Execute update
455 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
457 return;
458 }
461 ###############################################################################
462 =over
464 =item B<trigger_action_instant_update ($$)>
466 =over
468 =item description
470 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
472 =item parameter
474 $msg - STRING - complete GOsa-si message
475 $msg_hash - HASHREF - content of GOsa-si message in a hash
477 =item GOsa-si message xml content
479 None.
481 =item return
483 Nothing.
485 =back
487 =back
489 =cut
490 ###############################################################################
491 # Backward compatibility
492 sub trigger_action_instant_update {
493 my ($msg, $msg_hash) = @_;
495 # Execute update
496 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
498 return;
499 }
501 sub trigger_goto_settings_reload {
502 my ($msg, $msg_hash) = @_;
504 # Execute goto settings reload
505 my $cmd = "/etc/init.d/goto-agents";
506 my $pram = "start";
507 if (-f $cmd){
508 my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
509 } else {
510 &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
511 }
513 return;
514 }
517 1;