Code

Trigger actions also when no user is logged in
[gosa.git] / trunk / gosa-si / client / events / gosaTriggered.pm
2 =head1 NAME
4 gosaTriggered.pm
6 =head1 SYNOPSIS
8 use GOSA::GosaSupportDaemon;
10 use Data::Dumper;
12 use MIME::Base64
14 =head1 DESCRIPTION
16 This module contains all GOsa-SI-client processing instructions concerning actions controllable from GOsa.
18 =head1 VERSION
20 Version 1.0
22 =head1 AUTHOR
24 Andreas Rettenberger <rettenberger at gonicus dot de>
26 =head1 FUNCTIONS
28 =cut
31 package gosaTriggered;
32 use Exporter;
33 @ISA = qw(Exporter);
34 my @events = (
35     "get_events",
36     "usr_msg",
37     "trigger_action_localboot",
38     "trigger_action_halt",
39     "trigger_action_faireboot",
40     "trigger_action_reboot",
41     "trigger_action_reinstall",
42     "trigger_action_update",
43     "trigger_action_instant_update",
44     "trigger_goto_settings_reload",
45     );
46 @EXPORT = @events;
48 use strict;
49 use warnings;
50 use GOSA::GosaSupportDaemon;
51 use Data::Dumper;
52 use MIME::Base64;
53 use File::Temp qw/ tempfile/;
55 BEGIN {}
57 END {}
59 ###############################################################################
60 =over 
62 =item B<get_events ()>
64 =over
66 =item description 
68     Reports all provided functions.
70 =item parameter
72     None.
74 =item return 
76     \@events - ARRAYREF - array containing all functions 
78 =back
80 =back
82 =cut
83 ###############################################################################
84 sub get_events { return \@events; }
87 ###############################################################################
88 =over 
90 =item B<usr_msg ($$)>
92 =over
94 =item description 
96     Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
98 =item parameter
100     $msg - STRING - complete GOsa-si message
101     $msg_hash - HASHREF - content of GOsa-si message in a hash
103 =item GOsa-si message xml content
104     
105     <to> - STRING - username message should be deliverd to
106     <subject> - STRING - subject of the message, base64 encoded
107     <message> - STRING - message itself, base64 encoded
109 =item return 
111     $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
113 =back
115 =back
117 =cut
118 ###############################################################################
119 sub usr_msg {
120     my ($msg, $msg_hash) = @_;
121     my $header = @{$msg_hash->{'header'}}[0];
122     my $source = @{$msg_hash->{'source'}}[0];
123     my $target = @{$msg_hash->{'target'}}[0];
125     my $to = @{$msg_hash->{'usr'}}[0];
126     my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
127     my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
129     my ($rand_fh, $rand_file) = tempfile( SUFFIX => '.goto_notify');
130     print $rand_fh "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
131     close $rand_fh;
133     my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' &" );
135     return
139 ###############################################################################
140 =over 
142 =item B<trigger_action_localboot ($$)>
144 =over
146 =item description 
148     Executes '/sbin/shutdown -r' if  no user is logged in otherwise write 
149     'trigger_action_localboot' to '/etc/gosa-si/event'
151 =item parameter
153     $msg - STRING - complete GOsa-si message
154     $msg_hash - HASHREF - content of GOsa-si message in a hash
156 =item GOsa-si message xml content
158     <timeout> - INTEGER - timeout to wait befor restart, default 0
160 =item return 
161     
162     Nothing.
164 =back
166 =back
168 =cut
169 ###############################################################################
170 sub trigger_action_localboot {
171     my ($msg, $msg_hash) = @_;
172     my $timeout;
174     if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
175         $timeout = -1;
176     } 
177     else {
178         $timeout = @{$msg_hash->{timeout}}[0];
179     }
181     # check logged in user
182     my $logged_in_user = 1;
183     if( $logged_in_user ) {
184         # TODO do something
185     }
186     else {
187         $timeout = 0;
188     }
189         
190     # execute function
191     if( $timeout == 0 ) {
192         print STDERR ("shutdown -r +$timeout\n");
193     }
194     elsif( $timeout > 0 ) {
195         print STDERR ("shutdown -r +$timeout\n");
196     }
197     elsif( $timeout < 0 ) {
198         print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
199         open(FILE, "> /etc/gosa-si/event");
200         print FILE "trigger_action_localboot\n";
201         close(FILE);
202     }
203     else {
204         # TODO do something, error handling, logging
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     # check logged in user
285     my @user_list = &get_logged_in_users;
286     if( @user_list >= 1 ) {
287         system( "/usr/bin/goto-notify reboot" );
288     }
290     open(FILE, "> /etc/gosa-si/event");
291     print FILE "reboot\n";
292     close(FILE);
294     system( "/usr/sbin/goto-action &" );
296     return;
300 ###############################################################################
301 =over 
303 =item B<trigger_action_halt ($$)>
305 =over
307 =item description 
309     Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if  no 
310     user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
312 =item parameter
314     $msg - STRING - complete GOsa-si message
315     $msg_hash - HASHREF - content of GOsa-si message in a hash
317 =item GOsa-si message xml content
319     <timeout> - INTEGER - timeout to wait befor halt, default 0
321 =item return 
322     
323     Nothing.    
325 =back
327 =back
329 =cut
330 ###############################################################################
331 sub trigger_action_halt {
332     my ($msg, $msg_hash) = @_;
333     my $timeout;
335     # check logged in user
336     my @user_list = &get_logged_in_users;
337     if( @user_list >= 1 ) {
338         system( "/usr/bin/goto-notify halt" );
339     }
341     open(FILE, "> /etc/gosa-si/event");
342     print FILE "halt\n";
343     close(FILE);
345     system( "/usr/sbin/goto-action &" );
347     return;
351 ###############################################################################
352 =over 
354 =item B<trigger_action_reinstall>
356 =over
358 =item description 
360     Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no 
361     user is logged in otherwise write 'install' to '/etc/gosa-si/event'
363 =item parameter
365     $msg - STRING - complete GOsa-si message
366     $msg_hash - HASHREF - content of GOsa-si message in a hash
368 =item GOsa-si message xml content
370     None.
372 =item return 
373     
374     Nothing.
376 =back
378 =back
380 =cut
381 ###############################################################################
382 sub trigger_action_reinstall {
383     my ($msg, $msg_hash) = @_;
385     # check logged in user
386     my @user_list = &get_logged_in_users;
387     if( @user_list >= 1 ) {
388         system( "/usr/bin/goto-notify install" );
389     }
391     open(FILE, "> /etc/gosa-si/event");
392     print FILE "install\n";
393     close(FILE);
395     system( "/usr/sbin/goto-action &" );
397     return;
401 ###############################################################################
402 =over 
404 =item B<trigger_action_updae>
406 =over
408 =item description 
410     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
412 =item parameter
414     $msg - STRING - complete GOsa-si message
415     $msg_hash - HASHREF - content of GOsa-si message in a hash
417 =item GOsa-si message xml content
419     None.
421 =item return 
422     
423     Nothing
425 =back
427 =back
429 =cut
430 ###############################################################################
431 # Backward compatibility
432 sub trigger_action_update {
433     my ($msg, $msg_hash) = @_;
435     # check logged in user
436     my @user_list = &get_logged_in_users;
437     if( @user_list >= 1 ) {
438         system( "/usr/bin/goto-notify softupdate" );
439     }
441     open(FILE, "> /etc/gosa-si/event");
442     print FILE "softupdate\n";
443     close(FILE);
445     system( "/usr/sbin/goto-action &" );
447     return;
451 ###############################################################################
452 =over 
454 =item B<trigger_action_instant_update ($$)>
456 =over
458 =item description 
460     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
462 =item parameter
464     $msg - STRING - complete GOsa-si message
465     $msg_hash - HASHREF - content of GOsa-si message in a hash
467 =item GOsa-si message xml content
469     None.
471 =item return 
472     
473     Nothing.
475 =back
477 =back
479 =cut
480 ###############################################################################
481 # Backward compatibility
482 sub trigger_action_instant_update {
483     my ($msg, $msg_hash) = @_;
485     # check logged in user
486     my @user_list = &get_logged_in_users;
487     if( @user_list >= 1 ) {
488         system( "/usr/bin/goto-notify softupdate" );
489     }
491     open(FILE, "> /etc/gosa-si/event");
492     print FILE "softupdate\n";
493     close(FILE);
495     system( "/usr/sbin/goto-action &" );
497     return;
500 sub trigger_goto_settings_reload {
501     my ($msg, $msg_hash) = @_;
503     # Execute goto settings reload
504     my $cmd = "/etc/init.d/goto-agents";
505     my $pram = "start";
506     if (-f $cmd){
507         my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
508     } else {
509         &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
510     }
512     return;
516 1;