* nis/nis_call.c (__nisbind_create): Remove __nisbind_destroy,
[glibc.git] / nis / nis_call.c
blob07f95f041c66057a11eb0b9e5f60ded9c73e441d
1 /* Copyright (C) 1997, 1998, 2001, 2004, 2005 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 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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <libintl.h>
24 #include <rpc/rpc.h>
25 #include <rpc/auth.h>
26 #include <rpcsvc/nis.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
31 #include "nis_xdr.h"
32 #include "nis_intern.h"
34 static const struct timeval RPCTIMEOUT = {10, 0};
35 static const struct timeval UDPTIMEOUT = {5, 0};
37 extern u_short __pmap_getnisport (struct sockaddr_in *address, u_long program,
38 u_long version, u_int protocol);
40 unsigned long
41 inetstr2int (const char *str)
43 char buffer[strlen (str) + 3];
44 size_t buflen;
45 size_t i, j;
47 buflen = stpcpy (buffer, str) - buffer;
49 j = 0;
50 for (i = 0; i < buflen; ++i)
51 if (buffer[i] == '.')
53 ++j;
54 if (j == 4)
56 buffer[i] = '\0';
57 break;
61 return inet_addr (buffer);
64 void
65 __nisbind_destroy (dir_binding *bind)
67 if (bind->clnt != NULL)
69 if (bind->use_auth)
70 auth_destroy (bind->clnt->cl_auth);
71 clnt_destroy (bind->clnt);
74 libnsl_hidden_def (__nisbind_destroy)
76 nis_error
77 __nisbind_next (dir_binding *bind)
79 u_int j;
81 if (bind->clnt != NULL)
83 if (bind->use_auth)
84 auth_destroy (bind->clnt->cl_auth);
85 clnt_destroy (bind->clnt);
86 bind->clnt = NULL;
89 if (bind->trys >= bind->server_len)
90 return NIS_FAIL;
92 for (j = bind->current_ep + 1;
93 j < bind->server_val[bind->server_used].ep.ep_len; ++j)
94 if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family,
95 "inet") == 0)
96 if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-')
98 bind->current_ep = j;
99 return NIS_SUCCESS;
102 ++bind->trys;
103 ++bind->server_used;
104 if (bind->server_used >= bind->server_len)
105 bind->server_used = 0;
107 for (j = 0; j < bind->server_val[bind->server_used].ep.ep_len; ++j)
108 if (strcmp (bind->server_val[bind->server_used].ep.ep_val[j].family,
109 "inet") == 0)
110 if (bind->server_val[bind->server_used].ep.ep_val[j].proto[0] == '-')
112 bind->current_ep = j;
113 return NIS_SUCCESS;
116 return NIS_FAIL;
118 libnsl_hidden_def (__nisbind_next)
120 nis_error
121 __nisbind_connect (dir_binding *dbp)
123 nis_server *serv;
125 if (dbp == NULL)
126 return NIS_FAIL;
128 serv = &dbp->server_val[dbp->server_used];
130 memset (&dbp->addr, '\0', sizeof (dbp->addr));
131 dbp->addr.sin_family = AF_INET;
133 dbp->addr.sin_addr.s_addr =
134 inetstr2int (serv->ep.ep_val[dbp->current_ep].uaddr);
136 if (dbp->addr.sin_addr.s_addr == 0)
137 return NIS_FAIL;
139 /* Check, if the host is online and rpc.nisd is running. Much faster
140 then the clnt*_create functions: */
141 if (__pmap_getnisport (&dbp->addr, NIS_PROG, NIS_VERSION, IPPROTO_UDP) == 0)
142 return NIS_RPCERROR;
144 dbp->socket = RPC_ANYSOCK;
145 if (dbp->use_udp)
146 dbp->clnt = clntudp_create (&dbp->addr, NIS_PROG, NIS_VERSION,
147 UDPTIMEOUT, &dbp->socket);
148 else
149 dbp->clnt = clnttcp_create (&dbp->addr, NIS_PROG, NIS_VERSION,
150 &dbp->socket, 0, 0);
152 if (dbp->clnt == NULL)
153 return NIS_RPCERROR;
155 clnt_control (dbp->clnt, CLSET_TIMEOUT, (caddr_t) &RPCTIMEOUT);
156 /* If the program exists, close the socket */
157 if (fcntl (dbp->socket, F_SETFD, 1) == -1)
158 perror ("fcntl: F_SETFD");
160 if (dbp->use_auth)
162 if (serv->key_type == NIS_PK_DH)
164 char netname[MAXNETNAMELEN + 1];
165 char *p;
167 p = stpcpy (netname, "unix.");
168 strncpy (p, serv->name, MAXNETNAMELEN - 5);
169 netname[MAXNETNAMELEN] = '\0';
170 // XXX What is this supposed to do? If we really want to replace
171 // XXX the first dot, then we might as well use unix@ as the
172 // XXX prefix string. --drepper
173 p = strchr (netname, '.');
174 *p = '@';
175 dbp->clnt->cl_auth =
176 authdes_pk_create (netname, &serv->pkey, 300, NULL, NULL);
177 if (!dbp->clnt->cl_auth)
178 dbp->clnt->cl_auth = authunix_create_default ();
180 else
181 dbp->clnt->cl_auth = authunix_create_default ();
184 return NIS_SUCCESS;
186 libnsl_hidden_def (__nisbind_connect)
188 nis_error
189 __nisbind_create (dir_binding *dbp, const nis_server *serv_val,
190 unsigned int serv_len, unsigned int flags)
192 dbp->clnt = NULL;
194 dbp->server_len = serv_len;
195 dbp->server_val = (nis_server *)serv_val;
197 if (flags & USE_DGRAM)
198 dbp->use_udp = TRUE;
199 else
200 dbp->use_udp = FALSE;
202 if (flags & NO_AUTHINFO)
203 dbp->use_auth = FALSE;
204 else
205 dbp->use_auth = TRUE;
207 if (flags & MASTER_ONLY)
208 dbp->master_only = TRUE;
209 else
210 dbp->master_only = FALSE;
212 /* We try the first server */
213 dbp->trys = 1;
215 dbp->class = -1;
216 if (__nis_findfastest (dbp) < 1)
217 return NIS_NAMEUNREACHABLE;
219 return NIS_SUCCESS;
221 libnsl_hidden_def (__nisbind_create)
223 /* __nisbind_connect (dbp) must be run before calling this function !
224 So we could use the same binding twice */
225 nis_error
226 __do_niscall3 (dir_binding *dbp, u_long prog, xdrproc_t xargs, caddr_t req,
227 xdrproc_t xres, caddr_t resp, unsigned int flags, nis_cb *cb)
229 enum clnt_stat result;
230 nis_error retcode;
232 if (dbp == NULL)
233 return NIS_NAMEUNREACHABLE;
237 again:
238 result = clnt_call (dbp->clnt, prog, xargs, req, xres, resp, RPCTIMEOUT);
240 if (result != RPC_SUCCESS)
241 retcode = NIS_RPCERROR;
242 else
244 switch (prog)
246 case NIS_IBLIST:
247 if ((((nis_result *)resp)->status == NIS_CBRESULTS) &&
248 (cb != NULL))
250 __nis_do_callback (dbp, &((nis_result *) resp)->cookie, cb);
251 break;
253 /* Yes, the missing break is correct. If we doesn't have to
254 start a callback, look if we have to search another server */
255 case NIS_LOOKUP:
256 case NIS_ADD:
257 case NIS_MODIFY:
258 case NIS_REMOVE:
259 case NIS_IBADD:
260 case NIS_IBMODIFY:
261 case NIS_IBREMOVE:
262 case NIS_IBFIRST:
263 case NIS_IBNEXT:
264 if (((nis_result *)resp)->status == NIS_SYSTEMERROR
265 || ((nis_result *)resp)->status == NIS_NOSUCHNAME
266 || ((nis_result *)resp)->status == NIS_NOT_ME)
268 if (__nisbind_next (dbp) == NIS_SUCCESS)
270 while (__nisbind_connect (dbp) != NIS_SUCCESS)
272 if (__nisbind_next (dbp) != NIS_SUCCESS)
273 return NIS_SUCCESS;
276 else
277 break; /* No more servers to search in */
278 goto again;
280 break;
281 case NIS_FINDDIRECTORY:
282 if (((fd_result *)resp)->status == NIS_SYSTEMERROR
283 || ((fd_result *)resp)->status == NIS_NOSUCHNAME
284 || ((fd_result *)resp)->status == NIS_NOT_ME)
286 if (__nisbind_next (dbp) == NIS_SUCCESS)
288 while (__nisbind_connect (dbp) != NIS_SUCCESS)
290 if (__nisbind_next (dbp) != NIS_SUCCESS)
291 return NIS_SUCCESS;
294 else
295 break; /* No more servers to search in */
296 goto again;
298 break;
299 case NIS_DUMPLOG: /* log_result */
300 case NIS_DUMP:
301 if (((log_result *)resp)->lr_status == NIS_SYSTEMERROR
302 || ((log_result *)resp)->lr_status == NIS_NOSUCHNAME
303 || ((log_result *)resp)->lr_status == NIS_NOT_ME)
305 if (__nisbind_next (dbp) == NIS_SUCCESS)
307 while (__nisbind_connect (dbp) != NIS_SUCCESS)
309 if (__nisbind_next (dbp) != NIS_SUCCESS)
310 return NIS_SUCCESS;
313 else
314 break; /* No more servers to search in */
315 goto again;
317 break;
318 default:
319 break;
321 retcode = NIS_SUCCESS;
324 while ((flags & HARD_LOOKUP) && retcode == NIS_RPCERROR);
326 return retcode;
329 nis_error
330 __do_niscall2 (const nis_server *server, u_int server_len, u_long prog,
331 xdrproc_t xargs, caddr_t req, xdrproc_t xres, caddr_t resp,
332 unsigned int flags, nis_cb *cb)
334 dir_binding dbp;
335 nis_error status;
337 if (flags & MASTER_ONLY)
338 server_len = 1;
340 status = __nisbind_create (&dbp, server, server_len, flags);
341 if (status != NIS_SUCCESS)
342 return status;
344 while (__nisbind_connect (&dbp) != NIS_SUCCESS)
345 if (__nisbind_next (&dbp) != NIS_SUCCESS)
346 return NIS_NAMEUNREACHABLE;
348 status = __do_niscall3 (&dbp, prog, xargs, req, xres, resp, flags, cb);
350 __nisbind_destroy (&dbp);
352 return status;
356 static directory_obj *
357 rec_dirsearch (const_nis_name name, directory_obj *dir, nis_error *status)
359 fd_result *fd_res;
360 XDR xdrs;
362 switch (nis_dir_cmp (name, dir->do_name))
364 case SAME_NAME:
365 *status = NIS_SUCCESS;
366 return dir;
367 case NOT_SEQUENTIAL:
368 /* NOT_SEQUENTIAL means, go one up and try it there ! */
369 case HIGHER_NAME:
370 { /* We need data from a parent domain */
371 directory_obj *obj;
372 char ndomain [strlen (name) + 3];
374 nis_domain_of_r (dir->do_name, ndomain, sizeof (ndomain));
376 /* The root server of our domain is a replica of the parent
377 domain ! (Now I understand why a root server must be a
378 replica of the parent domain) */
379 fd_res = __nis_finddirectory (dir, ndomain);
380 *status = fd_res->status;
381 if (fd_res->status != NIS_SUCCESS)
383 /* Try the current directory obj, maybe it works */
384 __free_fdresult (fd_res);
385 return dir;
387 obj = calloc (1, sizeof (directory_obj));
388 xdrmem_create (&xdrs, fd_res->dir_data.dir_data_val,
389 fd_res->dir_data.dir_data_len, XDR_DECODE);
390 _xdr_directory_obj (&xdrs, obj);
391 xdr_destroy (&xdrs);
392 __free_fdresult (fd_res);
393 if (obj != NULL)
395 /* We have found a NIS+ server serving ndomain, now
396 let us search for "name" */
397 nis_free_directory (dir);
398 return rec_dirsearch (name, obj, status);
400 else
402 /* Ups, very bad. Are we already the root server ? */
403 nis_free_directory (dir);
404 return NULL;
407 break;
408 case LOWER_NAME:
410 directory_obj *obj;
411 size_t namelen = strlen (name);
412 char leaf[namelen + 3];
413 char domain[namelen + 3];
414 char ndomain[namelen + 3];
415 char *cp;
416 u_int run = 0;
418 strcpy (domain, name);
422 if (domain[0] == '\0')
424 nis_free_directory (dir);
425 return NULL;
427 nis_leaf_of_r (domain, leaf, sizeof (leaf));
428 nis_domain_of_r (domain, ndomain, sizeof (ndomain));
429 strcpy (domain, ndomain);
430 ++run;
432 while (nis_dir_cmp (domain, dir->do_name) != SAME_NAME);
434 if (run == 1)
436 /* We have found the directory above. Use it. */
437 return dir;
440 cp = strchr (leaf, '\0');
441 *cp++ = '.';
442 strcpy (cp, domain);
444 fd_res = __nis_finddirectory (dir, leaf);
445 *status = fd_res->status;
446 if (fd_res->status != NIS_SUCCESS)
448 /* Try the current directory object, maybe it works */
449 __free_fdresult (fd_res);
450 return dir;
452 obj = calloc(1, sizeof(directory_obj));
453 xdrmem_create(&xdrs, fd_res->dir_data.dir_data_val,
454 fd_res->dir_data.dir_data_len, XDR_DECODE);
455 _xdr_directory_obj(&xdrs, obj);
456 xdr_destroy(&xdrs);
457 __free_fdresult (fd_res);
458 if (obj != NULL)
460 /* We have found a NIS+ server serving ndomain, now
461 let us search for "name" */
462 nis_free_directory (dir);
463 return rec_dirsearch (name, obj, status);
466 break;
467 case BAD_NAME:
468 nis_free_directory (dir);
469 *status = NIS_BADNAME;
470 return NULL;
472 nis_free_directory (dir);
473 *status = NIS_FAIL;
474 return NULL;
477 /* We try to query the current server for the searched object,
478 maybe he know about it ? */
479 static directory_obj *
480 first_shoot (const_nis_name name, directory_obj *dir)
482 directory_obj *obj = NULL;
483 fd_result *fd_res;
484 XDR xdrs;
485 char domain[strlen (name) + 3];
487 if (nis_dir_cmp (name, dir->do_name) == SAME_NAME)
488 return dir;
490 nis_domain_of_r (name, domain, sizeof (domain));
492 if (nis_dir_cmp (domain, dir->do_name) == SAME_NAME)
493 return dir;
495 fd_res = __nis_finddirectory (dir, domain);
496 if (fd_res->status == NIS_SUCCESS
497 && (obj = calloc (1, sizeof (directory_obj))) != NULL)
499 xdrmem_create(&xdrs, fd_res->dir_data.dir_data_val,
500 fd_res->dir_data.dir_data_len, XDR_DECODE);
501 _xdr_directory_obj (&xdrs, obj);
502 xdr_destroy (&xdrs);
505 __free_fdresult (fd_res);
507 if (obj != NULL)
508 nis_free_directory (dir);
510 return obj;
513 nis_error
514 __nisfind_server (const_nis_name name, directory_obj **dir)
516 if (name == NULL)
517 return NIS_BADNAME;
519 #if 0
520 /* Search in local cache. In the moment, we ignore the fastest server */
521 if (!(flags & NO_CACHE))
522 dir = __nis_cache_search (name, flags, &cinfo);
523 #endif
525 if (*dir == NULL)
527 nis_error status;
528 directory_obj *obj;
530 *dir = readColdStartFile ();
531 if (*dir == NULL) /* No /var/nis/NIS_COLD_START->no NIS+ installed */
532 return NIS_UNAVAIL;
534 /* Try at first, if servers in "dir" know our object */
535 obj = first_shoot (name, *dir);
536 if (obj == NULL)
538 *dir = rec_dirsearch (name, *dir, &status);
539 if (*dir == NULL)
540 return status;
542 else
543 *dir = obj;
546 return NIS_SUCCESS;
549 nis_error
550 __do_niscall (const_nis_name name, u_long prog, xdrproc_t xargs,
551 caddr_t req, xdrproc_t xres, caddr_t resp, unsigned int flags,
552 nis_cb *cb)
554 nis_error retcode;
555 dir_binding bptr;
556 directory_obj *dir = NULL;
557 nis_server *server;
558 u_int server_len;
559 int saved_errno = errno;
561 retcode = __nisfind_server (name, &dir);
562 if (retcode != NIS_SUCCESS)
563 return retcode;
565 if (flags & MASTER_ONLY)
567 server = dir->do_servers.do_servers_val;
568 server_len = 1;
570 else
572 server = dir->do_servers.do_servers_val;
573 server_len = dir->do_servers.do_servers_len;
576 retcode = __nisbind_create (&bptr, server, server_len, flags);
577 if (retcode == NIS_SUCCESS)
579 while (__nisbind_connect (&bptr) != NIS_SUCCESS)
581 if (__nisbind_next (&bptr) != NIS_SUCCESS)
583 nis_free_directory (dir);
584 return NIS_NAMEUNREACHABLE;
587 retcode = __do_niscall3 (&bptr, prog, xargs, req, xres, resp, flags, cb);
589 __nisbind_destroy (&bptr);
592 nis_free_directory (dir);
594 __set_errno (saved_errno);
596 return retcode;