2017-10-26 01:56:09 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* User.php
|
|
|
|
*
|
|
|
|
* Check that user is set properly and we are running as the correct user. Check that user is the owner of install_dir.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-10-26 01:56:09 -05:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2017-10-26 01:56:09 -05:00
|
|
|
* @copyright 2017 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace LibreNMS\Validations;
|
|
|
|
|
2021-05-13 07:18:54 -05:00
|
|
|
use Illuminate\Support\Facades\App;
|
2020-04-17 17:37:56 -05:00
|
|
|
use Illuminate\Support\Str;
|
2017-10-26 01:56:09 -05:00
|
|
|
use LibreNMS\Config;
|
2020-07-09 15:03:12 -05:00
|
|
|
use LibreNMS\Util\EnvHelper;
|
2019-01-08 21:42:56 -06:00
|
|
|
use LibreNMS\Util\Git;
|
2017-10-26 01:56:09 -05:00
|
|
|
use LibreNMS\ValidationResult;
|
|
|
|
use LibreNMS\Validator;
|
|
|
|
|
2018-02-27 09:57:20 -06:00
|
|
|
class User extends BaseValidation
|
2017-10-26 01:56:09 -05:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Validate this module.
|
|
|
|
* To return ValidationResults, call ok, warn, fail, or result methods on the $validator
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param Validator $validator
|
2017-10-26 01:56:09 -05:00
|
|
|
*/
|
2022-06-10 16:25:33 -05:00
|
|
|
public function validate(Validator $validator): void
|
2017-10-26 01:56:09 -05:00
|
|
|
{
|
|
|
|
// Check we are running this as the root user
|
|
|
|
$username = $validator->getUsername();
|
2020-06-27 22:24:54 -05:00
|
|
|
$lnms_username = \config('librenms.user');
|
|
|
|
$lnms_groupname = \config('librenms.group');
|
2017-10-26 01:56:09 -05:00
|
|
|
|
|
|
|
if (! ($username === 'root' || $username === $lnms_username)) {
|
2021-05-13 07:18:54 -05:00
|
|
|
if (App::runningInConsole()) {
|
2021-04-30 05:44:31 +02:00
|
|
|
$validator->fail("You need to run this script as '$lnms_username' or root");
|
2017-11-10 09:20:47 -06:00
|
|
|
} elseif (function_exists('posix_getgrnam')) {
|
2017-10-26 01:56:09 -05:00
|
|
|
$lnms_group = posix_getgrnam($lnms_groupname);
|
2021-04-30 05:44:31 +02:00
|
|
|
|
|
|
|
if ($lnms_group === false) {
|
|
|
|
$validator->fail(
|
|
|
|
"The group '$lnms_groupname' does not exist",
|
|
|
|
"groupadd $lnms_groupname"
|
|
|
|
);
|
|
|
|
} elseif (! in_array($username, $lnms_group['members'])) {
|
2017-10-26 01:56:09 -05:00
|
|
|
$validator->fail(
|
2021-04-30 05:44:31 +02:00
|
|
|
"Your web server or php-fpm is not running as user '$lnms_username' or in the group '$lnms_groupname'",
|
2017-10-26 01:56:09 -05:00
|
|
|
"usermod -a -G $lnms_groupname $username"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:05:34 +01:00
|
|
|
// skip if docker image
|
2020-07-09 15:03:12 -05:00
|
|
|
if (EnvHelper::librenmsDocker()) {
|
2019-11-25 15:05:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-07 16:29:37 -06:00
|
|
|
// if no git, then we probably have different permissions by design
|
2022-10-02 00:41:56 -05:00
|
|
|
if (! Git::make()->repoPresent()) {
|
2018-12-07 16:29:37 -06:00
|
|
|
return;
|
|
|
|
}
|
2017-10-26 01:56:09 -05:00
|
|
|
|
|
|
|
// Let's test the user configured if we have it
|
2019-05-22 13:31:24 -05:00
|
|
|
if ($lnms_username) {
|
2017-10-26 01:56:09 -05:00
|
|
|
$dir = Config::get('install_dir');
|
2018-10-09 06:21:34 -05:00
|
|
|
$log_dir = Config::get('log_dir', "$dir/logs");
|
|
|
|
$rrd_dir = Config::get('rrd_dir', "$dir/rrd");
|
2018-09-18 14:58:47 -05:00
|
|
|
|
|
|
|
// generic fix
|
2018-10-18 21:08:46 -05:00
|
|
|
$fix = [
|
|
|
|
"sudo chown -R $lnms_username:$lnms_groupname $dir",
|
|
|
|
"sudo setfacl -d -m g::rwx $rrd_dir $log_dir $dir/bootstrap/cache/ $dir/storage/",
|
|
|
|
"sudo chmod -R ug=rwX $rrd_dir $log_dir $dir/bootstrap/cache/ $dir/storage/",
|
|
|
|
];
|
2018-09-18 14:58:47 -05:00
|
|
|
|
2021-03-26 23:07:33 -04:00
|
|
|
if (! Config::get('installed_from_package')) {
|
|
|
|
$find_result = rtrim(`find $dir \! -user $lnms_username -o \! -group $lnms_groupname 2> /dev/null`);
|
|
|
|
if (! empty($find_result)) {
|
|
|
|
// Ignore files created by the webserver
|
|
|
|
$ignore_files = [
|
|
|
|
"$log_dir/error_log",
|
|
|
|
"$log_dir/access_log",
|
|
|
|
"$dir/bootstrap/cache/",
|
|
|
|
"$dir/storage/framework/cache/",
|
|
|
|
"$dir/storage/framework/sessions/",
|
|
|
|
"$dir/storage/framework/views/",
|
|
|
|
"$dir/storage/debugbar/",
|
|
|
|
"$dir/.pki/", // ignore files/folders created by setting the librenms home directory to the install directory
|
|
|
|
];
|
2018-05-11 14:25:29 -05:00
|
|
|
|
2021-03-26 23:07:33 -04:00
|
|
|
$files = array_filter(explode(PHP_EOL, $find_result), function ($file) use ($ignore_files) {
|
|
|
|
if (Str::startsWith($file, $ignore_files)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-11 14:25:29 -05:00
|
|
|
|
2021-03-26 23:07:33 -04:00
|
|
|
return true;
|
|
|
|
});
|
2017-10-26 01:56:09 -05:00
|
|
|
|
2021-03-26 23:07:33 -04:00
|
|
|
if (! empty($files)) {
|
|
|
|
$result = ValidationResult::fail(
|
2021-04-30 05:44:31 +02:00
|
|
|
"We have found some files that are owned by a different user than '$lnms_username', this " .
|
2021-03-26 23:07:33 -04:00
|
|
|
'will stop you updating automatically and / or rrd files being updated causing graphs to fail.'
|
|
|
|
)
|
|
|
|
->setFix($fix)
|
|
|
|
->setList('Files', $files);
|
2017-10-26 01:56:09 -05:00
|
|
|
|
2021-03-26 23:07:33 -04:00
|
|
|
$validator->result($result);
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2021-03-26 23:07:33 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-10-26 01:56:09 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-18 14:58:47 -05:00
|
|
|
} else {
|
2021-04-30 05:44:31 +02:00
|
|
|
$validator->warn("You don't have LIBRENMS_USER set, this most likely needs to be set to 'librenms'");
|
2017-10-26 01:56:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|