r17930: Merge noinclude branch:
[jra/samba/.git] / source4 / ntvfs / posix / pvfs_search.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - directory search functions
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "vfs_posix.h"
25 #include "system/time.h"
26 #include "librpc/gen_ndr/security.h"
27 #include "smbd/service_stream.h"
28 #include "lib/events/events.h"
29 #include "lib/util/dlinklist.h"
30
31 /* place a reasonable limit on old-style searches as clients tend to
32    not send search close requests */
33 #define MAX_OLD_SEARCHES 2000
34 #define MAX_SEARCH_HANDLES (UINT16_MAX - 1)
35 #define INVALID_SEARCH_HANDLE UINT16_MAX
36
37 /*
38   destroy an open search
39 */
40 static int pvfs_search_destructor(struct pvfs_search_state *search)
41 {
42         DLIST_REMOVE(search->pvfs->search.list, search);
43         idr_remove(search->pvfs->search.idtree, search->handle);
44         return 0;
45 }
46
47 /*
48   called when a search timer goes off
49 */
50 static void pvfs_search_timer(struct event_context *ev, struct timed_event *te, 
51                                       struct timeval t, void *ptr)
52 {
53         struct pvfs_search_state *search = talloc_get_type(ptr, struct pvfs_search_state);
54         talloc_free(search);
55 }
56
57 /*
58   setup a timer to destroy a open search after a inactivity period
59 */
60 static void pvfs_search_setup_timer(struct pvfs_search_state *search)
61 {
62         struct event_context *ev = search->pvfs->ntvfs->ctx->event_ctx;
63         if (search->handle == INVALID_SEARCH_HANDLE) return;
64         talloc_free(search->te);
65         search->te = event_add_timed(ev, search, 
66                                      timeval_current_ofs(search->pvfs->search.inactivity_time, 0), 
67                                      pvfs_search_timer, search);
68 }
69
70 /*
71   fill in a single search result for a given info level
72 */
73 static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
74                                  enum smb_search_data_level level,
75                                  const char *unix_path,
76                                  const char *fname, 
77                                  struct pvfs_search_state *search,
78                                  uint32_t dir_index,
79                                  union smb_search_data *file)
80 {
81         struct pvfs_filename *name;
82         NTSTATUS status;
83         const char *shortname;
84
85         status = pvfs_resolve_partial(pvfs, file, unix_path, fname, &name);
86         if (!NT_STATUS_IS_OK(status)) {
87                 return status;
88         }
89
90         status = pvfs_match_attrib(pvfs, name, search->search_attrib, search->must_attrib);
91         if (!NT_STATUS_IS_OK(status)) {
92                 return status;
93         }
94
95         switch (level) {
96         case RAW_SEARCH_DATA_SEARCH:
97                 shortname = pvfs_short_name(pvfs, name, name);
98                 file->search.attrib           = name->dos.attrib;
99                 file->search.write_time       = nt_time_to_unix(name->dos.write_time);
100                 file->search.size             = name->st.st_size;
101                 file->search.name             = shortname;
102                 file->search.id.reserved      = search->handle >> 8;
103                 memset(file->search.id.name, ' ', sizeof(file->search.id.name));
104                 memcpy(file->search.id.name, shortname, 
105                        MIN(strlen(shortname)+1, sizeof(file->search.id.name)));
106                 file->search.id.handle        = search->handle & 0xFF;
107                 file->search.id.server_cookie = dir_index;
108                 file->search.id.client_cookie = 0;
109                 return NT_STATUS_OK;
110
111         case RAW_SEARCH_DATA_STANDARD:
112                 file->standard.resume_key   = dir_index;
113                 file->standard.create_time  = nt_time_to_unix(name->dos.create_time);
114                 file->standard.access_time  = nt_time_to_unix(name->dos.access_time);
115                 file->standard.write_time   = nt_time_to_unix(name->dos.write_time);
116                 file->standard.size         = name->st.st_size;
117                 file->standard.alloc_size   = name->dos.alloc_size;
118                 file->standard.attrib       = name->dos.attrib;
119                 file->standard.name.s       = fname;
120                 return NT_STATUS_OK;
121
122         case RAW_SEARCH_DATA_EA_SIZE:
123                 file->ea_size.resume_key   = dir_index;
124                 file->ea_size.create_time  = nt_time_to_unix(name->dos.create_time);
125                 file->ea_size.access_time  = nt_time_to_unix(name->dos.access_time);
126                 file->ea_size.write_time   = nt_time_to_unix(name->dos.write_time);
127                 file->ea_size.size         = name->st.st_size;
128                 file->ea_size.alloc_size   = name->dos.alloc_size;
129                 file->ea_size.attrib       = name->dos.attrib;
130                 file->ea_size.ea_size      = name->dos.ea_size;
131                 file->ea_size.name.s       = fname;
132                 return NT_STATUS_OK;
133
134         case RAW_SEARCH_DATA_EA_LIST:
135                 file->ea_list.resume_key   = dir_index;
136                 file->ea_list.create_time  = nt_time_to_unix(name->dos.create_time);
137                 file->ea_list.access_time  = nt_time_to_unix(name->dos.access_time);
138                 file->ea_list.write_time   = nt_time_to_unix(name->dos.write_time);
139                 file->ea_list.size         = name->st.st_size;
140                 file->ea_list.alloc_size   = name->dos.alloc_size;
141                 file->ea_list.attrib       = name->dos.attrib;
142                 file->ea_list.name.s       = fname;
143                 return pvfs_query_ea_list(pvfs, file, name, -1, 
144                                           search->num_ea_names,
145                                           search->ea_names,
146                                           &file->ea_list.eas);
147
148         case RAW_SEARCH_DATA_DIRECTORY_INFO:
149                 file->directory_info.file_index   = dir_index;
150                 file->directory_info.create_time  = name->dos.create_time;
151                 file->directory_info.access_time  = name->dos.access_time;
152                 file->directory_info.write_time   = name->dos.write_time;
153                 file->directory_info.change_time  = name->dos.change_time;
154                 file->directory_info.size         = name->st.st_size;
155                 file->directory_info.alloc_size   = name->dos.alloc_size;
156                 file->directory_info.attrib       = name->dos.attrib;
157                 file->directory_info.name.s       = fname;
158                 return NT_STATUS_OK;
159
160         case RAW_SEARCH_DATA_FULL_DIRECTORY_INFO:
161                 file->full_directory_info.file_index   = dir_index;
162                 file->full_directory_info.create_time  = name->dos.create_time;
163                 file->full_directory_info.access_time  = name->dos.access_time;
164                 file->full_directory_info.write_time   = name->dos.write_time;
165                 file->full_directory_info.change_time  = name->dos.change_time;
166                 file->full_directory_info.size         = name->st.st_size;
167                 file->full_directory_info.alloc_size   = name->dos.alloc_size;
168                 file->full_directory_info.attrib       = name->dos.attrib;
169                 file->full_directory_info.ea_size      = name->dos.ea_size;
170                 file->full_directory_info.name.s       = fname;
171                 return NT_STATUS_OK;
172
173         case RAW_SEARCH_DATA_NAME_INFO:
174                 file->name_info.file_index   = dir_index;
175                 file->name_info.name.s       = fname;
176                 return NT_STATUS_OK;
177
178         case RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO:
179                 file->both_directory_info.file_index   = dir_index;
180                 file->both_directory_info.create_time  = name->dos.create_time;
181                 file->both_directory_info.access_time  = name->dos.access_time;
182                 file->both_directory_info.write_time   = name->dos.write_time;
183                 file->both_directory_info.change_time  = name->dos.change_time;
184                 file->both_directory_info.size         = name->st.st_size;
185                 file->both_directory_info.alloc_size   = name->dos.alloc_size;
186                 file->both_directory_info.attrib       = name->dos.attrib;
187                 file->both_directory_info.ea_size      = name->dos.ea_size;
188                 file->both_directory_info.short_name.s = pvfs_short_name(pvfs, file, name);
189                 file->both_directory_info.name.s       = fname;
190                 return NT_STATUS_OK;
191
192         case RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO:
193                 file->id_full_directory_info.file_index   = dir_index;
194                 file->id_full_directory_info.create_time  = name->dos.create_time;
195                 file->id_full_directory_info.access_time  = name->dos.access_time;
196                 file->id_full_directory_info.write_time   = name->dos.write_time;
197                 file->id_full_directory_info.change_time  = name->dos.change_time;
198                 file->id_full_directory_info.size         = name->st.st_size;
199                 file->id_full_directory_info.alloc_size   = name->dos.alloc_size;
200                 file->id_full_directory_info.attrib       = name->dos.attrib;
201                 file->id_full_directory_info.ea_size      = name->dos.ea_size;
202                 file->id_full_directory_info.file_id      = name->dos.file_id;
203                 file->id_full_directory_info.name.s       = fname;
204                 return NT_STATUS_OK;
205
206         case RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO:
207                 file->id_both_directory_info.file_index   = dir_index;
208                 file->id_both_directory_info.create_time  = name->dos.create_time;
209                 file->id_both_directory_info.access_time  = name->dos.access_time;
210                 file->id_both_directory_info.write_time   = name->dos.write_time;
211                 file->id_both_directory_info.change_time  = name->dos.change_time;
212                 file->id_both_directory_info.size         = name->st.st_size;
213                 file->id_both_directory_info.alloc_size   = name->dos.alloc_size;
214                 file->id_both_directory_info.attrib       = name->dos.attrib;
215                 file->id_both_directory_info.ea_size      = name->dos.ea_size;
216                 file->id_both_directory_info.file_id      = name->dos.file_id;
217                 file->id_both_directory_info.short_name.s = pvfs_short_name(pvfs, file, name);
218                 file->id_both_directory_info.name.s       = fname;
219                 return NT_STATUS_OK;
220
221         case RAW_SEARCH_DATA_GENERIC:
222                 break;
223         }
224
225         return NT_STATUS_INVALID_LEVEL;
226 }
227
228
229 /*
230   the search fill loop
231 */
232 static NTSTATUS pvfs_search_fill(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
233                                  uint_t max_count, 
234                                  struct pvfs_search_state *search,
235                                  enum smb_search_data_level level,
236                                  uint_t *reply_count,
237                                  void *search_private, 
238                                  BOOL (*callback)(void *, union smb_search_data *))
239 {
240         struct pvfs_dir *dir = search->dir;
241         NTSTATUS status;
242
243         *reply_count = 0;
244
245         if (max_count == 0) {
246                 max_count = 1;
247         }
248
249         while ((*reply_count) < max_count) {
250                 union smb_search_data *file;
251                 const char *name;
252                 uint_t ofs = search->current_index;
253
254                 name = pvfs_list_next(dir, &search->current_index);
255                 if (name == NULL) break;
256
257                 file = talloc(mem_ctx, union smb_search_data);
258                 if (!file) {
259                         return NT_STATUS_NO_MEMORY;
260                 }
261
262                 status = fill_search_info(pvfs, level, 
263                                           pvfs_list_unix_path(dir), name, 
264                                           search, search->current_index, file);
265                 if (!NT_STATUS_IS_OK(status)) {
266                         talloc_free(file);
267                         continue;
268                 }
269
270                 if (!callback(search_private, file)) {
271                         talloc_free(file);
272                         search->current_index = ofs;
273                         break;
274                 }
275
276                 (*reply_count)++;
277                 talloc_free(file);
278         }
279
280         pvfs_search_setup_timer(search);
281
282         return NT_STATUS_OK;
283 }
284
285 /*
286   we've run out of search handles - cleanup those that the client forgot
287   to close
288 */
289 static void pvfs_search_cleanup(struct pvfs_state *pvfs)
290 {
291         int i;
292         time_t t = time(NULL);
293
294         for (i=0;i<MAX_OLD_SEARCHES;i++) {
295                 struct pvfs_search_state *search = idr_find(pvfs->search.idtree, i);
296                 if (search == NULL) return;
297                 if (pvfs_list_eos(search->dir, search->current_index) &&
298                     search->last_used != 0 &&
299                     t > search->last_used + 30) {
300                         /* its almost certainly been forgotten
301                          about */
302                         talloc_free(search);
303                 }
304         }
305 }
306
307
308 /* 
309    list files in a directory matching a wildcard pattern - old SMBsearch interface
310 */
311 static NTSTATUS pvfs_search_first_old(struct ntvfs_module_context *ntvfs,
312                                       struct ntvfs_request *req, union smb_search_first *io, 
313                                       void *search_private, 
314                                       BOOL (*callback)(void *, union smb_search_data *))
315 {
316         struct pvfs_dir *dir;
317         struct pvfs_state *pvfs = ntvfs->private_data;
318         struct pvfs_search_state *search;
319         uint_t reply_count;
320         uint16_t search_attrib;
321         const char *pattern;
322         NTSTATUS status;
323         struct pvfs_filename *name;
324         int id;
325
326         search_attrib = io->search_first.in.search_attrib;
327         pattern       = io->search_first.in.pattern;
328
329         /* resolve the cifs name to a posix name */
330         status = pvfs_resolve_name(pvfs, req, pattern, PVFS_RESOLVE_WILDCARD, &name);
331         if (!NT_STATUS_IS_OK(status)) {
332                 return status;
333         }
334
335         if (!name->has_wildcard && !name->exists) {
336                 return STATUS_NO_MORE_FILES;
337         }
338
339         status = pvfs_access_check_parent(pvfs, req, name, SEC_DIR_TRAVERSE | SEC_DIR_LIST);
340         if (!NT_STATUS_IS_OK(status)) {
341                 return status;
342         }
343
344         /* we initially make search a child of the request, then if we
345            need to keep it long term we steal it for the private
346            structure */
347         search = talloc(req, struct pvfs_search_state);
348         if (!search) {
349                 return NT_STATUS_NO_MEMORY;
350         }
351
352         /* do the actual directory listing */
353         status = pvfs_list_start(pvfs, name, search, &dir);
354         if (!NT_STATUS_IS_OK(status)) {
355                 return status;
356         }
357
358         /* we need to give a handle back to the client so it
359            can continue a search */
360         id = idr_get_new(pvfs->search.idtree, search, MAX_OLD_SEARCHES);
361         if (id == -1) {
362                 pvfs_search_cleanup(pvfs);
363                 id = idr_get_new(pvfs->search.idtree, search, MAX_OLD_SEARCHES);
364         }
365         if (id == -1) {
366                 return NT_STATUS_INSUFFICIENT_RESOURCES;
367         }
368
369         search->pvfs = pvfs;
370         search->handle = id;
371         search->dir = dir;
372         search->current_index = 0;
373         search->search_attrib = search_attrib & 0xFF;
374         search->must_attrib = (search_attrib>>8) & 0xFF;
375         search->last_used = time(NULL);
376         search->te = NULL;
377
378         DLIST_ADD(pvfs->search.list, search);
379
380         talloc_set_destructor(search, pvfs_search_destructor);
381
382         status = pvfs_search_fill(pvfs, req, io->search_first.in.max_count, search, io->generic.data_level,
383                                   &reply_count, search_private, callback);
384         if (!NT_STATUS_IS_OK(status)) {
385                 return status;
386         }
387
388         io->search_first.out.count = reply_count;
389
390         /* not matching any entries is an error */
391         if (reply_count == 0) {
392                 return STATUS_NO_MORE_FILES;
393         }
394
395         talloc_steal(pvfs, search);
396
397         return NT_STATUS_OK;
398 }
399
400 /* continue a old style search */
401 static NTSTATUS pvfs_search_next_old(struct ntvfs_module_context *ntvfs,
402                                      struct ntvfs_request *req, union smb_search_next *io, 
403                                      void *search_private, 
404                                      BOOL (*callback)(void *, union smb_search_data *))
405 {
406         struct pvfs_state *pvfs = ntvfs->private_data;
407         struct pvfs_search_state *search;
408         struct pvfs_dir *dir;
409         uint_t reply_count, max_count;
410         uint16_t handle;
411         NTSTATUS status;
412
413         handle    = io->search_next.in.id.handle | (io->search_next.in.id.reserved<<8);
414         max_count = io->search_next.in.max_count;
415
416         search = idr_find(pvfs->search.idtree, handle);
417         if (search == NULL) {
418                 /* we didn't find the search handle */
419                 return NT_STATUS_INVALID_HANDLE;
420         }
421
422         search->current_index = io->search_next.in.id.server_cookie;
423         search->last_used = time(NULL);
424         dir = search->dir;
425
426         status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.data_level,
427                                   &reply_count, search_private, callback);
428         if (!NT_STATUS_IS_OK(status)) {
429                 return status;
430         }
431
432         io->search_next.out.count = reply_count;
433
434         /* not matching any entries means end of search */
435         if (reply_count == 0) {
436                 talloc_free(search);
437         }
438
439         return NT_STATUS_OK;
440 }
441
442 /* 
443    list files in a directory matching a wildcard pattern
444 */
445 static NTSTATUS pvfs_search_first_trans2(struct ntvfs_module_context *ntvfs,
446                                          struct ntvfs_request *req, union smb_search_first *io, 
447                                          void *search_private, 
448                                          BOOL (*callback)(void *, union smb_search_data *))
449 {
450         struct pvfs_dir *dir;
451         struct pvfs_state *pvfs = ntvfs->private_data;
452         struct pvfs_search_state *search;
453         uint_t reply_count;
454         uint16_t search_attrib, max_count;
455         const char *pattern;
456         NTSTATUS status;
457         struct pvfs_filename *name;
458         int id;
459
460         search_attrib = io->t2ffirst.in.search_attrib;
461         pattern       = io->t2ffirst.in.pattern;
462         max_count     = io->t2ffirst.in.max_count;
463
464         /* resolve the cifs name to a posix name */
465         status = pvfs_resolve_name(pvfs, req, pattern, PVFS_RESOLVE_WILDCARD, &name);
466         if (!NT_STATUS_IS_OK(status)) {
467                 return status;
468         }
469
470         if (!name->has_wildcard && !name->exists) {
471                 return NT_STATUS_NO_SUCH_FILE;
472         }
473
474         status = pvfs_access_check_parent(pvfs, req, name, SEC_DIR_TRAVERSE | SEC_DIR_LIST);
475         if (!NT_STATUS_IS_OK(status)) {
476                 return status;
477         }
478
479         /* we initially make search a child of the request, then if we
480            need to keep it long term we steal it for the private
481            structure */
482         search = talloc(req, struct pvfs_search_state);
483         if (!search) {
484                 return NT_STATUS_NO_MEMORY;
485         }
486
487         /* do the actual directory listing */
488         status = pvfs_list_start(pvfs, name, search, &dir);
489         if (!NT_STATUS_IS_OK(status)) {
490                 return status;
491         }
492
493         id = idr_get_new(pvfs->search.idtree, search, MAX_SEARCH_HANDLES);
494         if (id == -1) {
495                 return NT_STATUS_INSUFFICIENT_RESOURCES;
496         }
497
498         search->pvfs = pvfs;
499         search->handle = id;
500         search->dir = dir;
501         search->current_index = 0;
502         search->search_attrib = search_attrib;
503         search->must_attrib = 0;
504         search->last_used = 0;
505         search->num_ea_names = io->t2ffirst.in.num_names;
506         search->ea_names = io->t2ffirst.in.ea_names;
507         search->te = NULL;
508
509         DLIST_ADD(pvfs->search.list, search);
510         talloc_set_destructor(search, pvfs_search_destructor);
511
512         status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.data_level,
513                                   &reply_count, search_private, callback);
514         if (!NT_STATUS_IS_OK(status)) {
515                 return status;
516         }
517
518         /* not matching any entries is an error */
519         if (reply_count == 0) {
520                 return NT_STATUS_NO_SUCH_FILE;
521         }
522
523         io->t2ffirst.out.count = reply_count;
524         io->t2ffirst.out.handle = search->handle;
525         io->t2ffirst.out.end_of_search = pvfs_list_eos(dir, search->current_index) ? 1 : 0;
526
527         /* work out if we are going to keep the search state
528            and allow for a search continue */
529         if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
530             ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && 
531              io->t2ffirst.out.end_of_search)) {
532                 talloc_free(search);
533         } else {
534                 talloc_steal(pvfs, search);
535         }
536
537         return NT_STATUS_OK;
538 }
539
540 /* continue a search */
541 static NTSTATUS pvfs_search_next_trans2(struct ntvfs_module_context *ntvfs,
542                                         struct ntvfs_request *req, union smb_search_next *io, 
543                                         void *search_private, 
544                                         BOOL (*callback)(void *, union smb_search_data *))
545 {
546         struct pvfs_state *pvfs = ntvfs->private_data;
547         struct pvfs_search_state *search;
548         struct pvfs_dir *dir;
549         uint_t reply_count;
550         uint16_t handle;
551         NTSTATUS status;
552
553         handle = io->t2fnext.in.handle;
554
555         search = idr_find(pvfs->search.idtree, handle);
556         if (search == NULL) {
557                 /* we didn't find the search handle */
558                 return NT_STATUS_INVALID_HANDLE;
559         }
560
561         dir = search->dir;
562
563         /* work out what type of continuation is being used */
564         if (io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
565                 status = pvfs_list_seek(dir, io->t2fnext.in.last_name, &search->current_index);
566                 if (!NT_STATUS_IS_OK(status)) {
567                         if (io->t2fnext.in.resume_key) {
568                                 search->current_index = io->t2fnext.in.resume_key;
569                         } else {
570                                 return status;
571                         }
572                 }
573         } else if (io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) {
574                 /* plain continue - nothing to do */
575         } else {
576                 search->current_index = io->t2fnext.in.resume_key;
577         }
578
579         search->num_ea_names = io->t2fnext.in.num_names;
580         search->ea_names = io->t2fnext.in.ea_names;
581
582         status = pvfs_search_fill(pvfs, req, io->t2fnext.in.max_count, search, io->generic.data_level,
583                                   &reply_count, search_private, callback);
584         if (!NT_STATUS_IS_OK(status)) {
585                 return status;
586         }
587
588         io->t2fnext.out.count = reply_count;
589         io->t2fnext.out.end_of_search = pvfs_list_eos(dir, search->current_index) ? 1 : 0;
590
591         /* work out if we are going to keep the search state */
592         if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
593             ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && 
594              io->t2fnext.out.end_of_search)) {
595                 talloc_free(search);
596         }
597
598         return NT_STATUS_OK;
599 }
600
601 static NTSTATUS pvfs_search_first_smb2(struct ntvfs_module_context *ntvfs,
602                                        struct ntvfs_request *req, const struct smb2_find *io, 
603                                        void *search_private, 
604                                        BOOL (*callback)(void *, union smb_search_data *))
605 {
606         struct pvfs_dir *dir;
607         struct pvfs_state *pvfs = ntvfs->private_data;
608         struct pvfs_search_state *search;
609         uint_t reply_count;
610         uint16_t max_count;
611         const char *pattern;
612         NTSTATUS status;
613         struct pvfs_filename *name;
614         struct pvfs_file *f;
615
616         f = pvfs_find_fd(pvfs, req, io->in.file.ntvfs);
617         if (!f) {
618                 return NT_STATUS_FILE_CLOSED;
619         }
620
621         /* its only valid for directories */
622         if (f->handle->fd != -1) {
623                 return NT_STATUS_INVALID_PARAMETER;
624         }
625
626         if (!(f->access_mask & SEC_DIR_LIST)) {
627                 return NT_STATUS_ACCESS_DENIED;
628         }
629
630         if (f->search) {
631                 talloc_free(f->search);
632                 f->search = NULL;
633         }
634
635         if (strequal(io->in.pattern, "")) {
636                 return NT_STATUS_OBJECT_NAME_INVALID;
637         }
638         if (strchr_m(io->in.pattern, '\\')) {
639                 return NT_STATUS_OBJECT_NAME_INVALID;
640         }
641         if (strchr_m(io->in.pattern, '/')) {
642                 return NT_STATUS_OBJECT_NAME_INVALID;
643         }
644
645         if (strequal("", f->handle->name->original_name)) {
646                 pattern = talloc_asprintf(req, "\\%s", io->in.pattern);
647                 NT_STATUS_HAVE_NO_MEMORY(pattern);
648         } else {
649                 pattern = talloc_asprintf(req, "\\%s\\%s",
650                                           f->handle->name->original_name,
651                                           io->in.pattern);
652                 NT_STATUS_HAVE_NO_MEMORY(pattern);
653         }
654
655         /* resolve the cifs name to a posix name */
656         status = pvfs_resolve_name(pvfs, req, pattern, PVFS_RESOLVE_WILDCARD, &name);
657         NT_STATUS_NOT_OK_RETURN(status);
658
659         if (!name->has_wildcard && !name->exists) {
660                 return NT_STATUS_NO_SUCH_FILE;
661         }
662
663         /* we initially make search a child of the request, then if we
664            need to keep it long term we steal it for the private
665            structure */
666         search = talloc(req, struct pvfs_search_state);
667         NT_STATUS_HAVE_NO_MEMORY(search);
668
669         /* do the actual directory listing */
670         status = pvfs_list_start(pvfs, name, search, &dir);
671         NT_STATUS_NOT_OK_RETURN(status);
672
673         search->pvfs            = pvfs;
674         search->handle          = INVALID_SEARCH_HANDLE;
675         search->dir             = dir;
676         search->current_index   = 0;
677         search->search_attrib   = 0;
678         search->must_attrib     = 0;
679         search->last_used       = 0;
680         search->num_ea_names    = 0;
681         search->ea_names        = NULL;
682         search->te              = NULL;
683
684         if (io->in.continue_flags & SMB2_CONTINUE_FLAG_SINGLE) {
685                 max_count = 1;
686         } else {
687                 max_count = UINT16_MAX;
688         }
689
690         status = pvfs_search_fill(pvfs, req, max_count, search, io->data_level,
691                                   &reply_count, search_private, callback);
692         NT_STATUS_NOT_OK_RETURN(status);
693
694         /* not matching any entries is an error */
695         if (reply_count == 0) {
696                 return NT_STATUS_NO_SUCH_FILE;
697         }
698
699         f->search = talloc_steal(f, search);
700
701         return NT_STATUS_OK;
702 }
703
704 static NTSTATUS pvfs_search_next_smb2(struct ntvfs_module_context *ntvfs,
705                                       struct ntvfs_request *req, const struct smb2_find *io, 
706                                       void *search_private, 
707                                       BOOL (*callback)(void *, union smb_search_data *))
708 {
709         struct pvfs_state *pvfs = ntvfs->private_data;
710         struct pvfs_search_state *search;
711         uint_t reply_count;
712         uint16_t max_count;
713         NTSTATUS status;
714         struct pvfs_file *f;
715
716         f = pvfs_find_fd(pvfs, req, io->in.file.ntvfs);
717         if (!f) {
718                 return NT_STATUS_FILE_CLOSED;
719         }
720
721         /* its only valid for directories */
722         if (f->handle->fd != -1) {
723                 return NT_STATUS_INVALID_PARAMETER;
724         }
725
726         /* if there's no search started on the dir handle, it's like a search_first */
727         search = f->search;
728         if (!search) {
729                 return pvfs_search_first_smb2(ntvfs, req, io, search_private, callback);
730         }
731
732         if (io->in.continue_flags & SMB2_CONTINUE_FLAG_RESTART) {
733                 search->current_index = 0;
734         }
735
736         if (io->in.continue_flags & SMB2_CONTINUE_FLAG_SINGLE) {
737                 max_count = 1;
738         } else {
739                 max_count = UINT16_MAX;
740         }
741
742         status = pvfs_search_fill(pvfs, req, max_count, search, io->data_level,
743                                   &reply_count, search_private, callback);
744         NT_STATUS_NOT_OK_RETURN(status);
745
746         /* not matching any entries is an error */
747         if (reply_count == 0) {
748                 return STATUS_NO_MORE_FILES;
749         }
750
751         return NT_STATUS_OK;
752 }
753
754 /* 
755    list files in a directory matching a wildcard pattern
756 */
757 NTSTATUS pvfs_search_first(struct ntvfs_module_context *ntvfs,
758                            struct ntvfs_request *req, union smb_search_first *io, 
759                            void *search_private, 
760                            BOOL (*callback)(void *, union smb_search_data *))
761 {
762         switch (io->generic.level) {
763         case RAW_SEARCH_SEARCH:
764         case RAW_SEARCH_FFIRST:
765         case RAW_SEARCH_FUNIQUE:
766                 return pvfs_search_first_old(ntvfs, req, io, search_private, callback);
767
768         case RAW_SEARCH_TRANS2:
769                 return pvfs_search_first_trans2(ntvfs, req, io, search_private, callback);
770
771         case RAW_SEARCH_SMB2:
772                 return pvfs_search_first_smb2(ntvfs, req, &io->smb2, search_private, callback);
773         }
774
775         return NT_STATUS_INVALID_LEVEL;
776 }
777
778 /* continue a search */
779 NTSTATUS pvfs_search_next(struct ntvfs_module_context *ntvfs,
780                           struct ntvfs_request *req, union smb_search_next *io, 
781                           void *search_private, 
782                           BOOL (*callback)(void *, union smb_search_data *))
783 {
784         switch (io->generic.level) {
785         case RAW_SEARCH_SEARCH:
786         case RAW_SEARCH_FFIRST:
787                 return pvfs_search_next_old(ntvfs, req, io, search_private, callback);
788
789         case RAW_SEARCH_FUNIQUE:
790                 return NT_STATUS_INVALID_LEVEL;
791
792         case RAW_SEARCH_TRANS2:
793                 return pvfs_search_next_trans2(ntvfs, req, io, search_private, callback);
794
795         case RAW_SEARCH_SMB2:
796                 return pvfs_search_next_smb2(ntvfs, req, &io->smb2, search_private, callback);
797         }
798
799         return NT_STATUS_INVALID_LEVEL;
800 }
801
802
803 /* close a search */
804 NTSTATUS pvfs_search_close(struct ntvfs_module_context *ntvfs,
805                            struct ntvfs_request *req, union smb_search_close *io)
806 {
807         struct pvfs_state *pvfs = ntvfs->private_data;
808         struct pvfs_search_state *search;
809         uint16_t handle = INVALID_SEARCH_HANDLE;
810
811         switch (io->generic.level) {
812         case RAW_FINDCLOSE_GENERIC:
813                 return NT_STATUS_INVALID_LEVEL;
814
815         case RAW_FINDCLOSE_FCLOSE:
816                 handle = io->fclose.in.id.handle;
817                 break;
818
819         case RAW_FINDCLOSE_FINDCLOSE:
820                 handle = io->findclose.in.handle;
821                 break;
822         }
823
824         search = idr_find(pvfs->search.idtree, handle);
825         if (search == NULL) {
826                 /* we didn't find the search handle */
827                 return NT_STATUS_INVALID_HANDLE;
828         }
829
830         talloc_free(search);
831
832         return NT_STATUS_OK;
833 }
834