]>
Commit | Line | Data |
---|---|---|
697d37b1 JJ |
1 | /* Copyright (C) 1997-1999, 2004, 2005, 2006, 2007 |
2 | Free Software Foundation, Inc. | |
51702635 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Thorsten Kukuk <kukuk@uni-paderborn.de>, 1997. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
51702635 UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
51702635 | 15 | |
41bdb6e2 AJ |
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 | |
19 | 02111-1307 USA. */ | |
51702635 UD |
20 | |
21 | #include <string.h> | |
22 | #include <rpcsvc/nis.h> | |
91eee4dd | 23 | #include "nis_xdr.h" |
51702635 | 24 | #include "nis_intern.h" |
062e719b UD |
25 | #include <libnsl.h> |
26 | ||
51702635 UD |
27 | |
28 | nis_result * | |
a1129917 | 29 | nis_lookup (const_nis_name name, const unsigned int flags) |
51702635 | 30 | { |
e852e889 | 31 | nis_result *res = calloc (1, sizeof (nis_result)); |
51702635 UD |
32 | struct ns_request req; |
33 | nis_name *names; | |
34 | nis_error status; | |
e852e889 | 35 | int link_first_try = 0; |
51702635 | 36 | int count_links = 0; /* We will follow only 16 links in the deep */ |
2d7da676 UD |
37 | int done = 0; |
38 | int name_nr = 0; | |
39 | nis_name namebuf[2] = {NULL, NULL}; | |
51702635 | 40 | |
91eee4dd UD |
41 | if (res == NULL) |
42 | return NULL; | |
51702635 | 43 | |
e852e889 | 44 | if ((flags & EXPAND_NAME) && (name[strlen (name) - 1] != '.')) |
51702635 | 45 | { |
2d7da676 | 46 | names = nis_getnames (name); |
51702635 UD |
47 | if (names == NULL) |
48 | { | |
91eee4dd | 49 | NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE; |
51702635 UD |
50 | return res; |
51 | } | |
51702635 UD |
52 | } |
53 | else | |
54 | { | |
2d7da676 | 55 | names = namebuf; |
91eee4dd | 56 | names[0] = (nis_name)name; |
2d7da676 | 57 | } |
51702635 | 58 | |
2d7da676 UD |
59 | req.ns_name = names[0]; |
60 | while (!done) | |
61 | { | |
e852e889 UD |
62 | dir_binding bptr; |
63 | directory_obj *dir = NULL; | |
2d7da676 UD |
64 | req.ns_object.ns_object_len = 0; |
65 | req.ns_object.ns_object_val = NULL; | |
2d7da676 | 66 | |
062e719b UD |
67 | status = __prepare_niscall (req.ns_name, &dir, &bptr, flags); |
68 | if (__builtin_expect (status != NIS_SUCCESS, 0)) | |
e852e889 UD |
69 | { |
70 | NIS_RES_STATUS (res) = status; | |
51e59260 | 71 | goto out; |
a334319f UD |
72 | } |
73 | ||
e852e889 UD |
74 | do |
75 | { | |
d00002ed | 76 | static const struct timeval RPCTIMEOUT = {10, 0}; |
e852e889 UD |
77 | enum clnt_stat result; |
78 | ||
79 | again: | |
80 | result = clnt_call (bptr.clnt, NIS_LOOKUP, | |
81 | (xdrproc_t) _xdr_ns_request, | |
82 | (caddr_t) &req, (xdrproc_t) _xdr_nis_result, | |
83 | (caddr_t) res, RPCTIMEOUT); | |
84 | ||
85 | if (result != RPC_SUCCESS) | |
86 | status = NIS_RPCERROR; | |
87 | else | |
51702635 | 88 | { |
50f301a8 AS |
89 | status = NIS_SUCCESS; |
90 | ||
e852e889 | 91 | if (NIS_RES_STATUS (res) == NIS_SUCCESS) |
2d7da676 | 92 | { |
062e719b | 93 | if (__type_of (NIS_RES_OBJECT (res)) == NIS_LINK_OBJ |
d00002ed | 94 | && (flags & FOLLOW_LINKS)) /* We are following links */ |
e852e889 | 95 | { |
e852e889 UD |
96 | /* if we hit the link limit, bail */ |
97 | if (count_links > NIS_MAXLINKS) | |
98 | { | |
99 | NIS_RES_STATUS (res) = NIS_LINKNAMEERROR; | |
100 | break; | |
101 | } | |
102 | ++count_links; | |
103 | req.ns_name = | |
062e719b | 104 | strdupa (NIS_RES_OBJECT (res)->LI_data.li_name); |
32abdb71 | 105 | |
51e59260 UD |
106 | /* The following is a non-obvious optimization. A |
107 | nis_freeresult call would call xdr_free as the | |
108 | following code. But it also would unnecessarily | |
109 | free the result structure. We avoid this here | |
110 | along with the necessary tests. */ | |
51e59260 UD |
111 | xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res); |
112 | memset (res, '\0', sizeof (*res)); | |
32abdb71 | 113 | |
e852e889 UD |
114 | link_first_try = 1; /* Try at first the old binding */ |
115 | goto again; | |
116 | } | |
2d7da676 | 117 | } |
e852e889 | 118 | else |
d00002ed UD |
119 | if (NIS_RES_STATUS (res) == NIS_SYSTEMERROR |
120 | || NIS_RES_STATUS (res) == NIS_NOSUCHNAME | |
121 | || NIS_RES_STATUS (res) == NIS_NOT_ME) | |
e852e889 UD |
122 | { |
123 | if (link_first_try) | |
124 | { | |
125 | __nisbind_destroy (&bptr); | |
126 | nis_free_directory (dir); | |
51e59260 UD |
127 | /* Otherwise __nisfind_server will not do anything. */ |
128 | dir = NULL; | |
e852e889 | 129 | |
697d37b1 JJ |
130 | if (__nisfind_server (req.ns_name, 1, &dir, &bptr, |
131 | flags & ~MASTER_ONLY) | |
d00002ed | 132 | != NIS_SUCCESS) |
51e59260 | 133 | goto out; |
e852e889 UD |
134 | } |
135 | else | |
136 | if (__nisbind_next (&bptr) != NIS_SUCCESS) | |
062e719b UD |
137 | { |
138 | /* No more servers to search. Try parent. */ | |
7ab174ed | 139 | const char *ndomain = __nis_domain_of (req.ns_name); |
062e719b | 140 | req.ns_name = strdupa (ndomain); |
7ab174ed | 141 | if (strcmp (req.ns_name, ".") == 0) |
bd1ebae0 UD |
142 | { |
143 | NIS_RES_STATUS (res) = NIS_NAMEUNREACHABLE; | |
144 | goto out; | |
145 | } | |
062e719b UD |
146 | |
147 | __nisbind_destroy (&bptr); | |
148 | nis_free_directory (dir); | |
149 | dir = NULL; | |
150 | status = __prepare_niscall (req.ns_name, &dir, | |
151 | &bptr, flags); | |
152 | if (__builtin_expect (status != NIS_SUCCESS, 0)) | |
153 | { | |
154 | NIS_RES_STATUS (res) = status; | |
155 | goto out; | |
156 | } | |
157 | goto again; | |
158 | } | |
e852e889 UD |
159 | |
160 | while (__nisbind_connect (&bptr) != NIS_SUCCESS) | |
161 | { | |
162 | if (__nisbind_next (&bptr) != NIS_SUCCESS) | |
163 | { | |
e852e889 | 164 | nis_free_directory (dir); |
51e59260 | 165 | goto out; |
e852e889 UD |
166 | } |
167 | } | |
168 | goto again; | |
169 | } | |
170 | break; | |
51702635 | 171 | } |
e852e889 | 172 | link_first_try = 0; /* Set it back */ |
e852e889 UD |
173 | } |
174 | while ((flags & HARD_LOOKUP) && status == NIS_RPCERROR); | |
175 | ||
176 | __nisbind_destroy (&bptr); | |
177 | nis_free_directory (dir); | |
178 | ||
179 | if (status != NIS_SUCCESS) | |
180 | { | |
181 | NIS_RES_STATUS (res) = status; | |
51e59260 | 182 | goto out; |
e852e889 UD |
183 | } |
184 | ||
185 | switch (NIS_RES_STATUS (res)) | |
186 | { | |
187 | case NIS_PARTIAL: | |
188 | case NIS_SUCCESS: | |
189 | case NIS_S_SUCCESS: | |
190 | case NIS_LINKNAMEERROR: /* We follow to max links */ | |
191 | case NIS_UNAVAIL: /* NIS+ is not installed, or all servers are down */ | |
3996f34b UD |
192 | ++done; |
193 | break; | |
2d7da676 UD |
194 | default: |
195 | /* Try the next domainname if we don't follow a link */ | |
196 | if (count_links) | |
51702635 | 197 | { |
2d7da676 | 198 | free (req.ns_name); |
91eee4dd | 199 | NIS_RES_STATUS (res) = NIS_LINKNAMEERROR; |
2d7da676 UD |
200 | ++done; |
201 | break; | |
51702635 | 202 | } |
2d7da676 UD |
203 | ++name_nr; |
204 | if (names[name_nr] == NULL) | |
205 | { | |
206 | ++done; | |
207 | break; | |
208 | } | |
209 | req.ns_name = names[name_nr]; | |
210 | break; | |
51702635 UD |
211 | } |
212 | } | |
213 | ||
51e59260 | 214 | out: |
2d7da676 UD |
215 | if (names != namebuf) |
216 | nis_freenames (names); | |
217 | ||
51702635 UD |
218 | return res; |
219 | } | |
7440c23e | 220 | libnsl_hidden_def (nis_lookup) |