1 /* Copyright (C) 1997-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
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.
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
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <assert.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <wctype.h>
24 #include <resolv/resolv-internal.h>
25 #include <resolv/resolv_context.h>
26 #include <netdb.h>
27 #include <arpa/inet.h>
28 #include "nsswitch.h"
29 
30 #ifdef USE_NSCD
31 # include <nscd/nscd_proto.h>
32 #endif
33 
34 int
__nss_hostname_digits_dots(const char * name,struct hostent * resbuf,char ** buffer,size_t * buffer_size,size_t buflen,struct hostent ** result,enum nss_status * status,int af,int * h_errnop)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,
38 			    enum nss_status *status, int af, int *h_errnop)
39 {
40   /* We have to test for the use of IPv6 which can only be done by
41      examining `_res'.  */
42   struct resolv_context *ctx = __resolv_context_get ();
43   if (ctx == NULL)
44     {
45       if (h_errnop)
46 	*h_errnop = NETDB_INTERNAL;
47       if (buffer_size == NULL)
48 	*status = NSS_STATUS_TRYAGAIN;
49       else
50 	*result = NULL;
51       return -1;
52     }
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
__nss_hostname_digits_dots_context(struct resolv_context * ctx,const char * name,struct hostent * resbuf,char ** buffer,size_t * buffer_size,size_t buflen,struct hostent ** result,enum nss_status * status,int af,int * h_errnop)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;
68 
69   /*
70    * disallow names consisting only of digits/dots, unless
71    * they end in a dot.
72    */
73   if (isdigit (name[0]) || isxdigit (name[0]) || name[0] == ':')
74     {
75       const char *cp;
76       char *hostname;
77       typedef unsigned char host_addr_t[16];
78       host_addr_t *host_addr;
79       typedef char *host_addr_list_t[2];
80       host_addr_list_t *h_addr_ptrs;
81       char **h_alias_ptr;
82       size_t size_needed;
83       int addr_size;
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:
96 	  af = res_use_inet6 () ? AF_INET6 : AF_INET;
97 	  addr_size = af == AF_INET6 ? IN6ADDRSZ : INADDRSZ;
98 	  break;
99 	}
100 
101       size_needed = (sizeof (*host_addr)
102 		     + sizeof (*h_addr_ptrs)
103 		     + sizeof (*h_alias_ptr) + strlen (name) + 1);
104 
105       if (buffer_size == NULL)
106         {
107 	  if (buflen < size_needed)
108 	    {
109 	      *status = NSS_STATUS_TRYAGAIN;
110 	      if (h_errnop != NULL)
111 		*h_errnop = NETDB_INTERNAL;
112 	      __set_errno (ERANGE);
113 	      goto done;
114 	    }
115 	}
116       else if (buffer_size != NULL && *buffer_size < size_needed)
117 	{
118 	  char *new_buf;
119 	  *buffer_size = size_needed;
120 	  new_buf = (char *) realloc (*buffer, *buffer_size);
121 
122 	  if (new_buf == NULL)
123 	    {
124 	      save = errno;
125 	      free (*buffer);
126 	      *buffer = NULL;
127 	      *buffer_size = 0;
128 	      __set_errno (save);
129 	      if (h_errnop != NULL)
130 		*h_errnop = NETDB_INTERNAL;
131 	      *result = NULL;
132 	      goto done;
133 	    }
134 	  *buffer = new_buf;
135 	}
136 
137       memset (*buffer, '\0', size_needed);
138 
139       host_addr = (host_addr_t *) *buffer;
140       h_addr_ptrs = (host_addr_list_t *)
141 	((char *) host_addr + sizeof (*host_addr));
142       h_alias_ptr = (char **) ((char *) h_addr_ptrs + sizeof (*h_addr_ptrs));
143       hostname = (char *) h_alias_ptr + sizeof (*h_alias_ptr);
144 
145       if (isdigit (name[0]))
146 	{
147 	  for (cp = name;; ++cp)
148 	    {
149 	      if (*cp == '\0')
150 		{
151 		  int ok;
152 
153 		  if (*--cp == '.')
154 		    break;
155 
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)
161 		    ok = __inet_aton_exact (name, (struct in_addr *) host_addr);
162 		  else
163 		    {
164 		      assert (af == AF_INET6);
165 		      ok = inet_pton (af, name, host_addr) > 0;
166 		    }
167 		  if (! ok)
168 		    {
169 		      *h_errnop = HOST_NOT_FOUND;
170 		      if (buffer_size == NULL)
171 			*status = NSS_STATUS_NOTFOUND;
172 		      else
173 			*result = NULL;
174 		      goto done;
175 		    }
176 
177 		  resbuf->h_name = strcpy (hostname, name);
178 		  h_alias_ptr[0] = NULL;
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;
183 		  if (af == AF_INET && res_use_inet6 ())
184 		    {
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;
202 		    }
203 		  else
204 		    {
205 		      resbuf->h_addrtype = af;
206 		      resbuf->h_length = addr_size;
207 		    }
208 		  if (h_errnop != NULL)
209 		    *h_errnop = NETDB_SUCCESS;
210 		  if (buffer_size == NULL)
211 		    *status = NSS_STATUS_SUCCESS;
212 		  else
213 		    *result = resbuf;
214 		  goto done;
215 		}
216 
217 	      if (!isdigit (*cp) && *cp != '.')
218 		break;
219 	    }
220 	}
221 
222       if ((isxdigit (name[0]) && strchr (name, ':') != NULL) || name[0] == ':')
223 	{
224 	  switch (af)
225 	    {
226 	    default:
227 	      af = res_use_inet6 () ? AF_INET6 : AF_INET;
228 	      if (af == AF_INET6)
229 		{
230 		  addr_size = IN6ADDRSZ;
231 		  break;
232 		}
233 	      /* FALLTHROUGH */
234 
235 	    case AF_INET:
236 	      /* This is not possible.  We cannot represent an IPv6 address
237 		 in an `struct in_addr' variable.  */
238 	      *h_errnop = HOST_NOT_FOUND;
239 	      if (buffer_size == NULL)
240 		*status = NSS_STATUS_NOTFOUND;
241 	      else
242 		*result = NULL;
243 	      goto done;
244 
245 	    case AF_INET6:
246 	      addr_size = IN6ADDRSZ;
247 	      break;
248 	    }
249 
250 	  for (cp = name;; ++cp)
251 	    {
252 	      if (!*cp)
253 		{
254 		  if (*--cp == '.')
255 		    break;
256 
257 		  /* All-IPv6-legal, no dot at the end. Fake up a
258 		     hostent as if we'd actually done a lookup.  */
259 		  if (inet_pton (AF_INET6, name, host_addr) <= 0)
260 		    {
261 		      *h_errnop = HOST_NOT_FOUND;
262 		      if (buffer_size == NULL)
263 			*status = NSS_STATUS_NOTFOUND;
264 		      else
265 			*result = NULL;
266 		      goto done;
267 		    }
268 
269 		  resbuf->h_name = strcpy (hostname, name);
270 		  h_alias_ptr[0] = NULL;
271 		  resbuf->h_aliases = h_alias_ptr;
272 		  (*h_addr_ptrs)[0] = (char *) host_addr;
273 		  (*h_addr_ptrs)[1] = (char *) 0;
274 		  resbuf->h_addr_list = *h_addr_ptrs;
275 		  resbuf->h_addrtype = AF_INET6;
276 		  resbuf->h_length = addr_size;
277 		  *h_errnop = NETDB_SUCCESS;
278 		  if (buffer_size == NULL)
279 		    *status = NSS_STATUS_SUCCESS;
280 		  else
281 		    *result = resbuf;
282 		  goto done;
283 		}
284 
285 	      if (!isxdigit (*cp) && *cp != ':' && *cp != '.')
286 		break;
287 	    }
288 	}
289     }
290 
291   return 0;
292 
293 done:
294   return 1;
295 }
296 libc_hidden_def (__nss_hostname_digits_dots)
297