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 struct idmap_tdb2_context {
40 uint32_t filter_low_id;
41 uint32_t filter_high_id;
42 const char *script; /* script to provide idmaps */
45 /* High water mark keys */
46 #define HWM_GROUP "GROUP HWM"
47 #define HWM_USER "USER HWM"
49 static struct idmap_tdb2_state {
50 /* User and group id pool */
51 uid_t low_uid, high_uid; /* Range of uids to allocate */
52 gid_t low_gid, high_gid; /* Range of gids to allocate */
56 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom,
59 /* handle to the permanent tdb */
60 static struct db_context *idmap_tdb2;
62 static NTSTATUS idmap_tdb2_alloc_load(void);
64 static NTSTATUS idmap_tdb2_load_ranges(void)
71 if (!lp_idmap_uid(&low_uid, &high_uid)) {
72 DEBUG(1, ("idmap uid missing\n"));
73 return NT_STATUS_UNSUCCESSFUL;
76 if (!lp_idmap_gid(&low_gid, &high_gid)) {
77 DEBUG(1, ("idmap gid missing\n"));
78 return NT_STATUS_UNSUCCESSFUL;
81 idmap_tdb2_state.low_uid = low_uid;
82 idmap_tdb2_state.high_uid = high_uid;
83 idmap_tdb2_state.low_gid = low_gid;
84 idmap_tdb2_state.high_gid = high_gid;
86 if (idmap_tdb2_state.high_uid <= idmap_tdb2_state.low_uid) {
87 DEBUG(1, ("idmap uid range missing or invalid\n"));
88 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
89 return NT_STATUS_UNSUCCESSFUL;
92 if (idmap_tdb2_state.high_gid <= idmap_tdb2_state.low_gid) {
93 DEBUG(1, ("idmap gid range missing or invalid\n"));
94 DEBUGADD(1, ("idmap will be unable to map foreign SIDs\n"));
95 return NT_STATUS_UNSUCCESSFUL;
102 open the permanent tdb
104 static NTSTATUS idmap_tdb2_open_db(void)
109 /* its already open */
113 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
114 if (db_path == NULL) {
115 /* fall back to the private directory, which, despite
116 its name, is usually on shared storage */
117 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
119 NT_STATUS_HAVE_NO_MEMORY(db_path);
121 /* Open idmap repository */
122 idmap_tdb2 = db_open(NULL, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
123 TALLOC_FREE(db_path);
125 if (idmap_tdb2 == NULL) {
126 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
128 return NT_STATUS_UNSUCCESSFUL;
131 /* load the ranges and high/low water marks */
132 return idmap_tdb2_alloc_load();
137 load the idmap allocation ranges and high/low water marks
139 static NTSTATUS idmap_tdb2_alloc_load(void)
146 status = idmap_tdb2_load_ranges();
147 if (!NT_STATUS_IS_OK(status)) {
151 /* Create high water marks for group and user id */
153 low_id = dbwrap_fetch_int32(idmap_tdb2, HWM_USER);
154 if ((low_id == -1) || (low_id < idmap_tdb2_state.low_uid)) {
155 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
156 idmap_tdb2, HWM_USER,
157 idmap_tdb2_state.low_uid))) {
158 DEBUG(0, ("Unable to initialise user hwm in idmap "
160 return NT_STATUS_INTERNAL_DB_ERROR;
164 low_id = dbwrap_fetch_int32(idmap_tdb2, HWM_GROUP);
165 if ((low_id == -1) || (low_id < idmap_tdb2_state.low_gid)) {
166 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
167 idmap_tdb2, HWM_GROUP,
168 idmap_tdb2_state.low_gid))) {
169 DEBUG(0, ("Unable to initialise group hwm in idmap "
171 return NT_STATUS_INTERNAL_DB_ERROR;
183 struct idmap_tdb2_allocate_id_context {
190 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
194 struct idmap_tdb2_allocate_id_context *state;
197 state = (struct idmap_tdb2_allocate_id_context *)private_data;
199 hwm = dbwrap_fetch_int32(db, state->hwmkey);
201 ret = NT_STATUS_INTERNAL_DB_ERROR;
205 /* check it is in the range */
206 if (hwm > state->high_hwm) {
207 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
208 state->hwmtype, (unsigned long)state->high_hwm));
209 ret = NT_STATUS_UNSUCCESSFUL;
213 /* fetch a new id and increment it */
214 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
215 if (!NT_STATUS_IS_OK(ret)) {
216 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
221 /* recheck it is in the range */
222 if (hwm > state->high_hwm) {
223 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
224 state->hwmtype, (unsigned long)state->high_hwm));
225 ret = NT_STATUS_UNSUCCESSFUL;
236 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
244 struct idmap_tdb2_context *ctx;
245 struct idmap_tdb2_allocate_id_context state;
247 status = idmap_tdb2_open_db();
248 NT_STATUS_NOT_OK_RETURN(status);
250 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
252 /* Get current high water mark */
266 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
267 return NT_STATUS_INVALID_PARAMETER;
270 high_hwm = ctx->filter_high_id;
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(dom, id);
315 IDMAP MAPPING TDB BACKEND
319 Initialise idmap database.
321 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom,
325 struct idmap_tdb2_context *ctx;
328 ctx = talloc(dom, struct idmap_tdb2_context);
330 DEBUG(0, ("Out of memory!\n"));
331 return NT_STATUS_NO_MEMORY;
334 if (strequal(dom->name, "*")) {
340 ctx->filter_low_id = 0;
341 ctx->filter_high_id = 0;
343 if (lp_idmap_uid(&low_uid, &high_uid)) {
344 ctx->filter_low_id = low_uid;
345 ctx->filter_high_id = high_uid;
347 DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
350 if (lp_idmap_gid(&low_gid, &high_gid)) {
351 if ((low_gid != low_uid) || (high_gid != high_uid)) {
352 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
353 " ranges do not agree -- building "
355 ctx->filter_low_id = MAX(ctx->filter_low_id,
357 ctx->filter_high_id = MIN(ctx->filter_high_id,
361 DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
364 ctx->script = lp_parm_const_string(-1, "idmap", "script", NULL);
366 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
369 char *config_option = NULL;
371 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
372 if ( ! config_option) {
373 DEBUG(0, ("Out of memory!\n"));
374 ret = NT_STATUS_NO_MEMORY;
378 range = lp_parm_const_string(-1, config_option, "range", NULL);
380 (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2))
382 ctx->filter_low_id = 0;
383 ctx->filter_high_id = 0;
386 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
388 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
391 talloc_free(config_option);
394 if (ctx->filter_low_id > ctx->filter_high_id) {
395 ctx->filter_low_id = 0;
396 ctx->filter_high_id = 0;
399 ret = idmap_tdb2_open_db();
400 if (!NT_STATUS_IS_OK(ret)) {
404 dom->private_data = ctx;
413 struct idmap_tdb2_set_mapping_context {
418 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
423 struct idmap_tdb2_set_mapping_context *state;
424 TALLOC_CTX *tmp_ctx = talloc_stackframe();
426 state = (struct idmap_tdb2_set_mapping_context *)private_data;
428 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
430 /* check wheter sid mapping is already present in db */
431 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
433 ret = NT_STATUS_OBJECT_NAME_COLLISION;
437 ret = dbwrap_store_bystring(db, state->ksidstr,
438 string_term_tdb_data(state->kidstr),
440 if (!NT_STATUS_IS_OK(ret)) {
441 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
445 ret = dbwrap_store_bystring(db, state->kidstr,
446 string_term_tdb_data(state->ksidstr),
448 if (!NT_STATUS_IS_OK(ret)) {
449 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
450 /* try to remove the previous stored SID -> ID map */
451 dbwrap_delete_bystring(db, state->ksidstr);
455 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
458 talloc_free(tmp_ctx);
464 run a script to perform a mapping
466 The script should the following command lines:
472 and should return one of the following as a single line of text
478 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
479 const char *fmt, ...)
487 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
488 NT_STATUS_HAVE_NO_MEMORY(cmd);
491 cmd = talloc_vasprintf_append(cmd, fmt, ap);
493 NT_STATUS_HAVE_NO_MEMORY(cmd);
498 return NT_STATUS_NONE_MAPPED;
501 if (fgets(line, sizeof(line)-1, p) == NULL) {
503 return NT_STATUS_NONE_MAPPED;
507 DEBUG(10,("idmap script gave: %s\n", line));
509 if (sscanf(line, "UID:%lu", &v) == 1) {
511 map->xid.type = ID_TYPE_UID;
512 } else if (sscanf(line, "GID:%lu", &v) == 1) {
514 map->xid.type = ID_TYPE_GID;
515 } else if (strncmp(line, "SID:S-", 6) == 0) {
516 if (!string_to_sid(map->sid, &line[4])) {
517 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
519 return NT_STATUS_NONE_MAPPED;
522 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
524 return NT_STATUS_NONE_MAPPED;
533 Single id to sid lookup function.
535 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
541 struct idmap_tdb2_context *ctx;
544 status = idmap_tdb2_open_db();
545 NT_STATUS_NOT_OK_RETURN(status);
548 return NT_STATUS_INVALID_PARAMETER;
551 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
553 /* apply filters before checking */
554 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
555 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
556 map->xid.id, dom->low_id, dom->high_id));
557 return NT_STATUS_NONE_MAPPED;
560 switch (map->xid.type) {
563 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
567 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
571 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
572 return NT_STATUS_INVALID_PARAMETER;
575 /* final SAFE_FREE safe */
578 if (keystr == NULL) {
579 DEBUG(0, ("Out of memory!\n"));
580 ret = NT_STATUS_NO_MEMORY;
584 DEBUG(10,("Fetching record %s\n", keystr));
586 /* Check if the mapping exists */
587 data = dbwrap_fetch_bystring(idmap_tdb2, keystr, keystr);
591 struct idmap_tdb2_set_mapping_context store_state;
593 DEBUG(10,("Record %s not found\n", keystr));
594 if (ctx->script == NULL) {
595 ret = NT_STATUS_NONE_MAPPED;
599 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
601 /* store it on shared storage */
602 if (!NT_STATUS_IS_OK(ret)) {
606 sidstr = sid_string_talloc(keystr, map->sid);
608 ret = NT_STATUS_NO_MEMORY;
612 store_state.ksidstr = sidstr;
613 store_state.kidstr = keystr;
615 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
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;
627 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
637 Single sid to id lookup function.
639 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
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();
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;
660 DEBUG(10,("Fetching record %s\n", keystr));
662 /* Check if sid is present in database */
663 data = dbwrap_fetch_bystring(idmap_tdb2, tmp_ctx, keystr);
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;
675 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
676 /* store it on shared storage */
677 if (!NT_STATUS_IS_OK(ret)) {
681 /* apply filters before returning result */
682 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
683 DEBUG(5, ("Script returned id (%u) out of range "
684 "(%u - %u). Filtered!\n",
685 map->xid.id, dom->low_id, dom->high_id));
686 ret = NT_STATUS_NONE_MAPPED;
690 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
691 map->xid.type == ID_TYPE_UID?'U':'G',
692 (unsigned long)map->xid.id);
694 ret = NT_STATUS_NO_MEMORY;
698 store_state.ksidstr = keystr;
699 store_state.kidstr = idstr;
701 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
706 /* What type of record is this ? */
707 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
708 map->xid.id = rec_id;
709 map->xid.type = ID_TYPE_UID;
710 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
713 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
714 map->xid.id = rec_id;
715 map->xid.type = ID_TYPE_GID;
716 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
719 } else { /* Unknown record type ! */
720 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
721 ret = NT_STATUS_INTERNAL_DB_ERROR;
725 /* apply filters before returning result */
726 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
727 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
728 map->xid.id, dom->low_id, dom->high_id));
729 ret = NT_STATUS_NONE_MAPPED;
733 talloc_free(tmp_ctx);
738 lookup a set of unix ids.
740 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
745 /* initialize the status to avoid suprise */
746 for (i = 0; ids[i]; i++) {
747 ids[i]->status = ID_UNKNOWN;
750 for (i = 0; ids[i]; i++) {
751 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
752 if ( ! NT_STATUS_IS_OK(ret)) {
754 /* if it is just a failed mapping continue */
755 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
757 /* make sure it is marked as unmapped */
758 ids[i]->status = ID_UNMAPPED;
762 /* some fatal error occurred, return immediately */
766 /* all ok, id is mapped */
767 ids[i]->status = ID_MAPPED;
777 lookup a set of sids.
780 struct idmap_tdb2_sids_to_unixids_context {
781 struct idmap_domain *dom;
783 bool allocate_unmapped;
786 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
789 struct idmap_tdb2_sids_to_unixids_context *state;
791 NTSTATUS ret = NT_STATUS_OK;
793 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
795 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
796 " domain: [%s], allocate: %s\n",
798 state->allocate_unmapped ? "yes" : "no"));
800 for (i = 0; state->ids[i]; i++) {
801 if ((state->ids[i]->status == ID_UNKNOWN) ||
802 /* retry if we could not map in previous run: */
803 (state->ids[i]->status == ID_UNMAPPED))
807 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
808 if (!NT_STATUS_IS_OK(ret2)) {
810 /* if it is just a failed mapping, continue */
811 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
813 /* make sure it is marked as unmapped */
814 state->ids[i]->status = ID_UNMAPPED;
815 ret = STATUS_SOME_UNMAPPED;
817 /* some fatal error occurred, return immediately */
822 /* all ok, id is mapped */
823 state->ids[i]->status = ID_MAPPED;
827 if ((state->ids[i]->status == ID_UNMAPPED) &&
828 state->allocate_unmapped)
830 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
831 if (!NT_STATUS_IS_OK(ret)) {
841 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
845 struct idmap_tdb2_sids_to_unixids_context state;
847 /* initialize the status to avoid suprise */
848 for (i = 0; ids[i]; i++) {
849 ids[i]->status = ID_UNKNOWN;
854 state.allocate_unmapped = false;
856 ret = idmap_tdb2_sids_to_unixids_action(idmap_tdb2, &state);
858 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED)) {
859 state.allocate_unmapped = true;
860 ret = dbwrap_trans_do(idmap_tdb2,
861 idmap_tdb2_sids_to_unixids_action,
873 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
875 struct idmap_tdb2_context *ctx;
877 char *ksidstr, *kidstr;
878 struct idmap_tdb2_set_mapping_context state;
880 if (!map || !map->sid) {
881 return NT_STATUS_INVALID_PARAMETER;
884 ksidstr = kidstr = NULL;
886 /* TODO: should we filter a set_mapping using low/high filters ? */
888 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
890 switch (map->xid.type) {
893 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
897 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
901 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
902 return NT_STATUS_INVALID_PARAMETER;
905 if (kidstr == NULL) {
906 DEBUG(0, ("ERROR: Out of memory!\n"));
907 ret = NT_STATUS_NO_MEMORY;
911 ksidstr = sid_string_talloc(ctx, map->sid);
912 if (ksidstr == NULL) {
913 DEBUG(0, ("Out of memory!\n"));
914 ret = NT_STATUS_NO_MEMORY;
918 state.ksidstr = ksidstr;
919 state.kidstr = kidstr;
921 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
925 talloc_free(ksidstr);
931 * Create a new mapping for an unmapped SID, also allocating a new ID.
932 * This should be run inside a transaction.
935 * Properly integrate this with multi domain idmap config:
936 * Currently, the allocator is default-config only.
938 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
943 TALLOC_CTX *mem_ctx = talloc_stackframe();
946 ret = NT_STATUS_INVALID_PARAMETER;
950 if ((map->xid.type != ID_TYPE_UID) && (map->xid.type != ID_TYPE_GID)) {
951 ret = NT_STATUS_INVALID_PARAMETER;
955 if (map->sid == NULL) {
956 ret = NT_STATUS_INVALID_PARAMETER;
960 /* check wheter the SID is already mapped in the db */
961 sidstr = sid_string_talloc(mem_ctx, map->sid);
962 if (sidstr == NULL) {
963 DEBUG(0, ("Out of memory!\n"));
964 ret = NT_STATUS_NO_MEMORY;
968 data = dbwrap_fetch_bystring(idmap_tdb2, mem_ctx, sidstr);
970 ret = NT_STATUS_OBJECT_NAME_COLLISION;
974 /* unmapped - get a new id */
975 ret = idmap_tdb2_get_new_id(dom, &map->xid);
976 if (!NT_STATUS_IS_OK(ret)) {
977 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(ret)));
981 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
982 sid_string_dbg(map->sid),
983 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
984 (unsigned long)map->xid.id));
986 map->status = ID_MAPPED;
988 /* store the mapping */
989 ret = idmap_tdb2_set_mapping(dom, map);
990 if (!NT_STATUS_IS_OK(ret)) {
991 DEBUG(3, ("Could not store the new mapping: %s\n",
996 talloc_free(mem_ctx);
1001 Close the idmap tdb instance
1003 static NTSTATUS idmap_tdb2_close(struct idmap_domain *dom)
1005 /* don't do anything */
1006 return NT_STATUS_OK;
1009 static struct idmap_methods db_methods = {
1010 .init = idmap_tdb2_db_init,
1011 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
1012 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
1013 .allocate_id = idmap_tdb2_get_new_id,
1014 .close_fn = idmap_tdb2_close
1017 NTSTATUS idmap_tdb2_init(void)
1019 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);