24dee76e46af9b9eb136ef7ca21f22c5c1b59304
[samba.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 NTSTATUS ntvfs_cifs_init(void);
67
68 #define CHECK_UPSTREAM_OPEN do { \
69         if (! p->transport->socket->sock) { \
70                 req->async_states->state|=NTVFS_ASYNC_STATE_CLOSE; \
71                 return NT_STATUS_CONNECTION_DISCONNECTED; \
72         } \
73 } while(0)
74
75 #define SETUP_PID do { \
76         p->tree->session->pid = req->smbpid; \
77         CHECK_UPSTREAM_OPEN; \
78 } while(0)
79
80 #define SETUP_FILE_HERE(f) do { \
81         f = ntvfs_handle_get_backend_data(io->generic.in.file.ntvfs, ntvfs); \
82         if (!f) return NT_STATUS_INVALID_HANDLE; \
83         io->generic.in.file.fnum = f->fnum; \
84 } while (0)
85
86 #define SETUP_FILE do { \
87         struct cvfs_file *f; \
88         SETUP_FILE_HERE(f); \
89 } while (0)
90
91 #define SETUP_PID_AND_FILE do { \
92         SETUP_PID; \
93         SETUP_FILE; \
94 } while (0)
95
96 #define CIFS_SERVER             "cifs:server"
97 #define CIFS_USER               "cifs:user"
98 #define CIFS_PASSWORD           "cifs:password"
99 #define CIFS_DOMAIN             "cifs:domain"
100 #define CIFS_SHARE              "cifs:share"
101 #define CIFS_USE_MACHINE_ACCT   "cifs:use-machine-account"
102 #define CIFS_MAP_GENERIC        "cifs:map-generic"
103 #define CIFS_MAP_TRANS2         "cifs:map-trans2"
104
105 #define CIFS_USE_MACHINE_ACCT_DEFAULT   false
106 #define CIFS_MAP_GENERIC_DEFAULT        false
107 #define CIFS_MAP_TRANS2_DEFAULT         true
108
109 /*
110   a handler for oplock break events from the server - these need to be passed
111   along to the client
112  */
113 static bool oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *p_private)
114 {
115         struct cvfs_private *p = p_private;
116         NTSTATUS status;
117         struct ntvfs_handle *h = NULL;
118         struct cvfs_file *f;
119
120         for (f=p->files; f; f=f->next) {
121                 if (f->fnum != fnum) continue;
122                 h = f->h;
123                 break;
124         }
125
126         if (!h) {
127                 DEBUG(5,("vfs_cifs: ignoring oplock break level %d for fnum %d\n", level, fnum));
128                 return true;
129         }
130
131         DEBUG(5,("vfs_cifs: sending oplock break level %d for fnum %d\n", level, fnum));
132         status = ntvfs_send_oplock_break(p->ntvfs, h, level);
133         if (!NT_STATUS_IS_OK(status)) return false;
134         return true;
135 }
136
137 /*
138   connect to a share - used when a tree_connect operation comes in.
139 */
140 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs, 
141                              struct ntvfs_request *req,
142                              union smb_tcon *tcon)
143 {
144         NTSTATUS status;
145         struct cvfs_private *p;
146         const char *host, *user, *pass, *domain, *remote_share;
147         struct smb_composite_connect io;
148         struct composite_context *creq;
149         struct share_config *scfg = ntvfs->ctx->config;
150
151         struct cli_credentials *credentials;
152         bool machine_account;
153         const char* sharename;
154
155         switch (tcon->generic.level) {
156         case RAW_TCON_TCON:
157                 sharename = tcon->tcon.in.service;
158                 break;
159         case RAW_TCON_TCONX:
160                 sharename = tcon->tconx.in.path;
161                 break;
162         case RAW_TCON_SMB2:
163                 sharename = tcon->smb2.in.path;
164                 break;
165         default:
166                 return NT_STATUS_INVALID_LEVEL;
167         }
168
169         if (strncmp(sharename, "\\\\", 2) == 0) {
170                 char *str = strchr(sharename+2, '\\');
171                 if (str) {
172                         sharename = str + 1;
173                 }
174         }
175
176         /* Here we need to determine which server to connect to.
177          * For now we use parametric options, type cifs.
178          * Later we will use security=server and auth_server.c.
179          */
180         host = share_string_option(scfg, CIFS_SERVER, NULL);
181         user = share_string_option(scfg, CIFS_USER, NULL);
182         pass = share_string_option(scfg, CIFS_PASSWORD, NULL);
183         domain = share_string_option(scfg, CIFS_DOMAIN, NULL);
184         remote_share = share_string_option(scfg, CIFS_SHARE, NULL);
185         if (!remote_share) {
186                 remote_share = sharename;
187         }
188
189         machine_account = share_bool_option(scfg, CIFS_USE_MACHINE_ACCT, CIFS_USE_MACHINE_ACCT_DEFAULT);
190
191         p = talloc_zero(ntvfs, struct cvfs_private);
192         if (!p) {
193                 return NT_STATUS_NO_MEMORY;
194         }
195
196         ntvfs->private_data = p;
197
198         if (!host) {
199                 DEBUG(1,("CIFS backend: You must supply server\n"));
200                 return NT_STATUS_INVALID_PARAMETER;
201         } 
202         
203         if (user && pass) {
204                 DEBUG(5, ("CIFS backend: Using specified password\n"));
205                 credentials = cli_credentials_init(p);
206                 if (!credentials) {
207                         return NT_STATUS_NO_MEMORY;
208                 }
209                 cli_credentials_set_conf(credentials, ntvfs->ctx->lp_ctx);
210                 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
211                 if (domain) {
212                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
213                 }
214                 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
215         } else if (machine_account) {
216                 DEBUG(5, ("CIFS backend: Using machine account\n"));
217                 credentials = cli_credentials_init(p);
218                 cli_credentials_set_conf(credentials, ntvfs->ctx->lp_ctx);
219                 if (domain) {
220                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
221                 }
222                 status = cli_credentials_set_machine_account(credentials, ntvfs->ctx->lp_ctx);
223                 if (!NT_STATUS_IS_OK(status)) {
224                         return status;
225                 }
226         } else if (req->session_info->credentials) {
227                 DEBUG(5, ("CIFS backend: Using delegated credentials\n"));
228                 credentials = req->session_info->credentials;
229         } else {
230                 DEBUG(1,("CIFS backend: NO delegated credentials found: You must supply server, user and password or the client must supply delegated credentials\n"));
231                 return NT_STATUS_INTERNAL_ERROR;
232         }
233
234         /* connect to the server, using the smbd event context */
235         io.in.dest_host = host;
236         io.in.dest_ports = lpcfg_smb_ports(ntvfs->ctx->lp_ctx);
237         io.in.socket_options = lpcfg_socket_options(ntvfs->ctx->lp_ctx);
238         io.in.called_name = host;
239         io.in.credentials = credentials;
240         io.in.fallback_to_anonymous = false;
241         io.in.workgroup = lpcfg_workgroup(ntvfs->ctx->lp_ctx);
242         io.in.service = remote_share;
243         io.in.service_type = "?????";
244         io.in.gensec_settings = lpcfg_gensec_settings(p, ntvfs->ctx->lp_ctx);
245         lpcfg_smbcli_options(ntvfs->ctx->lp_ctx, &io.in.options);
246         lpcfg_smbcli_session_options(ntvfs->ctx->lp_ctx, &io.in.session_options);
247
248         if (!(ntvfs->ctx->client_caps & NTVFS_CLIENT_CAP_LEVEL_II_OPLOCKS)) {
249                 io.in.options.use_level2_oplocks = false;
250         }
251
252         creq = smb_composite_connect_send(&io, p,
253                                           lpcfg_resolve_context(ntvfs->ctx->lp_ctx),
254                                           ntvfs->ctx->event_ctx);
255         status = smb_composite_connect_recv(creq, p);
256         NT_STATUS_NOT_OK_RETURN(status);
257
258         p->tree = io.out.tree;
259
260         p->transport = p->tree->session->transport;
261         SETUP_PID;
262         p->ntvfs = ntvfs;
263
264         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
265         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
266         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
267         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
268
269         if (tcon->generic.level == RAW_TCON_TCONX) {
270                 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
271                 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
272         }
273
274         /* we need to receive oplock break requests from the server */
275         smbcli_oplock_handler(p->transport, oplock_handler, p);
276
277         p->map_generic = share_bool_option(scfg, CIFS_MAP_GENERIC, CIFS_MAP_GENERIC_DEFAULT);
278
279         p->map_trans2 = share_bool_option(scfg, CIFS_MAP_TRANS2, CIFS_MAP_TRANS2_DEFAULT);
280
281         return NT_STATUS_OK;
282 }
283
284 /*
285   disconnect from a share
286 */
287 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs)
288 {
289         struct cvfs_private *p = ntvfs->private_data;
290         struct async_info *a, *an;
291
292         /* first cleanup pending requests */
293         for (a=p->pending; a; a = an) {
294                 an = a->next;
295                 smbcli_request_destroy(a->c_req);
296                 talloc_free(a);
297         }
298
299         talloc_free(p);
300         ntvfs->private_data = NULL;
301
302         return NT_STATUS_OK;
303 }
304
305 /*
306   destroy an async info structure
307 */
308 static int async_info_destructor(struct async_info *async)
309 {
310         DLIST_REMOVE(async->cvfs->pending, async);
311         return 0;
312 }
313
314 /*
315   a handler for simple async replies
316   this handler can only be used for functions that don't return any
317   parameters (those that just return a status code)
318  */
319 static void async_simple(struct smbcli_request *c_req)
320 {
321         struct async_info *async = c_req->async.private_data;
322         struct ntvfs_request *req = async->req;
323         req->async_states->status = smbcli_request_simple_recv(c_req);
324         talloc_free(async);
325         req->async_states->send_fn(req);
326 }
327
328
329 /* save some typing for the simple functions */
330 #define ASYNC_RECV_TAIL_F(io, async_fn, file) do { \
331         if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
332         { \
333                 struct async_info *async; \
334                 async = talloc(req, struct async_info); \
335                 if (!async) return NT_STATUS_NO_MEMORY; \
336                 async->parms = io; \
337                 async->req = req; \
338                 async->f = file; \
339                 async->cvfs = p; \
340                 async->c_req = c_req; \
341                 DLIST_ADD(p->pending, async); \
342                 c_req->async.private_data = async; \
343                 talloc_set_destructor(async, async_info_destructor); \
344         } \
345         c_req->async.fn = async_fn; \
346         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
347         return NT_STATUS_OK; \
348 } while (0)
349
350 #define ASYNC_RECV_TAIL(io, async_fn) ASYNC_RECV_TAIL_F(io, async_fn, NULL)
351
352 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
353
354 /*
355   delete a file - the dirtype specifies the file types to include in the search. 
356   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
357 */
358 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs, 
359                             struct ntvfs_request *req, union smb_unlink *unl)
360 {
361         struct cvfs_private *p = ntvfs->private_data;
362         struct smbcli_request *c_req;
363
364         SETUP_PID;
365
366         /* see if the front end will allow us to perform this
367            function asynchronously.  */
368         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
369                 return smb_raw_unlink(p->tree, unl);
370         }
371
372         c_req = smb_raw_unlink_send(p->tree, unl);
373
374         SIMPLE_ASYNC_TAIL;
375 }
376
377 /*
378   a handler for async ioctl replies
379  */
380 static void async_ioctl(struct smbcli_request *c_req)
381 {
382         struct async_info *async = c_req->async.private_data;
383         struct ntvfs_request *req = async->req;
384         req->async_states->status = smb_raw_ioctl_recv(c_req, req, async->parms);
385         talloc_free(async);
386         req->async_states->send_fn(req);
387 }
388
389 /*
390   ioctl interface
391 */
392 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs, 
393                            struct ntvfs_request *req, union smb_ioctl *io)
394 {
395         struct cvfs_private *p = ntvfs->private_data;
396         struct smbcli_request *c_req;
397
398         SETUP_PID_AND_FILE;
399
400         /* see if the front end will allow us to perform this
401            function asynchronously.  */
402         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
403                 return smb_raw_ioctl(p->tree, req, io);
404         }
405
406         c_req = smb_raw_ioctl_send(p->tree, io);
407
408         ASYNC_RECV_TAIL(io, async_ioctl);
409 }
410
411 /*
412   check if a directory exists
413 */
414 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs, 
415                              struct ntvfs_request *req, union smb_chkpath *cp)
416 {
417         struct cvfs_private *p = ntvfs->private_data;
418         struct smbcli_request *c_req;
419
420         SETUP_PID;
421
422         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
423                 return smb_raw_chkpath(p->tree, cp);
424         }
425
426         c_req = smb_raw_chkpath_send(p->tree, cp);
427
428         SIMPLE_ASYNC_TAIL;
429 }
430
431 /*
432   a handler for async qpathinfo replies
433  */
434 static void async_qpathinfo(struct smbcli_request *c_req)
435 {
436         struct async_info *async = c_req->async.private_data;
437         struct ntvfs_request *req = async->req;
438         req->async_states->status = smb_raw_pathinfo_recv(c_req, req, async->parms);
439         talloc_free(async);
440         req->async_states->send_fn(req);
441 }
442
443 /*
444   return info on a pathname
445 */
446 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs, 
447                                struct ntvfs_request *req, union smb_fileinfo *info)
448 {
449         struct cvfs_private *p = ntvfs->private_data;
450         struct smbcli_request *c_req;
451
452         SETUP_PID;
453
454         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
455                 return smb_raw_pathinfo(p->tree, req, info);
456         }
457
458         c_req = smb_raw_pathinfo_send(p->tree, info);
459
460         ASYNC_RECV_TAIL(info, async_qpathinfo);
461 }
462
463 /*
464   a handler for async qfileinfo replies
465  */
466 static void async_qfileinfo(struct smbcli_request *c_req)
467 {
468         struct async_info *async = c_req->async.private_data;
469         struct ntvfs_request *req = async->req;
470         req->async_states->status = smb_raw_fileinfo_recv(c_req, req, async->parms);
471         talloc_free(async);
472         req->async_states->send_fn(req);
473 }
474
475 /*
476   query info on a open file
477 */
478 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs, 
479                                struct ntvfs_request *req, union smb_fileinfo *io)
480 {
481         struct cvfs_private *p = ntvfs->private_data;
482         struct smbcli_request *c_req;
483
484         SETUP_PID_AND_FILE;
485
486         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
487                 return smb_raw_fileinfo(p->tree, req, io);
488         }
489
490         c_req = smb_raw_fileinfo_send(p->tree, io);
491
492         ASYNC_RECV_TAIL(io, async_qfileinfo);
493 }
494
495
496 /*
497   set info on a pathname
498 */
499 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs, 
500                                  struct ntvfs_request *req, union smb_setfileinfo *st)
501 {
502         struct cvfs_private *p = ntvfs->private_data;
503         struct smbcli_request *c_req;
504
505         SETUP_PID;
506
507         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
508                 return smb_raw_setpathinfo(p->tree, st);
509         }
510
511         c_req = smb_raw_setpathinfo_send(p->tree, st);
512
513         SIMPLE_ASYNC_TAIL;
514 }
515
516
517 /*
518   a handler for async open replies
519  */
520 static void async_open(struct smbcli_request *c_req)
521 {
522         struct async_info *async = c_req->async.private_data;
523         struct cvfs_private *cvfs = async->cvfs;
524         struct ntvfs_request *req = async->req;
525         struct cvfs_file *f = async->f;
526         union smb_open *io = async->parms;
527         union smb_handle *file;
528         talloc_free(async);
529         req->async_states->status = smb_raw_open_recv(c_req, req, io);
530         SMB_OPEN_OUT_FILE(io, file);
531         f->fnum = file->fnum;
532         file->ntvfs = NULL;
533         if (!NT_STATUS_IS_OK(req->async_states->status)) goto failed;
534         req->async_states->status = ntvfs_handle_set_backend_data(f->h, cvfs->ntvfs, f);
535         if (!NT_STATUS_IS_OK(req->async_states->status)) goto failed;
536         file->ntvfs = f->h;
537         DLIST_ADD(cvfs->files, f);
538 failed:
539         req->async_states->send_fn(req);
540 }
541
542 /*
543   open a file
544 */
545 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs, 
546                           struct ntvfs_request *req, union smb_open *io)
547 {
548         struct cvfs_private *p = ntvfs->private_data;
549         struct smbcli_request *c_req;
550         struct ntvfs_handle *h;
551         struct cvfs_file *f;
552         NTSTATUS status;
553
554         SETUP_PID;
555
556         if (io->generic.level != RAW_OPEN_GENERIC &&
557             p->map_generic) {
558                 return ntvfs_map_open(ntvfs, req, io);
559         }
560
561         status = ntvfs_handle_new(ntvfs, req, &h);
562         NT_STATUS_NOT_OK_RETURN(status);
563
564         f = talloc_zero(h, struct cvfs_file);
565         NT_STATUS_HAVE_NO_MEMORY(f);
566         f->h = h;
567
568         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
569                 union smb_handle *file;
570
571                 status = smb_raw_open(p->tree, req, io);
572                 NT_STATUS_NOT_OK_RETURN(status);
573
574                 SMB_OPEN_OUT_FILE(io, file);
575                 f->fnum = file->fnum;
576                 file->ntvfs = NULL;
577                 status = ntvfs_handle_set_backend_data(f->h, p->ntvfs, f);
578                 NT_STATUS_NOT_OK_RETURN(status);
579                 file->ntvfs = f->h;
580                 DLIST_ADD(p->files, f);
581
582                 return NT_STATUS_OK;
583         }
584
585         c_req = smb_raw_open_send(p->tree, io);
586
587         ASYNC_RECV_TAIL_F(io, async_open, f);
588 }
589
590 /*
591   create a directory
592 */
593 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs, 
594                            struct ntvfs_request *req, union smb_mkdir *md)
595 {
596         struct cvfs_private *p = ntvfs->private_data;
597         struct smbcli_request *c_req;
598
599         SETUP_PID;
600
601         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
602                 return smb_raw_mkdir(p->tree, md);
603         }
604
605         c_req = smb_raw_mkdir_send(p->tree, md);
606
607         SIMPLE_ASYNC_TAIL;
608 }
609
610 /*
611   remove a directory
612 */
613 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs, 
614                            struct ntvfs_request *req, struct smb_rmdir *rd)
615 {
616         struct cvfs_private *p = ntvfs->private_data;
617         struct smbcli_request *c_req;
618
619         SETUP_PID;
620
621         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
622                 return smb_raw_rmdir(p->tree, rd);
623         }
624         c_req = smb_raw_rmdir_send(p->tree, rd);
625
626         SIMPLE_ASYNC_TAIL;
627 }
628
629 /*
630   rename a set of files
631 */
632 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs, 
633                             struct ntvfs_request *req, union smb_rename *ren)
634 {
635         struct cvfs_private *p = ntvfs->private_data;
636         struct smbcli_request *c_req;
637
638         SETUP_PID;
639
640         if (ren->nttrans.level == RAW_RENAME_NTTRANS) {
641                 struct cvfs_file *f;
642                 f = ntvfs_handle_get_backend_data(ren->nttrans.in.file.ntvfs, ntvfs);
643                 if (!f) return NT_STATUS_INVALID_HANDLE;
644                 ren->nttrans.in.file.fnum = f->fnum;
645         }
646
647         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
648                 return smb_raw_rename(p->tree, ren);
649         }
650
651         c_req = smb_raw_rename_send(p->tree, ren);
652
653         SIMPLE_ASYNC_TAIL;
654 }
655
656 /*
657   copy a set of files
658 */
659 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs, 
660                           struct ntvfs_request *req, struct smb_copy *cp)
661 {
662         return NT_STATUS_NOT_SUPPORTED;
663 }
664
665 /*
666   a handler for async read replies
667  */
668 static void async_read(struct smbcli_request *c_req)
669 {
670         struct async_info *async = c_req->async.private_data;
671         struct ntvfs_request *req = async->req;
672         req->async_states->status = smb_raw_read_recv(c_req, async->parms);
673         talloc_free(async);
674         req->async_states->send_fn(req);
675 }
676
677 /*
678   read from a file
679 */
680 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs, 
681                           struct ntvfs_request *req, union smb_read *io)
682 {
683         struct cvfs_private *p = ntvfs->private_data;
684         struct smbcli_request *c_req;
685
686         SETUP_PID;
687
688         if (io->generic.level != RAW_READ_GENERIC &&
689             p->map_generic) {
690                 return ntvfs_map_read(ntvfs, req, io);
691         }
692
693         SETUP_FILE;
694
695         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
696                 return smb_raw_read(p->tree, io);
697         }
698
699         c_req = smb_raw_read_send(p->tree, io);
700
701         ASYNC_RECV_TAIL(io, async_read);
702 }
703
704 /*
705   a handler for async write replies
706  */
707 static void async_write(struct smbcli_request *c_req)
708 {
709         struct async_info *async = c_req->async.private_data;
710         struct ntvfs_request *req = async->req;
711         req->async_states->status = smb_raw_write_recv(c_req, async->parms);
712         talloc_free(async);
713         req->async_states->send_fn(req);
714 }
715
716 /*
717   write to a file
718 */
719 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs, 
720                            struct ntvfs_request *req, union smb_write *io)
721 {
722         struct cvfs_private *p = ntvfs->private_data;
723         struct smbcli_request *c_req;
724
725         SETUP_PID;
726
727         if (io->generic.level != RAW_WRITE_GENERIC &&
728             p->map_generic) {
729                 return ntvfs_map_write(ntvfs, req, io);
730         }
731         SETUP_FILE;
732
733         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
734                 return smb_raw_write(p->tree, io);
735         }
736
737         c_req = smb_raw_write_send(p->tree, io);
738
739         ASYNC_RECV_TAIL(io, async_write);
740 }
741
742 /*
743   a handler for async seek replies
744  */
745 static void async_seek(struct smbcli_request *c_req)
746 {
747         struct async_info *async = c_req->async.private_data;
748         struct ntvfs_request *req = async->req;
749         req->async_states->status = smb_raw_seek_recv(c_req, async->parms);
750         talloc_free(async);
751         req->async_states->send_fn(req);
752 }
753
754 /*
755   seek in a file
756 */
757 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs, 
758                           struct ntvfs_request *req,
759                           union smb_seek *io)
760 {
761         struct cvfs_private *p = ntvfs->private_data;
762         struct smbcli_request *c_req;
763
764         SETUP_PID_AND_FILE;
765
766         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
767                 return smb_raw_seek(p->tree, io);
768         }
769
770         c_req = smb_raw_seek_send(p->tree, io);
771
772         ASYNC_RECV_TAIL(io, async_seek);
773 }
774
775 /*
776   flush a file
777 */
778 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs, 
779                            struct ntvfs_request *req,
780                            union smb_flush *io)
781 {
782         struct cvfs_private *p = ntvfs->private_data;
783         struct smbcli_request *c_req;
784
785         SETUP_PID;
786         switch (io->generic.level) {
787         case RAW_FLUSH_FLUSH:
788                 SETUP_FILE;
789                 break;
790         case RAW_FLUSH_ALL:
791                 io->generic.in.file.fnum = 0xFFFF;
792                 break;
793         case RAW_FLUSH_SMB2:
794                 return NT_STATUS_INVALID_LEVEL;
795         }
796
797         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
798                 return smb_raw_flush(p->tree, io);
799         }
800
801         c_req = smb_raw_flush_send(p->tree, io);
802
803         SIMPLE_ASYNC_TAIL;
804 }
805
806 /*
807   close a file
808 */
809 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs, 
810                            struct ntvfs_request *req, union smb_close *io)
811 {
812         struct cvfs_private *p = ntvfs->private_data;
813         struct smbcli_request *c_req;
814         struct cvfs_file *f;
815         union smb_close io2;
816
817         SETUP_PID;
818
819         if (io->generic.level != RAW_CLOSE_GENERIC &&
820             p->map_generic) {
821                 return ntvfs_map_close(ntvfs, req, io);
822         }
823
824         if (io->generic.level == RAW_CLOSE_GENERIC) {
825                 ZERO_STRUCT(io2);
826                 io2.close.level = RAW_CLOSE_CLOSE;
827                 io2.close.in.file = io->generic.in.file;
828                 io2.close.in.write_time = io->generic.in.write_time;
829                 io = &io2;
830         }
831
832         SETUP_FILE_HERE(f);
833         /* Note, we aren't free-ing f, or it's h here. Should we?
834            even if file-close fails, we'll remove it from the list,
835            what else would we do? Maybe we should not remove until
836            after the proxied call completes? */
837         DLIST_REMOVE(p->files, f);
838
839         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
840                 return smb_raw_close(p->tree, io);
841         }
842
843         c_req = smb_raw_close_send(p->tree, io);
844
845         SIMPLE_ASYNC_TAIL;
846 }
847
848 /*
849   exit - closing files open by the pid
850 */
851 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs, 
852                           struct ntvfs_request *req)
853 {
854         struct cvfs_private *p = ntvfs->private_data;
855         struct smbcli_request *c_req;
856
857         SETUP_PID;
858
859         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
860                 return smb_raw_exit(p->tree->session);
861         }
862
863         c_req = smb_raw_exit_send(p->tree->session);
864
865         SIMPLE_ASYNC_TAIL;
866 }
867
868 /*
869   logoff - closing files open by the user
870 */
871 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs, 
872                             struct ntvfs_request *req)
873 {
874         /* we can't do this right in the cifs backend .... */
875         return NT_STATUS_OK;
876 }
877
878 /*
879   setup for an async call - nothing to do yet
880 */
881 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs, 
882                                  struct ntvfs_request *req, 
883                                  void *private_data)
884 {
885         return NT_STATUS_OK;
886 }
887
888 /*
889   cancel an async call
890 */
891 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs, 
892                             struct ntvfs_request *req)
893 {
894         struct cvfs_private *p = ntvfs->private_data;
895         struct async_info *a;
896
897         /* find the matching request */
898         for (a=p->pending;a;a=a->next) {
899                 if (a->req == req) {
900                         break;
901                 }
902         }
903
904         if (a == NULL) {
905                 return NT_STATUS_INVALID_PARAMETER;
906         }
907
908         return smb_raw_ntcancel(a->c_req);
909 }
910
911 /*
912   lock a byte range
913 */
914 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs, 
915                           struct ntvfs_request *req, union smb_lock *io)
916 {
917         struct cvfs_private *p = ntvfs->private_data;
918         struct smbcli_request *c_req;
919
920         SETUP_PID;
921
922         if (io->generic.level != RAW_LOCK_GENERIC &&
923             p->map_generic) {
924                 return ntvfs_map_lock(ntvfs, req, io);
925         }
926         SETUP_FILE;
927
928         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
929                 return smb_raw_lock(p->tree, io);
930         }
931
932         c_req = smb_raw_lock_send(p->tree, io);
933         SIMPLE_ASYNC_TAIL;
934 }
935
936 /*
937   set info on a open file
938 */
939 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs, 
940                                  struct ntvfs_request *req, 
941                                  union smb_setfileinfo *io)
942 {
943         struct cvfs_private *p = ntvfs->private_data;
944         struct smbcli_request *c_req;
945
946         SETUP_PID_AND_FILE;
947
948         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
949                 return smb_raw_setfileinfo(p->tree, io);
950         }
951         c_req = smb_raw_setfileinfo_send(p->tree, io);
952
953         SIMPLE_ASYNC_TAIL;
954 }
955
956
957 /*
958   a handler for async fsinfo replies
959  */
960 static void async_fsinfo(struct smbcli_request *c_req)
961 {
962         struct async_info *async = c_req->async.private_data;
963         struct ntvfs_request *req = async->req;
964         req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
965         talloc_free(async);
966         req->async_states->send_fn(req);
967 }
968
969 /*
970   return filesystem space info
971 */
972 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs, 
973                             struct ntvfs_request *req, union smb_fsinfo *fs)
974 {
975         struct cvfs_private *p = ntvfs->private_data;
976         struct smbcli_request *c_req;
977
978         SETUP_PID;
979
980         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
981                 return smb_raw_fsinfo(p->tree, req, fs);
982         }
983
984         c_req = smb_raw_fsinfo_send(p->tree, req, fs);
985
986         ASYNC_RECV_TAIL(fs, async_fsinfo);
987 }
988
989 /*
990   return print queue info
991 */
992 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs, 
993                          struct ntvfs_request *req, union smb_lpq *lpq)
994 {
995         return NT_STATUS_NOT_SUPPORTED;
996 }
997
998 /* 
999    list files in a directory matching a wildcard pattern
1000 */
1001 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs, 
1002                                   struct ntvfs_request *req, union smb_search_first *io, 
1003                                   void *search_private, 
1004                                   bool (*callback)(void *, const union smb_search_data *))
1005 {
1006         struct cvfs_private *p = ntvfs->private_data;
1007
1008         SETUP_PID;
1009
1010         return smb_raw_search_first(p->tree, req, io, search_private, callback);
1011 }
1012
1013 /* continue a search */
1014 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs, 
1015                                  struct ntvfs_request *req, union smb_search_next *io, 
1016                                  void *search_private, 
1017                                  bool (*callback)(void *, const union smb_search_data *))
1018 {
1019         struct cvfs_private *p = ntvfs->private_data;
1020
1021         SETUP_PID;
1022
1023         return smb_raw_search_next(p->tree, req, io, search_private, callback);
1024 }
1025
1026 /* close a search */
1027 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs, 
1028                                   struct ntvfs_request *req, union smb_search_close *io)
1029 {
1030         struct cvfs_private *p = ntvfs->private_data;
1031
1032         SETUP_PID;
1033
1034         return smb_raw_search_close(p->tree, io);
1035 }
1036
1037 /*
1038   a handler for async trans2 replies
1039  */
1040 static void async_trans2(struct smbcli_request *c_req)
1041 {
1042         struct async_info *async = c_req->async.private_data;
1043         struct ntvfs_request *req = async->req;
1044         req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
1045         talloc_free(async);
1046         req->async_states->send_fn(req);
1047 }
1048
1049 /* raw trans2 */
1050 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs, 
1051                             struct ntvfs_request *req,
1052                             struct smb_trans2 *trans2)
1053 {
1054         struct cvfs_private *p = ntvfs->private_data;
1055         struct smbcli_request *c_req;
1056
1057         if (p->map_trans2) {
1058                 return NT_STATUS_NOT_IMPLEMENTED;
1059         }
1060
1061         SETUP_PID;
1062
1063         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1064                 return smb_raw_trans2(p->tree, req, trans2);
1065         }
1066
1067         c_req = smb_raw_trans2_send(p->tree, trans2);
1068
1069         ASYNC_RECV_TAIL(trans2, async_trans2);
1070 }
1071
1072
1073 /* SMBtrans - not used on file shares */
1074 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs, 
1075                            struct ntvfs_request *req,
1076                            struct smb_trans2 *trans2)
1077 {
1078         return NT_STATUS_ACCESS_DENIED;
1079 }
1080
1081 /*
1082   a handler for async change notify replies
1083  */
1084 static void async_changenotify(struct smbcli_request *c_req)
1085 {
1086         struct async_info *async = c_req->async.private_data;
1087         struct ntvfs_request *req = async->req;
1088         req->async_states->status = smb_raw_changenotify_recv(c_req, req, async->parms);
1089         talloc_free(async);
1090         req->async_states->send_fn(req);
1091 }
1092
1093 /* change notify request - always async */
1094 static NTSTATUS cvfs_notify(struct ntvfs_module_context *ntvfs, 
1095                             struct ntvfs_request *req,
1096                             union smb_notify *io)
1097 {
1098         struct cvfs_private *p = ntvfs->private_data;
1099         struct smbcli_request *c_req;
1100         int saved_timeout = p->transport->options.request_timeout;
1101         struct cvfs_file *f;
1102
1103         if (io->nttrans.level != RAW_NOTIFY_NTTRANS) {
1104                 return NT_STATUS_NOT_IMPLEMENTED;
1105         }
1106
1107         SETUP_PID;
1108
1109         f = ntvfs_handle_get_backend_data(io->nttrans.in.file.ntvfs, ntvfs);
1110         if (!f) return NT_STATUS_INVALID_HANDLE;
1111         io->nttrans.in.file.fnum = f->fnum;
1112
1113         /* this request doesn't make sense unless its async */
1114         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1115                 return NT_STATUS_INVALID_PARAMETER;
1116         }
1117
1118         /* we must not timeout on notify requests - they wait
1119            forever */
1120         p->transport->options.request_timeout = 0;
1121
1122         c_req = smb_raw_changenotify_send(p->tree, io);
1123
1124         p->transport->options.request_timeout = saved_timeout;
1125
1126         ASYNC_RECV_TAIL(io, async_changenotify);
1127 }
1128
1129 /*
1130   initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
1131  */
1132 NTSTATUS ntvfs_cifs_init(void)
1133 {
1134         NTSTATUS ret;
1135         struct ntvfs_ops ops;
1136         NTVFS_CURRENT_CRITICAL_SIZES(vers);
1137
1138         ZERO_STRUCT(ops);
1139
1140         /* fill in the name and type */
1141         ops.name = "cifs";
1142         ops.type = NTVFS_DISK;
1143         
1144         /* fill in all the operations */
1145         ops.connect = cvfs_connect;
1146         ops.disconnect = cvfs_disconnect;
1147         ops.unlink = cvfs_unlink;
1148         ops.chkpath = cvfs_chkpath;
1149         ops.qpathinfo = cvfs_qpathinfo;
1150         ops.setpathinfo = cvfs_setpathinfo;
1151         ops.open = cvfs_open;
1152         ops.mkdir = cvfs_mkdir;
1153         ops.rmdir = cvfs_rmdir;
1154         ops.rename = cvfs_rename;
1155         ops.copy = cvfs_copy;
1156         ops.ioctl = cvfs_ioctl;
1157         ops.read = cvfs_read;
1158         ops.write = cvfs_write;
1159         ops.seek = cvfs_seek;
1160         ops.flush = cvfs_flush; 
1161         ops.close = cvfs_close;
1162         ops.exit = cvfs_exit;
1163         ops.lock = cvfs_lock;
1164         ops.setfileinfo = cvfs_setfileinfo;
1165         ops.qfileinfo = cvfs_qfileinfo;
1166         ops.fsinfo = cvfs_fsinfo;
1167         ops.lpq = cvfs_lpq;
1168         ops.search_first = cvfs_search_first;
1169         ops.search_next = cvfs_search_next;
1170         ops.search_close = cvfs_search_close;
1171         ops.trans = cvfs_trans;
1172         ops.logoff = cvfs_logoff;
1173         ops.async_setup = cvfs_async_setup;
1174         ops.cancel = cvfs_cancel;
1175         ops.notify = cvfs_notify;
1176         ops.trans2 = cvfs_trans2;
1177
1178         /* register ourselves with the NTVFS subsystem. We register
1179            under the name 'cifs'. */
1180         ret = ntvfs_register(&ops, &vers);
1181
1182         if (!NT_STATUS_IS_OK(ret)) {
1183                 DEBUG(0,("Failed to register CIFS backend!\n"));
1184         }
1185         
1186         return ret;
1187 }