]> git.ipfire.org Git - thirdparty/squid.git/blame - src/dnsserver.cc
remove dlinkAddTail
[thirdparty/squid.git] / src / dnsserver.cc
CommitLineData
b560dd20 1
30a4f2a8 2/*
bd34f258 3 * $Id: dnsserver.cc,v 1.47 1998/04/03 06:27:04 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
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
090089c4 216extern int h_errno;
217
6bf65235 218#if LIBRESOLV_DNS_TTL_HACK
28b2f45f 219extern int _dns_ttl_; /* this is a really *dirty* hack - bne */
6bf65235 220#endif
221
3bf15742 222#ifdef _SQUID_NEXT_
223/* This is a really bloody hack. frank@langen.bull.de
224 * Workaround bug in gethostbyname which sets h_errno wrong
225 * WARNING: This hack queries only the resolver and not NetInfo or YP
226 */
227struct hostent *_res_gethostbyname(char *name);
228#define gethostbyname _res_gethostbyname
229#endif /* _SQUID_NEXT_ */
230
429fdbec 231static struct in_addr no_addr;
090089c4 232
233/* error messages from gethostbyname() */
b8d8561b 234static char *
235my_h_msgs(int x)
a0b481fd 236{
237 if (x == HOST_NOT_FOUND)
238 return "Host not found (authoritative)";
239 else if (x == TRY_AGAIN)
240 return "Host not found (non-authoritative)";
241 else if (x == NO_RECOVERY)
242 return "Non recoverable errors";
243 else if (x == NO_DATA || x == NO_ADDRESS)
244 return "Valid name, no data record of requested type";
245 else
246 return "Unknown DNS problem";
247}
090089c4 248
88738790 249#define REQ_SZ 512
250
bd34f258 251static void
252lookup(const char *buf)
253{
254 const struct hostent *result = NULL;
255 int reverse = 0;
256 int ttl = 0;
257 int retry = 0;
258 int i;
259 struct in_addr addr;
260 if (0 == strcmp(buf, "$shutdown"))
261 exit(0);
262 if (0 == strcmp(buf, "$hello")) {
263 printf("$alive\n");
264 return;
265 }
266 /* check if it's already an IP address in text form. */
267 for (;;) {
268 if (safe_inet_addr(buf, &addr)) {
269 reverse = 1;
270 result = gethostbyaddr((char *) &addr.s_addr, 4, AF_INET);
271 } else {
272 result = gethostbyname(buf);
273 }
274 if (NULL != result)
275 break;
276 if (h_errno != TRY_AGAIN)
277 break;
278 if (++retry == 3)
279 break;
280 sleep(1);
281 }
282 if (NULL == result) {
283 if (h_errno == TRY_AGAIN) {
284 printf("$fail Name Server for domain '%s' is unavailable.\n", buf);
285 } else {
286 printf("$fail DNS Domain '%s' is invalid: %s.\n",
287 buf, my_h_msgs(h_errno));
288 }
289 return;
290 }
291#if LIBRESOLV_DNS_TTL_HACK
292 /* DNS TTL handling - bne@CareNet.hu
293 * for first try it's a dirty hack, by hacking getanswer
294 * to place the ttl in a global variable */
295 if (_dns_ttl_ > -1)
296 ttl = _dns_ttl_;
297#endif
298 if (reverse) {
299 printf("$name %d %s\n", ttl, result->h_name);
300 return;
301 }
302 printf("$addr %d", ttl);
303 for (i = 0; NULL != result->h_addr_list[i]; i++) {
304 if (32 == i)
305 break;
306 xmemcpy(&addr, result->h_addr_list[i], sizeof(addr));
307 printf(" %s", inet_ntoa(addr));
308 }
309 printf("\n");
310}
311
312static void
313usage(void)
314{
315 fprintf(stderr, "usage: dnsserver -Dhv -s nameserver\n"
316 "\t-D Enable resolver RES_DEFNAMES and RES_DNSRCH options\n"
317 "\t-h Help\n"
318 "\t-v Version\n"
319 "\t-s nameserver Specify alternate name server(s). 'nameserver'\n"
320 "\t must be an IP address, -s option may be repeated\n");
321}
322
b8d8561b 323int
324main(int argc, char *argv[])
090089c4 325{
88738790 326 char request[512];
090089c4 327 char *t = NULL;
090089c4 328 int c;
09c483ec 329 int opt_s = 0;
330 extern char *optarg;
090089c4 331
429fdbec 332 safe_inet_addr("255.255.255.255", &no_addr);
451bf90b 333
30a4f2a8 334#if HAVE_RES_INIT
335 res_init();
ec762af2 336#ifdef RES_DEFAULT
337 _res.options = RES_DEFAULT;
338#endif
30a4f2a8 339#ifdef RES_DEFNAMES
340 _res.options &= ~RES_DEFNAMES;
341#endif
342#ifdef RES_DNSRCH
343 _res.options &= ~RES_DNSRCH;
344#endif
345#endif
346
bd34f258 347 while ((c = getopt(argc, argv, "Dhs:v")) != -1) {
090089c4 348 switch (c) {
a0bb9c7a 349 case 'D':
350#ifdef RES_DEFNAMES
351 _res.options |= RES_DEFNAMES;
5a0188b7 352#endif
353#ifdef RES_DNSRCH
354 _res.options |= RES_DNSRCH;
a0bb9c7a 355#endif
356 break;
09c483ec 357 case 's':
358 if (opt_s == 0) {
359 _res.nscount = 0;
360 _res.options |= RES_INIT;
361 opt_s = 1;
362 }
363 safe_inet_addr(optarg, &_res.nsaddr_list[_res.nscount++].sin_addr);
364 break;
365 case 'v':
366 printf("dnsserver version %s\n", SQUID_VERSION);
367 exit(0);
368 break;
bd34f258 369 case 'h':
090089c4 370 default:
bd34f258 371 usage();
090089c4 372 exit(1);
373 break;
374 }
30a4f2a8 375 }
090089c4 376
cb0486c3 377 for (;;) {
88738790 378 memset(request, '\0', REQ_SZ);
bd34f258 379 if (fgets(request, REQ_SZ, stdin) == NULL)
090089c4 380 exit(1);
88738790 381 t = strrchr(request, '\n');
382 if (t == NULL) /* Ignore if no newline */
383 continue;
384 *t = '\0'; /* strip NL */
090089c4 385 if ((t = strrchr(request, '\r')) != NULL)
386 *t = '\0'; /* strip CR */
bd34f258 387 lookup(request);
388 fflush(stdout);
090089c4 389 }
b44c0fb4 390 /* NOTREACHED */
7690e8eb 391 return 0;
090089c4 392}