2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-2006,
5 * Copyright (C) Jean François Micouleau 1998-2001.
6 * Copyright (C) Volker Lendecke 2006.
7 * Copyright (C) Gerald Carter 2006.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "groupdb/mapping.h"
26 static struct db_context
*db
; /* used for driver files */
28 static bool enum_group_mapping(const DOM_SID
*domsid
, enum lsa_SidType sid_name_use
, GROUP_MAP
**pp_rmap
,
29 size_t *p_num_entries
, bool unix_only
);
30 static bool group_map_remove(const DOM_SID
*sid
);
32 /****************************************************************************
33 Open the group mapping tdb.
34 ****************************************************************************/
35 static bool init_group_mapping(void)
41 db
= db_open_trans(NULL
, state_path("group_mapping.tdb"), 0,
42 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
44 DEBUG(0, ("Failed to open group mapping database: %s\n",
51 * This code was designed to handle a group mapping version
52 * upgrade. mapping_tdb is not active by default anymore, so ignore
56 const char *vstring
= "INFO/version";
58 GROUP_MAP
*map_table
= NULL
;
59 size_t num_entries
= 0;
61 /* handle a Samba upgrade */
62 tdb_lock_bystring(tdb
, vstring
);
64 /* Cope with byte-reversed older versions of the db. */
65 vers_id
= tdb_fetch_int32(tdb
, vstring
);
66 if ((vers_id
== DATABASE_VERSION_V1
)
67 || (IREV(vers_id
) == DATABASE_VERSION_V1
)) {
69 * Written on a bigendian machine with old fetch_int
72 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
73 vers_id
= DATABASE_VERSION_V2
;
76 /* if its an unknown version we remove everthing in the db */
78 if (vers_id
!= DATABASE_VERSION_V2
) {
80 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
83 tdb_unlock_bystring(tdb
, vstring
);
85 /* cleanup any map entries with a gid == -1 */
87 if ( enum_group_mapping( NULL
, SID_NAME_UNKNOWN
, &map_table
,
88 &num_entries
, False
) ) {
91 for ( i
=0; i
<num_entries
; i
++ ) {
92 if ( map_table
[i
].gid
== -1 ) {
93 group_map_remove( &map_table
[i
].sid
);
97 SAFE_FREE( map_table
);
105 static char *group_mapping_key(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
)
107 char *sidstr
, *result
;
109 sidstr
= sid_string_talloc(talloc_tos(), sid
);
110 if (sidstr
== NULL
) {
114 result
= talloc_asprintf(mem_ctx
, "%s%s", GROUP_PREFIX
, sidstr
);
120 /****************************************************************************
121 ****************************************************************************/
122 static bool add_mapping_entry(GROUP_MAP
*map
, int flag
)
128 key
= group_mapping_key(talloc_tos(), &map
->sid
);
133 len
= tdb_pack(NULL
, 0, "ddff",
134 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
);
136 buf
= TALLOC_ARRAY(key
, char, len
);
141 len
= tdb_pack((uint8
*)buf
, len
, "ddff", map
->gid
,
142 map
->sid_name_use
, map
->nt_name
, map
->comment
);
144 status
= dbwrap_trans_store(
145 db
, string_term_tdb_data(key
),
146 make_tdb_data((uint8_t *)buf
, len
), TDB_REPLACE
);
150 return NT_STATUS_IS_OK(status
);
154 /****************************************************************************
155 Return the sid and the type of the unix group.
156 ****************************************************************************/
158 static bool get_group_map_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
164 /* the key is the SID, retrieving is direct */
166 key
= group_mapping_key(talloc_tos(), &sid
);
171 dbuf
= dbwrap_fetch_bystring(db
, key
, key
);
172 if (dbuf
.dptr
== NULL
) {
177 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
178 &map
->gid
, &map
->sid_name_use
,
179 &map
->nt_name
, &map
->comment
);
184 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
188 sid_copy(&map
->sid
, &sid
);
193 static bool dbrec2map(const struct db_record
*rec
, GROUP_MAP
*map
)
195 if ((rec
->key
.dsize
< strlen(GROUP_PREFIX
))
196 || (strncmp((char *)rec
->key
.dptr
, GROUP_PREFIX
,
197 GROUP_PREFIX_LEN
) != 0)) {
201 if (!string_to_sid(&map
->sid
, (const char *)rec
->key
.dptr
202 + GROUP_PREFIX_LEN
)) {
206 return tdb_unpack(rec
->value
.dptr
, rec
->value
.dsize
, "ddff",
207 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
,
208 &map
->comment
) != -1;
211 struct find_map_state
{
213 const char *name
; /* If != NULL, look for name */
214 gid_t gid
; /* valid iff name == NULL */
218 static int find_map(struct db_record
*rec
, void *private_data
)
220 struct find_map_state
*state
= (struct find_map_state
*)private_data
;
222 if (!dbrec2map(rec
, state
->map
)) {
223 DEBUG(10, ("failed to unpack map\n"));
227 if (state
->name
!= NULL
) {
228 if (strequal(state
->name
, state
->map
->nt_name
)) {
234 if (state
->map
->gid
== state
->gid
) {
243 /****************************************************************************
244 Return the sid and the type of the unix group.
245 ****************************************************************************/
247 static bool get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
249 struct find_map_state state
;
252 state
.name
= NULL
; /* Indicate we're looking for gid */
256 db
->traverse_read(db
, find_map
, (void *)&state
);
261 /****************************************************************************
262 Return the sid and the type of the unix group.
263 ****************************************************************************/
265 static bool get_group_map_from_ntname(const char *name
, GROUP_MAP
*map
)
267 struct find_map_state state
;
273 db
->traverse_read(db
, find_map
, (void *)&state
);
278 /****************************************************************************
279 Remove a group mapping entry.
280 ****************************************************************************/
282 static bool group_map_remove(const DOM_SID
*sid
)
287 key
= group_mapping_key(talloc_tos(), sid
);
292 status
= dbwrap_trans_delete(db
, string_term_tdb_data(key
));
295 return NT_STATUS_IS_OK(status
);
298 /****************************************************************************
299 Enumerate the group mapping.
300 ****************************************************************************/
302 struct enum_map_state
{
303 const DOM_SID
*domsid
;
304 enum lsa_SidType sid_name_use
;
311 static int collect_map(struct db_record
*rec
, void *private_data
)
313 struct enum_map_state
*state
= (struct enum_map_state
*)private_data
;
317 if (!dbrec2map(rec
, &map
)) {
320 /* list only the type or everything if UNKNOWN */
321 if (state
->sid_name_use
!= SID_NAME_UNKNOWN
322 && state
->sid_name_use
!= map
.sid_name_use
) {
323 DEBUG(11,("enum_group_mapping: group %s is not of the "
324 "requested type\n", map
.nt_name
));
328 if ((state
->unix_only
== ENUM_ONLY_MAPPED
) && (map
.gid
== -1)) {
329 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
334 if ((state
->domsid
!= NULL
) &&
335 (sid_compare_domain(state
->domsid
, &map
.sid
) != 0)) {
336 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
337 sid_string_dbg(&map
.sid
)));
341 if (!(tmp
= SMB_REALLOC_ARRAY(state
->maps
, GROUP_MAP
,
342 state
->num_maps
+1))) {
343 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
349 state
->maps
[state
->num_maps
] = map
;
354 static bool enum_group_mapping(const DOM_SID
*domsid
,
355 enum lsa_SidType sid_name_use
,
357 size_t *p_num_entries
, bool unix_only
)
359 struct enum_map_state state
;
361 state
.domsid
= domsid
;
362 state
.sid_name_use
= sid_name_use
;
363 state
.unix_only
= unix_only
;
367 if (db
->traverse_read(db
, collect_map
, (void *)&state
) < 0) {
371 *pp_rmap
= state
.maps
;
372 *p_num_entries
= state
.num_maps
;
377 /* This operation happens on session setup, so it should better be fast. We
378 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
380 static NTSTATUS
one_alias_membership(const DOM_SID
*member
,
381 DOM_SID
**sids
, size_t *num
)
388 NTSTATUS status
= NT_STATUS_OK
;
389 TALLOC_CTX
*frame
= talloc_stackframe();
391 slprintf(key
, sizeof(key
), "%s%s", MEMBEROF_PREFIX
,
392 sid_to_fstring(tmp
, member
));
394 dbuf
= dbwrap_fetch_bystring(db
, frame
, key
);
395 if (dbuf
.dptr
== NULL
) {
400 p
= (const char *)dbuf
.dptr
;
402 while (next_token_talloc(frame
, &p
, &string_sid
, " ")) {
405 if (!string_to_sid(&alias
, string_sid
))
408 status
= add_sid_to_array_unique(NULL
, &alias
, sids
, num
);
409 if (!NT_STATUS_IS_OK(status
)) {
419 static NTSTATUS
alias_memberships(const DOM_SID
*members
, size_t num_members
,
420 DOM_SID
**sids
, size_t *num
)
427 for (i
=0; i
<num_members
; i
++) {
428 NTSTATUS status
= one_alias_membership(&members
[i
], sids
, num
);
429 if (!NT_STATUS_IS_OK(status
))
435 static bool is_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
440 /* This feels the wrong way round, but the on-disk data structure
441 * dictates it this way. */
442 if (!NT_STATUS_IS_OK(alias_memberships(member
, 1, &sids
, &num
)))
445 for (i
=0; i
<num
; i
++) {
446 if (sid_compare(alias
, &sids
[i
]) == 0) {
456 static NTSTATUS
add_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
461 char *new_memberstring
;
462 struct db_record
*rec
;
465 if (!get_group_map_from_sid(*alias
, &map
))
466 return NT_STATUS_NO_SUCH_ALIAS
;
468 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
469 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
470 return NT_STATUS_NO_SUCH_ALIAS
;
472 if (is_aliasmem(alias
, member
))
473 return NT_STATUS_MEMBER_IN_ALIAS
;
475 sid_to_fstring(string_sid
, member
);
477 key
= talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX
,
480 return NT_STATUS_NO_MEMORY
;
483 if (db
->transaction_start(db
) != 0) {
484 DEBUG(0, ("transaction_start failed\n"));
485 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
488 rec
= db
->fetch_locked(db
, key
, string_term_tdb_data(key
));
491 DEBUG(10, ("fetch_lock failed\n"));
493 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
497 sid_to_fstring(string_sid
, alias
);
499 if (rec
->value
.dptr
!= NULL
) {
500 new_memberstring
= talloc_asprintf(
501 key
, "%s %s", (char *)(rec
->value
.dptr
), string_sid
);
503 new_memberstring
= talloc_strdup(key
, string_sid
);
506 if (new_memberstring
== NULL
) {
508 status
= NT_STATUS_NO_MEMORY
;
512 status
= rec
->store(rec
, string_term_tdb_data(new_memberstring
), 0);
516 if (!NT_STATUS_IS_OK(status
)) {
517 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status
)));
521 if (db
->transaction_commit(db
) != 0) {
522 DEBUG(0, ("transaction_commit failed\n"));
523 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
530 if (db
->transaction_cancel(db
) != 0) {
531 smb_panic("transaction_cancel failed");
537 struct aliasmem_state
{
538 const DOM_SID
*alias
;
543 static int collect_aliasmem(struct db_record
*rec
, void *priv
)
545 struct aliasmem_state
*state
= (struct aliasmem_state
*)priv
;
550 if (strncmp((const char *)rec
->key
.dptr
, MEMBEROF_PREFIX
,
551 MEMBEROF_PREFIX_LEN
) != 0)
554 p
= (const char *)rec
->value
.dptr
;
556 frame
= talloc_stackframe();
558 while (next_token_talloc(frame
, &p
, &alias_string
, " ")) {
559 DOM_SID alias
, member
;
560 const char *member_string
;
562 if (!string_to_sid(&alias
, alias_string
))
565 if (sid_compare(state
->alias
, &alias
) != 0)
568 /* Ok, we found the alias we're looking for in the membership
569 * list currently scanned. The key represents the alias
570 * member. Add that. */
572 member_string
= strchr((const char *)rec
->key
.dptr
, '/');
574 /* Above we tested for MEMBEROF_PREFIX which includes the
577 SMB_ASSERT(member_string
!= NULL
);
580 if (!string_to_sid(&member
, member_string
))
583 if (!NT_STATUS_IS_OK(add_sid_to_array(NULL
, &member
,
596 static NTSTATUS
enum_aliasmem(const DOM_SID
*alias
, DOM_SID
**sids
, size_t *num
)
599 struct aliasmem_state state
;
601 if (!get_group_map_from_sid(*alias
, &map
))
602 return NT_STATUS_NO_SUCH_ALIAS
;
604 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
605 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
606 return NT_STATUS_NO_SUCH_ALIAS
;
615 db
->traverse_read(db
, collect_aliasmem
, &state
);
619 static NTSTATUS
del_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
629 if (db
->transaction_start(db
) != 0) {
630 DEBUG(0, ("transaction_start failed\n"));
631 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
634 status
= alias_memberships(member
, 1, &sids
, &num
);
636 if (!NT_STATUS_IS_OK(status
)) {
640 for (i
=0; i
<num
; i
++) {
641 if (sid_compare(&sids
[i
], alias
) == 0) {
649 status
= NT_STATUS_MEMBER_NOT_IN_ALIAS
;
654 sids
[i
] = sids
[num
-1];
658 sid_to_fstring(sid_string
, member
);
660 key
= talloc_asprintf(sids
, "%s%s", MEMBEROF_PREFIX
, sid_string
);
663 status
= NT_STATUS_NO_MEMORY
;
668 status
= dbwrap_delete_bystring(db
, key
);
673 member_string
= talloc_strdup(sids
, "");
674 if (member_string
== NULL
) {
676 status
= NT_STATUS_NO_MEMORY
;
680 for (i
=0; i
<num
; i
++) {
682 sid_to_fstring(sid_string
, &sids
[i
]);
684 member_string
= talloc_asprintf_append_buffer(
685 member_string
, " %s", sid_string
);
687 if (member_string
== NULL
) {
689 status
= NT_STATUS_NO_MEMORY
;
694 status
= dbwrap_store_bystring(
695 db
, key
, string_term_tdb_data(member_string
), 0);
699 if (!NT_STATUS_IS_OK(status
)) {
700 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
705 if (db
->transaction_commit(db
) != 0) {
706 DEBUG(0, ("transaction_commit failed\n"));
707 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
714 if (db
->transaction_cancel(db
) != 0) {
715 smb_panic("transaction_cancel failed");
720 static const struct mapping_backend tdb_backend
= {
721 .add_mapping_entry
= add_mapping_entry
,
722 .get_group_map_from_sid
= get_group_map_from_sid
,
723 .get_group_map_from_gid
= get_group_map_from_gid
,
724 .get_group_map_from_ntname
= get_group_map_from_ntname
,
725 .group_map_remove
= group_map_remove
,
726 .enum_group_mapping
= enum_group_mapping
,
727 .one_alias_membership
= one_alias_membership
,
728 .add_aliasmem
= add_aliasmem
,
729 .del_aliasmem
= del_aliasmem
,
730 .enum_aliasmem
= enum_aliasmem
734 initialise the tdb mapping backend
736 const struct mapping_backend
*groupdb_tdb_init(void)
738 if (!init_group_mapping()) {
739 DEBUG(0,("Failed to initialise tdb mapping backend\n"));