s4:torture: the spoolss notify test should listen on the ncacn_np endpoint
[metze/samba/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/torture.h"
25 #include "torture/rpc/rpc.h"
26 #include "librpc/gen_ndr/ndr_spoolss_c.h"
27 #include "rpc_server/dcerpc_server.h"
28 #include "rpc_server/service_rpc.h"
29 #include "lib/events/events.h"
30 #include "smbd/process_model.h"
31 #include "smb_server/smb_server.h"
32 #include "librpc/rpc/dcerpc_proto.h"
33 #include "lib/socket/netif.h"
34 #include "../lib/util/dlinklist.h"
35 #include "ntvfs/ntvfs.h"
36 #include "param/param.h"
37
38 static NTSTATUS spoolss__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
39 {
40         return NT_STATUS_OK;
41 }
42
43 static void spoolss__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
44 {
45 }
46
47 static NTSTATUS spoolss__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
48 {
49         enum ndr_err_code ndr_err;
50         uint16_t opnum = dce_call->pkt.u.request.opnum;
51
52         dce_call->fault_code = 0;
53
54         if (opnum >= ndr_table_spoolss.num_calls) {
55                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
56                 return NT_STATUS_NET_WRITE_FAULT;
57         }
58
59         *r = talloc_size(mem_ctx, ndr_table_spoolss.calls[opnum].struct_size);
60         NT_STATUS_HAVE_NO_MEMORY(*r);
61
62         /* unravel the NDR for the packet */
63         ndr_err = ndr_table_spoolss.calls[opnum].ndr_pull(pull, NDR_IN, *r);
64         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
65                 dcerpc_log_packet(dce_call->conn->packet_log_dir,
66                                                   &ndr_table_spoolss, opnum, NDR_IN,
67                                   &dce_call->pkt.u.request.stub_and_verifier);
68                 dce_call->fault_code = DCERPC_FAULT_NDR;
69                 return NT_STATUS_NET_WRITE_FAULT;
70         }
71
72         return NT_STATUS_OK;
73 }
74
75 /* Note that received_packets are allocated in talloc_autofree_context(), 
76  * because no other context appears to stay around long enough. */
77 static struct received_packet {
78         uint16_t opnum;
79         void *r;
80         struct received_packet *prev, *next;
81 } *received_packets = NULL;
82
83
84 static NTSTATUS spoolss__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
85 {
86         uint16_t opnum = dce_call->pkt.u.request.opnum;
87         struct received_packet *rp;
88
89         rp = talloc_zero(talloc_autofree_context(), struct received_packet);
90         rp->opnum = opnum;
91         rp->r = talloc_reference(rp, r);
92
93         DLIST_ADD_END(received_packets, rp, struct received_packet *);
94
95         switch (opnum) {
96         case 58: {
97                 struct spoolss_ReplyOpenPrinter *r2 = (struct spoolss_ReplyOpenPrinter *)r;
98                 r2->out.result = WERR_OK;
99                 break;
100         }
101
102         default:
103                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
104                 break;
105         }
106
107         if (dce_call->fault_code != 0) {
108                 dcerpc_log_packet(dce_call->conn->packet_log_dir,
109                                                   &ndr_table_spoolss, opnum, NDR_IN,
110                                   &dce_call->pkt.u.request.stub_and_verifier);
111                 return NT_STATUS_NET_WRITE_FAULT;
112         }
113         return NT_STATUS_OK;
114 }
115
116
117 static NTSTATUS spoolss__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
118 {
119         return NT_STATUS_OK;
120 }
121
122
123 static NTSTATUS spoolss__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
124 {
125         enum ndr_err_code ndr_err;
126         uint16_t opnum = dce_call->pkt.u.request.opnum;
127
128         ndr_err = ndr_table_spoolss.calls[opnum].ndr_push(push, NDR_OUT, r);
129         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
130                 dce_call->fault_code = DCERPC_FAULT_NDR;
131                 return NT_STATUS_NET_WRITE_FAULT;
132         }
133
134         return NT_STATUS_OK;
135 }
136
137 const static struct dcesrv_interface notify_test_spoolss_interface = {
138         .name           = "spoolss",
139         .syntax_id  = {{0x12345678,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xab}},1.0},
140         .bind           = spoolss__op_bind,
141         .unbind         = spoolss__op_unbind,
142         .ndr_pull       = spoolss__op_ndr_pull,
143         .dispatch       = spoolss__op_dispatch,
144         .reply          = spoolss__op_reply,
145         .ndr_push       = spoolss__op_ndr_push
146 };
147
148 static bool spoolss__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
149 {
150         if (notify_test_spoolss_interface.syntax_id.if_version == if_version &&
151                 GUID_equal(&notify_test_spoolss_interface.syntax_id.uuid, uuid)) {
152                 memcpy(iface,&notify_test_spoolss_interface, sizeof(*iface));
153                 return true;
154         }
155
156         return false;
157 }
158
159 static bool spoolss__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
160 {
161         if (strcmp(notify_test_spoolss_interface.name, name)==0) {
162                 memcpy(iface, &notify_test_spoolss_interface, sizeof(*iface));
163                 return true;
164         }
165
166         return false;   
167 }
168
169 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
170 {
171         int i;
172
173         for (i=0;i<ndr_table_spoolss.endpoints->count;i++) {
174                 NTSTATUS ret;
175                 const char *name = ndr_table_spoolss.endpoints->names[i];
176
177                 ret = dcesrv_interface_register(dce_ctx, name, &notify_test_spoolss_interface, NULL);
178                 if (!NT_STATUS_IS_OK(ret)) {
179                         DEBUG(1,("spoolss_op_init_server: failed to register endpoint '%s'\n",name));
180                         return ret;
181                 }
182         }
183
184         return NT_STATUS_OK;
185 }
186
187 static bool test_RFFPCNEx(struct torture_context *tctx, 
188                           struct dcerpc_pipe *p)
189 {
190         struct spoolss_OpenPrinter q;
191         struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
192         struct dcesrv_endpoint_server ep_server;
193         NTSTATUS status;
194         struct dcesrv_context *dce_ctx;
195         const char *endpoints[] = { "spoolss", NULL };
196         struct dcesrv_endpoint *e;
197         struct spoolss_NotifyOption t1;
198         struct spoolss_ClosePrinter cp;
199
200         struct policy_handle handle;
201         const char *address;
202         struct interface *ifaces;
203
204         received_packets = NULL;
205
206         ntvfs_init(tctx->lp_ctx);
207
208         ZERO_STRUCT(q);
209
210         q.in.printername        = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
211         q.in.datatype           = NULL;
212         q.in.devmode_ctr.devmode= NULL;
213         q.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
214         q.out.handle            = &handle;
215
216         torture_comment(tctx, "Testing OpenPrinter(%s)\n", q.in.printername);
217
218         status = dcerpc_spoolss_OpenPrinter(p, tctx, &q);
219
220         torture_assert_ntstatus_ok(tctx, status, "OpenPrinter failed");
221
222         torture_assert_werr_ok(tctx, q.out.result, "OpenPrinter failed");
223
224         /* Start DCE/RPC server */
225
226         /* fill in our name */
227         ep_server.name = "spoolss";
228
229         /* fill in all the operations */
230         ep_server.init_server = spoolss__op_init_server;
231
232         ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
233         ep_server.interface_by_name = spoolss__op_interface_by_name;
234
235         torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
236                                   "unable to register spoolss server");
237
238         lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
239
240         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
241         address = iface_n_ip(ifaces, 0);
242         torture_comment(tctx, "Listening for callbacks on %s\n", address);
243         status = smbsrv_add_socket(p->conn->event_ctx, tctx->lp_ctx, &single_ops, address);
244         torture_assert_ntstatus_ok(tctx, status, "starting smb server");
245
246         status = dcesrv_init_context(tctx, tctx->lp_ctx, endpoints, &dce_ctx);
247         torture_assert_ntstatus_ok(tctx, status, 
248                                    "unable to initialize DCE/RPC server");
249
250         /* Make sure the directory for NCALRPC exists */
251         if (!directory_exist(lp_ncalrpc_dir(tctx->lp_ctx))) {
252                 int ret;
253                 ret = mkdir(lp_ncalrpc_dir(tctx->lp_ctx), 0755);
254                 torture_assert(tctx, (ret == 0), talloc_asprintf(tctx,
255                                "failed to mkdir(%s) ret[%d] errno[%d - %s]",
256                                lp_ncalrpc_dir(tctx->lp_ctx), ret,
257                                errno, strerror(errno)));
258         }
259
260         for (e=dce_ctx->endpoint_list;e;e=e->next) {
261                 status = dcesrv_add_ep(dce_ctx, tctx->lp_ctx,
262                                        e, tctx->ev, &single_ops);
263                 torture_assert_ntstatus_ok(tctx, status,
264                                 "unable listen on dcerpc endpoint server");
265         }
266
267         r.in.flags = 0;
268         r.in.local_machine = talloc_asprintf(tctx, "\\\\%s", address);
269         r.in.options = 0;
270         r.in.printer_local = 123;
271         t1.version = 2;
272         t1.flags = 0;
273         t1.count = 2;
274         t1.types = talloc_zero_array(tctx, struct spoolss_NotifyOptionType, 2);
275         t1.types[0].type = PRINTER_NOTIFY_TYPE;
276         t1.types[0].count = 1;
277         t1.types[0].fields = talloc_array(t1.types, union spoolss_Field, 1);
278         t1.types[0].fields[0].field = PRINTER_NOTIFY_FIELD_SERVER_NAME;
279
280         t1.types[1].type = JOB_NOTIFY_TYPE;
281         t1.types[1].count = 1;
282         t1.types[1].fields = talloc_array(t1.types, union spoolss_Field, 1);
283         t1.types[1].fields[0].field = PRINTER_NOTIFY_FIELD_PRINTER_NAME;
284
285         r.in.notify_options = &t1;
286         r.in.handle = &handle;
287
288         status = dcerpc_spoolss_RemoteFindFirstPrinterChangeNotifyEx(p, tctx, &r);
289
290         torture_assert_ntstatus_ok(tctx, status, "FFPCNEx failed");
291         
292         torture_assert_werr_ok(tctx, r.out.result, "error return code for FFPCNEx");
293
294         cp.in.handle = &handle;
295         cp.out.handle = &handle;
296
297         torture_comment(tctx, "Testing ClosePrinter\n");
298
299         status = dcerpc_spoolss_ClosePrinter(p, tctx, &cp);
300         torture_assert_ntstatus_ok(tctx, status, "ClosePrinter failed");
301
302         /* We should've had an incoming packet 58 (ReplyOpenPrinter) */
303         torture_assert(tctx, received_packets != NULL, "no packets received");
304         torture_assert_int_equal(tctx, received_packets->opnum, 58, "invalid opnum");
305
306         /* Shut down DCE/RPC server */
307         talloc_free(dce_ctx);
308
309         return true;
310 }
311
312 /** Test that makes sure that calling ReplyOpenPrinter()
313  * on Samba 4 will cause an irpc broadcast call.
314  */
315 static bool test_ReplyOpenPrinter(struct torture_context *tctx,
316                                   struct dcerpc_pipe *p)
317 {
318         struct spoolss_ReplyOpenPrinter r;
319         struct spoolss_ReplyClosePrinter s;
320         struct policy_handle h;
321
322         r.in.server_name = "earth";
323         r.in.printer_local = 2;
324         r.in.type = REG_DWORD;
325         r.in.bufsize = 0;
326         r.in.buffer = NULL;
327         r.out.handle = &h;
328
329         torture_assert_ntstatus_ok(tctx,
330                         dcerpc_spoolss_ReplyOpenPrinter(p, tctx, &r),
331                         "spoolss_ReplyOpenPrinter call failed");
332
333         torture_assert_werr_ok(tctx, r.out.result, "error return code");
334
335         s.in.handle = &h;
336         s.out.handle = &h;
337
338         torture_assert_ntstatus_ok(tctx,
339                         dcerpc_spoolss_ReplyClosePrinter(p, tctx, &s),
340                         "spoolss_ReplyClosePrinter call failed");
341
342         torture_assert_werr_ok(tctx, r.out.result, "error return code");
343
344         return true;
345 }
346
347 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
348 {
349         struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
350         
351         struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite, 
352                                                         "notify", &ndr_table_spoolss);
353
354         torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
355         torture_rpc_tcase_add_test(tcase, "testReplyOpenPrinter", test_ReplyOpenPrinter);
356         
357         return suite;
358 }