source: branches/samba-3.2.x/source/smbd/vfs.c@ 252

Last change on this file since 252 was 228, checked in by Herwig Bauernfeind, 16 years ago

Update 3.2 branch to 3.2.6

File size: 25.7 KB
Line 
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
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.
13
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.
18
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/>.
21
22 This work was sponsored by Optifacio Software Services, Inc.
23*/
24
25#include "includes.h"
26
27#undef DBGC_CLASS
28#define DBGC_CLASS DBGC_VFS
29
30static_decl_vfs;
31
32struct vfs_init_function_entry {
33 char *name;
34 const vfs_op_tuple *vfs_op_tuples;
35 struct vfs_init_function_entry *prev, *next;
36};
37
38static struct vfs_init_function_entry *backends = NULL;
39
40/****************************************************************************
41 maintain the list of available backends
42****************************************************************************/
43
44static struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
45{
46 struct vfs_init_function_entry *entry = backends;
47
48 DEBUG(10, ("vfs_find_backend_entry called for %s\n", name));
49
50 while(entry) {
51 if (strcmp(entry->name, name)==0) return entry;
52 entry = entry->next;
53 }
54
55 return NULL;
56}
57
58NTSTATUS smb_register_vfs(int version, const char *name, const vfs_op_tuple *vfs_op_tuples)
59{
60 struct vfs_init_function_entry *entry = backends;
61
62 if ((version != SMB_VFS_INTERFACE_VERSION)) {
63 DEBUG(0, ("Failed to register vfs module.\n"
64 "The module was compiled against SMB_VFS_INTERFACE_VERSION %d,\n"
65 "current SMB_VFS_INTERFACE_VERSION is %d.\n"
66 "Please recompile against the current Samba Version!\n",
67 version, SMB_VFS_INTERFACE_VERSION));
68 return NT_STATUS_OBJECT_TYPE_MISMATCH;
69 }
70
71 if (!name || !name[0] || !vfs_op_tuples) {
72 DEBUG(0,("smb_register_vfs() called with NULL pointer or empty name!\n"));
73 return NT_STATUS_INVALID_PARAMETER;
74 }
75
76 if (vfs_find_backend_entry(name)) {
77 DEBUG(0,("VFS module %s already loaded!\n", name));
78 return NT_STATUS_OBJECT_NAME_COLLISION;
79 }
80
81 entry = SMB_XMALLOC_P(struct vfs_init_function_entry);
82 entry->name = smb_xstrdup(name);
83 entry->vfs_op_tuples = vfs_op_tuples;
84
85 DLIST_ADD(backends, entry);
86 DEBUG(5, ("Successfully added vfs backend '%s'\n", name));
87 return NT_STATUS_OK;
88}
89
90/****************************************************************************
91 initialise default vfs hooks
92****************************************************************************/
93
94static void vfs_init_default(connection_struct *conn)
95{
96 DEBUG(3, ("Initialising default vfs hooks\n"));
97 vfs_init_custom(conn, DEFAULT_VFS_MODULE_NAME);
98}
99
100/****************************************************************************
101 initialise custom vfs hooks
102 ****************************************************************************/
103
104static inline void vfs_set_operation(struct vfs_ops * vfs, vfs_op_type which,
105 struct vfs_handle_struct * handle, void * op)
106{
107 ((struct vfs_handle_struct **)&vfs->handles)[which] = handle;
108 ((void **)(void *)&vfs->ops)[which] = op;
109}
110
111bool vfs_init_custom(connection_struct *conn, const char *vfs_object)
112{
113 const vfs_op_tuple *ops;
114 char *module_path = NULL;
115 char *module_name = NULL;
116 char *module_param = NULL, *p;
117 int i;
118 vfs_handle_struct *handle;
119 const struct vfs_init_function_entry *entry;
120
121 if (!conn||!vfs_object||!vfs_object[0]) {
122 DEBUG(0,("vfs_init_custon() called with NULL pointer or emtpy vfs_object!\n"));
123 return False;
124 }
125
126 if(!backends) {
127 static_init_vfs;
128 }
129
130 DEBUG(3, ("Initialising custom vfs hooks from [%s]\n", vfs_object));
131
132 module_path = smb_xstrdup(vfs_object);
133
134 p = strchr_m(module_path, ':');
135
136 if (p) {
137 *p = 0;
138 module_param = p+1;
139 trim_char(module_param, ' ', ' ');
140 }
141
142 trim_char(module_path, ' ', ' ');
143
144 module_name = smb_xstrdup(module_path);
145
146 if ((module_name[0] == '/') &&
147 (strcmp(module_path, DEFAULT_VFS_MODULE_NAME) != 0)) {
148
149 /*
150 * Extract the module name from the path. Just use the base
151 * name of the last path component.
152 */
153
154 SAFE_FREE(module_name);
155 module_name = smb_xstrdup(strrchr_m(module_path, '/')+1);
156
157 p = strchr_m(module_name, '.');
158
159 if (p != NULL) {
160 *p = '\0';
161 }
162 }
163
164 /* First, try to load the module with the new module system */
165 if((entry = vfs_find_backend_entry(module_name)) ||
166 (NT_STATUS_IS_OK(smb_probe_module("vfs", module_path)) &&
167 (entry = vfs_find_backend_entry(module_name)))) {
168
169 DEBUGADD(5,("Successfully loaded vfs module [%s] with the new modules system\n", vfs_object));
170
171 if ((ops = entry->vfs_op_tuples) == NULL) {
172 DEBUG(0, ("entry->vfs_op_tuples==NULL for [%s] failed\n", vfs_object));
173 goto fail;
174 }
175 } else {
176 DEBUG(0,("Can't find a vfs module [%s]\n",vfs_object));
177 goto fail;
178 }
179
180 handle = TALLOC_ZERO_P(conn->mem_ctx,vfs_handle_struct);
181 if (!handle) {
182 DEBUG(0,("TALLOC_ZERO() failed!\n"));
183 goto fail;
184 }
185 memcpy(&handle->vfs_next, &conn->vfs, sizeof(struct vfs_ops));
186 handle->conn = conn;
187 if (module_param) {
188 handle->param = talloc_strdup(conn->mem_ctx, module_param);
189 }
190 DLIST_ADD(conn->vfs_handles, handle);
191
192 for(i=0; ops[i].op != NULL; i++) {
193 DEBUG(5, ("Checking operation #%d (type %d, layer %d)\n", i, ops[i].type, ops[i].layer));
194 if(ops[i].layer == SMB_VFS_LAYER_OPAQUE) {
195 /* If this operation was already made opaque by different module, it
196 * will be overridden here.
197 */
198 DEBUGADD(5, ("Making operation type %d opaque [module %s]\n", ops[i].type, vfs_object));
199 vfs_set_operation(&conn->vfs_opaque, ops[i].type, handle, ops[i].op);
200 }
201 /* Change current VFS disposition*/
202 DEBUGADD(5, ("Accepting operation type %d from module %s\n", ops[i].type, vfs_object));
203 vfs_set_operation(&conn->vfs, ops[i].type, handle, ops[i].op);
204 }
205
206 SAFE_FREE(module_path);
207 SAFE_FREE(module_name);
208 return True;
209
210 fail:
211 SAFE_FREE(module_path);
212 SAFE_FREE(module_name);
213 return False;
214}
215
216/*****************************************************************
217 Allow VFS modules to extend files_struct with VFS-specific state.
218 This will be ok for small numbers of extensions, but might need to
219 be refactored if it becomes more widely used.
220******************************************************************/
221
222#define EXT_DATA_AREA(e) ((uint8 *)(e) + sizeof(struct vfs_fsp_data))
223
224void *vfs_add_fsp_extension_notype(vfs_handle_struct *handle, files_struct *fsp, size_t ext_size)
225{
226 struct vfs_fsp_data *ext;
227 void * ext_data;
228
229 /* Prevent VFS modules adding multiple extensions. */
230 if ((ext_data = vfs_fetch_fsp_extension(handle, fsp))) {
231 return ext_data;
232 }
233
234 ext = (struct vfs_fsp_data *)TALLOC_ZERO(
235 handle->conn->mem_ctx, sizeof(struct vfs_fsp_data) + ext_size);
236 if (ext == NULL) {
237 return NULL;
238 }
239
240 ext->owner = handle;
241 ext->next = fsp->vfs_extension;
242 fsp->vfs_extension = ext;
243 return EXT_DATA_AREA(ext);
244}
245
246void vfs_remove_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
247{
248 struct vfs_fsp_data *curr;
249 struct vfs_fsp_data *prev;
250
251 for (curr = fsp->vfs_extension, prev = NULL;
252 curr;
253 prev = curr, curr = curr->next) {
254 if (curr->owner == handle) {
255 if (prev) {
256 prev->next = curr->next;
257 } else {
258 fsp->vfs_extension = curr->next;
259 }
260 TALLOC_FREE(curr);
261 return;
262 }
263 }
264}
265
266void *vfs_memctx_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
267{
268 struct vfs_fsp_data *head;
269
270 for (head = fsp->vfs_extension; head; head = head->next) {
271 if (head->owner == handle) {
272 return head;
273 }
274 }
275
276 return NULL;
277}
278
279void *vfs_fetch_fsp_extension(vfs_handle_struct *handle, files_struct *fsp)
280{
281 struct vfs_fsp_data *head;
282
283 head = (struct vfs_fsp_data *)vfs_memctx_fsp_extension(handle, fsp);
284 if (head != NULL) {
285 return EXT_DATA_AREA(head);
286 }
287
288 return NULL;
289}
290
291#undef EXT_DATA_AREA
292
293/*****************************************************************
294 Generic VFS init.
295******************************************************************/
296
297bool smbd_vfs_init(connection_struct *conn)
298{
299 const char **vfs_objects;
300 unsigned int i = 0;
301 int j = 0;
302
303 /* Normal share - initialise with disk access functions */
304 vfs_init_default(conn);
305 vfs_objects = lp_vfs_objects(SNUM(conn));
306
307 /* Override VFS functions if 'vfs object' was not specified*/
308 if (!vfs_objects || !vfs_objects[0])
309 return True;
310
311 for (i=0; vfs_objects[i] ;) {
312 i++;
313 }
314
315 for (j=i-1; j >= 0; j--) {
316 if (!vfs_init_custom(conn, vfs_objects[j])) {
317 DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed for %s\n", vfs_objects[j]));
318 return False;
319 }
320 }
321 return True;
322}
323
324/*******************************************************************
325 Check if directory exists.
326********************************************************************/
327
328bool vfs_directory_exist(connection_struct *conn, const char *dname, SMB_STRUCT_STAT *st)
329{
330 SMB_STRUCT_STAT st2;
331 bool ret;
332
333 if (!st)
334 st = &st2;
335
336 if (SMB_VFS_STAT(conn,dname,st) != 0)
337 return(False);
338
339 ret = S_ISDIR(st->st_mode);
340 if(!ret){
341 DEBUG( 0, ( "PS - ENOTDIR4\n" ) );
342 errno = ENOTDIR;}
343
344 return ret;
345}
346
347/*******************************************************************
348 Check if an object exists in the vfs.
349********************************************************************/
350
351bool vfs_object_exist(connection_struct *conn,const char *fname,SMB_STRUCT_STAT *sbuf)
352{
353 SMB_STRUCT_STAT st;
354
355 if (!sbuf)
356 sbuf = &st;
357
358 ZERO_STRUCTP(sbuf);
359
360 if (SMB_VFS_STAT(conn,fname,sbuf) == -1)
361 return(False);
362 return True;
363}
364
365/*******************************************************************
366 Check if a file exists in the vfs.
367********************************************************************/
368
369bool vfs_file_exist(connection_struct *conn, const char *fname,SMB_STRUCT_STAT *sbuf)
370{
371 SMB_STRUCT_STAT st;
372
373 if (!sbuf)
374 sbuf = &st;
375
376 ZERO_STRUCTP(sbuf);
377
378 if (SMB_VFS_STAT(conn,fname,sbuf) == -1)
379 return False;
380 return(S_ISREG(sbuf->st_mode));
381}
382
383/****************************************************************************
384 Read data from fsp on the vfs. (note: EINTR re-read differs from vfs_write_data)
385****************************************************************************/
386
387ssize_t vfs_read_data(files_struct *fsp, char *buf, size_t byte_count)
388{
389 size_t total=0;
390
391 while (total < byte_count)
392 {
393 ssize_t ret = SMB_VFS_READ(fsp, buf + total,
394 byte_count - total);
395
396 if (ret == 0) return total;
397 if (ret == -1) {
398 if (errno == EINTR)
399 continue;
400 else
401 return -1;
402 }
403 total += ret;
404 }
405 return (ssize_t)total;
406}
407
408ssize_t vfs_pread_data(files_struct *fsp, char *buf,
409 size_t byte_count, SMB_OFF_T offset)
410{
411 size_t total=0;
412
413 while (total < byte_count)
414 {
415 ssize_t ret = SMB_VFS_PREAD(fsp, buf + total,
416 byte_count - total, offset + total);
417
418 if (ret == 0) return total;
419 if (ret == -1) {
420 if (errno == EINTR)
421 continue;
422 else
423 return -1;
424 }
425 total += ret;
426 }
427 return (ssize_t)total;
428}
429
430/****************************************************************************
431 Write data to a fd on the vfs.
432****************************************************************************/
433
434ssize_t vfs_write_data(struct smb_request *req,
435 files_struct *fsp,
436 const char *buffer,
437 size_t N)
438{
439 size_t total=0;
440 ssize_t ret;
441
442 if (req && req->unread_bytes) {
443 SMB_ASSERT(req->unread_bytes == N);
444 /* VFS_RECVFILE must drain the socket
445 * before returning. */
446 req->unread_bytes = 0;
447 return SMB_VFS_RECVFILE(smbd_server_fd(),
448 fsp,
449 (SMB_OFF_T)-1,
450 N);
451 }
452
453 while (total < N) {
454 ret = SMB_VFS_WRITE(fsp, buffer + total, N - total);
455
456 if (ret == -1)
457 return -1;
458 if (ret == 0)
459 return total;
460
461 total += ret;
462 }
463 return (ssize_t)total;
464}
465
466ssize_t vfs_pwrite_data(struct smb_request *req,
467 files_struct *fsp,
468 const char *buffer,
469 size_t N,
470 SMB_OFF_T offset)
471{
472 size_t total=0;
473 ssize_t ret;
474
475 if (req && req->unread_bytes) {
476 SMB_ASSERT(req->unread_bytes == N);
477 /* VFS_RECVFILE must drain the socket
478 * before returning. */
479 req->unread_bytes = 0;
480 return SMB_VFS_RECVFILE(smbd_server_fd(),
481 fsp,
482 offset,
483 N);
484 }
485
486 while (total < N) {
487 ret = SMB_VFS_PWRITE(fsp, buffer + total, N - total,
488 offset + total);
489
490 if (ret == -1)
491 return -1;
492 if (ret == 0)
493 return total;
494
495 total += ret;
496 }
497 return (ssize_t)total;
498}
499/****************************************************************************
500 An allocate file space call using the vfs interface.
501 Allocates space for a file from a filedescriptor.
502 Returns 0 on success, -1 on failure.
503****************************************************************************/
504
505int vfs_allocate_file_space(files_struct *fsp, SMB_BIG_UINT len)
506{
507 int ret;
508 SMB_STRUCT_STAT st;
509 connection_struct *conn = fsp->conn;
510 SMB_BIG_UINT space_avail;
511 SMB_BIG_UINT bsize,dfree,dsize;
512
513 release_level_2_oplocks_on_change(fsp);
514
515 /*
516 * Actually try and commit the space on disk....
517 */
518
519 DEBUG(10,("vfs_allocate_file_space: file %s, len %.0f\n", fsp->fsp_name, (double)len ));
520
521 if (((SMB_OFF_T)len) < 0) {
522 DEBUG(0,("vfs_allocate_file_space: %s negative len requested.\n", fsp->fsp_name ));
523 errno = EINVAL;
524 return -1;
525 }
526
527 ret = SMB_VFS_FSTAT(fsp, &st);
528 if (ret == -1)
529 return ret;
530
531 if (len == (SMB_BIG_UINT)st.st_size)
532 return 0;
533
534 if (len < (SMB_BIG_UINT)st.st_size) {
535 /* Shrink - use ftruncate. */
536
537 DEBUG(10,("vfs_allocate_file_space: file %s, shrink. Current size %.0f\n",
538 fsp->fsp_name, (double)st.st_size ));
539
540 flush_write_cache(fsp, SIZECHANGE_FLUSH);
541 if ((ret = SMB_VFS_FTRUNCATE(fsp, (SMB_OFF_T)len)) != -1) {
542 set_filelen_write_cache(fsp, len);
543 }
544 return ret;
545 }
546
547 /* Grow - we need to test if we have enough space. */
548
549 if (!lp_strict_allocate(SNUM(fsp->conn)))
550 return 0;
551
552 len -= st.st_size;
553 len /= 1024; /* Len is now number of 1k blocks needed. */
554 space_avail = get_dfree_info(conn,fsp->fsp_name,False,&bsize,&dfree,&dsize);
555 if (space_avail == (SMB_BIG_UINT)-1) {
556 return -1;
557 }
558
559 DEBUG(10,("vfs_allocate_file_space: file %s, grow. Current size %.0f, needed blocks = %.0f, space avail = %.0f\n",
560 fsp->fsp_name, (double)st.st_size, (double)len, (double)space_avail ));
561
562 if (len > space_avail) {
563 errno = ENOSPC;
564 return -1;
565 }
566
567 return 0;
568}
569
570/****************************************************************************
571 A vfs set_filelen call.
572 set the length of a file from a filedescriptor.
573 Returns 0 on success, -1 on failure.
574****************************************************************************/
575
576int vfs_set_filelen(files_struct *fsp, SMB_OFF_T len)
577{
578 int ret;
579
580 release_level_2_oplocks_on_change(fsp);
581 DEBUG(10,("vfs_set_filelen: ftruncate %s to len %.0f\n", fsp->fsp_name, (double)len));
582 flush_write_cache(fsp, SIZECHANGE_FLUSH);
583 if ((ret = SMB_VFS_FTRUNCATE(fsp, len)) != -1) {
584 set_filelen_write_cache(fsp, len);
585 notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED,
586 FILE_NOTIFY_CHANGE_SIZE
587 | FILE_NOTIFY_CHANGE_ATTRIBUTES,
588 fsp->fsp_name);
589 }
590
591 return ret;
592}
593
594/****************************************************************************
595 A vfs fill sparse call.
596 Writes zeros from the end of file to len, if len is greater than EOF.
597 Used only by strict_sync.
598 Returns 0 on success, -1 on failure.
599****************************************************************************/
600
601static char *sparse_buf;
602#define SPARSE_BUF_WRITE_SIZE (32*1024)
603
604int vfs_fill_sparse(files_struct *fsp, SMB_OFF_T len)
605{
606 int ret;
607 SMB_STRUCT_STAT st;
608 SMB_OFF_T offset;
609 size_t total;
610 size_t num_to_write;
611 ssize_t pwrite_ret;
612
613 release_level_2_oplocks_on_change(fsp);
614 ret = SMB_VFS_FSTAT(fsp, &st);
615 if (ret == -1) {
616 return ret;
617 }
618
619 if (len <= st.st_size) {
620 return 0;
621 }
622
623 DEBUG(10,("vfs_fill_sparse: write zeros in file %s from len %.0f to len %.0f (%.0f bytes)\n",
624 fsp->fsp_name, (double)st.st_size, (double)len, (double)(len - st.st_size)));
625
626 flush_write_cache(fsp, SIZECHANGE_FLUSH);
627
628#ifdef HAVE_POSIX_FALLOCATE
629 set_filelen_write_cache(fsp, len);
630#else
631 if (!sparse_buf) {
632 sparse_buf = SMB_CALLOC_ARRAY(char, SPARSE_BUF_WRITE_SIZE);
633 if (!sparse_buf) {
634 errno = ENOMEM;
635 return -1;
636 }
637 }
638
639 offset = st.st_size;
640 num_to_write = len - st.st_size;
641 total = 0;
642
643 while (total < num_to_write) {
644 size_t curr_write_size = MIN(SPARSE_BUF_WRITE_SIZE, (num_to_write - total));
645
646 pwrite_ret = SMB_VFS_PWRITE(fsp, sparse_buf, curr_write_size, offset + total);
647 if (pwrite_ret == -1) {
648 DEBUG(10,("vfs_fill_sparse: SMB_VFS_PWRITE for file %s failed with error %s\n",
649 fsp->fsp_name, strerror(errno) ));
650 return -1;
651 }
652 if (pwrite_ret == 0) {
653 return 0;
654 }
655
656 total += pwrite_ret;
657 }
658
659 ret = 0;
660#endif
661
662 set_filelen_write_cache(fsp, len);
663 return ret;
664}
665
666/****************************************************************************
667 Transfer some data (n bytes) between two file_struct's.
668****************************************************************************/
669
670static ssize_t vfs_read_fn(void *file, void *buf, size_t len)
671{
672 struct files_struct *fsp = (struct files_struct *)file;
673
674 return SMB_VFS_READ(fsp, buf, len);
675}
676
677static ssize_t vfs_write_fn(void *file, const void *buf, size_t len)
678{
679 struct files_struct *fsp = (struct files_struct *)file;
680
681 return SMB_VFS_WRITE(fsp, buf, len);
682}
683
684SMB_OFF_T vfs_transfer_file(files_struct *in, files_struct *out, SMB_OFF_T n)
685{
686 return transfer_file_internal((void *)in, (void *)out, n,
687 vfs_read_fn, vfs_write_fn);
688}
689
690/*******************************************************************
691 A vfs_readdir wrapper which just returns the file name.
692********************************************************************/
693
694char *vfs_readdirname(connection_struct *conn, void *p)
695{
696 SMB_STRUCT_DIRENT *ptr= NULL;
697 char *dname;
698
699 if (!p)
700 return(NULL);
701
702 ptr = SMB_VFS_READDIR(conn, (DIR *)p);
703 if (!ptr)
704 return(NULL);
705
706 dname = ptr->d_name;
707
708#ifdef NEXT2
709 if (telldir(p) < 0)
710 return(NULL);
711#endif
712
713#ifdef HAVE_BROKEN_READDIR_NAME
714 /* using /usr/ucb/cc is BAD */
715 dname = dname - 2;
716#endif
717
718 return(dname);
719}
720
721/*******************************************************************
722 A wrapper for vfs_chdir().
723********************************************************************/
724
725int vfs_ChDir(connection_struct *conn, const char *path)
726{
727 int res;
728 static char *LastDir = NULL;
729
730 if (!LastDir) {
731 LastDir = SMB_STRDUP("");
732 }
733
734 if (strcsequal(path,"."))
735 return(0);
736
737#ifdef __OS2__
738 if ((*path == '/' || *path == '\\' || (*path && path[1] == ':')) && strcsequal(LastDir,path))
739#else
740 if (*path == '/' && strcsequal(LastDir,path))
741#endif
742 return(0);
743
744 DEBUG(4,("vfs_ChDir to %s\n",path));
745
746 res = SMB_VFS_CHDIR(conn,path);
747 if (!res) {
748 SAFE_FREE(LastDir);
749 LastDir = SMB_STRDUP(path);
750 }
751 return(res);
752}
753
754/*******************************************************************
755 Return the absolute current directory path - given a UNIX pathname.
756 Note that this path is returned in DOS format, not UNIX
757 format. Note this can be called with conn == NULL.
758********************************************************************/
759
760struct getwd_cache_key {
761 SMB_DEV_T dev;
762 SMB_INO_T ino;
763};
764
765char *vfs_GetWd(TALLOC_CTX *ctx, connection_struct *conn)
766{
767 char s[PATH_MAX+1];
768 SMB_STRUCT_STAT st, st2;
769 char *result;
770 DATA_BLOB cache_value;
771 struct getwd_cache_key key;
772
773 *s = 0;
774
775 if (!lp_getwd_cache()) {
776 goto nocache;
777 }
778
779 SET_STAT_INVALID(st);
780
781 if (SMB_VFS_STAT(conn, ".",&st) == -1) {
782 /*
783 * Known to fail for root: the directory may be NFS-mounted
784 * and exported with root_squash (so has no root access).
785 */
786 DEBUG(1,("vfs_GetWd: couldn't stat \".\" error %s "
787 "(NFS problem ?)\n", strerror(errno) ));
788 goto nocache;
789 }
790
791 ZERO_STRUCT(key); /* unlikely, but possible padding */
792 key.dev = st.st_dev;
793 key.ino = st.st_ino;
794
795 if (!memcache_lookup(smbd_memcache(), GETWD_CACHE,
796 data_blob_const(&key, sizeof(key)),
797 &cache_value)) {
798 goto nocache;
799 }
800
801 SMB_ASSERT((cache_value.length > 0)
802 && (cache_value.data[cache_value.length-1] == '\0'));
803
804 if ((SMB_VFS_STAT(conn, (char *)cache_value.data, &st2) == 0)
805 && (st.st_dev == st2.st_dev) && (st.st_ino == st2.st_ino)
806 && (S_ISDIR(st.st_mode))) {
807 /*
808 * Ok, we're done
809 */
810 result = talloc_strdup(ctx, (char *)cache_value.data);
811 if (result == NULL) {
812 errno = ENOMEM;
813 }
814 return result;
815 }
816
817 nocache:
818
819 /*
820 * We don't have the information to hand so rely on traditional
821 * methods. The very slow getcwd, which spawns a process on some
822 * systems, or the not quite so bad getwd.
823 */
824
825 if (!SMB_VFS_GETWD(conn,s)) {
826 DEBUG(0, ("vfs_GetWd: SMB_VFS_GETWD call failed: %s\n",
827 strerror(errno)));
828 return NULL;
829 }
830
831 if (lp_getwd_cache() && VALID_STAT(st)) {
832 ZERO_STRUCT(key); /* unlikely, but possible padding */
833 key.dev = st.st_dev;
834 key.ino = st.st_ino;
835
836 memcache_add(smbd_memcache(), GETWD_CACHE,
837 data_blob_const(&key, sizeof(key)),
838 data_blob_const(s, strlen(s)+1));
839 }
840
841 result = talloc_strdup(ctx, s);
842 if (result == NULL) {
843 errno = ENOMEM;
844 }
845 return result;
846}
847
848/*******************************************************************
849 Reduce a file name, removing .. elements and checking that
850 it is below dir in the heirachy. This uses realpath.
851********************************************************************/
852
853NTSTATUS check_reduced_name(connection_struct *conn, const char *fname)
854{
855#ifdef REALPATH_TAKES_NULL
856 bool free_resolved_name = True;
857#else
858 char resolved_name_buf[PATH_MAX+1];
859 bool free_resolved_name = False;
860#endif
861 char *resolved_name = NULL;
862 size_t con_path_len = strlen(conn->connectpath);
863 char *p = NULL;
864
865 DEBUG(3,("reduce_name [%s] [%s]\n", fname, conn->connectpath));
866
867#ifdef REALPATH_TAKES_NULL
868 resolved_name = SMB_VFS_REALPATH(conn,fname,NULL);
869#else
870 resolved_name = SMB_VFS_REALPATH(conn,fname,resolved_name_buf);
871#endif
872
873 if (!resolved_name) {
874 switch (errno) {
875 case ENOTDIR:
876 DEBUG(3,("reduce_name: Component not a directory in getting realpath for %s\n", fname));
877 return map_nt_error_from_unix(errno);
878 case ENOENT:
879 {
880 TALLOC_CTX *ctx = talloc_tos();
881 char *tmp_fname = NULL;
882 char *last_component = NULL;
883 /* Last component didn't exist. Remove it and try and canonicalise the directory. */
884
885 tmp_fname = talloc_strdup(ctx, fname);
886 if (!tmp_fname) {
887 return NT_STATUS_NO_MEMORY;
888 }
889 p = strrchr_m(tmp_fname, '/');
890 if (p) {
891 *p++ = '\0';
892 last_component = p;
893 } else {
894 last_component = tmp_fname;
895 tmp_fname = talloc_strdup(ctx,
896 ".");
897 if (!tmp_fname) {
898 return NT_STATUS_NO_MEMORY;
899 }
900 }
901
902#ifdef REALPATH_TAKES_NULL
903 resolved_name = SMB_VFS_REALPATH(conn,tmp_fname,NULL);
904#else
905 resolved_name = SMB_VFS_REALPATH(conn,tmp_fname,resolved_name_buf);
906#endif
907 if (!resolved_name) {
908 DEBUG(3,("reduce_name: couldn't get realpath for %s\n", fname));
909 return map_nt_error_from_unix(errno);
910 }
911 tmp_fname = talloc_asprintf(ctx,
912 "%s/%s",
913 resolved_name,
914 last_component);
915 if (!tmp_fname) {
916 return NT_STATUS_NO_MEMORY;
917 }
918#ifdef REALPATH_TAKES_NULL
919 SAFE_FREE(resolved_name);
920 resolved_name = SMB_STRDUP(tmp_fname);
921 if (!resolved_name) {
922 DEBUG(0,("reduce_name: malloc fail for %s\n", tmp_fname));
923 return NT_STATUS_NO_MEMORY;
924 }
925#else
926 safe_strcpy(resolved_name_buf, tmp_fname, PATH_MAX);
927 resolved_name = resolved_name_buf;
928#endif
929 break;
930 }
931 default:
932 DEBUG(1,("reduce_name: couldn't get realpath for %s\n", fname));
933 return map_nt_error_from_unix(errno);
934 }
935 }
936
937 DEBUG(10,("reduce_name realpath [%s] -> [%s]\n", fname, resolved_name));
938
939 if (*resolved_name != '/') {
940 DEBUG(0,("reduce_name: realpath doesn't return absolute paths !\n"));
941 if (free_resolved_name) {
942 SAFE_FREE(resolved_name);
943 }
944 return NT_STATUS_OBJECT_NAME_INVALID;
945 }
946
947 /* Check for widelinks allowed. */
948 if (!lp_widelinks(SNUM(conn)) && (strncmp(conn->connectpath, resolved_name, con_path_len) != 0)) {
949 DEBUG(2, ("reduce_name: Bad access attempt: %s is a symlink outside the share path", fname));
950 if (free_resolved_name) {
951 SAFE_FREE(resolved_name);
952 }
953 return NT_STATUS_ACCESS_DENIED;
954 }
955
956 /* Check if we are allowing users to follow symlinks */
957 /* Patch from David Clerc <David.Clerc@cui.unige.ch>
958 University of Geneva */
959
960#ifdef S_ISLNK
961 if (!lp_symlinks(SNUM(conn))) {
962 SMB_STRUCT_STAT statbuf;
963 if ( (SMB_VFS_LSTAT(conn,fname,&statbuf) != -1) &&
964 (S_ISLNK(statbuf.st_mode)) ) {
965 if (free_resolved_name) {
966 SAFE_FREE(resolved_name);
967 }
968 DEBUG(3,("reduce_name: denied: file path name %s is a symlink\n",resolved_name));
969 return NT_STATUS_ACCESS_DENIED;
970 }
971 }
972#endif
973
974 DEBUG(3,("reduce_name: %s reduced to %s\n", fname, resolved_name));
975 if (free_resolved_name) {
976 SAFE_FREE(resolved_name);
977 }
978 return NT_STATUS_OK;
979}
Note: See TracBrowser for help on using the repository browser.