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