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