s3: piddir creation fix part 2.
[ira/wip.git] / source4 / ntvfs / smb2 / vfs_smb2.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    CIFS-to-SMB2 NTVFS filesystem backend
5
6    Copyright (C) Andrew Tridgell 2008
7
8    largely based on vfs_cifs.c which was 
9       Copyright (C) Andrew Tridgell 2003
10       Copyright (C) James J Myers 2003 <myersjj@samba.org>
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16    
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21    
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25 /*
26   this implements a CIFS->CIFS NTVFS filesystem backend. 
27   
28 */
29
30 #include "includes.h"
31 #include "libcli/raw/libcliraw.h"
32 #include "libcli/raw/raw_proto.h"
33 #include "libcli/composite/composite.h"
34 #include "libcli/smb_composite/smb_composite.h"
35 #include "auth/auth.h"
36 #include "auth/credentials/credentials.h"
37 #include "ntvfs/ntvfs.h"
38 #include "../lib/util/dlinklist.h"
39 #include "param/param.h"
40 #include "libcli/resolve/resolve.h"
41 #include "libcli/smb2/smb2.h"
42 #include "libcli/smb2/smb2_calls.h"
43
44 NTSTATUS ntvfs_smb2_init(void);
45
46 struct cvfs_file {
47         struct cvfs_file *prev, *next;
48         uint16_t fnum;
49         struct ntvfs_handle *h;
50 };
51
52 /* this is stored in ntvfs_private */
53 struct cvfs_private {
54         struct smb2_tree *tree;
55         struct smb2_transport *transport;
56         struct ntvfs_module_context *ntvfs;
57         struct async_info *pending;
58         struct cvfs_file *files;
59
60         /* a handle on the root of the share */
61         /* TODO: leaving this handle open could prevent other users
62            from opening the share with exclusive access. We probably
63            need to open it on demand */
64         struct smb2_handle roothandle;
65 };
66
67
68 /* a structure used to pass information to an async handler */
69 struct async_info {
70         struct async_info *next, *prev;
71         struct cvfs_private *cvfs;
72         struct ntvfs_request *req;
73         void *c_req;
74         struct composite_context *c_comp;
75         struct cvfs_file *f;
76         void *parms;
77 };
78
79 #define SETUP_FILE_HERE(f) do { \
80         f = ntvfs_handle_get_backend_data(io->generic.in.file.ntvfs, ntvfs); \
81         if (!f) return NT_STATUS_INVALID_HANDLE; \
82         io->generic.in.file.fnum = f->fnum; \
83 } while (0)
84
85 #define SETUP_FILE do { \
86         struct cvfs_file *f; \
87         SETUP_FILE_HERE(f); \
88 } while (0)
89
90 #define SMB2_SERVER             "smb2:server"
91 #define SMB2_USER               "smb2:user"
92 #define SMB2_PASSWORD           "smb2:password"
93 #define SMB2_DOMAIN             "smb2:domain"
94 #define SMB2_SHARE              "smb2:share"
95 #define SMB2_USE_MACHINE_ACCT   "smb2:use-machine-account"
96
97 #define SMB2_USE_MACHINE_ACCT_DEFAULT   false
98
99 /*
100   a handler for oplock break events from the server - these need to be passed
101   along to the client
102  */
103 static bool oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *p_private)
104 {
105         struct cvfs_private *p = p_private;
106         NTSTATUS status;
107         struct ntvfs_handle *h = NULL;
108         struct cvfs_file *f;
109
110         for (f=p->files; f; f=f->next) {
111                 if (f->fnum != fnum) continue;
112                 h = f->h;
113                 break;
114         }
115
116         if (!h) {
117                 DEBUG(5,("vfs_smb2: ignoring oplock break level %d for fnum %d\n", level, fnum));
118                 return true;
119         }
120
121         DEBUG(5,("vfs_smb2: sending oplock break level %d for fnum %d\n", level, fnum));
122         status = ntvfs_send_oplock_break(p->ntvfs, h, level);
123         if (!NT_STATUS_IS_OK(status)) return false;
124         return true;
125 }
126
127 /*
128   return a handle to the root of the share
129 */
130 static NTSTATUS smb2_get_roothandle(struct smb2_tree *tree, struct smb2_handle *handle)
131 {
132         struct smb2_create io;
133         NTSTATUS status;
134
135         ZERO_STRUCT(io);
136         io.in.oplock_level = 0;
137         io.in.desired_access = SEC_STD_SYNCHRONIZE | SEC_DIR_READ_ATTRIBUTE | SEC_DIR_LIST;
138         io.in.file_attributes   = 0;
139         io.in.create_disposition = NTCREATEX_DISP_OPEN;
140         io.in.share_access = 
141                 NTCREATEX_SHARE_ACCESS_READ |
142                 NTCREATEX_SHARE_ACCESS_WRITE|
143                 NTCREATEX_SHARE_ACCESS_DELETE;
144         io.in.create_options = 0;
145         io.in.fname = NULL;
146
147         status = smb2_create(tree, tree, &io);
148         NT_STATUS_NOT_OK_RETURN(status);
149
150         *handle = io.out.file.handle;
151
152         return NT_STATUS_OK;
153 }
154
155 /*
156   connect to a share - used when a tree_connect operation comes in.
157 */
158 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs, 
159                              struct ntvfs_request *req,
160                              union smb_tcon* tcon)
161 {
162         NTSTATUS status;
163         struct cvfs_private *p;
164         const char *host, *user, *pass, *domain, *remote_share, *sharename;
165         struct share_config *scfg = ntvfs->ctx->config;
166         struct smb2_tree *tree;
167         struct cli_credentials *credentials;
168         bool machine_account;
169         struct smbcli_options options;
170
171         switch (tcon->generic.level) {
172         case RAW_TCON_TCON:
173                 sharename = tcon->tcon.in.service;
174                 break;
175         case RAW_TCON_TCONX:
176                 sharename = tcon->tconx.in.path;
177                 break;
178         case RAW_TCON_SMB2:
179                 sharename = tcon->smb2.in.path;
180                 break;
181         default:
182                 return NT_STATUS_INVALID_LEVEL;
183         }
184
185         if (strncmp(sharename, "\\\\", 2) == 0) {
186                 char *str = strchr(sharename+2, '\\');
187                 if (str) {
188                         sharename = str + 1;
189                 }
190         }
191
192         /* Here we need to determine which server to connect to.
193          * For now we use parametric options, type cifs.
194          * Later we will use security=server and auth_server.c.
195          */
196         host = share_string_option(scfg, SMB2_SERVER, NULL);
197         user = share_string_option(scfg, SMB2_USER, NULL);
198         pass = share_string_option(scfg, SMB2_PASSWORD, NULL);
199         domain = share_string_option(scfg, SMB2_DOMAIN, NULL);
200         remote_share = share_string_option(scfg, SMB2_SHARE, NULL);
201         if (!remote_share) {
202                 remote_share = sharename;
203         }
204
205         machine_account = share_bool_option(scfg, SMB2_USE_MACHINE_ACCT, SMB2_USE_MACHINE_ACCT_DEFAULT);
206
207         p = talloc_zero(ntvfs, struct cvfs_private);
208         if (!p) {
209                 return NT_STATUS_NO_MEMORY;
210         }
211
212         ntvfs->private_data = p;
213
214         if (!host) {
215                 DEBUG(1,("CIFS backend: You must supply server\n"));
216                 return NT_STATUS_INVALID_PARAMETER;
217         } 
218         
219         if (user && pass) {
220                 DEBUG(5, ("CIFS backend: Using specified password\n"));
221                 credentials = cli_credentials_init(p);
222                 if (!credentials) {
223                         return NT_STATUS_NO_MEMORY;
224                 }
225                 cli_credentials_set_conf(credentials, ntvfs->ctx->lp_ctx);
226                 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
227                 if (domain) {
228                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
229                 }
230                 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
231         } else if (machine_account) {
232                 DEBUG(5, ("CIFS backend: Using machine account\n"));
233                 credentials = cli_credentials_init(p);
234                 cli_credentials_set_conf(credentials, ntvfs->ctx->lp_ctx);
235                 if (domain) {
236                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
237                 }
238                 status = cli_credentials_set_machine_account(credentials, ntvfs->ctx->lp_ctx);
239                 if (!NT_STATUS_IS_OK(status)) {
240                         return status;
241                 }
242         } else if (req->session_info->credentials) {
243                 DEBUG(5, ("CIFS backend: Using delegated credentials\n"));
244                 credentials = req->session_info->credentials;
245         } else {
246                 DEBUG(1,("CIFS backend: NO delegated credentials found: You must supply server, user and password or the client must supply delegated credentials\n"));
247                 return NT_STATUS_INVALID_PARAMETER;
248         }
249
250         lpcfg_smbcli_options(ntvfs->ctx->lp_ctx, &options);
251
252         status = smb2_connect(p, host,
253                         lpcfg_parm_string_list(p, ntvfs->ctx->lp_ctx, NULL, "smb2", "ports", NULL),
254                         remote_share,
255                         lpcfg_resolve_context(ntvfs->ctx->lp_ctx),
256                         credentials,
257                         &tree,
258                         ntvfs->ctx->event_ctx, &options,
259                         lpcfg_socket_options(ntvfs->ctx->lp_ctx),
260                         lpcfg_gensec_settings(p, ntvfs->ctx->lp_ctx));
261         NT_STATUS_NOT_OK_RETURN(status);
262
263         status = smb2_get_roothandle(tree, &p->roothandle);
264         NT_STATUS_NOT_OK_RETURN(status);
265
266         p->tree = tree;
267         p->transport = p->tree->session->transport;
268         p->ntvfs = ntvfs;
269
270         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
271         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
272         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
273         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
274
275         if (tcon->generic.level == RAW_TCON_TCONX) {
276                 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
277                 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
278         }
279
280         /* we need to receive oplock break requests from the server */
281         /* TODO: enable oplocks 
282         smbcli_oplock_handler(p->transport, oplock_handler, p);
283         */
284         return NT_STATUS_OK;
285 }
286
287 /*
288   disconnect from a share
289 */
290 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs)
291 {
292         struct cvfs_private *p = ntvfs->private_data;
293         struct async_info *a, *an;
294
295         /* first cleanup pending requests */
296         for (a=p->pending; a; a = an) {
297                 an = a->next;
298                 talloc_free(a->c_req);
299                 talloc_free(a);
300         }
301
302         talloc_free(p);
303         ntvfs->private_data = NULL;
304
305         return NT_STATUS_OK;
306 }
307
308 /*
309   destroy an async info structure
310 */
311 static int async_info_destructor(struct async_info *async)
312 {
313         DLIST_REMOVE(async->cvfs->pending, async);
314         return 0;
315 }
316
317 /*
318   a handler for simple async SMB2 replies
319   this handler can only be used for functions that don't return any
320   parameters (those that just return a status code)
321  */
322 static void async_simple_smb2(struct smb2_request *c_req)
323 {
324         struct async_info *async = c_req->async.private_data;
325         struct ntvfs_request *req = async->req;
326
327         smb2_request_receive(c_req);
328         req->async_states->status = smb2_request_destroy(c_req);
329         talloc_free(async);
330         req->async_states->send_fn(req);
331 }
332
333 /*
334   a handler for simple async composite replies
335   this handler can only be used for functions that don't return any
336   parameters (those that just return a status code)
337  */
338 static void async_simple_composite(struct composite_context *c_req)
339 {
340         struct async_info *async = c_req->async.private_data;
341         struct ntvfs_request *req = async->req;
342
343         req->async_states->status = composite_wait_free(c_req);
344         talloc_free(async);
345         req->async_states->send_fn(req);
346 }
347
348
349 /* save some typing for the simple functions */
350 #define ASYNC_RECV_TAIL_F(io, async_fn, file) do { \
351         if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
352         { \
353                 struct async_info *async; \
354                 async = talloc(req, struct async_info); \
355                 if (!async) return NT_STATUS_NO_MEMORY; \
356                 async->parms = io; \
357                 async->req = req; \
358                 async->f = file; \
359                 async->cvfs = p; \
360                 async->c_req = c_req; \
361                 DLIST_ADD(p->pending, async); \
362                 c_req->async.private_data = async; \
363                 talloc_set_destructor(async, async_info_destructor); \
364         } \
365         c_req->async.fn = async_fn; \
366         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
367         return NT_STATUS_OK; \
368 } while (0)
369
370 #define ASYNC_RECV_TAIL(io, async_fn) ASYNC_RECV_TAIL_F(io, async_fn, NULL)
371
372 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple_smb2)
373 #define SIMPLE_COMPOSITE_TAIL ASYNC_RECV_TAIL(NULL, async_simple_composite)
374
375 #define CHECK_ASYNC(req) do { \
376         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) { \
377                 DEBUG(0,("SMB2 proxy backend does not support sync operation at %s\n", \
378                          __location__)); \
379                 return NT_STATUS_NOT_IMPLEMENTED; \
380         }} while (0)
381
382 /*
383   delete a file - the dirtype specifies the file types to include in the search. 
384   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
385
386   BUGS:
387      - doesn't handle wildcards
388      - doesn't obey attrib restrictions
389 */
390 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs, 
391                             struct ntvfs_request *req, union smb_unlink *unl)
392 {
393         struct cvfs_private *p = ntvfs->private_data;
394         struct composite_context *c_req;
395
396         CHECK_ASYNC(req);
397
398         c_req = smb2_composite_unlink_send(p->tree, unl);
399
400         SIMPLE_COMPOSITE_TAIL;
401 }
402
403 /*
404   ioctl interface
405 */
406 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs, 
407                            struct ntvfs_request *req, union smb_ioctl *io)
408 {
409         return NT_STATUS_NOT_IMPLEMENTED;
410 }
411
412 /*
413   check if a directory exists
414 */
415 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs, 
416                              struct ntvfs_request *req, union smb_chkpath *cp)
417 {
418         struct cvfs_private *p = ntvfs->private_data;
419         struct smb2_request *c_req;
420         struct smb2_find f;
421
422         CHECK_ASYNC(req);
423         
424         /* SMB2 doesn't have a chkpath operation, and also doesn't
425          have a query path info call, so the best seems to be to do a
426          find call, using the roothandle we established at connect
427          time */
428         ZERO_STRUCT(f);
429         f.in.file.handle        = p->roothandle;
430         f.in.level              = SMB2_FIND_DIRECTORY_INFO;
431         f.in.pattern            = cp->chkpath.in.path;
432         /* SMB2 find doesn't accept \ or the empty string - this is the best
433            approximation */
434         if (strcmp(f.in.pattern, "\\") == 0 || 
435             strcmp(f.in.pattern, "") == 0) {
436                 f.in.pattern            = "?";
437         }
438         f.in.continue_flags     = SMB2_CONTINUE_FLAG_SINGLE | SMB2_CONTINUE_FLAG_RESTART;
439         f.in.max_response_size  = 0x1000;
440         
441         c_req = smb2_find_send(p->tree, &f);
442
443         SIMPLE_ASYNC_TAIL;
444 }
445
446 /*
447   return info on a pathname
448 */
449 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs, 
450                                struct ntvfs_request *req, union smb_fileinfo *info)
451 {
452         return NT_STATUS_NOT_IMPLEMENTED;
453 }
454
455 /*
456   query info on a open file
457 */
458 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs, 
459                                struct ntvfs_request *req, union smb_fileinfo *io)
460 {
461         return NT_STATUS_NOT_IMPLEMENTED;
462 }
463
464
465 /*
466   set info on a pathname
467 */
468 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs, 
469                                  struct ntvfs_request *req, union smb_setfileinfo *st)
470 {
471         return NT_STATUS_NOT_IMPLEMENTED;
472 }
473
474
475 /*
476   open a file
477 */
478 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs, 
479                           struct ntvfs_request *req, union smb_open *io)
480 {
481         return NT_STATUS_NOT_IMPLEMENTED;
482 }
483
484 /*
485   create a directory
486 */
487 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs, 
488                            struct ntvfs_request *req, union smb_mkdir *md)
489 {
490         struct cvfs_private *p = ntvfs->private_data;
491         struct composite_context *c_req;
492
493         CHECK_ASYNC(req);
494
495         c_req = smb2_composite_mkdir_send(p->tree, md);
496
497         SIMPLE_COMPOSITE_TAIL;
498 }
499
500 /*
501   remove a directory
502 */
503 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs, 
504                            struct ntvfs_request *req, struct smb_rmdir *rd)
505 {
506         struct cvfs_private *p = ntvfs->private_data;
507         struct composite_context *c_req;
508
509         CHECK_ASYNC(req);
510
511         c_req = smb2_composite_rmdir_send(p->tree, rd);
512
513         SIMPLE_COMPOSITE_TAIL;
514 }
515
516 /*
517   rename a set of files
518 */
519 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs, 
520                             struct ntvfs_request *req, union smb_rename *ren)
521 {
522         return NT_STATUS_NOT_IMPLEMENTED;
523 }
524
525 /*
526   copy a set of files
527 */
528 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs, 
529                           struct ntvfs_request *req, struct smb_copy *cp)
530 {
531         return NT_STATUS_NOT_SUPPORTED;
532 }
533
534 /*
535   read from a file
536 */
537 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs, 
538                           struct ntvfs_request *req, union smb_read *io)
539 {
540         return NT_STATUS_NOT_IMPLEMENTED;
541 }
542
543 /*
544   write to a file
545 */
546 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs, 
547                            struct ntvfs_request *req, union smb_write *io)
548 {
549         return NT_STATUS_NOT_IMPLEMENTED;
550 }
551
552 /*
553   seek in a file
554 */
555 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs, 
556                           struct ntvfs_request *req,
557                           union smb_seek *io)
558 {
559         return NT_STATUS_NOT_IMPLEMENTED;
560 }
561
562 /*
563   flush a file
564 */
565 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs, 
566                            struct ntvfs_request *req,
567                            union smb_flush *io)
568 {
569         return NT_STATUS_NOT_IMPLEMENTED;
570 }
571
572 /*
573   close a file
574 */
575 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs, 
576                            struct ntvfs_request *req, union smb_close *io)
577 {
578         return NT_STATUS_NOT_IMPLEMENTED;
579 }
580
581 /*
582   exit - closing files open by the pid
583 */
584 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs, 
585                           struct ntvfs_request *req)
586 {
587         return NT_STATUS_NOT_IMPLEMENTED;
588 }
589
590 /*
591   logoff - closing files open by the user
592 */
593 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs, 
594                             struct ntvfs_request *req)
595 {
596         /* we can't do this right in the cifs backend .... */
597         return NT_STATUS_OK;
598 }
599
600 /*
601   setup for an async call - nothing to do yet
602 */
603 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs, 
604                                  struct ntvfs_request *req, 
605                                  void *private_data)
606 {
607         return NT_STATUS_OK;
608 }
609
610 /*
611   cancel an async call
612 */
613 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs, 
614                             struct ntvfs_request *req)
615 {
616         return NT_STATUS_NOT_IMPLEMENTED;
617 }
618
619 /*
620   lock a byte range
621 */
622 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs, 
623                           struct ntvfs_request *req, union smb_lock *io)
624 {
625         return NT_STATUS_NOT_IMPLEMENTED;
626 }
627
628 /*
629   set info on a open file
630 */
631 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs, 
632                                  struct ntvfs_request *req, 
633                                  union smb_setfileinfo *io)
634 {
635         return NT_STATUS_NOT_IMPLEMENTED;
636 }
637
638
639 /*
640   a handler for async fsinfo replies
641  */
642 static void async_fsinfo(struct smb2_request *c_req)
643 {
644         struct async_info *async = c_req->async.private_data;
645         struct ntvfs_request *req = async->req;
646         req->async_states->status = smb2_getinfo_fs_recv(c_req, req, async->parms);
647         talloc_free(async);
648         req->async_states->send_fn(req);
649 }
650
651 /*
652   return filesystem space info
653 */
654 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs, 
655                             struct ntvfs_request *req, union smb_fsinfo *fs)
656 {
657         struct cvfs_private *p = ntvfs->private_data;
658         struct smb2_request *c_req;
659         enum smb_fsinfo_level level = fs->generic.level;
660
661         CHECK_ASYNC(req);
662
663         switch (level) {
664                 /* some levels go straight through */
665         case RAW_QFS_VOLUME_INFORMATION:
666         case RAW_QFS_SIZE_INFORMATION:
667         case RAW_QFS_DEVICE_INFORMATION:
668         case RAW_QFS_ATTRIBUTE_INFORMATION:
669         case RAW_QFS_QUOTA_INFORMATION:
670         case RAW_QFS_FULL_SIZE_INFORMATION:
671         case RAW_QFS_OBJECTID_INFORMATION:
672                 break;
673
674                 /* some get mapped */
675         case RAW_QFS_VOLUME_INFO:
676                 level = RAW_QFS_VOLUME_INFORMATION;
677                 break;
678         case RAW_QFS_SIZE_INFO:
679                 level = RAW_QFS_SIZE_INFORMATION;
680                 break;
681         case RAW_QFS_DEVICE_INFO:
682                 level = RAW_QFS_DEVICE_INFORMATION;
683                 break;
684         case RAW_QFS_ATTRIBUTE_INFO:
685                 level = RAW_QFS_ATTRIBUTE_INFO;
686                 break;
687
688         default:
689                 /* the rest get refused for now */
690                 DEBUG(0,("fsinfo level %u not possible on SMB2\n",
691                          (unsigned)fs->generic.level));
692                 break;
693         }
694
695         fs->generic.level = level;
696         fs->generic.handle = p->roothandle;
697
698         c_req = smb2_getinfo_fs_send(p->tree, fs);
699
700         ASYNC_RECV_TAIL(fs, async_fsinfo);
701 }
702
703 /*
704   return print queue info
705 */
706 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs, 
707                          struct ntvfs_request *req, union smb_lpq *lpq)
708 {
709         return NT_STATUS_NOT_SUPPORTED;
710 }
711
712 /* 
713    list files in a directory matching a wildcard pattern
714 */
715 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs, 
716                                   struct ntvfs_request *req, union smb_search_first *io, 
717                                   void *search_private, 
718                                   bool (*callback)(void *, const union smb_search_data *))
719 {
720         struct cvfs_private *p = ntvfs->private_data;
721         struct smb2_find f;
722         enum smb_search_data_level smb2_level;
723         unsigned int count, i;
724         union smb_search_data *data;
725         NTSTATUS status;
726
727         if (io->generic.level != RAW_SEARCH_TRANS2) {
728                 DEBUG(0,("We only support trans2 search in smb2 backend\n"));
729                 return NT_STATUS_NOT_SUPPORTED;
730         }
731
732         switch (io->generic.data_level) {
733         case RAW_SEARCH_DATA_DIRECTORY_INFO:
734                 smb2_level = SMB2_FIND_DIRECTORY_INFO;
735                 break;
736         case RAW_SEARCH_DATA_FULL_DIRECTORY_INFO:
737                 smb2_level = SMB2_FIND_FULL_DIRECTORY_INFO;
738                 break;
739         case RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO:
740                 smb2_level = SMB2_FIND_BOTH_DIRECTORY_INFO;
741                 break;
742         case RAW_SEARCH_DATA_NAME_INFO:
743                 smb2_level = SMB2_FIND_NAME_INFO;
744                 break;
745         case RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO:
746                 smb2_level = SMB2_FIND_ID_FULL_DIRECTORY_INFO;
747                 break;
748         case RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO:
749                 smb2_level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO;
750                 break;
751         default:
752                 DEBUG(0,("Unsupported search level %u for smb2 backend\n",
753                          (unsigned)io->generic.data_level));
754                 return NT_STATUS_INVALID_INFO_CLASS;
755         }
756
757         /* we do the search on the roothandle. This only works because
758            search is synchronous, otherwise we'd have no way to
759            distinguish multiple searches happening at once
760         */
761         ZERO_STRUCT(f);
762         f.in.file.handle        = p->roothandle;
763         f.in.level              = smb2_level;
764         f.in.pattern            = io->t2ffirst.in.pattern;
765         while (f.in.pattern[0] == '\\') {
766                 f.in.pattern++;
767         }
768         f.in.continue_flags     = 0;
769         f.in.max_response_size  = 0x10000;
770
771         status = smb2_find_level(p->tree, req, &f, &count, &data);
772         NT_STATUS_NOT_OK_RETURN(status);        
773
774         for (i=0;i<count;i++) {
775                 if (!callback(search_private, &data[i])) break;
776         }
777
778         io->t2ffirst.out.handle = 0;
779         io->t2ffirst.out.count = i;
780         /* TODO: fix end_of_file */
781         io->t2ffirst.out.end_of_search = 1;
782
783         talloc_free(data);
784         
785         return NT_STATUS_OK;
786 }
787
788 /* continue a search */
789 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs, 
790                                  struct ntvfs_request *req, union smb_search_next *io, 
791                                  void *search_private, 
792                                  bool (*callback)(void *, const union smb_search_data *))
793 {
794         return NT_STATUS_NOT_IMPLEMENTED;
795 }
796
797 /* close a search */
798 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs, 
799                                   struct ntvfs_request *req, union smb_search_close *io)
800 {
801         return NT_STATUS_NOT_IMPLEMENTED;
802 }
803
804 /* SMBtrans - not used on file shares */
805 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs, 
806                            struct ntvfs_request *req,
807                            struct smb_trans2 *trans2)
808 {
809         return NT_STATUS_ACCESS_DENIED;
810 }
811
812 /* change notify request - always async */
813 static NTSTATUS cvfs_notify(struct ntvfs_module_context *ntvfs, 
814                             struct ntvfs_request *req,
815                             union smb_notify *io)
816 {
817         return NT_STATUS_NOT_IMPLEMENTED;
818 }
819
820 /*
821   initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
822  */
823 NTSTATUS ntvfs_smb2_init(void)
824 {
825         NTSTATUS ret;
826         struct ntvfs_ops ops;
827         NTVFS_CURRENT_CRITICAL_SIZES(vers);
828
829         ZERO_STRUCT(ops);
830
831         /* fill in the name and type */
832         ops.name = "smb2";
833         ops.type = NTVFS_DISK;
834         
835         /* fill in all the operations */
836         ops.connect = cvfs_connect;
837         ops.disconnect = cvfs_disconnect;
838         ops.unlink = cvfs_unlink;
839         ops.chkpath = cvfs_chkpath;
840         ops.qpathinfo = cvfs_qpathinfo;
841         ops.setpathinfo = cvfs_setpathinfo;
842         ops.open = cvfs_open;
843         ops.mkdir = cvfs_mkdir;
844         ops.rmdir = cvfs_rmdir;
845         ops.rename = cvfs_rename;
846         ops.copy = cvfs_copy;
847         ops.ioctl = cvfs_ioctl;
848         ops.read = cvfs_read;
849         ops.write = cvfs_write;
850         ops.seek = cvfs_seek;
851         ops.flush = cvfs_flush; 
852         ops.close = cvfs_close;
853         ops.exit = cvfs_exit;
854         ops.lock = cvfs_lock;
855         ops.setfileinfo = cvfs_setfileinfo;
856         ops.qfileinfo = cvfs_qfileinfo;
857         ops.fsinfo = cvfs_fsinfo;
858         ops.lpq = cvfs_lpq;
859         ops.search_first = cvfs_search_first;
860         ops.search_next = cvfs_search_next;
861         ops.search_close = cvfs_search_close;
862         ops.trans = cvfs_trans;
863         ops.logoff = cvfs_logoff;
864         ops.async_setup = cvfs_async_setup;
865         ops.cancel = cvfs_cancel;
866         ops.notify = cvfs_notify;
867
868         /* register ourselves with the NTVFS subsystem. We register
869            under the name 'smb2'. */
870         ret = ntvfs_register(&ops, &vers);
871
872         if (!NT_STATUS_IS_OK(ret)) {
873                 DEBUG(0,("Failed to register SMB2 backend\n"));
874         }
875         
876         return ret;
877 }