Update.
[glibc.git] / nis / nis_names.c
blobbb58ad9eafb22128ee1d486db2d2e179fecb9bee
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 = (char *)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;
166 char *p1, *p2, *p3, *p4;
167 char buf1 [strlen (name) + 20];
168 char buf4 [strlen (name) + 20];
170 res = calloc (1, sizeof (nis_result));
172 req.ns_name = (char *)name;
174 req.ns_object.ns_object_len = 1;
175 req.ns_object.ns_object_val = nis_clone_object (obj, NULL);
177 p1 = req.ns_object.ns_object_val[0].zo_name;
178 req.ns_object.ns_object_val[0].zo_name =
179 nis_name_of_r (name, buf1, sizeof (buf1));
181 p2 = req.ns_object.ns_object_val[0].zo_owner;
182 if (p2 == NULL || strlen (p2) == 0)
183 req.ns_object.ns_object_val[0].zo_owner = nis_local_principal ();
185 p3 = req.ns_object.ns_object_val[0].zo_group;
186 if (p3 == NULL || strlen (p3) == 0)
187 req.ns_object.ns_object_val[0].zo_group = nis_local_group ();
189 p4 = req.ns_object.ns_object_val[0].zo_domain;
190 req.ns_object.ns_object_val[0].zo_domain =
191 nis_domain_of_r (name, buf4, sizeof (buf4));
193 if ((status = __do_niscall (NULL, 0, NIS_ADD, (xdrproc_t) xdr_ns_request,
194 (caddr_t) &req, (xdrproc_t) xdr_nis_result,
195 (caddr_t) res, 0)) != RPC_SUCCESS)
196 res->status = status;
198 req.ns_object.ns_object_val[0].zo_name = p1;
199 req.ns_object.ns_object_val[0].zo_owner = p2;
200 req.ns_object.ns_object_val[0].zo_group = p3;
201 req.ns_object.ns_object_val[0].zo_domain = p4;
203 nis_destroy_object (req.ns_object.ns_object_val);
205 return res;
208 nis_result *
209 nis_remove (const_nis_name name, const nis_object *obj)
211 nis_result *res;
212 nis_error status;
213 struct ns_request req;
215 res = calloc (1, sizeof (nis_result));
217 req.ns_name = (char *)name;
219 if (obj != NULL)
221 req.ns_object.ns_object_len = 1;
222 req.ns_object.ns_object_val = nis_clone_object (obj, NULL);
224 else
226 req.ns_object.ns_object_len = 0;
227 req.ns_object.ns_object_val = NULL;
230 if ((status = __do_niscall (NULL, 0, NIS_REMOVE, (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;
240 nis_result *
241 nis_modify (const_nis_name name, const nis_object *obj)
243 nis_result *res;
244 nis_error status;
245 struct ns_request req;
247 res = calloc (1, sizeof (nis_result));
249 req.ns_name = (char *)name;
251 req.ns_object.ns_object_len = 1;
252 req.ns_object.ns_object_val = nis_clone_object (obj, NULL);
254 if ((status = __do_niscall (NULL, 0, NIS_MODIFY, (xdrproc_t) xdr_ns_request,
255 (caddr_t) & req, (xdrproc_t) xdr_nis_result,
256 (caddr_t) res, 0)) != RPC_SUCCESS)
257 res->status = status;
259 nis_destroy_object (req.ns_object.ns_object_val);
261 return res;