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