]>
Commit | Line | Data |
---|---|---|
c0a25aa9 | 1 | /* Domain name processing functions. |
04277e02 | 2 | Copyright (C) 1995-2019 Free Software Foundation, Inc. |
c0a25aa9 FW |
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
28f540f4 | 19 | /* |
28f540f4 RM |
20 | * Copyright (c) 1985, 1993 |
21 | * The Regents of the University of California. All rights reserved. | |
6f9d8e68 | 22 | * |
28f540f4 RM |
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. | |
28f540f4 RM |
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. | |
6f9d8e68 | 34 | * |
28f540f4 RM |
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. | |
e685e07d UD |
46 | */ |
47 | ||
48 | /* | |
28f540f4 | 49 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
6f9d8e68 | 50 | * |
28f540f4 RM |
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. | |
6f9d8e68 | 57 | * |
28f540f4 RM |
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. | |
b43b13ac UD |
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. | |
28f540f4 RM |
83 | */ |
84 | ||
df21c858 | 85 | #include <sys/types.h> |
28f540f4 RM |
86 | #include <sys/param.h> |
87 | #include <netinet/in.h> | |
88 | #include <arpa/nameser.h> | |
28f540f4 | 89 | #include <ctype.h> |
66715f83 UD |
90 | #include <resolv.h> |
91 | #include <stdio.h> | |
b43b13ac UD |
92 | #include <string.h> |
93 | #include <unistd.h> | |
28f540f4 | 94 | |
28f540f4 RM |
95 | /* |
96 | * Expand compressed domain name 'comp_dn' to full domain name. | |
6f65e668 | 97 | * 'msg' is a pointer to the beginning of the message, |
28f540f4 RM |
98 | * 'eomorig' points to the first location after the message, |
99 | * 'exp_dn' is a pointer to a buffer of size 'length' for the result. | |
100 | * Return size of compressed name or -1 if there was an error. | |
101 | */ | |
102 | int | |
b43b13ac UD |
103 | dn_expand(const u_char *msg, const u_char *eom, const u_char *src, |
104 | char *dst, int dstsiz) | |
28f540f4 | 105 | { |
66715f83 | 106 | int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); |
28f540f4 | 107 | |
66715f83 UD |
108 | if (n > 0 && dst[0] == '.') |
109 | dst[0] = '\0'; | |
110 | return (n); | |
28f540f4 | 111 | } |
6f9d8e68 | 112 | libresolv_hidden_def (dn_expand) |
28f540f4 RM |
113 | |
114 | /* | |
66715f83 | 115 | * Pack domain name 'exp_dn' in presentation form into 'comp_dn'. |
28f540f4 RM |
116 | * Return the size of the compressed name or -1. |
117 | * 'length' is the size of the array pointed to by 'comp_dn'. | |
28f540f4 RM |
118 | */ |
119 | int | |
b43b13ac UD |
120 | dn_comp(const char *src, u_char *dst, int dstsiz, |
121 | u_char **dnptrs, u_char **lastdnptr) | |
28f540f4 | 122 | { |
66715f83 UD |
123 | return (ns_name_compress(src, dst, (size_t)dstsiz, |
124 | (const u_char **)dnptrs, | |
125 | (const u_char **)lastdnptr)); | |
28f540f4 | 126 | } |
6f9d8e68 | 127 | libresolv_hidden_def (dn_comp) |
28f540f4 RM |
128 | |
129 | /* | |
130 | * Skip over a compressed domain name. Return the size or -1. | |
131 | */ | |
132 | int | |
b43b13ac | 133 | dn_skipname(const u_char *ptr, const u_char *eom) { |
66715f83 | 134 | const u_char *saveptr = ptr; |
28f540f4 | 135 | |
66715f83 UD |
136 | if (ns_name_skip(&ptr, eom) == -1) |
137 | return (-1); | |
138 | return (ptr - saveptr); | |
28f540f4 | 139 | } |
6f9d8e68 | 140 | libresolv_hidden_def (dn_skipname) |
28f540f4 | 141 | |
c0a25aa9 FW |
142 | /* Return true if the string consists of printable ASCII characters |
143 | only. */ | |
144 | static bool | |
145 | printable_string (const char *dn) | |
146 | { | |
147 | while (true) | |
148 | { | |
149 | char ch = *dn; | |
150 | if (ch == '\0') | |
151 | return true; | |
152 | if (ch <= ' ' || ch > '~') | |
153 | return false; | |
154 | ++dn; | |
155 | } | |
156 | } | |
55707265 | 157 | |
c0a25aa9 FW |
158 | /* Return true if DN points to a name consisting only of [0-9a-zA-Z_-] |
159 | characters. DN must be in DNS wire format, without | |
160 | compression. */ | |
161 | static bool | |
162 | binary_hnok (const unsigned char *dn) | |
163 | { | |
164 | while (true) | |
165 | { | |
166 | size_t label_length = *dn; | |
167 | if (label_length == 0) | |
168 | break; | |
169 | ++dn; | |
170 | const unsigned char *label_end = dn + label_length; | |
171 | do | |
172 | { | |
173 | unsigned char ch = *dn; | |
174 | if (!(('0' <= ch && ch <= '9') | |
175 | || ('A' <= ch && ch <= 'Z') | |
176 | || ('a' <= ch && ch <= 'z') | |
177 | || ch == '-' || ch == '_')) | |
178 | return false; | |
179 | ++dn; | |
180 | } | |
181 | while (dn < label_end); | |
182 | } | |
183 | return true; | |
184 | } | |
185 | ||
186 | /* Return true if the binary domain name has a first labels which | |
187 | starts with '-'. */ | |
188 | static inline bool | |
189 | binary_leading_dash (const unsigned char *dn) | |
190 | { | |
191 | return dn[0] > 0 && dn[1] == '-'; | |
192 | } | |
55707265 | 193 | |
c0a25aa9 FW |
194 | /* Return 1 if res_hnok is a valid host name. Labels must only |
195 | contain [0-9a-zA-Z_-] characters, and the name must not start with | |
196 | a '-'. The latter is to avoid confusion with program options. */ | |
55707265 | 197 | int |
c0a25aa9 FW |
198 | res_hnok (const char *dn) |
199 | { | |
200 | unsigned char buf[NS_MAXCDNAME]; | |
201 | if (!printable_string (dn) | |
202 | || ns_name_pton (dn, buf, sizeof (buf)) < 0 | |
203 | || binary_leading_dash (buf)) | |
204 | return 0; | |
205 | return binary_hnok (buf); | |
55707265 | 206 | } |
6f9d8e68 | 207 | libresolv_hidden_def (res_hnok) |
55707265 | 208 | |
c0a25aa9 FW |
209 | /* Hostname-like (A, MX, WKS) owners can have "*" as their first label |
210 | but must otherwise be as a host name. */ | |
fa0bc87c | 211 | int |
c0a25aa9 FW |
212 | res_ownok (const char *dn) |
213 | { | |
214 | unsigned char buf[NS_MAXCDNAME]; | |
215 | if (!printable_string (dn) | |
216 | || ns_name_pton (dn, buf, sizeof (buf)) < 0 | |
217 | || binary_leading_dash (buf)) | |
218 | return 0; | |
219 | if (buf[0] == 1 && buf [1] == '*') | |
220 | /* Skip over the leading "*." part. */ | |
221 | return binary_hnok (buf + 2); | |
222 | else | |
223 | return binary_hnok (buf); | |
fa0bc87c RM |
224 | } |
225 | ||
c0a25aa9 FW |
226 | /* SOA RNAMEs and RP RNAMEs can have any byte in their first label, |
227 | but the rest of the name has to look like a host name. */ | |
fa0bc87c | 228 | int |
c0a25aa9 FW |
229 | res_mailok (const char *dn) |
230 | { | |
231 | unsigned char buf[NS_MAXCDNAME]; | |
232 | if (!printable_string (dn) | |
233 | || ns_name_pton (dn, buf, sizeof (buf)) < 0) | |
234 | return 0; | |
235 | unsigned char label_length = buf[0]; | |
236 | /* "." is a valid missing representation */ | |
237 | if (label_length == 0) | |
238 | return 1; | |
239 | /* Skip over the first label. */ | |
240 | unsigned char *tail = buf + 1 + label_length; | |
241 | if (*tail == 0) | |
242 | /* More than one label is required (except for "."). */ | |
243 | return 0; | |
244 | return binary_hnok (tail); | |
fa0bc87c RM |
245 | } |
246 | ||
c0a25aa9 FW |
247 | /* Return 1 if DN is a syntactically valid domain name. Empty names |
248 | are accepted. */ | |
55707265 | 249 | int |
c0a25aa9 FW |
250 | res_dnok (const char *dn) |
251 | { | |
252 | unsigned char buf[NS_MAXCDNAME]; | |
253 | return printable_string (dn) && ns_name_pton (dn, buf, sizeof (buf)) >= 0; | |
55707265 | 254 | } |
6f9d8e68 | 255 | libresolv_hidden_def (res_dnok) |
55707265 | 256 | |
28f540f4 | 257 | /* |
b43b13ac UD |
258 | * This module must export the following externally-visible symbols: |
259 | * ___putlong | |
260 | * ___putshort | |
261 | * __getlong | |
262 | * __getshort | |
263 | * Note that one _ comes from C and the others come from us. | |
28f540f4 | 264 | */ |
d9fee042 | 265 | void __putlong(uint32_t src, u_char *dst) { ns_put32(src, dst); } |
6f9d8e68 | 266 | libresolv_hidden_def (__putlong) |
d9fee042 | 267 | void __putshort(uint16_t src, u_char *dst) { ns_put16(src, dst); } |
6f9d8e68 | 268 | libresolv_hidden_def (__putshort) |
d9fee042 JM |
269 | uint32_t _getlong(const u_char *src) { return (ns_get32(src)); } |
270 | uint16_t _getshort(const u_char *src) { return (ns_get16(src)); } | |
e685e07d UD |
271 | \f |
272 | ||
273 | #include <shlib-compat.h> | |
274 | ||
275 | #if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2) | |
276 | # undef dn_expand | |
277 | weak_alias (__dn_expand, dn_expand); | |
278 | #endif |