diff options
Diffstat (limited to 'source3/groupdb')
-rw-r--r-- | source3/groupdb/mapping.c | 808 | ||||
-rw-r--r-- | source3/groupdb/mapping.h | 33 | ||||
-rw-r--r-- | source3/groupdb/mapping_ldb.c | 699 | ||||
-rw-r--r-- | source3/groupdb/mapping_tdb.c | 744 |
4 files changed, 2284 insertions, 0 deletions
diff --git a/source3/groupdb/mapping.c b/source3/groupdb/mapping.c new file mode 100644 index 0000000000..b952cda523 --- /dev/null +++ b/source3/groupdb/mapping.c @@ -0,0 +1,808 @@ +/* + * Unix SMB/CIFS implementation. + * RPC Pipe client / server routines + * Copyright (C) Andrew Tridgell 1992-2000, + * Copyright (C) Jean François Micouleau 1998-2001. + * Copyright (C) Volker Lendecke 2006. + * Copyright (C) Gerald Carter 2006. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + */ + +#include "includes.h" +#include "groupdb/mapping.h" + +static const struct mapping_backend *backend; + +/* + initialise a group mapping backend + */ +static bool init_group_mapping(void) +{ + const char *backend_string; + + if (backend != NULL) { + /* already initialised */ + return True; + } + + /* + * default to using the ldb backend. This parameter should + * disappear in future versions of Samba3. + * + * But it's needed for cluster setups, because it's + * not yet possible to distribute a ldb inside a cluster. + */ + backend_string = lp_parm_const_string(-1, "groupdb", "backend", "ldb"); + + if (strcmp(backend_string, "ldb") == 0) { + backend = groupdb_ldb_init(); + } else if (strcmp(backend_string, "tdb") == 0) { + backend = groupdb_tdb_init(); + } else { + DEBUG(0,("Unknown groupdb backend '%s'\n", backend_string)); + smb_panic("Unknown groupdb backend"); + } + + return backend != NULL; +} + +/**************************************************************************** +initialise first time the mapping list +****************************************************************************/ +NTSTATUS add_initial_entry(gid_t gid, const char *sid, enum lsa_SidType sid_name_use, const char *nt_name, const char *comment) +{ + GROUP_MAP map; + + if(!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + + map.gid=gid; + if (!string_to_sid(&map.sid, sid)) { + DEBUG(0, ("string_to_sid failed: %s", sid)); + return NT_STATUS_UNSUCCESSFUL; + } + + map.sid_name_use=sid_name_use; + fstrcpy(map.nt_name, nt_name); + fstrcpy(map.comment, comment); + + return pdb_add_group_mapping_entry(&map); +} + +static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members, + DOM_SID **sids, size_t *num) +{ + size_t i; + + *num = 0; + *sids = NULL; + + for (i=0; i<num_members; i++) { + NTSTATUS status = backend->one_alias_membership(&members[i], sids, num); + if (!NT_STATUS_IS_OK(status)) + return status; + } + return NT_STATUS_OK; +} + +struct aliasmem_closure { + const DOM_SID *alias; + DOM_SID **sids; + size_t *num; +}; + + + +/* + * + * High level functions + * better to use them than the lower ones. + * + * we are checking if the group is in the mapping file + * and if the group is an existing unix group + * + */ + +/* get a domain group from it's SID */ + +bool get_domain_group_from_sid(DOM_SID sid, GROUP_MAP *map) +{ + struct group *grp; + bool ret; + + if(!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return(False); + } + + DEBUG(10, ("get_domain_group_from_sid\n")); + + /* if the group is NOT in the database, it CAN NOT be a domain group */ + + become_root(); + ret = pdb_getgrsid(map, sid); + unbecome_root(); + + /* special case check for rid 513 */ + + if ( !ret ) { + uint32 rid; + + sid_peek_rid( &sid, &rid ); + + if ( rid == DOMAIN_GROUP_RID_USERS ) { + fstrcpy( map->nt_name, "None" ); + fstrcpy( map->comment, "Ordinary Users" ); + sid_copy( &map->sid, &sid ); + map->sid_name_use = SID_NAME_DOM_GRP; + map->gid = (gid_t)-1; + + return True; + } + + return False; + } + + DEBUG(10, ("get_domain_group_from_sid: SID found in the TDB\n")); + + /* if it's not a domain group, continue */ + if (map->sid_name_use!=SID_NAME_DOM_GRP) { + return False; + } + + DEBUG(10, ("get_domain_group_from_sid: SID is a domain group\n")); + + if (map->gid==-1) { + return False; + } + + DEBUG(10, ("get_domain_group_from_sid: SID is mapped to gid:%lu\n",(unsigned long)map->gid)); + + grp = getgrgid(map->gid); + if ( !grp ) { + DEBUG(10, ("get_domain_group_from_sid: gid DOESN'T exist in UNIX security\n")); + return False; + } + + DEBUG(10, ("get_domain_group_from_sid: gid exists in UNIX security\n")); + + return True; +} + +/**************************************************************************** + Create a UNIX group on demand. +****************************************************************************/ + +int smb_create_group(const char *unix_group, gid_t *new_gid) +{ + char *add_script = NULL; + int ret = -1; + int fd = 0; + + *new_gid = 0; + + /* defer to scripts */ + + if ( *lp_addgroup_script() ) { + TALLOC_CTX *ctx = talloc_tos(); + + add_script = talloc_strdup(ctx, + lp_addgroup_script()); + if (!add_script) { + return -1; + } + add_script = talloc_string_sub(ctx, + add_script, "%g", unix_group); + if (!add_script) { + return -1; + } + + ret = smbrun(add_script, &fd); + DEBUG(ret ? 0 : 3,("smb_create_group: Running the command `%s' gave %d\n",add_script,ret)); + if (ret == 0) { + smb_nscd_flush_group_cache(); + } + if (ret != 0) + return ret; + + if (fd != 0) { + fstring output; + + *new_gid = 0; + if (read(fd, output, sizeof(output)) > 0) { + *new_gid = (gid_t)strtoul(output, NULL, 10); + } + + close(fd); + } + + } + + if (*new_gid == 0) { + struct group *grp = getgrnam(unix_group); + + if (grp != NULL) + *new_gid = grp->gr_gid; + } + + return ret; +} + +/**************************************************************************** + Delete a UNIX group on demand. +****************************************************************************/ + +int smb_delete_group(const char *unix_group) +{ + char *del_script = NULL; + int ret = -1; + + /* defer to scripts */ + + if ( *lp_delgroup_script() ) { + TALLOC_CTX *ctx = talloc_tos(); + + del_script = talloc_strdup(ctx, + lp_delgroup_script()); + if (!del_script) { + return -1; + } + del_script = talloc_string_sub(ctx, + del_script, "%g", unix_group); + if (!del_script) { + return -1; + } + ret = smbrun(del_script,NULL); + DEBUG(ret ? 0 : 3,("smb_delete_group: Running the command `%s' gave %d\n",del_script,ret)); + if (ret == 0) { + smb_nscd_flush_group_cache(); + } + return ret; + } + + return -1; +} + +/**************************************************************************** + Set a user's primary UNIX group. +****************************************************************************/ + +int smb_set_primary_group(const char *unix_group, const char* unix_user) +{ + char *add_script = NULL; + int ret = -1; + + /* defer to scripts */ + + if ( *lp_setprimarygroup_script() ) { + TALLOC_CTX *ctx = talloc_tos(); + + add_script = talloc_strdup(ctx, + lp_setprimarygroup_script()); + if (!add_script) { + return -1; + } + add_script = talloc_all_string_sub(ctx, + add_script, "%g", unix_group); + if (!add_script) { + return -1; + } + add_script = talloc_string_sub(ctx, + add_script, "%u", unix_user); + if (!add_script) { + return -1; + } + ret = smbrun(add_script,NULL); + flush_pwnam_cache(); + DEBUG(ret ? 0 : 3,("smb_set_primary_group: " + "Running the command `%s' gave %d\n",add_script,ret)); + if (ret == 0) { + smb_nscd_flush_group_cache(); + } + return ret; + } + + return -1; +} + +/**************************************************************************** + Add a user to a UNIX group. +****************************************************************************/ + +int smb_add_user_group(const char *unix_group, const char *unix_user) +{ + char *add_script = NULL; + int ret = -1; + + /* defer to scripts */ + + if ( *lp_addusertogroup_script() ) { + TALLOC_CTX *ctx = talloc_tos(); + + add_script = talloc_strdup(ctx, + lp_addusertogroup_script()); + if (!add_script) { + return -1; + } + add_script = talloc_string_sub(ctx, + add_script, "%g", unix_group); + if (!add_script) { + return -1; + } + add_script = talloc_string_sub(ctx, + add_script, "%u", unix_user); + if (!add_script) { + return -1; + } + ret = smbrun(add_script,NULL); + DEBUG(ret ? 0 : 3,("smb_add_user_group: Running the command `%s' gave %d\n",add_script,ret)); + if (ret == 0) { + smb_nscd_flush_group_cache(); + } + return ret; + } + + return -1; +} + +/**************************************************************************** + Delete a user from a UNIX group +****************************************************************************/ + +int smb_delete_user_group(const char *unix_group, const char *unix_user) +{ + char *del_script = NULL; + int ret = -1; + + /* defer to scripts */ + + if ( *lp_deluserfromgroup_script() ) { + TALLOC_CTX *ctx = talloc_tos(); + + del_script = talloc_strdup(ctx, + lp_deluserfromgroup_script()); + if (!del_script) { + return -1; + } + del_script = talloc_string_sub(ctx, + del_script, "%g", unix_group); + if (!del_script) { + return -1; + } + del_script = talloc_string_sub(ctx, + del_script, "%u", unix_user); + if (!del_script) { + return -1; + } + ret = smbrun(del_script,NULL); + DEBUG(ret ? 0 : 3,("smb_delete_user_group: Running the command `%s' gave %d\n",del_script,ret)); + if (ret == 0) { + smb_nscd_flush_group_cache(); + } + return ret; + } + + return -1; +} + + +NTSTATUS pdb_default_getgrsid(struct pdb_methods *methods, GROUP_MAP *map, + DOM_SID sid) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->get_group_map_from_sid(sid, map) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_default_getgrgid(struct pdb_methods *methods, GROUP_MAP *map, + gid_t gid) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->get_group_map_from_gid(gid, map) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_default_getgrnam(struct pdb_methods *methods, GROUP_MAP *map, + const char *name) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->get_group_map_from_ntname(name, map) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_default_add_group_mapping_entry(struct pdb_methods *methods, + GROUP_MAP *map) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->add_mapping_entry(map, TDB_INSERT) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_default_update_group_mapping_entry(struct pdb_methods *methods, + GROUP_MAP *map) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->add_mapping_entry(map, TDB_REPLACE) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_default_delete_group_mapping_entry(struct pdb_methods *methods, + DOM_SID sid) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->group_map_remove(&sid) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_default_enum_group_mapping(struct pdb_methods *methods, + const DOM_SID *sid, enum lsa_SidType sid_name_use, + GROUP_MAP **pp_rmap, size_t *p_num_entries, + bool unix_only) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->enum_group_mapping(sid, sid_name_use, pp_rmap, p_num_entries, unix_only) ? + NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_default_create_alias(struct pdb_methods *methods, + const char *name, uint32 *rid) +{ + DOM_SID sid; + enum lsa_SidType type; + uint32 new_rid; + gid_t gid; + bool exists; + GROUP_MAP map; + TALLOC_CTX *mem_ctx; + NTSTATUS status; + + DEBUG(10, ("Trying to create alias %s\n", name)); + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + return NT_STATUS_NO_MEMORY; + } + + exists = lookup_name(mem_ctx, name, LOOKUP_NAME_LOCAL, + NULL, NULL, &sid, &type); + TALLOC_FREE(mem_ctx); + + if (exists) { + return NT_STATUS_ALIAS_EXISTS; + } + + if (!winbind_allocate_gid(&gid)) { + DEBUG(3, ("Could not get a gid out of winbind\n")); + return NT_STATUS_ACCESS_DENIED; + } + + if (!pdb_new_rid(&new_rid)) { + DEBUG(0, ("Could not allocate a RID -- wasted a gid :-(\n")); + return NT_STATUS_ACCESS_DENIED; + } + + DEBUG(10, ("Creating alias %s with gid %d and rid %d\n", + name, gid, new_rid)); + + sid_copy(&sid, get_global_sam_sid()); + sid_append_rid(&sid, new_rid); + + map.gid = gid; + sid_copy(&map.sid, &sid); + map.sid_name_use = SID_NAME_ALIAS; + fstrcpy(map.nt_name, name); + fstrcpy(map.comment, ""); + + status = pdb_add_group_mapping_entry(&map); + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0, ("Could not add group mapping entry for alias %s " + "(%s)\n", name, nt_errstr(status))); + return status; + } + + *rid = new_rid; + + return NT_STATUS_OK; +} + +NTSTATUS pdb_default_delete_alias(struct pdb_methods *methods, + const DOM_SID *sid) +{ + return pdb_delete_group_mapping_entry(*sid); +} + +NTSTATUS pdb_default_get_aliasinfo(struct pdb_methods *methods, + const DOM_SID *sid, + struct acct_info *info) +{ + GROUP_MAP map; + + if (!pdb_getgrsid(&map, *sid)) + return NT_STATUS_NO_SUCH_ALIAS; + + if ((map.sid_name_use != SID_NAME_ALIAS) && + (map.sid_name_use != SID_NAME_WKN_GRP)) { + DEBUG(2, ("%s is a %s, expected an alias\n", + sid_string_dbg(sid), + sid_type_lookup(map.sid_name_use))); + return NT_STATUS_NO_SUCH_ALIAS; + } + + fstrcpy(info->acct_name, map.nt_name); + fstrcpy(info->acct_desc, map.comment); + sid_peek_rid(&map.sid, &info->rid); + return NT_STATUS_OK; +} + +NTSTATUS pdb_default_set_aliasinfo(struct pdb_methods *methods, + const DOM_SID *sid, + struct acct_info *info) +{ + GROUP_MAP map; + + if (!pdb_getgrsid(&map, *sid)) + return NT_STATUS_NO_SUCH_ALIAS; + + fstrcpy(map.nt_name, info->acct_name); + fstrcpy(map.comment, info->acct_desc); + + return pdb_update_group_mapping_entry(&map); +} + +NTSTATUS pdb_default_add_aliasmem(struct pdb_methods *methods, + const DOM_SID *alias, const DOM_SID *member) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->add_aliasmem(alias, member); +} + +NTSTATUS pdb_default_del_aliasmem(struct pdb_methods *methods, + const DOM_SID *alias, const DOM_SID *member) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->del_aliasmem(alias, member); +} + +NTSTATUS pdb_default_enum_aliasmem(struct pdb_methods *methods, + const DOM_SID *alias, DOM_SID **pp_members, + size_t *p_num_members) +{ + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + return backend->enum_aliasmem(alias, pp_members, p_num_members); +} + +NTSTATUS pdb_default_alias_memberships(struct pdb_methods *methods, + TALLOC_CTX *mem_ctx, + const DOM_SID *domain_sid, + const DOM_SID *members, + size_t num_members, + uint32 **pp_alias_rids, + size_t *p_num_alias_rids) +{ + DOM_SID *alias_sids; + size_t i, num_alias_sids; + NTSTATUS result; + + if (!init_group_mapping()) { + DEBUG(0,("failed to initialize group mapping\n")); + return NT_STATUS_UNSUCCESSFUL; + } + + alias_sids = NULL; + num_alias_sids = 0; + + result = alias_memberships(members, num_members, + &alias_sids, &num_alias_sids); + + if (!NT_STATUS_IS_OK(result)) + return result; + + *p_num_alias_rids = 0; + + if (num_alias_sids == 0) { + TALLOC_FREE(alias_sids); + return NT_STATUS_OK; + } + + *pp_alias_rids = TALLOC_ARRAY(mem_ctx, uint32, num_alias_sids); + if (*pp_alias_rids == NULL) + return NT_STATUS_NO_MEMORY; + + for (i=0; i<num_alias_sids; i++) { + if (!sid_peek_check_rid(domain_sid, &alias_sids[i], + &(*pp_alias_rids)[*p_num_alias_rids])) + continue; + *p_num_alias_rids += 1; + } + + TALLOC_FREE(alias_sids); + + return NT_STATUS_OK; +} + +/********************************************************************** + no ops for passdb backends that don't implement group mapping + *********************************************************************/ + +NTSTATUS pdb_nop_getgrsid(struct pdb_methods *methods, GROUP_MAP *map, + DOM_SID sid) +{ + return NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_nop_getgrgid(struct pdb_methods *methods, GROUP_MAP *map, + gid_t gid) +{ + return NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_nop_getgrnam(struct pdb_methods *methods, GROUP_MAP *map, + const char *name) +{ + return NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_nop_add_group_mapping_entry(struct pdb_methods *methods, + GROUP_MAP *map) +{ + return NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_nop_update_group_mapping_entry(struct pdb_methods *methods, + GROUP_MAP *map) +{ + return NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_nop_delete_group_mapping_entry(struct pdb_methods *methods, + DOM_SID sid) +{ + return NT_STATUS_UNSUCCESSFUL; +} + +NTSTATUS pdb_nop_enum_group_mapping(struct pdb_methods *methods, + enum lsa_SidType sid_name_use, + GROUP_MAP **rmap, size_t *num_entries, + bool unix_only) +{ + return NT_STATUS_UNSUCCESSFUL; +} + +/**************************************************************************** + These need to be redirected through pdb_interface.c +****************************************************************************/ +bool pdb_get_dom_grp_info(const DOM_SID *sid, struct acct_info *info) +{ + GROUP_MAP map; + bool res; + + become_root(); + res = get_domain_group_from_sid(*sid, &map); + unbecome_root(); + + if (!res) + return False; + + fstrcpy(info->acct_name, map.nt_name); + fstrcpy(info->acct_desc, map.comment); + sid_peek_rid(sid, &info->rid); + return True; +} + +bool pdb_set_dom_grp_info(const DOM_SID *sid, const struct acct_info *info) +{ + GROUP_MAP map; + + if (!get_domain_group_from_sid(*sid, &map)) + return False; + + fstrcpy(map.nt_name, info->acct_name); + fstrcpy(map.comment, info->acct_desc); + + return NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map)); +} + +/******************************************************************** + Really just intended to be called by smbd +********************************************************************/ + +NTSTATUS pdb_create_builtin_alias(uint32 rid) +{ + DOM_SID sid; + enum lsa_SidType type; + gid_t gid; + GROUP_MAP map; + TALLOC_CTX *mem_ctx; + NTSTATUS status; + const char *name = NULL; + fstring groupname; + + DEBUG(10, ("Trying to create builtin alias %d\n", rid)); + + if ( !sid_compose( &sid, &global_sid_Builtin, rid ) ) { + return NT_STATUS_NO_SUCH_ALIAS; + } + + if ( (mem_ctx = talloc_new(NULL)) == NULL ) { + return NT_STATUS_NO_MEMORY; + } + + if ( !lookup_sid(mem_ctx, &sid, NULL, &name, &type) ) { + TALLOC_FREE( mem_ctx ); + return NT_STATUS_NO_SUCH_ALIAS; + } + + /* validate RID so copy the name and move on */ + + fstrcpy( groupname, name ); + TALLOC_FREE( mem_ctx ); + + if (!winbind_allocate_gid(&gid)) { + DEBUG(3, ("pdb_create_builtin_alias: Could not get a gid out of winbind\n")); + return NT_STATUS_ACCESS_DENIED; + } + + DEBUG(10,("Creating alias %s with gid %d\n", groupname, gid)); + + map.gid = gid; + sid_copy(&map.sid, &sid); + map.sid_name_use = SID_NAME_ALIAS; + fstrcpy(map.nt_name, groupname); + fstrcpy(map.comment, ""); + + status = pdb_add_group_mapping_entry(&map); + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0, ("pdb_create_builtin_alias: Could not add group mapping entry for alias %d " + "(%s)\n", rid, nt_errstr(status))); + } + + return status; +} + + diff --git a/source3/groupdb/mapping.h b/source3/groupdb/mapping.h new file mode 100644 index 0000000000..c37ae84b87 --- /dev/null +++ b/source3/groupdb/mapping.h @@ -0,0 +1,33 @@ +#define DATABASE_VERSION_V1 1 /* native byte format. */ +#define DATABASE_VERSION_V2 2 /* le format. */ + +#define GROUP_PREFIX "UNIXGROUP/" +#define GROUP_PREFIX_LEN 10 + +/* Alias memberships are stored reverse, as memberships. The performance + * critical operation is to determine the aliases a SID is member of, not + * listing alias members. So we store a list of alias SIDs a SID is member of + * hanging of the member as key. + */ +#define MEMBEROF_PREFIX "MEMBEROF/" +#define MEMBEROF_PREFIX_LEN 9 + +/* + groupdb mapping backend abstraction + */ +struct mapping_backend { + bool (*init_group_mapping)(void); + bool (*add_mapping_entry)(GROUP_MAP *map, int flag); + bool (*get_group_map_from_sid)(DOM_SID sid, GROUP_MAP *map); + bool (*get_group_map_from_gid)(gid_t gid, GROUP_MAP *map); + bool (*get_group_map_from_ntname)(const char *name, GROUP_MAP *map); + bool (*group_map_remove)(const DOM_SID *sid); + bool (*enum_group_mapping)(const DOM_SID *domsid, enum lsa_SidType sid_name_use, + GROUP_MAP **pp_rmap, + size_t *p_num_entries, bool unix_only); + NTSTATUS (*one_alias_membership)(const DOM_SID *member, + DOM_SID **sids, size_t *num); + NTSTATUS (*add_aliasmem)(const DOM_SID *alias, const DOM_SID *member); + NTSTATUS (*del_aliasmem)(const DOM_SID *alias, const DOM_SID *member); + NTSTATUS (*enum_aliasmem)(const DOM_SID *alias, DOM_SID **sids, size_t *num); +}; diff --git a/source3/groupdb/mapping_ldb.c b/source3/groupdb/mapping_ldb.c new file mode 100644 index 0000000000..7ce879fb6e --- /dev/null +++ b/source3/groupdb/mapping_ldb.c @@ -0,0 +1,699 @@ +/* + * Unix SMB/CIFS implementation. + * + * group mapping code on top of ldb + * + * Copyright (C) Andrew Tridgell 2006 + * + * based on tdb group mapping code from groupdb/mapping.c + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + */ + +#include "includes.h" +#include "groupdb/mapping.h" +#include "lib/ldb/include/includes.h" +#include "lib/ldb/include/ldb_errors.h" + +static struct ldb_context *ldb; + +static bool mapping_upgrade(const char *tdb_path); + +/* + connect to the group mapping ldb +*/ +static bool init_group_mapping(void) +{ + bool existed; + const char *init_ldif[] = + { "dn: @ATTRIBUTES\n" \ + "ntName: CASE_INSENSITIVE\n" \ + "\n", + "dn: @INDEXLIST\n" \ + "@IDXATTR: gidNumber\n" \ + "@IDXATTR: ntName\n" \ + "@IDXATTR: member\n" }; + const char *db_path, *tdb_path; + int ret; + int flags = 0; + + if (ldb != NULL) { + return True; + } + + /* this is needed as Samba3 doesn't have this globally yet */ + ldb_global_init(); + + db_path = state_path("group_mapping.ldb"); + + ldb = ldb_init(NULL); + if (ldb == NULL) goto failed; + + /* Ensure this db is created read/write for root only. */ + ldb_set_create_perms(ldb, 0600); + + existed = file_exist(db_path, NULL); + + if (lp_parm_bool(-1, "groupmap", "nosync", False)) { + flags |= LDB_FLG_NOSYNC; + } + + if (!lp_use_mmap()) { + flags |= LDB_FLG_NOMMAP; + } + + ret = ldb_connect(ldb, db_path, flags, NULL); + if (ret != LDB_SUCCESS) { + goto failed; + } + + /* force the permissions on the ldb to 0600 - this will fix + existing databases as well as new ones */ + if (chmod(db_path, 0600) != 0) { + goto failed; + } + + if (!existed) { + /* initialise the ldb with an index */ + struct ldb_ldif *ldif; + int i; + for (i=0;i<ARRAY_SIZE(init_ldif);i++) { + ldif = ldb_ldif_read_string(ldb, &init_ldif[i]); + if (ldif == NULL) goto failed; + ret = ldb_add(ldb, ldif->msg); + talloc_free(ldif); + if (ret == -1) goto failed; + } + } + + /* possibly upgrade */ + tdb_path = state_path("group_mapping.tdb"); + if (file_exist(tdb_path, NULL) && !mapping_upgrade(tdb_path)) { + unlink(state_path("group_mapping.ldb")); + goto failed; + } + + return True; + +failed: + DEBUG(0,("Failed to open group mapping ldb '%s' - '%s'\n", + db_path, ldb?ldb_errstring(ldb):strerror(errno))); + talloc_free(ldb); + ldb = NULL; + return False; +} + + +/* + form the DN for a mapping entry from a SID + */ +static struct ldb_dn *mapping_dn(TALLOC_CTX *mem_ctx, const DOM_SID *sid) +{ + fstring string_sid; + uint32_t rid; + DOM_SID domsid; + + sid_copy(&domsid, sid); + if (!sid_split_rid(&domsid, &rid)) { + return NULL; + } + if (!sid_to_fstring(string_sid, &domsid)) { + return NULL; + } + /* we split by domain and rid so we can do a subtree search + when we only want one domain */ + return ldb_dn_string_compose(mem_ctx, NULL, "rid=%u,domain=%s", + rid, string_sid); +} + +/* + add a group mapping entry + */ +static bool add_mapping_entry(GROUP_MAP *map, int flag) +{ + struct ldb_message *msg; + int ret, i; + fstring string_sid; + + msg = ldb_msg_new(ldb); + if (msg == NULL) { + return False; + } + + msg->dn = mapping_dn(msg, &map->sid); + if (msg->dn == NULL) { + goto failed; + } + + if (ldb_msg_add_string(msg, "objectClass", "groupMap") != LDB_SUCCESS || + ldb_msg_add_string(msg, "sid", + sid_to_fstring(string_sid, &map->sid)) != LDB_SUCCESS || + ldb_msg_add_fmt(msg, "gidNumber", "%u", (unsigned)map->gid) != LDB_SUCCESS || + ldb_msg_add_fmt(msg, "sidNameUse", "%u", (unsigned)map->sid_name_use) != LDB_SUCCESS || + ldb_msg_add_string(msg, "comment", map->comment) != LDB_SUCCESS || + ldb_msg_add_string(msg, "ntName", map->nt_name) != LDB_SUCCESS) { + goto failed; + } + + ret = ldb_add(ldb, msg); + + /* if it exists we update it. This is a hangover from the semantics the + tdb backend had */ + if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) { + for (i=0;i<msg->num_elements;i++) { + msg->elements[i].flags = LDB_FLAG_MOD_REPLACE; + } + ret = ldb_modify(ldb, msg); + } + + talloc_free(msg); + + return ret == LDB_SUCCESS; + +failed: + talloc_free(msg); + return False; +} + +/* + unpack a ldb message into a GROUP_MAP structure +*/ +static bool msg_to_group_map(struct ldb_message *msg, GROUP_MAP *map) +{ + const char *sidstr; + + map->gid = ldb_msg_find_attr_as_int(msg, "gidNumber", -1); + map->sid_name_use = ldb_msg_find_attr_as_int(msg, "sidNameUse", -1); + fstrcpy(map->nt_name, ldb_msg_find_attr_as_string(msg, "ntName", NULL)); + fstrcpy(map->comment, ldb_msg_find_attr_as_string(msg, "comment", NULL)); + sidstr = ldb_msg_find_attr_as_string(msg, "sid", NULL); + + if (!string_to_sid(&map->sid, sidstr) || + map->gid == (gid_t)-1 || + map->sid_name_use == (enum lsa_SidType)-1) { + DEBUG(0,("Unable to unpack group mapping\n")); + return False; + } + + return True; +} + +/* + return a group map entry for a given sid +*/ +static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map) +{ + int ret; + struct ldb_dn *dn; + struct ldb_result *res=NULL; + + dn = mapping_dn(ldb, &sid); + if (dn == NULL) goto failed; + + ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res); + talloc_steal(dn, res); + if (ret != LDB_SUCCESS || res->count != 1) { + goto failed; + } + + if (!msg_to_group_map(res->msgs[0], map)) goto failed; + + talloc_free(dn); + return True; + +failed: + talloc_free(dn); + return False; +} + +/* + return a group map entry for a given gid +*/ +static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map) +{ + int ret; + char *expr; + struct ldb_result *res=NULL; + + expr = talloc_asprintf(ldb, "(&(gidNumber=%u)(objectClass=groupMap))", + (unsigned)gid); + if (expr == NULL) goto failed; + + ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res); + talloc_steal(expr, res); + if (ret != LDB_SUCCESS || res->count != 1) goto failed; + + if (!msg_to_group_map(res->msgs[0], map)) goto failed; + + talloc_free(expr); + return True; + +failed: + talloc_free(expr); + return False; +} + +/* + Return the sid and the type of the unix group. +*/ +static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map) +{ + int ret; + char *expr; + struct ldb_result *res=NULL; + + expr = talloc_asprintf(ldb, "(&(ntName=%s)(objectClass=groupMap))", name); + if (expr == NULL) goto failed; + + ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res); + talloc_steal(expr, res); + if (ret != LDB_SUCCESS || res->count != 1) goto failed; + + if (!msg_to_group_map(res->msgs[0], map)) goto failed; + + talloc_free(expr); + return True; + +failed: + talloc_free(expr); + return False; +} + +/* + Remove a group mapping entry. +*/ +static bool group_map_remove(const DOM_SID *sid) +{ + struct ldb_dn *dn; + int ret; + + dn = mapping_dn(ldb, sid); + if (dn == NULL) { + return False; + } + ret = ldb_delete(ldb, dn); + talloc_free(dn); + + return ret == LDB_SUCCESS; +} + + +/* + Enumerate the group mappings for a domain +*/ +static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, + GROUP_MAP **pp_rmap, + size_t *p_num_entries, bool unix_only) +{ + int i, ret; + char *expr; + fstring name; + struct ldb_result *res = NULL; + struct ldb_dn *basedn=NULL; + TALLOC_CTX *tmp_ctx; + + tmp_ctx = talloc_new(ldb); + if (tmp_ctx == NULL) goto failed; + + if (sid_name_use == SID_NAME_UNKNOWN) { + expr = talloc_asprintf(tmp_ctx, "(&(objectClass=groupMap))"); + } else { + expr = talloc_asprintf(tmp_ctx, "(&(sidNameUse=%u)(objectClass=groupMap))", + sid_name_use); + } + if (expr == NULL) goto failed; + + /* we do a subtree search on the domain */ + if (domsid != NULL) { + sid_to_fstring(name, domsid); + basedn = ldb_dn_string_compose(tmp_ctx, NULL, "domain=%s", name); + if (basedn == NULL) goto failed; + } + + ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, expr, NULL, &res); + talloc_steal(tmp_ctx, res); + if (ret != LDB_SUCCESS) goto failed; + + (*pp_rmap) = NULL; + *p_num_entries = 0; + + for (i=0;i<res->count;i++) { + (*pp_rmap) = SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, + (*p_num_entries)+1); + if (!(*pp_rmap)) goto failed; + + if (!msg_to_group_map(res->msgs[i], &(*pp_rmap)[*p_num_entries])) { + goto failed; + } + + (*p_num_entries)++; + } + + talloc_free(tmp_ctx); + return True; + +failed: + talloc_free(tmp_ctx); + return False; +} + +/* + This operation happens on session setup, so it should better be fast. We + store a list of aliases a SID is member of hanging off MEMBEROF/SID. +*/ +static NTSTATUS one_alias_membership(const DOM_SID *member, + DOM_SID **sids, size_t *num) +{ + const char *attrs[] = { + "sid", + NULL + }; + DOM_SID alias; + char *expr; + int ret, i; + struct ldb_result *res=NULL; + fstring string_sid; + NTSTATUS status = NT_STATUS_INTERNAL_DB_CORRUPTION; + + if (!sid_to_fstring(string_sid, member)) { + return NT_STATUS_INVALID_PARAMETER; + } + + expr = talloc_asprintf(ldb, "(&(member=%s)(objectClass=groupMap))", + string_sid); + if (expr == NULL) goto failed; + + ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, attrs, &res); + talloc_steal(expr, res); + if (ret != LDB_SUCCESS) { + goto failed; + } + + for (i=0;i<res->count;i++) { + struct ldb_message_element *el; + el = ldb_msg_find_element(res->msgs[i], "sid"); + if (el == NULL || el->num_values != 1) { + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto failed; + } + string_to_sid(&alias, (char *)el->values[0].data); + status = add_sid_to_array_unique(NULL, &alias, sids, num); + if (!NT_STATUS_IS_OK(status)) { + goto failed; + } + } + + talloc_free(expr); + return NT_STATUS_OK; + +failed: + talloc_free(expr); + return status; +} + +/* + add/remove a member field +*/ +static NTSTATUS modify_aliasmem(const DOM_SID *alias, const DOM_SID *member, + int operation) +{ + fstring string_sid; + int ret; + struct ldb_message msg; + struct ldb_message_element el; + struct ldb_val val; + TALLOC_CTX *tmp_ctx; + GROUP_MAP map; + + if (!get_group_map_from_sid(*alias, &map)) { + sid_to_fstring(string_sid, alias); + return NT_STATUS_NO_SUCH_ALIAS; + } + + if ((map.sid_name_use != SID_NAME_ALIAS) && + (map.sid_name_use != SID_NAME_WKN_GRP)) { + DEBUG(0,("sid_name_use=%d\n", map.sid_name_use)); + return NT_STATUS_NO_SUCH_ALIAS; + } + + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + return NT_STATUS_NO_MEMORY; + } + + msg.dn = mapping_dn(tmp_ctx, alias); + if (msg.dn == NULL) { + return NT_STATUS_NO_MEMORY; + } + msg.num_elements = 1; + msg.elements = ⪙ + el.flags = operation; + el.name = talloc_strdup(tmp_ctx, "member"); + el.num_values = 1; + el.values = &val; + sid_to_fstring(string_sid, member); + val.data = (uint8_t *)string_sid; + val.length = strlen(string_sid); + + ret = ldb_modify(ldb, &msg); + talloc_free(tmp_ctx); + + if (ret == LDB_ERR_NO_SUCH_OBJECT) { + return NT_STATUS_NO_SUCH_ALIAS; + } + + if (operation == LDB_FLAG_MOD_ADD && + ret == LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) { + return NT_STATUS_MEMBER_IN_ALIAS; + } + + return (ret == LDB_SUCCESS ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED); +} + +static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member) +{ + return modify_aliasmem(alias, member, LDB_FLAG_MOD_ADD); +} + +static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member) +{ + return modify_aliasmem(alias, member, LDB_FLAG_MOD_DELETE); +} + + +/* + enumerate sids that have the given alias set in member +*/ +static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num) +{ + const char *attrs[] = { + "member", + NULL + }; + int ret, i; + NTSTATUS status = NT_STATUS_OK; + struct ldb_result *res=NULL; + struct ldb_dn *dn; + struct ldb_message_element *el; + + *sids = NULL; + *num = 0; + + dn = mapping_dn(ldb, alias); + if (dn == NULL) { + return NT_STATUS_NO_MEMORY; + } + + ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res); + talloc_steal(dn, res); + if (ret == LDB_SUCCESS && res->count == 0) { + talloc_free(dn); + return NT_STATUS_OK; + } + if (ret != LDB_SUCCESS) { + talloc_free(dn); + return NT_STATUS_INTERNAL_DB_CORRUPTION; + } + + el = ldb_msg_find_element(res->msgs[0], "member"); + if (el == NULL) { + talloc_free(dn); + return NT_STATUS_INTERNAL_DB_CORRUPTION; + } + + for (i=0;i<el->num_values;i++) { + DOM_SID sid; + string_to_sid(&sid, (const char *)el->values[i].data); + status = add_sid_to_array_unique(NULL, &sid, sids, num); + if (!NT_STATUS_IS_OK(status)) { + goto done; + } + } + +done: + talloc_free(dn); + return status; +} + +/* + upgrade one group mapping record from the old tdb format +*/ +static int upgrade_map_record(TDB_CONTEXT *tdb_ctx, TDB_DATA key, + TDB_DATA data, void *state) +{ + int ret; + GROUP_MAP map; + + if (strncmp((char *)key.dptr, GROUP_PREFIX, + MIN(key.dsize, strlen(GROUP_PREFIX))) != 0) { + return 0; + } + + if (!string_to_sid(&map.sid, strlen(GROUP_PREFIX) + (const char *)key.dptr)) { + DEBUG(0,("Bad sid key '%s' during upgrade\n", (const char *)key.dptr)); + *(int *)state = -1; + return -1; + } + + ret = tdb_unpack(data.dptr, data.dsize, "ddff", + &map.gid, &map.sid_name_use, &map.nt_name, &map.comment); + if (ret == -1) { + DEBUG(0,("Failed to unpack group map record during upgrade\n")); + *(int *)state = -1; + return -1; + } + + if (!add_mapping_entry(&map, 0)) { + DEBUG(0,("Failed to add mapping entry during upgrade\n")); + *(int *)state = -1; + return -1; + } + + return 0; +} + +/* + upgrade one alias record from the old tdb format +*/ +static int upgrade_alias_record(TDB_CONTEXT *tdb_ctx, TDB_DATA key, + TDB_DATA data, void *state) +{ + const char *p = (const char *)data.dptr; + char *string_sid; + DOM_SID member; + TALLOC_CTX *frame; + + if (strncmp((char *)key.dptr, MEMBEROF_PREFIX, + MIN(key.dsize, strlen(MEMBEROF_PREFIX))) != 0) { + return 0; + } + + if (!string_to_sid(&member, strlen(MEMBEROF_PREFIX) + (const char *)key.dptr)) { + DEBUG(0,("Bad alias key %s during upgrade\n", + (const char *)key.dptr)); + *(int *)state = -1; + } + + frame = talloc_stackframe(); + while (next_token_talloc(frame,&p, &string_sid, " ")) { + DOM_SID alias; + NTSTATUS status; + string_to_sid(&alias, string_sid); + status = add_aliasmem(&alias, &member); + if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_ALIAS)) { + DEBUG(0,("Ignoring orphaned alias record '%s'\n", + string_sid)); + } else if (!NT_STATUS_IS_OK(status)) { + DEBUG(0,("Failed to add alias member during upgrade - %s\n", + nt_errstr(status))); + *(int *)state = -1; + TALLOC_FREE(frame); + return -1; + } + } + TALLOC_FREE(frame); + return 0; +} + +/* + upgrade from a old style tdb +*/ +static bool mapping_upgrade(const char *tdb_path) +{ + static TDB_CONTEXT *tdb; + int ret, status=0; + + tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDWR, 0600); + if (tdb == NULL) goto failed; + + /* we have to do the map records first, as alias records may + reference them */ + ret = tdb_traverse(tdb, upgrade_map_record, &status); + if (ret == -1 || status == -1) goto failed; + + ret = tdb_traverse(tdb, upgrade_alias_record, &status); + if (ret == -1 || status == -1) goto failed; + + if (tdb) { + tdb_close(tdb); + tdb = NULL; + } + + { + const char *old_path = tdb_path; + char *new_path = state_path("group_mapping.tdb.upgraded"); + + if (!new_path) { + goto failed; + } + if (rename(old_path, new_path) != 0) { + DEBUG(0,("Failed to rename old group mapping database\n")); + goto failed; + } + } + return True; + +failed: + DEBUG(0,("Failed to upgrade group mapping database\n")); + if (tdb) tdb_close(tdb); + return False; +} + + + +static const struct mapping_backend ldb_backend = { + .add_mapping_entry = add_mapping_entry, + .get_group_map_from_sid = get_group_map_from_sid, + .get_group_map_from_gid = get_group_map_from_gid, + .get_group_map_from_ntname = get_group_map_from_ntname, + .group_map_remove = group_map_remove, + .enum_group_mapping = enum_group_mapping, + .one_alias_membership = one_alias_membership, + .add_aliasmem = add_aliasmem, + .del_aliasmem = del_aliasmem, + .enum_aliasmem = enum_aliasmem +}; + +/* + initialise the ldb mapping backend + */ +const struct mapping_backend *groupdb_ldb_init(void) +{ + if (!init_group_mapping()) { + DEBUG(0,("Failed to initialise ldb mapping backend\n")); + return NULL; + } + + return &ldb_backend; +} diff --git a/source3/groupdb/mapping_tdb.c b/source3/groupdb/mapping_tdb.c new file mode 100644 index 0000000000..7cee53a968 --- /dev/null +++ b/source3/groupdb/mapping_tdb.c @@ -0,0 +1,744 @@ +/* + * Unix SMB/CIFS implementation. + * RPC Pipe client / server routines + * Copyright (C) Andrew Tridgell 1992-2006, + * Copyright (C) Jean François Micouleau 1998-2001. + * Copyright (C) Volker Lendecke 2006. + * Copyright (C) Gerald Carter 2006. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses/>. + */ + +#include "includes.h" +#include "groupdb/mapping.h" + +static struct db_context *db; /* used for driver files */ + +static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_use, GROUP_MAP **pp_rmap, + size_t *p_num_entries, bool unix_only); +static bool group_map_remove(const DOM_SID *sid); + +/**************************************************************************** + Open the group mapping tdb. +****************************************************************************/ +static bool init_group_mapping(void) +{ + if (db != NULL) { + return true; + } + + db = db_open(NULL, state_path("group_mapping.tdb"), 0, + TDB_DEFAULT, O_RDWR|O_CREAT, 0600); + if (db == NULL) { + DEBUG(0, ("Failed to open group mapping database: %s\n", + strerror(errno))); + return false; + } + +#if 0 + /* + * This code was designed to handle a group mapping version + * upgrade. mapping_tdb is not active by default anymore, so ignore + * this here. + */ + { + const char *vstring = "INFO/version"; + int32 vers_id; + GROUP_MAP *map_table = NULL; + size_t num_entries = 0; + + /* handle a Samba upgrade */ + tdb_lock_bystring(tdb, vstring); + + /* Cope with byte-reversed older versions of the db. */ + vers_id = tdb_fetch_int32(tdb, vstring); + if ((vers_id == DATABASE_VERSION_V1) + || (IREV(vers_id) == DATABASE_VERSION_V1)) { + /* + * Written on a bigendian machine with old fetch_int + * code. Save as le. + */ + tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2); + vers_id = DATABASE_VERSION_V2; + } + + /* if its an unknown version we remove everthing in the db */ + + if (vers_id != DATABASE_VERSION_V2) { + tdb_wipe_all(tdb); + tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2); + } + + tdb_unlock_bystring(tdb, vstring); + + /* cleanup any map entries with a gid == -1 */ + + if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table, + &num_entries, False ) ) { + int i; + + for ( i=0; i<num_entries; i++ ) { + if ( map_table[i].gid == -1 ) { + group_map_remove( &map_table[i].sid ); + } + } + + SAFE_FREE( map_table ); + } + } +#endif + + return true; +} + +static char *group_mapping_key(TALLOC_CTX *mem_ctx, const DOM_SID *sid) +{ + char *sidstr, *result; + + sidstr = sid_string_talloc(talloc_tos(), sid); + if (sidstr == NULL) { + return NULL; + } + + result = talloc_asprintf(mem_ctx, "%s%s", GROUP_PREFIX, sidstr); + + TALLOC_FREE(sidstr); + return result; +} + +/**************************************************************************** +****************************************************************************/ +static bool add_mapping_entry(GROUP_MAP *map, int flag) +{ + char *key, *buf; + int len; + NTSTATUS status; + + key = group_mapping_key(talloc_tos(), &map->sid); + if (key == NULL) { + return false; + } + + len = tdb_pack(NULL, 0, "ddff", + map->gid, map->sid_name_use, map->nt_name, map->comment); + + buf = TALLOC_ARRAY(key, char, len); + if (!buf) { + TALLOC_FREE(key); + return false; + } + len = tdb_pack((uint8 *)buf, len, "ddff", map->gid, + map->sid_name_use, map->nt_name, map->comment); + + status = dbwrap_trans_store( + db, string_term_tdb_data(key), + make_tdb_data((uint8_t *)buf, len), TDB_REPLACE); + + TALLOC_FREE(key); + + return NT_STATUS_IS_OK(status); +} + + +/**************************************************************************** + Return the sid and the type of the unix group. +****************************************************************************/ + +static bool get_group_map_from_sid(DOM_SID sid, GROUP_MAP *map) +{ + TDB_DATA dbuf; + char *key; + int ret = 0; + + /* the key is the SID, retrieving is direct */ + + key = group_mapping_key(talloc_tos(), &sid); + if (key == NULL) { + return false; + } + + dbuf = dbwrap_fetch_bystring(db, key, key); + if (dbuf.dptr == NULL) { + TALLOC_FREE(key); + return false; + } + + ret = tdb_unpack(dbuf.dptr, dbuf.dsize, "ddff", + &map->gid, &map->sid_name_use, + &map->nt_name, &map->comment); + + TALLOC_FREE(key); + + if ( ret == -1 ) { + DEBUG(3,("get_group_map_from_sid: tdb_unpack failure\n")); + return false; + } + + sid_copy(&map->sid, &sid); + + return true; +} + +static bool dbrec2map(const struct db_record *rec, GROUP_MAP *map) +{ + if ((rec->key.dsize < strlen(GROUP_PREFIX)) + || (strncmp((char *)rec->key.dptr, GROUP_PREFIX, + GROUP_PREFIX_LEN) != 0)) { + return False; + } + + if (!string_to_sid(&map->sid, (const char *)rec->key.dptr + + GROUP_PREFIX_LEN)) { + return False; + } + + return tdb_unpack(rec->value.dptr, rec->value.dsize, "ddff", + &map->gid, &map->sid_name_use, &map->nt_name, + &map->comment) != -1; +} + +struct find_map_state { + bool found; + const char *name; /* If != NULL, look for name */ + gid_t gid; /* valid iff name == NULL */ + GROUP_MAP *map; +}; + +static int find_map(struct db_record *rec, void *private_data) +{ + struct find_map_state *state = (struct find_map_state *)private_data; + + if (!dbrec2map(rec, state->map)) { + DEBUG(10, ("failed to unpack map\n")); + return 0; + } + + if (state->name != NULL) { + if (strequal(state->name, state->map->nt_name)) { + state->found = true; + return 1; + } + } + else { + if (state->map->gid == state->gid) { + state->found = true; + return 1; + } + } + + return 0; +} + +/**************************************************************************** + Return the sid and the type of the unix group. +****************************************************************************/ + +static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map) +{ + struct find_map_state state; + + state.found = false; + state.name = NULL; /* Indicate we're looking for gid */ + state.gid = gid; + state.map = map; + + db->traverse_read(db, find_map, (void *)&state); + + return state.found; +} + +/**************************************************************************** + Return the sid and the type of the unix group. +****************************************************************************/ + +static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map) +{ + struct find_map_state state; + + state.found = false; + state.name = name; + state.map = map; + + db->traverse_read(db, find_map, (void *)&state); + + return state.found; +} + +/**************************************************************************** + Remove a group mapping entry. +****************************************************************************/ + +static bool group_map_remove(const DOM_SID *sid) +{ + char *key; + NTSTATUS status; + + key = group_mapping_key(talloc_tos(), sid); + if (key == NULL) { + return false; + } + + status = dbwrap_trans_delete(db, string_term_tdb_data(key)); + + TALLOC_FREE(key); + return NT_STATUS_IS_OK(status); +} + +/**************************************************************************** + Enumerate the group mapping. +****************************************************************************/ + +struct enum_map_state { + const DOM_SID *domsid; + enum lsa_SidType sid_name_use; + bool unix_only; + + size_t num_maps; + GROUP_MAP *maps; +}; + +static int collect_map(struct db_record *rec, void *private_data) +{ + struct enum_map_state *state = (struct enum_map_state *)private_data; + GROUP_MAP map; + GROUP_MAP *tmp; + + if (!dbrec2map(rec, &map)) { + return 0; + } + /* list only the type or everything if UNKNOWN */ + if (state->sid_name_use != SID_NAME_UNKNOWN + && state->sid_name_use != map.sid_name_use) { + DEBUG(11,("enum_group_mapping: group %s is not of the " + "requested type\n", map.nt_name)); + return 0; + } + + if ((state->unix_only == ENUM_ONLY_MAPPED) && (map.gid == -1)) { + DEBUG(11,("enum_group_mapping: group %s is non mapped\n", + map.nt_name)); + return 0; + } + + if ((state->domsid != NULL) && + (sid_compare_domain(state->domsid, &map.sid) != 0)) { + DEBUG(11,("enum_group_mapping: group %s is not in domain\n", + sid_string_dbg(&map.sid))); + return 0; + } + + if (!(tmp = SMB_REALLOC_ARRAY(state->maps, GROUP_MAP, + state->num_maps+1))) { + DEBUG(0,("enum_group_mapping: Unable to enlarge group " + "map!\n")); + return 1; + } + + state->maps = tmp; + state->maps[state->num_maps] = map; + state->num_maps++; + return 0; +} + +static bool enum_group_mapping(const DOM_SID *domsid, + enum lsa_SidType sid_name_use, + GROUP_MAP **pp_rmap, + size_t *p_num_entries, bool unix_only) +{ + struct enum_map_state state; + + state.domsid = domsid; + state.sid_name_use = sid_name_use; + state.unix_only = unix_only; + state.num_maps = 0; + state.maps = NULL; + + if (db->traverse_read(db, collect_map, (void *)&state) < 0) { + return false; + } + + *pp_rmap = state.maps; + *p_num_entries = state.num_maps; + + return true; +} + +/* This operation happens on session setup, so it should better be fast. We + * store a list of aliases a SID is member of hanging off MEMBEROF/SID. */ + +static NTSTATUS one_alias_membership(const DOM_SID *member, + DOM_SID **sids, size_t *num) +{ + fstring tmp; + fstring key; + char *string_sid; + TDB_DATA dbuf; + const char *p; + NTSTATUS status = NT_STATUS_OK; + TALLOC_CTX *frame = talloc_stackframe(); + + slprintf(key, sizeof(key), "%s%s", MEMBEROF_PREFIX, + sid_to_fstring(tmp, member)); + + dbuf = dbwrap_fetch_bystring(db, frame, key); + if (dbuf.dptr == NULL) { + TALLOC_FREE(frame); + return NT_STATUS_OK; + } + + p = (const char *)dbuf.dptr; + + while (next_token_talloc(frame, &p, &string_sid, " ")) { + DOM_SID alias; + + if (!string_to_sid(&alias, string_sid)) + continue; + + status= add_sid_to_array_unique(NULL, &alias, sids, num); + if (!NT_STATUS_IS_OK(status)) { + goto done; + } + } + +done: + TALLOC_FREE(frame); + return status; +} + +static NTSTATUS alias_memberships(const DOM_SID *members, size_t num_members, + DOM_SID **sids, size_t *num) +{ + size_t i; + + *num = 0; + *sids = NULL; + + for (i=0; i<num_members; i++) { + NTSTATUS status = one_alias_membership(&members[i], sids, num); + if (!NT_STATUS_IS_OK(status)) + return status; + } + return NT_STATUS_OK; +} + +static bool is_aliasmem(const DOM_SID *alias, const DOM_SID *member) +{ + DOM_SID *sids; + size_t i, num; + + /* This feels the wrong way round, but the on-disk data structure + * dictates it this way. */ + if (!NT_STATUS_IS_OK(alias_memberships(member, 1, &sids, &num))) + return False; + + for (i=0; i<num; i++) { + if (sid_compare(alias, &sids[i]) == 0) { + TALLOC_FREE(sids); + return True; + } + } + TALLOC_FREE(sids); + return False; +} + + +static NTSTATUS add_aliasmem(const DOM_SID *alias, const DOM_SID *member) +{ + GROUP_MAP map; + char *key; + fstring string_sid; + char *new_memberstring; + struct db_record *rec; + NTSTATUS status; + + if (!get_group_map_from_sid(*alias, &map)) + return NT_STATUS_NO_SUCH_ALIAS; + + if ( (map.sid_name_use != SID_NAME_ALIAS) && + (map.sid_name_use != SID_NAME_WKN_GRP) ) + return NT_STATUS_NO_SUCH_ALIAS; + + if (is_aliasmem(alias, member)) + return NT_STATUS_MEMBER_IN_ALIAS; + + sid_to_fstring(string_sid, member); + + key = talloc_asprintf(talloc_tos(), "%s%s", MEMBEROF_PREFIX, + string_sid); + if (key == NULL) { + return NT_STATUS_NO_MEMORY; + } + + if (db->transaction_start(db) != 0) { + DEBUG(0, ("transaction_start failed\n")); + return NT_STATUS_INTERNAL_DB_CORRUPTION; + } + + rec = db->fetch_locked(db, key, string_term_tdb_data(key)); + + if (rec == NULL) { + DEBUG(10, ("fetch_lock failed\n")); + TALLOC_FREE(key); + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + goto cancel; + } + + sid_to_fstring(string_sid, alias); + + if (rec->value.dptr != NULL) { + new_memberstring = talloc_asprintf( + key, "%s %s", (char *)(rec->value.dptr), string_sid); + } else { + new_memberstring = talloc_strdup(key, string_sid); + } + + if (new_memberstring == NULL) { + TALLOC_FREE(key); + status = NT_STATUS_NO_MEMORY; + goto cancel; + } + + status = rec->store(rec, string_term_tdb_data(new_memberstring), 0); + + TALLOC_FREE(key); + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(10, ("Could not store record: %s\n", nt_errstr(status))); + goto cancel; + } + + if (db->transaction_commit(db) != 0) { + DEBUG(0, ("transaction_commit failed\n")); + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + return status; + } + + return NT_STATUS_OK; + + cancel: + if (db->transaction_cancel(db) != 0) { + smb_panic("transaction_cancel failed"); + } + + return status; +} + +struct aliasmem_state { + const DOM_SID *alias; + DOM_SID **sids; + size_t *num; +}; + +static int collect_aliasmem(struct db_record *rec, void *priv) +{ + struct aliasmem_state *state = (struct aliasmem_state *)priv; + const char *p; + char *alias_string; + TALLOC_CTX *frame; + + if (strncmp((const char *)rec->key.dptr, MEMBEROF_PREFIX, + MEMBEROF_PREFIX_LEN) != 0) + return 0; + + p = (const char *)rec->value.dptr; + + frame = talloc_stackframe(); + + while (next_token_talloc(frame, &p, &alias_string, " ")) { + DOM_SID alias, member; + const char *member_string; + + if (!string_to_sid(&alias, alias_string)) + continue; + + if (sid_compare(state->alias, &alias) != 0) + continue; + + /* Ok, we found the alias we're looking for in the membership + * list currently scanned. The key represents the alias + * member. Add that. */ + + member_string = strchr((const char *)rec->key.dptr, '/'); + + /* Above we tested for MEMBEROF_PREFIX which includes the + * slash. */ + + SMB_ASSERT(member_string != NULL); + member_string += 1; + + if (!string_to_sid(&member, member_string)) + continue; + + if (!NT_STATUS_IS_OK(add_sid_to_array(NULL, &member, + state->sids, + state->num))) + { + /* talloc fail. */ + break; + } + } + + TALLOC_FREE(frame); + return 0; +} + +static NTSTATUS enum_aliasmem(const DOM_SID *alias, DOM_SID **sids, size_t *num) +{ + GROUP_MAP map; + struct aliasmem_state state; + + if (!get_group_map_from_sid(*alias, &map)) + return NT_STATUS_NO_SUCH_ALIAS; + + if ( (map.sid_name_use != SID_NAME_ALIAS) && + (map.sid_name_use != SID_NAME_WKN_GRP) ) + return NT_STATUS_NO_SUCH_ALIAS; + + *sids = NULL; + *num = 0; + + state.alias = alias; + state.sids = sids; + state.num = num; + + db->traverse_read(db, collect_aliasmem, &state); + return NT_STATUS_OK; +} + +static NTSTATUS del_aliasmem(const DOM_SID *alias, const DOM_SID *member) +{ + NTSTATUS status; + DOM_SID *sids; + size_t i, num; + bool found = False; + char *member_string; + char *key; + fstring sid_string; + + if (db->transaction_start(db) != 0) { + DEBUG(0, ("transaction_start failed\n")); + return NT_STATUS_INTERNAL_DB_CORRUPTION; + } + + status = alias_memberships(member, 1, &sids, &num); + + if (!NT_STATUS_IS_OK(status)) { + goto cancel; + } + + for (i=0; i<num; i++) { + if (sid_compare(&sids[i], alias) == 0) { + found = True; + break; + } + } + + if (!found) { + TALLOC_FREE(sids); + status = NT_STATUS_MEMBER_NOT_IN_ALIAS; + goto cancel; + } + + if (i < num) + sids[i] = sids[num-1]; + + num -= 1; + + sid_to_fstring(sid_string, member); + + key = talloc_asprintf(sids, "%s%s", MEMBEROF_PREFIX, sid_string); + if (key == NULL) { + TALLOC_FREE(sids); + status = NT_STATUS_NO_MEMORY; + goto cancel; + } + + if (num == 0) { + status = dbwrap_delete_bystring(db, key); + TALLOC_FREE(sids); + goto cancel; + } + + member_string = talloc_strdup(sids, ""); + if (member_string == NULL) { + TALLOC_FREE(sids); + status = NT_STATUS_NO_MEMORY; + goto cancel; + } + + for (i=0; i<num; i++) { + + sid_to_fstring(sid_string, &sids[i]); + + member_string = talloc_asprintf_append_buffer( + member_string, " %s", sid_string); + + if (member_string == NULL) { + TALLOC_FREE(sids); + status = NT_STATUS_NO_MEMORY; + goto cancel; + } + } + + status = dbwrap_store_bystring( + db, key, string_term_tdb_data(member_string), 0); + + TALLOC_FREE(sids); + + if (!NT_STATUS_IS_OK(status)) { + DEBUG(10, ("dbwrap_store_bystring failed: %s\n", + nt_errstr(status))); + goto cancel; + } + + if (db->transaction_commit(db) != 0) { + DEBUG(0, ("transaction_commit failed\n")); + status = NT_STATUS_INTERNAL_DB_CORRUPTION; + return status; + } + + return NT_STATUS_OK; + + cancel: + if (db->transaction_cancel(db) != 0) { + smb_panic("transaction_cancel failed"); + } + return status; +} + +static const struct mapping_backend tdb_backend = { + .add_mapping_entry = add_mapping_entry, + .get_group_map_from_sid = get_group_map_from_sid, + .get_group_map_from_gid = get_group_map_from_gid, + .get_group_map_from_ntname = get_group_map_from_ntname, + .group_map_remove = group_map_remove, + .enum_group_mapping = enum_group_mapping, + .one_alias_membership = one_alias_membership, + .add_aliasmem = add_aliasmem, + .del_aliasmem = del_aliasmem, + .enum_aliasmem = enum_aliasmem +}; + +/* + initialise the tdb mapping backend + */ +const struct mapping_backend *groupdb_tdb_init(void) +{ + if (!init_group_mapping()) { + DEBUG(0,("Failed to initialise tdb mapping backend\n")); + return NULL; + } + + return &tdb_backend; +} |