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 struct db_context *db;
41 const char *script; /* script to provide idmaps */
44 /* High water mark keys */
45 #define HWM_GROUP "GROUP HWM"
46 #define HWM_USER "USER HWM"
49 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom,
54 * check and initialize high/low water marks in the db
56 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
59 struct idmap_tdb2_context *ctx;
61 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
63 /* Create high water marks for group and user id */
65 low_id = dbwrap_fetch_int32(ctx->db, HWM_USER);
66 if ((low_id == -1) || (low_id < dom->low_id)) {
67 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
70 DEBUG(0, ("Unable to initialise user hwm in idmap "
72 return NT_STATUS_INTERNAL_DB_ERROR;
76 low_id = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
77 if ((low_id == -1) || (low_id < dom->low_id)) {
78 if (!NT_STATUS_IS_OK(dbwrap_trans_store_int32(
81 DEBUG(0, ("Unable to initialise group hwm in idmap "
83 return NT_STATUS_INTERNAL_DB_ERROR;
92 open the permanent tdb
94 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
97 struct idmap_tdb2_context *ctx;
99 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
102 /* its already open */
106 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
107 if (db_path == NULL) {
108 /* fall back to the private directory, which, despite
109 its name, is usually on shared storage */
110 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
112 NT_STATUS_HAVE_NO_MEMORY(db_path);
114 /* Open idmap repository */
115 ctx->db = db_open(NULL, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
116 TALLOC_FREE(db_path);
118 if (ctx->db == NULL) {
119 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
121 return NT_STATUS_UNSUCCESSFUL;
124 return idmap_tdb2_init_hwm(dom);
132 struct idmap_tdb2_allocate_id_context {
139 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
143 struct idmap_tdb2_allocate_id_context *state;
146 state = (struct idmap_tdb2_allocate_id_context *)private_data;
148 hwm = dbwrap_fetch_int32(db, state->hwmkey);
150 ret = NT_STATUS_INTERNAL_DB_ERROR;
154 /* check it is in the range */
155 if (hwm > state->high_hwm) {
156 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
157 state->hwmtype, (unsigned long)state->high_hwm));
158 ret = NT_STATUS_UNSUCCESSFUL;
162 /* fetch a new id and increment it */
163 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
164 if (!NT_STATUS_IS_OK(ret)) {
165 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
170 /* recheck it is in the range */
171 if (hwm > state->high_hwm) {
172 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
173 state->hwmtype, (unsigned long)state->high_hwm));
174 ret = NT_STATUS_UNSUCCESSFUL;
185 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
193 struct idmap_tdb2_allocate_id_context state;
194 struct idmap_tdb2_context *ctx;
196 status = idmap_tdb2_open_db(dom);
197 NT_STATUS_NOT_OK_RETURN(status);
199 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
201 /* Get current high water mark */
215 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
216 return NT_STATUS_INVALID_PARAMETER;
219 high_hwm = dom->high_id;
222 state.high_hwm = high_hwm;
223 state.hwmtype = hwmtype;
224 state.hwmkey = hwmkey;
226 status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
229 if (NT_STATUS_IS_OK(status)) {
231 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
233 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
240 * Allocate a new unix-ID.
241 * For now this is for the default idmap domain only.
242 * Should be extended later on.
244 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
249 if (!strequal(dom->name, "*")) {
250 DEBUG(3, ("idmap_tdb2_get_new_id: "
251 "Refusing creation of mapping for domain'%s'. "
252 "Currently only supported for the default "
255 return NT_STATUS_NOT_IMPLEMENTED;
258 ret = idmap_tdb2_allocate_id(dom, id);
264 IDMAP MAPPING TDB BACKEND
268 Initialise idmap database.
270 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom,
274 struct idmap_tdb2_context *ctx;
277 ctx = talloc_zero(dom, struct idmap_tdb2_context);
279 DEBUG(0, ("Out of memory!\n"));
280 return NT_STATUS_NO_MEMORY;
283 if (strequal(dom->name, "*")) {
284 ctx->script = lp_parm_const_string(-1, "idmap", "script", NULL);
286 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
289 char *config_option = NULL;
291 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
292 if ( ! config_option) {
293 DEBUG(0, ("Out of memory!\n"));
294 ret = NT_STATUS_NO_MEMORY;
298 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
300 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
303 talloc_free(config_option);
306 dom->private_data = ctx;
308 ret = idmap_tdb2_open_db(dom);
309 if (!NT_STATUS_IS_OK(ret)) {
320 struct idmap_tdb2_set_mapping_context {
325 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
330 struct idmap_tdb2_set_mapping_context *state;
331 TALLOC_CTX *tmp_ctx = talloc_stackframe();
333 state = (struct idmap_tdb2_set_mapping_context *)private_data;
335 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
337 /* check wheter sid mapping is already present in db */
338 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
340 ret = NT_STATUS_OBJECT_NAME_COLLISION;
344 ret = dbwrap_store_bystring(db, state->ksidstr,
345 string_term_tdb_data(state->kidstr),
347 if (!NT_STATUS_IS_OK(ret)) {
348 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
352 ret = dbwrap_store_bystring(db, state->kidstr,
353 string_term_tdb_data(state->ksidstr),
355 if (!NT_STATUS_IS_OK(ret)) {
356 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
357 /* try to remove the previous stored SID -> ID map */
358 dbwrap_delete_bystring(db, state->ksidstr);
362 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
365 talloc_free(tmp_ctx);
371 run a script to perform a mapping
373 The script should the following command lines:
379 and should return one of the following as a single line of text
385 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
386 const char *fmt, ...)
394 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
395 NT_STATUS_HAVE_NO_MEMORY(cmd);
398 cmd = talloc_vasprintf_append(cmd, fmt, ap);
400 NT_STATUS_HAVE_NO_MEMORY(cmd);
405 return NT_STATUS_NONE_MAPPED;
408 if (fgets(line, sizeof(line)-1, p) == NULL) {
410 return NT_STATUS_NONE_MAPPED;
414 DEBUG(10,("idmap script gave: %s\n", line));
416 if (sscanf(line, "UID:%lu", &v) == 1) {
418 map->xid.type = ID_TYPE_UID;
419 } else if (sscanf(line, "GID:%lu", &v) == 1) {
421 map->xid.type = ID_TYPE_GID;
422 } else if (strncmp(line, "SID:S-", 6) == 0) {
423 if (!string_to_sid(map->sid, &line[4])) {
424 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
426 return NT_STATUS_NONE_MAPPED;
429 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
431 return NT_STATUS_NONE_MAPPED;
440 Single id to sid lookup function.
442 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
448 struct idmap_tdb2_context *ctx;
452 return NT_STATUS_INVALID_PARAMETER;
455 status = idmap_tdb2_open_db(dom);
456 NT_STATUS_NOT_OK_RETURN(status);
458 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
460 /* apply filters before checking */
461 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
462 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
463 map->xid.id, dom->low_id, dom->high_id));
464 return NT_STATUS_NONE_MAPPED;
467 switch (map->xid.type) {
470 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
474 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
478 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
479 return NT_STATUS_INVALID_PARAMETER;
482 /* final SAFE_FREE safe */
485 if (keystr == NULL) {
486 DEBUG(0, ("Out of memory!\n"));
487 ret = NT_STATUS_NO_MEMORY;
491 DEBUG(10,("Fetching record %s\n", keystr));
493 /* Check if the mapping exists */
494 data = dbwrap_fetch_bystring(ctx->db, keystr, keystr);
498 struct idmap_tdb2_set_mapping_context store_state;
500 DEBUG(10,("Record %s not found\n", keystr));
501 if (ctx->script == NULL) {
502 ret = NT_STATUS_NONE_MAPPED;
506 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
508 /* store it on shared storage */
509 if (!NT_STATUS_IS_OK(ret)) {
513 sidstr = sid_string_talloc(keystr, map->sid);
515 ret = NT_STATUS_NO_MEMORY;
519 store_state.ksidstr = sidstr;
520 store_state.kidstr = keystr;
522 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
527 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
528 DEBUG(10,("INVALID SID (%s) in record %s\n",
529 (const char *)data.dptr, keystr));
530 ret = NT_STATUS_INTERNAL_DB_ERROR;
534 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
544 Single sid to id lookup function.
546 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
551 unsigned long rec_id = 0;
552 struct idmap_tdb2_context *ctx;
553 TALLOC_CTX *tmp_ctx = talloc_stackframe();
555 ret = idmap_tdb2_open_db(dom);
556 NT_STATUS_NOT_OK_RETURN(ret);
558 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
560 keystr = sid_string_talloc(tmp_ctx, map->sid);
561 if (keystr == NULL) {
562 DEBUG(0, ("Out of memory!\n"));
563 ret = NT_STATUS_NO_MEMORY;
567 DEBUG(10,("Fetching record %s\n", keystr));
569 /* Check if sid is present in database */
570 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
573 struct idmap_tdb2_set_mapping_context store_state;
575 DEBUG(10,(__location__ " Record %s not found\n", keystr));
577 if (ctx->script == NULL) {
578 ret = NT_STATUS_NONE_MAPPED;
582 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
583 /* store it on shared storage */
584 if (!NT_STATUS_IS_OK(ret)) {
588 /* apply filters before returning result */
589 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
590 DEBUG(5, ("Script returned id (%u) out of range "
591 "(%u - %u). Filtered!\n",
592 map->xid.id, dom->low_id, dom->high_id));
593 ret = NT_STATUS_NONE_MAPPED;
597 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
598 map->xid.type == ID_TYPE_UID?'U':'G',
599 (unsigned long)map->xid.id);
601 ret = NT_STATUS_NO_MEMORY;
605 store_state.ksidstr = keystr;
606 store_state.kidstr = idstr;
608 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
613 /* What type of record is this ? */
614 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
615 map->xid.id = rec_id;
616 map->xid.type = ID_TYPE_UID;
617 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
620 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
621 map->xid.id = rec_id;
622 map->xid.type = ID_TYPE_GID;
623 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
626 } else { /* Unknown record type ! */
627 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
628 ret = NT_STATUS_INTERNAL_DB_ERROR;
632 /* apply filters before returning result */
633 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
634 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
635 map->xid.id, dom->low_id, dom->high_id));
636 ret = NT_STATUS_NONE_MAPPED;
640 talloc_free(tmp_ctx);
645 lookup a set of unix ids.
647 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
652 /* initialize the status to avoid suprise */
653 for (i = 0; ids[i]; i++) {
654 ids[i]->status = ID_UNKNOWN;
657 for (i = 0; ids[i]; i++) {
658 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
659 if ( ! NT_STATUS_IS_OK(ret)) {
661 /* if it is just a failed mapping continue */
662 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
664 /* make sure it is marked as unmapped */
665 ids[i]->status = ID_UNMAPPED;
669 /* some fatal error occurred, return immediately */
673 /* all ok, id is mapped */
674 ids[i]->status = ID_MAPPED;
684 lookup a set of sids.
687 struct idmap_tdb2_sids_to_unixids_context {
688 struct idmap_domain *dom;
690 bool allocate_unmapped;
693 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
696 struct idmap_tdb2_sids_to_unixids_context *state;
698 NTSTATUS ret = NT_STATUS_OK;
700 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
702 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
703 " domain: [%s], allocate: %s\n",
705 state->allocate_unmapped ? "yes" : "no"));
707 for (i = 0; state->ids[i]; i++) {
708 if ((state->ids[i]->status == ID_UNKNOWN) ||
709 /* retry if we could not map in previous run: */
710 (state->ids[i]->status == ID_UNMAPPED))
714 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
715 if (!NT_STATUS_IS_OK(ret2)) {
717 /* if it is just a failed mapping, continue */
718 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
720 /* make sure it is marked as unmapped */
721 state->ids[i]->status = ID_UNMAPPED;
722 ret = STATUS_SOME_UNMAPPED;
724 /* some fatal error occurred, return immediately */
729 /* all ok, id is mapped */
730 state->ids[i]->status = ID_MAPPED;
734 if ((state->ids[i]->status == ID_UNMAPPED) &&
735 state->allocate_unmapped)
737 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
738 if (!NT_STATUS_IS_OK(ret)) {
748 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
752 struct idmap_tdb2_sids_to_unixids_context state;
753 struct idmap_tdb2_context *ctx;
755 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
757 /* initialize the status to avoid suprise */
758 for (i = 0; ids[i]; i++) {
759 ids[i]->status = ID_UNKNOWN;
764 state.allocate_unmapped = false;
766 ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
768 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
769 state.allocate_unmapped = true;
770 ret = dbwrap_trans_do(ctx->db,
771 idmap_tdb2_sids_to_unixids_action,
783 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
785 struct idmap_tdb2_context *ctx;
787 char *ksidstr, *kidstr;
788 struct idmap_tdb2_set_mapping_context state;
790 if (!map || !map->sid) {
791 return NT_STATUS_INVALID_PARAMETER;
794 ksidstr = kidstr = NULL;
796 /* TODO: should we filter a set_mapping using low/high filters ? */
798 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
800 switch (map->xid.type) {
803 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
807 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
811 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
812 return NT_STATUS_INVALID_PARAMETER;
815 if (kidstr == NULL) {
816 DEBUG(0, ("ERROR: Out of memory!\n"));
817 ret = NT_STATUS_NO_MEMORY;
821 ksidstr = sid_string_talloc(ctx, map->sid);
822 if (ksidstr == NULL) {
823 DEBUG(0, ("Out of memory!\n"));
824 ret = NT_STATUS_NO_MEMORY;
828 state.ksidstr = ksidstr;
829 state.kidstr = kidstr;
831 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
835 talloc_free(ksidstr);
841 * Create a new mapping for an unmapped SID, also allocating a new ID.
842 * This should be run inside a transaction.
845 * Properly integrate this with multi domain idmap config:
846 * Currently, the allocator is default-config only.
848 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
853 ret = NT_STATUS_INVALID_PARAMETER;
857 if ((map->xid.type != ID_TYPE_UID) && (map->xid.type != ID_TYPE_GID)) {
858 ret = NT_STATUS_INVALID_PARAMETER;
862 if (map->sid == NULL) {
863 ret = NT_STATUS_INVALID_PARAMETER;
867 ret = idmap_tdb2_get_new_id(dom, &map->xid);
868 if (!NT_STATUS_IS_OK(ret)) {
869 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(ret)));
873 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
874 sid_string_dbg(map->sid),
875 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
876 (unsigned long)map->xid.id));
878 map->status = ID_MAPPED;
880 /* store the mapping */
881 ret = idmap_tdb2_set_mapping(dom, map);
882 if (!NT_STATUS_IS_OK(ret)) {
883 DEBUG(3, ("Could not store the new mapping: %s\n",
892 Close the idmap tdb instance
894 static NTSTATUS idmap_tdb2_close(struct idmap_domain *dom)
896 /* don't do anything */
900 static struct idmap_methods db_methods = {
901 .init = idmap_tdb2_db_init,
902 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
903 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
904 .allocate_id = idmap_tdb2_get_new_id,
905 .close_fn = idmap_tdb2_close
908 NTSTATUS idmap_tdb2_init(void)
910 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);