2 Unix SMB/CIFS implementation.
3 default IPC$ NTVFS backend
5 Copyright (C) Andrew Tridgell 2003
6 Copyright (C) Stefan (metze) Metzmacher 2004-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/>.
22 this implements the IPC$ backend, called by the NTVFS subsystem to
23 handle requests on IPC$ shares
28 #include "../lib/util/dlinklist.h"
29 #include "ntvfs/ntvfs.h"
30 #include "libcli/rap/rap.h"
31 #include "ntvfs/ipc/proto.h"
32 #include "libcli/raw/ioctl.h"
33 #include "param/param.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../libcli/named_pipe_auth/npa_tstream.h"
36 #include "auth/auth.h"
37 #include "auth/auth_sam_reply.h"
38 #include "lib/socket/socket.h"
39 #include "../lib/util/tevent_unix.h"
40 #include "../lib/util/tevent_coroutine.h"
42 /* this is the private structure used to keep the state of an open
43 ipc$ connection. It needs to keep information about all open
46 struct ntvfs_module_context *ntvfs;
48 /* a list of open pipes */
50 struct pipe_state *next, *prev;
51 struct ipc_private *ipriv;
52 const char *pipe_name;
53 struct ntvfs_handle *handle;
54 struct tstream_context *npipe;
56 uint16_t device_state;
57 uint64_t allocation_size;
58 struct tevent_queue *write_queue;
59 struct tevent_queue *read_queue;
65 find a open pipe give a file handle
67 static struct pipe_state *pipe_state_find(struct ipc_private *ipriv, struct ntvfs_handle *handle)
72 p = ntvfs_handle_get_backend_data(handle, ipriv->ntvfs);
75 s = talloc_get_type(p, struct pipe_state);
82 find a open pipe give a wire fnum
84 static struct pipe_state *pipe_state_find_key(struct ipc_private *ipriv, struct ntvfs_request *req, const DATA_BLOB *key)
86 struct ntvfs_handle *h;
88 h = ntvfs_handle_search_by_wire_key(ipriv->ntvfs, req, key);
91 return pipe_state_find(ipriv, h);
96 connect to a share - always works
98 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
99 struct ntvfs_request *req, const char *sharename)
101 struct ipc_private *ipriv;
103 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "IPC");
104 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
106 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "IPC");
107 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
109 /* prepare the private state for this connection */
110 ipriv = talloc(ntvfs, struct ipc_private);
111 NT_STATUS_HAVE_NO_MEMORY(ipriv);
113 ntvfs->private_data = ipriv;
115 ipriv->ntvfs = ntvfs;
116 ipriv->pipe_list = NULL;
122 disconnect from a share
124 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs)
132 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
133 struct ntvfs_request *req,
134 union smb_unlink *unl)
136 return NT_STATUS_ACCESS_DENIED;
140 check if a directory exists
142 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
143 struct ntvfs_request *req,
144 union smb_chkpath *cp)
146 return NT_STATUS_ACCESS_DENIED;
150 return info on a pathname
152 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
153 struct ntvfs_request *req, union smb_fileinfo *info)
155 switch (info->generic.level) {
156 case RAW_FILEINFO_GENERIC:
157 return NT_STATUS_INVALID_DEVICE_REQUEST;
158 case RAW_FILEINFO_GETATTR:
159 return NT_STATUS_ACCESS_DENIED;
161 return ntvfs_map_qpathinfo(ntvfs, req, info);
166 set info on a pathname
168 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
169 struct ntvfs_request *req, union smb_setfileinfo *st)
171 return NT_STATUS_ACCESS_DENIED;
176 destroy a open pipe structure
178 static int ipc_fd_destructor(struct pipe_state *p)
180 DLIST_REMOVE(p->ipriv->pipe_list, p);
181 ntvfs_handle_remove_backend_data(p->handle, p->ipriv->ntvfs);
185 struct ipc_open_state {
186 struct ipc_private *ipriv;
187 struct pipe_state *p;
188 struct ntvfs_request *req;
190 struct netr_SamInfo3 *info3;
193 static void ipc_open_done(struct tevent_req *subreq);
196 open a file - used for MSRPC pipes
198 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
199 struct ntvfs_request *req, union smb_open *oi)
202 struct pipe_state *p;
203 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
205 struct smb_iconv_convenience *smb_ic
206 = lp_iconv_convenience(ipriv->ntvfs->ctx->lp_ctx);
207 struct ntvfs_handle *h;
208 struct ipc_open_state *state;
209 struct tevent_req *subreq;
211 const char *directory;
212 struct socket_address *client_sa;
213 struct tsocket_address *client_addr;
214 struct socket_address *server_sa;
215 struct tsocket_address *server_addr;
218 switch (oi->generic.level) {
219 case RAW_OPEN_NTCREATEX:
220 fname = oi->ntcreatex.in.fname;
223 fname = oi->openx.in.fname;
226 fname = oi->smb2.in.fname;
229 status = NT_STATUS_NOT_SUPPORTED;
233 directory = talloc_asprintf(req, "%s/np",
234 lp_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
235 NT_STATUS_HAVE_NO_MEMORY(directory);
237 state = talloc(req, struct ipc_open_state);
238 NT_STATUS_HAVE_NO_MEMORY(state);
240 status = ntvfs_handle_new(ntvfs, req, &h);
241 NT_STATUS_NOT_OK_RETURN(status);
243 p = talloc(h, struct pipe_state);
244 NT_STATUS_HAVE_NO_MEMORY(p);
246 while (fname[0] == '\\') fname++;
248 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
249 NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
254 p->write_queue = tevent_queue_create(p, "ipc_write_queue");
255 NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
257 p->read_queue = tevent_queue_create(p, "ipc_read_queue");
258 NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
260 state->ipriv = ipriv;
265 status = auth_convert_server_info_saminfo3(state,
266 req->session_info->server_info,
268 NT_STATUS_NOT_OK_RETURN(status);
270 client_sa = ntvfs_get_peer_addr(ntvfs, state);
272 return NT_STATUS_INTERNAL_ERROR;
275 server_sa = ntvfs_get_my_addr(ntvfs, state);
277 return NT_STATUS_INTERNAL_ERROR;
280 ret = tsocket_address_inet_from_strings(state, "ip",
285 status = map_nt_error_from_unix(errno);
289 ret = tsocket_address_inet_from_strings(state, "ip",
294 status = map_nt_error_from_unix(errno);
298 subreq = tstream_npa_connect_send(p,
299 ipriv->ntvfs->ctx->event_ctx,
308 req->session_info->session_key);
309 NT_STATUS_HAVE_NO_MEMORY(subreq);
310 tevent_req_set_callback(subreq, ipc_open_done, state);
312 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
316 static void ipc_open_done(struct tevent_req *subreq)
318 struct ipc_open_state *state = tevent_req_callback_data(subreq,
319 struct ipc_open_state);
320 struct ipc_private *ipriv = state->ipriv;
321 struct pipe_state *p = state->p;
322 struct ntvfs_request *req = state->req;
323 union smb_open *oi = state->oi;
328 ret = tstream_npa_connect_recv(subreq, &sys_errno,
332 &p->allocation_size);
335 status = map_nt_error_from_unix(sys_errno);
339 DLIST_ADD(ipriv->pipe_list, p);
340 talloc_set_destructor(p, ipc_fd_destructor);
342 status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
343 if (!NT_STATUS_IS_OK(status)) {
347 switch (oi->generic.level) {
348 case RAW_OPEN_NTCREATEX:
349 ZERO_STRUCT(oi->ntcreatex.out);
350 oi->ntcreatex.out.file.ntvfs = p->handle;
351 oi->ntcreatex.out.oplock_level = 0;
352 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
353 oi->ntcreatex.out.create_time = 0;
354 oi->ntcreatex.out.access_time = 0;
355 oi->ntcreatex.out.write_time = 0;
356 oi->ntcreatex.out.change_time = 0;
357 oi->ntcreatex.out.attrib = FILE_ATTRIBUTE_NORMAL;
358 oi->ntcreatex.out.alloc_size = p->allocation_size;
359 oi->ntcreatex.out.size = 0;
360 oi->ntcreatex.out.file_type = p->file_type;
361 oi->ntcreatex.out.ipc_state = p->device_state;
362 oi->ntcreatex.out.is_directory = 0;
365 ZERO_STRUCT(oi->openx.out);
366 oi->openx.out.file.ntvfs = p->handle;
367 oi->openx.out.attrib = FILE_ATTRIBUTE_NORMAL;
368 oi->openx.out.write_time = 0;
369 oi->openx.out.size = 0;
370 oi->openx.out.access = 0;
371 oi->openx.out.ftype = p->file_type;
372 oi->openx.out.devstate = p->device_state;
373 oi->openx.out.action = 0;
374 oi->openx.out.unique_fid = 0;
375 oi->openx.out.access_mask = 0;
376 oi->openx.out.unknown = 0;
379 ZERO_STRUCT(oi->smb2.out);
380 oi->smb2.out.file.ntvfs = p->handle;
381 oi->smb2.out.oplock_level = oi->smb2.in.oplock_level;
382 oi->smb2.out.create_action = NTCREATEX_ACTION_EXISTED;
383 oi->smb2.out.create_time = 0;
384 oi->smb2.out.access_time = 0;
385 oi->smb2.out.write_time = 0;
386 oi->smb2.out.change_time = 0;
387 oi->smb2.out.alloc_size = p->allocation_size;
388 oi->smb2.out.size = 0;
389 oi->smb2.out.file_attr = FILE_ATTRIBUTE_NORMAL;
390 oi->smb2.out.reserved2 = 0;
397 req->async_states->status = status;
398 req->async_states->send_fn(req);
404 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
405 struct ntvfs_request *req, union smb_mkdir *md)
407 return NT_STATUS_ACCESS_DENIED;
413 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
414 struct ntvfs_request *req, struct smb_rmdir *rd)
416 return NT_STATUS_ACCESS_DENIED;
420 rename a set of files
422 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
423 struct ntvfs_request *req, union smb_rename *ren)
425 return NT_STATUS_ACCESS_DENIED;
431 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
432 struct ntvfs_request *req, struct smb_copy *cp)
434 return NT_STATUS_ACCESS_DENIED;
437 struct ipc_readv_next_vector_state {
444 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
445 uint8_t *buf, size_t len)
450 s->len = MIN(len, UINT16_MAX);
451 //DEBUG(0,("readv_next_vector_init[%u 0x%04X]\n", s->len, s->len));
454 static int ipc_readv_next_vector(struct tstream_context *stream,
457 struct iovec **_vector,
460 struct ipc_readv_next_vector_state *state =
461 (struct ipc_readv_next_vector_state *)private_data;
462 struct iovec *vector;
466 if (state->ofs == state->len) {
469 // DEBUG(0,("readv_next_vector done ofs[%u 0x%04X]\n",
470 // state->ofs, state->ofs));
474 pending = tstream_pending_bytes(stream);
479 if (pending == 0 && state->ofs != 0) {
480 /* return a short read */
483 // DEBUG(0,("readv_next_vector short read ofs[%u 0x%04X]\n",
484 // state->ofs, state->ofs));
489 /* we want at least one byte and recheck again */
492 size_t missing = state->len - state->ofs;
493 if (pending > missing) {
494 /* there's more available */
495 state->remaining = pending - missing;
498 /* read what we can get and recheck in the next cycle */
503 vector = talloc_array(mem_ctx, struct iovec, 1);
508 vector[0].iov_base = state->buf + state->ofs;
509 vector[0].iov_len = wanted;
511 state->ofs += wanted;
518 struct ipc_read_state {
519 struct ipc_private *ipriv;
520 struct pipe_state *p;
521 struct ntvfs_request *req;
523 struct ipc_readv_next_vector_state next_vector;
526 static void ipc_read_done(struct tevent_req *subreq);
531 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
532 struct ntvfs_request *req, union smb_read *rd)
534 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
536 struct pipe_state *p;
537 struct ipc_read_state *state;
538 struct tevent_req *subreq;
540 if (rd->generic.level != RAW_READ_GENERIC) {
541 return ntvfs_map_read(ntvfs, req, rd);
544 p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
546 return NT_STATUS_INVALID_HANDLE;
549 state = talloc(req, struct ipc_read_state);
550 NT_STATUS_HAVE_NO_MEMORY(state);
552 state->ipriv = ipriv;
557 /* rd->readx.out.data is already allocated */
558 ipc_readv_next_vector_init(&state->next_vector,
560 rd->readx.in.maxcnt);
562 subreq = tstream_readv_pdu_queue_send(req,
563 ipriv->ntvfs->ctx->event_ctx,
566 ipc_readv_next_vector,
567 &state->next_vector);
568 NT_STATUS_HAVE_NO_MEMORY(subreq);
569 tevent_req_set_callback(subreq, ipc_read_done, state);
571 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
575 static void ipc_read_done(struct tevent_req *subreq)
577 struct ipc_read_state *state =
578 tevent_req_callback_data(subreq,
579 struct ipc_read_state);
580 struct ntvfs_request *req = state->req;
581 union smb_read *rd = state->rd;
586 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
589 status = map_nt_error_from_unix(sys_errno);
593 status = NT_STATUS_OK;
594 if (state->next_vector.remaining > 0) {
595 status = STATUS_BUFFER_OVERFLOW;
598 rd->readx.out.remaining = state->next_vector.remaining;
599 rd->readx.out.compaction_mode = 0;
600 rd->readx.out.nread = ret;
603 req->async_states->status = status;
604 req->async_states->send_fn(req);
607 struct example_ocor_state {
611 static struct tevent_coroutine_result *example_ocor_body(struct tevent_coroutine *coro,
612 struct tevent_context *ev,
615 static struct tevent_req *example_ocor_send(TALLOC_CTX *mem_ctx,
616 struct tevent_context *ev,
619 struct tevent_req *req;
620 struct example_ocor_state *state;
621 struct tevent_coroutine *coro;
623 req = tevent_req_create(mem_ctx, &state, struct example_ocor_state);
628 state->string = string;
630 coro = tevent_coroutine_create(req, ev, example_ocor_body);
631 if (tevent_req_nomem(coro, req)) {
632 return tevent_req_post(req, ev);
638 static struct tevent_coroutine_result *example_ocor_body(struct tevent_coroutine *coro,
639 struct tevent_context *ev,
642 struct example_ocor_state *state = talloc_get_type_abort(private_data,
643 struct example_ocor_state);
644 struct tevent_req *subreq;
647 DEBUG(0,("%s[%p]: 1. %s\n", __location__, coro, state->string));
649 subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0,500));
650 if (!tevent_coroutine_yield(coro, subreq)) {
651 return tevent_coroutine_return(coro);
653 ok = tevent_wakeup_recv(subreq);
656 tevent_coroutine_error(coro, ENOTSUP);
657 return tevent_coroutine_return(coro);
660 DEBUG(0,("%s[%p]: 2. %s wakeup[%d]\n", __location__, coro, state->string, ok));
662 tevent_coroutine_done(coro);
663 return tevent_coroutine_return(coro);
666 static int example_ocor_recv(struct tevent_req *req, int *perrno)
668 if (tevent_req_is_unix_error(req, perrno)) {
675 struct example_coro_state {
679 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
680 struct tevent_context *ev,
683 static struct tevent_req *example_coro_send(TALLOC_CTX *mem_ctx,
684 struct tevent_context *ev,
687 struct tevent_req *req;
688 struct example_coro_state *state;
689 struct tevent_coroutine *coro;
691 req = tevent_req_create(mem_ctx, &state, struct example_coro_state);
696 state->string = string;
698 coro = tevent_coroutine_create(req, ev, example_coro_body);
699 if (tevent_req_nomem(coro, req)) {
700 return tevent_req_post(req, ev);
706 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
707 struct tevent_context *ev,
710 struct example_coro_state *state = talloc_get_type_abort(private_data,
711 struct example_coro_state);
712 struct tevent_req *subreq;
716 DEBUG(0,("%s:%s[%p]: 1. %s\n", __location__, __FUNCTION__, coro, state->string));
718 subreq = example_ocor_send(state, ev, state->string);
719 if (!tevent_coroutine_yield(coro, subreq)) {
720 return tevent_coroutine_return(coro);
722 ret = example_ocor_recv(subreq, &sys_errno);
725 tevent_coroutine_error(coro, sys_errno);
726 return tevent_coroutine_return(coro);
729 DEBUG(0,("%s:%s[%p]: 2. %s example_ocor[%d]\n", __location__, __FUNCTION__, coro, state->string, ret));
731 subreq = example_ocor_send(state, ev, state->string);
732 if (!tevent_coroutine_yield(coro, subreq)) {
733 return tevent_coroutine_return(coro);
735 ret = example_ocor_recv(subreq, &sys_errno);
738 tevent_coroutine_error(coro, sys_errno);
739 return tevent_coroutine_return(coro);
742 DEBUG(0,("%s:%s[%p]: 3. %s example_ocor[%d]\n", __location__, __FUNCTION__, coro, state->string, ret));
744 tevent_coroutine_done(coro);
745 return tevent_coroutine_return(coro);
748 static int example_coro_recv(struct tevent_req *req, int *perrno)
750 if (tevent_req_is_unix_error(req, perrno)) {
757 struct ipc_write_state {
758 struct ipc_private *ipriv;
759 struct pipe_state *p;
760 struct ntvfs_request *req;
765 static void ipc_write_done(struct tevent_req *subreq);
767 static void ipc_write_coro_done(struct tevent_req *subreq);
772 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
773 struct ntvfs_request *req, union smb_write *wr)
775 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
777 struct pipe_state *p;
778 struct tevent_req *subreq;
779 struct ipc_write_state *state;
781 if (wr->generic.level != RAW_WRITE_GENERIC) {
782 return ntvfs_map_write(ntvfs, req, wr);
785 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
787 return NT_STATUS_INVALID_HANDLE;
790 state = talloc(req, struct ipc_write_state);
791 NT_STATUS_HAVE_NO_MEMORY(state);
793 state->ipriv = ipriv;
797 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
798 state->iov.iov_len = wr->writex.in.count;
800 subreq = example_coro_send(state,
801 ipriv->ntvfs->ctx->event_ctx,
803 NT_STATUS_HAVE_NO_MEMORY(subreq);
804 tevent_req_set_callback(subreq, ipc_write_coro_done, state);
806 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
810 static void ipc_write_coro_done(struct tevent_req *subreq)
812 struct ipc_write_state *state =
813 tevent_req_callback_data(subreq,
814 struct ipc_write_state);
815 struct pipe_state *p = state->p;
816 struct ipc_private *ipriv = state->ipriv;
817 struct ntvfs_request *req = state->req;
822 ret = example_coro_recv(subreq, &sys_errno);
825 status = map_nt_error_from_unix(sys_errno);
829 subreq = tstream_writev_queue_send(state,
830 ipriv->ntvfs->ctx->event_ctx,
835 status = NT_STATUS_NO_MEMORY;
838 tevent_req_set_callback(subreq, ipc_write_done, state);
842 req->async_states->status = status;
843 req->async_states->send_fn(req);
846 static void ipc_write_done(struct tevent_req *subreq)
848 struct ipc_write_state *state =
849 tevent_req_callback_data(subreq,
850 struct ipc_write_state);
851 struct ntvfs_request *req = state->req;
852 union smb_write *wr = state->wr;
857 ret = tstream_writev_queue_recv(subreq, &sys_errno);
860 status = map_nt_error_from_unix(sys_errno);
864 status = NT_STATUS_OK;
866 wr->writex.out.nwritten = ret;
867 wr->writex.out.remaining = 0;
870 req->async_states->status = status;
871 req->async_states->send_fn(req);
877 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
878 struct ntvfs_request *req,
881 return NT_STATUS_ACCESS_DENIED;
887 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
888 struct ntvfs_request *req,
891 return NT_STATUS_ACCESS_DENIED;
897 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
898 struct ntvfs_request *req, union smb_close *io)
900 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
902 struct pipe_state *p;
904 if (io->generic.level != RAW_CLOSE_CLOSE) {
905 return ntvfs_map_close(ntvfs, req, io);
908 p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
910 return NT_STATUS_INVALID_HANDLE;
921 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
922 struct ntvfs_request *req)
924 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
926 struct pipe_state *p, *next;
928 for (p=ipriv->pipe_list; p; p=next) {
930 if (p->handle->session_info == req->session_info &&
931 p->handle->smbpid == req->smbpid) {
940 logoff - closing files open by the user
942 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
943 struct ntvfs_request *req)
945 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
947 struct pipe_state *p, *next;
949 for (p=ipriv->pipe_list; p; p=next) {
951 if (p->handle->session_info == req->session_info) {
960 setup for an async call
962 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
963 struct ntvfs_request *req,
972 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
973 struct ntvfs_request *req)
975 return NT_STATUS_UNSUCCESSFUL;
981 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
982 struct ntvfs_request *req, union smb_lock *lck)
984 return NT_STATUS_ACCESS_DENIED;
988 set info on a open file
990 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
991 struct ntvfs_request *req, union smb_setfileinfo *info)
993 return NT_STATUS_ACCESS_DENIED;
997 query info on a open file
999 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
1000 struct ntvfs_request *req, union smb_fileinfo *info)
1002 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1003 struct ipc_private);
1004 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
1006 return NT_STATUS_INVALID_HANDLE;
1008 switch (info->generic.level) {
1009 case RAW_FILEINFO_GENERIC:
1011 ZERO_STRUCT(info->generic.out);
1012 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
1013 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
1014 info->generic.out.alloc_size = 4096;
1015 info->generic.out.nlink = 1;
1016 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
1017 info->generic.out.delete_pending = 1;
1018 return NT_STATUS_OK;
1020 case RAW_FILEINFO_ALT_NAME_INFO:
1021 case RAW_FILEINFO_ALT_NAME_INFORMATION:
1022 case RAW_FILEINFO_STREAM_INFO:
1023 case RAW_FILEINFO_STREAM_INFORMATION:
1024 case RAW_FILEINFO_COMPRESSION_INFO:
1025 case RAW_FILEINFO_COMPRESSION_INFORMATION:
1026 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1027 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1028 return NT_STATUS_INVALID_PARAMETER;
1029 case RAW_FILEINFO_ALL_EAS:
1030 return NT_STATUS_ACCESS_DENIED;
1032 return ntvfs_map_qfileinfo(ntvfs, req, info);
1035 return NT_STATUS_ACCESS_DENIED;
1040 return filesystem info
1042 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
1043 struct ntvfs_request *req, union smb_fsinfo *fs)
1045 return NT_STATUS_ACCESS_DENIED;
1049 return print queue info
1051 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
1052 struct ntvfs_request *req, union smb_lpq *lpq)
1054 return NT_STATUS_ACCESS_DENIED;
1058 list files in a directory matching a wildcard pattern
1060 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
1061 struct ntvfs_request *req, union smb_search_first *io,
1062 void *search_private,
1063 bool (*callback)(void *, const union smb_search_data *))
1065 return NT_STATUS_ACCESS_DENIED;
1069 continue listing files in a directory
1071 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
1072 struct ntvfs_request *req, union smb_search_next *io,
1073 void *search_private,
1074 bool (*callback)(void *, const union smb_search_data *))
1076 return NT_STATUS_ACCESS_DENIED;
1080 end listing files in a directory
1082 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
1083 struct ntvfs_request *req, union smb_search_close *io)
1085 return NT_STATUS_ACCESS_DENIED;
1088 struct ipc_trans_state {
1089 struct ipc_private *ipriv;
1090 struct pipe_state *p;
1091 struct ntvfs_request *req;
1092 struct smb_trans2 *trans;
1093 struct iovec writev_iov;
1094 struct ipc_readv_next_vector_state next_vector;
1097 static void ipc_trans_writev_done(struct tevent_req *subreq);
1098 static void ipc_trans_readv_done(struct tevent_req *subreq);
1100 /* SMBtrans - handle a DCERPC command */
1101 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
1102 struct ntvfs_request *req, struct smb_trans2 *trans)
1104 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1105 struct ipc_private);
1106 struct pipe_state *p;
1109 struct ipc_trans_state *state;
1110 struct tevent_req *subreq;
1113 * the fnum is in setup[1], a 16 bit value
1114 * the setup[*] values are already in host byteorder
1115 * but ntvfs_handle_search_by_wire_key() expects
1118 SSVAL(&fnum, 0, trans->in.setup[1]);
1119 fnum_key = data_blob_const(&fnum, 2);
1121 p = pipe_state_find_key(ipriv, req, &fnum_key);
1123 return NT_STATUS_INVALID_HANDLE;
1126 /* TODO: check if this os the correct logic */
1127 if (tevent_queue_length(p->read_queue) > 0) {
1128 return NT_STATUS_PIPE_BUSY;
1131 state = talloc(req, struct ipc_trans_state);
1132 NT_STATUS_HAVE_NO_MEMORY(state);
1134 trans->out.setup_count = 0;
1135 trans->out.setup = NULL;
1136 trans->out.params = data_blob(NULL, 0);
1137 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1138 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1140 state->ipriv = ipriv;
1143 state->trans = trans;
1144 state->writev_iov.iov_base = trans->in.data.data;
1145 state->writev_iov.iov_len = trans->in.data.length;
1147 ipc_readv_next_vector_init(&state->next_vector,
1148 trans->out.data.data,
1149 trans->out.data.length);
1151 subreq = tstream_writev_queue_send(state,
1152 ipriv->ntvfs->ctx->event_ctx,
1155 &state->writev_iov, 1);
1156 NT_STATUS_HAVE_NO_MEMORY(subreq);
1157 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1159 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1160 return NT_STATUS_OK;
1163 static void ipc_trans_writev_done(struct tevent_req *subreq)
1165 struct ipc_trans_state *state =
1166 tevent_req_callback_data(subreq,
1167 struct ipc_trans_state);
1168 struct ipc_private *ipriv = state->ipriv;
1169 struct pipe_state *p = state->p;
1170 struct ntvfs_request *req = state->req;
1175 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1176 TALLOC_FREE(subreq);
1178 status = NT_STATUS_PIPE_DISCONNECTED;
1180 } else if (ret == -1) {
1181 status = map_nt_error_from_unix(sys_errno);
1185 subreq = tstream_readv_pdu_queue_send(state,
1186 ipriv->ntvfs->ctx->event_ctx,
1189 ipc_readv_next_vector,
1190 &state->next_vector);
1192 status = NT_STATUS_NO_MEMORY;
1195 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1199 req->async_states->status = status;
1200 req->async_states->send_fn(req);
1203 static void ipc_trans_readv_done(struct tevent_req *subreq)
1205 struct ipc_trans_state *state =
1206 tevent_req_callback_data(subreq,
1207 struct ipc_trans_state);
1208 struct ntvfs_request *req = state->req;
1209 struct smb_trans2 *trans = state->trans;
1214 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1215 TALLOC_FREE(subreq);
1217 status = map_nt_error_from_unix(sys_errno);
1221 status = NT_STATUS_OK;
1222 if (state->next_vector.remaining > 0) {
1223 status = STATUS_BUFFER_OVERFLOW;
1226 trans->out.data.length = ret;
1229 req->async_states->status = status;
1230 req->async_states->send_fn(req);
1233 /* SMBtrans - set named pipe state */
1234 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1235 struct ntvfs_request *req, struct smb_trans2 *trans)
1237 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1238 struct ipc_private);
1239 struct pipe_state *p;
1242 /* the fnum is in setup[1] */
1243 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1245 p = pipe_state_find_key(ipriv, req, &fnum_key);
1247 return NT_STATUS_INVALID_HANDLE;
1250 if (trans->in.params.length != 2) {
1251 return NT_STATUS_INVALID_PARAMETER;
1254 p->device_state = SVAL(trans->in.params.data, 0);
1256 trans->out.setup_count = 0;
1257 trans->out.setup = NULL;
1258 trans->out.params = data_blob(NULL, 0);
1259 trans->out.data = data_blob(NULL, 0);
1261 return NT_STATUS_OK;
1265 /* SMBtrans - used to provide access to SMB pipes */
1266 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1267 struct ntvfs_request *req, struct smb_trans2 *trans)
1271 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1272 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1274 if (trans->in.setup_count != 2) {
1275 return NT_STATUS_INVALID_PARAMETER;
1278 switch (trans->in.setup[0]) {
1279 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1280 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1282 case TRANSACT_DCERPCCMD:
1283 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1286 status = NT_STATUS_INVALID_PARAMETER;
1293 struct ipc_ioctl_state {
1294 struct ipc_private *ipriv;
1295 struct pipe_state *p;
1296 struct ntvfs_request *req;
1297 union smb_ioctl *io;
1298 struct iovec writev_iov;
1299 struct ipc_readv_next_vector_state next_vector;
1302 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1303 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1305 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1306 struct ntvfs_request *req, union smb_ioctl *io)
1308 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1309 struct ipc_private);
1310 struct pipe_state *p;
1311 struct ipc_ioctl_state *state;
1312 struct tevent_req *subreq;
1314 switch (io->smb2.in.function) {
1315 case FSCTL_NAMED_PIPE_READ_WRITE:
1319 return NT_STATUS_FS_DRIVER_REQUIRED;
1322 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1324 return NT_STATUS_INVALID_HANDLE;
1327 /* TODO: check if this os the correct logic */
1328 if (tevent_queue_length(p->read_queue) > 0) {
1329 return NT_STATUS_PIPE_BUSY;
1332 state = talloc(req, struct ipc_ioctl_state);
1333 NT_STATUS_HAVE_NO_MEMORY(state);
1335 io->smb2.out._pad = 0;
1336 io->smb2.out.function = io->smb2.in.function;
1337 io->smb2.out.unknown2 = 0;
1338 io->smb2.out.unknown3 = 0;
1339 io->smb2.out.in = io->smb2.in.out;
1340 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1341 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1343 state->ipriv = ipriv;
1347 state->writev_iov.iov_base = io->smb2.in.out.data;
1348 state->writev_iov.iov_len = io->smb2.in.out.length;
1350 ipc_readv_next_vector_init(&state->next_vector,
1351 io->smb2.out.out.data,
1352 io->smb2.out.out.length);
1354 subreq = tstream_writev_queue_send(state,
1355 ipriv->ntvfs->ctx->event_ctx,
1358 &state->writev_iov, 1);
1359 NT_STATUS_HAVE_NO_MEMORY(subreq);
1360 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1362 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1363 return NT_STATUS_OK;
1366 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1368 struct ipc_ioctl_state *state =
1369 tevent_req_callback_data(subreq,
1370 struct ipc_ioctl_state);
1371 struct ipc_private *ipriv = state->ipriv;
1372 struct pipe_state *p = state->p;
1373 struct ntvfs_request *req = state->req;
1378 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1379 TALLOC_FREE(subreq);
1381 status = map_nt_error_from_unix(sys_errno);
1385 subreq = tstream_readv_pdu_queue_send(state,
1386 ipriv->ntvfs->ctx->event_ctx,
1389 ipc_readv_next_vector,
1390 &state->next_vector);
1392 status = NT_STATUS_NO_MEMORY;
1395 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1399 req->async_states->status = status;
1400 req->async_states->send_fn(req);
1403 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1405 struct ipc_ioctl_state *state =
1406 tevent_req_callback_data(subreq,
1407 struct ipc_ioctl_state);
1408 struct ntvfs_request *req = state->req;
1409 union smb_ioctl *io = state->io;
1414 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1415 TALLOC_FREE(subreq);
1417 status = map_nt_error_from_unix(sys_errno);
1421 status = NT_STATUS_OK;
1422 if (state->next_vector.remaining > 0) {
1423 status = STATUS_BUFFER_OVERFLOW;
1426 io->smb2.out.out.length = ret;
1429 req->async_states->status = status;
1430 req->async_states->send_fn(req);
1437 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1438 struct ntvfs_request *req, union smb_ioctl *io)
1440 switch (io->generic.level) {
1441 case RAW_IOCTL_SMB2:
1442 return ipc_ioctl_smb2(ntvfs, req, io);
1444 case RAW_IOCTL_SMB2_NO_HANDLE:
1445 return NT_STATUS_FS_DRIVER_REQUIRED;
1448 return NT_STATUS_ACCESS_DENIED;
1451 return NT_STATUS_ACCESS_DENIED;
1456 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1458 NTSTATUS ntvfs_ipc_init(void)
1461 struct ntvfs_ops ops;
1462 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1466 /* fill in the name and type */
1467 ops.name = "default";
1468 ops.type = NTVFS_IPC;
1470 /* fill in all the operations */
1471 ops.connect = ipc_connect;
1472 ops.disconnect = ipc_disconnect;
1473 ops.unlink = ipc_unlink;
1474 ops.chkpath = ipc_chkpath;
1475 ops.qpathinfo = ipc_qpathinfo;
1476 ops.setpathinfo = ipc_setpathinfo;
1477 ops.open = ipc_open;
1478 ops.mkdir = ipc_mkdir;
1479 ops.rmdir = ipc_rmdir;
1480 ops.rename = ipc_rename;
1481 ops.copy = ipc_copy;
1482 ops.ioctl = ipc_ioctl;
1483 ops.read = ipc_read;
1484 ops.write = ipc_write;
1485 ops.seek = ipc_seek;
1486 ops.flush = ipc_flush;
1487 ops.close = ipc_close;
1488 ops.exit = ipc_exit;
1489 ops.lock = ipc_lock;
1490 ops.setfileinfo = ipc_setfileinfo;
1491 ops.qfileinfo = ipc_qfileinfo;
1492 ops.fsinfo = ipc_fsinfo;
1494 ops.search_first = ipc_search_first;
1495 ops.search_next = ipc_search_next;
1496 ops.search_close = ipc_search_close;
1497 ops.trans = ipc_trans;
1498 ops.logoff = ipc_logoff;
1499 ops.async_setup = ipc_async_setup;
1500 ops.cancel = ipc_cancel;
1502 /* register ourselves with the NTVFS subsystem. */
1503 ret = ntvfs_register(&ops, &vers);
1505 if (!NT_STATUS_IS_OK(ret)) {
1506 DEBUG(0,("Failed to register IPC backend!\n"));