samr: for correctness, rename samr_RidTypeArray to samr_RidAttrArray.
[mat/samba.git] / source3 / utils / net_rpc.c
index 549539298f0ed26fd8d9d83dab5f4d1cbef0a826..7c49645cfc665d6869fd579266005619425a16e3 100644 (file)
 #include "includes.h"
 #include "utils/net.h"
 #include "../libcli/auth/libcli_auth.h"
+#include "../librpc/gen_ndr/cli_samr.h"
+#include "rpc_client/cli_samr.h"
+#include "rpc_client/init_samr.h"
+#include "../librpc/gen_ndr/cli_lsa.h"
+#include "rpc_client/cli_lsarpc.h"
+#include "../librpc/gen_ndr/cli_netlogon.h"
+#include "../librpc/gen_ndr/cli_srvsvc.h"
+#include "../librpc/gen_ndr/cli_spoolss.h"
+#include "../librpc/gen_ndr/cli_initshutdown.h"
+#include "../librpc/gen_ndr/cli_winreg.h"
+#include "secrets.h"
+#include "lib/netapi/netapi.h"
+#include "rpc_client/init_lsa.h"
+#include "../libcli/security/dom_sid.h"
 
 static int net_mode_share;
 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
@@ -52,7 +66,7 @@ static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
  **/
 
 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                                  DOM_SID **domain_sid,
+                                  struct dom_sid **domain_sid,
                                   const char **domain_name)
 {
        struct rpc_pipe_client *lsa_pipe = NULL;
@@ -71,7 +85,8 @@ NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                                     SEC_FLAG_MAXIMUM_ALLOWED,
                                     &pol);
        if (!NT_STATUS_IS_OK(result)) {
-               d_fprintf(stderr, _("open_policy failed: %s\n"),
+               d_fprintf(stderr, "open_policy %s: %s\n",
+                         _("failed"),
                          nt_errstr(result));
                return result;
        }
@@ -81,7 +96,8 @@ NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
                                            LSA_POLICY_INFO_ACCOUNT_DOMAIN,
                                            &info);
        if (!NT_STATUS_IS_OK(result)) {
-               d_fprintf(stderr, _("lsaquery failed: %s\n"),
+               d_fprintf(stderr, "lsaquery %s: %s\n",
+                         _("failed"),
                          nt_errstr(result));
                return result;
        }
@@ -119,7 +135,7 @@ int run_rpc_command(struct net_context *c,
        struct rpc_pipe_client *pipe_hnd = NULL;
        TALLOC_CTX *mem_ctx;
        NTSTATUS nt_status;
-       DOM_SID *domain_sid;
+       struct dom_sid *domain_sid;
        const char *domain_name;
        int ret = -1;
 
@@ -159,7 +175,7 @@ int run_rpc_command(struct net_context *c,
                        /* Always try and create an schannel netlogon pipe. */
                        nt_status = cli_rpc_pipe_open_schannel(
                                cli, interface, NCACN_NP,
-                               PIPE_AUTH_LEVEL_PRIVACY, domain_name,
+                               DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
                                &pipe_hnd);
                        if (!NT_STATUS_IS_OK(nt_status)) {
                                DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
@@ -172,7 +188,7 @@ int run_rpc_command(struct net_context *c,
                                        cli, interface,
                                        (conn_flags & NET_FLAGS_TCP) ?
                                        NCACN_IP_TCP : NCACN_NP,
-                                       PIPE_AUTH_LEVEL_PRIVACY,
+                                       DCERPC_AUTH_LEVEL_PRIVACY,
                                        lp_workgroup(), c->opt_user_name,
                                        c->opt_password, &pipe_hnd);
                        } else {
@@ -182,7 +198,8 @@ int run_rpc_command(struct net_context *c,
                        }
                        if (!NT_STATUS_IS_OK(nt_status)) {
                                DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
-                                       get_pipe_name_from_iface(interface),
+                                         get_pipe_name_from_syntax(
+                                                 talloc_tos(), interface),
                                        nt_errstr(nt_status) ));
                                goto fail;
                        }
@@ -231,7 +248,7 @@ fail:
  **/
 
 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -239,8 +256,16 @@ static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
+       NTSTATUS status;
+
+       status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
+                       nt_errstr(status));
+               return status;
+       }
 
-       return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
+       return NT_STATUS_OK;
 }
 
 /**
@@ -256,9 +281,11 @@ static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc changetrustpw\n"
-                          "    Change the machine trust password\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Change the machine trust password"));
                return 0;
        }
 
@@ -289,7 +316,7 @@ int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -301,7 +328,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
        fstring trust_passwd;
        unsigned char orig_trust_passwd_hash[16];
        NTSTATUS result;
-       uint32 sec_channel_type;
+       enum netr_SchannelType sec_channel_type;
 
        result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
                                          &pipe_hnd);
@@ -336,6 +363,7 @@ static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
        E_md4hash(trust_passwd, orig_trust_passwd_hash);
 
        result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
+                                             global_myname(),
                                              orig_trust_passwd_hash,
                                              sec_channel_type);
 
@@ -386,9 +414,11 @@ static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
        int rc = -1;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc oldjoin\n"
-                          "    Join a domain the old way\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Join a domain the old way"));
                return 0;
        }
 
@@ -416,8 +446,9 @@ static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
 int net_rpc_join(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc join -U <username>[%%password] <type>\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc join -U <username>[%%password] <type>\n"
                           "  Join a domain\n"
                           "    username\tName of the admin user"
                           "    password\tPassword of the admin user, will "
@@ -464,7 +495,7 @@ int net_rpc_join(struct net_context *c, int argc, const char **argv)
  **/
 
 NTSTATUS rpc_info_internals(struct net_context *c,
-                       const DOM_SID *domain_sid,
+                       const struct dom_sid *domain_sid,
                        const char *domain_name,
                        struct cli_state *cli,
                        struct rpc_pipe_client *pipe_hnd,
@@ -531,9 +562,11 @@ NTSTATUS rpc_info_internals(struct net_context *c,
 int net_rpc_info(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc info\n"
-                          "  Display information about the domain\n"));
+                          "  %s\n",
+                        _("Usage:"),
+                        _("Display information about the domain"));
                return 0;
        }
 
