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