ad5d12b504a7d3ecbf14c1d1b48c71ecbcdde1c7
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 my $xml = new XML::Simple();
58 # import local events
59 my ($error, $result, $event_hash) = &import_events($event_dir);
61 foreach my $log_line (@$result) {
62 if ($log_line =~ / succeed: /) {
63 &main::daemon_log("0 DEBUG: GosaPackages - $log_line", 7);
64 } else {
65 &main::daemon_log("0 ERROR: GosaPackages - $log_line", 1);
66 }
67 }
70 ## FUNCTIONS #################################################################
72 sub get_module_info {
73 my @info = ($main::gosa_address,
74 $gosa_passwd,
75 );
76 return \@info;
77 }
80 #=== FUNCTION ================================================================
81 # NAME: read_configfile
82 # PARAMETERS: cfg_file - string -
83 # RETURNS: nothing
84 # DESCRIPTION: read cfg_file and set variables
85 #===============================================================================
86 sub read_configfile {
87 my $cfg;
88 if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) {
89 if( -r $main::cfg_file ) {
90 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
91 } else {
92 print STDERR "Couldn't read config file!";
93 }
94 } else {
95 $cfg = Config::IniFiles->new() ;
96 }
97 foreach my $section (keys %cfg_defaults) {
98 foreach my $param (keys %{$cfg_defaults{ $section }}) {
99 my $pinfo = $cfg_defaults{ $section }{ $param };
100 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
101 }
102 }
103 }
106 #=== FUNCTION ================================================================
107 # NAME: get_mac
108 # PARAMETERS: interface name (i.e. eth0)
109 # RETURNS: (mac address)
110 # DESCRIPTION: Uses ioctl to get mac address directly from system.
111 #===============================================================================
112 sub get_mac {
113 my $ifreq= shift;
114 my $result;
115 if ($ifreq && length($ifreq) > 0) {
116 if($ifreq eq "all") {
117 $result = "00:00:00:00:00:00";
118 } else {
119 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
121 # A configured MAC Address should always override a guessed value
122 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
123 $result= $gosa_mac_address;
124 }
126 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
127 or die "socket: $!";
129 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
130 my ($if, $mac)= unpack 'h36 H12', $ifreq;
132 if (length($mac) > 0) {
133 $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])$/;
134 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
135 $result = $mac;
136 }
137 }
138 }
139 }
140 return $result;
141 }
144 #=== FUNCTION ================================================================
145 # NAME: process_incoming_msg
146 # PARAMETERS: crypted_msg - string - incoming crypted message
147 # RETURNS: nothing
148 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
149 #===============================================================================
150 sub process_incoming_msg {
151 my ($msg, $msg_hash, $session_id) = @_ ;
152 my $header = @{$msg_hash->{header}}[0];
153 my @msg_l;
154 my @out_msg_l;
156 &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 7);
158 if ($header =~ /^job_/) {
159 @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
160 }
161 elsif ($header =~ /^gosa_/) {
162 @msg_l = &process_gosa_msg($msg, $msg_hash, $session_id);
163 }
164 else {
165 &main::daemon_log("$session_id ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix", 1);
166 }
168 foreach my $out_msg ( @msg_l ) {
169 # substitute in all outgoing msg <source>GOSA</source> of <source>$server_address</source>
170 $out_msg =~ s/<source>GOSA<\/source>/<source>$server_address<\/source>/g;
171 $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
172 if (defined $out_msg){
173 push(@out_msg_l, $out_msg);
174 }
175 }
177 return \@out_msg_l;
178 }
181 sub process_gosa_msg {
182 my ($msg, $msg_hash, $session_id) = @_ ;
183 my $out_msg;
184 my @out_msg_l = ('nohandler');
185 my $sql_events;
187 # strip gosa_ prefix from header, it is not used any more
188 @{$msg_hash->{'header'}}[0] =~ s/gosa_//;
189 $msg =~ s/<header>gosa_/<header>/;
191 my $header = @{$msg_hash->{'header'}}[0];
192 my $target = @{$msg_hash->{'target'}}[0];
194 # check local installed events
195 if( exists $event_hash->{$header} ) {
196 # a event exists with the header as name
197 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
198 no strict 'refs';
199 @out_msg_l = &{$event_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
201 # check client registered events
202 } else {
203 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$target') OR (hostname='$target') )";
204 my $res = $main::known_clients_db->select_dbentry( $sql_events );
205 my $l = keys(%$res);
207 # set error if no or more than 1 hits are found for sql query
208 if ( $l != 1) {
209 @out_msg_l = ('knownclienterror');
211 # found exact 1 hit in db
212 } else {
213 my $client_events = $res->{'1'}->{'events'};
215 # client is registered for this event, deliver this message to client
216 if ($client_events =~ /,$header,/) {
217 @out_msg_l = ( $msg );
219 # client is not registered for this event, set error
220 } else {
221 @out_msg_l = ('noeventerror');
222 }
223 }
224 }
226 # if delivery not possible raise error and return
227 if (not defined $out_msg_l[0]) {
228 @out_msg_l = ();
229 } elsif ($out_msg_l[0] eq 'nohandler') {
230 &main::daemon_log("$session_id ERROR: GosaPackages: no event handler or core function defined for '$header'", 1);
231 @out_msg_l = ();
232 } elsif ($out_msg_l[0] eq 'knownclienterror') {
233 &main::daemon_log("$session_id ERROR: no event handler found for '$header', check client registration events!", 1);
234 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
235 &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
236 @out_msg_l = ();
237 } elsif ($out_msg_l[0] eq 'noeventerror') {
238 &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1);
239 @out_msg_l = ();
240 }
242 return @out_msg_l;
243 }
246 sub process_job_msg {
247 my ($msg, $msg_hash, $session_id)= @_ ;
248 my $out_msg;
249 my $error = 0;
251 my $header = @{$msg_hash->{'header'}}[0];
252 $header =~ s/job_//;
253 my $target = @{$msg_hash->{'target'}}[0];
255 # if no timestamp is specified, use 19700101000000
256 my $timestamp = "19700101000000";
257 if( exists $msg_hash->{'timestamp'} ) {
258 $timestamp = @{$msg_hash->{'timestamp'}}[0];
259 }
261 #if no macaddress is specified, raise error
262 my $macaddress;
263 if( exists $msg_hash->{'macaddress'} ) {
264 $macaddress = @{$msg_hash->{'macaddress'}}[0];
265 } 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 ) {
266 $macaddress = $1;
267 } else {
268 $error ++;
269 $out_msg = "<xml>".
270 "<header>answer</header>".
271 "<source>$server_address</source>".
272 "<target>GOSA</target>".
273 "<answer1>1</answer1>".
274 "<error_string>no mac address specified, neither in target-tag nor in macaddres-tag</error_string>".
275 "</xml>";
276 }
278 # if mac address is already known in ldap, set plain_name to 'cn'
279 my $plain_name;
280 my $ldap_handle = &main::get_ldap_handle($session_id);
281 if( not defined $ldap_handle ) {
282 &main::daemon_log("$session_id ERROR: cannot connect to ldap", 1);
283 $plain_name = "none";
285 # try to fetch a 'real name'
286 } else {
287 my $mesg = $ldap_handle->search(
288 base => $main::ldap_base,
289 scope => 'sub',
290 attrs => ['cn'],
291 filter => "(macAddress=$macaddress)");
292 if($mesg->code) {
293 &main::daemon_log($mesg->error, 1);
294 $plain_name = "none";
295 } else {
296 my $entry= $mesg->entry(0);
297 $plain_name = $entry->get_value("cn");
298 }
299 }
301 if( $error == 0 ) {
302 # add job to job queue
303 my $func_dic = {table=>$main::job_queue_tn,
304 primkey=>['macaddress', 'headertag'],
305 timestamp=>$timestamp,
306 status=>'waiting',
307 result=>'none',
308 progress=>'none',
309 headertag=>$header,
310 targettag=>$target,
311 xmlmessage=>$msg,
312 macaddress=>$macaddress,
313 plainname=>$plain_name,
314 siserver=>"localhost",
315 modified=>"1",
316 };
317 my $res = $main::job_db->add_dbentry($func_dic);
318 if (not $res == 0) {
319 &main::daemon_log("$session_id ERROR: GosaPackages: process_job_msg: $res", 1);
320 } else {
321 &main::daemon_log("$session_id INFO: GosaPackages: $header job successfully added to job queue", 5);
322 }
323 $out_msg = "<xml><header>answer</header><source>$server_address</source><target>GOSA</target><answer1>$res</answer1></xml>";
324 }
326 my @out_msg_l = ( $out_msg );
327 return @out_msg_l;
328 }
330 # vim:ts=4:shiftwidth:expandtab
331 1;