s4-smbtorture: pick correct last packet while checking backchannel replies in RPC...
[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 bool test_RemoteFindFirstPrinterChangeNotifyEx(struct torture_context *tctx,
246                                                       struct dcerpc_pipe *p,
247                                                       struct policy_handle *handle,
248                                                       const char *address)
249 {
250         struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
251         struct spoolss_NotifyOption t1;
252
253         torture_comment(tctx, "Testing RemoteFindFirstPrinterChangeNotifyEx\n");
254
255         t1.version = 2;
256         t1.flags = 0;
257         t1.count = 2;
258         t1.types = talloc_zero_array(tctx, struct spoolss_NotifyOptionType, 2);
259         t1.types[0].type = PRINTER_NOTIFY_TYPE;
260         t1.types[0].count = 1;
261         t1.types[0].fields = talloc_array(t1.types, union spoolss_Field, 1);
262         t1.types[0].fields[0].field = PRINTER_NOTIFY_FIELD_SERVER_NAME;
263
264         t1.types[1].type = JOB_NOTIFY_TYPE;
265         t1.types[1].count = 1;
266         t1.types[1].fields = talloc_array(t1.types, union spoolss_Field, 1);
267         t1.types[1].fields[0].field = PRINTER_NOTIFY_FIELD_PRINTER_NAME;
268
269         r.in.flags = 0;
270         r.in.local_machine = talloc_asprintf(tctx, "\\\\%s", address);
271         r.in.options = 0;
272         r.in.printer_local = 123;
273         r.in.notify_options = &t1;
274         r.in.handle = handle;
275
276         torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_RemoteFindFirstPrinterChangeNotifyEx(p, tctx, &r),
277                 "RemoteFindFirstPrinterChangeNotifyEx failed");
278         torture_assert_werr_ok(tctx, r.out.result,
279                 "error return code for RemoteFindFirstPrinterChangeNotifyEx");
280
281         return true;
282 }
283
284 static bool test_ClosePrinter(struct torture_context *tctx,
285                               struct dcerpc_pipe *p,
286                               struct policy_handle *handle)
287 {
288         struct spoolss_ClosePrinter r;
289
290         r.in.handle = handle;
291         r.out.handle = handle;
292
293         torture_comment(tctx, "Testing ClosePrinter\n");
294
295         torture_assert_ntstatus_ok(tctx, dcerpc_spoolss_ClosePrinter(p, tctx, &r),
296                 "ClosePrinter failed");
297         torture_assert_werr_ok(tctx, r.out.result,
298                 "ClosePrinter failed");
299
300         return true;
301 }
302
303 static bool test_start_dcerpc_server(struct torture_context *tctx,
304                                      struct tevent_context *event_ctx,
305                                      struct dcesrv_context **dce_ctx_p,
306                                      const char **address_p)
307 {
308         struct dcesrv_endpoint_server ep_server;
309         NTSTATUS status;
310         struct dcesrv_context *dce_ctx;
311         const char *endpoints[] = { "spoolss", NULL };
312         struct dcesrv_endpoint *e;
313         const char *address;
314         struct interface *ifaces;
315
316         ntvfs_init(tctx->lp_ctx);
317
318         /* fill in our name */
319         ep_server.name = "spoolss";
320
321         /* fill in all the operations */
322         ep_server.init_server = spoolss__op_init_server;
323
324         ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
325         ep_server.interface_by_name = spoolss__op_interface_by_name;
326
327         torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
328                                   "unable to register spoolss server");
329
330         lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
331
332         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
333         address = iface_n_ip(ifaces, 0);
334
335         torture_comment(tctx, "Listening for callbacks on %s\n", address);
336
337         status = smbsrv_add_socket(event_ctx, tctx->lp_ctx, &single_ops, address);
338         torture_assert_ntstatus_ok(tctx, status, "starting smb server");
339
340         status = dcesrv_init_context(tctx, tctx->lp_ctx, endpoints, &dce_ctx);
341         torture_assert_ntstatus_ok(tctx, status,
342                                    "unable to initialize DCE/RPC server");
343
344         for (e=dce_ctx->endpoint_list;e;e=e->next) {
345                 status = dcesrv_add_ep(dce_ctx, tctx->lp_ctx,
346                                        e, tctx->ev, &single_ops);
347                 torture_assert_ntstatus_ok(tctx, status,
348                                 "unable listen on dcerpc endpoint server");
349         }
350
351         *dce_ctx_p = dce_ctx;
352         *address_p = address;
353
354         return true;
355 }
356
357 static struct received_packet *last_packet(struct received_packet *p)
358 {
359         struct received_packet *tmp;
360         for (tmp = p; tmp->next; tmp = tmp->next) ;;
361         return tmp;
362 }
363
364 static bool test_RFFPCNEx(struct torture_context *tctx,
365                           struct dcerpc_pipe *p)
366 {
367         struct dcesrv_context *dce_ctx;
368         struct policy_handle handle;
369         const char *address;
370         struct received_packet *tmp;
371
372         received_packets = NULL;
373
374         /* Start DCE/RPC server */
375         torture_assert(tctx, test_start_dcerpc_server(tctx, p->conn->event_ctx, &dce_ctx, &address), "");
376
377         torture_assert(tctx, test_OpenPrinter(tctx, p, &handle), "");
378         torture_assert(tctx, test_RemoteFindFirstPrinterChangeNotifyEx(tctx, p, &handle, address), "");
379         torture_assert(tctx, received_packets, "no packets received");
380         torture_assert_int_equal(tctx, received_packets->opnum, NDR_SPOOLSS_REPLYOPENPRINTER,
381                 "no ReplyOpenPrinter packet after RemoteFindFirstPrinterChangeNotifyEx");
382         torture_assert(tctx, test_ClosePrinter(tctx, p, &handle), "");
383         tmp = last_packet(received_packets);
384         torture_assert_int_equal(tctx, tmp->opnum, NDR_SPOOLSS_REPLYCLOSEPRINTER,
385                 "no ReplyClosePrinter packet after ClosePrinter");
386
387         /* Shut down DCE/RPC server */
388         talloc_free(dce_ctx);
389
390         return true;
391 }
392
393 /** Test that makes sure that calling ReplyOpenPrinter()
394  * on Samba 4 will cause an irpc broadcast call.
395  */
396 static bool test_ReplyOpenPrinter(struct torture_context *tctx,
397                                   struct dcerpc_pipe *p)
398 {
399         struct spoolss_ReplyOpenPrinter r;
400         struct spoolss_ReplyClosePrinter s;
401         struct policy_handle h;
402
403         if (torture_setting_bool(tctx, "samba3", false)) {
404                 torture_skip(tctx, "skipping ReplyOpenPrinter server implementation test against s3\n");
405         }
406
407         r.in.server_name = "earth";
408         r.in.printer_local = 2;
409         r.in.type = REG_DWORD;
410         r.in.bufsize = 0;
411         r.in.buffer = NULL;
412         r.out.handle = &h;
413
414         torture_assert_ntstatus_ok(tctx,
415                         dcerpc_spoolss_ReplyOpenPrinter(p, tctx, &r),
416                         "spoolss_ReplyOpenPrinter call failed");
417
418         torture_assert_werr_ok(tctx, r.out.result, "error return code");
419
420         s.in.handle = &h;
421         s.out.handle = &h;
422
423         torture_assert_ntstatus_ok(tctx,
424                         dcerpc_spoolss_ReplyClosePrinter(p, tctx, &s),
425                         "spoolss_ReplyClosePrinter call failed");
426
427         torture_assert_werr_ok(tctx, r.out.result, "error return code");
428
429         return true;
430 }
431
432 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
433 {
434         struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
435
436         struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite,
437                                                         "notify", &ndr_table_spoolss);
438
439         torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
440         torture_rpc_tcase_add_test(tcase, "testReplyOpenPrinter", test_ReplyOpenPrinter);
441
442         return suite;
443 }