1 /*------------------------------------------------------------------------------
2  *
3  * Copyright (c) 2011-2021, EURid vzw. All rights reserved.
4  * The YADIFA TM software product is provided under the BSD 3-clause license:
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *        * Redistributions of source code must retain the above copyright
11  *          notice, this list of conditions and the following disclaimer.
12  *        * Redistributions in binary form must reproduce the above copyright
13  *          notice, this list of conditions and the following disclaimer in the
14  *          documentation and/or other materials provided with the distribution.
15  *        * Neither the name of EURid nor the names of its contributors may be
16  *          used to endorse or promote products derived from this software
17  *          without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  *------------------------------------------------------------------------------
32  *
33  */
34 
35 /** @defgroup dnscoreerror Error
36  *  @ingroup dnscore
37  *  @brief
38  *
39  *
40  *
41  * @{
42  *
43  *----------------------------------------------------------------------------*/
44 #ifndef ERROR_H_
45 #define ERROR_H_
46 
47 /*    ------------------------------------------------------------
48  *
49  *      INCLUDES
50  */
51 
52 /*
53  * Please only include "native" stuff.  sys_error.h should NOT depend
54  * on anything else (beside sys_types but sys_types.h already includes
55  * sys_error.h)
56  */
57 
58 #include <errno.h>
59 #include <stdarg.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <syslog.h>
64 
65 /*
66  * 8000 ERRNO
67  * 8001 SERVER
68  * 8002 CORE
69  * 8003 DNS
70  * 8004 DATABASE
71  * 8005 DNSSEC
72  * 8006 ACL
73  * 8007 CONFIG?
74  * 8008 YDF
75  * 8009 EAI
76  * 800A ZONE FILE READ
77  * 800B SANITY
78  * 800C NEW CONFIG
79  * 800D PARSER
80  * C000 DNS RCODE
81  */
82 
83 #if !defined(_SYSTYPES_H)
84 #error Included from a disallowed place.
85 #endif
86 
87 
88 #define SUCCESS                         0
89 #define OK                              0
90 
91 // The basic error code
92 
93 /// @note Everywhere in the source, "return ERROR;" should be replaced by something more specific
94 
95 #ifdef ERROR
96 #undef ERROR
97 #endif
98 #define ERROR                           -1
99 #define NOK				                -1
100 
101 #define TRACK_RETURNED_GENERIC_ERROR    0
102 
103 #define ERRNO_ERROR_BASE                0x80000000
104 
105 typedef int32_t ya_result;
106 
107 /* Two macros to easily check an error status */
108 
109 #define YA_ERROR_BASE(err_) ((err_) & 0xffff0000UL)
110 #define YA_ERROR_CODE(err_) ((err_) & 0x0000ffffUL)
111 
112 #if !DEBUG
113 #define FAIL(result__) ((result__)<0)
114 #define ISOK(result__) ((result__)>=0)
115 #else
116 
117 #if TRACK_RETURNED_GENERIC_ERROR
118 bool dnscore_monitored_isok(ya_result ret);
119 bool dnscore_monitored_fail(ya_result ret);
120 #define FAIL(result__) dnscore_monitored_fail((result__))
121 #define ISOK(result__) dnscore_monitored_isok((result__))
122 #else
123 #define FAIL(result) ((((u32)(result)) & ((u32)ERRNO_ERROR_BASE)) != 0) // trying to make scan-build understand
124 #define ISOK(result) ((((u32)(result)) & ((u32)ERRNO_ERROR_BASE)) == 0)
125 #endif
126 #endif
127 
128 /* 16 most significant bits : GROUP, the sign bit being ALWAYS set
129  * 16 least significant bits : ID
130  */
131 
132 #define ERRNO_ERROR                     ((s32)(ERRNO_ERROR_BASE+errno))
133 #define MAKE_ERRNO_ERROR(err_)          ((s32)(ERRNO_ERROR_BASE+(err_)))
134 
135 #define EXITFAIL(x) if((x)<0) {DIE(ERROR);exit(EXIT_FAILURE);}
136 
137 #define DNSMSG_ERROR_BASE               0xc0000000
138 
139 
140 /* -----------------------------------------------------------------------------
141  *
142  *   STRUCTS
143  */
144 
145 #define RCODE_ERROR_BASE                        0x80010000
146 #define RCODE_ERROR_CODE(code_)                 ((s32)(RCODE_ERROR_BASE+(code_)))
147 #define RCODE_ERROR_GETCODE(error_)             ((error_)&0xffff)
148 #define MAKE_DNSMSG_ERROR(err_)                 RCODE_ERROR_CODE(err_) // obsolete
149 #define MAKE_RCODE_ERROR(err_)                  RCODE_ERROR_CODE(err_)
150 
151 #define TSIG_BADSIG                             MAKE_RCODE_ERROR(16)      /* TSIG timestamp outisde of the time window    */
152 #define TSIG_BADKEY                             MAKE_RCODE_ERROR(17)      /* Unknown key name in TSIG record              */
153 #define TSIG_BADTIME                            MAKE_RCODE_ERROR(18)      /* TSIG timestamp outisde of the time window    */
154 #define TSIG_BADMODE                            MAKE_RCODE_ERROR(19)
155 #define TSIG_BADNAME                            MAKE_RCODE_ERROR(20)
156 #define TSIG_BADALG                             MAKE_RCODE_ERROR(21)
157 #define TSIG_BADTRUNC                           MAKE_RCODE_ERROR(22)
158 
159 #define CORE_ERROR_BASE                         0x80020000
160 #define CORE_ERROR_CODE(code_)                  ((s32)(CORE_ERROR_BASE+(code_)))
161 
162 #define LOGGER_INITIALISATION_ERROR             CORE_ERROR_CODE(1)
163 #define COMMAND_ARGUMENT_EXPECTED               CORE_ERROR_CODE(2)
164 #define OBJECT_NOT_INITIALIZED                  CORE_ERROR_CODE(3)
165 #define FORMAT_ALREADY_REGISTERED               CORE_ERROR_CODE(4)
166 #define STOPPED_BY_APPLICATION_SHUTDOWN         CORE_ERROR_CODE(5)
167 #define INVALID_STATE_ERROR                     CORE_ERROR_CODE(6)
168 #define FEATURE_NOT_IMPLEMENTED_ERROR           CORE_ERROR_CODE(7)
169 #define UNEXPECTED_NULL_ARGUMENT_ERROR          CORE_ERROR_CODE(8)
170 #define INVALID_ARGUMENT_ERROR                  CORE_ERROR_CODE(9)
171 #define UNABLE_TO_COMPLETE_FULL_READ            CORE_ERROR_CODE(11)
172 #define UNEXPECTED_EOF                          CORE_ERROR_CODE(12)
173 #define UNSUPPORTED_TYPE                        CORE_ERROR_CODE(13)
174 #define UNKNOWN_NAME                            CORE_ERROR_CODE(14)     /* name->value table */
175 #define BIGGER_THAN_PATH_MAX                    CORE_ERROR_CODE(15)
176 #define UNABLE_TO_COMPLETE_FULL_WRITE           CORE_ERROR_CODE(16)
177 #define BUFFER_WOULD_OVERFLOW                   CORE_ERROR_CODE(17)
178 #define CHROOT_NOT_A_DIRECTORY                  CORE_ERROR_CODE(18)
179 #define CHROOT_ALREADY_JAILED                   CORE_ERROR_CODE(19)
180 #define IP_VERSION_NOT_SUPPORTED                CORE_ERROR_CODE(20)
181 #define COLLECTION_DUPLICATE_ENTRY              CORE_ERROR_CODE(21)
182 #define INVALID_PATH                            CORE_ERROR_CODE(22)
183 #define PID_LOCKED                              CORE_ERROR_CODE(23)
184 #define ZALLOC_ERROR_MMAPFAILED                 CORE_ERROR_CODE(24)
185 #define ZALLOC_ERROR_OUTOFMEMORY                CORE_ERROR_CODE(25)
186 #define DIRECTORY_NOT_WRITABLE                  CORE_ERROR_CODE(26)
187 #define FEATURE_NOT_SUPPORTED                   CORE_ERROR_CODE(27)
188 #define LOCK_TIMEOUT                            CORE_ERROR_CODE(28)
189 #define CIRCULAR_FILE_FULL                      CORE_ERROR_CODE(29)
190 #define CIRCULAR_FILE_SHORT                     CORE_ERROR_CODE(30)
191 #define CIRCULAR_FILE_END                       CORE_ERROR_CODE(31)
192 #define CIRCULAR_FILE_LIMIT_EXCEEDED            CORE_ERROR_CODE(32)
193 #define DATA_FORMAT_ERROR                       CORE_ERROR_CODE(33)
194 #define LOCK_FAILED                             CORE_ERROR_CODE(34)
195 #define UNSUPPORTED_CLASS                       CORE_ERROR_CODE(35)
196 #define CANNOT_OPEN_FILE                        CORE_ERROR_CODE(36)
197 
198 #define PARSEB16_ERROR                          CORE_ERROR_CODE(0x1001)
199 #define PARSEB32_ERROR                          CORE_ERROR_CODE(0x1002)
200 #define PARSEB32H_ERROR                         CORE_ERROR_CODE(0x1003)
201 #define PARSEB64_ERROR                          CORE_ERROR_CODE(0x1004)
202 #define PARSEINT_ERROR                          CORE_ERROR_CODE(0x1005)
203 #define PARSEDATE_ERROR                         CORE_ERROR_CODE(0x1006)
204 #define PARSEIP_ERROR                           CORE_ERROR_CODE(0x1007)
205 #define PARSEWORD_NOMATCH_ERROR                 CORE_ERROR_CODE(0x1081)
206 #define PARSESTRING_ERROR                       CORE_ERROR_CODE(0x1082)
207 #define PARSE_BUFFER_TOO_SMALL_ERROR            CORE_ERROR_CODE(0x1083)
208 #define PARSE_INVALID_CHARACTER                 CORE_ERROR_CODE(0x1084)
209 #define PARSE_INVALID_ARGUMENT                  CORE_ERROR_CODE(0x1085)
210 #define PARSE_EMPTY_ARGUMENT                    CORE_ERROR_CODE(0x1086)
211 
212 #define CONFIG_SECTION_CALLBACK_ALREADY_SET     CORE_ERROR_CODE(0x1801)
213 #define CONFIG_SECTION_CALLBACK_NOT_SET         CORE_ERROR_CODE(0x1802)
214 #define CONFIG_SECTION_CALLBACK_NOT_FOUND       CORE_ERROR_CODE(0x1803)
215 #define CONFIG_NOT_A_REGULAR_FILE               CORE_ERROR_CODE(0x1804)
216 #define CONFIG_TOO_MANY_HOSTS                   CORE_ERROR_CODE(0x1805)
217 #define CONFIG_FQDN_NOT_ALLOWED                 CORE_ERROR_CODE(0x1806)
218 #define CONFIG_PORT_NOT_ALLOWED                 CORE_ERROR_CODE(0x1807)
219 #define CONFIG_EXPECTED_VALID_PORT_VALUE        CORE_ERROR_CODE(0x1808)
220 #define CONFIG_TSIG_NOT_ALLOWED                 CORE_ERROR_CODE(0x1809)
221 #define CONFIG_INTERNAL_ERROR                   CORE_ERROR_CODE(0x180a)
222 #define CONFIG_IPV4_NOT_ALLOWED                 CORE_ERROR_CODE(0x180b)
223 #define CONFIG_IPV6_NOT_ALLOWED                 CORE_ERROR_CODE(0x180c)
224 #define CONFIG_KEY_UNKNOWN                      CORE_ERROR_CODE(0x180d)
225 #define CONFIG_KEY_PARSE_ERROR                  CORE_ERROR_CODE(0x180e)
226 #define CONFIG_SECTION_ERROR                    CORE_ERROR_CODE(0x180f)
227 #define CONFIG_IS_BUSY                          CORE_ERROR_CODE(0x1810)
228 #define CONFIG_FILE_NOT_FOUND                   CORE_ERROR_CODE(0x1811)
229 
230 #define THREAD_CREATION_ERROR                   CORE_ERROR_CODE(0x2001)
231 #define THREAD_DOUBLEDESTRUCTION_ERROR          CORE_ERROR_CODE(0x2002)
232 #define SERVICE_ID_ERROR                        CORE_ERROR_CODE(0x2003)
233 #define SERVICE_WITHOUT_ENTRY_POINT             CORE_ERROR_CODE(0x2004)
234 #define SERVICE_ALREADY_INITIALISED             CORE_ERROR_CODE(0x2005)
235 #define SERVICE_ALREADY_RUNNING                 CORE_ERROR_CODE(0x2006)
236 #define SERVICE_NOT_RUNNING                     CORE_ERROR_CODE(0x2007)
237 #define SERVICE_NOT_INITIALISED                 CORE_ERROR_CODE(0x2008)
238 #define SERVICE_HAS_RUNNING_THREADS             CORE_ERROR_CODE(0x2009)
239 #define SERVICE_ALREADY_PAUSED                  CORE_ERROR_CODE(0x200a)
240 #define SERVICE_INITIALISATION_ERROR            CORE_ERROR_CODE(0x200b)
241 
242 #define TSIG_DUPLICATE_REGISTRATION             CORE_ERROR_CODE(0x3001)
243 #define TSIG_UNABLE_TO_SIGN                     CORE_ERROR_CODE(0x3002)
244 
245 #define NET_UNABLE_TO_RESOLVE_HOST              CORE_ERROR_CODE(0x4001)
246 #define TCP_RATE_TOO_SLOW                       CORE_ERROR_CODE(0x4002)
247 
248 #define CHARON_ERROR_FILE_LOCKED                CORE_ERROR_CODE(0x5001)
249 #define CHARON_ERROR_NOT_AUTHORISED             CORE_ERROR_CODE(0x5002)
250 #define CHARON_ERROR_UNKNOWN_ID                 CORE_ERROR_CODE(0x5003)
251 #define CHARON_ERROR_EXPECTED_MAGIC_HEAD        CORE_ERROR_CODE(0x5004)
252 #define CHARON_ERROR_INVALID_HEAD               CORE_ERROR_CODE(0x5006)
253 #define CHARON_ERROR_INVALID_TAIL               CORE_ERROR_CODE(0x5007)
254 #define CHARON_ERROR_INVALID_COMMAND            CORE_ERROR_CODE(0x5008)
255 #define CHARON_ERROR_COMMAND_SEQ_MISMATCHED     CORE_ERROR_CODE(0x5009)
256 #define CHARON_ERROR_UNKNOWN_MAGIC              CORE_ERROR_CODE(0x500a)
257 #define CHARON_ERROR_ALREADY_RUNNING            CORE_ERROR_CODE(0x500b)
258 #define CHARON_ERROR_ALREADY_STOPPED            CORE_ERROR_CODE(0x500c)
259 
260 #define LOGGER_CHANNEL_ALREADY_REGISTERED       CORE_ERROR_CODE(0x6001)
261 #define LOGGER_CHANNEL_NOT_REGISTERED           CORE_ERROR_CODE(0x6002)
262 #define LOGGER_CHANNEL_HAS_LINKS                CORE_ERROR_CODE(0x6003)
263 
264 #define ALARM_REARM                             CORE_ERROR_CODE(0xff00) // KEEP, used by alarm callback functions to automatically re-arm
265 
266 #define DNS_ERROR_BASE                          0x80030000
267 #define DNS_ERROR_CODE(code_)                   ((s32)(DNS_ERROR_BASE+(code_)))
268 #define IS_DNS_ERROR_CODE(code_)                (((code_)&0xffff0000) == DNS_ERROR_BASE)
269 #define DOMAIN_TOO_LONG                         DNS_ERROR_CODE(1)    /* FQDN is longer than 255           */
270 #define INCORRECT_IPADDRESS                     DNS_ERROR_CODE(2)    /* Incorrect ip address              */
271 #define INCORRECT_RDATA                         DNS_ERROR_CODE(3)
272 
273 #define LABEL_TOO_LONG                          DNS_ERROR_CODE(10)      /* label is longer than 63                      */
274 #define INVALID_CHARSET                         DNS_ERROR_CODE(11)      /*                                              */
275 #define ZONEFILE_INVALID_TYPE                   DNS_ERROR_CODE(12)      /* Type is unknown                              */
276 #define DOMAINNAME_INVALID                      DNS_ERROR_CODE(13)      /* invalid dnsname usually : double dot         */
277 
278 #define TSIG_FORMERR                            DNS_ERROR_CODE(14)
279 #define TSIG_SIZE_LIMIT_ERROR                   DNS_ERROR_CODE(15)
280 
281 #define UNPROCESSABLE_MESSAGE                   DNS_ERROR_CODE(23)
282 #define INVALID_PROTOCOL                        DNS_ERROR_CODE(24)
283 #define INVALID_RECORD                          DNS_ERROR_CODE(25)
284 #define UNSUPPORTED_RECORD                      DNS_ERROR_CODE(26)
285 #define ZONE_ALREADY_UP_TO_DATE                 DNS_ERROR_CODE(27)
286 #define UNKNOWN_DNS_TYPE                        DNS_ERROR_CODE(28)
287 #define UNKNOWN_DNS_CLASS                       DNS_ERROR_CODE(29)
288 
289 //#define INVALID_MESSAGE                         DNS_ERROR_CODE(30)
290 #define INVALID_MESSAGE                         UNPROCESSABLE_MESSAGE
291 
292 #define MESSAGE_HAS_WRONG_ID                    DNS_ERROR_CODE(31)
293 #define MESSAGE_IS_NOT_AN_ANSWER                DNS_ERROR_CODE(32)
294 #define MESSAGE_UNEXPECTED_ANSWER_DOMAIN        DNS_ERROR_CODE(33)
295 #define MESSAGE_UNEXPECTED_ANSWER_TYPE_CLASS    DNS_ERROR_CODE(34)
296 #define MESSAGE_CONTENT_OVERFLOW                DNS_ERROR_CODE(35)
297 #define MESSAGE_TRUNCATED                       DNS_ERROR_CODE(36)
298 
299 #define RRSIG_COVERED_TYPE_DIFFERS              DNS_ERROR_CODE(50)
300 #define RRSIG_OUTPUT_DIGEST_SIZE_TOO_BIG        DNS_ERROR_CODE(51)
301 #define RRSIG_UNSUPPORTED_COVERED_TYPE          DNS_ERROR_CODE(52)
302 #define RRSIG_VERIFICATION_FAILED               DNS_ERROR_CODE(53)
303 
304 #define DNSSEC_ALGORITHM_UNKOWN                     DNS_ERROR_CODE(100)
305 
306 /// @note EAI error codes are used for getaddrinfo
307 ///
308 /// @note EAI_ERROR_BADFLAGS error code is used for getaddrinfo through EAI_ERROR_CODE
309 /// @note EAI_ERROR_NONAME error code is used for getaddrinfo through EAI_ERROR_CODE
310 /// @note EAI_ERROR_AGAIN error code is used for getaddrinfo through EAI_ERROR_CODE
311 /// @note EAI_ERROR_FAIL error code is used for getaddrinfo through EAI_ERROR_CODE
312 /// @note EAI_ERROR_FAMILY error code is used for getaddrinfo through EAI_ERROR_CODE
313 /// @note EAI_ERROR_SOCKTYPE error code is used for getaddrinfo through EAI_ERROR_CODE
314 /// @note EAI_ERROR_SERVICE error code is used for getaddrinfo through EAI_ERROR_CODE
315 /// @note EAI_ERROR_MEMORY error code is used for getaddrinfo through EAI_ERROR_CODE
316 /// @note EAI_ERROR_SYSTEM error code is used for getaddrinfo through EAI_ERROR_CODE
317 /// @note EAI_ERROR_OVERFLOW error code is used for getaddrinfo through EAI_ERROR_CODE
318 
319 #define EAI_ERROR_BASE                          0x80090000
320 #define EAI_ERROR_CODE(code_)                   ((s32)(EAI_ERROR_BASE+(code_)))
321 
322 #define EAI_ERROR_BADFLAGS                      EAI_ERROR_CODE(-EAI_BADFLAGS)   /* minus because EAI_ values are < 0 */
323 #define EAI_ERROR_NONAME                        EAI_ERROR_CODE(-EAI_NONAME)
324 #define EAI_ERROR_AGAIN                         EAI_ERROR_CODE(-EAI_AGAIN)
325 #define EAI_ERROR_FAIL                          EAI_ERROR_CODE(-EAI_FAIL)
326 #define EAI_ERROR_FAMILY                        EAI_ERROR_CODE(-EAI_FAMILY)
327 #define EAI_ERROR_SOCKTYPE                      EAI_ERROR_CODE(-EAI_SOCKTYPE)
328 #define EAI_ERROR_SERVICE                       EAI_ERROR_CODE(-EAI_SERVICE)
329 #define EAI_ERROR_MEMORY                        EAI_ERROR_CODE(-EAI_MEMORY)
330 #define EAI_ERROR_SYSTEM                        EAI_ERROR_CODE(-EAI_SYSTEM)
331 #define EAI_ERROR_OVERFLOW                      EAI_ERROR_CODE(-EAI_OVERFLOW)
332 
333 #define EXIT_CODE_SELFCHECK_ERROR               249
334 #define EXIT_CODE_OUTOFMEMORY_ERROR             250
335 #define EXIT_CODE_THREADCREATE_ERROR            251
336 #define EXIT_CODE_FORMAT_ERROR                  252
337 #define EXIT_CODE_LOGLEVEL_ERROR                253
338 #define EXIT_CODE_LOGQUIT_ERROR                 254
339 
340 /* -----------------------------------------------------------------------------
341  *
342  *      PROTOTYPES
343  */
344 
345 void dief(ya_result error_code, const char *format, ...);
346 
347 /**
348  *
349  * Release the memory used by the error table
350  *
351  */
352 
353 void error_unregister_all();
354 
355 void error_register(ya_result code, const char * const text);
356 
357 /**
358  * @brief Returns the string associated to an error code
359  *
360  * Returns the string associated to an error code
361  *
362  * This is NOT thread-safe.  Only to be used
363  *
364  * @param[in] err the ya_result error code
365  *
366  * @return a pointer to the error message
367  */
368 
369 const char* error_gettext(ya_result code);
370 
371 struct output_stream;
372 
373 void error_writetext(struct output_stream *os, ya_result code);
374 
375 void dnscore_register_errors();
376 
377 ya_result ya_ssl_error();
378 
379 #define DIE(code) dief((code), "%s:%i\n", __FILE__, __LINE__);abort()
380 #define DIE_MSG(msg) dief(ERROR, "%s:%i %s\n", __FILE__, __LINE__, (msg));abort()
381 
382 #endif /* ERROR_H_ */
383 
384 /** @} */
385 
386