1 /* Copyright (C) 1996-1999, 2001-2003, 2004, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 /* The following is an ugly trick to avoid a prototype declaration for
23 #define _nss_nis_endgrent _nss_nis_endgrent_XXX
25 #undef _nss_nis_endgrent
29 #include <bits/libc-lock.h>
30 #include <rpcsvc/yp.h>
31 #include <rpcsvc/ypclnt.h>
35 /* Get the declaration of the parser function. */
37 #define STRUCTURE group
39 #include <nss/nss_files/files-parse.c>
41 /* Protect global state against multiple changers */
42 __libc_lock_define_initialized (static, lock
)
44 static bool_t new_start
= 1;
49 _nss_nis_setgrent (int stayopen
)
51 __libc_lock_lock (lock
);
61 __libc_lock_unlock (lock
);
63 return NSS_STATUS_SUCCESS
;
65 /* Make _nss_nis_endgrent an alias of _nss_nis_setgrent. We do this
66 even though the prototypes don't match. The argument of setgrent
67 is not used so this makes no difference. */
68 strong_alias (_nss_nis_setgrent
, _nss_nis_endgrent
)
70 static enum nss_status
71 internal_nis_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
75 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
76 return NSS_STATUS_UNAVAIL
;
78 /* Get the next entry until we found a correct one. */
89 yperr
= yp_first (domain
, "group.byname", &outkey
, &keylen
, &result
,
92 yperr
= yp_next (domain
, "group.byname", oldkey
, oldkeylen
, &outkey
,
93 &keylen
, &result
, &len
);
95 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
97 enum nss_status retval
= yperr2nss (yperr
);
99 if (retval
== NSS_STATUS_TRYAGAIN
)
104 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
108 return NSS_STATUS_TRYAGAIN
;
111 char *p
= strncpy (buffer
, result
, len
);
117 parse_res
= _nss_files_parse_grent (p
, grp
, (void *) buffer
, buflen
,
119 if (__builtin_expect (parse_res
== -1, 0))
123 return NSS_STATUS_TRYAGAIN
;
131 while (parse_res
< 1);
133 return NSS_STATUS_SUCCESS
;
137 _nss_nis_getgrent_r (struct group
*result
, char *buffer
, size_t buflen
,
142 __libc_lock_lock (lock
);
144 status
= internal_nis_getgrent_r (result
, buffer
, buflen
, errnop
);
146 __libc_lock_unlock (lock
);
152 _nss_nis_getgrnam_r (const char *name
, struct group
*grp
,
153 char *buffer
, size_t buflen
, int *errnop
)
158 return NSS_STATUS_UNAVAIL
;
162 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
163 return NSS_STATUS_UNAVAIL
;
167 int yperr
= yp_match (domain
, "group.byname", name
, strlen (name
), &result
,
170 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
172 enum nss_status retval
= yperr2nss (yperr
);
174 if (retval
== NSS_STATUS_TRYAGAIN
)
179 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
183 return NSS_STATUS_TRYAGAIN
;
186 char *p
= strncpy (buffer
, result
, len
);
192 int parse_res
= _nss_files_parse_grent (p
, grp
, (void *) buffer
, buflen
,
194 if (__builtin_expect (parse_res
< 1, 0))
197 return NSS_STATUS_TRYAGAIN
;
199 return NSS_STATUS_NOTFOUND
;
201 return NSS_STATUS_SUCCESS
;
205 _nss_nis_getgrgid_r (gid_t gid
, struct group
*grp
,
206 char *buffer
, size_t buflen
, int *errnop
)
209 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
210 return NSS_STATUS_UNAVAIL
;
213 int nlen
= sprintf (buf
, "%lu", (unsigned long int) gid
);
217 int yperr
= yp_match (domain
, "group.bygid", buf
, nlen
, &result
, &len
);
219 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
221 enum nss_status retval
= yperr2nss (yperr
);
223 if (retval
== NSS_STATUS_TRYAGAIN
)
228 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
232 return NSS_STATUS_TRYAGAIN
;
235 char *p
= strncpy (buffer
, result
, len
);
241 int parse_res
= _nss_files_parse_grent (p
, grp
, (void *) buffer
, buflen
,
243 if (__builtin_expect (parse_res
< 1, 0))
246 return NSS_STATUS_TRYAGAIN
;
248 return NSS_STATUS_NOTFOUND
;
250 return NSS_STATUS_SUCCESS
;