From 995d1567265be178b4e45f79ea4562a7041ffa52 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 26 Sep 2011 17:55:47 -0400 Subject: s3-group-mapping: Remove fstrings from GROUP_MAP. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andreas Schneider Autobuild-User: Günther Deschner Autobuild-Date: Wed Oct 12 19:28:12 CEST 2011 on sn-devel-104 --- source3/libnet/libnet_dssync_passdb.c | 200 +++++++++++++++++++++------------ source3/libnet/libnet_samsync_passdb.c | 144 ++++++++++++++---------- 2 files changed, 216 insertions(+), 128 deletions(-) (limited to 'source3/libnet') diff --git a/source3/libnet/libnet_dssync_passdb.c b/source3/libnet/libnet_dssync_passdb.c index be44cbaa86..b56c2d4451 100644 --- a/source3/libnet/libnet_dssync_passdb.c +++ b/source3/libnet/libnet_dssync_passdb.c @@ -452,7 +452,7 @@ static int dssync_passdb_traverse_gmembers(struct db_record *rec, struct dom_sid member_sid; struct samu *member = NULL; const char *member_dn = NULL; - GROUP_MAP map; + GROUP_MAP *map; struct group *grp; uint32_t rid; bool is_unix_member = false; @@ -508,19 +508,29 @@ static int dssync_passdb_traverse_gmembers(struct db_record *rec, break; } - if (!get_domain_group_from_sid(group_sid, &map)) { + map = talloc_zero(NULL, GROUP_MAP); + if (!map) { + return -1; + } + + if (!get_domain_group_from_sid(group_sid, map)) { DEBUG(0, ("Could not find global group %s\n", sid_string_dbg(&group_sid))); //return NT_STATUS_NO_SUCH_GROUP; + TALLOC_FREE(map); return -1; } - if (!(grp = getgrgid(map.gid))) { - DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map.gid)); + if (!(grp = getgrgid(map->gid))) { + DEBUG(0, ("Could not find unix group %lu\n", + (unsigned long)map->gid)); //return NT_STATUS_NO_SUCH_GROUP; + TALLOC_FREE(map); return -1; } + TALLOC_FREE(map); + DEBUG(0,("Group members of %s: ", grp->gr_name)); if ( !(member = samu_new(talloc_tos())) ) { @@ -1348,7 +1358,7 @@ static NTSTATUS handle_account_object(struct dssync_passdb *pctx, NTSTATUS status; fstring account; struct samu *sam_account=NULL; - GROUP_MAP map; + GROUP_MAP *map; struct group *grp; struct dom_sid user_sid; struct dom_sid group_sid; @@ -1430,14 +1440,19 @@ static NTSTATUS handle_account_object(struct dssync_passdb *pctx, group_sid = *pdb_get_group_sid(sam_account); - if (!pdb_getgrsid(&map, group_sid)) { + map = talloc_zero(NULL, GROUP_MAP); + if (!map) { + return NT_STATUS_NO_MEMORY; + } + + if (!pdb_getgrsid(map, group_sid)) { DEBUG(0, ("Primary group of %s has no mapping!\n", pdb_get_username(sam_account))); } else { - if (map.gid != passwd->pw_gid) { - if (!(grp = getgrgid(map.gid))) { + if (map->gid != passwd->pw_gid) { + if (!(grp = getgrgid(map->gid))) { DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n", - (unsigned long)map.gid, pdb_get_username(sam_account), + (unsigned long)map->gid, pdb_get_username(sam_account), sid_string_dbg(&group_sid))); } else { smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account)); @@ -1445,6 +1460,8 @@ static NTSTATUS handle_account_object(struct dssync_passdb *pctx, } } + TALLOC_FREE(map); + if ( !passwd ) { DEBUG(1, ("No unix user for this account (%s), cannot adjust mappings\n", pdb_get_username(sam_account))); @@ -1463,14 +1480,12 @@ static NTSTATUS handle_alias_object(struct dssync_passdb *pctx, { struct drsuapi_DsReplicaObjectListItemEx *cur = obj->cur; NTSTATUS status; - fstring name; - fstring comment; struct group *grp = NULL; struct dom_sid group_sid; uint32_t rid = 0; struct dom_sid *dom_sid = NULL; fstring sid_string; - GROUP_MAP map; + GROUP_MAP *map; bool insert = true; const char *sAMAccountName; @@ -1496,23 +1511,43 @@ static NTSTATUS handle_alias_object(struct dssync_passdb *pctx, return status; } - fstrcpy(name, sAMAccountName); - fstrcpy(comment, description); - dom_sid_split_rid(mem_ctx, &group_sid, &dom_sid, &rid); + map = talloc_zero(mem_ctx, GROUP_MAP); + if (map == NULL) { + status = NT_STATUS_NO_MEMORY; + goto done; + } + + map->nt_name = talloc_strdup(map, sAMAccountName); + if (map->nt_name == NULL) { + status = NT_STATUS_NO_MEMORY; + goto done; + } + + if (description) { + map->comment = talloc_strdup(map, description); + } else { + map->comment = talloc_strdup(map, ""); + } + if (map->comment == NULL) { + status = NT_STATUS_NO_MEMORY; + goto done; + } + sid_to_fstring(sid_string, &group_sid); DEBUG(0,("Creating alias[%s] - %s members[%u]\n", - name, sid_string, num_members)); + map->nt_name, sid_string, num_members)); status = dssync_insert_obj(pctx, pctx->aliases, obj); if (!NT_STATUS_IS_OK(status)) { - return status; + goto done; } - if (pdb_getgrsid(&map, group_sid)) { - if ( map.gid != -1 ) - grp = getgrgid(map.gid); + if (pdb_getgrsid(map, group_sid)) { + if (map->gid != -1) { + grp = getgrgid(map->gid); + } insert = false; } @@ -1520,22 +1555,27 @@ static NTSTATUS handle_alias_object(struct dssync_passdb *pctx, gid_t gid; /* No group found from mapping, find it from its name. */ - if ((grp = getgrnam(name)) == NULL) { + if ((grp = getgrnam(map->nt_name)) == NULL) { /* No appropriate group found, create one */ - DEBUG(0,("Creating unix group: '%s'\n", name)); + DEBUG(0, ("Creating unix group: '%s'\n", + map->nt_name)); - if (smb_create_group(name, &gid) != 0) - return NT_STATUS_ACCESS_DENIED; + if (smb_create_group(map->nt_name, &gid) != 0) { + status = NT_STATUS_ACCESS_DENIED; + goto done; + } - if ((grp = getgrgid(gid)) == NULL) - return NT_STATUS_ACCESS_DENIED; + if ((grp = getgrgid(gid)) == NULL) { + status = NT_STATUS_ACCESS_DENIED; + goto done; + } } } - map.gid = grp->gr_gid; - map.sid = group_sid; + map->gid = grp->gr_gid; + map->sid = group_sid; if (dom_sid_equal(dom_sid, &global_sid_Builtin)) { /* @@ -1543,23 +1583,17 @@ static NTSTATUS handle_alias_object(struct dssync_passdb *pctx, * * map.sid_name_use = SID_NAME_WKN_GRP; */ - map.sid_name_use = SID_NAME_ALIAS; + map->sid_name_use = SID_NAME_ALIAS; } else { - map.sid_name_use = SID_NAME_ALIAS; + map->sid_name_use = SID_NAME_ALIAS; } - strlcpy(map.nt_name, name, sizeof(map.nt_name)); - if (description) { - strlcpy(map.comment, comment, sizeof(map.comment)); + if (insert) { + pdb_add_group_mapping_entry(map); } else { - strlcpy(map.comment, "", sizeof(map.comment)); + pdb_update_group_mapping_entry(map); } - if (insert) - pdb_add_group_mapping_entry(&map); - else - pdb_update_group_mapping_entry(&map); - for (i=0; i < num_members; i++) { struct dssync_passdb_mem *mem; @@ -1567,11 +1601,15 @@ static NTSTATUS handle_alias_object(struct dssync_passdb *pctx, true /* active */, &members[i], &mem); if (!NT_STATUS_IS_OK(status)) { - return status; + goto done; } } - return NT_STATUS_OK; + status = NT_STATUS_OK; + +done: + TALLOC_FREE(map); + return status; } /**************************************************************** @@ -1583,12 +1621,10 @@ static NTSTATUS handle_group_object(struct dssync_passdb *pctx, { struct drsuapi_DsReplicaObjectListItemEx *cur = obj->cur; NTSTATUS status; - fstring name; - fstring comment; struct group *grp = NULL; struct dom_sid group_sid; fstring sid_string; - GROUP_MAP map; + GROUP_MAP *map; bool insert = true; const char *sAMAccountName; @@ -1614,21 +1650,39 @@ static NTSTATUS handle_group_object(struct dssync_passdb *pctx, return status; } - fstrcpy(name, sAMAccountName); - fstrcpy(comment, description); + map = talloc_zero(NULL, GROUP_MAP); + if (!map) { + return NT_STATUS_NO_MEMORY; + } + + map->nt_name = talloc_strdup(map, sAMAccountName); + if (!map->nt_name) { + status = NT_STATUS_NO_MEMORY; + goto done; + } + if (description) { + map->comment = talloc_strdup(map, description); + } else { + map->comment = talloc_strdup(map, ""); + } + if (!map->comment) { + status = NT_STATUS_NO_MEMORY; + goto done; + } sid_to_fstring(sid_string, &group_sid); DEBUG(0,("Creating group[%s] - %s members [%u]\n", - name, sid_string, num_members)); + map->nt_name, sid_string, num_members)); status = dssync_insert_obj(pctx, pctx->groups, obj); if (!NT_STATUS_IS_OK(status)) { - return status; + goto done; } - if (pdb_getgrsid(&map, group_sid)) { - if ( map.gid != -1 ) - grp = getgrgid(map.gid); + if (pdb_getgrsid(map, group_sid)) { + if (map->gid != -1) { + grp = getgrgid(map->gid); + } insert = false; } @@ -1636,35 +1690,35 @@ static NTSTATUS handle_group_object(struct dssync_passdb *pctx, gid_t gid; /* No group found from mapping, find it from its name. */ - if ((grp = getgrnam(name)) == NULL) { + if ((grp = getgrnam(map->nt_name)) == NULL) { /* No appropriate group found, create one */ - DEBUG(0,("Creating unix group: '%s'\n", name)); + DEBUG(0, ("Creating unix group: '%s'\n", + map->nt_name)); - if (smb_create_group(name, &gid) != 0) - return NT_STATUS_ACCESS_DENIED; + if (smb_create_group(map->nt_name, &gid) != 0) { + status = NT_STATUS_ACCESS_DENIED; + goto done; + } - if ((grp = getgrnam(name)) == NULL) - return NT_STATUS_ACCESS_DENIED; + if ((grp = getgrnam(map->nt_name)) == NULL) { + status = NT_STATUS_ACCESS_DENIED; + goto done; + } } } - map.gid = grp->gr_gid; - map.sid = group_sid; - map.sid_name_use = SID_NAME_DOM_GRP; - strlcpy(map.nt_name, name, sizeof(map.nt_name)); - if (description) { - strlcpy(map.comment, comment, sizeof(map.comment)); + map->gid = grp->gr_gid; + map->sid = group_sid; + map->sid_name_use = SID_NAME_DOM_GRP; + + if (insert) { + pdb_add_group_mapping_entry(map); } else { - strlcpy(map.comment, "", sizeof(map.comment)); + pdb_update_group_mapping_entry(map); } - if (insert) - pdb_add_group_mapping_entry(&map); - else - pdb_update_group_mapping_entry(&map); - for (i=0; i < num_members; i++) { struct dssync_passdb_mem *mem; @@ -1672,11 +1726,15 @@ static NTSTATUS handle_group_object(struct dssync_passdb *pctx, true /* active */, &members[i], &mem); if (!NT_STATUS_IS_OK(status)) { - return status; + goto done; } } - return NT_STATUS_OK; + status = NT_STATUS_OK; + +done: + TALLOC_FREE(map); + return status; } /**************************************************************** diff --git a/source3/libnet/libnet_samsync_passdb.c b/source3/libnet/libnet_samsync_passdb.c index 0cf2ed3323..cf47934475 100644 --- a/source3/libnet/libnet_samsync_passdb.c +++ b/source3/libnet/libnet_samsync_passdb.c @@ -301,7 +301,7 @@ static NTSTATUS fetch_account_info(TALLOC_CTX *mem_ctx, NTSTATUS nt_ret = NT_STATUS_UNSUCCESSFUL; fstring account; struct samu *sam_account=NULL; - GROUP_MAP map; + GROUP_MAP *map = NULL; struct group *grp; struct dom_sid user_sid; struct dom_sid group_sid; @@ -355,14 +355,19 @@ static NTSTATUS fetch_account_info(TALLOC_CTX *mem_ctx, group_sid = *pdb_get_group_sid(sam_account); - if (!pdb_getgrsid(&map, group_sid)) { + map = talloc_zero(mem_ctx, GROUP_MAP); + if (!map) { + return NT_STATUS_NO_MEMORY; + } + + if (!pdb_getgrsid(map, group_sid)) { DEBUG(0, ("Primary group of %s has no mapping!\n", pdb_get_username(sam_account))); } else { - if (map.gid != passwd->pw_gid) { - if (!(grp = getgrgid(map.gid))) { + if (map->gid != passwd->pw_gid) { + if (!(grp = getgrgid(map->gid))) { DEBUG(0, ("Could not find unix group %lu for user %s (group SID=%s)\n", - (unsigned long)map.gid, pdb_get_username(sam_account), sid_string_tos(&group_sid))); + (unsigned long)map->gid, pdb_get_username(sam_account), sid_string_tos(&group_sid))); } else { smb_set_primary_group(grp->gr_name, pdb_get_username(sam_account)); } @@ -376,6 +381,7 @@ static NTSTATUS fetch_account_info(TALLOC_CTX *mem_ctx, done: TALLOC_FREE(sam_account); + TALLOC_FREE(map); return nt_ret; } @@ -386,60 +392,65 @@ static NTSTATUS fetch_group_info(TALLOC_CTX *mem_ctx, uint32_t rid, struct netr_DELTA_GROUP *r) { - fstring name; - fstring comment; struct group *grp = NULL; struct dom_sid group_sid; fstring sid_string; - GROUP_MAP map; + GROUP_MAP *map; bool insert = true; - fstrcpy(name, r->group_name.string); - fstrcpy(comment, r->description.string); - + map = talloc_zero(mem_ctx, GROUP_MAP); + if (!map) { + return NT_STATUS_NO_MEMORY; + } /* add the group to the mapping table */ sid_compose(&group_sid, get_global_sam_sid(), rid); sid_to_fstring(sid_string, &group_sid); - if (pdb_getgrsid(&map, group_sid)) { - if ( map.gid != -1 ) - grp = getgrgid(map.gid); + if (pdb_getgrsid(map, group_sid)) { + if (map->gid != -1) { + grp = getgrgid(map->gid); + } insert = false; } + map->nt_name = talloc_strdup(map, r->group_name.string); + if (!map->nt_name) { + return NT_STATUS_NO_MEMORY; + } + map->comment = talloc_strdup(map, r->description.string); + if (!map->comment) { + return NT_STATUS_NO_MEMORY; + } + if (grp == NULL) { gid_t gid; /* No group found from mapping, find it from its name. */ - if ((grp = getgrnam(name)) == NULL) { + if ((grp = getgrnam(map->nt_name)) == NULL) { /* No appropriate group found, create one */ - d_printf("Creating unix group: '%s'\n", name); + d_printf("Creating unix group: '%s'\n", map->nt_name); - if (smb_create_group(name, &gid) != 0) + if (smb_create_group(map->nt_name, &gid) != 0) return NT_STATUS_ACCESS_DENIED; - if ((grp = getgrnam(name)) == NULL) + if ((grp = getgrnam(map->nt_name)) == NULL) return NT_STATUS_ACCESS_DENIED; } } - map.gid = grp->gr_gid; - map.sid = group_sid; - map.sid_name_use = SID_NAME_DOM_GRP; - strlcpy(map.nt_name, name, sizeof(map.nt_name)); - if (r->description.string) { - strlcpy(map.comment, comment, sizeof(map.comment)); + map->gid = grp->gr_gid; + map->sid = group_sid; + map->sid_name_use = SID_NAME_DOM_GRP; + + if (insert) { + pdb_add_group_mapping_entry(map); } else { - strlcpy(map.comment, "", sizeof(map.comment)); + pdb_update_group_mapping_entry(map); } - if (insert) - pdb_add_group_mapping_entry(&map); - else - pdb_update_group_mapping_entry(&map); - + TALLOC_FREE(map); return NT_STATUS_OK; } @@ -454,7 +465,7 @@ static NTSTATUS fetch_group_mem_info(TALLOC_CTX *mem_ctx, char **nt_members = NULL; char **unix_members; struct dom_sid group_sid; - GROUP_MAP map; + GROUP_MAP *map; struct group *grp; if (r->num_rids == 0) { @@ -463,16 +474,26 @@ static NTSTATUS fetch_group_mem_info(TALLOC_CTX *mem_ctx, sid_compose(&group_sid, get_global_sam_sid(), rid); - if (!get_domain_group_from_sid(group_sid, &map)) { + map = talloc_zero(mem_ctx, GROUP_MAP); + if (!map) { + return NT_STATUS_NO_MEMORY; + } + + if (!get_domain_group_from_sid(group_sid, map)) { DEBUG(0, ("Could not find global group %d\n", rid)); + TALLOC_FREE(map); return NT_STATUS_NO_SUCH_GROUP; } - if (!(grp = getgrgid(map.gid))) { - DEBUG(0, ("Could not find unix group %lu\n", (unsigned long)map.gid)); + if (!(grp = getgrgid(map->gid))) { + DEBUG(0, ("Could not find unix group %lu\n", + (unsigned long)map->gid)); + TALLOC_FREE(map); return NT_STATUS_NO_SUCH_GROUP; } + TALLOC_FREE(map); + d_printf("Group members of %s: ", grp->gr_name); if (r->num_rids) { @@ -575,56 +596,65 @@ static NTSTATUS fetch_alias_info(TALLOC_CTX *mem_ctx, struct netr_DELTA_ALIAS *r, const struct dom_sid *dom_sid) { - fstring name; - fstring comment; struct group *grp = NULL; struct dom_sid alias_sid; fstring sid_string; - GROUP_MAP map; + GROUP_MAP *map; bool insert = true; - fstrcpy(name, r->alias_name.string); - fstrcpy(comment, r->description.string); + map = talloc_zero(mem_ctx, GROUP_MAP); + if (!map) { + return NT_STATUS_NO_MEMORY; + } /* Find out whether the group is already mapped */ sid_compose(&alias_sid, dom_sid, rid); sid_to_fstring(sid_string, &alias_sid); - if (pdb_getgrsid(&map, alias_sid)) { - grp = getgrgid(map.gid); + if (pdb_getgrsid(map, alias_sid)) { + grp = getgrgid(map->gid); insert = false; } + map->nt_name = talloc_strdup(map, r->alias_name.string); + if (!map->nt_name) { + return NT_STATUS_NO_MEMORY; + } + map->comment = talloc_strdup(map, r->description.string); + if (!map->comment) { + return NT_STATUS_NO_MEMORY; + } + if (grp == NULL) { gid_t gid; /* No group found from mapping, find it from its name. */ - if ((grp = getgrnam(name)) == NULL) { + if ((grp = getgrnam(map->nt_name)) == NULL) { /* No appropriate group found, create one */ - d_printf("Creating unix group: '%s'\n", name); - if (smb_create_group(name, &gid) != 0) + d_printf("Creating unix group: '%s'\n", map->nt_name); + if (smb_create_group(map->nt_name, &gid) != 0) return NT_STATUS_ACCESS_DENIED; if ((grp = getgrgid(gid)) == NULL) return NT_STATUS_ACCESS_DENIED; } } - map.gid = grp->gr_gid; - map.sid = alias_sid; + map->gid = grp->gr_gid; + map->sid = alias_sid; - if (dom_sid_equal(dom_sid, &global_sid_Builtin)) - map.sid_name_use = SID_NAME_WKN_GRP; - else - map.sid_name_use = SID_NAME_ALIAS; - - strlcpy(map.nt_name, name, sizeof(map.nt_name)); - strlcpy(map.comment, comment, sizeof(map.comment)); + if (dom_sid_equal(dom_sid, &global_sid_Builtin)) { + map->sid_name_use = SID_NAME_WKN_GRP; + } else { + map->sid_name_use = SID_NAME_ALIAS; + } - if (insert) - pdb_add_group_mapping_entry(&map); - else - pdb_update_group_mapping_entry(&map); + if (insert) { + pdb_add_group_mapping_entry(map); + } else { + pdb_update_group_mapping_entry(map); + } + TALLOC_FREE(map); return NT_STATUS_OK; } -- cgit