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(lock_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)
95 fstring string_sid="";
98 sid_to_string(string_sid, &map->sid);
100 len = tdb_pack((uint8 *)buf, sizeof(buf), "ddff",
101 map->gid, map->sid_name_use, map->nt_name, map->comment);
103 if (len > sizeof(buf))
106 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
109 dbuf.dptr = (uint8 *)buf;
110 if (tdb_store_bystring(tdb, key, dbuf, flag) != 0) return False;
116 /****************************************************************************
117 Return the sid and the type of the unix group.
118 ****************************************************************************/
120 static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
127 /* the key is the SID, retrieving is direct */
129 sid_to_string(string_sid, &sid);
130 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
132 dbuf = tdb_fetch_bystring(tdb, key);
136 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
137 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
139 SAFE_FREE(dbuf.dptr);
142 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
146 sid_copy(&map->sid, &sid);
151 /****************************************************************************
152 Return the sid and the type of the unix group.
153 ****************************************************************************/
155 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
157 TDB_DATA kbuf, dbuf, newkey;
161 /* we need to enumerate the TDB to find the GID */
163 for (kbuf = tdb_firstkey(tdb);
165 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
167 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
169 dbuf = tdb_fetch(tdb, kbuf);
173 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
175 string_to_sid(&map->sid, string_sid);
177 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
178 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
180 SAFE_FREE(dbuf.dptr);
183 DEBUG(3,("get_group_map_from_gid: tdb_unpack failure\n"));
188 SAFE_FREE(kbuf.dptr);
196 /****************************************************************************
197 Return the sid and the type of the unix group.
198 ****************************************************************************/
200 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
202 TDB_DATA kbuf, dbuf, newkey;
206 /* we need to enumerate the TDB to find the name */
208 for (kbuf = tdb_firstkey(tdb);
210 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
212 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0) continue;
214 dbuf = tdb_fetch(tdb, kbuf);
218 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
220 string_to_sid(&map->sid, string_sid);
222 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
223 &map->gid, &map->sid_name_use, &map->nt_name, &map->comment);
225 SAFE_FREE(dbuf.dptr);
228 DEBUG(3,("get_group_map_from_ntname: tdb_unpack failure\n"));
232 if ( strequal(name, map->nt_name) ) {
233 SAFE_FREE(kbuf.dptr);
241 /****************************************************************************
242 Remove a group mapping entry.
243 ****************************************************************************/
245 static bool group_map_remove(const DOM_SID *sid)
251 /* the key is the SID, retrieving is direct */
253 sid_to_string(string_sid, sid);
254 slprintf(key, sizeof(key), "%s%s", GROUP_PREFIX, string_sid);
256 dbuf = tdb_fetch_bystring(tdb, key);
260 SAFE_FREE(dbuf.dptr);
262 if(tdb_delete_bystring(tdb, key) != TDB_SUCCESS)
268 /****************************************************************************
269 Enumerate the group mapping.
270 ****************************************************************************/
272 static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap,
273 size_t *p_num_entries, bool unix_only)
275 TDB_DATA kbuf, dbuf, newkey;
287 for (kbuf = tdb_firstkey(tdb);
289 newkey = tdb_nextkey(tdb, kbuf), safe_free(kbuf.dptr), kbuf=newkey) {
291 if (strncmp((const char *)kbuf.dptr, GROUP_PREFIX, strlen(GROUP_PREFIX)) != 0)
294 dbuf = tdb_fetch(tdb, kbuf);
298 fstrcpy(string_sid, (const char *)kbuf.dptr+strlen(GROUP_PREFIX));
300 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
301 &map.gid, &map.sid_name_use, &map.nt_name, &map.comment);
303 SAFE_FREE(dbuf.dptr);
306 DEBUG(3,("enum_group_mapping: tdb_unpack failure\n"));
310 /* list only the type or everything if UNKNOWN */
311 if (sid_name_use!=SID_NAME_UNKNOWN && sid_name_use!=map.sid_name_use) {
312 DEBUG(11,("enum_group_mapping: group %s is not of the requested type\n", map.nt_name));
316 if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
317 DEBUG(11,("enum_group_mapping: group %s is non mapped\n", map.nt_name));
321 string_to_sid(&grpsid, string_sid);
322 sid_copy( &map.sid, &grpsid );
324 sid_split_rid( &grpsid, &rid );
326 /* Only check the domain if we were given one */
328 if ( domsid && !sid_equal( domsid, &grpsid ) ) {
329 DEBUG(11,("enum_group_mapping: group %s is not in domain %s\n",
330 string_sid, sid_string_static(domsid)));
334 DEBUG(11,("enum_group_mapping: returning group %s of "
335 "type %s\n", map.nt_name,
336 sid_type_lookup(map.sid_name_use)));
338 (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
340 DEBUG(0,("enum_group_mapping: Unable to enlarge group map!\n"));
346 mapt[entries].gid = map.gid;
347 sid_copy( &mapt[entries].sid, &map.sid);
348 mapt[entries].sid_name_use = map.sid_name_use;
349 fstrcpy(mapt[entries].nt_name, map.nt_name);
350 fstrcpy(mapt[entries].comment, map.comment);
356 *p_num_entries=entries;
361 /* This operation happens on session setup, so it should better be fast. We
362 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
364 static NTSTATUS one_alias_membership(const DOM_SID *member,
365 DOM_SID **sids, size_t *num)
367 fstring key, string_sid;
371 sid_to_string(string_sid, member);
372 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
374 dbuf = tdb_fetch_bystring(tdb, key);
376 if (dbuf.dptr == NULL) {
380 p = (const char *)dbuf.dptr;
382 while (next_token(&p, string_sid, " ", sizeof(string_sid))) {
386 if (!string_to_sid(&alias, string_sid))
389 if (!add_sid_to_array_unique(NULL, &alias, sids, num)) {
390 return NT_STATUS_NO_MEMORY;
394 SAFE_FREE(dbuf.dptr);
398 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
399 DOM_SID **sids, size_t *num)
406 for (i=0; i<num_members; i++) {
407 NTSTATUS status = one_alias_membership(&members[i], sids, num);
408 if (!NT_STATUS_IS_OK(status))
414 static bool is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
419 /* This feels the wrong way round, but the on-disk data structure
420 * dictates it this way. */
421 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
424 for (i=0; i<num; i++) {
425 if (sid_compare(alias, &sids[i]) == 0) {
435 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
441 char *new_memberstring;
444 if (!get_group_map_from_sid(*alias, &map))
445 return NT_STATUS_NO_SUCH_ALIAS;
447 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
448 (map.sid_name_use != SID_NAME_WKN_GRP) )
449 return NT_STATUS_NO_SUCH_ALIAS;
451 if (is_aliasmem(alias, member))
452 return NT_STATUS_MEMBER_IN_ALIAS;
454 sid_to_string(string_sid, member);
455 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, string_sid);
457 dbuf = tdb_fetch_bystring(tdb, key);
459 sid_to_string(string_sid, alias);
461 if (dbuf.dptr != NULL) {
462 asprintf(&new_memberstring, "%s %s", (char *)(dbuf.dptr),
465 new_memberstring = SMB_STRDUP(string_sid);
468 if (new_memberstring == NULL)
469 return NT_STATUS_NO_MEMORY;
471 SAFE_FREE(dbuf.dptr);
472 dbuf = string_term_tdb_data(new_memberstring);
474 result = tdb_store_bystring(tdb, key, dbuf, 0);
476 SAFE_FREE(new_memberstring);
478 return (result == 0 ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED);
481 struct aliasmem_closure {
482 const DOM_SID *alias;
487 static int collect_aliasmem(TDB_CONTEXT *tdb_ctx, TDB_DATA key, TDB_DATA data,
490 struct aliasmem_closure *closure = (struct aliasmem_closure *)state;
492 fstring alias_string;
494 if (strncmp((const char *)key.dptr, MEMBEROF_PREFIX,
495 strlen(MEMBEROF_PREFIX)) != 0)
498 p = (const char *)data.dptr;
500 while (next_token(&p, alias_string, " ", sizeof(alias_string))) {
502 DOM_SID alias, member;
503 const char *member_string;
506 if (!string_to_sid(&alias, alias_string))
509 if (sid_compare(closure->alias, &alias) != 0)
512 /* Ok, we found the alias we're looking for in the membership
513 * list currently scanned. The key represents the alias
514 * member. Add that. */
516 member_string = strchr((const char *)key.dptr, '/');
518 /* Above we tested for MEMBEROF_PREFIX which includes the
521 SMB_ASSERT(member_string != NULL);
524 if (!string_to_sid(&member, member_string))
527 if (!add_sid_to_array(NULL, &member, closure->sids, closure->num)) {
536 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
539 struct aliasmem_closure closure;
541 if (!get_group_map_from_sid(*alias, &map))
542 return NT_STATUS_NO_SUCH_ALIAS;
544 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
545 (map.sid_name_use != SID_NAME_WKN_GRP) )
546 return NT_STATUS_NO_SUCH_ALIAS;
551 closure.alias = alias;
555 tdb_traverse(tdb, collect_aliasmem, &closure);
559 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
570 result = alias_memberships(member, 1, &sids, &num);
572 if (!NT_STATUS_IS_OK(result))
575 for (i=0; i<num; i++) {
576 if (sid_compare(&sids[i], alias) == 0) {
584 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
588 sids[i] = sids[num-1];
592 sid_to_string(sid_string, member);
593 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, sid_string);
596 return tdb_delete_bystring(tdb, key) == 0 ?
597 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
599 member_string = SMB_STRDUP("");
601 if (member_string == NULL) {
603 return NT_STATUS_NO_MEMORY;
606 for (i=0; i<num; i++) {
607 char *s = member_string;
609 sid_to_string(sid_string, &sids[i]);
610 asprintf(&member_string, "%s %s", s, sid_string);
613 if (member_string == NULL) {
615 return NT_STATUS_NO_MEMORY;
619 dbuf = string_term_tdb_data(member_string);
621 result = tdb_store_bystring(tdb, key, dbuf, 0) == 0 ?
622 NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
625 SAFE_FREE(member_string);
631 static const struct mapping_backend tdb_backend = {
632 .add_mapping_entry = add_mapping_entry,
633 .get_group_map_from_sid = get_group_map_from_sid,
634 .get_group_map_from_gid = get_group_map_from_gid,
635 .get_group_map_from_ntname = get_group_map_from_ntname,
636 .group_map_remove = group_map_remove,
637 .enum_group_mapping = enum_group_mapping,
638 .one_alias_membership = one_alias_membership,
639 .add_aliasmem = add_aliasmem,
640 .del_aliasmem = del_aliasmem,
641 .enum_aliasmem = enum_aliasmem
645 initialise the tdb mapping backend
647 const struct mapping_backend *groupdb_tdb_init(void)
649 if (!init_group_mapping()) {
650 DEBUG(0,("Failed to initialise tdb mapping backend\n"));