]> andersk Git - nss_nonlocal.git/blob - nonlocal-passwd.c
* Have initgroups() only add nonlocal groups to nonlocal users.
[nss_nonlocal.git] / nonlocal-passwd.c
1 /*
2  * nonlocal-passwd.c
3  * passwd database for nss_nonlocal proxy.
4  *
5  * Copyright © 2007 Anders Kaseorg <andersk@mit.edu> and Tim Abbott
6  * <tabbott@mit.edu>
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy,
12  * modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  */
28
29
30 #define _GNU_SOURCE
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <dlfcn.h>
37 #include <stdio.h>
38 #include <syslog.h>
39 #include <errno.h>
40 #include <pwd.h>
41 #include <grp.h>
42 #include <nss.h>
43 #include "nsswitch-internal.h"
44 #include "nonlocal.h"
45
46 #define MAGIC_LOCAL_PW_BUFLEN (sysconf(_SC_GETPW_R_SIZE_MAX) + 7)
47
48
49 static service_user *
50 nss_passwd_nonlocal_database(void)
51 {
52     static service_user *nip = NULL;
53     if (nip == NULL)
54         __nss_database_lookup("passwd_nonlocal", NULL, "", &nip);
55
56     return nip;
57 }
58
59
60 enum nss_status
61 check_nonlocal_uid(const char *user, uid_t uid, int *errnop)
62 {
63     enum nss_status status = NSS_STATUS_SUCCESS;
64     struct passwd pwbuf;
65     struct passwd *pwbufp = &pwbuf;
66     int ret;
67     int old_errno = errno;
68     int buflen = MAGIC_LOCAL_PW_BUFLEN;
69     char *buf = malloc(buflen);
70     if (buf == NULL) {
71         *errnop = ENOMEM;
72         errno = old_errno;
73         return NSS_STATUS_TRYAGAIN;
74     }
75     errno = 0;
76     ret = getpwuid_r(uid, pwbufp, buf, buflen, &pwbufp);
77     if (ret != 0) {
78         *errnop = errno;
79         status = NSS_STATUS_TRYAGAIN;
80     } else if (pwbufp != NULL) {
81         syslog(LOG_ERR, "nss_nonlocal: possible spoofing attack: non-local user %s has same UID as local user %s!\n", user, pwbuf.pw_name);
82         status = NSS_STATUS_NOTFOUND;
83     }
84     free(buf);
85     errno = old_errno;
86     return status;
87 }
88
89 enum nss_status
90 check_nonlocal_user(const char *user, int *errnop)
91 {
92     enum nss_status status = NSS_STATUS_SUCCESS;
93     struct passwd pwbuf;
94     struct passwd *pwbufp = &pwbuf;
95     int ret;
96     int old_errno = errno;
97     int buflen = MAGIC_LOCAL_PW_BUFLEN;
98     char *buf = malloc(buflen);
99     if (buf == NULL) {
100         *errnop = ENOMEM;
101         errno = old_errno;
102         return NSS_STATUS_TRYAGAIN;
103     }
104     errno = 0;
105     ret = getpwnam_r(user, pwbufp, buf, buflen, &pwbufp);
106     if (ret != 0) {
107         *errnop = errno;
108         status = NSS_STATUS_TRYAGAIN;
109     } else if (pwbufp != NULL) {
110         status = NSS_STATUS_NOTFOUND;
111     }
112     free(buf);
113     errno = old_errno;
114     return status;
115 }
116
117 static service_user *pwent_nip = NULL;
118 static void *pwent_fct_start;
119 static union {
120     enum nss_status (*l)(struct passwd *pwd, char *buffer, size_t buflen,
121                          int *errnop);
122     void *ptr;
123 } pwent_fct;
124 static const char *pwent_fct_name = "getpwent_r";
125
126 enum nss_status
127 _nss_nonlocal_setpwent(int stayopen)
128 {
129     static const char *fct_name = "setpwent";
130     static void *fct_start = NULL;
131     enum nss_status status;
132     service_user *nip;
133     union {
134         enum nss_status (*l)(int stayopen);
135         void *ptr;
136     } fct;
137
138     nip = nss_passwd_nonlocal_database();
139     if (nip == NULL)
140         return NSS_STATUS_UNAVAIL;
141     if (fct_start == NULL)
142         fct_start = __nss_lookup_function(nip, fct_name);
143     fct.ptr = fct_start;
144     do {
145         if (fct.ptr == NULL)
146             status = NSS_STATUS_UNAVAIL;
147         else
148             status = DL_CALL_FCT(fct.l, (stayopen));
149     } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
150     if (status != NSS_STATUS_SUCCESS)
151         return status;
152
153     pwent_nip = nip;
154     if (pwent_fct_start == NULL)
155         pwent_fct_start = __nss_lookup_function(nip, pwent_fct_name);
156     pwent_fct.ptr = pwent_fct_start;
157     return NSS_STATUS_SUCCESS;
158 }
159
160 enum nss_status
161 _nss_nonlocal_endpwent(void)
162 {
163     static const char *fct_name = "endpwent";
164     static void *fct_start = NULL;
165     enum nss_status status;
166     service_user *nip;
167     union {
168         enum nss_status (*l)(void);
169         void *ptr;
170     } fct;
171
172     pwent_nip = NULL;
173
174     nip = nss_passwd_nonlocal_database();
175     if (nip == NULL)
176         return NSS_STATUS_UNAVAIL;
177     if (fct_start == NULL)
178         fct_start = __nss_lookup_function(nip, fct_name);
179     fct.ptr = fct_start;
180     do {
181         if (fct.ptr == NULL)
182             status = NSS_STATUS_UNAVAIL;
183         else
184             status = DL_CALL_FCT(fct.l, ());
185     } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
186     return status;
187 }
188
189 enum nss_status
190 _nss_nonlocal_getpwent_r(struct passwd *pwd, char *buffer, size_t buflen,
191                          int *errnop)
192 {
193     enum nss_status status;
194     if (pwent_nip == NULL) {
195         status = _nss_nonlocal_setpwent(0);
196         if (status != NSS_STATUS_SUCCESS)
197             return status;
198     }
199     do {
200         if (pwent_fct.ptr == NULL)
201             status = NSS_STATUS_UNAVAIL;
202         else {
203             int nonlocal_errno;
204             do
205                 status = DL_CALL_FCT(pwent_fct.l, (pwd, buffer, buflen, errnop));       
206             while (status == NSS_STATUS_SUCCESS &&
207                    check_nonlocal_uid(pwd->pw_name, pwd->pw_uid, &nonlocal_errno) != NSS_STATUS_SUCCESS);
208         }
209         if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
210             return status;
211
212         if (status == NSS_STATUS_SUCCESS)
213             return NSS_STATUS_SUCCESS;
214     } while (__nss_next(&pwent_nip, pwent_fct_name, &pwent_fct.ptr, status, 0) == 0);
215
216     pwent_nip = NULL;
217     return NSS_STATUS_NOTFOUND;
218 }
219
220
221 enum nss_status
222 _nss_nonlocal_getpwnam_r(const char *name, struct passwd *pwd,
223                          char *buffer, size_t buflen, int *errnop)
224 {
225     static const char *fct_name = "getpwnam_r";
226     static void *fct_start = NULL;
227     enum nss_status status;
228     service_user *nip;
229     union {
230         enum nss_status (*l)(const char *name, struct passwd *pwd,
231                              char *buffer, size_t buflen, int *errnop);
232         void *ptr;
233     } fct;
234     int group_errno;
235
236     if (buflen == MAGIC_LOCAL_PW_BUFLEN)
237         return NSS_STATUS_UNAVAIL;
238
239     nip = nss_passwd_nonlocal_database();
240     if (nip == NULL)
241         return NSS_STATUS_UNAVAIL;
242     if (fct_start == NULL)
243         fct_start = __nss_lookup_function(nip, fct_name);
244     fct.ptr = fct_start;
245     do {
246         if (fct.ptr == NULL)
247             status = NSS_STATUS_UNAVAIL;
248         else
249             status = DL_CALL_FCT(fct.l, (name, pwd, buffer, buflen, errnop));
250         if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
251             break;
252     } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
253     if (status != NSS_STATUS_SUCCESS)
254         return status;
255
256     status = check_nonlocal_uid(name, pwd->pw_uid, errnop);
257     if (status != NSS_STATUS_SUCCESS)
258         return status;
259
260     if (check_nonlocal_gid(name, pwd->pw_gid, &group_errno) !=
261         NSS_STATUS_SUCCESS)
262         pwd->pw_gid = 65534 /* nogroup */;
263     return NSS_STATUS_SUCCESS;
264 }
265
266 enum nss_status
267 _nss_nonlocal_getpwuid_r(uid_t uid, struct passwd *pwd,
268                          char *buffer, size_t buflen, int *errnop)
269 {
270     static const char *fct_name = "getpwuid_r";
271     static void *fct_start = NULL;
272     enum nss_status status;
273     service_user *nip;
274     union {
275         enum nss_status (*l)(uid_t uid, struct passwd *pwd,
276                              char *buffer, size_t buflen, int *errnop);
277         void *ptr;
278     } fct;
279     int group_errno;
280
281     if (buflen == MAGIC_LOCAL_PW_BUFLEN)
282         return NSS_STATUS_UNAVAIL;
283
284     nip = nss_passwd_nonlocal_database();
285     if (nip == NULL)
286         return NSS_STATUS_UNAVAIL;
287     if (fct_start == NULL)
288         fct_start = __nss_lookup_function(nip, fct_name);
289     fct.ptr = fct_start;
290     do {
291         if (fct.ptr == NULL)
292             status = NSS_STATUS_UNAVAIL;
293         else
294             status = DL_CALL_FCT(fct.l, (uid, pwd, buffer, buflen, errnop));
295         if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
296             break;
297     } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
298     if (status != NSS_STATUS_SUCCESS)
299         return status;
300
301     status = check_nonlocal_uid(pwd->pw_name, pwd->pw_uid, errnop);
302     if (status != NSS_STATUS_SUCCESS)
303         return status;
304
305     if (check_nonlocal_gid(pwd->pw_name, pwd->pw_gid, &group_errno) !=
306         NSS_STATUS_SUCCESS)
307         pwd->pw_gid = 65534 /* nogroup */;
308     return NSS_STATUS_SUCCESS;
309 }
This page took 0.337022 seconds and 5 git commands to generate.