rcode.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004-2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1998-2003  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id$ */
00019 
00020 #include <config.h>
00021 #include <ctype.h>
00022 
00023 #include <isc/buffer.h>
00024 #include <isc/parseint.h>
00025 #include <isc/print.h>
00026 #include <isc/region.h>
00027 #include <isc/result.h>
00028 #include <isc/stdio.h>
00029 #include <isc/stdlib.h>
00030 #include <isc/string.h>
00031 #include <isc/types.h>
00032 #include <isc/util.h>
00033 
00034 #include <dns/cert.h>
00035 #include <dns/ds.h>
00036 #include <dns/dsdigest.h>
00037 #include <dns/keyflags.h>
00038 #include <dns/keyvalues.h>
00039 #include <dns/rcode.h>
00040 #include <dns/rdataclass.h>
00041 #include <dns/result.h>
00042 #include <dns/secalg.h>
00043 #include <dns/secproto.h>
00044 
00045 #define RETERR(x) \
00046         do { \
00047                 isc_result_t _r = (x); \
00048                 if (_r != ISC_R_SUCCESS) \
00049                         return (_r); \
00050         } while (0)
00051 
00052 #define NUMBERSIZE sizeof("037777777777") /* 2^32-1 octal + NUL */
00053 
00054 #define RCODENAMES \
00055         /* standard rcodes */ \
00056         { dns_rcode_noerror, "NOERROR", 0}, \
00057         { dns_rcode_formerr, "FORMERR", 0}, \
00058         { dns_rcode_servfail, "SERVFAIL", 0}, \
00059         { dns_rcode_nxdomain, "NXDOMAIN", 0}, \
00060         { dns_rcode_notimp, "NOTIMP", 0}, \
00061         { dns_rcode_refused, "REFUSED", 0}, \
00062         { dns_rcode_yxdomain, "YXDOMAIN", 0}, \
00063         { dns_rcode_yxrrset, "YXRRSET", 0}, \
00064         { dns_rcode_nxrrset, "NXRRSET", 0}, \
00065         { dns_rcode_notauth, "NOTAUTH", 0}, \
00066         { dns_rcode_notzone, "NOTZONE", 0},
00067 
00068 #define ERCODENAMES \
00069         /* extended rcodes */ \
00070         { dns_rcode_badvers, "BADVERS", 0}, \
00071         { 0, NULL, 0 }
00072 
00073 #define TSIGRCODENAMES \
00074         /* extended rcodes */ \
00075         { dns_tsigerror_badsig, "BADSIG", 0}, \
00076         { dns_tsigerror_badkey, "BADKEY", 0}, \
00077         { dns_tsigerror_badtime, "BADTIME", 0}, \
00078         { dns_tsigerror_badmode, "BADMODE", 0}, \
00079         { dns_tsigerror_badname, "BADNAME", 0}, \
00080         { dns_tsigerror_badalg, "BADALG", 0}, \
00081         { dns_tsigerror_badtrunc, "BADTRUNC", 0}, \
00082         { 0, NULL, 0 }
00083 
00084 /* RFC4398 section 2.1 */
00085 
00086 #define CERTNAMES \
00087         { 1, "PKIX", 0}, \
00088         { 2, "SPKI", 0}, \
00089         { 3, "PGP", 0}, \
00090         { 4, "IPKIX", 0}, \
00091         { 5, "ISPKI", 0}, \
00092         { 6, "IPGP", 0}, \
00093         { 7, "ACPKIX", 0}, \
00094         { 8, "IACPKIX", 0}, \
00095         { 253, "URI", 0}, \
00096         { 254, "OID", 0}, \
00097         { 0, NULL, 0}
00098 
00099 /* RFC2535 section 7, RFC3110 */
00100 
00101 #define SECALGNAMES \
00102         { DNS_KEYALG_RSAMD5, "RSAMD5", 0 }, \
00103         { DNS_KEYALG_RSAMD5, "RSA", 0 }, \
00104         { DNS_KEYALG_DH, "DH", 0 }, \
00105         { DNS_KEYALG_DSA, "DSA", 0 }, \
00106         { DNS_KEYALG_NSEC3DSA, "NSEC3DSA", 0 }, \
00107         { DNS_KEYALG_ECC, "ECC", 0 }, \
00108         { DNS_KEYALG_RSASHA1, "RSASHA1", 0 }, \
00109         { DNS_KEYALG_NSEC3RSASHA1, "NSEC3RSASHA1", 0 }, \
00110         { DNS_KEYALG_RSASHA256, "RSASHA256", 0 }, \
00111         { DNS_KEYALG_RSASHA512, "RSASHA512", 0 }, \
00112         { DNS_KEYALG_ECCGOST, "ECCGOST", 0 }, \
00113         { DNS_KEYALG_ECDSA256, "ECDSAP256SHA256", 0 }, \
00114         { DNS_KEYALG_ECDSA384, "ECDSAP384SHA384", 0 }, \
00115         { DNS_KEYALG_INDIRECT, "INDIRECT", 0 }, \
00116         { DNS_KEYALG_PRIVATEDNS, "PRIVATEDNS", 0 }, \
00117         { DNS_KEYALG_PRIVATEOID, "PRIVATEOID", 0 }, \
00118         { 0, NULL, 0}
00119 
00120 /* RFC2535 section 7.1 */
00121 
00122 #define SECPROTONAMES \
00123         {   0,    "NONE", 0 }, \
00124         {   1,    "TLS", 0 }, \
00125         {   2,    "EMAIL", 0 }, \
00126         {   3,    "DNSSEC", 0 }, \
00127         {   4,    "IPSEC", 0 }, \
00128         { 255,    "ALL", 0 }, \
00129         { 0, NULL, 0}
00130 
00131 #define HASHALGNAMES \
00132         { 1, "SHA-1", 0 }, \
00133         { 0, NULL, 0 }
00134 
00135 /* RFC3658, RFC4509, RFC5933, RFC6605 */
00136 
00137 #define DSDIGESTNAMES \
00138         { DNS_DSDIGEST_SHA1, "SHA-1", 0 }, \
00139         { DNS_DSDIGEST_SHA256, "SHA-256", 0 }, \
00140         { DNS_DSDIGEST_GOST, "GOST", 0 }, \
00141         { DNS_DSDIGEST_SHA384, "SHA-384", 0 }, \
00142         { 0, NULL, 0}
00143 
00144 struct tbl {
00145         unsigned int    value;
00146         const char      *name;
00147         int             flags;
00148 };
00149 
00150 static struct tbl rcodes[] = { RCODENAMES ERCODENAMES };
00151 static struct tbl tsigrcodes[] = { RCODENAMES TSIGRCODENAMES };
00152 static struct tbl certs[] = { CERTNAMES };
00153 static struct tbl secalgs[] = { SECALGNAMES };
00154 static struct tbl secprotos[] = { SECPROTONAMES };
00155 static struct tbl hashalgs[] = { HASHALGNAMES };
00156 static struct tbl dsdigests[] = { DSDIGESTNAMES };
00157 
00158 static struct keyflag {
00159         const char *name;
00160         unsigned int value;
00161         unsigned int mask;
00162 } keyflags[] = {
00163         { "NOCONF", 0x4000, 0xC000 },
00164         { "NOAUTH", 0x8000, 0xC000 },
00165         { "NOKEY",  0xC000, 0xC000 },
00166         { "FLAG2",  0x2000, 0x2000 },
00167         { "EXTEND", 0x1000, 0x1000 },
00168         { "FLAG4",  0x0800, 0x0800 },
00169         { "FLAG5",  0x0400, 0x0400 },
00170         { "USER",   0x0000, 0x0300 },
00171         { "ZONE",   0x0100, 0x0300 },
00172         { "HOST",   0x0200, 0x0300 },
00173         { "NTYP3",  0x0300, 0x0300 },
00174         { "FLAG8",  0x0080, 0x0080 },
00175         { "FLAG9",  0x0040, 0x0040 },
00176         { "FLAG10", 0x0020, 0x0020 },
00177         { "FLAG11", 0x0010, 0x0010 },
00178         { "SIG0",   0x0000, 0x000F },
00179         { "SIG1",   0x0001, 0x000F },
00180         { "SIG2",   0x0002, 0x000F },
00181         { "SIG3",   0x0003, 0x000F },
00182         { "SIG4",   0x0004, 0x000F },
00183         { "SIG5",   0x0005, 0x000F },
00184         { "SIG6",   0x0006, 0x000F },
00185         { "SIG7",   0x0007, 0x000F },
00186         { "SIG8",   0x0008, 0x000F },
00187         { "SIG9",   0x0009, 0x000F },
00188         { "SIG10",  0x000A, 0x000F },
00189         { "SIG11",  0x000B, 0x000F },
00190         { "SIG12",  0x000C, 0x000F },
00191         { "SIG13",  0x000D, 0x000F },
00192         { "SIG14",  0x000E, 0x000F },
00193         { "SIG15",  0x000F, 0x000F },
00194         { "KSK",  DNS_KEYFLAG_KSK, DNS_KEYFLAG_KSK },
00195         { NULL,     0, 0 }
00196 };
00197 
00198 static isc_result_t
00199 str_totext(const char *source, isc_buffer_t *target) {
00200         unsigned int l;
00201         isc_region_t region;
00202 
00203         isc_buffer_availableregion(target, &region);
00204         l = strlen(source);
00205 
00206         if (l > region.length)
00207                 return (ISC_R_NOSPACE);
00208 
00209         memmove(region.base, source, l);
00210         isc_buffer_add(target, l);
00211         return (ISC_R_SUCCESS);
00212 }
00213 
00214 static isc_result_t
00215 maybe_numeric(unsigned int *valuep, isc_textregion_t *source,
00216               unsigned int max, isc_boolean_t hex_allowed)
00217 {
00218         isc_result_t result;
00219         isc_uint32_t n;
00220         char buffer[NUMBERSIZE];
00221 
00222         if (! isdigit(source->base[0] & 0xff) ||
00223             source->length > NUMBERSIZE - 1)
00224                 return (ISC_R_BADNUMBER);
00225 
00226         /*
00227          * We have a potential number.  Try to parse it with
00228          * isc_parse_uint32().  isc_parse_uint32() requires
00229          * null termination, so we must make a copy.
00230          */
00231         strncpy(buffer, source->base, sizeof(buffer));
00232         buffer[sizeof(buffer) - 1] = '\0';
00233 
00234         INSIST(buffer[source->length] == '\0');
00235 
00236         result = isc_parse_uint32(&n, buffer, 10);
00237         if (result == ISC_R_BADNUMBER && hex_allowed)
00238                 result = isc_parse_uint32(&n, buffer, 16);
00239         if (result != ISC_R_SUCCESS)
00240                 return (result);
00241         if (n > max)
00242                 return (ISC_R_RANGE);
00243         *valuep = n;
00244         return (ISC_R_SUCCESS);
00245 }
00246 
00247 static isc_result_t
00248 dns_mnemonic_fromtext(unsigned int *valuep, isc_textregion_t *source,
00249                       struct tbl *table, unsigned int max)
00250 {
00251         isc_result_t result;
00252         int i;
00253 
00254         result = maybe_numeric(valuep, source, max, ISC_FALSE);
00255         if (result != ISC_R_BADNUMBER)
00256                 return (result);
00257 
00258         for (i = 0; table[i].name != NULL; i++) {
00259                 unsigned int n;
00260                 n = strlen(table[i].name);
00261                 if (n == source->length &&
00262                     strncasecmp(source->base, table[i].name, n) == 0) {
00263                         *valuep = table[i].value;
00264                         return (ISC_R_SUCCESS);
00265                 }
00266         }
00267         return (DNS_R_UNKNOWN);
00268 }
00269 
00270 static isc_result_t
00271 dns_mnemonic_totext(unsigned int value, isc_buffer_t *target,
00272                     struct tbl *table)
00273 {
00274         int i = 0;
00275         char buf[sizeof("4294967296")];
00276         while (table[i].name != NULL) {
00277                 if (table[i].value == value) {
00278                         return (str_totext(table[i].name, target));
00279                 }
00280                 i++;
00281         }
00282         snprintf(buf, sizeof(buf), "%u", value);
00283         return (str_totext(buf, target));
00284 }
00285 
00286 isc_result_t
00287 dns_rcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) {
00288         unsigned int value;
00289         RETERR(dns_mnemonic_fromtext(&value, source, rcodes, 0xffff));
00290         *rcodep = value;
00291         return (ISC_R_SUCCESS);
00292 }
00293 
00294 isc_result_t
00295 dns_rcode_totext(dns_rcode_t rcode, isc_buffer_t *target) {
00296         return (dns_mnemonic_totext(rcode, target, rcodes));
00297 }
00298 
00299 isc_result_t
00300 dns_tsigrcode_fromtext(dns_rcode_t *rcodep, isc_textregion_t *source) {
00301         unsigned int value;
00302         RETERR(dns_mnemonic_fromtext(&value, source, tsigrcodes, 0xffff));
00303         *rcodep = value;
00304         return (ISC_R_SUCCESS);
00305 }
00306 
00307 isc_result_t
00308 dns_tsigrcode_totext(dns_rcode_t rcode, isc_buffer_t *target) {
00309         return (dns_mnemonic_totext(rcode, target, tsigrcodes));
00310 }
00311 
00312 isc_result_t
00313 dns_cert_fromtext(dns_cert_t *certp, isc_textregion_t *source) {
00314         unsigned int value;
00315         RETERR(dns_mnemonic_fromtext(&value, source, certs, 0xffff));
00316         *certp = value;
00317         return (ISC_R_SUCCESS);
00318 }
00319 
00320 isc_result_t
00321 dns_cert_totext(dns_cert_t cert, isc_buffer_t *target) {
00322         return (dns_mnemonic_totext(cert, target, certs));
00323 }
00324 
00325 isc_result_t
00326 dns_secalg_fromtext(dns_secalg_t *secalgp, isc_textregion_t *source) {
00327         unsigned int value;
00328         RETERR(dns_mnemonic_fromtext(&value, source, secalgs, 0xff));
00329         *secalgp = value;
00330         return (ISC_R_SUCCESS);
00331 }
00332 
00333 isc_result_t
00334 dns_secalg_totext(dns_secalg_t secalg, isc_buffer_t *target) {
00335         return (dns_mnemonic_totext(secalg, target, secalgs));
00336 }
00337 
00338 void
00339 dns_secalg_format(dns_secalg_t alg, char *cp, unsigned int size) {
00340         isc_buffer_t b;
00341         isc_region_t r;
00342         isc_result_t result;
00343 
00344         REQUIRE(cp != NULL && size > 0);
00345         isc_buffer_init(&b, cp, size - 1);
00346         result = dns_secalg_totext(alg, &b);
00347         isc_buffer_usedregion(&b, &r);
00348         r.base[r.length] = 0;
00349         if (result != ISC_R_SUCCESS)
00350                 r.base[0] = 0;
00351 }
00352 
00353 isc_result_t
00354 dns_secproto_fromtext(dns_secproto_t *secprotop, isc_textregion_t *source) {
00355         unsigned int value;
00356         RETERR(dns_mnemonic_fromtext(&value, source, secprotos, 0xff));
00357         *secprotop = value;
00358         return (ISC_R_SUCCESS);
00359 }
00360 
00361 isc_result_t
00362 dns_secproto_totext(dns_secproto_t secproto, isc_buffer_t *target) {
00363         return (dns_mnemonic_totext(secproto, target, secprotos));
00364 }
00365 
00366 isc_result_t
00367 dns_hashalg_fromtext(unsigned char *hashalg, isc_textregion_t *source) {
00368         unsigned int value;
00369         RETERR(dns_mnemonic_fromtext(&value, source, hashalgs, 0xff));
00370         *hashalg = value;
00371         return (ISC_R_SUCCESS);
00372 }
00373 
00374 isc_result_t
00375 dns_keyflags_fromtext(dns_keyflags_t *flagsp, isc_textregion_t *source)
00376 {
00377         isc_result_t result;
00378         char *text, *end;
00379         unsigned int value, mask;
00380 
00381         result = maybe_numeric(&value, source, 0xffff, ISC_TRUE);
00382         if (result == ISC_R_SUCCESS) {
00383                 *flagsp = value;
00384                 return (ISC_R_SUCCESS);
00385         }
00386         if (result != ISC_R_BADNUMBER)
00387                 return (result);
00388 
00389         text = source->base;
00390         end = source->base + source->length;
00391         value = mask = 0;
00392 
00393         while (text < end) {
00394                 struct keyflag *p;
00395                 unsigned int len;
00396                 char *delim = memchr(text, '|', end - text);
00397                 if (delim != NULL)
00398                         len = (unsigned int)(delim - text);
00399                 else
00400                         len = (unsigned int)(end - text);
00401                 for (p = keyflags; p->name != NULL; p++) {
00402                         if (strncasecmp(p->name, text, len) == 0)
00403                                 break;
00404                 }
00405                 if (p->name == NULL)
00406                         return (DNS_R_UNKNOWNFLAG);
00407                 value |= p->value;
00408 #ifdef notyet
00409                 if ((mask & p->mask) != 0)
00410                         warn("overlapping key flags");
00411 #endif
00412                 mask |= p->mask;
00413                 text += len;
00414                 if (delim != NULL)
00415                         text++; /* Skip "|" */
00416         }
00417         *flagsp = value;
00418         return (ISC_R_SUCCESS);
00419 }
00420 
00421 isc_result_t
00422 dns_dsdigest_fromtext(dns_dsdigest_t *dsdigestp, isc_textregion_t *source) {
00423         unsigned int value;
00424         RETERR(dns_mnemonic_fromtext(&value, source, dsdigests, 0xff));
00425         *dsdigestp = value;
00426         return (ISC_R_SUCCESS);
00427 }
00428 
00429 isc_result_t
00430 dns_dsdigest_totext(dns_dsdigest_t dsdigest, isc_buffer_t *target) {
00431         return (dns_mnemonic_totext(dsdigest, target, dsdigests));
00432 }
00433 
00434 void
00435 dns_dsdigest_format(dns_dsdigest_t typ, char *cp, unsigned int size) {
00436         isc_buffer_t b;
00437         isc_region_t r;
00438         isc_result_t result;
00439 
00440         REQUIRE(cp != NULL && size > 0);
00441         isc_buffer_init(&b, cp, size - 1);
00442         result = dns_dsdigest_totext(typ, &b);
00443         isc_buffer_usedregion(&b, &r);
00444         r.base[r.length] = 0;
00445         if (result != ISC_R_SUCCESS)
00446                 r.base[0] = 0;
00447 }
00448 
00449 /*
00450  * This uses lots of hard coded values, but how often do we actually
00451  * add classes?
00452  */
00453 isc_result_t
00454 dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) {
00455 #define COMPARE(string, rdclass) \
00456         if (((sizeof(string) - 1) == source->length) \
00457             && (strncasecmp(source->base, string, source->length) == 0)) { \
00458                 *classp = rdclass; \
00459                 return (ISC_R_SUCCESS); \
00460         }
00461 
00462         switch (tolower((unsigned char)source->base[0])) {
00463         case 'a':
00464                 COMPARE("any", dns_rdataclass_any);
00465                 break;
00466         case 'c':
00467                 /*
00468                  * RFC1035 says the mnemonic for the CHAOS class is CH,
00469                  * but historical BIND practice is to call it CHAOS.
00470                  * We will accept both forms, but only generate CH.
00471                  */
00472                 COMPARE("ch", dns_rdataclass_chaos);
00473                 COMPARE("chaos", dns_rdataclass_chaos);
00474 
00475                 if (source->length > 5 &&
00476                     source->length < (5 + sizeof("65000")) &&
00477                     strncasecmp("class", source->base, 5) == 0) {
00478                         char buf[sizeof("65000")];
00479                         char *endp;
00480                         unsigned int val;
00481 
00482                         strncpy(buf, source->base + 5, source->length - 5);
00483                         buf[source->length - 5] = '\0';
00484                         val = strtoul(buf, &endp, 10);
00485                         if (*endp == '\0' && val <= 0xffff) {
00486                                 *classp = (dns_rdataclass_t)val;
00487                                 return (ISC_R_SUCCESS);
00488                         }
00489                 }
00490                 break;
00491         case 'h':
00492                 COMPARE("hs", dns_rdataclass_hs);
00493                 COMPARE("hesiod", dns_rdataclass_hs);
00494                 break;
00495         case 'i':
00496                 COMPARE("in", dns_rdataclass_in);
00497                 break;
00498         case 'n':
00499                 COMPARE("none", dns_rdataclass_none);
00500                 break;
00501         case 'r':
00502                 COMPARE("reserved0", dns_rdataclass_reserved0);
00503                 break;
00504         }
00505 
00506 #undef COMPARE
00507 
00508         return (DNS_R_UNKNOWN);
00509 }
00510 
00511 isc_result_t
00512 dns_rdataclass_totext(dns_rdataclass_t rdclass, isc_buffer_t *target) {
00513         char buf[sizeof("CLASS65535")];
00514 
00515         switch (rdclass) {
00516         case dns_rdataclass_any:
00517                 return (str_totext("ANY", target));
00518         case dns_rdataclass_chaos:
00519                 return (str_totext("CH", target));
00520         case dns_rdataclass_hs:
00521                 return (str_totext("HS", target));
00522         case dns_rdataclass_in:
00523                 return (str_totext("IN", target));
00524         case dns_rdataclass_none:
00525                 return (str_totext("NONE", target));
00526         case dns_rdataclass_reserved0:
00527                 return (str_totext("RESERVED0", target));
00528         default:
00529                 snprintf(buf, sizeof(buf), "CLASS%u", rdclass);
00530                 return (str_totext(buf, target));
00531         }
00532 }
00533 
00534 void
00535 dns_rdataclass_format(dns_rdataclass_t rdclass,
00536                       char *array, unsigned int size)
00537 {
00538         isc_result_t result;
00539         isc_buffer_t buf;
00540 
00541         if (size == 0U)
00542                 return;
00543 
00544         isc_buffer_init(&buf, array, size);
00545         result = dns_rdataclass_totext(rdclass, &buf);
00546         /*
00547          * Null terminate.
00548          */
00549         if (result == ISC_R_SUCCESS) {
00550                 if (isc_buffer_availablelength(&buf) >= 1)
00551                         isc_buffer_putuint8(&buf, 0);
00552                 else
00553                         result = ISC_R_NOSPACE;
00554         }
00555         if (result != ISC_R_SUCCESS)
00556                 strlcpy(array, "<unknown>", size);
00557 }

Generated on Tue Apr 28 17:40:59 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha