Skip to content

Commit 00a055c

Browse files
author
laking
committed
Update on certificte ssh-rsa authentication - shell plugins.
1 parent 28a1b86 commit 00a055c

File tree

2 files changed

+113
-50
lines changed

2 files changed

+113
-50
lines changed

server/plugins-available/shelluser_base_plugin.inc.php

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -194,59 +194,60 @@ function delete($event_name,$data) {
194194

195195
}
196196

197-
function _setup_ssh_rsa() {
198-
//global $app, $conf;
199-
//$app->uses('system');
200-
// Okay, here we have a question, .. how to determine the client id
201-
//$var = "Var:".intval($this->app->system->getuid($this->data['new']['puser']));
202-
//exec("echo $var >> /tmp/debug");
203-
/*
204-
// ssh-rsa authentication variables
205-
$sshrsa = escapeshellcmd($this->data['new']['ssh_rsa']);
206-
$usrdir = escapeshellcmd($this->data['new']['dir']);
207-
$sshdir = escapeshellcmd($this->data['new']['dir']).'/.ssh';
208-
$sshkeys= escapeshellcmd($this->data['new']['dir']).'/.ssh/authorized_keys';
209-
210-
// determine the client id
211-
$id = $this->data['new']['sys_groupid'];
212-
if ($id>0) $id = $id -1;
213-
214-
$user = $app->db->queryOneRecord("SELECT * FROM sys_user WHERE client_id = ".$id);
215-
$userkey = $user['ssh_rsa'];
216-
$username= $user['username'];
217-
218-
// If this user has no key yet, generate a pair
219-
if ($userkey == '')
220-
{
221-
//Generate ssh-rsa-keys
222-
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
223-
$app->db->query("UPDATE client SET created_at = ".time().", id_rsa = '".file_get_contents('/tmp/id_rsa')."', ssh_rsa = '".file_get_contents('/tmp/id_rsa.pub')."' WHERE client_id = ".$this->id;
224-
exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
225-
226-
$this->app->log("ssh-rsa keypair generated for ".$username,LOGLEVEL_DEBUG);
227-
228-
};
229-
230-
if (!file_exists($sshkeys))
231-
{
232-
// add root's key
233-
exec("mkdir '$sshdir'");
234-
exec("cat /root/.ssh/authorized_keys > '$sshkeys'");
235-
exec("echo '' >> '$sshkeys'");
236-
237-
// add the user's key
238-
exec("echo '$userkey' >> '$sshkeys'");
239-
exec("echo '' >> '$sshkeys'");
240-
}
197+
private function _setup_ssh_rsa() {
198+
$this->app->log("ssh-rsa setup shelluser_base",LOGLEVEL_DEBUG);
199+
// Get the client ID, username, and the key
200+
$domain_data = $this->app->dbmaster->queryOneRecord('SELECT sys_groupid FROM web_domain WHERE web_domain.domain_id = '.intval($this->data['new']['parent_domain_id']));
201+
$sys_group_data = $this->app->dbmaster->queryOneRecord('SELECT * FROM sys_group WHERE sys_group.groupid = '.intval($domain_data['sys_groupid']));
202+
$id = intval($sys_group_data['client_id']);
203+
$username= $sys_group_data['name'];
204+
$client_data = $this->app->dbmaster->queryOneRecord('SELECT * FROM client WHERE client.client_id = '.$id);
205+
$userkey = $client_data['ssh_rsa'];
206+
unset($domain_data);
207+
unset($client_data);
208+
209+
// ssh-rsa authentication variables
210+
$sshrsa = escapeshellcmd($this->data['new']['ssh_rsa']);
211+
$usrdir = escapeshellcmd($this->data['new']['dir']);
212+
$sshdir = $usrdir.'/.ssh';
213+
$sshkeys= $usrdir.'/.ssh/authorized_keys';
214+
215+
// If this user has no key yet, generate a pair
216+
if ($userkey == '' && $id>0)
217+
{
218+
//Generate ssh-rsa-keys
219+
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
220+
// save keypair in client table
221+
$this->app->dbmaster->query("UPDATE client SET created_at = ".time().", id_rsa = '".file_get_contents('/tmp/id_rsa')."', ssh_rsa = '".file_get_contents('/tmp/id_rsa.pub')."' WHERE client_id = ".$id);
222+
// and use the public key that has been generated
223+
$userkey = file_get_contents('/tmp/id_rsa.pub')
224+
;
225+
exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
226+
$this->app->log("ssh-rsa keypair generated for ".$username,LOGLEVEL_DEBUG);
227+
};
228+
229+
if (!file_exists($sshkeys))
230+
{
231+
// add root's key
232+
exec("mkdir '$sshdir'");
233+
exec("cat /root/.ssh/authorized_keys > '$sshkeys'");
234+
exec("echo '' >> '$sshkeys'");
235+
236+
// add the user's key
237+
exec("echo '$userkey' >> '$sshkeys'");
238+
exec("echo '' >> '$sshkeys'");
239+
$this->app->log("ssh-rsa authorisation keyfile created in ".$sshkeys,LOGLEVEL_DEBUG);
240+
}
241+
if ($sshrsa!=''){
241242
// add the custom key
242243
exec("echo '$sshrsa' >> '$sshkeys'");
243244
exec("echo '' >> '$sshkeys'");
244-
245-
// set proper file permissions
246-
exec("chown -R ".escapeshellcmd($this->data['new']['puser']).":".escapeshellcmd($this->data['new']['pgroup'])." ".$usrdir);
247-
exec("chmod 600 '$sshkeys'");
248-
*/
249-
$this->app->log("ssh-rsa key added to ".$sshkeys,LOGLEVEL_DEBUG);
245+
$this->app->log("ssh-rsa key updated in ".$sshkeys,LOGLEVEL_DEBUG);
246+
}
247+
// set proper file permissions
248+
exec("chown -R ".escapeshellcmd($this->data['new']['puser']).":".escapeshellcmd($this->data['new']['pgroup'])." ".$usrdir);
249+
exec("chmod 600 '$sshkeys'");
250+
250251
}
251252

