]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/bind9/lib/dns/rcode.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / bind9 / lib / dns / rcode.c
1 /*
2  * Copyright (C) 2004-2008, 2010  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: rcode.c,v 1.8.48.2 2010/01/15 23:47:33 tbox Exp $ */
19
20 #include <config.h>
21 #include <ctype.h>
22
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>
33
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>
42
43 #define RETERR(x) \
44         do { \
45                 isc_result_t _r = (x); \
46                 if (_r != ISC_R_SUCCESS) \
47                         return (_r); \
48         } while (0)
49
50 #define NUMBERSIZE sizeof("037777777777") /* 2^32-1 octal + NUL */
51
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},
65
66 #define ERCODENAMES \
67         /* extended rcodes */ \
68         { dns_rcode_badvers, "BADVERS", 0}, \
69         { 0, NULL, 0 }
70
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 }
81
82 /* RFC2538 section 2.1 */
83
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}
91
92 /* RFC2535 section 7, RFC3110 */
93
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_NSEC3DSA, "NSEC3DSA", 0 }, \
100         { DNS_KEYALG_ECC, "ECC", 0 }, \
101         { DNS_KEYALG_RSASHA1, "RSASHA1", 0 }, \
102         { DNS_KEYALG_NSEC3RSASHA1, "NSEC3RSASHA1", 0 }, \
103         { DNS_KEYALG_RSASHA256, "RSASHA256", 0 }, \
104         { DNS_KEYALG_RSASHA512, "RSASHA512", 0 }, \
105         { DNS_KEYALG_INDIRECT, "INDIRECT", 0 }, \
106         { DNS_KEYALG_PRIVATEDNS, "PRIVATEDNS", 0 }, \
107         { DNS_KEYALG_PRIVATEOID, "PRIVATEOID", 0 }, \
108         { 0, NULL, 0}
109
110 /* RFC2535 section 7.1 */
111
112 #define SECPROTONAMES \
113         {   0,    "NONE", 0 }, \
114         {   1,    "TLS", 0 }, \
115         {   2,    "EMAIL", 0 }, \
116         {   3,    "DNSSEC", 0 }, \
117         {   4,    "IPSEC", 0 }, \
118         { 255,    "ALL", 0 }, \
119         { 0, NULL, 0}
120
121 #define HASHALGNAMES \
122         { 1, "SHA-1", 0 }, \
123         { 0, NULL, 0 }
124
125 struct tbl {
126         unsigned int    value;
127         const char      *name;
128         int             flags;
129 };
130
131 static struct tbl rcodes[] = { RCODENAMES ERCODENAMES };
132 static struct tbl tsigrcodes[] = { RCODENAMES TSIGRCODENAMES };
133 static struct tbl certs[] = { CERTNAMES };
134 static struct tbl secalgs[] = { SECALGNAMES };
135 static struct tbl secprotos[] = { SECPROTONAMES };
136 static struct tbl hashalgs[] = { HASHALGNAMES };
137
138 static struct keyflag {
139         const char *name;
140         unsigned int value;
141         unsigned int mask;
142 } keyflags[] = {
143         { "NOCONF", 0x4000, 0xC000 },
144         { "NOAUTH", 0x8000, 0xC000 },
145         { "NOKEY",  0xC000, 0xC000 },
146         { "FLAG2",  0x2000, 0x2000 },
147         { "EXTEND", 0x1000, 0x1000 },
148         { "FLAG4",  0x0800, 0x0800 },
149         { "FLAG5",  0x0400, 0x0400 },
150         { "USER",   0x0000, 0x0300 },
151         { "ZONE",   0x0100, 0x0300 },
152         { "HOST",   0x0200, 0x0300 },
153         { "NTYP3",  0x0300, 0x0300 },
154         { "FLAG8",  0x0080, 0x0080 },
155         { "FLAG9",  0x0040, 0x0040 },
156         { "FLAG10", 0x0020, 0x0020 },
157         { "FLAG11", 0x0010, 0x0010 },
158         { "SIG0",   0x0000, 0x000F },
159         { "SIG1",   0x0001, 0x000F },
160         { "SIG2",   0x0002, 0x000F },
161         { "SIG3",   0x0003, 0x000F },
162         { "SIG4",   0x0004, 0x000F },
163         { "SIG5",   0x0005, 0x000F },
164         { "SIG6",   0x0006, 0x000F },
165         { "SIG7",   0x0007, 0x000F },
166         { "SIG8",   0x0008, 0x000F },
167         { "SIG9",   0x0009, 0x000F },
168         { "SIG10",  0x000A, 0x000F },
169         { "SIG11",  0x000B, 0x000F },
170         { "SIG12",  0x000C, 0x000F },
171         { "SIG13",  0x000D, 0x000F },
172         { "SIG14",  0x000E, 0x000F },
173         { "SIG15",  0x000F, 0x000F },
174         { "KSK",  DNS_KEYFLAG_KSK, DNS_KEYFLAG_KSK },
175         { NULL,     0, 0 }
176 };
177
178 static isc_result_t
179 str_totext(const char *source, isc_buffer_t *target) {
180         unsigned int l;
181         isc_region_t region;
182
183         isc_buffer_availableregion(target, &region);
184         l = strlen(source);
185
186         if (l > region.length)
187                 return (ISC_R_NOSPACE);
188
189         memcpy(region.base, source, l);
190         isc_buffer_add(target, l);
191         return (ISC_R_SUCCESS);
192 }
193
194 static isc_result_t
195 maybe_numeric(unsigned int *valuep, isc_textregion_t *source,
196               unsigned int max, isc_boolean_t hex_allowed)
197 {
198         isc_result_t result;
199         isc_uint32_t n;
200         char buffer[NUMBERSIZE];
201
202         if (! isdigit(source->base[0] & 0xff) ||
203             source->length > NUMBERSIZE - 1)
204                 return (ISC_R_BADNUMBER);
205
206         /*
207          * We have a potential number.  Try to parse it with
208          * isc_parse_uint32().  isc_parse_uint32() requires
209          * null termination, so we must make a copy.
210          */
211         strncpy(buffer, source->base, NUMBERSIZE);
212         INSIST(buffer[source->length] == '\0');
213
214         result = isc_parse_uint32(&n, buffer, 10);
215         if (result == ISC_R_BADNUMBER && hex_allowed)
216                 result = isc_parse_uint32(&n, buffer, 16);
217         if (result != ISC_R_SUCCESS)
218                 return (result);
219         if (n > max)
220                 return (ISC_R_RANGE);
221         *valuep = n;
222         return (ISC_R_SUCCESS);
223 }
224
225 static isc_result_t
226 dns_mnemonic_fromtext(unsigned int *valuep, isc_textregion_t *source,
227                       struct tbl *table, unsigned int max)
228 {
229         isc_result_t result;
230         int i;
231
232         result = maybe_numeric(valuep, source, max, ISC_FALSE);
233         if (result != ISC_R_BADNUMBER)
234                 return (result);
235
236         for (i = 0; table[i].name != NULL; i++) {
237                 unsigned int n;
238                 n = strlen(table[i].name);
239                 if (n == source->length &&
240                     strncasecmp(source->base, table[i].name, n) == 0) {
241                         *valuep = table[i].value;
242                         return (ISC_R_SUCCESS);
243                 }
244         }
245         return (DNS_R_UNKNOWN);
246 }
247
248 static isc_result_t
249 dns_mnemonic_totext(unsigned int value, isc_buffer_t *target,
250                     struct tbl *table)
251 {
252         int i = 0;
253         char buf[sizeof("4294967296")];
254         while (table[i].name != NULL) {
255                 if (table[i].value == value) {
256                         return (str_totext(table[i].name, target));
257                 }
258                 i++;
259         }
260         snprintf(buf, sizeof(buf), "%u", value);
261         return (str_totext(buf, target));
262 }
263
264 isc_result_t
265 dns_rcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) {
266         unsigned int value;
267         RETERR(dns_mnemonic_fromtext(&value, source, rcodes, 0xffff));
268         *rcodep = value;
269         return (ISC_R_SUCCESS);
270 }
271
272 isc_result_t
273 dns_rcode_totext(dns_rcode_t rcode, isc_buffer_t *target) {
274         return (dns_mnemonic_totext(rcode, target, rcodes));
275 }
276
277 isc_result_t
278 dns_tsigrcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) {
279         unsigned int value;
280         RETERR(dns_mnemonic_fromtext(&value, source, tsigrcodes, 0xffff));
281         *rcodep = value;
282         return (ISC_R_SUCCESS);
283 }
284
285 isc_result_t
286 dns_tsigrcode_totext(dns_rcode_t rcode, isc_buffer_t *target) {
287         return (dns_mnemonic_totext(rcode, target, tsigrcodes));
288 }
289
290 isc_result_t
291 dns_cert_fromtext(dns_cert_t *certp, isc_textregion_t *source) {
292         unsigned int value;
293         RETERR(dns_mnemonic_fromtext(&value, source, certs, 0xffff));
294         *certp = value;
295         return (ISC_R_SUCCESS);
296 }
297
298 isc_result_t
299 dns_cert_totext(dns_cert_t cert, isc_buffer_t *target) {
300         return (dns_mnemonic_totext(cert, target, certs));
301 }
302
303 isc_result_t
304 dns_secalg_fromtext(dns_secalg_t *secalgp, isc_textregion_t *source) {
305         unsigned int value;
306         RETERR(dns_mnemonic_fromtext(&value, source, secalgs, 0xff));
307         *secalgp = value;
308         return (ISC_R_SUCCESS);
309 }
310
311 isc_result_t
312 dns_secalg_totext(dns_secalg_t secalg, isc_buffer_t *target) {
313         return (dns_mnemonic_totext(secalg, target, secalgs));
314 }
315
316 isc_result_t
317 dns_secproto_fromtext(dns_secproto_t *secprotop, isc_textregion_t *source) {
318         unsigned int value;
319         RETERR(dns_mnemonic_fromtext(&value, source, secprotos, 0xff));
320         *secprotop = value;
321         return (ISC_R_SUCCESS);
322 }
323
324 isc_result_t
325 dns_secproto_totext(dns_secproto_t secproto, isc_buffer_t *target) {
326         return (dns_mnemonic_totext(secproto, target, secprotos));
327 }
328
329 isc_result_t
330 dns_hashalg_fromtext(unsigned char *hashalg, isc_textregion_t *source) {
331         unsigned int value;
332         RETERR(dns_mnemonic_fromtext(&value, source, hashalgs, 0xff));
333         *hashalg = value;
334         return (ISC_R_SUCCESS);
335 }
336
337 isc_result_t
338 dns_keyflags_fromtext(dns_keyflags_t *flagsp, isc_textregion_t *source)
339 {
340         isc_result_t result;
341         char *text, *end;
342         unsigned int value, mask;
343
344         result = maybe_numeric(&value, source, 0xffff, ISC_TRUE);
345         if (result == ISC_R_SUCCESS) {
346                 *flagsp = value;
347                 return (ISC_R_SUCCESS);
348         }
349         if (result != ISC_R_BADNUMBER)
350                 return (result);
351
352         text = source->base;
353         end = source->base + source->length;
354         value = mask = 0;
355
356         while (text < end) {
357                 struct keyflag *p;
358                 unsigned int len;
359                 char *delim = memchr(text, '|', end - text);
360                 if (delim != NULL)
361                         len = delim - text;
362                 else
363                         len = end - text;
364                 for (p = keyflags; p->name != NULL; p++) {
365                         if (strncasecmp(p->name, text, len) == 0)
366                                 break;
367                 }
368                 if (p->name == NULL)
369                         return (DNS_R_UNKNOWNFLAG);
370                 value |= p->value;
371 #ifdef notyet
372                 if ((mask & p->mask) != 0)
373                         warn("overlapping key flags");
374 #endif
375                 mask |= p->mask;
376                 text += len;
377                 if (delim != NULL)
378                         text++; /* Skip "|" */
379         }
380         *flagsp = value;
381         return (ISC_R_SUCCESS);
382 }
383
384 /*
385  * This uses lots of hard coded values, but how often do we actually
386  * add classes?
387  */
388 isc_result_t
389 dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) {
390 #define COMPARE(string, rdclass) \
391         if (((sizeof(string) - 1) == source->length) \
392             && (strncasecmp(source->base, string, source->length) == 0)) { \
393                 *classp = rdclass; \
394                 return (ISC_R_SUCCESS); \
395         }
396
397         switch (tolower((unsigned char)source->base[0])) {
398         case 'a':
399                 COMPARE("any", dns_rdataclass_any);
400                 break;
401         case 'c':
402                 /*
403                  * RFC1035 says the mnemonic for the CHAOS class is CH,
404                  * but historical BIND practice is to call it CHAOS.
405                  * We will accept both forms, but only generate CH.
406                  */
407                 COMPARE("ch", dns_rdataclass_chaos);
408                 COMPARE("chaos", dns_rdataclass_chaos);
409
410                 if (source->length > 5 &&
411                     source->length < (5 + sizeof("65000")) &&
412                     strncasecmp("class", source->base, 5) == 0) {
413                         char buf[sizeof("65000")];
414                         char *endp;
415                         unsigned int val;
416
417                         strncpy(buf, source->base + 5, source->length - 5);
418                         buf[source->length - 5] = '\0';
419                         val = strtoul(buf, &endp, 10);
420                         if (*endp == '\0' && val <= 0xffff) {
421                                 *classp = (dns_rdataclass_t)val;
422                                 return (ISC_R_SUCCESS);
423                         }
424                 }
425                 break;
426         case 'h':
427                 COMPARE("hs", dns_rdataclass_hs);
428                 COMPARE("hesiod", dns_rdataclass_hs);
429                 break;
430         case 'i':
431                 COMPARE("in", dns_rdataclass_in);
432                 break;
433         case 'n':
434                 COMPARE("none", dns_rdataclass_none);
435                 break;
436         case 'r':
437                 COMPARE("reserved0", dns_rdataclass_reserved0);
438                 break;
439         }
440
441 #undef COMPARE
442
443         return (DNS_R_UNKNOWN);
444 }
445
446 isc_result_t
447 dns_rdataclass_totext(dns_rdataclass_t rdclass, isc_buffer_t *target) {
448         char buf[sizeof("CLASS65535")];
449
450         switch (rdclass) {
451         case dns_rdataclass_any:
452                 return (str_totext("ANY", target));
453         case dns_rdataclass_chaos:
454                 return (str_totext("CH", target));
455         case dns_rdataclass_hs:
456                 return (str_totext("HS", target));
457         case dns_rdataclass_in:
458                 return (str_totext("IN", target));
459         case dns_rdataclass_none:
460                 return (str_totext("NONE", target));
461         case dns_rdataclass_reserved0:
462                 return (str_totext("RESERVED0", target));
463         default:
464                 snprintf(buf, sizeof(buf), "CLASS%u", rdclass);
465                 return (str_totext(buf, target));
466         }
467 }
468
469 void
470 dns_rdataclass_format(dns_rdataclass_t rdclass,
471                       char *array, unsigned int size)
472 {
473         isc_result_t result;
474         isc_buffer_t buf;
475
476         isc_buffer_init(&buf, array, size);
477         result = dns_rdataclass_totext(rdclass, &buf);
478         /*
479          * Null terminate.
480          */
481         if (result == ISC_R_SUCCESS) {
482                 if (isc_buffer_availablelength(&buf) >= 1)
483                         isc_buffer_putuint8(&buf, 0);
484                 else
485                         result = ISC_R_NOSPACE;
486         }
487         if (result != ISC_R_SUCCESS) {
488                 snprintf(array, size, "<unknown>");
489                 array[size - 1] = '\0';
490         }
491 }