]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 1996-2020 Free Software Foundation, Inc. |
6259ec0d UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
6259ec0d UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
6259ec0d | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
6259ec0d UD |
18 | |
19 | #include <nss.h> | |
20 | #include <netdb.h> | |
21 | #include <ctype.h> | |
22 | #include <errno.h> | |
9b48fa9b | 23 | #include <stdint.h> |
6259ec0d UD |
24 | #include <string.h> |
25 | #include <netinet/in.h> | |
26 | #include <arpa/inet.h> | |
ec999b8e | 27 | #include <libc-lock.h> |
6259ec0d UD |
28 | #include <rpcsvc/yp.h> |
29 | #include <rpcsvc/ypclnt.h> | |
30 | ||
31 | #include "nss-nis.h" | |
32 | ||
7e3be507 UD |
33 | /* Get the declaration of the parser function. */ |
34 | #define ENTNAME netent | |
35 | #define EXTERN_PARSER | |
cf29ffbe | 36 | #include <nss/nss_files/files-parse.c> |
7e3be507 | 37 | |
6259ec0d UD |
38 | __libc_lock_define_initialized (static, lock) |
39 | ||
40 | static bool_t new_start = 1; | |
fc9f33e3 UD |
41 | static char *oldkey; |
42 | static int oldkeylen; | |
6259ec0d UD |
43 | |
44 | enum nss_status | |
51eecc4a | 45 | _nss_nis_setnetent (int stayopen) |
6259ec0d | 46 | { |
d423e170 FW |
47 | return _nss_nis_endnetent (); |
48 | } | |
49 | ||
50 | enum nss_status | |
51 | _nss_nis_endnetent (void) | |
52 | { | |
53 | __libc_lock_lock (lock); | |
6259ec0d UD |
54 | |
55 | new_start = 1; | |
56 | if (oldkey != NULL) | |
57 | { | |
58 | free (oldkey); | |
59 | oldkey = NULL; | |
60 | oldkeylen = 0; | |
61 | } | |
62 | ||
63 | __libc_lock_unlock (lock); | |
64 | ||
65 | return NSS_STATUS_SUCCESS; | |
66 | } | |
d423e170 | 67 | libnss_nis_hidden_def (_nss_nis_endnetent) |
6259ec0d UD |
68 | |
69 | static enum nss_status | |
70 | internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen, | |
d71b808a | 71 | int *errnop, int *herrnop) |
6259ec0d | 72 | { |
7e3be507 | 73 | struct parser_data *data = (void *) buffer; |
6259ec0d | 74 | |
ab9a9ff8 | 75 | char *domain; |
a1ffb40e | 76 | if (__glibc_unlikely (yp_get_default_domain (&domain))) |
6259ec0d UD |
77 | return NSS_STATUS_UNAVAIL; |
78 | ||
79 | /* Get the next entry until we found a correct one. */ | |
ab9a9ff8 | 80 | int parse_res; |
6259ec0d UD |
81 | do |
82 | { | |
ab9a9ff8 UD |
83 | char *result; |
84 | char *outkey; | |
85 | int len; | |
86 | int keylen; | |
87 | int yperr; | |
6259ec0d UD |
88 | |
89 | if (new_start) | |
ab9a9ff8 UD |
90 | yperr = yp_first (domain, "networks.byname", &outkey, &keylen, &result, |
91 | &len); | |
6259ec0d | 92 | else |
ab9a9ff8 UD |
93 | yperr = yp_next (domain, "networks.byname", oldkey, oldkeylen, &outkey, |
94 | &keylen, &result, &len); | |
6259ec0d | 95 | |
a1ffb40e | 96 | if (__glibc_unlikely (yperr != YPERR_SUCCESS)) |
6259ec0d | 97 | { |
ab9a9ff8 UD |
98 | enum nss_status retval = yperr2nss (yperr); |
99 | ||
34816665 | 100 | if (retval == NSS_STATUS_TRYAGAIN) |
6259ec0d UD |
101 | { |
102 | *herrnop = NETDB_INTERNAL; | |
d71b808a | 103 | *errnop = errno; |
6259ec0d UD |
104 | } |
105 | return retval; | |
106 | } | |
107 | ||
a1ffb40e | 108 | if (__glibc_unlikely ((size_t) (len + 1) > buflen)) |
6259ec0d UD |
109 | { |
110 | free (result); | |
ac9f45cf | 111 | *errnop = ERANGE; |
6259ec0d UD |
112 | *herrnop = NETDB_INTERNAL; |
113 | return NSS_STATUS_TRYAGAIN; | |
114 | } | |
115 | ||
ab9a9ff8 | 116 | char *p = strncpy (buffer, result, len); |
6259ec0d UD |
117 | buffer[len] = '\0'; |
118 | while (isspace (*p)) | |
119 | ++p; | |
120 | free (result); | |
121 | ||
d71b808a | 122 | parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop); |
a1ffb40e | 123 | if (__glibc_unlikely (parse_res == -1)) |
6259ec0d | 124 | { |
60c96635 | 125 | free (outkey); |
6259ec0d | 126 | *herrnop = NETDB_INTERNAL; |
d71b808a | 127 | *errnop = ERANGE; |
6259ec0d UD |
128 | return NSS_STATUS_TRYAGAIN; |
129 | } | |
130 | ||
131 | free (oldkey); | |
132 | oldkey = outkey; | |
133 | oldkeylen = keylen; | |
134 | new_start = 0; | |
135 | } | |
136 | while (!parse_res); | |
137 | ||
138 | return NSS_STATUS_SUCCESS; | |
139 | } | |
140 | ||
141 | enum nss_status | |
142 | _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen, | |
d71b808a | 143 | int *errnop, int *herrnop) |
6259ec0d UD |
144 | { |
145 | enum nss_status status; | |
146 | ||
147 | __libc_lock_lock (lock); | |
148 | ||
d71b808a | 149 | status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop); |
6259ec0d UD |
150 | |
151 | __libc_lock_unlock (lock); | |
152 | ||
153 | return status; | |
154 | } | |
155 | ||
156 | enum nss_status | |
d71b808a UD |
157 | _nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer, |
158 | size_t buflen, int *errnop, int *herrnop) | |
6259ec0d | 159 | { |
6259ec0d UD |
160 | if (name == NULL) |
161 | { | |
ac9f45cf | 162 | *errnop = EINVAL; |
6259ec0d UD |
163 | *herrnop = NETDB_INTERNAL; |
164 | return NSS_STATUS_UNAVAIL; | |
165 | } | |
166 | ||
ab9a9ff8 | 167 | char *domain; |
a1ffb40e | 168 | if (__glibc_unlikely (yp_get_default_domain (&domain))) |
6259ec0d UD |
169 | return NSS_STATUS_UNAVAIL; |
170 | ||
ab9a9ff8 | 171 | struct parser_data *data = (void *) buffer; |
b112c02f UD |
172 | if (buflen < sizeof *data + 1) |
173 | { | |
174 | *herrnop = NETDB_INTERNAL; | |
175 | *errnop = ERANGE; | |
176 | return NSS_STATUS_TRYAGAIN; | |
177 | } | |
b112c02f | 178 | |
ab9a9ff8 UD |
179 | /* Convert name to lowercase. */ |
180 | size_t namlen = strlen (name); | |
315eb1d8 AS |
181 | /* Limit name length to the maximum size of an RPC packet. */ |
182 | if (namlen > UDPMSGSIZE) | |
183 | { | |
184 | *errnop = ERANGE; | |
185 | return NSS_STATUS_UNAVAIL; | |
186 | } | |
187 | ||
ab9a9ff8 UD |
188 | char name2[namlen + 1]; |
189 | size_t i; | |
b112c02f | 190 | |
ab9a9ff8 UD |
191 | for (i = 0; i < namlen; ++i) |
192 | name2[i] = _tolower (name[i]); | |
193 | name2[i] = '\0'; | |
b112c02f | 194 | |
ab9a9ff8 UD |
195 | char *result; |
196 | int len; | |
197 | int yperr = yp_match (domain, "networks.byname", name2, namlen, &result, | |
198 | &len); | |
6259ec0d | 199 | |
a1ffb40e | 200 | if (__glibc_unlikely (yperr != YPERR_SUCCESS)) |
6259ec0d | 201 | { |
ab9a9ff8 UD |
202 | enum nss_status retval = yperr2nss (yperr); |
203 | ||
34816665 | 204 | if (retval == NSS_STATUS_TRYAGAIN) |
6259ec0d | 205 | { |
d71b808a | 206 | *errnop = errno; |
6259ec0d UD |
207 | *herrnop = NETDB_INTERNAL; |
208 | } | |
209 | return retval; | |
210 | } | |
211 | ||
a1ffb40e | 212 | if (__glibc_unlikely ((size_t) (len + 1) > buflen)) |
6259ec0d UD |
213 | { |
214 | free (result); | |
d71b808a | 215 | *errnop = ERANGE; |
6259ec0d UD |
216 | *herrnop = NETDB_INTERNAL; |
217 | return NSS_STATUS_TRYAGAIN; | |
218 | } | |
219 | ||
ab9a9ff8 | 220 | char *p = strncpy (buffer, result, len); |
6259ec0d UD |
221 | buffer[len] = '\0'; |
222 | while (isspace (*p)) | |
223 | ++p; | |
224 | free (result); | |
225 | ||
ab9a9ff8 | 226 | int parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop); |
6259ec0d | 227 | |
a1ffb40e | 228 | if (__glibc_unlikely (parse_res < 1)) |
6259ec0d UD |
229 | { |
230 | *herrnop = NETDB_INTERNAL; | |
60c96635 | 231 | if (parse_res == -1) |
6259ec0d UD |
232 | return NSS_STATUS_TRYAGAIN; |
233 | else | |
34816665 | 234 | return NSS_STATUS_NOTFOUND; |
6259ec0d UD |
235 | } |
236 | else | |
237 | return NSS_STATUS_SUCCESS; | |
238 | } | |
239 | ||
240 | enum nss_status | |
9b48fa9b | 241 | _nss_nis_getnetbyaddr_r (uint32_t addr, int type, struct netent *net, |
d71b808a UD |
242 | char *buffer, size_t buflen, int *errnop, |
243 | int *herrnop) | |
6259ec0d UD |
244 | { |
245 | char *domain; | |
a1ffb40e | 246 | if (__glibc_unlikely (yp_get_default_domain (&domain))) |
6259ec0d UD |
247 | return NSS_STATUS_UNAVAIL; |
248 | ||
2fd0cd8b | 249 | struct in_addr in = { .s_addr = htonl (addr) }; |
ab9a9ff8 UD |
250 | char *buf = inet_ntoa (in); |
251 | size_t blen = strlen (buf); | |
6259ec0d UD |
252 | |
253 | while (1) | |
254 | { | |
ab9a9ff8 UD |
255 | char *result; |
256 | int len; | |
6259ec0d | 257 | |
ab9a9ff8 UD |
258 | int yperr = yp_match (domain, "networks.byaddr", buf, blen, &result, |
259 | &len); | |
6259ec0d | 260 | |
a1ffb40e | 261 | if (__glibc_unlikely (yperr != YPERR_SUCCESS)) |
6259ec0d | 262 | { |
ab9a9ff8 UD |
263 | enum nss_status retval = yperr2nss (yperr); |
264 | ||
6259ec0d UD |
265 | if (retval == NSS_STATUS_NOTFOUND) |
266 | { | |
267 | if (buf[blen - 2] == '.' && buf[blen - 1] == '0') | |
268 | { | |
269 | /* Try again, but with trailing dot(s) | |
270 | removed (one by one) */ | |
271 | buf[blen - 2] = '\0'; | |
272 | blen -= 2; | |
273 | continue; | |
274 | } | |
275 | else | |
34816665 | 276 | return NSS_STATUS_NOTFOUND; |
6259ec0d UD |
277 | } |
278 | else | |
279 | { | |
280 | if (retval == NSS_STATUS_TRYAGAIN) | |
d71b808a | 281 | *errnop = errno; |
6259ec0d UD |
282 | return retval; |
283 | } | |
284 | } | |
285 | ||
a1ffb40e | 286 | if (__glibc_unlikely ((size_t) (len + 1) > buflen)) |
6259ec0d UD |
287 | { |
288 | free (result); | |
d71b808a | 289 | *errnop = ERANGE; |
6259ec0d UD |
290 | *herrnop = NETDB_INTERNAL; |
291 | return NSS_STATUS_TRYAGAIN; | |
292 | } | |
293 | ||
ab9a9ff8 | 294 | char *p = strncpy (buffer, result, len); |
6259ec0d UD |
295 | buffer[len] = '\0'; |
296 | while (isspace (*p)) | |
297 | ++p; | |
298 | free (result); | |
299 | ||
ab9a9ff8 UD |
300 | int parse_res = _nss_files_parse_netent (p, net, (void *) buffer, |
301 | buflen, errnop); | |
6259ec0d | 302 | |
a1ffb40e | 303 | if (__glibc_unlikely (parse_res < 1)) |
6259ec0d UD |
304 | { |
305 | *herrnop = NETDB_INTERNAL; | |
60c96635 | 306 | if (parse_res == -1) |
6259ec0d UD |
307 | return NSS_STATUS_TRYAGAIN; |
308 | else | |
34816665 | 309 | return NSS_STATUS_NOTFOUND; |
6259ec0d UD |
310 | } |
311 | else | |
312 | return NSS_STATUS_SUCCESS; | |
313 | } | |
314 | } |