15185a700Sflorian /*
25185a700Sflorian * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian *
45185a700Sflorian * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian * copyright notice and this permission notice appear in all copies.
75185a700Sflorian *
85185a700Sflorian * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian */
165185a700Sflorian
17*b73bdc82Sjmc /* $Id: naptr_35.c,v 1.14 2022/12/26 19:24:11 jmc Exp $ */
185185a700Sflorian
195185a700Sflorian /* Reviewed: Thu Mar 16 16:52:50 PST 2000 by bwelling */
205185a700Sflorian
215185a700Sflorian /* RFC2915 */
225185a700Sflorian
235185a700Sflorian #ifndef RDATA_GENERIC_NAPTR_35_C
245185a700Sflorian #define RDATA_GENERIC_NAPTR_35_C
255185a700Sflorian
265185a700Sflorian #include <isc/regex.h>
275185a700Sflorian
285185a700Sflorian /*
295185a700Sflorian * Check the wire format of the Regexp field.
30*b73bdc82Sjmc * Don't allow embedded NUL's.
315185a700Sflorian */
325185a700Sflorian static inline isc_result_t
txt_valid_regex(const unsigned char * txt)335185a700Sflorian txt_valid_regex(const unsigned char *txt) {
345185a700Sflorian unsigned int nsub = 0;
355185a700Sflorian char regex[256];
365185a700Sflorian char *cp;
371fb015a8Sflorian int flags = 0;
381fb015a8Sflorian int replace = 0;
395185a700Sflorian unsigned char c;
405185a700Sflorian unsigned char delim;
415185a700Sflorian unsigned int len;
425185a700Sflorian int n;
435185a700Sflorian
445185a700Sflorian len = *txt++;
455185a700Sflorian if (len == 0U)
465185a700Sflorian return (ISC_R_SUCCESS);
475185a700Sflorian
485185a700Sflorian delim = *txt++;
495185a700Sflorian len--;
505185a700Sflorian
515185a700Sflorian /*
525185a700Sflorian * Digits, backslash and flags can't be delimiters.
535185a700Sflorian */
545185a700Sflorian switch (delim) {
555185a700Sflorian case '0': case '1': case '2': case '3': case '4':
565185a700Sflorian case '5': case '6': case '7': case '8': case '9':
575185a700Sflorian case '\\': case 'i': case 0:
585185a700Sflorian return (DNS_R_SYNTAX);
595185a700Sflorian }
605185a700Sflorian
615185a700Sflorian cp = regex;
625185a700Sflorian while (len-- > 0) {
635185a700Sflorian c = *txt++;
645185a700Sflorian if (c == 0)
655185a700Sflorian return (DNS_R_SYNTAX);
665185a700Sflorian if (c == delim && !replace) {
671fb015a8Sflorian replace = 1;
685185a700Sflorian continue;
695185a700Sflorian } else if (c == delim && !flags) {
701fb015a8Sflorian flags = 1;
715185a700Sflorian continue;
725185a700Sflorian } else if (c == delim)
735185a700Sflorian return (DNS_R_SYNTAX);
745185a700Sflorian /*
755185a700Sflorian * Flags are not escaped.
765185a700Sflorian */
775185a700Sflorian if (flags) {
785185a700Sflorian switch (c) {
795185a700Sflorian case 'i':
805185a700Sflorian continue;
815185a700Sflorian default:
825185a700Sflorian return (DNS_R_SYNTAX);
835185a700Sflorian }
845185a700Sflorian }
855185a700Sflorian if (!replace)
865185a700Sflorian *cp++ = c;
875185a700Sflorian if (c == '\\') {
885185a700Sflorian if (len == 0)
895185a700Sflorian return (DNS_R_SYNTAX);
905185a700Sflorian c = *txt++;
915185a700Sflorian if (c == 0)
925185a700Sflorian return (DNS_R_SYNTAX);
935185a700Sflorian len--;
945185a700Sflorian if (replace)
955185a700Sflorian switch (c) {
965185a700Sflorian case '0': return (DNS_R_SYNTAX);
975185a700Sflorian case '1': if (nsub < 1) nsub = 1; break;
985185a700Sflorian case '2': if (nsub < 2) nsub = 2; break;
995185a700Sflorian case '3': if (nsub < 3) nsub = 3; break;
1005185a700Sflorian case '4': if (nsub < 4) nsub = 4; break;
1015185a700Sflorian case '5': if (nsub < 5) nsub = 5; break;
1025185a700Sflorian case '6': if (nsub < 6) nsub = 6; break;
1035185a700Sflorian case '7': if (nsub < 7) nsub = 7; break;
1045185a700Sflorian case '8': if (nsub < 8) nsub = 8; break;
1055185a700Sflorian case '9': if (nsub < 9) nsub = 9; break;
1065185a700Sflorian }
1075185a700Sflorian if (!replace)
1085185a700Sflorian *cp++ = c;
1095185a700Sflorian }
1105185a700Sflorian }
1115185a700Sflorian if (!flags)
1125185a700Sflorian return (DNS_R_SYNTAX);
1135185a700Sflorian *cp = '\0';
1145185a700Sflorian n = isc_regex_validate(regex);
1155185a700Sflorian if (n < 0 || nsub > (unsigned int)n)
1165185a700Sflorian return (DNS_R_SYNTAX);
1175185a700Sflorian return (ISC_R_SUCCESS);
1185185a700Sflorian }
1195185a700Sflorian
1205185a700Sflorian static inline isc_result_t
totext_naptr(ARGS_TOTEXT)1215185a700Sflorian totext_naptr(ARGS_TOTEXT) {
1225185a700Sflorian isc_region_t region;
1235185a700Sflorian dns_name_t name;
1245185a700Sflorian dns_name_t prefix;
1251fb015a8Sflorian int sub;
1265185a700Sflorian char buf[sizeof("64000")];
1275185a700Sflorian unsigned short num;
1285185a700Sflorian
1295185a700Sflorian REQUIRE(rdata->type == dns_rdatatype_naptr);
1305185a700Sflorian REQUIRE(rdata->length != 0);
1315185a700Sflorian
1325185a700Sflorian dns_name_init(&name, NULL);
1335185a700Sflorian dns_name_init(&prefix, NULL);
1345185a700Sflorian
1355185a700Sflorian dns_rdata_toregion(rdata, ®ion);
1365185a700Sflorian
1375185a700Sflorian /*
1385185a700Sflorian * Order.
1395185a700Sflorian */
1405185a700Sflorian num = uint16_fromregion(®ion);
1415185a700Sflorian isc_region_consume(®ion, 2);
1425185a700Sflorian snprintf(buf, sizeof(buf), "%u", num);
143873f12b9Sflorian RETERR(isc_str_tobuffer(buf, target));
144873f12b9Sflorian RETERR(isc_str_tobuffer(" ", target));
1455185a700Sflorian
1465185a700Sflorian /*
1475185a700Sflorian * Preference.
1485185a700Sflorian */
1495185a700Sflorian num = uint16_fromregion(®ion);
1505185a700Sflorian isc_region_consume(®ion, 2);
1515185a700Sflorian snprintf(buf, sizeof(buf), "%u", num);
152873f12b9Sflorian RETERR(isc_str_tobuffer(buf, target));
153873f12b9Sflorian RETERR(isc_str_tobuffer(" ", target));
1545185a700Sflorian
1555185a700Sflorian /*
1565185a700Sflorian * Flags.
1575185a700Sflorian */
1581fb015a8Sflorian RETERR(txt_totext(®ion, 1, target));
159873f12b9Sflorian RETERR(isc_str_tobuffer(" ", target));
1605185a700Sflorian
1615185a700Sflorian /*
1625185a700Sflorian * Service.
1635185a700Sflorian */
1641fb015a8Sflorian RETERR(txt_totext(®ion, 1, target));
165873f12b9Sflorian RETERR(isc_str_tobuffer(" ", target));
1665185a700Sflorian
1675185a700Sflorian /*
1685185a700Sflorian * Regexp.
1695185a700Sflorian */
1701fb015a8Sflorian RETERR(txt_totext(®ion, 1, target));
171873f12b9Sflorian RETERR(isc_str_tobuffer(" ", target));
1725185a700Sflorian
1735185a700Sflorian /*
1745185a700Sflorian * Replacement.
1755185a700Sflorian */
1765185a700Sflorian dns_name_fromregion(&name, ®ion);
1775185a700Sflorian sub = name_prefix(&name, tctx->origin, &prefix);
1785185a700Sflorian return (dns_name_totext(&prefix, sub, target));
1795185a700Sflorian }
1805185a700Sflorian
1815185a700Sflorian static inline isc_result_t
fromwire_naptr(ARGS_FROMWIRE)1825185a700Sflorian fromwire_naptr(ARGS_FROMWIRE) {
1835185a700Sflorian dns_name_t name;
1845185a700Sflorian isc_region_t sr;
1855185a700Sflorian unsigned char *regex;
1865185a700Sflorian
1875185a700Sflorian REQUIRE(type == dns_rdatatype_naptr);
1885185a700Sflorian
1895185a700Sflorian UNUSED(type);
1905185a700Sflorian UNUSED(rdclass);
1915185a700Sflorian
1925185a700Sflorian dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
1935185a700Sflorian
1945185a700Sflorian dns_name_init(&name, NULL);
1955185a700Sflorian
1965185a700Sflorian /*
1975185a700Sflorian * Order, preference.
1985185a700Sflorian */
1995185a700Sflorian isc_buffer_activeregion(source, &sr);
2005185a700Sflorian if (sr.length < 4)
2015185a700Sflorian return (ISC_R_UNEXPECTEDEND);
202637d8eb6Sflorian RETERR(isc_mem_tobuffer(target, sr.base, 4));
2035185a700Sflorian isc_buffer_forward(source, 4);
2045185a700Sflorian
2055185a700Sflorian /*
2065185a700Sflorian * Flags.
2075185a700Sflorian */
2085185a700Sflorian RETERR(txt_fromwire(source, target));
2095185a700Sflorian
2105185a700Sflorian /*
2115185a700Sflorian * Service.
2125185a700Sflorian */
2135185a700Sflorian RETERR(txt_fromwire(source, target));
2145185a700Sflorian
2155185a700Sflorian /*
2165185a700Sflorian * Regexp.
2175185a700Sflorian */
2185185a700Sflorian regex = isc_buffer_used(target);
2195185a700Sflorian RETERR(txt_fromwire(source, target));
2205185a700Sflorian RETERR(txt_valid_regex(regex));
2215185a700Sflorian
2225185a700Sflorian /*
2235185a700Sflorian * Replacement.
2245185a700Sflorian */
2255185a700Sflorian return (dns_name_fromwire(&name, source, dctx, options, target));
2265185a700Sflorian }
2275185a700Sflorian
2285185a700Sflorian static inline isc_result_t
towire_naptr(ARGS_TOWIRE)2295185a700Sflorian towire_naptr(ARGS_TOWIRE) {
2305185a700Sflorian dns_name_t name;
2315185a700Sflorian dns_offsets_t offsets;
2325185a700Sflorian isc_region_t sr;
2335185a700Sflorian
2345185a700Sflorian REQUIRE(rdata->type == dns_rdatatype_naptr);
2355185a700Sflorian REQUIRE(rdata->length != 0);
2365185a700Sflorian
2375185a700Sflorian dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
2385185a700Sflorian /*
2395185a700Sflorian * Order, preference.
2405185a700Sflorian */
2415185a700Sflorian dns_rdata_toregion(rdata, &sr);
242637d8eb6Sflorian RETERR(isc_mem_tobuffer(target, sr.base, 4));
2435185a700Sflorian isc_region_consume(&sr, 4);
2445185a700Sflorian
2455185a700Sflorian /*
2465185a700Sflorian * Flags.
2475185a700Sflorian */
248637d8eb6Sflorian RETERR(isc_mem_tobuffer(target, sr.base, sr.base[0] + 1));
2495185a700Sflorian isc_region_consume(&sr, sr.base[0] + 1);
2505185a700Sflorian
2515185a700Sflorian /*
2525185a700Sflorian * Service.
2535185a700Sflorian */
254637d8eb6Sflorian RETERR(isc_mem_tobuffer(target, sr.base, sr.base[0] + 1));
2555185a700Sflorian isc_region_consume(&sr, sr.base[0] + 1);
2565185a700Sflorian
2575185a700Sflorian /*
2585185a700Sflorian * Regexp.
2595185a700Sflorian */
260637d8eb6Sflorian RETERR(isc_mem_tobuffer(target, sr.base, sr.base[0] + 1));
2615185a700Sflorian isc_region_consume(&sr, sr.base[0] + 1);
2625185a700Sflorian
2635185a700Sflorian /*
2645185a700Sflorian * Replacement.
2655185a700Sflorian */
2665185a700Sflorian dns_name_init(&name, offsets);
2675185a700Sflorian dns_name_fromregion(&name, &sr);
2685185a700Sflorian return (dns_name_towire(&name, cctx, target));
2695185a700Sflorian }
2705185a700Sflorian
2715185a700Sflorian #endif /* RDATA_GENERIC_NAPTR_35_C */
272