Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[samba.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 "torture/torture.h"
24 #include "torture/rpc/rpc.h"
25 #include "librpc/gen_ndr/ndr_spoolss_c.h"
26 #include "rpc_server/dcerpc_server.h"
27 #include "lib/events/events.h"
28 #include "smbd/process_model.h"
29 #include "smb_server/smb_server.h"
30 #include "librpc/rpc/dcerpc_proto.h"
31 #include "lib/socket/netif.h"
32 #include "util/dlinklist.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(&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
81 static NTSTATUS spoolss__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
82 {
83         uint16_t opnum = dce_call->pkt.u.request.opnum;
84         struct received_packet *rp;
85
86         rp = talloc_zero(talloc_autofree_context(), struct received_packet);
87         rp->opnum = opnum;
88         rp->r = talloc_reference(rp, r);
89
90         DLIST_ADD_END(received_packets, rp, struct received_packet *);
91
92         switch (opnum) {
93         case 58: {
94                 struct spoolss_ReplyOpenPrinter *r2 = (struct spoolss_ReplyOpenPrinter *)r;
95                 r2->out.result = WERR_OK;
96                 break;
97         }
98
99         default:
100                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
101                 break;
102         }
103
104         if (dce_call->fault_code != 0) {
105                 dcerpc_log_packet(&ndr_table_spoolss, opnum, NDR_IN,
106                                   &dce_call->pkt.u.request.stub_and_verifier);
107                 return NT_STATUS_NET_WRITE_FAULT;
108         }
109         return NT_STATUS_OK;
110 }
111
112
113 static NTSTATUS spoolss__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
114 {
115         return NT_STATUS_OK;
116 }
117
118
119 static NTSTATUS spoolss__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
120 {
121         enum ndr_err_code ndr_err;
122         uint16_t opnum = dce_call->pkt.u.request.opnum;
123
124         ndr_err = ndr_table_spoolss.calls[opnum].ndr_push(push, NDR_OUT, r);
125         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
126                 dce_call->fault_code = DCERPC_FAULT_NDR;
127                 return NT_STATUS_NET_WRITE_FAULT;
128         }
129
130         return NT_STATUS_OK;
131 }
132
133 const static struct dcesrv_interface notify_test_spoolss_interface = {
134         .name           = "spoolss",
135         .syntax_id  = {{0x12345678,0x1234,0xabcd,{0xef,0x00},{0x01,0x23,0x45,0x67,0x89,0xab}},1.0},
136         .bind           = spoolss__op_bind,
137         .unbind         = spoolss__op_unbind,
138         .ndr_pull       = spoolss__op_ndr_pull,
139         .dispatch       = spoolss__op_dispatch,
140         .reply          = spoolss__op_reply,
141         .ndr_push       = spoolss__op_ndr_push
142 };
143
144 static bool spoolss__op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
145 {
146         if (notify_test_spoolss_interface.syntax_id.if_version == if_version &&
147                 GUID_equal(&notify_test_spoolss_interface.syntax_id.uuid, uuid)) {
148                 memcpy(iface,&notify_test_spoolss_interface, sizeof(*iface));
149                 return true;
150         }
151
152         return false;
153 }
154
155 static bool spoolss__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
156 {
157         if (strcmp(notify_test_spoolss_interface.name, name)==0) {
158                 memcpy(iface, &notify_test_spoolss_interface, sizeof(*iface));
159                 return true;
160         }
161
162         return false;   
163 }
164
165 static NTSTATUS spoolss__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
166 {
167         int i;
168
169         for (i=0;i<ndr_table_spoolss.endpoints->count;i++) {
170                 NTSTATUS ret;
171                 const char *name = ndr_table_spoolss.endpoints->names[i];
172
173                 ret = dcesrv_interface_register(dce_ctx, name, &notify_test_spoolss_interface, NULL);
174                 if (!NT_STATUS_IS_OK(ret)) {
175                         DEBUG(1,("spoolss_op_init_server: failed to register endpoint '%s'\n",name));
176                         return ret;
177                 }
178         }
179
180         return NT_STATUS_OK;
181 }
182
183 static bool test_RFFPCNEx(struct torture_context *tctx, 
184                           struct dcerpc_pipe *p)
185 {
186         struct spoolss_OpenPrinter q;
187         struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r;
188         struct dcesrv_endpoint_server ep_server;
189         NTSTATUS status;
190         struct dcesrv_context *dce_ctx;
191         const char *endpoints[] = { "spoolss", NULL };
192         struct spoolss_NotifyOptionsContainer t1;
193         struct spoolss_ClosePrinter cp;
194
195         struct policy_handle handle;
196         const char *address;
197         struct interface *ifaces;
198
199         received_packets = NULL;
200
201         ntvfs_init(tctx->lp_ctx);
202
203         ZERO_STRUCT(q);
204
205         q.in.printername        = talloc_asprintf(tctx, "\\\\%s", dcerpc_server_name(p));
206         q.in.datatype           = NULL;
207         q.in.devmode_ctr.devmode= NULL;
208         q.in.access_mask        = SEC_FLAG_MAXIMUM_ALLOWED;
209         q.out.handle            = &handle;
210
211         torture_comment(tctx, "Testing OpenPrinter(%s)\n", q.in.printername);
212
213         status = dcerpc_spoolss_OpenPrinter(p, tctx, &q);
214
215         torture_assert_ntstatus_ok(tctx, status, "OpenPrinter failed");
216
217         torture_assert_werr_ok(tctx, q.out.result, "OpenPrinter failed");
218
219         /* Start DCE/RPC server */
220
221         /* fill in our name */
222         ep_server.name = "spoolss";
223
224         /* fill in all the operations */
225         ep_server.init_server = spoolss__op_init_server;
226
227         ep_server.interface_by_uuid = spoolss__op_interface_by_uuid;
228         ep_server.interface_by_name = spoolss__op_interface_by_name;
229
230         torture_assert_ntstatus_ok(tctx, dcerpc_register_ep_server(&ep_server),
231                                   "unable to register spoolss server");
232
233         lp_set_cmdline(tctx->lp_ctx, "dcerpc endpoint servers", "spoolss");
234
235         load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
236         address = iface_n_ip(ifaces, 0);
237         torture_comment(tctx, "Listening for callbacks on %s\n", address);
238         status = smbsrv_add_socket(p->conn->event_ctx, tctx->lp_ctx, &single_ops, address);
239         torture_assert_ntstatus_ok(tctx, status, "starting smb server");
240
241         status = dcesrv_init_context(tctx, tctx->lp_ctx, endpoints, &dce_ctx);
242         torture_assert_ntstatus_ok(tctx, status, 
243                                    "unable to initialize DCE/RPC server");
244
245         r.in.flags = 0;
246         r.in.str = talloc_asprintf(tctx, "\\\\%s", address);
247         r.in.options = 0;
248         r.in.printer_local = 123;
249         t1.version = 2;
250         t1.flags = 0;
251         t1.count = 2;
252         t1.options = talloc_zero_array(tctx, struct spoolss_NotifyOptionsArray, 2);
253         t1.options[0].type = SPOOLSS_NOTIFY_PRINTER;
254         t1.options[0].count = 1;
255         t1.options[0].fields = talloc_array(t1.options, enum spoolss_Field, 1);
256         t1.options[0].fields[0] = SPOOLSS_FIELD_SERVER_NAME;
257
258         t1.options[1].type = SPOOLSS_NOTIFY_JOB;
259         t1.options[1].count = 1;
260         t1.options[1].fields = talloc_array(t1.options, enum spoolss_Field, 1);
261         t1.options[1].fields[0] = SPOOLSS_FIELD_PRINTER_NAME;
262
263         r.in.t1 = &t1;
264         r.in.handle = &handle;
265
266         status = dcerpc_spoolss_RemoteFindFirstPrinterChangeNotifyEx(p, tctx, &r);
267
268         torture_assert_ntstatus_ok(tctx, status, "FFPCNEx failed");
269         
270         torture_assert_werr_ok(tctx, r.out.result, "error return code for FFPCNEx");
271
272         cp.in.handle = &handle;
273         cp.out.handle = &handle;
274
275         torture_comment(tctx, "Testing ClosePrinter\n");
276
277         status = dcerpc_spoolss_ClosePrinter(p, tctx, &cp);
278         torture_assert_ntstatus_ok(tctx, status, "ClosePrinter failed");
279
280         /* We should've had an incoming packet 58 (ReplyOpenPrinter) */
281         torture_assert(tctx, received_packets != NULL, "no packets received");
282         torture_assert_int_equal(tctx, received_packets->opnum, 58, "invalid opnum");
283
284         /* Shut down DCE/RPC server */
285         talloc_free(dce_ctx);
286
287         return true;
288 }
289
290 struct torture_suite *torture_rpc_spoolss_notify(TALLOC_CTX *mem_ctx)
291 {
292         struct torture_suite *suite = torture_suite_create(mem_ctx, "SPOOLSS-NOTIFY");
293         
294         struct torture_rpc_tcase *tcase = torture_suite_add_rpc_iface_tcase(suite, 
295                                                         "notify", &ndr_table_spoolss);
296
297         torture_rpc_tcase_add_test(tcase, "testRFFPCNEx", test_RFFPCNEx);
298         
299         return suite;
300 }