CVE-2023-0614 ldb: Make use of ldb_filter_attrs_in_place()
[samba.git] / lib / ldb / ldb_key_value / ldb_kv.h
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         /*
156          * Required as we might not get to the event loop before the
157          * timeout, so we need some old-style cooperative multitasking
158          * here.
159          */
160         struct timeval timeout_timeval;
161
162         /* Used to throttle calls to gettimeofday() */
163         size_t timeout_counter;
164
165         bool request_terminated;
166         struct ldb_kv_req_spy *spy;
167
168         /* search stuff */
169         const struct ldb_parse_tree *tree;
170         struct ldb_dn *base;
171         enum ldb_scope scope;
172         const char * const *attrs;
173         struct tevent_timer *timeout_event;
174
175         /* error handling */
176         int error;
177 };
178
179 struct ldb_kv_reindex_context {
180         int error;
181         uint32_t count;
182 };
183
184 struct ldb_kv_repack_context {
185         int error;
186         uint32_t count;
187         bool normal_record_seen;
188         uint32_t old_version;
189 };
190
191
192 /* special record types */
193 #define LDB_KV_INDEX      "@INDEX"
194 #define LDB_KV_INDEXLIST  "@INDEXLIST"
195 #define LDB_KV_IDX        "@IDX"
196 #define LDB_KV_IDXVERSION "@IDXVERSION"
197 #define LDB_KV_IDXATTR    "@IDXATTR"
198 #define LDB_KV_IDXONE     "@IDXONE"
199 #define LDB_KV_IDXDN     "@IDXDN"
200 #define LDB_KV_IDXGUID    "@IDXGUID"
201 #define LDB_KV_IDX_DN_GUID "@IDX_DN_GUID"
202
203 /*
204  * This will be used to indicate when a new, yet to be developed
205  * sub-database version of the indicies are in use, to ensure we do
206  * not load future databases unintentionally.
207  */
208
209 #define LDB_KV_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
210
211 #define LDB_KV_BASEINFO   "@BASEINFO"
212 #define LDB_KV_OPTIONS    "@OPTIONS"
213 #define LDB_KV_ATTRIBUTES "@ATTRIBUTES"
214
215 /* special attribute types */
216 #define LDB_KV_SEQUENCE_NUMBER "sequenceNumber"
217 #define LDB_KV_CHECK_BASE "checkBaseOnSearch"
218 #define LDB_KV_DISALLOW_DN_FILTER "disallowDNFilter"
219 #define LDB_KV_MOD_TIMESTAMP "whenChanged"
220 #define LDB_KV_OBJECTCLASS "objectClass"
221
222 /* DB keys */
223 #define LDB_KV_GUID_KEY_PREFIX "GUID="
224 #define LDB_KV_GUID_SIZE 16
225 #define LDB_KV_GUID_KEY_SIZE (LDB_KV_GUID_SIZE + sizeof(LDB_KV_GUID_KEY_PREFIX) - 1)
226
227 /* LDB KV options */
228 /*
229  * This allows pointers to be referenced after the callback to any variant of
230  * iterate or fetch_and_parse -- as long as an overall read lock is held.
231  */
232 #define LDB_KV_OPTION_STABLE_READ_LOCK 0x00000001
233
234 /*
235  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_cache.c
236  */
237
238 int ldb_kv_cache_reload(struct ldb_module *module);
239 int ldb_kv_cache_load(struct ldb_module *module);
240 int ldb_kv_increase_sequence_number(struct ldb_module *module);
241 int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
242
243 /*
244  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_index.c
245  */
246
247 /*
248  * The default size of the in memory TDB used to cache index records
249  * The value chosen gives a prime modulo for the hash table and keeps the
250  * tdb memory overhead under 4 kB
251  */
252 #define DEFAULT_INDEX_CACHE_SIZE 491
253
254 struct ldb_parse_tree;
255
256 int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
257 int ldb_kv_index_add_new(struct ldb_module *module,
258                          struct ldb_kv_private *ldb_kv,
259                          const struct ldb_message *msg);
260 int ldb_kv_index_delete(struct ldb_module *module,
261                         const struct ldb_message *msg);
262 int ldb_kv_index_del_element(struct ldb_module *module,
263                              struct ldb_kv_private *ldb_kv,
264                              const struct ldb_message *msg,
265                              struct ldb_message_element *el);
266 int ldb_kv_index_add_element(struct ldb_module *module,
267                              struct ldb_kv_private *ldb_kv,
268                              const struct ldb_message *msg,
269                              struct ldb_message_element *el);
270 int ldb_kv_index_del_value(struct ldb_module *module,
271                            struct ldb_kv_private *ldb_kv,
272                            const struct ldb_message *msg,
273                            struct ldb_message_element *el,
274                            unsigned int v_idx);
275 int ldb_kv_reindex(struct ldb_module *module);
276 int ldb_kv_repack(struct ldb_module *module);
277 int ldb_kv_index_transaction_start(
278         struct ldb_module *module,
279         size_t cache_size);
280 int ldb_kv_index_transaction_commit(struct ldb_module *module);
281 int ldb_kv_index_transaction_cancel(struct ldb_module *module);
282 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
283                            struct ldb_kv_private *ldb_kv,
284                            TALLOC_CTX *mem_ctx,
285                            struct ldb_dn *dn,
286                           struct ldb_val *key);
287
288 /*
289  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_search.c
290  */
291 int ldb_kv_search_dn1(struct ldb_module *module,
292                       struct ldb_dn *dn,
293                       struct ldb_message *msg,
294                       unsigned int unpack_flags);
295 int ldb_kv_search_base(struct ldb_module *module,
296                        TALLOC_CTX *mem_ctx,
297                        struct ldb_dn *dn,
298                        struct ldb_dn **ret_dn);
299 int ldb_kv_search_key(struct ldb_module *module,
300                       struct ldb_kv_private *ldb_kv,
301                       const struct ldb_val ldb_key,
302                       struct ldb_message *msg,
303                       unsigned int unpack_flags);
304 int ldb_kv_filter_attrs_in_place(struct ldb_message *msg,
305                                  const char *const *attrs);
306 int ldb_kv_search(struct ldb_kv_context *ctx);
307
308 /*
309  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv.c  */
310 /*
311  * Determine if this key could hold a normal record.  We allow the new
312  * GUID index, the old DN index and a possible future ID= but not
313  * DN=@.
314  */
315 bool ldb_kv_key_is_normal_record(struct ldb_val key);
316 struct ldb_val ldb_kv_key_dn(TALLOC_CTX *mem_ctx,
317                              struct ldb_dn *dn);
318 struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
319                              TALLOC_CTX *mem_ctx,
320                               const struct ldb_message *msg);
321 int ldb_kv_guid_to_key(const struct ldb_val *GUID_val,
322                        struct ldb_val *key);
323 int ldb_kv_idx_to_key(struct ldb_module *module,
324                       struct ldb_kv_private *ldb_kv,
325                       TALLOC_CTX *mem_ctx,
326                       const struct ldb_val *idx_val,
327                       struct ldb_val *key);
328 int ldb_kv_store(struct ldb_module *module,
329                  const struct ldb_message *msg,
330                  int flgs);
331 int ldb_kv_modify_internal(struct ldb_module *module,
332                            const struct ldb_message *msg,
333                            struct ldb_request *req);
334 int ldb_kv_delete_noindex(struct ldb_module *module,
335                           const struct ldb_message *msg);
336 int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
337                       const char *name,
338                       struct ldb_context *ldb,
339                       const char *options[],
340                       struct ldb_module **_module);
341 int ldb_kv_index_sub_transaction_start(struct ldb_kv_private *ldb_kv);
342 int ldb_kv_index_sub_transaction_cancel(struct ldb_kv_private *ldb_kv);
343 int ldb_kv_index_sub_transaction_commit(struct ldb_kv_private *ldb_kv);
344 #endif /* __LDB_KV_H__ */