]> andersk Git - nss_nonlocal.git/blob - nonlocal-group.c
Move the repetitive NSS walking logic into an include file
[nss_nonlocal.git] / nonlocal-group.c
1 /*
2  * nonlocal-group.c
3  * group 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 #define _GNU_SOURCE
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <dlfcn.h>
33 #include <stdio.h>
34 #include <syslog.h>
35 #include <errno.h>
36 #include <grp.h>
37 #include <nss.h>
38 #include "nsswitch-internal.h"
39 #include "nonlocal.h"
40
41 #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users"
42 #define MAGIC_LOCAL_GROUPNAME "nss-local-users"
43
44
45 enum nss_status
46 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
47                          char *buffer, size_t buflen, int *errnop);
48
49 enum nss_status
50 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
51                          char *buffer, size_t buflen, int *errnop);
52
53
54 static service_user *__nss_group_nonlocal_database;
55
56 static int
57 internal_function
58 __nss_group_nonlocal_lookup(service_user **ni, const char *fct_name,
59                             void **fctp)
60 {
61     if (__nss_group_nonlocal_database == NULL
62         && __nss_database_lookup("group_nonlocal", NULL, NULL,
63                                  &__nss_group_nonlocal_database) < 0)
64         return -1;
65
66     *ni = __nss_group_nonlocal_database;
67
68     *fctp = __nss_lookup_function(*ni, fct_name);
69     return 0;
70 }
71
72
73 enum nss_status
74 check_nonlocal_gid(const char *user, gid_t gid, int *errnop)
75 {
76     enum nss_status status;
77     struct group gbuf;
78     char *buf;
79     size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
80     const struct walk_nss w = {
81         .lookup = &__nss_group_lookup, .fct_name = "getgrgid_r",
82         .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen
83     };
84     const __typeof__(&_nss_nonlocal_getgrgid_r) self = &_nss_nonlocal_getgrgid_r;
85 #define args (gid, &gbuf, buf, buflen, errnop)
86 #include "walk_nss.h"
87 #undef args
88
89     if (status == NSS_STATUS_SUCCESS) {
90         syslog(LOG_DEBUG, "nss_nonlocal: removing local group %u (%s) from non-local user %s\n", gbuf.gr_gid, gbuf.gr_name, user);
91         free(buf);
92         status = NSS_STATUS_NOTFOUND;
93     } else if (status != NSS_STATUS_TRYAGAIN) {
94         status = NSS_STATUS_SUCCESS;
95     }
96
97     return status;
98 }
99
100 enum nss_status
101 check_nonlocal_group(const char *user, struct group *grp, int *errnop)
102 {
103     enum nss_status status = NSS_STATUS_SUCCESS;
104     int old_errno = errno;
105     char *end;
106     unsigned long gid;
107
108     errno = 0;
109     gid = strtoul(grp->gr_name, &end, 10);
110     if (errno == 0 && *end == '\0' && (gid_t)gid == gid) {
111         errno = old_errno;
112         status = check_nonlocal_gid(user, gid, errnop);
113     } else
114         errno = old_errno;
115     if (status != NSS_STATUS_SUCCESS)
116         return status;
117
118     return check_nonlocal_gid(user, grp->gr_gid, errnop);
119 }
120
121 enum nss_status
122 get_local_group(const char *name, struct group *grp, char **buffer, int *errnop)
123 {
124     enum nss_status status;
125     size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
126     const struct walk_nss w = {
127         .lookup = &__nss_group_lookup, .fct_name = "getgrnam_r",
128         .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen
129     };
130     const __typeof__(&_nss_nonlocal_getgrnam_r) self = &_nss_nonlocal_getgrnam_r;
131 #define args (name, grp, *buffer, buflen, errnop)
132 #include "walk_nss.h"
133 #undef args
134     return status;
135 }
136
137 static service_user *grent_startp, *grent_nip;
138 static void *grent_fct_start;
139 static union {
140     enum nss_status (*l)(struct group *grp, char *buffer, size_t buflen,
141                          int *errnop);
142     void *ptr;
143 } grent_fct;
144 static const char *grent_fct_name = "getgrent_r";
145
146 enum nss_status
147 _nss_nonlocal_setgrent(int stayopen)
148 {
149     enum nss_status status;
150     const struct walk_nss w = {
151         .lookup = &__nss_group_nonlocal_lookup, .fct_name = "setgrent",
152         .status = &status
153     };
154     const __typeof__(&_nss_nonlocal_setgrent) self = NULL;
155 #define args (stayopen)
156 #include "walk_nss.h"
157 #undef args
158     if (status != NSS_STATUS_SUCCESS)
159         return status;
160
161     if (grent_fct_start == NULL)
162         __nss_group_nonlocal_lookup(&grent_startp, grent_fct_name,
163                                     &grent_fct_start);
164     grent_nip = grent_startp;
165     grent_fct.ptr = grent_fct_start;
166     return NSS_STATUS_SUCCESS;
167 }
168
169 enum nss_status
170 _nss_nonlocal_endgrent(void)
171 {
172     enum nss_status status;
173     const struct walk_nss w = {
174         .lookup = &__nss_group_nonlocal_lookup, .fct_name = "endgrent",
175         .status = &status
176     };
177     const __typeof__(&_nss_nonlocal_endgrent) self = NULL;
178
179     grent_nip = NULL;
180
181 #define args ()
182 #include "walk_nss.h"
183 #undef args
184     return status;
185 }
186
187 enum nss_status
188 _nss_nonlocal_getgrent_r(struct group *grp, char *buffer, size_t buflen,
189                          int *errnop)
190 {
191     enum nss_status status;
192
193     char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
194     if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
195         return NSS_STATUS_UNAVAIL;
196
197     if (grent_nip == NULL) {
198         status = _nss_nonlocal_setgrent(0);
199         if (status != NSS_STATUS_SUCCESS)
200             return status;
201     }
202     do {
203         if (grent_fct.ptr == NULL)
204             status = NSS_STATUS_UNAVAIL;
205         else {
206             int nonlocal_errno;
207             do
208                 status = DL_CALL_FCT(grent_fct.l, (grp, buffer, buflen, errnop));
209             while (status == NSS_STATUS_SUCCESS &&
210                    check_nonlocal_group("(unknown)", grp, &nonlocal_errno) != NSS_STATUS_SUCCESS);
211         }
212         if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
213             return status;
214
215         if (status == NSS_STATUS_SUCCESS)
216             return NSS_STATUS_SUCCESS;
217     } while (__nss_next(&grent_nip, grent_fct_name, &grent_fct.ptr, status, 0) == 0);
218
219     grent_nip = NULL;
220     return NSS_STATUS_NOTFOUND;
221 }
222
223
224 enum nss_status
225 _nss_nonlocal_getgrnam_r(const char *name, struct group *grp,
226                          char *buffer, size_t buflen, int *errnop)
227 {
228     enum nss_status status;
229     const struct walk_nss w = {
230         .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrnam_r",
231         .status = &status, .errnop = errnop
232     };
233     const __typeof__(&_nss_nonlocal_getgrnam_r) self = NULL;
234
235     char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
236     if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
237         return NSS_STATUS_UNAVAIL;
238
239 #define args (name, grp, buffer, buflen, errnop)
240 #include "walk_nss.h"
241 #undef args
242     if (status != NSS_STATUS_SUCCESS)
243         return status;
244
245     if (strcmp(name, grp->gr_name) != 0) {
246         syslog(LOG_ERR, "nss_nonlocal: discarding group %s from lookup for group %s\n", grp->gr_name, name);
247         return NSS_STATUS_NOTFOUND;
248     }
249
250     return check_nonlocal_group(name, grp, errnop);
251 }
252
253 enum nss_status
254 _nss_nonlocal_getgrgid_r(gid_t gid, struct group *grp,
255                          char *buffer, size_t buflen, int *errnop)
256 {
257     enum nss_status status;
258     const struct walk_nss w = {
259         .lookup = &__nss_group_nonlocal_lookup, .fct_name = "getgrgid_r",
260         .status = &status, .errnop = errnop
261     };
262     const __typeof__(&_nss_nonlocal_getgrgid_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 (gid, grp, buffer, buflen, errnop)
269 #include "walk_nss.h"
270 #undef args
271     if (status != NSS_STATUS_SUCCESS)
272         return status;
273
274     if (gid != grp->gr_gid) {
275         syslog(LOG_ERR, "nss_nonlocal: discarding gid %d from lookup for gid %d\n", grp->gr_gid, gid);
276         return NSS_STATUS_NOTFOUND;
277     }
278
279     return check_nonlocal_group(grp->gr_name, grp, errnop);
280 }
281
282 enum nss_status
283 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
284                              long int *size, gid_t **groupsp, long int limit,
285                              int *errnop)
286 {
287     enum nss_status status;
288     const struct walk_nss w = {
289         .lookup = &__nss_group_nonlocal_lookup, .fct_name = "initgroups_dyn",
290         .status = &status, .errnop = errnop
291     };
292     const __typeof__(&_nss_nonlocal_initgroups_dyn) self = NULL;
293
294     struct group local_users_group, nonlocal_users_group;
295     gid_t local_users_gid, gid;
296     int is_local = 0;
297     char *buffer;
298     int old_errno;
299     int in, out, i;
300
301     /* Check that the user is a nonlocal user before adding any groups. */
302     status = check_nonlocal_user(user, errnop);
303     if (status == NSS_STATUS_TRYAGAIN)
304         return status;
305     else if (status != NSS_STATUS_SUCCESS)
306         is_local = 1;
307
308     old_errno = errno;
309
310     status = get_local_group(MAGIC_LOCAL_GROUPNAME,
311                              &local_users_group, &buffer, errnop);
312     if (status == NSS_STATUS_SUCCESS) {
313         local_users_gid = local_users_group.gr_gid;
314         free(buffer);
315     } else if (status == NSS_STATUS_TRYAGAIN) {
316         return status;
317     } else {
318         syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
319                MAGIC_LOCAL_GROUPNAME);
320         local_users_gid = -1;
321     }
322
323     if (is_local) {
324         gid = local_users_gid;
325     } else {
326         status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
327                                  &nonlocal_users_group, &buffer, errnop);
328         if (status == NSS_STATUS_SUCCESS) {
329             gid = nonlocal_users_group.gr_gid;
330             free(buffer);
331         } else if (status == NSS_STATUS_TRYAGAIN) {
332             return status;
333         } else {
334             syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
335                    MAGIC_NONLOCAL_GROUPNAME);
336             gid = -1;
337         }
338     }
339
340     if (gid != -1) {
341         int i;
342         for (i = 0; i < *start; ++i)
343             if ((*groupsp)[i] == gid)
344                 break;
345         if (i >= *start) {
346             if (*start + 1 > *size) {
347                 gid_t *newgroups;
348                 long int newsize = 2 * *size;
349                 if (limit > 0) {
350                     if (*size >= limit)
351                         return NSS_STATUS_SUCCESS;
352                     if (newsize > limit)
353                         newsize = limit;
354                 }
355                 newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
356                 errno = old_errno;
357                 if (newgroups == NULL) {
358                     *errnop = ENOMEM;
359                     return NSS_STATUS_TRYAGAIN;
360                 }
361                 *groupsp = newgroups;
362                 *size = newsize;
363             }
364             (*groupsp)[(*start)++] = gid;
365         }
366     }
367
368     if (is_local)
369         return NSS_STATUS_SUCCESS;
370
371     in = out = *start;
372
373 #define args (user, group, start, size, groupsp, limit, errnop)
374 #include "walk_nss.h"
375 #undef args
376     if (status != NSS_STATUS_SUCCESS)
377         return status;
378
379     for (; in < *start; ++in) {
380         int nonlocal_errno = *errnop;
381
382         for (i = 0; i < out; ++i)
383             if ((*groupsp)[i] == (*groupsp)[in])
384                 break;
385         if (i < out)
386             continue;
387
388         /* Don't let users get into MAGIC_LOCAL_GROUPNAME from nonlocal reasons. */
389         if (local_users_gid == (*groupsp)[in]) {
390             syslog(LOG_WARNING, "nss_nonlocal: Nonlocal user %s removed from special local users group %s",
391                    user, MAGIC_LOCAL_GROUPNAME);
392             continue;
393         }
394
395         status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
396         if (status == NSS_STATUS_SUCCESS) {
397             (*groupsp)[out++] = (*groupsp)[in];
398         } else if (status == NSS_STATUS_TRYAGAIN) {
399             *start = out;
400             *errnop = nonlocal_errno;
401             return status;
402         }
403     }
404
405     *start = out;
406     return NSS_STATUS_SUCCESS;
407 }
This page took 0.094176 seconds and 5 git commands to generate.