/*
* 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) Gerald Carter 2003,
* Copyright (C) Volker Lendecke 2004
*
* 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 .
*/
#include "includes.h"
#include "utils/net.h"
/*********************************************************
Figure out if the input was an NT group or a SID string.
Return the SID.
**********************************************************/
static bool get_sid_from_input(DOM_SID *sid, char *input)
{
GROUP_MAP map;
if (StrnCaseCmp( input, "S-", 2)) {
/* Perhaps its the NT group name? */
if (!pdb_getgrnam(&map, input)) {
printf("NT Group %s doesn't exist in mapping DB\n", input);
return false;
} else {
*sid = map.sid;
}
} else {
if (!string_to_sid(sid, input)) {
printf("converting sid %s from a string failed!\n", input);
return false;
}
}
return true;
}
/*********************************************************
Dump a GROUP_MAP entry to stdout (long or short listing)
**********************************************************/
static void print_map_entry ( GROUP_MAP map, bool long_list )
{
if (!long_list)
d_printf("%s (%s) -> %s\n", map.nt_name,
sid_string_tos(&map.sid), gidtoname(map.gid));
else {
d_printf("%s\n", map.nt_name);
d_printf("\tSID : %s\n", sid_string_tos(&map.sid));
d_printf("\tUnix gid : %d\n", map.gid);
d_printf("\tUnix group: %s\n", gidtoname(map.gid));
d_printf("\tGroup type: %s\n",
sid_type_lookup(map.sid_name_use));
d_printf("\tComment : %s\n", map.comment);
}
}
/*********************************************************
List the groups.
**********************************************************/
static int net_groupmap_list(struct net_context *c, int argc, const char **argv)
{
size_t entries;
bool long_list = false;
size_t i;
fstring ntgroup = "";
fstring sid_string = "";
const char list_usage_str[] = "net groupmap list [verbose] "
"[ntgroup=NT group] [sid=SID]\n"
" verbose\tPrint verbose list\n"
" ntgroup\tNT group to list\n"
" sid\tSID of group to list";
if (c->display_usage) {
d_printf("Usage:\n%s\n", list_usage_str);
return 0;
}
if (c->opt_verbose || c->opt_long_list_entries)
long_list = true;
/* get the options */
for ( i=0; i|sid=}"
" unixgroup= "
"[type=] "
"[ntgroup=] [comment=]";
ZERO_STRUCT(map);
/* Default is domain group. */
map.sid_name_use = SID_NAME_DOM_GRP;
name_type = "domain group";
if (c->display_usage) {
d_printf("Usage\n%s\n", add_usage_str);
return 0;
}
/* get the options */
for ( i=0; i|sid=} "
"[comment=] "
"[unixgroup=] "
"[type=]";
if (c->display_usage) {
d_printf("Usage:\n%s\n", modify_usage_str);
return 0;
}
/* get the options */
for ( i=0; i|sid=}";
if (c->display_usage) {
d_printf("Usage:\n%s\n", delete_usage_str);
return 0;
}
/* get the options */
for ( i=0; i 2) || c->display_usage) {
d_printf("Usage: net groupmap set \"NT Group\" "
"[\"unix group\"] [-C \"comment\"] [-L] [-D]\n");
return -1;
}
if ( c->opt_localgroup && c->opt_domaingroup ) {
d_printf("Can only specify -L or -D, not both\n");
return -1;
}
ntgroup = argv[0];
if (argc == 2) {
grp = getgrnam(argv[1]);
if (grp == NULL) {
d_fprintf(stderr, "Could not find unix group %s\n", argv[1]);
return -1;
}
}
have_map = pdb_getgrnam(&map, ntgroup);
if (!have_map) {
DOM_SID sid;
have_map = ( (strncmp(ntgroup, "S-", 2) == 0) &&
string_to_sid(&sid, ntgroup) &&
pdb_getgrsid(&map, sid) );
}
if (!have_map) {
/* Ok, add it */
if (grp == NULL) {
d_fprintf(stderr, "Could not find group mapping for %s\n",
ntgroup);
return -1;
}
map.gid = grp->gr_gid;
if (c->opt_rid == 0) {
if ( pdb_rid_algorithm() )
c->opt_rid = algorithmic_pdb_gid_to_group_rid(map.gid);
else {
if ( !pdb_new_rid((uint32*)&c->opt_rid) ) {
d_fprintf( stderr, "Could not allocate new RID\n");
return -1;
}
}
}
sid_copy(&map.sid, get_global_sam_sid());
sid_append_rid(&map.sid, c->opt_rid);
map.sid_name_use = SID_NAME_DOM_GRP;
fstrcpy(map.nt_name, ntgroup);
fstrcpy(map.comment, "");
if (!NT_STATUS_IS_OK(pdb_add_group_mapping_entry(&map))) {
d_fprintf(stderr, "Could not add mapping entry for %s\n",
ntgroup);
return -1;
}
}
/* Now we have a mapping entry, update that stuff */
if ( c->opt_localgroup || c->opt_domaingroup ) {
if (map.sid_name_use == SID_NAME_WKN_GRP) {
d_fprintf(stderr, "Can't change type of the BUILTIN group %s\n",
map.nt_name);
return -1;
}
}
if (c->opt_localgroup)
map.sid_name_use = SID_NAME_ALIAS;
if (c->opt_domaingroup)
map.sid_name_use = SID_NAME_DOM_GRP;
/* The case (opt_domaingroup && opt_localgroup) was tested for above */
if (strlen(c->opt_comment) > 0)
fstrcpy(map.comment, c->opt_comment);
if (strlen(c->opt_newntname) > 0)
fstrcpy(map.nt_name, c->opt_newntname);
if (grp != NULL)
map.gid = grp->gr_gid;
if (!NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map))) {
d_fprintf(stderr, "Could not update group mapping for %s\n", ntgroup);
return -1;
}
return 0;
}
static int net_groupmap_cleanup(struct net_context *c, int argc, const char **argv)
{
GROUP_MAP *map = NULL;
size_t i, entries;
if (c->display_usage) {
d_printf("Usage:\n"
"net groupmap cleanup\n"
" Delete all group mappings\n");
return 0;
}
if (!pdb_enum_group_mapping(NULL, SID_NAME_UNKNOWN, &map, &entries,
ENUM_ALL_MAPPED)) {
d_fprintf(stderr, "Could not list group mappings\n");
return -1;
}
for (i=0; idisplay_usage ||
!string_to_sid(&alias, argv[0]) ||
!string_to_sid(&member, argv[1]) ) {
d_printf("Usage: net groupmap addmem alias-sid member-sid\n");
return -1;
}
if (!NT_STATUS_IS_OK(pdb_add_aliasmem(&alias, &member))) {
d_fprintf(stderr, "Could not add sid %s to alias %s\n",
argv[1], argv[0]);
return -1;
}
return 0;
}
static int net_groupmap_delmem(struct net_context *c, int argc, const char **argv)
{
DOM_SID alias, member;
if ( (argc != 2) ||
c->display_usage ||
!string_to_sid(&alias, argv[0]) ||
!string_to_sid(&member, argv[1]) ) {
d_printf("Usage: net groupmap delmem alias-sid member-sid\n");
return -1;
}
if (!NT_STATUS_IS_OK(pdb_del_aliasmem(&alias, &member))) {
d_fprintf(stderr, "Could not delete sid %s from alias %s\n",
argv[1], argv[0]);
return -1;
}
return 0;
}
static int net_groupmap_listmem(struct net_context *c, int argc, const char **argv)
{
DOM_SID alias;
DOM_SID *members;
size_t i, num;
if ( (argc != 1) ||
c->display_usage ||
!string_to_sid(&alias, argv[0]) ) {
d_printf("Usage: net groupmap listmem alias-sid\n");
return -1;
}
members = NULL;
num = 0;
if (!NT_STATUS_IS_OK(pdb_enum_aliasmem(&alias, &members, &num))) {
d_fprintf(stderr, "Could not list members for sid %s\n", argv[0]);
return -1;
}
for (i = 0; i < num; i++) {
printf("%s\n", sid_string_tos(&(members[i])));
}
TALLOC_FREE(members);
return 0;
}
static bool print_alias_memberships(TALLOC_CTX *mem_ctx,
const DOM_SID *domain_sid,
const DOM_SID *member)
{
uint32 *alias_rids;
size_t i, num_alias_rids;
alias_rids = NULL;
num_alias_rids = 0;
if (!NT_STATUS_IS_OK(pdb_enum_alias_memberships(
mem_ctx, domain_sid, member, 1,
&alias_rids, &num_alias_rids))) {
d_fprintf(stderr, "Could not list memberships for sid %s\n",
sid_string_tos(member));
return false;
}
for (i = 0; i < num_alias_rids; i++) {
DOM_SID alias;
sid_copy(&alias, domain_sid);
sid_append_rid(&alias, alias_rids[i]);
printf("%s\n", sid_string_tos(&alias));
}
return true;
}
static int net_groupmap_memberships(struct net_context *c, int argc, const char **argv)
{
TALLOC_CTX *mem_ctx;
DOM_SID *domain_sid, *builtin_sid, member;
if ( (argc != 1) ||
c->display_usage ||
!string_to_sid(&member, argv[0]) ) {
d_printf("Usage: net groupmap memberof sid\n");
return -1;
}
mem_ctx = talloc_init("net_groupmap_memberships");
if (mem_ctx == NULL) {
d_fprintf(stderr, "talloc_init failed\n");
return -1;
}
domain_sid = get_global_sam_sid();
builtin_sid = string_sid_talloc(mem_ctx, "S-1-5-32");
if ((domain_sid == NULL) || (builtin_sid == NULL)) {
d_fprintf(stderr, "Could not get domain sid\n");
return -1;
}
if (!print_alias_memberships(mem_ctx, domain_sid, &member) ||
!print_alias_memberships(mem_ctx, builtin_sid, &member))
return -1;
talloc_destroy(mem_ctx);
return 0;
}
/***********************************************************
migrated functionality from smbgroupedit
**********************************************************/
int net_groupmap(struct net_context *c, int argc, const char **argv)
{
struct functable3 func[] = {
{
"add",
net_groupmap_add,
NET_TRANSPORT_LOCAL,
"Create a new group mapping",
"net groupmap add\n"
" Create a new group mapping"
},
{
"modify",
net_groupmap_modify,
NET_TRANSPORT_LOCAL,
"Update a group mapping",
"net groupmap modify\n"
" Modify an existing group mapping"
},
{
"delete",
net_groupmap_delete,
NET_TRANSPORT_LOCAL,
"Remove a group mapping",
"net groupmap delete\n"
" Remove a group mapping"
},
{
"set",
net_groupmap_set,
NET_TRANSPORT_LOCAL,
"Set group mapping",
"net groupmap set\n"
" Set a group mapping"
},
{
"cleanup",
net_groupmap_cleanup,
NET_TRANSPORT_LOCAL,
"Remove foreign group mapping entries",
"net groupmap cleanup\n"
" Remove foreign group mapping entries"
},
{
"addmem",
net_groupmap_addmem,
NET_TRANSPORT_LOCAL,
"Add a foreign alias member",
"net groupmap addmem\n"
" Add a foreign alias member"
},
{
"delmem",
net_groupmap_delmem,
NET_TRANSPORT_LOCAL,
"Delete foreign alias member",
"net groupmap delmem\n"
" Delete foreign alias member"
},
{
"listmem",
net_groupmap_listmem,
NET_TRANSPORT_LOCAL,
"List foreign group members",
"net groupmap listmem\n"
" List foreign alias members"
},
{
"memberships",
net_groupmap_memberships,
NET_TRANSPORT_LOCAL,
"List foreign group memberships",
"net groupmap memberships\n"
" List foreign group memberships"
},
{
"list",
net_groupmap_list,
NET_TRANSPORT_LOCAL,
"List current group map",
"net groupmap list\n"
" List current group map"
},
{NULL, NULL, 0, NULL, NULL}
};
/* we shouldn't have silly checks like this */
if (getuid() != 0) {
d_fprintf(stderr, "You must be root to edit group mappings.\n");
return -1;
}
return net_run_function3(c,argc, argv, "net groupmap", func);
}