]> andersk Git - nss_nonlocal.git/blob - nonlocal-group.c
Only look up MAGIC_LOCAL_GROUPNAME for local users
[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 static bool
283 add_group(gid_t group, long int *start, long int *size, gid_t **groupsp,
284           long int limit, int *errnop, enum nss_status *status)
285 {
286     int i, old_errno = errno;
287     for (i = 0; i < *start; ++i)
288         if ((*groupsp)[i] == group)
289             return true;
290     if (*start + 1 > *size) {
291         gid_t *newgroups;
292         long int newsize = 2 * *size;
293         if (limit > 0) {
294             if (*size >= limit) {
295                 *status = NSS_STATUS_SUCCESS;
296                 return false;
297             }
298             if (newsize > limit)
299                 newsize = limit;
300         }
301         newgroups = realloc(*groupsp, newsize * sizeof((*groupsp)[0]));
302         errno = old_errno;
303         if (newgroups == NULL) {
304             *errnop = ENOMEM;
305             *status = NSS_STATUS_TRYAGAIN;
306             return false;
307         }
308         *groupsp = newgroups;
309         *size = newsize;
310     }
311     (*groupsp)[(*start)++] = group;
312     return true;
313 }
314
315 enum nss_status
316 _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start,
317                              long int *size, gid_t **groupsp, long int limit,
318                              int *errnop)
319 {
320     enum nss_status status;
321     const struct walk_nss w = {
322         .lookup = &__nss_group_nonlocal_lookup, .fct_name = "initgroups_dyn",
323         .status = &status, .errnop = errnop
324     };
325     const __typeof__(&_nss_nonlocal_initgroups_dyn) self = NULL;
326
327     struct group local_users_group, nonlocal_users_group;
328     int is_local = 0;
329     char *buffer;
330     int in, out, i;
331
332     /* Check that the user is a nonlocal user before adding any groups. */
333     status = check_nonlocal_user(user, errnop);
334     if (status == NSS_STATUS_TRYAGAIN) {
335         return status;
336     } else if (status != NSS_STATUS_SUCCESS) {
337         is_local = 1;
338
339         status = get_local_group(MAGIC_LOCAL_GROUPNAME,
340                                  &local_users_group, &buffer, errnop);
341         if (status == NSS_STATUS_SUCCESS) {
342             free(buffer);
343             if (!add_group(local_users_group.gr_gid, start, size, groupsp,
344                            limit, errnop, &status))
345                 return status;
346         } else if (status == NSS_STATUS_TRYAGAIN) {
347             return status;
348         } else {
349             syslog(LOG_WARNING,
350                    "nss_nonlocal: Group %s does not exist locally!",
351                    MAGIC_LOCAL_GROUPNAME);
352         }
353     } else {
354         status = get_local_group(MAGIC_NONLOCAL_GROUPNAME,
355                                  &nonlocal_users_group, &buffer, errnop);
356         if (status == NSS_STATUS_SUCCESS) {
357             free(buffer);
358             if (!add_group(nonlocal_users_group.gr_gid, start, size, groupsp,
359                            limit, errnop, &status))
360                 return status;
361         } else if (status == NSS_STATUS_TRYAGAIN) {
362             return status;
363         } else {
364             syslog(LOG_WARNING, "nss_nonlocal: Group %s does not exist locally!",
365                    MAGIC_NONLOCAL_GROUPNAME);
366         }
367     }
368
369     if (is_local)
370         return NSS_STATUS_SUCCESS;
371
372     in = out = *start;
373
374 #define args (user, group, start, size, groupsp, limit, errnop)
375 #include "walk_nss.h"
376 #undef args
377     if (status != NSS_STATUS_SUCCESS)
378         return status;
379
380     for (; in < *start; ++in) {
381         int nonlocal_errno = *errnop;
382
383         for (i = 0; i < out; ++i)
384             if ((*groupsp)[i] == (*groupsp)[in])
385                 break;
386         if (i < out)
387             continue;
388
389         status = check_nonlocal_gid(user, (*groupsp)[in], &nonlocal_errno);
390         if (status == NSS_STATUS_SUCCESS) {
391             (*groupsp)[out++] = (*groupsp)[in];
392         } else if (status == NSS_STATUS_TRYAGAIN) {
393             *start = out;
394             *errnop = nonlocal_errno;
395             return status;
396         }
397     }
398
399     *start = out;
400     return NSS_STATUS_SUCCESS;
401 }
This page took 0.47829 seconds and 5 git commands to generate.