664c1008115e17f4c2e8583cb2befe79f0263c61
1 package GosaPackages;
3 use Exporter;
4 @ISA = ("Exporter");
6 use strict;
7 use warnings;
8 use GosaSupportDaemon;
9 use IO::Socket::INET;
10 use XML::Simple;
11 use File::Spec;
12 use DBsqlite;
14 BEGIN{}
15 END{}
17 my ($server_activ, $server_port, $server_passwd, $max_clients);
18 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
19 my ($gosa_activ, $gosa_ip, $gosa_port, $gosa_passwd);
20 my ($job_queue_timeout, $job_queue_file_name);
22 my $gosa_server;
23 my $event_dir = "/home/rettenbe/gonicus/projekte/gosa-trunk/gosa-si/server/events";
25 # name of table for storing gosa jobs
26 my $job_queue_table_name = 'jobs';
28 my %cfg_defaults =
29 ("general" =>
30 {"job_queue_file_name" => [\$job_queue_file_name, '/tmp/jobs.db'],
31 },
32 "server" =>
33 {"server_activ" => [\$server_activ, "on"],
34 "server_port" => [\$server_port, "20081"],
35 "server_passwd" => [\$server_passwd, ""],
36 "max_clients" => [\$max_clients, 100],
37 },
38 "bus" =>
39 {"bus_activ" => [\$bus_activ, "on"],
40 "bus_passwd" => [\$bus_passwd, ""],
41 "bus_ip" => [\$bus_ip, ""],
42 "bus_port" => [\$bus_port, "20080"],
43 },
44 "gosa" =>
45 {"gosa_activ" => [\$gosa_activ, "on"],
46 "gosa_ip" => [\$gosa_ip, ""],
47 "gosa_port" => [\$gosa_port, "20082"],
48 "gosa_passwd" => [\$gosa_passwd, "none"],
49 },
50 );
53 ### START ##########################
55 # read configfile and import variables
56 &read_configfile();
58 # detect own ip and mac address
59 my ($server_ip, $server_mac_address) = &get_ip_and_mac();
61 # complete addresses
62 my $server_address = "$server_ip:$server_port";
63 my $bus_address = "$bus_ip:$bus_port";
64 my $gosa_address = "$gosa_ip:$gosa_port";
66 # create general settings for this module
67 my $gosa_cipher = &create_ciphering($gosa_passwd);
68 my $xml = new XML::Simple();
70 # open gosa socket
71 if ($gosa_activ eq "on") {
72 &main::daemon_log(" ",1);
73 $gosa_server = IO::Socket::INET->new(LocalPort => $gosa_port,
74 Type => SOCK_STREAM,
75 Reuse => 1,
76 Listen => 1,
77 );
78 if (not defined $gosa_server) {
79 &main::daemon_log("cannot start tcp server at $gosa_port for communication to gosa: $@", 1);
80 } else {
81 &main::daemon_log("start server for communication to gosa: $gosa_address", 1);
83 }
84 }
86 # create gosa job queue as a SQLite DB
87 my @col_names = ("id", "timestamp", "status", "result", "header",
88 "target", "xml", "mac");
89 my $table_name = "jobs";
90 my $sqlite = DBsqlite->new($job_queue_file_name);
91 $sqlite->create_table($table_name, \@col_names);
96 ### FUNCTIONS #################################################################
98 sub get_module_info {
99 my @info = ($gosa_address,
100 $gosa_passwd,
101 $gosa_server,
102 $gosa_activ,
103 "socket",
104 );
105 return \@info;
106 }
109 #=== FUNCTION ================================================================
110 # NAME: read_configfile
111 # PARAMETERS: cfg_file - string -
112 # RETURNS: nothing
113 # DESCRIPTION: read cfg_file and set variables
114 #===============================================================================
115 sub read_configfile {
116 my $cfg;
117 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
118 if( -r $main::cfg_file ) {
119 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
120 } else {
121 print STDERR "Couldn't read config file!";
122 }
123 } else {
124 $cfg = Config::IniFiles->new() ;
125 }
126 foreach my $section (keys %cfg_defaults) {
127 foreach my $param (keys %{$cfg_defaults{ $section }}) {
128 my $pinfo = $cfg_defaults{ $section }{ $param };
129 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
130 }
131 }
132 }
135 #=== FUNCTION ================================================================
136 # NAME: get_ip_and_mac
137 # PARAMETERS: nothing
138 # RETURNS: (ip, mac)
139 # DESCRIPTION: executes /sbin/ifconfig and parses the output, the first occurence
140 # of a inet address is returned as well as the mac address in the line
141 # above the inet address
142 #===============================================================================
143 sub get_ip_and_mac {
144 my $ip = "0.0.0.0.0"; # Defualt-IP
145 my $mac = "00:00:00:00:00:00"; # Default-MAC
146 my @ifconfig = qx(/sbin/ifconfig);
147 foreach(@ifconfig) {
148 if (/Hardware Adresse (\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/) {
149 $mac = "$1:$2:$3:$4:$5:$6";
150 next;
151 }
152 if (/inet Adresse:(\d+).(\d+).(\d+).(\d+)/) {
153 $ip = "$1.$2.$3.$4";
154 last;
155 }
156 }
157 return ($ip, $mac);
158 }
161 #=== FUNCTION ================================================================
162 # NAME: open_socket
163 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
164 # [PeerPort] string necessary if port not appended by PeerAddr
165 # RETURNS: socket IO::Socket::INET
166 # DESCRIPTION: open a socket to PeerAddr
167 #===============================================================================
168 sub open_socket {
169 my ($PeerAddr, $PeerPort) = @_ ;
170 if(defined($PeerPort)){
171 $PeerAddr = $PeerAddr.":".$PeerPort;
172 }
173 my $socket;
174 $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
175 Porto => "tcp" ,
176 Type => SOCK_STREAM,
177 Timeout => 5,
178 );
179 if(not defined $socket) {
180 return;
181 }
182 &main::daemon_log("open_socket to: $PeerAddr", 7);
183 return $socket;
184 }
187 #=== FUNCTION ================================================================
188 # NAME: process_incoming_msg
189 # PARAMETERS: crypted_msg - string - incoming crypted message
190 # RETURNS: nothing
191 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
192 #===============================================================================
193 sub process_incoming_msg {
194 my ($crypted_msg) = @_ ;
195 if(not defined $crypted_msg) {
196 &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
197 }
198 # &main::daemon_log("GosaPackages: crypted_msg:$crypted_msg", 7);
199 # &main::daemon_log("GosaPackages: crypted_msg len:".length($crypted_msg), 7);
201 $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
202 $crypted_msg = $1;
203 my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
205 &main::daemon_log("GosaPackages: crypted_msg:$crypted_msg", 7);
206 # &main::daemon_log("GosaPackages: crypted_msg len:".length($crypted_msg), 7);
209 # collect addresses from possible incoming clients
210 # only gosa is allowd as incoming client
211 &main::daemon_log("GosaPackages: host_key: $host", 7);
212 &main::daemon_log("GosaPackages: key_passwd: $gosa_passwd", 7);
214 $gosa_cipher = &create_ciphering($gosa_passwd);
215 # determine the correct passwd for deciphering of the incoming msgs
216 my $msg = "";
217 my $msg_hash;
218 eval{
219 $msg = &decrypt_msg($crypted_msg, $gosa_cipher);
220 &main::daemon_log("GosaPackages: decrypted_msg: $msg", 7);
222 $msg_hash = $xml->XMLin($msg, ForceArray=>1);
223 };
224 if($@) {
225 &main::daemon_log("WARNING: GosaPackages do not understand the message:", 5);
226 &main::daemon_log("$@", 7);
227 return;
228 }
230 my $header = @{$msg_hash->{header}}[0];
232 &main::daemon_log("recieve '$header' at GosaPackages from $host", 1);
233 &main::daemon_log("$msg", 7);
235 my $out_msg;
236 if ($header =~ /^job_/) {
237 $out_msg = &process_job_msg($msg, $msg_hash);
238 } elsif ($header =~ /^gosa_/) {
239 $out_msg = &process_gosa_msg($msg, $header);
240 } else {
241 &main::daemon_log("ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix");
242 }
245 if (not defined $out_msg) {
246 return;
247 }
249 if ($out_msg =~ /<jobdb_id>(\d*?)<\/jobdb_id>/) {
250 my $job_id = $1;
251 my $sql = "UPDATE '$job_queue_table_name' SET status='done', result='$out_msg' WHERE id='$job_id'";
252 my $res = $sqlite->exec_statement($sql);
253 return;
255 } else {
256 my $out_cipher = &create_ciphering($gosa_passwd);
257 $out_msg = &encrypt_msg($out_msg, $out_cipher);
258 return $out_msg;
259 }
261 }
263 sub process_gosa_msg {
264 my ($msg, $header) = @_ ;
265 my $out_msg;
266 $header =~ s/gosa_//;
267 &main::daemon_log("GosaPackages: got a gosa msg $header", 5);
269 # fetch all available eventhandler under $event_dir
270 opendir (DIR, $event_dir) or &main::daemon_log("ERROR cannot open $event_dir: $!\n", 1) and return;
271 while (defined (my $file = readdir (DIR))) {
272 if (not $file eq $header) {
273 next;
274 }
275 # try to deliver incoming msg to eventhandler
277 my $cmd = File::Spec->join($event_dir, $header)." '$msg'";
278 &main::daemon_log("GosaPackages: execute event_handler $header", 3);
279 &main::daemon_log("GosaPackages: cmd: $cmd", 7);
281 $out_msg = "";
282 open(PIPE, "$cmd 2>&1 |");
283 while(<PIPE>) {
284 $out_msg.=$_;
285 }
286 close(PIPE);
287 &main::daemon_log("GosaPackages: answer of cmd: $out_msg", 5);
288 last;
289 }
291 # if delivery not possible raise error and return
292 if (not defined $out_msg) {
293 &main::daemon_log("ERROR: GosaPackages: no event_handler defined for $header", 1);
294 } elsif ($out_msg eq "") {
295 &main::daemon_log("ERROR: GosaPackages got not answer from event_handler $header", 1);
296 }
297 return $out_msg;
299 }
302 sub process_job_msg {
303 my ($msg, $msg_hash)= @_ ;
305 my $header = @{$msg_hash->{header}}[0];
306 $header =~ s/job_//;
307 &main::daemon_log("GosaPackages: got a job msg $header", 5);
309 # check wether mac address is already known in known_daemons or known_clients
310 my $target = 'not known until now';
312 # add job to job queue
313 my $func_dic = {table=>$table_name,
314 timestamp=>@{$msg_hash->{timestamp}}[0],
315 status=>'waiting',
316 result=>'none',
317 header=>$header,
318 target=>$target,
319 xml=>$msg,
320 mac=>@{$msg_hash->{mac}}[0],
321 };
322 my $res = $sqlite->add_dbentry($func_dic);
323 if (not $res == 0) {
324 &main::daemon_log("ERROR: GosaPackages: process_job_msg: $res", 1);
325 }
327 &main::daemon_log("GosaPackages: $header job successfully added to job queue", 3);
328 return;
330 }
332 1;