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