Back to index

glibc  2.9
res_comp.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1985, 1993
00003  *    The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00032  *
00033  * Permission to use, copy, modify, and distribute this software for any
00034  * purpose with or without fee is hereby granted, provided that the above
00035  * copyright notice and this permission notice appear in all copies, and that
00036  * the name of Digital Equipment Corporation not be used in advertising or
00037  * publicity pertaining to distribution of the document or software without
00038  * specific, written prior permission.
00039  *
00040  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00041  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00042  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00043  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00044  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00045  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00046  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00047  * SOFTWARE.
00048  */
00049 
00050 /*
00051  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
00052  *
00053  * Permission to use, copy, modify, and distribute this software for any
00054  * purpose with or without fee is hereby granted, provided that the above
00055  * copyright notice and this permission notice appear in all copies.
00056  *
00057  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00058  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00059  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00060  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00061  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00062  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00063  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00064  * SOFTWARE.
00065  */
00066 
00067 #if defined(LIBC_SCCS) && !defined(lint)
00068 static const char sccsid[] = "@(#)res_comp.c     8.1 (Berkeley) 6/4/93";
00069 static const char rcsid[] = "$BINDId: res_comp.c,v 8.15 1999/10/13 16:39:39 vixie Exp $";
00070 #endif /* LIBC_SCCS and not lint */
00071 
00072 #include <sys/types.h>
00073 #include <sys/param.h>
00074 #include <netinet/in.h>
00075 #include <arpa/nameser.h>
00076 #include <ctype.h>
00077 #include <resolv.h>
00078 #include <stdio.h>
00079 #include <string.h>
00080 #include <unistd.h>
00081 
00082 /*
00083  * Expand compressed domain name 'comp_dn' to full domain name.
00084  * 'msg' is a pointer to the begining of the message,
00085  * 'eomorig' points to the first location after the message,
00086  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
00087  * Return size of compressed name or -1 if there was an error.
00088  */
00089 int
00090 dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
00091          char *dst, int dstsiz)
00092 {
00093        int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
00094 
00095        if (n > 0 && dst[0] == '.')
00096               dst[0] = '\0';
00097        return (n);
00098 }
00099 libresolv_hidden_def (dn_expand)
00100 
00101 /*
00102  * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
00103  * Return the size of the compressed name or -1.
00104  * 'length' is the size of the array pointed to by 'comp_dn'.
00105  */
00106 int
00107 dn_comp(const char *src, u_char *dst, int dstsiz,
00108        u_char **dnptrs, u_char **lastdnptr)
00109 {
00110        return (ns_name_compress(src, dst, (size_t)dstsiz,
00111                              (const u_char **)dnptrs,
00112                              (const u_char **)lastdnptr));
00113 }
00114 libresolv_hidden_def (dn_comp)
00115 
00116 /*
00117  * Skip over a compressed domain name. Return the size or -1.
00118  */
00119 int
00120 dn_skipname(const u_char *ptr, const u_char *eom) {
00121        const u_char *saveptr = ptr;
00122 
00123        if (ns_name_skip(&ptr, eom) == -1)
00124               return (-1);
00125        return (ptr - saveptr);
00126 }
00127 libresolv_hidden_def (dn_skipname)
00128 
00129 /*
00130  * Verify that a domain name uses an acceptable character set.
00131  */
00132 
00133 /*
00134  * Note the conspicuous absence of ctype macros in these definitions.  On
00135  * non-ASCII hosts, we can't depend on string literals or ctype macros to
00136  * tell us anything about network-format data.  The rest of the BIND system
00137  * is not careful about this, but for some reason, we're doing it right here.
00138  */
00139 #define PERIOD 0x2e
00140 #define       hyphenchar(c) ((c) == 0x2d)
00141 #define       underscorechar(c) ((c) == 0x5f)
00142 #define bslashchar(c) ((c) == 0x5c)
00143 #define periodchar(c) ((c) == PERIOD)
00144 #define asterchar(c) ((c) == 0x2a)
00145 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
00146                  || ((c) >= 0x61 && (c) <= 0x7a))
00147 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
00148 
00149 #define borderchar(c) (alphachar(c) || digitchar(c))
00150 #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
00151 #define       domainchar(c) ((c) > 0x20 && (c) < 0x7f)
00152 
00153 int
00154 res_hnok(const char *dn) {
00155        int ppch = '\0', pch = PERIOD, ch = *dn++;
00156 
00157        while (ch != '\0') {
00158               int nch = *dn++;
00159 
00160               if (periodchar(ch)) {
00161                      (void)NULL;
00162               } else if (periodchar(pch)) {
00163                      if (!borderchar(ch))
00164                             return (0);
00165               } else if (periodchar(nch) || nch == '\0') {
00166                      if (!borderchar(ch))
00167                             return (0);
00168               } else {
00169                      if (!middlechar(ch))
00170                             return (0);
00171               }
00172               ppch = pch, pch = ch, ch = nch;
00173        }
00174        return (1);
00175 }
00176 libresolv_hidden_def (res_hnok)
00177 
00178 /*
00179  * hostname-like (A, MX, WKS) owners can have "*" as their first label
00180  * but must otherwise be as a host name.
00181  */
00182 int
00183 res_ownok(const char *dn) {
00184        if (asterchar(dn[0])) {
00185               if (periodchar(dn[1]))
00186                      return (res_hnok(dn+2));
00187               if (dn[1] == '\0')
00188                      return (1);
00189        }
00190        return (res_hnok(dn));
00191 }
00192 
00193 /*
00194  * SOA RNAMEs and RP RNAMEs can have any printable character in their first
00195  * label, but the rest of the name has to look like a host name.
00196  */
00197 int
00198 res_mailok(const char *dn) {
00199        int ch, escaped = 0;
00200 
00201        /* "." is a valid missing representation */
00202        if (*dn == '\0')
00203               return (1);
00204 
00205        /* otherwise <label>.<hostname> */
00206        while ((ch = *dn++) != '\0') {
00207               if (!domainchar(ch))
00208                      return (0);
00209               if (!escaped && periodchar(ch))
00210                      break;
00211               if (escaped)
00212                      escaped = 0;
00213               else if (bslashchar(ch))
00214                      escaped = 1;
00215        }
00216        if (periodchar(ch))
00217               return (res_hnok(dn));
00218        return (0);
00219 }
00220 
00221 /*
00222  * This function is quite liberal, since RFC 1034's character sets are only
00223  * recommendations.
00224  */
00225 int
00226 res_dnok(const char *dn) {
00227        int ch;
00228 
00229        while ((ch = *dn++) != '\0')
00230               if (!domainchar(ch))
00231                      return (0);
00232        return (1);
00233 }
00234 libresolv_hidden_def (res_dnok)
00235 
00236 #ifdef BIND_4_COMPAT
00237 /*
00238  * This module must export the following externally-visible symbols:
00239  *     ___putlong
00240  *     ___putshort
00241  *     __getlong
00242  *     __getshort
00243  * Note that one _ comes from C and the others come from us.
00244  */
00245 void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
00246 libresolv_hidden_def (__putlong)
00247 void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
00248 libresolv_hidden_def (__putshort)
00249 #ifndef __ultrix__
00250 u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
00251 u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
00252 #endif /*__ultrix__*/
00253 #endif /*BIND_4_COMPAT*/
00254 
00255 
00256 #include <shlib-compat.h>
00257 
00258 #if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2)
00259 # undef dn_expand
00260 weak_alias (__dn_expand, dn_expand);
00261 #endif