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_compose(&sid, &domain->sid, rid);
106 sid_to_fstring(keystr, &sid);
107 key2 = string_term_tdb_data(keystr);
109 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
110 if (!NT_STATUS_IS_OK(status)) {
111 DEBUG(0,("Unable to add record %s:%s\n",
112 (const char *)key2.dptr,
118 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
119 if (!NT_STATUS_IS_OK(status)) {
120 DEBUG(0,("Unable to update record %s:%s\n",
121 (const char *)rec->value.dptr,
127 status = rec->delete_rec(rec);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(0,("Unable to delete record %s:%s\n",
130 (const char *)rec->key.dptr,
139 /*****************************************************************************
140 Convert the idmap database from an older version.
141 *****************************************************************************/
143 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
146 bool bigendianheader;
147 struct convert_fn_state s;
149 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
151 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
153 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
155 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
156 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
158 * high and low records were created on a
159 * big endian machine and will need byte-reversing.
164 wm = dbwrap_fetch_int32(db, HWM_USER);
172 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
173 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
177 wm = dbwrap_fetch_int32(db, HWM_GROUP);
184 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
185 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
193 /* the old format stored as DOMAIN/rid - now we store the SID direct */
194 db->traverse(db, convert_fn, &s);
197 DEBUG(0, ("Problem during conversion\n"));
201 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
202 DEBUG(0, ("Unable to store idmap version in databse\n"));
209 static NTSTATUS idmap_tdb_load_ranges(void)
216 if (!lp_idmap_uid(&low_uid, &high_uid)) {
217 DEBUG(1, ("idmap uid missing\n"));
218 return NT_STATUS_UNSUCCESSFUL;
221 if (!lp_idmap_gid(&low_gid, &high_gid)) {
222 DEBUG(1, ("idmap gid missing\n"));
223 return NT_STATUS_UNSUCCESSFUL;
226 idmap_tdb_state.low_uid = low_uid;
227 idmap_tdb_state.high_uid = high_uid;
228 idmap_tdb_state.low_gid = low_gid;
229 idmap_tdb_state.high_gid = high_gid;
231 if (idmap_tdb_state.high_uid <= idmap_tdb_state.low_uid) {
232 DEBUG(1, ("idmap uid range missing or invalid\n"));
233 return NT_STATUS_UNSUCCESSFUL;
236 if (idmap_tdb_state.high_gid <= idmap_tdb_state.low_gid) {
237 DEBUG(1, ("idmap gid range missing or invalid\n"));
238 return NT_STATUS_UNSUCCESSFUL;
244 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
248 char *tdbfile = NULL;
249 struct db_context *db = NULL;
251 bool config_error = false;
252 struct idmap_tdb_context *ctx;
254 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
256 /* use our own context here */
257 mem_ctx = talloc_stackframe();
259 /* use the old database if present */
260 tdbfile = 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(mem_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(dom, db)) {
294 db->transaction_cancel(db);
295 DEBUG(0, ("Unable to open idmap database, it's in an old format, 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 ctx->db = talloc_move(ctx, &db);
311 talloc_free(mem_ctx);
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_init_hwm(struct idmap_domain *dom)
330 bool update_uid = false;
331 bool update_gid = false;
332 struct idmap_tdb_context *ctx;
334 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
336 low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
337 if (low_uid == -1 || low_uid < dom->low_id) {
341 low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
342 if (low_gid == -1 || low_gid < dom->low_id) {
346 if (!update_uid && !update_gid) {
350 if (ctx->db->transaction_start(ctx->db) != 0) {
351 DEBUG(0, ("Unable to start upgrade transaction!\n"));
352 return NT_STATUS_INTERNAL_DB_ERROR;
356 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
358 ctx->db->transaction_cancel(ctx->db);
359 DEBUG(0, ("Unable to initialise user hwm in idmap "
361 return NT_STATUS_INTERNAL_DB_ERROR;
366 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
368 ctx->db->transaction_cancel(ctx->db);
369 DEBUG(0, ("Unable to initialise group hwm in idmap "
371 return NT_STATUS_INTERNAL_DB_ERROR;
375 if (ctx->db->transaction_commit(ctx->db) != 0) {
376 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
377 return NT_STATUS_INTERNAL_DB_ERROR;
383 /**********************************
385 **********************************/
387 struct idmap_tdb_allocate_id_context {
394 static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
398 struct idmap_tdb_allocate_id_context *state;
401 state = (struct idmap_tdb_allocate_id_context *)private_data;
403 hwm = dbwrap_fetch_int32(db, state->hwmkey);
405 ret = NT_STATUS_INTERNAL_DB_ERROR;
409 /* check it is in the range */
410 if (hwm > state->high_hwm) {
411 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
412 state->hwmtype, (unsigned long)state->high_hwm));
413 ret = NT_STATUS_UNSUCCESSFUL;
417 /* fetch a new id and increment it */
418 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
419 if (!NT_STATUS_IS_OK(ret)) {
420 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
421 state->hwmtype, nt_errstr(ret)));
425 /* recheck it is in the range */
426 if (hwm > state->high_hwm) {
427 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
428 state->hwmtype, (unsigned long)state->high_hwm));
429 ret = NT_STATUS_UNSUCCESSFUL;
440 static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
447 struct idmap_tdb_allocate_id_context state;
449 /* Get current high water mark */
455 high_hwm = idmap_tdb_state.high_uid;
461 high_hwm = idmap_tdb_state.high_gid;
465 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
466 return NT_STATUS_INVALID_PARAMETER;
470 state.high_hwm = high_hwm;
471 state.hwmtype = hwmtype;
472 state.hwmkey = hwmkey;
474 status = dbwrap_trans_do(idmap_alloc_db, idmap_tdb_allocate_id_action,
477 if (NT_STATUS_IS_OK(status)) {
479 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
481 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
488 * Allocate a new unix-ID.
489 * For now this is for the default idmap domain only.
490 * Should be extended later on.
492 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
497 if (!strequal(dom->name, "*")) {
498 DEBUG(3, ("idmap_tdb_get_new_id: "
499 "Refusing allocation of a new unixid for domain'%s'. "
500 "Currently only supported for the default "
503 return NT_STATUS_NOT_IMPLEMENTED;
506 ret = idmap_tdb_allocate_id(id);
511 /**********************************
513 **********************************/
515 static NTSTATUS idmap_tdb_alloc_close(void)
517 TALLOC_FREE(idmap_alloc_db);
521 /**********************************************************************
522 IDMAP MAPPING TDB BACKEND
523 **********************************************************************/
525 /*****************************
526 Initialise idmap database.
527 *****************************/
529 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
532 struct idmap_tdb_context *ctx;
534 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
536 ctx = talloc(dom, struct idmap_tdb_context);
538 DEBUG(0, ("Out of memory!\n"));
539 return NT_STATUS_NO_MEMORY;
542 /* load backend specific configuration here: */
544 if (strequal(dom->name, "*")) {
549 dom->private_data = ctx;
551 ret = idmap_tdb_open_db(dom);
552 if ( ! NT_STATUS_IS_OK(ret)) {
563 /**********************************
564 Single id to sid lookup function.
565 **********************************/
567 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
572 struct idmap_tdb_context *ctx;
575 return NT_STATUS_INVALID_PARAMETER;
578 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
580 /* apply filters before checking */
581 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
582 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
583 map->xid.id, dom->low_id, dom->high_id));
584 return NT_STATUS_NONE_MAPPED;
587 switch (map->xid.type) {
590 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
594 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
598 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
599 return NT_STATUS_INVALID_PARAMETER;
602 /* final SAFE_FREE safe */
605 if (keystr == NULL) {
606 DEBUG(0, ("Out of memory!\n"));
607 ret = NT_STATUS_NO_MEMORY;
611 DEBUG(10,("Fetching record %s\n", keystr));
613 /* Check if the mapping exists */
614 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
617 DEBUG(10,("Record %s not found\n", keystr));
618 ret = NT_STATUS_NONE_MAPPED;
622 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
623 DEBUG(10,("INVALID SID (%s) in record %s\n",
624 (const char *)data.dptr, keystr));
625 ret = NT_STATUS_INTERNAL_DB_ERROR;
629 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
633 talloc_free(data.dptr);
638 /**********************************
639 Single sid to id lookup function.
640 **********************************/
642 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
647 unsigned long rec_id = 0;
648 struct idmap_tdb_context *ctx;
649 TALLOC_CTX *tmp_ctx = talloc_stackframe();
651 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
653 keystr = sid_string_talloc(tmp_ctx, map->sid);
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 sid is present in database */
663 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
665 DEBUG(10,("Record %s not found\n", keystr));
666 ret = NT_STATUS_NONE_MAPPED;
670 /* What type of record is this ? */
671 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
672 map->xid.id = rec_id;
673 map->xid.type = ID_TYPE_UID;
674 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
677 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
678 map->xid.id = rec_id;
679 map->xid.type = ID_TYPE_GID;
680 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
683 } else { /* Unknown record type ! */
684 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
685 ret = NT_STATUS_INTERNAL_DB_ERROR;
689 /* apply filters before returning result */
690 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
691 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
692 map->xid.id, dom->low_id, dom->high_id));
693 ret = NT_STATUS_NONE_MAPPED;
697 talloc_free(tmp_ctx);
701 /**********************************
702 lookup a set of unix ids.
703 **********************************/
705 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
707 struct idmap_tdb_context *ctx;
711 /* initialize the status to avoid suprise */
712 for (i = 0; ids[i]; i++) {
713 ids[i]->status = ID_UNKNOWN;
716 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
718 for (i = 0; ids[i]; i++) {
719 ret = idmap_tdb_id_to_sid(dom, ids[i]);
720 if ( ! NT_STATUS_IS_OK(ret)) {
722 /* if it is just a failed mapping continue */
723 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
725 /* make sure it is marked as unmapped */
726 ids[i]->status = ID_UNMAPPED;
730 /* some fatal error occurred, return immediately */
734 /* all ok, id is mapped */
735 ids[i]->status = ID_MAPPED;
744 /**********************************
745 lookup a set of sids.
746 **********************************/
748 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
750 struct idmap_tdb_context *ctx;
754 /* initialize the status to avoid suprise */
755 for (i = 0; ids[i]; i++) {
756 ids[i]->status = ID_UNKNOWN;
759 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
761 for (i = 0; ids[i]; i++) {
762 ret = idmap_tdb_sid_to_id(dom, ids[i]);
763 if ( ! NT_STATUS_IS_OK(ret)) {
765 /* if it is just a failed mapping continue */
766 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
768 /* make sure it is marked as unmapped */
769 ids[i]->status = ID_UNMAPPED;
773 /* some fatal error occurred, return immediately */
777 /* all ok, id is mapped */
778 ids[i]->status = ID_MAPPED;
787 /**********************************
789 **********************************/
791 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
792 const struct id_map *map)
794 struct idmap_tdb_context *ctx;
797 char *ksidstr, *kidstr;
800 if (!map || !map->sid) {
801 return NT_STATUS_INVALID_PARAMETER;
804 ksidstr = kidstr = NULL;
806 /* TODO: should we filter a set_mapping using low/high filters ? */
808 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
810 switch (map->xid.type) {
813 kidstr = talloc_asprintf(ctx, "UID %lu",
814 (unsigned long)map->xid.id);
818 kidstr = talloc_asprintf(ctx, "GID %lu",
819 (unsigned long)map->xid.id);
823 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
824 return NT_STATUS_INVALID_PARAMETER;
827 if (kidstr == NULL) {
828 DEBUG(0, ("ERROR: Out of memory!\n"));
829 ret = NT_STATUS_NO_MEMORY;
833 if ((ksidstr = talloc_asprintf(
834 ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
835 DEBUG(0, ("Out of memory!\n"));
836 ret = NT_STATUS_NO_MEMORY;
840 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr, kidstr));
841 kid = string_term_tdb_data(kidstr);
842 ksid = string_term_tdb_data(ksidstr);
844 if (ctx->db->transaction_start(ctx->db) != 0) {
845 DEBUG(0, ("Failed to start transaction for %s\n",
847 ret = NT_STATUS_INTERNAL_DB_ERROR;
851 ret = dbwrap_store(ctx->db, ksid, kid, TDB_REPLACE);
852 if (!NT_STATUS_IS_OK(ret)) {
853 ctx->db->transaction_cancel(ctx->db);
854 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
855 ksidstr, kidstr, nt_errstr(ret)));
858 ret = dbwrap_store(ctx->db, kid, ksid, TDB_REPLACE);
859 if (!NT_STATUS_IS_OK(ret)) {
860 ctx->db->transaction_cancel(ctx->db);
861 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
862 kidstr, ksidstr, nt_errstr(ret)));
866 if (ctx->db->transaction_commit(ctx->db) != 0) {
867 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
869 ret = NT_STATUS_INTERNAL_DB_ERROR;
873 DEBUG(10,("Stored %s <-> %s\n", ksidstr, kidstr));
877 talloc_free(ksidstr);
882 /**********************************
883 Close the idmap tdb instance
884 **********************************/
886 static NTSTATUS idmap_tdb_close(struct idmap_domain *dom)
888 struct idmap_tdb_context *ctx;
890 if (dom->private_data) {
891 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
893 TALLOC_FREE(ctx->db);
898 static struct idmap_methods db_methods = {
899 .init = idmap_tdb_db_init,
900 .unixids_to_sids = idmap_tdb_unixids_to_sids,
901 .sids_to_unixids = idmap_tdb_sids_to_unixids,
902 .allocate_id = idmap_tdb_get_new_id,
903 .close_fn = idmap_tdb_close
906 NTSTATUS idmap_tdb_init(void)
908 DEBUG(10, ("calling idmap_tdb_init\n"));
910 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);