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/>.
18 #define UID_WRAPPER_NOT_REPLACE
20 #include "system/passwd.h"
21 #include "system/filesys.h"
28 we keep the virtualised euid/egid/groups information here
39 static void uwrap_init(void)
41 if (uwrap.initialised) return;
42 uwrap.initialised = true;
43 if (getenv("UID_WRAPPER")) {
45 /* put us in one group */
47 uwrap.groups = talloc_array(talloc_autofree_context(), gid_t, 1);
53 _PUBLIC_ int uwrap_enabled(void)
56 return uwrap.enabled?1:0;
59 _PUBLIC_ int uwrap_seteuid(uid_t euid)
65 /* assume for now that the ruid stays as root */
70 _PUBLIC_ uid_t uwrap_geteuid(void)
79 _PUBLIC_ int uwrap_setegid(gid_t egid)
85 /* assume for now that the ruid stays as root */
90 _PUBLIC_ uid_t uwrap_getegid(void)
99 _PUBLIC_ int uwrap_setgroups(size_t size, const gid_t *list)
102 if (!uwrap.enabled) {
103 return setgroups(size, list);
106 talloc_free(uwrap.groups);
111 uwrap.groups = talloc_array(talloc_autofree_context(), gid_t, size);
112 if (uwrap.groups == NULL) {
116 memcpy(uwrap.groups, list, size*sizeof(gid_t));
117 uwrap.ngroups = size;
122 _PUBLIC_ int uwrap_getgroups(int size, gid_t *list)
125 if (!uwrap.enabled) {
126 return getgroups(size, list);
129 if (size > uwrap.ngroups) {
130 size = uwrap.ngroups;
133 return uwrap.ngroups;
135 if (size < uwrap.ngroups) {
139 memcpy(list, uwrap.groups, size*sizeof(gid_t));
140 return uwrap.ngroups;
143 _PUBLIC_ uid_t uwrap_getuid(void)
146 if (!uwrap.enabled) {
149 /* we don't simulate ruid changing */
153 _PUBLIC_ gid_t uwrap_getgid(void)
156 if (!uwrap.enabled) {
159 /* we don't simulate rgid changing */