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, 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)
41 db = db_open(NULL, state_path("group_mapping.tdb"), 0, TDB_DEFAULT,
42 O_RDWR|O_CREAT, 0600);
44 DEBUG(0, ("Failed to open group mapping database: %s\n",
51 * This code was designed to handle a group mapping version
52 * upgrade. mapping_tdb is not active by default anymore, so ignore
56 const char *vstring = "INFO/version";
58 GROUP_MAP *map_table = NULL;
59 size_t num_entries = 0;
61 /* handle a Samba upgrade */
62 tdb_lock_bystring(tdb, vstring);
64 /* Cope with byte-reversed older versions of the db. */
65 vers_id = tdb_fetch_int32(tdb, vstring);
66 if ((vers_id == DATABASE_VERSION_V1)
67 || (IREV(vers_id) == DATABASE_VERSION_V1)) {
69 * Written on a bigendian machine with old fetch_int
72 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
73 vers_id = DATABASE_VERSION_V2;
76 /* if its an unknown version we remove everthing in the db */
78 if (vers_id != DATABASE_VERSION_V2) {
80 tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2);
83 tdb_unlock_bystring(tdb, vstring);
85 /* cleanup any map entries with a gid == -1 */
87 if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table,
88 &num_entries, False ) ) {
91 for ( i=0; i<num_entries; i++ ) {
92 if ( map_table[i].gid == -1 ) {
93 group_map_remove( &map_table[i].sid );
97 SAFE_FREE( map_table );
105 static char *group_mapping_key(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
107 char *sidstr, *result;
109 sidstr = sid_string_talloc(talloc_tos(), sid);
110 if (sidstr == NULL) {
114 result = talloc_asprintf(mem_ctx, "GROUP_PREFIX%s", sidstr);
120 /****************************************************************************
121 ****************************************************************************/
122 static bool add_mapping_entry(GROUP_MAP *map, int flag)
128 key = group_mapping_key(talloc_tos(), &map->sid);
133 len = tdb_pack(NULL, sizeof(buf), "ddff",
134 map->gid, map->sid_name_use, map->nt_name, map->comment);
136 buf = TALLOC_ARRAY(key, char, len);
141 len = tdb_pack((uint8 *)buf, len, "ddff", map->gid,
142 map->sid_name_use, map->nt_name, map->comment);
144 status = dbwrap_store_bystring(
145 db, key, make_tdb_data((uint8_t *)buf, len), flag);
149 return NT_STATUS_IS_OK(status);
153 /****************************************************************************
154 Return the sid and the type of the unix group.
155 ****************************************************************************/
157 static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map)
163 /* the key is the SID, retrieving is direct */
165 key = group_mapping_key(talloc_tos(), &map->sid);
170 dbuf = dbwrap_fetch_bystring(db, key, key);
171 if (dbuf.dptr == NULL) {
176 ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff",
177 &map->gid, &map->sid_name_use,
178 &map->nt_name, &map->comment);
183 DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n"));
187 sid_copy(&map->sid, &sid);
192 static bool dbrec2map(const struct db_record *rec, GROUP_MAP *map)
194 if ((rec->key.dsize < strlen(GROUP_PREFIX))
195 || (strncmp((char *)rec->key.dptr, GROUP_PREFIX,
196 GROUP_PREFIX_LEN) != 0)) {
200 if (!string_to_sid(&map->sid, (const char *)rec->key.dptr
201 + GROUP_PREFIX_LEN)) {
205 return tdb_unpack(rec->value.dptr, rec->value.dsize, "ddff",
206 &map->gid, &map->sid_name_use, &map->nt_name,
207 &map->comment) != -1;
210 struct find_map_state {
212 const char *name; /* If != NULL, look for name */
213 gid_t gid; /* valid iff name == NULL */
217 static int find_map(struct db_record *rec, void *private_data)
219 struct find_map_state *state = (struct find_map_state *)private_data;
221 if (!dbrec2map(rec, state->map)) {
222 DEBUG(10, ("failed to unpack map\n"));
226 if (state->name != NULL) {
227 if (strequal(state->name, state->map->nt_name)) {
233 if (state->map->gid == state->gid) {
242 /****************************************************************************
243 Return the sid and the type of the unix group.
244 ****************************************************************************/
246 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
248 struct find_map_state state;
251 state.name = NULL; /* Indicate we're looking for gid */
255 db->traverse_read(db, find_map, (void *)&state);
260 /****************************************************************************
261 Return the sid and the type of the unix group.
262 ****************************************************************************/
264 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
266 struct find_map_state state;
272 db->traverse_read(db, find_map, (void *)&state);
277 /****************************************************************************
278 Remove a group mapping entry.
279 ****************************************************************************/
281 static bool group_map_remove(const DOM_SID *sid)
286 key = group_mapping_key(talloc_tos(), sid);
291 result = NT_STATUS_IS_OK(dbwrap_delete_bystring(db, key));
297 /****************************************************************************
298 Enumerate the group mapping.
299 ****************************************************************************/
301 struct enum_map_state {
302 const DOM_SID *domsid;
303 enum lsa_SidType sid_name_use;
310 static int collect_map(struct db_record *rec, void *private_data)
312 struct enum_map_state *state = (struct enum_map_state *)private_data;
316 if (!dbrec2map(rec, &map)) {
319 /* list only the type or everything if UNKNOWN */
320 if (state->sid_name_use != SID_NAME_UNKNOWN
321 && state->sid_name_use != map.sid_name_use) {
322 DEBUG(11,("enum_group_mapping: group %s is not of the "
323 "requested type\n", map.nt_name));
327 if ((state->unix_only == ENUM_ONLY_MAPPED) && (map.gid == -1)) {
328 DEBUG(11,("enum_group_mapping: group %s is non mapped\n",
333 if ((state->domsid != NULL) &&
334 (sid_compare_domain(state->domsid, &map.sid) != 0)) {
335 DEBUG(11,("enum_group_mapping: group %s is not in domain\n",
336 sid_string_dbg(&map.sid)));
340 if (!(tmp = SMB_REALLOC_ARRAY(state->maps, GROUP_MAP,
341 state->num_maps+1))) {
342 DEBUG(0,("enum_group_mapping: Unable to enlarge group "
348 state->maps[state->num_maps] = map;
353 static bool enum_group_mapping(const DOM_SID *domsid,
354 enum lsa_SidType sid_name_use,
356 size_t *p_num_entries, bool unix_only)
358 struct enum_map_state state;
360 state.domsid = domsid;
361 state.sid_name_use = sid_name_use;
362 state.unix_only = unix_only;
366 if (db->traverse_read(db, collect_map, (void *)&state) != 0) {
370 *pp_rmap = state.maps;
371 *p_num_entries = state.num_maps;
376 /* This operation happens on session setup, so it should better be fast. We
377 * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */
379 static NTSTATUS one_alias_membership(const DOM_SID *member,
380 DOM_SID **sids, size_t *num)
387 NTSTATUS status = NT_STATUS_OK;
388 TALLOC_CTX *frame = talloc_stackframe();
390 slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX,
391 sid_to_fstring(tmp, member));
393 dbuf = dbwrap_fetch_bystring(db, frame, key);
394 if (dbuf.dptr == NULL) {
399 p = (const char *)dbuf.dptr;
401 while (next_token_talloc(frame, &p, &string_sid, " ")) {
404 if (!string_to_sid(&alias, string_sid))
407 status= add_sid_to_array_unique(NULL, &alias, sids, num);
408 if (!NT_STATUS_IS_OK(status)) {
418 static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members,
419 DOM_SID **sids, size_t *num)
426 for (i=0; i<num_members; i++) {
427 NTSTATUS status = one_alias_membership(&members[i], sids, num);
428 if (!NT_STATUS_IS_OK(status))
434 static bool is_aliasmem(const DOM_SID *alias, const DOM_SID *member)
439 /* This feels the wrong way round, but the on-disk data structure
440 * dictates it this way. */
441 if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num)))
444 for (i=0; i<num; i++) {
445 if (sid_compare(alias, &sids[i]) == 0) {
455 static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member)
460 char *new_memberstring;
461 struct db_record *rec;
464 if (!get_group_map_from_sid(*alias, &map))
465 return NT_STATUS_NO_SUCH_ALIAS;
467 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
468 (map.sid_name_use != SID_NAME_WKN_GRP) )
469 return NT_STATUS_NO_SUCH_ALIAS;
471 if (is_aliasmem(alias, member))
472 return NT_STATUS_MEMBER_IN_ALIAS;
474 sid_to_fstring(string_sid, member);
476 key = talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX,
479 return NT_STATUS_NO_MEMORY;
482 rec = db->fetch_locked(db, key, string_term_tdb_data(key));
485 DEBUG(10, ("fetch_lock failed\n"));
487 return NT_STATUS_INTERNAL_DB_CORRUPTION;
490 sid_to_fstring(string_sid, alias);
492 if (rec->value.dptr != NULL) {
493 new_memberstring = talloc_asprintf(
494 key, "%s %s", (char *)(rec->value.dptr), string_sid);
496 new_memberstring = talloc_strdup(key, string_sid);
499 if (new_memberstring == NULL) {
501 return NT_STATUS_NO_MEMORY;
504 status = rec->store(rec, string_term_tdb_data(new_memberstring), 0);
508 if (!NT_STATUS_IS_OK(status)) {
509 DEBUG(10, ("Could not store record: %s\n", nt_errstr(status)));
515 struct aliasmem_state {
516 const DOM_SID *alias;
521 static int collect_aliasmem(struct db_record *rec, void *priv)
523 struct aliasmem_state *state = (struct aliasmem_state *)priv;
528 if (strncmp((const char *)rec->key.dptr, MEMBEROF_PREFIX,
529 MEMBEROF_PREFIX_LEN) != 0)
532 p = (const char *)rec->value.dptr;
534 frame = talloc_stackframe();
536 while (next_token_talloc(frame, &p, &alias_string, " ")) {
537 DOM_SID alias, member;
538 const char *member_string;
540 if (!string_to_sid(&alias, alias_string))
543 if (sid_compare(state->alias, &alias) != 0)
546 /* Ok, we found the alias we're looking for in the membership
547 * list currently scanned. The key represents the alias
548 * member. Add that. */
550 member_string = strchr((const char *)rec->key.dptr, '/');
552 /* Above we tested for MEMBEROF_PREFIX which includes the
555 SMB_ASSERT(member_string != NULL);
558 if (!string_to_sid(&member, member_string))
561 if (!NT_STATUS_IS_OK(add_sid_to_array(NULL, &member,
574 static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num)
577 struct aliasmem_state state;
579 if (!get_group_map_from_sid(*alias, &map))
580 return NT_STATUS_NO_SUCH_ALIAS;
582 if ( (map.sid_name_use != SID_NAME_ALIAS) &&
583 (map.sid_name_use != SID_NAME_WKN_GRP) )
584 return NT_STATUS_NO_SUCH_ALIAS;
593 db->traverse_read(db, collect_aliasmem, &state);
597 static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member)
607 status = alias_memberships(member, 1, &sids, &num);
609 if (!NT_STATUS_IS_OK(status))
612 for (i=0; i<num; i++) {
613 if (sid_compare(&sids[i], alias) == 0) {
621 return NT_STATUS_MEMBER_NOT_IN_ALIAS;
625 sids[i] = sids[num-1];
629 sid_to_fstring(sid_string, member);
631 key = talloc_asprintf(sids, "%s%s", MEMBEROF_PREFIX, sid_string);
634 return NT_STATUS_NO_MEMORY;
638 status = dbwrap_delete_bystring(db, key);
643 member_string = talloc_strdup(sids, "");
644 if (member_string == NULL) {
646 return NT_STATUS_NO_MEMORY;
649 for (i=0; i<num; i++) {
651 sid_to_fstring(sid_string, &sids[i]);
653 member_string = talloc_asprintf_append_buffer(
654 member_string, " %s", sid_string);
656 if (member_string == NULL) {
658 return NT_STATUS_NO_MEMORY;
662 status = dbwrap_store_bystring(
663 db, key, string_term_tdb_data(member_string), 0);
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"));