vfs_cifs - Use ntvfs request transport close to close downstream session
[Samba/vfs_proxy.git] / source4 / ntvfs / cifs / vfs_cifs.c
blobe615401e51380ddd2baff5be97f3806bd3f484a3
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
23 this implements a CIFS->CIFS NTVFS filesystem backend.
27 #include "includes.h"
28 #include "libcli/raw/libcliraw.h"
29 #include "libcli/raw/raw_proto.h"
30 #include "libcli/smb_composite/smb_composite.h"
31 #include "auth/auth.h"
32 #include "auth/credentials/credentials.h"
33 #include "ntvfs/ntvfs.h"
34 #include "../lib/util/dlinklist.h"
35 #include "param/param.h"
36 #include "libcli/resolve/resolve.h"
38 struct cvfs_file {
39 struct cvfs_file *prev, *next;
40 uint16_t fnum;
41 struct ntvfs_handle *h;
44 /* this is stored in ntvfs_private */
45 struct cvfs_private {
46 struct smbcli_tree *tree;
47 struct smbcli_transport *transport;
48 struct ntvfs_module_context *ntvfs;
49 struct async_info *pending;
50 struct cvfs_file *files;
51 bool map_generic;
52 bool map_trans2;
56 /* a structure used to pass information to an async handler */
57 struct async_info {
58 struct async_info *next, *prev;
59 struct cvfs_private *cvfs;
60 struct ntvfs_request *req;
61 struct smbcli_request *c_req;
62 struct cvfs_file *f;
63 void *parms;
66 #define CHECK_UPSTREAM_OPEN do { \
67 if (! private->transport->socket->sock) { \
68 req->async_states->state|=NTVFS_ASYNC_STATE_CLOSE; \
69 return NT_STATUS_CONNECTION_DISCONNECTED; \
70 } \
71 } while(0)
73 #define SETUP_PID do { \
74 private->tree->session->pid = req->smbpid; \
75 CHECK_UPSTREAM_OPEN; \
76 } while(0)
78 #define SETUP_FILE_HERE(f) do { \
79 f = ntvfs_handle_get_backend_data(io->generic.in.file.ntvfs, ntvfs); \
80 if (!f) return NT_STATUS_INVALID_HANDLE; \
81 io->generic.in.file.fnum = f->fnum; \
82 } while (0)
84 #define SETUP_FILE do { \
85 struct cvfs_file *f; \
86 SETUP_FILE_HERE(f); \
87 } while (0)
89 #define SETUP_PID_AND_FILE do { \
90 SETUP_PID; \
91 SETUP_FILE; \
92 } while (0)
94 #define CIFS_SERVER "cifs:server"
95 #define CIFS_USER "cifs:user"
96 #define CIFS_PASSWORD "cifs:password"
97 #define CIFS_DOMAIN "cifs:domain"
98 #define CIFS_SHARE "cifs:share"
99 #define CIFS_USE_MACHINE_ACCT "cifs:use-machine-account"
100 #define CIFS_MAP_GENERIC "cifs:map-generic"
101 #define CIFS_MAP_TRANS2 "cifs:map-trans2"
103 #define CIFS_USE_MACHINE_ACCT_DEFAULT false
104 #define CIFS_MAP_GENERIC_DEFAULT false
105 #define CIFS_MAP_TRANS2_DEFAULT true
108 a handler for oplock break events from the server - these need to be passed
109 along to the client
111 static bool oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *p_private)
113 struct cvfs_private *private = p_private;
114 NTSTATUS status;
115 struct ntvfs_handle *h = NULL;
116 struct cvfs_file *f;
118 for (f=private->files; f; f=f->next) {
119 if (f->fnum != fnum) continue;
120 h = f->h;
121 break;
124 if (!h) {
125 DEBUG(5,("vfs_cifs: ignoring oplock break level %d for fnum %d\n", level, fnum));
126 return true;
129 DEBUG(5,("vfs_cifs: sending oplock break level %d for fnum %d\n", level, fnum));
130 status = ntvfs_send_oplock_break(private->ntvfs, h, level);
131 if (!NT_STATUS_IS_OK(status)) return false;
132 return true;
136 connect to a share - used when a tree_connect operation comes in.
138 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs,
139 struct ntvfs_request *req, const char *sharename)
141 NTSTATUS status;
142 struct cvfs_private *private;
143 const char *host, *user, *pass, *domain, *remote_share;
144 struct smb_composite_connect io;
145 struct composite_context *creq;
146 struct share_config *scfg = ntvfs->ctx->config;
148 struct cli_credentials *credentials;
149 bool machine_account;
151 /* Here we need to determine which server to connect to.
152 * For now we use parametric options, type cifs.
153 * Later we will use security=server and auth_server.c.
155 host = share_string_option(scfg, CIFS_SERVER, NULL);
156 user = share_string_option(scfg, CIFS_USER, NULL);
157 pass = share_string_option(scfg, CIFS_PASSWORD, NULL);
158 domain = share_string_option(scfg, CIFS_DOMAIN, NULL);
159 remote_share = share_string_option(scfg, CIFS_SHARE, NULL);
160 if (!remote_share) {
161 remote_share = sharename;
164 machine_account = share_bool_option(scfg, CIFS_USE_MACHINE_ACCT, CIFS_USE_MACHINE_ACCT_DEFAULT);
166 private = talloc_zero(ntvfs, struct cvfs_private);
167 if (!private) {
168 return NT_STATUS_NO_MEMORY;
171 ntvfs->private_data = private;
173 if (!host) {
174 DEBUG(1,("CIFS backend: You must supply server\n"));
175 return NT_STATUS_INVALID_PARAMETER;
178 if (user && pass) {
179 DEBUG(5, ("CIFS backend: Using specified password\n"));
180 credentials = cli_credentials_init(private);
181 if (!credentials) {
182 return NT_STATUS_NO_MEMORY;
184 cli_credentials_set_conf(credentials, ntvfs->ctx->lp_ctx);
185 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
186 if (domain) {
187 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
189 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
190 } else if (machine_account) {
191 DEBUG(5, ("CIFS backend: Using machine account\n"));
192 credentials = cli_credentials_init(private);
193 cli_credentials_set_conf(credentials, ntvfs->ctx->lp_ctx);
194 if (domain) {
195 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
197 status = cli_credentials_set_machine_account(credentials, ntvfs->ctx->lp_ctx);
198 if (!NT_STATUS_IS_OK(status)) {
199 return status;
201 } else if (req->session_info->credentials) {
202 DEBUG(5, ("CIFS backend: Using delegated credentials\n"));
203 credentials = req->session_info->credentials;
204 } else {
205 DEBUG(1,("CIFS backend: NO delegated credentials found: You must supply server, user and password or the client must supply delegated credentials\n"));
206 return NT_STATUS_INVALID_PARAMETER;
209 /* connect to the server, using the smbd event context */
210 io.in.dest_host = host;
211 io.in.dest_ports = lp_smb_ports(ntvfs->ctx->lp_ctx);
212 io.in.socket_options = lp_socket_options(ntvfs->ctx->lp_ctx);
213 io.in.called_name = host;
214 io.in.credentials = credentials;
215 io.in.fallback_to_anonymous = false;
216 io.in.workgroup = lp_workgroup(ntvfs->ctx->lp_ctx);
217 io.in.service = remote_share;
218 io.in.service_type = "?????";
219 io.in.iconv_convenience = lp_iconv_convenience(ntvfs->ctx->lp_ctx);
220 io.in.gensec_settings = lp_gensec_settings(private, ntvfs->ctx->lp_ctx);
221 lp_smbcli_options(ntvfs->ctx->lp_ctx, &io.in.options);
222 lp_smbcli_session_options(ntvfs->ctx->lp_ctx, &io.in.session_options);
224 if (!(ntvfs->ctx->client_caps & NTVFS_CLIENT_CAP_LEVEL_II_OPLOCKS)) {
225 io.in.options.use_level2_oplocks = false;
228 creq = smb_composite_connect_send(&io, private,
229 lp_resolve_context(ntvfs->ctx->lp_ctx),
230 ntvfs->ctx->event_ctx);
231 status = smb_composite_connect_recv(creq, private);
232 NT_STATUS_NOT_OK_RETURN(status);
234 private->tree = io.out.tree;
236 private->transport = private->tree->session->transport;
237 SETUP_PID;
238 private->ntvfs = ntvfs;
240 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
241 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
242 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
243 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
245 /* we need to receive oplock break requests from the server */
246 smbcli_oplock_handler(private->transport, oplock_handler, private);
248 private->map_generic = share_bool_option(scfg, CIFS_MAP_GENERIC, CIFS_MAP_GENERIC_DEFAULT);
250 private->map_trans2 = share_bool_option(scfg, CIFS_MAP_TRANS2, CIFS_MAP_TRANS2_DEFAULT);
252 return NT_STATUS_OK;
256 disconnect from a share
258 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs)
260 struct cvfs_private *private = ntvfs->private_data;
261 struct async_info *a, *an;
263 /* first cleanup pending requests */
264 for (a=private->pending; a; a = an) {
265 an = a->next;
266 smbcli_request_destroy(a->c_req);
267 talloc_free(a);
270 talloc_free(private);
271 ntvfs->private_data = NULL;
273 return NT_STATUS_OK;
277 destroy an async info structure
279 static int async_info_destructor(struct async_info *async)
281 DLIST_REMOVE(async->cvfs->pending, async);
282 return 0;
286 a handler for simple async replies
287 this handler can only be used for functions that don't return any
288 parameters (those that just return a status code)
290 static void async_simple(struct smbcli_request *c_req)
292 struct async_info *async = c_req->async.private;
293 struct ntvfs_request *req = async->req;
294 req->async_states->status = smbcli_request_simple_recv(c_req);
295 talloc_free(async);
296 req->async_states->send_fn(req);
300 /* save some typing for the simple functions */
301 #define ASYNC_RECV_TAIL_F(io, async_fn, file) do { \
302 if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
304 struct async_info *async; \
305 async = talloc(req, struct async_info); \
306 if (!async) return NT_STATUS_NO_MEMORY; \
307 async->parms = io; \
308 async->req = req; \
309 async->f = file; \
310 async->cvfs = private; \
311 async->c_req = c_req; \
312 DLIST_ADD(private->pending, async); \
313 c_req->async.private = async; \
314 talloc_set_destructor(async, async_info_destructor); \
316 c_req->async.fn = async_fn; \
317 req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
318 return NT_STATUS_OK; \
319 } while (0)
321 #define ASYNC_RECV_TAIL(io, async_fn) ASYNC_RECV_TAIL_F(io, async_fn, NULL)
323 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
326 delete a file - the dirtype specifies the file types to include in the search.
327 The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
329 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs,
330 struct ntvfs_request *req, union smb_unlink *unl)
332 struct cvfs_private *private = ntvfs->private_data;
333 struct smbcli_request *c_req;
335 SETUP_PID;
337 /* see if the front end will allow us to perform this
338 function asynchronously. */
339 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
340 return smb_raw_unlink(private->tree, unl);
343 c_req = smb_raw_unlink_send(private->tree, unl);
345 SIMPLE_ASYNC_TAIL;
349 a handler for async ioctl replies
351 static void async_ioctl(struct smbcli_request *c_req)
353 struct async_info *async = c_req->async.private;
354 struct ntvfs_request *req = async->req;
355 req->async_states->status = smb_raw_ioctl_recv(c_req, req, async->parms);
356 talloc_free(async);
357 req->async_states->send_fn(req);
361 ioctl interface
363 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs,
364 struct ntvfs_request *req, union smb_ioctl *io)
366 struct cvfs_private *private = ntvfs->private_data;
367 struct smbcli_request *c_req;
369 SETUP_PID_AND_FILE;
371 /* see if the front end will allow us to perform this
372 function asynchronously. */
373 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
374 return smb_raw_ioctl(private->tree, req, io);
377 c_req = smb_raw_ioctl_send(private->tree, io);
379 ASYNC_RECV_TAIL(io, async_ioctl);
383 check if a directory exists
385 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs,
386 struct ntvfs_request *req, union smb_chkpath *cp)
388 struct cvfs_private *private = ntvfs->private_data;
389 struct smbcli_request *c_req;
391 SETUP_PID;
393 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
394 return smb_raw_chkpath(private->tree, cp);
397 c_req = smb_raw_chkpath_send(private->tree, cp);
399 SIMPLE_ASYNC_TAIL;
403 a handler for async qpathinfo replies
405 static void async_qpathinfo(struct smbcli_request *c_req)
407 struct async_info *async = c_req->async.private;
408 struct ntvfs_request *req = async->req;
409 req->async_states->status = smb_raw_pathinfo_recv(c_req, req, async->parms);
410 talloc_free(async);
411 req->async_states->send_fn(req);
415 return info on a pathname
417 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs,
418 struct ntvfs_request *req, union smb_fileinfo *info)
420 struct cvfs_private *private = ntvfs->private_data;
421 struct smbcli_request *c_req;
423 SETUP_PID;
425 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
426 return smb_raw_pathinfo(private->tree, req, info);
429 c_req = smb_raw_pathinfo_send(private->tree, info);
431 ASYNC_RECV_TAIL(info, async_qpathinfo);
435 a handler for async qfileinfo replies
437 static void async_qfileinfo(struct smbcli_request *c_req)
439 struct async_info *async = c_req->async.private;
440 struct ntvfs_request *req = async->req;
441 req->async_states->status = smb_raw_fileinfo_recv(c_req, req, async->parms);
442 talloc_free(async);
443 req->async_states->send_fn(req);
447 query info on a open file
449 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs,
450 struct ntvfs_request *req, union smb_fileinfo *io)
452 struct cvfs_private *private = ntvfs->private_data;
453 struct smbcli_request *c_req;
455 SETUP_PID_AND_FILE;
457 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
458 return smb_raw_fileinfo(private->tree, req, io);
461 c_req = smb_raw_fileinfo_send(private->tree, io);
463 ASYNC_RECV_TAIL(io, async_qfileinfo);
468 set info on a pathname
470 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
471 struct ntvfs_request *req, union smb_setfileinfo *st)
473 struct cvfs_private *private = ntvfs->private_data;
474 struct smbcli_request *c_req;
476 SETUP_PID;
478 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
479 return smb_raw_setpathinfo(private->tree, st);
482 c_req = smb_raw_setpathinfo_send(private->tree, st);
484 SIMPLE_ASYNC_TAIL;
489 a handler for async open replies
491 static void async_open(struct smbcli_request *c_req)
493 struct async_info *async = c_req->async.private;
494 struct cvfs_private *cvfs = async->cvfs;
495 struct ntvfs_request *req = async->req;
496 struct cvfs_file *f = async->f;
497 union smb_open *io = async->parms;
498 union smb_handle *file;
499 talloc_free(async);
500 req->async_states->status = smb_raw_open_recv(c_req, req, io);
501 SMB_OPEN_OUT_FILE(io, file);
502 f->fnum = file->fnum;
503 file->ntvfs = NULL;
504 if (!NT_STATUS_IS_OK(req->async_states->status)) goto failed;
505 req->async_states->status = ntvfs_handle_set_backend_data(f->h, cvfs->ntvfs, f);
506 if (!NT_STATUS_IS_OK(req->async_states->status)) goto failed;
507 file->ntvfs = f->h;
508 DLIST_ADD(cvfs->files, f);
509 failed:
510 req->async_states->send_fn(req);
514 open a file
516 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs,
517 struct ntvfs_request *req, union smb_open *io)
519 struct cvfs_private *private = ntvfs->private_data;
520 struct smbcli_request *c_req;
521 struct ntvfs_handle *h;
522 struct cvfs_file *f;
523 NTSTATUS status;
525 SETUP_PID;
527 if (io->generic.level != RAW_OPEN_GENERIC &&
528 private->map_generic) {
529 return ntvfs_map_open(ntvfs, req, io);
532 status = ntvfs_handle_new(ntvfs, req, &h);
533 NT_STATUS_NOT_OK_RETURN(status);
535 f = talloc_zero(h, struct cvfs_file);
536 NT_STATUS_HAVE_NO_MEMORY(f);
537 f->h = h;
539 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
540 union smb_handle *file;
542 status = smb_raw_open(private->tree, req, io);
543 NT_STATUS_NOT_OK_RETURN(status);
545 SMB_OPEN_OUT_FILE(io, file);
546 f->fnum = file->fnum;
547 file->ntvfs = NULL;
548 status = ntvfs_handle_set_backend_data(f->h, private->ntvfs, f);
549 NT_STATUS_NOT_OK_RETURN(status);
550 file->ntvfs = f->h;
551 DLIST_ADD(private->files, f);
553 return NT_STATUS_OK;
556 c_req = smb_raw_open_send(private->tree, io);
558 ASYNC_RECV_TAIL_F(io, async_open, f);
562 create a directory
564 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs,
565 struct ntvfs_request *req, union smb_mkdir *md)
567 struct cvfs_private *private = ntvfs->private_data;
568 struct smbcli_request *c_req;
570 SETUP_PID;
572 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
573 return smb_raw_mkdir(private->tree, md);
576 c_req = smb_raw_mkdir_send(private->tree, md);
578 SIMPLE_ASYNC_TAIL;
582 remove a directory
584 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs,
585 struct ntvfs_request *req, struct smb_rmdir *rd)
587 struct cvfs_private *private = ntvfs->private_data;
588 struct smbcli_request *c_req;
590 SETUP_PID;
592 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
593 return smb_raw_rmdir(private->tree, rd);
595 c_req = smb_raw_rmdir_send(private->tree, rd);
597 SIMPLE_ASYNC_TAIL;
601 rename a set of files
603 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs,
604 struct ntvfs_request *req, union smb_rename *ren)
606 struct cvfs_private *private = ntvfs->private_data;
607 struct smbcli_request *c_req;
609 SETUP_PID;
611 if (ren->nttrans.level == RAW_RENAME_NTTRANS) {
612 struct cvfs_file *f;
613 f = ntvfs_handle_get_backend_data(ren->nttrans.in.file.ntvfs, ntvfs);
614 if (!f) return NT_STATUS_INVALID_HANDLE;
615 ren->nttrans.in.file.fnum = f->fnum;
618 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
619 return smb_raw_rename(private->tree, ren);
622 c_req = smb_raw_rename_send(private->tree, ren);
624 SIMPLE_ASYNC_TAIL;
628 copy a set of files
630 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs,
631 struct ntvfs_request *req, struct smb_copy *cp)
633 return NT_STATUS_NOT_SUPPORTED;
637 a handler for async read replies
639 static void async_read(struct smbcli_request *c_req)
641 struct async_info *async = c_req->async.private;
642 struct ntvfs_request *req = async->req;
643 req->async_states->status = smb_raw_read_recv(c_req, async->parms);
644 talloc_free(async);
645 req->async_states->send_fn(req);
649 read from a file
651 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs,
652 struct ntvfs_request *req, union smb_read *io)
654 struct cvfs_private *private = ntvfs->private_data;
655 struct smbcli_request *c_req;
657 SETUP_PID;
659 if (io->generic.level != RAW_READ_GENERIC &&
660 private->map_generic) {
661 return ntvfs_map_read(ntvfs, req, io);
664 SETUP_FILE;
666 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
667 return smb_raw_read(private->tree, io);
670 c_req = smb_raw_read_send(private->tree, io);
672 ASYNC_RECV_TAIL(io, async_read);
676 a handler for async write replies
678 static void async_write(struct smbcli_request *c_req)
680 struct async_info *async = c_req->async.private;
681 struct ntvfs_request *req = async->req;
682 req->async_states->status = smb_raw_write_recv(c_req, async->parms);
683 talloc_free(async);
684 req->async_states->send_fn(req);
688 write to a file
690 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs,
691 struct ntvfs_request *req, union smb_write *io)
693 struct cvfs_private *private = ntvfs->private_data;
694 struct smbcli_request *c_req;
696 SETUP_PID;
698 if (io->generic.level != RAW_WRITE_GENERIC &&
699 private->map_generic) {
700 return ntvfs_map_write(ntvfs, req, io);
702 SETUP_FILE;
704 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
705 return smb_raw_write(private->tree, io);
708 c_req = smb_raw_write_send(private->tree, io);
710 ASYNC_RECV_TAIL(io, async_write);
714 a handler for async seek replies
716 static void async_seek(struct smbcli_request *c_req)
718 struct async_info *async = c_req->async.private;
719 struct ntvfs_request *req = async->req;
720 req->async_states->status = smb_raw_seek_recv(c_req, async->parms);
721 talloc_free(async);
722 req->async_states->send_fn(req);
726 seek in a file
728 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs,
729 struct ntvfs_request *req,
730 union smb_seek *io)
732 struct cvfs_private *private = ntvfs->private_data;
733 struct smbcli_request *c_req;
735 SETUP_PID_AND_FILE;
737 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
738 return smb_raw_seek(private->tree, io);
741 c_req = smb_raw_seek_send(private->tree, io);
743 ASYNC_RECV_TAIL(io, async_seek);
747 flush a file
749 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs,
750 struct ntvfs_request *req,
751 union smb_flush *io)
753 struct cvfs_private *private = ntvfs->private_data;
754 struct smbcli_request *c_req;
756 SETUP_PID;
757 switch (io->generic.level) {
758 case RAW_FLUSH_FLUSH:
759 SETUP_FILE;
760 break;
761 case RAW_FLUSH_ALL:
762 io->generic.in.file.fnum = 0xFFFF;
763 break;
764 case RAW_FLUSH_SMB2:
765 return NT_STATUS_INVALID_LEVEL;
768 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
769 return smb_raw_flush(private->tree, io);
772 c_req = smb_raw_flush_send(private->tree, io);
774 SIMPLE_ASYNC_TAIL;
778 close a file
780 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs,
781 struct ntvfs_request *req, union smb_close *io)
783 struct cvfs_private *private = ntvfs->private_data;
784 struct smbcli_request *c_req;
785 struct cvfs_file *f;
786 union smb_close io2;
788 SETUP_PID;
790 if (io->generic.level != RAW_CLOSE_GENERIC &&
791 private->map_generic) {
792 return ntvfs_map_close(ntvfs, req, io);
795 if (io->generic.level == RAW_CLOSE_GENERIC) {
796 ZERO_STRUCT(io2);
797 io2.close.level = RAW_CLOSE_CLOSE;
798 io2.close.in.file = io->generic.in.file;
799 io2.close.in.write_time = io->generic.in.write_time;
800 io = &io2;
803 SETUP_FILE_HERE(f);
804 /* Note, we aren't free-ing f, or it's h here. Should we?
805 even if file-close fails, we'll remove it from the list,
806 what else would we do? Maybe we should not remove until
807 after the proxied call completes? */
808 DLIST_REMOVE(private->files, f);
810 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
811 return smb_raw_close(private->tree, io);
814 c_req = smb_raw_close_send(private->tree, io);
816 SIMPLE_ASYNC_TAIL;
820 exit - closing files open by the pid
822 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs,
823 struct ntvfs_request *req)
825 struct cvfs_private *private = ntvfs->private_data;
826 struct smbcli_request *c_req;
828 SETUP_PID;
830 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
831 return smb_raw_exit(private->tree->session);
834 c_req = smb_raw_exit_send(private->tree->session);
836 SIMPLE_ASYNC_TAIL;
840 logoff - closing files open by the user
842 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs,
843 struct ntvfs_request *req)
845 /* we can't do this right in the cifs backend .... */
846 return NT_STATUS_OK;
850 setup for an async call - nothing to do yet
852 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs,
853 struct ntvfs_request *req,
854 void *private)
856 return NT_STATUS_OK;
860 cancel an async call
862 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs,
863 struct ntvfs_request *req)
865 struct cvfs_private *private = ntvfs->private_data;
866 struct async_info *a;
868 /* find the matching request */
869 for (a=private->pending;a;a=a->next) {
870 if (a->req == req) {
871 break;
875 if (a == NULL) {
876 return NT_STATUS_INVALID_PARAMETER;
879 return smb_raw_ntcancel(a->c_req);
883 lock a byte range
885 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs,
886 struct ntvfs_request *req, union smb_lock *io)
888 struct cvfs_private *private = ntvfs->private_data;
889 struct smbcli_request *c_req;
891 SETUP_PID;
893 if (io->generic.level != RAW_LOCK_GENERIC &&
894 private->map_generic) {
895 return ntvfs_map_lock(ntvfs, req, io);
897 SETUP_FILE;
899 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
900 return smb_raw_lock(private->tree, io);
903 c_req = smb_raw_lock_send(private->tree, io);
904 SIMPLE_ASYNC_TAIL;
908 set info on a open file
910 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
911 struct ntvfs_request *req,
912 union smb_setfileinfo *io)
914 struct cvfs_private *private = ntvfs->private_data;
915 struct smbcli_request *c_req;
917 SETUP_PID_AND_FILE;
919 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
920 return smb_raw_setfileinfo(private->tree, io);
922 c_req = smb_raw_setfileinfo_send(private->tree, io);
924 SIMPLE_ASYNC_TAIL;
929 a handler for async fsinfo replies
931 static void async_fsinfo(struct smbcli_request *c_req)
933 struct async_info *async = c_req->async.private;
934 struct ntvfs_request *req = async->req;
935 req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
936 talloc_free(async);
937 req->async_states->send_fn(req);
941 return filesystem space info
943 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs,
944 struct ntvfs_request *req, union smb_fsinfo *fs)
946 struct cvfs_private *private = ntvfs->private_data;
947 struct smbcli_request *c_req;
949 SETUP_PID;
951 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
952 return smb_raw_fsinfo(private->tree, req, fs);
955 c_req = smb_raw_fsinfo_send(private->tree, req, fs);
957 ASYNC_RECV_TAIL(fs, async_fsinfo);
961 return print queue info
963 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs,
964 struct ntvfs_request *req, union smb_lpq *lpq)
966 return NT_STATUS_NOT_SUPPORTED;
970 list files in a directory matching a wildcard pattern
972 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs,
973 struct ntvfs_request *req, union smb_search_first *io,
974 void *search_private,
975 bool (*callback)(void *, const union smb_search_data *))
977 struct cvfs_private *private = ntvfs->private_data;
979 SETUP_PID;
981 return smb_raw_search_first(private->tree, req, io, search_private, callback);
984 /* continue a search */
985 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs,
986 struct ntvfs_request *req, union smb_search_next *io,
987 void *search_private,
988 bool (*callback)(void *, const union smb_search_data *))
990 struct cvfs_private *private = ntvfs->private_data;
992 SETUP_PID;
994 return smb_raw_search_next(private->tree, req, io, search_private, callback);
997 /* close a search */
998 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs,
999 struct ntvfs_request *req, union smb_search_close *io)
1001 struct cvfs_private *private = ntvfs->private_data;
1003 SETUP_PID;
1005 return smb_raw_search_close(private->tree, io);
1009 a handler for async trans2 replies
1011 static void async_trans2(struct smbcli_request *c_req)
1013 struct async_info *async = c_req->async.private;
1014 struct ntvfs_request *req = async->req;
1015 req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
1016 talloc_free(async);
1017 req->async_states->send_fn(req);
1020 /* raw trans2 */
1021 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs,
1022 struct ntvfs_request *req,
1023 struct smb_trans2 *trans2)
1025 struct cvfs_private *private = ntvfs->private_data;
1026 struct smbcli_request *c_req;
1028 if (private->map_trans2) {
1029 return NT_STATUS_NOT_IMPLEMENTED;
1032 SETUP_PID;
1034 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1035 return smb_raw_trans2(private->tree, req, trans2);
1038 c_req = smb_raw_trans2_send(private->tree, trans2);
1040 ASYNC_RECV_TAIL(trans2, async_trans2);
1044 /* SMBtrans - not used on file shares */
1045 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs,
1046 struct ntvfs_request *req,
1047 struct smb_trans2 *trans2)
1049 return NT_STATUS_ACCESS_DENIED;
1053 a handler for async change notify replies
1055 static void async_changenotify(struct smbcli_request *c_req)
1057 struct async_info *async = c_req->async.private;
1058 struct ntvfs_request *req = async->req;
1059 req->async_states->status = smb_raw_changenotify_recv(c_req, req, async->parms);
1060 talloc_free(async);
1061 req->async_states->send_fn(req);
1064 /* change notify request - always async */
1065 static NTSTATUS cvfs_notify(struct ntvfs_module_context *ntvfs,
1066 struct ntvfs_request *req,
1067 union smb_notify *io)
1069 struct cvfs_private *private = ntvfs->private_data;
1070 struct smbcli_request *c_req;
1071 int saved_timeout = private->transport->options.request_timeout;
1072 struct cvfs_file *f;
1074 if (io->nttrans.level != RAW_NOTIFY_NTTRANS) {
1075 return NT_STATUS_NOT_IMPLEMENTED;
1078 SETUP_PID;
1080 f = ntvfs_handle_get_backend_data(io->nttrans.in.file.ntvfs, ntvfs);
1081 if (!f) return NT_STATUS_INVALID_HANDLE;
1082 io->nttrans.in.file.fnum = f->fnum;
1084 /* this request doesn't make sense unless its async */
1085 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1086 return NT_STATUS_INVALID_PARAMETER;
1089 /* we must not timeout on notify requests - they wait
1090 forever */
1091 private->transport->options.request_timeout = 0;
1093 c_req = smb_raw_changenotify_send(private->tree, io);
1095 private->transport->options.request_timeout = saved_timeout;
1097 ASYNC_RECV_TAIL(io, async_changenotify);
1101 initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
1103 NTSTATUS ntvfs_cifs_init(void)
1105 NTSTATUS ret;
1106 struct ntvfs_ops ops;
1107 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1109 ZERO_STRUCT(ops);
1111 /* fill in the name and type */
1112 ops.name = "cifs";
1113 ops.type = NTVFS_DISK;
1115 /* fill in all the operations */
1116 ops.connect = cvfs_connect;
1117 ops.disconnect = cvfs_disconnect;
1118 ops.unlink = cvfs_unlink;
1119 ops.chkpath = cvfs_chkpath;
1120 ops.qpathinfo = cvfs_qpathinfo;
1121 ops.setpathinfo = cvfs_setpathinfo;
1122 ops.open = cvfs_open;
1123 ops.mkdir = cvfs_mkdir;
1124 ops.rmdir = cvfs_rmdir;
1125 ops.rename = cvfs_rename;
1126 ops.copy = cvfs_copy;
1127 ops.ioctl = cvfs_ioctl;
1128 ops.read = cvfs_read;
1129 ops.write = cvfs_write;
1130 ops.seek = cvfs_seek;
1131 ops.flush = cvfs_flush;
1132 ops.close = cvfs_close;
1133 ops.exit = cvfs_exit;
1134 ops.lock = cvfs_lock;
1135 ops.setfileinfo = cvfs_setfileinfo;
1136 ops.qfileinfo = cvfs_qfileinfo;
1137 ops.fsinfo = cvfs_fsinfo;
1138 ops.lpq = cvfs_lpq;
1139 ops.search_first = cvfs_search_first;
1140 ops.search_next = cvfs_search_next;
1141 ops.search_close = cvfs_search_close;
1142 ops.trans = cvfs_trans;
1143 ops.logoff = cvfs_logoff;
1144 ops.async_setup = cvfs_async_setup;
1145 ops.cancel = cvfs_cancel;
1146 ops.notify = cvfs_notify;
1147 ops.trans2 = cvfs_trans2;
1149 /* register ourselves with the NTVFS subsystem. We register
1150 under the name 'cifs'. */
1151 ret = ntvfs_register(&ops, &vers);
1153 if (!NT_STATUS_IS_OK(ret)) {
1154 DEBUG(0,("Failed to register CIFS backend!\n"));
1157 return ret;