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