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