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"
40 /* this is the private structure used to keep the state of an open
41 ipc$ connection. It needs to keep information about all open
44 struct ntvfs_module_context *ntvfs;
46 /* a list of open pipes */
48 struct pipe_state *next, *prev;
49 struct ipc_private *ipriv;
50 const char *pipe_name;
51 struct ntvfs_handle *handle;
52 struct tstream_context *npipe;
54 uint16_t device_state;
55 uint64_t allocation_size;
56 struct tevent_queue *write_queue;
57 struct tevent_queue *read_queue;
63 find a open pipe give a file handle
65 static struct pipe_state *pipe_state_find(struct ipc_private *ipriv, struct ntvfs_handle *handle)
70 p = ntvfs_handle_get_backend_data(handle, ipriv->ntvfs);
73 s = talloc_get_type(p, struct pipe_state);
80 find a open pipe give a wire fnum
82 static struct pipe_state *pipe_state_find_key(struct ipc_private *ipriv, struct ntvfs_request *req, const DATA_BLOB *key)
84 struct ntvfs_handle *h;
86 h = ntvfs_handle_search_by_wire_key(ipriv->ntvfs, req, key);
89 return pipe_state_find(ipriv, h);
94 connect to a share - always works
96 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
97 struct ntvfs_request *req, const char *sharename)
99 struct ipc_private *ipriv;
101 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "IPC");
102 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
104 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "IPC");
105 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
107 /* prepare the private state for this connection */
108 ipriv = talloc(ntvfs, struct ipc_private);
109 NT_STATUS_HAVE_NO_MEMORY(ipriv);
111 ntvfs->private_data = ipriv;
113 ipriv->ntvfs = ntvfs;
114 ipriv->pipe_list = NULL;
120 disconnect from a share
122 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs)
130 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
131 struct ntvfs_request *req,
132 union smb_unlink *unl)
134 return NT_STATUS_ACCESS_DENIED;
138 check if a directory exists
140 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
141 struct ntvfs_request *req,
142 union smb_chkpath *cp)
144 return NT_STATUS_ACCESS_DENIED;
148 return info on a pathname
150 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
151 struct ntvfs_request *req, union smb_fileinfo *info)
153 switch (info->generic.level) {
154 case RAW_FILEINFO_GENERIC:
155 return NT_STATUS_INVALID_DEVICE_REQUEST;
156 case RAW_FILEINFO_GETATTR:
157 return NT_STATUS_ACCESS_DENIED;
159 return ntvfs_map_qpathinfo(ntvfs, req, info);
164 set info on a pathname
166 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
167 struct ntvfs_request *req, union smb_setfileinfo *st)
169 return NT_STATUS_ACCESS_DENIED;
174 destroy a open pipe structure
176 static int ipc_fd_destructor(struct pipe_state *p)
178 DLIST_REMOVE(p->ipriv->pipe_list, p);
179 ntvfs_handle_remove_backend_data(p->handle, p->ipriv->ntvfs);
183 struct ipc_open_state {
184 struct ipc_private *ipriv;
185 struct pipe_state *p;
186 struct ntvfs_request *req;
188 struct netr_SamInfo3 *info3;
191 static void ipc_open_done(struct tevent_req *subreq);
194 open a file - used for MSRPC pipes
196 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
197 struct ntvfs_request *req, union smb_open *oi)
200 struct pipe_state *p;
201 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
203 struct smb_iconv_convenience *smb_ic
204 = lp_iconv_convenience(ipriv->ntvfs->ctx->lp_ctx);
205 struct ntvfs_handle *h;
206 struct ipc_open_state *state;
207 struct tevent_req *subreq;
209 const char *directory;
210 struct socket_address *client_sa;
211 struct tsocket_address *client_addr;
212 struct socket_address *server_sa;
213 struct tsocket_address *server_addr;
216 switch (oi->generic.level) {
217 case RAW_OPEN_NTCREATEX:
218 fname = oi->ntcreatex.in.fname;
221 fname = oi->openx.in.fname;
224 fname = oi->smb2.in.fname;
227 status = NT_STATUS_NOT_SUPPORTED;
231 directory = talloc_asprintf(req, "%s/np",
232 lp_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
233 NT_STATUS_HAVE_NO_MEMORY(directory);
235 state = talloc(req, struct ipc_open_state);
236 NT_STATUS_HAVE_NO_MEMORY(state);
238 status = ntvfs_handle_new(ntvfs, req, &h);
239 NT_STATUS_NOT_OK_RETURN(status);
241 p = talloc(h, struct pipe_state);
242 NT_STATUS_HAVE_NO_MEMORY(p);
244 while (fname[0] == '\\') fname++;
246 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
247 NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
252 p->write_queue = tevent_queue_create(p, "ipc_write_queue");
253 NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
255 p->read_queue = tevent_queue_create(p, "ipc_read_queue");
256 NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
258 state->ipriv = ipriv;
263 status = auth_convert_server_info_saminfo3(state,
264 req->session_info->server_info,
266 NT_STATUS_NOT_OK_RETURN(status);
268 client_sa = ntvfs_get_peer_addr(ntvfs, state);
270 return NT_STATUS_INTERNAL_ERROR;
273 server_sa = ntvfs_get_my_addr(ntvfs, state);
275 return NT_STATUS_INTERNAL_ERROR;
278 ret = tsocket_address_inet_from_strings(state, "ip",
283 status = map_nt_error_from_unix(errno);
287 ret = tsocket_address_inet_from_strings(state, "ip",
292 status = map_nt_error_from_unix(errno);
296 subreq = tstream_npa_connect_send(p,
297 ipriv->ntvfs->ctx->event_ctx,
306 req->session_info->session_key);
307 NT_STATUS_HAVE_NO_MEMORY(subreq);
308 tevent_req_set_callback(subreq, ipc_open_done, state);
310 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
314 static void ipc_open_done(struct tevent_req *subreq)
316 struct ipc_open_state *state = tevent_req_callback_data(subreq,
317 struct ipc_open_state);
318 struct ipc_private *ipriv = state->ipriv;
319 struct pipe_state *p = state->p;
320 struct ntvfs_request *req = state->req;
321 union smb_open *oi = state->oi;
326 ret = tstream_npa_connect_recv(subreq, &sys_errno,
330 &p->allocation_size);
333 status = map_nt_error_from_unix(sys_errno);
337 DLIST_ADD(ipriv->pipe_list, p);
338 talloc_set_destructor(p, ipc_fd_destructor);
340 status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
341 if (!NT_STATUS_IS_OK(status)) {
345 switch (oi->generic.level) {
346 case RAW_OPEN_NTCREATEX:
347 ZERO_STRUCT(oi->ntcreatex.out);
348 oi->ntcreatex.out.file.ntvfs = p->handle;
349 oi->ntcreatex.out.oplock_level = 0;
350 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
351 oi->ntcreatex.out.create_time = 0;
352 oi->ntcreatex.out.access_time = 0;
353 oi->ntcreatex.out.write_time = 0;
354 oi->ntcreatex.out.change_time = 0;
355 oi->ntcreatex.out.attrib = FILE_ATTRIBUTE_NORMAL;
356 oi->ntcreatex.out.alloc_size = p->allocation_size;
357 oi->ntcreatex.out.size = 0;
358 oi->ntcreatex.out.file_type = p->file_type;
359 oi->ntcreatex.out.ipc_state = p->device_state;
360 oi->ntcreatex.out.is_directory = 0;
363 ZERO_STRUCT(oi->openx.out);
364 oi->openx.out.file.ntvfs = p->handle;
365 oi->openx.out.attrib = FILE_ATTRIBUTE_NORMAL;
366 oi->openx.out.write_time = 0;
367 oi->openx.out.size = 0;
368 oi->openx.out.access = 0;
369 oi->openx.out.ftype = p->file_type;
370 oi->openx.out.devstate = p->device_state;
371 oi->openx.out.action = 0;
372 oi->openx.out.unique_fid = 0;
373 oi->openx.out.access_mask = 0;
374 oi->openx.out.unknown = 0;
377 ZERO_STRUCT(oi->smb2.out);
378 oi->smb2.out.file.ntvfs = p->handle;
379 oi->smb2.out.oplock_level = oi->smb2.in.oplock_level;
380 oi->smb2.out.create_action = NTCREATEX_ACTION_EXISTED;
381 oi->smb2.out.create_time = 0;
382 oi->smb2.out.access_time = 0;
383 oi->smb2.out.write_time = 0;
384 oi->smb2.out.change_time = 0;
385 oi->smb2.out.alloc_size = p->allocation_size;
386 oi->smb2.out.size = 0;
387 oi->smb2.out.file_attr = FILE_ATTRIBUTE_NORMAL;
388 oi->smb2.out.reserved2 = 0;
395 req->async_states->status = status;
396 req->async_states->send_fn(req);
402 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
403 struct ntvfs_request *req, union smb_mkdir *md)
405 return NT_STATUS_ACCESS_DENIED;
411 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
412 struct ntvfs_request *req, struct smb_rmdir *rd)
414 return NT_STATUS_ACCESS_DENIED;
418 rename a set of files
420 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
421 struct ntvfs_request *req, union smb_rename *ren)
423 return NT_STATUS_ACCESS_DENIED;
429 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
430 struct ntvfs_request *req, struct smb_copy *cp)
432 return NT_STATUS_ACCESS_DENIED;
435 struct ipc_readv_next_vector_state {
442 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
443 uint8_t *buf, size_t len)
448 s->len = MIN(len, UINT16_MAX);
449 //DEBUG(0,("readv_next_vector_init[%u 0x%04X]\n", s->len, s->len));
452 static int ipc_readv_next_vector(struct tstream_context *stream,
455 struct iovec **_vector,
458 struct ipc_readv_next_vector_state *state =
459 (struct ipc_readv_next_vector_state *)private_data;
460 struct iovec *vector;
464 if (state->ofs == state->len) {
467 // DEBUG(0,("readv_next_vector done ofs[%u 0x%04X]\n",
468 // state->ofs, state->ofs));
472 pending = tstream_pending_bytes(stream);
477 if (pending == 0 && state->ofs != 0) {
478 /* return a short read */
481 // DEBUG(0,("readv_next_vector short read ofs[%u 0x%04X]\n",
482 // state->ofs, state->ofs));
487 /* we want at least one byte and recheck again */
490 size_t missing = state->len - state->ofs;
491 if (pending > missing) {
492 /* there's more available */
493 state->remaining = pending - missing;
496 /* read what we can get and recheck in the next cycle */
501 vector = talloc_array(mem_ctx, struct iovec, 1);
506 vector[0].iov_base = state->buf + state->ofs;
507 vector[0].iov_len = wanted;
509 state->ofs += wanted;
516 struct ipc_read_state {
517 struct ipc_private *ipriv;
518 struct pipe_state *p;
519 struct ntvfs_request *req;
521 struct ipc_readv_next_vector_state next_vector;
524 static void ipc_read_done(struct tevent_req *subreq);
529 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
530 struct ntvfs_request *req, union smb_read *rd)
532 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
534 struct pipe_state *p;
535 struct ipc_read_state *state;
536 struct tevent_req *subreq;
538 if (rd->generic.level != RAW_READ_GENERIC) {
539 return ntvfs_map_read(ntvfs, req, rd);
542 p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
544 return NT_STATUS_INVALID_HANDLE;
547 state = talloc(req, struct ipc_read_state);
548 NT_STATUS_HAVE_NO_MEMORY(state);
550 state->ipriv = ipriv;
555 /* rd->readx.out.data is already allocated */
556 ipc_readv_next_vector_init(&state->next_vector,
558 rd->readx.in.maxcnt);
560 subreq = tstream_readv_pdu_queue_send(req,
561 ipriv->ntvfs->ctx->event_ctx,
564 ipc_readv_next_vector,
565 &state->next_vector);
566 NT_STATUS_HAVE_NO_MEMORY(subreq);
567 tevent_req_set_callback(subreq, ipc_read_done, state);
569 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
573 static void ipc_read_done(struct tevent_req *subreq)
575 struct ipc_read_state *state =
576 tevent_req_callback_data(subreq,
577 struct ipc_read_state);
578 struct ntvfs_request *req = state->req;
579 union smb_read *rd = state->rd;
584 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
587 status = map_nt_error_from_unix(sys_errno);
591 status = NT_STATUS_OK;
592 if (state->next_vector.remaining > 0) {
593 status = STATUS_BUFFER_OVERFLOW;
596 rd->readx.out.remaining = state->next_vector.remaining;
597 rd->readx.out.compaction_mode = 0;
598 rd->readx.out.nread = ret;
601 req->async_states->status = status;
602 req->async_states->send_fn(req);
605 struct ipc_write_state {
606 struct ipc_private *ipriv;
607 struct pipe_state *p;
608 struct ntvfs_request *req;
613 static void ipc_write_done(struct tevent_req *subreq);
618 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
619 struct ntvfs_request *req, union smb_write *wr)
621 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
623 struct pipe_state *p;
624 struct tevent_req *subreq;
625 struct ipc_write_state *state;
627 if (wr->generic.level != RAW_WRITE_GENERIC) {
628 return ntvfs_map_write(ntvfs, req, wr);
631 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
633 return NT_STATUS_INVALID_HANDLE;
636 state = talloc(req, struct ipc_write_state);
637 NT_STATUS_HAVE_NO_MEMORY(state);
639 state->ipriv = ipriv;
643 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
644 state->iov.iov_len = wr->writex.in.count;
646 subreq = tstream_writev_queue_send(state,
647 ipriv->ntvfs->ctx->event_ctx,
651 NT_STATUS_HAVE_NO_MEMORY(subreq);
652 tevent_req_set_callback(subreq, ipc_write_done, state);
654 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
658 static void ipc_write_done(struct tevent_req *subreq)
660 struct ipc_write_state *state =
661 tevent_req_callback_data(subreq,
662 struct ipc_write_state);
663 struct ntvfs_request *req = state->req;
664 union smb_write *wr = state->wr;
669 ret = tstream_writev_queue_recv(subreq, &sys_errno);
672 status = map_nt_error_from_unix(sys_errno);
676 status = NT_STATUS_OK;
678 wr->writex.out.nwritten = ret;
679 wr->writex.out.remaining = 0;
682 req->async_states->status = status;
683 req->async_states->send_fn(req);
689 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
690 struct ntvfs_request *req,
693 return NT_STATUS_ACCESS_DENIED;
699 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
700 struct ntvfs_request *req,
703 return NT_STATUS_ACCESS_DENIED;
709 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
710 struct ntvfs_request *req, union smb_close *io)
712 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
714 struct pipe_state *p;
716 if (io->generic.level != RAW_CLOSE_CLOSE) {
717 return ntvfs_map_close(ntvfs, req, io);
720 p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
722 return NT_STATUS_INVALID_HANDLE;
733 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
734 struct ntvfs_request *req)
736 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
738 struct pipe_state *p, *next;
740 for (p=ipriv->pipe_list; p; p=next) {
742 if (p->handle->session_info == req->session_info &&
743 p->handle->smbpid == req->smbpid) {
752 logoff - closing files open by the user
754 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
755 struct ntvfs_request *req)
757 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
759 struct pipe_state *p, *next;
761 for (p=ipriv->pipe_list; p; p=next) {
763 if (p->handle->session_info == req->session_info) {
772 setup for an async call
774 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
775 struct ntvfs_request *req,
784 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
785 struct ntvfs_request *req)
787 return NT_STATUS_UNSUCCESSFUL;
793 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
794 struct ntvfs_request *req, union smb_lock *lck)
796 return NT_STATUS_ACCESS_DENIED;
800 set info on a open file
802 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
803 struct ntvfs_request *req, union smb_setfileinfo *info)
805 return NT_STATUS_ACCESS_DENIED;
809 query info on a open file
811 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
812 struct ntvfs_request *req, union smb_fileinfo *info)
814 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
816 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
818 return NT_STATUS_INVALID_HANDLE;
820 switch (info->generic.level) {
821 case RAW_FILEINFO_GENERIC:
823 ZERO_STRUCT(info->generic.out);
824 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
825 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
826 info->generic.out.alloc_size = 4096;
827 info->generic.out.nlink = 1;
828 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
829 info->generic.out.delete_pending = 1;
832 case RAW_FILEINFO_ALT_NAME_INFO:
833 case RAW_FILEINFO_ALT_NAME_INFORMATION:
834 case RAW_FILEINFO_STREAM_INFO:
835 case RAW_FILEINFO_STREAM_INFORMATION:
836 case RAW_FILEINFO_COMPRESSION_INFO:
837 case RAW_FILEINFO_COMPRESSION_INFORMATION:
838 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
839 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
840 return NT_STATUS_INVALID_PARAMETER;
841 case RAW_FILEINFO_ALL_EAS:
842 return NT_STATUS_ACCESS_DENIED;
844 return ntvfs_map_qfileinfo(ntvfs, req, info);
847 return NT_STATUS_ACCESS_DENIED;
852 return filesystem info
854 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
855 struct ntvfs_request *req, union smb_fsinfo *fs)
857 return NT_STATUS_ACCESS_DENIED;
861 return print queue info
863 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
864 struct ntvfs_request *req, union smb_lpq *lpq)
866 return NT_STATUS_ACCESS_DENIED;
870 list files in a directory matching a wildcard pattern
872 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
873 struct ntvfs_request *req, union smb_search_first *io,
874 void *search_private,
875 bool (*callback)(void *, const union smb_search_data *))
877 return NT_STATUS_ACCESS_DENIED;
881 continue listing files in a directory
883 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
884 struct ntvfs_request *req, union smb_search_next *io,
885 void *search_private,
886 bool (*callback)(void *, const union smb_search_data *))
888 return NT_STATUS_ACCESS_DENIED;
892 end listing files in a directory
894 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
895 struct ntvfs_request *req, union smb_search_close *io)
897 return NT_STATUS_ACCESS_DENIED;
900 struct ipc_trans_state {
901 struct ipc_private *ipriv;
902 struct pipe_state *p;
903 struct ntvfs_request *req;
904 struct smb_trans2 *trans;
905 struct iovec writev_iov;
906 struct ipc_readv_next_vector_state next_vector;
909 static void ipc_trans_writev_done(struct tevent_req *subreq);
910 static void ipc_trans_readv_done(struct tevent_req *subreq);
912 /* SMBtrans - handle a DCERPC command */
913 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
914 struct ntvfs_request *req, struct smb_trans2 *trans)
916 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
918 struct pipe_state *p;
921 struct ipc_trans_state *state;
922 struct tevent_req *subreq;
925 * the fnum is in setup[1], a 16 bit value
926 * the setup[*] values are already in host byteorder
927 * but ntvfs_handle_search_by_wire_key() expects
930 SSVAL(&fnum, 0, trans->in.setup[1]);
931 fnum_key = data_blob_const(&fnum, 2);
933 p = pipe_state_find_key(ipriv, req, &fnum_key);
935 return NT_STATUS_INVALID_HANDLE;
938 /* TODO: check if this os the correct logic */
939 if (tevent_queue_length(p->read_queue) > 0) {
940 return NT_STATUS_PIPE_BUSY;
943 state = talloc(req, struct ipc_trans_state);
944 NT_STATUS_HAVE_NO_MEMORY(state);
946 trans->out.setup_count = 0;
947 trans->out.setup = NULL;
948 trans->out.params = data_blob(NULL, 0);
949 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
950 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
952 state->ipriv = ipriv;
955 state->trans = trans;
956 state->writev_iov.iov_base = trans->in.data.data;
957 state->writev_iov.iov_len = trans->in.data.length;
959 ipc_readv_next_vector_init(&state->next_vector,
960 trans->out.data.data,
961 trans->out.data.length);
963 subreq = tstream_writev_queue_send(state,
964 ipriv->ntvfs->ctx->event_ctx,
967 &state->writev_iov, 1);
968 NT_STATUS_HAVE_NO_MEMORY(subreq);
969 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
971 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
975 static void ipc_trans_writev_done(struct tevent_req *subreq)
977 struct ipc_trans_state *state =
978 tevent_req_callback_data(subreq,
979 struct ipc_trans_state);
980 struct ipc_private *ipriv = state->ipriv;
981 struct pipe_state *p = state->p;
982 struct ntvfs_request *req = state->req;
987 ret = tstream_writev_queue_recv(subreq, &sys_errno);
990 status = NT_STATUS_PIPE_DISCONNECTED;
992 } else if (ret == -1) {
993 status = map_nt_error_from_unix(sys_errno);
997 subreq = tstream_readv_pdu_queue_send(state,
998 ipriv->ntvfs->ctx->event_ctx,
1001 ipc_readv_next_vector,
1002 &state->next_vector);
1004 status = NT_STATUS_NO_MEMORY;
1007 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1011 req->async_states->status = status;
1012 req->async_states->send_fn(req);
1015 static void ipc_trans_readv_done(struct tevent_req *subreq)
1017 struct ipc_trans_state *state =
1018 tevent_req_callback_data(subreq,
1019 struct ipc_trans_state);
1020 struct ntvfs_request *req = state->req;
1021 struct smb_trans2 *trans = state->trans;
1026 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1027 TALLOC_FREE(subreq);
1029 status = map_nt_error_from_unix(sys_errno);
1033 status = NT_STATUS_OK;
1034 if (state->next_vector.remaining > 0) {
1035 status = STATUS_BUFFER_OVERFLOW;
1038 trans->out.data.length = ret;
1041 req->async_states->status = status;
1042 req->async_states->send_fn(req);
1045 /* SMBtrans - set named pipe state */
1046 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1047 struct ntvfs_request *req, struct smb_trans2 *trans)
1049 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1050 struct ipc_private);
1051 struct pipe_state *p;
1054 /* the fnum is in setup[1] */
1055 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1057 p = pipe_state_find_key(ipriv, req, &fnum_key);
1059 return NT_STATUS_INVALID_HANDLE;
1062 if (trans->in.params.length != 2) {
1063 return NT_STATUS_INVALID_PARAMETER;
1066 p->device_state = SVAL(trans->in.params.data, 0);
1068 trans->out.setup_count = 0;
1069 trans->out.setup = NULL;
1070 trans->out.params = data_blob(NULL, 0);
1071 trans->out.data = data_blob(NULL, 0);
1073 return NT_STATUS_OK;
1077 /* SMBtrans - used to provide access to SMB pipes */
1078 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1079 struct ntvfs_request *req, struct smb_trans2 *trans)
1083 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1084 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1086 if (trans->in.setup_count != 2) {
1087 return NT_STATUS_INVALID_PARAMETER;
1090 switch (trans->in.setup[0]) {
1091 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1092 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1094 case TRANSACT_DCERPCCMD:
1095 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1098 status = NT_STATUS_INVALID_PARAMETER;
1105 struct ipc_ioctl_state {
1106 struct ipc_private *ipriv;
1107 struct pipe_state *p;
1108 struct ntvfs_request *req;
1109 union smb_ioctl *io;
1110 struct iovec writev_iov;
1111 struct ipc_readv_next_vector_state next_vector;
1114 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1115 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1117 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1118 struct ntvfs_request *req, union smb_ioctl *io)
1120 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1121 struct ipc_private);
1122 struct pipe_state *p;
1123 struct ipc_ioctl_state *state;
1124 struct tevent_req *subreq;
1126 switch (io->smb2.in.function) {
1127 case FSCTL_NAMED_PIPE_READ_WRITE:
1131 return NT_STATUS_FS_DRIVER_REQUIRED;
1134 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1136 return NT_STATUS_INVALID_HANDLE;
1139 /* TODO: check if this os the correct logic */
1140 if (tevent_queue_length(p->read_queue) > 0) {
1141 return NT_STATUS_PIPE_BUSY;
1144 state = talloc(req, struct ipc_ioctl_state);
1145 NT_STATUS_HAVE_NO_MEMORY(state);
1147 io->smb2.out._pad = 0;
1148 io->smb2.out.function = io->smb2.in.function;
1149 io->smb2.out.unknown2 = 0;
1150 io->smb2.out.unknown3 = 0;
1151 io->smb2.out.in = io->smb2.in.out;
1152 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1153 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1155 state->ipriv = ipriv;
1159 state->writev_iov.iov_base = io->smb2.in.out.data;
1160 state->writev_iov.iov_len = io->smb2.in.out.length;
1162 ipc_readv_next_vector_init(&state->next_vector,
1163 io->smb2.out.out.data,
1164 io->smb2.out.out.length);
1166 subreq = tstream_writev_queue_send(state,
1167 ipriv->ntvfs->ctx->event_ctx,
1170 &state->writev_iov, 1);
1171 NT_STATUS_HAVE_NO_MEMORY(subreq);
1172 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1174 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1175 return NT_STATUS_OK;
1178 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1180 struct ipc_ioctl_state *state =
1181 tevent_req_callback_data(subreq,
1182 struct ipc_ioctl_state);
1183 struct ipc_private *ipriv = state->ipriv;
1184 struct pipe_state *p = state->p;
1185 struct ntvfs_request *req = state->req;
1190 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1191 TALLOC_FREE(subreq);
1193 status = map_nt_error_from_unix(sys_errno);
1197 subreq = tstream_readv_pdu_queue_send(state,
1198 ipriv->ntvfs->ctx->event_ctx,
1201 ipc_readv_next_vector,
1202 &state->next_vector);
1204 status = NT_STATUS_NO_MEMORY;
1207 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1211 req->async_states->status = status;
1212 req->async_states->send_fn(req);
1215 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1217 struct ipc_ioctl_state *state =
1218 tevent_req_callback_data(subreq,
1219 struct ipc_ioctl_state);
1220 struct ntvfs_request *req = state->req;
1221 union smb_ioctl *io = state->io;
1226 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1227 TALLOC_FREE(subreq);
1229 status = map_nt_error_from_unix(sys_errno);
1233 status = NT_STATUS_OK;
1234 if (state->next_vector.remaining > 0) {
1235 status = STATUS_BUFFER_OVERFLOW;
1238 io->smb2.out.out.length = ret;
1241 req->async_states->status = status;
1242 req->async_states->send_fn(req);
1249 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1250 struct ntvfs_request *req, union smb_ioctl *io)
1252 switch (io->generic.level) {
1253 case RAW_IOCTL_SMB2:
1254 return ipc_ioctl_smb2(ntvfs, req, io);
1256 case RAW_IOCTL_SMB2_NO_HANDLE:
1257 return NT_STATUS_FS_DRIVER_REQUIRED;
1260 return NT_STATUS_ACCESS_DENIED;
1263 return NT_STATUS_ACCESS_DENIED;
1268 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1270 NTSTATUS ntvfs_ipc_init(void)
1273 struct ntvfs_ops ops;
1274 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1278 /* fill in the name and type */
1279 ops.name = "default";
1280 ops.type = NTVFS_IPC;
1282 /* fill in all the operations */
1283 ops.connect = ipc_connect;
1284 ops.disconnect = ipc_disconnect;
1285 ops.unlink = ipc_unlink;
1286 ops.chkpath = ipc_chkpath;
1287 ops.qpathinfo = ipc_qpathinfo;
1288 ops.setpathinfo = ipc_setpathinfo;
1289 ops.open = ipc_open;
1290 ops.mkdir = ipc_mkdir;
1291 ops.rmdir = ipc_rmdir;
1292 ops.rename = ipc_rename;
1293 ops.copy = ipc_copy;
1294 ops.ioctl = ipc_ioctl;
1295 ops.read = ipc_read;
1296 ops.write = ipc_write;
1297 ops.seek = ipc_seek;
1298 ops.flush = ipc_flush;
1299 ops.close = ipc_close;
1300 ops.exit = ipc_exit;
1301 ops.lock = ipc_lock;
1302 ops.setfileinfo = ipc_setfileinfo;
1303 ops.qfileinfo = ipc_qfileinfo;
1304 ops.fsinfo = ipc_fsinfo;
1306 ops.search_first = ipc_search_first;
1307 ops.search_next = ipc_search_next;
1308 ops.search_close = ipc_search_close;
1309 ops.trans = ipc_trans;
1310 ops.logoff = ipc_logoff;
1311 ops.async_setup = ipc_async_setup;
1312 ops.cancel = ipc_cancel;
1314 /* register ourselves with the NTVFS subsystem. */
1315 ret = ntvfs_register(&ops, &vers);
1317 if (!NT_STATUS_IS_OK(ret)) {
1318 DEBUG(0,("Failed to register IPC backend!\n"));