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