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 static struct idmap_tdb_state {
46 /* User and group id pool */
47 uid_t low_uid, high_uid; /* Range of uids to allocate */
48 gid_t low_gid, high_gid; /* Range of gids to allocate */
52 struct convert_fn_state {
53 struct db_context *db;
57 /*****************************************************************************
58 For idmap conversion: convert one record to new format
59 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
61 *****************************************************************************/
62 static int convert_fn(struct db_record *rec, void *private_data)
64 struct winbindd_domain *domain;
72 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
74 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
76 p = strchr((const char *)rec->key.dptr, '/');
81 fstrcpy(dom_name, (const char *)rec->key.dptr);
84 domain = find_domain_from_name(dom_name);
86 /* We must delete the old record. */
87 DEBUG(0,("Unable to find domain %s\n", dom_name ));
88 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
90 status = rec->delete_rec(rec);
91 if (!NT_STATUS_IS_OK(status)) {
92 DEBUG(0, ("Unable to delete record %s:%s\n",
93 (const char *)rec->key.dptr,
104 sid_copy(&sid, &domain->sid);
105 sid_append_rid(&sid, rid);
107 sid_to_fstring(keystr, &sid);
108 key2 = string_term_tdb_data(keystr);
110 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
111 if (!NT_STATUS_IS_OK(status)) {
112 DEBUG(0,("Unable to add record %s:%s\n",
113 (const char *)key2.dptr,
119 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
120 if (!NT_STATUS_IS_OK(status)) {
121 DEBUG(0,("Unable to update record %s:%s\n",
122 (const char *)rec->value.dptr,
128 status = rec->delete_rec(rec);
129 if (!NT_STATUS_IS_OK(status)) {
130 DEBUG(0,("Unable to delete record %s:%s\n",
131 (const char *)rec->key.dptr,
140 /*****************************************************************************
141 Convert the idmap database from an older version.
142 *****************************************************************************/
144 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
147 bool bigendianheader;
148 struct convert_fn_state s;
150 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
152 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
154 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
156 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
157 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
159 * high and low records were created on a
160 * big endian machine and will need byte-reversing.
165 wm = dbwrap_fetch_int32(db, HWM_USER);
173 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
174 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
178 wm = dbwrap_fetch_int32(db, HWM_GROUP);
185 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
186 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
194 /* the old format stored as DOMAIN/rid - now we store the SID direct */
195 db->traverse(db, convert_fn, &s);
198 DEBUG(0, ("Problem during conversion\n"));
202 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
203 DEBUG(0, ("Unable to store idmap version in databse\n"));
210 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
215 bool update_uid = false;
216 bool update_gid = false;
217 struct idmap_tdb_context *ctx;
219 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
221 low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
222 if (low_uid == -1 || low_uid < dom->low_id) {
226 low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
227 if (low_gid == -1 || low_gid < dom->low_id) {
231 if (!update_uid && !update_gid) {
235 if (ctx->db->transaction_start(ctx->db) != 0) {
236 DEBUG(0, ("Unable to start upgrade transaction!\n"));
237 return NT_STATUS_INTERNAL_DB_ERROR;
241 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
243 ctx->db->transaction_cancel(ctx->db);
244 DEBUG(0, ("Unable to initialise user hwm in idmap "
246 return NT_STATUS_INTERNAL_DB_ERROR;
251 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
253 ctx->db->transaction_cancel(ctx->db);
254 DEBUG(0, ("Unable to initialise group hwm in idmap "
256 return NT_STATUS_INTERNAL_DB_ERROR;
260 if (ctx->db->transaction_commit(ctx->db) != 0) {
261 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
262 return NT_STATUS_INTERNAL_DB_ERROR;
268 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
272 char *tdbfile = NULL;
273 struct db_context *db = NULL;
275 bool config_error = false;
276 struct idmap_tdb_context *ctx;
278 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
280 /* use our own context here */
281 mem_ctx = talloc_stackframe();
283 /* use the old database if present */
284 tdbfile = state_path("winbindd_idmap.tdb");
286 DEBUG(0, ("Out of memory!\n"));
287 ret = NT_STATUS_NO_MEMORY;
291 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
293 /* Open idmap repository */
294 db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
296 DEBUG(0, ("Unable to open idmap database\n"));
297 ret = NT_STATUS_UNSUCCESSFUL;
301 /* check against earlier versions */
302 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
303 if (version != IDMAP_VERSION) {
305 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
306 "possible with incomplete configuration\n",
307 version, IDMAP_VERSION));
308 ret = NT_STATUS_UNSUCCESSFUL;
311 if (db->transaction_start(db) != 0) {
312 DEBUG(0, ("Unable to start upgrade transaction!\n"));
313 ret = NT_STATUS_INTERNAL_DB_ERROR;
317 if (!idmap_tdb_upgrade(dom, db)) {
318 db->transaction_cancel(db);
319 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
320 ret = NT_STATUS_INTERNAL_DB_ERROR;
324 if (db->transaction_commit(db) != 0) {
325 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
326 ret = NT_STATUS_INTERNAL_DB_ERROR;
331 ctx->db = talloc_move(ctx, &db);
333 ret = idmap_tdb_init_hwm(dom);
336 talloc_free(mem_ctx);
340 /**********************************************************************
341 IDMAP ALLOC TDB BACKEND
342 **********************************************************************/
344 static struct db_context *idmap_alloc_db;
346 /**********************************
348 **********************************/
350 struct idmap_tdb_allocate_id_context {
357 static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
361 struct idmap_tdb_allocate_id_context *state;
364 state = (struct idmap_tdb_allocate_id_context *)private_data;
366 hwm = dbwrap_fetch_int32(db, state->hwmkey);
368 ret = NT_STATUS_INTERNAL_DB_ERROR;
372 /* check it is in the range */
373 if (hwm > state->high_hwm) {
374 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
375 state->hwmtype, (unsigned long)state->high_hwm));
376 ret = NT_STATUS_UNSUCCESSFUL;
380 /* fetch a new id and increment it */
381 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
382 if (!NT_STATUS_IS_OK(ret)) {
383 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
384 state->hwmtype, nt_errstr(ret)));
388 /* recheck it is in the range */
389 if (hwm > state->high_hwm) {
390 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
391 state->hwmtype, (unsigned long)state->high_hwm));
392 ret = NT_STATUS_UNSUCCESSFUL;
403 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
410 struct idmap_tdb_allocate_id_context state;
412 /* Get current high water mark */
418 high_hwm = idmap_tdb_state.high_uid;
424 high_hwm = idmap_tdb_state.high_gid;
428 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
429 return NT_STATUS_INVALID_PARAMETER;
433 state.high_hwm = high_hwm;
434 state.hwmtype = hwmtype;
435 state.hwmkey = hwmkey;
437 status = dbwrap_trans_do(idmap_alloc_db, idmap_tdb_allocate_id_action,
440 if (NT_STATUS_IS_OK(status)) {
442 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
444 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
451 * Allocate a new unix-ID.
452 * For now this is for the default idmap domain only.
453 * Should be extended later on.
455 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
460 if (!strequal(dom->name, "*")) {
461 DEBUG(3, ("idmap_tdb_get_new_id: "
462 "Refusing allocation of a new unixid for domain'%s'. "
463 "Currently only supported for the default "
466 return NT_STATUS_NOT_IMPLEMENTED;
469 ret = idmap_tdb_allocate_id(id);
474 /**********************************
476 **********************************/
478 static NTSTATUS idmap_tdb_alloc_close(void)
480 TALLOC_FREE(idmap_alloc_db);
484 /**********************************************************************
485 IDMAP MAPPING TDB BACKEND
486 **********************************************************************/
488 /*****************************
489 Initialise idmap database.
490 *****************************/
492 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
495 struct idmap_tdb_context *ctx;
497 ctx = talloc(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 dom->private_data = ctx;
512 ret = idmap_tdb_open_db(dom);
513 if ( ! NT_STATUS_IS_OK(ret)) {
524 /**********************************
525 Single id to sid lookup function.
526 **********************************/
528 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
533 struct idmap_tdb_context *ctx;
536 return NT_STATUS_INVALID_PARAMETER;
539 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
541 /* apply filters before checking */
542 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
543 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
544 map->xid.id, dom->low_id, dom->high_id));
545 return NT_STATUS_NONE_MAPPED;
548 switch (map->xid.type) {
551 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
555 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
559 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
560 return NT_STATUS_INVALID_PARAMETER;
563 /* final SAFE_FREE safe */
566 if (keystr == NULL) {
567 DEBUG(0, ("Out of memory!\n"));
568 ret = NT_STATUS_NO_MEMORY;
572 DEBUG(10,("Fetching record %s\n", keystr));
574 /* Check if the mapping exists */
575 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
578 DEBUG(10,("Record %s not found\n", keystr));
579 ret = NT_STATUS_NONE_MAPPED;
583 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
584 DEBUG(10,("INVALID SID (%s) in record %s\n",
585 (const char *)data.dptr, keystr));
586 ret = NT_STATUS_INTERNAL_DB_ERROR;
590 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
594 talloc_free(data.dptr);
599 /**********************************
600 Single sid to id lookup function.
601 **********************************/
603 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
608 unsigned long rec_id = 0;
609 struct idmap_tdb_context *ctx;
610 TALLOC_CTX *tmp_ctx = talloc_stackframe();
612 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
614 keystr = sid_string_talloc(tmp_ctx, map->sid);
615 if (keystr == NULL) {
616 DEBUG(0, ("Out of memory!\n"));
617 ret = NT_STATUS_NO_MEMORY;
621 DEBUG(10,("Fetching record %s\n", keystr));
623 /* Check if sid is present in database */
624 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
626 DEBUG(10,("Record %s not found\n", keystr));
627 ret = NT_STATUS_NONE_MAPPED;
631 /* What type of record is this ? */
632 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
633 map->xid.id = rec_id;
634 map->xid.type = ID_TYPE_UID;
635 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
638 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
639 map->xid.id = rec_id;
640 map->xid.type = ID_TYPE_GID;
641 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
644 } else { /* Unknown record type ! */
645 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
646 ret = NT_STATUS_INTERNAL_DB_ERROR;
650 /* apply filters before returning result */
651 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
652 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
653 map->xid.id, dom->low_id, dom->high_id));
654 ret = NT_STATUS_NONE_MAPPED;
658 talloc_free(tmp_ctx);
662 /**********************************
663 lookup a set of unix ids.
664 **********************************/
666 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
668 struct idmap_tdb_context *ctx;
672 /* initialize the status to avoid suprise */
673 for (i = 0; ids[i]; i++) {
674 ids[i]->status = ID_UNKNOWN;
677 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
679 for (i = 0; ids[i]; i++) {
680 ret = idmap_tdb_id_to_sid(dom, ids[i]);
681 if ( ! NT_STATUS_IS_OK(ret)) {
683 /* if it is just a failed mapping continue */
684 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
686 /* make sure it is marked as unmapped */
687 ids[i]->status = ID_UNMAPPED;
691 /* some fatal error occurred, return immediately */
695 /* all ok, id is mapped */
696 ids[i]->status = ID_MAPPED;
705 /**********************************
706 lookup a set of sids.
707 **********************************/
709 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
711 struct idmap_tdb_context *ctx;
715 /* initialize the status to avoid suprise */
716 for (i = 0; ids[i]; i++) {
717 ids[i]->status = ID_UNKNOWN;
720 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
722 for (i = 0; ids[i]; i++) {
723 ret = idmap_tdb_sid_to_id(dom, ids[i]);
724 if ( ! NT_STATUS_IS_OK(ret)) {
726 /* if it is just a failed mapping continue */
727 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
729 /* make sure it is marked as unmapped */
730 ids[i]->status = ID_UNMAPPED;
734 /* some fatal error occurred, return immediately */
738 /* all ok, id is mapped */
739 ids[i]->status = ID_MAPPED;
748 /**********************************
750 **********************************/
752 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
753 const struct id_map *map)
755 struct idmap_tdb_context *ctx;
758 char *ksidstr, *kidstr;
761 if (!map || !map->sid) {
762 return NT_STATUS_INVALID_PARAMETER;
765 ksidstr = kidstr = NULL;
767 /* TODO: should we filter a set_mapping using low/high filters ? */
769 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
771 switch (map->xid.type) {
774 kidstr = talloc_asprintf(ctx, "UID %lu",
775 (unsigned long)map->xid.id);
779 kidstr = talloc_asprintf(ctx, "GID %lu",
780 (unsigned long)map->xid.id);
784 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
785 return NT_STATUS_INVALID_PARAMETER;
788 if (kidstr == NULL) {
789 DEBUG(0, ("ERROR: Out of memory!\n"));
790 ret = NT_STATUS_NO_MEMORY;
794 if ((ksidstr = talloc_asprintf(
795 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
796 DEBUG(0, ("Out of memory!\n"));
797 ret = NT_STATUS_NO_MEMORY;
801 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
802 kid = string_term_tdb_data(kidstr);
803 ksid = string_term_tdb_data(ksidstr);
805 if (ctx->db->transaction_start(ctx->db) != 0) {
806 DEBUG(0, ("Failed to start transaction for %s\n",
808 ret = NT_STATUS_INTERNAL_DB_ERROR;
812 ret = dbwrap_store(ctx->db, ksid, kid, TDB_REPLACE);
813 if (!NT_STATUS_IS_OK(ret)) {
814 ctx->db->transaction_cancel(ctx->db);
815 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
816 ksidstr, kidstr, nt_errstr(ret)));
819 ret = dbwrap_store(ctx->db, kid, ksid, TDB_REPLACE);
820 if (!NT_STATUS_IS_OK(ret)) {
821 ctx->db->transaction_cancel(ctx->db);
822 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
823 kidstr, ksidstr, nt_errstr(ret)));
827 if (ctx->db->transaction_commit(ctx->db) != 0) {
828 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
830 ret = NT_STATUS_INTERNAL_DB_ERROR;
834 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
838 talloc_free(ksidstr);
843 /**********************************
844 Close the idmap tdb instance
845 **********************************/
847 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
849 struct idmap_tdb_context *ctx;
851 if (dom->private_data) {
852 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
854 TALLOC_FREE(ctx->db);
859 static struct idmap_methods db_methods = {
860 .init = idmap_tdb_db_init,
861 .unixids_to_sids = idmap_tdb_unixids_to_sids,
862 .sids_to_unixids = idmap_tdb_sids_to_unixids,
863 .allocate_id = idmap_tdb_get_new_id,
864 .close_fn = idmap_tdb_close
867 NTSTATUS idmap_tdb_init(void)
869 DEBUG(10, ("calling idmap_tdb_init\n"));
871 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);