@@ -559,7 +592,7 @@ int net_rpc_info(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_getsid_internals(struct net_context *c,
-                       const DOM_SID *domain_sid,
+                       const struct dom_sid *domain_sid,
                        const char *domain_name,
                        struct cli_state *cli,
                        struct rpc_pipe_client *pipe_hnd,
@@ -590,15 +623,23 @@ static NTSTATUS rpc_getsid_internals(struct net_context *c,
 
 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
 {
+       int conn_flags = NET_FLAGS_PDC;
+
+       if (!c->opt_user_specified) {
+               conn_flags |= NET_FLAGS_ANONYMOUS;
+       }
+
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc getsid\n"
-                          "    Fetch domain SID into local secrets.tdb\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Fetch domain SID into local secrets.tdb"));
                return 0;
        }
 
        return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
-                              NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
+                              conn_flags,
                               rpc_getsid_internals,
                               argc, argv);
 }
@@ -696,6 +737,57 @@ static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
        return status;
 }
 
+/**
+ * Set a user's primary group
+ *
+ * @param argc  Standard main() style argc.
+ * @param argv  Standard main() style argv. Initial components are already
+ *              stripped.
+ *
+ * @return A shell status integer (0 for success).
+ **/
+
+static int rpc_user_setprimarygroup(struct net_context *c, int argc,
+                                   const char **argv)
+{
+       NET_API_STATUS status;
+       uint8_t *buffer;
+       struct GROUP_INFO_2 *g2;
+       struct USER_INFO_1051 u1051;
+       uint32_t parm_err = 0;
+
+       if (argc != 2 || c->display_usage) {
+               rpc_user_usage(c, argc, argv);
+               return 0;
+       }
+
+       status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
+       if (status) {
+               d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
+                         argv[1],
+                         libnetapi_get_error_string(c->netapi_ctx, status));
+               return status;
+       }
+       g2 = (struct GROUP_INFO_2 *)buffer;
+
+       u1051.usri1051_primary_group_id = g2->grpi2_group_id;
+
+       NetApiBufferFree(buffer);
+
+       status = NetUserSetInfo(c->opt_host, argv[0], 1051,
+                               (uint8_t *)&u1051, &parm_err);
+       if (status) {
+               d_fprintf(stderr,
+                         _("Failed to set user's primary group %s to %s - "
+                           "%s\n"), argv[0], argv[1],
+                         libnetapi_get_error_string(c->netapi_ctx, status));
+       } else {
+               d_printf(_("Set primary group of user %s to %s\n"), argv[0],
+                        argv[1]);
+       }
+       return status;
+}
+
 /**
  * Delete a user from a remote RPC server.
  *
@@ -760,8 +852,11 @@ static int rpc_user_password(struct net_context *c, int argc, const char **argv)
                if (ret == -1) {
                        return -1;
                }
-               u1003.usri1003_password = getpass(prompt);
+               u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
                SAFE_FREE(prompt);
+               if (u1003.usri1003_password == NULL) {
+                       return -1;
+               }
        }
 
        status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
@@ -918,7 +1013,7 @@ int net_rpc_user(struct net_context *c, int argc, const char **argv)
                        NET_TRANSPORT_RPC,
                        N_("List domain groups of user"),
                        N_("net rpc user info\n"
-                          "    Lis domain groups of user")
+                          "    List domain groups of user")
                },
                {
                        "delete",
@@ -944,6 +1039,14 @@ int net_rpc_user(struct net_context *c, int argc, const char **argv)
                        N_("net rpc user rename\n"
                           "    Rename specified user")
                },
+               {
+                       "setprimarygroup",
+                       rpc_user_setprimarygroup,
+                       NET_TRANSPORT_RPC,
+                       "Set a user's primary group",
+                       "net rpc user setprimarygroup\n"
+                       "    Set a user's primary group"
+               },
                {NULL, NULL, 0, NULL, NULL}
        };
 
@@ -959,9 +1062,11 @@ int net_rpc_user(struct net_context *c, int argc, const char **argv)
 
        if (argc == 0) {
                if (c->display_usage) {
-                       d_printf(_("Usage:\n"));
-                       d_printf(_("net rpc user\n"
-                                  "    List all users\n"));
+                       d_printf(  "%s\n"
+                                  "net rpc user\n"
+                                  "    %s\n",
+                                _("Usage:"),
+                                _("List all users"));
                        net_display_usage_from_functable(func);
                        return 0;
                }
@@ -1005,12 +1110,13 @@ static NTSTATUS rpc_sh_handle_user(struct net_context *c,
 {
        struct policy_handle connect_pol, domain_pol, user_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       DOM_SID sid;
+       struct dom_sid sid;
        uint32 rid;
        enum lsa_SidType type;
 
        if (argc == 0) {
-               d_fprintf(stderr, _("usage: %s <username>\n"), ctx->whoami);
+               d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
+                         ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -1091,7 +1197,8 @@ static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
        union samr_UserInfo *info = NULL;
 
        if (argc != 0) {
-               d_fprintf(stderr, _("usage: %s show <username>\n"),ctx->whoami);
+               d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
+                         ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -1144,8 +1251,8 @@ static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
        union samr_UserInfo *info = NULL;
 
        if (argc > 1) {
-               d_fprintf(stderr, _("usage: %s <username> [new value|NULL]\n"),
-                         ctx->whoami);
+               d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
+                         _("Usage:"), ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -1235,7 +1342,7 @@ static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
             !strequal(argv[0], "no"))) {
                /* TRANSATORS: The yes|no here are program keywords. Please do
                   not translate. */
