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 GOSA::DBsqlite;
15 use MIME::Base64;
17 my $event_dir = "/usr/lib/gosa-si/server/GosaPackages";
18 use lib "/usr/lib/gosa-si/server/GosaPackages";
20 BEGIN{}
21 END{}
23 my $network_interface;
24 my $gosa_mac_address;
26 ## START ##########################
28 $network_interface= &get_interface_for_ip($main::server_ip);
29 $gosa_mac_address= &get_mac($network_interface);
31 # complete addresses
32 if( inet_aton($main::server_ip) ){ $main::server_ip = inet_ntoa(inet_aton($main::server_ip)); }
33 $main::server_address = $main::server_ip.":".$main::server_port;
37 # import local events
38 my ($error, $result, $event_hash) = &import_events($event_dir);
39 foreach my $log_line (@$result) {
40 if ($log_line =~ / succeed: /) {
41 &main::daemon_log("0 DEBUG: GosaPackages - $log_line", 7);
42 } else {
43 &main::daemon_log("0 ERROR: GosaPackages - $log_line", 1);
44 }
45 }
47 # build vice versa event_hash, event_name => module
48 my $event2module_hash = {};
49 while (my ($module, $mod_events) = each %$event_hash) {
50 while (my ($event_name, $nothing) = each %$mod_events) {
51 $event2module_hash->{$event_name} = $module;
52 }
54 }
57 ## FUNCTIONS #################################################################
59 sub get_module_info {
60 my @info = ($main::gosa_address,
61 $main::gosa_passwd,
62 $event_hash,
63 );
64 return \@info;
65 }
68 #=== FUNCTION ================================================================
69 # NAME: get_mac
70 # PARAMETERS: interface name (i.e. eth0)
71 # RETURNS: (mac address)
72 # DESCRIPTION: Uses ioctl to get mac address directly from system.
73 #===============================================================================
74 sub get_mac {
75 my $ifreq= shift;
76 my $result;
77 if ($ifreq && length($ifreq) > 0) {
78 if($ifreq eq "all") {
79 $result = "00:00:00:00:00:00";
80 } else {
81 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
83 # A configured MAC Address should always override a guessed value
84 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
85 $result= $gosa_mac_address;
86 }
88 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
89 or die "socket: $!";
91 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
92 my ($if, $mac)= unpack 'h36 H12', $ifreq;
94 if (length($mac) > 0) {
95 $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])$/;
96 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
97 $result = $mac;
98 }
99 }
100 }
101 }
102 return $result;
103 }
106 #=== FUNCTION ================================================================
107 # NAME: process_incoming_msg
108 # PARAMETERS: crypted_msg - string - incoming crypted message
109 # RETURNS: nothing
110 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
111 #===============================================================================
112 sub process_incoming_msg {
113 my ($msg, $msg_hash, $session_id) = @_ ;
114 my $header = @{$msg_hash->{header}}[0];
115 my @msg_l;
116 my @out_msg_l;
118 &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 7);
120 if ($header =~ /^job_/) {
121 @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
122 }
123 elsif ($header =~ /^gosa_/) {
124 @msg_l = &process_gosa_msg($msg, $msg_hash, $session_id);
125 }
126 else {
127 &main::daemon_log("$session_id ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix", 1);
128 }
130 foreach my $out_msg ( @msg_l ) {
131 # determine the correct outgoing source address to the corresponding target address
132 $out_msg =~ /<target>(\S*)<\/target>/;
133 my $act_target = $1;
134 $act_target =~ s/GOSA/$main::server_address/;
135 my $act_server_ip = &main::get_local_ip_for_remote_ip(sprintf("%s", $act_target =~ /^([0-9\.]*?):.*$/));
137 # Patch the correct outgoing source address
138 if ($out_msg =~ /<source>GOSA<\/source>/ ) {
139 $out_msg =~ s/<source>GOSA<\/source>/<source>$act_server_ip:$main::server_port<\/source>/g;
140 }
142 # Add to each outgoing message the current POE session id
143 $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
146 if (defined $out_msg){
147 push(@out_msg_l, $out_msg);
148 }
149 }
151 return \@out_msg_l;
152 }
155 sub process_gosa_msg {
156 my ($msg, $msg_hash, $session_id) = @_ ;
157 my $out_msg;
158 my @out_msg_l = ('nohandler');
159 my $sql_events;
161 # strip gosa_ prefix from header, it is not used any more
162 @{$msg_hash->{'header'}}[0] =~ s/gosa_//;
163 $msg =~ s/<header>gosa_/<header>/;
165 my $header = @{$msg_hash->{'header'}}[0];
166 my $target = @{$msg_hash->{'target'}}[0];
168 # check local installed events
169 if( exists $event2module_hash->{$header} ) {
170 # a event exists with the header as name
171 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event2module_hash->{$header}."'", 5);
172 no strict 'refs';
173 @out_msg_l = &{$event2module_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
175 # check client registered events
176 } else {
177 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$target') OR (hostname='$target') )";
178 my $res = $main::known_clients_db->select_dbentry( $sql_events );
179 my $l = keys(%$res);
181 # set error if no or more than 1 hits are found for sql query
182 if ( $l != 1) {
183 @out_msg_l = ('knownclienterror');
184 # found exact 1 hit in db
185 } else {
186 my $client_events = $res->{'1'}->{'events'};
188 # client is registered for this event, deliver this message to client
189 if ($client_events =~ /,$header,/) {
190 &main::daemon_log("$session_id INFO: client '$target' is registerd for event '$header', forward message to client.", 5);
191 @out_msg_l = ( $msg );
193 # client is not registered for this event, set error
194 } else {
195 @out_msg_l = ('noeventerror');
196 }
197 }
198 }
200 # if delivery not possible raise error and return
201 if (not defined $out_msg_l[0]) {
202 @out_msg_l = ();
203 } elsif ($out_msg_l[0] eq 'nohandler') {
204 &main::daemon_log("$session_id ERROR: GosaPackages: no event handler or core function defined for '$header'", 1);
205 @out_msg_l = ();
206 } elsif ($out_msg_l[0] eq 'knownclienterror') {
207 &main::daemon_log("$session_id ERROR: no event handler found for '$header', check client registration events!", 1);
208 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
209 &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
210 @out_msg_l = ();
211 } elsif ($out_msg_l[0] eq 'noeventerror') {
212 &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1);
213 @out_msg_l = ();
214 }
216 return @out_msg_l;
217 }
220 sub process_job_msg {
221 my ($msg, $msg_hash, $session_id)= @_ ;
222 my $out_msg;
223 my $error = 0;
225 my $header = @{$msg_hash->{'header'}}[0];
226 $header =~ s/job_//;
227 my $target = @{$msg_hash->{'target'}}[0];
229 # If no timestamp is specified, use 19700101000000
230 my $timestamp = "19700101000000";
231 if( exists $msg_hash->{'timestamp'} ) {
232 $timestamp = @{$msg_hash->{'timestamp'}}[0];
233 }
235 # If no macaddress is specified, raise error
236 my $macaddress;
237 if( exists $msg_hash->{'macaddress'} ) {
238 $macaddress = @{$msg_hash->{'macaddress'}}[0];
239 } 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 ) {
240 $macaddress = $1;
241 } else {
242 $error ++;
243 $out_msg = "<xml>".
244 "<header>answer</header>".
245 "<source>$main::server_address</source>".
246 "<target>GOSA</target>".
247 "<answer1>1</answer1>".
248 "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
249 "</xml>";
250 }
252 # Determine plain_name for host
253 my $plain_name;
254 if ($header eq "opsi_install_client") { # Opsi installing clients use hostId as plain_name
255 if (not exists $msg_hash->{'hostId'}) {
256 $error++;
257 &daemon_log("$session_id ERROR: opsi_install_client-message has no xml-tag 'hostID', job was not created: $msg", 1);
258 } else {
259 $plain_name = $msg_hash->{'hostId'}[0];
260 $header = "trigger_action_reinstall"
261 }
263 } else { # Try to determine plain_name via ladp search
264 my $ldap_handle = &main::get_ldap_handle($session_id);
265 if( not defined $ldap_handle ) {
266 &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
267 $plain_name = "none";
268 } else {
269 my $mesg = $ldap_handle->search(
270 base => $main::ldap_base,
271 scope => 'sub',
272 attrs => ['cn'],
273 filter => "(macAddress=$macaddress)");
274 if($mesg->code || ($mesg->count!=1)) {
275 &main::daemon_log($mesg->error, 1);
276 $plain_name = "none";
277 } else {
278 my $entry= $mesg->entry(0);
279 $plain_name = $entry->get_value("cn");
280 }
281 }
282 }
284 # Add job to job queue
285 if( $error == 0 ) {
286 my $func_dic = {table=>$main::job_queue_tn,
287 primkey=>['macaddress', 'headertag'],
288 timestamp=>$timestamp,
289 status=>'waiting',
290 result=>'none',
291 progress=>'none',
292 headertag=>$header,
293 targettag=>$target,
294 xmlmessage=>$msg,
295 macaddress=>$macaddress,
296 plainname=>$plain_name,
297 siserver=>"localhost",
298 modified=>"1",
299 };
300 my $res = $main::job_db->add_dbentry($func_dic);
301 if (not $res == 0) {
302 &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1);
303 } else {
304 &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
305 }
306 $out_msg = "<xml><header>answer</header><source>$main::server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
307 }
309 my @out_msg_l = ( $out_msg );
310 return @out_msg_l;
311 }
313 # vim:ts=4:shiftwidth:expandtab
314 1;