2 Unix SMB/CIFS implementation.
4 CIFS-on-CIFS NTVFS filesystem backend
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) James J Myers 2003 <myersjj@samba.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 this implements a CIFS->CIFS NTVFS filesystem backend.
30 #include "libcli/raw/libcliraw.h"
31 #include "libcli/composite/composite.h"
32 #include "smb_server/smb_server.h"
33 #include "smbd/service_stream.h"
35 /* this is stored in ntvfs_private */
37 struct smbcli_tree *tree;
38 struct smbcli_transport *transport;
39 struct smbsrv_tcon *tcon;
44 /* a structure used to pass information to an async handler */
46 struct smbsrv_request *req;
50 #define SETUP_PID private->tree->session->pid = SVAL(req->in.hdr, HDR_PID)
53 a handler for oplock break events from the server - these need to be passed
56 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *p_private)
58 struct cvfs_private *private = p_private;
60 DEBUG(5,("vfs_cifs: sending oplock break level %d for fnum %d\n", level, fnum));
61 return req_send_oplock_break(private->tcon, fnum, level);
65 a handler for read events on a connection to a backend server
67 static void cifs_socket_handler(struct event_context *ev, struct fd_event *fde,
68 struct timeval t, uint16_t flags)
70 struct cvfs_private *private = fde->private;
71 struct smbsrv_tcon *tcon = private->tcon;
73 DEBUG(5,("cifs_socket_handler event on fd %d\n", fde->fd));
75 if (!smbcli_transport_process(private->transport)) {
76 /* the connection to our server is dead */
82 connect to a share - used when a tree_connect operation comes in.
84 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs,
85 struct smbsrv_request *req, const char *sharename)
87 struct smbsrv_tcon *tcon = req->tcon;
89 struct cvfs_private *private;
90 const char *host, *user, *pass, *domain, *remote_share;
91 struct smb_composite_connect io;
92 struct composite_context *creq;
94 /* Here we need to determine which server to connect to.
95 * For now we use parametric options, type cifs.
96 * Later we will use security=server and auth_server.c.
98 host = lp_parm_string(req->tcon->service, "cifs", "server");
99 user = lp_parm_string(req->tcon->service, "cifs", "user");
100 pass = lp_parm_string(req->tcon->service, "cifs", "password");
101 domain = lp_parm_string(req->tcon->service, "cifs", "domain");
102 remote_share = lp_parm_string(req->tcon->service, "cifs", "share");
104 remote_share = sharename;
107 if (!host || !user || !pass || !domain) {
108 DEBUG(1,("CIFS backend: You must supply server, user, password and domain\n"));
109 return NT_STATUS_INVALID_PARAMETER;
112 private = talloc(req->tcon, struct cvfs_private);
114 return NT_STATUS_NO_MEMORY;
116 ZERO_STRUCTP(private);
118 ntvfs->private_data = private;
120 /* connect to the server, using the smbd event context */
121 io.in.dest_host = host;
123 io.in.called_name = host;
124 io.in.calling_name = "vfs_cifs";
125 io.in.service = remote_share;
126 io.in.service_type = "?????";
127 io.in.domain = domain;
129 io.in.password = pass;
131 creq = smb_composite_connect_send(&io, tcon->smb_conn->connection->event.ctx);
132 status = smb_composite_connect_recv(creq, private);
133 NT_STATUS_NOT_OK_RETURN(status);
135 private->tree = io.out.tree;
137 private->transport = private->tree->session->transport;
139 private->tcon = req->tcon;
141 tcon->fs_type = talloc_strdup(tcon, "NTFS");
142 tcon->dev_type = talloc_strdup(tcon, "A:");
144 /* we need to receive oplock break requests from the server */
145 smbcli_oplock_handler(private->transport, oplock_handler, private);
147 private->transport->socket->event.fde->handler = cifs_socket_handler;
148 private->transport->socket->event.fde->private = private;
150 private->map_generic = lp_parm_bool(req->tcon->service,
151 "cifs", "mapgeneric", False);
157 disconnect from a share
159 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs,
160 struct smbsrv_tcon *tcon)
162 struct cvfs_private *private = ntvfs->private_data;
164 talloc_free(private);
170 a handler for simple async replies
171 this handler can only be used for functions that don't return any
172 parameters (those that just return a status code)
174 static void async_simple(struct smbcli_request *c_req)
176 struct async_info *async = c_req->async.private;
177 struct smbsrv_request *req = async->req;
178 req->async_states->status = smbcli_request_simple_recv(c_req);
179 req->async_states->send_fn(req);
183 /* save some typing for the simple functions */
184 #define ASYNC_RECV_TAIL(io, async_fn) do { \
185 if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
187 struct async_info *async; \
188 async = talloc(req, struct async_info); \
189 if (!async) return NT_STATUS_NO_MEMORY; \
192 c_req->async.private = async; \
194 c_req->async.fn = async_fn; \
195 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
196 return NT_STATUS_OK; \
199 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
202 delete a file - the dirtype specifies the file types to include in the search.
203 The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
205 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs,
206 struct smbsrv_request *req, struct smb_unlink *unl)
208 struct cvfs_private *private = ntvfs->private_data;
209 struct smbcli_request *c_req;
213 /* see if the front end will allow us to perform this
214 function asynchronously. */
215 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
216 return smb_raw_unlink(private->tree, unl);
219 c_req = smb_raw_unlink_send(private->tree, unl);
225 a handler for async ioctl replies
227 static void async_ioctl(struct smbcli_request *c_req)
229 struct async_info *async = c_req->async.private;
230 struct smbsrv_request *req = async->req;
231 req->async_states->status = smb_raw_ioctl_recv(c_req, req, async->parms);
232 req->async_states->send_fn(req);
238 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs,
239 struct smbsrv_request *req, union smb_ioctl *io)
241 struct cvfs_private *private = ntvfs->private_data;
242 struct smbcli_request *c_req;
246 /* see if the front end will allow us to perform this
247 function asynchronously. */
248 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
249 return smb_raw_ioctl(private->tree, req, io);
252 c_req = smb_raw_ioctl_send(private->tree, io);
254 ASYNC_RECV_TAIL(io, async_ioctl);
258 check if a directory exists
260 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs,
261 struct smbsrv_request *req, struct smb_chkpath *cp)
263 struct cvfs_private *private = ntvfs->private_data;
264 struct smbcli_request *c_req;
268 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
269 return smb_raw_chkpath(private->tree, cp);
272 c_req = smb_raw_chkpath_send(private->tree, cp);
278 a handler for async qpathinfo replies
280 static void async_qpathinfo(struct smbcli_request *c_req)
282 struct async_info *async = c_req->async.private;
283 struct smbsrv_request *req = async->req;
284 req->async_states->status = smb_raw_pathinfo_recv(c_req, req, async->parms);
285 req->async_states->send_fn(req);
289 return info on a pathname
291 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs,
292 struct smbsrv_request *req, union smb_fileinfo *info)
294 struct cvfs_private *private = ntvfs->private_data;
295 struct smbcli_request *c_req;
299 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
300 return smb_raw_pathinfo(private->tree, req, info);
303 c_req = smb_raw_pathinfo_send(private->tree, info);
305 ASYNC_RECV_TAIL(info, async_qpathinfo);
309 a handler for async qfileinfo replies
311 static void async_qfileinfo(struct smbcli_request *c_req)
313 struct async_info *async = c_req->async.private;
314 struct smbsrv_request *req = async->req;
315 req->async_states->status = smb_raw_fileinfo_recv(c_req, req, async->parms);
316 req->async_states->send_fn(req);
320 query info on a open file
322 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs,
323 struct smbsrv_request *req, union smb_fileinfo *info)
325 struct cvfs_private *private = ntvfs->private_data;
326 struct smbcli_request *c_req;
330 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
331 return smb_raw_fileinfo(private->tree, req, info);
334 c_req = smb_raw_fileinfo_send(private->tree, info);
336 ASYNC_RECV_TAIL(info, async_qfileinfo);
341 set info on a pathname
343 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
344 struct smbsrv_request *req, union smb_setfileinfo *st)
346 struct cvfs_private *private = ntvfs->private_data;
347 struct smbcli_request *c_req;
351 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
352 return smb_raw_setpathinfo(private->tree, st);
355 c_req = smb_raw_setpathinfo_send(private->tree, st);
362 a handler for async open replies
364 static void async_open(struct smbcli_request *c_req)
366 struct async_info *async = c_req->async.private;
367 struct smbsrv_request *req = async->req;
368 req->async_states->status = smb_raw_open_recv(c_req, req, async->parms);
369 req->async_states->send_fn(req);
375 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs,
376 struct smbsrv_request *req, union smb_open *io)
378 struct cvfs_private *private = ntvfs->private_data;
379 struct smbcli_request *c_req;
383 if (io->generic.level != RAW_OPEN_GENERIC &&
384 private->map_generic) {
385 return ntvfs_map_open(req, io, ntvfs);
388 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
389 return smb_raw_open(private->tree, req, io);
392 c_req = smb_raw_open_send(private->tree, io);
394 ASYNC_RECV_TAIL(io, async_open);
400 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs,
401 struct smbsrv_request *req, union smb_mkdir *md)
403 struct cvfs_private *private = ntvfs->private_data;
404 struct smbcli_request *c_req;
408 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
409 return smb_raw_mkdir(private->tree, md);
412 c_req = smb_raw_mkdir_send(private->tree, md);
420 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs,
421 struct smbsrv_request *req, struct smb_rmdir *rd)
423 struct cvfs_private *private = ntvfs->private_data;
424 struct smbcli_request *c_req;
428 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
429 return smb_raw_rmdir(private->tree, rd);
431 c_req = smb_raw_rmdir_send(private->tree, rd);
437 rename a set of files
439 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs,
440 struct smbsrv_request *req, union smb_rename *ren)
442 struct cvfs_private *private = ntvfs->private_data;
443 struct smbcli_request *c_req;
447 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
448 return smb_raw_rename(private->tree, ren);
451 c_req = smb_raw_rename_send(private->tree, ren);
459 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs,
460 struct smbsrv_request *req, struct smb_copy *cp)
462 return NT_STATUS_NOT_SUPPORTED;
466 a handler for async read replies
468 static void async_read(struct smbcli_request *c_req)
470 struct async_info *async = c_req->async.private;
471 struct smbsrv_request *req = async->req;
472 req->async_states->status = smb_raw_read_recv(c_req, async->parms);
473 req->async_states->send_fn(req);
479 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs,
480 struct smbsrv_request *req, union smb_read *rd)
482 struct cvfs_private *private = ntvfs->private_data;
483 struct smbcli_request *c_req;
487 if (rd->generic.level != RAW_READ_GENERIC &&
488 private->map_generic) {
489 return ntvfs_map_read(req, rd, ntvfs);
492 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
493 return smb_raw_read(private->tree, rd);
496 c_req = smb_raw_read_send(private->tree, rd);
498 ASYNC_RECV_TAIL(rd, async_read);
502 a handler for async write replies
504 static void async_write(struct smbcli_request *c_req)
506 struct async_info *async = c_req->async.private;
507 struct smbsrv_request *req = async->req;
508 req->async_states->status = smb_raw_write_recv(c_req, async->parms);
509 req->async_states->send_fn(req);
515 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs,
516 struct smbsrv_request *req, union smb_write *wr)
518 struct cvfs_private *private = ntvfs->private_data;
519 struct smbcli_request *c_req;
523 if (wr->generic.level != RAW_WRITE_GENERIC &&
524 private->map_generic) {
525 return ntvfs_map_write(req, wr, ntvfs);
528 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
529 return smb_raw_write(private->tree, wr);
532 c_req = smb_raw_write_send(private->tree, wr);
534 ASYNC_RECV_TAIL(wr, async_write);
540 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs,
541 struct smbsrv_request *req, struct smb_seek *io)
543 struct cvfs_private *private = ntvfs->private_data;
544 struct smbcli_request *c_req;
548 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
549 return smb_raw_seek(private->tree, io);
552 c_req = smb_raw_seek_send(private->tree, io);
560 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs,
561 struct smbsrv_request *req, struct smb_flush *io)
563 struct cvfs_private *private = ntvfs->private_data;
564 struct smbcli_request *c_req;
568 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
569 return smb_raw_flush(private->tree, io);
572 c_req = smb_raw_flush_send(private->tree, io);
580 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs,
581 struct smbsrv_request *req, union smb_close *io)
583 struct cvfs_private *private = ntvfs->private_data;
584 struct smbcli_request *c_req;
588 if (io->generic.level != RAW_CLOSE_GENERIC &&
589 private->map_generic) {
590 return ntvfs_map_close(req, io, ntvfs);
593 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
594 return smb_raw_close(private->tree, io);
597 c_req = smb_raw_close_send(private->tree, io);
603 exit - closing files open by the pid
605 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs,
606 struct smbsrv_request *req)
608 struct cvfs_private *private = ntvfs->private_data;
609 struct smbcli_request *c_req;
613 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
614 return smb_raw_exit(private->tree->session);
617 c_req = smb_raw_exit_send(private->tree->session);
623 logoff - closing files open by the user
625 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs,
626 struct smbsrv_request *req)
628 /* we can't do this right in the cifs backend .... */
633 setup for an async call - nothing to do yet
635 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs,
636 struct smbsrv_request *req,
645 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs,
646 struct smbsrv_request *req)
648 return NT_STATUS_NOT_IMPLEMENTED;
654 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs,
655 struct smbsrv_request *req, union smb_lock *lck)
657 struct cvfs_private *private = ntvfs->private_data;
658 struct smbcli_request *c_req;
662 if (lck->generic.level != RAW_LOCK_GENERIC &&
663 private->map_generic) {
664 return ntvfs_map_lock(req, lck, ntvfs);
667 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
668 return smb_raw_lock(private->tree, lck);
671 c_req = smb_raw_lock_send(private->tree, lck);
676 set info on a open file
678 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
679 struct smbsrv_request *req,
680 union smb_setfileinfo *info)
682 struct cvfs_private *private = ntvfs->private_data;
683 struct smbcli_request *c_req;
687 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
688 return smb_raw_setfileinfo(private->tree, info);
690 c_req = smb_raw_setfileinfo_send(private->tree, info);
697 a handler for async fsinfo replies
699 static void async_fsinfo(struct smbcli_request *c_req)
701 struct async_info *async = c_req->async.private;
702 struct smbsrv_request *req = async->req;
703 req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
704 req->async_states->send_fn(req);
708 return filesystem space info
710 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs,
711 struct smbsrv_request *req, union smb_fsinfo *fs)
713 struct cvfs_private *private = ntvfs->private_data;
714 struct smbcli_request *c_req;
718 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
719 return smb_raw_fsinfo(private->tree, req, fs);
722 c_req = smb_raw_fsinfo_send(private->tree, req, fs);
724 ASYNC_RECV_TAIL(fs, async_fsinfo);
728 return print queue info
730 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs,
731 struct smbsrv_request *req, union smb_lpq *lpq)
733 return NT_STATUS_NOT_SUPPORTED;
737 list files in a directory matching a wildcard pattern
739 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs,
740 struct smbsrv_request *req, union smb_search_first *io,
741 void *search_private,
742 BOOL (*callback)(void *, union smb_search_data *))
744 struct cvfs_private *private = ntvfs->private_data;
748 return smb_raw_search_first(private->tree, req, io, search_private, callback);
751 /* continue a search */
752 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs,
753 struct smbsrv_request *req, union smb_search_next *io,
754 void *search_private,
755 BOOL (*callback)(void *, union smb_search_data *))
757 struct cvfs_private *private = ntvfs->private_data;
761 return smb_raw_search_next(private->tree, req, io, search_private, callback);
765 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs,
766 struct smbsrv_request *req, union smb_search_close *io)
768 struct cvfs_private *private = ntvfs->private_data;
772 return smb_raw_search_close(private->tree, io);
776 a handler for async trans2 replies
778 static void async_trans2(struct smbcli_request *c_req)
780 struct async_info *async = c_req->async.private;
781 struct smbsrv_request *req = async->req;
782 req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
783 req->async_states->send_fn(req);
787 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs,
788 struct smbsrv_request *req, struct smb_trans2 *trans2)
790 struct cvfs_private *private = ntvfs->private_data;
791 struct smbcli_request *c_req;
795 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
796 return smb_raw_trans2(private->tree, req, trans2);
799 c_req = smb_raw_trans2_send(private->tree, trans2);
801 ASYNC_RECV_TAIL(trans2, async_trans2);
805 /* SMBtrans - not used on file shares */
806 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs,
807 struct smbsrv_request *req, struct smb_trans2 *trans2)
809 return NT_STATUS_ACCESS_DENIED;
813 initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
815 NTSTATUS ntvfs_cifs_init(void)
818 struct ntvfs_ops ops;
822 /* fill in the name and type */
824 ops.type = NTVFS_DISK;
826 /* fill in all the operations */
827 ops.connect = cvfs_connect;
828 ops.disconnect = cvfs_disconnect;
829 ops.unlink = cvfs_unlink;
830 ops.chkpath = cvfs_chkpath;
831 ops.qpathinfo = cvfs_qpathinfo;
832 ops.setpathinfo = cvfs_setpathinfo;
833 ops.openfile = cvfs_open;
834 ops.mkdir = cvfs_mkdir;
835 ops.rmdir = cvfs_rmdir;
836 ops.rename = cvfs_rename;
837 ops.copy = cvfs_copy;
838 ops.ioctl = cvfs_ioctl;
839 ops.read = cvfs_read;
840 ops.write = cvfs_write;
841 ops.seek = cvfs_seek;
842 ops.flush = cvfs_flush;
843 ops.close = cvfs_close;
844 ops.exit = cvfs_exit;
845 ops.lock = cvfs_lock;
846 ops.setfileinfo = cvfs_setfileinfo;
847 ops.qfileinfo = cvfs_qfileinfo;
848 ops.fsinfo = cvfs_fsinfo;
850 ops.search_first = cvfs_search_first;
851 ops.search_next = cvfs_search_next;
852 ops.search_close = cvfs_search_close;
853 ops.trans = cvfs_trans;
854 ops.logoff = cvfs_logoff;
855 ops.async_setup = cvfs_async_setup;
856 ops.cancel = cvfs_cancel;
858 if (lp_parm_bool(-1, "cifs", "maptrans2", False)) {
859 ops.trans2 = cvfs_trans2;
862 /* register ourselves with the NTVFS subsystem. We register
863 under the name 'cifs'. */
864 ret = ntvfs_register(&ops);
866 if (!NT_STATUS_IS_OK(ret)) {
867 DEBUG(0,("Failed to register CIFS backend!\n"));