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