4e60d116fc6368f2cb4e48951edf462b0244c906
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 DEBUG: GosaPackages - $log_line", 7);
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'", 7);
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 INFO: found event '$header' at event-module '".$event2module_hash->{$header}."'", 5);
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,/) {
189 &main::daemon_log("$session_id INFO: client '$target' is registerd for event '$header', forward message to client.", 5);
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 &main::daemon_log("$session_id ERROR: no event handler found for '$header', check client registration events!", 1);
207 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
208 &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
209 @out_msg_l = ();
210 } elsif ($out_msg_l[0] eq 'noeventerror') {
211 &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1);
212 @out_msg_l = ();
213 }
215 return @out_msg_l;
216 }
219 sub process_job_msg {
220 my ($msg, $msg_hash, $session_id)= @_ ;
221 my $out_msg;
222 my $error = 0;
224 my $header = @{$msg_hash->{'header'}}[0];
225 $header =~ s/job_//;
226 my $target = @{$msg_hash->{'target'}}[0];
228 # If no timestamp is specified, use 19700101000000
229 my $timestamp = "19700101000000";
230 if( exists $msg_hash->{'timestamp'} ) {
231 $timestamp = @{$msg_hash->{'timestamp'}}[0];
232 }
234 # If no macaddress is specified, raise error
235 my $macaddress;
236 if( exists $msg_hash->{'macaddress'} ) {
237 $macaddress = @{$msg_hash->{'macaddress'}}[0];
238 } 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 ) {
239 $macaddress = $1;
240 } else {
241 $error ++;
242 $out_msg = "<xml>".
243 "<header>answer</header>".
244 "<source>$main::server_address</source>".
245 "<target>GOSA</target>".
246 "<answer1>1</answer1>".
247 "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
248 "</xml>";
249 }
251 # Determine plain_name for host
252 my $plain_name;
253 if ($header eq "opsi_install_client") { # Opsi installing clients use hostId as plain_name
254 if (not exists $msg_hash->{'hostId'}) {
255 $error++;
256 &daemon_log("$session_id ERROR: opsi_install_client-message has no xml-tag 'hostID', job was not created: $msg", 1);
257 } else {
258 $plain_name = $msg_hash->{'hostId'}[0];
259 $header = "trigger_action_reinstall"
260 }
262 } else { # Try to determine plain_name via ladp search
263 my $ldap_handle = &main::get_ldap_handle($session_id);
264 if( not defined $ldap_handle ) {
265 &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
266 $plain_name = "none";
267 } else {
268 my $mesg = $ldap_handle->search(
269 base => $main::ldap_base,
270 scope => 'sub',
271 attrs => ['cn'],
272 filter => "(macAddress=$macaddress)");
273 if($mesg->code || ($mesg->count!=1)) {
274 &main::daemon_log($mesg->error, 1);
275 $plain_name = "none";
276 } else {
277 my $entry= $mesg->entry(0);
278 $plain_name = $entry->get_value("cn");
279 }
280 }
281 }
283 # Add job to job queue
284 if( $error == 0 ) {
285 my $func_dic = {table=>$main::job_queue_tn,
286 primkey=>['macaddress', 'headertag'],
287 timestamp=>$timestamp,
288 status=>'waiting',
289 result=>'none',
290 progress=>'none',
291 headertag=>$header,
292 targettag=>$target,
293 xmlmessage=>$msg,
294 macaddress=>$macaddress,
295 plainname=>$plain_name,
296 siserver=>"localhost",
297 modified=>"1",
298 };
299 my $res = $main::job_db->add_dbentry($func_dic);
300 if (not $res == 0) {
301 &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1);
302 } else {
303 &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
304 }
305 $out_msg = "<xml><header>answer</header><source>$main::server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
306 }
308 my @out_msg_l = ( $out_msg );
309 return @out_msg_l;
310 }
312 # vim:ts=4:shiftwidth:expandtab
313 1;