2 Unix SMB/CIFS implementation.
3 test suite for spoolss rpc notify operations
5 Copyright (C) Jelmer Vernooij 2007
6 Copyright (C) Guenther Deschner 2010
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.
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.
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.
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"
36 static NTSTATUS spoolss__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
41 static void spoolss__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
45 static NTSTATUS spoolss__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
47 enum ndr_err_code ndr_err;
48 uint16_t opnum = dce_call->pkt.u.request.opnum;
50 dce_call->fault_code = 0;
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;
57 *r = talloc_size(mem_ctx, ndr_table_spoolss.calls[opnum].struct_size);
58 NT_STATUS_HAVE_NO_MEMORY(*r);
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;
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 {
78 struct received_packet *prev, *next;
79 } *received_packets = NULL;
81 static WERROR _spoolss_ReplyOpenPrinter(struct dcesrv_call_state *dce_call,
83 struct spoolss_ReplyOpenPrinter *r)
85 DEBUG(1,("_spoolss_ReplyOpenPrinter\n"));
87 NDR_PRINT_IN_DEBUG(spoolss_ReplyOpenPrinter, r);
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;
94 NDR_PRINT_OUT_DEBUG(spoolss_ReplyOpenPrinter, r);
99 static WERROR _spoolss_ReplyClosePrinter(struct dcesrv_call_state *dce_call,
101 struct spoolss_ReplyClosePrinter *r)
103 DEBUG(1,("_spoolss_ReplyClosePrinter\n"));
105 NDR_PRINT_IN_DEBUG(spoolss_ReplyClosePrinter, r);
107 ZERO_STRUCTP(r->out.handle);
108 r->out.result = WERR_OK;
110 NDR_PRINT_OUT_DEBUG(spoolss_ReplyClosePrinter, r);
115 static NTSTATUS spoolss__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
117 uint16_t opnum = dce_call->pkt.u.request.opnum;
118 struct received_packet *rp;
120 rp = talloc_zero(talloc_autofree_context(), struct received_packet);
122 rp->r = talloc_reference(rp, r);
124 DLIST_ADD_END(received_packets, rp, struct received_packet *);
128 struct spoolss_ReplyOpenPrinter *r2 = (struct spoolss_ReplyOpenPrinter *)r;
129 r2->out.result = _spoolss_ReplyOpenPrinter(dce_call, mem_ctx, r2);
133 struct spoolss_ReplyClosePrinter *r2 = (struct spoolss_ReplyClosePrinter *)r;
134 r2->out.result = _spoolss_ReplyClosePrinter(dce_call, mem_ctx, r2);
139 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
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;
153 static NTSTATUS spoolss__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
159 static NTSTATUS spoolss__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
161 enum ndr_err_code ndr_err;
162 uint16_t opnum = dce_call->pkt.u.request.opnum;
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;
173 const static struct dcesrv_interface notify_test_spoolss_interface = {
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
184 static bool spoolss__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
186 if (notify_test_spoolss_interface.syntax_id.if_version == if_version &&
187 GUID_equal(¬ify_test_spoolss_interface.syntax_id.uuid, uuid)) {
188 memcpy(iface,¬ify_test_spoolss_interface, sizeof(*iface));
195 static bool spoolss__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
197 if (strcmp(notify_test_spoolss_interface.name, name)==0) {
198 memcpy(iface, ¬ify_test_spoolss_interface, sizeof(*iface));
205 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
209 for (i=0;i<ndr_table_spoolss.endpoints->count;i++) {
211 const char *name = ndr_table_spoolss.endpoints->names[i];
213 ret = dcesrv_interface_register(dce_ctx, name, ¬ify_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));
223 static bool test_OpenPrinter(struct torture_context *tctx,
224 struct dcerpc_pipe *p,
225 struct policy_handle *handle)
227 struct spoolss_OpenPrinter r;
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;
237 torture_comment(tctx, "Testing OpenPrinter(%s)\n", r.in.printername);
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");
245 static struct spoolss_NotifyOption *setup_printserver_NotifyOption(struct torture_context *tctx)
247 struct spoolss_NotifyOption *o;
249 o = talloc_zero(tctx, struct spoolss_NotifyOption);
252 o->flags = PRINTER_NOTIFY_OPTIONS_REFRESH;
255 o->types = talloc_zero_array(o, struct spoolss_NotifyOptionType, o->count);
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;
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;
270 static bool test_RemoteFindFirstPrinterChangeNotifyEx(struct torture_context *tctx,
271 struct dcerpc_pipe *p,
272 struct policy_handle *handle,
274 struct spoolss_NotifyOption *option)
276 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
278 torture_comment(tctx, "Testing RemoteFindFirstPrinterChangeNotifyEx\n");
281 r.in.local_machine = talloc_asprintf(tctx, "\\\\%s", address);
283 r.in.printer_local = 0;
284 r.in.notify_options = option;
285 r.in.handle = handle;
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");
295 static bool test_RouterRefreshPrinterChangeNotify(struct torture_context *tctx,
296 struct dcerpc_pipe *p,
297 struct policy_handle *handle,
298 struct spoolss_NotifyOption *options)
300 struct spoolss_RouterRefreshPrinterChangeNotify r;
301 struct spoolss_NotifyInfo *info;
303 torture_comment(tctx, "Testing RouterRefreshPrinterChangeNotify\n");
305 r.in.handle = handle;
307 r.in.options = options;
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");
318 static bool test_ClosePrinter(struct torture_context *tctx,
319 struct dcerpc_pipe *p,
320 struct policy_handle *handle)
322 struct spoolss_ClosePrinter r;
324 r.in.handle = handle;
325 r.out.handle = handle;
327 torture_comment(tctx, "Testing ClosePrinter\n");
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");
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)
342 struct dcesrv_endpoint_server ep_server;
344 struct dcesrv_context *dce_ctx;
345 const char *endpoints[] = { "spoolss", NULL };
346 struct dcesrv_endpoint *e;
348 struct interface *ifaces;
350 ntvfs_init(tctx->lp_ctx);
352 /* fill in our name */
353 ep_server.name = "spoolss";
355 /* fill in all the operations */
356 ep_server.init_server = spoolss__op_init_server;
358 ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
359 ep_server.interface_by_name = spoolss__op_interface_by_name;
361 torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
362 "unable to register spoolss server");
364 lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
366 load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
367 address = iface_n_ip(ifaces, 0);
369 torture_comment(tctx, "Listening for callbacks on %s\n", address);
371 status = smbsrv_add_socket(event_ctx, tctx->lp_ctx, &single_ops, address);
372 torture_assert_ntstatus_ok(tctx, status, "starting smb server");
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");
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");
385 *dce_ctx_p = dce_ctx;
386 *address_p = address;
391 static struct received_packet *last_packet(struct received_packet *p)
393 struct received_packet *tmp;
394 for (tmp = p; tmp->next; tmp = tmp->next) ;;
398 static bool test_RFFPCNEx(struct torture_context *tctx,
399 struct dcerpc_pipe *p)
401 struct dcesrv_context *dce_ctx;
402 struct policy_handle handle;
404 struct received_packet *tmp;
405 struct spoolss_NotifyOption *server_option = setup_printserver_NotifyOption(tctx);
407 received_packets = NULL;
409 /* Start DCE/RPC server */
410 torture_assert(tctx, test_start_dcerpc_server(tctx, p->conn->event_ctx, &dce_ctx, &address), "");
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");
424 /* Shut down DCE/RPC server */
425 talloc_free(dce_ctx);
430 /** Test that makes sure that calling ReplyOpenPrinter()
431 * on Samba 4 will cause an irpc broadcast call.
433 static bool test_ReplyOpenPrinter(struct torture_context *tctx,
434 struct dcerpc_pipe *p)
436 struct spoolss_ReplyOpenPrinter r;
437 struct spoolss_ReplyClosePrinter s;
438 struct policy_handle h;
440 if (torture_setting_bool(tctx, "samba3", false)) {
441 torture_skip(tctx, "skipping ReplyOpenPrinter server implementation test against s3\n");
444 r.in.server_name = "earth";
445 r.in.printer_local = 2;
446 r.in.type = REG_DWORD;
451 torture_assert_ntstatus_ok(tctx,
452 dcerpc_spoolss_ReplyOpenPrinter(p, tctx, &r),
453 "spoolss_ReplyOpenPrinter call failed");
455 torture_assert_werr_ok(tctx, r.out.result, "error return code");
460 torture_assert_ntstatus_ok(tctx,
461 dcerpc_spoolss_ReplyClosePrinter(p, tctx, &s),
462 "spoolss_ReplyClosePrinter call failed");
464 torture_assert_werr_ok(tctx, r.out.result, "error return code");
469 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
471 struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
473 struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite,
474 "notify", &ndr_table_spoolss);
476 torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
477 torture_rpc_tcase_add_test(tcase, "testReplyOpenPrinter", test_ReplyOpenPrinter);