s3-rpcclient: Add rpcclient IRemoteWinspool commands
[sfrench/samba-autobuild/.git] / source3 / rpcclient / cmd_iremotewinspool.c
1 /*
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) 2013      Guenther Deschner <gd@samba.org>
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_winspool.h"
24 #include "libsmb/libsmb.h"
25 #include "auth/gensec/gensec.h"
26 #include "auth/credentials/credentials.h"
27
28 /****************************************************************************
29 ****************************************************************************/
30
31 static WERROR cmd_iremotewinspool_async_open_printer(struct rpc_pipe_client *cli,
32                                                      TALLOC_CTX *mem_ctx,
33                                                      int argc, const char **argv)
34 {
35         NTSTATUS status;
36         struct policy_handle hnd;
37         struct spoolss_DevmodeContainer devmode_ctr;
38         struct spoolss_UserLevelCtr client_info_ctr;
39         struct spoolss_UserLevel1 level1;
40         uint32_t access_mask = PRINTER_ALL_ACCESS;
41         struct dcerpc_binding_handle *b = cli->binding_handle;
42         struct GUID uuid;
43         struct winspool_AsyncOpenPrinter r;
44         struct cli_credentials *creds = gensec_get_credentials(cli->auth->auth_ctx);
45
46         if (argc < 2) {
47                 printf("Usage: %s <printername> [access_mask]\n", argv[0]);
48                 return WERR_OK;
49         }
50
51         if (argc >= 3) {
52                 sscanf(argv[2], "%x", &access_mask);
53         }
54
55         status = GUID_from_string(IREMOTEWINSPOOL_OBJECT_GUID, &uuid);
56         if (!NT_STATUS_IS_OK(status)) {
57                 return WERR_NOT_ENOUGH_MEMORY;
58         }
59
60         ZERO_STRUCT(devmode_ctr);
61
62         level1.size     = 40;
63         level1.client   = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name());
64         W_ERROR_HAVE_NO_MEMORY(level1.client);
65         level1.user     = cli_credentials_get_username(creds);
66         level1.build    = 1381;
67         level1.major    = 3;
68         level1.minor    = 0;
69         level1.processor = PROCESSOR_ARCHITECTURE_AMD64;
70
71         client_info_ctr.level = 1;
72         client_info_ctr.user_info.level1 = &level1;
73
74         r.in.pPrinterName       = argv[1];
75         r.in.pDatatype          = "RAW";
76         r.in.pDevModeContainer  = &devmode_ctr;
77         r.in.AccessRequired     = access_mask;
78         r.in.pClientInfo        = &client_info_ctr;
79         r.out.pHandle           = &hnd;
80
81         /* Open the printer handle */
82
83         status = dcerpc_binding_handle_call(b,
84                                             &uuid,
85                                             &ndr_table_iremotewinspool,
86                                             NDR_WINSPOOL_ASYNCOPENPRINTER,
87                                             mem_ctx,
88                                             &r);
89         if (!NT_STATUS_IS_OK(status)) {
90                 return ntstatus_to_werror(status);
91         }
92         if (!W_ERROR_IS_OK(r.out.result)) {
93                 return r.out.result;
94         }
95
96         printf("Printer %s opened successfully\n", argv[1]);
97
98         return WERR_OK;
99 }
100
101 /* List of commands exported by this module */
102 struct cmd_set iremotewinspool_commands[] = {
103
104         { "IRemoteWinspool"  },
105
106         { "winspool_AsyncOpenPrinter", RPC_RTYPE_WERROR, NULL,
107                 cmd_iremotewinspool_async_open_printer,
108                 &ndr_table_iremotewinspool,
109                 NULL, "Open printer handle", "" },
110
111         { NULL }
112 };