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 ###############################################################################
57 =over
59 =item B<get_events ()>
61 =over
63 =item description
65 Reports all provided functions.
67 =item parameter
69 None.
71 =item return
73 \@events - ARRAYREF - array containing all functions
75 =back
77 =back
79 =cut
80 ###############################################################################
81 sub get_events { return \@events; }
84 ###############################################################################
85 =over
87 =item B<usr_msg ($$)>
89 =over
91 =item description
93 Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
95 =item parameter
97 $msg - STRING - complete GOsa-si message
98 $msg_hash - HASHREF - content of GOsa-si message in a hash
100 =item GOsa-si message xml content
102 <to> - STRING - username message should be deliverd to
103 <subject> - STRING - subject of the message, base64 encoded
104 <message> - STRING - message itself, base64 encoded
106 =item return
108 $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
110 =back
112 =back
114 =cut
115 ###############################################################################
116 sub usr_msg {
117 my ($msg, $msg_hash) = @_;
118 my $header = @{$msg_hash->{'header'}}[0];
119 my $source = @{$msg_hash->{'source'}}[0];
120 my $target = @{$msg_hash->{'target'}}[0];
122 my $to = @{$msg_hash->{'usr'}}[0];
123 my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
124 my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
126 my ($rand_fh, $rand_file) = tempfile( SUFFIX => '.goto_notify');
127 print $rand_fh "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
128 close $rand_fh;
130 my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' &" );
132 return
133 }
136 ###############################################################################
137 =over
139 =item B<trigger_action_localboot ($$)>
141 =over
143 =item description
145 Executes '/sbin/shutdown -r' if no user is logged in otherwise write
146 'trigger_action_localboot' to '/etc/gosa-si/event'
148 =item parameter
150 $msg - STRING - complete GOsa-si message
151 $msg_hash - HASHREF - content of GOsa-si message in a hash
153 =item GOsa-si message xml content
155 <timeout> - INTEGER - timeout to wait befor restart, default 0
157 =item return
159 Nothing.
161 =back
163 =back
165 =cut
166 ###############################################################################
167 sub trigger_action_localboot {
168 my ($msg, $msg_hash) = @_;
169 my $timeout;
171 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
172 $timeout = -1;
173 }
174 else {
175 $timeout = @{$msg_hash->{timeout}}[0];
176 }
178 # check logged in user
179 my $logged_in_user = 1;
180 if( $logged_in_user ) {
181 # TODO do something
182 }
183 else {
184 $timeout = 0;
185 }
187 # execute function
188 if( $timeout == 0 ) {
189 print STDERR ("shutdown -r +$timeout\n");
190 }
191 elsif( $timeout > 0 ) {
192 print STDERR ("shutdown -r +$timeout\n");
193 }
194 elsif( $timeout < 0 ) {
195 print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
196 open(FILE, "> /etc/gosa-si/event");
197 print FILE "trigger_action_localboot\n";
198 close(FILE);
199 }
200 else {
201 # TODO do something, error handling, logging
202 }
204 return;
205 }
208 ###############################################################################
209 =over
211 =item B<trigger_action_faireboot ($$)>
213 =over
215 =item description
217 Executes '/usr/sbin/faireboot'.
219 =item parameter
221 $msg - STRING - complete GOsa-si message
222 $msg_hash - HASHREF - content of GOsa-si message in a hash
224 =item GOsa-si message xml content
226 None.
228 =item return
230 Nothing.
232 =back
234 =back
236 =cut
237 ###############################################################################
238 sub trigger_action_faireboot {
239 my ($msg, $msg_hash) = @_;
240 &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7);
241 system("/usr/sbin/faireboot");
242 return;
243 }
246 ###############################################################################
247 =over
249 =item B<trigger_action_reboot ($$)>
251 =over
253 =item description
255 Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r' if no
256 user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
258 =item parameter
260 $msg - STRING - complete GOsa-si message
261 $msg_hash - HASHREF - content of GOsa-si message in a hash
263 =item GOsa-si message xml content
265 <timeout> - INTEGER - timeout to wait befor reboot, default 0
267 =item return
269 Nothing.
271 =back
273 =back
275 =cut
276 ###############################################################################
277 sub trigger_action_reboot {
278 my ($msg, $msg_hash) = @_;
279 my $timeout;
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 logged in user
289 my @user_list = &get_logged_in_users;
290 if( @user_list >= 1 ) {
291 system( "/usr/bin/goto-notify reboot" );
292 open(FILE, "> /etc/gosa-si/event");
293 print FILE "reboot\n";
294 close(FILE);
295 }
296 else {
297 system( "/sbin/shutdown -r +$timeout &" );
298 }
300 return;
301 }
304 ###############################################################################
305 =over
307 =item B<trigger_action_halt ($$)>
309 =over
311 =item description
313 Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if no
314 user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
316 =item parameter
318 $msg - STRING - complete GOsa-si message
319 $msg_hash - HASHREF - content of GOsa-si message in a hash
321 =item GOsa-si message xml content
323 <timeout> - INTEGER - timeout to wait befor halt, default 0
325 =item return
327 Nothing.
329 =back
331 =back
333 =cut
334 ###############################################################################
335 sub trigger_action_halt {
336 my ($msg, $msg_hash) = @_;
337 my $timeout;
339 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
340 $timeout = 0;
341 }
342 else {
343 $timeout = @{$msg_hash->{timeout}}[0];
344 }
346 # check logged in user
347 my @user_list = &get_logged_in_users;
348 if( @user_list >= 1 ) {
349 system( "/usr/bin/goto-notify halt" );
350 open(FILE, "> /etc/gosa-si/event");
351 print FILE "halt\n";
352 close(FILE);
353 }
354 else {
355 system( "/sbin/shutdown -h +$timeout &" );
356 }
358 return;
359 }
362 ###############################################################################
363 =over
365 =item B<trigger_action_reinstall>
367 =over
369 =item description
371 Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no
372 user is logged in otherwise write 'install' to '/etc/gosa-si/event'
374 =item parameter
376 $msg - STRING - complete GOsa-si message
377 $msg_hash - HASHREF - content of GOsa-si message in a hash
379 =item GOsa-si message xml content
381 None.
383 =item return
385 Nothing.
387 =back
389 =back
391 =cut
392 ###############################################################################
393 sub trigger_action_reinstall {
394 my ($msg, $msg_hash) = @_;
396 # check logged in user
397 my @user_list = &get_logged_in_users;
398 if( @user_list >= 1 ) {
399 system( "/usr/bin/goto-notify install" );
400 open(FILE, "> /etc/gosa-si/event");
401 print FILE "install\n";
402 close(FILE);
403 }
404 else {
405 system( "/sbin/shutdown -r now &" );
406 }
408 return;
409 }
412 ###############################################################################
413 =over
415 =item B<trigger_action_updae>
417 =over
419 =item description
421 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
423 =item parameter
425 $msg - STRING - complete GOsa-si message
426 $msg_hash - HASHREF - content of GOsa-si message in a hash
428 =item GOsa-si message xml content
430 None.
432 =item return
434 Nothing
436 =back
438 =back
440 =cut
441 ###############################################################################
442 # Backward compatibility
443 sub trigger_action_update {
444 my ($msg, $msg_hash) = @_;
446 # Execute update
447 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
449 return;
450 }
453 ###############################################################################
454 =over
456 =item B<trigger_action_instant_update ($$)>
458 =over
460 =item description
462 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
464 =item parameter
466 $msg - STRING - complete GOsa-si message
467 $msg_hash - HASHREF - content of GOsa-si message in a hash
469 =item GOsa-si message xml content
471 None.
473 =item return
475 Nothing.
477 =back
479 =back
481 =cut
482 ###############################################################################
483 # Backward compatibility
484 sub trigger_action_instant_update {
485 my ($msg, $msg_hash) = @_;
487 # Execute update
488 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
490 return;
491 }
493 sub trigger_goto_settings_reload {
494 my ($msg, $msg_hash) = @_;
496 # Execute goto settings reload
497 my $cmd = "/etc/init.d/goto-agents";
498 my $pram = "start";
499 if (-f $cmd){
500 my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
501 } else {
502 &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
503 }
505 return;
506 }
509 1;