2 Unix SMB/CIFS implementation.
3 test suite for spoolss rpc notify operations
5 Copyright (C) Jelmer Vernooij 2007
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.
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.
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.
23 #include "system/filesys.h"
24 #include "torture/rpc/rpc.h"
25 #include "librpc/gen_ndr/ndr_spoolss_c.h"
26 #include "librpc/gen_ndr/ndr_spoolss.h"
27 #include "rpc_server/dcerpc_server.h"
28 #include "rpc_server/service_rpc.h"
29 #include "smbd/process_model.h"
30 #include "smb_server/smb_server.h"
31 #include "lib/socket/netif.h"
32 #include "ntvfs/ntvfs.h"
33 #include "param/param.h"
35 static NTSTATUS spoolss__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
40 static void spoolss__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
44 static NTSTATUS spoolss__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
46 enum ndr_err_code ndr_err;
47 uint16_t opnum = dce_call->pkt.u.request.opnum;
49 dce_call->fault_code = 0;
51 if (opnum >= ndr_table_spoolss.num_calls) {
52 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
53 return NT_STATUS_NET_WRITE_FAULT;
56 *r = talloc_size(mem_ctx, ndr_table_spoolss.calls[opnum].struct_size);
57 NT_STATUS_HAVE_NO_MEMORY(*r);
59 /* unravel the NDR for the packet */
60 ndr_err = ndr_table_spoolss.calls[opnum].ndr_pull(pull, NDR_IN, *r);
61 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
62 dcerpc_log_packet(dce_call->conn->packet_log_dir,
63 &ndr_table_spoolss, opnum, NDR_IN,
64 &dce_call->pkt.u.request.stub_and_verifier);
65 dce_call->fault_code = DCERPC_FAULT_NDR;
66 return NT_STATUS_NET_WRITE_FAULT;
72 /* Note that received_packets are allocated in talloc_autofree_context(),
73 * because no other context appears to stay around long enough. */
74 static struct received_packet {
77 struct received_packet *prev, *next;
78 } *received_packets = NULL;
80 static WERROR _spoolss_ReplyOpenPrinter(struct dcesrv_call_state *dce_call,
82 struct spoolss_ReplyOpenPrinter *r)
84 DEBUG(1,("_spoolss_ReplyOpenPrinter\n"));
86 NDR_PRINT_IN_DEBUG(spoolss_ReplyOpenPrinter, r);
88 r->out.handle = talloc(r, struct policy_handle);
89 r->out.handle->handle_type = 42;
90 r->out.handle->uuid = GUID_random();
91 r->out.result = WERR_OK;
93 NDR_PRINT_OUT_DEBUG(spoolss_ReplyOpenPrinter, r);
98 static NTSTATUS spoolss__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
100 uint16_t opnum = dce_call->pkt.u.request.opnum;
101 struct received_packet *rp;
103 rp = talloc_zero(talloc_autofree_context(), struct received_packet);
105 rp->r = talloc_reference(rp, r);
107 DLIST_ADD_END(received_packets, rp, struct received_packet *);
111 struct spoolss_ReplyOpenPrinter *r2 = (struct spoolss_ReplyOpenPrinter *)r;
112 r2->out.result = _spoolss_ReplyOpenPrinter(dce_call, mem_ctx, r2);
117 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
121 if (dce_call->fault_code != 0) {
122 dcerpc_log_packet(dce_call->conn->packet_log_dir,
123 &ndr_table_spoolss, opnum, NDR_IN,
124 &dce_call->pkt.u.request.stub_and_verifier);
125 return NT_STATUS_NET_WRITE_FAULT;
131 static NTSTATUS spoolss__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
137 static NTSTATUS spoolss__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
139 enum ndr_err_code ndr_err;
140 uint16_t opnum = dce_call->pkt.u.request.opnum;
142 ndr_err = ndr_table_spoolss.calls[opnum].ndr_push(push, NDR_OUT, r);
143 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
144 dce_call->fault_code = DCERPC_FAULT_NDR;
145 return NT_STATUS_NET_WRITE_FAULT;
151 const static struct dcesrv_interface notify_test_spoolss_interface = {
153 .syntax_id = {{0x12345678,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xab}},1.0},
154 .bind = spoolss__op_bind,
155 .unbind = spoolss__op_unbind,
156 .ndr_pull = spoolss__op_ndr_pull,
157 .dispatch = spoolss__op_dispatch,
158 .reply = spoolss__op_reply,
159 .ndr_push = spoolss__op_ndr_push
162 static bool spoolss__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
164 if (notify_test_spoolss_interface.syntax_id.if_version == if_version &&
165 GUID_equal(¬ify_test_spoolss_interface.syntax_id.uuid, uuid)) {
166 memcpy(iface,¬ify_test_spoolss_interface, sizeof(*iface));
173 static bool spoolss__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
175 if (strcmp(notify_test_spoolss_interface.name, name)==0) {
176 memcpy(iface, ¬ify_test_spoolss_interface, sizeof(*iface));
183 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
187 for (i=0;i<ndr_table_spoolss.endpoints->count;i++) {
189 const char *name = ndr_table_spoolss.endpoints->names[i];
191 ret = dcesrv_interface_register(dce_ctx, name, ¬ify_test_spoolss_interface, NULL);
192 if (!NT_STATUS_IS_OK(ret)) {
193 DEBUG(1,("spoolss_op_init_server: failed to register endpoint '%s'\n",name));
201 static bool test_RFFPCNEx(struct torture_context *tctx,
202 struct dcerpc_pipe *p)
204 struct spoolss_OpenPrinter q;
205 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
206 struct dcesrv_endpoint_server ep_server;
208 struct dcesrv_context *dce_ctx;
209 const char *endpoints[] = { "spoolss", NULL };
210 struct dcesrv_endpoint *e;
211 struct spoolss_NotifyOption t1;
212 struct spoolss_ClosePrinter cp;
214 struct policy_handle handle;
216 struct interface *ifaces;
218 received_packets = NULL;
220 ntvfs_init(tctx->lp_ctx);
224 q.in.printername = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
225 q.in.datatype = NULL;
226 q.in.devmode_ctr.devmode= NULL;
227 q.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
228 q.out.handle = &handle;
230 torture_comment(tctx, "Testing OpenPrinter(%s)\n", q.in.printername);
232 status = dcerpc_spoolss_OpenPrinter(p, tctx, &q);
234 torture_assert_ntstatus_ok(tctx, status, "OpenPrinter failed");
236 torture_assert_werr_ok(tctx, q.out.result, "OpenPrinter failed");
238 /* Start DCE/RPC server */
240 /* fill in our name */
241 ep_server.name = "spoolss";
243 /* fill in all the operations */
244 ep_server.init_server = spoolss__op_init_server;
246 ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
247 ep_server.interface_by_name = spoolss__op_interface_by_name;
249 torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
250 "unable to register spoolss server");
252 lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
254 load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
255 address = iface_n_ip(ifaces, 0);
256 torture_comment(tctx, "Listening for callbacks on %s\n", address);
257 status = smbsrv_add_socket(p->conn->event_ctx, tctx->lp_ctx, &single_ops, address);
258 torture_assert_ntstatus_ok(tctx, status, "starting smb server");
260 status = dcesrv_init_context(tctx, tctx->lp_ctx, endpoints, &dce_ctx);
261 torture_assert_ntstatus_ok(tctx, status,
262 "unable to initialize DCE/RPC server");
264 /* Make sure the directory for NCALRPC exists */
265 if (!directory_exist(lp_ncalrpc_dir(tctx->lp_ctx))) {
267 ret = mkdir(lp_ncalrpc_dir(tctx->lp_ctx), 0755);
268 torture_assert(tctx, (ret == 0), talloc_asprintf(tctx,
269 "failed to mkdir(%s) ret[%d] errno[%d - %s]",
270 lp_ncalrpc_dir(tctx->lp_ctx), ret,
271 errno, strerror(errno)));
274 for (e=dce_ctx->endpoint_list;e;e=e->next) {
275 status = dcesrv_add_ep(dce_ctx, tctx->lp_ctx,
276 e, tctx->ev, &single_ops);
277 torture_assert_ntstatus_ok(tctx, status,
278 "unable listen on dcerpc endpoint server");
282 r.in.local_machine = talloc_asprintf(tctx, "\\\\%s", address);
284 r.in.printer_local = 123;
288 t1.types = talloc_zero_array(tctx, struct spoolss_NotifyOptionType, 2);
289 t1.types[0].type = PRINTER_NOTIFY_TYPE;
290 t1.types[0].count = 1;
291 t1.types[0].fields = talloc_array(t1.types, union spoolss_Field, 1);
292 t1.types[0].fields[0].field = PRINTER_NOTIFY_FIELD_SERVER_NAME;
294 t1.types[1].type = JOB_NOTIFY_TYPE;
295 t1.types[1].count = 1;
296 t1.types[1].fields = talloc_array(t1.types, union spoolss_Field, 1);
297 t1.types[1].fields[0].field = PRINTER_NOTIFY_FIELD_PRINTER_NAME;
299 r.in.notify_options = &t1;
300 r.in.handle = &handle;
302 status = dcerpc_spoolss_RemoteFindFirstPrinterChangeNotifyEx(p, tctx, &r);
304 torture_assert_ntstatus_ok(tctx, status, "FFPCNEx failed");
306 torture_assert_werr_ok(tctx, r.out.result, "error return code for FFPCNEx");
308 cp.in.handle = &handle;
309 cp.out.handle = &handle;
311 torture_comment(tctx, "Testing ClosePrinter\n");
313 status = dcerpc_spoolss_ClosePrinter(p, tctx, &cp);
314 torture_assert_ntstatus_ok(tctx, status, "ClosePrinter failed");
316 /* We should've had an incoming packet 58 (ReplyOpenPrinter) */
317 torture_assert(tctx, received_packets != NULL, "no packets received");
318 torture_assert_int_equal(tctx, received_packets->opnum, 58, "invalid opnum");
320 /* Shut down DCE/RPC server */
321 talloc_free(dce_ctx);
326 /** Test that makes sure that calling ReplyOpenPrinter()
327 * on Samba 4 will cause an irpc broadcast call.
329 static bool test_ReplyOpenPrinter(struct torture_context *tctx,
330 struct dcerpc_pipe *p)
332 struct spoolss_ReplyOpenPrinter r;
333 struct spoolss_ReplyClosePrinter s;
334 struct policy_handle h;
336 if (torture_setting_bool(tctx, "samba3", false)) {
337 torture_skip(tctx, "skipping ReplyOpenPrinter server implementation test against s3\n");
340 r.in.server_name = "earth";
341 r.in.printer_local = 2;
342 r.in.type = REG_DWORD;
347 torture_assert_ntstatus_ok(tctx,
348 dcerpc_spoolss_ReplyOpenPrinter(p, tctx, &r),
349 "spoolss_ReplyOpenPrinter call failed");
351 torture_assert_werr_ok(tctx, r.out.result, "error return code");
356 torture_assert_ntstatus_ok(tctx,
357 dcerpc_spoolss_ReplyClosePrinter(p, tctx, &s),
358 "spoolss_ReplyClosePrinter call failed");
360 torture_assert_werr_ok(tctx, r.out.result, "error return code");
365 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
367 struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
369 struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite,
370 "notify", &ndr_table_spoolss);
372 torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
373 torture_rpc_tcase_add_test(tcase, "testReplyOpenPrinter", test_ReplyOpenPrinter);