]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_compat/compat-grp.c
4b873d69153820b002c6ace396cafb41fb3e69da
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. */
25 #include <bits/libc-lock.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
29 #include <rpcsvc/nis.h>
32 #include "nss-nisplus.h"
33 #include "nisplus-parser.h"
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;
40 /* Get the declaration of the parser function. */
42 #define STRUCTURE group
44 #include <nss/nss_files/files-parse.c>
46 /* Structure for remembering -group members ... */
47 #define BLACKLIST_INITIAL_SIZE 512
48 #define BLACKLIST_INCREMENT 256
64 struct blacklist_t blacklist
;
66 typedef struct ent_t ent_t
;
68 static ent_t ext_ent
= {0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0}};
70 /* Protect global state against multiple changers. */
71 __libc_lock_define_initialized (static, lock
)
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
*);
77 static enum nss_status
78 _nss_first_init (void)
82 __nss_database_lookup ("group_compat", NULL
, "nis", &ni
);
83 use_nisplus
= (strcmp (ni
->name
, "nisplus") == 0);
88 static const char key
[] = "group.org_dir.";
89 const char *local_dir
= nis_local_directory ();
90 size_t len_local_dir
= strlen (local_dir
);
92 grptable
= malloc (sizeof (key
) + len_local_dir
);
94 return NSS_STATUS_TRYAGAIN
;
96 grptablelen
= ((char *) mempcpy (mempcpy (grptable
,
97 key
, sizeof (key
) - 1),
98 local_dir
, len_local_dir
+ 1)
102 return NSS_STATUS_SUCCESS
;
105 static enum nss_status
106 internal_setgrent (ent_t
*ent
)
108 enum nss_status status
= NSS_STATUS_SUCCESS
;
110 ent
->nis
= ent
->nis_first
= 0;
112 if (_nss_first_init () != NSS_STATUS_SUCCESS
)
113 return NSS_STATUS_UNAVAIL
;
115 if (ent
->oldkey
!= NULL
)
122 if (ent
->result
!= NULL
)
124 nis_freeresult (ent
->result
);
128 if (ent
->blacklist
.data
!= NULL
)
130 ent
->blacklist
.current
= 1;
131 ent
->blacklist
.data
[0] = '|';
132 ent
->blacklist
.data
[1] = '\0';
135 ent
->blacklist
.current
= 0;
137 if (ent
->stream
== NULL
)
139 ent
->stream
= fopen ("/etc/group", "r");
141 if (ent
->stream
== NULL
)
142 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
145 /* We have to make sure the file is `closed on exec'. */
148 result
= flags
= fcntl (fileno (ent
->stream
), F_GETFD
, 0);
152 result
= fcntl (fileno (ent
->stream
), F_SETFD
, flags
);
156 /* Something went wrong. Close the stream and return a
158 fclose (ent
->stream
);
160 status
= NSS_STATUS_UNAVAIL
;
165 rewind (ent
->stream
);
172 _nss_compat_setgrent (void)
174 enum nss_status result
;
176 __libc_lock_lock (lock
);
178 result
= internal_setgrent (&ext_ent
);
180 __libc_lock_unlock (lock
);
186 static enum nss_status
187 internal_endgrent (ent_t
*ent
)
189 if (ent
->stream
!= NULL
)
191 fclose (ent
->stream
);
195 ent
->nis
= ent
->nis_first
= 0;
197 if (ent
->oldkey
!= NULL
)
204 if (ent
->result
!= NULL
)
206 nis_freeresult (ent
->result
);
210 if (ent
->blacklist
.data
!= NULL
)
212 ent
->blacklist
.current
= 1;
213 ent
->blacklist
.data
[0] = '|';
214 ent
->blacklist
.data
[1] = '\0';
217 ent
->blacklist
.current
= 0;
219 return NSS_STATUS_SUCCESS
;
223 _nss_compat_endgrent (void)
225 enum nss_status result
;
227 __libc_lock_lock (lock
);
229 result
= internal_endgrent (&ext_ent
);
231 __libc_lock_unlock (lock
);
236 static enum nss_status
237 getgrent_next_nis (struct group
*result
, ent_t
*ent
, char *buffer
,
238 size_t buflen
, int *errnop
)
240 struct parser_data
*data
= (void *) buffer
;
242 char *outkey
, *outval
;
243 int outkeylen
, outvallen
, parse_res
;
246 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
249 return NSS_STATUS_NOTFOUND
;
256 bool_t save_nis_first
;
260 if (yp_first (domain
, "group.byname", &outkey
, &outkeylen
,
261 &outval
, &outvallen
) != YPERR_SUCCESS
)
264 return NSS_STATUS_UNAVAIL
;
266 save_oldkey
= ent
->oldkey
;
267 save_oldlen
= ent
->oldkeylen
;
268 save_nis_first
= TRUE
;
269 ent
->oldkey
= outkey
;
270 ent
->oldkeylen
= outkeylen
;
271 ent
->nis_first
= FALSE
;
275 if (yp_next (domain
, "group.byname", ent
->oldkey
, ent
->oldkeylen
,
276 &outkey
, &outkeylen
, &outval
, &outvallen
)
280 return NSS_STATUS_NOTFOUND
;
283 save_oldkey
= ent
->oldkey
;
284 save_oldlen
= ent
->oldkeylen
;
285 save_nis_first
= FALSE
;
286 ent
->oldkey
= outkey
;
287 ent
->oldkeylen
= outkeylen
;
290 /* Copy the found data to our buffer */
291 p
= strncpy (buffer
, outval
, buflen
);
293 /* ...and free the data. */
299 parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
, errnop
);
303 ent
->oldkey
= save_oldkey
;
304 ent
->oldkeylen
= save_oldlen
;
305 ent
->nis_first
= save_nis_first
;
307 return NSS_STATUS_TRYAGAIN
;
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 static enum nss_status
325 getgrent_next_nisplus (struct group
*result
, ent_t
*ent
, char *buffer
,
326 size_t buflen
, int *errnop
)
332 nis_result
*save_oldres
;
333 bool_t save_nis_first
;
337 save_oldres
= ent
->result
;
338 save_nis_first
= TRUE
;
339 ent
->result
= nis_first_entry(grptable
);
340 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
343 return niserr2nss (ent
->result
->status
);
345 ent
->nis_first
= FALSE
;
351 save_oldres
= ent
->result
;
352 save_nis_first
= FALSE
;
353 res
= nis_next_entry(grptable
, &ent
->result
->cookie
);
355 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
358 return niserr2nss (ent
->result
->status
);
361 parse_res
= _nss_nisplus_parse_grent (ent
->result
, 0, result
,
362 buffer
, buflen
, errnop
);
365 nis_freeresult (ent
->result
);
366 ent
->result
= save_oldres
;
367 ent
->nis_first
= save_nis_first
;
369 return NSS_STATUS_TRYAGAIN
;
374 nis_freeresult (save_oldres
);
378 in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
379 parse_res
= 0; /* if result->gr_name in blacklist,search next entry */
383 return NSS_STATUS_SUCCESS
;
386 /* This function handle the +group entrys in /etc/group */
387 static enum nss_status
388 getgrnam_plusgroup (const char *name
, struct group
*result
, char *buffer
,
389 size_t buflen
, int *errnop
)
391 struct parser_data
*data
= (void *) buffer
;
394 if (use_nisplus
) /* Do the NIS+ query here */
397 char buf
[strlen (name
) + 24 + grptablelen
];
399 sprintf(buf
, "[name=%s],%s", name
, grptable
);
400 res
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
401 if (niserr2nss (res
->status
) != NSS_STATUS_SUCCESS
)
403 enum nss_status status
= niserr2nss (res
->status
);
405 nis_freeresult (res
);
408 parse_res
= _nss_nisplus_parse_grent (res
, 0, result
, buffer
, buflen
,
412 nis_freeresult (res
);
414 return NSS_STATUS_TRYAGAIN
;
416 nis_freeresult (res
);
420 char *domain
, *outval
, *p
;
423 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
424 return NSS_STATUS_NOTFOUND
;
426 if (yp_match (domain
, "group.byname", name
, strlen (name
),
427 &outval
, &outvallen
) != YPERR_SUCCESS
)
428 return NSS_STATUS_NOTFOUND
;
430 p
= strncpy (buffer
, outval
,
431 buflen
< (size_t) outvallen
? buflen
: (size_t) outvallen
);
435 parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
, errnop
);
437 return NSS_STATUS_TRYAGAIN
;
441 /* We found the entry. */
442 return NSS_STATUS_SUCCESS
;
444 return NSS_STATUS_RETURN
;
447 static enum nss_status
448 getgrent_next_file (struct group
*result
, ent_t
*ent
,
449 char *buffer
, size_t buflen
, int *errnop
)
451 struct parser_data
*data
= (void *) buffer
;
460 fgetpos (ent
->stream
, &pos
);
461 p
= fgets (buffer
, buflen
, ent
->stream
);
464 if (feof (ent
->stream
))
465 return NSS_STATUS_NOTFOUND
;
468 fsetpos (ent
->stream
, &pos
);
470 return NSS_STATUS_TRYAGAIN
;
474 /* Terminate the line for any case. */
475 buffer
[buflen
- 1] = '\0';
477 /* Skip leading blanks. */
481 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
482 /* Parse the line. If it is invalid, loop to
483 get the next line of the file to parse. */
484 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
489 /* The parser ran out of space. */
490 fsetpos (ent
->stream
, &pos
);
492 return NSS_STATUS_TRYAGAIN
;
495 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
496 /* This is a real entry. */
500 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0'
501 && result
->gr_name
[1] != '@')
503 blacklist_store_name (&result
->gr_name
[1], ent
);
508 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0'
509 && result
->gr_name
[1] != '@')
511 enum nss_status status
;
513 /* Store the group in the blacklist for the "+" at the end of
515 blacklist_store_name (&result
->gr_name
[1], ent
);
516 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, buffer
,
518 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
521 if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry */
522 || status
== NSS_STATUS_NOTFOUND
) /* No group in NIS */
526 if (status
== NSS_STATUS_TRYAGAIN
)
528 /* The parser ran out of space. */
529 fsetpos (ent
->stream
, &pos
);
537 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
540 ent
->nis_first
= TRUE
;
543 return getgrent_next_nisplus (result
, ent
, buffer
, buflen
, errnop
);
545 return getgrent_next_nis (result
, ent
, buffer
, buflen
, errnop
);
549 return NSS_STATUS_SUCCESS
;
553 static enum nss_status
554 internal_getgrent_r (struct group
*gr
, ent_t
*ent
, char *buffer
,
555 size_t buflen
, int *errnop
)
560 return getgrent_next_nisplus (gr
, ent
, buffer
, buflen
, errnop
);
562 return getgrent_next_nis (gr
, ent
, buffer
, buflen
, errnop
);
565 return getgrent_next_file (gr
, ent
, buffer
, buflen
, errnop
);
569 _nss_compat_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
572 enum nss_status status
= NSS_STATUS_SUCCESS
;
574 __libc_lock_lock (lock
);
576 /* Be prepared that the setgrent function was not called before. */
577 if (ext_ent
.stream
== NULL
)
578 status
= internal_setgrent (&ext_ent
);
580 if (status
== NSS_STATUS_SUCCESS
)
581 status
= internal_getgrent_r (grp
, &ext_ent
, buffer
, buflen
, errnop
);
583 __libc_lock_unlock (lock
);
588 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
589 static enum nss_status
590 internal_getgrnam_r (const char *name
, struct group
*result
, ent_t
*ent
,
591 char *buffer
, size_t buflen
, int *errnop
)
593 struct parser_data
*data
= (void *) buffer
;
602 fgetpos (ent
->stream
, &pos
);
603 p
= fgets (buffer
, buflen
, ent
->stream
);
606 if (feof (ent
->stream
))
607 return NSS_STATUS_NOTFOUND
;
610 fsetpos (ent
->stream
, &pos
);
612 return NSS_STATUS_TRYAGAIN
;
616 /* Terminate the line for any case. */
617 buffer
[buflen
- 1] = '\0';
619 /* Skip leading blanks. */
623 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
624 /* Parse the line. If it is invalid, loop to
625 get the next line of the file to parse. */
626 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
631 /* The parser ran out of space. */
632 fsetpos (ent
->stream
, &pos
);
634 return NSS_STATUS_TRYAGAIN
;
637 /* This is a real entry. */
638 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
640 if (strcmp (result
->gr_name
, name
) == 0)
641 return NSS_STATUS_SUCCESS
;
647 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
649 if (strcmp (&result
->gr_name
[1], name
) == 0)
650 return NSS_STATUS_NOTFOUND
;
656 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
658 if (strcmp (name
, &result
->gr_name
[1]) == 0)
660 enum nss_status status
;
662 status
= getgrnam_plusgroup (name
, result
, buffer
, buflen
,
664 if (status
== NSS_STATUS_RETURN
)
665 /* We couldn't parse the entry */
672 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
674 enum nss_status status
;
676 status
= getgrnam_plusgroup (name
, result
, buffer
, buflen
, errnop
);
677 if (status
== NSS_STATUS_RETURN
)
678 /* We couldn't parse the entry */
685 return NSS_STATUS_SUCCESS
;
689 _nss_compat_getgrnam_r (const char *name
, struct group
*grp
,
690 char *buffer
, size_t buflen
, int *errnop
)
692 ent_t ent
= {0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0}};
693 enum nss_status status
;
695 if (name
[0] == '-' || name
[0] == '+')
696 return NSS_STATUS_NOTFOUND
;
698 __libc_lock_lock (lock
);
700 status
= internal_setgrent (&ent
);
702 __libc_lock_unlock (lock
);
704 if (status
!= NSS_STATUS_SUCCESS
)
707 status
= internal_getgrnam_r (name
, grp
, &ent
, buffer
, buflen
, errnop
);
709 internal_endgrent (&ent
);
714 /* This function handle the + entry in /etc/group */
715 static enum nss_status
716 getgrgid_plusgroup (gid_t gid
, struct group
*result
, char *buffer
,
717 size_t buflen
, int *errnop
)
719 struct parser_data
*data
= (void *) buffer
;
722 if (use_nisplus
) /* Do the NIS+ query here */
725 char buf
[24 + grptablelen
];
727 sprintf(buf
, "[gid=%d],%s", gid
, grptable
);
728 res
= nis_list(buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
729 if (niserr2nss (res
->status
) != NSS_STATUS_SUCCESS
)
731 enum nss_status status
= niserr2nss (res
->status
);
733 nis_freeresult (res
);
736 if ((parse_res
= _nss_nisplus_parse_grent (res
, 0, result
, buffer
,
737 buflen
, errnop
)) == -1)
739 nis_freeresult (res
);
741 return NSS_STATUS_TRYAGAIN
;
743 nis_freeresult (res
);
748 char *domain
, *outval
, *p
;
751 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
754 return NSS_STATUS_TRYAGAIN
;
757 snprintf (buf
, sizeof (buf
), "%d", gid
);
759 if (yp_match (domain
, "group.bygid", buf
, strlen (buf
),
760 &outval
, &outvallen
) != YPERR_SUCCESS
)
763 return NSS_STATUS_TRYAGAIN
;
765 p
= strncpy (buffer
, outval
,
766 buflen
< (size_t) outvallen
? buflen
: (size_t) outvallen
);
770 parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
, errnop
);
772 return NSS_STATUS_TRYAGAIN
;
776 /* We found the entry. */
777 return NSS_STATUS_SUCCESS
;
779 return NSS_STATUS_RETURN
;
782 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
783 static enum nss_status
784 internal_getgrgid_r (gid_t gid
, struct group
*result
, ent_t
*ent
,
785 char *buffer
, size_t buflen
, int *errnop
)
787 struct parser_data
*data
= (void *) buffer
;
796 fgetpos (ent
->stream
, &pos
);
797 p
= fgets (buffer
, buflen
, ent
->stream
);
800 if (feof (ent
->stream
))
801 return NSS_STATUS_NOTFOUND
;
804 fsetpos (ent
->stream
, &pos
);
806 return NSS_STATUS_TRYAGAIN
;
810 /* Terminate the line for any case. */
811 buffer
[buflen
- 1] = '\0';
813 /* Skip leading blanks. */
817 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
818 /* Parse the line. If it is invalid, loop to
819 get the next line of the file to parse. */
820 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
825 /* The parser ran out of space. */
826 fsetpos (ent
->stream
, &pos
);
828 return NSS_STATUS_TRYAGAIN
;
831 /* This is a real entry. */
832 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
834 if (result
->gr_gid
== gid
)
835 return NSS_STATUS_SUCCESS
;
841 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
843 blacklist_store_name (&result
->gr_name
[1], ent
);
848 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
850 enum nss_status status
;
852 /* Store the group in the blacklist for the "+" at the end of
854 blacklist_store_name (&result
->gr_name
[1], ent
);
855 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, buffer
,
857 if (status
== NSS_STATUS_SUCCESS
&& result
->gr_gid
== gid
)
863 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
865 enum nss_status status
;
867 status
= getgrgid_plusgroup (gid
, result
, buffer
, buflen
, errnop
);
868 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
869 return NSS_STATUS_NOTFOUND
;
875 return NSS_STATUS_SUCCESS
;
879 _nss_compat_getgrgid_r (gid_t gid
, struct group
*grp
,
880 char *buffer
, size_t buflen
, int *errnop
)
882 ent_t ent
= {0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0}};
883 enum nss_status status
;
885 __libc_lock_lock (lock
);
887 status
= internal_setgrent (&ent
);
889 __libc_lock_unlock (lock
);
891 if (status
!= NSS_STATUS_SUCCESS
)
894 status
= internal_getgrgid_r (gid
, grp
, &ent
, buffer
, buflen
, errnop
);
896 internal_endgrent (&ent
);
902 /* Support routines for remembering -@netgroup and -user entries.
903 The names are stored in a single string with `|' as separator. */
905 blacklist_store_name (const char *name
, ent_t
*ent
)
907 int namelen
= strlen (name
);
910 /* first call, setup cache */
911 if (ent
->blacklist
.size
== 0)
913 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
914 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
915 if (ent
->blacklist
.data
== NULL
)
917 ent
->blacklist
.data
[0] = '|';
918 ent
->blacklist
.data
[1] = '\0';
919 ent
->blacklist
.current
= 1;
923 if (in_blacklist (name
, namelen
, ent
))
924 return; /* no duplicates */
926 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
928 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
929 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
932 free (ent
->blacklist
.data
);
933 ent
->blacklist
.size
= 0;
936 ent
->blacklist
.data
= tmp
;
940 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
943 ent
->blacklist
.current
+= namelen
+ 1;
948 /* returns TRUE if ent->blacklist contains name, else FALSE */
950 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
952 char buf
[namelen
+ 3];
955 if (ent
->blacklist
.data
== NULL
)
959 cp
= stpcpy (&buf
[1], name
);
962 return strstr (ent
->blacklist
.data
, buf
) != NULL
;