Debian Codegraph @

utils/check_config.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2023-2025 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This script is for checking configuration.
25 *
26 * The script can be useful for checking your configuration.
27 *
28 * @category Utilities
29 * @package DmarcSrg
30 * @author Aleksey Andreev (liuch)
31 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
32 */
33
34 namespace Liuch\DmarcSrg;
35
36 use Liuch\DmarcSrg\Mail\MailBoxes;
37 use Liuch\DmarcSrg\Directories\DirectoryList;
38 use Liuch\DmarcSrg\Exception\SoftException;
39 use Liuch\DmarcSrg\Exception\LogicException;
40 use Liuch\DmarcSrg\Exception\RuntimeException;
41 use Liuch\DmarcSrg\RemoteFilesystems\RemoteFilesystemList;
42
43 require realpath(__DIR__ . '/..') . '/init.php';
44
45 if (Core::isWEB()) {
46 echo 'Forbidden';
47 exit(1);
48 }
49
50 const RESULT_SUCCESS = 1;
51 const RESULT_WARNING = 2;
52 const RESULT_SKIPPED = 3;
53 const RESULT_ERROR = 4;
54 const RESULT_FATAL = 5;
55
56 $e_cnt = 0;
57 $w_cnt = 0;
58 $m_str = '';
59
60 $startChecking = function (string $message, int $margin = 0) use (&$m_str): void {
61 $col_w = 35;
62 $m_str = str_repeat(' ', $margin);
63 $s = "{$m_str} * {$message}";
64 echo $s, str_repeat('.', max($col_w - strlen($s), 0)), ' ';
65 };
66
67 $endChecking = function (string $message = '', int $result = 0) use (&$e_cnt, &$w_cnt, &$m_str): void {
68 if ($result === 0) {
69 $result = empty($message) ? RESULT_SUCCESS : RESULT_ERROR;
70 }
71 switch ($result) {
72 case RESULT_SUCCESS:
73 echo 'Ok', PHP_EOL;
74 break;
75 case RESULT_WARNING:
76 ++$w_cnt;
77 echo 'Warning', PHP_EOL;
78 break;
79 case RESULT_SKIPPED:
80 echo 'Skipped', PHP_EOL;
81 break;
82 case RESULT_ERROR:
83 ++$e_cnt;
84 echo 'Fail', PHP_EOL;
85 break;
86 case RESULT_FATAL:
87 echo 'Fail', PHP_EOL;
88 throw new SoftException($message);
89 }
90 if (!empty($message)) {
91 echo "{$m_str} Message: ", $message, PHP_EOL;
92 }
93 };
94
95 $core = Core::instance();
96 $core->setCurrentUser('admin');
97
98 ob_start(null, 0, PHP_OUTPUT_HANDLER_FLUSHABLE | PHP_OUTPUT_HANDLER_REMOVABLE);
99 $core->config('debug'); // Just in order to load the config file
100 $empty_buf = empty(ob_get_flush());
101
102 try {
103 echo '=== GENERAL INFORMATION ===', PHP_EOL;
104 $uname = implode(' ', array_map(function ($mode) {
105 return php_uname($mode);
106 }, [ 's', 'r', 'v', 'm' ]));
107 echo ' * OS information: ', $uname, PHP_EOL;
108 echo ' * PHP version: ', phpversion(), PHP_EOL;
109
110 echo PHP_EOL, '=== EXTENSIONS ===', PHP_EOL;
111 foreach ([ 'pdo_mysql', 'xmlreader', 'zip', 'json' ] as $ext) {
112 $startChecking($ext);
113 try {
114 $core->checkDependencies($ext);
115 $endChecking();
116 } catch (SoftException $e) {
117 $endChecking('The extension is not loaded');
118 }
119 }
120
121 echo PHP_EOL, '=== CONFIG FILE ===', PHP_EOL;
122 $startChecking('Checking if the file exists');
123 if (is_file(CONFIG_FILE)) {
124 $endChecking();
125 } else {
126 $endChecking('The configuration file `' . CONFIG_FILE . '` not found', RESULT_FATAL);
127 }
128 $startChecking('Checking read permission');
129 if (is_readable(CONFIG_FILE)) {
130 $endChecking();
131 } else {
132 $endChecking('The configuration file is not readable', RESULT_FATAL);
133 }
134 $startChecking('Checking write permission');
135 if (!is_writable(CONFIG_FILE)) {
136 $endChecking();
137 } else {
138 $endChecking('The configuration file is writable', RESULT_WARNING);
139 }
140 $startChecking('Checking access by other users');
141 $r = fileperms(CONFIG_FILE);
142 if ($r !== false) {
143 if (($r & 0x6) !== 0) {
144 $endChecking('The configuration file is accessible to other users', RESULT_WARNING);
145 } else {
146 $endChecking();
147 }
148 } else {
149 $endChecking('Fileperms failed', RESULT_ERROR);
150 }
151 $startChecking('Checking the output buffer');
152 if ($empty_buf) {
153 $endChecking();
154 } else {
155 $endChecking('There are extra characters before the "<?php" string');
156 }
157
158 echo PHP_EOL, '=== DATABASE ===', PHP_EOL;
159 $startChecking('Accessibility check');
160 try {
161 $db_s = $core->database()->state();
162 $endChecking();
163 } catch (RuntimeException $e) {
164 $db_s = null;
165 $endChecking($e->getMessage());
166 }
167 if ($db_s) {
168 $startChecking('Checking for integrity');
169 if ($db_s['correct']) {
170 $endChecking();
171 } else {
172 $endChecking($db_s['message'], ($db_s['needs_upgrade'] ?? false) ? RESULT_WARNING : RESULT_ERROR);
173 }
174 }
175
176 echo PHP_EOL, '=== MAILBOXES ===', PHP_EOL;
177 $startChecking('Checking mailboxes config');
178 try {
179 $mb_list = new MailBoxes();
180 $mb_lcnt = $mb_list->count();
181 if ($mb_lcnt === 0) {
182 $endChecking('No mailboxes found', RESULT_SUCCESS);
183 } else {
184 $endChecking("{$mb_lcnt} mailbox" . ($mb_lcnt > 1 ? 'ex' : '') . ' found', RESULT_SUCCESS);
185 $startChecking('Imap library');
186 try {
187 $lib = $core->config('fetcher/mailboxes/library', 'auto');
188 $authm = [];
189 switch ($lib) {
190 case 'imap-engine':
191 try {
192 $core->checkDependencies('imap-engine');
193 } catch (SoftException $e) {
194 throw new SoftException('ImapEngine is not installed');
195 }
196 $authm = [ 'plain', 'oauth' ];
197 break;
198 case 'php-extension':
199 try {
200 $core->checkDependencies('imap');
201 } catch (SoftException $e) {
202 throw new SoftException('PHP IMAP extension is not installed');
203 }
204 $authm = [ 'plain' ];
205 break;
206 case 'auto':
207 try {
208 $core->checkDependencies('imap-engine');
209 $lib = 'imap-engine (auto)';
210 $authm = [ 'plain', 'oauth' ];
211 } catch (SoftException $e) {
212 try {
213 $core->checkDependencies('imap');
214 $lib = 'php-extension (auto)';
215 $authm = [ 'plain' ];
216 } catch (SoftException $e) {
217 throw new SoftException('Neither ImapEngine nor PHP IMAP extension is installed');
218 }
219 }
220 break;
221 default:
222 throw new SoftException('Unknown library name: ' . $lib);
223 }
224 $endChecking('using ' . $lib, RESULT_SUCCESS);
225 echo " * Checking mailboxes ({$mb_lcnt})", PHP_EOL;
226 for ($mb_num = 1; $mb_num <= $mb_lcnt; ++$mb_num) {
227 $mb = $mb_list->mailbox($mb_num);
228 echo " - {$mb->name()}", PHP_EOL;
229 $startChecking('Accessibility', 2);
230 if (in_array($mb->authMethod(), $authm)) {
231 $res = $mb->check();
232 if (!$res['error_code']) {
233 $endChecking();
234 } else {
235 $endChecking($res['message'] ?? null);
236 }
237 } else {
238 $endChecking('Unsupported authentication method');
239 }
240 }
241 } catch (SoftException $e) {
242 $endChecking($e->getMessage());
243 }
244 }
245 } catch (RuntimeException $e) {
246 $endChecking($e->getMessage());
247 }
248
249 echo PHP_EOL, '=== DIRECTORIES ===', PHP_EOL;
250 $startChecking('Checking directories config');
251 try {
252 $dir_list = (new DirectoryList())->list();
253 $dir_lcnt = count($dir_list);
254 if ($dir_lcnt === 0) {
255 $endChecking('No directories found', RESULT_SUCCESS);
256 } else {
257 $endChecking("{$dir_lcnt} director" . ($dir_lcnt > 1 ? 'ies' : 'y') . ' found', RESULT_SUCCESS);
258 echo " * Checking directories ({$dir_lcnt})", PHP_EOL;
259 foreach ($dir_list as $dir) {
260 $dir_a = $dir->toArray();
261 echo " - {$dir_a['name']}", PHP_EOL;
262 $startChecking('Accessibility', 2);
263 $res = $dir->check();
264 if (!$res['error_code']) {
265 $endChecking();
266 $startChecking('Security', 2);
267 try {
268 $perms = fileperms($dir_a['location']);
269 if ($perms === false) {
270 throw new RuntimeException('Fileperms failed', RESULT_ERROR);
271 }
272 if ($perms & 0x2) {
273 throw new RuntimeException(
274 'Other users have write access to the directory',
275 RESULT_WARNING
276 );
277 }
278 $endChecking();
279 } catch (RuntimeException $e) {
280 $endChecking($e->getMessage(), $e->getCode());
281 } catch (\ErrorException $e) {
282 $endChecking($e->getMessage());
283 }
284 } else {
285 $endChecking($res['message'] ?? null);
286 }
287 }
288 }
289 } catch (SoftException $e) {
290 $endChecking($e->getMessage());
291 }
292
293 echo PHP_EOL, '=== REMOTE FILESYSTEMS ===', PHP_EOL;
294 $startChecking('Getting configuration');
295 $fs_list = $core->config('remote_filesystems');
296 if (is_array($fs_list) && count($fs_list) > 0) {
297 $endChecking();
298 try {
299 $startChecking('Checking installed packages');
300 try {
301 $core->checkDependencies('flyfs');
302 } catch (SoftException $e) {
303 throw new SoftException('Flysystem package is not installed');
304 }
305 $endChecking();
306 $startChecking('Analizing configuration data');
307 try {
308 $fs_list = (new RemoteFilesystemList(false))->list();
309 $endChecking();
310 } catch (LogicException $e) {
311 $endChecking($e->getMessage());
312 $fs_list = [];
313 }
314 $fs_lcnt = count($fs_list);
315 if ($fs_lcnt > 0) {
316 echo " * Checking remote filesystems ({$fs_lcnt})", PHP_EOL;
317 foreach ($fs_list as $fs) {
318 $fs_a = $fs->toArray();
319 echo " - {$fs_a['name']}", PHP_EOL;
320 $startChecking('Accessibility', 2);
321 $res = $fs->check();
322 if (!$res['error_code']) {
323 $endChecking();
324 //$startChecking('Security', 2);
325 //try {
326 // if ($fs->isPublic()) {
327 // throw new RuntimeException('Anyone can access the files on this filesystem');
328 // }
329 // $endChecking();
330 //} catch (RuntimeException $e) {
331 // $endChecking($e->getMessage(), RESULT_WARNING);
332 //} catch (\ErrorException $e) {
333 // $endChecking($e->getMessage());
334 //}
335 } else {
336 $endChecking($res['message'] ?? null);
337 }
338 }
339 }
340 } catch (SoftException $e) {
341 $endChecking($e->getMessage());
342 }
343 } else {
344 $endChecking('Configuration not found', RESULT_SKIPPED);
345 }
346
347 echo PHP_EOL, '=== REPORT MAILER ===', PHP_EOL;
348 $startChecking('Getting configuration');
349 $mailer = $core->config('mailer');
350 if (is_array($mailer)) {
351 $endChecking();
352 try {
353 $method = $core->config('mailer/method', 'mail');
354 $library = $core->config('mailer/library', 'internal');
355 $startChecking('Checking mailer/method');
356 $endChecking(in_array($method, [ 'smtp', 'mail' ]) ? '' : "Unknown mailing method: '{$method}'");
357 switch ($method) {
358 case 'smtp':
359 $startChecking('Checking mailer/library');
360 $endChecking(
361 $library === 'phpmailer' ?
362 '' :
363 ('The smtp method requires the phpmailer library, but ' .
364 (empty($library) ? 'none' : $library) . ' is specified')
365 );
366 $startChecking('Checking mailer/host');
367 $endChecking(empty($core->config('mailer/host')) ? 'Host must be specified' : '');
368 $startChecking('Checking mailer/port');
369 $endChecking(!is_int($core->config('mailer/port')) ? 'Port must be specified' : '');
370 $startChecking('Checking mailer/encryption');
371 $enc = $core->config('mailer/encryption');
372 $endChecking(
373 in_array($enc, [ 'ssl', 'starttls', 'none', null ]) ?
374 '' :
375 ('Unknown encryption method: ' . $enc)
376 );
377 break;
378 case 'mail':
379 break;
380 }
381 $startChecking('Checking mailer/library');
382 switch ($library) {
383 case 'phpmailer':
384 if (!class_exists('\PHPMailer\PHPMailer\PHPMailer')) {
385 throw new SoftException('The smtp method is used but the PHPMailer library is not installed');
386 }
387 break;
388 case 'internal':
389 break;
390 default:
391 throw new SoftException("Unsupported mailing library: '{$library}'");
392 }
393 $endChecking();
394 $startChecking('Checking mailer/default');
395 if (!filter_var($core->config('mailer/default', ''), FILTER_VALIDATE_EMAIL)) {
396 throw new SoftException('Invalid e-mail address');
397 }
398 $endChecking();
399 $startChecking('Checking mailer/from');
400 if (!filter_var($core->config('mailer/from', ''), FILTER_VALIDATE_EMAIL)) {
401 throw new SoftException('Invalid e-mail address');
402 }
403 $endChecking();
404 //
405 } catch (SoftException $e) {
406 $endChecking($e->getMessage());
407 }
408 } else {
409 $endChecking('Configuration not found', RESULT_SKIPPED);
410 }
411
412 echo PHP_EOL, '===', PHP_EOL;
413 if ($e_cnt === 0 && $w_cnt === 0) {
414 echo 'Success!', PHP_EOL;
415 } else {
416 echo 'There ', $e_cnt + $w_cnt === 1 ? 'is' : 'are', ' ';
417 $strs = [];
418 foreach ([ [$e_cnt, 'error' ], [ $w_cnt, 'warning' ] ] as &$it) {
419 $cnt = $it[0];
420 if ($cnt > 0) {
421 $strs[] = $cnt . ' ' . $it[1] . ($cnt > 1 ? 's' : '');
422 }
423 }
424 unset($it);
425 echo implode(' and ', $strs), '!', PHP_EOL;
426 }
427 } catch (SoftException $e) {
428 echo 'Fatal error: ', $e->getMessage(), PHP_EOL;
429 exit(1);
430 }
431
432 if ($e_cnt > 0) {
433 exit(2);
434 }
435 exit(0);