2 Unix SMB/CIFS implementation.
3 default IPC$ NTVFS backend
5 Copyright (C) Andrew Tridgell 2003
6 Copyright (C) Stefan (metze) Metzmacher 2004-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 this implements the IPC$ backend, called by the NTVFS subsystem to
23 handle requests on IPC$ shares
28 #include "../lib/util/dlinklist.h"
29 #include "ntvfs/ntvfs.h"
30 #include "libcli/rap/rap.h"
31 #include "ntvfs/ipc/proto.h"
32 #include "libcli/raw/ioctl.h"
33 #include "param/param.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../libcli/named_pipe_auth/npa_tstream.h"
36 #include "auth/auth.h"
37 #include "auth/auth_sam_reply.h"
38 #include "lib/socket/socket.h"
39 #include "auth/credentials/credentials.h"
40 #include "auth/credentials/credentials_krb5.h"
41 #include <gssapi/gssapi.h>
43 /* this is the private structure used to keep the state of an open
44 ipc$ connection. It needs to keep information about all open
47 struct ntvfs_module_context *ntvfs;
49 /* a list of open pipes */
51 struct pipe_state *next, *prev;
52 struct ipc_private *ipriv;
53 const char *pipe_name;
54 struct ntvfs_handle *handle;
55 struct tstream_context *npipe;
57 uint16_t device_state;
58 uint64_t allocation_size;
59 struct tevent_queue *write_queue;
60 struct tevent_queue *read_queue;
66 find a open pipe give a file handle
68 static struct pipe_state *pipe_state_find(struct ipc_private *ipriv, struct ntvfs_handle *handle)
73 p = ntvfs_handle_get_backend_data(handle, ipriv->ntvfs);
76 s = talloc_get_type(p, struct pipe_state);
83 find a open pipe give a wire fnum
85 static struct pipe_state *pipe_state_find_key(struct ipc_private *ipriv, struct ntvfs_request *req, const DATA_BLOB *key)
87 struct ntvfs_handle *h;
89 h = ntvfs_handle_search_by_wire_key(ipriv->ntvfs, req, key);
92 return pipe_state_find(ipriv, h);
97 connect to a share - always works
99 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
100 struct ntvfs_request *req,
101 union smb_tcon* tcon)
103 struct ipc_private *ipriv;
104 const char *sharename;
106 switch (tcon->generic.level) {
108 sharename = tcon->tcon.in.service;
111 sharename = tcon->tconx.in.path;
114 sharename = tcon->smb2.in.path;
117 return NT_STATUS_INVALID_LEVEL;
120 if (strncmp(sharename, "\\\\", 2) == 0) {
121 char *p = strchr(sharename+2, '\\');
127 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "IPC");
128 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
130 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "IPC");
131 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
133 if (tcon->generic.level == RAW_TCON_TCONX) {
134 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
135 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
138 /* prepare the private state for this connection */
139 ipriv = talloc(ntvfs, struct ipc_private);
140 NT_STATUS_HAVE_NO_MEMORY(ipriv);
142 ntvfs->private_data = ipriv;
144 ipriv->ntvfs = ntvfs;
145 ipriv->pipe_list = NULL;
151 disconnect from a share
153 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs)
161 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
162 struct ntvfs_request *req,
163 union smb_unlink *unl)
165 return NT_STATUS_ACCESS_DENIED;
169 check if a directory exists
171 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
172 struct ntvfs_request *req,
173 union smb_chkpath *cp)
175 return NT_STATUS_ACCESS_DENIED;
179 return info on a pathname
181 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
182 struct ntvfs_request *req, union smb_fileinfo *info)
184 switch (info->generic.level) {
185 case RAW_FILEINFO_GENERIC:
186 return NT_STATUS_INVALID_DEVICE_REQUEST;
187 case RAW_FILEINFO_GETATTR:
188 return NT_STATUS_ACCESS_DENIED;
190 return ntvfs_map_qpathinfo(ntvfs, req, info);
195 set info on a pathname
197 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
198 struct ntvfs_request *req, union smb_setfileinfo *st)
200 return NT_STATUS_ACCESS_DENIED;
205 destroy a open pipe structure
207 static int ipc_fd_destructor(struct pipe_state *p)
209 DLIST_REMOVE(p->ipriv->pipe_list, p);
210 ntvfs_handle_remove_backend_data(p->handle, p->ipriv->ntvfs);
214 struct ipc_open_state {
215 struct ipc_private *ipriv;
216 struct pipe_state *p;
217 struct ntvfs_request *req;
219 struct netr_SamInfo3 *info3;
222 static void ipc_open_done(struct tevent_req *subreq);
225 open a file - used for MSRPC pipes
227 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
228 struct ntvfs_request *req, union smb_open *oi)
231 struct pipe_state *p;
232 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
234 struct smb_iconv_convenience *smb_ic
235 = lp_iconv_convenience(ipriv->ntvfs->ctx->lp_ctx);
236 struct ntvfs_handle *h;
237 struct ipc_open_state *state;
238 struct tevent_req *subreq;
240 const char *directory;
241 struct socket_address *client_sa;
242 struct tsocket_address *client_addr;
243 struct socket_address *server_sa;
244 struct tsocket_address *server_addr;
246 DATA_BLOB delegated_creds = data_blob_null;
248 switch (oi->generic.level) {
249 case RAW_OPEN_NTCREATEX:
250 fname = oi->ntcreatex.in.fname;
253 fname = oi->openx.in.fname;
256 fname = oi->smb2.in.fname;
259 status = NT_STATUS_NOT_SUPPORTED;
263 directory = talloc_asprintf(req, "%s/np",
264 lp_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
265 NT_STATUS_HAVE_NO_MEMORY(directory);
267 state = talloc(req, struct ipc_open_state);
268 NT_STATUS_HAVE_NO_MEMORY(state);
270 status = ntvfs_handle_new(ntvfs, req, &h);
271 NT_STATUS_NOT_OK_RETURN(status);
273 p = talloc(h, struct pipe_state);
274 NT_STATUS_HAVE_NO_MEMORY(p);
276 while (fname[0] == '\\') fname++;
278 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
279 NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
284 p->write_queue = tevent_queue_create(p, "ipc_write_queue");
285 NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
287 p->read_queue = tevent_queue_create(p, "ipc_read_queue");
288 NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
290 state->ipriv = ipriv;
295 status = auth_convert_server_info_saminfo3(state,
296 req->session_info->server_info,
298 NT_STATUS_NOT_OK_RETURN(status);
300 client_sa = ntvfs_get_peer_addr(ntvfs, state);
302 return NT_STATUS_INTERNAL_ERROR;
305 server_sa = ntvfs_get_my_addr(ntvfs, state);
307 return NT_STATUS_INTERNAL_ERROR;
310 ret = tsocket_address_inet_from_strings(state, "ip",
315 status = map_nt_error_from_unix(errno);
319 ret = tsocket_address_inet_from_strings(state, "ip",
324 status = map_nt_error_from_unix(errno);
328 if (req->session_info->credentials) {
329 struct gssapi_creds_container *gcc;
331 OM_uint32 minor_status;
332 gss_buffer_desc cred_token;
334 ret = cli_credentials_get_client_gss_creds(req->session_info->credentials,
335 ipriv->ntvfs->ctx->event_ctx,
336 ipriv->ntvfs->ctx->lp_ctx,
342 gret = gss_export_cred(&minor_status,
345 if (gret != GSS_S_COMPLETE) {
346 return NT_STATUS_INTERNAL_ERROR;
349 if (cred_token.length) {
350 delegated_creds = data_blob_talloc(req,
353 gss_release_buffer(&minor_status, &cred_token);
354 NT_STATUS_HAVE_NO_MEMORY(delegated_creds.data);
360 subreq = tstream_npa_connect_send(p,
361 ipriv->ntvfs->ctx->event_ctx,
370 req->session_info->session_key,
372 NT_STATUS_HAVE_NO_MEMORY(subreq);
373 tevent_req_set_callback(subreq, ipc_open_done, state);
375 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
379 static void ipc_open_done(struct tevent_req *subreq)
381 struct ipc_open_state *state = tevent_req_callback_data(subreq,
382 struct ipc_open_state);
383 struct ipc_private *ipriv = state->ipriv;
384 struct pipe_state *p = state->p;
385 struct ntvfs_request *req = state->req;
386 union smb_open *oi = state->oi;
391 ret = tstream_npa_connect_recv(subreq, &sys_errno,
395 &p->allocation_size);
398 status = map_nt_error_from_unix(sys_errno);
402 DLIST_ADD(ipriv->pipe_list, p);
403 talloc_set_destructor(p, ipc_fd_destructor);
405 status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
406 if (!NT_STATUS_IS_OK(status)) {
410 switch (oi->generic.level) {
411 case RAW_OPEN_NTCREATEX:
412 ZERO_STRUCT(oi->ntcreatex.out);
413 oi->ntcreatex.out.file.ntvfs = p->handle;
414 oi->ntcreatex.out.oplock_level = 0;
415 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
416 oi->ntcreatex.out.create_time = 0;
417 oi->ntcreatex.out.access_time = 0;
418 oi->ntcreatex.out.write_time = 0;
419 oi->ntcreatex.out.change_time = 0;
420 oi->ntcreatex.out.attrib = FILE_ATTRIBUTE_NORMAL;
421 oi->ntcreatex.out.alloc_size = p->allocation_size;
422 oi->ntcreatex.out.size = 0;
423 oi->ntcreatex.out.file_type = p->file_type;
424 oi->ntcreatex.out.ipc_state = p->device_state;
425 oi->ntcreatex.out.is_directory = 0;
428 ZERO_STRUCT(oi->openx.out);
429 oi->openx.out.file.ntvfs = p->handle;
430 oi->openx.out.attrib = FILE_ATTRIBUTE_NORMAL;
431 oi->openx.out.write_time = 0;
432 oi->openx.out.size = 0;
433 oi->openx.out.access = 0;
434 oi->openx.out.ftype = p->file_type;
435 oi->openx.out.devstate = p->device_state;
436 oi->openx.out.action = 0;
437 oi->openx.out.unique_fid = 0;
438 oi->openx.out.access_mask = 0;
439 oi->openx.out.unknown = 0;
442 ZERO_STRUCT(oi->smb2.out);
443 oi->smb2.out.file.ntvfs = p->handle;
444 oi->smb2.out.oplock_level = oi->smb2.in.oplock_level;
445 oi->smb2.out.create_action = NTCREATEX_ACTION_EXISTED;
446 oi->smb2.out.create_time = 0;
447 oi->smb2.out.access_time = 0;
448 oi->smb2.out.write_time = 0;
449 oi->smb2.out.change_time = 0;
450 oi->smb2.out.alloc_size = p->allocation_size;
451 oi->smb2.out.size = 0;
452 oi->smb2.out.file_attr = FILE_ATTRIBUTE_NORMAL;
453 oi->smb2.out.reserved2 = 0;
460 req->async_states->status = status;
461 req->async_states->send_fn(req);
467 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
468 struct ntvfs_request *req, union smb_mkdir *md)
470 return NT_STATUS_ACCESS_DENIED;
476 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
477 struct ntvfs_request *req, struct smb_rmdir *rd)
479 return NT_STATUS_ACCESS_DENIED;
483 rename a set of files
485 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
486 struct ntvfs_request *req, union smb_rename *ren)
488 return NT_STATUS_ACCESS_DENIED;
494 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
495 struct ntvfs_request *req, struct smb_copy *cp)
497 return NT_STATUS_ACCESS_DENIED;
500 struct ipc_readv_next_vector_state {
507 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
508 uint8_t *buf, size_t len)
513 s->len = MIN(len, UINT16_MAX);
514 //DEBUG(0,("readv_next_vector_init[%u 0x%04X]\n", s->len, s->len));
517 static int ipc_readv_next_vector(struct tstream_context *stream,
520 struct iovec **_vector,
523 struct ipc_readv_next_vector_state *state =
524 (struct ipc_readv_next_vector_state *)private_data;
525 struct iovec *vector;
529 if (state->ofs == state->len) {
532 // DEBUG(0,("readv_next_vector done ofs[%u 0x%04X]\n",
533 // state->ofs, state->ofs));
537 pending = tstream_pending_bytes(stream);
542 if (pending == 0 && state->ofs != 0) {
543 /* return a short read */
546 // DEBUG(0,("readv_next_vector short read ofs[%u 0x%04X]\n",
547 // state->ofs, state->ofs));
552 /* we want at least one byte and recheck again */
555 size_t missing = state->len - state->ofs;
556 if (pending > missing) {
557 /* there's more available */
558 state->remaining = pending - missing;
561 /* read what we can get and recheck in the next cycle */
566 vector = talloc_array(mem_ctx, struct iovec, 1);
571 vector[0].iov_base = state->buf + state->ofs;
572 vector[0].iov_len = wanted;
574 state->ofs += wanted;
581 struct ipc_read_state {
582 struct ipc_private *ipriv;
583 struct pipe_state *p;
584 struct ntvfs_request *req;
586 struct ipc_readv_next_vector_state next_vector;
589 static void ipc_read_done(struct tevent_req *subreq);
594 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
595 struct ntvfs_request *req, union smb_read *rd)
597 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
599 struct pipe_state *p;
600 struct ipc_read_state *state;
601 struct tevent_req *subreq;
603 if (rd->generic.level != RAW_READ_GENERIC) {
604 return ntvfs_map_read(ntvfs, req, rd);
607 p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
609 return NT_STATUS_INVALID_HANDLE;
612 state = talloc(req, struct ipc_read_state);
613 NT_STATUS_HAVE_NO_MEMORY(state);
615 state->ipriv = ipriv;
620 /* rd->readx.out.data is already allocated */
621 ipc_readv_next_vector_init(&state->next_vector,
623 rd->readx.in.maxcnt);
625 subreq = tstream_readv_pdu_queue_send(req,
626 ipriv->ntvfs->ctx->event_ctx,
629 ipc_readv_next_vector,
630 &state->next_vector);
631 NT_STATUS_HAVE_NO_MEMORY(subreq);
632 tevent_req_set_callback(subreq, ipc_read_done, state);
634 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
638 static void ipc_read_done(struct tevent_req *subreq)
640 struct ipc_read_state *state =
641 tevent_req_callback_data(subreq,
642 struct ipc_read_state);
643 struct ntvfs_request *req = state->req;
644 union smb_read *rd = state->rd;
649 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
652 status = map_nt_error_from_unix(sys_errno);
656 status = NT_STATUS_OK;
657 if (state->next_vector.remaining > 0) {
658 status = STATUS_BUFFER_OVERFLOW;
661 rd->readx.out.remaining = state->next_vector.remaining;
662 rd->readx.out.compaction_mode = 0;
663 rd->readx.out.nread = ret;
666 req->async_states->status = status;
667 req->async_states->send_fn(req);
670 struct ipc_write_state {
671 struct ipc_private *ipriv;
672 struct pipe_state *p;
673 struct ntvfs_request *req;
678 static void ipc_write_done(struct tevent_req *subreq);
683 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
684 struct ntvfs_request *req, union smb_write *wr)
686 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
688 struct pipe_state *p;
689 struct tevent_req *subreq;
690 struct ipc_write_state *state;
692 if (wr->generic.level != RAW_WRITE_GENERIC) {
693 return ntvfs_map_write(ntvfs, req, wr);
696 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
698 return NT_STATUS_INVALID_HANDLE;
701 state = talloc(req, struct ipc_write_state);
702 NT_STATUS_HAVE_NO_MEMORY(state);
704 state->ipriv = ipriv;
708 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
709 state->iov.iov_len = wr->writex.in.count;
711 subreq = tstream_writev_queue_send(state,
712 ipriv->ntvfs->ctx->event_ctx,
716 NT_STATUS_HAVE_NO_MEMORY(subreq);
717 tevent_req_set_callback(subreq, ipc_write_done, state);
719 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
723 static void ipc_write_done(struct tevent_req *subreq)
725 struct ipc_write_state *state =
726 tevent_req_callback_data(subreq,
727 struct ipc_write_state);
728 struct ntvfs_request *req = state->req;
729 union smb_write *wr = state->wr;
734 ret = tstream_writev_queue_recv(subreq, &sys_errno);
737 status = map_nt_error_from_unix(sys_errno);
741 status = NT_STATUS_OK;
743 wr->writex.out.nwritten = ret;
744 wr->writex.out.remaining = 0;
747 req->async_states->status = status;
748 req->async_states->send_fn(req);
754 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
755 struct ntvfs_request *req,
758 return NT_STATUS_ACCESS_DENIED;
764 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
765 struct ntvfs_request *req,
768 return NT_STATUS_ACCESS_DENIED;
774 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
775 struct ntvfs_request *req, union smb_close *io)
777 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
779 struct pipe_state *p;
781 if (io->generic.level != RAW_CLOSE_CLOSE) {
782 return ntvfs_map_close(ntvfs, req, io);
785 p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
787 return NT_STATUS_INVALID_HANDLE;
798 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
799 struct ntvfs_request *req)
801 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
803 struct pipe_state *p, *next;
805 for (p=ipriv->pipe_list; p; p=next) {
807 if (p->handle->session_info == req->session_info &&
808 p->handle->smbpid == req->smbpid) {
817 logoff - closing files open by the user
819 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
820 struct ntvfs_request *req)
822 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
824 struct pipe_state *p, *next;
826 for (p=ipriv->pipe_list; p; p=next) {
828 if (p->handle->session_info == req->session_info) {
837 setup for an async call
839 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
840 struct ntvfs_request *req,
849 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
850 struct ntvfs_request *req)
852 return NT_STATUS_UNSUCCESSFUL;
858 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
859 struct ntvfs_request *req, union smb_lock *lck)
861 return NT_STATUS_ACCESS_DENIED;
865 set info on a open file
867 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
868 struct ntvfs_request *req, union smb_setfileinfo *info)
870 return NT_STATUS_ACCESS_DENIED;
874 query info on a open file
876 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
877 struct ntvfs_request *req, union smb_fileinfo *info)
879 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
881 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
883 return NT_STATUS_INVALID_HANDLE;
885 switch (info->generic.level) {
886 case RAW_FILEINFO_GENERIC:
888 ZERO_STRUCT(info->generic.out);
889 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
890 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
891 info->generic.out.alloc_size = 4096;
892 info->generic.out.nlink = 1;
893 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
894 info->generic.out.delete_pending = 1;
897 case RAW_FILEINFO_ALT_NAME_INFO:
898 case RAW_FILEINFO_ALT_NAME_INFORMATION:
899 case RAW_FILEINFO_STREAM_INFO:
900 case RAW_FILEINFO_STREAM_INFORMATION:
901 case RAW_FILEINFO_COMPRESSION_INFO:
902 case RAW_FILEINFO_COMPRESSION_INFORMATION:
903 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
904 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
905 return NT_STATUS_INVALID_PARAMETER;
906 case RAW_FILEINFO_ALL_EAS:
907 return NT_STATUS_ACCESS_DENIED;
909 return ntvfs_map_qfileinfo(ntvfs, req, info);
912 return NT_STATUS_ACCESS_DENIED;
917 return filesystem info
919 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
920 struct ntvfs_request *req, union smb_fsinfo *fs)
922 return NT_STATUS_ACCESS_DENIED;
926 return print queue info
928 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
929 struct ntvfs_request *req, union smb_lpq *lpq)
931 return NT_STATUS_ACCESS_DENIED;
935 list files in a directory matching a wildcard pattern
937 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
938 struct ntvfs_request *req, union smb_search_first *io,
939 void *search_private,
940 bool (*callback)(void *, const union smb_search_data *))
942 return NT_STATUS_ACCESS_DENIED;
946 continue listing files in a directory
948 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
949 struct ntvfs_request *req, union smb_search_next *io,
950 void *search_private,
951 bool (*callback)(void *, const union smb_search_data *))
953 return NT_STATUS_ACCESS_DENIED;
957 end listing files in a directory
959 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
960 struct ntvfs_request *req, union smb_search_close *io)
962 return NT_STATUS_ACCESS_DENIED;
965 struct ipc_trans_state {
966 struct ipc_private *ipriv;
967 struct pipe_state *p;
968 struct ntvfs_request *req;
969 struct smb_trans2 *trans;
970 struct iovec writev_iov;
971 struct ipc_readv_next_vector_state next_vector;
974 static void ipc_trans_writev_done(struct tevent_req *subreq);
975 static void ipc_trans_readv_done(struct tevent_req *subreq);
977 /* SMBtrans - handle a DCERPC command */
978 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
979 struct ntvfs_request *req, struct smb_trans2 *trans)
981 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
983 struct pipe_state *p;
986 struct ipc_trans_state *state;
987 struct tevent_req *subreq;
990 * the fnum is in setup[1], a 16 bit value
991 * the setup[*] values are already in host byteorder
992 * but ntvfs_handle_search_by_wire_key() expects
995 SSVAL(&fnum, 0, trans->in.setup[1]);
996 fnum_key = data_blob_const(&fnum, 2);
998 p = pipe_state_find_key(ipriv, req, &fnum_key);
1000 return NT_STATUS_INVALID_HANDLE;
1004 * Trans requests are only allowed
1005 * if no other Trans or Read is active
1007 if (tevent_queue_length(p->read_queue) > 0) {
1008 return NT_STATUS_PIPE_BUSY;
1011 state = talloc(req, struct ipc_trans_state);
1012 NT_STATUS_HAVE_NO_MEMORY(state);
1014 trans->out.setup_count = 0;
1015 trans->out.setup = NULL;
1016 trans->out.params = data_blob(NULL, 0);
1017 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1018 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1020 state->ipriv = ipriv;
1023 state->trans = trans;
1024 state->writev_iov.iov_base = trans->in.data.data;
1025 state->writev_iov.iov_len = trans->in.data.length;
1027 ipc_readv_next_vector_init(&state->next_vector,
1028 trans->out.data.data,
1029 trans->out.data.length);
1031 subreq = tstream_writev_queue_send(state,
1032 ipriv->ntvfs->ctx->event_ctx,
1035 &state->writev_iov, 1);
1036 NT_STATUS_HAVE_NO_MEMORY(subreq);
1037 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1039 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1040 return NT_STATUS_OK;
1043 static void ipc_trans_writev_done(struct tevent_req *subreq)
1045 struct ipc_trans_state *state =
1046 tevent_req_callback_data(subreq,
1047 struct ipc_trans_state);
1048 struct ipc_private *ipriv = state->ipriv;
1049 struct pipe_state *p = state->p;
1050 struct ntvfs_request *req = state->req;
1055 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1056 TALLOC_FREE(subreq);
1058 status = NT_STATUS_PIPE_DISCONNECTED;
1060 } else if (ret == -1) {
1061 status = map_nt_error_from_unix(sys_errno);
1065 subreq = tstream_readv_pdu_queue_send(state,
1066 ipriv->ntvfs->ctx->event_ctx,
1069 ipc_readv_next_vector,
1070 &state->next_vector);
1072 status = NT_STATUS_NO_MEMORY;
1075 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1079 req->async_states->status = status;
1080 req->async_states->send_fn(req);
1083 static void ipc_trans_readv_done(struct tevent_req *subreq)
1085 struct ipc_trans_state *state =
1086 tevent_req_callback_data(subreq,
1087 struct ipc_trans_state);
1088 struct ntvfs_request *req = state->req;
1089 struct smb_trans2 *trans = state->trans;
1094 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1095 TALLOC_FREE(subreq);
1097 status = map_nt_error_from_unix(sys_errno);
1101 status = NT_STATUS_OK;
1102 if (state->next_vector.remaining > 0) {
1103 status = STATUS_BUFFER_OVERFLOW;
1106 trans->out.data.length = ret;
1109 req->async_states->status = status;
1110 req->async_states->send_fn(req);
1113 /* SMBtrans - set named pipe state */
1114 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1115 struct ntvfs_request *req, struct smb_trans2 *trans)
1117 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1118 struct ipc_private);
1119 struct pipe_state *p;
1122 /* the fnum is in setup[1] */
1123 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1125 p = pipe_state_find_key(ipriv, req, &fnum_key);
1127 return NT_STATUS_INVALID_HANDLE;
1130 if (trans->in.params.length != 2) {
1131 return NT_STATUS_INVALID_PARAMETER;
1135 * TODO: pass this to the tstream_npa logic
1137 p->device_state = SVAL(trans->in.params.data, 0);
1139 trans->out.setup_count = 0;
1140 trans->out.setup = NULL;
1141 trans->out.params = data_blob(NULL, 0);
1142 trans->out.data = data_blob(NULL, 0);
1144 return NT_STATUS_OK;
1148 /* SMBtrans - used to provide access to SMB pipes */
1149 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1150 struct ntvfs_request *req, struct smb_trans2 *trans)
1154 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1155 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1157 if (trans->in.setup_count != 2) {
1158 return NT_STATUS_INVALID_PARAMETER;
1161 switch (trans->in.setup[0]) {
1162 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1163 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1165 case TRANSACT_DCERPCCMD:
1166 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1169 status = NT_STATUS_INVALID_PARAMETER;
1176 struct ipc_ioctl_state {
1177 struct ipc_private *ipriv;
1178 struct pipe_state *p;
1179 struct ntvfs_request *req;
1180 union smb_ioctl *io;
1181 struct iovec writev_iov;
1182 struct ipc_readv_next_vector_state next_vector;
1185 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1186 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1188 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1189 struct ntvfs_request *req, union smb_ioctl *io)
1191 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1192 struct ipc_private);
1193 struct pipe_state *p;
1194 struct ipc_ioctl_state *state;
1195 struct tevent_req *subreq;
1197 switch (io->smb2.in.function) {
1198 case FSCTL_NAMED_PIPE_READ_WRITE:
1202 return NT_STATUS_FS_DRIVER_REQUIRED;
1205 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1207 return NT_STATUS_INVALID_HANDLE;
1211 * Trans requests are only allowed
1212 * if no other Trans or Read is active
1214 if (tevent_queue_length(p->read_queue) > 0) {
1215 return NT_STATUS_PIPE_BUSY;
1218 state = talloc(req, struct ipc_ioctl_state);
1219 NT_STATUS_HAVE_NO_MEMORY(state);
1221 io->smb2.out._pad = 0;
1222 io->smb2.out.function = io->smb2.in.function;
1223 io->smb2.out.unknown2 = 0;
1224 io->smb2.out.unknown3 = 0;
1225 io->smb2.out.in = io->smb2.in.out;
1226 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1227 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1229 state->ipriv = ipriv;
1233 state->writev_iov.iov_base = io->smb2.in.out.data;
1234 state->writev_iov.iov_len = io->smb2.in.out.length;
1236 ipc_readv_next_vector_init(&state->next_vector,
1237 io->smb2.out.out.data,
1238 io->smb2.out.out.length);
1240 subreq = tstream_writev_queue_send(state,
1241 ipriv->ntvfs->ctx->event_ctx,
1244 &state->writev_iov, 1);
1245 NT_STATUS_HAVE_NO_MEMORY(subreq);
1246 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1248 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1249 return NT_STATUS_OK;
1252 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1254 struct ipc_ioctl_state *state =
1255 tevent_req_callback_data(subreq,
1256 struct ipc_ioctl_state);
1257 struct ipc_private *ipriv = state->ipriv;
1258 struct pipe_state *p = state->p;
1259 struct ntvfs_request *req = state->req;
1264 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1265 TALLOC_FREE(subreq);
1267 status = map_nt_error_from_unix(sys_errno);
1271 subreq = tstream_readv_pdu_queue_send(state,
1272 ipriv->ntvfs->ctx->event_ctx,
1275 ipc_readv_next_vector,
1276 &state->next_vector);
1278 status = NT_STATUS_NO_MEMORY;
1281 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1285 req->async_states->status = status;
1286 req->async_states->send_fn(req);
1289 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1291 struct ipc_ioctl_state *state =
1292 tevent_req_callback_data(subreq,
1293 struct ipc_ioctl_state);
1294 struct ntvfs_request *req = state->req;
1295 union smb_ioctl *io = state->io;
1300 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1301 TALLOC_FREE(subreq);
1303 status = map_nt_error_from_unix(sys_errno);
1307 status = NT_STATUS_OK;
1308 if (state->next_vector.remaining > 0) {
1309 status = STATUS_BUFFER_OVERFLOW;
1312 io->smb2.out.out.length = ret;
1315 req->async_states->status = status;
1316 req->async_states->send_fn(req);
1322 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1323 struct ntvfs_request *req, union smb_ioctl *io)
1325 switch (io->generic.level) {
1326 case RAW_IOCTL_SMB2:
1327 return ipc_ioctl_smb2(ntvfs, req, io);
1329 case RAW_IOCTL_SMB2_NO_HANDLE:
1330 return NT_STATUS_FS_DRIVER_REQUIRED;
1333 return NT_STATUS_ACCESS_DENIED;
1336 return NT_STATUS_ACCESS_DENIED;
1341 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1343 NTSTATUS ntvfs_ipc_init(void)
1346 struct ntvfs_ops ops;
1347 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1351 /* fill in the name and type */
1352 ops.name = "default";
1353 ops.type = NTVFS_IPC;
1355 /* fill in all the operations */
1356 ops.connect = ipc_connect;
1357 ops.disconnect = ipc_disconnect;
1358 ops.unlink = ipc_unlink;
1359 ops.chkpath = ipc_chkpath;
1360 ops.qpathinfo = ipc_qpathinfo;
1361 ops.setpathinfo = ipc_setpathinfo;
1362 ops.open = ipc_open;
1363 ops.mkdir = ipc_mkdir;
1364 ops.rmdir = ipc_rmdir;
1365 ops.rename = ipc_rename;
1366 ops.copy = ipc_copy;
1367 ops.ioctl = ipc_ioctl;
1368 ops.read = ipc_read;
1369 ops.write = ipc_write;
1370 ops.seek = ipc_seek;
1371 ops.flush = ipc_flush;
1372 ops.close = ipc_close;
1373 ops.exit = ipc_exit;
1374 ops.lock = ipc_lock;
1375 ops.setfileinfo = ipc_setfileinfo;
1376 ops.qfileinfo = ipc_qfileinfo;
1377 ops.fsinfo = ipc_fsinfo;
1379 ops.search_first = ipc_search_first;
1380 ops.search_next = ipc_search_next;
1381 ops.search_close = ipc_search_close;
1382 ops.trans = ipc_trans;
1383 ops.logoff = ipc_logoff;
1384 ops.async_setup = ipc_async_setup;
1385 ops.cancel = ipc_cancel;
1387 /* register ourselves with the NTVFS subsystem. */
1388 ret = ntvfs_register(&ops, &vers);
1390 if (!NT_STATUS_IS_OK(ret)) {
1391 DEBUG(0,("Failed to register IPC backend!\n"));