2 Unix SMB/CIFS implementation.
3 test suite for spoolss rpc notify operations
5 Copyright (C) Jelmer Vernooij 2007
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.
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.
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.
23 #include "system/filesys.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 "rpc_server/service_rpc.h"
28 #include "smbd/process_model.h"
29 #include "smb_server/smb_server.h"
30 #include "lib/socket/netif.h"
31 #include "ntvfs/ntvfs.h"
32 #include "param/param.h"
34 static NTSTATUS
spoolss__op_bind(struct dcesrv_call_state
*dce_call
, const struct dcesrv_interface
*iface
)
39 static void spoolss__op_unbind(struct dcesrv_connection_context
*context
, const struct dcesrv_interface
*iface
)
43 static NTSTATUS
spoolss__op_ndr_pull(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, struct ndr_pull
*pull
, void **r
)
45 enum ndr_err_code ndr_err
;
46 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
48 dce_call
->fault_code
= 0;
50 if (opnum
>= ndr_table_spoolss
.num_calls
) {
51 dce_call
->fault_code
= DCERPC_FAULT_OP_RNG_ERROR
;
52 return NT_STATUS_NET_WRITE_FAULT
;
55 *r
= talloc_size(mem_ctx
, ndr_table_spoolss
.calls
[opnum
].struct_size
);
56 NT_STATUS_HAVE_NO_MEMORY(*r
);
58 /* unravel the NDR for the packet */
59 ndr_err
= ndr_table_spoolss
.calls
[opnum
].ndr_pull(pull
, NDR_IN
, *r
);
60 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
61 dcerpc_log_packet(dce_call
->conn
->packet_log_dir
,
62 &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
;
71 /* Note that received_packets are allocated in talloc_autofree_context(),
72 * because no other context appears to stay around long enough. */
73 static struct received_packet
{
76 struct received_packet
*prev
, *next
;
77 } *received_packets
= NULL
;
80 static NTSTATUS
spoolss__op_dispatch(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, void *r
)
82 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
83 struct received_packet
*rp
;
85 rp
= talloc_zero(talloc_autofree_context(), struct received_packet
);
87 rp
->r
= talloc_reference(rp
, r
);
89 DLIST_ADD_END(received_packets
, rp
, struct received_packet
*);
93 struct spoolss_ReplyOpenPrinter
*r2
= (struct spoolss_ReplyOpenPrinter
*)r
;
94 r2
->out
.result
= WERR_OK
;
99 dce_call
->fault_code
= DCERPC_FAULT_OP_RNG_ERROR
;
103 if (dce_call
->fault_code
!= 0) {
104 dcerpc_log_packet(dce_call
->conn
->packet_log_dir
,
105 &ndr_table_spoolss
, opnum
, NDR_IN
,
106 &dce_call
->pkt
.u
.request
.stub_and_verifier
);
107 return NT_STATUS_NET_WRITE_FAULT
;
113 static NTSTATUS
spoolss__op_reply(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, void *r
)
119 static NTSTATUS
spoolss__op_ndr_push(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
, struct ndr_push
*push
, const void *r
)
121 enum ndr_err_code ndr_err
;
122 uint16_t opnum
= dce_call
->pkt
.u
.request
.opnum
;
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
;
133 const static struct dcesrv_interface notify_test_spoolss_interface
= {
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
144 static bool spoolss__op_interface_by_uuid(struct dcesrv_interface
*iface
, const struct GUID
*uuid
, uint32_t if_version
)
146 if (notify_test_spoolss_interface
.syntax_id
.if_version
== if_version
&&
147 GUID_equal(¬ify_test_spoolss_interface
.syntax_id
.uuid
, uuid
)) {
148 memcpy(iface
,¬ify_test_spoolss_interface
, sizeof(*iface
));
155 static bool spoolss__op_interface_by_name(struct dcesrv_interface
*iface
, const char *name
)
157 if (strcmp(notify_test_spoolss_interface
.name
, name
)==0) {
158 memcpy(iface
, ¬ify_test_spoolss_interface
, sizeof(*iface
));
165 static NTSTATUS
spoolss__op_init_server(struct dcesrv_context
*dce_ctx
, const struct dcesrv_endpoint_server
*ep_server
)
169 for (i
=0;i
<ndr_table_spoolss
.endpoints
->count
;i
++) {
171 const char *name
= ndr_table_spoolss
.endpoints
->names
[i
];
173 ret
= dcesrv_interface_register(dce_ctx
, name
, ¬ify_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
));
183 static bool test_RFFPCNEx(struct torture_context
*tctx
,
184 struct dcerpc_pipe
*p
)
186 struct spoolss_OpenPrinter q
;
187 struct spoolss_RemoteFindFirstPrinterChangeNotifyEx r
;
188 struct dcesrv_endpoint_server ep_server
;
190 struct dcesrv_context
*dce_ctx
;
191 const char *endpoints
[] = { "spoolss", NULL
};
192 struct dcesrv_endpoint
*e
;
193 struct spoolss_NotifyOption t1
;
194 struct spoolss_ClosePrinter cp
;
196 struct policy_handle handle
;
198 struct interface
*ifaces
;
200 received_packets
= NULL
;
202 ntvfs_init(tctx
->lp_ctx
);
206 q
.in
.printername
= talloc_asprintf(tctx
, "\\\\%s", dcerpc_server_name(p
));
207 q
.in
.datatype
= NULL
;
208 q
.in
.devmode_ctr
.devmode
= NULL
;
209 q
.in
.access_mask
= SEC_FLAG_MAXIMUM_ALLOWED
;
210 q
.out
.handle
= &handle
;
212 torture_comment(tctx
, "Testing OpenPrinter(%s)\n", q
.in
.printername
);
214 status
= dcerpc_spoolss_OpenPrinter(p
, tctx
, &q
);
216 torture_assert_ntstatus_ok(tctx
, status
, "OpenPrinter failed");
218 torture_assert_werr_ok(tctx
, q
.out
.result
, "OpenPrinter failed");
220 /* Start DCE/RPC server */
222 /* fill in our name */
223 ep_server
.name
= "spoolss";
225 /* fill in all the operations */
226 ep_server
.init_server
= spoolss__op_init_server
;
228 ep_server
.interface_by_uuid
= spoolss__op_interface_by_uuid
;
229 ep_server
.interface_by_name
= spoolss__op_interface_by_name
;
231 torture_assert_ntstatus_ok(tctx
, dcerpc_register_ep_server(&ep_server
),
232 "unable to register spoolss server");
234 lp_set_cmdline(tctx
->lp_ctx
, "dcerpc endpoint servers", "spoolss");
236 load_interfaces(tctx
, lp_interfaces(tctx
->lp_ctx
), &ifaces
);
237 address
= iface_n_ip(ifaces
, 0);
238 torture_comment(tctx
, "Listening for callbacks on %s\n", address
);
239 status
= smbsrv_add_socket(p
->conn
->event_ctx
, tctx
->lp_ctx
, &single_ops
, address
);
240 torture_assert_ntstatus_ok(tctx
, status
, "starting smb server");
242 status
= dcesrv_init_context(tctx
, tctx
->lp_ctx
, endpoints
, &dce_ctx
);
243 torture_assert_ntstatus_ok(tctx
, status
,
244 "unable to initialize DCE/RPC server");
246 /* Make sure the directory for NCALRPC exists */
247 if (!directory_exist(lp_ncalrpc_dir(tctx
->lp_ctx
))) {
249 ret
= mkdir(lp_ncalrpc_dir(tctx
->lp_ctx
), 0755);
250 torture_assert(tctx
, (ret
== 0), talloc_asprintf(tctx
,
251 "failed to mkdir(%s) ret[%d] errno[%d - %s]",
252 lp_ncalrpc_dir(tctx
->lp_ctx
), ret
,
253 errno
, strerror(errno
)));
256 for (e
=dce_ctx
->endpoint_list
;e
;e
=e
->next
) {
257 status
= dcesrv_add_ep(dce_ctx
, tctx
->lp_ctx
,
258 e
, tctx
->ev
, &single_ops
);
259 torture_assert_ntstatus_ok(tctx
, status
,
260 "unable listen on dcerpc endpoint server");
264 r
.in
.local_machine
= talloc_asprintf(tctx
, "\\\\%s", address
);
266 r
.in
.printer_local
= 123;
270 t1
.types
= talloc_zero_array(tctx
, struct spoolss_NotifyOptionType
, 2);
271 t1
.types
[0].type
= PRINTER_NOTIFY_TYPE
;
272 t1
.types
[0].count
= 1;
273 t1
.types
[0].fields
= talloc_array(t1
.types
, union spoolss_Field
, 1);
274 t1
.types
[0].fields
[0].field
= PRINTER_NOTIFY_FIELD_SERVER_NAME
;
276 t1
.types
[1].type
= JOB_NOTIFY_TYPE
;
277 t1
.types
[1].count
= 1;
278 t1
.types
[1].fields
= talloc_array(t1
.types
, union spoolss_Field
, 1);
279 t1
.types
[1].fields
[0].field
= PRINTER_NOTIFY_FIELD_PRINTER_NAME
;
281 r
.in
.notify_options
= &t1
;
282 r
.in
.handle
= &handle
;
284 status
= dcerpc_spoolss_RemoteFindFirstPrinterChangeNotifyEx(p
, tctx
, &r
);
286 torture_assert_ntstatus_ok(tctx
, status
, "FFPCNEx failed");
288 torture_assert_werr_ok(tctx
, r
.out
.result
, "error return code for FFPCNEx");
290 cp
.in
.handle
= &handle
;
291 cp
.out
.handle
= &handle
;
293 torture_comment(tctx
, "Testing ClosePrinter\n");
295 status
= dcerpc_spoolss_ClosePrinter(p
, tctx
, &cp
);
296 torture_assert_ntstatus_ok(tctx
, status
, "ClosePrinter failed");
298 /* We should've had an incoming packet 58 (ReplyOpenPrinter) */
299 torture_assert(tctx
, received_packets
!= NULL
, "no packets received");
300 torture_assert_int_equal(tctx
, received_packets
->opnum
, 58, "invalid opnum");
302 /* Shut down DCE/RPC server */
303 talloc_free(dce_ctx
);
308 /** Test that makes sure that calling ReplyOpenPrinter()
309 * on Samba 4 will cause an irpc broadcast call.
311 static bool test_ReplyOpenPrinter(struct torture_context
*tctx
,
312 struct dcerpc_pipe
*p
)
314 struct spoolss_ReplyOpenPrinter r
;
315 struct spoolss_ReplyClosePrinter s
;
316 struct policy_handle h
;
318 r
.in
.server_name
= "earth";
319 r
.in
.printer_local
= 2;
320 r
.in
.type
= REG_DWORD
;
325 torture_assert_ntstatus_ok(tctx
,
326 dcerpc_spoolss_ReplyOpenPrinter(p
, tctx
, &r
),
327 "spoolss_ReplyOpenPrinter call failed");
329 torture_assert_werr_ok(tctx
, r
.out
.result
, "error return code");
334 torture_assert_ntstatus_ok(tctx
,
335 dcerpc_spoolss_ReplyClosePrinter(p
, tctx
, &s
),
336 "spoolss_ReplyClosePrinter call failed");
338 torture_assert_werr_ok(tctx
, r
.out
.result
, "error return code");
343 struct torture_suite
*torture_rpc_spoolss_notify(TALLOC_CTX
*mem_ctx
)
345 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "SPOOLSS-NOTIFY");
347 struct torture_rpc_tcase
*tcase
= torture_suite_add_rpc_iface_tcase(suite
,
348 "notify", &ndr_table_spoolss
);
350 torture_rpc_tcase_add_test(tcase
, "testRFFPCNEx", test_RFFPCNEx
);
351 torture_rpc_tcase_add_test(tcase
, "testReplyOpenPrinter", test_ReplyOpenPrinter
);