]>
Commit | Line | Data |
---|---|---|
f6903667 AK |
1 | /* |
2 | * nonlocal-passwd.c | |
3 | * passwd database for nss_nonlocal proxy. | |
4 | * | |
96a1ee0f AK |
5 | * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu> and Tim |
6 | * Abbott <tabbott@mit.edu> | |
f6903667 | 7 | * |
96a1ee0f | 8 | * This file is part of nss_nonlocal. |
f6903667 | 9 | * |
96a1ee0f AK |
10 | * nss_nonlocal is free software; you can redistribute it and/or |
11 | * modify it under the terms of the GNU Lesser General Public License | |
12 | * as published by the Free Software Foundation; either version 2.1 of | |
13 | * the License, or (at your option) any later version. | |
f6903667 | 14 | * |
96a1ee0f AK |
15 | * nss_nonlocal is distributed in the hope that it will be useful, but |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * Lesser General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU Lesser General Public | |
21 | * License along with nss_nonlocal; if not, write to the Free Software | |
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
23 | * 02110-1301 USA | |
f6903667 AK |
24 | */ |
25 | ||
26 | ||
27 | #define _GNU_SOURCE | |
dc397f8f | 28 | |
f6903667 | 29 | #include <sys/types.h> |
f6903667 | 30 | #include <dlfcn.h> |
f6903667 | 31 | #include <errno.h> |
f6903667 | 32 | #include <nss.h> |
dc397f8f AK |
33 | #include <pwd.h> |
34 | #include <stdbool.h> | |
35 | #include <stddef.h> | |
36 | #include <stdlib.h> | |
37 | #include <string.h> | |
38 | #include <syslog.h> | |
39 | #include <unistd.h> | |
40 | ||
f6903667 AK |
41 | #include "nsswitch-internal.h" |
42 | #include "nonlocal.h" | |
43 | ||
c1812233 AK |
44 | |
45 | enum nss_status | |
46 | _nss_nonlocal_getpwuid_r(uid_t uid, struct passwd *pwd, | |
47 | char *buffer, size_t buflen, int *errnop); | |
48 | enum nss_status | |
49 | _nss_nonlocal_getpwnam_r(const char *name, struct passwd *pwd, | |
50 | char *buffer, size_t buflen, int *errnop); | |
f7293a54 | 51 | |
f6903667 | 52 | |
cbb0e3ea AK |
53 | static service_user *__nss_passwd_nonlocal_database; |
54 | ||
55 | static int | |
56 | internal_function | |
d52c3f35 AK |
57 | __nss_passwd_nonlocal_lookup2(service_user **ni, const char *fct_name, |
58 | const char *fct2_name, void **fctp) | |
f6903667 | 59 | { |
cbb0e3ea AK |
60 | if (__nss_passwd_nonlocal_database == NULL |
61 | && __nss_database_lookup("passwd_nonlocal", NULL, NULL, | |
62 | &__nss_passwd_nonlocal_database) < 0) | |
63 | return -1; | |
64 | ||
65 | *ni = __nss_passwd_nonlocal_database; | |
f6903667 | 66 | |
cbb0e3ea | 67 | *fctp = __nss_lookup_function(*ni, fct_name); |
d52c3f35 AK |
68 | if (*fctp == NULL && fct2_name != NULL) |
69 | *fctp = __nss_lookup_function(*ni, fct2_name); | |
cbb0e3ea | 70 | return 0; |
f6903667 AK |
71 | } |
72 | ||
73 | ||
f6903667 AK |
74 | enum nss_status |
75 | check_nonlocal_uid(const char *user, uid_t uid, int *errnop) | |
76 | { | |
30fb3957 | 77 | enum nss_status status; |
f7293a54 | 78 | struct passwd pwbuf; |
cbb0e3ea | 79 | char *buf; |
8870ee9c | 80 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
cbb0e3ea | 81 | const struct walk_nss w = { |
d52c3f35 | 82 | .lookup2 = &__nss_passwd_lookup2, .fct_name = "getpwuid_r", |
cbb0e3ea AK |
83 | .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen |
84 | }; | |
85 | const __typeof__(&_nss_nonlocal_getpwuid_r) self = &_nss_nonlocal_getpwuid_r; | |
86 | #define args (uid, &pwbuf, buf, buflen, errnop) | |
87 | #include "walk_nss.h" | |
88 | #undef args | |
30fb3957 AK |
89 | |
90 | if (status == NSS_STATUS_SUCCESS) { | |
f7293a54 | 91 | syslog(LOG_ERR, "nss_nonlocal: possible spoofing attack: non-local user %s has same UID as local user %s!\n", user, pwbuf.pw_name); |
cbb0e3ea | 92 | free(buf); |
f6903667 | 93 | status = NSS_STATUS_NOTFOUND; |
30fb3957 AK |
94 | } else if (status != NSS_STATUS_TRYAGAIN) { |
95 | status = NSS_STATUS_SUCCESS; | |
f6903667 | 96 | } |
30fb3957 | 97 | |
f6903667 AK |
98 | return status; |
99 | } | |
100 | ||
cf338316 AK |
101 | enum nss_status |
102 | check_nonlocal_passwd(const char *user, struct passwd *pwd, int *errnop) | |
103 | { | |
a07a7616 AK |
104 | enum nss_status status = NSS_STATUS_SUCCESS; |
105 | int old_errno = errno; | |
106 | char *end; | |
107 | unsigned long uid; | |
108 | ||
109 | errno = 0; | |
110 | uid = strtoul(pwd->pw_name, &end, 10); | |
f4061d47 AK |
111 | if (errno == 0 && *end == '\0' && (uid_t)uid == uid) { |
112 | errno = old_errno; | |
a07a7616 | 113 | status = check_nonlocal_uid(user, uid, errnop); |
f4061d47 AK |
114 | } else { |
115 | errno = old_errno; | |
116 | } | |
a07a7616 AK |
117 | if (status != NSS_STATUS_SUCCESS) |
118 | return status; | |
119 | ||
cf338316 AK |
120 | return check_nonlocal_uid(user, pwd->pw_uid, errnop); |
121 | } | |
122 | ||
48479fc7 TA |
123 | enum nss_status |
124 | check_nonlocal_user(const char *user, int *errnop) | |
125 | { | |
30fb3957 | 126 | enum nss_status status; |
48479fc7 | 127 | struct passwd pwbuf; |
cbb0e3ea | 128 | char *buf; |
8870ee9c | 129 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
cbb0e3ea | 130 | const struct walk_nss w = { |
d52c3f35 | 131 | .lookup2 = __nss_passwd_lookup2, .fct_name = "getpwnam_r", |
cbb0e3ea AK |
132 | .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen |
133 | }; | |
134 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = &_nss_nonlocal_getpwnam_r; | |
135 | #define args (user, &pwbuf, buf, buflen, errnop) | |
136 | #include "walk_nss.h" | |
137 | #undef args | |
30fb3957 | 138 | |
cbb0e3ea | 139 | if (status == NSS_STATUS_SUCCESS) { |
30fb3957 | 140 | free(buf); |
30fb3957 | 141 | status = NSS_STATUS_NOTFOUND; |
cbb0e3ea | 142 | } else if (status != NSS_STATUS_TRYAGAIN) { |
30fb3957 | 143 | status = NSS_STATUS_SUCCESS; |
cbb0e3ea | 144 | } |
30fb3957 | 145 | |
48479fc7 TA |
146 | return status; |
147 | } | |
f6903667 | 148 | |
775f7dc3 AK |
149 | enum nss_status |
150 | get_nonlocal_passwd(const char *name, struct passwd *pwd, char **buffer, | |
151 | int *errnop) | |
152 | { | |
153 | enum nss_status status; | |
154 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
155 | const struct walk_nss w = { | |
d52c3f35 | 156 | .lookup2 = __nss_passwd_nonlocal_lookup2, .fct_name = "getpwnam_r", |
775f7dc3 AK |
157 | .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen |
158 | }; | |
159 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL; | |
160 | #define args (name, pwd, *buffer, buflen, errnop) | |
161 | #include "walk_nss.h" | |
162 | #undef args | |
163 | return status; | |
164 | } | |
165 | ||
1e78305d | 166 | |
48939704 | 167 | static bool pwent_initialized = false; |
cbb0e3ea | 168 | static service_user *pwent_startp, *pwent_nip; |
f6903667 AK |
169 | static void *pwent_fct_start; |
170 | static union { | |
171 | enum nss_status (*l)(struct passwd *pwd, char *buffer, size_t buflen, | |
172 | int *errnop); | |
173 | void *ptr; | |
174 | } pwent_fct; | |
175 | static const char *pwent_fct_name = "getpwent_r"; | |
176 | ||
177 | enum nss_status | |
178 | _nss_nonlocal_setpwent(int stayopen) | |
179 | { | |
f6903667 | 180 | enum nss_status status; |
cbb0e3ea | 181 | const struct walk_nss w = { |
d52c3f35 | 182 | .lookup2 = &__nss_passwd_nonlocal_lookup2, .fct_name = "setpwent", |
cbb0e3ea AK |
183 | .status = &status |
184 | }; | |
185 | const __typeof__(&_nss_nonlocal_setpwent) self = NULL; | |
186 | #define args (stayopen) | |
187 | #include "walk_nss.h" | |
188 | #undef args | |
f6903667 AK |
189 | if (status != NSS_STATUS_SUCCESS) |
190 | return status; | |
191 | ||
48939704 | 192 | if (!pwent_initialized) { |
d52c3f35 AK |
193 | __nss_passwd_nonlocal_lookup2(&pwent_startp, pwent_fct_name, NULL, |
194 | &pwent_fct_start); | |
48939704 AK |
195 | __sync_synchronize(); |
196 | pwent_initialized = true; | |
197 | } | |
cbb0e3ea | 198 | pwent_nip = pwent_startp; |
f6903667 AK |
199 | pwent_fct.ptr = pwent_fct_start; |
200 | return NSS_STATUS_SUCCESS; | |
201 | } | |
202 | ||
203 | enum nss_status | |
204 | _nss_nonlocal_endpwent(void) | |
205 | { | |
f6903667 | 206 | enum nss_status status; |
cbb0e3ea | 207 | const struct walk_nss w = { |
d52c3f35 | 208 | .lookup2 = &__nss_passwd_nonlocal_lookup2, .fct_name = "endpwent", |
23ca0ded | 209 | .status = &status, .all_values = 1, |
cbb0e3ea AK |
210 | }; |
211 | const __typeof__(&_nss_nonlocal_endpwent) self = NULL; | |
f6903667 AK |
212 | |
213 | pwent_nip = NULL; | |
214 | ||
cbb0e3ea AK |
215 | #define args () |
216 | #include "walk_nss.h" | |
217 | #undef args | |
f6903667 AK |
218 | return status; |
219 | } | |
220 | ||
221 | enum nss_status | |
222 | _nss_nonlocal_getpwent_r(struct passwd *pwd, char *buffer, size_t buflen, | |
223 | int *errnop) | |
224 | { | |
225 | enum nss_status status; | |
a360549e TA |
226 | |
227 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); | |
c1812233 | 228 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
a360549e TA |
229 | return NSS_STATUS_UNAVAIL; |
230 | ||
f6903667 AK |
231 | if (pwent_nip == NULL) { |
232 | status = _nss_nonlocal_setpwent(0); | |
233 | if (status != NSS_STATUS_SUCCESS) | |
234 | return status; | |
235 | } | |
236 | do { | |
237 | if (pwent_fct.ptr == NULL) | |
238 | status = NSS_STATUS_UNAVAIL; | |
239 | else { | |
240 | int nonlocal_errno; | |
241 | do | |
a360549e | 242 | status = DL_CALL_FCT(pwent_fct.l, (pwd, buffer, buflen, errnop)); |
f6903667 | 243 | while (status == NSS_STATUS_SUCCESS && |
cf338316 | 244 | check_nonlocal_passwd(pwd->pw_name, pwd, &nonlocal_errno) != NSS_STATUS_SUCCESS); |
f6903667 AK |
245 | } |
246 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
247 | return status; | |
248 | ||
249 | if (status == NSS_STATUS_SUCCESS) | |
250 | return NSS_STATUS_SUCCESS; | |
d52c3f35 AK |
251 | } while (__nss_next2(&pwent_nip, pwent_fct_name, 0, &pwent_fct.ptr, status, |
252 | 0) == 0); | |
f6903667 AK |
253 | |
254 | pwent_nip = NULL; | |
255 | return NSS_STATUS_NOTFOUND; | |
256 | } | |
257 | ||
258 | ||
259 | enum nss_status | |
260 | _nss_nonlocal_getpwnam_r(const char *name, struct passwd *pwd, | |
261 | char *buffer, size_t buflen, int *errnop) | |
262 | { | |
f6903667 | 263 | enum nss_status status; |
f6903667 | 264 | int group_errno; |
cbb0e3ea | 265 | const struct walk_nss w = { |
d52c3f35 | 266 | .lookup2 = __nss_passwd_nonlocal_lookup2, .fct_name = "getpwnam_r", |
cbb0e3ea AK |
267 | .status = &status, .errnop = errnop |
268 | }; | |
269 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL; | |
f6903667 | 270 | |
a360549e | 271 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
c1812233 | 272 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
48479fc7 TA |
273 | return NSS_STATUS_UNAVAIL; |
274 | ||
cbb0e3ea AK |
275 | #define args (name, pwd, buffer, buflen, errnop) |
276 | #include "walk_nss.h" | |
277 | #undef args | |
f6903667 AK |
278 | if (status != NSS_STATUS_SUCCESS) |
279 | return status; | |
280 | ||
22562df0 AK |
281 | if (strcmp(name, pwd->pw_name) != 0) { |
282 | syslog(LOG_ERR, "nss_nonlocal: discarding user %s from lookup for user %s\n", pwd->pw_name, name); | |
283 | return NSS_STATUS_NOTFOUND; | |
284 | } | |
285 | ||
cf338316 | 286 | status = check_nonlocal_passwd(name, pwd, errnop); |
f6903667 AK |
287 | if (status != NSS_STATUS_SUCCESS) |
288 | return status; | |
289 | ||
25468b96 | 290 | if (check_nonlocal_gid(name, NULL, pwd->pw_gid, &group_errno) != |
f6903667 AK |
291 | NSS_STATUS_SUCCESS) |
292 | pwd->pw_gid = 65534 /* nogroup */; | |
293 | return NSS_STATUS_SUCCESS; | |
294 | } | |
295 | ||
296 | enum nss_status | |
297 | _nss_nonlocal_getpwuid_r(uid_t uid, struct passwd *pwd, | |
298 | char *buffer, size_t buflen, int *errnop) | |
299 | { | |
f6903667 | 300 | enum nss_status status; |
f6903667 | 301 | int group_errno; |
cbb0e3ea | 302 | const struct walk_nss w = { |
d52c3f35 | 303 | .lookup2 = &__nss_passwd_nonlocal_lookup2, .fct_name = "getpwuid_r", |
cbb0e3ea AK |
304 | .status = &status, .errnop = errnop |
305 | }; | |
306 | const __typeof__(&_nss_nonlocal_getpwuid_r) self = NULL; | |
f6903667 | 307 | |
a360549e | 308 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
c1812233 | 309 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
f6903667 AK |
310 | return NSS_STATUS_UNAVAIL; |
311 | ||
cbb0e3ea AK |
312 | #define args (uid, pwd, buffer, buflen, errnop) |
313 | #include "walk_nss.h" | |
314 | #undef args | |
f6903667 AK |
315 | if (status != NSS_STATUS_SUCCESS) |
316 | return status; | |
317 | ||
8027fdc4 AK |
318 | if (uid != pwd->pw_uid) { |
319 | syslog(LOG_ERR, "nss_nonlocal: discarding uid %d from lookup for uid %d\n", pwd->pw_uid, uid); | |
320 | return NSS_STATUS_NOTFOUND; | |
321 | } | |
322 | ||
cf338316 | 323 | status = check_nonlocal_passwd(pwd->pw_name, pwd, errnop); |
f6903667 AK |
324 | if (status != NSS_STATUS_SUCCESS) |
325 | return status; | |
326 | ||
25468b96 | 327 | if (check_nonlocal_gid(pwd->pw_name, NULL, pwd->pw_gid, &group_errno) != |
f6903667 AK |
328 | NSS_STATUS_SUCCESS) |
329 | pwd->pw_gid = 65534 /* nogroup */; | |
330 | return NSS_STATUS_SUCCESS; | |
331 | } |