-               d_fprintf(stderr, _("usage: %s <username> [yes|no]\n"),
+               d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
                          ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -1392,7 +1499,7 @@ static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -1404,7 +1511,7 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
        bool group_is_primary = false;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32_t group_rid;
-       struct samr_RidTypeArray *rids = NULL;
+       struct samr_RidAttrArray *rids = NULL;
        /* char **names; */
        int i;
        /* struct samr_RidWithAttribute *user_gids; */
@@ -1548,7 +1655,7 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
                                        d_printf(_("ok\n"));
                        } else {
                                if (c->opt_verbose)
-                                       d_printf(_("failed\n"));
+                                       d_printf("%s\n", _("failed"));
                                goto done;
                        }
                }
@@ -1678,10 +1785,10 @@ static int rpc_group_add(struct net_context *c, int argc, const char **argv)
 static NTSTATUS get_sid_from_name(struct cli_state *cli,
                                TALLOC_CTX *mem_ctx,
                                const char *name,
-                               DOM_SID *sid,
+                               struct dom_sid *sid,
                                enum lsa_SidType *type)
 {
-       DOM_SID *sids = NULL;
+       struct dom_sid *sids = NULL;
        enum lsa_SidType *types = NULL;
        struct rpc_pipe_client *pipe_hnd = NULL;
        struct policy_handle lsa_pol;
@@ -1719,7 +1826,7 @@ static NTSTATUS get_sid_from_name(struct cli_state *cli,
 
                /* Try as S-1-5-whatever */
 
-               DOM_SID tmp_sid;
+               struct dom_sid tmp_sid;
 
                if (string_to_sid(&tmp_sid, name)) {
                        sid_copy(sid, &tmp_sid);
@@ -1733,7 +1840,7 @@ static NTSTATUS get_sid_from_name(struct cli_state *cli,
 
 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *group_sid,
+                               const struct dom_sid *group_sid,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
@@ -1744,7 +1851,7 @@ static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
        struct samr_Ids rids, rid_types;
        struct lsa_String lsa_acct_name;
 
-       DOM_SID sid;
+       struct dom_sid sid;
 
        sid_copy(&sid, group_sid);
 
@@ -1808,7 +1915,7 @@ static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
 
 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *alias_sid,
+                               const struct dom_sid *alias_sid,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
@@ -1816,10 +1923,10 @@ static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
        uint32 alias_rid;
        struct policy_handle alias_pol;
 
-       DOM_SID member_sid;
+       struct dom_sid member_sid;
        enum lsa_SidType member_type;
 
-       DOM_SID sid;
+       struct dom_sid sid;
 
        sid_copy(&sid, alias_sid);
 
@@ -1879,7 +1986,7 @@ static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
 }
 
 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -1887,12 +1994,13 @@ static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       DOM_SID group_sid;
+       struct dom_sid group_sid;
        enum lsa_SidType group_type;
 
        if (argc != 2 || c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc group addmem <group> <member>\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc group addmem <group> <member>\n"
                           "  Add a member to a group\n"
                           "    group\tGroup to add member to\n"
                           "    member\tMember to add to group\n"));
@@ -1944,7 +2052,7 @@ static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
 static NTSTATUS rpc_del_groupmem(struct net_context *c,
                                struct rpc_pipe_client *pipe_hnd,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *group_sid,
+                               const struct dom_sid *group_sid,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
@@ -1955,7 +2063,7 @@ static NTSTATUS rpc_del_groupmem(struct net_context *c,
        struct samr_Ids rids, rid_types;
        struct lsa_String lsa_acct_name;
 
-       DOM_SID sid;
+       struct dom_sid sid;
 
        sid_copy(&sid, group_sid);
 
@@ -2013,7 +2121,7 @@ static NTSTATUS rpc_del_groupmem(struct net_context *c,
 
 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *alias_sid,
+                               const struct dom_sid *alias_sid,
                                const char *member)
 {
        struct policy_handle connect_pol, domain_pol;
@@ -2021,10 +2129,10 @@ static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
        uint32 alias_rid;
        struct policy_handle alias_pol;
 
-       DOM_SID member_sid;
+       struct dom_sid member_sid;
        enum lsa_SidType member_type;
 
-       DOM_SID sid;
+       struct dom_sid sid;
 
        sid_copy(&sid, alias_sid);
 
@@ -2081,7 +2189,7 @@ static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
 }
 
 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -2089,12 +2197,13 @@ static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
                                        int argc,
                                        const char **argv)
 {
-       DOM_SID group_sid;
+       struct dom_sid group_sid;
        enum lsa_SidType group_type;
 
        if (argc != 2 || c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc group delmem <group> <member>\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc group delmem <group> <member>\n"
                           "  Delete a member from a group\n"
                           "    group\tGroup to delete member from\n"
                           "    member\tMember to delete from group\n"));
@@ -2160,7 +2269,7 @@ static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_group_list_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -2177,8 +2286,9 @@ static NTSTATUS rpc_group_list_internals(struct net_context *c,
        bool builtin = false;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc group list [global] [local] [builtin]\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc group list [global] [local] [builtin]\n"
                           "  List groups on RPC server\n"
                           "    global\tList global groups\n"
                           "    local\tList local groups\n"
@@ -2394,7 +2504,7 @@ static NTSTATUS rpc_list_group_members(struct net_context *c,
                                        struct rpc_pipe_client *pipe_hnd,
                                        TALLOC_CTX *mem_ctx,
                                        const char *domain_name,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        struct policy_handle *domain_pol,
                                        uint32 rid)
 {
@@ -2402,7 +2512,7 @@ static NTSTATUS rpc_list_group_members(struct net_context *c,
        struct policy_handle group_pol;
        uint32 num_members, *group_rids;
        int i;
-       struct samr_RidTypeArray *rids = NULL;
+       struct samr_RidAttrArray *rids = NULL;
        struct lsa_Strings names;
        struct samr_Ids types;
 
@@ -2477,7 +2587,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
        struct rpc_pipe_client *lsa_pipe;
        struct policy_handle alias_pol, lsa_pol;
        uint32 num_members;
-       DOM_SID *alias_sids;
+       struct dom_sid *alias_sids;
        char **domains;
        char **names;
        enum lsa_SidType *types;
@@ -2526,7 +2636,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
                return result;
        }
 
-       alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
+       alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
        if (!alias_sids) {
                d_fprintf(stderr, _("Out of memory\n"));
                TALLOC_FREE(lsa_pipe);
@@ -2569,7 +2679,7 @@ static NTSTATUS rpc_list_alias_members(struct net_context *c,
 }
 
 static NTSTATUS rpc_group_members_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -2616,7 +2726,7 @@ static NTSTATUS rpc_group_members_internals(struct net_context *c,
 
                /* Ok, did not find it in the global sam, try with builtin */
 
-               DOM_SID sid_Builtin;
+               struct dom_sid sid_Builtin;
 
                rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
 
@@ -2686,7 +2796,8 @@ static int rpc_group_rename_internals(struct net_context *c, int argc, const cha
        uint32_t parm_err;
 
        if (argc != 2) {
-               d_printf(_("Usage: 'net rpc group rename group newname'\n"));
+               d_printf(_("Usage:\n"));
+               d_printf("net rpc group rename group newname\n");
                return -1;
        }
 
@@ -3024,9 +3135,11 @@ static int rpc_share_list(struct net_context *c, int argc, const char **argv)
        uint32_t i, level = 1;
 
        if (c->display_usage) {
-               d_printf(_("Usage\n"
+               d_printf(  "%s\n"
                           "net rpc share list\n"
-                          "    List shares on remote server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List shares on remote server"));
                return 0;
        }
 
@@ -3057,13 +3170,19 @@ static int rpc_share_list(struct net_context *c, int argc, const char **argv)
 
 static bool check_share_availability(struct cli_state *cli, const char *netname)
 {
-       if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
+       NTSTATUS status;
+
+       status = cli_tcon_andx(cli, netname, "A:", "", 0);
+       if (!NT_STATUS_IS_OK(status)) {
                d_printf(_("skipping   [%s]: not a file share.\n"), netname);
                return false;
        }
 
-       if (!cli_tdis(cli))
+       status = cli_tdis(cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
                return false;
+       }
 
        return true;
 }
@@ -3109,7 +3228,7 @@ static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
  **/
 
 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -3202,9 +3321,11 @@ static int rpc_share_migrate_shares(struct net_context *c, int argc,
                                    const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc share migrate shares\n"
-                          "    Migrate shares to local server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate shares to local server"));
                return 0;
        }
 
@@ -3226,7 +3347,7 @@ static int rpc_share_migrate_shares(struct net_context *c, int argc,
  * @param state        arg-pointer
  *
  **/
-static void copy_fn(const char *mnt, file_info *f,
+static void copy_fn(const char *mnt, struct file_info *f,
                    const char *mask, void *state)
 {
        static NTSTATUS nt_status;
@@ -3335,6 +3456,7 @@ static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
 {
        struct cli_state *targetcli;
        char *targetpath = NULL;
+       NTSTATUS status;
 
        DEBUG(3,("calling cli_list with mask: %s\n", mask));
 
@@ -3346,9 +3468,11 @@ static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
                return false;
        }
 
-       if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
+       status = cli_list(targetcli, targetpath, cp_clistate->attribute,
+                         copy_fn, cp_clistate);
+       if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("listing %s failed with error: %s\n"),
-                       mask, cli_errstr(targetcli));
+                         mask, nt_errstr(status));
                return false;
        }
 
@@ -3412,7 +3536,7 @@ bool copy_top_level_perms(struct net_context *c,
  **/
 
 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -3535,9 +3659,11 @@ done:
 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net share migrate files\n"
-                          "    Migrate files to local server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate files to local server"));
                return 0;
        }
 
@@ -3568,7 +3694,7 @@ static int rpc_share_migrate_files(struct net_context *c, int argc, const char *
  **/
 
 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -3660,9 +3786,11 @@ static int rpc_share_migrate_security(struct net_context *c, int argc,
                                      const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc share migrate security\n"
-                          "    Migrate share-acls to local server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate share-acls to local server"));
                return 0;
        }
 
@@ -3693,9 +3821,11 @@ static int rpc_share_migrate_all(struct net_context *c, int argc,
        int ret;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc share migrate all\n"
-                          "    Migrates shares including all share settings\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrates shares including all share settings"));
                return 0;
        }
 
@@ -3774,9 +3904,9 @@ static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
 }
 
 struct full_alias {
-       DOM_SID sid;
+       struct dom_sid sid;
        uint32 num_members;
-       DOM_SID *members;
+       struct dom_sid *members;
 };
 
 static int num_server_aliases;
@@ -3802,7 +3932,7 @@ static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
                                        TALLOC_CTX *mem_ctx,
                                        struct policy_handle *connect_pol,
-                                       const DOM_SID *domain_sid)
+                                       const struct dom_sid *domain_sid)
 {
        uint32 start_idx, max_entries, num_entries, i;
        struct samr_SamArray *groups = NULL;
@@ -3859,15 +3989,15 @@ static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
                        alias.members = NULL;
 
                        if (alias.num_members > 0) {
-                               alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
+                               alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
 
                                for (j = 0; j < alias.num_members; j++)
                                        sid_copy(&alias.members[j],
                                                 sid_array.sids[j].sid);
                        }
 
-                       sid_copy(&alias.sid, domain_sid);
-                       sid_append_rid(&alias.sid, groups->entries[i].idx);
+                       sid_compose(&alias.sid, domain_sid,
+                                   groups->entries[i].idx);
 
                        push_alias(mem_ctx, &alias);
                }
