Code

Updated locales
[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
28 package gosaTriggered;
30 use strict;
31 use warnings;
33 use MIME::Base64;
34 use File::Temp qw/ tempfile/;
35 use GOsaSI::GosaSupportDaemon;
37 use Exporter;
39 our @ISA = qw(Exporter);
41 my @events = (
42     "get_events",
43     "usr_msg",
44     "trigger_action_localboot",
45     "trigger_action_halt",
46     "trigger_action_faireboot",
47     "trigger_action_reboot",
48     "trigger_action_reinstall",
49     "trigger_action_update",
50     "trigger_action_instant_update",
51     "trigger_goto_settings_reload",
52     );
53     
54 our @EXPORT = @events;
56 BEGIN {}
58 END {}
60 ### Parameter declarations ###################################################
61 my $userNotification;
62 my %cfg_defaults = (
63 "client" => {
64         "user-notification-of-admin-activities" => [\$userNotification, 'true'],
65         },
66 );
68 # Read config file
69 &read_configfile($main::cfg_file, %cfg_defaults);
71 ###############################################################################
72 =over 
74 =item B<get_events ()>
76 =over
78 =item description 
80     Reports all provided functions.
82 =item parameter
84     None.
86 =item return 
88     \@events - ARRAYREF - array containing all functions 
90 =back
92 =back
94 =cut
95 ###############################################################################
96 sub get_events { return \@events; }
99 ###############################################################################
100 =over 
102 =item B<usr_msg ($$)>
104 =over
106 =item description 
108     Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
110 =item parameter
112     $msg - STRING - complete GOsa-si message
113     $msg_hash - HASHREF - content of GOsa-si message in a hash
115 =item GOsa-si message xml content
116     
117     <to> - STRING - username message should be deliverd to
118     <subject> - STRING - subject of the message, base64 encoded
119     <message> - STRING - message itself, base64 encoded
121 =item return 
123     $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
125 =back
127 =back
129 =cut
130 ###############################################################################
131 sub usr_msg {
132     my ($msg, $msg_hash) = @_;
133     my $header = @{$msg_hash->{'header'}}[0];
134     my $source = @{$msg_hash->{'source'}}[0];
135     my $target = @{$msg_hash->{'target'}}[0];
137     my $to = @{$msg_hash->{'usr'}}[0];
138     my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
139     my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
141     my ($rand_fh, $rand_file) = tempfile( SUFFIX => '.goto_notify');
142     print $rand_fh "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
143     close $rand_fh;
144         
145     my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' &" );
147     return
151 ###############################################################################
152 =over 
154 =item B<trigger_action_localboot ($$)>
156 =over
158 =item description 
160     Executes '/sbin/shutdown -r' if  no user is logged in otherwise write 
161     'trigger_action_localboot' to '/etc/gosa-si/event'
163 =item parameter
165     $msg - STRING - complete GOsa-si message
166     $msg_hash - HASHREF - content of GOsa-si message in a hash
168 =item GOsa-si message xml content
170     <timeout> - INTEGER - timeout to wait befor restart, default 0
172 =item return 
173     
174     Nothing.
176 =back
178 =back
180 =cut
181 ###############################################################################
182 sub trigger_action_localboot {
183     my ($msg, $msg_hash) = @_;
184     my $timeout;
186         # Invalid timeout parameter are set to 0
187     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
188         $timeout = -1;
189     } else {
190         $timeout = @{$msg_hash->{timeout}}[0];
191     }
193         # Check if user should be notificated or not
194         if ($userNotification eq "true") {
195                 # Check logged in user
196                 my @user_list = &get_logged_in_users;
197                 if( @user_list >= 1 ) {
198                         open(my $FILE, ">", "/etc/gosa-si/event");
199                         print $FILE "trigger_action_localboot\n";
200                         close($FILE);
201                 }
202         }
203     else {
204         system( "/sbin/shutdown -r +$timeout &" );
205     }
207     return;
211 ###############################################################################
212 =over 
214 =item B<trigger_action_faireboot ($$)>
216 =over
218 =item description 
220     Executes '/usr/sbin/faireboot'.
222 =item parameter
224     $msg - STRING - complete GOsa-si message
225     $msg_hash - HASHREF - content of GOsa-si message in a hash
227 =item GOsa-si message xml content
229     None.
231 =item return 
232     
233     Nothing.
235 =back
237 =back
239 =cut
240 ###############################################################################
241 sub trigger_action_faireboot {
242     my ($msg, $msg_hash) = @_;
243         &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7); 
244     system("/usr/sbin/faireboot");
245     return;
249 ###############################################################################
250 =over 
252 =item B<trigger_action_reboot ($$)>
254 =over
256 =item description 
258     Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r'  if  no 
259     user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
261 =item parameter
263     $msg - STRING - complete GOsa-si message
264     $msg_hash - HASHREF - content of GOsa-si message in a hash
266 =item GOsa-si message xml content
268     <timeout> - INTEGER - timeout to wait befor reboot, default 0
270 =item return 
271     
272     Nothing.
274 =back
276 =back
278 =cut
279 ###############################################################################
280 sub trigger_action_reboot {
281     my ($msg, $msg_hash) = @_;
282     my $timeout;
284         # Invalid timeout parameter are set to 0
285     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
286         $timeout = 0;
287     } 
288     else {
289         $timeout = @{$msg_hash->{timeout}}[0];
290     }
292         # Check if user should be notificated or not
293         if ($userNotification eq "true") {
294                 # Check logged in user
295                 my @user_list = &get_logged_in_users;
296                 if( @user_list >= 1 ) {
297                         system( "/usr/bin/goto-notify reboot" );
298                         open(my $FILE, ">", "/etc/gosa-si/event");
299                         print $FILE "reboot\n";
300                         close($FILE);
301                 }
302         }
303     else {
304         system( "/sbin/shutdown -r +$timeout &" );
305     }
307     return;
311 ###############################################################################
312 =over 
314 =item B<trigger_action_halt ($$)>
316 =over
318 =item description 
320     Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if  no 
321     user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
323 =item parameter
325     $msg - STRING - complete GOsa-si message
326     $msg_hash - HASHREF - content of GOsa-si message in a hash
328 =item GOsa-si message xml content
330     <timeout> - INTEGER - timeout to wait befor halt, default 0
332 =item return 
333     
334     Nothing.    
336 =back
338 =back
340 =cut
341 ###############################################################################
342 sub trigger_action_halt {
343     my ($msg, $msg_hash) = @_;
344     my $timeout;
346         # Invalid timeout parameter are set to 0
347     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
348         $timeout = 0;
349     } 
350     else {
351         $timeout = @{$msg_hash->{timeout}}[0];
352     }
354         # Check if user should be notificated or not
355         if ($userNotification eq "true") {
356                 # Check logged in user
357                 my @user_list = &get_logged_in_users;
358                 if( @user_list >= 1 ) {
359                         system( "/usr/bin/goto-notify halt" );
360                         open(my $FILE, ">", "/etc/gosa-si/event");
361                         print $FILE "halt\n";
362                         close($FILE);
363                 }
364     } else {
365         system( "/sbin/shutdown -h +$timeout &" );
366     }
368     return;
372 ###############################################################################
373 =over 
375 =item B<trigger_action_reinstall>
377 =over
379 =item description 
381     Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no 
382     user is logged in otherwise write 'install' to '/etc/gosa-si/event'
384 =item parameter
386     $msg - STRING - complete GOsa-si message
387     $msg_hash - HASHREF - content of GOsa-si message in a hash
389 =item GOsa-si message xml content
391     None.
393 =item return 
394     
395     Nothing.
397 =back
399 =back
401 =cut
402 ###############################################################################
403 sub trigger_action_reinstall {
404     my ($msg, $msg_hash) = @_;
406         # Check if user should be notificated or not
407         if ($userNotification eq "true") {
408                 # Check logged in user
409                 my @user_list = &get_logged_in_users;
410                 if( @user_list >= 1 ) {
411                         system( "/usr/bin/goto-notify install" );
412                         open(my $FILE, ">", "/etc/gosa-si/event");
413                         print $FILE "install\n";
414                         close($FILE);
415                 }
416         } else {
417                 system( "/sbin/shutdown -r now &" );
418         }
420     return;
424 ###############################################################################
425 =over 
427 =item B<trigger_action_updae>
429 =over
431 =item description 
433     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
435 =item parameter
437     $msg - STRING - complete GOsa-si message
438     $msg_hash - HASHREF - content of GOsa-si message in a hash
440 =item GOsa-si message xml content
442     None.
444 =item return 
445     
446     Nothing
448 =back
450 =back
452 =cut
453 ###############################################################################
454 # Backward compatibility
455 sub trigger_action_update {
456     my ($msg, $msg_hash) = @_;
458     # Execute update
459     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
461     return;
465 ###############################################################################
466 =over 
468 =item B<trigger_action_instant_update ($$)>
470 =over
472 =item description 
474     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
476 =item parameter
478     $msg - STRING - complete GOsa-si message
479     $msg_hash - HASHREF - content of GOsa-si message in a hash
481 =item GOsa-si message xml content
483     None.
485 =item return 
486     
487     Nothing.
489 =back
491 =back
493 =cut
494 ###############################################################################
495 # Backward compatibility
496 sub trigger_action_instant_update {
497     my ($msg, $msg_hash) = @_;
499     # Execute update
500     system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
502     return;
505 sub trigger_goto_settings_reload {
506     my ($msg, $msg_hash) = @_;
508     # Execute goto settings reload
509     my $cmd = "/etc/init.d/goto-agents";
510     my $pram = "start";
511     if (-f $cmd){
512         my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
513     } else {
514         &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
515     }
517     return;
521 1;