1*85f5e301Schristos /* $NetBSD: res_data.c,v 1.15 2024/01/23 17:24:38 christos Exp $ */
22b237084Schristos
32b237084Schristos /*
42b237084Schristos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52b237084Schristos * Copyright (c) 1995-1999 by Internet Software Consortium.
62b237084Schristos *
72b237084Schristos * Permission to use, copy, modify, and distribute this software for any
82b237084Schristos * purpose with or without fee is hereby granted, provided that the above
92b237084Schristos * copyright notice and this permission notice appear in all copies.
102b237084Schristos *
112b237084Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
122b237084Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
132b237084Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
142b237084Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
152b237084Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
162b237084Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
172b237084Schristos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
182b237084Schristos */
192b237084Schristos
20ca31adbdSchristos #include <sys/cdefs.h>
212b237084Schristos #if defined(LIBC_SCCS) && !defined(lint)
22ca31adbdSchristos #ifdef notdef
2359a755a4Schristos static const char rcsid[] = "Id: res_data.c,v 1.7 2008/12/11 09:59:00 marka Exp";
24ca31adbdSchristos #else
25*85f5e301Schristos __RCSID("$NetBSD: res_data.c,v 1.15 2024/01/23 17:24:38 christos Exp $");
26ca31adbdSchristos #endif
272b237084Schristos #endif /* LIBC_SCCS and not lint */
282b237084Schristos
292b237084Schristos #include "port_before.h"
302b237084Schristos
316f3786f4Schristos #include "namespace.h"
322b237084Schristos #include <sys/types.h>
332b237084Schristos #include <sys/param.h>
342b237084Schristos #include <sys/socket.h>
352b237084Schristos #include <sys/time.h>
362b237084Schristos
372b237084Schristos #include <netinet/in.h>
382b237084Schristos #include <arpa/inet.h>
392b237084Schristos #include <arpa/nameser.h>
402b237084Schristos
412b237084Schristos #include <ctype.h>
422b237084Schristos #include <netdb.h>
432b237084Schristos #include <resolv.h>
442b237084Schristos #include <res_update.h>
452b237084Schristos #include <stdio.h>
462b237084Schristos #include <stdlib.h>
472b237084Schristos #include <string.h>
482b237084Schristos #include <unistd.h>
492b237084Schristos
502b237084Schristos #include "port_after.h"
516f3786f4Schristos
52249296c4Schristos #include "res_private.h"
53249296c4Schristos
546f3786f4Schristos #ifdef __weak_alias
556f3786f4Schristos __weak_alias(res_init,_res_init)
566f3786f4Schristos __weak_alias(res_mkquery,_res_mkquery)
57162504eeSchristos __weak_alias(res_query,_res_query)
58162504eeSchristos __weak_alias(res_search,_res_search)
596f3786f4Schristos __weak_alias(res_send,__res_send)
606f3786f4Schristos __weak_alias(res_close,__res_close)
61162504eeSchristos /* XXX: these leaked in the old bind8 libc */
626f3786f4Schristos __weak_alias(res_querydomain,__res_querydomain)
63162504eeSchristos __weak_alias(res_send_setqhook,__res_send_setqhook)
64162504eeSchristos __weak_alias(res_send_setrhook,__res_send_setrhook)
65162504eeSchristos #if 0
66162504eeSchristos __weak_alias(p_query,__p_query)
67162504eeSchristos __weak_alias(fp_query,__fp_query)
68162504eeSchristos __weak_alias(fp_nquery,__fp_nquery)
69162504eeSchristos __weak_alias(res_isourserver,__res_isourserver)
706f3786f4Schristos __weak_alias(res_opt,_res_opt)
716f3786f4Schristos __weak_alias(hostalias,__hostalias)
726f3786f4Schristos #endif
735ce4c264Schristos #endif
746f3786f4Schristos
752b237084Schristos const char *_res_opcodes[] = {
762b237084Schristos "QUERY",
772b237084Schristos "IQUERY",
782b237084Schristos "CQUERYM",
79d73eb73dSchristos "CQUERYU", /*%< experimental */
80d73eb73dSchristos "NOTIFY", /*%< experimental */
812b237084Schristos "UPDATE",
822b237084Schristos "6",
832b237084Schristos "7",
842b237084Schristos "8",
852b237084Schristos "9",
862b237084Schristos "10",
872b237084Schristos "11",
882b237084Schristos "12",
892b237084Schristos "13",
902b237084Schristos "ZONEINIT",
912b237084Schristos "ZONEREF",
922b237084Schristos };
932b237084Schristos
942b237084Schristos #ifdef BIND_UPDATE
952b237084Schristos const char *_res_sectioncodes[] = {
962b237084Schristos "ZONE",
972b237084Schristos "PREREQUISITES",
982b237084Schristos "UPDATE",
992b237084Schristos "ADDITIONAL",
1002b237084Schristos };
1012b237084Schristos #endif
1022b237084Schristos
1032b237084Schristos #ifndef __BIND_NOSTATIC
1044b05f4b5Schristos extern struct __res_state _nres;
1052b237084Schristos
1062b237084Schristos /* Proto. */
1072b237084Schristos
108df510acfSchristos int res_ourserver_p(const res_state, const struct sockaddr *);
1092b237084Schristos
1102b237084Schristos int
res_init(void)1112b237084Schristos res_init(void) {
1124b05f4b5Schristos int rv;
1134b05f4b5Schristos #ifdef COMPAT__RES
1144b05f4b5Schristos /*
1154b05f4b5Schristos * Compatibility with program that were accessing _res directly
1164b05f4b5Schristos * to set options. We keep another struct res that is the same
1174b05f4b5Schristos * size as the original res structure, and then copy fields to
1184b05f4b5Schristos * it so that we achieve the same initialization
1194b05f4b5Schristos */
1204b05f4b5Schristos res_state ores = __res_get_old_state();
1214b05f4b5Schristos
1224b05f4b5Schristos if (ores->options != 0)
1234b05f4b5Schristos _nres.options = ores->options;
1244b05f4b5Schristos if (ores->retrans != 0)
1254b05f4b5Schristos _nres.retrans = ores->retrans;
1264b05f4b5Schristos if (ores->retry != 0)
1274b05f4b5Schristos _nres.retry = ores->retry;
1284b05f4b5Schristos #endif
1292b237084Schristos
1302b237084Schristos /*
1312b237084Schristos * These three fields used to be statically initialized. This made
1322b237084Schristos * it hard to use this code in a shared library. It is necessary,
1332b237084Schristos * now that we're doing dynamic initialization here, that we preserve
1342b237084Schristos * the old semantics: if an application modifies one of these three
1352b237084Schristos * fields of _res before res_init() is called, res_init() will not
1362b237084Schristos * alter them. Of course, if an application is setting them to
1372b237084Schristos * _zero_ before calling res_init(), hoping to override what used
1382b237084Schristos * to be the static default, we can't detect it and unexpected results
1392b237084Schristos * will follow. Zero for any of these fields would make no sense,
1402b237084Schristos * so one can safely assume that the applications were already getting
1412b237084Schristos * unexpected results.
1422b237084Schristos *
1434b05f4b5Schristos * _nres.options is tricky since some apps were known to diddle the bits
1442b237084Schristos * before res_init() was first called. We can't replicate that semantic
1452b237084Schristos * with dynamic initialization (they may have turned bits off that are
1462b237084Schristos * set in RES_DEFAULT). Our solution is to declare such applications
1472b237084Schristos * "broken". They could fool us by setting RES_INIT but none do (yet).
1482b237084Schristos */
1494b05f4b5Schristos if (!_nres.retrans)
1504b05f4b5Schristos _nres.retrans = RES_TIMEOUT;
1514b05f4b5Schristos if (!_nres.retry)
1524b05f4b5Schristos _nres.retry = 4;
1534b05f4b5Schristos if (!(_nres.options & RES_INIT))
1544b05f4b5Schristos _nres.options = RES_DEFAULT;
1552b237084Schristos
1562b237084Schristos /*
1572b237084Schristos * This one used to initialize implicitly to zero, so unless the app
1582b237084Schristos * has set it to something in particular, we can randomize it now.
1592b237084Schristos */
1604b05f4b5Schristos if (!_nres.id)
16159a755a4Schristos _nres.id = res_nrandomid(&_nres);
1622b237084Schristos
1634b05f4b5Schristos rv = __res_vinit(&_nres, 1);
1644b05f4b5Schristos #ifdef COMPAT__RES
1654b05f4b5Schristos __res_put_old_state(&_nres);
1664b05f4b5Schristos #endif
1674b05f4b5Schristos return rv;
1682b237084Schristos }
1692b237084Schristos
1702b237084Schristos void
p_query(const u_char * msg)1712b237084Schristos p_query(const u_char *msg) {
1722b237084Schristos fp_query(msg, stdout);
1732b237084Schristos }
1742b237084Schristos
1752b237084Schristos void
fp_query(const u_char * msg,FILE * file)1762b237084Schristos fp_query(const u_char *msg, FILE *file) {
1772b237084Schristos fp_nquery(msg, PACKETSZ, file);
1782b237084Schristos }
1792b237084Schristos
1802b237084Schristos void
fp_nquery(const u_char * msg,int len,FILE * file)1812b237084Schristos fp_nquery(const u_char *msg, int len, FILE *file) {
1824b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1)
1832b237084Schristos return;
1842b237084Schristos
1854b05f4b5Schristos res_pquery(&_nres, msg, len, file);
1862b237084Schristos }
1872b237084Schristos
1882b237084Schristos int
res_mkquery(int op,const char * dname,int class,int type,const u_char * data,int datalen,const u_char * newrr_in,u_char * buf,int buflen)189d73eb73dSchristos res_mkquery(int op, /*!< opcode of query */
190d73eb73dSchristos const char *dname, /*!< domain name */
191d73eb73dSchristos int class, int type, /*!< class and type of query */
192d73eb73dSchristos const u_char *data, /*!< resource record data */
193d73eb73dSchristos int datalen, /*!< length of data */
194d73eb73dSchristos const u_char *newrr_in, /*!< new rr for modify or append */
195d73eb73dSchristos u_char *buf, /*!< buffer to put query */
196d73eb73dSchristos int buflen) /*!< size of buffer */
1972b237084Schristos {
1984b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
1994b05f4b5Schristos RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
2002b237084Schristos return (-1);
2012b237084Schristos }
2024b05f4b5Schristos return (res_nmkquery(&_nres, op, dname, class, type,
2032b237084Schristos data, datalen,
2042b237084Schristos newrr_in, buf, buflen));
2052b237084Schristos }
2062b237084Schristos
207df510acfSchristos #ifdef _LIBRESOLV
2082b237084Schristos int
res_mkupdate(ns_updrec * rrecp_in,u_char * buf,int buflen)2092b237084Schristos res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
2104b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
2114b05f4b5Schristos RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
2122b237084Schristos return (-1);
2132b237084Schristos }
2142b237084Schristos
2154b05f4b5Schristos return (res_nmkupdate(&_nres, rrecp_in, buf, buflen));
2162b237084Schristos }
217df510acfSchristos #endif
2182b237084Schristos
2192b237084Schristos int
res_query(const char * name,int class,int type,u_char * answer,int anslen)220d73eb73dSchristos res_query(const char *name, /*!< domain name */
221d73eb73dSchristos int class, int type, /*!< class and type of query */
222d73eb73dSchristos u_char *answer, /*!< buffer to put answer */
223d73eb73dSchristos int anslen) /*!< size of answer buffer */
2242b237084Schristos {
2254b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
2264b05f4b5Schristos RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
2272b237084Schristos return (-1);
2282b237084Schristos }
2294b05f4b5Schristos return (res_nquery(&_nres, name, class, type, answer, anslen));
2302b237084Schristos }
2312b237084Schristos
2322b237084Schristos void
res_send_setqhook(res_send_qhook hook)2332b237084Schristos res_send_setqhook(res_send_qhook hook) {
2344b05f4b5Schristos _nres.qhook = hook;
2352b237084Schristos }
2362b237084Schristos
2372b237084Schristos void
res_send_setrhook(res_send_rhook hook)2382b237084Schristos res_send_setrhook(res_send_rhook hook) {
2394b05f4b5Schristos _nres.rhook = hook;
2402b237084Schristos }
2412b237084Schristos
2422b237084Schristos int
res_isourserver(const struct sockaddr_in * inp)2432b237084Schristos res_isourserver(const struct sockaddr_in *inp) {
2444b05f4b5Schristos return (res_ourserver_p(&_nres, (const struct sockaddr *)(const void *)inp));
2452b237084Schristos }
2462b237084Schristos
2472b237084Schristos int
res_send(const u_char * buf,int buflen,u_char * ans,int anssiz)2482b237084Schristos res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
2494b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
2502b237084Schristos /* errno should have been set by res_init() in this case. */
2512b237084Schristos return (-1);
2522b237084Schristos }
2532b237084Schristos
2544b05f4b5Schristos return (res_nsend(&_nres, buf, buflen, ans, anssiz));
2552b237084Schristos }
2562b237084Schristos
257df510acfSchristos #ifdef _LIBRESOLV
2582b237084Schristos int
res_sendsigned(const u_char * buf,int buflen,ns_tsig_key * key,u_char * ans,int anssiz)2592b237084Schristos res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
2602b237084Schristos u_char *ans, int anssiz)
2612b237084Schristos {
2624b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
2632b237084Schristos /* errno should have been set by res_init() in this case. */
2642b237084Schristos return (-1);
2652b237084Schristos }
2662b237084Schristos
2674b05f4b5Schristos return (res_nsendsigned(&_nres, buf, buflen, key, ans, anssiz));
2682b237084Schristos }
269df510acfSchristos #endif
2702b237084Schristos
2712b237084Schristos void
res_close(void)2722b237084Schristos res_close(void) {
2734b05f4b5Schristos res_nclose(&_nres);
2742b237084Schristos }
2752b237084Schristos
276df510acfSchristos #ifdef _LIBRESOLV
2772b237084Schristos int
res_update(ns_updrec * rrecp_in)2782b237084Schristos res_update(ns_updrec *rrecp_in) {
2794b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
2804b05f4b5Schristos RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
2812b237084Schristos return (-1);
2822b237084Schristos }
2832b237084Schristos
2844b05f4b5Schristos return (res_nupdate(&_nres, rrecp_in, NULL));
2852b237084Schristos }
286df510acfSchristos #endif
2872b237084Schristos
2882b237084Schristos int
res_search(const char * name,int class,int type,u_char * answer,int anslen)289d73eb73dSchristos res_search(const char *name, /*!< domain name */
290d73eb73dSchristos int class, int type, /*!< class and type of query */
291d73eb73dSchristos u_char *answer, /*!< buffer to put answer */
292d73eb73dSchristos int anslen) /*!< size of answer */
2932b237084Schristos {
2944b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
2954b05f4b5Schristos RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
2962b237084Schristos return (-1);
2972b237084Schristos }
2982b237084Schristos
2994b05f4b5Schristos return (res_nsearch(&_nres, name, class, type, answer, anslen));
3002b237084Schristos }
3012b237084Schristos
3022b237084Schristos int
res_querydomain(const char * name,const char * domain,int class,int type,u_char * answer,int anslen)3032b237084Schristos res_querydomain(const char *name,
3042b237084Schristos const char *domain,
305d73eb73dSchristos int class, int type, /*!< class and type of query */
306d73eb73dSchristos u_char *answer, /*!< buffer to put answer */
307d73eb73dSchristos int anslen) /*!< size of answer */
3082b237084Schristos {
3094b05f4b5Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
3104b05f4b5Schristos RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
3112b237084Schristos return (-1);
3122b237084Schristos }
3132b237084Schristos
3144b05f4b5Schristos return (res_nquerydomain(&_nres, name, domain,
3152b237084Schristos class, type,
3162b237084Schristos answer, anslen));
3172b237084Schristos }
3182b237084Schristos
319df510acfSchristos int
res_opt(int a,u_char * b,int c,int d)320df510acfSchristos res_opt(int a, u_char *b, int c, int d)
321df510acfSchristos {
3224b05f4b5Schristos return res_nopt(&_nres, a, b, c, d);
323df510acfSchristos }
324df510acfSchristos
32559a755a4Schristos u_int
res_randomid(void)32659a755a4Schristos res_randomid(void) {
32759a755a4Schristos if ((_nres.options & RES_INIT) == 0U && res_init() == -1) {
32859a755a4Schristos RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
32950ce2a6bSchristos return (u_int)-1;
33059a755a4Schristos }
33159a755a4Schristos
33259a755a4Schristos return (res_nrandomid(&_nres));
33359a755a4Schristos }
33459a755a4Schristos
3352b237084Schristos const char *
hostalias(const char * name)3362b237084Schristos hostalias(const char *name) {
3372b237084Schristos static char abuf[MAXDNAME];
3382b237084Schristos
3394b05f4b5Schristos return (res_hostalias(&_nres, name, abuf, sizeof abuf));
3402b237084Schristos }
3412b237084Schristos
3422b237084Schristos #ifdef ultrix
3432b237084Schristos int
local_hostname_length(const char * hostname)3442b237084Schristos local_hostname_length(const char *hostname) {
3452b237084Schristos int len_host, len_domain;
3462b237084Schristos
3474b05f4b5Schristos if (!*_nres.defdname)
3482b237084Schristos res_init();
3492b237084Schristos len_host = strlen(hostname);
3504b05f4b5Schristos len_domain = strlen(_nres.defdname);
3512b237084Schristos if (len_host > len_domain &&
3524b05f4b5Schristos !strcasecmp(hostname + len_host - len_domain, _nres.defdname) &&
3532b237084Schristos hostname[len_host - len_domain - 1] == '.')
3542b237084Schristos return (len_host - len_domain - 1);
3552b237084Schristos return (0);
3562b237084Schristos }
3572b237084Schristos #endif /*ultrix*/
3582b237084Schristos
3592b237084Schristos #endif
360d73eb73dSchristos
361d73eb73dSchristos /*! \file */
362