@@ -3886,7 +4016,7 @@ static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
  */
 
 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
-                               const DOM_SID *domain_sid,
+                               const struct dom_sid *domain_sid,
                                const char *domain_name,
                                struct cli_state *cli,
                                struct rpc_pipe_client *pipe_hnd,
@@ -3952,7 +4082,7 @@ static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
  */
 
 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -3985,45 +4115,45 @@ static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
        return result;
 }
 
-static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
+static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
 {
        token->num_sids = 4;
 
-       if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
-               d_fprintf(stderr, _("malloc failed\n"));
+       if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
+               d_fprintf(stderr, "malloc %s\n",_("failed"));
                token->num_sids = 0;
                return;
        }
 
-       token->user_sids[0] = *user_sid;
-       sid_copy(&token->user_sids[1], &global_sid_World);
-       sid_copy(&token->user_sids[2], &global_sid_Network);
-       sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
+       token->sids[0] = *user_sid;
+       sid_copy(&token->sids[1], &global_sid_World);
+       sid_copy(&token->sids[2], &global_sid_Network);
+       sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
 }
 
-static void free_user_token(NT_USER_TOKEN *token)
+static void free_user_token(struct security_token *token)
 {
-       SAFE_FREE(token->user_sids);
+       SAFE_FREE(token->sids);
 }
 
