From a7e4ec0ce669549c826cdbb8add5c7d50469a772 Mon Sep 17 00:00:00 2001
From: Till Brehm <tbrehm@ispconfig.org>
Date: Thu, 14 Aug 2014 15:58:40 -0400
Subject: [PATCH] Added new directory delete procedure in jailkit plugin.

---
 server/plugins-available/shelluser_base_plugin.inc.php    |    7 +++++++
 server/plugins-available/shelluser_jailkit_plugin.inc.php |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/server/plugins-available/shelluser_base_plugin.inc.php b/server/plugins-available/shelluser_base_plugin.inc.php
index a6dbfcf..651ccc9 100755
--- a/server/plugins-available/shelluser_base_plugin.inc.php
+++ b/server/plugins-available/shelluser_base_plugin.inc.php
@@ -254,6 +254,11 @@
 				// check if we have to delete the dir
 				$check = $app->db->queryOneRecord('SELECT shell_user_id FROM `shell_user` WHERE `dir` = \'' . $app->db->quote($data['old']['dir']) . '\'');
 				if(!$check && is_dir($data['old']['dir'])) {
+					
+					$web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".intval($data['old']['parent_domain_id']));
+					
+					$app->system->web_folder_protection($web['document_root'], false);
+					
 					// delete dir
 					$homedir = $data['old']['dir'];
 					if(substr($homedir, -1) !== '/') $homedir .= '/';
@@ -281,6 +286,8 @@
 					}
 					unset($files);
 					unset($dirs);
+					
+					$app->system->web_folder_protection($web['document_root'], true);
 				}
 				
 				// We delete only non jailkit users, jailkit users will be deleted by the jailkit plugin.
diff --git a/server/plugins-available/shelluser_jailkit_plugin.inc.php b/server/plugins-available/shelluser_jailkit_plugin.inc.php
index cb222fd..a391835 100755
--- a/server/plugins-available/shelluser_jailkit_plugin.inc.php
+++ b/server/plugins-available/shelluser_jailkit_plugin.inc.php
@@ -247,9 +247,13 @@
 			$app->system->web_folder_protection($web['document_root'], false);
 
 			if(@is_dir($data['old']['dir'].$jailkit_chroot_userhome)) {
+				$userid = intval($app->system->getuid($data['old']['username']));
 				$command = 'killall -u '.escapeshellcmd($data['old']['username']).' ; userdel -f';
 				$command .= ' '.escapeshellcmd($data['old']['username']).' &> /dev/null';
 				exec($command);
+				
+				$this->_delete_homedir($data['old']['dir'].$jailkit_chroot_userhome,$userid,$data['old']['parent_domain_id']);
+				
 				$app->log("Jailkit Plugin -> delete chroot home:".$data['old']['dir'].$jailkit_chroot_userhome, LOGLEVEL_DEBUG);
 			}
 
@@ -523,6 +527,48 @@
 		exec("chmod 600 '$sshkeys'");
 
 	}
+	
+	private function _delete_homedir($homedir,$userid,$parent_domain_id) {
+		global $app, $conf;
+		
+		// check if we have to delete the dir
+				$check = $app->db->queryOneRecord('SELECT shell_user_id FROM `shell_user` WHERE `dir` = \'' . $app->db->quote($homedir) . '\'');
+				
+				if(!$check && is_dir($homedir)) {
+					$web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".intval($parent_domain_id));
+					$app->system->web_folder_protection($web['document_root'], false);
+					
+					// delete dir
+					if(substr($homedir, -1) !== '/') $homedir .= '/';
+					$files = array('.bash_logout', '.bash_history', '.bashrc', '.profile');
+					$dirs = array('.ssh');
+					foreach($files as $delfile) {
+						if(is_file($homedir . $delfile) && fileowner($homedir . $delfile) == $userid) unlink($homedir . $delfile);
+					}
+					foreach($dirs as $deldir) {
+						if(is_dir($homedir . $deldir) && fileowner($homedir . $deldir) == $userid) exec('rm -rf ' . escapeshellarg($homedir . $deldir));
+					}
+					$empty = true;
+					$dirres = opendir($homedir);
+					if($dirres) {
+						while(($entry = readdir($dirres)) !== false) {
+							if($entry != '.' && $entry != '..') {
+								$empty = false;
+								break;
+							}
+						}
+						closedir($dirres);
+					}
+					if($empty == true) {
+						rmdir($homedir);
+					}
+					unset($files);
+					unset($dirs);
+					
+					$app->system->web_folder_protection($web['document_root'], true);
+				}
+	
+	}
 
 } // end class
 

--
Gitblit v1.9.1