1 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
29 #include <rpcsvc/nis.h>
33 #include "nss-nisplus.h"
34 #include "nisplus-parser.h"
36 static service_user *ni = NULL;
37 static bool_t use_nisplus = FALSE; /* default: group_compat: nis */
38 static nis_name grptable = NULL; /* Name of the group table */
39 static size_t grptablelen = 0;
41 /* Get the declaration of the parser function. */
43 #define STRUCTURE group
45 #include <nss/nss_files/files-parse.c>
47 /* Structure for remembering -group members ... */
48 #define BLACKLIST_INITIAL_SIZE 512
49 #define BLACKLIST_INCREMENT 256
60 struct response_t *next;
71 struct blacklist_t blacklist;
72 struct response_t *start;
73 struct response_t *next;
75 typedef struct ent_t ent_t;
78 /* Prototypes for local functions. */
79 static void blacklist_store_name (const char *, ent_t *);
80 static int in_blacklist (const char *, int, ent_t *);
83 saveit (int instatus, char *inkey, int inkeylen, char *inval,
84 int invallen, char *indata)
86 ent_t *intern = (ent_t *) indata;
88 if (instatus != YP_TRUE)
91 if (inkey && inkeylen > 0 && inval && invallen > 0)
93 if (intern->start == NULL)
95 intern->start = malloc (sizeof (struct response_t));
96 if (intern->start == NULL)
98 intern->next = intern->start;
102 intern->next->next = malloc (sizeof (struct response_t));
103 if (intern->next->next == NULL)
105 intern->next = intern->next->next;
107 intern->next->next = NULL;
108 intern->next->val = malloc (invallen + 1);
109 if (intern->next->val == NULL)
111 strncpy (intern->next->val, inval, invallen);
112 intern->next->val[invallen] = '\0';
118 static enum nss_status
119 _nss_first_init (void)
123 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
124 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
127 if (grptable == NULL)
129 static const char key[] = "group.org_dir.";
130 const char *local_dir = nis_local_directory ();
131 size_t len_local_dir = strlen (local_dir);
133 grptable = malloc (sizeof (key) + len_local_dir);
134 if (grptable == NULL)
135 return NSS_STATUS_TRYAGAIN;
137 grptablelen = ((char *) mempcpy (mempcpy (grptable,
138 key, sizeof (key) - 1),
139 local_dir, len_local_dir + 1)
143 return NSS_STATUS_SUCCESS;
146 static enum nss_status
147 internal_setgrent (ent_t *ent)
149 enum nss_status status = NSS_STATUS_SUCCESS;
151 ent->nis = ent->nis_first = 0;
156 if (_nss_first_init () != NSS_STATUS_SUCCESS)
157 return NSS_STATUS_UNAVAIL;
159 if (ent->oldkey != NULL)
166 if (ent->result != NULL)
168 nis_freeresult (ent->result);
172 if (ent->blacklist.data != NULL)
174 ent->blacklist.current = 1;
175 ent->blacklist.data[0] = '|';
176 ent->blacklist.data[1] = '\0';
179 ent->blacklist.current = 0;
181 if (ent->stream == NULL)
183 ent->stream = fopen ("/etc/group", "r");
185 if (ent->stream == NULL)
186 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
189 /* We have to make sure the file is `closed on exec'. */
192 result = flags = fcntl (fileno (ent->stream), F_GETFD, 0);
196 result = fcntl (fileno (ent->stream), F_SETFD, flags);
200 /* Something went wrong. Close the stream and return a
202 fclose (ent->stream);
204 status = NSS_STATUS_UNAVAIL;
209 rewind (ent->stream);
215 static enum nss_status
216 internal_endgrent (ent_t *ent)
218 if (ent->stream != NULL)
220 fclose (ent->stream);
224 ent->nis = ent->nis_first = 0;
226 if (ent->oldkey != NULL)
233 if (ent->result != NULL)
235 nis_freeresult (ent->result);
239 if (ent->blacklist.data != NULL)
241 ent->blacklist.current = 1;
242 ent->blacklist.data[0] = '|';
243 ent->blacklist.data[1] = '\0';
246 ent->blacklist.current = 0;
248 while (ent->start != NULL)
250 if (ent->start->val != NULL)
251 free (ent->start->val);
252 ent->next = ent->start;
253 ent->start = ent->start->next;
258 return NSS_STATUS_SUCCESS;
261 static enum nss_status
262 getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
263 size_t buflen, int *errnop)
265 struct parser_data *data = (void *) buffer;
269 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
273 return NSS_STATUS_NOTFOUND;
276 if (ent->start == NULL)
278 struct ypall_callback ypcb;
279 enum nss_status status;
281 ypcb.foreach = saveit;
282 ypcb.data = (char *) ent;
283 status = yperr2nss (yp_all (domain, "group.byname", &ypcb));
284 ent->next = ent->start;
286 if (ent->start == NULL || status != NSS_STATUS_SUCCESS)
290 return NSS_STATUS_UNAVAIL;
297 if (ent->next == NULL)
301 return NSS_STATUS_NOTFOUND;
304 /* Copy the found data to our buffer... */
305 p = strncpy (buffer, ent->next->val, buflen);
309 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
313 return NSS_STATUS_TRYAGAIN;
316 ent->next = ent->next->next;
319 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
320 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
324 return NSS_STATUS_SUCCESS;
327 static enum nss_status
328 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
329 size_t buflen, int *errnop)
335 nis_result *save_oldres;
336 bool_t save_nis_first;
340 save_oldres = ent->result;
341 save_nis_first = TRUE;
342 ent->result = nis_first_entry(grptable);
343 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
346 return niserr2nss (ent->result->status);
348 ent->nis_first = FALSE;
354 save_oldres = ent->result;
355 save_nis_first = FALSE;
356 res = nis_next_entry(grptable, &ent->result->cookie);
358 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
361 return niserr2nss (ent->result->status);
364 parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
365 buffer, buflen, errnop);
368 nis_freeresult (ent->result);
369 ent->result = save_oldres;
370 ent->nis_first = save_nis_first;
372 return NSS_STATUS_TRYAGAIN;
377 nis_freeresult (save_oldres);
381 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
382 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
386 return NSS_STATUS_SUCCESS;
389 /* This function handle the +group entrys in /etc/group */
390 static enum nss_status
391 getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
392 size_t buflen, int *errnop)
394 struct parser_data *data = (void *) buffer;
397 if (use_nisplus) /* Do the NIS+ query here */
400 char buf[strlen (name) + 24 + grptablelen];
402 sprintf(buf, "[name=%s],%s", name, grptable);
403 res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
404 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
406 enum nss_status status = niserr2nss (res->status);
408 nis_freeresult (res);
411 parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer, buflen,
415 nis_freeresult (res);
417 return NSS_STATUS_TRYAGAIN;
419 nis_freeresult (res);
423 char *domain, *outval, *p;
426 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
429 return NSS_STATUS_NOTFOUND;
432 if (yp_match (domain, "group.byname", name, strlen (name),
433 &outval, &outvallen) != YPERR_SUCCESS)
436 return NSS_STATUS_NOTFOUND;
439 if (buflen < ((size_t) outvallen + 1))
443 return NSS_STATUS_TRYAGAIN;
446 /* Copy the found data to our buffer... */
447 p = strncpy (buffer, outval, buflen);
449 /* ... and free the data. */
453 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
457 return NSS_STATUS_TRYAGAIN;
462 /* We found the entry. */
463 return NSS_STATUS_SUCCESS;
465 return NSS_STATUS_RETURN;
468 static enum nss_status
469 getgrent_next_file (struct group *result, ent_t *ent,
470 char *buffer, size_t buflen, int *errnop)
472 struct parser_data *data = (void *) buffer;
481 fgetpos (ent->stream, &pos);
482 buffer[buflen - 1] = '\xff';
483 p = fgets (buffer, buflen, ent->stream);
484 if (p == NULL && feof (ent->stream))
487 return NSS_STATUS_NOTFOUND;
489 if (p == NULL || buffer[buflen - 1] != '\xff')
491 fsetpos (ent->stream, &pos);
493 return NSS_STATUS_TRYAGAIN;
496 /* Terminate the line for any case. */
497 buffer[buflen - 1] = '\0';
499 /* Skip leading blanks. */
503 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
504 /* Parse the line. If it is invalid, loop to
505 get the next line of the file to parse. */
506 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
511 /* The parser ran out of space. */
512 fsetpos (ent->stream, &pos);
514 return NSS_STATUS_TRYAGAIN;
517 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
518 /* This is a real entry. */
522 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
523 && result->gr_name[1] != '@')
525 blacklist_store_name (&result->gr_name[1], ent);
530 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
531 && result->gr_name[1] != '@')
533 enum nss_status status;
535 /* Store the group in the blacklist for the "+" at the end of
537 blacklist_store_name (&result->gr_name[1], ent);
538 status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
540 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
543 if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
544 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
548 if (status == NSS_STATUS_TRYAGAIN)
550 /* The parser ran out of space. */
551 fsetpos (ent->stream, &pos);
559 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
562 ent->nis_first = TRUE;
565 return getgrent_next_nisplus (result, ent, buffer, buflen, errnop);
567 return getgrent_next_nis (result, ent, buffer, buflen, errnop);
571 return NSS_STATUS_SUCCESS;
575 static enum nss_status
576 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
577 size_t buflen, int *errnop)
582 return getgrent_next_nisplus (gr, ent, buffer, buflen, errnop);
584 return getgrent_next_nis (gr, ent, buffer, buflen, errnop);
587 return getgrent_next_file (gr, ent, buffer, buflen, errnop);
591 _nss_compat_initgroups_dyn (const char *user, gid_t group, long int *start,
592 long int *size, gid_t **groupsp, int *errnop)
594 struct group grpbuf, *g;
595 size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
597 enum nss_status status;
598 ent_t intern = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
599 gid_t *groups = *groupsp;
601 status = internal_setgrent (&intern);
602 if (status != NSS_STATUS_SUCCESS)
605 tmpbuf = __alloca (buflen);
610 internal_getgrent_r (&grpbuf, &intern, tmpbuf, buflen,
611 errnop)) == NSS_STATUS_TRYAGAIN
612 && *errnop == ERANGE)
615 tmpbuf = __alloca (buflen);
618 if (status != NSS_STATUS_SUCCESS)
622 if (g->gr_gid != group)
626 for (m = g->gr_mem; *m != NULL; ++m)
627 if (strcmp (*m, user) == 0)
629 /* Matches user. Insert this group. */
632 /* Need a bigger buffer. */
634 newgroups = realloc (groups, 2 * *size * sizeof (*groups));
635 if (newgroups == NULL)
637 *groupsp = groups = newgroups;
641 groups[*start] = g->gr_gid;
648 while (status == NSS_STATUS_SUCCESS);
651 internal_endgrent (&intern);
653 return NSS_STATUS_SUCCESS;
657 /* Support routines for remembering -@netgroup and -user entries.
658 The names are stored in a single string with `|' as separator. */
660 blacklist_store_name (const char *name, ent_t *ent)
662 int namelen = strlen (name);
665 /* first call, setup cache */
666 if (ent->blacklist.size == 0)
668 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
669 ent->blacklist.data = malloc (ent->blacklist.size);
670 if (ent->blacklist.data == NULL)
672 ent->blacklist.data[0] = '|';
673 ent->blacklist.data[1] = '\0';
674 ent->blacklist.current = 1;
678 if (in_blacklist (name, namelen, ent))
679 return; /* no duplicates */
681 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
683 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
684 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
687 free (ent->blacklist.data);
688 ent->blacklist.size = 0;
691 ent->blacklist.data = tmp;
695 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
698 ent->blacklist.current += namelen + 1;
703 /* returns TRUE if ent->blacklist contains name, else FALSE */
705 in_blacklist (const char *name, int namelen, ent_t *ent)
707 char buf[namelen + 3];
710 if (ent->blacklist.data == NULL)
714 cp = stpcpy (&buf[1], name);
717 return strstr (ent->blacklist.data, buf) != NULL;