119370Spst/* 2130803Smarcel * Copyright (c) 1985, 1993 3130803Smarcel * The Regents of the University of California. All rights reserved. 4130803Smarcel * 5130803Smarcel * Redistribution and use in source and binary forms, with or without 619370Spst * modification, are permitted provided that the following conditions 719370Spst * are met: 898944Sobrien * 1. Redistributions of source code must retain the above copyright 946283Sdfr * notice, this list of conditions and the following disclaimer. 1098944Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1119370Spst * notice, this list of conditions and the following disclaimer in the 1298944Sobrien * documentation and/or other materials provided with the distribution. 1398944Sobrien * 4. Neither the name of the University nor the names of its contributors 1498944Sobrien * may be used to endorse or promote products derived from this software 1598944Sobrien * without specific prior written permission. 1619370Spst * 1798944Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1898944Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1998944Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2098944Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2119370Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2298944Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2398944Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2498944Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2598944Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2619370Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2719370Spst * SUCH DAMAGE. 2898944Sobrien */ 2919370Spst 3019370Spst/* 3119370Spst * Portions Copyright (c) 1993 by Digital Equipment Corporation. 3219370Spst * 3346283Sdfr * Permission to use, copy, modify, and distribute this software for any 3419370Spst * purpose with or without fee is hereby granted, provided that the above 3519370Spst * copyright notice and this permission notice appear in all copies, and that 3619370Spst * the name of Digital Equipment Corporation not be used in advertising or 3719370Spst * publicity pertaining to distribution of the document or software without 3898944Sobrien * specific, written prior permission. 39130803Smarcel * 4046283Sdfr * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 4198944Sobrien * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 4298944Sobrien * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 4398944Sobrien * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 4446283Sdfr * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 4598944Sobrien * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 4698944Sobrien * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 4746283Sdfr * SOFTWARE. 4898944Sobrien */ 4946283Sdfr 5098944Sobrien/* 5146283Sdfr * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5219370Spst * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 5398944Sobrien * 5419370Spst * Permission to use, copy, modify, and distribute this software for any 5519370Spst * purpose with or without fee is hereby granted, provided that the above 5646283Sdfr * copyright notice and this permission notice appear in all copies. 5798944Sobrien * 5846283Sdfr * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 5946283Sdfr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 6098944Sobrien * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 6119370Spst * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 6298944Sobrien * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 6319370Spst * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 6498944Sobrien * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 6519370Spst */ 6698944Sobrien 6719370Spst#if defined(LIBC_SCCS) && !defined(lint) 6846283Sdfrstatic const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 6998944Sobrienstatic const char rcsid[] = "$Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp $"; 7046283Sdfr#endif /* LIBC_SCCS and not lint */ 7146283Sdfr#include <sys/cdefs.h> 7298944Sobrien__FBSDID("$FreeBSD$"); 7319370Spst 7498944Sobrien#include "port_before.h" 7519370Spst#include <sys/types.h> 7698944Sobrien#include <sys/param.h> 7719370Spst#include <netinet/in.h> 7898944Sobrien#include <arpa/nameser.h> 7919370Spst#include <ctype.h> 8098944Sobrien#include <resolv.h> 8119370Spst#include <stdio.h> 8298944Sobrien#include <string.h> 8319370Spst#include <unistd.h> 8446283Sdfr#include "port_after.h" 8598944Sobrien 8646283Sdfr/*% 8746283Sdfr * Expand compressed domain name 'src' to full domain name. 8898944Sobrien * 8946283Sdfr * \li 'msg' is a pointer to the begining of the message, 9019370Spst * \li 'eom' points to the first location after the message, 9119370Spst * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result. 9246283Sdfr * \li Return size of compressed name or -1 if there was an error. 9319370Spst */ 9446283Sdfrint 9546283Sdfrdn_expand(const u_char *msg, const u_char *eom, const u_char *src, 9646283Sdfr char *dst, int dstsiz) 9746283Sdfr{ 9819370Spst int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); 9919370Spst 10019370Spst if (n > 0 && dst[0] == '.') 10119370Spst dst[0] = '\0'; 10219370Spst return (n); 10319370Spst} 10498944Sobrien 10598944Sobrien/*% 10619370Spst * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. 10719370Spst * 10819370Spst * \li Return the size of the compressed name or -1. 10998944Sobrien * \li 'length' is the size of the array pointed to by 'comp_dn'. 11098944Sobrien */ 11198944Sobrienint 11298944Sobriendn_comp(const char *src, u_char *dst, int dstsiz, 11398944Sobrien u_char **dnptrs, u_char **lastdnptr) 11498944Sobrien{ 11519370Spst return (ns_name_compress(src, dst, (size_t)dstsiz, 11698944Sobrien (const u_char **)dnptrs, 11798944Sobrien (const u_char **)lastdnptr)); 11898944Sobrien} 11998944Sobrien 12098944Sobrien/*% 12119370Spst * Skip over a compressed domain name. Return the size or -1. 12298944Sobrien */ 12346283Sdfrint 12498944Sobriendn_skipname(const u_char *ptr, const u_char *eom) { 12519370Spst const u_char *saveptr = ptr; 12698944Sobrien 12719370Spst if (ns_name_skip(&ptr, eom) == -1) 12898944Sobrien return (-1); 12919370Spst return (ptr - saveptr); 13098944Sobrien} 13198944Sobrien 13298944Sobrien/*% 13398944Sobrien * Verify that a domain name uses an acceptable character set. 13446283Sdfr * 13598944Sobrien * Note the conspicuous absence of ctype macros in these definitions. On 13698944Sobrien * non-ASCII hosts, we can't depend on string literals or ctype macros to 13746283Sdfr * tell us anything about network-format data. The rest of the BIND system 13898944Sobrien * is not careful about this, but for some reason, we're doing it right here. 13998944Sobrien */ 14098944Sobrien#define PERIOD 0x2e 14198944Sobrien#define hyphenchar(c) ((c) == 0x2d) 14298944Sobrien#define bslashchar(c) ((c) == 0x5c) 14346283Sdfr#define underscorechar(c) ((c) == 0x5f) 14498944Sobrien#define periodchar(c) ((c) == PERIOD) 14598944Sobrien#define asterchar(c) ((c) == 0x2a) 14698944Sobrien#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ 14798944Sobrien || ((c) >= 0x61 && (c) <= 0x7a)) 14898944Sobrien#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) 14998944Sobrien 15098944Sobrien#define borderchar(c) (alphachar(c) || digitchar(c)) 15146283Sdfr#ifdef RES_ENFORCE_RFC1034 15298944Sobrien#define middlechar(c) (borderchar(c) || hyphenchar(c)) 15398944Sobrien#else 15498944Sobrien#define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c)) 15598944Sobrien#endif 15698944Sobrien#define domainchar(c) ((c) > 0x20 && (c) < 0x7f) 157130803Smarcel 15846283Sdfrint 15919370Spstres_hnok(const char *dn) { 16098944Sobrien int pch = PERIOD, ch = *dn++; 16119370Spst 16219370Spst while (ch != '\0') { 16319370Spst int nch = *dn++; 16446283Sdfr 16546283Sdfr if (periodchar(ch)) { 16698944Sobrien (void)NULL; 16746283Sdfr } else if (periodchar(pch)) { 16846283Sdfr if (!borderchar(ch)) 16946283Sdfr return (0); 17098944Sobrien } else if (periodchar(nch) || nch == '\0') { 17146283Sdfr if (!borderchar(ch)) 17246283Sdfr return (0); 17346283Sdfr } else { 17498944Sobrien if (!middlechar(ch)) 17519370Spst return (0); 17619370Spst } 17719370Spst pch = ch, ch = nch; 17819370Spst } 17919370Spst return (1); 18019370Spst} 18119370Spst 18246283Sdfr/*% 18398944Sobrien * hostname-like (A, MX, WKS) owners can have "*" as their first label 18419370Spst * but must otherwise be as a host name. 18598944Sobrien */ 18698944Sobrienint 18719370Spstres_ownok(const char *dn) { 18819370Spst if (asterchar(dn[0])) { 18919370Spst if (periodchar(dn[1])) 19019370Spst return (res_hnok(dn+2)); 19119370Spst if (dn[1] == '\0') 192130803Smarcel return (1); 193130803Smarcel } 19419370Spst return (res_hnok(dn)); 195130803Smarcel} 19619370Spst 19719370Spst/*% 19819370Spst * SOA RNAMEs and RP RNAMEs can have any printable character in their first 19919370Spst * label, but the rest of the name has to look like a host name. 20019370Spst */ 20119370Spstint 20219370Spstres_mailok(const char *dn) { 20319370Spst int ch, escaped = 0; 20419370Spst 20519370Spst /* "." is a valid missing representation */ 20646283Sdfr if (*dn == '\0') 20719370Spst return (1); 20846283Sdfr 20946283Sdfr /* otherwise <label>.<hostname> */ 21046283Sdfr while ((ch = *dn++) != '\0') { 21146283Sdfr if (!domainchar(ch)) 21246283Sdfr return (0); 21319370Spst if (!escaped && periodchar(ch)) 21498944Sobrien break; 21519370Spst if (escaped) 21619370Spst escaped = 0; 21719370Spst else if (bslashchar(ch)) 21819370Spst escaped = 1; 21919370Spst } 22019370Spst if (periodchar(ch)) 221130803Smarcel return (res_hnok(dn)); 22246283Sdfr return (0); 22398944Sobrien} 22498944Sobrien 225130803Smarcel/*% 22698944Sobrien * This function is quite liberal, since RFC1034's character sets are only 22719370Spst * recommendations. 22819370Spst */ 22919370Spstint 23019370Spstres_dnok(const char *dn) { 231130803Smarcel int ch; 23246283Sdfr 23398944Sobrien while ((ch = *dn++) != '\0') 23419370Spst if (!domainchar(ch)) 23519370Spst return (0); 23698944Sobrien return (1); 23719370Spst} 23819370Spst 23946283Sdfr#ifdef BIND_4_COMPAT 24046283Sdfr/*% 24198944Sobrien * This module must export the following externally-visible symbols: 24246283Sdfr * ___putlong 24346283Sdfr * ___putshort 24446283Sdfr * __getlong 24546283Sdfr * __getshort 24646283Sdfr * Note that one _ comes from C and the others come from us. 24746283Sdfr */ 248130803Smarcel 249130803Smarcel#ifdef SOLARIS2 250130803Smarcel#ifdef __putlong 251130803Smarcel#undef __putlong 252130803Smarcel#endif 253130803Smarcel#ifdef __putshort 254130803Smarcel#undef __putshort 25546283Sdfr#endif 25646283Sdfr#pragma weak putlong = __putlong 257130803Smarcel#pragma weak putshort = __putshort 25846283Sdfr#endif /* SOLARIS2 */ 25919370Spst 26019370Spstvoid __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); } 26119370Spstvoid __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); } 262130803Smarcel#ifndef __ultrix__ 26319370Spstu_int32_t _getlong(const u_char *src) { return (ns_get32(src)); } 26419370Spstu_int16_t _getshort(const u_char *src) { return (ns_get16(src)); } 26519370Spst#endif /*__ultrix__*/ 26619370Spst#endif /*BIND_4_COMPAT*/ 26719370Spst 26819370Spst/* 269130803Smarcel * Weak aliases for applications that use certain private entry points, 270130803Smarcel * and fail to include <resolv.h>. 27119370Spst */ 27219370Spst#undef dn_comp 27319370Spst__weak_reference(__dn_comp, dn_comp); 27419370Spst#undef dn_expand 27519370Spst__weak_reference(__dn_expand, dn_expand); 276130803Smarcel 277130803Smarcel/*! \file */ 278130803Smarcel