Blame resolv/res_comp.c

Packit Service 82fcde
/* Domain name processing functions.
Packit Service 82fcde
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Copyright (c) 1985, 1993
Packit Service 82fcde
 *    The Regents of the University of California.  All rights reserved.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Redistribution and use in source and binary forms, with or without
Packit Service 82fcde
 * modification, are permitted provided that the following conditions
Packit Service 82fcde
 * are met:
Packit Service 82fcde
 * 1. Redistributions of source code must retain the above copyright
Packit Service 82fcde
 *    notice, this list of conditions and the following disclaimer.
Packit Service 82fcde
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 82fcde
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 82fcde
 *    documentation and/or other materials provided with the distribution.
Packit Service 82fcde
 * 4. Neither the name of the University nor the names of its contributors
Packit Service 82fcde
 *    may be used to endorse or promote products derived from this software
Packit Service 82fcde
 *    without specific prior written permission.
Packit Service 82fcde
 *
Packit Service 82fcde
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit Service 82fcde
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service 82fcde
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service 82fcde
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit Service 82fcde
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service 82fcde
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit Service 82fcde
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service 82fcde
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit Service 82fcde
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit Service 82fcde
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit Service 82fcde
 * SUCH DAMAGE.
Packit Service 82fcde
 */
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Permission to use, copy, modify, and distribute this software for any
Packit Service 82fcde
 * purpose with or without fee is hereby granted, provided that the above
Packit Service 82fcde
 * copyright notice and this permission notice appear in all copies, and that
Packit Service 82fcde
 * the name of Digital Equipment Corporation not be used in advertising or
Packit Service 82fcde
 * publicity pertaining to distribution of the document or software without
Packit Service 82fcde
 * specific, written prior permission.
Packit Service 82fcde
 *
Packit Service 82fcde
 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
Packit Service 82fcde
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
Packit Service 82fcde
 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
Packit Service 82fcde
 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
Packit Service 82fcde
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
Packit Service 82fcde
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
Packit Service 82fcde
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
Packit Service 82fcde
 * SOFTWARE.
Packit Service 82fcde
 */
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Permission to use, copy, modify, and distribute this software for any
Packit Service 82fcde
 * purpose with or without fee is hereby granted, provided that the above
Packit Service 82fcde
 * copyright notice and this permission notice appear in all copies.
Packit Service 82fcde
 *
Packit Service 82fcde
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
Packit Service 82fcde
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
Packit Service 82fcde
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
Packit Service 82fcde
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
Packit Service 82fcde
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
Packit Service 82fcde
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
Packit Service 82fcde
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
Packit Service 82fcde
 * SOFTWARE.
Packit Service 82fcde
 */
Packit Service 82fcde
Packit Service 82fcde
#include <sys/types.h>
Packit Service 82fcde
#include <sys/param.h>
Packit Service 82fcde
#include <netinet/in.h>
Packit Service 82fcde
#include <arpa/nameser.h>
Packit Service 82fcde
#include <ctype.h>
Packit Service 82fcde
#include <resolv.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Expand compressed domain name 'comp_dn' to full domain name.
Packit Service 82fcde
 * 'msg' is a pointer to the beginning of the message,
Packit Service 82fcde
 * 'eomorig' points to the first location after the message,
Packit Service 82fcde
 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
Packit Service 82fcde
 * Return size of compressed name or -1 if there was an error.
Packit Service 82fcde
 */
Packit Service 82fcde
int
Packit Service 82fcde
dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
Packit Service 82fcde
	  char *dst, int dstsiz)
Packit Service 82fcde
{
Packit Service 82fcde
	int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
Packit Service 82fcde
Packit Service 82fcde
	if (n > 0 && dst[0] == '.')
Packit Service 82fcde
		dst[0] = '\0';
Packit Service 82fcde
	return (n);
Packit Service 82fcde
}
Packit Service 82fcde
libresolv_hidden_def (dn_expand)
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
Packit Service 82fcde
 * Return the size of the compressed name or -1.
Packit Service 82fcde
 * 'length' is the size of the array pointed to by 'comp_dn'.
Packit Service 82fcde
 */
