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
,
29 enum lsa_SidType sid_name_use
,
31 size_t *p_num_entries
,
33 static bool group_map_remove(const DOM_SID
*sid
);
35 static bool mapping_switch(const char *ldb_path
);
37 /****************************************************************************
38 Open the group mapping tdb.
39 ****************************************************************************/
40 static bool init_group_mapping(void)
48 db
= db_open(NULL
, state_path("group_mapping.tdb"), 0,
49 TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
51 DEBUG(0, ("Failed to open group mapping database: %s\n",
56 ldb_path
= state_path("group_mapping.ldb");
57 if (file_exist(ldb_path
) && !mapping_switch(ldb_path
)) {
58 unlink(state_path("group_mapping.tdb"));
62 /* handle upgrade from old versions of the database */
63 #if 0 /* -- Needs conversion to dbwrap -- */
64 const char *vstring
= "INFO/version";
66 GROUP_MAP
*map_table
= NULL
;
67 size_t num_entries
= 0;
69 /* handle a Samba upgrade */
70 tdb_lock_bystring(tdb
, vstring
);
72 /* Cope with byte-reversed older versions of the db. */
73 vers_id
= tdb_fetch_int32(tdb
, vstring
);
74 if ((vers_id
== DATABASE_VERSION_V1
)
75 || (IREV(vers_id
) == DATABASE_VERSION_V1
)) {
77 * Written on a bigendian machine with old fetch_int
80 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
81 vers_id
= DATABASE_VERSION_V2
;
84 /* if its an unknown version we remove everthing in the db */
86 if (vers_id
!= DATABASE_VERSION_V2
) {
88 tdb_store_int32(tdb
, vstring
, DATABASE_VERSION_V2
);
91 tdb_unlock_bystring(tdb
, vstring
);
93 /* cleanup any map entries with a gid == -1 */
95 if ( enum_group_mapping( NULL
, SID_NAME_UNKNOWN
, &map_table
,
96 &num_entries
, False
) ) {
99 for ( i
=0; i
<num_entries
; i
++ ) {
100 if ( map_table
[i
].gid
== -1 ) {
101 group_map_remove( &map_table
[i
].sid
);
105 SAFE_FREE( map_table
);
112 static char *group_mapping_key(TALLOC_CTX
*mem_ctx
, const DOM_SID
*sid
)
114 char *sidstr
, *result
;
116 sidstr
= sid_string_talloc(talloc_tos(), sid
);
117 if (sidstr
== NULL
) {
121 result
= talloc_asprintf(mem_ctx
, "%s%s", GROUP_PREFIX
, sidstr
);
127 /****************************************************************************
128 ****************************************************************************/
129 static bool add_mapping_entry(GROUP_MAP
*map
, int flag
)
135 key
= group_mapping_key(talloc_tos(), &map
->sid
);
140 len
= tdb_pack(NULL
, 0, "ddff",
141 map
->gid
, map
->sid_name_use
, map
->nt_name
, map
->comment
);
143 buf
= TALLOC_ARRAY(key
, char, len
);
148 len
= tdb_pack((uint8
*)buf
, len
, "ddff", map
->gid
,
149 map
->sid_name_use
, map
->nt_name
, map
->comment
);
151 status
= dbwrap_trans_store(
152 db
, string_term_tdb_data(key
),
153 make_tdb_data((uint8_t *)buf
, len
), TDB_REPLACE
);
157 return NT_STATUS_IS_OK(status
);
161 /****************************************************************************
162 Return the sid and the type of the unix group.
163 ****************************************************************************/
165 static bool get_group_map_from_sid(DOM_SID sid
, GROUP_MAP
*map
)
171 /* the key is the SID, retrieving is direct */
173 key
= group_mapping_key(talloc_tos(), &sid
);
178 dbuf
= dbwrap_fetch_bystring(db
, key
, key
);
179 if (dbuf
.dptr
== NULL
) {
184 ret
= tdb_unpack(dbuf
.dptr
, dbuf
.dsize
, "ddff",
185 &map
->gid
, &map
->sid_name_use
,
186 &map
->nt_name
, &map
->comment
);
191 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
195 sid_copy(&map
->sid
, &sid
);
200 static bool dbrec2map(const struct db_record
*rec
, GROUP_MAP
*map
)
202 if ((rec
->key
.dsize
< strlen(GROUP_PREFIX
))
203 || (strncmp((char *)rec
->key
.dptr
, GROUP_PREFIX
,
204 GROUP_PREFIX_LEN
) != 0)) {
208 if (!string_to_sid(&map
->sid
, (const char *)rec
->key
.dptr
209 + GROUP_PREFIX_LEN
)) {
213 return tdb_unpack(rec
->value
.dptr
, rec
->value
.dsize
, "ddff",
214 &map
->gid
, &map
->sid_name_use
, &map
->nt_name
,
215 &map
->comment
) != -1;
218 struct find_map_state
{
220 const char *name
; /* If != NULL, look for name */
221 gid_t gid
; /* valid iff name == NULL */
225 static int find_map(struct db_record
*rec
, void *private_data
)
227 struct find_map_state
*state
= (struct find_map_state
*)private_data
;
229 if (!dbrec2map(rec
, state
->map
)) {
230 DEBUG(10, ("failed to unpack map\n"));
234 if (state
->name
!= NULL
) {
235 if (strequal(state
->name
, state
->map
->nt_name
)) {
241 if (state
->map
->gid
== state
->gid
) {
250 /****************************************************************************
251 Return the sid and the type of the unix group.
252 ****************************************************************************/
254 static bool get_group_map_from_gid(gid_t gid
, GROUP_MAP
*map
)
256 struct find_map_state state
;
259 state
.name
= NULL
; /* Indicate we're looking for gid */
263 db
->traverse_read(db
, find_map
, (void *)&state
);
268 /****************************************************************************
269 Return the sid and the type of the unix group.
270 ****************************************************************************/
272 static bool get_group_map_from_ntname(const char *name
, GROUP_MAP
*map
)
274 struct find_map_state state
;
280 db
->traverse_read(db
, find_map
, (void *)&state
);
285 /****************************************************************************
286 Remove a group mapping entry.
287 ****************************************************************************/
289 static bool group_map_remove(const DOM_SID
*sid
)
294 key
= group_mapping_key(talloc_tos(), sid
);
299 status
= dbwrap_trans_delete(db
, string_term_tdb_data(key
));
302 return NT_STATUS_IS_OK(status
);
305 /****************************************************************************
306 Enumerate the group mapping.
307 ****************************************************************************/
309 struct enum_map_state
{
310 const DOM_SID
*domsid
;
311 enum lsa_SidType sid_name_use
;
318 static int collect_map(struct db_record
*rec
, void *private_data
)
320 struct enum_map_state
*state
= (struct enum_map_state
*)private_data
;
324 if (!dbrec2map(rec
, &map
)) {
327 /* list only the type or everything if UNKNOWN */
328 if (state
->sid_name_use
!= SID_NAME_UNKNOWN
329 && state
->sid_name_use
!= map
.sid_name_use
) {
330 DEBUG(11,("enum_group_mapping: group %s is not of the "
331 "requested type\n", map
.nt_name
));
335 if ((state
->unix_only
== ENUM_ONLY_MAPPED
) && (map
.gid
== -1)) {
336 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
341 if ((state
->domsid
!= NULL
) &&
342 (sid_compare_domain(state
->domsid
, &map
.sid
) != 0)) {
343 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
344 sid_string_dbg(&map
.sid
)));
348 if (!(tmp
= SMB_REALLOC_ARRAY(state
->maps
, GROUP_MAP
,
349 state
->num_maps
+1))) {
350 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
356 state
->maps
[state
->num_maps
] = map
;
361 static bool enum_group_mapping(const DOM_SID
*domsid
,
362 enum lsa_SidType sid_name_use
,
364 size_t *p_num_entries
, bool unix_only
)
366 struct enum_map_state state
;
368 state
.domsid
= domsid
;
369 state
.sid_name_use
= sid_name_use
;
370 state
.unix_only
= unix_only
;
374 if (db
->traverse_read(db
, collect_map
, (void *)&state
) < 0) {
378 *pp_rmap
= state
.maps
;
379 *p_num_entries
= state
.num_maps
;
384 /* This operation happens on session setup, so it should better be fast. We
385 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
387 static NTSTATUS
one_alias_membership(const DOM_SID
*member
,
388 DOM_SID
**sids
, size_t *num
)
395 NTSTATUS status
= NT_STATUS_OK
;
396 TALLOC_CTX
*frame
= talloc_stackframe();
398 slprintf(key
, sizeof(key
), "%s%s", MEMBEROF_PREFIX
,
399 sid_to_fstring(tmp
, member
));
401 dbuf
= dbwrap_fetch_bystring(db
, frame
, key
);
402 if (dbuf
.dptr
== NULL
) {
407 p
= (const char *)dbuf
.dptr
;
409 while (next_token_talloc(frame
, &p
, &string_sid
, " ")) {
412 if (!string_to_sid(&alias
, string_sid
))
415 status
= add_sid_to_array_unique(NULL
, &alias
, sids
, num
);
416 if (!NT_STATUS_IS_OK(status
)) {
426 static NTSTATUS
alias_memberships(const DOM_SID
*members
, size_t num_members
,
427 DOM_SID
**sids
, size_t *num
)
434 for (i
=0; i
<num_members
; i
++) {
435 NTSTATUS status
= one_alias_membership(&members
[i
], sids
, num
);
436 if (!NT_STATUS_IS_OK(status
))
442 static bool is_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
447 /* This feels the wrong way round, but the on-disk data structure
448 * dictates it this way. */
449 if (!NT_STATUS_IS_OK(alias_memberships(member
, 1, &sids
, &num
)))
452 for (i
=0; i
<num
; i
++) {
453 if (sid_compare(alias
, &sids
[i
]) == 0) {
463 static NTSTATUS
add_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
468 char *new_memberstring
;
469 struct db_record
*rec
;
472 if (!get_group_map_from_sid(*alias
, &map
))
473 return NT_STATUS_NO_SUCH_ALIAS
;
475 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
476 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
477 return NT_STATUS_NO_SUCH_ALIAS
;
479 if (is_aliasmem(alias
, member
))
480 return NT_STATUS_MEMBER_IN_ALIAS
;
482 sid_to_fstring(string_sid
, member
);
484 key
= talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX
,
487 return NT_STATUS_NO_MEMORY
;
490 if (db
->transaction_start(db
) != 0) {
491 DEBUG(0, ("transaction_start failed\n"));
492 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
495 rec
= db
->fetch_locked(db
, key
, string_term_tdb_data(key
));
498 DEBUG(10, ("fetch_lock failed\n"));
500 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
504 sid_to_fstring(string_sid
, alias
);
506 if (rec
->value
.dptr
!= NULL
) {
507 new_memberstring
= talloc_asprintf(
508 key
, "%s %s", (char *)(rec
->value
.dptr
), string_sid
);
510 new_memberstring
= talloc_strdup(key
, string_sid
);
513 if (new_memberstring
== NULL
) {
515 status
= NT_STATUS_NO_MEMORY
;
519 status
= rec
->store(rec
, string_term_tdb_data(new_memberstring
), 0);
523 if (!NT_STATUS_IS_OK(status
)) {
524 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status
)));
528 if (db
->transaction_commit(db
) != 0) {
529 DEBUG(0, ("transaction_commit failed\n"));
530 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
537 if (db
->transaction_cancel(db
) != 0) {
538 smb_panic("transaction_cancel failed");
544 struct aliasmem_state
{
546 const DOM_SID
*alias
;
551 static int collect_aliasmem(struct db_record
*rec
, void *priv
)
553 struct aliasmem_state
*state
= (struct aliasmem_state
*)priv
;
558 if (strncmp((const char *)rec
->key
.dptr
, MEMBEROF_PREFIX
,
559 MEMBEROF_PREFIX_LEN
) != 0)
562 p
= (const char *)rec
->value
.dptr
;
564 frame
= talloc_stackframe();
566 while (next_token_talloc(frame
, &p
, &alias_string
, " ")) {
567 DOM_SID alias
, member
;
568 const char *member_string
;
570 if (!string_to_sid(&alias
, alias_string
))
573 if (sid_compare(state
->alias
, &alias
) != 0)
576 /* Ok, we found the alias we're looking for in the membership
577 * list currently scanned. The key represents the alias
578 * member. Add that. */
580 member_string
= strchr((const char *)rec
->key
.dptr
, '/');
582 /* Above we tested for MEMBEROF_PREFIX which includes the
585 SMB_ASSERT(member_string
!= NULL
);
588 if (!string_to_sid(&member
, member_string
))
591 if (!NT_STATUS_IS_OK(add_sid_to_array(state
->mem_ctx
, &member
,
604 static NTSTATUS
enum_aliasmem(const DOM_SID
*alias
, TALLOC_CTX
*mem_ctx
,
605 DOM_SID
**sids
, size_t *num
)
608 struct aliasmem_state state
;
610 if (!get_group_map_from_sid(*alias
, &map
))
611 return NT_STATUS_NO_SUCH_ALIAS
;
613 if ( (map
.sid_name_use
!= SID_NAME_ALIAS
) &&
614 (map
.sid_name_use
!= SID_NAME_WKN_GRP
) )
615 return NT_STATUS_NO_SUCH_ALIAS
;
623 state
.mem_ctx
= mem_ctx
;
625 db
->traverse_read(db
, collect_aliasmem
, &state
);
629 static NTSTATUS
del_aliasmem(const DOM_SID
*alias
, const DOM_SID
*member
)
639 if (db
->transaction_start(db
) != 0) {
640 DEBUG(0, ("transaction_start failed\n"));
641 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
644 status
= alias_memberships(member
, 1, &sids
, &num
);
646 if (!NT_STATUS_IS_OK(status
)) {
650 for (i
=0; i
<num
; i
++) {
651 if (sid_compare(&sids
[i
], alias
) == 0) {
659 status
= NT_STATUS_MEMBER_NOT_IN_ALIAS
;
664 sids
[i
] = sids
[num
-1];
668 sid_to_fstring(sid_string
, member
);
670 key
= talloc_asprintf(sids
, "%s%s", MEMBEROF_PREFIX
, sid_string
);
673 status
= NT_STATUS_NO_MEMORY
;
678 status
= dbwrap_delete_bystring(db
, key
);
682 member_string
= talloc_strdup(sids
, "");
683 if (member_string
== NULL
) {
685 status
= NT_STATUS_NO_MEMORY
;
689 for (i
=0; i
<num
; i
++) {
691 sid_to_fstring(sid_string
, &sids
[i
]);
693 member_string
= talloc_asprintf_append_buffer(
694 member_string
, " %s", sid_string
);
696 if (member_string
== NULL
) {
698 status
= NT_STATUS_NO_MEMORY
;
703 status
= dbwrap_store_bystring(
704 db
, key
, string_term_tdb_data(member_string
), 0);
708 if (!NT_STATUS_IS_OK(status
)) {
709 DEBUG(10, ("dbwrap_store_bystring failed: %s\n",
714 if (db
->transaction_commit(db
) != 0) {
715 DEBUG(0, ("transaction_commit failed\n"));
716 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
723 if (db
->transaction_cancel(db
) != 0) {
724 smb_panic("transaction_cancel failed");
730 /* -- ldb->tdb switching code -------------------------------------------- */
732 /* change this if the data format ever changes */
733 #define LTDB_PACKING_FORMAT 0x26011967
735 /* old packing formats (not supported for now,
736 * it was never used for group mapping AFAIK) */
737 #define LTDB_PACKING_FORMAT_NODN 0x26011966
739 static unsigned int pull_uint32(uint8_t *p
, int ofs
)
742 return p
[0] | (p
[1]<<8) | (p
[2]<<16) | (p
[3]<<24);
746 unpack a ldb message from a linear buffer in TDB_DATA
748 static int convert_ldb_record(TDB_CONTEXT
*ltdb
, TDB_DATA key
,
749 TDB_DATA data
, void *ptr
)
751 TALLOC_CTX
*tmp_ctx
= talloc_tos();
756 unsigned int remaining
;
762 uint32_t num_mem
= 0;
763 DOM_SID
*members
= NULL
;
765 p
= (uint8_t *)data
.dptr
;
766 if (data
.dsize
< 8) {
771 format
= pull_uint32(p
, 0);
772 num_el
= pull_uint32(p
, 4);
775 remaining
= data
.dsize
- 8;
778 case LTDB_PACKING_FORMAT
:
779 len
= strnlen((char *)p
, remaining
);
780 if (len
== remaining
) {
786 /* ignore special LDB attributes */
790 if (strncmp((char *)p
, "rid=", 4)) {
791 /* unknown entry, ignore */
792 DEBUG(3, ("Found unknown entry in group mapping "
793 "database named [%s]\n", (char *)p
));
797 remaining
-= len
+ 1;
801 case LTDB_PACKING_FORMAT_NODN
:
808 /* bad entry, ignore */
812 if (num_el
> remaining
/ 6) {
819 for (i
= 0; i
< num_el
; i
++) {
822 if (remaining
< 10) {
826 len
= strnlen((char *)p
, remaining
- 6);
827 if (len
== remaining
- 6) {
831 name
= talloc_strndup(tmp_ctx
, (char *)p
, len
);
836 remaining
-= len
+ 1;
839 num_vals
= pull_uint32(p
, 0);
840 if (StrCaseCmp(name
, "member") == 0) {
842 members
= talloc_array(tmp_ctx
, DOM_SID
, num_mem
);
843 if (members
== NULL
) {
847 } else if (num_vals
!= 1) {
855 for (j
= 0; j
< num_vals
; j
++) {
856 len
= pull_uint32(p
, 0);
857 if (len
> remaining
-5) {
862 val
= talloc_strndup(tmp_ctx
, (char *)(p
+ 4), len
);
868 remaining
-= len
+4+1;
871 /* we ignore unknown or uninteresting attributes
872 * (objectclass, etc.) */
873 if (StrCaseCmp(name
, "gidNumber") == 0) {
874 map
.gid
= strtoul(val
, &q
, 10);
879 } else if (StrCaseCmp(name
, "sid") == 0) {
880 if (!string_to_sid(&map
.sid
, val
)) {
884 } else if (StrCaseCmp(name
, "sidNameUse") == 0) {
885 map
.sid_name_use
= strtoul(val
, &q
, 10);
890 } else if (StrCaseCmp(name
, "ntname") == 0) {
891 strlcpy(map
.nt_name
, val
,
892 sizeof(map
.nt_name
) -1);
893 } else if (StrCaseCmp(name
, "comment") == 0) {
894 strlcpy(map
.comment
, val
,
895 sizeof(map
.comment
) -1);
896 } else if (StrCaseCmp(name
, "member") == 0) {
897 if (!string_to_sid(&members
[j
], val
)) {
909 if (!add_mapping_entry(&map
, 0)) {
915 for (j
= 0; j
< num_mem
; j
++) {
917 status
= add_aliasmem(&map
.sid
, &members
[j
]);
918 if (!NT_STATUS_IS_OK(status
)) {
925 if (remaining
!= 0) {
926 DEBUG(0, ("Errror: %d bytes unread in ltdb_unpack_data\n",
936 static bool mapping_switch(const char *ldb_path
)
943 frame
= talloc_stackframe();
945 ltdb
= tdb_open_log(ldb_path
, 0, TDB_DEFAULT
, O_RDONLY
, 0600);
946 if (ltdb
== NULL
) goto failed
;
948 /* ldb is just a very fancy tdb, read out raw data and perform
950 ret
= tdb_traverse(ltdb
, convert_ldb_record
, NULL
);
951 if (ret
== -1) goto failed
;
958 /* now rename the old db out of the way */
959 new_path
= state_path("group_mapping.ldb.replaced");
963 if (rename(ldb_path
, new_path
) != 0) {
964 DEBUG(0,("Failed to rename old group mapping database\n"));
971 DEBUG(0,("Failed to swith to tdb group mapping database\n"));
972 if (ltdb
) tdb_close(ltdb
);
977 static const struct mapping_backend tdb_backend
= {
978 .add_mapping_entry
= add_mapping_entry
,
979 .get_group_map_from_sid
= get_group_map_from_sid
,
980 .get_group_map_from_gid
= get_group_map_from_gid
,
981 .get_group_map_from_ntname
= get_group_map_from_ntname
,
982 .group_map_remove
= group_map_remove
,
983 .enum_group_mapping
= enum_group_mapping
,
984 .one_alias_membership
= one_alias_membership
,
985 .add_aliasmem
= add_aliasmem
,
986 .del_aliasmem
= del_aliasmem
,
987 .enum_aliasmem
= enum_aliasmem
991 initialise the tdb mapping backend
993 const struct mapping_backend
*groupdb_tdb_init(void)
995 if (!init_group_mapping()) {
996 DEBUG(0,("Failed to initialise tdb mapping backend\n"));
1000 return &tdb_backend
;