1 /*	$NetBSD: mx_15.c,v 1.5 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009, 2012  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1998-2001, 2003  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: mx_15.c,v 1.58 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* reviewed: Wed Mar 15 18:05:46 PST 2000 by brister */
23 
24 #ifndef RDATA_GENERIC_MX_15_C
25 #define RDATA_GENERIC_MX_15_C
26 
27 #include <string.h>
28 
29 #include <isc/net.h>
30 
31 #define RRTYPE_MX_ATTRIBUTES (0)
32 
33 static isc_boolean_t
34 check_mx(isc_token_t *token) {
35 	char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
36 	struct in_addr addr;
37 	struct in6_addr addr6;
38 
39 	if (strlcpy(tmp, DNS_AS_STR(*token), sizeof(tmp)) >= sizeof(tmp))
40 		return (ISC_TRUE);
41 
42 	if (tmp[strlen(tmp) - 1] == '.')
43 		tmp[strlen(tmp) - 1] = '\0';
44 	if (inet_aton(tmp, &addr) == 1 ||
45 	    inet_pton(AF_INET6, tmp, &addr6) == 1)
46 		return (ISC_FALSE);
47 
48 	return (ISC_TRUE);
49 }
50 
51 static inline isc_result_t
52 fromtext_mx(ARGS_FROMTEXT) {
53 	isc_token_t token;
54 	dns_name_t name;
55 	isc_buffer_t buffer;
56 	isc_boolean_t ok;
57 
58 	REQUIRE(type == 15);
59 
60 	UNUSED(type);
61 	UNUSED(rdclass);
62 
63 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
64 				      ISC_FALSE));
65 	if (token.value.as_ulong > 0xffffU)
66 		RETTOK(ISC_R_RANGE);
67 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
68 
69 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
70 				      ISC_FALSE));
71 
72 	ok = ISC_TRUE;
73 	if ((options & DNS_RDATA_CHECKMX) != 0)
74 		ok = check_mx(&token);
75 	if (!ok && (options & DNS_RDATA_CHECKMXFAIL) != 0)
76 		RETTOK(DNS_R_MXISADDRESS);
77 	if (!ok && callbacks != NULL)
78 		warn_badmx(&token, lexer, callbacks);
79 
80 	dns_name_init(&name, NULL);
81 	buffer_fromregion(&buffer, &token.value.as_region);
82 	origin = (origin != NULL) ? origin : dns_rootname;
83 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
84 	ok = ISC_TRUE;
85 	if ((options & DNS_RDATA_CHECKNAMES) != 0)
86 		ok = dns_name_ishostname(&name, ISC_FALSE);
87 	if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0)
88 		RETTOK(DNS_R_BADNAME);
89 	if (!ok && callbacks != NULL)
90 		warn_badname(&name, lexer, callbacks);
91 	return (ISC_R_SUCCESS);
92 }
93 
94 static inline isc_result_t
95 totext_mx(ARGS_TOTEXT) {
96 	isc_region_t region;
97 	dns_name_t name;
98 	dns_name_t prefix;
99 	isc_boolean_t sub;
100 	char buf[sizeof("64000")];
101 	unsigned short num;
102 
103 	REQUIRE(rdata->type == 15);
104 	REQUIRE(rdata->length != 0);
105 
106 	dns_name_init(&name, NULL);
107 	dns_name_init(&prefix, NULL);
108 
109 	dns_rdata_toregion(rdata, &region);
110 	num = uint16_fromregion(&region);
111 	isc_region_consume(&region, 2);
112 	sprintf(buf, "%u", num);
113 	RETERR(str_totext(buf, target));
114 
115 	RETERR(str_totext(" ", target));
116 
117 	dns_name_fromregion(&name, &region);
118 	sub = name_prefix(&name, tctx->origin, &prefix);
119 	return (dns_name_totext(&prefix, sub, target));
120 }
121 
122 static inline isc_result_t
123 fromwire_mx(ARGS_FROMWIRE) {
124 	dns_name_t name;
125 	isc_region_t sregion;
126 
127 	REQUIRE(type == 15);
128 
129 	UNUSED(type);
130 	UNUSED(rdclass);
131 
132 	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
133 
134 	dns_name_init(&name, NULL);
135 
136 	isc_buffer_activeregion(source, &sregion);
137 	if (sregion.length < 2)
138 		return (ISC_R_UNEXPECTEDEND);
139 	RETERR(mem_tobuffer(target, sregion.base, 2));
140 	isc_buffer_forward(source, 2);
141 	return (dns_name_fromwire(&name, source, dctx, options, target));
142 }
143 
144 static inline isc_result_t
145 towire_mx(ARGS_TOWIRE) {
146 	dns_name_t name;
147 	dns_offsets_t offsets;
148 	isc_region_t region;
149 
150 	REQUIRE(rdata->type == 15);
151 	REQUIRE(rdata->length != 0);
152 
153 	dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
154 
155 	dns_rdata_toregion(rdata, &region);
156 	RETERR(mem_tobuffer(target, region.base, 2));
157 	isc_region_consume(&region, 2);
158 
159 	dns_name_init(&name, offsets);
160 	dns_name_fromregion(&name, &region);
161 
162 	return (dns_name_towire(&name, cctx, target));
163 }
164 
165 static inline int
166 compare_mx(ARGS_COMPARE) {
167 	dns_name_t name1;
168 	dns_name_t name2;
169 	isc_region_t region1;
170 	isc_region_t region2;
171 	int order;
172 
173 	REQUIRE(rdata1->type == rdata2->type);
174 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
175 	REQUIRE(rdata1->type == 15);
176 	REQUIRE(rdata1->length != 0);
177 	REQUIRE(rdata2->length != 0);
178 
179 	order = memcmp(rdata1->data, rdata2->data, 2);
180 	if (order != 0)
181 		return (order < 0 ? -1 : 1);
182 
183 	dns_name_init(&name1, NULL);
184 	dns_name_init(&name2, NULL);
185 
186 	dns_rdata_toregion(rdata1, &region1);
187 	dns_rdata_toregion(rdata2, &region2);
188 
189 	isc_region_consume(&region1, 2);
190 	isc_region_consume(&region2, 2);
191 
192 	dns_name_fromregion(&name1, &region1);
193 	dns_name_fromregion(&name2, &region2);
194 
195 	return (dns_name_rdatacompare(&name1, &name2));
196 }
197 
198 static inline isc_result_t
199 fromstruct_mx(ARGS_FROMSTRUCT) {
200 	dns_rdata_mx_t *mx = source;
201 	isc_region_t region;
202 
203 	REQUIRE(type == 15);
204 	REQUIRE(source != NULL);
205 	REQUIRE(mx->common.rdtype == type);
206 	REQUIRE(mx->common.rdclass == rdclass);
207 
208 	UNUSED(type);
209 	UNUSED(rdclass);
210 
211 	RETERR(uint16_tobuffer(mx->pref, target));
212 	dns_name_toregion(&mx->mx, &region);
213 	return (isc_buffer_copyregion(target, &region));
214 }
215 
216 static inline isc_result_t
217 tostruct_mx(ARGS_TOSTRUCT) {
218 	isc_region_t region;
219 	dns_rdata_mx_t *mx = target;
220 	dns_name_t name;
221 
222 	REQUIRE(rdata->type == 15);
223 	REQUIRE(target != NULL);
224 	REQUIRE(rdata->length != 0);
225 
226 	mx->common.rdclass = rdata->rdclass;
227 	mx->common.rdtype = rdata->type;
228 	ISC_LINK_INIT(&mx->common, link);
229 
230 	dns_name_init(&name, NULL);
231 	dns_rdata_toregion(rdata, &region);
232 	mx->pref = uint16_fromregion(&region);
233 	isc_region_consume(&region, 2);
234 	dns_name_fromregion(&name, &region);
235 	dns_name_init(&mx->mx, NULL);
236 	RETERR(name_duporclone(&name, mctx, &mx->mx));
237 	mx->mctx = mctx;
238 	return (ISC_R_SUCCESS);
239 }
240 
241 static inline void
242 freestruct_mx(ARGS_FREESTRUCT) {
243 	dns_rdata_mx_t *mx = source;
244 
245 	REQUIRE(source != NULL);
246 	REQUIRE(mx->common.rdtype == 15);
247 
248 	if (mx->mctx == NULL)
249 		return;
250 
251 	dns_name_free(&mx->mx, mx->mctx);
252 	mx->mctx = NULL;
253 }
254 
255 static inline isc_result_t
256 additionaldata_mx(ARGS_ADDLDATA) {
257 	dns_name_t name;
258 	dns_offsets_t offsets;
259 	isc_region_t region;
260 
261 	REQUIRE(rdata->type == 15);
262 
263 	dns_name_init(&name, offsets);
264 	dns_rdata_toregion(rdata, &region);
265 	isc_region_consume(&region, 2);
266 	dns_name_fromregion(&name, &region);
267 
268 	return ((add)(arg, &name, dns_rdatatype_a));
269 }
270 
271 static inline isc_result_t
272 digest_mx(ARGS_DIGEST) {
273 	isc_region_t r1, r2;
274 	dns_name_t name;
275 
276 	REQUIRE(rdata->type == 15);
277 
278 	dns_rdata_toregion(rdata, &r1);
279 	r2 = r1;
280 	isc_region_consume(&r2, 2);
281 	r1.length = 2;
282 	RETERR((digest)(arg, &r1));
283 	dns_name_init(&name, NULL);
284 	dns_name_fromregion(&name, &r2);
285 	return (dns_name_digest(&name, digest, arg));
286 }
287 
288 static inline isc_boolean_t
289 checkowner_mx(ARGS_CHECKOWNER) {
290 
291 	REQUIRE(type == 15);
292 
293 	UNUSED(type);
294 	UNUSED(rdclass);
295 
296 	return (dns_name_ishostname(name, wildcard));
297 }
298 
299 static inline isc_boolean_t
300 checknames_mx(ARGS_CHECKNAMES) {
301 	isc_region_t region;
302 	dns_name_t name;
303 
304 	REQUIRE(rdata->type == 15);
305 
306 	UNUSED(owner);
307 
308 	dns_rdata_toregion(rdata, &region);
309 	isc_region_consume(&region, 2);
310 	dns_name_init(&name, NULL);
311 	dns_name_fromregion(&name, &region);
312 	if (!dns_name_ishostname(&name, ISC_FALSE)) {
313 		if (bad != NULL)
314 			dns_name_clone(&name, bad);
315 		return (ISC_FALSE);
316 	}
317 	return (ISC_TRUE);
318 }
319 
320 static inline int
321 casecompare_mx(ARGS_COMPARE) {
322 	return (compare_mx(rdata1, rdata2));
323 }
324 
325 #endif	/* RDATA_GENERIC_MX_15_C */
326