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