s4:ntvfs/ipc/vfs_ipc.c - add more "const" in order to fix warnings
[Samba/ekacnet.git] / source4 / ntvfs / ipc / vfs_ipc.c
blob676f545832e24f5ef444fb27d06f0359e093335d
1 /*
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
27 #include "includes.h"
28 #include "../lib/util/dlinklist.h"
29 #include "ntvfs/ntvfs.h"
30 #include "../librpc/gen_ndr/rap.h"
31 #include "ntvfs/ipc/proto.h"
32 #include "libcli/raw/ioctl.h"
33 #include "param/param.h"
34 #include "../lib/tsocket/tsocket.h"
35 #include "../libcli/named_pipe_auth/npa_tstream.h"
36 #include "auth/auth.h"
37 #include "auth/auth_sam_reply.h"
38 #include "lib/socket/socket.h"
39 #include "auth/credentials/credentials.h"
40 #include "auth/credentials/credentials_krb5.h"
41 #include <gssapi/gssapi.h>
42 #include "system/locale.h"
44 /* this is the private structure used to keep the state of an open
45 ipc$ connection. It needs to keep information about all open
46 pipes */
47 struct ipc_private {
48 struct ntvfs_module_context *ntvfs;
50 /* a list of open pipes */
51 struct pipe_state {
52 struct pipe_state *next, *prev;
53 struct ipc_private *ipriv;
54 const char *pipe_name;
55 struct ntvfs_handle *handle;
56 struct tstream_context *npipe;
57 uint16_t file_type;
58 uint16_t device_state;
59 uint64_t allocation_size;
60 struct tevent_queue *write_queue;
61 struct tevent_queue *read_queue;
62 } *pipe_list;
67 find a open pipe give a file handle
69 static struct pipe_state *pipe_state_find(struct ipc_private *ipriv, struct ntvfs_handle *handle)
71 struct pipe_state *s;
72 void *p;
74 p = ntvfs_handle_get_backend_data(handle, ipriv->ntvfs);
75 if (!p) return NULL;
77 s = talloc_get_type(p, struct pipe_state);
78 if (!s) return NULL;
80 return s;
84 find a open pipe give a wire fnum
86 static struct pipe_state *pipe_state_find_key(struct ipc_private *ipriv, struct ntvfs_request *req, const DATA_BLOB *key)
88 struct ntvfs_handle *h;
90 h = ntvfs_handle_search_by_wire_key(ipriv->ntvfs, req, key);
91 if (!h) return NULL;
93 return pipe_state_find(ipriv, h);
98 connect to a share - always works
100 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
101 struct ntvfs_request *req,
102 union smb_tcon* tcon)
104 struct ipc_private *ipriv;
105 const char *sharename;
107 switch (tcon->generic.level) {
108 case RAW_TCON_TCON:
109 sharename = tcon->tcon.in.service;
110 break;
111 case RAW_TCON_TCONX:
112 sharename = tcon->tconx.in.path;
113 break;
114 case RAW_TCON_SMB2:
115 sharename = tcon->smb2.in.path;
116 break;
117 default:
118 return NT_STATUS_INVALID_LEVEL;
121 if (strncmp(sharename, "\\\\", 2) == 0) {
122 char *p = strchr(sharename+2, '\\');
123 if (p) {
124 sharename = p + 1;
128 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "IPC");
129 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
131 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "IPC");
132 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
134 if (tcon->generic.level == RAW_TCON_TCONX) {
135 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
136 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
139 /* prepare the private state for this connection */
140 ipriv = talloc(ntvfs, struct ipc_private);
141 NT_STATUS_HAVE_NO_MEMORY(ipriv);
143 ntvfs->private_data = ipriv;
145 ipriv->ntvfs = ntvfs;
146 ipriv->pipe_list = NULL;
148 return NT_STATUS_OK;
152 disconnect from a share
154 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs)
156 return NT_STATUS_OK;
160 delete a file
162 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
163 struct ntvfs_request *req,
164 union smb_unlink *unl)
166 return NT_STATUS_ACCESS_DENIED;
170 check if a directory exists
172 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
173 struct ntvfs_request *req,
174 union smb_chkpath *cp)
176 return NT_STATUS_ACCESS_DENIED;
180 return info on a pathname
182 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
183 struct ntvfs_request *req, union smb_fileinfo *info)
185 switch (info->generic.level) {
186 case RAW_FILEINFO_GENERIC:
187 return NT_STATUS_INVALID_DEVICE_REQUEST;
188 case RAW_FILEINFO_GETATTR:
189 return NT_STATUS_ACCESS_DENIED;
190 default:
191 return ntvfs_map_qpathinfo(ntvfs, req, info);
196 set info on a pathname
198 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
199 struct ntvfs_request *req, union smb_setfileinfo *st)
201 return NT_STATUS_ACCESS_DENIED;
206 destroy a open pipe structure
208 static int ipc_fd_destructor(struct pipe_state *p)
210 DLIST_REMOVE(p->ipriv->pipe_list, p);
211 ntvfs_handle_remove_backend_data(p->handle, p->ipriv->ntvfs);
212 return 0;
215 struct ipc_open_state {
216 struct ipc_private *ipriv;
217 struct pipe_state *p;
218 struct ntvfs_request *req;
219 union smb_open *oi;
220 struct netr_SamInfo3 *info3;
223 static void ipc_open_done(struct tevent_req *subreq);
226 check the pipename is valid
228 static NTSTATUS validate_pipename(const char *name)
230 while (*name) {
231 if (!isalnum(*name)) return NT_STATUS_INVALID_PARAMETER;
232 name++;
234 return NT_STATUS_OK;
238 open a file - used for MSRPC pipes
240 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
241 struct ntvfs_request *req, union smb_open *oi)
243 NTSTATUS status;
244 struct pipe_state *p;
245 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
246 struct ipc_private);
247 struct ntvfs_handle *h;
248 struct ipc_open_state *state;
249 struct tevent_req *subreq;
250 const char *fname;
251 const char *directory;
252 const struct tsocket_address *client_addr;
253 const struct tsocket_address *server_addr;
254 int ret;
255 DATA_BLOB delegated_creds = data_blob_null;
257 switch (oi->generic.level) {
258 case RAW_OPEN_NTCREATEX:
259 case RAW_OPEN_NTTRANS_CREATE:
260 fname = oi->ntcreatex.in.fname;
261 break;
262 case RAW_OPEN_OPENX:
263 fname = oi->openx.in.fname;
264 break;
265 case RAW_OPEN_SMB2:
266 fname = oi->smb2.in.fname;
267 break;
268 default:
269 return NT_STATUS_NOT_SUPPORTED;
272 directory = talloc_asprintf(req, "%s/np",
273 lp_ncalrpc_dir(ipriv->ntvfs->ctx->lp_ctx));
274 NT_STATUS_HAVE_NO_MEMORY(directory);
276 state = talloc(req, struct ipc_open_state);
277 NT_STATUS_HAVE_NO_MEMORY(state);
279 status = ntvfs_handle_new(ntvfs, req, &h);
280 NT_STATUS_NOT_OK_RETURN(status);
282 p = talloc(h, struct pipe_state);
283 NT_STATUS_HAVE_NO_MEMORY(p);
285 while (fname[0] == '\\') fname++;
287 /* check for valid characters in name */
288 fname = strlower_talloc(p, fname);
290 status = validate_pipename(fname);
291 NT_STATUS_NOT_OK_RETURN(status);
293 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
294 NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
296 p->handle = h;
297 p->ipriv = ipriv;
299 p->write_queue = tevent_queue_create(p, "ipc_write_queue");
300 NT_STATUS_HAVE_NO_MEMORY(p->write_queue);
302 p->read_queue = tevent_queue_create(p, "ipc_read_queue");
303 NT_STATUS_HAVE_NO_MEMORY(p->read_queue);
305 state->ipriv = ipriv;
306 state->p = p;
307 state->req = req;
308 state->oi = oi;
310 status = auth_convert_server_info_saminfo3(state,
311 req->session_info->server_info,
312 &state->info3);
313 NT_STATUS_NOT_OK_RETURN(status);
315 client_addr = ntvfs_get_local_address(ipriv->ntvfs);
316 server_addr = ntvfs_get_remote_address(ipriv->ntvfs);
318 if (req->session_info->credentials) {
319 struct gssapi_creds_container *gcc;
320 OM_uint32 gret;
321 OM_uint32 minor_status;
322 gss_buffer_desc cred_token;
323 const char *error_string;
325 ret = cli_credentials_get_client_gss_creds(req->session_info->credentials,
326 ipriv->ntvfs->ctx->event_ctx,
327 ipriv->ntvfs->ctx->lp_ctx,
328 &gcc, &error_string);
329 if (ret) {
330 goto skip;
333 gret = gss_export_cred(&minor_status,
334 gcc->creds,
335 &cred_token);
336 if (gret != GSS_S_COMPLETE) {
337 return NT_STATUS_INTERNAL_ERROR;
340 if (cred_token.length) {
341 delegated_creds = data_blob_talloc(req,
342 cred_token.value,
343 cred_token.length);
344 gss_release_buffer(&minor_status, &cred_token);
345 NT_STATUS_HAVE_NO_MEMORY(delegated_creds.data);
349 skip:
351 subreq = tstream_npa_connect_send(p,
352 ipriv->ntvfs->ctx->event_ctx,
353 directory,
354 fname,
355 client_addr,
356 NULL,
357 server_addr,
358 NULL,
359 state->info3,
360 req->session_info->session_key,
361 delegated_creds);
362 NT_STATUS_HAVE_NO_MEMORY(subreq);
363 tevent_req_set_callback(subreq, ipc_open_done, state);
365 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
366 return NT_STATUS_OK;
369 static void ipc_open_done(struct tevent_req *subreq)
371 struct ipc_open_state *state = tevent_req_callback_data(subreq,
372 struct ipc_open_state);
373 struct ipc_private *ipriv = state->ipriv;
374 struct pipe_state *p = state->p;
375 struct ntvfs_request *req = state->req;
376 union smb_open *oi = state->oi;
377 int ret;
378 int sys_errno;
379 NTSTATUS status;
381 ret = tstream_npa_connect_recv(subreq, &sys_errno,
382 p, &p->npipe,
383 &p->file_type,
384 &p->device_state,
385 &p->allocation_size);
386 TALLOC_FREE(subreq);
387 if (ret == -1) {
388 status = map_nt_error_from_unix(sys_errno);
389 goto reply;
392 DLIST_ADD(ipriv->pipe_list, p);
393 talloc_set_destructor(p, ipc_fd_destructor);
395 status = ntvfs_handle_set_backend_data(p->handle, ipriv->ntvfs, p);
396 if (!NT_STATUS_IS_OK(status)) {
397 goto reply;
400 switch (oi->generic.level) {
401 case RAW_OPEN_NTCREATEX:
402 ZERO_STRUCT(oi->ntcreatex.out);
403 oi->ntcreatex.out.file.ntvfs = p->handle;
404 oi->ntcreatex.out.oplock_level = 0;
405 oi->ntcreatex.out.create_action = NTCREATEX_ACTION_EXISTED;
406 oi->ntcreatex.out.create_time = 0;
407 oi->ntcreatex.out.access_time = 0;
408 oi->ntcreatex.out.write_time = 0;
409 oi->ntcreatex.out.change_time = 0;
410 oi->ntcreatex.out.attrib = FILE_ATTRIBUTE_NORMAL;
411 oi->ntcreatex.out.alloc_size = p->allocation_size;
412 oi->ntcreatex.out.size = 0;
413 oi->ntcreatex.out.file_type = p->file_type;
414 oi->ntcreatex.out.ipc_state = p->device_state;
415 oi->ntcreatex.out.is_directory = 0;
416 break;
417 case RAW_OPEN_OPENX:
418 ZERO_STRUCT(oi->openx.out);
419 oi->openx.out.file.ntvfs = p->handle;
420 oi->openx.out.attrib = FILE_ATTRIBUTE_NORMAL;
421 oi->openx.out.write_time = 0;
422 oi->openx.out.size = 0;
423 oi->openx.out.access = 0;
424 oi->openx.out.ftype = p->file_type;
425 oi->openx.out.devstate = p->device_state;
426 oi->openx.out.action = 0;
427 oi->openx.out.unique_fid = 0;
428 oi->openx.out.access_mask = 0;
429 oi->openx.out.unknown = 0;
430 break;
431 case RAW_OPEN_SMB2:
432 ZERO_STRUCT(oi->smb2.out);
433 oi->smb2.out.file.ntvfs = p->handle;
434 oi->smb2.out.oplock_level = oi->smb2.in.oplock_level;
435 oi->smb2.out.create_action = NTCREATEX_ACTION_EXISTED;
436 oi->smb2.out.create_time = 0;
437 oi->smb2.out.access_time = 0;
438 oi->smb2.out.write_time = 0;
439 oi->smb2.out.change_time = 0;
440 oi->smb2.out.alloc_size = p->allocation_size;
441 oi->smb2.out.size = 0;
442 oi->smb2.out.file_attr = FILE_ATTRIBUTE_NORMAL;
443 oi->smb2.out.reserved2 = 0;
444 break;
445 default:
446 break;
449 reply:
450 req->async_states->status = status;
451 req->async_states->send_fn(req);
455 create a directory
457 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
458 struct ntvfs_request *req, union smb_mkdir *md)
460 return NT_STATUS_ACCESS_DENIED;
464 remove a directory
466 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
467 struct ntvfs_request *req, struct smb_rmdir *rd)
469 return NT_STATUS_ACCESS_DENIED;
473 rename a set of files
475 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
476 struct ntvfs_request *req, union smb_rename *ren)
478 return NT_STATUS_ACCESS_DENIED;
482 copy a set of files
484 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
485 struct ntvfs_request *req, struct smb_copy *cp)
487 return NT_STATUS_ACCESS_DENIED;
490 struct ipc_readv_next_vector_state {
491 uint8_t *buf;
492 size_t len;
493 off_t ofs;
494 size_t remaining;
497 static void ipc_readv_next_vector_init(struct ipc_readv_next_vector_state *s,
498 uint8_t *buf, size_t len)
500 ZERO_STRUCTP(s);
502 s->buf = buf;
503 s->len = MIN(len, UINT16_MAX);
506 static int ipc_readv_next_vector(struct tstream_context *stream,
507 void *private_data,
508 TALLOC_CTX *mem_ctx,
509 struct iovec **_vector,
510 size_t *count)
512 struct ipc_readv_next_vector_state *state =
513 (struct ipc_readv_next_vector_state *)private_data;
514 struct iovec *vector;
515 ssize_t pending;
516 size_t wanted;
518 if (state->ofs == state->len) {
519 *_vector = NULL;
520 *count = 0;
521 return 0;
524 pending = tstream_pending_bytes(stream);
525 if (pending == -1) {
526 return -1;
529 if (pending == 0 && state->ofs != 0) {
530 /* return a short read */
531 *_vector = NULL;
532 *count = 0;
533 return 0;
536 if (pending == 0) {
537 /* we want at least one byte and recheck again */
538 wanted = 1;
539 } else {
540 size_t missing = state->len - state->ofs;
541 if (pending > missing) {
542 /* there's more available */
543 state->remaining = pending - missing;
544 wanted = missing;
545 } else {
546 /* read what we can get and recheck in the next cycle */
547 wanted = pending;
551 vector = talloc_array(mem_ctx, struct iovec, 1);
552 if (!vector) {
553 return -1;
556 vector[0].iov_base = state->buf + state->ofs;
557 vector[0].iov_len = wanted;
559 state->ofs += wanted;
561 *_vector = vector;
562 *count = 1;
563 return 0;
566 struct ipc_read_state {
567 struct ipc_private *ipriv;
568 struct pipe_state *p;
569 struct ntvfs_request *req;
570 union smb_read *rd;
571 struct ipc_readv_next_vector_state next_vector;
574 static void ipc_read_done(struct tevent_req *subreq);
577 read from a file
579 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
580 struct ntvfs_request *req, union smb_read *rd)
582 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
583 struct ipc_private);
584 struct pipe_state *p;
585 struct ipc_read_state *state;
586 struct tevent_req *subreq;
588 if (rd->generic.level != RAW_READ_GENERIC) {
589 return ntvfs_map_read(ntvfs, req, rd);
592 p = pipe_state_find(ipriv, rd->readx.in.file.ntvfs);
593 if (!p) {
594 return NT_STATUS_INVALID_HANDLE;
597 state = talloc(req, struct ipc_read_state);
598 NT_STATUS_HAVE_NO_MEMORY(state);
600 state->ipriv = ipriv;
601 state->p = p;
602 state->req = req;
603 state->rd = rd;
605 /* rd->readx.out.data is already allocated */
606 ipc_readv_next_vector_init(&state->next_vector,
607 rd->readx.out.data,
608 rd->readx.in.maxcnt);
610 subreq = tstream_readv_pdu_queue_send(req,
611 ipriv->ntvfs->ctx->event_ctx,
612 p->npipe,
613 p->read_queue,
614 ipc_readv_next_vector,
615 &state->next_vector);
616 NT_STATUS_HAVE_NO_MEMORY(subreq);
617 tevent_req_set_callback(subreq, ipc_read_done, state);
619 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
620 return NT_STATUS_OK;
623 static void ipc_read_done(struct tevent_req *subreq)
625 struct ipc_read_state *state =
626 tevent_req_callback_data(subreq,
627 struct ipc_read_state);
628 struct ntvfs_request *req = state->req;
629 union smb_read *rd = state->rd;
630 int ret;
631 int sys_errno;
632 NTSTATUS status;
634 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
635 TALLOC_FREE(subreq);
636 if (ret == -1) {
637 status = map_nt_error_from_unix(sys_errno);
638 goto reply;
641 status = NT_STATUS_OK;
642 if (state->next_vector.remaining > 0) {
643 status = STATUS_BUFFER_OVERFLOW;
646 rd->readx.out.remaining = state->next_vector.remaining;
647 rd->readx.out.compaction_mode = 0;
648 rd->readx.out.nread = ret;
650 reply:
651 req->async_states->status = status;
652 req->async_states->send_fn(req);
655 struct ipc_write_state {
656 struct ipc_private *ipriv;
657 struct pipe_state *p;
658 struct ntvfs_request *req;
659 union smb_write *wr;
660 struct iovec iov;
663 static void ipc_write_done(struct tevent_req *subreq);
666 write to a file
668 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
669 struct ntvfs_request *req, union smb_write *wr)
671 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
672 struct ipc_private);
673 struct pipe_state *p;
674 struct tevent_req *subreq;
675 struct ipc_write_state *state;
677 if (wr->generic.level != RAW_WRITE_GENERIC) {
678 return ntvfs_map_write(ntvfs, req, wr);
681 p = pipe_state_find(ipriv, wr->writex.in.file.ntvfs);
682 if (!p) {
683 return NT_STATUS_INVALID_HANDLE;
686 state = talloc(req, struct ipc_write_state);
687 NT_STATUS_HAVE_NO_MEMORY(state);
689 state->ipriv = ipriv;
690 state->p = p;
691 state->req = req;
692 state->wr = wr;
693 state->iov.iov_base = discard_const_p(void, wr->writex.in.data);
694 state->iov.iov_len = wr->writex.in.count;
696 subreq = tstream_writev_queue_send(state,
697 ipriv->ntvfs->ctx->event_ctx,
698 p->npipe,
699 p->write_queue,
700 &state->iov, 1);
701 NT_STATUS_HAVE_NO_MEMORY(subreq);
702 tevent_req_set_callback(subreq, ipc_write_done, state);
704 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
705 return NT_STATUS_OK;
708 static void ipc_write_done(struct tevent_req *subreq)
710 struct ipc_write_state *state =
711 tevent_req_callback_data(subreq,
712 struct ipc_write_state);
713 struct ntvfs_request *req = state->req;
714 union smb_write *wr = state->wr;
715 int ret;
716 int sys_errno;
717 NTSTATUS status;
719 ret = tstream_writev_queue_recv(subreq, &sys_errno);
720 TALLOC_FREE(subreq);
721 if (ret == -1) {
722 status = map_nt_error_from_unix(sys_errno);
723 goto reply;
726 status = NT_STATUS_OK;
728 wr->writex.out.nwritten = ret;
729 wr->writex.out.remaining = 0;
731 reply:
732 req->async_states->status = status;
733 req->async_states->send_fn(req);
737 seek in a file
739 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
740 struct ntvfs_request *req,
741 union smb_seek *io)
743 return NT_STATUS_ACCESS_DENIED;
747 flush a file
749 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
750 struct ntvfs_request *req,
751 union smb_flush *io)
753 return NT_STATUS_ACCESS_DENIED;
757 close a file
759 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
760 struct ntvfs_request *req, union smb_close *io)
762 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
763 struct ipc_private);
764 struct pipe_state *p;
766 if (io->generic.level != RAW_CLOSE_CLOSE) {
767 return ntvfs_map_close(ntvfs, req, io);
770 p = pipe_state_find(ipriv, io->close.in.file.ntvfs);
771 if (!p) {
772 return NT_STATUS_INVALID_HANDLE;
775 talloc_free(p);
777 return NT_STATUS_OK;
781 exit - closing files
783 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
784 struct ntvfs_request *req)
786 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
787 struct ipc_private);
788 struct pipe_state *p, *next;
790 for (p=ipriv->pipe_list; p; p=next) {
791 next = p->next;
792 if (p->handle->session_info == req->session_info &&
793 p->handle->smbpid == req->smbpid) {
794 talloc_free(p);
798 return NT_STATUS_OK;
802 logoff - closing files open by the user
804 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
805 struct ntvfs_request *req)
807 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
808 struct ipc_private);
809 struct pipe_state *p, *next;
811 for (p=ipriv->pipe_list; p; p=next) {
812 next = p->next;
813 if (p->handle->session_info == req->session_info) {
814 talloc_free(p);
818 return NT_STATUS_OK;
822 setup for an async call
824 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
825 struct ntvfs_request *req,
826 void *private_data)
828 return NT_STATUS_OK;
832 cancel an async call
834 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
835 struct ntvfs_request *req)
837 return NT_STATUS_UNSUCCESSFUL;
841 lock a byte range
843 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
844 struct ntvfs_request *req, union smb_lock *lck)
846 return NT_STATUS_ACCESS_DENIED;
850 set info on a open file
852 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
853 struct ntvfs_request *req, union smb_setfileinfo *info)
855 return NT_STATUS_ACCESS_DENIED;
859 query info on a open file
861 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
862 struct ntvfs_request *req, union smb_fileinfo *info)
864 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
865 struct ipc_private);
866 struct pipe_state *p = pipe_state_find(ipriv, info->generic.in.file.ntvfs);
867 if (!p) {
868 return NT_STATUS_INVALID_HANDLE;
870 switch (info->generic.level) {
871 case RAW_FILEINFO_GENERIC:
873 ZERO_STRUCT(info->generic.out);
874 info->generic.out.attrib = FILE_ATTRIBUTE_NORMAL;
875 info->generic.out.fname.s = strrchr(p->pipe_name, '\\');
876 info->generic.out.alloc_size = 4096;
877 info->generic.out.nlink = 1;
878 /* What the heck? Match Win2k3: IPC$ pipes are delete pending */
879 info->generic.out.delete_pending = 1;
880 return NT_STATUS_OK;
882 case RAW_FILEINFO_ALT_NAME_INFO:
883 case RAW_FILEINFO_ALT_NAME_INFORMATION:
884 case RAW_FILEINFO_STREAM_INFO:
885 case RAW_FILEINFO_STREAM_INFORMATION:
886 case RAW_FILEINFO_COMPRESSION_INFO:
887 case RAW_FILEINFO_COMPRESSION_INFORMATION:
888 case RAW_FILEINFO_NETWORK_OPEN_INFORMATION:
889 case RAW_FILEINFO_ATTRIBUTE_TAG_INFORMATION:
890 return NT_STATUS_INVALID_PARAMETER;
891 case RAW_FILEINFO_ALL_EAS:
892 return NT_STATUS_ACCESS_DENIED;
893 default:
894 return ntvfs_map_qfileinfo(ntvfs, req, info);
897 return NT_STATUS_ACCESS_DENIED;
902 return filesystem info
904 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
905 struct ntvfs_request *req, union smb_fsinfo *fs)
907 return NT_STATUS_ACCESS_DENIED;
911 return print queue info
913 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
914 struct ntvfs_request *req, union smb_lpq *lpq)
916 return NT_STATUS_ACCESS_DENIED;
920 list files in a directory matching a wildcard pattern
922 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
923 struct ntvfs_request *req, union smb_search_first *io,
924 void *search_private,
925 bool (*callback)(void *, const union smb_search_data *))
927 return NT_STATUS_ACCESS_DENIED;
931 continue listing files in a directory
933 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
934 struct ntvfs_request *req, union smb_search_next *io,
935 void *search_private,
936 bool (*callback)(void *, const union smb_search_data *))
938 return NT_STATUS_ACCESS_DENIED;
942 end listing files in a directory
944 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
945 struct ntvfs_request *req, union smb_search_close *io)
947 return NT_STATUS_ACCESS_DENIED;
950 struct ipc_trans_state {
951 struct ipc_private *ipriv;
952 struct pipe_state *p;
953 struct ntvfs_request *req;
954 struct smb_trans2 *trans;
955 struct iovec writev_iov;
956 struct ipc_readv_next_vector_state next_vector;
959 static void ipc_trans_writev_done(struct tevent_req *subreq);
960 static void ipc_trans_readv_done(struct tevent_req *subreq);
962 /* SMBtrans - handle a DCERPC command */
963 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
964 struct ntvfs_request *req, struct smb_trans2 *trans)
966 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
967 struct ipc_private);
968 struct pipe_state *p;
969 DATA_BLOB fnum_key;
970 uint16_t fnum;
971 struct ipc_trans_state *state;
972 struct tevent_req *subreq;
975 * the fnum is in setup[1], a 16 bit value
976 * the setup[*] values are already in host byteorder
977 * but ntvfs_handle_search_by_wire_key() expects
978 * network byteorder
980 SSVAL(&fnum, 0, trans->in.setup[1]);
981 fnum_key = data_blob_const(&fnum, 2);
983 p = pipe_state_find_key(ipriv, req, &fnum_key);
984 if (!p) {
985 return NT_STATUS_INVALID_HANDLE;
989 * Trans requests are only allowed
990 * if no other Trans or Read is active
992 if (tevent_queue_length(p->read_queue) > 0) {
993 return NT_STATUS_PIPE_BUSY;
996 state = talloc(req, struct ipc_trans_state);
997 NT_STATUS_HAVE_NO_MEMORY(state);
999 trans->out.setup_count = 0;
1000 trans->out.setup = NULL;
1001 trans->out.params = data_blob(NULL, 0);
1002 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
1003 NT_STATUS_HAVE_NO_MEMORY(trans->out.data.data);
1005 state->ipriv = ipriv;
1006 state->p = p;
1007 state->req = req;
1008 state->trans = trans;
1009 state->writev_iov.iov_base = trans->in.data.data;
1010 state->writev_iov.iov_len = trans->in.data.length;
1012 ipc_readv_next_vector_init(&state->next_vector,
1013 trans->out.data.data,
1014 trans->out.data.length);
1016 subreq = tstream_writev_queue_send(state,
1017 ipriv->ntvfs->ctx->event_ctx,
1018 p->npipe,
1019 p->write_queue,
1020 &state->writev_iov, 1);
1021 NT_STATUS_HAVE_NO_MEMORY(subreq);
1022 tevent_req_set_callback(subreq, ipc_trans_writev_done, state);
1024 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1025 return NT_STATUS_OK;
1028 static void ipc_trans_writev_done(struct tevent_req *subreq)
1030 struct ipc_trans_state *state =
1031 tevent_req_callback_data(subreq,
1032 struct ipc_trans_state);
1033 struct ipc_private *ipriv = state->ipriv;
1034 struct pipe_state *p = state->p;
1035 struct ntvfs_request *req = state->req;
1036 int ret;
1037 int sys_errno;
1038 NTSTATUS status;
1040 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1041 TALLOC_FREE(subreq);
1042 if (ret == 0) {
1043 status = NT_STATUS_PIPE_DISCONNECTED;
1044 goto reply;
1045 } else if (ret == -1) {
1046 status = map_nt_error_from_unix(sys_errno);
1047 goto reply;
1050 subreq = tstream_readv_pdu_queue_send(state,
1051 ipriv->ntvfs->ctx->event_ctx,
1052 p->npipe,
1053 p->read_queue,
1054 ipc_readv_next_vector,
1055 &state->next_vector);
1056 if (!subreq) {
1057 status = NT_STATUS_NO_MEMORY;
1058 goto reply;
1060 tevent_req_set_callback(subreq, ipc_trans_readv_done, state);
1061 return;
1063 reply:
1064 req->async_states->status = status;
1065 req->async_states->send_fn(req);
1068 static void ipc_trans_readv_done(struct tevent_req *subreq)
1070 struct ipc_trans_state *state =
1071 tevent_req_callback_data(subreq,
1072 struct ipc_trans_state);
1073 struct ntvfs_request *req = state->req;
1074 struct smb_trans2 *trans = state->trans;
1075 int ret;
1076 int sys_errno;
1077 NTSTATUS status;
1079 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1080 TALLOC_FREE(subreq);
1081 if (ret == -1) {
1082 status = map_nt_error_from_unix(sys_errno);
1083 goto reply;
1086 status = NT_STATUS_OK;
1087 if (state->next_vector.remaining > 0) {
1088 status = STATUS_BUFFER_OVERFLOW;
1091 trans->out.data.length = ret;
1093 reply:
1094 req->async_states->status = status;
1095 req->async_states->send_fn(req);
1098 /* SMBtrans - set named pipe state */
1099 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
1100 struct ntvfs_request *req, struct smb_trans2 *trans)
1102 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1103 struct ipc_private);
1104 struct pipe_state *p;
1105 DATA_BLOB fnum_key;
1107 /* the fnum is in setup[1] */
1108 fnum_key = data_blob_const(&trans->in.setup[1], sizeof(trans->in.setup[1]));
1110 p = pipe_state_find_key(ipriv, req, &fnum_key);
1111 if (!p) {
1112 return NT_STATUS_INVALID_HANDLE;
1115 if (trans->in.params.length != 2) {
1116 return NT_STATUS_INVALID_PARAMETER;
1120 * TODO: pass this to the tstream_npa logic
1122 p->device_state = SVAL(trans->in.params.data, 0);
1124 trans->out.setup_count = 0;
1125 trans->out.setup = NULL;
1126 trans->out.params = data_blob(NULL, 0);
1127 trans->out.data = data_blob(NULL, 0);
1129 return NT_STATUS_OK;
1133 /* SMBtrans - used to provide access to SMB pipes */
1134 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
1135 struct ntvfs_request *req, struct smb_trans2 *trans)
1137 NTSTATUS status;
1139 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
1140 return ipc_rap_call(req, ntvfs->ctx->event_ctx, ntvfs->ctx->lp_ctx, trans);
1142 if (trans->in.setup_count != 2) {
1143 return NT_STATUS_INVALID_PARAMETER;
1146 switch (trans->in.setup[0]) {
1147 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
1148 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
1149 break;
1150 case TRANSACT_DCERPCCMD:
1151 status = ipc_dcerpc_cmd(ntvfs, req, trans);
1152 break;
1153 default:
1154 status = NT_STATUS_INVALID_PARAMETER;
1155 break;
1158 return status;
1161 struct ipc_ioctl_state {
1162 struct ipc_private *ipriv;
1163 struct pipe_state *p;
1164 struct ntvfs_request *req;
1165 union smb_ioctl *io;
1166 struct iovec writev_iov;
1167 struct ipc_readv_next_vector_state next_vector;
1170 static void ipc_ioctl_writev_done(struct tevent_req *subreq);
1171 static void ipc_ioctl_readv_done(struct tevent_req *subreq);
1173 static NTSTATUS ipc_ioctl_smb2(struct ntvfs_module_context *ntvfs,
1174 struct ntvfs_request *req, union smb_ioctl *io)
1176 struct ipc_private *ipriv = talloc_get_type_abort(ntvfs->private_data,
1177 struct ipc_private);
1178 struct pipe_state *p;
1179 struct ipc_ioctl_state *state;
1180 struct tevent_req *subreq;
1182 switch (io->smb2.in.function) {
1183 case FSCTL_NAMED_PIPE_READ_WRITE:
1184 break;
1186 default:
1187 return NT_STATUS_FS_DRIVER_REQUIRED;
1190 p = pipe_state_find(ipriv, io->smb2.in.file.ntvfs);
1191 if (!p) {
1192 return NT_STATUS_INVALID_HANDLE;
1196 * Trans requests are only allowed
1197 * if no other Trans or Read is active
1199 if (tevent_queue_length(p->read_queue) > 0) {
1200 return NT_STATUS_PIPE_BUSY;
1203 state = talloc(req, struct ipc_ioctl_state);
1204 NT_STATUS_HAVE_NO_MEMORY(state);
1206 io->smb2.out._pad = 0;
1207 io->smb2.out.function = io->smb2.in.function;
1208 io->smb2.out.unknown2 = 0;
1209 io->smb2.out.unknown3 = 0;
1210 io->smb2.out.in = io->smb2.in.out;
1211 io->smb2.out.out = data_blob_talloc(req, NULL, io->smb2.in.max_response_size);
1212 NT_STATUS_HAVE_NO_MEMORY(io->smb2.out.out.data);
1214 state->ipriv = ipriv;
1215 state->p = p;
1216 state->req = req;
1217 state->io = io;
1218 state->writev_iov.iov_base = io->smb2.in.out.data;
1219 state->writev_iov.iov_len = io->smb2.in.out.length;
1221 ipc_readv_next_vector_init(&state->next_vector,
1222 io->smb2.out.out.data,
1223 io->smb2.out.out.length);
1225 subreq = tstream_writev_queue_send(state,
1226 ipriv->ntvfs->ctx->event_ctx,
1227 p->npipe,
1228 p->write_queue,
1229 &state->writev_iov, 1);
1230 NT_STATUS_HAVE_NO_MEMORY(subreq);
1231 tevent_req_set_callback(subreq, ipc_ioctl_writev_done, state);
1233 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
1234 return NT_STATUS_OK;
1237 static void ipc_ioctl_writev_done(struct tevent_req *subreq)
1239 struct ipc_ioctl_state *state =
1240 tevent_req_callback_data(subreq,
1241 struct ipc_ioctl_state);
1242 struct ipc_private *ipriv = state->ipriv;
1243 struct pipe_state *p = state->p;
1244 struct ntvfs_request *req = state->req;
1245 int ret;
1246 int sys_errno;
1247 NTSTATUS status;
1249 ret = tstream_writev_queue_recv(subreq, &sys_errno);
1250 TALLOC_FREE(subreq);
1251 if (ret == -1) {
1252 status = map_nt_error_from_unix(sys_errno);
1253 goto reply;
1256 subreq = tstream_readv_pdu_queue_send(state,
1257 ipriv->ntvfs->ctx->event_ctx,
1258 p->npipe,
1259 p->read_queue,
1260 ipc_readv_next_vector,
1261 &state->next_vector);
1262 if (!subreq) {
1263 status = NT_STATUS_NO_MEMORY;
1264 goto reply;
1266 tevent_req_set_callback(subreq, ipc_ioctl_readv_done, state);
1267 return;
1269 reply:
1270 req->async_states->status = status;
1271 req->async_states->send_fn(req);
1274 static void ipc_ioctl_readv_done(struct tevent_req *subreq)
1276 struct ipc_ioctl_state *state =
1277 tevent_req_callback_data(subreq,
1278 struct ipc_ioctl_state);
1279 struct ntvfs_request *req = state->req;
1280 union smb_ioctl *io = state->io;
1281 int ret;
1282 int sys_errno;
1283 NTSTATUS status;
1285 ret = tstream_readv_pdu_queue_recv(subreq, &sys_errno);
1286 TALLOC_FREE(subreq);
1287 if (ret == -1) {
1288 status = map_nt_error_from_unix(sys_errno);
1289 goto reply;
1292 status = NT_STATUS_OK;
1293 if (state->next_vector.remaining > 0) {
1294 status = STATUS_BUFFER_OVERFLOW;
1297 io->smb2.out.out.length = ret;
1299 reply:
1300 req->async_states->status = status;
1301 req->async_states->send_fn(req);
1305 ioctl interface
1307 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
1308 struct ntvfs_request *req, union smb_ioctl *io)
1310 switch (io->generic.level) {
1311 case RAW_IOCTL_SMB2:
1312 return ipc_ioctl_smb2(ntvfs, req, io);
1314 case RAW_IOCTL_SMB2_NO_HANDLE:
1315 return NT_STATUS_FS_DRIVER_REQUIRED;
1317 default:
1318 return NT_STATUS_ACCESS_DENIED;
1321 return NT_STATUS_ACCESS_DENIED;
1326 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
1328 NTSTATUS ntvfs_ipc_init(void)
1330 NTSTATUS ret;
1331 struct ntvfs_ops ops;
1332 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1334 ZERO_STRUCT(ops);
1336 /* fill in the name and type */
1337 ops.name = "default";
1338 ops.type = NTVFS_IPC;
1340 /* fill in all the operations */
1341 ops.connect = ipc_connect;
1342 ops.disconnect = ipc_disconnect;
1343 ops.unlink = ipc_unlink;
1344 ops.chkpath = ipc_chkpath;
1345 ops.qpathinfo = ipc_qpathinfo;
1346 ops.setpathinfo = ipc_setpathinfo;
1347 ops.open = ipc_open;
1348 ops.mkdir = ipc_mkdir;
1349 ops.rmdir = ipc_rmdir;
1350 ops.rename = ipc_rename;
1351 ops.copy = ipc_copy;
1352 ops.ioctl = ipc_ioctl;
1353 ops.read = ipc_read;
1354 ops.write = ipc_write;
1355 ops.seek = ipc_seek;
1356 ops.flush = ipc_flush;
1357 ops.close = ipc_close;
1358 ops.exit = ipc_exit;
1359 ops.lock = ipc_lock;
1360 ops.setfileinfo = ipc_setfileinfo;
1361 ops.qfileinfo = ipc_qfileinfo;
1362 ops.fsinfo = ipc_fsinfo;
1363 ops.lpq = ipc_lpq;
1364 ops.search_first = ipc_search_first;
1365 ops.search_next = ipc_search_next;
1366 ops.search_close = ipc_search_close;
1367 ops.trans = ipc_trans;
1368 ops.logoff = ipc_logoff;
1369 ops.async_setup = ipc_async_setup;
1370 ops.cancel = ipc_cancel;
1372 /* register ourselves with the NTVFS subsystem. */
1373 ret = ntvfs_register(&ops, &vers);
1375 if (!NT_STATUS_IS_OK(ret)) {
1376 DEBUG(0,("Failed to register IPC backend!\n"));
1377 return ret;
1380 return ret;