-static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
+static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
 {
        if (is_sid_in_token(token, sid))
                return;
 
-       token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
-       if (!token->user_sids) {
+       token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
+       if (!token->sids) {
                return;
        }
 
-       sid_copy(&token->user_sids[token->num_sids], sid);
+       sid_copy(&token->sids[token->num_sids], sid);
 
        token->num_sids += 1;
 }
 
 struct user_token {
        fstring name;
-       NT_USER_TOKEN token;
+       struct security_token token;
 };
 
 static void dump_user_token(struct user_token *token)
@@ -4033,23 +4163,23 @@ static void dump_user_token(struct user_token *token)
        d_printf("%s\n", token->name);
 
        for (i=0; i<token->token.num_sids; i++) {
-               d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
+               d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
        }
 }
 
-static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
+static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
 {
        int i;
 
        for (i=0; i<alias->num_members; i++) {
-               if (sid_compare(sid, &alias->members[i]) == 0)
+               if (dom_sid_compare(sid, &alias->members[i]) == 0)
                        return true;
        }
 
        return false;
 }
 
-static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
+static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
 {
        int i;
 
@@ -4066,24 +4196,24 @@ static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
  * add them to the token.
  */
 
-static void collect_alias_memberships(NT_USER_TOKEN *token)
+static void collect_alias_memberships(struct security_token *token)
 {
        int num_global_sids = token->num_sids;
        int i;
 
        for (i=0; i<num_global_sids; i++) {
-               collect_sid_memberships(token, token->user_sids[i]);
+               collect_sid_memberships(token, token->sids[i]);
        }
 }
 
-static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
+static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
 {
        wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
        enum wbcSidType type;
        fstring full_name;
        struct wbcDomainSid wsid;
        char *sid_str = NULL;
-       DOM_SID user_sid;
+       struct dom_sid user_sid;
        uint32_t num_groups;
        gid_t *groups = NULL;
        uint32_t i;
@@ -4133,7 +4263,7 @@ static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *t
 
        for (i = 0; i < num_groups; i++) {
                gid_t gid = groups[i];
-               DOM_SID sid;
+               struct dom_sid sid;
 
                wbc_status = wbcGidToSid(gid, &wsid);
                if (!WBC_ERROR_IS_OK(wbc_status)) {
@@ -4245,13 +4375,14 @@ static bool get_user_tokens_from_file(FILE *f,
                        return true;
                }
 
-               if (line[strlen(line)-1] == '\n')
+               if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
                        line[strlen(line)-1] = '\0';
+               }
 
                if (line[0] == ' ') {
                        /* We have a SID */
 
-                       DOM_SID sid;
+                       struct dom_sid sid;
                        if(!string_to_sid(&sid, &line[1])) {
                                DEBUG(1,("get_user_tokens_from_file: Could "
                                        "not convert sid %s \n",&line[1]));
@@ -4280,7 +4411,7 @@ static bool get_user_tokens_from_file(FILE *f,
 
                fstrcpy(token->name, line);
                token->token.num_sids = 0;
-               token->token.user_sids = NULL;
+               token->token.sids = NULL;
                continue;
        }
        
@@ -4299,8 +4430,8 @@ static void show_userlist(struct rpc_pipe_client *pipe_hnd,
                        struct user_token *tokens)
 {
        uint16_t fnum;
-       SEC_DESC *share_sd = NULL;
-       SEC_DESC *root_sd = NULL;
+       struct security_descriptor *share_sd = NULL;
+       struct security_descriptor *root_sd = NULL;
        struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
        int i;
        union srvsvc_NetShareInfo info;
@@ -4415,7 +4546,7 @@ static void collect_share(const char *name, uint32 m,
  **/
 
 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -4495,9 +4626,11 @@ static int rpc_share_allowedusers(struct net_context *c, int argc,
        int result;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc share allowedusers\n"
-                           "    List allowed users\n"));
+                           "    %s\n",
+                         _("Usage:"),
+                         _("List allowed users"));
                return 0;
        }
 
@@ -4621,8 +4754,9 @@ int net_rpc_share(struct net_context *c, int argc, const char **argv)
 
        if (argc == 0) {
                if (c->display_usage) {
-                       d_printf(_("Usage:\n"
-                                  "net rpc share\n"
+                       d_printf("%s\n%s",
+                                _("Usage:"),
+                                _("net rpc share\n"
                                   "    List shares\n"
                                   "    Alias for net rpc share list\n"));
                        net_display_usage_from_functable(func);
@@ -4656,7 +4790,7 @@ static NTSTATUS rpc_sh_share_add(struct net_context *c,
        struct SHARE_INFO_2 i2;
 
        if ((argc < 2) || (argc > 3)) {
-               d_fprintf(stderr, _("usage: %s <share> <path> [comment]\n"),
+               d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
                          ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -4685,7 +4819,7 @@ static NTSTATUS rpc_sh_share_delete(struct net_context *c,
                                    int argc, const char **argv)
 {
        if (argc != 1) {
-               d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
+               d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -4703,7 +4837,7 @@ static NTSTATUS rpc_sh_share_info(struct net_context *c,
        NTSTATUS status;
 
        if (argc != 1) {
-               d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
+               d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -4927,7 +5061,7 @@ int net_rpc_file(struct net_context *c, int argc, const char **argv)
  **/
 
 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -4966,7 +5100,7 @@ static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
  **/
 
 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -5003,9 +5137,11 @@ static int rpc_shutdown_abort(struct net_context *c, int argc,
        int rc = -1;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc abortshutdown\n"
-                          "    Abort a scheduled shutdown\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Abort a scheduled shutdown"));
                return 0;
        }
 
@@ -5040,7 +5176,7 @@ static int rpc_shutdown_abort(struct net_context *c, int argc,
  **/
 
 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
-                                    const DOM_SID *domain_sid,
+                                    const struct dom_sid *domain_sid,
                                     const char *domain_name,
                                     struct cli_state *cli,
                                     struct rpc_pipe_client *pipe_hnd,
@@ -5094,7 +5230,7 @@ NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
  **/
 
 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
-                                   const DOM_SID *domain_sid,
+                                   const struct dom_sid *domain_sid,
                                    const char *domain_name,
                                    struct cli_state *cli,
                                    struct rpc_pipe_client *pipe_hnd,
@@ -5150,9 +5286,11 @@ static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
        int rc =  -1;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc shutdown\n"
-                          "    Shut down a remote RPC server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Shut down a remote RPC server"));
                return 0;
        }
 
@@ -5189,7 +5327,7 @@ static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
  */
 
 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -5209,12 +5347,14 @@ static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
        unsigned int orig_timeout;
 
        if (argc != 2) {
-               d_printf(_("Usage: net rpc trustdom add <domain_name> "
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _(" net rpc trustdom add <domain_name> "
                           "<trust password>\n"));
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /* 
+       /*
         * Make valid trusting domain account (ie. uppercased and with '$' appended)
         */
 
@@ -5322,8 +5462,9 @@ static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
                return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
                                       rpc_trustdom_add_internals, argc, argv);
        } else {
-               d_printf(_("Usage:\n"
-                          "net rpc trustdom add <domain_name> <trust "
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc trustdom add <domain_name> <trust "
                           "password>\n"));
                return -1;
        }
@@ -5347,7 +5488,7 @@ static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
  */
 
 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -5358,12 +5499,14 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        struct policy_handle connect_pol, domain_pol, user_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        char *acct_name;
-       DOM_SID trust_acct_sid;
+       struct dom_sid trust_acct_sid;
        struct samr_Ids user_rids, name_types;
        struct lsa_String lsa_acct_name;
 
        if (argc != 1) {
-               d_printf(_("Usage: net rpc trustdom del <domain_name>\n"));
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _(" net rpc trustdom del <domain_name>\n"));
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -5426,8 +5569,7 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
        }
 
        /* append the rid to the domain sid */
-       sid_copy(&trust_acct_sid, domain_sid);
-       if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
+       if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
                goto done;
        }
 
@@ -5480,8 +5622,9 @@ static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
                return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
                                       rpc_trustdom_del_internals, argc, argv);
        } else {
-               d_printf(_("Usage:\n"
-                          "net rpc trustdom del <domain>\n"));
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc trustdom del <domain>\n"));
                return -1;
        }
 }
@@ -5551,7 +5694,7 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
        struct policy_handle connect_hnd;
        TALLOC_CTX *mem_ctx;
        NTSTATUS nt_status;
-       DOM_SID *domain_sid;
+       struct dom_sid *domain_sid;
 
        char* domain_name;
        char* acct_name;
@@ -5563,8 +5706,9 @@ static int rpc_trustdom_establish(struct net_context *c, int argc,
         */
 
        if (argc != 1 || c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc trustdom establish <domain_name>\n"));
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc trustdom establish <domain_name>\n"));
                return -1;
        }
 
@@ -5727,8 +5871,9 @@ static int rpc_trustdom_revoke(struct net_context *c, int argc,
        int rc = -1;
 
        if (argc < 1 || c->display_usage) {
-               d_printf(_("Usage:\n"
-                          "net rpc trustdom revoke <domain_name>\n"
+               d_printf("%s\n%s",
+                        _("Usage:"),
+                        _("net rpc trustdom revoke <domain_name>\n"
                           "  Revoke trust relationship\n"
                           "    domain_name\tName of domain to revoke trust\n"));
                return -1;
@@ -5752,7 +5897,7 @@ done:
 }
 
 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -5761,31 +5906,27 @@ static NTSTATUS rpc_query_domain_sid(struct net_context *c,
                                        const char **argv)
 {
        fstring str_sid;
-       sid_to_fstring(str_sid, domain_sid);
+       if (!sid_to_fstring(str_sid, domain_sid)) {
+               return NT_STATUS_UNSUCCESSFUL;
+       }
        d_printf("%s\n", str_sid);
        return NT_STATUS_OK;
 }
 
-static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
+static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
 {
-       fstring ascii_sid, padding;
-       int pad_len, col_len = 20;
+       fstring ascii_sid;
 
        /* convert sid into ascii string */
        sid_to_fstring(ascii_sid, dom_sid);
 
-       /* calculate padding space for d_printf to look nicer */
-       pad_len = col_len - strlen(trusted_dom_name);
-       padding[pad_len] = 0;
-       do padding[--pad_len] = ' '; while (pad_len);
-
-       d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
+       d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
 }
 
 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
                                      TALLOC_CTX *mem_ctx,
                                      struct policy_handle *pol,
-                                     DOM_SID dom_sid,
+                                     struct dom_sid dom_sid,
                                      const char *trusted_dom_name)
 {
        NTSTATUS nt_status;
@@ -5851,7 +5992,7 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        struct rpc_pipe_client *pipe_hnd = NULL;
        NTSTATUS nt_status;
        const char *domain_name = NULL;
-       DOM_SID *queried_dom_sid;
+       struct dom_sid *queried_dom_sid;
        struct policy_handle connect_hnd;
        union lsa_PolicyInformation *info = NULL;
 
@@ -5862,9 +6003,11 @@ static int rpc_trustdom_vampire(struct net_context *c, int argc,
        fstring pdc_name;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc trustdom vampire\n"
-                          "  Vampire trust relationship from remote server\n"));
+                          "  %s\n",
+                        _("Usage:"),
+                        _("Vampire trust relationship from remote server"));
                return 0;
        }
 
@@ -6002,26 +6145,28 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
        struct rpc_pipe_client *pipe_hnd = NULL;
        NTSTATUS nt_status;
        const char *domain_name = NULL;
-       DOM_SID *queried_dom_sid;
-       fstring padding;
+       struct dom_sid *queried_dom_sid;
        int ascii_dom_name_len;
        struct policy_handle connect_hnd;
        union lsa_PolicyInformation *info = NULL;
 
        /* trusted domains listing variables */
        unsigned int num_domains, enum_ctx = 0;
-       int i, pad_len, col_len = 20;
+       int i;
        struct lsa_DomainList dom_list;
        fstring pdc_name;
+       bool found_domain;
 
        /* trusting domains listing variables */
        struct policy_handle domain_hnd;
        struct samr_SamArray *trusts = NULL;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc trustdom list\n"
-                          "    List trust relationships\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List incoming and outgoing trust relationships"));
                return 0;
        }
 
@@ -6094,9 +6239,11 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
         * Keep calling LsaEnumTrustdom over opened pipe until
         * the end of enumeration is reached
         */
-        
+
        d_printf(_("Trusted domains list:\n\n"));
 
+       found_domain = false;
+
        do {
                nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
                                                    &connect_hnd,
@@ -6114,16 +6261,20 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                for (i = 0; i < dom_list.count; i++) {
                        print_trusted_domain(dom_list.domains[i].sid,
                                             dom_list.domains[i].name.string);
+                       found_domain = true;
                };
 
-               /*
-                * in case of no trusted domains say something rather
-                * than just display blank line
-                */
-               if (!dom_list.count) d_printf(_("none\n"));
 
        } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
 
+       /*
+        * in case of no trusted domains say something rather
+        * than just display blank line
+        */
+       if (!found_domain) {
+               d_printf(_("none\n"));
+       }
+
        /* close this connection before doing next one */
        nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
        if (NT_STATUS_IS_ERR(nt_status)) {
@@ -6186,6 +6337,8 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
         * perform actual enumeration
         */
 
+       found_domain = false;
+
        enum_ctx = 0;   /* reset enumeration context from last enumeration */
        do {
 
@@ -6208,6 +6361,8 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
 
                        char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
 
+                       found_domain = true;
+
                        /*
                         * get each single domain's sid (do we _really_ need this ?):
                         *  1) connect to domain's pdc
@@ -6219,17 +6374,12 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                        if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
                                str[ascii_dom_name_len - 1] = '\0';
 
-                       /* calculate padding space for d_printf to look nicer */
-                       pad_len = col_len - strlen(str);
-                       padding[pad_len] = 0;
-                       do padding[--pad_len] = ' '; while (pad_len);
-
                        /* set opt_* variables to remote domain */
                        strupper_m(str);
                        c->opt_workgroup = talloc_strdup(mem_ctx, str);
                        c->opt_target_workgroup = c->opt_workgroup;
 
-                       d_printf("%s%s", str, padding);
+                       d_printf("%-20s", str);
 
                        /* connect to remote domain controller */
                        nt_status = net_make_ipc_connection(c,
@@ -6242,8 +6392,7 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                                            &ndr_table_lsarpc.syntax_id, 0,
                                            rpc_query_domain_sid, argc,
                                            argv))
-                                       d_fprintf(stderr,
-                                             _("couldn't get domain's sid\n"));
+                                       d_printf(_("strange - couldn't get domain's sid\n"));
 
                                cli_shutdown(remote_cli);
 
@@ -6251,13 +6400,16 @@ static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
                                d_fprintf(stderr, _("domain controller is not "
                                          "responding: %s\n"),
                                          nt_errstr(nt_status));
-                       };
-               };
-
-               if (!num_domains) d_printf("none\n");
+                               d_printf(_("couldn't get domain's sid\n"));
+                       }
+               }
 
        } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
 
