| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\Cron; |
| 4 |
|
| 5 |
|
| 6 |
/* |
| 7 |
|
| 8 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 9 |
Copyright (C) 2024 - 2025 Roland Gruber |
| 10 |
|
| 11 |
This program is free software; you can redistribute it and/or modify |
| 12 |
it under the terms of the GNU General Public License as published by |
| 13 |
the Free Software Foundation; either version 2 of the License, or |
| 14 |
(at your option) any later version. |
| 15 |
|
| 16 |
This program is distributed in the hope that it will be useful, |
| 17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
GNU General Public License for more details. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License |
| 22 |
along with this program; if not, write to the Free Software |
| 23 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 |
|
| 25 |
*/ |
| 26 |
|
| 27 |
use Exception; |
| 28 |
use LAM\DB\CronDatabase; |
| 29 |
use LAM\JOB\JobResultLog; |
| 30 |
use LAMCfgMain; |
| 31 |
use LAMConfig; |
| 32 |
use LAMException; |
| 33 |
use Ldap; |
| 34 |
use PDO; |
| 35 |
use PDOException; |
| 36 |
use ServerProfilePersistenceManager; |
| 37 |
|
| 38 |
/** |
| 39 |
* Cron jobs for LAM. |
| 40 |
* |
| 41 |
* @package jobs |
| 42 |
* @author Roland Gruber |
| 43 |
*/ |
| 44 |
|
| 45 |
/** access to configuration data */ |
| 46 |
include_once(__DIR__ . "/config.inc"); |
| 47 |
/** access to ldap data */ |
| 48 |
include_once(__DIR__ . "/ldap.inc"); |
| 49 |
/** access to modules */ |
| 50 |
include_once(__DIR__ . "/modules.inc"); |
| 51 |
|
| 52 |
/** |
| 53 |
* Executes cron jobs. |
| 54 |
*/ |
| 55 |
class CronRunner { |
| 56 |
|
| 57 |
private bool $isDryRun; |
| 58 |
|
| 59 |
/** |
| 60 |
* Constructor |
| 61 |
* |
| 62 |
* @param bool $isDryRun dry-run |
| 63 |
*/ |
| 64 |
public function __construct(bool $isDryRun) { |
| 65 |
$this->isDryRun = $isDryRun; |
| 66 |
if ($isDryRun) { |
| 67 |
echo "Dry run: no changes, only logging.\n"; |
| 68 |
} |
| 69 |
$_SESSION['cfgMain'] = new LAMCfgMain(); |
| 70 |
// set certificates variables |
| 71 |
setSSLCaCert(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Runs all global cron actions. |
| 76 |
* @throws LAMException error running actions |
| 77 |
*/ |
| 78 |
public function runGlobalCronJobs(): void { |
| 79 |
if ($this->isDryRun) { |
| 80 |
echo "Global actions started\n\n"; |
| 81 |
} |
| 82 |
$errorsOccurred = false; |
| 83 |
$allModules = getAllModules(); |
| 84 |
foreach ($allModules as $module) { |
| 85 |
if (!$module->supportsGlobalCronJob()) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
try { |
| 89 |
if ($this->isDryRun) { |
| 90 |
echo "Started actions for " . $module->get_alias() . "\n"; |
| 91 |
} |
| 92 |
logNewMessage(LOG_NOTICE, 'Started actions for ' . $module->get_alias()); |
| 93 |
$module->runGlobalCronActions($this->isDryRun); |
| 94 |
if ($this->isDryRun) { |
| 95 |
echo "Finished actions for " . $module->get_alias() . "\n"; |
| 96 |
} |
| 97 |
logNewMessage(LOG_NOTICE, 'Finished actions for ' . $module->get_alias()); |
| 98 |
} |
| 99 |
catch (Exception $e) { |
| 100 |
$errorsOccurred = true; |
| 101 |
echo "Error in " . $module->get_alias() . ': ' . $e->getMessage() . "\n" . $e->getTraceAsString() . "\n"; |
| 102 |
} |
| 103 |
} |
| 104 |
if ($errorsOccurred) { |
| 105 |
throw new LAMException("Problems occurred during cron execution"); |
| 106 |
} |
| 107 |
if ($this->isDryRun) { |
| 108 |
echo "\nGlobal actions done\n\n"; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Runs the actions of a single server profile. |
| 114 |
* |
| 115 |
* @param string $serverProfileName profile name |
| 116 |
* @throws LAMException error running actions |
| 117 |
*/ |
| 118 |
public function runSingleServerProfileActions(string $serverProfileName): void { |
| 119 |
/** Access to database */ |
| 120 |
include_once(__DIR__ . "/database.inc"); |
| 121 |
$serverProfilePersistenceManager = new ServerProfilePersistenceManager(); |
| 122 |
$this->validateServerProfileName($serverProfileName, $serverProfilePersistenceManager); |
| 123 |
if ($this->isDryRun) { |
| 124 |
echo "Server profile started: " . $serverProfileName . "\n\n"; |
| 125 |
} |
| 126 |
|
| 127 |
try { |
| 128 |
$conf = $serverProfilePersistenceManager->loadProfile($serverProfileName); |
| 129 |
$_SESSION['config'] = $conf; |
| 130 |
} |
| 131 |
catch (LAMException $e) { |
| 132 |
echo $e->getTitle(); |
| 133 |
exit(1); |
| 134 |
} |
| 135 |
|
| 136 |
// test database connection |
| 137 |
try { |
| 138 |
$cronDatabase = new CronDatabase($conf); |
| 139 |
$cronDatabase->connect(); |
| 140 |
$pdo = $cronDatabase->getPdo(); |
| 141 |
} |
| 142 |
catch (PDOException $e) { |
| 143 |
throw new LAMException("Unable to connect to database. " . $e->getMessage(), '', $e); |
| 144 |
} |
| 145 |
catch (LAMException $e) { |
| 146 |
throw new LAMException("Unable to connect to database. " . $e->getTitle(), '', $e); |
| 147 |
} |
| 148 |
// get jobs to run |
| 149 |
if (empty($cronDatabase->getJobs())) { |
| 150 |
if ($this->isDryRun) { |
| 151 |
echo "No jobs configured for " . $serverProfileName . "\n\n"; |
| 152 |
} |
| 153 |
return; |
| 154 |
} |
| 155 |
$this->openLdapConnection(); |
| 156 |
if ($this->isDryRun) { |
| 157 |
echo "\nServer profile done: " . $serverProfileName . "\n\n"; |
| 158 |
} |
| 159 |
$this->runServerProfileJobs($conf, $cronDatabase, $pdo, $serverProfileName); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Validates the server profile. |
| 164 |
* |
| 165 |
* @param string $serverProfileName profile name |
| 166 |
* @param ServerProfilePersistenceManager $serverProfilePersistenceManager persistence manager |
| 167 |
* @throws LAMException invalid profile |
| 168 |
*/ |
| 169 |
private function validateServerProfileName(string $serverProfileName, ServerProfilePersistenceManager $serverProfilePersistenceManager): void { |
| 170 |
if (!LAMConfig::isValidName($serverProfileName)) { |
| 171 |
throw new LAMException("Invalid config name: " . $serverProfileName); |
| 172 |
} |
| 173 |
$serverProfiles = []; |
| 174 |
try { |
| 175 |
$serverProfiles = $serverProfilePersistenceManager->getProfiles(); |
| 176 |
} |
| 177 |
catch (LAMException $e) { |
| 178 |
logNewMessage(LOG_ERR, 'Unable to read server profiles: ' . $e->getTitle()); |
| 179 |
} |
| 180 |
if (!in_array($serverProfileName, $serverProfiles)) { |
| 181 |
throw new LAMException("Config does not exist: " . $serverProfileName); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Opens the LDAP connection. |
| 187 |
* @throws LAMException unable to connect to LDAP |
| 188 |
*/ |
| 189 |
private function openLdapConnection(): void { |
| 190 |
$ldap = new Ldap($_SESSION['config']); |
| 191 |
try { |
| 192 |
$ldap->connect($_SESSION['config']->getJobsBindUser(), deobfuscateText($_SESSION['config']->getJobsBindPassword()), true); |
| 193 |
} |
| 194 |
catch (LAMException) { |
| 195 |
if ($ldap->server() === null) { |
| 196 |
throw new LAMException("Unable to connect to LDAP"); |
| 197 |
} |
| 198 |
throw new LAMException("Unable to connect to LDAP with error: " . ldap_error($ldap->server()) . "\n" . getExtendedLDAPErrorMessage($ldap->server())); |
| 199 |
} |
| 200 |
$_SESSION['ldap'] = $ldap; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Runs the actions of a server profile. |
| 205 |
* |
| 206 |
* @param LAMConfig $conf config |
| 207 |
* @param CronDatabase $cronDatabase cron DB |
| 208 |
* @param PDO $pdo PDO |
| 209 |
* @param string $serverProfileName server profile name |
| 210 |
* @throws LAMException error executing job |
| 211 |
*/ |
| 212 |
private function runServerProfileJobs(LAMConfig $conf, CronDatabase $cronDatabase, PDO $pdo, string $serverProfileName): void { |
| 213 |
$errorsOccurred = false; |
| 214 |
foreach ($cronDatabase->getJobs() as $jobId => $job) { |
| 215 |
$resultLog = new JobResultLog(); |
| 216 |
$jobName = $job->getName(); |
| 217 |
try { |
| 218 |
logNewMessage(LOG_NOTICE, 'Running job ' . $jobName . ' for server profile ' . $serverProfileName); |
| 219 |
$resultLog->logInfo('Running job ' . $jobName . ' for server profile ' . $serverProfileName); |
| 220 |
$job->execute($jobId, $conf->getJobSettings(), $pdo, $this->isDryRun, $resultLog); |
| 221 |
if (!$this->isDryRun) { |
| 222 |
if ($resultLog->hasError()) { |
| 223 |
$cronDatabase->saveJobRun($jobName, $jobId, 1, implode("\n", $resultLog->getMessages())); |
| 224 |
} |
| 225 |
else { |
| 226 |
$cronDatabase->saveJobRun($jobName, $jobId, 0, implode("\n", $resultLog->getMessages())); |
| 227 |
} |
| 228 |
} |
| 229 |
else { |
| 230 |
// for dry-run print messages to console |
| 231 |
echo implode("\n", $resultLog->getMessages()) . "\n\n"; |
| 232 |
} |
| 233 |
logNewMessage(LOG_NOTICE, 'Finished job ' . $jobName . ' for server profile ' . $serverProfileName); |
| 234 |
$resultLog->logInfo('Finished job ' . $jobName . ' for server profile ' . $serverProfileName); |
| 235 |
} |
| 236 |
catch (Exception $e) { |
| 237 |
$errorsOccurred = true; |
| 238 |
echo "Error in job " . $jobName . ': ' . $e->getMessage() . "\n" . $e->getTraceAsString() . "\n"; |
| 239 |
if (!$this->isDryRun) { |
| 240 |
$cronDatabase->saveJobRun($jobName, $jobId, 1, implode("\n", $resultLog->getMessages()) . "\n" . $e->getMessage()); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
if ($errorsOccurred) { |
| 245 |
throw new LAMException("Problems occurred during job execution for server profile " . $serverProfileName); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
} |