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 /* this is stored in ntvfs_private */
32 struct smbcli_tree *tree;
33 struct smbcli_transport *transport;
34 struct smbsrv_tcon *tcon;
35 /*const struct ntvfs_ops *ops;*/
39 /* a structure used to pass information to an async handler */
41 struct smbsrv_request *req;
46 an idle function to cope with messages from the smbd client while
47 waiting for a reply from the server
48 this function won't be needed once all of the cifs backend
49 and the core of smbd is converted to use async calls
51 static void idle_func(struct smbcli_transport *transport, void *p_private)
53 struct cvfs_private *private = p_private;
54 int fd = socket_get_fd(private->tcon->smb_conn->connection->socket);
56 if (socket_pending(fd)) {
57 smbd_process_async(private->tcon->smb_conn);
62 a handler for oplock break events from the server - these need to be passed
65 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *p_private)
67 struct cvfs_private *private = p_private;
69 DEBUG(5,("vfs_cifs: sending oplock break level %d for fnum %d\n", level, fnum));
70 return req_send_oplock_break(private->tcon, fnum, level);
74 a handler for read events on a connection to a backend server
76 static void cifs_socket_handler(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags)
78 struct cvfs_private *private = fde->private;
79 struct smbsrv_tcon *tcon = private->tcon;
81 DEBUG(5,("cifs_socket_handler event on fd %d\n", fde->fd));
83 if (!smbcli_transport_process(private->transport)) {
84 /* the connection to our server is dead */
90 connect to a share - used when a tree_connect operation comes in.
92 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs,
93 struct smbsrv_request *req, const char *sharename)
95 struct smbsrv_tcon *tcon = req->tcon;
97 struct cvfs_private *private;
98 const char *host, *user, *pass, *domain, *remote_share;
100 /* Here we need to determine which server to connect to.
101 * For now we use parametric options, type cifs.
102 * Later we will use security=server and auth_server.c.
104 host = lp_parm_string(req->tcon->service, "cifs", "server");
105 user = lp_parm_string(req->tcon->service, "cifs", "user");
106 pass = lp_parm_string(req->tcon->service, "cifs", "password");
107 domain = lp_parm_string(req->tcon->service, "cifs", "domain");
108 remote_share = lp_parm_string(req->tcon->service, "cifs", "share");
110 remote_share = sharename;
113 if (!host || !user || !pass || !domain) {
114 DEBUG(1,("CIFS backend: You must supply server, user, password and domain\n"));
115 return NT_STATUS_INVALID_PARAMETER;
118 private = talloc(req->tcon, sizeof(struct cvfs_private));
120 return NT_STATUS_NO_MEMORY;
122 ZERO_STRUCTP(private);
124 ntvfs->private_data = private;
126 status = smbcli_tree_full_connection(private,
131 remote_share, "?????",
134 if (!NT_STATUS_IS_OK(status)) {
138 private->transport = private->tree->session->transport;
139 private->tree->session->pid = SVAL(req->in.hdr, HDR_PID);
140 private->tcon = req->tcon;
142 tcon->fs_type = talloc_strdup(tcon, "NTFS");
143 tcon->dev_type = talloc_strdup(tcon, "A:");
145 /* we need to receive oplock break requests from the server */
146 smbcli_oplock_handler(private->transport, oplock_handler, private);
147 smbcli_transport_idle_handler(private->transport, idle_func, 1, private);
149 private->transport->event.fde->handler = cifs_socket_handler;
150 private->transport->event.fde->private = private;
152 private->transport->event.ctx = event_context_merge(tcon->smb_conn->connection->event.ctx,
153 private->transport->event.ctx);
154 talloc_reference(private, private->transport->event.ctx);
160 disconnect from a share
162 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs,
163 struct smbsrv_tcon *tcon)
165 struct cvfs_private *private = ntvfs->private_data;
167 smb_tree_disconnect(private->tree);
168 talloc_free(private->tree);
174 a handler for simple async replies
175 this handler can only be used for functions that don't return any
176 parameters (those that just return a status code)
178 static void async_simple(struct smbcli_request *c_req)
180 struct async_info *async = c_req->async.private;
181 struct smbsrv_request *req = async->req;
182 req->async.status = smbcli_request_simple_recv(c_req);
183 req->async.send_fn(req);
187 /* save some typing for the simple functions */
188 #define ASYNC_RECV_TAIL(io, async_fn) do { \
189 if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
191 struct async_info *async; \
192 async = talloc_p(req, struct async_info); \
193 if (!async) return NT_STATUS_NO_MEMORY; \
196 c_req->async.private = async; \
198 c_req->async.fn = async_fn; \
199 req->control_flags |= REQ_CONTROL_ASYNC; \
200 return NT_STATUS_OK; \
203 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
206 delete a file - the dirtype specifies the file types to include in the search.
207 The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
209 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs,
210 struct smbsrv_request *req, struct smb_unlink *unl)
212 struct cvfs_private *private = ntvfs->private_data;
213 struct smbcli_request *c_req;
215 /* see if the front end will allow us to perform this
216 function asynchronously. */
217 if (!req->async.send_fn) {
218 return smb_raw_unlink(private->tree, unl);
221 c_req = smb_raw_unlink_send(private->tree, unl);
227 a handler for async ioctl replies
229 static void async_ioctl(struct smbcli_request *c_req)
231 struct async_info *async = c_req->async.private;
232 struct smbsrv_request *req = async->req;
233 req->async.status = smb_raw_ioctl_recv(c_req, req, async->parms);
234 req->async.send_fn(req);
240 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs,
241 struct smbsrv_request *req, union smb_ioctl *io)
243 struct cvfs_private *private = ntvfs->private_data;
244 struct smbcli_request *c_req;
246 /* see if the front end will allow us to perform this
247 function asynchronously. */
248 if (!req->async.send_fn) {
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;
266 if (!req->async.send_fn) {
267 return smb_raw_chkpath(private->tree, cp);
270 c_req = smb_raw_chkpath_send(private->tree, cp);
276 a handler for async qpathinfo replies
278 static void async_qpathinfo(struct smbcli_request *c_req)
280 struct async_info *async = c_req->async.private;
281 struct smbsrv_request *req = async->req;
282 req->async.status = smb_raw_pathinfo_recv(c_req, req, async->parms);
283 req->async.send_fn(req);
287 return info on a pathname
289 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs,
290 struct smbsrv_request *req, union smb_fileinfo *info)
292 struct cvfs_private *private = ntvfs->private_data;
293 struct smbcli_request *c_req;
295 if (!req->async.send_fn) {
296 return smb_raw_pathinfo(private->tree, req, info);
299 c_req = smb_raw_pathinfo_send(private->tree, info);
301 ASYNC_RECV_TAIL(info, async_qpathinfo);
305 a handler for async qfileinfo replies
307 static void async_qfileinfo(struct smbcli_request *c_req)
309 struct async_info *async = c_req->async.private;
310 struct smbsrv_request *req = async->req;
311 req->async.status = smb_raw_fileinfo_recv(c_req, req, async->parms);
312 req->async.send_fn(req);
316 query info on a open file
318 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs,
319 struct smbsrv_request *req, union smb_fileinfo *info)
321 struct cvfs_private *private = ntvfs->private_data;
322 struct smbcli_request *c_req;
324 if (!req->async.send_fn) {
325 return smb_raw_fileinfo(private->tree, req, info);
328 c_req = smb_raw_fileinfo_send(private->tree, info);
330 ASYNC_RECV_TAIL(info, async_qfileinfo);
335 set info on a pathname
337 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
338 struct smbsrv_request *req, union smb_setfileinfo *st)
340 struct cvfs_private *private = ntvfs->private_data;
341 struct smbcli_request *c_req;
343 if (!req->async.send_fn) {
344 return smb_raw_setpathinfo(private->tree, st);
347 c_req = smb_raw_setpathinfo_send(private->tree, st);
354 a handler for async open replies
356 static void async_open(struct smbcli_request *c_req)
358 struct async_info *async = c_req->async.private;
359 struct smbsrv_request *req = async->req;
360 req->async.status = smb_raw_open_recv(c_req, req, async->parms);
361 req->async.send_fn(req);
367 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs,
368 struct smbsrv_request *req, union smb_open *io)
370 struct cvfs_private *private = ntvfs->private_data;
371 struct smbcli_request *c_req;
373 if (!req->async.send_fn) {
374 return smb_raw_open(private->tree, req, io);
377 c_req = smb_raw_open_send(private->tree, io);
379 ASYNC_RECV_TAIL(io, async_open);
385 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs,
386 struct smbsrv_request *req, union smb_mkdir *md)
388 struct cvfs_private *private = ntvfs->private_data;
389 struct smbcli_request *c_req;
391 if (!req->async.send_fn) {
392 return smb_raw_mkdir(private->tree, md);
395 c_req = smb_raw_mkdir_send(private->tree, md);
403 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs,
404 struct smbsrv_request *req, struct smb_rmdir *rd)
406 struct cvfs_private *private = ntvfs->private_data;
407 struct smbcli_request *c_req;
409 if (!req->async.send_fn) {
410 return smb_raw_rmdir(private->tree, rd);
412 c_req = smb_raw_rmdir_send(private->tree, rd);
418 rename a set of files
420 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs,
421 struct smbsrv_request *req, union smb_rename *ren)
423 struct cvfs_private *private = ntvfs->private_data;
424 struct smbcli_request *c_req;
426 if (!req->async.send_fn) {
427 return smb_raw_rename(private->tree, ren);
430 c_req = smb_raw_rename_send(private->tree, ren);
438 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs,
439 struct smbsrv_request *req, struct smb_copy *cp)
441 return NT_STATUS_NOT_SUPPORTED;
445 a handler for async read replies
447 static void async_read(struct smbcli_request *c_req)
449 struct async_info *async = c_req->async.private;
450 struct smbsrv_request *req = async->req;
451 req->async.status = smb_raw_read_recv(c_req, async->parms);
452 req->async.send_fn(req);
458 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs,
459 struct smbsrv_request *req, union smb_read *rd)
461 struct cvfs_private *private = ntvfs->private_data;
462 struct smbcli_request *c_req;
464 if (!req->async.send_fn) {
465 return smb_raw_read(private->tree, rd);
468 c_req = smb_raw_read_send(private->tree, rd);
470 ASYNC_RECV_TAIL(rd, async_read);
474 a handler for async write replies
476 static void async_write(struct smbcli_request *c_req)
478 struct async_info *async = c_req->async.private;
479 struct smbsrv_request *req = async->req;
480 req->async.status = smb_raw_write_recv(c_req, async->parms);
481 req->async.send_fn(req);
487 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs,
488 struct smbsrv_request *req, union smb_write *wr)
490 struct cvfs_private *private = ntvfs->private_data;
491 struct smbcli_request *c_req;
493 if (!req->async.send_fn) {
494 return smb_raw_write(private->tree, wr);
497 c_req = smb_raw_write_send(private->tree, wr);
499 ASYNC_RECV_TAIL(wr, async_write);
505 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs,
506 struct smbsrv_request *req, struct smb_seek *io)
508 return NT_STATUS_NOT_SUPPORTED;
514 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs,
515 struct smbsrv_request *req, struct smb_flush *io)
523 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs,
524 struct smbsrv_request *req, union smb_close *io)
526 struct cvfs_private *private = ntvfs->private_data;
527 struct smbcli_request *c_req;
529 if (!req->async.send_fn) {
530 return smb_raw_close(private->tree, io);
533 c_req = smb_raw_close_send(private->tree, io);
539 exit - closing files open by the pid
541 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs,
542 struct smbsrv_request *req)
544 struct cvfs_private *private = ntvfs->private_data;
545 struct smbcli_request *c_req;
547 if (!req->async.send_fn) {
548 return smb_raw_exit(private->tree->session);
551 c_req = smb_raw_exit_send(private->tree->session);
557 logoff - closing files open by the user
559 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs,
560 struct smbsrv_request *req)
562 /* we can't do this right in the cifs backend .... */
567 setup for an async call - nothing to do yet
569 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs,
570 struct smbsrv_request *req,
579 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs,
580 struct smbsrv_request *req, union smb_lock *lck)
582 struct cvfs_private *private = ntvfs->private_data;
583 struct smbcli_request *c_req;
585 if (!req->async.send_fn) {
586 return smb_raw_lock(private->tree, lck);
589 c_req = smb_raw_lock_send(private->tree, lck);
594 set info on a open file
596 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
597 struct smbsrv_request *req,
598 union smb_setfileinfo *info)
600 struct cvfs_private *private = ntvfs->private_data;
601 struct smbcli_request *c_req;
603 if (!req->async.send_fn) {
604 return smb_raw_setfileinfo(private->tree, info);
606 c_req = smb_raw_setfileinfo_send(private->tree, info);
613 a handler for async fsinfo replies
615 static void async_fsinfo(struct smbcli_request *c_req)
617 struct async_info *async = c_req->async.private;
618 struct smbsrv_request *req = async->req;
619 req->async.status = smb_raw_fsinfo_recv(c_req, req, async->parms);
620 req->async.send_fn(req);
624 return filesystem space info
626 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs,
627 struct smbsrv_request *req, union smb_fsinfo *fs)
629 struct cvfs_private *private = ntvfs->private_data;
630 struct smbcli_request *c_req;
632 if (!req->async.send_fn) {
633 return smb_raw_fsinfo(private->tree, req, fs);
636 c_req = smb_raw_fsinfo_send(private->tree, req, fs);
638 ASYNC_RECV_TAIL(fs, async_fsinfo);
642 return print queue info
644 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs,
645 struct smbsrv_request *req, union smb_lpq *lpq)
647 return NT_STATUS_NOT_SUPPORTED;
651 list files in a directory matching a wildcard pattern
653 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs,
654 struct smbsrv_request *req, union smb_search_first *io,
655 void *search_private,
656 BOOL (*callback)(void *, union smb_search_data *))
658 struct cvfs_private *private = ntvfs->private_data;
660 return smb_raw_search_first(private->tree, req, io, search_private, callback);
663 /* continue a search */
664 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs,
665 struct smbsrv_request *req, union smb_search_next *io,
666 void *search_private,
667 BOOL (*callback)(void *, union smb_search_data *))
669 struct cvfs_private *private = ntvfs->private_data;
671 return smb_raw_search_next(private->tree, req, io, search_private, callback);
675 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs,
676 struct smbsrv_request *req, union smb_search_close *io)
678 struct cvfs_private *private = ntvfs->private_data;
680 return smb_raw_search_close(private->tree, io);
684 a handler for async trans2 replies
686 static void async_trans2(struct smbcli_request *c_req)
688 struct async_info *async = c_req->async.private;
689 struct smbsrv_request *req = async->req;
690 req->async.status = smb_raw_trans2_recv(c_req, req, async->parms);
691 req->async.send_fn(req);
695 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs,
696 struct smbsrv_request *req, struct smb_trans2 *trans2)
698 struct cvfs_private *private = ntvfs->private_data;
699 struct smbcli_request *c_req;
701 if (!req->async.send_fn) {
702 return smb_raw_trans2(private->tree, req, trans2);
705 c_req = smb_raw_trans2_send(private->tree, trans2);
707 ASYNC_RECV_TAIL(trans2, async_trans2);
711 /* SMBtrans - not used on file shares */
712 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs,
713 struct smbsrv_request *req, struct smb_trans2 *trans2)
715 return NT_STATUS_ACCESS_DENIED;
719 initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
721 NTSTATUS ntvfs_cifs_init(void)
724 struct ntvfs_ops ops;
728 /* fill in the name and type */
730 ops.type = NTVFS_DISK;
732 /* fill in all the operations */
733 ops.connect = cvfs_connect;
734 ops.disconnect = cvfs_disconnect;
735 ops.unlink = cvfs_unlink;
736 ops.chkpath = cvfs_chkpath;
737 ops.qpathinfo = cvfs_qpathinfo;
738 ops.setpathinfo = cvfs_setpathinfo;
739 ops.open = cvfs_open;
740 ops.mkdir = cvfs_mkdir;
741 ops.rmdir = cvfs_rmdir;
742 ops.rename = cvfs_rename;
743 ops.copy = cvfs_copy;
744 ops.ioctl = cvfs_ioctl;
745 ops.read = cvfs_read;
746 ops.write = cvfs_write;
747 ops.seek = cvfs_seek;
748 ops.flush = cvfs_flush;
749 ops.close = cvfs_close;
750 ops.exit = cvfs_exit;
751 ops.lock = cvfs_lock;
752 ops.setfileinfo = cvfs_setfileinfo;
753 ops.qfileinfo = cvfs_qfileinfo;
754 ops.fsinfo = cvfs_fsinfo;
756 ops.search_first = cvfs_search_first;
757 ops.search_next = cvfs_search_next;
758 ops.search_close = cvfs_search_close;
759 ops.trans = cvfs_trans;
760 ops.logoff = cvfs_logoff;
761 ops.async_setup = cvfs_async_setup;
763 if (lp_parm_bool(-1, "cifs", "maptrans2", False)) {
764 ops.trans2 = cvfs_trans2;
767 /* register ourselves with the NTVFS subsystem. We register
768 under the name 'cifs'. */
769 ret = register_backend("ntvfs", &ops);
771 if (!NT_STATUS_IS_OK(ret)) {
772 DEBUG(0,("Failed to register CIFS backend!\n"));