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/>.
31 #include "../libcli/security/security.h"
34 #define DBGC_CLASS DBGC_IDMAP
36 /* idmap version determines auto-conversion - this is the database
37 structure version specifier. */
39 #define IDMAP_VERSION 2
41 struct idmap_tdb_context {
42 struct db_context *db;
43 struct idmap_rw_ops *rw_ops;
46 /* High water mark keys */
47 #define HWM_GROUP "GROUP HWM"
48 #define HWM_USER "USER HWM"
50 struct convert_fn_state {
51 struct db_context *db;
55 /*****************************************************************************
56 For idmap conversion: convert one record to new format
57 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
59 *****************************************************************************/
60 static int convert_fn(struct db_record *rec, void *private_data)
62 struct winbindd_domain *domain;
70 struct convert_fn_state *s = (struct convert_fn_state *)private_data;
72 DEBUG(10,("Converting %s\n", (const char *)rec->key.dptr));
74 p = strchr((const char *)rec->key.dptr, '/');
79 fstrcpy(dom_name, (const char *)rec->key.dptr);
82 domain = find_domain_from_name(dom_name);
84 /* We must delete the old record. */
85 DEBUG(0,("Unable to find domain %s\n", dom_name ));
86 DEBUG(0,("deleting record %s\n", (const char *)rec->key.dptr ));
88 status = rec->delete_rec(rec);
89 if (!NT_STATUS_IS_OK(status)) {
90 DEBUG(0, ("Unable to delete record %s:%s\n",
91 (const char *)rec->key.dptr,
102 sid_compose(&sid, &domain->sid, rid);
104 sid_to_fstring(keystr, &sid);
105 key2 = string_term_tdb_data(keystr);
107 status = dbwrap_store(s->db, key2, rec->value, TDB_INSERT);
108 if (!NT_STATUS_IS_OK(status)) {
109 DEBUG(0,("Unable to add record %s:%s\n",
110 (const char *)key2.dptr,
116 status = dbwrap_store(s->db, rec->value, key2, TDB_REPLACE);
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(0,("Unable to update record %s:%s\n",
119 (const char *)rec->value.dptr,
125 status = rec->delete_rec(rec);
126 if (!NT_STATUS_IS_OK(status)) {
127 DEBUG(0,("Unable to delete record %s:%s\n",
128 (const char *)rec->key.dptr,
137 /*****************************************************************************
138 Convert the idmap database from an older version.
139 *****************************************************************************/
141 static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db)
144 bool bigendianheader;
145 struct convert_fn_state s;
147 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
149 bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False;
151 vers = dbwrap_fetch_int32(db, "IDMAP_VERSION");
153 if (((vers == -1) && bigendianheader) || (IREV(vers) == IDMAP_VERSION)) {
154 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
156 * high and low records were created on a
157 * big endian machine and will need byte-reversing.
162 wm = dbwrap_fetch_int32(db, HWM_USER);
170 if (dbwrap_store_int32(db, HWM_USER, wm) == -1) {
171 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
175 wm = dbwrap_fetch_int32(db, HWM_GROUP);
182 if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) {
183 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
191 /* the old format stored as DOMAIN/rid - now we store the SID direct */
192 db->traverse(db, convert_fn, &s);
195 DEBUG(0, ("Problem during conversion\n"));
199 if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
200 DEBUG(0, ("Unable to store idmap version in database\n"));
207 static NTSTATUS idmap_tdb_init_hwm(struct idmap_domain *dom)
212 bool update_uid = false;
213 bool update_gid = false;
214 struct idmap_tdb_context *ctx;
216 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
218 low_uid = dbwrap_fetch_int32(ctx->db, HWM_USER);
219 if (low_uid == -1 || low_uid < dom->low_id) {
223 low_gid = dbwrap_fetch_int32(ctx->db, HWM_GROUP);
224 if (low_gid == -1 || low_gid < dom->low_id) {
228 if (!update_uid && !update_gid) {
232 if (ctx->db->transaction_start(ctx->db) != 0) {
233 DEBUG(0, ("Unable to start upgrade transaction!\n"));
234 return NT_STATUS_INTERNAL_DB_ERROR;
238 ret = dbwrap_store_int32(ctx->db, HWM_USER, dom->low_id);
240 ctx->db->transaction_cancel(ctx->db);
241 DEBUG(0, ("Unable to initialise user hwm in idmap "
243 return NT_STATUS_INTERNAL_DB_ERROR;
248 ret = dbwrap_store_int32(ctx->db, HWM_GROUP, dom->low_id);
250 ctx->db->transaction_cancel(ctx->db);
251 DEBUG(0, ("Unable to initialise group hwm in idmap "
253 return NT_STATUS_INTERNAL_DB_ERROR;
257 if (ctx->db->transaction_commit(ctx->db) != 0) {
258 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
259 return NT_STATUS_INTERNAL_DB_ERROR;
265 static NTSTATUS idmap_tdb_open_db(struct idmap_domain *dom)
269 char *tdbfile = NULL;
270 struct db_context *db = NULL;
272 bool config_error = false;
273 struct idmap_tdb_context *ctx;
275 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
278 /* it is already open */
282 /* use our own context here */
283 mem_ctx = talloc_stackframe();
285 /* use the old database if present */
286 tdbfile = state_path("winbindd_idmap.tdb");
288 DEBUG(0, ("Out of memory!\n"));
289 ret = NT_STATUS_NO_MEMORY;
293 DEBUG(10,("Opening tdbfile %s\n", tdbfile ));
295 /* Open idmap repository */
296 db = db_open(mem_ctx, tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
298 DEBUG(0, ("Unable to open idmap database\n"));
299 ret = NT_STATUS_UNSUCCESSFUL;
303 /* check against earlier versions */
304 version = dbwrap_fetch_int32(db, "IDMAP_VERSION");
305 if (version != IDMAP_VERSION) {
307 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
308 "possible with incomplete configuration\n",
309 version, IDMAP_VERSION));
310 ret = NT_STATUS_UNSUCCESSFUL;
313 if (db->transaction_start(db) != 0) {
314 DEBUG(0, ("Unable to start upgrade transaction!\n"));
315 ret = NT_STATUS_INTERNAL_DB_ERROR;
319 if (!idmap_tdb_upgrade(dom, db)) {
320 db->transaction_cancel(db);
321 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
322 ret = NT_STATUS_INTERNAL_DB_ERROR;
326 if (db->transaction_commit(db) != 0) {
327 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
328 ret = NT_STATUS_INTERNAL_DB_ERROR;
333 ctx->db = talloc_move(ctx, &db);
335 ret = idmap_tdb_init_hwm(dom);
338 talloc_free(mem_ctx);
342 /**********************************************************************
343 IDMAP ALLOC TDB BACKEND
344 **********************************************************************/
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 idmap_domain *dom,
411 struct idmap_tdb_allocate_id_context state;
412 struct idmap_tdb_context *ctx;
414 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
416 /* Get current high water mark */
430 DEBUG(2, ("Invalid ID type (0x%x)\n", xid->type));
431 return NT_STATUS_INVALID_PARAMETER;
434 high_hwm = dom->high_id;
437 state.high_hwm = high_hwm;
438 state.hwmtype = hwmtype;
439 state.hwmkey = hwmkey;
441 status = dbwrap_trans_do(ctx->db, idmap_tdb_allocate_id_action,
444 if (NT_STATUS_IS_OK(status)) {
446 DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
448 DEBUG(1, ("Error allocating a new %s\n", hwmtype));
455 * Allocate a new unix-ID.
456 * For now this is for the default idmap domain only.
457 * Should be extended later on.
459 static NTSTATUS idmap_tdb_get_new_id(struct idmap_domain *dom,
464 if (!strequal(dom->name, "*")) {
465 DEBUG(3, ("idmap_tdb_get_new_id: "
466 "Refusing allocation of a new unixid for domain'%s'. "
467 "Currently only supported for the default "
470 return NT_STATUS_NOT_IMPLEMENTED;
473 ret = idmap_tdb_allocate_id(dom, id);
478 /**********************************************************************
479 IDMAP MAPPING TDB BACKEND
480 **********************************************************************/
482 /*****************************
483 Initialise idmap database.
484 *****************************/
486 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
487 const struct id_map *map);
489 static NTSTATUS idmap_tdb_db_init(struct idmap_domain *dom, const char *params)
492 struct idmap_tdb_context *ctx;
494 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
496 ctx = talloc_zero(dom, struct idmap_tdb_context);
498 DEBUG(0, ("Out of memory!\n"));
499 return NT_STATUS_NO_MEMORY;
502 /* load backend specific configuration here: */
504 if (strequal(dom->name, "*")) {
509 ctx->rw_ops = talloc_zero(ctx, struct idmap_rw_ops);
510 if (ctx->rw_ops == NULL) {
511 DEBUG(0, ("Out of memory!\n"));
512 ret = NT_STATUS_NO_MEMORY;
516 ctx->rw_ops->get_new_id = idmap_tdb_get_new_id;
517 ctx->rw_ops->set_mapping = idmap_tdb_set_mapping;
519 dom->private_data = ctx;
521 ret = idmap_tdb_open_db(dom);
522 if ( ! NT_STATUS_IS_OK(ret)) {
535 * store a mapping in the database
538 struct idmap_tdb_set_mapping_context {
543 static NTSTATUS idmap_tdb_set_mapping_action(struct db_context *db,
547 struct idmap_tdb_set_mapping_context *state;
549 state = (struct idmap_tdb_set_mapping_context *)private_data;
551 DEBUG(10, ("Storing %s <-> %s map\n", state->ksidstr, state->kidstr));
553 ret = dbwrap_store_bystring(db, state->ksidstr,
554 string_term_tdb_data(state->kidstr),
556 if (!NT_STATUS_IS_OK(ret)) {
557 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
558 state->ksidstr, state->kidstr, nt_errstr(ret)));
562 ret = dbwrap_store_bystring(db, state->kidstr,
563 string_term_tdb_data(state->ksidstr),
565 if (!NT_STATUS_IS_OK(ret)) {
566 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
567 state->kidstr, state->ksidstr, nt_errstr(ret)));
571 DEBUG(10,("Stored %s <-> %s\n", state->ksidstr, state->kidstr));
578 static NTSTATUS idmap_tdb_set_mapping(struct idmap_domain *dom,
579 const struct id_map *map)
581 struct idmap_tdb_context *ctx;
583 char *ksidstr, *kidstr;
584 struct idmap_tdb_set_mapping_context state;
586 if (!map || !map->sid) {
587 return NT_STATUS_INVALID_PARAMETER;
590 ksidstr = kidstr = NULL;
592 /* TODO: should we filter a set_mapping using low/high filters ? */
594 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
596 switch (map->xid.type) {
599 kidstr = talloc_asprintf(ctx, "UID %lu",
600 (unsigned long)map->xid.id);
604 kidstr = talloc_asprintf(ctx, "GID %lu",
605 (unsigned long)map->xid.id);
609 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
610 return NT_STATUS_INVALID_PARAMETER;
613 if (kidstr == NULL) {
614 DEBUG(0, ("ERROR: Out of memory!\n"));
615 ret = NT_STATUS_NO_MEMORY;
619 ksidstr = sid_string_talloc(ctx, map->sid);
620 if (ksidstr == NULL) {
621 DEBUG(0, ("Out of memory!\n"));
622 ret = NT_STATUS_NO_MEMORY;
626 state.ksidstr = ksidstr;
627 state.kidstr = kidstr;
629 ret = dbwrap_trans_do(ctx->db, idmap_tdb_set_mapping_action, &state);
632 talloc_free(ksidstr);
638 * Create a new mapping for an unmapped SID, also allocating a new ID.
639 * This should be run inside a transaction.
642 * Properly integrate this with multi domain idmap config:
643 * Currently, the allocator is default-config only.
645 static NTSTATUS idmap_tdb_new_mapping(struct idmap_domain *dom, struct id_map *map)
648 struct idmap_tdb_context *ctx;
650 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
652 ret = idmap_rw_new_mapping(dom, ctx->rw_ops, map);
658 /**********************************
659 Single id to sid lookup function.
660 **********************************/
662 static NTSTATUS idmap_tdb_id_to_sid(struct idmap_domain *dom, struct id_map *map)
667 struct idmap_tdb_context *ctx;
670 return NT_STATUS_INVALID_PARAMETER;
673 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
675 /* apply filters before checking */
676 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
677 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
678 map->xid.id, dom->low_id, dom->high_id));
679 return NT_STATUS_NONE_MAPPED;
682 switch (map->xid.type) {
685 keystr = talloc_asprintf(ctx, "UID %lu", (unsigned long)map->xid.id);
689 keystr = talloc_asprintf(ctx, "GID %lu", (unsigned long)map->xid.id);
693 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
694 return NT_STATUS_INVALID_PARAMETER;
697 /* final SAFE_FREE safe */
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 the mapping exists */
709 data = dbwrap_fetch_bystring(ctx->db, NULL, keystr);
712 DEBUG(10,("Record %s not found\n", keystr));
713 ret = NT_STATUS_NONE_MAPPED;
717 if (!string_to_sid(map->sid, (const char *)data.dptr)) {
718 DEBUG(10,("INVALID SID (%s) in record %s\n",
719 (const char *)data.dptr, keystr));
720 ret = NT_STATUS_INTERNAL_DB_ERROR;
724 DEBUG(10,("Found record %s -> %s\n", keystr, (const char *)data.dptr));
728 talloc_free(data.dptr);
733 /**********************************
734 Single sid to id lookup function.
735 **********************************/
737 static NTSTATUS idmap_tdb_sid_to_id(struct idmap_domain *dom, struct id_map *map)
742 unsigned long rec_id = 0;
743 struct idmap_tdb_context *ctx;
744 TALLOC_CTX *tmp_ctx = talloc_stackframe();
746 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
748 keystr = sid_string_talloc(tmp_ctx, map->sid);
749 if (keystr == NULL) {
750 DEBUG(0, ("Out of memory!\n"));
751 ret = NT_STATUS_NO_MEMORY;
755 DEBUG(10,("Fetching record %s\n", keystr));
757 /* Check if sid is present in database */
758 data = dbwrap_fetch_bystring(ctx->db, tmp_ctx, keystr);
760 DEBUG(10,("Record %s not found\n", keystr));
761 ret = NT_STATUS_NONE_MAPPED;
765 /* What type of record is this ? */
766 if (sscanf((const char *)data.dptr, "UID %lu", &rec_id) == 1) { /* Try a UID record. */
767 map->xid.id = rec_id;
768 map->xid.type = ID_TYPE_UID;
769 DEBUG(10,("Found uid record %s -> %s \n", keystr, (const char *)data.dptr ));
772 } else if (sscanf((const char *)data.dptr, "GID %lu", &rec_id) == 1) { /* Try a GID record. */
773 map->xid.id = rec_id;
774 map->xid.type = ID_TYPE_GID;
775 DEBUG(10,("Found gid record %s -> %s \n", keystr, (const char *)data.dptr ));
778 } else { /* Unknown record type ! */
779 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
780 ret = NT_STATUS_INTERNAL_DB_ERROR;
784 /* apply filters before returning result */
785 if (!idmap_unix_id_is_in_range(map->xid.id, dom)) {
786 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
787 map->xid.id, dom->low_id, dom->high_id));
788 ret = NT_STATUS_NONE_MAPPED;
792 talloc_free(tmp_ctx);
796 /**********************************
797 lookup a set of unix ids.
798 **********************************/
800 static NTSTATUS idmap_tdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
802 struct idmap_tdb_context *ctx;
806 /* initialize the status to avoid suprise */
807 for (i = 0; ids[i]; i++) {
808 ids[i]->status = ID_UNKNOWN;
811 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
813 for (i = 0; ids[i]; i++) {
814 ret = idmap_tdb_id_to_sid(dom, ids[i]);
815 if ( ! NT_STATUS_IS_OK(ret)) {
817 /* if it is just a failed mapping continue */
818 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
820 /* make sure it is marked as unmapped */
821 ids[i]->status = ID_UNMAPPED;
825 /* some fatal error occurred, return immediately */
829 /* all ok, id is mapped */
830 ids[i]->status = ID_MAPPED;
839 /**********************************
840 lookup a set of sids.
841 **********************************/
843 struct idmap_tdb_sids_to_unixids_context {
844 struct idmap_domain *dom;
846 bool allocate_unmapped;
849 static NTSTATUS idmap_tdb_sids_to_unixids_action(struct db_context *db,
852 struct idmap_tdb_sids_to_unixids_context *state;
854 NTSTATUS ret = NT_STATUS_OK;
856 state = (struct idmap_tdb_sids_to_unixids_context *)private_data;
858 DEBUG(10, ("idmap_tdb_sids_to_unixids_action: "
859 " domain: [%s], allocate: %s\n",
861 state->allocate_unmapped ? "yes" : "no"));
863 for (i = 0; state->ids[i]; i++) {
864 if ((state->ids[i]->status == ID_UNKNOWN) ||
865 /* retry if we could not map in previous run: */
866 (state->ids[i]->status == ID_UNMAPPED))
870 ret2 = idmap_tdb_sid_to_id(state->dom, state->ids[i]);
871 if (!NT_STATUS_IS_OK(ret2)) {
873 /* if it is just a failed mapping, continue */
874 if (NT_STATUS_EQUAL(ret2, NT_STATUS_NONE_MAPPED)) {
876 /* make sure it is marked as unmapped */
877 state->ids[i]->status = ID_UNMAPPED;
878 ret = STATUS_SOME_UNMAPPED;
880 /* some fatal error occurred, return immediately */
885 /* all ok, id is mapped */
886 state->ids[i]->status = ID_MAPPED;
890 if ((state->ids[i]->status == ID_UNMAPPED) &&
891 state->allocate_unmapped)
893 ret = idmap_tdb_new_mapping(state->dom, state->ids[i]);
894 if (!NT_STATUS_IS_OK(ret)) {
904 static NTSTATUS idmap_tdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
906 struct idmap_tdb_context *ctx;
909 struct idmap_tdb_sids_to_unixids_context state;
911 /* initialize the status to avoid suprise */
912 for (i = 0; ids[i]; i++) {
913 ids[i]->status = ID_UNKNOWN;
916 ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
920 state.allocate_unmapped = false;
922 ret = idmap_tdb_sids_to_unixids_action(ctx->db, &state);
924 if (NT_STATUS_EQUAL(ret, STATUS_SOME_UNMAPPED) && !dom->read_only) {
925 state.allocate_unmapped = true;
926 ret = dbwrap_trans_do(ctx->db,
927 idmap_tdb_sids_to_unixids_action,
935 /**********************************
936 Close the idmap tdb instance
937 **********************************/
939 static struct idmap_methods db_methods = {
940 .init = idmap_tdb_db_init,
941 .unixids_to_sids = idmap_tdb_unixids_to_sids,
942 .sids_to_unixids = idmap_tdb_sids_to_unixids,
943 .allocate_id = idmap_tdb_get_new_id,
946 NTSTATUS idmap_tdb_init(void)
948 DEBUG(10, ("calling idmap_tdb_init\n"));
950 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "tdb", &db_methods);