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_init_hwm(struct idmap_domain
*dom
)
214 bool update_uid
= false;
215 bool update_gid
= false;
216 struct idmap_tdb_context
*ctx
;
218 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
220 low_uid
= dbwrap_fetch_int32(ctx
->db
, HWM_USER
);
221 if (low_uid
== -1 || low_uid
< dom
->low_id
) {
225 low_gid
= dbwrap_fetch_int32(ctx
->db
, HWM_GROUP
);
226 if (low_gid
== -1 || low_gid
< dom
->low_id
) {
230 if (!update_uid
&& !update_gid
) {
234 if (ctx
->db
->transaction_start(ctx
->db
) != 0) {
235 DEBUG(0, ("Unable to start upgrade transaction!\n"));
236 return NT_STATUS_INTERNAL_DB_ERROR
;
240 ret
= dbwrap_store_int32(ctx
->db
, HWM_USER
, dom
->low_id
);
242 ctx
->db
->transaction_cancel(ctx
->db
);
243 DEBUG(0, ("Unable to initialise user hwm in idmap "
245 return NT_STATUS_INTERNAL_DB_ERROR
;
250 ret
= dbwrap_store_int32(ctx
->db
, HWM_GROUP
, dom
->low_id
);
252 ctx
->db
->transaction_cancel(ctx
->db
);
253 DEBUG(0, ("Unable to initialise group hwm in idmap "
255 return NT_STATUS_INTERNAL_DB_ERROR
;
259 if (ctx
->db
->transaction_commit(ctx
->db
) != 0) {
260 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
261 return NT_STATUS_INTERNAL_DB_ERROR
;
267 static NTSTATUS
idmap_tdb_open_db(struct idmap_domain
*dom
)
271 char *tdbfile
= NULL
;
272 struct db_context
*db
= NULL
;
274 bool config_error
= false;
275 struct idmap_tdb_context
*ctx
;
277 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
279 /* use our own context here */
280 mem_ctx
= talloc_stackframe();
282 /* use the old database if present */
283 tdbfile
= state_path("winbindd_idmap.tdb");
285 DEBUG(0, ("Out of memory!\n"));
286 ret
= NT_STATUS_NO_MEMORY
;
290 DEBUG(10,("Opening tdbfile %s\n", tdbfile
));
292 /* Open idmap repository */
293 db
= db_open(mem_ctx
, tdbfile
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644);
295 DEBUG(0, ("Unable to open idmap database\n"));
296 ret
= NT_STATUS_UNSUCCESSFUL
;
300 /* check against earlier versions */
301 version
= dbwrap_fetch_int32(db
, "IDMAP_VERSION");
302 if (version
!= IDMAP_VERSION
) {
304 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
305 "possible with incomplete configuration\n",
306 version
, IDMAP_VERSION
));
307 ret
= NT_STATUS_UNSUCCESSFUL
;
310 if (db
->transaction_start(db
) != 0) {
311 DEBUG(0, ("Unable to start upgrade transaction!\n"));
312 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
316 if (!idmap_tdb_upgrade(dom
, db
)) {
317 db
->transaction_cancel(db
);
318 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
319 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
323 if (db
->transaction_commit(db
) != 0) {
324 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
325 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
330 ctx
->db
= talloc_move(ctx
, &db
);
332 ret
= idmap_tdb_init_hwm(dom
);
335 talloc_free(mem_ctx
);
339 /**********************************************************************
340 IDMAP ALLOC TDB BACKEND
341 **********************************************************************/
343 /**********************************
345 **********************************/
347 struct idmap_tdb_allocate_id_context
{
354 static NTSTATUS
idmap_tdb_allocate_id_action(struct db_context
*db
,
358 struct idmap_tdb_allocate_id_context
*state
;
361 state
= (struct idmap_tdb_allocate_id_context
*)private_data
;
363 hwm
= dbwrap_fetch_int32(db
, state
->hwmkey
);
365 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
369 /* check it is in the range */
370 if (hwm
> state
->high_hwm
) {
371 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
372 state
->hwmtype
, (unsigned long)state
->high_hwm
));
373 ret
= NT_STATUS_UNSUCCESSFUL
;
377 /* fetch a new id and increment it */
378 ret
= dbwrap_trans_change_uint32_atomic(db
, state
->hwmkey
, &hwm
, 1);
379 if (!NT_STATUS_IS_OK(ret
)) {
380 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
381 state
->hwmtype
, nt_errstr(ret
)));
385 /* recheck it is in the range */
386 if (hwm
> state
->high_hwm
) {
387 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
388 state
->hwmtype
, (unsigned long)state
->high_hwm
));
389 ret
= NT_STATUS_UNSUCCESSFUL
;
400 static NTSTATUS
idmap_tdb_allocate_id(struct idmap_domain
*dom
,
408 struct idmap_tdb_allocate_id_context state
;
409 struct idmap_tdb_context
*ctx
;
411 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
413 /* Get current high water mark */
427 DEBUG(2, ("Invalid ID type (0x%x)\n", xid
->type
));
428 return NT_STATUS_INVALID_PARAMETER
;
431 high_hwm
= dom
->high_id
;
434 state
.high_hwm
= high_hwm
;
435 state
.hwmtype
= hwmtype
;
436 state
.hwmkey
= hwmkey
;
438 status
= dbwrap_trans_do(ctx
->db
, idmap_tdb_allocate_id_action
,
441 if (NT_STATUS_IS_OK(status
)) {
443 DEBUG(10,("New %s = %d\n", hwmtype
, state
.hwm
));
445 DEBUG(1, ("Error allocating a new %s\n", hwmtype
));
452 * Allocate a new unix-ID.
453 * For now this is for the default idmap domain only.
454 * Should be extended later on.
456 static NTSTATUS
idmap_tdb_get_new_id(struct idmap_domain
*dom
,
461 if (!strequal(dom
->name
, "*")) {
462 DEBUG(3, ("idmap_tdb_get_new_id: "
463 "Refusing allocation of a new unixid for domain'%s'. "
464 "Currently only supported for the default "
467 return NT_STATUS_NOT_IMPLEMENTED
;
470 ret
= idmap_tdb_allocate_id(dom
, id
);
475 /**********************************************************************
476 IDMAP MAPPING TDB BACKEND
477 **********************************************************************/
479 /*****************************
480 Initialise idmap database.
481 *****************************/
483 static NTSTATUS
idmap_tdb_db_init(struct idmap_domain
*dom
, const char *params
)
486 struct idmap_tdb_context
*ctx
;
488 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom
->name
));
490 ctx
= talloc(dom
, struct idmap_tdb_context
);
492 DEBUG(0, ("Out of memory!\n"));
493 return NT_STATUS_NO_MEMORY
;
496 /* load backend specific configuration here: */
498 if (strequal(dom
->name
, "*")) {
503 dom
->private_data
= ctx
;
505 ret
= idmap_tdb_open_db(dom
);
506 if ( ! NT_STATUS_IS_OK(ret
)) {
517 /**********************************
518 Single id to sid lookup function.
519 **********************************/
521 static NTSTATUS
idmap_tdb_id_to_sid(struct idmap_domain
*dom
, struct id_map
*map
)
526 struct idmap_tdb_context
*ctx
;
529 return NT_STATUS_INVALID_PARAMETER
;
532 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
534 /* apply filters before checking */
535 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
536 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
537 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
538 return NT_STATUS_NONE_MAPPED
;
541 switch (map
->xid
.type
) {
544 keystr
= talloc_asprintf(ctx
, "UID %lu", (unsigned long)map
->xid
.id
);
548 keystr
= talloc_asprintf(ctx
, "GID %lu", (unsigned long)map
->xid
.id
);
552 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
553 return NT_STATUS_INVALID_PARAMETER
;
556 /* final SAFE_FREE safe */
559 if (keystr
== NULL
) {
560 DEBUG(0, ("Out of memory!\n"));
561 ret
= NT_STATUS_NO_MEMORY
;
565 DEBUG(10,("Fetching record %s\n", keystr
));
567 /* Check if the mapping exists */
568 data
= dbwrap_fetch_bystring(ctx
->db
, NULL
, keystr
);
571 DEBUG(10,("Record %s not found\n", keystr
));
572 ret
= NT_STATUS_NONE_MAPPED
;
576 if (!string_to_sid(map
->sid
, (const char *)data
.dptr
)) {
577 DEBUG(10,("INVALID SID (%s) in record %s\n",
578 (const char *)data
.dptr
, keystr
));
579 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
583 DEBUG(10,("Found record %s -> %s\n", keystr
, (const char *)data
.dptr
));
587 talloc_free(data
.dptr
);
592 /**********************************
593 Single sid to id lookup function.
594 **********************************/
596 static NTSTATUS
idmap_tdb_sid_to_id(struct idmap_domain
*dom
, struct id_map
*map
)
601 unsigned long rec_id
= 0;
602 struct idmap_tdb_context
*ctx
;
603 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
605 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
607 keystr
= sid_string_talloc(tmp_ctx
, map
->sid
);
608 if (keystr
== NULL
) {
609 DEBUG(0, ("Out of memory!\n"));
610 ret
= NT_STATUS_NO_MEMORY
;
614 DEBUG(10,("Fetching record %s\n", keystr
));
616 /* Check if sid is present in database */
617 data
= dbwrap_fetch_bystring(ctx
->db
, tmp_ctx
, keystr
);
619 DEBUG(10,("Record %s not found\n", keystr
));
620 ret
= NT_STATUS_NONE_MAPPED
;
624 /* What type of record is this ? */
625 if (sscanf((const char *)data
.dptr
, "UID %lu", &rec_id
) == 1) { /* Try a UID record. */
626 map
->xid
.id
= rec_id
;
627 map
->xid
.type
= ID_TYPE_UID
;
628 DEBUG(10,("Found uid record %s -> %s \n", keystr
, (const char *)data
.dptr
));
631 } else if (sscanf((const char *)data
.dptr
, "GID %lu", &rec_id
) == 1) { /* Try a GID record. */
632 map
->xid
.id
= rec_id
;
633 map
->xid
.type
= ID_TYPE_GID
;
634 DEBUG(10,("Found gid record %s -> %s \n", keystr
, (const char *)data
.dptr
));
637 } else { /* Unknown record type ! */
638 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr
, (const char *)data
.dptr
));
639 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
643 /* apply filters before returning result */
644 if (!idmap_unix_id_is_in_range(map
->xid
.id
, dom
)) {
645 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
646 map
->xid
.id
, dom
->low_id
, dom
->high_id
));
647 ret
= NT_STATUS_NONE_MAPPED
;
651 talloc_free(tmp_ctx
);
655 /**********************************
656 lookup a set of unix ids.
657 **********************************/
659 static NTSTATUS
idmap_tdb_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
661 struct idmap_tdb_context
*ctx
;
665 /* initialize the status to avoid suprise */
666 for (i
= 0; ids
[i
]; i
++) {
667 ids
[i
]->status
= ID_UNKNOWN
;
670 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
672 for (i
= 0; ids
[i
]; i
++) {
673 ret
= idmap_tdb_id_to_sid(dom
, ids
[i
]);
674 if ( ! NT_STATUS_IS_OK(ret
)) {
676 /* if it is just a failed mapping continue */
677 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
679 /* make sure it is marked as unmapped */
680 ids
[i
]->status
= ID_UNMAPPED
;
684 /* some fatal error occurred, return immediately */
688 /* all ok, id is mapped */
689 ids
[i
]->status
= ID_MAPPED
;
698 /**********************************
699 lookup a set of sids.
700 **********************************/
702 static NTSTATUS
idmap_tdb_sids_to_unixids(struct idmap_domain
*dom
, struct id_map
**ids
)
704 struct idmap_tdb_context
*ctx
;
708 /* initialize the status to avoid suprise */
709 for (i
= 0; ids
[i
]; i
++) {
710 ids
[i
]->status
= ID_UNKNOWN
;
713 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
715 for (i
= 0; ids
[i
]; i
++) {
716 ret
= idmap_tdb_sid_to_id(dom
, ids
[i
]);
717 if ( ! NT_STATUS_IS_OK(ret
)) {
719 /* if it is just a failed mapping continue */
720 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
722 /* make sure it is marked as unmapped */
723 ids
[i
]->status
= ID_UNMAPPED
;
727 /* some fatal error occurred, return immediately */
731 /* all ok, id is mapped */
732 ids
[i
]->status
= ID_MAPPED
;
741 /**********************************
743 **********************************/
745 static NTSTATUS
idmap_tdb_set_mapping(struct idmap_domain
*dom
,
746 const struct id_map
*map
)
748 struct idmap_tdb_context
*ctx
;
751 char *ksidstr
, *kidstr
;
754 if (!map
|| !map
->sid
) {
755 return NT_STATUS_INVALID_PARAMETER
;
758 ksidstr
= kidstr
= NULL
;
760 /* TODO: should we filter a set_mapping using low/high filters ? */
762 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
764 switch (map
->xid
.type
) {
767 kidstr
= talloc_asprintf(ctx
, "UID %lu",
768 (unsigned long)map
->xid
.id
);
772 kidstr
= talloc_asprintf(ctx
, "GID %lu",
773 (unsigned long)map
->xid
.id
);
777 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
778 return NT_STATUS_INVALID_PARAMETER
;
781 if (kidstr
== NULL
) {
782 DEBUG(0, ("ERROR: Out of memory!\n"));
783 ret
= NT_STATUS_NO_MEMORY
;
787 if ((ksidstr
= talloc_asprintf(
788 ctx
, "%s", sid_to_fstring(tmp
, map
->sid
))) == NULL
) {
789 DEBUG(0, ("Out of memory!\n"));
790 ret
= NT_STATUS_NO_MEMORY
;
794 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr
, kidstr
));
795 kid
= string_term_tdb_data(kidstr
);
796 ksid
= string_term_tdb_data(ksidstr
);
798 if (ctx
->db
->transaction_start(ctx
->db
) != 0) {
799 DEBUG(0, ("Failed to start transaction for %s\n",
801 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
805 ret
= dbwrap_store(ctx
->db
, ksid
, kid
, TDB_REPLACE
);
806 if (!NT_STATUS_IS_OK(ret
)) {
807 ctx
->db
->transaction_cancel(ctx
->db
);
808 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
809 ksidstr
, kidstr
, nt_errstr(ret
)));
812 ret
= dbwrap_store(ctx
->db
, kid
, ksid
, TDB_REPLACE
);
813 if (!NT_STATUS_IS_OK(ret
)) {
814 ctx
->db
->transaction_cancel(ctx
->db
);
815 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
816 kidstr
, ksidstr
, nt_errstr(ret
)));
820 if (ctx
->db
->transaction_commit(ctx
->db
) != 0) {
821 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
823 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
827 DEBUG(10,("Stored %s <-> %s\n", ksidstr
, kidstr
));
831 talloc_free(ksidstr
);
836 /**********************************
837 Close the idmap tdb instance
838 **********************************/
840 static NTSTATUS
idmap_tdb_close(struct idmap_domain
*dom
)
842 struct idmap_tdb_context
*ctx
;
844 if (dom
->private_data
) {
845 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
847 TALLOC_FREE(ctx
->db
);
852 static struct idmap_methods db_methods
= {
853 .init
= idmap_tdb_db_init
,
854 .unixids_to_sids
= idmap_tdb_unixids_to_sids
,
855 .sids_to_unixids
= idmap_tdb_sids_to_unixids
,
856 .allocate_id
= idmap_tdb_get_new_id
,
857 .close_fn
= idmap_tdb_close
860 NTSTATUS
idmap_tdb_init(void)
862 DEBUG(10, ("calling idmap_tdb_init\n"));
864 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_methods
);