2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1998,
5 * Largely re-written : 2005
6 * Copyright (C) Jeremy Allison 1998 - 2005
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "fake_file.h"
26 #include "rpc_server/rpc_ncacn_np.h"
27 #include "rpc_server/srv_pipe_hnd.h"
28 #include "rpc_client/local_np.h"
29 #include "rpc_server/rpc_server.h"
30 #include "rpc_server/rpc_config.h"
31 #include "../lib/tsocket/tsocket.h"
32 #include "../lib/util/tevent_ntstatus.h"
33 #include "librpc/ndr/ndr_table.h"
36 #define DBGC_CLASS DBGC_RPC_SRV
38 bool fsp_is_np(struct files_struct
*fsp
)
40 enum FAKE_FILE_TYPE type
;
42 if ((fsp
== NULL
) || (fsp
->fake_file_handle
== NULL
)) {
46 type
= fsp
->fake_file_handle
->type
;
48 return (type
== FAKE_FILE_TYPE_NAMED_PIPE_PROXY
);
51 NTSTATUS
np_open(TALLOC_CTX
*mem_ctx
, const char *name
,
52 const struct tsocket_address
*remote_client_address
,
53 const struct tsocket_address
*local_server_address
,
54 struct auth_session_info
*session_info
,
55 struct tevent_context
*ev_ctx
,
56 struct messaging_context
*msg_ctx
,
57 struct dcesrv_context
*dce_ctx
,
58 struct fake_file_handle
**phandle
)
60 struct fake_file_handle
*handle
;
61 struct npa_state
*npa
= NULL
;
64 handle
= talloc(mem_ctx
, struct fake_file_handle
);
66 return NT_STATUS_NO_MEMORY
;
69 npa
= npa_state_init(handle
);
72 return NT_STATUS_NO_MEMORY
;
74 *handle
= (struct fake_file_handle
) {
75 .type
= FAKE_FILE_TYPE_NAMED_PIPE_PROXY
,
79 ret
= local_np_connect(
83 remote_client_address
,
91 DBG_DEBUG("local_np_connect failed: %s\n",
94 return map_nt_error_from_unix(ret
);
102 bool np_read_in_progress(struct fake_file_handle
*handle
)
104 if (handle
->type
== FAKE_FILE_TYPE_NAMED_PIPE_PROXY
) {
105 struct npa_state
*p
=
106 talloc_get_type_abort(handle
->private_data
,
110 read_count
= tevent_queue_length(p
->read_queue
);
111 if (read_count
> 0) {
121 struct np_write_state
{
122 struct tevent_context
*ev
;
128 static void np_write_done(struct tevent_req
*subreq
);
130 struct tevent_req
*np_write_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
131 struct fake_file_handle
*handle
,
132 const uint8_t *data
, size_t len
)
134 struct tevent_req
*req
;
135 struct np_write_state
*state
;
136 struct npa_state
*p
= NULL
;
137 struct tevent_req
*subreq
= NULL
;
139 DBG_INFO("len: %zu\n", len
);
140 dump_data(50, data
, len
);
142 req
= tevent_req_create(mem_ctx
, &state
, struct np_write_state
);
147 if (handle
->type
!= FAKE_FILE_TYPE_NAMED_PIPE_PROXY
) {
148 tevent_req_nterror(req
, NT_STATUS_INVALID_HANDLE
);
149 return tevent_req_post(req
, ev
);
154 tevent_req_done(req
);
155 return tevent_req_post(req
, ev
);
158 p
= talloc_get_type_abort(handle
->private_data
, struct npa_state
);
162 state
->iov
.iov_base
= discard_const_p(void, data
);
163 state
->iov
.iov_len
= len
;
165 subreq
= tstream_writev_queue_send(
166 state
, ev
, p
->stream
, p
->write_queue
, &state
->iov
, 1);
167 if (tevent_req_nomem(subreq
, req
)) {
168 return tevent_req_post(req
, ev
);
170 tevent_req_set_callback(subreq
, np_write_done
, req
);
174 static void np_write_done(struct tevent_req
*subreq
)
176 struct tevent_req
*req
= tevent_req_callback_data(
177 subreq
, struct tevent_req
);
178 struct np_write_state
*state
= tevent_req_data(
179 req
, struct np_write_state
);
183 received
= tstream_writev_queue_recv(subreq
, &err
);
185 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
188 state
->nwritten
= received
;
189 tevent_req_done(req
);
192 NTSTATUS
np_write_recv(struct tevent_req
*req
, ssize_t
*pnwritten
)
194 struct np_write_state
*state
= tevent_req_data(
195 req
, struct np_write_state
);
198 if (tevent_req_is_nterror(req
, &status
)) {
201 *pnwritten
= state
->nwritten
;
205 struct np_ipc_readv_next_vector_state
{
212 static void np_ipc_readv_next_vector_init(struct np_ipc_readv_next_vector_state
*s
,
213 uint8_t *buf
, size_t len
)
218 s
->len
= MIN(len
, UINT16_MAX
);
221 static int np_ipc_readv_next_vector(struct tstream_context
*stream
,
224 struct iovec
**_vector
,
227 struct np_ipc_readv_next_vector_state
*state
=
228 (struct np_ipc_readv_next_vector_state
*)private_data
;
229 struct iovec
*vector
;
233 if (state
->ofs
== state
->len
) {
239 pending
= tstream_pending_bytes(stream
);
244 if (pending
== 0 && state
->ofs
!= 0) {
245 /* return a short read */
252 /* we want at least one byte and recheck again */
255 size_t missing
= state
->len
- state
->ofs
;
256 if (pending
> missing
) {
257 /* there's more available */
258 state
->remaining
= pending
- missing
;
261 /* read what we can get and recheck in the next cycle */
266 vector
= talloc_array(mem_ctx
, struct iovec
, 1);
271 vector
[0].iov_base
= state
->buf
+ state
->ofs
;
272 vector
[0].iov_len
= wanted
;
274 state
->ofs
+= wanted
;
281 struct np_read_zero_state
;
283 struct np_read_state
{
285 struct np_ipc_readv_next_vector_state next_vector
;
288 bool is_data_outstanding
;
290 struct np_read_zero_state
*zs
;
293 struct np_read_zero_state
{
294 struct np_read_state
*state
;
295 struct tevent_req
*req
;
298 static int np_read_zero_state_destructor(struct np_read_zero_state
*zs
)
300 if (zs
->state
!= NULL
) {
301 zs
->state
->zs
= NULL
;
304 tevent_req_nterror(zs
->req
, NT_STATUS_PIPE_BROKEN
);
308 static void np_read_send_cleanup(struct tevent_req
*req
,
309 enum tevent_req_state req_state
)
311 struct np_read_state
*state
= tevent_req_data(
312 req
, struct np_read_state
);
314 if (state
->zs
!= NULL
) {
315 talloc_set_destructor(state
->zs
, NULL
);
316 TALLOC_FREE(state
->zs
);
320 static void np_read_done(struct tevent_req
*subreq
);
322 struct tevent_req
*np_read_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
323 struct fake_file_handle
*handle
,
324 uint8_t *data
, size_t len
)
326 struct tevent_req
*req
;
327 struct np_read_state
*state
;
328 struct npa_state
*p
= NULL
;
329 struct tevent_req
*subreq
= NULL
;
331 req
= tevent_req_create(mem_ctx
, &state
, struct np_read_state
);
336 tevent_req_set_cleanup_fn(req
, np_read_send_cleanup
);
338 if (handle
->type
!= FAKE_FILE_TYPE_NAMED_PIPE_PROXY
) {
339 tevent_req_nterror(req
, NT_STATUS_INVALID_HANDLE
);
340 return tevent_req_post(req
, ev
);
343 p
= talloc_get_type_abort(handle
->private_data
, struct npa_state
);
346 state
->zs
= talloc_zero(p
, struct np_read_zero_state
);
347 if (tevent_req_nomem(state
->zs
, req
)) {
348 return tevent_req_post(req
, ev
);
350 talloc_set_destructor(state
->zs
,
351 np_read_zero_state_destructor
);
352 state
->zs
->state
= state
;
353 state
->zs
->req
= req
;
357 np_ipc_readv_next_vector_init(&state
->next_vector
, data
, len
);
359 subreq
= tstream_readv_pdu_queue_send(
364 np_ipc_readv_next_vector
,
365 &state
->next_vector
);
366 if (tevent_req_nomem(subreq
, req
)) {
367 return tevent_req_post(req
, ev
);
369 tevent_req_set_callback(subreq
, np_read_done
, req
);
373 static void np_read_done(struct tevent_req
*subreq
)
375 struct tevent_req
*req
= tevent_req_callback_data(
376 subreq
, struct tevent_req
);
377 struct np_read_state
*state
= tevent_req_data(
378 req
, struct np_read_state
);
382 ret
= tstream_readv_pdu_queue_recv(subreq
, &err
);
385 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
390 state
->is_data_outstanding
= (state
->next_vector
.remaining
> 0);
392 tevent_req_done(req
);
396 NTSTATUS
np_read_recv(struct tevent_req
*req
, ssize_t
*nread
,
397 bool *is_data_outstanding
)
399 struct np_read_state
*state
= tevent_req_data(
400 req
, struct np_read_state
);
403 if (tevent_req_is_nterror(req
, &status
)) {
407 DEBUG(10, ("Received %d bytes. There is %smore data outstanding\n",
408 (int)state
->nread
, state
->is_data_outstanding
?"":"no "));
410 *nread
= state
->nread
;
411 *is_data_outstanding
= state
->is_data_outstanding
;