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_
int uwrap_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
)
91 return setresuid(ruid
, euid
, suid
);
93 /* assume for now that the ruid stays as root */
98 _PUBLIC_ uid_t
uwrap_geteuid(void)
101 if (!uwrap
.enabled
) {
107 _PUBLIC_
int uwrap_setegid(gid_t egid
)
110 if (!uwrap
.enabled
) {
111 return setegid(egid
);
113 /* assume for now that the ruid stays as root */
118 _PUBLIC_
int uwrap_setregid(gid_t rgid
, gid_t egid
)
121 if (!uwrap
.enabled
) {
122 return setregid(rgid
, egid
);
124 /* assume for now that the ruid stays as root */
129 _PUBLIC_ uid_t
uwrap_getegid(void)
132 if (!uwrap
.enabled
) {
138 _PUBLIC_
int uwrap_setgroups(size_t size
, const gid_t
*list
)
141 if (!uwrap
.enabled
) {
142 return setgroups(size
, list
);
145 talloc_free(uwrap
.groups
);
149 uwrap
.groups
= talloc_array(NULL
, gid_t
, size
);
150 if (uwrap
.groups
== NULL
) {
154 memcpy(uwrap
.groups
, list
, size
*sizeof(gid_t
));
159 _PUBLIC_
int uwrap_getgroups(int size
, gid_t
*list
)
164 if (!uwrap
.enabled
) {
165 return getgroups(size
, list
);
168 ngroups
= talloc_array_length(uwrap
.groups
);
170 if (size
> ngroups
) {
176 if (size
< ngroups
) {
180 memcpy(list
, uwrap
.groups
, size
*sizeof(gid_t
));
184 _PUBLIC_ uid_t
uwrap_getuid(void)
187 if (!uwrap
.enabled
) {
190 /* we don't simulate ruid changing */
194 _PUBLIC_ gid_t
uwrap_getgid(void)
197 if (!uwrap
.enabled
) {
200 /* we don't simulate rgid changing */