1 package GosaPackages;
3 use Exporter;
4 @ISA = ("Exporter");
6 use strict;
7 use warnings;
8 use GOSA::GosaSupportDaemon;
9 use IO::Socket::INET;
10 use Socket;
11 use XML::Simple;
12 use File::Spec;
13 use Data::Dumper;
14 use MIME::Base64;
16 my $event_dir = "/usr/lib/gosa-si/server/GosaPackages";
17 use lib "/usr/lib/gosa-si/server/GosaPackages";
19 BEGIN{}
20 END{}
22 my $network_interface;
23 my $gosa_mac_address;
25 ## START ##########################
27 $network_interface= &get_interface_for_ip($main::server_ip);
28 $gosa_mac_address= &get_mac($network_interface);
30 # complete addresses
31 if( inet_aton($main::server_ip) ){ $main::server_ip = inet_ntoa(inet_aton($main::server_ip)); }
32 $main::server_address = $main::server_ip.":".$main::server_port;
36 # import local events
37 my ($error, $result, $event_hash) = &import_events($event_dir);
38 foreach my $log_line (@$result) {
39 if ($log_line =~ / succeed: /) {
40 &main::daemon_log("0 INFO: GosaPackages - $log_line", 5);
41 } else {
42 &main::daemon_log("0 ERROR: GosaPackages - $log_line", 1);
43 }
44 }
46 # build vice versa event_hash, event_name => module
47 my $event2module_hash = {};
48 while (my ($module, $mod_events) = each %$event_hash) {
49 while (my ($event_name, $nothing) = each %$mod_events) {
50 $event2module_hash->{$event_name} = $module;
51 }
53 }
56 ## FUNCTIONS #################################################################
58 sub get_module_info {
59 my @info = ($main::gosa_address,
60 $main::gosa_passwd,
61 $event_hash,
62 );
63 return \@info;
64 }
67 #=== FUNCTION ================================================================
68 # NAME: get_mac
69 # PARAMETERS: interface name (i.e. eth0)
70 # RETURNS: (mac address)
71 # DESCRIPTION: Uses ioctl to get mac address directly from system.
72 #===============================================================================
73 sub get_mac {
74 my $ifreq= shift;
75 my $result;
76 if ($ifreq && length($ifreq) > 0) {
77 if($ifreq eq "all") {
78 $result = "00:00:00:00:00:00";
79 } else {
80 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
82 # A configured MAC Address should always override a guessed value
83 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
84 $result= $gosa_mac_address;
85 }
87 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
88 or die "socket: $!";
90 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
91 my ($if, $mac)= unpack 'h36 H12', $ifreq;
93 if (length($mac) > 0) {
94 $mac=~ m/^([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/;
95 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
96 $result = $mac;
97 }
98 }
99 }
100 }
101 return $result;
102 }
105 #=== FUNCTION ================================================================
106 # NAME: process_incoming_msg
107 # PARAMETERS: crypted_msg - string - incoming crypted message
108 # RETURNS: nothing
109 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
110 #===============================================================================
111 sub process_incoming_msg {
112 my ($msg, $msg_hash, $session_id) = @_ ;
113 my $header = @{$msg_hash->{header}}[0];
114 my @msg_l;
115 my @out_msg_l;
117 &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 26);
119 if ($header =~ /^job_/) {
120 @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
121 }
122 elsif ($header =~ /^gosa_/) {
123 @msg_l = &process_gosa_msg($msg, $msg_hash, $session_id);
124 }
125 else {
126 &main::daemon_log("$session_id ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix", 1);
127 }
129 foreach my $out_msg ( @msg_l ) {
130 # determine the correct outgoing source address to the corresponding target address
131 $out_msg =~ /<target>(\S*)<\/target>/;
132 my $act_target = $1;
133 $act_target =~ s/GOSA/$main::server_address/;
134 my $act_server_ip = &main::get_local_ip_for_remote_ip(sprintf("%s", $act_target =~ /^([0-9\.]*?):.*$/));
136 # Patch the correct outgoing source address
137 if ($out_msg =~ /<source>GOSA<\/source>/ ) {
138 $out_msg =~ s/<source>GOSA<\/source>/<source>$act_server_ip:$main::server_port<\/source>/g;
139 }
141 # Add to each outgoing message the current POE session id
142 $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
145 if (defined $out_msg){
146 push(@out_msg_l, $out_msg);
147 }
148 }
150 return \@out_msg_l;
151 }
154 sub process_gosa_msg {
155 my ($msg, $msg_hash, $session_id) = @_ ;
156 my $out_msg;
157 my @out_msg_l = ('nohandler');
158 my $sql_events;
160 # strip gosa_ prefix from header, it is not used any more
161 @{$msg_hash->{'header'}}[0] =~ s/gosa_//;
162 $msg =~ s/<header>gosa_/<header>/;
164 my $header = @{$msg_hash->{'header'}}[0];
165 my $target = @{$msg_hash->{'target'}}[0];
167 # check local installed events
168 if( exists $event2module_hash->{$header} ) {
169 # a event exists with the header as name
170 &main::daemon_log("$session_id DEBUG: found event '$header' at event-module '".$event2module_hash->{$header}."'", 26);
171 no strict 'refs';
172 @out_msg_l = &{$event2module_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
174 # check client registered events
175 } else {
176 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$target') OR (hostname='$target') )";
177 my $res = $main::known_clients_db->select_dbentry( $sql_events );
178 my $l = keys(%$res);
180 # set error if no or more than 1 hits are found for sql query
181 if ( $l != 1) {
182 @out_msg_l = ('knownclienterror');
183 # found exact 1 hit in db
184 } else {
185 my $client_events = $res->{'1'}->{'events'};
187 # client is registered for this event, deliver this message to client
188 if (($client_events =~ /^$header,/) || ($client_events =~ /,$header,/) || ($client_events =~ /,$header$/)) {
189 &main::daemon_log("$session_id DEBUg: client '$target' is registerd for event '$header', forward message to client.", 26);
190 @out_msg_l = ( $msg );
192 # client is not registered for this event, set error
193 } else {
194 @out_msg_l = ('noeventerror');
195 }
196 }
197 }
199 # if delivery not possible raise error and return
200 if (not defined $out_msg_l[0]) {
201 @out_msg_l = ();
202 } elsif ($out_msg_l[0] eq 'nohandler') {
203 &main::daemon_log("$session_id ERROR: GosaPackages: no event handler or core function defined for '$header'", 1);
204 @out_msg_l = ();
205 } elsif ($out_msg_l[0] eq 'knownclienterror') {
206 if ($header eq "ping") {
207 &main::daemon_log("$session_id WARNING: Cannot send '$header' to '$target'. GOsa-si do not know client. Maybe client is offline or gosa-si-client process is not running.", 3);
208 } else {
209 &main::daemon_log("$session_id ERROR: no general event handler found for '$header', check client registration events!", 1);
210 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
211 &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
212 }
213 @out_msg_l = ();
214 } elsif ($out_msg_l[0] eq 'noeventerror') {
215 &main::daemon_log("$session_id ERROR: no general event handler found for '$header', check client registration events!", 1);
216 &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1);
217 @out_msg_l = ();
218 }
220 return @out_msg_l;
221 }
224 sub process_job_msg {
225 my ($msg, $msg_hash, $session_id)= @_ ;
226 my $out_msg;
227 my $error = 0;
229 my $header = @{$msg_hash->{'header'}}[0];
230 $header =~ s/job_//;
231 my $target = @{$msg_hash->{'target'}}[0];
233 # If no timestamp is specified, use 19700101000000
234 my $timestamp = "19700101000000";
235 if( exists $msg_hash->{'timestamp'} ) {
236 $timestamp = @{$msg_hash->{'timestamp'}}[0];
237 }
239 # If no macaddress is specified, raise error
240 my $macaddress;
241 if( exists $msg_hash->{'macaddress'} ) {
242 $macaddress = @{$msg_hash->{'macaddress'}}[0];
243 } elsif (@{$msg_hash->{'target'}}[0] =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i ) {
244 $macaddress = $1;
245 } else {
246 $error ++;
247 $out_msg = "<xml>".
248 "<header>answer</header>".
249 "<source>$main::server_address</source>".
250 "<target>GOSA</target>".
251 "<answer1>1</answer1>".
252 "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
253 "</xml>";
255 return ($out_msg);
256 }
258 # Determine plain_name for host
259 my $plain_name;
260 if ($header eq "opsi_install_client") { # Opsi installing clients use hostId as plain_name
261 if (not exists $msg_hash->{'hostId'}) {
262 $error++;
263 &daemon_log("$session_id ERROR: opsi_install_client-message has no xml-tag 'hostID', job was not created: $msg", 1);
264 } else {
265 $plain_name = $msg_hash->{'hostId'}[0];
266 $header = "trigger_action_reinstall"
267 }
269 } else { # Try to determine plain_name via ldap search
270 my $ldap_handle=&main::get_ldap_handle();
271 my $mesg = $ldap_handle->search(
272 base => $main::ldap_base,
273 scope => 'sub',
274 attrs => ['cn'],
275 filter => "(macAddress=$macaddress)");
276 if($mesg->code || ($mesg->count!=1)) {
277 &main::daemon_log($mesg->error, 1);
278 $plain_name = "none";
279 } else {
280 my $entry= $mesg->entry(0);
281 $plain_name = $entry->get_value("cn");
282 }
283 &main::release_ldap_handle($ldap_handle);
284 }
286 # Check if it is a periodical job
287 my $periodic = 'none';
288 if (exists $msg_hash->{periodic})
289 {
290 $periodic = $msg_hash->{periodic}[0];
291 if (not $periodic =~ /[0-9]+_(hours|minutes|days|weeks|months)/) # Periodic tag is not valid
292 {
293 &main::daemon_log("$session_id ERROR: Message contains invalid periodic-tag '$periodic'.".
294 " Please use the following pattern for the tag: 'INTEGER_[minutes|hours|days|weeks|months]'".
295 " Aborted message: $msg", 1);
296 $out_msg = "<xml>".
297 "<header>answer</header><source>$main::server_address</source><target>GOSA</target>".
298 "<answer1>1</answer1><error_string>Message contains invalid periodic-tag '$periodic'</error_string>".
299 "</xml>";
300 return ($out_msg);
301 }
302 }
304 # Add job to job queue
305 if( $error == 0 ) {
306 my $func_dic = {table=>$main::job_queue_tn,
307 primkey=>['macaddress', 'headertag'],
308 timestamp=>$timestamp,
309 status=>'waiting',
310 result=>'none',
311 progress=>'none',
312 headertag=>$header,
313 targettag=>$target,
314 xmlmessage=>$msg,
315 macaddress=>$macaddress,
316 plainname=>$plain_name,
317 siserver=>"localhost",
318 modified=>"1",
319 periodic=>$periodic,
320 };
321 my $res = $main::job_db->add_dbentry($func_dic);
322 if (not $res == 0) {
323 &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1);
324 } else {
325 &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
326 }
327 $out_msg = "<xml><header>answer</header><source>$main::server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
328 }
330 my @out_msg_l = ( $out_msg );
331 return @out_msg_l;
332 }
334 # vim:ts=4:shiftwidth:expandtab
335 1;