'Missing required parameters']); return; } error_log(" Creating user " . $username . " DebugMode: " . $GLOBALS['debug']); $userExisted = false; if( strpos(file_get_contents("/etc/passwd"),$username) !== false) { $userExisted = true; } if ($userExisted != true) { // Create user without login access if ($GLOBALS['debug'] == true) {error_log("Adding User: " . $username); } exec("sudo /usr/sbin/useradd -m -k -M -s /usr/sbin/nologin $username 2>&1", $userOutput, $userReturnCode); if ($userReturnCode !== 0) { error_log("deploy: ERROR: Useradd for $username failed, details => " . implode("\n", $userOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to create user', 'details' => implode("\n", $userOutput)]); return; } } if ($GLOBALS['debug'] == true) { error_log("Creating logdir for : " . $username); } if (is_dir("/home/$username/logs") != true) { exec("sudo /usr/bin/mkdir -p /home/$username/logs 2>&1", $mkdirOutput, $mkdirReturnCode); if ($mkdirReturnCode !== 0) { error_log("deploy: ERROR: Failed to create log directory for $username failed, details => " . implode("\n", $mkdirOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to create logs dir', 'details' => implode("\n", $mkdirOutput)]); return; } } if ($GLOBALS['debug'] == true) { error_log("Creating backup dir for : " . $username); } if (is_dir("/home/$username/backups") != true) { exec("sudo /usr/bin/mkdir -p /home/$username/backups 2>&1", $mkdirOutput, $mkdirReturnCode); if ($mkdirReturnCode !== 0) { error_log("deploy: ERROR: mkdir /home/$username/backups failed, details => " . implode("\n", $mkdirOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to create backups dir', 'details' => implode("\n", $mkdirOutput)]); return; } } if ($GLOBALS['debug'] == true) { error_log("Creating tmpdir for : " . $username); } if (is_dir("/home/$username/tmp") != true) { exec("sudo /usr/bin/mkdir -p /home/$username/tmp 2>&1", $mkdirOutput, $mkdirReturnCode); if ($mkdirReturnCode !== 0) { error_log("deploy: ERROR: Failed to create temp directory for $username failed, details => " . implode("\n", $mkdirOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to create tmp dir', 'details' => implode("\n", $mkdirOutput)]); return; } } if ($GLOBALS['debug'] == true) { error_log("Chown homedir: " . $username); } exec("sudo /usr/bin/chown $username:$username /home/$username -R 2>&1", $chownOutput, $chownReturnCode); if ($chownReturnCode !== 0) { error_log("deploy: ERROR: chown on /home/$username failed, details => " . implode("\n", $chownOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to chown backups dir', 'details' => implode("\n", $chownOutput)]); return; } // Create PHP-FPM User // /etc/php/8.2/fpm/user.in if ($userExisted != true) { if ($GLOBALS['debug'] == true) {error_log("Writing PHP-FPM Config for : " . $username); } $phpContent = file_get_contents("/etc/php/8.2/fpm/user.in"); $phpContent = str_replace("USERNAME", $username, $phpContent); file_put_contents("/etc/php/8.2/fpm/pool.d/" . $username . ".conf", $phpContent); if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);} exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode); if ($phpReturnCode !== 0) { error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]); return; } } echo json_encode(['success' => 'User crreation successfully','details' => '']); } public static function terminate($data): void { $username = $data['username'] ?? ''; $domain = $data['domain'] ?? ''; if (empty($username)) { error_log("terminate: ERROR: No username provided"); http_response_code(400); error_log(print_r($data,true)); echo json_encode(['error' => 'Missing username']); return; } if( strpos(file_get_contents("/etc/passwd"),$username) == false) { error_log("terminate: ERROR: User $username does not exist"); http_response_code(400); error_log(print_r($data,true)); echo json_encode(['error' => 'Unknown user']); return; } // remove PHP-FPM User if ($GLOBALS['debug'] == true) {error_log("Removing PHP-FPM Config for : " . $username); } $configName = "/etc/php/8.2/fpm/pool.d/" . $username . ".conf"; exec("sudo /usr/bin/rm -f $configName 2>&1", $userOutput, $userReturnCode); if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);} exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode); if ($phpReturnCode !== 0) { error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]); return; } if(!empty($domain)) { $configFile = "/etc/apache2/sites-enabled/$domain.conf"; if ($GLOBALS['debug'] == true) { error_log("Remove config of user : " . $username); } exec("sudo /usr/bin/rm -f $configFile 2>&1", $userOutput, $userReturnCode); exec("sudo /usr/bin/certbot delete --cert-name $domain --non-interactive 2>&1", $output, $returnCode); if ($returnCode !== 0) { error_log("deploy: ERROR: certbot failed to delete certificate on $domain, details => " . implode("\n", $output)); http_response_code(500); echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]); return; } exec('sudo /usr/bin/systemctl reload apache2 2>&1', $apacheOutput, $apacheReturnCode); if ($GLOBALS['debug'] == true) { error_log("Restarting Apache"); } if ($apacheReturnCode !== 0) { error_log("deploy: ERROR: Apache Reload error, details => " . implode("\n", $apacheOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]); return; } } // Remove user and files if ($GLOBALS['debug'] == true) {error_log("Remove User: " . $username); } exec("sudo /usr/sbin/userdel -r -f $username 2>&1", $userOutput, $userReturnCode); if ($userReturnCode !== 0) { error_log("deploy: ERROR: Userdel for $username failed, details => " . implode("\n", $userOutput)); http_response_code(500); echo json_encode(['error' => 'Failed to remove user', 'details' => implode("\n", $userOutput)]); return; } echo json_encode(['success' => 'Removing user ' . $username . ' successfully']); } }