Skip to content

Commit ce68575

Browse files
committed
fixup! use setuid and setgid
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 1dc369f commit ce68575

1 file changed

Lines changed: 41 additions & 11 deletions

File tree

occ

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,53 @@
22
<?php
33

44
/**
5+
* SPDX-FileCopyrightText: 2013 Thomas Müller <thomas.mueller@tmit.eu>
56
* SPDX-FileCopyrightText: 2014 ownCloud, Inc.
67
* SPDX-FileCopyrightText: 2014 Olivier Paroz
7-
* SPDX-FileCopyrightText: 2013 Thomas Müller <thomas.mueller@tmit.eu>
8+
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH
89
* SPDX-License-Identifier: AGPL-3.0-only
910
*/
1011

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+
1142
// Drop privileges when run as root
12-
if (posix_getuid() === 0){
43+
if (posix_getuid() === 0) {
1344
$configPath = __DIR__ . '/config/config.php';
1445
$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+
}
2452
}
53+
54+
require_once __DIR__ . '/console.php';

0 commit comments

Comments
 (0)