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