00001 /* 00002 * Copyright (C) 2004-2007, 2010, 2013-2015 Internet Systems Consortium, Inc. ("ISC") 00003 * Copyright (C) 2000-2002 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: cfg.h,v 1.46 2010/08/13 23:47:04 tbox Exp $ */ 00019 00020 #ifndef ISCCFG_CFG_H 00021 #define ISCCFG_CFG_H 1 00022 00023 /***** 00024 ***** Module Info 00025 *****/ 00026 00027 /*! \file isccfg/cfg.h 00028 * \brief 00029 * This is the new, table-driven, YACC-free configuration file parser. 00030 */ 00031 00032 /*** 00033 *** Imports 00034 ***/ 00035 00036 #include <isc/formatcheck.h> 00037 #include <isc/lang.h> 00038 #include <isc/refcount.h> 00039 #include <isc/types.h> 00040 #include <isc/list.h> 00041 00042 /*** 00043 *** Types 00044 ***/ 00045 00046 /*% 00047 * A configuration parser. 00048 */ 00049 typedef struct cfg_parser cfg_parser_t; 00050 00051 /*% 00052 * A configuration type definition object. There is a single 00053 * static cfg_type_t object for each data type supported by 00054 * the configuration parser. 00055 */ 00056 typedef struct cfg_type cfg_type_t; 00057 00058 /*% 00059 * A configuration object. This is the basic building block of the 00060 * configuration parse tree. It contains a value (which may be 00061 * of one of several types) and information identifying the file 00062 * and line number the value came from, for printing error 00063 * messages. 00064 */ 00065 typedef struct cfg_obj cfg_obj_t; 00066 00067 /*% 00068 * A configuration object list element. 00069 */ 00070 typedef struct cfg_listelt cfg_listelt_t; 00071 00072 /*% 00073 * A callback function to be called when parsing an option 00074 * that needs to be interpreted at parsing time, like 00075 * "directory". 00076 */ 00077 typedef isc_result_t 00078 (*cfg_parsecallback_t)(const char *clausename, const cfg_obj_t *obj, void *arg); 00079 00080 /*** 00081 *** Functions 00082 ***/ 00083 00084 ISC_LANG_BEGINDECLS 00085 00086 void 00087 cfg_parser_attach(cfg_parser_t *src, cfg_parser_t **dest); 00088 /*%< 00089 * Reference a parser object. 00090 */ 00091 00092 isc_result_t 00093 cfg_parser_create(isc_mem_t *mctx, isc_log_t *lctx, cfg_parser_t **ret); 00094 /*%< 00095 * Create a configuration file parser. Any warning and error 00096 * messages will be logged to 'lctx'. 00097 * 00098 * The parser object returned can be used for a single call 00099 * to cfg_parse_file() or cfg_parse_buffer(). It must not 00100 * be reused for parsing multiple files or buffers. 00101 */ 00102 00103 void 00104 cfg_parser_setcallback(cfg_parser_t *pctx, 00105 cfg_parsecallback_t callback, 00106 void *arg); 00107 /*%< 00108 * Make the parser call 'callback' whenever it encounters 00109 * a configuration clause with the callback attribute, 00110 * passing it the clause name, the clause value, 00111 * and 'arg' as arguments. 00112 * 00113 * To restore the default of not invoking callbacks, pass 00114 * callback==NULL and arg==NULL. 00115 */ 00116 00117 isc_result_t 00118 cfg_parse_file(cfg_parser_t *pctx, const char *filename, 00119 const cfg_type_t *type, cfg_obj_t **ret); 00120 isc_result_t 00121 cfg_parse_buffer(cfg_parser_t *pctx, isc_buffer_t *buffer, 00122 const cfg_type_t *type, cfg_obj_t **ret); 00123 /*%< 00124 * Read a configuration containing data of type 'type' 00125 * and make '*ret' point to its parse tree. 00126 * 00127 * The configuration is read from the file 'filename' 00128 * (isc_parse_file()) or the buffer 'buffer' 00129 * (isc_parse_buffer()). 00130 * 00131 * Returns an error if the file does not parse correctly. 00132 * 00133 * Requires: 00134 *\li "filename" is valid. 00135 *\li "mem" is valid. 00136 *\li "type" is valid. 00137 *\li "cfg" is non-NULL and "*cfg" is NULL. 00138 * 00139 * Returns: 00140 * \li #ISC_R_SUCCESS - success 00141 *\li #ISC_R_NOMEMORY - no memory available 00142 *\li #ISC_R_INVALIDFILE - file doesn't exist or is unreadable 00143 *\li others - file contains errors 00144 */ 00145 00146 isc_result_t 00147 cfg_parser_mapadd(cfg_parser_t *pctx, cfg_obj_t *mapobj, 00148 cfg_obj_t *obj, const char *clause); 00149 /*%< 00150 * Add the object 'obj' to the specified clause in mapbody 'mapobj'. 00151 * Used for adding new zones. 00152 * 00153 * Require: 00154 * \li 'obj' is a valid cfg_obj_t. 00155 * \li 'mapobj' is a valid cfg_obj_t of type map. 00156 * \li 'pctx' is a valid cfg_parser_t. 00157 */ 00158 00159 void 00160 cfg_parser_reset(cfg_parser_t *pctx); 00161 /*%< 00162 * Reset an existing parser so it can be re-used for a new file or 00163 * buffer. 00164 */ 00165 00166 void 00167 cfg_parser_destroy(cfg_parser_t **pctxp); 00168 /*%< 00169 * Remove a reference to a configuration parser; destroy it if there are no 00170 * more references. 00171 */ 00172 00173 isc_boolean_t 00174 cfg_obj_isvoid(const cfg_obj_t *obj); 00175 /*%< 00176 * Return true iff 'obj' is of void type (e.g., an optional 00177 * value not specified). 00178 */ 00179 00180 isc_boolean_t 00181 cfg_obj_ismap(const cfg_obj_t *obj); 00182 /*%< 00183 * Return true iff 'obj' is of a map type. 00184 */ 00185 00186 isc_result_t 00187 cfg_map_get(const cfg_obj_t *mapobj, const char* name, const cfg_obj_t **obj); 00188 /*%< 00189 * Extract an element from a configuration object, which 00190 * must be of a map type. 00191 * 00192 * Requires: 00193 * \li 'mapobj' points to a valid configuration object of a map type. 00194 * \li 'name' points to a null-terminated string. 00195 * \li 'obj' is non-NULL and '*obj' is NULL. 00196 * 00197 * Returns: 00198 * \li #ISC_R_SUCCESS - success 00199 * \li #ISC_R_NOTFOUND - name not found in map 00200 */ 00201 00202 const cfg_obj_t * 00203 cfg_map_getname(const cfg_obj_t *mapobj); 00204 /*%< 00205 * Get the name of a named map object, like a server "key" clause. 00206 * 00207 * Requires: 00208 * \li 'mapobj' points to a valid configuration object of a map type. 00209 * 00210 * Returns: 00211 * \li A pointer to a configuration object naming the map object, 00212 * or NULL if the map object does not have a name. 00213 */ 00214 00215 unsigned int 00216 cfg_map_count(const cfg_obj_t *mapobj); 00217 /*%< 00218 * Get the number of elements defined in the symbol table of a map object. 00219 * 00220 * Requires: 00221 * \li 'mapobj' points to a valid configuration object of a map type. 00222 * 00223 * Returns: 00224 * \li The number of elements in the map object. 00225 */ 00226 00227 isc_boolean_t 00228 cfg_obj_istuple(const cfg_obj_t *obj); 00229 /*%< 00230 * Return true iff 'obj' is of a map type. 00231 */ 00232 00233 const cfg_obj_t * 00234 cfg_tuple_get(const cfg_obj_t *tupleobj, const char *name); 00235 /*%< 00236 * Extract an element from a configuration object, which 00237 * must be of a tuple type. 00238 * 00239 * Requires: 00240 * \li 'tupleobj' points to a valid configuration object of a tuple type. 00241 * \li 'name' points to a null-terminated string naming one of the 00242 *\li fields of said tuple type. 00243 */ 00244 00245 isc_boolean_t 00246 cfg_obj_isuint32(const cfg_obj_t *obj); 00247 /*%< 00248 * Return true iff 'obj' is of integer type. 00249 */ 00250 00251 isc_uint32_t 00252 cfg_obj_asuint32(const cfg_obj_t *obj); 00253 /*%< 00254 * Returns the value of a configuration object of 32-bit integer type. 00255 * 00256 * Requires: 00257 * \li 'obj' points to a valid configuration object of 32-bit integer type. 00258 * 00259 * Returns: 00260 * \li A 32-bit unsigned integer. 00261 */ 00262 00263 isc_boolean_t 00264 cfg_obj_isuint64(const cfg_obj_t *obj); 00265 /*%< 00266 * Return true iff 'obj' is of integer type. 00267 */ 00268 00269 isc_uint64_t 00270 cfg_obj_asuint64(const cfg_obj_t *obj); 00271 /*%< 00272 * Returns the value of a configuration object of 64-bit integer type. 00273 * 00274 * Requires: 00275 * \li 'obj' points to a valid configuration object of 64-bit integer type. 00276 * 00277 * Returns: 00278 * \li A 64-bit unsigned integer. 00279 */ 00280 00281 isc_boolean_t 00282 cfg_obj_isstring(const cfg_obj_t *obj); 00283 /*%< 00284 * Return true iff 'obj' is of string type. 00285 */ 00286 00287 const char * 00288 cfg_obj_asstring(const cfg_obj_t *obj); 00289 /*%< 00290 * Returns the value of a configuration object of a string type 00291 * as a null-terminated string. 00292 * 00293 * Requires: 00294 * \li 'obj' points to a valid configuration object of a string type. 00295 * 00296 * Returns: 00297 * \li A pointer to a null terminated string. 00298 */ 00299 00300 isc_boolean_t 00301 cfg_obj_isboolean(const cfg_obj_t *obj); 00302 /*%< 00303 * Return true iff 'obj' is of a boolean type. 00304 */ 00305 00306 isc_boolean_t 00307 cfg_obj_asboolean(const cfg_obj_t *obj); 00308 /*%< 00309 * Returns the value of a configuration object of a boolean type. 00310 * 00311 * Requires: 00312 * \li 'obj' points to a valid configuration object of a boolean type. 00313 * 00314 * Returns: 00315 * \li A boolean value. 00316 */ 00317 00318 isc_boolean_t 00319 cfg_obj_issockaddr(const cfg_obj_t *obj); 00320 /*%< 00321 * Return true iff 'obj' is a socket address. 00322 */ 00323 00324 const isc_sockaddr_t * 00325 cfg_obj_assockaddr(const cfg_obj_t *obj); 00326 /*%< 00327 * Returns the value of a configuration object representing a socket address. 00328 * 00329 * Requires: 00330 * \li 'obj' points to a valid configuration object of a socket address type. 00331 * 00332 * Returns: 00333 * \li A pointer to a sockaddr. The sockaddr must be copied by the caller 00334 * if necessary. 00335 */ 00336 00337 isc_dscp_t 00338 cfg_obj_getdscp(const cfg_obj_t *obj); 00339 /*%< 00340 * Returns the DSCP value of a configuration object representing a 00341 * socket address. 00342 * 00343 * Requires: 00344 * \li 'obj' points to a valid configuration object of a 00345 * socket address type. 00346 * 00347 * Returns: 00348 * \li DSCP value associated with a sockaddr, or -1. 00349 */ 00350 00351 isc_boolean_t 00352 cfg_obj_isnetprefix(const cfg_obj_t *obj); 00353 /*%< 00354 * Return true iff 'obj' is a network prefix. 00355 */ 00356 00357 void 00358 cfg_obj_asnetprefix(const cfg_obj_t *obj, isc_netaddr_t *netaddr, 00359 unsigned int *prefixlen); 00360 /*%< 00361 * Gets the value of a configuration object representing a network 00362 * prefix. The network address is returned through 'netaddr' and the 00363 * prefix length in bits through 'prefixlen'. 00364 * 00365 * Requires: 00366 * \li 'obj' points to a valid configuration object of network prefix type. 00367 *\li 'netaddr' and 'prefixlen' are non-NULL. 00368 */ 00369 00370 isc_boolean_t 00371 cfg_obj_islist(const cfg_obj_t *obj); 00372 /*%< 00373 * Return true iff 'obj' is of list type. 00374 */ 00375 00376 const cfg_listelt_t * 00377 cfg_list_first(const cfg_obj_t *obj); 00378 /*%< 00379 * Returns the first list element in a configuration object of a list type. 00380 * 00381 * Requires: 00382 * \li 'obj' points to a valid configuration object of a list type or NULL. 00383 * 00384 * Returns: 00385 * \li A pointer to a cfg_listelt_t representing the first list element, 00386 * or NULL if the list is empty or nonexistent. 00387 */ 00388 00389 const cfg_listelt_t * 00390 cfg_list_next(const cfg_listelt_t *elt); 00391 /*%< 00392 * Returns the next element of a list of configuration objects. 00393 * 00394 * Requires: 00395 * \li 'elt' points to cfg_listelt_t obtained from cfg_list_first() or 00396 * a previous call to cfg_list_next(). 00397 * 00398 * Returns: 00399 * \li A pointer to a cfg_listelt_t representing the next element, 00400 * or NULL if there are no more elements. 00401 */ 00402 00403 unsigned int 00404 cfg_list_length(const cfg_obj_t *obj, isc_boolean_t recurse); 00405 /*%< 00406 * Returns the length of a list of configure objects. If obj is 00407 * not a list, returns 0. If recurse is true, add in the length of 00408 * all contained lists. 00409 */ 00410 00411 cfg_obj_t * 00412 cfg_listelt_value(const cfg_listelt_t *elt); 00413 /*%< 00414 * Returns the configuration object associated with cfg_listelt_t. 00415 * 00416 * Requires: 00417 * \li 'elt' points to cfg_listelt_t obtained from cfg_list_first() or 00418 * cfg_list_next(). 00419 * 00420 * Returns: 00421 * \li A non-NULL pointer to a configuration object. 00422 */ 00423 00424 void 00425 cfg_print(const cfg_obj_t *obj, 00426 void (*f)(void *closure, const char *text, int textlen), 00427 void *closure); 00428 void 00429 cfg_printx(const cfg_obj_t *obj, unsigned int flags, 00430 void (*f)(void *closure, const char *text, int textlen), 00431 void *closure); 00432 00433 #define CFG_PRINTER_XKEY 0x1 /* '?' out shared keys. */ 00434 #define CFG_PRINTER_ONELINE 0x2 /* print config as a single line */ 00435 00436 /*%< 00437 * Print the configuration object 'obj' by repeatedly calling the 00438 * function 'f', passing 'closure' and a region of text starting 00439 * at 'text' and comprising 'textlen' characters. 00440 * 00441 * If CFG_PRINTER_XKEY the contents of shared keys will be obscured 00442 * by replacing them with question marks ('?') 00443 */ 00444 00445 void 00446 cfg_print_grammar(const cfg_type_t *type, 00447 void (*f)(void *closure, const char *text, int textlen), 00448 void *closure); 00449 /*%< 00450 * Print a summary of the grammar of the configuration type 'type'. 00451 */ 00452 00453 isc_boolean_t 00454 cfg_obj_istype(const cfg_obj_t *obj, const cfg_type_t *type); 00455 /*%< 00456 * Return true iff 'obj' is of type 'type'. 00457 */ 00458 00459 void 00460 cfg_obj_attach(cfg_obj_t *src, cfg_obj_t **dest); 00461 /*%< 00462 * Reference a configuration object. 00463 */ 00464 00465 void 00466 cfg_obj_destroy(cfg_parser_t *pctx, cfg_obj_t **obj); 00467 /*%< 00468 * Delete a reference to a configuration object; destroy the object if 00469 * there are no more references. 00470 * 00471 * Require: 00472 * \li '*obj' is a valid cfg_obj_t. 00473 * \li 'pctx' is a valid cfg_parser_t. 00474 */ 00475 00476 void 00477 cfg_obj_log(const cfg_obj_t *obj, isc_log_t *lctx, int level, 00478 const char *fmt, ...) 00479 ISC_FORMAT_PRINTF(4, 5); 00480 /*%< 00481 * Log a message concerning configuration object 'obj' to the logging 00482 * channel of 'pctx', at log level 'level'. The message will be prefixed 00483 * with the file name(s) and line number where 'obj' was defined. 00484 */ 00485 00486 const char * 00487 cfg_obj_file(const cfg_obj_t *obj); 00488 /*%< 00489 * Return the file that defined this object. 00490 */ 00491 00492 unsigned int 00493 cfg_obj_line(const cfg_obj_t *obj); 00494 /*%< 00495 * Return the line in file where this object was defined. 00496 */ 00497 00498 ISC_LANG_ENDDECLS 00499 00500 #endif /* ISCCFG_CFG_H */