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