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