1 /*
2    ldb database library
3 
4    Copyright (C) Andrew Tridgell    2004
5    Copyright (C) Stefan Metzmacher  2004
6    Copyright (C) Simo Sorce         2004-2005
7 
8      ** NOTE! The following LGPL license applies to the ldb
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11 
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16 
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21 
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb private header
30  *
31  *  Description: defines internal ldb structures used by the subsystem and modules
32  *
33  *  Author: Andrew Tridgell
34  *  Author: Stefan Metzmacher
35  */
36 
37 #ifndef _LDB_PRIVATE_H_
38 #define _LDB_PRIVATE_H_ 1
39 
40 #include "replace.h"
41 #include "system/filesys.h"
42 #include "system/time.h"
43 #include "ldb.h"
44 #include "ldb_module.h"
45 
46 struct ldb_context;
47 
48 struct ldb_module_ops;
49 
50 struct ldb_backend_ops;
51 
52 #define LDB_HANDLE_FLAG_DONE_CALLED 1
53 /* call is from an untrusted source - eg. over ldap:// */
54 #define LDB_HANDLE_FLAG_UNTRUSTED   2
55 
56 struct ldb_handle {
57 	int status;
58 	enum ldb_state state;
59 	struct ldb_context *ldb;
60 	unsigned flags;
61 	/* flags dedicated to be set by application using ldb */
62 	uint32_t custom_flags;
63 	unsigned nesting;
64 
65 	/* Private event context (if not NULL) */
66 	struct tevent_context *event_context;
67 
68 	/* used for debugging */
69 	struct ldb_request *parent;
70 	const char *location;
71 };
72 
73 /* basic module structure */
74 struct ldb_module {
75 	struct ldb_module *prev, *next;
76 	struct ldb_context *ldb;
77 	void *private_data;
78 	const struct ldb_module_ops *ops;
79 };
80 
81 /*
82   schema related information needed for matching rules
83 */
84 struct ldb_schema {
85 	void *attribute_handler_override_private;
86 	ldb_attribute_handler_override_fn_t attribute_handler_override;
87 
88 	/* attribute handling table */
89 	unsigned num_attributes;
90 	struct ldb_schema_attribute *attributes;
91 
92 	unsigned num_dn_extended_syntax;
93 	struct ldb_dn_extended_syntax *dn_extended_syntax;
94 
95 	/*
96 	 * If set, the attribute_handler_override has the details of
97 	 * what attributes have an index
98 	 */
99 	bool index_handler_override;
100 	bool one_level_indexes;
101 
102 	const char *GUID_index_attribute;
103 	const char *GUID_index_dn_component;
104 };
105 
106 /*
107   every ldb connection is started by establishing a ldb_context
108 */
109 struct ldb_context {
110 	/* the operations provided by the backend */
111 	struct ldb_module *modules;
112 
113 	/* debugging operations */
114 	struct ldb_debug_ops debug_ops;
115 
116 	/* extended matching rules */
117 	struct ldb_extended_match_entry {
118 		const struct ldb_extended_match_rule *rule;
119 		struct ldb_extended_match_entry *prev, *next;
120 	} *extended_match_rules;
121 
122 	/* custom utf8 functions */
123 	struct ldb_utf8_fns utf8_fns;
124 
125 	/* backend specific opaque parameters */
126 	struct ldb_opaque {
127 		struct ldb_opaque *next;
128 		const char *name;
129 		void *value;
130 	} *opaque;
131 
132 	struct ldb_schema schema;
133 
134 	char *err_string;
135 
136 	int transaction_active;
137 
138 	int default_timeout;
139 
140 	unsigned int flags;
141 
142 	unsigned int create_perms;
143 
144 	struct tevent_context *ev_ctx;
145 
146 	/*
147 	 * If the backend holds locks, we must not use a global event
148 	 * context, so this flag will be set and ldb_handle_new() will
149 	 * build a new event context
150 	 */
151 	bool require_private_event_context;
152 
153 	bool prepare_commit_done;
154 
155 	char *partial_debug;
156 
157 	struct poptOption *popt_options;
158 
159 	/*
160 	 * The ldb options passed to ldb_connect
161 	 * A NULL terminated array of zero terminated strings
162 	 */
163 	const char **options;
164 };
165 
166 /* The following definitions come from lib/ldb/common/ldb.c  */
167 
168 extern const struct ldb_module_ops ldb_objectclass_module_ops;
169 extern const struct ldb_module_ops ldb_paged_results_module_ops;
170 extern const struct ldb_module_ops ldb_rdn_name_module_ops;
171 extern const struct ldb_module_ops ldb_schema_module_ops;
172 extern const struct ldb_module_ops ldb_asq_module_ops;
173 extern const struct ldb_module_ops ldb_server_sort_module_ops;
174 extern const struct ldb_module_ops ldb_ldap_module_ops;
175 extern const struct ldb_module_ops ldb_ildap_module_ops;
176 extern const struct ldb_module_ops ldb_paged_searches_module_ops;
177 extern const struct ldb_module_ops ldb_tdb_module_ops;
178 extern const struct ldb_module_ops ldb_skel_module_ops;
179 extern const struct ldb_module_ops ldb_subtree_rename_module_ops;
180 extern const struct ldb_module_ops ldb_subtree_delete_module_ops;
181 extern const struct ldb_module_ops ldb_sqlite3_module_ops;
182 extern const struct ldb_module_ops ldb_wins_ldb_module_ops;
183 extern const struct ldb_module_ops ldb_ranged_results_module_ops;
184 
185 extern const struct ldb_backend_ops ldb_tdb_backend_ops;
186 extern const struct ldb_backend_ops ldb_sqlite3_backend_ops;
187 extern const struct ldb_backend_ops ldb_ldap_backend_ops;
188 extern const struct ldb_backend_ops ldb_ldapi_backend_ops;
189 extern const struct ldb_backend_ops ldb_ldaps_backend_ops;
190 
191 int ldb_setup_wellknown_attributes(struct ldb_context *ldb);
192 /*
193   remove attributes with a specified flag (eg LDB_ATTR_FLAG_FROM_DB) for this ldb context
194 
195   This is to permit correct reloads
196 */
197 void ldb_schema_attribute_remove_flagged(struct ldb_context *ldb, unsigned int flag);
198 int ldb_schema_attribute_fill_with_syntax(struct ldb_context *ldb,
199 					  TALLOC_CTX *mem_ctx,
200 					  const char *attribute,
201 					  unsigned flags,
202 					  const struct ldb_schema_syntax *syntax,
203 					  struct ldb_schema_attribute *a);
204 
205 const char **ldb_subclass_list(struct ldb_context *ldb, const char *classname);
206 void ldb_subclass_remove(struct ldb_context *ldb, const char *classname);
207 int ldb_subclass_add(struct ldb_context *ldb, const char *classname, const char *subclass);
208 
209 /* The following definitions come from lib/ldb/common/ldb_utf8.c */
210 char *ldb_casefold_default(void *context, TALLOC_CTX *mem_ctx, const char *s, size_t n);
211 
212 void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f);
213 
214 
215 /* The following definitions come from lib/ldb/common/ldb_modules.c  */
216 
217 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string);
218 int ldb_load_modules(struct ldb_context *ldb, const char *options[]);
219 
220 struct ldb_val ldb_binary_decode(TALLOC_CTX *mem_ctx, const char *str);
221 
222 
223 /* The following definitions come from lib/ldb/common/ldb_options.c  */
224 
225 const char *ldb_options_find(struct ldb_context *ldb, const char *options[],
226 			     const char *option_name);
227 const char **ldb_options_copy(TALLOC_CTX *ctx, const char *options[]);
228 
229 /* The following definitions come from lib/ldb/common/ldb_ldif.c  */
230 
231 struct ldif_read_file_state {
232 	FILE *f;
233 	size_t line_no;
234 };
235 
236 struct ldb_ldif *ldb_ldif_read_file_state(struct ldb_context *ldb,
237 					  struct ldif_read_file_state *state);
238 
239 char *ldb_ldif_write_redacted_trace_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
240 					   const struct ldb_ldif *ldif);
241 
242 /*
243  * Get the LDB context in use on an LDB DN.
244  *
245  * This is helpful to the python LDB code, which may use as part of
246  * adding base and child components to an existing DN.
247  */
248 struct ldb_context *ldb_dn_get_ldb_context(struct ldb_dn *dn);
249 
250 #define LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES 1
251 
252 /**
253   Determine whether any values in an element are also in another element,
254   and optionally fix that.
255 
256   \param ldb      an ldb context
257   \param mem_ctx  a talloc context
258   \param el       an element
259   \param other_el another element
260   \param options  flags controlling the function behaviour
261 
262   Without the LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES flag, return
263   LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS if the elements share values, and
264   LDB_SUCCESS if they don't. That is, determine whether there is an
265   intersection without changing anything.
266 
267   With the LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES flag, any values in common
268   are removed from the first element and LDB_SUCCESS is returned.
269 
270   LDB_ERR_OPERATIONS_ERROR indicates an allocation failure or an unknown option.
271   LDB_ERR_INAPPROPRIATE_MATCHING means the elements differ in name.
272 */
273 
274 int ldb_msg_find_common_values(struct ldb_context *ldb,
275 			       TALLOC_CTX *mem_ctx,
276 			       struct ldb_message_element *el,
277 			       struct ldb_message_element *other_el,
278 			       uint32_t options);
279 
280 /**
281    Detect whether an element contains duplicate values
282 
283    \param ldb a currently unused ldb_context struct
284    \param mem_ctx a talloc context
285    \param el the element to search
286    \param duplicate will point to a duplicate value if there are duplicates,
287    or NULL otherwise.
288    \param options is a flags field. All values are reserved.
289 
290    \return an ldb error code. LDB_ERR_OPERATIONS_ERROR indicates an allocation
291    failure or an unknown option flag. Otherwise LDB_SUCCESS.
292 
293    \note This search is case sensitive
294 */
295 int ldb_msg_find_duplicate_val(struct ldb_context *ldb,
296 			       TALLOC_CTX *mem_ctx,
297 			       const struct ldb_message_element *el,
298 			       struct ldb_val **duplicate,
299 			       uint32_t options);
300 /**
301   Check if a particular message will match the given filter
302 
303   \param ldb an ldb context
304   \param msg the message to be checked
305   \param tree the filter tree to check against
306   \param scope the scope to match against
307          (to avoid matching special DNs except on a base search)
308   \param matched a pointer to a boolean set true if it matches,
309          false otherwise
310 
311   returns LDB_SUCCESS or an error
312 
313   \note this is a recursive function, and does short-circuit evaluation
314  */
315 int ldb_match_message(struct ldb_context *ldb,
316 		      const struct ldb_message *msg,
317 		      const struct ldb_parse_tree *tree,
318 		      enum ldb_scope scope, bool *matched);
319 
320 #endif
321