PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/openldap-2.4.31/evo-openldap-2.4.31/libraries/librewrite/rewrite-int.h

#
C++ Header | 627 lines | 336 code | 89 blank | 202 comment | 3 complexity | 3f33eba98b5bafb443d5948c31c49c57 MD5 | raw file
Possible License(s): GPL-3.0, MPL-2.0-no-copyleft-exception
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 2000-2012 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /* ACKNOWLEDGEMENT:
  16. * This work was initially developed by Pierangelo Masarati for
  17. * inclusion in OpenLDAP Software.
  18. */
  19. #ifndef REWRITE_INT_H
  20. #define REWRITE_INT_H
  21. /*
  22. * These are required by every file of the library, so they're included here
  23. */
  24. #include <ac/stdlib.h>
  25. #include <ac/string.h>
  26. #include <ac/syslog.h>
  27. #include <ac/regex.h>
  28. #include <ac/socket.h>
  29. #include <ac/unistd.h>
  30. #include <ac/ctype.h>
  31. #include <lber.h>
  32. #include <ldap.h>
  33. #define LDAP_DEFINE_LDAP_DEBUG
  34. #include <ldap_log.h>
  35. #include <lutil.h>
  36. #include <avl.h>
  37. #include <rewrite.h>
  38. #define malloc(x) ber_memalloc(x)
  39. #define calloc(x,y) ber_memcalloc(x,y)
  40. #define realloc(x,y) ber_memrealloc(x,y)
  41. #define free(x) ber_memfree(x)
  42. #undef strdup
  43. #define strdup(x) ber_strdup(x)
  44. /* Uncomment to use ldap pvt threads */
  45. #define USE_REWRITE_LDAP_PVT_THREADS
  46. #include <ldap_pvt_thread.h>
  47. /*
  48. * For details, see RATIONALE.
  49. */
  50. #define REWRITE_MAX_MATCH 11 /* 0: overall string; 1-9: submatches */
  51. #define REWRITE_MAX_PASSES 100
  52. /*
  53. * Submatch escape char
  54. */
  55. /* the '\' conflicts with slapd.conf parsing */
  56. /* #define REWRITE_SUBMATCH_ESCAPE '\\' */
  57. #define REWRITE_SUBMATCH_ESCAPE_ORIG '%'
  58. #define REWRITE_SUBMATCH_ESCAPE '$'
  59. #define IS_REWRITE_SUBMATCH_ESCAPE(c) \
  60. ((c) == REWRITE_SUBMATCH_ESCAPE || (c) == REWRITE_SUBMATCH_ESCAPE_ORIG)
  61. /*
  62. * REGEX flags
  63. */
  64. #define REWRITE_FLAG_HONORCASE 'C'
  65. #define REWRITE_FLAG_BASICREGEX 'R'
  66. /*
  67. * Action flags
  68. */
  69. #define REWRITE_FLAG_EXECONCE ':'
  70. #define REWRITE_FLAG_STOP '@'
  71. #define REWRITE_FLAG_UNWILLING '#'
  72. #define REWRITE_FLAG_GOTO 'G' /* requires an arg */
  73. #define REWRITE_FLAG_USER 'U' /* requires an arg */
  74. #define REWRITE_FLAG_MAX_PASSES 'M' /* requires an arg */
  75. #define REWRITE_FLAG_IGNORE_ERR 'I'
  76. /*
  77. * Map operators
  78. */
  79. #define REWRITE_OPERATOR_SUBCONTEXT '>'
  80. #define REWRITE_OPERATOR_COMMAND '|'
  81. #define REWRITE_OPERATOR_VARIABLE_SET '&'
  82. #define REWRITE_OPERATOR_VARIABLE_GET '*'
  83. #define REWRITE_OPERATOR_PARAM_GET '$'
  84. /***********
  85. * PRIVATE *
  86. ***********/
  87. /*
  88. * Action
  89. */
  90. struct rewrite_action {
  91. struct rewrite_action *la_next;
  92. #define REWRITE_ACTION_STOP 0x0001
  93. #define REWRITE_ACTION_UNWILLING 0x0002
  94. #define REWRITE_ACTION_GOTO 0x0003
  95. #define REWRITE_ACTION_IGNORE_ERR 0x0004
  96. #define REWRITE_ACTION_USER 0x0005
  97. int la_type;
  98. void *la_args;
  99. };
  100. /*
  101. * Map
  102. */
  103. struct rewrite_map {
  104. /*
  105. * Legacy stuff
  106. */
  107. #define REWRITE_MAP_XFILEMAP 0x0001 /* Rough implementation! */
  108. #define REWRITE_MAP_XPWDMAP 0x0002 /* uid -> gecos */
  109. #define REWRITE_MAP_XLDAPMAP 0x0003 /* Not implemented yet! */
  110. /*
  111. * Maps with args
  112. */
  113. #define REWRITE_MAP_SUBCONTEXT 0x0101
  114. #define REWRITE_MAP_SET_OP_VAR 0x0102
  115. #define REWRITE_MAP_SETW_OP_VAR 0x0103
  116. #define REWRITE_MAP_GET_OP_VAR 0x0104
  117. #define REWRITE_MAP_SET_SESN_VAR 0x0105
  118. #define REWRITE_MAP_SETW_SESN_VAR 0x0106
  119. #define REWRITE_MAP_GET_SESN_VAR 0x0107
  120. #define REWRITE_MAP_GET_PARAM 0x0108
  121. #define REWRITE_MAP_BUILTIN 0x0109
  122. int lm_type;
  123. char *lm_name;
  124. void *lm_data;
  125. /*
  126. * Old maps store private data in _lm_args;
  127. * new maps store the substitution pattern in _lm_subst
  128. */
  129. union {
  130. void *_lm_args;
  131. struct rewrite_subst *_lm_subst;
  132. } lm_union;
  133. #define lm_args lm_union._lm_args
  134. #define lm_subst lm_union._lm_subst
  135. #ifdef USE_REWRITE_LDAP_PVT_THREADS
  136. ldap_pvt_thread_mutex_t lm_mutex;
  137. #endif /* USE_REWRITE_LDAP_PVT_THREADS */
  138. };
  139. /*
  140. * Builtin maps
  141. */
  142. struct rewrite_builtin_map {
  143. #define REWRITE_BUILTIN_MAP 0x0200
  144. int lb_type;
  145. char *lb_name;
  146. void *lb_private;
  147. const rewrite_mapper *lb_mapper;
  148. #ifdef USE_REWRITE_LDAP_PVT_THREADS
  149. ldap_pvt_thread_mutex_t lb_mutex;
  150. #endif /* USE_REWRITE_LDAP_PVT_THREADS */
  151. };
  152. /*
  153. * Submatch substitution
  154. */
  155. struct rewrite_submatch {
  156. #define REWRITE_SUBMATCH_ASIS 0x0000
  157. #define REWRITE_SUBMATCH_XMAP 0x0001
  158. #define REWRITE_SUBMATCH_MAP_W_ARG 0x0002
  159. int ls_type;
  160. struct rewrite_map *ls_map;
  161. int ls_submatch;
  162. /*
  163. * The first one represents the index of the submatch in case
  164. * the map has single submatch as argument;
  165. * the latter represents the map argument scheme in case
  166. * the map has substitution string argument form
  167. */
  168. };
  169. /*
  170. * Pattern substitution
  171. */
  172. struct rewrite_subst {
  173. size_t lt_subs_len;
  174. struct berval *lt_subs;
  175. int lt_num_submatch;
  176. struct rewrite_submatch *lt_submatch;
  177. };
  178. /*
  179. * Rule
  180. */
  181. struct rewrite_rule {
  182. struct rewrite_rule *lr_next;
  183. struct rewrite_rule *lr_prev;
  184. char *lr_pattern;
  185. char *lr_subststring;
  186. char *lr_flagstring;
  187. regex_t lr_regex;
  188. /*
  189. * I was thinking about some kind of per-rule mutex, but there's
  190. * probably no need, because rules after compilation are only read;
  191. * however, I need to check whether regexec is reentrant ...
  192. */
  193. struct rewrite_subst *lr_subst;
  194. #define REWRITE_REGEX_ICASE REG_ICASE
  195. #define REWRITE_REGEX_EXTENDED REG_EXTENDED
  196. int lr_flags;
  197. #define REWRITE_RECURSE 0x0001
  198. #define REWRITE_EXEC_ONCE 0x0002
  199. int lr_mode;
  200. int lr_max_passes;
  201. struct rewrite_action *lr_action;
  202. };
  203. /*
  204. * Rewrite Context (set of rules)
  205. */
  206. struct rewrite_context {
  207. char *lc_name;
  208. struct rewrite_context *lc_alias;
  209. struct rewrite_rule *lc_rule;
  210. };
  211. /*
  212. * Session
  213. */
  214. struct rewrite_session {
  215. void *ls_cookie;
  216. Avlnode *ls_vars;
  217. #ifdef USE_REWRITE_LDAP_PVT_THREADS
  218. ldap_pvt_thread_rdwr_t ls_vars_mutex;
  219. ldap_pvt_thread_mutex_t ls_mutex;
  220. #endif /* USE_REWRITE_LDAP_PVT_THREADS */
  221. int ls_count;
  222. };
  223. /*
  224. * Variable
  225. */
  226. struct rewrite_var {
  227. char *lv_name;
  228. int lv_flags;
  229. struct berval lv_value;
  230. };
  231. /*
  232. * Operation
  233. */
  234. struct rewrite_op {
  235. int lo_num_passes;
  236. int lo_depth;
  237. #if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
  238. char *lo_string;
  239. #endif
  240. char *lo_result;
  241. Avlnode *lo_vars;
  242. const void *lo_cookie;
  243. };
  244. /**********
  245. * PUBLIC *
  246. **********/
  247. /*
  248. * Rewrite info
  249. */
  250. struct rewrite_info {
  251. Avlnode *li_context;
  252. Avlnode *li_maps;
  253. /*
  254. * No global mutex because maps are read only at
  255. * config time
  256. */
  257. Avlnode *li_params;
  258. Avlnode *li_cookies;
  259. int li_num_cookies;
  260. #ifdef USE_REWRITE_LDAP_PVT_THREADS
  261. ldap_pvt_thread_rdwr_t li_params_mutex;
  262. ldap_pvt_thread_rdwr_t li_cookies_mutex;
  263. #endif /* USE_REWRITE_LDAP_PVT_THREADS */
  264. /*
  265. * Default to `off';
  266. * use `rewriteEngine {on|off}' directive to alter
  267. */
  268. int li_state;
  269. /*
  270. * Defaults to REWRITE_MAXPASSES;
  271. * use `rewriteMaxPasses numPasses' directive to alter
  272. */
  273. #define REWRITE_MAXPASSES 100
  274. int li_max_passes;
  275. int li_max_passes_per_rule;
  276. /*
  277. * Behavior in case a NULL or non-existent context is required
  278. */
  279. int li_rewrite_mode;
  280. };
  281. /***********
  282. * PRIVATE *
  283. ***********/
  284. LDAP_REWRITE_V (struct rewrite_context*) rewrite_int_curr_context;
  285. /*
  286. * Maps
  287. */
  288. /*
  289. * Parses a map (also in legacy 'x' version)
  290. */
  291. LDAP_REWRITE_F (struct rewrite_map *)
  292. rewrite_map_parse(
  293. struct rewrite_info *info,
  294. const char *s,
  295. const char **end
  296. );
  297. LDAP_REWRITE_F (struct rewrite_map *)
  298. rewrite_xmap_parse(
  299. struct rewrite_info *info,
  300. const char *s,
  301. const char **end
  302. );
  303. /*
  304. * Resolves key in val by means of map (also in legacy 'x' version)
  305. */
  306. LDAP_REWRITE_F (int)
  307. rewrite_map_apply(
  308. struct rewrite_info *info,
  309. struct rewrite_op *op,
  310. struct rewrite_map *map,
  311. struct berval *key,
  312. struct berval *val
  313. );
  314. LDAP_REWRITE_F (int)
  315. rewrite_xmap_apply(
  316. struct rewrite_info *info,
  317. struct rewrite_op *op,
  318. struct rewrite_map *map,
  319. struct berval *key,
  320. struct berval *val
  321. );
  322. LDAP_REWRITE_F (int)
  323. rewrite_map_destroy(
  324. struct rewrite_map **map
  325. );
  326. LDAP_REWRITE_F (int)
  327. rewrite_xmap_destroy(
  328. struct rewrite_map **map
  329. );
  330. LDAP_REWRITE_F (void)
  331. rewrite_builtin_map_free(
  332. void *map
  333. );
  334. /*
  335. * Submatch substitution
  336. */
  337. /*
  338. * Compiles a substitution pattern
  339. */
  340. LDAP_REWRITE_F (struct rewrite_subst *)
  341. rewrite_subst_compile(
  342. struct rewrite_info *info,
  343. const char *result
  344. );
  345. /*
  346. * Substitutes a portion of rewritten string according to substitution
  347. * pattern using submatches
  348. */
  349. LDAP_REWRITE_F (int)
  350. rewrite_subst_apply(
  351. struct rewrite_info *info,
  352. struct rewrite_op *op,
  353. struct rewrite_subst *subst,
  354. const char *string,
  355. const regmatch_t *match,
  356. struct berval *val
  357. );
  358. LDAP_REWRITE_F (int)
  359. rewrite_subst_destroy(
  360. struct rewrite_subst **subst
  361. );
  362. /*
  363. * Rules
  364. */
  365. /*
  366. * Compiles the rule and appends it at the running context
  367. */
  368. LDAP_REWRITE_F (int)
  369. rewrite_rule_compile(
  370. struct rewrite_info *info,
  371. struct rewrite_context *context,
  372. const char *pattern,
  373. const char *result,
  374. const char *flagstring
  375. );
  376. /*
  377. * Rewrites string according to rule; may return:
  378. * REWRITE_REGEXEC_OK: fine; if *result != NULL rule matched
  379. * and rewrite succeeded.
  380. * REWRITE_REGEXEC_STOP: fine, rule matched; stop processing
  381. * following rules
  382. * REWRITE_REGEXEC_UNWILL: rule matched; force 'unwilling to perform'
  383. * REWRITE_REGEXEC_ERR: an error occurred
  384. */
  385. LDAP_REWRITE_F (int)
  386. rewrite_rule_apply(
  387. struct rewrite_info *info,
  388. struct rewrite_op *op,
  389. struct rewrite_rule *rule,
  390. const char *string,
  391. char **result
  392. );
  393. LDAP_REWRITE_F (int)
  394. rewrite_rule_destroy(
  395. struct rewrite_rule **rule
  396. );
  397. /*
  398. * Sessions
  399. */
  400. /*
  401. * Fetches a struct rewrite_session
  402. */
  403. LDAP_REWRITE_F (struct rewrite_session *)
  404. rewrite_session_find(
  405. struct rewrite_info *info,
  406. const void *cookie
  407. );
  408. /*
  409. * Defines and inits a variable with session scope
  410. */
  411. LDAP_REWRITE_F (int)
  412. rewrite_session_var_set_f(
  413. struct rewrite_info *info,
  414. const void *cookie,
  415. const char *name,
  416. const char *value,
  417. int flags
  418. );
  419. /*
  420. * Gets a var with session scope
  421. */
  422. LDAP_REWRITE_F (int)
  423. rewrite_session_var_get(
  424. struct rewrite_info *info,
  425. const void *cookie,
  426. const char *name,
  427. struct berval *val
  428. );
  429. /*
  430. * Deletes a session
  431. */
  432. LDAP_REWRITE_F (int)
  433. rewrite_session_delete(
  434. struct rewrite_info *info,
  435. const void *cookie
  436. );
  437. /*
  438. * Destroys the cookie tree
  439. */
  440. LDAP_REWRITE_F (int)
  441. rewrite_session_destroy(
  442. struct rewrite_info *info
  443. );
  444. /*
  445. * Vars
  446. */
  447. /*
  448. * Finds a var
  449. */
  450. LDAP_REWRITE_F (struct rewrite_var *)
  451. rewrite_var_find(
  452. Avlnode *tree,
  453. const char *name
  454. );
  455. /*
  456. * Replaces the value of a variable
  457. */
  458. LDAP_REWRITE_F (int)
  459. rewrite_var_replace(
  460. struct rewrite_var *var,
  461. const char *value,
  462. int flags
  463. );
  464. /*
  465. * Inserts a newly created var
  466. */
  467. LDAP_REWRITE_F (struct rewrite_var *)
  468. rewrite_var_insert_f(
  469. Avlnode **tree,
  470. const char *name,
  471. const char *value,
  472. int flags
  473. );
  474. #define rewrite_var_insert(tree, name, value) \
  475. rewrite_var_insert_f((tree), (name), (value), \
  476. REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE)
  477. /*
  478. * Sets/inserts a var
  479. */
  480. LDAP_REWRITE_F (struct rewrite_var *)
  481. rewrite_var_set_f(
  482. Avlnode **tree,
  483. const char *name,
  484. const char *value,
  485. int flags
  486. );
  487. #define rewrite_var_set(tree, name, value, insert) \
  488. rewrite_var_set_f((tree), (name), (value), \
  489. REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE|((insert)? REWRITE_VAR_INSERT : 0))
  490. /*
  491. * Deletes a var tree
  492. */
  493. LDAP_REWRITE_F (int)
  494. rewrite_var_delete(
  495. Avlnode *tree
  496. );
  497. /*
  498. * Contexts
  499. */
  500. /*
  501. * Finds the context named rewriteContext in the context tree
  502. */
  503. LDAP_REWRITE_F (struct rewrite_context *)
  504. rewrite_context_find(
  505. struct rewrite_info *info,
  506. const char *rewriteContext
  507. );
  508. /*
  509. * Creates a new context called rewriteContext and stores in into the tree
  510. */
  511. LDAP_REWRITE_F (struct rewrite_context *)
  512. rewrite_context_create(
  513. struct rewrite_info *info,
  514. const char *rewriteContext
  515. );
  516. /*
  517. * Rewrites string according to context; may return:
  518. * OK: fine; if *result != NULL rule matched and rewrite succeeded.
  519. * STOP: fine, rule matched; stop processing following rules
  520. * UNWILL: rule matched; force 'unwilling to perform'
  521. */
  522. LDAP_REWRITE_F (int)
  523. rewrite_context_apply(
  524. struct rewrite_info *info,
  525. struct rewrite_op *op,
  526. struct rewrite_context *context,
  527. const char *string,
  528. char **result
  529. );
  530. LDAP_REWRITE_F (int)
  531. rewrite_context_destroy(
  532. struct rewrite_context **context
  533. );
  534. LDAP_REWRITE_F (void)
  535. rewrite_context_free(
  536. void *tmp
  537. );
  538. #endif /* REWRITE_INT_H */