@@ -103,14 +103,27 @@ function insert($event_name, $data) {
103103 if ($ uid > $ this ->min_uid ) {
104104 //* Remove webfolder protection
105105 $ app ->system ->web_folder_protection ($ web ['document_root ' ], false );
106-
107- if (!is_dir ($ data ['new ' ]['dir ' ])){
108- $ app ->file ->mkdirs (escapeshellcmd ($ data ['new ' ]['dir ' ]), '0700 ' );
109- $ app ->system ->chown (escapeshellcmd ($ data ['new ' ]['dir ' ]),escapeshellcmd ($ data ['new ' ]['username ' ]));
110- $ app ->system ->chgrp (escapeshellcmd ($ data ['new ' ]['dir ' ]),escapeshellcmd ($ data ['new ' ]['pgroup ' ]));
106+
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 ' ]));
111124 }
112125 $ command = 'useradd ' ;
113- $ command .= ' -d ' .escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] );
126+ $ command .= ' -d ' .escapeshellcmd ($ homedir );
114127 $ command .= ' -g ' .escapeshellcmd ($ data ['new ' ]['pgroup ' ]);
115128 $ command .= ' -o ' ; // non unique
116129 if ($ data ['new ' ]['password ' ] != '' ) $ command .= ' -p ' .escapeshellcmd ($ data ['new ' ]['password ' ]);
@@ -129,10 +142,10 @@ function insert($event_name, $data) {
129142 $ this ->_setup_ssh_rsa ();
130143
131144 //* Create .bash_history file
132- $ app ->system ->touch (escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] ).'/.bash_history ' );
133- $ app ->system ->chmod (escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] ).'/.bash_history ' , 0755 );
134- $ app ->system ->chown (escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] ).'/.bash_history ' , $ data ['new ' ]['username ' ]);
135- $ app ->system ->chgrp (escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] ).'/.bash_history ' , $ data ['new ' ]['pgroup ' ]);
145+ $ app ->system ->touch (escapeshellcmd ($ homedir ).'/.bash_history ' );
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 ' ]);
136149
137150 //* Disable shell user temporarily if we use jailkit
138151 if ($ data ['new ' ]['chroot ' ] == 'jailkit ' ) {
@@ -185,6 +198,16 @@ function update($event_name, $data) {
185198 // Get the UID of the parent user
186199 $ uid = intval ($ app ->system ->getuid ($ data ['new ' ]['puser ' ]));
187200 if ($ uid > $ this ->min_uid ) {
201+
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+
188211 // Check if the user that we want to update exists, if not, we insert it
189212 if ($ app ->system ->is_user ($ data ['old ' ]['username ' ])) {
190213 /*
@@ -202,12 +225,32 @@ function update($event_name, $data) {
202225 $app->log("Executed command: $command ",LOGLEVEL_DEBUG);
203226 */
204227 //$groupinfo = $app->system->posix_getgrnam($data['new']['pgroup']);
205- if ($ data ['new ' ]['dir ' ] != $ data ['old ' ]['dir ' ] && !is_dir ($ data ['new ' ]['dir ' ])){
206- $ app ->file ->mkdirs (escapeshellcmd ($ data ['new ' ]['dir ' ]), '0700 ' );
207- $ app ->system ->chown (escapeshellcmd ($ data ['new ' ]['dir ' ]),escapeshellcmd ($ data ['new ' ]['username ' ]));
208- $ app ->system ->chgrp (escapeshellcmd ($ data ['new ' ]['dir ' ]),escapeshellcmd ($ data ['new ' ]['pgroup ' ]));
228+ if ($ homedir != $ homedir_old && !is_dir ($ homedir )){
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+ }
209252 }
210- $ app ->system ->usermod ($ data ['old ' ]['username ' ], 0 , $ app ->system ->getgid ($ data ['new ' ]['pgroup ' ]), $ data [ ' new ' ][ ' dir ' ] , $ data ['new ' ]['shell ' ], $ data ['new ' ]['password ' ], $ data ['new ' ]['username ' ]);
253+ $ app ->system ->usermod ($ data ['old ' ]['username ' ], 0 , $ app ->system ->getgid ($ data ['new ' ]['pgroup ' ]), $ homedir , $ data ['new ' ]['shell ' ], $ data ['new ' ]['password ' ], $ data ['new ' ]['username ' ]);
211254 $ app ->log ("Updated shelluser: " .$ data ['old ' ]['username ' ], LOGLEVEL_DEBUG );
212255
213256 // call the ssh-rsa update function
@@ -218,10 +261,10 @@ function update($event_name, $data) {
218261
219262 //* Create .bash_history file
220263 if (!is_file ($ data ['new ' ]['dir ' ]).'/.bash_history ' ) {
221- $ app ->system ->touch (escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] ).'/.bash_history ' );
222- $ app ->system ->chmod (escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] ).'/.bash_history ' , 0755 );
223- $ app ->system ->chown (escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] ).'/.bash_history ' , escapeshellcmd ($ data ['new ' ]['username ' ]));
224- $ app ->system ->chgrp (escapeshellcmd ($ data [ ' new ' ][ ' dir ' ] ).'/.bash_history ' , escapeshellcmd ($ data ['new ' ]['pgroup ' ]));
264+ $ app ->system ->touch (escapeshellcmd ($ homedir ).'/.bash_history ' );
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 ' ]));
225268 }
226269
227270 } else {
@@ -260,7 +303,12 @@ function delete($event_name, $data) {
260303 $ app ->system ->web_folder_protection ($ web ['document_root ' ], false );
261304
262305 // delete dir
263- $ homedir = $ data ['old ' ]['dir ' ];
306+ if ($ data ['new ' ]['chroot ' ] == 'jailkit ' ) {
307+ $ homedir = $ data ['old ' ]['dir ' ];
308+ } else {
309+ $ homedir = $ data ['old ' ]['dir ' ].'/home/ ' .$ data ['old ' ]['username ' ];
310+ }
311+
264312 if (substr ($ homedir , -1 ) !== '/ ' ) $ homedir .= '/ ' ;
265313 $ files = array ('.bash_logout ' , '.bash_history ' , '.bashrc ' , '.profile ' );
266314 $ dirs = array ('.ssh ' , '.cache ' );
0 commit comments