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/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 "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 netr_SamInfo3 *info3;
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)) return NT_STATUS_INVALID_PARAMETER;
238 open a file - used for MSRPC pipes
240 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
241 struct ntvfs_request *req, union smb_open *oi)
244 struct pipe_state *p;
245 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
247 struct smb_iconv_convenience *smb_ic
248 = lp_iconv_convenience(ipriv->ntvfs->ctx->lp_ctx);
249 struct ntvfs_handle *h;
250 struct ipc_open_state *state;
251 struct tevent_req *subreq;
253 const char *directory;
254 struct tsocket_address *client_addr;
255 struct tsocket_address *server_addr;
257 DATA_BLOB delegated_creds = data_blob_null;
259 switch (oi->generic.level) {
260 case RAW_OPEN_NTCREATEX:
261 case RAW_OPEN_NTTRANS_CREATE:
262 fname = oi->ntcreatex.in.fname;
265 fname = oi->openx.in.fname;
268 fname = oi->smb2.in.fname;
271 return NT_STATUS_NOT_SUPPORTED;
274 directory = talloc_asprintf(req, "%s/np",
275 lp_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
276 NT_STATUS_HAVE_NO_MEMORY(directory);
278 state = talloc(req, struct ipc_open_state);
279 NT_STATUS_HAVE_NO_MEMORY(state);
281 status = ntvfs_handle_new(ntvfs, req, &h);
282 NT_STATUS_NOT_OK_RETURN(status);
284 p = talloc(h, struct pipe_state);
285 NT_STATUS_HAVE_NO_MEMORY(p);
287 while (fname[0] == '\\') fname++;
289 /* check for valid characters in name */
290 fname = strlower_talloc(p, fname);
292 status = validate_pipename(fname);
293 NT_STATUS_NOT_OK_RETURN(status);
295 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
296 NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
301 p->write_queue = tevent_queue_create(p, "ipc_write_queue");
302 NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
304 p->read_queue = tevent_queue_create(p, "ipc_read_queue");
305 NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
307 state->ipriv = ipriv;
312 status = auth_convert_server_info_saminfo3(state,
313 req->session_info->server_info,
315 NT_STATUS_NOT_OK_RETURN(status);
317 client_addr = ntvfs_get_local_address(ipriv->ntvfs);
318 server_addr = ntvfs_get_remote_address(ipriv->ntvfs);
320 if (req->session_info->credentials) {
321 struct gssapi_creds_container *gcc;
323 OM_uint32 minor_status;
324 gss_buffer_desc cred_token;
325 const char *error_string;
327 ret = cli_credentials_get_client_gss_creds(req->session_info->credentials,
328 ipriv->ntvfs->ctx->event_ctx,
329 ipriv->ntvfs->ctx->lp_ctx,
330 &gcc, &error_string);
335 gret = gss_export_cred(&minor_status,
338 if (gret != GSS_S_COMPLETE) {
339 return NT_STATUS_INTERNAL_ERROR;
342 if (cred_token.length) {
343 delegated_creds = data_blob_talloc(req,
346 gss_release_buffer(&minor_status, &cred_token);
347 NT_STATUS_HAVE_NO_MEMORY(delegated_creds.data);
353 subreq = tstream_npa_connect_send(p,
354 ipriv->ntvfs->ctx->event_ctx,
363 req->session_info->session_key,
365 NT_STATUS_HAVE_NO_MEMORY(subreq);
366 tevent_req_set_callback(subreq, ipc_open_done, state);
368 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
372 static void ipc_open_done(struct tevent_req *subreq)
374 struct ipc_open_state *state = tevent_req_callback_data(subreq,
375 struct ipc_open_state);
376 struct ipc_private *ipriv = state->ipriv;
377 struct pipe_state *p = state->p;
378 struct ntvfs_request *req = state->req;
379 union smb_open *oi = state->oi;
384 ret = tstream_npa_connect_recv(subreq, &sys_errno,
388 &p->allocation_size);
391 status = map_nt_error_from_unix(sys_errno);
395 DLIST_ADD(ipriv->pipe_list, p);
396 talloc_set_destructor(p, ipc_fd_destructor);
398 status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
399 if (!NT_STATUS_IS_OK(status)) {
403 switch (oi->generic.level) {
404 case RAW_OPEN_NTCREATEX:
405 ZERO_STRUCT(oi->ntcreatex.out);
406 oi->ntcreatex.out.file.ntvfs = p->handle;
407 oi->ntcreatex.out.oplock_level = 0;
408 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
409 oi->ntcreatex.out.create_time = 0;
410 oi->ntcreatex.out.access_time = 0;
411 oi->ntcreatex.out.write_time = 0;
412 oi->ntcreatex.out.change_time = 0;
413 oi->ntcreatex.out.attrib = FILE_ATTRIBUTE_NORMAL;
414 oi->ntcreatex.out.alloc_size = p->allocation_size;
415 oi->ntcreatex.out.size = 0;
416 oi->ntcreatex.out.file_type = p->file_type;
417 oi->ntcreatex.out.ipc_state = p->device_state;
418 oi->ntcreatex.out.is_directory = 0;
421 ZERO_STRUCT(oi->openx.out);
422 oi->openx.out.file.ntvfs = p->handle;
423 oi->openx.out.attrib = FILE_ATTRIBUTE_NORMAL;
424 oi->openx.out.write_time = 0;
425 oi->openx.out.size = 0;
426 oi->openx.out.access = 0;
427 oi->openx.out.ftype = p->file_type;
428 oi->openx.out.devstate = p->device_state;
429 oi->openx.out.action = 0;
430 oi->openx.out.unique_fid = 0;
431 oi->openx.out.access_mask = 0;
432 oi->openx.out.unknown = 0;
435 ZERO_STRUCT(oi->smb2.out);
436 oi->smb2.out.file.ntvfs = p->handle;
437 oi->smb2.out.oplock_level = oi->smb2.in.oplock_level;
438 oi->smb2.out.create_action = NTCREATEX_ACTION_EXISTED;
439 oi->smb2.out.create_time = 0;
440 oi->smb2.out.access_time = 0;
441 oi->smb2.out.write_time = 0;
442 oi->smb2.out.change_time = 0;
443 oi->smb2.out.alloc_size = p->allocation_size;
444 oi->smb2.out.size = 0;
445 oi->smb2.out.file_attr = FILE_ATTRIBUTE_NORMAL;
446 oi->smb2.out.reserved2 = 0;
453 req->async_states->status = status;
454 req->async_states->send_fn(req);
460 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
461 struct ntvfs_request *req, union smb_mkdir *md)
463 return NT_STATUS_ACCESS_DENIED;
469 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
470 struct ntvfs_request *req, struct smb_rmdir *rd)
472 return NT_STATUS_ACCESS_DENIED;
476 rename a set of files
478 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
479 struct ntvfs_request *req, union smb_rename *ren)
481 return NT_STATUS_ACCESS_DENIED;
487 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
488 struct ntvfs_request *req, struct smb_copy *cp)
490 return NT_STATUS_ACCESS_DENIED;
493 struct ipc_readv_next_vector_state {
500 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
501 uint8_t *buf, size_t len)
506 s->len = MIN(len, UINT16_MAX);
509 static int ipc_readv_next_vector(struct tstream_context *stream,
512 struct iovec **_vector,
515 struct ipc_readv_next_vector_state *state =
516 (struct ipc_readv_next_vector_state *)private_data;
517 struct iovec *vector;
521 if (state->ofs == state->len) {
527 pending = tstream_pending_bytes(stream);
532 if (pending == 0 && state->ofs != 0) {
533 /* return a short read */
540 /* we want at least one byte and recheck again */
543 size_t missing = state->len - state->ofs;
544 if (pending > missing) {
545 /* there's more available */
546 state->remaining = pending - missing;
549 /* read what we can get and recheck in the next cycle */
554 vector = talloc_array(mem_ctx, struct iovec, 1);
559 vector[0].iov_base = state->buf + state->ofs;
560 vector[0].iov_len = wanted;
562 state->ofs += wanted;
569 struct ipc_read_state {
570 struct ipc_private *ipriv;
571 struct pipe_state *p;
572 struct ntvfs_request *req;
574 struct ipc_readv_next_vector_state next_vector;
577 static void ipc_read_done(struct tevent_req *subreq);
582 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
583 struct ntvfs_request *req, union smb_read *rd)
585 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
587 struct pipe_state *p;
588 struct ipc_read_state *state;
589 struct tevent_req *subreq;
591 if (rd->generic.level != RAW_READ_GENERIC) {
592 return ntvfs_map_read(ntvfs, req, rd);
595 p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
597 return NT_STATUS_INVALID_HANDLE;
600 state = talloc(req, struct ipc_read_state);
601 NT_STATUS_HAVE_NO_MEMORY(state);
603 state->ipriv = ipriv;
608 /* rd->readx.out.data is already allocated */
609 ipc_readv_next_vector_init(&state->next_vector,
611 rd->readx.in.maxcnt);
613 subreq = tstream_readv_pdu_queue_send(req,
614 ipriv->ntvfs->ctx->event_ctx,
617 ipc_readv_next_vector,
618 &state->next_vector);
619 NT_STATUS_HAVE_NO_MEMORY(subreq);
620 tevent_req_set_callback(subreq, ipc_read_done, state);
622 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
626 static void ipc_read_done(struct tevent_req *subreq)
628 struct ipc_read_state *state =
629 tevent_req_callback_data(subreq,
630 struct ipc_read_state);
631 struct ntvfs_request *req = state->req;
632 union smb_read *rd = state->rd;
637 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
640 status = map_nt_error_from_unix(sys_errno);
644 status = NT_STATUS_OK;
645 if (state->next_vector.remaining > 0) {
646 status = STATUS_BUFFER_OVERFLOW;
649 rd->readx.out.remaining = state->next_vector.remaining;
650 rd->readx.out.compaction_mode = 0;
651 rd->readx.out.nread = ret;
654 req->async_states->status = status;
655 req->async_states->send_fn(req);
658 struct ipc_write_state {
659 struct ipc_private *ipriv;
660 struct pipe_state *p;
661 struct ntvfs_request *req;
666 static void ipc_write_done(struct tevent_req *subreq);
671 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
672 struct ntvfs_request *req, union smb_write *wr)
674 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
676 struct pipe_state *p;
677 struct tevent_req *subreq;
678 struct ipc_write_state *state;
680 if (wr->generic.level != RAW_WRITE_GENERIC) {
681 return ntvfs_map_write(ntvfs, req, wr);
684 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
686 return NT_STATUS_INVALID_HANDLE;
689 state = talloc(req, struct ipc_write_state);
690 NT_STATUS_HAVE_NO_MEMORY(state);
692 state->ipriv = ipriv;
696 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
697 state->iov.iov_len = wr->writex.in.count;
699 subreq = tstream_writev_queue_send(state,
700 ipriv->ntvfs->ctx->event_ctx,
704 NT_STATUS_HAVE_NO_MEMORY(subreq);
705 tevent_req_set_callback(subreq, ipc_write_done, state);
707 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
711 static void ipc_write_done(struct tevent_req *subreq)
713 struct ipc_write_state *state =
714 tevent_req_callback_data(subreq,
715 struct ipc_write_state);
716 struct ntvfs_request *req = state->req;
717 union smb_write *wr = state->wr;
722 ret = tstream_writev_queue_recv(subreq, &sys_errno);
725 status = map_nt_error_from_unix(sys_errno);
729 status = NT_STATUS_OK;
731 wr->writex.out.nwritten = ret;
732 wr->writex.out.remaining = 0;
735 req->async_states->status = status;
736 req->async_states->send_fn(req);
742 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
743 struct ntvfs_request *req,
746 return NT_STATUS_ACCESS_DENIED;
752 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
753 struct ntvfs_request *req,
756 return NT_STATUS_ACCESS_DENIED;
762 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
763 struct ntvfs_request *req, union smb_close *io)
765 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
767 struct pipe_state *p;
769 if (io->generic.level != RAW_CLOSE_CLOSE) {
770 return ntvfs_map_close(ntvfs, req, io);
773 p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
775 return NT_STATUS_INVALID_HANDLE;
786 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
787 struct ntvfs_request *req)
789 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
791 struct pipe_state *p, *next;
793 for (p=ipriv->pipe_list; p; p=next) {
795 if (p->handle->session_info == req->session_info &&
796 p->handle->smbpid == req->smbpid) {
805 logoff - closing files open by the user
807 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
808 struct ntvfs_request *req)
810 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
812 struct pipe_state *p, *next;
814 for (p=ipriv->pipe_list; p; p=next) {
816 if (p->handle->session_info == req->session_info) {
825 setup for an async call
827 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
828 struct ntvfs_request *req,
837 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
838 struct ntvfs_request *req)
840 return NT_STATUS_UNSUCCESSFUL;
846 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
847 struct ntvfs_request *req, union smb_lock *lck)
849 return NT_STATUS_ACCESS_DENIED;
853 set info on a open file
855 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
856 struct ntvfs_request *req, union smb_setfileinfo *info)
858 return NT_STATUS_ACCESS_DENIED;
862 query info on a open file
864 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
865 struct ntvfs_request *req, union smb_fileinfo *info)
867 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
869 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
871 return NT_STATUS_INVALID_HANDLE;
873 switch (info->generic.level) {
874 case RAW_FILEINFO_GENERIC:
876 ZERO_STRUCT(info->generic.out);
877 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
878 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
879 info->generic.out.alloc_size = 4096;
880 info->generic.out.nlink = 1;
881 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
882 info->generic.out.delete_pending = 1;
885 case RAW_FILEINFO_ALT_NAME_INFO:
886 case RAW_FILEINFO_ALT_NAME_INFORMATION:
887 case RAW_FILEINFO_STREAM_INFO:
888 case RAW_FILEINFO_STREAM_INFORMATION:
889 case RAW_FILEINFO_COMPRESSION_INFO:
890 case RAW_FILEINFO_COMPRESSION_INFORMATION:
891 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
892 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
893 return NT_STATUS_INVALID_PARAMETER;
894 case RAW_FILEINFO_ALL_EAS:
895 return NT_STATUS_ACCESS_DENIED;
897 return ntvfs_map_qfileinfo(ntvfs, req, info);
900 return NT_STATUS_ACCESS_DENIED;
905 return filesystem info
907 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
908 struct ntvfs_request *req, union smb_fsinfo *fs)
910 return NT_STATUS_ACCESS_DENIED;
914 return print queue info
916 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
917 struct ntvfs_request *req, union smb_lpq *lpq)
919 return NT_STATUS_ACCESS_DENIED;
923 list files in a directory matching a wildcard pattern
925 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
926 struct ntvfs_request *req, union smb_search_first *io,
927 void *search_private,
928 bool (*callback)(void *, const union smb_search_data *))
930 return NT_STATUS_ACCESS_DENIED;
934 continue listing files in a directory
936 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
937 struct ntvfs_request *req, union smb_search_next *io,
938 void *search_private,
939 bool (*callback)(void *, const union smb_search_data *))
941 return NT_STATUS_ACCESS_DENIED;
945 end listing files in a directory
947 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
948 struct ntvfs_request *req, union smb_search_close *io)
950 return NT_STATUS_ACCESS_DENIED;
953 struct ipc_trans_state {
954 struct ipc_private *ipriv;
955 struct pipe_state *p;
956 struct ntvfs_request *req;
957 struct smb_trans2 *trans;
958 struct iovec writev_iov;
959 struct ipc_readv_next_vector_state next_vector;
962 static void ipc_trans_writev_done(struct tevent_req *subreq);
963 static void ipc_trans_readv_done(struct tevent_req *subreq);
965 /* SMBtrans - handle a DCERPC command */
966 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
967 struct ntvfs_request *req, struct smb_trans2 *trans)
969 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
971 struct pipe_state *p;
974 struct ipc_trans_state *state;
975 struct tevent_req *subreq;
978 * the fnum is in setup[1], a 16 bit value
979 * the setup[*] values are already in host byteorder
980 * but ntvfs_handle_search_by_wire_key() expects
983 SSVAL(&fnum, 0, trans->in.setup[1]);
984 fnum_key = data_blob_const(&fnum, 2);
986 p = pipe_state_find_key(ipriv, req, &fnum_key);
988 return NT_STATUS_INVALID_HANDLE;
992 * Trans requests are only allowed
993 * if no other Trans or Read is active
995 if (tevent_queue_length(p->read_queue) > 0) {
996 return NT_STATUS_PIPE_BUSY;
999 state = talloc(req, struct ipc_trans_state);
1000 NT_STATUS_HAVE_NO_MEMORY(state);
1002 trans->out.setup_count = 0;
1003 trans->out.setup = NULL;
1004 trans->out.params = data_blob(NULL, 0);
1005 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1006 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1008 state->ipriv = ipriv;
1011 state->trans = trans;
1012 state->writev_iov.iov_base = trans->in.data.data;
1013 state->writev_iov.iov_len = trans->in.data.length;
1015 ipc_readv_next_vector_init(&state->next_vector,
1016 trans->out.data.data,
1017 trans->out.data.length);
1019 subreq = tstream_writev_queue_send(state,
1020 ipriv->ntvfs->ctx->event_ctx,
1023 &state->writev_iov, 1);
1024 NT_STATUS_HAVE_NO_MEMORY(subreq);
1025 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1027 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1028 return NT_STATUS_OK;
1031 static void ipc_trans_writev_done(struct tevent_req *subreq)
1033 struct ipc_trans_state *state =
1034 tevent_req_callback_data(subreq,
1035 struct ipc_trans_state);
1036 struct ipc_private *ipriv = state->ipriv;
1037 struct pipe_state *p = state->p;
1038 struct ntvfs_request *req = state->req;
1043 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1044 TALLOC_FREE(subreq);
1046 status = NT_STATUS_PIPE_DISCONNECTED;
1048 } else if (ret == -1) {
1049 status = map_nt_error_from_unix(sys_errno);
1053 subreq = tstream_readv_pdu_queue_send(state,
1054 ipriv->ntvfs->ctx->event_ctx,
1057 ipc_readv_next_vector,
1058 &state->next_vector);
1060 status = NT_STATUS_NO_MEMORY;
1063 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1067 req->async_states->status = status;
1068 req->async_states->send_fn(req);
1071 static void ipc_trans_readv_done(struct tevent_req *subreq)
1073 struct ipc_trans_state *state =
1074 tevent_req_callback_data(subreq,
1075 struct ipc_trans_state);
1076 struct ntvfs_request *req = state->req;
1077 struct smb_trans2 *trans = state->trans;
1082 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1083 TALLOC_FREE(subreq);
1085 status = map_nt_error_from_unix(sys_errno);
1089 status = NT_STATUS_OK;
1090 if (state->next_vector.remaining > 0) {
1091 status = STATUS_BUFFER_OVERFLOW;
1094 trans->out.data.length = ret;
1097 req->async_states->status = status;
1098 req->async_states->send_fn(req);
1101 /* SMBtrans - set named pipe state */
1102 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1103 struct ntvfs_request *req, struct smb_trans2 *trans)
1105 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1106 struct ipc_private);
1107 struct pipe_state *p;
1110 /* the fnum is in setup[1] */
1111 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1113 p = pipe_state_find_key(ipriv, req, &fnum_key);
1115 return NT_STATUS_INVALID_HANDLE;
1118 if (trans->in.params.length != 2) {
1119 return NT_STATUS_INVALID_PARAMETER;
1123 * TODO: pass this to the tstream_npa logic
1125 p->device_state = SVAL(trans->in.params.data, 0);
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(NULL, 0);
1132 return NT_STATUS_OK;
1136 /* SMBtrans - used to provide access to SMB pipes */
1137 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1138 struct ntvfs_request *req, struct smb_trans2 *trans)
1142 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1143 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1145 if (trans->in.setup_count != 2) {
1146 return NT_STATUS_INVALID_PARAMETER;
1149 switch (trans->in.setup[0]) {
1150 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1151 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1153 case TRANSACT_DCERPCCMD:
1154 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1157 status = NT_STATUS_INVALID_PARAMETER;
1164 struct ipc_ioctl_state {
1165 struct ipc_private *ipriv;
1166 struct pipe_state *p;
1167 struct ntvfs_request *req;
1168 union smb_ioctl *io;
1169 struct iovec writev_iov;
1170 struct ipc_readv_next_vector_state next_vector;
1173 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1174 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1176 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1177 struct ntvfs_request *req, union smb_ioctl *io)
1179 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1180 struct ipc_private);
1181 struct pipe_state *p;
1182 struct ipc_ioctl_state *state;
1183 struct tevent_req *subreq;
1185 switch (io->smb2.in.function) {
1186 case FSCTL_NAMED_PIPE_READ_WRITE:
1190 return NT_STATUS_FS_DRIVER_REQUIRED;
1193 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1195 return NT_STATUS_INVALID_HANDLE;
1199 * Trans requests are only allowed
1200 * if no other Trans or Read is active
1202 if (tevent_queue_length(p->read_queue) > 0) {
1203 return NT_STATUS_PIPE_BUSY;
1206 state = talloc(req, struct ipc_ioctl_state);
1207 NT_STATUS_HAVE_NO_MEMORY(state);
1209 io->smb2.out._pad = 0;
1210 io->smb2.out.function = io->smb2.in.function;
1211 io->smb2.out.unknown2 = 0;
1212 io->smb2.out.unknown3 = 0;
1213 io->smb2.out.in = io->smb2.in.out;
1214 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1215 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1217 state->ipriv = ipriv;
1221 state->writev_iov.iov_base = io->smb2.in.out.data;
1222 state->writev_iov.iov_len = io->smb2.in.out.length;
1224 ipc_readv_next_vector_init(&state->next_vector,
1225 io->smb2.out.out.data,
1226 io->smb2.out.out.length);
1228 subreq = tstream_writev_queue_send(state,
1229 ipriv->ntvfs->ctx->event_ctx,
1232 &state->writev_iov, 1);
1233 NT_STATUS_HAVE_NO_MEMORY(subreq);
1234 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1236 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1237 return NT_STATUS_OK;
1240 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1242 struct ipc_ioctl_state *state =
1243 tevent_req_callback_data(subreq,
1244 struct ipc_ioctl_state);
1245 struct ipc_private *ipriv = state->ipriv;
1246 struct pipe_state *p = state->p;
1247 struct ntvfs_request *req = state->req;
1252 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1253 TALLOC_FREE(subreq);
1255 status = map_nt_error_from_unix(sys_errno);
1259 subreq = tstream_readv_pdu_queue_send(state,
1260 ipriv->ntvfs->ctx->event_ctx,
1263 ipc_readv_next_vector,
1264 &state->next_vector);
1266 status = NT_STATUS_NO_MEMORY;
1269 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1273 req->async_states->status = status;
1274 req->async_states->send_fn(req);
1277 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1279 struct ipc_ioctl_state *state =
1280 tevent_req_callback_data(subreq,
1281 struct ipc_ioctl_state);
1282 struct ntvfs_request *req = state->req;
1283 union smb_ioctl *io = state->io;
1288 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1289 TALLOC_FREE(subreq);
1291 status = map_nt_error_from_unix(sys_errno);
1295 status = NT_STATUS_OK;
1296 if (state->next_vector.remaining > 0) {
1297 status = STATUS_BUFFER_OVERFLOW;
1300 io->smb2.out.out.length = ret;
1303 req->async_states->status = status;
1304 req->async_states->send_fn(req);
1310 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1311 struct ntvfs_request *req, union smb_ioctl *io)
1313 switch (io->generic.level) {
1314 case RAW_IOCTL_SMB2:
1315 return ipc_ioctl_smb2(ntvfs, req, io);
1317 case RAW_IOCTL_SMB2_NO_HANDLE:
1318 return NT_STATUS_FS_DRIVER_REQUIRED;
1321 return NT_STATUS_ACCESS_DENIED;
1324 return NT_STATUS_ACCESS_DENIED;
1329 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1331 NTSTATUS ntvfs_ipc_init(void)
1334 struct ntvfs_ops ops;
1335 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1339 /* fill in the name and type */
1340 ops.name = "default";
1341 ops.type = NTVFS_IPC;
1343 /* fill in all the operations */
1344 ops.connect = ipc_connect;
1345 ops.disconnect = ipc_disconnect;
1346 ops.unlink = ipc_unlink;
1347 ops.chkpath = ipc_chkpath;
1348 ops.qpathinfo = ipc_qpathinfo;
1349 ops.setpathinfo = ipc_setpathinfo;
1350 ops.open = ipc_open;
1351 ops.mkdir = ipc_mkdir;
1352 ops.rmdir = ipc_rmdir;
1353 ops.rename = ipc_rename;
1354 ops.copy = ipc_copy;
1355 ops.ioctl = ipc_ioctl;
1356 ops.read = ipc_read;
1357 ops.write = ipc_write;
1358 ops.seek = ipc_seek;
1359 ops.flush = ipc_flush;
1360 ops.close = ipc_close;
1361 ops.exit = ipc_exit;
1362 ops.lock = ipc_lock;
1363 ops.setfileinfo = ipc_setfileinfo;
1364 ops.qfileinfo = ipc_qfileinfo;
1365 ops.fsinfo = ipc_fsinfo;
1367 ops.search_first = ipc_search_first;
1368 ops.search_next = ipc_search_next;
1369 ops.search_close = ipc_search_close;
1370 ops.trans = ipc_trans;
1371 ops.logoff = ipc_logoff;
1372 ops.async_setup = ipc_async_setup;
1373 ops.cancel = ipc_cancel;
1375 /* register ourselves with the NTVFS subsystem. */
1376 ret = ntvfs_register(&ops, &vers);
1378 if (!NT_STATUS_IS_OK(ret)) {
1379 DEBUG(0,("Failed to register IPC backend!\n"));