]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 1997-2014 Free Software Foundation, Inc. |
e61abf83 UD |
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 | |
41bdb6e2 AJ |
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. | |
e61abf83 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
e61abf83 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
e61abf83 | 18 | |
48b29391 | 19 | #include <atomic.h> |
e61abf83 UD |
20 | #include <nss.h> |
21 | #include <grp.h> | |
22 | #include <ctype.h> | |
23 | #include <errno.h> | |
24 | #include <string.h> | |
5107cf1d | 25 | #include <bits/libc-lock.h> |
e61abf83 | 26 | #include <rpcsvc/nis.h> |
e61abf83 UD |
27 | |
28 | #include "nss-nisplus.h" | |
2d7da676 | 29 | #include "nisplus-parser.h" |
a17fa610 UD |
30 | #include <libnsl.h> |
31 | #include <nis_intern.h> | |
32 | #include <nis_xdr.h> | |
e61abf83 | 33 | |
48b29391 | 34 | |
e61abf83 UD |
35 | __libc_lock_define_initialized (static, lock); |
36 | ||
a17fa610 UD |
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; | |
45 | ||
46 | ||
68361572 UD |
47 | nis_name grp_tablename_val attribute_hidden; |
48 | size_t grp_tablename_len attribute_hidden; | |
0ecb606c | 49 | |
68361572 UD |
50 | enum nss_status |
51 | _nss_grp_create_tablename (int *errnop) | |
e61abf83 | 52 | { |
68361572 | 53 | if (grp_tablename_val == NULL) |
e61abf83 | 54 | { |
48b29391 UD |
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."; | |
2d7da676 | 58 | |
48b29391 | 59 | char *p = malloc (sizeof (prefix) + local_dir_len); |
c8e82b4a | 60 | if (p == NULL) |
d71b808a UD |
61 | { |
62 | *errnop = errno; | |
63 | return NSS_STATUS_TRYAGAIN; | |
64 | } | |
48b29391 UD |
65 | |
66 | memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1); | |
67 | ||
68361572 | 68 | grp_tablename_len = sizeof (prefix) - 1 + local_dir_len; |
48b29391 UD |
69 | |
70 | atomic_write_barrier (); | |
71 | ||
68361572 UD |
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 | } | |
e61abf83 | 78 | } |
48b29391 | 79 | |
2d7da676 UD |
80 | return NSS_STATUS_SUCCESS; |
81 | } | |
5107cf1d | 82 | |
a17fa610 UD |
83 | |
84 | static void | |
85 | internal_endgrent (void) | |
0ecb606c | 86 | { |
a17fa610 UD |
87 | __nisbind_destroy (&bptr); |
88 | memset (&bptr, '\0', sizeof (bptr)); | |
0ecb606c | 89 | |
a17fa610 UD |
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 | ||
104 | static enum nss_status | |
105 | internal_setgrent (int *errnop) | |
106 | { | |
107 | enum nss_status status = NSS_STATUS_SUCCESS; | |
0ecb606c | 108 | |
68361572 UD |
109 | if (grp_tablename_val == NULL) |
110 | status = _nss_grp_create_tablename (errnop); | |
0ecb606c | 111 | |
a17fa610 | 112 | if (status == NSS_STATUS_SUCCESS) |
a334319f | 113 | { |
68361572 | 114 | ibreq = __create_ib_request (grp_tablename_val, 0); |
a17fa610 | 115 | if (ibreq == NULL) |
0ecb606c | 116 | { |
a17fa610 UD |
117 | *errnop = errno; |
118 | return NSS_STATUS_TRYAGAIN; | |
119 | } | |
120 | ||
68361572 | 121 | nis_error retcode = __prepare_niscall (grp_tablename_val, &dir, &bptr, 0); |
a17fa610 UD |
122 | if (retcode != NIS_SUCCESS) |
123 | { | |
124 | nis_free_request (ibreq); | |
125 | ibreq = NULL; | |
126 | status = niserr2nss (retcode); | |
901956a5 | 127 | } |
e61abf83 | 128 | } |
a17fa610 | 129 | |
8f2ece69 | 130 | return status; |
e61abf83 UD |
131 | } |
132 | ||
a17fa610 | 133 | |
e61abf83 | 134 | enum nss_status |
51eecc4a | 135 | _nss_nisplus_setgrent (int stayopen) |
e61abf83 | 136 | { |
2d7da676 UD |
137 | enum nss_status status; |
138 | ||
e61abf83 UD |
139 | __libc_lock_lock (lock); |
140 | ||
a17fa610 UD |
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); | |
e61abf83 UD |
146 | |
147 | __libc_lock_unlock (lock); | |
148 | ||
2d7da676 | 149 | return status; |
e61abf83 UD |
150 | } |
151 | ||
a17fa610 | 152 | |
e61abf83 UD |
153 | enum nss_status |
154 | _nss_nisplus_endgrent (void) | |
155 | { | |
156 | __libc_lock_lock (lock); | |
157 | ||
a17fa610 | 158 | internal_endgrent (); |
e61abf83 UD |
159 | |
160 | __libc_lock_unlock (lock); | |
161 | ||
162 | return NSS_STATUS_SUCCESS; | |
163 | } | |
164 | ||
a17fa610 | 165 | |
e61abf83 | 166 | static enum nss_status |
d71b808a UD |
167 | internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen, |
168 | int *errnop) | |
e61abf83 | 169 | { |
a17fa610 UD |
170 | int parse_res = -1; |
171 | enum nss_status retval = NSS_STATUS_SUCCESS; | |
e61abf83 | 172 | |
a334319f UD |
173 | /* Get the next entry until we found a correct one. */ |
174 | do | |
175 | { | |
a17fa610 UD |
176 | nis_error status; |
177 | nis_result result; | |
178 | memset (&result, '\0', sizeof (result)); | |
0ecb606c | 179 | |
a17fa610 UD |
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, | |
d71b808a | 244 | buffer, buflen, errnop); |
a1ffb40e | 245 | if (__glibc_unlikely (parse_res == -1)) |
a17fa610 UD |
246 | { |
247 | *errnop = ERANGE; | |
248 | retval = NSS_STATUS_TRYAGAIN; | |
249 | goto freeres; | |
250 | } | |
60c96635 | 251 | |
a17fa610 UD |
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)); | |
e61abf83 UD |
264 | } |
265 | while (!parse_res); | |
266 | ||
a17fa610 | 267 | return retval; |
e61abf83 UD |
268 | } |
269 | ||
270 | enum nss_status | |
d71b808a UD |
271 | _nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen, |
272 | int *errnop) | |
e61abf83 UD |
273 | { |
274 | int status; | |
275 | ||
276 | __libc_lock_lock (lock); | |
277 | ||
d71b808a | 278 | status = internal_nisplus_getgrent_r (result, buffer, buflen, errnop); |
e61abf83 UD |
279 | |
280 | __libc_lock_unlock (lock); | |
281 | ||
282 | return status; | |
283 | } | |
284 | ||
285 | enum nss_status | |
286 | _nss_nisplus_getgrnam_r (const char *name, struct group *gr, | |
d71b808a | 287 | char *buffer, size_t buflen, int *errnop) |
e61abf83 UD |
288 | { |
289 | int parse_res; | |
290 | ||
68361572 | 291 | if (grp_tablename_val == NULL) |
d71b808a | 292 | { |
68361572 | 293 | enum nss_status status = _nss_grp_create_tablename (errnop); |
48b29391 | 294 | |
d71b808a UD |
295 | if (status != NSS_STATUS_SUCCESS) |
296 | return status; | |
297 | } | |
2d7da676 | 298 | |
ac9f45cf UD |
299 | if (name == NULL) |
300 | { | |
301 | *errnop = EINVAL; | |
302 | return NSS_STATUS_NOTFOUND; | |
303 | } | |
48b29391 UD |
304 | |
305 | nis_result *result; | |
68361572 | 306 | char buf[strlen (name) + 9 + grp_tablename_len]; |
48b29391 UD |
307 | int olderr = errno; |
308 | ||
68361572 | 309 | snprintf (buf, sizeof (buf), "[name=%s],%s", name, grp_tablename_val); |
48b29391 UD |
310 | |
311 | result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL); | |
312 | ||
313 | if (result == NULL) | |
e61abf83 | 314 | { |
48b29391 UD |
315 | *errnop = ENOMEM; |
316 | return NSS_STATUS_TRYAGAIN; | |
317 | } | |
e61abf83 | 318 | |
a1ffb40e | 319 | if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS)) |
48b29391 UD |
320 | { |
321 | enum nss_status status = niserr2nss (result->status); | |
e61abf83 | 322 | |
48b29391 UD |
323 | nis_freeresult (result); |
324 | return status; | |
325 | } | |
e61abf83 | 326 | |
a17fa610 | 327 | parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop); |
48b29391 | 328 | nis_freeresult (result); |
a1ffb40e | 329 | if (__glibc_unlikely (parse_res < 1)) |
48b29391 UD |
330 | { |
331 | if (parse_res == -1) | |
901956a5 | 332 | { |
48b29391 | 333 | *errnop = ERANGE; |
901956a5 UD |
334 | return NSS_STATUS_TRYAGAIN; |
335 | } | |
48b29391 | 336 | else |
a334319f | 337 | { |
48b29391 UD |
338 | __set_errno (olderr); |
339 | return NSS_STATUS_NOTFOUND; | |
a334319f | 340 | } |
a334319f | 341 | } |
48b29391 UD |
342 | |
343 | return NSS_STATUS_SUCCESS; | |
e61abf83 UD |
344 | } |
345 | ||
346 | enum nss_status | |
347 | _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr, | |
d71b808a | 348 | char *buffer, size_t buflen, int *errnop) |
e61abf83 | 349 | { |
68361572 | 350 | if (grp_tablename_val == NULL) |
d71b808a | 351 | { |
68361572 | 352 | enum nss_status status = _nss_grp_create_tablename (errnop); |
48b29391 | 353 | |
d71b808a UD |
354 | if (status != NSS_STATUS_SUCCESS) |
355 | return status; | |
356 | } | |
e61abf83 | 357 | |
48b29391 UD |
358 | int parse_res; |
359 | nis_result *result; | |
68361572 | 360 | char buf[8 + 3 * sizeof (unsigned long int) + grp_tablename_len]; |
48b29391 | 361 | int olderr = errno; |
e61abf83 | 362 | |
48b29391 | 363 | snprintf (buf, sizeof (buf), "[gid=%lu],%s", |
68361572 | 364 | (unsigned long int) gid, grp_tablename_val); |
e61abf83 | 365 | |
48b29391 | 366 | result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL); |
e61abf83 | 367 | |
48b29391 UD |
368 | if (result == NULL) |
369 | { | |
370 | *errnop = ENOMEM; | |
371 | return NSS_STATUS_TRYAGAIN; | |
372 | } | |
2d7da676 | 373 | |
a1ffb40e | 374 | if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS)) |
48b29391 UD |
375 | { |
376 | enum nss_status status = niserr2nss (result->status); | |
0ecb606c | 377 | |
48b29391 UD |
378 | __set_errno (olderr); |
379 | ||
380 | nis_freeresult (result); | |
381 | return status; | |
382 | } | |
383 | ||
a17fa610 | 384 | parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop); |
48b29391 UD |
385 | |
386 | nis_freeresult (result); | |
a1ffb40e | 387 | if (__glibc_unlikely (parse_res < 1)) |
48b29391 UD |
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; | |
e61abf83 | 401 | } |