Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rcode.c
blobe750bab013a11b9cffc678993858b97d43f8405d
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003 Internet Software Consortium.
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.
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.
20 /* Id: rcode.c,v 1.12 2009/10/22 02:21:30 each Exp */
22 #include <config.h>
23 #include <ctype.h>
25 #include <isc/buffer.h>
26 #include <isc/parseint.h>
27 #include <isc/print.h>
28 #include <isc/region.h>
29 #include <isc/result.h>
30 #include <isc/stdio.h>
31 #include <isc/stdlib.h>
32 #include <isc/string.h>
33 #include <isc/types.h>
34 #include <isc/util.h>
36 #include <dns/cert.h>
37 #include <dns/keyflags.h>
38 #include <dns/keyvalues.h>
39 #include <dns/rcode.h>
40 #include <dns/rdataclass.h>
41 #include <dns/result.h>
42 #include <dns/secalg.h>
43 #include <dns/secproto.h>
45 #define RETERR(x) \
46 do { \
47 isc_result_t _r = (x); \
48 if (_r != ISC_R_SUCCESS) \
49 return (_r); \
50 } while (0)
52 #define NUMBERSIZE sizeof("037777777777") /* 2^32-1 octal + NUL */
54 #define RCODENAMES \
55 /* standard rcodes */ \
56 { dns_rcode_noerror, "NOERROR", 0}, \
57 { dns_rcode_formerr, "FORMERR", 0}, \
58 { dns_rcode_servfail, "SERVFAIL", 0}, \
59 { dns_rcode_nxdomain, "NXDOMAIN", 0}, \
60 { dns_rcode_notimp, "NOTIMP", 0}, \
61 { dns_rcode_refused, "REFUSED", 0}, \
62 { dns_rcode_yxdomain, "YXDOMAIN", 0}, \
63 { dns_rcode_yxrrset, "YXRRSET", 0}, \
64 { dns_rcode_nxrrset, "NXRRSET", 0}, \
65 { dns_rcode_notauth, "NOTAUTH", 0}, \
66 { dns_rcode_notzone, "NOTZONE", 0},
68 #define ERCODENAMES \
69 /* extended rcodes */ \
70 { dns_rcode_badvers, "BADVERS", 0}, \
71 { 0, NULL, 0 }
73 #define TSIGRCODENAMES \
74 /* extended rcodes */ \
75 { dns_tsigerror_badsig, "BADSIG", 0}, \
76 { dns_tsigerror_badkey, "BADKEY", 0}, \
77 { dns_tsigerror_badtime, "BADTIME", 0}, \
78 { dns_tsigerror_badmode, "BADMODE", 0}, \
79 { dns_tsigerror_badname, "BADNAME", 0}, \
80 { dns_tsigerror_badalg, "BADALG", 0}, \
81 { dns_tsigerror_badtrunc, "BADTRUNC", 0}, \
82 { 0, NULL, 0 }
84 /* RFC4398 section 2.1 */
86 #define CERTNAMES \
87 { 1, "PKIX", 0}, \
88 { 2, "SPKI", 0}, \
89 { 3, "PGP", 0}, \
90 { 4, "IPKIX", 0}, \
91 { 5, "ISPKI", 0}, \
92 { 6, "IPGP", 0}, \
93 { 7, "ACPKIX", 0}, \
94 { 8, "IACPKIX", 0}, \
95 { 253, "URI", 0}, \
96 { 254, "OID", 0}, \
97 { 0, NULL, 0}
99 /* RFC2535 section 7, RFC3110 */
101 #define SECALGNAMES \
102 { DNS_KEYALG_RSAMD5, "RSAMD5", 0 }, \
103 { DNS_KEYALG_RSAMD5, "RSA", 0 }, \
104 { DNS_KEYALG_DH, "DH", 0 }, \
105 { DNS_KEYALG_DSA, "DSA", 0 }, \
106 { DNS_KEYALG_NSEC3DSA, "NSEC3DSA", 0 }, \
107 { DNS_KEYALG_ECC, "ECC", 0 }, \
108 { DNS_KEYALG_RSASHA1, "RSASHA1", 0 }, \
109 { DNS_KEYALG_NSEC3RSASHA1, "NSEC3RSASHA1", 0 }, \
110 { DNS_KEYALG_RSASHA256, "RSASHA256", 0 }, \
111 { DNS_KEYALG_RSASHA512, "RSASHA512", 0 }, \
112 { DNS_KEYALG_INDIRECT, "INDIRECT", 0 }, \
113 { DNS_KEYALG_PRIVATEDNS, "PRIVATEDNS", 0 }, \
114 { DNS_KEYALG_PRIVATEOID, "PRIVATEOID", 0 }, \
115 { 0, NULL, 0}
117 /* RFC2535 section 7.1 */
119 #define SECPROTONAMES \
120 { 0, "NONE", 0 }, \
121 { 1, "TLS", 0 }, \
122 { 2, "EMAIL", 0 }, \
123 { 3, "DNSSEC", 0 }, \
124 { 4, "IPSEC", 0 }, \
125 { 255, "ALL", 0 }, \
126 { 0, NULL, 0}
128 #define HASHALGNAMES \
129 { 1, "SHA-1", 0 }, \
130 { 0, NULL, 0 }
132 struct tbl {
133 unsigned int value;
134 const char *name;
135 int flags;
138 static struct tbl rcodes[] = { RCODENAMES ERCODENAMES };
139 static struct tbl tsigrcodes[] = { RCODENAMES TSIGRCODENAMES };
140 static struct tbl certs[] = { CERTNAMES };
141 static struct tbl secalgs[] = { SECALGNAMES };
142 static struct tbl secprotos[] = { SECPROTONAMES };
143 static struct tbl hashalgs[] = { HASHALGNAMES };
145 static struct keyflag {
146 const char *name;
147 unsigned int value;
148 unsigned int mask;
149 } keyflags[] = {
150 { "NOCONF", 0x4000, 0xC000 },
151 { "NOAUTH", 0x8000, 0xC000 },
152 { "NOKEY", 0xC000, 0xC000 },
153 { "FLAG2", 0x2000, 0x2000 },
154 { "EXTEND", 0x1000, 0x1000 },
155 { "FLAG4", 0x0800, 0x0800 },
156 { "FLAG5", 0x0400, 0x0400 },
157 { "USER", 0x0000, 0x0300 },
158 { "ZONE", 0x0100, 0x0300 },
159 { "HOST", 0x0200, 0x0300 },
160 { "NTYP3", 0x0300, 0x0300 },
161 { "FLAG8", 0x0080, 0x0080 },
162 { "FLAG9", 0x0040, 0x0040 },
163 { "FLAG10", 0x0020, 0x0020 },
164 { "FLAG11", 0x0010, 0x0010 },
165 { "SIG0", 0x0000, 0x000F },
166 { "SIG1", 0x0001, 0x000F },
167 { "SIG2", 0x0002, 0x000F },
168 { "SIG3", 0x0003, 0x000F },
169 { "SIG4", 0x0004, 0x000F },
170 { "SIG5", 0x0005, 0x000F },
171 { "SIG6", 0x0006, 0x000F },
172 { "SIG7", 0x0007, 0x000F },
173 { "SIG8", 0x0008, 0x000F },
174 { "SIG9", 0x0009, 0x000F },
175 { "SIG10", 0x000A, 0x000F },
176 { "SIG11", 0x000B, 0x000F },
177 { "SIG12", 0x000C, 0x000F },
178 { "SIG13", 0x000D, 0x000F },
179 { "SIG14", 0x000E, 0x000F },
180 { "SIG15", 0x000F, 0x000F },
181 { "KSK", DNS_KEYFLAG_KSK, DNS_KEYFLAG_KSK },
182 { NULL, 0, 0 }
185 static isc_result_t
186 str_totext(const char *source, isc_buffer_t *target) {
187 unsigned int l;
188 isc_region_t region;
190 isc_buffer_availableregion(target, &region);
191 l = strlen(source);
193 if (l > region.length)
194 return (ISC_R_NOSPACE);
196 memcpy(region.base, source, l);
197 isc_buffer_add(target, l);
198 return (ISC_R_SUCCESS);
201 static isc_result_t
202 maybe_numeric(unsigned int *valuep, isc_textregion_t *source,
203 unsigned int max, isc_boolean_t hex_allowed)
205 isc_result_t result;
206 isc_uint32_t n;
207 char buffer[NUMBERSIZE];
209 if (! isdigit(source->base[0] & 0xff) ||
210 source->length > NUMBERSIZE - 1)
211 return (ISC_R_BADNUMBER);
214 * We have a potential number. Try to parse it with
215 * isc_parse_uint32(). isc_parse_uint32() requires
216 * null termination, so we must make a copy.
218 strncpy(buffer, source->base, NUMBERSIZE);
219 INSIST(buffer[source->length] == '\0');
221 result = isc_parse_uint32(&n, buffer, 10);
222 if (result == ISC_R_BADNUMBER && hex_allowed)
223 result = isc_parse_uint32(&n, buffer, 16);
224 if (result != ISC_R_SUCCESS)
225 return (result);
226 if (n > max)
227 return (ISC_R_RANGE);
228 *valuep = n;
229 return (ISC_R_SUCCESS);
232 static isc_result_t
233 dns_mnemonic_fromtext(unsigned int *valuep, isc_textregion_t *source,
234 struct tbl *table, unsigned int max)
236 isc_result_t result;
237 int i;
239 result = maybe_numeric(valuep, source, max, ISC_FALSE);
240 if (result != ISC_R_BADNUMBER)
241 return (result);
243 for (i = 0; table[i].name != NULL; i++) {
244 unsigned int n;
245 n = strlen(table[i].name);
246 if (n == source->length &&
247 strncasecmp(source->base, table[i].name, n) == 0) {
248 *valuep = table[i].value;
249 return (ISC_R_SUCCESS);
252 return (DNS_R_UNKNOWN);
255 static isc_result_t
256 dns_mnemonic_totext(unsigned int value, isc_buffer_t *target,
257 struct tbl *table)
259 int i = 0;
260 char buf[sizeof("4294967296")];
261 while (table[i].name != NULL) {
262 if (table[i].value == value) {
263 return (str_totext(table[i].name, target));
265 i++;
267 snprintf(buf, sizeof(buf), "%u", value);
268 return (str_totext(buf, target));
271 isc_result_t
272 dns_rcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) {
273 unsigned int value;
274 RETERR(dns_mnemonic_fromtext(&value, source, rcodes, 0xffff));
275 *rcodep = value;
276 return (ISC_R_SUCCESS);
279 isc_result_t
280 dns_rcode_totext(dns_rcode_t rcode, isc_buffer_t *target) {
281 return (dns_mnemonic_totext(rcode, target, rcodes));
284 isc_result_t
285 dns_tsigrcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) {
286 unsigned int value;
287 RETERR(dns_mnemonic_fromtext(&value, source, tsigrcodes, 0xffff));
288 *rcodep = value;
289 return (ISC_R_SUCCESS);
292 isc_result_t
293 dns_tsigrcode_totext(dns_rcode_t rcode, isc_buffer_t *target) {
294 return (dns_mnemonic_totext(rcode, target, tsigrcodes));
297 isc_result_t
298 dns_cert_fromtext(dns_cert_t *certp, isc_textregion_t *source) {
299 unsigned int value;
300 RETERR(dns_mnemonic_fromtext(&value, source, certs, 0xffff));
301 *certp = value;
302 return (ISC_R_SUCCESS);
305 isc_result_t
306 dns_cert_totext(dns_cert_t cert, isc_buffer_t *target) {
307 return (dns_mnemonic_totext(cert, target, certs));
310 isc_result_t
311 dns_secalg_fromtext(dns_secalg_t *secalgp, isc_textregion_t *source) {
312 unsigned int value;
313 RETERR(dns_mnemonic_fromtext(&value, source, secalgs, 0xff));
314 *secalgp = value;
315 return (ISC_R_SUCCESS);
318 isc_result_t
319 dns_secalg_totext(dns_secalg_t secalg, isc_buffer_t *target) {
320 return (dns_mnemonic_totext(secalg, target, secalgs));
323 void
324 dns_secalg_format(dns_secalg_t alg, char *cp, unsigned int size) {
325 isc_buffer_t b;
326 isc_region_t r;
327 isc_result_t result;
329 REQUIRE(cp != NULL && size > 0);
330 isc_buffer_init(&b, cp, size - 1);
331 result = dns_secalg_totext(alg, &b);
332 isc_buffer_usedregion(&b, &r);
333 r.base[r.length] = 0;
334 if (result != ISC_R_SUCCESS)
335 r.base[0] = 0;
338 isc_result_t
339 dns_secproto_fromtext(dns_secproto_t *secprotop, isc_textregion_t *source) {
340 unsigned int value;
341 RETERR(dns_mnemonic_fromtext(&value, source, secprotos, 0xff));
342 *secprotop = value;
343 return (ISC_R_SUCCESS);
346 isc_result_t
347 dns_secproto_totext(dns_secproto_t secproto, isc_buffer_t *target) {
348 return (dns_mnemonic_totext(secproto, target, secprotos));
351 isc_result_t
352 dns_hashalg_fromtext(unsigned char *hashalg, isc_textregion_t *source) {
353 unsigned int value;
354 RETERR(dns_mnemonic_fromtext(&value, source, hashalgs, 0xff));
355 *hashalg = value;
356 return (ISC_R_SUCCESS);
359 isc_result_t
360 dns_keyflags_fromtext(dns_keyflags_t *flagsp, isc_textregion_t *source)
362 isc_result_t result;
363 char *text, *end;
364 unsigned int value, mask;
366 result = maybe_numeric(&value, source, 0xffff, ISC_TRUE);
367 if (result == ISC_R_SUCCESS) {
368 *flagsp = value;
369 return (ISC_R_SUCCESS);
371 if (result != ISC_R_BADNUMBER)
372 return (result);
374 text = source->base;
375 end = source->base + source->length;
376 value = mask = 0;
378 while (text < end) {
379 struct keyflag *p;
380 unsigned int len;
381 char *delim = memchr(text, '|', end - text);
382 if (delim != NULL)
383 len = delim - text;
384 else
385 len = end - text;
386 for (p = keyflags; p->name != NULL; p++) {
387 if (strncasecmp(p->name, text, len) == 0)
388 break;
390 if (p->name == NULL)
391 return (DNS_R_UNKNOWNFLAG);
392 value |= p->value;
393 #ifdef notyet
394 if ((mask & p->mask) != 0)
395 warn("overlapping key flags");
396 #endif
397 mask |= p->mask;
398 text += len;
399 if (delim != NULL)
400 text++; /* Skip "|" */
402 *flagsp = value;
403 return (ISC_R_SUCCESS);
407 * This uses lots of hard coded values, but how often do we actually
408 * add classes?
410 isc_result_t
411 dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) {
412 #define COMPARE(string, rdclass) \
413 if (((sizeof(string) - 1) == source->length) \
414 && (strncasecmp(source->base, string, source->length) == 0)) { \
415 *classp = rdclass; \
416 return (ISC_R_SUCCESS); \
419 switch (tolower((unsigned char)source->base[0])) {
420 case 'a':
421 COMPARE("any", dns_rdataclass_any);
422 break;
423 case 'c':
425 * RFC1035 says the mnemonic for the CHAOS class is CH,
426 * but historical BIND practice is to call it CHAOS.
427 * We will accept both forms, but only generate CH.
429 COMPARE("ch", dns_rdataclass_chaos);
430 COMPARE("chaos", dns_rdataclass_chaos);
432 if (source->length > 5 &&
433 source->length < (5 + sizeof("65000")) &&
434 strncasecmp("class", source->base, 5) == 0) {
435 char buf[sizeof("65000")];
436 char *endp;
437 unsigned int val;
439 strncpy(buf, source->base + 5, source->length - 5);
440 buf[source->length - 5] = '\0';
441 val = strtoul(buf, &endp, 10);
442 if (*endp == '\0' && val <= 0xffff) {
443 *classp = (dns_rdataclass_t)val;
444 return (ISC_R_SUCCESS);
447 break;
448 case 'h':
449 COMPARE("hs", dns_rdataclass_hs);
450 COMPARE("hesiod", dns_rdataclass_hs);
451 break;
452 case 'i':
453 COMPARE("in", dns_rdataclass_in);
454 break;
455 case 'n':
456 COMPARE("none", dns_rdataclass_none);
457 break;
458 case 'r':
459 COMPARE("reserved0", dns_rdataclass_reserved0);
460 break;
463 #undef COMPARE
465 return (DNS_R_UNKNOWN);
468 isc_result_t
469 dns_rdataclass_totext(dns_rdataclass_t rdclass, isc_buffer_t *target) {
470 char buf[sizeof("CLASS65535")];
472 switch (rdclass) {
473 case dns_rdataclass_any:
474 return (str_totext("ANY", target));
475 case dns_rdataclass_chaos:
476 return (str_totext("CH", target));
477 case dns_rdataclass_hs:
478 return (str_totext("HS", target));
479 case dns_rdataclass_in:
480 return (str_totext("IN", target));
481 case dns_rdataclass_none:
482 return (str_totext("NONE", target));
483 case dns_rdataclass_reserved0:
484 return (str_totext("RESERVED0", target));
485 default:
486 snprintf(buf, sizeof(buf), "CLASS%u", rdclass);
487 return (str_totext(buf, target));
491 void
492 dns_rdataclass_format(dns_rdataclass_t rdclass,
493 char *array, unsigned int size)
495 isc_result_t result;
496 isc_buffer_t buf;
498 isc_buffer_init(&buf, array, size);
499 result = dns_rdataclass_totext(rdclass, &buf);
501 * Null terminate.
503 if (result == ISC_R_SUCCESS) {
504 if (isc_buffer_availablelength(&buf) >= 1)
505 isc_buffer_putuint8(&buf, 0);
506 else
507 result = ISC_R_NOSPACE;
509 if (result != ISC_R_SUCCESS) {
510 snprintf(array, size, "<unknown>");
511 array[size - 1] = '\0';