]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/digits_dots.c
[powerpc] No need to enter "Ignore Exceptions Mode"
[thirdparty/glibc.git] / nss / digits_dots.c
CommitLineData
04277e02 1/* Copyright (C) 1997-2019 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 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://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 31#ifdef USE_NSCD
a5fdf99b 32# include <nscd/nscd_proto.h>
4aebaa6b
UD
33#endif
34
a5fdf99b
UD
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,
1773d1ba 39 enum nss_status *status, int af, int *h_errnop)
a5fdf99b 40{
a5fdf99b
UD
41 /* We have to test for the use of IPv6 which can only be done by
42 examining `_res'. */
352f4ff9
FW
43 struct resolv_context *ctx = __resolv_context_get ();
44 if (ctx == NULL)
a5fdf99b
UD
45 {
46 if (h_errnop)
47 *h_errnop = NETDB_INTERNAL;
d5dd6189
AS
48 if (buffer_size == NULL)
49 *status = NSS_STATUS_TRYAGAIN;
50 else
51 *result = NULL;
a5fdf99b
UD
52 return -1;
53 }
352f4ff9
FW
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;
a5fdf99b 69
61c162b5
UD
70 /*
71 * disallow names consisting only of digits/dots, unless
72 * they end in a dot.
73 */
a5fdf99b 74 if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':')
61c162b5
UD
75 {
76 const char *cp;
77 char *hostname;
8337f053 78 typedef unsigned char host_addr_t[16];
61c162b5 79 host_addr_t *host_addr;
8337f053 80 typedef char *host_addr_list_t[2];
61c162b5 81 host_addr_list_t *h_addr_ptrs;
8337f053 82 char **h_alias_ptr;
61c162b5
UD
83 size_t size_needed;
84 int addr_size;
61c162b5
UD
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:
b76e0659 97 af = res_use_inet6 () ? AF_INET6 : AF_INET;
1773d1ba 98 addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ;
61c162b5
UD
99 break;
100 }
101
dfd2257a 102 size_needed = (sizeof (*host_addr)
d5dd6189
AS
103 + sizeof (*h_addr_ptrs)
104 + sizeof (*h_alias_ptr) + strlen (name) + 1);
61c162b5 105
a5fdf99b
UD
106 if (buffer_size == NULL)
107 {
108 if (buflen < size_needed)
109 {
d5dd6189 110 *status = NSS_STATUS_TRYAGAIN;
a5fdf99b 111 if (h_errnop != NULL)
d5dd6189 112 *h_errnop = NETDB_INTERNAL;
a5fdf99b
UD
113 __set_errno (ERANGE);
114 goto done;
115 }
61c162b5 116 }
a5fdf99b 117 else if (buffer_size != NULL && *buffer_size < size_needed)
61c162b5
UD
118 {
119 char *new_buf;
a5fdf99b
UD
120 *buffer_size = size_needed;
121 new_buf = (char *) realloc (*buffer, *buffer_size);
61c162b5
UD
122
123 if (new_buf == NULL)
124 {
125 save = errno;
a5fdf99b
UD
126 free (*buffer);
127 *buffer = NULL;
128 *buffer_size = 0;
61c162b5 129 __set_errno (save);
a5fdf99b 130 if (h_errnop != NULL)
d5dd6189 131 *h_errnop = NETDB_INTERNAL;
a5fdf99b 132 *result = NULL;
61c162b5
UD
133 goto done;
134 }
a5fdf99b 135 *buffer = new_buf;
61c162b5 136 }
61c162b5 137
a5fdf99b 138 memset (*buffer, '\0', size_needed);
61c162b5 139
a5fdf99b 140 host_addr = (host_addr_t *) *buffer;
61c162b5 141 h_addr_ptrs = (host_addr_list_t *)
a553168f 142 ((char *) host_addr + sizeof (*host_addr));
8337f053
UD
143 h_alias_ptr = (char **) ((char *) h_addr_ptrs + sizeof (*h_addr_ptrs));
144 hostname = (char *) h_alias_ptr + sizeof (*h_alias_ptr);
61c162b5
UD
145
146 if (isdigit (name[0]))
147 {
148 for (cp = name;; ++cp)
149 {
a5fdf99b 150 if (*cp == '\0')
61c162b5 151 {
607c351a 152 int ok;
16b0f634
UD
153
154 if (*--cp == '.')
155 break;
61c162b5 156
6df34c4b
UD
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)
108bc404 162 ok = __inet_aton_exact (name, (struct in_addr *) host_addr);
6df34c4b 163 else
16b0f634 164 {
6df34c4b 165 assert (af == AF_INET6);
a5fdf99b 166 ok = inet_pton (af, name, host_addr) > 0;
16b0f634 167 }
607c351a 168 if (! ok)
61c162b5 169 {
a553168f 170 *h_errnop = HOST_NOT_FOUND;
d5dd6189
AS
171 if (buffer_size == NULL)
172 *status = NSS_STATUS_NOTFOUND;
173 else
a5fdf99b 174 *result = NULL;
61c162b5
UD
175 goto done;
176 }
177
a5fdf99b 178 resbuf->h_name = strcpy (hostname, name);
8337f053 179 h_alias_ptr[0] = NULL;
a5fdf99b
UD
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;
b76e0659 184 if (af == AF_INET && res_use_inet6 ())
61c162b5 185 {
1773d1ba
UD
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;
61c162b5
UD
203 }
204 else
205 {
a5fdf99b
UD
206 resbuf->h_addrtype = af;
207 resbuf->h_length = addr_size;
61c162b5 208 }
a5fdf99b
UD
209 if (h_errnop != NULL)
210 *h_errnop = NETDB_SUCCESS;
211 if (buffer_size == NULL)
212 *status = NSS_STATUS_SUCCESS;
213 else
d5dd6189 214 *result = resbuf;
61c162b5
UD
215 goto done;
216 }
217
a5fdf99b
UD
218 if (!isdigit (*cp) && *cp != '.')
219 break;
61c162b5
UD
220 }
221 }
222
a5fdf99b 223 if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
61c162b5 224 {
61c162b5
UD
225 switch (af)
226 {
a553168f 227 default:
b76e0659 228 af = res_use_inet6 () ? AF_INET6 : AF_INET;
a553168f
UD
229 if (af == AF_INET6)
230 {
231 addr_size = IN6ADDRSZ;
232 break;
233 }
234 /* FALLTHROUGH */
235
61c162b5 236 case AF_INET:
a553168f
UD
237 /* This is not possible. We cannot represent an IPv6 address
238 in an `struct in_addr' variable. */
239 *h_errnop = HOST_NOT_FOUND;
d5dd6189
AS
240 if (buffer_size == NULL)
241 *status = NSS_STATUS_NOTFOUND;
242 else
243 *result = NULL;
a553168f 244 goto done;
61c162b5
UD
245
246 case AF_INET6:
247 addr_size = IN6ADDRSZ;
248 break;
61c162b5
UD
249 }
250
61c162b5
UD
251 for (cp = name;; ++cp)
252 {
253 if (!*cp)
254 {
1fb05e3d
UD
255 if (*--cp == '.')
256 break;
61c162b5
UD
257
258 /* All-IPv6-legal, no dot at the end. Fake up a
259 hostent as if we'd actually done a lookup. */
a553168f 260 if (inet_pton (AF_INET6, name, host_addr) <= 0)
61c162b5 261 {
a553168f 262 *h_errnop = HOST_NOT_FOUND;
d5dd6189
AS
263 if (buffer_size == NULL)
264 *status = NSS_STATUS_NOTFOUND;
265 else
a5fdf99b 266 *result = NULL;
61c162b5
UD
267 goto done;
268 }
269
a5fdf99b 270 resbuf->h_name = strcpy (hostname, name);
8337f053 271 h_alias_ptr[0] = NULL;
a5fdf99b 272 resbuf->h_aliases = h_alias_ptr;
61c162b5
UD
273 (*h_addr_ptrs)[0] = (char *) host_addr;
274 (*h_addr_ptrs)[1] = (char *) 0;
a5fdf99b
UD
275 resbuf->h_addr_list = *h_addr_ptrs;
276 resbuf->h_addrtype = AF_INET6;
277 resbuf->h_length = addr_size;
a553168f 278 *h_errnop = NETDB_SUCCESS;
a5fdf99b
UD
279 if (buffer_size == NULL)
280 *status = NSS_STATUS_SUCCESS;
281 else
282 *result = resbuf;
61c162b5
UD
283 goto done;
284 }
285
a5fdf99b
UD
286 if (!isxdigit (*cp) && *cp != ':' && *cp != '.')
287 break;
61c162b5
UD
288 }
289 }
290 }
a5fdf99b
UD
291
292 return 0;
293
294done:
295 return 1;
296}
5656e294 297libc_hidden_def (__nss_hostname_digits_dots)