vendor/BIND: Update to 9.5.2-P3
[dragonfly.git] / contrib / bind / lib / dns / rcode.c
blob135c3eec06545cffa8dbfb4941a8da1ce7ebcf66
1 /*
2 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: rcode.c,v 1.6 2007/06/19 23:47:16 tbox Exp $ */
20 #include <config.h>
21 #include <ctype.h>
23 #include <isc/buffer.h>
24 #include <isc/parseint.h>
25 #include <isc/print.h>
26 #include <isc/region.h>
27 #include <isc/result.h>
28 #include <isc/stdio.h>
29 #include <isc/stdlib.h>
30 #include <isc/string.h>
31 #include <isc/types.h>
32 #include <isc/util.h>
34 #include <dns/cert.h>
35 #include <dns/keyflags.h>
36 #include <dns/keyvalues.h>
37 #include <dns/rcode.h>
38 #include <dns/rdataclass.h>
39 #include <dns/result.h>
40 #include <dns/secalg.h>
41 #include <dns/secproto.h>
43 #define RETERR(x) \
44 do { \
45 isc_result_t _r = (x); \
46 if (_r != ISC_R_SUCCESS) \
47 return (_r); \
48 } while (0)
50 #define NUMBERSIZE sizeof("037777777777") /* 2^32-1 octal + NUL */
52 #define RCODENAMES \
53 /* standard rcodes */ \
54 { dns_rcode_noerror, "NOERROR", 0}, \
55 { dns_rcode_formerr, "FORMERR", 0}, \
56 { dns_rcode_servfail, "SERVFAIL", 0}, \
57 { dns_rcode_nxdomain, "NXDOMAIN", 0}, \
58 { dns_rcode_notimp, "NOTIMP", 0}, \
59 { dns_rcode_refused, "REFUSED", 0}, \
60 { dns_rcode_yxdomain, "YXDOMAIN", 0}, \
61 { dns_rcode_yxrrset, "YXRRSET", 0}, \
62 { dns_rcode_nxrrset, "NXRRSET", 0}, \
63 { dns_rcode_notauth, "NOTAUTH", 0}, \
64 { dns_rcode_notzone, "NOTZONE", 0},
66 #define ERCODENAMES \
67 /* extended rcodes */ \
68 { dns_rcode_badvers, "BADVERS", 0}, \
69 { 0, NULL, 0 }
71 #define TSIGRCODENAMES \
72 /* extended rcodes */ \
73 { dns_tsigerror_badsig, "BADSIG", 0}, \
74 { dns_tsigerror_badkey, "BADKEY", 0}, \
75 { dns_tsigerror_badtime, "BADTIME", 0}, \
76 { dns_tsigerror_badmode, "BADMODE", 0}, \
77 { dns_tsigerror_badname, "BADNAME", 0}, \
78 { dns_tsigerror_badalg, "BADALG", 0}, \
79 { dns_tsigerror_badtrunc, "BADTRUNC", 0}, \
80 { 0, NULL, 0 }
82 /* RFC2538 section 2.1 */
84 #define CERTNAMES \
85 { 1, "PKIX", 0}, \
86 { 2, "SPKI", 0}, \
87 { 3, "PGP", 0}, \
88 { 253, "URI", 0}, \
89 { 254, "OID", 0}, \
90 { 0, NULL, 0}
92 /* RFC2535 section 7, RFC3110 */
94 #define SECALGNAMES \
95 { DNS_KEYALG_RSAMD5, "RSAMD5", 0 }, \
96 { DNS_KEYALG_RSAMD5, "RSA", 0 }, \
97 { DNS_KEYALG_DH, "DH", 0 }, \
98 { DNS_KEYALG_DSA, "DSA", 0 }, \
99 { DNS_KEYALG_ECC, "ECC", 0 }, \
100 { DNS_KEYALG_RSASHA1, "RSASHA1", 0 }, \
101 { DNS_KEYALG_INDIRECT, "INDIRECT", 0 }, \
102 { DNS_KEYALG_PRIVATEDNS, "PRIVATEDNS", 0 }, \
103 { DNS_KEYALG_PRIVATEOID, "PRIVATEOID", 0 }, \
104 { 0, NULL, 0}
106 /* RFC2535 section 7.1 */
108 #define SECPROTONAMES \
109 { 0, "NONE", 0 }, \
110 { 1, "TLS", 0 }, \
111 { 2, "EMAIL", 0 }, \
112 { 3, "DNSSEC", 0 }, \
113 { 4, "IPSEC", 0 }, \
114 { 255, "ALL", 0 }, \
115 { 0, NULL, 0}
117 struct tbl {
118 unsigned int value;
119 const char *name;
120 int flags;
123 static struct tbl rcodes[] = { RCODENAMES ERCODENAMES };
124 static struct tbl tsigrcodes[] = { RCODENAMES TSIGRCODENAMES };
125 static struct tbl certs[] = { CERTNAMES };
126 static struct tbl secalgs[] = { SECALGNAMES };
127 static struct tbl secprotos[] = { SECPROTONAMES };
129 static struct keyflag {
130 const char *name;
131 unsigned int value;
132 unsigned int mask;
133 } keyflags[] = {
134 { "NOCONF", 0x4000, 0xC000 },
135 { "NOAUTH", 0x8000, 0xC000 },
136 { "NOKEY", 0xC000, 0xC000 },
137 { "FLAG2", 0x2000, 0x2000 },
138 { "EXTEND", 0x1000, 0x1000 },
139 { "FLAG4", 0x0800, 0x0800 },
140 { "FLAG5", 0x0400, 0x0400 },
141 { "USER", 0x0000, 0x0300 },
142 { "ZONE", 0x0100, 0x0300 },
143 { "HOST", 0x0200, 0x0300 },
144 { "NTYP3", 0x0300, 0x0300 },
145 { "FLAG8", 0x0080, 0x0080 },
146 { "FLAG9", 0x0040, 0x0040 },
147 { "FLAG10", 0x0020, 0x0020 },
148 { "FLAG11", 0x0010, 0x0010 },
149 { "SIG0", 0x0000, 0x000F },
150 { "SIG1", 0x0001, 0x000F },
151 { "SIG2", 0x0002, 0x000F },
152 { "SIG3", 0x0003, 0x000F },
153 { "SIG4", 0x0004, 0x000F },
154 { "SIG5", 0x0005, 0x000F },
155 { "SIG6", 0x0006, 0x000F },
156 { "SIG7", 0x0007, 0x000F },
157 { "SIG8", 0x0008, 0x000F },
158 { "SIG9", 0x0009, 0x000F },
159 { "SIG10", 0x000A, 0x000F },
160 { "SIG11", 0x000B, 0x000F },
161 { "SIG12", 0x000C, 0x000F },
162 { "SIG13", 0x000D, 0x000F },
163 { "SIG14", 0x000E, 0x000F },
164 { "SIG15", 0x000F, 0x000F },
165 { "KSK", DNS_KEYFLAG_KSK, DNS_KEYFLAG_KSK },
166 { NULL, 0, 0 }
169 static isc_result_t
170 str_totext(const char *source, isc_buffer_t *target) {
171 unsigned int l;
172 isc_region_t region;
174 isc_buffer_availableregion(target, &region);
175 l = strlen(source);
177 if (l > region.length)
178 return (ISC_R_NOSPACE);
180 memcpy(region.base, source, l);
181 isc_buffer_add(target, l);
182 return (ISC_R_SUCCESS);
185 static isc_result_t
186 maybe_numeric(unsigned int *valuep, isc_textregion_t *source,
187 unsigned int max, isc_boolean_t hex_allowed)
189 isc_result_t result;
190 isc_uint32_t n;
191 char buffer[NUMBERSIZE];
193 if (! isdigit(source->base[0] & 0xff) ||
194 source->length > NUMBERSIZE - 1)
195 return (ISC_R_BADNUMBER);
198 * We have a potential number. Try to parse it with
199 * isc_parse_uint32(). isc_parse_uint32() requires
200 * null termination, so we must make a copy.
202 strncpy(buffer, source->base, NUMBERSIZE);
203 INSIST(buffer[source->length] == '\0');
205 result = isc_parse_uint32(&n, buffer, 10);
206 if (result == ISC_R_BADNUMBER && hex_allowed)
207 result = isc_parse_uint32(&n, buffer, 16);
208 if (result != ISC_R_SUCCESS)
209 return (result);
210 if (n > max)
211 return (ISC_R_RANGE);
212 *valuep = n;
213 return (ISC_R_SUCCESS);
216 static isc_result_t
217 dns_mnemonic_fromtext(unsigned int *valuep, isc_textregion_t *source,
218 struct tbl *table, unsigned int max)
220 isc_result_t result;
221 int i;
223 result = maybe_numeric(valuep, source, max, ISC_FALSE);
224 if (result != ISC_R_BADNUMBER)
225 return (result);
227 for (i = 0; table[i].name != NULL; i++) {
228 unsigned int n;
229 n = strlen(table[i].name);
230 if (n == source->length &&
231 strncasecmp(source->base, table[i].name, n) == 0) {
232 *valuep = table[i].value;
233 return (ISC_R_SUCCESS);
236 return (DNS_R_UNKNOWN);
239 static isc_result_t
240 dns_mnemonic_totext(unsigned int value, isc_buffer_t *target,
241 struct tbl *table)
243 int i = 0;
244 char buf[sizeof("4294967296")];
245 while (table[i].name != NULL) {
246 if (table[i].value == value) {
247 return (str_totext(table[i].name, target));
249 i++;
251 snprintf(buf, sizeof(buf), "%u", value);
252 return (str_totext(buf, target));
255 isc_result_t
256 dns_rcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) {
257 unsigned int value;
258 RETERR(dns_mnemonic_fromtext(&value, source, rcodes, 0xffff));
259 *rcodep = value;
260 return (ISC_R_SUCCESS);
263 isc_result_t
264 dns_rcode_totext(dns_rcode_t rcode, isc_buffer_t *target) {
265 return (dns_mnemonic_totext(rcode, target, rcodes));
268 isc_result_t
269 dns_tsigrcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) {
270 unsigned int value;
271 RETERR(dns_mnemonic_fromtext(&value, source, tsigrcodes, 0xffff));
272 *rcodep = value;
273 return (ISC_R_SUCCESS);
276 isc_result_t
277 dns_tsigrcode_totext(dns_rcode_t rcode, isc_buffer_t *target) {
278 return (dns_mnemonic_totext(rcode, target, tsigrcodes));
281 isc_result_t
282 dns_cert_fromtext(dns_cert_t *certp, isc_textregion_t *source) {
283 unsigned int value;
284 RETERR(dns_mnemonic_fromtext(&value, source, certs, 0xffff));
285 *certp = value;
286 return (ISC_R_SUCCESS);
289 isc_result_t
290 dns_cert_totext(dns_cert_t cert, isc_buffer_t *target) {
291 return (dns_mnemonic_totext(cert, target, certs));
294 isc_result_t
295 dns_secalg_fromtext(dns_secalg_t *secalgp, isc_textregion_t *source) {
296 unsigned int value;
297 RETERR(dns_mnemonic_fromtext(&value, source, secalgs, 0xff));
298 *secalgp = value;
299 return (ISC_R_SUCCESS);
302 isc_result_t
303 dns_secalg_totext(dns_secalg_t secalg, isc_buffer_t *target) {
304 return (dns_mnemonic_totext(secalg, target, secalgs));
307 isc_result_t
308 dns_secproto_fromtext(dns_secproto_t *secprotop, isc_textregion_t *source) {
309 unsigned int value;
310 RETERR(dns_mnemonic_fromtext(&value, source, secprotos, 0xff));
311 *secprotop = value;
312 return (ISC_R_SUCCESS);
315 isc_result_t
316 dns_secproto_totext(dns_secproto_t secproto, isc_buffer_t *target) {
317 return (dns_mnemonic_totext(secproto, target, secprotos));
320 isc_result_t
321 dns_keyflags_fromtext(dns_keyflags_t *flagsp, isc_textregion_t *source)
323 isc_result_t result;
324 char *text, *end;
325 unsigned int value, mask;
327 result = maybe_numeric(&value, source, 0xffff, ISC_TRUE);
328 if (result == ISC_R_SUCCESS) {
329 *flagsp = value;
330 return (ISC_R_SUCCESS);
332 if (result != ISC_R_BADNUMBER)
333 return (result);
335 text = source->base;
336 end = source->base + source->length;
337 value = mask = 0;
339 while (text < end) {
340 struct keyflag *p;
341 unsigned int len;
342 char *delim = memchr(text, '|', end - text);
343 if (delim != NULL)
344 len = delim - text;
345 else
346 len = end - text;
347 for (p = keyflags; p->name != NULL; p++) {
348 if (strncasecmp(p->name, text, len) == 0)
349 break;
351 if (p->name == NULL)
352 return (DNS_R_UNKNOWNFLAG);
353 value |= p->value;
354 #ifdef notyet
355 if ((mask & p->mask) != 0)
356 warn("overlapping key flags");
357 #endif
358 mask |= p->mask;
359 text += len;
360 if (delim != NULL)
361 text++; /* Skip "|" */
363 *flagsp = value;
364 return (ISC_R_SUCCESS);
368 * This uses lots of hard coded values, but how often do we actually
369 * add classes?
371 isc_result_t
372 dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) {
373 #define COMPARE(string, rdclass) \
374 if (((sizeof(string) - 1) == source->length) \
375 && (strncasecmp(source->base, string, source->length) == 0)) { \
376 *classp = rdclass; \
377 return (ISC_R_SUCCESS); \
380 switch (tolower((unsigned char)source->base[0])) {
381 case 'a':
382 COMPARE("any", dns_rdataclass_any);
383 break;
384 case 'c':
386 * RFC1035 says the mnemonic for the CHAOS class is CH,
387 * but historical BIND practice is to call it CHAOS.
388 * We will accept both forms, but only generate CH.
390 COMPARE("ch", dns_rdataclass_chaos);
391 COMPARE("chaos", dns_rdataclass_chaos);
393 if (source->length > 5 &&
394 source->length < (5 + sizeof("65000")) &&
395 strncasecmp("class", source->base, 5) == 0) {
396 char buf[sizeof("65000")];
397 char *endp;
398 unsigned int val;
400 strncpy(buf, source->base + 5, source->length - 5);
401 buf[source->length - 5] = '\0';
402 val = strtoul(buf, &endp, 10);
403 if (*endp == '\0' && val <= 0xffff) {
404 *classp = (dns_rdataclass_t)val;
405 return (ISC_R_SUCCESS);
408 break;
409 case 'h':
410 COMPARE("hs", dns_rdataclass_hs);
411 COMPARE("hesiod", dns_rdataclass_hs);
412 break;
413 case 'i':
414 COMPARE("in", dns_rdataclass_in);
415 break;
416 case 'n':
417 COMPARE("none", dns_rdataclass_none);
418 break;
419 case 'r':
420 COMPARE("reserved0", dns_rdataclass_reserved0);
421 break;
424 #undef COMPARE
426 return (DNS_R_UNKNOWN);
429 isc_result_t
430 dns_rdataclass_totext(dns_rdataclass_t rdclass, isc_buffer_t *target) {
431 char buf[sizeof("CLASS65535")];
433 switch (rdclass) {
434 case dns_rdataclass_any:
435 return (str_totext("ANY", target));
436 case dns_rdataclass_chaos:
437 return (str_totext("CH", target));
438 case dns_rdataclass_hs:
439 return (str_totext("HS", target));
440 case dns_rdataclass_in:
441 return (str_totext("IN", target));
442 case dns_rdataclass_none:
443 return (str_totext("NONE", target));
444 case dns_rdataclass_reserved0:
445 return (str_totext("RESERVED0", target));
446 default:
447 snprintf(buf, sizeof(buf), "CLASS%u", rdclass);
448 return (str_totext(buf, target));
452 void
453 dns_rdataclass_format(dns_rdataclass_t rdclass,
454 char *array, unsigned int size)
456 isc_result_t result;
457 isc_buffer_t buf;
459 isc_buffer_init(&buf, array, size);
460 result = dns_rdataclass_totext(rdclass, &buf);
462 * Null terminate.
464 if (result == ISC_R_SUCCESS) {
465 if (isc_buffer_availablelength(&buf) >= 1)
466 isc_buffer_putuint8(&buf, 0);
467 else
468 result = ISC_R_NOSPACE;
470 if (result != ISC_R_SUCCESS) {
471 snprintf(array, size, "<unknown>");
472 array[size - 1] = '\0';