s3/docs: Fix typo.
[Samba/gebeck_regimport.git] / source3 / smbd / vfs.c
blob385454e587f835f564feb98a54a3e70594e792b8
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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 This work was sponsored by Optifacio Software Services, Inc.
25 #include "includes.h"
26 #include "smbd/globals.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_VFS
31 static_decl_vfs;
33 struct vfs_init_function_entry {
34 char *name;
35 const vfs_op_tuple *vfs_op_tuples;
36 struct vfs_init_function_entry *prev, *next;
39 /****************************************************************************
40 maintain the list of available backends
41 ****************************************************************************/
43 static struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
45 struct vfs_init_function_entry *entry = backends;
47 DEBUG(10, ("vfs_find_backend_entry called for %s\n", name));
49 while(entry) {
50 if (strcmp(entry->name, name)==0) return entry;
51 entry = entry->next;
54 return NULL;
57 NTSTATUS smb_register_vfs(int version, const char *name, const vfs_op_tuple *vfs_op_tuples)
59 struct vfs_init_function_entry *entry = backends;
61 if ((version != SMB_VFS_INTERFACE_VERSION)) {
62 DEBUG(0, ("Failed to register vfs module.\n"
63 "The module was compiled against SMB_VFS_INTERFACE_VERSION %d,\n"
64 "current SMB_VFS_INTERFACE_VERSION is %d.\n"
65 "Please recompile against the current Samba Version!\n",
66 version, SMB_VFS_INTERFACE_VERSION));
67 return NT_STATUS_OBJECT_TYPE_MISMATCH;
70 if (!name || !name[0] || !vfs_op_tuples) {
71 DEBUG(0,("smb_register_vfs() called with NULL pointer or empty name!\n"));
72 return NT_STATUS_INVALID_PARAMETER;
75 if (vfs_find_backend_entry(name)) {
76 DEBUG(0,("VFS module %s already loaded!\n", name));
77 return NT_STATUS_OBJECT_NAME_COLLISION;
80 entry = SMB_XMALLOC_P(struct vfs_init_function_entry);
81 entry->name = smb_xstrdup(name);
82 entry->vfs_op_tuples = vfs_op_tuples;
84 DLIST_ADD(backends, entry);
85 DEBUG(5, ("Successfully added vfs backend '%s'\n", name));
86 return NT_STATUS_OK;
89 /****************************************************************************
90 initialise default vfs hooks
91 ****************************************************************************/
93 static void vfs_init_default(connection_struct *conn)
95 DEBUG(3, ("Initialising default vfs hooks\n"));
96 vfs_init_custom(conn, DEFAULT_VFS_MODULE_NAME);
99 /****************************************************************************
100 initialise custom vfs hooks
101 ****************************************************************************/
103 static inline void vfs_set_operation(struct vfs_ops * vfs, vfs_op_type which,
104 struct vfs_handle_struct * handle, void * op)
106 ((struct vfs_handle_struct **)&vfs->handles)[which] = handle;
107 ((void **)(void *)&vfs->ops)[which] = op;
110 bool vfs_init_custom(connection_struct *conn, const char *vfs_object)
112 const vfs_op_tuple *ops;
113 char *module_path = NULL;
114 char *module_name = NULL;
115 char *module_param = NULL, *p;
116 int i;
117 vfs_handle_struct *handle;
118 const struct vfs_init_function_entry *entry;
120 if (!conn||!vfs_object||!vfs_object[0]) {
121 DEBUG(0,("vfs_init_custon() called with NULL pointer or emtpy vfs_object!\n"));
122 return False;
125 if(!backends) {
126 static_init_vfs;
129 DEBUG(3, ("Initialising custom vfs hooks from [%s]\n", vfs_object));
131 module_path = smb_xstrdup(vfs_object);
133 p = strchr_m(module_path, ':');
135 if (p) {
136 *p = 0;
137 module_param = p+1;
138 trim_char(module_param, ' ', ' ');
141 trim_char(module_path, ' ', ' ');
143 module_name = smb_xstrdup(module_path);
145 if ((module_name[0] == '/') &&
146 (strcmp(module_path, DEFAULT_VFS_MODULE_NAME) != 0)) {
149 * Extract the module name from the path. Just use the base
150 * name of the last path component.
153 SAFE_FREE(module_name);
154 module_name = smb_xstrdup(strrchr_m(module_path, '/')+1);
156 p = strchr_m(module_name, '.');
158 if (p != NULL) {
159 *p = '\0';
163 /* First, try to load the module with the new module system */
164 entry = vfs_find_backend_entry(module_name);
165 if (!entry) {
166 NTSTATUS status;
168 DEBUG(5, ("vfs module [%s] not loaded - trying to load...\n",
169 vfs_object));
171 status = smb_probe_module("vfs", module_path);
172 if (!NT_STATUS_IS_OK(status)) {
173 DEBUG(0, ("error probing vfs module '%s': %s\n",
174 module_path, nt_errstr(status)));
175 goto fail;
178 entry = vfs_find_backend_entry(module_name);
179 if (!entry) {
180 DEBUG(0,("Can't find a vfs module [%s]\n",vfs_object));
181 goto fail;
185 DEBUGADD(5,("Successfully loaded vfs module [%s] with the new modules system\n", vfs_object));
186 if ((ops = entry->vfs_op_tuples) == NULL) {
187 DEBUG(0, ("entry->vfs_op_tuples==NULL for [%s] failed\n", vfs_object));
188 goto fail;
191 handle = TALLOC_ZERO_P(conn, vfs_handle_struct);
192 if (!handle) {
193 DEBUG(0,("TALLOC_ZERO() failed!\n"));
194 goto fail;
196 memcpy(&handle->vfs_next, &conn->vfs, sizeof(struct vfs_ops));
197 handle->conn = conn;
198 if (module_param) {
199 handle->param = talloc_strdup(conn, module_param);
201 DLIST_ADD(conn->vfs_handles, handle);
203 for(i=0; ops[i].op != NULL; i++) {
204 DEBUG(5, ("Checking operation #%d (type %d, layer %d)\n", i, ops[i].type, ops[i].layer));
205 if(ops[i].layer == SMB_VFS_LAYER_OPAQUE) {
206 /* If this operation was already made opaque by different module, it
207 * will be overridden here.
209 DEBUGADD(5, ("Making operation type %d opaque [module %s]\n", ops[i].type, vfs_object));
210 vfs_set_operation(&conn->vfs_opaque, ops[i].type, handle, ops[i].op);
212 /* Change current VFS disposition*/
213 DEBUGADD(5, ("Accepting operation type %d from module %s\n", ops[i].type, vfs_object));
214 vfs_set_operation(&conn->vfs, ops[i].type, handle, ops[i].op);
217 SAFE_FREE(module_path);
218 SAFE_FREE(module_name);
219 return True;
221 fail:
222 SAFE_FREE(module_path);
223 SAFE_FREE(module_name);
224 return False;
227 /*****************************************************************
228 Allow VFS modules to extend files_struct with VFS-specific state.
229 This will be ok for small numbers of extensions, but might need to
230 be refactored if it becomes more widely used.
231 ******************************************************************/
233 #define EXT_DATA_AREA(e) ((uint8 *)(e) + sizeof(struct vfs_fsp_data))
235 void *vfs_add_fsp_extension_notype(vfs_handle_struct *handle,
236 files_struct *fsp, size_t ext_size,
237 void (*destroy_fn)(void *p_data))
239 struct vfs_fsp_data *ext;
240 void * ext_data;
242 /* Prevent VFS modules adding multiple extensions. */
243 if ((ext_data = vfs_fetch_fsp_extension(handle, fsp))) {
244 return ext_data;
247 ext = (struct vfs_fsp_data *)TALLOC_ZERO(
248 handle->conn, sizeof(struct vfs_fsp_data) + ext_size);
249 if (ext == NULL) {
250 return NULL;
253 ext->owner = handle;
254 ext->next = fsp->vfs_extension;
255 ext->destroy = destroy_fn;
256 fsp->vfs_extension = ext;
257 return EXT_DATA_AREA(ext);
260 void vfs_remove_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
262 struct vfs_fsp_data *curr;
263 struct vfs_fsp_data *prev;
265 for (curr = fsp->vfs_extension, prev = NULL;
266 curr;
267 prev = curr, curr = curr->next) {
268 if (curr->owner == handle) {
269 if (prev) {
270 prev->next = curr->next;
271 } else {
272 fsp->vfs_extension = curr->next;
274 if (curr->destroy) {
275 curr->destroy(EXT_DATA_AREA(curr));
277 TALLOC_FREE(curr);
278 return;
283 void *vfs_memctx_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
285 struct vfs_fsp_data *head;
287 for (head = fsp->vfs_extension; head; head = head->next) {
288 if (head->owner == handle) {
289 return head;
293 return NULL;
296 void *vfs_fetch_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
298 struct vfs_fsp_data *head;
300 head = (struct vfs_fsp_data *)vfs_memctx_fsp_extension(handle, fsp);
301 if (head != NULL) {
302 return EXT_DATA_AREA(head);
305 return NULL;
308 #undef EXT_DATA_AREA
310 /*****************************************************************
311 Generic VFS init.
312 ******************************************************************/
314 bool smbd_vfs_init(connection_struct *conn)
316 const char **vfs_objects;
317 unsigned int i = 0;
318 int j = 0;
320 /* Normal share - initialise with disk access functions */
321 vfs_init_default(conn);
322 vfs_objects = lp_vfs_objects(SNUM(conn));
324 /* Override VFS functions if 'vfs object' was not specified*/
325 if (!vfs_objects || !vfs_objects[0])
326 return True;
328 for (i=0; vfs_objects[i] ;) {
329 i++;
332 for (j=i-1; j >= 0; j--) {
333 if (!vfs_init_custom(conn, vfs_objects[j])) {
334 DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed for %s\n", vfs_objects[j]));
335 return False;
338 return True;
341 /*******************************************************************
342 Check if directory exists.
343 ********************************************************************/
345 bool vfs_directory_exist(connection_struct *conn, const char *dname, SMB_STRUCT_STAT *st)
347 SMB_STRUCT_STAT st2;
348 bool ret;
350 if (!st)
351 st = &st2;
353 if (SMB_VFS_STAT(conn,dname,st) != 0)
354 return(False);
356 ret = S_ISDIR(st->st_ex_mode);
357 if(!ret)
358 errno = ENOTDIR;
360 return ret;
363 /*******************************************************************
364 Check if an object exists in the vfs.
365 ********************************************************************/
367 bool vfs_object_exist(connection_struct *conn,const char *fname,SMB_STRUCT_STAT *sbuf)
369 SMB_STRUCT_STAT st;
371 if (!sbuf)
372 sbuf = &st;
374 ZERO_STRUCTP(sbuf);
376 if (SMB_VFS_STAT(conn,fname,sbuf) == -1)
377 return(False);
378 return True;
381 /*******************************************************************
382 Check if a file exists in the vfs.
383 ********************************************************************/
385 NTSTATUS vfs_file_exist(connection_struct *conn, struct smb_filename *smb_fname)
387 char *fname = NULL;
388 NTSTATUS status;
390 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
391 if (!NT_STATUS_IS_OK(status)) {
392 goto out;
395 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
396 if (SMB_VFS_STAT(conn, fname, &smb_fname->st) == -1) {
397 goto out;
400 /* Only return OK if stat was successful and S_ISREG */
401 if (S_ISREG(smb_fname->st.st_ex_mode)) {
402 status = NT_STATUS_OK;
404 out:
405 TALLOC_FREE(fname);
406 return status;
409 /****************************************************************************
410 Read data from fsp on the vfs. (note: EINTR re-read differs from vfs_write_data)
411 ****************************************************************************/
413 ssize_t vfs_read_data(files_struct *fsp, char *buf, size_t byte_count)
415 size_t total=0;
417 while (total < byte_count)
419 ssize_t ret = SMB_VFS_READ(fsp, buf + total,
420 byte_count - total);
422 if (ret == 0) return total;
423 if (ret == -1) {
424 if (errno == EINTR)
425 continue;
426 else
427 return -1;
429 total += ret;
431 return (ssize_t)total;
434 ssize_t vfs_pread_data(files_struct *fsp, char *buf,
435 size_t byte_count, SMB_OFF_T offset)
437 size_t total=0;
439 while (total < byte_count)
441 ssize_t ret = SMB_VFS_PREAD(fsp, buf + total,
442 byte_count - total, offset + total);
444 if (ret == 0) return total;
445 if (ret == -1) {
446 if (errno == EINTR)
447 continue;
448 else
449 return -1;
451 total += ret;
453 return (ssize_t)total;
456 /****************************************************************************
457 Write data to a fd on the vfs.
458 ****************************************************************************/
460 ssize_t vfs_write_data(struct smb_request *req,
461 files_struct *fsp,
462 const char *buffer,
463 size_t N)
465 size_t total=0;
466 ssize_t ret;
468 if (req && req->unread_bytes) {
469 SMB_ASSERT(req->unread_bytes == N);
470 /* VFS_RECVFILE must drain the socket
471 * before returning. */
472 req->unread_bytes = 0;
473 return SMB_VFS_RECVFILE(smbd_server_fd(),
474 fsp,
475 (SMB_OFF_T)-1,
479 while (total < N) {
480 ret = SMB_VFS_WRITE(fsp, buffer + total, N - total);
482 if (ret == -1)
483 return -1;
484 if (ret == 0)
485 return total;
487 total += ret;
489 return (ssize_t)total;
492 ssize_t vfs_pwrite_data(struct smb_request *req,
493 files_struct *fsp,
494 const char *buffer,
495 size_t N,
496 SMB_OFF_T offset)
498 size_t total=0;
499 ssize_t ret;
501 if (req && req->unread_bytes) {
502 SMB_ASSERT(req->unread_bytes == N);
503 /* VFS_RECVFILE must drain the socket
504 * before returning. */
505 req->unread_bytes = 0;
506 return SMB_VFS_RECVFILE(smbd_server_fd(),
507 fsp,
508 offset,
512 while (total < N) {
513 ret = SMB_VFS_PWRITE(fsp, buffer + total, N - total,
514 offset + total);
516 if (ret == -1)
517 return -1;
518 if (ret == 0)
519 return total;
521 total += ret;
523 return (ssize_t)total;
525 /****************************************************************************
526 An allocate file space call using the vfs interface.
527 Allocates space for a file from a filedescriptor.
528 Returns 0 on success, -1 on failure.
529 ****************************************************************************/
531 int vfs_allocate_file_space(files_struct *fsp, uint64_t len)
533 int ret;
534 SMB_STRUCT_STAT st;
535 connection_struct *conn = fsp->conn;
536 uint64_t space_avail;
537 uint64_t bsize,dfree,dsize;
540 * Actually try and commit the space on disk....
543 DEBUG(10,("vfs_allocate_file_space: file %s, len %.0f\n", fsp->fsp_name, (double)len ));
545 if (((SMB_OFF_T)len) < 0) {
546 DEBUG(0,("vfs_allocate_file_space: %s negative len requested.\n", fsp->fsp_name ));
547 errno = EINVAL;
548 return -1;
551 ret = SMB_VFS_FSTAT(fsp, &st);
552 if (ret == -1)
553 return ret;
555 if (len == (uint64_t)st.st_ex_size)
556 return 0;
558 if (len < (uint64_t)st.st_ex_size) {
559 /* Shrink - use ftruncate. */
561 DEBUG(10,("vfs_allocate_file_space: file %s, shrink. Current size %.0f\n",
562 fsp->fsp_name, (double)st.st_ex_size ));
564 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_ALLOC_SHRINK);
566 flush_write_cache(fsp, SIZECHANGE_FLUSH);
567 if ((ret = SMB_VFS_FTRUNCATE(fsp, (SMB_OFF_T)len)) != -1) {
568 set_filelen_write_cache(fsp, len);
571 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_ALLOC_SHRINK);
573 return ret;
576 /* Grow - we need to test if we have enough space. */
578 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_ALLOC_GROW);
579 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_ALLOC_GROW);
581 if (!lp_strict_allocate(SNUM(fsp->conn)))
582 return 0;
584 len -= st.st_ex_size;
585 len /= 1024; /* Len is now number of 1k blocks needed. */
586 space_avail = get_dfree_info(conn,fsp->fsp_name,False,&bsize,&dfree,&dsize);
587 if (space_avail == (uint64_t)-1) {
588 return -1;
591 DEBUG(10,("vfs_allocate_file_space: file %s, grow. Current size %.0f, needed blocks = %.0f, space avail = %.0f\n",
592 fsp->fsp_name, (double)st.st_ex_size, (double)len, (double)space_avail ));
594 if (len > space_avail) {
595 errno = ENOSPC;
596 return -1;
599 return 0;
602 /****************************************************************************
603 A vfs set_filelen call.
604 set the length of a file from a filedescriptor.
605 Returns 0 on success, -1 on failure.
606 ****************************************************************************/
608 int vfs_set_filelen(files_struct *fsp, SMB_OFF_T len)
610 int ret;
612 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
614 DEBUG(10,("vfs_set_filelen: ftruncate %s to len %.0f\n", fsp->fsp_name, (double)len));
615 flush_write_cache(fsp, SIZECHANGE_FLUSH);
616 if ((ret = SMB_VFS_FTRUNCATE(fsp, len)) != -1) {
617 set_filelen_write_cache(fsp, len);
618 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
619 FILE_NOTIFY_CHANGE_SIZE
620 | FILE_NOTIFY_CHANGE_ATTRIBUTES,
621 fsp->fsp_name);
624 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_SET_FILE_LEN);
626 return ret;
629 /****************************************************************************
630 A vfs fill sparse call.
631 Writes zeros from the end of file to len, if len is greater than EOF.
632 Used only by strict_sync.
633 Returns 0 on success, -1 on failure.
634 ****************************************************************************/
636 #define SPARSE_BUF_WRITE_SIZE (32*1024)
638 int vfs_fill_sparse(files_struct *fsp, SMB_OFF_T len)
640 int ret;
641 SMB_STRUCT_STAT st;
642 SMB_OFF_T offset;
643 size_t total;
644 size_t num_to_write;
645 ssize_t pwrite_ret;
647 ret = SMB_VFS_FSTAT(fsp, &st);
648 if (ret == -1) {
649 return ret;
652 if (len <= st.st_ex_size) {
653 return 0;
656 DEBUG(10,("vfs_fill_sparse: write zeros in file %s from len %.0f to len %.0f (%.0f bytes)\n",
657 fsp->fsp_name, (double)st.st_ex_size, (double)len, (double)(len - st.st_ex_size)));
659 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_FILL_SPARSE);
661 flush_write_cache(fsp, SIZECHANGE_FLUSH);
663 if (!sparse_buf) {
664 sparse_buf = SMB_CALLOC_ARRAY(char, SPARSE_BUF_WRITE_SIZE);
665 if (!sparse_buf) {
666 errno = ENOMEM;
667 ret = -1;
668 goto out;
672 offset = st.st_ex_size;
673 num_to_write = len - st.st_ex_size;
674 total = 0;
676 while (total < num_to_write) {
677 size_t curr_write_size = MIN(SPARSE_BUF_WRITE_SIZE, (num_to_write - total));
679 pwrite_ret = SMB_VFS_PWRITE(fsp, sparse_buf, curr_write_size, offset + total);
680 if (pwrite_ret == -1) {
681 DEBUG(10,("vfs_fill_sparse: SMB_VFS_PWRITE for file %s failed with error %s\n",
682 fsp->fsp_name, strerror(errno) ));
683 ret = -1;
684 goto out;
686 if (pwrite_ret == 0) {
687 ret = 0;
688 goto out;
691 total += pwrite_ret;
694 set_filelen_write_cache(fsp, len);
696 ret = 0;
697 out:
698 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_FILL_SPARSE);
699 return ret;
702 /****************************************************************************
703 Transfer some data (n bytes) between two file_struct's.
704 ****************************************************************************/
706 static ssize_t vfs_read_fn(void *file, void *buf, size_t len)
708 struct files_struct *fsp = (struct files_struct *)file;
710 return SMB_VFS_READ(fsp, buf, len);
713 static ssize_t vfs_write_fn(void *file, const void *buf, size_t len)
715 struct files_struct *fsp = (struct files_struct *)file;
717 return SMB_VFS_WRITE(fsp, buf, len);
720 SMB_OFF_T vfs_transfer_file(files_struct *in, files_struct *out, SMB_OFF_T n)
722 return transfer_file_internal((void *)in, (void *)out, n,
723 vfs_read_fn, vfs_write_fn);
726 /*******************************************************************
727 A vfs_readdir wrapper which just returns the file name.
728 ********************************************************************/
730 char *vfs_readdirname(connection_struct *conn, void *p, SMB_STRUCT_STAT *sbuf)
732 SMB_STRUCT_DIRENT *ptr= NULL;
733 char *dname;
735 if (!p)
736 return(NULL);
738 ptr = SMB_VFS_READDIR(conn, (DIR *)p, sbuf);
739 if (!ptr)
740 return(NULL);
742 dname = ptr->d_name;
744 #ifdef NEXT2
745 if (telldir(p) < 0)
746 return(NULL);
747 #endif
749 #ifdef HAVE_BROKEN_READDIR_NAME
750 /* using /usr/ucb/cc is BAD */
751 dname = dname - 2;
752 #endif
754 return(dname);
757 /*******************************************************************
758 A wrapper for vfs_chdir().
759 ********************************************************************/
761 int vfs_ChDir(connection_struct *conn, const char *path)
763 int res;
765 if (!LastDir) {
766 LastDir = SMB_STRDUP("");
769 if (strcsequal(path,"."))
770 return(0);
772 if (*path == '/' && strcsequal(LastDir,path))
773 return(0);
775 DEBUG(4,("vfs_ChDir to %s\n",path));
777 res = SMB_VFS_CHDIR(conn,path);
778 if (!res) {
779 SAFE_FREE(LastDir);
780 LastDir = SMB_STRDUP(path);
782 return(res);
785 /*******************************************************************
786 Return the absolute current directory path - given a UNIX pathname.
787 Note that this path is returned in DOS format, not UNIX
788 format. Note this can be called with conn == NULL.
789 ********************************************************************/
791 char *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn)
793 char s[PATH_MAX+1];
794 SMB_STRUCT_STAT st, st2;
795 char *result;
796 DATA_BLOB cache_value;
797 struct file_id key;
799 *s = 0;
801 if (!lp_getwd_cache()) {
802 goto nocache;
805 SET_STAT_INVALID(st);
807 if (SMB_VFS_STAT(conn, ".",&st) == -1) {
809 * Known to fail for root: the directory may be NFS-mounted
810 * and exported with root_squash (so has no root access).
812 DEBUG(1,("vfs_GetWd: couldn't stat \".\" error %s "
813 "(NFS problem ?)\n", strerror(errno) ));
814 goto nocache;
817 key = vfs_file_id_from_sbuf(conn, &st);
819 if (!memcache_lookup(smbd_memcache(), GETWD_CACHE,
820 data_blob_const(&key, sizeof(key)),
821 &cache_value)) {
822 goto nocache;
825 SMB_ASSERT((cache_value.length > 0)
826 && (cache_value.data[cache_value.length-1] == '\0'));
828 if ((SMB_VFS_STAT(conn, (char *)cache_value.data, &st2) == 0)
829 && (st.st_ex_dev == st2.st_ex_dev) && (st.st_ex_ino == st2.st_ex_ino)
830 && (S_ISDIR(st.st_ex_mode))) {
832 * Ok, we're done
834 result = talloc_strdup(ctx, (char *)cache_value.data);
835 if (result == NULL) {
836 errno = ENOMEM;
838 return result;
841 nocache:
844 * We don't have the information to hand so rely on traditional
845 * methods. The very slow getcwd, which spawns a process on some
846 * systems, or the not quite so bad getwd.
849 if (!SMB_VFS_GETWD(conn,s)) {
850 DEBUG(0, ("vfs_GetWd: SMB_VFS_GETWD call failed: %s\n",
851 strerror(errno)));
852 return NULL;
855 if (lp_getwd_cache() && VALID_STAT(st)) {
856 key = vfs_file_id_from_sbuf(conn, &st);
858 memcache_add(smbd_memcache(), GETWD_CACHE,
859 data_blob_const(&key, sizeof(key)),
860 data_blob_const(s, strlen(s)+1));
863 result = talloc_strdup(ctx, s);
864 if (result == NULL) {
865 errno = ENOMEM;
867 return result;
870 /*******************************************************************
871 Reduce a file name, removing .. elements and checking that
872 it is below dir in the heirachy. This uses realpath.
873 ********************************************************************/
875 NTSTATUS check_reduced_name(connection_struct *conn, const char *fname)
877 #ifdef REALPATH_TAKES_NULL
878 bool free_resolved_name = True;
879 #else
880 char resolved_name_buf[PATH_MAX+1];
881 bool free_resolved_name = False;
882 #endif
883 char *resolved_name = NULL;
884 char *p = NULL;
886 DEBUG(3,("reduce_name [%s] [%s]\n", fname, conn->connectpath));
888 #ifdef REALPATH_TAKES_NULL
889 resolved_name = SMB_VFS_REALPATH(conn,fname,NULL);
890 #else
891 resolved_name = SMB_VFS_REALPATH(conn,fname,resolved_name_buf);
892 #endif
894 if (!resolved_name) {
895 switch (errno) {
896 case ENOTDIR:
897 DEBUG(3,("reduce_name: Component not a directory in getting realpath for %s\n", fname));
898 return map_nt_error_from_unix(errno);
899 case ENOENT:
901 TALLOC_CTX *ctx = talloc_tos();
902 char *tmp_fname = NULL;
903 char *last_component = NULL;
904 /* Last component didn't exist. Remove it and try and canonicalise the directory. */
906 tmp_fname = talloc_strdup(ctx, fname);
907 if (!tmp_fname) {
908 return NT_STATUS_NO_MEMORY;
910 p = strrchr_m(tmp_fname, '/');
911 if (p) {
912 *p++ = '\0';
913 last_component = p;
914 } else {
915 last_component = tmp_fname;
916 tmp_fname = talloc_strdup(ctx,
917 ".");
918 if (!tmp_fname) {
919 return NT_STATUS_NO_MEMORY;
923 #ifdef REALPATH_TAKES_NULL
924 resolved_name = SMB_VFS_REALPATH(conn,tmp_fname,NULL);
925 #else
926 resolved_name = SMB_VFS_REALPATH(conn,tmp_fname,resolved_name_buf);
927 #endif
928 if (!resolved_name) {
929 DEBUG(3,("reduce_name: couldn't get realpath for %s\n", fname));
930 return map_nt_error_from_unix(errno);
932 tmp_fname = talloc_asprintf(ctx,
933 "%s/%s",
934 resolved_name,
935 last_component);
936 if (!tmp_fname) {
937 return NT_STATUS_NO_MEMORY;
939 #ifdef REALPATH_TAKES_NULL
940 SAFE_FREE(resolved_name);
941 resolved_name = SMB_STRDUP(tmp_fname);
942 if (!resolved_name) {
943 DEBUG(0,("reduce_name: malloc fail for %s\n", tmp_fname));
944 return NT_STATUS_NO_MEMORY;
946 #else
947 safe_strcpy(resolved_name_buf, tmp_fname, PATH_MAX);
948 resolved_name = resolved_name_buf;
949 #endif
950 break;
952 default:
953 DEBUG(1,("reduce_name: couldn't get realpath for %s\n", fname));
954 return map_nt_error_from_unix(errno);
958 DEBUG(10,("reduce_name realpath [%s] -> [%s]\n", fname, resolved_name));
960 if (*resolved_name != '/') {
961 DEBUG(0,("reduce_name: realpath doesn't return absolute paths !\n"));
962 if (free_resolved_name) {
963 SAFE_FREE(resolved_name);
965 return NT_STATUS_OBJECT_NAME_INVALID;
968 /* Check for widelinks allowed. */
969 if (!lp_widelinks(SNUM(conn))) {
970 const char *conn_rootdir;
972 conn_rootdir = SMB_VFS_CONNECTPATH(conn, fname);
973 if (conn_rootdir == NULL) {
974 DEBUG(2, ("check_reduced_name: Could not get conn_rootdir\n"));
975 if (free_resolved_name) {
976 SAFE_FREE(resolved_name);
978 return NT_STATUS_ACCESS_DENIED;
981 if (strncmp(conn_rootdir, resolved_name,
982 strlen(conn_rootdir)) != 0) {
983 DEBUG(2, ("reduce_name: Bad access attempt: %s is "
984 "a symlink outside the share path",
985 fname));
986 if (free_resolved_name) {
987 SAFE_FREE(resolved_name);
989 return NT_STATUS_ACCESS_DENIED;
993 /* Check if we are allowing users to follow symlinks */
994 /* Patch from David Clerc <David.Clerc@cui.unige.ch>
995 University of Geneva */
997 #ifdef S_ISLNK
998 if (!lp_symlinks(SNUM(conn))) {
999 SMB_STRUCT_STAT statbuf;
1000 if ( (SMB_VFS_LSTAT(conn,fname,&statbuf) != -1) &&
1001 (S_ISLNK(statbuf.st_ex_mode)) ) {
1002 if (free_resolved_name) {
1003 SAFE_FREE(resolved_name);
1005 DEBUG(3,("reduce_name: denied: file path name %s is a symlink\n",resolved_name));
1006 return NT_STATUS_ACCESS_DENIED;
1009 #endif
1011 DEBUG(3,("reduce_name: %s reduced to %s\n", fname, resolved_name));
1012 if (free_resolved_name) {
1013 SAFE_FREE(resolved_name);
1015 return NT_STATUS_OK;