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
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #define DBGC_CLASS DBGC_IDMAP
31 /* idmap version determines auto-conversion - this is the database
32 structure version specifier. */
34 #define IDMAP_VERSION 2
36 struct idmap_tdb_context {
37 struct db_context *db;
40 /* High water mark keys */
41 #define HWM_GROUP "GROUP HWM"
42 #define HWM_USER "USER HWM"
44 struct convert_fn_state {
45 struct db_context *db;
49 /*****************************************************************************
50 For idmap conversion: convert one record to new format
51 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
53 *****************************************************************************/
54 static int convert_fn(struct db_record *rec, void *private_data)
56 struct winbindd_domain *domain;
64 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
66 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
68 p = strchr((const char *)rec->key.dptr, '/');
73 fstrcpy(dom_name, (const char *)rec->key.dptr);
76 domain = find_domain_from_name(dom_name);
78 /* We must delete the old record. */
79 DEBUG(0,("Unable to find domain %s\n", dom_name ));
80 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
82 status = rec->delete_rec(rec);
83 if (!NT_STATUS_IS_OK(status)) {
84 DEBUG(0, ("Unable to delete record %s:%s\n",
85 (const char *)rec->key.dptr,
96 sid_compose(&sid, &domain->sid, rid);
98 sid_to_fstring(keystr, &sid);
99 key2 = string_term_tdb_data(keystr);
101 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
102 if (!NT_STATUS_IS_OK(status)) {
103 DEBUG(0,("Unable to add record %s:%s\n",
104 (const char *)key2.dptr,
110 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
111 if (!NT_STATUS_IS_OK(status)) {
112 DEBUG(0,("Unable to update record %s:%s\n",
113 (const char *)rec->value.dptr,
119 status = rec->delete_rec(rec);
120 if (!NT_STATUS_IS_OK(status)) {
121 DEBUG(0,("Unable to delete record %s:%s\n",
122 (const char *)rec->key.dptr,
131 /*****************************************************************************
132 Convert the idmap database from an older version.
133 *****************************************************************************/
135 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
138 bool bigendianheader;
139 struct convert_fn_state s;
141 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
143 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
145 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
147 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
148 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
150 * high and low records were created on a
151 * big endian machine and will need byte-reversing.
156 wm = dbwrap_fetch_int32(db, HWM_USER);
164 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
165 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
169 wm = dbwrap_fetch_int32(db, HWM_GROUP);
176 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
177 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
185 /* the old format stored as DOMAIN/rid - now we store the SID direct */
186 db->traverse(db, convert_fn, &s);
189 DEBUG(0, ("Problem during conversion\n"));
193 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
194 DEBUG(0, ("Unable to store idmap version in databse\n"));
201 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
206 bool update_uid = false;
207 bool update_gid = false;
208 struct idmap_tdb_context *ctx;
210 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
212 low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
213 if (low_uid == -1 || low_uid < dom->low_id) {
217 low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
218 if (low_gid == -1 || low_gid < dom->low_id) {
222 if (!update_uid && !update_gid) {
226 if (ctx->db->transaction_start(ctx->db) != 0) {
227 DEBUG(0, ("Unable to start upgrade transaction!\n"));
228 return NT_STATUS_INTERNAL_DB_ERROR;
232 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
234 ctx->db->transaction_cancel(ctx->db);
235 DEBUG(0, ("Unable to initialise user hwm in idmap "
237 return NT_STATUS_INTERNAL_DB_ERROR;
242 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
244 ctx->db->transaction_cancel(ctx->db);
245 DEBUG(0, ("Unable to initialise group hwm in idmap "
247 return NT_STATUS_INTERNAL_DB_ERROR;
251 if (ctx->db->transaction_commit(ctx->db) != 0) {
252 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
253 return NT_STATUS_INTERNAL_DB_ERROR;
259 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
263 char *tdbfile = NULL;
264 struct db_context *db = NULL;
266 bool config_error = false;
267 struct idmap_tdb_context *ctx;
269 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
271 /* use our own context here */
272 mem_ctx = talloc_stackframe();
274 /* use the old database if present */
275 tdbfile = state_path("winbindd_idmap.tdb");
277 DEBUG(0, ("Out of memory!\n"));
278 ret = NT_STATUS_NO_MEMORY;
282 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
284 /* Open idmap repository */
285 db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
287 DEBUG(0, ("Unable to open idmap database\n"));
288 ret = NT_STATUS_UNSUCCESSFUL;
292 /* check against earlier versions */
293 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
294 if (version != IDMAP_VERSION) {
296 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
297 "possible with incomplete configuration\n",
298 version, IDMAP_VERSION));
299 ret = NT_STATUS_UNSUCCESSFUL;
302 if (db->transaction_start(db) != 0) {
303 DEBUG(0, ("Unable to start upgrade transaction!\n"));
304 ret = NT_STATUS_INTERNAL_DB_ERROR;
308 if (!idmap_tdb_upgrade(dom, db)) {
309 db->transaction_cancel(db);
310 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
311 ret = NT_STATUS_INTERNAL_DB_ERROR;
315 if (db->transaction_commit(db) != 0) {
316 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
317 ret = NT_STATUS_INTERNAL_DB_ERROR;
322 ctx->db = talloc_move(ctx, &db);
324 ret = idmap_tdb_init_hwm(dom);
327 talloc_free(mem_ctx);
331 /**********************************************************************
332 IDMAP ALLOC TDB BACKEND
333 **********************************************************************/
335 /**********************************
337 **********************************/
339 struct idmap_tdb_allocate_id_context {
346 static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
350 struct idmap_tdb_allocate_id_context *state;
353 state = (struct idmap_tdb_allocate_id_context *)private_data;
355 hwm = dbwrap_fetch_int32(db, state->hwmkey);
357 ret = NT_STATUS_INTERNAL_DB_ERROR;
361 /* check it is in the range */
362 if (hwm > state->high_hwm) {
363 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
364 state->hwmtype, (unsigned long)state->high_hwm));
365 ret = NT_STATUS_UNSUCCESSFUL;
369 /* fetch a new id and increment it */
370 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
371 if (!NT_STATUS_IS_OK(ret)) {
372 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
373 state->hwmtype, nt_errstr(ret)));
377 /* recheck it is in the range */
378 if (hwm > state->high_hwm) {
379 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
380 state->hwmtype, (unsigned long)state->high_hwm));
381 ret = NT_STATUS_UNSUCCESSFUL;
392 static NTSTATUS idmap_tdb_allocate_id(struct idmap_domain *dom,
400 struct idmap_tdb_allocate_id_context state;
401 struct idmap_tdb_context *ctx;
403 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
405 /* Get current high water mark */
419 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
420 return NT_STATUS_INVALID_PARAMETER;
423 high_hwm = dom->high_id;
426 state.high_hwm = high_hwm;
427 state.hwmtype = hwmtype;
428 state.hwmkey = hwmkey;
430 status = dbwrap_trans_do(ctx->db, idmap_tdb_allocate_id_action,
433 if (NT_STATUS_IS_OK(status)) {
435 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
437 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
444 * Allocate a new unix-ID.
445 * For now this is for the default idmap domain only.
446 * Should be extended later on.
448 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
453 if (!strequal(dom->name, "*")) {
454 DEBUG(3, ("idmap_tdb_get_new_id: "
455 "Refusing allocation of a new unixid for domain'%s'. "
456 "Currently only supported for the default "
459 return NT_STATUS_NOT_IMPLEMENTED;
462 ret = idmap_tdb_allocate_id(dom, id);
467 /**********************************************************************
468 IDMAP MAPPING TDB BACKEND
469 **********************************************************************/
471 /*****************************
472 Initialise idmap database.
473 *****************************/
475 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
478 struct idmap_tdb_context *ctx;
480 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
482 ctx = talloc(dom, struct idmap_tdb_context);
484 DEBUG(0, ("Out of memory!\n"));
485 return NT_STATUS_NO_MEMORY;
488 /* load backend specific configuration here: */
490 if (strequal(dom->name, "*")) {
495 dom->private_data = ctx;
497 ret = idmap_tdb_open_db(dom);
498 if ( ! NT_STATUS_IS_OK(ret)) {
511 * store a mapping in the database
514 struct idmap_tdb_set_mapping_context {
519 static NTSTATUS idmap_tdb_set_mapping_action(struct db_context *db,
523 struct idmap_tdb_set_mapping_context *state;
525 state = (struct idmap_tdb_set_mapping_context *)private_data;
527 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
529 ret = dbwrap_store_bystring(db, state->ksidstr,
530 string_term_tdb_data(state->kidstr),
532 if (!NT_STATUS_IS_OK(ret)) {
533 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
534 state->ksidstr, state->kidstr, nt_errstr(ret)));
538 ret = dbwrap_store_bystring(db, state->kidstr,
539 string_term_tdb_data(state->ksidstr),
541 if (!NT_STATUS_IS_OK(ret)) {
542 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
543 state->kidstr, state->ksidstr, nt_errstr(ret)));
547 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
554 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
555 const struct id_map *map)
557 struct idmap_tdb_context *ctx;
559 char *ksidstr, *kidstr;
560 struct idmap_tdb_set_mapping_context state;
562 if (!map || !map->sid) {
563 return NT_STATUS_INVALID_PARAMETER;
566 ksidstr = kidstr = NULL;
568 /* TODO: should we filter a set_mapping using low/high filters ? */
570 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
572 switch (map->xid.type) {
575 kidstr = talloc_asprintf(ctx, "UID %lu",
576 (unsigned long)map->xid.id);
580 kidstr = talloc_asprintf(ctx, "GID %lu",
581 (unsigned long)map->xid.id);
585 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
586 return NT_STATUS_INVALID_PARAMETER;
589 if (kidstr == NULL) {
590 DEBUG(0, ("ERROR: Out of memory!\n"));
591 ret = NT_STATUS_NO_MEMORY;
595 ksidstr = sid_string_talloc(ctx, map->sid);
596 if (ksidstr == NULL) {
597 DEBUG(0, ("Out of memory!\n"));
598 ret = NT_STATUS_NO_MEMORY;
602 state.ksidstr = ksidstr;
603 state.kidstr = kidstr;
605 ret = dbwrap_trans_do(ctx->db, idmap_tdb_set_mapping_action, &state);
608 talloc_free(ksidstr);
614 * Create a new mapping for an unmapped SID, also allocating a new ID.
615 * This should be run inside a transaction.
618 * Properly integrate this with multi domain idmap config:
619 * Currently, the allocator is default-config only.
621 static NTSTATUS idmap_tdb_new_mapping(struct idmap_domain *dom, struct id_map *map)
626 ret = NT_STATUS_INVALID_PARAMETER;
630 if ((map->xid.type != ID_TYPE_UID) && (map->xid.type != ID_TYPE_GID)) {
631 ret = NT_STATUS_INVALID_PARAMETER;
635 if (map->sid == NULL) {
636 ret = NT_STATUS_INVALID_PARAMETER;
640 ret = idmap_tdb_get_new_id(dom, &map->xid);
641 if (!NT_STATUS_IS_OK(ret)) {
642 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(ret)));
646 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
647 sid_string_dbg(map->sid),
648 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
649 (unsigned long)map->xid.id));
651 map->status = ID_MAPPED;
653 /* store the mapping */
654 ret = idmap_tdb_set_mapping(dom, map);
655 if (!NT_STATUS_IS_OK(ret)) {
656 DEBUG(3, ("Could not store the new mapping: %s\n",
665 /**********************************
666 Single id to sid lookup function.
667 **********************************/
669 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
674 struct idmap_tdb_context *ctx;
677 return NT_STATUS_INVALID_PARAMETER;
680 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
682 /* apply filters before checking */
683 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
684 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
685 map->xid.id, dom->low_id, dom->high_id));
686 return NT_STATUS_NONE_MAPPED;
689 switch (map->xid.type) {
692 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
696 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
700 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
701 return NT_STATUS_INVALID_PARAMETER;
704 /* final SAFE_FREE safe */
707 if (keystr == NULL) {
708 DEBUG(0, ("Out of memory!\n"));
709 ret = NT_STATUS_NO_MEMORY;
713 DEBUG(10,("Fetching record %s\n", keystr));
715 /* Check if the mapping exists */
716 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
719 DEBUG(10,("Record %s not found\n", keystr));
720 ret = NT_STATUS_NONE_MAPPED;
724 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
725 DEBUG(10,("INVALID SID (%s) in record %s\n",
726 (const char *)data.dptr, keystr));
727 ret = NT_STATUS_INTERNAL_DB_ERROR;
731 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
735 talloc_free(data.dptr);
740 /**********************************
741 Single sid to id lookup function.
742 **********************************/
744 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
749 unsigned long rec_id = 0;
750 struct idmap_tdb_context *ctx;
751 TALLOC_CTX *tmp_ctx = talloc_stackframe();
753 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
755 keystr = sid_string_talloc(tmp_ctx, map->sid);
756 if (keystr == NULL) {
757 DEBUG(0, ("Out of memory!\n"));
758 ret = NT_STATUS_NO_MEMORY;
762 DEBUG(10,("Fetching record %s\n", keystr));
764 /* Check if sid is present in database */
765 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
767 DEBUG(10,("Record %s not found\n", keystr));
768 ret = NT_STATUS_NONE_MAPPED;
772 /* What type of record is this ? */
773 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
774 map->xid.id = rec_id;
775 map->xid.type = ID_TYPE_UID;
776 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
779 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
780 map->xid.id = rec_id;
781 map->xid.type = ID_TYPE_GID;
782 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
785 } else { /* Unknown record type ! */
786 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
787 ret = NT_STATUS_INTERNAL_DB_ERROR;
791 /* apply filters before returning result */
792 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
793 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
794 map->xid.id, dom->low_id, dom->high_id));
795 ret = NT_STATUS_NONE_MAPPED;
799 talloc_free(tmp_ctx);
803 /**********************************
804 lookup a set of unix ids.
805 **********************************/
807 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
809 struct idmap_tdb_context *ctx;
813 /* initialize the status to avoid suprise */
814 for (i = 0; ids[i]; i++) {
815 ids[i]->status = ID_UNKNOWN;
818 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
820 for (i = 0; ids[i]; i++) {
821 ret = idmap_tdb_id_to_sid(dom, ids[i]);
822 if ( ! NT_STATUS_IS_OK(ret)) {
824 /* if it is just a failed mapping continue */
825 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
827 /* make sure it is marked as unmapped */
828 ids[i]->status = ID_UNMAPPED;
832 /* some fatal error occurred, return immediately */
836 /* all ok, id is mapped */
837 ids[i]->status = ID_MAPPED;
846 /**********************************
847 lookup a set of sids.
848 **********************************/
850 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
852 struct idmap_tdb_context *ctx;
856 /* initialize the status to avoid suprise */
857 for (i = 0; ids[i]; i++) {
858 ids[i]->status = ID_UNKNOWN;
861 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
863 for (i = 0; ids[i]; i++) {
864 ret = idmap_tdb_sid_to_id(dom, ids[i]);
865 if ( ! NT_STATUS_IS_OK(ret)) {
867 /* if it is just a failed mapping continue */
868 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
870 /* make sure it is marked as unmapped */
871 ids[i]->status = ID_UNMAPPED;
875 /* some fatal error occurred, return immediately */
879 /* all ok, id is mapped */
880 ids[i]->status = ID_MAPPED;
889 /**********************************
890 Close the idmap tdb instance
891 **********************************/
893 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
895 struct idmap_tdb_context *ctx;
897 if (dom->private_data) {
898 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
900 TALLOC_FREE(ctx->db);
905 static struct idmap_methods db_methods = {
906 .init = idmap_tdb_db_init,
907 .unixids_to_sids = idmap_tdb_unixids_to_sids,
908 .sids_to_unixids = idmap_tdb_sids_to_unixids,
909 .allocate_id = idmap_tdb_get_new_id,
910 .close_fn = idmap_tdb_close
913 NTSTATUS idmap_tdb_init(void)
915 DEBUG(10, ("calling idmap_tdb_init\n"));
917 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);