summaryrefslogtreecommitdiffstats
path: root/libraries/librewrite/rewrite-int.h
blob: 4c7bf56704c7e85e032502d7f9122ad5072ee149 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 2000-2021 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENT:
 * This work was initially developed by Pierangelo Masarati for
 * inclusion in OpenLDAP Software.
 */

#ifndef REWRITE_INT_H
#define REWRITE_INT_H

/*
 * These are required by every file of the library, so they're included here
 */
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/syslog.h>
#include <ac/regex.h>
#include <ac/socket.h>
#include <ac/unistd.h>
#include <ac/ctype.h>

#include <lber.h>
#include <ldap.h>
#define LDAP_DEFINE_LDAP_DEBUG
#include <ldap_log.h>
#include <lutil.h>
#include <avl.h>

#include <rewrite.h>

#define malloc(x)	ber_memalloc(x)
#define calloc(x,y)	ber_memcalloc(x,y)
#define realloc(x,y)	ber_memrealloc(x,y)
#define free(x)	ber_memfree(x)
#undef strdup
#define	strdup(x)	ber_strdup(x)

/* Uncomment to use ldap pvt threads */
#define USE_REWRITE_LDAP_PVT_THREADS
#include <ldap_pvt_thread.h>

/*
 * For details, see RATIONALE.
 */

#define REWRITE_MAX_MATCH	11	/* 0: overall string; 1-9: submatches */
#define REWRITE_MAX_PASSES	100

/*
 * Submatch escape char
 */
/* the '\' conflicts with slapd.conf parsing */
/* #define REWRITE_SUBMATCH_ESCAPE			'\\' */
#define REWRITE_SUBMATCH_ESCAPE_ORIG		'%'
#define REWRITE_SUBMATCH_ESCAPE			'$'
#define IS_REWRITE_SUBMATCH_ESCAPE(c) \
	((c) == REWRITE_SUBMATCH_ESCAPE || (c) == REWRITE_SUBMATCH_ESCAPE_ORIG)

/*
 * REGEX flags
 */

#define REWRITE_FLAG_HONORCASE			'C'
#define REWRITE_FLAG_BASICREGEX			'R'

/*
 * Action flags
 */
#define REWRITE_FLAG_EXECONCE			':'
#define REWRITE_FLAG_STOP			'@'
#define REWRITE_FLAG_UNWILLING			'#'
#define REWRITE_FLAG_GOTO			'G'	/* requires an arg */
#define REWRITE_FLAG_USER			'U'	/* requires an arg */
#define REWRITE_FLAG_MAX_PASSES			'M'	/* requires an arg */
#define REWRITE_FLAG_IGNORE_ERR			'I'

/*
 * Map operators
 */
#define REWRITE_OPERATOR_SUBCONTEXT		'>'
#define REWRITE_OPERATOR_COMMAND		'|'
#define REWRITE_OPERATOR_VARIABLE_SET		'&'
#define REWRITE_OPERATOR_VARIABLE_GET		'*'
#define REWRITE_OPERATOR_PARAM_GET		'$'


/***********
 * PRIVATE *
 ***********/

/*
 * Action
 */
struct rewrite_action {
	struct rewrite_action          *la_next;
	
#define REWRITE_ACTION_STOP		0x0001
#define REWRITE_ACTION_UNWILLING	0x0002
#define REWRITE_ACTION_GOTO		0x0003
#define REWRITE_ACTION_IGNORE_ERR	0x0004
#define REWRITE_ACTION_USER		0x0005
	int                             la_type;
	void                           *la_args;
};

/*
 * Map
 */
struct rewrite_map {

	/*
	 * Legacy stuff
	 */
#define REWRITE_MAP_XFILEMAP		0x0001	/* Rough implementation! */
#define REWRITE_MAP_XPWDMAP		0x0002  /* uid -> gecos */
#define REWRITE_MAP_XLDAPMAP		0x0003	/* Not implemented yet! */

