1 /* Domain name processing functions.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
20 * Copyright (c) 1985, 1993
21 * The Regents of the University of California. All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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.
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
49 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
69 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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
85 #include <sys/types.h>
86 #include <sys/param.h>
87 #include <netinet/in.h>
88 #include <arpa/nameser.h>
96 * Expand compressed domain name 'comp_dn' to full domain name.
97 * 'msg' is a pointer to the beginning of the message,
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.
103 dn_expand(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
104 char *dst
, int dstsiz
)
106 int n
= ns_name_uncompress(msg
, eom
, src
, dst
, (size_t)dstsiz
);
108 if (n
> 0 && dst
[0] == '.')
112 libresolv_hidden_def (dn_expand
)
115 * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
116 * Return the size of the compressed name or -1.
117 * 'length' is the size of the array pointed to by 'comp_dn'.
120 dn_comp(const char *src
, u_char
*dst
, int dstsiz
,
121 u_char
**dnptrs
, u_char
**lastdnptr
)
123 return (ns_name_compress(src
, dst
, (size_t)dstsiz
,
124 (const u_char
**)dnptrs
,
125 (const u_char
**)lastdnptr
));
127 libresolv_hidden_def (dn_comp
)
130 * Skip over a compressed domain name. Return the size or -1.
133 dn_skipname(const u_char
*ptr
, const u_char
*eom
) {
134 const u_char
*saveptr
= ptr
;
136 if (ns_name_skip(&ptr
, eom
) == -1)
138 return (ptr
- saveptr
);
140 libresolv_hidden_def (dn_skipname
)
142 /* Return true if the string consists of printable ASCII characters
145 printable_string (const char *dn
)
152 if (ch
<= ' ' || ch
> '~')
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
162 binary_hnok (const unsigned char *dn
)
166 size_t label_length
= *dn
;
167 if (label_length
== 0)
170 const unsigned char *label_end
= dn
+ label_length
;
173 unsigned char ch
= *dn
;
174 if (!(('0' <= ch
&& ch
<= '9')
175 || ('A' <= ch
&& ch
<= 'Z')
176 || ('a' <= ch
&& ch
<= 'z')
177 || ch
== '-' || ch
== '_'))
181 while (dn
< label_end
);
186 /* Return true if the binary domain name has a first labels which
189 binary_leading_dash (const unsigned char *dn
)
191 return dn
[0] > 0 && dn
[1] == '-';
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. */
198 res_hnok (const char *dn
)
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
))
205 return binary_hnok (buf
);
207 libresolv_hidden_def (res_hnok
)
209 /* Hostname-like (A, MX, WKS) owners can have "*" as their first label
210 but must otherwise be as a host name. */
212 res_ownok (const char *dn
)
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
))
219 if (buf
[0] == 1 && buf
[1] == '*')
220 /* Skip over the leading "*." part. */
221 return binary_hnok (buf
+ 2);
223 return binary_hnok (buf
);
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. */
229 res_mailok (const char *dn
)
231 unsigned char buf
[NS_MAXCDNAME
];
232 if (!printable_string (dn
)
233 || ns_name_pton (dn
, buf
, sizeof (buf
)) < 0)
235 unsigned char label_length
= buf
[0];
236 /* "." is a valid missing representation */
237 if (label_length
== 0)
239 /* Skip over the first label. */
240 unsigned char *tail
= buf
+ 1 + label_length
;
242 /* More than one label is required (except for "."). */
244 return binary_hnok (tail
);
247 /* Return 1 if DN is a syntactically valid domain name. Empty names
250 res_dnok (const char *dn
)
252 unsigned char buf
[NS_MAXCDNAME
];
253 return printable_string (dn
) && ns_name_pton (dn
, buf
, sizeof (buf
)) >= 0;
255 libresolv_hidden_def (res_dnok
)
258 * This module must export the following externally-visible symbols:
263 * Note that one _ comes from C and the others come from us.
265 void __putlong(uint32_t src
, u_char
*dst
) { ns_put32(src
, dst
); }
266 libresolv_hidden_def (__putlong
)
267 void __putshort(uint16_t src
, u_char
*dst
) { ns_put16(src
, dst
); }
268 libresolv_hidden_def (__putshort
)
269 uint32_t _getlong(const u_char
*src
) { return (ns_get32(src
)); }
270 uint16_t _getshort(const u_char
*src
) { return (ns_get16(src
)); }
273 #include <shlib-compat.h>
275 #if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2)
277 weak_alias (__dn_expand
, dn_expand
);