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 "system/filesys.h"
26 #include "groupdb/mapping.h"
28 #include "../libcli/security/security.h"
30 static struct db_context
*db
; /* used for driver files */
32 static bool enum_group_mapping(const struct dom_sid
*domsid
,
33 enum lsa_SidType sid_name_use
,
35 size_t *p_num_entries
,
37 static bool group_map_remove(const struct dom_sid
*sid
);
39 static bool mapping_switch(const char *ldb_path
);
41 /****************************************************************************
42 Open the group mapping tdb.
43 ****************************************************************************/
44 static bool init_group_mapping(void)
52 db
= db_open(NULL
, state_path("group_mapping.tdb"), 0,
53 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
55 DEBUG(0, ("Failed to open group mapping database: %s\n",
60 ldb_path
= state_path("group_mapping.ldb");
61 if (file_exist(ldb_path
) && !mapping_switch(ldb_path
)) {
62 unlink(state_path("group_mapping.tdb"));
66 /* handle upgrade from old versions of the database */
67 #if 0 /* -- Needs conversion to dbwrap -- */
68 const char *vstring
= "INFO/version";
70 GROUP_MAP
*map_table
= NULL
;
71 size_t num_entries
= 0;
73 /* handle a Samba upgrade */
74 tdb_lock_bystring(tdb
, vstring
);
76 /* Cope with byte-reversed older versions of the db. */
77 vers_id
= tdb_fetch_int32(tdb
, vstring
);
78 if ((vers_id
== DATABASE_VERSION_V1
)
79 || (IREV(vers_id
) == DATABASE_VERSION_V1
)) {
81 * Written on a bigendian machine with old fetch_int
84 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
85 vers_id
= DATABASE_VERSION_V2
;
88 /* if its an unknown version we remove everthing in the db */
90 if (vers_id
!= DATABASE_VERSION_V2
) {
92 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
95 tdb_unlock_bystring(tdb
, vstring
);
97 /* cleanup any map entries with a gid == -1 */
99 if ( enum_group_mapping( NULL
, SID_NAME_UNKNOWN
, &map_table
,
100 &num_entries
, False
) ) {
103 for ( i
=0; i
<num_entries
; i
++ ) {
104 if ( map_table
[i
].gid
== -1 ) {
105 group_map_remove( &map_table
[i
].sid
);
109 SAFE_FREE( map_table
);
116 static char *group_mapping_key(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
)
118 char *sidstr
, *result
;
120 sidstr
= sid_string_talloc(talloc_tos(), sid
);
121 if (sidstr
== NULL
) {
125 result
= talloc_asprintf(mem_ctx
, "%s%s", GROUP_PREFIX
, sidstr
);
131 /****************************************************************************
132 ****************************************************************************/
133 static bool add_mapping_entry(GROUP_MAP
*map
, int flag
)
139 key
= group_mapping_key(talloc_tos(), &map
->sid
);
144 len
= tdb_pack(NULL
, 0, "ddff",
145 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
);
147 buf
= TALLOC_ARRAY(key
, char, len
);
152 len
= tdb_pack((uint8
*)buf
, len
, "ddff", map
->gid
,
153 map
->sid_name_use
, map
->nt_name
, map
->comment
);
155 status
= dbwrap_trans_store(
156 db
, string_term_tdb_data(key
),
157 make_tdb_data((uint8_t *)buf
, len
), TDB_REPLACE
);
161 return NT_STATUS_IS_OK(status
);
165 /****************************************************************************
166 Return the sid and the type of the unix group.
167 ****************************************************************************/
169 static bool get_group_map_from_sid(struct dom_sid sid
, GROUP_MAP
*map
)
175 /* the key is the SID, retrieving is direct */
177 key
= group_mapping_key(talloc_tos(), &sid
);
182 dbuf
= dbwrap_fetch_bystring(db
, key
, key
);
183 if (dbuf
.dptr
== NULL
) {
188 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
189 &map
->gid
, &map
->sid_name_use
,
190 &map
->nt_name
, &map
->comment
);
195 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
199 sid_copy(&map
->sid
, &sid
);
204 static bool dbrec2map(const struct db_record
*rec
, GROUP_MAP
*map
)
206 if ((rec
->key
.dsize
< strlen(GROUP_PREFIX
))
207 || (strncmp((char *)rec
->key
.dptr
, GROUP_PREFIX
,
208 GROUP_PREFIX_LEN
) != 0)) {
212 if (!string_to_sid(&map
->sid
, (const char *)rec
->key
.dptr
213 + GROUP_PREFIX_LEN
)) {
217 return tdb_unpack(rec
->value
.dptr
, rec
->value
.dsize
, "ddff",
218 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
,
219 &map
->comment
) != -1;
222 struct find_map_state
{
224 const char *name
; /* If != NULL, look for name */
225 gid_t gid
; /* valid iff name == NULL */
229 static int find_map(struct db_record
*rec
, void *private_data
)
231 struct find_map_state
*state
= (struct find_map_state
*)private_data
;
233 if (!dbrec2map(rec
, state
->map
)) {
234 DEBUG(10, ("failed to unpack map\n"));
238 if (state
->name
!= NULL
) {
239 if (strequal(state
->name
, state
->map
->nt_name
)) {
245 if (state
->map
->gid
== state
->gid
) {
254 /****************************************************************************
255 Return the sid and the type of the unix group.
256 ****************************************************************************/
258 static bool get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
260 struct find_map_state state
;
263 state
.name
= NULL
; /* Indicate we're looking for gid */
267 db
->traverse_read(db
, find_map
, (void *)&state
);
272 /****************************************************************************
273 Return the sid and the type of the unix group.
274 ****************************************************************************/
276 static bool get_group_map_from_ntname(const char *name
, GROUP_MAP
*map
)
278 struct find_map_state state
;
284 db
->traverse_read(db
, find_map
, (void *)&state
);
289 /****************************************************************************
290 Remove a group mapping entry.
291 ****************************************************************************/
293 static bool group_map_remove(const struct dom_sid
*sid
)
298 key
= group_mapping_key(talloc_tos(), sid
);
303 status
= dbwrap_trans_delete(db
, string_term_tdb_data(key
));
306 return NT_STATUS_IS_OK(status
);
309 /****************************************************************************
310 Enumerate the group mapping.
311 ****************************************************************************/
313 struct enum_map_state
{
314 const struct dom_sid
*domsid
;
315 enum lsa_SidType sid_name_use
;
322 static int collect_map(struct db_record
*rec
, void *private_data
)
324 struct enum_map_state
*state
= (struct enum_map_state
*)private_data
;
328 if (!dbrec2map(rec
, &map
)) {
331 /* list only the type or everything if UNKNOWN */
332 if (state
->sid_name_use
!= SID_NAME_UNKNOWN
333 && state
->sid_name_use
!= map
.sid_name_use
) {
334 DEBUG(11,("enum_group_mapping: group %s is not of the "
335 "requested type\n", map
.nt_name
));
339 if ((state
->unix_only
== ENUM_ONLY_MAPPED
) && (map
.gid
== -1)) {
340 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
345 if ((state
->domsid
!= NULL
) &&
346 (dom_sid_compare_domain(state
->domsid
, &map
.sid
) != 0)) {
347 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
348 sid_string_dbg(&map
.sid
)));
352 if (!(tmp
= SMB_REALLOC_ARRAY(state
->maps
, GROUP_MAP
,
353 state
->num_maps
+1))) {
354 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
360 state
->maps
[state
->num_maps
] = map
;
365 static bool enum_group_mapping(const struct dom_sid
*domsid
,
366 enum lsa_SidType sid_name_use
,
368 size_t *p_num_entries
, bool unix_only
)
370 struct enum_map_state state
;
372 state
.domsid
= domsid
;
373 state
.sid_name_use
= sid_name_use
;
374 state
.unix_only
= unix_only
;
378 if (db
->traverse_read(db
, collect_map
, (void *)&state
) < 0) {
382 *pp_rmap
= state
.maps
;
383 *p_num_entries
= state
.num_maps
;
388 /* This operation happens on session setup, so it should better be fast. We
389 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
391 static NTSTATUS
one_alias_membership(const struct dom_sid
*member
,
392 struct dom_sid
**sids
, size_t *num
)
399 NTSTATUS status
= NT_STATUS_OK
;
400 TALLOC_CTX
*frame
= talloc_stackframe();
402 slprintf(key
, sizeof(key
), "%s%s", MEMBEROF_PREFIX
,
403 sid_to_fstring(tmp
, member
));
405 dbuf
= dbwrap_fetch_bystring(db
, frame
, key
);
406 if (dbuf
.dptr
== NULL
) {
411 p
= (const char *)dbuf
.dptr
;
413 while (next_token_talloc(frame
, &p
, &string_sid
, " ")) {
414 struct dom_sid alias
;
417 if (!string_to_sid(&alias
, string_sid
))
421 status
= add_sid_to_array_unique(NULL
, &alias
, sids
, &num_sids
);
422 if (!NT_STATUS_IS_OK(status
)) {
433 static NTSTATUS
alias_memberships(const struct dom_sid
*members
, size_t num_members
,
434 struct dom_sid
**sids
, size_t *num
)
441 for (i
=0; i
<num_members
; i
++) {
442 NTSTATUS status
= one_alias_membership(&members
[i
], sids
, num
);
443 if (!NT_STATUS_IS_OK(status
))
449 static bool is_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
451 struct dom_sid
*sids
;
455 /* This feels the wrong way round, but the on-disk data structure
456 * dictates it this way. */
457 if (!NT_STATUS_IS_OK(alias_memberships(member
, 1, &sids
, &num
)))
460 for (i
=0; i
<num
; i
++) {
461 if (dom_sid_compare(alias
, &sids
[i
]) == 0) {
471 static NTSTATUS
add_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
476 char *new_memberstring
;
477 struct db_record
*rec
;
480 if (!get_group_map_from_sid(*alias
, &map
))
481 return NT_STATUS_NO_SUCH_ALIAS
;
483 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
484 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
485 return NT_STATUS_NO_SUCH_ALIAS
;
487 if (is_aliasmem(alias
, member
))
488 return NT_STATUS_MEMBER_IN_ALIAS
;
490 sid_to_fstring(string_sid
, member
);
492 key
= talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX
,
495 return NT_STATUS_NO_MEMORY
;
498 if (db
->transaction_start(db
) != 0) {
499 DEBUG(0, ("transaction_start failed\n"));
500 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
503 rec
= db
->fetch_locked(db
, key
, string_term_tdb_data(key
));
506 DEBUG(10, ("fetch_lock failed\n"));
508 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
512 sid_to_fstring(string_sid
, alias
);
514 if (rec
->value
.dptr
!= NULL
) {
515 new_memberstring
= talloc_asprintf(
516 key
, "%s %s", (char *)(rec
->value
.dptr
), string_sid
);
518 new_memberstring
= talloc_strdup(key
, string_sid
);
521 if (new_memberstring
== NULL
) {
523 status
= NT_STATUS_NO_MEMORY
;
527 status
= rec
->store(rec
, string_term_tdb_data(new_memberstring
), 0);
531 if (!NT_STATUS_IS_OK(status
)) {
532 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status
)));
536 if (db
->transaction_commit(db
) != 0) {
537 DEBUG(0, ("transaction_commit failed\n"));
538 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
545 if (db
->transaction_cancel(db
) != 0) {
546 smb_panic("transaction_cancel failed");
552 struct aliasmem_state
{
554 const struct dom_sid
*alias
;
555 struct dom_sid
**sids
;
559 static int collect_aliasmem(struct db_record
*rec
, void *priv
)
561 struct aliasmem_state
*state
= (struct aliasmem_state
*)priv
;
566 if (strncmp((const char *)rec
->key
.dptr
, MEMBEROF_PREFIX
,
567 MEMBEROF_PREFIX_LEN
) != 0)
570 p
= (const char *)rec
->value
.dptr
;
572 frame
= talloc_stackframe();
574 while (next_token_talloc(frame
, &p
, &alias_string
, " ")) {
575 struct dom_sid alias
, member
;
576 const char *member_string
;
579 if (!string_to_sid(&alias
, alias_string
))
582 if (dom_sid_compare(state
->alias
, &alias
) != 0)
585 /* Ok, we found the alias we're looking for in the membership
586 * list currently scanned. The key represents the alias
587 * member. Add that. */
589 member_string
= strchr((const char *)rec
->key
.dptr
, '/');
591 /* Above we tested for MEMBEROF_PREFIX which includes the
594 SMB_ASSERT(member_string
!= NULL
);
597 if (!string_to_sid(&member
, member_string
))
600 num_sids
= *state
->num
;
601 if (!NT_STATUS_IS_OK(add_sid_to_array(state
->mem_ctx
, &member
,
608 *state
->num
= num_sids
;
615 static NTSTATUS
enum_aliasmem(const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
616 struct dom_sid
**sids
, size_t *num
)
619 struct aliasmem_state state
;
621 if (!get_group_map_from_sid(*alias
, &map
))
622 return NT_STATUS_NO_SUCH_ALIAS
;
624 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
625 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
626 return NT_STATUS_NO_SUCH_ALIAS
;
634 state
.mem_ctx
= mem_ctx
;
636 db
->traverse_read(db
, collect_aliasmem
, &state
);
640 static NTSTATUS
del_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
643 struct dom_sid
*sids
;
650 if (db
->transaction_start(db
) != 0) {
651 DEBUG(0, ("transaction_start failed\n"));
652 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
655 status
= alias_memberships(member
, 1, &sids
, &num
);
657 if (!NT_STATUS_IS_OK(status
)) {
661 for (i
=0; i
<num
; i
++) {
662 if (dom_sid_compare(&sids
[i
], alias
) == 0) {
670 status
= NT_STATUS_MEMBER_NOT_IN_ALIAS
;
675 sids
[i
] = sids
[num
-1];
679 sid_to_fstring(sid_string
, member
);
681 key
= talloc_asprintf(sids
, "%s%s", MEMBEROF_PREFIX
, sid_string
);
684 status
= NT_STATUS_NO_MEMORY
;
689 status
= dbwrap_delete_bystring(db
, key
);
693 member_string
= talloc_strdup(sids
, "");
694 if (member_string
== NULL
) {
696 status
= NT_STATUS_NO_MEMORY
;
700 for (i
=0; i
<num
; i
++) {
702 sid_to_fstring(sid_string
, &sids
[i
]);
704 member_string
= talloc_asprintf_append_buffer(
705 member_string
, " %s", sid_string
);
707 if (member_string
== NULL
) {
709 status
= NT_STATUS_NO_MEMORY
;
714 status
= dbwrap_store_bystring(
715 db
, key
, string_term_tdb_data(member_string
), 0);
719 if (!NT_STATUS_IS_OK(status
)) {
720 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
725 if (db
->transaction_commit(db
) != 0) {
726 DEBUG(0, ("transaction_commit failed\n"));
727 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
734 if (db
->transaction_cancel(db
) != 0) {
735 smb_panic("transaction_cancel failed");
741 /* -- ldb->tdb switching code -------------------------------------------- */
743 /* change this if the data format ever changes */
744 #define LTDB_PACKING_FORMAT 0x26011967
746 /* old packing formats (not supported for now,
747 * it was never used for group mapping AFAIK) */
748 #define LTDB_PACKING_FORMAT_NODN 0x26011966
750 static unsigned int pull_uint32(uint8_t *p
, int ofs
)
753 return p
[0] | (p
[1]<<8) | (p
[2]<<16) | (p
[3]<<24);
757 unpack a ldb message from a linear buffer in TDB_DATA
759 static int convert_ldb_record(TDB_CONTEXT
*ltdb
, TDB_DATA key
,
760 TDB_DATA data
, void *ptr
)
762 TALLOC_CTX
*tmp_ctx
= talloc_tos();
767 unsigned int remaining
;
773 uint32_t num_mem
= 0;
774 struct dom_sid
*members
= NULL
;
776 p
= (uint8_t *)data
.dptr
;
777 if (data
.dsize
< 8) {
782 format
= pull_uint32(p
, 0);
783 num_el
= pull_uint32(p
, 4);
786 remaining
= data
.dsize
- 8;
789 case LTDB_PACKING_FORMAT
:
790 len
= strnlen((char *)p
, remaining
);
791 if (len
== remaining
) {
797 /* ignore special LDB attributes */
801 if (strncmp((char *)p
, "rid=", 4)) {
802 /* unknown entry, ignore */
803 DEBUG(3, ("Found unknown entry in group mapping "
804 "database named [%s]\n", (char *)p
));
808 remaining
-= len
+ 1;
812 case LTDB_PACKING_FORMAT_NODN
:
819 /* bad entry, ignore */
823 if (num_el
> remaining
/ 6) {
830 for (i
= 0; i
< num_el
; i
++) {
833 if (remaining
< 10) {
837 len
= strnlen((char *)p
, remaining
- 6);
838 if (len
== remaining
- 6) {
842 name
= talloc_strndup(tmp_ctx
, (char *)p
, len
);
847 remaining
-= len
+ 1;
850 num_vals
= pull_uint32(p
, 0);
851 if (StrCaseCmp(name
, "member") == 0) {
853 members
= talloc_array(tmp_ctx
, struct dom_sid
, num_mem
);
854 if (members
== NULL
) {
858 } else if (num_vals
!= 1) {
866 for (j
= 0; j
< num_vals
; j
++) {
867 len
= pull_uint32(p
, 0);
868 if (len
> remaining
-5) {
873 val
= talloc_strndup(tmp_ctx
, (char *)(p
+ 4), len
);
879 remaining
-= len
+4+1;
882 /* we ignore unknown or uninteresting attributes
883 * (objectclass, etc.) */
884 if (StrCaseCmp(name
, "gidNumber") == 0) {
885 map
.gid
= strtoul(val
, &q
, 10);
890 } else if (StrCaseCmp(name
, "sid") == 0) {
891 if (!string_to_sid(&map
.sid
, val
)) {
895 } else if (StrCaseCmp(name
, "sidNameUse") == 0) {
896 map
.sid_name_use
= strtoul(val
, &q
, 10);
901 } else if (StrCaseCmp(name
, "ntname") == 0) {
902 strlcpy(map
.nt_name
, val
,
903 sizeof(map
.nt_name
));
904 } else if (StrCaseCmp(name
, "comment") == 0) {
905 strlcpy(map
.comment
, val
,
906 sizeof(map
.comment
));
907 } else if (StrCaseCmp(name
, "member") == 0) {
908 if (!string_to_sid(&members
[j
], val
)) {
920 if (!add_mapping_entry(&map
, 0)) {
926 for (j
= 0; j
< num_mem
; j
++) {
928 status
= add_aliasmem(&map
.sid
, &members
[j
]);
929 if (!NT_STATUS_IS_OK(status
)) {
936 if (remaining
!= 0) {
937 DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
947 static bool mapping_switch(const char *ldb_path
)
954 frame
= talloc_stackframe();
956 ltdb
= tdb_open_log(ldb_path
, 0, TDB_DEFAULT
, O_RDONLY
, 0600);
957 if (ltdb
== NULL
) goto failed
;
959 /* ldb is just a very fancy tdb, read out raw data and perform
961 ret
= tdb_traverse(ltdb
, convert_ldb_record
, NULL
);
962 if (ret
== -1) goto failed
;
969 /* now rename the old db out of the way */
970 new_path
= state_path("group_mapping.ldb.replaced");
974 if (rename(ldb_path
, new_path
) != 0) {
975 DEBUG(0,("Failed to rename old group mapping database\n"));
982 DEBUG(0, ("Failed to switch to tdb group mapping database\n"));
983 if (ltdb
) tdb_close(ltdb
);
988 static const struct mapping_backend tdb_backend
= {
989 .add_mapping_entry
= add_mapping_entry
,
990 .get_group_map_from_sid
= get_group_map_from_sid
,
991 .get_group_map_from_gid
= get_group_map_from_gid
,
992 .get_group_map_from_ntname
= get_group_map_from_ntname
,
993 .group_map_remove
= group_map_remove
,
994 .enum_group_mapping
= enum_group_mapping
,
995 .one_alias_membership
= one_alias_membership
,
996 .add_aliasmem
= add_aliasmem
,
997 .del_aliasmem
= del_aliasmem
,
998 .enum_aliasmem
= enum_aliasmem
1002 initialise the tdb mapping backend
1004 const struct mapping_backend
*groupdb_tdb_init(void)
1006 if (!init_group_mapping()) {
1007 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
1011 return &tdb_backend
;