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"
45 #include "../lib/util/tevent_unix.h"
46 #include "../lib/util/tevent_coroutine.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 *client_addr;
259 const struct tsocket_address *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 client_addr = ntvfs_get_local_address(ipriv->ntvfs);
327 server_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 example_ocor_state {
635 static struct tevent_coroutine_result *example_ocor_body(struct tevent_coroutine *coro,
636 struct tevent_context *ev,
639 static struct tevent_req *example_ocor_send(TALLOC_CTX *mem_ctx,
640 struct tevent_context *ev,
643 struct tevent_req *req;
644 struct example_ocor_state *state;
645 struct tevent_coroutine *coro;
647 req = tevent_req_create(mem_ctx, &state, struct example_ocor_state);
652 state->string = string;
654 coro = tevent_coroutine_create(req, ev, example_ocor_body);
655 if (tevent_req_nomem(coro, req)) {
656 return tevent_req_post(req, ev);
659 tevent_coroutine_run(coro);
660 if (!tevent_req_is_in_progress(req)) {
661 return tevent_req_post(req, ev);
667 static struct tevent_coroutine_result *example_ocor_body(struct tevent_coroutine *coro,
668 struct tevent_context *ev,
671 struct example_ocor_state *state = talloc_get_type_abort(private_data,
672 struct example_ocor_state);
673 struct tevent_req *subreq;
676 DEBUG(0,("%s[%p]: 1. %s\n", __location__, coro, state->string));
678 subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0,500));
679 if (tevent_coroutine_nomem(subreq, coro)) {
680 return tevent_coroutine_return(coro);
682 tevent_coroutine_yield(coro, subreq);
683 ok = tevent_wakeup_recv(subreq);
685 DEBUG(0,("%s[%p]: 2. %s wakeup[%d]\n", __location__, coro, state->string, ok));
687 tevent_coroutine_done(coro);
688 return tevent_coroutine_return(coro);
691 static int example_ocor_recv(struct tevent_req *req, int *perrno)
693 if (tevent_req_is_unix_error(req, perrno)) {
700 struct example_coro_state {
704 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
705 struct tevent_context *ev,
708 static struct tevent_req *example_coro_send(TALLOC_CTX *mem_ctx,
709 struct tevent_context *ev,
712 struct tevent_req *req;
713 struct example_coro_state *state;
714 struct tevent_coroutine *coro;
716 req = tevent_req_create(mem_ctx, &state, struct example_coro_state);
721 state->string = string;
723 coro = tevent_coroutine_create(req, ev, example_coro_body);
724 if (tevent_req_nomem(coro, req)) {
725 return tevent_req_post(req, ev);
728 tevent_coroutine_run(coro);
729 if (!tevent_req_is_in_progress(req)) {
730 return tevent_req_post(req, ev);
736 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
737 struct tevent_context *ev,
740 struct example_coro_state *state = talloc_get_type_abort(private_data,
741 struct example_coro_state);
742 struct tevent_req *subreq;
746 DEBUG(0,("%s:%s[%p]: 1. %s\n", __location__, __FUNCTION__, coro, state->string));
748 subreq = example_ocor_send(state, ev, state->string);
749 if (tevent_coroutine_nomem(subreq, coro)) {
750 return tevent_coroutine_return(coro);
752 tevent_coroutine_yield(coro, subreq);
753 ret = example_ocor_recv(subreq, &sys_errno);
755 tevent_coroutine_error(coro, sys_errno);
756 return tevent_coroutine_return(coro);
759 DEBUG(0,("%s:%s[%p]: 2. %s example_ocor[%d]\n", __location__, __FUNCTION__, coro, state->string, ret));
761 tevent_coroutine_done(coro);
762 return tevent_coroutine_return(coro);
765 static int example_coro_recv(struct tevent_req *req, int *perrno)
767 if (tevent_req_is_unix_error(req, perrno)) {
774 struct ipc_write_state {
775 struct ipc_private *ipriv;
776 struct pipe_state *p;
777 struct ntvfs_request *req;
782 static void ipc_write_done(struct tevent_req *subreq);
784 static void ipc_write_coro_done(struct tevent_req *subreq);
789 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
790 struct ntvfs_request *req, union smb_write *wr)
792 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
794 struct pipe_state *p;
795 struct tevent_req *subreq;
796 struct ipc_write_state *state;
798 if (wr->generic.level != RAW_WRITE_GENERIC) {
799 return ntvfs_map_write(ntvfs, req, wr);
802 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
804 return NT_STATUS_INVALID_HANDLE;
807 state = talloc(req, struct ipc_write_state);
808 NT_STATUS_HAVE_NO_MEMORY(state);
810 state->ipriv = ipriv;
814 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
815 state->iov.iov_len = wr->writex.in.count;
817 subreq = example_coro_send(state,
818 ipriv->ntvfs->ctx->event_ctx,
820 NT_STATUS_HAVE_NO_MEMORY(subreq);
821 tevent_req_set_callback(subreq, ipc_write_coro_done, state);
823 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
827 static void ipc_write_coro_done(struct tevent_req *subreq)
829 struct ipc_write_state *state =
830 tevent_req_callback_data(subreq,
831 struct ipc_write_state);
832 struct pipe_state *p = state->p;
833 struct ipc_private *ipriv = state->ipriv;
834 struct ntvfs_request *req = state->req;
839 ret = example_coro_recv(subreq, &sys_errno);
842 status = map_nt_error_from_unix(sys_errno);
846 subreq = tstream_writev_queue_send(state,
847 ipriv->ntvfs->ctx->event_ctx,
852 status = NT_STATUS_NO_MEMORY;
855 tevent_req_set_callback(subreq, ipc_write_done, state);
859 req->async_states->status = status;
860 req->async_states->send_fn(req);
863 static void ipc_write_done(struct tevent_req *subreq)
865 struct ipc_write_state *state =
866 tevent_req_callback_data(subreq,
867 struct ipc_write_state);
868 struct ntvfs_request *req = state->req;
869 union smb_write *wr = state->wr;
874 ret = tstream_writev_queue_recv(subreq, &sys_errno);
877 status = map_nt_error_from_unix_common(sys_errno);
881 status = NT_STATUS_OK;
883 wr->writex.out.nwritten = ret;
884 wr->writex.out.remaining = 0;
887 req->async_states->status = status;
888 req->async_states->send_fn(req);
894 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
895 struct ntvfs_request *req,
898 return NT_STATUS_ACCESS_DENIED;
904 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
905 struct ntvfs_request *req,
908 return NT_STATUS_ACCESS_DENIED;
914 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
915 struct ntvfs_request *req, union smb_close *io)
917 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
919 struct pipe_state *p;
921 if (io->generic.level != RAW_CLOSE_CLOSE) {
922 return ntvfs_map_close(ntvfs, req, io);
925 p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
927 return NT_STATUS_INVALID_HANDLE;
938 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
939 struct ntvfs_request *req)
941 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
943 struct pipe_state *p, *next;
945 for (p=ipriv->pipe_list; p; p=next) {
947 if (p->handle->session_info == req->session_info &&
948 p->handle->smbpid == req->smbpid) {
957 logoff - closing files open by the user
959 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
960 struct ntvfs_request *req)
962 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
964 struct pipe_state *p, *next;
966 for (p=ipriv->pipe_list; p; p=next) {
968 if (p->handle->session_info == req->session_info) {
977 setup for an async call
979 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
980 struct ntvfs_request *req,
989 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
990 struct ntvfs_request *req)
992 return NT_STATUS_UNSUCCESSFUL;
998 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
999 struct ntvfs_request *req, union smb_lock *lck)
1001 return NT_STATUS_ACCESS_DENIED;
1005 set info on a open file
1007 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
1008 struct ntvfs_request *req, union smb_setfileinfo *info)
1010 return NT_STATUS_ACCESS_DENIED;
1014 query info on a open file
1016 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
1017 struct ntvfs_request *req, union smb_fileinfo *info)
1019 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1020 struct ipc_private);
1021 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
1023 return NT_STATUS_INVALID_HANDLE;
1025 switch (info->generic.level) {
1026 case RAW_FILEINFO_GENERIC:
1028 ZERO_STRUCT(info->generic.out);
1029 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
1030 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
1031 info->generic.out.alloc_size = 4096;
1032 info->generic.out.nlink = 1;
1033 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
1034 info->generic.out.delete_pending = 1;
1035 return NT_STATUS_OK;
1037 case RAW_FILEINFO_ALT_NAME_INFO:
1038 case RAW_FILEINFO_ALT_NAME_INFORMATION:
1039 case RAW_FILEINFO_STREAM_INFO:
1040 case RAW_FILEINFO_STREAM_INFORMATION:
1041 case RAW_FILEINFO_COMPRESSION_INFO:
1042 case RAW_FILEINFO_COMPRESSION_INFORMATION:
1043 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1044 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1045 return NT_STATUS_INVALID_PARAMETER;
1046 case RAW_FILEINFO_ALL_EAS:
1047 return NT_STATUS_ACCESS_DENIED;
1049 return ntvfs_map_qfileinfo(ntvfs, req, info);
1055 return filesystem info
1057 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
1058 struct ntvfs_request *req, union smb_fsinfo *fs)
1060 return NT_STATUS_ACCESS_DENIED;
1064 return print queue info
1066 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
1067 struct ntvfs_request *req, union smb_lpq *lpq)
1069 return NT_STATUS_ACCESS_DENIED;
1073 list files in a directory matching a wildcard pattern
1075 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
1076 struct ntvfs_request *req, union smb_search_first *io,
1077 void *search_private,
1078 bool (*callback)(void *, const union smb_search_data *))
1080 return NT_STATUS_ACCESS_DENIED;
1084 continue listing files in a directory
1086 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
1087 struct ntvfs_request *req, union smb_search_next *io,
1088 void *search_private,
1089 bool (*callback)(void *, const union smb_search_data *))
1091 return NT_STATUS_ACCESS_DENIED;
1095 end listing files in a directory
1097 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
1098 struct ntvfs_request *req, union smb_search_close *io)
1100 return NT_STATUS_ACCESS_DENIED;
1103 struct ipc_trans_state {
1104 struct ipc_private *ipriv;
1105 struct pipe_state *p;
1106 struct ntvfs_request *req;
1107 struct smb_trans2 *trans;
1108 struct iovec writev_iov;
1109 struct ipc_readv_next_vector_state next_vector;
1112 static void ipc_trans_writev_done(struct tevent_req *subreq);
1113 static void ipc_trans_readv_done(struct tevent_req *subreq);
1115 /* SMBtrans - handle a DCERPC command */
1116 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
1117 struct ntvfs_request *req, struct smb_trans2 *trans)
1119 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1120 struct ipc_private);
1121 struct pipe_state *p;
1124 struct ipc_trans_state *state;
1125 struct tevent_req *subreq;
1128 * the fnum is in setup[1], a 16 bit value
1129 * the setup[*] values are already in host byteorder
1130 * but ntvfs_handle_search_by_wire_key() expects
1133 SSVAL(&fnum, 0, trans->in.setup[1]);
1134 fnum_key = data_blob_const(&fnum, 2);
1136 p = pipe_state_find_key(ipriv, req, &fnum_key);
1138 return NT_STATUS_INVALID_HANDLE;
1142 * Trans requests are only allowed
1143 * if no other Trans or Read is active
1145 if (tevent_queue_length(p->read_queue) > 0) {
1146 return NT_STATUS_PIPE_BUSY;
1149 state = talloc(req, struct ipc_trans_state);
1150 NT_STATUS_HAVE_NO_MEMORY(state);
1152 trans->out.setup_count = 0;
1153 trans->out.setup = NULL;
1154 trans->out.params = data_blob(NULL, 0);
1155 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1156 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1158 state->ipriv = ipriv;
1161 state->trans = trans;
1162 state->writev_iov.iov_base = (char *) trans->in.data.data;
1163 state->writev_iov.iov_len = trans->in.data.length;
1165 ipc_readv_next_vector_init(&state->next_vector,
1166 trans->out.data.data,
1167 trans->out.data.length);
1169 subreq = tstream_writev_queue_send(state,
1170 ipriv->ntvfs->ctx->event_ctx,
1173 &state->writev_iov, 1);
1174 NT_STATUS_HAVE_NO_MEMORY(subreq);
1175 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1177 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1178 return NT_STATUS_OK;
1181 static void ipc_trans_writev_done(struct tevent_req *subreq)
1183 struct ipc_trans_state *state =
1184 tevent_req_callback_data(subreq,
1185 struct ipc_trans_state);
1186 struct ipc_private *ipriv = state->ipriv;
1187 struct pipe_state *p = state->p;
1188 struct ntvfs_request *req = state->req;
1193 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1194 TALLOC_FREE(subreq);
1196 status = NT_STATUS_PIPE_DISCONNECTED;
1198 } else if (ret == -1) {
1199 status = map_nt_error_from_unix_common(sys_errno);
1203 subreq = tstream_readv_pdu_queue_send(state,
1204 ipriv->ntvfs->ctx->event_ctx,
1207 ipc_readv_next_vector,
1208 &state->next_vector);
1210 status = NT_STATUS_NO_MEMORY;
1213 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1217 req->async_states->status = status;
1218 req->async_states->send_fn(req);
1221 static void ipc_trans_readv_done(struct tevent_req *subreq)
1223 struct ipc_trans_state *state =
1224 tevent_req_callback_data(subreq,
1225 struct ipc_trans_state);
1226 struct ntvfs_request *req = state->req;
1227 struct smb_trans2 *trans = state->trans;
1232 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1233 TALLOC_FREE(subreq);
1235 status = map_nt_error_from_unix_common(sys_errno);
1239 status = NT_STATUS_OK;
1240 if (state->next_vector.remaining > 0) {
1241 status = STATUS_BUFFER_OVERFLOW;
1244 trans->out.data.length = ret;
1247 req->async_states->status = status;
1248 req->async_states->send_fn(req);
1251 /* SMBtrans - set named pipe state */
1252 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1253 struct ntvfs_request *req, struct smb_trans2 *trans)
1255 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1256 struct ipc_private);
1257 struct pipe_state *p;
1260 /* the fnum is in setup[1] */
1261 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1263 p = pipe_state_find_key(ipriv, req, &fnum_key);
1265 return NT_STATUS_INVALID_HANDLE;
1268 if (trans->in.params.length != 2) {
1269 return NT_STATUS_INVALID_PARAMETER;
1273 * TODO: pass this to the tstream_npa logic
1275 p->device_state = SVAL(trans->in.params.data, 0);
1277 trans->out.setup_count = 0;
1278 trans->out.setup = NULL;
1279 trans->out.params = data_blob(NULL, 0);
1280 trans->out.data = data_blob(NULL, 0);
1282 return NT_STATUS_OK;
1286 /* SMBtrans - used to provide access to SMB pipes */
1287 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1288 struct ntvfs_request *req, struct smb_trans2 *trans)
1292 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1293 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1295 if (trans->in.setup_count != 2) {
1296 return NT_STATUS_INVALID_PARAMETER;
1299 switch (trans->in.setup[0]) {
1300 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1301 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1303 case TRANSACT_DCERPCCMD:
1304 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1307 status = NT_STATUS_INVALID_PARAMETER;
1314 struct ipc_ioctl_state {
1315 struct ipc_private *ipriv;
1316 struct pipe_state *p;
1317 struct ntvfs_request *req;
1318 union smb_ioctl *io;
1319 struct iovec writev_iov;
1320 struct ipc_readv_next_vector_state next_vector;
1323 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1324 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1326 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1327 struct ntvfs_request *req, union smb_ioctl *io)
1329 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1330 struct ipc_private);
1331 struct pipe_state *p;
1332 struct ipc_ioctl_state *state;
1333 struct tevent_req *subreq;
1335 switch (io->smb2.in.function) {
1336 case FSCTL_NAMED_PIPE_READ_WRITE:
1340 return NT_STATUS_FS_DRIVER_REQUIRED;
1343 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1345 return NT_STATUS_INVALID_HANDLE;
1349 * Trans requests are only allowed
1350 * if no other Trans or Read is active
1352 if (tevent_queue_length(p->read_queue) > 0) {
1353 return NT_STATUS_PIPE_BUSY;
1356 state = talloc(req, struct ipc_ioctl_state);
1357 NT_STATUS_HAVE_NO_MEMORY(state);
1359 io->smb2.out._pad = 0;
1360 io->smb2.out.function = io->smb2.in.function;
1361 io->smb2.out.unknown2 = 0;
1362 io->smb2.out.unknown3 = 0;
1363 io->smb2.out.in = data_blob_null;
1364 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1365 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1367 state->ipriv = ipriv;
1371 state->writev_iov.iov_base = (char *) io->smb2.in.out.data;
1372 state->writev_iov.iov_len = io->smb2.in.out.length;
1374 ipc_readv_next_vector_init(&state->next_vector,
1375 io->smb2.out.out.data,
1376 io->smb2.out.out.length);
1378 subreq = tstream_writev_queue_send(state,
1379 ipriv->ntvfs->ctx->event_ctx,
1382 &state->writev_iov, 1);
1383 NT_STATUS_HAVE_NO_MEMORY(subreq);
1384 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1386 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1387 return NT_STATUS_OK;
1390 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1392 struct ipc_ioctl_state *state =
1393 tevent_req_callback_data(subreq,
1394 struct ipc_ioctl_state);
1395 struct ipc_private *ipriv = state->ipriv;
1396 struct pipe_state *p = state->p;
1397 struct ntvfs_request *req = state->req;
1402 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1403 TALLOC_FREE(subreq);
1405 status = map_nt_error_from_unix_common(sys_errno);
1409 subreq = tstream_readv_pdu_queue_send(state,
1410 ipriv->ntvfs->ctx->event_ctx,
1413 ipc_readv_next_vector,
1414 &state->next_vector);
1416 status = NT_STATUS_NO_MEMORY;
1419 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1423 req->async_states->status = status;
1424 req->async_states->send_fn(req);
1427 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1429 struct ipc_ioctl_state *state =
1430 tevent_req_callback_data(subreq,
1431 struct ipc_ioctl_state);
1432 struct ntvfs_request *req = state->req;
1433 union smb_ioctl *io = state->io;
1438 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1439 TALLOC_FREE(subreq);
1441 status = map_nt_error_from_unix_common(sys_errno);
1445 status = NT_STATUS_OK;
1446 if (state->next_vector.remaining > 0) {
1447 status = STATUS_BUFFER_OVERFLOW;
1450 io->smb2.out.out.length = ret;
1453 req->async_states->status = status;
1454 req->async_states->send_fn(req);
1460 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1461 struct ntvfs_request *req, union smb_ioctl *io)
1463 switch (io->generic.level) {
1464 case RAW_IOCTL_SMB2:
1465 return ipc_ioctl_smb2(ntvfs, req, io);
1467 case RAW_IOCTL_SMB2_NO_HANDLE:
1468 return NT_STATUS_FS_DRIVER_REQUIRED;
1471 return NT_STATUS_ACCESS_DENIED;
1477 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1479 NTSTATUS ntvfs_ipc_init(void)
1482 struct ntvfs_ops ops;
1483 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1487 /* fill in the name and type */
1488 ops.name = "default";
1489 ops.type = NTVFS_IPC;
1491 /* fill in all the operations */
1492 ops.connect_fn = ipc_connect;
1493 ops.disconnect_fn = ipc_disconnect;
1494 ops.unlink_fn = ipc_unlink;
1495 ops.chkpath_fn = ipc_chkpath;
1496 ops.qpathinfo_fn = ipc_qpathinfo;
1497 ops.setpathinfo_fn = ipc_setpathinfo;
1498 ops.open_fn = ipc_open;
1499 ops.mkdir_fn = ipc_mkdir;
1500 ops.rmdir_fn = ipc_rmdir;
1501 ops.rename_fn = ipc_rename;
1502 ops.copy_fn = ipc_copy;
1503 ops.ioctl_fn = ipc_ioctl;
1504 ops.read_fn = ipc_read;
1505 ops.write_fn = ipc_write;
1506 ops.seek_fn = ipc_seek;
1507 ops.flush_fn = ipc_flush;
1508 ops.close_fn = ipc_close;
1509 ops.exit_fn = ipc_exit;
1510 ops.lock_fn = ipc_lock;
1511 ops.setfileinfo_fn = ipc_setfileinfo;
1512 ops.qfileinfo_fn = ipc_qfileinfo;
1513 ops.fsinfo_fn = ipc_fsinfo;
1514 ops.lpq_fn = ipc_lpq;
1515 ops.search_first_fn = ipc_search_first;
1516 ops.search_next_fn = ipc_search_next;
1517 ops.search_close_fn = ipc_search_close;
1518 ops.trans_fn = ipc_trans;
1519 ops.logoff_fn = ipc_logoff;
1520 ops.async_setup_fn = ipc_async_setup;
1521 ops.cancel_fn = ipc_cancel;
1523 /* register ourselves with the NTVFS subsystem. */
1524 ret = ntvfs_register(&ops, &vers);
1526 if (!NT_STATUS_IS_OK(ret)) {
1527 DEBUG(0,("Failed to register IPC backend!\n"));