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 static struct ib_request
*
28 __create_ib_request (const_nis_name name
, u_long flags
)
30 struct ib_request
*ibreq
= calloc (1, sizeof (ib_request
));
31 char buf
[strlen (name
) + 1];
32 nis_attr
*search_val
= NULL
;
37 ibreq
->ibr_flags
= flags
;
39 cptr
= strcpy (buf
, name
);
41 /* Not of "[key=value,key=value,...],foo.." format? */
44 ibreq
->ibr_name
= strdup (cptr
);
48 /* "[key=value,...],foo" format */
49 ibreq
->ibr_name
= strchr (cptr
, ']');
50 if (ibreq
->ibr_name
== NULL
|| ibreq
->ibr_name
[1] != ',')
53 /* Check if we have an entry of "[key=value,],bar". If, remove the "," */
54 if (ibreq
->ibr_name
[-1] == ',')
55 ibreq
->ibr_name
[-1] = '\0';
57 ibreq
->ibr_name
[0] = '\0';
59 ibreq
->ibr_name
= strdup (ibreq
->ibr_name
);
61 ++cptr
; /* Remove "[" */
63 while (cptr
!= NULL
&& cptr
[0] != '\0')
66 char *val
= strchr (cptr
, '=');
68 cptr
= strchr (key
, ',');
74 nis_free_request (ibreq
);
78 if ((search_len
+ 1) >= size
)
82 search_val
= malloc (size
* sizeof (nis_attr
));
84 search_val
= realloc (search_val
, size
* sizeof (nis_attr
));
85 if (search_val
== NULL
)
87 nis_free_request (ibreq
);
91 search_val
[search_len
].zattr_ndx
= strdup (key
);
92 if ((search_val
[search_len
].zattr_ndx
) == NULL
)
94 nis_free_request (ibreq
);
97 search_val
[search_len
].zattr_val
.zattr_val_len
= strlen (val
) + 1;
98 search_val
[search_len
].zattr_val
.zattr_val_val
= strdup (val
);
99 if (search_val
[search_len
].zattr_val
.zattr_val_val
== NULL
)
101 nis_free_request (ibreq
);
107 ibreq
->ibr_srch
.ibr_srch_val
= search_val
;
108 ibreq
->ibr_srch
.ibr_srch_len
= search_len
;
114 nis_list (const_nis_name name
, u_long flags
,
115 int (*callback
) (const_nis_name name
,
116 const nis_object
*object
,
117 const void *userdata
),
118 const void *userdata
)
120 nis_result
*res
= NULL
;
123 int count_links
= 0; /* We will only follow NIS_MAXLINKS links! */
126 nis_name namebuf
[2] = {NULL
, NULL
};
130 res
= calloc (1, sizeof (nis_result
));
136 NIS_RES_STATUS (res
) = NIS_BADNAME
;
140 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
142 NIS_RES_STATUS (res
) = NIS_BADNAME
;
146 if ((flags
& EXPAND_NAME
) &&
147 ibreq
->ibr_name
[strlen (ibreq
->ibr_name
) - 1] != '.')
149 names
= nis_getnames (ibreq
->ibr_name
);
150 free (ibreq
->ibr_name
);
151 ibreq
->ibr_name
= NULL
;
154 NIS_RES_STATUS (res
) = NIS_BADNAME
;
157 ibreq
->ibr_name
= strdup (names
[name_nr
]);
162 names
[name_nr
] = ibreq
->ibr_name
;
167 if (flags
& FOLLOW_PATH
|| flags
& ALL_RESULTS
)
170 u_long newflags
= flags
& ~FOLLOW_PATH
& ~ALL_RESULTS
;
171 char table_path
[NIS_MAXPATH
+ 3];
173 u_long done
= 0, failures
= 0;
175 while (names
[name_nr
] != NULL
&& !done
)
177 lres
= nis_lookup (names
[name_nr
], newflags
| NO_AUTHINFO
);
178 if (lres
== NULL
|| NIS_RES_STATUS (lres
) != NIS_SUCCESS
)
180 NIS_RES_STATUS (res
) = NIS_RES_STATUS (lres
);
181 nis_freeresult (lres
);
186 /* nis_lookup handles FOLLOW_LINKS,
187 so we must have a table object.*/
188 if (__type_of (NIS_RES_OBJECT (lres
)) != NIS_TABLE_OBJ
)
190 nis_freeresult (lres
);
191 NIS_RES_STATUS (res
) = NIS_INVALIDOBJ
;
195 /* Save the path, discard everything else. */
196 p
= __stpncpy (table_path
, names
[name_nr
], NIS_MAXPATH
);
198 p
= __stpncpy (p
, NIS_RES_OBJECT (lres
)->TA_data
.ta_path
,
199 NIS_MAXPATH
- (p
- table_path
));
201 nis_freeresult (lres
);
207 while (((ntable
= strsep (&p
, ":")) != NULL
) && !done
)
212 nis_freeresult (res
);
214 /* Do the job recursive here! */
215 if ((c
= strchr(name
, ']')) != NULL
)
217 /* Have indexed name ! */
218 int index_len
= c
- name
+ 2;
219 char buf
[index_len
+ strlen (ntable
) + 1];
221 c
= __stpncpy (buf
, name
, index_len
);
223 res
= nis_list (buf
, newflags
, callback
,userdata
);
226 res
= nis_list (ntable
, newflags
, callback
, userdata
);
229 switch (NIS_RES_STATUS (res
))
233 if (!(flags
& ALL_RESULTS
))
236 case NIS_PARTIAL
: /* The table is correct, we doesn't found
240 if (flags
& ALL_RESULTS
)
247 if (NIS_RES_STATUS (res
) == NIS_SUCCESS
&& failures
)
248 NIS_RES_STATUS (res
) = NIS_S_SUCCESS
;
249 if (NIS_RES_STATUS (res
) == NIS_NOTFOUND
&& failures
)
250 NIS_RES_STATUS (res
) = NIS_S_NOTFOUND
;
256 if (callback
!= NULL
)
258 cb
= __nis_create_callback (callback
, userdata
, flags
);
259 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 1;
260 ibreq
->ibr_cbhost
.ibr_cbhost_val
= cb
->serv
;
265 memset (res
, '\0', sizeof (nis_result
));
267 status
= __do_niscall (ibreq
->ibr_name
, NIS_IBLIST
,
268 (xdrproc_t
) _xdr_ib_request
,
269 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
270 (caddr_t
) res
, flags
, cb
);
271 if (status
!= NIS_SUCCESS
)
272 NIS_RES_STATUS (res
) = status
;
274 switch (NIS_RES_STATUS (res
))
279 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
&&
280 flags
& FOLLOW_LINKS
) /* We are following links. */
282 /* If we hit the link limit, bail. */
283 if (count_links
> NIS_MAXLINKS
)
285 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
290 free (ibreq
->ibr_name
);
292 free (ibreq
->ibr_name
);
294 strdup (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
295 if (NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
)
296 if (ibreq
->ibr_srch
.ibr_srch_len
== 0)
298 ibreq
->ibr_srch
.ibr_srch_len
=
299 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_len
;
300 ibreq
->ibr_srch
.ibr_srch_val
=
301 NIS_RES_OBJECT (res
)->LI_data
.li_attrs
.li_attrs_val
;
303 nis_freeresult (res
);
304 res
= calloc (1, sizeof (nis_result
));
310 /* Calback is handled in nis_call.c (__do_niscall2),
311 but we have to change the error code */
312 NIS_RES_STATUS (res
) = cb
->result
;
316 /* NIS+ is not installed, or all servers are down. */
320 /* Try the next domainname if we don't follow a link. */
323 free (ibreq
->ibr_name
);
324 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
329 if (names
[name_nr
] == NULL
)
334 ibreq
->ibr_name
= names
[name_nr
];
338 } /* End of not FOLLOW_PATH. */
340 if (names
!= namebuf
)
341 nis_freenames (names
);
345 __nis_destroy_callback (cb
);
346 ibreq
->ibr_cbhost
.ibr_cbhost_len
= 0;
347 ibreq
->ibr_cbhost
.ibr_cbhost_val
= NULL
;
350 nis_free_request (ibreq
);
356 nis_add_entry (const_nis_name name
, const nis_object
*obj2
, u_long flags
)
362 size_t namelen
= strlen (name
);
363 char buf1
[namelen
+ 20];
364 char buf4
[namelen
+ 20];
366 res
= calloc (1, sizeof (nis_result
));
372 NIS_RES_STATUS (res
) = NIS_BADNAME
;
376 if ((ibreq
= __create_ib_request (name
, flags
)) == NULL
)
378 NIS_RES_STATUS (res
) = NIS_BADNAME
;
382 memcpy (&obj
, obj2
, sizeof (nis_object
));
384 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
385 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
387 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
388 obj
.zo_owner
= nis_local_principal ();
390 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
391 obj
.zo_group
= nis_local_group ();
393 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
395 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
396 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
398 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
401 ibreq
->ibr_obj
.ibr_obj_len
= 1;
403 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBADD
,
404 (xdrproc_t
) _xdr_ib_request
,
406 (xdrproc_t
) _xdr_nis_result
,
407 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
408 NIS_RES_STATUS (res
) = status
;
410 nis_free_request (ibreq
);
416 nis_modify_entry (const_nis_name name
, const nis_object
*obj2
, u_long flags
)
422 size_t namelen
= strlen (name
);
423 char buf1
[namelen
+ 20];
424 char buf4
[namelen
+ 20];
426 res
= calloc (1, sizeof (nis_result
));
430 if (( ibreq
=__create_ib_request (name
, flags
)) == NULL
)
432 NIS_RES_STATUS (res
) = NIS_BADNAME
;
436 memcpy (&obj
, obj2
, sizeof (nis_object
));
438 if (obj
.zo_name
== NULL
|| strlen (obj
.zo_name
) == 0)
439 obj
.zo_name
= nis_leaf_of_r (name
, buf1
, sizeof (buf1
));
441 if (obj
.zo_owner
== NULL
|| strlen (obj
.zo_owner
) == 0)
442 obj
.zo_owner
= nis_local_principal ();
444 if (obj
.zo_group
== NULL
|| strlen (obj
.zo_group
) == 0)
445 obj
.zo_group
= nis_local_group ();
447 obj
.zo_domain
= nis_domain_of_r (name
, buf4
, sizeof (buf4
));
449 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (&obj
, NULL
);
450 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
452 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
455 ibreq
->ibr_obj
.ibr_obj_len
= 1;
457 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBMODIFY
,
458 (xdrproc_t
) _xdr_ib_request
,
459 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
460 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
461 NIS_RES_STATUS (res
) = status
;
463 nis_free_request (ibreq
);
469 nis_remove_entry (const_nis_name name
, const nis_object
*obj
,
476 res
= calloc (1, sizeof (nis_result
));
482 NIS_RES_STATUS (res
) = NIS_BADNAME
;
486 if ((ibreq
=__create_ib_request (name
, flags
)) == NULL
)
488 NIS_RES_STATUS (res
) = NIS_BADNAME
;
494 ibreq
->ibr_obj
.ibr_obj_val
= nis_clone_object (obj
, NULL
);
495 if (ibreq
->ibr_obj
.ibr_obj_val
== NULL
)
497 NIS_RES_STATUS (res
) = NIS_NOMEMORY
;
500 ibreq
->ibr_obj
.ibr_obj_len
= 1;
503 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBREMOVE
,
504 (xdrproc_t
) _xdr_ib_request
,
505 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
506 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
507 NIS_RES_STATUS (res
) = status
;
509 nis_free_request (ibreq
);
515 nis_first_entry (const_nis_name name
)
521 res
= calloc (1, sizeof (nis_result
));
527 NIS_RES_STATUS (res
) = NIS_BADNAME
;
531 if ((ibreq
=__create_ib_request (name
, 0)) == NULL
)
533 NIS_RES_STATUS (res
) = NIS_BADNAME
;
537 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBFIRST
,
538 (xdrproc_t
) _xdr_ib_request
,
539 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
540 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
541 NIS_RES_STATUS (res
) = status
;
543 nis_free_request (ibreq
);
549 nis_next_entry (const_nis_name name
, const netobj
*cookie
)
555 res
= calloc (1, sizeof (nis_result
));
561 NIS_RES_STATUS (res
) = NIS_BADNAME
;
565 if (( ibreq
=__create_ib_request (name
, 0)) == NULL
)
567 NIS_RES_STATUS (res
) = NIS_BADNAME
;
573 ibreq
->ibr_cookie
.n_bytes
= cookie
->n_bytes
;
574 ibreq
->ibr_cookie
.n_len
= cookie
->n_len
;
577 if ((status
= __do_niscall (ibreq
->ibr_name
, NIS_IBNEXT
,
578 (xdrproc_t
) _xdr_ib_request
,
579 (caddr_t
) ibreq
, (xdrproc_t
) _xdr_nis_result
,
580 (caddr_t
) res
, 0, NULL
)) != NIS_SUCCESS
)
581 NIS_RES_STATUS (res
) = status
;
585 /* Don't give cookie free, it is not from us */
586 ibreq
->ibr_cookie
.n_bytes
= NULL
;
587 ibreq
->ibr_cookie
.n_len
= 0;
590 nis_free_request (ibreq
);