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"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_open.h"
30 #include "../libcli/security/security.h"
32 static struct db_context
*db
; /* used for driver files */
34 static bool enum_group_mapping(const struct dom_sid
*domsid
,
35 enum lsa_SidType sid_name_use
,
37 size_t *p_num_entries
,
39 static bool group_map_remove(const struct dom_sid
*sid
);
41 static bool mapping_switch(const char *ldb_path
);
43 /****************************************************************************
44 Open the group mapping tdb.
45 ****************************************************************************/
46 static bool init_group_mapping(void)
54 db
= db_open(NULL
, state_path("group_mapping.tdb"), 0,
55 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
57 DEBUG(0, ("Failed to open group mapping database: %s\n",
62 ldb_path
= state_path("group_mapping.ldb");
63 if (file_exist(ldb_path
) && !mapping_switch(ldb_path
)) {
64 unlink(state_path("group_mapping.tdb"));
68 /* handle upgrade from old versions of the database */
69 #if 0 /* -- Needs conversion to dbwrap -- */
70 const char *vstring
= "INFO/version";
72 GROUP_MAP
*map_table
= NULL
;
73 size_t num_entries
= 0;
75 /* handle a Samba upgrade */
76 tdb_lock_bystring(tdb
, vstring
);
78 /* Cope with byte-reversed older versions of the db. */
79 vers_id
= tdb_fetch_int32(tdb
, vstring
);
80 if ((vers_id
== DATABASE_VERSION_V1
)
81 || (IREV(vers_id
) == DATABASE_VERSION_V1
)) {
83 * Written on a bigendian machine with old fetch_int
86 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
87 vers_id
= DATABASE_VERSION_V2
;
90 /* if its an unknown version we remove everthing in the db */
92 if (vers_id
!= DATABASE_VERSION_V2
) {
94 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
97 tdb_unlock_bystring(tdb
, vstring
);
99 /* cleanup any map entries with a gid == -1 */
101 if ( enum_group_mapping( NULL
, SID_NAME_UNKNOWN
, &map_table
,
102 &num_entries
, False
) ) {
105 for ( i
=0; i
<num_entries
; i
++ ) {
106 if ( map_table
[i
].gid
== -1 ) {
107 group_map_remove( &map_table
[i
].sid
);
111 SAFE_FREE( map_table
);
118 static char *group_mapping_key(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
)
120 char *sidstr
, *result
;
122 sidstr
= sid_string_talloc(talloc_tos(), sid
);
123 if (sidstr
== NULL
) {
127 result
= talloc_asprintf(mem_ctx
, "%s%s", GROUP_PREFIX
, sidstr
);
133 /****************************************************************************
134 ****************************************************************************/
135 static bool add_mapping_entry(GROUP_MAP
*map
, int flag
)
141 key
= group_mapping_key(talloc_tos(), &map
->sid
);
146 len
= tdb_pack(NULL
, 0, "ddff",
147 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
);
149 buf
= talloc_array(key
, char, len
);
154 len
= tdb_pack((uint8
*)buf
, len
, "ddff", map
->gid
,
155 map
->sid_name_use
, map
->nt_name
, map
->comment
);
157 status
= dbwrap_trans_store(
158 db
, string_term_tdb_data(key
),
159 make_tdb_data((uint8_t *)buf
, len
), TDB_REPLACE
);
163 return NT_STATUS_IS_OK(status
);
167 /****************************************************************************
168 Return the sid and the type of the unix group.
169 ****************************************************************************/
171 static bool get_group_map_from_sid(struct dom_sid sid
, GROUP_MAP
*map
)
177 /* the key is the SID, retrieving is direct */
179 key
= group_mapping_key(talloc_tos(), &sid
);
184 dbuf
= dbwrap_fetch_bystring(db
, key
, key
);
185 if (dbuf
.dptr
== NULL
) {
190 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
191 &map
->gid
, &map
->sid_name_use
,
192 &map
->nt_name
, &map
->comment
);
197 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
201 sid_copy(&map
->sid
, &sid
);
206 static bool dbrec2map(const struct db_record
*rec
, GROUP_MAP
*map
)
208 if ((rec
->key
.dsize
< strlen(GROUP_PREFIX
))
209 || (strncmp((char *)rec
->key
.dptr
, GROUP_PREFIX
,
210 GROUP_PREFIX_LEN
) != 0)) {
214 if (!string_to_sid(&map
->sid
, (const char *)rec
->key
.dptr
215 + GROUP_PREFIX_LEN
)) {
219 return tdb_unpack(rec
->value
.dptr
, rec
->value
.dsize
, "ddff",
220 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
,
221 &map
->comment
) != -1;
224 struct find_map_state
{
226 const char *name
; /* If != NULL, look for name */
227 gid_t gid
; /* valid iff name == NULL */
231 static int find_map(struct db_record
*rec
, void *private_data
)
233 struct find_map_state
*state
= (struct find_map_state
*)private_data
;
235 if (!dbrec2map(rec
, state
->map
)) {
236 DEBUG(10, ("failed to unpack map\n"));
240 if (state
->name
!= NULL
) {
241 if (strequal(state
->name
, state
->map
->nt_name
)) {
247 if (state
->map
->gid
== state
->gid
) {
256 /****************************************************************************
257 Return the sid and the type of the unix group.
258 ****************************************************************************/
260 static bool get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
262 struct find_map_state state
;
265 state
.name
= NULL
; /* Indicate we're looking for gid */
269 db
->traverse_read(db
, find_map
, (void *)&state
);
274 /****************************************************************************
275 Return the sid and the type of the unix group.
276 ****************************************************************************/
278 static bool get_group_map_from_ntname(const char *name
, GROUP_MAP
*map
)
280 struct find_map_state state
;
286 db
->traverse_read(db
, find_map
, (void *)&state
);
291 /****************************************************************************
292 Remove a group mapping entry.
293 ****************************************************************************/
295 static bool group_map_remove(const struct dom_sid
*sid
)
300 key
= group_mapping_key(talloc_tos(), sid
);
305 status
= dbwrap_trans_delete(db
, string_term_tdb_data(key
));
308 return NT_STATUS_IS_OK(status
);
311 /****************************************************************************
312 Enumerate the group mapping.
313 ****************************************************************************/
315 struct enum_map_state
{
316 const struct dom_sid
*domsid
;
317 enum lsa_SidType sid_name_use
;
324 static int collect_map(struct db_record
*rec
, void *private_data
)
326 struct enum_map_state
*state
= (struct enum_map_state
*)private_data
;
330 if (!dbrec2map(rec
, &map
)) {
333 /* list only the type or everything if UNKNOWN */
334 if (state
->sid_name_use
!= SID_NAME_UNKNOWN
335 && state
->sid_name_use
!= map
.sid_name_use
) {
336 DEBUG(11,("enum_group_mapping: group %s is not of the "
337 "requested type\n", map
.nt_name
));
341 if ((state
->unix_only
== ENUM_ONLY_MAPPED
) && (map
.gid
== -1)) {
342 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
347 if ((state
->domsid
!= NULL
) &&
348 (dom_sid_compare_domain(state
->domsid
, &map
.sid
) != 0)) {
349 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
350 sid_string_dbg(&map
.sid
)));
354 if (!(tmp
= SMB_REALLOC_ARRAY(state
->maps
, GROUP_MAP
,
355 state
->num_maps
+1))) {
356 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
362 state
->maps
[state
->num_maps
] = map
;
367 static bool enum_group_mapping(const struct dom_sid
*domsid
,
368 enum lsa_SidType sid_name_use
,
370 size_t *p_num_entries
, bool unix_only
)
372 struct enum_map_state state
;
374 state
.domsid
= domsid
;
375 state
.sid_name_use
= sid_name_use
;
376 state
.unix_only
= unix_only
;
380 if (db
->traverse_read(db
, collect_map
, (void *)&state
) < 0) {
384 *pp_rmap
= state
.maps
;
385 *p_num_entries
= state
.num_maps
;
390 /* This operation happens on session setup, so it should better be fast. We
391 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
393 static NTSTATUS
one_alias_membership(const struct dom_sid
*member
,
394 struct dom_sid
**sids
, size_t *num
)
401 NTSTATUS status
= NT_STATUS_OK
;
402 TALLOC_CTX
*frame
= talloc_stackframe();
404 slprintf(key
, sizeof(key
), "%s%s", MEMBEROF_PREFIX
,
405 sid_to_fstring(tmp
, member
));
407 dbuf
= dbwrap_fetch_bystring(db
, frame
, key
);
408 if (dbuf
.dptr
== NULL
) {
413 p
= (const char *)dbuf
.dptr
;
415 while (next_token_talloc(frame
, &p
, &string_sid
, " ")) {
416 struct dom_sid alias
;
419 if (!string_to_sid(&alias
, string_sid
))
423 status
= add_sid_to_array_unique(NULL
, &alias
, sids
, &num_sids
);
424 if (!NT_STATUS_IS_OK(status
)) {
435 static NTSTATUS
alias_memberships(const struct dom_sid
*members
, size_t num_members
,
436 struct dom_sid
**sids
, size_t *num
)
443 for (i
=0; i
<num_members
; i
++) {
444 NTSTATUS status
= one_alias_membership(&members
[i
], sids
, num
);
445 if (!NT_STATUS_IS_OK(status
))
451 static bool is_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
453 struct dom_sid
*sids
;
457 /* This feels the wrong way round, but the on-disk data structure
458 * dictates it this way. */
459 if (!NT_STATUS_IS_OK(alias_memberships(member
, 1, &sids
, &num
)))
462 for (i
=0; i
<num
; i
++) {
463 if (dom_sid_compare(alias
, &sids
[i
]) == 0) {
473 static NTSTATUS
add_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
478 char *new_memberstring
;
479 struct db_record
*rec
;
482 if (!get_group_map_from_sid(*alias
, &map
))
483 return NT_STATUS_NO_SUCH_ALIAS
;
485 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
486 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
487 return NT_STATUS_NO_SUCH_ALIAS
;
489 if (is_aliasmem(alias
, member
))
490 return NT_STATUS_MEMBER_IN_ALIAS
;
492 sid_to_fstring(string_sid
, member
);
494 key
= talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX
,
497 return NT_STATUS_NO_MEMORY
;
500 if (db
->transaction_start(db
) != 0) {
501 DEBUG(0, ("transaction_start failed\n"));
502 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
505 rec
= db
->fetch_locked(db
, key
, string_term_tdb_data(key
));
508 DEBUG(10, ("fetch_lock failed\n"));
510 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
514 sid_to_fstring(string_sid
, alias
);
516 if (rec
->value
.dptr
!= NULL
) {
517 new_memberstring
= talloc_asprintf(
518 key
, "%s %s", (char *)(rec
->value
.dptr
), string_sid
);
520 new_memberstring
= talloc_strdup(key
, string_sid
);
523 if (new_memberstring
== NULL
) {
525 status
= NT_STATUS_NO_MEMORY
;
529 status
= rec
->store(rec
, string_term_tdb_data(new_memberstring
), 0);
533 if (!NT_STATUS_IS_OK(status
)) {
534 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status
)));
538 if (db
->transaction_commit(db
) != 0) {
539 DEBUG(0, ("transaction_commit failed\n"));
540 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
547 if (db
->transaction_cancel(db
) != 0) {
548 smb_panic("transaction_cancel failed");
554 struct aliasmem_state
{
556 const struct dom_sid
*alias
;
557 struct dom_sid
**sids
;
561 static int collect_aliasmem(struct db_record
*rec
, void *priv
)
563 struct aliasmem_state
*state
= (struct aliasmem_state
*)priv
;
568 if (strncmp((const char *)rec
->key
.dptr
, MEMBEROF_PREFIX
,
569 MEMBEROF_PREFIX_LEN
) != 0)
572 p
= (const char *)rec
->value
.dptr
;
574 frame
= talloc_stackframe();
576 while (next_token_talloc(frame
, &p
, &alias_string
, " ")) {
577 struct dom_sid alias
, member
;
578 const char *member_string
;
581 if (!string_to_sid(&alias
, alias_string
))
584 if (dom_sid_compare(state
->alias
, &alias
) != 0)
587 /* Ok, we found the alias we're looking for in the membership
588 * list currently scanned. The key represents the alias
589 * member. Add that. */
591 member_string
= strchr((const char *)rec
->key
.dptr
, '/');
593 /* Above we tested for MEMBEROF_PREFIX which includes the
596 SMB_ASSERT(member_string
!= NULL
);
599 if (!string_to_sid(&member
, member_string
))
602 num_sids
= *state
->num
;
603 if (!NT_STATUS_IS_OK(add_sid_to_array(state
->mem_ctx
, &member
,
610 *state
->num
= num_sids
;
617 static NTSTATUS
enum_aliasmem(const struct dom_sid
*alias
, TALLOC_CTX
*mem_ctx
,
618 struct dom_sid
**sids
, size_t *num
)
621 struct aliasmem_state state
;
623 if (!get_group_map_from_sid(*alias
, &map
))
624 return NT_STATUS_NO_SUCH_ALIAS
;
626 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
627 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
628 return NT_STATUS_NO_SUCH_ALIAS
;
636 state
.mem_ctx
= mem_ctx
;
638 db
->traverse_read(db
, collect_aliasmem
, &state
);
642 static NTSTATUS
del_aliasmem(const struct dom_sid
*alias
, const struct dom_sid
*member
)
645 struct dom_sid
*sids
;
652 if (db
->transaction_start(db
) != 0) {
653 DEBUG(0, ("transaction_start failed\n"));
654 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
657 status
= alias_memberships(member
, 1, &sids
, &num
);
659 if (!NT_STATUS_IS_OK(status
)) {
663 for (i
=0; i
<num
; i
++) {
664 if (dom_sid_compare(&sids
[i
], alias
) == 0) {
672 status
= NT_STATUS_MEMBER_NOT_IN_ALIAS
;
677 sids
[i
] = sids
[num
-1];
681 sid_to_fstring(sid_string
, member
);
683 key
= talloc_asprintf(sids
, "%s%s", MEMBEROF_PREFIX
, sid_string
);
686 status
= NT_STATUS_NO_MEMORY
;
691 status
= dbwrap_delete_bystring(db
, key
);
695 member_string
= talloc_strdup(sids
, "");
696 if (member_string
== NULL
) {
698 status
= NT_STATUS_NO_MEMORY
;
702 for (i
=0; i
<num
; i
++) {
704 sid_to_fstring(sid_string
, &sids
[i
]);
706 member_string
= talloc_asprintf_append_buffer(
707 member_string
, " %s", sid_string
);
709 if (member_string
== NULL
) {
711 status
= NT_STATUS_NO_MEMORY
;
716 status
= dbwrap_store_bystring(
717 db
, key
, string_term_tdb_data(member_string
), 0);
721 if (!NT_STATUS_IS_OK(status
)) {
722 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
727 if (db
->transaction_commit(db
) != 0) {
728 DEBUG(0, ("transaction_commit failed\n"));
729 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
736 if (db
->transaction_cancel(db
) != 0) {
737 smb_panic("transaction_cancel failed");
743 /* -- ldb->tdb switching code -------------------------------------------- */
745 /* change this if the data format ever changes */
746 #define LTDB_PACKING_FORMAT 0x26011967
748 /* old packing formats (not supported for now,
749 * it was never used for group mapping AFAIK) */
750 #define LTDB_PACKING_FORMAT_NODN 0x26011966
752 static unsigned int pull_uint32(uint8_t *p
, int ofs
)
755 return p
[0] | (p
[1]<<8) | (p
[2]<<16) | (p
[3]<<24);
759 unpack a ldb message from a linear buffer in TDB_DATA
761 static int convert_ldb_record(TDB_CONTEXT
*ltdb
, TDB_DATA key
,
762 TDB_DATA data
, void *ptr
)
764 TALLOC_CTX
*tmp_ctx
= talloc_tos();
769 unsigned int remaining
;
775 uint32_t num_mem
= 0;
776 struct dom_sid
*members
= NULL
;
778 p
= (uint8_t *)data
.dptr
;
779 if (data
.dsize
< 8) {
784 format
= pull_uint32(p
, 0);
785 num_el
= pull_uint32(p
, 4);
788 remaining
= data
.dsize
- 8;
791 case LTDB_PACKING_FORMAT
:
792 len
= strnlen((char *)p
, remaining
);
793 if (len
== remaining
) {
799 /* ignore special LDB attributes */
803 if (strncmp((char *)p
, "rid=", 4)) {
804 /* unknown entry, ignore */
805 DEBUG(3, ("Found unknown entry in group mapping "
806 "database named [%s]\n", (char *)p
));
810 remaining
-= len
+ 1;
814 case LTDB_PACKING_FORMAT_NODN
:
821 /* bad entry, ignore */
825 if (num_el
> remaining
/ 6) {
832 for (i
= 0; i
< num_el
; i
++) {
835 if (remaining
< 10) {
839 len
= strnlen((char *)p
, remaining
- 6);
840 if (len
== remaining
- 6) {
844 name
= talloc_strndup(tmp_ctx
, (char *)p
, len
);
849 remaining
-= len
+ 1;
852 num_vals
= pull_uint32(p
, 0);
853 if (strcasecmp_m(name
, "member") == 0) {
855 members
= talloc_array(tmp_ctx
, struct dom_sid
, num_mem
);
856 if (members
== NULL
) {
860 } else if (num_vals
!= 1) {
868 for (j
= 0; j
< num_vals
; j
++) {
869 len
= pull_uint32(p
, 0);
870 if (len
> remaining
-5) {
875 val
= talloc_strndup(tmp_ctx
, (char *)(p
+ 4), len
);
881 remaining
-= len
+4+1;
884 /* we ignore unknown or uninteresting attributes
885 * (objectclass, etc.) */
886 if (strcasecmp_m(name
, "gidNumber") == 0) {
887 map
.gid
= strtoul(val
, &q
, 10);
892 } else if (strcasecmp_m(name
, "sid") == 0) {
893 if (!string_to_sid(&map
.sid
, val
)) {
897 } else if (strcasecmp_m(name
, "sidNameUse") == 0) {
898 map
.sid_name_use
= strtoul(val
, &q
, 10);
903 } else if (strcasecmp_m(name
, "ntname") == 0) {
904 strlcpy(map
.nt_name
, val
,
905 sizeof(map
.nt_name
));
906 } else if (strcasecmp_m(name
, "comment") == 0) {
907 strlcpy(map
.comment
, val
,
908 sizeof(map
.comment
));
909 } else if (strcasecmp_m(name
, "member") == 0) {
910 if (!string_to_sid(&members
[j
], val
)) {
922 if (!add_mapping_entry(&map
, 0)) {
928 for (j
= 0; j
< num_mem
; j
++) {
930 status
= add_aliasmem(&map
.sid
, &members
[j
]);
931 if (!NT_STATUS_IS_OK(status
)) {
938 if (remaining
!= 0) {
939 DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
949 static bool mapping_switch(const char *ldb_path
)
956 frame
= talloc_stackframe();
958 ltdb
= tdb_open_log(ldb_path
, 0, TDB_DEFAULT
, O_RDONLY
, 0600);
959 if (ltdb
== NULL
) goto failed
;
961 /* ldb is just a very fancy tdb, read out raw data and perform
963 ret
= tdb_traverse(ltdb
, convert_ldb_record
, NULL
);
964 if (ret
< 0) goto failed
;
971 /* now rename the old db out of the way */
972 new_path
= state_path("group_mapping.ldb.replaced");
976 if (rename(ldb_path
, new_path
) != 0) {
977 DEBUG(0,("Failed to rename old group mapping database\n"));
984 DEBUG(0, ("Failed to switch to tdb group mapping database\n"));
985 if (ltdb
) tdb_close(ltdb
);
990 static const struct mapping_backend tdb_backend
= {
991 .add_mapping_entry
= add_mapping_entry
,
992 .get_group_map_from_sid
= get_group_map_from_sid
,
993 .get_group_map_from_gid
= get_group_map_from_gid
,
994 .get_group_map_from_ntname
= get_group_map_from_ntname
,
995 .group_map_remove
= group_map_remove
,
996 .enum_group_mapping
= enum_group_mapping
,
997 .one_alias_membership
= one_alias_membership
,
998 .add_aliasmem
= add_aliasmem
,
999 .del_aliasmem
= del_aliasmem
,
1000 .enum_aliasmem
= enum_aliasmem
1004 initialise the tdb mapping backend
1006 const struct mapping_backend
*groupdb_tdb_init(void)
1008 if (!init_group_mapping()) {
1009 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
1013 return &tdb_backend
;