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