Fixed a dereferenced null pointer.
[ira/wip.git] / source3 / modules / vfs_default.c
1 /*
2    Unix SMB/CIFS implementation.
3    Wrap disk only vfs functions to sidestep dodgy compilers.
4    Copyright (C) Tim Potter 1998
5    Copyright (C) Jeremy Allison 2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
25
26 /* Check for NULL pointer parameters in vfswrap_* functions */
27
28 /* We don't want to have NULL function pointers lying around.  Someone
29    is sure to try and execute them.  These stubs are used to prevent
30    this possibility. */
31
32 static int vfswrap_connect(vfs_handle_struct *handle,  const char *service, const char *user)
33 {
34     return 0;    /* Return >= 0 for success */
35 }
36
37 static void vfswrap_disconnect(vfs_handle_struct *handle)
38 {
39 }
40
41 /* Disk operations */
42
43 static uint64_t vfswrap_disk_free(vfs_handle_struct *handle,  const char *path, bool small_query, uint64_t *bsize,
44                                uint64_t *dfree, uint64_t *dsize)
45 {
46         uint64_t result;
47
48         result = sys_disk_free(handle->conn, path, small_query, bsize, dfree, dsize);
49         return result;
50 }
51
52 static int vfswrap_get_quota(struct vfs_handle_struct *handle,  enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
53 {
54 #ifdef HAVE_SYS_QUOTAS
55         int result;
56
57         START_PROFILE(syscall_get_quota);
58         result = sys_get_quota(handle->conn->connectpath, qtype, id, qt);
59         END_PROFILE(syscall_get_quota);
60         return result;
61 #else
62         errno = ENOSYS;
63         return -1;
64 #endif
65 }
66
67 static int vfswrap_set_quota(struct vfs_handle_struct *handle,  enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *qt)
68 {
69 #ifdef HAVE_SYS_QUOTAS
70         int result;
71
72         START_PROFILE(syscall_set_quota);
73         result = sys_set_quota(handle->conn->connectpath, qtype, id, qt);
74         END_PROFILE(syscall_set_quota);
75         return result;
76 #else
77         errno = ENOSYS;
78         return -1;
79 #endif
80 }
81
82 static int vfswrap_get_shadow_copy_data(struct vfs_handle_struct *handle, struct files_struct *fsp, SHADOW_COPY_DATA *shadow_copy_data, bool labels)
83 {
84         errno = ENOSYS;
85         return -1;  /* Not implemented. */
86 }
87
88 static int vfswrap_statvfs(struct vfs_handle_struct *handle,  const char *path, vfs_statvfs_struct *statbuf)
89 {
90         return sys_statvfs(path, statbuf);
91 }
92
93 static uint32_t vfswrap_fs_capabilities(struct vfs_handle_struct *handle,
94                 enum timestamp_set_resolution *p_ts_res)
95 {
96         connection_struct *conn = handle->conn;
97         uint32_t caps = FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
98         struct smb_filename *smb_fname_cpath = NULL;
99         NTSTATUS status;
100         int ret = -1;
101
102 #if defined(DARWINOS)
103         struct vfs_statvfs_struct statbuf;
104         ZERO_STRUCT(statbuf);
105         sys_statvfs(conn->connectpath, &statbuf);
106         caps = statbuf.FsCapabilities;
107 #endif
108
109         *p_ts_res = TIMESTAMP_SET_SECONDS;
110
111         /* Work out what timestamp resolution we can
112          * use when setting a timestamp. */
113
114         status = create_synthetic_smb_fname(talloc_tos(),
115                                 conn->connectpath,
116                                 NULL,
117                                 NULL,
118                                 &smb_fname_cpath);
119         if (!NT_STATUS_IS_OK(status)) {
120                 return caps;
121         }
122
123         ret = SMB_VFS_STAT(conn, smb_fname_cpath);
124         if (ret == -1) {
125                 TALLOC_FREE(smb_fname_cpath);
126                 return caps;
127         }
128
129         if (smb_fname_cpath->st.st_ex_mtime.tv_nsec ||
130                         smb_fname_cpath->st.st_ex_atime.tv_nsec ||
131                         smb_fname_cpath->st.st_ex_ctime.tv_nsec) {
132                 /* If any of the normal UNIX directory timestamps
133                  * have a non-zero tv_nsec component assume
134                  * we might be able to set sub-second timestamps.
135                  * See what filetime set primitives we have.
136                  */
137 #if defined(HAVE_UTIMES)
138                 /* utimes allows msec timestamps to be set. */
139                 *p_ts_res = TIMESTAMP_SET_MSEC;
140 #elif defined(HAVE_UTIME)
141                 /* utime only allows sec timestamps to be set. */
142                 *p_ts_res = TIMESTAMP_SET_SECONDS;
143 #endif
144
145                 /* TODO. Add a configure test for the Linux
146                  * nsec timestamp set system call, and use it
147                  * if available....
148                  */
149                 DEBUG(10,("vfswrap_fs_capabilities: timestamp "
150                         "resolution of %s "
151                         "available on share %s, directory %s\n",
152                         *p_ts_res == TIMESTAMP_SET_MSEC ? "msec" : "sec",
153                         lp_servicename(conn->cnum),
154                         conn->connectpath ));
155         }
156         TALLOC_FREE(smb_fname_cpath);
157         return caps;
158 }
159
160 /* Directory operations */
161
162 static SMB_STRUCT_DIR *vfswrap_opendir(vfs_handle_struct *handle,  const char *fname, const char *mask, uint32 attr)
163 {
164         SMB_STRUCT_DIR *result;
165
166         START_PROFILE(syscall_opendir);
167         result = sys_opendir(fname);
168         END_PROFILE(syscall_opendir);
169         return result;
170 }
171
172 static SMB_STRUCT_DIRENT *vfswrap_readdir(vfs_handle_struct *handle,
173                                           SMB_STRUCT_DIR *dirp,
174                                           SMB_STRUCT_STAT *sbuf)
175 {
176         SMB_STRUCT_DIRENT *result;
177
178         START_PROFILE(syscall_readdir);
179         result = sys_readdir(dirp);
180         /* Default Posix readdir() does not give us stat info.
181          * Set to invalid to indicate we didn't return this info. */
182         if (sbuf)
183                 SET_STAT_INVALID(*sbuf);
184         END_PROFILE(syscall_readdir);
185         return result;
186 }
187
188 static void vfswrap_seekdir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp, long offset)
189 {
190         START_PROFILE(syscall_seekdir);
191         sys_seekdir(dirp, offset);
192         END_PROFILE(syscall_seekdir);
193 }
194
195 static long vfswrap_telldir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
196 {
197         long result;
198         START_PROFILE(syscall_telldir);
199         result = sys_telldir(dirp);
200         END_PROFILE(syscall_telldir);
201         return result;
202 }
203
204 static void vfswrap_rewinddir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
205 {
206         START_PROFILE(syscall_rewinddir);
207         sys_rewinddir(dirp);
208         END_PROFILE(syscall_rewinddir);
209 }
210
211 static int vfswrap_mkdir(vfs_handle_struct *handle,  const char *path, mode_t mode)
212 {
213         int result;
214         bool has_dacl = False;
215         char *parent = NULL;
216
217         START_PROFILE(syscall_mkdir);
218
219         if (lp_inherit_acls(SNUM(handle->conn))
220             && parent_dirname(talloc_tos(), path, &parent, NULL)
221             && (has_dacl = directory_has_default_acl(handle->conn, parent)))
222                 mode = 0777;
223
224         TALLOC_FREE(parent);
225
226         result = mkdir(path, mode);
227
228         if (result == 0 && !has_dacl) {
229                 /*
230                  * We need to do this as the default behavior of POSIX ACLs
231                  * is to set the mask to be the requested group permission
232                  * bits, not the group permission bits to be the requested
233                  * group permission bits. This is not what we want, as it will
234                  * mess up any inherited ACL bits that were set. JRA.
235                  */
236                 int saved_errno = errno; /* We may get ENOSYS */
237                 if ((SMB_VFS_CHMOD_ACL(handle->conn, path, mode) == -1) && (errno == ENOSYS))
238                         errno = saved_errno;
239         }
240
241         END_PROFILE(syscall_mkdir);
242         return result;
243 }
244
245 static int vfswrap_rmdir(vfs_handle_struct *handle,  const char *path)
246 {
247         int result;
248
249         START_PROFILE(syscall_rmdir);
250         result = rmdir(path);
251         END_PROFILE(syscall_rmdir);
252         return result;
253 }
254
255 static int vfswrap_closedir(vfs_handle_struct *handle,  SMB_STRUCT_DIR *dirp)
256 {
257         int result;
258
259         START_PROFILE(syscall_closedir);
260         result = sys_closedir(dirp);
261         END_PROFILE(syscall_closedir);
262         return result;
263 }
264
265 static void vfswrap_init_search_op(vfs_handle_struct *handle,
266                                    SMB_STRUCT_DIR *dirp)
267 {
268         /* Default behavior is a NOOP */
269 }
270
271 /* File operations */
272
273 static int vfswrap_open(vfs_handle_struct *handle,
274                         struct smb_filename *smb_fname,
275                         files_struct *fsp, int flags, mode_t mode)
276 {
277         int result = -1;
278
279         START_PROFILE(syscall_open);
280
281         if (smb_fname->stream_name) {
282                 errno = ENOENT;
283                 goto out;
284         }
285
286         result = sys_open(smb_fname->base_name, flags, mode);
287  out:
288         END_PROFILE(syscall_open);
289         return result;
290 }
291
292 static NTSTATUS vfswrap_create_file(vfs_handle_struct *handle,
293                                     struct smb_request *req,
294                                     uint16_t root_dir_fid,
295                                     struct smb_filename *smb_fname,
296                                     uint32_t access_mask,
297                                     uint32_t share_access,
298                                     uint32_t create_disposition,
299                                     uint32_t create_options,
300                                     uint32_t file_attributes,
301                                     uint32_t oplock_request,
302                                     uint64_t allocation_size,
303                                     struct security_descriptor *sd,
304                                     struct ea_list *ea_list,
305                                     files_struct **result,
306                                     int *pinfo)
307 {
308         return create_file_default(handle->conn, req, root_dir_fid, smb_fname,
309                                    access_mask, share_access,
310                                    create_disposition, create_options,
311                                    file_attributes, oplock_request,
312                                    allocation_size, sd, ea_list, result,
313                                    pinfo);
314 }
315
316 static int vfswrap_close(vfs_handle_struct *handle, files_struct *fsp)
317 {
318         int result;
319
320         START_PROFILE(syscall_close);
321         result = fd_close_posix(fsp);
322         END_PROFILE(syscall_close);
323         return result;
324 }
325
326 static ssize_t vfswrap_read(vfs_handle_struct *handle, files_struct *fsp, void *data, size_t n)
327 {
328         ssize_t result;
329
330         START_PROFILE_BYTES(syscall_read, n);
331         result = sys_read(fsp->fh->fd, data, n);
332         END_PROFILE(syscall_read);
333         return result;
334 }
335
336 static ssize_t vfswrap_pread(vfs_handle_struct *handle, files_struct *fsp, void *data,
337                         size_t n, SMB_OFF_T offset)
338 {
339         ssize_t result;
340
341 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
342         START_PROFILE_BYTES(syscall_pread, n);
343         result = sys_pread(fsp->fh->fd, data, n, offset);
344         END_PROFILE(syscall_pread);
345
346         if (result == -1 && errno == ESPIPE) {
347                 /* Maintain the fiction that pipes can be seeked (sought?) on. */
348                 result = SMB_VFS_READ(fsp, data, n);
349                 fsp->fh->pos = 0;
350         }
351
352 #else /* HAVE_PREAD */
353         SMB_OFF_T   curr;
354         int lerrno;
355
356         curr = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
357         if (curr == -1 && errno == ESPIPE) {
358                 /* Maintain the fiction that pipes can be seeked (sought?) on. */
359                 result = SMB_VFS_READ(fsp, data, n);
360                 fsp->fh->pos = 0;
361                 return result;
362         }
363
364         if (SMB_VFS_LSEEK(fsp, offset, SEEK_SET) == -1) {
365                 return -1;
366         }
367
368         errno = 0;
369         result = SMB_VFS_READ(fsp, data, n);
370         lerrno = errno;
371
372         SMB_VFS_LSEEK(fsp, curr, SEEK_SET);
373         errno = lerrno;
374
375 #endif /* HAVE_PREAD */
376
377         return result;
378 }
379
380 static ssize_t vfswrap_write(vfs_handle_struct *handle, files_struct *fsp, const void *data, size_t n)
381 {
382         ssize_t result;
383
384         START_PROFILE_BYTES(syscall_write, n);
385         result = sys_write(fsp->fh->fd, data, n);
386         END_PROFILE(syscall_write);
387         return result;
388 }
389
390 static ssize_t vfswrap_pwrite(vfs_handle_struct *handle, files_struct *fsp, const void *data,
391                         size_t n, SMB_OFF_T offset)
392 {
393         ssize_t result;
394
395 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
396         START_PROFILE_BYTES(syscall_pwrite, n);
397         result = sys_pwrite(fsp->fh->fd, data, n, offset);
398         END_PROFILE(syscall_pwrite);
399
400         if (result == -1 && errno == ESPIPE) {
401                 /* Maintain the fiction that pipes can be sought on. */
402                 result = SMB_VFS_WRITE(fsp, data, n);
403         }
404
405 #else /* HAVE_PWRITE */
406         SMB_OFF_T   curr;
407         int         lerrno;
408
409         curr = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
410         if (curr == -1) {
411                 return -1;
412         }
413
414         if (SMB_VFS_LSEEK(fsp, offset, SEEK_SET) == -1) {
415                 return -1;
416         }
417
418         result = SMB_VFS_WRITE(fsp, data, n);
419         lerrno = errno;
420
421         SMB_VFS_LSEEK(fsp, curr, SEEK_SET);
422         errno = lerrno;
423
424 #endif /* HAVE_PWRITE */
425
426         return result;
427 }
428
429 static SMB_OFF_T vfswrap_lseek(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T offset, int whence)
430 {
431         SMB_OFF_T result = 0;
432
433         START_PROFILE(syscall_lseek);
434
435         /* Cope with 'stat' file opens. */
436         if (fsp->fh->fd != -1)
437                 result = sys_lseek(fsp->fh->fd, offset, whence);
438
439         /*
440          * We want to maintain the fiction that we can seek
441          * on a fifo for file system purposes. This allows
442          * people to set up UNIX fifo's that feed data to Windows
443          * applications. JRA.
444          */
445
446         if((result == -1) && (errno == ESPIPE)) {
447                 result = 0;
448                 errno = 0;
449         }
450
451         END_PROFILE(syscall_lseek);
452         return result;
453 }
454
455 static ssize_t vfswrap_sendfile(vfs_handle_struct *handle, int tofd, files_struct *fromfsp, const DATA_BLOB *hdr,
456                         SMB_OFF_T offset, size_t n)
457 {
458         ssize_t result;
459
460         START_PROFILE_BYTES(syscall_sendfile, n);
461         result = sys_sendfile(tofd, fromfsp->fh->fd, hdr, offset, n);
462         END_PROFILE(syscall_sendfile);
463         return result;
464 }
465
466 static ssize_t vfswrap_recvfile(vfs_handle_struct *handle,
467                         int fromfd,
468                         files_struct *tofsp,
469                         SMB_OFF_T offset,
470                         size_t n)
471 {
472         ssize_t result;
473
474         START_PROFILE_BYTES(syscall_recvfile, n);
475         result = sys_recvfile(fromfd, tofsp->fh->fd, offset, n);
476         END_PROFILE(syscall_recvfile);
477         return result;
478 }
479
480 /*********************************************************
481  For rename across filesystems Patch from Warren Birnbaum
482  <warrenb@hpcvscdp.cv.hp.com>
483 **********************************************************/
484
485 static int copy_reg(const char *source, const char *dest)
486 {
487         SMB_STRUCT_STAT source_stats;
488         int saved_errno;
489         int ifd = -1;
490         int ofd = -1;
491
492         if (sys_lstat (source, &source_stats) == -1)
493                 return -1;
494
495         if (!S_ISREG (source_stats.st_ex_mode))
496                 return -1;
497
498         if((ifd = sys_open (source, O_RDONLY, 0)) < 0)
499                 return -1;
500
501         if (unlink (dest) && errno != ENOENT)
502                 return -1;
503
504 #ifdef O_NOFOLLOW
505         if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600)) < 0 )
506 #else
507         if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC , 0600)) < 0 )
508 #endif
509                 goto err;
510
511         if (transfer_file(ifd, ofd, (size_t)-1) == -1)
512                 goto err;
513
514         /*
515          * Try to preserve ownership.  For non-root it might fail, but that's ok.
516          * But root probably wants to know, e.g. if NFS disallows it.
517          */
518
519 #ifdef HAVE_FCHOWN
520         if ((fchown(ofd, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
521 #else
522         if ((chown(dest, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM))
523 #endif
524                 goto err;
525
526         /*
527          * fchown turns off set[ug]id bits for non-root,
528          * so do the chmod last.
529          */
530
531 #if defined(HAVE_FCHMOD)
532         if (fchmod (ofd, source_stats.st_ex_mode & 07777))
533 #else
534         if (chmod (dest, source_stats.st_ex_mode & 07777))
535 #endif
536                 goto err;
537
538         if (close (ifd) == -1)
539                 goto err;
540
541         if (close (ofd) == -1)
542                 return -1;
543
544         /* Try to copy the old file's modtime and access time.  */
545         {
546                 struct utimbuf tv;
547
548                 tv.actime = convert_timespec_to_time_t(source_stats.st_ex_atime);
549                 tv.modtime = convert_timespec_to_time_t(source_stats.st_ex_mtime);
550                 utime(dest, &tv);
551         }
552
553         if (unlink (source) == -1)
554                 return -1;
555
556         return 0;
557
558   err:
559
560         saved_errno = errno;
561         if (ifd != -1)
562                 close(ifd);
563         if (ofd != -1)
564                 close(ofd);
565         errno = saved_errno;
566         return -1;
567 }
568
569 static int vfswrap_rename(vfs_handle_struct *handle,
570                           const struct smb_filename *smb_fname_src,
571                           const struct smb_filename *smb_fname_dst)
572 {
573         int result = -1;
574
575         START_PROFILE(syscall_rename);
576
577         if (smb_fname_src->stream_name || smb_fname_dst->stream_name) {
578                 errno = ENOENT;
579                 goto out;
580         }
581
582         result = rename(smb_fname_src->base_name, smb_fname_dst->base_name);
583         if ((result == -1) && (errno == EXDEV)) {
584                 /* Rename across filesystems needed. */
585                 result = copy_reg(smb_fname_src->base_name,
586                                   smb_fname_dst->base_name);
587         }
588
589  out:
590         END_PROFILE(syscall_rename);
591         return result;
592 }
593
594 static int vfswrap_fsync(vfs_handle_struct *handle, files_struct *fsp)
595 {
596 #ifdef HAVE_FSYNC
597         int result;
598
599         START_PROFILE(syscall_fsync);
600         result = fsync(fsp->fh->fd);
601         END_PROFILE(syscall_fsync);
602         return result;
603 #else
604         return 0;
605 #endif
606 }
607
608 static int vfswrap_stat(vfs_handle_struct *handle,
609                         struct smb_filename *smb_fname)
610 {
611         int result = -1;
612
613         START_PROFILE(syscall_stat);
614
615         if (smb_fname->stream_name) {
616                 errno = ENOENT;
617                 goto out;
618         }
619
620         result = sys_stat(smb_fname->base_name, &smb_fname->st);
621  out:
622         END_PROFILE(syscall_stat);
623         return result;
624 }
625
626 static int vfswrap_fstat(vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
627 {
628         int result;
629
630         START_PROFILE(syscall_fstat);
631         result = sys_fstat(fsp->fh->fd, sbuf);
632         END_PROFILE(syscall_fstat);
633         return result;
634 }
635
636 static int vfswrap_lstat(vfs_handle_struct *handle,
637                          struct smb_filename *smb_fname)
638 {
639         int result = -1;
640
641         START_PROFILE(syscall_lstat);
642
643         if (smb_fname->stream_name) {
644                 errno = ENOENT;
645                 goto out;
646         }
647
648         result = sys_lstat(smb_fname->base_name, &smb_fname->st);
649  out:
650         END_PROFILE(syscall_lstat);
651         return result;
652 }
653
654 static NTSTATUS vfswrap_translate_name(vfs_handle_struct *handle,
655                                        char **mapped_name)
656 {
657         /* Default behavior is a NOOP */
658
659         if (*mapped_name != NULL)
660                 return NT_STATUS_OK;
661
662         return NT_STATUS_INVALID_PARAMETER;
663 }
664 /********************************************************************
665  Given a stat buffer return the allocated size on disk, taking into
666  account sparse files.
667 ********************************************************************/
668 static uint64_t vfswrap_get_alloc_size(vfs_handle_struct *handle,
669                                        struct files_struct *fsp,
670                                        const SMB_STRUCT_STAT *sbuf)
671 {
672         uint64_t result;
673
674         START_PROFILE(syscall_get_alloc_size);
675
676         if(S_ISDIR(sbuf->st_ex_mode)) {
677                 result = 0;
678                 goto out;
679         }
680
681 #if defined(HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
682         result = (uint64_t)STAT_ST_BLOCKSIZE * (uint64_t)sbuf->st_ex_blocks;
683 #else
684         result = get_file_size_stat(sbuf);
685 #endif
686
687         if (fsp && fsp->initial_allocation_size)
688                 result = MAX(result,fsp->initial_allocation_size);
689
690         result = smb_roundup(handle->conn, result);
691
692  out:
693         END_PROFILE(syscall_get_alloc_size);
694         return result;
695 }
696
697 static int vfswrap_unlink(vfs_handle_struct *handle,
698                           const struct smb_filename *smb_fname)
699 {
700         int result = -1;
701
702         START_PROFILE(syscall_unlink);
703
704         if (smb_fname->stream_name) {
705                 errno = ENOENT;
706                 goto out;
707         }
708         result = unlink(smb_fname->base_name);
709
710  out:
711         END_PROFILE(syscall_unlink);
712         return result;
713 }
714
715 static int vfswrap_chmod(vfs_handle_struct *handle,  const char *path, mode_t mode)
716 {
717         int result;
718
719         START_PROFILE(syscall_chmod);
720
721         /*
722          * We need to do this due to the fact that the default POSIX ACL
723          * chmod modifies the ACL *mask* for the group owner, not the
724          * group owner bits directly. JRA.
725          */
726
727
728         {
729                 int saved_errno = errno; /* We might get ENOSYS */
730                 if ((result = SMB_VFS_CHMOD_ACL(handle->conn, path, mode)) == 0) {
731                         END_PROFILE(syscall_chmod);
732                         return result;
733                 }
734                 /* Error - return the old errno. */
735                 errno = saved_errno;
736         }
737
738         result = chmod(path, mode);
739         END_PROFILE(syscall_chmod);
740         return result;
741 }
742
743 static int vfswrap_fchmod(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
744 {
745         int result;
746
747         START_PROFILE(syscall_fchmod);
748
749         /*
750          * We need to do this due to the fact that the default POSIX ACL
751          * chmod modifies the ACL *mask* for the group owner, not the
752          * group owner bits directly. JRA.
753          */
754
755         {
756                 int saved_errno = errno; /* We might get ENOSYS */
757                 if ((result = SMB_VFS_FCHMOD_ACL(fsp, mode)) == 0) {
758                         END_PROFILE(syscall_fchmod);
759                         return result;
760                 }
761                 /* Error - return the old errno. */
762                 errno = saved_errno;
763         }
764
765 #if defined(HAVE_FCHMOD)
766         result = fchmod(fsp->fh->fd, mode);
767 #else
768         result = -1;
769         errno = ENOSYS;
770 #endif
771
772         END_PROFILE(syscall_fchmod);
773         return result;
774 }
775
776 static int vfswrap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
777 {
778         int result;
779
780         START_PROFILE(syscall_chown);
781         result = chown(path, uid, gid);
782         END_PROFILE(syscall_chown);
783         return result;
784 }
785
786 static int vfswrap_fchown(vfs_handle_struct *handle, files_struct *fsp, uid_t uid, gid_t gid)
787 {
788 #ifdef HAVE_FCHOWN
789         int result;
790
791         START_PROFILE(syscall_fchown);
792         result = fchown(fsp->fh->fd, uid, gid);
793         END_PROFILE(syscall_fchown);
794         return result;
795 #else
796         errno = ENOSYS;
797         return -1;
798 #endif
799 }
800
801 static int vfswrap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
802 {
803         int result;
804
805         START_PROFILE(syscall_lchown);
806         result = lchown(path, uid, gid);
807         END_PROFILE(syscall_lchown);
808         return result;
809 }
810
811 static int vfswrap_chdir(vfs_handle_struct *handle,  const char *path)
812 {
813         int result;
814
815         START_PROFILE(syscall_chdir);
816         result = chdir(path);
817         END_PROFILE(syscall_chdir);
818         return result;
819 }
820
821 static char *vfswrap_getwd(vfs_handle_struct *handle,  char *path)
822 {
823         char *result;
824
825         START_PROFILE(syscall_getwd);
826         result = sys_getwd(path);
827         END_PROFILE(syscall_getwd);
828         return result;
829 }
830
831 /*********************************************************************
832  nsec timestamp resolution call. Convert down to whatever the underlying
833  system will support.
834 **********************************************************************/
835
836 static int vfswrap_ntimes(vfs_handle_struct *handle,
837                           const struct smb_filename *smb_fname,
838                           struct smb_file_time *ft)
839 {
840         int result = -1;
841
842         START_PROFILE(syscall_ntimes);
843
844         if (smb_fname->stream_name) {
845                 errno = ENOENT;
846                 goto out;
847         }
848
849         if (null_timespec(ft->atime)) {
850                 ft->atime= smb_fname->st.st_ex_atime;
851         }
852
853         if (null_timespec(ft->mtime)) {
854                 ft->mtime = smb_fname->st.st_ex_mtime;
855         }
856
857         if (!null_timespec(ft->create_time) &&
858                         lp_store_create_time(SNUM(handle->conn))) {
859                 set_create_timespec_ea(handle->conn,
860                                 NULL,
861                                 smb_fname,
862                                 ft->create_time);
863         }
864
865         if ((timespec_compare(&ft->atime,
866                                 &smb_fname->st.st_ex_atime) == 0) &&
867                         (timespec_compare(&ft->mtime,
868                                 &smb_fname->st.st_ex_mtime) == 0)) {
869                 return 0;
870         }
871
872 #if defined(HAVE_UTIMES)
873         if (ft != NULL) {
874                 struct timeval tv[2];
875                 tv[0] = convert_timespec_to_timeval(ft->atime);
876                 tv[1] = convert_timespec_to_timeval(ft->mtime);
877                 result = utimes(smb_fname->base_name, tv);
878         } else {
879                 result = utimes(smb_fname->base_name, NULL);
880         }
881 #elif defined(HAVE_UTIME)
882         if (ft != NULL) {
883                 struct utimbuf times;
884                 times.actime = convert_timespec_to_time_t(ft->atime);
885                 times.modtime = convert_timespec_to_time_t(ft->mtime);
886                 result = utime(smb_fname->base_name, &times);
887         } else {
888                 result = utime(smb_fname->base_name, NULL);
889         }
890 #else
891         errno = ENOSYS;
892         result = -1;
893 #endif
894
895  out:
896         END_PROFILE(syscall_ntimes);
897         return result;
898 }
899
900 /*********************************************************************
901  A version of ftruncate that will write the space on disk if strict
902  allocate is set.
903 **********************************************************************/
904
905 static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T len)
906 {
907         SMB_STRUCT_STAT st;
908         SMB_OFF_T currpos = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
909         unsigned char zero_space[4096];
910         SMB_OFF_T space_to_write;
911
912         if (currpos == -1)
913                 return -1;
914
915         if (SMB_VFS_FSTAT(fsp, &st) == -1)
916                 return -1;
917
918         space_to_write = len - st.st_ex_size;
919
920 #ifdef S_ISFIFO
921         if (S_ISFIFO(st.st_ex_mode))
922                 return 0;
923 #endif
924
925         if (st.st_ex_size == len)
926                 return 0;
927
928         /* Shrink - just ftruncate. */
929         if (st.st_ex_size > len)
930                 return sys_ftruncate(fsp->fh->fd, len);
931
932         /* available disk space is enough or not? */
933         if (lp_strict_allocate(SNUM(fsp->conn))){
934                 uint64_t space_avail;
935                 uint64_t bsize,dfree,dsize;
936
937                 space_avail = get_dfree_info(fsp->conn,
938                                              fsp->fsp_name->base_name, false,
939                                              &bsize, &dfree, &dsize);
940                 /* space_avail is 1k blocks */
941                 if (space_avail == (uint64_t)-1 ||
942                                 ((uint64_t)space_to_write/1024 > space_avail) ) {
943                         errno = ENOSPC;
944                         return -1;
945                 }
946         }
947
948         /* Write out the real space on disk. */
949         if (SMB_VFS_LSEEK(fsp, st.st_ex_size, SEEK_SET) != st.st_ex_size)
950                 return -1;
951
952         space_to_write = len - st.st_ex_size;
953
954         memset(zero_space, '\0', sizeof(zero_space));
955         while ( space_to_write > 0) {
956                 SMB_OFF_T retlen;
957                 SMB_OFF_T current_len_to_write = MIN(sizeof(zero_space),space_to_write);
958
959                 retlen = SMB_VFS_WRITE(fsp,(char *)zero_space,current_len_to_write);
960                 if (retlen <= 0)
961                         return -1;
962
963                 space_to_write -= retlen;
964         }
965
966         /* Seek to where we were */
967         if (SMB_VFS_LSEEK(fsp, currpos, SEEK_SET) != currpos)
968                 return -1;
969
970         return 0;
971 }
972
973 static int vfswrap_ftruncate(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T len)
974 {
975         int result = -1;
976         SMB_STRUCT_STAT st;
977         char c = 0;
978         SMB_OFF_T currpos;
979
980         START_PROFILE(syscall_ftruncate);
981
982         if (lp_strict_allocate(SNUM(fsp->conn))) {
983                 result = strict_allocate_ftruncate(handle, fsp, len);
984                 END_PROFILE(syscall_ftruncate);
985                 return result;
986         }
987
988         /* we used to just check HAVE_FTRUNCATE_EXTEND and only use
989            sys_ftruncate if the system supports it. Then I discovered that
990            you can have some filesystems that support ftruncate
991            expansion and some that don't! On Linux fat can't do
992            ftruncate extend but ext2 can. */
993
994         result = sys_ftruncate(fsp->fh->fd, len);
995         if (result == 0)
996                 goto done;
997
998         /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
999            extend a file with ftruncate. Provide alternate implementation
1000            for this */
1001         currpos = SMB_VFS_LSEEK(fsp, 0, SEEK_CUR);
1002         if (currpos == -1) {
1003                 goto done;
1004         }
1005
1006         /* Do an fstat to see if the file is longer than the requested
1007            size in which case the ftruncate above should have
1008            succeeded or shorter, in which case seek to len - 1 and
1009            write 1 byte of zero */
1010         if (SMB_VFS_FSTAT(fsp, &st) == -1) {
1011                 goto done;
1012         }
1013
1014 #ifdef S_ISFIFO
1015         if (S_ISFIFO(st.st_ex_mode)) {
1016                 result = 0;
1017                 goto done;
1018         }
1019 #endif
1020
1021         if (st.st_ex_size == len) {
1022                 result = 0;
1023                 goto done;
1024         }
1025
1026         if (st.st_ex_size > len) {
1027                 /* the sys_ftruncate should have worked */
1028                 goto done;
1029         }
1030
1031         if (SMB_VFS_LSEEK(fsp, len-1, SEEK_SET) != len -1)
1032                 goto done;
1033
1034         if (SMB_VFS_WRITE(fsp, &c, 1)!=1)
1035                 goto done;
1036
1037         /* Seek to where we were */
1038         if (SMB_VFS_LSEEK(fsp, currpos, SEEK_SET) != currpos)
1039                 goto done;
1040         result = 0;
1041
1042   done:
1043
1044         END_PROFILE(syscall_ftruncate);
1045         return result;
1046 }
1047
1048 static bool vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
1049 {
1050         bool result;
1051
1052         START_PROFILE(syscall_fcntl_lock);
1053         result =  fcntl_lock(fsp->fh->fd, op, offset, count, type);
1054         END_PROFILE(syscall_fcntl_lock);
1055         return result;
1056 }
1057
1058 static int vfswrap_kernel_flock(vfs_handle_struct *handle, files_struct *fsp,
1059                                 uint32 share_mode)
1060 {
1061         START_PROFILE(syscall_kernel_flock);
1062         kernel_flock(fsp->fh->fd, share_mode);
1063         END_PROFILE(syscall_kernel_flock);
1064         return 0;
1065 }
1066
1067 static bool vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
1068 {
1069         bool result;
1070
1071         START_PROFILE(syscall_fcntl_getlock);
1072         result =  fcntl_getlock(fsp->fh->fd, poffset, pcount, ptype, ppid);
1073         END_PROFILE(syscall_fcntl_getlock);
1074         return result;
1075 }
1076
1077 static int vfswrap_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
1078                                 int leasetype)
1079 {
1080         int result = -1;
1081
1082         START_PROFILE(syscall_linux_setlease);
1083
1084 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1085         /* first set the signal handler */
1086         if(linux_set_lease_sighandler(fsp->fh->fd) == -1) {
1087                 return -1;
1088         }
1089
1090         result = linux_setlease(fsp->fh->fd, leasetype);
1091 #else
1092         errno = ENOSYS;
1093 #endif
1094         END_PROFILE(syscall_linux_setlease);
1095         return result;
1096 }
1097
1098 static int vfswrap_symlink(vfs_handle_struct *handle,  const char *oldpath, const char *newpath)
1099 {
1100         int result;
1101
1102         START_PROFILE(syscall_symlink);
1103         result = symlink(oldpath, newpath);
1104         END_PROFILE(syscall_symlink);
1105         return result;
1106 }
1107
1108 static int vfswrap_readlink(vfs_handle_struct *handle,  const char *path, char *buf, size_t bufsiz)
1109 {
1110         int result;
1111
1112         START_PROFILE(syscall_readlink);
1113         result = readlink(path, buf, bufsiz);
1114         END_PROFILE(syscall_readlink);
1115         return result;
1116 }
1117
1118 static int vfswrap_link(vfs_handle_struct *handle,  const char *oldpath, const char *newpath)
1119 {
1120         int result;
1121
1122         START_PROFILE(syscall_link);
1123         result = link(oldpath, newpath);
1124         END_PROFILE(syscall_link);
1125         return result;
1126 }
1127
1128 static int vfswrap_mknod(vfs_handle_struct *handle,  const char *pathname, mode_t mode, SMB_DEV_T dev)
1129 {
1130         int result;
1131
1132         START_PROFILE(syscall_mknod);
1133         result = sys_mknod(pathname, mode, dev);
1134         END_PROFILE(syscall_mknod);
1135         return result;
1136 }
1137
1138 static char *vfswrap_realpath(vfs_handle_struct *handle,  const char *path, char *resolved_path)
1139 {
1140         char *result;
1141
1142         START_PROFILE(syscall_realpath);
1143         result = realpath(path, resolved_path);
1144         END_PROFILE(syscall_realpath);
1145         return result;
1146 }
1147
1148 static NTSTATUS vfswrap_notify_watch(vfs_handle_struct *vfs_handle,
1149                                      struct sys_notify_context *ctx,
1150                                      struct notify_entry *e,
1151                                      void (*callback)(struct sys_notify_context *ctx, 
1152                                                       void *private_data,
1153                                                       struct notify_event *ev),
1154                                      void *private_data, void *handle)
1155 {
1156         /*
1157          * So far inotify is the only supported default notify mechanism. If
1158          * another platform like the the BSD's or a proprietary Unix comes
1159          * along and wants another default, we can play the same trick we
1160          * played with Posix ACLs.
1161          *
1162          * Until that is the case, hard-code inotify here.
1163          */
1164 #ifdef HAVE_INOTIFY
1165         if (lp_kernel_change_notify(ctx->conn->params)) {
1166                 return inotify_watch(ctx, e, callback, private_data, handle);
1167         }
1168 #endif
1169         /*
1170          * Do nothing, leave everything to notify_internal.c
1171          */
1172         return NT_STATUS_OK;
1173 }
1174
1175 static int vfswrap_chflags(vfs_handle_struct *handle, const char *path,
1176                            unsigned int flags)
1177 {
1178 #ifdef HAVE_CHFLAGS
1179         return chflags(path, flags);
1180 #else
1181         errno = ENOSYS;
1182         return -1;
1183 #endif
1184 }
1185
1186 static struct file_id vfswrap_file_id_create(struct vfs_handle_struct *handle,
1187                                              const SMB_STRUCT_STAT *sbuf)
1188 {
1189         struct file_id key;
1190
1191         /* the ZERO_STRUCT ensures padding doesn't break using the key as a
1192          * blob */
1193         ZERO_STRUCT(key);
1194
1195         key.devid = sbuf->st_ex_dev;
1196         key.inode = sbuf->st_ex_ino;
1197         /* key.extid is unused by default. */
1198
1199         return key;
1200 }
1201
1202 static NTSTATUS vfswrap_streaminfo(vfs_handle_struct *handle,
1203                                    struct files_struct *fsp,
1204                                    const char *fname,
1205                                    TALLOC_CTX *mem_ctx,
1206                                    unsigned int *pnum_streams,
1207                                    struct stream_struct **pstreams)
1208 {
1209         SMB_STRUCT_STAT sbuf;
1210         unsigned int num_streams = 0;
1211         struct stream_struct *streams = NULL;
1212         int ret;
1213
1214         if ((fsp != NULL) && (fsp->is_directory)) {
1215                 /*
1216                  * No default streams on directories
1217                  */
1218                 goto done;
1219         }
1220
1221         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
1222                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
1223         }
1224         else {
1225                 struct smb_filename *smb_fname = NULL;
1226                 NTSTATUS status;
1227
1228                 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
1229                                                     NULL, &smb_fname);
1230                 if (!NT_STATUS_IS_OK(status)) {
1231                         return status;
1232                 }
1233                 ret = SMB_VFS_STAT(handle->conn, smb_fname);
1234                 sbuf = smb_fname->st;
1235                 TALLOC_FREE(smb_fname);
1236         }
1237
1238         if (ret == -1) {
1239                 return map_nt_error_from_unix(errno);
1240         }
1241
1242         if (S_ISDIR(sbuf.st_ex_mode)) {
1243                 goto done;
1244         }
1245
1246         streams = talloc(mem_ctx, struct stream_struct);
1247
1248         if (streams == NULL) {
1249                 return NT_STATUS_NO_MEMORY;
1250         }
1251
1252         streams->size = sbuf.st_ex_size;
1253         streams->alloc_size = SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp, &sbuf);
1254
1255         streams->name = talloc_strdup(streams, "::$DATA");
1256         if (streams->name == NULL) {
1257                 TALLOC_FREE(streams);
1258                 return NT_STATUS_NO_MEMORY;
1259         }
1260
1261         num_streams = 1;
1262  done:
1263         *pnum_streams = num_streams;
1264         *pstreams = streams;
1265         return NT_STATUS_OK;
1266 }
1267
1268 static int vfswrap_get_real_filename(struct vfs_handle_struct *handle,
1269                                      const char *path,
1270                                      const char *name,
1271                                      TALLOC_CTX *mem_ctx,
1272                                      char **found_name)
1273 {
1274         /*
1275          * Don't fall back to get_real_filename so callers can differentiate
1276          * between a full directory scan and an actual case-insensitive stat.
1277          */
1278         errno = EOPNOTSUPP;
1279         return -1;
1280 }
1281
1282 static const char *vfswrap_connectpath(struct vfs_handle_struct *handle,
1283                                        const char *fname)
1284 {
1285         return handle->conn->connectpath;
1286 }
1287
1288 static NTSTATUS vfswrap_brl_lock_windows(struct vfs_handle_struct *handle,
1289                                          struct byte_range_lock *br_lck,
1290                                          struct lock_struct *plock,
1291                                          bool blocking_lock,
1292                                          struct blocking_lock_record *blr)
1293 {
1294         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1295
1296         /* Note: blr is not used in the default implementation. */
1297         return brl_lock_windows_default(br_lck, plock, blocking_lock);
1298 }
1299
1300 static bool vfswrap_brl_unlock_windows(struct vfs_handle_struct *handle,
1301                                        struct messaging_context *msg_ctx,
1302                                        struct byte_range_lock *br_lck,
1303                                        const struct lock_struct *plock)
1304 {
1305         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1306
1307         return brl_unlock_windows_default(msg_ctx, br_lck, plock);
1308 }
1309
1310 static bool vfswrap_brl_cancel_windows(struct vfs_handle_struct *handle,
1311                                        struct byte_range_lock *br_lck,
1312                                        struct lock_struct *plock,
1313                                        struct blocking_lock_record *blr)
1314 {
1315         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
1316
1317         /* Note: blr is not used in the default implementation. */
1318         return brl_lock_cancel_default(br_lck, plock);
1319 }
1320
1321 static bool vfswrap_strict_lock(struct vfs_handle_struct *handle,
1322                                 files_struct *fsp,
1323                                 struct lock_struct *plock)
1324 {
1325         SMB_ASSERT(plock->lock_type == READ_LOCK ||
1326             plock->lock_type == WRITE_LOCK);
1327
1328         return strict_lock_default(fsp, plock);
1329 }
1330
1331 static void vfswrap_strict_unlock(struct vfs_handle_struct *handle,
1332                                 files_struct *fsp,
1333                                 struct lock_struct *plock)
1334 {
1335         SMB_ASSERT(plock->lock_type == READ_LOCK ||
1336             plock->lock_type == WRITE_LOCK);
1337
1338         strict_unlock_default(fsp, plock);
1339 }
1340
1341 /* NT ACL operations. */
1342
1343 static NTSTATUS vfswrap_fget_nt_acl(vfs_handle_struct *handle,
1344                                     files_struct *fsp,
1345                                     uint32 security_info, SEC_DESC **ppdesc)
1346 {
1347         NTSTATUS result;
1348
1349         START_PROFILE(fget_nt_acl);
1350         result = posix_fget_nt_acl(fsp, security_info, ppdesc);
1351         END_PROFILE(fget_nt_acl);
1352         return result;
1353 }
1354
1355 static NTSTATUS vfswrap_get_nt_acl(vfs_handle_struct *handle,
1356                                    const char *name,
1357                                    uint32 security_info, SEC_DESC **ppdesc)
1358 {
1359         NTSTATUS result;
1360
1361         START_PROFILE(get_nt_acl);
1362         result = posix_get_nt_acl(handle->conn, name, security_info, ppdesc);
1363         END_PROFILE(get_nt_acl);
1364         return result;
1365 }
1366
1367 static NTSTATUS vfswrap_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp, uint32 security_info_sent, const SEC_DESC *psd)
1368 {
1369         NTSTATUS result;
1370
1371         START_PROFILE(fset_nt_acl);
1372         result = set_nt_acl(fsp, security_info_sent, psd);
1373         END_PROFILE(fset_nt_acl);
1374         return result;
1375 }
1376
1377 static int vfswrap_chmod_acl(vfs_handle_struct *handle,  const char *name, mode_t mode)
1378 {
1379 #ifdef HAVE_NO_ACL
1380         errno = ENOSYS;
1381         return -1;
1382 #else
1383         int result;
1384
1385         START_PROFILE(chmod_acl);
1386         result = chmod_acl(handle->conn, name, mode);
1387         END_PROFILE(chmod_acl);
1388         return result;
1389 #endif
1390 }
1391
1392 static int vfswrap_fchmod_acl(vfs_handle_struct *handle, files_struct *fsp, mode_t mode)
1393 {
1394 #ifdef HAVE_NO_ACL
1395         errno = ENOSYS;
1396         return -1;
1397 #else
1398         int result;
1399
1400         START_PROFILE(fchmod_acl);
1401         result = fchmod_acl(fsp, mode);
1402         END_PROFILE(fchmod_acl);
1403         return result;
1404 #endif
1405 }
1406
1407 static int vfswrap_sys_acl_get_entry(vfs_handle_struct *handle,  SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
1408 {
1409         return sys_acl_get_entry(theacl, entry_id, entry_p);
1410 }
1411
1412 static int vfswrap_sys_acl_get_tag_type(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
1413 {
1414         return sys_acl_get_tag_type(entry_d, tag_type_p);
1415 }
1416
1417 static int vfswrap_sys_acl_get_permset(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
1418 {
1419         return sys_acl_get_permset(entry_d, permset_p);
1420 }
1421
1422 static void * vfswrap_sys_acl_get_qualifier(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry_d)
1423 {
1424         return sys_acl_get_qualifier(entry_d);
1425 }
1426
1427 static SMB_ACL_T vfswrap_sys_acl_get_file(vfs_handle_struct *handle,  const char *path_p, SMB_ACL_TYPE_T type)
1428 {
1429         return sys_acl_get_file(handle, path_p, type);
1430 }
1431
1432 static SMB_ACL_T vfswrap_sys_acl_get_fd(vfs_handle_struct *handle, files_struct *fsp)
1433 {
1434         return sys_acl_get_fd(handle, fsp);
1435 }
1436
1437 static int vfswrap_sys_acl_clear_perms(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset)
1438 {
1439         return sys_acl_clear_perms(permset);
1440 }
1441
1442 static int vfswrap_sys_acl_add_perm(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1443 {
1444         return sys_acl_add_perm(permset, perm);
1445 }
1446
1447 static char * vfswrap_sys_acl_to_text(vfs_handle_struct *handle,  SMB_ACL_T theacl, ssize_t *plen)
1448 {
1449         return sys_acl_to_text(theacl, plen);
1450 }
1451
1452 static SMB_ACL_T vfswrap_sys_acl_init(vfs_handle_struct *handle,  int count)
1453 {
1454         return sys_acl_init(count);
1455 }
1456
1457 static int vfswrap_sys_acl_create_entry(vfs_handle_struct *handle,  SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1458 {
1459         return sys_acl_create_entry(pacl, pentry);
1460 }
1461
1462 static int vfswrap_sys_acl_set_tag_type(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
1463 {
1464         return sys_acl_set_tag_type(entry, tagtype);
1465 }
1466
1467 static int vfswrap_sys_acl_set_qualifier(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, void *qual)
1468 {
1469         return sys_acl_set_qualifier(entry, qual);
1470 }
1471
1472 static int vfswrap_sys_acl_set_permset(vfs_handle_struct *handle,  SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
1473 {
1474         return sys_acl_set_permset(entry, permset);
1475 }
1476
1477 static int vfswrap_sys_acl_valid(vfs_handle_struct *handle,  SMB_ACL_T theacl )
1478 {
1479         return sys_acl_valid(theacl );
1480 }
1481
1482 static int vfswrap_sys_acl_set_file(vfs_handle_struct *handle,  const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
1483 {
1484         return sys_acl_set_file(handle, name, acltype, theacl);
1485 }
1486
1487 static int vfswrap_sys_acl_set_fd(vfs_handle_struct *handle, files_struct *fsp, SMB_ACL_T theacl)
1488 {
1489         return sys_acl_set_fd(handle, fsp, theacl);
1490 }
1491
1492 static int vfswrap_sys_acl_delete_def_file(vfs_handle_struct *handle,  const char *path)
1493 {
1494         return sys_acl_delete_def_file(handle, path);
1495 }
1496
1497 static int vfswrap_sys_acl_get_perm(vfs_handle_struct *handle,  SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
1498 {
1499         return sys_acl_get_perm(permset, perm);
1500 }
1501
1502 static int vfswrap_sys_acl_free_text(vfs_handle_struct *handle,  char *text)
1503 {
1504         return sys_acl_free_text(text);
1505 }
1506
1507 static int vfswrap_sys_acl_free_acl(vfs_handle_struct *handle,  SMB_ACL_T posix_acl)
1508 {
1509         return sys_acl_free_acl(posix_acl);
1510 }
1511
1512 static int vfswrap_sys_acl_free_qualifier(vfs_handle_struct *handle,  void *qualifier, SMB_ACL_TAG_T tagtype)
1513 {
1514         return sys_acl_free_qualifier(qualifier, tagtype);
1515 }
1516
1517 /****************************************************************
1518  Extended attribute operations.
1519 *****************************************************************/
1520
1521 static ssize_t vfswrap_getxattr(struct vfs_handle_struct *handle,const char *path, const char *name, void *value, size_t size)
1522 {
1523         return sys_getxattr(path, name, value, size);
1524 }
1525
1526 static ssize_t vfswrap_lgetxattr(struct vfs_handle_struct *handle,const char *path, const char *name, void *value, size_t size)
1527 {
1528         return sys_lgetxattr(path, name, value, size);
1529 }
1530
1531 static ssize_t vfswrap_fgetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, void *value, size_t size)
1532 {
1533         return sys_fgetxattr(fsp->fh->fd, name, value, size);
1534 }
1535
1536 static ssize_t vfswrap_listxattr(struct vfs_handle_struct *handle, const char *path, char *list, size_t size)
1537 {
1538         return sys_listxattr(path, list, size);
1539 }
1540
1541 ssize_t vfswrap_llistxattr(struct vfs_handle_struct *handle, const char *path, char *list, size_t size)
1542 {
1543         return sys_llistxattr(path, list, size);
1544 }
1545
1546 ssize_t vfswrap_flistxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, char *list, size_t size)
1547 {
1548         return sys_flistxattr(fsp->fh->fd, list, size);
1549 }
1550
1551 static int vfswrap_removexattr(struct vfs_handle_struct *handle, const char *path, const char *name)
1552 {
1553         return sys_removexattr(path, name);
1554 }
1555
1556 static int vfswrap_lremovexattr(struct vfs_handle_struct *handle, const char *path, const char *name)
1557 {
1558         return sys_lremovexattr(path, name);
1559 }
1560
1561 static int vfswrap_fremovexattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name)
1562 {
1563         return sys_fremovexattr(fsp->fh->fd, name);
1564 }
1565
1566 static int vfswrap_setxattr(struct vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
1567 {
1568         return sys_setxattr(path, name, value, size, flags);
1569 }
1570
1571 static int vfswrap_lsetxattr(struct vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
1572 {
1573         return sys_lsetxattr(path, name, value, size, flags);
1574 }
1575
1576 static int vfswrap_fsetxattr(struct vfs_handle_struct *handle, struct files_struct *fsp, const char *name, const void *value, size_t size, int flags)
1577 {
1578         return sys_fsetxattr(fsp->fh->fd, name, value, size, flags);
1579 }
1580
1581 static int vfswrap_aio_read(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1582 {
1583         int ret;
1584         /*
1585          * aio_read must be done as root, because in the glibc aio
1586          * implementation the helper thread needs to be able to send a signal
1587          * to the main thread, even when it has done a seteuid() to a
1588          * different user.
1589          */
1590         become_root();
1591         ret = sys_aio_read(aiocb);
1592         unbecome_root();
1593         return ret;
1594 }
1595
1596 static int vfswrap_aio_write(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1597 {
1598         int ret;
1599         /*
1600          * aio_write must be done as root, because in the glibc aio
1601          * implementation the helper thread needs to be able to send a signal
1602          * to the main thread, even when it has done a seteuid() to a
1603          * different user.
1604          */
1605         become_root();
1606         ret = sys_aio_write(aiocb);
1607         unbecome_root();
1608         return ret;
1609 }
1610
1611 static ssize_t vfswrap_aio_return(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1612 {
1613         return sys_aio_return(aiocb);
1614 }
1615
1616 static int vfswrap_aio_cancel(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1617 {
1618         return sys_aio_cancel(fsp->fh->fd, aiocb);
1619 }
1620
1621 static int vfswrap_aio_error(struct vfs_handle_struct *handle, struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
1622 {
1623         return sys_aio_error(aiocb);
1624 }
1625
1626 static int vfswrap_aio_fsync(struct vfs_handle_struct *handle, struct files_struct *fsp, int op, SMB_STRUCT_AIOCB *aiocb)
1627 {
1628         return sys_aio_fsync(op, aiocb);
1629 }
1630
1631 static int vfswrap_aio_suspend(struct vfs_handle_struct *handle, struct files_struct *fsp, const SMB_STRUCT_AIOCB * const aiocb[], int n, const struct timespec *timeout)
1632 {
1633         return sys_aio_suspend(aiocb, n, timeout);
1634 }
1635
1636 static bool vfswrap_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
1637 {
1638         return false;
1639 }
1640
1641 static bool vfswrap_is_offline(struct vfs_handle_struct *handle, const char *path, SMB_STRUCT_STAT *sbuf)
1642 {
1643         if (ISDOT(path) || ISDOTDOT(path)) {
1644                 return false;
1645         }
1646
1647         if (!lp_dmapi_support(SNUM(handle->conn)) || !dmapi_have_session()) {
1648 #if defined(ENOTSUP)
1649                 errno = ENOTSUP;
1650 #endif
1651                 return false;
1652         }
1653
1654         return (dmapi_file_flags(path) & FILE_ATTRIBUTE_OFFLINE) != 0;
1655 }
1656
1657 static int vfswrap_set_offline(struct vfs_handle_struct *handle, const char *path)
1658 {
1659         /* We don't know how to set offline bit by default, needs to be overriden in the vfs modules */
1660 #if defined(ENOTSUP)
1661         errno = ENOTSUP;
1662 #endif
1663         return -1;
1664 }
1665
1666 static struct vfs_fn_pointers vfs_default_fns = {
1667         /* Disk operations */
1668
1669         .connect_fn = vfswrap_connect,
1670         .disconnect = vfswrap_disconnect,
1671         .disk_free = vfswrap_disk_free,
1672         .get_quota = vfswrap_get_quota,
1673         .set_quota = vfswrap_set_quota,
1674         .get_shadow_copy_data = vfswrap_get_shadow_copy_data,
1675         .statvfs = vfswrap_statvfs,
1676         .fs_capabilities = vfswrap_fs_capabilities,
1677
1678         /* Directory operations */
1679
1680         .opendir = vfswrap_opendir,
1681         .readdir = vfswrap_readdir,
1682         .seekdir = vfswrap_seekdir,
1683         .telldir = vfswrap_telldir,
1684         .rewind_dir = vfswrap_rewinddir,
1685         .mkdir = vfswrap_mkdir,
1686         .rmdir = vfswrap_rmdir,
1687         .closedir = vfswrap_closedir,
1688         .init_search_op = vfswrap_init_search_op,
1689
1690         /* File operations */
1691
1692         .open = vfswrap_open,
1693         .create_file = vfswrap_create_file,
1694         .close_fn = vfswrap_close,
1695         .vfs_read = vfswrap_read,
1696         .pread = vfswrap_pread,
1697         .write = vfswrap_write,
1698         .pwrite = vfswrap_pwrite,
1699         .lseek = vfswrap_lseek,
1700         .sendfile = vfswrap_sendfile,
1701         .recvfile = vfswrap_recvfile,
1702         .rename = vfswrap_rename,
1703         .fsync = vfswrap_fsync,
1704         .stat = vfswrap_stat,
1705         .fstat = vfswrap_fstat,
1706         .lstat = vfswrap_lstat,
1707         .get_alloc_size = vfswrap_get_alloc_size,
1708         .unlink = vfswrap_unlink,
1709         .chmod = vfswrap_chmod,
1710         .fchmod = vfswrap_fchmod,
1711         .chown = vfswrap_chown,
1712         .fchown = vfswrap_fchown,
1713         .lchown = vfswrap_lchown,
1714         .chdir = vfswrap_chdir,
1715         .getwd = vfswrap_getwd,
1716         .ntimes = vfswrap_ntimes,
1717         .ftruncate = vfswrap_ftruncate,
1718         .lock = vfswrap_lock,
1719         .kernel_flock = vfswrap_kernel_flock,
1720         .linux_setlease = vfswrap_linux_setlease,
1721         .getlock = vfswrap_getlock,
1722         .symlink = vfswrap_symlink,
1723         .vfs_readlink = vfswrap_readlink,
1724         .link = vfswrap_link,
1725         .mknod = vfswrap_mknod,
1726         .realpath = vfswrap_realpath,
1727         .notify_watch = vfswrap_notify_watch,
1728         .chflags = vfswrap_chflags,
1729         .file_id_create = vfswrap_file_id_create,
1730         .streaminfo = vfswrap_streaminfo,
1731         .get_real_filename = vfswrap_get_real_filename,
1732         .connectpath = vfswrap_connectpath,
1733         .brl_lock_windows = vfswrap_brl_lock_windows,
1734         .brl_unlock_windows = vfswrap_brl_unlock_windows,
1735         .brl_cancel_windows = vfswrap_brl_cancel_windows,
1736         .strict_lock = vfswrap_strict_lock,
1737         .strict_unlock = vfswrap_strict_unlock,
1738         .translate_name = vfswrap_translate_name,
1739
1740         /* NT ACL operations. */
1741
1742         .fget_nt_acl = vfswrap_fget_nt_acl,
1743         .get_nt_acl = vfswrap_get_nt_acl,
1744         .fset_nt_acl = vfswrap_fset_nt_acl,
1745
1746         /* POSIX ACL operations. */
1747
1748         .chmod_acl = vfswrap_chmod_acl,
1749         .fchmod_acl = vfswrap_fchmod_acl,
1750
1751         .sys_acl_get_entry = vfswrap_sys_acl_get_entry,
1752         .sys_acl_get_tag_type = vfswrap_sys_acl_get_tag_type,
1753         .sys_acl_get_permset = vfswrap_sys_acl_get_permset,
1754         .sys_acl_get_qualifier = vfswrap_sys_acl_get_qualifier,
1755         .sys_acl_get_file = vfswrap_sys_acl_get_file,
1756         .sys_acl_get_fd = vfswrap_sys_acl_get_fd,
1757         .sys_acl_clear_perms = vfswrap_sys_acl_clear_perms,
1758         .sys_acl_add_perm = vfswrap_sys_acl_add_perm,
1759         .sys_acl_to_text = vfswrap_sys_acl_to_text,
1760         .sys_acl_init = vfswrap_sys_acl_init,
1761         .sys_acl_create_entry = vfswrap_sys_acl_create_entry,
1762         .sys_acl_set_tag_type = vfswrap_sys_acl_set_tag_type,
1763         .sys_acl_set_qualifier = vfswrap_sys_acl_set_qualifier,
1764         .sys_acl_set_permset = vfswrap_sys_acl_set_permset,
1765         .sys_acl_valid = vfswrap_sys_acl_valid,
1766         .sys_acl_set_file = vfswrap_sys_acl_set_file,
1767         .sys_acl_set_fd = vfswrap_sys_acl_set_fd,
1768         .sys_acl_delete_def_file = vfswrap_sys_acl_delete_def_file,
1769         .sys_acl_get_perm = vfswrap_sys_acl_get_perm,
1770         .sys_acl_free_text = vfswrap_sys_acl_free_text,
1771         .sys_acl_free_acl = vfswrap_sys_acl_free_acl,
1772         .sys_acl_free_qualifier = vfswrap_sys_acl_free_qualifier,
1773
1774         /* EA operations. */
1775         .getxattr = vfswrap_getxattr,
1776         .lgetxattr = vfswrap_lgetxattr,
1777         .fgetxattr = vfswrap_fgetxattr,
1778         .listxattr = vfswrap_listxattr,
1779         .llistxattr = vfswrap_llistxattr,
1780         .flistxattr = vfswrap_flistxattr,
1781         .removexattr = vfswrap_removexattr,
1782         .lremovexattr = vfswrap_lremovexattr,
1783         .fremovexattr = vfswrap_fremovexattr,
1784         .setxattr = vfswrap_setxattr,
1785         .lsetxattr = vfswrap_lsetxattr,
1786         .fsetxattr = vfswrap_fsetxattr,
1787
1788         /* aio operations */
1789         .aio_read = vfswrap_aio_read,
1790         .aio_write = vfswrap_aio_write,
1791         .aio_return_fn = vfswrap_aio_return,
1792         .aio_cancel = vfswrap_aio_cancel,
1793         .aio_error_fn = vfswrap_aio_error,
1794         .aio_fsync = vfswrap_aio_fsync,
1795         .aio_suspend = vfswrap_aio_suspend,
1796         .aio_force = vfswrap_aio_force,
1797
1798         /* offline operations */
1799         .is_offline = vfswrap_is_offline,
1800         .set_offline = vfswrap_set_offline
1801 };
1802
1803 NTSTATUS vfs_default_init(void);
1804 NTSTATUS vfs_default_init(void)
1805 {
1806         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1807                                 DEFAULT_VFS_MODULE_NAME, &vfs_default_fns);
1808 }
1809
1810