Code

bb3a305811ab71224ff3ba417d3f8b8ad193e405
[gosa.git] / gosa-si / modules / GosaSupportDaemon.pm
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);
80     
81     # create ciphering object
82     my $act_cipher = &create_ciphering($passwd);
83     
84     # encrypt xml msg
85     my $crypted_msg = &encrypt_msg($msg_xml, $act_cipher);
86     
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     }
93     
94     # send xml msg
95     print $socket $crypted_msg."\n";
96     
97     close $socket;
99     daemon_log("send '$header'-msg to $address", 1);
100     daemon_log("$msg_xml", 5);
101     return 0;
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;
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;
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;
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     my $crypted_msg = $my_cipher->encrypt($msg);
172     chomp($crypted_msg = &encode_base64($crypted_msg));
173     return $crypted_msg;
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 ($crypted_msg, $my_cipher) = @_ ;
186     $crypted_msg = &decode_base64($crypted_msg);
187     my $msg = $my_cipher->decrypt($crypted_msg); 
188     $msg =~ s/\0*//g;
189     return $msg;
193 #===  FUNCTION  ================================================================
194 #         NAME:  create_ciphering
195 #   PARAMETERS:  passwd - string - used to create ciphering
196 #      RETURNS:  cipher - object
197 #  DESCRIPTION:  creates a Crypt::Rijndael::MODE_CBC object with passwd as key
198 #===============================================================================
199 sub create_ciphering {
200     my ($passwd) = @_;
201     $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
202     my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
204     #daemon_log("iv: $iv", 7);
205     #daemon_log("key: $passwd", 7);
206     my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
207     $my_cipher->set_iv($iv);
208     return $my_cipher;
212 #===  FUNCTION  ================================================================
213 #         NAME:  open_socket
214 #   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
215 #                [PeerPort] string necessary if port not appended by PeerAddr
216 #      RETURNS:  socket IO::Socket::INET
217 #  DESCRIPTION:  open a socket to PeerAddr
218 #===============================================================================
219 sub open_socket {
220     my ($PeerAddr, $PeerPort) = @_ ;
221     if(defined($PeerPort)){
222         $PeerAddr = $PeerAddr.":".$PeerPort;
223     }
224     my $socket;
225     $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
226             Porto => "tcp",
227             Type => SOCK_STREAM,
228             Timeout => 5,
229             );
230     if(not defined $socket) {
231         return;
232     }
233     &daemon_log("open_socket:", 7);
234     &daemon_log("\t$PeerAddr", 7);
235     return $socket;
239 sub get_time {
240     my ($seconds, $minutes, $hours, $monthday, $month,
241             $year, $weekday, $yearday, $sommertime) = localtime(time);
242     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
243     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
244     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
245     $month+=1;
246     $month = $month < 10 ? $month = "0".$month : $month;
247     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
248     $year+=1900;
249     return "$year$month$monthday$hours$minutes$seconds";
254 #===  FUNCTION  ================================================================
255 #         NAME: send_msg
256 #  DESCRIPTION: Send a message to a destination
257 #   PARAMETERS: [header] Name of the header
258 #               [from]   sender ip
259 #               [to]     recipient ip
260 #               [data]   Hash containing additional attributes for the xml
261 #                        package
262 #      RETURNS:  nothing
263 #===============================================================================
264 sub send_msg ($$$$$) {
265         my ($header, $from, $to, $data, $hostkey) = @_;
267         my $out_hash = &create_xml_hash($header, $from, $to);
269         while ( my ($key, $value) = each(%$data) ) {
270                 if(ref($value) eq 'ARRAY'){
271                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
272                 } else {
273                         &add_content2xml_hash($out_hash, $key, $value);
274                 }
275         }
277         &send_msg_hash2address($out_hash, $to, $hostkey);
280 1;