From 439c7e0ca2ceb866cac4f0fbed65e157aa12482d Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 12 Oct 2001 05:56:23 +0000 Subject: Some old stuff hanging around since the CIFS conference. Big cleanup of 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/rpcclient/cmd_lsarpc.c | 182 +++++++++-------------------------------- 1 file changed, 39 insertions(+), 143 deletions(-) (limited to 'source3/rpcclient/cmd_lsarpc.c') diff --git a/source3/rpcclient/cmd_lsarpc.c b/source3/rpcclient/cmd_lsarpc.c index 273662b6f9..8b3e49051e 100644 --- a/source3/rpcclient/cmd_lsarpc.c +++ b/source3/rpcclient/cmd_lsarpc.c @@ -21,124 +21,85 @@ */ #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 } }; -- cgit