diff --git a/interface/lib/classes/functions.inc.php b/interface/lib/classes/functions.inc.php index 3a9fcb9a10d4a891a2a36fbb637250646a759796..da35a370025a75215ef61c7934ad3e778ff58327 100644 --- a/interface/lib/classes/functions.inc.php +++ b/interface/lib/classes/functions.inc.php @@ -437,6 +437,23 @@ class functions { return $customer_no; } + + public function generate_ssh_key($client_id, $username = ''){ + global $app; + + // generate the SSH key pair for the client + $id_rsa_file = '/tmp/'.uniqid('',true); + $id_rsa_pub_file = $id_rsa_file.'.pub'; + if(file_exists($id_rsa_file)) unset($id_rsa_file); + if(file_exists($id_rsa_pub_file)) unset($id_rsa_pub_file); + if(!file_exists($id_rsa_file) && !file_exists($id_rsa_pub_file)) { + exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f '.$id_rsa_file.' -N ""'); + $app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", @file_get_contents($id_rsa_file), @file_get_contents($id_rsa_pub_file), $client_id); + exec('rm -f '.$id_rsa_file.' '.$id_rsa_pub_file); + } else { + $app->log("Failed to create SSH keypair for ".$username, LOGLEVEL_WARN); + } + } } ?> diff --git a/interface/lib/classes/remoting.inc.php b/interface/lib/classes/remoting.inc.php index 5934646bacf892eddaf002161fb90a1bfc34c331..69ebac2e4c3bc628aee969470974861ba22cbe7b 100644 --- a/interface/lib/classes/remoting.inc.php +++ b/interface/lib/classes/remoting.inc.php @@ -230,9 +230,8 @@ class remoting { */ /* copied from the client_edit php */ - exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""'); - $app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", @file_get_contents('/tmp/id_rsa'), @file_get_contents('/tmp/id_rsa.pub'), $this->id); - exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub'); + $app->uses('functions'); + $app->functions->generate_ssh_key($this->id, $username); diff --git a/interface/web/client/client_edit.php b/interface/web/client/client_edit.php index 3f63c7ee8cae0bcdc37806ccf6891098810c15e2..9345b5b7c357139cfe4c8bda92c243de94c58d3a 100644 --- a/interface/web/client/client_edit.php +++ b/interface/web/client/client_edit.php @@ -260,9 +260,8 @@ class page_action extends tform_actions { // Create the controlpaneluser for the client //Generate ssh-rsa-keys - exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""'); - $app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", @file_get_contents('/tmp/id_rsa'), @file_get_contents('/tmp/id_rsa.pub'), $this->id); - exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub'); + $app->uses('functions'); + $app->functions->generate_ssh_key($this->id, $username); // Create the controlpaneluser for the client $sql = "INSERT INTO sys_user (username,passwort,modules,startmodule,app_theme,typ,active,language,groups,default_group,client_id) diff --git a/interface/web/tools/import_vpopmail.php b/interface/web/tools/import_vpopmail.php index 242ea5f2b5833f585ce67ae9e8dd124a18447e3d..9e560cdf30455be6c9e3459aec98334cfa3a7a54 100644 --- a/interface/web/tools/import_vpopmail.php +++ b/interface/web/tools/import_vpopmail.php @@ -133,9 +133,8 @@ function start_import() { // Create the controlpaneluser for the client //Generate ssh-rsa-keys - exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""'); - $app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", @file_get_contents('/tmp/id_rsa'), @file_get_contents('/tmp/id_rsa.pub'), $client_id); - exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub'); + $app->uses('functions'); + $app->functions->generate_ssh_key($client_id, $username); // Create the controlpaneluser for the client $sql = "INSERT INTO sys_user (username,passwort,modules,startmodule,app_theme,typ,active,language,groups,default_group,client_id) diff --git a/server/lib/classes/functions.inc.php b/server/lib/classes/functions.inc.php index 1f9c6b6efcbf18f3f14267a33389b2543dac590b..e36ed5b04f5f4b8853a2f35bcec5358d7dc9f09e 100644 --- a/server/lib/classes/functions.inc.php +++ b/server/lib/classes/functions.inc.php @@ -415,6 +415,23 @@ class functions { } return implode("\n", $domains); } + + public function generate_ssh_key($client_id, $username = ''){ + global $app; + + // generate the SSH key pair for the client + $id_rsa_file = '/tmp/'.uniqid('',true); + $id_rsa_pub_file = $id_rsa_file.'.pub'; + if(file_exists($id_rsa_file)) unset($id_rsa_file); + if(file_exists($id_rsa_pub_file)) unset($id_rsa_pub_file); + if(!file_exists($id_rsa_file) && !file_exists($id_rsa_pub_file)) { + exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f '.$id_rsa_file.' -N ""'); + $app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", $app->system->file_get_contents($id_rsa_file), $app->system->file_get_contents($id_rsa_pub_file), $client_id); + exec('rm -f '.$id_rsa_file.' '.$id_rsa_pub_file); + } else { + $app->log("Failed to create SSH keypair for ".$username, LOGLEVEL_WARN); + } + } } diff --git a/server/plugins-available/shelluser_base_plugin.inc.php b/server/plugins-available/shelluser_base_plugin.inc.php index 71275d27a99f709a707b9dbf1f7d20e87e990d24..d3376f10ca37477a31a0375ecba3784ce86739ed 100755 --- a/server/plugins-available/shelluser_base_plugin.inc.php +++ b/server/plugins-available/shelluser_base_plugin.inc.php @@ -450,16 +450,8 @@ class shelluser_base_plugin { // If this user has no key yet, generate a pair if ($userkey == '' && $id > 0){ //Generate ssh-rsa-keys - exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""'); - - // use the public key that has been generated - $userkey = $app->system->file_get_contents('/tmp/id_rsa.pub'); - - // save keypair in client table - $this->app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", $app->system->file_get_contents('/tmp/id_rsa'), $userkey, $id); - - $app->system->unlink('/tmp/id_rsa'); - $app->system->unlink('/tmp/id_rsa.pub'); + $app->uses('functions'); + $app->functions->generate_ssh_key($id, $username); $this->app->log("ssh-rsa keypair generated for ".$username, LOGLEVEL_DEBUG); }; diff --git a/server/plugins-available/shelluser_jailkit_plugin.inc.php b/server/plugins-available/shelluser_jailkit_plugin.inc.php index 16dbcc7a67c4f50625e46009f30e4d0c6dca91cf..291c771e6c4568eb23ffc93346fa0d9ed1204423 100755 --- a/server/plugins-available/shelluser_jailkit_plugin.inc.php +++ b/server/plugins-available/shelluser_jailkit_plugin.inc.php @@ -468,16 +468,9 @@ class shelluser_jailkit_plugin { // If this user has no key yet, generate a pair if ($userkey == '' && $id > 0){ //Generate ssh-rsa-keys - exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""'); - - // use the public key that has been generated - $userkey = $app->system->file_get_contents('/tmp/id_rsa.pub'); - - // save keypair in client table - $this->app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ? ssh_rsa = ? WHERE client_id = ?", $app->system->file_get_contents('/tmp/id_rsa'), $userkey, $id); - - $app->system->unlink('/tmp/id_rsa'); - $app->system->unlink('/tmp/id_rsa.pub'); + $app->uses('functions'); + $app->functions->generate_ssh_key($id, $username); + $this->app->log("ssh-rsa keypair generated for ".$username, LOGLEVEL_DEBUG); };