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