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.
29 #include "lib/events/events.h"
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 uint16_t flags, void *private)
70 struct cvfs_private *cvfs = talloc_get_type(private, struct cvfs_private);
71 struct smbsrv_tcon *tcon = cvfs->tcon;
73 if (!smbcli_transport_process(cvfs->transport)) {
74 /* the connection to our server is dead */
80 connect to a share - used when a tree_connect operation comes in.
82 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs,
83 struct smbsrv_request *req, const char *sharename)
85 struct smbsrv_tcon *tcon = req->tcon;
87 struct cvfs_private *private;
88 const char *host, *user, *pass, *domain, *remote_share;
89 struct smb_composite_connect io;
90 struct composite_context *creq;
93 /* Here we need to determine which server to connect to.
94 * For now we use parametric options, type cifs.
95 * Later we will use security=server and auth_server.c.
97 host = lp_parm_string(req->tcon->service, "cifs", "server");
98 user = lp_parm_string(req->tcon->service, "cifs", "user");
99 pass = lp_parm_string(req->tcon->service, "cifs", "password");
100 domain = lp_parm_string(req->tcon->service, "cifs", "domain");
101 remote_share = lp_parm_string(req->tcon->service, "cifs", "share");
103 remote_share = sharename;
106 if (!host || !user || !pass || !domain) {
107 DEBUG(1,("CIFS backend: You must supply server, user, password and domain\n"));
108 return NT_STATUS_INVALID_PARAMETER;
111 private = talloc(req->tcon, struct cvfs_private);
113 return NT_STATUS_NO_MEMORY;
115 ZERO_STRUCTP(private);
117 ntvfs->private_data = private;
119 /* connect to the server, using the smbd event context */
120 io.in.dest_host = host;
122 io.in.called_name = host;
123 io.in.calling_name = "vfs_cifs";
124 io.in.service = remote_share;
125 io.in.service_type = "?????";
126 io.in.domain = domain;
128 io.in.password = pass;
130 creq = smb_composite_connect_send(&io, tcon->smb_conn->connection->event.ctx);
131 status = smb_composite_connect_recv(creq, private);
132 NT_STATUS_NOT_OK_RETURN(status);
134 private->tree = io.out.tree;
136 private->transport = private->tree->session->transport;
138 private->tcon = req->tcon;
140 tcon->fs_type = talloc_strdup(tcon, "NTFS");
141 tcon->dev_type = talloc_strdup(tcon, "A:");
143 /* we need to receive oplock break requests from the server */
144 smbcli_oplock_handler(private->transport, oplock_handler, private);
146 /* take over event handling for this socket */
147 talloc_free(private->transport->socket->event.fde);
148 fde = event_add_fd(private->transport->socket->event.ctx,
150 socket_get_fd(private->transport->socket->sock),
151 EVENT_FD_READ | EVENT_FD_WRITE,
154 private->transport->socket->event.fde = fde;
157 private->map_generic = lp_parm_bool(req->tcon->service,
158 "cifs", "mapgeneric", False);
164 disconnect from a share
166 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs,
167 struct smbsrv_tcon *tcon)
169 struct cvfs_private *private = ntvfs->private_data;
171 talloc_free(private);
177 a handler for simple async replies
178 this handler can only be used for functions that don't return any
179 parameters (those that just return a status code)
181 static void async_simple(struct smbcli_request *c_req)
183 struct async_info *async = c_req->async.private;
184 struct smbsrv_request *req = async->req;
185 req->async_states->status = smbcli_request_simple_recv(c_req);
186 req->async_states->send_fn(req);
190 /* save some typing for the simple functions */
191 #define ASYNC_RECV_TAIL(io, async_fn) do { \
192 if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
194 struct async_info *async; \
195 async = talloc(req, struct async_info); \
196 if (!async) return NT_STATUS_NO_MEMORY; \
199 c_req->async.private = async; \
201 c_req->async.fn = async_fn; \
202 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
203 return NT_STATUS_OK; \
206 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
209 delete a file - the dirtype specifies the file types to include in the search.
210 The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
212 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs,
213 struct smbsrv_request *req, struct smb_unlink *unl)
215 struct cvfs_private *private = ntvfs->private_data;
216 struct smbcli_request *c_req;
220 /* see if the front end will allow us to perform this
221 function asynchronously. */
222 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
223 return smb_raw_unlink(private->tree, unl);
226 c_req = smb_raw_unlink_send(private->tree, unl);
232 a handler for async ioctl replies
234 static void async_ioctl(struct smbcli_request *c_req)
236 struct async_info *async = c_req->async.private;
237 struct smbsrv_request *req = async->req;
238 req->async_states->status = smb_raw_ioctl_recv(c_req, req, async->parms);
239 req->async_states->send_fn(req);
245 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs,
246 struct smbsrv_request *req, union smb_ioctl *io)
248 struct cvfs_private *private = ntvfs->private_data;
249 struct smbcli_request *c_req;
253 /* see if the front end will allow us to perform this
254 function asynchronously. */
255 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
256 return smb_raw_ioctl(private->tree, req, io);
259 c_req = smb_raw_ioctl_send(private->tree, io);
261 ASYNC_RECV_TAIL(io, async_ioctl);
265 check if a directory exists
267 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs,
268 struct smbsrv_request *req, struct smb_chkpath *cp)
270 struct cvfs_private *private = ntvfs->private_data;
271 struct smbcli_request *c_req;
275 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
276 return smb_raw_chkpath(private->tree, cp);
279 c_req = smb_raw_chkpath_send(private->tree, cp);
285 a handler for async qpathinfo replies
287 static void async_qpathinfo(struct smbcli_request *c_req)
289 struct async_info *async = c_req->async.private;
290 struct smbsrv_request *req = async->req;
291 req->async_states->status = smb_raw_pathinfo_recv(c_req, req, async->parms);
292 req->async_states->send_fn(req);
296 return info on a pathname
298 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs,
299 struct smbsrv_request *req, union smb_fileinfo *info)
301 struct cvfs_private *private = ntvfs->private_data;
302 struct smbcli_request *c_req;
306 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
307 return smb_raw_pathinfo(private->tree, req, info);
310 c_req = smb_raw_pathinfo_send(private->tree, info);
312 ASYNC_RECV_TAIL(info, async_qpathinfo);
316 a handler for async qfileinfo replies
318 static void async_qfileinfo(struct smbcli_request *c_req)
320 struct async_info *async = c_req->async.private;
321 struct smbsrv_request *req = async->req;
322 req->async_states->status = smb_raw_fileinfo_recv(c_req, req, async->parms);
323 req->async_states->send_fn(req);
327 query info on a open file
329 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs,
330 struct smbsrv_request *req, union smb_fileinfo *info)
332 struct cvfs_private *private = ntvfs->private_data;
333 struct smbcli_request *c_req;
337 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
338 return smb_raw_fileinfo(private->tree, req, info);
341 c_req = smb_raw_fileinfo_send(private->tree, info);
343 ASYNC_RECV_TAIL(info, async_qfileinfo);
348 set info on a pathname
350 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
351 struct smbsrv_request *req, union smb_setfileinfo *st)
353 struct cvfs_private *private = ntvfs->private_data;
354 struct smbcli_request *c_req;
358 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
359 return smb_raw_setpathinfo(private->tree, st);
362 c_req = smb_raw_setpathinfo_send(private->tree, st);
369 a handler for async open replies
371 static void async_open(struct smbcli_request *c_req)
373 struct async_info *async = c_req->async.private;
374 struct smbsrv_request *req = async->req;
375 req->async_states->status = smb_raw_open_recv(c_req, req, async->parms);
376 req->async_states->send_fn(req);
382 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs,
383 struct smbsrv_request *req, union smb_open *io)
385 struct cvfs_private *private = ntvfs->private_data;
386 struct smbcli_request *c_req;
390 if (io->generic.level != RAW_OPEN_GENERIC &&
391 private->map_generic) {
392 return ntvfs_map_open(req, io, ntvfs);
395 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
396 return smb_raw_open(private->tree, req, io);
399 c_req = smb_raw_open_send(private->tree, io);
401 ASYNC_RECV_TAIL(io, async_open);
407 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs,
408 struct smbsrv_request *req, union smb_mkdir *md)
410 struct cvfs_private *private = ntvfs->private_data;
411 struct smbcli_request *c_req;
415 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
416 return smb_raw_mkdir(private->tree, md);
419 c_req = smb_raw_mkdir_send(private->tree, md);
427 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs,
428 struct smbsrv_request *req, struct smb_rmdir *rd)
430 struct cvfs_private *private = ntvfs->private_data;
431 struct smbcli_request *c_req;
435 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
436 return smb_raw_rmdir(private->tree, rd);
438 c_req = smb_raw_rmdir_send(private->tree, rd);
444 rename a set of files
446 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs,
447 struct smbsrv_request *req, union smb_rename *ren)
449 struct cvfs_private *private = ntvfs->private_data;
450 struct smbcli_request *c_req;
454 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
455 return smb_raw_rename(private->tree, ren);
458 c_req = smb_raw_rename_send(private->tree, ren);
466 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs,
467 struct smbsrv_request *req, struct smb_copy *cp)
469 return NT_STATUS_NOT_SUPPORTED;
473 a handler for async read replies
475 static void async_read(struct smbcli_request *c_req)
477 struct async_info *async = c_req->async.private;
478 struct smbsrv_request *req = async->req;
479 req->async_states->status = smb_raw_read_recv(c_req, async->parms);
480 req->async_states->send_fn(req);
486 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs,
487 struct smbsrv_request *req, union smb_read *rd)
489 struct cvfs_private *private = ntvfs->private_data;
490 struct smbcli_request *c_req;
494 if (rd->generic.level != RAW_READ_GENERIC &&
495 private->map_generic) {
496 return ntvfs_map_read(req, rd, ntvfs);
499 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
500 return smb_raw_read(private->tree, rd);
503 c_req = smb_raw_read_send(private->tree, rd);
505 ASYNC_RECV_TAIL(rd, async_read);
509 a handler for async write replies
511 static void async_write(struct smbcli_request *c_req)
513 struct async_info *async = c_req->async.private;
514 struct smbsrv_request *req = async->req;
515 req->async_states->status = smb_raw_write_recv(c_req, async->parms);
516 req->async_states->send_fn(req);
522 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs,
523 struct smbsrv_request *req, union smb_write *wr)
525 struct cvfs_private *private = ntvfs->private_data;
526 struct smbcli_request *c_req;
530 if (wr->generic.level != RAW_WRITE_GENERIC &&
531 private->map_generic) {
532 return ntvfs_map_write(req, wr, ntvfs);
535 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
536 return smb_raw_write(private->tree, wr);
539 c_req = smb_raw_write_send(private->tree, wr);
541 ASYNC_RECV_TAIL(wr, async_write);
547 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs,
548 struct smbsrv_request *req, struct smb_seek *io)
550 struct cvfs_private *private = ntvfs->private_data;
551 struct smbcli_request *c_req;
555 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
556 return smb_raw_seek(private->tree, io);
559 c_req = smb_raw_seek_send(private->tree, io);
567 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs,
568 struct smbsrv_request *req, struct smb_flush *io)
570 struct cvfs_private *private = ntvfs->private_data;
571 struct smbcli_request *c_req;
575 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
576 return smb_raw_flush(private->tree, io);
579 c_req = smb_raw_flush_send(private->tree, io);
587 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs,
588 struct smbsrv_request *req, union smb_close *io)
590 struct cvfs_private *private = ntvfs->private_data;
591 struct smbcli_request *c_req;
595 if (io->generic.level != RAW_CLOSE_GENERIC &&
596 private->map_generic) {
597 return ntvfs_map_close(req, io, ntvfs);
600 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
601 return smb_raw_close(private->tree, io);
604 c_req = smb_raw_close_send(private->tree, io);
610 exit - closing files open by the pid
612 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs,
613 struct smbsrv_request *req)
615 struct cvfs_private *private = ntvfs->private_data;
616 struct smbcli_request *c_req;
620 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
621 return smb_raw_exit(private->tree->session);
624 c_req = smb_raw_exit_send(private->tree->session);
630 logoff - closing files open by the user
632 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs,
633 struct smbsrv_request *req)
635 /* we can't do this right in the cifs backend .... */
640 setup for an async call - nothing to do yet
642 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs,
643 struct smbsrv_request *req,
652 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs,
653 struct smbsrv_request *req)
655 return NT_STATUS_NOT_IMPLEMENTED;
661 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs,
662 struct smbsrv_request *req, union smb_lock *lck)
664 struct cvfs_private *private = ntvfs->private_data;
665 struct smbcli_request *c_req;
669 if (lck->generic.level != RAW_LOCK_GENERIC &&
670 private->map_generic) {
671 return ntvfs_map_lock(req, lck, ntvfs);
674 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
675 return smb_raw_lock(private->tree, lck);
678 c_req = smb_raw_lock_send(private->tree, lck);
683 set info on a open file
685 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
686 struct smbsrv_request *req,
687 union smb_setfileinfo *info)
689 struct cvfs_private *private = ntvfs->private_data;
690 struct smbcli_request *c_req;
694 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
695 return smb_raw_setfileinfo(private->tree, info);
697 c_req = smb_raw_setfileinfo_send(private->tree, info);
704 a handler for async fsinfo replies
706 static void async_fsinfo(struct smbcli_request *c_req)
708 struct async_info *async = c_req->async.private;
709 struct smbsrv_request *req = async->req;
710 req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
711 req->async_states->send_fn(req);
715 return filesystem space info
717 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs,
718 struct smbsrv_request *req, union smb_fsinfo *fs)
720 struct cvfs_private *private = ntvfs->private_data;
721 struct smbcli_request *c_req;
725 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
726 return smb_raw_fsinfo(private->tree, req, fs);
729 c_req = smb_raw_fsinfo_send(private->tree, req, fs);
731 ASYNC_RECV_TAIL(fs, async_fsinfo);
735 return print queue info
737 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs,
738 struct smbsrv_request *req, union smb_lpq *lpq)
740 return NT_STATUS_NOT_SUPPORTED;
744 list files in a directory matching a wildcard pattern
746 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs,
747 struct smbsrv_request *req, union smb_search_first *io,
748 void *search_private,
749 BOOL (*callback)(void *, union smb_search_data *))
751 struct cvfs_private *private = ntvfs->private_data;
755 return smb_raw_search_first(private->tree, req, io, search_private, callback);
758 /* continue a search */
759 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs,
760 struct smbsrv_request *req, union smb_search_next *io,
761 void *search_private,
762 BOOL (*callback)(void *, union smb_search_data *))
764 struct cvfs_private *private = ntvfs->private_data;
768 return smb_raw_search_next(private->tree, req, io, search_private, callback);
772 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs,
773 struct smbsrv_request *req, union smb_search_close *io)
775 struct cvfs_private *private = ntvfs->private_data;
779 return smb_raw_search_close(private->tree, io);
783 a handler for async trans2 replies
785 static void async_trans2(struct smbcli_request *c_req)
787 struct async_info *async = c_req->async.private;
788 struct smbsrv_request *req = async->req;
789 req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
790 req->async_states->send_fn(req);
794 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs,
795 struct smbsrv_request *req, struct smb_trans2 *trans2)
797 struct cvfs_private *private = ntvfs->private_data;
798 struct smbcli_request *c_req;
802 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
803 return smb_raw_trans2(private->tree, req, trans2);
806 c_req = smb_raw_trans2_send(private->tree, trans2);
808 ASYNC_RECV_TAIL(trans2, async_trans2);
812 /* SMBtrans - not used on file shares */
813 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs,
814 struct smbsrv_request *req, struct smb_trans2 *trans2)
816 return NT_STATUS_ACCESS_DENIED;
820 initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
822 NTSTATUS ntvfs_cifs_init(void)
825 struct ntvfs_ops ops;
829 /* fill in the name and type */
831 ops.type = NTVFS_DISK;
833 /* fill in all the operations */
834 ops.connect = cvfs_connect;
835 ops.disconnect = cvfs_disconnect;
836 ops.unlink = cvfs_unlink;
837 ops.chkpath = cvfs_chkpath;
838 ops.qpathinfo = cvfs_qpathinfo;
839 ops.setpathinfo = cvfs_setpathinfo;
840 ops.openfile = cvfs_open;
841 ops.mkdir = cvfs_mkdir;
842 ops.rmdir = cvfs_rmdir;
843 ops.rename = cvfs_rename;
844 ops.copy = cvfs_copy;
845 ops.ioctl = cvfs_ioctl;
846 ops.read = cvfs_read;
847 ops.write = cvfs_write;
848 ops.seek = cvfs_seek;
849 ops.flush = cvfs_flush;
850 ops.close = cvfs_close;
851 ops.exit = cvfs_exit;
852 ops.lock = cvfs_lock;
853 ops.setfileinfo = cvfs_setfileinfo;
854 ops.qfileinfo = cvfs_qfileinfo;
855 ops.fsinfo = cvfs_fsinfo;
857 ops.search_first = cvfs_search_first;
858 ops.search_next = cvfs_search_next;
859 ops.search_close = cvfs_search_close;
860 ops.trans = cvfs_trans;
861 ops.logoff = cvfs_logoff;
862 ops.async_setup = cvfs_async_setup;
863 ops.cancel = cvfs_cancel;
865 if (lp_parm_bool(-1, "cifs", "maptrans2", False)) {
866 ops.trans2 = cvfs_trans2;
869 /* register ourselves with the NTVFS subsystem. We register
870 under the name 'cifs'. */
871 ret = ntvfs_register(&ops);
873 if (!NT_STATUS_IS_OK(ret)) {
874 DEBUG(0,("Failed to register CIFS backend!\n"));