r3463: separated out some more headers (asn_1.h, messages.h, dlinklist.h and ioctl.h)
[bbaumbach/samba-autobuild/.git] / source4 / ntvfs / simple / vfs_simple.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    simple NTVFS filesystem backend
5
6    Copyright (C) Andrew Tridgell 2003
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   this implements a very simple NTVFS filesystem backend. 
24   
25   this backend largely ignores the POSIX -> CIFS mappings, just doing absolutely
26   minimal work to give a working backend.
27 */
28
29 #include "includes.h"
30 #include "svfs.h"
31 #include "system/time.h"
32 #include "dlinklist.h"
33
34 #ifndef O_DIRECTORY
35 #define O_DIRECTORY 0
36 #endif
37
38 #define CHECK_READ_ONLY(req) do { if (lp_readonly(req->tcon->service)) return NT_STATUS_ACCESS_DENIED; } while (0)
39
40 #ifndef HAVE_PREAD
41 static ssize_t pread(int __fd, void *__buf, size_t __nbytes, off_t __offset)
42 {
43         if (lseek(__fd, __offset, SEEK_SET) != __offset) {
44                 return -1;
45         }
46         return read(__fd, __buf, __nbytes);
47 }
48 #endif
49
50 #ifndef HAVE_PWRITE
51 static ssize_t pwrite(int __fd, const void *__buf, size_t __nbytes, off_t __offset)
52 {
53         if (lseek(__fd, __offset, SEEK_SET) != __offset) {
54                 return -1;
55         }
56         return write(__fd, __buf, __nbytes);
57 }
58 #endif
59
60 /*
61   connect to a share - used when a tree_connect operation comes
62   in. For a disk based backend we needs to ensure that the base
63   directory exists (tho it doesn't need to be accessible by the user,
64   that comes later)
65 */
66 static NTSTATUS svfs_connect(struct ntvfs_module_context *ntvfs,
67                              struct smbsrv_request *req, const char *sharename)
68 {
69         struct stat st;
70         struct smbsrv_tcon *tcon = req->tcon;
71         struct svfs_private *private;
72
73         private = talloc_p(tcon, struct svfs_private);
74
75         private->next_search_handle = 0;
76         private->connectpath = talloc_strdup(tcon, lp_pathname(tcon->service));
77         private->open_files = NULL;
78         private->search = NULL;
79
80         /* the directory must exist */
81         if (stat(private->connectpath, &st) != 0 || !S_ISDIR(st.st_mode)) {
82                 DEBUG(0,("'%s' is not a directory, when connecting to [%s]\n", 
83                          private->connectpath, sharename));
84                 return NT_STATUS_BAD_NETWORK_NAME;
85         }
86
87         tcon->fs_type = talloc_strdup(tcon, "NTFS");
88         tcon->dev_type = talloc_strdup(tcon, "A:");
89
90         ntvfs->private_data = private;
91
92         DEBUG(0,("WARNING: ntvfs simple: connect to share [%s] with ROOT privileges!!!\n",sharename));
93
94         return NT_STATUS_OK;
95 }
96
97 /*
98   disconnect from a share
99 */
100 static NTSTATUS svfs_disconnect(struct ntvfs_module_context *ntvfs,
101                                 struct smbsrv_tcon *tcon)
102 {
103         return NT_STATUS_OK;
104 }
105
106 /*
107   find open file handle given fd
108 */
109 static struct svfs_file *find_fd(struct svfs_private *private, int fd)
110 {
111         struct svfs_file *f;
112         for (f=private->open_files;f;f=f->next) {
113                 if (f->fd == fd) {
114                         return f;
115                 }
116         }
117         return NULL;
118 }
119
120 /*
121   delete a file - the dirtype specifies the file types to include in the search. 
122   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
123 */
124 static NTSTATUS svfs_unlink(struct ntvfs_module_context *ntvfs,
125                             struct smbsrv_request *req, struct smb_unlink *unl)
126 {
127         char *unix_path;
128
129         CHECK_READ_ONLY(req);
130
131         unix_path = svfs_unix_path(ntvfs, req, unl->in.pattern);
132
133         /* ignoring wildcards ... */
134         if (unlink(unix_path) == -1) {
135                 return map_nt_error_from_unix(errno);
136         }
137
138         return NT_STATUS_OK;
139 }
140
141
142 /*
143   ioctl interface - we don't do any
144 */
145 static NTSTATUS svfs_ioctl(struct ntvfs_module_context *ntvfs,
146                            struct smbsrv_request *req, union smb_ioctl *io)
147 {
148         return NT_STATUS_INVALID_PARAMETER;
149 }
150
151 /*
152   check if a directory exists
153 */
154 static NTSTATUS svfs_chkpath(struct ntvfs_module_context *ntvfs,
155                              struct smbsrv_request *req, struct smb_chkpath *cp)
156 {
157         char *unix_path;
158         struct stat st;
159
160         unix_path = svfs_unix_path(ntvfs, req, cp->in.path);
161
162         if (stat(unix_path, &st) == -1) {
163                 return map_nt_error_from_unix(errno);
164         }
165
166         if (!S_ISDIR(st.st_mode)) {
167                 return NT_STATUS_NOT_A_DIRECTORY;
168         }
169
170         return NT_STATUS_OK;
171 }
172
173 /*
174   approximately map a struct stat to a generic fileinfo struct
175 */
176 static NTSTATUS svfs_map_fileinfo(struct ntvfs_module_context *ntvfs,
177                                   struct smbsrv_request *req, union smb_fileinfo *info, 
178                                   struct stat *st, const char *unix_path)
179 {
180         struct svfs_dir *dir = NULL;
181         char *pattern = NULL;
182         int i;
183         const char *s, *short_name;
184
185         s = strrchr(unix_path, '/');
186         if (s) {
187                 short_name = s+1;
188         } else {
189                 short_name = "";
190         }
191
192         asprintf(&pattern, "%s:*", unix_path);
193         
194         if (pattern) {
195                 dir = svfs_list_unix(req, req, pattern);
196         }
197
198         unix_to_nt_time(&info->generic.out.create_time, st->st_ctime);
199         unix_to_nt_time(&info->generic.out.access_time, st->st_atime);
200         unix_to_nt_time(&info->generic.out.write_time,  st->st_mtime);
201         unix_to_nt_time(&info->generic.out.change_time, st->st_mtime);
202         info->generic.out.alloc_size = st->st_size;
203         info->generic.out.size = st->st_size;
204         info->generic.out.attrib = svfs_unix_to_dos_attrib(st->st_mode);
205         info->generic.out.alloc_size = st->st_blksize * st->st_blocks;
206         info->generic.out.nlink = st->st_nlink;
207         info->generic.out.directory = S_ISDIR(st->st_mode) ? 1 : 0;
208         info->generic.out.file_id = svfs_file_id(st);
209         /* REWRITE: TODO stuff in here */
210         info->generic.out.delete_pending = 0;
211         info->generic.out.ea_size = 0;
212         info->generic.out.num_eas = 0;
213         info->generic.out.fname.s = talloc_strdup(req, short_name);
214         info->generic.out.alt_fname.s = talloc_strdup(req, short_name);
215         info->generic.out.compressed_size = 0;
216         info->generic.out.format = 0;
217         info->generic.out.unit_shift = 0;
218         info->generic.out.chunk_shift = 0;
219         info->generic.out.cluster_shift = 0;
220         
221         info->generic.out.access_flags = 0;
222         info->generic.out.position = 0;
223         info->generic.out.mode = 0;
224         info->generic.out.alignment_requirement = 0;
225         info->generic.out.reparse_tag = 0;
226         info->generic.out.num_streams = 0;
227         /* setup a single data stream */
228         info->generic.out.num_streams = 1 + (dir?dir->count:0);
229         info->generic.out.streams = talloc_array_p(req, 
230                                                    struct stream_struct,
231                                                    info->generic.out.num_streams);
232         if (!info->generic.out.streams) {
233                 return NT_STATUS_NO_MEMORY;
234         }
235         info->generic.out.streams[0].size = st->st_size;
236         info->generic.out.streams[0].alloc_size = st->st_size;
237         info->generic.out.streams[0].stream_name.s = talloc_strdup(req,"::$DATA");
238
239         for (i=0;dir && i<dir->count;i++) {
240                 s = strchr(dir->files[i].name, ':');
241                 info->generic.out.streams[1+i].size = dir->files[i].st.st_size;
242                 info->generic.out.streams[1+i].alloc_size = dir->files[i].st.st_size;
243                 info->generic.out.streams[1+i].stream_name.s = s?s:dir->files[i].name;
244         }
245
246         return NT_STATUS_OK;
247 }
248
249 /*
250   return info on a pathname
251 */
252 static NTSTATUS svfs_qpathinfo(struct ntvfs_module_context *ntvfs,
253                                struct smbsrv_request *req, union smb_fileinfo *info)
254 {
255         char *unix_path;
256         struct stat st;
257
258         DEBUG(19,("svfs_qpathinfo: file %s level 0x%x\n", info->generic.in.fname, info->generic.level));
259         if (info->generic.level != RAW_FILEINFO_GENERIC) {
260                 return ntvfs_map_qpathinfo(req, info, ntvfs);
261         }
262         
263         unix_path = svfs_unix_path(ntvfs, req, info->generic.in.fname);
264         DEBUG(19,("svfs_qpathinfo: file %s\n", unix_path));
265         if (stat(unix_path, &st) == -1) {
266                 DEBUG(19,("svfs_qpathinfo: file %s errno=%d\n", unix_path, errno));
267                 return map_nt_error_from_unix(errno);
268         }
269         DEBUG(19,("svfs_qpathinfo: file %s, stat done\n", unix_path));
270         return svfs_map_fileinfo(ntvfs, req, info, &st, unix_path);
271 }
272
273 /*
274   query info on a open file
275 */
276 static NTSTATUS svfs_qfileinfo(struct ntvfs_module_context *ntvfs,
277                                struct smbsrv_request *req, union smb_fileinfo *info)
278 {
279         struct svfs_private *private = ntvfs->private_data;
280         struct svfs_file *f;
281         struct stat st;
282
283         if (info->generic.level != RAW_FILEINFO_GENERIC) {
284                 return ntvfs_map_qfileinfo(req, info, ntvfs);
285         }
286
287         f = find_fd(private, info->generic.in.fnum);
288         if (!f) {
289                 return NT_STATUS_INVALID_HANDLE;
290         }
291         
292         if (fstat(info->generic.in.fnum, &st) == -1) {
293                 return map_nt_error_from_unix(errno);
294         }
295
296         return svfs_map_fileinfo(ntvfs, req,info, &st, f->name);
297 }
298
299
300 /*
301   open a file
302 */
303 static NTSTATUS svfs_open(struct ntvfs_module_context *ntvfs,
304                           struct smbsrv_request *req, union smb_open *io)
305 {
306         struct svfs_private *private = ntvfs->private_data;
307         char *unix_path;
308         struct stat st;
309         int fd, flags;
310         struct svfs_file *f;
311         int create_flags, rdwr_flags;
312         
313         if (io->generic.level != RAW_OPEN_GENERIC) {
314                 return ntvfs_map_open(req, io, ntvfs);
315         }
316
317         if (lp_readonly(req->tcon->service)) {
318                 create_flags = 0;
319                 rdwr_flags = O_RDONLY;
320         } else {
321                 create_flags = O_CREAT;
322                 rdwr_flags = O_RDWR;
323         }
324
325         unix_path = svfs_unix_path(ntvfs, req, io->ntcreatex.in.fname);
326
327         switch (io->generic.in.open_disposition) {
328         case NTCREATEX_DISP_SUPERSEDE:
329         case NTCREATEX_DISP_OVERWRITE_IF:
330                 flags = create_flags | O_TRUNC;
331                 break;
332         case NTCREATEX_DISP_OPEN:
333         case NTCREATEX_DISP_OVERWRITE:
334                 flags = 0;
335                 break;
336         case NTCREATEX_DISP_CREATE:
337                 flags = create_flags | O_EXCL;
338                 break;
339         case NTCREATEX_DISP_OPEN_IF:
340                 flags = create_flags;
341                 break;
342         default:
343                 flags = 0;
344                 break;
345         }
346         
347         flags |= rdwr_flags;
348
349         if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
350                 flags = O_RDONLY | O_DIRECTORY;
351                 if (lp_readonly(req->tcon->service)) {
352                         goto do_open;
353                 }
354                 switch (io->generic.in.open_disposition) {
355                 case NTCREATEX_DISP_CREATE:
356                         if (mkdir(unix_path, 0755) == -1) {
357                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
358                                 return map_nt_error_from_unix(errno);
359                         }
360                         break;
361                 case NTCREATEX_DISP_OPEN_IF:
362                         if (mkdir(unix_path, 0755) == -1 && errno != EEXIST) {
363                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
364                                 return map_nt_error_from_unix(errno);
365                         }
366                         break;
367                 }
368         }
369
370 do_open:
371         fd = open(unix_path, flags, 0644);
372         if (fd == -1) {
373                 return map_nt_error_from_unix(errno);
374         }
375
376         if (fstat(fd, &st) == -1) {
377                 DEBUG(9,("svfs_open: fstat errno=%d\n", errno));
378                 close(fd);
379                 return map_nt_error_from_unix(errno);
380         }
381
382         f = talloc_p(req->tcon, struct svfs_file);
383         f->fd = fd;
384         f->name = talloc_strdup(req->tcon, unix_path);
385
386         DLIST_ADD(private->open_files, f);
387
388         ZERO_STRUCT(io->generic.out);
389         
390         unix_to_nt_time(&io->generic.out.create_time, st.st_ctime);
391         unix_to_nt_time(&io->generic.out.access_time, st.st_atime);
392         unix_to_nt_time(&io->generic.out.write_time,  st.st_mtime);
393         unix_to_nt_time(&io->generic.out.change_time, st.st_mtime);
394         io->generic.out.fnum = fd;
395         io->generic.out.alloc_size = st.st_size;
396         io->generic.out.size = st.st_size;
397         io->generic.out.attrib = svfs_unix_to_dos_attrib(st.st_mode);
398         io->generic.out.is_directory = S_ISDIR(st.st_mode) ? 1 : 0;
399
400         return NT_STATUS_OK;
401 }
402
403 /*
404   create a directory
405 */
406 static NTSTATUS svfs_mkdir(struct ntvfs_module_context *ntvfs,
407                            struct smbsrv_request *req, union smb_mkdir *md)
408 {
409         char *unix_path;
410
411         CHECK_READ_ONLY(req);
412
413         if (md->generic.level != RAW_MKDIR_MKDIR) {
414                 return NT_STATUS_INVALID_LEVEL;
415         }
416
417         unix_path = svfs_unix_path(ntvfs, req, md->mkdir.in.path);
418
419         if (mkdir(unix_path, 0777) == -1) {
420                 return map_nt_error_from_unix(errno);
421         }
422
423         return NT_STATUS_OK;
424 }
425
426 /*
427   remove a directory
428 */
429 static NTSTATUS svfs_rmdir(struct ntvfs_module_context *ntvfs,
430                            struct smbsrv_request *req, struct smb_rmdir *rd)
431 {
432         char *unix_path;
433
434         CHECK_READ_ONLY(req);
435
436         unix_path = svfs_unix_path(ntvfs, req, rd->in.path);
437
438         if (rmdir(unix_path) == -1) {
439                 return map_nt_error_from_unix(errno);
440         }
441
442         return NT_STATUS_OK;
443 }
444
445 /*
446   rename a set of files
447 */
448 static NTSTATUS svfs_rename(struct ntvfs_module_context *ntvfs,
449                             struct smbsrv_request *req, union smb_rename *ren)
450 {
451         char *unix_path1, *unix_path2;
452
453         CHECK_READ_ONLY(req);
454
455         if (ren->generic.level != RAW_RENAME_RENAME) {
456                 return NT_STATUS_INVALID_LEVEL;
457         }
458
459         unix_path1 = svfs_unix_path(ntvfs, req, ren->rename.in.pattern1);
460         unix_path2 = svfs_unix_path(ntvfs, req, ren->rename.in.pattern2);
461
462         if (rename(unix_path1, unix_path2) == -1) {
463                 return map_nt_error_from_unix(errno);
464         }
465         
466         return NT_STATUS_OK;
467 }
468
469 /*
470   copy a set of files
471 */
472 static NTSTATUS svfs_copy(struct ntvfs_module_context *ntvfs,
473                           struct smbsrv_request *req, struct smb_copy *cp)
474 {
475         return NT_STATUS_NOT_SUPPORTED;
476 }
477
478 /*
479   read from a file
480 */
481 static NTSTATUS svfs_read(struct ntvfs_module_context *ntvfs,
482                           struct smbsrv_request *req, union smb_read *rd)
483 {
484         ssize_t ret;
485
486         if (rd->generic.level != RAW_READ_READX) {
487                 return NT_STATUS_NOT_SUPPORTED;
488         }
489
490         ret = pread(rd->readx.in.fnum, 
491                     rd->readx.out.data, 
492                     rd->readx.in.maxcnt,
493                     rd->readx.in.offset);
494         if (ret == -1) {
495                 return map_nt_error_from_unix(errno);
496         }
497
498         rd->readx.out.nread = ret;
499         rd->readx.out.remaining = 0; /* should fill this in? */
500         rd->readx.out.compaction_mode = 0; 
501
502         return NT_STATUS_OK;
503 }
504
505 /*
506   write to a file
507 */
508 static NTSTATUS svfs_write(struct ntvfs_module_context *ntvfs,
509                            struct smbsrv_request *req, union smb_write *wr)
510 {
511         ssize_t ret;
512
513         if (wr->generic.level != RAW_WRITE_WRITEX) {
514                 return ntvfs_map_write(req, wr, ntvfs);
515         }
516
517         CHECK_READ_ONLY(req);
518
519         ret = pwrite(wr->writex.in.fnum, 
520                      wr->writex.in.data, 
521                      wr->writex.in.count,
522                      wr->writex.in.offset);
523         if (ret == -1) {
524                 return map_nt_error_from_unix(errno);
525         }
526                 
527         wr->writex.out.nwritten = ret;
528         wr->writex.out.remaining = 0; /* should fill this in? */
529         
530         return NT_STATUS_OK;
531 }
532
533 /*
534   seek in a file
535 */
536 static NTSTATUS svfs_seek(struct ntvfs_module_context *ntvfs,
537                           struct smbsrv_request *req, struct smb_seek *io)
538 {
539         return NT_STATUS_NOT_SUPPORTED;
540 }
541
542 /*
543   flush a file
544 */
545 static NTSTATUS svfs_flush(struct ntvfs_module_context *ntvfs,
546                            struct smbsrv_request *req, struct smb_flush *io)
547 {
548         fsync(io->in.fnum);
549         return NT_STATUS_OK;
550 }
551
552 /*
553   close a file
554 */
555 static NTSTATUS svfs_close(struct ntvfs_module_context *ntvfs,
556                            struct smbsrv_request *req, union smb_close *io)
557 {
558         struct svfs_private *private = ntvfs->private_data;
559         struct svfs_file *f;
560
561         if (io->generic.level != RAW_CLOSE_CLOSE) {
562                 /* we need a mapping function */
563                 return NT_STATUS_INVALID_LEVEL;
564         }
565
566         f = find_fd(private, io->close.in.fnum);
567         if (!f) {
568                 return NT_STATUS_INVALID_HANDLE;
569         }
570
571         if (close(io->close.in.fnum) == -1) {
572                 return map_nt_error_from_unix(errno);
573         }
574
575         DLIST_REMOVE(private->open_files, f);
576         talloc_free(f->name);
577         talloc_free(f);
578
579         return NT_STATUS_OK;
580 }
581
582 /*
583   exit - closing files
584 */
585 static NTSTATUS svfs_exit(struct ntvfs_module_context *ntvfs,
586                           struct smbsrv_request *req)
587 {
588         return NT_STATUS_NOT_SUPPORTED;
589 }
590
591 /*
592   logoff - closing files
593 */
594 static NTSTATUS svfs_logoff(struct ntvfs_module_context *ntvfs,
595                             struct smbsrv_request *req)
596 {
597         return NT_STATUS_NOT_SUPPORTED;
598 }
599
600 /*
601   setup for an async call
602 */
603 static NTSTATUS svfs_async_setup(struct ntvfs_module_context *ntvfs,
604                                  struct smbsrv_request *req, 
605                                  void *private)
606 {
607         return NT_STATUS_OK;
608 }
609
610 /*
611   lock a byte range
612 */
613 static NTSTATUS svfs_lock(struct ntvfs_module_context *ntvfs,
614                           struct smbsrv_request *req, union smb_lock *lck)
615 {
616         DEBUG(0,("REWRITE: not doing byte range locking!\n"));
617         return NT_STATUS_OK;
618 }
619
620 /*
621   set info on a pathname
622 */
623 static NTSTATUS svfs_setpathinfo(struct ntvfs_module_context *ntvfs,
624                                  struct smbsrv_request *req, union smb_setfileinfo *st)
625 {
626         CHECK_READ_ONLY(req);
627
628         return NT_STATUS_NOT_SUPPORTED;
629 }
630
631 /*
632   set info on a open file
633 */
634 static NTSTATUS svfs_setfileinfo(struct ntvfs_module_context *ntvfs,
635                                  struct smbsrv_request *req, 
636                                  union smb_setfileinfo *info)
637 {
638         struct utimbuf unix_times;
639         int fd;
640
641         CHECK_READ_ONLY(req);
642                 
643         switch (info->generic.level) {
644         case RAW_SFILEINFO_END_OF_FILE_INFO:
645         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
646                 if (ftruncate(info->end_of_file_info.file.fnum, 
647                               info->end_of_file_info.in.size) == -1) {
648                         return map_nt_error_from_unix(errno);
649                 }
650                 break;
651         case RAW_SFILEINFO_SETATTRE:
652                 unix_times.actime = info->setattre.in.access_time;
653                 unix_times.modtime = info->setattre.in.write_time;
654                 fd = info->setattre.file.fnum;
655         
656                 if (unix_times.actime == 0 && unix_times.modtime == 0) {
657                         break;
658                 } 
659
660                 /* set modify time = to access time if modify time was 0 */
661                 if (unix_times.actime != 0 && unix_times.modtime == 0) {
662                         unix_times.modtime = unix_times.actime;
663                 }
664
665                 /* Set the date on this file */
666                 if (svfs_file_utime(fd, &unix_times) != 0) {
667                         return NT_STATUS_ACCESS_DENIED;
668                 }
669                 break;
670         }
671         return NT_STATUS_OK;
672 }
673
674
675 /*
676   return filesystem space info
677 */
678 static NTSTATUS svfs_fsinfo(struct ntvfs_module_context *ntvfs,
679                             struct smbsrv_request *req, union smb_fsinfo *fs)
680 {
681         struct svfs_private *private = ntvfs->private_data;
682         struct stat st;
683
684         if (fs->generic.level != RAW_QFS_GENERIC) {
685                 return ntvfs_map_fsinfo(req, fs, ntvfs);
686         }
687
688         if (sys_fsusage(private->connectpath, 
689                         &fs->generic.out.blocks_free, 
690                         &fs->generic.out.blocks_total) == -1) {
691                 return map_nt_error_from_unix(errno);
692         }
693
694         fs->generic.out.block_size = 512;
695
696         if (stat(private->connectpath, &st) != 0) {
697                 return NT_STATUS_DISK_CORRUPT_ERROR;
698         }
699         
700         fs->generic.out.fs_id = st.st_ino;
701         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
702         fs->generic.out.serial_number = st.st_ino;
703         fs->generic.out.fs_attr = 0;
704         fs->generic.out.max_file_component_length = 255;
705         fs->generic.out.device_type = 0;
706         fs->generic.out.device_characteristics = 0;
707         fs->generic.out.quota_soft = 0;
708         fs->generic.out.quota_hard = 0;
709         fs->generic.out.quota_flags = 0;
710         fs->generic.out.volume_name = talloc_strdup(req, lp_servicename(req->tcon->service));
711         fs->generic.out.fs_type = req->tcon->fs_type;
712
713         return NT_STATUS_OK;
714 }
715
716 #if 0
717 /*
718   return filesystem attribute info
719 */
720 static NTSTATUS svfs_fsattr(struct ntvfs_module_context *ntvfs,
721                             struct smbsrv_request *req, union smb_fsattr *fs)
722 {
723         struct stat st;
724         struct svfs_private *private = ntvfs->private_data;
725
726         if (fs->generic.level != RAW_FSATTR_GENERIC) {
727                 return ntvfs_map_fsattr(req, fs, ntvfs);
728         }
729
730         if (stat(private->connectpath, &st) == -1) {
731                 return map_nt_error_from_unix(errno);
732         }
733
734         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
735         fs->generic.out.fs_attr = 
736                 FILE_CASE_PRESERVED_NAMES | 
737                 FILE_CASE_SENSITIVE_SEARCH | 
738                 FILE_PERSISTENT_ACLS;
739         fs->generic.out.max_file_component_length = 255;
740         fs->generic.out.serial_number = 1;
741         fs->generic.out.fs_type = talloc_strdup(req, "NTFS");
742         fs->generic.out.volume_name = talloc_strdup(req, 
743                                                     lp_servicename(req->tcon->service));
744
745         return NT_STATUS_OK;
746 }
747 #endif
748
749 /*
750   return print queue info
751 */
752 static NTSTATUS svfs_lpq(struct ntvfs_module_context *ntvfs,
753                          struct smbsrv_request *req, union smb_lpq *lpq)
754 {
755         return NT_STATUS_NOT_SUPPORTED;
756 }
757
758 /* 
759    list files in a directory matching a wildcard pattern
760 */
761 static NTSTATUS svfs_search_first(struct ntvfs_module_context *ntvfs,
762                                   struct smbsrv_request *req, union smb_search_first *io, 
763                                   void *search_private, 
764                                   BOOL (*callback)(void *, union smb_search_data *))
765 {
766         struct svfs_dir *dir;
767         int i;
768         struct svfs_private *private = ntvfs->private_data;
769         struct search_state *search;
770         union smb_search_data file;
771         uint_t max_count;
772
773         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
774                 return NT_STATUS_NOT_SUPPORTED;
775         }
776
777         search = talloc_zero_p(private, struct search_state);
778         if (!search) {
779                 return NT_STATUS_NO_MEMORY;
780         }
781
782         max_count = io->t2ffirst.in.max_count;
783
784         dir = svfs_list(ntvfs, req, io->t2ffirst.in.pattern);
785         if (!dir) {
786                 return NT_STATUS_FOOBAR;
787         }
788
789         search->handle = private->next_search_handle;
790         search->dir = dir;
791
792         if (dir->count < max_count) {
793                 max_count = dir->count;
794         }
795
796         for (i=0; i < max_count;i++) {
797                 ZERO_STRUCT(file);
798                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
799                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
800                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
801                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
802                 file.both_directory_info.name.s = dir->files[i].name;
803                 file.both_directory_info.short_name.s = dir->files[i].name;
804                 file.both_directory_info.size = dir->files[i].st.st_size;
805                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
806
807                 if (!callback(search_private, &file)) {
808                         break;
809                 }
810         }
811
812         search->current_index = i;
813
814         io->t2ffirst.out.count = i;
815         io->t2ffirst.out.handle = search->handle;
816         io->t2ffirst.out.end_of_search = (i == dir->count) ? 1 : 0;
817
818         /* work out if we are going to keep the search state */
819         if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
820             ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
821                 talloc_free(search);
822         } else {
823                 private->next_search_handle++;
824                 DLIST_ADD(private->search, search);
825         }
826
827         return NT_STATUS_OK;
828 }
829
830 /* continue a search */
831 static NTSTATUS svfs_search_next(struct ntvfs_module_context *ntvfs,
832                                  struct smbsrv_request *req, union smb_search_next *io, 
833                                  void *search_private, 
834                                  BOOL (*callback)(void *, union smb_search_data *))
835 {
836         struct svfs_dir *dir;
837         int i;
838         struct svfs_private *private = ntvfs->private_data;
839         struct search_state *search;
840         union smb_search_data file;
841         uint_t max_count;
842
843         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
844                 return NT_STATUS_NOT_SUPPORTED;
845         }
846
847         for (search=private->search; search; search = search->next) {
848                 if (search->handle == io->t2fnext.in.handle) break;
849         }
850         
851         if (!search) {
852                 /* we didn't find the search handle */
853                 return NT_STATUS_FOOBAR;
854         }
855
856         dir = search->dir;
857
858         /* the client might be asking for something other than just continuing
859            with the search */
860         if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) &&
861             (io->t2fnext.in.flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) &&
862             io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
863                 /* look backwards first */
864                 for (i=search->current_index; i > 0; i--) {
865                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
866                                 search->current_index = i;
867                                 goto found;
868                         }
869                 }
870
871                 /* then look forwards */
872                 for (i=search->current_index+1; i <= dir->count; i++) {
873                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
874                                 search->current_index = i;
875                                 goto found;
876                         }
877                 }
878         }
879
880 found:  
881         max_count = search->current_index + io->t2fnext.in.max_count;
882
883         if (max_count > dir->count) {
884                 max_count = dir->count;
885         }
886
887         for (i = search->current_index; i < max_count;i++) {
888                 ZERO_STRUCT(file);
889                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
890                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
891                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
892                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
893                 file.both_directory_info.name.s = dir->files[i].name;
894                 file.both_directory_info.short_name.s = dir->files[i].name;
895                 file.both_directory_info.size = dir->files[i].st.st_size;
896                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
897
898                 if (!callback(search_private, &file)) {
899                         break;
900                 }
901         }
902
903         io->t2fnext.out.count = i - search->current_index;
904         io->t2fnext.out.end_of_search = (i == dir->count) ? 1 : 0;
905
906         search->current_index = i;
907
908         /* work out if we are going to keep the search state */
909         if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
910             ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
911                 DLIST_REMOVE(private->search, search);
912                 talloc_free(search);
913         }
914
915         return NT_STATUS_OK;
916 }
917
918 /* close a search */
919 static NTSTATUS svfs_search_close(struct ntvfs_module_context *ntvfs,
920                                   struct smbsrv_request *req, union smb_search_close *io)
921 {
922         struct svfs_private *private = ntvfs->private_data;
923         struct search_state *search;
924
925         for (search=private->search; search; search = search->next) {
926                 if (search->handle == io->findclose.in.handle) break;
927         }
928         
929         if (!search) {
930                 /* we didn't find the search handle */
931                 return NT_STATUS_FOOBAR;
932         }
933
934         DLIST_REMOVE(private->search, search);
935         talloc_free(search);
936
937         return NT_STATUS_OK;
938 }
939
940 /* SMBtrans - not used on file shares */
941 static NTSTATUS svfs_trans(struct ntvfs_module_context *ntvfs,
942                            struct smbsrv_request *req, struct smb_trans2 *trans2)
943 {
944         return NT_STATUS_ACCESS_DENIED;
945 }
946
947
948 /*
949   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
950  */
951 NTSTATUS ntvfs_simple_init(void)
952 {
953         NTSTATUS ret;
954         struct ntvfs_ops ops;
955
956         ZERO_STRUCT(ops);
957
958         /* fill in all the operations */
959         ops.connect = svfs_connect;
960         ops.disconnect = svfs_disconnect;
961         ops.unlink = svfs_unlink;
962         ops.chkpath = svfs_chkpath;
963         ops.qpathinfo = svfs_qpathinfo;
964         ops.setpathinfo = svfs_setpathinfo;
965         ops.openfile = svfs_open;
966         ops.mkdir = svfs_mkdir;
967         ops.rmdir = svfs_rmdir;
968         ops.rename = svfs_rename;
969         ops.copy = svfs_copy;
970         ops.ioctl = svfs_ioctl;
971         ops.read = svfs_read;
972         ops.write = svfs_write;
973         ops.seek = svfs_seek;
974         ops.flush = svfs_flush; 
975         ops.close = svfs_close;
976         ops.exit = svfs_exit;
977         ops.lock = svfs_lock;
978         ops.setfileinfo = svfs_setfileinfo;
979         ops.qfileinfo = svfs_qfileinfo;
980         ops.fsinfo = svfs_fsinfo;
981         ops.lpq = svfs_lpq;
982         ops.search_first = svfs_search_first;
983         ops.search_next = svfs_search_next;
984         ops.search_close = svfs_search_close;
985         ops.trans = svfs_trans;
986         ops.logoff = svfs_logoff;
987         ops.async_setup = svfs_async_setup;
988
989         /* register ourselves with the NTVFS subsystem. We register
990            under names 'simple'
991         */
992
993         ops.type = NTVFS_DISK;
994         ops.name = "simple";
995         ret = register_backend("ntvfs", &ops);
996
997         if (!NT_STATUS_IS_OK(ret)) {
998                 DEBUG(0,("Failed to register simple backend with name: %s!\n",
999                          ops.name));
1000         }
1001
1002         return ret;
1003 }