ldb: relatively efficient functions for finding duplicate values
[samba.git] / lib / ldb / include / ldb_private.h
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
103 /*
104   every ldb connection is started by establishing a ldb_context
105 */
106 struct ldb_context {
107         /* the operations provided by the backend */
108         struct ldb_module *modules;
109
110         /* debugging operations */
111         struct ldb_debug_ops debug_ops;
112
113         /* extended matching rules */
114         struct ldb_extended_match_entry {
115                 const struct ldb_extended_match_rule *rule;
116                 struct ldb_extended_match_entry *prev, *next;
117         } *extended_match_rules;
118
119         /* custom utf8 functions */
120         struct ldb_utf8_fns utf8_fns;
121
122         /* backend specific opaque parameters */
123         struct ldb_opaque {
124                 struct ldb_opaque *next;
125                 const char *name;
126                 void *value;
127         } *opaque;
128
129         struct ldb_schema schema;
130
131         char *err_string;
132
133         int transaction_active;
134
135         int default_timeout;
136
137         unsigned int flags;
138
139         unsigned int create_perms;
140
141         struct tevent_context *ev_ctx;
142
143         /*
144          * If the backend holds locks, we must not use a global event
145          * context, so this flag will be set and ldb_handle_new() will
146          * build a new event context
147          */
148         bool require_private_event_context;
149
150         bool prepare_commit_done;
151
152         char *partial_debug;
153
154         struct poptOption *popt_options;
155 };
156
157 /* The following definitions come from lib/ldb/common/ldb.c  */
158
159 extern const struct ldb_module_ops ldb_objectclass_module_ops;
160 extern const struct ldb_module_ops ldb_paged_results_module_ops;
161 extern const struct ldb_module_ops ldb_rdn_name_module_ops;
162 extern const struct ldb_module_ops ldb_schema_module_ops;
163 extern const struct ldb_module_ops ldb_asq_module_ops;
164 extern const struct ldb_module_ops ldb_server_sort_module_ops;
165 extern const struct ldb_module_ops ldb_ldap_module_ops;
166 extern const struct ldb_module_ops ldb_ildap_module_ops;
167 extern const struct ldb_module_ops ldb_paged_searches_module_ops;
168 extern const struct ldb_module_ops ldb_tdb_module_ops;
169 extern const struct ldb_module_ops ldb_skel_module_ops;
170 extern const struct ldb_module_ops ldb_subtree_rename_module_ops;
171 extern const struct ldb_module_ops ldb_subtree_delete_module_ops;
172 extern const struct ldb_module_ops ldb_sqlite3_module_ops;
173 extern const struct ldb_module_ops ldb_wins_ldb_module_ops;
174 extern const struct ldb_module_ops ldb_ranged_results_module_ops;
175
176 extern const struct ldb_backend_ops ldb_tdb_backend_ops;
177 extern const struct ldb_backend_ops ldb_sqlite3_backend_ops;
178 extern const struct ldb_backend_ops ldb_ldap_backend_ops;
179 extern const struct ldb_backend_ops ldb_ldapi_backend_ops;
180 extern const struct ldb_backend_ops ldb_ldaps_backend_ops;
181
182 int ldb_setup_wellknown_attributes(struct ldb_context *ldb);
183 /*
184   remove attributes with a specified flag (eg LDB_ATTR_FLAG_FROM_DB) for this ldb context
185
186   This is to permit correct reloads
187 */
188 void ldb_schema_attribute_remove_flagged(struct ldb_context *ldb, unsigned int flag);
189 int ldb_schema_attribute_fill_with_syntax(struct ldb_context *ldb,
190                                           TALLOC_CTX *mem_ctx,
191                                           const char *attribute,
192                                           unsigned flags,
193                                           const struct ldb_schema_syntax *syntax,
194                                           struct ldb_schema_attribute *a);
195
196 const char **ldb_subclass_list(struct ldb_context *ldb, const char *classname);
197 void ldb_subclass_remove(struct ldb_context *ldb, const char *classname);
198 int ldb_subclass_add(struct ldb_context *ldb, const char *classname, const char *subclass);
199
200 /* The following definitions come from lib/ldb/common/ldb_utf8.c */
201 char *ldb_casefold_default(void *context, TALLOC_CTX *mem_ctx, const char *s, size_t n);
202
203 void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f);
204
205
206 /* The following definitions come from lib/ldb/common/ldb_modules.c  */
207
208 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string);
209 int ldb_load_modules(struct ldb_context *ldb, const char *options[]);
210
211 struct ldb_val ldb_binary_decode(TALLOC_CTX *mem_ctx, const char *str);
212
213
214 /* The following definitions come from lib/ldb/common/ldb_options.c  */
215
216 const char *ldb_options_find(struct ldb_context *ldb, const char *options[],
217                              const char *option_name);
218
219 /* The following definitions come from lib/ldb/common/ldb_ldif.c  */
220
221 struct ldif_read_file_state {
222         FILE *f;
223         size_t line_no;
224 };
225
226 struct ldb_ldif *ldb_ldif_read_file_state(struct ldb_context *ldb, 
227                                           struct ldif_read_file_state *state);
228
229 char *ldb_ldif_write_redacted_trace_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
230                                            const struct ldb_ldif *ldif);
231
232 /*
233  * Get the LDB context in use on an LDB DN.
234  *
235  * This is helpful to the python LDB code, which may use as part of
236  * adding base and child components to an existing DN.
237  */
238 struct ldb_context *ldb_dn_get_ldb_context(struct ldb_dn *dn);
239
240 #define LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES 1
241
242 /**
243   Determine whether any values in an element are also in another element,
244   and optionally fix that.
245
246   \param ldb      an ldb context
247   \param mem_ctx  a talloc context
248   \param el       an element
249   \param other_el another element
250   \param options  flags controlling the function behaviour
251
252   Without the LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES flag, return
253   LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS if the elements share values, and
254   LDB_SUCCESS if they don't. That is, determine whether there is an
255   intersection without changing anything.
256
257   With the LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES flag, any values in common
258   are removed from the first element and LDB_SUCCESS is returned.
259
260   LDB_ERR_OPERATIONS_ERROR indicates an allocation failure or an unknown option.
261   LDB_ERR_INAPPROPRIATE_MATCHING means the elements differ in name.
262 */
263
264 int ldb_msg_find_common_values(struct ldb_context *ldb,
265                                TALLOC_CTX *mem_ctx,
266                                struct ldb_message_element *el,
267                                struct ldb_message_element *other_el,
268                                uint32_t options);
269
270 /**
271    Detect whether an element contains duplicate values
272
273    \param ldb a currently unused ldb_context struct
274    \param mem_ctx a talloc context
275    \param el the element to search
276    \param duplicate will point to a duplicate value if there are duplicates,
277    or NULL otherwise.
278    \param options is a flags field. All values are reserved.
279
280    \return an ldb error code. LDB_ERR_OPERATIONS_ERROR indicates an allocation
281    failure or an unknown option flag. Otherwise LDB_SUCCESS.
282
283    \note This search is case sensitive
284 */
285 int ldb_msg_find_duplicate_val(struct ldb_context *ldb,
286                                TALLOC_CTX *mem_ctx,
287                                const struct ldb_message_element *el,
288                                struct ldb_val **duplicate,
289                                uint32_t options);
290
291 #endif