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);
635 tevent_coroutine_run(coro);
636 if (!tevent_req_is_in_progress(req)) {
637 return tevent_req_post(req, ev);
643 static struct tevent_coroutine_result *example_ocor_body(struct tevent_coroutine *coro,
644 struct tevent_context *ev,
647 struct example_ocor_state *state = talloc_get_type_abort(private_data,
648 struct example_ocor_state);
649 struct tevent_req *subreq;
652 DEBUG(0,("%s[%p]: 1. %s\n", __location__, coro, state->string));
654 subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0,500));
655 if (tevent_coroutine_nomem(subreq, coro)) {
656 return tevent_coroutine_return(coro);
658 tevent_coroutine_yield(coro, subreq);
659 ok = tevent_wakeup_recv(subreq);
661 DEBUG(0,("%s[%p]: 2. %s wakeup[%d]\n", __location__, coro, state->string, ok));
663 tevent_coroutine_done(coro);
664 return tevent_coroutine_return(coro);
667 static int example_ocor_recv(struct tevent_req *req, int *perrno)
669 if (tevent_req_is_unix_error(req, perrno)) {
676 struct example_coro_state {
680 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
681 struct tevent_context *ev,
684 static struct tevent_req *example_coro_send(TALLOC_CTX *mem_ctx,
685 struct tevent_context *ev,
688 struct tevent_req *req;
689 struct example_coro_state *state;
690 struct tevent_coroutine *coro;
692 req = tevent_req_create(mem_ctx, &state, struct example_coro_state);
697 state->string = string;
699 coro = tevent_coroutine_create(req, ev, example_coro_body);
700 if (tevent_req_nomem(coro, req)) {
701 return tevent_req_post(req, ev);
704 tevent_coroutine_run(coro);
705 if (!tevent_req_is_in_progress(req)) {
706 return tevent_req_post(req, ev);
712 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
713 struct tevent_context *ev,
716 struct example_coro_state *state = talloc_get_type_abort(private_data,
717 struct example_coro_state);
718 struct tevent_req *subreq;
722 DEBUG(0,("%s:%s[%p]: 1. %s\n", __location__, __FUNCTION__, coro, state->string));
724 subreq = example_ocor_send(state, ev, state->string);
725 if (tevent_coroutine_nomem(subreq, coro)) {
726 return tevent_coroutine_return(coro);
728 tevent_coroutine_yield(coro, subreq);
729 ret = example_ocor_recv(subreq, &sys_errno);
731 tevent_coroutine_error(coro, sys_errno);
732 return tevent_coroutine_return(coro);
735 DEBUG(0,("%s:%s[%p]: 2. %s example_ocor[%d]\n", __location__, __FUNCTION__, coro, state->string, ret));
737 tevent_coroutine_done(coro);
738 return tevent_coroutine_return(coro);
741 static int example_coro_recv(struct tevent_req *req, int *perrno)
743 if (tevent_req_is_unix_error(req, perrno)) {
750 struct ipc_write_state {
751 struct ipc_private *ipriv;
752 struct pipe_state *p;
753 struct ntvfs_request *req;
758 static void ipc_write_done(struct tevent_req *subreq);
760 static void ipc_write_coro_done(struct tevent_req *subreq);
765 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
766 struct ntvfs_request *req, union smb_write *wr)
768 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
770 struct pipe_state *p;
771 struct tevent_req *subreq;
772 struct ipc_write_state *state;
774 if (wr->generic.level != RAW_WRITE_GENERIC) {
775 return ntvfs_map_write(ntvfs, req, wr);
778 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
780 return NT_STATUS_INVALID_HANDLE;
783 state = talloc(req, struct ipc_write_state);
784 NT_STATUS_HAVE_NO_MEMORY(state);
786 state->ipriv = ipriv;
790 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
791 state->iov.iov_len = wr->writex.in.count;
793 subreq = example_coro_send(state,
794 ipriv->ntvfs->ctx->event_ctx,
796 NT_STATUS_HAVE_NO_MEMORY(subreq);
797 tevent_req_set_callback(subreq, ipc_write_coro_done, state);
799 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
803 static void ipc_write_coro_done(struct tevent_req *subreq)
805 struct ipc_write_state *state =
806 tevent_req_callback_data(subreq,
807 struct ipc_write_state);
808 struct pipe_state *p = state->p;
809 struct ipc_private *ipriv = state->ipriv;
810 struct ntvfs_request *req = state->req;
815 ret = example_coro_recv(subreq, &sys_errno);
818 status = map_nt_error_from_unix(sys_errno);
822 subreq = tstream_writev_queue_send(state,
823 ipriv->ntvfs->ctx->event_ctx,
828 status = NT_STATUS_NO_MEMORY;
831 tevent_req_set_callback(subreq, ipc_write_done, state);
835 req->async_states->status = status;
836 req->async_states->send_fn(req);
839 static void ipc_write_done(struct tevent_req *subreq)
841 struct ipc_write_state *state =
842 tevent_req_callback_data(subreq,
843 struct ipc_write_state);
844 struct ntvfs_request *req = state->req;
845 union smb_write *wr = state->wr;
850 ret = tstream_writev_queue_recv(subreq, &sys_errno);
853 status = map_nt_error_from_unix(sys_errno);
857 status = NT_STATUS_OK;
859 wr->writex.out.nwritten = ret;
860 wr->writex.out.remaining = 0;
863 req->async_states->status = status;
864 req->async_states->send_fn(req);
870 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
871 struct ntvfs_request *req,
874 return NT_STATUS_ACCESS_DENIED;
880 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
881 struct ntvfs_request *req,
884 return NT_STATUS_ACCESS_DENIED;
890 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
891 struct ntvfs_request *req, union smb_close *io)
893 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
895 struct pipe_state *p;
897 if (io->generic.level != RAW_CLOSE_CLOSE) {
898 return ntvfs_map_close(ntvfs, req, io);
901 p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
903 return NT_STATUS_INVALID_HANDLE;
914 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
915 struct ntvfs_request *req)
917 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
919 struct pipe_state *p, *next;
921 for (p=ipriv->pipe_list; p; p=next) {
923 if (p->handle->session_info == req->session_info &&
924 p->handle->smbpid == req->smbpid) {
933 logoff - closing files open by the user
935 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
936 struct ntvfs_request *req)
938 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
940 struct pipe_state *p, *next;
942 for (p=ipriv->pipe_list; p; p=next) {
944 if (p->handle->session_info == req->session_info) {
953 setup for an async call
955 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
956 struct ntvfs_request *req,
965 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
966 struct ntvfs_request *req)
968 return NT_STATUS_UNSUCCESSFUL;
974 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
975 struct ntvfs_request *req, union smb_lock *lck)
977 return NT_STATUS_ACCESS_DENIED;
981 set info on a open file
983 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
984 struct ntvfs_request *req, union smb_setfileinfo *info)
986 return NT_STATUS_ACCESS_DENIED;
990 query info on a open file
992 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
993 struct ntvfs_request *req, union smb_fileinfo *info)
995 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
997 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
999 return NT_STATUS_INVALID_HANDLE;
1001 switch (info->generic.level) {
1002 case RAW_FILEINFO_GENERIC:
1004 ZERO_STRUCT(info->generic.out);
1005 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
1006 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
1007 info->generic.out.alloc_size = 4096;
1008 info->generic.out.nlink = 1;
1009 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
1010 info->generic.out.delete_pending = 1;
1011 return NT_STATUS_OK;
1013 case RAW_FILEINFO_ALT_NAME_INFO:
1014 case RAW_FILEINFO_ALT_NAME_INFORMATION:
1015 case RAW_FILEINFO_STREAM_INFO:
1016 case RAW_FILEINFO_STREAM_INFORMATION:
1017 case RAW_FILEINFO_COMPRESSION_INFO:
1018 case RAW_FILEINFO_COMPRESSION_INFORMATION:
1019 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1020 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1021 return NT_STATUS_INVALID_PARAMETER;
1022 case RAW_FILEINFO_ALL_EAS:
1023 return NT_STATUS_ACCESS_DENIED;
1025 return ntvfs_map_qfileinfo(ntvfs, req, info);
1028 return NT_STATUS_ACCESS_DENIED;
1033 return filesystem info
1035 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
1036 struct ntvfs_request *req, union smb_fsinfo *fs)
1038 return NT_STATUS_ACCESS_DENIED;
1042 return print queue info
1044 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
1045 struct ntvfs_request *req, union smb_lpq *lpq)
1047 return NT_STATUS_ACCESS_DENIED;
1051 list files in a directory matching a wildcard pattern
1053 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
1054 struct ntvfs_request *req, union smb_search_first *io,
1055 void *search_private,
1056 bool (*callback)(void *, const union smb_search_data *))
1058 return NT_STATUS_ACCESS_DENIED;
1062 continue listing files in a directory
1064 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
1065 struct ntvfs_request *req, union smb_search_next *io,
1066 void *search_private,
1067 bool (*callback)(void *, const union smb_search_data *))
1069 return NT_STATUS_ACCESS_DENIED;
1073 end listing files in a directory
1075 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
1076 struct ntvfs_request *req, union smb_search_close *io)
1078 return NT_STATUS_ACCESS_DENIED;
1081 struct ipc_trans_state {
1082 struct ipc_private *ipriv;
1083 struct pipe_state *p;
1084 struct ntvfs_request *req;
1085 struct smb_trans2 *trans;
1086 struct iovec writev_iov;
1087 struct ipc_readv_next_vector_state next_vector;
1090 static void ipc_trans_writev_done(struct tevent_req *subreq);
1091 static void ipc_trans_readv_done(struct tevent_req *subreq);
1093 /* SMBtrans - handle a DCERPC command */
1094 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
1095 struct ntvfs_request *req, struct smb_trans2 *trans)
1097 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1098 struct ipc_private);
1099 struct pipe_state *p;
1102 struct ipc_trans_state *state;
1103 struct tevent_req *subreq;
1106 * the fnum is in setup[1], a 16 bit value
1107 * the setup[*] values are already in host byteorder
1108 * but ntvfs_handle_search_by_wire_key() expects
1111 SSVAL(&fnum, 0, trans->in.setup[1]);
1112 fnum_key = data_blob_const(&fnum, 2);
1114 p = pipe_state_find_key(ipriv, req, &fnum_key);
1116 return NT_STATUS_INVALID_HANDLE;
1119 /* TODO: check if this os the correct logic */
1120 if (tevent_queue_length(p->read_queue) > 0) {
1121 return NT_STATUS_PIPE_BUSY;
1124 state = talloc(req, struct ipc_trans_state);
1125 NT_STATUS_HAVE_NO_MEMORY(state);
1127 trans->out.setup_count = 0;
1128 trans->out.setup = NULL;
1129 trans->out.params = data_blob(NULL, 0);
1130 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1131 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1133 state->ipriv = ipriv;
1136 state->trans = trans;
1137 state->writev_iov.iov_base = trans->in.data.data;
1138 state->writev_iov.iov_len = trans->in.data.length;
1140 ipc_readv_next_vector_init(&state->next_vector,
1141 trans->out.data.data,
1142 trans->out.data.length);
1144 subreq = tstream_writev_queue_send(state,
1145 ipriv->ntvfs->ctx->event_ctx,
1148 &state->writev_iov, 1);
1149 NT_STATUS_HAVE_NO_MEMORY(subreq);
1150 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1152 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1153 return NT_STATUS_OK;
1156 static void ipc_trans_writev_done(struct tevent_req *subreq)
1158 struct ipc_trans_state *state =
1159 tevent_req_callback_data(subreq,
1160 struct ipc_trans_state);
1161 struct ipc_private *ipriv = state->ipriv;
1162 struct pipe_state *p = state->p;
1163 struct ntvfs_request *req = state->req;
1168 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1169 TALLOC_FREE(subreq);
1171 status = NT_STATUS_PIPE_DISCONNECTED;
1173 } else if (ret == -1) {
1174 status = map_nt_error_from_unix(sys_errno);
1178 subreq = tstream_readv_pdu_queue_send(state,
1179 ipriv->ntvfs->ctx->event_ctx,
1182 ipc_readv_next_vector,
1183 &state->next_vector);
1185 status = NT_STATUS_NO_MEMORY;
1188 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1192 req->async_states->status = status;
1193 req->async_states->send_fn(req);
1196 static void ipc_trans_readv_done(struct tevent_req *subreq)
1198 struct ipc_trans_state *state =
1199 tevent_req_callback_data(subreq,
1200 struct ipc_trans_state);
1201 struct ntvfs_request *req = state->req;
1202 struct smb_trans2 *trans = state->trans;
1207 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1208 TALLOC_FREE(subreq);
1210 status = map_nt_error_from_unix(sys_errno);
1214 status = NT_STATUS_OK;
1215 if (state->next_vector.remaining > 0) {
1216 status = STATUS_BUFFER_OVERFLOW;
1219 trans->out.data.length = ret;
1222 req->async_states->status = status;
1223 req->async_states->send_fn(req);
1226 /* SMBtrans - set named pipe state */
1227 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1228 struct ntvfs_request *req, struct smb_trans2 *trans)
1230 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1231 struct ipc_private);
1232 struct pipe_state *p;
1235 /* the fnum is in setup[1] */
1236 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1238 p = pipe_state_find_key(ipriv, req, &fnum_key);
1240 return NT_STATUS_INVALID_HANDLE;
1243 if (trans->in.params.length != 2) {
1244 return NT_STATUS_INVALID_PARAMETER;
1247 p->device_state = SVAL(trans->in.params.data, 0);
1249 trans->out.setup_count = 0;
1250 trans->out.setup = NULL;
1251 trans->out.params = data_blob(NULL, 0);
1252 trans->out.data = data_blob(NULL, 0);
1254 return NT_STATUS_OK;
1258 /* SMBtrans - used to provide access to SMB pipes */
1259 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1260 struct ntvfs_request *req, struct smb_trans2 *trans)
1264 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1265 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1267 if (trans->in.setup_count != 2) {
1268 return NT_STATUS_INVALID_PARAMETER;
1271 switch (trans->in.setup[0]) {
1272 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1273 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1275 case TRANSACT_DCERPCCMD:
1276 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1279 status = NT_STATUS_INVALID_PARAMETER;
1286 struct ipc_ioctl_state {
1287 struct ipc_private *ipriv;
1288 struct pipe_state *p;
1289 struct ntvfs_request *req;
1290 union smb_ioctl *io;
1291 struct iovec writev_iov;
1292 struct ipc_readv_next_vector_state next_vector;
1295 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1296 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1298 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1299 struct ntvfs_request *req, union smb_ioctl *io)
1301 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1302 struct ipc_private);
1303 struct pipe_state *p;
1304 struct ipc_ioctl_state *state;
1305 struct tevent_req *subreq;
1307 switch (io->smb2.in.function) {
1308 case FSCTL_NAMED_PIPE_READ_WRITE:
1312 return NT_STATUS_FS_DRIVER_REQUIRED;
1315 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1317 return NT_STATUS_INVALID_HANDLE;
1320 /* TODO: check if this os the correct logic */
1321 if (tevent_queue_length(p->read_queue) > 0) {
1322 return NT_STATUS_PIPE_BUSY;
1325 state = talloc(req, struct ipc_ioctl_state);
1326 NT_STATUS_HAVE_NO_MEMORY(state);
1328 io->smb2.out._pad = 0;
1329 io->smb2.out.function = io->smb2.in.function;
1330 io->smb2.out.unknown2 = 0;
1331 io->smb2.out.unknown3 = 0;
1332 io->smb2.out.in = io->smb2.in.out;
1333 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1334 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1336 state->ipriv = ipriv;
1340 state->writev_iov.iov_base = io->smb2.in.out.data;
1341 state->writev_iov.iov_len = io->smb2.in.out.length;
1343 ipc_readv_next_vector_init(&state->next_vector,
1344 io->smb2.out.out.data,
1345 io->smb2.out.out.length);
1347 subreq = tstream_writev_queue_send(state,
1348 ipriv->ntvfs->ctx->event_ctx,
1351 &state->writev_iov, 1);
1352 NT_STATUS_HAVE_NO_MEMORY(subreq);
1353 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1355 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1356 return NT_STATUS_OK;
1359 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1361 struct ipc_ioctl_state *state =
1362 tevent_req_callback_data(subreq,
1363 struct ipc_ioctl_state);
1364 struct ipc_private *ipriv = state->ipriv;
1365 struct pipe_state *p = state->p;
1366 struct ntvfs_request *req = state->req;
1371 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1372 TALLOC_FREE(subreq);
1374 status = map_nt_error_from_unix(sys_errno);
1378 subreq = tstream_readv_pdu_queue_send(state,
1379 ipriv->ntvfs->ctx->event_ctx,
1382 ipc_readv_next_vector,
1383 &state->next_vector);
1385 status = NT_STATUS_NO_MEMORY;
1388 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1392 req->async_states->status = status;
1393 req->async_states->send_fn(req);
1396 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1398 struct ipc_ioctl_state *state =
1399 tevent_req_callback_data(subreq,
1400 struct ipc_ioctl_state);
1401 struct ntvfs_request *req = state->req;
1402 union smb_ioctl *io = state->io;
1407 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1408 TALLOC_FREE(subreq);
1410 status = map_nt_error_from_unix(sys_errno);
1414 status = NT_STATUS_OK;
1415 if (state->next_vector.remaining > 0) {
1416 status = STATUS_BUFFER_OVERFLOW;
1419 io->smb2.out.out.length = ret;
1422 req->async_states->status = status;
1423 req->async_states->send_fn(req);
1430 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1431 struct ntvfs_request *req, union smb_ioctl *io)
1433 switch (io->generic.level) {
1434 case RAW_IOCTL_SMB2:
1435 return ipc_ioctl_smb2(ntvfs, req, io);
1437 case RAW_IOCTL_SMB2_NO_HANDLE:
1438 return NT_STATUS_FS_DRIVER_REQUIRED;
1441 return NT_STATUS_ACCESS_DENIED;
1444 return NT_STATUS_ACCESS_DENIED;
1449 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1451 NTSTATUS ntvfs_ipc_init(void)
1454 struct ntvfs_ops ops;
1455 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1459 /* fill in the name and type */
1460 ops.name = "default";
1461 ops.type = NTVFS_IPC;
1463 /* fill in all the operations */
1464 ops.connect = ipc_connect;
1465 ops.disconnect = ipc_disconnect;
1466 ops.unlink = ipc_unlink;
1467 ops.chkpath = ipc_chkpath;
1468 ops.qpathinfo = ipc_qpathinfo;
1469 ops.setpathinfo = ipc_setpathinfo;
1470 ops.open = ipc_open;
1471 ops.mkdir = ipc_mkdir;
1472 ops.rmdir = ipc_rmdir;
1473 ops.rename = ipc_rename;
1474 ops.copy = ipc_copy;
1475 ops.ioctl = ipc_ioctl;
1476 ops.read = ipc_read;
1477 ops.write = ipc_write;
1478 ops.seek = ipc_seek;
1479 ops.flush = ipc_flush;
1480 ops.close = ipc_close;
1481 ops.exit = ipc_exit;
1482 ops.lock = ipc_lock;
1483 ops.setfileinfo = ipc_setfileinfo;
1484 ops.qfileinfo = ipc_qfileinfo;
1485 ops.fsinfo = ipc_fsinfo;
1487 ops.search_first = ipc_search_first;
1488 ops.search_next = ipc_search_next;
1489 ops.search_close = ipc_search_close;
1490 ops.trans = ipc_trans;
1491 ops.logoff = ipc_logoff;
1492 ops.async_setup = ipc_async_setup;
1493 ops.cancel = ipc_cancel;
1495 /* register ourselves with the NTVFS subsystem. */
1496 ret = ntvfs_register(&ops, &vers);
1498 if (!NT_STATUS_IS_OK(ret)) {
1499 DEBUG(0,("Failed to register IPC backend!\n"));