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