Updated to fedora-glibc-20060424T0820
[glibc.git] / nis / nss_nis / nis-grp.c
blob68f3ced99205f38eacb7c7ca4024e4e49675305c
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
18 02111-1307 USA. */
20 #include <nss.h>
21 /* The following is an ugly trick to avoid a prototype declaration for
22 _nss_nis_endgrent. */
23 #define _nss_nis_endgrent _nss_nis_endgrent_XXX
24 #include <grp.h>
25 #undef _nss_nis_endgrent
26 #include <ctype.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <bits/libc-lock.h>
30 #include <rpcsvc/yp.h>
31 #include <rpcsvc/ypclnt.h>
33 #include "nss-nis.h"
35 /* Get the declaration of the parser function. */
36 #define ENTNAME grent
37 #define STRUCTURE group
38 #define EXTERN_PARSER
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;
45 static char *oldkey;
46 static int oldkeylen;
48 enum nss_status
49 _nss_nis_setgrent (int stayopen)
51 __libc_lock_lock (lock);
53 new_start = 1;
54 if (oldkey != NULL)
56 free (oldkey);
57 oldkey = NULL;
58 oldkeylen = 0;
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,
72 int *errnop)
74 char *domain;
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. */
79 int parse_res;
82 char *result;
83 char *outkey;
84 int len;
85 int keylen;
86 int yperr;
88 if (new_start)
89 yperr = yp_first (domain, "group.byname", &outkey, &keylen, &result,
90 &len);
91 else
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)
100 *errnop = errno;
101 return retval;
104 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
106 free (result);
107 *errnop = ERANGE;
108 return NSS_STATUS_TRYAGAIN;
111 char *p = strncpy (buffer, result, len);
112 buffer[len] = '\0';
113 while (isspace (*p))
114 ++p;
115 free (result);
117 parse_res = _nss_files_parse_grent (p, grp, (void *) buffer, buflen,
118 errnop);
119 if (__builtin_expect (parse_res == -1, 0))
121 free (outkey);
122 *errnop = ERANGE;
123 return NSS_STATUS_TRYAGAIN;
126 free (oldkey);
127 oldkey = outkey;
128 oldkeylen = keylen;
129 new_start = 0;
131 while (parse_res < 1);
133 return NSS_STATUS_SUCCESS;
136 enum nss_status
137 _nss_nis_getgrent_r (struct group *result, char *buffer, size_t buflen,
138 int *errnop)
140 int status;
142 __libc_lock_lock (lock);
144 status = internal_nis_getgrent_r (result, buffer, buflen, errnop);
146 __libc_lock_unlock (lock);
148 return status;
151 enum nss_status
152 _nss_nis_getgrnam_r (const char *name, struct group *grp,
153 char *buffer, size_t buflen, int *errnop)
155 if (name == NULL)
157 *errnop = EINVAL;
158 return NSS_STATUS_UNAVAIL;
161 char *domain;
162 if (__builtin_expect (yp_get_default_domain (&domain), 0))
163 return NSS_STATUS_UNAVAIL;
165 char *result;
166 int len;
167 int yperr = yp_match (domain, "group.byname", name, strlen (name), &result,
168 &len);
170 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
172 enum nss_status retval = yperr2nss (yperr);
174 if (retval == NSS_STATUS_TRYAGAIN)
175 *errnop = errno;
176 return retval;
179 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
181 free (result);
182 *errnop = ERANGE;
183 return NSS_STATUS_TRYAGAIN;
186 char *p = strncpy (buffer, result, len);
187 buffer[len] = '\0';
188 while (isspace (*p))
189 ++p;
190 free (result);
192 int parse_res = _nss_files_parse_grent (p, grp, (void *) buffer, buflen,
193 errnop);
194 if (__builtin_expect (parse_res < 1, 0))
196 if (parse_res == -1)
197 return NSS_STATUS_TRYAGAIN;
198 else
199 return NSS_STATUS_NOTFOUND;
201 return NSS_STATUS_SUCCESS;
204 enum nss_status
205 _nss_nis_getgrgid_r (gid_t gid, struct group *grp,
206 char *buffer, size_t buflen, int *errnop)
208 char *domain;
209 if (__builtin_expect (yp_get_default_domain (&domain), 0))
210 return NSS_STATUS_UNAVAIL;
212 char buf[32];
213 int nlen = sprintf (buf, "%lu", (unsigned long int) gid);
215 char *result;
216 int len;
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)
224 *errnop = errno;
225 return retval;
228 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
230 free (result);
231 *errnop = ERANGE;
232 return NSS_STATUS_TRYAGAIN;
235 char *p = strncpy (buffer, result, len);
236 buffer[len] = '\0';
237 while (isspace (*p))
238 ++p;
239 free (result);
241 int parse_res = _nss_files_parse_grent (p, grp, (void *) buffer, buflen,
242 errnop);
243 if (__builtin_expect (parse_res < 1, 0))
245 if (parse_res == -1)
246 return NSS_STATUS_TRYAGAIN;
247 else
248 return NSS_STATUS_NOTFOUND;
250 return NSS_STATUS_SUCCESS;