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