/*-1* SPDX-License-Identifier: (BSD-3-Clause AND ISC)2*3* Copyright (c) 1985, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031/*32* Portions Copyright (c) 1993 by Digital Equipment Corporation.33*34* Permission to use, copy, modify, and distribute this software for any35* purpose with or without fee is hereby granted, provided that the above36* copyright notice and this permission notice appear in all copies, and that37* the name of Digital Equipment Corporation not be used in advertising or38* publicity pertaining to distribution of the document or software without39* specific, written prior permission.40*41* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL42* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES43* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT44* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL45* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR46* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS47* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS48* SOFTWARE.49*/5051/*52* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")53* Portions Copyright (c) 1996-1999 by Internet Software Consortium.54*55* Permission to use, copy, modify, and distribute this software for any56* purpose with or without fee is hereby granted, provided that the above57* copyright notice and this permission notice appear in all copies.58*59* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES60* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF61* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR62* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES63* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN64* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT65* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.66*/6768#include "port_before.h"69#include <sys/param.h>70#include <netinet/in.h>71#include <arpa/nameser.h>72#include <ctype.h>73#include <resolv.h>74#include <stdio.h>75#include <string.h>76#include <unistd.h>77#include "port_after.h"7879/*%80* Expand compressed domain name 'src' to full domain name.81*82* \li 'msg' is a pointer to the beginning of the message,83* \li 'eom' points to the first location after the message,84* \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.85* \li Return size of compressed name or -1 if there was an error.86*/87int88dn_expand(const u_char *msg, const u_char *eom, const u_char *src,89char *dst, int dstsiz)90{91int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);9293if (n > 0 && dst[0] == '.')94dst[0] = '\0';95return (n);96}9798/*%99* Pack domain name 'exp_dn' in presentation form into 'comp_dn'.100*101* \li Return the size of the compressed name or -1.102* \li 'length' is the size of the array pointed to by 'comp_dn'.103*/104int105dn_comp(const char *src, u_char *dst, int dstsiz,106u_char **dnptrs, u_char **lastdnptr)107{108return (ns_name_compress(src, dst, (size_t)dstsiz,109(const u_char **)dnptrs,110(const u_char **)lastdnptr));111}112113/*%114* Skip over a compressed domain name. Return the size or -1.115*/116int117dn_skipname(const u_char *ptr, const u_char *eom) {118const u_char *saveptr = ptr;119120if (ns_name_skip(&ptr, eom) == -1)121return (-1);122return (ptr - saveptr);123}124125/*%126* Verify that a domain name uses an acceptable character set.127*128* Note the conspicuous absence of ctype macros in these definitions. On129* non-ASCII hosts, we can't depend on string literals or ctype macros to130* tell us anything about network-format data. The rest of the BIND system131* is not careful about this, but for some reason, we're doing it right here.132*/133#define PERIOD 0x2e134#define hyphenchar(c) ((c) == 0x2d)135#define bslashchar(c) ((c) == 0x5c)136#define underscorechar(c) ((c) == 0x5f)137#define periodchar(c) ((c) == PERIOD)138#define asterchar(c) ((c) == 0x2a)139#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \140|| ((c) >= 0x61 && (c) <= 0x7a))141#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)142143#ifdef RES_ENFORCE_RFC1034144#define borderchar(c) (alphachar(c) || digitchar(c))145#else146#define borderchar(c) (alphachar(c) || digitchar(c) || underscorechar(c))147#endif148#define middlechar(c) (borderchar(c) || hyphenchar(c))149#define domainchar(c) ((c) > 0x20 && (c) < 0x7f)150151int152res_hnok(const char *dn) {153int pch = PERIOD, ch = *dn++;154155while (ch != '\0') {156int nch = *dn++;157158if (periodchar(ch)) {159(void)NULL;160} else if (periodchar(pch)) {161if (!borderchar(ch))162return (0);163} else if (periodchar(nch) || nch == '\0') {164if (!borderchar(ch))165return (0);166} else {167if (!middlechar(ch))168return (0);169}170pch = ch, ch = nch;171}172return (1);173}174175/*%176* hostname-like (A, MX, WKS) owners can have "*" as their first label177* but must otherwise be as a host name.178*/179int180res_ownok(const char *dn) {181if (asterchar(dn[0])) {182if (periodchar(dn[1]))183return (res_hnok(dn+2));184if (dn[1] == '\0')185return (1);186}187return (res_hnok(dn));188}189190/*%191* SOA RNAMEs and RP RNAMEs can have any printable character in their first192* label, but the rest of the name has to look like a host name.193*/194int195res_mailok(const char *dn) {196int ch, escaped = 0;197198/* "." is a valid missing representation */199if (*dn == '\0')200return (1);201202/* otherwise <label>.<hostname> */203while ((ch = *dn++) != '\0') {204if (!domainchar(ch))205return (0);206if (!escaped && periodchar(ch))207break;208if (escaped)209escaped = 0;210else if (bslashchar(ch))211escaped = 1;212}213if (periodchar(ch))214return (res_hnok(dn));215return (0);216}217218/*%219* This function is quite liberal, since RFC1034's character sets are only220* recommendations.221*/222int223res_dnok(const char *dn) {224int ch;225226while ((ch = *dn++) != '\0')227if (!domainchar(ch))228return (0);229return (1);230}231232#ifdef BIND_4_COMPAT233/*%234* This module must export the following externally-visible symbols:235* ___putlong236* ___putshort237* __getlong238* __getshort239* Note that one _ comes from C and the others come from us.240*/241242#ifdef SOLARIS2243#ifdef __putlong244#undef __putlong245#endif246#ifdef __putshort247#undef __putshort248#endif249#pragma weak putlong = __putlong250#pragma weak putshort = __putshort251#endif /* SOLARIS2 */252253void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }254void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }255#ifndef __ultrix__256u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }257u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }258#endif /*__ultrix__*/259#endif /*BIND_4_COMPAT*/260261/*262* Weak aliases for applications that use certain private entry points,263* and fail to include <resolv.h>.264*/265#undef dn_comp266__weak_reference(__dn_comp, dn_comp);267#undef dn_expand268__weak_reference(__dn_expand, dn_expand);269270/*! \file */271272273