s4 dns: Clean up tests a bit
[Samba/gebeck_regimport.git] / source3 / winbindd / idmap_tdb2.c
blob7098da1ed775289241950152b55ea115429bbd18
1 /*
2 Unix SMB/CIFS implementation.
4 idmap TDB2 backend, used for clustered Samba setups.
6 This uses dbwrap to access tdb files. The location can be set
7 using tdb:idmap2.tdb =" in smb.conf
9 Copyright (C) Andrew Tridgell 2007
11 This is heavily based upon idmap_tdb.c, which is:
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
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.
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.
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.
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"
44 #undef DBGC_CLASS
45 #define DBGC_CLASS DBGC_IDMAP
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;
53 /* High water mark keys */
54 #define HWM_GROUP "GROUP HWM"
55 #define HWM_USER "USER HWM"
59 * check and initialize high/low water marks in the db
61 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
63 NTSTATUS status;
64 uint32 low_id;
65 struct idmap_tdb2_context *ctx;
67 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
69 /* Create high water marks for group and user id */
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;
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;
93 return NT_STATUS_OK;
98 open the permanent tdb
100 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
102 char *db_path;
103 struct idmap_tdb2_context *ctx;
105 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
107 if (ctx->db) {
108 /* its already open */
109 return NT_STATUS_OK;
112 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
113 NT_STATUS_HAVE_NO_MEMORY(db_path);
115 /* Open idmap repository */
116 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
117 DBWRAP_LOCK_ORDER_1);
118 TALLOC_FREE(db_path);
120 if (ctx->db == NULL) {
121 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
122 db_path));
123 return NT_STATUS_UNSUCCESSFUL;
126 return idmap_tdb2_init_hwm(dom);
131 Allocate a new id.
134 struct idmap_tdb2_allocate_id_context {
135 const char *hwmkey;
136 const char *hwmtype;
137 uint32_t high_hwm;
138 uint32_t hwm;
141 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
142 void *private_data)
144 NTSTATUS ret;
145 struct idmap_tdb2_allocate_id_context *state;
146 uint32_t hwm;
148 state = (struct idmap_tdb2_allocate_id_context *)private_data;
150 ret = dbwrap_fetch_uint32(db, state->hwmkey, &hwm);
151 if (!NT_STATUS_IS_OK(ret)) {
152 ret = NT_STATUS_INTERNAL_DB_ERROR;
153 goto done;
156 /* check it is in the range */
157 if (hwm > state->high_hwm) {
158 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
159 state->hwmtype, (unsigned long)state->high_hwm));
160 ret = NT_STATUS_UNSUCCESSFUL;
161 goto done;
164 /* fetch a new id and increment it */
165 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
166 if (!NT_STATUS_IS_OK(ret)) {
167 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
168 state->hwmtype));
169 goto done;
172 /* recheck it is in the range */
173 if (hwm > state->high_hwm) {
174 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
175 state->hwmtype, (unsigned long)state->high_hwm));
176 ret = NT_STATUS_UNSUCCESSFUL;
177 goto done;
180 ret = NT_STATUS_OK;
181 state->hwm = hwm;
183 done:
184 return ret;
187 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
188 struct unixid *xid)
190 const char *hwmkey;
191 const char *hwmtype;
192 uint32_t high_hwm;
193 uint32_t hwm = 0;
194 NTSTATUS status;
195 struct idmap_tdb2_allocate_id_context state;
196 struct idmap_tdb2_context *ctx;
198 status = idmap_tdb2_open_db(dom);
199 NT_STATUS_NOT_OK_RETURN(status);
201 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
203 /* Get current high water mark */
204 switch (xid->type) {
206 case ID_TYPE_UID:
207 hwmkey = HWM_USER;
208 hwmtype = "UID";
209 break;
211 case ID_TYPE_GID:
212 hwmkey = HWM_GROUP;
213 hwmtype = "GID";
214 break;
216 default:
217 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
218 return NT_STATUS_INVALID_PARAMETER;
221 high_hwm = dom->high_id;
223 state.hwm = hwm;
224 state.high_hwm = high_hwm;
225 state.hwmtype = hwmtype;
226 state.hwmkey = hwmkey;
228 status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
229 &state);
231 if (NT_STATUS_IS_OK(status)) {
232 xid->id = state.hwm;
233 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
234 } else {
235 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
238 return status;
242 * Allocate a new unix-ID.
243 * For now this is for the default idmap domain only.
244 * Should be extended later on.
246 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
247 struct unixid *id)
249 NTSTATUS ret;
251 if (!strequal(dom->name, "*")) {
252 DEBUG(3, ("idmap_tdb2_get_new_id: "
253 "Refusing creation of mapping for domain'%s'. "
254 "Currently only supported for the default "
255 "domain \"*\".\n",
256 dom->name));
257 return NT_STATUS_NOT_IMPLEMENTED;
260 ret = idmap_tdb2_allocate_id(dom, id);
262 return ret;
266 IDMAP MAPPING TDB BACKEND
269 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom,
270 const struct id_map *map);
273 Initialise idmap database.
275 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom)
277 NTSTATUS ret;
278 struct idmap_tdb2_context *ctx;
279 char *config_option = NULL;
280 const char * idmap_script = NULL;
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;
288 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
289 if (config_option == NULL) {
290 DEBUG(0, ("Out of memory!\n"));
291 ret = NT_STATUS_NO_MEMORY;
292 goto failed;
294 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
295 talloc_free(config_option);
297 idmap_script = lp_parm_const_string(-1, "idmap", "script", NULL);
298 if (idmap_script != NULL) {
299 DEBUG(0, ("Warning: 'idmap:script' is deprecated. "
300 " Please use 'idmap config * : script' instead!\n"));
303 if (strequal(dom->name, "*") && ctx->script == NULL) {
304 /* fall back to idmap:script for backwards compatibility */
305 ctx->script = idmap_script;
308 if (ctx->script) {
309 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
312 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
313 if (ctx->rw_ops == NULL) {
314 DEBUG(0, ("Out of memory!\n"));
315 ret = NT_STATUS_NO_MEMORY;
316 goto failed;
319 ctx->rw_ops->get_new_id = idmap_tdb2_get_new_id;
320 ctx->rw_ops->set_mapping = idmap_tdb2_set_mapping;
322 dom->private_data = ctx;
324 ret = idmap_tdb2_open_db(dom);
325 if (!NT_STATUS_IS_OK(ret)) {
326 goto failed;
329 return NT_STATUS_OK;
331 failed:
332 talloc_free(ctx);
333 return ret;
338 * store a mapping in the database.
341 struct idmap_tdb2_set_mapping_context {
342 const char *ksidstr;
343 const char *kidstr;
346 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
347 void *private_data)
349 TDB_DATA data;
350 NTSTATUS ret;
351 struct idmap_tdb2_set_mapping_context *state;
352 TALLOC_CTX *tmp_ctx = talloc_stackframe();
354 state = (struct idmap_tdb2_set_mapping_context *)private_data;
356 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
358 /* check wheter sid mapping is already present in db */
359 ret = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr, &data);
360 if (!NT_STATUS_IS_OK(ret)) {
361 ret = NT_STATUS_OBJECT_NAME_COLLISION;
362 goto done;
365 ret = dbwrap_store_bystring(db, state->ksidstr,
366 string_term_tdb_data(state->kidstr),
367 TDB_INSERT);
368 if (!NT_STATUS_IS_OK(ret)) {
369 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
370 goto done;
373 ret = dbwrap_store_bystring(db, state->kidstr,
374 string_term_tdb_data(state->ksidstr),
375 TDB_INSERT);
376 if (!NT_STATUS_IS_OK(ret)) {
377 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
378 /* try to remove the previous stored SID -> ID map */
379 dbwrap_delete_bystring(db, state->ksidstr);
380 goto done;
383 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
385 done:
386 talloc_free(tmp_ctx);
387 return ret;
390 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
392 struct idmap_tdb2_context *ctx;
393 NTSTATUS ret;
394 char *ksidstr, *kidstr;
395 struct idmap_tdb2_set_mapping_context state;
397 if (!map || !map->sid) {
398 return NT_STATUS_INVALID_PARAMETER;
401 ksidstr = kidstr = NULL;
403 /* TODO: should we filter a set_mapping using low/high filters ? */
405 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
407 switch (map->xid.type) {
409 case ID_TYPE_UID:
410 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
411 break;
413 case ID_TYPE_GID:
414 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
415 break;
417 default:
418 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
419 return NT_STATUS_INVALID_PARAMETER;
422 if (kidstr == NULL) {
423 DEBUG(0, ("ERROR: Out of memory!\n"));
424 ret = NT_STATUS_NO_MEMORY;
425 goto done;
428 ksidstr = sid_string_talloc(ctx, map->sid);
429 if (ksidstr == NULL) {
430 DEBUG(0, ("Out of memory!\n"));
431 ret = NT_STATUS_NO_MEMORY;
432 goto done;
435 state.ksidstr = ksidstr;
436 state.kidstr = kidstr;
438 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
439 &state);
441 done:
442 talloc_free(ksidstr);
443 talloc_free(kidstr);
444 return ret;
448 * Create a new mapping for an unmapped SID, also allocating a new ID.
449 * This should be run inside a transaction.
451 * TODO:
452 * Properly integrate this with multi domain idmap config:
453 * Currently, the allocator is default-config only.
455 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
457 NTSTATUS ret;
458 struct idmap_tdb2_context *ctx;
460 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
462 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
464 return ret;
469 run a script to perform a mapping
471 The script should the following command lines:
473 SIDTOID S-1-xxxx
474 IDTOSID UID xxxx
475 IDTOSID GID xxxx
477 and should return one of the following as a single line of text
478 UID:xxxx
479 GID:xxxx
480 SID:xxxx
481 ERR:xxxx
483 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
484 const char *fmt, ...)
486 va_list ap;
487 char *cmd;
488 FILE *p;
489 char line[64];
490 unsigned long v;
492 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
493 NT_STATUS_HAVE_NO_MEMORY(cmd);
495 va_start(ap, fmt);
496 cmd = talloc_vasprintf_append(cmd, fmt, ap);
497 va_end(ap);
498 NT_STATUS_HAVE_NO_MEMORY(cmd);
500 p = popen(cmd, "r");
501 talloc_free(cmd);
502 if (p == NULL) {
503 return NT_STATUS_NONE_MAPPED;
506 if (fgets(line, sizeof(line)-1, p) == NULL) {
507 pclose(p);
508 return NT_STATUS_NONE_MAPPED;
510 pclose(p);
512 DEBUG(10,("idmap script gave: %s\n", line));
514 if (sscanf(line, "UID:%lu", &v) == 1) {
515 map->xid.id = v;
516 map->xid.type = ID_TYPE_UID;
517 } else if (sscanf(line, "GID:%lu", &v) == 1) {
518 map->xid.id = v;
519 map->xid.type = ID_TYPE_GID;
520 } else if (strncmp(line, "SID:S-", 6) == 0) {
521 if (!string_to_sid(map->sid, &line[4])) {
522 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
523 line, ctx->script));
524 return NT_STATUS_NONE_MAPPED;
526 } else {
527 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
528 line, ctx->script));
529 return NT_STATUS_NONE_MAPPED;
532 return NT_STATUS_OK;
538 Single id to sid lookup function.
540 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
542 NTSTATUS ret;
543 TDB_DATA data;
544 char *keystr;
545 NTSTATUS status;
546 struct idmap_tdb2_context *ctx;
549 if (!dom || !map) {
550 return NT_STATUS_INVALID_PARAMETER;
553 status = idmap_tdb2_open_db(dom);
554 NT_STATUS_NOT_OK_RETURN(status);
556 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
558 /* apply filters before checking */
559 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
560 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
561 map->xid.id, dom->low_id, dom->high_id));
562 return NT_STATUS_NONE_MAPPED;
565 switch (map->xid.type) {
567 case ID_TYPE_UID:
568 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
569 break;
571 case ID_TYPE_GID:
572 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
573 break;
575 default:
576 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
577 return NT_STATUS_INVALID_PARAMETER;
580 if (keystr == NULL) {
581 DEBUG(0, ("Out of memory!\n"));
582 ret = NT_STATUS_NO_MEMORY;
583 goto done;
586 DEBUG(10,("Fetching record %s\n", keystr));
588 /* Check if the mapping exists */
589 status = dbwrap_fetch_bystring(ctx->db, keystr, keystr, &data);
591 if (!NT_STATUS_IS_OK(status)) {
592 char *sidstr;
593 struct idmap_tdb2_set_mapping_context store_state;
595 DEBUG(10,("Record %s not found\n", keystr));
596 if (ctx->script == NULL) {
597 ret = NT_STATUS_NONE_MAPPED;
598 goto done;
601 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
602 if (!NT_STATUS_IS_OK(ret)) {
603 goto done;
606 sidstr = sid_string_talloc(keystr, map->sid);
607 if (!sidstr) {
608 ret = NT_STATUS_NO_MEMORY;
609 goto done;
612 store_state.ksidstr = sidstr;
613 store_state.kidstr = keystr;
615 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
616 &store_state);
617 goto done;
620 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
621 DEBUG(10,("INVALID SID (%s) in record %s\n",
622 (const char *)data.dptr, keystr));
623 ret = NT_STATUS_INTERNAL_DB_ERROR;
624 goto done;
627 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
628 ret = NT_STATUS_OK;
630 done:
631 talloc_free(keystr);
632 return ret;
637 Single sid to id lookup function.
639 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
641 NTSTATUS ret;
642 TDB_DATA data;
643 char *keystr;
644 unsigned long rec_id = 0;
645 struct idmap_tdb2_context *ctx;
646 TALLOC_CTX *tmp_ctx = talloc_stackframe();
648 ret = idmap_tdb2_open_db(dom);
649 NT_STATUS_NOT_OK_RETURN(ret);
651 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
653 keystr = sid_string_talloc(tmp_ctx, map->sid);
654 if (keystr == NULL) {
655 DEBUG(0, ("Out of memory!\n"));
656 ret = NT_STATUS_NO_MEMORY;
657 goto done;
660 DEBUG(10,("Fetching record %s\n", keystr));
662 /* Check if sid is present in database */
663 ret = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr, &data);
664 if (!NT_STATUS_IS_OK(ret)) {
665 char *idstr;
666 struct idmap_tdb2_set_mapping_context store_state;
668 DEBUG(10,(__location__ " Record %s not found\n", keystr));
670 if (ctx->script == NULL) {
671 ret = NT_STATUS_NONE_MAPPED;
672 goto done;
675 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
676 if (!NT_STATUS_IS_OK(ret)) {
677 goto done;
680 /* apply filters before returning result */
681 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
682 DEBUG(5, ("Script returned id (%u) out of range "
683 "(%u - %u). Filtered!\n",
684 map->xid.id, dom->low_id, dom->high_id));
685 ret = NT_STATUS_NONE_MAPPED;
686 goto done;
689 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
690 map->xid.type == ID_TYPE_UID?'U':'G',
691 (unsigned long)map->xid.id);
692 if (idstr == NULL) {
693 ret = NT_STATUS_NO_MEMORY;
694 goto done;
697 store_state.ksidstr = keystr;
698 store_state.kidstr = idstr;
700 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
701 &store_state);
702 goto done;
705 /* What type of record is this ? */
706 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
707 map->xid.id = rec_id;
708 map->xid.type = ID_TYPE_UID;
709 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
710 ret = NT_STATUS_OK;
712 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
713 map->xid.id = rec_id;
714 map->xid.type = ID_TYPE_GID;
715 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
716 ret = NT_STATUS_OK;
718 } else { /* Unknown record type ! */
719 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
720 ret = NT_STATUS_INTERNAL_DB_ERROR;
721 goto done;
724 /* apply filters before returning result */
725 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
726 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
727 map->xid.id, dom->low_id, dom->high_id));
728 ret = NT_STATUS_NONE_MAPPED;
731 done:
732 talloc_free(tmp_ctx);
733 return ret;
737 lookup a set of unix ids.
739 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
741 NTSTATUS ret;
742 int i;
744 /* initialize the status to avoid suprise */
745 for (i = 0; ids[i]; i++) {
746 ids[i]->status = ID_UNKNOWN;
749 for (i = 0; ids[i]; i++) {
750 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
751 if ( ! NT_STATUS_IS_OK(ret)) {
753 /* if it is just a failed mapping continue */
754 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
756 /* make sure it is marked as unmapped */
757 ids[i]->status = ID_UNMAPPED;
758 continue;
761 /* some fatal error occurred, return immediately */
762 goto done;
765 /* all ok, id is mapped */
766 ids[i]->status = ID_MAPPED;
769 ret = NT_STATUS_OK;
771 done:
772 return ret;
776 lookup a set of sids.
779 struct idmap_tdb2_sids_to_unixids_context {
780 struct idmap_domain *dom;
781 struct id_map **ids;
782 bool allocate_unmapped;
785 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
786 void *private_data)
788 struct idmap_tdb2_sids_to_unixids_context *state;
789 int i;
790 NTSTATUS ret = NT_STATUS_OK;
792 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
794 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
795 " domain: [%s], allocate: %s\n",
796 state->dom->name,
797 state->allocate_unmapped ? "yes" : "no"));
799 for (i = 0; state->ids[i]; i++) {
800 if ((state->ids[i]->status == ID_UNKNOWN) ||
801 /* retry if we could not map in previous run: */
802 (state->ids[i]->status == ID_UNMAPPED))
804 NTSTATUS ret2;
806 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
807 if (!NT_STATUS_IS_OK(ret2)) {
809 /* if it is just a failed mapping, continue */
810 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
812 /* make sure it is marked as unmapped */
813 state->ids[i]->status = ID_UNMAPPED;
814 ret = STATUS_SOME_UNMAPPED;
815 } else {
816 /* some fatal error occurred, return immediately */
817 ret = ret2;
818 goto done;
820 } else {
821 /* all ok, id is mapped */
822 state->ids[i]->status = ID_MAPPED;
826 if ((state->ids[i]->status == ID_UNMAPPED) &&
827 state->allocate_unmapped)
829 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
830 if (!NT_STATUS_IS_OK(ret)) {
831 goto done;
836 done:
837 return ret;
840 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
842 NTSTATUS ret;
843 int i;
844 struct idmap_tdb2_sids_to_unixids_context state;
845 struct idmap_tdb2_context *ctx;
847 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
849 /* initialize the status to avoid suprise */
850 for (i = 0; ids[i]; i++) {
851 ids[i]->status = ID_UNKNOWN;
854 state.dom = dom;
855 state.ids = ids;
856 state.allocate_unmapped = false;
858 ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
860 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
861 state.allocate_unmapped = true;
862 ret = dbwrap_trans_do(ctx->db,
863 idmap_tdb2_sids_to_unixids_action,
864 &state);
867 return ret;
871 static struct idmap_methods db_methods = {
872 .init = idmap_tdb2_db_init,
873 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
874 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
875 .allocate_id = idmap_tdb2_get_new_id
878 NTSTATUS samba_init_module(void)
880 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);