1/* Copyright (C) 1997-2016 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.
4
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.
9
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.
14
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/>. */
18
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>
27
28#include "nss-nisplus.h"
29#include "nisplus-parser.h"
30#include <libnsl.h>
31#include <nis_intern.h>
32#include <nis_xdr.h>
33
34
35__libc_lock_define_initialized (static, lock);
36
37/* Connection information. */
38static ib_request *ibreq;
39static directory_obj *dir;
40static dir_binding bptr;
41static char *tablepath;
42static char *tableptr;
43/* Cursor. */
44static netobj cursor;
45
46
47nis_name grp_tablename_val attribute_hidden;
48size_t grp_tablename_len attribute_hidden;
49
50enum nss_status
51_nss_grp_create_tablename (int *errnop)
52{
53 if (grp_tablename_val == NULL)
54 {
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.";
58
59 char *p = malloc (sizeof (prefix) + local_dir_len);
60 if (p == NULL)
61 {
62 *errnop = errno;
63 return NSS_STATUS_TRYAGAIN;
64 }
65
66 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
67
68 grp_tablename_len = sizeof (prefix) - 1 + local_dir_len;
69
70 atomic_write_barrier ();
71
72 if (atomic_compare_and_exchange_bool_acq (&grp_tablename_val, p, NULL))
73 {
74 /* Another thread already installed the value. */
75 free (p);
76 grp_tablename_len = strlen (grp_tablename_val);
77 }
78 }
79
80 return NSS_STATUS_SUCCESS;
81}
82
83
84static void
85internal_endgrent (void)
86{
87 __nisbind_destroy (&bptr);
88 memset (&bptr, '\0', sizeof (bptr));
89
90 nis_free_directory (dir);
91 dir = NULL;
92
93 nis_free_request (ibreq);
94 ibreq = NULL;
95
96 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
97 memset (&cursor, '\0', sizeof (cursor));
98
99 free (tablepath);
100 tableptr = tablepath = NULL;
101}
102
103
104static enum nss_status
105internal_setgrent (int *errnop)
106{
107 enum nss_status status = NSS_STATUS_SUCCESS;
108
109 if (grp_tablename_val == NULL)
110 status = _nss_grp_create_tablename (errnop);
111
112 if (status == NSS_STATUS_SUCCESS)
113 {
114 ibreq = __create_ib_request (grp_tablename_val, 0);
115 if (ibreq == NULL)
116 {
117 *errnop = errno;
118 return NSS_STATUS_TRYAGAIN;
119 }
120
121 nis_error retcode = __prepare_niscall (grp_tablename_val, &dir, &bptr, 0);
122 if (retcode != NIS_SUCCESS)
123 {
124 nis_free_request (ibreq);
125 ibreq = NULL;
126 status = niserr2nss (retcode);
127 }
128 }
129
130 return status;
131}
132
133
134enum nss_status
135_nss_nisplus_setgrent (int stayopen)
136{
137 enum nss_status status;
138
139 __libc_lock_lock (lock);
140
141 internal_endgrent ();
142
143 // XXX We need to be able to set errno. Pass in new parameter.
144 int err;
145 status = internal_setgrent (&err);
146
147 __libc_lock_unlock (lock);
148
149 return status;
150}
151
152
153enum nss_status
154_nss_nisplus_endgrent (void)
155{
156 __libc_lock_lock (lock);
157
158 internal_endgrent ();
159
160 __libc_lock_unlock (lock);
161
162 return NSS_STATUS_SUCCESS;
163}
164
165
166static enum nss_status
167internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen,
168 int *errnop)
169{
170 int parse_res = -1;
171 enum nss_status retval = NSS_STATUS_SUCCESS;
172
173 /* Get the next entry until we found a correct one. */
174 do
175 {
176 nis_error status;
177 nis_result result;
178 memset (&result, '\0', sizeof (result));
179
180 if (cursor.n_bytes == NULL)
181 {
182 if (ibreq == NULL)
183 {
184 retval = internal_setgrent (errnop);
185 if (retval != NSS_STATUS_SUCCESS)
186 return retval;
187 }
188
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);
195 }
196 else
197 {
198 ibreq->ibr_cookie.n_bytes = cursor.n_bytes;
199 ibreq->ibr_cookie.n_len = cursor.n_len;
200
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);
207
208 ibreq->ibr_cookie.n_bytes = NULL;
209 ibreq->ibr_cookie.n_len = 0;
210 }
211
212 if (status != NIS_SUCCESS)
213 return niserr2nss (status);
214
215 if (NIS_RES_STATUS (&result) == NIS_NOTFOUND)
216 {
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);
222
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);
228
229 nis_free_directory (dir);
230 dir = newdir;
231 __nisbind_destroy (&bptr);
232 bptr = newbptr;
233
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;
239 }
240 else if (NIS_RES_STATUS (&result) != NIS_SUCCESS)
241 return niserr2nss (NIS_RES_STATUS (&result));
242
243 parse_res = _nss_nisplus_parse_grent (&result, gr,
244 buffer, buflen, errnop);
245 if (__glibc_unlikely (parse_res == -1))
246 {
247 *errnop = ERANGE;
248 retval = NSS_STATUS_TRYAGAIN;
249 goto freeres;
250 }
251
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));
264 }
265 while (!parse_res);
266
267 return retval;
268}
269
270enum nss_status
271_nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen,
272 int *errnop)
273{
274 int status;
275
276 __libc_lock_lock (lock);
277
278 status = internal_nisplus_getgrent_r (result, buffer, buflen, errnop);
279
280 __libc_lock_unlock (lock);
281
282 return status;
283}
284
285enum nss_status
286_nss_nisplus_getgrnam_r (const char *name, struct group *gr,
287 char *buffer, size_t buflen, int *errnop)
288{
289 int parse_res;
290
291 if (grp_tablename_val == NULL)
292 {
293 enum nss_status status = _nss_grp_create_tablename (errnop);
294
295 if (status != NSS_STATUS_SUCCESS)
296 return status;
297 }
298
299 if (name == NULL)
300 {
301 *errnop = EINVAL;
302 return NSS_STATUS_NOTFOUND;
303 }
304
305 nis_result *result;
306 char buf[strlen (name) + 9 + grp_tablename_len];
307 int olderr = errno;
308
309 snprintf (buf, sizeof (buf), "[name=%s],%s", name, grp_tablename_val);
310
311 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
312
313 if (result == NULL)
314 {
315 *errnop = ENOMEM;
316 return NSS_STATUS_TRYAGAIN;
317 }
318
319 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
320 {
321 enum nss_status status = niserr2nss (result->status);
322
323 nis_freeresult (result);
324 return status;
325 }
326
327 parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop);
328 nis_freeresult (result);
329 if (__glibc_unlikely (parse_res < 1))
330 {
331 if (parse_res == -1)
332 {
333 *errnop = ERANGE;
334 return NSS_STATUS_TRYAGAIN;
335 }
336 else
337 {
338 __set_errno (olderr);
339 return NSS_STATUS_NOTFOUND;
340 }
341 }
342
343 return NSS_STATUS_SUCCESS;
344}
345
346enum nss_status
347_nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
348 char *buffer, size_t buflen, int *errnop)
349{
350 if (grp_tablename_val == NULL)
351 {
352 enum nss_status status = _nss_grp_create_tablename (errnop);
353
354 if (status != NSS_STATUS_SUCCESS)
355 return status;
356 }
357
358 int parse_res;
359 nis_result *result;
360 char buf[8 + 3 * sizeof (unsigned long int) + grp_tablename_len];
361 int olderr = errno;
362
363 snprintf (buf, sizeof (buf), "[gid=%lu],%s",
364 (unsigned long int) gid, grp_tablename_val);
365
366 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
367
368 if (result == NULL)
369 {
370 *errnop = ENOMEM;
371 return NSS_STATUS_TRYAGAIN;
372 }
373
374 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
375 {
376 enum nss_status status = niserr2nss (result->status);
377
378 __set_errno (olderr);
379
380 nis_freeresult (result);
381 return status;
382 }
383
384 parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop);
385
386 nis_freeresult (result);
387 if (__glibc_unlikely (parse_res < 1))
388 {
389 __set_errno (olderr);
390
391 if (parse_res == -1)
392 {
393 *errnop = ERANGE;
394 return NSS_STATUS_TRYAGAIN;
395 }
396 else
397 return NSS_STATUS_NOTFOUND;
398 }
399
400 return NSS_STATUS_SUCCESS;
401}
402