res_comp.c revision 156952
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 * 3. All advertising materials mentioning features or use of this software 14156952Sume * must display the following acknowledgement: 15156952Sume * This product includes software developed by the University of 16156952Sume * California, Berkeley and its contributors. 17156952Sume * 4. Neither the name of the University nor the names of its contributors 18156952Sume * may be used to endorse or promote products derived from this software 19156952Sume * without specific prior written permission. 20156952Sume * 21156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31156952Sume * SUCH DAMAGE. 32156952Sume */ 33156952Sume 34156952Sume/* 35156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 36156952Sume * 37156952Sume * Permission to use, copy, modify, and distribute this software for any 38156952Sume * purpose with or without fee is hereby granted, provided that the above 39156952Sume * copyright notice and this permission notice appear in all copies, and that 40156952Sume * the name of Digital Equipment Corporation not be used in advertising or 41156952Sume * publicity pertaining to distribution of the document or software without 42156952Sume * specific, written prior permission. 43156952Sume * 44156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 45156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 46156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 47156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 48156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 49156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 50156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 51156952Sume * SOFTWARE. 52156952Sume */ 53156952Sume 54156952Sume/* 55156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 56156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 57156952Sume * 58156952Sume * Permission to use, copy, modify, and distribute this software for any 59156952Sume * purpose with or without fee is hereby granted, provided that the above 60156952Sume * copyright notice and this permission notice appear in all copies. 61156952Sume * 62156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 63156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 64156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 65156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 66156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 67156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 68156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 69156952Sume */ 70156952Sume 71156952Sume#if defined(LIBC_SCCS) && !defined(lint) 72156952Sumestatic const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 73156952Sumestatic const char rcsid[] = "$Id: res_comp.c,v 1.1.2.1.4.2 2005/07/28 07:43:22 marka Exp $"; 74156952Sume#endif /* LIBC_SCCS and not lint */ 75156952Sume 76156952Sume#include "port_before.h" 77156952Sume#include <sys/types.h> 78156952Sume#include <sys/param.h> 79156952Sume#include <netinet/in.h> 80156952Sume#include <arpa/nameser.h> 81156952Sume#include <ctype.h> 82156952Sume#include <resolv.h> 83156952Sume#include <stdio.h> 84156952Sume#include <string.h> 85156952Sume#include <unistd.h> 86156952Sume#include "port_after.h" 87156952Sume 88156952Sume/* 89156952Sume * Expand compressed domain name 'src' to full domain name. 90156952Sume * 'msg' is a pointer to the begining of the message, 91156952Sume * 'eom' points to the first location after the message, 92156952Sume * 'dst' is a pointer to a buffer of size 'dstsiz' for the result. 93156952Sume * Return size of compressed name or -1 if there was an error. 94156952Sume */ 95156952Sumeint 96156952Sumedn_expand(const u_char *msg, const u_char *eom, const u_char *src, 97156952Sume char *dst, int dstsiz) 98156952Sume{ 99156952Sume int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); 100156952Sume 101156952Sume if (n > 0 && dst[0] == '.') 102156952Sume dst[0] = '\0'; 103156952Sume return (n); 104156952Sume} 105156952Sume 106156952Sume/* 107156952Sume * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. 108156952Sume * Return the size of the compressed name or -1. 109156952Sume * '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 120156952Sume/* 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 132156952Sume/* 133156952Sume * Verify that a domain name uses an acceptable character set. 134156952Sume */ 135156952Sume 136156952Sume/* 137156952Sume * Note the conspicuous absence of ctype macros in these definitions. On 138156952Sume * non-ASCII hosts, we can't depend on string literals or ctype macros to 139156952Sume * tell us anything about network-format data. The rest of the BIND system 140156952Sume * is not careful about this, but for some reason, we're doing it right here. 141156952Sume */ 142156952Sume#define PERIOD 0x2e 143156952Sume#define hyphenchar(c) ((c) == 0x2d) 144156952Sume#define bslashchar(c) ((c) == 0x5c) 145156952Sume#define periodchar(c) ((c) == PERIOD) 146156952Sume#define asterchar(c) ((c) == 0x2a) 147156952Sume#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ 148156952Sume || ((c) >= 0x61 && (c) <= 0x7a)) 149156952Sume#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) 150156952Sume 151156952Sume#define borderchar(c) (alphachar(c) || digitchar(c)) 152156952Sume#define middlechar(c) (borderchar(c) || hyphenchar(c)) 153156952Sume#define domainchar(c) ((c) > 0x20 && (c) < 0x7f) 154156952Sume 155156952Sumeint 156156952Sumeres_hnok(const char *dn) { 157156952Sume int pch = PERIOD, ch = *dn++; 158156952Sume 159156952Sume while (ch != '\0') { 160156952Sume int nch = *dn++; 161156952Sume 162156952Sume if (periodchar(ch)) { 163156952Sume (void)NULL; 164156952Sume } else if (periodchar(pch)) { 165156952Sume if (!borderchar(ch)) 166156952Sume return (0); 167156952Sume } else if (periodchar(nch) || nch == '\0') { 168156952Sume if (!borderchar(ch)) 169156952Sume return (0); 170156952Sume } else { 171156952Sume if (!middlechar(ch)) 172156952Sume return (0); 173156952Sume } 174156952Sume pch = ch, ch = nch; 175156952Sume } 176156952Sume return (1); 177156952Sume} 178156952Sume 179156952Sume/* 180156952Sume * hostname-like (A, MX, WKS) owners can have "*" as their first label 181156952Sume * but must otherwise be as a host name. 182156952Sume */ 183156952Sumeint 184156952Sumeres_ownok(const char *dn) { 185156952Sume if (asterchar(dn[0])) { 186156952Sume if (periodchar(dn[1])) 187156952Sume return (res_hnok(dn+2)); 188156952Sume if (dn[1] == '\0') 189156952Sume return (1); 190156952Sume } 191156952Sume return (res_hnok(dn)); 192156952Sume} 193156952Sume 194156952Sume/* 195156952Sume * SOA RNAMEs and RP RNAMEs can have any printable character in their first 196156952Sume * label, but the rest of the name has to look like a host name. 197156952Sume */ 198156952Sumeint 199156952Sumeres_mailok(const char *dn) { 200156952Sume int ch, escaped = 0; 201156952Sume 202156952Sume /* "." is a valid missing representation */ 203156952Sume if (*dn == '\0') 204156952Sume return (1); 205156952Sume 206156952Sume /* otherwise <label>.<hostname> */ 207156952Sume while ((ch = *dn++) != '\0') { 208156952Sume if (!domainchar(ch)) 209156952Sume return (0); 210156952Sume if (!escaped && periodchar(ch)) 211156952Sume break; 212156952Sume if (escaped) 213156952Sume escaped = 0; 214156952Sume else if (bslashchar(ch)) 215156952Sume escaped = 1; 216156952Sume } 217156952Sume if (periodchar(ch)) 218156952Sume return (res_hnok(dn)); 219156952Sume return (0); 220156952Sume} 221156952Sume 222156952Sume/* 223156952Sume * This function is quite liberal, since RFC 1034's character sets are only 224156952Sume * recommendations. 225156952Sume */ 226156952Sumeint 227156952Sumeres_dnok(const char *dn) { 228156952Sume int ch; 229156952Sume 230156952Sume while ((ch = *dn++) != '\0') 231156952Sume if (!domainchar(ch)) 232156952Sume return (0); 233156952Sume return (1); 234156952Sume} 235156952Sume 236156952Sume#ifdef BIND_4_COMPAT 237156952Sume/* 238156952Sume * This module must export the following externally-visible symbols: 239156952Sume * ___putlong 240156952Sume * ___putshort 241156952Sume * __getlong 242156952Sume * __getshort 243156952Sume * Note that one _ comes from C and the others come from us. 244156952Sume */ 245156952Sume 246156952Sume#ifdef SOLARIS2 247156952Sume#ifdef __putlong 248156952Sume#undef __putlong 249156952Sume#endif 250156952Sume#ifdef __putshort 251156952Sume#undef __putshort 252156952Sume#endif 253156952Sume#pragma weak putlong = __putlong 254156952Sume#pragma weak putshort = __putshort 255156952Sume#endif /* SOLARIS2 */ 256156952Sume 257156952Sumevoid __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); } 258156952Sumevoid __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); } 259156952Sume#ifndef __ultrix__ 260156952Sumeu_int32_t _getlong(const u_char *src) { return (ns_get32(src)); } 261156952Sumeu_int16_t _getshort(const u_char *src) { return (ns_get16(src)); } 262156952Sume#endif /*__ultrix__*/ 263156952Sume#endif /*BIND_4_COMPAT*/ 264