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. */
24 #include <libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
28 #include <rpcsvc/nis.h>
29 #include <rpcsvc/nislib.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. */
39 #define STRUCTURE group
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
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
*,
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
)
92 if (ent
->result
!= NULL
)
94 nis_freeresult (ent
->result
);
98 if (ent
->names
!= NULL
)
100 nis_freenames (ent
->names
);
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
;
116 rewind (ent
->stream
);
123 _nss_compat_setgrent (void)
125 enum nss_status result
;
127 __libc_lock_lock (lock
);
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
);
143 static enum nss_status
144 internal_endgrent (ent_t
*ent
)
146 if (ent
->stream
!= NULL
)
148 fclose (ent
->stream
);
152 ent
->nis
= ent
->nis_first
= 0;
154 if (ent
->oldkey
!= NULL
)
161 if (ent
->result
!= NULL
)
163 nis_freeresult (ent
->result
);
167 if (ent
->names
!= NULL
)
169 nis_freenames (ent
->names
);
173 ent
->blacklist
.current
= 0;
174 if (ent
->blacklist
.data
!= NULL
)
175 ent
->blacklist
.data
[0] = '\0';
177 return NSS_STATUS_SUCCESS
;
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
);
194 static enum nss_status
195 getgrent_next_nis (struct group
*result
, ent_t
*ent
, char *buffer
,
198 struct parser_data
*data
= (void *) buffer
;
200 char *outkey
, *outval
;
201 int outkeylen
, outvallen
, parse_res
;
204 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
207 return NSS_STATUS_NOTFOUND
;
214 if (yp_first (domain
, "group.byname", &outkey
, &outkeylen
,
215 &outval
, &outvallen
) != YPERR_SUCCESS
)
218 return NSS_STATUS_UNAVAIL
;
221 ent
->oldkey
= outkey
;
222 ent
->oldkeylen
= outkeylen
;
223 ent
->nis_first
= FALSE
;
227 if (yp_next (domain
, "group.byname", ent
->oldkey
, ent
->oldkeylen
,
228 &outkey
, &outkeylen
, &outval
, &outvallen
)
232 return NSS_STATUS_NOTFOUND
;
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. */
249 parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
);
252 in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
253 parse_res
= 0; /* if result->gr_name in blacklist,search next entry */
257 return NSS_STATUS_SUCCESS
;
260 static enum nss_status
261 getgrent_next_nisplus (struct group
*result
, ent_t
*ent
, char *buffer
,
266 if (ent
->names
== NULL
)
268 ent
->names
= nis_getnames ("group.org_dir");
269 if (ent
->names
== NULL
|| ent
->names
[0] == NULL
)
272 return NSS_STATUS_UNAVAIL
;
281 ent
->result
= nis_first_entry(ent
->names
[ent
->names_nr
]);
282 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
285 return niserr2nss (ent
->result
->status
);
287 ent
->nis_first
= FALSE
;
293 res
= nis_next_entry(ent
->names
[ent
->names_nr
],
294 &ent
->result
->cookie
);
295 nis_freeresult (ent
->result
);
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
);
309 return niserr2nss (ent
->result
->status
);
313 parse_res
= _nss_nisplus_parse_grent (ent
->result
, result
, buffer
,
316 in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
317 parse_res
= 0; /* if result->gr_name in blacklist,search next entry */
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
,
329 struct parser_data
*data
= (void *) buffer
;
332 if (use_nisplus
) /* Do the NIS+ query here */
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
);
347 parse_res
= _nss_nisplus_parse_grent (res
, result
, buffer
, buflen
);
348 nis_freeresult (res
);
352 char *domain
, *outval
, *p
;
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
)
361 return NSS_STATUS_TRYAGAIN
;
362 p
= strncpy (buffer
, outval
,
363 buflen
< (size_t) outvallen
? buflen
: (size_t) outvallen
);
367 parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
);
371 /* We found the entry. */
372 return NSS_STATUS_SUCCESS
;
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
;
389 p
= fgets (buffer
, buflen
, ent
->stream
);
391 return NSS_STATUS_NOTFOUND
;
393 /* Terminate the line for any case. */
394 buffer
[buflen
- 1] = '\0';
396 /* Skip leading blanks. */
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. */
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
);
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. */
428 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
435 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
438 ent
->nis_first
= TRUE
;
441 return getgrent_next_nisplus (result
, ent
, buffer
, buflen
);
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
,
458 return getgrent_next_nisplus (gr
, ent
, buffer
, buflen
);
460 return getgrent_next_nis (gr
, ent
, buffer
, buflen
);
463 return getgrent_next_file (gr
, ent
, buffer
, buflen
);
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
);
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
);
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
);
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
)
516 while ((status
= internal_getgrent_r (grp
, &ent
, buffer
, buflen
))
517 == NSS_STATUS_SUCCESS
)
518 if (strcmp (grp
->gr_name
, name
) == 0)
521 internal_endgrent (&ent
);
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
);
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
)
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] != '-')
552 internal_endgrent (&ent
);
557 /* Support routines for remembering -@netgroup and -user entries.
558 The names are stored in a single string with `|' as separator. */
560 blacklist_store_name (const char *name
, ent_t
*ent
)
562 int namelen
= strlen (name
);
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
)
572 ent
->blacklist
.data
[0] = '|';
573 ent
->blacklist
.data
[1] = '\0';
574 ent
->blacklist
.current
= 1;
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
);
587 free (ent
->blacklist
.data
);
588 ent
->blacklist
.size
= 0;
591 ent
->blacklist
.data
= tmp
;
595 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
598 ent
->blacklist
.current
+= namelen
+ 1;
603 /* returns TRUE if ent->blacklist contains name, else FALSE */
605 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
607 char buf
[namelen
+ 3];
610 if (ent
->blacklist
.data
== NULL
)
614 cp
= stpcpy (&buf
[1], name
);
617 return strstr (ent
->blacklist
.data
, buf
) != NULL
;