1156952Sume/* 2156952Sume * Copyright (c) 1985, 1993 3156952Sume * The Regents of the University of California. All rights reserved. 4156952Sume * 5156952Sume * Redistribution and use in source and binary forms, with or without 6156952Sume * modification, are permitted provided that the following conditions 7156952Sume * are met: 8156952Sume * 1. Redistributions of source code must retain the above copyright 9156952Sume * notice, this list of conditions and the following disclaimer. 10156952Sume * 2. Redistributions in binary form must reproduce the above copyright 11156952Sume * notice, this list of conditions and the following disclaimer in the 12156952Sume * documentation and/or other materials provided with the distribution. 13156952Sume * 4. Neither the name of the University nor the names of its contributors 14156952Sume * may be used to endorse or promote products derived from this software 15156952Sume * without specific prior written permission. 16156952Sume * 17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27156952Sume * SUCH DAMAGE. 28156952Sume */ 29156952Sume 30156952Sume/* 31156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 32156952Sume * 33156952Sume * Permission to use, copy, modify, and distribute this software for any 34156952Sume * purpose with or without fee is hereby granted, provided that the above 35156952Sume * copyright notice and this permission notice appear in all copies, and that 36156952Sume * the name of Digital Equipment Corporation not be used in advertising or 37156952Sume * publicity pertaining to distribution of the document or software without 38156952Sume * specific, written prior permission. 39156952Sume * 40156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 41156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 42156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 43156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 44156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 45156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 46156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 47156952Sume * SOFTWARE. 48156952Sume */ 49156952Sume 50156952Sume/* 51156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 52156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 53156952Sume * 54156952Sume * Permission to use, copy, modify, and distribute this software for any 55156952Sume * purpose with or without fee is hereby granted, provided that the above 56156952Sume * copyright notice and this permission notice appear in all copies. 57156952Sume * 58156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 59156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 60156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 61156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 62156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 63156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 64156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 65156952Sume */ 66156952Sume 67156952Sume#if defined(LIBC_SCCS) && !defined(lint) 68156952Sumestatic const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 69170244Sumestatic const char rcsid[] = "$Id: res_comp.c,v 1.3.18.2 2005/07/28 07:38:11 marka Exp $"; 70156952Sume#endif /* LIBC_SCCS and not lint */ 71156956Sume#include <sys/cdefs.h> 72156956Sume__FBSDID("$FreeBSD$"); 73156952Sume 74156952Sume#include "port_before.h" 75156952Sume#include <sys/types.h> 76156952Sume#include <sys/param.h> 77156952Sume#include <netinet/in.h> 78156952Sume#include <arpa/nameser.h> 79156952Sume#include <ctype.h> 80156952Sume#include <resolv.h> 81156952Sume#include <stdio.h> 82156952Sume#include <string.h> 83156952Sume#include <unistd.h> 84156952Sume#include "port_after.h" 85156952Sume 86170244Sume/*% 87156952Sume * Expand compressed domain name 'src' to full domain name. 88170244Sume * 89170244Sume * \li 'msg' is a pointer to the begining of the message, 90170244Sume * \li 'eom' points to the first location after the message, 91170244Sume * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result. 92170244Sume * \li Return size of compressed name or -1 if there was an error. 93156952Sume */ 94156952Sumeint 95156952Sumedn_expand(const u_char *msg, const u_char *eom, const u_char *src, 96156952Sume char *dst, int dstsiz) 97156952Sume{ 98156952Sume int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); 99156952Sume 100156952Sume if (n > 0 && dst[0] == '.') 101156952Sume dst[0] = '\0'; 102156952Sume return (n); 103156952Sume} 104156952Sume 105170244Sume/*% 106156952Sume * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. 107170244Sume * 108170244Sume * \li Return the size of the compressed name or -1. 109170244Sume * \li 'length' is the size of the array pointed to by 'comp_dn'. 110156952Sume */ 111156952Sumeint 112156952Sumedn_comp(const char *src, u_char *dst, int dstsiz, 113156952Sume u_char **dnptrs, u_char **lastdnptr) 114156952Sume{ 115156952Sume return (ns_name_compress(src, dst, (size_t)dstsiz, 116156952Sume (const u_char **)dnptrs, 117156952Sume (const u_char **)lastdnptr)); 118156952Sume} 119156952Sume 120170244Sume/*% 121156952Sume * Skip over a compressed domain name. Return the size or -1. 122156952Sume */ 123156952Sumeint 124156952Sumedn_skipname(const u_char *ptr, const u_char *eom) { 125156952Sume const u_char *saveptr = ptr; 126156952Sume 127156952Sume if (ns_name_skip(&ptr, eom) == -1) 128156952Sume return (-1); 129156952Sume return (ptr - saveptr); 130156952Sume} 131156952Sume 132170244Sume/*% 133156952Sume * Verify that a domain name uses an acceptable character set. 134170244Sume * 135156952Sume * Note the conspicuous absence of ctype macros in these definitions. On 136156952Sume * non-ASCII hosts, we can't depend on string literals or ctype macros to 137156952Sume * tell us anything about network-format data. The rest of the BIND system 138156952Sume * is not careful about this, but for some reason, we're doing it right here. 139156952Sume */ 140156952Sume#define PERIOD 0x2e 141156952Sume#define hyphenchar(c) ((c) == 0x2d) 142156952Sume#define bslashchar(c) ((c) == 0x5c) 143176325Sdelphij#define underscorechar(c) ((c) == 0x5f) 144156952Sume#define periodchar(c) ((c) == PERIOD) 145156952Sume#define asterchar(c) ((c) == 0x2a) 146156952Sume#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ 147156952Sume || ((c) >= 0x61 && (c) <= 0x7a)) 148156952Sume#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) 149156952Sume 150156952Sume#define borderchar(c) (alphachar(c) || digitchar(c)) 151193023Sdelphij#ifdef RES_ENFORCE_RFC1034 152193023Sdelphij#define middlechar(c) (borderchar(c) || hyphenchar(c)) 153193023Sdelphij#else 154176325Sdelphij#define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c)) 155193023Sdelphij#endif 156156952Sume#define domainchar(c) ((c) > 0x20 && (c) < 0x7f) 157156952Sume 158156952Sumeint 159156952Sumeres_hnok(const char *dn) { 160156952Sume int pch = PERIOD, ch = *dn++; 161156952Sume 162156952Sume while (ch != '\0') { 163156952Sume int nch = *dn++; 164156952Sume 165156952Sume if (periodchar(ch)) { 166156952Sume (void)NULL; 167156952Sume } else if (periodchar(pch)) { 168156952Sume if (!borderchar(ch)) 169156952Sume return (0); 170156952Sume } else if (periodchar(nch) || nch == '\0') { 171156952Sume if (!borderchar(ch)) 172156952Sume return (0); 173156952Sume } else { 174156952Sume if (!middlechar(ch)) 175156952Sume return (0); 176156952Sume } 177156952Sume pch = ch, ch = nch; 178156952Sume } 179156952Sume return (1); 180156952Sume} 181156952Sume 182170244Sume/*% 183156952Sume * hostname-like (A, MX, WKS) owners can have "*" as their first label 184156952Sume * but must otherwise be as a host name. 185156952Sume */ 186156952Sumeint 187156952Sumeres_ownok(const char *dn) { 188156952Sume if (asterchar(dn[0])) { 189156952Sume if (periodchar(dn[1])) 190156952Sume return (res_hnok(dn+2)); 191156952Sume if (dn[1] == '\0') 192156952Sume return (1); 193156952Sume } 194156952Sume return (res_hnok(dn)); 195156952Sume} 196156952Sume 197170244Sume/*% 198156952Sume * SOA RNAMEs and RP RNAMEs can have any printable character in their first 199156952Sume * label, but the rest of the name has to look like a host name. 200156952Sume */ 201156952Sumeint 202156952Sumeres_mailok(const char *dn) { 203156952Sume int ch, escaped = 0; 204156952Sume 205156952Sume /* "." is a valid missing representation */ 206156952Sume if (*dn == '\0') 207156952Sume return (1); 208156952Sume 209156952Sume /* otherwise <label>.<hostname> */ 210156952Sume while ((ch = *dn++) != '\0') { 211156952Sume if (!domainchar(ch)) 212156952Sume return (0); 213156952Sume if (!escaped && periodchar(ch)) 214156952Sume break; 215156952Sume if (escaped) 216156952Sume escaped = 0; 217156952Sume else if (bslashchar(ch)) 218156952Sume escaped = 1; 219156952Sume } 220156952Sume if (periodchar(ch)) 221156952Sume return (res_hnok(dn)); 222156952Sume return (0); 223156952Sume} 224156952Sume 225170244Sume/*% 226170244Sume * This function is quite liberal, since RFC1034's character sets are only 227156952Sume * recommendations. 228156952Sume */ 229156952Sumeint 230156952Sumeres_dnok(const char *dn) { 231156952Sume int ch; 232156952Sume 233156952Sume while ((ch = *dn++) != '\0') 234156952Sume if (!domainchar(ch)) 235156952Sume return (0); 236156952Sume return (1); 237156952Sume} 238156952Sume 239156952Sume#ifdef BIND_4_COMPAT 240170244Sume/*% 241156952Sume * This module must export the following externally-visible symbols: 242156952Sume * ___putlong 243156952Sume * ___putshort 244156952Sume * __getlong 245156952Sume * __getshort 246156952Sume * Note that one _ comes from C and the others come from us. 247156952Sume */ 248156952Sume 249156952Sume#ifdef SOLARIS2 250156952Sume#ifdef __putlong 251156952Sume#undef __putlong 252156952Sume#endif 253156952Sume#ifdef __putshort 254156952Sume#undef __putshort 255156952Sume#endif 256156952Sume#pragma weak putlong = __putlong 257156952Sume#pragma weak putshort = __putshort 258156952Sume#endif /* SOLARIS2 */ 259156952Sume 260156952Sumevoid __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); } 261156952Sumevoid __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); } 262156952Sume#ifndef __ultrix__ 263156952Sumeu_int32_t _getlong(const u_char *src) { return (ns_get32(src)); } 264156952Sumeu_int16_t _getshort(const u_char *src) { return (ns_get16(src)); } 265156952Sume#endif /*__ultrix__*/ 266156952Sume#endif /*BIND_4_COMPAT*/ 267156956Sume 268156956Sume/* 269156956Sume * Weak aliases for applications that use certain private entry points, 270156956Sume * and fail to include <resolv.h>. 271156956Sume */ 272156956Sume#undef dn_comp 273156956Sume__weak_reference(__dn_comp, dn_comp); 274156956Sume#undef dn_expand 275156956Sume__weak_reference(__dn_expand, dn_expand); 276170244Sume 277170244Sume/*! \file */ 278