Packit Service 82fcde
int
Packit Service 82fcde
dn_comp(const char *src, u_char *dst, int dstsiz,
Packit Service 82fcde
	u_char **dnptrs, u_char **lastdnptr)
Packit Service 82fcde
{
Packit Service 82fcde
	return (ns_name_compress(src, dst, (size_t)dstsiz,
Packit Service 82fcde
				 (const u_char **)dnptrs,
Packit Service 82fcde
				 (const u_char **)lastdnptr));
Packit Service 82fcde
}
Packit Service 82fcde
libresolv_hidden_def (dn_comp)
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * Skip over a compressed domain name. Return the size or -1.
Packit Service 82fcde
 */
Packit Service 82fcde
int
Packit Service 82fcde
dn_skipname(const u_char *ptr, const u_char *eom) {
Packit Service 82fcde
	const u_char *saveptr = ptr;
Packit Service 82fcde
Packit Service 82fcde
	if (ns_name_skip(&ptr, eom) == -1)
Packit Service 82fcde
		return (-1);
Packit Service 82fcde
	return (ptr - saveptr);
Packit Service 82fcde
}
Packit Service 82fcde
libresolv_hidden_def (dn_skipname)
Packit Service 82fcde
Packit Service 82fcde
/* Return true if the string consists of printable ASCII characters
Packit Service 82fcde
   only.  */
Packit Service 82fcde
static bool
Packit Service 82fcde
printable_string (const char *dn)
Packit Service 82fcde
{
Packit Service 82fcde
  while (true)
Packit Service 82fcde
    {
Packit Service 82fcde
      char ch = *dn;
Packit Service 82fcde
      if (ch == '\0')
Packit Service 82fcde
	return true;
Packit Service 82fcde
      if (ch <= ' ' || ch > '~')
Packit Service 82fcde
	return false;
Packit Service 82fcde
      ++dn;
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Return true if DN points to a name consisting only of [0-9a-zA-Z_-]
Packit Service 82fcde
   characters.  DN must be in DNS wire format, without
Packit Service 82fcde
   compression.  */
Packit Service 82fcde
static bool
Packit Service 82fcde
binary_hnok (const unsigned char *dn)
Packit Service 82fcde
{
Packit Service 82fcde
  while (true)
Packit Service 82fcde
    {
Packit Service 82fcde
      size_t label_length = *dn;
Packit Service 82fcde
      if (label_length == 0)
Packit Service 82fcde
	break;
Packit Service 82fcde
      ++dn;
Packit Service 82fcde
      const unsigned char *label_end = dn + label_length;
Packit Service 82fcde
      do
Packit Service 82fcde
	{
Packit Service 82fcde
	  unsigned char ch = *dn;
Packit Service 82fcde
	  if (!(('0' <= ch && ch <= '9')
Packit Service 82fcde
		|| ('A' <= ch && ch <= 'Z')
Packit Service 82fcde
		|| ('a' <= ch && ch <= 'z')
Packit Service 82fcde
		|| ch == '-' || ch == '_'))
Packit Service 82fcde
	    return false;
Packit Service 82fcde
	  ++dn;
Packit Service 82fcde
	}
Packit Service 82fcde
      while (dn < label_end);
Packit Service 82fcde
    }
Packit Service 82fcde
  return true;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Return true if the binary domain name has a first labels which
Packit Service 82fcde
   starts with '-'.  */
Packit Service 82fcde
static inline bool
Packit Service 82fcde
binary_leading_dash (const unsigned char *dn)
Packit Service 82fcde
{
Packit Service 82fcde
  return dn[0] > 0 && dn[1] == '-';
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Return 1 if res_hnok is a valid host name.  Labels must only
Packit Service 82fcde
   contain [0-9a-zA-Z_-] characters, and the name must not start with
Packit Service 82fcde
   a '-'.  The latter is to avoid confusion with program options.  */
Packit Service 82fcde
int
Packit Service 82fcde
res_hnok (const char *dn)
Packit Service 82fcde
{
Packit Service 82fcde
  unsigned char buf[NS_MAXCDNAME];
Packit Service 82fcde
  if (!printable_string (dn)
Packit Service 82fcde
      || ns_name_pton (dn, buf, sizeof (buf)) < 0
Packit Service 82fcde
      || binary_leading_dash (buf))
Packit Service 82fcde
    return 0;
Packit Service 82fcde
  return binary_hnok (buf);
Packit Service 82fcde
}
Packit Service 82fcde
libresolv_hidden_def (res_hnok)
Packit Service 82fcde
Packit Service 82fcde
/* Hostname-like (A, MX, WKS) owners can have "*" as their first label
Packit Service 82fcde
   but must otherwise be as a host name.  */
Packit Service 82fcde
int
Packit Service 82fcde
res_ownok (const char *dn)
Packit Service 82fcde
{
Packit Service 82fcde
  unsigned char buf[NS_MAXCDNAME];
Packit Service 82fcde
  if (!printable_string (dn)
Packit Service 82fcde
      || ns_name_pton (dn, buf, sizeof (buf)) < 0
Packit Service 82fcde
      || binary_leading_dash (buf))
Packit Service 82fcde
    return 0;
Packit Service 82fcde
  if (buf[0] == 1 && buf [1] == '*')
Packit Service 82fcde
    /* Skip over the leading "*." part.  */
Packit Service 82fcde
    return binary_hnok (buf + 2);
Packit Service 82fcde
  else
Packit Service 82fcde
    return binary_hnok (buf);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* SOA RNAMEs and RP RNAMEs can have any byte in their first label,
Packit Service 82fcde
   but the rest of the name has to look like a host name.  */
Packit Service 82fcde
int
Packit Service 82fcde
res_mailok (const char *dn)
Packit Service 82fcde
{
Packit Service 82fcde
  unsigned char buf[NS_MAXCDNAME];
Packit Service 82fcde
  if (!printable_string (dn)
Packit Service 82fcde
      || ns_name_pton (dn, buf, sizeof (buf)) < 0)
Packit Service 82fcde
    return 0;
Packit Service 82fcde
  unsigned char label_length = buf[0];
Packit Service 82fcde
  /* "." is a valid missing representation */
Packit Service 82fcde
  if (label_length == 0)
Packit Service 82fcde
    return 1;
Packit Service 82fcde
  /* Skip over the first label.  */
Packit Service 82fcde
  unsigned char *tail = buf + 1 + label_length;
Packit Service 82fcde
  if (*tail == 0)
Packit Service 82fcde
    /* More than one label is required (except for ".").  */
Packit Service 82fcde
    return 0;
Packit Service 82fcde
  return binary_hnok (tail);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* Return 1 if DN is a syntactically valid domain name.  Empty names
Packit Service 82fcde
   are accepted.  */
Packit Service 82fcde
int
Packit Service 82fcde
res_dnok (const char *dn)
Packit Service 82fcde
{
Packit Service 82fcde
  unsigned char buf[NS_MAXCDNAME];
Packit Service 82fcde
  return printable_string (dn) && ns_name_pton (dn, buf, sizeof (buf)) >= 0;
Packit Service 82fcde
}
Packit Service 82fcde
libresolv_hidden_def (res_dnok)
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * This module must export the following externally-visible symbols:
Packit Service 82fcde
 *	___putlong
Packit Service 82fcde
 *	___putshort
Packit Service 82fcde
 *	__getlong
Packit Service 82fcde
 *	__getshort
Packit Service 82fcde
 * Note that one _ comes from C and the others come from us.
Packit Service 82fcde
 */
Packit Service 82fcde
void __putlong(uint32_t src, u_char *dst) { ns_put32(src, dst); }
Packit Service 82fcde
libresolv_hidden_def (__putlong)
Packit Service 82fcde
void __putshort(uint16_t src, u_char *dst) { ns_put16(src, dst); }
Packit Service 82fcde
libresolv_hidden_def (__putshort)
Packit Service 82fcde
uint32_t _getlong(const u_char *src) { return (ns_get32(src)); }
Packit Service 82fcde
uint16_t _getshort(const u_char *src) { return (ns_get16(src)); }
Packit Service 82fcde

Packit Service 82fcde
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
Packit Service 82fcde
#if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2)
Packit Service 82fcde
# undef dn_expand
Packit Service 82fcde
weak_alias (__dn_expand, dn_expand);
Packit Service 82fcde
#endif