+       if (!found_domain) {
+               d_printf("none\n");
+       }
+
        /* close opened samr and domain policy handles */
        nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -6292,41 +6444,41 @@ static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
                        "add",
                        rpc_trustdom_add,
                        NET_TRANSPORT_RPC,
-                       N_("Add trusted domain's account"),
+                       N_("Add trusting domain's account"),
                        N_("net rpc trustdom add\n"
-                          "    Add trusted domain's account")
+                          "    Add trusting domain's account")
                },
                {
                        "del",
                        rpc_trustdom_del,
                        NET_TRANSPORT_RPC,
-                       N_("Remove trusted domain's account"),
+                       N_("Remove trusting domain's account"),
                        N_("net rpc trustdom del\n"
-                          "    Remove trusted domain's account")
+                          "    Remove trusting domain's account")
                },
                {
                        "establish",
                        rpc_trustdom_establish,
                        NET_TRANSPORT_RPC,
-                       N_("Establish trust relationship"),
+                       N_("Establish outgoing trust relationship"),
                        N_("net rpc trustdom establish\n"
-                          "    Establish trust relationship")
+                          "    Establish outgoing trust relationship")
                },
                {
                        "revoke",
                        rpc_trustdom_revoke,
                        NET_TRANSPORT_RPC,
-                       N_("Revoke trust relationship"),
+                       N_("Revoke outgoing trust relationship"),
                        N_("net rpc trustdom revoke\n"
-                          "    Revoke trust relationship")
+                          "    Revoke outgoing trust relationship")
                },
                {
                        "list",
                        rpc_trustdom_list,
                        NET_TRANSPORT_RPC,
-                       N_("List domain trusts"),
+                       N_("List in- and outgoing domain trusts"),
                        N_("net rpc trustdom list\n"
-                          "    List domain trusts")
+                          "    List in- and outgoing domain trusts")
                },
                {
                        "vampire",
@@ -6385,9 +6537,11 @@ bool net_rpc_check(struct net_context *c, unsigned flags)
 /* dump sam database via samsync rpc calls */
 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc samdump\n"
-                          "    Dump remote SAM database\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Dump remote SAM database"));
                return 0;
        }
 
