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