	/*
	 * Maps with args
	 */
#define REWRITE_MAP_SUBCONTEXT		0x0101
	
#define REWRITE_MAP_SET_OP_VAR		0x0102
#define REWRITE_MAP_SETW_OP_VAR		0x0103
#define REWRITE_MAP_GET_OP_VAR		0x0104
#define	REWRITE_MAP_SET_SESN_VAR	0x0105
#define REWRITE_MAP_SETW_SESN_VAR	0x0106
#define	REWRITE_MAP_GET_SESN_VAR	0x0107
#define REWRITE_MAP_GET_PARAM		0x0108
#define REWRITE_MAP_BUILTIN		0x0109
	int                             lm_type;

	char                           *lm_name;
	void                           *lm_data;

	/*
	 * Old maps store private data in _lm_args;
	 * new maps store the substitution pattern in _lm_subst
	 */
	union {		
	        void                   *_lm_args;
		struct rewrite_subst   *_lm_subst;
	} lm_union;
#define	lm_args lm_union._lm_args
#define	lm_subst lm_union._lm_subst

#ifdef USE_REWRITE_LDAP_PVT_THREADS
	ldap_pvt_thread_mutex_t         lm_mutex;
#endif /* USE_REWRITE_LDAP_PVT_THREADS */
};

/*
 * Builtin maps
 */
struct rewrite_builtin_map {
#define REWRITE_BUILTIN_MAP	0x0200
	int                             lb_type;
	char                           *lb_name;
	void                           *lb_private;
	const rewrite_mapper		   *lb_mapper;

#ifdef USE_REWRITE_LDAP_PVT_THREADS
	ldap_pvt_thread_mutex_t         lb_mutex;
#endif /* USE_REWRITE_LDAP_PVT_THREADS */
};

/*
 * Submatch substitution
 */
struct rewrite_submatch {
#define REWRITE_SUBMATCH_ASIS		0x0000
#define REWRITE_SUBMATCH_XMAP		0x0001
#define REWRITE_SUBMATCH_MAP_W_ARG	0x0002
	int                             ls_type;
	struct rewrite_map             *ls_map;
	int                             ls_submatch;
	/*
	 * The first one represents the index of the submatch in case
	 * the map has single submatch as argument;
	 * the latter represents the map argument scheme in case
	 * the map has substitution string argument form
	 */
};

/*
 * Pattern substitution
 */
struct rewrite_subst {
	size_t                          lt_subs_len;
	struct berval                  *lt_subs;
	
	int                             lt_num_submatch;
	struct rewrite_submatch        *lt_submatch;
};

/*
 * Rule
 */
struct rewrite_rule {
	struct rewrite_rule            *lr_next;
	struct rewrite_rule            *lr_prev;

	char                           *lr_pattern;
	char                           *lr_subststring;
	char                           *lr_flagstring;
	regex_t				lr_regex;

	/*
	 * I was thinking about some kind of per-rule mutex, but there's
	 * probably no need, because rules after compilation are only read;
	 * however, I need to check whether regexec is reentrant ...
	 */

	struct rewrite_subst           *lr_subst;
	
#define REWRITE_REGEX_ICASE		REG_ICASE
#define REWRITE_REGEX_EXTENDED		REG_EXTENDED	
	int                             lr_flags;

#define REWRITE_RECURSE			0x0001
#define REWRITE_EXEC_ONCE          	0x0002
	int				lr_mode;
	int				lr_max_passes;

	struct rewrite_action          *lr_action;
};

/*
 * Rewrite Context (set of rules)
 */
struct rewrite_context {
	char                           *lc_name;
	struct rewrite_context         *lc_alias;
	struct rewrite_rule            *lc_rule;
};

/*
 * Session
 */
struct rewrite_session {
	void                           *ls_cookie;
	Avlnode                        *ls_vars;
#ifdef USE_REWRITE_LDAP_PVT_THREADS
	ldap_pvt_thread_rdwr_t          ls_vars_mutex;
	ldap_pvt_thread_mutex_t		ls_mutex;
#endif /* USE_REWRITE_LDAP_PVT_THREADS */
	int				ls_count;
};

/*
 * Variable
 */
struct rewrite_var {
	char                           *lv_name;
	int				lv_flags;
	struct berval                   lv_value;
};

