1 package GOSA::GosaSupportDaemon;
3 use Exporter;
4 @ISA = qw(Exporter);
5 @EXPORT = qw(create_xml_hash send_msg_hash2address get_content_from_xml_hash add_content2xml_hash create_xml_string encrypt_msg decrypt_msg create_ciphering transform_msg2hash get_time send_msg);
7 use strict;
8 use warnings;
9 use IO::Socket::INET;
10 use Crypt::Rijndael;
11 use Digest::MD5 qw(md5 md5_hex md5_base64);
12 use MIME::Base64;
13 use XML::Simple;
17 BEGIN {}
19 END {}
21 ### Start ######################################################################
23 my $xml = new XML::Simple();
25 sub process_incoming_msg {
26 return;
27 }
29 sub daemon_log {
30 my ($msg, $level) = @_ ;
31 &main::daemon_log($msg, $level);
32 return;
33 }
36 #=== FUNCTION ================================================================
37 # NAME: create_xml_hash
38 # PARAMETERS: header - string - message header (required)
39 # source - string - where the message come from (required)
40 # target - string - where the message should go to (required)
41 # [header_value] - string - something usefull (optional)
42 # RETURNS: hash - hash - nomen est omen
43 # DESCRIPTION: creates a key-value hash, all values are stored in a array
44 #===============================================================================
45 sub create_xml_hash {
46 my ($header, $source, $target, $header_value) = @_;
47 my $hash = {
48 header => [$header],
49 source => [$source],
50 target => [$target],
51 $header => [$header_value],
52 };
53 return $hash
54 }
57 sub transform_msg2hash {
58 my ($msg) = @_ ;
59 my $hash = $xml->XMLin($msg, ForceArray=>1);
61 # xml tags without a content are created as an empty hash
62 # substitute it with an empty list
63 eval {
64 while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
65 if( 1 == @{ $xml_content } ) {
66 # there is only one element in xml_content list ...
67 my $element = @{ $xml_content }[0];
68 if( ref($element) eq "HASH" ) {
69 # and this element is an hash ...
70 my $len_element = keys %{ $element };
71 if( $len_element == 0 ) {
72 # and this hash is empty, then substitute the xml_content
73 # with an empty string in list
74 $hash->{$xml_tag} = [ "none" ];
75 }
76 }
77 }
78 }
79 };
80 if( $@ ) {
81 $hash = undef;
82 }
84 return $hash;
85 }
88 #=== FUNCTION ================================================================
89 # NAME: send_msg_hash2address
90 # PARAMETERS: msg_hash - hash - xml_hash created with function create_xml_hash
91 # PeerAddr string - socket address to send msg
92 # PeerPort string - socket port, if not included in socket address
93 # RETURNS: nothing
94 # DESCRIPTION: ????
95 #===============================================================================
96 sub send_msg_hash2address ($$$){
97 my ($msg_hash, $address, $passwd) = @_ ;
99 # fetch header for logging
100 my $header = @{$msg_hash->{header}}[0];
102 # generate xml string
103 my $msg_xml = &create_xml_string($msg_hash);
105 # create ciphering object
106 my $act_cipher = &create_ciphering($passwd);
108 # encrypt xml msg
109 my $crypted_msg = &encrypt_msg($msg_xml, $act_cipher);
111 # opensocket
112 my $socket = &open_socket($address);
113 if(not defined $socket){
114 daemon_log("cannot send '$header'-msg to $address , server not reachable", 5);
115 return 1;
116 }
118 # send xml msg
119 print $socket $crypted_msg."\n";
121 close $socket;
123 daemon_log("send '$header'-msg to $address", 1);
124 daemon_log("message:\n$msg_xml", 8);
125 return 0;
126 }
129 #=== FUNCTION ================================================================
130 # NAME: get_content_from_xml_hash
131 # PARAMETERS: xml_ref - ref - reference of the xml hash
132 # element - string - key of the value you want
133 # RETURNS: value - string - if key is either header, target or source
134 # value - list - for all other keys in xml hash
135 # DESCRIPTION:
136 #===============================================================================
137 sub get_content_from_xml_hash {
138 my ($xml_ref, $element) = @_ ;
139 #my $result = $main::xml_ref->{$element};
140 #if( $element eq "header" || $element eq "target" || $element eq "source") {
141 # return @$result[0];
142 #}
143 my @result = $xml_ref->{$element};
144 return \@result;
145 }
148 #=== FUNCTION ================================================================
149 # NAME: add_content2xml_hash
150 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
151 # element - string - key for the hash
152 # content - string - value for the hash
153 # RETURNS: nothing
154 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
155 # then append value to list
156 #===============================================================================
157 sub add_content2xml_hash {
158 my ($xml_ref, $element, $content) = @_;
159 if(not exists $$xml_ref{$element} ) {
160 $$xml_ref{$element} = [];
161 }
162 my $tmp = $$xml_ref{$element};
163 push(@$tmp, $content);
164 return;
165 }
168 #=== FUNCTION ================================================================
169 # NAME: create_xml_string
170 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
171 # RETURNS: xml_string - string - xml string representation of the hash
172 # DESCRIPTION: transform the hash to a string using XML::Simple module
173 #===============================================================================
174 sub create_xml_string {
175 my ($xml_hash) = @_ ;
176 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
177 #$xml_string =~ s/[\n]+//g;
178 #daemon_log("create_xml_string:",7);
179 #daemon_log("$xml_string\n", 7);
180 return $xml_string;
181 }
184 #=== FUNCTION ================================================================
185 # NAME: encrypt_msg
186 # PARAMETERS: msg - string - message to encrypt
187 # my_cipher - ref - reference to a Crypt::Rijndael object
188 # RETURNS: crypted_msg - string - crypted message
189 # DESCRIPTION: crypts the incoming message with the Crypt::Rijndael module
190 #===============================================================================
191 sub encrypt_msg {
192 my ($msg, $my_cipher) = @_;
193 if(not defined $my_cipher) { print "no cipher object\n"; }
194 $msg = "\0"x(16-length($msg)%16).$msg;
195 $msg = $my_cipher->encrypt($msg);
196 chomp($msg = &encode_base64($msg));
197 return $msg;
198 }
201 #=== FUNCTION ================================================================
202 # NAME: decrypt_msg
203 # PARAMETERS: crypted_msg - string - message to decrypt
204 # my_cipher - ref - reference to a Crypt::Rijndael object
205 # RETURNS: msg - string - decrypted message
206 # DESCRIPTION: decrypts the incoming message with the Crypt::Rijndael module
207 #===============================================================================
208 sub decrypt_msg {
209 my ($msg, $my_cipher) = @_ ;
210 if(defined $msg && defined $my_cipher) {
211 $msg = &decode_base64($msg);
212 }
213 $msg = $my_cipher->decrypt($msg);
214 $msg =~ s/\0*//g;
215 return $msg;
216 }
219 #=== FUNCTION ================================================================
220 # NAME: create_ciphering
221 # PARAMETERS: passwd - string - used to create ciphering
222 # RETURNS: cipher - object
223 # DESCRIPTION: creates a Crypt::Rijndael::MODE_CBC object with passwd as key
224 #===============================================================================
225 sub create_ciphering {
226 my ($passwd) = @_;
227 $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
228 my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
230 #daemon_log("iv: $iv", 7);
231 #daemon_log("key: $passwd", 7);
232 my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
233 $my_cipher->set_iv($iv);
234 return $my_cipher;
235 }
238 #=== FUNCTION ================================================================
239 # NAME: open_socket
240 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
241 # [PeerPort] string necessary if port not appended by PeerAddr
242 # RETURNS: socket IO::Socket::INET
243 # DESCRIPTION: open a socket to PeerAddr
244 #===============================================================================
245 sub open_socket {
246 my ($PeerAddr, $PeerPort) = @_ ;
247 if(defined($PeerPort)){
248 $PeerAddr = $PeerAddr.":".$PeerPort;
249 }
250 my $socket;
251 $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
252 Porto => "tcp",
253 Type => SOCK_STREAM,
254 Timeout => 5,
255 );
256 if(not defined $socket) {
257 return;
258 }
259 &daemon_log("open_socket: $PeerAddr", 7);
260 return $socket;
261 }
264 sub get_time {
265 my ($seconds, $minutes, $hours, $monthday, $month,
266 $year, $weekday, $yearday, $sommertime) = localtime(time);
267 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
268 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
269 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
270 $month+=1;
271 $month = $month < 10 ? $month = "0".$month : $month;
272 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
273 $year+=1900;
274 return "$year$month$monthday$hours$minutes$seconds";
276 }
279 #=== FUNCTION ================================================================
280 # NAME: send_msg
281 # DESCRIPTION: Send a message to a destination
282 # PARAMETERS: [header] Name of the header
283 # [from] sender ip
284 # [to] recipient ip
285 # [data] Hash containing additional attributes for the xml
286 # package
287 # RETURNS: nothing
288 #===============================================================================
289 sub send_msg ($$$$$) {
290 my ($header, $from, $to, $data, $hostkey) = @_;
292 my $out_hash = &create_xml_hash($header, $from, $to);
294 while ( my ($key, $value) = each(%$data) ) {
295 if(ref($value) eq 'ARRAY'){
296 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
297 } else {
298 &add_content2xml_hash($out_hash, $key, $value);
299 }
300 }
302 &send_msg_hash2address($out_hash, $to, $hostkey);
303 }
305 1;