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 XML::Simple;
11 use File::Spec;
12 use Data::Dumper;
13 use GOSA::DBsqlite;
14 use MIME::Base64;
16 BEGIN{}
17 END{}
19 my ($server_activ, $server_port, $server_passwd, $max_clients, $server_event_dir);
20 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
21 my ($gosa_activ, $gosa_ip, $gosa_port, $gosa_passwd);
22 my ($job_queue_timeout, $job_queue_file_name);
24 my $gosa_server;
26 my %cfg_defaults =
27 ("general" =>
28 {"job_queue_file_name" => [\$job_queue_file_name, '/var/lib/gosa-si/jobs.db'],
29 },
30 "server" =>
31 {"server_activ" => [\$server_activ, "on"],
32 "server_port" => [\$server_port, "20081"],
33 "server_passwd" => [\$server_passwd, ""],
34 "max_clients" => [\$max_clients, 100],
35 "server_event_dir" => [\$server_event_dir, '/usr/lib/gosa-si/server/events'],
36 },
37 "bus" =>
38 {"bus_activ" => [\$bus_activ, "on"],
39 "bus_passwd" => [\$bus_passwd, ""],
40 "bus_ip" => [\$bus_ip, ""],
41 "bus_port" => [\$bus_port, "20080"],
42 },
43 "gosa" =>
44 {"gosa_activ" => [\$gosa_activ, "on"],
45 "gosa_ip" => [\$gosa_ip, ""],
46 "gosa_port" => [\$gosa_port, "20082"],
47 "gosa_passwd" => [\$gosa_passwd, "none"],
48 },
49 );
52 ## START ##########################
54 # read configfile and import variables
55 &read_configfile();
57 # detect own ip and mac address
58 my ($server_ip, $server_mac_address) = &get_ip_and_mac();
60 # complete addresses
61 my $server_address = "$server_ip:$server_port";
62 my $bus_address = "$bus_ip:$bus_port";
63 my $gosa_address = "$gosa_ip:$gosa_port";
65 # create general settings for this module
66 my $gosa_cipher = &create_ciphering($gosa_passwd);
67 my $xml = new XML::Simple();
69 # open gosa socket
70 if ($gosa_activ eq "on") {
71 &main::daemon_log(" ",1);
72 $gosa_server = IO::Socket::INET->new(LocalPort => $gosa_port,
73 Type => SOCK_STREAM,
74 Reuse => 1,
75 Listen => 1,
76 );
77 if (not defined $gosa_server) {
78 &main::daemon_log("cannot start tcp server at $gosa_port for communication to gosa: $@", 1);
79 } else {
80 &main::daemon_log("start server for communication to gosa: $gosa_address", 1);
82 }
83 }
85 # create gosa job queue as a SQLite DB
86 my @col_names = ("id", "timestamp", "status", "result", "header",
87 "target", "xml", "mac");
88 my $table_name = "jobs";
89 my $sqlite = GOSA::DBsqlite->new($job_queue_file_name);
90 #$sqlite->create_table($table_name, \@col_names);
95 ## FUNCTIONS #################################################################
97 sub get_module_info {
98 my @info = ($gosa_address,
99 $gosa_passwd,
100 $gosa_server,
101 $gosa_activ,
102 "socket",
103 );
104 return \@info;
105 }
108 #=== FUNCTION ================================================================
109 # NAME: read_configfile
110 # PARAMETERS: cfg_file - string -
111 # RETURNS: nothing
112 # DESCRIPTION: read cfg_file and set variables
113 #===============================================================================
114 sub read_configfile {
115 my $cfg;
116 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
117 if( -r $main::cfg_file ) {
118 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
119 } else {
120 print STDERR "Couldn't read config file!";
121 }
122 } else {
123 $cfg = Config::IniFiles->new() ;
124 }
125 foreach my $section (keys %cfg_defaults) {
126 foreach my $param (keys %{$cfg_defaults{ $section }}) {
127 my $pinfo = $cfg_defaults{ $section }{ $param };
128 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
129 }
130 }
131 }
134 #=== FUNCTION ================================================================
135 # NAME: get_ip_and_mac
136 # PARAMETERS: nothing
137 # RETURNS: (ip, mac)
138 # DESCRIPTION: executes /sbin/ifconfig and parses the output, the first occurence
139 # of a inet address is returned as well as the mac address in the line
140 # above the inet address
141 #===============================================================================
142 sub get_ip_and_mac {
143 my $ip = "0.0.0.0.0"; # Defualt-IP
144 my $mac = "00:00:00:00:00:00"; # Default-MAC
145 my @ifconfig = qx(/sbin/ifconfig);
146 foreach(@ifconfig) {
147 if (/Hardware Adresse (\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/) {
148 $mac = "$1:$2:$3:$4:$5:$6";
149 next;
150 }
151 if (/inet Adresse:(\d+).(\d+).(\d+).(\d+)/) {
152 $ip = "$1.$2.$3.$4";
153 last;
154 }
155 }
156 return ($ip, $mac);
157 }
160 #=== FUNCTION ================================================================
161 # NAME: open_socket
162 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
163 # [PeerPort] string necessary if port not appended by PeerAddr
164 # RETURNS: socket IO::Socket::INET
165 # DESCRIPTION: open a socket to PeerAddr
166 #===============================================================================
167 sub open_socket {
168 my ($PeerAddr, $PeerPort) = @_ ;
169 if(defined($PeerPort)){
170 $PeerAddr = $PeerAddr.":".$PeerPort;
171 }
172 my $socket;
173 $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
174 Porto => "tcp" ,
175 Type => SOCK_STREAM,
176 Timeout => 5,
177 );
178 if(not defined $socket) {
179 return;
180 }
181 &main::daemon_log("open_socket to: $PeerAddr", 7);
182 return $socket;
183 }
186 #=== FUNCTION ================================================================
187 # NAME: process_incoming_msg
188 # PARAMETERS: crypted_msg - string - incoming crypted message
189 # RETURNS: nothing
190 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
191 #===============================================================================
192 sub process_incoming_msg {
193 my ($crypted_msg) = @_ ;
194 if(not defined $crypted_msg) {
195 &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
196 }
197 &main::daemon_log("GosaPackages: incoming msg: \n$crypted_msg", 7);
199 $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
200 $crypted_msg = $1;
201 my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
203 # collect addresses from possible incoming clients
204 # only gosa is allowd as incoming client
205 &main::daemon_log("GosaPackages: host_key: $host", 7);
206 &main::daemon_log("GosaPackages: key_passwd: $gosa_passwd", 7);
208 $gosa_cipher = &create_ciphering($gosa_passwd);
210 # determine the correct passwd for deciphering of the incoming msgs
211 my $msg = "";
212 my $msg_hash;
213 eval{
214 $msg = &decrypt_msg($crypted_msg, $gosa_cipher);
215 &main::daemon_log("GosaPackages: decrypted_msg: \n$msg", 7);
217 $msg_hash = $xml->XMLin($msg, ForceArray=>1);
218 };
219 if($@) {
220 &main::daemon_log("WARNING: GosaPackages do not understand the message:", 5);
221 &main::daemon_log("$@", 7);
222 return;
223 }
225 my $header = @{$msg_hash->{header}}[0];
227 &main::daemon_log("recieve '$header' at GosaPackages from $host", 1);
228 &main::daemon_log("$msg", 7);
230 my $out_msg;
231 if ($header =~ /^job_/) {
232 $out_msg = &process_job_msg($msg, $msg_hash);
233 } elsif ($header =~ /^gosa_/) {
234 $out_msg = &process_gosa_msg($msg, $header);
235 } else {
236 &main::daemon_log("ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix");
237 }
239 if (not defined $out_msg) {
240 return;
241 }
243 if ($out_msg =~ /<jobdb_id>(\d*?)<\/jobdb_id>/) {
244 my $job_id = $1;
245 my $sql = "UPDATE '$main::job_queue_table_name' SET status='done', result='$out_msg' WHERE id='$job_id'";
246 my $res = $main::job_db->exec_statement($sql);
247 return;
249 } else {
250 my $out_cipher = &create_ciphering($gosa_passwd);
251 $out_msg = &encrypt_msg($out_msg, $out_cipher);
252 return $out_msg;
253 }
255 }
257 sub process_gosa_msg {
258 my ($msg, $header) = @_ ;
259 my $out_msg;
260 $header =~ s/gosa_//;
261 &main::daemon_log("GosaPackages: got a gosa msg $header", 5);
263 # decide wether msg is a core function or a event handler
264 if ( $header eq 'query_jobdb') { $out_msg = &query_jobdb }
265 else {
266 # msg could not be assigned to core function
267 # fetch all available eventhandler under $server_event_dir
268 opendir (DIR, $server_event_dir) or &main::daemon_log("ERROR cannot open $server_event_dir: $!\n", 1) and return;
269 while (defined (my $file = readdir (DIR))) {
270 if (not $file eq $header) {
271 next;
272 }
273 # try to deliver incoming msg to eventhandler
274 my $cmd = File::Spec->join($server_event_dir, $header)." '$msg'";
275 &main::daemon_log("GosaPackages: execute event_handler $header", 3);
276 &main::daemon_log("GosaPackages: cmd: $cmd", 7);
278 $out_msg = "";
279 open(PIPE, "$cmd 2>&1 |");
280 while(<PIPE>) {
281 $out_msg.=$_;
282 }
283 close(PIPE);
284 &main::daemon_log("GosaPackages: answer of cmd: $out_msg", 5);
285 last;
286 }
287 }
289 # if delivery not possible raise error and return
290 if (not defined $out_msg) {
291 &main::daemon_log("ERROR: GosaPackages: no event handler or core function defined for $header", 1);
292 } elsif ($out_msg eq "") {
293 &main::daemon_log("ERROR: GosaPackages got not answer from event_handler $header", 1);
294 }
295 return $out_msg;
297 }
300 sub process_job_msg {
301 my ($msg, $msg_hash)= @_ ;
303 my $header = @{$msg_hash->{header}}[0];
304 $header =~ s/job_//;
305 &main::daemon_log("GosaPackages: got a job msg $header", 5);
307 # check wether mac address is already known in known_daemons or known_clients
308 my $target = 'not known until now';
310 # add job to job queue
311 my $func_dic = {table=>$main::job_queue_table_name,
312 timestamp=>@{$msg_hash->{timestamp}}[0],
313 status=>'waiting',
314 result=>'none',
315 header=>$header,
316 target=>$target,
317 xmlmessage=>$msg,
318 macaddress=>@{$msg_hash->{mac}}[0],
319 };
320 my $res = $main::job_db->add_dbentry($func_dic);
321 if (not $res == 0) {
322 &main::daemon_log("ERROR: GosaPackages: process_job_msg: $res", 1);
323 }
325 &main::daemon_log("GosaPackages: $header job successfully added to job queue", 3);
326 return;
328 }
331 sub db_res_2_xml {
332 my ($db_res) = @_ ;
333 my $xml = "<xml>";
335 while ( my ($hit, $hash) = each %{ $db_res } ) {
336 $xml .= "<$hit>";
338 while ( my ($column_name, $column_value) = each %{$hash} ) {
339 $xml .= "<$column_name>";
340 my $xml_content = $column_value;
341 if( $column_name eq "xml" ) {
342 $xml_content = &encode_base64($column_value);
343 }
344 $xml .= $xml_content;
345 $xml .= "</$column_name>";
346 }
348 $xml .= "</$hit>";
349 }
351 $xml .= "</xml>";
352 return $xml;
353 }
356 ## CORE FUNCTIONS ############################################################
358 sub query_jobdb {
359 my ($msg) = @_;
360 my $msg_hash = &transform_msg2hash($msg);
362 # prepare query sql statement
363 my @where = @{$msg_hash->{where}};
364 my $where_hash = {table=>$main::job_queue_table_name };
365 foreach my $where_pram (@where) {
366 my $where_val = @{$msg_hash->{$where_pram}}[0];
367 if (defined $where_val) {
368 $where_hash->{$where_pram} = $where_val;
369 }
370 }
372 # execute db query
373 my $res_hash = $main::job_db->select_dbentry($where_hash);
375 my $out_xml = &db_res_2_xml($res_hash);
376 return $out_xml;
377 }
381 1;