1 /** @file 2 Copyright (c) 1999 - 2014, Intel Corporation. All rights reserved.<BR> 3 This program and the accompanying materials are licensed and made available 4 under the terms and conditions of the BSD License which accompanies this 5 distribution. The full text of the license may be found at 6 http://opensource.org/licenses/bsd-license.php. 7 8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 10 **/ 11 /* 12 * Copyright (c) 1985, 1993 13 * The Regents of the University of California. All rights reserved. 14 * 15 * Portions copyright (c) 1999, 2000 16 * Intel Corporation. 17 * All rights reserved. 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 23 * 1. Redistributions of source code must retain the above copyright 24 * notice, this list of conditions and the following disclaimer. 25 * 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * 3. All advertising materials mentioning features or use of this software 31 * must display the following acknowledgement: 32 * 33 * This product includes software developed by the University of 34 * California, Berkeley, Intel Corporation, and its contributors. 35 * 36 * 4. Neither the name of University, Intel Corporation, or their respective 37 * contributors may be used to endorse or promote products derived from 38 * this software without specific prior written permission. 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND 41 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 42 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 43 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS, 44 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 45 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 47 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 48 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 50 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 51 * 52 */ 53 54 /* 55 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 56 * 57 * Permission to use, copy, modify, and distribute this software for any 58 * purpose with or without fee is hereby granted, provided that the above 59 * copyright notice and this permission notice appear in all copies, and that 60 * the name of Digital Equipment Corporation not be used in advertising or 61 * publicity pertaining to distribution of the document or software without 62 * specific, written prior permission. 63 * 64 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 65 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 66 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 67 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 68 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 69 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 70 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 71 * SOFTWARE. 72 */ 73 74 /* 75 * Portions Copyright (c) 1996 by Internet Software Consortium. 76 * 77 * Permission to use, copy, modify, and distribute this software for any 78 * purpose with or without fee is hereby granted, provided that the above 79 * copyright notice and this permission notice appear in all copies. 80 * 81 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 82 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 83 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 84 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 85 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 86 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 87 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 88 * SOFTWARE. 89 */ 90 91 #if defined(LIBC_SCCS) && !defined(lint) 92 static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 93 static char orig_rcsid[] = "From: Id: res_comp.c,v 8.11 1997/05/21 19:31:04 halley Exp $"; 94 static char rcsid[] = "$Id: res_comp.c,v 1.1.1.1 2003/11/19 01:51:35 kyu3 Exp $"; 95 #endif /* LIBC_SCCS and not lint */ 96 97 #include <sys/types.h> 98 #include <sys/param.h> 99 #include <netinet/in.h> 100 #include <arpa/nameser.h> 101 #include <ctype.h> 102 #include <resolv.h> 103 #include <stdio.h> 104 #include <string.h> 105 #include <unistd.h> 106 107 #define BIND_4_COMPAT 108 109 /* 110 * Expand compressed domain name 'comp_dn' to full domain name. 111 * 'msg' is a pointer to the begining of the message, 112 * 'eomorig' points to the first location after the message, 113 * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 114 * Return size of compressed name or -1 if there was an error. 115 */ 116 int 117 dn_expand(const u_char *msg, const u_char *eom, const u_char *src, 118 char *dst, int dstsiz) 119 { 120 int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); 121 122 if (n > 0 && dst[0] == '.') 123 dst[0] = '\0'; 124 return (n); 125 } 126 127 /* 128 * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. 129 * Return the size of the compressed name or -1. 130 * 'length' is the size of the array pointed to by 'comp_dn'. 131 */ 132 int 133 dn_comp(const char *src, u_char *dst, int dstsiz, 134 u_char **dnptrs, u_char **lastdnptr) 135 { 136 return (ns_name_compress(src, dst, (size_t)dstsiz, 137 (const u_char **)dnptrs, 138 (const u_char **)lastdnptr)); 139 } 140 141 /* 142 * Skip over a compressed domain name. Return the size or -1. 143 */ 144 int 145 dn_skipname(const u_char *ptr, const u_char *eom) { 146 const u_char *saveptr = ptr; 147 148 if (ns_name_skip(&ptr, eom) == -1) 149 return (-1); 150 return ((int)(ptr - saveptr)); 151 } 152 153 /* 154 * Verify that a domain name uses an acceptable character set. 155 */ 156 157 /* 158 * Note the conspicuous absence of ctype macros in these definitions. On 159 * non-ASCII hosts, we can't depend on string literals or ctype macros to 160 * tell us anything about network-format data. The rest of the BIND system 161 * is not careful about this, but for some reason, we're doing it right here. 162 */ 163 #define PERIOD 0x2e 164 #define hyphenchar(c) ((c) == 0x2d) 165 #define bslashchar(c) ((c) == 0x5c) 166 #define periodchar(c) ((c) == PERIOD) 167 #define asterchar(c) ((c) == 0x2a) 168 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ 169 || ((c) >= 0x61 && (c) <= 0x7a)) 170 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) 171 172 #define borderchar(c) (alphachar(c) || digitchar(c)) 173 #define middlechar(c) (borderchar(c) || hyphenchar(c)) 174 #define domainchar(c) ((c) > 0x20 && (c) < 0x7f) 175 176 int 177 res_hnok( 178 const char *dn 179 ) 180 { 181 int pch = PERIOD, ch = *dn++; 182 183 while (ch != '\0') { 184 int nch = *dn++; 185 186 if (periodchar(ch)) { 187 (void)NULL; 188 } else if (periodchar(pch)) { 189 if (!borderchar(ch)) 190 return (0); 191 } else if (periodchar(nch) || nch == '\0') { 192 if (!borderchar(ch)) 193 return (0); 194 } else { 195 if (!middlechar(ch)) 196 return (0); 197 } 198 pch = ch; 199 ch = nch; 200 } 201 return (1); 202 } 203 204 /* 205 * hostname-like (A, MX, WKS) owners can have "*" as their first label 206 * but must otherwise be as a host name. 207 */ 208 int 209 res_ownok( 210 const char *dn 211 ) 212 { 213 if (asterchar(dn[0])) { 214 if (periodchar(dn[1])) 215 return (res_hnok(dn+2)); 216 if (dn[1] == '\0') 217 return (1); 218 } 219 return (res_hnok(dn)); 220 } 221 222 /* 223 * SOA RNAMEs and RP RNAMEs can have any printable character in their first 224 * label, but the rest of the name has to look like a host name. 225 */ 226 int 227 res_mailok( 228 const char *dn 229 ) 230 { 231 int ch, escaped = 0; 232 233 /* "." is a valid missing representation */ 234 if (*dn == '\0') 235 return (1); 236 237 /* otherwise <label>.<hostname> */ 238 while ((ch = *dn++) != '\0') { 239 if (!domainchar(ch)) 240 return (0); 241 if (!escaped && periodchar(ch)) 242 break; 243 if (escaped) 244 escaped = 0; 245 else if (bslashchar(ch)) 246 escaped = 1; 247 } 248 if (periodchar(ch)) 249 return (res_hnok(dn)); 250 return (0); 251 } 252 253 /* 254 * This function is quite liberal, since RFC 1034's character sets are only 255 * recommendations. 256 */ 257 int 258 res_dnok( 259 const char *dn 260 ) 261 { 262 int ch; 263 264 while ((ch = *dn++) != '\0') 265 if (!domainchar(ch)) 266 return (0); 267 return (1); 268 } 269 270 #ifdef BIND_4_COMPAT 271 /* 272 * This module must export the following externally-visible symbols: 273 * ___putlong 274 * ___putshort 275 * __getlong 276 * __getshort 277 * Note that one _ comes from C and the others come from us. 278 */ 279 void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); } 280 void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); } 281 u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); } 282 u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); } 283 #endif /*BIND_4_COMPAT*/ 284