Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to configure php.user #45307

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,15 @@
*/
'config_is_read_only' => false,

/**
* In certain environments it is desired to set the config.php owner to
* something else than the user that is running the php process.
* In that case in order to determine the user that the php process uses,
* you can overwrite the user with this config flag for console.php and cron.php
* Defaults to ``''`` (empty string)
*/
'php.user' => '',

/**
* Logging
*/
Expand Down
9 changes: 8 additions & 1 deletion console.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ function exceptionHandler($exception) {
}

$user = posix_getuid();
$userNameArray = posix_getpwuid($user);
$username = null;
if ($userNameArray !== false) {
$userName = $userNameArray['name'];
}
$configUser = fileowner(OC::$configDir . 'config.php');
if ($user !== $configUser) {
$configuredUser = $config->getSystemValueString('php.user', '');
if ($user !== $configUser && $username !== null && $userName !== $configuredUser) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check always needs to check for $configuredUser if set. Also accepting $configUser can lead to the very issue this is trying to prevent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like

$phpUser = $config->getSystemValueString('php.user', '');
if (!$phpUser) {
    $userNameArray = posix_getpwuid($user);
	if ($userNameArray !== false) {
		$phpUser = $userNameArray['name'];
	}
}
if ($user != $phpUser) {

maybe

echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL;
echo "Current user id: " . $user . PHP_EOL;
echo "Owner id of config.php: " . $configUser . PHP_EOL;
echo "Try adding 'sudo -u #" . $configUser . "' to the beginning of the command (without the single quotes)" . PHP_EOL;
echo "If running with 'docker exec' try adding the option '-u " . $configUser . "' to the docker command (without the single quotes)" . PHP_EOL;
echo "Another option is to configure 'php.user' in config.php which will overwrite this check.";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think as worded this is misleading, the option doesn't overwrite the check, it changes that the expected value of the check is.

I would go with something like

If the config file is not owned by the user running the webserver you can set the correct user by setting the 'php.user' option in your config.php

exit(1);
}

Expand Down
9 changes: 8 additions & 1 deletion cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,18 @@
}

$user = posix_getuid();
$userNameArray = posix_getpwuid($user);
$username = null;
if ($userNameArray !== false) {
$userName = $userNameArray['name'];
}
$configUser = fileowner(OC::$configDir . 'config.php');
if ($user !== $configUser) {
$configuredUser = $config->getSystemValueString('php.user', '');
if ($user !== $configUser && $username !== null && $userName !== $configuredUser) {

Check failure on line 140 in cron.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

TypeDoesNotContainType

cron.php:140:7: TypeDoesNotContainType: Type null for $username is always !null (see https://psalm.dev/056)

Check failure

Code scanning / Psalm

TypeDoesNotContainType Error

Type null for $username is always !null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is not true? How can I fix this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? Documentation says it should return false on failure: https://www.php.net/manual/en/function.posix-getpwuid.php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

echo "Console has to be executed with the user that owns the file config/config.php" . PHP_EOL;
echo "Current user id: " . $user . PHP_EOL;
echo "Owner id of config.php: " . $configUser . PHP_EOL;
echo "Another option is to configure 'php.user' in config.php which will overwrite this check.";
exit(1);
}

Expand Down
Loading