Revert making public of the samba-module library.
[garming/samba-autobuild/.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/dbwrap.h"
40 #include "dbwrap/dbwrap_open.h"
41 #include "../libcli/security/dom_sid.h"
42 #include "util_tdb.h"
43
44 #undef DBGC_CLASS
45 #define DBGC_CLASS DBGC_IDMAP
46
47 struct idmap_tdb2_context {
48         struct db_context *db;
49         const char *script; /* script to provide idmaps */
50         struct idmap_rw_ops *rw_ops;
51 };
52
53 /* High water mark keys */
54 #define HWM_GROUP  "GROUP HWM"
55 #define HWM_USER   "USER HWM"
56
57
58 /*
59  * check and initialize high/low water marks in the db
60  */
61 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
62 {
63         NTSTATUS status;
64         uint32 low_id;
65         struct idmap_tdb2_context *ctx;
66
67         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
68
69         /* Create high water marks for group and user id */
70
71         status = dbwrap_fetch_uint32(ctx->db, HWM_USER, &low_id);
72         if (!NT_STATUS_IS_OK(status) || (low_id < dom->low_id)) {
73                 status = dbwrap_trans_store_uint32(ctx->db, HWM_USER,
74                                                    dom->low_id);
75                 if (!NT_STATUS_IS_OK(status)) {
76                         DEBUG(0, ("Unable to initialise user hwm in idmap "
77                                   "database: %s\n", nt_errstr(status)));
78                         return NT_STATUS_INTERNAL_DB_ERROR;
79                 }
80         }
81
82         status = dbwrap_fetch_uint32(ctx->db, HWM_GROUP, &low_id);
83         if (!NT_STATUS_IS_OK(status) || (low_id < dom->low_id)) {
84                 status = dbwrap_trans_store_uint32(ctx->db, HWM_GROUP,
85                                                    dom->low_id);
86                 if (!NT_STATUS_IS_OK(status)) {
87                         DEBUG(0, ("Unable to initialise group hwm in idmap "
88                                   "database: %s\n", nt_errstr(status)));
89                         return NT_STATUS_INTERNAL_DB_ERROR;
90                 }
91         }
92
93         return NT_STATUS_OK;
94 }
95
96
97 /*
98   open the permanent tdb
99  */
100 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
101 {
102         char *db_path;
103         struct idmap_tdb2_context *ctx;
104
105         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
106
107         if (ctx->db) {
108                 /* its already open */
109                 return NT_STATUS_OK;
110         }
111
112         db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
113         NT_STATUS_HAVE_NO_MEMORY(db_path);
114
115         /* Open idmap repository */
116         ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
117         TALLOC_FREE(db_path);
118
119         if (ctx->db == NULL) {
120                 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
121                           db_path));
122                 return NT_STATUS_UNSUCCESSFUL;
123         }
124
125         return idmap_tdb2_init_hwm(dom);
126 }
127
128
129 /*
130   Allocate a new id. 
131 */
132
133 struct idmap_tdb2_allocate_id_context {
134         const char *hwmkey;
135         const char *hwmtype;
136         uint32_t high_hwm;
137         uint32_t hwm;
138 };
139
140 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
141                                               void *private_data)
142 {
143         NTSTATUS ret;
144         struct idmap_tdb2_allocate_id_context *state;
145         uint32_t hwm;
146
147         state = (struct idmap_tdb2_allocate_id_context *)private_data;
148
149         ret = dbwrap_fetch_uint32(db, state->hwmkey, &hwm);
150         if (!NT_STATUS_IS_OK(ret)) {
151                 ret = NT_STATUS_INTERNAL_DB_ERROR;
152                 goto done;
153         }
154
155         /* check it is in the range */
156         if (hwm > state->high_hwm) {
157                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
158                           state->hwmtype, (unsigned long)state->high_hwm));
159                 ret = NT_STATUS_UNSUCCESSFUL;
160                 goto done;
161         }
162
163         /* fetch a new id and increment it */
164         ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
165         if (!NT_STATUS_IS_OK(ret)) {
166                 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
167                           state->hwmtype));
168                 goto done;
169         }
170
171         /* recheck it is in the range */
172         if (hwm > state->high_hwm) {
173                 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
174                           state->hwmtype, (unsigned long)state->high_hwm));
175                 ret = NT_STATUS_UNSUCCESSFUL;
176                 goto done;
177         }
178
179         ret = NT_STATUS_OK;
180         state->hwm = hwm;
181
182 done:
183         return ret;
184 }
185
186 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
187                                        struct unixid *xid)
188 {
189         const char *hwmkey;
190         const char *hwmtype;
191         uint32_t high_hwm;
192         uint32_t hwm = 0;
193         NTSTATUS status;
194         struct idmap_tdb2_allocate_id_context state;
195         struct idmap_tdb2_context *ctx;
196
197         status = idmap_tdb2_open_db(dom);
198         NT_STATUS_NOT_OK_RETURN(status);
199
200         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
201
202         /* Get current high water mark */
203         switch (xid->type) {
204
205         case ID_TYPE_UID:
206                 hwmkey = HWM_USER;
207                 hwmtype = "UID";
208                 break;
209
210         case ID_TYPE_GID:
211                 hwmkey = HWM_GROUP;
212                 hwmtype = "GID";
213                 break;
214
215         default:
216                 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
217                 return NT_STATUS_INVALID_PARAMETER;
218         }
219
220         high_hwm = dom->high_id;
221
222         state.hwm = hwm;
223         state.high_hwm = high_hwm;
224         state.hwmtype = hwmtype;
225         state.hwmkey = hwmkey;
226
227         status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
228                                  &state);
229
230         if (NT_STATUS_IS_OK(status)) {
231                 xid->id = state.hwm;
232                 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
233         } else {
234                 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
235         }
236
237         return status;
238 }
239
240 /**
241  * Allocate a new unix-ID.
242  * For now this is for the default idmap domain only.
243  * Should be extended later on.
244  */
245 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
246                                       struct unixid *id)
247 {
248         NTSTATUS ret;
249
250         if (!strequal(dom->name, "*")) {
251                 DEBUG(3, ("idmap_tdb2_get_new_id: "
252                           "Refusing creation of mapping for domain'%s'. "
253                           "Currently only supported for the default "
254                           "domain \"*\".\n",
255                            dom->name));
256                 return NT_STATUS_NOT_IMPLEMENTED;
257         }
258
259         ret = idmap_tdb2_allocate_id(dom, id);
260
261         return ret;
262 }
263
264 /*
265   IDMAP MAPPING TDB BACKEND
266 */
267
268 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom,
269                                        const struct id_map *map);
270
271 /*
272   Initialise idmap database. 
273 */
274 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom)
275 {
276         NTSTATUS ret;
277         struct idmap_tdb2_context *ctx;
278         char *config_option = NULL;
279         const char * idmap_script = NULL;
280
281         ctx = talloc_zero(dom, struct idmap_tdb2_context);
282         if ( ! ctx) {
283                 DEBUG(0, ("Out of memory!\n"));
284                 return NT_STATUS_NO_MEMORY;
285         }
286
287         config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
288         if (config_option == NULL) {
289                 DEBUG(0, ("Out of memory!\n"));
290                 ret = NT_STATUS_NO_MEMORY;
291                 goto failed;
292         }
293         ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
294         talloc_free(config_option);
295
296         idmap_script = lp_parm_const_string(-1, "idmap", "script", NULL);
297         if (idmap_script != NULL) {
298                 DEBUG(0, ("Warning: 'idmap:script' is deprecated. "
299                           " Please use 'idmap config * : script' instead!\n"));
300         }
301
302         if (strequal(dom->name, "*") && ctx->script == NULL) {
303                 /* fall back to idmap:script for backwards compatibility */
304                 ctx->script = idmap_script;
305         }
306
307         if (ctx->script) {
308                 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
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         ret = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr, &data);
359         if (!NT_STATUS_IS_OK(ret)) {
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         if (keystr == NULL) {
580                 DEBUG(0, ("Out of memory!\n"));
581                 ret = NT_STATUS_NO_MEMORY;
582                 goto done;
583         }
584
585         DEBUG(10,("Fetching record %s\n", keystr));
586
587         /* Check if the mapping exists */
588         status = dbwrap_fetch_bystring(ctx->db, keystr, keystr, &data);
589
590         if (!NT_STATUS_IS_OK(status)) {
591                 char *sidstr;
592                 struct idmap_tdb2_set_mapping_context store_state;
593
594                 DEBUG(10,("Record %s not found\n", keystr));
595                 if (ctx->script == NULL) {
596                         ret = NT_STATUS_NONE_MAPPED;
597                         goto done;
598                 }
599
600                 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
601                 if (!NT_STATUS_IS_OK(ret)) {
602                         goto done;
603                 }
604
605                 sidstr = sid_string_talloc(keystr, map->sid);
606                 if (!sidstr) {
607                         ret = NT_STATUS_NO_MEMORY;
608                         goto done;
609                 }
610
611                 store_state.ksidstr = sidstr;
612                 store_state.kidstr = keystr;
613
614                 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
615                                       &store_state);
616                 goto done;
617         }
618
619         if (!string_to_sid(map->sid, (const char *)data.dptr)) {
620                 DEBUG(10,("INVALID SID (%s) in record %s\n",
621                         (const char *)data.dptr, keystr));
622                 ret = NT_STATUS_INTERNAL_DB_ERROR;
623                 goto done;
624         }
625
626         DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
627         ret = NT_STATUS_OK;
628
629 done:
630         talloc_free(keystr);
631         return ret;
632 }
633
634
635 /*
636  Single sid to id lookup function. 
637 */
638 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
639 {
640         NTSTATUS ret;
641         TDB_DATA data;
642         char *keystr;
643         unsigned long rec_id = 0;
644         struct idmap_tdb2_context *ctx;
645         TALLOC_CTX *tmp_ctx = talloc_stackframe();
646
647         ret = idmap_tdb2_open_db(dom);
648         NT_STATUS_NOT_OK_RETURN(ret);
649
650         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
651
652         keystr = sid_string_talloc(tmp_ctx, map->sid);
653         if (keystr == NULL) {
654                 DEBUG(0, ("Out of memory!\n"));
655                 ret = NT_STATUS_NO_MEMORY;
656                 goto done;
657         }
658
659         DEBUG(10,("Fetching record %s\n", keystr));
660
661         /* Check if sid is present in database */
662         ret = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr, &data);
663         if (!NT_STATUS_IS_OK(ret)) {
664                 char *idstr;
665                 struct idmap_tdb2_set_mapping_context store_state;
666
667                 DEBUG(10,(__location__ " Record %s not found\n", keystr));
668
669                 if (ctx->script == NULL) {
670                         ret = NT_STATUS_NONE_MAPPED;
671                         goto done;
672                 }
673
674                 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
675                 if (!NT_STATUS_IS_OK(ret)) {
676                         goto done;
677                 }
678
679                 /* apply filters before returning result */
680                 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
681                         DEBUG(5, ("Script returned id (%u) out of range "
682                                   "(%u - %u). Filtered!\n",
683                                   map->xid.id, dom->low_id, dom->high_id));
684                         ret = NT_STATUS_NONE_MAPPED;
685                         goto done;
686                 }
687
688                 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
689                                         map->xid.type == ID_TYPE_UID?'U':'G',
690                                         (unsigned long)map->xid.id);
691                 if (idstr == NULL) {
692                         ret = NT_STATUS_NO_MEMORY;
693                         goto done;
694                 }
695
696                 store_state.ksidstr = keystr;
697                 store_state.kidstr = idstr;
698
699                 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
700                                       &store_state);
701                 goto done;
702         }
703
704         /* What type of record is this ? */
705         if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
706                 map->xid.id = rec_id;
707                 map->xid.type = ID_TYPE_UID;
708                 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
709                 ret = NT_STATUS_OK;
710
711         } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
712                 map->xid.id = rec_id;
713                 map->xid.type = ID_TYPE_GID;
714                 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
715                 ret = NT_STATUS_OK;
716
717         } else { /* Unknown record type ! */
718                 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
719                 ret = NT_STATUS_INTERNAL_DB_ERROR;
720                 goto done;
721         }
722
723         /* apply filters before returning result */
724         if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
725                 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
726                                 map->xid.id, dom->low_id, dom->high_id));
727                 ret = NT_STATUS_NONE_MAPPED;
728         }
729
730 done:
731         talloc_free(tmp_ctx);
732         return ret;
733 }
734
735 /*
736   lookup a set of unix ids. 
737 */
738 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
739 {
740         NTSTATUS ret;
741         int i;
742
743         /* initialize the status to avoid suprise */
744         for (i = 0; ids[i]; i++) {
745                 ids[i]->status = ID_UNKNOWN;
746         }
747
748         for (i = 0; ids[i]; i++) {
749                 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
750                 if ( ! NT_STATUS_IS_OK(ret)) {
751
752                         /* if it is just a failed mapping continue */
753                         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
754
755                                 /* make sure it is marked as unmapped */
756                                 ids[i]->status = ID_UNMAPPED;
757                                 continue;
758                         }
759
760                         /* some fatal error occurred, return immediately */
761                         goto done;
762                 }
763
764                 /* all ok, id is mapped */
765                 ids[i]->status = ID_MAPPED;
766         }
767
768         ret = NT_STATUS_OK;
769
770 done:
771         return ret;
772 }
773
774 /*
775   lookup a set of sids. 
776 */
777
778 struct idmap_tdb2_sids_to_unixids_context {
779         struct idmap_domain *dom;
780         struct id_map **ids;
781         bool allocate_unmapped;
782 };
783
784 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
785                                                   void *private_data)
786 {
787         struct idmap_tdb2_sids_to_unixids_context *state;
788         int i;
789         NTSTATUS ret = NT_STATUS_OK;
790
791         state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
792
793         DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
794                    " domain: [%s], allocate: %s\n",
795                    state->dom->name,
796                    state->allocate_unmapped ? "yes" : "no"));
797
798         for (i = 0; state->ids[i]; i++) {
799                 if ((state->ids[i]->status == ID_UNKNOWN) ||
800                     /* retry if we could not map in previous run: */
801                     (state->ids[i]->status == ID_UNMAPPED))
802                 {
803                         NTSTATUS ret2;
804
805                         ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
806                         if (!NT_STATUS_IS_OK(ret2)) {
807
808                                 /* if it is just a failed mapping, continue */
809                                 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
810
811                                         /* make sure it is marked as unmapped */
812                                         state->ids[i]->status = ID_UNMAPPED;
813                                         ret = STATUS_SOME_UNMAPPED;
814                                 } else {
815                                         /* some fatal error occurred, return immediately */
816                                         ret = ret2;
817                                         goto done;
818                                 }
819                         } else {
820                                 /* all ok, id is mapped */
821                                 state->ids[i]->status = ID_MAPPED;
822                         }
823                 }
824
825                 if ((state->ids[i]->status == ID_UNMAPPED) &&
826                     state->allocate_unmapped)
827                 {
828                         ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
829                         if (!NT_STATUS_IS_OK(ret)) {
830                                 goto done;
831                         }
832                 }
833         }
834
835 done:
836         return ret;
837 }
838
839 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
840 {
841         NTSTATUS ret;
842         int i;
843         struct idmap_tdb2_sids_to_unixids_context state;
844         struct idmap_tdb2_context *ctx;
845
846         ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
847
848         /* initialize the status to avoid suprise */
849         for (i = 0; ids[i]; i++) {
850                 ids[i]->status = ID_UNKNOWN;
851         }
852
853         state.dom = dom;
854         state.ids = ids;
855         state.allocate_unmapped = false;
856
857         ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
858
859         if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
860                 state.allocate_unmapped = true;
861                 ret = dbwrap_trans_do(ctx->db,
862                                       idmap_tdb2_sids_to_unixids_action,
863                                       &state);
864         }
865
866         return ret;
867 }
868
869
870 static struct idmap_methods db_methods = {
871         .init            = idmap_tdb2_db_init,
872         .unixids_to_sids = idmap_tdb2_unixids_to_sids,
873         .sids_to_unixids = idmap_tdb2_sids_to_unixids,
874         .allocate_id     = idmap_tdb2_get_new_id
875 };
876
877 NTSTATUS samba_init_module(void)
878 {
879         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);
880 }