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 "../librpc/gen_ndr/rap.h"
31 #include "ntvfs/ipc/proto.h"
32 #include "../libcli/smb/smb_constants.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 "auth/credentials/credentials.h"
40 #include "auth/credentials/credentials_krb5.h"
41 #include <gssapi/gssapi.h>
42 #include "system/locale.h"
44 /* this is the private structure used to keep the state of an open
45 ipc$ connection. It needs to keep information about all open
48 struct ntvfs_module_context *ntvfs;
50 /* a list of open pipes */
52 struct pipe_state *next, *prev;
53 struct ipc_private *ipriv;
54 const char *pipe_name;
55 struct ntvfs_handle *handle;
56 struct tstream_context *npipe;
58 uint16_t device_state;
59 uint64_t allocation_size;
60 struct tevent_queue *write_queue;
61 struct tevent_queue *read_queue;
67 find a open pipe give a file handle
69 static struct pipe_state *pipe_state_find(struct ipc_private *ipriv, struct ntvfs_handle *handle)
74 p = ntvfs_handle_get_backend_data(handle, ipriv->ntvfs);
77 s = talloc_get_type(p, struct pipe_state);
84 find a open pipe give a wire fnum
86 static struct pipe_state *pipe_state_find_key(struct ipc_private *ipriv, struct ntvfs_request *req, const DATA_BLOB *key)
88 struct ntvfs_handle *h;
90 h = ntvfs_handle_search_by_wire_key(ipriv->ntvfs, req, key);
93 return pipe_state_find(ipriv, h);
98 connect to a share - always works
100 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
101 struct ntvfs_request *req,
102 union smb_tcon* tcon)
104 struct ipc_private *ipriv;
105 const char *sharename;
107 switch (tcon->generic.level) {
109 sharename = tcon->tcon.in.service;
112 sharename = tcon->tconx.in.path;
115 sharename = tcon->smb2.in.path;
118 return NT_STATUS_INVALID_LEVEL;
121 if (strncmp(sharename, "\\\\", 2) == 0) {
122 char *p = strchr(sharename+2, '\\');
128 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "IPC");
129 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
131 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "IPC");
132 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
134 if (tcon->generic.level == RAW_TCON_TCONX) {
135 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
136 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
139 /* prepare the private state for this connection */
140 ipriv = talloc(ntvfs, struct ipc_private);
141 NT_STATUS_HAVE_NO_MEMORY(ipriv);
143 ntvfs->private_data = ipriv;
145 ipriv->ntvfs = ntvfs;
146 ipriv->pipe_list = NULL;
152 disconnect from a share
154 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs)
162 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
163 struct ntvfs_request *req,
164 union smb_unlink *unl)
166 return NT_STATUS_ACCESS_DENIED;
170 check if a directory exists
172 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
173 struct ntvfs_request *req,
174 union smb_chkpath *cp)
176 return NT_STATUS_ACCESS_DENIED;
180 return info on a pathname
182 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
183 struct ntvfs_request *req, union smb_fileinfo *info)
185 switch (info->generic.level) {
186 case RAW_FILEINFO_GENERIC:
187 return NT_STATUS_INVALID_DEVICE_REQUEST;
188 case RAW_FILEINFO_GETATTR:
189 return NT_STATUS_ACCESS_DENIED;
191 return ntvfs_map_qpathinfo(ntvfs, req, info);
196 set info on a pathname
198 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
199 struct ntvfs_request *req, union smb_setfileinfo *st)
201 return NT_STATUS_ACCESS_DENIED;
206 destroy a open pipe structure
208 static int ipc_fd_destructor(struct pipe_state *p)
210 DLIST_REMOVE(p->ipriv->pipe_list, p);
211 ntvfs_handle_remove_backend_data(p->handle, p->ipriv->ntvfs);
215 struct ipc_open_state {
216 struct ipc_private *ipriv;
217 struct pipe_state *p;
218 struct ntvfs_request *req;
220 struct auth_session_info_transport *session_info_transport;
223 static void ipc_open_done(struct tevent_req *subreq);
226 check the pipename is valid
228 static NTSTATUS validate_pipename(const char *name)
231 if (!isalnum(*name) && *name != '_') {
232 return NT_STATUS_INVALID_PARAMETER;
240 open a file - used for MSRPC pipes
242 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
243 struct ntvfs_request *req, union smb_open *oi)
246 struct pipe_state *p;
247 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
249 struct ntvfs_handle *h;
250 struct ipc_open_state *state;
251 struct tevent_req *subreq;
253 const char *directory;
254 const struct tsocket_address *client_addr;
255 const struct tsocket_address *server_addr;
257 switch (oi->generic.level) {
258 case RAW_OPEN_NTCREATEX:
259 case RAW_OPEN_NTTRANS_CREATE:
260 fname = oi->ntcreatex.in.fname;
263 fname = oi->openx.in.fname;
266 fname = oi->smb2.in.fname;
269 return NT_STATUS_NOT_SUPPORTED;
272 directory = talloc_asprintf(req, "%s/np",
273 lpcfg_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
274 NT_STATUS_HAVE_NO_MEMORY(directory);
276 state = talloc(req, struct ipc_open_state);
277 NT_STATUS_HAVE_NO_MEMORY(state);
279 status = ntvfs_handle_new(ntvfs, req, &h);
280 NT_STATUS_NOT_OK_RETURN(status);
282 p = talloc(h, struct pipe_state);
283 NT_STATUS_HAVE_NO_MEMORY(p);
285 while (fname[0] == '\\') fname++;
287 /* check for valid characters in name */
288 fname = strlower_talloc(p, fname);
290 status = validate_pipename(fname);
291 NT_STATUS_NOT_OK_RETURN(status);
293 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
294 NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
299 p->write_queue = tevent_queue_create(p, "ipc_write_queue");
300 NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
302 p->read_queue = tevent_queue_create(p, "ipc_read_queue");
303 NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
305 state->ipriv = ipriv;
310 status = auth_session_info_transport_from_session(state,
312 ipriv->ntvfs->ctx->event_ctx,
313 ipriv->ntvfs->ctx->lp_ctx,
314 &state->session_info_transport);
316 NT_STATUS_NOT_OK_RETURN(status);
318 client_addr = ntvfs_get_local_address(ipriv->ntvfs);
319 server_addr = ntvfs_get_remote_address(ipriv->ntvfs);
321 subreq = tstream_npa_connect_send(p,
322 ipriv->ntvfs->ctx->event_ctx,
329 state->session_info_transport);
330 NT_STATUS_HAVE_NO_MEMORY(subreq);
331 tevent_req_set_callback(subreq, ipc_open_done, state);
333 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
337 static void ipc_open_done(struct tevent_req *subreq)
339 struct ipc_open_state *state = tevent_req_callback_data(subreq,
340 struct ipc_open_state);
341 struct ipc_private *ipriv = state->ipriv;
342 struct pipe_state *p = state->p;
343 struct ntvfs_request *req = state->req;
344 union smb_open *oi = state->oi;
349 ret = tstream_npa_connect_recv(subreq, &sys_errno,
353 &p->allocation_size);
356 status = map_nt_error_from_unix_common(sys_errno);
360 DLIST_ADD(ipriv->pipe_list, p);
361 talloc_set_destructor(p, ipc_fd_destructor);
363 status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
364 if (!NT_STATUS_IS_OK(status)) {
368 switch (oi->generic.level) {
369 case RAW_OPEN_NTCREATEX:
370 ZERO_STRUCT(oi->ntcreatex.out);
371 oi->ntcreatex.out.file.ntvfs = p->handle;
372 oi->ntcreatex.out.oplock_level = 0;
373 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
374 oi->ntcreatex.out.create_time = 0;
375 oi->ntcreatex.out.access_time = 0;
376 oi->ntcreatex.out.write_time = 0;
377 oi->ntcreatex.out.change_time = 0;
378 oi->ntcreatex.out.attrib = FILE_ATTRIBUTE_NORMAL;
379 oi->ntcreatex.out.alloc_size = p->allocation_size;
380 oi->ntcreatex.out.size = 0;
381 oi->ntcreatex.out.file_type = p->file_type;
382 oi->ntcreatex.out.ipc_state = p->device_state;
383 oi->ntcreatex.out.is_directory = 0;
386 ZERO_STRUCT(oi->openx.out);
387 oi->openx.out.file.ntvfs = p->handle;
388 oi->openx.out.attrib = FILE_ATTRIBUTE_NORMAL;
389 oi->openx.out.write_time = 0;
390 oi->openx.out.size = 0;
391 oi->openx.out.access = 0;
392 oi->openx.out.ftype = p->file_type;
393 oi->openx.out.devstate = p->device_state;
394 oi->openx.out.action = 0;
395 oi->openx.out.unique_fid = 0;
396 oi->openx.out.access_mask = 0;
397 oi->openx.out.unknown = 0;
400 ZERO_STRUCT(oi->smb2.out);
401 oi->smb2.out.file.ntvfs = p->handle;
402 oi->smb2.out.oplock_level = oi->smb2.in.oplock_level;
403 oi->smb2.out.create_action = NTCREATEX_ACTION_EXISTED;
404 oi->smb2.out.create_time = 0;
405 oi->smb2.out.access_time = 0;
406 oi->smb2.out.write_time = 0;
407 oi->smb2.out.change_time = 0;
408 oi->smb2.out.alloc_size = p->allocation_size;
409 oi->smb2.out.size = 0;
410 oi->smb2.out.file_attr = FILE_ATTRIBUTE_NORMAL;
411 oi->smb2.out.reserved2 = 0;
418 req->async_states->status = status;
419 req->async_states->send_fn(req);
425 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
426 struct ntvfs_request *req, union smb_mkdir *md)
428 return NT_STATUS_ACCESS_DENIED;
434 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
435 struct ntvfs_request *req, struct smb_rmdir *rd)
437 return NT_STATUS_ACCESS_DENIED;
441 rename a set of files
443 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
444 struct ntvfs_request *req, union smb_rename *ren)
446 return NT_STATUS_ACCESS_DENIED;
452 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
453 struct ntvfs_request *req, struct smb_copy *cp)
455 return NT_STATUS_ACCESS_DENIED;
458 struct ipc_readv_next_vector_state {
465 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
466 uint8_t *buf, size_t len)
471 s->len = MIN(len, UINT16_MAX);
474 static int ipc_readv_next_vector(struct tstream_context *stream,
477 struct iovec **_vector,
480 struct ipc_readv_next_vector_state *state =
481 (struct ipc_readv_next_vector_state *)private_data;
482 struct iovec *vector;
486 if (state->ofs == state->len) {
492 pending = tstream_pending_bytes(stream);
497 if (pending == 0 && state->ofs != 0) {
498 /* return a short read */
505 /* we want at least one byte and recheck again */
508 size_t missing = state->len - state->ofs;
509 if (pending > missing) {
510 /* there's more available */
511 state->remaining = pending - missing;
514 /* read what we can get and recheck in the next cycle */
519 vector = talloc_array(mem_ctx, struct iovec, 1);
524 vector[0].iov_base = (char *) (state->buf + state->ofs);
525 vector[0].iov_len = wanted;
527 state->ofs += wanted;
534 struct ipc_read_state {
535 struct ipc_private *ipriv;
536 struct pipe_state *p;
537 struct ntvfs_request *req;
539 struct ipc_readv_next_vector_state next_vector;
542 static void ipc_read_done(struct tevent_req *subreq);
547 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
548 struct ntvfs_request *req, union smb_read *rd)
550 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
552 struct pipe_state *p;
553 struct ipc_read_state *state;
554 struct tevent_req *subreq;
556 if (rd->generic.level != RAW_READ_GENERIC) {
557 return ntvfs_map_read(ntvfs, req, rd);
560 p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
562 return NT_STATUS_INVALID_HANDLE;
565 state = talloc(req, struct ipc_read_state);
566 NT_STATUS_HAVE_NO_MEMORY(state);
568 state->ipriv = ipriv;
573 /* rd->readx.out.data is already allocated */
574 ipc_readv_next_vector_init(&state->next_vector,
576 rd->readx.in.maxcnt);
578 subreq = tstream_readv_pdu_queue_send(req,
579 ipriv->ntvfs->ctx->event_ctx,
582 ipc_readv_next_vector,
583 &state->next_vector);
584 NT_STATUS_HAVE_NO_MEMORY(subreq);
585 tevent_req_set_callback(subreq, ipc_read_done, state);
587 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
591 static void ipc_read_done(struct tevent_req *subreq)
593 struct ipc_read_state *state =
594 tevent_req_callback_data(subreq,
595 struct ipc_read_state);
596 struct ntvfs_request *req = state->req;
597 union smb_read *rd = state->rd;
602 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
605 status = map_nt_error_from_unix_common(sys_errno);
609 status = NT_STATUS_OK;
610 if (state->next_vector.remaining > 0) {
611 status = STATUS_BUFFER_OVERFLOW;
614 rd->readx.out.remaining = state->next_vector.remaining;
615 rd->readx.out.compaction_mode = 0;
616 rd->readx.out.nread = ret;
619 req->async_states->status = status;
620 req->async_states->send_fn(req);
623 struct ipc_write_state {
624 struct ipc_private *ipriv;
625 struct pipe_state *p;
626 struct ntvfs_request *req;
631 static void ipc_write_done(struct tevent_req *subreq);
636 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
637 struct ntvfs_request *req, union smb_write *wr)
639 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
641 struct pipe_state *p;
642 struct tevent_req *subreq;
643 struct ipc_write_state *state;
645 if (wr->generic.level != RAW_WRITE_GENERIC) {
646 return ntvfs_map_write(ntvfs, req, wr);
649 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
651 return NT_STATUS_INVALID_HANDLE;
654 state = talloc(req, struct ipc_write_state);
655 NT_STATUS_HAVE_NO_MEMORY(state);
657 state->ipriv = ipriv;
661 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
662 state->iov.iov_len = wr->writex.in.count;
664 subreq = tstream_writev_queue_send(state,
665 ipriv->ntvfs->ctx->event_ctx,
669 NT_STATUS_HAVE_NO_MEMORY(subreq);
670 tevent_req_set_callback(subreq, ipc_write_done, state);
672 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
676 static void ipc_write_done(struct tevent_req *subreq)
678 struct ipc_write_state *state =
679 tevent_req_callback_data(subreq,
680 struct ipc_write_state);
681 struct ntvfs_request *req = state->req;
682 union smb_write *wr = state->wr;
687 ret = tstream_writev_queue_recv(subreq, &sys_errno);
690 status = map_nt_error_from_unix_common(sys_errno);
694 status = NT_STATUS_OK;
696 wr->writex.out.nwritten = ret;
697 wr->writex.out.remaining = 0;
700 req->async_states->status = status;
701 req->async_states->send_fn(req);
707 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
708 struct ntvfs_request *req,
711 return NT_STATUS_ACCESS_DENIED;
717 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
718 struct ntvfs_request *req,
721 return NT_STATUS_ACCESS_DENIED;
727 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
728 struct ntvfs_request *req, union smb_close *io)
730 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
732 struct pipe_state *p;
734 if (io->generic.level != RAW_CLOSE_CLOSE) {
735 return ntvfs_map_close(ntvfs, req, io);
738 p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
740 return NT_STATUS_INVALID_HANDLE;
751 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
752 struct ntvfs_request *req)
754 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
756 struct pipe_state *p, *next;
758 for (p=ipriv->pipe_list; p; p=next) {
760 if (p->handle->session_info == req->session_info &&
761 p->handle->smbpid == req->smbpid) {
770 logoff - closing files open by the user
772 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
773 struct ntvfs_request *req)
775 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
777 struct pipe_state *p, *next;
779 for (p=ipriv->pipe_list; p; p=next) {
781 if (p->handle->session_info == req->session_info) {
790 setup for an async call
792 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
793 struct ntvfs_request *req,
802 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
803 struct ntvfs_request *req)
805 return NT_STATUS_UNSUCCESSFUL;
811 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
812 struct ntvfs_request *req, union smb_lock *lck)
814 return NT_STATUS_ACCESS_DENIED;
818 set info on a open file
820 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
821 struct ntvfs_request *req, union smb_setfileinfo *info)
823 return NT_STATUS_ACCESS_DENIED;
827 query info on a open file
829 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
830 struct ntvfs_request *req, union smb_fileinfo *info)
832 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
834 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
836 return NT_STATUS_INVALID_HANDLE;
838 switch (info->generic.level) {
839 case RAW_FILEINFO_GENERIC:
841 ZERO_STRUCT(info->generic.out);
842 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
843 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
844 info->generic.out.alloc_size = 4096;
845 info->generic.out.nlink = 1;
846 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
847 info->generic.out.delete_pending = 1;
850 case RAW_FILEINFO_ALT_NAME_INFO:
851 case RAW_FILEINFO_ALT_NAME_INFORMATION:
852 case RAW_FILEINFO_STREAM_INFO:
853 case RAW_FILEINFO_STREAM_INFORMATION:
854 case RAW_FILEINFO_COMPRESSION_INFO:
855 case RAW_FILEINFO_COMPRESSION_INFORMATION:
856 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
857 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
858 return NT_STATUS_INVALID_PARAMETER;
859 case RAW_FILEINFO_ALL_EAS:
860 return NT_STATUS_ACCESS_DENIED;
862 return ntvfs_map_qfileinfo(ntvfs, req, info);
868 return filesystem info
870 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
871 struct ntvfs_request *req, union smb_fsinfo *fs)
873 return NT_STATUS_ACCESS_DENIED;
877 return print queue info
879 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
880 struct ntvfs_request *req, union smb_lpq *lpq)
882 return NT_STATUS_ACCESS_DENIED;
886 list files in a directory matching a wildcard pattern
888 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
889 struct ntvfs_request *req, union smb_search_first *io,
890 void *search_private,
891 bool (*callback)(void *, const union smb_search_data *))
893 return NT_STATUS_ACCESS_DENIED;
897 continue listing files in a directory
899 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
900 struct ntvfs_request *req, union smb_search_next *io,
901 void *search_private,
902 bool (*callback)(void *, const union smb_search_data *))
904 return NT_STATUS_ACCESS_DENIED;
908 end listing files in a directory
910 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
911 struct ntvfs_request *req, union smb_search_close *io)
913 return NT_STATUS_ACCESS_DENIED;
916 struct ipc_trans_state {
917 struct ipc_private *ipriv;
918 struct pipe_state *p;
919 struct ntvfs_request *req;
920 struct smb_trans2 *trans;
921 struct iovec writev_iov;
922 struct ipc_readv_next_vector_state next_vector;
925 static void ipc_trans_writev_done(struct tevent_req *subreq);
926 static void ipc_trans_readv_done(struct tevent_req *subreq);
928 /* SMBtrans - handle a DCERPC command */
929 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
930 struct ntvfs_request *req, struct smb_trans2 *trans)
932 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
934 struct pipe_state *p;
937 struct ipc_trans_state *state;
938 struct tevent_req *subreq;
941 * the fnum is in setup[1], a 16 bit value
942 * the setup[*] values are already in host byteorder
943 * but ntvfs_handle_search_by_wire_key() expects
946 SSVAL(&fnum, 0, trans->in.setup[1]);
947 fnum_key = data_blob_const(&fnum, 2);
949 p = pipe_state_find_key(ipriv, req, &fnum_key);
951 return NT_STATUS_INVALID_HANDLE;
955 * Trans requests are only allowed
956 * if no other Trans or Read is active
958 if (tevent_queue_length(p->read_queue) > 0) {
959 return NT_STATUS_PIPE_BUSY;
962 state = talloc(req, struct ipc_trans_state);
963 NT_STATUS_HAVE_NO_MEMORY(state);
965 trans->out.setup_count = 0;
966 trans->out.setup = NULL;
967 trans->out.params = data_blob(NULL, 0);
968 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
969 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
971 state->ipriv = ipriv;
974 state->trans = trans;
975 state->writev_iov.iov_base = (char *) trans->in.data.data;
976 state->writev_iov.iov_len = trans->in.data.length;
978 ipc_readv_next_vector_init(&state->next_vector,
979 trans->out.data.data,
980 trans->out.data.length);
982 subreq = tstream_writev_queue_send(state,
983 ipriv->ntvfs->ctx->event_ctx,
986 &state->writev_iov, 1);
987 NT_STATUS_HAVE_NO_MEMORY(subreq);
988 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
990 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
994 static void ipc_trans_writev_done(struct tevent_req *subreq)
996 struct ipc_trans_state *state =
997 tevent_req_callback_data(subreq,
998 struct ipc_trans_state);
999 struct ipc_private *ipriv = state->ipriv;
1000 struct pipe_state *p = state->p;
1001 struct ntvfs_request *req = state->req;
1006 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1007 TALLOC_FREE(subreq);
1009 status = NT_STATUS_PIPE_DISCONNECTED;
1011 } else if (ret == -1) {
1012 status = map_nt_error_from_unix_common(sys_errno);
1016 subreq = tstream_readv_pdu_queue_send(state,
1017 ipriv->ntvfs->ctx->event_ctx,
1020 ipc_readv_next_vector,
1021 &state->next_vector);
1023 status = NT_STATUS_NO_MEMORY;
1026 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1030 req->async_states->status = status;
1031 req->async_states->send_fn(req);
1034 static void ipc_trans_readv_done(struct tevent_req *subreq)
1036 struct ipc_trans_state *state =
1037 tevent_req_callback_data(subreq,
1038 struct ipc_trans_state);
1039 struct ntvfs_request *req = state->req;
1040 struct smb_trans2 *trans = state->trans;
1045 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1046 TALLOC_FREE(subreq);
1048 status = map_nt_error_from_unix_common(sys_errno);
1052 status = NT_STATUS_OK;
1053 if (state->next_vector.remaining > 0) {
1054 status = STATUS_BUFFER_OVERFLOW;
1057 trans->out.data.length = ret;
1060 req->async_states->status = status;
1061 req->async_states->send_fn(req);
1064 /* SMBtrans - set named pipe state */
1065 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1066 struct ntvfs_request *req, struct smb_trans2 *trans)
1068 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1069 struct ipc_private);
1070 struct pipe_state *p;
1073 /* the fnum is in setup[1] */
1074 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1076 p = pipe_state_find_key(ipriv, req, &fnum_key);
1078 return NT_STATUS_INVALID_HANDLE;
1081 if (trans->in.params.length != 2) {
1082 return NT_STATUS_INVALID_PARAMETER;
1086 * TODO: pass this to the tstream_npa logic
1088 p->device_state = SVAL(trans->in.params.data, 0);
1090 trans->out.setup_count = 0;
1091 trans->out.setup = NULL;
1092 trans->out.params = data_blob(NULL, 0);
1093 trans->out.data = data_blob(NULL, 0);
1095 return NT_STATUS_OK;
1099 /* SMBtrans - used to provide access to SMB pipes */
1100 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1101 struct ntvfs_request *req, struct smb_trans2 *trans)
1105 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1106 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1108 if (trans->in.setup_count != 2) {
1109 return NT_STATUS_INVALID_PARAMETER;
1112 switch (trans->in.setup[0]) {
1113 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1114 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1116 case TRANSACT_DCERPCCMD:
1117 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1120 status = NT_STATUS_INVALID_PARAMETER;
1127 struct ipc_ioctl_state {
1128 struct ipc_private *ipriv;
1129 struct pipe_state *p;
1130 struct ntvfs_request *req;
1131 union smb_ioctl *io;
1132 struct iovec writev_iov;
1133 struct ipc_readv_next_vector_state next_vector;
1136 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1137 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1139 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1140 struct ntvfs_request *req, union smb_ioctl *io)
1142 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1143 struct ipc_private);
1144 struct pipe_state *p;
1145 struct ipc_ioctl_state *state;
1146 struct tevent_req *subreq;
1148 switch (io->smb2.in.function) {
1149 case FSCTL_NAMED_PIPE_READ_WRITE:
1153 return NT_STATUS_FS_DRIVER_REQUIRED;
1156 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1158 return NT_STATUS_INVALID_HANDLE;
1162 * Trans requests are only allowed
1163 * if no other Trans or Read is active
1165 if (tevent_queue_length(p->read_queue) > 0) {
1166 return NT_STATUS_PIPE_BUSY;
1169 state = talloc(req, struct ipc_ioctl_state);
1170 NT_STATUS_HAVE_NO_MEMORY(state);
1172 io->smb2.out._pad = 0;
1173 io->smb2.out.function = io->smb2.in.function;
1174 io->smb2.out.unknown2 = 0;
1175 io->smb2.out.unknown3 = 0;
1176 io->smb2.out.in = io->smb2.in.out;
1177 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1178 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1180 state->ipriv = ipriv;
1184 state->writev_iov.iov_base = (char *) io->smb2.in.out.data;
1185 state->writev_iov.iov_len = io->smb2.in.out.length;
1187 ipc_readv_next_vector_init(&state->next_vector,
1188 io->smb2.out.out.data,
1189 io->smb2.out.out.length);
1191 subreq = tstream_writev_queue_send(state,
1192 ipriv->ntvfs->ctx->event_ctx,
1195 &state->writev_iov, 1);
1196 NT_STATUS_HAVE_NO_MEMORY(subreq);
1197 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1199 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1200 return NT_STATUS_OK;
1203 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1205 struct ipc_ioctl_state *state =
1206 tevent_req_callback_data(subreq,
1207 struct ipc_ioctl_state);
1208 struct ipc_private *ipriv = state->ipriv;
1209 struct pipe_state *p = state->p;
1210 struct ntvfs_request *req = state->req;
1215 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1216 TALLOC_FREE(subreq);
1218 status = map_nt_error_from_unix_common(sys_errno);
1222 subreq = tstream_readv_pdu_queue_send(state,
1223 ipriv->ntvfs->ctx->event_ctx,
1226 ipc_readv_next_vector,
1227 &state->next_vector);
1229 status = NT_STATUS_NO_MEMORY;
1232 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1236 req->async_states->status = status;
1237 req->async_states->send_fn(req);
1240 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1242 struct ipc_ioctl_state *state =
1243 tevent_req_callback_data(subreq,
1244 struct ipc_ioctl_state);
1245 struct ntvfs_request *req = state->req;
1246 union smb_ioctl *io = state->io;
1251 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1252 TALLOC_FREE(subreq);
1254 status = map_nt_error_from_unix_common(sys_errno);
1258 status = NT_STATUS_OK;
1259 if (state->next_vector.remaining > 0) {
1260 status = STATUS_BUFFER_OVERFLOW;
1263 io->smb2.out.out.length = ret;
1266 req->async_states->status = status;
1267 req->async_states->send_fn(req);
1273 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1274 struct ntvfs_request *req, union smb_ioctl *io)
1276 switch (io->generic.level) {
1277 case RAW_IOCTL_SMB2:
1278 return ipc_ioctl_smb2(ntvfs, req, io);
1280 case RAW_IOCTL_SMB2_NO_HANDLE:
1281 return NT_STATUS_FS_DRIVER_REQUIRED;
1284 return NT_STATUS_ACCESS_DENIED;
1290 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1292 NTSTATUS ntvfs_ipc_init(void)
1295 struct ntvfs_ops ops;
1296 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1300 /* fill in the name and type */
1301 ops.name = "default";
1302 ops.type = NTVFS_IPC;
1304 /* fill in all the operations */
1305 ops.connect = ipc_connect;
1306 ops.disconnect = ipc_disconnect;
1307 ops.unlink = ipc_unlink;
1308 ops.chkpath = ipc_chkpath;
1309 ops.qpathinfo = ipc_qpathinfo;
1310 ops.setpathinfo = ipc_setpathinfo;
1311 ops.open = ipc_open;
1312 ops.mkdir = ipc_mkdir;
1313 ops.rmdir = ipc_rmdir;
1314 ops.rename = ipc_rename;
1315 ops.copy = ipc_copy;
1316 ops.ioctl = ipc_ioctl;
1317 ops.read = ipc_read;
1318 ops.write = ipc_write;
1319 ops.seek = ipc_seek;
1320 ops.flush = ipc_flush;
1321 ops.close = ipc_close;
1322 ops.exit = ipc_exit;
1323 ops.lock = ipc_lock;
1324 ops.setfileinfo = ipc_setfileinfo;
1325 ops.qfileinfo = ipc_qfileinfo;
1326 ops.fsinfo = ipc_fsinfo;
1328 ops.search_first = ipc_search_first;
1329 ops.search_next = ipc_search_next;
1330 ops.search_close = ipc_search_close;
1331 ops.trans = ipc_trans;
1332 ops.logoff = ipc_logoff;
1333 ops.async_setup = ipc_async_setup;
1334 ops.cancel = ipc_cancel;
1336 /* register ourselves with the NTVFS subsystem. */
1337 ret = ntvfs_register(&ops, &vers);
1339 if (!NT_STATUS_IS_OK(ret)) {
1340 DEBUG(0,("Failed to register IPC backend!\n"));