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.
35 #include "system/filesys.h"
40 #include "../libcli/security/dom_sid.h"
43 #define DBGC_CLASS DBGC_IDMAP
45 struct idmap_tdb2_context {
46 struct db_context *db;
47 const char *script; /* script to provide idmaps */
48 struct idmap_rw_ops *rw_ops;
51 /* High water mark keys */
52 #define HWM_GROUP "GROUP HWM"
53 #define HWM_USER "USER HWM"
57 * check and initialize high/low water marks in the db
59 static NTSTATUS idmap_tdb2_init_hwm(struct idmap_domain *dom)
63 struct idmap_tdb2_context *ctx;
65 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
67 /* Create high water marks for group and user id */
69 low_id = dbwrap_fetch_int32(ctx->db, HWM_USER);
70 if ((low_id == -1) || (low_id < dom->low_id)) {
71 status = dbwrap_trans_store_int32(ctx->db, HWM_USER,
73 if (!NT_STATUS_IS_OK(status)) {
74 DEBUG(0, ("Unable to initialise user hwm in idmap "
75 "database: %s\n", nt_errstr(status)));
76 return NT_STATUS_INTERNAL_DB_ERROR;
80 low_id = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
81 if ((low_id == -1) || (low_id < dom->low_id)) {
82 status = dbwrap_trans_store_int32(ctx->db, HWM_GROUP,
84 if (!NT_STATUS_IS_OK(status)) {
85 DEBUG(0, ("Unable to initialise group hwm in idmap "
86 "database: %s\n", nt_errstr(status)));
87 return NT_STATUS_INTERNAL_DB_ERROR;
96 open the permanent tdb
98 static NTSTATUS idmap_tdb2_open_db(struct idmap_domain *dom)
101 struct idmap_tdb2_context *ctx;
103 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
106 /* its already open */
110 db_path = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
111 if (db_path == NULL) {
112 /* fall back to the private directory, which, despite
113 its name, is usually on shared storage */
114 db_path = talloc_asprintf(NULL, "%s/idmap2.tdb", lp_private_dir());
116 NT_STATUS_HAVE_NO_MEMORY(db_path);
118 /* Open idmap repository */
119 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
120 TALLOC_FREE(db_path);
122 if (ctx->db == NULL) {
123 DEBUG(0, ("Unable to open idmap_tdb2 database '%s'\n",
125 return NT_STATUS_UNSUCCESSFUL;
128 return idmap_tdb2_init_hwm(dom);
136 struct idmap_tdb2_allocate_id_context {
143 static NTSTATUS idmap_tdb2_allocate_id_action(struct db_context *db,
147 struct idmap_tdb2_allocate_id_context *state;
150 state = (struct idmap_tdb2_allocate_id_context *)private_data;
152 hwm = dbwrap_fetch_int32(db, state->hwmkey);
154 ret = NT_STATUS_INTERNAL_DB_ERROR;
158 /* check it is in the range */
159 if (hwm > state->high_hwm) {
160 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
161 state->hwmtype, (unsigned long)state->high_hwm));
162 ret = NT_STATUS_UNSUCCESSFUL;
166 /* fetch a new id and increment it */
167 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
168 if (!NT_STATUS_IS_OK(ret)) {
169 DEBUG(1, ("Fatal error while fetching a new %s value\n!",
174 /* recheck it is in the range */
175 if (hwm > state->high_hwm) {
176 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
177 state->hwmtype, (unsigned long)state->high_hwm));
178 ret = NT_STATUS_UNSUCCESSFUL;
189 static NTSTATUS idmap_tdb2_allocate_id(struct idmap_domain *dom,
197 struct idmap_tdb2_allocate_id_context state;
198 struct idmap_tdb2_context *ctx;
200 status = idmap_tdb2_open_db(dom);
201 NT_STATUS_NOT_OK_RETURN(status);
203 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
205 /* Get current high water mark */
219 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
220 return NT_STATUS_INVALID_PARAMETER;
223 high_hwm = dom->high_id;
226 state.high_hwm = high_hwm;
227 state.hwmtype = hwmtype;
228 state.hwmkey = hwmkey;
230 status = dbwrap_trans_do(ctx->db, idmap_tdb2_allocate_id_action,
233 if (NT_STATUS_IS_OK(status)) {
235 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
237 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
244 * Allocate a new unix-ID.
245 * For now this is for the default idmap domain only.
246 * Should be extended later on.
248 static NTSTATUS idmap_tdb2_get_new_id(struct idmap_domain *dom,
253 if (!strequal(dom->name, "*")) {
254 DEBUG(3, ("idmap_tdb2_get_new_id: "
255 "Refusing creation of mapping for domain'%s'. "
256 "Currently only supported for the default "
259 return NT_STATUS_NOT_IMPLEMENTED;
262 ret = idmap_tdb2_allocate_id(dom, id);
268 IDMAP MAPPING TDB BACKEND
271 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom,
272 const struct id_map *map);
275 Initialise idmap database.
277 static NTSTATUS idmap_tdb2_db_init(struct idmap_domain *dom)
280 struct idmap_tdb2_context *ctx;
282 ctx = talloc_zero(dom, struct idmap_tdb2_context);
284 DEBUG(0, ("Out of memory!\n"));
285 return NT_STATUS_NO_MEMORY;
288 if (strequal(dom->name, "*")) {
289 ctx->script = lp_parm_const_string(-1, "idmap", "script", NULL);
291 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
294 char *config_option = NULL;
296 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
297 if ( ! config_option) {
298 DEBUG(0, ("Out of memory!\n"));
299 ret = NT_STATUS_NO_MEMORY;
303 ctx->script = lp_parm_const_string(-1, config_option, "script", NULL);
305 DEBUG(1, ("using idmap script '%s'\n", ctx->script));
308 talloc_free(config_option);
311 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
312 if (ctx->rw_ops == NULL) {
313 DEBUG(0, ("Out of memory!\n"));
314 ret = NT_STATUS_NO_MEMORY;
318 ctx->rw_ops->get_new_id = idmap_tdb2_get_new_id;
319 ctx->rw_ops->set_mapping = idmap_tdb2_set_mapping;
321 dom->private_data = ctx;
323 ret = idmap_tdb2_open_db(dom);
324 if (!NT_STATUS_IS_OK(ret)) {
337 * store a mapping in the database.
340 struct idmap_tdb2_set_mapping_context {
345 static NTSTATUS idmap_tdb2_set_mapping_action(struct db_context *db,
350 struct idmap_tdb2_set_mapping_context *state;
351 TALLOC_CTX *tmp_ctx = talloc_stackframe();
353 state = (struct idmap_tdb2_set_mapping_context *)private_data;
355 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
357 /* check wheter sid mapping is already present in db */
358 data = dbwrap_fetch_bystring(db, tmp_ctx, state->ksidstr);
360 ret = NT_STATUS_OBJECT_NAME_COLLISION;
364 ret = dbwrap_store_bystring(db, state->ksidstr,
365 string_term_tdb_data(state->kidstr),
367 if (!NT_STATUS_IS_OK(ret)) {
368 DEBUG(0, ("Error storing SID -> ID: %s\n", nt_errstr(ret)));
372 ret = dbwrap_store_bystring(db, state->kidstr,
373 string_term_tdb_data(state->ksidstr),
375 if (!NT_STATUS_IS_OK(ret)) {
376 DEBUG(0, ("Error storing ID -> SID: %s\n", nt_errstr(ret)));
377 /* try to remove the previous stored SID -> ID map */
378 dbwrap_delete_bystring(db, state->ksidstr);
382 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
385 talloc_free(tmp_ctx);
389 static NTSTATUS idmap_tdb2_set_mapping(struct idmap_domain *dom, const struct id_map *map)
391 struct idmap_tdb2_context *ctx;
393 char *ksidstr, *kidstr;
394 struct idmap_tdb2_set_mapping_context state;
396 if (!map || !map->sid) {
397 return NT_STATUS_INVALID_PARAMETER;
400 ksidstr = kidstr = NULL;
402 /* TODO: should we filter a set_mapping using low/high filters ? */
404 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
406 switch (map->xid.type) {
409 kidstr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
413 kidstr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
417 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
418 return NT_STATUS_INVALID_PARAMETER;
421 if (kidstr == NULL) {
422 DEBUG(0, ("ERROR: Out of memory!\n"));
423 ret = NT_STATUS_NO_MEMORY;
427 ksidstr = sid_string_talloc(ctx, map->sid);
428 if (ksidstr == NULL) {
429 DEBUG(0, ("Out of memory!\n"));
430 ret = NT_STATUS_NO_MEMORY;
434 state.ksidstr = ksidstr;
435 state.kidstr = kidstr;
437 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
441 talloc_free(ksidstr);
447 * Create a new mapping for an unmapped SID, also allocating a new ID.
448 * This should be run inside a transaction.
451 * Properly integrate this with multi domain idmap config:
452 * Currently, the allocator is default-config only.
454 static NTSTATUS idmap_tdb2_new_mapping(struct idmap_domain *dom, struct id_map *map)
457 struct idmap_tdb2_context *ctx;
459 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
461 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
468 run a script to perform a mapping
470 The script should the following command lines:
476 and should return one of the following as a single line of text
482 static NTSTATUS idmap_tdb2_script(struct idmap_tdb2_context *ctx, struct id_map *map,
483 const char *fmt, ...)
491 cmd = talloc_asprintf(ctx, "%s ", ctx->script);
492 NT_STATUS_HAVE_NO_MEMORY(cmd);
495 cmd = talloc_vasprintf_append(cmd, fmt, ap);
497 NT_STATUS_HAVE_NO_MEMORY(cmd);
502 return NT_STATUS_NONE_MAPPED;
505 if (fgets(line, sizeof(line)-1, p) == NULL) {
507 return NT_STATUS_NONE_MAPPED;
511 DEBUG(10,("idmap script gave: %s\n", line));
513 if (sscanf(line, "UID:%lu", &v) == 1) {
515 map->xid.type = ID_TYPE_UID;
516 } else if (sscanf(line, "GID:%lu", &v) == 1) {
518 map->xid.type = ID_TYPE_GID;
519 } else if (strncmp(line, "SID:S-", 6) == 0) {
520 if (!string_to_sid(map->sid, &line[4])) {
521 DEBUG(0,("Bad SID in '%s' from idmap script %s\n",
523 return NT_STATUS_NONE_MAPPED;
526 DEBUG(0,("Bad reply '%s' from idmap script %s\n",
528 return NT_STATUS_NONE_MAPPED;
537 Single id to sid lookup function.
539 static NTSTATUS idmap_tdb2_id_to_sid(struct idmap_domain *dom, struct id_map *map)
545 struct idmap_tdb2_context *ctx;
549 return NT_STATUS_INVALID_PARAMETER;
552 status = idmap_tdb2_open_db(dom);
553 NT_STATUS_NOT_OK_RETURN(status);
555 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
557 /* apply filters before checking */
558 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
559 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
560 map->xid.id, dom->low_id, dom->high_id));
561 return NT_STATUS_NONE_MAPPED;
564 switch (map->xid.type) {
567 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
571 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
575 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
576 return NT_STATUS_INVALID_PARAMETER;
579 /* final SAFE_FREE safe */
582 if (keystr == NULL) {
583 DEBUG(0, ("Out of memory!\n"));
584 ret = NT_STATUS_NO_MEMORY;
588 DEBUG(10,("Fetching record %s\n", keystr));
590 /* Check if the mapping exists */
591 data = dbwrap_fetch_bystring(ctx->db, keystr, keystr);
595 struct idmap_tdb2_set_mapping_context store_state;
597 DEBUG(10,("Record %s not found\n", keystr));
598 if (ctx->script == NULL) {
599 ret = NT_STATUS_NONE_MAPPED;
603 ret = idmap_tdb2_script(ctx, map, "IDTOSID %s", keystr);
605 /* store it on shared storage */
606 if (!NT_STATUS_IS_OK(ret)) {
610 sidstr = sid_string_talloc(keystr, map->sid);
612 ret = NT_STATUS_NO_MEMORY;
616 store_state.ksidstr = sidstr;
617 store_state.kidstr = keystr;
619 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
624 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
625 DEBUG(10,("INVALID SID (%s) in record %s\n",
626 (const char *)data.dptr, keystr));
627 ret = NT_STATUS_INTERNAL_DB_ERROR;
631 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
641 Single sid to id lookup function.
643 static NTSTATUS idmap_tdb2_sid_to_id(struct idmap_domain *dom, struct id_map *map)
648 unsigned long rec_id = 0;
649 struct idmap_tdb2_context *ctx;
650 TALLOC_CTX *tmp_ctx = talloc_stackframe();
652 ret = idmap_tdb2_open_db(dom);
653 NT_STATUS_NOT_OK_RETURN(ret);
655 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
657 keystr = sid_string_talloc(tmp_ctx, map->sid);
658 if (keystr == NULL) {
659 DEBUG(0, ("Out of memory!\n"));
660 ret = NT_STATUS_NO_MEMORY;
664 DEBUG(10,("Fetching record %s\n", keystr));
666 /* Check if sid is present in database */
667 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
670 struct idmap_tdb2_set_mapping_context store_state;
672 DEBUG(10,(__location__ " Record %s not found\n", keystr));
674 if (ctx->script == NULL) {
675 ret = NT_STATUS_NONE_MAPPED;
679 ret = idmap_tdb2_script(ctx, map, "SIDTOID %s", keystr);
680 /* store it on shared storage */
681 if (!NT_STATUS_IS_OK(ret)) {
685 /* apply filters before returning result */
686 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
687 DEBUG(5, ("Script returned id (%u) out of range "
688 "(%u - %u). Filtered!\n",
689 map->xid.id, dom->low_id, dom->high_id));
690 ret = NT_STATUS_NONE_MAPPED;
694 idstr = talloc_asprintf(tmp_ctx, "%cID %lu",
695 map->xid.type == ID_TYPE_UID?'U':'G',
696 (unsigned long)map->xid.id);
698 ret = NT_STATUS_NO_MEMORY;
702 store_state.ksidstr = keystr;
703 store_state.kidstr = idstr;
705 ret = dbwrap_trans_do(ctx->db, idmap_tdb2_set_mapping_action,
710 /* What type of record is this ? */
711 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
712 map->xid.id = rec_id;
713 map->xid.type = ID_TYPE_UID;
714 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
717 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
718 map->xid.id = rec_id;
719 map->xid.type = ID_TYPE_GID;
720 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
723 } else { /* Unknown record type ! */
724 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
725 ret = NT_STATUS_INTERNAL_DB_ERROR;
729 /* apply filters before returning result */
730 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
731 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
732 map->xid.id, dom->low_id, dom->high_id));
733 ret = NT_STATUS_NONE_MAPPED;
737 talloc_free(tmp_ctx);
742 lookup a set of unix ids.
744 static NTSTATUS idmap_tdb2_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
749 /* initialize the status to avoid suprise */
750 for (i = 0; ids[i]; i++) {
751 ids[i]->status = ID_UNKNOWN;
754 for (i = 0; ids[i]; i++) {
755 ret = idmap_tdb2_id_to_sid(dom, ids[i]);
756 if ( ! NT_STATUS_IS_OK(ret)) {
758 /* if it is just a failed mapping continue */
759 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
761 /* make sure it is marked as unmapped */
762 ids[i]->status = ID_UNMAPPED;
766 /* some fatal error occurred, return immediately */
770 /* all ok, id is mapped */
771 ids[i]->status = ID_MAPPED;
781 lookup a set of sids.
784 struct idmap_tdb2_sids_to_unixids_context {
785 struct idmap_domain *dom;
787 bool allocate_unmapped;
790 static NTSTATUS idmap_tdb2_sids_to_unixids_action(struct db_context *db,
793 struct idmap_tdb2_sids_to_unixids_context *state;
795 NTSTATUS ret = NT_STATUS_OK;
797 state = (struct idmap_tdb2_sids_to_unixids_context *)private_data;
799 DEBUG(10, ("idmap_tdb2_sids_to_unixids_action: "
800 " domain: [%s], allocate: %s\n",
802 state->allocate_unmapped ? "yes" : "no"));
804 for (i = 0; state->ids[i]; i++) {
805 if ((state->ids[i]->status == ID_UNKNOWN) ||
806 /* retry if we could not map in previous run: */
807 (state->ids[i]->status == ID_UNMAPPED))
811 ret2 = idmap_tdb2_sid_to_id(state->dom, state->ids[i]);
812 if (!NT_STATUS_IS_OK(ret2)) {
814 /* if it is just a failed mapping, continue */
815 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
817 /* make sure it is marked as unmapped */
818 state->ids[i]->status = ID_UNMAPPED;
819 ret = STATUS_SOME_UNMAPPED;
821 /* some fatal error occurred, return immediately */
826 /* all ok, id is mapped */
827 state->ids[i]->status = ID_MAPPED;
831 if ((state->ids[i]->status == ID_UNMAPPED) &&
832 state->allocate_unmapped)
834 ret = idmap_tdb2_new_mapping(state->dom, state->ids[i]);
835 if (!NT_STATUS_IS_OK(ret)) {
845 static NTSTATUS idmap_tdb2_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
849 struct idmap_tdb2_sids_to_unixids_context state;
850 struct idmap_tdb2_context *ctx;
852 ctx = talloc_get_type(dom->private_data, struct idmap_tdb2_context);
854 /* initialize the status to avoid suprise */
855 for (i = 0; ids[i]; i++) {
856 ids[i]->status = ID_UNKNOWN;
861 state.allocate_unmapped = false;
863 ret = idmap_tdb2_sids_to_unixids_action(ctx->db, &state);
865 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
866 state.allocate_unmapped = true;
867 ret = dbwrap_trans_do(ctx->db,
868 idmap_tdb2_sids_to_unixids_action,
876 static struct idmap_methods db_methods = {
877 .init = idmap_tdb2_db_init,
878 .unixids_to_sids = idmap_tdb2_unixids_to_sids,
879 .sids_to_unixids = idmap_tdb2_sids_to_unixids,
880 .allocate_id = idmap_tdb2_get_new_id
883 NTSTATUS idmap_tdb2_init(void)
885 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb2", &db_methods);