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