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 *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 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 tevent_coroutine_yield(coro, subreq);
680 ok = tevent_wakeup_recv(subreq);
683 tevent_coroutine_error(coro, ENOTSUP);
684 return tevent_coroutine_return(coro);
687 DEBUG(0,("%s[%p]: 2. %s wakeup[%d]\n", __location__, coro, state->string, ok));
689 tevent_coroutine_done(coro);
690 return tevent_coroutine_return(coro);
693 static int example_ocor_recv(struct tevent_req *req, int *perrno)
695 if (tevent_req_is_unix_error(req, perrno)) {
702 struct example_coro_state {
706 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
707 struct tevent_context *ev,
710 static struct tevent_req *example_coro_send(TALLOC_CTX *mem_ctx,
711 struct tevent_context *ev,
714 struct tevent_req *req;
715 struct example_coro_state *state;
716 struct tevent_coroutine *coro;
718 req = tevent_req_create(mem_ctx, &state, struct example_coro_state);
723 state->string = string;
725 coro = tevent_coroutine_create(req, ev, example_coro_body);
726 if (tevent_req_nomem(coro, req)) {
727 return tevent_req_post(req, ev);
730 tevent_coroutine_run(coro);
731 if (!tevent_req_is_in_progress(req)) {
732 return tevent_req_post(req, ev);
738 static struct tevent_coroutine_result *example_coro_body(struct tevent_coroutine *coro,
739 struct tevent_context *ev,
742 struct example_coro_state *state = talloc_get_type_abort(private_data,
743 struct example_coro_state);
744 struct tevent_req *subreq;
748 DEBUG(0,("%s:%s[%p]: 1. %s\n", __location__, __FUNCTION__, coro, state->string));
750 subreq = example_ocor_send(state, ev, state->string);
751 tevent_coroutine_yield(coro, subreq);
752 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 subreq = example_ocor_send(state, ev, state->string);
762 tevent_coroutine_yield(coro, subreq);
763 ret = example_ocor_recv(subreq, &sys_errno);
766 tevent_coroutine_error(coro, sys_errno);
767 return tevent_coroutine_return(coro);
770 DEBUG(0,("%s:%s[%p]: 3. %s example_ocor[%d]\n", __location__, __FUNCTION__, coro, state->string, ret));
772 tevent_coroutine_done(coro);
773 return tevent_coroutine_return(coro);
776 static int example_coro_recv(struct tevent_req *req, int *perrno)
778 if (tevent_req_is_unix_error(req, perrno)) {
785 struct ipc_write_state {
786 struct ipc_private *ipriv;
787 struct pipe_state *p;
788 struct ntvfs_request *req;
793 static void ipc_write_done(struct tevent_req *subreq);
795 static void ipc_write_coro_done(struct tevent_req *subreq);
800 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
801 struct ntvfs_request *req, union smb_write *wr)
803 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
805 struct pipe_state *p;
806 struct tevent_req *subreq;
807 struct ipc_write_state *state;
809 if (wr->generic.level != RAW_WRITE_GENERIC) {
810 return ntvfs_map_write(ntvfs, req, wr);
813 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
815 return NT_STATUS_INVALID_HANDLE;
818 state = talloc(req, struct ipc_write_state);
819 NT_STATUS_HAVE_NO_MEMORY(state);
821 state->ipriv = ipriv;
825 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
826 state->iov.iov_len = wr->writex.in.count;
828 subreq = example_coro_send(state,
829 ipriv->ntvfs->ctx->event_ctx,
831 NT_STATUS_HAVE_NO_MEMORY(subreq);
832 tevent_req_set_callback(subreq, ipc_write_coro_done, state);
834 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
838 static void ipc_write_coro_done(struct tevent_req *subreq)
840 struct ipc_write_state *state =
841 tevent_req_callback_data(subreq,
842 struct ipc_write_state);
843 struct pipe_state *p = state->p;
844 struct ipc_private *ipriv = state->ipriv;
845 struct ntvfs_request *req = state->req;
850 ret = example_coro_recv(subreq, &sys_errno);
853 status = map_nt_error_from_unix(sys_errno);
857 subreq = tstream_writev_queue_send(state,
858 ipriv->ntvfs->ctx->event_ctx,
863 status = NT_STATUS_NO_MEMORY;
866 tevent_req_set_callback(subreq, ipc_write_done, state);
870 req->async_states->status = status;
871 req->async_states->send_fn(req);
874 static void ipc_write_done(struct tevent_req *subreq)
876 struct ipc_write_state *state =
877 tevent_req_callback_data(subreq,
878 struct ipc_write_state);
879 struct ntvfs_request *req = state->req;
880 union smb_write *wr = state->wr;
885 ret = tstream_writev_queue_recv(subreq, &sys_errno);
888 status = map_nt_error_from_unix_common(sys_errno);
892 status = NT_STATUS_OK;
894 wr->writex.out.nwritten = ret;
895 wr->writex.out.remaining = 0;
898 req->async_states->status = status;
899 req->async_states->send_fn(req);
905 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
906 struct ntvfs_request *req,
909 return NT_STATUS_ACCESS_DENIED;
915 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
916 struct ntvfs_request *req,
919 return NT_STATUS_ACCESS_DENIED;
925 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
926 struct ntvfs_request *req, union smb_close *io)
928 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
930 struct pipe_state *p;
932 if (io->generic.level != RAW_CLOSE_GENERIC) {
933 return ntvfs_map_close(ntvfs, req, io);
936 ZERO_STRUCT(io->generic.out);
938 p = pipe_state_find(ipriv, io->generic.in.file.ntvfs);
940 return NT_STATUS_INVALID_HANDLE;
951 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
952 struct ntvfs_request *req)
954 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
956 struct pipe_state *p, *next;
958 for (p=ipriv->pipe_list; p; p=next) {
960 if (p->handle->session_info == req->session_info &&
961 p->handle->smbpid == req->smbpid) {
970 logoff - closing files open by the user
972 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
973 struct ntvfs_request *req)
975 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
977 struct pipe_state *p, *next;
979 for (p=ipriv->pipe_list; p; p=next) {
981 if (p->handle->session_info == req->session_info) {
990 setup for an async call
992 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
993 struct ntvfs_request *req,
1000 cancel an async call
1002 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
1003 struct ntvfs_request *req)
1005 return NT_STATUS_UNSUCCESSFUL;
1011 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
1012 struct ntvfs_request *req, union smb_lock *lck)
1014 return NT_STATUS_ACCESS_DENIED;
1018 set info on a open file
1020 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
1021 struct ntvfs_request *req, union smb_setfileinfo *info)
1023 return NT_STATUS_ACCESS_DENIED;
1027 query info on a open file
1029 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
1030 struct ntvfs_request *req, union smb_fileinfo *info)
1032 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1033 struct ipc_private);
1034 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
1036 return NT_STATUS_INVALID_HANDLE;
1038 switch (info->generic.level) {
1039 case RAW_FILEINFO_GENERIC:
1041 ZERO_STRUCT(info->generic.out);
1042 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
1043 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
1044 info->generic.out.alloc_size = 4096;
1045 info->generic.out.nlink = 1;
1046 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
1047 info->generic.out.delete_pending = 1;
1048 return NT_STATUS_OK;
1050 case RAW_FILEINFO_ALT_NAME_INFO:
1051 case RAW_FILEINFO_ALT_NAME_INFORMATION:
1052 case RAW_FILEINFO_STREAM_INFO:
1053 case RAW_FILEINFO_STREAM_INFORMATION:
1054 case RAW_FILEINFO_COMPRESSION_INFO:
1055 case RAW_FILEINFO_COMPRESSION_INFORMATION:
1056 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
1057 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
1058 return NT_STATUS_INVALID_PARAMETER;
1059 case RAW_FILEINFO_ALL_EAS:
1060 return NT_STATUS_ACCESS_DENIED;
1062 return ntvfs_map_qfileinfo(ntvfs, req, info);
1068 return filesystem info
1070 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
1071 struct ntvfs_request *req, union smb_fsinfo *fs)
1073 return NT_STATUS_ACCESS_DENIED;
1077 return print queue info
1079 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
1080 struct ntvfs_request *req, union smb_lpq *lpq)
1082 return NT_STATUS_ACCESS_DENIED;
1086 list files in a directory matching a wildcard pattern
1088 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
1089 struct ntvfs_request *req, union smb_search_first *io,
1090 void *search_private,
1091 bool (*callback)(void *, const union smb_search_data *))
1093 return NT_STATUS_ACCESS_DENIED;
1097 continue listing files in a directory
1099 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
1100 struct ntvfs_request *req, union smb_search_next *io,
1101 void *search_private,
1102 bool (*callback)(void *, const union smb_search_data *))
1104 return NT_STATUS_ACCESS_DENIED;
1108 end listing files in a directory
1110 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
1111 struct ntvfs_request *req, union smb_search_close *io)
1113 return NT_STATUS_ACCESS_DENIED;
1116 struct ipc_trans_state {
1117 struct ipc_private *ipriv;
1118 struct pipe_state *p;
1119 struct ntvfs_request *req;
1120 struct smb_trans2 *trans;
1121 struct iovec writev_iov;
1122 struct ipc_readv_next_vector_state next_vector;
1125 static void ipc_trans_writev_done(struct tevent_req *subreq);
1126 static void ipc_trans_readv_done(struct tevent_req *subreq);
1128 /* SMBtrans - handle a DCERPC command */
1129 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
1130 struct ntvfs_request *req, struct smb_trans2 *trans)
1132 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1133 struct ipc_private);
1134 struct pipe_state *p;
1137 struct ipc_trans_state *state;
1138 struct tevent_req *subreq;
1141 * the fnum is in setup[1], a 16 bit value
1142 * the setup[*] values are already in host byteorder
1143 * but ntvfs_handle_search_by_wire_key() expects
1146 SSVAL(&fnum, 0, trans->in.setup[1]);
1147 fnum_key = data_blob_const(&fnum, 2);
1149 p = pipe_state_find_key(ipriv, req, &fnum_key);
1151 return NT_STATUS_INVALID_HANDLE;
1155 * Trans requests are only allowed
1156 * if no other Trans or Read is active
1158 if (tevent_queue_length(p->read_queue) > 0) {
1159 return NT_STATUS_PIPE_BUSY;
1162 state = talloc(req, struct ipc_trans_state);
1163 NT_STATUS_HAVE_NO_MEMORY(state);
1165 trans->out.setup_count = 0;
1166 trans->out.setup = NULL;
1167 trans->out.params = data_blob(NULL, 0);
1168 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1169 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1171 state->ipriv = ipriv;
1174 state->trans = trans;
1175 state->writev_iov.iov_base = (char *) trans->in.data.data;
1176 state->writev_iov.iov_len = trans->in.data.length;
1178 ipc_readv_next_vector_init(&state->next_vector,
1179 trans->out.data.data,
1180 trans->out.data.length);
1182 subreq = tstream_writev_queue_send(state,
1183 ipriv->ntvfs->ctx->event_ctx,
1186 &state->writev_iov, 1);
1187 NT_STATUS_HAVE_NO_MEMORY(subreq);
1188 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1190 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1191 return NT_STATUS_OK;
1194 static void ipc_trans_writev_done(struct tevent_req *subreq)
1196 struct ipc_trans_state *state =
1197 tevent_req_callback_data(subreq,
1198 struct ipc_trans_state);
1199 struct ipc_private *ipriv = state->ipriv;
1200 struct pipe_state *p = state->p;
1201 struct ntvfs_request *req = state->req;
1206 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1207 TALLOC_FREE(subreq);
1209 status = NT_STATUS_PIPE_DISCONNECTED;
1211 } else if (ret == -1) {
1212 status = map_nt_error_from_unix_common(sys_errno);
1216 subreq = tstream_readv_pdu_queue_send(state,
1217 ipriv->ntvfs->ctx->event_ctx,
1220 ipc_readv_next_vector,
1221 &state->next_vector);
1223 status = NT_STATUS_NO_MEMORY;
1226 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1230 req->async_states->status = status;
1231 req->async_states->send_fn(req);
1234 static void ipc_trans_readv_done(struct tevent_req *subreq)
1236 struct ipc_trans_state *state =
1237 tevent_req_callback_data(subreq,
1238 struct ipc_trans_state);
1239 struct ntvfs_request *req = state->req;
1240 struct smb_trans2 *trans = state->trans;
1245 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1246 TALLOC_FREE(subreq);
1248 status = map_nt_error_from_unix_common(sys_errno);
1252 status = NT_STATUS_OK;
1253 if (state->next_vector.remaining > 0) {
1254 status = STATUS_BUFFER_OVERFLOW;
1257 trans->out.data.length = ret;
1260 req->async_states->status = status;
1261 req->async_states->send_fn(req);
1264 /* SMBtrans - set named pipe state */
1265 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1266 struct ntvfs_request *req, struct smb_trans2 *trans)
1268 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1269 struct ipc_private);
1270 struct pipe_state *p;
1273 /* the fnum is in setup[1] */
1274 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1276 p = pipe_state_find_key(ipriv, req, &fnum_key);
1278 return NT_STATUS_INVALID_HANDLE;
1281 if (trans->in.params.length != 2) {
1282 return NT_STATUS_INVALID_PARAMETER;
1286 * TODO: pass this to the tstream_npa logic
1288 p->device_state = SVAL(trans->in.params.data, 0);
1290 trans->out.setup_count = 0;
1291 trans->out.setup = NULL;
1292 trans->out.params = data_blob(NULL, 0);
1293 trans->out.data = data_blob(NULL, 0);
1295 return NT_STATUS_OK;
1299 /* SMBtrans - used to provide access to SMB pipes */
1300 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1301 struct ntvfs_request *req, struct smb_trans2 *trans)
1305 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1306 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1308 if (trans->in.setup_count != 2) {
1309 return NT_STATUS_INVALID_PARAMETER;
1312 switch (trans->in.setup[0]) {
1313 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1314 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1316 case TRANSACT_DCERPCCMD:
1317 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1320 status = NT_STATUS_INVALID_PARAMETER;
1327 struct ipc_ioctl_state {
1328 struct ipc_private *ipriv;
1329 struct pipe_state *p;
1330 struct ntvfs_request *req;
1331 union smb_ioctl *io;
1332 struct iovec writev_iov;
1333 struct ipc_readv_next_vector_state next_vector;
1336 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1337 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1339 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1340 struct ntvfs_request *req, union smb_ioctl *io)
1342 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1343 struct ipc_private);
1344 struct pipe_state *p;
1345 struct ipc_ioctl_state *state;
1346 struct tevent_req *subreq;
1348 switch (io->smb2.in.function) {
1349 case FSCTL_NAMED_PIPE_READ_WRITE:
1353 return NT_STATUS_FS_DRIVER_REQUIRED;
1356 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1358 return NT_STATUS_INVALID_HANDLE;
1362 * Trans requests are only allowed
1363 * if no other Trans or Read is active
1365 if (tevent_queue_length(p->read_queue) > 0) {
1366 return NT_STATUS_PIPE_BUSY;
1369 state = talloc(req, struct ipc_ioctl_state);
1370 NT_STATUS_HAVE_NO_MEMORY(state);
1372 io->smb2.out._pad = 0;
1373 io->smb2.out.function = io->smb2.in.function;
1374 io->smb2.out.unknown2 = 0;
1375 io->smb2.out.unknown3 = 0;
1376 io->smb2.out.in = data_blob_null;
1377 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1378 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1380 state->ipriv = ipriv;
1384 state->writev_iov.iov_base = (char *) io->smb2.in.out.data;
1385 state->writev_iov.iov_len = io->smb2.in.out.length;
1387 ipc_readv_next_vector_init(&state->next_vector,
1388 io->smb2.out.out.data,
1389 io->smb2.out.out.length);
1391 subreq = tstream_writev_queue_send(state,
1392 ipriv->ntvfs->ctx->event_ctx,
1395 &state->writev_iov, 1);
1396 NT_STATUS_HAVE_NO_MEMORY(subreq);
1397 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1399 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1400 return NT_STATUS_OK;
1403 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1405 struct ipc_ioctl_state *state =
1406 tevent_req_callback_data(subreq,
1407 struct ipc_ioctl_state);
1408 struct ipc_private *ipriv = state->ipriv;
1409 struct pipe_state *p = state->p;
1410 struct ntvfs_request *req = state->req;
1415 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1416 TALLOC_FREE(subreq);
1418 status = map_nt_error_from_unix_common(sys_errno);
1422 subreq = tstream_readv_pdu_queue_send(state,
1423 ipriv->ntvfs->ctx->event_ctx,
1426 ipc_readv_next_vector,
1427 &state->next_vector);
1429 status = NT_STATUS_NO_MEMORY;
1432 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1436 req->async_states->status = status;
1437 req->async_states->send_fn(req);
1440 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1442 struct ipc_ioctl_state *state =
1443 tevent_req_callback_data(subreq,
1444 struct ipc_ioctl_state);
1445 struct ntvfs_request *req = state->req;
1446 union smb_ioctl *io = state->io;
1451 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1452 TALLOC_FREE(subreq);
1454 status = map_nt_error_from_unix_common(sys_errno);
1458 status = NT_STATUS_OK;
1459 if (state->next_vector.remaining > 0) {
1460 status = STATUS_BUFFER_OVERFLOW;
1463 io->smb2.out.out.length = ret;
1466 req->async_states->status = status;
1467 req->async_states->send_fn(req);
1473 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1474 struct ntvfs_request *req, union smb_ioctl *io)
1476 switch (io->generic.level) {
1477 case RAW_IOCTL_SMB2:
1478 return ipc_ioctl_smb2(ntvfs, req, io);
1480 case RAW_IOCTL_SMB2_NO_HANDLE:
1481 return NT_STATUS_FS_DRIVER_REQUIRED;
1484 return NT_STATUS_ACCESS_DENIED;
1490 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1492 NTSTATUS ntvfs_ipc_init(TALLOC_CTX *ctx)
1495 struct ntvfs_ops ops;
1496 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1500 /* fill in the name and type */
1501 ops.name = "default";
1502 ops.type = NTVFS_IPC;
1504 /* fill in all the operations */
1505 ops.connect_fn = ipc_connect;
1506 ops.disconnect_fn = ipc_disconnect;
1507 ops.unlink_fn = ipc_unlink;
1508 ops.chkpath_fn = ipc_chkpath;
1509 ops.qpathinfo_fn = ipc_qpathinfo;
1510 ops.setpathinfo_fn = ipc_setpathinfo;
1511 ops.open_fn = ipc_open;
1512 ops.mkdir_fn = ipc_mkdir;
1513 ops.rmdir_fn = ipc_rmdir;
1514 ops.rename_fn = ipc_rename;
1515 ops.copy_fn = ipc_copy;
1516 ops.ioctl_fn = ipc_ioctl;
1517 ops.read_fn = ipc_read;
1518 ops.write_fn = ipc_write;
1519 ops.seek_fn = ipc_seek;
1520 ops.flush_fn = ipc_flush;
1521 ops.close_fn = ipc_close;
1522 ops.exit_fn = ipc_exit;
1523 ops.lock_fn = ipc_lock;
1524 ops.setfileinfo_fn = ipc_setfileinfo;
1525 ops.qfileinfo_fn = ipc_qfileinfo;
1526 ops.fsinfo_fn = ipc_fsinfo;
1527 ops.lpq_fn = ipc_lpq;
1528 ops.search_first_fn = ipc_search_first;
1529 ops.search_next_fn = ipc_search_next;
1530 ops.search_close_fn = ipc_search_close;
1531 ops.trans_fn = ipc_trans;
1532 ops.logoff_fn = ipc_logoff;
1533 ops.async_setup_fn = ipc_async_setup;
1534 ops.cancel_fn = ipc_cancel;
1536 /* register ourselves with the NTVFS subsystem. */
1537 ret = ntvfs_register(&ops, &vers);
1539 if (!NT_STATUS_IS_OK(ret)) {
1540 DEBUG(0,("Failed to register IPC backend!\n"));