1 /* Syntax checking for DNS domain names.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 /*
20 * Copyright (c) 1985, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 4. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /*
49 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
50 *
51 * Permission to use, copy, modify, and distribute this software for any
52 * purpose with or without fee is hereby granted, provided that the above
53 * copyright notice and this permission notice appear in all copies, and that
54 * the name of Digital Equipment Corporation not be used in advertising or
55 * publicity pertaining to distribution of the document or software without
56 * specific, written prior permission.
57 *
58 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
59 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
61 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
62 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
63 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
64 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
65 * SOFTWARE.
66 */
67
68 /*
69 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
70 *
71 * Permission to use, copy, modify, and distribute this software for any
72 * purpose with or without fee is hereby granted, provided that the above
73 * copyright notice and this permission notice appear in all copies.
74 *
75 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
76 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
77 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
78 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
79 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
80 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
81 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
82 * SOFTWARE.
83 */
84
85 #include <arpa/nameser.h>
86 #include <resolv.h>
87 #include <shlib-compat.h>
88
89 /* Return true if the string consists of printable ASCII characters
90 only. */
91 static bool
printable_string(const char * dn)92 printable_string (const char *dn)
93 {
94 while (true)
95 {
96 char ch = *dn;
97 if (ch == '\0')
98 return true;
99 if (ch <= ' ' || ch > '~')
100 return false;
101 ++dn;
102 }
103 }
104
105 /* Return true if DN points to a name consisting only of [0-9a-zA-Z_-]
106 characters. DN must be in DNS wire format, without
107 compression. */
108 static bool
binary_hnok(const unsigned char * dn)109 binary_hnok (const unsigned char *dn)
110 {
111 while (true)
112 {
113 size_t label_length = *dn;
114 if (label_length == 0)
115 break;
116 ++dn;
117 const unsigned char *label_end = dn + label_length;
118 do
119 {
120 unsigned char ch = *dn;
121 if (!(('0' <= ch && ch <= '9')
122 || ('A' <= ch && ch <= 'Z')
123 || ('a' <= ch && ch <= 'z')
124 || ch == '-' || ch == '_'))
125 return false;
126 ++dn;
127 }
128 while (dn < label_end);
129 }
130 return true;
131 }
132
133 /* Return true if the binary domain name has a first labels which
134 starts with '-'. */
135 static inline bool
binary_leading_dash(const unsigned char * dn)136 binary_leading_dash (const unsigned char *dn)
137 {
138 return dn[0] > 0 && dn[1] == '-';
139 }
140
141 /* Return 1 if res_hnok is a valid host name. Labels must only
142 contain [0-9a-zA-Z_-] characters, and the name must not start with
143 a '-'. The latter is to avoid confusion with program options. */
144 int
___res_hnok(const char * dn)145 ___res_hnok (const char *dn)
146 {
147 unsigned char buf[NS_MAXCDNAME];
148 if (!printable_string (dn)
149 || __ns_name_pton (dn, buf, sizeof (buf)) < 0
150 || binary_leading_dash (buf))
151 return 0;
152 return binary_hnok (buf);
153 }
154 versioned_symbol (libc, ___res_hnok, res_hnok, GLIBC_2_34);
155 versioned_symbol (libc, ___res_hnok, __libc_res_hnok, GLIBC_PRIVATE);
156 libc_hidden_ver (___res_hnok, __libc_res_hnok)
157 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_34)
158 compat_symbol (libresolv, ___res_hnok, __res_hnok, GLIBC_2_0);
159 #endif
160
161 /* Hostname-like (A, MX, WKS) owners can have "*" as their first label
162 but must otherwise be as a host name. */
163 int
___res_ownok(const char * dn)164 ___res_ownok (const char *dn)
165 {
166 unsigned char buf[NS_MAXCDNAME];
167 if (!printable_string (dn)
168 || __ns_name_pton (dn, buf, sizeof (buf)) < 0
169 || binary_leading_dash (buf))
170 return 0;
171 if (buf[0] == 1 && buf [1] == '*')
172 /* Skip over the leading "*." part. */
173 return binary_hnok (buf + 2);
174 else
175 return binary_hnok (buf);
176 }
177 versioned_symbol (libc, ___res_ownok, res_ownok, GLIBC_2_34);
178 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_34)
179 compat_symbol (libresolv, ___res_ownok, __res_ownok, GLIBC_2_0);
180 #endif
181
182 /* SOA RNAMEs and RP RNAMEs can have any byte in their first label,
183 but the rest of the name has to look like a host name. */
184 int
___res_mailok(const char * dn)185 ___res_mailok (const char *dn)
186 {
187 unsigned char buf[NS_MAXCDNAME];
188 if (!printable_string (dn)
189 || __ns_name_pton (dn, buf, sizeof (buf)) < 0)
190 return 0;
191 unsigned char label_length = buf[0];
192 /* "." is a valid missing representation */
193 if (label_length == 0)
194 return 1;
195 /* Skip over the first label. */
196 unsigned char *tail = buf + 1 + label_length;
197 if (*tail == 0)
198 /* More than one label is required (except for "."). */
199 return 0;
200 return binary_hnok (tail);
201 }
202 versioned_symbol (libc, ___res_mailok, res_mailok, GLIBC_2_34);
203 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_34)
204 compat_symbol (libresolv, ___res_mailok, __res_mailok, GLIBC_2_0);
205 #endif
206
207 /* Return 1 if DN is a syntactically valid domain name. Empty names
208 are accepted. */
209 int
___res_dnok(const char * dn)210 ___res_dnok (const char *dn)
211 {
212 unsigned char buf[NS_MAXCDNAME];
213 return printable_string (dn) && __ns_name_pton (dn, buf, sizeof (buf)) >= 0;
214 }
215 versioned_symbol (libc, ___res_dnok, res_dnok, GLIBC_2_34);
216 versioned_symbol (libc, ___res_dnok, __libc_res_dnok, GLIBC_PRIVATE);
217 libc_hidden_ver (___res_dnok, __libc_res_dnok)
218 #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_34)
219 compat_symbol (libresolv, ___res_dnok, __res_dnok, GLIBC_2_0);
220 #endif
221