ldb_tdb: Ensure we can not commit an index that is corrupt due to partial re-index
[metze/samba/wip.git] / lib / ldb / ldb_tdb / ldb_tdb.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 struct ltdb_private;
8 typedef int (*ldb_kv_traverse_fn)(struct ltdb_private *ltdb,
9                                   struct ldb_val key, struct ldb_val data,
10                                   void *ctx);
11
12 struct kv_db_ops {
13         int (*store)(struct ltdb_private *ltdb, TDB_DATA key, TDB_DATA data, int flags);
14         int (*delete)(struct ltdb_private *ltdb, TDB_DATA key);
15         int (*iterate)(struct ltdb_private *ltdb, ldb_kv_traverse_fn fn, void *ctx);
16         int (*update_in_iterate)(struct ltdb_private *ltdb, TDB_DATA key,
17                                  TDB_DATA key2, TDB_DATA data, void *ctx);
18         int (*fetch_and_parse)(struct ltdb_private *ltdb, TDB_DATA key,
19                                int (*parser)(TDB_DATA key, TDB_DATA data,
20                                              void *private_data),
21                                void *ctx);
22         int (*lock_read)(struct ldb_module *);
23         int (*unlock_read)(struct ldb_module *);
24         int (*begin_write)(struct ltdb_private *);
25         int (*prepare_write)(struct ltdb_private *);
26         int (*abort_write)(struct ltdb_private *);
27         int (*finish_write)(struct ltdb_private *);
28         int (*error)(struct ltdb_private *ltdb);
29         const char * (*errorstr)(struct ltdb_private *ltdb);
30         const char * (*name)(struct ltdb_private *ltdb);
31         bool (*has_changed)(struct ltdb_private *ltdb);
32 };
33
34 /* this private structure is used by the ltdb backend in the
35    ldb_context */
36 struct ltdb_private {
37         const struct kv_db_ops *kv_ops;
38         TDB_CONTEXT *tdb;
39         unsigned int connect_flags;
40         
41         unsigned long long sequence_number;
42
43         /* the low level tdb seqnum - used to avoid loading BASEINFO when
44            possible */
45         int tdb_seqnum;
46
47         struct ltdb_cache {
48                 struct ldb_message *indexlist;
49                 bool one_level_indexes;
50                 bool attribute_indexes;
51                 const char *GUID_index_attribute;
52                 const char *GUID_index_dn_component;
53         } *cache;
54
55         int in_transaction;
56
57         bool check_base;
58         bool disallow_dn_filter;
59         struct ltdb_idxptr *idxptr;
60         bool prepared_commit;
61         int read_lock_count;
62
63         bool warn_unindexed;
64         bool warn_reindex;
65
66         bool read_only;
67
68         bool reindex_failed;
69
70         const struct ldb_schema_syntax *GUID_index_syntax;
71
72         /*
73          * Maximum index key length.  If non zero keys longer than this length
74          * will be truncated for non unique indexes. Keys for unique indexes
75          * greater than this length will be rejected.
76          */
77         unsigned max_key_length;
78 };
79
80 struct ltdb_context {
81         struct ldb_module *module;
82         struct ldb_request *req;
83
84         bool request_terminated;
85         struct ltdb_req_spy *spy;
86
87         /* search stuff */
88         const struct ldb_parse_tree *tree;
89         struct ldb_dn *base;
90         enum ldb_scope scope;
91         const char * const *attrs;
92         struct tevent_timer *timeout_event;
93
94         /* error handling */
95         int error;
96 };
97
98 struct ltdb_reindex_context {
99         struct ldb_module *module;
100         int error;
101         uint32_t count;
102 };
103
104
105 /* special record types */
106 #define LTDB_INDEX      "@INDEX"
107 #define LTDB_INDEXLIST  "@INDEXLIST"
108 #define LTDB_IDX        "@IDX"
109 #define LTDB_IDXVERSION "@IDXVERSION"
110 #define LTDB_IDXATTR    "@IDXATTR"
111 #define LTDB_IDXONE     "@IDXONE"
112 #define LTDB_IDXDN     "@IDXDN"
113 #define LTDB_IDXGUID    "@IDXGUID"
114 #define LTDB_IDX_DN_GUID "@IDX_DN_GUID"
115 #define LTDB_BASEINFO   "@BASEINFO"
116 #define LTDB_OPTIONS    "@OPTIONS"
117 #define LTDB_ATTRIBUTES "@ATTRIBUTES"
118
119 /* special attribute types */
120 #define LTDB_SEQUENCE_NUMBER "sequenceNumber"
121 #define LTDB_CHECK_BASE "checkBaseOnSearch"
122 #define LTDB_DISALLOW_DN_FILTER "disallowDNFilter"
123 #define LTDB_MOD_TIMESTAMP "whenChanged"
124 #define LTDB_OBJECTCLASS "objectClass"
125
126 /* DB keys */
127 #define LTDB_GUID_KEY_PREFIX "GUID="
128 #define LTDB_GUID_SIZE 16
129 #define LTDB_GUID_KEY_SIZE (LTDB_GUID_SIZE + sizeof(LTDB_GUID_KEY_PREFIX) - 1)
130
131 /* The following definitions come from lib/ldb/ldb_tdb/ldb_cache.c  */
132
133 int ltdb_cache_reload(struct ldb_module *module);
134 int ltdb_cache_load(struct ldb_module *module);
135 int ltdb_increase_sequence_number(struct ldb_module *module);
136 int ltdb_check_at_attributes_values(const struct ldb_val *value);
137
138 /* The following definitions come from lib/ldb/ldb_tdb/ldb_index.c  */
139
140 struct ldb_parse_tree;
141
142 int ltdb_search_indexed(struct ltdb_context *ctx, uint32_t *);
143 int ltdb_index_add_new(struct ldb_module *module,
144                        struct ltdb_private *ltdb,
145                        const struct ldb_message *msg);
146 int ltdb_index_delete(struct ldb_module *module, const struct ldb_message *msg);
147 int ltdb_index_del_element(struct ldb_module *module,
148                            struct ltdb_private *ltdb,
149                            const struct ldb_message *msg,
150                            struct ldb_message_element *el);
151 int ltdb_index_add_element(struct ldb_module *module,
152                            struct ltdb_private *ltdb,
153                            const struct ldb_message *msg,
154                            struct ldb_message_element *el);
155 int ltdb_index_del_value(struct ldb_module *module,
156                          struct ltdb_private *ltdb,
157                          const struct ldb_message *msg,
158                          struct ldb_message_element *el, unsigned int v_idx);
159 int ltdb_reindex(struct ldb_module *module);
160 int ltdb_index_transaction_start(struct ldb_module *module);
161 int ltdb_index_transaction_commit(struct ldb_module *module);
162 int ltdb_index_transaction_cancel(struct ldb_module *module);
163 int ltdb_key_dn_from_idx(struct ldb_module *module,
164                          struct ltdb_private *ltdb,
165                          TALLOC_CTX *mem_ctx,
166                          struct ldb_dn *dn,
167                          TDB_DATA *tdb_key);
168
169 /* The following definitions come from lib/ldb/ldb_tdb/ldb_search.c  */
170
171 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
172                       const struct ldb_val *val);
173 void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg);
174 int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg,
175                     unsigned int unpack_flags);
176 int ltdb_search_base(struct ldb_module *module,
177                      TALLOC_CTX *mem_ctx,
178                      struct ldb_dn *dn,
179                      struct ldb_dn **ret_dn);
180 int ltdb_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
181                     struct TDB_DATA tdb_key,
182                     struct ldb_message *msg,
183                     unsigned int unpack_flags);
184 int ltdb_filter_attrs(TALLOC_CTX *mem_ctx,
185                       const struct ldb_message *msg, const char * const *attrs,
186                       struct ldb_message **filtered_msg);
187 int ltdb_search(struct ltdb_context *ctx);
188
189 /* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c  */
190 /* 
191  * Determine if this key could hold a record.  We allow the new GUID
192  * index, the old DN index and a possible future ID=
193  */
194 bool ltdb_key_is_record(TDB_DATA key);
195 TDB_DATA ltdb_key_dn(struct ldb_module *module, TALLOC_CTX *mem_ctx,
196                      struct ldb_dn *dn);
197 TDB_DATA ltdb_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
198                       const struct ldb_message *msg);
199 int ltdb_guid_to_key(struct ldb_module *module,
200                      struct ltdb_private *ltdb,
201                      const struct ldb_val *GUID_val,
202                      TDB_DATA *key);
203 int ltdb_idx_to_key(struct ldb_module *module,
204                     struct ltdb_private *ltdb,
205                     TALLOC_CTX *mem_ctx,
206                     const struct ldb_val *idx_val,
207                     TDB_DATA *key);
208 TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn);
209 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs);
210 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg, struct ldb_request *req);
211 int ltdb_delete_noindex(struct ldb_module *module,
212                         const struct ldb_message *msg);
213 int ltdb_err_map(enum TDB_ERROR tdb_code);
214
215 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
216                                    const char *path, int hash_size, int tdb_flags,
217                                    int open_flags, mode_t mode,
218                                    struct ldb_context *ldb);
219 int init_store(struct ltdb_private *ltdb, const char *name,
220                struct ldb_context *ldb, const char *options[],
221                struct ldb_module **_module);
222
223 int ltdb_connect(struct ldb_context *ldb, const char *url,
224                  unsigned int flags, const char *options[],
225                  struct ldb_module **_module);