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 /* High water mark keys */
37 #define HWM_GROUP "GROUP HWM"
38 #define HWM_USER "USER HWM"
40 static struct idmap_tdb_state
{
42 /* User and group id pool */
43 uid_t low_uid
, high_uid
; /* Range of uids to allocate */
44 gid_t low_gid
, high_gid
; /* Range of gids to allocate */
48 struct convert_fn_state
{
49 struct db_context
*db
;
53 /*****************************************************************************
54 For idmap conversion: convert one record to new format
55 Ancient versions (eg 2.2.3a) of winbindd_idmap.tdb mapped DOMAINNAME/rid
57 *****************************************************************************/
58 static int convert_fn(struct db_record
*rec
, void *private_data
)
60 struct winbindd_domain
*domain
;
68 struct convert_fn_state
*s
= (struct convert_fn_state
*)private_data
;
70 DEBUG(10,("Converting %s\n", (const char *)rec
->key
.dptr
));
72 p
= strchr((const char *)rec
->key
.dptr
, '/');
77 fstrcpy(dom_name
, (const char *)rec
->key
.dptr
);
80 domain
= find_domain_from_name(dom_name
);
82 /* We must delete the old record. */
83 DEBUG(0,("Unable to find domain %s\n", dom_name
));
84 DEBUG(0,("deleting record %s\n", (const char *)rec
->key
.dptr
));
86 status
= rec
->delete_rec(rec
);
87 if (!NT_STATUS_IS_OK(status
)) {
88 DEBUG(0, ("Unable to delete record %s:%s\n",
89 (const char *)rec
->key
.dptr
,
100 sid_copy(&sid
, &domain
->sid
);
101 sid_append_rid(&sid
, rid
);
103 sid_to_fstring(keystr
, &sid
);
104 key2
= string_term_tdb_data(keystr
);
106 status
= dbwrap_store(s
->db
, key2
, rec
->value
, TDB_INSERT
);
107 if (!NT_STATUS_IS_OK(status
)) {
108 DEBUG(0,("Unable to add record %s:%s\n",
109 (const char *)key2
.dptr
,
115 status
= dbwrap_store(s
->db
, rec
->value
, key2
, TDB_REPLACE
);
116 if (!NT_STATUS_IS_OK(status
)) {
117 DEBUG(0,("Unable to update record %s:%s\n",
118 (const char *)rec
->value
.dptr
,
124 status
= rec
->delete_rec(rec
);
125 if (!NT_STATUS_IS_OK(status
)) {
126 DEBUG(0,("Unable to delete record %s:%s\n",
127 (const char *)rec
->key
.dptr
,
136 /*****************************************************************************
137 Convert the idmap database from an older version.
138 *****************************************************************************/
140 static bool idmap_tdb_upgrade(struct db_context
*db
)
143 bool bigendianheader
;
144 struct convert_fn_state s
;
146 DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n"));
148 bigendianheader
= (db
->get_flags(db
) & TDB_BIGENDIAN
) ? True
: False
;
150 vers
= dbwrap_fetch_int32(db
, "IDMAP_VERSION");
152 if (((vers
== -1) && bigendianheader
) || (IREV(vers
) == IDMAP_VERSION
)) {
153 /* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
155 * high and low records were created on a
156 * big endian machine and will need byte-reversing.
161 wm
= dbwrap_fetch_int32(db
, HWM_USER
);
166 wm
= idmap_tdb_state
.low_uid
;
169 if (dbwrap_store_int32(db
, HWM_USER
, wm
) == -1) {
170 DEBUG(0, ("Unable to byteswap user hwm in idmap database\n"));
174 wm
= dbwrap_fetch_int32(db
, HWM_GROUP
);
178 wm
= idmap_tdb_state
.low_gid
;
181 if (dbwrap_store_int32(db
, HWM_GROUP
, wm
) == -1) {
182 DEBUG(0, ("Unable to byteswap group hwm in idmap database\n"));
190 /* the old format stored as DOMAIN/rid - now we store the SID direct */
191 db
->traverse(db
, convert_fn
, &s
);
194 DEBUG(0, ("Problem during conversion\n"));
198 if (dbwrap_store_int32(db
, "IDMAP_VERSION", IDMAP_VERSION
) == -1) {
199 DEBUG(0, ("Unable to store idmap version in databse\n"));
206 static NTSTATUS
idmap_tdb_load_ranges(void)
213 if (!lp_idmap_uid(&low_uid
, &high_uid
)) {
214 DEBUG(1, ("idmap uid missing\n"));
215 return NT_STATUS_UNSUCCESSFUL
;
218 if (!lp_idmap_gid(&low_gid
, &high_gid
)) {
219 DEBUG(1, ("idmap gid missing\n"));
220 return NT_STATUS_UNSUCCESSFUL
;
223 idmap_tdb_state
.low_uid
= low_uid
;
224 idmap_tdb_state
.high_uid
= high_uid
;
225 idmap_tdb_state
.low_gid
= low_gid
;
226 idmap_tdb_state
.high_gid
= high_gid
;
228 if (idmap_tdb_state
.high_uid
<= idmap_tdb_state
.low_uid
) {
229 DEBUG(1, ("idmap uid range missing or invalid\n"));
230 return NT_STATUS_UNSUCCESSFUL
;
233 if (idmap_tdb_state
.high_gid
<= idmap_tdb_state
.low_gid
) {
234 DEBUG(1, ("idmap gid range missing or invalid\n"));
235 return NT_STATUS_UNSUCCESSFUL
;
241 static NTSTATUS
idmap_tdb_open_db(TALLOC_CTX
*memctx
,
243 struct db_context
**dbctx
)
247 char *tdbfile
= NULL
;
248 struct db_context
*db
= NULL
;
250 bool config_error
= false;
252 ret
= idmap_tdb_load_ranges();
253 if (!NT_STATUS_IS_OK(ret
)) {
260 /* use our own context here */
261 ctx
= talloc_stackframe();
263 /* use the old database if present */
264 tdbfile
= state_path("winbindd_idmap.tdb");
266 DEBUG(0, ("Out of memory!\n"));
267 ret
= NT_STATUS_NO_MEMORY
;
271 DEBUG(10,("Opening tdbfile %s\n", tdbfile
));
273 /* Open idmap repository */
274 db
= db_open(ctx
, tdbfile
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644);
276 DEBUG(0, ("Unable to open idmap database\n"));
277 ret
= NT_STATUS_UNSUCCESSFUL
;
281 /* check against earlier versions */
282 version
= dbwrap_fetch_int32(db
, "IDMAP_VERSION");
283 if (version
!= IDMAP_VERSION
) {
285 DEBUG(0,("Upgrade of IDMAP_VERSION from %d to %d is not "
286 "possible with incomplete configuration\n",
287 version
, IDMAP_VERSION
));
288 ret
= NT_STATUS_UNSUCCESSFUL
;
291 if (db
->transaction_start(db
) != 0) {
292 DEBUG(0, ("Unable to start upgrade transaction!\n"));
293 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
297 if (!idmap_tdb_upgrade(db
)) {
298 db
->transaction_cancel(db
);
299 DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
300 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
304 if (db
->transaction_commit(db
) != 0) {
305 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
306 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
311 *dbctx
= talloc_move(memctx
, &db
);
319 /**********************************************************************
320 IDMAP ALLOC TDB BACKEND
321 **********************************************************************/
323 static struct db_context
*idmap_alloc_db
;
325 /**********************************
326 Initialise idmap alloc database.
327 **********************************/
329 static NTSTATUS
idmap_tdb_alloc_init( const char *params
)
335 bool update_uid
= false;
336 bool update_gid
= false;
338 status
= idmap_tdb_open_db(NULL
, true, &idmap_alloc_db
);
339 if (!NT_STATUS_IS_OK(status
)) {
340 DEBUG(0, ("idmap will be unable to map foreign SIDs: %s\n",
345 low_uid
= dbwrap_fetch_int32(idmap_alloc_db
, HWM_USER
);
346 if (low_uid
== -1 || low_uid
< idmap_tdb_state
.low_uid
) {
350 low_gid
= dbwrap_fetch_int32(idmap_alloc_db
, HWM_GROUP
);
351 if (low_gid
== -1 || low_gid
< idmap_tdb_state
.low_gid
) {
355 if (!update_uid
&& !update_gid
) {
359 if (idmap_alloc_db
->transaction_start(idmap_alloc_db
) != 0) {
360 TALLOC_FREE(idmap_alloc_db
);
361 DEBUG(0, ("Unable to start upgrade transaction!\n"));
362 return NT_STATUS_INTERNAL_DB_ERROR
;
366 ret
= dbwrap_store_int32(idmap_alloc_db
, HWM_USER
,
367 idmap_tdb_state
.low_uid
);
369 idmap_alloc_db
->transaction_cancel(idmap_alloc_db
);
370 TALLOC_FREE(idmap_alloc_db
);
371 DEBUG(0, ("Unable to initialise user hwm in idmap "
373 return NT_STATUS_INTERNAL_DB_ERROR
;
378 ret
= dbwrap_store_int32(idmap_alloc_db
, HWM_GROUP
,
379 idmap_tdb_state
.low_gid
);
381 idmap_alloc_db
->transaction_cancel(idmap_alloc_db
);
382 TALLOC_FREE(idmap_alloc_db
);
383 DEBUG(0, ("Unable to initialise group hwm in idmap "
385 return NT_STATUS_INTERNAL_DB_ERROR
;
389 if (idmap_alloc_db
->transaction_commit(idmap_alloc_db
) != 0) {
390 TALLOC_FREE(idmap_alloc_db
);
391 DEBUG(0, ("Unable to commit upgrade transaction!\n"));
392 return NT_STATUS_INTERNAL_DB_ERROR
;
398 /**********************************
400 **********************************/
402 static NTSTATUS
idmap_tdb_allocate_id(struct unixid
*xid
)
411 /* Get current high water mark */
417 high_hwm
= idmap_tdb_state
.high_uid
;
423 high_hwm
= idmap_tdb_state
.high_gid
;
427 DEBUG(2, ("Invalid ID type (0x%x)\n", xid
->type
));
428 return NT_STATUS_INVALID_PARAMETER
;
431 res
= idmap_alloc_db
->transaction_start(idmap_alloc_db
);
433 DEBUG(1, (__location__
" Failed to start transaction.\n"));
434 return NT_STATUS_UNSUCCESSFUL
;
437 if ((hwm
= dbwrap_fetch_int32(idmap_alloc_db
, hwmkey
)) == -1) {
438 idmap_alloc_db
->transaction_cancel(idmap_alloc_db
);
439 return NT_STATUS_INTERNAL_DB_ERROR
;
442 /* check it is in the range */
443 if (hwm
> high_hwm
) {
444 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
445 hwmtype
, (unsigned long)high_hwm
));
446 idmap_alloc_db
->transaction_cancel(idmap_alloc_db
);
447 return NT_STATUS_UNSUCCESSFUL
;
450 /* fetch a new id and increment it */
451 ret
= dbwrap_change_uint32_atomic(idmap_alloc_db
, hwmkey
, &hwm
, 1);
452 if (!NT_STATUS_IS_OK(ret
)) {
453 DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
454 hwmtype
, nt_errstr(ret
)));
455 idmap_alloc_db
->transaction_cancel(idmap_alloc_db
);
459 /* recheck it is in the range */
460 if (hwm
> high_hwm
) {
461 DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
462 hwmtype
, (unsigned long)high_hwm
));
463 idmap_alloc_db
->transaction_cancel(idmap_alloc_db
);
464 return NT_STATUS_UNSUCCESSFUL
;
467 res
= idmap_alloc_db
->transaction_commit(idmap_alloc_db
);
469 DEBUG(1, (__location__
" Failed to commit transaction.\n"));
470 return NT_STATUS_UNSUCCESSFUL
;
474 DEBUG(10,("New %s = %d\n", hwmtype
, hwm
));
479 /**********************************
480 Get current highest id.
481 **********************************/
483 static NTSTATUS
idmap_tdb_get_hwm(struct unixid
*xid
)
490 /* Get current high water mark */
496 high_hwm
= idmap_tdb_state
.high_uid
;
502 high_hwm
= idmap_tdb_state
.high_gid
;
506 return NT_STATUS_INVALID_PARAMETER
;
509 if ((hwm
= dbwrap_fetch_int32(idmap_alloc_db
, hwmkey
)) == -1) {
510 return NT_STATUS_INTERNAL_DB_ERROR
;
515 /* Warn if it is out of range */
516 if (hwm
>= high_hwm
) {
517 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
518 hwmtype
, (unsigned long)high_hwm
));
524 /**********************************
526 **********************************/
528 static NTSTATUS
idmap_tdb_set_hwm(struct unixid
*xid
)
536 /* Get current high water mark */
542 high_hwm
= idmap_tdb_state
.high_uid
;
548 high_hwm
= idmap_tdb_state
.high_gid
;
552 return NT_STATUS_INVALID_PARAMETER
;
557 /* Warn if it is out of range */
558 if (hwm
>= high_hwm
) {
559 DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
560 hwmtype
, (unsigned long)high_hwm
));
563 ret
= dbwrap_trans_store_uint32(idmap_alloc_db
, hwmkey
, hwm
);
568 /**********************************
570 **********************************/
572 static NTSTATUS
idmap_tdb_alloc_close(void)
574 TALLOC_FREE(idmap_alloc_db
);
578 /**********************************************************************
579 IDMAP MAPPING TDB BACKEND
580 **********************************************************************/
582 struct idmap_tdb_context
{
583 struct db_context
*db
;
584 uint32_t filter_low_id
;
585 uint32_t filter_high_id
;
588 /*****************************
589 Initialise idmap database.
590 *****************************/
592 static NTSTATUS
idmap_tdb_db_init(struct idmap_domain
*dom
, const char *params
)
595 struct idmap_tdb_context
*ctx
;
597 DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom
->name
));
599 ctx
= talloc(dom
, struct idmap_tdb_context
);
601 DEBUG(0, ("Out of memory!\n"));
602 return NT_STATUS_NO_MEMORY
;
605 if (strequal(dom
->name
, "*")) {
611 ctx
->filter_low_id
= 0;
612 ctx
->filter_high_id
= 0;
614 if (lp_idmap_uid(&low_uid
, &high_uid
)) {
615 ctx
->filter_low_id
= low_uid
;
616 ctx
->filter_high_id
= high_uid
;
618 DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
621 if (lp_idmap_gid(&low_gid
, &high_gid
)) {
622 if ((low_gid
!= low_uid
) || (high_gid
!= high_uid
)) {
623 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
624 " ranges do not agree -- building "
626 ctx
->filter_low_id
= MAX(ctx
->filter_low_id
,
628 ctx
->filter_high_id
= MIN(ctx
->filter_high_id
,
632 DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
635 char *config_option
= NULL
;
638 config_option
= talloc_asprintf(ctx
, "idmap config %s", dom
->name
);
639 if ( ! config_option
) {
640 DEBUG(0, ("Out of memory!\n"));
641 ret
= NT_STATUS_NO_MEMORY
;
645 range
= lp_parm_const_string(-1, config_option
, "range", NULL
);
647 (sscanf(range
, "%u - %u", &ctx
->filter_low_id
, &ctx
->filter_high_id
) != 2))
649 ctx
->filter_low_id
= 0;
650 ctx
->filter_high_id
= 0;
653 talloc_free(config_option
);
656 if (ctx
->filter_low_id
> ctx
->filter_high_id
) {
657 ctx
->filter_low_id
= 0;
658 ctx
->filter_high_id
= 0;
661 DEBUG(10, ("idmap_tdb_db_init: filter range %u-%u loaded for domain "
662 "'%s'\n", ctx
->filter_low_id
, ctx
->filter_high_id
, dom
->name
));
664 ret
= idmap_tdb_open_db(ctx
, false, &ctx
->db
);
665 if ( ! NT_STATUS_IS_OK(ret
)) {
669 dom
->private_data
= ctx
;
678 /**********************************
679 Single id to sid lookup function.
680 **********************************/
682 static NTSTATUS
idmap_tdb_id_to_sid(struct idmap_tdb_context
*ctx
, struct id_map
*map
)
689 return NT_STATUS_INVALID_PARAMETER
;
692 /* apply filters before checking */
693 if ((ctx
->filter_low_id
&& (map
->xid
.id
< ctx
->filter_low_id
)) ||
694 (ctx
->filter_high_id
&& (map
->xid
.id
> ctx
->filter_high_id
))) {
695 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
696 map
->xid
.id
, ctx
->filter_low_id
, ctx
->filter_high_id
));
697 return NT_STATUS_NONE_MAPPED
;
700 switch (map
->xid
.type
) {
703 keystr
= talloc_asprintf(ctx
, "UID %lu", (unsigned long)map
->xid
.id
);
707 keystr
= talloc_asprintf(ctx
, "GID %lu", (unsigned long)map
->xid
.id
);
711 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
712 return NT_STATUS_INVALID_PARAMETER
;
715 /* final SAFE_FREE safe */
718 if (keystr
== NULL
) {
719 DEBUG(0, ("Out of memory!\n"));
720 ret
= NT_STATUS_NO_MEMORY
;
724 DEBUG(10,("Fetching record %s\n", keystr
));
726 /* Check if the mapping exists */
727 data
= dbwrap_fetch_bystring(ctx
->db
, NULL
, keystr
);
730 DEBUG(10,("Record %s not found\n", keystr
));
731 ret
= NT_STATUS_NONE_MAPPED
;
735 if (!string_to_sid(map
->sid
, (const char *)data
.dptr
)) {
736 DEBUG(10,("INVALID SID (%s) in record %s\n",
737 (const char *)data
.dptr
, keystr
));
738 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
742 DEBUG(10,("Found record %s -> %s\n", keystr
, (const char *)data
.dptr
));
746 talloc_free(data
.dptr
);
751 /**********************************
752 Single sid to id lookup function.
753 **********************************/
755 static NTSTATUS
idmap_tdb_sid_to_id(struct idmap_tdb_context
*ctx
, struct id_map
*map
)
760 unsigned long rec_id
= 0;
761 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
763 keystr
= sid_string_talloc(tmp_ctx
, map
->sid
);
764 if (keystr
== NULL
) {
765 DEBUG(0, ("Out of memory!\n"));
766 ret
= NT_STATUS_NO_MEMORY
;
770 DEBUG(10,("Fetching record %s\n", keystr
));
772 /* Check if sid is present in database */
773 data
= dbwrap_fetch_bystring(ctx
->db
, tmp_ctx
, keystr
);
775 DEBUG(10,("Record %s not found\n", keystr
));
776 ret
= NT_STATUS_NONE_MAPPED
;
780 /* What type of record is this ? */
781 if (sscanf((const char *)data
.dptr
, "UID %lu", &rec_id
) == 1) { /* Try a UID record. */
782 map
->xid
.id
= rec_id
;
783 map
->xid
.type
= ID_TYPE_UID
;
784 DEBUG(10,("Found uid record %s -> %s \n", keystr
, (const char *)data
.dptr
));
787 } else if (sscanf((const char *)data
.dptr
, "GID %lu", &rec_id
) == 1) { /* Try a GID record. */
788 map
->xid
.id
= rec_id
;
789 map
->xid
.type
= ID_TYPE_GID
;
790 DEBUG(10,("Found gid record %s -> %s \n", keystr
, (const char *)data
.dptr
));
793 } else { /* Unknown record type ! */
794 DEBUG(2, ("Found INVALID record %s -> %s\n", keystr
, (const char *)data
.dptr
));
795 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
798 /* apply filters before returning result */
799 if ((ctx
->filter_low_id
&& (map
->xid
.id
< ctx
->filter_low_id
)) ||
800 (ctx
->filter_high_id
&& (map
->xid
.id
> ctx
->filter_high_id
))) {
801 DEBUG(5, ("Requested id (%u) out of range (%u - %u). Filtered!\n",
802 map
->xid
.id
, ctx
->filter_low_id
, ctx
->filter_high_id
));
803 ret
= NT_STATUS_NONE_MAPPED
;
807 talloc_free(tmp_ctx
);
811 /**********************************
812 lookup a set of unix ids.
813 **********************************/
815 static NTSTATUS
idmap_tdb_unixids_to_sids(struct idmap_domain
*dom
, struct id_map
**ids
)
817 struct idmap_tdb_context
*ctx
;
821 /* initialize the status to avoid suprise */
822 for (i
= 0; ids
[i
]; i
++) {
823 ids
[i
]->status
= ID_UNKNOWN
;
826 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
828 for (i
= 0; ids
[i
]; i
++) {
829 ret
= idmap_tdb_id_to_sid(ctx
, ids
[i
]);
830 if ( ! NT_STATUS_IS_OK(ret
)) {
832 /* if it is just a failed mapping continue */
833 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
835 /* make sure it is marked as unmapped */
836 ids
[i
]->status
= ID_UNMAPPED
;
840 /* some fatal error occurred, return immediately */
844 /* all ok, id is mapped */
845 ids
[i
]->status
= ID_MAPPED
;
854 /**********************************
855 lookup a set of sids.
856 **********************************/
858 static NTSTATUS
idmap_tdb_sids_to_unixids(struct idmap_domain
*dom
, struct id_map
**ids
)
860 struct idmap_tdb_context
*ctx
;
864 /* initialize the status to avoid suprise */
865 for (i
= 0; ids
[i
]; i
++) {
866 ids
[i
]->status
= ID_UNKNOWN
;
869 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
871 for (i
= 0; ids
[i
]; i
++) {
872 ret
= idmap_tdb_sid_to_id(ctx
, ids
[i
]);
873 if ( ! NT_STATUS_IS_OK(ret
)) {
875 /* if it is just a failed mapping continue */
876 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
878 /* make sure it is marked as unmapped */
879 ids
[i
]->status
= ID_UNMAPPED
;
883 /* some fatal error occurred, return immediately */
887 /* all ok, id is mapped */
888 ids
[i
]->status
= ID_MAPPED
;
897 /**********************************
899 **********************************/
901 static NTSTATUS
idmap_tdb_set_mapping(struct idmap_domain
*dom
,
902 const struct id_map
*map
)
904 struct idmap_tdb_context
*ctx
;
907 char *ksidstr
, *kidstr
;
910 if (!map
|| !map
->sid
) {
911 return NT_STATUS_INVALID_PARAMETER
;
914 ksidstr
= kidstr
= NULL
;
916 /* TODO: should we filter a set_mapping using low/high filters ? */
918 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
920 switch (map
->xid
.type
) {
923 kidstr
= talloc_asprintf(ctx
, "UID %lu",
924 (unsigned long)map
->xid
.id
);
928 kidstr
= talloc_asprintf(ctx
, "GID %lu",
929 (unsigned long)map
->xid
.id
);
933 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
934 return NT_STATUS_INVALID_PARAMETER
;
937 if (kidstr
== NULL
) {
938 DEBUG(0, ("ERROR: Out of memory!\n"));
939 ret
= NT_STATUS_NO_MEMORY
;
943 if ((ksidstr
= talloc_asprintf(
944 ctx
, "%s", sid_to_fstring(tmp
, map
->sid
))) == NULL
) {
945 DEBUG(0, ("Out of memory!\n"));
946 ret
= NT_STATUS_NO_MEMORY
;
950 DEBUG(10, ("Storing %s <-> %s map\n", ksidstr
, kidstr
));
951 kid
= string_term_tdb_data(kidstr
);
952 ksid
= string_term_tdb_data(ksidstr
);
954 if (ctx
->db
->transaction_start(ctx
->db
) != 0) {
955 DEBUG(0, ("Failed to start transaction for %s\n",
957 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
961 ret
= dbwrap_store(ctx
->db
, ksid
, kid
, TDB_REPLACE
);
962 if (!NT_STATUS_IS_OK(ret
)) {
963 ctx
->db
->transaction_cancel(ctx
->db
);
964 DEBUG(0, ("Error storing SID -> ID (%s -> %s): %s\n",
965 ksidstr
, kidstr
, nt_errstr(ret
)));
968 ret
= dbwrap_store(ctx
->db
, kid
, ksid
, TDB_REPLACE
);
969 if (!NT_STATUS_IS_OK(ret
)) {
970 ctx
->db
->transaction_cancel(ctx
->db
);
971 DEBUG(0, ("Error storing ID -> SID (%s -> %s): %s\n",
972 kidstr
, ksidstr
, nt_errstr(ret
)));
976 if (ctx
->db
->transaction_commit(ctx
->db
) != 0) {
977 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
979 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
983 DEBUG(10,("Stored %s <-> %s\n", ksidstr
, kidstr
));
987 talloc_free(ksidstr
);
992 /**********************************
994 **********************************/
996 static NTSTATUS
idmap_tdb_remove_mapping(struct idmap_domain
*dom
,
997 const struct id_map
*map
)
999 struct idmap_tdb_context
*ctx
;
1001 TDB_DATA ksid
, kid
, data
;
1002 char *ksidstr
, *kidstr
;
1005 if (!map
|| !map
->sid
) {
1006 return NT_STATUS_INVALID_PARAMETER
;
1009 ksidstr
= kidstr
= NULL
;
1012 /* TODO: should we filter a remove_mapping using low/high filters ? */
1014 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
1016 switch (map
->xid
.type
) {
1019 kidstr
= talloc_asprintf(ctx
, "UID %lu",
1020 (unsigned long)map
->xid
.id
);
1024 kidstr
= talloc_asprintf(ctx
, "GID %lu",
1025 (unsigned long)map
->xid
.id
);
1029 DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map
->xid
.type
));
1030 return NT_STATUS_INVALID_PARAMETER
;
1033 if (kidstr
== NULL
) {
1034 DEBUG(0, ("ERROR: Out of memory!\n"));
1035 ret
= NT_STATUS_NO_MEMORY
;
1039 if ((ksidstr
= talloc_asprintf(
1040 ctx
, "%s", sid_to_fstring(tmp
, map
->sid
))) == NULL
) {
1041 DEBUG(0, ("Out of memory!\n"));
1042 ret
= NT_STATUS_NO_MEMORY
;
1046 DEBUG(10, ("Checking %s <-> %s map\n", ksidstr
, kidstr
));
1047 ksid
= string_term_tdb_data(ksidstr
);
1048 kid
= string_term_tdb_data(kidstr
);
1050 if (ctx
->db
->transaction_start(ctx
->db
) != 0) {
1051 DEBUG(0, ("Failed to start transaction for %s\n",
1053 return NT_STATUS_INTERNAL_DB_ERROR
;
1056 /* Check if sid is present in database */
1057 data
= dbwrap_fetch(ctx
->db
, NULL
, ksid
);
1059 ctx
->db
->transaction_cancel(ctx
->db
);
1060 DEBUG(10,("Record %s not found\n", ksidstr
));
1061 ret
= NT_STATUS_NONE_MAPPED
;
1065 /* Check if sid is mapped to the specified ID */
1066 if ((data
.dsize
!= kid
.dsize
) ||
1067 (memcmp(data
.dptr
, kid
.dptr
, data
.dsize
) != 0)) {
1068 ctx
->db
->transaction_cancel(ctx
->db
);
1069 DEBUG(10,("Specified SID does not map to specified ID\n"));
1070 DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr
,
1071 (const char *)data
.dptr
));
1072 ret
= NT_STATUS_NONE_MAPPED
;
1076 DEBUG(10, ("Removing %s <-> %s map\n", ksidstr
, kidstr
));
1078 /* Delete previous mappings. */
1080 DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr
, kidstr
));
1081 ret
= dbwrap_delete(ctx
->db
, ksid
);
1082 if (!NT_STATUS_IS_OK(ret
)) {
1083 DEBUG(0,("Warning: Failed to delete %s: %s\n",
1084 ksidstr
, nt_errstr(ret
)));
1087 DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr
, ksidstr
));
1088 ret
= dbwrap_delete(ctx
->db
, kid
);
1089 if (!NT_STATUS_IS_OK(ret
)) {
1090 DEBUG(0,("Warning: Failed to delete %s: %s\n",
1091 kidstr
, nt_errstr(ret
)));
1094 if (ctx
->db
->transaction_commit(ctx
->db
) != 0) {
1095 DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
1097 ret
= NT_STATUS_INTERNAL_DB_ERROR
;
1104 talloc_free(ksidstr
);
1105 talloc_free(kidstr
);
1106 talloc_free(data
.dptr
);
1110 /**********************************
1111 Close the idmap tdb instance
1112 **********************************/
1114 static NTSTATUS
idmap_tdb_close(struct idmap_domain
*dom
)
1116 struct idmap_tdb_context
*ctx
;
1118 if (dom
->private_data
) {
1119 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
1121 TALLOC_FREE(ctx
->db
);
1123 return NT_STATUS_OK
;
1128 struct id_map
**maps
;
1133 static int idmap_tdb_dump_one_entry(struct db_record
*rec
, void *pdata
)
1135 struct dump_data
*data
= talloc_get_type(pdata
, struct dump_data
);
1136 struct id_map
*maps
;
1137 int num_maps
= *data
->num_maps
;
1139 /* ignore any record but the ones with a SID as key */
1140 if (strncmp((const char *)rec
->key
.dptr
, "S-", 2) == 0) {
1142 maps
= talloc_realloc(NULL
, *data
->maps
, struct id_map
, num_maps
+1);
1144 DEBUG(0, ("Out of memory!\n"));
1145 data
->ret
= NT_STATUS_NO_MEMORY
;
1149 maps
[num_maps
].sid
= talloc(maps
, DOM_SID
);
1150 if ( ! maps
[num_maps
].sid
) {
1151 DEBUG(0, ("Out of memory!\n"));
1152 data
->ret
= NT_STATUS_NO_MEMORY
;
1156 if (!string_to_sid(maps
[num_maps
].sid
, (const char *)rec
->key
.dptr
)) {
1157 DEBUG(10,("INVALID record %s\n", (const char *)rec
->key
.dptr
));
1158 /* continue even with errors */
1162 /* Try a UID record. */
1163 if (sscanf((const char *)rec
->value
.dptr
, "UID %u", &(maps
[num_maps
].xid
.id
)) == 1) {
1164 maps
[num_maps
].xid
.type
= ID_TYPE_UID
;
1165 maps
[num_maps
].status
= ID_MAPPED
;
1166 *data
->num_maps
= num_maps
+ 1;
1168 /* Try a GID record. */
1170 if (sscanf((const char *)rec
->value
.dptr
, "GID %u", &(maps
[num_maps
].xid
.id
)) == 1) {
1171 maps
[num_maps
].xid
.type
= ID_TYPE_GID
;
1172 maps
[num_maps
].status
= ID_MAPPED
;
1173 *data
->num_maps
= num_maps
+ 1;
1175 /* Unknown record type ! */
1177 maps
[num_maps
].status
= ID_UNKNOWN
;
1178 DEBUG(2, ("Found INVALID record %s -> %s\n",
1179 (const char *)rec
->key
.dptr
,
1180 (const char *)rec
->value
.dptr
));
1181 /* do not increment num_maps */
1188 /**********************************
1189 Dump all mappings out
1190 **********************************/
1192 static NTSTATUS
idmap_tdb_dump_data(struct idmap_domain
*dom
, struct id_map
**maps
, int *num_maps
)
1194 struct idmap_tdb_context
*ctx
;
1195 struct dump_data
*data
;
1196 NTSTATUS ret
= NT_STATUS_OK
;
1198 ctx
= talloc_get_type(dom
->private_data
, struct idmap_tdb_context
);
1200 data
= TALLOC_ZERO_P(ctx
, struct dump_data
);
1202 DEBUG(0, ("Out of memory!\n"));
1203 return NT_STATUS_NO_MEMORY
;
1206 data
->num_maps
= num_maps
;
1207 data
->ret
= NT_STATUS_OK
;
1209 ctx
->db
->traverse_read(ctx
->db
, idmap_tdb_dump_one_entry
, data
);
1211 if ( ! NT_STATUS_IS_OK(data
->ret
)) {
1219 static struct idmap_methods db_methods
= {
1221 .init
= idmap_tdb_db_init
,
1222 .unixids_to_sids
= idmap_tdb_unixids_to_sids
,
1223 .sids_to_unixids
= idmap_tdb_sids_to_unixids
,
1224 .set_mapping
= idmap_tdb_set_mapping
,
1225 .remove_mapping
= idmap_tdb_remove_mapping
,
1226 .dump_data
= idmap_tdb_dump_data
,
1227 .close_fn
= idmap_tdb_close
1230 static struct idmap_alloc_methods db_alloc_methods
= {
1232 .init
= idmap_tdb_alloc_init
,
1233 .allocate_id
= idmap_tdb_allocate_id
,
1234 .get_id_hwm
= idmap_tdb_get_hwm
,
1235 .set_id_hwm
= idmap_tdb_set_hwm
,
1236 .close_fn
= idmap_tdb_alloc_close
1239 NTSTATUS
idmap_alloc_tdb_init(void)
1241 return smb_register_idmap_alloc(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_alloc_methods
);
1244 NTSTATUS
idmap_tdb_init(void)
1248 DEBUG(10, ("calling idmap_tdb_init\n"));
1250 /* FIXME: bad hack to actually register also the alloc_tdb module without changining configure.in */
1251 ret
= idmap_alloc_tdb_init();
1252 if (! NT_STATUS_IS_OK(ret
)) {
1255 return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION
, "tdb", &db_methods
);