68b475a0845fd355e4a9d5341aeff5e1f28bd95e
[bbaumbach/samba-autobuild/.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         struct cli_credentials *credentials;
166         bool machine_account;
167         struct smbcli_options options;
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         lp_smbcli_options(ntvfs->ctx->lp_ctx, &options);
228
229         creq = smb2_connect_send(private, host, remote_share, 
230                                  lp_resolve_context(ntvfs->ctx->lp_ctx),
231                                  credentials,
232                                  ntvfs->ctx->event_ctx, &options);
233
234         status = smb2_connect_recv(creq, private, &tree);
235         NT_STATUS_NOT_OK_RETURN(status);
236
237         status = smb2_get_roothandle(tree, &private->roothandle);
238         NT_STATUS_NOT_OK_RETURN(status);
239
240         private->tree = tree;
241         private->transport = private->tree->session->transport;
242         private->ntvfs = ntvfs;
243
244         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
245         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
246         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
247         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
248
249         /* we need to receive oplock break requests from the server */
250         /* TODO: enable oplocks 
251         smbcli_oplock_handler(private->transport, oplock_handler, private);
252         */
253         return NT_STATUS_OK;
254 }
255
256 /*
257   disconnect from a share
258 */
259 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs)
260 {
261         struct cvfs_private *private = ntvfs->private_data;
262         struct async_info *a, *an;
263
264         /* first cleanup pending requests */
265         for (a=private->pending; a; a = an) {
266                 an = a->next;
267                 talloc_free(a->c_req);
268                 talloc_free(a);
269         }
270
271         talloc_free(private);
272         ntvfs->private_data = NULL;
273
274         return NT_STATUS_OK;
275 }
276
277 /*
278   destroy an async info structure
279 */
280 static int async_info_destructor(struct async_info *async)
281 {
282         DLIST_REMOVE(async->cvfs->pending, async);
283         return 0;
284 }
285
286 /*
287   a handler for simple async SMB2 replies
288   this handler can only be used for functions that don't return any
289   parameters (those that just return a status code)
290  */
291 static void async_simple_smb2(struct smb2_request *c_req)
292 {
293         struct async_info *async = c_req->async.private_data;
294         struct ntvfs_request *req = async->req;
295
296         smb2_request_receive(c_req);
297         req->async_states->status = smb2_request_destroy(c_req);
298         talloc_free(async);
299         req->async_states->send_fn(req);
300 }
301
302 /*
303   a handler for simple async composite replies
304   this handler can only be used for functions that don't return any
305   parameters (those that just return a status code)
306  */
307 static void async_simple_composite(struct composite_context *c_req)
308 {
309         struct async_info *async = c_req->async.private_data;
310         struct ntvfs_request *req = async->req;
311
312         req->async_states->status = composite_wait_free(c_req);
313         talloc_free(async);
314         req->async_states->send_fn(req);
315 }
316
317
318 /* save some typing for the simple functions */
319 #define ASYNC_RECV_TAIL_F(io, async_fn, file) do { \
320         if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
321         { \
322                 struct async_info *async; \
323                 async = talloc(req, struct async_info); \
324                 if (!async) return NT_STATUS_NO_MEMORY; \
325                 async->parms = io; \
326                 async->req = req; \
327                 async->f = file; \
328                 async->cvfs = private; \
329                 async->c_req = c_req; \
330                 DLIST_ADD(private->pending, async); \
331                 c_req->async.private_data = async; \
332                 talloc_set_destructor(async, async_info_destructor); \
333         } \
334         c_req->async.fn = async_fn; \
335         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
336         return NT_STATUS_OK; \
337 } while (0)
338
339 #define ASYNC_RECV_TAIL(io, async_fn) ASYNC_RECV_TAIL_F(io, async_fn, NULL)
340
341 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple_smb2)
342 #define SIMPLE_COMPOSITE_TAIL ASYNC_RECV_TAIL(NULL, async_simple_composite)
343
344 #define CHECK_ASYNC(req) do { \
345         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) { \
346                 DEBUG(0,("SMB2 proxy backend does not support sync operation at %s\n", \
347                          __location__)); \
348                 return NT_STATUS_NOT_IMPLEMENTED; \
349         }} while (0)
350
351 /*
352   delete a file - the dirtype specifies the file types to include in the search. 
353   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
354
355   BUGS:
356      - doesn't handle wildcards
357      - doesn't obey attrib restrictions
358 */
359 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs, 
360                             struct ntvfs_request *req, union smb_unlink *unl)
361 {
362         struct cvfs_private *private = ntvfs->private_data;
363         struct composite_context *c_req;
364
365         CHECK_ASYNC(req);
366
367         c_req = smb2_composite_unlink_send(private->tree, unl);
368
369         SIMPLE_COMPOSITE_TAIL;
370 }
371
372 /*
373   ioctl interface
374 */
375 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs, 
376                            struct ntvfs_request *req, union smb_ioctl *io)
377 {
378         return NT_STATUS_NOT_IMPLEMENTED;
379 }
380
381 /*
382   check if a directory exists
383 */
384 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs, 
385                              struct ntvfs_request *req, union smb_chkpath *cp)
386 {
387         struct cvfs_private *private = ntvfs->private_data;
388         struct smb2_request *c_req;
389         struct smb2_find f;
390
391         CHECK_ASYNC(req);
392         
393         /* SMB2 doesn't have a chkpath operation, and also doesn't
394          have a query path info call, so the best seems to be to do a
395          find call, using the roothandle we established at connect
396          time */
397         ZERO_STRUCT(f);
398         f.in.file.handle        = private->roothandle;
399         f.in.level              = SMB2_FIND_DIRECTORY_INFO;
400         f.in.pattern            = cp->chkpath.in.path;
401         /* SMB2 find doesn't accept \ or the empty string - this is the best
402            approximation */
403         if (strcmp(f.in.pattern, "\\") == 0 || 
404             strcmp(f.in.pattern, "") == 0) {
405                 f.in.pattern            = "?";
406         }
407         f.in.continue_flags     = SMB2_CONTINUE_FLAG_SINGLE | SMB2_CONTINUE_FLAG_RESTART;
408         f.in.max_response_size  = 0x1000;
409         
410         c_req = smb2_find_send(private->tree, &f);
411
412         SIMPLE_ASYNC_TAIL;
413 }
414
415 /*
416   return info on a pathname
417 */
418 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs, 
419                                struct ntvfs_request *req, union smb_fileinfo *info)
420 {
421         return NT_STATUS_NOT_IMPLEMENTED;
422 }
423
424 /*
425   query info on a open file
426 */
427 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs, 
428                                struct ntvfs_request *req, union smb_fileinfo *io)
429 {
430         return NT_STATUS_NOT_IMPLEMENTED;
431 }
432
433
434 /*
435   set info on a pathname
436 */
437 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs, 
438                                  struct ntvfs_request *req, union smb_setfileinfo *st)
439 {
440         return NT_STATUS_NOT_IMPLEMENTED;
441 }
442
443
444 /*
445   open a file
446 */
447 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs, 
448                           struct ntvfs_request *req, union smb_open *io)
449 {
450         return NT_STATUS_NOT_IMPLEMENTED;
451 }
452
453 /*
454   create a directory
455 */
456 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs, 
457                            struct ntvfs_request *req, union smb_mkdir *md)
458 {
459         struct cvfs_private *private = ntvfs->private_data;
460         struct composite_context *c_req;
461
462         CHECK_ASYNC(req);
463
464         c_req = smb2_composite_mkdir_send(private->tree, md);
465
466         SIMPLE_COMPOSITE_TAIL;
467 }
468
469 /*
470   remove a directory
471 */
472 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs, 
473                            struct ntvfs_request *req, struct smb_rmdir *rd)
474 {
475         struct cvfs_private *private = ntvfs->private_data;
476         struct composite_context *c_req;
477
478         CHECK_ASYNC(req);
479
480         c_req = smb2_composite_rmdir_send(private->tree, rd);
481
482         SIMPLE_COMPOSITE_TAIL;
483 }
484
485 /*
486   rename a set of files
487 */
488 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs, 
489                             struct ntvfs_request *req, union smb_rename *ren)
490 {
491         return NT_STATUS_NOT_IMPLEMENTED;
492 }
493
494 /*
495   copy a set of files
496 */
497 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs, 
498                           struct ntvfs_request *req, struct smb_copy *cp)
499 {
500         return NT_STATUS_NOT_SUPPORTED;
501 }
502
503 /*
504   read from a file
505 */
506 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs, 
507                           struct ntvfs_request *req, union smb_read *io)
508 {
509         return NT_STATUS_NOT_IMPLEMENTED;
510 }
511
512 /*
513   write to a file
514 */
515 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs, 
516                            struct ntvfs_request *req, union smb_write *io)
517 {
518         return NT_STATUS_NOT_IMPLEMENTED;
519 }
520
521 /*
522   seek in a file
523 */
524 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs, 
525                           struct ntvfs_request *req,
526                           union smb_seek *io)
527 {
528         return NT_STATUS_NOT_IMPLEMENTED;
529 }
530
531 /*
532   flush a file
533 */
534 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs, 
535                            struct ntvfs_request *req,
536                            union smb_flush *io)
537 {
538         return NT_STATUS_NOT_IMPLEMENTED;
539 }
540
541 /*
542   close a file
543 */
544 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs, 
545                            struct ntvfs_request *req, union smb_close *io)
546 {
547         return NT_STATUS_NOT_IMPLEMENTED;
548 }
549
550 /*
551   exit - closing files open by the pid
552 */
553 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs, 
554                           struct ntvfs_request *req)
555 {
556         return NT_STATUS_NOT_IMPLEMENTED;
557 }
558
559 /*
560   logoff - closing files open by the user
561 */
562 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs, 
563                             struct ntvfs_request *req)
564 {
565         /* we can't do this right in the cifs backend .... */
566         return NT_STATUS_OK;
567 }
568
569 /*
570   setup for an async call - nothing to do yet
571 */
572 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs, 
573                                  struct ntvfs_request *req, 
574                                  void *private)
575 {
576         return NT_STATUS_OK;
577 }
578
579 /*
580   cancel an async call
581 */
582 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs, 
583                             struct ntvfs_request *req)
584 {
585         return NT_STATUS_NOT_IMPLEMENTED;
586 }
587
588 /*
589   lock a byte range
590 */
591 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs, 
592                           struct ntvfs_request *req, union smb_lock *io)
593 {
594         return NT_STATUS_NOT_IMPLEMENTED;
595 }
596
597 /*
598   set info on a open file
599 */
600 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs, 
601                                  struct ntvfs_request *req, 
602                                  union smb_setfileinfo *io)
603 {
604         return NT_STATUS_NOT_IMPLEMENTED;
605 }
606
607
608 /*
609   a handler for async fsinfo replies
610  */
611 static void async_fsinfo(struct smb2_request *c_req)
612 {
613         struct async_info *async = c_req->async.private_data;
614         struct ntvfs_request *req = async->req;
615         req->async_states->status = smb2_getinfo_fs_recv(c_req, req, async->parms);
616         talloc_free(async);
617         req->async_states->send_fn(req);
618 }
619
620 /*
621   return filesystem space info
622 */
623 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs, 
624                             struct ntvfs_request *req, union smb_fsinfo *fs)
625 {
626         struct cvfs_private *private = ntvfs->private_data;
627         struct smb2_request *c_req;
628         enum smb_fsinfo_level level = fs->generic.level;
629
630         CHECK_ASYNC(req);
631
632         switch (level) {
633                 /* some levels go straight through */
634         case RAW_QFS_VOLUME_INFORMATION:
635         case RAW_QFS_SIZE_INFORMATION:
636         case RAW_QFS_DEVICE_INFORMATION:
637         case RAW_QFS_ATTRIBUTE_INFORMATION:
638         case RAW_QFS_QUOTA_INFORMATION:
639         case RAW_QFS_FULL_SIZE_INFORMATION:
640         case RAW_QFS_OBJECTID_INFORMATION:
641                 break;
642
643                 /* some get mapped */
644         case RAW_QFS_VOLUME_INFO:
645                 level = RAW_QFS_VOLUME_INFORMATION;
646                 break;
647         case RAW_QFS_SIZE_INFO:
648                 level = RAW_QFS_SIZE_INFORMATION;
649                 break;
650         case RAW_QFS_DEVICE_INFO:
651                 level = RAW_QFS_DEVICE_INFORMATION;
652                 break;
653         case RAW_QFS_ATTRIBUTE_INFO:
654                 level = RAW_QFS_ATTRIBUTE_INFO;
655                 break;
656
657         default:
658                 /* the rest get refused for now */
659                 DEBUG(0,("fsinfo level %u not possible on SMB2\n",
660                          (unsigned)fs->generic.level));
661                 break;
662         }
663
664         fs->generic.level = level;
665         fs->generic.handle = private->roothandle;
666
667         c_req = smb2_getinfo_fs_send(private->tree, fs);
668
669         ASYNC_RECV_TAIL(fs, async_fsinfo);
670 }
671
672 /*
673   return print queue info
674 */
675 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs, 
676                          struct ntvfs_request *req, union smb_lpq *lpq)
677 {
678         return NT_STATUS_NOT_SUPPORTED;
679 }
680
681 /* 
682    list files in a directory matching a wildcard pattern
683 */
684 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs, 
685                                   struct ntvfs_request *req, union smb_search_first *io, 
686                                   void *search_private, 
687                                   bool (*callback)(void *, const union smb_search_data *))
688 {
689         struct cvfs_private *private = ntvfs->private_data;
690         struct smb2_find f;
691         enum smb_search_data_level smb2_level;
692         uint_t count, i;
693         union smb_search_data *data;
694         NTSTATUS status;
695
696         if (io->generic.level != RAW_SEARCH_TRANS2) {
697                 DEBUG(0,("We only support trans2 search in smb2 backend\n"));
698                 return NT_STATUS_NOT_SUPPORTED;
699         }
700
701         switch (io->generic.data_level) {
702         case RAW_SEARCH_DATA_DIRECTORY_INFO:
703                 smb2_level = SMB2_FIND_DIRECTORY_INFO;
704                 break;
705         case RAW_SEARCH_DATA_FULL_DIRECTORY_INFO:
706                 smb2_level = SMB2_FIND_FULL_DIRECTORY_INFO;
707                 break;
708         case RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO:
709                 smb2_level = SMB2_FIND_BOTH_DIRECTORY_INFO;
710                 break;
711         case RAW_SEARCH_DATA_NAME_INFO:
712                 smb2_level = SMB2_FIND_NAME_INFO;
713                 break;
714         case RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO:
715                 smb2_level = SMB2_FIND_ID_FULL_DIRECTORY_INFO;
716                 break;
717         case RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO:
718                 smb2_level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO;
719                 break;
720         default:
721                 DEBUG(0,("Unsupported search level %u for smb2 backend\n",
722                          (unsigned)io->generic.data_level));
723                 return NT_STATUS_INVALID_INFO_CLASS;
724         }
725
726         /* we do the search on the roothandle. This only works because
727            search is synchronous, otherwise we'd have no way to
728            distinguish multiple searches happening at once
729         */
730         ZERO_STRUCT(f);
731         f.in.file.handle        = private->roothandle;
732         f.in.level              = smb2_level;
733         f.in.pattern            = io->t2ffirst.in.pattern;
734         while (f.in.pattern[0] == '\\') {
735                 f.in.pattern++;
736         }
737         f.in.continue_flags     = 0;
738         f.in.max_response_size  = 0x10000;
739
740         status = smb2_find_level(private->tree, req, &f, &count, &data);
741         NT_STATUS_NOT_OK_RETURN(status);        
742
743         for (i=0;i<count;i++) {
744                 if (!callback(search_private, &data[i])) break;
745         }
746
747         io->t2ffirst.out.handle = 0;
748         io->t2ffirst.out.count = i;
749         /* TODO: fix end_of_file */
750         io->t2ffirst.out.end_of_search = 1;
751
752         talloc_free(data);
753         
754         return NT_STATUS_OK;
755 }
756
757 /* continue a search */
758 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs, 
759                                  struct ntvfs_request *req, union smb_search_next *io, 
760                                  void *search_private, 
761                                  bool (*callback)(void *, const union smb_search_data *))
762 {
763         return NT_STATUS_NOT_IMPLEMENTED;
764 }
765
766 /* close a search */
767 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs, 
768                                   struct ntvfs_request *req, union smb_search_close *io)
769 {
770         return NT_STATUS_NOT_IMPLEMENTED;
771 }
772
773 /* SMBtrans - not used on file shares */
774 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs, 
775                            struct ntvfs_request *req,
776                            struct smb_trans2 *trans2)
777 {
778         return NT_STATUS_ACCESS_DENIED;
779 }
780
781 /* change notify request - always async */
782 static NTSTATUS cvfs_notify(struct ntvfs_module_context *ntvfs, 
783                             struct ntvfs_request *req,
784                             union smb_notify *io)
785 {
786         return NT_STATUS_NOT_IMPLEMENTED;
787 }
788
789 /*
790   initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
791  */
792 NTSTATUS ntvfs_smb2_init(void)
793 {
794         NTSTATUS ret;
795         struct ntvfs_ops ops;
796         NTVFS_CURRENT_CRITICAL_SIZES(vers);
797
798         ZERO_STRUCT(ops);
799
800         /* fill in the name and type */
801         ops.name = "smb2";
802         ops.type = NTVFS_DISK;
803         
804         /* fill in all the operations */
805         ops.connect = cvfs_connect;
806         ops.disconnect = cvfs_disconnect;
807         ops.unlink = cvfs_unlink;
808         ops.chkpath = cvfs_chkpath;
809         ops.qpathinfo = cvfs_qpathinfo;
810         ops.setpathinfo = cvfs_setpathinfo;
811         ops.open = cvfs_open;
812         ops.mkdir = cvfs_mkdir;
813         ops.rmdir = cvfs_rmdir;
814         ops.rename = cvfs_rename;
815         ops.copy = cvfs_copy;
816         ops.ioctl = cvfs_ioctl;
817         ops.read = cvfs_read;
818         ops.write = cvfs_write;
819         ops.seek = cvfs_seek;
820         ops.flush = cvfs_flush; 
821         ops.close = cvfs_close;
822         ops.exit = cvfs_exit;
823         ops.lock = cvfs_lock;
824         ops.setfileinfo = cvfs_setfileinfo;
825         ops.qfileinfo = cvfs_qfileinfo;
826         ops.fsinfo = cvfs_fsinfo;
827         ops.lpq = cvfs_lpq;
828         ops.search_first = cvfs_search_first;
829         ops.search_next = cvfs_search_next;
830         ops.search_close = cvfs_search_close;
831         ops.trans = cvfs_trans;
832         ops.logoff = cvfs_logoff;
833         ops.async_setup = cvfs_async_setup;
834         ops.cancel = cvfs_cancel;
835         ops.notify = cvfs_notify;
836
837         /* register ourselves with the NTVFS subsystem. We register
838            under the name 'smb2'. */
839         ret = ntvfs_register(&ops, &vers);
840
841         if (!NT_STATUS_IS_OK(ret)) {
842                 DEBUG(0,("Failed to register SMB2 backend\n"));
843         }
844         
845         return ret;
846 }