]>
git.ipfire.org Git - thirdparty/glibc.git/blob - 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.
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 char buf
[20 + strlen (nis_local_directory ())];
91 p
= stpcpy (buf
, "group.org_dir.");
92 p
= stpcpy (p
, nis_local_directory ());
93 grptable
= strdup (buf
);
95 return NSS_STATUS_TRYAGAIN
;
96 grptablelen
= strlen (grptable
);
99 return NSS_STATUS_SUCCESS
;
102 static enum nss_status
103 internal_setgrent (ent_t
*ent
)
105 enum nss_status status
= NSS_STATUS_SUCCESS
;
107 ent
->nis
= ent
->nis_first
= 0;
109 if (_nss_first_init () != NSS_STATUS_SUCCESS
)
110 return NSS_STATUS_UNAVAIL
;
112 if (ent
->oldkey
!= NULL
)
119 if (ent
->result
!= NULL
)
121 nis_freeresult (ent
->result
);
125 ent
->blacklist
.current
= 0;
126 if (ent
->blacklist
.data
!= NULL
)
127 ent
->blacklist
.data
[0] = '\0';
129 if (ent
->stream
== NULL
)
131 ent
->stream
= fopen ("/etc/group", "r");
133 if (ent
->stream
== NULL
)
134 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
137 /* We have to make sure the file is `closed on exec'. */
140 result
= flags
= fcntl (fileno (ent
->stream
), F_GETFD
, 0);
144 result
= fcntl (fileno (ent
->stream
), F_SETFD
, flags
);
148 /* Something went wrong. Close the stream and return a
150 fclose (ent
->stream
);
152 status
= NSS_STATUS_UNAVAIL
;
157 rewind (ent
->stream
);
164 _nss_compat_setgrent (void)
166 enum nss_status result
;
168 __libc_lock_lock (lock
);
170 result
= internal_setgrent (&ext_ent
);
172 __libc_lock_unlock (lock
);
178 static enum nss_status
179 internal_endgrent (ent_t
*ent
)
181 if (ent
->stream
!= NULL
)
183 fclose (ent
->stream
);
187 ent
->nis
= ent
->nis_first
= 0;
189 if (ent
->oldkey
!= NULL
)
196 if (ent
->result
!= NULL
)
198 nis_freeresult (ent
->result
);
202 ent
->blacklist
.current
= 0;
203 if (ent
->blacklist
.data
!= NULL
)
204 ent
->blacklist
.data
[0] = '\0';
206 return NSS_STATUS_SUCCESS
;
210 _nss_compat_endgrent (void)
212 enum nss_status result
;
214 __libc_lock_lock (lock
);
216 result
= internal_endgrent (&ext_ent
);
218 __libc_lock_unlock (lock
);
223 static enum nss_status
224 getgrent_next_nis (struct group
*result
, ent_t
*ent
, char *buffer
,
227 struct parser_data
*data
= (void *) buffer
;
229 char *outkey
, *outval
;
230 int outkeylen
, outvallen
, parse_res
;
233 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
236 return NSS_STATUS_NOTFOUND
;
243 bool_t save_nis_first
;
247 if (yp_first (domain
, "group.byname", &outkey
, &outkeylen
,
248 &outval
, &outvallen
) != YPERR_SUCCESS
)
251 return NSS_STATUS_UNAVAIL
;
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
;
262 if (yp_next (domain
, "group.byname", ent
->oldkey
, ent
->oldkeylen
,
263 &outkey
, &outkeylen
, &outval
, &outvallen
)
267 return NSS_STATUS_NOTFOUND
;
270 save_oldkey
= ent
->oldkey
;
271 save_oldlen
= ent
->oldkeylen
;
272 save_nis_first
= FALSE
;
273 ent
->oldkey
= outkey
;
274 ent
->oldkeylen
= outkeylen
;
277 /* Copy the found data to our buffer */
278 p
= strncpy (buffer
, outval
, buflen
);
280 /* ...and free the data. */
286 if ((parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
)) == -1)
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
;
302 in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
303 parse_res
= 0; /* if result->gr_name in blacklist,search next entry */
307 return NSS_STATUS_SUCCESS
;
310 static enum nss_status
311 getgrent_next_nisplus (struct group
*result
, ent_t
*ent
, char *buffer
,
318 nis_result
*save_oldres
;
319 bool_t save_nis_first
;
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
)
329 return niserr2nss (ent
->result
->status
);
331 ent
->nis_first
= FALSE
;
337 save_oldres
= ent
->result
;
338 save_nis_first
= FALSE
;
339 res
= nis_next_entry(grptable
, &ent
->result
->cookie
);
341 if (niserr2nss (ent
->result
->status
) != NSS_STATUS_SUCCESS
)
344 return niserr2nss (ent
->result
->status
);
347 if ((parse_res
= _nss_nisplus_parse_grent (ent
->result
, 0, result
,
348 buffer
, buflen
)) == -1)
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
;
359 nis_freeresult (save_oldres
);
363 in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
364 parse_res
= 0; /* if result->gr_name in blacklist,search next entry */
368 return NSS_STATUS_SUCCESS
;
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
,
376 struct parser_data
*data
= (void *) buffer
;
379 if (use_nisplus
) /* Do the NIS+ query here */
382 char buf
[strlen (result
->gr_name
) + 24 + grptablelen
];
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
)
388 enum nss_status status
= niserr2nss (res
->status
);
390 nis_freeresult (res
);
393 if ((parse_res
= _nss_nisplus_parse_grent (res
, 0, result
, buffer
,
396 __set_errno (ERANGE
);
397 nis_freeresult (res
);
398 return NSS_STATUS_TRYAGAIN
;
400 nis_freeresult (res
);
404 char *domain
, *outval
, *p
;
407 if (yp_get_default_domain (&domain
) != YPERR_SUCCESS
)
408 return NSS_STATUS_TRYAGAIN
;
410 if (yp_match (domain
, "group.byname", &result
->gr_name
[1],
411 strlen (result
->gr_name
) - 1, &outval
, &outvallen
)
413 return NSS_STATUS_TRYAGAIN
;
414 p
= strncpy (buffer
, outval
,
415 buflen
< (size_t) outvallen
? buflen
: (size_t) outvallen
);
419 if ((parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
)) == -1)
421 __set_errno (ERANGE
);
422 return NSS_STATUS_TRYAGAIN
;
427 /* We found the entry. */
428 return NSS_STATUS_SUCCESS
;
430 return NSS_STATUS_RETURN
;
434 static enum nss_status
435 getgrent_next_file (struct group
*result
, ent_t
*ent
,
436 char *buffer
, size_t buflen
)
438 struct parser_data
*data
= (void *) buffer
;
447 fgetpos (ent
->stream
, &pos
);
448 p
= fgets (buffer
, buflen
, ent
->stream
);
451 if (feof (ent
->stream
))
452 return NSS_STATUS_NOTFOUND
;
455 __set_errno (ERANGE
);
456 return NSS_STATUS_TRYAGAIN
;
460 /* Terminate the line for any case. */
461 buffer
[buflen
- 1] = '\0';
463 /* Skip leading blanks. */
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
)));
474 /* The parser ran out of space. */
475 fsetpos (ent
->stream
, &pos
);
476 __set_errno (ERANGE
);
477 return NSS_STATUS_TRYAGAIN
;
480 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
481 /* This is a real entry. */
485 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0'
486 && result
->gr_name
[1] != '@')
488 blacklist_store_name (&result
->gr_name
[1], ent
);
493 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0'
494 && result
->gr_name
[1] != '@')
496 enum nss_status status
;
498 status
= getgrent_next_file_plusgroup (result
, buffer
, buflen
);
499 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
502 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
509 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
512 ent
->nis_first
= TRUE
;
515 return getgrent_next_nisplus (result
, ent
, buffer
, buflen
);
517 return getgrent_next_nis (result
, ent
, buffer
, buflen
);
521 return NSS_STATUS_SUCCESS
;
525 static enum nss_status
526 internal_getgrent_r (struct group
*gr
, ent_t
*ent
, char *buffer
,
532 return getgrent_next_nisplus (gr
, ent
, buffer
, buflen
);
534 return getgrent_next_nis (gr
, ent
, buffer
, buflen
);
537 return getgrent_next_file (gr
, ent
, buffer
, buflen
);
541 _nss_compat_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
)
543 enum nss_status status
= NSS_STATUS_SUCCESS
;
545 __libc_lock_lock (lock
);
547 /* Be prepared that the setgrent function was not called before. */
548 if (ext_ent
.stream
== NULL
)
549 status
= internal_setgrent (&ext_ent
);
551 if (status
== NSS_STATUS_SUCCESS
)
552 status
= internal_getgrent_r (grp
, &ext_ent
, buffer
, buflen
);
554 __libc_lock_unlock (lock
);
561 _nss_compat_getgrnam_r (const char *name
, struct group
*grp
,
562 char *buffer
, size_t buflen
)
564 ent_t ent
= {0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0}};
565 enum nss_status status
;
567 if (name
[0] == '-' || name
[0] == '+')
568 return NSS_STATUS_NOTFOUND
;
570 __libc_lock_lock (lock
);
572 status
= internal_setgrent (&ent
);
574 __libc_lock_unlock (lock
);
576 if (status
!= NSS_STATUS_SUCCESS
)
579 while ((status
= internal_getgrent_r (grp
, &ent
, buffer
, buflen
))
580 == NSS_STATUS_SUCCESS
)
581 if (strcmp (grp
->gr_name
, name
) == 0)
584 internal_endgrent (&ent
);
590 _nss_compat_getgrgid_r (gid_t gid
, struct group
*grp
,
591 char *buffer
, size_t buflen
)
593 ent_t ent
= {0, 0, NULL
, 0, NULL
, NULL
, {NULL
, 0, 0}};
594 enum nss_status status
;
596 __libc_lock_lock (lock
);
598 status
= internal_setgrent (&ent
);
600 __libc_lock_unlock (lock
);
602 if (status
!= NSS_STATUS_SUCCESS
)
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] != '-')
610 internal_endgrent (&ent
);
615 /* Support routines for remembering -@netgroup and -user entries.
616 The names are stored in a single string with `|' as separator. */
618 blacklist_store_name (const char *name
, ent_t
*ent
)
620 int namelen
= strlen (name
);
623 /* first call, setup cache */
624 if (ent
->blacklist
.size
== 0)
626 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
627 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
628 if (ent
->blacklist
.data
== NULL
)
630 ent
->blacklist
.data
[0] = '|';
631 ent
->blacklist
.data
[1] = '\0';
632 ent
->blacklist
.current
= 1;
636 if (in_blacklist (name
, namelen
, ent
))
637 return; /* no duplicates */
639 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
641 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
642 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
645 free (ent
->blacklist
.data
);
646 ent
->blacklist
.size
= 0;
649 ent
->blacklist
.data
= tmp
;
653 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
656 ent
->blacklist
.current
+= namelen
+ 1;
661 /* returns TRUE if ent->blacklist contains name, else FALSE */
663 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
665 char buf
[namelen
+ 3];
668 if (ent
->blacklist
.data
== NULL
)
672 cp
= stpcpy (&buf
[1], name
);
675 return strstr (ent
->blacklist
.data
, buf
) != NULL
;