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 const char *script; /* script to provide idmaps */
43 /* High water mark keys */
44 #define HWM_GROUP "GROUP HWM"
45 #define HWM_USER "USER HWM"
48 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom,
51 /* handle to the permanent tdb */
52 static struct db_context *idmap_tdb2;
56 * check and initialize high/low water marks in the db
58 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
62 /* Create high water marks for group and user id */
64 low_id = dbwrap_fetch_int32(idmap_tdb2, HWM_USER);
65 if ((low_id == -1) || (low_id < dom->low_id)) {
66 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
69 DEBUG(0, ("Unable to initialise user hwm in idmap "
71 return NT_STATUS_INTERNAL_DB_ERROR;
75 low_id = dbwrap_fetch_int32(idmap_tdb2, HWM_GROUP);
76 if ((low_id == -1) || (low_id < dom->low_id)) {
77 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
78 idmap_tdb2, HWM_GROUP,
80 DEBUG(0, ("Unable to initialise group hwm in idmap "
82 return NT_STATUS_INTERNAL_DB_ERROR;
91 open the permanent tdb
93 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
98 /* its already open */
102 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
103 if (db_path == NULL) {
104 /* fall back to the private directory, which, despite
105 its name, is usually on shared storage */
106 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
108 NT_STATUS_HAVE_NO_MEMORY(db_path);
110 /* Open idmap repository */
111 idmap_tdb2 = db_open(NULL, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
112 TALLOC_FREE(db_path);
114 if (idmap_tdb2 == NULL) {
115 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
117 return NT_STATUS_UNSUCCESSFUL;
120 return idmap_tdb2_init_hwm(dom);
128 struct idmap_tdb2_allocate_id_context {
135 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
139 struct idmap_tdb2_allocate_id_context *state;
142 state = (struct idmap_tdb2_allocate_id_context *)private_data;
144 hwm = dbwrap_fetch_int32(db, state->hwmkey);
146 ret = NT_STATUS_INTERNAL_DB_ERROR;
150 /* check it is in the range */
151 if (hwm > state->high_hwm) {
152 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
153 state->hwmtype, (unsigned long)state->high_hwm));
154 ret = NT_STATUS_UNSUCCESSFUL;
158 /* fetch a new id and increment it */
159 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
160 if (!NT_STATUS_IS_OK(ret)) {
161 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
166 /* recheck it is in the range */
167 if (hwm > state->high_hwm) {
168 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
169 state->hwmtype, (unsigned long)state->high_hwm));
170 ret = NT_STATUS_UNSUCCESSFUL;
181 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
189 struct idmap_tdb2_allocate_id_context state;
191 status = idmap_tdb2_open_db(dom);
192 NT_STATUS_NOT_OK_RETURN(status);
194 /* Get current high water mark */
208 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
209 return NT_STATUS_INVALID_PARAMETER;
212 high_hwm = dom->high_id;
215 state.high_hwm = high_hwm;
216 state.hwmtype = hwmtype;
217 state.hwmkey = hwmkey;
219 status = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_allocate_id_action,
222 if (NT_STATUS_IS_OK(status)) {
224 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
226 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
233 * Allocate a new unix-ID.
234 * For now this is for the default idmap domain only.
235 * Should be extended later on.
237 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
242 if (!strequal(dom->name, "*")) {
243 DEBUG(3, ("idmap_tdb2_get_new_id: "
244 "Refusing creation of mapping for domain'%s'. "
245 "Currently only supported for the default "
248 return NT_STATUS_NOT_IMPLEMENTED;
251 ret = idmap_tdb2_allocate_id(dom, id);
257 IDMAP MAPPING TDB BACKEND
261 Initialise idmap database.
263 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom,
267 struct idmap_tdb2_context *ctx;
270 ctx = talloc(dom, struct idmap_tdb2_context);
272 DEBUG(0, ("Out of memory!\n"));
273 return NT_STATUS_NO_MEMORY;
276 if (strequal(dom->name, "*")) {
277 ctx->script = lp_parm_const_string(-1, "idmap", "script", NULL);
279 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
282 char *config_option = NULL;
284 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
285 if ( ! config_option) {
286 DEBUG(0, ("Out of memory!\n"));
287 ret = NT_STATUS_NO_MEMORY;
291 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
293 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
296 talloc_free(config_option);
299 dom->private_data = ctx;
301 ret = idmap_tdb2_open_db(dom);
302 if (!NT_STATUS_IS_OK(ret)) {
313 struct idmap_tdb2_set_mapping_context {
318 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
323 struct idmap_tdb2_set_mapping_context *state;
324 TALLOC_CTX *tmp_ctx = talloc_stackframe();
326 state = (struct idmap_tdb2_set_mapping_context *)private_data;
328 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
330 /* check wheter sid mapping is already present in db */
331 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
333 ret = NT_STATUS_OBJECT_NAME_COLLISION;
337 ret = dbwrap_store_bystring(db, state->ksidstr,
338 string_term_tdb_data(state->kidstr),
340 if (!NT_STATUS_IS_OK(ret)) {
341 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
345 ret = dbwrap_store_bystring(db, state->kidstr,
346 string_term_tdb_data(state->ksidstr),
348 if (!NT_STATUS_IS_OK(ret)) {
349 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
350 /* try to remove the previous stored SID -> ID map */
351 dbwrap_delete_bystring(db, state->ksidstr);
355 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
358 talloc_free(tmp_ctx);
364 run a script to perform a mapping
366 The script should the following command lines:
372 and should return one of the following as a single line of text
378 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
379 const char *fmt, ...)
387 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
388 NT_STATUS_HAVE_NO_MEMORY(cmd);
391 cmd = talloc_vasprintf_append(cmd, fmt, ap);
393 NT_STATUS_HAVE_NO_MEMORY(cmd);
398 return NT_STATUS_NONE_MAPPED;
401 if (fgets(line, sizeof(line)-1, p) == NULL) {
403 return NT_STATUS_NONE_MAPPED;
407 DEBUG(10,("idmap script gave: %s\n", line));
409 if (sscanf(line, "UID:%lu", &v) == 1) {
411 map->xid.type = ID_TYPE_UID;
412 } else if (sscanf(line, "GID:%lu", &v) == 1) {
414 map->xid.type = ID_TYPE_GID;
415 } else if (strncmp(line, "SID:S-", 6) == 0) {
416 if (!string_to_sid(map->sid, &line[4])) {
417 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
419 return NT_STATUS_NONE_MAPPED;
422 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
424 return NT_STATUS_NONE_MAPPED;
433 Single id to sid lookup function.
435 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
441 struct idmap_tdb2_context *ctx;
445 return NT_STATUS_INVALID_PARAMETER;
448 status = idmap_tdb2_open_db(dom);
449 NT_STATUS_NOT_OK_RETURN(status);
451 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
453 /* apply filters before checking */
454 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
455 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
456 map->xid.id, dom->low_id, dom->high_id));
457 return NT_STATUS_NONE_MAPPED;
460 switch (map->xid.type) {
463 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
467 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
471 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
472 return NT_STATUS_INVALID_PARAMETER;
475 /* final SAFE_FREE safe */
478 if (keystr == NULL) {
479 DEBUG(0, ("Out of memory!\n"));
480 ret = NT_STATUS_NO_MEMORY;
484 DEBUG(10,("Fetching record %s\n", keystr));
486 /* Check if the mapping exists */
487 data = dbwrap_fetch_bystring(idmap_tdb2, keystr, keystr);
491 struct idmap_tdb2_set_mapping_context store_state;
493 DEBUG(10,("Record %s not found\n", keystr));
494 if (ctx->script == NULL) {
495 ret = NT_STATUS_NONE_MAPPED;
499 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
501 /* store it on shared storage */
502 if (!NT_STATUS_IS_OK(ret)) {
506 sidstr = sid_string_talloc(keystr, map->sid);
508 ret = NT_STATUS_NO_MEMORY;
512 store_state.ksidstr = sidstr;
513 store_state.kidstr = keystr;
515 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
520 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
521 DEBUG(10,("INVALID SID (%s) in record %s\n",
522 (const char *)data.dptr, keystr));
523 ret = NT_STATUS_INTERNAL_DB_ERROR;
527 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
537 Single sid to id lookup function.
539 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
544 unsigned long rec_id = 0;
545 struct idmap_tdb2_context *ctx;
546 TALLOC_CTX *tmp_ctx = talloc_stackframe();
548 ret = idmap_tdb2_open_db(dom);
549 NT_STATUS_NOT_OK_RETURN(ret);
551 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
553 keystr = sid_string_talloc(tmp_ctx, map->sid);
554 if (keystr == NULL) {
555 DEBUG(0, ("Out of memory!\n"));
556 ret = NT_STATUS_NO_MEMORY;
560 DEBUG(10,("Fetching record %s\n", keystr));
562 /* Check if sid is present in database */
563 data = dbwrap_fetch_bystring(idmap_tdb2, tmp_ctx, keystr);
566 struct idmap_tdb2_set_mapping_context store_state;
568 DEBUG(10,(__location__ " Record %s not found\n", keystr));
570 if (ctx->script == NULL) {
571 ret = NT_STATUS_NONE_MAPPED;
575 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
576 /* store it on shared storage */
577 if (!NT_STATUS_IS_OK(ret)) {
581 /* apply filters before returning result */
582 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
583 DEBUG(5, ("Script returned id (%u) out of range "
584 "(%u - %u). Filtered!\n",
585 map->xid.id, dom->low_id, dom->high_id));
586 ret = NT_STATUS_NONE_MAPPED;
590 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
591 map->xid.type == ID_TYPE_UID?'U':'G',
592 (unsigned long)map->xid.id);
594 ret = NT_STATUS_NO_MEMORY;
598 store_state.ksidstr = keystr;
599 store_state.kidstr = idstr;
601 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
606 /* What type of record is this ? */
607 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
608 map->xid.id = rec_id;
609 map->xid.type = ID_TYPE_UID;
610 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
613 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
614 map->xid.id = rec_id;
615 map->xid.type = ID_TYPE_GID;
616 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
619 } else { /* Unknown record type ! */
620 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
621 ret = NT_STATUS_INTERNAL_DB_ERROR;
625 /* apply filters before returning result */
626 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
627 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
628 map->xid.id, dom->low_id, dom->high_id));
629 ret = NT_STATUS_NONE_MAPPED;
633 talloc_free(tmp_ctx);
638 lookup a set of unix ids.
640 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
645 /* initialize the status to avoid suprise */
646 for (i = 0; ids[i]; i++) {
647 ids[i]->status = ID_UNKNOWN;
650 for (i = 0; ids[i]; i++) {
651 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
652 if ( ! NT_STATUS_IS_OK(ret)) {
654 /* if it is just a failed mapping continue */
655 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
657 /* make sure it is marked as unmapped */
658 ids[i]->status = ID_UNMAPPED;
662 /* some fatal error occurred, return immediately */
666 /* all ok, id is mapped */
667 ids[i]->status = ID_MAPPED;
677 lookup a set of sids.
680 struct idmap_tdb2_sids_to_unixids_context {
681 struct idmap_domain *dom;
683 bool allocate_unmapped;
686 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
689 struct idmap_tdb2_sids_to_unixids_context *state;
691 NTSTATUS ret = NT_STATUS_OK;
693 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
695 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
696 " domain: [%s], allocate: %s\n",
698 state->allocate_unmapped ? "yes" : "no"));
700 for (i = 0; state->ids[i]; i++) {
701 if ((state->ids[i]->status == ID_UNKNOWN) ||
702 /* retry if we could not map in previous run: */
703 (state->ids[i]->status == ID_UNMAPPED))
707 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
708 if (!NT_STATUS_IS_OK(ret2)) {
710 /* if it is just a failed mapping, continue */
711 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
713 /* make sure it is marked as unmapped */
714 state->ids[i]->status = ID_UNMAPPED;
715 ret = STATUS_SOME_UNMAPPED;
717 /* some fatal error occurred, return immediately */
722 /* all ok, id is mapped */
723 state->ids[i]->status = ID_MAPPED;
727 if ((state->ids[i]->status == ID_UNMAPPED) &&
728 state->allocate_unmapped)
730 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
731 if (!NT_STATUS_IS_OK(ret)) {
741 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
745 struct idmap_tdb2_sids_to_unixids_context state;
747 /* initialize the status to avoid suprise */
748 for (i = 0; ids[i]; i++) {
749 ids[i]->status = ID_UNKNOWN;
754 state.allocate_unmapped = false;
756 ret = idmap_tdb2_sids_to_unixids_action(idmap_tdb2, &state);
758 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
759 state.allocate_unmapped = true;
760 ret = dbwrap_trans_do(idmap_tdb2,
761 idmap_tdb2_sids_to_unixids_action,
773 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
775 struct idmap_tdb2_context *ctx;
777 char *ksidstr, *kidstr;
778 struct idmap_tdb2_set_mapping_context state;
780 if (!map || !map->sid) {
781 return NT_STATUS_INVALID_PARAMETER;
784 ksidstr = kidstr = NULL;
786 /* TODO: should we filter a set_mapping using low/high filters ? */
788 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
790 switch (map->xid.type) {
793 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
797 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
801 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
802 return NT_STATUS_INVALID_PARAMETER;
805 if (kidstr == NULL) {
806 DEBUG(0, ("ERROR: Out of memory!\n"));
807 ret = NT_STATUS_NO_MEMORY;
811 ksidstr = sid_string_talloc(ctx, map->sid);
812 if (ksidstr == NULL) {
813 DEBUG(0, ("Out of memory!\n"));
814 ret = NT_STATUS_NO_MEMORY;
818 state.ksidstr = ksidstr;
819 state.kidstr = kidstr;
821 ret = dbwrap_trans_do(idmap_tdb2, idmap_tdb2_set_mapping_action,
825 talloc_free(ksidstr);
831 * Create a new mapping for an unmapped SID, also allocating a new ID.
832 * This should be run inside a transaction.
835 * Properly integrate this with multi domain idmap config:
836 * Currently, the allocator is default-config only.
838 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
843 TALLOC_CTX *mem_ctx = talloc_stackframe();
846 ret = NT_STATUS_INVALID_PARAMETER;
850 if ((map->xid.type != ID_TYPE_UID) && (map->xid.type != ID_TYPE_GID)) {
851 ret = NT_STATUS_INVALID_PARAMETER;
855 if (map->sid == NULL) {
856 ret = NT_STATUS_INVALID_PARAMETER;
860 /* check wheter the SID is already mapped in the db */
861 sidstr = sid_string_talloc(mem_ctx, map->sid);
862 if (sidstr == NULL) {
863 DEBUG(0, ("Out of memory!\n"));
864 ret = NT_STATUS_NO_MEMORY;
868 data = dbwrap_fetch_bystring(idmap_tdb2, mem_ctx, sidstr);
870 ret = NT_STATUS_OBJECT_NAME_COLLISION;
874 /* unmapped - get a new id */
875 ret = idmap_tdb2_get_new_id(dom, &map->xid);
876 if (!NT_STATUS_IS_OK(ret)) {
877 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(ret)));
881 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
882 sid_string_dbg(map->sid),
883 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
884 (unsigned long)map->xid.id));
886 map->status = ID_MAPPED;
888 /* store the mapping */
889 ret = idmap_tdb2_set_mapping(dom, map);
890 if (!NT_STATUS_IS_OK(ret)) {
891 DEBUG(3, ("Could not store the new mapping: %s\n",
896 talloc_free(mem_ctx);
901 Close the idmap tdb instance
903 static NTSTATUS idmap_tdb2_close(struct idmap_domain *dom)
905 /* don't do anything */
909 static struct idmap_methods db_methods = {
910 .init = idmap_tdb2_db_init,
911 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
912 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
913 .allocate_id = idmap_tdb2_get_new_id,
914 .close_fn = idmap_tdb2_close
917 NTSTATUS idmap_tdb2_init(void)
919 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);