dc324861d051ee5026c637970535b8be79f41744
[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    Copyright (C) Guenther Deschner 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "torture/rpc/rpc.h"
26 #include "librpc/gen_ndr/ndr_spoolss_c.h"
27 #include "librpc/gen_ndr/ndr_spoolss.h"
28 #include "rpc_server/dcerpc_server.h"
29 #include "rpc_server/service_rpc.h"
30 #include "smbd/process_model.h"
31 #include "smb_server/smb_server.h"
32 #include "lib/socket/netif.h"
33 #include "ntvfs/ntvfs.h"
34 #include "param/param.h"
35
36 static NTSTATUS spoolss__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
37 {
38         return NT_STATUS_OK;
39 }
40
41 static void spoolss__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
42 {
43 }
44
45 static NTSTATUS spoolss__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
46 {
47         enum ndr_err_code ndr_err;
48         uint16_t opnum = dce_call->pkt.u.request.opnum;
49
50         dce_call->fault_code = 0;
51
52         if (opnum >= ndr_table_spoolss.num_calls) {
53                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
54                 return NT_STATUS_NET_WRITE_FAULT;
55         }
56
57         *r = talloc_size(mem_ctx, ndr_table_spoolss.calls[opnum].struct_size);
58         NT_STATUS_HAVE_NO_MEMORY(*r);
59
60         /* unravel the NDR for the packet */
61         ndr_err = ndr_table_spoolss.calls[opnum].ndr_pull(pull, NDR_IN, *r);
62         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
63                 dcerpc_log_packet(dce_call->conn->packet_log_dir,
64                                                   &ndr_table_spoolss, opnum, NDR_IN,
65                                   &dce_call->pkt.u.request.stub_and_verifier);
66                 dce_call->fault_code = DCERPC_FAULT_NDR;
67                 return NT_STATUS_NET_WRITE_FAULT;
68         }
69
70         return NT_STATUS_OK;
71 }
72
73 /* Note that received_packets are allocated in talloc_autofree_context(),
74  * because no other context appears to stay around long enough. */
75 static struct received_packet {
76         uint16_t opnum;
77         void *r;
78         struct received_packet *prev, *next;
79 } *received_packets = NULL;
80
81 static WERROR _spoolss_ReplyOpenPrinter(struct dcesrv_call_state *dce_call,
82                                         TALLOC_CTX *mem_ctx,
83                                         struct spoolss_ReplyOpenPrinter *r)
84 {
85         DEBUG(1,("_spoolss_ReplyOpenPrinter\n"));
86
87         NDR_PRINT_IN_DEBUG(spoolss_ReplyOpenPrinter, r);
88
89         r->out.handle = talloc(r, struct policy_handle);
90         r->out.handle->handle_type = 42;
91         r->out.handle->uuid = GUID_random();
92         r->out.result = WERR_OK;
93
94         NDR_PRINT_OUT_DEBUG(spoolss_ReplyOpenPrinter, r);
95
96         return WERR_OK;
97 }
98
99 static WERROR _spoolss_ReplyClosePrinter(struct dcesrv_call_state *dce_call,
100                                          TALLOC_CTX *mem_ctx,
101                                          struct spoolss_ReplyClosePrinter *r)
102 {
103         DEBUG(1,("_spoolss_ReplyClosePrinter\n"));
104
105         NDR_PRINT_IN_DEBUG(spoolss_ReplyClosePrinter, r);
106
107         ZERO_STRUCTP(r->out.handle);
108         r->out.result = WERR_OK;
109
110         NDR_PRINT_OUT_DEBUG(spoolss_ReplyClosePrinter, r);
111
112         return WERR_OK;
113 }
114
115 static NTSTATUS spoolss__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
116 {
117         uint16_t opnum = dce_call->pkt.u.request.opnum;
118         struct received_packet *rp;
119
120         rp = talloc_zero(talloc_autofree_context(), struct received_packet);
121         rp->opnum = opnum;
122         rp->r = talloc_reference(rp, r);
123
124         DLIST_ADD_END(received_packets, rp, struct received_packet *);
125
126         switch (opnum) {
127         case 58: {
128                 struct spoolss_ReplyOpenPrinter *r2 = (struct spoolss_ReplyOpenPrinter *)r;
129                 r2->out.result = _spoolss_ReplyOpenPrinter(dce_call, mem_ctx, r2);
130                 break;
131         }
132         case 60: {
133                 struct spoolss_ReplyClosePrinter *r2 = (struct spoolss_ReplyClosePrinter *)r;
134                 r2->out.result = _spoolss_ReplyClosePrinter(dce_call, mem_ctx, r2);
135                 break;
136         }
137
138         default:
139                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
140                 break;
141         }
142
143         if (dce_call->fault_code != 0) {
144                 dcerpc_log_packet(dce_call->conn->packet_log_dir,
145                                                   &ndr_table_spoolss, opnum, NDR_IN,
146                                   &dce_call->pkt.u.request.stub_and_verifier);
147                 return NT_STATUS_NET_WRITE_FAULT;
148         }
149         return NT_STATUS_OK;
150 }
151
152
153 static NTSTATUS spoolss__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
154 {
155         return NT_STATUS_OK;
156 }
157
158
159 static NTSTATUS spoolss__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
160 {
161         enum ndr_err_code ndr_err;
162         uint16_t opnum = dce_call->pkt.u.request.opnum;
163
164         ndr_err = ndr_table_spoolss.calls[opnum].ndr_push(push, NDR_OUT, r);
165         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
166                 dce_call->fault_code = DCERPC_FAULT_NDR;
167                 return NT_STATUS_NET_WRITE_FAULT;
168         }
169
170         return NT_STATUS_OK;
171 }
172
173 const static struct dcesrv_interface notify_test_spoolss_interface = {
174         .name           = "spoolss",
175         .syntax_id  = {{0x12345678,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xab}},1.0},
176         .bind           = spoolss__op_bind,
177         .unbind         = spoolss__op_unbind,
178         .ndr_pull       = spoolss__op_ndr_pull,
179         .dispatch       = spoolss__op_dispatch,
180         .reply          = spoolss__op_reply,
181         .ndr_push       = spoolss__op_ndr_push
182 };
183
184 static bool spoolss__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
185 {
186         if (notify_test_spoolss_interface.syntax_id.if_version == if_version &&
187                 GUID_equal(&notify_test_spoolss_interface.syntax_id.uuid, uuid)) {
188                 memcpy(iface,&notify_test_spoolss_interface, sizeof(*iface));
189                 return true;
190         }
191
192         return false;
193 }
194
195 static bool spoolss__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
196 {
197         if (strcmp(notify_test_spoolss_interface.name, name)==0) {
198                 memcpy(iface, &notify_test_spoolss_interface, sizeof(*iface));
199                 return true;
200         }
201
202         return false;
203 }
204
205 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
206 {
207         int i;
208
209         for (i=0;i<ndr_table_spoolss.endpoints->count;i++) {
210                 NTSTATUS ret;
211                 const char *name = ndr_table_spoolss.endpoints->names[i];
212
213                 ret = dcesrv_interface_register(dce_ctx, name, &notify_test_spoolss_interface, NULL);
214                 if (!NT_STATUS_IS_OK(ret)) {
215                         DEBUG(1,("spoolss_op_init_server: failed to register endpoint '%s'\n",name));
216                         return ret;
217                 }
218         }
219
220         return NT_STATUS_OK;
221 }
222
223 static bool test_OpenPrinter(struct torture_context *tctx,
224                              struct dcerpc_pipe *p,
225                              struct policy_handle *handle)
226 {
227         struct spoolss_OpenPrinter r;
228
229         ZERO_STRUCT(r);
230
231         r.in.printername        = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
232         r.in.datatype           = NULL;
233         r.in.devmode_ctr.devmode= NULL;
234         r.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
235         r.out.handle            = handle;
236
237         torture_comment(tctx, "Testing OpenPrinter(%s)\n", r.in.printername);
238
239         torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_OpenPrinter(p, tctx, &r), "OpenPrinter failed");
240         torture_assert_werr_ok(tctx, r.out.result, "OpenPrinter failed");
241
242         return true;
243 }
244
245 static struct spoolss_NotifyOption *setup_printserver_NotifyOption(struct torture_context *tctx)
246 {
247         struct spoolss_NotifyOption *o;
248
249         o = talloc_zero(tctx, struct spoolss_NotifyOption);
250
251         o->version = 2;
252         o->flags = PRINTER_NOTIFY_OPTIONS_REFRESH;
253
254         o->count = 2;
255         o->types = talloc_zero_array(o, struct spoolss_NotifyOptionType, o->count);
256
257         o->types[0].type = PRINTER_NOTIFY_TYPE;
258         o->types[0].count = 1;
259         o->types[0].fields = talloc_array(o->types, union spoolss_Field, o->types[0].count);
260         o->types[0].fields[0].field = PRINTER_NOTIFY_FIELD_SERVER_NAME;
261
262         o->types[1].type = JOB_NOTIFY_TYPE;
263         o->types[1].count = 1;
264         o->types[1].fields = talloc_array(o->types, union spoolss_Field, o->types[1].count);
265         o->types[1].fields[0].field = JOB_NOTIFY_FIELD_MACHINE_NAME;
266
267         return o;
268 }
269
270 static bool test_RemoteFindFirstPrinterChangeNotifyEx(struct torture_context *tctx,
271                                                       struct dcerpc_pipe *p,
272                                                       struct policy_handle *handle,
273                                                       const char *address,
274                                                       struct spoolss_NotifyOption *option)
275 {
276         struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
277
278         torture_comment(tctx, "Testing RemoteFindFirstPrinterChangeNotifyEx\n");
279
280         r.in.flags = 0;
281         r.in.local_machine = talloc_asprintf(tctx, "\\\\%s", address);
282         r.in.options = 0;
283         r.in.printer_local = 0;
284         r.in.notify_options = option;
285         r.in.handle = handle;
286
287         torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_RemoteFindFirstPrinterChangeNotifyEx(p, tctx, &r),
288                 "RemoteFindFirstPrinterChangeNotifyEx failed");
289         torture_assert_werr_ok(tctx, r.out.result,
290                 "error return code for RemoteFindFirstPrinterChangeNotifyEx");
291
292         return true;
293 }
294
295 static bool test_RouterRefreshPrinterChangeNotify(struct torture_context *tctx,
296                                                   struct dcerpc_pipe *p,
297                                                   struct policy_handle *handle,
298                                                   struct spoolss_NotifyOption *options)
299 {
300         struct spoolss_RouterRefreshPrinterChangeNotify r;
301         struct spoolss_NotifyInfo *info;
302
303         torture_comment(tctx, "Testing RouterRefreshPrinterChangeNotify\n");
304
305         r.in.handle = handle;
306         r.in.change_low = 0;
307         r.in.options = options;
308         r.out.info = &info;
309
310         torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_RouterRefreshPrinterChangeNotify(p, tctx, &r),
311                 "RouterRefreshPrinterChangeNotify failed");
312         torture_assert_werr_ok(tctx, r.out.result,
313                 "error return code for RouterRefreshPrinterChangeNotify");
314
315         return true;
316 }
317
318 static bool test_ClosePrinter(struct torture_context *tctx,
319                               struct dcerpc_pipe *p,
320                               struct policy_handle *handle)
321 {
322         struct spoolss_ClosePrinter r;
323
324         r.in.handle = handle;
325         r.out.handle = handle;
326
327         torture_comment(tctx, "Testing ClosePrinter\n");
328
329         torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_ClosePrinter(p, tctx, &r),
330                 "ClosePrinter failed");
331         torture_assert_werr_ok(tctx, r.out.result,
332                 "ClosePrinter failed");
333
334         return true;
335 }
336
337 static bool test_start_dcerpc_server(struct torture_context *tctx,
338                                      struct tevent_context *event_ctx,
339                                      struct dcesrv_context **dce_ctx_p,
340                                      const char **address_p)
341 {
342         struct dcesrv_endpoint_server ep_server;
343         NTSTATUS status;
344         struct dcesrv_context *dce_ctx;
345         const char *endpoints[] = { "spoolss", NULL };
346         struct dcesrv_endpoint *e;
347         const char *address;
348         struct interface *ifaces;
349
350         ntvfs_init(tctx->lp_ctx);
351
352         /* fill in our name */
353         ep_server.name = "spoolss";
354
355         /* fill in all the operations */
356         ep_server.init_server = spoolss__op_init_server;
357
358         ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
359         ep_server.interface_by_name = spoolss__op_interface_by_name;
360
361         torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
362                                   "unable to register spoolss server");
363
364         lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
365
366         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
367         address = iface_n_ip(ifaces, 0);
368
369         torture_comment(tctx, "Listening for callbacks on %s\n", address);
370
371         status = smbsrv_add_socket(event_ctx, tctx->lp_ctx, &single_ops, address);
372         torture_assert_ntstatus_ok(tctx, status, "starting smb server");
373
374         status = dcesrv_init_context(tctx, tctx->lp_ctx, endpoints, &dce_ctx);
375         torture_assert_ntstatus_ok(tctx, status,
376                                    "unable to initialize DCE/RPC server");
377
378         for (e=dce_ctx->endpoint_list;e;e=e->next) {
379                 status = dcesrv_add_ep(dce_ctx, tctx->lp_ctx,
380                                        e, tctx->ev, &single_ops);
381                 torture_assert_ntstatus_ok(tctx, status,
382                                 "unable listen on dcerpc endpoint server");
383         }
384
385         *dce_ctx_p = dce_ctx;
386         *address_p = address;
387
388         return true;
389 }
390
391 static struct received_packet *last_packet(struct received_packet *p)
392 {
393         struct received_packet *tmp;
394         for (tmp = p; tmp->next; tmp = tmp->next) ;;
395         return tmp;
396 }
397
398 static bool test_RFFPCNEx(struct torture_context *tctx,
399                           struct dcerpc_pipe *p)
400 {
401         struct dcesrv_context *dce_ctx;
402         struct policy_handle handle;
403         const char *address;
404         struct received_packet *tmp;
405         struct spoolss_NotifyOption *server_option = setup_printserver_NotifyOption(tctx);
406
407         received_packets = NULL;
408
409         /* Start DCE/RPC server */
410         torture_assert(tctx, test_start_dcerpc_server(tctx, p->conn->event_ctx, &dce_ctx, &address), "");
411
412         torture_assert(tctx, test_OpenPrinter(tctx, p, &handle), "");
413         torture_assert(tctx, test_RemoteFindFirstPrinterChangeNotifyEx(tctx, p, &handle, address, server_option), "");
414         torture_assert(tctx, received_packets, "no packets received");
415         torture_assert_int_equal(tctx, received_packets->opnum, NDR_SPOOLSS_REPLYOPENPRINTER,
416                 "no ReplyOpenPrinter packet after RemoteFindFirstPrinterChangeNotifyEx");
417         torture_assert(tctx, test_RouterRefreshPrinterChangeNotify(tctx, p, &handle, NULL), "");
418         torture_assert(tctx, test_RouterRefreshPrinterChangeNotify(tctx, p, &handle, server_option), "");
419         torture_assert(tctx, test_ClosePrinter(tctx, p, &handle), "");
420         tmp = last_packet(received_packets);
421         torture_assert_int_equal(tctx, tmp->opnum, NDR_SPOOLSS_REPLYCLOSEPRINTER,
422                 "no ReplyClosePrinter packet after ClosePrinter");
423
424         /* Shut down DCE/RPC server */
425         talloc_free(dce_ctx);
426
427         return true;
428 }
429
430 /** Test that makes sure that calling ReplyOpenPrinter()
431  * on Samba 4 will cause an irpc broadcast call.
432  */
433 static bool test_ReplyOpenPrinter(struct torture_context *tctx,
434                                   struct dcerpc_pipe *p)
435 {
436         struct spoolss_ReplyOpenPrinter r;
437         struct spoolss_ReplyClosePrinter s;
438         struct policy_handle h;
439
440         if (torture_setting_bool(tctx, "samba3", false)) {
441                 torture_skip(tctx, "skipping ReplyOpenPrinter server implementation test against s3\n");
442         }
443
444         r.in.server_name = "earth";
445         r.in.printer_local = 2;
446         r.in.type = REG_DWORD;
447         r.in.bufsize = 0;
448         r.in.buffer = NULL;
449         r.out.handle = &h;
450
451         torture_assert_ntstatus_ok(tctx,
452                         dcerpc_spoolss_ReplyOpenPrinter(p, tctx, &r),
453                         "spoolss_ReplyOpenPrinter call failed");
454
455         torture_assert_werr_ok(tctx, r.out.result, "error return code");
456
457         s.in.handle = &h;
458         s.out.handle = &h;
459
460         torture_assert_ntstatus_ok(tctx,
461                         dcerpc_spoolss_ReplyClosePrinter(p, tctx, &s),
462                         "spoolss_ReplyClosePrinter call failed");
463
464         torture_assert_werr_ok(tctx, r.out.result, "error return code");
465
466         return true;
467 }
468
469 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
470 {
471         struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
472
473         struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite,
474                                                         "notify", &ndr_table_spoolss);
475
476         torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
477         torture_rpc_tcase_add_test(tcase, "testReplyOpenPrinter", test_ReplyOpenPrinter);
478
479         return suite;
480 }