]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nis_table.c
1 /* Copyright (c) 1997, 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 #include <rpcsvc/nis.h>
24 #include "nis_intern.h"
27 splitname (const_nis_name name
, nis_name
*ibr_name
, int *srch_len
,
30 char *cptr
, *key
, *val
, *next
;
43 /* Not of "[key=value,key=value,...],foo.." format? */
50 *ibr_name
= strchr (cptr
, ']');
51 if (*ibr_name
== NULL
|| (*ibr_name
)[1] != ',')
60 *ibr_name
= strdup (*ibr_name
);
62 if (srch_len
== NULL
|| srch_val
== NULL
)
71 next
= strchr (key
, ',');
78 val
= strchr (key
, '=');
82 *srch_val
= malloc (sizeof (nis_attr
));
83 if (*srch_val
== NULL
)
90 (*srch_val
)[0].zattr_val
.zattr_val_len
= 0;
91 (*srch_val
)[0].zattr_val
.zattr_val_val
= NULL
;
98 if ((*srch_len
) + 1 >= size
)
102 *srch_val
= malloc (size
* sizeof (char *));
104 *srch_val
= realloc (val
, size
* sizeof (char *));
105 if (*srch_val
== NULL
)
114 (*srch_val
)[*srch_len
].zattr_ndx
= strdup (key
);
115 if (((*srch_val
)[*srch_len
].zattr_ndx
) == NULL
)
122 (*srch_val
)[*srch_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
123 (*srch_val
)[*srch_len
].zattr_val
.zattr_val_val
= strdup (val
);
124 if ((*srch_val
)[*srch_len
].zattr_val
.zattr_val_val
== NULL
)
141 static struct ib_request
*
142 __create_ib_request (const_nis_name name
, struct ib_request
*ibreq
,
145 splitname (name
, &ibreq
->ibr_name
, &ibreq
->ibr_srch
.ibr_srch_len
,
146 &ibreq
->ibr_srch
.ibr_srch_val
);
147 if (ibreq
->ibr_name
== NULL
)
150 ibreq
->ibr_flags
= flags
;
151 ibreq
->ibr_obj
.ibr_obj_len
= 0;
152 ibreq
->ibr_obj
.ibr_obj_val
= NULL
;
153 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
154 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
155 ibreq
->ibr_bufsize
= 0;
156 ibreq
->ibr_cookie
.n_len
= 0;
157 ibreq
->ibr_cookie
.n_bytes
= NULL
;
163 nis_list (const_nis_name name
, u_long flags
,
164 int (*callback
) (const_nis_name name
,
165 const nis_object
*object
,
166 const void *userdata
),
167 const void *userdata
)
169 nis_result
*res
= NULL
;
170 ib_request
*ibreq
= calloc (1, sizeof (ib_request
));
172 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
175 nis_name namebuf
[2] = {NULL
, NULL
};
179 res
= calloc (1, sizeof (nis_result
));
183 if (__create_ib_request (name
, ibreq
, flags
) == NULL
)
185 NIS_RES_STATUS (res
) = NIS_BADNAME
;
189 if (flags
& EXPAND_NAME
)
191 names
= nis_getnames (ibreq
->ibr_name
);
192 free (ibreq
->ibr_name
);
193 ibreq
->ibr_name
= NULL
;
196 NIS_RES_STATUS (res
) = NIS_BADNAME
;
199 ibreq
->ibr_name
= strdup (names
[name_nr
]);
204 names
[name_nr
] = ibreq
->ibr_name
;
209 if (flags
& FOLLOW_PATH
|| flags
& ALL_RESULTS
)
212 u_long newflags
= flags
& ~FOLLOW_PATH
& ~ALL_RESULTS
;
213 char table_path
[NIS_MAXPATH
+ 1];
215 u_long done
= 0, failures
= 0;
217 while (names
[name_nr
] != NULL
&& !done
)
219 lres
= nis_lookup (names
[name_nr
], newflags
);
220 if (lres
== NULL
|| NIS_RES_STATUS (lres
) != NIS_SUCCESS
)
222 NIS_RES_STATUS (res
) = NIS_RES_STATUS (lres
);
223 nis_freeresult (lres
);
228 /* nis_lookup handles FOLLOW_LINKS,
229 so we must have a table object. */
230 if (__type_of (NIS_RES_OBJECT (lres
)) != NIS_TABLE_OBJ
)
232 nis_freeresult (lres
);
233 NIS_RES_STATUS (res
) = NIS_INVALIDOBJ
;
237 /* Save the path, discard everything else. */
238 snprintf (table_path
, NIS_MAXPATH
, "%s:%s", names
[name_nr
],
239 NIS_RES_OBJECT (lres
)->TA_data
.ta_path
);
240 nis_freeresult (lres
);
246 while (((ntable
= strsep (&p
, ":")) != NULL
) && !done
)
251 nis_freeresult (res
);
253 /* Do the job recursive here! */
254 if ((c
= strchr(name
, ']')) != NULL
)
256 /* Have indexed name ! */
257 int index_len
= c
- name
+ 2;
258 char buf
[index_len
+ strlen (ntable
) + 1];
260 c
= __stpncpy (buf
, name
, index_len
);
262 res
= nis_list (buf
, newflags
, callback
,userdata
);
265 res
= nis_list (ntable
, newflags
, callback
, userdata
);
268 switch (NIS_RES_STATUS (res
))
272 if (!(flags
& ALL_RESULTS
))
275 case NIS_PARTIAL
: /* The table is correct, we doesn't found
279 if (flags
& ALL_RESULTS
)
286 if (NIS_RES_STATUS (res
) == NIS_SUCCESS
&& failures
)
287 NIS_RES_STATUS (res
) = NIS_S_SUCCESS
;
288 if (NIS_RES_STATUS (res
) == NIS_NOTFOUND
&& failures
)
289 NIS_RES_STATUS (res
) = NIS_S_NOTFOUND
;
295 if (callback
!= NULL
)
297 cb
= __nis_create_callback (callback
, userdata
, flags
);
298 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
299 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
304 memset (res
, '\0', sizeof (nis_result
));
306 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBLIST
,
307 (xdrproc_t
) _xdr_ib_request
,
308 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
309 (caddr_t
) res
, flags
, cb
);
310 if (status
!= NIS_SUCCESS
)
311 NIS_RES_STATUS (res
) = status
;
313 switch (NIS_RES_STATUS (res
))
318 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
&&
319 flags
& FOLLOW_LINKS
) /* We are following links. */
321 /* If we hit the link limit, bail. */
322 if (count_links
> NIS_MAXLINKS
)
324 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
329 free (ibreq
->ibr_name
);
331 free (ibreq
->ibr_name
);
333 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
334 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
335 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
337 ibreq
->ibr_srch
.ibr_srch_len
=
338 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
339 ibreq
->ibr_srch
.ibr_srch_val
=
340 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
342 nis_freeresult (res
);
343 res
= calloc (1, sizeof (nis_result
));
349 /* Calback is handled in nis_call.c (__do_niscall2),
350 but we have to change the error code */
351 NIS_RES_STATUS (res
) = cb
->result
;
355 /* NIS+ is not installed, or all servers are down. */
359 /* Try the next domainname if we don't follow a link. */
362 free (ibreq
->ibr_name
);
363 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
368 if (names
[name_nr
] == NULL
)
373 ibreq
->ibr_name
= names
[name_nr
];
377 } /* End of not FOLLOW_PATH. */
379 if (names
!= namebuf
)
380 nis_freenames (names
);
384 __nis_destroy_callback (cb
);
385 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
386 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
389 nis_free_request (ibreq
);
395 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, u_long flags
)
400 ib_request
*ibreq
= calloc (1, sizeof (ib_request
));
401 char buf1
[strlen (name
) + 20];
402 char buf4
[strlen (name
) + 20];
404 res
= calloc (1, sizeof (nis_result
));
410 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
414 if (__create_ib_request (name
, ibreq
, flags
) == NULL
)
416 NIS_RES_STATUS (res
) = NIS_BADNAME
;
420 memcpy (&obj
, obj2
, sizeof (nis_object
));
422 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
423 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
425 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
426 obj
.zo_owner
= nis_local_principal ();
428 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
429 obj
.zo_group
= nis_local_group ();
431 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
433 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
434 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
436 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
439 ibreq
->ibr_obj
.ibr_obj_len
= 1;
441 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
442 (xdrproc_t
) _xdr_ib_request
,
444 (xdrproc_t
) _xdr_nis_result
,
445 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
446 NIS_RES_STATUS (res
) = status
;
448 nis_free_request (ibreq
);
454 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
, u_long flags
)
459 ib_request
*ibreq
= calloc (1, sizeof (ib_request
));
460 char buf1
[strlen (name
) + 20];
461 char buf4
[strlen (name
) + 20];
463 res
= calloc (1, sizeof (nis_result
));
465 if (__create_ib_request (name
, ibreq
, flags
) == NULL
)
467 NIS_RES_STATUS (res
) = NIS_BADNAME
;
471 memcpy (&obj
, obj2
, sizeof (nis_object
));
473 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
474 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
476 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
477 obj
.zo_owner
= nis_local_principal ();
479 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
480 obj
.zo_group
= nis_local_group ();
482 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
484 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
485 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
487 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
490 ibreq
->ibr_obj
.ibr_obj_len
= 1;
492 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
493 (xdrproc_t
) _xdr_ib_request
,
494 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
495 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
496 NIS_RES_STATUS (res
) = status
;
498 nis_free_request (ibreq
);
504 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
508 ib_request
*ibreq
= calloc (1, sizeof (ib_request
));
511 res
= calloc (1, sizeof (nis_result
));
517 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
521 if (__create_ib_request (name
, ibreq
, flags
) == NULL
)
523 NIS_RES_STATUS (res
) = NIS_BADNAME
;
529 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
530 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
532 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
535 ibreq
->ibr_obj
.ibr_obj_len
= 1;
538 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
539 (xdrproc_t
) _xdr_ib_request
,
540 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
541 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
542 NIS_RES_STATUS (res
) = status
;
544 nis_free_request (ibreq
);
550 nis_first_entry (const_nis_name name
)
553 ib_request
*ibreq
= calloc (1, sizeof (ib_request
));
556 res
= calloc (1, sizeof (nis_result
));
562 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
566 if (__create_ib_request (name
, ibreq
, 0) == NULL
)
568 NIS_RES_STATUS (res
) = NIS_BADNAME
;
572 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
573 (xdrproc_t
) _xdr_ib_request
,
574 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
575 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
576 NIS_RES_STATUS (res
) = status
;
578 nis_free_request (ibreq
);
584 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
587 ib_request
*ibreq
= calloc (1, sizeof (ib_request
));
590 res
= calloc (1, sizeof (nis_result
));
596 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
600 if (__create_ib_request (name
, ibreq
, 0) == NULL
)
602 NIS_RES_STATUS (res
) = NIS_BADNAME
;
608 ibreq
->ibr_cookie
.n_bytes
= malloc (cookie
->n_len
);
609 if (ibreq
->ibr_cookie
.n_bytes
== NULL
)
611 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
612 nis_free_request (ibreq
);
615 memcpy (ibreq
->ibr_cookie
.n_bytes
, cookie
->n_bytes
, cookie
->n_len
);
616 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
619 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
620 (xdrproc_t
) _xdr_ib_request
,
621 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
622 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
623 NIS_RES_STATUS (res
) = status
;
625 nis_free_request (ibreq
);