Update.
[glibc.git] / nis / nss_nis / nis-grp.c
blob6dca66e4df025240edba9c27054b5aa31e63a050
1 /* Copyright (C) 1996, 1997, 1998 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. */
20 #include <nss.h>
21 #include <grp.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
29 #include "nss-nis.h"
31 /* Get the declaration of the parser function. */
32 #define ENTNAME grent
33 #define STRUCTURE group
34 #define EXTERN_PARSER
35 #include <nss/nss_files/files-parse.c>
37 /* Protect global state against multiple changers */
38 __libc_lock_define_initialized (static, lock)
40 static bool_t new_start = 1;
41 static char *oldkey = NULL;
42 static int oldkeylen = 0;
44 enum nss_status
45 _nss_nis_setgrent (void)
47 __libc_lock_lock (lock);
49 new_start = 1;
50 if (oldkey != NULL)
52 free (oldkey);
53 oldkey = NULL;
54 oldkeylen = 0;
57 __libc_lock_unlock (lock);
59 return NSS_STATUS_SUCCESS;
62 enum nss_status
63 _nss_nis_endgrent (void)
65 __libc_lock_lock (lock);
67 new_start = 1;
68 if (oldkey != NULL)
70 free (oldkey);
71 oldkey = NULL;
72 oldkeylen = 0;
75 __libc_lock_unlock (lock);
77 return NSS_STATUS_SUCCESS;
80 static enum nss_status
81 internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen,
82 int *errnop)
84 struct parser_data *data = (void *) buffer;
85 char *domain, *result, *outkey;
86 int len, keylen, parse_res;
88 if (yp_get_default_domain (&domain))
89 return NSS_STATUS_UNAVAIL;
91 /* Get the next entry until we found a correct one. */
94 enum nss_status retval;
95 char *p;
97 if (new_start)
98 retval = yperr2nss (yp_first (domain, "group.byname",
99 &outkey, &keylen, &result, &len));
100 else
101 retval = yperr2nss ( yp_next (domain, "group.byname",
102 oldkey, oldkeylen,
103 &outkey, &keylen, &result, &len));
105 if (retval != NSS_STATUS_SUCCESS)
107 if (retval == NSS_STATUS_NOTFOUND)
108 *errnop = ENOENT;
109 else if (retval == NSS_STATUS_TRYAGAIN)
110 *errnop = errno;
111 return retval;
114 if ((size_t) (len + 1) > buflen)
116 free (result);
117 *errnop = ERANGE;
118 return NSS_STATUS_TRYAGAIN;
121 p = strncpy (buffer, result, len);
122 buffer[len] = '\0';
123 while (isspace (*p))
124 ++p;
125 free (result);
127 parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
128 if (parse_res == -1)
130 free (outkey);
131 *errnop = ERANGE;
132 return NSS_STATUS_TRYAGAIN;
135 free (oldkey);
136 oldkey = outkey;
137 oldkeylen = keylen;
138 new_start = 0;
140 while (parse_res < 1);
142 return NSS_STATUS_SUCCESS;
145 enum nss_status
146 _nss_nis_getgrent_r (struct group *result, char *buffer, size_t buflen,
147 int *errnop)
149 int status;
151 __libc_lock_lock (lock);
153 status = internal_nis_getgrent_r (result, buffer, buflen, errnop);
155 __libc_lock_unlock (lock);
157 return status;
160 enum nss_status
161 _nss_nis_getgrnam_r (const char *name, struct group *grp,
162 char *buffer, size_t buflen, int *errnop)
164 struct parser_data *data = (void *) buffer;
165 enum nss_status retval;
166 char *domain, *result, *p;
167 int len, parse_res;
169 if (name == NULL)
171 *errnop = EINVAL;
172 return NSS_STATUS_UNAVAIL;
175 if (yp_get_default_domain (&domain))
176 return NSS_STATUS_UNAVAIL;
178 retval = yperr2nss (yp_match (domain, "group.byname", name,
179 strlen (name), &result, &len));
181 if (retval != NSS_STATUS_SUCCESS)
183 if (retval == NSS_STATUS_NOTFOUND)
184 *errnop = EAGAIN;
185 else if (retval == NSS_STATUS_TRYAGAIN)
186 *errnop = errno;
187 return retval;
190 if ((size_t) (len + 1) > buflen)
192 free (result);
193 *errnop = ERANGE;
194 return NSS_STATUS_TRYAGAIN;
197 p = strncpy (buffer, result, len);
198 buffer[len] = '\0';
199 while (isspace (*p))
200 ++p;
201 free (result);
203 parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
204 if (parse_res < 1)
206 if (parse_res == -1)
207 return NSS_STATUS_TRYAGAIN;
208 else
210 *errnop = EAGAIN;
211 return NSS_STATUS_NOTFOUND;
214 return NSS_STATUS_SUCCESS;
217 enum nss_status
218 _nss_nis_getgrgid_r (gid_t gid, struct group *grp,
219 char *buffer, size_t buflen, int *errnop)
221 struct parser_data *data = (void *) buffer;
222 enum nss_status retval;
223 char *domain, *result, *p;
224 int len, nlen, parse_res;
225 char buf[32];
227 if (yp_get_default_domain (&domain))
228 return NSS_STATUS_UNAVAIL;
230 nlen = sprintf (buf, "%d", gid);
232 retval = yperr2nss (yp_match (domain, "group.bygid", buf,
233 nlen, &result, &len));
235 if (retval != NSS_STATUS_SUCCESS)
237 if (retval == NSS_STATUS_NOTFOUND)
238 *errnop = ENOENT;
239 else if (retval == NSS_STATUS_TRYAGAIN)
240 *errnop = errno;
241 return retval;
244 if ((size_t) (len + 1) > buflen)
246 free (result);
247 *errnop = ERANGE;
248 return NSS_STATUS_TRYAGAIN;
251 p = strncpy (buffer, result, len);
252 buffer[len] = '\0';
253 while (isspace (*p))
254 ++p;
255 free (result);
257 parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
258 if (parse_res < 1)
260 if (parse_res == -1)
261 return NSS_STATUS_TRYAGAIN;
262 else
264 *errnop = ENOENT;
265 return NSS_STATUS_NOTFOUND;
268 return NSS_STATUS_SUCCESS;