Update.
[glibc.git] / nis / nss_compat / compat-grp.c
blob68f852d19a734bf5385009e7f742b7aea069f84c
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
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. */
20 #include <errno.h>
21 #include <nss.h>
22 #include <grp.h>
23 #include <ctype.h>
24 #include <libc-lock.h>
25 #include <string.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
28 #include <rpcsvc/nis.h>
29 #include <rpcsvc/nislib.h>
30 #include <nsswitch.h>
32 #include "nss-nisplus.h"
34 static service_user *ni = NULL;
35 static bool_t use_nisplus = FALSE; /* default: group_compat: nis */
37 /* Get the declaration of the parser function. */
38 #define ENTNAME grent
39 #define STRUCTURE group
40 #define EXTERN_PARSER
41 #include "../../nss/nss_files/files-parse.c"
43 /* Structure for remembering -group members ... */
44 #define BLACKLIST_INITIAL_SIZE 512
45 #define BLACKLIST_INCREMENT 256
46 struct blacklist_t
48 char *data;
49 int current;
50 int size;
53 struct ent_t
55 bool_t nis;
56 bool_t nis_first;
57 char *oldkey;
58 int oldkeylen;
59 nis_result *result;
60 nis_name *names;
61 u_long names_nr;
62 FILE *stream;
63 struct blacklist_t blacklist;
65 typedef struct ent_t ent_t;
67 static ent_t ext_ent = {0, 0, NULL, 0, NULL, NULL, 0, NULL, {NULL, 0, 0}};
69 /* Protect global state against multiple changers. */
70 __libc_lock_define_initialized (static, lock)
72 /* Prototypes for local functions. */
73 static void blacklist_store_name (const char *, ent_t *);
74 static int in_blacklist (const char *, int, ent_t *);
75 extern int _nss_nisplus_parse_grent (nis_result *, struct group *,
76 char *, size_t);
78 static enum nss_status
79 internal_setgrent (ent_t *ent)
81 enum nss_status status = NSS_STATUS_SUCCESS;
83 ent->nis = ent->nis_first = 0;
85 if (ent->oldkey != NULL)
87 free (ent->oldkey);
88 ent->oldkey = NULL;
89 ent->oldkeylen = 0;
92 if (ent->result != NULL)
94 nis_freeresult (ent->result);
95 ent->result = NULL;
98 if (ent->names != NULL)
100 nis_freenames (ent->names);
101 ent->names = NULL;
103 ent->names_nr = 0;
104 ent->blacklist.current = 0;
105 if (ent->blacklist.data != NULL)
106 ent->blacklist.data[0] = '\0';
108 if (ent->stream == NULL)
110 ent->stream = fopen ("/etc/group", "r");
112 if (ent->stream == NULL)
113 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
115 else
116 rewind (ent->stream);
118 return status;
122 enum nss_status
123 _nss_compat_setgrent (void)
125 enum nss_status result;
127 __libc_lock_lock (lock);
129 if (ni == NULL)
131 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
132 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
135 result = internal_setgrent (&ext_ent);
137 __libc_lock_unlock (lock);
139 return result;
143 static enum nss_status
144 internal_endgrent (ent_t *ent)
146 if (ent->stream != NULL)
148 fclose (ent->stream);
149 ent->stream = NULL;
152 ent->nis = ent->nis_first = 0;
154 if (ent->oldkey != NULL)
156 free (ent->oldkey);
157 ent->oldkey = NULL;
158 ent->oldkeylen = 0;
161 if (ent->result != NULL)
163 nis_freeresult (ent->result);
164 ent->result = NULL;
167 if (ent->names != NULL)
169 nis_freenames (ent->names);
170 ent->names = NULL;
172 ent->names_nr = 0;
173 ent->blacklist.current = 0;
174 if (ent->blacklist.data != NULL)
175 ent->blacklist.data[0] = '\0';
177 return NSS_STATUS_SUCCESS;
180 enum nss_status
181 _nss_compat_endgrent (void)
183 enum nss_status result;
185 __libc_lock_lock (lock);
187 result = internal_endgrent (&ext_ent);
189 __libc_lock_unlock (lock);
191 return result;
194 static enum nss_status
195 getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
196 size_t buflen)
198 struct parser_data *data = (void *) buffer;
199 char *domain;
200 char *outkey, *outval;
201 int outkeylen, outvallen, parse_res;
202 char *p;
204 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
206 ent->nis = 0;
207 return NSS_STATUS_NOTFOUND;
212 if (ent->nis_first)
214 if (yp_first (domain, "group.byname", &outkey, &outkeylen,
215 &outval, &outvallen) != YPERR_SUCCESS)
217 ent->nis = 0;
218 return NSS_STATUS_UNAVAIL;
221 ent->oldkey = outkey;
222 ent->oldkeylen = outkeylen;
223 ent->nis_first = FALSE;
225 else
227 if (yp_next (domain, "group.byname", ent->oldkey, ent->oldkeylen,
228 &outkey, &outkeylen, &outval, &outvallen)
229 != YPERR_SUCCESS)
231 ent->nis = 0;
232 return NSS_STATUS_NOTFOUND;
235 free (ent->oldkey);
236 ent->oldkey = outkey;
237 ent->oldkeylen = outkeylen;
240 /* Copy the found data to our buffer */
241 p = strncpy (buffer, outval, buflen);
243 /* ...and free the data. */
244 free (outval);
246 while (isspace (*p))
247 ++p;
249 parse_res = _nss_files_parse_grent (p, result, data, buflen);
251 if (parse_res &&
252 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
253 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
255 while (!parse_res);
257 return NSS_STATUS_SUCCESS;
260 static enum nss_status
261 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
262 size_t buflen)
264 int parse_res;
266 if (ent->names == NULL)
268 ent->names = nis_getnames ("group.org_dir");
269 if (ent->names == NULL || ent->names[0] == NULL)
271 ent->nis = 0;
272 return NSS_STATUS_UNAVAIL;
278 if (ent->nis_first)
280 next_name:
281 ent->result = nis_first_entry(ent->names[ent->names_nr]);
282 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
284 ent->nis = 0;
285 return niserr2nss (ent->result->status);
287 ent->nis_first = FALSE;
289 else
291 nis_result *res;
293 res = nis_next_entry(ent->names[ent->names_nr],
294 &ent->result->cookie);
295 nis_freeresult (ent->result);
296 ent->result = res;
297 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
299 if ((ent->result->status == NIS_NOTFOUND) &&
300 ent->names[ent->names_nr + 1] != NULL)
302 nis_freeresult (ent->result);
303 ent->names_nr += 1;
304 goto next_name;
306 else
308 ent->nis = 0;
309 return niserr2nss (ent->result->status);
313 parse_res = _nss_nisplus_parse_grent (ent->result, result, buffer,
314 buflen);
315 if (parse_res &&
316 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
317 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
319 while (!parse_res);
321 return NSS_STATUS_SUCCESS;
324 /* This function handle the +group entrys in /etc/group */
325 static enum nss_status
326 getgrent_next_file_plusgroup (struct group *result, char *buffer,
327 size_t buflen)
329 struct parser_data *data = (void *) buffer;
330 int parse_res;
332 if (use_nisplus) /* Do the NIS+ query here */
334 nis_result *res;
335 char buf[strlen (result->gr_name) + 24];
337 sprintf(buf, "[name=%s],group.org_dir",
338 &result->gr_name[1]);
339 res = nis_list(buf, EXPAND_NAME, NULL, NULL);
340 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
342 enum nss_status status = niserr2nss (res->status);
344 nis_freeresult (res);
345 return status;
347 parse_res = _nss_nisplus_parse_grent (res, result, buffer, buflen);
348 nis_freeresult (res);
350 else /* Use NIS */
352 char *domain, *outval, *p;
353 int outvallen;
355 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
356 return NSS_STATUS_TRYAGAIN;
358 if (yp_match (domain, "group.byname", &result->gr_name[1],
359 strlen (result->gr_name) - 1, &outval, &outvallen)
360 != YPERR_SUCCESS)
361 return NSS_STATUS_TRYAGAIN;
362 p = strncpy (buffer, outval,
363 buflen < (size_t) outvallen ? buflen : (size_t) outvallen);
364 free (outval);
365 while (isspace (*p))
366 p++;
367 parse_res = _nss_files_parse_grent (p, result, data, buflen);
370 if (parse_res)
371 /* We found the entry. */
372 return NSS_STATUS_SUCCESS;
373 else
374 return NSS_STATUS_RETURN;
378 static enum nss_status
379 getgrent_next_file (struct group *result, ent_t *ent,
380 char *buffer, size_t buflen)
382 struct parser_data *data = (void *) buffer;
383 while (1)
385 char *p;
389 p = fgets (buffer, buflen, ent->stream);
390 if (p == NULL)
391 return NSS_STATUS_NOTFOUND;
393 /* Terminate the line for any case. */
394 buffer[buflen - 1] = '\0';
396 /* Skip leading blanks. */
397 while (isspace (*p))
398 ++p;
400 /* Ignore empty and comment lines. */
401 while (*p == '\0' || *p == '#' ||
402 /* Parse the line. If it is invalid, loop to
403 get the next line of the file to parse. */
404 !_nss_files_parse_grent (p, result, data, buflen));
406 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
407 /* This is a real entry. */
408 break;
410 /* -group */
411 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
412 && result->gr_name[1] != '@')
414 blacklist_store_name (&result->gr_name[1], ent);
415 continue;
418 /* +group */
419 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
420 && result->gr_name[1] != '@')
422 enum nss_status status;
424 status = getgrent_next_file_plusgroup (result, buffer, buflen);
425 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
426 break;
427 else
428 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
429 continue;
430 else
431 return status;
434 /* +:... */
435 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
437 ent->nis = TRUE;
438 ent->nis_first = TRUE;
440 if (use_nisplus)
441 return getgrent_next_nisplus (result, ent, buffer, buflen);
442 else
443 return getgrent_next_nis (result, ent, buffer, buflen);
447 return NSS_STATUS_SUCCESS;
451 static enum nss_status
452 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
453 size_t buflen)
455 if (ent->nis)
457 if (use_nisplus)
458 return getgrent_next_nisplus (gr, ent, buffer, buflen);
459 else
460 return getgrent_next_nis (gr, ent, buffer, buflen);
462 else
463 return getgrent_next_file (gr, ent, buffer, buflen);
466 enum nss_status
467 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen)
469 enum nss_status status = NSS_STATUS_SUCCESS;
471 __libc_lock_lock (lock);
473 if (ni == NULL)
475 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
476 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
479 /* Be prepared that the setgrent function was not called before. */
480 if (ext_ent.stream == NULL)
481 status = internal_setgrent (&ext_ent);
483 if (status == NSS_STATUS_SUCCESS)
484 status = internal_getgrent_r (grp, &ext_ent, buffer, buflen);
486 __libc_lock_unlock (lock);
488 return status;
492 enum nss_status
493 _nss_compat_getgrnam_r (const char *name, struct group *grp,
494 char *buffer, size_t buflen)
496 ent_t ent = {0, 0, NULL, 0, NULL, NULL, 0, NULL, {NULL, 0, 0}};
497 enum nss_status status;
499 if (name[0] == '-' || name[0] == '+')
500 return NSS_STATUS_NOTFOUND;
502 __libc_lock_lock (lock);
504 if (ni == NULL)
506 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
507 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
510 __libc_lock_unlock (lock);
512 status = internal_setgrent (&ent);
513 if (status != NSS_STATUS_SUCCESS)
514 return status;
516 while ((status = internal_getgrent_r (grp, &ent, buffer, buflen))
517 == NSS_STATUS_SUCCESS)
518 if (strcmp (grp->gr_name, name) == 0)
519 break;
521 internal_endgrent (&ent);
522 return status;
526 enum nss_status
527 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
528 char *buffer, size_t buflen)
530 ent_t ent = {0, 0, NULL, 0, NULL, NULL, 0, NULL, {NULL, 0, 0}};
531 enum nss_status status;
533 __libc_lock_lock (lock);
535 if (ni == NULL)
537 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
538 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
541 __libc_lock_unlock (lock);
543 status = internal_setgrent (&ent);
544 if (status != NSS_STATUS_SUCCESS)
545 return status;
547 while ((status = internal_getgrent_r (grp, &ent, buffer, buflen))
548 == NSS_STATUS_SUCCESS)
549 if (grp->gr_gid == gid && grp->gr_name[0] != '+' && grp->gr_name[0] != '-')
550 break;
552 internal_endgrent (&ent);
553 return status;
557 /* Support routines for remembering -@netgroup and -user entries.
558 The names are stored in a single string with `|' as separator. */
559 static void
560 blacklist_store_name (const char *name, ent_t *ent)
562 int namelen = strlen (name);
563 char *tmp;
565 /* first call, setup cache */
566 if (ent->blacklist.size == 0)
568 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
569 ent->blacklist.data = malloc (ent->blacklist.size);
570 if (ent->blacklist.data == NULL)
571 return;
572 ent->blacklist.data[0] = '|';
573 ent->blacklist.data[1] = '\0';
574 ent->blacklist.current = 1;
576 else
578 if (in_blacklist (name, namelen, ent))
579 return; /* no duplicates */
581 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
583 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
584 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
585 if (tmp == NULL)
587 free (ent->blacklist.data);
588 ent->blacklist.size = 0;
589 return;
591 ent->blacklist.data = tmp;
595 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
596 *tmp++ = '|';
597 *tmp = '\0';
598 ent->blacklist.current += namelen + 1;
600 return;
603 /* returns TRUE if ent->blacklist contains name, else FALSE */
604 static bool_t
605 in_blacklist (const char *name, int namelen, ent_t *ent)
607 char buf[namelen + 3];
608 char *cp;
610 if (ent->blacklist.data == NULL)
611 return FALSE;
613 buf[0] = '|';
614 cp = stpcpy (&buf[1], name);
615 *cp++= '|';
616 *cp = '\0';
617 return strstr (ent->blacklist.data, buf) != NULL;