s4:torture: Initialize struct cldap_netlogon
[Samba.git] / source3 / winbindd / idmap_tdb.c
blob77714d9ca12e55b162b2a4dec7088f1a5d1ef7f6
1 /*
2 Unix SMB/CIFS implementation.
4 idmap TDB backend
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) Jeremy Allison 2006
9 Copyright (C) Simo Sorce 2003-2006
10 Copyright (C) Michael Adam 2009-2010
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program 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
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "system/filesys.h"
28 #include "winbindd.h"
29 #include "idmap.h"
30 #include "idmap_rw.h"
31 #include "idmap_tdb_common.h"
32 #include "dbwrap/dbwrap.h"
33 #include "dbwrap/dbwrap_open.h"
34 #include "../libcli/security/security.h"
35 #include "util_tdb.h"
36 #include "lib/util/string_wrappers.h"
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_IDMAP
41 /* idmap version determines auto-conversion - this is the database
42 structure version specifier. */
44 #define IDMAP_VERSION 2
46 /* High water mark keys */
47 #define HWM_GROUP "GROUP HWM"
48 #define HWM_USER "USER HWM"
50 struct convert_fn_state {
51 struct db_context *db;
52 bool failed;
55 /*****************************************************************************
56 For idmap conversion: convert one record to new format
57 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
58 instead of the SID.
59 *****************************************************************************/
60 static int convert_fn(struct db_record *rec, void *private_data)
62 struct winbindd_domain *domain;
63 char *p;
64 NTSTATUS status;
65 struct dom_sid sid;
66 uint32_t rid;
67 struct dom_sid_buf keystr;
68 fstring dom_name;
69 TDB_DATA key;
70 TDB_DATA key2;
71 TDB_DATA value;
72 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
74 key = dbwrap_record_get_key(rec);
76 DEBUG(10,("Converting %s\n", (const char *)key.dptr));
78 p = strchr((const char *)key.dptr, '/');
79 if (!p)
80 return 0;
82 *p = 0;
83 fstrcpy(dom_name, (const char *)key.dptr);
84 *p++ = '/';
86 domain = find_domain_from_name(dom_name);
87 if (domain == NULL) {
88 /* We must delete the old record. */
89 DEBUG(0,("Unable to find domain %s\n", dom_name ));
90 DEBUG(0,("deleting record %s\n", (const char *)key.dptr ));
92 status = dbwrap_record_delete(rec);
93 if (!NT_STATUS_IS_OK(status)) {
94 DEBUG(0, ("Unable to delete record %s:%s\n",
95 (const char *)key.dptr,
96 nt_errstr(status)));
97 s->failed = true;
98 return -1;
101 return 0;
104 rid = atoi(p);
106 sid_compose(&sid, &domain->sid, rid);
108 key2 = string_term_tdb_data(dom_sid_str_buf(&sid, &keystr));
110 value = dbwrap_record_get_value(rec);
112 status = dbwrap_store(s->db, key2, value, TDB_INSERT);
113 if (!NT_STATUS_IS_OK(status)) {
114 DEBUG(0,("Unable to add record %s:%s\n",
115 (const char *)key2.dptr,
116 nt_errstr(status)));
117 s->failed = true;
118 return -1;
121 status = dbwrap_store(s->db, value, key2, TDB_REPLACE);
122 if (!NT_STATUS_IS_OK(status)) {
123 DEBUG(0,("Unable to update record %s:%s\n",
124 (const char *)value.dptr,
125 nt_errstr(status)));
126 s->failed = true;
127 return -1;
130 status = dbwrap_record_delete(rec);
131 if (!NT_STATUS_IS_OK(status)) {
132 DEBUG(0,("Unable to delete record %s:%s\n",
133 (const char *)key.dptr,
134 nt_errstr(status)));
135 s->failed = true;
136 return -1;
139 return 0;
142 /*****************************************************************************
143 Convert the idmap database from an older version.
144 *****************************************************************************/
146 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
148 int32_t vers;
149 struct convert_fn_state s;
150 NTSTATUS status;
152 status = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &vers);
153 if (!NT_STATUS_IS_OK(status)) {
154 vers = -1;
157 if (IREV(vers) == IDMAP_VERSION) {
158 /* Arrggghh ! Bytereversed - make order independent ! */
160 * high and low records were created on a
161 * big endian machine and will need byte-reversing.
164 int32_t wm;
166 status = dbwrap_fetch_int32_bystring(db, HWM_USER, &wm);
167 if (!NT_STATUS_IS_OK(status)) {
168 wm = -1;
171 if (wm != -1) {
172 wm = IREV(wm);
173 } else {
174 wm = dom->low_id;
177 status = dbwrap_store_int32_bystring(db, HWM_USER, wm);
178 if (!NT_STATUS_IS_OK(status)) {
179 DEBUG(0, ("Unable to byteswap user hwm in idmap "
180 "database: %s\n", nt_errstr(status)));
181 return False;
184 status = dbwrap_fetch_int32_bystring(db, HWM_GROUP, &wm);
185 if (!NT_STATUS_IS_OK(status)) {
186 wm = -1;
189 if (wm != -1) {
190 wm = IREV(wm);
191 } else {
192 wm = dom->low_id;
195 status = dbwrap_store_int32_bystring(db, HWM_GROUP, wm);
196 if (!NT_STATUS_IS_OK(status)) {
197 DEBUG(0, ("Unable to byteswap group hwm in idmap "
198 "database: %s\n", nt_errstr(status)));
199 return False;
203 s.db = db;
204 s.failed = false;
206 /* the old format stored as DOMAIN/rid - now we store the SID direct */
207 status = dbwrap_traverse(db, convert_fn, &s, NULL);
209 if (!NT_STATUS_IS_OK(status)) {
210 DEBUG(0, ("Database traverse failed during conversion\n"));
211 return false;
214 if (s.failed) {
215 DEBUG(0, ("Problem during conversion\n"));
216 return False;
219 status = dbwrap_store_int32_bystring(db, "IDMAP_VERSION",
220 IDMAP_VERSION);
221 if (!NT_STATUS_IS_OK(status)) {
222 DEBUG(0, ("Unable to store idmap version in database: %s\n",
223 nt_errstr(status)));
224 return False;
227 return True;
230 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
232 uint32_t low_uid;
233 uint32_t low_gid;
234 bool update_uid = false;
235 bool update_gid = false;
236 struct idmap_tdb_common_context *ctx;
237 NTSTATUS status;
239 ctx = talloc_get_type(dom->private_data,
240 struct idmap_tdb_common_context);
242 status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_USER, &low_uid);
243 if (!NT_STATUS_IS_OK(status) || low_uid < dom->low_id) {
244 update_uid = true;
247 status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_GROUP, &low_gid);
248 if (!NT_STATUS_IS_OK(status) || low_gid < dom->low_id) {
249 update_gid = true;
252 if (!update_uid && !update_gid) {
253 return NT_STATUS_OK;
256 if (dbwrap_transaction_start(ctx->db) != 0) {
257 DEBUG(0, ("Unable to start upgrade transaction!\n"));
258 return NT_STATUS_INTERNAL_DB_ERROR;
261 if (update_uid) {
262 status = dbwrap_store_uint32_bystring(ctx->db, HWM_USER,
263 dom->low_id);
264 if (!NT_STATUS_IS_OK(status)) {
265 dbwrap_transaction_cancel(ctx->db);
266 DEBUG(0, ("Unable to initialise user hwm in idmap "
267 "database: %s\n", nt_errstr(status)));
268 return NT_STATUS_INTERNAL_DB_ERROR;
272 if (update_gid) {
273 status = dbwrap_store_uint32_bystring(ctx->db, HWM_GROUP,
274 dom->low_id);
275 if (!NT_STATUS_IS_OK(status)) {
276 dbwrap_transaction_cancel(ctx->db);
277 DEBUG(0, ("Unable to initialise group hwm in idmap "
278 "database: %s\n", nt_errstr(status)));
279 return NT_STATUS_INTERNAL_DB_ERROR;
283 if (dbwrap_transaction_commit(ctx->db) != 0) {
284 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
285 return NT_STATUS_INTERNAL_DB_ERROR;
288 return NT_STATUS_OK;
291 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
293 NTSTATUS ret;
294 TALLOC_CTX *mem_ctx;
295 char *tdbfile = NULL;
296 struct db_context *db = NULL;
297 int32_t version;
298 struct idmap_tdb_common_context *ctx;
300 ctx = talloc_get_type(dom->private_data,
301 struct idmap_tdb_common_context);
303 if (ctx->db) {
304 /* it is already open */
305 return NT_STATUS_OK;
308 /* use our own context here */
309 mem_ctx = talloc_stackframe();
311 /* use the old database if present */
312 tdbfile = state_path(talloc_tos(), "winbindd_idmap.tdb");
313 if (!tdbfile) {
314 DEBUG(0, ("Out of memory!\n"));
315 ret = NT_STATUS_NO_MEMORY;
316 goto done;
319 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
321 /* Open idmap repository */
322 db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644,
323 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
324 if (!db) {
325 DEBUG(0, ("Unable to open idmap database\n"));
326 ret = NT_STATUS_UNSUCCESSFUL;
327 goto done;
330 /* check against earlier versions */
331 ret = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &version);
332 if (!NT_STATUS_IS_OK(ret)) {
333 version = -1;
336 if (version != IDMAP_VERSION) {
337 if (dbwrap_transaction_start(db) != 0) {
338 DEBUG(0, ("Unable to start upgrade transaction!\n"));
339 ret = NT_STATUS_INTERNAL_DB_ERROR;
340 goto done;
343 if (!idmap_tdb_upgrade(dom, db)) {
344 dbwrap_transaction_cancel(db);
345 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
346 ret = NT_STATUS_INTERNAL_DB_ERROR;
347 goto done;
350 if (dbwrap_transaction_commit(db) != 0) {
351 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
352 ret = NT_STATUS_INTERNAL_DB_ERROR;
353 goto done;
357 ctx->db = talloc_move(ctx, &db);
359 ret = idmap_tdb_init_hwm(dom);
361 done:
362 talloc_free(mem_ctx);
363 return ret;
366 /**********************************************************************
367 IDMAP MAPPING TDB BACKEND
368 **********************************************************************/
370 /*****************************
371 Initialise idmap database.
372 *****************************/
374 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom)
376 NTSTATUS ret;
377 struct idmap_tdb_common_context *ctx;
379 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
381 ctx = talloc_zero(dom, struct idmap_tdb_common_context);
382 if ( ! ctx) {
383 DEBUG(0, ("Out of memory!\n"));
384 return NT_STATUS_NO_MEMORY;
387 /* load backend specific configuration here: */
388 #if 0
389 if (strequal(dom->name, "*")) {
390 } else {
392 #endif
394 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
395 if (ctx->rw_ops == NULL) {
396 DEBUG(0, ("Out of memory!\n"));
397 ret = NT_STATUS_NO_MEMORY;
398 goto failed;
401 ctx->max_id = dom->high_id;
402 ctx->hwmkey_uid = HWM_USER;
403 ctx->hwmkey_gid = HWM_GROUP;
405 ctx->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
406 ctx->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
408 dom->private_data = ctx;
410 ret = idmap_tdb_open_db(dom);
411 if ( ! NT_STATUS_IS_OK(ret)) {
412 goto failed;
415 return NT_STATUS_OK;
417 failed:
418 talloc_free(ctx);
419 return ret;
422 static const struct idmap_methods db_methods = {
423 .init = idmap_tdb_db_init,
424 .unixids_to_sids = idmap_tdb_common_unixids_to_sids,
425 .sids_to_unixids = idmap_tdb_common_sids_to_unixids,
426 .allocate_id = idmap_tdb_common_get_new_id,
429 NTSTATUS idmap_tdb_init(TALLOC_CTX *mem_ctx)
431 DEBUG(10, ("calling idmap_tdb_init\n"));
433 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);