1 #include "replace.h"
2 #include "system/filesys.h"
3 #include "system/time.h"
4 #include "tdb.h"
5 #include "ldb_module.h"
6 
7 #ifndef __LDB_KV_H__
8 #define __LDB_KV_H__
9 struct ldb_kv_private;
10 typedef int (*ldb_kv_traverse_fn)(struct ldb_kv_private *ldb_kv,
11 				  struct ldb_val key,
12 				  struct ldb_val data,
13 				  void *ctx);
14 
15 struct kv_db_ops {
16 	uint32_t options;
17 
18 	int (*store)(struct ldb_kv_private *ldb_kv,
19 		     struct ldb_val key,
20 		     struct ldb_val data,
21 		     int flags);
22 	int (*delete)(struct ldb_kv_private *ldb_kv, struct ldb_val key);
23 	int (*iterate)(struct ldb_kv_private *ldb_kv,
24 		       ldb_kv_traverse_fn fn,
25 		       void *ctx);
26 	int (*update_in_iterate)(struct ldb_kv_private *ldb_kv,
27 				 struct ldb_val key,
28 				 struct ldb_val key2,
29 				 struct ldb_val data,
30 				 void *ctx);
31 	int (*fetch_and_parse)(struct ldb_kv_private *ldb_kv,
32 			       struct ldb_val key,
33 			       int (*parser)(struct ldb_val key,
34 					     struct ldb_val data,
35 					     void *private_data),
36 			       void *ctx);
37 	int (*iterate_range)(struct ldb_kv_private *ldb_kv,
38 			     struct ldb_val start_key,
39 			     struct ldb_val end_key,
40 			     ldb_kv_traverse_fn fn,
41 			     void *ctx);
42 	int (*lock_read)(struct ldb_module *);
43 	int (*unlock_read)(struct ldb_module *);
44 	int (*begin_write)(struct ldb_kv_private *);
45 	int (*prepare_write)(struct ldb_kv_private *);
46 	int (*abort_write)(struct ldb_kv_private *);
47 	int (*finish_write)(struct ldb_kv_private *);
48 	int (*error)(struct ldb_kv_private *ldb_kv);
49 	const char *(*errorstr)(struct ldb_kv_private *ldb_kv);
50 	const char *(*name)(struct ldb_kv_private *ldb_kv);
51 	bool (*has_changed)(struct ldb_kv_private *ldb_kv);
52 	bool (*transaction_active)(struct ldb_kv_private *ldb_kv);
53 	size_t (*get_size)(struct ldb_kv_private *ldb_kv);
54 	int (*begin_nested_write)(struct ldb_kv_private *);
55 	int (*finish_nested_write)(struct ldb_kv_private *);
56 	int (*abort_nested_write)(struct ldb_kv_private *);
57 };
58 
59 /* this private structure is used by the key value backends in the
60    ldb_context */
61 struct ldb_kv_private {
62 	const struct kv_db_ops *kv_ops;
63 	struct ldb_module *module;
64 	TDB_CONTEXT *tdb;
65 	struct lmdb_private *lmdb_private;
66 	unsigned int connect_flags;
67 
68 	unsigned long long sequence_number;
69 	uint32_t pack_format_version;
70 	uint32_t target_pack_format_version;
71 	uint32_t pack_format_override;
72 
73 	/* the low level tdb seqnum - used to avoid loading BASEINFO when
74 	   possible */
75 	int tdb_seqnum;
76 
77 	struct ldb_kv_cache {
78 		struct ldb_message *indexlist;
79 		bool one_level_indexes;
80 		bool attribute_indexes;
81 		const char *GUID_index_attribute;
82 		const char *GUID_index_dn_component;
83 	} *cache;
84 
85 
86 	bool check_base;
87 	bool disallow_dn_filter;
88 	/*
89 	 * To improve the performance of batch operations we maintain a cache
90 	 * of index records, these entries get written to disk in the
91 	 * prepare_commit phase.
92 	 */
93 	struct ldb_kv_idxptr *idxptr;
94 	/*
95 	 * To ensure that the indexes in idxptr are consistent we cache any
96 	 * index updates during an operation, i.e. ldb_kv_add, ldb_kv_delete ...
97 	 * Once the changes to the data record have been commited to disk
98 	 * the contents of this cache are copied to idxptr
99 	 */
100 	struct ldb_kv_idxptr *nested_idx_ptr;
101 	/*
102 	 * If batch mode is set the sub transactions and index caching
103 	 * wrapping individual operations is disabled.
104 	 * This is to improve the performance of large batch operations
105 	 * i.e. domain joins.
106 	 */
107 	bool batch_mode;
108 	/*
109 	 * Has an operation failed, if true and we're in batch_mode
110 	 * the transaction commit will fail.
111 	 */
112 	bool operation_failed;
113 
114 	bool prepared_commit;
115 	int read_lock_count;
116 
117 	bool warn_unindexed;
118 	bool warn_reindex;
119 
120 	bool read_only;
121 
122 	bool reindex_failed;
123 
124 	const struct ldb_schema_syntax *GUID_index_syntax;
125 
126 	/*
127 	 * Maximum index key length.  If non zero keys longer than this length
128 	 * will be truncated for non unique indexes. Keys for unique indexes
129 	 * greater than this length will be rejected.
130 	 */
131 	unsigned max_key_length;
132 
133 	/*
134 	 * To allow testing that ensures the DB does not fall back
135 	 * to a full scan
136 	 */
137 	bool disable_full_db_scan;
138 
139 	/*
140 	 * The PID that opened this database so we don't work in a
141 	 * fork()ed child.
142 	 */
143 	pid_t pid;
144 
145 	/*
146 	 * The size to be used for the index transaction cache
147 	 */
148 	size_t index_transaction_cache_size;
149 };
150 
151 struct ldb_kv_context {
152 	struct ldb_module *module;
153 	struct ldb_request *req;
154 
155 	bool request_terminated;
156 	struct ldb_kv_req_spy *spy;
157 
158 	/* search stuff */
159 	const struct ldb_parse_tree *tree;
160 	struct ldb_dn *base;
161 	enum ldb_scope scope;
162 	const char * const *attrs;
163 	struct tevent_timer *timeout_event;
164 
165 	/* error handling */
166 	int error;
167 };
168 
169 struct ldb_kv_reindex_context {
170 	int error;
171 	uint32_t count;
172 };
173 
174 struct ldb_kv_repack_context {
175 	int error;
176 	uint32_t count;
177 	bool normal_record_seen;
178 	uint32_t old_version;
179 };
180 
181 
182 /* special record types */
183 #define LDB_KV_INDEX      "@INDEX"
184 #define LDB_KV_INDEXLIST  "@INDEXLIST"
185 #define LDB_KV_IDX        "@IDX"
186 #define LDB_KV_IDXVERSION "@IDXVERSION"
187 #define LDB_KV_IDXATTR    "@IDXATTR"
188 #define LDB_KV_IDXONE     "@IDXONE"
189 #define LDB_KV_IDXDN     "@IDXDN"
190 #define LDB_KV_IDXGUID    "@IDXGUID"
191 #define LDB_KV_IDX_DN_GUID "@IDX_DN_GUID"
192 
193 /*
194  * This will be used to indicate when a new, yet to be developed
195  * sub-database version of the indicies are in use, to ensure we do
196  * not load future databases unintentionally.
197  */
198 
199 #define LDB_KV_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
200 
201 #define LDB_KV_BASEINFO   "@BASEINFO"
202 #define LDB_KV_OPTIONS    "@OPTIONS"
203 #define LDB_KV_ATTRIBUTES "@ATTRIBUTES"
204 
205 /* special attribute types */
206 #define LDB_KV_SEQUENCE_NUMBER "sequenceNumber"
207 #define LDB_KV_CHECK_BASE "checkBaseOnSearch"
208 #define LDB_KV_DISALLOW_DN_FILTER "disallowDNFilter"
209 #define LDB_KV_MOD_TIMESTAMP "whenChanged"
210 #define LDB_KV_OBJECTCLASS "objectClass"
211 
212 /* DB keys */
213 #define LDB_KV_GUID_KEY_PREFIX "GUID="
214 #define LDB_KV_GUID_SIZE 16
215 #define LDB_KV_GUID_KEY_SIZE (LDB_KV_GUID_SIZE + sizeof(LDB_KV_GUID_KEY_PREFIX) - 1)
216 
217 /* LDB KV options */
218 /*
219  * This allows pointers to be referenced after the callback to any variant of
220  * iterate or fetch_and_parse -- as long as an overall read lock is held.
221  */
222 #define LDB_KV_OPTION_STABLE_READ_LOCK 0x00000001
223 
224 /*
225  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_cache.c
226  */
227 
228 int ldb_kv_cache_reload(struct ldb_module *module);
229 int ldb_kv_cache_load(struct ldb_module *module);
230 int ldb_kv_increase_sequence_number(struct ldb_module *module);
231 int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
232 
233 /*
234  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_index.c
235  */
236 
237 /*
238  * The default size of the in memory TDB used to cache index records
239  * The value chosen gives a prime modulo for the hash table and keeps the
240  * tdb memory overhead under 4 kB
241  */
242 #define DEFAULT_INDEX_CACHE_SIZE 491
243 
244 struct ldb_parse_tree;
245 
246 int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
247 int ldb_kv_index_add_new(struct ldb_module *module,
248 			 struct ldb_kv_private *ldb_kv,
249 			 const struct ldb_message *msg);
250 int ldb_kv_index_delete(struct ldb_module *module,
251 			const struct ldb_message *msg);
252 int ldb_kv_index_del_element(struct ldb_module *module,
253 			     struct ldb_kv_private *ldb_kv,
254 			     const struct ldb_message *msg,
255 			     struct ldb_message_element *el);
256 int ldb_kv_index_add_element(struct ldb_module *module,
257 			     struct ldb_kv_private *ldb_kv,
258 			     const struct ldb_message *msg,
259 			     struct ldb_message_element *el);
260 int ldb_kv_index_del_value(struct ldb_module *module,
261 			   struct ldb_kv_private *ldb_kv,
262 			   const struct ldb_message *msg,
263 			   struct ldb_message_element *el,
264 			   unsigned int v_idx);
265 int ldb_kv_reindex(struct ldb_module *module);
266 int ldb_kv_repack(struct ldb_module *module);
267 int ldb_kv_index_transaction_start(
268 	struct ldb_module *module,
269 	size_t cache_size);
270 int ldb_kv_index_transaction_commit(struct ldb_module *module);
271 int ldb_kv_index_transaction_cancel(struct ldb_module *module);
272 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
273 			   struct ldb_kv_private *ldb_kv,
274 			   TALLOC_CTX *mem_ctx,
275 			   struct ldb_dn *dn,
276 			  struct ldb_val *key);
277 
278 /*
279  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_search.c
280  */
281 int ldb_kv_search_dn1(struct ldb_module *module,
282 		      struct ldb_dn *dn,
283 		      struct ldb_message *msg,
284 		      unsigned int unpack_flags);
285 int ldb_kv_search_base(struct ldb_module *module,
286 		       TALLOC_CTX *mem_ctx,
287 		       struct ldb_dn *dn,
288 		       struct ldb_dn **ret_dn);
289 int ldb_kv_search_key(struct ldb_module *module,
290 		      struct ldb_kv_private *ldb_kv,
291 		      const struct ldb_val ldb_key,
292 		      struct ldb_message *msg,
293 		      unsigned int unpack_flags);
294 int ldb_kv_filter_attrs(struct ldb_context *ldb,
295 			const struct ldb_message *msg,
296 			const char *const *attrs,
297 			struct ldb_message *filtered_msg);
298 int ldb_kv_search(struct ldb_kv_context *ctx);
299 
300 /*
301  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv.c  */
302 /*
303  * Determine if this key could hold a normal record.  We allow the new
304  * GUID index, the old DN index and a possible future ID= but not
305  * DN=@.
306  */
307 bool ldb_kv_key_is_normal_record(struct ldb_val key);
308 struct ldb_val ldb_kv_key_dn(TALLOC_CTX *mem_ctx,
309 			     struct ldb_dn *dn);
310 struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
311 			     TALLOC_CTX *mem_ctx,
312 			      const struct ldb_message *msg);
313 int ldb_kv_guid_to_key(const struct ldb_val *GUID_val,
314 		       struct ldb_val *key);
315 int ldb_kv_idx_to_key(struct ldb_module *module,
316 		      struct ldb_kv_private *ldb_kv,
317 		      TALLOC_CTX *mem_ctx,
318 		      const struct ldb_val *idx_val,
319 		      struct ldb_val *key);
320 int ldb_kv_store(struct ldb_module *module,
321 		 const struct ldb_message *msg,
322 		 int flgs);
323 int ldb_kv_modify_internal(struct ldb_module *module,
324 			   const struct ldb_message *msg,
325 			   struct ldb_request *req);
326 int ldb_kv_delete_noindex(struct ldb_module *module,
327 			  const struct ldb_message *msg);
328 int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
329 		      const char *name,
330 		      struct ldb_context *ldb,
331 		      const char *options[],
332 		      struct ldb_module **_module);
333 int ldb_kv_index_sub_transaction_start(struct ldb_kv_private *ldb_kv);
334 int ldb_kv_index_sub_transaction_cancel(struct ldb_kv_private *ldb_kv);
335 int ldb_kv_index_sub_transaction_commit(struct ldb_kv_private *ldb_kv);
336 #endif /* __LDB_KV_H__ */
337