s4-smbtorture: restructure spoolss server implementations in RPC-SPOOLSS-NOTIFY.
[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 "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"
34
35 static NTSTATUS spoolss__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
36 {
37         return NT_STATUS_OK;
38 }
39
40 static void spoolss__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
41 {
42 }
43
44 static NTSTATUS spoolss__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
45 {
46         enum ndr_err_code ndr_err;
47         uint16_t opnum = dce_call->pkt.u.request.opnum;
48
49         dce_call->fault_code = 0;
50
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;
54         }
55
56         *r = talloc_size(mem_ctx, ndr_table_spoolss.calls[opnum].struct_size);
57         NT_STATUS_HAVE_NO_MEMORY(*r);
58
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;
67         }
68
69         return NT_STATUS_OK;
70 }
71
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 {
75         uint16_t opnum;
76         void *r;
77         struct received_packet *prev, *next;
78 } *received_packets = NULL;
79
80 static WERROR _spoolss_ReplyOpenPrinter(struct dcesrv_call_state *dce_call,
81                                         TALLOC_CTX *mem_ctx,
82                                         struct spoolss_ReplyOpenPrinter *r)
83 {
84         DEBUG(1,("_spoolss_ReplyOpenPrinter\n"));
85
86         NDR_PRINT_IN_DEBUG(spoolss_ReplyOpenPrinter, r);
87
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;
92
93         NDR_PRINT_OUT_DEBUG(spoolss_ReplyOpenPrinter, r);
94
95         return WERR_OK;
96 }
97
98 static NTSTATUS spoolss__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
99 {
100         uint16_t opnum = dce_call->pkt.u.request.opnum;
101         struct received_packet *rp;
102
103         rp = talloc_zero(talloc_autofree_context(), struct received_packet);
104         rp->opnum = opnum;
105         rp->r = talloc_reference(rp, r);
106
107         DLIST_ADD_END(received_packets, rp, struct received_packet *);
108
109         switch (opnum) {
110         case 58: {
111                 struct spoolss_ReplyOpenPrinter *r2 = (struct spoolss_ReplyOpenPrinter *)r;
112                 r2->out.result = _spoolss_ReplyOpenPrinter(dce_call, mem_ctx, r2);
113                 break;
114         }
115
116         default:
117                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
118                 break;
119         }
120
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;
126         }
127         return NT_STATUS_OK;
128 }
129
130
131 static NTSTATUS spoolss__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
132 {
133         return NT_STATUS_OK;
134 }
135
136
137 static NTSTATUS spoolss__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
138 {
139         enum ndr_err_code ndr_err;
140         uint16_t opnum = dce_call->pkt.u.request.opnum;
141
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;
146         }
147
148         return NT_STATUS_OK;
149 }
150
151 const static struct dcesrv_interface notify_test_spoolss_interface = {
152         .name           = "spoolss",
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
160 };
161
162 static bool spoolss__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
163 {
164         if (notify_test_spoolss_interface.syntax_id.if_version == if_version &&
165                 GUID_equal(&notify_test_spoolss_interface.syntax_id.uuid, uuid)) {
166                 memcpy(iface,&notify_test_spoolss_interface, sizeof(*iface));
167                 return true;
168         }
169
170         return false;
171 }
172
173 static bool spoolss__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
174 {
175         if (strcmp(notify_test_spoolss_interface.name, name)==0) {
176                 memcpy(iface, &notify_test_spoolss_interface, sizeof(*iface));
177                 return true;
178         }
179
180         return false;
181 }
182
183 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
184 {
185         int i;
186
187         for (i=0;i<ndr_table_spoolss.endpoints->count;i++) {
188                 NTSTATUS ret;
189                 const char *name = ndr_table_spoolss.endpoints->names[i];
190
191                 ret = dcesrv_interface_register(dce_ctx, name, &notify_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));
194                         return ret;
195                 }
196         }
197
198         return NT_STATUS_OK;
199 }
200
201 static bool test_RFFPCNEx(struct torture_context *tctx,
202                           struct dcerpc_pipe *p)
203 {
204         struct spoolss_OpenPrinter q;
205         struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
206         struct dcesrv_endpoint_server ep_server;
207         NTSTATUS status;
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;
213
214         struct policy_handle handle;
215         const char *address;
216         struct interface *ifaces;
217
218         received_packets = NULL;
219
220         ntvfs_init(tctx->lp_ctx);
221
222         ZERO_STRUCT(q);
223
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;
229
230         torture_comment(tctx, "Testing OpenPrinter(%s)\n", q.in.printername);
231
232         status = dcerpc_spoolss_OpenPrinter(p, tctx, &q);
233
234         torture_assert_ntstatus_ok(tctx, status, "OpenPrinter failed");
235
236         torture_assert_werr_ok(tctx, q.out.result, "OpenPrinter failed");
237
238         /* Start DCE/RPC server */
239
240         /* fill in our name */
241         ep_server.name = "spoolss";
242
243         /* fill in all the operations */
244         ep_server.init_server = spoolss__op_init_server;
245
246         ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
247         ep_server.interface_by_name = spoolss__op_interface_by_name;
248
249         torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
250                                   "unable to register spoolss server");
251
252         lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
253
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");
259
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");
263
264         /* Make sure the directory for NCALRPC exists */
265         if (!directory_exist(lp_ncalrpc_dir(tctx->lp_ctx))) {
266                 int ret;
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)));
272         }
273
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");
279         }
280
281         r.in.flags = 0;
282         r.in.local_machine = talloc_asprintf(tctx, "\\\\%s", address);
283         r.in.options = 0;
284         r.in.printer_local = 123;
285         t1.version = 2;
286         t1.flags = 0;
287         t1.count = 2;
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;
293
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;
298
299         r.in.notify_options = &t1;
300         r.in.handle = &handle;
301
302         status = dcerpc_spoolss_RemoteFindFirstPrinterChangeNotifyEx(p, tctx, &r);
303
304         torture_assert_ntstatus_ok(tctx, status, "FFPCNEx failed");
305
306         torture_assert_werr_ok(tctx, r.out.result, "error return code for FFPCNEx");
307
308         cp.in.handle = &handle;
309         cp.out.handle = &handle;
310
311         torture_comment(tctx, "Testing ClosePrinter\n");
312
313         status = dcerpc_spoolss_ClosePrinter(p, tctx, &cp);
314         torture_assert_ntstatus_ok(tctx, status, "ClosePrinter failed");
315
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");
319
320         /* Shut down DCE/RPC server */
321         talloc_free(dce_ctx);
322
323         return true;
324 }
325
326 /** Test that makes sure that calling ReplyOpenPrinter()
327  * on Samba 4 will cause an irpc broadcast call.
328  */
329 static bool test_ReplyOpenPrinter(struct torture_context *tctx,
330                                   struct dcerpc_pipe *p)
331 {
332         struct spoolss_ReplyOpenPrinter r;
333         struct spoolss_ReplyClosePrinter s;
334         struct policy_handle h;
335
336         if (torture_setting_bool(tctx, "samba3", false)) {
337                 torture_skip(tctx, "skipping ReplyOpenPrinter server implementation test against s3\n");
338         }
339
340         r.in.server_name = "earth";
341         r.in.printer_local = 2;
342         r.in.type = REG_DWORD;
343         r.in.bufsize = 0;
344         r.in.buffer = NULL;
345         r.out.handle = &h;
346
347         torture_assert_ntstatus_ok(tctx,
348                         dcerpc_spoolss_ReplyOpenPrinter(p, tctx, &r),
349                         "spoolss_ReplyOpenPrinter call failed");
350
351         torture_assert_werr_ok(tctx, r.out.result, "error return code");
352
353         s.in.handle = &h;
354         s.out.handle = &h;
355
356         torture_assert_ntstatus_ok(tctx,
357                         dcerpc_spoolss_ReplyClosePrinter(p, tctx, &s),
358                         "spoolss_ReplyClosePrinter call failed");
359
360         torture_assert_werr_ok(tctx, r.out.result, "error return code");
361
362         return true;
363 }
364
365 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
366 {
367         struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
368
369         struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite,
370                                                         "notify", &ndr_table_spoolss);
371
372         torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
373         torture_rpc_tcase_add_test(tcase, "testReplyOpenPrinter", test_ReplyOpenPrinter);
374
375         return suite;
376 }