/** * --- Retrieve users --- * Retrieves all users along with their corresponding packages as well as the assigned nimbusec bundle * Executes two request to the WHM API */ function retrieveUsers() { $hash = file_get_contents("/root/.accesshash"); $host = gethostname(); $serverAddr = gethostbyname($host); // Contains all packages + users $packages = array(); // Get access data for WHM API $whmApi = new WHMAPIClient($hash, $serverAddr); // Retrieve all packages $pkgResult = $whmApi->sendRequest('listpkgs'); // Loop through every package to get the name of the package foreach ($pkgResult['data']['pkg'] as $pkg) { if (!empty($pkg['_PACKAGE_EXTENSIONS'])) { if (strpos($pkg['_PACKAGE_EXTENSIONS'], "nimbusec") !== false) { $bundle = explode('_', $pkg['nimbusec_bundles']); array_push($packages, array("packageName" => $pkg['name'], "bundleName" => $bundle[0], "bundleID" => $bundle[1], "users" => array())); } } } if (!empty($packages)) { // Retrieve all users $acctResult = $whmApi->sendRequest('listaccts'); foreach ($acctResult['data']['acct'] as $acct) { foreach ($packages as $key => $pkg) { // If the user's package has the same package name if (in_array($acct['plan'], $pkg, true)) { array_push($packages[$key]['users'], array('email' => $acct['email'], 'domain' => $acct['domain'], 'name' => $acct['user'])); } } } return $packages; } else { return "No package with nimbusec included could be found on the system."; } }
function checkPackage($input = array()) { $logger = new Logger("/usr/local/nimbusec/logs", "changePackage.log", true); $data = $input['data']; $logger->info("Triggered change_package hook"); try { $logger->info("Check bundles"); // Get access data for WHM API $hash = file_get_contents("/root/.accesshash"); $host = gethostname(); $serverAddr = gethostbyname($host); $whmApi = new WHMAPIClient($hash, $serverAddr); $oldPkgName = $data['cur_pkg']; $newPkgName = $data['new_pkg']; $userName = $data['user']; $logger->debug("Get input data [cur/old_pkg] '{$oldPkgName}' [new_pkg] '{$newPkgName}' and [user] '{$userName}'"); $oldPkgRes = $whmApi->sendRequest('getpkginfo', array("pkg" => $oldPkgName)); $newPkgRes = $whmApi->sendRequest('getpkginfo', array("pkg" => $newPkgName)); $logger->info("Read infomation for both packages"); // Isset is much faster than array_key_exists $old_hasNimbusec = isset($oldPkgRes['data']['pkg']['nimbusec_bundles']); $new_hasNimbusec = isset($newPkgRes['data']['pkg']['nimbusec_bundles']); $logger->debug("Old package [has_nimbusec] 'json_encode({$old_hasNimbusec})', new package [has_nimbusec] 'json_encode({$new_hasNimbusec})'"); list($key, $secret) = $whmApi->getNVData(array("NIMBUSEC_APIKEY", "NIMBUSEC_APISECRET")); if (!$old_hasNimbusec && $new_hasNimbusec) { $logger->info("Include nimbusec and provision [user] '{$userName}'"); $accRes = $whmApi->sendRequest('accountsummary', array("user" => $userName)); $logger->info("Retrieve account information of user"); $data = array("user" => $userName, "domain" => $accRes['data']['acct'][0]['domain'], "contactemail" => $accRes['data']['acct'][0]['email'], "nimbusec_bundles" => $newPkgRes['data']['pkg']['nimbusec_bundles']); if (isset($data['contactemail'])) { $logger->debug("Read relevant data for [user] '{$userName}': [domain] => '{$accRes['data']['acct'][0]['domain']}'\n\t\t\t\t, [contactemail] => '{$accRes['data']['acct'][0]['email']}' and [nimbusec_bundles] => '{$newPkgRes['data']['pkg']['nimbusec_bundles']}'"); $logger->info("Provisioning begins.."); $provision = new Provision($key, $secret); $res = $provision->provisionUser($data, $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Provisioning ends.."); $logger->close(); return array(1, $res[1]); } else { $str = "No email specified. Therefore user can't be provisioned with nimbusec"; $logger->info($str); $logger->close(); return array("1", $str); } } else { if ($old_hasNimbusec && $new_hasNimbusec) { $logger->info("Checking package bundles..."); $oldBundle = $oldPkgRes['data']['pkg']['nimbusec_bundles']; $newBundle = $newPkgRes['data']['pkg']['nimbusec_bundles']; // Check / update bundles if ($oldBundle == $newBundle) { $logger->info("The old package's bundle and the new package's bundle is the same"); $logger->debug("[old_bundle] '{$oldBundle}' == [new_bundle] '{$newBundle}'"); $logger->close(); return array("1", "The old package's bundle and the new package's bundle is the same"); } else { $logger->info("Updating bundle begins.."); $accRes = $whmApi->sendRequest('accountsummary', array("user" => $userName)); $logger->info("Retrieve account information of user"); $email = $accRes['data']['acct'][0]['email']; $logger->debug("Updating {$oldPkgName} with nimbusec bundle '{$oldBundle}' to {$newPkgName} with nimbusec bundle '{$newBundle}' for user {$email}"); $provision = new Provision($key, $secret); $res = $provision->updateBundle($email, $newBundle, $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Updating bundle ends.."); $logger->close(); return array(1, $res[1]); } } else { if ($old_hasNimbusec && !$new_hasNimbusec) { $logger->info("Removing account begins.."); $accRes = $whmApi->sendRequest('accountsummary', array("user" => $userName)); $logger->info("Retrieve account information of user"); $email = $accRes['data']['acct'][0]['email']; $logger->debug("Removing user {$userName} with email {$email} from database and system"); $provision = new Provision($key, $secret); $res = $provision->removeUser(array("user" => $userName, "contactemail" => $email), $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Removing user ends.."); $logger->close(); return array(1, $res[1]); } else { if (!$old_hasNimbusec && !$new_hasNimbusec) { $str = "Neither the old package nor the new package have nimbusec included."; $logger->info($str); $logger->close(); return array("1", $str); } } } } $str = "Something unexpected happened in change_package hook. Better check immediately"; $logger->error($str); $logger->close(); return array("1", $str); } catch (CUrlException $exp) { $logger->error("[CUrl SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in change_package hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (NimbusecException $exp) { $logger->error("[Nimbusec SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in change_package hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (WHMException $exp) { $logger->error("[WHM SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in change_package hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (Exception $exp) { $logger->error("[UNSPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in change_package hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } }
$logger->progress("Nimbusec uninstallation begins..."); // Get access data for WHM API - do not delete $hash = file_get_contents("/root/.accesshash"); $host = gethostname(); $serverAddr = gethostbyname($host); $whmApi = new WHMAPIClient($hash, $serverAddr); list($key, $secret, $agentKey) = $whmApi->getNVData(array("NIMBUSEC_APIKEY", "NIMBUSEC_APISECRET", "NIMBUSEC_SERVERAGENTKEY")); $nimbusecAPI = new NimbusecAPI($key, $secret); // To remind, every error will be handled in the particular method of each PHP (!) class you use // and catched in this class // So you can log after every command without needing to check whether it worked properly or not // because if it's not, it will be catched by the 'catch' - clause which will log it in addition // ################################## 1.) Remove all users and domains from database ################################## $logger->progress("Removing all user and domains from database..."); // Retrieve names of all packages containing nimbusec extension into an array $pkgResult = $whmApi->sendRequest('listpkgs'); $logger->info("Packages retrieved"); $logger->debug("Resultstatus WHM API listpkgs: {$pkgResult['metadata']['result']}"); // Loop through every package to get name + list of package extensions foreach ($pkgResult['data']['pkg'] as $pkg) { if (!empty($pkg['_PACKAGE_EXTENSIONS'])) { // Space-seperated list $extensions = explode(' ', $pkg['_PACKAGE_EXTENSIONS']); // Look for the key word nimbusec.. // If the package has nimbusec, remove it from the list and add it to the array if (($keyPos = array_search("nimbusec", $extensions, true)) !== false) { unset($extensions[$keyPos]); array_push($pkgInfos, array("name" => $pkg['name'], "extensions" => implode(' ', $extensions))); } } else { $logger->debug("Zero package extensions installed for package: {$pkg['name']}");
function removeUser($input = array()) { $logger = new Logger("/usr/local/nimbusec/logs", "remove.log", true); $data = $input['data']; $userName = ""; if (array_key_exists('user', $data)) { $userName = $data['user']; } else { $userName = $data['username']; } $logger->info("Triggered removeuser hook"); $logger->debug("Removing user {$userName}"); $logger->info("Check bundle"); try { // Get access data for WHM API $hash = file_get_contents("/root/.accesshash"); $host = gethostname(); $serverAddr = gethostbyname($host); $whmApi = new WHMAPIClient($hash, $serverAddr); $accRes = $whmApi->sendRequest('accountsummary', array("user" => $userName)); $logger->info("Retrieve account information of user"); $pkgRes = $whmApi->sendRequest('getpkginfo', array("pkg" => $accRes['data']['acct'][0]['plan'])); $logger->info("Read infomation for user's package {$accRes['data']['acct'][0]['plan']}"); $hasNimbusec = isset($pkgRes['data']['pkg']['nimbusec_bundles']); $logger->debug("User has nimbusec [has_nimbusec] 'json_encode({$hasNimbusec})'"); list($key, $secret) = $whmApi->getNVData(array("NIMBUSEC_APIKEY", "NIMBUSEC_APISECRET")); if ($hasNimbusec) { $logger->info("User has nimbusec"); $logger->info("Removing begins.."); $provision = new Provision($key, $secret); $res = $provision->removeUser(array("user" => $userName, "contactemail" => $accRes['data']['acct'][0]['email']), $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Removing ends.."); $logger->close(); return array(1, $res[1]); } else { $str = "User doesn't have nimbusec"; $logger->info($str); $logger->close(); return array("1", $str); } $str = "Something unexpected happened in remove hook. Better check immediately"; $logger->error($str); $logger->close(); return array("1", $str); } catch (CUrlException $exp) { $logger->error("[CUrl SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in remove hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (NimbusecException $exp) { $logger->error("[Nimbusec SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in remove hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (WHMException $exp) { $logger->error("[WHM SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in remove hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (Exception $exp) { $logger->error("[UNSPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in remove hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } }