Blame resolv/res_comp.c

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