1 /*
2  * Copyright (C) 2004, 2005, 2007-2013, 2015  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$ */
19 
20 /*! \file */
21 
22 #include <config.h>
23 
24 #include <isc/once.h>
25 #include <isc/util.h>
26 
27 #include <dns/result.h>
28 #include <dns/lib.h>
29 
30 static const char *text[DNS_R_NRESULTS] = {
31 	"label too long",		       /*%< 0 DNS_R_LABELTOOLONG */
32 	"bad escape",			       /*%< 1 DNS_R_BADESCAPE */
33 	/*!
34 	 * Note that DNS_R_BADBITSTRING and DNS_R_BITSTRINGTOOLONG are
35 	 * deprecated.
36 	 */
37 	"bad bitstring",		       /*%< 2 DNS_R_BADBITSTRING */
38 	"bitstring too long",		       /*%< 3 DNS_R_BITSTRINGTOOLONG */
39 	"empty label",			       /*%< 4 DNS_R_EMPTYLABEL */
40 
41 	"bad dotted quad",		       /*%< 5 DNS_R_BADDOTTEDQUAD */
42 	"invalid NS owner name (wildcard)",    /*%< 6 DNS_R_INVALIDNS */
43 	"unknown class/type",		       /*%< 7 DNS_R_UNKNOWN */
44 	"bad label type",		       /*%< 8 DNS_R_BADLABELTYPE */
45 	"bad compression pointer",	       /*%< 9 DNS_R_BADPOINTER */
46 
47 	"too many hops",		       /*%< 10 DNS_R_TOOMANYHOPS */
48 	"disallowed (by application policy)",  /*%< 11 DNS_R_DISALLOWED */
49 	"extra input text",		       /*%< 12 DNS_R_EXTRATOKEN */
50 	"extra input data",		       /*%< 13 DNS_R_EXTRADATA */
51 	"text too long",		       /*%< 14 DNS_R_TEXTTOOLONG */
52 
53 	"not at top of zone",		       /*%< 15 DNS_R_NOTZONETOP */
54 	"syntax error",			       /*%< 16 DNS_R_SYNTAX */
55 	"bad checksum",			       /*%< 17 DNS_R_BADCKSUM */
56 	"bad IPv6 address",		       /*%< 18 DNS_R_BADAAAA */
57 	"no owner",			       /*%< 19 DNS_R_NOOWNER */
58 
59 	"no ttl",			       /*%< 20 DNS_R_NOTTL */
60 	"bad class",			       /*%< 21 DNS_R_BADCLASS */
61 	"name too long",		       /*%< 22 DNS_R_NAMETOOLONG */
62 	"partial match",		       /*%< 23 DNS_R_PARTIALMATCH */
63 	"new origin",			       /*%< 24 DNS_R_NEWORIGIN */
64 
65 	"unchanged",			       /*%< 25 DNS_R_UNCHANGED */
66 	"bad ttl",			       /*%< 26 DNS_R_BADTTL */
67 	"more data needed/to be rendered",     /*%< 27 DNS_R_NOREDATA */
68 	"continue",			       /*%< 28 DNS_R_CONTINUE */
69 	"delegation",			       /*%< 29 DNS_R_DELEGATION */
70 
71 	"glue",				       /*%< 30 DNS_R_GLUE */
72 	"dname",			       /*%< 31 DNS_R_DNAME */
73 	"cname",			       /*%< 32 DNS_R_CNAME */
74 	"bad database",			       /*%< 33 DNS_R_BADDB */
75 	"zonecut",			       /*%< 34 DNS_R_ZONECUT */
76 
77 	"bad zone",			       /*%< 35 DNS_R_BADZONE */
78 	"more data",			       /*%< 36 DNS_R_MOREDATA */
79 	"up to date",			       /*%< 37 DNS_R_UPTODATE */
80 	"tsig verify failure",		       /*%< 38 DNS_R_TSIGVERIFYFAILURE */
81 	"tsig indicates error",		       /*%< 39 DNS_R_TSIGERRORSET */
82 
83 	"RRSIG failed to verify",	       /*%< 40 DNS_R_SIGINVALID */
84 	"RRSIG has expired",		       /*%< 41 DNS_R_SIGEXPIRED */
85 	"RRSIG validity period has not begun", /*%< 42 DNS_R_SIGFUTURE */
86 	"key is unauthorized to sign data",    /*%< 43 DNS_R_KEYUNAUTHORIZED */
87 	"invalid time",			       /*%< 44 DNS_R_INVALIDTIME */
88 
89 	"expected a TSIG or SIG(0)",	       /*%< 45 DNS_R_EXPECTEDTSIG */
90 	"did not expect a TSIG or SIG(0)",     /*%< 46 DNS_R_UNEXPECTEDTSIG */
91 	"TKEY is unacceptable",		       /*%< 47 DNS_R_INVALIDTKEY */
92 	"hint",				       /*%< 48 DNS_R_HINT */
93 	"drop",				       /*%< 49 DNS_R_DROP */
94 
95 	"zone not loaded",		       /*%< 50 DNS_R_NOTLOADED */
96 	"ncache nxdomain",		       /*%< 51 DNS_R_NCACHENXDOMAIN */
97 	"ncache nxrrset",		       /*%< 52 DNS_R_NCACHENXRRSET */
98 	"wait",				       /*%< 53 DNS_R_WAIT */
99 	"not verified yet",		       /*%< 54 DNS_R_NOTVERIFIEDYET */
100 
101 	"no identity",			       /*%< 55 DNS_R_NOIDENTITY */
102 	"no journal",			       /*%< 56 DNS_R_NOJOURNAL */
103 	"alias",			       /*%< 57 DNS_R_ALIAS */
104 	"use TCP",			       /*%< 58 DNS_R_USETCP */
105 	"no valid RRSIG",		       /*%< 59 DNS_R_NOVALIDSIG */
106 
107 	"no valid NSEC",		       /*%< 60 DNS_R_NOVALIDNSEC */
108 	"insecurity proof failed",	       /*%< 61 DNS_R_NOTINSECURE */
109 	"unknown service",		       /*%< 62 DNS_R_UNKNOWNSERVICE */
110 	"recoverable error occurred",	       /*%< 63 DNS_R_RECOVERABLE */
111 	"unknown opt attribute record",	       /*%< 64 DNS_R_UNKNOWNOPT */
112 
113 	"unexpected message id",	       /*%< 65 DNS_R_UNEXPECTEDID */
114 	"seen include file",		       /*%< 66 DNS_R_SEENINCLUDE */
115 	"not exact",		       	       /*%< 67 DNS_R_NOTEXACT */
116 	"address blackholed",	       	       /*%< 68 DNS_R_BLACKHOLED */
117 	"bad algorithm",		       /*%< 69 DNS_R_BADALG */
118 
119 	"invalid use of a meta type",	       /*%< 70 DNS_R_METATYPE */
120 	"CNAME and other data",		       /*%< 71 DNS_R_CNAMEANDOTHER */
121 	"multiple RRs of singleton type",      /*%< 72 DNS_R_SINGLETON */
122 	"hint nxrrset",			       /*%< 73 DNS_R_HINTNXRRSET */
123 	"no master file configured",	       /*%< 74 DNS_R_NOMASTERFILE */
124 
125 	"unknown protocol",		       /*%< 75 DNS_R_UNKNOWNPROTO */
126 	"clocks are unsynchronized",	       /*%< 76 DNS_R_CLOCKSKEW */
127 	"IXFR failed",			       /*%< 77 DNS_R_BADIXFR */
128 	"not authoritative",		       /*%< 78 DNS_R_NOTAUTHORITATIVE */
129 	"no valid KEY",		       	       /*%< 79 DNS_R_NOVALIDKEY */
130 
131 	"obsolete",			       /*%< 80 DNS_R_OBSOLETE */
132 	"already frozen",		       /*%< 81 DNS_R_FROZEN */
133 	"unknown flag",			       /*%< 82 DNS_R_UNKNOWNFLAG */
134 	"expected a response",		       /*%< 83 DNS_R_EXPECTEDRESPONSE */
135 	"no valid DS",			       /*%< 84 DNS_R_NOVALIDDS */
136 
137 	"NS is an address",		       /*%< 85 DNS_R_NSISADDRESS */
138 	"received FORMERR",		       /*%< 86 DNS_R_REMOTEFORMERR */
139 	"truncated TCP response",	       /*%< 87 DNS_R_TRUNCATEDTCP */
140 	"lame server detected",		       /*%< 88 DNS_R_LAME */
141 	"unexpected RCODE",		       /*%< 89 DNS_R_UNEXPECTEDRCODE */
142 
143 	"unexpected OPCODE",		       /*%< 90 DNS_R_UNEXPECTEDOPCODE */
144 	"chase DS servers",		       /*%< 91 DNS_R_CHASEDSSERVERS */
145 	"empty name",			       /*%< 92 DNS_R_EMPTYNAME */
146 	"empty wild",			       /*%< 93 DNS_R_EMPTYWILD */
147 	"bad bitmap",			       /*%< 94 DNS_R_BADBITMAP */
148 
149 	"from wildcard",		       /*%< 95 DNS_R_FROMWILDCARD */
150 	"bad owner name (check-names)",	       /*%< 96 DNS_R_BADOWNERNAME */
151 	"bad name (check-names)",	       /*%< 97 DNS_R_BADNAME */
152 	"dynamic zone",			       /*%< 98 DNS_R_DYNAMIC */
153 	"unknown command",		       /*%< 99 DNS_R_UNKNOWNCOMMAND */
154 
155 	"must-be-secure",		       /*%< 100 DNS_R_MUSTBESECURE */
156 	"covering NSEC record returned",       /*%< 101 DNS_R_COVERINGNSEC */
157 	"MX is an address",		       /*%< 102 DNS_R_MXISADDRESS */
158 	"duplicate query",		       /*%< 103 DNS_R_DUPLICATE */
159 	"invalid NSEC3 owner name (wildcard)", /*%< 104 DNS_R_INVALIDNSEC3 */
160 
161 	"not master",			       /*%< 105 DNS_R_NOTMASTER */
162 	"broken trust chain",		       /*%< 106 DNS_R_BROKENCHAIN */
163 	"expired",			       /*%< 107 DNS_R_EXPIRED */
164 	"not dynamic",			       /*%< 108 DNS_R_NOTDYNAMIC */
165 	"bad EUI",			       /*%< 109 DNS_R_BADEUI */
166 
167 	"covered by negative trust anchor",    /*%< 110 DNS_R_NTACOVERED */
168 	"bad CDS",			       /*%< 111 DNS_R_BADCSD */
169 	"bad CDNSKEY",			       /*%< 112 DNS_R_BADCDNSKEY */
170 	"malformed OPT option"		       /*%< 113 DNS_R_OPTERR */
171 };
172 
173 static const char *rcode_text[DNS_R_NRCODERESULTS] = {
174 	"NOERROR",				/*%< 0 DNS_R_NOEROR */
175 	"FORMERR",				/*%< 1 DNS_R_FORMERR */
176 	"SERVFAIL",				/*%< 2 DNS_R_SERVFAIL */
177 	"NXDOMAIN",				/*%< 3 DNS_R_NXDOMAIN */
178 	"NOTIMP",				/*%< 4 DNS_R_NOTIMP */
179 
180 	"REFUSED",				/*%< 5 DNS_R_REFUSED */
181 	"YXDOMAIN",				/*%< 6 DNS_R_YXDOMAIN */
182 	"YXRRSET",				/*%< 7 DNS_R_YXRRSET */
183 	"NXRRSET",				/*%< 8 DNS_R_NXRRSET */
184 	"NOTAUTH",				/*%< 9 DNS_R_NOTAUTH */
185 
186 	"NOTZONE",				/*%< 10 DNS_R_NOTZONE */
187 	"<rcode 11>",				/*%< 11 has no macro */
188 	"<rcode 12>",				/*%< 12 has no macro */
189 	"<rcode 13>",				/*%< 13 has no macro */
190 	"<rcode 14>",				/*%< 14 has no macro */
191 
192 	"<rcode 15>",				/*%< 15 has no macro */
193 	"BADVERS",				/*%< 16 DNS_R_BADVERS */
194 };
195 
196 #define DNS_RESULT_RESULTSET			2
197 #define DNS_RESULT_RCODERESULTSET		3
198 
199 static isc_once_t		once = ISC_ONCE_INIT;
200 
201 static void
initialize_action(void)202 initialize_action(void) {
203 	isc_result_t result;
204 
205 	result = isc_result_register(ISC_RESULTCLASS_DNS, DNS_R_NRESULTS,
206 				     text, dns_msgcat, DNS_RESULT_RESULTSET);
207 	if (result == ISC_R_SUCCESS)
208 		result = isc_result_register(ISC_RESULTCLASS_DNSRCODE,
209 					     DNS_R_NRCODERESULTS,
210 					     rcode_text, dns_msgcat,
211 					     DNS_RESULT_RCODERESULTSET);
212 	if (result != ISC_R_SUCCESS)
213 		UNEXPECTED_ERROR(__FILE__, __LINE__,
214 				 "isc_result_register() failed: %u", result);
215 }
216 
217 static void
initialize(void)218 initialize(void) {
219 	dns_lib_initmsgcat();
220 	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
221 }
222 
223 const char *
dns_result_totext(isc_result_t result)224 dns_result_totext(isc_result_t result) {
225 	initialize();
226 
227 	return (isc_result_totext(result));
228 }
229 
230 void
dns_result_register(void)231 dns_result_register(void) {
232 	initialize();
233 }
234 
235 dns_rcode_t
dns_result_torcode(isc_result_t result)236 dns_result_torcode(isc_result_t result) {
237 	dns_rcode_t rcode = dns_rcode_servfail;
238 
239 	if (DNS_RESULT_ISRCODE(result)) {
240 		/*
241 		 * Rcodes can't be bigger than 12 bits, which is why we
242 		 * AND with 0xFFF instead of 0xFFFF.
243 		 */
244 		return ((dns_rcode_t)((result) & 0xFFF));
245 	}
246 
247 	/*
248 	 * Try to supply an appropriate rcode.
249 	 */
250 	switch (result) {
251 	case ISC_R_SUCCESS:
252 		rcode = dns_rcode_noerror;
253 		break;
254 	case ISC_R_BADBASE64:
255 	case ISC_R_NOSPACE:
256 	case ISC_R_RANGE:
257 	case ISC_R_UNEXPECTEDEND:
258 	case DNS_R_BADAAAA:
259 	/* case DNS_R_BADBITSTRING: deprecated */
260 	case DNS_R_BADCKSUM:
261 	case DNS_R_BADCLASS:
262 	case DNS_R_BADLABELTYPE:
263 	case DNS_R_BADPOINTER:
264 	case DNS_R_BADTTL:
265 	case DNS_R_BADZONE:
266 	/* case DNS_R_BITSTRINGTOOLONG: deprecated */
267 	case DNS_R_EXTRADATA:
268 	case DNS_R_LABELTOOLONG:
269 	case DNS_R_NOREDATA:
270 	case DNS_R_SYNTAX:
271 	case DNS_R_TEXTTOOLONG:
272 	case DNS_R_TOOMANYHOPS:
273 	case DNS_R_TSIGERRORSET:
274 	case DNS_R_UNKNOWN:
275 	case DNS_R_NAMETOOLONG:
276 	case DNS_R_OPTERR:
277 		rcode = dns_rcode_formerr;
278 		break;
279 	case DNS_R_DISALLOWED:
280 		rcode = dns_rcode_refused;
281 		break;
282 	case DNS_R_TSIGVERIFYFAILURE:
283 	case DNS_R_CLOCKSKEW:
284 		rcode = dns_rcode_notauth;
285 		break;
286 	default:
287 		rcode = dns_rcode_servfail;
288 	}
289 
290 	return (rcode);
291 }
292