Update.
[glibc.git] / nis / nis_names.c
blobaa8c8802493ce44e5819253d38cc2e5fcd9e3135
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@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. */
20 #include <string.h>
21 #include <rpc/rpc.h>
22 #include <rpcsvc/nis.h>
23 #include <rpcsvc/nislib.h>
25 #include "nis_intern.h"
27 nis_result *
28 nis_lookup (const nis_name name, const u_long flags)
30 nis_result *res;
31 struct ns_request req;
32 nis_name *names;
33 nis_error status;
34 int is_link = 1; /* We should go at least once in the while loop */
35 int count_links = 0; /* We will follow only 16 links in the deep */
36 int i;
38 res = calloc (1, sizeof (nis_result));
40 if (flags & EXPAND_NAME)
42 names = __nis_expandname (name);
43 if (names == NULL)
45 res->status = NIS_NAMEUNREACHABLE;
46 return res;
49 i = 0;
50 while (names[i] != NULL && (i == 0 || res->status > 1))
52 req.ns_name = names[i];
54 while (is_link)
56 req.ns_object.ns_object_len = 0;
57 req.ns_object.ns_object_val = NULL;
58 memset (res, '\0', sizeof (nis_result));
60 if ((status = __do_niscall (NULL, 0, NIS_LOOKUP,
61 (xdrproc_t) xdr_ns_request,
62 (caddr_t) & req,
63 (xdrproc_t) xdr_nis_result,
64 (caddr_t) res, flags)) != RPC_SUCCESS)
66 res->status = status;
67 nis_freenames (names);
68 return res;
71 if ((res->status == NIS_SUCCESS || res->status == NIS_S_SUCCESS)
72 && (res->objects.objects_len > 0 &&
73 res->objects.objects_val->zo_data.zo_type == LINK_OBJ))
74 is_link = 1;
75 else
76 is_link = 0;
78 if (is_link)
80 if ((flags & FOLLOW_LINKS) == FOLLOW_LINKS)
82 if (count_links == 16)
84 res->status = NIS_LINKNAMEERROR;
85 return res;
87 else
88 ++count_links;
90 req.ns_name = res->objects.objects_val->LI_data.li_name;
92 else
94 res->status = NIS_NOTSEARCHABLE;
95 return res;
100 ++i;
101 if (res->status == NIS_NOT_ME)
102 res->status = NIS_NOSUCHNAME;
105 nis_freenames (names);
107 else
109 req.ns_name = name;
111 while (is_link)
113 req.ns_object.ns_object_len = 0;
114 req.ns_object.ns_object_val = NULL;
115 memset (res, '\0', sizeof (nis_result));
117 if ((status = __do_niscall (NULL, 0, NIS_LOOKUP,
118 (xdrproc_t) xdr_ns_request,
119 (caddr_t) & req,
120 (xdrproc_t) xdr_nis_result,
121 (caddr_t) res, flags)) != RPC_SUCCESS)
123 res->status = status;
124 return res;
127 if ((res->status == NIS_SUCCESS || res->status == NIS_S_SUCCESS) &&
128 (res->objects.objects_len > 0 &&
129 res->objects.objects_val->zo_data.zo_type == LINK_OBJ))
130 is_link = 1;
131 else
132 is_link = 0;
134 if (is_link)
136 if ((flags & FOLLOW_LINKS) == FOLLOW_LINKS)
138 if (count_links == 16)
140 res->status = NIS_LINKNAMEERROR;
141 return res;
143 else
144 ++count_links;
146 req.ns_name = res->objects.objects_val->LI_data.li_name;
148 else
150 res->status = NIS_NOTSEARCHABLE;
151 return res;
157 return res;
160 nis_result *
161 nis_add (const nis_name name, const nis_object *obj)
163 nis_result *res;
164 nis_error status;
165 struct ns_request req;
167 res = calloc (1, sizeof (nis_result));
169 req.ns_name = name;
171 req.ns_object.ns_object_len = 1;
172 req.ns_object.ns_object_val = nis_clone_object (obj, NULL);
174 if ((status = __do_niscall (NULL, 0, NIS_ADD, (xdrproc_t) xdr_ns_request,
175 (caddr_t) & req, (xdrproc_t) xdr_nis_result,
176 (caddr_t) res, 0)) != RPC_SUCCESS)
177 res->status = status;
179 nis_destroy_object (req.ns_object.ns_object_val);
181 return res;
184 nis_result *
185 nis_remove (const nis_name name, const nis_object *obj)
187 nis_result *res;
188 nis_error status;
189 struct ns_request req;
191 res = calloc (1, sizeof (nis_result));
193 req.ns_name = name;
195 if (obj != NULL)
197 req.ns_object.ns_object_len = 1;
198 req.ns_object.ns_object_val = nis_clone_object (obj, NULL);
200 else
202 req.ns_object.ns_object_len = 0;
203 req.ns_object.ns_object_val = NULL;
206 if ((status = __do_niscall (NULL, 0, NIS_REMOVE, (xdrproc_t) xdr_ns_request,
207 (caddr_t) & req, (xdrproc_t) xdr_nis_result,
208 (caddr_t) res, 0)) != RPC_SUCCESS)
209 res->status = status;
211 nis_destroy_object (req.ns_object.ns_object_val);
213 return res;
216 nis_result *
217 nis_modify (const nis_name name, const nis_object *obj)
219 nis_result *res;
220 nis_error status;
221 struct ns_request req;
223 res = calloc (1, sizeof (nis_result));
225 req.ns_name = name;
227 req.ns_object.ns_object_len = 1;
228 req.ns_object.ns_object_val = nis_clone_object (obj, NULL);
230 if ((status = __do_niscall (NULL, 0, NIS_MODIFY, (xdrproc_t) xdr_ns_request,
231 (caddr_t) & req, (xdrproc_t) xdr_nis_result,
232 (caddr_t) res, 0)) != RPC_SUCCESS)
233 res->status = status;
235 nis_destroy_object (req.ns_object.ns_object_val);
237 return res;