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 TDB_CONTEXT *tdb; /* 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)
37 const char *vstring = "INFO/version";
39 GROUP_MAP *map_table = NULL;
40 size_t num_entries = 0;
45 tdb = tdb_open_log(state_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
47 DEBUG(0,("Failed to open group mapping database\n"));
51 /* handle a Samba upgrade */
52 tdb_lock_bystring(tdb, vstring);
54 /* Cope with byte-reversed older versions of the db. */
55 vers_id = tdb_fetch_int32(tdb, vstring);
56 if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) {
57 /* Written on a bigendian machine with old fetch_int code. Save as le. */
58 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
59 vers_id = DATABASE_VERSION_V2;
62 /* if its an unknown version we remove everthing in the db */
64 if (vers_id != DATABASE_VERSION_V2) {
65 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
66 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
69 tdb_unlock_bystring(tdb, vstring);
71 /* cleanup any map entries with a gid == -1 */
73 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table, &num_entries, False ) ) {
76 for ( i=0; i<num_entries; i++ ) {
77 if ( map_table[i].gid == -1 ) {
78 group_map_remove( &map_table[i].sid );
82 SAFE_FREE( map_table );
89 /****************************************************************************
90 ****************************************************************************/
91 static bool add_mapping_entry(GROUP_MAP *map, int flag)
96 fstring string_sid="";
100 sid_to_string(string_sid, &map->sid);
102 len = tdb_pack(NULL, sizeof(buf), "ddff",
103 map->gid, map->sid_name_use, map->nt_name, map->comment);
105 buf = SMB_MALLOC_ARRAY(char, len);
109 len = tdb_pack((uint8 *)buf, sizeof(buf), "ddff", map->gid,
110 map->sid_name_use, map->nt_name, map->comment);
113 if (asprintf(&key, "%s%s", GROUP_PREFIX, string_sid) < 0) {
119 dbuf.dptr = (uint8 *)buf;
121 ret = (tdb_store_bystring(tdb, key, dbuf, flag) == 0);
129 /****************************************************************************
130 Return the sid and the type of the unix group.
131 ****************************************************************************/
133 static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
140 /* the key is the SID, retrieving is direct */
142 sid_to_string(string_sid, &sid);
143 if (asprintf(&key, "%s%s", GROUP_PREFIX, string_sid) < 0) {
147 dbuf = tdb_fetch_bystring(tdb, key);
155 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
156 &map->gid, &map->sid_name_use,
157 &map->nt_name, &map->comment);
159 SAFE_FREE(dbuf.dptr);
162 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
166 sid_copy(&map->sid, &sid);
171 /****************************************************************************
172 Return the sid and the type of the unix group.
173 ****************************************************************************/
175 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
177 TDB_DATA kbuf, dbuf, newkey;
181 /* we need to enumerate the TDB to find the GID */
183 for (kbuf = tdb_firstkey(tdb);
185 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
187 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
189 dbuf = tdb_fetch(tdb, kbuf);
193 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
195 string_to_sid(&map->sid, string_sid);
197 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
198 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
200 SAFE_FREE(dbuf.dptr);
203 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
208 SAFE_FREE(kbuf.dptr);
216 /****************************************************************************
217 Return the sid and the type of the unix group.
218 ****************************************************************************/
220 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
222 TDB_DATA kbuf, dbuf, newkey;
226 /* we need to enumerate the TDB to find the name */
228 for (kbuf = tdb_firstkey(tdb);
230 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
232 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
234 dbuf = tdb_fetch(tdb, kbuf);
238 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
240 string_to_sid(&map->sid, string_sid);
242 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
243 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
245 SAFE_FREE(dbuf.dptr);
248 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
252 if ( strequal(name, map->nt_name) ) {
253 SAFE_FREE(kbuf.dptr);
261 /****************************************************************************
262 Remove a group mapping entry.
263 ****************************************************************************/
265 static bool group_map_remove(const DOM_SID *sid)
272 /* the key is the SID, retrieving is direct */
274 sid_to_string(string_sid, sid);
275 if (asprintf(&key, "%s%s", GROUP_PREFIX, string_sid) < 0) {
279 dbuf = tdb_fetch_bystring(tdb, key);
285 SAFE_FREE(dbuf.dptr);
287 ret = (tdb_delete_bystring(tdb, key) == TDB_SUCCESS);
292 /****************************************************************************
293 Enumerate the group mapping.
294 ****************************************************************************/
296 static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
297 size_t *p_num_entries, bool unix_only)
299 TDB_DATA kbuf, dbuf, newkey;
311 for (kbuf = tdb_firstkey(tdb);
313 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
315 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
318 dbuf = tdb_fetch(tdb, kbuf);
322 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
324 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
325 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
327 SAFE_FREE(dbuf.dptr);
330 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
334 /* list only the type or everything if UNKNOWN */
335 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
336 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
340 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
341 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
345 string_to_sid(&grpsid, string_sid);
346 sid_copy( &map.sid, &grpsid );
348 sid_split_rid( &grpsid, &rid );
350 /* Only check the domain if we were given one */
352 if ( domsid && !sid_equal( domsid, &grpsid ) ) {
353 DEBUG(11,("enum_group_mapping: group %s is not in domain %s\n",
354 string_sid, sid_string_static(domsid)));
358 DEBUG(11,("enum_group_mapping: returning group %s of "
359 "type %s\n", map.nt_name,
360 sid_type_lookup(map.sid_name_use)));
362 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
364 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
370 mapt[entries].gid = map.gid;
371 sid_copy( &mapt[entries].sid, &map.sid);
372 mapt[entries].sid_name_use = map.sid_name_use;
373 fstrcpy(mapt[entries].nt_name, map.nt_name);
374 fstrcpy(mapt[entries].comment, map.comment);
380 *p_num_entries=entries;
385 /* This operation happens on session setup, so it should better be fast. We
386 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
388 static NTSTATUS one_alias_membership(const DOM_SID *member,
389 DOM_SID **sids, size_t *num)
391 fstring key, string_sid;
395 sid_to_string(string_sid, member);
396 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
398 dbuf = tdb_fetch_bystring(tdb, key);
400 if (dbuf.dptr == NULL) {
404 p = (const char *)dbuf.dptr;
406 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
410 if (!string_to_sid(&alias, string_sid))
413 if (!add_sid_to_array_unique(NULL, &alias, sids, num)) {
414 return NT_STATUS_NO_MEMORY;
418 SAFE_FREE(dbuf.dptr);
422 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
423 DOM_SID **sids, size_t *num)
430 for (i=0; i<num_members; i++) {
431 NTSTATUS status = one_alias_membership(&members[i], sids, num);
432 if (!NT_STATUS_IS_OK(status))
438 static bool is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
443 /* This feels the wrong way round, but the on-disk data structure
444 * dictates it this way. */
445 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
448 for (i=0; i<num; i++) {
449 if (sid_compare(alias, &sids[i]) == 0) {
459 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
465 char *new_memberstring;
468 if (!get_group_map_from_sid(*alias, &map))
469 return NT_STATUS_NO_SUCH_ALIAS;
471 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
472 (map.sid_name_use != SID_NAME_WKN_GRP) )
473 return NT_STATUS_NO_SUCH_ALIAS;
475 if (is_aliasmem(alias, member))
476 return NT_STATUS_MEMBER_IN_ALIAS;
478 sid_to_string(string_sid, member);
479 if (asprintf(&key, "%s%s", MEMBEROF_PREFIX, string_sid) < 0) {
480 return NT_STATUS_NO_MEMORY;
483 dbuf = tdb_fetch_bystring(tdb, key);
485 sid_to_string(string_sid, alias);
487 if (dbuf.dptr != NULL) {
488 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
491 new_memberstring = SMB_STRDUP(string_sid);
494 if (new_memberstring == NULL) {
496 return NT_STATUS_NO_MEMORY;
499 SAFE_FREE(dbuf.dptr);
500 dbuf = string_term_tdb_data(new_memberstring);
502 result = tdb_store_bystring(tdb, key, dbuf, 0);
504 SAFE_FREE(new_memberstring);
507 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
510 struct aliasmem_closure {
511 const DOM_SID *alias;
516 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
519 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
521 fstring alias_string;
523 if (strncmp((const char *)key.dptr, MEMBEROF_PREFIX,
524 strlen(MEMBEROF_PREFIX)) != 0)
527 p = (const char *)data.dptr;
529 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
531 DOM_SID alias, member;
532 const char *member_string;
535 if (!string_to_sid(&alias, alias_string))
538 if (sid_compare(closure->alias, &alias) != 0)
541 /* Ok, we found the alias we're looking for in the membership
542 * list currently scanned. The key represents the alias
543 * member. Add that. */
545 member_string = strchr((const char *)key.dptr, '/');
547 /* Above we tested for MEMBEROF_PREFIX which includes the
550 SMB_ASSERT(member_string != NULL);
553 if (!string_to_sid(&member, member_string))
556 if (!add_sid_to_array(NULL, &member, closure->sids, closure->num)) {
565 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
568 struct aliasmem_closure closure;
570 if (!get_group_map_from_sid(*alias, &map))
571 return NT_STATUS_NO_SUCH_ALIAS;
573 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
574 (map.sid_name_use != SID_NAME_WKN_GRP) )
575 return NT_STATUS_NO_SUCH_ALIAS;
580 closure.alias = alias;
584 tdb_traverse(tdb, collect_aliasmem, &closure);
588 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
599 result = alias_memberships(member, 1, &sids, &num);
601 if (!NT_STATUS_IS_OK(result))
604 for (i=0; i<num; i++) {
605 if (sid_compare(&sids[i], alias) == 0) {
613 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
617 sids[i] = sids[num-1];
621 sid_to_string(sid_string, member);
622 if (asprintf(&key, "%s%s", MEMBEROF_PREFIX, sid_string) < 0) {
624 return NT_STATUS_NO_MEMORY;
628 NTSTATUS ret = (tdb_delete_bystring(tdb, key) == 0 ?
629 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
635 member_string = SMB_STRDUP("");
637 if (member_string == NULL) {
640 return NT_STATUS_NO_MEMORY;
643 for (i=0; i<num; i++) {
644 char *s = member_string;
646 sid_to_string(sid_string, &sids[i]);
647 asprintf(&member_string, "%s %s", s, sid_string);
650 if (member_string == NULL) {
653 return NT_STATUS_NO_MEMORY;
657 dbuf = string_term_tdb_data(member_string);
659 result = tdb_store_bystring(tdb, key, dbuf, 0) == 0 ?
660 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
663 SAFE_FREE(member_string);
670 static const struct mapping_backend tdb_backend = {
671 .add_mapping_entry = add_mapping_entry,
672 .get_group_map_from_sid = get_group_map_from_sid,
673 .get_group_map_from_gid = get_group_map_from_gid,
674 .get_group_map_from_ntname = get_group_map_from_ntname,
675 .group_map_remove = group_map_remove,
676 .enum_group_mapping = enum_group_mapping,
677 .one_alias_membership = one_alias_membership,
678 .add_aliasmem = add_aliasmem,
679 .del_aliasmem = del_aliasmem,
680 .enum_aliasmem = enum_aliasmem
684 initialise the tdb mapping backend
686 const struct mapping_backend *groupdb_tdb_init(void)
688 if (!init_group_mapping()) {
689 DEBUG(0,("Failed to initialise tdb mapping backend\n"));