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 /* High water mark keys */
37 #define HWM_GROUP "GROUP HWM"
38 #define HWM_USER "USER HWM"
40 static struct idmap_tdb_state {
42 /* User and group id pool */
43 uid_t low_uid, high_uid; /* Range of uids to allocate */
44 gid_t low_gid, high_gid; /* Range of gids to allocate */
48 struct convert_fn_state {
49 struct db_context *db;
53 /*****************************************************************************
54 For idmap conversion: convert one record to new format
55 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
57 *****************************************************************************/
58 static int convert_fn(struct db_record *rec, void *private_data)
60 struct winbindd_domain *domain;
68 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
70 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
72 p = strchr((const char *)rec->key.dptr, '/');
77 fstrcpy(dom_name, (const char *)rec->key.dptr);
80 domain = find_domain_from_name(dom_name);
82 /* We must delete the old record. */
83 DEBUG(0,("Unable to find domain %s\n", dom_name ));
84 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
86 status = rec->delete_rec(rec);
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(0, ("Unable to delete record %s:%s\n",
89 (const char *)rec->key.dptr,
100 sid_copy(&sid, &domain->sid);
101 sid_append_rid(&sid, rid);
103 sid_to_fstring(keystr, &sid);
104 key2 = string_term_tdb_data(keystr);
106 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
107 if (!NT_STATUS_IS_OK(status)) {
108 DEBUG(0,("Unable to add record %s:%s\n",
109 (const char *)key2.dptr,
115 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
116 if (!NT_STATUS_IS_OK(status)) {
117 DEBUG(0,("Unable to update record %s:%s\n",
118 (const char *)rec->value.dptr,
124 status = rec->delete_rec(rec);
125 if (!NT_STATUS_IS_OK(status)) {
126 DEBUG(0,("Unable to delete record %s:%s\n",
127 (const char *)rec->key.dptr,
136 /*****************************************************************************
137 Convert the idmap database from an older version.
138 *****************************************************************************/
140 static bool idmap_tdb_upgrade(struct db_context *db)
143 bool bigendianheader;
144 struct convert_fn_state s;
146 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
148 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
150 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
152 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
153 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
155 * high and low records were created on a
156 * big endian machine and will need byte-reversing.
161 wm = dbwrap_fetch_int32(db, HWM_USER);
166 wm = idmap_tdb_state.low_uid;
169 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
170 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
174 wm = dbwrap_fetch_int32(db, HWM_GROUP);
178 wm = idmap_tdb_state.low_gid;
181 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
182 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
190 /* the old format stored as DOMAIN/rid - now we store the SID direct */
191 db->traverse(db, convert_fn, &s);
194 DEBUG(0, ("Problem during conversion\n"));
198 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
199 DEBUG(0, ("Unable to dtore idmap version in databse\n"));
206 static NTSTATUS idmap_tdb_open_db(TALLOC_CTX *memctx,
208 struct db_context **dbctx)
212 char *tdbfile = NULL;
213 struct db_context *db = NULL;
219 bool config_error = false;
222 if (!lp_idmap_uid(&low_uid, &high_uid)
223 || !lp_idmap_gid(&low_gid, &high_gid)) {
224 DEBUG(1, ("idmap uid or idmap gid missing\n"));
227 return NT_STATUS_UNSUCCESSFUL;
231 idmap_tdb_state.low_uid = low_uid;
232 idmap_tdb_state.high_uid = high_uid;
233 idmap_tdb_state.low_gid = low_gid;
234 idmap_tdb_state.high_gid = high_gid;
236 if (idmap_tdb_state.high_uid <= idmap_tdb_state.low_uid) {
237 DEBUG(1, ("idmap uid range missing or invalid\n"));
240 return NT_STATUS_UNSUCCESSFUL;
244 if (idmap_tdb_state.high_gid <= idmap_tdb_state.low_gid) {
245 DEBUG(1, ("idmap gid range missing or invalid\n"));
248 return NT_STATUS_UNSUCCESSFUL;
252 /* use our own context here */
253 ctx = talloc_new(memctx);
255 DEBUG(0, ("Out of memory!\n"));
256 return NT_STATUS_NO_MEMORY;
259 /* use the old database if present */
260 tdbfile = talloc_strdup(ctx, state_path("winbindd_idmap.tdb"));
262 DEBUG(0, ("Out of memory!\n"));
263 ret = NT_STATUS_NO_MEMORY;
267 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
269 /* Open idmap repository */
270 db = db_open(ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
272 DEBUG(0, ("Unable to open idmap database\n"));
273 ret = NT_STATUS_UNSUCCESSFUL;
277 /* check against earlier versions */
278 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
279 if (version != IDMAP_VERSION) {
281 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
282 "possible with incomplete configuration\n",
283 version, IDMAP_VERSION));
284 ret = NT_STATUS_UNSUCCESSFUL;
287 if (db->transaction_start(db) != 0) {
288 DEBUG(0, ("Unable to start upgrade transaction!\n"));
289 ret = NT_STATUS_INTERNAL_DB_ERROR;
293 if (!idmap_tdb_upgrade(db)) {
294 db->transaction_cancel(db);
295 DEBUG(0, ("Unable to open idmap database, it's in an old formati, and upgrade failed!\n"));
296 ret = NT_STATUS_INTERNAL_DB_ERROR;
300 if (db->transaction_commit(db) != 0) {
301 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
302 ret = NT_STATUS_INTERNAL_DB_ERROR;
307 *dbctx = talloc_move(memctx, &db);
315 /**********************************************************************
316 IDMAP ALLOC TDB BACKEND
317 **********************************************************************/
319 static struct db_context *idmap_alloc_db;
321 /**********************************
322 Initialise idmap alloc database.
323 **********************************/
325 static NTSTATUS idmap_tdb_alloc_init( const char *params )
331 bool update_uid = false;
332 bool update_gid = false;
334 status = idmap_tdb_open_db(NULL, true, &idmap_alloc_db);
335 if (!NT_STATUS_IS_OK(status)) {
336 DEBUG(0, ("idmap will be unable to map foreign SIDs: %s\n",
341 low_uid = dbwrap_fetch_int32(idmap_alloc_db, HWM_USER);
342 if (low_uid == -1 || low_uid < idmap_tdb_state.low_uid) {
346 low_gid = dbwrap_fetch_int32(idmap_alloc_db, HWM_GROUP);
347 if (low_gid == -1 || low_gid < idmap_tdb_state.low_gid) {
351 if (!update_uid && !update_gid) {
355 if (idmap_alloc_db->transaction_start(idmap_alloc_db) != 0) {
356 TALLOC_FREE(idmap_alloc_db);
357 DEBUG(0, ("Unable to start upgrade transaction!\n"));
358 return NT_STATUS_INTERNAL_DB_ERROR;
362 ret = dbwrap_store_int32(idmap_alloc_db, HWM_USER,
363 idmap_tdb_state.low_uid);
365 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
366 TALLOC_FREE(idmap_alloc_db);
367 DEBUG(0, ("Unable to initialise user hwm in idmap "
369 return NT_STATUS_INTERNAL_DB_ERROR;
374 ret = dbwrap_store_int32(idmap_alloc_db, HWM_GROUP,
375 idmap_tdb_state.low_gid);
377 idmap_alloc_db->transaction_cancel(idmap_alloc_db);
378 TALLOC_FREE(idmap_alloc_db);
379 DEBUG(0, ("Unable to initialise group hwm in idmap "
381 return NT_STATUS_INTERNAL_DB_ERROR;
385 if (idmap_alloc_db->transaction_commit(idmap_alloc_db) != 0) {
386 TALLOC_FREE(idmap_alloc_db);
387 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
388 return NT_STATUS_INTERNAL_DB_ERROR;
394 /**********************************
396 **********************************/
398 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
406 /* Get current high water mark */
412 high_hwm = idmap_tdb_state.high_uid;
418 high_hwm = idmap_tdb_state.high_gid;
422 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
423 return NT_STATUS_INVALID_PARAMETER;
426 if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
427 return NT_STATUS_INTERNAL_DB_ERROR;
430 /* check it is in the range */
431 if (hwm > high_hwm) {
432 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
433 hwmtype, (unsigned long)high_hwm));
434 return NT_STATUS_UNSUCCESSFUL;
437 /* fetch a new id and increment it */
438 ret = dbwrap_change_uint32_atomic(idmap_alloc_db, hwmkey, &hwm, 1);
440 DEBUG(0, ("Fatal error while fetching a new %s value\n!", hwmtype));
441 return NT_STATUS_UNSUCCESSFUL;
444 /* recheck it is in the range */
445 if (hwm > high_hwm) {
446 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
447 hwmtype, (unsigned long)high_hwm));
448 return NT_STATUS_UNSUCCESSFUL;
452 DEBUG(10,("New %s = %d\n", hwmtype, hwm));
457 /**********************************
458 Get current highest id.
459 **********************************/
461 static NTSTATUS idmap_tdb_get_hwm(struct unixid *xid)
468 /* Get current high water mark */
474 high_hwm = idmap_tdb_state.high_uid;
480 high_hwm = idmap_tdb_state.high_gid;
484 return NT_STATUS_INVALID_PARAMETER;
487 if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
488 return NT_STATUS_INTERNAL_DB_ERROR;
493 /* Warn if it is out of range */
494 if (hwm >= high_hwm) {
495 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
496 hwmtype, (unsigned long)high_hwm));
502 /**********************************
504 **********************************/
506 static NTSTATUS idmap_tdb_set_hwm(struct unixid *xid)
513 /* Get current high water mark */
519 high_hwm = idmap_tdb_state.high_uid;
525 high_hwm = idmap_tdb_state.high_gid;
529 return NT_STATUS_INVALID_PARAMETER;
534 if ((hwm = dbwrap_store_uint32(idmap_alloc_db, hwmkey, hwm)) == -1) {
535 return NT_STATUS_INTERNAL_DB_ERROR;
538 /* Warn if it is out of range */
539 if (hwm >= high_hwm) {
540 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
541 hwmtype, (unsigned long)high_hwm));
547 /**********************************
549 **********************************/
551 static NTSTATUS idmap_tdb_alloc_close(void)
553 TALLOC_FREE(idmap_alloc_db);
557 /**********************************************************************
558 IDMAP MAPPING TDB BACKEND
559 **********************************************************************/
561 struct idmap_tdb_context {
562 struct db_context *db;
563 uint32_t filter_low_id;
564 uint32_t filter_high_id;
567 /*****************************
568 Initialise idmap database.
569 *****************************/
571 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
574 struct idmap_tdb_context *ctx;
575 char *config_option = NULL;
578 ctx = talloc(dom, struct idmap_tdb_context);
580 DEBUG(0, ("Out of memory!\n"));
581 return NT_STATUS_NO_MEMORY;
584 config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
585 if ( ! config_option) {
586 DEBUG(0, ("Out of memory!\n"));
587 ret = NT_STATUS_NO_MEMORY;
591 ret = idmap_tdb_open_db(ctx, false, &ctx->db);
592 if ( ! NT_STATUS_IS_OK(ret)) {
596 range = lp_parm_const_string(-1, config_option, "range", NULL);
598 (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
599 (ctx->filter_low_id > ctx->filter_high_id)) {
600 ctx->filter_low_id = 0;
601 ctx->filter_high_id = 0;
604 dom->private_data = ctx;
606 talloc_free(config_option);
614 /**********************************
615 Single id to sid lookup function.
616 **********************************/
618 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_tdb_context *ctx, struct id_map *map)
625 return NT_STATUS_INVALID_PARAMETER;
628 /* apply filters before checking */
629 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
630 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
631 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
632 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
633 return NT_STATUS_NONE_MAPPED;
636 switch (map->xid.type) {
639 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
643 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
647 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
648 return NT_STATUS_INVALID_PARAMETER;
651 /* final SAFE_FREE safe */
654 if (keystr == NULL) {
655 DEBUG(0, ("Out of memory!\n"));
656 ret = NT_STATUS_NO_MEMORY;
660 DEBUG(10,("Fetching record %s\n", keystr));
662 /* Check if the mapping exists */
663 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
666 DEBUG(10,("Record %s not found\n", keystr));
667 ret = NT_STATUS_NONE_MAPPED;
671 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
672 DEBUG(10,("INVALID SID (%s) in record %s\n",
673 (const char *)data.dptr, keystr));
674 ret = NT_STATUS_INTERNAL_DB_ERROR;
678 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
682 talloc_free(data.dptr);
687 /**********************************
688 Single sid to id lookup function.
689 **********************************/
691 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_tdb_context *ctx, struct id_map *map)
696 unsigned long rec_id = 0;
697 TALLOC_CTX *tmp_ctx = talloc_stackframe();
699 keystr = sid_string_talloc(tmp_ctx, map->sid);
700 if (keystr == NULL) {
701 DEBUG(0, ("Out of memory!\n"));
702 ret = NT_STATUS_NO_MEMORY;
706 DEBUG(10,("Fetching record %s\n", keystr));
708 /* Check if sid is present in database */
709 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
711 DEBUG(10,("Record %s not found\n", keystr));
712 ret = NT_STATUS_NONE_MAPPED;
716 /* What type of record is this ? */
717 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
718 map->xid.id = rec_id;
719 map->xid.type = ID_TYPE_UID;
720 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
723 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
724 map->xid.id = rec_id;
725 map->xid.type = ID_TYPE_GID;
726 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
729 } else { /* Unknown record type ! */
730 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
731 ret = NT_STATUS_INTERNAL_DB_ERROR;
734 /* apply filters before returning result */
735 if ((ctx->filter_low_id && (map->xid.id < ctx->filter_low_id)) ||
736 (ctx->filter_high_id && (map->xid.id > ctx->filter_high_id))) {
737 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
738 map->xid.id, ctx->filter_low_id, ctx->filter_high_id));
739 ret = NT_STATUS_NONE_MAPPED;
743 talloc_free(tmp_ctx);
747 /**********************************
748 lookup a set of unix ids.
749 **********************************/
751 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
753 struct idmap_tdb_context *ctx;
757 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
759 for (i = 0; ids[i]; i++) {
760 ret = idmap_tdb_id_to_sid(ctx, ids[i]);
761 if ( ! NT_STATUS_IS_OK(ret)) {
763 /* if it is just a failed mapping continue */
764 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
766 /* make sure it is marked as unmapped */
767 ids[i]->status = ID_UNMAPPED;
771 /* some fatal error occurred, return immediately */
775 /* all ok, id is mapped */
776 ids[i]->status = ID_MAPPED;
785 /**********************************
786 lookup a set of sids.
787 **********************************/
789 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
791 struct idmap_tdb_context *ctx;
795 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
797 for (i = 0; ids[i]; i++) {
798 ret = idmap_tdb_sid_to_id(ctx, ids[i]);
799 if ( ! NT_STATUS_IS_OK(ret)) {
801 /* if it is just a failed mapping continue */
802 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
804 /* make sure it is marked as unmapped */
805 ids[i]->status = ID_UNMAPPED;
809 /* some fatal error occurred, return immediately */
813 /* all ok, id is mapped */
814 ids[i]->status = ID_MAPPED;
823 /**********************************
825 **********************************/
827 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
828 const struct id_map *map)
830 struct idmap_tdb_context *ctx;
833 char *ksidstr, *kidstr;
836 if (!map || !map->sid) {
837 return NT_STATUS_INVALID_PARAMETER;
840 ksidstr = kidstr = NULL;
842 /* TODO: should we filter a set_mapping using low/high filters ? */
844 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
846 switch (map->xid.type) {
849 kidstr = talloc_asprintf(ctx, "UID %lu",
850 (unsigned long)map->xid.id);
854 kidstr = talloc_asprintf(ctx, "GID %lu",
855 (unsigned long)map->xid.id);
859 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
860 return NT_STATUS_INVALID_PARAMETER;
863 if (kidstr == NULL) {
864 DEBUG(0, ("ERROR: Out of memory!\n"));
865 ret = NT_STATUS_NO_MEMORY;
869 if ((ksidstr = talloc_asprintf(
870 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
871 DEBUG(0, ("Out of memory!\n"));
872 ret = NT_STATUS_NO_MEMORY;
876 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
877 kid = string_term_tdb_data(kidstr);
878 ksid = string_term_tdb_data(ksidstr);
880 if (ctx->db->transaction_start(ctx->db) != 0) {
881 DEBUG(0, ("Failed to start transaction for %s\n",
883 ret = NT_STATUS_INTERNAL_DB_ERROR;
887 ret = dbwrap_store(ctx->db, ksid, kid, TDB_REPLACE);
888 if (!NT_STATUS_IS_OK(ret)) {
889 ctx->db->transaction_cancel(ctx->db);
890 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
891 ksidstr, kidstr, nt_errstr(ret)));
894 ret = dbwrap_store(ctx->db, kid, ksid, TDB_REPLACE);
895 if (!NT_STATUS_IS_OK(ret)) {
896 ctx->db->transaction_cancel(ctx->db);
897 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
898 kidstr, ksidstr, nt_errstr(ret)));
902 if (ctx->db->transaction_commit(ctx->db) != 0) {
903 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
905 ret = NT_STATUS_INTERNAL_DB_ERROR;
909 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
913 talloc_free(ksidstr);
918 /**********************************
920 **********************************/
922 static NTSTATUS idmap_tdb_remove_mapping(struct idmap_domain *dom,
923 const struct id_map *map)
925 struct idmap_tdb_context *ctx;
927 TDB_DATA ksid, kid, data;
928 char *ksidstr, *kidstr;
931 if (!map || !map->sid) {
932 return NT_STATUS_INVALID_PARAMETER;
935 ksidstr = kidstr = NULL;
938 /* TODO: should we filter a remove_mapping using low/high filters ? */
940 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
942 switch (map->xid.type) {
945 kidstr = talloc_asprintf(ctx, "UID %lu",
946 (unsigned long)map->xid.id);
950 kidstr = talloc_asprintf(ctx, "GID %lu",
951 (unsigned long)map->xid.id);
955 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
956 return NT_STATUS_INVALID_PARAMETER;
959 if (kidstr == NULL) {
960 DEBUG(0, ("ERROR: Out of memory!\n"));
961 ret = NT_STATUS_NO_MEMORY;
965 if ((ksidstr = talloc_asprintf(
966 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
967 DEBUG(0, ("Out of memory!\n"));
968 ret = NT_STATUS_NO_MEMORY;
972 DEBUG(10, ("Checking %s <-> %s map\n", ksidstr, kidstr));
973 ksid = string_term_tdb_data(ksidstr);
974 kid = string_term_tdb_data(kidstr);
976 if (ctx->db->transaction_start(ctx->db) != 0) {
977 DEBUG(0, ("Failed to start transaction for %s\n",
979 return NT_STATUS_INTERNAL_DB_ERROR;
982 /* Check if sid is present in database */
983 data = dbwrap_fetch(ctx->db, NULL, ksid);
985 ctx->db->transaction_cancel(ctx->db);
986 DEBUG(10,("Record %s not found\n", ksidstr));
987 ret = NT_STATUS_NONE_MAPPED;
991 /* Check if sid is mapped to the specified ID */
992 if ((data.dsize != kid.dsize) ||
993 (memcmp(data.dptr, kid.dptr, data.dsize) != 0)) {
994 ctx->db->transaction_cancel(ctx->db);
995 DEBUG(10,("Specified SID does not map to specified ID\n"));
996 DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr,
997 (const char *)data.dptr));
998 ret = NT_STATUS_NONE_MAPPED;
1002 DEBUG(10, ("Removing %s <-> %s map\n", ksidstr, kidstr));
1004 /* Delete previous mappings. */
1006 DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr, kidstr ));
1007 ret = dbwrap_delete(ctx->db, ksid);
1008 if (!NT_STATUS_IS_OK(ret)) {
1009 DEBUG(0,("Warning: Failed to delete %s: %s\n",
1010 ksidstr, nt_errstr(ret)));
1013 DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr, ksidstr ));
1014 ret = dbwrap_delete(ctx->db, kid);
1015 if (!NT_STATUS_IS_OK(ret)) {
1016 DEBUG(0,("Warning: Failed to delete %s: %s\n",
1017 kidstr, nt_errstr(ret)));
1020 if (ctx->db->transaction_commit(ctx->db) != 0) {
1021 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
1023 ret = NT_STATUS_INTERNAL_DB_ERROR;
1030 talloc_free(ksidstr);
1031 talloc_free(kidstr);
1032 talloc_free(data.dptr);
1036 /**********************************
1037 Close the idmap tdb instance
1038 **********************************/
1040 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
1042 struct idmap_tdb_context *ctx;
1044 if (dom->private_data) {
1045 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1047 TALLOC_FREE(ctx->db);
1049 return NT_STATUS_OK;
1054 struct id_map **maps;
1059 static int idmap_tdb_dump_one_entry(struct db_record *rec, void *pdata)
1061 struct dump_data *data = talloc_get_type(pdata, struct dump_data);
1062 struct id_map *maps;
1063 int num_maps = *data->num_maps;
1065 /* ignore any record but the ones with a SID as key */
1066 if (strncmp((const char *)rec->key.dptr, "S-", 2) == 0) {
1068 maps = talloc_realloc(NULL, *data->maps, struct id_map, num_maps+1);
1070 DEBUG(0, ("Out of memory!\n"));
1071 data->ret = NT_STATUS_NO_MEMORY;
1075 maps[num_maps].sid = talloc(maps, DOM_SID);
1076 if ( ! maps[num_maps].sid) {
1077 DEBUG(0, ("Out of memory!\n"));
1078 data->ret = NT_STATUS_NO_MEMORY;
1082 if (!string_to_sid(maps[num_maps].sid, (const char *)rec->key.dptr)) {
1083 DEBUG(10,("INVALID record %s\n", (const char *)rec->key.dptr));
1084 /* continue even with errors */
1088 /* Try a UID record. */
1089 if (sscanf((const char *)rec->value.dptr, "UID %u", &(maps[num_maps].xid.id)) == 1) {
1090 maps[num_maps].xid.type = ID_TYPE_UID;
1091 maps[num_maps].status = ID_MAPPED;
1092 *data->num_maps = num_maps + 1;
1094 /* Try a GID record. */
1096 if (sscanf((const char *)rec->value.dptr, "GID %u", &(maps[num_maps].xid.id)) == 1) {
1097 maps[num_maps].xid.type = ID_TYPE_GID;
1098 maps[num_maps].status = ID_MAPPED;
1099 *data->num_maps = num_maps + 1;
1101 /* Unknown record type ! */
1103 maps[num_maps].status = ID_UNKNOWN;
1104 DEBUG(2, ("Found INVALID record %s -> %s\n",
1105 (const char *)rec->key.dptr,
1106 (const char *)rec->value.dptr));
1107 /* do not increment num_maps */
1114 /**********************************
1115 Dump all mappings out
1116 **********************************/
1118 static NTSTATUS idmap_tdb_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
1120 struct idmap_tdb_context *ctx;
1121 struct dump_data *data;
1122 NTSTATUS ret = NT_STATUS_OK;
1124 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
1126 data = TALLOC_ZERO_P(ctx, struct dump_data);
1128 DEBUG(0, ("Out of memory!\n"));
1129 return NT_STATUS_NO_MEMORY;
1132 data->num_maps = num_maps;
1133 data->ret = NT_STATUS_OK;
1135 ctx->db->traverse_read(ctx->db, idmap_tdb_dump_one_entry, data);
1137 if ( ! NT_STATUS_IS_OK(data->ret)) {
1145 static struct idmap_methods db_methods = {
1147 .init = idmap_tdb_db_init,
1148 .unixids_to_sids = idmap_tdb_unixids_to_sids,
1149 .sids_to_unixids = idmap_tdb_sids_to_unixids,
1150 .set_mapping = idmap_tdb_set_mapping,
1151 .remove_mapping = idmap_tdb_remove_mapping,
1152 .dump_data = idmap_tdb_dump_data,
1153 .close_fn = idmap_tdb_close
1156 static struct idmap_alloc_methods db_alloc_methods = {
1158 .init = idmap_tdb_alloc_init,
1159 .allocate_id = idmap_tdb_allocate_id,
1160 .get_id_hwm = idmap_tdb_get_hwm,
1161 .set_id_hwm = idmap_tdb_set_hwm,
1162 .close_fn = idmap_tdb_alloc_close
1165 NTSTATUS idmap_alloc_tdb_init(void)
1167 return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_alloc_methods);
1170 NTSTATUS idmap_tdb_init(void)
1174 DEBUG(10, ("calling idmap_tdb_init\n"));
1176 /* FIXME: bad hack to actually register also the alloc_tdb module without changining configure.in */
1177 ret = idmap_alloc_tdb_init();
1178 if (! NT_STATUS_IS_OK(ret)) {
1181 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);