Some old stuff hanging around since the CIFS conference. Big cleanup of
authorTim Potter <tpot@samba.org>
Fri, 12 Oct 2001 05:56:23 +0000 (05:56 +0000)
committerTim Potter <tpot@samba.org>
Fri, 12 Oct 2001 05:56:23 +0000 (05:56 +0000)
rpcclient code.  Refactored cmd_* functions to move common mem_ctx and pipe
opening stuff up one level.  Moved rpcclient.h into rpcclient directory and
out of includes/smb.h
(This used to be commit a40facba9651f9fb1dcc9e143f92ca298a324312)

source3/include/smb.h
source3/rpcclient/cmd_dfs.c
source3/rpcclient/cmd_lsarpc.c
source3/rpcclient/cmd_netlogon.c
source3/rpcclient/cmd_reg.c
source3/rpcclient/cmd_samr.c
source3/rpcclient/cmd_spoolss.c
source3/rpcclient/cmd_srvsvc.c
source3/rpcclient/rpcclient.c

index e0a1275c0ca18af804a92cdfbb092e01f93ad934..a805407b9f7708c91a8a2946875d546db957b13e 100644 (file)
@@ -1563,7 +1563,6 @@ typedef struct {
 #include "ntdomain.h"
 
 #include "client.h"
-#include "rpcclient.h"
 
 /*
  * Size of new password account encoding string.  This is enough space to
index a3de9f626d88b120f35c0055749909132925e105..78f68dcc86b3fe079abafe284c4a2b635879d123 100644 (file)
 */
 
 #include "includes.h"
-
-extern pstring server;
+#include "rpcclient.h"
 
 /* Check DFS is supported by the remote server */
 
-static NTSTATUS cmd_dfs_exist(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_dfs_exist(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                              int argc, char **argv)
 {
-       TALLOC_CTX *mem_ctx;
        BOOL dfs_exists;
        NTSTATUS result;
 
@@ -37,34 +36,17 @@ static NTSTATUS cmd_dfs_exist(struct cli_state *cli, int argc, char **argv)
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_dfs_exist: talloc_init failed\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
-               DEBUG(0, ("Could not initialize netdfs pipe!\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
        result = cli_dfs_exist(cli, mem_ctx, &dfs_exists);
 
        if (NT_STATUS_IS_OK(result))
                printf("dfs is %spresent\n", dfs_exists ? "" : "not ");
 
-       cli_nt_session_close(cli);
-
-done:
-       talloc_destroy(mem_ctx);
        return result;
 }
 
-static NTSTATUS cmd_dfs_add(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_dfs_add(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                            int argc, char **argv)
 {
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result;
        char *entrypath, *servername, *sharename, *comment;
        uint32 flags = 0;
@@ -80,32 +62,15 @@ static NTSTATUS cmd_dfs_add(struct cli_state *cli, int argc, char **argv)
        sharename = argv[3];
        comment = argv[4];
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_dfs_add: talloc_init failed\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
-               DEBUG(0, ("Could not initialize netdfs pipe!\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
        result = cli_dfs_add(cli, mem_ctx, entrypath, servername, 
                             sharename, comment, flags);
 
-       cli_nt_session_close(cli);
-
-done:
-       talloc_destroy(mem_ctx);
        return result;
 }
 
-static NTSTATUS cmd_dfs_remove(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_dfs_remove(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                               int argc, char **argv)
 {
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result;
        char *entrypath, *servername, *sharename;
 
@@ -118,26 +83,9 @@ static NTSTATUS cmd_dfs_remove(struct cli_state *cli, int argc, char **argv)
        servername = argv[2];
        sharename = argv[3];
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_dfs_remove: talloc_init failed\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
-               DEBUG(0, ("Could not initialize netdfs pipe!\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
        result = cli_dfs_remove(cli, mem_ctx, entrypath, servername, 
                                sharename);
 
-       cli_nt_session_close(cli);
-
-done:
-       talloc_destroy(mem_ctx);
        return result;
 }
 
@@ -221,9 +169,9 @@ static void display_dfs_info_ctr(DFS_INFO_CTR *ctr)
 
 /* Enumerate dfs shares */
 
-static NTSTATUS cmd_dfs_enum(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_dfs_enum(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                             int argc, char **argv)
 {
-       TALLOC_CTX *mem_ctx;
        DFS_INFO_CTR ctr;
        NTSTATUS result;
        uint32 info_level = 1;
@@ -236,44 +184,25 @@ static NTSTATUS cmd_dfs_enum(struct cli_state *cli, int argc, char **argv)
        if (argc == 2)
                info_level = atoi(argv[1]);
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_dfs_enum: talloc_init failed\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
-               DEBUG(0, ("Could not initialize netdfs pipe!\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Call RPC function */
-
        result = cli_dfs_enum(cli, mem_ctx, info_level, &ctr);
-       if (NT_STATUS_IS_OK(result)) {
-               /* Print results */
-               display_dfs_info_ctr(&ctr);
-       }
 
-       cli_nt_session_close(cli);
+       if (NT_STATUS_IS_OK(result))
+               display_dfs_info_ctr(&ctr);
 
-done:
-       talloc_destroy(mem_ctx);
        return result;
 }
 
-static NTSTATUS cmd_dfs_getinfo(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_dfs_getinfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                                int argc, char **argv)
 {
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result;
        char *entrypath, *servername, *sharename;
        uint32 info_level = 1;
        DFS_INFO_CTR ctr;
 
        if (argc < 4 || argc > 5) {
-               printf("Usage: %s entrypath servername sharename [info_level]\n", argv[0]);
+               printf("Usage: %s entrypath servername sharename "
+                       "[info_level]\n", argv[0]);
                return NT_STATUS_OK;
        }
 
@@ -284,32 +213,12 @@ static NTSTATUS cmd_dfs_getinfo(struct cli_state *cli, int argc, char **argv)
        if (argc == 5)
                info_level = atoi(argv[4]);
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_dfs_getinfo: talloc_init failed\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETDFS)) {
-               DEBUG(0, ("Could not initialize netdfs pipe!\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
-               goto done;
-       }
-
-       /* Call RPC function */
-
        result = cli_dfs_get_info(cli, mem_ctx, entrypath, servername, 
                                  sharename, info_level, &ctr);
-       if (NT_STATUS_IS_OK(result)) {
-               /* Print results */
-               display_dfs_info_ctr(&ctr);
-       }
 
-       cli_nt_session_close(cli);
+       if (NT_STATUS_IS_OK(result))
+               display_dfs_info_ctr(&ctr);
 
-done:
-       talloc_destroy(mem_ctx);
        return result;
 }
 
@@ -319,11 +228,11 @@ struct cmd_set dfs_commands[] = {
 
        { "DFS" },
 
-       { "dfsexist",   cmd_dfs_exist,   "Query DFS support",    "" },
-       { "dfsadd",     cmd_dfs_add,     "Add a DFS share",      "" },
-       { "dfsremove",  cmd_dfs_remove,  "Remove a DFS share",   "" },
-       { "dfsgetinfo", cmd_dfs_getinfo, "Query DFS share info", "" },
-       { "dfsenum",    cmd_dfs_enum,    "Enumerate dfs shares", "" },
+       { "dfsexist",   cmd_dfs_exist,   PIPE_NETDFS, "Query DFS support",    "" },
+       { "dfsadd",     cmd_dfs_add,     PIPE_NETDFS, "Add a DFS share",      "" },
+       { "dfsremove",  cmd_dfs_remove,  PIPE_NETDFS, "Remove a DFS share",   "" },
+       { "dfsgetinfo", cmd_dfs_getinfo, PIPE_NETDFS, "Query DFS share info", "" },
+       { "dfsenum",    cmd_dfs_enum,    PIPE_NETDFS, "Enumerate dfs shares", "" },
 
        { NULL }
 };
index 273662b6f90e559045a5ef863b5e32dc591048ba..8b3e49051eda2ff6a0cbfe7539c2894556baca10 100644 (file)
 */
 
 #include "includes.h"
-
-extern pstring server;
+#include "rpcclient.h"
 
 /* Look up domain related information on a remote host */
-static NTSTATUS cmd_lsa_query_info_policy(struct cli_state *cli, int argc, char **argv) 
+
+static NTSTATUS cmd_lsa_query_info_policy(struct cli_state *cli, 
+                                          TALLOC_CTX *mem_ctx, int argc, 
+                                          char **argv) 
 {
        POLICY_HND pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL got_policy_hnd = False;
        DOM_SID dom_sid;
        fstring sid_str, domain_name;
        uint32 info_class = 3;
-       TALLOC_CTX *mem_ctx;
 
        if (argc > 2) {
                printf("Usage: %s [info_class]\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_lsa_query_info_poicy: talloc_init failed\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       if (argc == 2) {
+       if (argc == 2)
                info_class = atoi(argv[1]);
-       }
        
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
-               DEBUG(0, ("Could not initialize samr pipe!\n"));
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        result = cli_lsa_open_policy(cli, mem_ctx, True, 
                                     SEC_RIGHTS_MAXIMUM_ALLOWED,
                                     &pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_policy_hnd = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        /* Lookup info policy */
 
        result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class, 
                                           domain_name, &dom_sid);
-       if (!NT_STATUS_IS_OK(result)) {
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
        sid_to_string(sid_str, &dom_sid);
 
-       if (domain_name[0]) {
+       if (domain_name[0])
                printf("domain %s has sid %s\n", domain_name, sid_str);
-       } else {
+       else
                printf("could not query info for level %d\n", info_class);
-       }
-
-done:
-
-       if (got_policy_hnd) {
-               cli_lsa_close(cli, mem_ctx, &pol);
-       }
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
 
+ done:
        return result;
 }
 
 /* Resolve a list of names to a list of sids */
 
-static NTSTATUS cmd_lsa_lookup_names(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_lsa_lookup_names(struct cli_state *cli, 
+                                     TALLOC_CTX *mem_ctx, int argc, 
+                                     char **argv)
 {
        POLICY_HND pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL got_policy_hnd = False;
        DOM_SID *sids;
        uint32 *types;
        int num_names, i;
-       TALLOC_CTX *mem_ctx;
 
        if (argc == 1) {
                printf("Usage: %s [name1 [name2 [...]]]\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_lsa_lookup_names: talloc_init failed\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
-               DEBUG(0, ("Could not initialize samr pipe!\n"));
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-
        result = cli_lsa_open_policy(cli, mem_ctx, True, 
                                     SEC_RIGHTS_MAXIMUM_ALLOWED,
                                     &pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_policy_hnd = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        /* Lookup the names */
 
        result = cli_lsa_lookup_names(cli, mem_ctx, &pol, argc - 1, 
                                      &argv[1], &sids, &types, &num_names);
-       if (!NT_STATUS_IS_OK(result)) {
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
        /* Print results */
 
@@ -151,76 +112,52 @@ static NTSTATUS cmd_lsa_lookup_names(struct cli_state *cli, int argc, char **arg
        }
 
  done:
-
-       if (got_policy_hnd) {
-               cli_lsa_close(cli, mem_ctx, &pol);
-       }
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Resolve a list of SIDs to a list of names */
 
-static NTSTATUS cmd_lsa_lookup_sids(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                                    int argc, char **argv)
 {
        POLICY_HND pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL got_policy_hnd = False;
        DOM_SID *sids;
        char **names;
        uint32 *types;
        int num_names, i;
-       TALLOC_CTX *mem_ctx;
 
        if (argc == 1) {
                printf("Usage: %s [sid1 [sid2 [...]]]\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_lsa_lookup_sids: talloc_init failed\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
-               DEBUG(0, ("Could not initialize samr pipe!\n"));
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        result = cli_lsa_open_policy(cli, mem_ctx, True, 
                                     SEC_RIGHTS_MAXIMUM_ALLOWED,
                                     &pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_policy_hnd = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        /* Convert arguments to sids */
 
        sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * (argc - 1));
 
        if (!sids) {
-               printf("out of memory\n");
+               printf("could not allocate memory for %d sids\n", argc - 1);
                goto done;
        }
 
-       for (i = 0; i < argc - 1; i++) {
+       for (i = 0; i < argc - 1; i++)
                string_to_sid(&sids[i], argv[i + 1]);
-       }
 
        /* Lookup the SIDs */
 
        result = cli_lsa_lookup_sids(cli, mem_ctx, &pol, argc - 1, sids, 
                                     &names, &types, &num_names);
-       if (!NT_STATUS_IS_OK(result)) {
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
        /* Print results */
 
@@ -232,77 +169,44 @@ static NTSTATUS cmd_lsa_lookup_sids(struct cli_state *cli, int argc, char **argv
                       "*unknown*", types[i]);
        }
 
-#if 0  /* JERRY */
-       SAFE_FREE(sids);
-       SAFE_FREE(types);      
-
-       for (i = 0; i < num_names; i++) {
-               SAFE_FREE(names[i]);
-       }
-
-       SAFE_FREE(names);
-#endif
-
  done:
-
-       if (got_policy_hnd) {
-               cli_lsa_close(cli, mem_ctx, &pol);
-       }
-
-       cli_nt_session_close(cli);
-       talloc_destroy (mem_ctx);
-
        return result;
 }
 
 /* Enumerate list of trusted domains */
 
-static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli, 
+                                       TALLOC_CTX *mem_ctx, int argc, 
+                                       char **argv)
 {
        POLICY_HND pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL got_policy_hnd = False;
        DOM_SID *domain_sids;
        char **domain_names;
        uint32 enum_ctx = 0;
        uint32 num_domains;
        int i;
-       TALLOC_CTX *mem_ctx;
 
        if (argc != 1) {
                printf("Usage: %s\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_lsa_enum_trust_dom: talloc_init failed\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
-               DEBUG(0, ("Could not initialize samr pipe!\n"));
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        result = cli_lsa_open_policy(cli, mem_ctx, True, 
                                     SEC_RIGHTS_MAXIMUM_ALLOWED,
                                     &pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_policy_hnd = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        /* Lookup list of trusted domains */
 
        result = cli_lsa_enum_trust_dom(cli, mem_ctx, &pol, &enum_ctx,
                                        &num_domains, &domain_names,
                                        &domain_sids);
-       if (!NT_STATUS_IS_OK(result)) {
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
        /* Print results */
 
@@ -315,14 +219,6 @@ static NTSTATUS cmd_lsa_enum_trust_dom(struct cli_state *cli, int argc, char **a
        }
 
  done:
-
-       if (got_policy_hnd) {
-               cli_lsa_close(cli, mem_ctx, &pol);
-       }
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
@@ -332,10 +228,10 @@ struct cmd_set lsarpc_commands[] = {
 
        { "LSARPC" },
 
-       { "lsaquery",    cmd_lsa_query_info_policy,     "Query info policy",         "" },
-       { "lookupsids",  cmd_lsa_lookup_sids,           "Convert SIDs to names",     "" },
-       { "lookupnames", cmd_lsa_lookup_names,          "Convert names to SIDs",     "" },
-       { "enumtrust",   cmd_lsa_enum_trust_dom,        "Enumerate trusted domains", "" },
+       { "lsaquery",    cmd_lsa_query_info_policy,     PIPE_LSARPC, "Query info policy",         "" },
+       { "lookupsids",  cmd_lsa_lookup_sids,           PIPE_LSARPC, "Convert SIDs to names",     "" },
+       { "lookupnames", cmd_lsa_lookup_names,          PIPE_LSARPC, "Convert names to SIDs",     "" },
+       { "enumtrust",   cmd_lsa_enum_trust_dom,        PIPE_LSARPC, "Enumerate trusted domains", "" },
 
        { NULL }
 };
index 3d73fc1fde98c6c36244cdba4fb422b035ba197f..a935900faa5fe6183c37dc67363ef15faee0269e 100644 (file)
 */
 
 #include "includes.h"
+#include "rpcclient.h"
 
-static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli, int argc,
-                                      char **argv)
+static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx, int argc, 
+                                         char **argv)
 {
        uint32 query_level = 1;
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 
        if (argc > 1) {
-               printf("Usage: %s\n", argv[0]);
+               fprintf(stderr, "Usage: %s\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
-               goto done;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
-               DEBUG(0, ("Could not initialize srvsvc pipe!\n"));
-               goto done;
-       }
-
        result = cli_netlogon_logon_ctrl2(cli, mem_ctx, query_level);
-       if (!NT_STATUS_IS_OK(result)) {
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
        /* Display results */
 
@@ -57,32 +46,20 @@ static NTSTATUS cmd_netlogon_logon_ctrl2(struct cli_state *cli, int argc,
        return result;
 }
 
-static NTSTATUS cmd_netlogon_logon_ctrl(struct cli_state *cli, int argc,
-                                     char **argv)
+static NTSTATUS cmd_netlogon_logon_ctrl(struct cli_state *cli, 
+                                        TALLOC_CTX *mem_ctx, int argc, 
+                                        char **argv)
 {
 #if 0
        uint32 query_level = 1;
 #endif
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 
        if (argc > 1) {
-               printf("Usage: %s\n", argv[0]);
+               fprintf(stderr, "Usage: %s\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
-               goto done;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
-               DEBUG(0, ("Could not initialize netlogon pipe!\n"));
-               goto done;
-       }
-
 #if 0
        result = cli_netlogon_logon_ctrl(cli, mem_ctx, query_level);
        if (!NT_STATUS_IS_OK(result)) {
@@ -92,7 +69,6 @@ static NTSTATUS cmd_netlogon_logon_ctrl(struct cli_state *cli, int argc,
 
        /* Display results */
 
- done:
        return result;
 }
 
@@ -110,25 +86,25 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
                         unistr2_to_ascii(name,
                                          &deltas[i].domain_info.uni_dom_name,
                                          sizeof(name) - 1);
-                        DEBUG(0, ("Domain: %s\n", name));
+                        printf("Domain: %s\n", name);
                         break;
                 case SAM_DELTA_GROUP_INFO:
                         unistr2_to_ascii(name,
                                          &deltas[i].group_info.uni_grp_name,
                                          sizeof(name) - 1);
-                        DEBUG(0, ("Group: %s\n", name));
+                        printf("Group: %s\n", name);
                         break;
                 case SAM_DELTA_ACCOUNT_INFO:
                         unistr2_to_ascii(name, 
                                          &deltas[i].account_info.uni_acct_name,
                                          sizeof(name) - 1);
-                        DEBUG(0, ("Account: %s\n", name));
+                        printf("Account: %s\n", name);
                         break;
                 case SAM_DELTA_ALIAS_INFO:
                         unistr2_to_ascii(name, 
                                          &deltas[i].alias_info.uni_als_name,
                                          sizeof(name) - 1);
-                        DEBUG(0, ("Alias: %s\n", name));
+                        printf("Alias: %s\n", name);
                         break;
                 case SAM_DELTA_ALIAS_MEM: {
                         SAM_ALIAS_MEM_INFO *alias = &deltas[i].als_mem_info;
@@ -138,7 +114,7 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
 
                                 sid_to_string(sid_str, &alias->sids[j].sid);
 
-                                DEBUG(0, ("%s\n", sid_str));
+                                printf("%s\n", sid_str);
                         }
                         break;
                 }
@@ -146,20 +122,20 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
                         SAM_GROUP_MEM_INFO *group = &deltas[i].grp_mem_info;
 
                         for (j = 0; j < group->num_members; j++)
-                                DEBUG(0, ("rid 0x%x, attrib 0x%08x\n", 
-                                          group->rids[j], group->attribs[j]));
+                                printf("rid 0x%x, attrib 0x%08x\n", 
+                                          group->rids[j], group->attribs[j]);
                         break;
                 }
                 case SAM_DELTA_SAM_STAMP: {
                         SAM_DELTA_STAMP *stamp = &deltas[i].stamp;
 
-                        DEBUG(0, ("sam sequence update: 0x%04x\n",
-                                  stamp->seqnum));
+                        printf("sam sequence update: 0x%04x\n",
+                                  stamp->seqnum);
                         break;
                 }                                  
                 default:
-                        DEBUG(0, ("unknown delta type 0x%02x\n", 
-                                  hdr_deltas[i].type));
+                        printf("unknown delta type 0x%02x\n", 
+                                  hdr_deltas[i].type);
                         break;
                 }
         }
@@ -167,18 +143,18 @@ static void display_sam_sync(uint32 num_deltas, SAM_DELTA_HDR *hdr_deltas,
 
 /* Perform sam synchronisation */
 
-static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, int argc,
-                                    char **argv)
+static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, 
+                                      TALLOC_CTX *mem_ctx, int argc,
+                                      char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
         unsigned char trust_passwd[16];
-        TALLOC_CTX *mem_ctx;
         uint32 database_id = 0, num_deltas;
         SAM_DELTA_HDR *hdr_deltas;
         SAM_DELTA_CTR *deltas;
 
         if (argc > 2) {
-                printf("Usage: %s [database_id]\n", argv[0]);
+                fprintf(stderr, "Usage: %s [database_id]\n", argv[0]);
                 return NT_STATUS_OK;
         }
 
@@ -186,34 +162,22 @@ static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, int argc,
                 database_id = atoi(argv[1]);
 
         if (!secrets_init()) {
-                DEBUG(0, ("Unable to initialise secrets database\n"));
+                fprintf(stderr, "Unable to initialise secrets database\n");
                 return result;
         }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("talloc_init failed\n"));
-               return result;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
-               DEBUG(0, ("Could not initialize netlogon pipe!\n"));
-               goto done;
-       }
-
         /* Initialise session credentials */
 
        if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
                                                   NULL)) {
-               DEBUG(0, ("could not fetch trust account password\n"));
+               fprintf(stderr, "could not fetch trust account password\n");
                goto done;
        }        
 
         result = cli_nt_setup_creds(cli, trust_passwd);
 
         if (!NT_STATUS_IS_OK(result)) {
-                DEBUG(0, ("Error initialising session creds\n"));
+                fprintf(stderr, "Error initialising session creds\n");
                 goto done;
         }
 
@@ -221,36 +185,33 @@ static NTSTATUS cmd_netlogon_sam_sync(struct cli_state *cli, int argc,
 
        result = cli_netlogon_sam_sync(cli, mem_ctx, database_id,
                                       &num_deltas, &hdr_deltas, &deltas);
-       if (!NT_STATUS_IS_OK(result)) {
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
         /* Display results */
 
         display_sam_sync(num_deltas, hdr_deltas, deltas);
 
  done:
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-        
         return result;
 }
 
 /* Perform sam delta synchronisation */
 
-static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, int argc,
-                                      char **argv)
+static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, 
+                                        TALLOC_CTX *mem_ctx, int argc,
+                                        char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
         unsigned char trust_passwd[16];
-        TALLOC_CTX *mem_ctx = NULL;
         uint32 database_id, num_deltas, tmp;
         SAM_DELTA_HDR *hdr_deltas;
         SAM_DELTA_CTR *deltas;
         UINT64_S seqnum;
 
         if (argc != 3) {
-                printf("Usage: %s database_id seqnum\n", argv[0]);
+                fprintf(stderr, "Usage: %s database_id seqnum\n", argv[0]);
                 return NT_STATUS_OK;
         }
 
@@ -261,34 +222,22 @@ static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, int argc,
         seqnum.high = 0;
 
         if (!secrets_init()) {
-                DEBUG(0, ("Unable to initialise secrets database\n"));
+                fprintf(stderr, "Unable to initialise secrets database\n");
                 goto done;
         }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("talloc_init failed\n"));
-               goto done;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_NETLOGON)) {
-               DEBUG(0, ("Could not initialize netlogon pipe!\n"));
-               goto done;
-       }
-
         /* Initialise session credentials */
 
        if (!secrets_fetch_trust_account_password(lp_workgroup(), trust_passwd,
                                                   NULL)) {
-               DEBUG(0, ("could not fetch trust account password\n"));
+               fprintf(stderr, "could not fetch trust account password\n");
                goto done;
        }        
 
         result = cli_nt_setup_creds(cli, trust_passwd);
 
         if (!NT_STATUS_IS_OK(result)) {
-                DEBUG(0, ("Error initialising session creds\n"));
+                fprintf(stderr, "Error initialising session creds\n");
                 goto done;
         }
 
@@ -297,18 +246,15 @@ static NTSTATUS cmd_netlogon_sam_deltas(struct cli_state *cli, int argc,
        result = cli_netlogon_sam_deltas(cli, mem_ctx, database_id,
                                         seqnum, &num_deltas, 
                                         &hdr_deltas, &deltas);
-       if (!NT_STATUS_IS_OK(result)) {
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
         /* Display results */
 
         display_sam_sync(num_deltas, hdr_deltas, deltas);
         
  done:
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
         return result;
 }
 
@@ -318,10 +264,10 @@ struct cmd_set netlogon_commands[] = {
 
        { "NETLOGON" },
 
-       { "logonctrl2", cmd_netlogon_logon_ctrl2, "Logon Control 2",     "" },
-       { "logonctrl",  cmd_netlogon_logon_ctrl,  "Logon Control",       "" },
-       { "samsync",    cmd_netlogon_sam_sync,    "Sam Synchronisation", "" },
-       { "samdeltas",  cmd_netlogon_sam_deltas,  "Query Sam Deltas", "" },
+       { "logonctrl2", cmd_netlogon_logon_ctrl2, PIPE_NETLOGON, "Logon Control 2",     "" },
+       { "logonctrl",  cmd_netlogon_logon_ctrl,  PIPE_NETLOGON, "Logon Control",       "" },
+       { "samsync",    cmd_netlogon_sam_sync,    PIPE_NETLOGON, "Sam Synchronisation", "" },
+       { "samdeltas",  cmd_netlogon_sam_deltas,  PIPE_NETLOGON, "Query Sam Deltas",    "" },
 
        { NULL }
 };
index 28cd83ef58c1e56e4251ed2f8f0a451d30ea15ee..991d76b3a32f5cefa8fa9178dc3c60e9d0e5fe76 100644 (file)
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-#ifdef SYSLOG
-#undef SYSLOG
-#endif
-
 #include "includes.h"
-
-extern int smb_tidx;
-
-extern FILE* out_hnd;
+#include "rpcclient.h"
 
 /*
  * keys.  of the form:
@@ -902,7 +895,8 @@ static void cmd_reg_get_key_sec(struct client_info *info)
 /****************************************************************************
 nt registry shutdown
 ****************************************************************************/
-static NTSTATUS cmd_reg_shutdown(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_reg_shutdown(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                                 int argc, char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        fstring msg;
@@ -911,7 +905,6 @@ static NTSTATUS cmd_reg_shutdown(struct cli_state *cli, int argc, char **argv)
        int opt;
        int ret;
        char *srv_name;
-       TALLOC_CTX *mem_ctx;
 
        ret = asprintf (&srv_name, "\\\\%s", cli->desthost);
        if (ret < 0) {
@@ -920,18 +913,6 @@ static NTSTATUS cmd_reg_shutdown(struct cli_state *cli, int argc, char **argv)
        }
        strupper(srv_name);
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_getprinter: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-       
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_WINREG)) {
-               fprintf (stderr, "Could not initialize winreg pipe!\n");
-               goto done;
-       }
-                       
        *msg = 0;
        optind = 0; /* TODO: test if this hack works on other systems too --simo */
 
@@ -974,23 +955,19 @@ static NTSTATUS cmd_reg_shutdown(struct cli_state *cli, int argc, char **argv)
        else
                DEBUG(5,("cmd_reg_shutdown: query failed\n"));
 
-       cli_nt_session_close(cli);
-
-done:
-       talloc_destroy(mem_ctx);
-                       
        return result;
 }
 
 /****************************************************************************
 abort a shutdown
 ****************************************************************************/
-static NTSTATUS cmd_reg_abort_shutdown(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_reg_abort_shutdown(struct cli_state *cli, 
+                                       TALLOC_CTX *mem_ctx, int argc, 
+                                       char **argv)
 {
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        int ret;
        char *srv_name;
-       TALLOC_CTX *mem_ctx;
 
        ret = asprintf(&srv_name, "\\\\%s", cli->desthost);
        if (ret < 0) {
@@ -999,18 +976,6 @@ static NTSTATUS cmd_reg_abort_shutdown(struct cli_state *cli, int argc, char **a
        }
        strupper(srv_name);
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_getprinter: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-       
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_WINREG)) {
-               fprintf (stderr, "Could not initialize winreg pipe!\n");
-               goto done;
-       }
-
        result = cli_reg_abort_shutdown(cli, mem_ctx, srv_name);
 
        if (NT_STATUS_IS_OK(result))
@@ -1018,11 +983,6 @@ static NTSTATUS cmd_reg_abort_shutdown(struct cli_state *cli, int argc, char **a
        else
                DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
 
-       cli_nt_session_close(cli);
-
-done:
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
@@ -1032,10 +992,10 @@ struct cmd_set reg_commands[] = {
 
        { "REG"  },
 
-       { "shutdown",           cmd_reg_shutdown,               "Remote Shutdown",
+       { "shutdown",           cmd_reg_shutdown,               PIPE_WINREG, "Remote Shutdown",
                                "[-m message] [-t timeout] [-r] [-f] (-r == reboot, -f == force)" },
                                
-       { "abortshutdown",      cmd_reg_abort_shutdown,         "Abort Shutdown",
+       { "abortshutdown",      cmd_reg_abort_shutdown,         PIPE_WINREG, "Abort Shutdown",
                                "" },                           
 /*
        { "regenum",            cmd_reg_enum,                   "Registry Enumeration",
index 883df2b2d3e57134ef217dc06a198588df4ed92e..9a4b87e11a960c1732309821252982e3e6861eb6 100644 (file)
@@ -24,6 +24,7 @@
 */
 
 #include "includes.h"
+#include "rpcclient.h"
 
 extern DOM_SID domain_sid;
 
@@ -138,20 +139,17 @@ void display_sam_info_1(SAM_ENTRY1 *e1, SAM_STR1 *s1)
 /**********************************************************************
  * Query user information 
  */
-static NTSTATUS cmd_samr_query_user(struct cli_state *cli, int argc, char **argv) 
+static NTSTATUS cmd_samr_query_user(struct cli_state *cli, 
+                                    TALLOC_CTX *mem_ctx,
+                                    int argc, char **argv) 
 {
        POLICY_HND connect_pol, domain_pol, user_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 info_level = 21;
-       BOOL    got_connect_pol = False, 
-               got_domain_pol = False,
-               got_user_pol = False;
        SAM_USERINFO_CTR *user_ctr;
-       fstring                 server;
-       TALLOC_CTX              *mem_ctx;
+       fstring server;
        uint32 user_rid;
        
-       
        if (argc != 2) {
                printf("Usage: %s rid\n", argv[0]);
                return NT_STATUS_OK;
@@ -159,68 +157,42 @@ static NTSTATUS cmd_samr_query_user(struct cli_state *cli, int argc, char **argv
        
        sscanf(argv[1], "%i", &user_rid);
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_samr_query_user: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-       
        slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (server);
        
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
                                  &connect_pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_connect_pol = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      &domain_sid, &domain_pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_domain_pol = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
                                    MAXIMUM_ALLOWED_ACCESS,
                                    user_rid, &user_pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_user_pol = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        ZERO_STRUCT(user_ctr);
 
        result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol, 
                                         info_level, &user_ctr);
-       if (!NT_STATUS_IS_OK(result)) {
+
+       if (!NT_STATUS_IS_OK(result))
                goto done;
-       }
 
        display_sam_user_info_21(user_ctr->info.id21);
 
 done:
-       if (got_user_pol) cli_samr_close(cli, mem_ctx, &user_pol);
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
@@ -270,16 +242,15 @@ static void display_group_info_ctr(GROUP_INFO_CTR *ctr)
 /***********************************************************************
  * Query group information 
  */
-static NTSTATUS cmd_samr_query_group(struct cli_state *cli, int argc, char **argv) 
+static NTSTATUS cmd_samr_query_group(struct cli_state *cli, 
+                                     TALLOC_CTX *mem_ctx,
+                                     int argc, char **argv) 
 {
        POLICY_HND connect_pol, domain_pol, group_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 info_level = 1;
-       BOOL got_connect_pol = False, got_domain_pol = False,
-               got_group_pol = False;
        GROUP_INFO_CTR group_ctr;
        fstring                 server; 
-       TALLOC_CTX              *mem_ctx;
        uint32 group_rid;
        
        if (argc != 2) {
@@ -289,48 +260,30 @@ static NTSTATUS cmd_samr_query_group(struct cli_state *cli, int argc, char **arg
 
        group_rid = atoi(argv[1]);
 
-       if (!(mem_ctx=talloc_init())) {
-               DEBUG(0,("cmd_samr_query_group: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-       
        slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (server);
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
                                  &connect_pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_connect_pol = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      &domain_sid, &domain_pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_domain_pol = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
                                     MAXIMUM_ALLOWED_ACCESS,
                                     group_rid, &group_pol);
-       if (!NT_STATUS_IS_OK(result)) {
-               goto done;
-       }
 
-       got_group_pol = True;
+       if (!NT_STATUS_IS_OK(result))
+               goto done;
 
        ZERO_STRUCT(group_ctr);
 
@@ -343,56 +296,34 @@ static NTSTATUS cmd_samr_query_group(struct cli_state *cli, int argc, char **arg
        display_group_info_ctr(&group_ctr);
 
 done:
-       if (got_group_pol) cli_samr_close(cli, mem_ctx, &group_pol);
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Query groups a user is a member of */
 
-static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli, int argc, char **argv) 
+static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli, 
+                                          TALLOC_CTX *mem_ctx,
+                                          int argc, char **argv) 
 {
        POLICY_HND              connect_pol, 
                                domain_pol, 
                                user_pol;
        NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
-       BOOL                    got_connect_pol = False, 
-                               got_domain_pol = False,
-                               got_user_pol = False;
        uint32                  num_groups, 
                                user_rid;
        DOM_GID                 *user_gids;
        int                     i;
        fstring                 server;
-       TALLOC_CTX              *mem_ctx;
        
        if (argc != 2) {
                printf("Usage: %s rid\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_samr_query_usergroups: talloc_init returned NULL!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
        sscanf(argv[1], "%i", &user_rid);
 
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (server);
                
@@ -402,8 +333,6 @@ static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli, int argc, char
                goto done;
        }
 
-       got_connect_pol = True;
-
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      &domain_sid, &domain_pol);
@@ -411,8 +340,6 @@ static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli, int argc, char
                goto done;
        }
 
-       got_domain_pol = True;
-
        result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
                                    MAXIMUM_ALLOWED_ACCESS,
                                    user_rid, &user_pol);
@@ -420,8 +347,6 @@ static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli, int argc, char
                goto done;
        }
 
-       got_user_pol = True;
-
        result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
                                           &num_groups, &user_gids);
        if (!NT_STATUS_IS_OK(result)) {
@@ -434,52 +359,30 @@ static NTSTATUS cmd_samr_query_usergroups(struct cli_state *cli, int argc, char
        }
 
  done:
-       if (got_user_pol) cli_samr_close(cli, mem_ctx, &user_pol);
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Query members of a group */
 
-static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli, int argc, char **argv) 
+static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli, 
+                                        TALLOC_CTX *mem_ctx,
+                                        int argc, char **argv) 
 {
        POLICY_HND connect_pol, domain_pol, group_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL    got_connect_pol = False, 
-               got_domain_pol = False,
-               got_group_pol = False;
        uint32 num_members, *group_rids, *group_attrs, group_rid;
        int i;
        fstring                 server;
-       TALLOC_CTX              *mem_ctx;
        
        if (argc != 2) {
                printf("Usage: %s rid\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_samr_query_groupmem: talloc_init returned NULL!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
        sscanf(argv[1], "%i", &group_rid);
 
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        slprintf (server, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (server);
 
@@ -489,8 +392,6 @@ static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli, int argc, char **
                goto done;
        }
 
-       got_connect_pol = True;
-
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      &domain_sid, &domain_pol);
@@ -498,8 +399,6 @@ static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli, int argc, char **
                goto done;
        }
 
-       got_domain_pol = True;
-
        result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
                                     MAXIMUM_ALLOWED_ACCESS,
                                     group_rid, &group_pol);
@@ -507,8 +406,6 @@ static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli, int argc, char **
                goto done;
        }
 
-       got_group_pol = True;
-
        result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
                                         &num_members, &group_rids,
                                         &group_attrs);
@@ -522,25 +419,17 @@ static NTSTATUS cmd_samr_query_groupmem(struct cli_state *cli, int argc, char **
        }
 
  done:
-       if (got_group_pol) cli_samr_close(cli, mem_ctx, &group_pol);
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Enumerate domain groups */
 
-static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli, int argc, 
-                                      char **argv) 
+static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx,
+                                         int argc, char **argv) 
 {
        POLICY_HND connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL got_connect_pol = False, got_domain_pol = False;
-       TALLOC_CTX *mem_ctx;
        uint32 start_idx, size, num_dom_groups, i;
        struct acct_info *dom_groups;
 
@@ -549,22 +438,8 @@ static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli, int argc,
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0, ("cmd_samr_enum_dom_groups: talloc_init returned "
-                         "NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Get sam policy handle */
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
@@ -573,8 +448,6 @@ static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_connect_pol = True;
-
        /* Get domain policy handle */
 
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
@@ -584,8 +457,6 @@ static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_domain_pol = True;
-
        /* Enumerate domain groups */
 
        start_idx = 0;
@@ -600,24 +471,16 @@ static NTSTATUS cmd_samr_enum_dom_groups(struct cli_state *cli, int argc,
                       dom_groups[i].rid);
 
  done:
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Query alias membership */
 
-static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli, int argc, 
-                                     char **argv) 
+static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli, 
+                                        TALLOC_CTX *mem_ctx,
+                                        int argc, char **argv) 
 {
        POLICY_HND connect_pol, domain_pol, alias_pol;
-       BOOL got_connect_pol = False, got_domain_pol = False,
-               got_alias_pol = False;
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        uint32 alias_rid, num_members, i;
        DOM_SID *alias_sids;
@@ -627,24 +490,10 @@ static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli, int argc,
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx=talloc_init())) {
-               DEBUG(0,("cmd_samr_query_aliasmem: talloc_init() "
-                        "returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        sscanf(argv[1], "%i", &alias_rid);
 
-       /* Initialise RPC connection */
-
        fetch_domain_sid(cli);
 
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Open SAMR handle */
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
@@ -653,8 +502,6 @@ static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_connect_pol = True;
-
        /* Open handle on domain */
 
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
@@ -664,8 +511,6 @@ static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_domain_pol = True;
-
        /* Open handle on alias */
 
        result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
@@ -675,8 +520,6 @@ static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_alias_pol = True;
-
        result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
                                         &num_members, &alias_sids);
        if (!NT_STATUS_IS_OK(result)) {
@@ -691,25 +534,17 @@ static NTSTATUS cmd_samr_query_aliasmem(struct cli_state *cli, int argc,
        }
 
  done:
-       if (got_alias_pol) cli_samr_close(cli, mem_ctx, &alias_pol);
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Query display info */
 
-static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli, int argc, 
-                                       char **argv) 
+static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli, 
+                                        TALLOC_CTX *mem_ctx,
+                                        int argc, char **argv) 
 {
        POLICY_HND connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL got_connect_pol = False, got_domain_pol = False;
-       TALLOC_CTX *mem_ctx;
        uint32 start_idx, max_entries, num_entries, i;
        uint16 info_level = 1;
        SAM_DISPINFO_CTR ctr;
@@ -720,22 +555,8 @@ static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli, int argc,
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0, ("cmd_samr_query_dispinfo: talloc_init returned "
-                         "NULL!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Get sam policy handle */
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
@@ -744,8 +565,6 @@ static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_connect_pol = True;
-
        /* Get domain policy handle */
 
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
@@ -755,8 +574,6 @@ static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_domain_pol = True;
-
        /* Query display info */
 
        start_idx = 0;
@@ -777,24 +594,17 @@ static NTSTATUS cmd_samr_query_dispinfo(struct cli_state *cli, int argc,
        }
 
  done:
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Query domain info */
 
-static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli, int argc, 
-                                      char **argv) 
+static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli, 
+                                       TALLOC_CTX *mem_ctx,
+                                       int argc, char **argv) 
 {
        POLICY_HND connect_pol, domain_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL got_connect_pol = False, got_domain_pol = False;
-       TALLOC_CTX *mem_ctx;
        uint16 switch_value = 2;
        SAM_UNK_CTR ctr;
 
@@ -806,22 +616,8 @@ static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli, int argc,
        if (argc == 2)
                switch_value = atoi(argv[1]);
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0, ("cmd_samr_query_dispinfo: talloc_init returned "
-                         "NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Get sam policy handle */
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
@@ -830,8 +626,6 @@ static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_connect_pol = True;
-
        /* Get domain policy handle */
 
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
@@ -841,8 +635,6 @@ static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_domain_pol = True;
-
        /* Query domain info */
 
        result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
@@ -864,25 +656,17 @@ static NTSTATUS cmd_samr_query_dominfo(struct cli_state *cli, int argc,
        }
 
  done:
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Create domain user */
 
-static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli, int argc, 
-                                        char **argv) 
+static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx,
+                                         int argc, char **argv) 
 {
        POLICY_HND connect_pol, domain_pol, user_pol;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL got_connect_pol = False, got_domain_pol = False, 
-               got_user_pol = False;
-       TALLOC_CTX *mem_ctx;
        char *acct_name;
        uint16 acb_info;
        uint32 unknown, user_rid;
@@ -894,22 +678,8 @@ static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli, int argc,
 
        acct_name = argv[1];
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0, ("cmd_samr_query_dispinfo: talloc_init returned "
-                         "NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Get sam policy handle */
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
@@ -918,8 +688,6 @@ static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_connect_pol = True;
-
        /* Get domain policy handle */
 
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
@@ -929,8 +697,6 @@ static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_domain_pol = True;
-
        /* Create domain user */
 
        acb_info = ACB_NORMAL;
@@ -943,29 +709,19 @@ static NTSTATUS cmd_samr_create_dom_user(struct cli_state *cli, int argc,
                goto done;
        }
 
-       got_user_pol = True;
-
  done:
-       if (got_user_pol) cli_samr_close(cli, mem_ctx, &user_pol);
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Lookup sam names */
 
-static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli, int argc, 
-                                   char **argv) 
+static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli, 
+                                      TALLOC_CTX *mem_ctx,
+                                      int argc, char **argv) 
 {
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        POLICY_HND connect_pol, domain_pol;
-       BOOL got_connect_pol = False, got_domain_pol = False;
-       uint32 flags = 0x000003e8;
+       uint32 flags = 0x000003e8; /* Unknown */
        uint32 num_rids, num_names, *name_types, *rids;
        char **names;
        int i;
@@ -975,40 +731,25 @@ static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli, int argc,
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0, ("cmd_samr_lookup_names: talloc_init failed\n"));
-               return result;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Get sam policy and domain handles */
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
                                  &connect_pol);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
-       got_connect_pol = True;
-
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      &domain_sid, &domain_pol);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
-       got_domain_pol = True;
-
        /* Look up names */
 
        num_names = argc - 1;
@@ -1020,6 +761,7 @@ static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli, int argc,
        result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
                                       flags, num_names, names,
                                       &num_rids, &rids, &name_types);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
@@ -1031,25 +773,18 @@ static NTSTATUS cmd_samr_lookup_names(struct cli_state *cli, int argc,
                       name_types[i]);
 
  done:
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Lookup sam rids */
 
-static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli, int argc, 
-                                  char **argv) 
+static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli, 
+                                     TALLOC_CTX *mem_ctx,
+                                     int argc, char **argv) 
 {
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        POLICY_HND connect_pol, domain_pol;
-       BOOL got_connect_pol = False, got_domain_pol = False;
-       uint32 flags = 0x000003e8;
+       uint32 flags = 0x000003e8; /* Unknown */
        uint32 num_rids, num_names, *rids, *name_types;
        char **names;
        int i;
@@ -1059,40 +794,25 @@ static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli, int argc,
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0, ("cmd_samr_lookup_rids: talloc_init failed\n"));
-               return result;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               fprintf (stderr, "Could not initialize samr pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Get sam policy and domain handles */
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
                                  &connect_pol);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
-       got_connect_pol = True;
-
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      &domain_sid, &domain_pol);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
-       got_domain_pol = True;
-
        /* Look up rids */
 
        num_rids = argc - 1;
@@ -1104,6 +824,7 @@ static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli, int argc,
        result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
                                      flags, num_rids, rids,
                                      &num_names, &names, &name_types);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
@@ -1114,21 +835,15 @@ static NTSTATUS cmd_samr_lookup_rids(struct cli_state *cli, int argc,
                printf("rid %x: %s (%d)\n", rids[i], names[i], name_types[i]);
 
  done:
-       if (got_domain_pol) cli_samr_close(cli, mem_ctx, &domain_pol);
-       if (got_connect_pol) cli_samr_close(cli, mem_ctx, &connect_pol);
-
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /* Delete domain user */
 
-static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, int argc, 
-                                      char **argv) 
+static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx,
+                                         int argc, char **argv) 
 {
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        POLICY_HND connect_pol, domain_pol, user_pol;
 
@@ -1137,25 +852,13 @@ static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, int argc,
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0, ("cmd_samr_delete_dom_user: talloc_init failed\n"));
-               return result;
-       }
-
        fetch_domain_sid(cli);
 
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_SAMR)) {
-               DEBUG(0, ("cmd_samr_delete_dom_user: could not open samr pipe!\n"));
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Get sam policy and domain handles */
 
        result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
                                  &connect_pol);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
@@ -1163,6 +866,7 @@ static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, int argc,
        result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
                                      MAXIMUM_ALLOWED_ACCESS,
                                      &domain_sid, &domain_pol);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
@@ -1171,12 +875,13 @@ static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, int argc,
 
        {
                uint32 *user_rids, num_rids, *name_types;
-               uint32 flags = 0x000003e8;
+               uint32 flags = 0x000003e8; /* Unknown */
 
                result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
                                               flags, 1, &argv[1],
                                               &num_rids, &user_rids,
                                               &name_types);
+
                if (!NT_STATUS_IS_OK(result)) {
                        goto done;
                }
@@ -1184,6 +889,7 @@ static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, int argc,
                result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
                                            MAXIMUM_ALLOWED_ACCESS,
                                            user_rids[0], &user_pol);
+
                if (!NT_STATUS_IS_OK(result)) {
                        goto done;
                }
@@ -1192,6 +898,7 @@ static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, int argc,
        /* Delete user */
 
        result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
@@ -1199,9 +906,6 @@ static NTSTATUS cmd_samr_delete_dom_user(struct cli_state *cli, int argc,
        /* Display results */
 
  done:
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
index 31683c63866b2eb423b5fc5d695bb8528dd90da9..abcb3589d49f80d68a38a4728966956bd89d522c 100644 (file)
 */
 
 #include "includes.h"
-
-extern pstring server;
-extern pstring global_myname;
-extern pstring username, password;
-extern pstring workgroup;
+#include "rpcclient.h"
 
 struct table_node {
        char    *long_archi;
@@ -78,8 +74,9 @@ BOOL get_short_archi(char *short_archi, char *long_archi)
 /**********************************************************************
  * dummy function  -- placeholder
   */
-static NTSTATUS cmd_spoolss_not_implemented (struct cli_state *cli, 
-                                          int argc, char **argv)
+static NTSTATUS cmd_spoolss_not_implemented(struct cli_state *cli, 
+                                            TALLOC_CTX *mem_ctx,
+                                            int argc, char **argv)
 {
        printf ("(*) This command is not currently implemented.\n");
        return NT_STATUS_OK;
@@ -140,13 +137,14 @@ static void display_sec_desc(SEC_DESC *sec)
 /***********************************************************************
  * Get printer information
  */
-static NTSTATUS cmd_spoolss_open_printer_ex(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_open_printer_ex(struct cli_state *cli, 
+                                            TALLOC_CTX *mem_ctx,
+                                            int argc, char **argv)
 {
        NTSTATUS        result = NT_STATUS_UNSUCCESSFUL; 
        pstring         printername;
        fstring         servername, user;
        POLICY_HND      hnd;
-       TALLOC_CTX      *mem_ctx;
        
        if (argc != 2) {
                printf("Usage: %s <printername>\n", argv[0]);
@@ -156,26 +154,11 @@ static NTSTATUS cmd_spoolss_open_printer_ex(struct cli_state *cli, int argc, cha
        if (!cli)
                return NT_STATUS_UNSUCCESSFUL;
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_open_printer_ex: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-
        slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (servername);
        fstrcpy  (user, cli->user_name);
        fstrcpy  (printername, argv[1]);
 
-               
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Open the printer handle */
        result = cli_spoolss_open_printer_ex (cli, mem_ctx, printername, "", 
                                MAXIMUM_ALLOWED_ACCESS, servername, user, &hnd);
@@ -188,9 +171,6 @@ static NTSTATUS cmd_spoolss_open_printer_ex(struct cli_state *cli, int argc, cha
                }
        }
 
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
@@ -330,14 +310,15 @@ static void display_print_info_3(PRINTER_INFO_3 *i3)
 
 /* Enumerate printers */
 
-static NTSTATUS cmd_spoolss_enum_printers(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_enum_printers(struct cli_state *cli, 
+                                          TALLOC_CTX *mem_ctx,
+                                          int argc, char **argv)
 {
        NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
        uint32                  info_level = 1;
        PRINTER_INFO_CTR        ctr;
        int                     returned;
        uint32                  i = 0;
-       TALLOC_CTX              *mem_ctx;
 
        if (argc > 2) 
        {
@@ -345,24 +326,10 @@ static NTSTATUS cmd_spoolss_enum_printers(struct cli_state *cli, int argc, char
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_enum_printers: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-
        if (argc == 2) {
                info_level = atoi(argv[1]);
        }
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Enumerate printers  -- Should we enumerate types other 
           than PRINTER_ENUM_LOCAL?  Maybe accept as a parameter?  --jerry */
        ZERO_STRUCT(ctr);
@@ -401,9 +368,6 @@ static NTSTATUS cmd_spoolss_enum_printers(struct cli_state *cli, int argc, char
                }
        }
 
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
@@ -440,37 +404,24 @@ static void display_port_info_2(PORT_INFO_2 *i2)
 
 /* Enumerate ports */
 
-static NTSTATUS cmd_spoolss_enum_ports(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_enum_ports(struct cli_state *cli, 
+                                       TALLOC_CTX *mem_ctx,
+                                       int argc, char **argv)
 {
        NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
        uint32                  info_level = 1;
        PORT_INFO_CTR           ctr;
        int                     returned;
-       TALLOC_CTX              *mem_ctx;
        
        if (argc > 2) {
                printf("Usage: %s [level]\n", argv[0]);
                return NT_STATUS_OK;
        }
        
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_enum_ports: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-       
-
        if (argc == 2) {
                info_level = atoi(argv[1]);
        }
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Enumerate ports */
        ZERO_STRUCT(ctr);
 
@@ -494,16 +445,15 @@ static NTSTATUS cmd_spoolss_enum_ports(struct cli_state *cli, int argc, char **a
                }
        }
 
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
 /***********************************************************************
  * Get printer information
  */
-static NTSTATUS cmd_spoolss_getprinter(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_getprinter(struct cli_state *cli, 
+                                       TALLOC_CTX *mem_ctx,
+                                       int argc, char **argv)
 {
        POLICY_HND      pol;
        NTSTATUS        result;
@@ -513,27 +463,12 @@ static NTSTATUS cmd_spoolss_getprinter(struct cli_state *cli, int argc, char **a
        fstring         printername, 
                        servername,
                        user;
-       TALLOC_CTX      *mem_ctx;
 
        if (argc == 1 || argc > 3) {
                printf("Usage: %s <printername> [level]\n", argv[0]);
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_getprinter: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* Open a printer handle */
        if (argc == 3) {
                info_level = atoi(argv[2]);
@@ -584,9 +519,6 @@ static NTSTATUS cmd_spoolss_getprinter(struct cli_state *cli, int argc, char **a
        if (opened_hnd) 
                cli_spoolss_close_printer(cli, mem_ctx, &pol);
 
-       cli_nt_session_close(cli);
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
@@ -703,7 +635,9 @@ static void display_print_driver_3(DRIVER_INFO_3 *i1)
 /***********************************************************************
  * Get printer information
  */
-static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, 
+                                      TALLOC_CTX *mem_ctx,
+                                      int argc, char **argv)
 {
        POLICY_HND      pol;
        NTSTATUS        result;
@@ -714,7 +648,6 @@ static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, int argc, char **ar
                        servername, 
                        user;
        uint32          i;
-       TALLOC_CTX      *mem_ctx;
 
        if ((argc == 1) || (argc > 3)) 
        {
@@ -722,20 +655,6 @@ static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, int argc, char **ar
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_getdriver: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) 
-       {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* get the arguments need to open the printer handle */
        slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (servername);
@@ -783,12 +702,9 @@ static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, int argc, char **ar
                }
        }
        
-
        /* cleanup */
        if (opened_hnd)
                cli_spoolss_close_printer (cli, mem_ctx, &pol);
-       cli_nt_session_close (cli);
-       talloc_destroy(mem_ctx);
        
        return result;
                
@@ -797,7 +713,9 @@ static NTSTATUS cmd_spoolss_getdriver(struct cli_state *cli, int argc, char **ar
 /***********************************************************************
  * Get printer information
  */
-static NTSTATUS cmd_spoolss_enum_drivers(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_enum_drivers(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx,
+                                         int argc, char **argv)
 {
        NTSTATUS        result = NT_STATUS_OK;
        uint32          info_level = 1;
@@ -805,7 +723,6 @@ static NTSTATUS cmd_spoolss_enum_drivers(struct cli_state *cli, int argc, char *
        fstring         servername;
        uint32          i, j,
                        returned;
-       TALLOC_CTX      *mem_ctx;
 
        if (argc > 2) 
        {
@@ -813,20 +730,6 @@ static NTSTATUS cmd_spoolss_enum_drivers(struct cli_state *cli, int argc, char *
                return NT_STATUS_OK;
        }
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_enum_drivers: talloc_init returned NULL!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) 
-       {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* get the arguments need to open the printer handle */
        slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (servername);
@@ -877,11 +780,6 @@ static NTSTATUS cmd_spoolss_enum_drivers(struct cli_state *cli, int argc, char *
                }
        }
        
-
-       /* cleanup */
-       cli_nt_session_close (cli);
-       talloc_destroy(mem_ctx);
-       
        return result;
 }
 
@@ -902,12 +800,13 @@ static void display_printdriverdir_1(DRIVER_DIRECTORY_1 *i1)
 /***********************************************************************
  * Get printer driver directory information
  */
-static NTSTATUS cmd_spoolss_getdriverdir(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_getdriverdir(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx,
+                                         int argc, char **argv)
 {
        NTSTATUS                result;
        fstring                 env;
        DRIVER_DIRECTORY_CTR    ctr;
-       TALLOC_CTX              *mem_ctx;
 
        if (argc > 2) 
        {
@@ -915,20 +814,6 @@ static NTSTATUS cmd_spoolss_getdriverdir(struct cli_state *cli, int argc, char *
                return NT_STATUS_OK;
        }
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) 
-       {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-       
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_getdriverdir: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-
        /* get the arguments need to open the printer handle */
        if (argc == 2)
                fstrcpy (env, argv[1]);
@@ -944,12 +829,7 @@ static NTSTATUS cmd_spoolss_getdriverdir(struct cli_state *cli, int argc, char *
        
        display_printdriverdir_1 (ctr.info1);
 
-       /* cleanup */
-       cli_nt_session_close (cli);
-       talloc_destroy(mem_ctx);
-       
        return result;
-               
 }
 
 /*******************************************************************************
@@ -1060,7 +940,9 @@ static BOOL init_drv_info_3_members (
 }
 
 
-static NTSTATUS cmd_spoolss_addprinterdriver (struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_addprinterdriver(struct cli_state *cli, 
+                                             TALLOC_CTX *mem_ctx,
+                                             int argc, char **argv)
 {
        NTSTATUS                result;
        uint32                  level = 3;
@@ -1068,7 +950,6 @@ static NTSTATUS cmd_spoolss_addprinterdriver (struct cli_state *cli, int argc, c
        DRIVER_INFO_3           info3;
        fstring                 arch;
        fstring                 driver_name;
-       TALLOC_CTX              *mem_ctx = NULL;
 
        /* parse the command arguements */
        if (argc != 3)
@@ -1080,21 +961,6 @@ static NTSTATUS cmd_spoolss_addprinterdriver (struct cli_state *cli, int argc, c
 
                return NT_STATUS_OK;
         }
-       
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_addprinterdriver: talloc_init returned NULL!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) 
-       {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
                
        /* Fill in the DRIVER_INFO_3 struct */
        ZERO_STRUCT(info3);
@@ -1122,23 +988,19 @@ static NTSTATUS cmd_spoolss_addprinterdriver (struct cli_state *cli, int argc, c
        rpcstr_pull(driver_name, info3.name.buffer, sizeof(driver_name), 0, STR_TERMINATE);
        printf ("Printer Driver %s successfully installed.\n", driver_name);
 
-       /* cleanup */
-       cli_nt_session_close (cli);
-       talloc_destroy(mem_ctx);
-       
        return result;
-               
 }
 
 
-static NTSTATUS cmd_spoolss_addprinterex (struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_addprinterex(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx, 
+                                         int argc, char **argv)
 {
        NTSTATUS                result;
        uint32                  level = 2;
        PRINTER_INFO_CTR        ctr;
        PRINTER_INFO_2          info2;
        fstring                 servername;
-       TALLOC_CTX              *mem_ctx = NULL;
        
        /* parse the command arguements */
        if (argc != 5)
@@ -1147,25 +1009,9 @@ static NTSTATUS cmd_spoolss_addprinterex (struct cli_state *cli, int argc, char
                return NT_STATUS_OK;
         }
        
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_addprinterex: talloc_init returned NULL!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-
         slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
         strupper (servername);
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) 
-       {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-               
        /* Fill in the DRIVER_INFO_3 struct */
        ZERO_STRUCT(info2);
 #if 0  /* JERRY */
@@ -1197,21 +1043,17 @@ static NTSTATUS cmd_spoolss_addprinterex (struct cli_state *cli, int argc, char
        ctr.printers_2 = &info2;
        result = cli_spoolss_addprinterex (cli, mem_ctx, level, &ctr);
        if (!NT_STATUS_IS_OK(result)) {
-               cli_nt_session_close (cli);
                return result;
        }
 
        printf ("Printer %s successfully installed.\n", argv[1]);
 
-       /* cleanup */
-       cli_nt_session_close (cli);
-       talloc_destroy(mem_ctx);
-       
        return result;
-               
 }
 
-static NTSTATUS cmd_spoolss_setdriver (struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_setdriver(struct cli_state *cli, 
+                                      TALLOC_CTX *mem_ctx,
+                                      int argc, char **argv)
 {
        POLICY_HND              pol;
        NTSTATUS                result;
@@ -1222,7 +1064,6 @@ static NTSTATUS cmd_spoolss_setdriver (struct cli_state *cli, int argc, char **a
        fstring                 servername,
                                printername,
                                user;
-       TALLOC_CTX              *mem_ctx = NULL;
        
        /* parse the command arguements */
        if (argc != 3)
@@ -1231,26 +1072,11 @@ static NTSTATUS cmd_spoolss_setdriver (struct cli_state *cli, int argc, char **a
                return NT_STATUS_OK;
         }
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_setdriver: talloc_init returned NULL!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
        slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (servername);
        slprintf (printername, sizeof(fstring)-1, "%s\\%s", servername, argv[1]);
        fstrcpy  (user, cli->user_name);
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) 
-       {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-       
-               
        /* get a printer handle */
        result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername, "", 
                                             MAXIMUM_ALLOWED_ACCESS, servername, user, &pol);
@@ -1283,18 +1109,17 @@ done:
        /* cleanup */
        if (opened_hnd)
                cli_spoolss_close_printer(cli, mem_ctx, &pol);
-       cli_nt_session_close (cli);
-       talloc_destroy(mem_ctx);
        
        return result;          
 }
 
 
-static NTSTATUS cmd_spoolss_deletedriver (struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_spoolss_deletedriver(struct cli_state *cli, 
+                                         TALLOC_CTX *mem_ctx,
+                                         int argc, char **argv)
 {
        NTSTATUS                result = NT_STATUS_UNSUCCESSFUL;
        fstring                 servername;
-       TALLOC_CTX              *mem_ctx = NULL;
        int                     i;
        
        /* parse the command arguements */
@@ -1304,23 +1129,9 @@ static NTSTATUS cmd_spoolss_deletedriver (struct cli_state *cli, int argc, char
                return NT_STATUS_OK;
         }
 
-       if (!(mem_ctx=talloc_init()))
-       {
-               DEBUG(0,("cmd_spoolss_deletedriver: talloc_init returned NULL!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        slprintf (servername, sizeof(fstring)-1, "\\\\%s", cli->desthost);
        strupper (servername);
 
-       /* Initialise RPC connection */
-       if (!cli_nt_session_open (cli, PIPE_SPOOLSS)) 
-       {
-               fprintf (stderr, "Could not initialize spoolss pipe!\n");
-               talloc_destroy(mem_ctx);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
        /* delete the driver for all architectures */
        for (i=0; archi_table[i].long_archi; i++)
        {
@@ -1335,11 +1146,6 @@ static NTSTATUS cmd_spoolss_deletedriver (struct cli_state *cli, int argc, char
                        printf ("Driver %s removed for arch [%s].\n", argv[1], archi_table[i].long_archi);
        }
                
-
-       /* cleanup */
-       cli_nt_session_close (cli);
-       talloc_destroy(mem_ctx);
-       
        return NT_STATUS_OK;            
 }
 
@@ -1349,20 +1155,20 @@ struct cmd_set spoolss_commands[] = {
 
        { "SPOOLSS"  },
 
-       { "adddriver",          cmd_spoolss_addprinterdriver,   "Add a print driver",                  "" },
-       { "addprinter",         cmd_spoolss_addprinterex,       "Add a printer",                       "" },
-       { "deldriver",          cmd_spoolss_deletedriver,       "Delete a printer driver",             "" },
-       { "enumdata",           cmd_spoolss_not_implemented,    "Enumerate printer data (*)",          "" },
-       { "enumjobs",           cmd_spoolss_not_implemented,    "Enumerate print jobs (*)",            "" },
-       { "enumports",          cmd_spoolss_enum_ports,         "Enumerate printer ports",             "" },
-       { "enumdrivers",        cmd_spoolss_enum_drivers,       "Enumerate installed printer drivers", "" },
-       { "enumprinters",       cmd_spoolss_enum_printers,      "Enumerate printers",                  "" },
-       { "getdata",            cmd_spoolss_not_implemented,    "Get print driver data (*)",           "" },
-       { "getdriver",          cmd_spoolss_getdriver,          "Get print driver information",        "" },
-       { "getdriverdir",       cmd_spoolss_getdriverdir,       "Get print driver upload directory",   "" },
-       { "getprinter",         cmd_spoolss_getprinter,         "Get printer info",                    "" },
-       { "openprinter",        cmd_spoolss_open_printer_ex,    "Open printer handle",                 "" },
-       { "setdriver",          cmd_spoolss_setdriver,          "Set printer driver",                  "" },
+       { "adddriver",          cmd_spoolss_addprinterdriver,   PIPE_SPOOLSS, "Add a print driver",                  "" },
+       { "addprinter",         cmd_spoolss_addprinterex,       PIPE_SPOOLSS, "Add a printer",                       "" },
+       { "deldriver",          cmd_spoolss_deletedriver,       PIPE_SPOOLSS, "Delete a printer driver",             "" },
+       { "enumdata",           cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Enumerate printer data (*)",          "" },
+       { "enumjobs",           cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Enumerate print jobs (*)",            "" },
+       { "enumports",          cmd_spoolss_enum_ports,         PIPE_SPOOLSS, "Enumerate printer ports",             "" },
+       { "enumdrivers",        cmd_spoolss_enum_drivers,       PIPE_SPOOLSS, "Enumerate installed printer drivers", "" },
+       { "enumprinters",       cmd_spoolss_enum_printers,      PIPE_SPOOLSS, "Enumerate printers",                  "" },
+       { "getdata",            cmd_spoolss_not_implemented,    PIPE_SPOOLSS, "Get print driver data (*)",           "" },
+       { "getdriver",          cmd_spoolss_getdriver,          PIPE_SPOOLSS, "Get print driver information",        "" },
+       { "getdriverdir",       cmd_spoolss_getdriverdir,       PIPE_SPOOLSS, "Get print driver upload directory",   "" },
+       { "getprinter",         cmd_spoolss_getprinter,         PIPE_SPOOLSS, "Get printer info",                    "" },
+       { "openprinter",        cmd_spoolss_open_printer_ex,    PIPE_SPOOLSS, "Open printer handle",                 "" },
+       { "setdriver",          cmd_spoolss_setdriver,          PIPE_SPOOLSS, "Set printer driver",                  "" },
 
        { NULL }
 };
index 9e5b2054ee81b90a0ccbb022adec9be2c08328c0..f4ff11adf1321a520c1b0c8dbd0555f9698115a9 100644 (file)
@@ -23,6 +23,7 @@
 */
 
 #include "includes.h"
+#include "rpcclient.h"
 
 /* Display server query info */
 
@@ -180,12 +181,12 @@ static void display_srv_info_102(SRV_INFO_102 *sv102)
 
 /* Server query info */
 
-static NTSTATUS cmd_srvsvc_srv_query_info(struct cli_state *cli, int argc,
-                                       char **argv)
+static NTSTATUS cmd_srvsvc_srv_query_info(struct cli_state *cli, 
+                                          TALLOC_CTX *mem_ctx,
+                                          int argc, char **argv)
 {
        uint32 info_level = 101;
        SRV_INFO_CTR ctr;
-       TALLOC_CTX *mem_ctx;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
 
        if (argc > 2) {
@@ -196,20 +197,9 @@ static NTSTATUS cmd_srvsvc_srv_query_info(struct cli_state *cli, int argc,
        if (argc == 2)
                info_level = atoi(argv[1]);
 
-       if (!(mem_ctx = talloc_init())) {
-               DEBUG(0,("cmd_srvsvc_srv_query_info: talloc_init failed\n"));
-               return result;
-       }
-
-       /* Initialise RPC connection */
-
-       if (!cli_nt_session_open (cli, PIPE_SRVSVC)) {
-               DEBUG(0, ("Could not initialize srvsvc pipe!\n"));
-               goto done;
-       }
-
        result = cli_srvsvc_net_srv_get_info(cli, mem_ctx, info_level,
                                             &ctr);
+
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
@@ -229,8 +219,6 @@ static NTSTATUS cmd_srvsvc_srv_query_info(struct cli_state *cli, int argc,
        }
 
  done:
-       talloc_destroy(mem_ctx);
-
        return result;
 }
 
@@ -240,7 +228,7 @@ struct cmd_set srvsvc_commands[] = {
 
        { "SRVSVC" },
 
-       { "srvinfo",    cmd_srvsvc_srv_query_info,  "Server query info", "" },
+       { "srvinfo",    cmd_srvsvc_srv_query_info,  PIPE_SRVSVC, "Server query info", "" },
 
        { NULL }
 };
index a5fb42796bc4e3f1bb6ef684708c47dc52ec7684..77bf413723ad82c21e145a92c6d20efd7e927474 100644 (file)
@@ -21,6 +21,7 @@
 */
 
 #include "includes.h"
+#include "rpcclient.h"
 
 extern pstring debugf;
 
@@ -266,7 +267,8 @@ void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
 
 /* Display help on commands */
 
-static NTSTATUS cmd_help(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                         int argc, char **argv)
 {
        struct cmd_list *tmp;
         struct cmd_set *tmp_set;
@@ -324,7 +326,8 @@ static NTSTATUS cmd_help(struct cli_state *cli, int argc, char **argv)
 
 /* Change the debug level */
 
-static NTSTATUS cmd_debuglevel(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                               int argc, char **argv)
 {
        if (argc > 2) {
                printf("Usage: %s [debuglevel]\n", argv[0]);
@@ -340,7 +343,8 @@ static NTSTATUS cmd_debuglevel(struct cli_state *cli, int argc, char **argv)
        return NT_STATUS_OK;
 }
 
-static NTSTATUS cmd_quit(struct cli_state *cli, int argc, char **argv)
+static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+                         int argc, char **argv)
 {
        exit(0);
        return NT_STATUS_OK; /* NOTREACHED */
@@ -389,7 +393,7 @@ static struct cmd_set *rpcclient_command_list[] = {
        NULL
 };
 
-void add_command_set(struct cmd_set *cmd_set)
+static void add_command_set(struct cmd_set *cmd_set)
 {
        struct cmd_list *entry;
 
@@ -404,10 +408,11 @@ void add_command_set(struct cmd_set *cmd_set)
        DLIST_ADD(cmd_list, entry);
 }
 
-static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, char *cmd)
+static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, 
+                       char *cmd)
 {
        char *p = cmd, **argv = NULL;
-       NTSTATUS result;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        pstring buf;
        int argc = 0, i;
 
@@ -428,10 +433,12 @@ static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, char *c
                /* Create argument list */
 
                argv = (char **)malloc(sizeof(char *) * argc);
+                memset(argv, 0, sizeof(char *) * argc);
 
                if (!argv) {
                        fprintf(stderr, "out of memory\n");
-                       return NT_STATUS_NO_MEMORY;
+                       result = NT_STATUS_NO_MEMORY;
+                        goto done;
                }
                                        
                p = cmd;
@@ -441,21 +448,52 @@ static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, char *c
        }
 
        /* Call the function */
+
        if (cmd_entry->fn) {
-               result = cmd_entry->fn(cli, argc, argv);
-       }
-       else {
+                TALLOC_CTX *mem_ctx;
+
+                /* Create mem_ctx */
+
+                if (!(mem_ctx = talloc_init())) {
+                        DEBUG(0, ("talloc_init() failed\n"));
+                        goto done;
+                }
+
+                /* Open pipe */
+
+                if (cmd_entry->pipe)
+                        if (!cli_nt_session_open(cli, cmd_entry->pipe)) {
+                                DEBUG(0, ("Could not initialise %s\n",
+                                          cmd_entry->pipe));
+                                goto done;
+                        }
+
+                /* Run command */
+
+                result = cmd_entry->fn(cli, mem_ctx, argc, argv);
+
+                /* Cleanup */
+
+                if (cmd_entry->pipe)
+                        cli_nt_session_close(cli);
+
+                talloc_destroy(mem_ctx);
+
+       } else {
                fprintf (stderr, "Invalid command\n");
-               result = NT_STATUS_INVALID_PARAMETER;
-       }
+                goto done;
+        }
 
+ done:
                                                
        /* Cleanup */
-       for (i = 0; i < argc; i++) {
-               SAFE_FREE(argv[i]);
-       }
+
+        if (argv) {
+                for (i = 0; i < argc; i++)
+                        SAFE_FREE(argv[i]);
        
-       SAFE_FREE(argv);
+                SAFE_FREE(argv);
+        }
        
        return result;
 }
@@ -490,8 +528,9 @@ static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
 
                while(temp_set->name) {
                        if (strequal(buf, temp_set->name)) {
-                               found = True;
+                                found = True;
                                result = do_cmd(cli, temp_set, cmd);
+
                                goto done;
                        }
                        temp_set++;