Code

- Corrected open perl function, corrected variable passing to open and close
[gosa.git] / gosa-si / client / events / gosaTriggered.pm
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;
31 use strict;
32 use warnings;
33 use Exporter;
35 use GOSA::GosaSupportDaemon;
36 use MIME::Base64;
37 use File::Temp qw/ tempfile/;
39 @ISA = qw(Exporter);
40 my @events = (
41     "get_events",
42     "usr_msg",
43     "trigger_action_localboot",
44     "trigger_action_halt",
45     "trigger_action_faireboot",
46     "trigger_action_reboot",
47     "trigger_action_reinstall",
48     "trigger_action_update",
49     "trigger_action_instant_update",
50     "trigger_goto_settings_reload",
51     );
52 @EXPORT = @events;
54 BEGIN {}
56 END {}
58 ### Parameter declarations ###################################################
59 my $userNotification;
60 my %cfg_defaults = (
61 "client" => {
62         "user-notification-of-admin-activities" => [\$userNotification, 'true'],
63         },
64 );
66 # Read config file
67 &read_configfile($main::cfg_file, %cfg_defaults);
69 ###############################################################################
70 =over 
72 =item B<get_events ()>
74 =over
76 =item description 
78     Reports all provided functions.
80 =item parameter
82     None.
84 =item return 
86     \@events - ARRAYREF - array containing all functions 
88 =back
90 =back
92 =cut
93 ###############################################################################
94 sub get_events { return \@events; }
97 ###############################################################################
98 =over 
100 =item B<usr_msg ($$)>
102 =over
104 =item description 
106     Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
108 =item parameter
110     $msg - STRING - complete GOsa-si message
111     $msg_hash - HASHREF - content of GOsa-si message in a hash
113 =item GOsa-si message xml content
114     
115     <to> - STRING - username message should be deliverd to
116     <subject> - STRING - subject of the message, base64 encoded
117     <message> - STRING - message itself, base64 encoded
119 =item return 
121     $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
123 =back
125 =back
127 =cut
128 ###############################################################################
129 sub usr_msg {
130     my ($msg, $msg_hash) = @_;
131     my $header = @{$msg_hash->{'header'}}[0];
132     my $source = @{$msg_hash->{'source'}}[0];
133     my $target = @{$msg_hash->{'target'}}[0];
135     my $to = @{$msg_hash->{'usr'}}[0];
136     my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
137     my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
139     my ($rand_fh, $rand_file) = tempfile( SUFFIX => '.goto_notify');
140     print $rand_fh "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
141     close $rand_fh;
142         
143     my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' &" );
145     return
149 ###############################################################################
150 =over 
152 =item B<trigger_action_localboot ($$)>
154 =over
156 =item description 
158     Executes '/sbin/shutdown -r' if  no user is logged in otherwise write 
159     'trigger_action_localboot' to '/etc/gosa-si/event'
161 =item parameter
163     $msg - STRING - complete GOsa-si message
164     $msg_hash - HASHREF - content of GOsa-si message in a hash
166 =item GOsa-si message xml content
168     <timeout> - INTEGER - timeout to wait befor restart, default 0
170 =item return 
171     
172     Nothing.
174 =back
176 =back
178 =cut
179 ###############################################################################
180 sub trigger_action_localboot {
181     my ($msg, $msg_hash) = @_;
182     my $timeout;
184         # Invalid timeout parameter are set to 0
185     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
186         $timeout = -1;
187     } else {
188         $timeout = @{$msg_hash->{timeout}}[0];
189     }
191         # Check if user should be notificated or not
192         if ($userNotification eq "true") {
193                 # Check logged in user
194                 my @user_list = &get_logged_in_users;
195                 if( @user_list >= 1 ) {
196                         open($FILE, ">", "/etc/gosa-si/event");
197                         print $FILE "trigger_action_localboot\n";
198                         close($FILE);
199                 }
200         }
201     else {
202         system( "/sbin/shutdown -r +$timeout &" );
203     }
205     return;
209 ###############################################################################
210 =over 
212 =item B<trigger_action_faireboot ($$)>
214 =over
216 =item description 
218     Executes '/usr/sbin/faireboot'.
220 =item parameter
222     $msg - STRING - complete GOsa-si message
223     $msg_hash - HASHREF - content of GOsa-si message in a hash
225 =item GOsa-si message xml content
227     None.
229 =item return 
230     
231     Nothing.
233 =back
235 =back
237 =cut
238 ###############################################################################
239 sub trigger_action_faireboot {
240     my ($msg, $msg_hash) = @_;
241         &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7); 
242     system("/usr/sbin/faireboot");
243     return;
247 ###############################################################################
248 =over 
250 =item B<trigger_action_reboot ($$)>
252 =over
254 =item description 
256     Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r'  if  no 
257     user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
259 =item parameter
261     $msg - STRING - complete GOsa-si message
262     $msg_hash - HASHREF - content of GOsa-si message in a hash
264 =item GOsa-si message xml content
266     <timeout> - INTEGER - timeout to wait befor reboot, default 0
268 =item return 
269     
270     Nothing.
272 =back
274 =back
276 =cut
277 ###############################################################################
278 sub trigger_action_reboot {
279     my ($msg, $msg_hash) = @_;
280     my $timeout;
282         # Invalid timeout parameter are set to 0
283     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
284         $timeout = 0;
285     } 
286     else {
287         $timeout = @{$msg_hash->{timeout}}[0];
288     }
290         # Check if user should be notificated or not
291         if ($userNotification eq "true") {
292                 # Check logged in user
293                 my @user_list = &get_logged_in_users;
294                 if( @user_list >= 1 ) {
295                         system( "/usr/bin/goto-notify reboot" );
296                         open($FILE, ">", "/etc/gosa-si/event");
297                         print $FILE "reboot\n";
298                         close($FILE);
299                 }
300         }
301     else {
302         system( "/sbin/shutdown -r +$timeout &" );
303     }
305     return;
309 ###############################################################################
310 =over 
312 =item B<trigger_action_halt ($$)>
314 =over
316 =item description 
318     Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if  no 
319     user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
321 =item parameter
323     $msg - STRING - complete GOsa-si message
324     $msg_hash - HASHREF - content of GOsa-si message in a hash
326 =item GOsa-si message xml content
328     <timeout> - INTEGER - timeout to wait befor halt, default 0
330 =item return 
331     
332     Nothing.    
334 =back
336 =back
338 =cut
339 ###############################################################################
340 sub trigger_action_halt {
341     my ($msg, $msg_hash) = @_;
342     my $timeout;
344         # Invalid timeout parameter are set to 0
345     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
346         $timeout = 0;
347     } 
348     else {
349         $timeout = @{$msg_hash->{timeout}}[0];
350     }
352         # Check if user should be notificated or not
353         if ($userNotification eq "true") {
354                 # Check logged in user
355                 my @user_list = &get_logged_in_users;
356                 if( @user_list >= 1 ) {
357                         system( "/usr/bin/goto-notify halt" );
358                         open($FILE, ">", "/etc/gosa-si/event");
359                         print $FILE "halt\n";
360                         close($FILE);
361                 }
362     } else {
363         system( "/sbin/shutdown -h +$timeout &" );
364     }
366     return;
370 ###############################################################################
371 =over 
373 =item B<trigger_action_reinstall>
375 =over
377 =item description 
379     Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no 
380     user is logged in otherwise write 'install' to '/etc/gosa-si/event'
382 =item parameter
384     $msg - STRING - complete GOsa-si message
385     $msg_hash - HASHREF - content of GOsa-si message in a hash
387 =item GOsa-si message xml content
389     None.
391 =item return 
392     
393     Nothing.
395 =back
397 =back
399 =cut
400 ###############################################################################
401 sub trigger_action_reinstall {
402     my ($msg, $msg_hash) = @_;
404         # Check if user should be notificated or not
405         if ($userNotification eq "true") {
406                 # Check logged in user
407                 my @user_list = &get_logged_in_users;
408                 if( @user_list >= 1 ) {
409                         system( "/usr/bin/goto-notify install" );
410                         open($FILE, ">", "/etc/gosa-si/event");
411                         print $FILE "install\n";
412                         close($FILE);
413                 }
414         } else {
415                 system( "/sbin/shutdown -r now &" );
416         }
418     return;
422 ###############################################################################
423 =over 
425 =item B<trigger_action_updae>
427 =over
429 =item description 
431     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
433 =item parameter
435     $msg - STRING - complete GOsa-si message
436     $msg_hash - HASHREF - content of GOsa-si message in a hash
438 =item GOsa-si message xml content
440     None.
442 =item return 
443     
444     Nothing
446 =back
448 =back
450 =cut
451 ###############################################################################
452 # Backward compatibility
453 sub trigger_action_update {
454     my ($msg, $msg_hash) = @_;
456     # Execute update
457     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
459     return;
463 ###############################################################################
464 =over 
466 =item B<trigger_action_instant_update ($$)>
468 =over
470 =item description 
472     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
474 =item parameter
476     $msg - STRING - complete GOsa-si message
477     $msg_hash - HASHREF - content of GOsa-si message in a hash
479 =item GOsa-si message xml content
481     None.
483 =item return 
484     
485     Nothing.
487 =back
489 =back
491 =cut
492 ###############################################################################
493 # Backward compatibility
494 sub trigger_action_instant_update {
495     my ($msg, $msg_hash) = @_;
497     # Execute update
498     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
500     return;
503 sub trigger_goto_settings_reload {
504     my ($msg, $msg_hash) = @_;
506     # Execute goto settings reload
507     my $cmd = "/etc/init.d/goto-agents";
508     my $pram = "start";
509     if (-f $cmd){
510         my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
511     } else {
512         &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
513     }
515     return;
519 1;