1 /*	$NetBSD: rewrite.h,v 1.3 2021/08/14 16:14:55 christos Exp $	*/
2 
3 /* $OpenLDAP$
4  */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 2000-2021 The OpenLDAP Foundation.
8  * Portions Copyright 2000-2003 Pierangelo Masarati.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* ACKNOWLEDGEMENT:
20  * This work was initially developed by Pierangelo Masarati for
21  * inclusion in OpenLDAP Software.
22  */
23 
24 #ifndef REWRITE_H
25 #define REWRITE_H
26 
27 /*
28  * Default rewrite context
29  */
30 #define REWRITE_DEFAULT_CONTEXT		"default"
31 
32 /*
33  * Rewrite engine states
34  */
35 #define REWRITE_OFF			0x0000
36 #define REWRITE_ON			0x0001
37 #define REWRITE_DEFAULT			REWRITE_OFF
38 
39 /*
40  * Rewrite internal status returns
41  */
42 #define REWRITE_SUCCESS			LDAP_SUCCESS
43 #define REWRITE_ERR			LDAP_OTHER
44 
45 /*
46  * Rewrite modes (input values for rewrite_info_init); determine the
47  * behavior in case a null or non existent context is required:
48  *
49  * 	REWRITE_MODE_ERR		error
50  * 	REWRITE_MODE_OK			no error but no rewrite
51  * 	REWRITE_MODE_COPY_INPUT		a copy of the input is returned
52  * 	REWRITE_MODE_USE_DEFAULT	the default context is used.
53  */
54 #define REWRITE_MODE_ERR		0x0010
55 #define REWRITE_MODE_OK			0x0011
56 #define REWRITE_MODE_COPY_INPUT		0x0012
57 #define REWRITE_MODE_USE_DEFAULT	0x0013
58 
59 /*
60  * Rewrite status returns
61  *
62  * 	REWRITE_REGEXEC_OK		success (result may be empty in case
63  * 					of no match)
64  * 	REWRITE_REGEXEC_ERR		error (internal error,
65  * 					misconfiguration, map not working ...)
66  * 	REWRITE_REGEXEC_STOP		internal use; never returned
67  * 	REWRITE_REGEXEC_UNWILLING	the server should issue an 'unwilling
68  * 					to perform' error
69  */
70 #define REWRITE_REGEXEC_OK              (0)
71 #define REWRITE_REGEXEC_ERR             (-1)
72 #define REWRITE_REGEXEC_STOP            (-2)
73 #define REWRITE_REGEXEC_UNWILLING       (-3)
74 #define REWRITE_REGEXEC_USER		(1)	/* and above: LDAP errors */
75 
76 /*
77  * Rewrite variable flags
78  *	REWRITE_VAR_INSERT		insert mode (default) when adding
79  *					a variable; if not set during value
80  *					update, the variable is not inserted
81  *					if not present
82  *	REWRITE_VAR_UPDATE		update mode (default) when updating
83  *					a variable; if not set during insert,
84  *					the value is not updated if the
85  *					variable already exists
86  *	REWRITE_VAR_COPY_NAME		copy the variable name; if not set,
87  *					the name is not copied; be sure the
88  *					referenced string is available for
89  *					the entire life scope of the variable.
90  *	REWRITE_VAR_COPY_VALUE		copy the variable value; if not set,
91  *					the value is not copied; be sure the
92  *					referenced string is available for
93  *					the entire life scope of the variable.
94  */
95 #define REWRITE_VAR_NONE		0x0000
96 #define REWRITE_VAR_INSERT		0x0001
97 #define REWRITE_VAR_UPDATE		0x0002
98 #define REWRITE_VAR_COPY_NAME		0x0004
99 #define REWRITE_VAR_COPY_VALUE		0x0008
100 
101 /*
102  * Rewrite info
103  */
104 struct rewrite_info;
105 
106 struct berval; /* avoid include */
107 
108 LDAP_BEGIN_DECL
109 
110 /*
111  * Inits the info
112  */
113 LDAP_REWRITE_F (struct rewrite_info *)
114 rewrite_info_init(
115 		int mode
116 );
117 
118 /*
119  * Cleans up the info structure
120  */
121 LDAP_REWRITE_F (int)
122 rewrite_info_delete(
123                 struct rewrite_info **info
124 );
125 
126 
127 /*
128  * Parses a config line and takes actions to fit content in rewrite structure;
129  * lines handled are of the form:
130  *
131  *      rewriteEngine 		{on|off}
132  *      rewriteMaxPasses	numPasses
133  *      rewriteContext 		contextName [alias aliasedRewriteContex]
134  *      rewriteRule 		pattern substPattern [ruleFlags]
135  *      rewriteMap 		mapType mapName [mapArgs]
136  *      rewriteParam		paramName paramValue
137  */
138 LDAP_REWRITE_F (int)
139 rewrite_parse(
140 		struct rewrite_info *info,
141                 const char *fname,
142                 int lineno,
143                 int argc,
144                 char **argv
145 );
146 
147 /*
148  * process a config file that was already opened. Uses rewrite_parse.
149  */
150 LDAP_REWRITE_F (int)
151 rewrite_read(
152 		FILE *fin,
153 		struct rewrite_info *info
154 );
155 
156 /*
157  * Rewrites a string according to context.
158  * If the engine is off, OK is returned, but the return string will be NULL.
159  * In case of 'unwilling to perform', UNWILLING is returned, and the
160  * return string will also be null. The same in case of error.
161  * Otherwise, OK is returned, and result will hold a newly allocated string
162  * with the rewriting.
163  *
164  * What to do in case of non-existing rewrite context is still an issue.
165  * Four possibilities:
166  *      - error,
167  *      - ok with NULL result,
168  *      - ok with copy of string as result,
169  *      - use the default rewrite context.
170  */
171 LDAP_REWRITE_F (int)
172 rewrite(
173 		struct rewrite_info *info,
174 		const char *rewriteContext,
175 		const char *string,
176 		char **result
177 );
178 
179 /*
180  * Same as above; the cookie relates the rewrite to a session
181  */
182 LDAP_REWRITE_F (int)
183 rewrite_session(
184 		struct rewrite_info *info,
185 		const char *rewriteContext,
186 		const char *string,
187 		const void *cookie,
188 		char **result
189 );
190 
191 /*
192  * Inits a session
193  */
194 LDAP_REWRITE_F (struct rewrite_session *)
195 rewrite_session_init(
196                 struct rewrite_info *info,
197                 const void *cookie
198 );
199 
200 /*
201  * Defines and inits a variable with session scope
202  */
203 LDAP_REWRITE_F (int)
204 rewrite_session_var_set_f(
205 		struct rewrite_info *info,
206 		const void *cookie,
207 		const char *name,
208 		const char *value,
209 		int flags
210 );
211 
212 #define rewrite_session_var_set(info, cookie, name, value) \
213 	rewrite_session_var_set_f((info), (cookie), (name), (value), \
214 			REWRITE_VAR_INSERT|REWRITE_VAR_UPDATE|REWRITE_VAR_COPY_NAME|REWRITE_VAR_COPY_VALUE)
215 
216 /*
217  * Deletes a session
218  */
219 LDAP_REWRITE_F (int)
220 rewrite_session_delete(
221 		struct rewrite_info *info,
222 		const void *cookie
223 );
224 
225 
226 /*
227  * Params
228  */
229 
230 /*
231  * Defines and inits a variable with global scope
232  */
233 LDAP_REWRITE_F (int)
234 rewrite_param_set(
235                 struct rewrite_info *info,
236                 const char *name,
237                 const char *value
238 );
239 
240 /*
241  * Gets a var with global scope
242  */
243 LDAP_REWRITE_F (int)
244 rewrite_param_get(
245                 struct rewrite_info *info,
246                 const char *name,
247                 struct berval *value
248 );
249 
250 /*
251  * Destroys the parameter tree
252  */
253 LDAP_REWRITE_F (int)
254 rewrite_param_destroy(
255                 struct rewrite_info *info
256 );
257 
258 /*
259  * Mapping implementations
260  */
261 
262 struct rewrite_mapper;
263 
264 typedef void * (rewrite_mapper_config)(
265 	const char *fname,
266 	int lineno,
267 	int argc,
268 	char **argv );
269 
270 typedef int (rewrite_mapper_apply)(
271 	void *ctx,
272 	const char *arg,
273 	struct berval *retval );
274 
275 typedef int (rewrite_mapper_destroy)(
276 	void *ctx );
277 
278 typedef struct rewrite_mapper {
279 	char *rm_name;
280 	rewrite_mapper_config *rm_config;
281 	rewrite_mapper_apply *rm_apply;
282 	rewrite_mapper_destroy *rm_destroy;
283 } rewrite_mapper;
284 
285 /* For dynamic loading and unloading of mappers */
286 LDAP_REWRITE_F (int)
287 rewrite_mapper_register(
288 	const rewrite_mapper *map );
289 
290 LDAP_REWRITE_F (int)
291 rewrite_mapper_unregister(
292 	const rewrite_mapper *map );
293 
294 LDAP_REWRITE_F (const rewrite_mapper *)
295 rewrite_mapper_find(
296 	const char *name );
297 
298 LDAP_END_DECL
299 
300 #endif /* REWRITE_H */
301