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 ($server_ip, $server_port, $server_passwd, $max_clients);
24 my ($gosa_ip, $gosa_mac_address, $gosa_port, $gosa_passwd, $network_interface);
25 my ($job_queue_timeout, $job_queue_file_name);
27 my %cfg_defaults = (
28 "server" => {
29 "ip" => [\$server_ip, "0.0.0.0"],
30 "port" => [\$server_port, "20081"],
31 "key" => [\$server_passwd, ""],
32 "max-clients" => [\$max_clients, 100],
33 },
34 "GOsaPackages" => {
35 "ip" => [\$gosa_ip, "0.0.0.0"],
36 "port" => [\$gosa_port, "20082"],
37 "key" => [\$gosa_passwd, "none"],
38 "job-queue" => [\$job_queue_file_name, '/var/lib/gosa-si/jobs.db'],
39 },
40 );
43 ## START ##########################
45 # read configfile and import variables
46 &read_configfile();
47 $network_interface= &get_interface_for_ip($server_ip);
48 $gosa_mac_address= &get_mac($network_interface);
50 # complete addresses
51 if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); }
52 our $server_address = "$server_ip:$server_port";
53 if( inet_aton($gosa_ip) ){ $gosa_ip = inet_ntoa(inet_aton($gosa_ip)); }
54 $main::gosa_address = "$gosa_ip:$gosa_port";
56 # create general settings for this module
57 #y $gosa_cipher = &create_ciphering($gosa_passwd);
58 my $xml = new XML::Simple();
60 # import local events
61 my ($error, $result, $event_hash) = &import_events($event_dir);
62 if ($error == 0) {
63 foreach my $log_line (@$result) {
64 &main::daemon_log("0 DEBUG: GosaPackages - $log_line", 7);
65 }
66 } else {
67 foreach my $log_line (@$result) {
68 &main::daemon_log("0 ERROR: GosaPackages - $log_line", 1);
69 }
70 }
73 ## FUNCTIONS #################################################################
75 sub get_module_info {
76 my @info = ($main::gosa_address,
77 $gosa_passwd,
78 );
79 return \@info;
80 }
83 #=== FUNCTION ================================================================
84 # NAME: read_configfile
85 # PARAMETERS: cfg_file - string -
86 # RETURNS: nothing
87 # DESCRIPTION: read cfg_file and set variables
88 #===============================================================================
89 sub read_configfile {
90 my $cfg;
91 if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) {
92 if( -r $main::cfg_file ) {
93 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
94 } else {
95 print STDERR "Couldn't read config file!";
96 }
97 } else {
98 $cfg = Config::IniFiles->new() ;
99 }
100 foreach my $section (keys %cfg_defaults) {
101 foreach my $param (keys %{$cfg_defaults{ $section }}) {
102 my $pinfo = $cfg_defaults{ $section }{ $param };
103 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
104 }
105 }
106 }
108 #=== FUNCTION ================================================================
109 # NAME: get_interface_for_ip
110 # PARAMETERS: ip address (i.e. 192.168.0.1)
111 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
112 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
113 #===============================================================================
114 sub get_interface_for_ip {
115 my $result;
116 my $ip= shift;
117 if ($ip && length($ip) > 0) {
118 my @ifs= &get_interfaces();
119 if($ip eq "0.0.0.0") {
120 $result = "all";
121 } else {
122 foreach (@ifs) {
123 my $if=$_;
124 if(get_ip($if) eq $ip) {
125 $result = $if;
126 }
127 }
128 }
129 }
130 return $result;
131 }
133 #=== FUNCTION ================================================================
134 # NAME: get_interfaces
135 # PARAMETERS: none
136 # RETURNS: (list of interfaces)
137 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
138 #===============================================================================
139 sub get_interfaces {
140 my @result;
141 my $PROC_NET_DEV= ('/proc/net/dev');
143 open(PROC_NET_DEV, "<$PROC_NET_DEV")
144 or die "Could not open $PROC_NET_DEV";
146 my @ifs = <PROC_NET_DEV>;
148 close(PROC_NET_DEV);
150 # Eat first two line
151 shift @ifs;
152 shift @ifs;
154 chomp @ifs;
155 foreach my $line(@ifs) {
156 my $if= (split /:/, $line)[0];
157 $if =~ s/^\s+//;
158 push @result, $if;
159 }
161 return @result;
162 }
164 #=== FUNCTION ================================================================
165 # NAME: get_mac
166 # PARAMETERS: interface name (i.e. eth0)
167 # RETURNS: (mac address)
168 # DESCRIPTION: Uses ioctl to get mac address directly from system.
169 #===============================================================================
170 sub get_mac {
171 my $ifreq= shift;
172 my $result;
173 if ($ifreq && length($ifreq) > 0) {
174 if($ifreq eq "all") {
175 $result = "00:00:00:00:00:00";
176 } else {
177 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
179 # A configured MAC Address should always override a guessed value
180 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
181 $result= $gosa_mac_address;
182 }
184 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
185 or die "socket: $!";
187 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
188 my ($if, $mac)= unpack 'h36 H12', $ifreq;
190 if (length($mac) > 0) {
191 $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])$/;
192 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
193 $result = $mac;
194 }
195 }
196 }
197 }
198 return $result;
199 }
201 #=== FUNCTION ================================================================
202 # NAME: get_ip
203 # PARAMETERS: interface name (i.e. eth0)
204 # RETURNS: (ip address)
205 # DESCRIPTION: Uses ioctl to get ip address directly from system.
206 #===============================================================================
207 sub get_ip {
208 my $ifreq= shift;
209 my $result= "";
210 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
211 my $proto= getprotobyname('ip');
213 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
214 or die "socket: $!";
216 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
217 my ($if, $sin) = unpack 'a16 a16', $ifreq;
218 my ($port, $addr) = sockaddr_in $sin;
219 my $ip = inet_ntoa $addr;
221 if ($ip && length($ip) > 0) {
222 $result = $ip;
223 }
224 }
226 return $result;
227 }
230 #=== FUNCTION ================================================================
231 # NAME: process_incoming_msg
232 # PARAMETERS: crypted_msg - string - incoming crypted message
233 # RETURNS: nothing
234 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
235 #===============================================================================
236 sub process_incoming_msg {
237 my ($msg, $msg_hash, $session_id) = @_ ;
238 my $header = @{$msg_hash->{header}}[0];
239 my @msg_l;
240 my @out_msg_l;
242 &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 7);
244 if ($header =~ /^job_/) {
245 @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
246 }
247 elsif ($header =~ /^gosa_/) {
248 @msg_l = &process_gosa_msg($msg, $msg_hash, $session_id);
249 }
250 else {
251 &main::daemon_log("$session_id ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix", 1);
252 }
254 foreach my $out_msg ( @msg_l ) {
255 # substitute in all outgoing msg <source>GOSA</source> of <source>$server_address</source>
256 $out_msg =~ s/<source>GOSA<\/source>/<source>$server_address<\/source>/g;
257 $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
258 if (defined $out_msg){
259 push(@out_msg_l, $out_msg);
260 }
262 }
264 return \@out_msg_l;
265 }
268 sub process_gosa_msg {
269 my ($msg, $msg_hash, $session_id) = @_ ;
270 my $out_msg;
271 my @out_msg_l = ('nohandler');
272 my $sql_events;
274 my $header = @{$msg_hash->{'header'}}[0];
275 my $target = @{$msg_hash->{'target'}}[0];
276 $header =~ s/gosa_//;
278 # check local installed events
279 if( exists $event_hash->{$header} ) {
280 # a event exists with the header as name
281 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
282 no strict 'refs';
283 @out_msg_l = &{$event_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
285 # check client registered events
286 } else {
287 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$target') OR (hostname='$target') )";
288 my $res = $main::known_clients_db->select_dbentry( $sql_events );
289 my $l = keys(%$res);
291 # set error if no or more than 1 hits are found for sql query
292 if ( $l != 1) {
293 @out_msg_l = ('knownclienterror');
295 # found exact 1 hit in db
296 } else {
297 my $client_events = $res->{'1'}->{'events'};
299 # client is registered for this event, deliver this message to client
300 if ($client_events =~ /,$header,/) {
301 $msg =~ s/<header>gosa_/<header>/;
302 @out_msg_l = ( $msg );
304 # client is not registered for this event, set error
305 } else {
306 @out_msg_l = ('noeventerror');
307 }
308 }
309 }
311 # if delivery not possible raise error and return
312 if (not defined $out_msg_l[0]) {
313 @out_msg_l = ();
314 } elsif ($out_msg_l[0] eq 'nohandler') {
315 &main::daemon_log("$session_id ERROR: GosaPackages: no event handler or core function defined for '$header'", 1);
316 @out_msg_l = ();
317 } elsif ($out_msg_l[0] eq 'knownclienterror') {
318 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
319 &main::daemon_log("$session_id WARNING: processing is aborted and message will not be forwarded", 3);
320 @out_msg_l = ();
321 } elsif ($out_msg_l[0] eq 'noeventerror') {
322 &main::daemon_log("$session_id WARNING: client '$target' is not registered for event '$header', processing is aborted", 3);
323 @out_msg_l = ();
324 }
326 return @out_msg_l;
327 }
330 sub process_job_msg {
331 my ($msg, $msg_hash, $session_id)= @_ ;
332 my $out_msg;
333 my $error = 0;
335 my $header = @{$msg_hash->{'header'}}[0];
336 $header =~ s/job_//;
337 my $target = @{$msg_hash->{'target'}}[0];
339 # if no timestamp is specified, use 19700101000000
340 my $timestamp = "19700101000000";
341 if( exists $msg_hash->{'timestamp'} ) {
342 $timestamp = @{$msg_hash->{'timestamp'}}[0];
343 }
345 #if no macaddress is specified, raise error
346 my $macaddress;
347 if( exists $msg_hash->{'macaddress'} ) {
348 $macaddress = @{$msg_hash->{'macaddress'}}[0];
349 } 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 ) {
350 $macaddress = $1;
351 } else {
352 $error ++;
353 $out_msg = "<xml>".
354 "<header>answer</header>".
355 "<source>$server_address</source>".
356 "<target>GOSA</target>".
357 "<answer1>1</answer1>".
358 "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
359 "</xml>";
360 }
362 # if mac address is already known in ldap, set plain_name to 'cn'
363 my $plain_name;
364 my $ldap_handle = &main::get_ldap_handle($session_id);
365 if( not defined $ldap_handle ) {
366 &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
367 $plain_name = "none";
369 # try to fetch a 'real name'
370 } else {
371 my $mesg = $ldap_handle->search(
372 base => $main::ldap_base,
373 scope => 'sub',
374 attrs => ['cn'],
375 filter => "(macAddress=$macaddress)");
376 if($mesg->code) {
377 &main::daemon_log($mesg->error, 1);
378 $plain_name = "none";
379 } else {
380 my $entry= $mesg->entry(0);
381 $plain_name = $entry->get_value("cn");
382 }
383 }
385 if( $error == 0 ) {
386 # add job to job queue
387 my $func_dic = {table=>$main::job_queue_tn,
388 primkey=>[],
389 timestamp=>$timestamp,
390 status=>'waiting',
391 result=>'none',
392 progress=>'none',
393 headertag=>$header,
394 targettag=>$target,
395 xmlmessage=>$msg,
396 macaddress=>$macaddress,
397 plainname=>$plain_name,
398 };
399 my $res = $main::job_db->add_dbentry($func_dic);
400 if (not $res == 0) {
401 &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1);
402 } else {
403 &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
404 }
405 $out_msg = "<xml><header>answer</header><source>$server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
406 }
408 my @out_msg_l = ( $out_msg );
409 return @out_msg_l;
410 }
412 1;