Implement 'net groupmap set' and 'net groupmap cleanup'.
authorVolker Lendecke <vlendec@samba.org>
Thu, 26 Feb 2004 11:34:33 +0000 (11:34 +0000)
committerVolker Lendecke <vlendec@samba.org>
Thu, 26 Feb 2004 11:34:33 +0000 (11:34 +0000)
I was rather annoyed by the net groupmap syntax, I could never get it
right.

net groupmap set "domain admins" domadm

creates a mapping,

net groupmap set "domain admins" -C "Comment" -N "newntname"

should also do what you expect.

net groupmap cleanup

solves a problem I've had two times now: Our SID changed, and a user's primary
group was mapped to a SID that is not ours. net groupmap cleanup removes all
mappings that are not from our domain sid.

Volker

source/utils/net.c
source/utils/net.h
source/utils/net_groupmap.c

index 01f56b8a0c722145ef1ac491e5ccac3a6a871bcc..8004ced43ebd9fa3fdb74e57c2add3e0e06cca7e 100644 (file)
@@ -73,6 +73,10 @@ int opt_flags = -1;
 int opt_timeout = 0;
 const char *opt_target_workgroup = NULL;
 int opt_machine_pass = 0;
+BOOL opt_localgroup = False;
+BOOL opt_domaingroup = False;
+const char *opt_newntname = "";
+int opt_rid = 0;
 
 BOOL opt_have_ip = False;
 struct in_addr opt_dest_ip;
@@ -680,6 +684,13 @@ static struct functable net_func[] = {
                {"timeout",     't', POPT_ARG_INT,    &opt_timeout},
                {"machine-pass",'P', POPT_ARG_NONE,   &opt_machine_pass},
                {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
+
+               /* Options for 'net groupmap set' */
+               {"local",       'L', POPT_ARG_NONE,   &opt_localgroup},
+               {"domain",      'D', POPT_ARG_NONE,   &opt_domaingroup},
+               {"ntname",      'N', POPT_ARG_STRING, &opt_newntname},
+               {"rid",         'R', POPT_ARG_INT,    &opt_rid},
+
                POPT_COMMON_SAMBA
                { 0, 0, 0, 0}
        };
index 94e6de9d181d2f6fa15197227db5010379f5bf4c..62d5a74237592296cb8c24c2347c0bf2494257cc 100644 (file)
@@ -57,6 +57,11 @@ extern const char *opt_user_name;
 extern const char *opt_password;
 extern BOOL opt_user_specified;
 
+extern BOOL opt_localgroup;
+extern BOOL opt_domaingroup;
+extern const char *opt_newntname;
+extern int opt_rid;
+
 extern BOOL opt_have_ip;
 extern struct in_addr opt_dest_ip;
 
index 416f42507d2eb7b52ad4d1488b5c80b6d89e7dc8..2b487ef17b4cb825644f5d1e3a99e3dc08942d59 100644 (file)
@@ -473,6 +473,141 @@ static int net_groupmap_delete(int argc, const char **argv)
        return 0;
 }
 
+static int net_groupmap_set(int argc, const char **argv)
+{
+       const char *ntgroup = NULL;
+       struct group *grp = NULL;
+       GROUP_MAP map;
+       BOOL have_map = False;
+
+       if ((argc < 1) || (argc > 2)) {
+               d_printf("Usage: net groupmap set \"NT Group\" "
+                        "[\"unix group\"] [-C \"comment\"] [-L] [-D]\n");
+               return -1;
+       }
+
+       if ( opt_localgroup && 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_printf("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_printf("Could not find group mapping for %s\n",
+                                ntgroup);
+                       return -1;
+               }
+
+               map.gid = grp->gr_gid;
+
+               if (opt_rid == 0) {
+                       opt_rid = pdb_gid_to_group_rid(map.gid);
+               }
+
+               sid_copy(&map.sid, get_global_sam_sid());
+               sid_append_rid(&map.sid, opt_rid);
+
+               map.sid_name_use = SID_NAME_DOM_GRP;
+               fstrcpy(map.nt_name, ntgroup);
+               fstrcpy(map.comment, "");
+
+               if (!pdb_add_group_mapping_entry(&map)) {
+                       d_printf("Could not add mapping entry for %s\n",
+                                ntgroup);
+                       return -1;
+               }
+       }
+
+       /* Now we have a mapping entry, update that stuff */
+
+       if ( opt_localgroup || opt_domaingroup ) {
+               if (map.sid_name_use == SID_NAME_WKN_GRP) {
+                       d_printf("Can't change type of the BUILTIN group %s\n",
+                                map.nt_name);
+                       return -1;
+               }
+       }
+
+       if (opt_localgroup)
+               map.sid_name_use = SID_NAME_ALIAS;
+
+       if (opt_domaingroup)
+               map.sid_name_use = SID_NAME_DOM_GRP;
+
+       /* The case (opt_domaingroup && opt_localgroup) was tested for above */
+
+       if (strlen(opt_comment) > 0)
+               fstrcpy(map.comment, opt_comment);
+
+       if (strlen(opt_newntname) > 0)
+               fstrcpy(map.nt_name, opt_newntname);
+
+       if (grp != NULL)
+               map.gid = grp->gr_gid;
+
+       if (!pdb_update_group_mapping_entry(&map)) {
+               d_printf("Could not update group mapping for %s\n", ntgroup);
+               return -1;
+       }
+
+       return 0;
+}
+
+static int net_groupmap_cleanup(int argc, const char **argv)
+{
+       GROUP_MAP *map = NULL;
+       int i, entries;
+
+       if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &entries,
+                                   ENUM_ALL_MAPPED)) {
+               d_printf("Could not list group mappings\n");
+               return -1;
+       }
+
+       for (i=0; i<entries; i++) {
+
+               if (map[i].sid_name_use == SID_NAME_WKN_GRP)
+                       continue;
+
+               if (map[i].gid == -1)
+                       printf("Group %s is not mapped\n", map[i].nt_name);
+
+               if (!sid_check_is_in_our_domain(&map[i].sid)) {
+                       printf("Deleting mapping for NT Group %s, sid %s\n",
+                              map[i].nt_name,
+                              sid_string_static(&map[i].sid));
+                       pdb_delete_group_mapping_entry(map[i].sid);
+               }
+       }
+
+       SAFE_FREE(map);
+
+       return 0;
+}
+
 int net_help_groupmap(int argc, const char **argv)
 {
        d_printf("net groupmap add"\
@@ -483,6 +618,10 @@ int net_help_groupmap(int argc, const char **argv)
                "\n  Remove a group mapping\n");
        d_printf("net groupmap list"\
                "\n  List current group map\n");
+       d_printf("net groupmap set"\
+               "\n  Set group mapping\n");
+       d_printf("net groupmap cleanup"\
+               "\n  Remove foreign group mapping entries\n");
        
        return -1;
 }
@@ -497,6 +636,8 @@ int net_groupmap(int argc, const char **argv)
                {"add", net_groupmap_add},
                {"modify", net_groupmap_modify},
                {"delete", net_groupmap_delete},
+               {"set", net_groupmap_set},
+               {"cleanup", net_groupmap_cleanup},
                {"list", net_groupmap_list},
                {"help", net_help_groupmap},
                {NULL, NULL}