s3-talloc Change TALLOC_REALLOC_ARRAY() to talloc_realloc()
[kai/samba.git] / source3 / rpcclient / cmd_epmapper.c
1 /*
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Volker Lendecke 2009
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "rpcclient.h"
23 #include "../librpc/gen_ndr/ndr_epmapper_c.h"
24 #include "../librpc/gen_ndr/ndr_lsa.h"
25
26 static NTSTATUS cmd_epmapper_map(struct rpc_pipe_client *p,
27                                  TALLOC_CTX *mem_ctx,
28                                  int argc, const char **argv)
29 {
30         struct dcerpc_binding_handle *b = p->binding_handle;
31         struct dcerpc_binding map_binding;
32         struct epm_twr_t map_tower;
33         struct epm_twr_p_t towers[500];
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;
39         uint32_t result;
40         uint32_t i;
41
42         abstract_syntax = ndr_table_lsarpc.syntax_id;
43
44         map_binding.transport = NCACN_NP;
45         map_binding.object = abstract_syntax;
46         map_binding.host = "127.0.0.1"; /* needed? */
47         map_binding.endpoint = "0"; /* correct? needed? */
48
49         status = dcerpc_binding_build_tower(tmp_ctx, &map_binding,
50                                             &map_tower.tower);
51         if (!NT_STATUS_IS_OK(status)) {
52                 d_fprintf(stderr, "dcerpc_binding_build_tower returned %s\n",
53                           nt_errstr(status));
54                 return status;
55         }
56
57         ZERO_STRUCT(towers);
58         ZERO_STRUCT(entry_handle);
59
60         status = dcerpc_epm_Map(
61                 b, tmp_ctx, &abstract_syntax.uuid,
62                 &map_tower, &entry_handle, ARRAY_SIZE(towers),
63                 &num_towers, towers, &result);
64         if (!NT_STATUS_IS_OK(status)) {
65                 d_fprintf(stderr, "dcerpc_epm_Map returned %s\n",
66                           nt_errstr(status));
67                 return status;
68         }
69
70         if (result != EPMAPPER_STATUS_OK) {
71                 d_fprintf(stderr, "epm_Map returned %u (0x%08X)\n",
72                           result, result);
73                 return NT_STATUS_UNSUCCESSFUL;
74         }
75
76         d_printf("num_tower[%u]\n", num_towers);
77
78         for (i=0; i < num_towers; i++) {
79                 struct dcerpc_binding *binding;
80
81                 if (towers[i].twr == NULL) {
82                         d_fprintf(stderr, "tower[%u] NULL\n", i);
83                         break;
84                 }
85
86                 status = dcerpc_binding_from_tower(tmp_ctx, &towers[i].twr->tower,
87                                                    &binding);
88                 if (!NT_STATUS_IS_OK(status)) {
89                         break;
90                 }
91
92                 d_printf("tower[%u] %s\n", i, dcerpc_binding_string(tmp_ctx, binding));
93         }
94
95         return NT_STATUS_OK;
96 }
97
98 static NTSTATUS cmd_epmapper_lookup(struct rpc_pipe_client *p,
99                                     TALLOC_CTX *mem_ctx,
100                                     int argc, const char **argv)
101 {
102         struct dcerpc_binding_handle *b = p->binding_handle;
103         struct policy_handle entry_handle;
104
105         ZERO_STRUCT(entry_handle);
106
107         while (true) {
108                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
109                 uint32_t num_entries;
110                 struct epm_entry_t entry;
111                 NTSTATUS status;
112                 char *guid_string;
113                 struct dcerpc_binding *binding;
114                 uint32_t result;
115
116                 status = dcerpc_epm_Lookup(b, tmp_ctx,
117                                    0, /* rpc_c_ep_all */
118                                    NULL,
119                                    NULL,
120                                    0, /* rpc_c_vers_all */
121                                    &entry_handle,
122                                    1, /* max_ents */
123                                    &num_entries, &entry,
124                                    &result);
125                 if (!NT_STATUS_IS_OK(status)) {
126                         d_fprintf(stderr, "dcerpc_epm_Lookup returned %s\n",
127                                   nt_errstr(status));
128                         break;
129                 }
130
131                 if (result == EPMAPPER_STATUS_NO_MORE_ENTRIES) {
132                         d_fprintf(stderr, "epm_Lookup no more entries\n");
133                         break;
134                 }
135
136                 if (result != EPMAPPER_STATUS_OK) {
137                         d_fprintf(stderr, "epm_Lookup returned %u (0x%08X)\n",
138                                   result, result);
139                         break;
140                 }
141
142                 if (num_entries != 1) {
143                         d_fprintf(stderr, "epm_Lookup returned %d "
144                                   "entries, expected one\n", (int)num_entries);
145                         break;
146                 }
147
148                 guid_string = GUID_string(tmp_ctx, &entry.object);
149                 if (guid_string == NULL) {
150                         break;
151                 }
152
153                 status = dcerpc_binding_from_tower(tmp_ctx, &entry.tower->tower,
154                                                    &binding);
155                 if (!NT_STATUS_IS_OK(status)) {
156                         break;
157                 }
158
159                 d_printf("%s %s: %s\n", guid_string,
160                          dcerpc_binding_string(tmp_ctx, binding),
161                          entry.annotation);
162
163                 TALLOC_FREE(tmp_ctx);
164         }
165
166         return NT_STATUS_OK;
167 }
168
169
170 /* List of commands exported by this module */
171
172 struct cmd_set epmapper_commands[] = {
173
174         { "EPMAPPER" },
175
176         { "epmmap", RPC_RTYPE_NTSTATUS, cmd_epmapper_map,     NULL,
177           &ndr_table_epmapper.syntax_id, NULL, "Map a binding", "" },
178         { "epmlookup", RPC_RTYPE_NTSTATUS, cmd_epmapper_lookup,     NULL,
179           &ndr_table_epmapper.syntax_id, NULL, "Lookup bindings", "" },
180         { NULL }
181 };