]> 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 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 <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>
31
32 #include "nss-nisplus.h"
33
34 static service_user *ni = NULL;
35 static bool_t use_nisplus = FALSE; /* default: group_compat: nis */
36
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"
42
43 /* Structure for remembering -group members ... */
44 #define BLACKLIST_INITIAL_SIZE 512
45 #define BLACKLIST_INCREMENT 256
46 struct blacklist_t
47 {
48 char *data;
49 int current;
50 int size;
51 };
52
53 struct ent_t
54 {
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;
64 };
65 typedef struct ent_t ent_t;
66
67 static ent_t ext_ent = {0, 0, NULL, 0, NULL, NULL, 0, NULL, {NULL, 0, 0}};
68
69 /* Protect global state against multiple changers. */
70 __libc_lock_define_initialized (static, lock)
71
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);
77
78 static enum nss_status
79 internal_setgrent (ent_t *ent)
80 {
81 enum nss_status status = NSS_STATUS_SUCCESS;
82
83 ent->nis = ent->nis_first = 0;
84
85 if (ent->oldkey != NULL)
86 {
87 free (ent->oldkey);
88 ent->oldkey = NULL;
89 ent->oldkeylen = 0;
90 }
91
92 if (ent->result != NULL)
93 {
94 nis_freeresult (ent->result);
95 ent->result = NULL;
96 }
97
98 if (ent->names != NULL)
99 {
100 nis_freenames (ent->names);
101 ent->names = NULL;
102 }
103 ent->names_nr = 0;
104 ent->blacklist.current = 0;
105 if (ent->blacklist.data != NULL)
106 ent->blacklist.data[0] = '\0';
107
108 if (ent->stream == NULL)
109 {
110 ent->stream = fopen ("/etc/group", "r");
111
112 if (ent->stream == NULL)
113 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
114 }
115 else
116 rewind (ent->stream);
117
118 return status;
119 }
120
121
122 enum nss_status
123 _nss_compat_setgrent (void)
124 {
125 enum nss_status result;
126
127 __libc_lock_lock (lock);
128
129 if (ni == NULL)
130 {
131 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
132 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
133 }
134
135 result = internal_setgrent (&ext_ent);
136
137 __libc_lock_unlock (lock);
138
139 return result;
140 }
141
142
143 static enum nss_status
144 internal_endgrent (ent_t *ent)
145 {
146 if (ent->stream != NULL)
147 {
148 fclose (ent->stream);
149 ent->stream = NULL;
150 }
151
152 ent->nis = ent->nis_first = 0;
153
154 if (ent->oldkey != NULL)
155 {
156 free (ent->oldkey);
157 ent->oldkey = NULL;
158 ent->oldkeylen = 0;
159 }
160
161 if (ent->result != NULL)
162 {
163 nis_freeresult (ent->result);
164 ent->result = NULL;
165 }
166
167 if (ent->names != NULL)
168 {
169 nis_freenames (ent->names);
170 ent->names = NULL;
171 }
172 ent->names_nr = 0;
173 ent->blacklist.current = 0;
174 if (ent->blacklist.data != NULL)
175 ent->blacklist.data[0] = '\0';
176
177 return NSS_STATUS_SUCCESS;
178 }
179
180 enum nss_status
181 _nss_compat_endgrent (void)
182 {
183 enum nss_status result;
184
185 __libc_lock_lock (lock);
186
187 result = internal_endgrent (&ext_ent);
188
189 __libc_lock_unlock (lock);
190
191 return result;
192 }
193
194 static enum nss_status
195 getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
196 size_t buflen)
197 {
198 struct parser_data *data = (void *) buffer;
199 char *domain;
200 char *outkey, *outval;
201 int outkeylen, outvallen, parse_res;
202 char *p;
203
204 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
205 {
206 ent->nis = 0;
207 return NSS_STATUS_NOTFOUND;
208 }
209
210 do
211 {
212 if (ent->nis_first)
213 {
214 if (yp_first (domain, "group.byname", &outkey, &outkeylen,
215 &outval, &outvallen) != YPERR_SUCCESS)
216 {
217 ent->nis = 0;
218 return NSS_STATUS_UNAVAIL;
219 }
220
221 ent->oldkey = outkey;
222 ent->oldkeylen = outkeylen;
223 ent->nis_first = FALSE;
224 }
225 else
226 {
227 if (yp_next (domain, "group.byname", ent->oldkey, ent->oldkeylen,
228 &outkey, &outkeylen, &outval, &outvallen)
229 != YPERR_SUCCESS)
230 {
231 ent->nis = 0;
232 return NSS_STATUS_NOTFOUND;
233 }
234
235 free (ent->oldkey);
236 ent->oldkey = outkey;
237 ent->oldkeylen = outkeylen;
238 }
239
240 /* Copy the found data to our buffer */
241 p = strncpy (buffer, outval, buflen);
242
243 /* ...and free the data. */
244 free (outval);
245
246 while (isspace (*p))
247 ++p;
248
249 parse_res = _nss_files_parse_grent (p, result, data, buflen);
250
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 */
254 }
255 while (!parse_res);
256
257 return NSS_STATUS_SUCCESS;
258 }
259
260 static enum nss_status
261 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
262 size_t buflen)
263 {
264 int parse_res;
265
266 if (ent->names == NULL)
267 {
268 ent->names = nis_getnames ("group.org_dir");
269 if (ent->names == NULL || ent->names[0] == NULL)
270 {
271 ent->nis = 0;
272 return NSS_STATUS_UNAVAIL;
273 }
274 }
275
276 do
277 {
278 if (ent->nis_first)
279 {
280 next_name:
281 ent->result = nis_first_entry(ent->names[ent->names_nr]);
282 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
283 {
284 ent->nis = 0;
285 return niserr2nss (ent->result->status);
286 }
287 ent->nis_first = FALSE;
288 }
289 else
290 {
291 nis_result *res;
292
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)
298 {
299 if ((ent->result->status == NIS_NOTFOUND) &&
300 ent->names[ent->names_nr + 1] != NULL)
301 {
302 nis_freeresult (ent->result);
303 ent->names_nr += 1;
304 goto next_name;
305 }
306 else
307 {
308 ent->nis = 0;
309 return niserr2nss (ent->result->status);
310 }
311 }
312 }
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 */
318 }
319 while (!parse_res);
320
321 return NSS_STATUS_SUCCESS;
322 }
323
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)
328 {
329 struct parser_data *data = (void *) buffer;
330 int parse_res;
331
332 if (use_nisplus) /* Do the NIS+ query here */
333 {
334 nis_result *res;
335 char buf[strlen (result->gr_name) + 24];
336
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)
341 {
342 enum nss_status status = niserr2nss (res->status);
343
344 nis_freeresult (res);
345 return status;
346 }
347 parse_res = _nss_nisplus_parse_grent (res, result, buffer, buflen);
348 nis_freeresult (res);
349 }
350 else /* Use NIS */
351 {
352 char *domain, *outval, *p;
353 int outvallen;
354
355 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
356 return NSS_STATUS_TRYAGAIN;
357
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 < outvallen ? buflen : outvallen);
364 free (outval);
365 while (isspace (*p))
366 p++;
367 parse_res = _nss_files_parse_grent (p, result, data, buflen);
368 }
369
370 if (parse_res)
371 /* We found the entry. */
372 return NSS_STATUS_SUCCESS;
373 else
374 return NSS_STATUS_RETURN;
375 }
376
377
378 static enum nss_status
379 getgrent_next_file (struct group *result, ent_t *ent,
380 char *buffer, size_t buflen)
381 {
382 struct parser_data *data = (void *) buffer;
383 while (1)
384 {
385 char *p;
386
387 do
388 {
389 p = fgets (buffer, buflen, ent->stream);
390 if (p == NULL)
391 return NSS_STATUS_NOTFOUND;
392
393 /* Terminate the line for any case. */
394 buffer[buflen - 1] = '\0';
395
396 /* Skip leading blanks. */
397 while (isspace (*p))
398 ++p;
399 }
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));
405
406 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
407 /* This is a real entry. */
408 break;
409
410 /* -group */
411 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
412 && result->gr_name[1] != '@')
413 {
414 blacklist_store_name (&result->gr_name[1], ent);
415 continue;
416 }
417
418 /* +group */
419 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
420 && result->gr_name[1] != '@')
421 {
422 enum nss_status status;
423
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;
432 }
433
434 /* +:... */
435 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
436 {
437 ent->nis = TRUE;
438 ent->nis_first = TRUE;
439
440 if (use_nisplus)
441 return getgrent_next_nisplus (result, ent, buffer, buflen);
442 else
443 return getgrent_next_nis (result, ent, buffer, buflen);
444 }
445 }
446
447 return NSS_STATUS_SUCCESS;
448 }
449
450
451 static enum nss_status
452 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
453 size_t buflen)
454 {
455 if (ent->nis)
456 {
457 if (use_nisplus)
458 return getgrent_next_nisplus (gr, ent, buffer, buflen);
459 else
460 return getgrent_next_nis (gr, ent, buffer, buflen);
461 }
462 else
463 return getgrent_next_file (gr, ent, buffer, buflen);
464 }
465
466 enum nss_status
467 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen)
468 {
469 enum nss_status status = NSS_STATUS_SUCCESS;
470
471 __libc_lock_lock (lock);
472
473 if (ni == NULL)
474 {
475 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
476 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
477 }
478
479 /* Be prepared that the setgrent function was not called before. */
480 if (ext_ent.stream == NULL)
481 status = internal_setgrent (&ext_ent);
482
483 if (status == NSS_STATUS_SUCCESS)
484 status = internal_getgrent_r (grp, &ext_ent, buffer, buflen);
485
486 __libc_lock_unlock (lock);
487
488 return status;
489 }
490
491
492 enum nss_status
493 _nss_compat_getgrnam_r (const char *name, struct group *grp,
494 char *buffer, size_t buflen)
495 {
496 ent_t ent = {0, 0, NULL, 0, NULL, NULL, 0, NULL, {NULL, 0, 0}};
497 enum nss_status status;
498
499 if (name[0] == '-' || name[0] == '+')
500 return NSS_STATUS_NOTFOUND;
501
502 __libc_lock_lock (lock);
503
504 if (ni == NULL)
505 {
506 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
507 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
508 }
509
510 __libc_lock_unlock (lock);
511
512 status = internal_setgrent (&ent);
513 if (status != NSS_STATUS_SUCCESS)
514 return status;
515
516 while ((status = internal_getgrent_r (grp, &ent, buffer, buflen))
517 == NSS_STATUS_SUCCESS)
518 if (strcmp (grp->gr_name, name) == 0)
519 break;
520
521 internal_endgrent (&ent);
522 return status;
523 }
524
525
526 enum nss_status
527 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
528 char *buffer, size_t buflen)
529 {
530 ent_t ent = {0, 0, NULL, 0, NULL, NULL, 0, NULL, {NULL, 0, 0}};
531 enum nss_status status;
532
533 __libc_lock_lock (lock);
534
535 if (ni == NULL)
536 {
537 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
538 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
539 }
540
541 __libc_lock_unlock (lock);
542
543 status = internal_setgrent (&ent);
544 if (status != NSS_STATUS_SUCCESS)
545 return status;
546
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;
551
552 internal_endgrent (&ent);
553 return status;
554 }
555
556
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)
561 {
562 int namelen = strlen (name);
563 char *tmp;
564
565 /* first call, setup cache */
566 if (ent->blacklist.size == 0)
567 {
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;
575 }
576 else
577 {
578 if (in_blacklist (name, namelen, ent))
579 return; /* no duplicates */
580
581 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
582 {
583 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
584 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
585 if (tmp == NULL)
586 {
587 free (ent->blacklist.data);
588 ent->blacklist.size = 0;
589 return;
590 }
591 ent->blacklist.data = tmp;
592 }
593 }
594
595 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
596 *tmp++ = '|';
597 *tmp = '\0';
598 ent->blacklist.current += namelen + 1;
599
600 return;
601 }
602
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)
606 {
607 char buf[namelen + 3];
608
609 if (ent->blacklist.data == NULL)
610 return FALSE;
611
612 stpcpy (stpcpy (stpcpy (buf, "|"), name), "|");
613 return strstr (ent->blacklist.data, buf) != NULL;
614 }