d0575a90d88537be6fafe2cdde5d58cdf1a37260
[obnox/samba-ctdb.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    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "winbindd.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_IDMAP
30
31 /* idmap version determines auto-conversion - this is the database
32    structure version specifier. */
33
34 #define IDMAP_VERSION 2
35
36 struct idmap_tdb_context {
37         struct db_context *db;
38 };
39
40 /* High water mark keys */
41 #define HWM_GROUP  "GROUP HWM"
42 #define HWM_USER   "USER HWM"
43
44 static struct idmap_tdb_state {
45
46         /* User and group id pool */
47         uid_t low_uid, high_uid;               /* Range of uids to allocate */
48         gid_t low_gid, high_gid;               /* Range of gids to allocate */
49
50 } idmap_tdb_state;
51
52 struct convert_fn_state {
53         struct db_context *db;
54         bool failed;
55 };
56
57 /*****************************************************************************
58  For idmap conversion: convert one record to new format
59  Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
60  instead of the SID.
61 *****************************************************************************/
62 static int convert_fn(struct db_record *rec, void *private_data)
63 {
64         struct winbindd_domain *domain;
65         char *p;
66         NTSTATUS status;
67         DOM_SID sid;
68         uint32 rid;
69         fstring keystr;
70         fstring dom_name;
71         TDB_DATA key2;
72         struct convert_fn_state *s = (struct convert_fn_state *)private_data;
73
74         DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
75
76         p = strchr((const char *)rec->key.dptr, '/');
77         if (!p)
78                 return 0;
79
80         *p = 0;
81         fstrcpy(dom_name, (const char *)rec->key.dptr);
82         *p++ = '/';
83
84         domain = find_domain_from_name(dom_name);
85         if (domain == NULL) {
86                 /* We must delete the old record. */
87                 DEBUG(0,("Unable to find domain %s\n", dom_name ));
88                 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
89
90                 status = rec->delete_rec(rec);
91                 if (!NT_STATUS_IS_OK(status)) {
92                         DEBUG(0, ("Unable to delete record %s:%s\n",
93                                 (const char *)rec->key.dptr,
94                                 nt_errstr(status)));
95                         s->failed = true;
96                         return -1;
97                 }
98
99                 return 0;
100         }
101
102         rid = atoi(p);
103
104         sid_copy(&sid, &domain->sid);
105         sid_append_rid(&sid, rid);
106
107         sid_to_fstring(keystr, &sid);
108         key2 = string_term_tdb_data(keystr);
109
110         status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
111         if (!NT_STATUS_IS_OK(status)) {
112                 DEBUG(0,("Unable to add record %s:%s\n",
113                         (const char *)key2.dptr,
114                         nt_errstr(status)));
115                 s->failed = true;
116                 return -1;
117         }
118
119         status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
120         if (!NT_STATUS_IS_OK(status)) {
121                 DEBUG(0,("Unable to update record %s:%s\n",
122                         (const char *)rec->value.dptr,
123                         nt_errstr(status)));
124                 s->failed = true;
125                 return -1;
126         }
127
128         status = rec->delete_rec(rec);
129         if (!NT_STATUS_IS_OK(status)) {
130                 DEBUG(0,("Unable to delete record %s:%s\n",
131                         (const char *)rec->key.dptr,
132                         nt_errstr(status)));
133                 s->failed = true;
134                 return -1;
135         }
136
137         return 0;
138 }
139
140 /*****************************************************************************
141  Convert the idmap database from an older version.
142 *****************************************************************************/
143
144 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
145 {
146         int32 vers;
147         bool bigendianheader;
148         struct convert_fn_state s;
149
150         DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
151
152         bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
153
154         vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
155
156         if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
157                 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
158                 /*
159                  * high and low records were created on a
160                  * big endian machine and will need byte-reversing.
161                  */
162
163                 int32 wm;
164
165                 wm = dbwrap_fetch_int32(db, HWM_USER);
166
167                 if (wm != -1) {
168                         wm = IREV(wm);
169                 }  else {
170                         wm = dom->low_id;
171                 }
172
173                 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
174                         DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
175                         return False;
176                 }
177
178                 wm = dbwrap_fetch_int32(db, HWM_GROUP);
179                 if (wm != -1) {
180                         wm = IREV(wm);
181                 } else {
182                         wm = dom->low_id;
183                 }
184
185                 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
186                         DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
187                         return False;
188                 }
189         }
190
191         s.db = db;
192         s.failed = false;
193
194         /* the old format stored as DOMAIN/rid - now we store the SID direct */
195         db->traverse(db, convert_fn, &s);
196
197         if (s.failed) {
198                 DEBUG(0, ("Problem during conversion\n"));
199                 return False;
200         }
201
202         if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
203                 DEBUG(0, ("Unable to store idmap version in databse\n"));
204                 return False;
205         }
206
207         return True;
208 }
209
210 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
211 {
212         int ret;
213         uint32_t low_uid;
214         uint32_t low_gid;
215         bool update_uid = false;
216         bool update_gid = false;
217         struct idmap_tdb_context *ctx;
218
219         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
220
221         low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
222         if (low_uid == -1 || low_uid < dom->low_id) {
223                 update_uid = true;
224         }
225
226         low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
227         if (low_gid == -1 || low_gid < dom->low_id) {
228                 update_gid = true;
229         }
230
231         if (!update_uid && !update_gid) {
232                 return NT_STATUS_OK;
233         }
234
235         if (ctx->db->transaction_start(ctx->db) != 0) {
236                 DEBUG(0, ("Unable to start upgrade transaction!\n"));
237                 return NT_STATUS_INTERNAL_DB_ERROR;
238         }
239
240         if (update_uid) {
241                 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
242                 if (ret == -1) {
243                         ctx->db->transaction_cancel(ctx->db);
244                         DEBUG(0, ("Unable to initialise user hwm in idmap "
245                                   "database\n"));
246                         return NT_STATUS_INTERNAL_DB_ERROR;
247                 }
248         }
249
250         if (update_gid) {
251                 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
252                 if (ret == -1) {
253                         ctx->db->transaction_cancel(ctx->db);
254                         DEBUG(0, ("Unable to initialise group hwm in idmap "
255                                   "database\n"));
256                         return NT_STATUS_INTERNAL_DB_ERROR;
257                 }
258         }
259
260         if (ctx->db->transaction_commit(ctx->db) != 0) {
261                 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
262                 return NT_STATUS_INTERNAL_DB_ERROR;
263         }
264
265         return NT_STATUS_OK;
266 }
267
268 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
269 {
270         NTSTATUS ret;
271         TALLOC_CTX *mem_ctx;
272         char *tdbfile = NULL;
273         struct db_context *db = NULL;
274         int32_t version;
275         bool config_error = false;
276         struct idmap_tdb_context *ctx;
277
278         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
279
280         /* use our own context here */
281         mem_ctx = talloc_stackframe();
282
283         /* use the old database if present */
284         tdbfile = state_path("winbindd_idmap.tdb");
285         if (!tdbfile) {
286                 DEBUG(0, ("Out of memory!\n"));
287                 ret = NT_STATUS_NO_MEMORY;
288                 goto done;
289         }
290
291         DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
292
293         /* Open idmap repository */
294         db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
295         if (!db) {
296                 DEBUG(0, ("Unable to open idmap database\n"));
297                 ret = NT_STATUS_UNSUCCESSFUL;
298                 goto done;
299         }
300
301         /* check against earlier versions */
302         version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
303         if (version != IDMAP_VERSION) {
304                 if (config_error) {
305                         DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
306                                  "possible with incomplete configuration\n",
307                                  version, IDMAP_VERSION));
308                         ret = NT_STATUS_UNSUCCESSFUL;
309                         goto done;
310                 }
311                 if (db->transaction_start(db) != 0) {
312                         DEBUG(0, ("Unable to start upgrade transaction!\n"));
313                         ret = NT_STATUS_INTERNAL_DB_ERROR;
314                         goto done;
315                 }
316
317                 if (!idmap_tdb_upgrade(dom, db)) {
318                         db->transaction_cancel(db);
319                         DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
320                         ret = NT_STATUS_INTERNAL_DB_ERROR;
321                         goto done;
322                 }
323
324                 if (db->transaction_commit(db) != 0) {
325                         DEBUG(0, ("Unable to commit upgrade transaction!\n"));
326                         ret = NT_STATUS_INTERNAL_DB_ERROR;
327                         goto done;
328                 }
329         }
330
331         ctx->db = talloc_move(ctx, &db);
332
333         ret = idmap_tdb_init_hwm(dom);
334
335 done:
336         talloc_free(mem_ctx);
337         return ret;
338 }
339
340 /**********************************************************************
341  IDMAP ALLOC TDB BACKEND
342 **********************************************************************/
343  
344 static struct db_context *idmap_alloc_db;
345
346 /**********************************
347  Allocate a new id. 
348 **********************************/
349
350 struct idmap_tdb_allocate_id_context {
351         const char *hwmkey;
352         const char *hwmtype;
353         uint32_t high_hwm;
354         uint32_t hwm;
355 };
356
357 static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
358                                              void *private_data)
359 {
360         NTSTATUS ret;
361         struct idmap_tdb_allocate_id_context *state;
362         uint32_t hwm;
363
364         state = (struct idmap_tdb_allocate_id_context *)private_data;
365
366         hwm = dbwrap_fetch_int32(db, state->hwmkey);
367         if (hwm == -1) {
368                 ret = NT_STATUS_INTERNAL_DB_ERROR;
369                 goto done;
370         }
371
372         /* check it is in the range */
373         if (hwm > state->high_hwm) {
374                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
375                           state->hwmtype, (unsigned long)state->high_hwm));
376                 ret = NT_STATUS_UNSUCCESSFUL;
377                 goto done;
378         }
379
380         /* fetch a new id and increment it */
381         ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
382         if (!NT_STATUS_IS_OK(ret)) {
383                 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
384                           state->hwmtype, nt_errstr(ret)));
385                 goto done;
386         }
387
388         /* recheck it is in the range */
389         if (hwm > state->high_hwm) {
390                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
391                           state->hwmtype, (unsigned long)state->high_hwm));
392                 ret = NT_STATUS_UNSUCCESSFUL;
393                 goto done;
394         }
395
396         ret = NT_STATUS_OK;
397         state->hwm = hwm;
398
399 done:
400         return ret;
401 }
402
403 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
404 {
405         const char *hwmkey;
406         const char *hwmtype;
407         uint32_t high_hwm;
408         uint32_t hwm = 0;
409         NTSTATUS status;
410         struct idmap_tdb_allocate_id_context state;
411
412         /* Get current high water mark */
413         switch (xid->type) {
414
415         case ID_TYPE_UID:
416                 hwmkey = HWM_USER;
417                 hwmtype = "UID";
418                 high_hwm = idmap_tdb_state.high_uid;
419                 break;
420
421         case ID_TYPE_GID:
422                 hwmkey = HWM_GROUP;
423                 hwmtype = "GID";
424                 high_hwm = idmap_tdb_state.high_gid;
425                 break;
426
427         default:
428                 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
429                 return NT_STATUS_INVALID_PARAMETER;
430         }
431
432         state.hwm = hwm;
433         state.high_hwm = high_hwm;
434         state.hwmtype = hwmtype;
435         state.hwmkey = hwmkey;
436
437         status = dbwrap_trans_do(idmap_alloc_db, idmap_tdb_allocate_id_action,
438                                  &state);
439
440         if (NT_STATUS_IS_OK(status)) {
441                 xid->id = state.hwm;
442                 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
443         } else {
444                 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
445         }
446
447         return status;
448 }
449
450 /**
451  * Allocate a new unix-ID.
452  * For now this is for the default idmap domain only.
453  * Should be extended later on.
454  */
455 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
456                                      struct unixid *id)
457 {
458         NTSTATUS ret;
459
460         if (!strequal(dom->name, "*")) {
461                 DEBUG(3, ("idmap_tdb_get_new_id: "
462                           "Refusing allocation of a new unixid for domain'%s'. "
463                           "Currently only supported for the default "
464                           "domain \"*\".\n",
465                            dom->name));
466                 return NT_STATUS_NOT_IMPLEMENTED;
467         }
468
469         ret = idmap_tdb_allocate_id(id);
470
471         return ret;
472 }
473
474 /**********************************
475  Close the alloc tdb 
476 **********************************/
477
478 static NTSTATUS idmap_tdb_alloc_close(void)
479 {
480         TALLOC_FREE(idmap_alloc_db);
481         return NT_STATUS_OK;
482 }
483
484 /**********************************************************************
485  IDMAP MAPPING TDB BACKEND
486 **********************************************************************/
487
488 /*****************************
489  Initialise idmap database. 
490 *****************************/
491
492 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
493 {
494         NTSTATUS ret;
495         struct idmap_tdb_context *ctx;
496
497         ctx = talloc(dom, struct idmap_tdb_context);
498         if ( ! ctx) {
499                 DEBUG(0, ("Out of memory!\n"));
500                 return NT_STATUS_NO_MEMORY;
501         }
502
503         /* load backend specific configuration here: */
504 #if 0
505         if (strequal(dom->name, "*")) {
506         } else {
507         }
508 #endif
509
510         dom->private_data = ctx;
511
512         ret = idmap_tdb_open_db(dom);
513         if ( ! NT_STATUS_IS_OK(ret)) {
514                 goto failed;
515         }
516
517         return NT_STATUS_OK;
518
519 failed:
520         talloc_free(ctx);
521         return ret;
522 }
523
524 /**********************************
525  Single id to sid lookup function. 
526 **********************************/
527
528 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
529 {
530         NTSTATUS ret;
531         TDB_DATA data;
532         char *keystr;
533         struct idmap_tdb_context *ctx;
534
535         if (!dom || !map) {
536                 return NT_STATUS_INVALID_PARAMETER;
537         }
538
539         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
540
541         /* apply filters before checking */
542         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
543                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
544                                 map->xid.id, dom->low_id, dom->high_id));
545                 return NT_STATUS_NONE_MAPPED;
546         }
547
548         switch (map->xid.type) {
549
550         case ID_TYPE_UID:
551                 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
552                 break;
553                 
554         case ID_TYPE_GID:
555                 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
556                 break;
557
558         default:
559                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
560                 return NT_STATUS_INVALID_PARAMETER;
561         }
562
563         /* final SAFE_FREE safe */
564         data.dptr = NULL;
565
566         if (keystr == NULL) {
567                 DEBUG(0, ("Out of memory!\n"));
568                 ret = NT_STATUS_NO_MEMORY;
569                 goto done;
570         }
571
572         DEBUG(10,("Fetching record %s\n", keystr));
573
574         /* Check if the mapping exists */
575         data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
576
577         if (!data.dptr) {
578                 DEBUG(10,("Record %s not found\n", keystr));
579                 ret = NT_STATUS_NONE_MAPPED;
580                 goto done;
581         }
582                 
583         if (!string_to_sid(map->sid, (const char *)data.dptr)) {
584                 DEBUG(10,("INVALID SID (%s) in record %s\n",
585                         (const char *)data.dptr, keystr));
586                 ret = NT_STATUS_INTERNAL_DB_ERROR;
587                 goto done;
588         }
589
590         DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
591         ret = NT_STATUS_OK;
592
593 done:
594         talloc_free(data.dptr);
595         talloc_free(keystr);
596         return ret;
597 }
598
599 /**********************************
600  Single sid to id lookup function. 
601 **********************************/
602
603 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
604 {
605         NTSTATUS ret;
606         TDB_DATA data;
607         char *keystr;
608         unsigned long rec_id = 0;
609         struct idmap_tdb_context *ctx;
610         TALLOC_CTX *tmp_ctx = talloc_stackframe();
611
612         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
613
614         keystr = sid_string_talloc(tmp_ctx, map->sid);
615         if (keystr == NULL) {
616                 DEBUG(0, ("Out of memory!\n"));
617                 ret = NT_STATUS_NO_MEMORY;
618                 goto done;
619         }
620
621         DEBUG(10,("Fetching record %s\n", keystr));
622
623         /* Check if sid is present in database */
624         data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
625         if (!data.dptr) {
626                 DEBUG(10,("Record %s not found\n", keystr));
627                 ret = NT_STATUS_NONE_MAPPED;
628                 goto done;
629         }
630
631         /* What type of record is this ? */
632         if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
633                 map->xid.id = rec_id;
634                 map->xid.type = ID_TYPE_UID;
635                 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
636                 ret = NT_STATUS_OK;
637
638         } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
639                 map->xid.id = rec_id;
640                 map->xid.type = ID_TYPE_GID;
641                 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
642                 ret = NT_STATUS_OK;
643
644         } else { /* Unknown record type ! */
645                 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
646                 ret = NT_STATUS_INTERNAL_DB_ERROR;
647                 goto done;
648         }
649
650         /* apply filters before returning result */
651         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
652                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
653                                 map->xid.id, dom->low_id, dom->high_id));
654                 ret = NT_STATUS_NONE_MAPPED;
655         }
656
657 done:
658         talloc_free(tmp_ctx);
659         return ret;
660 }
661
662 /**********************************
663  lookup a set of unix ids. 
664 **********************************/
665
666 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
667 {
668         struct idmap_tdb_context *ctx;
669         NTSTATUS ret;
670         int i;
671
672         /* initialize the status to avoid suprise */
673         for (i = 0; ids[i]; i++) {
674                 ids[i]->status = ID_UNKNOWN;
675         }
676         
677         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
678
679         for (i = 0; ids[i]; i++) {
680                 ret = idmap_tdb_id_to_sid(dom, ids[i]);
681                 if ( ! NT_STATUS_IS_OK(ret)) {
682
683                         /* if it is just a failed mapping continue */
684                         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
685
686                                 /* make sure it is marked as unmapped */
687                                 ids[i]->status = ID_UNMAPPED;
688                                 continue;
689                         }
690                         
691                         /* some fatal error occurred, return immediately */
692                         goto done;
693                 }
694
695                 /* all ok, id is mapped */
696                 ids[i]->status = ID_MAPPED;
697         }
698
699         ret = NT_STATUS_OK;
700
701 done:
702         return ret;
703 }
704
705 /**********************************
706  lookup a set of sids. 
707 **********************************/
708
709 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
710 {
711         struct idmap_tdb_context *ctx;
712         NTSTATUS ret;
713         int i;
714
715         /* initialize the status to avoid suprise */
716         for (i = 0; ids[i]; i++) {
717                 ids[i]->status = ID_UNKNOWN;
718         }
719         
720         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
721
722         for (i = 0; ids[i]; i++) {
723                 ret = idmap_tdb_sid_to_id(dom, ids[i]);
724                 if ( ! NT_STATUS_IS_OK(ret)) {
725
726                         /* if it is just a failed mapping continue */
727                         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
728
729                                 /* make sure it is marked as unmapped */
730                                 ids[i]->status = ID_UNMAPPED;
731                                 continue;
732                         }
733                         
734                         /* some fatal error occurred, return immediately */
735                         goto done;
736                 }
737
738                 /* all ok, id is mapped */
739                 ids[i]->status = ID_MAPPED;
740         }
741
742         ret = NT_STATUS_OK;
743
744 done:
745         return ret;
746 }
747
748 /**********************************
749  set a mapping.
750 **********************************/
751
752 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
753                                       const struct id_map *map)
754 {
755         struct idmap_tdb_context *ctx;
756         NTSTATUS ret;
757         TDB_DATA ksid, kid;
758         char *ksidstr, *kidstr;
759         fstring tmp;
760
761         if (!map || !map->sid) {
762                 return NT_STATUS_INVALID_PARAMETER;
763         }
764
765         ksidstr = kidstr = NULL;
766
767         /* TODO: should we filter a set_mapping using low/high filters ? */
768
769         ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
770
771         switch (map->xid.type) {
772
773         case ID_TYPE_UID:
774                 kidstr = talloc_asprintf(ctx, "UID %lu",
775                                          (unsigned long)map->xid.id);
776                 break;
777
778         case ID_TYPE_GID:
779                 kidstr = talloc_asprintf(ctx, "GID %lu",
780                                          (unsigned long)map->xid.id);
781                 break;
782
783         default:
784                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
785                 return NT_STATUS_INVALID_PARAMETER;
786         }
787
788         if (kidstr == NULL) {
789                 DEBUG(0, ("ERROR: Out of memory!\n"));
790                 ret = NT_STATUS_NO_MEMORY;
791                 goto done;
792         }
793
794         if ((ksidstr = talloc_asprintf(
795                      ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
796                 DEBUG(0, ("Out of memory!\n"));
797                 ret = NT_STATUS_NO_MEMORY;
798                 goto done;
799         }
800
801         DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
802         kid = string_term_tdb_data(kidstr);
803         ksid = string_term_tdb_data(ksidstr);
804
805         if (ctx->db->transaction_start(ctx->db) != 0) {
806                 DEBUG(0, ("Failed to start transaction for %s\n",
807                           ksidstr));
808                 ret = NT_STATUS_INTERNAL_DB_ERROR;
809                 goto done;
810         }
811
812         ret = dbwrap_store(ctx->db, ksid, kid, TDB_REPLACE);
813         if (!NT_STATUS_IS_OK(ret)) {
814                 ctx->db->transaction_cancel(ctx->db);
815                 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
816                           ksidstr, kidstr, nt_errstr(ret)));
817                 goto done;
818         }
819         ret = dbwrap_store(ctx->db, kid, ksid, TDB_REPLACE);
820         if (!NT_STATUS_IS_OK(ret)) {
821                 ctx->db->transaction_cancel(ctx->db);
822                 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
823                           kidstr, ksidstr, nt_errstr(ret)));
824                 goto done;
825         }
826
827         if (ctx->db->transaction_commit(ctx->db) != 0) {
828                 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
829                           ksidstr, kidstr));
830                 ret = NT_STATUS_INTERNAL_DB_ERROR;
831                 goto done;
832         }
833
834         DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
835         ret = NT_STATUS_OK;
836
837 done:
838         talloc_free(ksidstr);
839         talloc_free(kidstr);
840         return ret;
841 }
842
843 /**********************************
844  Close the idmap tdb instance
845 **********************************/
846
847 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
848 {
849         struct idmap_tdb_context *ctx;
850
851         if (dom->private_data) {
852                 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
853
854                 TALLOC_FREE(ctx->db);
855         }
856         return NT_STATUS_OK;
857 }
858
859 static struct idmap_methods db_methods = {
860         .init = idmap_tdb_db_init,
861         .unixids_to_sids = idmap_tdb_unixids_to_sids,
862         .sids_to_unixids = idmap_tdb_sids_to_unixids,
863         .allocate_id = idmap_tdb_get_new_id,
864         .close_fn = idmap_tdb_close
865 };
866
867 NTSTATUS idmap_tdb_init(void)
868 {
869         DEBUG(10, ("calling idmap_tdb_init\n"));
870
871         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);
872 }