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