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