|
2 | 2 | <?php |
3 | 3 |
|
4 | 4 | /** |
| 5 | + * SPDX-FileCopyrightText: 2013 Thomas Müller <thomas.mueller@tmit.eu> |
5 | 6 | * SPDX-FileCopyrightText: 2014 ownCloud, Inc. |
6 | 7 | * SPDX-FileCopyrightText: 2014 Olivier Paroz |
7 | | - * SPDX-FileCopyrightText: 2013 Thomas Müller <thomas.mueller@tmit.eu> |
| 8 | + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH |
8 | 9 | * SPDX-License-Identifier: AGPL-3.0-only |
9 | 10 | */ |
10 | 11 |
|
| 12 | +/** |
| 13 | + * Get the UID and GID of a user name |
| 14 | + * |
| 15 | + * @return list{int,int}|false |
| 16 | + */ |
| 17 | +function getIdsByName(string $name): array|false { |
| 18 | + $info = posix_getpwnam($name); |
| 19 | + if ($info !== false) { |
| 20 | + return [$info['uid'], $info['gid']]; |
| 21 | + } |
| 22 | + return false; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Get the UID and GID of the fileowner of a file. |
| 27 | + * |
| 28 | + * @return list{int,int}|false |
| 29 | + */ |
| 30 | +function getIdsByFile(string $path): array|false { |
| 31 | + $uid = fileowner($path); |
| 32 | + if ($uid === false) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + $info = posix_getpwuid($uid); |
| 36 | + if ($info === false) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + return [$uid, $info['gid']]; |
| 40 | +} |
| 41 | + |
11 | 42 | // Drop privileges when run as root |
12 | | -if (posix_getuid() === 0){ |
| 43 | +if (posix_getuid() === 0) { |
13 | 44 | $configPath = __DIR__ . '/config/config.php'; |
14 | 45 | $fallbackUser = 'www-data'; |
15 | | - $guessedUser = match (file_exists($configPath)) { |
16 | | - true => ($ownerUid = fileowner($configPath)) ? posix_getpwuid($ownerUid)['name'] : $fallbackUser, |
17 | | - false => $fallbackUser, |
18 | | - }; |
19 | | - $command = implode (' ', $argv); |
20 | | - echo(shell_exec("sudo -u $guessedUser php -f " . $command)); |
21 | | - exit; |
22 | | -} else { |
23 | | - require_once __DIR__ . '/console.php'; |
| 46 | + |
| 47 | + $info = getIdsByFile($configPath) ?: getIdsByName($fallbackUser); |
| 48 | + if ($info !== false) { |
| 49 | + posix_setuid($info[0]); |
| 50 | + posix_setgid($info[1]); |
| 51 | + } |
24 | 52 | } |
| 53 | + |
| 54 | +require_once __DIR__ . '/console.php'; |
0 commit comments