Make "net ads listmem" also work for domain groups
[ira/wip.git] / source3 / utils / net_sam.c
index 32656f02763df284a8be4cdc936ab77b2776799f..ddf4d4e16214f992d4acf31a26fa1018d84da618 100644 (file)
@@ -36,7 +36,7 @@ static int net_sam_userset(struct net_context *c, int argc, const char **argv,
        const char *dom, *name;
        NTSTATUS status;
 
-       if (argc != 2) {
+       if (argc != 2 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam set %s <user> <value>\n",
                          field);
                return -1;
@@ -139,8 +139,9 @@ static int net_sam_set_userflag(struct net_context *c, int argc,
        NTSTATUS status;
        uint16 acct_flags;
 
-       if ((argc != 2) || (!strequal(argv[1], "yes") &&
-                           !strequal(argv[1], "no"))) {
+       if ((argc != 2) || c->display_usage ||
+           (!strequal(argv[1], "yes") &&
+            !strequal(argv[1], "no"))) {
                d_fprintf(stderr, "usage: net sam set %s <user> [yes|no]\n",
                          field);
                return -1;
@@ -229,8 +230,9 @@ static int net_sam_set_pwdmustchangenow(struct net_context *c, int argc,
        const char *dom, *name;
        NTSTATUS status;
 
-       if ((argc != 2) || (!strequal(argv[1], "yes") &&
-                           !strequal(argv[1], "no"))) {
+       if ((argc != 2) || c->display_usage ||
+           (!strequal(argv[1], "yes") &&
+            !strequal(argv[1], "no"))) {
                d_fprintf(stderr, "usage: net sam set pwdmustchangenow <user> [yes|no]\n");
                return -1;
        }
@@ -291,7 +293,7 @@ static int net_sam_set_comment(struct net_context *c, int argc,
        const char *dom, *name;
        NTSTATUS status;
 
-       if (argc != 2) {
+       if (argc != 2 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam set comment <name> "
                          "<comment>\n");
                return -1;
@@ -338,35 +340,107 @@ static int net_sam_set_comment(struct net_context *c, int argc,
 
 static int net_sam_set(struct net_context *c, int argc, const char **argv)
 {
-       struct functable2 func[] = {
-               { "homedir", net_sam_set_homedir,
-                 "Change a user's home directory" },
-               { "profilepath", net_sam_set_profilepath,
-                 "Change a user's profile path" },
-               { "comment", net_sam_set_comment,
-                 "Change a users or groups description" },
-               { "fullname", net_sam_set_fullname,
-                 "Change a user's full name" },
-               { "logonscript", net_sam_set_logonscript,
-                 "Change a user's logon script" },
-               { "homedrive", net_sam_set_homedrive,
-                 "Change a user's home drive" },
-               { "workstations", net_sam_set_workstations,
-                 "Change a user's allowed workstations" },
-               { "disabled", net_sam_set_disabled,
-                 "Disable/Enable a user" },
-               { "pwnotreq", net_sam_set_pwnotreq,
-                 "Disable/Enable the password not required flag" },
-               { "autolock", net_sam_set_autolock,
-                 "Disable/Enable a user's lockout flag" },
-               { "pwnoexp", net_sam_set_pwnoexp,
-                 "Disable/Enable whether a user's pw does not expire" },
-               { "pwdmustchangenow", net_sam_set_pwdmustchangenow,
-                 "Force users password must change at next logon" },
-               {NULL, NULL}
+       struct functable func[] = {
+               {
+                       "homedir",
+                       net_sam_set_homedir,
+                       NET_TRANSPORT_LOCAL,
+                       "Change a user's home directory",
+                       "net sam set homedir\n"
+                       "    Change a user's home directory"
+               },
+               {
+                       "profilepath",
+                       net_sam_set_profilepath,
+                       NET_TRANSPORT_LOCAL,
+                       "Change a user's profile path",
+                       "net sam set profilepath\n"
+                       "    Change a user's profile path"
+               },
+               {
+                       "comment",
+                       net_sam_set_comment,
+                       NET_TRANSPORT_LOCAL,
+                       "Change a users or groups description",
+                       "net sam set comment\n"
+                       "    Change a users or groups description"
+               },
+               {
+                       "fullname",
+                       net_sam_set_fullname,
+                       NET_TRANSPORT_LOCAL,
+                       "Change a user's full name",
+                       "net sam set fullname\n"
+                       "    Change a user's full name"
+               },
+               {
+                       "logonscript",
+                       net_sam_set_logonscript,
+                       NET_TRANSPORT_LOCAL,
+                       "Change a user's logon script",
+                       "net sam set logonscript\n"
+                       "    Change a user's logon script"
+               },
+               {
+                       "homedrive",
+                       net_sam_set_homedrive,
+                       NET_TRANSPORT_LOCAL,
+                       "Change a user's home drive",
+                       "net sam set homedrive\n"
+                       "    Change a user's home drive"
+               },
+               {
+                       "workstations",
+                       net_sam_set_workstations,
+                       NET_TRANSPORT_LOCAL,
+                       "Change a user's allowed workstations",
+                       "net sam set workstations\n"
+                       "    Change a user's allowed workstations"
+               },
+               {
+                       "disabled",
+                       net_sam_set_disabled,
+                       NET_TRANSPORT_LOCAL,
+                       "Disable/Enable a user",
+                       "net sam set disable\n"
+                       "    Disable/Enable a user"
+               },
+               {
+                       "pwnotreq",
+                       net_sam_set_pwnotreq,
+                       NET_TRANSPORT_LOCAL,
+                       "Disable/Enable the password not required flag",
+                       "net sam set pwnotreq\n"
+                       "    Disable/Enable the password not required flag"
+               },
+               {
+                       "autolock",
+                       net_sam_set_autolock,
+                       NET_TRANSPORT_LOCAL,
+                       "Disable/Enable a user's lockout flag",
+                       "net sam set autolock\n"
+                       "    Disable/Enable a user's lockout flag"
+               },
+               {
+                       "pwnoexp",
+                       net_sam_set_pwnoexp,
+                       NET_TRANSPORT_LOCAL,
+                       "Disable/Enable whether a user's pw does not expire",
+                       "net sam set pwnoexp\n"
+                       "    Disable/Enable whether a user's pw does not expire"
+               },
+               {
+                       "pwdmustchangenow",
+                       net_sam_set_pwdmustchangenow,
+                       NET_TRANSPORT_LOCAL,
+                       "Force users password must change at next logon",
+                       "net sam set pwdmustchangenow\n"
+                       "    Force users password must change at next logon"
+               },
+               {NULL, NULL, 0, NULL, NULL}
        };
 
-       return net_run_function2(c, argc, argv, "net sam set", func);
+       return net_run_function(c, argc, argv, "net sam set", func);
 }
 
 /*
@@ -381,7 +455,7 @@ static int net_sam_policy_set(struct net_context *c, int argc, const char **argv
        int field;
        char *endptr;
 
-        if (argc != 2) {
+        if (argc != 2 || c->display_usage) {
                 d_fprintf(stderr, "usage: net sam policy set " 
                          "\"<account policy>\" <value> \n");
                 return -1;
@@ -447,7 +521,7 @@ static int net_sam_policy_show(struct net_context *c, int argc, const char **arg
         uint32 old_value;
         int field;
 
-        if (argc != 1) {
+        if (argc != 1 || c->display_usage) {
                 d_fprintf(stderr, "usage: net sam policy show"
                          " \"<account policy>\" \n");
                 return -1;
@@ -491,6 +565,14 @@ static int net_sam_policy_list(struct net_context *c, int argc, const char **arg
        const char **names;
        int count;
        int i;
+
+       if (c->display_usage) {
+               d_printf("Usage:\n"
+                        "net sam policy list\n"
+                        "    List account policies\n");
+               return 0;
+       }
+
        account_policy_names_list(&names, &count);
         if (count != 0) {
                d_fprintf(stderr, "Valid account policies "
@@ -505,17 +587,35 @@ static int net_sam_policy_list(struct net_context *c, int argc, const char **arg
 
 static int net_sam_policy(struct net_context *c, int argc, const char **argv)
 {
-        struct functable2 func[] = {
-               { "list", net_sam_policy_list,
-                  "List account policies" },
-                { "show", net_sam_policy_show,
-                  "Show account policies" },
-               { "set", net_sam_policy_set,
-                  "Change account policies" },
-                {NULL, NULL}
+        struct functable func[] = {
+               {
+                       "list",
+                       net_sam_policy_list,
+                       NET_TRANSPORT_LOCAL,
+                       "List account policies",
+                       "net sam policy list\n"
+                       "    List account policies"
+               },
+                {
+                       "show",
+                       net_sam_policy_show,
+                       NET_TRANSPORT_LOCAL,
+                       "Show account policies",
+                       "net sam policy show\n"
+                       "    Show account policies"
+               },
+               {
+                       "set",
+                       net_sam_policy_set,
+                       NET_TRANSPORT_LOCAL,
+                       "Change account policies",
+                       "net sam policy set\n"
+                       "    Change account policies"
+               },
+                {NULL, NULL, 0, NULL, NULL}
         };
 
-        return net_run_function2(c, argc, argv, "net sam policy", func);
+        return net_run_function(c, argc, argv, "net sam policy", func);
 }
 
 extern PRIVS privs[];
@@ -525,7 +625,7 @@ static int net_sam_rights_list(struct net_context *c, int argc,
 {
        SE_PRIV mask;
 
-       if (argc > 1) {
+       if (argc > 1 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam rights list [privilege name]\n");
                return -1;
        }
@@ -579,7 +679,7 @@ static int net_sam_rights_grant(struct net_context *c, int argc,
        const char *dom, *name;
        SE_PRIV mask;
 
-       if (argc != 2) {
+       if (argc != 2 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam rights grant <name> "
                          "<right>\n");
                return -1;
@@ -612,7 +712,7 @@ static int net_sam_rights_revoke(struct net_context *c, int argc, const char **a
        const char *dom, *name;
        SE_PRIV mask;
 
-       if (argc != 2) {
+       if (argc != 2 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam rights revoke <name> "
                          "<right>\n");
                return -1;
@@ -640,16 +740,34 @@ static int net_sam_rights_revoke(struct net_context *c, int argc, const char **a
 
 static int net_sam_rights(struct net_context *c, int argc, const char **argv)
 {
-       struct functable2 func[] = {
-               { "list", net_sam_rights_list,
-                 "List possible user rights" },
-               { "grant", net_sam_rights_grant,
-                 "Grant a right" },
-               { "revoke", net_sam_rights_revoke,
-                 "Revoke a right" },
-               { NULL }
+       struct functable func[] = {
+               {
+                       "list",
+                       net_sam_rights_list,
+                       NET_TRANSPORT_LOCAL,
+                       "List possible user rights",
+                       "net sam rights list\n"
+                       "    List possible user rights"
+               },
+               {
+                       "grant",
+                       net_sam_rights_grant,
+                       NET_TRANSPORT_LOCAL,
+                       "Grant a right",
+                       "net sam rights grant\n"
+                       "    Grant a right"
+               },
+               {
+                       "revoke",
+                       net_sam_rights_revoke,
+                       NET_TRANSPORT_LOCAL,
+                       "Revoke a right",
+                       "net sam rights revoke\n"
+                       "    Revoke a right"
+               },
+               {NULL, NULL, 0, NULL, NULL}
        };
-        return net_run_function2(c, argc, argv, "net sam rights", func);
+        return net_run_function(c, argc, argv, "net sam rights", func);
 }
 
 /*
@@ -717,7 +835,7 @@ static int net_sam_mapunixgroup(struct net_context *c, int argc, const char **ar
        GROUP_MAP map;
        struct group *grp;
 
-       if (argc != 1) {
+       if (argc != 1 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam mapunixgroup <name>\n");
                return -1;
        }
@@ -779,7 +897,7 @@ static int net_sam_unmapunixgroup(struct net_context *c, int argc, const char **
        GROUP_MAP map;
        struct group *grp;
 
-       if (argc != 1) {
+       if (argc != 1 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam unmapunixgroup <name>\n");
                return -1;
        }
@@ -803,6 +921,34 @@ static int net_sam_unmapunixgroup(struct net_context *c, int argc, const char **
        return 0;
 }
 
+/*
+ * Create a domain group
+ */
+
+static int net_sam_createdomaingroup(struct net_context *c, int argc,
+                                    const char **argv)
+{
+       NTSTATUS status;
+       uint32 rid;
+
+       if (argc != 1 || c->display_usage) {
+               d_fprintf(stderr, "usage: net sam createdomaingroup <name>\n");
+               return -1;
+       }
+
+       status = pdb_create_dom_group(talloc_tos(), argv[0], &rid);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, "Creating %s failed with %s\n",
+                         argv[0], nt_errstr(status));
+               return -1;
+       }
+
+       d_printf("Created domain group %s with RID %d\n", argv[0], rid);
+
+       return 0;
+}
+
 /*
  * Create a local group
  */
@@ -812,7 +958,7 @@ static int net_sam_createlocalgroup(struct net_context *c, int argc, const char
        NTSTATUS status;
        uint32 rid;
 
-       if (argc != 1) {
+       if (argc != 1 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam createlocalgroup <name>\n");
                return -1;
        }
@@ -847,7 +993,7 @@ static int net_sam_deletelocalgroup(struct net_context *c, int argc, const char
         const char *dom, *name;
        NTSTATUS status;
 
-       if (argc != 1) {
+       if (argc != 1 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam deletelocalgroup <name>\n");
                return -1;
        }
@@ -878,7 +1024,7 @@ static int net_sam_deletelocalgroup(struct net_context *c, int argc, const char
 }
 
 /*
- * Create a local group
+ * Create a builtin group
  */
 
 static int net_sam_createbuiltingroup(struct net_context *c, int argc, const char **argv)
@@ -889,13 +1035,13 @@ static int net_sam_createbuiltingroup(struct net_context *c, int argc, const cha
        fstring groupname;
        DOM_SID sid;
 
-       if (argc != 1) {
+       if (argc != 1 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam createbuiltingroup <name>\n");
                return -1;
        }
 
        if (!winbind_ping()) {
-               d_fprintf(stderr, "winbind seems not to run. createlocalgroup "
+               d_fprintf(stderr, "winbind seems not to run. createbuiltingroup "
                          "only works when winbind runs.\n");
                return -1;
        }
@@ -940,7 +1086,7 @@ static int net_sam_addmem(struct net_context *c, int argc, const char **argv)
        enum lsa_SidType grouptype, membertype;
        NTSTATUS status;
 
-       if (argc != 2) {
+       if (argc != 2 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam addmem <group> <member>\n");
                return -1;
        }
@@ -1013,7 +1159,7 @@ static int net_sam_delmem(struct net_context *c, int argc, const char **argv)
        enum lsa_SidType grouptype;
        NTSTATUS status;
 
-       if (argc != 2) {
+       if (argc != 2 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam delmem <group> <member>\n");
                return -1;
        }
@@ -1068,10 +1214,12 @@ static int net_sam_listmem(struct net_context *c, int argc, const char **argv)
 {
        const char *groupdomain, *groupname;
        DOM_SID group;
+       DOM_SID *members = NULL;
+       size_t i, num_members = 0;
        enum lsa_SidType grouptype;
        NTSTATUS status;
 
-       if (argc != 1) {
+       if (argc != 1 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam listmem <group>\n");
                return -1;
        }
@@ -1084,36 +1232,55 @@ static int net_sam_listmem(struct net_context *c, int argc, const char **argv)
 
        if ((grouptype == SID_NAME_ALIAS) ||
            (grouptype == SID_NAME_WKN_GRP)) {
-               DOM_SID *members = NULL;
-               size_t i, num_members = 0;
-
-               status = pdb_enum_aliasmem(&group, &members, &num_members);
+               status = pdb_enum_aliasmem(&group, talloc_tos(), &members,
+                                          &num_members);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_fprintf(stderr, "Listing group members failed with "
+                                 "%s\n", nt_errstr(status));
+                       return -1;
+               }
+       } else if (grouptype == SID_NAME_DOM_GRP) {
+               uint32_t *rids;
 
+               status = pdb_enum_group_members(talloc_tos(), &group,
+                                               &rids, &num_members);
                if (!NT_STATUS_IS_OK(status)) {
                        d_fprintf(stderr, "Listing group members failed with "
                                  "%s\n", nt_errstr(status));
                        return -1;
                }
 
-               d_printf("%s\\%s has %u members\n", groupdomain, groupname,
-                        (unsigned int)num_members);
-               for (i=0; i<num_members; i++) {
-                       const char *dom, *name;
-                       if (lookup_sid(talloc_tos(), &members[i],
-                                      &dom, &name, NULL)) {
-                               d_printf(" %s\\%s\n", dom, name);
-                       } else {
-                               d_printf(" %s\n", sid_string_tos(&members[i]));
-                       }
+               members = talloc_array(talloc_tos(), struct dom_sid,
+                                      num_members);
+               if (members == NULL) {
+                       TALLOC_FREE(rids);
+                       return -1;
                }
 
-               TALLOC_FREE(members);
+               for (i=0; i<num_members; i++) {
+                       sid_compose(&members[i], get_global_sam_sid(),
+                                   rids[i]);
+               }
+               TALLOC_FREE(rids);
        } else {
                d_fprintf(stderr, "Can only list local group members so far.\n"
                          "%s is a %s\n", argv[0], sid_type_lookup(grouptype));
                return -1;
        }
 
+       d_printf("%s\\%s has %u members\n", groupdomain, groupname,
+                (unsigned int)num_members);
+       for (i=0; i<num_members; i++) {
+               const char *dom, *name;
+               if (lookup_sid(talloc_tos(), &members[i], &dom, &name, NULL)) {
+                       d_printf(" %s\\%s\n", dom, name);
+               } else {
+                       d_printf(" %s\n", sid_string_tos(&members[i]));
+               }
+       }
+
+               TALLOC_FREE(members);
+
        return 0;
 }
 
@@ -1125,7 +1292,7 @@ static int net_sam_do_list(struct net_context *c, int argc, const char **argv,
 {
        bool verbose = (argc == 1);
 
-       if ((argc > 1) ||
+       if ((argc > 1) || c->display_usage ||
            ((argc == 1) && !strequal(argv[0], "verbose"))) {
                d_fprintf(stderr, "usage: net sam list %s [verbose]\n", what);
                return -1;
@@ -1136,7 +1303,7 @@ static int net_sam_do_list(struct net_context *c, int argc, const char **argv,
                return -1;
        }
 
-       while (True) {
+       while (true) {
                struct samr_displayentry entry;
                if (!search->next_entry(search, &entry)) {
                        break;
@@ -1151,28 +1318,31 @@ static int net_sam_do_list(struct net_context *c, int argc, const char **argv,
                }
        }
 
-       pdb_search_destroy(search);
+       TALLOC_FREE(search);
        return 0;
 }
 
 static int net_sam_list_users(struct net_context *c, int argc,
                              const char **argv)
 {
-       return net_sam_do_list(c, argc, argv, pdb_search_users(ACB_NORMAL),
+       return net_sam_do_list(c, argc, argv,
+                              pdb_search_users(talloc_tos(), ACB_NORMAL),
                               "users");
 }
 
 static int net_sam_list_groups(struct net_context *c, int argc,
                               const char **argv)
 {
-       return net_sam_do_list(c, argc, argv, pdb_search_groups(), "groups");
+       return net_sam_do_list(c, argc, argv, pdb_search_groups(talloc_tos()),
+                              "groups");
 }
 
 static int net_sam_list_localgroups(struct net_context *c, int argc,
                                    const char **argv)
 {
        return net_sam_do_list(c, argc, argv,
-                              pdb_search_aliases(get_global_sam_sid()),
+                              pdb_search_aliases(talloc_tos(),
+                                                 get_global_sam_sid()),
                               "localgroups");
 }
 
@@ -1180,7 +1350,8 @@ static int net_sam_list_builtin(struct net_context *c, int argc,
                                const char **argv)
 {
        return net_sam_do_list(c, argc, argv,
-                              pdb_search_aliases(&global_sid_Builtin),
+                              pdb_search_aliases(talloc_tos(),
+                                                 &global_sid_Builtin),
                               "builtin");
 }
 
@@ -1188,7 +1359,7 @@ static int net_sam_list_workstations(struct net_context *c, int argc,
                                     const char **argv)
 {
        return net_sam_do_list(c, argc, argv,
-                              pdb_search_users(ACB_WSTRUST),
+                              pdb_search_users(talloc_tos(), ACB_WSTRUST),
                               "workstations");
 }
 
@@ -1198,21 +1369,51 @@ static int net_sam_list_workstations(struct net_context *c, int argc,
 
 static int net_sam_list(struct net_context *c, int argc, const char **argv)
 {
-       struct functable2 func[] = {
-               { "users", net_sam_list_users,
-                 "List SAM users" },
-               { "groups", net_sam_list_groups,
-                 "List SAM groups" },
-               { "localgroups", net_sam_list_localgroups,
-                 "List SAM local groups" },
-               { "builtin", net_sam_list_builtin,
-                 "List builtin groups" },
-               { "workstations", net_sam_list_workstations,
-                 "List domain member workstations" },
-               {NULL, NULL}
+       struct functable func[] = {
+               {
+                       "users",
+                       net_sam_list_users,
+                       NET_TRANSPORT_LOCAL,
+                       "List SAM users",
+                       "net sam list users\n"
+                       "    List SAM users"
+               },
+               {
+                       "groups",
+                       net_sam_list_groups,
+                       NET_TRANSPORT_LOCAL,
+                       "List SAM groups",
+                       "net sam list groups\n"
+                       "    List SAM groups"
+               },
+               {
+                       "localgroups",
+                       net_sam_list_localgroups,
+                       NET_TRANSPORT_LOCAL,
+                       "List SAM local groups",
+                       "net sam list localgroups\n"
+                       "    List SAM local groups"
+               },
+               {
+                       "builtin",
+                       net_sam_list_builtin,
+                       NET_TRANSPORT_LOCAL,
+                       "List builtin groups",
+                       "net sam list builtin\n"
+                       "    List builtin groups"
+               },
+               {
+                       "workstations",
+                       net_sam_list_workstations,
+                       NET_TRANSPORT_LOCAL,
+                       "List domain member workstations",
+                       "net sam list workstations\n"
+                       "    List domain member workstations"
+               },
+               {NULL, NULL, 0, NULL, NULL}
        };
 
-       return net_run_function2(c, argc, argv, "net sam list", func);
+       return net_run_function(c, argc, argv, "net sam list", func);
 }
 
 /*
@@ -1225,7 +1426,7 @@ static int net_sam_show(struct net_context *c, int argc, const char **argv)
        enum lsa_SidType type;
        const char *dom, *name;
 
-       if (argc != 1) {
+       if (argc != 1 || c->display_usage) {
                d_fprintf(stderr, "usage: net sam show <name>\n");
                return -1;
        }
@@ -1263,6 +1464,13 @@ static int net_sam_provision(struct net_context *c, int argc, const char **argv)
        struct samu *samuser;
        struct passwd *pwd;
 
+       if (c->display_usage) {
+               d_printf("Usage:\n"
+                        "net sam provision\n"
+                        "    Init an LDAP tree with default users/groups\n");
+               return 0;
+       }
+
        tc = talloc_new(NULL);
        if (!tc) {
                d_fprintf(stderr, "Out of Memory!\n");
@@ -1288,8 +1496,8 @@ static int net_sam_provision(struct net_context *c, int argc, const char **argv)
                goto failed;
        }
        
-       if (!lp_parm_bool(-1, "ldapsam", "trusted", False) ||
-           !lp_parm_bool(-1, "ldapsam", "editposix", False)) {
+       if (!lp_parm_bool(-1, "ldapsam", "trusted", false) ||
+           !lp_parm_bool(-1, "ldapsam", "editposix", false)) {
 
                d_fprintf(stderr, "Provisioning works only if ldapsam:trusted"
                                  " and ldapsam:editposix are enabled.\n");
@@ -1331,7 +1539,7 @@ static int net_sam_provision(struct net_context *c, int argc, const char **argv)
                uname = talloc_strdup(tc, "domusers");
                wname = talloc_strdup(tc, "Domain Users");
                dn = talloc_asprintf(tc, "cn=%s,%s", "domusers", lp_ldap_group_suffix());
-               gidstr = talloc_asprintf(tc, "%d", domusers_gid);
+               gidstr = talloc_asprintf(tc, "%u", (unsigned int)domusers_gid);
                gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
 
                if (!uname || !wname || !dn || !gidstr || !gtype) {
@@ -1386,7 +1594,7 @@ domu_done:
                uname = talloc_strdup(tc, "domadmins");
                wname = talloc_strdup(tc, "Domain Admins");
                dn = talloc_asprintf(tc, "cn=%s,%s", "domadmins", lp_ldap_group_suffix());
-               gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
+               gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
                gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
 
                if (!uname || !wname || !dn || !gidstr || !gtype) {
@@ -1449,8 +1657,8 @@ doma_done:
                }
                name = talloc_strdup(tc, "Administrator");
                dn = talloc_asprintf(tc, "uid=Administrator,%s", lp_ldap_user_suffix());
-               uidstr = talloc_asprintf(tc, "%d", uid);
-               gidstr = talloc_asprintf(tc, "%d", domadmins_gid);
+               uidstr = talloc_asprintf(tc, "%u", (unsigned int)uid);
+               gidstr = talloc_asprintf(tc, "%u", (unsigned int)domadmins_gid);
                dir = talloc_sub_specified(tc, lp_template_homedir(),
                                                "Administrator",
                                                get_global_sam_name(),
@@ -1540,8 +1748,8 @@ doma_done:
                sid_compose(&sid, get_global_sam_sid(), DOMAIN_USER_RID_GUEST);
 
                dn = talloc_asprintf(tc, "uid=%s,%s", pwd->pw_name, lp_ldap_user_suffix ());
-               uidstr = talloc_asprintf(tc, "%d", pwd->pw_uid);
-               gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
+               uidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_uid);
+               gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
                if (!dn || !uidstr || !gidstr) {
                        d_fprintf(stderr, "Out of Memory!\n");
                        goto failed;
@@ -1580,7 +1788,7 @@ doma_done:
 
        d_printf("Checking Guest's group.\n");
 
-       pwd = getpwnam_alloc(NULL, lp_guestaccount());
+       pwd = getpwnam_alloc(talloc_autofree_context(), lp_guestaccount());
        if (!pwd) {
                d_fprintf(stderr, "Failed to find just created Guest account!\n"
                                  "   Is nss properly configured?!\n");
@@ -1606,7 +1814,7 @@ doma_done:
                uname = talloc_strdup(tc, "domguests");
                wname = talloc_strdup(tc, "Domain Guests");
                dn = talloc_asprintf(tc, "cn=%s,%s", "domguests", lp_ldap_group_suffix());
-               gidstr = talloc_asprintf(tc, "%d", pwd->pw_gid);
+               gidstr = talloc_asprintf(tc, "%u", (unsigned int)pwd->pw_gid);
                gtype = talloc_asprintf(tc, "%d", SID_NAME_DOM_GRP);
 
                if (!uname || !wname || !dn || !gidstr || !gtype) {
@@ -1653,38 +1861,132 @@ failed:
  **********************************************************/
 int net_sam(struct net_context *c, int argc, const char **argv)
 {
-       struct functable2 func[] = {
-               { "createbuiltingroup", net_sam_createbuiltingroup,
-                 "Create a new BUILTIN group" },
-               { "createlocalgroup", net_sam_createlocalgroup,
-                 "Create a new local group" },
-               { "deletelocalgroup", net_sam_deletelocalgroup,
-                 "Delete an existing local group" },
-               { "mapunixgroup", net_sam_mapunixgroup,
-                 "Map a unix group to a domain group" },
-               { "unmapunixgroup", net_sam_unmapunixgroup,
-                 "Remove a group mapping of an unix group to a domain group" },
-               { "addmem", net_sam_addmem,
-                 "Add a member to a group" },
-               { "delmem", net_sam_delmem,
-                 "Delete a member from a group" },
-               { "listmem", net_sam_listmem,
-                 "List group members" },
-               { "list", net_sam_list,
-                 "List users, groups and local groups" },
-               { "show", net_sam_show,
-                 "Show details of a SAM entry" },
-               { "set", net_sam_set,
-                 "Set details of a SAM account" },
-               { "policy", net_sam_policy,
-                 "Set account policies" },
-               { "rights", net_sam_rights,
-                 "Manipulate user privileges" },
+       struct functable func[] = {
+               {
+                       "createbuiltingroup",
+                       net_sam_createbuiltingroup,
+                       NET_TRANSPORT_LOCAL,
+                       "Create a new BUILTIN group",
+                       "net sam createbuiltingroup\n"
+                       "    Create a new BUILTIN group"
+               },
+               {
+                       "createlocalgroup",
+                       net_sam_createlocalgroup,
+                       NET_TRANSPORT_LOCAL,
+                       "Create a new local group",
+                       "net sam createlocalgroup\n"
+                       "    Create a new local group"
+               },
+               {
+                       "createdomaingroup",
+                       net_sam_createdomaingroup,
+                       NET_TRANSPORT_LOCAL,
+                       "Create a new group",
+                       "net sam createdomaingroup\n"
+                       "    Create a new group"
+               },
+               {
+                       "deletelocalgroup",
+                       net_sam_deletelocalgroup,
+                       NET_TRANSPORT_LOCAL,
+                       "Delete an existing local group",
+                       "net sam deletelocalgroup\n"
+                       "    Delete an existing local group"
+               },
+               {
+                       "mapunixgroup",
+                       net_sam_mapunixgroup,
+                       NET_TRANSPORT_LOCAL,
+                       "Map a unix group to a domain group",
+                       "net sam mapunixgroup\n"
+                       "    Map a unix group to a domain group"
+               },
+               {
+                       "unmapunixgroup",
+                       net_sam_unmapunixgroup,
+                       NET_TRANSPORT_LOCAL,
+                       "Remove a group mapping of an unix group to a domain "
+                       "group",
+                       "net sam unmapunixgroup\n"
+                       "    Remove a group mapping of an unix group to a "
+                       "domain group"
+               },
+               {
+                       "addmem",
+                       net_sam_addmem,
+                       NET_TRANSPORT_LOCAL,
+                       "Add a member to a group",
+                       "net sam addmem\n"
+                       "    Add a member to a group"
+               },
+               {
+                       "delmem",
+                       net_sam_delmem,
+                       NET_TRANSPORT_LOCAL,
+                       "Delete a member from a group",
+                       "net sam delmem\n"
+                       "    Delete a member from a group"
+               },
+               {
+                       "listmem",
+                       net_sam_listmem,
+                       NET_TRANSPORT_LOCAL,
+                       "List group members",
+                       "net sam listmem\n"
+                       "    List group members"
+               },
+               {
+                       "list",
+                       net_sam_list,
+                       NET_TRANSPORT_LOCAL,
+                       "List users, groups and local groups",
+                       "net sam list\n"
+                       "    List users, groups and local groups"
+               },
+               {
+                       "show",
+                       net_sam_show,
+                       NET_TRANSPORT_LOCAL,
+                       "Show details of a SAM entry",
+                       "net sam show\n"
+                       "    Show details of a SAM entry"
+               },
+               {
+                       "set",
+                       net_sam_set,
+                       NET_TRANSPORT_LOCAL,
+                       "Set details of a SAM account",
+                       "net sam set\n"
+                       "    Set details of a SAM account"
+               },
+               {
+                       "policy",
+                       net_sam_policy,
+                       NET_TRANSPORT_LOCAL,
+                       "Set account policies",
+                       "net sam policy\n"
+                       "    Set account policies"
+               },
+               {
+                       "rights",
+                       net_sam_rights,
+                       NET_TRANSPORT_LOCAL,
+                       "Manipulate user privileges",
+                       "net sam rights\n"
+                       "    Manipulate user privileges"
+               },
 #ifdef HAVE_LDAP
-               { "provision", net_sam_provision,
-                 "Provision a clean User Database" },
+               {
+                       "provision",
+                       net_sam_provision,
+                       NET_TRANSPORT_LOCAL,
+                       "Provision a clean user database",
+                       "net sam privison\n"
+                       "    Provision a clear user database"
+               },
 #endif
-               { NULL, NULL, NULL }
+               {NULL, NULL, 0, NULL, NULL}
        };
 
        if (getuid() != 0) {
@@ -1692,6 +1994,6 @@ int net_sam(struct net_context *c, int argc, const char **argv)
                          "work\n");
        }
 
-       return net_run_function2(c, argc, argv, "net sam", func);
+       return net_run_function(c, argc, argv, "net sam", func);
 }