Merge branch 'master' of ssh://git.samba.org/data/git/samba
[ira/wip.git] / source4 / ntvfs / cifs / vfs_cifs.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    CIFS-on-CIFS NTVFS filesystem backend
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) James J Myers 2003 <myersjj@samba.org>
8
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.
13    
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.
18    
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/>.
21 */
22 /*
23   this implements a CIFS->CIFS NTVFS filesystem backend. 
24   
25 */
26
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"
37
38 struct cvfs_file {
39         struct cvfs_file *prev, *next;
40         uint16_t fnum;
41         struct ntvfs_handle *h;
42 };
43
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;
53 };
54
55
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;
64 };
65
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)
72
73 #define SETUP_PID do { \
74         private->tree->session->pid = req->smbpid; \
75         CHECK_UPSTREAM_OPEN; \
76 } while(0)
77
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)
83
84 #define SETUP_FILE do { \
85         struct cvfs_file *f; \
86         SETUP_FILE_HERE(f); \
87 } while (0)
88
89 #define SETUP_PID_AND_FILE do { \
90         SETUP_PID; \
91         SETUP_FILE; \
92 } while (0)
93
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"
102
103 #define CIFS_USE_MACHINE_ACCT_DEFAULT   false
104 #define CIFS_MAP_GENERIC_DEFAULT        false
105 #define CIFS_MAP_TRANS2_DEFAULT         true
106
107 /*
108   a handler for oplock break events from the server - these need to be passed
109   along to the client
110  */
111 static bool oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *p_private)
112 {
113         struct cvfs_private *private = p_private;
114         NTSTATUS status;
115         struct ntvfs_handle *h = NULL;
116         struct cvfs_file *f;
117
118         for (f=private->files; f; f=f->next) {
119                 if (f->fnum != fnum) continue;
120                 h = f->h;
121                 break;
122         }
123
124         if (!h) {
125                 DEBUG(5,("vfs_cifs: ignoring oplock break level %d for fnum %d\n", level, fnum));
126                 return true;
127         }
128
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;
133 }
134
135 /*
136   connect to a share - used when a tree_connect operation comes in.
137 */
138 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs, 
139                              struct ntvfs_request *req, const char *sharename)
140 {
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;
147
148         struct cli_credentials *credentials;
149         bool machine_account;
150
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.
154          */
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;
162         }
163
164         machine_account = share_bool_option(scfg, CIFS_USE_MACHINE_ACCT, CIFS_USE_MACHINE_ACCT_DEFAULT);
165
166         private = talloc_zero(ntvfs, struct cvfs_private);
167         if (!private) {
168                 return NT_STATUS_NO_MEMORY;
169         }
170
171         ntvfs->private_data = private;
172
173         if (!host) {
174                 DEBUG(1,("CIFS backend: You must supply server\n"));
175                 return NT_STATUS_INVALID_PARAMETER;
176         } 
177         
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;
183                 }
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);
188                 }
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);
196                 }
197                 status = cli_credentials_set_machine_account(credentials, ntvfs->ctx->lp_ctx);
198                 if (!NT_STATUS_IS_OK(status)) {
199                         return status;
200                 }
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;
207         }
208
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);
223
224         if (!(ntvfs->ctx->client_caps & NTVFS_CLIENT_CAP_LEVEL_II_OPLOCKS)) {
225                 io.in.options.use_level2_oplocks = false;
226         }
227
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);
233
234         private->tree = io.out.tree;
235
236         private->transport = private->tree->session->transport;
237         SETUP_PID;
238         private->ntvfs = ntvfs;
239
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);
244
245         /* we need to receive oplock break requests from the server */
246         smbcli_oplock_handler(private->transport, oplock_handler, private);
247
248         private->map_generic = share_bool_option(scfg, CIFS_MAP_GENERIC, CIFS_MAP_GENERIC_DEFAULT);
249
250         private->map_trans2 = share_bool_option(scfg, CIFS_MAP_TRANS2, CIFS_MAP_TRANS2_DEFAULT);
251
252         return NT_STATUS_OK;
253 }
254
255 /*
256   disconnect from a share
257 */
258 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs)
259 {
260         struct cvfs_private *private = ntvfs->private_data;
261         struct async_info *a, *an;
262
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);
268         }
269
270         talloc_free(private);
271         ntvfs->private_data = NULL;
272
273         return NT_STATUS_OK;
274 }
275
276 /*
277   destroy an async info structure
278 */
279 static int async_info_destructor(struct async_info *async)
280 {
281         DLIST_REMOVE(async->cvfs->pending, async);
282         return 0;
283 }
284
285 /*
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)
289  */
290 static void async_simple(struct smbcli_request *c_req)
291 {
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);
297 }
298
299
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; \
303         { \
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); \
315         } \
316         c_req->async.fn = async_fn; \
317         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
318         return NT_STATUS_OK; \
319 } while (0)
320
321 #define ASYNC_RECV_TAIL(io, async_fn) ASYNC_RECV_TAIL_F(io, async_fn, NULL)
322
323 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
324
325 /*
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)
328 */
329 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs, 
330                             struct ntvfs_request *req, union smb_unlink *unl)
331 {
332         struct cvfs_private *private = ntvfs->private_data;
333         struct smbcli_request *c_req;
334
335         SETUP_PID;
336
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);
341         }
342
343         c_req = smb_raw_unlink_send(private->tree, unl);
344
345         SIMPLE_ASYNC_TAIL;
346 }
347
348 /*
349   a handler for async ioctl replies
350  */
351 static void async_ioctl(struct smbcli_request *c_req)
352 {
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);
358 }
359
360 /*
361   ioctl interface
362 */
363 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs, 
364                            struct ntvfs_request *req, union smb_ioctl *io)
365 {
366         struct cvfs_private *private = ntvfs->private_data;
367         struct smbcli_request *c_req;
368
369         SETUP_PID_AND_FILE;
370
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);
375         }
376
377         c_req = smb_raw_ioctl_send(private->tree, io);
378
379         ASYNC_RECV_TAIL(io, async_ioctl);
380 }
381
382 /*
383   check if a directory exists
384 */
385 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs, 
386                              struct ntvfs_request *req, union smb_chkpath *cp)
387 {
388         struct cvfs_private *private = ntvfs->private_data;
389         struct smbcli_request *c_req;
390
391         SETUP_PID;
392
393         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
394                 return smb_raw_chkpath(private->tree, cp);
395         }
396
397         c_req = smb_raw_chkpath_send(private->tree, cp);
398
399         SIMPLE_ASYNC_TAIL;
400 }
401
402 /*
403   a handler for async qpathinfo replies
404  */
405 static void async_qpathinfo(struct smbcli_request *c_req)
406 {
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);
412 }
413
414 /*
415   return info on a pathname
416 */
417 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs, 
418                                struct ntvfs_request *req, union smb_fileinfo *info)
419 {
420         struct cvfs_private *private = ntvfs->private_data;
421         struct smbcli_request *c_req;
422
423         SETUP_PID;
424
425         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
426                 return smb_raw_pathinfo(private->tree, req, info);
427         }
428
429         c_req = smb_raw_pathinfo_send(private->tree, info);
430
431         ASYNC_RECV_TAIL(info, async_qpathinfo);
432 }
433
434 /*
435   a handler for async qfileinfo replies
436  */
437 static void async_qfileinfo(struct smbcli_request *c_req)
438 {
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);
444 }
445
446 /*
447   query info on a open file
448 */
449 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs, 
450                                struct ntvfs_request *req, union smb_fileinfo *io)
451 {
452         struct cvfs_private *private = ntvfs->private_data;
453         struct smbcli_request *c_req;
454
455         SETUP_PID_AND_FILE;
456
457         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
458                 return smb_raw_fileinfo(private->tree, req, io);
459         }
460
461         c_req = smb_raw_fileinfo_send(private->tree, io);
462
463         ASYNC_RECV_TAIL(io, async_qfileinfo);
464 }
465
466
467 /*
468   set info on a pathname
469 */
470 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs, 
471                                  struct ntvfs_request *req, union smb_setfileinfo *st)
472 {
473         struct cvfs_private *private = ntvfs->private_data;
474         struct smbcli_request *c_req;
475
476         SETUP_PID;
477
478         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
479                 return smb_raw_setpathinfo(private->tree, st);
480         }
481
482         c_req = smb_raw_setpathinfo_send(private->tree, st);
483
484         SIMPLE_ASYNC_TAIL;
485 }
486
487
488 /*
489   a handler for async open replies
490  */
491 static void async_open(struct smbcli_request *c_req)
492 {
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);
511 }
512
513 /*
514   open a file
515 */
516 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs, 
517                           struct ntvfs_request *req, union smb_open *io)
518 {
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;
524
525         SETUP_PID;
526
527         if (io->generic.level != RAW_OPEN_GENERIC &&
528             private->map_generic) {
529                 return ntvfs_map_open(ntvfs, req, io);
530         }
531
532         status = ntvfs_handle_new(ntvfs, req, &h);
533         NT_STATUS_NOT_OK_RETURN(status);
534
535         f = talloc_zero(h, struct cvfs_file);
536         NT_STATUS_HAVE_NO_MEMORY(f);
537         f->h = h;
538
539         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
540                 union smb_handle *file;
541
542                 status = smb_raw_open(private->tree, req, io);
543                 NT_STATUS_NOT_OK_RETURN(status);
544
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);
552
553                 return NT_STATUS_OK;
554         }
555
556         c_req = smb_raw_open_send(private->tree, io);
557
558         ASYNC_RECV_TAIL_F(io, async_open, f);
559 }
560
561 /*
562   create a directory
563 */
564 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs, 
565                            struct ntvfs_request *req, union smb_mkdir *md)
566 {
567         struct cvfs_private *private = ntvfs->private_data;
568         struct smbcli_request *c_req;
569
570         SETUP_PID;
571
572         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
573                 return smb_raw_mkdir(private->tree, md);
574         }
575
576         c_req = smb_raw_mkdir_send(private->tree, md);
577
578         SIMPLE_ASYNC_TAIL;
579 }
580
581 /*
582   remove a directory
583 */
584 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs, 
585                            struct ntvfs_request *req, struct smb_rmdir *rd)
586 {
587         struct cvfs_private *private = ntvfs->private_data;
588         struct smbcli_request *c_req;
589
590         SETUP_PID;
591
592         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
593                 return smb_raw_rmdir(private->tree, rd);
594         }
595         c_req = smb_raw_rmdir_send(private->tree, rd);
596
597         SIMPLE_ASYNC_TAIL;
598 }
599
600 /*
601   rename a set of files
602 */
603 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs, 
604                             struct ntvfs_request *req, union smb_rename *ren)
605 {
606         struct cvfs_private *private = ntvfs->private_data;
607         struct smbcli_request *c_req;
608
609         SETUP_PID;
610
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;
616         }
617
618         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
619                 return smb_raw_rename(private->tree, ren);
620         }
621
622         c_req = smb_raw_rename_send(private->tree, ren);
623
624         SIMPLE_ASYNC_TAIL;
625 }
626
627 /*
628   copy a set of files
629 */
630 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs, 
631                           struct ntvfs_request *req, struct smb_copy *cp)
632 {
633         return NT_STATUS_NOT_SUPPORTED;
634 }
635
636 /*
637   a handler for async read replies
638  */
639 static void async_read(struct smbcli_request *c_req)
640 {
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);
646 }
647
648 /*
649   read from a file
650 */
651 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs, 
652                           struct ntvfs_request *req, union smb_read *io)
653 {
654         struct cvfs_private *private = ntvfs->private_data;
655         struct smbcli_request *c_req;
656
657         SETUP_PID;
658
659         if (io->generic.level != RAW_READ_GENERIC &&
660             private->map_generic) {
661                 return ntvfs_map_read(ntvfs, req, io);
662         }
663
664         SETUP_FILE;
665
666         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
667                 return smb_raw_read(private->tree, io);
668         }
669
670         c_req = smb_raw_read_send(private->tree, io);
671
672         ASYNC_RECV_TAIL(io, async_read);
673 }
674
675 /*
676   a handler for async write replies
677  */
678 static void async_write(struct smbcli_request *c_req)
679 {
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);
685 }
686
687 /*
688   write to a file
689 */
690 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs, 
691                            struct ntvfs_request *req, union smb_write *io)
692 {
693         struct cvfs_private *private = ntvfs->private_data;
694         struct smbcli_request *c_req;
695
696         SETUP_PID;
697
698         if (io->generic.level != RAW_WRITE_GENERIC &&
699             private->map_generic) {
700                 return ntvfs_map_write(ntvfs, req, io);
701         }
702         SETUP_FILE;
703
704         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
705                 return smb_raw_write(private->tree, io);
706         }
707
708         c_req = smb_raw_write_send(private->tree, io);
709
710         ASYNC_RECV_TAIL(io, async_write);
711 }
712
713 /*
714   a handler for async seek replies
715  */
716 static void async_seek(struct smbcli_request *c_req)
717 {
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);
723 }
724
725 /*
726   seek in a file
727 */
728 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs, 
729                           struct ntvfs_request *req,
730                           union smb_seek *io)
731 {
732         struct cvfs_private *private = ntvfs->private_data;
733         struct smbcli_request *c_req;
734
735         SETUP_PID_AND_FILE;
736
737         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
738                 return smb_raw_seek(private->tree, io);
739         }
740
741         c_req = smb_raw_seek_send(private->tree, io);
742
743         ASYNC_RECV_TAIL(io, async_seek);
744 }
745
746 /*
747   flush a file
748 */
749 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs, 
750                            struct ntvfs_request *req,
751                            union smb_flush *io)
752 {
753         struct cvfs_private *private = ntvfs->private_data;
754         struct smbcli_request *c_req;
755
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;
766         }
767
768         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
769                 return smb_raw_flush(private->tree, io);
770         }
771
772         c_req = smb_raw_flush_send(private->tree, io);
773
774         SIMPLE_ASYNC_TAIL;
775 }
776
777 /*
778   close a file
779 */
780 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs, 
781                            struct ntvfs_request *req, union smb_close *io)
782 {
783         struct cvfs_private *private = ntvfs->private_data;
784         struct smbcli_request *c_req;
785         struct cvfs_file *f;
786         union smb_close io2;
787
788         SETUP_PID;
789
790         if (io->generic.level != RAW_CLOSE_GENERIC &&
791             private->map_generic) {
792                 return ntvfs_map_close(ntvfs, req, io);
793         }
794
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;
801         }
802
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);
809
810         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
811                 return smb_raw_close(private->tree, io);
812         }
813
814         c_req = smb_raw_close_send(private->tree, io);
815
816         SIMPLE_ASYNC_TAIL;
817 }
818
819 /*
820   exit - closing files open by the pid
821 */
822 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs, 
823                           struct ntvfs_request *req)
824 {
825         struct cvfs_private *private = ntvfs->private_data;
826         struct smbcli_request *c_req;
827
828         SETUP_PID;
829
830         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
831                 return smb_raw_exit(private->tree->session);
832         }
833
834         c_req = smb_raw_exit_send(private->tree->session);
835
836         SIMPLE_ASYNC_TAIL;
837 }
838
839 /*
840   logoff - closing files open by the user
841 */
842 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs, 
843                             struct ntvfs_request *req)
844 {
845         /* we can't do this right in the cifs backend .... */
846         return NT_STATUS_OK;
847 }
848
849 /*
850   setup for an async call - nothing to do yet
851 */
852 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs, 
853                                  struct ntvfs_request *req, 
854                                  void *private)
855 {
856         return NT_STATUS_OK;
857 }
858
859 /*
860   cancel an async call
861 */
862 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs, 
863                             struct ntvfs_request *req)
864 {
865         struct cvfs_private *private = ntvfs->private_data;
866         struct async_info *a;
867
868         /* find the matching request */
869         for (a=private->pending;a;a=a->next) {
870                 if (a->req == req) {
871                         break;
872                 }
873         }
874
875         if (a == NULL) {
876                 return NT_STATUS_INVALID_PARAMETER;
877         }
878
879         return smb_raw_ntcancel(a->c_req);
880 }
881
882 /*
883   lock a byte range
884 */
885 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs, 
886                           struct ntvfs_request *req, union smb_lock *io)
887 {
888         struct cvfs_private *private = ntvfs->private_data;
889         struct smbcli_request *c_req;
890
891         SETUP_PID;
892
893         if (io->generic.level != RAW_LOCK_GENERIC &&
894             private->map_generic) {
895                 return ntvfs_map_lock(ntvfs, req, io);
896         }
897         SETUP_FILE;
898
899         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
900                 return smb_raw_lock(private->tree, io);
901         }
902
903         c_req = smb_raw_lock_send(private->tree, io);
904         SIMPLE_ASYNC_TAIL;
905 }
906
907 /*
908   set info on a open file
909 */
910 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs, 
911                                  struct ntvfs_request *req, 
912                                  union smb_setfileinfo *io)
913 {
914         struct cvfs_private *private = ntvfs->private_data;
915         struct smbcli_request *c_req;
916
917         SETUP_PID_AND_FILE;
918
919         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
920                 return smb_raw_setfileinfo(private->tree, io);
921         }
922         c_req = smb_raw_setfileinfo_send(private->tree, io);
923
924         SIMPLE_ASYNC_TAIL;
925 }
926
927
928 /*
929   a handler for async fsinfo replies
930  */
931 static void async_fsinfo(struct smbcli_request *c_req)
932 {
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);
938 }
939
940 /*
941   return filesystem space info
942 */
943 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs, 
944                             struct ntvfs_request *req, union smb_fsinfo *fs)
945 {
946         struct cvfs_private *private = ntvfs->private_data;
947         struct smbcli_request *c_req;
948
949         SETUP_PID;
950
951         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
952                 return smb_raw_fsinfo(private->tree, req, fs);
953         }
954
955         c_req = smb_raw_fsinfo_send(private->tree, req, fs);
956
957         ASYNC_RECV_TAIL(fs, async_fsinfo);
958 }
959
960 /*
961   return print queue info
962 */
963 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs, 
964                          struct ntvfs_request *req, union smb_lpq *lpq)
965 {
966         return NT_STATUS_NOT_SUPPORTED;
967 }
968
969 /* 
970    list files in a directory matching a wildcard pattern
971 */
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 *))
976 {
977         struct cvfs_private *private = ntvfs->private_data;
978
979         SETUP_PID;
980
981         return smb_raw_search_first(private->tree, req, io, search_private, callback);
982 }
983
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 *))
989 {
990         struct cvfs_private *private = ntvfs->private_data;
991
992         SETUP_PID;
993
994         return smb_raw_search_next(private->tree, req, io, search_private, callback);
995 }
996
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)
1000 {
1001         struct cvfs_private *private = ntvfs->private_data;
1002
1003         SETUP_PID;
1004
1005         return smb_raw_search_close(private->tree, io);
1006 }
1007
1008 /*
1009   a handler for async trans2 replies
1010  */
1011 static void async_trans2(struct smbcli_request *c_req)
1012 {
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);
1018 }
1019
1020 /* raw trans2 */
1021 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs, 
1022                             struct ntvfs_request *req,
1023                             struct smb_trans2 *trans2)
1024 {
1025         struct cvfs_private *private = ntvfs->private_data;
1026         struct smbcli_request *c_req;
1027
1028         if (private->map_trans2) {
1029                 return NT_STATUS_NOT_IMPLEMENTED;
1030         }
1031
1032         SETUP_PID;
1033
1034         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1035                 return smb_raw_trans2(private->tree, req, trans2);
1036         }
1037
1038         c_req = smb_raw_trans2_send(private->tree, trans2);
1039
1040         ASYNC_RECV_TAIL(trans2, async_trans2);
1041 }
1042
1043
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)
1048 {
1049         return NT_STATUS_ACCESS_DENIED;
1050 }
1051
1052 /*
1053   a handler for async change notify replies
1054  */
1055 static void async_changenotify(struct smbcli_request *c_req)
1056 {
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);
1062 }
1063
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)
1068 {
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;
1073
1074         if (io->nttrans.level != RAW_NOTIFY_NTTRANS) {
1075                 return NT_STATUS_NOT_IMPLEMENTED;
1076         }
1077
1078         SETUP_PID;
1079
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;
1083
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;
1087         }
1088
1089         /* we must not timeout on notify requests - they wait
1090            forever */
1091         private->transport->options.request_timeout = 0;
1092
1093         c_req = smb_raw_changenotify_send(private->tree, io);
1094
1095         private->transport->options.request_timeout = saved_timeout;
1096
1097         ASYNC_RECV_TAIL(io, async_changenotify);
1098 }
1099
1100 /*
1101   initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
1102  */
1103 NTSTATUS ntvfs_cifs_init(void)
1104 {
1105         NTSTATUS ret;
1106         struct ntvfs_ops ops;
1107         NTVFS_CURRENT_CRITICAL_SIZES(vers);
1108
1109         ZERO_STRUCT(ops);
1110
1111         /* fill in the name and type */
1112         ops.name = "cifs";
1113         ops.type = NTVFS_DISK;
1114         
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;
1148
1149         /* register ourselves with the NTVFS subsystem. We register
1150            under the name 'cifs'. */
1151         ret = ntvfs_register(&ops, &vers);
1152
1153         if (!NT_STATUS_IS_OK(ret)) {
1154                 DEBUG(0,("Failed to register CIFS backend!\n"));
1155         }
1156         
1157         return ret;
1158 }