/*
 * Operation
 */
struct rewrite_op {
	int                             lo_num_passes;
	int                             lo_depth;
#if 0 /* FIXME: not used anywhere! (debug? then, why strdup?) */
	char                           *lo_string;
#endif
	char                           *lo_result;
	Avlnode                        *lo_vars;
	const void                     *lo_cookie;
};


/**********
 * PUBLIC *
 **********/

/*
 * Rewrite info
 */
struct rewrite_info {
	Avlnode                        *li_context;
	Avlnode                        *li_maps;
	/*
	 * No global mutex because maps are read only at 
	 * config time
	 */
	Avlnode                        *li_params;
	Avlnode                        *li_cookies;
	int                             li_num_cookies;

#ifdef USE_REWRITE_LDAP_PVT_THREADS
	ldap_pvt_thread_rdwr_t          li_params_mutex;
        ldap_pvt_thread_rdwr_t          li_cookies_mutex;
#endif /* USE_REWRITE_LDAP_PVT_THREADS */

	/*
	 * Default to `off';
	 * use `rewriteEngine {on|off}' directive to alter
	 */
	int				li_state;

	/*
	 * Defaults to REWRITE_MAXPASSES;
	 * use `rewriteMaxPasses numPasses' directive to alter
	 */
#define REWRITE_MAXPASSES		100
	int                             li_max_passes;
	int                             li_max_passes_per_rule;

	/*
	 * Behavior in case a NULL or non-existent context is required
	 */
	int                             li_rewrite_mode;
};

/***********
 * PRIVATE *
 ***********/

LDAP_REWRITE_V (struct rewrite_context*) rewrite_int_curr_context;

/*
 * Maps
 */

/*
 * Parses a map (also in legacy 'x' version)
 */
LDAP_REWRITE_F (struct rewrite_map *)
rewrite_map_parse(
		struct rewrite_info *info,
		const char *s,
		const char **end
);

LDAP_REWRITE_F (struct rewrite_map *)
rewrite_xmap_parse(
		struct rewrite_info *info,
		const char *s,
		const char **end
);

/*
 * Resolves key in val by means of map (also in legacy 'x' version)
 */
LDAP_REWRITE_F (int)
rewrite_map_apply(
		struct rewrite_info *info,
		struct rewrite_op *op,
		struct rewrite_map *map,
		struct berval *key,
		struct berval *val
);

LDAP_REWRITE_F (int)
rewrite_xmap_apply(
		struct rewrite_info *info,
		struct rewrite_op *op,
		struct rewrite_map *map,
		struct berval *key,
		struct berval *val
);

LDAP_REWRITE_F (int)
rewrite_map_destroy(
		struct rewrite_map **map
);

LDAP_REWRITE_F (int)
rewrite_xmap_destroy(
		struct rewrite_map **map
);

LDAP_REWRITE_F (void)
rewrite_builtin_map_free(
		void *map
);
/*
 * Submatch substitution
 */

/*
 * Compiles a substitution pattern
 */
LDAP_REWRITE_F (struct rewrite_subst *)
rewrite_subst_compile(
		struct rewrite_info *info,
		const char *result
);

/*
 * Substitutes a portion of rewritten string according to substitution
 * pattern using submatches
 */
LDAP_REWRITE_F (int)
rewrite_subst_apply(
		struct rewrite_info *info,
		struct rewrite_op *op,
		struct rewrite_subst *subst,
		const char *string,
		const regmatch_t *match,
		struct berval *val
);

LDAP_REWRITE_F (int)
rewrite_subst_destroy(
		struct rewrite_subst **subst
);


/*
 * Rules
 */

/*
 * Compiles the rule and appends it at the running context
 */
LDAP_REWRITE_F (int)
rewrite_rule_compile(
		struct rewrite_info *info,
		struct rewrite_context *context,
		const char *pattern,
		const char *result,
		const char *flagstring
);

