dns-type.c revision d0129ddb9fbb07bed7c8ea51b8031f824bf506fb
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/***
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani This file is part of systemd.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Copyright 2014 Zbigniew Jędrzejewski-Szmek
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani systemd is free software; you can redistribute it and/or modify it
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani under the terms of the GNU Lesser General Public License as published by
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani the Free Software Foundation; either version 2.1 of the License, or
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani (at your option) any later version.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani systemd is distributed in the hope that it will be useful, but
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani WITHOUT ANY WARRANTY; without even the implied warranty of
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Lesser General Public License for more details.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani You should have received a copy of the GNU Lesser General Public License
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani along with systemd; If not, see <http://www.gnu.org/licenses/>.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani***/
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "dns-type.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "string-util.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanitypedef const struct {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani uint16_t type;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const char *name;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani} dns_type;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic const struct dns_type_name *
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanilookup_dns_type (register const char *str, register unsigned int len);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "dns_type-from-name.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "dns_type-to-name.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniint dns_type_from_string(const char *s) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const struct dns_type_name *sc;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert(s);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani sc = lookup_dns_type(s, strlen(s));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!sc)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return _DNS_TYPE_INVALID;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return sc->id;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanibool dns_type_is_pseudo(uint16_t type) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Checks whether the specified type is a "pseudo-type". What
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * a "pseudo-type" precisely is, is defined only very weakly,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * but apparently entails all RR types that are not actually
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * stored as RRs on the server and should hence also not be
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * cached. We use this list primarily to validate NSEC type
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * bitfields, and to verify what to cache. */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return IN_SET(type,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani 0, /* A Pseudo RR type, according to RFC 2931 */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_ANY,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_AXFR,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_IXFR,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_OPT,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_TSIG,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_TKEY
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani );
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanibool dns_class_is_pseudo(uint16_t class) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return class == DNS_TYPE_ANY;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanibool dns_type_is_valid_query(uint16_t type) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* The types valid as questions in packets */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return !IN_SET(type,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani 0,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_OPT,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_TSIG,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_TKEY);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanibool dns_type_is_valid_rr(uint16_t type) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* The types valid as RR in packets (but not necessarily
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * stored on servers). */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return !IN_SET(type,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_ANY,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_AXFR,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_IXFR);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanibool dns_class_is_valid_rr(uint16_t class) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return class != DNS_CLASS_ANY;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanibool dns_type_may_redirect(uint16_t type) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* The following record types should never be redirected using
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * CNAME/DNAME RRs. See
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani * <https://tools.ietf.org/html/rfc4035#section-2.5>. */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (dns_type_is_pseudo(type))
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return false;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return !IN_SET(type,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_CNAME,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_DNAME,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_NSEC3,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_NSEC,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_RRSIG,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_NXT,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_SIG,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_KEY);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanibool dns_type_is_dnssec(uint16_t type) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return IN_SET(type,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_DS,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_DNSKEY,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_RRSIG,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_NSEC,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_NSEC3,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_NSEC3PARAM);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanibool dns_type_is_obsolete(uint16_t type) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return IN_SET(type,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Obsoleted by RFC 973 */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_MD,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_MF,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_MAILA,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Kinda obsoleted by RFC 2505 */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_MB,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_MG,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_MR,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_MINFO,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_MAILB,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* RFC1127 kinda obsoleted this by recommending against its use */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_WKS,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Declared historical by RFC 6563 */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_A6,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* Obsoleted by DNSSEC-bis */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_NXT,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani /* RFC 1035 removed support for concepts that needed this from RFC 883 */
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani DNS_TYPE_NULL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniconst char *dns_class_to_string(uint16_t class) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani switch (class) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani case DNS_CLASS_IN:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return "IN";
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani case DNS_CLASS_ANY:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return "ANY";
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return NULL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniint dns_class_from_string(const char *s) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!s)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return _DNS_CLASS_INVALID;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (strcaseeq(s, "IN"))
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return DNS_CLASS_IN;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani else if (strcaseeq(s, "ANY"))
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return DNS_CLASS_ANY;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return _DNS_CLASS_INVALID;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani