2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) Jeremy Allison 2006
9 Copyright (C) Simo Sorce 2003-2006
10 Copyright (C) Michael Adam 2009-2010
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "system/filesys.h"
32 #include "../libcli/security/security.h"
35 #define DBGC_CLASS DBGC_IDMAP
37 /* idmap version determines auto-conversion - this is the database
38 structure version specifier. */
40 #define IDMAP_VERSION 2
42 struct idmap_tdb_context {
43 struct db_context *db;
44 struct idmap_rw_ops *rw_ops;
47 /* High water mark keys */
48 #define HWM_GROUP "GROUP HWM"
49 #define HWM_USER "USER HWM"
51 struct convert_fn_state {
52 struct db_context *db;
56 /*****************************************************************************
57 For idmap conversion: convert one record to new format
58 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
60 *****************************************************************************/
61 static int convert_fn(struct db_record *rec, void *private_data)
63 struct winbindd_domain *domain;
71 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
73 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
75 p = strchr((const char *)rec->key.dptr, '/');
80 fstrcpy(dom_name, (const char *)rec->key.dptr);
83 domain = find_domain_from_name(dom_name);
85 /* We must delete the old record. */
86 DEBUG(0,("Unable to find domain %s\n", dom_name ));
87 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
89 status = rec->delete_rec(rec);
90 if (!NT_STATUS_IS_OK(status)) {
91 DEBUG(0, ("Unable to delete record %s:%s\n",
92 (const char *)rec->key.dptr,
103 sid_compose(&sid, &domain->sid, rid);
105 sid_to_fstring(keystr, &sid);
106 key2 = string_term_tdb_data(keystr);
108 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
109 if (!NT_STATUS_IS_OK(status)) {
110 DEBUG(0,("Unable to add record %s:%s\n",
111 (const char *)key2.dptr,
117 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
118 if (!NT_STATUS_IS_OK(status)) {
119 DEBUG(0,("Unable to update record %s:%s\n",
120 (const char *)rec->value.dptr,
126 status = rec->delete_rec(rec);
127 if (!NT_STATUS_IS_OK(status)) {
128 DEBUG(0,("Unable to delete record %s:%s\n",
129 (const char *)rec->key.dptr,
138 /*****************************************************************************
139 Convert the idmap database from an older version.
140 *****************************************************************************/
142 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
145 bool bigendianheader;
146 struct convert_fn_state s;
148 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
150 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
152 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
154 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
155 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
157 * high and low records were created on a
158 * big endian machine and will need byte-reversing.
163 wm = dbwrap_fetch_int32(db, HWM_USER);
171 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
172 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
176 wm = dbwrap_fetch_int32(db, HWM_GROUP);
183 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
184 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
192 /* the old format stored as DOMAIN/rid - now we store the SID direct */
193 db->traverse(db, convert_fn, &s);
196 DEBUG(0, ("Problem during conversion\n"));
200 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
201 DEBUG(0, ("Unable to store idmap version in database\n"));
208 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
213 bool update_uid = false;
214 bool update_gid = false;
215 struct idmap_tdb_context *ctx;
217 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
219 low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
220 if (low_uid == -1 || low_uid < dom->low_id) {
224 low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
225 if (low_gid == -1 || low_gid < dom->low_id) {
229 if (!update_uid && !update_gid) {
233 if (ctx->db->transaction_start(ctx->db) != 0) {
234 DEBUG(0, ("Unable to start upgrade transaction!\n"));
235 return NT_STATUS_INTERNAL_DB_ERROR;
239 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
241 ctx->db->transaction_cancel(ctx->db);
242 DEBUG(0, ("Unable to initialise user hwm in idmap "
244 return NT_STATUS_INTERNAL_DB_ERROR;
249 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
251 ctx->db->transaction_cancel(ctx->db);
252 DEBUG(0, ("Unable to initialise group hwm in idmap "
254 return NT_STATUS_INTERNAL_DB_ERROR;
258 if (ctx->db->transaction_commit(ctx->db) != 0) {
259 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
260 return NT_STATUS_INTERNAL_DB_ERROR;
266 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
270 char *tdbfile = NULL;
271 struct db_context *db = NULL;
273 bool config_error = false;
274 struct idmap_tdb_context *ctx;
276 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
279 /* it is already open */
283 /* use our own context here */
284 mem_ctx = talloc_stackframe();
286 /* use the old database if present */
287 tdbfile = state_path("winbindd_idmap.tdb");
289 DEBUG(0, ("Out of memory!\n"));
290 ret = NT_STATUS_NO_MEMORY;
294 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
296 /* Open idmap repository */
297 db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
299 DEBUG(0, ("Unable to open idmap database\n"));
300 ret = NT_STATUS_UNSUCCESSFUL;
304 /* check against earlier versions */
305 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
306 if (version != IDMAP_VERSION) {
308 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
309 "possible with incomplete configuration\n",
310 version, IDMAP_VERSION));
311 ret = NT_STATUS_UNSUCCESSFUL;
314 if (db->transaction_start(db) != 0) {
315 DEBUG(0, ("Unable to start upgrade transaction!\n"));
316 ret = NT_STATUS_INTERNAL_DB_ERROR;
320 if (!idmap_tdb_upgrade(dom, db)) {
321 db->transaction_cancel(db);
322 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
323 ret = NT_STATUS_INTERNAL_DB_ERROR;
327 if (db->transaction_commit(db) != 0) {
328 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
329 ret = NT_STATUS_INTERNAL_DB_ERROR;
334 ctx->db = talloc_move(ctx, &db);
336 ret = idmap_tdb_init_hwm(dom);
339 talloc_free(mem_ctx);
343 /**********************************************************************
344 IDMAP ALLOC TDB BACKEND
345 **********************************************************************/
347 /**********************************
349 **********************************/
351 struct idmap_tdb_allocate_id_context {
358 static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
362 struct idmap_tdb_allocate_id_context *state;
365 state = (struct idmap_tdb_allocate_id_context *)private_data;
367 hwm = dbwrap_fetch_int32(db, state->hwmkey);
369 ret = NT_STATUS_INTERNAL_DB_ERROR;
373 /* check it is in the range */
374 if (hwm > state->high_hwm) {
375 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
376 state->hwmtype, (unsigned long)state->high_hwm));
377 ret = NT_STATUS_UNSUCCESSFUL;
381 /* fetch a new id and increment it */
382 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
383 if (!NT_STATUS_IS_OK(ret)) {
384 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
385 state->hwmtype, nt_errstr(ret)));
389 /* recheck it is in the range */
390 if (hwm > state->high_hwm) {
391 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
392 state->hwmtype, (unsigned long)state->high_hwm));
393 ret = NT_STATUS_UNSUCCESSFUL;
404 static NTSTATUS idmap_tdb_allocate_id(struct idmap_domain *dom,
412 struct idmap_tdb_allocate_id_context state;
413 struct idmap_tdb_context *ctx;
415 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
417 /* Get current high water mark */
431 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
432 return NT_STATUS_INVALID_PARAMETER;
435 high_hwm = dom->high_id;
438 state.high_hwm = high_hwm;
439 state.hwmtype = hwmtype;
440 state.hwmkey = hwmkey;
442 status = dbwrap_trans_do(ctx->db, idmap_tdb_allocate_id_action,
445 if (NT_STATUS_IS_OK(status)) {
447 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
449 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
456 * Allocate a new unix-ID.
457 * For now this is for the default idmap domain only.
458 * Should be extended later on.
460 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
465 if (!strequal(dom->name, "*")) {
466 DEBUG(3, ("idmap_tdb_get_new_id: "
467 "Refusing allocation of a new unixid for domain'%s'. "
468 "Currently only supported for the default "
471 return NT_STATUS_NOT_IMPLEMENTED;
474 ret = idmap_tdb_allocate_id(dom, id);
479 /**********************************************************************
480 IDMAP MAPPING TDB BACKEND
481 **********************************************************************/
483 /*****************************
484 Initialise idmap database.
485 *****************************/
487 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
488 const struct id_map *map);
490 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom)
493 struct idmap_tdb_context *ctx;
495 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
497 ctx = talloc_zero(dom, struct idmap_tdb_context);
499 DEBUG(0, ("Out of memory!\n"));
500 return NT_STATUS_NO_MEMORY;
503 /* load backend specific configuration here: */
505 if (strequal(dom->name, "*")) {
510 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
511 if (ctx->rw_ops == NULL) {
512 DEBUG(0, ("Out of memory!\n"));
513 ret = NT_STATUS_NO_MEMORY;
517 ctx->rw_ops->get_new_id = idmap_tdb_get_new_id;
518 ctx->rw_ops->set_mapping = idmap_tdb_set_mapping;
520 dom->private_data = ctx;
522 ret = idmap_tdb_open_db(dom);
523 if ( ! NT_STATUS_IS_OK(ret)) {
536 * store a mapping in the database
539 struct idmap_tdb_set_mapping_context {
544 static NTSTATUS idmap_tdb_set_mapping_action(struct db_context *db,
548 struct idmap_tdb_set_mapping_context *state;
550 state = (struct idmap_tdb_set_mapping_context *)private_data;
552 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
554 ret = dbwrap_store_bystring(db, state->ksidstr,
555 string_term_tdb_data(state->kidstr),
557 if (!NT_STATUS_IS_OK(ret)) {
558 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
559 state->ksidstr, state->kidstr, nt_errstr(ret)));
563 ret = dbwrap_store_bystring(db, state->kidstr,
564 string_term_tdb_data(state->ksidstr),
566 if (!NT_STATUS_IS_OK(ret)) {
567 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
568 state->kidstr, state->ksidstr, nt_errstr(ret)));
572 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
579 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
580 const struct id_map *map)
582 struct idmap_tdb_context *ctx;
584 char *ksidstr, *kidstr;
585 struct idmap_tdb_set_mapping_context state;
587 if (!map || !map->sid) {
588 return NT_STATUS_INVALID_PARAMETER;
591 ksidstr = kidstr = NULL;
593 /* TODO: should we filter a set_mapping using low/high filters ? */
595 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
597 switch (map->xid.type) {
600 kidstr = talloc_asprintf(ctx, "UID %lu",
601 (unsigned long)map->xid.id);
605 kidstr = talloc_asprintf(ctx, "GID %lu",
606 (unsigned long)map->xid.id);
610 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
611 return NT_STATUS_INVALID_PARAMETER;
614 if (kidstr == NULL) {
615 DEBUG(0, ("ERROR: Out of memory!\n"));
616 ret = NT_STATUS_NO_MEMORY;
620 ksidstr = sid_string_talloc(ctx, map->sid);
621 if (ksidstr == NULL) {
622 DEBUG(0, ("Out of memory!\n"));
623 ret = NT_STATUS_NO_MEMORY;
627 state.ksidstr = ksidstr;
628 state.kidstr = kidstr;
630 ret = dbwrap_trans_do(ctx->db, idmap_tdb_set_mapping_action, &state);
633 talloc_free(ksidstr);
639 * Create a new mapping for an unmapped SID, also allocating a new ID.
640 * This should be run inside a transaction.
643 * Properly integrate this with multi domain idmap config:
644 * Currently, the allocator is default-config only.
646 static NTSTATUS idmap_tdb_new_mapping(struct idmap_domain *dom, struct id_map *map)
649 struct idmap_tdb_context *ctx;
651 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
653 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
659 /**********************************
660 Single id to sid lookup function.
661 **********************************/
663 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
668 struct idmap_tdb_context *ctx;
671 return NT_STATUS_INVALID_PARAMETER;
674 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
676 /* apply filters before checking */
677 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
678 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
679 map->xid.id, dom->low_id, dom->high_id));
680 return NT_STATUS_NONE_MAPPED;
683 switch (map->xid.type) {
686 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
690 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
694 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
695 return NT_STATUS_INVALID_PARAMETER;
698 /* final SAFE_FREE safe */
701 if (keystr == NULL) {
702 DEBUG(0, ("Out of memory!\n"));
703 ret = NT_STATUS_NO_MEMORY;
707 DEBUG(10,("Fetching record %s\n", keystr));
709 /* Check if the mapping exists */
710 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
713 DEBUG(10,("Record %s not found\n", keystr));
714 ret = NT_STATUS_NONE_MAPPED;
718 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
719 DEBUG(10,("INVALID SID (%s) in record %s\n",
720 (const char *)data.dptr, keystr));
721 ret = NT_STATUS_INTERNAL_DB_ERROR;
725 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
729 talloc_free(data.dptr);
734 /**********************************
735 Single sid to id lookup function.
736 **********************************/
738 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
743 unsigned long rec_id = 0;
744 struct idmap_tdb_context *ctx;
745 TALLOC_CTX *tmp_ctx = talloc_stackframe();
747 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
749 keystr = sid_string_talloc(tmp_ctx, map->sid);
750 if (keystr == NULL) {
751 DEBUG(0, ("Out of memory!\n"));
752 ret = NT_STATUS_NO_MEMORY;
756 DEBUG(10,("Fetching record %s\n", keystr));
758 /* Check if sid is present in database */
759 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
761 DEBUG(10,("Record %s not found\n", keystr));
762 ret = NT_STATUS_NONE_MAPPED;
766 /* What type of record is this ? */
767 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
768 map->xid.id = rec_id;
769 map->xid.type = ID_TYPE_UID;
770 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
773 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
774 map->xid.id = rec_id;
775 map->xid.type = ID_TYPE_GID;
776 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
779 } else { /* Unknown record type ! */
780 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
781 ret = NT_STATUS_INTERNAL_DB_ERROR;
785 /* apply filters before returning result */
786 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
787 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
788 map->xid.id, dom->low_id, dom->high_id));
789 ret = NT_STATUS_NONE_MAPPED;
793 talloc_free(tmp_ctx);
797 /**********************************
798 lookup a set of unix ids.
799 **********************************/
801 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
806 /* initialize the status to avoid suprise */
807 for (i = 0; ids[i]; i++) {
808 ids[i]->status = ID_UNKNOWN;
811 for (i = 0; ids[i]; i++) {
812 ret = idmap_tdb_id_to_sid(dom, ids[i]);
813 if ( ! NT_STATUS_IS_OK(ret)) {
815 /* if it is just a failed mapping continue */
816 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
818 /* make sure it is marked as unmapped */
819 ids[i]->status = ID_UNMAPPED;
823 /* some fatal error occurred, return immediately */
827 /* all ok, id is mapped */
828 ids[i]->status = ID_MAPPED;
837 /**********************************
838 lookup a set of sids.
839 **********************************/
841 struct idmap_tdb_sids_to_unixids_context {
842 struct idmap_domain *dom;
844 bool allocate_unmapped;
847 static NTSTATUS idmap_tdb_sids_to_unixids_action(struct db_context *db,
850 struct idmap_tdb_sids_to_unixids_context *state;
852 NTSTATUS ret = NT_STATUS_OK;
854 state = (struct idmap_tdb_sids_to_unixids_context *)private_data;
856 DEBUG(10, ("idmap_tdb_sids_to_unixids_action: "
857 " domain: [%s], allocate: %s\n",
859 state->allocate_unmapped ? "yes" : "no"));
861 for (i = 0; state->ids[i]; i++) {
862 if ((state->ids[i]->status == ID_UNKNOWN) ||
863 /* retry if we could not map in previous run: */
864 (state->ids[i]->status == ID_UNMAPPED))
868 ret2 = idmap_tdb_sid_to_id(state->dom, state->ids[i]);
869 if (!NT_STATUS_IS_OK(ret2)) {
871 /* if it is just a failed mapping, continue */
872 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
874 /* make sure it is marked as unmapped */
875 state->ids[i]->status = ID_UNMAPPED;
876 ret = STATUS_SOME_UNMAPPED;
878 /* some fatal error occurred, return immediately */
883 /* all ok, id is mapped */
884 state->ids[i]->status = ID_MAPPED;
888 if ((state->ids[i]->status == ID_UNMAPPED) &&
889 state->allocate_unmapped)
891 ret = idmap_tdb_new_mapping(state->dom, state->ids[i]);
892 if (!NT_STATUS_IS_OK(ret)) {
902 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
904 struct idmap_tdb_context *ctx;
907 struct idmap_tdb_sids_to_unixids_context state;
909 /* initialize the status to avoid suprise */
910 for (i = 0; ids[i]; i++) {
911 ids[i]->status = ID_UNKNOWN;
914 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
918 state.allocate_unmapped = false;
920 ret = idmap_tdb_sids_to_unixids_action(ctx->db, &state);
922 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
923 state.allocate_unmapped = true;
924 ret = dbwrap_trans_do(ctx->db,
925 idmap_tdb_sids_to_unixids_action,
933 /**********************************
934 Close the idmap tdb instance
935 **********************************/
937 static struct idmap_methods db_methods = {
938 .init = idmap_tdb_db_init,
939 .unixids_to_sids = idmap_tdb_unixids_to_sids,
940 .sids_to_unixids = idmap_tdb_sids_to_unixids,
941 .allocate_id = idmap_tdb_get_new_id,
944 NTSTATUS idmap_tdb_init(void)
946 DEBUG(10, ("calling idmap_tdb_init\n"));
948 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);