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
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #define DBGC_CLASS DBGC_IDMAP
39 /* High water mark keys */
40 #define HWM_GROUP "GROUP HWM"
41 #define HWM_USER "USER HWM"
43 static struct idmap_tdb2_state {
44 /* User and group id pool */
45 uid_t low_uid, high_uid; /* Range of uids to allocate */
46 gid_t low_gid, high_gid; /* Range of gids to allocate */
47 const char *idmap_script;
51 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom,
54 /* handle to the permanent tdb */
55 static struct db_context *idmap_tdb2;
57 static NTSTATUS idmap_tdb2_alloc_load(void);
59 static NTSTATUS idmap_tdb2_load_ranges(void)
66 if (!lp_idmap_uid(&low_uid, &high_uid)) {
67 DEBUG(1, ("idmap uid missing\n"));
68 return NT_STATUS_UNSUCCESSFUL;
71 if (!lp_idmap_gid(&low_gid, &high_gid)) {
72 DEBUG(1, ("idmap gid missing\n"));
73 return NT_STATUS_UNSUCCESSFUL;
76 idmap_tdb2_state.low_uid = low_uid;
77 idmap_tdb2_state.high_uid = high_uid;
78 idmap_tdb2_state.low_gid = low_gid;
79 idmap_tdb2_state.high_gid = high_gid;
81 if (idmap_tdb2_state.high_uid <= idmap_tdb2_state.low_uid) {
82 DEBUG(1, ("idmap uid range missing or invalid\n"));
83 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
84 return NT_STATUS_UNSUCCESSFUL;
87 if (idmap_tdb2_state.high_gid <= idmap_tdb2_state.low_gid) {
88 DEBUG(1, ("idmap gid range missing or invalid\n"));
89 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
90 return NT_STATUS_UNSUCCESSFUL;
97 open the permanent tdb
99 static NTSTATUS idmap_tdb2_open_db(void)
104 /* its already open */
108 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
109 if (db_path == NULL) {
110 /* fall back to the private directory, which, despite
111 its name, is usually on shared storage */
112 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
114 NT_STATUS_HAVE_NO_MEMORY(db_path);
116 /* Open idmap repository */
117 idmap_tdb2 = db_open(NULL, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
118 TALLOC_FREE(db_path);
120 if (idmap_tdb2 == NULL) {
121 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
123 return NT_STATUS_UNSUCCESSFUL;
126 /* load the ranges and high/low water marks */
127 return idmap_tdb2_alloc_load();
132 load the idmap allocation ranges and high/low water marks
134 static NTSTATUS idmap_tdb2_alloc_load(void)
139 /* see if a idmap script is configured */
140 idmap_tdb2_state.idmap_script = lp_parm_const_string(-1, "idmap",
143 if (idmap_tdb2_state.idmap_script) {
144 DEBUG(1, ("using idmap script '%s'\n",
145 idmap_tdb2_state.idmap_script));
150 status = idmap_tdb2_load_ranges();
151 if (!NT_STATUS_IS_OK(status)) {
155 /* Create high water marks for group and user id */
157 low_id = dbwrap_fetch_int32(idmap_tdb2, HWM_USER);
158 if ((low_id == -1) || (low_id < idmap_tdb2_state.low_uid)) {
159 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
160 idmap_tdb2, HWM_USER,
161 idmap_tdb2_state.low_uid))) {
162 DEBUG(0, ("Unable to initialise user hwm in idmap "
164 return NT_STATUS_INTERNAL_DB_ERROR;
168 low_id = dbwrap_fetch_int32(idmap_tdb2, HWM_GROUP);
169 if ((low_id == -1) || (low_id < idmap_tdb2_state.low_gid)) {
170 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
171 idmap_tdb2, HWM_GROUP,
172 idmap_tdb2_state.low_gid))) {
173 DEBUG(0, ("Unable to initialise group hwm in idmap "
175 return NT_STATUS_INTERNAL_DB_ERROR;
187 struct idmap_tdb2_allocate_id_context {
194 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
198 struct idmap_tdb2_allocate_id_context *state;
201 state = (struct idmap_tdb2_allocate_id_context *)private_data;
203 hwm = dbwrap_fetch_int32(db, state->hwmkey);
205 ret = NT_STATUS_INTERNAL_DB_ERROR;
209 /* check it is in the range */
210 if (hwm > state->high_hwm) {
211 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
212 state->hwmtype, (unsigned long)state->high_hwm));
213 ret = NT_STATUS_UNSUCCESSFUL;
217 /* fetch a new id and increment it */
218 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
219 if (!NT_STATUS_IS_OK(ret)) {
220 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
225 /* recheck it is in the range */
226 if (hwm > state->high_hwm) {
227 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
228 state->hwmtype, (unsigned long)state->high_hwm));
229 ret = NT_STATUS_UNSUCCESSFUL;
240 static NTSTATUS idmap_tdb2_allocate_id(struct unixid *xid)
247 struct idmap_tdb2_allocate_id_context state;
249 status = idmap_tdb2_open_db();
250 NT_STATUS_NOT_OK_RETURN(status);
252 /* Get current high water mark */
258 high_hwm = idmap_tdb2_state.high_uid;
264 high_hwm = idmap_tdb2_state.high_gid;
268 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
269 return NT_STATUS_INVALID_PARAMETER;
273 state.high_hwm = high_hwm;
274 state.hwmtype = hwmtype;
275 state.hwmkey = hwmkey;
277 status = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_allocate_id_action,
280 if (NT_STATUS_IS_OK(status)) {
282 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
284 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
291 * Allocate a new unix-ID.
292 * For now this is for the default idmap domain only.
293 * Should be extended later on.
295 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
300 if (!strequal(dom->name, "*")) {
301 DEBUG(3, ("idmap_tdb2_get_new_id: "
302 "Refusing creation of mapping for domain'%s'. "
303 "Currently only supported for the default "
306 return NT_STATUS_NOT_IMPLEMENTED;
309 ret = idmap_tdb2_allocate_id(id);
315 IDMAP MAPPING TDB BACKEND
317 struct idmap_tdb2_context {
318 uint32_t filter_low_id;
319 uint32_t filter_high_id;
323 Initialise idmap database.
325 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom,
329 struct idmap_tdb2_context *ctx;
332 status = idmap_tdb2_open_db();
333 NT_STATUS_NOT_OK_RETURN(status);
335 ctx = talloc(dom, struct idmap_tdb2_context);
337 DEBUG(0, ("Out of memory!\n"));
338 return NT_STATUS_NO_MEMORY;
341 if (strequal(dom->name, "*")) {
347 ctx->filter_low_id = 0;
348 ctx->filter_high_id = 0;
350 if (lp_idmap_uid(&low_uid, &high_uid)) {
351 ctx->filter_low_id = low_uid;
352 ctx->filter_high_id = high_uid;
354 DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
357 if (lp_idmap_gid(&low_gid, &high_gid)) {
358 if ((low_gid != low_uid) || (high_gid != high_uid)) {
359 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
360 " ranges do not agree -- building "
362 ctx->filter_low_id = MAX(ctx->filter_low_id,
364 ctx->filter_high_id = MIN(ctx->filter_high_id,
368 DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
371 char *config_option = NULL;
373 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
374 if ( ! config_option) {
375 DEBUG(0, ("Out of memory!\n"));
376 ret = NT_STATUS_NO_MEMORY;
380 range = lp_parm_const_string(-1, config_option, "range", NULL);
382 (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2))
384 ctx->filter_low_id = 0;
385 ctx->filter_high_id = 0;
388 talloc_free(config_option);
391 if (ctx->filter_low_id > ctx->filter_high_id) {
392 ctx->filter_low_id = 0;
393 ctx->filter_high_id = 0;
396 dom->private_data = ctx;
405 struct idmap_tdb2_set_mapping_context {
410 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
415 struct idmap_tdb2_set_mapping_context *state;
416 TALLOC_CTX *tmp_ctx = talloc_stackframe();
418 state = (struct idmap_tdb2_set_mapping_context *)private_data;
420 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
422 /* check wheter sid mapping is already present in db */
423 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
425 ret = NT_STATUS_OBJECT_NAME_COLLISION;
429 ret = dbwrap_store_bystring(db, state->ksidstr,
430 string_term_tdb_data(state->kidstr),
432 if (!NT_STATUS_IS_OK(ret)) {
433 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
437 ret = dbwrap_store_bystring(db, state->kidstr,
438 string_term_tdb_data(state->ksidstr),
440 if (!NT_STATUS_IS_OK(ret)) {
441 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
442 /* try to remove the previous stored SID -> ID map */
443 dbwrap_delete_bystring(db, state->ksidstr);
447 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
450 talloc_free(tmp_ctx);
456 run a script to perform a mapping
458 The script should the following command lines:
464 and should return one of the following as a single line of text
470 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
471 const char *fmt, ...)
479 cmd = talloc_asprintf(ctx, "%s ", idmap_tdb2_state.idmap_script);
480 NT_STATUS_HAVE_NO_MEMORY(cmd);
483 cmd = talloc_vasprintf_append(cmd, fmt, ap);
485 NT_STATUS_HAVE_NO_MEMORY(cmd);
490 return NT_STATUS_NONE_MAPPED;
493 if (fgets(line, sizeof(line)-1, p) == NULL) {
495 return NT_STATUS_NONE_MAPPED;
499 DEBUG(10,("idmap script gave: %s\n", line));
501 if (sscanf(line, "UID:%lu", &v) == 1) {
503 map->xid.type = ID_TYPE_UID;
504 } else if (sscanf(line, "GID:%lu", &v) == 1) {
506 map->xid.type = ID_TYPE_GID;
507 } else if (strncmp(line, "SID:S-", 6) == 0) {
508 if (!string_to_sid(map->sid, &line[4])) {
509 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
510 line, idmap_tdb2_state.idmap_script));
511 return NT_STATUS_NONE_MAPPED;
514 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
515 line, idmap_tdb2_state.idmap_script));
516 return NT_STATUS_NONE_MAPPED;
525 Single id to sid lookup function.
527 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_tdb2_context *ctx, struct id_map *map)
534 status = idmap_tdb2_open_db();
535 NT_STATUS_NOT_OK_RETURN(status);
538 return NT_STATUS_INVALID_PARAMETER;
541 /* apply filters before checking */
542 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
543 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
544 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
545 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
546 return NT_STATUS_NONE_MAPPED;
549 switch (map->xid.type) {
552 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
556 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
560 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
561 return NT_STATUS_INVALID_PARAMETER;
564 /* final SAFE_FREE safe */
567 if (keystr == NULL) {
568 DEBUG(0, ("Out of memory!\n"));
569 ret = NT_STATUS_NO_MEMORY;
573 DEBUG(10,("Fetching record %s\n", keystr));
575 /* Check if the mapping exists */
576 data = dbwrap_fetch_bystring(idmap_tdb2, keystr, keystr);
580 struct idmap_tdb2_set_mapping_context store_state;
582 DEBUG(10,("Record %s not found\n", keystr));
583 if (idmap_tdb2_state.idmap_script == NULL) {
584 ret = NT_STATUS_NONE_MAPPED;
588 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
590 /* store it on shared storage */
591 if (!NT_STATUS_IS_OK(ret)) {
595 sidstr = sid_string_talloc(keystr, map->sid);
597 ret = NT_STATUS_NO_MEMORY;
601 store_state.ksidstr = sidstr;
602 store_state.kidstr = keystr;
604 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
609 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
610 DEBUG(10,("INVALID SID (%s) in record %s\n",
611 (const char *)data.dptr, keystr));
612 ret = NT_STATUS_INTERNAL_DB_ERROR;
616 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
626 Single sid to id lookup function.
628 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_tdb2_context *ctx, struct id_map *map)
633 unsigned long rec_id = 0;
634 TALLOC_CTX *tmp_ctx = talloc_stackframe();
636 ret = idmap_tdb2_open_db();
637 NT_STATUS_NOT_OK_RETURN(ret);
639 keystr = sid_string_talloc(tmp_ctx, map->sid);
640 if (keystr == NULL) {
641 DEBUG(0, ("Out of memory!\n"));
642 ret = NT_STATUS_NO_MEMORY;
646 DEBUG(10,("Fetching record %s\n", keystr));
648 /* Check if sid is present in database */
649 data = dbwrap_fetch_bystring(idmap_tdb2, tmp_ctx, keystr);
652 struct idmap_tdb2_set_mapping_context store_state;
654 DEBUG(10,(__location__ " Record %s not found\n", keystr));
656 if (idmap_tdb2_state.idmap_script == NULL) {
657 ret = NT_STATUS_NONE_MAPPED;
661 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
662 /* store it on shared storage */
663 if (!NT_STATUS_IS_OK(ret)) {
667 /* apply filters before returning result */
668 if ((ctx->filter_low_id
669 && (map->xid.id < ctx->filter_low_id)) ||
671 && (map->xid.id > ctx->filter_high_id))) {
672 DEBUG(5, ("Script returned id (%u) out of range "
673 "(%u - %u). Filtered!\n",
675 ctx->filter_low_id, ctx->filter_high_id));
676 ret = NT_STATUS_NONE_MAPPED;
680 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
681 map->xid.type == ID_TYPE_UID?'U':'G',
682 (unsigned long)map->xid.id);
684 ret = NT_STATUS_NO_MEMORY;
688 store_state.ksidstr = keystr;
689 store_state.kidstr = idstr;
691 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
696 /* What type of record is this ? */
697 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
698 map->xid.id = rec_id;
699 map->xid.type = ID_TYPE_UID;
700 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
703 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
704 map->xid.id = rec_id;
705 map->xid.type = ID_TYPE_GID;
706 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
709 } else { /* Unknown record type ! */
710 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
711 ret = NT_STATUS_INTERNAL_DB_ERROR;
715 /* apply filters before returning result */
716 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
717 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
718 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
719 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
720 ret = NT_STATUS_NONE_MAPPED;
724 talloc_free(tmp_ctx);
729 lookup a set of unix ids.
731 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
733 struct idmap_tdb2_context *ctx;
737 /* initialize the status to avoid suprise */
738 for (i = 0; ids[i]; i++) {
739 ids[i]->status = ID_UNKNOWN;
742 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
744 for (i = 0; ids[i]; i++) {
745 ret = idmap_tdb2_id_to_sid(ctx, ids[i]);
746 if ( ! NT_STATUS_IS_OK(ret)) {
748 /* if it is just a failed mapping continue */
749 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
751 /* make sure it is marked as unmapped */
752 ids[i]->status = ID_UNMAPPED;
756 /* some fatal error occurred, return immediately */
760 /* all ok, id is mapped */
761 ids[i]->status = ID_MAPPED;
771 lookup a set of sids.
774 struct idmap_tdb2_sids_to_unixids_context {
775 struct idmap_domain *dom;
777 bool allocate_unmapped;
780 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
783 struct idmap_tdb2_sids_to_unixids_context *state;
785 struct idmap_tdb2_context *ctx;
786 NTSTATUS ret = NT_STATUS_OK;
788 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
790 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
791 " domain: [%s], allocate: %s\n",
793 state->allocate_unmapped ? "yes" : "no"));
795 ctx = talloc_get_type(state->dom->private_data,
796 struct idmap_tdb2_context);
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))
806 ret2 = idmap_tdb2_sid_to_id(ctx, 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;
816 /* some fatal error occurred, return immediately */
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)) {
840 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
844 struct idmap_tdb2_sids_to_unixids_context state;
846 /* initialize the status to avoid suprise */
847 for (i = 0; ids[i]; i++) {
848 ids[i]->status = ID_UNKNOWN;
853 state.allocate_unmapped = false;
855 ret = idmap_tdb2_sids_to_unixids_action(idmap_tdb2, &state);
857 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED)) {
858 state.allocate_unmapped = true;
859 ret = dbwrap_trans_do(idmap_tdb2,
860 idmap_tdb2_sids_to_unixids_action,
872 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
874 struct idmap_tdb2_context *ctx;
876 char *ksidstr, *kidstr;
877 struct idmap_tdb2_set_mapping_context state;
879 if (!map || !map->sid) {
880 return NT_STATUS_INVALID_PARAMETER;
883 ksidstr = kidstr = NULL;
885 /* TODO: should we filter a set_mapping using low/high filters ? */
887 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
889 switch (map->xid.type) {
892 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
896 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
900 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
901 return NT_STATUS_INVALID_PARAMETER;
904 if (kidstr == NULL) {
905 DEBUG(0, ("ERROR: Out of memory!\n"));
906 ret = NT_STATUS_NO_MEMORY;
910 ksidstr = sid_string_talloc(ctx, map->sid);
911 if (ksidstr == NULL) {
912 DEBUG(0, ("Out of memory!\n"));
913 ret = NT_STATUS_NO_MEMORY;
917 state.ksidstr = ksidstr;
918 state.kidstr = kidstr;
920 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
924 talloc_free(ksidstr);
930 * Create a new mapping for an unmapped SID, also allocating a new ID.
931 * This should be run inside a transaction.
934 * Properly integrate this with multi domain idmap config:
935 * Currently, the allocator is default-config only.
937 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
942 TALLOC_CTX *mem_ctx = talloc_stackframe();
945 ret = NT_STATUS_INVALID_PARAMETER;
949 if ((map->xid.type != ID_TYPE_UID) && (map->xid.type != ID_TYPE_GID)) {
950 ret = NT_STATUS_INVALID_PARAMETER;
954 if (map->sid == NULL) {
955 ret = NT_STATUS_INVALID_PARAMETER;
959 /* check wheter the SID is already mapped in the db */
960 sidstr = sid_string_talloc(mem_ctx, map->sid);
961 if (sidstr == NULL) {
962 DEBUG(0, ("Out of memory!\n"));
963 ret = NT_STATUS_NO_MEMORY;
967 data = dbwrap_fetch_bystring(idmap_tdb2, mem_ctx, sidstr);
969 ret = NT_STATUS_OBJECT_NAME_COLLISION;
973 /* unmapped - get a new id */
974 ret = idmap_tdb2_get_new_id(dom, &map->xid);
975 if (!NT_STATUS_IS_OK(ret)) {
976 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(ret)));
980 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
981 sid_string_dbg(map->sid),
982 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
983 (unsigned long)map->xid.id));
985 map->status = ID_MAPPED;
987 /* store the mapping */
988 ret = idmap_tdb2_set_mapping(dom, map);
989 if (!NT_STATUS_IS_OK(ret)) {
990 DEBUG(3, ("Could not store the new mapping: %s\n",
995 talloc_free(mem_ctx);
1000 Close the idmap tdb instance
1002 static NTSTATUS idmap_tdb2_close(struct idmap_domain *dom)
1004 /* don't do anything */
1005 return NT_STATUS_OK;
1008 static struct idmap_methods db_methods = {
1009 .init = idmap_tdb2_db_init,
1010 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
1011 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
1012 .allocate_id = idmap_tdb2_get_new_id,
1013 .close_fn = idmap_tdb2_close
1016 NTSTATUS idmap_tdb2_init(void)
1018 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);