]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_compat/compat-grp.c
Update.
[thirdparty/glibc.git] / nis / nss_compat / compat-grp.c
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.
4
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.
9
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.
14
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. */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <nss.h>
23 #include <grp.h>
24 #include <ctype.h>
25 #include <bits/libc-lock.h>
26 #include <string.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
29 #include <rpcsvc/nis.h>
30 #include <nsswitch.h>
31
32 #include "nss-nisplus.h"
33 #include "nisplus-parser.h"
34
35 static service_user *ni = NULL;
36 static bool_t use_nisplus = FALSE; /* default: group_compat: nis */
37 static nis_name grptable = NULL; /* Name of the group table */
38 static size_t grptablelen = 0;
39
40 /* Get the declaration of the parser function. */
41 #define ENTNAME grent
42 #define STRUCTURE group
43 #define EXTERN_PARSER
44 #include "../../nss/nss_files/files-parse.c"
45
46 /* Structure for remembering -group members ... */
47 #define BLACKLIST_INITIAL_SIZE 512
48 #define BLACKLIST_INCREMENT 256
49 struct blacklist_t
50 {
51 char *data;
52 int current;
53 int size;
54 };
55
56 struct ent_t
57 {
58 bool_t nis;
59 bool_t nis_first;
60 char *oldkey;
61 int oldkeylen;
62 nis_result *result;
63 FILE *stream;
64 struct blacklist_t blacklist;
65 };
66 typedef struct ent_t ent_t;
67
68 static ent_t ext_ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
69
70 /* Protect global state against multiple changers. */
71 __libc_lock_define_initialized (static, lock)
72
73 /* Prototypes for local functions. */
74 static void blacklist_store_name (const char *, ent_t *);
75 static int in_blacklist (const char *, int, ent_t *);
76
77 static enum nss_status
78 _nss_first_init (void)
79 {
80 if (ni == NULL)
81 {
82 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
83 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
84 }
85
86 if (grptable == NULL)
87 {
88 char buf [20 + strlen (nis_local_directory ())];
89 char *p;
90
91 p = stpcpy (buf, "group.org_dir.");
92 p = stpcpy (p, nis_local_directory ());
93 grptable = strdup (buf);
94 if (grptable == NULL)
95 return NSS_STATUS_TRYAGAIN;
96 grptablelen = strlen (grptable);
97 }
98
99 return NSS_STATUS_SUCCESS;
100 }
101
102 static enum nss_status
103 internal_setgrent (ent_t *ent)
104 {
105 enum nss_status status = NSS_STATUS_SUCCESS;
106
107 ent->nis = ent->nis_first = 0;
108
109 if (_nss_first_init () != NSS_STATUS_SUCCESS)
110 return NSS_STATUS_UNAVAIL;
111
112 if (ent->oldkey != NULL)
113 {
114 free (ent->oldkey);
115 ent->oldkey = NULL;
116 ent->oldkeylen = 0;
117 }
118
119 if (ent->result != NULL)
120 {
121 nis_freeresult (ent->result);
122 ent->result = NULL;
123 }
124
125 ent->blacklist.current = 0;
126 if (ent->blacklist.data != NULL)
127 ent->blacklist.data[0] = '\0';
128
129 if (ent->stream == NULL)
130 {
131 ent->stream = fopen ("/etc/group", "r");
132
133 if (ent->stream == NULL)
134 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
135 else
136 {
137 /* We have to make sure the file is `closed on exec'. */
138 int result, flags;
139
140 result = flags = fcntl (fileno (ent->stream), F_GETFD, 0);
141 if (result >= 0)
142 {
143 flags |= FD_CLOEXEC;
144 result = fcntl (fileno (ent->stream), F_SETFD, flags);
145 }
146 if (result < 0)
147 {
148 /* Something went wrong. Close the stream and return a
149 failure. */
150 fclose (ent->stream);
151 ent->stream = NULL;
152 status = NSS_STATUS_UNAVAIL;
153 }
154 }
155 }
156 else
157 rewind (ent->stream);
158
159 return status;
160 }
161
162
163 enum nss_status
164 _nss_compat_setgrent (void)
165 {
166 enum nss_status result;
167
168 __libc_lock_lock (lock);
169
170 result = internal_setgrent (&ext_ent);
171
172 __libc_lock_unlock (lock);
173
174 return result;
175 }
176
177
178 static enum nss_status
179 internal_endgrent (ent_t *ent)
180 {
181 if (ent->stream != NULL)
182 {
183 fclose (ent->stream);
184 ent->stream = NULL;
185 }
186
187 ent->nis = ent->nis_first = 0;
188
189 if (ent->oldkey != NULL)
190 {
191 free (ent->oldkey);
192 ent->oldkey = NULL;
193 ent->oldkeylen = 0;
194 }
195
196 if (ent->result != NULL)
197 {
198 nis_freeresult (ent->result);
199 ent->result = NULL;
200 }
201
202 ent->blacklist.current = 0;
203 if (ent->blacklist.data != NULL)
204 ent->blacklist.data[0] = '\0';
205
206 return NSS_STATUS_SUCCESS;
207 }
208
209 enum nss_status
210 _nss_compat_endgrent (void)
211 {
212 enum nss_status result;
213
214 __libc_lock_lock (lock);
215
216 result = internal_endgrent (&ext_ent);
217
218 __libc_lock_unlock (lock);
219
220 return result;
221 }
222
223 static enum nss_status
224 getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
225 size_t buflen)
226 {
227 struct parser_data *data = (void *) buffer;
228 char *domain;
229 char *outkey, *outval;
230 int outkeylen, outvallen, parse_res;
231 char *p;
232
233 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
234 {
235 ent->nis = 0;
236 return NSS_STATUS_NOTFOUND;
237 }
238
239 do
240 {
241 char *save_oldkey;
242 int save_oldlen;
243 bool_t save_nis_first;
244
245 if (ent->nis_first)
246 {
247 if (yp_first (domain, "group.byname", &outkey, &outkeylen,
248 &outval, &outvallen) != YPERR_SUCCESS)
249 {
250 ent->nis = 0;
251 return NSS_STATUS_UNAVAIL;
252 }
253 save_oldkey = ent->oldkey;
254 save_oldlen = ent->oldkeylen;
255 save_nis_first = TRUE;
256 ent->oldkey = outkey;
257 ent->oldkeylen = outkeylen;
258 ent->nis_first = FALSE;
259 }
260 else
261 {
262 if (yp_next (domain, "group.byname", ent->oldkey, ent->oldkeylen,
263 &outkey, &outkeylen, &outval, &outvallen)
264 != YPERR_SUCCESS)
265 {
266 ent->nis = 0;
267 return NSS_STATUS_NOTFOUND;
268 }
269
270 save_oldkey = ent->oldkey;
271 save_oldlen = ent->oldkeylen;
272 save_nis_first = FALSE;
273 ent->oldkey = outkey;
274 ent->oldkeylen = outkeylen;
275 }
276
277 /* Copy the found data to our buffer */
278 p = strncpy (buffer, outval, buflen);
279
280 /* ...and free the data. */
281 free (outval);
282
283 while (isspace (*p))
284 ++p;
285
286 if ((parse_res = _nss_files_parse_grent (p, result, data, buflen)) == -1)
287 {
288 free (ent->oldkey);
289 ent->oldkey = save_oldkey;
290 ent->oldkeylen = save_oldlen;
291 ent->nis_first = save_nis_first;
292 __set_errno (ERANGE);
293 return NSS_STATUS_TRYAGAIN;
294 }
295 else
296 {
297 if (!save_nis_first)
298 free (save_oldkey);
299 }
300
301 if (parse_res &&
302 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
303 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
304 }
305 while (!parse_res);
306
307 return NSS_STATUS_SUCCESS;
308 }
309
310 static enum nss_status
311 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
312 size_t buflen)
313 {
314 int parse_res;
315
316 do
317 {
318 nis_result *save_oldres;
319 bool_t save_nis_first;
320
321 if (ent->nis_first)
322 {
323 save_oldres = ent->result;
324 save_nis_first = TRUE;
325 ent->result = nis_first_entry(grptable);
326 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
327 {
328 ent->nis = 0;
329 return niserr2nss (ent->result->status);
330 }
331 ent->nis_first = FALSE;
332 }
333 else
334 {
335 nis_result *res;
336
337 save_oldres = ent->result;
338 save_nis_first = FALSE;
339 res = nis_next_entry(grptable, &ent->result->cookie);
340 ent->result = res;
341 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
342 {
343 ent->nis = 0;
344 return niserr2nss (ent->result->status);
345 }
346 }
347 if ((parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
348 buffer, buflen)) == -1)
349 {
350 nis_freeresult (ent->result);
351 ent->result = save_oldres;
352 ent->nis_first = save_nis_first;
353 __set_errno (ERANGE);
354 return NSS_STATUS_TRYAGAIN;
355 }
356 else
357 {
358 if (!save_nis_first)
359 nis_freeresult (save_oldres);
360 }
361
362 if (parse_res &&
363 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
364 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
365 }
366 while (!parse_res);
367
368 return NSS_STATUS_SUCCESS;
369 }
370
371 /* This function handle the +group entrys in /etc/group */
372 static enum nss_status
373 getgrent_next_file_plusgroup (struct group *result, char *buffer,
374 size_t buflen)
375 {
376 struct parser_data *data = (void *) buffer;
377 int parse_res;
378
379 if (use_nisplus) /* Do the NIS+ query here */
380 {
381 nis_result *res;
382 char buf[strlen (result->gr_name) + 24 + grptablelen];
383
384 sprintf(buf, "[name=%s],%s", &result->gr_name[1], grptable);
385 res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
386 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
387 {
388 enum nss_status status = niserr2nss (res->status);
389
390 nis_freeresult (res);
391 return status;
392 }
393 if ((parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer,
394 buflen)) == -1)
395 {
396 __set_errno (ERANGE);
397 nis_freeresult (res);
398 return NSS_STATUS_TRYAGAIN;
399 }
400 nis_freeresult (res);
401 }
402 else /* Use NIS */
403 {
404 char *domain, *outval, *p;
405 int outvallen;
406
407 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
408 return NSS_STATUS_TRYAGAIN;
409
410 if (yp_match (domain, "group.byname", &result->gr_name[1],
411 strlen (result->gr_name) - 1, &outval, &outvallen)
412 != YPERR_SUCCESS)
413 return NSS_STATUS_TRYAGAIN;
414 p = strncpy (buffer, outval,
415 buflen < (size_t) outvallen ? buflen : (size_t) outvallen);
416 free (outval);
417 while (isspace (*p))
418 p++;
419 if ((parse_res = _nss_files_parse_grent (p, result, data, buflen)) == -1)
420 {
421 __set_errno (ERANGE);
422 return NSS_STATUS_TRYAGAIN;
423 }
424 }
425
426 if (parse_res)
427 /* We found the entry. */
428 return NSS_STATUS_SUCCESS;
429 else
430 return NSS_STATUS_RETURN;
431 }
432
433
434 static enum nss_status
435 getgrent_next_file (struct group *result, ent_t *ent,
436 char *buffer, size_t buflen)
437 {
438 struct parser_data *data = (void *) buffer;
439 while (1)
440 {
441 fpos_t pos;
442 int parse_res = 0;
443 char *p;
444
445 do
446 {
447 fgetpos (ent->stream, &pos);
448 p = fgets (buffer, buflen, ent->stream);
449 if (p == NULL)
450 {
451 if (feof (ent->stream))
452 return NSS_STATUS_NOTFOUND;
453 else
454 {
455 __set_errno (ERANGE);
456 return NSS_STATUS_TRYAGAIN;
457 }
458 }
459
460 /* Terminate the line for any case. */
461 buffer[buflen - 1] = '\0';
462
463 /* Skip leading blanks. */
464 while (isspace (*p))
465 ++p;
466 }
467 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
468 /* Parse the line. If it is invalid, loop to
469 get the next line of the file to parse. */
470 !(parse_res = _nss_files_parse_grent (p, result, data, buflen)));
471
472 if (parse_res == -1)
473 {
474 /* The parser ran out of space. */
475 fsetpos (ent->stream, &pos);
476 __set_errno (ERANGE);
477 return NSS_STATUS_TRYAGAIN;
478 }
479
480 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
481 /* This is a real entry. */
482 break;
483
484 /* -group */
485 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
486 && result->gr_name[1] != '@')
487 {
488 blacklist_store_name (&result->gr_name[1], ent);
489 continue;
490 }
491
492 /* +group */
493 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
494 && result->gr_name[1] != '@')
495 {
496 enum nss_status status;
497
498 status = getgrent_next_file_plusgroup (result, buffer, buflen);
499 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
500 break;
501 else
502 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
503 continue;
504 else
505 return status;
506 }
507
508 /* +:... */
509 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
510 {
511 ent->nis = TRUE;
512 ent->nis_first = TRUE;
513
514 if (use_nisplus)
515 return getgrent_next_nisplus (result, ent, buffer, buflen);
516 else
517 return getgrent_next_nis (result, ent, buffer, buflen);
518 }
519 }
520
521 return NSS_STATUS_SUCCESS;
522 }
523
524
525 static enum nss_status
526 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
527 size_t buflen)
528 {
529 if (ent->nis)
530 {
531 if (use_nisplus)
532 return getgrent_next_nisplus (gr, ent, buffer, buflen);
533 else
534 return getgrent_next_nis (gr, ent, buffer, buflen);
535 }
536 else
537 return getgrent_next_file (gr, ent, buffer, buflen);
538 }
539
540 enum nss_status
541 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen)
542 {
543 enum nss_status status = NSS_STATUS_SUCCESS;
544
545 __libc_lock_lock (lock);
546
547 /* Be prepared that the setgrent function was not called before. */
548 if (ext_ent.stream == NULL)
549 status = internal_setgrent (&ext_ent);
550
551 if (status == NSS_STATUS_SUCCESS)
552 status = internal_getgrent_r (grp, &ext_ent, buffer, buflen);
553
554 __libc_lock_unlock (lock);
555
556 return status;
557 }
558
559
560 enum nss_status
561 _nss_compat_getgrnam_r (const char *name, struct group *grp,
562 char *buffer, size_t buflen)
563 {
564 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
565 enum nss_status status;
566
567 if (name[0] == '-' || name[0] == '+')
568 return NSS_STATUS_NOTFOUND;
569
570 __libc_lock_lock (lock);
571
572 status = internal_setgrent (&ent);
573
574 __libc_lock_unlock (lock);
575
576 if (status != NSS_STATUS_SUCCESS)
577 return status;
578
579 while ((status = internal_getgrent_r (grp, &ent, buffer, buflen))
580 == NSS_STATUS_SUCCESS)
581 if (strcmp (grp->gr_name, name) == 0)
582 break;
583
584 internal_endgrent (&ent);
585 return status;
586 }
587
588
589 enum nss_status
590 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
591 char *buffer, size_t buflen)
592 {
593 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
594 enum nss_status status;
595
596 __libc_lock_lock (lock);
597
598 status = internal_setgrent (&ent);
599
600 __libc_lock_unlock (lock);
601
602 if (status != NSS_STATUS_SUCCESS)
603 return status;
604
605 while ((status = internal_getgrent_r (grp, &ent, buffer, buflen))
606 == NSS_STATUS_SUCCESS)
607 if (grp->gr_gid == gid && grp->gr_name[0] != '+' && grp->gr_name[0] != '-')
608 break;
609
610 internal_endgrent (&ent);
611 return status;
612 }
613
614
615 /* Support routines for remembering -@netgroup and -user entries.
616 The names are stored in a single string with `|' as separator. */
617 static void
618 blacklist_store_name (const char *name, ent_t *ent)
619 {
620 int namelen = strlen (name);
621 char *tmp;
622
623 /* first call, setup cache */
624 if (ent->blacklist.size == 0)
625 {
626 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
627 ent->blacklist.data = malloc (ent->blacklist.size);
628 if (ent->blacklist.data == NULL)
629 return;
630 ent->blacklist.data[0] = '|';
631 ent->blacklist.data[1] = '\0';
632 ent->blacklist.current = 1;
633 }
634 else
635 {
636 if (in_blacklist (name, namelen, ent))
637 return; /* no duplicates */
638
639 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
640 {
641 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
642 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
643 if (tmp == NULL)
644 {
645 free (ent->blacklist.data);
646 ent->blacklist.size = 0;
647 return;
648 }
649 ent->blacklist.data = tmp;
650 }
651 }
652
653 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
654 *tmp++ = '|';
655 *tmp = '\0';
656 ent->blacklist.current += namelen + 1;
657
658 return;
659 }
660
661 /* returns TRUE if ent->blacklist contains name, else FALSE */
662 static bool_t
663 in_blacklist (const char *name, int namelen, ent_t *ent)
664 {
665 char buf[namelen + 3];
666 char *cp;
667
668 if (ent->blacklist.data == NULL)
669 return FALSE;
670
671 buf[0] = '|';
672 cp = stpcpy (&buf[1], name);
673 *cp++= '|';
674 *cp = '\0';
675 return strstr (ent->blacklist.data, buf) != NULL;
676 }