2 Unix SMB/CIFS implementation.
3 default IPC$ NTVFS backend
5 Copyright (C) Andrew Tridgell 2003
6 Copyright (C) Stefan (metze) Metzmacher 2004
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 /* this is the private structure used to keep the state of an open
34 ipc$ connection. It needs to keep information about all open
37 struct idr_context *idtree_fnum;
39 /* a list of open pipes */
41 struct pipe_state *next, *prev;
42 struct ipc_private *private;
43 const char *pipe_name;
45 struct dcesrv_connection *dce_conn;
47 /* we need to remember the session it was opened on,
48 as it is illegal to operate on someone elses fnum */
49 struct smbsrv_session *session;
51 /* we need to remember the client pid that
52 opened the file so SMBexit works */
60 find a open pipe give a file descriptor
62 static struct pipe_state *pipe_state_find(struct ipc_private *private, uint16_t fnum)
64 return idr_find(private->idtree_fnum, fnum);
69 connect to a share - always works
71 static NTSTATUS ipc_connect(struct ntvfs_module_context *ntvfs,
72 struct smbsrv_request *req, const char *sharename)
74 struct smbsrv_tcon *tcon = req->tcon;
75 struct ipc_private *private;
77 tcon->fs_type = talloc_strdup(tcon, "IPC");
78 tcon->dev_type = talloc_strdup(tcon, "IPC");
80 /* prepare the private state for this connection */
81 private = talloc_p(tcon, struct ipc_private);
83 return NT_STATUS_NO_MEMORY;
85 ntvfs->private_data = private;
87 private->pipe_list = NULL;
89 private->idtree_fnum = idr_init(private);
90 if (private->idtree_fnum == NULL) {
91 return NT_STATUS_NO_MEMORY;
98 disconnect from a share
100 static NTSTATUS ipc_disconnect(struct ntvfs_module_context *ntvfs,
101 struct smbsrv_tcon *tcon)
103 struct ipc_private *private = ntvfs->private_data;
105 /* close any pipes that are open. Discard any unread data */
106 while (private->pipe_list) {
107 talloc_free(private->pipe_list);
116 static NTSTATUS ipc_unlink(struct ntvfs_module_context *ntvfs,
117 struct smbsrv_request *req, struct smb_unlink *unl)
119 return NT_STATUS_ACCESS_DENIED;
124 ioctl interface - we don't do any
126 static NTSTATUS ipc_ioctl(struct ntvfs_module_context *ntvfs,
127 struct smbsrv_request *req, union smb_ioctl *io)
129 return NT_STATUS_ACCESS_DENIED;
133 check if a directory exists
135 static NTSTATUS ipc_chkpath(struct ntvfs_module_context *ntvfs,
136 struct smbsrv_request *req, struct smb_chkpath *cp)
138 return NT_STATUS_ACCESS_DENIED;
142 return info on a pathname
144 static NTSTATUS ipc_qpathinfo(struct ntvfs_module_context *ntvfs,
145 struct smbsrv_request *req, union smb_fileinfo *info)
147 return NT_STATUS_ACCESS_DENIED;
151 set info on a pathname
153 static NTSTATUS ipc_setpathinfo(struct ntvfs_module_context *ntvfs,
154 struct smbsrv_request *req, union smb_setfileinfo *st)
156 return NT_STATUS_ACCESS_DENIED;
161 destroy a open pipe structure
163 static int ipc_fd_destructor(void *ptr)
165 struct pipe_state *p = ptr;
166 idr_remove(p->private->idtree_fnum, p->fnum);
167 DLIST_REMOVE(p->private->pipe_list, p);
168 talloc_free(p->dce_conn);
174 open a file backend - used for MSRPC pipes
176 static NTSTATUS ipc_open_generic(struct ntvfs_module_context *ntvfs,
177 struct smbsrv_request *req, const char *fname,
178 struct pipe_state **ps)
180 struct pipe_state *p;
182 struct dcerpc_binding ep_description;
183 struct auth_session_info *session_info = NULL;
184 struct ipc_private *private = ntvfs->private_data;
187 p = talloc_p(req, struct pipe_state);
189 return NT_STATUS_NO_MEMORY;
192 while (fname[0] == '\\') fname++;
194 p->pipe_name = talloc_asprintf(p, "\\pipe\\%s", fname);
196 return NT_STATUS_NO_MEMORY;
199 fnum = idr_get_new(private->idtree_fnum, p, UINT16_MAX);
201 return NT_STATUS_TOO_MANY_OPENED_FILES;
205 p->ipc_state = 0x5ff;
208 we're all set, now ask the dcerpc server subsystem to open the
209 endpoint. At this stage the pipe isn't bound, so we don't
210 know what interface the user actually wants, just that they want
211 one of the interfaces attached to this pipe endpoint.
213 TODO: note that we aren't passing any credentials here. We
214 will need to do that once the credentials infrastructure is
217 ep_description.transport = NCACN_NP;
218 ep_description.endpoint = p->pipe_name;
220 /* tell the RPC layer the session_info */
222 /* The session info is refcount-increased in the
223 dcesrv_endpoint_search_connect() function */
224 session_info = req->session->session_info;
227 status = dcesrv_endpoint_search_connect(req->smb_conn->dcesrv,
231 if (!NT_STATUS_IS_OK(status)) {
232 idr_remove(private->idtree_fnum, p->fnum);
236 DLIST_ADD(private->pipe_list, p);
238 p->smbpid = req->smbpid;
239 p->session = req->session;
240 p->private = private;
244 talloc_steal(private, p);
246 talloc_set_destructor(p, ipc_fd_destructor);
252 open a file with ntcreatex - used for MSRPC pipes
254 static NTSTATUS ipc_open_ntcreatex(struct ntvfs_module_context *ntvfs,
255 struct smbsrv_request *req, union smb_open *oi)
257 struct pipe_state *p;
260 status = ipc_open_generic(ntvfs, req, oi->ntcreatex.in.fname, &p);
261 if (!NT_STATUS_IS_OK(status)) {
265 ZERO_STRUCT(oi->ntcreatex.out);
266 oi->ntcreatex.out.fnum = p->fnum;
267 oi->ntcreatex.out.ipc_state = p->ipc_state;
273 open a file with openx - used for MSRPC pipes
275 static NTSTATUS ipc_open_openx(struct ntvfs_module_context *ntvfs,
276 struct smbsrv_request *req, union smb_open *oi)
278 struct pipe_state *p;
280 const char *fname = oi->openx.in.fname;
282 status = ipc_open_generic(ntvfs, req, fname, &p);
283 if (!NT_STATUS_IS_OK(status)) {
287 ZERO_STRUCT(oi->openx.out);
288 oi->openx.out.fnum = p->fnum;
289 oi->openx.out.ftype = 2;
290 oi->openx.out.devstate = p->ipc_state;
296 open a file - used for MSRPC pipes
298 static NTSTATUS ipc_open(struct ntvfs_module_context *ntvfs,
299 struct smbsrv_request *req, union smb_open *oi)
303 switch (oi->generic.level) {
304 case RAW_OPEN_NTCREATEX:
305 status = ipc_open_ntcreatex(ntvfs, req, oi);
308 status = ipc_open_openx(ntvfs, req, oi);
311 status = NT_STATUS_NOT_SUPPORTED;
321 static NTSTATUS ipc_mkdir(struct ntvfs_module_context *ntvfs,
322 struct smbsrv_request *req, union smb_mkdir *md)
324 return NT_STATUS_ACCESS_DENIED;
330 static NTSTATUS ipc_rmdir(struct ntvfs_module_context *ntvfs,
331 struct smbsrv_request *req, struct smb_rmdir *rd)
333 return NT_STATUS_ACCESS_DENIED;
337 rename a set of files
339 static NTSTATUS ipc_rename(struct ntvfs_module_context *ntvfs,
340 struct smbsrv_request *req, union smb_rename *ren)
342 return NT_STATUS_ACCESS_DENIED;
348 static NTSTATUS ipc_copy(struct ntvfs_module_context *ntvfs,
349 struct smbsrv_request *req, struct smb_copy *cp)
351 return NT_STATUS_ACCESS_DENIED;
357 static NTSTATUS ipc_read(struct ntvfs_module_context *ntvfs,
358 struct smbsrv_request *req, union smb_read *rd)
360 struct ipc_private *private = ntvfs->private_data;
363 struct pipe_state *p;
366 if (rd->generic.level != RAW_READ_GENERIC) {
367 return ntvfs_map_read(req, rd, ntvfs);
370 fnum = rd->readx.in.fnum;
372 p = pipe_state_find(private, fnum);
374 return NT_STATUS_INVALID_HANDLE;
377 data.length = rd->readx.in.maxcnt;
378 data.data = rd->readx.out.data;
379 if (data.length > UINT16_MAX) {
383 if (data.length != 0) {
384 status = dcesrv_output_blob(p->dce_conn, &data);
385 if (NT_STATUS_IS_ERR(status)) {
390 rd->readx.out.remaining = 0;
391 rd->readx.out.compaction_mode = 0;
392 rd->readx.out.nread = data.length;
400 static NTSTATUS ipc_write(struct ntvfs_module_context *ntvfs,
401 struct smbsrv_request *req, union smb_write *wr)
403 struct ipc_private *private = ntvfs->private_data;
406 struct pipe_state *p;
409 if (wr->generic.level != RAW_WRITE_GENERIC) {
410 return ntvfs_map_write(req, wr, ntvfs);
413 fnum = wr->writex.in.fnum;
414 data.data = discard_const_p(void, wr->writex.in.data);
415 data.length = wr->writex.in.count;
417 p = pipe_state_find(private, fnum);
419 return NT_STATUS_INVALID_HANDLE;
422 status = dcesrv_input(p->dce_conn, &data);
423 if (!NT_STATUS_IS_OK(status)) {
427 wr->writex.out.nwritten = data.length;
428 wr->writex.out.remaining = 0;
436 static NTSTATUS ipc_seek(struct ntvfs_module_context *ntvfs,
437 struct smbsrv_request *req, struct smb_seek *io)
439 return NT_STATUS_ACCESS_DENIED;
445 static NTSTATUS ipc_flush(struct ntvfs_module_context *ntvfs,
446 struct smbsrv_request *req, struct smb_flush *io)
448 return NT_STATUS_ACCESS_DENIED;
454 static NTSTATUS ipc_close(struct ntvfs_module_context *ntvfs,
455 struct smbsrv_request *req, union smb_close *io)
457 struct ipc_private *private = ntvfs->private_data;
458 struct pipe_state *p;
460 if (io->generic.level != RAW_CLOSE_CLOSE) {
461 return ntvfs_map_close(req, io, ntvfs);
464 p = pipe_state_find(private, io->close.in.fnum);
466 return NT_STATUS_INVALID_HANDLE;
477 static NTSTATUS ipc_exit(struct ntvfs_module_context *ntvfs,
478 struct smbsrv_request *req)
480 struct ipc_private *private = ntvfs->private_data;
481 struct pipe_state *p, *next;
483 for (p=private->pipe_list; p; p=next) {
485 if (p->smbpid == req->smbpid) {
494 logoff - closing files open by the user
496 static NTSTATUS ipc_logoff(struct ntvfs_module_context *ntvfs,
497 struct smbsrv_request *req)
499 struct ipc_private *private = ntvfs->private_data;
500 struct pipe_state *p, *next;
502 for (p=private->pipe_list; p; p=next) {
504 if (p->session == req->session) {
513 setup for an async call
515 static NTSTATUS ipc_async_setup(struct ntvfs_module_context *ntvfs,
516 struct smbsrv_request *req,
525 static NTSTATUS ipc_cancel(struct ntvfs_module_context *ntvfs,
526 struct smbsrv_request *req)
528 return NT_STATUS_UNSUCCESSFUL;
534 static NTSTATUS ipc_lock(struct ntvfs_module_context *ntvfs,
535 struct smbsrv_request *req, union smb_lock *lck)
537 return NT_STATUS_ACCESS_DENIED;
541 set info on a open file
543 static NTSTATUS ipc_setfileinfo(struct ntvfs_module_context *ntvfs,
544 struct smbsrv_request *req, union smb_setfileinfo *info)
546 return NT_STATUS_ACCESS_DENIED;
550 query info on a open file
552 static NTSTATUS ipc_qfileinfo(struct ntvfs_module_context *ntvfs,
553 struct smbsrv_request *req, union smb_fileinfo *info)
555 return NT_STATUS_ACCESS_DENIED;
560 return filesystem info
562 static NTSTATUS ipc_fsinfo(struct ntvfs_module_context *ntvfs,
563 struct smbsrv_request *req, union smb_fsinfo *fs)
565 return NT_STATUS_ACCESS_DENIED;
569 return print queue info
571 static NTSTATUS ipc_lpq(struct ntvfs_module_context *ntvfs,
572 struct smbsrv_request *req, union smb_lpq *lpq)
574 return NT_STATUS_ACCESS_DENIED;
578 list files in a directory matching a wildcard pattern
580 static NTSTATUS ipc_search_first(struct ntvfs_module_context *ntvfs,
581 struct smbsrv_request *req, union smb_search_first *io,
582 void *search_private,
583 BOOL (*callback)(void *, union smb_search_data *))
585 return NT_STATUS_ACCESS_DENIED;
589 continue listing files in a directory
591 static NTSTATUS ipc_search_next(struct ntvfs_module_context *ntvfs,
592 struct smbsrv_request *req, union smb_search_next *io,
593 void *search_private,
594 BOOL (*callback)(void *, union smb_search_data *))
596 return NT_STATUS_ACCESS_DENIED;
600 end listing files in a directory
602 static NTSTATUS ipc_search_close(struct ntvfs_module_context *ntvfs,
603 struct smbsrv_request *req, union smb_search_close *io)
605 return NT_STATUS_ACCESS_DENIED;
609 /* SMBtrans - handle a DCERPC command */
610 static NTSTATUS ipc_dcerpc_cmd(struct ntvfs_module_context *ntvfs,
611 struct smbsrv_request *req, struct smb_trans2 *trans)
613 struct pipe_state *p;
614 struct ipc_private *private = ntvfs->private_data;
617 /* the fnum is in setup[1] */
618 p = pipe_state_find(private, trans->in.setup[1]);
620 return NT_STATUS_INVALID_HANDLE;
623 trans->out.data = data_blob_talloc(req, NULL, trans->in.max_data);
624 if (!trans->out.data.data) {
625 return NT_STATUS_NO_MEMORY;
628 /* pass the data to the dcerpc server. Note that we don't
629 expect this to fail, and things like NDR faults are not
630 reported at this stage. Those sorts of errors happen in the
631 dcesrv_output stage */
632 status = dcesrv_input(p->dce_conn, &trans->in.data);
633 if (!NT_STATUS_IS_OK(status)) {
638 now ask the dcerpc system for some output. This doesn't yet handle
639 async calls. Again, we only expect NT_STATUS_OK. If the call fails then
640 the error is encoded at the dcerpc level
642 status = dcesrv_output_blob(p->dce_conn, &trans->out.data);
643 if (NT_STATUS_IS_ERR(status)) {
647 trans->out.setup_count = 0;
648 trans->out.setup = NULL;
649 trans->out.params = data_blob(NULL, 0);
655 /* SMBtrans - set named pipe state */
656 static NTSTATUS ipc_set_nm_pipe_state(struct ntvfs_module_context *ntvfs,
657 struct smbsrv_request *req, struct smb_trans2 *trans)
659 struct ipc_private *private = ntvfs->private_data;
660 struct pipe_state *p;
662 /* the fnum is in setup[1] */
663 p = pipe_state_find(private, trans->in.setup[1]);
665 return NT_STATUS_INVALID_HANDLE;
668 if (trans->in.params.length != 2) {
669 return NT_STATUS_INVALID_PARAMETER;
671 p->ipc_state = SVAL(trans->in.params.data, 0);
673 trans->out.setup_count = 0;
674 trans->out.setup = NULL;
675 trans->out.params = data_blob(NULL, 0);
676 trans->out.data = data_blob(NULL, 0);
682 /* SMBtrans - used to provide access to SMB pipes */
683 static NTSTATUS ipc_trans(struct ntvfs_module_context *ntvfs,
684 struct smbsrv_request *req, struct smb_trans2 *trans)
688 if (strequal(trans->in.trans_name, "\\PIPE\\LANMAN"))
689 return ipc_rap_call(req, trans);
691 if (trans->in.setup_count != 2) {
692 return NT_STATUS_INVALID_PARAMETER;
695 switch (trans->in.setup[0]) {
696 case TRANSACT_SETNAMEDPIPEHANDLESTATE:
697 status = ipc_set_nm_pipe_state(ntvfs, req, trans);
699 case TRANSACT_DCERPCCMD:
700 status = ipc_dcerpc_cmd(ntvfs, req, trans);
703 status = NT_STATUS_INVALID_PARAMETER;
713 initialialise the IPC backend, registering ourselves with the ntvfs subsystem
715 NTSTATUS ntvfs_ipc_init(void)
718 struct ntvfs_ops ops;
722 /* fill in the name and type */
723 ops.name = "default";
724 ops.type = NTVFS_IPC;
726 /* fill in all the operations */
727 ops.connect = ipc_connect;
728 ops.disconnect = ipc_disconnect;
729 ops.unlink = ipc_unlink;
730 ops.chkpath = ipc_chkpath;
731 ops.qpathinfo = ipc_qpathinfo;
732 ops.setpathinfo = ipc_setpathinfo;
733 ops.openfile = ipc_open;
734 ops.mkdir = ipc_mkdir;
735 ops.rmdir = ipc_rmdir;
736 ops.rename = ipc_rename;
738 ops.ioctl = ipc_ioctl;
740 ops.write = ipc_write;
742 ops.flush = ipc_flush;
743 ops.close = ipc_close;
746 ops.setfileinfo = ipc_setfileinfo;
747 ops.qfileinfo = ipc_qfileinfo;
748 ops.fsinfo = ipc_fsinfo;
750 ops.search_first = ipc_search_first;
751 ops.search_next = ipc_search_next;
752 ops.search_close = ipc_search_close;
753 ops.trans = ipc_trans;
754 ops.logoff = ipc_logoff;
755 ops.async_setup = ipc_async_setup;
756 ops.cancel = ipc_cancel;
758 /* register ourselves with the NTVFS subsystem. */
759 ret = ntvfs_register(&ops);
761 if (!NT_STATUS_IS_OK(ret)) {
762 DEBUG(0,("Failed to register IPC backend!\n"));