]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * nonlocal-passwd.c | |
3 | * passwd database for nss_nonlocal proxy. | |
4 | * | |
5 | * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu> and Tim | |
6 | * Abbott <tabbott@mit.edu> | |
7 | * | |
8 | * This file is part of nss_nonlocal. | |
9 | * | |
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. | |
14 | * | |
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 | |
24 | */ | |
25 | ||
26 | ||
27 | #define _GNU_SOURCE | |
28 | ||
29 | #include <sys/types.h> | |
30 | #include <dlfcn.h> | |
31 | #include <errno.h> | |
32 | #include <nss.h> | |
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 | ||
41 | #include "nsswitch-internal.h" | |
42 | #include "nonlocal.h" | |
43 | ||
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); | |
51 | ||
52 | ||
53 | static service_user *__nss_passwd_nonlocal_database; | |
54 | ||
55 | static int | |
56 | internal_function | |
57 | __nss_passwd_nonlocal_lookup(service_user **ni, const char *fct_name, | |
58 | void **fctp) | |
59 | { | |
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; | |
66 | ||
67 | *fctp = __nss_lookup_function(*ni, fct_name); | |
68 | return 0; | |
69 | } | |
70 | ||
71 | ||
72 | enum nss_status | |
73 | check_nonlocal_uid(const char *user, uid_t uid, int *errnop) | |
74 | { | |
75 | enum nss_status status; | |
76 | struct passwd pwbuf; | |
77 | char *buf; | |
78 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
79 | const struct walk_nss w = { | |
80 | .lookup = &__nss_passwd_lookup, .fct_name = "getpwuid_r", | |
81 | .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen | |
82 | }; | |
83 | const __typeof__(&_nss_nonlocal_getpwuid_r) self = &_nss_nonlocal_getpwuid_r; | |
84 | #define args (uid, &pwbuf, buf, buflen, errnop) | |
85 | #include "walk_nss.h" | |
86 | #undef args | |
87 | ||
88 | if (status == NSS_STATUS_SUCCESS) { | |
89 | syslog(LOG_ERR, "nss_nonlocal: possible spoofing attack: non-local user %s has same UID as local user %s!\n", user, pwbuf.pw_name); | |
90 | free(buf); | |
91 | status = NSS_STATUS_NOTFOUND; | |
92 | } else if (status != NSS_STATUS_TRYAGAIN) { | |
93 | status = NSS_STATUS_SUCCESS; | |
94 | } | |
95 | ||
96 | return status; | |
97 | } | |
98 | ||
99 | enum nss_status | |
100 | check_nonlocal_passwd(const char *user, struct passwd *pwd, int *errnop) | |
101 | { | |
102 | enum nss_status status = NSS_STATUS_SUCCESS; | |
103 | int old_errno = errno; | |
104 | char *end; | |
105 | unsigned long uid; | |
106 | ||
107 | errno = 0; | |
108 | uid = strtoul(pwd->pw_name, &end, 10); | |
109 | if (errno == 0 && *end == '\0' && (uid_t)uid == uid) { | |
110 | errno = old_errno; | |
111 | status = check_nonlocal_uid(user, uid, errnop); | |
112 | } else { | |
113 | errno = old_errno; | |
114 | } | |
115 | if (status != NSS_STATUS_SUCCESS) | |
116 | return status; | |
117 | ||
118 | return check_nonlocal_uid(user, pwd->pw_uid, errnop); | |
119 | } | |
120 | ||
121 | enum nss_status | |
122 | check_nonlocal_user(const char *user, int *errnop) | |
123 | { | |
124 | enum nss_status status; | |
125 | struct passwd pwbuf; | |
126 | char *buf; | |
127 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
128 | const struct walk_nss w = { | |
129 | .lookup = __nss_passwd_lookup, .fct_name = "getpwnam_r", | |
130 | .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen | |
131 | }; | |
132 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = &_nss_nonlocal_getpwnam_r; | |
133 | #define args (user, &pwbuf, buf, buflen, errnop) | |
134 | #include "walk_nss.h" | |
135 | #undef args | |
136 | ||
137 | if (status == NSS_STATUS_SUCCESS) { | |
138 | free(buf); | |
139 | status = NSS_STATUS_NOTFOUND; | |
140 | } else if (status != NSS_STATUS_TRYAGAIN) { | |
141 | status = NSS_STATUS_SUCCESS; | |
142 | } | |
143 | ||
144 | return status; | |
145 | } | |
146 | ||
147 | enum nss_status | |
148 | get_nonlocal_passwd(const char *name, struct passwd *pwd, char **buffer, | |
149 | int *errnop) | |
150 | { | |
151 | enum nss_status status; | |
152 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
153 | const struct walk_nss w = { | |
154 | .lookup = __nss_passwd_nonlocal_lookup, .fct_name = "getpwnam_r", | |
155 | .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen | |
156 | }; | |
157 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL; | |
158 | #define args (name, pwd, *buffer, buflen, errnop) | |
159 | #include "walk_nss.h" | |
160 | #undef args | |
161 | return status; | |
162 | } | |
163 | ||
164 | ||
165 | static service_user *pwent_startp, *pwent_nip; | |
166 | static void *pwent_fct_start; | |
167 | static union { | |
168 | enum nss_status (*l)(struct passwd *pwd, char *buffer, size_t buflen, | |
169 | int *errnop); | |
170 | void *ptr; | |
171 | } pwent_fct; | |
172 | static const char *pwent_fct_name = "getpwent_r"; | |
173 | ||
174 | enum nss_status | |
175 | _nss_nonlocal_setpwent(int stayopen) | |
176 | { | |
177 | enum nss_status status; | |
178 | const struct walk_nss w = { | |
179 | .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "setpwent", | |
180 | .status = &status | |
181 | }; | |
182 | const __typeof__(&_nss_nonlocal_setpwent) self = NULL; | |
183 | #define args (stayopen) | |
184 | #include "walk_nss.h" | |
185 | #undef args | |
186 | if (status != NSS_STATUS_SUCCESS) | |
187 | return status; | |
188 | ||
189 | if (pwent_fct_start == NULL) | |
190 | __nss_passwd_nonlocal_lookup(&pwent_startp, pwent_fct_name, | |
191 | &pwent_fct_start); | |
192 | pwent_nip = pwent_startp; | |
193 | pwent_fct.ptr = pwent_fct_start; | |
194 | return NSS_STATUS_SUCCESS; | |
195 | } | |
196 | ||
197 | enum nss_status | |
198 | _nss_nonlocal_endpwent(void) | |
199 | { | |
200 | enum nss_status status; | |
201 | const struct walk_nss w = { | |
202 | .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "endpwent", | |
203 | .status = &status | |
204 | }; | |
205 | const __typeof__(&_nss_nonlocal_endpwent) self = NULL; | |
206 | ||
207 | pwent_nip = NULL; | |
208 | ||
209 | #define args () | |
210 | #include "walk_nss.h" | |
211 | #undef args | |
212 | return status; | |
213 | } | |
214 | ||
215 | enum nss_status | |
216 | _nss_nonlocal_getpwent_r(struct passwd *pwd, char *buffer, size_t buflen, | |
217 | int *errnop) | |
218 | { | |
219 | enum nss_status status; | |
220 | ||
221 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); | |
222 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') | |
223 | return NSS_STATUS_UNAVAIL; | |
224 | ||
225 | if (pwent_nip == NULL) { | |
226 | status = _nss_nonlocal_setpwent(0); | |
227 | if (status != NSS_STATUS_SUCCESS) | |
228 | return status; | |
229 | } | |
230 | do { | |
231 | if (pwent_fct.ptr == NULL) | |
232 | status = NSS_STATUS_UNAVAIL; | |
233 | else { | |
234 | int nonlocal_errno; | |
235 | do | |
236 | status = DL_CALL_FCT(pwent_fct.l, (pwd, buffer, buflen, errnop)); | |
237 | while (status == NSS_STATUS_SUCCESS && | |
238 | check_nonlocal_passwd(pwd->pw_name, pwd, &nonlocal_errno) != NSS_STATUS_SUCCESS); | |
239 | } | |
240 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) | |
241 | return status; | |
242 | ||
243 | if (status == NSS_STATUS_SUCCESS) | |
244 | return NSS_STATUS_SUCCESS; | |
245 | } while (__nss_next(&pwent_nip, pwent_fct_name, &pwent_fct.ptr, status, 0) == 0); | |
246 | ||
247 | pwent_nip = NULL; | |
248 | return NSS_STATUS_NOTFOUND; | |
249 | } | |
250 | ||
251 | ||
252 | enum nss_status | |
253 | _nss_nonlocal_getpwnam_r(const char *name, struct passwd *pwd, | |
254 | char *buffer, size_t buflen, int *errnop) | |
255 | { | |
256 | enum nss_status status; | |
257 | int group_errno; | |
258 | const struct walk_nss w = { | |
259 | .lookup = __nss_passwd_nonlocal_lookup, .fct_name = "getpwnam_r", | |
260 | .status = &status, .errnop = errnop | |
261 | }; | |
262 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL; | |
263 | ||
264 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); | |
265 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') | |
266 | return NSS_STATUS_UNAVAIL; | |
267 | ||
268 | #define args (name, pwd, buffer, buflen, errnop) | |
269 | #include "walk_nss.h" | |
270 | #undef args | |
271 | if (status != NSS_STATUS_SUCCESS) | |
272 | return status; | |
273 | ||
274 | if (strcmp(name, pwd->pw_name) != 0) { | |
275 | syslog(LOG_ERR, "nss_nonlocal: discarding user %s from lookup for user %s\n", pwd->pw_name, name); | |
276 | return NSS_STATUS_NOTFOUND; | |
277 | } | |
278 | ||
279 | status = check_nonlocal_passwd(name, pwd, errnop); | |
280 | if (status != NSS_STATUS_SUCCESS) | |
281 | return status; | |
282 | ||
283 | if (check_nonlocal_gid(name, NULL, pwd->pw_gid, &group_errno) != | |
284 | NSS_STATUS_SUCCESS) | |
285 | pwd->pw_gid = 65534 /* nogroup */; | |
286 | return NSS_STATUS_SUCCESS; | |
287 | } | |
288 | ||
289 | enum nss_status | |
290 | _nss_nonlocal_getpwuid_r(uid_t uid, struct passwd *pwd, | |
291 | char *buffer, size_t buflen, int *errnop) | |
292 | { | |
293 | enum nss_status status; | |
294 | int group_errno; | |
295 | const struct walk_nss w = { | |
296 | .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "getpwuid_r", | |
297 | .status = &status, .errnop = errnop | |
298 | }; | |
299 | const __typeof__(&_nss_nonlocal_getpwuid_r) self = NULL; | |
300 | ||
301 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); | |
302 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') | |
303 | return NSS_STATUS_UNAVAIL; | |
304 | ||
305 | #define args (uid, pwd, buffer, buflen, errnop) | |
306 | #include "walk_nss.h" | |
307 | #undef args | |
308 | if (status != NSS_STATUS_SUCCESS) | |
309 | return status; | |
310 | ||
311 | if (uid != pwd->pw_uid) { | |
312 | syslog(LOG_ERR, "nss_nonlocal: discarding uid %d from lookup for uid %d\n", pwd->pw_uid, uid); | |
313 | return NSS_STATUS_NOTFOUND; | |
314 | } | |
315 | ||
316 | status = check_nonlocal_passwd(pwd->pw_name, pwd, errnop); | |
317 | if (status != NSS_STATUS_SUCCESS) | |
318 | return status; | |
319 | ||
320 | if (check_nonlocal_gid(pwd->pw_name, NULL, pwd->pw_gid, &group_errno) != | |
321 | NSS_STATUS_SUCCESS) | |
322 | pwd->pw_gid = 65534 /* nogroup */; | |
323 | return NSS_STATUS_SUCCESS; | |
324 | } |