xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/dns/rdata/generic/rp_17.c (revision 4afad4b7fa6d4a0d3dedf41d1587a7250710ae54)
1 /*	$NetBSD: rp_17.c,v 1.1 2024/02/18 20:57:44 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 /* RFC1183 */
17 
18 #ifndef RDATA_GENERIC_RP_17_C
19 #define RDATA_GENERIC_RP_17_C
20 
21 #define RRTYPE_RP_ATTRIBUTES (0)
22 
23 static isc_result_t
fromtext_rp(ARGS_FROMTEXT)24 fromtext_rp(ARGS_FROMTEXT) {
25 	isc_token_t token;
26 	dns_name_t name;
27 	isc_buffer_t buffer;
28 	int i;
29 	bool ok;
30 
31 	REQUIRE(type == dns_rdatatype_rp);
32 
33 	UNUSED(type);
34 	UNUSED(rdclass);
35 	UNUSED(callbacks);
36 
37 	if (origin == NULL) {
38 		origin = dns_rootname;
39 	}
40 
41 	for (i = 0; i < 2; i++) {
42 		RETERR(isc_lex_getmastertoken(lexer, &token,
43 					      isc_tokentype_string, false));
44 		dns_name_init(&name, NULL);
45 		buffer_fromregion(&buffer, &token.value.as_region);
46 		RETTOK(dns_name_fromtext(&name, &buffer, origin, options,
47 					 target));
48 		ok = true;
49 		if ((options & DNS_RDATA_CHECKNAMES) != 0 && i == 0) {
50 			ok = dns_name_ismailbox(&name);
51 		}
52 		if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0) {
53 			RETTOK(DNS_R_BADNAME);
54 		}
55 		if (!ok && callbacks != NULL) {
56 			warn_badname(&name, lexer, callbacks);
57 		}
58 	}
59 	return (ISC_R_SUCCESS);
60 }
61 
62 static isc_result_t
totext_rp(ARGS_TOTEXT)63 totext_rp(ARGS_TOTEXT) {
64 	isc_region_t region;
65 	dns_name_t rmail;
66 	dns_name_t email;
67 	dns_name_t prefix;
68 	bool sub;
69 
70 	REQUIRE(rdata->type == dns_rdatatype_rp);
71 	REQUIRE(rdata->length != 0);
72 
73 	dns_name_init(&rmail, NULL);
74 	dns_name_init(&email, NULL);
75 	dns_name_init(&prefix, NULL);
76 
77 	dns_rdata_toregion(rdata, &region);
78 
79 	dns_name_fromregion(&rmail, &region);
80 	isc_region_consume(&region, rmail.length);
81 
82 	dns_name_fromregion(&email, &region);
83 	isc_region_consume(&region, email.length);
84 
85 	sub = name_prefix(&rmail, tctx->origin, &prefix);
86 	RETERR(dns_name_totext(&prefix, sub, target));
87 
88 	RETERR(str_totext(" ", target));
89 
90 	sub = name_prefix(&email, tctx->origin, &prefix);
91 	return (dns_name_totext(&prefix, sub, target));
92 }
93 
94 static isc_result_t
fromwire_rp(ARGS_FROMWIRE)95 fromwire_rp(ARGS_FROMWIRE) {
96 	dns_name_t rmail;
97 	dns_name_t email;
98 
99 	REQUIRE(type == dns_rdatatype_rp);
100 
101 	UNUSED(type);
102 	UNUSED(rdclass);
103 
104 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
105 
106 	dns_name_init(&rmail, NULL);
107 	dns_name_init(&email, NULL);
108 
109 	RETERR(dns_name_fromwire(&rmail, source, dctx, options, target));
110 	return (dns_name_fromwire(&email, source, dctx, options, target));
111 }
112 
113 static isc_result_t
towire_rp(ARGS_TOWIRE)114 towire_rp(ARGS_TOWIRE) {
115 	isc_region_t region;
116 	dns_name_t rmail;
117 	dns_name_t email;
118 	dns_offsets_t roffsets;
119 	dns_offsets_t eoffsets;
120 
121 	REQUIRE(rdata->type == dns_rdatatype_rp);
122 	REQUIRE(rdata->length != 0);
123 
124 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
125 	dns_name_init(&rmail, roffsets);
126 	dns_name_init(&email, eoffsets);
127 
128 	dns_rdata_toregion(rdata, &region);
129 
130 	dns_name_fromregion(&rmail, &region);
131 	isc_region_consume(&region, rmail.length);
132 
133 	RETERR(dns_name_towire(&rmail, cctx, target));
134 
135 	dns_name_fromregion(&rmail, &region);
136 	isc_region_consume(&region, rmail.length);
137 
138 	return (dns_name_towire(&rmail, cctx, target));
139 }
140 
141 static int
compare_rp(ARGS_COMPARE)142 compare_rp(ARGS_COMPARE) {
143 	isc_region_t region1;
144 	isc_region_t region2;
145 	dns_name_t name1;
146 	dns_name_t name2;
147 	int order;
148 
149 	REQUIRE(rdata1->type == rdata2->type);
150 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
151 	REQUIRE(rdata1->type == dns_rdatatype_rp);
152 	REQUIRE(rdata1->length != 0);
153 	REQUIRE(rdata2->length != 0);
154 
155 	dns_name_init(&name1, NULL);
156 	dns_name_init(&name2, NULL);
157 
158 	dns_rdata_toregion(rdata1, &region1);
159 	dns_rdata_toregion(rdata2, &region2);
160 
161 	dns_name_fromregion(&name1, &region1);
162 	dns_name_fromregion(&name2, &region2);
163 
164 	order = dns_name_rdatacompare(&name1, &name2);
165 	if (order != 0) {
166 		return (order);
167 	}
168 
169 	isc_region_consume(&region1, name_length(&name1));
170 	isc_region_consume(&region2, name_length(&name2));
171 
172 	dns_name_init(&name1, NULL);
173 	dns_name_init(&name2, NULL);
174 
175 	dns_name_fromregion(&name1, &region1);
176 	dns_name_fromregion(&name2, &region2);
177 
178 	return (dns_name_rdatacompare(&name1, &name2));
179 }
180 
181 static isc_result_t
fromstruct_rp(ARGS_FROMSTRUCT)182 fromstruct_rp(ARGS_FROMSTRUCT) {
183 	dns_rdata_rp_t *rp = source;
184 	isc_region_t region;
185 
186 	REQUIRE(type == dns_rdatatype_rp);
187 	REQUIRE(rp != NULL);
188 	REQUIRE(rp->common.rdtype == type);
189 	REQUIRE(rp->common.rdclass == rdclass);
190 
191 	UNUSED(type);
192 	UNUSED(rdclass);
193 
194 	dns_name_toregion(&rp->mail, &region);
195 	RETERR(isc_buffer_copyregion(target, &region));
196 	dns_name_toregion(&rp->text, &region);
197 	return (isc_buffer_copyregion(target, &region));
198 }
199 
200 static isc_result_t
tostruct_rp(ARGS_TOSTRUCT)201 tostruct_rp(ARGS_TOSTRUCT) {
202 	isc_result_t result;
203 	isc_region_t region;
204 	dns_rdata_rp_t *rp = target;
205 	dns_name_t name;
206 
207 	REQUIRE(rdata->type == dns_rdatatype_rp);
208 	REQUIRE(rp != NULL);
209 	REQUIRE(rdata->length != 0);
210 
211 	rp->common.rdclass = rdata->rdclass;
212 	rp->common.rdtype = rdata->type;
213 	ISC_LINK_INIT(&rp->common, link);
214 
215 	dns_name_init(&name, NULL);
216 	dns_rdata_toregion(rdata, &region);
217 	dns_name_fromregion(&name, &region);
218 	dns_name_init(&rp->mail, NULL);
219 	RETERR(name_duporclone(&name, mctx, &rp->mail));
220 	isc_region_consume(&region, name_length(&name));
221 	dns_name_fromregion(&name, &region);
222 	dns_name_init(&rp->text, NULL);
223 	result = name_duporclone(&name, mctx, &rp->text);
224 	if (result != ISC_R_SUCCESS) {
225 		goto cleanup;
226 	}
227 
228 	rp->mctx = mctx;
229 	return (ISC_R_SUCCESS);
230 
231 cleanup:
232 	if (mctx != NULL) {
233 		dns_name_free(&rp->mail, mctx);
234 	}
235 	return (ISC_R_NOMEMORY);
236 }
237 
238 static void
freestruct_rp(ARGS_FREESTRUCT)239 freestruct_rp(ARGS_FREESTRUCT) {
240 	dns_rdata_rp_t *rp = source;
241 
242 	REQUIRE(rp != NULL);
243 	REQUIRE(rp->common.rdtype == dns_rdatatype_rp);
244 
245 	if (rp->mctx == NULL) {
246 		return;
247 	}
248 
249 	dns_name_free(&rp->mail, rp->mctx);
250 	dns_name_free(&rp->text, rp->mctx);
251 	rp->mctx = NULL;
252 }
253 
254 static isc_result_t
additionaldata_rp(ARGS_ADDLDATA)255 additionaldata_rp(ARGS_ADDLDATA) {
256 	REQUIRE(rdata->type == dns_rdatatype_rp);
257 
258 	UNUSED(rdata);
259 	UNUSED(add);
260 	UNUSED(arg);
261 
262 	return (ISC_R_SUCCESS);
263 }
264 
265 static isc_result_t
digest_rp(ARGS_DIGEST)266 digest_rp(ARGS_DIGEST) {
267 	isc_region_t r;
268 	dns_name_t name;
269 
270 	REQUIRE(rdata->type == dns_rdatatype_rp);
271 
272 	dns_rdata_toregion(rdata, &r);
273 	dns_name_init(&name, NULL);
274 
275 	dns_name_fromregion(&name, &r);
276 	RETERR(dns_name_digest(&name, digest, arg));
277 	isc_region_consume(&r, name_length(&name));
278 
279 	dns_name_init(&name, NULL);
280 	dns_name_fromregion(&name, &r);
281 
282 	return (dns_name_digest(&name, digest, arg));
283 }
284 
285 static bool
checkowner_rp(ARGS_CHECKOWNER)286 checkowner_rp(ARGS_CHECKOWNER) {
287 	REQUIRE(type == dns_rdatatype_rp);
288 
289 	UNUSED(name);
290 	UNUSED(type);
291 	UNUSED(rdclass);
292 	UNUSED(wildcard);
293 
294 	return (true);
295 }
296 
297 static bool
checknames_rp(ARGS_CHECKNAMES)298 checknames_rp(ARGS_CHECKNAMES) {
299 	isc_region_t region;
300 	dns_name_t name;
301 
302 	REQUIRE(rdata->type == dns_rdatatype_rp);
303 
304 	UNUSED(owner);
305 
306 	dns_rdata_toregion(rdata, &region);
307 	dns_name_init(&name, NULL);
308 	dns_name_fromregion(&name, &region);
309 	if (!dns_name_ismailbox(&name)) {
310 		if (bad != NULL) {
311 			dns_name_clone(&name, bad);
312 		}
313 		return (false);
314 	}
315 	return (true);
316 }
317 
318 static int
casecompare_rp(ARGS_COMPARE)319 casecompare_rp(ARGS_COMPARE) {
320 	return (compare_rp(rdata1, rdata2));
321 }
322 #endif /* RDATA_GENERIC_RP_17_C */
323