wafsamba: Fix handling of pyembed/pyext.
[Samba.git] / source3 / winbindd / idmap_tdb2.c
blob0613fdb596ffde3ff644ab9a9cdfbcdf2cdd069e
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 "winbindd.h"
36 #include "idmap.h"
37 #include "idmap_rw.h"
38 #include "dbwrap.h"
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_IDMAP
43 struct idmap_tdb2_context {
44 struct db_context *db;
45 const char *script; /* script to provide idmaps */
46 struct idmap_rw_ops *rw_ops;
49 /* High water mark keys */
50 #define HWM_GROUP "GROUP HWM"
51 #define HWM_USER "USER HWM"
55 * check and initialize high/low water marks in the db
57 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
59 uint32 low_id;
60 struct idmap_tdb2_context *ctx;
62 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
64 /* Create high water marks for group and user id */
66 low_id = dbwrap_fetch_int32(ctx->db, HWM_USER);
67 if ((low_id == -1) || (low_id < dom->low_id)) {
68 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
69 ctx->db, HWM_USER,
70 dom->low_id))) {
71 DEBUG(0, ("Unable to initialise user hwm in idmap "
72 "database\n"));
73 return NT_STATUS_INTERNAL_DB_ERROR;
77 low_id = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
78 if ((low_id == -1) || (low_id < dom->low_id)) {
79 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
80 ctx->db, HWM_GROUP,
81 dom->low_id))) {
82 DEBUG(0, ("Unable to initialise group hwm in idmap "
83 "database\n"));
84 return NT_STATUS_INTERNAL_DB_ERROR;
88 return NT_STATUS_OK;
93 open the permanent tdb
95 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
97 char *db_path;
98 struct idmap_tdb2_context *ctx;
100 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
102 if (ctx->db) {
103 /* its already open */
104 return NT_STATUS_OK;
107 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
108 if (db_path == NULL) {
109 /* fall back to the private directory, which, despite
110 its name, is usually on shared storage */
111 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 TALLOC_FREE(db_path);
119 if (ctx->db == NULL) {
120 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
121 db_path));
122 return NT_STATUS_UNSUCCESSFUL;
125 return idmap_tdb2_init_hwm(dom);
130 Allocate a new id.
133 struct idmap_tdb2_allocate_id_context {
134 const char *hwmkey;
135 const char *hwmtype;
136 uint32_t high_hwm;
137 uint32_t hwm;
140 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
141 void *private_data)
143 NTSTATUS ret;
144 struct idmap_tdb2_allocate_id_context *state;
145 uint32_t hwm;
147 state = (struct idmap_tdb2_allocate_id_context *)private_data;
149 hwm = dbwrap_fetch_int32(db, state->hwmkey);
150 if (hwm == -1) {
151 ret = NT_STATUS_INTERNAL_DB_ERROR;
152 goto done;
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;
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;
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;
179 ret = NT_STATUS_OK;
180 state->hwm = hwm;
182 done:
183 return ret;
186 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
187 struct unixid *xid)
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;
197 status = idmap_tdb2_open_db(dom);
198 NT_STATUS_NOT_OK_RETURN(status);
200 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
202 /* Get current high water mark */
203 switch (xid->type) {
205 case ID_TYPE_UID:
206 hwmkey = HWM_USER;
207 hwmtype = "UID";
208 break;
210 case ID_TYPE_GID:
211 hwmkey = HWM_GROUP;
212 hwmtype = "GID";
213 break;
215 default:
216 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
217 return NT_STATUS_INVALID_PARAMETER;
220 high_hwm = dom->high_id;
222 state.hwm = hwm;
223 state.high_hwm = high_hwm;
224 state.hwmtype = hwmtype;
225 state.hwmkey = hwmkey;
227 status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
228 &state);
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));
237 return status;
241 * Allocate a new unix-ID.
242 * For now this is for the default idmap domain only.
243 * Should be extended later on.
245 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
246 struct unixid *id)
248 NTSTATUS ret;
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;
259 ret = idmap_tdb2_allocate_id(dom, id);
261 return ret;
265 IDMAP MAPPING TDB BACKEND
268 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom,
269 const struct id_map *map);
272 Initialise idmap database.
274 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom,
275 const char *params)
277 NTSTATUS ret;
278 struct idmap_tdb2_context *ctx;
280 ctx = talloc_zero(dom, struct idmap_tdb2_context);
281 if ( ! ctx) {
282 DEBUG(0, ("Out of memory!\n"));
283 return NT_STATUS_NO_MEMORY;
286 if (strequal(dom->name, "*")) {
287 ctx->script = lp_parm_const_string(-1, "idmap", "script", NULL);
288 if (ctx->script) {
289 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
291 } else {
292 char *config_option = NULL;
294 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
295 if ( ! config_option) {
296 DEBUG(0, ("Out of memory!\n"));
297 ret = NT_STATUS_NO_MEMORY;
298 goto failed;
301 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
302 if (ctx->script) {
303 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
306 talloc_free(config_option);
309 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
310 if (ctx->rw_ops == NULL) {
311 DEBUG(0, ("Out of memory!\n"));
312 ret = NT_STATUS_NO_MEMORY;
313 goto failed;
316 ctx->rw_ops->get_new_id = idmap_tdb2_get_new_id;
317 ctx->rw_ops->set_mapping = idmap_tdb2_set_mapping;
319 dom->private_data = ctx;
321 ret = idmap_tdb2_open_db(dom);
322 if (!NT_STATUS_IS_OK(ret)) {
323 goto failed;
326 return NT_STATUS_OK;
328 failed:
329 talloc_free(ctx);
330 return ret;
335 * store a mapping in the database.
338 struct idmap_tdb2_set_mapping_context {
339 const char *ksidstr;
340 const char *kidstr;
343 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
344 void *private_data)
346 TDB_DATA data;
347 NTSTATUS ret;
348 struct idmap_tdb2_set_mapping_context *state;
349 TALLOC_CTX *tmp_ctx = talloc_stackframe();
351 state = (struct idmap_tdb2_set_mapping_context *)private_data;
353 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
355 /* check wheter sid mapping is already present in db */
356 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
357 if (data.dptr) {
358 ret = NT_STATUS_OBJECT_NAME_COLLISION;
359 goto done;
362 ret = dbwrap_store_bystring(db, state->ksidstr,
363 string_term_tdb_data(state->kidstr),
364 TDB_INSERT);
365 if (!NT_STATUS_IS_OK(ret)) {
366 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
367 goto done;
370 ret = dbwrap_store_bystring(db, state->kidstr,
371 string_term_tdb_data(state->ksidstr),
372 TDB_INSERT);
373 if (!NT_STATUS_IS_OK(ret)) {
374 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
375 /* try to remove the previous stored SID -> ID map */
376 dbwrap_delete_bystring(db, state->ksidstr);
377 goto done;
380 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
382 done:
383 talloc_free(tmp_ctx);
384 return ret;
387 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
389 struct idmap_tdb2_context *ctx;
390 NTSTATUS ret;
391 char *ksidstr, *kidstr;
392 struct idmap_tdb2_set_mapping_context state;
394 if (!map || !map->sid) {
395 return NT_STATUS_INVALID_PARAMETER;
398 ksidstr = kidstr = NULL;
400 /* TODO: should we filter a set_mapping using low/high filters ? */
402 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
404 switch (map->xid.type) {
406 case ID_TYPE_UID:
407 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
408 break;
410 case ID_TYPE_GID:
411 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
412 break;
414 default:
415 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
416 return NT_STATUS_INVALID_PARAMETER;
419 if (kidstr == NULL) {
420 DEBUG(0, ("ERROR: Out of memory!\n"));
421 ret = NT_STATUS_NO_MEMORY;
422 goto done;
425 ksidstr = sid_string_talloc(ctx, map->sid);
426 if (ksidstr == NULL) {
427 DEBUG(0, ("Out of memory!\n"));
428 ret = NT_STATUS_NO_MEMORY;
429 goto done;
432 state.ksidstr = ksidstr;
433 state.kidstr = kidstr;
435 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
436 &state);
438 done:
439 talloc_free(ksidstr);
440 talloc_free(kidstr);
441 return ret;
445 * Create a new mapping for an unmapped SID, also allocating a new ID.
446 * This should be run inside a transaction.
448 * TODO:
449 * Properly integrate this with multi domain idmap config:
450 * Currently, the allocator is default-config only.
452 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
454 NTSTATUS ret;
455 struct idmap_tdb2_context *ctx;
457 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
459 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
461 return ret;
466 run a script to perform a mapping
468 The script should the following command lines:
470 SIDTOID S-1-xxxx
471 IDTOSID UID xxxx
472 IDTOSID GID xxxx
474 and should return one of the following as a single line of text
475 UID:xxxx
476 GID:xxxx
477 SID:xxxx
478 ERR:xxxx
480 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
481 const char *fmt, ...)
483 va_list ap;
484 char *cmd;
485 FILE *p;
486 char line[64];
487 unsigned long v;
489 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
490 NT_STATUS_HAVE_NO_MEMORY(cmd);
492 va_start(ap, fmt);
493 cmd = talloc_vasprintf_append(cmd, fmt, ap);
494 va_end(ap);
495 NT_STATUS_HAVE_NO_MEMORY(cmd);
497 p = popen(cmd, "r");
498 talloc_free(cmd);
499 if (p == NULL) {
500 return NT_STATUS_NONE_MAPPED;
503 if (fgets(line, sizeof(line)-1, p) == NULL) {
504 pclose(p);
505 return NT_STATUS_NONE_MAPPED;
507 pclose(p);
509 DEBUG(10,("idmap script gave: %s\n", line));
511 if (sscanf(line, "UID:%lu", &v) == 1) {
512 map->xid.id = v;
513 map->xid.type = ID_TYPE_UID;
514 } else if (sscanf(line, "GID:%lu", &v) == 1) {
515 map->xid.id = v;
516 map->xid.type = ID_TYPE_GID;
517 } else if (strncmp(line, "SID:S-", 6) == 0) {
518 if (!string_to_sid(map->sid, &line[4])) {
519 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
520 line, ctx->script));
521 return NT_STATUS_NONE_MAPPED;
523 } else {
524 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
525 line, ctx->script));
526 return NT_STATUS_NONE_MAPPED;
529 return NT_STATUS_OK;
535 Single id to sid lookup function.
537 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
539 NTSTATUS ret;
540 TDB_DATA data;
541 char *keystr;
542 NTSTATUS status;
543 struct idmap_tdb2_context *ctx;
546 if (!dom || !map) {
547 return NT_STATUS_INVALID_PARAMETER;
550 status = idmap_tdb2_open_db(dom);
551 NT_STATUS_NOT_OK_RETURN(status);
553 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
555 /* apply filters before checking */
556 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
557 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
558 map->xid.id, dom->low_id, dom->high_id));
559 return NT_STATUS_NONE_MAPPED;
562 switch (map->xid.type) {
564 case ID_TYPE_UID:
565 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
566 break;
568 case ID_TYPE_GID:
569 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
570 break;
572 default:
573 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
574 return NT_STATUS_INVALID_PARAMETER;
577 /* final SAFE_FREE safe */
578 data.dptr = NULL;
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 data = dbwrap_fetch_bystring(ctx->db, keystr, keystr);
591 if (!data.dptr) {
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);
603 /* store it on shared storage */
604 if (!NT_STATUS_IS_OK(ret)) {
605 goto done;
608 sidstr = sid_string_talloc(keystr, map->sid);
609 if (!sidstr) {
610 ret = NT_STATUS_NO_MEMORY;
611 goto done;
614 store_state.ksidstr = sidstr;
615 store_state.kidstr = keystr;
617 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
618 &store_state);
619 goto done;
622 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
623 DEBUG(10,("INVALID SID (%s) in record %s\n",
624 (const char *)data.dptr, keystr));
625 ret = NT_STATUS_INTERNAL_DB_ERROR;
626 goto done;
629 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
630 ret = NT_STATUS_OK;
632 done:
633 talloc_free(keystr);
634 return ret;
639 Single sid to id lookup function.
641 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
643 NTSTATUS ret;
644 TDB_DATA data;
645 char *keystr;
646 unsigned long rec_id = 0;
647 struct idmap_tdb2_context *ctx;
648 TALLOC_CTX *tmp_ctx = talloc_stackframe();
650 ret = idmap_tdb2_open_db(dom);
651 NT_STATUS_NOT_OK_RETURN(ret);
653 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
655 keystr = sid_string_talloc(tmp_ctx, map->sid);
656 if (keystr == NULL) {
657 DEBUG(0, ("Out of memory!\n"));
658 ret = NT_STATUS_NO_MEMORY;
659 goto done;
662 DEBUG(10,("Fetching record %s\n", keystr));
664 /* Check if sid is present in database */
665 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
666 if (!data.dptr) {
667 char *idstr;
668 struct idmap_tdb2_set_mapping_context store_state;
670 DEBUG(10,(__location__ " Record %s not found\n", keystr));
672 if (ctx->script == NULL) {
673 ret = NT_STATUS_NONE_MAPPED;
674 goto done;
677 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
678 /* store it on shared storage */
679 if (!NT_STATUS_IS_OK(ret)) {
680 goto done;
683 /* apply filters before returning result */
684 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
685 DEBUG(5, ("Script returned id (%u) out of range "
686 "(%u - %u). Filtered!\n",
687 map->xid.id, dom->low_id, dom->high_id));
688 ret = NT_STATUS_NONE_MAPPED;
689 goto done;
692 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
693 map->xid.type == ID_TYPE_UID?'U':'G',
694 (unsigned long)map->xid.id);
695 if (idstr == NULL) {
696 ret = NT_STATUS_NO_MEMORY;
697 goto done;
700 store_state.ksidstr = keystr;
701 store_state.kidstr = idstr;
703 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
704 &store_state);
705 goto done;
708 /* What type of record is this ? */
709 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
710 map->xid.id = rec_id;
711 map->xid.type = ID_TYPE_UID;
712 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
713 ret = NT_STATUS_OK;
715 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
716 map->xid.id = rec_id;
717 map->xid.type = ID_TYPE_GID;
718 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
719 ret = NT_STATUS_OK;
721 } else { /* Unknown record type ! */
722 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
723 ret = NT_STATUS_INTERNAL_DB_ERROR;
724 goto done;
727 /* apply filters before returning result */
728 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
729 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
730 map->xid.id, dom->low_id, dom->high_id));
731 ret = NT_STATUS_NONE_MAPPED;
734 done:
735 talloc_free(tmp_ctx);
736 return ret;
740 lookup a set of unix ids.
742 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
744 NTSTATUS ret;
745 int i;
747 /* initialize the status to avoid suprise */
748 for (i = 0; ids[i]; i++) {
749 ids[i]->status = ID_UNKNOWN;
752 for (i = 0; ids[i]; i++) {
753 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
754 if ( ! NT_STATUS_IS_OK(ret)) {
756 /* if it is just a failed mapping continue */
757 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
759 /* make sure it is marked as unmapped */
760 ids[i]->status = ID_UNMAPPED;
761 continue;
764 /* some fatal error occurred, return immediately */
765 goto done;
768 /* all ok, id is mapped */
769 ids[i]->status = ID_MAPPED;
772 ret = NT_STATUS_OK;
774 done:
775 return ret;
779 lookup a set of sids.
782 struct idmap_tdb2_sids_to_unixids_context {
783 struct idmap_domain *dom;
784 struct id_map **ids;
785 bool allocate_unmapped;
788 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
789 void *private_data)
791 struct idmap_tdb2_sids_to_unixids_context *state;
792 int i;
793 NTSTATUS ret = NT_STATUS_OK;
795 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
797 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
798 " domain: [%s], allocate: %s\n",
799 state->dom->name,
800 state->allocate_unmapped ? "yes" : "no"));
802 for (i = 0; state->ids[i]; i++) {
803 if ((state->ids[i]->status == ID_UNKNOWN) ||
804 /* retry if we could not map in previous run: */
805 (state->ids[i]->status == ID_UNMAPPED))
807 NTSTATUS ret2;
809 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
810 if (!NT_STATUS_IS_OK(ret2)) {
812 /* if it is just a failed mapping, continue */
813 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
815 /* make sure it is marked as unmapped */
816 state->ids[i]->status = ID_UNMAPPED;
817 ret = STATUS_SOME_UNMAPPED;
818 } else {
819 /* some fatal error occurred, return immediately */
820 ret = ret2;
821 goto done;
823 } else {
824 /* all ok, id is mapped */
825 state->ids[i]->status = ID_MAPPED;
829 if ((state->ids[i]->status == ID_UNMAPPED) &&
830 state->allocate_unmapped)
832 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
833 if (!NT_STATUS_IS_OK(ret)) {
834 goto done;
839 done:
840 return ret;
843 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
845 NTSTATUS ret;
846 int i;
847 struct idmap_tdb2_sids_to_unixids_context state;
848 struct idmap_tdb2_context *ctx;
850 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
852 /* initialize the status to avoid suprise */
853 for (i = 0; ids[i]; i++) {
854 ids[i]->status = ID_UNKNOWN;
857 state.dom = dom;
858 state.ids = ids;
859 state.allocate_unmapped = false;
861 ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
863 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
864 state.allocate_unmapped = true;
865 ret = dbwrap_trans_do(ctx->db,
866 idmap_tdb2_sids_to_unixids_action,
867 &state);
870 return ret;
875 Close the idmap tdb instance
877 static NTSTATUS idmap_tdb2_close(struct idmap_domain *dom)
879 /* don't do anything */
880 return NT_STATUS_OK;
883 static struct idmap_methods db_methods = {
884 .init = idmap_tdb2_db_init,
885 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
886 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
887 .allocate_id = idmap_tdb2_get_new_id,
888 .close_fn = idmap_tdb2_close
891 NTSTATUS idmap_tdb2_init(void)
893 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);