/*
 * Rewrites string according to rule; may return:
 *      REWRITE_REGEXEC_OK:	fine; if *result != NULL rule matched
 *      			and rewrite succeeded.
 *      REWRITE_REGEXEC_STOP:   fine, rule matched; stop processing
 *      			following rules
 *      REWRITE_REGEXEC_UNWILL: rule matched; force 'unwilling to perform'
 *      REWRITE_REGEXEC_ERR:	an error occurred
 */
LDAP_REWRITE_F (int)
rewrite_rule_apply(
		struct rewrite_info *info,
		struct rewrite_op *op,
		struct rewrite_rule *rule,
		const char *string,
		char **result
);

LDAP_REWRITE_F (int)
rewrite_rule_destroy(
		struct rewrite_rule **rule
);

/*
 * Sessions
 */

/*
 * Fetches a struct rewrite_session
 */
LDAP_REWRITE_F (struct rewrite_session *)
rewrite_session_find(
                struct rewrite_info *info,
                const void *cookie
);

/*
 * Defines and inits a variable with session scope
 */
LDAP_REWRITE_F (int)
rewrite_session_var_set_f(
                struct rewrite_info *info,
                const void *cookie,
                const char *name,
                const char *value,
		int flags
);

/*
 * Gets a var with session scope
 */
LDAP_REWRITE_F (int)
rewrite_session_var_get(
                struct rewrite_info *info,
                const void *cookie,
                const char *name,
                struct berval *val
);

/*
 * Deletes a session
 */
LDAP_REWRITE_F (int)
rewrite_session_delete(
                struct rewrite_info *info,
                const void *cookie
);

/*
 * Destroys the cookie tree
 */
LDAP_REWRITE_F (int)
rewrite_session_destroy(
                struct rewrite_info *info
);


/*
 * Vars
 */

/*
 * Finds a var
 */
LDAP_REWRITE_F (struct rewrite_var *)
rewrite_var_find(
                Avlnode *tree,
                const char *name
);

/*
 * Replaces the value of a variable
 */
LDAP_REWRITE_F (int)
rewrite_var_replace(
		struct rewrite_var *var,
		const char *value,
		int flags
);

/*
 * Inserts a newly created var
 */
LDAP_REWRITE_F (struct rewrite_var *)
rewrite_var_insert_f(
                Avlnode **tree,
                const char *name,
                const char *value,
		int flags
);

#define rewrite_var_insert(tree, name, value) \
	rewrite_var_insert_f((tree), (name), (value), \
			REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE)

/*
 * Sets/inserts a var
 */
LDAP_REWRITE_F (struct rewrite_var *)
rewrite_var_set_f(
                Avlnode **tree,
                const char *name,
                const char *value,
                int flags
);

#define rewrite_var_set(tree, name, value, insert) \
	rewrite_var_set_f((tree), (name), (value), \
			REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE|((insert)? REWRITE_VAR_INSERT : 0))

/*
 * Deletes a var tree
 */
LDAP_REWRITE_F (int)
rewrite_var_delete(
                Avlnode *tree
);


/*
 * Contexts
 */

/*
 * Finds the context named rewriteContext in the context tree
 */
LDAP_REWRITE_F (struct rewrite_context *)
rewrite_context_find(
		struct rewrite_info *info,
		const char *rewriteContext
);

/*
 * Creates a new context called rewriteContext and stores in into the tree
 */
LDAP_REWRITE_F (struct rewrite_context *)
rewrite_context_create(
		struct rewrite_info *info,
		const char *rewriteContext
);

/*
 * Rewrites string according to context; may return:
 *      OK:     fine; if *result != NULL rule matched and rewrite succeeded.
 *      STOP:   fine, rule matched; stop processing following rules
 *      UNWILL: rule matched; force 'unwilling to perform'
 */
LDAP_REWRITE_F (int)
rewrite_context_apply(
		struct rewrite_info *info,
		struct rewrite_op *op,
		struct rewrite_context *context,
		const char *string,
		char **result
);

LDAP_REWRITE_F (int)
rewrite_context_destroy(
		struct rewrite_context **context
);

LDAP_REWRITE_F (void)
rewrite_context_free(
		void *tmp
);

#endif /* REWRITE_INT_H */