252253

server/plugins-available/shelluser_jailkit_plugin.inc.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ function insert($event_name,$data) {
9191

9292
$this->_add_jailkit_user();
9393

94+
// call the ssh-rsa update function
95+
$this->_setup_ssh_rsa();
96+
9497
$command .= 'usermod -U '.escapeshellcmd($data['new']['username']);
9598
exec($command);
9699

@@ -129,6 +132,9 @@ function update($event_name,$data) {
129132
$this->_setup_jailkit_chroot();
130133
$this->_add_jailkit_user();
131134

135+
// call the ssh-rsa update function
136+
$this->_setup_ssh_rsa();
137+
132138
$this->_update_website_security_level();
133139
}
134140

@@ -272,6 +278,7 @@ function _add_jailkit_user()
272278
chgrp(escapeshellcmd($this->data['new']['dir'].$jailkit_chroot_puserhome), $this->data['new']['pgroup']);
273279

274280
$this->app->log("Added created jailkit parent user home in : ".$this->data['new']['dir'].$jailkit_chroot_puserhome,LOGLEVEL_DEBUG);
281+
275282
/*
276283
// ssh-rsa authentication variables
277284
$sshrsa = escapeshellcmd($this->data['new']['ssh_rsa']);
@@ -356,6 +363,61 @@ private function _exec($command) {
356363
exec($command);
357364
}
358365

366+
private function _setup_ssh_rsa() {
367+
$this->app->log("ssh-rsa setup shelluser_jailkit",LOGLEVEL_DEBUG);
368+
// Get the client ID, username, and the key
369+
$domain_data = $this->app->dbmaster->queryOneRecord('SELECT sys_groupid FROM web_domain WHERE web_domain.domain_id = '.intval($this->data['new']['parent_domain_id']));
370+
$sys_group_data = $this->app->dbmaster->queryOneRecord('SELECT * FROM sys_group WHERE sys_group.groupid = '.intval($domain_data['sys_groupid']));
371+
$id = intval($sys_group_data['client_id']);
372+
$username= $sys_group_data['name'];
373+
$client_data = $this->app->dbmaster->queryOneRecord('SELECT * FROM client WHERE client.client_id = '.$id);
374+
$userkey = $client_data['ssh_rsa'];
375+
unset($domain_data);
376+
unset($client_data);
377+
378+
// ssh-rsa authentication variables
379+
$sshrsa = escapeshellcmd($this->data['new']['ssh_rsa']);
380+
$usrdir = escapeshellcmd($this->data['new']['dir']).'/'.$this->_get_home_dir($this->data['new']['username']);
381+
$sshdir = $usrdir.'/.ssh';
382+
$sshkeys= $usrdir.'/.ssh/authorized_keys';
383+
384+
// If this user has no key yet, generate a pair
385+
if ($userkey == '' && $id>0)
386+
{
387+
//Generate ssh-rsa-keys
388+
exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
389+
// save keypair in client table
390+
$this->app->dbmaster->query("UPDATE client SET created_at = ".time().", id_rsa = '".file_get_contents('/tmp/id_rsa')."', ssh_rsa = '".file_get_contents('/tmp/id_rsa.pub')."' WHERE client_id = ".$id);
391+
// and use the public key that has been generated
392+
$userkey = file_get_contents('/tmp/id_rsa.pub')
393+
;
394+
exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
395+
$this->app->log("ssh-rsa keypair generated for ".$username,LOGLEVEL_DEBUG);
396+
};
397+
398+
if (!file_exists($sshkeys))
399+
{
400+
// add root's key
401+
exec("mkdir '$sshdir'");
402+
exec("cat /root/.ssh/authorized_keys > '$sshkeys'");
403+
exec("echo '' >> '$sshkeys'");
404+
405+
// add the user's key
406+
exec("echo '$userkey' >> '$sshkeys'");
407+
exec("echo '' >> '$sshkeys'");
408+
$this->app->log("ssh-rsa authorisation keyfile created in ".$sshkeys,LOGLEVEL_DEBUG);
409+
}
410+
if ($sshrsa!=''){
411+
// add the custom key
412+
exec("echo '$sshrsa' >> '$sshkeys'");
413+
exec("echo '' >> '$sshkeys'");
414+
$this->app->log("ssh-rsa key updated in ".$sshkeys,LOGLEVEL_DEBUG);
415+
}
416+
// set proper file permissions
417+
exec("chown -R ".escapeshellcmd($this->data['new']['puser']).":".escapeshellcmd($this->data['new']['pgroup'])." ".$usrdir);
418+
exec("chmod 600 '$sshkeys'");
419+
420+
}
359421
} // end class
360422

361423
?>

0 commit comments

Comments
 (0)