ldb: Add read_lock and read_unlock to ldb_module_ops
[Samba.git] / lib / ldb / include / ldb_module.h
blob0f5feeb084cfd6283b042a3be69eaa8abc6a5ff6
1 /*
2 ldb database library
4 Copyright (C) Simo Sorce 2008
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 * Name: ldb
27 * Component: ldb module header
29 * Description: defines ldb modules structures and helpers
33 #ifndef _LDB_MODULE_H_
34 #define _LDB_MODULE_H_
36 #include <ldb.h>
38 struct ldb_context;
39 struct ldb_module;
41 /**
42 internal flag bits on message elements. Must be within LDB_FLAG_INTERNAL_MASK
44 #define LDB_FLAG_INTERNAL_DISABLE_VALIDATION 0x10
46 /* disable any single value checking on this attribute */
47 #define LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK 0x20
49 /* attribute has failed access check and must not be exposed */
50 #define LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE 0x40
52 /* force single value checking on this attribute */
53 #define LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK 0x80
55 /* an extended match rule that always fails to match */
56 #define SAMBA_LDAP_MATCH_ALWAYS_FALSE "1.3.6.1.4.1.7165.4.5.1"
58 /* The const char * const * pointer to a list of secret (password)
59 * attributes, not to be printed in trace messages */
60 #define LDB_SECRET_ATTRIBUTE_LIST_OPAQUE "LDB_SECRET_ATTRIBUTE_LIST"
63 these function pointers define the operations that a ldb module can intercept
65 struct ldb_module_ops {
66 const char *name;
67 int (*init_context) (struct ldb_module *);
68 int (*search)(struct ldb_module *, struct ldb_request *); /* search */
69 int (*add)(struct ldb_module *, struct ldb_request *); /* add */
70 int (*modify)(struct ldb_module *, struct ldb_request *); /* modify */
71 int (*del)(struct ldb_module *, struct ldb_request *); /* delete */
72 int (*rename)(struct ldb_module *, struct ldb_request *); /* rename */
73 int (*request)(struct ldb_module *, struct ldb_request *); /* match any other operation */
74 int (*extended)(struct ldb_module *, struct ldb_request *); /* extended operations */
75 int (*start_transaction)(struct ldb_module *);
76 int (*prepare_commit)(struct ldb_module *);
77 int (*end_transaction)(struct ldb_module *);
78 int (*del_transaction)(struct ldb_module *);
79 int (*sequence_number)(struct ldb_module *, struct ldb_request *);
80 int (*read_lock)(struct ldb_module *);
81 int (*read_unlock)(struct ldb_module *);
82 void *private_data;
86 /* The following definitions come from lib/ldb/common/ldb_debug.c */
87 void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
88 void ldb_debug_set(struct ldb_context *ldb, enum ldb_debug_level level,
89 const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
90 void ldb_debug_add(struct ldb_context *ldb, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
91 void ldb_debug_end(struct ldb_context *ldb, enum ldb_debug_level level);
92 void ldb_vdebug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
94 #define ldb_error(ldb, ecode, reason) ldb_error_at(ldb, ecode, reason, __FILE__, __LINE__)
95 #define ldb_module_error(module, ecode, reason) ldb_error_at(ldb_module_get_ctx(module), ecode, reason, __FILE__, __LINE__)
97 #define ldb_oom(ldb) ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "ldb out of memory")
98 #define ldb_module_oom(module) ldb_oom(ldb_module_get_ctx(module))
99 #define ldb_operr(ldb) ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "operations error")
100 #define ldb_module_operr(module) ldb_error(ldb_module_get_ctx(module), LDB_ERR_OPERATIONS_ERROR, "operations error")
102 /* The following definitions come from lib/ldb/common/ldb.c */
104 void ldb_request_set_state(struct ldb_request *req, int state);
105 int ldb_request_get_status(struct ldb_request *req);
107 unsigned int ldb_get_create_perms(struct ldb_context *ldb);
109 const struct ldb_schema_syntax *ldb_standard_syntax_by_name(struct ldb_context *ldb,
110 const char *syntax);
112 /* The following definitions come from lib/ldb/common/ldb_attributes.c */
114 int ldb_schema_attribute_add_with_syntax(struct ldb_context *ldb,
115 const char *name,
116 unsigned flags,
117 const struct ldb_schema_syntax *syntax);
118 int ldb_schema_attribute_add(struct ldb_context *ldb,
119 const char *name,
120 unsigned flags,
121 const char *syntax);
122 void ldb_schema_attribute_remove(struct ldb_context *ldb, const char *name);
124 /* we allow external code to override the name -> schema_attribute function */
125 typedef const struct ldb_schema_attribute *(*ldb_attribute_handler_override_fn_t)(struct ldb_context *, void *, const char *);
128 Allow the caller to define a callback for the attribute handler
130 \param ldb The ldb context
131 \param override The callback to be used for attribute lookups
132 \param private_data Private data for the callback
135 void ldb_schema_attribute_set_override_handler(struct ldb_context *ldb,
136 ldb_attribute_handler_override_fn_t override,
137 void *private_data);
140 Allow the caller to define that the callback for the attribute handler
141 also overrides the index list
143 \param ldb The ldb context
144 \param one_level_indexes Indicates that the index for SCOPE_ONELEVEL
145 should also be maintained
148 void ldb_schema_set_override_indexlist(struct ldb_context *ldb,
149 bool one_level_indexes);
151 /* A useful function to build comparison functions with */
152 int ldb_any_comparison(struct ldb_context *ldb, void *mem_ctx,
153 ldb_attr_handler_t canonicalise_fn,
154 const struct ldb_val *v1,
155 const struct ldb_val *v2);
157 /* The following definitions come from lib/ldb/common/ldb_controls.c */
158 int ldb_save_controls(struct ldb_control *exclude, struct ldb_request *req, struct ldb_control ***saver);
159 /* Returns a list of controls, except the one specified. Included
160 * controls become a child of returned list if they were children of
161 * controls_in */
162 struct ldb_control **ldb_controls_except_specified(struct ldb_control **controls_in,
163 TALLOC_CTX *mem_ctx,
164 struct ldb_control *exclude);
165 int ldb_check_critical_controls(struct ldb_control **controls);
167 /* The following definitions come from lib/ldb/common/ldb_ldif.c */
168 int ldb_should_b64_encode(struct ldb_context *ldb, const struct ldb_val *val);
170 /* The following definitions come from lib/ldb/common/ldb_match.c */
171 int ldb_match_msg(struct ldb_context *ldb,
172 const struct ldb_message *msg,
173 const struct ldb_parse_tree *tree,
174 struct ldb_dn *base,
175 enum ldb_scope scope);
177 int ldb_match_msg_error(struct ldb_context *ldb,
178 const struct ldb_message *msg,
179 const struct ldb_parse_tree *tree,
180 struct ldb_dn *base,
181 enum ldb_scope scope,
182 bool *matched);
184 int ldb_match_msg_objectclass(const struct ldb_message *msg,
185 const char *objectclass);
187 int ldb_register_extended_match_rules(struct ldb_context *ldb);
189 /* The following definitions come from lib/ldb/common/ldb_modules.c */
191 struct ldb_module *ldb_module_new(TALLOC_CTX *memctx,
192 struct ldb_context *ldb,
193 const char *module_name,
194 const struct ldb_module_ops *ops);
196 const char * ldb_module_get_name(struct ldb_module *module);
197 struct ldb_context *ldb_module_get_ctx(struct ldb_module *module);
198 void *ldb_module_get_private(struct ldb_module *module);
199 void ldb_module_set_private(struct ldb_module *module, void *private_data);
200 const struct ldb_module_ops *ldb_module_get_ops(struct ldb_module *module);
202 int ldb_next_request(struct ldb_module *module, struct ldb_request *request);
203 int ldb_next_start_trans(struct ldb_module *module);
204 int ldb_next_end_trans(struct ldb_module *module);
205 int ldb_next_del_trans(struct ldb_module *module);
206 int ldb_next_prepare_commit(struct ldb_module *module);
207 int ldb_next_init(struct ldb_module *module);
208 int ldb_next_read_lock(struct ldb_module *module);
209 int ldb_next_read_unlock(struct ldb_module *module);
211 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string);
212 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
213 void ldb_reset_err_string(struct ldb_context *ldb);
214 int ldb_error_at(struct ldb_context *ldb, int ecode, const char *reason, const char *file, int line);
216 const char *ldb_default_modules_dir(void);
218 int ldb_register_module(const struct ldb_module_ops *);
220 typedef int (*ldb_connect_fn)(struct ldb_context *ldb, const char *url,
221 unsigned int flags, const char *options[],
222 struct ldb_module **module);
225 Require that LDB use a private event context for each request
227 A private event context may need to be created to avoid nested event
228 loops during ldb_tdb with the locks held. This indicates that a
229 backend is in use that requires this to hold locks safely.
231 \param handle The ldb handle to set the flag on
233 void ldb_set_require_private_event_context(struct ldb_context *ldb);
235 struct ldb_backend_ops {
236 const char *name;
237 ldb_connect_fn connect_fn;
240 const char *ldb_default_modules_dir(void);
242 int ldb_register_backend(const char *url_prefix, ldb_connect_fn, bool);
244 struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb);
247 Obtains the private event context for the handle,
249 A private event context may have been created to avoid nested event
250 loops during ldb_tdb with the locks held. Otherwise return the
251 global one.
253 \param handle The ldb handle to obtain the event context for
254 \return the tevent event context for this handle (private or global)
256 struct tevent_context *ldb_handle_get_event_context(struct ldb_handle *handle);
258 int ldb_module_send_entry(struct ldb_request *req,
259 struct ldb_message *msg,
260 struct ldb_control **ctrls);
262 int ldb_module_send_referral(struct ldb_request *req,
263 char *ref);
265 int ldb_module_done(struct ldb_request *req,
266 struct ldb_control **ctrls,
267 struct ldb_extended *response,
268 int error);
270 int ldb_mod_register_control(struct ldb_module *module, const char *oid);
272 void ldb_set_default_dns(struct ldb_context *ldb);
274 Add a ldb_control to a ldb_reply
276 \param ares the reply struct where to add the control
277 \param oid the object identifier of the control as string
278 \param critical whether the control should be critical or not
279 \param data a talloc pointer to the control specific data
281 \return result code (LDB_SUCCESS on success, or a failure code)
283 int ldb_reply_add_control(struct ldb_reply *ares, const char *oid, bool critical, void *data);
286 mark a request as untrusted.
288 This tells the rootdse module to remove unregistered controls
290 \param req the request to mark as untrusted
292 void ldb_req_mark_untrusted(struct ldb_request *req);
295 mark a request as trusted.
297 This tells the rootdse module to allow unregistered controls
299 \param req the request to mark as trusted
301 void ldb_req_mark_trusted(struct ldb_request *req);
304 return true is a request is untrusted
306 This indicates the request came across a trust boundary
307 for example over LDAP
309 \param req the request check
310 \return is req trusted
312 bool ldb_req_is_untrusted(struct ldb_request *req);
315 set custom flags. Those flags are set by applications using ldb,
316 they are application dependent and the same bit can have different
317 meaning in different application.
319 void ldb_req_set_custom_flags(struct ldb_request *req, uint32_t flags);
322 get custom flags. Those flags are set by applications using ldb,
323 they are application dependent and the same bit can have different
324 meaning in different application.
326 uint32_t ldb_req_get_custom_flags(struct ldb_request *req);
328 /* load all modules from the given directory */
329 int ldb_modules_load(const char *modules_path, const char *version);
331 /* init functions prototype */
332 typedef int (*ldb_module_init_fn)(const char *);
335 general ldb hook function
337 enum ldb_module_hook_type { LDB_MODULE_HOOK_CMDLINE_OPTIONS = 1,
338 LDB_MODULE_HOOK_CMDLINE_PRECONNECT = 2,
339 LDB_MODULE_HOOK_CMDLINE_POSTCONNECT = 3 };
341 typedef int (*ldb_hook_fn)(struct ldb_context *, enum ldb_module_hook_type );
344 register a ldb hook function
346 int ldb_register_hook(ldb_hook_fn hook_fn);
349 call ldb hooks of a given type
351 int ldb_modules_hook(struct ldb_context *ldb, enum ldb_module_hook_type t);
353 #define LDB_MODULE_CHECK_VERSION(version) do { \
354 if (strcmp(version, LDB_VERSION) != 0) { \
355 fprintf(stderr, "ldb: module version mismatch in %s : ldb_version=%s module_version=%s\n", \
356 __FILE__, version, LDB_VERSION); \
357 return LDB_ERR_UNAVAILABLE; \
358 }} while (0)
362 return a string representation of the calling chain for the given
363 ldb request
365 char *ldb_module_call_chain(struct ldb_request *req, TALLOC_CTX *mem_ctx);
368 return the next module in the chain
370 struct ldb_module *ldb_module_next(struct ldb_module *module);
373 set the next module in the module chain
375 void ldb_module_set_next(struct ldb_module *module, struct ldb_module *next);
378 load a list of modules
380 int ldb_module_load_list(struct ldb_context *ldb, const char **module_list,
381 struct ldb_module *backend, struct ldb_module **out);
384 get the popt_options pointer in the ldb structure. This allows a ldb
385 module to change the command line parsing
387 struct poptOption **ldb_module_popt_options(struct ldb_context *ldb);
389 /* modules are called in inverse order on the stack.
390 Lets place them as an admin would think the right order is.
391 Modules order is important */
392 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string);
395 return the current ldb flags LDB_FLG_*
397 uint32_t ldb_module_flags(struct ldb_context *ldb);
399 int ldb_module_connect_backend(struct ldb_context *ldb,
400 const char *url,
401 const char *options[],
402 struct ldb_module **backend_module);
405 initialise a chain of modules
407 int ldb_module_init_chain(struct ldb_context *ldb, struct ldb_module *module);
410 * prototype for the init function defined by dynamically loaded modules
412 int ldb_init_module(const char *version);
414 /* replace the components of a DN with those from another DN, without
415 * touching the extended components
417 * return true if successful and false if not
418 * if false is returned the dn may be marked invalid
420 bool ldb_dn_replace_components(struct ldb_dn *dn, struct ldb_dn *new_dn);
423 walk a parse tree, calling the provided callback on each node
425 int ldb_parse_tree_walk(struct ldb_parse_tree *tree,
426 int (*callback)(struct ldb_parse_tree *tree, void *),
427 void *private_context);
429 /* compare two message elements with ordering - used by modify */
430 bool ldb_msg_element_equal_ordered(const struct ldb_message_element *el1,
431 const struct ldb_message_element *el2);
434 struct ldb_extended_match_rule
436 const char *oid;
437 int (*callback)(struct ldb_context *, const char *oid,
438 const struct ldb_message *, const char *,
439 const struct ldb_val *, bool *);
442 int ldb_register_extended_match_rule(struct ldb_context *ldb,
443 const struct ldb_extended_match_rule *rule);
446 * these pack/unpack functions are exposed in the library for use by
447 * ldb tools like ldbdump and for use in tests,
448 * but are not part of the public API
450 int ldb_pack_data(struct ldb_context *ldb,
451 const struct ldb_message *message,
452 struct ldb_val *data);
454 * Unpack a ldb message from a linear buffer in ldb_val
456 * Providing a list of attributes to this function allows selective unpacking.
457 * Giving a NULL list (or a list_size of 0) unpacks all the attributes.
459 int ldb_unpack_data_only_attr_list(struct ldb_context *ldb,
460 const struct ldb_val *data,
461 struct ldb_message *message,
462 const char* const * list,
463 unsigned int list_size,
464 unsigned int *nb_attributes_indb);
465 int ldb_unpack_data(struct ldb_context *ldb,
466 const struct ldb_val *data,
467 struct ldb_message *message);
469 * Unpack a ldb message from a linear buffer in ldb_val
471 * Providing a list of attributes to this function allows selective unpacking.
472 * Giving a NULL list (or a list_size of 0) unpacks all the attributes.
474 * Flags allow control of allocation, so that if
475 * LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC is specified, then data in values are
476 * not allocated, instead they point into the supplier constant buffer.
478 * If LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC is specified, then values
479 * array are not allocated individually (for single-valued
480 * attributes), instead they point into a single buffer per message.
482 * LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC is only valid when
483 * LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC is also specified.
485 * Likewise if LDB_UNPACK_DATA_FLAG_NO_DN is specified, the DN is omitted.
487 int ldb_unpack_data_only_attr_list_flags(struct ldb_context *ldb,
488 const struct ldb_val *data,
489 struct ldb_message *message,
490 const char * const *list,
491 unsigned int list_size,
492 unsigned int flags,
493 unsigned int *nb_elements_in_db);
495 #define LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC 0x0001
496 #define LDB_UNPACK_DATA_FLAG_NO_DN 0x0002
497 #define LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC 0x0004
500 Forces a specific ldb handle to use the global event context.
502 This allows a nested event loop to operate, so any open
503 transaction also needs to be aborted.
505 Any events on this event context will be lost.
507 This is used in Samba when sending an IRPC to another part of the
508 same process instead of making a local DB modification.
510 \param handle The ldb handle to force to use the global context
513 void ldb_handle_use_global_event_context(struct ldb_handle *handle);
515 #endif