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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 this implements the IPC$ backend, called by the NTVFS subsystem to
24 handle requests on IPC$ shares
29 #include "system/filesys.h"
30 #include "dlinklist.h"
31 #include "smb_server/smb_server.h"
33 #define IPC_BASE_FNUM 0x400
35 /* this is the private structure used to keep the state of an open
36 ipc$ connection. It needs to keep information about all open
39 struct idr_context *idtree_fnum;
41 struct dcesrv_context *dcesrv;
43 /* a list of open pipes */
45 struct pipe_state *next, *prev;
46 struct ipc_private *private;
47 const char *pipe_name;
49 struct dcesrv_connection *dce_conn;
51 /* we need to remember the session it was opened on,
52 as it is illegal to operate on someone elses fnum */
53 struct smbsrv_session *session;
55 /* we need to remember the client pid that
56 opened the file so SMBexit works */
64 find a open pipe give a file descriptor
66 static struct pipe_state *pipe_state_find(struct ipc_private *private, uint16_t fnum)
68 return idr_find(private->idtree_fnum, fnum);
73 connect to a share - always works
75 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
76 struct smbsrv_request *req, const char *sharename)
79 struct smbsrv_tcon *tcon = req->tcon;
80 struct ipc_private *private;
82 tcon->fs_type = talloc_strdup(tcon, "IPC");
83 tcon->dev_type = talloc_strdup(tcon, "IPC");
85 /* prepare the private state for this connection */
86 private = talloc(tcon, struct ipc_private);
87 NT_STATUS_HAVE_NO_MEMORY(private);
89 ntvfs->private_data = private;
91 private->pipe_list = NULL;
93 private->idtree_fnum = idr_init(private);
94 NT_STATUS_HAVE_NO_MEMORY(private->idtree_fnum);
96 /* setup the DCERPC server subsystem */
97 status = dcesrv_init_ipc_context(private, &private->dcesrv);
98 NT_STATUS_NOT_OK_RETURN(status);
104 disconnect from a share
106 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs,
107 struct smbsrv_tcon *tcon)
115 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
116 struct smbsrv_request *req, struct smb_unlink *unl)
118 return NT_STATUS_ACCESS_DENIED;
123 ioctl interface - we don't do any
125 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
126 struct smbsrv_request *req, union smb_ioctl *io)
128 return NT_STATUS_ACCESS_DENIED;
132 check if a directory exists
134 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
135 struct smbsrv_request *req, struct smb_chkpath *cp)
137 return NT_STATUS_ACCESS_DENIED;
141 return info on a pathname
143 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
144 struct smbsrv_request *req, union smb_fileinfo *info)
146 return NT_STATUS_ACCESS_DENIED;
150 set info on a pathname
152 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
153 struct smbsrv_request *req, union smb_setfileinfo *st)
155 return NT_STATUS_ACCESS_DENIED;
160 destroy a open pipe structure
162 static int ipc_fd_destructor(void *ptr)
164 struct pipe_state *p = ptr;
165 idr_remove(p->private->idtree_fnum, p->fnum);
166 DLIST_REMOVE(p->private->pipe_list, p);
172 open a file backend - used for MSRPC pipes
174 static NTSTATUS ipc_open_generic(struct ntvfs_module_context *ntvfs,
175 struct smbsrv_request *req, const char *fname,
176 struct pipe_state **ps)
178 struct pipe_state *p;
180 struct dcerpc_binding ep_description;
181 struct ipc_private *private = ntvfs->private_data;
183 struct stream_connection *srv_conn = req->smb_conn->connection;
185 if (!req->session || !req->session->session_info) {
186 return NT_STATUS_ACCESS_DENIED;
189 p = talloc(req, struct pipe_state);
190 NT_STATUS_HAVE_NO_MEMORY(p);
192 while (fname[0] == '\\') fname++;
194 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
195 NT_STATUS_HAVE_NO_MEMORY(p->pipe_name);
197 fnum = idr_get_new_above(private->idtree_fnum, p, IPC_BASE_FNUM, UINT16_MAX);
199 return NT_STATUS_TOO_MANY_OPENED_FILES;
203 p->ipc_state = 0x5ff;
206 we're all set, now ask the dcerpc server subsystem to open the
207 endpoint. At this stage the pipe isn't bound, so we don't
208 know what interface the user actually wants, just that they want
209 one of the interfaces attached to this pipe endpoint.
211 ep_description.transport = NCACN_NP;
212 ep_description.endpoint = p->pipe_name;
214 /* The session info is refcount-increased in the
215 * dcesrv_endpoint_search_connect() function
217 status = dcesrv_endpoint_search_connect(private->dcesrv,
220 req->session->session_info,
223 if (!NT_STATUS_IS_OK(status)) {
224 idr_remove(private->idtree_fnum, p->fnum);
228 DLIST_ADD(private->pipe_list, p);
230 p->smbpid = req->smbpid;
231 p->session = req->session;
232 p->private = private;
236 talloc_steal(private, p);
238 talloc_set_destructor(p, ipc_fd_destructor);
244 open a file with ntcreatex - used for MSRPC pipes
246 static NTSTATUS ipc_open_ntcreatex(struct ntvfs_module_context *ntvfs,
247 struct smbsrv_request *req, union smb_open *oi)
249 struct pipe_state *p;
252 status = ipc_open_generic(ntvfs, req, oi->ntcreatex.in.fname, &p);
253 if (!NT_STATUS_IS_OK(status)) {
257 ZERO_STRUCT(oi->ntcreatex.out);
258 oi->ntcreatex.out.fnum = p->fnum;
259 oi->ntcreatex.out.ipc_state = p->ipc_state;
260 oi->ntcreatex.out.file_type = FILE_TYPE_MESSAGE_MODE_PIPE;
266 open a file with openx - used for MSRPC pipes
268 static NTSTATUS ipc_open_openx(struct ntvfs_module_context *ntvfs,
269 struct smbsrv_request *req, union smb_open *oi)
271 struct pipe_state *p;
273 const char *fname = oi->openx.in.fname;
275 status = ipc_open_generic(ntvfs, req, fname, &p);
276 if (!NT_STATUS_IS_OK(status)) {
280 ZERO_STRUCT(oi->openx.out);
281 oi->openx.out.fnum = p->fnum;
282 oi->openx.out.ftype = 2;
283 oi->openx.out.devstate = p->ipc_state;
289 open a file - used for MSRPC pipes
291 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
292 struct smbsrv_request *req, union smb_open *oi)
296 switch (oi->generic.level) {
297 case RAW_OPEN_NTCREATEX:
298 status = ipc_open_ntcreatex(ntvfs, req, oi);
301 status = ipc_open_openx(ntvfs, req, oi);
304 status = NT_STATUS_NOT_SUPPORTED;
314 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
315 struct smbsrv_request *req, union smb_mkdir *md)
317 return NT_STATUS_ACCESS_DENIED;
323 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
324 struct smbsrv_request *req, struct smb_rmdir *rd)
326 return NT_STATUS_ACCESS_DENIED;
330 rename a set of files
332 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
333 struct smbsrv_request *req, union smb_rename *ren)
335 return NT_STATUS_ACCESS_DENIED;
341 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
342 struct smbsrv_request *req, struct smb_copy *cp)
344 return NT_STATUS_ACCESS_DENIED;
350 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
351 struct smbsrv_request *req, union smb_read *rd)
353 struct ipc_private *private = ntvfs->private_data;
356 struct pipe_state *p;
359 if (rd->generic.level != RAW_READ_GENERIC) {
360 return ntvfs_map_read(req, rd, ntvfs);
363 fnum = rd->readx.in.fnum;
365 p = pipe_state_find(private, fnum);
367 return NT_STATUS_INVALID_HANDLE;
370 data.length = rd->readx.in.maxcnt;
371 data.data = rd->readx.out.data;
372 if (data.length > UINT16_MAX) {
376 if (data.length != 0) {
377 status = dcesrv_output_blob(p->dce_conn, &data);
378 if (NT_STATUS_IS_ERR(status)) {
383 rd->readx.out.remaining = 0;
384 rd->readx.out.compaction_mode = 0;
385 rd->readx.out.nread = data.length;
393 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
394 struct smbsrv_request *req, union smb_write *wr)
396 struct ipc_private *private = ntvfs->private_data;
399 struct pipe_state *p;
402 if (wr->generic.level != RAW_WRITE_GENERIC) {
403 return ntvfs_map_write(req, wr, ntvfs);
406 fnum = wr->writex.in.fnum;
407 data.data = discard_const_p(void, wr->writex.in.data);
408 data.length = wr->writex.in.count;
410 p = pipe_state_find(private, fnum);
412 return NT_STATUS_INVALID_HANDLE;
415 status = dcesrv_input(p->dce_conn, &data);
416 if (!NT_STATUS_IS_OK(status)) {
420 wr->writex.out.nwritten = data.length;
421 wr->writex.out.remaining = 0;
429 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
430 struct smbsrv_request *req, struct smb_seek *io)
432 return NT_STATUS_ACCESS_DENIED;
438 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
439 struct smbsrv_request *req, struct smb_flush *io)
441 return NT_STATUS_ACCESS_DENIED;
447 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
448 struct smbsrv_request *req, union smb_close *io)
450 struct ipc_private *private = ntvfs->private_data;
451 struct pipe_state *p;
453 if (io->generic.level != RAW_CLOSE_CLOSE) {
454 return ntvfs_map_close(req, io, ntvfs);
457 p = pipe_state_find(private, io->close.in.fnum);
459 return NT_STATUS_INVALID_HANDLE;
470 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
471 struct smbsrv_request *req)
473 struct ipc_private *private = ntvfs->private_data;
474 struct pipe_state *p, *next;
476 for (p=private->pipe_list; p; p=next) {
478 if (p->smbpid == req->smbpid) {
487 logoff - closing files open by the user
489 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
490 struct smbsrv_request *req)
492 struct ipc_private *private = ntvfs->private_data;
493 struct pipe_state *p, *next;
495 for (p=private->pipe_list; p; p=next) {
497 if (p->session == req->session) {
506 setup for an async call
508 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
509 struct smbsrv_request *req,
518 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
519 struct smbsrv_request *req)
521 return NT_STATUS_UNSUCCESSFUL;
527 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
528 struct smbsrv_request *req, union smb_lock *lck)
530 return NT_STATUS_ACCESS_DENIED;
534 set info on a open file
536 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
537 struct smbsrv_request *req, union smb_setfileinfo *info)
539 return NT_STATUS_ACCESS_DENIED;
543 query info on a open file
545 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
546 struct smbsrv_request *req, union smb_fileinfo *info)
548 return NT_STATUS_ACCESS_DENIED;
553 return filesystem info
555 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
556 struct smbsrv_request *req, union smb_fsinfo *fs)
558 return NT_STATUS_ACCESS_DENIED;
562 return print queue info
564 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
565 struct smbsrv_request *req, union smb_lpq *lpq)
567 return NT_STATUS_ACCESS_DENIED;
571 list files in a directory matching a wildcard pattern
573 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
574 struct smbsrv_request *req, union smb_search_first *io,
575 void *search_private,
576 BOOL (*callback)(void *, union smb_search_data *))
578 return NT_STATUS_ACCESS_DENIED;
582 continue listing files in a directory
584 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
585 struct smbsrv_request *req, union smb_search_next *io,
586 void *search_private,
587 BOOL (*callback)(void *, union smb_search_data *))
589 return NT_STATUS_ACCESS_DENIED;
593 end listing files in a directory
595 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
596 struct smbsrv_request *req, union smb_search_close *io)
598 return NT_STATUS_ACCESS_DENIED;
602 /* SMBtrans - handle a DCERPC command */
603 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
604 struct smbsrv_request *req, struct smb_trans2 *trans)
606 struct pipe_state *p;
607 struct ipc_private *private = ntvfs->private_data;
610 /* the fnum is in setup[1] */
611 p = pipe_state_find(private, trans->in.setup[1]);
613 return NT_STATUS_INVALID_HANDLE;
616 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
617 if (!trans->out.data.data) {
618 return NT_STATUS_NO_MEMORY;
621 /* pass the data to the dcerpc server. Note that we don't
622 expect this to fail, and things like NDR faults are not
623 reported at this stage. Those sorts of errors happen in the
624 dcesrv_output stage */
625 status = dcesrv_input(p->dce_conn, &trans->in.data);
626 if (!NT_STATUS_IS_OK(status)) {
631 now ask the dcerpc system for some output. This doesn't yet handle
632 async calls. Again, we only expect NT_STATUS_OK. If the call fails then
633 the error is encoded at the dcerpc level
635 status = dcesrv_output_blob(p->dce_conn, &trans->out.data);
636 if (NT_STATUS_IS_ERR(status)) {
640 trans->out.setup_count = 0;
641 trans->out.setup = NULL;
642 trans->out.params = data_blob(NULL, 0);
648 /* SMBtrans - set named pipe state */
649 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
650 struct smbsrv_request *req, struct smb_trans2 *trans)
652 struct ipc_private *private = ntvfs->private_data;
653 struct pipe_state *p;
655 /* the fnum is in setup[1] */
656 p = pipe_state_find(private, trans->in.setup[1]);
658 return NT_STATUS_INVALID_HANDLE;
661 if (trans->in.params.length != 2) {
662 return NT_STATUS_INVALID_PARAMETER;
664 p->ipc_state = SVAL(trans->in.params.data, 0);
666 trans->out.setup_count = 0;
667 trans->out.setup = NULL;
668 trans->out.params = data_blob(NULL, 0);
669 trans->out.data = data_blob(NULL, 0);
675 /* SMBtrans - used to provide access to SMB pipes */
676 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
677 struct smbsrv_request *req, struct smb_trans2 *trans)
681 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
682 return ipc_rap_call(req, trans);
684 if (trans->in.setup_count != 2) {
685 return NT_STATUS_INVALID_PARAMETER;
688 switch (trans->in.setup[0]) {
689 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
690 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
692 case TRANSACT_DCERPCCMD:
693 status = ipc_dcerpc_cmd(ntvfs, req, trans);
696 status = NT_STATUS_INVALID_PARAMETER;
706 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
708 NTSTATUS ntvfs_ipc_init(void)
711 struct ntvfs_ops ops;
715 /* fill in the name and type */
716 ops.name = "default";
717 ops.type = NTVFS_IPC;
719 /* fill in all the operations */
720 ops.connect = ipc_connect;
721 ops.disconnect = ipc_disconnect;
722 ops.unlink = ipc_unlink;
723 ops.chkpath = ipc_chkpath;
724 ops.qpathinfo = ipc_qpathinfo;
725 ops.setpathinfo = ipc_setpathinfo;
726 ops.openfile = ipc_open;
727 ops.mkdir = ipc_mkdir;
728 ops.rmdir = ipc_rmdir;
729 ops.rename = ipc_rename;
731 ops.ioctl = ipc_ioctl;
733 ops.write = ipc_write;
735 ops.flush = ipc_flush;
736 ops.close = ipc_close;
739 ops.setfileinfo = ipc_setfileinfo;
740 ops.qfileinfo = ipc_qfileinfo;
741 ops.fsinfo = ipc_fsinfo;
743 ops.search_first = ipc_search_first;
744 ops.search_next = ipc_search_next;
745 ops.search_close = ipc_search_close;
746 ops.trans = ipc_trans;
747 ops.logoff = ipc_logoff;
748 ops.async_setup = ipc_async_setup;
749 ops.cancel = ipc_cancel;
751 /* register ourselves with the NTVFS subsystem. */
752 ret = ntvfs_register(&ops);
754 if (!NT_STATUS_IS_OK(ret)) {
755 DEBUG(0,("Failed to register IPC backend!\n"));