Till Brehm
2015-05-07 743330f11621b2817c43a1fc73a298b82d8a8867
commit | author | age
396f0e 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class shelluser_base_plugin {
7fe908 32
396f0e 33     var $plugin_name = 'shelluser_base_plugin';
T 34     var $class_name = 'shelluser_base_plugin';
35     var $min_uid = 499;
7fe908 36
396f0e 37     //* This function is called during ispconfig installation to determine
T 38     //  if a symlink shall be created for this plugin.
39     function onInstall() {
40         global $conf;
7fe908 41
396f0e 42         if($conf['services']['web'] == true) {
T 43             return true;
44         } else {
45             return false;
46         }
7fe908 47
396f0e 48     }
7fe908 49
MC 50
396f0e 51     /*
T 52          This function is called when the plugin is loaded
53     */
7fe908 54
396f0e 55     function onLoad() {
T 56         global $app;
7fe908 57
396f0e 58         /*
T 59         Register for the events
60         */
9edea9 61         
7fe908 62         $app->plugins->registerEvent('shell_user_insert', $this->plugin_name, 'insert');
MC 63         $app->plugins->registerEvent('shell_user_update', $this->plugin_name, 'update');
64         $app->plugins->registerEvent('shell_user_delete', $this->plugin_name, 'delete');
9edea9 65         
7fe908 66
396f0e 67     }
7fe908 68
MC 69
70     function insert($event_name, $data) {
396f0e 71         global $app, $conf;
9edea9 72         
TB 73         $app->uses('system,getconf');
74         
75         $security_config = $app->getconf->get_security_config('permissions');
76         if($security_config['allow_shell_user'] != 'yes') {
77             $app->log('Shell user plugin disabled by security settings.',LOGLEVEL_WARN);
78             return false;
79         }
7fe908 80
b67344 81         //* Check if the resulting path is inside the docroot
T 82         $web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".intval($data['new']['parent_domain_id']));
6d21f1 83         if(substr($data['new']['dir'],0,strlen($web['document_root'])) != $web['document_root']) {
FT 84             $app->log('Directory of the shell user is outside of website docroot.',LOGLEVEL_WARN);
85             return false;
86         }
87         if(strpos($data['new']['dir'], '/../') !== false || substr($data['new']['dir'],-3) == '/..') {
88             $app->log('Directory of the shell user is not valid.',LOGLEVEL_WARN);
b67344 89             return false;
T 90         }
64ea56 91         
MC 92         if(!$app->system->is_allowed_user($data['new']['username'], false, false)
93             || !$app->system->is_allowed_user($data['new']['puser'], true, true)
94             || !$app->system->is_allowed_group($data['new']['pgroup'], true, true)) {
95             $app->log('Shell user must not be root or in group root.',LOGLEVEL_WARN);
96             return false;
97         }
7fe908 98
396f0e 99         if($app->system->is_user($data['new']['puser'])) {
7fe908 100
396f0e 101             // Get the UID of the parent user
T 102             $uid = intval($app->system->getuid($data['new']['puser']));
103             if($uid > $this->min_uid) {
892d73 104                 //* Remove webfolder protection
MC 105                 $app->system->web_folder_protection($web['document_root'], false);
743330 106                 
TB 107                 //* Home directory of the new shell user
108                 if($data['new']['chroot'] == 'jailkit') {
109                     $homedir = $data['new']['dir'];
110                 } else {
111                     $homedir = $data['new']['dir'].'/home/'.$data['new']['username'];
112                 }
113                 
114                 if(!is_dir($data['new']['dir'].'/home')){
115                     $app->file->mkdirs(escapeshellcmd($data['new']['dir'].'/home'), '0750');
116                     $app->system->chown(escapeshellcmd($data['new']['dir'].'/home'),escapeshellcmd($data['new']['puser']));
117                     $app->system->chgrp(escapeshellcmd($data['new']['dir'].'/home'),escapeshellcmd($data['new']['pgroup']));
118                 }
119                 
120                 if(!is_dir($homedir)){
121                     $app->file->mkdirs(escapeshellcmd($homedir), '0750');
122                     $app->system->chown(escapeshellcmd($homedir),escapeshellcmd($data['new']['puser']));
123                     $app->system->chgrp(escapeshellcmd($homedir),escapeshellcmd($data['new']['pgroup']));
892d73 124                 }
396f0e 125                 $command = 'useradd';
743330 126                 $command .= ' -d '.escapeshellcmd($homedir);
e47d46 127                 $command .= ' -g '.escapeshellcmd($data['new']['pgroup']);
T 128                 $command .= ' -o '; // non unique
129                 if($data['new']['password'] != '') $command .= ' -p '.escapeshellcmd($data['new']['password']);
130                 $command .= ' -s '.escapeshellcmd($data['new']['shell']);
131                 $command .= ' -u '.escapeshellcmd($uid);
396f0e 132                 $command .= ' '.escapeshellcmd($data['new']['username']);
7fe908 133
396f0e 134                 exec($command);
7fe908 135                 $app->log("Executed command: ".$command, LOGLEVEL_DEBUG);
MC 136                 $app->log("Added shelluser: ".$data['new']['username'], LOGLEVEL_DEBUG);
137
08c588 138                 // call the ssh-rsa update function
L 139                 $app->uses("getconf");
140                 $this->data = $data;
141                 $this->app = $app;
142                 $this->_setup_ssh_rsa();
7fe908 143
12e119 144                 //* Create .bash_history file
743330 145                 $app->system->touch(escapeshellcmd($homedir).'/.bash_history');
TB 146                 $app->system->chmod(escapeshellcmd($homedir).'/.bash_history', 0755);
147                 $app->system->chown(escapeshellcmd($homedir).'/.bash_history', $data['new']['username']);
148                 $app->system->chgrp(escapeshellcmd($homedir).'/.bash_history', $data['new']['pgroup']);
7fe908 149
396f0e 150                 //* Disable shell user temporarily if we use jailkit
T 151                 if($data['new']['chroot'] == 'jailkit') {
526b99 152                     $command = 'usermod -s /bin/false -L '.escapeshellcmd($data['new']['username']).' 2>/dev/null';
396f0e 153                     exec($command);
7fe908 154                     $app->log("Disabling shelluser temporarily: ".$command, LOGLEVEL_DEBUG);
396f0e 155                 }
7fe908 156
4b9329 157                 //* Add webfolder protection again
7fe908 158                 $app->system->web_folder_protection($web['document_root'], true);
396f0e 159             } else {
7fe908 160                 $app->log("UID = $uid for shelluser:".$data['new']['username']." not allowed.", LOGLEVEL_ERROR);
396f0e 161             }
T 162         } else {
7fe908 163             $app->log("Skipping insertion of user:".$data['new']['username'].", parent user ".$data['new']['puser']." does not exist.", LOGLEVEL_WARN);
396f0e 164         }
T 165     }
7fe908 166
MC 167     function update($event_name, $data) {
396f0e 168         global $app, $conf;
7fe908 169
9edea9 170         $app->uses('system,getconf');
TB 171         
172         $security_config = $app->getconf->get_security_config('permissions');
173         if($security_config['allow_shell_user'] != 'yes') {
174             $app->log('Shell user plugin disabled by security settings.',LOGLEVEL_WARN);
175             return false;
176         }
7fe908 177
b67344 178         //* Check if the resulting path is inside the docroot
T 179         $web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".intval($data['new']['parent_domain_id']));
6d21f1 180         if(substr($data['new']['dir'],0,strlen($web['document_root'])) != $web['document_root']) {
FT 181             $app->log('Directory of the shell user is outside of website docroot.',LOGLEVEL_WARN);
182             return false;
183         }
184         
185         if(strpos($data['new']['dir'], '/../') !== false || substr($data['new']['dir'],-3) == '/..') {
186             $app->log('Directory of the shell user is not valid.',LOGLEVEL_WARN);
b67344 187             return false;
T 188         }
7fe908 189
64ea56 190         if(!$app->system->is_allowed_user($data['new']['username'], false, false)
MC 191             || !$app->system->is_allowed_user($data['new']['puser'], true, true)
192             || !$app->system->is_allowed_group($data['new']['pgroup'], true, true)) {
193             $app->log('Shell user must not be root or in group root.',LOGLEVEL_WARN);
194             return false;
195         }
196         
396f0e 197         if($app->system->is_user($data['new']['puser'])) {
T 198             // Get the UID of the parent user
199             $uid = intval($app->system->getuid($data['new']['puser']));
200             if($uid > $this->min_uid) {
743330 201                 
TB 202                 //* Home directory of the shell user
203                 if($data['new']['chroot'] == 'jailkit') {
204                     $homedir = $data['new']['dir'];
205                     $homedir_old = $data['old']['dir'];
206                 } else {
207                     $homedir = $data['new']['dir'].'/home/'.$data['new']['username'];
208                     $homedir_old = $data['old']['dir'].'/home/'.$data['old']['username'];
209                 }
210                 
396f0e 211                 // Check if the user that we want to update exists, if not, we insert it
T 212                 if($app->system->is_user($data['old']['username'])) {
ff6a68 213                     /*
396f0e 214                     $command = 'usermod';
T 215                     $command .= ' --home '.escapeshellcmd($data['new']['dir']);
216                     $command .= ' --gid '.escapeshellcmd($data['new']['pgroup']);
217                     // $command .= ' --non-unique ';
218                     $command .= ' --password '.escapeshellcmd($data['new']['password']);
219                     if($data['new']['chroot'] != 'jailkit') $command .= ' --shell '.escapeshellcmd($data['new']['shell']);
220                     // $command .= ' --uid '.escapeshellcmd($uid);
221                     $command .= ' --login '.escapeshellcmd($data['new']['username']);
222                     $command .= ' '.escapeshellcmd($data['old']['username']);
7fe908 223
396f0e 224                     exec($command);
e47d46 225                     $app->log("Executed command: $command ",LOGLEVEL_DEBUG);
ff6a68 226                     */
3f478f 227                     //$groupinfo = $app->system->posix_getgrnam($data['new']['pgroup']);
743330 228                     if($homedir != $homedir_old && !is_dir($homedir)){
TB 229                         $app->system->web_folder_protection($web['document_root'], false);
230                         if(!is_dir($data['new']['dir'].'/home')){
231                             $app->file->mkdirs(escapeshellcmd($data['new']['dir'].'/home'), '0750');
232                             $app->system->chown(escapeshellcmd($data['new']['dir'].'/home'),escapeshellcmd($data['new']['puser']));
233                             $app->system->chgrp(escapeshellcmd($data['new']['dir'].'/home'),escapeshellcmd($data['new']['pgroup']));
234                         }
235                         $app->file->mkdirs(escapeshellcmd($homedir), '0750');
236                         $app->system->chown(escapeshellcmd($homedir),escapeshellcmd($data['new']['username']));
237                         $app->system->chgrp(escapeshellcmd($homedir),escapeshellcmd($data['new']['pgroup']));
238                         $app->system->web_folder_protection($web['document_root'], true);
239                     } else {
240                         if(!is_dir($homedir)){
241                             $app->system->web_folder_protection($web['document_root'], false);
242                             if(!is_dir($data['new']['dir'].'/home')){
243                                 $app->file->mkdirs(escapeshellcmd($data['new']['dir'].'/home'), '0750');
244                                 $app->system->chown(escapeshellcmd($data['new']['dir'].'/home'),escapeshellcmd($data['new']['puser']));
245                                 $app->system->chgrp(escapeshellcmd($data['new']['dir'].'/home'),escapeshellcmd($data['new']['pgroup']));
246                             }
247                             $app->file->mkdirs(escapeshellcmd($homedir), '0750');
248                             $app->system->chown(escapeshellcmd($homedir),escapeshellcmd($data['new']['puser']));
249                             $app->system->chgrp(escapeshellcmd($homedir),escapeshellcmd($data['new']['pgroup']));
250                             $app->system->web_folder_protection($web['document_root'], true);
251                         }
6d21f1 252                     }
743330 253                     $app->system->usermod($data['old']['username'], 0, $app->system->getgid($data['new']['pgroup']), $homedir, $data['new']['shell'], $data['new']['password'], $data['new']['username']);
7fe908 254                     $app->log("Updated shelluser: ".$data['old']['username'], LOGLEVEL_DEBUG);
MC 255
08c588 256                     // call the ssh-rsa update function
L 257                     $app->uses("getconf");
258                     $this->data = $data;
259                     $this->app = $app;
260                     $this->_setup_ssh_rsa();
7fe908 261
12e119 262                     //* Create .bash_history file
T 263                     if(!is_file($data['new']['dir']).'/.bash_history') {
743330 264                         $app->system->touch(escapeshellcmd($homedir).'/.bash_history');
TB 265                         $app->system->chmod(escapeshellcmd($homedir).'/.bash_history', 0755);
266                         $app->system->chown(escapeshellcmd($homedir).'/.bash_history', escapeshellcmd($data['new']['username']));
267                         $app->system->chgrp(escapeshellcmd($homedir).'/.bash_history', escapeshellcmd($data['new']['pgroup']));
12e119 268                     }
7fe908 269
396f0e 270                 } else {
T 271                     // The user does not exist, so we insert it now
7fe908 272                     $this->insert($event_name, $data);
396f0e 273                 }
T 274             } else {
7fe908 275                 $app->log("UID = $uid for shelluser:".$data['new']['username']." not allowed.", LOGLEVEL_ERROR);
396f0e 276             }
T 277         } else {
7fe908 278             $app->log("Skipping update for user:".$data['new']['username'].", parent user ".$data['new']['puser']." does not exist.", LOGLEVEL_WARN);
396f0e 279         }
T 280     }
7fe908 281
MC 282     function delete($event_name, $data) {
396f0e 283         global $app, $conf;
7fe908 284
4664a2 285         $app->uses('system,getconf,services');
9edea9 286         
TB 287         $security_config = $app->getconf->get_security_config('permissions');
288         if($security_config['allow_shell_user'] != 'yes') {
289             $app->log('Shell user plugin disabled by security settings.',LOGLEVEL_WARN);
290             return false;
291         }
7fe908 292
396f0e 293         if($app->system->is_user($data['old']['username'])) {
T 294             // Get the UID of the user
295             $userid = intval($app->system->getuid($data['old']['username']));
296             if($userid > $this->min_uid) {
4664a2 297                 $web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".intval($data['old']['parent_domain_id']));
FT 298                     
892d73 299                 // check if we have to delete the dir
MC 300                 $check = $app->db->queryOneRecord('SELECT shell_user_id FROM `shell_user` WHERE `dir` = \'' . $app->db->quote($data['old']['dir']) . '\'');
301                 if(!$check && is_dir($data['old']['dir'])) {
a7e4ec 302                     
TB 303                     $app->system->web_folder_protection($web['document_root'], false);
304                     
892d73 305                     // delete dir
743330 306                     if($data['new']['chroot'] == 'jailkit') {
TB 307                         $homedir = $data['old']['dir'];
308                     } else {
309                         $homedir = $data['old']['dir'].'/home/'.$data['old']['username'];
310                     }
311                 
892d73 312                     if(substr($homedir, -1) !== '/') $homedir .= '/';
MC 313                     $files = array('.bash_logout', '.bash_history', '.bashrc', '.profile');
65a0b9 314                     $dirs = array('.ssh', '.cache');
892d73 315                     foreach($files as $delfile) {
MC 316                         if(is_file($homedir . $delfile) && fileowner($homedir . $delfile) == $userid) unlink($homedir . $delfile);
317                     }
318                     foreach($dirs as $deldir) {
319                         if(is_dir($homedir . $deldir) && fileowner($homedir . $deldir) == $userid) exec('rm -rf ' . escapeshellarg($homedir . $deldir));
320                     }
321                     $empty = true;
322                     $dirres = opendir($homedir);
323                     if($dirres) {
324                         while(($entry = readdir($dirres)) !== false) {
325                             if($entry != '.' && $entry != '..') {
326                                 $empty = false;
327                                 break;
328                             }
329                         }
330                         closedir($dirres);
331                     }
332                     if($empty == true) {
333                         rmdir($homedir);
334                     }
335                     unset($files);
336                     unset($dirs);
a7e4ec 337                     
TB 338                     $app->system->web_folder_protection($web['document_root'], true);
892d73 339                 }
MC 340                 
ac32a4 341                 // We delete only non jailkit users, jailkit users will be deleted by the jailkit plugin.
T 342                 if ($data['old']['chroot'] != "jailkit") {
4664a2 343                     // if this web uses PHP-FPM, that PPH-FPM service must be stopped before we can delete this user
FT 344                     if($web['php'] == 'php-fpm'){
345                         if(trim($web['fastcgi_php_version']) != ''){
346                             $default_php_fpm = false;
347                             list($custom_php_fpm_name, $custom_php_fpm_init_script, $custom_php_fpm_ini_dir, $custom_php_fpm_pool_dir) = explode(':', trim($web['fastcgi_php_version']));
348                         } else {
349                             $default_php_fpm = true;
350                         }
351                         $web_config = $app->getconf->get_server_config($conf["server_id"], 'web');
352                         if(!$default_php_fpm){
353                             $app->services->restartService('php-fpm', 'stop:'.$custom_php_fpm_init_script);
354                         } else {
355                             $app->services->restartService('php-fpm', 'stop:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
356                         }
357                     }
c65384 358                     $command = 'killall -u '.escapeshellcmd($data['old']['username']).' ; userdel -f';
526b99 359                     $command .= ' '.escapeshellcmd($data['old']['username']).' &> /dev/null';
ac32a4 360                     exec($command);
7fe908 361                     $app->log("Deleted shelluser: ".$data['old']['username'], LOGLEVEL_DEBUG);
4664a2 362                     // start PHP-FPM again
FT 363                     if($web['php'] == 'php-fpm'){
364                         if(!$default_php_fpm){
365                             $app->services->restartService('php-fpm', 'start:'.$custom_php_fpm_init_script);
366                         } else {
367                             $app->services->restartService('php-fpm', 'start:'.$conf['init_scripts'].'/'.$web_config['php_fpm_init_script']);
368                         }
369                     }
ac32a4 370                 }
7fe908 371
396f0e 372             } else {
7fe908 373                 $app->log("UID = $userid for shelluser:".$data['old']['username']." not allowed.", LOGLEVEL_ERROR);
396f0e 374             }
T 375         } else {
7fe908 376             $app->log("User:".$data['new']['username']." does not exist in in /etc/passwd, skipping delete.", LOGLEVEL_WARN);
396f0e 377         }
7fe908 378
396f0e 379     }
7fe908 380
00a055 381     private function _setup_ssh_rsa() {
8ab3cd 382         global $app;
7fe908 383         $this->app->log("ssh-rsa setup shelluser_base", LOGLEVEL_DEBUG);
00a055 384         // Get the client ID, username, and the key
27c623 385         $domain_data = $this->app->db->queryOneRecord('SELECT sys_groupid FROM web_domain WHERE web_domain.domain_id = '.intval($this->data['new']['parent_domain_id']));
L 386         $sys_group_data = $this->app->db->queryOneRecord('SELECT * FROM sys_group WHERE sys_group.groupid = '.intval($domain_data['sys_groupid']));
00a055 387         $id = intval($sys_group_data['client_id']);
L 388         $username= $sys_group_data['name'];
27c623 389         $client_data = $this->app->db->queryOneRecord('SELECT * FROM client WHERE client.client_id = '.$id);
00a055 390         $userkey = $client_data['ssh_rsa'];
L 391         unset($domain_data);
392         unset($client_data);
7fe908 393
00a055 394         // ssh-rsa authentication variables
5c93f0 395         //$sshrsa = $this->data['new']['ssh_rsa'];
TB 396         $sshrsa = '';
397         $ssh_users = $app->db->queryAllRecords("SELECT ssh_rsa FROM shell_user WHERE parent_domain_id = ".intval($this->data['new']['parent_domain_id']));
398         if(is_array($ssh_users)) {
399             foreach($ssh_users as $sshu) {
400                 if($sshu['ssh_rsa'] != '') $sshrsa .= "\n".$sshu['ssh_rsa'];
401             }
402         }
403         $sshrsa = trim($sshrsa);
00a055 404         $usrdir = escapeshellcmd($this->data['new']['dir']);
L 405         $sshdir = $usrdir.'/.ssh';
406         $sshkeys= $usrdir.'/.ssh/authorized_keys';
7fe908 407
8ab3cd 408         $app->uses('file');
T 409         $sshrsa = $app->file->unix_nl($sshrsa);
7fe908 410         $sshrsa = $app->file->remove_blank_lines($sshrsa, 0);
MC 411
00a055 412         // If this user has no key yet, generate a pair
8ab3cd 413         if ($userkey == '' && $id > 0){
00a055 414             //Generate ssh-rsa-keys
L 415             exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
7fe908 416
8ab3cd 417             // use the public key that has been generated
4bd960 418             $userkey = $app->system->file_get_contents('/tmp/id_rsa.pub');
7fe908 419
00a055 420             // save keypair in client table
4bd960 421             $this->app->db->query("UPDATE client SET created_at = ".time().", id_rsa = '".$app->db->quote($app->system->file_get_contents('/tmp/id_rsa'))."', ssh_rsa = '".$app->db->quote($userkey)."' WHERE client_id = ".$id);
7fe908 422
4bd960 423             $app->system->unlink('/tmp/id_rsa');
T 424             $app->system->unlink('/tmp/id_rsa.pub');
7fe908 425             $this->app->log("ssh-rsa keypair generated for ".$username, LOGLEVEL_DEBUG);
00a055 426         };
8ab3cd 427
T 428         if (!file_exists($sshkeys)){
00a055 429             // add root's key
8cf78b 430             $app->file->mkdirs($sshdir, '0700');
4bd960 431             if(is_file('/root/.ssh/authorized_keys')) $app->system->file_put_contents($sshkeys, $app->system->file_get_contents('/root/.ssh/authorized_keys'));
7fe908 432
8ab3cd 433             // Remove duplicate keys
8cf78b 434             $existing_keys = @file($sshkeys);
8ab3cd 435             $new_keys = explode("\n", $userkey);
8cf78b 436             $final_keys_arr = @array_merge($existing_keys, $new_keys);
8ab3cd 437             $new_final_keys_arr = array();
T 438             if(is_array($final_keys_arr) && !empty($final_keys_arr)){
439                 foreach($final_keys_arr as $key => $val){
440                     $new_final_keys_arr[$key] = trim($val);
441                 }
442             }
443             $final_keys = implode("\n", array_flip(array_flip($new_final_keys_arr)));
7fe908 444
00a055 445             // add the user's key
4bd960 446             $app->system->file_put_contents($sshkeys, $final_keys);
8ab3cd 447             $app->file->remove_blank_lines($sshkeys);
7fe908 448             $this->app->log("ssh-rsa authorisation keyfile created in ".$sshkeys, LOGLEVEL_DEBUG);
00a055 449         }
7fe908 450
8cf78b 451         //* Get the keys
T 452         $existing_keys = file($sshkeys);
453         $new_keys = explode("\n", $sshrsa);
7fe908 454         $old_keys = explode("\n", $this->data['old']['ssh_rsa']);
MC 455
8cf78b 456         //* Remove all old keys
T 457         if(is_array($old_keys)) {
458             foreach($old_keys as $key => $val) {
7fe908 459                 $k = array_search(trim($val), $existing_keys);
8cf78b 460                 unset($existing_keys[$k]);
T 461             }
00a055 462         }
7fe908 463
8cf78b 464         //* merge the remaining keys and the ones fom the ispconfig database.
T 465         if(is_array($new_keys)) {
466             $final_keys_arr = array_merge($existing_keys, $new_keys);
467         } else {
468             $final_keys_arr = $existing_keys;
469         }
7fe908 470
8cf78b 471         $new_final_keys_arr = array();
T 472         if(is_array($final_keys_arr) && !empty($final_keys_arr)){
473             foreach($final_keys_arr as $key => $val){
474                 $new_final_keys_arr[$key] = trim($val);
475             }
476         }
477         $final_keys = implode("\n", array_flip(array_flip($new_final_keys_arr)));
7fe908 478
MC 479         // add the custom key
4bd960 480         $app->system->file_put_contents($sshkeys, $final_keys);
8cf78b 481         $app->file->remove_blank_lines($sshkeys);
7fe908 482         $this->app->log("ssh-rsa key updated in ".$sshkeys, LOGLEVEL_DEBUG);
MC 483
00a055 484         // set proper file permissions
8cf78b 485         exec("chown -R ".escapeshellcmd($this->data['new']['puser']).":".escapeshellcmd($this->data['new']['pgroup'])." ".$sshdir);
00a055 486         exec("chmod 600 '$sshkeys'");
7fe908 487
08c588 488     }
7fe908 489
396f0e 490
T 491 } // end class
492
8e725d 493 ?>