1 /*	$NetBSD: roken_gethostby.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <config.h>
37 
38 #include <krb5/roken.h>
39 
40 #undef roken_gethostbyname
41 #undef roken_gethostbyaddr
42 
43 static struct sockaddr_in dns_addr;
44 static char *dns_req;
45 
46 static int
make_address(const char * address,struct in_addr * ip)47 make_address(const char *address, struct in_addr *ip)
48 {
49     if(inet_aton(address, ip) == 0){
50 	/* try to resolve as hostname, it might work if the address we
51            are trying to lookup is local, for instance a web proxy */
52 	struct hostent *he = gethostbyname(address);
53 	if(he) {
54 	    unsigned char *p = (unsigned char*)he->h_addr;
55 	    ip->s_addr = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
56 	} else {
57 	    return -1;
58 	}
59     }
60     return 0;
61 }
62 
63 static int
setup_int(const char * proxy_host,short proxy_port,const char * dns_host,short dns_port,const char * dns_path)64 setup_int(const char *proxy_host, short proxy_port,
65 	  const char *dns_host, short dns_port,
66 	  const char *dns_path)
67 {
68     memset(&dns_addr, 0, sizeof(dns_addr));
69     if(dns_req)
70 	free(dns_req);
71     dns_req = NULL;
72     if(proxy_host) {
73 	if(make_address(proxy_host, &dns_addr.sin_addr) != 0)
74 	    return -1;
75 	dns_addr.sin_port = htons(proxy_port);
76 	if (asprintf(&dns_req, "http://%s:%d%s", dns_host, dns_port, dns_path) < 0)
77 	    return -1;
78     } else {
79 	if(make_address(dns_host, &dns_addr.sin_addr) != 0)
80 	    return -1;
81 	dns_addr.sin_port = htons(dns_port);
82 	asprintf(&dns_req, "%s", dns_path);
83     }
84     dns_addr.sin_family = AF_INET;
85     return 0;
86 }
87 
88 static void
split_spec(const char * spec,char ** host,int * port,char ** path,int def_port)89 split_spec(const char *spec, char **host, int *port, char **path, int def_port)
90 {
91     char *p;
92     *host = strdup(spec);
93     p = strchr(*host, ':');
94     if(p) {
95 	*p++ = '\0';
96 	if(sscanf(p, "%d", port) != 1)
97 	    *port = def_port;
98     } else
99 	*port = def_port;
100     p = strchr(p ? p : *host, '/');
101     if(p) {
102 	if(path)
103 	    *path = strdup(p);
104 	*p = '\0';
105     }else
106 	if(path)
107 	    *path = NULL;
108 }
109 
110 
111 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
roken_gethostby_setup(const char * proxy_spec,const char * dns_spec)112 roken_gethostby_setup(const char *proxy_spec, const char *dns_spec)
113 {
114     char *proxy_host = NULL;
115     int proxy_port = 0;
116     char *dns_host, *dns_path;
117     int dns_port;
118 
119     int ret = -1;
120 
121     split_spec(dns_spec, &dns_host, &dns_port, &dns_path, 80);
122     if(dns_path == NULL)
123 	goto out;
124     if(proxy_spec)
125 	split_spec(proxy_spec, &proxy_host, &proxy_port, NULL, 80);
126     ret = setup_int(proxy_host, proxy_port, dns_host, dns_port, dns_path);
127 out:
128     free(proxy_host);
129     free(dns_host);
130     free(dns_path);
131     return ret;
132 }
133 
134 
135 /* Try to lookup a name or an ip-address using http as transport
136    mechanism. See the end of this file for an example program. */
137 static struct hostent*
roken_gethostby(const char * hostname)138 roken_gethostby(const char *hostname)
139 {
140     int s;
141     struct sockaddr_in addr;
142     char *request = NULL;
143     char buf[1024];
144     int offset = 0;
145     int n;
146     char *p, *foo;
147     size_t len;
148 
149     if(dns_addr.sin_family == 0)
150 	return NULL; /* no configured host */
151     addr = dns_addr;
152     if (asprintf(&request, "GET %s?%s HTTP/1.0\r\n\r\n", dns_req, hostname) < 0)
153 	return NULL;
154     if(request == NULL)
155 	return NULL;
156     s  = socket(AF_INET, SOCK_STREAM, 0);
157     if(s < 0) {
158 	free(request);
159 	return NULL;
160     }
161     if(connect(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
162 	close(s);
163 	free(request);
164 	return NULL;
165     }
166 
167     len = strlen(request);
168     if(write(s, request, len) != (ssize_t)len) {
169 	close(s);
170 	free(request);
171 	return NULL;
172     }
173     free(request);
174     while(1) {
175 	n = read(s, buf + offset, sizeof(buf) - offset);
176 	if(n <= 0)
177 	    break;
178 	offset += n;
179     }
180     buf[offset] = '\0';
181     close(s);
182     p = strstr(buf, "\r\n\r\n"); /* find end of header */
183     if(p) p += 4;
184     else return NULL;
185     foo = NULL;
186     p = strtok_r(p, " \t\r\n", &foo);
187     if(p == NULL)
188 	return NULL;
189     {
190 	/* make a hostent to return */
191 #define MAX_ADDRS 16
192 	static struct hostent he;
193 	static char addrs[4 * MAX_ADDRS];
194 	static char *addr_list[MAX_ADDRS + 1];
195 	int num_addrs = 0;
196 
197 	he.h_name = p;
198 	he.h_aliases = NULL;
199 	he.h_addrtype = AF_INET;
200 	he.h_length = 4;
201 
202 	while((p = strtok_r(NULL, " \t\r\n", &foo)) && num_addrs < MAX_ADDRS) {
203 	    struct in_addr ip;
204 	    inet_aton(p, &ip);
205 	    ip.s_addr = ntohl(ip.s_addr);
206 	    addr_list[num_addrs] = &addrs[num_addrs * 4];
207 	    addrs[num_addrs * 4 + 0] = (ip.s_addr >> 24) & 0xff;
208 	    addrs[num_addrs * 4 + 1] = (ip.s_addr >> 16) & 0xff;
209 	    addrs[num_addrs * 4 + 2] = (ip.s_addr >> 8) & 0xff;
210 	    addrs[num_addrs * 4 + 3] = (ip.s_addr >> 0) & 0xff;
211 	    addr_list[++num_addrs] = NULL;
212 	}
213 	he.h_addr_list = addr_list;
214 	return &he;
215     }
216 }
217 
218 ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL
roken_gethostbyname(const char * hostname)219 roken_gethostbyname(const char *hostname)
220 {
221     struct hostent *he;
222     he = gethostbyname(hostname);
223     if(he)
224 	return he;
225     return roken_gethostby(hostname);
226 }
227 
228 ROKEN_LIB_FUNCTION struct hostent* ROKEN_LIB_CALL
roken_gethostbyaddr(const void * addr,size_t len,int type)229 roken_gethostbyaddr(const void *addr, size_t len, int type)
230 {
231     struct in_addr a;
232     const char *p;
233     struct hostent *he;
234     he = gethostbyaddr(addr, len, type);
235     if(he)
236 	return he;
237     if(type != AF_INET || len != 4)
238 	return NULL;
239     p = addr;
240     a.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
241     return roken_gethostby(inet_ntoa(a));
242 }
243 
244 #if 0
245 
246 /* this program can be used as a cgi `script' to lookup names and
247    ip-addresses */
248 
249 #include <stdio.h>
250 #include <stdlib.h>
251 #include <netdb.h>
252 #include <sys/param.h>
253 
254 int
255 main(int argc, char **argv)
256 {
257     char *query = getenv("QUERY_STRING");
258     char host[MAXHOSTNAMELEN];
259     int i;
260     struct hostent *he;
261 
262     printf("Content-type: text/plain\n\n");
263     if(query == NULL)
264 	exit(0);
265     he = gethostbyname(query);
266     strncpy(host, he->h_name, sizeof(host));
267     host[sizeof(host) - 1] = '\0';
268     he = gethostbyaddr(he->h_addr, he->h_length, AF_INET);
269     printf("%s\n", he->h_name);
270     for(i = 0; he->h_addr_list[i]; i++) {
271 	struct in_addr ip;
272 	unsigned char *p = (unsigned char*)he->h_addr_list[i];
273 	ip.s_addr = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
274 	printf("%s\n", inet_ntoa(ip));
275     }
276     exit(0);
277 }
278 
279 #endif
280