]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 1997-2024 Free Software Foundation, Inc. |
61c162b5 | 2 | This file is part of the GNU C Library. |
61c162b5 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. | |
61c162b5 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. |
61c162b5 | 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/>. */ |
61c162b5 | 17 | |
a5fdf99b UD |
18 | #include <assert.h> |
19 | #include <errno.h> | |
20 | #include <string.h> | |
21 | #include <stdlib.h> | |
22 | #include <ctype.h> | |
23 | #include <wctype.h> | |
b76e0659 | 24 | #include <resolv/resolv-internal.h> |
352f4ff9 | 25 | #include <resolv/resolv_context.h> |
a5fdf99b UD |
26 | #include <netdb.h> |
27 | #include <arpa/inet.h> | |
28 | #include "nsswitch.h" | |
29 | ||
4aebaa6b | 30 | #ifdef USE_NSCD |
a5fdf99b | 31 | # include <nscd/nscd_proto.h> |
4aebaa6b UD |
32 | #endif |
33 | ||
a5fdf99b UD |
34 | int |
35 | __nss_hostname_digits_dots (const char *name, struct hostent *resbuf, | |
36 | char **buffer, size_t *buffer_size, | |
37 | size_t buflen, struct hostent **result, | |
1773d1ba | 38 | enum nss_status *status, int af, int *h_errnop) |
a5fdf99b | 39 | { |
a5fdf99b UD |
40 | /* We have to test for the use of IPv6 which can only be done by |
41 | examining `_res'. */ | |
352f4ff9 FW |
42 | struct resolv_context *ctx = __resolv_context_get (); |
43 | if (ctx == NULL) | |
a5fdf99b UD |
44 | { |
45 | if (h_errnop) | |
46 | *h_errnop = NETDB_INTERNAL; | |
d5dd6189 AS |
47 | if (buffer_size == NULL) |
48 | *status = NSS_STATUS_TRYAGAIN; | |
49 | else | |
50 | *result = NULL; | |
a5fdf99b UD |
51 | return -1; |
52 | } | |
352f4ff9 FW |
53 | int ret = __nss_hostname_digits_dots_context |
54 | (ctx, name, resbuf, buffer, buffer_size, buflen, | |
55 | result, status, af, h_errnop); | |
56 | __resolv_context_put (ctx); | |
57 | return ret; | |
58 | } | |
59 | ||
60 | int | |
61 | __nss_hostname_digits_dots_context (struct resolv_context *ctx, | |
62 | const char *name, struct hostent *resbuf, | |
63 | char **buffer, size_t *buffer_size, | |
64 | size_t buflen, struct hostent **result, | |
65 | enum nss_status *status, int af, int *h_errnop) | |
66 | { | |
67 | int save; | |
a5fdf99b | 68 | |
61c162b5 UD |
69 | /* |
70 | * disallow names consisting only of digits/dots, unless | |
71 | * they end in a dot. | |
72 | */ | |
a5fdf99b | 73 | if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':') |
61c162b5 UD |
74 | { |
75 | const char *cp; | |
76 | char *hostname; | |
8337f053 | 77 | typedef unsigned char host_addr_t[16]; |
61c162b5 | 78 | host_addr_t *host_addr; |
8337f053 | 79 | typedef char *host_addr_list_t[2]; |
61c162b5 | 80 | host_addr_list_t *h_addr_ptrs; |
8337f053 | 81 | char **h_alias_ptr; |
61c162b5 UD |
82 | size_t size_needed; |
83 | int addr_size; | |
61c162b5 UD |
84 | |
85 | switch (af) | |
86 | { | |
87 | case AF_INET: | |
88 | addr_size = INADDRSZ; | |
89 | break; | |
90 | ||
91 | case AF_INET6: | |
92 | addr_size = IN6ADDRSZ; | |
93 | break; | |
94 | ||
95 | default: | |
b76e0659 | 96 | af = res_use_inet6 () ? AF_INET6 : AF_INET; |
1773d1ba | 97 | addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ; |
61c162b5 UD |
98 | break; |
99 | } | |
100 | ||
dfd2257a | 101 | size_needed = (sizeof (*host_addr) |
d5dd6189 AS |
102 | + sizeof (*h_addr_ptrs) |
103 | + sizeof (*h_alias_ptr) + strlen (name) + 1); | |
61c162b5 | 104 | |
a5fdf99b UD |
105 | if (buffer_size == NULL) |
106 | { | |
107 | if (buflen < size_needed) | |
108 | { | |
d5dd6189 | 109 | *status = NSS_STATUS_TRYAGAIN; |
a5fdf99b | 110 | if (h_errnop != NULL) |
d5dd6189 | 111 | *h_errnop = NETDB_INTERNAL; |
a5fdf99b UD |
112 | __set_errno (ERANGE); |
113 | goto done; | |
114 | } | |
61c162b5 | 115 | } |
a5fdf99b | 116 | else if (buffer_size != NULL && *buffer_size < size_needed) |
61c162b5 UD |
117 | { |
118 | char *new_buf; | |
a5fdf99b UD |
119 | *buffer_size = size_needed; |
120 | new_buf = (char *) realloc (*buffer, *buffer_size); | |
61c162b5 UD |
121 | |
122 | if (new_buf == NULL) | |
123 | { | |
124 | save = errno; | |
a5fdf99b UD |
125 | free (*buffer); |
126 | *buffer = NULL; | |
127 | *buffer_size = 0; | |
61c162b5 | 128 | __set_errno (save); |
a5fdf99b | 129 | if (h_errnop != NULL) |
d5dd6189 | 130 | *h_errnop = NETDB_INTERNAL; |
a5fdf99b | 131 | *result = NULL; |
61c162b5 UD |
132 | goto done; |
133 | } | |
a5fdf99b | 134 | *buffer = new_buf; |
61c162b5 | 135 | } |
61c162b5 | 136 | |
a5fdf99b | 137 | memset (*buffer, '\0', size_needed); |
61c162b5 | 138 | |
a5fdf99b | 139 | host_addr = (host_addr_t *) *buffer; |
61c162b5 | 140 | h_addr_ptrs = (host_addr_list_t *) |
a553168f | 141 | ((char *) host_addr + sizeof (*host_addr)); |
8337f053 UD |
142 | h_alias_ptr = (char **) ((char *) h_addr_ptrs + sizeof (*h_addr_ptrs)); |
143 | hostname = (char *) h_alias_ptr + sizeof (*h_alias_ptr); | |
61c162b5 UD |
144 | |
145 | if (isdigit (name[0])) | |
146 | { | |
147 | for (cp = name;; ++cp) | |
148 | { | |
a5fdf99b | 149 | if (*cp == '\0') |
61c162b5 | 150 | { |
607c351a | 151 | int ok; |
16b0f634 UD |
152 | |
153 | if (*--cp == '.') | |
154 | break; | |
61c162b5 | 155 | |
6df34c4b UD |
156 | /* All-numeric, no dot at the end. Fake up a hostent as if |
157 | we'd actually done a lookup. What if someone types | |
158 | 255.255.255.255? The test below will succeed | |
159 | spuriously... ??? */ | |
160 | if (af == AF_INET) | |
108bc404 | 161 | ok = __inet_aton_exact (name, (struct in_addr *) host_addr); |
6df34c4b | 162 | else |
16b0f634 | 163 | { |
6df34c4b | 164 | assert (af == AF_INET6); |
a5fdf99b | 165 | ok = inet_pton (af, name, host_addr) > 0; |
16b0f634 | 166 | } |
607c351a | 167 | if (! ok) |
61c162b5 | 168 | { |
a553168f | 169 | *h_errnop = HOST_NOT_FOUND; |
d5dd6189 AS |
170 | if (buffer_size == NULL) |
171 | *status = NSS_STATUS_NOTFOUND; | |
172 | else | |
a5fdf99b | 173 | *result = NULL; |
61c162b5 UD |
174 | goto done; |
175 | } | |
176 | ||
a5fdf99b | 177 | resbuf->h_name = strcpy (hostname, name); |
8337f053 | 178 | h_alias_ptr[0] = NULL; |
a5fdf99b UD |
179 | resbuf->h_aliases = h_alias_ptr; |
180 | (*h_addr_ptrs)[0] = (char *) host_addr; | |
181 | (*h_addr_ptrs)[1] = NULL; | |
182 | resbuf->h_addr_list = *h_addr_ptrs; | |
b76e0659 | 183 | if (af == AF_INET && res_use_inet6 ()) |
61c162b5 | 184 | { |
1773d1ba UD |
185 | /* We need to change the IP v4 address into the |
186 | IP v6 address. */ | |
187 | char tmp[INADDRSZ]; | |
188 | char *p = (char *) host_addr; | |
189 | int i; | |
190 | ||
191 | /* Save a copy of the IP v4 address. */ | |
192 | memcpy (tmp, host_addr, INADDRSZ); | |
193 | /* Mark this ipv6 addr as a mapped ipv4. */ | |
194 | for (i = 0; i < 10; i++) | |
195 | *p++ = 0x00; | |
196 | *p++ = 0xff; | |
197 | *p++ = 0xff; | |
198 | /* Copy the IP v4 address. */ | |
199 | memcpy (p, tmp, INADDRSZ); | |
200 | resbuf->h_addrtype = AF_INET6; | |
201 | resbuf->h_length = IN6ADDRSZ; | |
61c162b5 UD |
202 | } |
203 | else | |
204 | { | |
a5fdf99b UD |
205 | resbuf->h_addrtype = af; |
206 | resbuf->h_length = addr_size; | |
61c162b5 | 207 | } |
a5fdf99b UD |
208 | if (h_errnop != NULL) |
209 | *h_errnop = NETDB_SUCCESS; | |
210 | if (buffer_size == NULL) | |
211 | *status = NSS_STATUS_SUCCESS; | |
212 | else | |
d5dd6189 | 213 | *result = resbuf; |
61c162b5 UD |
214 | goto done; |
215 | } | |
216 | ||
a5fdf99b UD |
217 | if (!isdigit (*cp) && *cp != '.') |
218 | break; | |
61c162b5 UD |
219 | } |
220 | } | |
221 | ||
a5fdf99b | 222 | if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':') |
61c162b5 | 223 | { |
61c162b5 UD |
224 | switch (af) |
225 | { | |
a553168f | 226 | default: |
b76e0659 | 227 | af = res_use_inet6 () ? AF_INET6 : AF_INET; |
a553168f UD |
228 | if (af == AF_INET6) |
229 | { | |
230 | addr_size = IN6ADDRSZ; | |
231 | break; | |
232 | } | |
233 | /* FALLTHROUGH */ | |
234 | ||
61c162b5 | 235 | case AF_INET: |
a553168f UD |
236 | /* This is not possible. We cannot represent an IPv6 address |
237 | in an `struct in_addr' variable. */ | |
238 | *h_errnop = HOST_NOT_FOUND; | |
d5dd6189 AS |
239 | if (buffer_size == NULL) |
240 | *status = NSS_STATUS_NOTFOUND; | |
241 | else | |
242 | *result = NULL; | |
a553168f | 243 | goto done; |
61c162b5 UD |
244 | |
245 | case AF_INET6: | |
246 | addr_size = IN6ADDRSZ; | |
247 | break; | |
61c162b5 UD |
248 | } |
249 | ||
61c162b5 UD |
250 | for (cp = name;; ++cp) |
251 | { | |
252 | if (!*cp) | |
253 | { | |
1fb05e3d UD |
254 | if (*--cp == '.') |
255 | break; | |
61c162b5 UD |
256 | |
257 | /* All-IPv6-legal, no dot at the end. Fake up a | |
258 | hostent as if we'd actually done a lookup. */ | |
a553168f | 259 | if (inet_pton (AF_INET6, name, host_addr) <= 0) |
61c162b5 | 260 | { |
a553168f | 261 | *h_errnop = HOST_NOT_FOUND; |
d5dd6189 AS |
262 | if (buffer_size == NULL) |
263 | *status = NSS_STATUS_NOTFOUND; | |
264 | else | |
a5fdf99b | 265 | *result = NULL; |
61c162b5 UD |
266 | goto done; |
267 | } | |
268 | ||
a5fdf99b | 269 | resbuf->h_name = strcpy (hostname, name); |
8337f053 | 270 | h_alias_ptr[0] = NULL; |
a5fdf99b | 271 | resbuf->h_aliases = h_alias_ptr; |
61c162b5 UD |
272 | (*h_addr_ptrs)[0] = (char *) host_addr; |
273 | (*h_addr_ptrs)[1] = (char *) 0; | |
a5fdf99b UD |
274 | resbuf->h_addr_list = *h_addr_ptrs; |
275 | resbuf->h_addrtype = AF_INET6; | |
276 | resbuf->h_length = addr_size; | |
a553168f | 277 | *h_errnop = NETDB_SUCCESS; |
a5fdf99b UD |
278 | if (buffer_size == NULL) |
279 | *status = NSS_STATUS_SUCCESS; | |
280 | else | |
281 | *result = resbuf; | |
61c162b5 UD |
282 | goto done; |
283 | } | |
284 | ||
a5fdf99b UD |
285 | if (!isxdigit (*cp) && *cp != ':' && *cp != '.') |
286 | break; | |
61c162b5 UD |
287 | } |
288 | } | |
289 | } | |
a5fdf99b UD |
290 | |
291 | return 0; | |
292 | ||
293 | done: | |
294 | return 1; | |
295 | } | |
5656e294 | 296 | libc_hidden_def (__nss_hostname_digits_dots) |