xref: /freebsd/lib/libutil/realhostname.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 
39 #include <stdio.h>
40 #include <string.h>
41 
42 #include "libutil.h"
43 
44 struct sockinet {
45 	u_char	si_len;
46 	u_char	si_family;
47 	u_short	si_port;
48 };
49 
50 int
51 realhostname(char *host, size_t hsize, const struct in_addr *ip)
52 {
53 	char trimmed[MAXHOSTNAMELEN];
54 	int result;
55 	struct hostent *hp;
56 
57 	result = HOSTNAME_INVALIDADDR;
58 	hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET);
59 
60 	if (hp != NULL) {
61 		strlcpy(trimmed, hp->h_name, sizeof(trimmed));
62 		trimdomain(trimmed, strlen(trimmed));
63 		if (strlen(trimmed) <= hsize) {
64 			char lookup[MAXHOSTNAMELEN];
65 
66 			strlcpy(lookup, hp->h_name, sizeof(lookup));
67 			hp = gethostbyname(lookup);
68 			if (hp == NULL)
69 				result = HOSTNAME_INVALIDNAME;
70 			else for (; ; hp->h_addr_list++) {
71 				if (*hp->h_addr_list == NULL) {
72 					result = HOSTNAME_INCORRECTNAME;
73 					break;
74 				}
75 				if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
76 					strncpy(host, trimmed, hsize);
77 					return HOSTNAME_FOUND;
78 				}
79 			}
80 		}
81 	}
82 
83 	strncpy(host, inet_ntoa(*ip), hsize);
84 
85 	return result;
86 }
87 
88 /*
89  * struct sockaddr has very lax alignment requirements, since all its
90  * members are char or equivalent.  This is a problem when trying to
91  * dereference a struct sockaddr_in6 * that was passed in as a struct
92  * sockaddr *.  Although we know (or trust) that the passed-in struct was
93  * properly aligned, the compiler doesn't, and (rightly) complains.  These
94  * macros perform the cast in a way that the compiler will accept.
95  */
96 #define SOCKADDR_IN6(p) ((struct sockaddr_in6 *)(void *)(p))
97 #define SOCKADDR_IN(p) ((struct sockaddr_in *)(void *)(p))
98 #define SOCKINET(p) ((struct sockinet *)(void *)(p))
99 
100 int
101 realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
102 {
103 	int result, error;
104 	char buf[NI_MAXHOST];
105 #ifdef INET6
106 	struct sockaddr_in lsin;
107 #endif
108 
109 	result = HOSTNAME_INVALIDADDR;
110 
111 #ifdef INET6
112 	/* IPv4 mapped IPv6 addr consideraton, specified in rfc2373. */
113 	if (addr->sa_family == AF_INET6 &&
114 	    addrlen == sizeof(struct sockaddr_in6) &&
115 	    IN6_IS_ADDR_V4MAPPED(&SOCKADDR_IN6(addr)->sin6_addr)) {
116 		struct sockaddr_in6 *sin6;
117 
118 		sin6 = SOCKADDR_IN6(addr);
119 
120 		memset(&lsin, 0, sizeof(lsin));
121 		lsin.sin_len = sizeof(struct sockaddr_in);
122 		lsin.sin_family = AF_INET;
123 		lsin.sin_port = sin6->sin6_port;
124 		memcpy(&lsin.sin_addr, &sin6->sin6_addr.s6_addr[12],
125 		       sizeof(struct in_addr));
126 		addr = (struct sockaddr *)&lsin;
127 		addrlen = lsin.sin_len;
128 	}
129 #endif
130 
131 	error = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
132 			    NI_NAMEREQD);
133 	if (error == 0) {
134 		struct addrinfo hints, *res, *ores;
135 		struct sockaddr *sa;
136 
137 		memset(&hints, 0, sizeof(struct addrinfo));
138 		hints.ai_family = addr->sa_family;
139 		hints.ai_flags = AI_CANONNAME | AI_PASSIVE;
140 		hints.ai_socktype = SOCK_STREAM;
141 
142 		error = getaddrinfo(buf, NULL, &hints, &res);
143 		if (error) {
144 			result = HOSTNAME_INVALIDNAME;
145 			goto numeric;
146 		}
147 		for (ores = res; ; res = res->ai_next) {
148 			if (res == NULL) {
149 				freeaddrinfo(ores);
150 				result = HOSTNAME_INCORRECTNAME;
151 				goto numeric;
152 			}
153 			sa = res->ai_addr;
154 			if (sa == NULL) {
155 				freeaddrinfo(ores);
156 				result = HOSTNAME_INCORRECTNAME;
157 				goto numeric;
158 			}
159 			if (sa->sa_len == addrlen &&
160 			    sa->sa_family == addr->sa_family) {
161 				SOCKINET(sa)->si_port = SOCKINET(addr)->si_port;
162 #ifdef INET6
163 				/*
164 				 * XXX: sin6_socpe_id may not been
165 				 * filled by DNS
166 				 */
167 				if (sa->sa_family == AF_INET6 &&
168 				    SOCKADDR_IN6(sa)->sin6_scope_id == 0)
169 					SOCKADDR_IN6(sa)->sin6_scope_id =
170 					    SOCKADDR_IN6(addr)->sin6_scope_id;
171 #endif
172 				if (!memcmp(sa, addr, sa->sa_len)) {
173 					result = HOSTNAME_FOUND;
174 					if (ores->ai_canonname == NULL) {
175 						freeaddrinfo(ores);
176 						goto numeric;
177 					}
178 					strlcpy(buf, ores->ai_canonname,
179 						sizeof(buf));
180 					trimdomain(buf, hsize);
181 					if (strlen(buf) > hsize &&
182 					    addr->sa_family == AF_INET) {
183 						freeaddrinfo(ores);
184 						goto numeric;
185 					}
186 					strncpy(host, buf, hsize);
187 					break;
188 				}
189 			}
190 		}
191 		freeaddrinfo(ores);
192 	} else {
193     numeric:
194 		if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0,
195 				NI_NUMERICHOST) == 0)
196 			strncpy(host, buf, hsize);
197 	}
198 
199 	return result;
200 }
201 
202 
203