s3:idmap_tdb2: deprecate the idmap:script parameter and use "idmap config * : script...
[Samba.git] / source3 / winbindd / idmap_tdb2.c
blob5612d57da777bace27ac453c89340fcf5ffc7713
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.h"
40 #include "../libcli/security/dom_sid.h"
41 #include "util_tdb.h"
43 #undef DBGC_CLASS
44 #define DBGC_CLASS DBGC_IDMAP
46 struct idmap_tdb2_context {
47 struct db_context *db;
48 const char *script; /* script to provide idmaps */
49 struct idmap_rw_ops *rw_ops;
52 /* High water mark keys */
53 #define HWM_GROUP "GROUP HWM"
54 #define HWM_USER "USER HWM"
58 * check and initialize high/low water marks in the db
60 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
62 NTSTATUS status;
63 uint32 low_id;
64 struct idmap_tdb2_context *ctx;
66 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
68 /* Create high water marks for group and user id */
70 low_id = dbwrap_fetch_int32(ctx->db, HWM_USER);
71 if ((low_id == -1) || (low_id < dom->low_id)) {
72 status = dbwrap_trans_store_int32(ctx->db, HWM_USER,
73 dom->low_id);
74 if (!NT_STATUS_IS_OK(status)) {
75 DEBUG(0, ("Unable to initialise user hwm in idmap "
76 "database: %s\n", nt_errstr(status)));
77 return NT_STATUS_INTERNAL_DB_ERROR;
81 low_id = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
82 if ((low_id == -1) || (low_id < dom->low_id)) {
83 status = dbwrap_trans_store_int32(ctx->db, HWM_GROUP,
84 dom->low_id);
85 if (!NT_STATUS_IS_OK(status)) {
86 DEBUG(0, ("Unable to initialise group hwm in idmap "
87 "database: %s\n", nt_errstr(status)));
88 return NT_STATUS_INTERNAL_DB_ERROR;
92 return NT_STATUS_OK;
97 open the permanent tdb
99 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
101 char *db_path;
102 struct idmap_tdb2_context *ctx;
104 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
106 if (ctx->db) {
107 /* its already open */
108 return NT_STATUS_OK;
111 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
112 if (db_path == NULL) {
113 /* fall back to the private directory, which, despite
114 its name, is usually on shared storage */
115 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
117 NT_STATUS_HAVE_NO_MEMORY(db_path);
119 /* Open idmap repository */
120 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
121 TALLOC_FREE(db_path);
123 if (ctx->db == NULL) {
124 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
125 db_path));
126 return NT_STATUS_UNSUCCESSFUL;
129 return idmap_tdb2_init_hwm(dom);
134 Allocate a new id.
137 struct idmap_tdb2_allocate_id_context {
138 const char *hwmkey;
139 const char *hwmtype;
140 uint32_t high_hwm;
141 uint32_t hwm;
144 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
145 void *private_data)
147 NTSTATUS ret;
148 struct idmap_tdb2_allocate_id_context *state;
149 uint32_t hwm;
151 state = (struct idmap_tdb2_allocate_id_context *)private_data;
153 hwm = dbwrap_fetch_int32(db, state->hwmkey);
154 if (hwm == -1) {
155 ret = NT_STATUS_INTERNAL_DB_ERROR;
156 goto done;
159 /* check it is in the range */
160 if (hwm > state->high_hwm) {
161 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
162 state->hwmtype, (unsigned long)state->high_hwm));
163 ret = NT_STATUS_UNSUCCESSFUL;
164 goto done;
167 /* fetch a new id and increment it */
168 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
169 if (!NT_STATUS_IS_OK(ret)) {
170 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
171 state->hwmtype));
172 goto done;
175 /* recheck it is in the range */
176 if (hwm > state->high_hwm) {
177 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
178 state->hwmtype, (unsigned long)state->high_hwm));
179 ret = NT_STATUS_UNSUCCESSFUL;
180 goto done;
183 ret = NT_STATUS_OK;
184 state->hwm = hwm;
186 done:
187 return ret;
190 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
191 struct unixid *xid)
193 const char *hwmkey;
194 const char *hwmtype;
195 uint32_t high_hwm;
196 uint32_t hwm = 0;
197 NTSTATUS status;
198 struct idmap_tdb2_allocate_id_context state;
199 struct idmap_tdb2_context *ctx;
201 status = idmap_tdb2_open_db(dom);
202 NT_STATUS_NOT_OK_RETURN(status);
204 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
206 /* Get current high water mark */
207 switch (xid->type) {
209 case ID_TYPE_UID:
210 hwmkey = HWM_USER;
211 hwmtype = "UID";
212 break;
214 case ID_TYPE_GID:
215 hwmkey = HWM_GROUP;
216 hwmtype = "GID";
217 break;
219 default:
220 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
221 return NT_STATUS_INVALID_PARAMETER;
224 high_hwm = dom->high_id;
226 state.hwm = hwm;
227 state.high_hwm = high_hwm;
228 state.hwmtype = hwmtype;
229 state.hwmkey = hwmkey;
231 status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
232 &state);
234 if (NT_STATUS_IS_OK(status)) {
235 xid->id = state.hwm;
236 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
237 } else {
238 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
241 return status;
245 * Allocate a new unix-ID.
246 * For now this is for the default idmap domain only.
247 * Should be extended later on.
249 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
250 struct unixid *id)
252 NTSTATUS ret;
254 if (!strequal(dom->name, "*")) {
255 DEBUG(3, ("idmap_tdb2_get_new_id: "
256 "Refusing creation of mapping for domain'%s'. "
257 "Currently only supported for the default "
258 "domain \"*\".\n",
259 dom->name));
260 return NT_STATUS_NOT_IMPLEMENTED;
263 ret = idmap_tdb2_allocate_id(dom, id);
265 return ret;
269 IDMAP MAPPING TDB BACKEND
272 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom,
273 const struct id_map *map);
276 Initialise idmap database.
278 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom)
280 NTSTATUS ret;
281 struct idmap_tdb2_context *ctx;
282 char *config_option = NULL;
283 const char * idmap_script = NULL;
285 ctx = talloc_zero(dom, struct idmap_tdb2_context);
286 if ( ! ctx) {
287 DEBUG(0, ("Out of memory!\n"));
288 return NT_STATUS_NO_MEMORY;
291 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
292 if (config_option == NULL) {
293 DEBUG(0, ("Out of memory!\n"));
294 ret = NT_STATUS_NO_MEMORY;
295 goto failed;
297 ctx->script = lp_parm_const_string(-1, config_option, "script", "NULL");
298 talloc_free(config_option);
300 idmap_script = lp_parm_const_string(-1, "idmap", "script", NULL);
301 if (idmap_script != NULL) {
302 DEBUG(0, ("Warning: 'idmap:script' is deprecated. "
303 " Please use 'idmap config * : script' instead!\n"));
306 if (strequal(dom->name, "*") && ctx->script == NULL) {
307 /* fall back to idmap:script for backwards compatibility */
308 ctx->script = idmap_script;
311 if (ctx->script) {
312 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
315 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
316 if (ctx->rw_ops == NULL) {
317 DEBUG(0, ("Out of memory!\n"));
318 ret = NT_STATUS_NO_MEMORY;
319 goto failed;
322 ctx->rw_ops->get_new_id = idmap_tdb2_get_new_id;
323 ctx->rw_ops->set_mapping = idmap_tdb2_set_mapping;
325 dom->private_data = ctx;
327 ret = idmap_tdb2_open_db(dom);
328 if (!NT_STATUS_IS_OK(ret)) {
329 goto failed;
332 return NT_STATUS_OK;
334 failed:
335 talloc_free(ctx);
336 return ret;
341 * store a mapping in the database.
344 struct idmap_tdb2_set_mapping_context {
345 const char *ksidstr;
346 const char *kidstr;
349 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
350 void *private_data)
352 TDB_DATA data;
353 NTSTATUS ret;
354 struct idmap_tdb2_set_mapping_context *state;
355 TALLOC_CTX *tmp_ctx = talloc_stackframe();
357 state = (struct idmap_tdb2_set_mapping_context *)private_data;
359 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
361 /* check wheter sid mapping is already present in db */
362 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
363 if (data.dptr) {
364 ret = NT_STATUS_OBJECT_NAME_COLLISION;
365 goto done;
368 ret = dbwrap_store_bystring(db, state->ksidstr,
369 string_term_tdb_data(state->kidstr),
370 TDB_INSERT);
371 if (!NT_STATUS_IS_OK(ret)) {
372 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
373 goto done;
376 ret = dbwrap_store_bystring(db, state->kidstr,
377 string_term_tdb_data(state->ksidstr),
378 TDB_INSERT);
379 if (!NT_STATUS_IS_OK(ret)) {
380 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
381 /* try to remove the previous stored SID -> ID map */
382 dbwrap_delete_bystring(db, state->ksidstr);
383 goto done;
386 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
388 done:
389 talloc_free(tmp_ctx);
390 return ret;
393 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
395 struct idmap_tdb2_context *ctx;
396 NTSTATUS ret;
397 char *ksidstr, *kidstr;
398 struct idmap_tdb2_set_mapping_context state;
400 if (!map || !map->sid) {
401 return NT_STATUS_INVALID_PARAMETER;
404 ksidstr = kidstr = NULL;
406 /* TODO: should we filter a set_mapping using low/high filters ? */
408 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
410 switch (map->xid.type) {
412 case ID_TYPE_UID:
413 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
414 break;
416 case ID_TYPE_GID:
417 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
418 break;
420 default:
421 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
422 return NT_STATUS_INVALID_PARAMETER;
425 if (kidstr == NULL) {
426 DEBUG(0, ("ERROR: Out of memory!\n"));
427 ret = NT_STATUS_NO_MEMORY;
428 goto done;
431 ksidstr = sid_string_talloc(ctx, map->sid);
432 if (ksidstr == NULL) {
433 DEBUG(0, ("Out of memory!\n"));
434 ret = NT_STATUS_NO_MEMORY;
435 goto done;
438 state.ksidstr = ksidstr;
439 state.kidstr = kidstr;
441 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
442 &state);
444 done:
445 talloc_free(ksidstr);
446 talloc_free(kidstr);
447 return ret;
451 * Create a new mapping for an unmapped SID, also allocating a new ID.
452 * This should be run inside a transaction.
454 * TODO:
455 * Properly integrate this with multi domain idmap config:
456 * Currently, the allocator is default-config only.
458 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
460 NTSTATUS ret;
461 struct idmap_tdb2_context *ctx;
463 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
465 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
467 return ret;
472 run a script to perform a mapping
474 The script should the following command lines:
476 SIDTOID S-1-xxxx
477 IDTOSID UID xxxx
478 IDTOSID GID xxxx
480 and should return one of the following as a single line of text
481 UID:xxxx
482 GID:xxxx
483 SID:xxxx
484 ERR:xxxx
486 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
487 const char *fmt, ...)
489 va_list ap;
490 char *cmd;
491 FILE *p;
492 char line[64];
493 unsigned long v;
495 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
496 NT_STATUS_HAVE_NO_MEMORY(cmd);
498 va_start(ap, fmt);
499 cmd = talloc_vasprintf_append(cmd, fmt, ap);
500 va_end(ap);
501 NT_STATUS_HAVE_NO_MEMORY(cmd);
503 p = popen(cmd, "r");
504 talloc_free(cmd);
505 if (p == NULL) {
506 return NT_STATUS_NONE_MAPPED;
509 if (fgets(line, sizeof(line)-1, p) == NULL) {
510 pclose(p);
511 return NT_STATUS_NONE_MAPPED;
513 pclose(p);
515 DEBUG(10,("idmap script gave: %s\n", line));
517 if (sscanf(line, "UID:%lu", &v) == 1) {
518 map->xid.id = v;
519 map->xid.type = ID_TYPE_UID;
520 } else if (sscanf(line, "GID:%lu", &v) == 1) {
521 map->xid.id = v;
522 map->xid.type = ID_TYPE_GID;
523 } else if (strncmp(line, "SID:S-", 6) == 0) {
524 if (!string_to_sid(map->sid, &line[4])) {
525 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
526 line, ctx->script));
527 return NT_STATUS_NONE_MAPPED;
529 } else {
530 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
531 line, ctx->script));
532 return NT_STATUS_NONE_MAPPED;
535 return NT_STATUS_OK;
541 Single id to sid lookup function.
543 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
545 NTSTATUS ret;
546 TDB_DATA data;
547 char *keystr;
548 NTSTATUS status;
549 struct idmap_tdb2_context *ctx;
552 if (!dom || !map) {
553 return NT_STATUS_INVALID_PARAMETER;
556 status = idmap_tdb2_open_db(dom);
557 NT_STATUS_NOT_OK_RETURN(status);
559 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
561 /* apply filters before checking */
562 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
563 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
564 map->xid.id, dom->low_id, dom->high_id));
565 return NT_STATUS_NONE_MAPPED;
568 switch (map->xid.type) {
570 case ID_TYPE_UID:
571 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
572 break;
574 case ID_TYPE_GID:
575 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
576 break;
578 default:
579 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
580 return NT_STATUS_INVALID_PARAMETER;
583 /* final SAFE_FREE safe */
584 data.dptr = NULL;
586 if (keystr == NULL) {
587 DEBUG(0, ("Out of memory!\n"));
588 ret = NT_STATUS_NO_MEMORY;
589 goto done;
592 DEBUG(10,("Fetching record %s\n", keystr));
594 /* Check if the mapping exists */
595 data = dbwrap_fetch_bystring(ctx->db, keystr, keystr);
597 if (!data.dptr) {
598 char *sidstr;
599 struct idmap_tdb2_set_mapping_context store_state;
601 DEBUG(10,("Record %s not found\n", keystr));
602 if (ctx->script == NULL) {
603 ret = NT_STATUS_NONE_MAPPED;
604 goto done;
607 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
609 /* store it on shared storage */
610 if (!NT_STATUS_IS_OK(ret)) {
611 goto done;
614 sidstr = sid_string_talloc(keystr, map->sid);
615 if (!sidstr) {
616 ret = NT_STATUS_NO_MEMORY;
617 goto done;
620 store_state.ksidstr = sidstr;
621 store_state.kidstr = keystr;
623 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
624 &store_state);
625 goto done;
628 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
629 DEBUG(10,("INVALID SID (%s) in record %s\n",
630 (const char *)data.dptr, keystr));
631 ret = NT_STATUS_INTERNAL_DB_ERROR;
632 goto done;
635 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
636 ret = NT_STATUS_OK;
638 done:
639 talloc_free(keystr);
640 return ret;
645 Single sid to id lookup function.
647 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
649 NTSTATUS ret;
650 TDB_DATA data;
651 char *keystr;
652 unsigned long rec_id = 0;
653 struct idmap_tdb2_context *ctx;
654 TALLOC_CTX *tmp_ctx = talloc_stackframe();
656 ret = idmap_tdb2_open_db(dom);
657 NT_STATUS_NOT_OK_RETURN(ret);
659 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
661 keystr = sid_string_talloc(tmp_ctx, map->sid);
662 if (keystr == NULL) {
663 DEBUG(0, ("Out of memory!\n"));
664 ret = NT_STATUS_NO_MEMORY;
665 goto done;
668 DEBUG(10,("Fetching record %s\n", keystr));
670 /* Check if sid is present in database */
671 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
672 if (!data.dptr) {
673 char *idstr;
674 struct idmap_tdb2_set_mapping_context store_state;
676 DEBUG(10,(__location__ " Record %s not found\n", keystr));
678 if (ctx->script == NULL) {
679 ret = NT_STATUS_NONE_MAPPED;
680 goto done;
683 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
684 /* store it on shared storage */
685 if (!NT_STATUS_IS_OK(ret)) {
686 goto done;
689 /* apply filters before returning result */
690 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
691 DEBUG(5, ("Script returned id (%u) out of range "
692 "(%u - %u). Filtered!\n",
693 map->xid.id, dom->low_id, dom->high_id));
694 ret = NT_STATUS_NONE_MAPPED;
695 goto done;
698 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
699 map->xid.type == ID_TYPE_UID?'U':'G',
700 (unsigned long)map->xid.id);
701 if (idstr == NULL) {
702 ret = NT_STATUS_NO_MEMORY;
703 goto done;
706 store_state.ksidstr = keystr;
707 store_state.kidstr = idstr;
709 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
710 &store_state);
711 goto done;
714 /* What type of record is this ? */
715 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
716 map->xid.id = rec_id;
717 map->xid.type = ID_TYPE_UID;
718 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
719 ret = NT_STATUS_OK;
721 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
722 map->xid.id = rec_id;
723 map->xid.type = ID_TYPE_GID;
724 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
725 ret = NT_STATUS_OK;
727 } else { /* Unknown record type ! */
728 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
729 ret = NT_STATUS_INTERNAL_DB_ERROR;
730 goto done;
733 /* apply filters before returning result */
734 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
735 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
736 map->xid.id, dom->low_id, dom->high_id));
737 ret = NT_STATUS_NONE_MAPPED;
740 done:
741 talloc_free(tmp_ctx);
742 return ret;
746 lookup a set of unix ids.
748 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
750 NTSTATUS ret;
751 int i;
753 /* initialize the status to avoid suprise */
754 for (i = 0; ids[i]; i++) {
755 ids[i]->status = ID_UNKNOWN;
758 for (i = 0; ids[i]; i++) {
759 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
760 if ( ! NT_STATUS_IS_OK(ret)) {
762 /* if it is just a failed mapping continue */
763 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
765 /* make sure it is marked as unmapped */
766 ids[i]->status = ID_UNMAPPED;
767 continue;
770 /* some fatal error occurred, return immediately */
771 goto done;
774 /* all ok, id is mapped */
775 ids[i]->status = ID_MAPPED;
778 ret = NT_STATUS_OK;
780 done:
781 return ret;
785 lookup a set of sids.
788 struct idmap_tdb2_sids_to_unixids_context {
789 struct idmap_domain *dom;
790 struct id_map **ids;
791 bool allocate_unmapped;
794 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
795 void *private_data)
797 struct idmap_tdb2_sids_to_unixids_context *state;
798 int i;
799 NTSTATUS ret = NT_STATUS_OK;
801 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
803 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
804 " domain: [%s], allocate: %s\n",
805 state->dom->name,
806 state->allocate_unmapped ? "yes" : "no"));
808 for (i = 0; state->ids[i]; i++) {
809 if ((state->ids[i]->status == ID_UNKNOWN) ||
810 /* retry if we could not map in previous run: */
811 (state->ids[i]->status == ID_UNMAPPED))
813 NTSTATUS ret2;
815 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
816 if (!NT_STATUS_IS_OK(ret2)) {
818 /* if it is just a failed mapping, continue */
819 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
821 /* make sure it is marked as unmapped */
822 state->ids[i]->status = ID_UNMAPPED;
823 ret = STATUS_SOME_UNMAPPED;
824 } else {
825 /* some fatal error occurred, return immediately */
826 ret = ret2;
827 goto done;
829 } else {
830 /* all ok, id is mapped */
831 state->ids[i]->status = ID_MAPPED;
835 if ((state->ids[i]->status == ID_UNMAPPED) &&
836 state->allocate_unmapped)
838 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
839 if (!NT_STATUS_IS_OK(ret)) {
840 goto done;
845 done:
846 return ret;
849 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
851 NTSTATUS ret;
852 int i;
853 struct idmap_tdb2_sids_to_unixids_context state;
854 struct idmap_tdb2_context *ctx;
856 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
858 /* initialize the status to avoid suprise */
859 for (i = 0; ids[i]; i++) {
860 ids[i]->status = ID_UNKNOWN;
863 state.dom = dom;
864 state.ids = ids;
865 state.allocate_unmapped = false;
867 ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
869 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
870 state.allocate_unmapped = true;
871 ret = dbwrap_trans_do(ctx->db,
872 idmap_tdb2_sids_to_unixids_action,
873 &state);
876 return ret;
880 static struct idmap_methods db_methods = {
881 .init = idmap_tdb2_db_init,
882 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
883 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
884 .allocate_id = idmap_tdb2_get_new_id
887 NTSTATUS idmap_tdb2_init(void)
889 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);