]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_compat/compat-grp.c
1 /* Copyright (C) 1996-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
25 #include <stdio_ext.h>
27 #include <rpc/types.h>
28 #include <libc-lock.h>
29 #include <kernel-features.h>
31 static service_user
*ni
;
32 static enum nss_status (*nss_setgrent
) (int stayopen
);
33 static enum nss_status (*nss_getgrnam_r
) (const char *name
,
34 struct group
* grp
, char *buffer
,
35 size_t buflen
, int *errnop
);
36 static enum nss_status (*nss_getgrgid_r
) (gid_t gid
, struct group
* grp
,
37 char *buffer
, size_t buflen
,
39 static enum nss_status (*nss_getgrent_r
) (struct group
* grp
, char *buffer
,
40 size_t buflen
, int *errnop
);
41 static enum nss_status (*nss_endgrent
) (void);
43 /* Get the declaration of the parser function. */
45 #define STRUCTURE group
47 #include <nss/nss_files/files-parse.c>
49 /* Structure for remembering -group members ... */
50 #define BLACKLIST_INITIAL_SIZE 512
51 #define BLACKLIST_INCREMENT 256
62 enum nss_status setent_status
;
64 struct blacklist_t blacklist
;
66 typedef struct ent_t ent_t
;
68 static ent_t ext_ent
= { TRUE
, NSS_STATUS_SUCCESS
, 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 /* Initialize the NSS interface/functions. The calling function must
80 init_nss_interface (void)
82 if (__nss_database_lookup ("group_compat", NULL
, "nis", &ni
) >= 0)
84 nss_setgrent
= __nss_lookup_function (ni
, "setgrent");
85 nss_getgrnam_r
= __nss_lookup_function (ni
, "getgrnam_r");
86 nss_getgrgid_r
= __nss_lookup_function (ni
, "getgrgid_r");
87 nss_getgrent_r
= __nss_lookup_function (ni
, "getgrent_r");
88 nss_endgrent
= __nss_lookup_function (ni
, "endgrent");
92 static enum nss_status
93 internal_setgrent (ent_t
*ent
, int stayopen
, int needent
)
95 enum nss_status status
= NSS_STATUS_SUCCESS
;
99 if (ent
->blacklist
.data
!= NULL
)
101 ent
->blacklist
.current
= 1;
102 ent
->blacklist
.data
[0] = '|';
103 ent
->blacklist
.data
[1] = '\0';
106 ent
->blacklist
.current
= 0;
108 if (ent
->stream
== NULL
)
110 ent
->stream
= fopen ("/etc/group", "rme");
112 if (ent
->stream
== NULL
)
113 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
115 /* We take care of locking ourself. */
116 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
119 rewind (ent
->stream
);
121 if (needent
&& status
== NSS_STATUS_SUCCESS
&& nss_setgrent
)
122 ent
->setent_status
= nss_setgrent (stayopen
);
129 _nss_compat_setgrent (int stayopen
)
131 enum nss_status result
;
133 __libc_lock_lock (lock
);
136 init_nss_interface ();
138 result
= internal_setgrent (&ext_ent
, stayopen
, 1);
140 __libc_lock_unlock (lock
);
146 static enum nss_status
147 internal_endgrent (ent_t
*ent
)
149 if (ent
->stream
!= NULL
)
151 fclose (ent
->stream
);
155 if (ent
->blacklist
.data
!= NULL
)
157 ent
->blacklist
.current
= 1;
158 ent
->blacklist
.data
[0] = '|';
159 ent
->blacklist
.data
[1] = '\0';
162 ent
->blacklist
.current
= 0;
164 return NSS_STATUS_SUCCESS
;
168 _nss_compat_endgrent (void)
170 enum nss_status result
;
172 __libc_lock_lock (lock
);
177 result
= internal_endgrent (&ext_ent
);
179 __libc_lock_unlock (lock
);
184 /* get the next group from NSS (+ entry) */
185 static enum nss_status
186 getgrent_next_nss (struct group
*result
, ent_t
*ent
, char *buffer
,
187 size_t buflen
, int *errnop
)
190 return NSS_STATUS_UNAVAIL
;
192 /* If the setgrent call failed, say so. */
193 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
194 return ent
->setent_status
;
198 enum nss_status status
;
200 if ((status
= nss_getgrent_r (result
, buffer
, buflen
, errnop
)) !=
204 while (in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
));
206 return NSS_STATUS_SUCCESS
;
209 /* This function handle the +group entrys in /etc/group */
210 static enum nss_status
211 getgrnam_plusgroup (const char *name
, struct group
*result
, ent_t
*ent
,
212 char *buffer
, size_t buflen
, int *errnop
)
215 return NSS_STATUS_UNAVAIL
;
217 enum nss_status status
= nss_getgrnam_r (name
, result
, buffer
, buflen
,
219 if (status
!= NSS_STATUS_SUCCESS
)
222 if (in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
223 return NSS_STATUS_NOTFOUND
;
225 /* We found the entry. */
226 return NSS_STATUS_SUCCESS
;
229 static enum nss_status
230 getgrent_next_file (struct group
*result
, ent_t
*ent
,
231 char *buffer
, size_t buflen
, int *errnop
)
233 struct parser_data
*data
= (void *) buffer
;
242 /* We need at least 3 characters for one line. */
243 if (__glibc_unlikely (buflen
< 3))
247 return NSS_STATUS_TRYAGAIN
;
250 fgetpos (ent
->stream
, &pos
);
251 buffer
[buflen
- 1] = '\xff';
252 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
253 if (p
== NULL
&& feof_unlocked (ent
->stream
))
254 return NSS_STATUS_NOTFOUND
;
256 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
259 fsetpos (ent
->stream
, &pos
);
263 /* Terminate the line for any case. */
264 buffer
[buflen
- 1] = '\0';
266 /* Skip leading blanks. */
270 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
271 /* Parse the line. If it is invalid, loop to
272 get the next line of the file to parse. */
273 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
276 if (__glibc_unlikely (parse_res
== -1))
277 /* The parser ran out of space. */
280 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
281 /* This is a real entry. */
285 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0'
286 && result
->gr_name
[1] != '@')
288 blacklist_store_name (&result
->gr_name
[1], ent
);
293 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0'
294 && result
->gr_name
[1] != '@')
296 size_t len
= strlen (result
->gr_name
);
298 enum nss_status status
;
300 /* Store the group in the blacklist for the "+" at the end of
302 memcpy (buf
, &result
->gr_name
[1], len
);
303 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, ent
,
304 buffer
, buflen
, errnop
);
305 blacklist_store_name (buf
, ent
);
306 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
308 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry*/
309 || status
== NSS_STATUS_NOTFOUND
) /* No group in NIS */
313 if (status
== NSS_STATUS_TRYAGAIN
)
314 /* The parser ran out of space. */
322 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
326 return getgrent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
330 return NSS_STATUS_SUCCESS
;
335 _nss_compat_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
338 enum nss_status result
= NSS_STATUS_SUCCESS
;
340 __libc_lock_lock (lock
);
342 /* Be prepared that the setgrent function was not called before. */
344 init_nss_interface ();
346 if (ext_ent
.stream
== NULL
)
347 result
= internal_setgrent (&ext_ent
, 1, 1);
349 if (result
== NSS_STATUS_SUCCESS
)
352 result
= getgrent_next_file (grp
, &ext_ent
, buffer
, buflen
, errnop
);
354 result
= getgrent_next_nss (grp
, &ext_ent
, buffer
, buflen
, errnop
);
356 __libc_lock_unlock (lock
);
361 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
362 static enum nss_status
363 internal_getgrnam_r (const char *name
, struct group
*result
, ent_t
*ent
,
364 char *buffer
, size_t buflen
, int *errnop
)
366 struct parser_data
*data
= (void *) buffer
;
375 /* We need at least 3 characters for one line. */
376 if (__glibc_unlikely (buflen
< 3))
380 return NSS_STATUS_TRYAGAIN
;
383 fgetpos (ent
->stream
, &pos
);
384 buffer
[buflen
- 1] = '\xff';
385 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
386 if (p
== NULL
&& feof_unlocked (ent
->stream
))
387 return NSS_STATUS_NOTFOUND
;
389 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
392 fsetpos (ent
->stream
, &pos
);
396 /* Terminate the line for any case. */
397 buffer
[buflen
- 1] = '\0';
399 /* Skip leading blanks. */
403 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
404 /* Parse the line. If it is invalid, loop to
405 get the next line of the file to parse. */
406 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
409 if (__glibc_unlikely (parse_res
== -1))
410 /* The parser ran out of space. */
413 /* This is a real entry. */
414 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
416 if (strcmp (result
->gr_name
, name
) == 0)
417 return NSS_STATUS_SUCCESS
;
423 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
425 if (strcmp (&result
->gr_name
[1], name
) == 0)
426 return NSS_STATUS_NOTFOUND
;
432 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
434 if (strcmp (name
, &result
->gr_name
[1]) == 0)
436 enum nss_status status
;
438 status
= getgrnam_plusgroup (name
, result
, ent
,
439 buffer
, buflen
, errnop
);
440 if (status
== NSS_STATUS_RETURN
)
441 /* We couldn't parse the entry */
448 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
450 enum nss_status status
;
452 status
= getgrnam_plusgroup (name
, result
, ent
,
453 buffer
, buflen
, errnop
);
454 if (status
== NSS_STATUS_RETURN
)
455 /* We couldn't parse the entry */
462 return NSS_STATUS_SUCCESS
;
466 _nss_compat_getgrnam_r (const char *name
, struct group
*grp
,
467 char *buffer
, size_t buflen
, int *errnop
)
469 ent_t ent
= { TRUE
, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 }};
470 enum nss_status result
;
472 if (name
[0] == '-' || name
[0] == '+')
473 return NSS_STATUS_NOTFOUND
;
475 __libc_lock_lock (lock
);
478 init_nss_interface ();
480 __libc_lock_unlock (lock
);
482 result
= internal_setgrent (&ent
, 0, 0);
484 if (result
== NSS_STATUS_SUCCESS
)
485 result
= internal_getgrnam_r (name
, grp
, &ent
, buffer
, buflen
, errnop
);
487 internal_endgrent (&ent
);
492 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
493 static enum nss_status
494 internal_getgrgid_r (gid_t gid
, struct group
*result
, ent_t
*ent
,
495 char *buffer
, size_t buflen
, int *errnop
)
497 struct parser_data
*data
= (void *) buffer
;
506 /* We need at least 3 characters for one line. */
507 if (__glibc_unlikely (buflen
< 3))
511 return NSS_STATUS_TRYAGAIN
;
514 fgetpos (ent
->stream
, &pos
);
515 buffer
[buflen
- 1] = '\xff';
516 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
517 if (p
== NULL
&& feof_unlocked (ent
->stream
))
518 return NSS_STATUS_NOTFOUND
;
520 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
523 fsetpos (ent
->stream
, &pos
);
527 /* Terminate the line for any case. */
528 buffer
[buflen
- 1] = '\0';
530 /* Skip leading blanks. */
534 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
535 /* Parse the line. If it is invalid, loop to
536 get the next line of the file to parse. */
537 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
540 if (__glibc_unlikely (parse_res
== -1))
541 /* The parser ran out of space. */
544 /* This is a real entry. */
545 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
547 if (result
->gr_gid
== gid
)
548 return NSS_STATUS_SUCCESS
;
554 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
556 blacklist_store_name (&result
->gr_name
[1], ent
);
561 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
563 /* Yes, no +1, see the memcpy call below. */
564 size_t len
= strlen (result
->gr_name
);
566 enum nss_status status
;
568 /* Store the group in the blacklist for the "+" at the end of
570 memcpy (buf
, &result
->gr_name
[1], len
);
571 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, ent
,
572 buffer
, buflen
, errnop
);
573 blacklist_store_name (buf
, ent
);
574 if (status
== NSS_STATUS_SUCCESS
&& result
->gr_gid
== gid
)
580 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
583 return NSS_STATUS_UNAVAIL
;
585 enum nss_status status
= nss_getgrgid_r (gid
, result
, buffer
, buflen
,
587 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
588 return NSS_STATUS_NOTFOUND
;
594 return NSS_STATUS_SUCCESS
;
598 _nss_compat_getgrgid_r (gid_t gid
, struct group
*grp
,
599 char *buffer
, size_t buflen
, int *errnop
)
601 ent_t ent
= { TRUE
, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 }};
602 enum nss_status result
;
604 __libc_lock_lock (lock
);
607 init_nss_interface ();
609 __libc_lock_unlock (lock
);
611 result
= internal_setgrent (&ent
, 0, 0);
613 if (result
== NSS_STATUS_SUCCESS
)
614 result
= internal_getgrgid_r (gid
, grp
, &ent
, buffer
, buflen
, errnop
);
616 internal_endgrent (&ent
);
622 /* Support routines for remembering -@netgroup and -user entries.
623 The names are stored in a single string with `|' as separator. */
625 blacklist_store_name (const char *name
, ent_t
*ent
)
627 int namelen
= strlen (name
);
630 /* first call, setup cache */
631 if (ent
->blacklist
.size
== 0)
633 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
634 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
635 if (ent
->blacklist
.data
== NULL
)
637 ent
->blacklist
.data
[0] = '|';
638 ent
->blacklist
.data
[1] = '\0';
639 ent
->blacklist
.current
= 1;
643 if (in_blacklist (name
, namelen
, ent
))
644 return; /* no duplicates */
646 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
648 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
649 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
652 free (ent
->blacklist
.data
);
653 ent
->blacklist
.size
= 0;
656 ent
->blacklist
.data
= tmp
;
660 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
663 ent
->blacklist
.current
+= namelen
+ 1;
668 /* returns TRUE if ent->blacklist contains name, else FALSE */
670 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
672 char buf
[namelen
+ 3];
675 if (ent
->blacklist
.data
== NULL
)
679 cp
= stpcpy (&buf
[1], name
);
682 return strstr (ent
->blacklist
.data
, buf
) != NULL
;