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 Data::Random qw(:all);
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
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_file = "/tmp/goto_notify_".join("",rand_chars(set=>'alphanumeric', min=>16, max=>16));
130 open(DATEI, ">$rand_file");
131 print DATEI "source:$source\ntarget:$target\nusr:$to\nsubject:".@{$msg_hash->{'subject'}}[0]."\nmessage:".@{$msg_hash->{'message'}}[0]."\n";
132 close DATEI;
134 my $feedback = system("/usr/bin/goto-notify user-message '$to' '$subject' '$message' '$rand_file' &" );
136 return
137 }
140 ###############################################################################
141 =over
143 =item B<trigger_action_localboot ($$)>
145 =over
147 =item description
149 Executes '/sbin/shutdown -r' if no user is logged in otherwise write
150 'trigger_action_localboot' to '/etc/gosa-si/event'
152 =item parameter
154 $msg - STRING - complete GOsa-si message
155 $msg_hash - HASHREF - content of GOsa-si message in a hash
157 =item GOsa-si message xml content
159 <timeout> - INTEGER - timeout to wait befor restart, default 0
161 =item return
163 Nothing.
165 =back
167 =back
169 =cut
170 ###############################################################################
171 sub trigger_action_localboot {
172 my ($msg, $msg_hash) = @_;
173 my $timeout;
175 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
176 $timeout = -1;
177 }
178 else {
179 $timeout = @{$msg_hash->{timeout}}[0];
180 }
182 # check logged in user
183 my $logged_in_user = 1;
184 if( $logged_in_user ) {
185 # TODO do something
186 }
187 else {
188 $timeout = 0;
189 }
191 # execute function
192 if( $timeout == 0 ) {
193 print STDERR ("shutdown -r +$timeout\n");
194 }
195 elsif( $timeout > 0 ) {
196 print STDERR ("shutdown -r +$timeout\n");
197 }
198 elsif( $timeout < 0 ) {
199 print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
200 open(FILE, "> /etc/gosa-si/event");
201 print FILE "trigger_action_localboot\n";
202 close(FILE);
203 }
204 else {
205 # TODO do something, error handling, logging
206 }
208 return;
209 }
212 ###############################################################################
213 =over
215 =item B<trigger_action_faireboot ($$)>
217 =over
219 =item description
221 Executes '/usr/sbin/faireboot'.
223 =item parameter
225 $msg - STRING - complete GOsa-si message
226 $msg_hash - HASHREF - content of GOsa-si message in a hash
228 =item GOsa-si message xml content
230 None.
232 =item return
234 Nothing.
236 =back
238 =back
240 =cut
241 ###############################################################################
242 sub trigger_action_faireboot {
243 my ($msg, $msg_hash) = @_;
244 &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7);
245 system("/usr/sbin/faireboot");
246 return;
247 }
250 ###############################################################################
251 =over
253 =item B<trigger_action_reboot ($$)>
255 =over
257 =item description
259 Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r' if no
260 user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
262 =item parameter
264 $msg - STRING - complete GOsa-si message
265 $msg_hash - HASHREF - content of GOsa-si message in a hash
267 =item GOsa-si message xml content
269 <timeout> - INTEGER - timeout to wait befor reboot, default 0
271 =item return
273 Nothing.
275 =back
277 =back
279 =cut
280 ###############################################################################
281 sub trigger_action_reboot {
282 my ($msg, $msg_hash) = @_;
283 my $timeout;
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 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 else {
301 system( "/sbin/shutdown -r +$timeout &" );
302 }
304 return;
305 }
308 ###############################################################################
309 =over
311 =item B<trigger_action_halt ($$)>
313 =over
315 =item description
317 Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if no
318 user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
320 =item parameter
322 $msg - STRING - complete GOsa-si message
323 $msg_hash - HASHREF - content of GOsa-si message in a hash
325 =item GOsa-si message xml content
327 <timeout> - INTEGER - timeout to wait befor halt, default 0
329 =item return
331 Nothing.
333 =back
335 =back
337 =cut
338 ###############################################################################
339 sub trigger_action_halt {
340 my ($msg, $msg_hash) = @_;
341 my $timeout;
343 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
344 $timeout = 0;
345 }
346 else {
347 $timeout = @{$msg_hash->{timeout}}[0];
348 }
350 # check logged in user
351 my @user_list = &get_logged_in_users;
352 if( @user_list >= 1 ) {
353 system( "/usr/bin/goto-notify halt" );
354 open(FILE, "> /etc/gosa-si/event");
355 print FILE "halt\n";
356 close(FILE);
357 }
358 else {
359 system( "/sbin/shutdown -h +$timeout &" );
360 }
362 return;
363 }
366 ###############################################################################
367 =over
369 =item B<trigger_action_reinstall>
371 =over
373 =item description
375 Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no
376 user is logged in otherwise write 'install' to '/etc/gosa-si/event'
378 =item parameter
380 $msg - STRING - complete GOsa-si message
381 $msg_hash - HASHREF - content of GOsa-si message in a hash
383 =item GOsa-si message xml content
385 None.
387 =item return
389 Nothing.
391 =back
393 =back
395 =cut
396 ###############################################################################
397 sub trigger_action_reinstall {
398 my ($msg, $msg_hash) = @_;
400 # check logged in user
401 my @user_list = &get_logged_in_users;
402 if( @user_list >= 1 ) {
403 system( "/usr/bin/goto-notify install" );
404 open(FILE, "> /etc/gosa-si/event");
405 print FILE "install\n";
406 close(FILE);
407 }
408 else {
409 system( "/sbin/shutdown -r now &" );
410 }
412 return;
413 }
416 ###############################################################################
417 =over
419 =item B<trigger_action_updae>
421 =over
423 =item description
425 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
427 =item parameter
429 $msg - STRING - complete GOsa-si message
430 $msg_hash - HASHREF - content of GOsa-si message in a hash
432 =item GOsa-si message xml content
434 None.
436 =item return
438 Nothing
440 =back
442 =back
444 =cut
445 ###############################################################################
446 # Backward compatibility
447 sub trigger_action_update {
448 my ($msg, $msg_hash) = @_;
450 # Execute update
451 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
453 return;
454 }
457 ###############################################################################
458 =over
460 =item B<trigger_action_instant_update ($$)>
462 =over
464 =item description
466 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
468 =item parameter
470 $msg - STRING - complete GOsa-si message
471 $msg_hash - HASHREF - content of GOsa-si message in a hash
473 =item GOsa-si message xml content
475 None.
477 =item return
479 Nothing.
481 =back
483 =back
485 =cut
486 ###############################################################################
487 # Backward compatibility
488 sub trigger_action_instant_update {
489 my ($msg, $msg_hash) = @_;
491 # Execute update
492 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
494 return;
495 }
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;
510 }
513 1;