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