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 "dbwrap/dbwrap_open.h"
33 #include "../libcli/security/security.h"
37 #define DBGC_CLASS DBGC_IDMAP
39 /* idmap version determines auto-conversion - this is the database
40 structure version specifier. */
42 #define IDMAP_VERSION 2
44 struct idmap_tdb_context {
45 struct db_context *db;
46 struct idmap_rw_ops *rw_ops;
49 /* High water mark keys */
50 #define HWM_GROUP "GROUP HWM"
51 #define HWM_USER "USER HWM"
53 struct convert_fn_state {
54 struct db_context *db;
58 /*****************************************************************************
59 For idmap conversion: convert one record to new format
60 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
62 *****************************************************************************/
63 static int convert_fn(struct db_record *rec, void *private_data)
65 struct winbindd_domain *domain;
73 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
75 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
77 p = strchr((const char *)rec->key.dptr, '/');
82 fstrcpy(dom_name, (const char *)rec->key.dptr);
85 domain = find_domain_from_name(dom_name);
87 /* We must delete the old record. */
88 DEBUG(0,("Unable to find domain %s\n", dom_name ));
89 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
91 status = rec->delete_rec(rec);
92 if (!NT_STATUS_IS_OK(status)) {
93 DEBUG(0, ("Unable to delete record %s:%s\n",
94 (const char *)rec->key.dptr,
105 sid_compose(&sid, &domain->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;
151 /* If we are bigendian, tdb is bigendian if NOT converted. */
154 unsigned char small[2];
157 if (u.small[0] == 0x01)
158 bigendianheader = !(db->get_flags(db) & TDB_CONVERT);
160 assert(u.small[0] == 0x02);
161 bigendianheader = (db->get_flags(db) & TDB_CONVERT);
164 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
166 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
168 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
170 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
171 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
173 * high and low records were created on a
174 * big endian machine and will need byte-reversing.
179 wm = dbwrap_fetch_int32(db, HWM_USER);
187 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
188 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
192 wm = dbwrap_fetch_int32(db, HWM_GROUP);
199 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
200 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
208 /* the old format stored as DOMAIN/rid - now we store the SID direct */
209 db->traverse(db, convert_fn, &s);
212 DEBUG(0, ("Problem during conversion\n"));
216 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
217 DEBUG(0, ("Unable to store idmap version in database\n"));
224 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
229 bool update_uid = false;
230 bool update_gid = false;
231 struct idmap_tdb_context *ctx;
233 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
235 low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
236 if (low_uid == -1 || low_uid < dom->low_id) {
240 low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
241 if (low_gid == -1 || low_gid < dom->low_id) {
245 if (!update_uid && !update_gid) {
249 if (ctx->db->transaction_start(ctx->db) != 0) {
250 DEBUG(0, ("Unable to start upgrade transaction!\n"));
251 return NT_STATUS_INTERNAL_DB_ERROR;
255 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
257 ctx->db->transaction_cancel(ctx->db);
258 DEBUG(0, ("Unable to initialise user hwm in idmap "
260 return NT_STATUS_INTERNAL_DB_ERROR;
265 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
267 ctx->db->transaction_cancel(ctx->db);
268 DEBUG(0, ("Unable to initialise group hwm in idmap "
270 return NT_STATUS_INTERNAL_DB_ERROR;
274 if (ctx->db->transaction_commit(ctx->db) != 0) {
275 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
276 return NT_STATUS_INTERNAL_DB_ERROR;
282 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
286 char *tdbfile = NULL;
287 struct db_context *db = NULL;
289 bool config_error = false;
290 struct idmap_tdb_context *ctx;
292 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
295 /* it is already open */
299 /* use our own context here */
300 mem_ctx = talloc_stackframe();
302 /* use the old database if present */
303 tdbfile = state_path("winbindd_idmap.tdb");
305 DEBUG(0, ("Out of memory!\n"));
306 ret = NT_STATUS_NO_MEMORY;
310 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
312 /* Open idmap repository */
313 db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
315 DEBUG(0, ("Unable to open idmap database\n"));
316 ret = NT_STATUS_UNSUCCESSFUL;
320 /* check against earlier versions */
321 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
322 if (version != IDMAP_VERSION) {
324 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
325 "possible with incomplete configuration\n",
326 version, IDMAP_VERSION));
327 ret = NT_STATUS_UNSUCCESSFUL;
330 if (db->transaction_start(db) != 0) {
331 DEBUG(0, ("Unable to start upgrade transaction!\n"));
332 ret = NT_STATUS_INTERNAL_DB_ERROR;
336 if (!idmap_tdb_upgrade(dom, db)) {
337 db->transaction_cancel(db);
338 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
339 ret = NT_STATUS_INTERNAL_DB_ERROR;
343 if (db->transaction_commit(db) != 0) {
344 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
345 ret = NT_STATUS_INTERNAL_DB_ERROR;
350 ctx->db = talloc_move(ctx, &db);
352 ret = idmap_tdb_init_hwm(dom);
355 talloc_free(mem_ctx);
359 /**********************************************************************
360 IDMAP ALLOC TDB BACKEND
361 **********************************************************************/
363 /**********************************
365 **********************************/
367 struct idmap_tdb_allocate_id_context {
374 static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
378 struct idmap_tdb_allocate_id_context *state;
381 state = (struct idmap_tdb_allocate_id_context *)private_data;
383 hwm = dbwrap_fetch_int32(db, state->hwmkey);
385 ret = NT_STATUS_INTERNAL_DB_ERROR;
389 /* check 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;
397 /* fetch a new id and increment it */
398 ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
399 if (!NT_STATUS_IS_OK(ret)) {
400 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
401 state->hwmtype, nt_errstr(ret)));
405 /* recheck it is in the range */
406 if (hwm > state->high_hwm) {
407 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
408 state->hwmtype, (unsigned long)state->high_hwm));
409 ret = NT_STATUS_UNSUCCESSFUL;
420 static NTSTATUS idmap_tdb_allocate_id(struct idmap_domain *dom,
428 struct idmap_tdb_allocate_id_context state;
429 struct idmap_tdb_context *ctx;
431 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
433 /* Get current high water mark */
447 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
448 return NT_STATUS_INVALID_PARAMETER;
451 high_hwm = dom->high_id;
454 state.high_hwm = high_hwm;
455 state.hwmtype = hwmtype;
456 state.hwmkey = hwmkey;
458 status = dbwrap_trans_do(ctx->db, idmap_tdb_allocate_id_action,
461 if (NT_STATUS_IS_OK(status)) {
463 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
465 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
472 * Allocate a new unix-ID.
473 * For now this is for the default idmap domain only.
474 * Should be extended later on.
476 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
481 if (!strequal(dom->name, "*")) {
482 DEBUG(3, ("idmap_tdb_get_new_id: "
483 "Refusing allocation of a new unixid for domain'%s'. "
484 "Currently only supported for the default "
487 return NT_STATUS_NOT_IMPLEMENTED;
490 ret = idmap_tdb_allocate_id(dom, id);
495 /**********************************************************************
496 IDMAP MAPPING TDB BACKEND
497 **********************************************************************/
499 /*****************************
500 Initialise idmap database.
501 *****************************/
503 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
504 const struct id_map *map);
506 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom)
509 struct idmap_tdb_context *ctx;
511 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
513 ctx = talloc_zero(dom, struct idmap_tdb_context);
515 DEBUG(0, ("Out of memory!\n"));
516 return NT_STATUS_NO_MEMORY;
519 /* load backend specific configuration here: */
521 if (strequal(dom->name, "*")) {
526 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
527 if (ctx->rw_ops == NULL) {
528 DEBUG(0, ("Out of memory!\n"));
529 ret = NT_STATUS_NO_MEMORY;
533 ctx->rw_ops->get_new_id = idmap_tdb_get_new_id;
534 ctx->rw_ops->set_mapping = idmap_tdb_set_mapping;
536 dom->private_data = ctx;
538 ret = idmap_tdb_open_db(dom);
539 if ( ! NT_STATUS_IS_OK(ret)) {
552 * store a mapping in the database
555 struct idmap_tdb_set_mapping_context {
560 static NTSTATUS idmap_tdb_set_mapping_action(struct db_context *db,
564 struct idmap_tdb_set_mapping_context *state;
566 state = (struct idmap_tdb_set_mapping_context *)private_data;
568 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
570 ret = dbwrap_store_bystring(db, state->ksidstr,
571 string_term_tdb_data(state->kidstr),
573 if (!NT_STATUS_IS_OK(ret)) {
574 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
575 state->ksidstr, state->kidstr, nt_errstr(ret)));
579 ret = dbwrap_store_bystring(db, state->kidstr,
580 string_term_tdb_data(state->ksidstr),
582 if (!NT_STATUS_IS_OK(ret)) {
583 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
584 state->kidstr, state->ksidstr, nt_errstr(ret)));
588 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
595 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
596 const struct id_map *map)
598 struct idmap_tdb_context *ctx;
600 char *ksidstr, *kidstr;
601 struct idmap_tdb_set_mapping_context state;
603 if (!map || !map->sid) {
604 return NT_STATUS_INVALID_PARAMETER;
607 ksidstr = kidstr = NULL;
609 /* TODO: should we filter a set_mapping using low/high filters ? */
611 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
613 switch (map->xid.type) {
616 kidstr = talloc_asprintf(ctx, "UID %lu",
617 (unsigned long)map->xid.id);
621 kidstr = talloc_asprintf(ctx, "GID %lu",
622 (unsigned long)map->xid.id);
626 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
627 return NT_STATUS_INVALID_PARAMETER;
630 if (kidstr == NULL) {
631 DEBUG(0, ("ERROR: Out of memory!\n"));
632 ret = NT_STATUS_NO_MEMORY;
636 ksidstr = sid_string_talloc(ctx, map->sid);
637 if (ksidstr == NULL) {
638 DEBUG(0, ("Out of memory!\n"));
639 ret = NT_STATUS_NO_MEMORY;
643 state.ksidstr = ksidstr;
644 state.kidstr = kidstr;
646 ret = dbwrap_trans_do(ctx->db, idmap_tdb_set_mapping_action, &state);
649 talloc_free(ksidstr);
655 * Create a new mapping for an unmapped SID, also allocating a new ID.
656 * This should be run inside a transaction.
659 * Properly integrate this with multi domain idmap config:
660 * Currently, the allocator is default-config only.
662 static NTSTATUS idmap_tdb_new_mapping(struct idmap_domain *dom, struct id_map *map)
665 struct idmap_tdb_context *ctx;
667 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
669 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
675 /**********************************
676 Single id to sid lookup function.
677 **********************************/
679 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
684 struct idmap_tdb_context *ctx;
687 return NT_STATUS_INVALID_PARAMETER;
690 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
692 /* apply filters before checking */
693 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
694 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
695 map->xid.id, dom->low_id, dom->high_id));
696 return NT_STATUS_NONE_MAPPED;
699 switch (map->xid.type) {
702 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
706 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
710 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
711 return NT_STATUS_INVALID_PARAMETER;
714 /* final SAFE_FREE safe */
717 if (keystr == NULL) {
718 DEBUG(0, ("Out of memory!\n"));
719 ret = NT_STATUS_NO_MEMORY;
723 DEBUG(10,("Fetching record %s\n", keystr));
725 /* Check if the mapping exists */
726 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
729 DEBUG(10,("Record %s not found\n", keystr));
730 ret = NT_STATUS_NONE_MAPPED;
734 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
735 DEBUG(10,("INVALID SID (%s) in record %s\n",
736 (const char *)data.dptr, keystr));
737 ret = NT_STATUS_INTERNAL_DB_ERROR;
741 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
745 talloc_free(data.dptr);
750 /**********************************
751 Single sid to id lookup function.
752 **********************************/
754 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
759 unsigned long rec_id = 0;
760 struct idmap_tdb_context *ctx;
761 TALLOC_CTX *tmp_ctx = talloc_stackframe();
763 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
765 keystr = sid_string_talloc(tmp_ctx, map->sid);
766 if (keystr == NULL) {
767 DEBUG(0, ("Out of memory!\n"));
768 ret = NT_STATUS_NO_MEMORY;
772 DEBUG(10,("Fetching record %s\n", keystr));
774 /* Check if sid is present in database */
775 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
777 DEBUG(10,("Record %s not found\n", keystr));
778 ret = NT_STATUS_NONE_MAPPED;
782 /* What type of record is this ? */
783 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
784 map->xid.id = rec_id;
785 map->xid.type = ID_TYPE_UID;
786 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
789 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
790 map->xid.id = rec_id;
791 map->xid.type = ID_TYPE_GID;
792 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
795 } else { /* Unknown record type ! */
796 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
797 ret = NT_STATUS_INTERNAL_DB_ERROR;
801 /* apply filters before returning result */
802 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
803 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
804 map->xid.id, dom->low_id, dom->high_id));
805 ret = NT_STATUS_NONE_MAPPED;
809 talloc_free(tmp_ctx);
813 /**********************************
814 lookup a set of unix ids.
815 **********************************/
817 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
822 /* initialize the status to avoid suprise */
823 for (i = 0; ids[i]; i++) {
824 ids[i]->status = ID_UNKNOWN;
827 for (i = 0; ids[i]; i++) {
828 ret = idmap_tdb_id_to_sid(dom, ids[i]);
829 if ( ! NT_STATUS_IS_OK(ret)) {
831 /* if it is just a failed mapping continue */
832 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
834 /* make sure it is marked as unmapped */
835 ids[i]->status = ID_UNMAPPED;
839 /* some fatal error occurred, return immediately */
843 /* all ok, id is mapped */
844 ids[i]->status = ID_MAPPED;
853 /**********************************
854 lookup a set of sids.
855 **********************************/
857 struct idmap_tdb_sids_to_unixids_context {
858 struct idmap_domain *dom;
860 bool allocate_unmapped;
863 static NTSTATUS idmap_tdb_sids_to_unixids_action(struct db_context *db,
866 struct idmap_tdb_sids_to_unixids_context *state;
868 NTSTATUS ret = NT_STATUS_OK;
870 state = (struct idmap_tdb_sids_to_unixids_context *)private_data;
872 DEBUG(10, ("idmap_tdb_sids_to_unixids_action: "
873 " domain: [%s], allocate: %s\n",
875 state->allocate_unmapped ? "yes" : "no"));
877 for (i = 0; state->ids[i]; i++) {
878 if ((state->ids[i]->status == ID_UNKNOWN) ||
879 /* retry if we could not map in previous run: */
880 (state->ids[i]->status == ID_UNMAPPED))
884 ret2 = idmap_tdb_sid_to_id(state->dom, state->ids[i]);
885 if (!NT_STATUS_IS_OK(ret2)) {
887 /* if it is just a failed mapping, continue */
888 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
890 /* make sure it is marked as unmapped */
891 state->ids[i]->status = ID_UNMAPPED;
892 ret = STATUS_SOME_UNMAPPED;
894 /* some fatal error occurred, return immediately */
899 /* all ok, id is mapped */
900 state->ids[i]->status = ID_MAPPED;
904 if ((state->ids[i]->status == ID_UNMAPPED) &&
905 state->allocate_unmapped)
907 ret = idmap_tdb_new_mapping(state->dom, state->ids[i]);
908 if (!NT_STATUS_IS_OK(ret)) {
918 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
920 struct idmap_tdb_context *ctx;
923 struct idmap_tdb_sids_to_unixids_context state;
925 /* initialize the status to avoid suprise */
926 for (i = 0; ids[i]; i++) {
927 ids[i]->status = ID_UNKNOWN;
930 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
934 state.allocate_unmapped = false;
936 ret = idmap_tdb_sids_to_unixids_action(ctx->db, &state);
938 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
939 state.allocate_unmapped = true;
940 ret = dbwrap_trans_do(ctx->db,
941 idmap_tdb_sids_to_unixids_action,
949 /**********************************
950 Close the idmap tdb instance
951 **********************************/
953 static struct idmap_methods db_methods = {
954 .init = idmap_tdb_db_init,
955 .unixids_to_sids = idmap_tdb_unixids_to_sids,
956 .sids_to_unixids = idmap_tdb_sids_to_unixids,
957 .allocate_id = idmap_tdb_get_new_id,
960 NTSTATUS idmap_tdb_init(void)
962 DEBUG(10, ("calling idmap_tdb_init\n"));
964 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);