Skip to content

Commit 2cfdbcd

Browse files
author
Till Brehm
committed
Improved the code that generates the ssh keys for clients and shell users and reorganized it into a central function.
1 parent 5516b5b commit 2cfdbcd

File tree

7 files changed

+45
-29
lines changed

7 files changed

+45
-29
lines changed

interface/lib/classes/functions.inc.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,23 @@ public function generate_customer_no(){
437437

438438
return $customer_no;
439439
}
440+
441+
public function generate_ssh_key($client_id, $username = ''){
442+
global $app;
443+
444+
// generate the SSH key pair for the client
445+
$id_rsa_file = '/tmp/'.uniqid('',true);
446+
$id_rsa_pub_file = $id_rsa_file.'.pub';
447+
if(file_exists($id_rsa_file)) unset($id_rsa_file);
448+
if(file_exists($id_rsa_pub_file)) unset($id_rsa_pub_file);
449+
if(!file_exists($id_rsa_file) && !file_exists($id_rsa_pub_file)) {
450+
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f '.$id_rsa_file.' -N ""');
451+
$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);
452+
exec('rm -f '.$id_rsa_file.' '.$id_rsa_pub_file);
453+
} else {
454+
$app->log("Failed to create SSH keypair for ".$username, LOGLEVEL_WARN);
455+
}
456+
}
440457
}
441458

442459
?>

interface/lib/classes/remoting.inc.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,8 @@ protected function klientadd($formdef_file, $reseller_id, $params)
230230
*/
231231

232232
/* copied from the client_edit php */
233-
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
234-
$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);
235-
exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
233+
$app->uses('functions');
234+
$app->functions->generate_ssh_key($this->id, $username);
236235

237236

238237

interface/web/client/client_edit.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,8 @@ function onAfterInsert() {
260260

261261
// Create the controlpaneluser for the client
262262
//Generate ssh-rsa-keys
263-
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
264-
$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);
265-
exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
263+
$app->uses('functions');
264+
$app->functions->generate_ssh_key($this->id, $username);
266265

267266
// Create the controlpaneluser for the client
268267
$sql = "INSERT INTO sys_user (username,passwort,modules,startmodule,app_theme,typ,active,language,groups,default_group,client_id)

interface/web/tools/import_vpopmail.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,8 @@ function start_import() {
133133

134134
// Create the controlpaneluser for the client
135135
//Generate ssh-rsa-keys
136-
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
137-
$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);
138-
exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
136+
$app->uses('functions');
137+
$app->functions->generate_ssh_key($client_id, $username);
139138

140139
// Create the controlpaneluser for the client
141140
$sql = "INSERT INTO sys_user (username,passwort,modules,startmodule,app_theme,typ,active,language,groups,default_group,client_id)

server/lib/classes/functions.inc.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,23 @@ public function idn_decode($domain) {
415415
}
416416
return implode("\n", $domains);
417417
}
418+
419+
public function generate_ssh_key($client_id, $username = ''){
420+
global $app;
421+
422+
// generate the SSH key pair for the client
423+
$id_rsa_file = '/tmp/'.uniqid('',true);
424+
$id_rsa_pub_file = $id_rsa_file.'.pub';
425+
if(file_exists($id_rsa_file)) unset($id_rsa_file);
426+
if(file_exists($id_rsa_pub_file)) unset($id_rsa_pub_file);
427+
if(!file_exists($id_rsa_file) && !file_exists($id_rsa_pub_file)) {
428+
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f '.$id_rsa_file.' -N ""');
429+
$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);
430+
exec('rm -f '.$id_rsa_file.' '.$id_rsa_pub_file);
431+
} else {
432+
$app->log("Failed to create SSH keypair for ".$username, LOGLEVEL_WARN);
433+
}
434+
}
418435

419436
}
420437

server/plugins-available/shelluser_base_plugin.inc.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -450,16 +450,8 @@ private function _setup_ssh_rsa() {
450450
// If this user has no key yet, generate a pair
451451
if ($userkey == '' && $id > 0){
452452
//Generate ssh-rsa-keys
453-
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
454-
455-
// use the public key that has been generated
456-
$userkey = $app->system->file_get_contents('/tmp/id_rsa.pub');
457-
458-
// save keypair in client table
459-
$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);
460-
461-
$app->system->unlink('/tmp/id_rsa');
462-
$app->system->unlink('/tmp/id_rsa.pub');
453+
$app->uses('functions');
454+
$app->functions->generate_ssh_key($id, $username);
463455
$this->app->log("ssh-rsa keypair generated for ".$username, LOGLEVEL_DEBUG);
464456
};
465457

server/plugins-available/shelluser_jailkit_plugin.inc.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,9 @@ private function _setup_ssh_rsa() {
468468
// If this user has no key yet, generate a pair
469469
if ($userkey == '' && $id > 0){
470470
//Generate ssh-rsa-keys
471-
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
472-
473-
// use the public key that has been generated
474-
$userkey = $app->system->file_get_contents('/tmp/id_rsa.pub');
475-
476-
// save keypair in client table
477-
$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);
478-
479-
$app->system->unlink('/tmp/id_rsa');
480-
$app->system->unlink('/tmp/id_rsa.pub');
471+
$app->uses('functions');
472+
$app->functions->generate_ssh_key($id, $username);
473+
481474
$this->app->log("ssh-rsa keypair generated for ".$username, LOGLEVEL_DEBUG);
482475
};
483476

0 commit comments

Comments
 (0)