]>
Commit | Line | Data |
---|---|---|
b560dd20 | 1 | |
30a4f2a8 | 2 | /* |
cbdec147 | 3 | * $Id: dnsserver.cc,v 1.51 1998/07/20 17:19:34 wessels Exp $ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 0 DNS Resolver | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
42c04c16 | 8 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
30a4f2a8 | 9 | * -------------------------------------------------------- |
10 | * | |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
13 | * National Laboratory for Applied Network Research and funded by | |
14 | * the National Science Foundation. | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation; either version 2 of the License, or | |
19 | * (at your option) any later version. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with this program; if not, write to the Free Software | |
cbdec147 | 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
30a4f2a8 | 29 | * |
30 | */ | |
31 | ||
32 | /* | |
33 | * Copyright (c) 1994, 1995. All rights reserved. | |
34 | * | |
35 | * The Harvest software was developed by the Internet Research Task | |
36 | * Force Research Group on Resource Discovery (IRTF-RD): | |
37 | * | |
38 | * Mic Bowman of Transarc Corporation. | |
39 | * Peter Danzig of the University of Southern California. | |
40 | * Darren R. Hardy of the University of Colorado at Boulder. | |
41 | * Udi Manber of the University of Arizona. | |
42 | * Michael F. Schwartz of the University of Colorado at Boulder. | |
43 | * Duane Wessels of the University of Colorado at Boulder. | |
44 | * | |
45 | * This copyright notice applies to software in the Harvest | |
46 | * ``src/'' directory only. Users should consult the individual | |
47 | * copyright notices in the ``components/'' subdirectories for | |
48 | * copyright information about other software bundled with the | |
49 | * Harvest source code distribution. | |
50 | * | |
51 | * TERMS OF USE | |
52 | * | |
53 | * The Harvest software may be used and re-distributed without | |
54 | * charge, provided that the software origin and research team are | |
55 | * cited in any use of the system. Most commonly this is | |
56 | * accomplished by including a link to the Harvest Home Page | |
57 | * (http://harvest.cs.colorado.edu/) from the query page of any | |
58 | * Broker you deploy, as well as in the query result pages. These | |
59 | * links are generated automatically by the standard Broker | |
60 | * software distribution. | |
61 | * | |
62 | * The Harvest software is provided ``as is'', without express or | |
63 | * implied warranty, and with no support nor obligation to assist | |
64 | * in its use, correction, modification or enhancement. We assume | |
65 | * no liability with respect to the infringement of copyrights, | |
66 | * trade secrets, or any patents, and are not responsible for | |
67 | * consequential damages. Proper use of the Harvest software is | |
68 | * entirely the responsibility of the user. | |
69 | * | |
70 | * DERIVATIVE WORKS | |
71 | * | |
72 | * Users may make derivative works from the Harvest software, subject | |
73 | * to the following constraints: | |
74 | * | |
75 | * - You must include the above copyright notice and these | |
76 | * accompanying paragraphs in all forms of derivative works, | |
77 | * and any documentation and other materials related to such | |
78 | * distribution and use acknowledge that the software was | |
79 | * developed at the above institutions. | |
80 | * | |
81 | * - You must notify IRTF-RD regarding your distribution of | |
82 | * the derivative work. | |
83 | * | |
84 | * - You must clearly notify users that your are distributing | |
85 | * a modified version and not the original Harvest software. | |
86 | * | |
87 | * - Any derivative product is also subject to these copyright | |
88 | * and use restrictions. | |
89 | * | |
90 | * Note that the Harvest software is NOT in the public domain. We | |
91 | * retain copyright, as specified above. | |
92 | * | |
93 | * HISTORY OF FREE SOFTWARE STATUS | |
94 | * | |
95 | * Originally we required sites to license the software in cases | |
96 | * where they were going to build commercial products/services | |
97 | * around Harvest. In June 1995 we changed this policy. We now | |
98 | * allow people to use the core Harvest software (the code found in | |
99 | * the Harvest ``src/'' directory) for free. We made this change | |
100 | * in the interest of encouraging the widest possible deployment of | |
101 | * the technology. The Harvest software is really a reference | |
102 | * implementation of a set of protocols and formats, some of which | |
103 | * we intend to standardize. We encourage commercial | |
104 | * re-implementations of code complying to this set of standards. | |
105 | */ | |
ed43818f | 106 | |
af00901c | 107 | |
108 | ||
109 | ||
110 | #include "config.h" | |
111 | ||
112 | #if HAVE_UNISTD_H | |
113 | #include <unistd.h> | |
114 | #endif | |
115 | #if HAVE_STDLIB_H | |
116 | #include <stdlib.h> | |
117 | #endif | |
118 | #if HAVE_STDIO_H | |
119 | #include <stdio.h> | |
120 | #endif | |
121 | #if HAVE_SYS_TYPES_H | |
122 | #include <sys/types.h> | |
123 | #endif | |
124 | #if HAVE_CTYPE_H | |
125 | #include <ctype.h> | |
126 | #endif | |
127 | #if HAVE_ERRNO_H | |
128 | #include <errno.h> | |
129 | #endif | |
130 | #if HAVE_FCNTL_H | |
131 | #include <fcntl.h> | |
132 | #endif | |
133 | #if HAVE_GRP_H | |
134 | #include <grp.h> | |
135 | #endif | |
88738790 | 136 | #if HAVE_GNUMALLOC_H |
137 | #include <gnumalloc.h> | |
138 | #elif HAVE_MALLOC_H && !defined(_SQUID_FREEBSD_) && !defined(_SQUID_NEXT_) | |
af00901c | 139 | #include <malloc.h> |
140 | #endif | |
141 | #if HAVE_MEMORY_H | |
142 | #include <memory.h> | |
143 | #endif | |
144 | #if HAVE_NETDB_H && !defined(_SQUID_NETDB_H_) /* protect NEXTSTEP */ | |
145 | #define _SQUID_NETDB_H_ | |
146 | #include <netdb.h> | |
147 | #endif | |
148 | #if HAVE_PWD_H | |
149 | #include <pwd.h> | |
150 | #endif | |
151 | #if HAVE_SIGNAL_H | |
152 | #include <signal.h> | |
153 | #endif | |
154 | #if HAVE_TIME_H | |
155 | #include <time.h> | |
156 | #endif | |
157 | #if HAVE_SYS_PARAM_H | |
158 | #include <sys/param.h> | |
159 | #endif | |
160 | #if HAVE_SYS_TIME_H | |
161 | #include <sys/time.h> | |
162 | #endif | |
163 | #if HAVE_SYS_RESOURCE_H | |
164 | #include <sys/resource.h> /* needs sys/time.h above it */ | |
165 | #endif | |
166 | #if HAVE_SYS_SOCKET_H | |
167 | #include <sys/socket.h> | |
168 | #endif | |
169 | #if HAVE_NETINET_IN_H | |
170 | #include <netinet/in.h> | |
171 | #endif | |
172 | #if HAVE_ARPA_INET_H | |
173 | #include <arpa/inet.h> | |
174 | #endif | |
175 | #if HAVE_SYS_STAT_H | |
176 | #include <sys/stat.h> | |
177 | #endif | |
178 | #if HAVE_SYS_UN_H | |
179 | #include <sys/un.h> | |
180 | #endif | |
181 | #if HAVE_SYS_WAIT_H | |
182 | #include <sys/wait.h> | |
183 | #endif | |
184 | #if HAVE_LIBC_H | |
185 | #include <libc.h> | |
186 | #endif | |
187 | #ifdef HAVE_SYS_SYSCALL_H | |
188 | #include <sys/syscall.h> | |
189 | #endif | |
190 | #ifdef HAVE_STRING_H | |
191 | #include <string.h> | |
192 | #endif | |
193 | #ifdef HAVE_STRINGS_H | |
194 | #include <strings.h> | |
195 | #endif | |
196 | #if HAVE_BSTRING_H | |
197 | #include <bstring.h> | |
198 | #endif | |
199 | #ifdef HAVE_CRYPT_H | |
200 | #include <crypt.h> | |
201 | #endif | |
202 | #if HAVE_SYS_SELECT_H | |
203 | #include <sys/select.h> | |
204 | #endif | |
205 | ||
30a4f2a8 | 206 | #if HAVE_ARPA_NAMESER_H |
207 | #include <arpa/nameser.h> | |
208 | #endif | |
209 | #if HAVE_RESOLV_H | |
210 | #include <resolv.h> | |
211 | #endif | |
090089c4 | 212 | |
af00901c | 213 | #include "util.h" |
8350fe9b | 214 | #include "snprintf.h" |
af00901c | 215 | |
44965270 | 216 | #if !defined(_SQUID_AIX_) |
090089c4 | 217 | extern int h_errno; |
44965270 | 218 | #endif |
090089c4 | 219 | |
6bf65235 | 220 | #if LIBRESOLV_DNS_TTL_HACK |
28b2f45f | 221 | extern int _dns_ttl_; /* this is a really *dirty* hack - bne */ |
6bf65235 | 222 | #endif |
223 | ||
3bf15742 | 224 | #ifdef _SQUID_NEXT_ |
225 | /* This is a really bloody hack. frank@langen.bull.de | |
226 | * Workaround bug in gethostbyname which sets h_errno wrong | |
227 | * WARNING: This hack queries only the resolver and not NetInfo or YP | |
228 | */ | |
229 | struct hostent *_res_gethostbyname(char *name); | |
230 | #define gethostbyname _res_gethostbyname | |
231 | #endif /* _SQUID_NEXT_ */ | |
232 | ||
429fdbec | 233 | static struct in_addr no_addr; |
090089c4 | 234 | |
235 | /* error messages from gethostbyname() */ | |
b8d8561b | 236 | static char * |
237 | my_h_msgs(int x) | |
a0b481fd | 238 | { |
239 | if (x == HOST_NOT_FOUND) | |
240 | return "Host not found (authoritative)"; | |
241 | else if (x == TRY_AGAIN) | |
242 | return "Host not found (non-authoritative)"; | |
243 | else if (x == NO_RECOVERY) | |
244 | return "Non recoverable errors"; | |
245 | else if (x == NO_DATA || x == NO_ADDRESS) | |
246 | return "Valid name, no data record of requested type"; | |
247 | else | |
248 | return "Unknown DNS problem"; | |
249 | } | |
090089c4 | 250 | |
88738790 | 251 | #define REQ_SZ 512 |
252 | ||
bd34f258 | 253 | static void |
254 | lookup(const char *buf) | |
255 | { | |
256 | const struct hostent *result = NULL; | |
257 | int reverse = 0; | |
258 | int ttl = 0; | |
259 | int retry = 0; | |
260 | int i; | |
261 | struct in_addr addr; | |
262 | if (0 == strcmp(buf, "$shutdown")) | |
263 | exit(0); | |
264 | if (0 == strcmp(buf, "$hello")) { | |
265 | printf("$alive\n"); | |
266 | return; | |
267 | } | |
268 | /* check if it's already an IP address in text form. */ | |
269 | for (;;) { | |
270 | if (safe_inet_addr(buf, &addr)) { | |
271 | reverse = 1; | |
272 | result = gethostbyaddr((char *) &addr.s_addr, 4, AF_INET); | |
273 | } else { | |
274 | result = gethostbyname(buf); | |
275 | } | |
276 | if (NULL != result) | |
277 | break; | |
278 | if (h_errno != TRY_AGAIN) | |
279 | break; | |
280 | if (++retry == 3) | |
281 | break; | |
282 | sleep(1); | |
283 | } | |
284 | if (NULL == result) { | |
285 | if (h_errno == TRY_AGAIN) { | |
286 | printf("$fail Name Server for domain '%s' is unavailable.\n", buf); | |
287 | } else { | |
288 | printf("$fail DNS Domain '%s' is invalid: %s.\n", | |
289 | buf, my_h_msgs(h_errno)); | |
290 | } | |
291 | return; | |
292 | } | |
293 | #if LIBRESOLV_DNS_TTL_HACK | |
294 | /* DNS TTL handling - bne@CareNet.hu | |
295 | * for first try it's a dirty hack, by hacking getanswer | |
296 | * to place the ttl in a global variable */ | |
297 | if (_dns_ttl_ > -1) | |
298 | ttl = _dns_ttl_; | |
299 | #endif | |
300 | if (reverse) { | |
301 | printf("$name %d %s\n", ttl, result->h_name); | |
302 | return; | |
303 | } | |
304 | printf("$addr %d", ttl); | |
305 | for (i = 0; NULL != result->h_addr_list[i]; i++) { | |
306 | if (32 == i) | |
307 | break; | |
308 | xmemcpy(&addr, result->h_addr_list[i], sizeof(addr)); | |
309 | printf(" %s", inet_ntoa(addr)); | |
310 | } | |
311 | printf("\n"); | |
312 | } | |
313 | ||
314 | static void | |
315 | usage(void) | |
316 | { | |
317 | fprintf(stderr, "usage: dnsserver -Dhv -s nameserver\n" | |
318 | "\t-D Enable resolver RES_DEFNAMES and RES_DNSRCH options\n" | |
319 | "\t-h Help\n" | |
320 | "\t-v Version\n" | |
321 | "\t-s nameserver Specify alternate name server(s). 'nameserver'\n" | |
322 | "\t must be an IP address, -s option may be repeated\n"); | |
323 | } | |
324 | ||
b8d8561b | 325 | int |
326 | main(int argc, char *argv[]) | |
090089c4 | 327 | { |
88738790 | 328 | char request[512]; |
090089c4 | 329 | char *t = NULL; |
090089c4 | 330 | int c; |
09c483ec | 331 | int opt_s = 0; |
332 | extern char *optarg; | |
090089c4 | 333 | |
429fdbec | 334 | safe_inet_addr("255.255.255.255", &no_addr); |
451bf90b | 335 | |
30a4f2a8 | 336 | #if HAVE_RES_INIT |
337 | res_init(); | |
ec762af2 | 338 | #ifdef RES_DEFAULT |
339 | _res.options = RES_DEFAULT; | |
340 | #endif | |
30a4f2a8 | 341 | #ifdef RES_DEFNAMES |
342 | _res.options &= ~RES_DEFNAMES; | |
343 | #endif | |
344 | #ifdef RES_DNSRCH | |
345 | _res.options &= ~RES_DNSRCH; | |
346 | #endif | |
347 | #endif | |
348 | ||
bd34f258 | 349 | while ((c = getopt(argc, argv, "Dhs:v")) != -1) { |
090089c4 | 350 | switch (c) { |
a0bb9c7a | 351 | case 'D': |
352 | #ifdef RES_DEFNAMES | |
353 | _res.options |= RES_DEFNAMES; | |
5a0188b7 | 354 | #endif |
355 | #ifdef RES_DNSRCH | |
356 | _res.options |= RES_DNSRCH; | |
a0bb9c7a | 357 | #endif |
358 | break; | |
09c483ec | 359 | case 's': |
df087e68 | 360 | #if HAVE_RES_INIT |
09c483ec | 361 | if (opt_s == 0) { |
362 | _res.nscount = 0; | |
363 | _res.options |= RES_INIT; | |
364 | opt_s = 1; | |
365 | } | |
366 | safe_inet_addr(optarg, &_res.nsaddr_list[_res.nscount++].sin_addr); | |
df087e68 | 367 | #else |
4b4cd312 | 368 | fprintf(stderr, "-s is not supported on this resolver\n"); |
df087e68 | 369 | #endif /* HAVE_RES_INIT */ |
09c483ec | 370 | break; |
371 | case 'v': | |
372 | printf("dnsserver version %s\n", SQUID_VERSION); | |
373 | exit(0); | |
374 | break; | |
bd34f258 | 375 | case 'h': |
090089c4 | 376 | default: |
bd34f258 | 377 | usage(); |
090089c4 | 378 | exit(1); |
379 | break; | |
380 | } | |
30a4f2a8 | 381 | } |
090089c4 | 382 | |
cb0486c3 | 383 | for (;;) { |
88738790 | 384 | memset(request, '\0', REQ_SZ); |
bd34f258 | 385 | if (fgets(request, REQ_SZ, stdin) == NULL) |
090089c4 | 386 | exit(1); |
88738790 | 387 | t = strrchr(request, '\n'); |
388 | if (t == NULL) /* Ignore if no newline */ | |
389 | continue; | |
390 | *t = '\0'; /* strip NL */ | |
090089c4 | 391 | if ((t = strrchr(request, '\r')) != NULL) |
392 | *t = '\0'; /* strip CR */ | |
bd34f258 | 393 | lookup(request); |
394 | fflush(stdout); | |
090089c4 | 395 | } |
b44c0fb4 | 396 | /* NOTREACHED */ |
7690e8eb | 397 | return 0; |
090089c4 | 398 | } |