Add SMB_QFS_PROXY_INFO to identify WAFS proxy capabilities
[Samba/vfs_proxy.git] / source4 / ntvfs / proxy / vfs_proxy.c
blob3d1bfbd63ff0ca76c65ad5b09546c059585a7145
1 /*
2 Unix SMB/PROXY implementation.
4 CIFS PROXY NTVFS filesystem backend
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) James J Myers 2003 <myersjj@samba.org>
8 Copyright (C) Sam Liddicott <sam@liddicott.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this implements a CIFS->CIFS NTVFS filesystem caching proxy.
28 #include "includes.h"
29 #include "libcli/raw/libcliraw.h"
30 #include "libcli/raw/raw_proto.h"
31 #include "libcli/smb_composite/smb_composite.h"
32 #include "auth/auth.h"
33 #include "auth/credentials/credentials.h"
34 #include "ntvfs/ntvfs.h"
35 #include "../lib/util/dlinklist.h"
36 #include "param/param.h"
37 #include "libcli/resolve/resolve.h"
39 struct proxy_file {
40 struct proxy_file *prev, *next;
41 uint16_t fnum;
42 struct ntvfs_handle *h;
45 /* this is stored in ntvfs_private */
46 struct proxy_private {
47 struct smbcli_tree *tree;
48 struct smbcli_transport *transport;
49 struct ntvfs_module_context *ntvfs;
50 struct async_info *pending;
51 struct proxy_file *files;
52 bool map_generic;
53 bool map_trans2;
57 /* a structure used to pass information to an async handler */
58 struct async_info {
59 struct async_info *next, *prev;
60 struct proxy_private *proxy;
61 struct ntvfs_request *req;
62 struct smbcli_request *c_req;
63 struct proxy_file *f;
64 void *parms;
67 #define SETUP_PID private->tree->session->pid = req->smbpid
69 #define SETUP_FILE_HERE(f) do { \
70 f = ntvfs_handle_get_backend_data(io->generic.in.file.ntvfs, ntvfs); \
71 if (!f) return NT_STATUS_INVALID_HANDLE; \
72 io->generic.in.file.fnum = f->fnum; \
73 } while (0)
75 #define SETUP_FILE do { \
76 struct proxy_file *f; \
77 SETUP_FILE_HERE(f); \
78 } while (0)
80 #define SETUP_PID_AND_FILE do { \
81 SETUP_PID; \
82 SETUP_FILE; \
83 } while (0)
85 #define PROXY_SERVER "proxy:server"
86 #define PROXY_USER "proxy:user"
87 #define PROXY_PASSWORD "proxy:password"
88 #define PROXY_DOMAIN "proxy:domain"
89 #define PROXY_SHARE "proxy:share"
90 #define PROXY_USE_MACHINE_ACCT "proxy:use-machine-account"
91 #define PROXY_MAP_GENERIC "proxy:map-generic"
92 #define PROXY_MAP_TRANS2 "proxy:map-trans2"
94 #define PROXY_CACHE_ENABLED "proxy:cache-enabled"
95 #define PROXY_CACHE_ENABLED_DEFAULT false
97 #define PROXY_CACHE_READAHEAD "proxy:cache-readahead"
98 #define PROXY_CACHE_READAHEAD_DEFAULT 32768
99 /* size of each read-ahead request. */
100 #define PROXY_CACHE_READAHEAD_BLOCK "proxy:cache-readaheadblock"
101 #define PROXY_CACHE_READAHEAD_BLOCK_DEFAULT 32768
103 #define PROXY_USE_MACHINE_ACCT_DEFAULT false
104 #define PROXY_MAP_GENERIC_DEFAULT false
105 #define PROXY_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 proxy_private *private = p_private;
114 NTSTATUS status;
115 struct ntvfs_handle *h = NULL;
116 struct proxy_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_proxy: ignoring oplock break level %d for fnum %d\n", level, fnum));
126 return true;
129 DEBUG(5,("vfs_proxy: 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 proxy_connect(struct ntvfs_module_context *ntvfs,
139 struct ntvfs_request *req, const char *sharename)
141 NTSTATUS status;
142 struct proxy_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 proxy.
153 * Later we will use security=server and auth_server.c.
155 host = share_string_option(scfg, PROXY_SERVER, NULL);
156 user = share_string_option(scfg, PROXY_USER, NULL);
157 pass = share_string_option(scfg, PROXY_PASSWORD, NULL);
158 domain = share_string_option(scfg, PROXY_DOMAIN, NULL);
159 remote_share = share_string_option(scfg, PROXY_SHARE, NULL);
160 if (!remote_share) {
161 remote_share = sharename;
164 machine_account = share_bool_option(scfg, PROXY_USE_MACHINE_ACCT, PROXY_USE_MACHINE_ACCT_DEFAULT);
166 private = talloc_zero(ntvfs, struct proxy_private);
167 if (!private) {
168 return NT_STATUS_NO_MEMORY;
171 ntvfs->private_data = private;
173 if (!host) {
174 DEBUG(1,("PROXY backend: You must supply server\n"));
175 return NT_STATUS_INVALID_PARAMETER;
178 if (user && pass) {
179 DEBUG(5, ("PROXY 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, ("PROXY 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, ("PROXY backend: Using delegated credentials\n"));
203 credentials = req->session_info->credentials;
204 } else {
205 DEBUG(1,("PROXY 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, PROXY_MAP_GENERIC, PROXY_MAP_GENERIC_DEFAULT);
250 private->map_trans2 = share_bool_option(scfg, PROXY_MAP_TRANS2, PROXY_MAP_TRANS2_DEFAULT);
252 return NT_STATUS_OK;
256 disconnect from a share
258 static NTSTATUS proxy_disconnect(struct ntvfs_module_context *ntvfs)
260 struct proxy_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->proxy->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->proxy = 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 PROXY wildcards, but rarely does (except with OS/2 clients)
329 static NTSTATUS proxy_unlink(struct ntvfs_module_context *ntvfs,
330 struct ntvfs_request *req, union smb_unlink *unl)
332 struct proxy_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 proxy_ioctl(struct ntvfs_module_context *ntvfs,
364 struct ntvfs_request *req, union smb_ioctl *io)
366 struct proxy_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 proxy_chkpath(struct ntvfs_module_context *ntvfs,
386 struct ntvfs_request *req, union smb_chkpath *cp)
388 struct proxy_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 proxy_qpathinfo(struct ntvfs_module_context *ntvfs,
418 struct ntvfs_request *req, union smb_fileinfo *info)
420 struct proxy_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 proxy_qfileinfo(struct ntvfs_module_context *ntvfs,
450 struct ntvfs_request *req, union smb_fileinfo *io)
452 struct proxy_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 proxy_setpathinfo(struct ntvfs_module_context *ntvfs,
471 struct ntvfs_request *req, union smb_setfileinfo *st)
473 struct proxy_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 proxy_private *proxy = async->proxy;
495 struct ntvfs_request *req = async->req;
496 struct proxy_file *f = async->f;
497 union smb_open *io = async->parms;
498 union smb_handle *file;
500 talloc_free(async);
501 req->async_states->status = smb_raw_open_recv(c_req, req, io);
502 SMB_OPEN_OUT_FILE(io, file);
503 f->fnum = file->fnum;
504 file->ntvfs = NULL;
505 if (!NT_STATUS_IS_OK(req->async_states->status)) goto failed;
506 req->async_states->status = ntvfs_handle_set_backend_data(f->h, proxy->ntvfs, f);
507 if (!NT_STATUS_IS_OK(req->async_states->status)) goto failed;
508 file->ntvfs = f->h;
509 DLIST_ADD(proxy->files, f);
510 failed:
511 req->async_states->send_fn(req);
515 open a file
517 static NTSTATUS proxy_open(struct ntvfs_module_context *ntvfs,
518 struct ntvfs_request *req, union smb_open *io)
520 struct proxy_private *private = ntvfs->private_data;
521 struct smbcli_request *c_req;
522 struct ntvfs_handle *h;
523 struct proxy_file *f;
524 NTSTATUS status;
526 SETUP_PID;
528 if (io->generic.level != RAW_OPEN_GENERIC &&
529 private->map_generic) {
530 return ntvfs_map_open(ntvfs, req, io);
533 status = ntvfs_handle_new(ntvfs, req, &h);
534 NT_STATUS_NOT_OK_RETURN(status);
536 f = talloc_zero(h, struct proxy_file);
537 NT_STATUS_HAVE_NO_MEMORY(f);
538 f->h = h;
540 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
541 union smb_handle *file;
543 status = smb_raw_open(private->tree, req, io);
544 NT_STATUS_NOT_OK_RETURN(status);
546 SMB_OPEN_OUT_FILE(io, file);
547 f->fnum = file->fnum;
548 file->ntvfs = NULL;
549 status = ntvfs_handle_set_backend_data(f->h, private->ntvfs, f);
550 NT_STATUS_NOT_OK_RETURN(status);
551 file->ntvfs = f->h;
552 DLIST_ADD(private->files, f);
554 return NT_STATUS_OK;
557 c_req = smb_raw_open_send(private->tree, io);
559 ASYNC_RECV_TAIL_F(io, async_open, f);
563 create a directory
565 static NTSTATUS proxy_mkdir(struct ntvfs_module_context *ntvfs,
566 struct ntvfs_request *req, union smb_mkdir *md)
568 struct proxy_private *private = ntvfs->private_data;
569 struct smbcli_request *c_req;
571 SETUP_PID;
573 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
574 return smb_raw_mkdir(private->tree, md);
577 c_req = smb_raw_mkdir_send(private->tree, md);
579 SIMPLE_ASYNC_TAIL;
583 remove a directory
585 static NTSTATUS proxy_rmdir(struct ntvfs_module_context *ntvfs,
586 struct ntvfs_request *req, struct smb_rmdir *rd)
588 struct proxy_private *private = ntvfs->private_data;
589 struct smbcli_request *c_req;
591 SETUP_PID;
593 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
594 return smb_raw_rmdir(private->tree, rd);
596 c_req = smb_raw_rmdir_send(private->tree, rd);
598 SIMPLE_ASYNC_TAIL;
602 rename a set of files
604 static NTSTATUS proxy_rename(struct ntvfs_module_context *ntvfs,
605 struct ntvfs_request *req, union smb_rename *ren)
607 struct proxy_private *private = ntvfs->private_data;
608 struct smbcli_request *c_req;
610 SETUP_PID;
612 if (ren->nttrans.level == RAW_RENAME_NTTRANS) {
613 struct proxy_file *f;
614 f = ntvfs_handle_get_backend_data(ren->nttrans.in.file.ntvfs, ntvfs);
615 if (!f) return NT_STATUS_INVALID_HANDLE;
616 ren->nttrans.in.file.fnum = f->fnum;
619 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
620 return smb_raw_rename(private->tree, ren);
623 c_req = smb_raw_rename_send(private->tree, ren);
625 SIMPLE_ASYNC_TAIL;
629 copy a set of files
631 static NTSTATUS proxy_copy(struct ntvfs_module_context *ntvfs,
632 struct ntvfs_request *req, struct smb_copy *cp)
634 return NT_STATUS_NOT_SUPPORTED;
638 a handler for async read replies
640 static void async_read(struct smbcli_request *c_req)
642 struct async_info *async = c_req->async.private;
643 struct ntvfs_request *req = async->req;
644 req->async_states->status = smb_raw_read_recv(c_req, async->parms);
645 talloc_free(async);
646 req->async_states->send_fn(req);
650 read from a file
652 static NTSTATUS proxy_read(struct ntvfs_module_context *ntvfs,
653 struct ntvfs_request *req, union smb_read *io)
655 struct proxy_private *private = ntvfs->private_data;
656 struct smbcli_request *c_req;
658 SETUP_PID;
660 if (io->generic.level != RAW_READ_GENERIC &&
661 private->map_generic) {
662 return ntvfs_map_read(ntvfs, req, io);
665 SETUP_FILE;
667 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
668 return smb_raw_read(private->tree, io);
671 c_req = smb_raw_read_send(private->tree, io);
673 ASYNC_RECV_TAIL(io, async_read);
677 a handler for async write replies
679 static void async_write(struct smbcli_request *c_req)
681 struct async_info *async = c_req->async.private;
682 struct ntvfs_request *req = async->req;
683 req->async_states->status = smb_raw_write_recv(c_req, async->parms);
684 talloc_free(async);
685 req->async_states->send_fn(req);
689 write to a file
691 static NTSTATUS proxy_write(struct ntvfs_module_context *ntvfs,
692 struct ntvfs_request *req, union smb_write *io)
694 struct proxy_private *private = ntvfs->private_data;
695 struct smbcli_request *c_req;
697 SETUP_PID;
699 if (io->generic.level != RAW_WRITE_GENERIC &&
700 private->map_generic) {
701 return ntvfs_map_write(ntvfs, req, io);
703 SETUP_FILE;
705 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
706 return smb_raw_write(private->tree, io);
709 c_req = smb_raw_write_send(private->tree, io);
711 ASYNC_RECV_TAIL(io, async_write);
715 a handler for async seek replies
717 static void async_seek(struct smbcli_request *c_req)
719 struct async_info *async = c_req->async.private;
720 struct ntvfs_request *req = async->req;
721 req->async_states->status = smb_raw_seek_recv(c_req, async->parms);
722 talloc_free(async);
723 req->async_states->send_fn(req);
727 seek in a file
729 static NTSTATUS proxy_seek(struct ntvfs_module_context *ntvfs,
730 struct ntvfs_request *req,
731 union smb_seek *io)
733 struct proxy_private *private = ntvfs->private_data;
734 struct smbcli_request *c_req;
736 SETUP_PID_AND_FILE;
738 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
739 return smb_raw_seek(private->tree, io);
742 c_req = smb_raw_seek_send(private->tree, io);
744 ASYNC_RECV_TAIL(io, async_seek);
748 flush a file
750 static NTSTATUS proxy_flush(struct ntvfs_module_context *ntvfs,
751 struct ntvfs_request *req,
752 union smb_flush *io)
754 struct proxy_private *private = ntvfs->private_data;
755 struct smbcli_request *c_req;
757 SETUP_PID;
758 switch (io->generic.level) {
759 case RAW_FLUSH_FLUSH:
760 SETUP_FILE;
761 break;
762 case RAW_FLUSH_ALL:
763 io->generic.in.file.fnum = 0xFFFF;
764 break;
765 case RAW_FLUSH_SMB2:
766 return NT_STATUS_INVALID_LEVEL;
769 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
770 return smb_raw_flush(private->tree, io);
773 c_req = smb_raw_flush_send(private->tree, io);
775 SIMPLE_ASYNC_TAIL;
779 close a file
781 static NTSTATUS proxy_close(struct ntvfs_module_context *ntvfs,
782 struct ntvfs_request *req, union smb_close *io)
784 struct proxy_private *private = ntvfs->private_data;
785 struct smbcli_request *c_req;
786 struct proxy_file *f;
787 union smb_close io2;
789 SETUP_PID;
791 if (io->generic.level != RAW_CLOSE_GENERIC &&
792 private->map_generic) {
793 return ntvfs_map_close(ntvfs, req, io);
796 if (io->generic.level == RAW_CLOSE_GENERIC) {
797 ZERO_STRUCT(io2);
798 io2.close.level = RAW_CLOSE_CLOSE;
799 io2.close.in.file = io->generic.in.file;
800 io2.close.in.write_time = io->generic.in.write_time;
801 io = &io2;
804 SETUP_FILE_HERE(f);
805 /* Note, we aren't free-ing f, or it's h here. Should we?
806 even if file-close fails, we'll remove it from the list,
807 what else would we do? Maybe we should not remove until
808 after the proxied call completes? */
809 DLIST_REMOVE(private->files, f);
811 /* possibly samba can't do RAW_CLOSE_SEND yet */
812 if (! (c_req = smb_raw_close_send(private->tree, io))) {
813 if (io->generic.level == RAW_CLOSE_GENERIC) {
814 ZERO_STRUCT(io2);
815 io2.close.level = RAW_CLOSE_CLOSE;
816 io2.close.in.file = io->generic.in.file;
817 io2.close.in.write_time = io->generic.in.write_time;
818 io = &io2;
820 c_req = smb_raw_close_send(private->tree, io);
823 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
824 return smbcli_request_simple_recv(c_req);
827 SIMPLE_ASYNC_TAIL;
831 exit - closing files open by the pid
833 static NTSTATUS proxy_exit(struct ntvfs_module_context *ntvfs,
834 struct ntvfs_request *req)
836 struct proxy_private *private = ntvfs->private_data;
837 struct smbcli_request *c_req;
839 SETUP_PID;
841 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
842 return smb_raw_exit(private->tree->session);
845 c_req = smb_raw_exit_send(private->tree->session);
847 SIMPLE_ASYNC_TAIL;
851 logoff - closing files open by the user
853 static NTSTATUS proxy_logoff(struct ntvfs_module_context *ntvfs,
854 struct ntvfs_request *req)
856 /* we can't do this right in the proxy backend .... */
857 return NT_STATUS_OK;
861 setup for an async call - nothing to do yet
863 static NTSTATUS proxy_async_setup(struct ntvfs_module_context *ntvfs,
864 struct ntvfs_request *req,
865 void *private)
867 return NT_STATUS_OK;
871 cancel an async call
873 static NTSTATUS proxy_cancel(struct ntvfs_module_context *ntvfs,
874 struct ntvfs_request *req)
876 struct proxy_private *private = ntvfs->private_data;
877 struct async_info *a;
879 /* find the matching request */
880 for (a=private->pending;a;a=a->next) {
881 if (a->req == req) {
882 break;
886 if (a == NULL) {
887 return NT_STATUS_INVALID_PARAMETER;
890 return smb_raw_ntcancel(a->c_req);
894 lock a byte range
896 static NTSTATUS proxy_lock(struct ntvfs_module_context *ntvfs,
897 struct ntvfs_request *req, union smb_lock *io)
899 struct proxy_private *private = ntvfs->private_data;
900 struct smbcli_request *c_req;
902 SETUP_PID;
904 if (io->generic.level != RAW_LOCK_GENERIC &&
905 private->map_generic) {
906 return ntvfs_map_lock(ntvfs, req, io);
908 SETUP_FILE;
910 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
911 return smb_raw_lock(private->tree, io);
914 c_req = smb_raw_lock_send(private->tree, io);
915 SIMPLE_ASYNC_TAIL;
919 set info on a open file
921 static NTSTATUS proxy_setfileinfo(struct ntvfs_module_context *ntvfs,
922 struct ntvfs_request *req,
923 union smb_setfileinfo *io)
925 struct proxy_private *private = ntvfs->private_data;
926 struct smbcli_request *c_req;
928 SETUP_PID_AND_FILE;
930 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
931 return smb_raw_setfileinfo(private->tree, io);
933 c_req = smb_raw_setfileinfo_send(private->tree, io);
935 SIMPLE_ASYNC_TAIL;
940 a handler for async fsinfo replies
942 static void async_fsinfo(struct smbcli_request *c_req)
944 struct async_info *async = c_req->async.private;
945 struct ntvfs_request *req = async->req;
946 req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
947 talloc_free(async);
948 req->async_states->send_fn(req);
952 return filesystem space info
954 static NTSTATUS proxy_fsinfo(struct ntvfs_module_context *ntvfs,
955 struct ntvfs_request *req, union smb_fsinfo *fs)
957 struct proxy_private *private = ntvfs->private_data;
958 struct smbcli_request *c_req;
960 SETUP_PID;
962 /* QFS Proxy */
963 if (fs->generic.level == RAW_QFS_PROXY_INFO) {
964 fs->proxy_info.out.major_version=1;
965 fs->proxy_info.out.minor_version=0;
966 fs->proxy_info.out.capability=0;
967 return NT_STATUS_OK;
970 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
971 return smb_raw_fsinfo(private->tree, req, fs);
974 c_req = smb_raw_fsinfo_send(private->tree, req, fs);
976 ASYNC_RECV_TAIL(fs, async_fsinfo);
980 return print queue info
982 static NTSTATUS proxy_lpq(struct ntvfs_module_context *ntvfs,
983 struct ntvfs_request *req, union smb_lpq *lpq)
985 return NT_STATUS_NOT_SUPPORTED;
989 list files in a directory matching a wildcard pattern
991 static NTSTATUS proxy_search_first(struct ntvfs_module_context *ntvfs,
992 struct ntvfs_request *req, union smb_search_first *io,
993 void *search_private,
994 bool (*callback)(void *, const union smb_search_data *))
996 struct proxy_private *private = ntvfs->private_data;
998 SETUP_PID;
1000 return smb_raw_search_first(private->tree, req, io, search_private, callback);
1003 /* continue a search */
1004 static NTSTATUS proxy_search_next(struct ntvfs_module_context *ntvfs,
1005 struct ntvfs_request *req, union smb_search_next *io,
1006 void *search_private,
1007 bool (*callback)(void *, const union smb_search_data *))
1009 struct proxy_private *private = ntvfs->private_data;
1011 SETUP_PID;
1013 return smb_raw_search_next(private->tree, req, io, search_private, callback);
1016 /* close a search */
1017 static NTSTATUS proxy_search_close(struct ntvfs_module_context *ntvfs,
1018 struct ntvfs_request *req, union smb_search_close *io)
1020 struct proxy_private *private = ntvfs->private_data;
1022 SETUP_PID;
1024 return smb_raw_search_close(private->tree, io);
1028 a handler for async trans2 replies
1030 static void async_trans2(struct smbcli_request *c_req)
1032 struct async_info *async = c_req->async.private;
1033 struct ntvfs_request *req = async->req;
1034 req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
1035 talloc_free(async);
1036 req->async_states->send_fn(req);
1039 /* raw trans2 */
1040 static NTSTATUS proxy_trans2(struct ntvfs_module_context *ntvfs,
1041 struct ntvfs_request *req,
1042 struct smb_trans2 *trans2)
1044 struct proxy_private *private = ntvfs->private_data;
1045 struct smbcli_request *c_req;
1047 if (private->map_trans2) {
1048 return NT_STATUS_NOT_IMPLEMENTED;
1051 SETUP_PID;
1053 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1054 return smb_raw_trans2(private->tree, req, trans2);
1057 c_req = smb_raw_trans2_send(private->tree, trans2);
1059 ASYNC_RECV_TAIL(trans2, async_trans2);
1063 /* SMBtrans - not used on file shares */
1064 static NTSTATUS proxy_trans(struct ntvfs_module_context *ntvfs,
1065 struct ntvfs_request *req,
1066 struct smb_trans2 *trans2)
1068 return NT_STATUS_ACCESS_DENIED;
1072 a handler for async change notify replies
1074 static void async_changenotify(struct smbcli_request *c_req)
1076 struct async_info *async = c_req->async.private;
1077 struct ntvfs_request *req = async->req;
1078 req->async_states->status = smb_raw_changenotify_recv(c_req, req, async->parms);
1079 talloc_free(async);
1080 req->async_states->send_fn(req);
1083 /* change notify request - always async */
1084 static NTSTATUS proxy_notify(struct ntvfs_module_context *ntvfs,
1085 struct ntvfs_request *req,
1086 union smb_notify *io)
1088 struct proxy_private *private = ntvfs->private_data;
1089 struct smbcli_request *c_req;
1090 int saved_timeout = private->transport->options.request_timeout;
1091 struct proxy_file *f;
1093 if (io->nttrans.level != RAW_NOTIFY_NTTRANS) {
1094 return NT_STATUS_NOT_IMPLEMENTED;
1097 SETUP_PID;
1099 f = ntvfs_handle_get_backend_data(io->nttrans.in.file.ntvfs, ntvfs);
1100 if (!f) return NT_STATUS_INVALID_HANDLE;
1101 io->nttrans.in.file.fnum = f->fnum;
1103 /* this request doesn't make sense unless its async */
1104 if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1105 return NT_STATUS_INVALID_PARAMETER;
1108 /* we must not timeout on notify requests - they wait
1109 forever */
1110 private->transport->options.request_timeout = 0;
1112 c_req = smb_raw_changenotify_send(private->tree, io);
1114 private->transport->options.request_timeout = saved_timeout;
1116 ASYNC_RECV_TAIL(io, async_changenotify);
1120 initialise the PROXY->PROXY backend, registering ourselves with the ntvfs subsystem
1122 NTSTATUS ntvfs_proxy_init(void)
1124 NTSTATUS ret;
1125 struct ntvfs_ops ops;
1126 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1128 ZERO_STRUCT(ops);
1130 /* fill in the name and type */
1131 ops.name = "proxy";
1132 ops.type = NTVFS_DISK;
1134 /* fill in all the operations */
1135 ops.connect = proxy_connect;
1136 ops.disconnect = proxy_disconnect;
1137 ops.unlink = proxy_unlink;
1138 ops.chkpath = proxy_chkpath;
1139 ops.qpathinfo = proxy_qpathinfo;
1140 ops.setpathinfo = proxy_setpathinfo;
1141 ops.open = proxy_open;
1142 ops.mkdir = proxy_mkdir;
1143 ops.rmdir = proxy_rmdir;
1144 ops.rename = proxy_rename;
1145 ops.copy = proxy_copy;
1146 ops.ioctl = proxy_ioctl;
1147 ops.read = proxy_read;
1148 ops.write = proxy_write;
1149 ops.seek = proxy_seek;
1150 ops.flush = proxy_flush;
1151 ops.close = proxy_close;
1152 ops.exit = proxy_exit;
1153 ops.lock = proxy_lock;
1154 ops.setfileinfo = proxy_setfileinfo;
1155 ops.qfileinfo = proxy_qfileinfo;
1156 ops.fsinfo = proxy_fsinfo;
1157 ops.lpq = proxy_lpq;
1158 ops.search_first = proxy_search_first;
1159 ops.search_next = proxy_search_next;
1160 ops.search_close = proxy_search_close;
1161 ops.trans = proxy_trans;
1162 ops.logoff = proxy_logoff;
1163 ops.async_setup = proxy_async_setup;
1164 ops.cancel = proxy_cancel;
1165 ops.notify = proxy_notify;
1166 ops.trans2 = proxy_trans2;
1168 /* register ourselves with the NTVFS subsystem. We register
1169 under the name 'proxy'. */
1170 ret = ntvfs_register(&ops, &vers);
1172 if (!NT_STATUS_IS_OK(ret)) {
1173 DEBUG(0,("Failed to register PROXY backend!\n"));
1176 return ret;