TDB2: make SAMBA use tdb1 again for the moment.
[amitay/samba.git] / source3 / winbindd / idmap_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    idmap TDB backend
5
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
11
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.
16
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.
21
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/>.
24 */
25
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
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_IDMAP
39
40 /* idmap version determines auto-conversion - this is the database
41    structure version specifier. */
42
43 #define IDMAP_VERSION 2
44
45 /* High water mark keys */
46 #define HWM_GROUP  "GROUP HWM"
47 #define HWM_USER   "USER HWM"
48
49 struct convert_fn_state {
50         struct db_context *db;
51         bool failed;
52 };
53
54 /*****************************************************************************
55  For idmap conversion: convert one record to new format
56  Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
57  instead of the SID.
58 *****************************************************************************/
59 static int convert_fn(struct db_record *rec, void *private_data)
60 {
61         struct winbindd_domain *domain;
62         char *p;
63         NTSTATUS status;
64         struct dom_sid sid;
65         uint32 rid;
66         fstring keystr;
67         fstring dom_name;
68         TDB_DATA key;
69         TDB_DATA key2;
70         TDB_DATA value;
71         struct convert_fn_state *s = (struct convert_fn_state *)private_data;
72
73         key = dbwrap_record_get_key(rec);
74
75         DEBUG(10,("Converting %s\n", (const char *)key.dptr));
76
77         p = strchr((const char *)key.dptr, '/');
78         if (!p)
79                 return 0;
80
81         *p = 0;
82         fstrcpy(dom_name, (const char *)key.dptr);
83         *p++ = '/';
84
85         domain = find_domain_from_name(dom_name);
86         if (domain == NULL) {
87                 /* We must delete the old record. */
88                 DEBUG(0,("Unable to find domain %s\n", dom_name ));
89                 DEBUG(0,("deleting record %s\n", (const char *)key.dptr ));
90
91                 status = dbwrap_record_delete(rec);
92                 if (!NT_STATUS_IS_OK(status)) {
93                         DEBUG(0, ("Unable to delete record %s:%s\n",
94                                 (const char *)key.dptr,
95                                 nt_errstr(status)));
96                         s->failed = true;
97                         return -1;
98                 }
99
100                 return 0;
101         }
102
103         rid = atoi(p);
104
105         sid_compose(&sid, &domain->sid, rid);
106
107         sid_to_fstring(keystr, &sid);
108         key2 = string_term_tdb_data(keystr);
109
110         value = dbwrap_record_get_value(rec);
111
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;
119         }
120
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;
128         }
129
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;
137         }
138
139         return 0;
140 }
141
142 /*****************************************************************************
143  Convert the idmap database from an older version.
144 *****************************************************************************/
145
146 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
147 {
148         int32 vers;
149         bool bigendianheader;
150         struct convert_fn_state s;
151         NTSTATUS status;
152
153         /* If we are bigendian, tdb is bigendian if NOT converted. */
154         union {
155                 uint16 large;
156                 unsigned char small[2];
157         } u;
158         u.large = 0x0102;
159         if (u.small[0] == 0x01)
160                 bigendianheader = !(dbwrap_get_flags(db) & TDB_CONVERT);
161         else {
162                 assert(u.small[0] == 0x02);
163                 bigendianheader = (dbwrap_get_flags(db) & TDB_CONVERT);
164         }
165         DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
166
167         status = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &vers);
168         if (!NT_STATUS_IS_OK(status)) {
169                 vers = -1;
170         }
171
172         if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
173                 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
174                 /*
175                  * high and low records were created on a
176                  * big endian machine and will need byte-reversing.
177                  */
178
179                 int32 wm;
180
181                 status = dbwrap_fetch_int32_bystring(db, HWM_USER, &wm);
182                 if (!NT_STATUS_IS_OK(status)) {
183                         wm = -1;
184                 }
185
186                 if (wm != -1) {
187                         wm = IREV(wm);
188                 }  else {
189                         wm = dom->low_id;
190                 }
191
192                 status = dbwrap_store_int32_bystring(db, HWM_USER, wm);
193                 if (!NT_STATUS_IS_OK(status)) {
194                         DEBUG(0, ("Unable to byteswap user hwm in idmap "
195                                   "database: %s\n", nt_errstr(status)));
196                         return False;
197                 }
198
199                 status = dbwrap_fetch_int32_bystring(db, HWM_GROUP, &wm);
200                 if (!NT_STATUS_IS_OK(status)) {
201                         wm = -1;
202                 }
203
204                 if (wm != -1) {
205                         wm = IREV(wm);
206                 } else {
207                         wm = dom->low_id;
208                 }
209
210                 status = dbwrap_store_int32_bystring(db, HWM_GROUP, wm);
211                 if (!NT_STATUS_IS_OK(status)) {
212                         DEBUG(0, ("Unable to byteswap group hwm in idmap "
213                                   "database: %s\n", nt_errstr(status)));
214                         return False;
215                 }
216         }
217
218         s.db = db;
219         s.failed = false;
220
221         /* the old format stored as DOMAIN/rid - now we store the SID direct */
222         status = dbwrap_traverse(db, convert_fn, &s, NULL);
223
224         if (!NT_STATUS_IS_OK(status)) {
225                 DEBUG(0, ("Database traverse failed during conversion\n"));
226                 return false;
227         }
228
229         if (s.failed) {
230                 DEBUG(0, ("Problem during conversion\n"));
231                 return False;
232         }
233
234         status = dbwrap_store_int32_bystring(db, "IDMAP_VERSION",
235                                              IDMAP_VERSION);
236         if (!NT_STATUS_IS_OK(status)) {
237                 DEBUG(0, ("Unable to store idmap version in database: %s\n",
238                           nt_errstr(status)));
239                 return False;
240         }
241
242         return True;
243 }
244
245 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
246 {
247         uint32_t low_uid;
248         uint32_t low_gid;
249         bool update_uid = false;
250         bool update_gid = false;
251         struct idmap_tdb_common_context *ctx;
252         NTSTATUS status;
253
254         ctx = talloc_get_type(dom->private_data,
255                               struct idmap_tdb_common_context);
256
257         status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_USER, &low_uid);
258         if (!NT_STATUS_IS_OK(status) || low_uid < dom->low_id) {
259                 update_uid = true;
260         }
261
262         status = dbwrap_fetch_uint32_bystring(ctx->db, HWM_GROUP, &low_gid);
263         if (!NT_STATUS_IS_OK(status) || low_gid < dom->low_id) {
264                 update_gid = true;
265         }
266
267         if (!update_uid && !update_gid) {
268                 return NT_STATUS_OK;
269         }
270
271         if (dbwrap_transaction_start(ctx->db) != 0) {
272                 DEBUG(0, ("Unable to start upgrade transaction!\n"));
273                 return NT_STATUS_INTERNAL_DB_ERROR;
274         }
275
276         if (update_uid) {
277                 status = dbwrap_store_uint32_bystring(ctx->db, HWM_USER,
278                                                       dom->low_id);
279                 if (!NT_STATUS_IS_OK(status)) {
280                         dbwrap_transaction_cancel(ctx->db);
281                         DEBUG(0, ("Unable to initialise user hwm in idmap "
282                                   "database: %s\n", nt_errstr(status)));
283                         return NT_STATUS_INTERNAL_DB_ERROR;
284                 }
285         }
286
287         if (update_gid) {
288                 status = dbwrap_store_uint32_bystring(ctx->db, HWM_GROUP,
289                                                       dom->low_id);
290                 if (!NT_STATUS_IS_OK(status)) {
291                         dbwrap_transaction_cancel(ctx->db);
292                         DEBUG(0, ("Unable to initialise group hwm in idmap "
293                                   "database: %s\n", nt_errstr(status)));
294                         return NT_STATUS_INTERNAL_DB_ERROR;
295                 }
296         }
297
298         if (dbwrap_transaction_commit(ctx->db) != 0) {
299                 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
300                 return NT_STATUS_INTERNAL_DB_ERROR;
301         }
302
303         return NT_STATUS_OK;
304 }
305
306 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
307 {
308         NTSTATUS ret;
309         TALLOC_CTX *mem_ctx;
310         char *tdbfile = NULL;
311         struct db_context *db = NULL;
312         int32_t version;
313         bool config_error = false;
314         struct idmap_tdb_common_context *ctx;
315
316         ctx = talloc_get_type(dom->private_data,
317                               struct idmap_tdb_common_context);
318
319         if (ctx->db) {
320                 /* it is already open */
321                 return NT_STATUS_OK;
322         }
323
324         /* use our own context here */
325         mem_ctx = talloc_stackframe();
326
327         /* use the old database if present */
328         tdbfile = state_path("winbindd_idmap.tdb");
329         if (!tdbfile) {
330                 DEBUG(0, ("Out of memory!\n"));
331                 ret = NT_STATUS_NO_MEMORY;
332                 goto done;
333         }
334
335         DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
336
337         /* Open idmap repository */
338         db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644,
339                      DBWRAP_LOCK_ORDER_1);
340         if (!db) {
341                 DEBUG(0, ("Unable to open idmap database\n"));
342                 ret = NT_STATUS_UNSUCCESSFUL;
343                 goto done;
344         }
345
346         /* check against earlier versions */
347         ret = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &version);
348         if (!NT_STATUS_IS_OK(ret)) {
349                 version = -1;
350         }
351
352         if (version != IDMAP_VERSION) {
353                 if (config_error) {
354                         DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
355                                  "possible with incomplete configuration\n",
356                                  version, IDMAP_VERSION));
357                         ret = NT_STATUS_UNSUCCESSFUL;
358                         goto done;
359                 }
360                 if (dbwrap_transaction_start(db) != 0) {
361                         DEBUG(0, ("Unable to start upgrade transaction!\n"));
362                         ret = NT_STATUS_INTERNAL_DB_ERROR;
363                         goto done;
364                 }
365
366                 if (!idmap_tdb_upgrade(dom, db)) {
367                         dbwrap_transaction_cancel(db);
368                         DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
369                         ret = NT_STATUS_INTERNAL_DB_ERROR;
370                         goto done;
371                 }
372
373                 if (dbwrap_transaction_commit(db) != 0) {
374                         DEBUG(0, ("Unable to commit upgrade transaction!\n"));
375                         ret = NT_STATUS_INTERNAL_DB_ERROR;
376                         goto done;
377                 }
378         }
379
380         ctx->db = talloc_move(ctx, &db);
381
382         ret = idmap_tdb_init_hwm(dom);
383
384 done:
385         talloc_free(mem_ctx);
386         return ret;
387 }
388
389 /**********************************************************************
390  IDMAP MAPPING TDB BACKEND
391 **********************************************************************/
392
393 /*****************************
394  Initialise idmap database. 
395 *****************************/
396
397 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom)
398 {
399         NTSTATUS ret;
400         struct idmap_tdb_common_context *ctx;
401
402         DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
403
404         ctx = talloc_zero(dom, struct idmap_tdb_common_context);
405         if ( ! ctx) {
406                 DEBUG(0, ("Out of memory!\n"));
407                 return NT_STATUS_NO_MEMORY;
408         }
409
410         /* load backend specific configuration here: */
411 #if 0
412         if (strequal(dom->name, "*")) {
413         } else {
414         }
415 #endif
416
417         ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
418         if (ctx->rw_ops == NULL) {
419                 DEBUG(0, ("Out of memory!\n"));
420                 ret = NT_STATUS_NO_MEMORY;
421                 goto failed;
422         }
423
424         ctx->max_id = dom->high_id;
425         ctx->hwmkey_uid = HWM_USER;
426         ctx->hwmkey_gid = HWM_GROUP;
427
428         ctx->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
429         ctx->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
430
431         dom->private_data = ctx;
432
433         ret = idmap_tdb_open_db(dom);
434         if ( ! NT_STATUS_IS_OK(ret)) {
435                 goto failed;
436         }
437
438         return NT_STATUS_OK;
439
440 failed:
441         talloc_free(ctx);
442         return ret;
443 }
444
445 static struct idmap_methods db_methods = {
446         .init = idmap_tdb_db_init,
447         .unixids_to_sids = idmap_tdb_common_unixids_to_sids,
448         .sids_to_unixids = idmap_tdb_common_sids_to_unixids,
449         .allocate_id = idmap_tdb_common_get_new_id,
450 };
451
452 NTSTATUS idmap_tdb_init(void)
453 {
454         DEBUG(10, ("calling idmap_tdb_init\n"));
455
456         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);
457 }