s3-spoolss: fix rpcclient after setprinter IDL fixes.
[ira/wip.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/cli_epmapper.h"
24
25 static NTSTATUS cmd_epmapper_map(struct rpc_pipe_client *p,
26                                  TALLOC_CTX *mem_ctx,
27                                  int argc, const char **argv)
28 {
29         struct dcerpc_binding map_binding;
30         struct epm_twr_t map_tower;
31         struct epm_twr_t res_tower;
32         struct epm_twr_p_t towers;
33         struct policy_handle entry_handle;
34         struct ndr_syntax_id abstract_syntax;
35         uint32_t num_towers;
36         TALLOC_CTX *tmp_ctx = talloc_stackframe();
37         NTSTATUS status;
38
39         abstract_syntax = ndr_table_lsarpc.syntax_id;
40
41         map_binding.transport = NCACN_NP;
42         map_binding.object = abstract_syntax;
43         map_binding.host = "127.0.0.1"; /* needed? */
44         map_binding.endpoint = "0"; /* correct? needed? */
45
46         status = dcerpc_binding_build_tower(tmp_ctx, &map_binding,
47                                             &map_tower.tower);
48         if (!NT_STATUS_IS_OK(status)) {
49                 d_fprintf(stderr, "dcerpc_binding_build_tower returned %s\n",
50                           nt_errstr(status));
51                 return status;
52         }
53
54         towers.twr = &res_tower;
55
56         ZERO_STRUCT(entry_handle);
57         status = rpccli_epm_Map(
58                 p, tmp_ctx, &abstract_syntax.uuid,
59                 &map_tower, &entry_handle, 1,
60                 &num_towers, &towers);
61
62         return status;
63 }
64
65 static NTSTATUS cmd_epmapper_lookup(struct rpc_pipe_client *p,
66                                     TALLOC_CTX *mem_ctx,
67                                     int argc, const char **argv)
68 {
69         struct policy_handle entry_handle;
70
71         ZERO_STRUCT(entry_handle);
72
73         while (true) {
74                 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
75                 uint32_t num_entries;
76                 struct epm_entry_t entry;
77                 NTSTATUS status;
78                 char *guid_string;
79                 struct dcerpc_binding *binding;
80
81                 status = rpccli_epm_Lookup(p, tmp_ctx,
82                                    0, /* rpc_c_ep_all */
83                                    NULL,
84                                    NULL,
85                                    0, /* rpc_c_vers_all */
86                                    &entry_handle,
87                                    1, /* max_ents */
88                                    &num_entries, &entry);
89                 if (!NT_STATUS_IS_OK(status)) {
90                         d_fprintf(stderr, "rpccli_epm_Lookup returned %s\n",
91                                   nt_errstr(status));
92                         break;
93                 }
94
95                 if (num_entries != 1) {
96                         d_fprintf(stderr, "rpccli_epm_Lookup returned %d "
97                                   "entries, expected one\n", (int)num_entries);
98                         break;
99                 }
100
101                 guid_string = GUID_string(tmp_ctx, &entry.object);
102                 if (guid_string == NULL) {
103                         break;
104                 }
105
106                 status = dcerpc_binding_from_tower(tmp_ctx, &entry.tower->tower,
107                                                    &binding);
108                 if (!NT_STATUS_IS_OK(status)) {
109                         break;
110                 }
111
112                 d_printf("%s %s: %s\n", guid_string,
113                          dcerpc_binding_string(tmp_ctx, binding),
114                          entry.annotation);
115
116                 TALLOC_FREE(tmp_ctx);
117         }
118
119         return NT_STATUS_OK;
120 }
121
122
123 /* List of commands exported by this module */
124
125 struct cmd_set epmapper_commands[] = {
126
127         { "EPMAPPER" },
128
129         { "epmmap", RPC_RTYPE_NTSTATUS, cmd_epmapper_map,     NULL,
130           &ndr_table_epmapper.syntax_id, NULL, "Map a binding", "" },
131         { "epmlookup", RPC_RTYPE_NTSTATUS, cmd_epmapper_lookup,     NULL,
132           &ndr_table_epmapper.syntax_id, NULL, "Lookup bindings", "" },
133         { NULL }
134 };