@@ -6432,9 +6586,11 @@ static int rpc_vampire(struct net_context *c, int argc, const char **argv)
 
        if (argc == 0) {
                if (c->display_usage) {
-                       d_printf(_("Usage:\n"
+                       d_printf(  "%s\n"
                                   "net rpc vampire\n"
-                                  "    Vampire remote SAM database\n"));
+                                  "    %s\n",
+                                _("Usage:"),
+                                _("Vampire remote SAM database"));
                        return 0;
                }
 
@@ -6468,9 +6624,11 @@ static int rpc_printer_migrate_all(struct net_context *c, int argc,
        int ret;
 
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate all\n"
-                          "    Migrate everything from a print server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate everything from a print server"));
                return 0;
        }
 
@@ -6522,9 +6680,11 @@ static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
                                       const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate drivers\n"
-                          "     Migrate print-drivers from a print-server\n"));
+                          "     %s\n",
+                        _("Usage:"),
+                        _("Migrate print-drivers from a print-server"));
                return 0;
        }
 
@@ -6552,9 +6712,11 @@ static int rpc_printer_migrate_forms(struct net_context *c, int argc,
                                     const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate forms\n"
-                          "    Migrate print-forms from a print-server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate print-forms from a print-server"));
                return 0;
        }
 
