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