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;
54 BEGIN {}
56 END {}
58 ###############################################################################
59 =over
61 =item B<get_events ()>
63 =over
65 =item description
67 Reports all provided functions.
69 =item parameter
71 None.
73 =item return
75 \@events - ARRAYREF - array containing all functions
77 =back
79 =back
81 =cut
82 ###############################################################################
83 sub get_events { return \@events; }
86 ###############################################################################
87 =over
89 =item B<usr_msg ($$)>
91 =over
93 =item description
95 Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
97 =item parameter
99 $msg - STRING - complete GOsa-si message
100 $msg_hash - HASHREF - content of GOsa-si message in a hash
102 =item GOsa-si message xml content
104 <to> - STRING - username message should be deliverd to
105 <subject> - STRING - subject of the message, base64 encoded
106 <message> - STRING - message itself, base64 encoded
108 =item return
110 $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
112 =back
114 =back
116 =cut
117 ###############################################################################
118 sub usr_msg {
119 my ($msg, $msg_hash) = @_;
120 my $header = @{$msg_hash->{'header'}}[0];
121 my $source = @{$msg_hash->{'source'}}[0];
122 my $target = @{$msg_hash->{'target'}}[0];
124 my $to = @{$msg_hash->{'usr'}}[0];
125 my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
126 my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
128 system( "/usr/bin/goto-notify user-message '$to' '$subject' '$message'" );
130 # give gosa-si-server feedback, that msg was received
131 $msg =~ s/<header>usr_msg<\/header>/<header>confirm_usr_msg<\/header>/g;
132 my $out_hash = &create_xml_hash("confirm_usr_msg", $target, $source);
133 &add_content2xml_hash($out_hash, 'usr', $to);
134 &add_content2xml_hash($out_hash, 'subject', @{$msg_hash->{'subject'}}[0]);
135 &add_content2xml_hash($out_hash, 'message', @{$msg_hash->{'message'}}[0]);
138 return &create_xml_string($out_hash);
139 }
142 ###############################################################################
143 =over
145 =item B<trigger_action_localboot ($$)>
147 =over
149 =item description
151 Executes '/sbin/shutdown -r' if no user is logged in otherwise write
152 'trigger_action_localboot' to '/etc/gosa-si/event'
154 =item parameter
156 $msg - STRING - complete GOsa-si message
157 $msg_hash - HASHREF - content of GOsa-si message in a hash
159 =item GOsa-si message xml content
161 <timeout> - INTEGER - timeout to wait befor restart, default 0
163 =item return
165 Nothing.
167 =back
169 =back
171 =cut
172 ###############################################################################
173 sub trigger_action_localboot {
174 my ($msg, $msg_hash) = @_;
175 my $timeout;
177 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
178 $timeout = -1;
179 }
180 else {
181 $timeout = @{$msg_hash->{timeout}}[0];
182 }
184 # check logged in user
185 my $logged_in_user = 1;
186 if( $logged_in_user ) {
187 # TODO do something
188 }
189 else {
190 $timeout = 0;
191 }
193 # execute function
194 if( $timeout == 0 ) {
195 print STDERR ("shutdown -r +$timeout\n");
196 }
197 elsif( $timeout > 0 ) {
198 print STDERR ("shutdown -r +$timeout\n");
199 }
200 elsif( $timeout < 0 ) {
201 print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
202 open(FILE, "> /etc/gosa-si/event");
203 print FILE "trigger_action_localboot\n";
204 close(FILE);
205 }
206 else {
207 # TODO do something, error handling, logging
208 }
210 return;
211 }
214 ###############################################################################
215 =over
217 =item B<trigger_action_faireboot ($$)>
219 =over
221 =item description
223 Executes '/usr/sbin/faireboot'.
225 =item parameter
227 $msg - STRING - complete GOsa-si message
228 $msg_hash - HASHREF - content of GOsa-si message in a hash
230 =item GOsa-si message xml content
232 None.
234 =item return
236 Nothing.
238 =back
240 =back
242 =cut
243 ###############################################################################
244 sub trigger_action_faireboot {
245 my ($msg, $msg_hash) = @_;
246 &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7);
247 system("/usr/sbin/faireboot");
248 return;
249 }
252 ###############################################################################
253 =over
255 =item B<trigger_action_reboot ($$)>
257 =over
259 =item description
261 Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r' if no
262 user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
264 =item parameter
266 $msg - STRING - complete GOsa-si message
267 $msg_hash - HASHREF - content of GOsa-si message in a hash
269 =item GOsa-si message xml content
271 <timeout> - INTEGER - timeout to wait befor reboot, default 0
273 =item return
275 Nothing.
277 =back
279 =back
281 =cut
282 ###############################################################################
283 sub trigger_action_reboot {
284 my ($msg, $msg_hash) = @_;
285 my $timeout;
287 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
288 $timeout = 0;
289 }
290 else {
291 $timeout = @{$msg_hash->{timeout}}[0];
292 }
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(FILE, "> /etc/gosa-si/event");
299 print FILE "reboot\n";
300 close(FILE);
301 }
302 else {
303 system( "/sbin/shutdown -r +$timeout &" );
304 }
306 return;
307 }
310 ###############################################################################
311 =over
313 =item B<trigger_action_halt ($$)>
315 =over
317 =item description
319 Executes '/usr/bin/goto-notify halt' and '/sbin/shutdown -h' if no
320 user is logged in otherwise write 'halt' to '/etc/gosa-si/event'
322 =item parameter
324 $msg - STRING - complete GOsa-si message
325 $msg_hash - HASHREF - content of GOsa-si message in a hash
327 =item GOsa-si message xml content
329 <timeout> - INTEGER - timeout to wait befor halt, default 0
331 =item return
333 Nothing.
335 =back
337 =back
339 =cut
340 ###############################################################################
341 sub trigger_action_halt {
342 my ($msg, $msg_hash) = @_;
343 my $timeout;
345 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
346 $timeout = 0;
347 }
348 else {
349 $timeout = @{$msg_hash->{timeout}}[0];
350 }
352 # check logged in user
353 my @user_list = &get_logged_in_users;
354 if( @user_list >= 1 ) {
355 system( "/usr/bin/goto-notify halt" );
356 open(FILE, "> /etc/gosa-si/event");
357 print FILE "halt\n";
358 close(FILE);
359 }
360 else {
361 system( "/sbin/shutdown -h +$timeout &" );
362 }
364 return;
365 }
368 ###############################################################################
369 =over
371 =item B<trigger_action_reinstall>
373 =over
375 =item description
377 Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no
378 user is logged in otherwise write 'install' to '/etc/gosa-si/event'
380 =item parameter
382 $msg - STRING - complete GOsa-si message
383 $msg_hash - HASHREF - content of GOsa-si message in a hash
385 =item GOsa-si message xml content
387 None.
389 =item return
391 Nothing.
393 =back
395 =back
397 =cut
398 ###############################################################################
399 sub trigger_action_reinstall {
400 my ($msg, $msg_hash) = @_;
402 # check logged in user
403 my @user_list = &get_logged_in_users;
404 if( @user_list >= 1 ) {
405 system( "/usr/bin/goto-notify install" );
406 open(FILE, "> /etc/gosa-si/event");
407 print FILE "install\n";
408 close(FILE);
409 }
410 else {
411 system( "/sbin/shutdown -r now &" );
412 }
414 return;
415 }
418 ###############################################################################
419 =over
421 =item B<trigger_action_updae>
423 =over
425 =item description
427 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
429 =item parameter
431 $msg - STRING - complete GOsa-si message
432 $msg_hash - HASHREF - content of GOsa-si message in a hash
434 =item GOsa-si message xml content
436 None.
438 =item return
440 Nothing
442 =back
444 =back
446 =cut
447 ###############################################################################
448 # Backward compatibility
449 sub trigger_action_update {
450 my ($msg, $msg_hash) = @_;
452 # Execute update
453 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
455 return;
456 }
459 ###############################################################################
460 =over
462 =item B<trigger_action_instant_update ($$)>
464 =over
466 =item description
468 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
470 =item parameter
472 $msg - STRING - complete GOsa-si message
473 $msg_hash - HASHREF - content of GOsa-si message in a hash
475 =item GOsa-si message xml content
477 None.
479 =item return
481 Nothing.
483 =back
485 =back
487 =cut
488 ###############################################################################
489 # Backward compatibility
490 sub trigger_action_instant_update {
491 my ($msg, $msg_hash) = @_;
493 # Execute update
494 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
496 return;
497 }
499 sub trigger_goto_settings_reload {
500 my ($msg, $msg_hash) = @_;
502 # Execute goto settings reload
503 my $cmd = "/etc/init.d/goto-agents";
504 my $pram = "start";
505 if (-f $cmd){
506 my $feedback = system("$cmd $pram") or &main::daemon_log("ERROR: $@");
507 } else {
508 &main::daemon_log("ERROR: cannot exec $cmd, file not found!");
509 }
511 return;
512 }
515 1;