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