Code

ed19d312083cee6c4a816f4582209845049a6b31
[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         open(FILE, "> /etc/gosa-si/event");
390         print FILE "install\n";
391         close(FILE);
392     }
394     system( "/usr/sbin/goto-action &" );
396     return;
400 ###############################################################################
401 =over 
403 =item B<trigger_action_updae>
405 =over
407 =item description 
409     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
411 =item parameter
413     $msg - STRING - complete GOsa-si message
414     $msg_hash - HASHREF - content of GOsa-si message in a hash
416 =item GOsa-si message xml content
418     None.
420 =item return 
421     
422     Nothing
424 =back
426 =back
428 =cut
429 ###############################################################################
430 # Backward compatibility
431 sub trigger_action_update {
432     my ($msg, $msg_hash) = @_;
434     # check logged in user
435     my @user_list = &get_logged_in_users;
436     if( @user_list >= 1 ) {
437         system( "/usr/bin/goto-notify softupdate" );
438         open(FILE, "> /etc/gosa-si/event");
439         print FILE "softupdate\n";
440         close(FILE);
441     }
443     system( "/usr/sbin/goto-action &" );
445     return;
449 ###############################################################################
450 =over 
452 =item B<trigger_action_instant_update ($$)>
454 =over
456 =item description 
458     Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
460 =item parameter
462     $msg - STRING - complete GOsa-si message
463     $msg_hash - HASHREF - content of GOsa-si message in a hash
465 =item GOsa-si message xml content
467     None.
469 =item return 
470     
471     Nothing.
473 =back
475 =back
477 =cut
478 ###############################################################################
479 # Backward compatibility
480 sub trigger_action_instant_update {
481     my ($msg, $msg_hash) = @_;
483     # check logged in user
484     my @user_list = &get_logged_in_users;
485     if( @user_list >= 1 ) {
486         system( "/usr/bin/goto-notify softupdate" );
487         open(FILE, "> /etc/gosa-si/event");
488         print FILE "softupdate\n";
489         close(FILE);
490     }
492     system( "/usr/sbin/goto-action &" );
494     return;
497 sub trigger_goto_settings_reload {
498     my ($msg, $msg_hash) = @_;
500     # Execute goto settings reload
501     my $cmd = "/etc/init.d/goto-agents";
502     my $pram = "start";
503     if (-f $cmd){
504         my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
505     } else {
506         &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
507     }
509     return;
513 1;