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