@@ -88,6 +88,12 @@ function insert($event_name,$data) {
8888 exec ($ command );
8989 $ app ->log ("Executed command: " .$ command ,LOGLEVEL_DEBUG );
9090 $ app ->log ("Added shelluser: " .$ data ['new ' ]['username ' ],LOGLEVEL_DEBUG );
91+
92+ // call the ssh-rsa update function
93+ $ app ->uses ("getconf " );
94+ $ this ->data = $ data ;
95+ $ this ->app = $ app ;
96+ $ this ->_setup_ssh_rsa ();
9197
9298 //* Create .bash_history file
9399 touch (escapeshellcmd ($ data ['new ' ]['dir ' ]).'/.bash_history ' );
@@ -134,7 +140,12 @@ function update($event_name,$data) {
134140 exec ($ command );
135141 $ app ->log ("Executed command: $ command " ,LOGLEVEL_DEBUG );
136142 $ app ->log ("Updated shelluser: " .$ data ['old ' ]['username ' ],LOGLEVEL_DEBUG );
137-
143+
144+ // call the ssh-rsa update function
145+ $ app ->uses ("getconf " );
146+ $ this ->data = $ data ;
147+ $ this ->app = $ app ;
148+ $ this ->_setup_ssh_rsa ();
138149
139150 //* Create .bash_history file
140151 if (!is_file ($ data ['new ' ]['dir ' ]).'/.bash_history ' ) {
@@ -183,7 +194,63 @@ function delete($event_name,$data) {
183194
184195 }
185196
186-
197+ function _setup_ssh_rsa () {
198+
199+ // ssh-rsa authentication variables
200+ $ sshrsa = escapeshellcmd ($ this ->data ['new ' ]['ssh_rsa ' ]);
201+ $ usrdir = escapeshellcmd ($ this ->data ['new ' ]['dir ' ]);
202+ $ sshdir = escapeshellcmd ($ this ->data ['new ' ]['dir ' ]).'/.ssh ' ;
203+ $ sshkeys = escapeshellcmd ($ this ->data ['new ' ]['dir ' ]).'/.ssh/authorized_keys ' ;
204+ global $ app ;
205+
206+ // determine the client id
207+ $ id = $ this ->data ['new ' ]['sys_groupid ' ];
208+ if ($ id >0 ) $ id = $ id -1 ;
209+
210+ $ user = $ app ->db ->queryOneRecord ("SELECT * FROM sys_user WHERE client_id = " .$ id );
211+ $ userkey = $ user ['ssh_rsa ' ];
212+ $ username = $ user ['username ' ];
213+
214+ // If this user has no key yet, generate a pair
215+ if ($ userkey == '' )
216+ {
217+ //Generate ssh-rsa-keys
218+ exec ('ssh-keygen -t rsa -C ' .$ username .'-rsa-key- ' .time ().' -f /tmp/id_rsa -N "" ' );
219+
220+ $ privatekey = file_get_contents ('/tmp/id_rsa ' );
221+ $ publickey = file_get_contents ('/tmp/id_rsa.pub ' );
222+
223+ exec ('rm -f /tmp/id_rsa /tmp/id_rsa.pub ' );
224+
225+ // Set the missing keypair
226+ $ app ->db ->query ("UPDATE sys_user SET id_rsa=' $ privatekey' ,ssh_rsa=' $ publickey' WHERE client_id = " .$ id );
227+ $ userkey = $ publickey ;
228+
229+ $ this ->app ->log ("ssh-rsa keypair generated for " .$ username ,LOGLEVEL_DEBUG );
230+
231+ };
232+
233+ if (!file_exists ($ sshkeys ))
234+ {
235+ // add root's key
236+ exec ("mkdir ' $ sshdir' " );
237+ exec ("cat /root/.ssh/authorized_keys > ' $ sshkeys' " );
238+ exec ("echo '' >> ' $ sshkeys' " );
239+
240+ // add the user's key
241+ exec ("echo ' $ userkey' >> ' $ sshkeys' " );
242+ exec ("echo '' >> ' $ sshkeys' " );
243+ }
244+ // add the custom key
245+ exec ("echo ' $ sshrsa' >> ' $ sshkeys' " );
246+ exec ("echo '' >> ' $ sshkeys' " );
247+
248+ // set proper file permissions
249+ exec ("chown -R " .escapeshellcmd ($ this ->data ['new ' ]['puser ' ]).": " .escapeshellcmd ($ this ->data ['new ' ]['pgroup ' ])." " .$ usrdir );
250+ exec ("chmod 600 ' $ sshkeys' " );
251+
252+ $ this ->app ->log ("ssh-rsa key added to " .$ sshkeys ,LOGLEVEL_DEBUG );
253+ }
187254
188255
189256} // end class
0 commit comments