You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cfg_rcl.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef CFG_RCL_H_
  17. #define CFG_RCL_H_
  18. #include "config.h"
  19. #include "ucl.h"
  20. #include "mem_pool.h"
  21. #define CFG_RCL_ERROR cfg_rcl_error_quark ()
  22. static inline GQuark
  23. cfg_rcl_error_quark (void)
  24. {
  25. return g_quark_from_static_string ("cfg-rcl-error-quark");
  26. }
  27. struct rspamd_rcl_section;
  28. struct rspamd_config;
  29. struct rspamd_rcl_default_handler_data;
  30. enum rspamd_rcl_flag {
  31. RSPAMD_CL_FLAG_TIME_FLOAT = 0x1 << 0,
  32. RSPAMD_CL_FLAG_TIME_TIMEVAL = 0x1 << 1,
  33. RSPAMD_CL_FLAG_TIME_TIMESPEC = 0x1 << 2,
  34. RSPAMD_CL_FLAG_TIME_INTEGER = 0x1 << 3,
  35. RSPAMD_CL_FLAG_TIME_UINT_32 = 0x1 << 4,
  36. RSPAMD_CL_FLAG_INT_16 = 0x1 << 5,
  37. RSPAMD_CL_FLAG_INT_32 = 0x1 << 6,
  38. RSPAMD_CL_FLAG_INT_64 = 0x1 << 7,
  39. RSPAMD_CL_FLAG_UINT = 0x1 << 8,
  40. RSPAMD_CL_FLAG_INT_SIZE = 0x1 << 9,
  41. RSPAMD_CL_FLAG_STRING_PATH = 0x1 << 10,
  42. RSPAMD_CL_FLAG_BOOLEAN_INVERSE = 0x1 << 11,
  43. RSPAMD_CL_FLAG_STRING_LIST_HASH = 0x1 << 12,
  44. RSPAMD_CL_FLAG_MULTIPLE = 0x1 << 13
  45. };
  46. struct rspamd_rcl_struct_parser {
  47. gpointer user_struct;
  48. goffset offset;
  49. enum rspamd_rcl_flag flags;
  50. };
  51. /**
  52. * Common handler type
  53. * @param cfg configuration
  54. * @param obj object to parse
  55. * @param ud user data (depends on section)
  56. * @param err error object
  57. * @return TRUE if a section has been parsed
  58. */
  59. typedef gboolean (*rspamd_rcl_handler_t) (rspamd_mempool_t *pool,
  60. const ucl_object_t *obj,
  61. const gchar *key,
  62. gpointer ud,
  63. struct rspamd_rcl_section *section,
  64. GError **err);
  65. typedef gboolean (*rspamd_rcl_default_handler_t) (rspamd_mempool_t *pool,
  66. const ucl_object_t *obj,
  67. gpointer ud,
  68. struct rspamd_rcl_section *section,
  69. GError **err);
  70. /**
  71. * A handler type that is called at the end of section parsing
  72. * @param cfg configuration
  73. * @param ud user data
  74. */
  75. typedef void (*rspamd_rcl_section_fin_t)(rspamd_mempool_t *pool, gpointer ud);
  76. /**
  77. * Add a default handler for a section
  78. * @param section section pointer
  79. * @param name name of param
  80. * @param handler handler of param
  81. * @param offset offset in a structure
  82. * @param flags flags for the parser
  83. * @return newly created structure
  84. */
  85. struct rspamd_rcl_default_handler_data *rspamd_rcl_add_default_handler (
  86. struct rspamd_rcl_section *section,
  87. const gchar *name,
  88. rspamd_rcl_default_handler_t handler,
  89. goffset offset,
  90. gint flags,
  91. const gchar *doc_string);
  92. /**
  93. * Add new section to the configuration
  94. * @param top top section
  95. * @param name the name of the section
  96. * @param key_attr name of the attribute that should be used as key attribute
  97. * @param handler handler function for all attributes
  98. * @param type type of object handled by a handler
  99. * @param required whether at least one of these sections is required
  100. * @param strict_type turn on strict check for types for this section
  101. * @return newly created structure
  102. */
  103. struct rspamd_rcl_section *rspamd_rcl_add_section (
  104. struct rspamd_rcl_section **top,
  105. const gchar *name, const gchar *key_attr,
  106. rspamd_rcl_handler_t handler,
  107. enum ucl_type type, gboolean required, gboolean strict_type);
  108. struct rspamd_rcl_section *rspamd_rcl_add_section_doc (
  109. struct rspamd_rcl_section **top,
  110. const gchar *name, const gchar *key_attr,
  111. rspamd_rcl_handler_t handler,
  112. enum ucl_type type, gboolean required,
  113. gboolean strict_type,
  114. ucl_object_t *doc_target,
  115. const gchar *doc_string);
  116. /**
  117. * Init common sections known to rspamd
  118. * @return top section
  119. */
  120. struct rspamd_rcl_section * rspamd_rcl_config_init (struct rspamd_config *cfg);
  121. /**
  122. * Get a section specified by path, it understand paths separated by '/' character
  123. * @param top top section
  124. * @param path '/' divided path
  125. * @return
  126. */
  127. struct rspamd_rcl_section * rspamd_rcl_config_get_section (
  128. struct rspamd_rcl_section *top,
  129. const char *path);
  130. /**
  131. * Read RCL configuration and parse it to a config file
  132. * @param top top section
  133. * @param cfg target configuration
  134. * @param obj object to handle
  135. * @return TRUE if an object can be parsed
  136. */
  137. gboolean rspamd_rcl_parse (struct rspamd_rcl_section *top,
  138. gpointer ptr, rspamd_mempool_t *pool,
  139. const ucl_object_t *obj, GError **err);
  140. /**
  141. * Parse default structure for a section
  142. * @param section section
  143. * @param cfg config file
  144. * @param obj object to parse
  145. * @param ptr ptr to pass
  146. * @param err error ptr
  147. * @return TRUE if the object has been parsed
  148. */
  149. gboolean rspamd_rcl_section_parse_defaults (struct rspamd_rcl_section *section,
  150. rspamd_mempool_t *pool, const ucl_object_t *obj, gpointer ptr,
  151. GError **err);
  152. /**
  153. * Here is a section of common handlers that accepts rcl_struct_parser
  154. * which itself contains a struct pointer and the offset of a member in a
  155. * specific structure
  156. */
  157. /**
  158. * Parse a string field of a structure
  159. * @param cfg config pointer
  160. * @param obj object to parse
  161. * @param ud struct_parser structure
  162. * @param section the current section
  163. * @param err error pointer
  164. * @return TRUE if a string value has been successfully parsed
  165. */
  166. gboolean rspamd_rcl_parse_struct_string (rspamd_mempool_t *pool,
  167. const ucl_object_t *obj,
  168. gpointer ud,
  169. struct rspamd_rcl_section *section,
  170. GError **err);
  171. /**
  172. * Parse an integer field of a structure
  173. * @param cfg config pointer
  174. * @param obj object to parse
  175. * @param ud struct_parser structure
  176. * @param section the current section
  177. * @param err error pointer
  178. * @return TRUE if a value has been successfully parsed
  179. */
  180. gboolean rspamd_rcl_parse_struct_integer (rspamd_mempool_t *pool,
  181. const ucl_object_t *obj,
  182. gpointer ud,
  183. struct rspamd_rcl_section *section,
  184. GError **err);
  185. /**
  186. * Parse a float field of a structure
  187. * @param cfg config pointer
  188. * @param obj object to parse
  189. * @param ud struct_parser structure
  190. * @param section the current section
  191. * @param err error pointer
  192. * @return TRUE if a value has been successfully parsed
  193. */
  194. gboolean rspamd_rcl_parse_struct_double (rspamd_mempool_t *pool,
  195. const ucl_object_t *obj,
  196. gpointer ud,
  197. struct rspamd_rcl_section *section,
  198. GError **err);
  199. /**
  200. * Parse a time field of a structure
  201. * @param cfg config pointer
  202. * @param obj object to parse
  203. * @param ud struct_parser structure (flags mean the exact structure used)
  204. * @param section the current section
  205. * @param err error pointer
  206. * @return TRUE if a value has been successfully parsed
  207. */
  208. gboolean rspamd_rcl_parse_struct_time (rspamd_mempool_t *pool,
  209. const ucl_object_t *obj,
  210. gpointer ud,
  211. struct rspamd_rcl_section *section,
  212. GError **err);
  213. /**
  214. * Parse a string list field of a structure presented by a GList* object
  215. * @param cfg config pointer
  216. * @param obj object to parse
  217. * @param ud struct_parser structure (flags mean the exact structure used)
  218. * @param section the current section
  219. * @param err error pointer
  220. * @return TRUE if a value has been successfully parsed
  221. */
  222. gboolean rspamd_rcl_parse_struct_string_list (rspamd_mempool_t *pool,
  223. const ucl_object_t *obj,
  224. gpointer ud,
  225. struct rspamd_rcl_section *section,
  226. GError **err);
  227. /**
  228. * Parse a boolean field of a structure
  229. * @param cfg config pointer
  230. * @param obj object to parse
  231. * @param ud struct_parser structure (flags mean the exact structure used)
  232. * @param section the current section
  233. * @param err error pointer
  234. * @return TRUE if a value has been successfully parsed
  235. */
  236. gboolean rspamd_rcl_parse_struct_boolean (rspamd_mempool_t *pool,
  237. const ucl_object_t *obj,
  238. gpointer ud,
  239. struct rspamd_rcl_section *section,
  240. GError **err);
  241. /**
  242. * Parse a keypair field of a structure
  243. * @param cfg config pointer
  244. * @param obj object to parse
  245. * @param ud struct_parser structure (flags mean the exact structure used)
  246. * @param section the current section
  247. * @param err error pointer
  248. * @return TRUE if a value has been successfully parsed
  249. */
  250. gboolean rspamd_rcl_parse_struct_keypair (rspamd_mempool_t *pool,
  251. const ucl_object_t *obj,
  252. gpointer ud,
  253. struct rspamd_rcl_section *section,
  254. GError **err);
  255. /**
  256. * Parse a inet addr field of a structure
  257. * @param cfg config pointer
  258. * @param obj object to parse
  259. * @param ud struct_parser structure (flags mean the exact structure used)
  260. * @param section the current section
  261. * @param err error pointer
  262. * @return TRUE if a value has been successfully parsed
  263. */
  264. gboolean rspamd_rcl_parse_struct_addr (rspamd_mempool_t *pool,
  265. const ucl_object_t *obj,
  266. gpointer ud,
  267. struct rspamd_rcl_section *section,
  268. GError **err);
  269. /**
  270. * Parse a gmime inet address field of a structure
  271. * @param cfg config pointer
  272. * @param obj object to parse
  273. * @param ud struct_parser structure (flags mean the exact structure used)
  274. * @param section the current section
  275. * @param err error pointer
  276. * @return TRUE if a value has been successfully parsed
  277. */
  278. gboolean rspamd_rcl_parse_struct_mime_addr (rspamd_mempool_t *pool,
  279. const ucl_object_t *obj,
  280. gpointer ud,
  281. struct rspamd_rcl_section *section,
  282. GError **err);
  283. /**
  284. * Utility functions
  285. */
  286. /**
  287. * Register new parser for a worker type of an option with the specified name
  288. * @param cfg config structure
  289. * @param type type of worker (GQuark)
  290. * @param name name of option
  291. * @param handler handler of option
  292. * @param target opaque target structure
  293. * @param offset offset inside a structure
  294. */
  295. void rspamd_rcl_register_worker_option (struct rspamd_config *cfg,
  296. GQuark type,
  297. const gchar *name,
  298. rspamd_rcl_default_handler_t handler,
  299. gpointer target,
  300. glong offset,
  301. gint flags,
  302. const gchar *doc_string);
  303. /**
  304. * Register a default parser for a worker
  305. * @param cfg config structure
  306. * @param type type of worker (GQuark)
  307. * @param func handler function
  308. * @param ud userdata for handler function
  309. */
  310. void rspamd_rcl_register_worker_parser (struct rspamd_config *cfg, gint type,
  311. gboolean (*func)(ucl_object_t *, gpointer), gpointer ud);
  312. /**
  313. * Adds new documentation object to the configuration
  314. * @param doc_target target object where to insert documentation (top object is used if this is NULL)
  315. * @param doc_object documentation object to insert
  316. */
  317. ucl_object_t *rspamd_rcl_add_doc_obj (ucl_object_t *doc_target,
  318. const char *doc_string,
  319. const char *doc_name,
  320. ucl_type_t type,
  321. rspamd_rcl_default_handler_t handler,
  322. gint flags,
  323. const char *default_value,
  324. gboolean required);
  325. /**
  326. * Adds new documentation option specified by path `doc_path` that should be
  327. * splitted by dots
  328. */
  329. ucl_object_t *rspamd_rcl_add_doc_by_path (struct rspamd_config *cfg,
  330. const gchar *doc_path,
  331. const char *doc_string,
  332. const char *doc_name,
  333. ucl_type_t type,
  334. rspamd_rcl_default_handler_t handler,
  335. gint flags,
  336. const char *default_value,
  337. gboolean required);
  338. #endif /* CFG_RCL_H_ */