5d81a6b847743404ac1f885b8f98d819a49483b6
[ira/wip.git] / source4 / torture / rpc / spoolss_notify.c
1 /*
2    Unix SMB/CIFS implementation.
3    test suite for spoolss rpc notify operations
4
5    Copyright (C) Jelmer Vernooij 2007
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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "torture/rpc/rpc.h"
25 #include "librpc/gen_ndr/ndr_spoolss_c.h"
26 #include "rpc_server/dcerpc_server.h"
27 #include "rpc_server/service_rpc.h"
28 #include "smbd/process_model.h"
29 #include "smb_server/smb_server.h"
30 #include "lib/socket/netif.h"
31 #include "ntvfs/ntvfs.h"
32 #include "param/param.h"
33
34 static NTSTATUS spoolss__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
35 {
36         return NT_STATUS_OK;
37 }
38
39 static void spoolss__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
40 {
41 }
42
43 static NTSTATUS spoolss__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
44 {
45         enum ndr_err_code ndr_err;
46         uint16_t opnum = dce_call->pkt.u.request.opnum;
47
48         dce_call->fault_code = 0;
49
50         if (opnum >= ndr_table_spoolss.num_calls) {
51                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
52                 return NT_STATUS_NET_WRITE_FAULT;
53         }
54
55         *r = talloc_size(mem_ctx, ndr_table_spoolss.calls[opnum].struct_size);
56         NT_STATUS_HAVE_NO_MEMORY(*r);
57
58         /* unravel the NDR for the packet */
59         ndr_err = ndr_table_spoolss.calls[opnum].ndr_pull(pull, NDR_IN, *r);
60         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
61                 dcerpc_log_packet(dce_call->conn->packet_log_dir,
62                                                   &ndr_table_spoolss, opnum, NDR_IN,
63                                   &dce_call->pkt.u.request.stub_and_verifier);
64                 dce_call->fault_code = DCERPC_FAULT_NDR;
65                 return NT_STATUS_NET_WRITE_FAULT;
66         }
67
68         return NT_STATUS_OK;
69 }
70
71 /* Note that received_packets are allocated in talloc_autofree_context(),
72  * because no other context appears to stay around long enough. */
73 static struct received_packet {
74         uint16_t opnum;
75         void *r;
76         struct received_packet *prev, *next;
77 } *received_packets = NULL;
78
79
80 static NTSTATUS spoolss__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
81 {
82         uint16_t opnum = dce_call->pkt.u.request.opnum;
83         struct received_packet *rp;
84
85         rp = talloc_zero(talloc_autofree_context(), struct received_packet);
86         rp->opnum = opnum;
87         rp->r = talloc_reference(rp, r);
88
89         DLIST_ADD_END(received_packets, rp, struct received_packet *);
90
91         switch (opnum) {
92         case 58: {
93                 struct spoolss_ReplyOpenPrinter *r2 = (struct spoolss_ReplyOpenPrinter *)r;
94                 r2->out.result = WERR_OK;
95                 break;
96         }
97
98         default:
99                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
100                 break;
101         }
102
103         if (dce_call->fault_code != 0) {
104                 dcerpc_log_packet(dce_call->conn->packet_log_dir,
105                                                   &ndr_table_spoolss, opnum, NDR_IN,
106                                   &dce_call->pkt.u.request.stub_and_verifier);
107                 return NT_STATUS_NET_WRITE_FAULT;
108         }
109         return NT_STATUS_OK;
110 }
111
112
113 static NTSTATUS spoolss__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
114 {
115         return NT_STATUS_OK;
116 }
117
118
119 static NTSTATUS spoolss__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
120 {
121         enum ndr_err_code ndr_err;
122         uint16_t opnum = dce_call->pkt.u.request.opnum;
123
124         ndr_err = ndr_table_spoolss.calls[opnum].ndr_push(push, NDR_OUT, r);
125         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
126                 dce_call->fault_code = DCERPC_FAULT_NDR;
127                 return NT_STATUS_NET_WRITE_FAULT;
128         }
129
130         return NT_STATUS_OK;
131 }
132
133 const static struct dcesrv_interface notify_test_spoolss_interface = {
134         .name           = "spoolss",
135         .syntax_id  = {{0x12345678,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xab}},1.0},
136         .bind           = spoolss__op_bind,
137         .unbind         = spoolss__op_unbind,
138         .ndr_pull       = spoolss__op_ndr_pull,
139         .dispatch       = spoolss__op_dispatch,
140         .reply          = spoolss__op_reply,
141         .ndr_push       = spoolss__op_ndr_push
142 };
143
144 static bool spoolss__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
145 {
146         if (notify_test_spoolss_interface.syntax_id.if_version == if_version &&
147                 GUID_equal(&notify_test_spoolss_interface.syntax_id.uuid, uuid)) {
148                 memcpy(iface,&notify_test_spoolss_interface, sizeof(*iface));
149                 return true;
150         }
151
152         return false;
153 }
154
155 static bool spoolss__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
156 {
157         if (strcmp(notify_test_spoolss_interface.name, name)==0) {
158                 memcpy(iface, &notify_test_spoolss_interface, sizeof(*iface));
159                 return true;
160         }
161
162         return false;
163 }
164
165 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
166 {
167         int i;
168
169         for (i=0;i<ndr_table_spoolss.endpoints->count;i++) {
170                 NTSTATUS ret;
171                 const char *name = ndr_table_spoolss.endpoints->names[i];
172
173                 ret = dcesrv_interface_register(dce_ctx, name, &notify_test_spoolss_interface, NULL);
174                 if (!NT_STATUS_IS_OK(ret)) {
175                         DEBUG(1,("spoolss_op_init_server: failed to register endpoint '%s'\n",name));
176                         return ret;
177                 }
178         }
179
180         return NT_STATUS_OK;
181 }
182
183 static bool test_RFFPCNEx(struct torture_context *tctx,
184                           struct dcerpc_pipe *p)
185 {
186         struct spoolss_OpenPrinter q;
187         struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
188         struct dcesrv_endpoint_server ep_server;
189         NTSTATUS status;
190         struct dcesrv_context *dce_ctx;
191         const char *endpoints[] = { "spoolss", NULL };
192         struct dcesrv_endpoint *e;
193         struct spoolss_NotifyOption t1;
194         struct spoolss_ClosePrinter cp;
195
196         struct policy_handle handle;
197         const char *address;
198         struct interface *ifaces;
199
200         received_packets = NULL;
201
202         ntvfs_init(tctx->lp_ctx);
203
204         ZERO_STRUCT(q);
205
206         q.in.printername        = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
207         q.in.datatype           = NULL;
208         q.in.devmode_ctr.devmode= NULL;
209         q.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
210         q.out.handle            = &handle;
211
212         torture_comment(tctx, "Testing OpenPrinter(%s)\n", q.in.printername);
213
214         status = dcerpc_spoolss_OpenPrinter(p, tctx, &q);
215
216         torture_assert_ntstatus_ok(tctx, status, "OpenPrinter failed");
217
218         torture_assert_werr_ok(tctx, q.out.result, "OpenPrinter failed");
219
220         /* Start DCE/RPC server */
221
222         /* fill in our name */
223         ep_server.name = "spoolss";
224
225         /* fill in all the operations */
226         ep_server.init_server = spoolss__op_init_server;
227
228         ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
229         ep_server.interface_by_name = spoolss__op_interface_by_name;
230
231         torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
232                                   "unable to register spoolss server");
233
234         lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
235
236         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
237         address = iface_n_ip(ifaces, 0);
238         torture_comment(tctx, "Listening for callbacks on %s\n", address);
239         status = smbsrv_add_socket(p->conn->event_ctx, tctx->lp_ctx, &single_ops, address);
240         torture_assert_ntstatus_ok(tctx, status, "starting smb server");
241
242         status = dcesrv_init_context(tctx, tctx->lp_ctx, endpoints, &dce_ctx);
243         torture_assert_ntstatus_ok(tctx, status,
244                                    "unable to initialize DCE/RPC server");
245
246         /* Make sure the directory for NCALRPC exists */
247         if (!directory_exist(lp_ncalrpc_dir(tctx->lp_ctx))) {
248                 int ret;
249                 ret = mkdir(lp_ncalrpc_dir(tctx->lp_ctx), 0755);
250                 torture_assert(tctx, (ret == 0), talloc_asprintf(tctx,
251                                "failed to mkdir(%s) ret[%d] errno[%d - %s]",
252                                lp_ncalrpc_dir(tctx->lp_ctx), ret,
253                                errno, strerror(errno)));
254         }
255
256         for (e=dce_ctx->endpoint_list;e;e=e->next) {
257                 status = dcesrv_add_ep(dce_ctx, tctx->lp_ctx,
258                                        e, tctx->ev, &single_ops);
259                 torture_assert_ntstatus_ok(tctx, status,
260                                 "unable listen on dcerpc endpoint server");
261         }
262
263         r.in.flags = 0;
264         r.in.local_machine = talloc_asprintf(tctx, "\\\\%s", address);
265         r.in.options = 0;
266         r.in.printer_local = 123;
267         t1.version = 2;
268         t1.flags = 0;
269         t1.count = 2;
270         t1.types = talloc_zero_array(tctx, struct spoolss_NotifyOptionType, 2);
271         t1.types[0].type = PRINTER_NOTIFY_TYPE;
272         t1.types[0].count = 1;
273         t1.types[0].fields = talloc_array(t1.types, union spoolss_Field, 1);
274         t1.types[0].fields[0].field = PRINTER_NOTIFY_FIELD_SERVER_NAME;
275
276         t1.types[1].type = JOB_NOTIFY_TYPE;
277         t1.types[1].count = 1;
278         t1.types[1].fields = talloc_array(t1.types, union spoolss_Field, 1);
279         t1.types[1].fields[0].field = PRINTER_NOTIFY_FIELD_PRINTER_NAME;
280
281         r.in.notify_options = &t1;
282         r.in.handle = &handle;
283
284         status = dcerpc_spoolss_RemoteFindFirstPrinterChangeNotifyEx(p, tctx, &r);
285
286         torture_assert_ntstatus_ok(tctx, status, "FFPCNEx failed");
287
288         torture_assert_werr_ok(tctx, r.out.result, "error return code for FFPCNEx");
289
290         cp.in.handle = &handle;
291         cp.out.handle = &handle;
292
293         torture_comment(tctx, "Testing ClosePrinter\n");
294
295         status = dcerpc_spoolss_ClosePrinter(p, tctx, &cp);
296         torture_assert_ntstatus_ok(tctx, status, "ClosePrinter failed");
297
298         /* We should've had an incoming packet 58 (ReplyOpenPrinter) */
299         torture_assert(tctx, received_packets != NULL, "no packets received");
300         torture_assert_int_equal(tctx, received_packets->opnum, 58, "invalid opnum");
301
302         /* Shut down DCE/RPC server */
303         talloc_free(dce_ctx);
304
305         return true;
306 }
307
308 /** Test that makes sure that calling ReplyOpenPrinter()
309  * on Samba 4 will cause an irpc broadcast call.
310  */
311 static bool test_ReplyOpenPrinter(struct torture_context *tctx,
312                                   struct dcerpc_pipe *p)
313 {
314         struct spoolss_ReplyOpenPrinter r;
315         struct spoolss_ReplyClosePrinter s;
316         struct policy_handle h;
317
318         r.in.server_name = "earth";
319         r.in.printer_local = 2;
320         r.in.type = REG_DWORD;
321         r.in.bufsize = 0;
322         r.in.buffer = NULL;
323         r.out.handle = &h;
324
325         torture_assert_ntstatus_ok(tctx,
326                         dcerpc_spoolss_ReplyOpenPrinter(p, tctx, &r),
327                         "spoolss_ReplyOpenPrinter call failed");
328
329         torture_assert_werr_ok(tctx, r.out.result, "error return code");
330
331         s.in.handle = &h;
332         s.out.handle = &h;
333
334         torture_assert_ntstatus_ok(tctx,
335                         dcerpc_spoolss_ReplyClosePrinter(p, tctx, &s),
336                         "spoolss_ReplyClosePrinter call failed");
337
338         torture_assert_werr_ok(tctx, r.out.result, "error return code");
339
340         return true;
341 }
342
343 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
344 {
345         struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
346
347         struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite,
348                                                         "notify", &ndr_table_spoolss);
349
350         torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
351         torture_rpc_tcase_add_test(tcase, "testReplyOpenPrinter", test_ReplyOpenPrinter);
352
353         return suite;
354 }