2 Copyright (C) Andrew Tridgell 2009
3 Copyright (c) 2011 Andreas Schneider <asn@samba.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #define UID_WRAPPER_NOT_REPLACE
23 #include "system/passwd.h"
25 #include "../lib/util/setid.h"
27 #else /* _SAMBA_BUILD_ */
29 #error uid_wrapper_only_supported_in_samba_yet
38 we keep the virtualised euid/egid/groups information here
50 static void uwrap_init(void)
52 if (uwrap
.initialised
) return;
53 uwrap
.initialised
= true;
54 if (getenv("UID_WRAPPER")) {
56 /* put us in one group */
57 uwrap
.myuid
= uwrap
.euid
= geteuid();
58 uwrap
.mygid
= uwrap
.egid
= getegid();
59 uwrap
.groups
= talloc_array(NULL
, gid_t
, 1);
65 _PUBLIC_
int uwrap_enabled(void)
68 return uwrap
.enabled
?1:0;
72 _PUBLIC_
int uwrap_seteuid(uid_t euid
)
76 return samba_seteuid(euid
);
78 /* assume for now that the ruid stays as root */
80 uwrap
.euid
= uwrap
.myuid
;
89 _PUBLIC_
int uwrap_setreuid(uid_t ruid
, uid_t euid
)
93 return samba_setreuid(ruid
, euid
);
95 /* assume for now that the ruid stays as root */
97 uwrap
.euid
= uwrap
.myuid
;
105 #ifdef HAVE_SETRESUID
106 _PUBLIC_
int uwrap_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
)
109 if (!uwrap
.enabled
) {
110 return samba_setresuid(ruid
, euid
, suid
);
112 /* assume for now that the ruid stays as root */
114 uwrap
.euid
= uwrap
.myuid
;
122 _PUBLIC_ uid_t
uwrap_geteuid(void)
125 if (!uwrap
.enabled
) {
132 _PUBLIC_
int uwrap_setegid(gid_t egid
)
135 if (!uwrap
.enabled
) {
136 return samba_setegid(egid
);
138 /* assume for now that the ruid stays as root */
140 uwrap
.egid
= uwrap
.mygid
;
149 _PUBLIC_
int uwrap_setregid(gid_t rgid
, gid_t egid
)
152 if (!uwrap
.enabled
) {
153 return samba_setregid(rgid
, egid
);
155 /* assume for now that the ruid stays as root */
157 uwrap
.egid
= uwrap
.mygid
;
165 #ifdef HAVE_SETRESGID
166 _PUBLIC_
int uwrap_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
)
169 if (!uwrap
.enabled
) {
170 return samba_setresgid(rgid
, egid
, sgid
);
172 /* assume for now that the ruid stays as root */
174 uwrap
.egid
= uwrap
.mygid
;
182 _PUBLIC_ uid_t
uwrap_getegid(void)
185 if (!uwrap
.enabled
) {
191 _PUBLIC_
int uwrap_setgroups(size_t size
, const gid_t
*list
)
194 if (!uwrap
.enabled
) {
195 return samba_setgroups(size
, list
);
198 talloc_free(uwrap
.groups
);
202 uwrap
.groups
= talloc_array(NULL
, gid_t
, size
);
203 if (uwrap
.groups
== NULL
) {
207 memcpy(uwrap
.groups
, list
, size
*sizeof(gid_t
));
212 _PUBLIC_
int uwrap_getgroups(int size
, gid_t
*list
)
217 if (!uwrap
.enabled
) {
218 return getgroups(size
, list
);
221 ngroups
= talloc_array_length(uwrap
.groups
);
223 if (size
> ngroups
) {
229 if (size
< ngroups
) {
233 memcpy(list
, uwrap
.groups
, size
*sizeof(gid_t
));
237 _PUBLIC_ uid_t
uwrap_getuid(void)
240 if (!uwrap
.enabled
) {
243 /* we don't simulate ruid changing */
247 _PUBLIC_ gid_t
uwrap_getgid(void)
250 if (!uwrap
.enabled
) {
253 /* we don't simulate rgid changing */