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);
60 if ($error == 0) {
61 foreach my $log_line (@$result) {
62 &main::daemon_log("0 DEBUG: GosaPackages - $log_line", 7);
63 }
64 } else {
65 foreach my $log_line (@$result) {
66 &main::daemon_log("0 ERROR: GosaPackages - $log_line", 1);
67 }
68 }
71 ## FUNCTIONS #################################################################
73 sub get_module_info {
74 my @info = ($main::gosa_address,
75 $gosa_passwd,
76 );
77 return \@info;
78 }
81 #=== FUNCTION ================================================================
82 # NAME: read_configfile
83 # PARAMETERS: cfg_file - string -
84 # RETURNS: nothing
85 # DESCRIPTION: read cfg_file and set variables
86 #===============================================================================
87 sub read_configfile {
88 my $cfg;
89 if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) {
90 if( -r $main::cfg_file ) {
91 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
92 } else {
93 print STDERR "Couldn't read config file!";
94 }
95 } else {
96 $cfg = Config::IniFiles->new() ;
97 }
98 foreach my $section (keys %cfg_defaults) {
99 foreach my $param (keys %{$cfg_defaults{ $section }}) {
100 my $pinfo = $cfg_defaults{ $section }{ $param };
101 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
102 }
103 }
104 }
107 #=== FUNCTION ================================================================
108 # NAME: get_mac
109 # PARAMETERS: interface name (i.e. eth0)
110 # RETURNS: (mac address)
111 # DESCRIPTION: Uses ioctl to get mac address directly from system.
112 #===============================================================================
113 sub get_mac {
114 my $ifreq= shift;
115 my $result;
116 if ($ifreq && length($ifreq) > 0) {
117 if($ifreq eq "all") {
118 $result = "00:00:00:00:00:00";
119 } else {
120 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
122 # A configured MAC Address should always override a guessed value
123 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
124 $result= $gosa_mac_address;
125 }
127 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
128 or die "socket: $!";
130 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
131 my ($if, $mac)= unpack 'h36 H12', $ifreq;
133 if (length($mac) > 0) {
134 $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])$/;
135 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
136 $result = $mac;
137 }
138 }
139 }
140 }
141 return $result;
142 }
145 #=== FUNCTION ================================================================
146 # NAME: process_incoming_msg
147 # PARAMETERS: crypted_msg - string - incoming crypted message
148 # RETURNS: nothing
149 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
150 #===============================================================================
151 sub process_incoming_msg {
152 my ($msg, $msg_hash, $session_id) = @_ ;
153 my $header = @{$msg_hash->{header}}[0];
154 my @msg_l;
155 my @out_msg_l;
157 &main::daemon_log("$session_id DEBUG: GosaPackages: msg to process '$header'", 7);
159 if ($header =~ /^job_/) {
160 @msg_l = &process_job_msg($msg, $msg_hash, $session_id);
161 }
162 elsif ($header =~ /^gosa_/) {
163 @msg_l = &process_gosa_msg($msg, $msg_hash, $session_id);
164 }
165 else {
166 &main::daemon_log("$session_id ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix", 1);
167 }
169 foreach my $out_msg ( @msg_l ) {
170 # substitute in all outgoing msg <source>GOSA</source> of <source>$server_address</source>
171 $out_msg =~ s/<source>GOSA<\/source>/<source>$server_address<\/source>/g;
172 $out_msg =~ s/<\/xml>/<session_id>$session_id<\/session_id><\/xml>/;
173 if (defined $out_msg){
174 push(@out_msg_l, $out_msg);
175 }
177 }
179 return \@out_msg_l;
180 }
183 sub process_gosa_msg {
184 my ($msg, $msg_hash, $session_id) = @_ ;
185 my $out_msg;
186 my @out_msg_l = ('nohandler');
187 my $sql_events;
189 my $header = @{$msg_hash->{'header'}}[0];
190 my $target = @{$msg_hash->{'target'}}[0];
191 $header =~ s/gosa_//;
193 # check local installed events
194 if( exists $event_hash->{$header} ) {
195 # a event exists with the header as name
196 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
197 no strict 'refs';
198 @out_msg_l = &{$event_hash->{$header}."::$header"}( $msg, $msg_hash, $session_id );
200 # check client registered events
201 } else {
202 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$target') OR (hostname='$target') )";
203 my $res = $main::known_clients_db->select_dbentry( $sql_events );
204 my $l = keys(%$res);
206 # set error if no or more than 1 hits are found for sql query
207 if ( $l != 1) {
208 @out_msg_l = ('knownclienterror');
210 # found exact 1 hit in db
211 } else {
212 my $client_events = $res->{'1'}->{'events'};
214 # client is registered for this event, deliver this message to client
215 if ($client_events =~ /,$header,/) {
216 $msg =~ s/<header>gosa_/<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;