/
fsio.c
7268 lines (5812 loc) · 166 KB
/
fsio.c
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
/*
* ProFTPD - FTP server daemon
* Copyright (c) 1997, 1998 Public Flood Software
* Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
* Copyright (c) 2001-2016 The ProFTPD Project
*
* 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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
* and other respective copyright holders give permission to link this program
* with OpenSSL, and distribute the resulting executable, without including
* the source code for OpenSSL in the source distribution.
*/
/* ProFTPD virtual/modular file-system support */
#include "conf.h"
#include "privs.h"
#ifdef HAVE_SYS_STATVFS_H
# include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_VFS_H
# include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
# include <sys/mount.h>
#endif
#ifdef AIX3
# include <sys/statfs.h>
#endif
#ifdef HAVE_ACL_LIBACL_H
# include <acl/libacl.h>
#endif
/* For determining whether a file is on an NFS filesystem. Note that
* this value is Linux specific. See Bug#3874 for details.
*/
#ifndef NFS_SUPER_MAGIC
# define NFS_SUPER_MAGIC 0x6969
#endif
typedef struct fsopendir fsopendir_t;
struct fsopendir {
fsopendir_t *next,*prev;
/* pool for this object's use */
pool *pool;
pr_fs_t *fsdir;
DIR *dir;
};
static const char *trace_channel = "fsio";
static pr_fs_t *root_fs = NULL, *fs_cwd = NULL;
static array_header *fs_map = NULL;
#ifdef PR_FS_MATCH
static pr_fs_match_t *fs_match_list = NULL;
#endif /* PR_FS_MATCH */
static fsopendir_t *fsopendir_list;
static void *fs_cache_dir = NULL;
static pr_fs_t *fs_cache_fsdir = NULL;
/* Internal flag set whenever a new pr_fs_t has been added or removed, and
* cleared once the fs_map has been scanned
*/
static unsigned char chk_fs_map = FALSE;
/* Virtual working directory */
static char vwd[PR_TUNABLE_PATH_MAX + 1] = "/";
static char cwd[PR_TUNABLE_PATH_MAX + 1] = "/";
static size_t cwd_len = 1;
static int fsio_guard_chroot = FALSE;
/* Runtime enabling/disabling of mkdtemp(3) use. */
#ifdef HAVE_MKDTEMP
static int fsio_use_mkdtemp = TRUE;
#else
static int fsio_use_mkdtemp = FALSE;
#endif /* HAVE_MKDTEMP */
/* Runtime enabling/disabling of encoding of paths. */
static int use_encoding = TRUE;
/* Guard against attacks like "Roaring Beast" when we are chrooted. See:
*
* https://auscert.org.au/15286
* https://auscert.org.au/15526
*
* Currently, we guard the /etc and /lib directories.
*/
static int chroot_allow_path(const char *path) {
size_t path_len;
int res = 0;
/* Note: we expect to get (and DO get) the absolute path here. Should that
* ever not be the case, this check will not work.
*/
path_len = strlen(path);
if (path_len < 4) {
/* Path is not long enough to include one of the guarded directories. */
return 0;
}
if (path_len == 4) {
if (strcmp(path, "/etc") == 0 ||
strcmp(path, "/lib") == 0) {
res = -1;
}
} else {
if (strncmp(path, "/etc/", 5) == 0 ||
strncmp(path, "/lib/", 5) == 0) {
res = -1;
}
}
if (res < 0) {
pr_trace_msg(trace_channel, 1, "rejecting path '%s' within chroot '%s'",
path, session.chroot_path);
pr_log_debug(DEBUG2,
"WARNING: attempt to use sensitive path '%s' within chroot '%s', "
"rejecting", path, session.chroot_path);
errno = EACCES;
}
return res;
}
/* The following static functions are simply wrappers for system functions
*/
static int sys_stat(pr_fs_t *fs, const char *path, struct stat *sbuf) {
return stat(path, sbuf);
}
static int sys_fstat(pr_fh_t *fh, int fd, struct stat *sbuf) {
return fstat(fd, sbuf);
}
static int sys_lstat(pr_fs_t *fs, const char *path, struct stat *sbuf) {
return lstat(path, sbuf);
}
static int sys_rename(pr_fs_t *fs, const char *rnfm, const char *rnto) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(rnfm);
if (res < 0) {
return -1;
}
res = chroot_allow_path(rnto);
if (res < 0) {
return -1;
}
}
res = rename(rnfm, rnto);
return res;
}
static int sys_unlink(pr_fs_t *fs, const char *path) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(path);
if (res < 0) {
return -1;
}
}
res = unlink(path);
return res;
}
static int sys_open(pr_fh_t *fh, const char *path, int flags) {
int res;
#ifdef O_BINARY
/* On Cygwin systems, we need the open(2) equivalent of fopen(3)'s "b"
* option. Cygwin defines an O_BINARY flag for this purpose.
*/
flags |= O_BINARY;
#endif
if (fsio_guard_chroot) {
/* If we are creating (or truncating) a file, then we need to check.
* Note: should O_RDWR be added to this list?
*/
if (flags & (O_APPEND|O_CREAT|O_TRUNC|O_WRONLY)) {
res = chroot_allow_path(path);
if (res < 0) {
return -1;
}
}
}
res = open(path, flags, PR_OPEN_MODE);
return res;
}
static int sys_creat(pr_fh_t *fh, const char *path, mode_t mode) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(path);
if (res < 0) {
return -1;
}
}
res = creat(path, mode);
return res;
}
static int sys_close(pr_fh_t *fh, int fd) {
return close(fd);
}
static int sys_read(pr_fh_t *fh, int fd, char *buf, size_t size) {
return read(fd, buf, size);
}
static int sys_write(pr_fh_t *fh, int fd, const char *buf, size_t size) {
return write(fd, buf, size);
}
static off_t sys_lseek(pr_fh_t *fh, int fd, off_t offset, int whence) {
return lseek(fd, offset, whence);
}
static int sys_link(pr_fs_t *fs, const char *target_path,
const char *link_path) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(link_path);
if (res < 0) {
return -1;
}
}
res = link(target_path, link_path);
return res;
}
static int sys_symlink(pr_fs_t *fs, const char *target_path,
const char *link_path) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(link_path);
if (res < 0) {
return -1;
}
}
res = symlink(target_path, link_path);
return res;
}
static int sys_readlink(pr_fs_t *fs, const char *path, char *buf,
size_t buflen) {
return readlink(path, buf, buflen);
}
static int sys_ftruncate(pr_fh_t *fh, int fd, off_t len) {
return ftruncate(fd, len);
}
static int sys_truncate(pr_fs_t *fs, const char *path, off_t len) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(path);
if (res < 0) {
return -1;
}
}
res = truncate(path, len);
return res;
}
static int sys_chmod(pr_fs_t *fs, const char *path, mode_t mode) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(path);
if (res < 0) {
return -1;
}
}
res = chmod(path, mode);
return res;
}
static int sys_fchmod(pr_fh_t *fh, int fd, mode_t mode) {
return fchmod(fd, mode);
}
static int sys_chown(pr_fs_t *fs, const char *path, uid_t uid, gid_t gid) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(path);
if (res < 0) {
return -1;
}
}
res = chown(path, uid, gid);
return res;
}
static int sys_fchown(pr_fh_t *fh, int fd, uid_t uid, gid_t gid) {
return fchown(fd, uid, gid);
}
static int sys_lchown(pr_fs_t *fs, const char *path, uid_t uid, gid_t gid) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(path);
if (res < 0) {
return -1;
}
}
res = lchown(path, uid, gid);
return res;
}
/* We provide our own equivalent of access(2) here, rather than using
* access(2) directly, because access(2) uses the real IDs, rather than
* the effective IDs, of the process.
*/
static int sys_access(pr_fs_t *fs, const char *path, int mode, uid_t uid,
gid_t gid, array_header *suppl_gids) {
mode_t mask;
struct stat st;
if (pr_fsio_stat(path, &st) < 0) {
return -1;
}
/* Root always succeeds. */
if (uid == PR_ROOT_UID) {
return 0;
}
/* Initialize mask to reflect the permission bits that are applicable for
* the given user. mask contains the user-bits if the user ID equals the
* ID of the file owner. mask contains the group bits if the group ID
* belongs to the group of the file. mask will always contain the other
* bits of the permission bits.
*/
mask = S_IROTH|S_IWOTH|S_IXOTH;
if (st.st_uid == uid) {
mask |= S_IRUSR|S_IWUSR|S_IXUSR;
}
/* Check the current group, as well as all supplementary groups.
* Fortunately, we have this information cached, so accessing it is
* almost free.
*/
if (st.st_gid == gid) {
mask |= S_IRGRP|S_IWGRP|S_IXGRP;
} else {
if (suppl_gids) {
register unsigned int i = 0;
for (i = 0; i < suppl_gids->nelts; i++) {
if (st.st_gid == ((gid_t *) suppl_gids->elts)[i]) {
mask |= S_IRGRP|S_IWGRP|S_IXGRP;
break;
}
}
}
}
mask &= st.st_mode;
/* Perform requested access checks. */
if (mode & R_OK) {
if (!(mask & (S_IRUSR|S_IRGRP|S_IROTH))) {
errno = EACCES;
return -1;
}
}
if (mode & W_OK) {
if (!(mask & (S_IWUSR|S_IWGRP|S_IWOTH))) {
errno = EACCES;
return -1;
}
}
if (mode & X_OK) {
if (!(mask & (S_IXUSR|S_IXGRP|S_IXOTH))) {
errno = EACCES;
return -1;
}
}
/* F_OK already checked by checking the return value of stat. */
return 0;
}
static int sys_faccess(pr_fh_t *fh, int mode, uid_t uid, gid_t gid,
array_header *suppl_gids) {
return sys_access(fh->fh_fs, fh->fh_path, mode, uid, gid, suppl_gids);
}
static int sys_utimes(pr_fs_t *fs, const char *path, struct timeval *tvs) {
int res;
if (fsio_guard_chroot) {
res = chroot_allow_path(path);
if (res < 0) {
return -1;
}
}
res = utimes(path, tvs);
return res;
}
static int sys_futimes(pr_fh_t *fh, int fd, struct timeval *tvs) {
#ifdef HAVE_FUTIMES
int res;
/* Check for an ENOSYS errno; if so, fallback to using sys_utimes. Some
* platforms will provide a futimes(2) stub which does not actually do
* anything.
*/
res = futimes(fd, tvs);
if (res < 0 &&
errno == ENOSYS) {
return sys_utimes(fh->fh_fs, fh->fh_path, tvs);
}
return res;
#else
return sys_utimes(fh->fh_fs, fh->fh_path, tvs);
#endif
}
static int sys_fsync(pr_fh_t *fh, int fd) {
int res;
#ifdef HAVE_FSYNC
res = fsync(fd);
#else
errno = ENOSYS;
res = -1;
#endif /* HAVE_FSYNC */
return res;
}
static ssize_t sys_getxattr(pool *p, pr_fs_t *fs, const char *path,
const char *name, void *val, size_t valsz) {
ssize_t res;
(void) p;
#ifdef PR_USE_XATTR
# if defined(HAVE_SYS_EXTATTR_H)
res = extattr_get_file(path, EXTATTR_NAMESPACE_USER, name, val, valsz);
# elif defined(HAVE_SYS_XATTR_H)
# if defined(XATTR_NOFOLLOW)
res = getxattr(path, name, val, valsz, 0, 0);
# else
res = getxattr(path, name, val, valsz);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
#else
(void) fs;
(void) path;
(void) name;
(void) val;
(void) valsz;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return res;
}
static ssize_t sys_lgetxattr(pool *p, pr_fs_t *fs, const char *path,
const char *name, void *val, size_t valsz) {
ssize_t res;
(void) p;
#ifdef PR_USE_XATTR
# if defined(HAVE_SYS_EXTATTR_H)
# if defined(HAVE_EXTATTR_GET_LINK)
res = extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, val, valsz);
# else
res = extattr_get_file(path, EXTATTR_NAMESPACE_USER, name, val, valsz);
# endif /* HAVE_EXTATTR_GET_LINK */
# elif defined(HAVE_SYS_XATTR_H)
# if defined(HAVE_LGETXATTR)
res = lgetxattr(path, name, val, valsz);
# elif defined(XATTR_NOFOLLOW)
res = getxattr(path, name, val, valsz, 0, XATTR_NOFOLLOW);
# else
res = getxattr(path, name, val, valsz);
# endif /* HAVE_LGETXATTR */
# endif /* HAVE_SYS_XATTR_H */
#else
(void) fs;
(void) path;
(void) name;
(void) val;
(void) valsz;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return res;
}
static ssize_t sys_fgetxattr(pool *p, pr_fh_t *fh, int fd, const char *name,
void *val, size_t valsz) {
ssize_t res;
(void) p;
#ifdef PR_USE_XATTR
# if defined(HAVE_SYS_EXTATTR_H)
res = extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, name, val, valsz);
# elif defined(HAVE_SYS_XATTR_H)
# if defined(XATTR_NOFOLLOW)
res = fgetxattr(fd, name, val, valsz, 0, 0);
# else
res = fgetxattr(fd, name, val, valsz);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
#else
(void) fh;
(void) fd;
(void) name;
(void) val;
(void) valsz;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return res;
}
#ifdef PR_USE_XATTR
static array_header *parse_xattr_namelist(pool *p, char *namelist, size_t sz) {
array_header *names;
char *ptr;
names = make_array(p, 0, sizeof(char *));
ptr = namelist;
# if defined(HAVE_SYS_EXTATTR_H)
/* BSD style name lists use a one-byte length prefix (limiting xattr names
* to a maximum length of 255 bytes), followed by the name, without any
* terminating NUL.
*/
while (sz > 0) {
unsigned char len;
pr_signals_handle();
len = (unsigned char) *ptr;
ptr++;
sz--;
*((char **) push_array(names)) = pstrndup(p, ptr, len);
ptr += len;
sz -= len;
}
# elif defined(HAVE_SYS_XATTR_H)
/* Linux/MacOSX style name lists use NUL-terminated xattr names. */
while (sz > 0) {
char *ptr2;
size_t len;
pr_signals_handle();
for (ptr2 = ptr; *ptr2; ptr2++);
len = ptr2 - ptr;
*((char **) push_array(names)) = pstrndup(p, ptr, len);
ptr = ptr2 + 1;
sz -= (len + 1);
}
# endif /* HAVE_SYS_XATTR_H */
return names;
}
static ssize_t unix_listxattr(const char *path, char *namelist, size_t len) {
ssize_t res;
#if defined(HAVE_SYS_EXTATTR_H)
res = extattr_list_file(path, EXTATTR_NAMESPACE_USER, namelist, len);
#elif defined(HAVE_SYS_XATTR_H)
# if defined(XATTR_NOFOLLOW)
res = listxattr(path, namelist, len, 0);
# else
res = listxattr(path, namelist, len);
# endif /* XATTR_NOFOLLOW */
#endif /* HAVE_SYS_XATTR_H */
return res;
}
static ssize_t unix_llistxattr(const char *path, char *namelist, size_t len) {
ssize_t res;
# if defined(HAVE_SYS_EXTATTR_H)
# if defined(HAVE_EXTATTR_LIST_LINK)
res = extattr_list_link(path, EXTATTR_NAMESPACE_USER, namelist, len);
# else
res = extattr_list_file(path, EXTATTR_NAMESPACE_USER, namelist, len);
# endif /* HAVE_EXTATTR_LIST_LINK */
# elif defined(HAVE_SYS_XATTR_H)
# if defined(HAVE_LLISTXATTR)
res = llistxattr(path, namelist, len);
# elif defined(XATTR_NOFOLLOW)
res = listxattr(path, namelist, len, XATTR_NOFOLLOW);
# else
res = listxattr(path, namelist, len);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
return res;
}
static ssize_t unix_flistxattr(int fd, char *namelist, size_t len) {
ssize_t res;
# if defined(HAVE_SYS_EXTATTR_H)
res = extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, namelist, len);
# elif defined(HAVE_SYS_XATTR_H)
# if defined(XATTR_NOFOLLOW)
res = flistxattr(fd, namelist, len, 0);
# else
res = flistxattr(fd, namelist, len);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
return res;
}
#endif /* PR_USE_XATTR */
static int sys_listxattr(pool *p, pr_fs_t *fs, const char *path,
array_header **names) {
ssize_t res;
char *namelist = NULL;
size_t len = 0;
#ifdef PR_USE_XATTR
/* We need to handle the different formats of namelists that listxattr et al
* can provide. On *BSDs, the namelist buffer uses length prefixes and no
* terminating NULs; on Linux/Mac, the namelist buffer uses ONLY
* NUL-terminated names.
*
* Thus we ALWAYS provide all the available attribute names, by first
* querying for the full namelist buffer size, allocating that out of
* given pool, querying for the names (using the buffer), and then parsing
* them into an array.
*/
res = unix_listxattr(path, NULL, 0);
if (res < 0) {
return -1;
}
len = res;
namelist = palloc(p, len);
res = unix_listxattr(path, namelist, len);
if (res < 0) {
return -1;
}
*names = parse_xattr_namelist(p, namelist, len);
res = (*names)->nelts;
#else
(void) fs;
(void) path;
(void) names;
(void) namelist;
(void) len;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return (int) res;
}
static int sys_llistxattr(pool *p, pr_fs_t *fs, const char *path,
array_header **names) {
ssize_t res;
char *namelist = NULL;
size_t len = 0;
#ifdef PR_USE_XATTR
/* See sys_listxattr for a description of why we use this approach. */
res = unix_llistxattr(path, NULL, 0);
if (res < 0) {
return -1;
}
len = res;
namelist = palloc(p, len);
res = unix_llistxattr(path, namelist, len);
if (res < 0) {
return -1;
}
*names = parse_xattr_namelist(p, namelist, len);
res = (*names)->nelts;
#else
(void) fs;
(void) path;
(void) names;
(void) namelist;
(void) len;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return (int) res;
}
static int sys_flistxattr(pool *p, pr_fh_t *fh, int fd, array_header **names) {
ssize_t res;
char *namelist = NULL;
size_t len = 0;
#ifdef PR_USE_XATTR
/* See sys_listxattr for a description of why we use this approach. */
res = unix_flistxattr(fd, NULL, 0);
if (res < 0) {
return -1;
}
len = res;
namelist = palloc(p, len);
res = unix_flistxattr(fd, namelist, len);
if (res < 0) {
return -1;
}
*names = parse_xattr_namelist(p, namelist, len);
res = (*names)->nelts;
#else
(void) fh;
(void) fd;
(void) names;
(void) namelist;
(void) len;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return (int) res;
}
static int sys_removexattr(pool *p, pr_fs_t *fs, const char *path,
const char *name) {
int res;
(void) p;
#ifdef PR_USE_XATTR
# if defined(HAVE_SYS_EXTATTR_H)
res = extattr_delete_file(path, EXTATTR_NAMESPACE_USER, name);
# elif defined(HAVE_SYS_XATTR_H)
# if defined(XATTR_NOFOLLOW)
res = removexattr(path, name, 0);
# else
res = removexattr(path, name);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
#else
(void) fs;
(void) path;
(void) name;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return res;
}
static int sys_lremovexattr(pool *p, pr_fs_t *fs, const char *path,
const char *name) {
int res;
(void) p;
#ifdef PR_USE_XATTR
# if defined(HAVE_SYS_EXTATTR_H)
# if defined(HAVE_EXTATTR_DELETE_LINK)
res = extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
# else
res = extattr_delete_file(path, EXTATTR_NAMESPACE_USER, name);
# endif /* HAVE_EXTATTR_DELETE_LINK */
# elif defined(HAVE_SYS_XATTR_H)
# if defined(HAVE_LREMOVEXATTR)
res = lremovexattr(path, name);
# elif defined(XATTR_NOFOLLOW)
res = removexattr(path, name, XATTR_NOFOLLOW);
# else
res = removexattr(path, name);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
#else
(void) fs;
(void) path;
(void) name;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return res;
}
static int sys_fremovexattr(pool *p, pr_fh_t *fh, int fd, const char *name) {
int res;
(void) p;
#ifdef PR_USE_XATTR
# if defined(HAVE_SYS_EXTATTR_H)
res = extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, name);
# elif defined(HAVE_SYS_XATTR_H)
# if defined(XATTR_NOFOLLOW)
res = fremovexattr(fd, name, 0);
# else
res = fremovexattr(fd, name);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
#else
(void) fh;
(void) fd;
(void) name;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return res;
}
#ifdef PR_USE_XATTR
/* Map the given flags onto the sys/xattr.h flags */
static int get_setxattr_flags(int fsio_flags) {
int xattr_flags = 0;
/* If both CREATE and REPLACE are set, use a value of zero; per the
* man pages, this value gives the desired "create or replace" semantics.
* Right?
*/
if (fsio_flags & PR_FSIO_XATTR_FL_CREATE) {
#if defined(XATTR_CREATE)
xattr_flags = XATTR_CREATE;
#endif /* XATTR_CREATE */
if (fsio_flags & PR_FSIO_XATTR_FL_REPLACE) {
xattr_flags = 0;
}
} else if (fsio_flags & PR_FSIO_XATTR_FL_REPLACE) {
#if defined(XATTR_REPLACE)
xattr_flags = XATTR_REPLACE;
#endif /* XATTR_REPLACE */
}
return xattr_flags;
}
#endif /* PR_USE_XATTR */
static int sys_setxattr(pool *p, pr_fs_t *fs, const char *path,
const char *name, void *val, size_t valsz, int flags) {
int res, xattr_flags = 0;
(void) p;
#ifdef PR_USE_XATTR
# if defined(HAVE_SYS_EXTATTR_H)
(void) xattr_flags;
res = extattr_set_file(path, EXTATTR_NAMESPACE_USER, name, val, valsz);
# elif defined(HAVE_SYS_XATTR_H)
xattr_flags = get_setxattr_flags(flags);
# if defined(XATTR_NOFOLLOW)
res = setxattr(path, name, val, valsz, 0, flags);
# else
res = setxattr(path, name, val, valsz, flags);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
#else
(void) fs;
(void) path;
(void) name;
(void) val;
(void) valsz;
(void) flags;
(void) xattr_flags;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return res;
}
static int sys_lsetxattr(pool *p, pr_fs_t *fs, const char *path,
const char *name, void *val, size_t valsz, int flags) {
int res, xattr_flags = 0;
(void) p;
#ifdef PR_USE_XATTR
# if defined(HAVE_SYS_EXTATTR_H)
(void) xattr_flags;
# if defined(HAVE_EXTATTR_SET_LINK)
res = extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, val, valsz);
# else
res = extattr_set_file(path, EXTATTR_NAMESPACE_USER, name, val, valsz);
# endif /* HAVE_EXTATTR_SET_LINK */
# elif defined(HAVE_SYS_XATTR_H)
xattr_flags = get_setxattr_flags(flags);
# if defined(HAVE_LSETXATTR)
res = lsetxattr(path, name, val, valsz, flags);
# elif defined(XATTR_NOFOLLOW)
xattr_flags |= XATTR_NOFOLLOW;
res = setxattr(path, name, val, valsz, 0, flags);
# else
res = setxattr(path, name, val, valsz, flags);
# endif /* XATTR_NOFOLLOW */
# endif /* HAVE_SYS_XATTR_H */
#else
(void) fs;
(void) path;
(void) name;
(void) val;
(void) valsz;
(void) flags;
(void) xattr_flags;
errno = ENOSYS;
res = -1;
#endif /* PR_USE_XATTR */
return res;
}