@@ -6582,9 +6744,11 @@ static int rpc_printer_migrate_printers(struct net_context *c, int argc,
                                        const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate printers\n"
-                          "    Migrate printers from a print-server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate printers from a print-server"));
                return 0;
        }
 
@@ -6612,9 +6776,11 @@ static int rpc_printer_migrate_security(struct net_context *c, int argc,
                                        const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate security\n"
-                          "    Migrate printer-ACLs from a print-server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Migrate printer-ACLs from a print-server"));
                return 0;
        }
 
@@ -6642,10 +6808,12 @@ static int rpc_printer_migrate_settings(struct net_context *c, int argc,
                                        const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer migrate settings\n"
-                           "    Migrate printer-settings from a "
-                           "print-server\n"));
+                           "    %s\n",
+                         _("Usage:"),
+                         _("Migrate printer-settings from a "
+                           "print-server"));
                return 0;
        }
 
@@ -6744,9 +6912,11 @@ int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer list\n"
-                          "    List printers on a remote RPC server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List printers on a remote RPC server"));
                return 0;
        }
 
@@ -6769,9 +6939,11 @@ static int rpc_printer_driver_list(struct net_context *c, int argc,
                                   const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer driver\n"
-                          "    List printer-drivers on a remote RPC server\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List printer-drivers on a remote RPC server"));
                return 0;
        }
 
@@ -6794,9 +6966,11 @@ static int rpc_printer_publish_publish(struct net_context *c, int argc,
                                       const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer publish publish\n"
-                          "     Publish printer in ADS via MSRPC\n"));
+                          "     %s\n",
+                        _("Usage:"),
+                        _("Publish printer in ADS via MSRPC"));
                return 0;
        }
 
@@ -6818,9 +6992,11 @@ static int rpc_printer_publish_publish(struct net_context *c, int argc,
 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer publish update\n"
-                          "    Update printer in ADS via MSRPC\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("Update printer in ADS via MSRPC"));
                return 0;
        }
 
@@ -6843,9 +7019,11 @@ static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
                                         const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer publish unpublish\n"
-                          "    UnPublish printer in ADS via MSRPC\n"));
+                          "    %s\n",
+                        _("Usage:\n"),
+                        _("UnPublish printer in ADS via MSRPC"));
                return 0;
        }
 
@@ -6868,9 +7046,11 @@ static int rpc_printer_publish_list(struct net_context *c, int argc,
                                    const char **argv)
 {
        if (c->display_usage) {
-               d_printf(_("Usage:\n"
+               d_printf(  "%s\n"
                           "net rpc printer publish list\n"
-                          "    List published printers via MSRPC\n"));
+                          "    %s\n",
+                        _("Usage:"),
+                        _("List published printers via MSRPC"));
                return 0;
        }
 
@@ -7254,6 +7434,9 @@ int net_rpc(struct net_context *c, int argc, const char **argv)
        if (c->opt_kerberos) {
                libnetapi_set_use_kerberos(c->netapi_ctx);
        }
+       if (c->opt_ccache) {
+               libnetapi_set_use_ccache(c->netapi_ctx);
+       }
 
        return net_run_function(c, argc, argv, "net rpc", func);
 }