Fix crash when get_gpo_info returns incorrect data.
[Samba.git] / source3 / rpcclient / cmd_epmapper.c
blob3ee012083836a6d02f5932b5e8d62325fc541ad8
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Volker Lendecke 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "rpcclient.h"
23 #include "../librpc/gen_ndr/cli_epmapper.h"
24 #include "../librpc/gen_ndr/ndr_lsa.h"
26 static NTSTATUS cmd_epmapper_map(struct rpc_pipe_client *p,
27 TALLOC_CTX *mem_ctx,
28 int argc, const char **argv)
30 struct dcerpc_binding map_binding;
31 struct epm_twr_t map_tower;
32 struct epm_twr_t res_tower;
33 struct epm_twr_p_t towers;
34 struct policy_handle entry_handle;
35 struct ndr_syntax_id abstract_syntax;
36 uint32_t num_towers;
37 TALLOC_CTX *tmp_ctx = talloc_stackframe();
38 NTSTATUS status;
40 abstract_syntax = ndr_table_lsarpc.syntax_id;
42 map_binding.transport = NCACN_NP;
43 map_binding.object = abstract_syntax;
44 map_binding.host = "127.0.0.1"; /* needed? */
45 map_binding.endpoint = "0"; /* correct? needed? */
47 status = dcerpc_binding_build_tower(tmp_ctx, &map_binding,
48 &map_tower.tower);
49 if (!NT_STATUS_IS_OK(status)) {
50 d_fprintf(stderr, "dcerpc_binding_build_tower returned %s\n",
51 nt_errstr(status));
52 return status;
55 towers.twr = &res_tower;
57 ZERO_STRUCT(entry_handle);
58 status = rpccli_epm_Map(
59 p, tmp_ctx, &abstract_syntax.uuid,
60 &map_tower, &entry_handle, 1,
61 &num_towers, &towers);
63 return status;
66 static NTSTATUS cmd_epmapper_lookup(struct rpc_pipe_client *p,
67 TALLOC_CTX *mem_ctx,
68 int argc, const char **argv)
70 struct policy_handle entry_handle;
72 ZERO_STRUCT(entry_handle);
74 while (true) {
75 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
76 uint32_t num_entries;
77 struct epm_entry_t entry;
78 NTSTATUS status;
79 char *guid_string;
80 struct dcerpc_binding *binding;
82 status = rpccli_epm_Lookup(p, tmp_ctx,
83 0, /* rpc_c_ep_all */
84 NULL,
85 NULL,
86 0, /* rpc_c_vers_all */
87 &entry_handle,
88 1, /* max_ents */
89 &num_entries, &entry);
90 if (!NT_STATUS_IS_OK(status)) {
91 d_fprintf(stderr, "rpccli_epm_Lookup returned %s\n",
92 nt_errstr(status));
93 break;
96 if (num_entries != 1) {
97 d_fprintf(stderr, "rpccli_epm_Lookup returned %d "
98 "entries, expected one\n", (int)num_entries);
99 break;
102 guid_string = GUID_string(tmp_ctx, &entry.object);
103 if (guid_string == NULL) {
104 break;
107 status = dcerpc_binding_from_tower(tmp_ctx, &entry.tower->tower,
108 &binding);
109 if (!NT_STATUS_IS_OK(status)) {
110 break;
113 d_printf("%s %s: %s\n", guid_string,
114 dcerpc_binding_string(tmp_ctx, binding),
115 entry.annotation);
117 TALLOC_FREE(tmp_ctx);
120 return NT_STATUS_OK;
124 /* List of commands exported by this module */
126 struct cmd_set epmapper_commands[] = {
128 { "EPMAPPER" },
130 { "epmmap", RPC_RTYPE_NTSTATUS, cmd_epmapper_map, NULL,
131 &ndr_table_epmapper.syntax_id, NULL, "Map a binding", "" },
132 { "epmlookup", RPC_RTYPE_NTSTATUS, cmd_epmapper_lookup, NULL,
133 &ndr_table_epmapper.syntax_id, NULL, "Lookup bindings", "" },
134 { NULL }