]>
Commit | Line | Data |
---|---|---|
bfff8b1b | 1 | /* Copyright (c) 1997-2017 Free Software Foundation, Inc. |
e61abf83 | 2 | This file is part of the GNU C Library. |
3d8fa13a | 3 | Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997. |
e61abf83 UD |
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 | |
a17fa610 | 19 | #include <assert.h> |
e61abf83 UD |
20 | #include <string.h> |
21 | #include <rpcsvc/nis.h> | |
9090848d | 22 | #include <libc-diag.h> |
91eee4dd UD |
23 | |
24 | #include "nis_xdr.h" | |
e61abf83 | 25 | #include "nis_intern.h" |
a17fa610 | 26 | #include "libnsl.h" |
e61abf83 | 27 | |
d6db1d53 | 28 | |
a17fa610 UD |
29 | struct ib_request * |
30 | __create_ib_request (const_nis_name name, unsigned int flags) | |
e61abf83 | 31 | { |
3218d55b | 32 | struct ib_request *ibreq = calloc (1, sizeof (struct ib_request)); |
d6db1d53 | 33 | nis_attr *search_val = NULL; |
8112cc70 | 34 | size_t search_len = 0; |
d6db1d53 | 35 | size_t size = 0; |
e61abf83 | 36 | |
3d8fa13a UD |
37 | if (ibreq == NULL) |
38 | return NULL; | |
39 | ||
d6db1d53 | 40 | ibreq->ibr_flags = flags; |
e61abf83 | 41 | |
3218d55b | 42 | char *cptr = strdupa (name); |
e61abf83 UD |
43 | |
44 | /* Not of "[key=value,key=value,...],foo.." format? */ | |
45 | if (cptr[0] != '[') | |
0292b0dd UD |
46 | { |
47 | ibreq->ibr_name = strdup (cptr); | |
48 | if (ibreq->ibr_name == NULL) | |
49 | { | |
50 | free (ibreq); | |
51 | return NULL; | |
52 | } | |
53 | return ibreq; | |
54 | } | |
e61abf83 | 55 | |
d6db1d53 UD |
56 | /* "[key=value,...],foo" format */ |
57 | ibreq->ibr_name = strchr (cptr, ']'); | |
58 | if (ibreq->ibr_name == NULL || ibreq->ibr_name[1] != ',') | |
3d8fa13a | 59 | { |
3218d55b UD |
60 | /* The object has not really been built yet so we use free. */ |
61 | free (ibreq); | |
3d8fa13a UD |
62 | return NULL; |
63 | } | |
e61abf83 | 64 | |
9be31a51 | 65 | /* Check if we have an entry of "[key=value,],bar". If, remove the "," */ |
d6db1d53 UD |
66 | if (ibreq->ibr_name[-1] == ',') |
67 | ibreq->ibr_name[-1] = '\0'; | |
68 | else | |
69 | ibreq->ibr_name[0] = '\0'; | |
70 | ibreq->ibr_name += 2; | |
71 | ibreq->ibr_name = strdup (ibreq->ibr_name); | |
32abdb71 | 72 | if (ibreq->ibr_name == NULL) |
9be31a51 UD |
73 | { |
74 | free_null: | |
75 | while (search_len-- > 0) | |
76 | { | |
77 | free (search_val[search_len].zattr_ndx); | |
78 | free (search_val[search_len].zattr_val.zattr_val_val); | |
79 | } | |
80 | free (search_val); | |
81 | nis_free_request (ibreq); | |
82 | return NULL; | |
83 | } | |
e61abf83 | 84 | |
d6db1d53 | 85 | ++cptr; /* Remove "[" */ |
e61abf83 | 86 | |
d6db1d53 | 87 | while (cptr != NULL && cptr[0] != '\0') |
e61abf83 | 88 | { |
d6db1d53 UD |
89 | char *key = cptr; |
90 | char *val = strchr (cptr, '='); | |
91 | ||
92 | cptr = strchr (key, ','); | |
93 | if (cptr != NULL) | |
94 | *cptr++ = '\0'; | |
e61abf83 | 95 | |
a1ffb40e | 96 | if (__glibc_unlikely (val == NULL)) |
e61abf83 | 97 | { |
d6db1d53 UD |
98 | nis_free_request (ibreq); |
99 | return NULL; | |
e61abf83 | 100 | } |
d6db1d53 | 101 | *val++ = '\0'; |
fff04b32 | 102 | if (search_len + 1 >= size) |
ab26144e UD |
103 | { |
104 | size += 1; | |
458901c6 UD |
105 | nis_attr *newp = realloc (search_val, size * sizeof (nis_attr)); |
106 | if (newp == NULL) | |
9be31a51 | 107 | goto free_null; |
458901c6 | 108 | search_val = newp; |
e61abf83 | 109 | } |
d6db1d53 | 110 | search_val[search_len].zattr_ndx = strdup (key); |
fff04b32 | 111 | if (search_val[search_len].zattr_ndx == NULL) |
9be31a51 | 112 | goto free_null; |
32abdb71 | 113 | |
d6db1d53 UD |
114 | search_val[search_len].zattr_val.zattr_val_len = strlen (val) + 1; |
115 | search_val[search_len].zattr_val.zattr_val_val = strdup (val); | |
116 | if (search_val[search_len].zattr_val.zattr_val_val == NULL) | |
9be31a51 UD |
117 | { |
118 | free (search_val[search_len].zattr_ndx); | |
119 | goto free_null; | |
120 | } | |
32abdb71 | 121 | |
d6db1d53 | 122 | ++search_len; |
e61abf83 | 123 | } |
e61abf83 | 124 | |
d6db1d53 UD |
125 | ibreq->ibr_srch.ibr_srch_val = search_val; |
126 | ibreq->ibr_srch.ibr_srch_len = search_len; | |
e61abf83 UD |
127 | |
128 | return ibreq; | |
129 | } | |
1e4d83f6 | 130 | libnsl_hidden_nolink_def (__create_ib_request, GLIBC_PRIVATE) |
e61abf83 | 131 | |
3218d55b | 132 | static const struct timeval RPCTIMEOUT = {10, 0}; |
e852e889 UD |
133 | |
134 | static char * | |
3e4370cf | 135 | get_tablepath (char *name, dir_binding *bptr) |
e852e889 UD |
136 | { |
137 | enum clnt_stat result; | |
3218d55b | 138 | nis_result res; |
e852e889 UD |
139 | struct ns_request req; |
140 | ||
3218d55b | 141 | memset (&res, '\0', sizeof (res)); |
e852e889 UD |
142 | |
143 | req.ns_name = name; | |
144 | req.ns_object.ns_object_len = 0; | |
145 | req.ns_object.ns_object_val = NULL; | |
146 | ||
147 | result = clnt_call (bptr->clnt, NIS_LOOKUP, (xdrproc_t) _xdr_ns_request, | |
148 | (caddr_t) &req, (xdrproc_t) _xdr_nis_result, | |
3218d55b | 149 | (caddr_t) &res, RPCTIMEOUT); |
e852e889 | 150 | |
d00002ed | 151 | const char *cptr; |
3218d55b UD |
152 | if (result == RPC_SUCCESS && NIS_RES_STATUS (&res) == NIS_SUCCESS |
153 | && __type_of (NIS_RES_OBJECT (&res)) == NIS_TABLE_OBJ) | |
154 | cptr = NIS_RES_OBJECT (&res)->TA_data.ta_path; | |
e852e889 | 155 | else |
3218d55b UD |
156 | cptr = ""; |
157 | ||
3e4370cf UD |
158 | char *str = strdup (cptr); |
159 | ||
160 | if (result == RPC_SUCCESS) | |
161 | xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &res); | |
162 | ||
163 | return str; | |
e852e889 UD |
164 | } |
165 | ||
a17fa610 UD |
166 | |
167 | nis_error | |
168 | __follow_path (char **tablepath, char **tableptr, struct ib_request *ibreq, | |
169 | dir_binding *bptr) | |
170 | { | |
171 | if (*tablepath == NULL) | |
172 | { | |
173 | *tablepath = get_tablepath (ibreq->ibr_name, bptr); | |
174 | if (*tablepath == NULL) | |
175 | return NIS_NOMEMORY; | |
176 | ||
177 | *tableptr = *tablepath; | |
178 | } | |
2a9feb92 CM |
179 | |
180 | /* Since tableptr is only set here, and it's set when tablepath is NULL, | |
181 | which it is initially defined as, we know it will always be set here. */ | |
182 | DIAG_PUSH_NEEDS_COMMENT; | |
183 | DIAG_IGNORE_NEEDS_COMMENT (4.7, "-Wmaybe-uninitialized"); | |
184 | ||
a17fa610 UD |
185 | if (*tableptr == NULL) |
186 | return NIS_NOTFOUND; | |
187 | ||
188 | char *newname = strsep (tableptr, ":"); | |
189 | if (newname[0] == '\0') | |
190 | return NIS_NOTFOUND; | |
191 | ||
2a9feb92 CM |
192 | DIAG_POP_NEEDS_COMMENT; |
193 | ||
a17fa610 UD |
194 | newname = strdup (newname); |
195 | if (newname == NULL) | |
196 | return NIS_NOMEMORY; | |
197 | ||
198 | free (ibreq->ibr_name); | |
199 | ibreq->ibr_name = newname; | |
200 | ||
201 | return NIS_SUCCESS; | |
202 | } | |
1e4d83f6 | 203 | libnsl_hidden_nolink_def (__follow_path, GLIBC_PRIVATE) |
a17fa610 UD |
204 | |
205 | ||
e61abf83 | 206 | nis_result * |
a1129917 | 207 | nis_list (const_nis_name name, unsigned int flags, |
c131718c | 208 | int (*callback) (const_nis_name name, |
e61abf83 UD |
209 | const nis_object *object, |
210 | const void *userdata), | |
211 | const void *userdata) | |
212 | { | |
f2d5cf50 | 213 | nis_result *res = malloc (sizeof (nis_result)); |
d6db1d53 | 214 | ib_request *ibreq; |
2d7da676 | 215 | int status; |
e852e889 | 216 | enum clnt_stat clnt_status; |
650425ce | 217 | int count_links = 0; /* We will only follow NIS_MAXLINKS links! */ |
2d7da676 UD |
218 | int done = 0; |
219 | nis_name *names; | |
220 | nis_name namebuf[2] = {NULL, NULL}; | |
221 | int name_nr = 0; | |
650425ce | 222 | nis_cb *cb = NULL; |
07683f84 UD |
223 | char *tableptr; |
224 | char *tablepath = NULL; | |
e852e889 | 225 | int first_try = 0; /* Do we try the old binding at first ? */ |
68361572 | 226 | nis_result *allres = NULL; |
e61abf83 | 227 | |
91eee4dd UD |
228 | if (res == NULL) |
229 | return NULL; | |
e61abf83 | 230 | |
d6db1d53 UD |
231 | if (name == NULL) |
232 | { | |
fff04b32 | 233 | status = NIS_BADNAME; |
f2d5cf50 | 234 | err_out: |
68361572 | 235 | nis_freeresult (allres); |
f2d5cf50 | 236 | memset (res, '\0', sizeof (nis_result)); |
fff04b32 | 237 | NIS_RES_STATUS (res) = status; |
d6db1d53 UD |
238 | return res; |
239 | } | |
240 | ||
a17fa610 | 241 | ibreq = __create_ib_request (name, flags); |
fff04b32 | 242 | if (ibreq == NULL) |
e61abf83 | 243 | { |
fff04b32 | 244 | status = NIS_BADNAME; |
f2d5cf50 | 245 | goto err_out; |
e61abf83 UD |
246 | } |
247 | ||
901956a5 UD |
248 | if ((flags & EXPAND_NAME) |
249 | && ibreq->ibr_name[strlen (ibreq->ibr_name) - 1] != '.') | |
e61abf83 | 250 | { |
dfd2257a UD |
251 | names = nis_getnames (ibreq->ibr_name); |
252 | free (ibreq->ibr_name); | |
253 | ibreq->ibr_name = NULL; | |
2d7da676 | 254 | if (names == NULL) |
e61abf83 | 255 | { |
3d8fa13a | 256 | nis_free_request (ibreq); |
fff04b32 | 257 | status = NIS_BADNAME; |
f2d5cf50 | 258 | goto err_out; |
e61abf83 | 259 | } |
dfd2257a | 260 | ibreq->ibr_name = strdup (names[name_nr]); |
901956a5 UD |
261 | if (ibreq->ibr_name == NULL) |
262 | { | |
07683f84 | 263 | nis_freenames (names); |
901956a5 | 264 | nis_free_request (ibreq); |
fff04b32 | 265 | status = NIS_NOMEMORY; |
f2d5cf50 | 266 | goto err_out; |
901956a5 | 267 | } |
2d7da676 UD |
268 | } |
269 | else | |
650425ce UD |
270 | { |
271 | names = namebuf; | |
dfd2257a | 272 | names[name_nr] = ibreq->ibr_name; |
650425ce | 273 | } |
e61abf83 | 274 | |
650425ce UD |
275 | cb = NULL; |
276 | ||
e852e889 | 277 | while (!done) |
2d7da676 | 278 | { |
e852e889 UD |
279 | dir_binding bptr; |
280 | directory_obj *dir = NULL; | |
650425ce | 281 | |
e852e889 | 282 | memset (res, '\0', sizeof (nis_result)); |
650425ce | 283 | |
bd1ebae0 | 284 | status = __nisfind_server (ibreq->ibr_name, |
697d37b1 JJ |
285 | ibreq->ibr_srch.ibr_srch_val != NULL, |
286 | &dir, &bptr, flags & ~MASTER_ONLY); | |
e852e889 | 287 | if (status != NIS_SUCCESS) |
07683f84 | 288 | { |
ab26144e UD |
289 | NIS_RES_STATUS (res) = status; |
290 | goto fail3; | |
07683f84 | 291 | } |
a334319f | 292 | |
e852e889 | 293 | while (__nisbind_connect (&bptr) != NIS_SUCCESS) |
a1ffb40e | 294 | if (__glibc_unlikely (__nisbind_next (&bptr) != NIS_SUCCESS)) |
e852e889 | 295 | { |
e852e889 | 296 | NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE; |
07683f84 | 297 | goto fail; |
e852e889 | 298 | } |
6f0ee462 | 299 | |
650425ce UD |
300 | if (callback != NULL) |
301 | { | |
a17fa610 | 302 | assert (cb == NULL); |
650425ce | 303 | cb = __nis_create_callback (callback, userdata, flags); |
dfd2257a UD |
304 | ibreq->ibr_cbhost.ibr_cbhost_len = 1; |
305 | ibreq->ibr_cbhost.ibr_cbhost_val = cb->serv; | |
e852e889 | 306 | } |
650425ce | 307 | |
e852e889 UD |
308 | again: |
309 | clnt_status = clnt_call (bptr.clnt, NIS_IBLIST, | |
310 | (xdrproc_t) _xdr_ib_request, (caddr_t) ibreq, | |
311 | (xdrproc_t) _xdr_nis_result, | |
312 | (caddr_t) res, RPCTIMEOUT); | |
313 | ||
a1ffb40e | 314 | if (__glibc_unlikely (clnt_status != RPC_SUCCESS)) |
e852e889 UD |
315 | NIS_RES_STATUS (res) = NIS_RPCERROR; |
316 | else | |
317 | switch (NIS_RES_STATUS (res)) | |
318 | { /* start switch */ | |
319 | case NIS_PARTIAL: | |
320 | case NIS_SUCCESS: | |
321 | case NIS_S_SUCCESS: | |
707c7499 UD |
322 | if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ |
323 | && (flags & FOLLOW_LINKS)) /* We are following links. */ | |
e852e889 UD |
324 | { |
325 | free (ibreq->ibr_name); | |
3d8fa13a | 326 | ibreq->ibr_name = NULL; |
e852e889 | 327 | /* If we hit the link limit, bail. */ |
a1ffb40e | 328 | if (__glibc_unlikely (count_links > NIS_MAXLINKS)) |
e852e889 UD |
329 | { |
330 | NIS_RES_STATUS (res) = NIS_LINKNAMEERROR; | |
331 | ++done; | |
332 | break; | |
333 | } | |
334 | ++count_links; | |
335 | ibreq->ibr_name = | |
336 | strdup (NIS_RES_OBJECT (res)->LI_data.li_name); | |
901956a5 UD |
337 | if (ibreq->ibr_name == NULL) |
338 | { | |
a334319f | 339 | NIS_RES_STATUS (res) = NIS_NOMEMORY; |
07683f84 UD |
340 | fail: |
341 | __nisbind_destroy (&bptr); | |
07683f84 UD |
342 | nis_free_directory (dir); |
343 | fail3: | |
344 | free (tablepath); | |
345 | if (cb) | |
346 | { | |
347 | __nis_destroy_callback (cb); | |
348 | ibreq->ibr_cbhost.ibr_cbhost_len = 0; | |
349 | ibreq->ibr_cbhost.ibr_cbhost_val = NULL; | |
350 | } | |
351 | if (names != namebuf) | |
352 | nis_freenames (names); | |
353 | nis_free_request (ibreq); | |
68361572 | 354 | nis_freeresult (allres); |
901956a5 UD |
355 | return res; |
356 | } | |
e852e889 UD |
357 | if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len) |
358 | if (ibreq->ibr_srch.ibr_srch_len == 0) | |
650425ce | 359 | { |
e852e889 UD |
360 | ibreq->ibr_srch.ibr_srch_len = |
361 | NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len; | |
362 | ibreq->ibr_srch.ibr_srch_val = | |
363 | NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_val; | |
650425ce | 364 | } |
56a5719e UD |
365 | /* The following is a non-obvious optimization. A |
366 | nis_freeresult call would call xdr_free as the | |
367 | following code. But it also would unnecessarily | |
368 | free the result structure. We avoid this here | |
369 | along with the necessary tests. */ | |
56a5719e UD |
370 | xdr_free ((xdrproc_t) _xdr_nis_result, (char *)res); |
371 | memset (res, '\0', sizeof (*res)); | |
e852e889 UD |
372 | first_try = 1; /* Try at first the old binding */ |
373 | goto again; | |
374 | } | |
707c7499 UD |
375 | else if ((flags & FOLLOW_PATH) |
376 | && NIS_RES_STATUS (res) == NIS_PARTIAL) | |
e852e889 | 377 | { |
ab26144e UD |
378 | enum nis_error err = __follow_path (&tablepath, &tableptr, |
379 | ibreq, &bptr); | |
380 | if (err != NIS_SUCCESS) | |
e852e889 | 381 | { |
ab26144e UD |
382 | if (err == NIS_NOMEMORY) |
383 | NIS_RES_STATUS (res) = err; | |
a334319f | 384 | ++done; |
e852e889 UD |
385 | } |
386 | else | |
387 | { | |
707c7499 UD |
388 | /* The following is a non-obvious optimization. A |
389 | nis_freeresult call would call xdr_free as the | |
390 | following code. But it also would unnecessarily | |
391 | free the result structure. We avoid this here | |
392 | along with the necessary tests. */ | |
a17fa610 | 393 | xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res); |
707c7499 | 394 | memset (res, '\0', sizeof (*res)); |
a334319f UD |
395 | first_try = 1; |
396 | goto again; | |
e852e889 UD |
397 | } |
398 | } | |
68361572 UD |
399 | else if ((flags & (FOLLOW_PATH | ALL_RESULTS)) |
400 | == (FOLLOW_PATH | ALL_RESULTS)) | |
401 | { | |
402 | if (allres == NULL) | |
403 | { | |
404 | allres = res; | |
405 | res = malloc (sizeof (nis_result)); | |
406 | if (res == NULL) | |
407 | { | |
408 | res = allres; | |
409 | allres = NULL; | |
410 | NIS_RES_STATUS (res) = NIS_NOMEMORY; | |
411 | goto fail; | |
412 | } | |
413 | NIS_RES_STATUS (res) = NIS_RES_STATUS (allres); | |
414 | } | |
415 | else | |
416 | { | |
417 | nis_object *objects_val | |
418 | = realloc (NIS_RES_OBJECT (allres), | |
419 | (NIS_RES_NUMOBJ (allres) | |
420 | + NIS_RES_NUMOBJ (res)) | |
421 | * sizeof (nis_object)); | |
422 | if (objects_val == NULL) | |
423 | { | |
424 | NIS_RES_STATUS (res) = NIS_NOMEMORY; | |
425 | goto fail; | |
426 | } | |
427 | NIS_RES_OBJECT (allres) = objects_val; | |
428 | memcpy (NIS_RES_OBJECT (allres) + NIS_RES_NUMOBJ (allres), | |
429 | NIS_RES_OBJECT (res), | |
430 | NIS_RES_NUMOBJ (res) * sizeof (nis_object)); | |
431 | NIS_RES_NUMOBJ (allres) += NIS_RES_NUMOBJ (res); | |
432 | NIS_RES_NUMOBJ (res) = 0; | |
433 | free (NIS_RES_OBJECT (res)); | |
434 | NIS_RES_OBJECT (res) = NULL; | |
435 | NIS_RES_STATUS (allres) = NIS_RES_STATUS (res); | |
436 | xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res); | |
437 | } | |
ab26144e UD |
438 | enum nis_error err = __follow_path (&tablepath, &tableptr, |
439 | ibreq, &bptr); | |
440 | if (err != NIS_SUCCESS) | |
68361572 | 441 | { |
4df92d57 UD |
442 | /* Prepare for the nis_freeresult call. */ |
443 | memset (res, '\0', sizeof (*res)); | |
444 | ||
ab26144e UD |
445 | if (err == NIS_NOMEMORY) |
446 | NIS_RES_STATUS (allres) = err; | |
68361572 UD |
447 | ++done; |
448 | } | |
449 | } | |
e852e889 | 450 | else |
2d7da676 | 451 | ++done; |
e852e889 UD |
452 | break; |
453 | case NIS_CBRESULTS: | |
454 | if (cb != NULL) | |
455 | { | |
456 | __nis_do_callback (&bptr, &res->cookie, cb); | |
457 | NIS_RES_STATUS (res) = cb->result; | |
458 | ||
459 | if (!(flags & ALL_RESULTS)) | |
650425ce | 460 | ++done; |
e852e889 UD |
461 | else |
462 | { | |
ab26144e | 463 | enum nis_error err |
a17fa610 | 464 | = __follow_path (&tablepath, &tableptr, ibreq, &bptr); |
ab26144e | 465 | if (err != NIS_SUCCESS) |
0008163a | 466 | { |
ab26144e UD |
467 | if (err == NIS_NOMEMORY) |
468 | NIS_RES_STATUS (res) = err; | |
0008163a UD |
469 | ++done; |
470 | } | |
e852e889 UD |
471 | } |
472 | } | |
473 | break; | |
474 | case NIS_SYSTEMERROR: | |
475 | case NIS_NOSUCHNAME: | |
476 | case NIS_NOT_ME: | |
477 | /* If we had first tried the old binding, do nothing, but | |
478 | get a new binding */ | |
479 | if (!first_try) | |
480 | { | |
481 | if (__nisbind_next (&bptr) != NIS_SUCCESS) | |
482 | { | |
483 | ++done; | |
484 | break; /* No more servers to search */ | |
485 | } | |
486 | while (__nisbind_connect (&bptr) != NIS_SUCCESS) | |
487 | { | |
488 | if (__nisbind_next (&bptr) != NIS_SUCCESS) | |
489 | { | |
490 | ++done; | |
491 | break; /* No more servers to search */ | |
492 | } | |
493 | } | |
494 | goto again; | |
495 | } | |
496 | break; | |
497 | default: | |
498 | if (!first_try) | |
499 | { | |
500 | /* Try the next domainname if we don't follow a link. */ | |
901956a5 UD |
501 | free (ibreq->ibr_name); |
502 | ibreq->ibr_name = NULL; | |
a1ffb40e | 503 | if (__glibc_unlikely (count_links)) |
e852e889 | 504 | { |
e852e889 UD |
505 | NIS_RES_STATUS (res) = NIS_LINKNAMEERROR; |
506 | ++done; | |
507 | break; | |
508 | } | |
509 | ++name_nr; | |
510 | if (names[name_nr] == NULL) | |
511 | { | |
512 | ++done; | |
513 | break; | |
514 | } | |
901956a5 UD |
515 | ibreq->ibr_name = strdup (names[name_nr]); |
516 | if (ibreq->ibr_name == NULL) | |
517 | { | |
901956a5 | 518 | NIS_RES_STATUS (res) = NIS_NOMEMORY; |
07683f84 | 519 | goto fail; |
901956a5 | 520 | } |
e852e889 UD |
521 | first_try = 1; /* Try old binding at first */ |
522 | goto again; | |
523 | } | |
524 | break; | |
525 | } | |
526 | first_try = 0; | |
527 | ||
528 | if (cb) | |
529 | { | |
530 | __nis_destroy_callback (cb); | |
531 | ibreq->ibr_cbhost.ibr_cbhost_len = 0; | |
532 | ibreq->ibr_cbhost.ibr_cbhost_val = NULL; | |
07683f84 | 533 | cb = NULL; |
e61abf83 | 534 | } |
e852e889 UD |
535 | |
536 | __nisbind_destroy (&bptr); | |
537 | nis_free_directory (dir); | |
538 | } | |
e61abf83 | 539 | |
07683f84 UD |
540 | free (tablepath); |
541 | ||
2d7da676 UD |
542 | if (names != namebuf) |
543 | nis_freenames (names); | |
544 | ||
dfd2257a | 545 | nis_free_request (ibreq); |
e61abf83 | 546 | |
68361572 UD |
547 | if (allres) |
548 | { | |
549 | nis_freeresult (res); | |
550 | return allres; | |
551 | } | |
552 | ||
e61abf83 UD |
553 | return res; |
554 | } | |
1e4d83f6 | 555 | libnsl_hidden_nolink_def (nis_list, GLIBC_2_1) |
e61abf83 UD |
556 | |
557 | nis_result * | |
a1129917 | 558 | nis_add_entry (const_nis_name name, const nis_object *obj2, unsigned int flags) |
e61abf83 | 559 | { |
0292b0dd | 560 | nis_result *res = calloc (1, sizeof (nis_result)); |
91eee4dd UD |
561 | if (res == NULL) |
562 | return NULL; | |
563 | ||
d6db1d53 | 564 | if (name == NULL) |
91eee4dd | 565 | { |
d6db1d53 | 566 | NIS_RES_STATUS (res) = NIS_BADNAME; |
91eee4dd UD |
567 | return res; |
568 | } | |
e61abf83 | 569 | |
a17fa610 | 570 | ib_request *ibreq = __create_ib_request (name, flags); |
0292b0dd | 571 | if (ibreq == NULL) |
e61abf83 | 572 | { |
91eee4dd | 573 | NIS_RES_STATUS (res) = NIS_BADNAME; |
e61abf83 UD |
574 | return res; |
575 | } | |
576 | ||
0292b0dd | 577 | nis_object obj; |
91eee4dd | 578 | memcpy (&obj, obj2, sizeof (nis_object)); |
e61abf83 | 579 | |
fff04b32 UD |
580 | size_t namelen = strlen (name); |
581 | char buf1[namelen + 20]; | |
582 | char buf4[namelen + 20]; | |
583 | ||
91eee4dd UD |
584 | if (obj.zo_name == NULL || strlen (obj.zo_name) == 0) |
585 | obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1)); | |
ce37fa88 | 586 | |
91eee4dd UD |
587 | if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0) |
588 | obj.zo_owner = nis_local_principal (); | |
ce37fa88 | 589 | |
91eee4dd UD |
590 | if (obj.zo_group == NULL || strlen (obj.zo_group) == 0) |
591 | obj.zo_group = nis_local_group (); | |
ce37fa88 | 592 | |
91eee4dd UD |
593 | obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4)); |
594 | ||
595 | ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL); | |
596 | if (ibreq->ibr_obj.ibr_obj_val == NULL) | |
597 | { | |
3d8fa13a | 598 | nis_free_request (ibreq); |
91eee4dd UD |
599 | NIS_RES_STATUS (res) = NIS_NOMEMORY; |
600 | return res; | |
601 | } | |
602 | ibreq->ibr_obj.ibr_obj_len = 1; | |
ce37fa88 | 603 | |
0292b0dd UD |
604 | nis_error status = __do_niscall (ibreq->ibr_name, NIS_IBADD, |
605 | (xdrproc_t) _xdr_ib_request, | |
606 | (caddr_t) ibreq, | |
607 | (xdrproc_t) _xdr_nis_result, | |
608 | (caddr_t) res, 0, NULL); | |
a1ffb40e | 609 | if (__glibc_unlikely (status != NIS_SUCCESS)) |
91eee4dd | 610 | NIS_RES_STATUS (res) = status; |
ce37fa88 | 611 | |
dfd2257a | 612 | nis_free_request (ibreq); |
e61abf83 UD |
613 | |
614 | return res; | |
615 | } | |
1e4d83f6 | 616 | libnsl_hidden_nolink_def (nis_add_entry, GLIBC_2_1) |
e61abf83 UD |
617 | |
618 | nis_result * | |
a1129917 UD |
619 | nis_modify_entry (const_nis_name name, const nis_object *obj2, |
620 | unsigned int flags) | |
e61abf83 | 621 | { |
91eee4dd | 622 | nis_object obj; |
e61abf83 | 623 | nis_result *res; |
e61abf83 | 624 | nis_error status; |
d6db1d53 UD |
625 | ib_request *ibreq; |
626 | size_t namelen = strlen (name); | |
627 | char buf1[namelen + 20]; | |
628 | char buf4[namelen + 20]; | |
e61abf83 UD |
629 | |
630 | res = calloc (1, sizeof (nis_result)); | |
d6db1d53 UD |
631 | if (res == NULL) |
632 | return NULL; | |
e61abf83 | 633 | |
a17fa610 | 634 | ibreq = __create_ib_request (name, flags); |
fff04b32 | 635 | if (ibreq == NULL) |
e61abf83 | 636 | { |
91eee4dd | 637 | NIS_RES_STATUS (res) = NIS_BADNAME; |
e61abf83 UD |
638 | return res; |
639 | } | |
640 | ||
91eee4dd | 641 | memcpy (&obj, obj2, sizeof (nis_object)); |
e61abf83 | 642 | |
91eee4dd UD |
643 | if (obj.zo_name == NULL || strlen (obj.zo_name) == 0) |
644 | obj.zo_name = nis_leaf_of_r (name, buf1, sizeof (buf1)); | |
ce37fa88 | 645 | |
91eee4dd UD |
646 | if (obj.zo_owner == NULL || strlen (obj.zo_owner) == 0) |
647 | obj.zo_owner = nis_local_principal (); | |
ce37fa88 | 648 | |
91eee4dd UD |
649 | if (obj.zo_group == NULL || strlen (obj.zo_group) == 0) |
650 | obj.zo_group = nis_local_group (); | |
ce37fa88 | 651 | |
91eee4dd UD |
652 | obj.zo_domain = nis_domain_of_r (name, buf4, sizeof (buf4)); |
653 | ||
654 | ibreq->ibr_obj.ibr_obj_val = nis_clone_object (&obj, NULL); | |
655 | if (ibreq->ibr_obj.ibr_obj_val == NULL) | |
656 | { | |
3d8fa13a | 657 | nis_free_request (ibreq); |
91eee4dd UD |
658 | NIS_RES_STATUS (res) = NIS_NOMEMORY; |
659 | return res; | |
660 | } | |
661 | ibreq->ibr_obj.ibr_obj_len = 1; | |
ce37fa88 | 662 | |
fff04b32 UD |
663 | status = __do_niscall (ibreq->ibr_name, NIS_IBMODIFY, |
664 | (xdrproc_t) _xdr_ib_request, | |
665 | (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result, | |
666 | (caddr_t) res, 0, NULL); | |
a1ffb40e | 667 | if (__glibc_unlikely (status != NIS_SUCCESS)) |
91eee4dd | 668 | NIS_RES_STATUS (res) = status; |
ce37fa88 | 669 | |
dfd2257a | 670 | nis_free_request (ibreq); |
e61abf83 UD |
671 | |
672 | return res; | |
673 | } | |
1e4d83f6 | 674 | libnsl_hidden_nolink_def (nis_modify_entry, GLIBC_2_1) |
e61abf83 UD |
675 | |
676 | nis_result * | |
c131718c | 677 | nis_remove_entry (const_nis_name name, const nis_object *obj, |
a1129917 | 678 | unsigned int flags) |
e61abf83 UD |
679 | { |
680 | nis_result *res; | |
d6db1d53 | 681 | ib_request *ibreq; |
e61abf83 UD |
682 | nis_error status; |
683 | ||
684 | res = calloc (1, sizeof (nis_result)); | |
91eee4dd UD |
685 | if (res == NULL) |
686 | return NULL; | |
687 | ||
d6db1d53 | 688 | if (name == NULL) |
91eee4dd | 689 | { |
d6db1d53 | 690 | NIS_RES_STATUS (res) = NIS_BADNAME; |
91eee4dd UD |
691 | return res; |
692 | } | |
e61abf83 | 693 | |
a17fa610 | 694 | ibreq = __create_ib_request (name, flags); |
fff04b32 | 695 | if (ibreq == NULL) |
e61abf83 | 696 | { |
91eee4dd | 697 | NIS_RES_STATUS (res) = NIS_BADNAME; |
e61abf83 UD |
698 | return res; |
699 | } | |
700 | ||
e61abf83 UD |
701 | if (obj != NULL) |
702 | { | |
dfd2257a | 703 | ibreq->ibr_obj.ibr_obj_val = nis_clone_object (obj, NULL); |
91eee4dd UD |
704 | if (ibreq->ibr_obj.ibr_obj_val == NULL) |
705 | { | |
3d8fa13a | 706 | nis_free_request (ibreq); |
91eee4dd UD |
707 | NIS_RES_STATUS (res) = NIS_NOMEMORY; |
708 | return res; | |
709 | } | |
dfd2257a | 710 | ibreq->ibr_obj.ibr_obj_len = 1; |
e61abf83 UD |
711 | } |
712 | ||
dfd2257a | 713 | if ((status = __do_niscall (ibreq->ibr_name, NIS_IBREMOVE, |
91eee4dd UD |
714 | (xdrproc_t) _xdr_ib_request, |
715 | (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result, | |
650425ce | 716 | (caddr_t) res, 0, NULL)) != NIS_SUCCESS) |
91eee4dd | 717 | NIS_RES_STATUS (res) = status; |
e61abf83 | 718 | |
dfd2257a | 719 | nis_free_request (ibreq); |
e61abf83 UD |
720 | |
721 | return res; | |
722 | } | |
1e4d83f6 | 723 | libnsl_hidden_nolink_def (nis_remove_entry, GLIBC_2_1) |
e61abf83 UD |
724 | |
725 | nis_result * | |
c131718c | 726 | nis_first_entry (const_nis_name name) |
e61abf83 UD |
727 | { |
728 | nis_result *res; | |
d6db1d53 | 729 | ib_request *ibreq; |
e61abf83 UD |
730 | nis_error status; |
731 | ||
732 | res = calloc (1, sizeof (nis_result)); | |
91eee4dd UD |
733 | if (res == NULL) |
734 | return NULL; | |
735 | ||
d6db1d53 | 736 | if (name == NULL) |
91eee4dd | 737 | { |
d6db1d53 | 738 | NIS_RES_STATUS (res) = NIS_BADNAME; |
91eee4dd UD |
739 | return res; |
740 | } | |
e61abf83 | 741 | |
a17fa610 | 742 | ibreq = __create_ib_request (name, 0); |
32abdb71 | 743 | if (ibreq == NULL) |
e61abf83 | 744 | { |
91eee4dd | 745 | NIS_RES_STATUS (res) = NIS_BADNAME; |
e61abf83 UD |
746 | return res; |
747 | } | |
748 | ||
32abdb71 UD |
749 | status = __do_niscall (ibreq->ibr_name, NIS_IBFIRST, |
750 | (xdrproc_t) _xdr_ib_request, | |
751 | (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result, | |
752 | (caddr_t) res, 0, NULL); | |
753 | ||
a1ffb40e | 754 | if (__glibc_unlikely (status != NIS_SUCCESS)) |
91eee4dd | 755 | NIS_RES_STATUS (res) = status; |
e61abf83 | 756 | |
dfd2257a | 757 | nis_free_request (ibreq); |
e61abf83 UD |
758 | |
759 | return res; | |
760 | } | |
1e4d83f6 | 761 | libnsl_hidden_nolink_def (nis_first_entry, GLIBC_2_1) |
e61abf83 UD |
762 | |
763 | nis_result * | |
c131718c | 764 | nis_next_entry (const_nis_name name, const netobj *cookie) |
e61abf83 UD |
765 | { |
766 | nis_result *res; | |
d6db1d53 | 767 | ib_request *ibreq; |
e61abf83 UD |
768 | nis_error status; |
769 | ||
770 | res = calloc (1, sizeof (nis_result)); | |
91eee4dd UD |
771 | if (res == NULL) |
772 | return NULL; | |
773 | ||
d6db1d53 | 774 | if (name == NULL) |
91eee4dd | 775 | { |
d6db1d53 | 776 | NIS_RES_STATUS (res) = NIS_BADNAME; |
91eee4dd UD |
777 | return res; |
778 | } | |
e61abf83 | 779 | |
a17fa610 | 780 | ibreq = __create_ib_request (name, 0); |
32abdb71 | 781 | if (ibreq == NULL) |
e61abf83 | 782 | { |
91eee4dd | 783 | NIS_RES_STATUS (res) = NIS_BADNAME; |
e61abf83 UD |
784 | return res; |
785 | } | |
786 | ||
787 | if (cookie != NULL) | |
788 | { | |
d6db1d53 | 789 | ibreq->ibr_cookie.n_bytes = cookie->n_bytes; |
dfd2257a | 790 | ibreq->ibr_cookie.n_len = cookie->n_len; |
e61abf83 UD |
791 | } |
792 | ||
32abdb71 UD |
793 | status = __do_niscall (ibreq->ibr_name, NIS_IBNEXT, |
794 | (xdrproc_t) _xdr_ib_request, | |
795 | (caddr_t) ibreq, (xdrproc_t) _xdr_nis_result, | |
796 | (caddr_t) res, 0, NULL); | |
797 | ||
a1ffb40e | 798 | if (__glibc_unlikely (status != NIS_SUCCESS)) |
91eee4dd | 799 | NIS_RES_STATUS (res) = status; |
e61abf83 | 800 | |
d6db1d53 UD |
801 | if (cookie != NULL) |
802 | { | |
803 | /* Don't give cookie free, it is not from us */ | |
804 | ibreq->ibr_cookie.n_bytes = NULL; | |
805 | ibreq->ibr_cookie.n_len = 0; | |
806 | } | |
807 | ||
dfd2257a | 808 | nis_free_request (ibreq); |
e61abf83 UD |
809 | |
810 | return res; | |
811 | } | |
1e4d83f6 | 812 | libnsl_hidden_nolink_def (nis_next_entry, GLIBC_2_1) |