--- /dev/null
+/*\r
+ * Copyright (c) 1988, 1993\r
+ * The Regents of the University of California. All rights reserved.\r
+ *\r
+ * Portions copyright (c) 1999, 2000\r
+ * Intel Corporation.\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ *\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ *\r
+ * 3. All advertising materials mentioning features or use of this software\r
+ * must display the following acknowledgement:\r
+ *\r
+ * This product includes software developed by the University of\r
+ * California, Berkeley, Intel Corporation, and its contributors.\r
+ *\r
+ * 4. Neither the name of University, Intel Corporation, or their respective\r
+ * contributors may be used to endorse or promote products derived from\r
+ * this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND\r
+ * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
+ * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,\r
+ * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ */\r
+\r
+/*\r
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.\r
+ *\r
+ * Permission to use, copy, modify, and distribute this software for any\r
+ * purpose with or without fee is hereby granted, provided that the above\r
+ * copyright notice and this permission notice appear in all copies, and that\r
+ * the name of Digital Equipment Corporation not be used in advertising or\r
+ * publicity pertaining to distribution of the document or software without\r
+ * specific, written prior permission.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL\r
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES\r
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT\r
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
+ * SOFTWARE.\r
+ */\r
+\r
+/*\r
+ * Portions Copyright (c) 1996 by Internet Software Consortium.\r
+ *\r
+ * Permission to use, copy, modify, and distribute this software for any\r
+ * purpose with or without fee is hereby granted, provided that the above\r
+ * copyright notice and this permission notice appear in all copies.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
+ * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
+ * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
+ * SOFTWARE.\r
+ */\r
+\r
+#if defined(LIBC_SCCS) && !defined(lint)\r
+static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";\r
+static char orig_rcsid = "From: Id: res_query.c,v 8.14 1997/06/09 17:47:05 halley Exp $";\r
+static char rcsid[] = "$Id: res_query.c,v 1.1.1.1 2003/11/19 01:51:38 kyu3 Exp $";\r
+#endif /* LIBC_SCCS and not lint */\r
+\r
+#include <sys/types.h>\r
+#include <sys/param.h>\r
+#include <netinet/in.h>\r
+#include <arpa/inet.h>\r
+#include <arpa/nameser.h>\r
+#include <ctype.h>\r
+#include <errno.h>\r
+#include <netdb.h>\r
+#include <resolv.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+\r
+#include "res_config.h"\r
+\r
+#if PACKETSZ > 1024\r
+#define MAXPACKET PACKETSZ\r
+#else\r
+#define MAXPACKET 1024\r
+#endif\r
+\r
+/*\r
+ * Formulate a normal query, send, and await answer.\r
+ * Returned answer is placed in supplied buffer "answer".\r
+ * Perform preliminary check of answer, returning success only\r
+ * if no error is indicated and the answer count is nonzero.\r
+ * Return the size of the response on success, -1 on error.\r
+ * Error number is left in h_errno.\r
+ *\r
+ * Caller must parse answer and determine whether it answers the question.\r
+ */\r
+int\r
+res_query(\r
+ const char *name, /* domain name */\r
+ int class, /* class of query */\r
+ int type, /* type of query */\r
+ u_char *answer, /* buffer to put answer */\r
+ int anslen /* size of answer buffer */\r
+ )\r
+{\r
+ u_char buf[MAXPACKET];\r
+ HEADER *hp = (HEADER *) answer;\r
+ int n;\r
+\r
+ hp->rcode = NOERROR; /* default */\r
+\r
+ if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
+ h_errno = NETDB_INTERNAL;\r
+ return (-1);\r
+ }\r
+#ifdef DEBUG\r
+ if (_res.options & RES_DEBUG)\r
+ printf(";; res_query(%s, %d, %d)\n", name, class, type);\r
+#endif\r
+\r
+ n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,\r
+ buf, sizeof(buf));\r
+ if (n <= 0) {\r
+#ifdef DEBUG\r
+ if (_res.options & RES_DEBUG)\r
+ printf(";; res_query: mkquery failed\n");\r
+#endif\r
+ h_errno = NO_RECOVERY;\r
+ return (n);\r
+ }\r
+ n = res_send(buf, n, answer, anslen);\r
+ if (n < 0) {\r
+#ifdef DEBUG\r
+ if (_res.options & RES_DEBUG)\r
+ printf(";; res_query: send error\n");\r
+#endif\r
+ h_errno = TRY_AGAIN;\r
+ return (n);\r
+ }\r
+\r
+ if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {\r
+#ifdef DEBUG\r
+ if (_res.options & RES_DEBUG)\r
+ printf(";; rcode = %d, ancount=%d\n", hp->rcode,\r
+ ntohs(hp->ancount));\r
+#endif\r
+ switch (hp->rcode) {\r
+ case NXDOMAIN:\r
+ h_errno = HOST_NOT_FOUND;\r
+ break;\r
+ case SERVFAIL:\r
+ h_errno = TRY_AGAIN;\r
+ break;\r
+ case NOERROR:\r
+ h_errno = NO_DATA;\r
+ break;\r
+ case FORMERR:\r
+ case NOTIMP:\r
+ case REFUSED:\r
+ default:\r
+ h_errno = NO_RECOVERY;\r
+ break;\r
+ }\r
+ return (-1);\r
+ }\r
+ return (n);\r
+}\r
+\r
+/*\r
+ * Formulate a normal query, send, and retrieve answer in supplied buffer.\r
+ * Return the size of the response on success, -1 on error.\r
+ * If enabled, implement search rules until answer or unrecoverable failure\r
+ * is detected. Error code, if any, is left in h_errno.\r
+ */\r
+int\r
+res_search(\r
+ const char *name, /* domain name */\r
+ int class, /* class of query */\r
+ int type, /* type of query */\r
+ u_char *answer, /* buffer to put answer */\r
+ int anslen /* size of answer */\r
+ )\r
+{\r
+ const char *cp, * const *domain;\r
+ HEADER *hp = (HEADER *) answer;\r
+ u_int dots;\r
+ int trailing_dot, ret, saved_herrno;\r
+ int got_nodata = 0, got_servfail = 0, tried_as_is = 0;\r
+\r
+ if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
+ h_errno = NETDB_INTERNAL;\r
+ return (-1);\r
+ }\r
+ errno = 0;\r
+ h_errno = HOST_NOT_FOUND; /* default, if we never query */\r
+ dots = 0;\r
+ for (cp = name; *cp; cp++)\r
+ dots += (*cp == '.');\r
+ trailing_dot = 0;\r
+ if (cp > name && *--cp == '.')\r
+ trailing_dot++;\r
+\r
+ /* If there aren't any dots, it could be a user-level alias */\r
+ if (!dots && (cp = hostalias(name)) != NULL)\r
+ return (res_query(cp, class, type, answer, anslen));\r
+\r
+ /*\r
+ * If there are dots in the name already, let's just give it a try\r
+ * 'as is'. The threshold can be set with the "ndots" option.\r
+ */\r
+ saved_herrno = -1;\r
+ if (dots >= _res.ndots) {\r
+ ret = res_querydomain(name, NULL, class, type, answer, anslen);\r
+ if (ret > 0)\r
+ return (ret);\r
+ saved_herrno = h_errno;\r
+ tried_as_is++;\r
+ }\r
+\r
+ /*\r
+ * We do at least one level of search if\r
+ * - there is no dot and RES_DEFNAME is set, or\r
+ * - there is at least one dot, there is no trailing dot,\r
+ * and RES_DNSRCH is set.\r
+ */\r
+ if ((!dots && (_res.options & RES_DEFNAMES)) ||\r
+ (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {\r
+ int done = 0;\r
+\r
+ for (domain = (const char * const *)_res.dnsrch;\r
+ *domain && !done;\r
+ domain++) {\r
+\r
+ ret = res_querydomain(name, *domain, class, type,\r
+ answer, anslen);\r
+ if (ret > 0)\r
+ return (ret);\r
+\r
+ /*\r
+ * If no server present, give up.\r
+ * If name isn't found in this domain,\r
+ * keep trying higher domains in the search list\r
+ * (if that's enabled).\r
+ * On a NO_DATA error, keep trying, otherwise\r
+ * a wildcard entry of another type could keep us\r
+ * from finding this entry higher in the domain.\r
+ * If we get some other error (negative answer or\r
+ * server failure), then stop searching up,\r
+ * but try the input name below in case it's\r
+ * fully-qualified.\r
+ */\r
+ if (errno == ECONNREFUSED) {\r
+ h_errno = TRY_AGAIN;\r
+ return (-1);\r
+ }\r
+\r
+ switch (h_errno) {\r
+ case NO_DATA:\r
+ got_nodata++;\r
+ /* FALLTHROUGH */\r
+ case HOST_NOT_FOUND:\r
+ /* keep trying */\r
+ break;\r
+ case TRY_AGAIN:\r
+ if (hp->rcode == SERVFAIL) {\r
+ /* try next search element, if any */\r
+ got_servfail++;\r
+ break;\r
+ }\r
+ /* FALLTHROUGH */\r
+ default:\r
+ /* anything else implies that we're done */\r
+ done++;\r
+ }\r
+\r
+ /* if we got here for some reason other than DNSRCH,\r
+ * we only wanted one iteration of the loop, so stop.\r
+ */\r
+ if (!(_res.options & RES_DNSRCH))\r
+ done++;\r
+ }\r
+ }\r
+\r
+ /*\r
+ * If we have not already tried the name "as is", do that now.\r
+ * note that we do this regardless of how many dots were in the\r
+ * name or whether it ends with a dot unless NOTLDQUERY is set.\r
+ */\r
+ if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {\r
+ ret = res_querydomain(name, NULL, class, type, answer, anslen);\r
+ if (ret > 0)\r
+ return (ret);\r
+ }\r
+\r
+ /* if we got here, we didn't satisfy the search.\r
+ * if we did an initial full query, return that query's h_errno\r
+ * (note that we wouldn't be here if that query had succeeded).\r
+ * else if we ever got a nodata, send that back as the reason.\r
+ * else send back meaningless h_errno, that being the one from\r
+ * the last DNSRCH we did.\r
+ */\r
+ if (saved_herrno != -1)\r
+ h_errno = saved_herrno;\r
+ else if (got_nodata)\r
+ h_errno = NO_DATA;\r
+ else if (got_servfail)\r
+ h_errno = TRY_AGAIN;\r
+ return (-1);\r
+}\r
+\r
+/*\r
+ * Perform a call on res_query on the concatenation of name and domain,\r
+ * removing a trailing dot from name if domain is NULL.\r
+ */\r
+int\r
+res_querydomain(\r
+ const char *name,\r
+ const char *domain,\r
+ int class, /* class of query */\r
+ int type, /* type of query */\r
+ u_char *answer, /* buffer to put answer */\r
+ int anslen /* size of answer */\r
+ )\r
+{\r
+ char nbuf[MAXDNAME];\r
+ const char *longname = nbuf;\r
+ int n, d;\r
+\r
+ if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
+ h_errno = NETDB_INTERNAL;\r
+ return (-1);\r
+ }\r
+#ifdef DEBUG\r
+ if (_res.options & RES_DEBUG)\r
+ printf(";; res_querydomain(%s, %s, %d, %d)\n",\r
+ name, domain?domain:"<Nil>", class, type);\r
+#endif\r
+ if (domain == NULL) {\r
+ /*\r
+ * Check for trailing '.';\r
+ * copy without '.' if present.\r
+ */\r
+ n = (int)strlen(name);\r
+ if (n >= MAXDNAME) {\r
+ h_errno = NO_RECOVERY;\r
+ return (-1);\r
+ }\r
+ n--;\r
+ if (n >= 0 && name[n] == '.') {\r
+ strncpy(nbuf, name, n);\r
+ nbuf[n] = '\0';\r
+ } else\r
+ longname = name;\r
+ } else {\r
+ n = (int)strlen(name);\r
+ d = (int)strlen(domain);\r
+ if (n + d + 1 >= MAXDNAME) {\r
+ h_errno = NO_RECOVERY;\r
+ return (-1);\r
+ }\r
+ sprintf(nbuf, "%s.%s", name, domain);\r
+ }\r
+ return (res_query(longname, class, type, answer, anslen));\r
+}\r
+\r
+const char *\r
+hostalias(\r
+ const char *name\r
+ )\r
+{\r
+ register char *cp1, *cp2;\r
+ FILE *fp;\r
+ char *file;\r
+ char buf[BUFSIZ];\r
+ static char abuf[MAXDNAME];\r
+\r
+ if (_res.options & RES_NOALIASES)\r
+ return (NULL);\r
+#ifdef _ORG_FREEBSD_\r
+ if (issetugid())\r
+ return (NULL);\r
+#endif\r
+ file = getenv("HOSTALIASES");\r
+ if (file == NULL || (fp = fopen(file, "r")) == NULL)\r
+ return (NULL);\r
+ setbuf(fp, NULL);\r
+ buf[sizeof(buf) - 1] = '\0';\r
+ while (fgets(buf, sizeof(buf), fp)) {\r
+ for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)\r
+ ;\r
+ if (!*cp1)\r
+ break;\r
+ *cp1 = '\0';\r
+ if (!strcasecmp(buf, name)) {\r
+ while (isspace(*++cp1))\r
+ ;\r
+ if (!*cp1)\r
+ break;\r
+ for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)\r
+ ;\r
+ abuf[sizeof(abuf) - 1] = *cp2 = '\0';\r
+ strncpy(abuf, cp1, sizeof(abuf) - 1);\r
+ fclose(fp);\r
+ return (abuf);\r
+ }\r
+ }\r
+ fclose(fp);\r
+ return (NULL);\r
+}\r