r14012: added support for the SMBntcancel operation in the cifs ntvfs
[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 #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 smbsrv_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 smbsrv_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                                 struct smbsrv_tcon *tcon)
181 {
182         struct cvfs_private *private = ntvfs->private_data;
183
184         talloc_free(private);
185         ntvfs->private_data = NULL;
186
187         return NT_STATUS_OK;
188 }
189
190 /*
191   destroy an async info structure
192 */
193 static int async_info_destructor(void *p)
194 {
195         struct async_info *async = talloc_get_type(p, struct async_info);
196         DLIST_REMOVE(async->cvfs->pending, async);
197         return 0;
198 }
199
200 /*
201   a handler for simple async replies
202   this handler can only be used for functions that don't return any
203   parameters (those that just return a status code)
204  */
205 static void async_simple(struct smbcli_request *c_req)
206 {
207         struct async_info *async = c_req->async.private;
208         struct smbsrv_request *req = async->req;
209         req->async_states->status = smbcli_request_simple_recv(c_req);
210         req->async_states->send_fn(req);
211 }
212
213
214 /* save some typing for the simple functions */
215 #define ASYNC_RECV_TAIL(io, async_fn) do { \
216         if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
217         { \
218                 struct async_info *async; \
219                 async = talloc(req, struct async_info); \
220                 if (!async) return NT_STATUS_NO_MEMORY; \
221                 async->parms = io; \
222                 async->req = req; \
223                 async->cvfs = private; \
224                 async->c_req = c_req; \
225                 DLIST_ADD(private->pending, async); \
226                 c_req->async.private = async; \
227                 talloc_set_destructor(async, async_info_destructor); \
228         } \
229         c_req->async.fn = async_fn; \
230         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
231         return NT_STATUS_OK; \
232 } while (0)
233
234 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
235
236 /*
237   delete a file - the dirtype specifies the file types to include in the search. 
238   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
239 */
240 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs, 
241                             struct smbsrv_request *req, struct smb_unlink *unl)
242 {
243         struct cvfs_private *private = ntvfs->private_data;
244         struct smbcli_request *c_req;
245
246         SETUP_PID;
247
248         /* see if the front end will allow us to perform this
249            function asynchronously.  */
250         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
251                 return smb_raw_unlink(private->tree, unl);
252         }
253
254         c_req = smb_raw_unlink_send(private->tree, unl);
255
256         SIMPLE_ASYNC_TAIL;
257 }
258
259 /*
260   a handler for async ioctl replies
261  */
262 static void async_ioctl(struct smbcli_request *c_req)
263 {
264         struct async_info *async = c_req->async.private;
265         struct smbsrv_request *req = async->req;
266         req->async_states->status = smb_raw_ioctl_recv(c_req, req, async->parms);
267         req->async_states->send_fn(req);
268 }
269
270 /*
271   ioctl interface
272 */
273 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs, 
274                                 struct smbsrv_request *req, union smb_ioctl *io)
275 {
276         struct cvfs_private *private = ntvfs->private_data;
277         struct smbcli_request *c_req;
278
279         SETUP_PID;
280
281         /* see if the front end will allow us to perform this
282            function asynchronously.  */
283         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
284                 return smb_raw_ioctl(private->tree, req, io);
285         }
286
287         c_req = smb_raw_ioctl_send(private->tree, io);
288
289         ASYNC_RECV_TAIL(io, async_ioctl);
290 }
291
292 /*
293   check if a directory exists
294 */
295 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs, 
296                                 struct smbsrv_request *req, struct smb_chkpath *cp)
297 {
298         struct cvfs_private *private = ntvfs->private_data;
299         struct smbcli_request *c_req;
300
301         SETUP_PID;
302
303         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
304                 return smb_raw_chkpath(private->tree, cp);
305         }
306
307         c_req = smb_raw_chkpath_send(private->tree, cp);
308
309         SIMPLE_ASYNC_TAIL;
310 }
311
312 /*
313   a handler for async qpathinfo replies
314  */
315 static void async_qpathinfo(struct smbcli_request *c_req)
316 {
317         struct async_info *async = c_req->async.private;
318         struct smbsrv_request *req = async->req;
319         req->async_states->status = smb_raw_pathinfo_recv(c_req, req, async->parms);
320         req->async_states->send_fn(req);
321 }
322
323 /*
324   return info on a pathname
325 */
326 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs, 
327                                 struct smbsrv_request *req, union smb_fileinfo *info)
328 {
329         struct cvfs_private *private = ntvfs->private_data;
330         struct smbcli_request *c_req;
331
332         SETUP_PID;
333
334         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
335                 return smb_raw_pathinfo(private->tree, req, info);
336         }
337
338         c_req = smb_raw_pathinfo_send(private->tree, info);
339
340         ASYNC_RECV_TAIL(info, async_qpathinfo);
341 }
342
343 /*
344   a handler for async qfileinfo replies
345  */
346 static void async_qfileinfo(struct smbcli_request *c_req)
347 {
348         struct async_info *async = c_req->async.private;
349         struct smbsrv_request *req = async->req;
350         req->async_states->status = smb_raw_fileinfo_recv(c_req, req, async->parms);
351         req->async_states->send_fn(req);
352 }
353
354 /*
355   query info on a open file
356 */
357 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs, 
358                                 struct smbsrv_request *req, union smb_fileinfo *info)
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_fileinfo(private->tree, req, info);
367         }
368
369         c_req = smb_raw_fileinfo_send(private->tree, info);
370
371         ASYNC_RECV_TAIL(info, async_qfileinfo);
372 }
373
374
375 /*
376   set info on a pathname
377 */
378 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs, 
379                                 struct smbsrv_request *req, union smb_setfileinfo *st)
380 {
381         struct cvfs_private *private = ntvfs->private_data;
382         struct smbcli_request *c_req;
383
384         SETUP_PID;
385
386         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
387                 return smb_raw_setpathinfo(private->tree, st);
388         }
389
390         c_req = smb_raw_setpathinfo_send(private->tree, st);
391
392         SIMPLE_ASYNC_TAIL;
393 }
394
395
396 /*
397   a handler for async open replies
398  */
399 static void async_open(struct smbcli_request *c_req)
400 {
401         struct async_info *async = c_req->async.private;
402         struct smbsrv_request *req = async->req;
403         req->async_states->status = smb_raw_open_recv(c_req, req, async->parms);
404         req->async_states->send_fn(req);
405 }
406
407 /*
408   open a file
409 */
410 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs, 
411                                 struct smbsrv_request *req, union smb_open *io)
412 {
413         struct cvfs_private *private = ntvfs->private_data;
414         struct smbcli_request *c_req;
415
416         SETUP_PID;
417
418         if (io->generic.level != RAW_OPEN_GENERIC &&
419             private->map_generic) {
420                 return ntvfs_map_open(req, io, ntvfs);
421         }
422
423         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
424                 return smb_raw_open(private->tree, req, io);
425         }
426
427         c_req = smb_raw_open_send(private->tree, io);
428
429         ASYNC_RECV_TAIL(io, async_open);
430 }
431
432 /*
433   create a directory
434 */
435 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs, 
436                                 struct smbsrv_request *req, union smb_mkdir *md)
437 {
438         struct cvfs_private *private = ntvfs->private_data;
439         struct smbcli_request *c_req;
440
441         SETUP_PID;
442
443         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
444                 return smb_raw_mkdir(private->tree, md);
445         }
446
447         c_req = smb_raw_mkdir_send(private->tree, md);
448
449         SIMPLE_ASYNC_TAIL;
450 }
451
452 /*
453   remove a directory
454 */
455 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs, 
456                                 struct smbsrv_request *req, struct smb_rmdir *rd)
457 {
458         struct cvfs_private *private = ntvfs->private_data;
459         struct smbcli_request *c_req;
460
461         SETUP_PID;
462
463         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
464                 return smb_raw_rmdir(private->tree, rd);
465         }
466         c_req = smb_raw_rmdir_send(private->tree, rd);
467
468         SIMPLE_ASYNC_TAIL;
469 }
470
471 /*
472   rename a set of files
473 */
474 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs, 
475                                 struct smbsrv_request *req, union smb_rename *ren)
476 {
477         struct cvfs_private *private = ntvfs->private_data;
478         struct smbcli_request *c_req;
479
480         SETUP_PID;
481
482         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
483                 return smb_raw_rename(private->tree, ren);
484         }
485
486         c_req = smb_raw_rename_send(private->tree, ren);
487
488         SIMPLE_ASYNC_TAIL;
489 }
490
491 /*
492   copy a set of files
493 */
494 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs, 
495                                 struct smbsrv_request *req, struct smb_copy *cp)
496 {
497         return NT_STATUS_NOT_SUPPORTED;
498 }
499
500 /*
501   a handler for async read replies
502  */
503 static void async_read(struct smbcli_request *c_req)
504 {
505         struct async_info *async = c_req->async.private;
506         struct smbsrv_request *req = async->req;
507         req->async_states->status = smb_raw_read_recv(c_req, async->parms);
508         req->async_states->send_fn(req);
509 }
510
511 /*
512   read from a file
513 */
514 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs, 
515                                 struct smbsrv_request *req, union smb_read *rd)
516 {
517         struct cvfs_private *private = ntvfs->private_data;
518         struct smbcli_request *c_req;
519
520         SETUP_PID;
521
522         if (rd->generic.level != RAW_READ_GENERIC &&
523             private->map_generic) {
524                 return ntvfs_map_read(req, rd, ntvfs);
525         }
526
527         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
528                 return smb_raw_read(private->tree, rd);
529         }
530
531         c_req = smb_raw_read_send(private->tree, rd);
532
533         ASYNC_RECV_TAIL(rd, async_read);
534 }
535
536 /*
537   a handler for async write replies
538  */
539 static void async_write(struct smbcli_request *c_req)
540 {
541         struct async_info *async = c_req->async.private;
542         struct smbsrv_request *req = async->req;
543         req->async_states->status = smb_raw_write_recv(c_req, async->parms);
544         req->async_states->send_fn(req);
545 }
546
547 /*
548   write to a file
549 */
550 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs, 
551                                 struct smbsrv_request *req, union smb_write *wr)
552 {
553         struct cvfs_private *private = ntvfs->private_data;
554         struct smbcli_request *c_req;
555
556         SETUP_PID;
557
558         if (wr->generic.level != RAW_WRITE_GENERIC &&
559             private->map_generic) {
560                 return ntvfs_map_write(req, wr, ntvfs);
561         }
562
563         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
564                 return smb_raw_write(private->tree, wr);
565         }
566
567         c_req = smb_raw_write_send(private->tree, wr);
568
569         ASYNC_RECV_TAIL(wr, async_write);
570 }
571
572 /*
573   a handler for async seek replies
574  */
575 static void async_seek(struct smbcli_request *c_req)
576 {
577         struct async_info *async = c_req->async.private;
578         struct smbsrv_request *req = async->req;
579         req->async_states->status = smb_raw_seek_recv(c_req, async->parms);
580         req->async_states->send_fn(req);
581 }
582
583 /*
584   seek in a file
585 */
586 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs, 
587                           struct smbsrv_request *req, struct 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 smbsrv_request *req, struct smb_flush *io)
608 {
609         struct cvfs_private *private = ntvfs->private_data;
610         struct smbcli_request *c_req;
611
612         SETUP_PID;
613
614         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
615                 return smb_raw_flush(private->tree, io);
616         }
617
618         c_req = smb_raw_flush_send(private->tree, io);
619
620         SIMPLE_ASYNC_TAIL;
621 }
622
623 /*
624   close a file
625 */
626 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs, 
627                                 struct smbsrv_request *req, union smb_close *io)
628 {
629         struct cvfs_private *private = ntvfs->private_data;
630         struct smbcli_request *c_req;
631
632         SETUP_PID;
633
634         if (io->generic.level != RAW_CLOSE_GENERIC &&
635             private->map_generic) {
636                 return ntvfs_map_close(req, io, ntvfs);
637         }
638
639         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
640                 return smb_raw_close(private->tree, io);
641         }
642
643         c_req = smb_raw_close_send(private->tree, io);
644
645         SIMPLE_ASYNC_TAIL;
646 }
647
648 /*
649   exit - closing files open by the pid
650 */
651 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs, 
652                                 struct smbsrv_request *req)
653 {
654         struct cvfs_private *private = ntvfs->private_data;
655         struct smbcli_request *c_req;
656
657         SETUP_PID;
658
659         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
660                 return smb_raw_exit(private->tree->session);
661         }
662
663         c_req = smb_raw_exit_send(private->tree->session);
664
665         SIMPLE_ASYNC_TAIL;
666 }
667
668 /*
669   logoff - closing files open by the user
670 */
671 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs, 
672                                 struct smbsrv_request *req)
673 {
674         /* we can't do this right in the cifs backend .... */
675         return NT_STATUS_OK;
676 }
677
678 /*
679   setup for an async call - nothing to do yet
680 */
681 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs, 
682                                  struct smbsrv_request *req, 
683                                  void *private)
684 {
685         return NT_STATUS_OK;
686 }
687
688 /*
689   cancel an async call
690 */
691 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs, 
692                             struct smbsrv_request *req)
693 {
694         struct cvfs_private *private = ntvfs->private_data;
695
696         /* find the matching request */
697         struct async_info *a;
698         for (a=private->pending;a;a=a->next) {
699                 if (SVAL(a->req->in.hdr, HDR_MID) == SVAL(req->in.hdr, HDR_MID)) {
700                         break;
701                 }
702         }
703
704         if (a == NULL) {
705                 return NT_STATUS_INVALID_PARAMETER;
706         }
707
708         return smb_raw_ntcancel(a->c_req);
709 }
710
711 /*
712   lock a byte range
713 */
714 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs, 
715                                 struct smbsrv_request *req, union smb_lock *lck)
716 {
717         struct cvfs_private *private = ntvfs->private_data;
718         struct smbcli_request *c_req;
719
720         SETUP_PID;
721
722         if (lck->generic.level != RAW_LOCK_GENERIC &&
723             private->map_generic) {
724                 return ntvfs_map_lock(req, lck, ntvfs);
725         }
726
727         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
728                 return smb_raw_lock(private->tree, lck);
729         }
730
731         c_req = smb_raw_lock_send(private->tree, lck);
732         SIMPLE_ASYNC_TAIL;
733 }
734
735 /*
736   set info on a open file
737 */
738 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs, 
739                                  struct smbsrv_request *req, 
740                                  union smb_setfileinfo *info)
741 {
742         struct cvfs_private *private = ntvfs->private_data;
743         struct smbcli_request *c_req;
744
745         SETUP_PID;
746
747         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
748                 return smb_raw_setfileinfo(private->tree, info);
749         }
750         c_req = smb_raw_setfileinfo_send(private->tree, info);
751
752         SIMPLE_ASYNC_TAIL;
753 }
754
755
756 /*
757   a handler for async fsinfo replies
758  */
759 static void async_fsinfo(struct smbcli_request *c_req)
760 {
761         struct async_info *async = c_req->async.private;
762         struct smbsrv_request *req = async->req;
763         req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
764         req->async_states->send_fn(req);
765 }
766
767 /*
768   return filesystem space info
769 */
770 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs, 
771                                 struct smbsrv_request *req, union smb_fsinfo *fs)
772 {
773         struct cvfs_private *private = ntvfs->private_data;
774         struct smbcli_request *c_req;
775
776         SETUP_PID;
777
778         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
779                 return smb_raw_fsinfo(private->tree, req, fs);
780         }
781
782         c_req = smb_raw_fsinfo_send(private->tree, req, fs);
783
784         ASYNC_RECV_TAIL(fs, async_fsinfo);
785 }
786
787 /*
788   return print queue info
789 */
790 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs, 
791                                 struct smbsrv_request *req, union smb_lpq *lpq)
792 {
793         return NT_STATUS_NOT_SUPPORTED;
794 }
795
796 /* 
797    list files in a directory matching a wildcard pattern
798 */
799 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs, 
800                                   struct smbsrv_request *req, union smb_search_first *io, 
801                                   void *search_private, 
802                                   BOOL (*callback)(void *, union smb_search_data *))
803 {
804         struct cvfs_private *private = ntvfs->private_data;
805
806         SETUP_PID;
807
808         return smb_raw_search_first(private->tree, req, io, search_private, callback);
809 }
810
811 /* continue a search */
812 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs, 
813                                  struct smbsrv_request *req, union smb_search_next *io, 
814                                  void *search_private, 
815                                  BOOL (*callback)(void *, union smb_search_data *))
816 {
817         struct cvfs_private *private = ntvfs->private_data;
818
819         SETUP_PID;
820
821         return smb_raw_search_next(private->tree, req, io, search_private, callback);
822 }
823
824 /* close a search */
825 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs, 
826                                   struct smbsrv_request *req, union smb_search_close *io)
827 {
828         struct cvfs_private *private = ntvfs->private_data;
829
830         SETUP_PID;
831
832         return smb_raw_search_close(private->tree, io);
833 }
834
835 /*
836   a handler for async trans2 replies
837  */
838 static void async_trans2(struct smbcli_request *c_req)
839 {
840         struct async_info *async = c_req->async.private;
841         struct smbsrv_request *req = async->req;
842         req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
843         req->async_states->send_fn(req);
844 }
845
846 /* raw trans2 */
847 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs, 
848                                 struct smbsrv_request *req, struct smb_trans2 *trans2)
849 {
850         struct cvfs_private *private = ntvfs->private_data;
851         struct smbcli_request *c_req;
852
853         SETUP_PID;
854
855         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
856                 return smb_raw_trans2(private->tree, req, trans2);
857         }
858
859         c_req = smb_raw_trans2_send(private->tree, trans2);
860
861         ASYNC_RECV_TAIL(trans2, async_trans2);
862 }
863
864
865 /* SMBtrans - not used on file shares */
866 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs, 
867                                 struct smbsrv_request *req, struct smb_trans2 *trans2)
868 {
869         return NT_STATUS_ACCESS_DENIED;
870 }
871
872 /*
873   a handler for async change notify replies
874  */
875 static void async_changenotify(struct smbcli_request *c_req)
876 {
877         struct async_info *async = c_req->async.private;
878         struct smbsrv_request *req = async->req;
879         req->async_states->status = smb_raw_changenotify_recv(c_req, req, async->parms);
880         req->async_states->send_fn(req);
881 }
882
883 /* change notify request - always async */
884 static NTSTATUS cvfs_notify(struct ntvfs_module_context *ntvfs, 
885                             struct smbsrv_request *req, struct smb_notify *info)
886 {
887         struct cvfs_private *private = ntvfs->private_data;
888         struct smbcli_request *c_req;
889
890         SETUP_PID;
891
892         /* this request doesn't make sense unless its async */
893         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
894                 return NT_STATUS_INVALID_PARAMETER;
895         }
896         
897         c_req = smb_raw_changenotify_send(private->tree, info);
898
899         ASYNC_RECV_TAIL(info, async_changenotify);
900 }
901
902 /*
903   initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
904  */
905 NTSTATUS ntvfs_cifs_init(void)
906 {
907         NTSTATUS ret;
908         struct ntvfs_ops ops;
909
910         ZERO_STRUCT(ops);
911
912         /* fill in the name and type */
913         ops.name = "cifs";
914         ops.type = NTVFS_DISK;
915         
916         /* fill in all the operations */
917         ops.connect = cvfs_connect;
918         ops.disconnect = cvfs_disconnect;
919         ops.unlink = cvfs_unlink;
920         ops.chkpath = cvfs_chkpath;
921         ops.qpathinfo = cvfs_qpathinfo;
922         ops.setpathinfo = cvfs_setpathinfo;
923         ops.openfile = cvfs_open;
924         ops.mkdir = cvfs_mkdir;
925         ops.rmdir = cvfs_rmdir;
926         ops.rename = cvfs_rename;
927         ops.copy = cvfs_copy;
928         ops.ioctl = cvfs_ioctl;
929         ops.read = cvfs_read;
930         ops.write = cvfs_write;
931         ops.seek = cvfs_seek;
932         ops.flush = cvfs_flush; 
933         ops.close = cvfs_close;
934         ops.exit = cvfs_exit;
935         ops.lock = cvfs_lock;
936         ops.setfileinfo = cvfs_setfileinfo;
937         ops.qfileinfo = cvfs_qfileinfo;
938         ops.fsinfo = cvfs_fsinfo;
939         ops.lpq = cvfs_lpq;
940         ops.search_first = cvfs_search_first;
941         ops.search_next = cvfs_search_next;
942         ops.search_close = cvfs_search_close;
943         ops.trans = cvfs_trans;
944         ops.logoff = cvfs_logoff;
945         ops.async_setup = cvfs_async_setup;
946         ops.cancel = cvfs_cancel;
947         ops.notify = cvfs_notify;
948
949         if (lp_parm_bool(-1, "cifs", "maptrans2", False)) {
950                 ops.trans2 = cvfs_trans2;
951         }
952
953         /* register ourselves with the NTVFS subsystem. We register
954            under the name 'cifs'. */
955         ret = ntvfs_register(&ops);
956
957         if (!NT_STATUS_IS_OK(ret)) {
958                 DEBUG(0,("Failed to register CIFS backend!\n"));
959         }
960         
961         return ret;
962 }