1331722Seadler/* 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"; 69269867Sumestatic const char rcsid[] = "$Id: res_comp.c,v 1.5 2005/07/28 06:51:50 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/param.h> 76156952Sume#include <netinet/in.h> 77156952Sume#include <arpa/nameser.h> 78156952Sume#include <ctype.h> 79156952Sume#include <resolv.h> 80156952Sume#include <stdio.h> 81156952Sume#include <string.h> 82156952Sume#include <unistd.h> 83156952Sume#include "port_after.h" 84156952Sume 85170244Sume/*% 86156952Sume * Expand compressed domain name 'src' to full domain name. 87170244Sume * 88298830Spfg * \li 'msg' is a pointer to the beginning of the message, 89170244Sume * \li 'eom' points to the first location after the message, 90170244Sume * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result. 91170244Sume * \li Return size of compressed name or -1 if there was an error. 92156952Sume */ 93156952Sumeint 94156952Sumedn_expand(const u_char *msg, const u_char *eom, const u_char *src, 95156952Sume char *dst, int dstsiz) 96156952Sume{ 97156952Sume int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); 98156952Sume 99156952Sume if (n > 0 && dst[0] == '.') 100156952Sume dst[0] = '\0'; 101156952Sume return (n); 102156952Sume} 103156952Sume 104170244Sume/*% 105156952Sume * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. 106170244Sume * 107170244Sume * \li Return the size of the compressed name or -1. 108170244Sume * \li 'length' is the size of the array pointed to by 'comp_dn'. 109156952Sume */ 110156952Sumeint 111156952Sumedn_comp(const char *src, u_char *dst, int dstsiz, 112156952Sume u_char **dnptrs, u_char **lastdnptr) 113156952Sume{ 114156952Sume return (ns_name_compress(src, dst, (size_t)dstsiz, 115156952Sume (const u_char **)dnptrs, 116156952Sume (const u_char **)lastdnptr)); 117156952Sume} 118156952Sume 119170244Sume/*% 120156952Sume * Skip over a compressed domain name. Return the size or -1. 121156952Sume */ 122156952Sumeint 123156952Sumedn_skipname(const u_char *ptr, const u_char *eom) { 124156952Sume const u_char *saveptr = ptr; 125156952Sume 126156952Sume if (ns_name_skip(&ptr, eom) == -1) 127156952Sume return (-1); 128156952Sume return (ptr - saveptr); 129156952Sume} 130156952Sume 131170244Sume/*% 132156952Sume * Verify that a domain name uses an acceptable character set. 133170244Sume * 134156952Sume * Note the conspicuous absence of ctype macros in these definitions. On 135156952Sume * non-ASCII hosts, we can't depend on string literals or ctype macros to 136156952Sume * tell us anything about network-format data. The rest of the BIND system 137156952Sume * is not careful about this, but for some reason, we're doing it right here. 138156952Sume */ 139156952Sume#define PERIOD 0x2e 140156952Sume#define hyphenchar(c) ((c) == 0x2d) 141156952Sume#define bslashchar(c) ((c) == 0x5c) 142176325Sdelphij#define underscorechar(c) ((c) == 0x5f) 143156952Sume#define periodchar(c) ((c) == PERIOD) 144156952Sume#define asterchar(c) ((c) == 0x2a) 145156952Sume#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ 146156952Sume || ((c) >= 0x61 && (c) <= 0x7a)) 147156952Sume#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) 148156952Sume 149284229Sume#ifdef RES_ENFORCE_RFC1034 150156952Sume#define borderchar(c) (alphachar(c) || digitchar(c)) 151193023Sdelphij#else 152284229Sume#define borderchar(c) (alphachar(c) || digitchar(c) || underscorechar(c)) 153193023Sdelphij#endif 154284229Sume#define middlechar(c) (borderchar(c) || hyphenchar(c)) 155156952Sume#define domainchar(c) ((c) > 0x20 && (c) < 0x7f) 156156952Sume 157156952Sumeint 158156952Sumeres_hnok(const char *dn) { 159156952Sume int pch = PERIOD, ch = *dn++; 160156952Sume 161156952Sume while (ch != '\0') { 162156952Sume int nch = *dn++; 163156952Sume 164156952Sume if (periodchar(ch)) { 165156952Sume (void)NULL; 166156952Sume } else if (periodchar(pch)) { 167156952Sume if (!borderchar(ch)) 168156952Sume return (0); 169156952Sume } else if (periodchar(nch) || nch == '\0') { 170156952Sume if (!borderchar(ch)) 171156952Sume return (0); 172156952Sume } else { 173156952Sume if (!middlechar(ch)) 174156952Sume return (0); 175156952Sume } 176156952Sume pch = ch, ch = nch; 177156952Sume } 178156952Sume return (1); 179156952Sume} 180156952Sume 181170244Sume/*% 182156952Sume * hostname-like (A, MX, WKS) owners can have "*" as their first label 183156952Sume * but must otherwise be as a host name. 184156952Sume */ 185156952Sumeint 186156952Sumeres_ownok(const char *dn) { 187156952Sume if (asterchar(dn[0])) { 188156952Sume if (periodchar(dn[1])) 189156952Sume return (res_hnok(dn+2)); 190156952Sume if (dn[1] == '\0') 191156952Sume return (1); 192156952Sume } 193156952Sume return (res_hnok(dn)); 194156952Sume} 195156952Sume 196170244Sume/*% 197156952Sume * SOA RNAMEs and RP RNAMEs can have any printable character in their first 198156952Sume * label, but the rest of the name has to look like a host name. 199156952Sume */ 200156952Sumeint 201156952Sumeres_mailok(const char *dn) { 202156952Sume int ch, escaped = 0; 203156952Sume 204156952Sume /* "." is a valid missing representation */ 205156952Sume if (*dn == '\0') 206156952Sume return (1); 207156952Sume 208156952Sume /* otherwise <label>.<hostname> */ 209156952Sume while ((ch = *dn++) != '\0') { 210156952Sume if (!domainchar(ch)) 211156952Sume return (0); 212156952Sume if (!escaped && periodchar(ch)) 213156952Sume break; 214156952Sume if (escaped) 215156952Sume escaped = 0; 216156952Sume else if (bslashchar(ch)) 217156952Sume escaped = 1; 218156952Sume } 219156952Sume if (periodchar(ch)) 220156952Sume return (res_hnok(dn)); 221156952Sume return (0); 222156952Sume} 223156952Sume 224170244Sume/*% 225170244Sume * This function is quite liberal, since RFC1034's character sets are only 226156952Sume * recommendations. 227156952Sume */ 228156952Sumeint 229156952Sumeres_dnok(const char *dn) { 230156952Sume int ch; 231156952Sume 232156952Sume while ((ch = *dn++) != '\0') 233156952Sume if (!domainchar(ch)) 234156952Sume return (0); 235156952Sume return (1); 236156952Sume} 237156952Sume 238156952Sume#ifdef BIND_4_COMPAT 239170244Sume/*% 240156952Sume * This module must export the following externally-visible symbols: 241156952Sume * ___putlong 242156952Sume * ___putshort 243156952Sume * __getlong 244156952Sume * __getshort 245156952Sume * Note that one _ comes from C and the others come from us. 246156952Sume */ 247156952Sume 248156952Sume#ifdef SOLARIS2 249156952Sume#ifdef __putlong 250156952Sume#undef __putlong 251156952Sume#endif 252156952Sume#ifdef __putshort 253156952Sume#undef __putshort 254156952Sume#endif 255156952Sume#pragma weak putlong = __putlong 256156952Sume#pragma weak putshort = __putshort 257156952Sume#endif /* SOLARIS2 */ 258156952Sume 259156952Sumevoid __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); } 260156952Sumevoid __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); } 261156952Sume#ifndef __ultrix__ 262156952Sumeu_int32_t _getlong(const u_char *src) { return (ns_get32(src)); } 263156952Sumeu_int16_t _getshort(const u_char *src) { return (ns_get16(src)); } 264156952Sume#endif /*__ultrix__*/ 265156952Sume#endif /*BIND_4_COMPAT*/ 266156956Sume 267156956Sume/* 268156956Sume * Weak aliases for applications that use certain private entry points, 269156956Sume * and fail to include <resolv.h>. 270156956Sume */ 271156956Sume#undef dn_comp 272156956Sume__weak_reference(__dn_comp, dn_comp); 273156956Sume#undef dn_expand 274156956Sume__weak_reference(__dn_expand, dn_expand); 275170244Sume 276170244Sume/*! \file */ 277