dbwrap: dbwrap_store_int32->dbwrap_store_int32_bystring
[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 BUILD_TDB2
154         /* If we are bigendian, tdb is bigendian if NOT converted. */
155         union {
156                 uint16 large;
157                 unsigned char small[2];
158         } u;
159         u.large = 0x0102;
160         if (u.small[0] == 0x01)
161                 bigendianheader = !(dbwrap_get_flags(db) & TDB_CONVERT);
162         else {
163                 assert(u.small[0] == 0x02);
164                 bigendianheader = (dbwrap_get_flags(db) & TDB_CONVERT);
165         }
166 #else
167         bigendianheader = (dbwrap_get_flags(db) & TDB_BIGENDIAN) ? True : False;
168 #endif
169         DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
170
171         status = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &vers);
172         if (!NT_STATUS_IS_OK(status)) {
173                 vers = -1;
174         }
175
176         if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
177                 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
178                 /*
179                  * high and low records were created on a
180                  * big endian machine and will need byte-reversing.
181                  */
182
183                 int32 wm;
184
185                 status = dbwrap_fetch_int32_bystring(db, HWM_USER, &wm);
186                 if (!NT_STATUS_IS_OK(status)) {
187                         wm = -1;
188                 }
189
190                 if (wm != -1) {
191                         wm = IREV(wm);
192                 }  else {
193                         wm = dom->low_id;
194                 }
195
196                 status = dbwrap_store_int32_bystring(db, HWM_USER, wm);
197                 if (!NT_STATUS_IS_OK(status)) {
198                         DEBUG(0, ("Unable to byteswap user hwm in idmap "
199                                   "database: %s\n", nt_errstr(status)));
200                         return False;
201                 }
202
203                 status = dbwrap_fetch_int32_bystring(db, HWM_GROUP, &wm);
204                 if (!NT_STATUS_IS_OK(status)) {
205                         wm = -1;
206                 }
207
208                 if (wm != -1) {
209                         wm = IREV(wm);
210                 } else {
211                         wm = dom->low_id;
212                 }
213
214                 status = dbwrap_store_int32_bystring(db, HWM_GROUP, wm);
215                 if (!NT_STATUS_IS_OK(status)) {
216                         DEBUG(0, ("Unable to byteswap group hwm in idmap "
217                                   "database: %s\n", nt_errstr(status)));
218                         return False;
219                 }
220         }
221
222         s.db = db;
223         s.failed = false;
224
225         /* the old format stored as DOMAIN/rid - now we store the SID direct */
226         status = dbwrap_traverse(db, convert_fn, &s, NULL);
227
228         if (!NT_STATUS_IS_OK(status)) {
229                 DEBUG(0, ("Database traverse failed during conversion\n"));
230                 return false;
231         }
232
233         if (s.failed) {
234                 DEBUG(0, ("Problem during conversion\n"));
235                 return False;
236         }
237
238         status = dbwrap_store_int32_bystring(db, "IDMAP_VERSION",
239                                              IDMAP_VERSION);
240         if (!NT_STATUS_IS_OK(status)) {
241                 DEBUG(0, ("Unable to store idmap version in database: %s\n",
242                           nt_errstr(status)));
243                 return False;
244         }
245
246         return True;
247 }
248
249 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
250 {
251         uint32_t low_uid;
252         uint32_t low_gid;
253         bool update_uid = false;
254         bool update_gid = false;
255         struct idmap_tdb_common_context *ctx;
256         NTSTATUS status;
257
258         ctx = talloc_get_type(dom->private_data,
259                               struct idmap_tdb_common_context);
260
261         status = dbwrap_fetch_uint32(ctx->db, HWM_USER, &low_uid);
262         if (!NT_STATUS_IS_OK(status) || low_uid < dom->low_id) {
263                 update_uid = true;
264         }
265
266         status = dbwrap_fetch_uint32(ctx->db, HWM_GROUP, &low_gid);
267         if (!NT_STATUS_IS_OK(status) || low_gid < dom->low_id) {
268                 update_gid = true;
269         }
270
271         if (!update_uid && !update_gid) {
272                 return NT_STATUS_OK;
273         }
274
275         if (dbwrap_transaction_start(ctx->db) != 0) {
276                 DEBUG(0, ("Unable to start upgrade transaction!\n"));
277                 return NT_STATUS_INTERNAL_DB_ERROR;
278         }
279
280         if (update_uid) {
281                 status = dbwrap_store_uint32(ctx->db, HWM_USER, dom->low_id);
282                 if (!NT_STATUS_IS_OK(status)) {
283                         dbwrap_transaction_cancel(ctx->db);
284                         DEBUG(0, ("Unable to initialise user hwm in idmap "
285                                   "database: %s\n", nt_errstr(status)));
286                         return NT_STATUS_INTERNAL_DB_ERROR;
287                 }
288         }
289
290         if (update_gid) {
291                 status = dbwrap_store_uint32(ctx->db, HWM_GROUP, dom->low_id);
292                 if (!NT_STATUS_IS_OK(status)) {
293                         dbwrap_transaction_cancel(ctx->db);
294                         DEBUG(0, ("Unable to initialise group hwm in idmap "
295                                   "database: %s\n", nt_errstr(status)));
296                         return NT_STATUS_INTERNAL_DB_ERROR;
297                 }
298         }
299
300         if (dbwrap_transaction_commit(ctx->db) != 0) {
301                 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
302                 return NT_STATUS_INTERNAL_DB_ERROR;
303         }
304
305         return NT_STATUS_OK;
306 }
307
308 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
309 {
310         NTSTATUS ret;
311         TALLOC_CTX *mem_ctx;
312         char *tdbfile = NULL;
313         struct db_context *db = NULL;
314         int32_t version;
315         bool config_error = false;
316         struct idmap_tdb_common_context *ctx;
317
318         ctx = talloc_get_type(dom->private_data,
319                               struct idmap_tdb_common_context);
320
321         if (ctx->db) {
322                 /* it is already open */
323                 return NT_STATUS_OK;
324         }
325
326         /* use our own context here */
327         mem_ctx = talloc_stackframe();
328
329         /* use the old database if present */
330         tdbfile = state_path("winbindd_idmap.tdb");
331         if (!tdbfile) {
332                 DEBUG(0, ("Out of memory!\n"));
333                 ret = NT_STATUS_NO_MEMORY;
334                 goto done;
335         }
336
337         DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
338
339         /* Open idmap repository */
340         db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644,
341                      DBWRAP_LOCK_ORDER_1);
342         if (!db) {
343                 DEBUG(0, ("Unable to open idmap database\n"));
344                 ret = NT_STATUS_UNSUCCESSFUL;
345                 goto done;
346         }
347
348         /* check against earlier versions */
349         ret = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &version);
350         if (!NT_STATUS_IS_OK(ret)) {
351                 version = -1;
352         }
353
354         if (version != IDMAP_VERSION) {
355                 if (config_error) {
356                         DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
357                                  "possible with incomplete configuration\n",
358                                  version, IDMAP_VERSION));
359                         ret = NT_STATUS_UNSUCCESSFUL;
360                         goto done;
361                 }
362                 if (dbwrap_transaction_start(db) != 0) {
363                         DEBUG(0, ("Unable to start upgrade transaction!\n"));
364                         ret = NT_STATUS_INTERNAL_DB_ERROR;
365                         goto done;
366                 }
367
368                 if (!idmap_tdb_upgrade(dom, db)) {
369                         dbwrap_transaction_cancel(db);
370                         DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
371                         ret = NT_STATUS_INTERNAL_DB_ERROR;
372                         goto done;
373                 }
374
375                 if (dbwrap_transaction_commit(db) != 0) {
376                         DEBUG(0, ("Unable to commit upgrade transaction!\n"));
377                         ret = NT_STATUS_INTERNAL_DB_ERROR;
378                         goto done;
379                 }
380         }
381
382         ctx->db = talloc_move(ctx, &db);
383
384         ret = idmap_tdb_init_hwm(dom);
385
386 done:
387         talloc_free(mem_ctx);
388         return ret;
389 }
390
391 /**********************************************************************
392  IDMAP MAPPING TDB BACKEND
393 **********************************************************************/
394
395 /*****************************
396  Initialise idmap database. 
397 *****************************/
398
399 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom)
400 {
401         NTSTATUS ret;
402         struct idmap_tdb_common_context *ctx;
403
404         DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
405
406         ctx = talloc_zero(dom, struct idmap_tdb_common_context);
407         if ( ! ctx) {
408                 DEBUG(0, ("Out of memory!\n"));
409                 return NT_STATUS_NO_MEMORY;
410         }
411
412         /* load backend specific configuration here: */
413 #if 0
414         if (strequal(dom->name, "*")) {
415         } else {
416         }
417 #endif
418
419         ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
420         if (ctx->rw_ops == NULL) {
421                 DEBUG(0, ("Out of memory!\n"));
422                 ret = NT_STATUS_NO_MEMORY;
423                 goto failed;
424         }
425
426         ctx->max_id = dom->high_id;
427         ctx->hwmkey_uid = HWM_USER;
428         ctx->hwmkey_gid = HWM_GROUP;
429
430         ctx->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
431         ctx->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
432
433         dom->private_data = ctx;
434
435         ret = idmap_tdb_open_db(dom);
436         if ( ! NT_STATUS_IS_OK(ret)) {
437                 goto failed;
438         }
439
440         return NT_STATUS_OK;
441
442 failed:
443         talloc_free(ctx);
444         return ret;
445 }
446
447 static struct idmap_methods db_methods = {
448         .init = idmap_tdb_db_init,
449         .unixids_to_sids = idmap_tdb_common_unixids_to_sids,
450         .sids_to_unixids = idmap_tdb_common_sids_to_unixids,
451         .allocate_id = idmap_tdb_common_get_new_id,
452 };
453
454 NTSTATUS idmap_tdb_init(void)
455 {
456         DEBUG(10, ("calling idmap_tdb_init\n"));
457
458         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);
459 }