The Samba-Bugzilla – Attachment 17935 Details for
Bug 15376
Fix pathref file handling to make Samba usable on FreeBSD 13+
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
freebsd test program to verify fdescfs/O_PATH/acl/xattrs functionality
freebsd_tst.c (text/x-csrc), 13.31 KB, created by
Peter Eriksson
on 2023-06-21 18:45:02 UTC
(
hide
)
Description:
freebsd test program to verify fdescfs/O_PATH/acl/xattrs functionality
Filename:
MIME Type:
Creator:
Peter Eriksson
Created:
2023-06-21 18:45:02 UTC
Size:
13.31 KB
patch
obsolete
>/* > * freebsd_fdescfs_opath_acl_extattr_test.c > * > * Compile with: > * cc -o tst tst.c -I/usr/local/include -L/usr/local/lib -lsunacl > * > * Requires: > * pkg add libsunacl > * fdescfs mounted as /compat/linux/dev/fd with option linrdlnk enabled > > * Usage: > * ./tst <options> <basedir> <path> > * > * Options: > * ./tst -h > * > * Examples: > * ./tst -axrd /home/peter86 testsubdir > * ./tst -ssaxrd /home/peter86 testsubdir > */ > >#include <stdio.h> >#include <fcntl.h> >#include <errno.h> >#include <stdlib.h> >#include <string.h> >#include <unistd.h> >#include <time.h> >#include <ctype.h> >#include <sys/types.h> >#include <sys/stat.h> >#include <sunacl.h> >#include <sys/extattr.h> > > >int f_stat = 0; >int f_dir = 0; >int f_read = 0; >int f_write = 0; >int f_acl = 0; >int f_attr = 0; >int f_user = 0; > >char * >mode2type(mode_t m) { > if (S_ISDIR(m)) > return "dir"; > > if (S_ISLNK(m)) > return "symlink"; > > if (S_ISBLK(m)) > return "block"; > > if (S_ISCHR(m)) > return "char"; > > if (S_ISREG(m)) > return "file"; > > return "other"; >} > > >int >test_acl_fd(char *pfx, > int fd) { > char *acebuf; > int rc, na; > > > if (!f_acl) > return 0; > > na = facl(fd, ACE_GETACLCNT, 0, NULL); > if (na < 0) { > printf("%sfacl(%d, ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", > pfx, fd, na, errno, strerror(errno)); > return -1; > } > printf("%sfacl(%d, ACE_GETACLCNT) -> %d\n", pfx, fd, na); > > if (f_read || f_write) { > acebuf = malloc(na*sizeof(ace_t)); > if (!acebuf) > abort(); > > rc = facl(fd, ACE_GETACL, na, acebuf); > if (rc < 0) { > printf("%sfacl(%d, ACE_GETACL) -> %d [errno=%d (%s)]\n", > pfx, fd, na, errno, strerror(errno)); > free(acebuf); > return -2; > } > printf("%sfacl(%d, ACE_GETACL) -> %d\n", pfx, fd, na); > > > if (f_write) { > rc = facl(fd, ACE_SETACL, na, acebuf); > if (rc < 0) { > printf("%sfacl(%d, ACE_SETACL) -> %d [errno=%d (%s)]\n", > pfx, fd, na, errno, strerror(errno)); > free(acebuf); > return -3; > } > printf("%sfacl(%d, ACE_SETACL) -> %d\n", pfx, fd, na); > } > > free(acebuf); > } > > return 0; >} > > >int >test_acl_file(char *pfx, > char *path) { > char *acebuf; > int rc, na; > > > if (!f_acl) > return 0; > > na = acl(path, ACE_GETACLCNT, 0, NULL); > if (na < 0) { > printf("%sacl(\"%s\", ACE_GETACLCNT) -> %d [errno=%d (%s)]\n", > pfx, path, na, errno, strerror(errno)); > return -1; > } > printf("%sacl(\"%s\", ACE_GETACLCNT) -> %d\n", pfx, path, na); > > if (f_read || f_write) { > acebuf = malloc(na*sizeof(ace_t)); > if (!acebuf) > abort(); > > rc = acl(path, ACE_GETACL, na, acebuf); > if (rc < 0) { > printf("%sacl(\"%s\", ACE_GETACL) -> %d [errno=%d (%s)]\n", > pfx, path, na, errno, strerror(errno)); > free(acebuf); > return -2; > } > printf("%sacl(\"%s\", ACE_GETACL) -> %d\n", pfx, path, na); > > > if (f_write) { > rc = acl(path, ACE_SETACL, na, acebuf); > if (rc < 0) { > printf("%sacl(\"%s\", ACE_SETACL) -> %d [errno=%d (%s)]\n", > pfx, path, na, errno, strerror(errno)); > free(acebuf); > return -3; > } > printf("%sacl(\"%s\", ACE_SETACL) -> %d\n", pfx, path, na); > } > > free(acebuf); > } > > return 0; >} > > > >void >list_attrnames(char *pfx, > void *buf, > ssize_t len, > char *attrname) { > unsigned char *bp = (unsigned char *) buf; > int na = 0; > > > while (len > 0 && *bp != 0) { > if (attrname) { > memcpy(attrname, bp+1, (int) *bp); > attrname[*bp] = '\0'; > } > > printf("%s - %.*s\n", pfx, (int) *bp, bp+1); > len -= *bp+1; > bp += *bp+1; > } >} > > >void >print_attr(int len, > unsigned char *buf) { > int i; > > for (i = 0; i < len && isprint(buf[i]); i++) > ; > if (i >= len) > printf("\"%.*s\"", len, buf); > else > for (i = 0; i < len; i++) > printf("%02X ", buf[i]); >} > >int >test_attr_fd(char *pfx, > int fd) { > int rc; > > if (!f_attr) > return 0; > > rc = extattr_list_fd(fd, EXTATTR_NAMESPACE_SYSTEM, NULL, 0); > if (rc < 0) { > printf("%sextattr_list_fd(%d, EXTATTR_NAMESPACE_SYSTEM, NULL, 0) -> %d [errno=%d (%s)]\n", > pfx, fd, rc, errno, strerror(errno)); > } else { > char *buf; > int len; > > > printf("%sextattr_list_fd(%d, EXTATTR_NAMESPACE_SYSTEM, NULL, 0) -> %d\n", > pfx, fd, rc); > > if (f_read) { > len = rc; > buf = malloc(len); > rc = extattr_list_fd(fd, EXTATTR_NAMESPACE_SYSTEM, buf, len); > if (rc < 0) { > printf("%sextattr_list_fd(%d, EXTATTR_NAMESPACE_SYSTEM, %p, %d) -> %d [errno=%d (%s)]\n", > pfx, fd, buf, len, rc, errno, strerror(errno)); > } else { > list_attrnames(pfx, buf, rc, NULL); > } > free(buf); > } > } > > > rc = extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, NULL, 0); > if (rc < 0) { > printf("%sextattr_list_fd(%d, EXTATTR_NAMESPACE_USER, NULL, 0) -> %d [errno=%d (%s)]\n", > pfx, fd, rc, errno, strerror(errno)); > } else { > char *buf; > int len; > > > printf("%sextattr_list_fd(%d, EXTATTR_NAMESPACE_USER, NULL, 0) -> %d\n", > pfx, fd, rc); > > if (f_read) { > char attrname[257]; > unsigned char databuf[256]; > int read_test_done = 0; > > > attrname[0] = '\0'; > > len = rc; > buf = malloc(len); > rc = extattr_list_fd(fd, EXTATTR_NAMESPACE_USER, buf, len); > if (rc < 0) { > printf("%sextattr_list_fd(%d, EXTATTR_NAMESPACE_USER, %p, %d) -> %d [errno=%d (%s)]\n", > pfx, fd, buf, len, rc, errno, strerror(errno)); > } else { > printf("%sextattr_list_fd(%d, EXTATTR_NAMESPACE_USER, %p, %d) -> %d\n", > pfx, fd, buf, len, rc); > list_attrnames(pfx, buf, rc, attrname); > } > free(buf); > > if (attrname[0]) { > rc = extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, attrname, databuf, sizeof(databuf)); > if (rc < 0) { > printf("%sextattr_get_fd(%d, EXTATTR_NAMESPACE_USER, \"%s\", %p, %lu) -> %d [errno=%d (%s)]\n", > pfx, fd, attrname, databuf, sizeof(databuf), rc, errno, strerror(errno)); > } else { > printf("%sextattr_get_fd(%d, EXTATTR_NAMESPACE_USER, \"%s\", %p, %lu) -> %d\n", > pfx, fd, attrname, databuf, sizeof(databuf), rc); > printf("%s %s = ", pfx, attrname); > print_attr(rc, databuf); > putchar('\n'); > read_test_done = 1; > } > } > > if (f_write) { > strcpy(attrname, "liu.se-test"); > > rc = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, attrname, databuf, sizeof(databuf)); > if (rc < 0) { > printf("%sextattr_set_fd(%d, EXTATTR_NAMESPACE_USER, %s, %p, %lu) -> %d [errno=%d (%s)]\n", > pfx, fd, attrname, databuf, sizeof(databuf), rc, errno, strerror(errno)); > } else { > printf("%sextattr_set_fd(%d, EXTATTR_NAMESPACE_USER, \"%s\", %p, %lu) -> %d\n", > pfx, fd, attrname, databuf, sizeof(databuf), rc); > > if (!read_test_done) { > rc = extattr_get_fd(fd, EXTATTR_NAMESPACE_USER, attrname, databuf, sizeof(databuf)); > if (rc < 0) { > printf("%sextattr_get_fd(%d, EXTATTR_NAMESPACE_USER, \"%s\", %p, %lu) -> %d [errno=%d (%s)]\n", > pfx, fd, attrname, databuf, sizeof(databuf), rc, errno, strerror(errno)); > } else { > printf("%sextattr_get_fd(%d, EXTATTR_NAMESPACE_USER, \"%s\", %p, %lu) -> %d\n", > pfx, fd, attrname, databuf, sizeof(databuf), rc); > printf("%s %s = ", pfx, attrname); > print_attr(rc, databuf); > putchar('\n'); > } > } > > rc = extattr_delete_fd(fd, EXTATTR_NAMESPACE_USER, attrname); > if (rc < 0) { > printf("%sextattr_delete_fd(%d, EXTATTR_NAMESPACE_USER, \"%s\") -> %d [errno=%d (%s)]\n", > pfx, fd, attrname, rc, errno, strerror(errno)); > } else { > printf("%sextattr_delete_fd(%d, EXTATTR_NAMESPACE_USER, \"%s\") -> %d\n", > pfx, fd, attrname, rc); > } > } > } > } > } > > return 0; >} > > >int >test_attr_file(char *pfx, > char *path) { > int rc; > > if (!f_attr) > return 0; > > rc = extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0); > if (rc < 0) { > printf("%sextattr_list_file(\"%s\", EXTATTR_NAMESPACE_USER, NULL, 0) -> %d [errno=%d (%s)]\n", > pfx, path, rc, errno, strerror(errno)); > } else { > printf("%sextattr_list_file(\"%s\", EXTATTR_NAMESPACE_USER, NULL, 0) -> %d\n", > pfx, path, rc); > } > > rc = extattr_list_file(path, EXTATTR_NAMESPACE_SYSTEM, NULL, 0); > if (rc < 0) { > printf("%sextattr_list_file(\"%s\", EXTATTR_NAMESPACE_SYSTEM, NULL, 0) -> %d [errno=%d (%s)]\n", > pfx, path, rc, errno, strerror(errno)); > } else { > printf("%sextattr_list_file(\"%s\", EXTATTR_NAMESPACE_SYSTEM, NULL, 0) -> %d\n", > pfx, path, rc); > } > > return 0; >} > > >int >main(int argc, > char *argv[]) { > int fd, rfd; > char buf[2048]; > int na, rc; > struct stat sb; > int i, j; > int dfd; > char path[2048]; > int uid = 0; > > for (i = 1; i < argc && argv[i][0] == '-'; i++) { > for (j = 1; argv[i][j]; j++) { > switch (argv[i][j]) { > case 'h': > printf("Usage:\n %s [<options>] <basedir> <path>\n\n", argv[0]); > puts("Options:"); > puts(" -h Display this information"); > puts(" -s Do stat() once before using fdescfs fd's"); > puts(" -ss Do stat() twice before using fdescfs fd's"); > puts(" -d Set O_DIRECTORY on open operations"); > puts(" -a Perform ACL tests"); > puts(" -x Perform extattr tests"); > puts(" -r Perform read tests"); > puts(" -w Perform write tests"); > puts(" -u Call seteuid before tests (with basedir owner)"); > puts(" -uu Call setuid before tests (with basedir owner)"); > exit(0); > > case 's': > f_stat++; > break; > case 'd': > f_dir++; > break; > case 'r': > f_read++; > break; > case 'w': > f_write++; > break; > case 'a': > f_acl++; > break; > case 'x': > f_attr++; > break; > case 'u': > f_user++; > break; > default: > fprintf(stderr, "%s: Error: -%c: Invalid switch\n", argv[0], argv[i][j]); > exit(1); > } > } > NextArg:; > } > > if (i+1 >= argc) { > fprintf(stderr, "%s: Error: Missing required <basedir> <path> arguments\n", > argv[0]); > exit(1); > } > > if (f_user) { > if (stat(argv[i], &sb) < 0) { > fprintf(stderr, "%s: Error: stat(\"%s\"): %s\n", > argv[0], argv[i], strerror(errno)); > exit(1); > } > > if (f_user == 1) { > if (seteuid(sb.st_uid) < 0) { > fprintf(stderr, "%s: Error: seteuid(%d): %s\n", > argv[0], sb.st_uid, strerror(errno)); > exit(1); > } else { > if (setuid(sb.st_uid) < 0) { > fprintf(stderr, "%s: Error: setuid(%d): %s\n", > argv[0], sb.st_uid, strerror(errno)); > exit(1); > } > } > } > } > > dfd = open(argv[i], O_PATH); > if (dfd < 0) { > fprintf(stderr, "%s: Error: open(\"%s\", O_PATH): %s\n", > argv[0], argv[i], strerror(errno)); > exit(1); > } else > printf("open(\"%s\", O_PATH) -> %d\n", argv[i], dfd); > > > > test_acl_fd(" ", dfd); > test_attr_fd(" ", dfd); > > > fd = openat(dfd, argv[i+1], O_RDONLY); > if (fd < 0) { > printf(" openat(%d, \"%s\", O_RDONLY) -> %d [errno=%d (%s)]\n", dfd, argv[i+1], fd, errno, strerror(errno)); > } else { > printf(" openat(%d, \"%s\", O_RDONLY) -> %d\n", dfd, argv[i+1], fd); > > test_acl_fd(" ", fd); > test_attr_fd(" ", fd); > > sprintf(buf, "/compat/linux/dev/fd/%d", fd); > > if (f_stat) { > for (i = 0; i < f_stat; i++) { > if ((rc = stat(buf, &sb)) < 0) { > printf(" stat(\"%s\") -> %d [errno=%d (%s)]\n", buf, rc, errno, strerror(errno)); > } else { > printf(" stat(\"%s\") -> %d [type=%s, size=%lu, uid=%u, gid=%u]\n", buf, rc, mode2type(sb.st_mode), sb.st_size, sb.st_uid, sb.st_gid); > if (S_ISLNK(sb.st_mode)) { > rc = readlink(buf, path, sizeof(path)); > if (rc < 0) { > printf(" readlink(\"%s\") -> %d [errno=%d (%s)]\n", buf, rc, errno, strerror(errno)); > } else { > path[rc] = '\0'; > printf(" readlink(\"%s\") -> %d [path=%s]\n", buf, rc, path); > } > } > } > } > } > > test_acl_file(" ", buf); > test_attr_file(" ", buf); > close(fd); > } > > > > fd = openat(dfd, argv[i+1], O_PATH); > if (fd < 0) { > printf(" openat(%d, \"%s\", O_PATH) -> %d [errno=%d (%s)]\n", dfd, argv[i+1], fd, errno, strerror(errno)); > } else { > printf(" openat(%d, \"%s\", O_PATH) -> %d\n", dfd, argv[i+1], fd); > > test_acl_fd(" ", fd); > test_attr_fd(" ", fd); > > > sprintf(buf, "/compat/linux/dev/fd/%d", fd); > > if (f_stat) { > for (i = 0; i < f_stat; i++) { > if ((rc = stat(buf, &sb)) < 0) { > printf(" stat(\"%s\") -> %d [errno=%d (%s)]\n", buf, rc, errno, strerror(errno)); > } else { > printf(" stat(\"%s\") -> %d [type=%s, size=%lu, uid=%u, gid=%u]\n", buf, rc, mode2type(sb.st_mode), sb.st_size, sb.st_uid, sb.st_gid); > if (S_ISLNK(sb.st_mode)) { > rc = readlink(buf, path, sizeof(path)); > if (rc < 0) { > printf(" readlink(\"%s\") -> %d [errno=%d (%s)]\n", buf, rc, errno, strerror(errno)); > } else { > path[rc] = '\0'; > printf(" readlink(\"%s\") -> %d [path=%s]\n", buf, rc, path); > } > } > } > } > } > > test_acl_file(" ", buf); > test_attr_file(" ", buf); > > > rfd = openat(fd, "", O_EMPTY_PATH); > if (rfd < 0) { > printf(" openat(%d, \"\", O_EMPTY_PATH) -> %d [errno=%d (%s)]\n", fd, rfd, errno, strerror(errno)); > } else { > printf(" openat(%d, \"\", O_EMPTY_PATH) -> %d\n", fd, rfd); > > test_acl_fd(" ", rfd); > test_attr_fd(" ", rfd); > > close(rfd); > } > > rfd = open(buf, O_RDONLY|(f_dir ? O_DIRECTORY : 0)); > if (rfd < 0) { > printf(" open(\"%s\", O_RDONLY) -> %d [errno=%d (%s)]\n", buf, rfd, errno, strerror(errno)); > } else { > printf(" open(\"%s\", O_RDONLY) -> %d\n", buf, rfd); > > test_acl_fd(" ", rfd); > test_attr_fd(" ", rfd); > > close(rfd); > } > close(fd); > } > > exit(0); >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 15376
:
17900
|
17901
|
17913
| 17935 |
18181
|
18524
|
18525