3f5bc8ca20036a2439e013b8ff4d761bbe0c6a4a
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 }
48 ## FUNCTIONS #################################################################
50 sub get_module_info {
51 my @info = ($main::gosa_address,
52 $main::gosa_passwd,
53 );
54 return \@info;
55 }
58 #=== FUNCTION ================================================================
59 # NAME: get_mac
60 # PARAMETERS: interface name (i.e. eth0)
61 # RETURNS: (mac address)
62 # DESCRIPTION: Uses ioctl to get mac address directly from system.
63 #===============================================================================
64 sub get_mac {
65 my $ifreq= shift;
66 my $result;
67 if ($ifreq && length($ifreq) > 0) {
68 if($ifreq eq "all") {
69 $result = "00:00:00:00:00:00";
70 } else {
71 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
73 # A configured MAC Address should always override a guessed value
74 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
75 $result= $gosa_mac_address;
76 }
78 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
79 or die "socket: $!";
81 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
82 my ($if, $mac)= unpack 'h36 H12', $ifreq;
84 if (length($mac) > 0) {
85 $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])$/;
86 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
87 $result = $mac;
88 }
89 }
90 }
91 }
92 return $result;
93 }
96 #=== FUNCTION ================================================================
97 # NAME: process_incoming_msg
98 # PARAMETERS: crypted_msg - string - incoming crypted message
99 # RETURNS: nothing
100 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
101 #===============================================================================
102 sub process_incoming_msg {
103 my ($msg, $msg_hash, $session_id) = @_ ;
104 my $header = @{$msg_hash->{header}}[0];
105 my @msg_l;
106 my @out_msg_l;
108 &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 7);
110 if ($header =~ /^job_/) {
111 @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
112 }
113 elsif ($header =~ /^gosa_/) {
114 @msg_l = &process_gosa_msg($msg, $msg_hash, $session_id);
115 }
116 else {
117 &main::daemon_log("$session_id ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix", 1);
118 }
120 foreach my $out_msg ( @msg_l ) {
121 # determine the correct outgoing source address to the corresponding target address
122 $out_msg =~ /<target>(\S*)<\/target>/;
123 my $act_server_ip = &main::get_local_ip_for_remote_ip(sprintf("%s", $1 =~ /^([0-9\.]*?):.*$/));
125 # Patch the correct outgoing source address
126 if ($out_msg =~ /<source>GOSA<\/source>/ ) {
127 $out_msg =~ s/<source>GOSA<\/source>/<source>$act_server_ip:$main::server_port<\/source>/g;
128 }
130 # Patch the correct outgoing forward_to_gosa address
131 if ($out_msg =~ /<forward_to_gosa>(\S+),(\d+)<\/forward_to_gosa>/ ) {
132 $out_msg =~ s/<forward_to_gosa>\S+<\/forward_to_gosa>/<forward_to_gosa>$act_server_ip:$main::server_port,$session_id<\/forward_to_gosa>/;
133 } else {
134 $out_msg =~ s/<\/xml>/<forward_to_gosa>$act_server_ip:$main::server_port,$session_id<\/forward_to_gosa> <\/xml>/;
135 }
137 # Add to each outgoing message the current POE session id
138 $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
141 if (defined $out_msg){
142 push(@out_msg_l, $out_msg);
143 }
144 }
146 return \@out_msg_l;
147 }
150 sub process_gosa_msg {
151 my ($msg, $msg_hash, $session_id) = @_ ;
152 my $out_msg;
153 my @out_msg_l = ('nohandler');
154 my $sql_events;
156 # strip gosa_ prefix from header, it is not used any more
157 @{$msg_hash->{'header'}}[0] =~ s/gosa_//;
158 $msg =~ s/<header>gosa_/<header>/;
160 my $header = @{$msg_hash->{'header'}}[0];
161 my $target = @{$msg_hash->{'target'}}[0];
163 # check local installed events
164 if( exists $event_hash->{$header} ) {
165 # a event exists with the header as name
166 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
167 no strict 'refs';
168 @out_msg_l = &{$event_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
170 # check client registered events
171 } else {
172 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$target') OR (hostname='$target') )";
173 my $res = $main::known_clients_db->select_dbentry( $sql_events );
174 my $l = keys(%$res);
176 # set error if no or more than 1 hits are found for sql query
177 if ( $l != 1) {
178 @out_msg_l = ('knownclienterror');
179 # found exact 1 hit in db
180 } else {
181 my $client_events = $res->{'1'}->{'events'};
183 # client is registered for this event, deliver this message to client
184 if ($client_events =~ /,$header,/) {
185 @out_msg_l = ( $msg );
187 # client is not registered for this event, set error
188 } else {
189 @out_msg_l = ('noeventerror');
190 }
191 }
192 }
194 # if delivery not possible raise error and return
195 if (not defined $out_msg_l[0]) {
196 @out_msg_l = ();
197 } elsif ($out_msg_l[0] eq 'nohandler') {
198 &main::daemon_log("$session_id ERROR: GosaPackages: no event handler or core function defined for '$header'", 1);
199 @out_msg_l = ();
200 } elsif ($out_msg_l[0] eq 'knownclienterror') {
201 &main::daemon_log("$session_id ERROR: no event handler found for '$header', check client registration events!", 1);
202 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
203 &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
204 @out_msg_l = ();
205 } elsif ($out_msg_l[0] eq 'noeventerror') {
206 &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1);
207 @out_msg_l = ();
208 }
210 return @out_msg_l;
211 }
214 sub process_job_msg {
215 my ($msg, $msg_hash, $session_id)= @_ ;
216 my $out_msg;
217 my $error = 0;
219 my $header = @{$msg_hash->{'header'}}[0];
220 $header =~ s/job_//;
221 my $target = @{$msg_hash->{'target'}}[0];
223 # if no timestamp is specified, use 19700101000000
224 my $timestamp = "19700101000000";
225 if( exists $msg_hash->{'timestamp'} ) {
226 $timestamp = @{$msg_hash->{'timestamp'}}[0];
227 }
229 #if no macaddress is specified, raise error
230 my $macaddress;
231 if( exists $msg_hash->{'macaddress'} ) {
232 $macaddress = @{$msg_hash->{'macaddress'}}[0];
233 } 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 ) {
234 $macaddress = $1;
235 } else {
236 $error ++;
237 $out_msg = "<xml>".
238 "<header>answer</header>".
239 "<source>$main::server_address</source>".
240 "<target>GOSA</target>".
241 "<answer1>1</answer1>".
242 "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
243 "</xml>";
244 }
246 # if mac address is already known in ldap, set plain_name to 'cn'
247 my $plain_name;
248 my $ldap_handle = &main::get_ldap_handle($session_id);
249 if( not defined $ldap_handle ) {
250 &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
251 $plain_name = "none";
253 # try to fetch a 'real name'
254 } else {
255 my $mesg = $ldap_handle->search(
256 base => $main::ldap_base,
257 scope => 'sub',
258 attrs => ['cn'],
259 filter => "(macAddress=$macaddress)");
260 if($mesg->code) {
261 &main::daemon_log($mesg->error, 1);
262 $plain_name = "none";
263 } else {
264 my $entry= $mesg->entry(0);
265 $plain_name = $entry->get_value("cn");
266 }
267 }
269 if( $error == 0 ) {
270 # add job to job queue
271 my $func_dic = {table=>$main::job_queue_tn,
272 primkey=>['macaddress', 'headertag'],
273 timestamp=>$timestamp,
274 status=>'waiting',
275 result=>'none',
276 progress=>'none',
277 headertag=>$header,
278 targettag=>$target,
279 xmlmessage=>$msg,
280 macaddress=>$macaddress,
281 plainname=>$plain_name,
282 siserver=>"localhost",
283 modified=>"1",
284 };
285 my $res = $main::job_db->add_dbentry($func_dic);
286 if (not $res == 0) {
287 &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1);
288 } else {
289 &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
290 }
291 $out_msg = "<xml><header>answer</header><source>$main::server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
292 }
294 my @out_msg_l = ( $out_msg );
295 return @out_msg_l;
296 }
298 # vim:ts=4:shiftwidth:expandtab
299 1;