1 /* Copyright (C) 1997-1999, 2004, 2005, 2006, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #include <rpcsvc/nis.h>
24 #include "nis_intern.h"
29 nis_lookup (const_nis_name name
, const unsigned int flags
)
31 nis_result
*res
= calloc (1, sizeof (nis_result
));
32 struct ns_request req
;
35 int link_first_try
= 0;
36 int count_links
= 0; /* We will follow only 16 links in the deep */
39 nis_name namebuf
[2] = {NULL
, NULL
};
44 if ((flags
& EXPAND_NAME
) && (name
[strlen (name
) - 1] != '.'))
46 names
= nis_getnames (name
);
49 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
56 names
[0] = (nis_name
)name
;
59 req
.ns_name
= names
[0];
63 directory_obj
*dir
= NULL
;
64 req
.ns_object
.ns_object_len
= 0;
65 req
.ns_object
.ns_object_val
= NULL
;
67 status
= __prepare_niscall (req
.ns_name
, &dir
, &bptr
, flags
);
68 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
70 NIS_RES_STATUS (res
) = status
;
76 static const struct timeval RPCTIMEOUT
= {10, 0};
77 enum clnt_stat result
;
78 char ndomain
[strlen (req
.ns_name
) + 1];
81 result
= clnt_call (bptr
.clnt
, NIS_LOOKUP
,
82 (xdrproc_t
) _xdr_ns_request
,
83 (caddr_t
) &req
, (xdrproc_t
) _xdr_nis_result
,
84 (caddr_t
) res
, RPCTIMEOUT
);
86 if (result
!= RPC_SUCCESS
)
87 status
= NIS_RPCERROR
;
92 if (NIS_RES_STATUS (res
) == NIS_SUCCESS
)
94 if (__type_of (NIS_RES_OBJECT (res
)) == NIS_LINK_OBJ
95 && (flags
& FOLLOW_LINKS
)) /* We are following links */
97 /* if we hit the link limit, bail */
98 if (count_links
> NIS_MAXLINKS
)
100 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
105 strdupa (NIS_RES_OBJECT (res
)->LI_data
.li_name
);
107 /* The following is a non-obvious optimization. A
108 nis_freeresult call would call xdr_free as the
109 following code. But it also would unnecessarily
110 free the result structure. We avoid this here
111 along with the necessary tests. */
112 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) res
);
113 memset (res
, '\0', sizeof (*res
));
115 link_first_try
= 1; /* Try at first the old binding */
120 if (NIS_RES_STATUS (res
) == NIS_SYSTEMERROR
121 || NIS_RES_STATUS (res
) == NIS_NOSUCHNAME
122 || NIS_RES_STATUS (res
) == NIS_NOT_ME
)
126 __nisbind_destroy (&bptr
);
127 nis_free_directory (dir
);
128 /* Otherwise __nisfind_server will not do anything. */
131 if (__nisfind_server (req
.ns_name
, 1, &dir
, &bptr
,
132 flags
& ~MASTER_ONLY
)
137 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
139 /* No more servers to search. Try parent. */
140 nis_domain_of_r (req
.ns_name
, ndomain
,
142 req
.ns_name
= strdupa (ndomain
);
143 if (strcmp (ndomain
, ".") == 0)
145 NIS_RES_STATUS (res
) = NIS_NAMEUNREACHABLE
;
149 __nisbind_destroy (&bptr
);
150 nis_free_directory (dir
);
152 status
= __prepare_niscall (req
.ns_name
, &dir
,
154 if (__builtin_expect (status
!= NIS_SUCCESS
, 0))
156 NIS_RES_STATUS (res
) = status
;
162 while (__nisbind_connect (&bptr
) != NIS_SUCCESS
)
164 if (__nisbind_next (&bptr
) != NIS_SUCCESS
)
166 nis_free_directory (dir
);
174 link_first_try
= 0; /* Set it back */
176 while ((flags
& HARD_LOOKUP
) && status
== NIS_RPCERROR
);
178 __nisbind_destroy (&bptr
);
179 nis_free_directory (dir
);
181 if (status
!= NIS_SUCCESS
)
183 NIS_RES_STATUS (res
) = status
;
187 switch (NIS_RES_STATUS (res
))
192 case NIS_LINKNAMEERROR
: /* We follow to max links */
193 case NIS_UNAVAIL
: /* NIS+ is not installed, or all servers are down */
197 /* Try the next domainname if we don't follow a link */
201 NIS_RES_STATUS (res
) = NIS_LINKNAMEERROR
;
206 if (names
[name_nr
] == NULL
)
211 req
.ns_name
= names
[name_nr
];
217 if (names
!= namebuf
)
218 nis_freenames (names
);
222 libnsl_hidden_def (nis_lookup
)