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