s3:idmap_tdb2: fix build of tdb2
[samba.git] / source3 / winbindd / idmap_tdb2.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    idmap TDB2 backend, used for clustered Samba setups.
5
6    This uses dbwrap to access tdb files. The location can be set
7    using tdb:idmap2.tdb =" in smb.conf
8
9    Copyright (C) Andrew Tridgell 2007
10
11    This is heavily based upon idmap_tdb.c, which is:
12
13    Copyright (C) Tim Potter 2000
14    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
15    Copyright (C) Jeremy Allison 2006
16    Copyright (C) Simo Sorce 2003-2006
17    Copyright (C) Michael Adam 2009-2010
18
19    This program is free software; you can redistribute it and/or modify
20    it under the terms of the GNU General Public License as published by
21    the Free Software Foundation; either version 2 of the License, or
22    (at your option) any later version.
23
24    This program is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27    GNU General Public License for more details.
28
29    You should have received a copy of the GNU General Public License
30    along with this program; if not, write to the Free Software
31    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33
34 #include "includes.h"
35 #include "system/filesys.h"
36 #include "winbindd.h"
37 #include "idmap.h"
38 #include "idmap_rw.h"
39 #include "dbwrap.h"
40 #include "../libcli/security/dom_sid.h"
41
42 #undef DBGC_CLASS
43 #define DBGC_CLASS DBGC_IDMAP
44
45 struct idmap_tdb2_context {
46         struct db_context *db;
47         const char *script; /* script to provide idmaps */
48         struct idmap_rw_ops *rw_ops;
49 };
50
51 /* High water mark keys */
52 #define HWM_GROUP  "GROUP HWM"
53 #define HWM_USER   "USER HWM"
54
55
56 /*
57  * check and initialize high/low water marks in the db
58  */
59 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
60 {
61         NTSTATUS status;
62         uint32 low_id;
63         struct idmap_tdb2_context *ctx;
64
65         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
66
67         /* Create high water marks for group and user id */
68
69         low_id = dbwrap_fetch_int32(ctx->db, HWM_USER);
70         if ((low_id == -1) || (low_id < dom->low_id)) {
71                 status = dbwrap_trans_store_int32(ctx->db, HWM_USER,
72                                                   dom->low_id);
73                 if (!NT_STATUS_IS_OK(status)) {
74                         DEBUG(0, ("Unable to initialise user hwm in idmap "
75                                   "database: %s\n", nt_errstr(status)));
76                         return NT_STATUS_INTERNAL_DB_ERROR;
77                 }
78         }
79
80         low_id = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
81         if ((low_id == -1) || (low_id < dom->low_id)) {
82                 status = dbwrap_trans_store_int32(ctx->db, HWM_GROUP,
83                                                   dom->low_id);
84                 if (!NT_STATUS_IS_OK(status)) {
85                         DEBUG(0, ("Unable to initialise group hwm in idmap "
86                                   "database: %s\n", nt_errstr(status)));
87                         return NT_STATUS_INTERNAL_DB_ERROR;
88                 }
89         }
90
91         return NT_STATUS_OK;
92 }
93
94
95 /*
96   open the permanent tdb
97  */
98 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
99 {
100         char *db_path;
101         struct idmap_tdb2_context *ctx;
102
103         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
104
105         if (ctx->db) {
106                 /* its already open */
107                 return NT_STATUS_OK;
108         }
109
110         db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
111         if (db_path == NULL) {
112                 /* fall back to the private directory, which, despite
113                    its name, is usually on shared storage */
114                 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
115         }
116         NT_STATUS_HAVE_NO_MEMORY(db_path);
117
118         /* Open idmap repository */
119         ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
120         TALLOC_FREE(db_path);
121
122         if (ctx->db == NULL) {
123                 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
124                           db_path));
125                 return NT_STATUS_UNSUCCESSFUL;
126         }
127
128         return idmap_tdb2_init_hwm(dom);
129 }
130
131
132 /*
133   Allocate a new id. 
134 */
135
136 struct idmap_tdb2_allocate_id_context {
137         const char *hwmkey;
138         const char *hwmtype;
139         uint32_t high_hwm;
140         uint32_t hwm;
141 };
142
143 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
144                                               void *private_data)
145 {
146         NTSTATUS ret;
147         struct idmap_tdb2_allocate_id_context *state;
148         uint32_t hwm;
149
150         state = (struct idmap_tdb2_allocate_id_context *)private_data;
151
152         hwm = dbwrap_fetch_int32(db, state->hwmkey);
153         if (hwm == -1) {
154                 ret = NT_STATUS_INTERNAL_DB_ERROR;
155                 goto done;
156         }
157
158         /* check it is in the range */
159         if (hwm > state->high_hwm) {
160                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
161                           state->hwmtype, (unsigned long)state->high_hwm));
162                 ret = NT_STATUS_UNSUCCESSFUL;
163                 goto done;
164         }
165
166         /* fetch a new id and increment it */
167         ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
168         if (!NT_STATUS_IS_OK(ret)) {
169                 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
170                           state->hwmtype));
171                 goto done;
172         }
173
174         /* recheck it is in the range */
175         if (hwm > state->high_hwm) {
176                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
177                           state->hwmtype, (unsigned long)state->high_hwm));
178                 ret = NT_STATUS_UNSUCCESSFUL;
179                 goto done;
180         }
181
182         ret = NT_STATUS_OK;
183         state->hwm = hwm;
184
185 done:
186         return ret;
187 }
188
189 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
190                                        struct unixid *xid)
191 {
192         const char *hwmkey;
193         const char *hwmtype;
194         uint32_t high_hwm;
195         uint32_t hwm = 0;
196         NTSTATUS status;
197         struct idmap_tdb2_allocate_id_context state;
198         struct idmap_tdb2_context *ctx;
199
200         status = idmap_tdb2_open_db(dom);
201         NT_STATUS_NOT_OK_RETURN(status);
202
203         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
204
205         /* Get current high water mark */
206         switch (xid->type) {
207
208         case ID_TYPE_UID:
209                 hwmkey = HWM_USER;
210                 hwmtype = "UID";
211                 break;
212
213         case ID_TYPE_GID:
214                 hwmkey = HWM_GROUP;
215                 hwmtype = "GID";
216                 break;
217
218         default:
219                 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
220                 return NT_STATUS_INVALID_PARAMETER;
221         }
222
223         high_hwm = dom->high_id;
224
225         state.hwm = hwm;
226         state.high_hwm = high_hwm;
227         state.hwmtype = hwmtype;
228         state.hwmkey = hwmkey;
229
230         status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
231                                  &state);
232
233         if (NT_STATUS_IS_OK(status)) {
234                 xid->id = state.hwm;
235                 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
236         } else {
237                 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
238         }
239
240         return status;
241 }
242
243 /**
244  * Allocate a new unix-ID.
245  * For now this is for the default idmap domain only.
246  * Should be extended later on.
247  */
248 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
249                                       struct unixid *id)
250 {
251         NTSTATUS ret;
252
253         if (!strequal(dom->name, "*")) {
254                 DEBUG(3, ("idmap_tdb2_get_new_id: "
255                           "Refusing creation of mapping for domain'%s'. "
256                           "Currently only supported for the default "
257                           "domain \"*\".\n",
258                            dom->name));
259                 return NT_STATUS_NOT_IMPLEMENTED;
260         }
261
262         ret = idmap_tdb2_allocate_id(dom, id);
263
264         return ret;
265 }
266
267 /*
268   IDMAP MAPPING TDB BACKEND
269 */
270
271 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom,
272                                        const struct id_map *map);
273
274 /*
275   Initialise idmap database. 
276 */
277 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom)
278 {
279         NTSTATUS ret;
280         struct idmap_tdb2_context *ctx;
281
282         ctx = talloc_zero(dom, struct idmap_tdb2_context);
283         if ( ! ctx) {
284                 DEBUG(0, ("Out of memory!\n"));
285                 return NT_STATUS_NO_MEMORY;
286         }
287
288         if (strequal(dom->name, "*")) {
289                 ctx->script = lp_parm_const_string(-1, "idmap", "script", NULL);
290                 if (ctx->script) {
291                         DEBUG(1, ("using idmap script '%s'\n", ctx->script));
292                 }
293         } else {
294                 char *config_option = NULL;
295
296                 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
297                 if ( ! config_option) {
298                         DEBUG(0, ("Out of memory!\n"));
299                         ret = NT_STATUS_NO_MEMORY;
300                         goto failed;
301                 }
302
303                 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
304                 if (ctx->script) {
305                         DEBUG(1, ("using idmap script '%s'\n", ctx->script));
306                 }
307
308                 talloc_free(config_option);
309         }
310
311         ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
312         if (ctx->rw_ops == NULL) {
313                 DEBUG(0, ("Out of memory!\n"));
314                 ret = NT_STATUS_NO_MEMORY;
315                 goto failed;
316         }
317
318         ctx->rw_ops->get_new_id = idmap_tdb2_get_new_id;
319         ctx->rw_ops->set_mapping = idmap_tdb2_set_mapping;
320
321         dom->private_data = ctx;
322
323         ret = idmap_tdb2_open_db(dom);
324         if (!NT_STATUS_IS_OK(ret)) {
325                 goto failed;
326         }
327
328         return NT_STATUS_OK;
329
330 failed:
331         talloc_free(ctx);
332         return ret;
333 }
334
335
336 /**
337  * store a mapping in the database.
338  */
339
340 struct idmap_tdb2_set_mapping_context {
341         const char *ksidstr;
342         const char *kidstr;
343 };
344
345 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
346                                               void *private_data)
347 {
348         TDB_DATA data;
349         NTSTATUS ret;
350         struct idmap_tdb2_set_mapping_context *state;
351         TALLOC_CTX *tmp_ctx = talloc_stackframe();
352
353         state = (struct idmap_tdb2_set_mapping_context *)private_data;
354
355         DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
356
357         /* check wheter sid mapping is already present in db */
358         data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
359         if (data.dptr) {
360                 ret = NT_STATUS_OBJECT_NAME_COLLISION;
361                 goto done;
362         }
363
364         ret = dbwrap_store_bystring(db, state->ksidstr,
365                                     string_term_tdb_data(state->kidstr),
366                                     TDB_INSERT);
367         if (!NT_STATUS_IS_OK(ret)) {
368                 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
369                 goto done;
370         }
371
372         ret = dbwrap_store_bystring(db, state->kidstr,
373                                     string_term_tdb_data(state->ksidstr),
374                                     TDB_INSERT);
375         if (!NT_STATUS_IS_OK(ret)) {
376                 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
377                 /* try to remove the previous stored SID -> ID map */
378                 dbwrap_delete_bystring(db, state->ksidstr);
379                 goto done;
380         }
381
382         DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
383
384 done:
385         talloc_free(tmp_ctx);
386         return ret;
387 }
388
389 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
390 {
391         struct idmap_tdb2_context *ctx;
392         NTSTATUS ret;
393         char *ksidstr, *kidstr;
394         struct idmap_tdb2_set_mapping_context state;
395
396         if (!map || !map->sid) {
397                 return NT_STATUS_INVALID_PARAMETER;
398         }
399
400         ksidstr = kidstr = NULL;
401
402         /* TODO: should we filter a set_mapping using low/high filters ? */
403
404         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
405
406         switch (map->xid.type) {
407
408         case ID_TYPE_UID:
409                 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
410                 break;
411
412         case ID_TYPE_GID:
413                 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
414                 break;
415
416         default:
417                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
418                 return NT_STATUS_INVALID_PARAMETER;
419         }
420
421         if (kidstr == NULL) {
422                 DEBUG(0, ("ERROR: Out of memory!\n"));
423                 ret = NT_STATUS_NO_MEMORY;
424                 goto done;
425         }
426
427         ksidstr = sid_string_talloc(ctx, map->sid);
428         if (ksidstr == NULL) {
429                 DEBUG(0, ("Out of memory!\n"));
430                 ret = NT_STATUS_NO_MEMORY;
431                 goto done;
432         }
433
434         state.ksidstr = ksidstr;
435         state.kidstr = kidstr;
436
437         ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
438                               &state);
439
440 done:
441         talloc_free(ksidstr);
442         talloc_free(kidstr);
443         return ret;
444 }
445
446 /**
447  * Create a new mapping for an unmapped SID, also allocating a new ID.
448  * This should be run inside a transaction.
449  *
450  * TODO:
451 *  Properly integrate this with multi domain idmap config:
452  * Currently, the allocator is default-config only.
453  */
454 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
455 {
456         NTSTATUS ret;
457         struct idmap_tdb2_context *ctx;
458
459         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
460
461         ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
462
463         return ret;
464 }
465
466
467 /*
468   run a script to perform a mapping
469
470   The script should the following command lines:
471
472       SIDTOID S-1-xxxx
473       IDTOSID UID xxxx
474       IDTOSID GID xxxx
475
476   and should return one of the following as a single line of text
477      UID:xxxx
478      GID:xxxx
479      SID:xxxx
480      ERR:xxxx
481  */
482 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
483                                   const char *fmt, ...)
484 {
485         va_list ap;
486         char *cmd;
487         FILE *p;
488         char line[64];
489         unsigned long v;
490
491         cmd = talloc_asprintf(ctx, "%s ", ctx->script);
492         NT_STATUS_HAVE_NO_MEMORY(cmd);  
493
494         va_start(ap, fmt);
495         cmd = talloc_vasprintf_append(cmd, fmt, ap);
496         va_end(ap);
497         NT_STATUS_HAVE_NO_MEMORY(cmd);
498
499         p = popen(cmd, "r");
500         talloc_free(cmd);
501         if (p == NULL) {
502                 return NT_STATUS_NONE_MAPPED;
503         }
504
505         if (fgets(line, sizeof(line)-1, p) == NULL) {
506                 pclose(p);
507                 return NT_STATUS_NONE_MAPPED;
508         }
509         pclose(p);
510
511         DEBUG(10,("idmap script gave: %s\n", line));
512
513         if (sscanf(line, "UID:%lu", &v) == 1) {
514                 map->xid.id   = v;
515                 map->xid.type = ID_TYPE_UID;
516         } else if (sscanf(line, "GID:%lu", &v) == 1) {
517                 map->xid.id   = v;
518                 map->xid.type = ID_TYPE_GID;            
519         } else if (strncmp(line, "SID:S-", 6) == 0) {
520                 if (!string_to_sid(map->sid, &line[4])) {
521                         DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
522                                  line, ctx->script));
523                         return NT_STATUS_NONE_MAPPED;                   
524                 }
525         } else {
526                 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
527                          line, ctx->script));
528                 return NT_STATUS_NONE_MAPPED;
529         }
530
531         return NT_STATUS_OK;
532 }
533
534
535
536 /*
537   Single id to sid lookup function. 
538 */
539 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
540 {
541         NTSTATUS ret;
542         TDB_DATA data;
543         char *keystr;
544         NTSTATUS status;
545         struct idmap_tdb2_context *ctx;
546
547
548         if (!dom || !map) {
549                 return NT_STATUS_INVALID_PARAMETER;
550         }
551
552         status = idmap_tdb2_open_db(dom);
553         NT_STATUS_NOT_OK_RETURN(status);
554
555         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
556
557         /* apply filters before checking */
558         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
559                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
560                                 map->xid.id, dom->low_id, dom->high_id));
561                 return NT_STATUS_NONE_MAPPED;
562         }
563
564         switch (map->xid.type) {
565
566         case ID_TYPE_UID:
567                 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
568                 break;
569
570         case ID_TYPE_GID:
571                 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
572                 break;
573
574         default:
575                 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
576                 return NT_STATUS_INVALID_PARAMETER;
577         }
578
579         /* final SAFE_FREE safe */
580         data.dptr = NULL;
581
582         if (keystr == NULL) {
583                 DEBUG(0, ("Out of memory!\n"));
584                 ret = NT_STATUS_NO_MEMORY;
585                 goto done;
586         }
587
588         DEBUG(10,("Fetching record %s\n", keystr));
589
590         /* Check if the mapping exists */
591         data = dbwrap_fetch_bystring(ctx->db, keystr, keystr);
592
593         if (!data.dptr) {
594                 char *sidstr;
595                 struct idmap_tdb2_set_mapping_context store_state;
596
597                 DEBUG(10,("Record %s not found\n", keystr));
598                 if (ctx->script == NULL) {
599                         ret = NT_STATUS_NONE_MAPPED;
600                         goto done;
601                 }
602
603                 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
604
605                 /* store it on shared storage */
606                 if (!NT_STATUS_IS_OK(ret)) {
607                         goto done;
608                 }
609
610                 sidstr = sid_string_talloc(keystr, map->sid);
611                 if (!sidstr) {
612                         ret = NT_STATUS_NO_MEMORY;
613                         goto done;
614                 }
615
616                 store_state.ksidstr = sidstr;
617                 store_state.kidstr = keystr;
618
619                 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
620                                       &store_state);
621                 goto done;
622         }
623
624         if (!string_to_sid(map->sid, (const char *)data.dptr)) {
625                 DEBUG(10,("INVALID SID (%s) in record %s\n",
626                         (const char *)data.dptr, keystr));
627                 ret = NT_STATUS_INTERNAL_DB_ERROR;
628                 goto done;
629         }
630
631         DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
632         ret = NT_STATUS_OK;
633
634 done:
635         talloc_free(keystr);
636         return ret;
637 }
638
639
640 /*
641  Single sid to id lookup function. 
642 */
643 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
644 {
645         NTSTATUS ret;
646         TDB_DATA data;
647         char *keystr;
648         unsigned long rec_id = 0;
649         struct idmap_tdb2_context *ctx;
650         TALLOC_CTX *tmp_ctx = talloc_stackframe();
651
652         ret = idmap_tdb2_open_db(dom);
653         NT_STATUS_NOT_OK_RETURN(ret);
654
655         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
656
657         keystr = sid_string_talloc(tmp_ctx, map->sid);
658         if (keystr == NULL) {
659                 DEBUG(0, ("Out of memory!\n"));
660                 ret = NT_STATUS_NO_MEMORY;
661                 goto done;
662         }
663
664         DEBUG(10,("Fetching record %s\n", keystr));
665
666         /* Check if sid is present in database */
667         data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
668         if (!data.dptr) {
669                 char *idstr;
670                 struct idmap_tdb2_set_mapping_context store_state;
671
672                 DEBUG(10,(__location__ " Record %s not found\n", keystr));
673
674                 if (ctx->script == NULL) {
675                         ret = NT_STATUS_NONE_MAPPED;
676                         goto done;
677                 }
678
679                 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
680                 /* store it on shared storage */
681                 if (!NT_STATUS_IS_OK(ret)) {
682                         goto done;
683                 }
684
685                 /* apply filters before returning result */
686                 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
687                         DEBUG(5, ("Script returned id (%u) out of range "
688                                   "(%u - %u). Filtered!\n",
689                                   map->xid.id, dom->low_id, dom->high_id));
690                         ret = NT_STATUS_NONE_MAPPED;
691                         goto done;
692                 }
693
694                 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
695                                         map->xid.type == ID_TYPE_UID?'U':'G',
696                                         (unsigned long)map->xid.id);
697                 if (idstr == NULL) {
698                         ret = NT_STATUS_NO_MEMORY;
699                         goto done;
700                 }
701
702                 store_state.ksidstr = keystr;
703                 store_state.kidstr = idstr;
704
705                 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
706                                       &store_state);
707                 goto done;
708         }
709
710         /* What type of record is this ? */
711         if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
712                 map->xid.id = rec_id;
713                 map->xid.type = ID_TYPE_UID;
714                 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
715                 ret = NT_STATUS_OK;
716
717         } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
718                 map->xid.id = rec_id;
719                 map->xid.type = ID_TYPE_GID;
720                 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
721                 ret = NT_STATUS_OK;
722
723         } else { /* Unknown record type ! */
724                 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
725                 ret = NT_STATUS_INTERNAL_DB_ERROR;
726                 goto done;
727         }
728
729         /* apply filters before returning result */
730         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
731                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
732                                 map->xid.id, dom->low_id, dom->high_id));
733                 ret = NT_STATUS_NONE_MAPPED;
734         }
735
736 done:
737         talloc_free(tmp_ctx);
738         return ret;
739 }
740
741 /*
742   lookup a set of unix ids. 
743 */
744 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
745 {
746         NTSTATUS ret;
747         int i;
748
749         /* initialize the status to avoid suprise */
750         for (i = 0; ids[i]; i++) {
751                 ids[i]->status = ID_UNKNOWN;
752         }
753
754         for (i = 0; ids[i]; i++) {
755                 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
756                 if ( ! NT_STATUS_IS_OK(ret)) {
757
758                         /* if it is just a failed mapping continue */
759                         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
760
761                                 /* make sure it is marked as unmapped */
762                                 ids[i]->status = ID_UNMAPPED;
763                                 continue;
764                         }
765
766                         /* some fatal error occurred, return immediately */
767                         goto done;
768                 }
769
770                 /* all ok, id is mapped */
771                 ids[i]->status = ID_MAPPED;
772         }
773
774         ret = NT_STATUS_OK;
775
776 done:
777         return ret;
778 }
779
780 /*
781   lookup a set of sids. 
782 */
783
784 struct idmap_tdb2_sids_to_unixids_context {
785         struct idmap_domain *dom;
786         struct id_map **ids;
787         bool allocate_unmapped;
788 };
789
790 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
791                                                   void *private_data)
792 {
793         struct idmap_tdb2_sids_to_unixids_context *state;
794         int i;
795         NTSTATUS ret = NT_STATUS_OK;
796
797         state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
798
799         DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
800                    " domain: [%s], allocate: %s\n",
801                    state->dom->name,
802                    state->allocate_unmapped ? "yes" : "no"));
803
804         for (i = 0; state->ids[i]; i++) {
805                 if ((state->ids[i]->status == ID_UNKNOWN) ||
806                     /* retry if we could not map in previous run: */
807                     (state->ids[i]->status == ID_UNMAPPED))
808                 {
809                         NTSTATUS ret2;
810
811                         ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
812                         if (!NT_STATUS_IS_OK(ret2)) {
813
814                                 /* if it is just a failed mapping, continue */
815                                 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
816
817                                         /* make sure it is marked as unmapped */
818                                         state->ids[i]->status = ID_UNMAPPED;
819                                         ret = STATUS_SOME_UNMAPPED;
820                                 } else {
821                                         /* some fatal error occurred, return immediately */
822                                         ret = ret2;
823                                         goto done;
824                                 }
825                         } else {
826                                 /* all ok, id is mapped */
827                                 state->ids[i]->status = ID_MAPPED;
828                         }
829                 }
830
831                 if ((state->ids[i]->status == ID_UNMAPPED) &&
832                     state->allocate_unmapped)
833                 {
834                         ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
835                         if (!NT_STATUS_IS_OK(ret)) {
836                                 goto done;
837                         }
838                 }
839         }
840
841 done:
842         return ret;
843 }
844
845 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
846 {
847         NTSTATUS ret;
848         int i;
849         struct idmap_tdb2_sids_to_unixids_context state;
850         struct idmap_tdb2_context *ctx;
851
852         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
853
854         /* initialize the status to avoid suprise */
855         for (i = 0; ids[i]; i++) {
856                 ids[i]->status = ID_UNKNOWN;
857         }
858
859         state.dom = dom;
860         state.ids = ids;
861         state.allocate_unmapped = false;
862
863         ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
864
865         if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
866                 state.allocate_unmapped = true;
867                 ret = dbwrap_trans_do(ctx->db,
868                                       idmap_tdb2_sids_to_unixids_action,
869                                       &state);
870         }
871
872         return ret;
873 }
874
875
876 static struct idmap_methods db_methods = {
877         .init            = idmap_tdb2_db_init,
878         .unixids_to_sids = idmap_tdb2_unixids_to_sids,
879         .sids_to_unixids = idmap_tdb2_sids_to_unixids,
880         .allocate_id     = idmap_tdb2_get_new_id
881 };
882
883 NTSTATUS idmap_tdb2_init(void)
884 {
885         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);
886 }