s4 dns: Move record lookup to dns_utils.c
[Samba/gebeck_regimport.git] / source3 / smbd / vfs.c
blob6239ad479661b3a500ec0be8efae11c73e2994f1
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 VFS initialisation and support functions
5 Copyright (C) Tim Potter 1999
6 Copyright (C) Alexander Bokovoy 2002
7 Copyright (C) James Peach 2006
8 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
23 This work was sponsored by Optifacio Software Services, Inc.
26 #include "includes.h"
27 #include "system/filesys.h"
28 #include "smbd/smbd.h"
29 #include "smbd/globals.h"
30 #include "memcache.h"
31 #include "transfer_file.h"
32 #include "ntioctl.h"
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_VFS
37 static_decl_vfs;
39 struct vfs_init_function_entry {
40 char *name;
41 struct vfs_init_function_entry *prev, *next;
42 const struct vfs_fn_pointers *fns;
45 /****************************************************************************
46 maintain the list of available backends
47 ****************************************************************************/
49 static struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
51 struct vfs_init_function_entry *entry = backends;
53 DEBUG(10, ("vfs_find_backend_entry called for %s\n", name));
55 while(entry) {
56 if (strcmp(entry->name, name)==0) return entry;
57 entry = entry->next;
60 return NULL;
63 NTSTATUS smb_register_vfs(int version, const char *name,
64 const struct vfs_fn_pointers *fns)
66 struct vfs_init_function_entry *entry = backends;
68 if ((version != SMB_VFS_INTERFACE_VERSION)) {
69 DEBUG(0, ("Failed to register vfs module.\n"
70 "The module was compiled against SMB_VFS_INTERFACE_VERSION %d,\n"
71 "current SMB_VFS_INTERFACE_VERSION is %d.\n"
72 "Please recompile against the current Samba Version!\n",
73 version, SMB_VFS_INTERFACE_VERSION));
74 return NT_STATUS_OBJECT_TYPE_MISMATCH;
77 if (!name || !name[0]) {
78 DEBUG(0,("smb_register_vfs() called with NULL pointer or empty name!\n"));
79 return NT_STATUS_INVALID_PARAMETER;
82 if (vfs_find_backend_entry(name)) {
83 DEBUG(0,("VFS module %s already loaded!\n", name));
84 return NT_STATUS_OBJECT_NAME_COLLISION;
87 entry = SMB_XMALLOC_P(struct vfs_init_function_entry);
88 entry->name = smb_xstrdup(name);
89 entry->fns = fns;
91 DLIST_ADD(backends, entry);
92 DEBUG(5, ("Successfully added vfs backend '%s'\n", name));
93 return NT_STATUS_OK;
96 /****************************************************************************
97 initialise default vfs hooks
98 ****************************************************************************/
100 static void vfs_init_default(connection_struct *conn)
102 DEBUG(3, ("Initialising default vfs hooks\n"));
103 vfs_init_custom(conn, DEFAULT_VFS_MODULE_NAME);
106 /****************************************************************************
107 initialise custom vfs hooks
108 ****************************************************************************/
110 bool vfs_init_custom(connection_struct *conn, const char *vfs_object)
112 char *module_path = NULL;
113 char *module_name = NULL;
114 char *module_param = NULL, *p;
115 vfs_handle_struct *handle;
116 const struct vfs_init_function_entry *entry;
118 if (!conn||!vfs_object||!vfs_object[0]) {
119 DEBUG(0, ("vfs_init_custom() called with NULL pointer or "
120 "empty vfs_object!\n"));
121 return False;
124 if(!backends) {
125 static_init_vfs;
128 DEBUG(3, ("Initialising custom vfs hooks from [%s]\n", vfs_object));
130 module_path = smb_xstrdup(vfs_object);
132 p = strchr_m(module_path, ':');
134 if (p) {
135 *p = 0;
136 module_param = p+1;
137 trim_char(module_param, ' ', ' ');
140 trim_char(module_path, ' ', ' ');
142 module_name = smb_xstrdup(module_path);
144 if ((module_name[0] == '/') &&
145 (strcmp(module_path, DEFAULT_VFS_MODULE_NAME) != 0)) {
148 * Extract the module name from the path. Just use the base
149 * name of the last path component.
152 SAFE_FREE(module_name);
153 module_name = smb_xstrdup(strrchr_m(module_path, '/')+1);
155 p = strchr_m(module_name, '.');
157 if (p != NULL) {
158 *p = '\0';
162 /* First, try to load the module with the new module system */
163 entry = vfs_find_backend_entry(module_name);
164 if (!entry) {
165 NTSTATUS status;
167 DEBUG(5, ("vfs module [%s] not loaded - trying to load...\n",
168 vfs_object));
170 status = smb_probe_module("vfs", module_path);
171 if (!NT_STATUS_IS_OK(status)) {
172 DEBUG(0, ("error probing vfs module '%s': %s\n",
173 module_path, nt_errstr(status)));
174 goto fail;
177 entry = vfs_find_backend_entry(module_name);
178 if (!entry) {
179 DEBUG(0,("Can't find a vfs module [%s]\n",vfs_object));
180 goto fail;
184 DEBUGADD(5,("Successfully loaded vfs module [%s] with the new modules system\n", vfs_object));
186 handle = talloc_zero(conn, vfs_handle_struct);
187 if (!handle) {
188 DEBUG(0,("TALLOC_ZERO() failed!\n"));
189 goto fail;
191 handle->conn = conn;
192 handle->fns = entry->fns;
193 if (module_param) {
194 handle->param = talloc_strdup(conn, module_param);
196 DLIST_ADD(conn->vfs_handles, handle);
198 SAFE_FREE(module_path);
199 SAFE_FREE(module_name);
200 return True;
202 fail:
203 SAFE_FREE(module_path);
204 SAFE_FREE(module_name);
205 return False;
208 /*****************************************************************
209 Allow VFS modules to extend files_struct with VFS-specific state.
210 This will be ok for small numbers of extensions, but might need to
211 be refactored if it becomes more widely used.
212 ******************************************************************/
214 #define EXT_DATA_AREA(e) ((uint8 *)(e) + sizeof(struct vfs_fsp_data))
216 void *vfs_add_fsp_extension_notype(vfs_handle_struct *handle,
217 files_struct *fsp, size_t ext_size,
218 void (*destroy_fn)(void *p_data))
220 struct vfs_fsp_data *ext;
221 void * ext_data;
223 /* Prevent VFS modules adding multiple extensions. */
224 if ((ext_data = vfs_fetch_fsp_extension(handle, fsp))) {
225 return ext_data;
228 ext = (struct vfs_fsp_data *)TALLOC_ZERO(
229 handle->conn, sizeof(struct vfs_fsp_data) + ext_size);
230 if (ext == NULL) {
231 return NULL;
234 ext->owner = handle;
235 ext->next = fsp->vfs_extension;
236 ext->destroy = destroy_fn;
237 fsp->vfs_extension = ext;
238 return EXT_DATA_AREA(ext);
241 void vfs_remove_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
243 struct vfs_fsp_data *curr;
244 struct vfs_fsp_data *prev;
246 for (curr = fsp->vfs_extension, prev = NULL;
247 curr;
248 prev = curr, curr = curr->next) {
249 if (curr->owner == handle) {
250 if (prev) {
251 prev->next = curr->next;
252 } else {
253 fsp->vfs_extension = curr->next;
255 if (curr->destroy) {
256 curr->destroy(EXT_DATA_AREA(curr));
258 TALLOC_FREE(curr);
259 return;
264 void *vfs_memctx_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
266 struct vfs_fsp_data *head;
268 for (head = fsp->vfs_extension; head; head = head->next) {
269 if (head->owner == handle) {
270 return head;
274 return NULL;
277 void *vfs_fetch_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
279 struct vfs_fsp_data *head;
281 head = (struct vfs_fsp_data *)vfs_memctx_fsp_extension(handle, fsp);
282 if (head != NULL) {
283 return EXT_DATA_AREA(head);
286 return NULL;
289 #undef EXT_DATA_AREA
291 /*****************************************************************
292 Generic VFS init.
293 ******************************************************************/
295 bool smbd_vfs_init(connection_struct *conn)
297 const char **vfs_objects;
298 unsigned int i = 0;
299 int j = 0;
301 /* Normal share - initialise with disk access functions */
302 vfs_init_default(conn);
303 vfs_objects = lp_vfs_objects(SNUM(conn));
305 /* Override VFS functions if 'vfs object' was not specified*/
306 if (!vfs_objects || !vfs_objects[0])
307 return True;
309 for (i=0; vfs_objects[i] ;) {
310 i++;
313 for (j=i-1; j >= 0; j--) {
314 if (!vfs_init_custom(conn, vfs_objects[j])) {
315 DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed for %s\n", vfs_objects[j]));
316 return False;
319 return True;
322 /*******************************************************************
323 Check if a file exists in the vfs.
324 ********************************************************************/
326 NTSTATUS vfs_file_exist(connection_struct *conn, struct smb_filename *smb_fname)
328 /* Only return OK if stat was successful and S_ISREG */
329 if ((SMB_VFS_STAT(conn, smb_fname) != -1) &&
330 S_ISREG(smb_fname->st.st_ex_mode)) {
331 return NT_STATUS_OK;
334 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
337 /****************************************************************************
338 Read data from fsp on the vfs. (note: EINTR re-read differs from vfs_write_data)
339 ****************************************************************************/
341 ssize_t vfs_read_data(files_struct *fsp, char *buf, size_t byte_count)
343 size_t total=0;
345 while (total < byte_count)
347 ssize_t ret = SMB_VFS_READ(fsp, buf + total,
348 byte_count - total);
350 if (ret == 0) return total;
351 if (ret == -1) {
352 if (errno == EINTR)
353 continue;
354 else
355 return -1;
357 total += ret;
359 return (ssize_t)total;
362 ssize_t vfs_pread_data(files_struct *fsp, char *buf,
363 size_t byte_count, SMB_OFF_T offset)
365 size_t total=0;
367 while (total < byte_count)
369 ssize_t ret = SMB_VFS_PREAD(fsp, buf + total,
370 byte_count - total, offset + total);
372 if (ret == 0) return total;
373 if (ret == -1) {
374 if (errno == EINTR)
375 continue;
376 else
377 return -1;
379 total += ret;
381 return (ssize_t)total;
384 /****************************************************************************
385 Write data to a fd on the vfs.
386 ****************************************************************************/
388 ssize_t vfs_write_data(struct smb_request *req,
389 files_struct *fsp,
390 const char *buffer,
391 size_t N)
393 size_t total=0;
394 ssize_t ret;
396 if (req && req->unread_bytes) {
397 SMB_ASSERT(req->unread_bytes == N);
398 /* VFS_RECVFILE must drain the socket
399 * before returning. */
400 req->unread_bytes = 0;
401 return SMB_VFS_RECVFILE(req->sconn->sock,
402 fsp,
403 (SMB_OFF_T)-1,
407 while (total < N) {
408 ret = SMB_VFS_WRITE(fsp, buffer + total, N - total);
410 if (ret == -1)
411 return -1;
412 if (ret == 0)
413 return total;
415 total += ret;
417 return (ssize_t)total;
420 ssize_t vfs_pwrite_data(struct smb_request *req,
421 files_struct *fsp,
422 const char *buffer,
423 size_t N,
424 SMB_OFF_T offset)
426 size_t total=0;
427 ssize_t ret;
429 if (req && req->unread_bytes) {
430 SMB_ASSERT(req->unread_bytes == N);
431 /* VFS_RECVFILE must drain the socket
432 * before returning. */
433 req->unread_bytes = 0;
434 return SMB_VFS_RECVFILE(req->sconn->sock,
435 fsp,
436 offset,
440 while (total < N) {
441 ret = SMB_VFS_PWRITE(fsp, buffer + total, N - total,
442 offset + total);
444 if (ret == -1)
445 return -1;
446 if (ret == 0)
447 return total;
449 total += ret;
451 return (ssize_t)total;
453 /****************************************************************************
454 An allocate file space call using the vfs interface.
455 Allocates space for a file from a filedescriptor.
456 Returns 0 on success, -1 on failure.
457 ****************************************************************************/
459 int vfs_allocate_file_space(files_struct *fsp, uint64_t len)
461 int ret;
462 connection_struct *conn = fsp->conn;
463 uint64_t space_avail;
464 uint64_t bsize,dfree,dsize;
465 NTSTATUS status;
468 * Actually try and commit the space on disk....
471 DEBUG(10,("vfs_allocate_file_space: file %s, len %.0f\n",
472 fsp_str_dbg(fsp), (double)len));
474 if (((SMB_OFF_T)len) < 0) {
475 DEBUG(0,("vfs_allocate_file_space: %s negative len "
476 "requested.\n", fsp_str_dbg(fsp)));
477 errno = EINVAL;
478 return -1;
481 status = vfs_stat_fsp(fsp);
482 if (!NT_STATUS_IS_OK(status)) {
483 return -1;
486 if (len == (uint64_t)fsp->fsp_name->st.st_ex_size)
487 return 0;
489 if (len < (uint64_t)fsp->fsp_name->st.st_ex_size) {
490 /* Shrink - use ftruncate. */
492 DEBUG(10,("vfs_allocate_file_space: file %s, shrink. Current "
493 "size %.0f\n", fsp_str_dbg(fsp),
494 (double)fsp->fsp_name->st.st_ex_size));
496 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_ALLOC_SHRINK);
498 flush_write_cache(fsp, SIZECHANGE_FLUSH);
499 if ((ret = SMB_VFS_FTRUNCATE(fsp, (SMB_OFF_T)len)) != -1) {
500 set_filelen_write_cache(fsp, len);
503 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_ALLOC_SHRINK);
505 return ret;
508 if (!lp_strict_allocate(SNUM(fsp->conn)))
509 return 0;
511 /* Grow - we need to test if we have enough space. */
513 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_ALLOC_GROW);
515 /* See if we have a syscall that will allocate beyond end-of-file
516 without changing EOF. */
517 ret = SMB_VFS_FALLOCATE(fsp, VFS_FALLOCATE_KEEP_SIZE, 0, len);
519 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_ALLOC_GROW);
521 if (ret == 0) {
522 /* We changed the allocation size on disk, but not
523 EOF - exactly as required. We're done ! */
524 return 0;
527 len -= fsp->fsp_name->st.st_ex_size;
528 len /= 1024; /* Len is now number of 1k blocks needed. */
529 space_avail = get_dfree_info(conn, fsp->fsp_name->base_name, false,
530 &bsize, &dfree, &dsize);
531 if (space_avail == (uint64_t)-1) {
532 return -1;
535 DEBUG(10,("vfs_allocate_file_space: file %s, grow. Current size %.0f, "
536 "needed blocks = %.0f, space avail = %.0f\n",
537 fsp_str_dbg(fsp), (double)fsp->fsp_name->st.st_ex_size, (double)len,
538 (double)space_avail));
540 if (len > space_avail) {
541 errno = ENOSPC;
542 return -1;
545 return 0;
548 /****************************************************************************
549 A vfs set_filelen call.
550 set the length of a file from a filedescriptor.
551 Returns 0 on success, -1 on failure.
552 ****************************************************************************/
554 int vfs_set_filelen(files_struct *fsp, SMB_OFF_T len)
556 int ret;
558 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
560 DEBUG(10,("vfs_set_filelen: ftruncate %s to len %.0f\n",
561 fsp_str_dbg(fsp), (double)len));
562 flush_write_cache(fsp, SIZECHANGE_FLUSH);
563 if ((ret = SMB_VFS_FTRUNCATE(fsp, len)) != -1) {
564 set_filelen_write_cache(fsp, len);
565 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
566 FILE_NOTIFY_CHANGE_SIZE
567 | FILE_NOTIFY_CHANGE_ATTRIBUTES,
568 fsp->fsp_name->base_name);
571 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
573 return ret;
576 /****************************************************************************
577 A slow version of fallocate. Fallback code if SMB_VFS_FALLOCATE
578 fails. Needs to be outside of the default version of SMB_VFS_FALLOCATE
579 as this is also called from the default SMB_VFS_FTRUNCATE code.
580 Always extends the file size.
581 Returns 0 on success, errno on failure.
582 ****************************************************************************/
584 #define SPARSE_BUF_WRITE_SIZE (32*1024)
586 int vfs_slow_fallocate(files_struct *fsp, SMB_OFF_T offset, SMB_OFF_T len)
588 ssize_t pwrite_ret;
589 size_t total = 0;
591 if (!sparse_buf) {
592 sparse_buf = SMB_CALLOC_ARRAY(char, SPARSE_BUF_WRITE_SIZE);
593 if (!sparse_buf) {
594 errno = ENOMEM;
595 return ENOMEM;
599 while (total < len) {
600 size_t curr_write_size = MIN(SPARSE_BUF_WRITE_SIZE, (len - total));
602 pwrite_ret = SMB_VFS_PWRITE(fsp, sparse_buf, curr_write_size, offset + total);
603 if (pwrite_ret == -1) {
604 DEBUG(10,("vfs_slow_fallocate: SMB_VFS_PWRITE for file "
605 "%s failed with error %s\n",
606 fsp_str_dbg(fsp), strerror(errno)));
607 return errno;
609 total += pwrite_ret;
612 return 0;
615 /****************************************************************************
616 A vfs fill sparse call.
617 Writes zeros from the end of file to len, if len is greater than EOF.
618 Used only by strict_sync.
619 Returns 0 on success, -1 on failure.
620 ****************************************************************************/
622 int vfs_fill_sparse(files_struct *fsp, SMB_OFF_T len)
624 int ret;
625 NTSTATUS status;
626 SMB_OFF_T offset;
627 size_t num_to_write;
629 status = vfs_stat_fsp(fsp);
630 if (!NT_STATUS_IS_OK(status)) {
631 return -1;
634 if (len <= fsp->fsp_name->st.st_ex_size) {
635 return 0;
638 #ifdef S_ISFIFO
639 if (S_ISFIFO(fsp->fsp_name->st.st_ex_mode)) {
640 return 0;
642 #endif
644 DEBUG(10,("vfs_fill_sparse: write zeros in file %s from len %.0f to "
645 "len %.0f (%.0f bytes)\n", fsp_str_dbg(fsp),
646 (double)fsp->fsp_name->st.st_ex_size, (double)len,
647 (double)(len - fsp->fsp_name->st.st_ex_size)));
649 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_FILL_SPARSE);
651 flush_write_cache(fsp, SIZECHANGE_FLUSH);
653 offset = fsp->fsp_name->st.st_ex_size;
654 num_to_write = len - fsp->fsp_name->st.st_ex_size;
656 /* Only do this on non-stream file handles. */
657 if (fsp->base_fsp == NULL) {
658 /* for allocation try fallocate first. This can fail on some
659 * platforms e.g. when the filesystem doesn't support it and no
660 * emulation is being done by the libc (like on AIX with JFS1). In that
661 * case we do our own emulation. fallocate implementations can
662 * return ENOTSUP or EINVAL in cases like that. */
663 ret = SMB_VFS_FALLOCATE(fsp, VFS_FALLOCATE_EXTEND_SIZE,
664 offset, num_to_write);
665 if (ret == ENOSPC) {
666 errno = ENOSPC;
667 ret = -1;
668 goto out;
670 if (ret == 0) {
671 goto out;
673 DEBUG(10,("vfs_fill_sparse: SMB_VFS_FALLOCATE failed with "
674 "error %d. Falling back to slow manual allocation\n", ret));
677 ret = vfs_slow_fallocate(fsp, offset, num_to_write);
678 if (ret != 0) {
679 errno = ret;
680 ret = -1;
683 out:
685 if (ret == 0) {
686 set_filelen_write_cache(fsp, len);
689 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_FILL_SPARSE);
690 return ret;
693 /****************************************************************************
694 Transfer some data (n bytes) between two file_struct's.
695 ****************************************************************************/
697 static ssize_t vfs_read_fn(void *file, void *buf, size_t len)
699 struct files_struct *fsp = (struct files_struct *)file;
701 return SMB_VFS_READ(fsp, buf, len);
704 static ssize_t vfs_write_fn(void *file, const void *buf, size_t len)
706 struct files_struct *fsp = (struct files_struct *)file;
708 return SMB_VFS_WRITE(fsp, buf, len);
711 SMB_OFF_T vfs_transfer_file(files_struct *in, files_struct *out, SMB_OFF_T n)
713 return transfer_file_internal((void *)in, (void *)out, n,
714 vfs_read_fn, vfs_write_fn);
717 /*******************************************************************
718 A vfs_readdir wrapper which just returns the file name.
719 ********************************************************************/
721 const char *vfs_readdirname(connection_struct *conn, void *p,
722 SMB_STRUCT_STAT *sbuf, char **talloced)
724 SMB_STRUCT_DIRENT *ptr= NULL;
725 const char *dname;
726 char *translated;
727 NTSTATUS status;
729 if (!p)
730 return(NULL);
732 ptr = SMB_VFS_READDIR(conn, (SMB_STRUCT_DIR *)p, sbuf);
733 if (!ptr)
734 return(NULL);
736 dname = ptr->d_name;
739 #ifdef NEXT2
740 if (telldir(p) < 0)
741 return(NULL);
742 #endif
744 #ifdef HAVE_BROKEN_READDIR_NAME
745 /* using /usr/ucb/cc is BAD */
746 dname = dname - 2;
747 #endif
749 status = SMB_VFS_TRANSLATE_NAME(conn, dname, vfs_translate_to_windows,
750 talloc_tos(), &translated);
751 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
752 *talloced = NULL;
753 return dname;
755 *talloced = translated;
756 if (!NT_STATUS_IS_OK(status)) {
757 return NULL;
759 return translated;
762 /*******************************************************************
763 A wrapper for vfs_chdir().
764 ********************************************************************/
766 int vfs_ChDir(connection_struct *conn, const char *path)
768 int res;
770 if (!LastDir) {
771 LastDir = SMB_STRDUP("");
774 if (strcsequal(path,"."))
775 return(0);
777 if (*path == '/' && strcsequal(LastDir,path))
778 return(0);
780 DEBUG(4,("vfs_ChDir to %s\n",path));
782 res = SMB_VFS_CHDIR(conn,path);
783 if (!res) {
784 SAFE_FREE(LastDir);
785 LastDir = SMB_STRDUP(path);
787 return(res);
790 /*******************************************************************
791 Return the absolute current directory path - given a UNIX pathname.
792 Note that this path is returned in DOS format, not UNIX
793 format. Note this can be called with conn == NULL.
794 ********************************************************************/
796 char *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn)
798 char *current_dir = NULL;
799 char *result = NULL;
800 DATA_BLOB cache_value;
801 struct file_id key;
802 struct smb_filename *smb_fname_dot = NULL;
803 struct smb_filename *smb_fname_full = NULL;
804 NTSTATUS status;
806 if (!lp_getwd_cache()) {
807 goto nocache;
810 status = create_synthetic_smb_fname(ctx, ".", NULL, NULL,
811 &smb_fname_dot);
812 if (!NT_STATUS_IS_OK(status)) {
813 errno = map_errno_from_nt_status(status);
814 goto out;
817 if (SMB_VFS_STAT(conn, smb_fname_dot) == -1) {
819 * Known to fail for root: the directory may be NFS-mounted
820 * and exported with root_squash (so has no root access).
822 DEBUG(1,("vfs_GetWd: couldn't stat \".\" error %s "
823 "(NFS problem ?)\n", strerror(errno) ));
824 goto nocache;
827 key = vfs_file_id_from_sbuf(conn, &smb_fname_dot->st);
829 if (!memcache_lookup(smbd_memcache(), GETWD_CACHE,
830 data_blob_const(&key, sizeof(key)),
831 &cache_value)) {
832 goto nocache;
835 SMB_ASSERT((cache_value.length > 0)
836 && (cache_value.data[cache_value.length-1] == '\0'));
838 status = create_synthetic_smb_fname(ctx, (char *)cache_value.data,
839 NULL, NULL, &smb_fname_full);
840 if (!NT_STATUS_IS_OK(status)) {
841 errno = map_errno_from_nt_status(status);
842 goto out;
845 if ((SMB_VFS_STAT(conn, smb_fname_full) == 0) &&
846 (smb_fname_dot->st.st_ex_dev == smb_fname_full->st.st_ex_dev) &&
847 (smb_fname_dot->st.st_ex_ino == smb_fname_full->st.st_ex_ino) &&
848 (S_ISDIR(smb_fname_dot->st.st_ex_mode))) {
850 * Ok, we're done
852 result = talloc_strdup(ctx, smb_fname_full->base_name);
853 if (result == NULL) {
854 errno = ENOMEM;
856 goto out;
859 nocache:
862 * We don't have the information to hand so rely on traditional
863 * methods. The very slow getcwd, which spawns a process on some
864 * systems, or the not quite so bad getwd.
867 current_dir = SMB_VFS_GETWD(conn);
868 if (current_dir == NULL) {
869 DEBUG(0, ("vfs_GetWd: SMB_VFS_GETWD call failed: %s\n",
870 strerror(errno)));
871 goto out;
874 if (lp_getwd_cache() && VALID_STAT(smb_fname_dot->st)) {
875 key = vfs_file_id_from_sbuf(conn, &smb_fname_dot->st);
877 memcache_add(smbd_memcache(), GETWD_CACHE,
878 data_blob_const(&key, sizeof(key)),
879 data_blob_const(current_dir,
880 strlen(current_dir)+1));
883 result = talloc_strdup(ctx, current_dir);
884 if (result == NULL) {
885 errno = ENOMEM;
888 out:
889 TALLOC_FREE(smb_fname_dot);
890 TALLOC_FREE(smb_fname_full);
891 SAFE_FREE(current_dir);
892 return result;
895 /*******************************************************************
896 Reduce a file name, removing .. elements and checking that
897 it is below dir in the heirachy. This uses realpath.
898 ********************************************************************/
900 NTSTATUS check_reduced_name(connection_struct *conn, const char *fname)
902 char *resolved_name = NULL;
903 bool allow_symlinks = true;
904 bool allow_widelinks = false;
906 DEBUG(3,("check_reduced_name [%s] [%s]\n", fname, conn->connectpath));
908 resolved_name = SMB_VFS_REALPATH(conn,fname);
910 if (!resolved_name) {
911 switch (errno) {
912 case ENOTDIR:
913 DEBUG(3,("check_reduced_name: Component not a "
914 "directory in getting realpath for "
915 "%s\n", fname));
916 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
917 case ENOENT:
919 TALLOC_CTX *ctx = talloc_tos();
920 char *dir_name = NULL;
921 const char *last_component = NULL;
922 char *new_name = NULL;
924 /* Last component didn't exist.
925 Remove it and try and canonicalise
926 the directory name. */
927 if (!parent_dirname(ctx, fname,
928 &dir_name,
929 &last_component)) {
930 return NT_STATUS_NO_MEMORY;
933 resolved_name = SMB_VFS_REALPATH(conn,dir_name);
934 if (!resolved_name) {
935 NTSTATUS status = map_nt_error_from_unix(errno);
937 if (errno == ENOENT || errno == ENOTDIR) {
938 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
941 DEBUG(3,("check_reduce_name: "
942 "couldn't get realpath for "
943 "%s (%s)\n",
944 fname,
945 nt_errstr(status)));
946 return status;
948 new_name = talloc_asprintf(ctx,
949 "%s/%s",
950 resolved_name,
951 last_component);
952 if (!new_name) {
953 return NT_STATUS_NO_MEMORY;
955 SAFE_FREE(resolved_name);
956 resolved_name = SMB_STRDUP(new_name);
957 if (!resolved_name) {
958 return NT_STATUS_NO_MEMORY;
960 break;
962 default:
963 DEBUG(3,("check_reduced_name: couldn't get "
964 "realpath for %s\n", fname));
965 return map_nt_error_from_unix(errno);
969 DEBUG(10,("check_reduced_name realpath [%s] -> [%s]\n", fname,
970 resolved_name));
972 if (*resolved_name != '/') {
973 DEBUG(0,("check_reduced_name: realpath doesn't return "
974 "absolute paths !\n"));
975 SAFE_FREE(resolved_name);
976 return NT_STATUS_OBJECT_NAME_INVALID;
979 allow_widelinks = lp_widelinks(SNUM(conn));
980 allow_symlinks = lp_symlinks(SNUM(conn));
982 /* Common widelinks and symlinks checks. */
983 if (!allow_widelinks || !allow_symlinks) {
984 const char *conn_rootdir;
985 size_t rootdir_len;
987 conn_rootdir = SMB_VFS_CONNECTPATH(conn, fname);
988 if (conn_rootdir == NULL) {
989 DEBUG(2, ("check_reduced_name: Could not get "
990 "conn_rootdir\n"));
991 SAFE_FREE(resolved_name);
992 return NT_STATUS_ACCESS_DENIED;
995 rootdir_len = strlen(conn_rootdir);
996 if (strncmp(conn_rootdir, resolved_name,
997 rootdir_len) != 0) {
998 DEBUG(2, ("check_reduced_name: Bad access "
999 "attempt: %s is a symlink outside the "
1000 "share path\n", fname));
1001 DEBUGADD(2, ("conn_rootdir =%s\n", conn_rootdir));
1002 DEBUGADD(2, ("resolved_name=%s\n", resolved_name));
1003 SAFE_FREE(resolved_name);
1004 return NT_STATUS_ACCESS_DENIED;
1007 /* Extra checks if all symlinks are disallowed. */
1008 if (!allow_symlinks) {
1009 /* fname can't have changed in resolved_path. */
1010 const char *p = &resolved_name[rootdir_len];
1012 /* *p can be '\0' if fname was "." */
1013 if (*p == '\0' && ISDOT(fname)) {
1014 goto out;
1017 if (*p != '/') {
1018 DEBUG(2, ("check_reduced_name: logic error (%c) "
1019 "in resolved_name: %s\n",
1021 fname));
1022 SAFE_FREE(resolved_name);
1023 return NT_STATUS_ACCESS_DENIED;
1026 p++;
1027 if (strcmp(fname, p)!=0) {
1028 DEBUG(2, ("check_reduced_name: Bad access "
1029 "attempt: %s is a symlink\n",
1030 fname));
1031 SAFE_FREE(resolved_name);
1032 return NT_STATUS_ACCESS_DENIED;
1037 out:
1039 DEBUG(3,("check_reduced_name: %s reduced to %s\n", fname,
1040 resolved_name));
1041 SAFE_FREE(resolved_name);
1042 return NT_STATUS_OK;
1046 * XXX: This is temporary and there should be no callers of this once
1047 * smb_filename is plumbed through all path based operations.
1049 int vfs_stat_smb_fname(struct connection_struct *conn, const char *fname,
1050 SMB_STRUCT_STAT *psbuf)
1052 struct smb_filename *smb_fname = NULL;
1053 NTSTATUS status;
1054 int ret;
1056 status = create_synthetic_smb_fname_split(talloc_tos(), fname, NULL,
1057 &smb_fname);
1058 if (!NT_STATUS_IS_OK(status)) {
1059 errno = map_errno_from_nt_status(status);
1060 return -1;
1063 if (lp_posix_pathnames()) {
1064 ret = SMB_VFS_LSTAT(conn, smb_fname);
1065 } else {
1066 ret = SMB_VFS_STAT(conn, smb_fname);
1069 if (ret != -1) {
1070 *psbuf = smb_fname->st;
1073 TALLOC_FREE(smb_fname);
1074 return ret;
1078 * XXX: This is temporary and there should be no callers of this once
1079 * smb_filename is plumbed through all path based operations.
1081 int vfs_lstat_smb_fname(struct connection_struct *conn, const char *fname,
1082 SMB_STRUCT_STAT *psbuf)
1084 struct smb_filename *smb_fname = NULL;
1085 NTSTATUS status;
1086 int ret;
1088 status = create_synthetic_smb_fname_split(talloc_tos(), fname, NULL,
1089 &smb_fname);
1090 if (!NT_STATUS_IS_OK(status)) {
1091 errno = map_errno_from_nt_status(status);
1092 return -1;
1095 ret = SMB_VFS_LSTAT(conn, smb_fname);
1096 if (ret != -1) {
1097 *psbuf = smb_fname->st;
1100 TALLOC_FREE(smb_fname);
1101 return ret;
1105 * Ensure LSTAT is called for POSIX paths.
1108 NTSTATUS vfs_stat_fsp(files_struct *fsp)
1110 int ret;
1112 if(fsp->fh->fd == -1) {
1113 if (fsp->posix_open) {
1114 ret = SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
1115 } else {
1116 ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
1118 if (ret == -1) {
1119 return map_nt_error_from_unix(errno);
1121 } else {
1122 if(SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) {
1123 return map_nt_error_from_unix(errno);
1126 return NT_STATUS_OK;
1130 * Initialize num_streams and streams, then call VFS op streaminfo
1132 NTSTATUS vfs_streaminfo(connection_struct *conn,
1133 struct files_struct *fsp,
1134 const char *fname,
1135 TALLOC_CTX *mem_ctx,
1136 unsigned int *num_streams,
1137 struct stream_struct **streams)
1139 *num_streams = 0;
1140 *streams = NULL;
1141 return SMB_VFS_STREAMINFO(conn, fsp, fname, mem_ctx, num_streams, streams);
1145 generate a file_id from a stat structure
1147 struct file_id vfs_file_id_from_sbuf(connection_struct *conn, const SMB_STRUCT_STAT *sbuf)
1149 return SMB_VFS_FILE_ID_CREATE(conn, sbuf);
1152 int smb_vfs_call_connect(struct vfs_handle_struct *handle,
1153 const char *service, const char *user)
1155 VFS_FIND(connect);
1156 return handle->fns->connect_fn(handle, service, user);
1159 void smb_vfs_call_disconnect(struct vfs_handle_struct *handle)
1161 VFS_FIND(disconnect);
1162 handle->fns->disconnect_fn(handle);
1165 uint64_t smb_vfs_call_disk_free(struct vfs_handle_struct *handle,
1166 const char *path, bool small_query,
1167 uint64_t *bsize, uint64_t *dfree,
1168 uint64_t *dsize)
1170 VFS_FIND(disk_free);
1171 return handle->fns->disk_free_fn(handle, path, small_query, bsize,
1172 dfree, dsize);
1175 int smb_vfs_call_get_quota(struct vfs_handle_struct *handle,
1176 enum SMB_QUOTA_TYPE qtype, unid_t id,
1177 SMB_DISK_QUOTA *qt)
1179 VFS_FIND(get_quota);
1180 return handle->fns->get_quota_fn(handle, qtype, id, qt);
1183 int smb_vfs_call_set_quota(struct vfs_handle_struct *handle,
1184 enum SMB_QUOTA_TYPE qtype, unid_t id,
1185 SMB_DISK_QUOTA *qt)
1187 VFS_FIND(set_quota);
1188 return handle->fns->set_quota_fn(handle, qtype, id, qt);
1191 int smb_vfs_call_get_shadow_copy_data(struct vfs_handle_struct *handle,
1192 struct files_struct *fsp,
1193 struct shadow_copy_data *shadow_copy_data,
1194 bool labels)
1196 VFS_FIND(get_shadow_copy_data);
1197 return handle->fns->get_shadow_copy_data_fn(handle, fsp,
1198 shadow_copy_data,
1199 labels);
1201 int smb_vfs_call_statvfs(struct vfs_handle_struct *handle, const char *path,
1202 struct vfs_statvfs_struct *statbuf)
1204 VFS_FIND(statvfs);
1205 return handle->fns->statvfs_fn(handle, path, statbuf);
1208 uint32_t smb_vfs_call_fs_capabilities(struct vfs_handle_struct *handle,
1209 enum timestamp_set_resolution *p_ts_res)
1211 VFS_FIND(fs_capabilities);
1212 return handle->fns->fs_capabilities_fn(handle, p_ts_res);
1215 NTSTATUS smb_vfs_call_get_dfs_referrals(struct vfs_handle_struct *handle,
1216 struct dfs_GetDFSReferral *r)
1218 VFS_FIND(get_dfs_referrals);
1219 return handle->fns->get_dfs_referrals_fn(handle, r);
1222 SMB_STRUCT_DIR *smb_vfs_call_opendir(struct vfs_handle_struct *handle,
1223 const char *fname, const char *mask,
1224 uint32 attributes)
1226 VFS_FIND(opendir);
1227 return handle->fns->opendir_fn(handle, fname, mask, attributes);
1230 SMB_STRUCT_DIR *smb_vfs_call_fdopendir(struct vfs_handle_struct *handle,
1231 struct files_struct *fsp,
1232 const char *mask,
1233 uint32 attributes)
1235 VFS_FIND(fdopendir);
1236 return handle->fns->fdopendir_fn(handle, fsp, mask, attributes);
1239 SMB_STRUCT_DIRENT *smb_vfs_call_readdir(struct vfs_handle_struct *handle,
1240 SMB_STRUCT_DIR *dirp,
1241 SMB_STRUCT_STAT *sbuf)
1243 VFS_FIND(readdir);
1244 return handle->fns->readdir_fn(handle, dirp, sbuf);
1247 void smb_vfs_call_seekdir(struct vfs_handle_struct *handle,
1248 SMB_STRUCT_DIR *dirp, long offset)
1250 VFS_FIND(seekdir);
1251 handle->fns->seekdir_fn(handle, dirp, offset);
1254 long smb_vfs_call_telldir(struct vfs_handle_struct *handle,
1255 SMB_STRUCT_DIR *dirp)
1257 VFS_FIND(telldir);
1258 return handle->fns->telldir_fn(handle, dirp);
1261 void smb_vfs_call_rewind_dir(struct vfs_handle_struct *handle,
1262 SMB_STRUCT_DIR *dirp)
1264 VFS_FIND(rewind_dir);
1265 handle->fns->rewind_dir_fn(handle, dirp);
1268 int smb_vfs_call_mkdir(struct vfs_handle_struct *handle, const char *path,
1269 mode_t mode)
1271 VFS_FIND(mkdir);
1272 return handle->fns->mkdir_fn(handle, path, mode);
1275 int smb_vfs_call_rmdir(struct vfs_handle_struct *handle, const char *path)
1277 VFS_FIND(rmdir);
1278 return handle->fns->rmdir_fn(handle, path);
1281 int smb_vfs_call_closedir(struct vfs_handle_struct *handle,
1282 SMB_STRUCT_DIR *dir)
1284 VFS_FIND(closedir);
1285 return handle->fns->closedir_fn(handle, dir);
1288 void smb_vfs_call_init_search_op(struct vfs_handle_struct *handle,
1289 SMB_STRUCT_DIR *dirp)
1291 VFS_FIND(init_search_op);
1292 handle->fns->init_search_op_fn(handle, dirp);
1295 int smb_vfs_call_open(struct vfs_handle_struct *handle,
1296 struct smb_filename *smb_fname, struct files_struct *fsp,
1297 int flags, mode_t mode)
1299 VFS_FIND(open);
1300 return handle->fns->open_fn(handle, smb_fname, fsp, flags, mode);
1303 NTSTATUS smb_vfs_call_create_file(struct vfs_handle_struct *handle,
1304 struct smb_request *req,
1305 uint16_t root_dir_fid,
1306 struct smb_filename *smb_fname,
1307 uint32_t access_mask,
1308 uint32_t share_access,
1309 uint32_t create_disposition,
1310 uint32_t create_options,
1311 uint32_t file_attributes,
1312 uint32_t oplock_request,
1313 uint64_t allocation_size,
1314 uint32_t private_flags,
1315 struct security_descriptor *sd,
1316 struct ea_list *ea_list,
1317 files_struct **result,
1318 int *pinfo)
1320 VFS_FIND(create_file);
1321 return handle->fns->create_file_fn(
1322 handle, req, root_dir_fid, smb_fname, access_mask,
1323 share_access, create_disposition, create_options,
1324 file_attributes, oplock_request, allocation_size,
1325 private_flags, sd, ea_list,
1326 result, pinfo);
1329 int smb_vfs_call_close(struct vfs_handle_struct *handle,
1330 struct files_struct *fsp)
1332 VFS_FIND(close);
1333 return handle->fns->close_fn(handle, fsp);
1336 ssize_t smb_vfs_call_read(struct vfs_handle_struct *handle,
1337 struct files_struct *fsp, void *data, size_t n)
1339 VFS_FIND(read);
1340 return handle->fns->read_fn(handle, fsp, data, n);
1343 ssize_t smb_vfs_call_pread(struct vfs_handle_struct *handle,
1344 struct files_struct *fsp, void *data, size_t n,
1345 SMB_OFF_T offset)
1347 VFS_FIND(pread);
1348 return handle->fns->pread_fn(handle, fsp, data, n, offset);
1351 ssize_t smb_vfs_call_write(struct vfs_handle_struct *handle,
1352 struct files_struct *fsp, const void *data,
1353 size_t n)
1355 VFS_FIND(write);
1356 return handle->fns->write_fn(handle, fsp, data, n);
1359 ssize_t smb_vfs_call_pwrite(struct vfs_handle_struct *handle,
1360 struct files_struct *fsp, const void *data,
1361 size_t n, SMB_OFF_T offset)
1363 VFS_FIND(pwrite);
1364 return handle->fns->pwrite_fn(handle, fsp, data, n, offset);
1367 SMB_OFF_T smb_vfs_call_lseek(struct vfs_handle_struct *handle,
1368 struct files_struct *fsp, SMB_OFF_T offset,
1369 int whence)
1371 VFS_FIND(lseek);
1372 return handle->fns->lseek_fn(handle, fsp, offset, whence);
1375 ssize_t smb_vfs_call_sendfile(struct vfs_handle_struct *handle, int tofd,
1376 files_struct *fromfsp, const DATA_BLOB *header,
1377 SMB_OFF_T offset, size_t count)
1379 VFS_FIND(sendfile);
1380 return handle->fns->sendfile_fn(handle, tofd, fromfsp, header, offset,
1381 count);
1384 ssize_t smb_vfs_call_recvfile(struct vfs_handle_struct *handle, int fromfd,
1385 files_struct *tofsp, SMB_OFF_T offset,
1386 size_t count)
1388 VFS_FIND(recvfile);
1389 return handle->fns->recvfile_fn(handle, fromfd, tofsp, offset, count);
1392 int smb_vfs_call_rename(struct vfs_handle_struct *handle,
1393 const struct smb_filename *smb_fname_src,
1394 const struct smb_filename *smb_fname_dst)
1396 VFS_FIND(rename);
1397 return handle->fns->rename_fn(handle, smb_fname_src, smb_fname_dst);
1400 int smb_vfs_call_fsync(struct vfs_handle_struct *handle,
1401 struct files_struct *fsp)
1403 VFS_FIND(fsync);
1404 return handle->fns->fsync_fn(handle, fsp);
1407 int smb_vfs_call_stat(struct vfs_handle_struct *handle,
1408 struct smb_filename *smb_fname)
1410 VFS_FIND(stat);
1411 return handle->fns->stat_fn(handle, smb_fname);
1414 int smb_vfs_call_fstat(struct vfs_handle_struct *handle,
1415 struct files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1417 VFS_FIND(fstat);
1418 return handle->fns->fstat_fn(handle, fsp, sbuf);
1421 int smb_vfs_call_lstat(struct vfs_handle_struct *handle,
1422 struct smb_filename *smb_filename)
1424 VFS_FIND(lstat);
1425 return handle->fns->lstat_fn(handle, smb_filename);
1428 uint64_t smb_vfs_call_get_alloc_size(struct vfs_handle_struct *handle,
1429 struct files_struct *fsp,
1430 const SMB_STRUCT_STAT *sbuf)
1432 VFS_FIND(get_alloc_size);
1433 return handle->fns->get_alloc_size_fn(handle, fsp, sbuf);
1436 int smb_vfs_call_unlink(struct vfs_handle_struct *handle,
1437 const struct smb_filename *smb_fname)
1439 VFS_FIND(unlink);
1440 return handle->fns->unlink_fn(handle, smb_fname);
1443 int smb_vfs_call_chmod(struct vfs_handle_struct *handle, const char *path,
1444 mode_t mode)
1446 VFS_FIND(chmod);
1447 return handle->fns->chmod_fn(handle, path, mode);
1450 int smb_vfs_call_fchmod(struct vfs_handle_struct *handle,
1451 struct files_struct *fsp, mode_t mode)
1453 VFS_FIND(fchmod);
1454 return handle->fns->fchmod_fn(handle, fsp, mode);
1457 int smb_vfs_call_chown(struct vfs_handle_struct *handle, const char *path,
1458 uid_t uid, gid_t gid)
1460 VFS_FIND(chown);
1461 return handle->fns->chown_fn(handle, path, uid, gid);
1464 int smb_vfs_call_fchown(struct vfs_handle_struct *handle,
1465 struct files_struct *fsp, uid_t uid, gid_t gid)
1467 VFS_FIND(fchown);
1468 return handle->fns->fchown_fn(handle, fsp, uid, gid);
1471 int smb_vfs_call_lchown(struct vfs_handle_struct *handle, const char *path,
1472 uid_t uid, gid_t gid)
1474 VFS_FIND(lchown);
1475 return handle->fns->lchown_fn(handle, path, uid, gid);
1478 NTSTATUS vfs_chown_fsp(files_struct *fsp, uid_t uid, gid_t gid)
1480 int ret;
1481 bool as_root = false;
1482 const char *path;
1483 char *saved_dir = NULL;
1484 char *parent_dir = NULL;
1485 NTSTATUS status;
1487 if (fsp->fh->fd != -1) {
1488 /* Try fchown. */
1489 ret = SMB_VFS_FCHOWN(fsp, uid, gid);
1490 if (ret == 0) {
1491 return NT_STATUS_OK;
1493 if (ret == -1 && errno != ENOSYS) {
1494 return map_nt_error_from_unix(errno);
1498 as_root = (geteuid() == 0);
1500 if (as_root) {
1502 * We are being asked to chown as root. Make
1503 * sure we chdir() into the path to pin it,
1504 * and always act using lchown to ensure we
1505 * don't deref any symbolic links.
1507 const char *final_component = NULL;
1508 struct smb_filename local_fname;
1510 saved_dir = vfs_GetWd(talloc_tos(),fsp->conn);
1511 if (!saved_dir) {
1512 status = map_nt_error_from_unix(errno);
1513 DEBUG(0,("vfs_chown_fsp: failed to get "
1514 "current working directory. Error was %s\n",
1515 strerror(errno)));
1516 return status;
1519 if (!parent_dirname(talloc_tos(),
1520 fsp->fsp_name->base_name,
1521 &parent_dir,
1522 &final_component)) {
1523 return NT_STATUS_NO_MEMORY;
1526 /* cd into the parent dir to pin it. */
1527 ret = vfs_ChDir(fsp->conn, parent_dir);
1528 if (ret == -1) {
1529 return map_nt_error_from_unix(errno);
1532 ZERO_STRUCT(local_fname);
1533 local_fname.base_name = discard_const_p(char, final_component);
1535 /* Must use lstat here. */
1536 ret = SMB_VFS_LSTAT(fsp->conn, &local_fname);
1537 if (ret == -1) {
1538 status = map_nt_error_from_unix(errno);
1539 goto out;
1542 /* Ensure it matches the fsp stat. */
1543 if (!check_same_stat(&local_fname.st, &fsp->fsp_name->st)) {
1544 status = NT_STATUS_ACCESS_DENIED;
1545 goto out;
1547 path = final_component;
1548 } else {
1549 path = fsp->fsp_name->base_name;
1552 if (fsp->posix_open || as_root) {
1553 ret = SMB_VFS_LCHOWN(fsp->conn,
1554 path,
1555 uid, gid);
1556 } else {
1557 ret = SMB_VFS_CHOWN(fsp->conn,
1558 path,
1559 uid, gid);
1562 if (ret == 0) {
1563 status = NT_STATUS_OK;
1564 } else {
1565 status = map_nt_error_from_unix(errno);
1568 out:
1570 if (as_root) {
1571 vfs_ChDir(fsp->conn,saved_dir);
1572 TALLOC_FREE(saved_dir);
1573 TALLOC_FREE(parent_dir);
1575 return status;
1578 int smb_vfs_call_chdir(struct vfs_handle_struct *handle, const char *path)
1580 VFS_FIND(chdir);
1581 return handle->fns->chdir_fn(handle, path);
1584 char *smb_vfs_call_getwd(struct vfs_handle_struct *handle)
1586 VFS_FIND(getwd);
1587 return handle->fns->getwd_fn(handle);
1590 int smb_vfs_call_ntimes(struct vfs_handle_struct *handle,
1591 const struct smb_filename *smb_fname,
1592 struct smb_file_time *ft)
1594 VFS_FIND(ntimes);
1595 return handle->fns->ntimes_fn(handle, smb_fname, ft);
1598 int smb_vfs_call_ftruncate(struct vfs_handle_struct *handle,
1599 struct files_struct *fsp, SMB_OFF_T offset)
1601 VFS_FIND(ftruncate);
1602 return handle->fns->ftruncate_fn(handle, fsp, offset);
1605 int smb_vfs_call_fallocate(struct vfs_handle_struct *handle,
1606 struct files_struct *fsp,
1607 enum vfs_fallocate_mode mode,
1608 SMB_OFF_T offset,
1609 SMB_OFF_T len)
1611 VFS_FIND(fallocate);
1612 return handle->fns->fallocate_fn(handle, fsp, mode, offset, len);
1615 int smb_vfs_call_kernel_flock(struct vfs_handle_struct *handle,
1616 struct files_struct *fsp, uint32 share_mode,
1617 uint32_t access_mask)
1619 VFS_FIND(kernel_flock);
1620 return handle->fns->kernel_flock_fn(handle, fsp, share_mode,
1621 access_mask);
1624 int smb_vfs_call_linux_setlease(struct vfs_handle_struct *handle,
1625 struct files_struct *fsp, int leasetype)
1627 VFS_FIND(linux_setlease);
1628 return handle->fns->linux_setlease_fn(handle, fsp, leasetype);
1631 int smb_vfs_call_symlink(struct vfs_handle_struct *handle, const char *oldpath,
1632 const char *newpath)
1634 VFS_FIND(symlink);
1635 return handle->fns->symlink_fn(handle, oldpath, newpath);
1638 int smb_vfs_call_readlink(struct vfs_handle_struct *handle,
1639 const char *path, char *buf, size_t bufsiz)
1641 VFS_FIND(readlink);
1642 return handle->fns->readlink_fn(handle, path, buf, bufsiz);
1645 int smb_vfs_call_link(struct vfs_handle_struct *handle, const char *oldpath,
1646 const char *newpath)
1648 VFS_FIND(link);
1649 return handle->fns->link_fn(handle, oldpath, newpath);
1652 int smb_vfs_call_mknod(struct vfs_handle_struct *handle, const char *path,
1653 mode_t mode, SMB_DEV_T dev)
1655 VFS_FIND(mknod);
1656 return handle->fns->mknod_fn(handle, path, mode, dev);
1659 char *smb_vfs_call_realpath(struct vfs_handle_struct *handle, const char *path)
1661 VFS_FIND(realpath);
1662 return handle->fns->realpath_fn(handle, path);
1665 NTSTATUS smb_vfs_call_notify_watch(struct vfs_handle_struct *handle,
1666 struct sys_notify_context *ctx,
1667 struct notify_entry *e,
1668 void (*callback)(struct sys_notify_context *ctx,
1669 void *private_data,
1670 struct notify_event *ev),
1671 void *private_data, void *handle_p)
1673 VFS_FIND(notify_watch);
1674 return handle->fns->notify_watch_fn(handle, ctx, e, callback,
1675 private_data, handle_p);
1678 int smb_vfs_call_chflags(struct vfs_handle_struct *handle, const char *path,
1679 unsigned int flags)
1681 VFS_FIND(chflags);
1682 return handle->fns->chflags_fn(handle, path, flags);
1685 struct file_id smb_vfs_call_file_id_create(struct vfs_handle_struct *handle,
1686 const SMB_STRUCT_STAT *sbuf)
1688 VFS_FIND(file_id_create);
1689 return handle->fns->file_id_create_fn(handle, sbuf);
1692 NTSTATUS smb_vfs_call_streaminfo(struct vfs_handle_struct *handle,
1693 struct files_struct *fsp,
1694 const char *fname,
1695 TALLOC_CTX *mem_ctx,
1696 unsigned int *num_streams,
1697 struct stream_struct **streams)
1699 VFS_FIND(streaminfo);
1700 return handle->fns->streaminfo_fn(handle, fsp, fname, mem_ctx,
1701 num_streams, streams);
1704 int smb_vfs_call_get_real_filename(struct vfs_handle_struct *handle,
1705 const char *path, const char *name,
1706 TALLOC_CTX *mem_ctx, char **found_name)
1708 VFS_FIND(get_real_filename);
1709 return handle->fns->get_real_filename_fn(handle, path, name, mem_ctx,
1710 found_name);
1713 const char *smb_vfs_call_connectpath(struct vfs_handle_struct *handle,
1714 const char *filename)
1716 VFS_FIND(connectpath);
1717 return handle->fns->connectpath_fn(handle, filename);
1720 bool smb_vfs_call_strict_lock(struct vfs_handle_struct *handle,
1721 struct files_struct *fsp,
1722 struct lock_struct *plock)
1724 VFS_FIND(strict_lock);
1725 return handle->fns->strict_lock_fn(handle, fsp, plock);
1728 void smb_vfs_call_strict_unlock(struct vfs_handle_struct *handle,
1729 struct files_struct *fsp,
1730 struct lock_struct *plock)
1732 VFS_FIND(strict_unlock);
1733 handle->fns->strict_unlock_fn(handle, fsp, plock);
1736 NTSTATUS smb_vfs_call_translate_name(struct vfs_handle_struct *handle,
1737 const char *name,
1738 enum vfs_translate_direction direction,
1739 TALLOC_CTX *mem_ctx,
1740 char **mapped_name)
1742 VFS_FIND(translate_name);
1743 return handle->fns->translate_name_fn(handle, name, direction, mem_ctx,
1744 mapped_name);
1747 NTSTATUS smb_vfs_call_fsctl(struct vfs_handle_struct *handle,
1748 struct files_struct *fsp,
1749 TALLOC_CTX *ctx,
1750 uint32_t function,
1751 uint16_t req_flags,
1752 const uint8_t *in_data,
1753 uint32_t in_len,
1754 uint8_t **out_data,
1755 uint32_t max_out_len,
1756 uint32_t *out_len)
1758 VFS_FIND(fsctl);
1759 return handle->fns->fsctl_fn(handle, fsp, ctx, function, req_flags,
1760 in_data, in_len, out_data, max_out_len,
1761 out_len);
1764 NTSTATUS smb_vfs_call_fget_nt_acl(struct vfs_handle_struct *handle,
1765 struct files_struct *fsp,
1766 uint32 security_info,
1767 struct security_descriptor **ppdesc)
1769 VFS_FIND(fget_nt_acl);
1770 return handle->fns->fget_nt_acl_fn(handle, fsp, security_info,
1771 ppdesc);
1774 NTSTATUS smb_vfs_call_get_nt_acl(struct vfs_handle_struct *handle,
1775 const char *name,
1776 uint32 security_info,
1777 struct security_descriptor **ppdesc)
1779 VFS_FIND(get_nt_acl);
1780 return handle->fns->get_nt_acl_fn(handle, name, security_info, ppdesc);
1783 NTSTATUS smb_vfs_call_fset_nt_acl(struct vfs_handle_struct *handle,
1784 struct files_struct *fsp,
1785 uint32 security_info_sent,
1786 const struct security_descriptor *psd)
1788 VFS_FIND(fset_nt_acl);
1789 return handle->fns->fset_nt_acl_fn(handle, fsp, security_info_sent,
1790 psd);
1793 int smb_vfs_call_chmod_acl(struct vfs_handle_struct *handle, const char *name,
1794 mode_t mode)
1796 VFS_FIND(chmod_acl);
1797 return handle->fns->chmod_acl_fn(handle, name, mode);
1800 int smb_vfs_call_fchmod_acl(struct vfs_handle_struct *handle,
1801 struct files_struct *fsp, mode_t mode)
1803 VFS_FIND(fchmod_acl);
1804 return handle->fns->fchmod_acl_fn(handle, fsp, mode);
1807 int smb_vfs_call_sys_acl_get_entry(struct vfs_handle_struct *handle,
1808 SMB_ACL_T theacl, int entry_id,
1809 SMB_ACL_ENTRY_T *entry_p)
1811 VFS_FIND(sys_acl_get_entry);
1812 return handle->fns->sys_acl_get_entry_fn(handle, theacl, entry_id,
1813 entry_p);
1816 int smb_vfs_call_sys_acl_get_tag_type(struct vfs_handle_struct *handle,
1817 SMB_ACL_ENTRY_T entry_d,
1818 SMB_ACL_TAG_T *tag_type_p)
1820 VFS_FIND(sys_acl_get_tag_type);
1821 return handle->fns->sys_acl_get_tag_type_fn(handle, entry_d,
1822 tag_type_p);
1825 int smb_vfs_call_sys_acl_get_permset(struct vfs_handle_struct *handle,
1826 SMB_ACL_ENTRY_T entry_d,
1827 SMB_ACL_PERMSET_T *permset_p)
1829 VFS_FIND(sys_acl_get_permset);
1830 return handle->fns->sys_acl_get_permset_fn(handle, entry_d, permset_p);
1833 void * smb_vfs_call_sys_acl_get_qualifier(struct vfs_handle_struct *handle,
1834 SMB_ACL_ENTRY_T entry_d)
1836 VFS_FIND(sys_acl_get_qualifier);
1837 return handle->fns->sys_acl_get_qualifier_fn(handle, entry_d);
1840 SMB_ACL_T smb_vfs_call_sys_acl_get_file(struct vfs_handle_struct *handle,
1841 const char *path_p,
1842 SMB_ACL_TYPE_T type)
1844 VFS_FIND(sys_acl_get_file);
1845 return handle->fns->sys_acl_get_file_fn(handle, path_p, type);
1848 SMB_ACL_T smb_vfs_call_sys_acl_get_fd(struct vfs_handle_struct *handle,
1849 struct files_struct *fsp)
1851 VFS_FIND(sys_acl_get_fd);
1852 return handle->fns->sys_acl_get_fd_fn(handle, fsp);
1855 int smb_vfs_call_sys_acl_clear_perms(struct vfs_handle_struct *handle,
1856 SMB_ACL_PERMSET_T permset)
1858 VFS_FIND(sys_acl_clear_perms);
1859 return handle->fns->sys_acl_clear_perms_fn(handle, permset);
1862 int smb_vfs_call_sys_acl_add_perm(struct vfs_handle_struct *handle,
1863 SMB_ACL_PERMSET_T permset,
1864 SMB_ACL_PERM_T perm)
1866 VFS_FIND(sys_acl_add_perm);
1867 return handle->fns->sys_acl_add_perm_fn(handle, permset, perm);
1870 char * smb_vfs_call_sys_acl_to_text(struct vfs_handle_struct *handle,
1871 SMB_ACL_T theacl, ssize_t *plen)
1873 VFS_FIND(sys_acl_to_text);
1874 return handle->fns->sys_acl_to_text_fn(handle, theacl, plen);
1877 SMB_ACL_T smb_vfs_call_sys_acl_init(struct vfs_handle_struct *handle,
1878 int count)
1880 VFS_FIND(sys_acl_init);
1881 return handle->fns->sys_acl_init_fn(handle, count);
1884 int smb_vfs_call_sys_acl_create_entry(struct vfs_handle_struct *handle,
1885 SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
1887 VFS_FIND(sys_acl_create_entry);
1888 return handle->fns->sys_acl_create_entry_fn(handle, pacl, pentry);
1891 int smb_vfs_call_sys_acl_set_tag_type(struct vfs_handle_struct *handle,
1892 SMB_ACL_ENTRY_T entry,
1893 SMB_ACL_TAG_T tagtype)
1895 VFS_FIND(sys_acl_set_tag_type);
1896 return handle->fns->sys_acl_set_tag_type_fn(handle, entry, tagtype);
1899 int smb_vfs_call_sys_acl_set_qualifier(struct vfs_handle_struct *handle,
1900 SMB_ACL_ENTRY_T entry, void *qual)
1902 VFS_FIND(sys_acl_set_qualifier);
1903 return handle->fns->sys_acl_set_qualifier_fn(handle, entry, qual);
1906 int smb_vfs_call_sys_acl_set_permset(struct vfs_handle_struct *handle,
1907 SMB_ACL_ENTRY_T entry,
1908 SMB_ACL_PERMSET_T permset)
1910 VFS_FIND(sys_acl_set_permset);
1911 return handle->fns->sys_acl_set_permset_fn(handle, entry, permset);
1914 int smb_vfs_call_sys_acl_valid(struct vfs_handle_struct *handle,
1915 SMB_ACL_T theacl)
1917 VFS_FIND(sys_acl_valid);
1918 return handle->fns->sys_acl_valid_fn(handle, theacl);
1921 int smb_vfs_call_sys_acl_set_file(struct vfs_handle_struct *handle,
1922 const char *name, SMB_ACL_TYPE_T acltype,
1923 SMB_ACL_T theacl)
1925 VFS_FIND(sys_acl_set_file);
1926 return handle->fns->sys_acl_set_file_fn(handle, name, acltype, theacl);
1929 int smb_vfs_call_sys_acl_set_fd(struct vfs_handle_struct *handle,
1930 struct files_struct *fsp, SMB_ACL_T theacl)
1932 VFS_FIND(sys_acl_set_fd);
1933 return handle->fns->sys_acl_set_fd_fn(handle, fsp, theacl);
1936 int smb_vfs_call_sys_acl_delete_def_file(struct vfs_handle_struct *handle,
1937 const char *path)
1939 VFS_FIND(sys_acl_delete_def_file);
1940 return handle->fns->sys_acl_delete_def_file_fn(handle, path);
1943 int smb_vfs_call_sys_acl_get_perm(struct vfs_handle_struct *handle,
1944 SMB_ACL_PERMSET_T permset,
1945 SMB_ACL_PERM_T perm)
1947 VFS_FIND(sys_acl_get_perm);
1948 return handle->fns->sys_acl_get_perm_fn(handle, permset, perm);
1951 int smb_vfs_call_sys_acl_free_text(struct vfs_handle_struct *handle,
1952 char *text)
1954 VFS_FIND(sys_acl_free_text);
1955 return handle->fns->sys_acl_free_text_fn(handle, text);
1958 int smb_vfs_call_sys_acl_free_acl(struct vfs_handle_struct *handle,
1959 SMB_ACL_T posix_acl)
1961 VFS_FIND(sys_acl_free_acl);
1962 return handle->fns->sys_acl_free_acl_fn(handle, posix_acl);
1965 int smb_vfs_call_sys_acl_free_qualifier(struct vfs_handle_struct *handle,
1966 void *qualifier, SMB_ACL_TAG_T tagtype)
1968 VFS_FIND(sys_acl_free_qualifier);
1969 return handle->fns->sys_acl_free_qualifier_fn(handle, qualifier,
1970 tagtype);
1973 ssize_t smb_vfs_call_getxattr(struct vfs_handle_struct *handle,
1974 const char *path, const char *name, void *value,
1975 size_t size)
1977 VFS_FIND(getxattr);
1978 return handle->fns->getxattr_fn(handle, path, name, value, size);
1981 ssize_t smb_vfs_call_lgetxattr(struct vfs_handle_struct *handle,
1982 const char *path, const char *name, void *value,
1983 size_t size)
1985 VFS_FIND(lgetxattr);
1986 return handle->fns->lgetxattr_fn(handle, path, name, value, size);
1989 ssize_t smb_vfs_call_fgetxattr(struct vfs_handle_struct *handle,
1990 struct files_struct *fsp, const char *name,
1991 void *value, size_t size)
1993 VFS_FIND(fgetxattr);
1994 return handle->fns->fgetxattr_fn(handle, fsp, name, value, size);
1997 ssize_t smb_vfs_call_listxattr(struct vfs_handle_struct *handle,
1998 const char *path, char *list, size_t size)
2000 VFS_FIND(listxattr);
2001 return handle->fns->listxattr_fn(handle, path, list, size);
2004 ssize_t smb_vfs_call_llistxattr(struct vfs_handle_struct *handle,
2005 const char *path, char *list, size_t size)
2007 VFS_FIND(llistxattr);
2008 return handle->fns->llistxattr_fn(handle, path, list, size);
2011 ssize_t smb_vfs_call_flistxattr(struct vfs_handle_struct *handle,
2012 struct files_struct *fsp, char *list,
2013 size_t size)
2015 VFS_FIND(flistxattr);
2016 return handle->fns->flistxattr_fn(handle, fsp, list, size);
2019 int smb_vfs_call_removexattr(struct vfs_handle_struct *handle,
2020 const char *path, const char *name)
2022 VFS_FIND(removexattr);
2023 return handle->fns->removexattr_fn(handle, path, name);
2026 int smb_vfs_call_lremovexattr(struct vfs_handle_struct *handle,
2027 const char *path, const char *name)
2029 VFS_FIND(lremovexattr);
2030 return handle->fns->lremovexattr_fn(handle, path, name);
2033 int smb_vfs_call_fremovexattr(struct vfs_handle_struct *handle,
2034 struct files_struct *fsp, const char *name)
2036 VFS_FIND(fremovexattr);
2037 return handle->fns->fremovexattr_fn(handle, fsp, name);
2040 int smb_vfs_call_setxattr(struct vfs_handle_struct *handle, const char *path,
2041 const char *name, const void *value, size_t size,
2042 int flags)
2044 VFS_FIND(setxattr);
2045 return handle->fns->setxattr_fn(handle, path, name, value, size, flags);
2048 int smb_vfs_call_lsetxattr(struct vfs_handle_struct *handle, const char *path,
2049 const char *name, const void *value, size_t size,
2050 int flags)
2052 VFS_FIND(lsetxattr);
2053 return handle->fns->lsetxattr_fn(handle, path, name, value, size,
2054 flags);
2057 int smb_vfs_call_fsetxattr(struct vfs_handle_struct *handle,
2058 struct files_struct *fsp, const char *name,
2059 const void *value, size_t size, int flags)
2061 VFS_FIND(fsetxattr);
2062 return handle->fns->fsetxattr_fn(handle, fsp, name, value, size, flags);
2065 int smb_vfs_call_aio_read(struct vfs_handle_struct *handle,
2066 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
2068 VFS_FIND(aio_read);
2069 return handle->fns->aio_read_fn(handle, fsp, aiocb);
2072 int smb_vfs_call_aio_write(struct vfs_handle_struct *handle,
2073 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
2075 VFS_FIND(aio_write);
2076 return handle->fns->aio_write_fn(handle, fsp, aiocb);
2079 ssize_t smb_vfs_call_aio_return(struct vfs_handle_struct *handle,
2080 struct files_struct *fsp,
2081 SMB_STRUCT_AIOCB *aiocb)
2083 VFS_FIND(aio_return);
2084 return handle->fns->aio_return_fn(handle, fsp, aiocb);
2087 int smb_vfs_call_aio_cancel(struct vfs_handle_struct *handle,
2088 struct files_struct *fsp, SMB_STRUCT_AIOCB *aiocb)
2090 VFS_FIND(aio_cancel);
2091 return handle->fns->aio_cancel_fn(handle, fsp, aiocb);
2094 int smb_vfs_call_aio_error(struct vfs_handle_struct *handle,
2095 struct files_struct *fsp,
2096 SMB_STRUCT_AIOCB *aiocb)
2098 VFS_FIND(aio_error);
2099 return handle->fns->aio_error_fn(handle, fsp, aiocb);
2102 int smb_vfs_call_aio_fsync(struct vfs_handle_struct *handle,
2103 struct files_struct *fsp, int op,
2104 SMB_STRUCT_AIOCB *aiocb)
2106 VFS_FIND(aio_fsync);
2107 return handle->fns->aio_fsync_fn(handle, fsp, op, aiocb);
2110 int smb_vfs_call_aio_suspend(struct vfs_handle_struct *handle,
2111 struct files_struct *fsp,
2112 const SMB_STRUCT_AIOCB * const aiocb[], int n,
2113 const struct timespec *timeout)
2115 VFS_FIND(aio_suspend);
2116 return handle->fns->aio_suspend_fn(handle, fsp, aiocb, n, timeout);
2119 bool smb_vfs_call_aio_force(struct vfs_handle_struct *handle,
2120 struct files_struct *fsp)
2122 VFS_FIND(aio_force);
2123 return handle->fns->aio_force_fn(handle, fsp);
2126 bool smb_vfs_call_is_offline(struct vfs_handle_struct *handle,
2127 const struct smb_filename *fname,
2128 SMB_STRUCT_STAT *sbuf)
2130 VFS_FIND(is_offline);
2131 return handle->fns->is_offline_fn(handle, fname, sbuf);
2134 int smb_vfs_call_set_offline(struct vfs_handle_struct *handle,
2135 const struct smb_filename *fname)
2137 VFS_FIND(set_offline);
2138 return handle->fns->set_offline_fn(handle, fname);