1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
|
/*
Unix SMB/Netbios implementation.
Version 1.9.
VFS initialisation and support functions
Copyright (C) Tim Potter 1999
Copyright (C) Alexander Bokovoy 2002
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
This work was sponsored by Optifacio Software Services, Inc.
*/
#include "includes.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_VFS
struct vfs_init_function_entry {
char *name;
vfs_op_tuple *ops, *(*init)(const struct vfs_ops *, struct smb_vfs_handle_struct *);
struct vfs_init_function_entry *prev, *next;
};
static struct vfs_init_function_entry *backends = NULL;
/* Some structures to help us initialise the vfs operations table */
struct vfs_syminfo {
char *name;
void *fptr;
};
/*
Opaque (final) vfs operations. This is a combination of first-met opaque vfs operations
across all currently processed modules. */
static vfs_op_tuple vfs_opaque_ops[SMB_VFS_OP_LAST];
/* Default vfs hooks. WARNING: The order of these initialisers is
very important. They must be in the same order as defined in
vfs.h. Change at your own peril. */
static struct vfs_ops default_vfs_ops = {
/* Disk operations */
vfswrap_dummy_connect,
vfswrap_dummy_disconnect,
vfswrap_disk_free,
/* Directory operations */
vfswrap_opendir,
vfswrap_readdir,
vfswrap_mkdir,
vfswrap_rmdir,
vfswrap_closedir,
/* File operations */
vfswrap_open,
vfswrap_close,
vfswrap_read,
vfswrap_write,
vfswrap_lseek,
vfswrap_sendfile,
vfswrap_rename,
vfswrap_fsync,
vfswrap_stat,
vfswrap_fstat,
vfswrap_lstat,
vfswrap_unlink,
vfswrap_chmod,
vfswrap_fchmod,
vfswrap_chown,
vfswrap_fchown,
vfswrap_chdir,
vfswrap_getwd,
vfswrap_utime,
vfswrap_ftruncate,
vfswrap_lock,
vfswrap_symlink,
vfswrap_readlink,
vfswrap_link,
vfswrap_mknod,
vfswrap_realpath,
vfswrap_fget_nt_acl,
vfswrap_get_nt_acl,
vfswrap_fset_nt_acl,
vfswrap_set_nt_acl,
/* POSIX ACL operations. */
#if defined(HAVE_NO_ACLS)
NULL,
NULL,
#else
vfswrap_chmod_acl,
vfswrap_fchmod_acl,
#endif
vfswrap_sys_acl_get_entry,
vfswrap_sys_acl_get_tag_type,
vfswrap_sys_acl_get_permset,
vfswrap_sys_acl_get_qualifier,
vfswrap_sys_acl_get_file,
vfswrap_sys_acl_get_fd,
vfswrap_sys_acl_clear_perms,
vfswrap_sys_acl_add_perm,
vfswrap_sys_acl_to_text,
vfswrap_sys_acl_init,
vfswrap_sys_acl_create_entry,
vfswrap_sys_acl_set_tag_type,
vfswrap_sys_acl_set_qualifier,
vfswrap_sys_acl_set_permset,
vfswrap_sys_acl_valid,
vfswrap_sys_acl_set_file,
vfswrap_sys_acl_set_fd,
vfswrap_sys_acl_delete_def_file,
vfswrap_sys_acl_get_perm,
vfswrap_sys_acl_free_text,
vfswrap_sys_acl_free_acl,
vfswrap_sys_acl_free_qualifier
};
/****************************************************************************
maintain the list of available backends
****************************************************************************/
struct vfs_init_function_entry *vfs_find_backend_entry(const char *name)
{
struct vfs_init_function_entry *entry = backends;
while(entry) {
if (strequal(entry->name, name)) return entry;
entry = entry->next;
}
return NULL;
}
BOOL smb_register_vfs(const char *name, vfs_op_tuple *(*init)(const struct vfs_ops *, struct smb_vfs_handle_struct *), int version)
{
struct vfs_init_function_entry *entry = backends;
if ((version < SMB_VFS_INTERFACE_CASCADED)) {
DEBUG(0, ("vfs_init() returned wrong interface version info (was %d, should be no less than %d)\n",
version, SMB_VFS_INTERFACE_VERSION ));
return False;
}
if ((version < SMB_VFS_INTERFACE_VERSION)) {
DEBUG(0, ("Warning: vfs_init() states that module confirms interface version #%d, current interface version is #%d.\n\
Proceeding in compatibility mode, new operations (since version #%d) will fallback to default ones.\n",
version, SMB_VFS_INTERFACE_VERSION, version ));
return False;
}
while(entry) {
if (strequal(entry->name, name)) {
DEBUG(0,("VFS module %s already loaded!\n", name));
return False;
}
entry = entry->next;
}
entry = smb_xmalloc(sizeof(struct vfs_init_function_entry));
entry->name = smb_xstrdup(name);
entry->init = init;
DLIST_ADD(backends, entry);
DEBUG(5, ("Successfully added vfs backend '%s'\n", name));
return True;
}
/****************************************************************************
initialise default vfs hooks
****************************************************************************/
static void vfs_init_default(connection_struct *conn)
{
DEBUG(3, ("Initialising default vfs hooks\n"));
memcpy(&conn->vfs_ops, &default_vfs_ops, sizeof(struct vfs_ops));
conn->vfs_private = NULL;
}
/***************************************************************************
Function to load old VFS modules. Should go away after a while.
**************************************************************************/
static BOOL vfs_load_old_plugin(connection_struct *conn, const char *vfs_object)
{
int vfs_version = -1;
vfs_op_tuple *ops, *(*init_fptr)(int *, const struct vfs_ops *, struct smb_vfs_handle_struct *);
/* Open object file */
if ((conn->vfs_private->handle = sys_dlopen(vfs_object, RTLD_NOW)) == NULL) {
DEBUG(0, ("Error opening %s: %s\n", vfs_object, sys_dlerror()));
return False;
}
/* Get handle on vfs_init() symbol */
init_fptr = (vfs_op_tuple *(*)(int *, const struct vfs_ops *, struct smb_vfs_handle_struct *))sys_dlsym(conn->vfs_private->handle, "vfs_init");
if (init_fptr == NULL) {
DEBUG(0, ("No vfs_init() symbol found in %s\n", vfs_object));
sys_dlclose(conn->vfs_private->handle);
return False;
}
/* Initialise vfs_ops structure */
if ((ops = init_fptr(&vfs_version, &conn->vfs_ops, conn->vfs_private)) == NULL) {
DEBUG(0, ("vfs_init() function from %s failed\n", vfs_object));
sys_dlclose(conn->vfs_private->handle);
return False;
}
if ((vfs_version < SMB_VFS_INTERFACE_CASCADED)) {
DEBUG(0, ("vfs_init() returned wrong interface version info (was %d, should be no less than %d)\n",
vfs_version, SMB_VFS_INTERFACE_VERSION ));
sys_dlclose(conn->vfs_private->handle);
return False;
}
if ((vfs_version < SMB_VFS_INTERFACE_VERSION)) {
DEBUG(0, ("Warning: vfs_init() states that module confirms interface version #%d, current interface version is #%d.\n\
Proceeding in compatibility mode, new operations (since version #%d) will fallback to default ones.\n",
vfs_version, SMB_VFS_INTERFACE_VERSION, vfs_version ));
sys_dlclose(conn->vfs_private->handle);
return False;
}
return True;
}
/****************************************************************************
initialise custom vfs hooks
****************************************************************************/
BOOL vfs_init_custom(connection_struct *conn, const char *vfs_object)
{
vfs_op_tuple *ops;
int i;
struct vfs_init_function_entry *entry;
DEBUG(3, ("Initialising custom vfs hooks from %s\n", vfs_object));
if(!backends) static_init_vfs;
/* First, try to load the module with the new module system */
if((entry = vfs_find_backend_entry(vfs_object)) ||
(smb_probe_module("vfs", vfs_object) &&
(entry = vfs_find_backend_entry(vfs_object)))) {
DEBUG(3,("Successfully loaded %s with the new modules system\n", vfs_object));
if ((ops = entry->init(&conn->vfs_ops, conn->vfs_private)) == NULL) {
DEBUG(0, ("vfs init function from %s failed\n", vfs_object));
return False;
}
} else {
/* If that doesn't work, fall back to the old system
* (This part should go away after a while, it's only here
* for backwards compatibility) */
DEBUG(2, ("Can't load module with new modules system, falling back to old\n"));
if (!vfs_load_old_plugin(conn, vfs_object)) return False;
}
for(i=0; ops[i].op != NULL; i++) {
DEBUG(3, ("Checking operation #%d (type %d, layer %d)\n", i, ops[i].type, ops[i].layer));
if(ops[i].layer == SMB_VFS_LAYER_OPAQUE) {
/* Check whether this operation was already made opaque by different module */
if(vfs_opaque_ops[ops[i].type].op == ((void**)&default_vfs_ops)[ops[i].type]) {
/* No, it isn't overloaded yet. Overload. */
DEBUG(3, ("Making operation type %d opaque [module %s]\n", ops[i].type, vfs_object));
vfs_opaque_ops[ops[i].type] = ops[i];
}
}
/* Change current VFS disposition*/
DEBUG(3, ("Accepting operation type %d from module %s\n", ops[i].type, vfs_object));
((void**)&conn->vfs_ops)[ops[i].type] = ops[i].op;
}
return True;
}
/*****************************************************************
Generic VFS init.
******************************************************************/
BOOL smbd_vfs_init(connection_struct *conn)
{
const char **vfs_objects;
char *vfs_module, *vfs_path;
unsigned int i;
unsigned int j = 0;
struct smb_vfs_handle_struct *handle;
/* Normal share - initialise with disk access functions */
vfs_init_default(conn);
vfs_objects = lp_vfsobj(SNUM(conn));
/* Override VFS functions if 'vfs object' was specified*/
if (!vfs_objects)
return True;
for(i=0; i<SMB_VFS_OP_LAST; i++) {
vfs_opaque_ops[i].op = ((void**)&default_vfs_ops)[i];
vfs_opaque_ops[i].type = i;
vfs_opaque_ops[i].layer = SMB_VFS_LAYER_OPAQUE;
}
vfs_path = lp_vfs_path(SNUM(conn));
for (j=0; vfs_objects[j]; j++) {
conn->vfs_private = NULL;
handle = (struct smb_vfs_handle_struct *) smb_xmalloc(sizeof(smb_vfs_handle_struct));
/* Loadable object file */
handle->handle = NULL;
DLIST_ADD(conn->vfs_private, handle);
vfs_module = NULL;
if (vfs_path && *vfs_path) {
asprintf(&vfs_module, "%s/%s", vfs_path, vfs_objects[j]);
} else {
asprintf(&vfs_module, "%s", vfs_objects[j]);
}
if (!vfs_init_custom(conn, vfs_module)) {
DEBUG(0, ("smbd_vfs_init: vfs_init_custom failed for %s\n", vfs_module));
SAFE_FREE(vfs_module);
DLIST_REMOVE(conn->vfs_private, handle);
SAFE_FREE(handle);
return False;
}
SAFE_FREE(vfs_module);
}
return True;
}
/*******************************************************************
Create vfs_ops reflecting current vfs_opaque_ops
*******************************************************************/
struct vfs_ops *smb_vfs_get_opaque_ops(void)
{
int i;
struct vfs_ops *ops;
ops = smb_xmalloc(sizeof(struct vfs_ops));
for(i=0; i<SMB_VFS_OP_LAST; i++) {
((void**)ops)[i] = vfs_opaque_ops[i].op;
}
return ops;
}
/*******************************************************************
Check if directory exists.
********************************************************************/
BOOL vfs_directory_exist(connection_struct *conn, const char *dname, SMB_STRUCT_STAT *st)
{
SMB_STRUCT_STAT st2;
BOOL ret;
if (!st)
st = &st2;
if (vfs_stat(conn,dname,st) != 0)
return(False);
ret = S_ISDIR(st->st_mode);
if(!ret)
errno = ENOTDIR;
return ret;
}
/*******************************************************************
vfs getwd wrapper
********************************************************************/
static char *vfs_getwd(connection_struct *conn, char *path)
{
return conn->vfs_ops.getwd(conn,path);
}
/*******************************************************************
vfs mkdir wrapper
********************************************************************/
int vfs_mkdir(connection_struct *conn, const char *name, mode_t mode)
{
int ret;
SMB_STRUCT_STAT sbuf;
if(!(ret=conn->vfs_ops.mkdir(conn,name,mode))) {
inherit_access_acl(conn, name, mode);
/*
* Check if high bits should have been set,
* then (if bits are missing): add them.
* Consider bits automagically set by UNIX, i.e. SGID bit from parent dir.
*/
if(mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) &&
!vfs_stat(conn,name,&sbuf) && (mode & ~sbuf.st_mode))
vfs_chmod(conn,name,sbuf.st_mode | (mode & ~sbuf.st_mode));
}
return ret;
}
/*******************************************************************
Check if an object exists in the vfs.
********************************************************************/
BOOL vfs_object_exist(connection_struct *conn,const char *fname,SMB_STRUCT_STAT *sbuf)
{
SMB_STRUCT_STAT st;
if (!sbuf)
sbuf = &st;
ZERO_STRUCTP(sbuf);
if (vfs_stat(conn,fname,sbuf) == -1)
return(False);
return True;
}
/*******************************************************************
Check if a file exists in the vfs.
********************************************************************/
BOOL vfs_file_exist(connection_struct *conn, const char *fname,SMB_STRUCT_STAT *sbuf)
{
SMB_STRUCT_STAT st;
if (!sbuf)
sbuf = &st;
ZERO_STRUCTP(sbuf);
if (vfs_stat(conn,fname,sbuf) == -1)
return False;
return(S_ISREG(sbuf->st_mode));
}
/****************************************************************************
Read data from fsp on the vfs. (note: EINTR re-read differs from vfs_write_data)
****************************************************************************/
ssize_t vfs_read_data(files_struct *fsp, char *buf, size_t byte_count)
{
size_t total=0;
while (total < byte_count)
{
ssize_t ret = fsp->conn->vfs_ops.read(fsp, fsp->fd, buf + total,
byte_count - total);
if (ret == 0) return total;
if (ret == -1) {
if (errno == EINTR)
continue;
else
return -1;
}
total += ret;
}
return (ssize_t)total;
}
/****************************************************************************
Write data to a fd on the vfs.
****************************************************************************/
ssize_t vfs_write_data(files_struct *fsp,const char *buffer,size_t N)
{
size_t total=0;
ssize_t ret;
while (total < N) {
ret = fsp->conn->vfs_ops.write(fsp,fsp->fd,buffer + total,N - total);
if (ret == -1)
return -1;
if (ret == 0)
return total;
total += ret;
}
return (ssize_t)total;
}
/****************************************************************************
An allocate file space call using the vfs interface.
Allocates space for a file from a filedescriptor.
Returns 0 on success, -1 on failure.
****************************************************************************/
int vfs_allocate_file_space(files_struct *fsp, SMB_BIG_UINT len)
{
int ret;
SMB_STRUCT_STAT st;
connection_struct *conn = fsp->conn;
struct vfs_ops *vfs_ops = &conn->vfs_ops;
SMB_BIG_UINT space_avail;
SMB_BIG_UINT bsize,dfree,dsize;
release_level_2_oplocks_on_change(fsp);
/*
* Actually try and commit the space on disk....
*/
DEBUG(10,("vfs_allocate_file_space: file %s, len %.0f\n", fsp->fsp_name, (double)len ));
if (((SMB_OFF_T)len) < 0) {
DEBUG(0,("vfs_allocate_file_space: %s negative len requested.\n", fsp->fsp_name ));
return -1;
}
ret = vfs_fstat(fsp,fsp->fd,&st);
if (ret == -1)
return ret;
if (len == (SMB_BIG_UINT)st.st_size)
return 0;
if (len < (SMB_BIG_UINT)st.st_size) {
/* Shrink - use ftruncate. */
DEBUG(10,("vfs_allocate_file_space: file %s, shrink. Current size %.0f\n",
fsp->fsp_name, (double)st.st_size ));
flush_write_cache(fsp, SIZECHANGE_FLUSH);
if ((ret = vfs_ops->ftruncate(fsp, fsp->fd, (SMB_OFF_T)len)) != -1) {
set_filelen_write_cache(fsp, len);
}
return ret;
}
/* Grow - we need to test if we have enough space. */
if (!lp_strict_allocate(SNUM(fsp->conn)))
return 0;
len -= st.st_size;
len /= 1024; /* Len is now number of 1k blocks needed. */
space_avail = conn->vfs_ops.disk_free(conn,fsp->fsp_name,False,&bsize,&dfree,&dsize);
DEBUG(10,("vfs_allocate_file_space: file %s, grow. Current size %.0f, needed blocks = %.0f, space avail = %.0f\n",
fsp->fsp_name, (double)st.st_size, (double)len, (double)space_avail ));
if (len > space_avail) {
errno = ENOSPC;
return -1;
}
return 0;
}
/****************************************************************************
A vfs set_filelen call.
set the length of a file from a filedescriptor.
Returns 0 on success, -1 on failure.
****************************************************************************/
int vfs_set_filelen(files_struct *fsp, SMB_OFF_T len)
{
int ret;
release_level_2_oplocks_on_change(fsp);
DEBUG(10,("vfs_set_filelen: ftruncate %s to len %.0f\n", fsp->fsp_name, (double)len));
flush_write_cache(fsp, SIZECHANGE_FLUSH);
if ((ret = fsp->conn->vfs_ops.ftruncate(fsp, fsp->fd, len)) != -1)
set_filelen_write_cache(fsp, len);
return ret;
}
/****************************************************************************
Transfer some data (n bytes) between two file_struct's.
****************************************************************************/
static files_struct *in_fsp;
static files_struct *out_fsp;
static ssize_t read_fn(int fd, void *buf, size_t len)
{
return in_fsp->conn->vfs_ops.read(in_fsp, fd, buf, len);
}
static ssize_t write_fn(int fd, const void *buf, size_t len)
{
return out_fsp->conn->vfs_ops.write(out_fsp, fd, buf, len);
}
SMB_OFF_T vfs_transfer_file(files_struct *in, files_struct *out, SMB_OFF_T n)
{
in_fsp = in;
out_fsp = out;
return transfer_file_internal(in_fsp->fd, out_fsp->fd, n, read_fn, write_fn);
}
/*******************************************************************
A vfs_readdir wrapper which just returns the file name.
********************************************************************/
char *vfs_readdirname(connection_struct *conn, void *p)
{
struct dirent *ptr;
char *dname;
if (!p)
return(NULL);
ptr = (struct dirent *)conn->vfs_ops.readdir(conn,p);
if (!ptr)
return(NULL);
dname = ptr->d_name;
#ifdef NEXT2
if (telldir(p) < 0)
return(NULL);
#endif
#ifdef HAVE_BROKEN_READDIR
/* using /usr/ucb/cc is BAD */
dname = dname - 2;
#endif
return(dname);
}
/* VFS options not quite working yet */
#if 0
/***************************************************************************
handle the interpretation of the vfs option parameter
*************************************************************************/
static BOOL handle_vfs_option(char *pszParmValue, char **ptr)
{
struct vfs_options *new_option, **options = (struct vfs_options **)ptr;
int i;
/* Create new vfs option */
new_option = (struct vfs_options *)malloc(sizeof(*new_option));
if (new_option == NULL) {
return False;
}
ZERO_STRUCTP(new_option);
/* Get name and value */
new_option->name = strtok(pszParmValue, "=");
if (new_option->name == NULL) {
return False;
}
while(isspace(*new_option->name)) {
new_option->name++;
}
for (i = strlen(new_option->name); i > 0; i--) {
if (!isspace(new_option->name[i - 1])) break;
}
new_option->name[i] = '\0';
new_option->name = strdup(new_option->name);
new_option->value = strtok(NULL, "=");
if (new_option->value != NULL) {
while(isspace(*new_option->value)) {
new_option->value++;
}
for (i = strlen(new_option->value); i > 0; i--) {
if (!isspace(new_option->value[i - 1])) break;
}
new_option->value[i] = '\0';
new_option->value = strdup(new_option->value);
}
/* Add to list */
DLIST_ADD(*options, new_option);
return True;
}
#endif
/*******************************************************************
A wrapper for vfs_chdir().
********************************************************************/
int vfs_ChDir(connection_struct *conn, const char *path)
{
int res;
static pstring LastDir="";
if (strcsequal(path,"."))
return(0);
if (*path == '/' && strcsequal(LastDir,path))
return(0);
DEBUG(3,("vfs_ChDir to %s\n",path));
res = vfs_chdir(conn,path);
if (!res)
pstrcpy(LastDir,path);
return(res);
}
/* number of list structures for a caching GetWd function. */
#define MAX_GETWDCACHE (50)
static struct {
SMB_DEV_T dev; /* These *must* be compatible with the types returned in a stat() call. */
SMB_INO_T inode; /* These *must* be compatible with the types returned in a stat() call. */
char *dos_path; /* The pathname in DOS format. */
BOOL valid;
} ino_list[MAX_GETWDCACHE];
extern BOOL use_getwd_cache;
/****************************************************************************
Prompte a ptr (to make it recently used)
****************************************************************************/
static void array_promote(char *array,int elsize,int element)
{
char *p;
if (element == 0)
return;
p = (char *)malloc(elsize);
if (!p) {
DEBUG(5,("array_promote: malloc fail\n"));
return;
}
memcpy(p,array + element * elsize, elsize);
memmove(array + elsize,array,elsize*element);
memcpy(array,p,elsize);
SAFE_FREE(p);
}
/*******************************************************************
Return the absolute current directory path - given a UNIX pathname.
Note that this path is returned in DOS format, not UNIX
format. Note this can be called with conn == NULL.
********************************************************************/
char *vfs_GetWd(connection_struct *conn, char *path)
{
pstring s;
static BOOL getwd_cache_init = False;
SMB_STRUCT_STAT st, st2;
int i;
*s = 0;
if (!use_getwd_cache)
return(vfs_getwd(conn,path));
/* init the cache */
if (!getwd_cache_init) {
getwd_cache_init = True;
for (i=0;i<MAX_GETWDCACHE;i++) {
string_set(&ino_list[i].dos_path,"");
ino_list[i].valid = False;
}
}
/* Get the inode of the current directory, if this doesn't work we're
in trouble :-) */
if (vfs_stat(conn, ".",&st) == -1) {
DEBUG(0,("Very strange, couldn't stat \".\" path=%s\n", path));
return(vfs_getwd(conn,path));
}
for (i=0; i<MAX_GETWDCACHE; i++) {
if (ino_list[i].valid) {
/* If we have found an entry with a matching inode and dev number
then find the inode number for the directory in the cached string.
If this agrees with that returned by the stat for the current
directory then all is o.k. (but make sure it is a directory all
the same...) */
if (st.st_ino == ino_list[i].inode && st.st_dev == ino_list[i].dev) {
if (vfs_stat(conn,ino_list[i].dos_path,&st2) == 0) {
if (st.st_ino == st2.st_ino && st.st_dev == st2.st_dev &&
(st2.st_mode & S_IFMT) == S_IFDIR) {
pstrcpy (path, ino_list[i].dos_path);
/* promote it for future use */
array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
return (path);
} else {
/* If the inode is different then something's changed,
scrub the entry and start from scratch. */
ino_list[i].valid = False;
}
}
}
}
}
/* We don't have the information to hand so rely on traditional methods.
The very slow getcwd, which spawns a process on some systems, or the
not quite so bad getwd. */
if (!vfs_getwd(conn,s)) {
DEBUG(0,("vfs_GetWd: vfs_getwd call failed, errno %s\n",strerror(errno)));
return (NULL);
}
pstrcpy(path,s);
DEBUG(5,("vfs_GetWd %s, inode %.0f, dev %.0f\n",s,(double)st.st_ino,(double)st.st_dev));
/* add it to the cache */
i = MAX_GETWDCACHE - 1;
string_set(&ino_list[i].dos_path,s);
ino_list[i].dev = st.st_dev;
ino_list[i].inode = st.st_ino;
ino_list[i].valid = True;
/* put it at the top of the list */
array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
return (path);
}
/* check if the file 'nmae' is a symlink, in that case check that it point to
a file that reside under the 'dir' tree */
static BOOL readlink_check(connection_struct *conn, const char *dir, char *name)
{
BOOL ret = True;
pstring flink;
pstring cleanlink;
pstring savedir;
pstring realdir;
size_t reallen;
if (!vfs_GetWd(conn, savedir)) {
DEBUG(0,("couldn't vfs_GetWd for %s %s\n", name, dir));
return False;
}
if (vfs_ChDir(conn, dir) != 0) {
DEBUG(0,("couldn't vfs_ChDir to %s\n", dir));
return False;
}
if (!vfs_GetWd(conn, realdir)) {
DEBUG(0,("couldn't vfs_GetWd for %s\n", dir));
vfs_ChDir(conn, savedir);
return(False);
}
reallen = strlen(realdir);
if (realdir[reallen -1] == '/') {
reallen--;
realdir[reallen] = 0;
}
if (conn->vfs_ops.readlink(conn, name, flink, sizeof(pstring) -1) != -1) {
DEBUG(3,("reduce_name: file path name %s is a symlink\nChecking it's path\n", name));
if (*flink == '/') {
pstrcpy(cleanlink, flink);
} else {
pstrcpy(cleanlink, realdir);
pstrcat(cleanlink, "/");
pstrcat(cleanlink, flink);
}
unix_clean_name(cleanlink);
if (strncmp(cleanlink, realdir, reallen) != 0) {
DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n", name, realdir, cleanlink, (int)reallen));
ret = False;
}
}
vfs_ChDir(conn, savedir);
return ret;
}
/*******************************************************************
Reduce a file name, removing .. elements and checking that
it is below dir in the heirachy. This uses vfs_GetWd() and so must be run
on the system that has the referenced file system.
Widelinks are allowed if widelinks is true.
********************************************************************/
BOOL reduce_name(connection_struct *conn, pstring s, const char *dir,BOOL widelinks)
{
#ifndef REDUCE_PATHS
return True;
#else
pstring dir2;
pstring wd;
pstring base_name;
pstring newname;
char *p=NULL;
BOOL relative = (*s != '/');
*dir2 = *wd = *base_name = *newname = 0;
if (widelinks) {
unix_clean_name(s);
/* can't have a leading .. */
if (strncmp(s,"..",2) == 0 && (s[2]==0 || s[2]=='/')) {
DEBUG(3,("Illegal file name? (%s)\n",s));
return(False);
}
if (strlen(s) == 0)
pstrcpy(s,"./");
return(True);
}
DEBUG(3,("reduce_name [%s] [%s]\n",s,dir));
/* remove any double slashes */
all_string_sub(s,"//","/",0);
pstrcpy(base_name,s);
p = strrchr_m(base_name,'/');
if (!p)
return readlink_check(conn, dir, s);
if (!vfs_GetWd(conn,wd)) {
DEBUG(0,("couldn't vfs_GetWd for %s %s\n",s,dir));
return(False);
}
if (vfs_ChDir(conn,dir) != 0) {
DEBUG(0,("couldn't vfs_ChDir to %s\n",dir));
return(False);
}
if (!vfs_GetWd(conn,dir2)) {
DEBUG(0,("couldn't vfs_GetWd for %s\n",dir));
vfs_ChDir(conn,wd);
return(False);
}
if (p && (p != base_name)) {
*p = 0;
if (strcmp(p+1,".")==0)
p[1]=0;
if (strcmp(p+1,"..")==0)
*p = '/';
}
if (vfs_ChDir(conn,base_name) != 0) {
vfs_ChDir(conn,wd);
DEBUG(3,("couldn't vfs_ChDir for %s %s basename=%s\n",s,dir,base_name));
return(False);
}
if (!vfs_GetWd(conn,newname)) {
vfs_ChDir(conn,wd);
DEBUG(2,("couldn't get vfs_GetWd for %s %s\n",s,base_name));
return(False);
}
if (p && (p != base_name)) {
pstrcat(newname,"/");
pstrcat(newname,p+1);
}
{
size_t l = strlen(dir2);
if (dir2[l-1] == '/')
l--;
if (strncmp(newname,dir2,l) != 0) {
vfs_ChDir(conn,wd);
DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,(int)l));
return(False);
}
if (!readlink_check(conn, dir, newname)) {
DEBUG(2, ("Bad access attemt? %s is a symlink outside the share path", s));
return(False);
}
if (relative) {
if (newname[l] == '/')
pstrcpy(s,newname + l + 1);
else
pstrcpy(s,newname+l);
} else
pstrcpy(s,newname);
}
vfs_ChDir(conn,wd);
if (strlen(s) == 0)
pstrcpy(s,"./");
DEBUG(3,("reduced to %s\n",s));
return(True);
#endif
}
|