From 98c0483a5014a5df92ba25e0c136ce8594f50627 Mon Sep 17 00:00:00 2001 From: rettenbe Date: Fri, 7 Dec 2007 11:00:13 +0000 Subject: [PATCH] communication between gosa and gosa support daemon is working git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8057 594d385d-05f5-0310-b6e9-bd551577e9d8 --- contrib/daemon/gosa-sd | 31 +++++++++------ contrib/daemon/gosa-sd-bus | 52 +++++++++++++++----------- contrib/daemon/modules/GosaPackages.pm | 18 ++++----- contrib/daemon/testGosa.pl | 52 ++++++++++++++++++++------ contrib/socket_server/client.php | 10 +++-- include/class_socketClient.inc | 3 +- 6 files changed, 108 insertions(+), 58 deletions(-) diff --git a/contrib/daemon/gosa-sd b/contrib/daemon/gosa-sd index 43f4c3a35..ee96e6205 100755 --- a/contrib/daemon/gosa-sd +++ b/contrib/daemon/gosa-sd @@ -28,7 +28,8 @@ use Time::HiRes qw( gettimeofday ); use Fcntl; use IO::Socket::INET; -use Crypt::CBC; +use Crypt::Rijndael; +use MIME::Base64; use Digest::MD5 qw(md5 md5_hex md5_base64); use XML::Simple; use Data::Dumper; @@ -850,7 +851,7 @@ sub create_xml_hash { sub create_xml_string { my ($xml_hash) = @_ ; my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml'); - #$xml_string =~ s/[\n]+//g; + $xml_string =~ s/[\n]+//g; daemon_log("create_xml_string:",7); daemon_log("$xml_string\n", 7); return $xml_string; @@ -904,9 +905,9 @@ sub get_content_from_xml_hash { sub encrypt_msg { my ($msg, $my_cipher) = @_; if(not defined $my_cipher) { print "no cipher object\n"; } - + $msg = "\0"x(16-length($msg)%16).$msg; my $crypted_msg = $my_cipher->encrypt($msg); - + chomp($crypted_msg = &encode_base64($crypted_msg)); return $crypted_msg; } @@ -920,6 +921,7 @@ sub encrypt_msg { #=============================================================================== sub decrypt_msg { my ($crypted_msg, $my_cipher) = @_ ; + $crypted_msg = &decode_base64($crypted_msg); my $msg = $my_cipher->decrypt($crypted_msg); return $msg; } @@ -934,16 +936,12 @@ sub decrypt_msg { sub create_ciphering { my ($passwd) = @_; $passwd = substr(md5_hex("$passwd") x 32, 0, 32); - my $iv = substr(md5_hex('GONICUS GmbH'),0, 16); daemon_log("iv: $iv", 7); daemon_log("key: $passwd", 7); - my $my_cipher = Crypt::CBC->new(-key=>$passwd , - -cipher => 'Rijndael', - -iv => $iv, - -header => "none", - ); + my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC()); + $my_cipher->set_iv($iv); return $my_cipher; } @@ -1978,7 +1976,18 @@ while(1) { if (vec($rout, fileno $fhd, 1) ) { daemon_log("process child $pid is ready to read", 5); - chomp( my $in_msg = <$fhd> ); + + $fhd->blocking(1); + my $in_msg = <$fhd>; + $fhd->blocking(0); + my $part_in_msg; + while ($part_in_msg = <$fhd>) { + if (not defined $part_in_msg) { + last; + } + $in_msg .= $part_in_msg; + } + daemon_log("process child read: $in_msg\n", 5); if (not defined $in_msg) { next; diff --git a/contrib/daemon/gosa-sd-bus b/contrib/daemon/gosa-sd-bus index b1e118f0e..9987d47f3 100755 --- a/contrib/daemon/gosa-sd-bus +++ b/contrib/daemon/gosa-sd-bus @@ -26,7 +26,8 @@ use POSIX; use Time::HiRes qw( gettimeofday ); use IO::Socket::INET; -use Crypt::CBC; +use Crypt::Rijndael; +use MIME::Base64; use Digest::MD5 qw(md5 md5_hex md5_base64); use XML::Simple; use Data::Dumper; @@ -460,8 +461,9 @@ sub process_incoming_msg { daemon_log("daemon_passwd: $key_passwd\n", 7); my $key_cipher = &create_ciphering($key_passwd); $msg = &decrypt_msg($crypted_msg, $key_cipher); - daemon_log("daemon decrypted msg: $msg", 7); + daemon_log("daemon decrypted msg:$msg", 7); $msg_hash = $xml->XMLin($msg, ForceArray=>1); + print Dumper $msg_hash; }; if($@) { daemon_log("msg processing raise error", 7); @@ -473,7 +475,7 @@ sub process_incoming_msg { } if($msg_flag >= $l) { - daemon_log("\nERROR: do not understand the message:\n\t$msg" , 1); + daemon_log("\nERROR: do not understand the message:\n$msg" , 1); return; } @@ -580,16 +582,13 @@ sub create_passwd { #=============================================================================== sub create_ciphering { my ($passwd) = @_; - $passwd = substr("$passwd" x 32, 0, 32); - daemon_log("create_ciphering: new passwd: $passwd", 7); - + $passwd = substr(md5_hex("$passwd") x 32, 0, 32); my $iv = substr(md5_hex('GONICUS GmbH'),0, 16); - my $my_cipher = Crypt::CBC->new(-key=>$passwd , - -cipher => 'Rijndael', - -iv => $iv, - -header => "none", - ); + daemon_log("iv: $iv", 7); + daemon_log("key: $passwd", 7); + my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC()); + $my_cipher->set_iv($iv); return $my_cipher; } @@ -604,9 +603,9 @@ sub create_ciphering { sub encrypt_msg { my ($msg, $my_cipher) = @_; if(not defined $my_cipher) { print "no cipher object\n"; } - + $msg = "\0"x(16-length($msg)%16).$msg; my $crypted_msg = $my_cipher->encrypt($msg); - + chomp($crypted_msg = &encode_base64($crypted_msg)); return $crypted_msg; } @@ -620,6 +619,7 @@ sub encrypt_msg { #=============================================================================== sub decrypt_msg { my ($crypted_msg, $my_cipher) = @_ ; + $crypted_msg = &decode_base64($crypted_msg); my $msg = $my_cipher->decrypt($crypted_msg); return $msg; } @@ -739,15 +739,25 @@ sub open_socket { #=============================================================================== sub read_from_socket { my ($socket) = @_; - my $result = ""; - my $len = 16; - while($len == 16){ - my $char; - $len = sysread($socket, $char, 16); - if($len != 16) { last } - if($len != 16) { last } - $result .= $char; + + $socket->blocking(1); + my $result = <$socket>; + $socket->blocking(0); + my $part_msg; + while ($part_msg = <$socket>) { + if (not defined $part_msg) { last; } + $result .= $part_msg; } + + #my $result = ""; + #my $len = 16; + #while($len == 16){ + # my $char; + # $len = sysread($socket, $char, 16); + # if($len != 16) { last } + # if($len != 16) { last } + # $result .= $char; + #} return $result; } diff --git a/contrib/daemon/modules/GosaPackages.pm b/contrib/daemon/modules/GosaPackages.pm index a2450cb9d..48a9a60d0 100644 --- a/contrib/daemon/modules/GosaPackages.pm +++ b/contrib/daemon/modules/GosaPackages.pm @@ -34,11 +34,6 @@ sub get_module_tags { return \%tag_hash; } -#sub read_configfile { -# &main::read_configfile(); -# return; -#} - sub process_incoming_msg { my ($crypted_msg) = @_ ; @@ -69,7 +64,7 @@ sub process_incoming_msg { $msg = &main::decrypt_msg($crypted_msg, $gosa_cipher); &main::daemon_log("GosaPackages: decrypted_msg: $msg", 7); - $msg_hash = $main::xml->XMLin($msg, ForceArray=>1); + #$msg_hash = $main::xml->XMLin($msg, ForceArray=>1); }; if($@) { &main::daemon_log("ERROR: GosaPackages do not understand the message: $@", 1); @@ -80,11 +75,12 @@ sub process_incoming_msg { &main::daemon_log("\t$host", 1); &main::daemon_log("GosaPackages: msg to process:", 5); &main::daemon_log("\t$msg", 5); - - - - - return "GosaPackages got msg: $msg"; + + $msg = "GosaPackages got msg: ".$msg; + + my $out_cipher = &main::create_ciphering($main::gosa_passwd); + my $out_msg = &main::encrypt_msg($msg, $out_cipher); + return $out_msg; } diff --git a/contrib/daemon/testGosa.pl b/contrib/daemon/testGosa.pl index 3e120b661..9ecb8f385 100644 --- a/contrib/daemon/testGosa.pl +++ b/contrib/daemon/testGosa.pl @@ -22,31 +22,38 @@ use strict; use warnings; use IO::Socket::INET; use Digest::MD5 qw(md5 md5_hex md5_base64); -use Crypt::CBC; - +use Crypt::Rijndael; +use MIME::Base64; sub create_ciphering { my ($passwd) = @_; - $passwd = substr(md5_hex("$passwd") x 32, 0, 32); + $passwd = substr(md5_hex("$passwd") x 32, 0, 32); my $iv = substr(md5_hex('GONICUS GmbH'),0, 16); - print "iv: $iv\n"; print "key: $passwd\n"; - my $my_cipher = Crypt::CBC->new(-key=>$passwd , - -cipher => 'Rijndael', - -iv => $iv, - -header => "none", - ); + + my $my_cipher = Crypt::Rijndael->new($passwd ,Crypt::Rijndael::MODE_CBC() ); + $my_cipher->set_iv($iv); return $my_cipher; } sub decrypt_msg { my ($crypted_msg, $my_cipher) = @_ ; + $crypted_msg = &decode_base64($crypted_msg); my $msg = $my_cipher->decrypt($crypted_msg); return $msg; } +sub encrypt_msg { + my ($msg, $my_cipher) = @_; + if(not defined $my_cipher) { print "no cipher object\n"; } + $msg = "\0"x(16-length($msg)%16).$msg; + my $crypted_msg = $my_cipher->encrypt($msg); + chomp($crypted_msg = &encode_base64($crypted_msg)); + return $crypted_msg; +} + my $gosa_server = IO::Socket::INET->new(LocalPort => "9999", @@ -68,10 +75,33 @@ if(not defined $other_end) { my $actual_ip = inet_ntoa($iaddr); print "accept client at gosa socket from $actual_ip\n"; chomp(my $crypted_msg = <$client>); - print "crypted msg: >>>$crypted_msg<<<\n"; + print "crypted msg: <<<$crypted_msg<<<\n"; my $cipher = &create_ciphering("ferdinand_frost"); my $msg = &decrypt_msg($crypted_msg, $cipher); - print "msg: >>>$msg<<<\n"; + print "msg: <<<$msg<<<\n"; + + print "\n#################################\n\n"; + + my $answer = "gosa answer: $msg"; + + print "answer: $answer\n"; + + my $out_cipher = &create_ciphering("ferdinand_frost"); + my $crypted_answer = &encrypt_msg($answer, $out_cipher); + + print $client $crypted_answer."\n"; + } + +sleep(3); +close($client); + + + + + + + + diff --git a/contrib/socket_server/client.php b/contrib/socket_server/client.php index d3a1b396b..8d88d88f4 100755 --- a/contrib/socket_server/client.php +++ b/contrib/socket_server/client.php @@ -4,14 +4,18 @@ require_once("../../include/class_socketClient.inc"); error_reporting(E_ALL); -$sock = new Socket_Client("10.89.1.182","10000",TRUE,1); +$sock = new Socket_Client("10.89.1.155","9999",TRUE,1); $sock->setEncryptionKey("ferdinand_frost"); if($sock->connected()){ /* Prepare a hunge bunch of data to be send */ - $data = "Hallo Andi. Alles wird toll."; + $data = "Hallo Andi. Alles Wird Toll."; $sock->write($data); - echo $sock->read(); + + #$sock->setEncryptionKey("ferdinand_frost"); + + $answer = $sock->read(); + echo "$answer\n"; $sock->close(); }else{ echo "... FAILED!\n"; diff --git a/include/class_socketClient.inc b/include/class_socketClient.inc index d31e6764f..ae72d1051 100755 --- a/include/class_socketClient.inc +++ b/include/class_socketClient.inc @@ -51,13 +51,14 @@ class Socket_Client private function encrypt($data) { mcrypt_generic_init($this->td, $this->ckey, $this->iv); - return mcrypt_generic($this->td, $data); + return base64_encode(mcrypt_generic($this->td, $data)); } private function decrypt($data) { /* decrypt data */ + $data = base64_decode($data); mcrypt_generic_init($this->td, $this->ckey, $this->iv); return mdecrypt_generic($this->td, $data); } -- 2.30.2