2 Copyright (C) Andrew Tridgell 2009
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #define UID_WRAPPER_NOT_REPLACE
21 #include "../replace/replace.h"
23 #include "system/passwd.h"
25 #else /* _SAMBA_BUILD_ */
27 #error uid_wrapper_only_supported_in_samba_yet
36 we keep the virtualised euid/egid/groups information here
46 static void uwrap_init(void)
48 if (uwrap.initialised) return;
49 uwrap.initialised = true;
50 if (getenv("UID_WRAPPER")) {
52 /* put us in one group */
53 uwrap.groups = talloc_array(NULL, gid_t, 1);
59 _PUBLIC_ int uwrap_enabled(void)
62 return uwrap.enabled?1:0;
65 _PUBLIC_ int uwrap_seteuid(uid_t euid)
71 /* assume for now that the ruid stays as root */
76 _PUBLIC_ int uwrap_setreuid(uid_t ruid, uid_t euid)
80 return setreuid(ruid, euid);
82 /* assume for now that the ruid stays as root */
87 _PUBLIC_ uid_t uwrap_geteuid(void)
96 _PUBLIC_ int uwrap_setegid(gid_t egid)
100 return setegid(egid);
102 /* assume for now that the ruid stays as root */
107 _PUBLIC_ uid_t uwrap_getegid(void)
110 if (!uwrap.enabled) {
116 _PUBLIC_ int uwrap_setgroups(size_t size, const gid_t *list)
119 if (!uwrap.enabled) {
120 return setgroups(size, list);
123 talloc_free(uwrap.groups);
127 uwrap.groups = talloc_array(NULL, gid_t, size);
128 if (uwrap.groups == NULL) {
132 memcpy(uwrap.groups, list, size*sizeof(gid_t));
137 _PUBLIC_ int uwrap_getgroups(int size, gid_t *list)
142 if (!uwrap.enabled) {
143 return getgroups(size, list);
146 ngroups = talloc_array_length(uwrap.groups);
148 if (size > ngroups) {
154 if (size < ngroups) {
158 memcpy(list, uwrap.groups, size*sizeof(gid_t));
162 _PUBLIC_ uid_t uwrap_getuid(void)
165 if (!uwrap.enabled) {
168 /* we don't simulate ruid changing */
172 _PUBLIC_ gid_t uwrap_getgid(void)
175 if (!uwrap.enabled) {
178 /* we don't simulate rgid changing */