s4-smbtorture: rework spoolss_NotifyOption handling in RPC-SPOOLSS-NOTIFY.
[Samba/fernandojvsilva.git] / source4 / torture / rpc / spoolss_notify.c
blob1e441f9ac2f7acbba459ec02940920efaa890c59
1 /*
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.
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"
36 static NTSTATUS spoolss__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
38 return NT_STATUS_OK;
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;
70 return NT_STATUS_OK;
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;
81 static WERROR _spoolss_ReplyOpenPrinter(struct dcesrv_call_state *dce_call,
82 TALLOC_CTX *mem_ctx,
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);
96 return WERR_OK;
99 static WERROR _spoolss_ReplyClosePrinter(struct dcesrv_call_state *dce_call,
100 TALLOC_CTX *mem_ctx,
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);
112 return WERR_OK;
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);
121 rp->opnum = opnum;
122 rp->r = talloc_reference(rp, r);
124 DLIST_ADD_END(received_packets, rp, struct received_packet *);
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;
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;
138 default:
139 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
140 break;
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;
149 return NT_STATUS_OK;
153 static NTSTATUS spoolss__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
155 return NT_STATUS_OK;
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;
170 return NT_STATUS_OK;
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
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(&notify_test_spoolss_interface.syntax_id.uuid, uuid)) {
188 memcpy(iface,&notify_test_spoolss_interface, sizeof(*iface));
189 return true;
192 return false;
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, &notify_test_spoolss_interface, sizeof(*iface));
199 return true;
202 return false;
205 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
207 int i;
209 for (i=0;i<ndr_table_spoolss.endpoints->count;i++) {
210 NTSTATUS ret;
211 const char *name = ndr_table_spoolss.endpoints->names[i];
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;
220 return NT_STATUS_OK;
223 static bool test_OpenPrinter(struct torture_context *tctx,
224 struct dcerpc_pipe *p,
225 struct policy_handle *handle)
227 struct spoolss_OpenPrinter r;
229 ZERO_STRUCT(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");
242 return true;
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);
251 o->version = 2;
252 o->flags = PRINTER_NOTIFY_OPTIONS_REFRESH;
254 o->count = 2;
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;
267 return o;
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)
276 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
278 torture_comment(tctx, "Testing RemoteFindFirstPrinterChangeNotifyEx\n");
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;
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");
292 return true;
295 static bool test_ClosePrinter(struct torture_context *tctx,
296 struct dcerpc_pipe *p,
297 struct policy_handle *handle)
299 struct spoolss_ClosePrinter r;
301 r.in.handle = handle;
302 r.out.handle = handle;
304 torture_comment(tctx, "Testing ClosePrinter\n");
306 torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_ClosePrinter(p, tctx, &r),
307 "ClosePrinter failed");
308 torture_assert_werr_ok(tctx, r.out.result,
309 "ClosePrinter failed");
311 return true;
314 static bool test_start_dcerpc_server(struct torture_context *tctx,
315 struct tevent_context *event_ctx,
316 struct dcesrv_context **dce_ctx_p,
317 const char **address_p)
319 struct dcesrv_endpoint_server ep_server;
320 NTSTATUS status;
321 struct dcesrv_context *dce_ctx;
322 const char *endpoints[] = { "spoolss", NULL };
323 struct dcesrv_endpoint *e;
324 const char *address;
325 struct interface *ifaces;
327 ntvfs_init(tctx->lp_ctx);
329 /* fill in our name */
330 ep_server.name = "spoolss";
332 /* fill in all the operations */
333 ep_server.init_server = spoolss__op_init_server;
335 ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
336 ep_server.interface_by_name = spoolss__op_interface_by_name;
338 torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
339 "unable to register spoolss server");
341 lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
343 load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
344 address = iface_n_ip(ifaces, 0);
346 torture_comment(tctx, "Listening for callbacks on %s\n", address);
348 status = smbsrv_add_socket(event_ctx, tctx->lp_ctx, &single_ops, address);
349 torture_assert_ntstatus_ok(tctx, status, "starting smb server");
351 status = dcesrv_init_context(tctx, tctx->lp_ctx, endpoints, &dce_ctx);
352 torture_assert_ntstatus_ok(tctx, status,
353 "unable to initialize DCE/RPC server");
355 for (e=dce_ctx->endpoint_list;e;e=e->next) {
356 status = dcesrv_add_ep(dce_ctx, tctx->lp_ctx,
357 e, tctx->ev, &single_ops);
358 torture_assert_ntstatus_ok(tctx, status,
359 "unable listen on dcerpc endpoint server");
362 *dce_ctx_p = dce_ctx;
363 *address_p = address;
365 return true;
368 static struct received_packet *last_packet(struct received_packet *p)
370 struct received_packet *tmp;
371 for (tmp = p; tmp->next; tmp = tmp->next) ;;
372 return tmp;
375 static bool test_RFFPCNEx(struct torture_context *tctx,
376 struct dcerpc_pipe *p)
378 struct dcesrv_context *dce_ctx;
379 struct policy_handle handle;
380 const char *address;
381 struct received_packet *tmp;
382 struct spoolss_NotifyOption *server_option = setup_printserver_NotifyOption(tctx);
384 received_packets = NULL;
386 /* Start DCE/RPC server */
387 torture_assert(tctx, test_start_dcerpc_server(tctx, p->conn->event_ctx, &dce_ctx, &address), "");
389 torture_assert(tctx, test_OpenPrinter(tctx, p, &handle), "");
390 torture_assert(tctx, test_RemoteFindFirstPrinterChangeNotifyEx(tctx, p, &handle, address, server_option), "");
391 torture_assert(tctx, received_packets, "no packets received");
392 torture_assert_int_equal(tctx, received_packets->opnum, NDR_SPOOLSS_REPLYOPENPRINTER,
393 "no ReplyOpenPrinter packet after RemoteFindFirstPrinterChangeNotifyEx");
394 torture_assert(tctx, test_ClosePrinter(tctx, p, &handle), "");
395 tmp = last_packet(received_packets);
396 torture_assert_int_equal(tctx, tmp->opnum, NDR_SPOOLSS_REPLYCLOSEPRINTER,
397 "no ReplyClosePrinter packet after ClosePrinter");
399 /* Shut down DCE/RPC server */
400 talloc_free(dce_ctx);
402 return true;
405 /** Test that makes sure that calling ReplyOpenPrinter()
406 * on Samba 4 will cause an irpc broadcast call.
408 static bool test_ReplyOpenPrinter(struct torture_context *tctx,
409 struct dcerpc_pipe *p)
411 struct spoolss_ReplyOpenPrinter r;
412 struct spoolss_ReplyClosePrinter s;
413 struct policy_handle h;
415 if (torture_setting_bool(tctx, "samba3", false)) {
416 torture_skip(tctx, "skipping ReplyOpenPrinter server implementation test against s3\n");
419 r.in.server_name = "earth";
420 r.in.printer_local = 2;
421 r.in.type = REG_DWORD;
422 r.in.bufsize = 0;
423 r.in.buffer = NULL;
424 r.out.handle = &h;
426 torture_assert_ntstatus_ok(tctx,
427 dcerpc_spoolss_ReplyOpenPrinter(p, tctx, &r),
428 "spoolss_ReplyOpenPrinter call failed");
430 torture_assert_werr_ok(tctx, r.out.result, "error return code");
432 s.in.handle = &h;
433 s.out.handle = &h;
435 torture_assert_ntstatus_ok(tctx,
436 dcerpc_spoolss_ReplyClosePrinter(p, tctx, &s),
437 "spoolss_ReplyClosePrinter call failed");
439 torture_assert_werr_ok(tctx, r.out.result, "error return code");
441 return true;
444 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
446 struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
448 struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite,
449 "notify", &ndr_table_spoolss);
451 torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
452 torture_rpc_tcase_add_test(tcase, "testReplyOpenPrinter", test_ReplyOpenPrinter);
454 return suite;