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 "system/kerberos.h"
42 #include "system/gssapi.h"
43 #include "system/locale.h"
44 #include "system/filesys.h"
48 /* this is the private structure used to keep the state of an open
49 ipc$ connection. It needs to keep information about all open
52 struct ntvfs_module_context *ntvfs;
54 /* a list of open pipes */
56 struct pipe_state *next, *prev;
57 struct ipc_private *ipriv;
58 const char *pipe_name;
59 struct ntvfs_handle *handle;
60 struct tstream_context *npipe;
62 uint16_t device_state;
63 uint64_t allocation_size;
64 struct tevent_queue *write_queue;
65 struct tevent_queue *read_queue;
71 find a open pipe give a file handle
73 static struct pipe_state *pipe_state_find(struct ipc_private *ipriv, struct ntvfs_handle *handle)
78 p = ntvfs_handle_get_backend_data(handle, ipriv->ntvfs);
81 s = talloc_get_type(p, struct pipe_state);
88 find a open pipe give a wire fnum
90 static struct pipe_state *pipe_state_find_key(struct ipc_private *ipriv, struct ntvfs_request *req, const DATA_BLOB *key)
92 struct ntvfs_handle *h;
94 h = ntvfs_handle_search_by_wire_key(ipriv->ntvfs, req, key);
97 return pipe_state_find(ipriv, h);
102 connect to a share - always works
104 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
105 struct ntvfs_request *req,
106 union smb_tcon* tcon)
108 struct ipc_private *ipriv;
109 const char *sharename;
111 switch (tcon->generic.level) {
113 sharename = tcon->tcon.in.service;
116 sharename = tcon->tconx.in.path;
119 sharename = tcon->smb2.in.path;
122 return NT_STATUS_INVALID_LEVEL;
125 if (strncmp(sharename, "\\\\", 2) == 0) {
126 char *p = strchr(sharename+2, '\\');
132 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "IPC");
133 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
135 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "IPC");
136 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
138 if (tcon->generic.level == RAW_TCON_TCONX) {
139 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
140 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
143 /* prepare the private state for this connection */
144 ipriv = talloc(ntvfs, struct ipc_private);
145 NT_STATUS_HAVE_NO_MEMORY(ipriv);
147 ntvfs->private_data = ipriv;
149 ipriv->ntvfs = ntvfs;
150 ipriv->pipe_list = NULL;
156 disconnect from a share
158 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs)
166 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
167 struct ntvfs_request *req,
168 union smb_unlink *unl)
170 return NT_STATUS_ACCESS_DENIED;
174 check if a directory exists
176 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
177 struct ntvfs_request *req,
178 union smb_chkpath *cp)
180 return NT_STATUS_ACCESS_DENIED;
184 return info on a pathname
186 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
187 struct ntvfs_request *req, union smb_fileinfo *info)
189 switch (info->generic.level) {
190 case RAW_FILEINFO_GENERIC:
191 return NT_STATUS_INVALID_DEVICE_REQUEST;
192 case RAW_FILEINFO_GETATTR:
193 return NT_STATUS_ACCESS_DENIED;
195 return ntvfs_map_qpathinfo(ntvfs, req, info);
200 set info on a pathname
202 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
203 struct ntvfs_request *req, union smb_setfileinfo *st)
205 return NT_STATUS_ACCESS_DENIED;
210 destroy a open pipe structure
212 static int ipc_fd_destructor(struct pipe_state *p)
214 DLIST_REMOVE(p->ipriv->pipe_list, p);
215 ntvfs_handle_remove_backend_data(p->handle, p->ipriv->ntvfs);
219 struct ipc_open_state {
220 struct ipc_private *ipriv;
221 struct pipe_state *p;
222 struct ntvfs_request *req;
224 struct auth_session_info_transport *session_info_transport;
227 static void ipc_open_done(struct tevent_req *subreq);
230 check the pipename is valid
232 static NTSTATUS validate_pipename(const char *name)
235 if (!isalnum(*name) && *name != '_') {
236 return NT_STATUS_INVALID_PARAMETER;
244 open a file - used for MSRPC pipes
246 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
247 struct ntvfs_request *req, union smb_open *oi)
250 struct pipe_state *p;
251 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
253 struct ntvfs_handle *h;
254 struct ipc_open_state *state;
255 struct tevent_req *subreq;
257 const char *directory;
258 const struct tsocket_address *remote_client_addr;
259 const struct tsocket_address *local_server_addr;
261 switch (oi->generic.level) {
262 case RAW_OPEN_NTCREATEX:
263 case RAW_OPEN_NTTRANS_CREATE:
264 fname = oi->ntcreatex.in.fname;
265 while (fname[0] == '\\') fname++;
268 fname = oi->openx.in.fname;
269 while (fname[0] == '\\') fname++;
270 if (strncasecmp(fname, "PIPE\\", 5) != 0) {
271 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
273 while (fname[0] == '\\') fname++;
276 fname = oi->smb2.in.fname;
279 return NT_STATUS_NOT_SUPPORTED;
282 directory = talloc_asprintf(req, "%s/np",
283 lpcfg_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
284 NT_STATUS_HAVE_NO_MEMORY(directory);
286 state = talloc(req, struct ipc_open_state);
287 NT_STATUS_HAVE_NO_MEMORY(state);
289 status = ntvfs_handle_new(ntvfs, req, &h);
290 NT_STATUS_NOT_OK_RETURN(status);
292 p = talloc(h, struct pipe_state);
293 NT_STATUS_HAVE_NO_MEMORY(p);
295 /* check for valid characters in name */
296 fname = strlower_talloc(p, fname);
298 status = validate_pipename(fname);
299 NT_STATUS_NOT_OK_RETURN(status);
301 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
302 NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
307 p->write_queue = tevent_queue_create(p, "ipc_write_queue");
308 NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
310 p->read_queue = tevent_queue_create(p, "ipc_read_queue");
311 NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
313 state->ipriv = ipriv;
318 status = auth_session_info_transport_from_session(state,
320 ipriv->ntvfs->ctx->event_ctx,
321 ipriv->ntvfs->ctx->lp_ctx,
322 &state->session_info_transport);
324 NT_STATUS_NOT_OK_RETURN(status);
326 local_server_addr = ntvfs_get_local_address(ipriv->ntvfs);
327 remote_client_addr = ntvfs_get_remote_address(ipriv->ntvfs);
329 subreq = tstream_npa_connect_send(p,
330 ipriv->ntvfs->ctx->event_ctx,
337 state->session_info_transport);
338 NT_STATUS_HAVE_NO_MEMORY(subreq);
339 tevent_req_set_callback(subreq, ipc_open_done, state);
341 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
345 static void ipc_open_done(struct tevent_req *subreq)
347 struct ipc_open_state *state = tevent_req_callback_data(subreq,
348 struct ipc_open_state);
349 struct ipc_private *ipriv = state->ipriv;
350 struct pipe_state *p = state->p;
351 struct ntvfs_request *req = state->req;
352 union smb_open *oi = state->oi;
357 ret = tstream_npa_connect_recv(subreq, &sys_errno,
361 &p->allocation_size);
364 status = map_nt_error_from_unix_common(sys_errno);
368 DLIST_ADD(ipriv->pipe_list, p);
369 talloc_set_destructor(p, ipc_fd_destructor);
371 status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
372 if (!NT_STATUS_IS_OK(status)) {
376 switch (oi->generic.level) {
377 case RAW_OPEN_NTCREATEX:
378 ZERO_STRUCT(oi->ntcreatex.out);
379 oi->ntcreatex.out.file.ntvfs = p->handle;
380 oi->ntcreatex.out.oplock_level = 0;
381 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
382 oi->ntcreatex.out.create_time = 0;
383 oi->ntcreatex.out.access_time = 0;
384 oi->ntcreatex.out.write_time = 0;
385 oi->ntcreatex.out.change_time = 0;
386 oi->ntcreatex.out.attrib = FILE_ATTRIBUTE_NORMAL;
387 oi->ntcreatex.out.alloc_size = p->allocation_size;
388 oi->ntcreatex.out.size = 0;
389 oi->ntcreatex.out.file_type = p->file_type;
390 oi->ntcreatex.out.ipc_state = p->device_state;
391 oi->ntcreatex.out.is_directory = 0;
394 ZERO_STRUCT(oi->openx.out);
395 oi->openx.out.file.ntvfs = p->handle;
396 oi->openx.out.attrib = FILE_ATTRIBUTE_NORMAL;
397 oi->openx.out.write_time = 0;
398 oi->openx.out.size = 0;
399 oi->openx.out.access = 0;
400 oi->openx.out.ftype = p->file_type;
401 oi->openx.out.devstate = p->device_state;
402 oi->openx.out.action = 0;
403 oi->openx.out.unique_fid = 0;
404 oi->openx.out.access_mask = 0;
405 oi->openx.out.unknown = 0;
408 ZERO_STRUCT(oi->smb2.out);
409 oi->smb2.out.file.ntvfs = p->handle;
410 oi->smb2.out.oplock_level = oi->smb2.in.oplock_level;
411 oi->smb2.out.create_action = NTCREATEX_ACTION_EXISTED;
412 oi->smb2.out.create_time = 0;
413 oi->smb2.out.access_time = 0;
414 oi->smb2.out.write_time = 0;
415 oi->smb2.out.change_time = 0;
416 oi->smb2.out.alloc_size = p->allocation_size;
417 oi->smb2.out.size = 0;
418 oi->smb2.out.file_attr = FILE_ATTRIBUTE_NORMAL;
419 oi->smb2.out.reserved2 = 0;
426 req->async_states->status = status;
427 req->async_states->send_fn(req);
433 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
434 struct ntvfs_request *req, union smb_mkdir *md)
436 return NT_STATUS_ACCESS_DENIED;
442 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
443 struct ntvfs_request *req, struct smb_rmdir *rd)
445 return NT_STATUS_ACCESS_DENIED;
449 rename a set of files
451 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
452 struct ntvfs_request *req, union smb_rename *ren)
454 return NT_STATUS_ACCESS_DENIED;
460 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
461 struct ntvfs_request *req, struct smb_copy *cp)
463 return NT_STATUS_ACCESS_DENIED;
466 struct ipc_readv_next_vector_state {
473 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
474 uint8_t *buf, size_t len)
479 s->len = MIN(len, UINT16_MAX);
482 static int ipc_readv_next_vector(struct tstream_context *stream,
485 struct iovec **_vector,
488 struct ipc_readv_next_vector_state *state =
489 (struct ipc_readv_next_vector_state *)private_data;
490 struct iovec *vector;
494 if (state->ofs == state->len) {
500 pending = tstream_pending_bytes(stream);
505 if (pending == 0 && state->ofs != 0) {
506 /* return a short read */
513 /* we want at least one byte and recheck again */
516 size_t missing = state->len - state->ofs;
517 if (pending > missing) {
518 /* there's more available */
519 state->remaining = pending - missing;
522 /* read what we can get and recheck in the next cycle */
527 vector = talloc_array(mem_ctx, struct iovec, 1);
532 vector[0].iov_base = (char *) (state->buf + state->ofs);
533 vector[0].iov_len = wanted;
535 state->ofs += wanted;
542 struct ipc_read_state {
543 struct ipc_private *ipriv;
544 struct pipe_state *p;
545 struct ntvfs_request *req;
547 struct ipc_readv_next_vector_state next_vector;
550 static void ipc_read_done(struct tevent_req *subreq);
555 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
556 struct ntvfs_request *req, union smb_read *rd)
558 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
560 struct pipe_state *p;
561 struct ipc_read_state *state;
562 struct tevent_req *subreq;
564 if (rd->generic.level != RAW_READ_GENERIC) {
565 return ntvfs_map_read(ntvfs, req, rd);
568 p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
570 return NT_STATUS_INVALID_HANDLE;
573 state = talloc(req, struct ipc_read_state);
574 NT_STATUS_HAVE_NO_MEMORY(state);
576 state->ipriv = ipriv;
581 /* rd->readx.out.data is already allocated */
582 ipc_readv_next_vector_init(&state->next_vector,
584 rd->readx.in.maxcnt);
586 subreq = tstream_readv_pdu_queue_send(req,
587 ipriv->ntvfs->ctx->event_ctx,
590 ipc_readv_next_vector,
591 &state->next_vector);
592 NT_STATUS_HAVE_NO_MEMORY(subreq);
593 tevent_req_set_callback(subreq, ipc_read_done, state);
595 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
599 static void ipc_read_done(struct tevent_req *subreq)
601 struct ipc_read_state *state =
602 tevent_req_callback_data(subreq,
603 struct ipc_read_state);
604 struct ntvfs_request *req = state->req;
605 union smb_read *rd = state->rd;
610 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
613 status = map_nt_error_from_unix_common(sys_errno);
617 status = NT_STATUS_OK;
618 if (state->next_vector.remaining > 0) {
619 status = STATUS_BUFFER_OVERFLOW;
622 rd->readx.out.remaining = state->next_vector.remaining;
623 rd->readx.out.compaction_mode = 0;
624 rd->readx.out.nread = ret;
627 req->async_states->status = status;
628 req->async_states->send_fn(req);
631 struct ipc_write_state {
632 struct ipc_private *ipriv;
633 struct pipe_state *p;
634 struct ntvfs_request *req;
639 static void ipc_write_done(struct tevent_req *subreq);
644 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
645 struct ntvfs_request *req, union smb_write *wr)
647 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
649 struct pipe_state *p;
650 struct tevent_req *subreq;
651 struct ipc_write_state *state;
653 if (wr->generic.level != RAW_WRITE_GENERIC) {
654 return ntvfs_map_write(ntvfs, req, wr);
657 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
659 return NT_STATUS_INVALID_HANDLE;
662 state = talloc(req, struct ipc_write_state);
663 NT_STATUS_HAVE_NO_MEMORY(state);
665 state->ipriv = ipriv;
669 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
670 state->iov.iov_len = wr->writex.in.count;
672 subreq = tstream_writev_queue_send(state,
673 ipriv->ntvfs->ctx->event_ctx,
677 NT_STATUS_HAVE_NO_MEMORY(subreq);
678 tevent_req_set_callback(subreq, ipc_write_done, state);
680 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
684 static void ipc_write_done(struct tevent_req *subreq)
686 struct ipc_write_state *state =
687 tevent_req_callback_data(subreq,
688 struct ipc_write_state);
689 struct ntvfs_request *req = state->req;
690 union smb_write *wr = state->wr;
695 ret = tstream_writev_queue_recv(subreq, &sys_errno);
698 status = map_nt_error_from_unix_common(sys_errno);
702 status = NT_STATUS_OK;
704 wr->writex.out.nwritten = ret;
705 wr->writex.out.remaining = 0;
708 req->async_states->status = status;
709 req->async_states->send_fn(req);
715 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
716 struct ntvfs_request *req,
719 return NT_STATUS_ACCESS_DENIED;
725 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
726 struct ntvfs_request *req,
729 return NT_STATUS_ACCESS_DENIED;
735 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
736 struct ntvfs_request *req, union smb_close *io)
738 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
740 struct pipe_state *p;
742 if (io->generic.level != RAW_CLOSE_GENERIC) {
743 return ntvfs_map_close(ntvfs, req, io);
746 ZERO_STRUCT(io->generic.out);
748 p = pipe_state_find(ipriv, io->generic.in.file.ntvfs);
750 return NT_STATUS_INVALID_HANDLE;
761 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
762 struct ntvfs_request *req)
764 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
766 struct pipe_state *p, *next;
768 for (p=ipriv->pipe_list; p; p=next) {
770 if (p->handle->session_info == req->session_info &&
771 p->handle->smbpid == req->smbpid) {
780 logoff - closing files open by the user
782 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
783 struct ntvfs_request *req)
785 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
787 struct pipe_state *p, *next;
789 for (p=ipriv->pipe_list; p; p=next) {
791 if (p->handle->session_info == req->session_info) {
800 setup for an async call
802 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
803 struct ntvfs_request *req,
812 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
813 struct ntvfs_request *req)
815 return NT_STATUS_UNSUCCESSFUL;
821 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
822 struct ntvfs_request *req, union smb_lock *lck)
824 return NT_STATUS_ACCESS_DENIED;
828 set info on a open file
830 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
831 struct ntvfs_request *req, union smb_setfileinfo *info)
833 return NT_STATUS_ACCESS_DENIED;
837 query info on a open file
839 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
840 struct ntvfs_request *req, union smb_fileinfo *info)
842 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
844 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
846 return NT_STATUS_INVALID_HANDLE;
848 switch (info->generic.level) {
849 case RAW_FILEINFO_GENERIC:
851 ZERO_STRUCT(info->generic.out);
852 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
853 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
854 info->generic.out.alloc_size = 4096;
855 info->generic.out.nlink = 1;
856 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
857 info->generic.out.delete_pending = 1;
860 case RAW_FILEINFO_ALT_NAME_INFO:
861 case RAW_FILEINFO_ALT_NAME_INFORMATION:
862 case RAW_FILEINFO_STREAM_INFO:
863 case RAW_FILEINFO_STREAM_INFORMATION:
864 case RAW_FILEINFO_COMPRESSION_INFO:
865 case RAW_FILEINFO_COMPRESSION_INFORMATION:
866 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
867 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
868 return NT_STATUS_INVALID_PARAMETER;
869 case RAW_FILEINFO_ALL_EAS:
870 return NT_STATUS_ACCESS_DENIED;
872 return ntvfs_map_qfileinfo(ntvfs, req, info);
878 return filesystem info
880 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
881 struct ntvfs_request *req, union smb_fsinfo *fs)
883 return NT_STATUS_ACCESS_DENIED;
887 return print queue info
889 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
890 struct ntvfs_request *req, union smb_lpq *lpq)
892 return NT_STATUS_ACCESS_DENIED;
896 list files in a directory matching a wildcard pattern
898 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
899 struct ntvfs_request *req, union smb_search_first *io,
900 void *search_private,
901 bool (*callback)(void *, const union smb_search_data *))
903 return NT_STATUS_ACCESS_DENIED;
907 continue listing files in a directory
909 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
910 struct ntvfs_request *req, union smb_search_next *io,
911 void *search_private,
912 bool (*callback)(void *, const union smb_search_data *))
914 return NT_STATUS_ACCESS_DENIED;
918 end listing files in a directory
920 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
921 struct ntvfs_request *req, union smb_search_close *io)
923 return NT_STATUS_ACCESS_DENIED;
926 struct ipc_trans_state {
927 struct ipc_private *ipriv;
928 struct pipe_state *p;
929 struct ntvfs_request *req;
930 struct smb_trans2 *trans;
931 struct iovec writev_iov;
932 struct ipc_readv_next_vector_state next_vector;
935 static void ipc_trans_writev_done(struct tevent_req *subreq);
936 static void ipc_trans_readv_done(struct tevent_req *subreq);
938 /* SMBtrans - handle a DCERPC command */
939 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
940 struct ntvfs_request *req, struct smb_trans2 *trans)
942 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
944 struct pipe_state *p;
947 struct ipc_trans_state *state;
948 struct tevent_req *subreq;
951 * the fnum is in setup[1], a 16 bit value
952 * the setup[*] values are already in host byteorder
953 * but ntvfs_handle_search_by_wire_key() expects
956 SSVAL(&fnum, 0, trans->in.setup[1]);
957 fnum_key = data_blob_const(&fnum, 2);
959 p = pipe_state_find_key(ipriv, req, &fnum_key);
961 return NT_STATUS_INVALID_HANDLE;
965 * Trans requests are only allowed
966 * if no other Trans or Read is active
968 if (tevent_queue_length(p->read_queue) > 0) {
969 return NT_STATUS_PIPE_BUSY;
972 state = talloc(req, struct ipc_trans_state);
973 NT_STATUS_HAVE_NO_MEMORY(state);
975 trans->out.setup_count = 0;
976 trans->out.setup = NULL;
977 trans->out.params = data_blob(NULL, 0);
978 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
979 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
981 state->ipriv = ipriv;
984 state->trans = trans;
985 state->writev_iov.iov_base = (char *) trans->in.data.data;
986 state->writev_iov.iov_len = trans->in.data.length;
988 ipc_readv_next_vector_init(&state->next_vector,
989 trans->out.data.data,
990 trans->out.data.length);
992 subreq = tstream_writev_queue_send(state,
993 ipriv->ntvfs->ctx->event_ctx,
996 &state->writev_iov, 1);
997 NT_STATUS_HAVE_NO_MEMORY(subreq);
998 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1000 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1001 return NT_STATUS_OK;
1004 static void ipc_trans_writev_done(struct tevent_req *subreq)
1006 struct ipc_trans_state *state =
1007 tevent_req_callback_data(subreq,
1008 struct ipc_trans_state);
1009 struct ipc_private *ipriv = state->ipriv;
1010 struct pipe_state *p = state->p;
1011 struct ntvfs_request *req = state->req;
1016 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1017 TALLOC_FREE(subreq);
1019 status = NT_STATUS_PIPE_DISCONNECTED;
1021 } else if (ret == -1) {
1022 status = map_nt_error_from_unix_common(sys_errno);
1026 subreq = tstream_readv_pdu_queue_send(state,
1027 ipriv->ntvfs->ctx->event_ctx,
1030 ipc_readv_next_vector,
1031 &state->next_vector);
1033 status = NT_STATUS_NO_MEMORY;
1036 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1040 req->async_states->status = status;
1041 req->async_states->send_fn(req);
1044 static void ipc_trans_readv_done(struct tevent_req *subreq)
1046 struct ipc_trans_state *state =
1047 tevent_req_callback_data(subreq,
1048 struct ipc_trans_state);
1049 struct ntvfs_request *req = state->req;
1050 struct smb_trans2 *trans = state->trans;
1055 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1056 TALLOC_FREE(subreq);
1058 status = map_nt_error_from_unix_common(sys_errno);
1062 status = NT_STATUS_OK;
1063 if (state->next_vector.remaining > 0) {
1064 status = STATUS_BUFFER_OVERFLOW;
1067 trans->out.data.length = ret;
1070 req->async_states->status = status;
1071 req->async_states->send_fn(req);
1074 /* SMBtrans - set named pipe state */
1075 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1076 struct ntvfs_request *req, struct smb_trans2 *trans)
1078 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1079 struct ipc_private);
1080 struct pipe_state *p;
1083 /* the fnum is in setup[1] */
1084 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1086 p = pipe_state_find_key(ipriv, req, &fnum_key);
1088 return NT_STATUS_INVALID_HANDLE;
1091 if (trans->in.params.length != 2) {
1092 return NT_STATUS_INVALID_PARAMETER;
1096 * TODO: pass this to the tstream_npa logic
1098 p->device_state = SVAL(trans->in.params.data, 0);
1100 trans->out.setup_count = 0;
1101 trans->out.setup = NULL;
1102 trans->out.params = data_blob(NULL, 0);
1103 trans->out.data = data_blob(NULL, 0);
1105 return NT_STATUS_OK;
1109 /* SMBtrans - used to provide access to SMB pipes */
1110 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1111 struct ntvfs_request *req, struct smb_trans2 *trans)
1115 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1116 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1118 if (trans->in.setup_count != 2) {
1119 return NT_STATUS_INVALID_PARAMETER;
1122 switch (trans->in.setup[0]) {
1123 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1124 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1126 case TRANSACT_DCERPCCMD:
1127 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1130 status = NT_STATUS_INVALID_PARAMETER;
1137 struct ipc_ioctl_state {
1138 struct ipc_private *ipriv;
1139 struct pipe_state *p;
1140 struct ntvfs_request *req;
1141 union smb_ioctl *io;
1142 struct iovec writev_iov;
1143 struct ipc_readv_next_vector_state next_vector;
1146 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1147 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1149 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1150 struct ntvfs_request *req, union smb_ioctl *io)
1152 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1153 struct ipc_private);
1154 struct pipe_state *p;
1155 struct ipc_ioctl_state *state;
1156 struct tevent_req *subreq;
1158 switch (io->smb2.in.function) {
1159 case FSCTL_NAMED_PIPE_READ_WRITE:
1163 return NT_STATUS_FS_DRIVER_REQUIRED;
1166 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1168 return NT_STATUS_INVALID_HANDLE;
1172 * Trans requests are only allowed
1173 * if no other Trans or Read is active
1175 if (tevent_queue_length(p->read_queue) > 0) {
1176 return NT_STATUS_PIPE_BUSY;
1179 state = talloc(req, struct ipc_ioctl_state);
1180 NT_STATUS_HAVE_NO_MEMORY(state);
1182 io->smb2.out.reserved = 0;
1183 io->smb2.out.function = io->smb2.in.function;
1184 io->smb2.out.flags = 0;
1185 io->smb2.out.reserved2 = 0;
1186 io->smb2.out.in = data_blob_null;
1187 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_output_response);
1188 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1190 state->ipriv = ipriv;
1194 state->writev_iov.iov_base = (char *) io->smb2.in.out.data;
1195 state->writev_iov.iov_len = io->smb2.in.out.length;
1197 ipc_readv_next_vector_init(&state->next_vector,
1198 io->smb2.out.out.data,
1199 io->smb2.out.out.length);
1201 subreq = tstream_writev_queue_send(state,
1202 ipriv->ntvfs->ctx->event_ctx,
1205 &state->writev_iov, 1);
1206 NT_STATUS_HAVE_NO_MEMORY(subreq);
1207 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1209 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1210 return NT_STATUS_OK;
1213 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1215 struct ipc_ioctl_state *state =
1216 tevent_req_callback_data(subreq,
1217 struct ipc_ioctl_state);
1218 struct ipc_private *ipriv = state->ipriv;
1219 struct pipe_state *p = state->p;
1220 struct ntvfs_request *req = state->req;
1225 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1226 TALLOC_FREE(subreq);
1228 status = map_nt_error_from_unix_common(sys_errno);
1232 subreq = tstream_readv_pdu_queue_send(state,
1233 ipriv->ntvfs->ctx->event_ctx,
1236 ipc_readv_next_vector,
1237 &state->next_vector);
1239 status = NT_STATUS_NO_MEMORY;
1242 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1246 req->async_states->status = status;
1247 req->async_states->send_fn(req);
1250 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1252 struct ipc_ioctl_state *state =
1253 tevent_req_callback_data(subreq,
1254 struct ipc_ioctl_state);
1255 struct ntvfs_request *req = state->req;
1256 union smb_ioctl *io = state->io;
1261 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1262 TALLOC_FREE(subreq);
1264 status = map_nt_error_from_unix_common(sys_errno);
1268 status = NT_STATUS_OK;
1269 if (state->next_vector.remaining > 0) {
1270 status = STATUS_BUFFER_OVERFLOW;
1273 io->smb2.out.out.length = ret;
1276 req->async_states->status = status;
1277 req->async_states->send_fn(req);
1283 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1284 struct ntvfs_request *req, union smb_ioctl *io)
1286 switch (io->generic.level) {
1287 case RAW_IOCTL_SMB2:
1288 return ipc_ioctl_smb2(ntvfs, req, io);
1290 case RAW_IOCTL_SMB2_NO_HANDLE:
1291 return NT_STATUS_FS_DRIVER_REQUIRED;
1294 return NT_STATUS_ACCESS_DENIED;
1300 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1302 NTSTATUS ntvfs_ipc_init(TALLOC_CTX *ctx)
1305 struct ntvfs_ops ops;
1306 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1310 /* fill in the name and type */
1311 ops.name = "default";
1312 ops.type = NTVFS_IPC;
1314 /* fill in all the operations */
1315 ops.connect_fn = ipc_connect;
1316 ops.disconnect_fn = ipc_disconnect;
1317 ops.unlink_fn = ipc_unlink;
1318 ops.chkpath_fn = ipc_chkpath;
1319 ops.qpathinfo_fn = ipc_qpathinfo;
1320 ops.setpathinfo_fn = ipc_setpathinfo;
1321 ops.open_fn = ipc_open;
1322 ops.mkdir_fn = ipc_mkdir;
1323 ops.rmdir_fn = ipc_rmdir;
1324 ops.rename_fn = ipc_rename;
1325 ops.copy_fn = ipc_copy;
1326 ops.ioctl_fn = ipc_ioctl;
1327 ops.read_fn = ipc_read;
1328 ops.write_fn = ipc_write;
1329 ops.seek_fn = ipc_seek;
1330 ops.flush_fn = ipc_flush;
1331 ops.close_fn = ipc_close;
1332 ops.exit_fn = ipc_exit;
1333 ops.lock_fn = ipc_lock;
1334 ops.setfileinfo_fn = ipc_setfileinfo;
1335 ops.qfileinfo_fn = ipc_qfileinfo;
1336 ops.fsinfo_fn = ipc_fsinfo;
1337 ops.lpq_fn = ipc_lpq;
1338 ops.search_first_fn = ipc_search_first;
1339 ops.search_next_fn = ipc_search_next;
1340 ops.search_close_fn = ipc_search_close;
1341 ops.trans_fn = ipc_trans;
1342 ops.logoff_fn = ipc_logoff;
1343 ops.async_setup_fn = ipc_async_setup;
1344 ops.cancel_fn = ipc_cancel;
1346 /* register ourselves with the NTVFS subsystem. */
1347 ret = ntvfs_register(&ops, &vers);
1349 if (!NT_STATUS_IS_OK(ret)) {
1350 DEBUG(0,("Failed to register IPC backend!\n"));