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 );
45 @EXPORT = @events;
47 use strict;
48 use warnings;
49 use GOSA::GosaSupportDaemon;
50 use Data::Dumper;
51 use MIME::Base64;
53 BEGIN {}
55 END {}
57 ###############################################################################
58 =over
60 =item B<get_events ()>
62 =over
64 =item description
66 Reports all provided functions.
68 =item parameter
70 None.
72 =item return
74 ARRAYREF - array containing all functions
76 =back
78 =back
80 =cut
81 ###############################################################################
82 sub get_events { return \@events; }
85 ###############################################################################
86 =over
88 =item B<usr_msg ($$)>
90 =over
92 =item description
94 Executes '/usr/bin/goto-notify' wich displays the message, subject und receiver at screen
96 =item parameter
98 $msg - STRING - complete GOsa-si message
99 $msg_hash - HASHREF - content of GOsa-si message in a hash
101 =item GOsa-si message xml content
103 <to> - STRING - username message should be deliverd to
104 <subject> - STRING - subject of the message, base64 encoded
105 <message> - STRING - message itself, base64 encoded
107 =item return
109 $out_msg - STRING - GOsa-si valid xml message, feedback that message was deliverd
111 =back
113 =back
115 =cut
116 ###############################################################################
117 sub usr_msg {
118 my ($msg, $msg_hash) = @_;
121 my $to = @{$msg_hash->{'usr'}}[0];
122 my $subject = &decode_base64(@{$msg_hash->{'subject'}}[0]);
123 my $message = &decode_base64(@{$msg_hash->{'message'}}[0]);
124 system( "/usr/bin/goto-notify user-message '$to' '$subject' '$message'" );
126 # give gosa-si-server feedback, that msg was received
127 $msg =~ s/<header>usr_msg<\/header>/<header>confirm_usr_msg<\/header>/g;
128 return $msg;
129 }
132 ###############################################################################
133 =over
135 =item B<trigger_action_localboot ($$)>
137 =over
139 =item description
141 Executes '/sbin/shutdown -r' if no user is logged in otherwise write
142 'trigger_action_localboot' to '/etc/gosa-si/event'
144 =item parameter
146 $msg - STRING - complete GOsa-si message
147 $msg_hash - HASHREF - content of GOsa-si message in a hash
149 =item GOsa-si message xml content
151 <timeout> - INTEGER - timeout to wait befor restart, default 0
153 =item return
155 Nothing.
157 =back
159 =back
161 =cut
162 ###############################################################################
163 sub trigger_action_localboot {
164 my ($msg, $msg_hash) = @_;
165 my $timeout;
167 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
168 $timeout = -1;
169 }
170 else {
171 $timeout = @{$msg_hash->{timeout}}[0];
172 }
174 # check logged in user
175 my $logged_in_user = 1;
176 if( $logged_in_user ) {
177 # TODO do something
178 }
179 else {
180 $timeout = 0;
181 }
183 # execute function
184 if( $timeout == 0 ) {
185 print STDERR ("shutdown -r +$timeout\n");
186 }
187 elsif( $timeout > 0 ) {
188 print STDERR ("shutdown -r +$timeout\n");
189 }
190 elsif( $timeout < 0 ) {
191 print STDERR "The administrator has sent a signal to reboot this workstation. It will reboot after you've logged out.\n";
192 open(FILE, "> /etc/gosa-si/event");
193 print FILE "trigger_action_localboot\n";
194 close(FILE);
195 }
196 else {
197 # TODO do something, error handling, logging
198 }
200 return;
201 }
204 ###############################################################################
205 =over
207 =item B<trigger_action_faireboot ($$)>
209 =over
211 =item description
213 Executes '/usr/sbin/faireboot'.
215 =item parameter
217 $msg - STRING - complete GOsa-si message
218 $msg_hash - HASHREF - content of GOsa-si message in a hash
220 =item GOsa-si message xml content
222 None.
224 =item return
226 Nothing.
228 =back
230 =back
232 =cut
233 ###############################################################################
234 sub trigger_action_faireboot {
235 my ($msg, $msg_hash) = @_;
236 &main::daemon_log("DEBUG: run /usr/sbin/faireboot\n", 7);
237 system("/usr/sbin/faireboot");
238 return;
239 }
242 ###############################################################################
243 =over
245 =item B<trigger_action_reboot ($$)>
247 =over
249 =item description
251 Executes '/usr/bin/goto-notify reboot' and '/sbin/shutdown -r' if no
252 user is logged in otherwise write 'reboot' to '/etc/gosa-si/event'
254 =item parameter
256 $msg - STRING - complete GOsa-si message
257 $msg_hash - HASHREF - content of GOsa-si message in a hash
259 =item GOsa-si message xml content
261 <timeout> - INTEGER - timeout to wait befor reboot, default 0
263 =item return
265 Nothing.
267 =back
269 =back
271 =cut
272 ###############################################################################
273 sub trigger_action_reboot {
274 my ($msg, $msg_hash) = @_;
275 my $timeout;
277 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
278 $timeout = 0;
279 }
280 else {
281 $timeout = @{$msg_hash->{timeout}}[0];
282 }
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 open(FILE, "> /etc/gosa-si/event");
289 print FILE "reboot\n";
290 close(FILE);
291 }
292 else {
293 system( "/sbin/shutdown -r +$timeout &" );
294 }
296 return;
297 }
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
323 Nothing.
325 =back
327 =back
329 =cut
330 ###############################################################################
331 sub trigger_action_halt {
332 my ($msg, $msg_hash) = @_;
333 my $timeout;
335 if((not exists $msg_hash->{timeout} ) || (1 != @{$msg_hash->{timeout}} ) ) {
336 $timeout = 0;
337 }
338 else {
339 $timeout = @{$msg_hash->{timeout}}[0];
340 }
342 # check logged in user
343 my @user_list = &get_logged_in_users;
344 if( @user_list >= 1 ) {
345 system( "/usr/bin/goto-notify halt" );
346 open(FILE, "> /etc/gosa-si/event");
347 print FILE "halt\n";
348 close(FILE);
349 }
350 else {
351 system( "/sbin/shutdown -h +$timeout &" );
352 }
354 return;
355 }
358 ###############################################################################
359 =over
361 =item B<trigger_action_reinstall>
363 =over
365 =item description
367 Executes '/usr/bin/goto-notify install' and '/sbin/shutdown -r now' if no
368 user is logged in otherwise write 'install' to '/etc/gosa-si/event'
370 =item parameter
372 $msg - STRING - complete GOsa-si message
373 $msg_hash - HASHREF - content of GOsa-si message in a hash
375 =item GOsa-si message xml content
377 None.
379 =item return
381 Nothing.
383 =back
385 =back
387 =cut
388 ###############################################################################
389 sub trigger_action_reinstall {
390 my ($msg, $msg_hash) = @_;
392 # check logged in user
393 my @user_list = &get_logged_in_users;
394 if( @user_list >= 1 ) {
395 system( "/usr/bin/goto-notify install" );
396 open(FILE, "> /etc/gosa-si/event");
397 print FILE "install\n";
398 close(FILE);
399 }
400 else {
401 system( "/sbin/shutdown -r now &" );
402 }
404 return;
405 }
408 ###############################################################################
409 =over
411 =item B<trigger_action_updae>
413 =over
415 =item description
417 Executes 'DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &'
419 =item parameter
421 $msg - STRING - complete GOsa-si message
422 $msg_hash - HASHREF - content of GOsa-si message in a hash
424 =item GOsa-si message xml content
426 None.
428 =item return
430 Nothing
432 =back
434 =back
436 =cut
437 ###############################################################################
438 # Backward compatibility
439 sub trigger_action_update {
440 my ($msg, $msg_hash) = @_;
442 # Execute update
443 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
445 return;
446 }
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
471 Nothing.
473 =back
475 =back
477 =cut
478 ###############################################################################
479 # Backward compatibility
480 sub trigger_action_instant_update {
481 my ($msg, $msg_hash) = @_;
483 # Execute update
484 system( "DEBIAN_FRONTEND=noninteractive /usr/sbin/fai-softupdate &" );
486 return;
487 }
490 1;