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);
60 return $hash;
61 }
64 #=== FUNCTION ================================================================
65 # NAME: send_msg_hash2address
66 # PARAMETERS: msg_hash - hash - xml_hash created with function create_xml_hash
67 # PeerAddr string - socket address to send msg
68 # PeerPort string - socket port, if not included in socket address
69 # RETURNS: nothing
70 # DESCRIPTION: ????
71 #===============================================================================
72 sub send_msg_hash2address ($$$){
73 my ($msg_hash, $address, $passwd) = @_ ;
75 # fetch header for logging
76 my $header = @{$msg_hash->{header}}[0];
78 # generate xml string
79 my $msg_xml = &create_xml_string($msg_hash);
81 # create ciphering object
82 my $act_cipher = &create_ciphering($passwd);
84 # encrypt xml msg
85 my $crypted_msg = &encrypt_msg($msg_xml, $act_cipher);
87 # opensocket
88 my $socket = &open_socket($address);
89 if(not defined $socket){
90 daemon_log("cannot send '$header'-msg to $address , server not reachable", 5);
91 return 1;
92 }
94 # send xml msg
95 print $socket $crypted_msg."\n";
97 close $socket;
99 daemon_log("send '$header'-msg to $address", 1);
100 daemon_log("$msg_xml", 5);
101 return 0;
102 }
105 #=== FUNCTION ================================================================
106 # NAME: get_content_from_xml_hash
107 # PARAMETERS: xml_ref - ref - reference of the xml hash
108 # element - string - key of the value you want
109 # RETURNS: value - string - if key is either header, target or source
110 # value - list - for all other keys in xml hash
111 # DESCRIPTION:
112 #===============================================================================
113 sub get_content_from_xml_hash {
114 my ($xml_ref, $element) = @_ ;
115 #my $result = $main::xml_ref->{$element};
116 #if( $element eq "header" || $element eq "target" || $element eq "source") {
117 # return @$result[0];
118 #}
119 my @result = $xml_ref->{$element};
120 return \@result;
121 }
124 #=== FUNCTION ================================================================
125 # NAME: add_content2xml_hash
126 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
127 # element - string - key for the hash
128 # content - string - value for the hash
129 # RETURNS: nothing
130 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
131 # then append value to list
132 #===============================================================================
133 sub add_content2xml_hash {
134 my ($xml_ref, $element, $content) = @_;
135 if(not exists $$xml_ref{$element} ) {
136 $$xml_ref{$element} = [];
137 }
138 my $tmp = $$xml_ref{$element};
139 push(@$tmp, $content);
140 return;
141 }
144 #=== FUNCTION ================================================================
145 # NAME: create_xml_string
146 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
147 # RETURNS: xml_string - string - xml string representation of the hash
148 # DESCRIPTION: transform the hash to a string using XML::Simple module
149 #===============================================================================
150 sub create_xml_string {
151 my ($xml_hash) = @_ ;
152 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
153 #$xml_string =~ s/[\n]+//g;
154 #daemon_log("create_xml_string:",7);
155 #daemon_log("$xml_string\n", 7);
156 return $xml_string;
157 }
160 #=== FUNCTION ================================================================
161 # NAME: encrypt_msg
162 # PARAMETERS: msg - string - message to encrypt
163 # my_cipher - ref - reference to a Crypt::Rijndael object
164 # RETURNS: crypted_msg - string - crypted message
165 # DESCRIPTION: crypts the incoming message with the Crypt::Rijndael module
166 #===============================================================================
167 sub encrypt_msg {
168 my ($msg, $my_cipher) = @_;
169 if(not defined $my_cipher) { print "no cipher object\n"; }
170 $msg = "\0"x(16-length($msg)%16).$msg;
171 $msg = $my_cipher->encrypt($msg);
172 chomp($msg = &encode_base64($msg));
173 return $msg;
174 }
177 #=== FUNCTION ================================================================
178 # NAME: decrypt_msg
179 # PARAMETERS: crypted_msg - string - message to decrypt
180 # my_cipher - ref - reference to a Crypt::Rijndael object
181 # RETURNS: msg - string - decrypted message
182 # DESCRIPTION: decrypts the incoming message with the Crypt::Rijndael module
183 #===============================================================================
184 sub decrypt_msg {
185 my ($msg, $my_cipher) = @_ ;
186 if(defined $msg && defined $my_cipher) {
187 $msg = &decode_base64($msg);
188 }
189 $msg = $my_cipher->decrypt($msg);
190 $msg =~ s/\0*//g;
191 return $msg;
192 }
195 #=== FUNCTION ================================================================
196 # NAME: create_ciphering
197 # PARAMETERS: passwd - string - used to create ciphering
198 # RETURNS: cipher - object
199 # DESCRIPTION: creates a Crypt::Rijndael::MODE_CBC object with passwd as key
200 #===============================================================================
201 sub create_ciphering {
202 my ($passwd) = @_;
203 $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
204 my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
206 #daemon_log("iv: $iv", 7);
207 #daemon_log("key: $passwd", 7);
208 my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
209 $my_cipher->set_iv($iv);
210 return $my_cipher;
211 }
214 #=== FUNCTION ================================================================
215 # NAME: open_socket
216 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
217 # [PeerPort] string necessary if port not appended by PeerAddr
218 # RETURNS: socket IO::Socket::INET
219 # DESCRIPTION: open a socket to PeerAddr
220 #===============================================================================
221 sub open_socket {
222 my ($PeerAddr, $PeerPort) = @_ ;
223 if(defined($PeerPort)){
224 $PeerAddr = $PeerAddr.":".$PeerPort;
225 }
226 my $socket;
227 $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
228 Porto => "tcp",
229 Type => SOCK_STREAM,
230 Timeout => 5,
231 );
232 if(not defined $socket) {
233 return;
234 }
235 &daemon_log("open_socket:", 7);
236 &daemon_log("\t$PeerAddr", 7);
237 return $socket;
238 }
241 sub get_time {
242 my ($seconds, $minutes, $hours, $monthday, $month,
243 $year, $weekday, $yearday, $sommertime) = localtime(time);
244 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
245 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
246 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
247 $month+=1;
248 $month = $month < 10 ? $month = "0".$month : $month;
249 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
250 $year+=1900;
251 return "$year$month$monthday$hours$minutes$seconds";
253 }
256 #=== FUNCTION ================================================================
257 # NAME: send_msg
258 # DESCRIPTION: Send a message to a destination
259 # PARAMETERS: [header] Name of the header
260 # [from] sender ip
261 # [to] recipient ip
262 # [data] Hash containing additional attributes for the xml
263 # package
264 # RETURNS: nothing
265 #===============================================================================
266 sub send_msg ($$$$$) {
267 my ($header, $from, $to, $data, $hostkey) = @_;
269 my $out_hash = &create_xml_hash($header, $from, $to);
271 while ( my ($key, $value) = each(%$data) ) {
272 if(ref($value) eq 'ARRAY'){
273 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
274 } else {
275 &add_content2xml_hash($out_hash, $key, $value);
276 }
277 }
279 &send_msg_hash2address($out_hash, $to, $hostkey);
280 }
282 1;