regex: Gnulib unibyte RRI uses bytes not chars
[glibc.git] / nis / nss_nisplus / nisplus-grp.c
blobd3ee6cedc012290bf66e96e117d354f9726c35d5
1 /* Copyright (C) 1997-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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/>. */
19 #include <atomic.h>
20 #include <nss.h>
21 #include <grp.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
29 #include "nisplus-parser.h"
30 #include <libnsl.h>
31 #include <nis_intern.h>
32 #include <nis_xdr.h>
35 __libc_lock_define_initialized (static, lock);
37 /* Connection information. */
38 static ib_request *ibreq;
39 static directory_obj *dir;
40 static dir_binding bptr;
41 static char *tablepath;
42 static char *tableptr;
43 /* Cursor. */
44 static netobj cursor;
47 nis_name grp_tablename_val attribute_hidden;
48 size_t grp_tablename_len attribute_hidden;
50 enum nss_status
51 _nss_grp_create_tablename (int *errnop)
53 if (grp_tablename_val == NULL)
55 const char *local_dir = nis_local_directory ();
56 size_t local_dir_len = strlen (local_dir);
57 static const char prefix[] = "group.org_dir.";
59 char *p = malloc (sizeof (prefix) + local_dir_len);
60 if (p == NULL)
62 *errnop = errno;
63 return NSS_STATUS_TRYAGAIN;
66 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
68 grp_tablename_len = sizeof (prefix) - 1 + local_dir_len;
70 atomic_write_barrier ();
72 if (atomic_compare_and_exchange_bool_acq (&grp_tablename_val, p, NULL))
74 /* Another thread already installed the value. */
75 free (p);
76 grp_tablename_len = strlen (grp_tablename_val);
80 return NSS_STATUS_SUCCESS;
84 static void
85 internal_endgrent (void)
87 __nisbind_destroy (&bptr);
88 memset (&bptr, '\0', sizeof (bptr));
90 nis_free_directory (dir);
91 dir = NULL;
93 nis_free_request (ibreq);
94 ibreq = NULL;
96 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
97 memset (&cursor, '\0', sizeof (cursor));
99 free (tablepath);
100 tableptr = tablepath = NULL;
104 static enum nss_status
105 internal_setgrent (int *errnop)
107 enum nss_status status = NSS_STATUS_SUCCESS;
109 if (grp_tablename_val == NULL)
110 status = _nss_grp_create_tablename (errnop);
112 if (status == NSS_STATUS_SUCCESS)
114 ibreq = __create_ib_request (grp_tablename_val, 0);
115 if (ibreq == NULL)
117 *errnop = errno;
118 return NSS_STATUS_TRYAGAIN;
121 nis_error retcode = __prepare_niscall (grp_tablename_val, &dir, &bptr, 0);
122 if (retcode != NIS_SUCCESS)
124 nis_free_request (ibreq);
125 ibreq = NULL;
126 status = niserr2nss (retcode);
130 return status;
134 enum nss_status
135 _nss_nisplus_setgrent (int stayopen)
137 enum nss_status status;
139 __libc_lock_lock (lock);
141 internal_endgrent ();
143 // XXX We need to be able to set errno. Pass in new parameter.
144 int err;
145 status = internal_setgrent (&err);
147 __libc_lock_unlock (lock);
149 return status;
153 enum nss_status
154 _nss_nisplus_endgrent (void)
156 __libc_lock_lock (lock);
158 internal_endgrent ();
160 __libc_lock_unlock (lock);
162 return NSS_STATUS_SUCCESS;
166 static enum nss_status
167 internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen,
168 int *errnop)
170 int parse_res = -1;
171 enum nss_status retval = NSS_STATUS_SUCCESS;
173 /* Get the next entry until we found a correct one. */
176 nis_error status;
177 nis_result result;
178 memset (&result, '\0', sizeof (result));
180 if (cursor.n_bytes == NULL)
182 if (ibreq == NULL)
184 retval = internal_setgrent (errnop);
185 if (retval != NSS_STATUS_SUCCESS)
186 return retval;
189 status = __do_niscall3 (&bptr, NIS_IBFIRST,
190 (xdrproc_t) _xdr_ib_request,
191 (caddr_t) ibreq,
192 (xdrproc_t) _xdr_nis_result,
193 (caddr_t) &result,
194 0, NULL);
196 else
198 ibreq->ibr_cookie.n_bytes = cursor.n_bytes;
199 ibreq->ibr_cookie.n_len = cursor.n_len;
201 status = __do_niscall3 (&bptr, NIS_IBNEXT,
202 (xdrproc_t) _xdr_ib_request,
203 (caddr_t) ibreq,
204 (xdrproc_t) _xdr_nis_result,
205 (caddr_t) &result,
206 0, NULL);
208 ibreq->ibr_cookie.n_bytes = NULL;
209 ibreq->ibr_cookie.n_len = 0;
212 if (status != NIS_SUCCESS)
213 return niserr2nss (status);
215 if (NIS_RES_STATUS (&result) == NIS_NOTFOUND)
217 /* No more entries on this server. This means we have to go
218 to the next server on the path. */
219 status = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
220 if (status != NIS_SUCCESS)
221 return niserr2nss (status);
223 directory_obj *newdir = NULL;
224 dir_binding newbptr;
225 status = __prepare_niscall (ibreq->ibr_name, &newdir, &newbptr, 0);
226 if (status != NIS_SUCCESS)
227 return niserr2nss (status);
229 nis_free_directory (dir);
230 dir = newdir;
231 __nisbind_destroy (&bptr);
232 bptr = newbptr;
234 xdr_free ((xdrproc_t) xdr_netobj, (char *) &result.cookie);
235 result.cookie.n_bytes = NULL;
236 result.cookie.n_len = 0;
237 parse_res = 0;
238 goto next;
240 else if (NIS_RES_STATUS (&result) != NIS_SUCCESS)
241 return niserr2nss (NIS_RES_STATUS (&result));
243 parse_res = _nss_nisplus_parse_grent (&result, gr,
244 buffer, buflen, errnop);
245 if (__glibc_unlikely (parse_res == -1))
247 *errnop = ERANGE;
248 retval = NSS_STATUS_TRYAGAIN;
249 goto freeres;
252 next:
253 /* Free the old cursor. */
254 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
255 /* Remember the new one. */
256 cursor.n_bytes = result.cookie.n_bytes;
257 cursor.n_len = result.cookie.n_len;
258 /* Free the result structure. NB: we do not remove the cookie. */
259 result.cookie.n_bytes = NULL;
260 result.cookie.n_len = 0;
261 freeres:
262 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &result);
263 memset (&result, '\0', sizeof (result));
265 while (!parse_res);
267 return retval;
270 enum nss_status
271 _nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen,
272 int *errnop)
274 int status;
276 __libc_lock_lock (lock);
278 status = internal_nisplus_getgrent_r (result, buffer, buflen, errnop);
280 __libc_lock_unlock (lock);
282 return status;
285 enum nss_status
286 _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
287 char *buffer, size_t buflen, int *errnop)
289 int parse_res;
291 if (grp_tablename_val == NULL)
293 enum nss_status status = _nss_grp_create_tablename (errnop);
295 if (status != NSS_STATUS_SUCCESS)
296 return status;
299 if (name == NULL)
301 *errnop = EINVAL;
302 return NSS_STATUS_NOTFOUND;
305 nis_result *result;
306 char buf[strlen (name) + 9 + grp_tablename_len];
307 int olderr = errno;
309 snprintf (buf, sizeof (buf), "[name=%s],%s", name, grp_tablename_val);
311 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
313 if (result == NULL)
315 *errnop = ENOMEM;
316 return NSS_STATUS_TRYAGAIN;
319 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
321 enum nss_status status = niserr2nss (result->status);
323 nis_freeresult (result);
324 return status;
327 parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop);
328 nis_freeresult (result);
329 if (__glibc_unlikely (parse_res < 1))
331 if (parse_res == -1)
333 *errnop = ERANGE;
334 return NSS_STATUS_TRYAGAIN;
336 else
338 __set_errno (olderr);
339 return NSS_STATUS_NOTFOUND;
343 return NSS_STATUS_SUCCESS;
346 enum nss_status
347 _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
348 char *buffer, size_t buflen, int *errnop)
350 if (grp_tablename_val == NULL)
352 enum nss_status status = _nss_grp_create_tablename (errnop);
354 if (status != NSS_STATUS_SUCCESS)
355 return status;
358 int parse_res;
359 nis_result *result;
360 char buf[8 + 3 * sizeof (unsigned long int) + grp_tablename_len];
361 int olderr = errno;
363 snprintf (buf, sizeof (buf), "[gid=%lu],%s",
364 (unsigned long int) gid, grp_tablename_val);
366 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
368 if (result == NULL)
370 *errnop = ENOMEM;
371 return NSS_STATUS_TRYAGAIN;
374 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
376 enum nss_status status = niserr2nss (result->status);
378 __set_errno (olderr);
380 nis_freeresult (result);
381 return status;
384 parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop);
386 nis_freeresult (result);
387 if (__glibc_unlikely (parse_res < 1))
389 __set_errno (olderr);
391 if (parse_res == -1)
393 *errnop = ERANGE;
394 return NSS_STATUS_TRYAGAIN;
396 else
397 return NSS_STATUS_NOTFOUND;
400 return NSS_STATUS_SUCCESS;