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 #include "../libcli/security/dom_sid.h"
28 static struct db_context
*db
; /* used for driver files */
30 static bool enum_group_mapping(const struct dom_sid
*domsid
,
31 enum lsa_SidType sid_name_use
,
33 size_t *p_num_entries
,
35 static bool group_map_remove(const struct dom_sid
*sid
);
37 static bool mapping_switch(const char *ldb_path
);
39 /****************************************************************************
40 Open the group mapping tdb.
41 ****************************************************************************/
42 static bool init_group_mapping(void)
50 db
= db_open(NULL
, state_path("group_mapping.tdb"), 0,
51 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
53 DEBUG(0, ("Failed to open group mapping database: %s\n",
58 ldb_path
= state_path("group_mapping.ldb");
59 if (file_exist(ldb_path
) && !mapping_switch(ldb_path
)) {
60 unlink(state_path("group_mapping.tdb"));
64 /* handle upgrade from old versions of the database */
65 #if 0 /* -- Needs conversion to dbwrap -- */
66 const char *vstring
= "INFO/version";
68 GROUP_MAP
*map_table
= NULL
;
69 size_t num_entries
= 0;
71 /* handle a Samba upgrade */
72 tdb_lock_bystring(tdb
, vstring
);
74 /* Cope with byte-reversed older versions of the db. */
75 vers_id
= tdb_fetch_int32(tdb
, vstring
);
76 if ((vers_id
== DATABASE_VERSION_V1
)
77 || (IREV(vers_id
) == DATABASE_VERSION_V1
)) {
79 * Written on a bigendian machine with old fetch_int
82 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
83 vers_id
= DATABASE_VERSION_V2
;
86 /* if its an unknown version we remove everthing in the db */
88 if (vers_id
!= DATABASE_VERSION_V2
) {
90 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
93 tdb_unlock_bystring(tdb
, vstring
);
95 /* cleanup any map entries with a gid == -1 */
97 if ( enum_group_mapping( NULL
, SID_NAME_UNKNOWN
, &map_table
,
98 &num_entries
, False
) ) {
101 for ( i
=0; i
<num_entries
; i
++ ) {
102 if ( map_table
[i
].gid
== -1 ) {
103 group_map_remove( &map_table
[i
].sid
);
107 SAFE_FREE( map_table
);
114 static char *group_mapping_key(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
)
116 char *sidstr
, *result
;
118 sidstr
= sid_string_talloc(talloc_tos(), sid
);
119 if (sidstr
== NULL
) {
123 result
= talloc_asprintf(mem_ctx
, "%s%s", GROUP_PREFIX
, sidstr
);
129 /****************************************************************************
130 ****************************************************************************/
131 static bool add_mapping_entry(GROUP_MAP
*map
, int flag
)
137 key
= group_mapping_key(talloc_tos(), &map
->sid
);
142 len
= tdb_pack(NULL
, 0, "ddff",
143 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
);
145 buf
= TALLOC_ARRAY(key
, char, len
);
150 len
= tdb_pack((uint8
*)buf
, len
, "ddff", map
->gid
,
151 map
->sid_name_use
, map
->nt_name
, map
->comment
);
153 status
= dbwrap_trans_store(
154 db
, string_term_tdb_data(key
),
155 make_tdb_data((uint8_t *)buf
, len
), TDB_REPLACE
);
159 return NT_STATUS_IS_OK(status
);
163 /****************************************************************************
164 Return the sid and the type of the unix group.
165 ****************************************************************************/
167 static bool get_group_map_from_sid(struct dom_sid sid
, GROUP_MAP
*map
)
173 /* the key is the SID, retrieving is direct */
175 key
= group_mapping_key(talloc_tos(), &sid
);
180 dbuf
= dbwrap_fetch_bystring(db
, key
, key
);
181 if (dbuf
.dptr
== NULL
) {
186 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
187 &map
->gid
, &map
->sid_name_use
,
188 &map
->nt_name
, &map
->comment
);
193 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
197 sid_copy(&map
->sid
, &sid
);
202 static bool dbrec2map(const struct db_record
*rec
, GROUP_MAP
*map
)
204 if ((rec
->key
.dsize
< strlen(GROUP_PREFIX
))
205 || (strncmp((char *)rec
->key
.dptr
, GROUP_PREFIX
,
206 GROUP_PREFIX_LEN
) != 0)) {
210 if (!string_to_sid(&map
->sid
, (const char *)rec
->key
.dptr
211 + GROUP_PREFIX_LEN
)) {
215 return tdb_unpack(rec
->value
.dptr
, rec
->value
.dsize
, "ddff",
216 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
,
217 &map
->comment
) != -1;
220 struct find_map_state
{
222 const char *name
; /* If != NULL, look for name */
223 gid_t gid
; /* valid iff name == NULL */
227 static int find_map(struct db_record
*rec
, void *private_data
)
229 struct find_map_state
*state
= (struct find_map_state
*)private_data
;
231 if (!dbrec2map(rec
, state
->map
)) {
232 DEBUG(10, ("failed to unpack map\n"));
236 if (state
->name
!= NULL
) {
237 if (strequal(state
->name
, state
->map
->nt_name
)) {
243 if (state
->map
->gid
== state
->gid
) {
252 /****************************************************************************
253 Return the sid and the type of the unix group.
254 ****************************************************************************/
256 static bool get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
258 struct find_map_state state
;
261 state
.name
= NULL
; /* Indicate we're looking for gid */
265 db
->traverse_read(db
, find_map
, (void *)&state
);
270 /****************************************************************************
271 Return the sid and the type of the unix group.
272 ****************************************************************************/
274 static bool get_group_map_from_ntname(const char *name
, GROUP_MAP
*map
)
276 struct find_map_state state
;
282 db
->traverse_read(db
, find_map
, (void *)&state
);
287 /****************************************************************************
288 Remove a group mapping entry.
289 ****************************************************************************/
291 static bool group_map_remove(const struct dom_sid
*sid
)
296 key
= group_mapping_key(talloc_tos(), sid
);
301 status
= dbwrap_trans_delete(db
, string_term_tdb_data(key
));
304 return NT_STATUS_IS_OK(status
);
307 /****************************************************************************
308 Enumerate the group mapping.
309 ****************************************************************************/
311 struct enum_map_state
{
312 const struct dom_sid
*domsid
;
313 enum lsa_SidType sid_name_use
;
320 static int collect_map(struct db_record
*rec
, void *private_data
)
322 struct enum_map_state
*state
= (struct enum_map_state
*)private_data
;
326 if (!dbrec2map(rec
, &map
)) {
329 /* list only the type or everything if UNKNOWN */
330 if (state
->sid_name_use
!= SID_NAME_UNKNOWN
331 && state
->sid_name_use
!= map
.sid_name_use
) {
332 DEBUG(11,("enum_group_mapping: group %s is not of the "
333 "requested type\n", map
.nt_name
));
337 if ((state
->unix_only
== ENUM_ONLY_MAPPED
) && (map
.gid
== -1)) {
338 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
343 if ((state
->domsid
!= NULL
) &&
344 (dom_sid_compare_domain(state
->domsid
, &map
.sid
) != 0)) {
345 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
346 sid_string_dbg(&map
.sid
)));
350 if (!(tmp
= SMB_REALLOC_ARRAY(state
->maps
, GROUP_MAP
,
351 state
->num_maps
+1))) {
352 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
358 state
->maps
[state
->num_maps
] = map
;
363 static bool enum_group_mapping(const struct dom_sid
*domsid
,
364 enum lsa_SidType sid_name_use
,
366 size_t *p_num_entries
, bool unix_only
)
368 struct enum_map_state state
;
370 state
.domsid
= domsid
;
371 state
.sid_name_use
= sid_name_use
;
372 state
.unix_only
= unix_only
;
376 if (db
->traverse_read(db
, collect_map
, (void *)&state
) < 0) {
380 *pp_rmap
= state
.maps
;
381 *p_num_entries
= state
.num_maps
;
386 /* This operation happens on session setup, so it should better be fast. We
387 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
389 static NTSTATUS
one_alias_membership(const struct dom_sid
*member
,
390 struct dom_sid
**sids
, size_t *num
)
397 NTSTATUS status
= NT_STATUS_OK
;
398 TALLOC_CTX
*frame
= talloc_stackframe();
400 slprintf(key
, sizeof(key
), "%s%s", MEMBEROF_PREFIX
,
401 sid_to_fstring(tmp
, member
));
403 dbuf
= dbwrap_fetch_bystring(db
, frame
, key
);
404 if (dbuf
.dptr
== NULL
) {
409 p
= (const char *)dbuf
.dptr
;
411 while (next_token_talloc(frame
, &p
, &string_sid
, " ")) {
412 struct dom_sid alias
;
415 if (!string_to_sid(&alias
, string_sid
))
419 status
= add_sid_to_array_unique(NULL
, &alias
, sids
, &num_sids
);
420 if (!NT_STATUS_IS_OK(status
)) {
431 static NTSTATUS
alias_memberships(const struct dom_sid
*members
, size_t num_members
,
432 struct dom_sid
**sids
, size_t *num
)
439 for (i
=0; i
<num_members
; i
++) {
440 NTSTATUS status
= one_alias_membership(&members
[i
], sids
, num
);
441 if (!NT_STATUS_IS_OK(status
))
447 static bool is_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
449 struct dom_sid
*sids
;
453 /* This feels the wrong way round, but the on-disk data structure
454 * dictates it this way. */
455 if (!NT_STATUS_IS_OK(alias_memberships(member
, 1, &sids
, &num
)))
458 for (i
=0; i
<num
; i
++) {
459 if (dom_sid_compare(alias
, &sids
[i
]) == 0) {
469 static NTSTATUS
add_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
474 char *new_memberstring
;
475 struct db_record
*rec
;
478 if (!get_group_map_from_sid(*alias
, &map
))
479 return NT_STATUS_NO_SUCH_ALIAS
;
481 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
482 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
483 return NT_STATUS_NO_SUCH_ALIAS
;
485 if (is_aliasmem(alias
, member
))
486 return NT_STATUS_MEMBER_IN_ALIAS
;
488 sid_to_fstring(string_sid
, member
);
490 key
= talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX
,
493 return NT_STATUS_NO_MEMORY
;
496 if (db
->transaction_start(db
) != 0) {
497 DEBUG(0, ("transaction_start failed\n"));
498 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
501 rec
= db
->fetch_locked(db
, key
, string_term_tdb_data(key
));
504 DEBUG(10, ("fetch_lock failed\n"));
506 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
510 sid_to_fstring(string_sid
, alias
);
512 if (rec
->value
.dptr
!= NULL
) {
513 new_memberstring
= talloc_asprintf(
514 key
, "%s %s", (char *)(rec
->value
.dptr
), string_sid
);
516 new_memberstring
= talloc_strdup(key
, string_sid
);
519 if (new_memberstring
== NULL
) {
521 status
= NT_STATUS_NO_MEMORY
;
525 status
= rec
->store(rec
, string_term_tdb_data(new_memberstring
), 0);
529 if (!NT_STATUS_IS_OK(status
)) {
530 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status
)));
534 if (db
->transaction_commit(db
) != 0) {
535 DEBUG(0, ("transaction_commit failed\n"));
536 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
543 if (db
->transaction_cancel(db
) != 0) {
544 smb_panic("transaction_cancel failed");
550 struct aliasmem_state
{
552 const struct dom_sid
*alias
;
553 struct dom_sid
**sids
;
557 static int collect_aliasmem(struct db_record
*rec
, void *priv
)
559 struct aliasmem_state
*state
= (struct aliasmem_state
*)priv
;
564 if (strncmp((const char *)rec
->key
.dptr
, MEMBEROF_PREFIX
,
565 MEMBEROF_PREFIX_LEN
) != 0)
568 p
= (const char *)rec
->value
.dptr
;
570 frame
= talloc_stackframe();
572 while (next_token_talloc(frame
, &p
, &alias_string
, " ")) {
573 struct dom_sid alias
, member
;
574 const char *member_string
;
577 if (!string_to_sid(&alias
, alias_string
))
580 if (dom_sid_compare(state
->alias
, &alias
) != 0)
583 /* Ok, we found the alias we're looking for in the membership
584 * list currently scanned. The key represents the alias
585 * member. Add that. */
587 member_string
= strchr((const char *)rec
->key
.dptr
, '/');
589 /* Above we tested for MEMBEROF_PREFIX which includes the
592 SMB_ASSERT(member_string
!= NULL
);
595 if (!string_to_sid(&member
, member_string
))
598 num_sids
= *state
->num
;
599 if (!NT_STATUS_IS_OK(add_sid_to_array(state
->mem_ctx
, &member
,
606 *state
->num
= num_sids
;
613 static NTSTATUS
enum_aliasmem(const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
614 struct dom_sid
**sids
, size_t *num
)
617 struct aliasmem_state state
;
619 if (!get_group_map_from_sid(*alias
, &map
))
620 return NT_STATUS_NO_SUCH_ALIAS
;
622 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
623 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
624 return NT_STATUS_NO_SUCH_ALIAS
;
632 state
.mem_ctx
= mem_ctx
;
634 db
->traverse_read(db
, collect_aliasmem
, &state
);
638 static NTSTATUS
del_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
641 struct dom_sid
*sids
;
648 if (db
->transaction_start(db
) != 0) {
649 DEBUG(0, ("transaction_start failed\n"));
650 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
653 status
= alias_memberships(member
, 1, &sids
, &num
);
655 if (!NT_STATUS_IS_OK(status
)) {
659 for (i
=0; i
<num
; i
++) {
660 if (dom_sid_compare(&sids
[i
], alias
) == 0) {
668 status
= NT_STATUS_MEMBER_NOT_IN_ALIAS
;
673 sids
[i
] = sids
[num
-1];
677 sid_to_fstring(sid_string
, member
);
679 key
= talloc_asprintf(sids
, "%s%s", MEMBEROF_PREFIX
, sid_string
);
682 status
= NT_STATUS_NO_MEMORY
;
687 status
= dbwrap_delete_bystring(db
, key
);
691 member_string
= talloc_strdup(sids
, "");
692 if (member_string
== NULL
) {
694 status
= NT_STATUS_NO_MEMORY
;
698 for (i
=0; i
<num
; i
++) {
700 sid_to_fstring(sid_string
, &sids
[i
]);
702 member_string
= talloc_asprintf_append_buffer(
703 member_string
, " %s", sid_string
);
705 if (member_string
== NULL
) {
707 status
= NT_STATUS_NO_MEMORY
;
712 status
= dbwrap_store_bystring(
713 db
, key
, string_term_tdb_data(member_string
), 0);
717 if (!NT_STATUS_IS_OK(status
)) {
718 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
723 if (db
->transaction_commit(db
) != 0) {
724 DEBUG(0, ("transaction_commit failed\n"));
725 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
732 if (db
->transaction_cancel(db
) != 0) {
733 smb_panic("transaction_cancel failed");
739 /* -- ldb->tdb switching code -------------------------------------------- */
741 /* change this if the data format ever changes */
742 #define LTDB_PACKING_FORMAT 0x26011967
744 /* old packing formats (not supported for now,
745 * it was never used for group mapping AFAIK) */
746 #define LTDB_PACKING_FORMAT_NODN 0x26011966
748 static unsigned int pull_uint32(uint8_t *p
, int ofs
)
751 return p
[0] | (p
[1]<<8) | (p
[2]<<16) | (p
[3]<<24);
755 unpack a ldb message from a linear buffer in TDB_DATA
757 static int convert_ldb_record(TDB_CONTEXT
*ltdb
, TDB_DATA key
,
758 TDB_DATA data
, void *ptr
)
760 TALLOC_CTX
*tmp_ctx
= talloc_tos();
765 unsigned int remaining
;
771 uint32_t num_mem
= 0;
772 struct dom_sid
*members
= NULL
;
774 p
= (uint8_t *)data
.dptr
;
775 if (data
.dsize
< 8) {
780 format
= pull_uint32(p
, 0);
781 num_el
= pull_uint32(p
, 4);
784 remaining
= data
.dsize
- 8;
787 case LTDB_PACKING_FORMAT
:
788 len
= strnlen((char *)p
, remaining
);
789 if (len
== remaining
) {
795 /* ignore special LDB attributes */
799 if (strncmp((char *)p
, "rid=", 4)) {
800 /* unknown entry, ignore */
801 DEBUG(3, ("Found unknown entry in group mapping "
802 "database named [%s]\n", (char *)p
));
806 remaining
-= len
+ 1;
810 case LTDB_PACKING_FORMAT_NODN
:
817 /* bad entry, ignore */
821 if (num_el
> remaining
/ 6) {
828 for (i
= 0; i
< num_el
; i
++) {
831 if (remaining
< 10) {
835 len
= strnlen((char *)p
, remaining
- 6);
836 if (len
== remaining
- 6) {
840 name
= talloc_strndup(tmp_ctx
, (char *)p
, len
);
845 remaining
-= len
+ 1;
848 num_vals
= pull_uint32(p
, 0);
849 if (StrCaseCmp(name
, "member") == 0) {
851 members
= talloc_array(tmp_ctx
, struct dom_sid
, num_mem
);
852 if (members
== NULL
) {
856 } else if (num_vals
!= 1) {
864 for (j
= 0; j
< num_vals
; j
++) {
865 len
= pull_uint32(p
, 0);
866 if (len
> remaining
-5) {
871 val
= talloc_strndup(tmp_ctx
, (char *)(p
+ 4), len
);
877 remaining
-= len
+4+1;
880 /* we ignore unknown or uninteresting attributes
881 * (objectclass, etc.) */
882 if (StrCaseCmp(name
, "gidNumber") == 0) {
883 map
.gid
= strtoul(val
, &q
, 10);
888 } else if (StrCaseCmp(name
, "sid") == 0) {
889 if (!string_to_sid(&map
.sid
, val
)) {
893 } else if (StrCaseCmp(name
, "sidNameUse") == 0) {
894 map
.sid_name_use
= strtoul(val
, &q
, 10);
899 } else if (StrCaseCmp(name
, "ntname") == 0) {
900 strlcpy(map
.nt_name
, val
,
901 sizeof(map
.nt_name
) -1);
902 } else if (StrCaseCmp(name
, "comment") == 0) {
903 strlcpy(map
.comment
, val
,
904 sizeof(map
.comment
) -1);
905 } else if (StrCaseCmp(name
, "member") == 0) {
906 if (!string_to_sid(&members
[j
], val
)) {
918 if (!add_mapping_entry(&map
, 0)) {
924 for (j
= 0; j
< num_mem
; j
++) {
926 status
= add_aliasmem(&map
.sid
, &members
[j
]);
927 if (!NT_STATUS_IS_OK(status
)) {
934 if (remaining
!= 0) {
935 DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
945 static bool mapping_switch(const char *ldb_path
)
952 frame
= talloc_stackframe();
954 ltdb
= tdb_open_log(ldb_path
, 0, TDB_DEFAULT
, O_RDONLY
, 0600);
955 if (ltdb
== NULL
) goto failed
;
957 /* ldb is just a very fancy tdb, read out raw data and perform
959 ret
= tdb_traverse(ltdb
, convert_ldb_record
, NULL
);
960 if (ret
== -1) goto failed
;
967 /* now rename the old db out of the way */
968 new_path
= state_path("group_mapping.ldb.replaced");
972 if (rename(ldb_path
, new_path
) != 0) {
973 DEBUG(0,("Failed to rename old group mapping database\n"));
980 DEBUG(0, ("Failed to switch to tdb group mapping database\n"));
981 if (ltdb
) tdb_close(ltdb
);
986 static const struct mapping_backend tdb_backend
= {
987 .add_mapping_entry
= add_mapping_entry
,
988 .get_group_map_from_sid
= get_group_map_from_sid
,
989 .get_group_map_from_gid
= get_group_map_from_gid
,
990 .get_group_map_from_ntname
= get_group_map_from_ntname
,
991 .group_map_remove
= group_map_remove
,
992 .enum_group_mapping
= enum_group_mapping
,
993 .one_alias_membership
= one_alias_membership
,
994 .add_aliasmem
= add_aliasmem
,
995 .del_aliasmem
= del_aliasmem
,
996 .enum_aliasmem
= enum_aliasmem
1000 initialise the tdb mapping backend
1002 const struct mapping_backend
*groupdb_tdb_init(void)
1004 if (!init_group_mapping()) {
1005 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
1009 return &tdb_backend
;