r17669: Remove RID algorithm support from unmapped users and groups
[samba.git] / source3 / utils / net_groupmap.c
index a9f47172fe47b077e16322db03a6d090e4bfc3b5..4708efa908d9a5914298bc6b70fbc50cb484700a 100644 (file)
@@ -3,7 +3,8 @@
  *  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) 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
@@ -22,7 +23,7 @@
 
 
 #include "includes.h"
-#include "../utils/net.h"
+#include "utils/net.h"
 
 
 /*********************************************************
@@ -65,7 +66,7 @@ static BOOL get_sid_from_input(DOM_SID *sid, char *input)
 
        if (StrnCaseCmp( input, "S-", 2)) {
                /* Perhaps its the NT group name? */
-               if (!pdb_getgrnam(&map, input, MAPPING_WITHOUT_PRIV)) {
+               if (!pdb_getgrnam(&map, input)) {
                        printf("NT Group %s doesn't exist in mapping DB\n", input);
                        return False;
                } else {
@@ -86,82 +87,90 @@ static BOOL get_sid_from_input(DOM_SID *sid, char *input)
 
 static void print_map_entry ( GROUP_MAP map, BOOL long_list )
 {
-       fstring string_sid;
-       fstring group_type;
-       fstring priv_text;
-       
-       decode_sid_name_use(group_type, map.sid_name_use);
-       sid_to_string(string_sid, &map.sid);
-       convert_priv_to_text(&(map.priv_set), priv_text);
-               
        if (!long_list)
-               d_printf("%s (%s) -> %s\n", map.nt_name, string_sid, gidtoname(map.gid));
+               d_printf("%s (%s) -> %s\n", map.nt_name,
+                        sid_string_static(&map.sid), gidtoname(map.gid));
        else {
                d_printf("%s\n", map.nt_name);
-               d_printf("\tSID       : %s\n", string_sid);
+               d_printf("\tSID       : %s\n", sid_string_static(&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", group_type);
+               d_printf("\tGroup type: %s\n",
+                        sid_type_lookup(map.sid_name_use));
                d_printf("\tComment   : %s\n", map.comment);
-               d_printf("\tPrivilege : %s\n\n", priv_text);
        }
 
 }
 /*********************************************************
  List the groups.
 **********************************************************/
-int net_groupmap_list(int argc, const char **argv)
+static int net_groupmap_list(int argc, const char **argv)
 {
-       int entries;
+       size_t entries;
        BOOL long_list = False;
-       int i;
+       size_t i;
        fstring ntgroup = "";
+       fstring sid_string = "";
+
+       if (opt_verbose || opt_long_list_entries)
+               long_list = True;
        
        /* get the options */
        for ( i=0; i<argc; i++ ) {
                if ( !StrCaseCmp(argv[i], "verbose")) {
                        long_list = True;
                }
-               else if ( !StrnCaseCmp(argv[i], "name", strlen("name")) ) {
+               else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
                        fstrcpy( ntgroup, get_string_param( argv[i] ) );
                        if ( !ntgroup[0] ) {
-                               d_printf("must supply a name\n");
+                               d_fprintf(stderr, "must supply a name\n");
+                               return -1;
+                       }               
+               }
+               else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
+                       fstrcpy( sid_string, get_string_param( argv[i] ) );
+                       if ( !sid_string[0] ) {
+                               d_fprintf(stderr, "must supply a SID\n");
                                return -1;
                        }               
                }
                else {
-                       d_printf("Bad option: %s\n", argv[i]);
+                       d_fprintf(stderr, "Bad option: %s\n", argv[i]);
                        return -1;
                }
        }
 
        /* list a single group is given a name */
-       if ( ntgroup[0] ) {
+       if ( ntgroup[0] || sid_string[0] ) {
                DOM_SID sid;
                GROUP_MAP map;
+               
+               if ( sid_string[0] )
+                       fstrcpy( ntgroup, sid_string);
                        
                if (!get_sid_from_input(&sid, ntgroup)) {
                        return -1;
                }
 
                /* Get the current mapping from the database */
-               if(!pdb_getgrsid(&map, sid, MAPPING_WITH_PRIV)) {
-                       d_printf("Failure to local group SID in the database\n");
+               if(!pdb_getgrsid(&map, sid)) {
+                       d_fprintf(stderr, "Failure to local group SID in the database\n");
                        return -1;
                }
        
                print_map_entry( map, long_list );
-               free_privilege(&(map.priv_set));        
        }
        else {
                GROUP_MAP *map=NULL;
                /* enumerate all group mappings */
-               if ( !pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &entries, ENUM_ALL_MAPPED, MAPPING_WITH_PRIV) )
+               if (!pdb_enum_group_mapping(NULL, SID_NAME_UNKNOWN, &map, &entries, ENUM_ALL_MAPPED))
                        return -1;
        
                for (i=0; i<entries; i++) {
                        print_map_entry( map[i], long_list );
-                       free_privilege(&(map[i].priv_set));
                }
+
+               SAFE_FREE(map);
        }
 
        return 0;
@@ -171,46 +180,62 @@ int net_groupmap_list(int argc, const char **argv)
  Add a new group mapping entry
 **********************************************************/
 
-int net_groupmap_add(int argc, const char **argv)
+static int net_groupmap_add(int argc, const char **argv)
 {
-       PRIVILEGE_SET se_priv;
        DOM_SID sid;
        fstring ntgroup = "";
+       fstring unixgrp = "";
        fstring string_sid = "";
        fstring type = "";
        fstring ntcomment = "";
-       enum SID_NAME_USE sid_type = SID_NAME_UNKNOWN;
+       enum SID_NAME_USE sid_type = SID_NAME_DOM_GRP;
        uint32 rid = 0; 
        gid_t gid;
        int i;
+       GROUP_MAP map;
        
+       const char *name_type;
+
+       ZERO_STRUCT(map);
+
+       /* Default is domain group. */
+       map.sid_name_use = SID_NAME_DOM_GRP;
+       name_type = "domain group";
+
        /* get the options */
        for ( i=0; i<argc; i++ ) {
                if ( !StrnCaseCmp(argv[i], "rid", strlen("rid")) ) {
                        rid = get_int_param(argv[i]);
                        if ( rid < DOMAIN_GROUP_RID_ADMINS ) {
-                               d_printf("RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
+                               d_fprintf(stderr, "RID must be greater than %d\n", (uint32)DOMAIN_GROUP_RID_ADMINS-1);
                                return -1;
                        }
                }
-               else if ( !StrnCaseCmp(argv[i], "name", strlen("name")) ) {
+               else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
+                       fstrcpy( unixgrp, get_string_param( argv[i] ) );
+                       if ( !unixgrp[0] ) {
+                               d_fprintf(stderr, "must supply a name\n");
+                               return -1;
+                       }               
+               }
+               else if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
                        fstrcpy( ntgroup, get_string_param( argv[i] ) );
                        if ( !ntgroup[0] ) {
-                               d_printf("must supply a name\n");
+                               d_fprintf(stderr, "must supply a name\n");
                                return -1;
                        }               
                }
                else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
                        fstrcpy( string_sid, get_string_param( argv[i] ) );
                        if ( !string_sid[0] ) {
-                               d_printf("must supply a SID\n");
+                               d_fprintf(stderr, "must supply a SID\n");
                                return -1;
                        }               
                }
                else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
                        fstrcpy( ntcomment, get_string_param( argv[i] ) );
                        if ( !ntcomment[0] ) {
-                               d_printf("must supply a comment string\n");
+                               d_fprintf(stderr, "must supply a comment string\n");
                                return -1;
                        }                               
                }
@@ -220,28 +245,59 @@ int net_groupmap_add(int argc, const char **argv)
                                case 'b':
                                case 'B':
                                        sid_type = SID_NAME_WKN_GRP;
+                                       name_type = "wellknown group";
                                        break;
                                case 'd':
                                case 'D':
                                        sid_type = SID_NAME_DOM_GRP;
+                                       name_type = "domain group";
                                        break;
                                case 'l':
                                case 'L':
                                        sid_type = SID_NAME_ALIAS;
+                                       name_type = "alias (local) group";
                                        break;
+                               default:
+                                       d_fprintf(stderr, "unknown group type %s\n", type);
+                                       return -1;
                        }
                }
                else {
-                       d_printf("Bad option: %s\n", argv[i]);
+                       d_fprintf(stderr, "Bad option: %s\n", argv[i]);
                        return -1;
                }
        }
 
-       if ( !ntgroup[0] || (!rid && !string_sid[0]) || sid_type==SID_NAME_UNKNOWN ) {
-               d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} name=<string>| type=<domain|local|builtin> [comment=<string>]\n");
+       if ( !unixgrp[0] ) {
+               d_printf("Usage: net groupmap add {rid=<int>|sid=<string>} unixgroup=<string> [type=<domain|local|builtin>] [ntgroup=<string>] [comment=<string>]\n");
                return -1;
        }
        
+       if ( (gid = nametogid(unixgrp)) == (gid_t)-1 ) {
+               d_fprintf(stderr, "Can't lookup UNIX group %s\n", unixgrp);
+               return -1;
+       }
+
+       {
+               if (pdb_getgrgid(&map, gid)) {
+                       d_printf("Unix group %s already mapped to SID %s\n",
+                                unixgrp, sid_string_static(&map.sid));
+                       return -1;
+               }
+       }
+       
+       if ( (rid == 0) && (string_sid[0] == '\0') ) {
+               d_printf("No rid or sid specified, choosing a RID\n");
+               if (pdb_rid_algorithm()) {
+                       rid = algorithmic_pdb_gid_to_group_rid(gid);
+               } else {
+                       if (!pdb_new_rid(&rid)) {
+                               d_printf("Could not get new RID\n");
+                       }
+               }
+               d_printf("Got RID %d\n", rid);
+       }
+
        /* append the rid to our own domain/machine SID if we don't have a full SID */
        if ( !string_sid[0] ) {
                sid_copy(&sid, get_global_sam_sid());
@@ -249,55 +305,77 @@ int net_groupmap_add(int argc, const char **argv)
                sid_to_string(string_sid, &sid);
        }
 
-       if (ntcomment[0])
-               fstrcpy(ntcomment, "Local Unix group");
-               
-       if ( !(gid = nametogid(ntgroup)) ) {
-               d_printf("Can't lookup UNIX group %s\n", ntgroup);
-               return -1;
+       if (!ntcomment[0]) {
+               switch (sid_type) {
+               case SID_NAME_WKN_GRP:
+                       fstrcpy(ntcomment, "Wellknown Unix group");
+                       break;
+               case SID_NAME_DOM_GRP:
+                       fstrcpy(ntcomment, "Domain Unix group");
+                       break;
+               case SID_NAME_ALIAS:
+                       fstrcpy(ntcomment, "Local Unix group");
+                       break;
+               default:
+                       fstrcpy(ntcomment, "Unix group");
+                       break;
+               }
        }
+               
+       if (!ntgroup[0] )
+               fstrcpy( ntgroup, unixgrp );
+               
        
-       init_privilege(&se_priv);
-#if 0
-       if (privilege!=NULL)
-               convert_priv_from_text(&se_priv, privilege);
-#endif
-
-       if (!add_initial_entry(gid, string_sid, sid_type, ntgroup,
-                             ntcomment, se_priv, PR_ACCESS_FROM_NETWORK) ) {
-               d_printf("adding entry for group %s failed!\n", ntgroup);
+       if (!NT_STATUS_IS_OK(add_initial_entry(gid, string_sid, sid_type, ntgroup, ntcomment))) {
+               d_fprintf(stderr, "adding entry for group %s failed!\n", ntgroup);
                return -1;
        }
 
-       free_privilege(&se_priv);
-               
-       d_printf("Successully added group %s to the mapping db\n", ntgroup);
+       d_printf("Successfully added group %s to the mapping db as a %s\n",
+                ntgroup, name_type);
        return 0;
 }
 
-int net_groupmap_modify(int argc, const char **argv)
+static int net_groupmap_modify(int argc, const char **argv)
 {
        DOM_SID sid;
        GROUP_MAP map;
        fstring ntcomment = "";
        fstring type = "";
        fstring ntgroup = "";
+       fstring unixgrp = "";
+       fstring sid_string = "";
        enum SID_NAME_USE sid_type = SID_NAME_UNKNOWN;
        int i;
+       gid_t gid;
 
        /* get the options */
        for ( i=0; i<argc; i++ ) {
-               if ( !StrnCaseCmp(argv[i], "name", strlen("name")) ) {
+               if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
                        fstrcpy( ntgroup, get_string_param( argv[i] ) );
                        if ( !ntgroup[0] ) {
-                               d_printf("must supply a name\n");
+                               d_fprintf(stderr, "must supply a name\n");
+                               return -1;
+                       }               
+               }
+               else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
+                       fstrcpy( sid_string, get_string_param( argv[i] ) );
+                       if ( !sid_string[0] ) {
+                               d_fprintf(stderr, "must supply a name\n");
                                return -1;
                        }               
                }
                else if ( !StrnCaseCmp(argv[i], "comment", strlen("comment")) ) {
                        fstrcpy( ntcomment, get_string_param( argv[i] ) );
                        if ( !ntcomment[0] ) {
-                               d_printf("must supply a comment string\n");
+                               d_fprintf(stderr, "must supply a comment string\n");
+                               return -1;
+                       }                               
+               }
+               else if ( !StrnCaseCmp(argv[i], "unixgroup", strlen("unixgroup")) ) {
+                       fstrcpy( unixgrp, get_string_param( argv[i] ) );
+                       if ( !unixgrp[0] ) {
+                               d_fprintf(stderr, "must supply a group name\n");
                                return -1;
                        }                               
                }
@@ -315,23 +393,34 @@ int net_groupmap_modify(int argc, const char **argv)
                        }
                }
                else {
-                       d_printf("Bad option: %s\n", argv[i]);
+                       d_fprintf(stderr, "Bad option: %s\n", argv[i]);
                        return -1;
                }
        }
        
-       if ( !ntgroup[0] ) {
-               d_printf("Usage: net groupmap modify name=<string|SID> [comment=<string>] [type=<domain|local>\n");
+       if ( !ntgroup[0] && !sid_string[0] ) {
+               d_printf("Usage: net groupmap modify {ntgroup=<string>|sid=<SID>} [comment=<string>] [unixgroup=<string>] [type=<domain|local>]\n");
                return -1;
        }
-       
-       if (!get_sid_from_input(&sid, ntgroup)) {
-               return -1;
+
+       /* give preference to the SID; if both the ntgroup name and SID
+          are defined, use the SID and assume that the group name could be a 
+          new name */
+               
+       if ( sid_string[0] ) {  
+               if (!get_sid_from_input(&sid, sid_string)) {
+                       return -1;
+               }
        }
+       else {
+               if (!get_sid_from_input(&sid, ntgroup)) {
+                       return -1;
+               }
+       }       
 
        /* Get the current mapping from the database */
-       if(!pdb_getgrsid(&map, sid, MAPPING_WITH_PRIV)) {
-               d_printf("Failure to local group SID in the database\n");
+       if(!pdb_getgrsid(&map, sid)) {
+               d_fprintf(stderr, "Failure to local group SID in the database\n");
                return -1;
        }
        
@@ -339,78 +428,92 @@ int net_groupmap_modify(int argc, const char **argv)
         * Allow changing of group type only between domain and local
         * We disallow changing Builtin groups !!! (SID problem)
         */ 
-       if (sid_type==SID_NAME_ALIAS 
-           || sid_type==SID_NAME_DOM_GRP 
-           || sid_type==SID_NAME_UNKNOWN) 
-       {
-               if (map.sid_name_use==SID_NAME_ALIAS 
-                   || map.sid_name_use==SID_NAME_DOM_GRP
-                   || map.sid_name_use==SID_NAME_UNKNOWN) 
-               {
-                       map.sid_name_use=sid_type;
-               } else {
-                       printf("cannot change group type to builtin\n");
-               };
-       } else {
-               printf("cannot change group type from builtin\n");
+       if (sid_type == SID_NAME_UNKNOWN) {
+               d_fprintf(stderr, "Can't map to an unknown group type.\n");
+               return -1;
+        }
+
+       if (map.sid_name_use == SID_NAME_WKN_GRP) {
+               d_fprintf(stderr, "You can only change between domain and local groups.\n");
+               return -1;
        }
 
+       map.sid_name_use=sid_type;
+
        /* Change comment if new one */
        if ( ntcomment[0] )
                fstrcpy( map.comment, ntcomment );
+               
+       if ( ntgroup[0] )
+               fstrcpy( map.nt_name, ntgroup );
+               
+       if ( unixgrp[0] ) {
+               gid = nametogid( unixgrp );
+               if ( gid == -1 ) {
+                       d_fprintf(stderr, "Unable to lookup UNIX group %s.  Make sure the group exists.\n",
+                               unixgrp);
+                       return -1;
+               }
+               
+               map.gid = gid;
+       }
 
-#if 0
-       /* Change the privilege if new one */
-       if (privilege!=NULL)
-               convert_priv_from_text(&map.priv_set, privilege);
-#endif
-
-       if ( !pdb_update_group_mapping_entry(&map) ) {
-               d_printf("Could not update group database\n");
-               free_privilege(&map.priv_set);
+       if ( !NT_STATUS_IS_OK(pdb_update_group_mapping_entry(&map)) ) {
+               d_fprintf(stderr, "Could not update group database\n");
                return -1;
        }
        
-       free_privilege(&map.priv_set);
-       
-       d_printf("Updated mapping entry for %s\n", ntgroup);
+       d_printf("Updated mapping entry for %s\n", map.nt_name);
 
        return 0;
 }
 
-int net_groupmap_delete(int argc, const char **argv)
+static int net_groupmap_delete(int argc, const char **argv)
 {
        DOM_SID sid;
        fstring ntgroup = "";
+       fstring sid_string = "";
        int i;
 
        /* get the options */
        for ( i=0; i<argc; i++ ) {
-               if ( !StrnCaseCmp(argv[i], "name", strlen("name")) ) {
+               if ( !StrnCaseCmp(argv[i], "ntgroup", strlen("ntgroup")) ) {
                        fstrcpy( ntgroup, get_string_param( argv[i] ) );
                        if ( !ntgroup[0] ) {
-                               d_printf("must supply a name\n");
+                               d_fprintf(stderr, "must supply a name\n");
+                               return -1;
+                       }               
+               }
+               else if ( !StrnCaseCmp(argv[i], "sid", strlen("sid")) ) {
+                       fstrcpy( sid_string, get_string_param( argv[i] ) );
+                       if ( !sid_string[0] ) {
+                               d_fprintf(stderr, "must supply a SID\n");
                                return -1;
                        }               
                }
                else {
-                       d_printf("Bad option: %s\n", argv[i]);
+                       d_fprintf(stderr, "Bad option: %s\n", argv[i]);
                        return -1;
                }
        }
        
-       if ( !ntgroup[0] ) {
-               d_printf("Usage: net groupmap delete name=<string|SID>\n");
+       if ( !ntgroup[0] && !sid_string[0]) {
+               d_printf("Usage: net groupmap delete {ntgroup=<string>|sid=<SID>}\n");
                return -1;
        }
        
+       /* give preference to the SID if we have that */
+       
+       if ( sid_string[0] )
+               fstrcpy( ntgroup, sid_string );
+               
        if ( !get_sid_from_input(&sid, ntgroup) ) {
-               d_printf("Unable to resolve group %s to a SID\n", ntgroup);
+               d_fprintf(stderr, "Unable to resolve group %s to a SID\n", ntgroup);
                return -1;
        }
 
-       if ( !pdb_delete_group_mapping_entry(sid) ) {
-               printf("Failed to removing group %s from the mapping db!\n", ntgroup);
+       if ( !NT_STATUS_IS_OK(pdb_delete_group_mapping_entry(sid)) ) {
+               d_fprintf(stderr, "Failed to removing group %s from the mapping db!\n", ntgroup);
                return -1;
        }
 
@@ -419,3 +522,331 @@ 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_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 (opt_rid == 0) {
+                       if ( pdb_rid_algorithm() )
+                               opt_rid = algorithmic_pdb_gid_to_group_rid(map.gid);
+                       else {
+                               if ( !pdb_new_rid((uint32*)&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, 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 ( opt_localgroup || 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 (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 (!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(int argc, const char **argv)
+{
+       GROUP_MAP *map = NULL;
+       size_t i, entries;
+
+       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; i<entries; i++) {
+
+               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;
+}
+
+static int net_groupmap_addmem(int argc, const char **argv)
+{
+       DOM_SID alias, member;
+
+       if ( (argc != 2) || 
+            !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(int argc, const char **argv)
+{
+       DOM_SID alias, member;
+
+       if ( (argc != 2) || 
+            !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(int argc, const char **argv)
+{
+       DOM_SID alias;
+       DOM_SID *members;
+       size_t i, num;
+
+       if ( (argc != 1) || 
+            !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_static(&(members[i])));
+       }
+
+       SAFE_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_static(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_static(&alias));
+       }
+
+       return True;
+}
+
+static int net_groupmap_memberships(int argc, const char **argv)
+{
+       TALLOC_CTX *mem_ctx;
+       DOM_SID *domain_sid, *builtin_sid, member;
+
+       if ( (argc != 1) || 
+            !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;
+}
+
+int net_help_groupmap(int argc, const char **argv)
+{
+       d_printf("net groupmap add"\
+               "\n  Create a new group mapping\n");
+       d_printf("net groupmap modify"\
+               "\n  Update a group mapping\n");
+       d_printf("net groupmap delete"\
+               "\n  Remove a group mapping\n");
+       d_printf("net groupmap addmem"\
+                "\n  Add a foreign alias member\n");
+       d_printf("net groupmap delmem"\
+                "\n  Delete a foreign alias member\n");
+       d_printf("net groupmap listmem"\
+                "\n  List foreign group members\n");
+       d_printf("net groupmap memberships"\
+                "\n  List foreign group memberships\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;
+}
+
+
+/***********************************************************
+ migrated functionality from smbgroupedit
+ **********************************************************/
+int net_groupmap(int argc, const char **argv)
+{
+       struct functable func[] = {
+               {"add", net_groupmap_add},
+               {"modify", net_groupmap_modify},
+               {"delete", net_groupmap_delete},
+               {"set", net_groupmap_set},
+               {"cleanup", net_groupmap_cleanup},
+               {"addmem", net_groupmap_addmem},
+               {"delmem", net_groupmap_delmem},
+               {"listmem", net_groupmap_listmem},
+               {"memberships", net_groupmap_memberships},
+               {"list", net_groupmap_list},
+               {"help", net_help_groupmap},
+               {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;
+       }
+       
+       if ( argc )
+               return net_run_function(argc, argv, func, net_help_groupmap);
+
+       return net_help_groupmap( argc, argv );
+}
+