2 Unix SMB/CIFS implementation.
5 Copyright (C) Simo Sorce 2002
6 Copyright (C) Eric Lorimer 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "smbd/smbd.h"
24 #include "system/passwd.h"
25 #include "system/filesys.h"
27 #include "../lib/util/util_pw.h"
28 #include "libcli/security/security.h"
29 #include "passdb/machine_sid.h"
31 static const char *null_string = "";
33 static uint32_t ssf_flags(void)
35 return lp_posix_pathnames() ? SMB_FILENAME_POSIX_PATH : 0;
38 static NTSTATUS cmd_load_module(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
43 printf("Usage: load <modules>\n");
47 for (i=argc-1;i>0;i--) {
48 if (!vfs_init_custom(vfs->conn, argv[i])) {
49 DEBUG(0, ("load: (vfs_init_custom failed for %s)\n", argv[i]));
50 return NT_STATUS_UNSUCCESSFUL;
57 static NTSTATUS cmd_populate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
62 printf("Usage: populate <char> <size>\n");
67 vfs->data = talloc_array(mem_ctx, char, size);
68 if (vfs->data == NULL) {
69 printf("populate: error=-1 (not enough memory)");
70 return NT_STATUS_UNSUCCESSFUL;
72 memset(vfs->data, c, size);
73 vfs->data_size = size;
77 static NTSTATUS cmd_show_data(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
81 if (argc != 1 && argc != 3) {
82 printf("Usage: showdata [<offset> <len>]\n");
85 if (vfs->data == NULL || vfs->data_size == 0) {
86 printf("show_data: error=-1 (buffer empty)\n");
87 return NT_STATUS_UNSUCCESSFUL;
91 offset = atoi(argv[1]);
97 if ((offset + len) > vfs->data_size) {
98 printf("show_data: error=-1 (not enough data in buffer)\n");
99 return NT_STATUS_UNSUCCESSFUL;
101 dump_data(0, (uint8_t *)(vfs->data) + offset, len);
105 static NTSTATUS cmd_connect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
107 const struct loadparm_substitution *lp_sub =
108 loadparm_s3_global_substitution();
110 SMB_VFS_CONNECT(vfs->conn, lp_servicename(talloc_tos(), lp_sub, SNUM(vfs->conn)), "vfstest");
114 static NTSTATUS cmd_disconnect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
116 SMB_VFS_DISCONNECT(vfs->conn);
120 static NTSTATUS cmd_disk_free(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
122 struct smb_filename *smb_fname = NULL;
123 uint64_t diskfree, bsize, dfree, dsize;
125 printf("Usage: disk_free <path>\n");
129 smb_fname = synthetic_smb_fname(talloc_tos(),
135 if (smb_fname == NULL) {
136 return NT_STATUS_NO_MEMORY;
138 diskfree = SMB_VFS_DISK_FREE(vfs->conn, smb_fname,
139 &bsize, &dfree, &dsize);
140 printf("disk_free: %lu, bsize = %lu, dfree = %lu, dsize = %lu\n",
141 (unsigned long)diskfree,
142 (unsigned long)bsize,
143 (unsigned long)dfree,
144 (unsigned long)dsize);
149 static NTSTATUS cmd_opendir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
151 struct smb_filename *smb_fname = NULL;
155 printf("Usage: opendir <fname>\n");
159 smb_fname = synthetic_smb_fname(talloc_tos(),
165 if (smb_fname == NULL) {
166 return NT_STATUS_NO_MEMORY;
169 status = OpenDir(vfs->conn,
175 if (!NT_STATUS_IS_OK(status)) {
176 int err = map_errno_from_nt_status(status);
177 printf("opendir error=%d (%s)\n", err, strerror(err));
178 TALLOC_FREE(smb_fname);
180 return NT_STATUS_UNSUCCESSFUL;
183 vfs->currentdir_offset = 0;
185 TALLOC_FREE(smb_fname);
186 printf("opendir: ok\n");
191 static NTSTATUS cmd_readdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
194 const char *dname = NULL;
195 char *talloced = NULL;
197 if (vfs->currentdir == NULL) {
198 printf("readdir: error=-1 (no open directory)\n");
199 return NT_STATUS_UNSUCCESSFUL;
202 dname = ReadDirName(vfs->currentdir,
203 &vfs->currentdir_offset,
207 printf("readdir: NULL\n");
211 printf("readdir: %s\n", dname);
212 if (VALID_STAT(st)) {
214 printf(" stat available");
215 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
216 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
217 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
218 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
219 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
220 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
221 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
222 printf(" Size: %10u", (unsigned int)st.st_ex_size);
223 #ifdef HAVE_STAT_ST_BLOCKS
224 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
226 #ifdef HAVE_STAT_ST_BLKSIZE
227 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
229 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
230 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
231 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
232 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
233 printf(" Uid: %5lu Gid: %5lu\n",
234 (unsigned long)st.st_ex_uid,
235 (unsigned long)st.st_ex_gid);
236 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
237 printf(" Access: %s", ctime(&tmp_time));
238 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
239 printf(" Modify: %s", ctime(&tmp_time));
240 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
241 printf(" Change: %s", ctime(&tmp_time));
244 TALLOC_FREE(talloced);
249 static NTSTATUS cmd_mkdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
251 struct smb_filename *smb_fname = NULL;
255 printf("Usage: mkdir <path>\n");
259 smb_fname = synthetic_smb_fname(talloc_tos(),
266 if (smb_fname == NULL) {
267 return NT_STATUS_NO_MEMORY;
270 ret = SMB_VFS_MKDIRAT(vfs->conn,
275 printf("mkdir error=%d (%s)\n", errno, strerror(errno));
276 return NT_STATUS_UNSUCCESSFUL;
279 printf("mkdir: ok\n");
284 static NTSTATUS cmd_closedir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
286 if (vfs->currentdir == NULL) {
287 printf("closedir: failure (no directory open)\n");
288 return NT_STATUS_UNSUCCESSFUL;
291 TALLOC_FREE(vfs->currentdir);
292 vfs->currentdir_offset = 0;
294 printf("closedir: ok\n");
299 static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
305 struct files_struct *fspcwd = NULL;
306 struct smb_filename *smb_fname = NULL;
312 if (argc < 3 || argc > 5) {
313 printf("Usage: open <filename> <flags> <mode>\n");
314 printf(" flags: O = O_RDONLY\n");
315 printf(" R = O_RDWR\n");
316 printf(" W = O_WRONLY\n");
317 printf(" C = O_CREAT\n");
318 printf(" E = O_EXCL\n");
319 printf(" T = O_TRUNC\n");
320 printf(" A = O_APPEND\n");
321 printf(" N = O_NONBLOCK/O_NDELAY\n");
323 printf(" S = O_SYNC\n");
326 printf(" F = O_NOFOLLOW\n");
328 printf(" mode: see open.2\n");
329 printf(" mode is ignored if C flag not present\n");
330 printf(" mode defaults to 00400\n");
372 printf("open: error=-1 (invalid flag!)\n");
373 return NT_STATUS_UNSUCCESSFUL;
377 if ((flags & O_CREAT) && argc == 4) {
378 if (sscanf(argv[3], "%ho", (unsigned short *)&mode) == 0) {
379 printf("open: error=-1 (invalid mode!)\n");
380 return NT_STATUS_UNSUCCESSFUL;
384 fsp = talloc_zero(vfs, struct files_struct);
388 fsp->fh = fd_handle_create(fsp);
389 if (fsp->fh == NULL) {
392 fsp->conn = vfs->conn;
394 smb_fname = synthetic_smb_fname_split(NULL,
396 lp_posix_pathnames());
397 if (smb_fname == NULL) {
401 fsp->fsp_name = smb_fname;
403 status = vfs_at_fspcwd(fsp, vfs->conn, &fspcwd);
404 if (!NT_STATUS_IS_OK(status)) {
408 if (is_named_stream(smb_fname)) {
409 struct smb_filename *base_name = NULL;
411 base_name = cp_smb_filename_nostream(NULL, smb_fname);
412 if (base_name == NULL) {
416 status = openat_pathref_fsp(fspcwd, base_name);
417 if (!NT_STATUS_IS_OK(status)) {
423 fsp->base_fsp = base_name->fsp;
426 fd = SMB_VFS_OPENAT(vfs->conn,
433 printf("open: error=%d (%s)\n", errno, strerror(errno));
434 status = map_nt_error_from_unix(errno);
439 status = vfs_stat_fsp(fsp);
440 if (!NT_STATUS_IS_OK(status)) {
441 /* If we have an fd, this stat should succeed. */
442 DEBUG(0,("Error doing fstat on open file %s "
444 smb_fname_str_dbg(smb_fname),
445 nt_errstr(status) ));
446 } else if (S_ISDIR(smb_fname->st.st_ex_mode)) {
448 status = NT_STATUS_FILE_IS_A_DIRECTORY;
451 if (!NT_STATUS_IS_OK(status)) {
456 fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
457 fsp->vuid = UID_FIELD_INVALID;
459 fsp->fsp_flags.can_lock = true;
460 fsp->fsp_flags.can_read = true;
461 fsp->fsp_flags.can_write = CAN_WRITE(vfs->conn);
462 fsp->print_file = NULL;
463 fsp->fsp_flags.modified = false;
464 fsp->sent_oplock_break = NO_BREAK_SENT;
465 fsp->fsp_flags.is_directory = false;
467 vfs->files[fsp_get_pathref_fd(fsp)] = fsp;
468 printf("open: fd=%d\n", fsp_get_pathref_fd(fsp));
472 status = NT_STATUS_NO_MEMORY;
474 TALLOC_FREE(smb_fname);
480 static NTSTATUS cmd_pathfunc(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
482 struct smb_filename *smb_fname = NULL;
486 printf("Usage: %s <path>\n", argv[0]);
490 smb_fname = synthetic_smb_fname(talloc_tos(),
497 if (smb_fname == NULL) {
498 return NT_STATUS_NO_MEMORY;
501 if (strcmp("rmdir", argv[0]) == 0 ) {
502 ret = SMB_VFS_UNLINKAT(vfs->conn,
506 TALLOC_FREE(smb_fname);
507 } else if (strcmp("unlink", argv[0]) == 0 ) {
508 TALLOC_FREE(smb_fname);
509 /* unlink can be a stream:name */
510 smb_fname = synthetic_smb_fname_split(talloc_tos(),
512 lp_posix_pathnames());
513 if (smb_fname == NULL) {
514 return NT_STATUS_NO_MEMORY;
516 ret = SMB_VFS_UNLINKAT(vfs->conn,
520 TALLOC_FREE(smb_fname);
521 } else if (strcmp("chdir", argv[0]) == 0 ) {
522 ret = SMB_VFS_CHDIR(vfs->conn, smb_fname);
523 TALLOC_FREE(smb_fname);
525 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
526 return NT_STATUS_UNSUCCESSFUL;
530 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
531 return NT_STATUS_UNSUCCESSFUL;
534 printf("%s: ok\n", argv[0]);
539 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
545 printf("Usage: close <fd>\n");
550 if (vfs->files[fd] == NULL) {
551 printf("close: error=-1 (invalid file descriptor)\n");
555 status = fd_close(vfs->files[fd]);
556 if (!NT_STATUS_IS_OK(status))
557 printf("close: error=%s\n", nt_errstr(status));
559 printf("close: ok\n");
561 TALLOC_FREE(vfs->files[fd]);
562 vfs->files[fd] = NULL;
567 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
574 printf("Usage: read <fd> <size>\n");
578 /* do some error checking on these */
580 size = atoi(argv[2]);
581 vfs->data = talloc_array(mem_ctx, char, size);
582 if (vfs->data == NULL) {
583 printf("read: error=-1 (not enough memory)");
584 return NT_STATUS_UNSUCCESSFUL;
586 vfs->data_size = size;
588 rsize = read_file(vfs->files[fd], vfs->data, 0, size);
590 printf("read: error=%d (%s)\n", errno, strerror(errno));
591 return NT_STATUS_UNSUCCESSFUL;
594 printf("read: ok\n");
599 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
605 printf("Usage: write <fd> <size>\n");
609 /* some error checking should go here */
611 size = atoi(argv[2]);
612 if (vfs->data == NULL) {
613 printf("write: error=-1 (buffer empty, please populate it before writing)");
614 return NT_STATUS_UNSUCCESSFUL;
617 if (vfs->data_size < size) {
618 printf("write: error=-1 (buffer too small, please put some more data in)");
619 return NT_STATUS_UNSUCCESSFUL;
622 wsize = write_file(NULL, vfs->files[fd], vfs->data, 0, size);
625 printf("write: error=%d (%s)\n", errno, strerror(errno));
626 return NT_STATUS_UNSUCCESSFUL;
629 printf("write: ok\n");
634 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
636 int fd, offset, whence;
640 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
645 offset = atoi(argv[2]);
646 whence = atoi(argv[3]);
648 case 1: whence = SEEK_SET; break;
649 case 2: whence = SEEK_CUR; break;
650 default: whence = SEEK_END;
653 pos = SMB_VFS_LSEEK(vfs->files[fd], offset, whence);
654 if (pos == (off_t)-1) {
655 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
656 return NT_STATUS_UNSUCCESSFUL;
659 printf("lseek: ok\n");
664 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
667 struct smb_filename *smb_fname_src = NULL;
668 struct smb_filename *smb_fname_dst = NULL;
671 printf("Usage: rename <old> <new>\n");
675 smb_fname_src = synthetic_smb_fname_split(mem_ctx,
677 lp_posix_pathnames());
678 if (smb_fname_src == NULL) {
679 return NT_STATUS_NO_MEMORY;
682 smb_fname_dst = synthetic_smb_fname_split(mem_ctx,
684 lp_posix_pathnames());
685 if (smb_fname_dst == NULL) {
686 TALLOC_FREE(smb_fname_src);
687 return NT_STATUS_NO_MEMORY;
690 ret = SMB_VFS_RENAMEAT(vfs->conn,
696 TALLOC_FREE(smb_fname_src);
697 TALLOC_FREE(smb_fname_dst);
699 printf("rename: error=%d (%s)\n", errno, strerror(errno));
700 return NT_STATUS_UNSUCCESSFUL;
703 printf("rename: ok\n");
707 static NTSTATUS cmd_fsync(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
711 printf("Usage: fsync <fd>\n");
716 ret = smb_vfs_fsync_sync(vfs->files[fd]);
718 printf("fsync: error=%d (%s)\n", errno, strerror(errno));
719 return NT_STATUS_UNSUCCESSFUL;
722 printf("fsync: ok\n");
727 static NTSTATUS cmd_stat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
732 struct passwd *pwd = NULL;
733 struct group *grp = NULL;
734 struct smb_filename *smb_fname = NULL;
739 printf("Usage: stat <fname>\n");
743 smb_fname = synthetic_smb_fname_split(mem_ctx,
745 lp_posix_pathnames());
746 if (smb_fname == NULL) {
747 return NT_STATUS_NO_MEMORY;
750 ret = SMB_VFS_STAT(vfs->conn, smb_fname);
752 printf("stat: error=%d (%s)\n", errno, strerror(errno));
753 TALLOC_FREE(smb_fname);
754 return NT_STATUS_UNSUCCESSFUL;
757 TALLOC_FREE(smb_fname);
759 pwd = getpwuid(st.st_ex_uid);
760 if (pwd != NULL) user = pwd->pw_name;
761 else user = null_string;
762 grp = getgrgid(st.st_ex_gid);
763 if (grp != NULL) group = grp->gr_name;
764 else group = null_string;
766 printf("stat: ok\n");
767 printf(" File: %s", argv[1]);
768 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
769 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
770 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
771 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
772 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
773 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
774 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
775 printf(" Size: %10u", (unsigned int)st.st_ex_size);
776 #ifdef HAVE_STAT_ST_BLOCKS
777 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
779 #ifdef HAVE_STAT_ST_BLKSIZE
780 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
782 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
783 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
784 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
785 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
786 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
787 (unsigned long)st.st_ex_gid, group);
788 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
789 printf(" Access: %s", ctime(&tmp_time));
790 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
791 printf(" Modify: %s", ctime(&tmp_time));
792 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
793 printf(" Change: %s", ctime(&tmp_time));
799 static NTSTATUS cmd_fstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
804 struct passwd *pwd = NULL;
805 struct group *grp = NULL;
810 printf("Usage: fstat <fd>\n");
815 if (fd < 0 || fd >= 1024) {
816 printf("fstat: error=%d (file descriptor out of range)\n", EBADF);
820 if (vfs->files[fd] == NULL) {
821 printf("fstat: error=%d (invalid file descriptor)\n", EBADF);
825 if (SMB_VFS_FSTAT(vfs->files[fd], &st) == -1) {
826 printf("fstat: error=%d (%s)\n", errno, strerror(errno));
827 return NT_STATUS_UNSUCCESSFUL;
830 pwd = getpwuid(st.st_ex_uid);
831 if (pwd != NULL) user = pwd->pw_name;
832 else user = null_string;
833 grp = getgrgid(st.st_ex_gid);
834 if (grp != NULL) group = grp->gr_name;
835 else group = null_string;
837 printf("fstat: ok\n");
838 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
839 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
840 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
841 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
842 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
843 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
844 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
845 printf(" Size: %10u", (unsigned int)st.st_ex_size);
846 #ifdef HAVE_STAT_ST_BLOCKS
847 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
849 #ifdef HAVE_STAT_ST_BLKSIZE
850 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
852 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
853 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
854 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
855 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
856 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
857 (unsigned long)st.st_ex_gid, group);
858 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
859 printf(" Access: %s", ctime(&tmp_time));
860 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
861 printf(" Modify: %s", ctime(&tmp_time));
862 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
863 printf(" Change: %s", ctime(&tmp_time));
869 static NTSTATUS cmd_lstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
873 struct passwd *pwd = NULL;
874 struct group *grp = NULL;
875 struct smb_filename *smb_fname = NULL;
880 printf("Usage: lstat <path>\n");
884 smb_fname = synthetic_smb_fname_split(mem_ctx,
886 lp_posix_pathnames());
887 if (smb_fname == NULL) {
888 return NT_STATUS_NO_MEMORY;
891 if (SMB_VFS_LSTAT(vfs->conn, smb_fname) == -1) {
892 printf("lstat: error=%d (%s)\n", errno, strerror(errno));
893 TALLOC_FREE(smb_fname);
894 return NT_STATUS_UNSUCCESSFUL;
897 TALLOC_FREE(smb_fname);
899 pwd = getpwuid(st.st_ex_uid);
900 if (pwd != NULL) user = pwd->pw_name;
901 else user = null_string;
902 grp = getgrgid(st.st_ex_gid);
903 if (grp != NULL) group = grp->gr_name;
904 else group = null_string;
906 printf("lstat: ok\n");
907 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
908 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
909 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
910 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
911 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
912 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
913 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
914 printf(" Size: %10u", (unsigned int)st.st_ex_size);
915 #ifdef HAVE_STAT_ST_BLOCKS
916 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
918 #ifdef HAVE_STAT_ST_BLKSIZE
919 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
921 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
922 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
923 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
924 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
925 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
926 (unsigned long)st.st_ex_gid, group);
927 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
928 printf(" Access: %s", ctime(&tmp_time));
929 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
930 printf(" Modify: %s", ctime(&tmp_time));
931 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
932 printf(" Change: %s", ctime(&tmp_time));
938 static NTSTATUS cmd_chmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
940 struct smb_filename *smb_fname = NULL;
942 struct smb_filename *pathref_fname = NULL;
945 printf("Usage: chmod <path> <mode>\n");
949 mode = atoi(argv[2]);
951 smb_fname = synthetic_smb_fname_split(mem_ctx,
953 lp_posix_pathnames());
954 if (smb_fname == NULL) {
955 return NT_STATUS_NO_MEMORY;
958 status = synthetic_pathref(mem_ctx,
960 smb_fname->base_name,
966 if (!NT_STATUS_IS_OK(status)) {
969 if (SMB_VFS_FCHMOD(pathref_fname->fsp, mode) == -1) {
970 printf("chmod: error=%d (%s)\n", errno, strerror(errno));
971 return NT_STATUS_UNSUCCESSFUL;
974 printf("chmod: ok\n");
979 static NTSTATUS cmd_fchmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
984 printf("Usage: fchmod <fd> <mode>\n");
989 mode = atoi(argv[2]);
990 if (fd < 0 || fd >= 1024) {
991 printf("fchmod: error=%d (file descriptor out of range)\n", EBADF);
994 if (vfs->files[fd] == NULL) {
995 printf("fchmod: error=%d (invalid file descriptor)\n", EBADF);
999 if (SMB_VFS_FCHMOD(vfs->files[fd], mode) == -1) {
1000 printf("fchmod: error=%d (%s)\n", errno, strerror(errno));
1001 return NT_STATUS_UNSUCCESSFUL;
1004 printf("fchmod: ok\n");
1005 return NT_STATUS_OK;
1008 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1014 printf("Usage: fchown <fd> <uid> <gid>\n");
1015 return NT_STATUS_OK;
1018 uid = atoi(argv[2]);
1019 gid = atoi(argv[3]);
1021 if (fd < 0 || fd >= 1024) {
1022 printf("fchown: faliure=%d (file descriptor out of range)\n", EBADF);
1023 return NT_STATUS_OK;
1025 if (vfs->files[fd] == NULL) {
1026 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
1027 return NT_STATUS_OK;
1029 if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
1030 printf("fchown error=%d (%s)\n", errno, strerror(errno));
1031 return NT_STATUS_UNSUCCESSFUL;
1034 printf("fchown: ok\n");
1035 return NT_STATUS_OK;
1039 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1041 struct smb_filename *smb_fname = SMB_VFS_GETWD(vfs->conn, talloc_tos());
1042 if (smb_fname == NULL) {
1043 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
1044 return NT_STATUS_UNSUCCESSFUL;
1047 printf("getwd: %s\n", smb_fname->base_name);
1048 TALLOC_FREE(smb_fname);
1049 return NT_STATUS_OK;
1052 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1054 struct smb_file_time ft;
1055 struct smb_filename *smb_fname = NULL;
1059 printf("Usage: utime <path> <access> <modify>\n");
1060 return NT_STATUS_OK;
1063 init_smb_file_time(&ft);
1065 ft.atime = time_t_to_full_timespec(atoi(argv[2]));
1066 ft.mtime = time_t_to_full_timespec(atoi(argv[3]));
1068 status = filename_convert(mem_ctx,
1074 if (!NT_STATUS_IS_OK(status)) {
1075 printf("utime: %s\n", nt_errstr(status));
1079 if (SMB_VFS_FNTIMES(smb_fname->fsp, &ft) != 0) {
1080 printf("utime: error=%d (%s)\n", errno, strerror(errno));
1081 TALLOC_FREE(smb_fname);
1082 return NT_STATUS_UNSUCCESSFUL;
1085 TALLOC_FREE(smb_fname);
1086 printf("utime: ok\n");
1087 return NT_STATUS_OK;
1090 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1095 printf("Usage: ftruncate <fd> <length>\n");
1096 return NT_STATUS_OK;
1100 off = atoi(argv[2]);
1101 if (fd < 0 || fd >= 1024) {
1102 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
1103 return NT_STATUS_OK;
1105 if (vfs->files[fd] == NULL) {
1106 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
1107 return NT_STATUS_OK;
1110 if (SMB_VFS_FTRUNCATE(vfs->files[fd], off) == -1) {
1111 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
1112 return NT_STATUS_UNSUCCESSFUL;
1115 printf("ftruncate: ok\n");
1116 return NT_STATUS_OK;
1119 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1126 const char *typestr;
1129 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
1130 printf(" ops: G = F_GETLK\n");
1131 printf(" S = F_SETLK\n");
1132 printf(" W = F_SETLKW\n");
1133 printf(" type: R = F_RDLCK\n");
1134 printf(" W = F_WRLCK\n");
1135 printf(" U = F_UNLCK\n");
1136 return NT_STATUS_OK;
1139 if (sscanf(argv[1], "%d", &fd) == 0) {
1140 printf("lock: error=-1 (error parsing fd)\n");
1141 return NT_STATUS_UNSUCCESSFUL;
1156 printf("lock: error=-1 (invalid op flag!)\n");
1157 return NT_STATUS_UNSUCCESSFUL;
1160 if (sscanf(argv[3], "%ld", &offset) == 0) {
1161 printf("lock: error=-1 (error parsing fd)\n");
1162 return NT_STATUS_UNSUCCESSFUL;
1165 if (sscanf(argv[4], "%ld", &count) == 0) {
1166 printf("lock: error=-1 (error parsing fd)\n");
1167 return NT_STATUS_UNSUCCESSFUL;
1184 printf("lock: error=-1 (invalid type flag!)\n");
1185 return NT_STATUS_UNSUCCESSFUL;
1190 printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
1192 if (SMB_VFS_LOCK(vfs->files[fd], op, offset, count, type) == False) {
1193 printf("lock: error=%d (%s)\n", errno, strerror(errno));
1194 return NT_STATUS_UNSUCCESSFUL;
1197 printf("lock: ok\n");
1198 return NT_STATUS_OK;
1201 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1204 char *target = NULL;
1205 struct smb_filename target_fname;
1206 struct smb_filename *new_smb_fname = NULL;
1210 printf("Usage: symlink <path> <link>\n");
1211 return NT_STATUS_OK;
1214 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1216 lp_posix_pathnames());
1217 if (new_smb_fname == NULL) {
1218 return NT_STATUS_NO_MEMORY;
1221 target = talloc_strdup(mem_ctx, argv[1]);
1222 if (target == NULL) {
1223 return NT_STATUS_NO_MEMORY;
1226 target_fname = (struct smb_filename) {
1227 .base_name = target,
1230 /* Removes @GMT tokens if any */
1231 status = canonicalize_snapshot_path(&target_fname, UCF_GMT_PATHNAME, 0);
1232 if (!NT_STATUS_IS_OK(status)) {
1236 ret = SMB_VFS_SYMLINKAT(vfs->conn,
1241 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
1242 return NT_STATUS_UNSUCCESSFUL;
1245 printf("symlink: ok\n");
1246 return NT_STATUS_OK;
1250 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1252 char buffer[PATH_MAX];
1253 struct smb_filename *smb_fname = NULL;
1257 printf("Usage: readlink <path>\n");
1258 return NT_STATUS_OK;
1261 smb_fname = synthetic_smb_fname_split(mem_ctx,
1263 lp_posix_pathnames());
1264 if (smb_fname == NULL) {
1265 return NT_STATUS_NO_MEMORY;
1267 size = SMB_VFS_READLINKAT(vfs->conn,
1274 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
1275 return NT_STATUS_UNSUCCESSFUL;
1278 buffer[size] = '\0';
1279 printf("readlink: %s\n", buffer);
1280 return NT_STATUS_OK;
1284 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1286 struct smb_filename *old_smb_fname = NULL;
1287 struct smb_filename *new_smb_fname = NULL;
1291 printf("Usage: link <path> <link>\n");
1292 return NT_STATUS_OK;
1295 old_smb_fname = synthetic_smb_fname_split(mem_ctx,
1297 lp_posix_pathnames());
1298 if (old_smb_fname == NULL) {
1299 return NT_STATUS_NO_MEMORY;
1301 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1303 lp_posix_pathnames());
1304 if (new_smb_fname == NULL) {
1305 return NT_STATUS_NO_MEMORY;
1308 ret = SMB_VFS_LINKAT(vfs->conn,
1315 printf("link: error=%d (%s)\n", errno, strerror(errno));
1316 return NT_STATUS_UNSUCCESSFUL;
1319 printf("link: ok\n");
1320 return NT_STATUS_OK;
1323 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1326 unsigned int dev_val;
1328 struct smb_filename *smb_fname = NULL;
1332 printf("Usage: mknod <path> <mode> <dev>\n");
1333 printf(" mode is octal\n");
1334 printf(" dev is hex\n");
1335 return NT_STATUS_OK;
1338 if (sscanf(argv[2], "%ho", (unsigned short *)&mode) == 0) {
1339 printf("open: error=-1 (invalid mode!)\n");
1340 return NT_STATUS_UNSUCCESSFUL;
1343 if (sscanf(argv[3], "%x", &dev_val) == 0) {
1344 printf("open: error=-1 (invalid dev!)\n");
1345 return NT_STATUS_UNSUCCESSFUL;
1347 dev = (SMB_DEV_T)dev_val;
1349 smb_fname = synthetic_smb_fname_split(mem_ctx,
1351 lp_posix_pathnames());
1352 if (smb_fname == NULL) {
1353 return NT_STATUS_NO_MEMORY;
1356 ret = SMB_VFS_MKNODAT(vfs->conn,
1363 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1364 return NT_STATUS_UNSUCCESSFUL;
1367 printf("mknod: ok\n");
1368 return NT_STATUS_OK;
1371 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1373 struct smb_filename *smb_fname = NULL;
1376 printf("Usage: realpath <path>\n");
1377 return NT_STATUS_OK;
1380 smb_fname = synthetic_smb_fname_split(mem_ctx,
1382 lp_posix_pathnames());
1383 if (smb_fname == NULL) {
1384 return NT_STATUS_NO_MEMORY;
1386 if (SMB_VFS_REALPATH(vfs->conn, mem_ctx, smb_fname) == NULL) {
1387 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1388 return NT_STATUS_UNSUCCESSFUL;
1391 printf("realpath: ok\n");
1392 return NT_STATUS_OK;
1395 static NTSTATUS cmd_getxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1396 int argc, const char **argv)
1400 struct smb_filename *smb_fname = NULL;
1401 struct smb_filename *pathref_fname = NULL;
1405 printf("Usage: getxattr <path> <xattr>\n");
1406 return NT_STATUS_OK;
1411 smb_fname = synthetic_smb_fname_split(mem_ctx,
1413 lp_posix_pathnames());
1414 if (smb_fname == NULL) {
1415 return NT_STATUS_NO_MEMORY;
1417 status = synthetic_pathref(mem_ctx,
1419 smb_fname->base_name,
1425 if (!NT_STATUS_IS_OK(status)) {
1428 ret = SMB_VFS_FGETXATTR(pathref_fname->fsp,
1431 talloc_get_size(buf));
1434 printf("getxattr returned (%s)\n", strerror(err));
1435 return map_nt_error_from_unix(err);
1437 buf = talloc_array(mem_ctx, uint8_t, ret);
1439 return NT_STATUS_NO_MEMORY;
1441 ret = SMB_VFS_FGETXATTR(pathref_fname->fsp,
1444 talloc_get_size(buf));
1447 printf("getxattr returned (%s)\n", strerror(err));
1448 return map_nt_error_from_unix(err);
1450 dump_data_file(buf, talloc_get_size(buf), false, stdout);
1451 return NT_STATUS_OK;
1454 static NTSTATUS cmd_listxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1455 int argc, const char **argv)
1459 struct smb_filename *smb_fname = NULL;
1460 struct smb_filename *pathref_fname = NULL;
1463 printf("Usage: listxattr <path>\n");
1464 return NT_STATUS_OK;
1469 smb_fname = synthetic_smb_fname_split(mem_ctx,
1471 lp_posix_pathnames());
1472 if (smb_fname == NULL) {
1473 return NT_STATUS_NO_MEMORY;
1475 status = synthetic_pathref(mem_ctx,
1477 smb_fname->base_name,
1483 if (!NT_STATUS_IS_OK(status)) {
1487 ret = SMB_VFS_FLISTXATTR(pathref_fname->fsp,
1488 buf, talloc_get_size(buf));
1491 printf("listxattr returned (%s)\n", strerror(err));
1492 return map_nt_error_from_unix(err);
1494 buf = talloc_array(mem_ctx, char, ret);
1496 return NT_STATUS_NO_MEMORY;
1498 ret = SMB_VFS_FLISTXATTR(pathref_fname->fsp,
1499 buf, talloc_get_size(buf));
1502 printf("listxattr returned (%s)\n", strerror(err));
1503 return map_nt_error_from_unix(err);
1506 return NT_STATUS_OK;
1508 if (buf[ret-1] != '\0') {
1509 printf("listxattr returned non 0-terminated strings\n");
1510 return NT_STATUS_INTERNAL_ERROR;
1514 while (p < buf+ret) {
1519 return NT_STATUS_OK;
1522 static NTSTATUS cmd_fsetxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1523 int argc, const char **argv)
1527 struct smb_filename *smb_fname = NULL;
1528 struct smb_filename *pathref_fname = NULL;
1531 if ((argc < 4) || (argc > 5)) {
1532 printf("Usage: setxattr <path> <xattr> <value> [flags]\n");
1533 return NT_STATUS_OK;
1537 flags = atoi(argv[4]);
1540 smb_fname = synthetic_smb_fname_split(mem_ctx,
1542 lp_posix_pathnames());
1543 if (smb_fname == NULL) {
1544 return NT_STATUS_NO_MEMORY;
1547 status = synthetic_pathref(mem_ctx,
1549 smb_fname->base_name,
1555 if (!NT_STATUS_IS_OK(status)) {
1559 ret = SMB_VFS_FSETXATTR(pathref_fname->fsp, argv[2],
1560 argv[3], strlen(argv[3]), flags);
1563 printf("fsetxattr returned (%s)\n", strerror(err));
1564 return map_nt_error_from_unix(err);
1566 return NT_STATUS_OK;
1569 static NTSTATUS cmd_removexattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1570 int argc, const char **argv)
1573 struct smb_filename *smb_fname = NULL;
1574 struct smb_filename *pathref_fname = NULL;
1578 printf("Usage: removexattr <path> <xattr>\n");
1579 return NT_STATUS_OK;
1582 smb_fname = synthetic_smb_fname_split(mem_ctx,
1584 lp_posix_pathnames());
1585 if (smb_fname == NULL) {
1586 return NT_STATUS_NO_MEMORY;
1588 status = synthetic_pathref(mem_ctx,
1590 smb_fname->base_name,
1596 if (!NT_STATUS_IS_OK(status)) {
1599 ret = SMB_VFS_FREMOVEXATTR(pathref_fname->fsp, argv[2]);
1602 printf("removexattr returned (%s)\n", strerror(err));
1603 return map_nt_error_from_unix(err);
1605 return NT_STATUS_OK;
1608 static NTSTATUS cmd_fget_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1609 int argc, const char **argv)
1613 struct security_descriptor *sd;
1616 printf("Usage: fget_nt_acl <fd>\n");
1617 return NT_STATUS_OK;
1621 if (fd < 0 || fd >= 1024) {
1622 printf("fget_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1623 return NT_STATUS_OK;
1625 if (vfs->files[fd] == NULL) {
1626 printf("fget_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1627 return NT_STATUS_OK;
1630 status = SMB_VFS_FGET_NT_ACL(vfs->files[fd],
1631 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1633 if (!NT_STATUS_IS_OK(status)) {
1634 printf("fget_nt_acl returned (%s)\n", nt_errstr(status));
1637 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1639 return NT_STATUS_OK;
1642 static NTSTATUS cmd_get_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1643 int argc, const char **argv)
1646 struct security_descriptor *sd;
1647 struct smb_filename *smb_fname = NULL;
1648 struct smb_filename *pathref_fname = NULL;
1651 printf("Usage: get_nt_acl <path>\n");
1652 return NT_STATUS_OK;
1655 smb_fname = synthetic_smb_fname(talloc_tos(),
1662 if (smb_fname == NULL) {
1663 return NT_STATUS_NO_MEMORY;
1666 status = synthetic_pathref(mem_ctx,
1668 smb_fname->base_name,
1674 if (!NT_STATUS_IS_OK(status)) {
1675 TALLOC_FREE(smb_fname);
1678 status = SMB_VFS_FGET_NT_ACL(pathref_fname->fsp,
1679 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1682 if (!NT_STATUS_IS_OK(status)) {
1683 printf("get_nt_acl returned (%s)\n", nt_errstr(status));
1684 TALLOC_FREE(smb_fname);
1685 TALLOC_FREE(pathref_fname);
1688 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1690 TALLOC_FREE(smb_fname);
1691 TALLOC_FREE(pathref_fname);
1692 return NT_STATUS_OK;
1695 static NTSTATUS cmd_fset_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1696 int argc, const char **argv)
1700 struct security_descriptor *sd;
1703 printf("Usage: fset_nt_acl <fd> <sddl>\n");
1704 return NT_STATUS_OK;
1708 if (fd < 0 || fd >= 1024) {
1709 printf("fset_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1710 return NT_STATUS_OK;
1712 if (vfs->files[fd] == NULL) {
1713 printf("fset_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1714 return NT_STATUS_OK;
1717 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1719 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1720 return NT_STATUS_INVALID_PARAMETER;
1723 status = SMB_VFS_FSET_NT_ACL(vfs->files[fd], SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1724 if (!NT_STATUS_IS_OK(status)) {
1725 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1729 return NT_STATUS_OK;
1732 static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1737 struct files_struct *fspcwd = NULL;
1738 struct smb_filename *smb_fname = NULL;
1740 struct security_descriptor *sd = NULL;
1744 printf("Usage: set_nt_acl <file> <sddl>\n");
1745 return NT_STATUS_OK;
1750 fsp = talloc_zero(vfs, struct files_struct);
1752 return NT_STATUS_NO_MEMORY;
1754 fsp->fh = fd_handle_create(fsp);
1755 if (fsp->fh == NULL) {
1757 return NT_STATUS_NO_MEMORY;
1759 fsp->conn = vfs->conn;
1761 smb_fname = synthetic_smb_fname_split(NULL,
1763 lp_posix_pathnames());
1764 if (smb_fname == NULL) {
1766 return NT_STATUS_NO_MEMORY;
1769 fsp->fsp_name = smb_fname;
1772 flags = O_RDONLY|O_DIRECTORY;
1774 /* POSIX allows us to open a directory with O_RDONLY. */
1778 status = vfs_at_fspcwd(fsp, vfs->conn, &fspcwd);
1779 if (!NT_STATUS_IS_OK(status)) {
1783 fd = SMB_VFS_OPENAT(vfs->conn,
1789 if (fd == -1 && errno == EISDIR) {
1790 fd = SMB_VFS_OPENAT(vfs->conn,
1798 printf("open: error=%d (%s)\n", errno, strerror(errno));
1800 TALLOC_FREE(smb_fname);
1801 return NT_STATUS_UNSUCCESSFUL;
1803 fsp_set_fd(fsp, fd);
1805 status = vfs_stat_fsp(fsp);
1806 if (!NT_STATUS_IS_OK(status)) {
1807 /* If we have an fd, this stat should succeed. */
1808 DEBUG(0,("Error doing fstat on open file %s "
1810 smb_fname_str_dbg(smb_fname),
1811 nt_errstr(status) ));
1815 fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
1816 fsp->vuid = UID_FIELD_INVALID;
1818 fsp->fsp_flags.can_lock = true;
1819 fsp->fsp_flags.can_read = true;
1820 fsp->fsp_flags.can_write = true;
1821 fsp->print_file = NULL;
1822 fsp->fsp_flags.modified = false;
1823 fsp->sent_oplock_break = NO_BREAK_SENT;
1824 fsp->fsp_flags.is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
1826 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1828 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1829 status = NT_STATUS_INVALID_PARAMETER;
1833 status = SMB_VFS_FSET_NT_ACL(fsp, SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1834 if (!NT_STATUS_IS_OK(status)) {
1835 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1841 status = fd_close(fsp);
1842 if (!NT_STATUS_IS_OK(status))
1843 printf("close: error= (%s)\n", nt_errstr(status));
1852 static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1853 int argc, const char **argv)
1860 printf("Usage: sys_acl_get_fd <fd>\n");
1861 return NT_STATUS_OK;
1865 if (fd < 0 || fd >= 1024) {
1866 printf("sys_acl_get_fd: error=%d (file descriptor out of range)\n", EBADF);
1867 return NT_STATUS_OK;
1869 if (vfs->files[fd] == NULL) {
1870 printf("sys_acl_get_fd: error=%d (invalid file descriptor)\n", EBADF);
1871 return NT_STATUS_OK;
1874 acl = SMB_VFS_SYS_ACL_GET_FD(vfs->files[fd],
1875 SMB_ACL_TYPE_ACCESS,
1878 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1879 return NT_STATUS_UNSUCCESSFUL;
1881 acl_text = sys_acl_to_text(acl, NULL);
1882 printf("%s", acl_text);
1884 SAFE_FREE(acl_text);
1885 return NT_STATUS_OK;
1888 static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1889 int argc, const char **argv)
1894 struct smb_filename *smb_fname = NULL;
1895 struct smb_filename *pathref_fname = NULL;
1899 printf("Usage: sys_acl_get_file <path> <type>\n");
1900 return NT_STATUS_OK;
1903 smb_fname = synthetic_smb_fname_split(talloc_tos(),
1905 lp_posix_pathnames());
1906 if (smb_fname == NULL) {
1907 return NT_STATUS_NO_MEMORY;
1909 type = atoi(argv[2]);
1911 status = synthetic_pathref(mem_ctx,
1913 smb_fname->base_name,
1919 if (!NT_STATUS_IS_OK(status)) {
1920 TALLOC_FREE(smb_fname);
1924 acl = SMB_VFS_SYS_ACL_GET_FD(pathref_fname->fsp,
1925 type, talloc_tos());
1927 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1928 TALLOC_FREE(smb_fname);
1929 TALLOC_FREE(pathref_fname);
1930 return NT_STATUS_UNSUCCESSFUL;
1932 acl_text = sys_acl_to_text(acl, NULL);
1933 printf("%s", acl_text);
1935 TALLOC_FREE(smb_fname);
1936 TALLOC_FREE(pathref_fname);
1937 SAFE_FREE(acl_text);
1938 return NT_STATUS_OK;
1941 static NTSTATUS cmd_sys_acl_blob_get_file(struct vfs_state *vfs,
1942 TALLOC_CTX *mem_ctx,
1943 int argc, const char **argv)
1949 struct smb_filename *smb_fname = NULL;
1950 struct smb_filename *pathref_fname = NULL;
1954 printf("Usage: sys_acl_blob_get_file <path>\n");
1955 return NT_STATUS_OK;
1958 smb_fname = synthetic_smb_fname_split(mem_ctx,
1960 lp_posix_pathnames());
1961 if (smb_fname == NULL) {
1962 return NT_STATUS_NO_MEMORY;
1964 status = synthetic_pathref(mem_ctx,
1966 smb_fname->base_name,
1972 if (!NT_STATUS_IS_OK(status)) {
1973 TALLOC_FREE(smb_fname);
1977 ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(pathref_fname->fsp,
1982 status = map_nt_error_from_unix(errno);
1983 printf("sys_acl_blob_get_file failed (%s)\n", strerror(errno));
1984 TALLOC_FREE(smb_fname);
1985 TALLOC_FREE(pathref_fname);
1988 printf("Description: %s\n", description);
1989 for (i = 0; i < blob.length; i++) {
1990 printf("%.2x ", blob.data[i]);
1994 TALLOC_FREE(smb_fname);
1995 TALLOC_FREE(pathref_fname);
1996 return NT_STATUS_OK;
1999 static NTSTATUS cmd_sys_acl_blob_get_fd(struct vfs_state *vfs,
2000 TALLOC_CTX *mem_ctx,
2001 int argc, const char **argv)
2010 printf("Usage: sys_acl_blob_get_fd <fd>\n");
2011 return NT_STATUS_OK;
2015 if (fd < 0 || fd >= 1024) {
2016 printf("sys_acl_blob_get_fd: error=%d "
2017 "(file descriptor out of range)\n", EBADF);
2018 return NT_STATUS_OK;
2020 if (vfs->files[fd] == NULL) {
2021 printf("sys_acl_blob_get_fd: error=%d "
2022 "(invalid file descriptor)\n", EBADF);
2023 return NT_STATUS_OK;
2026 ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(vfs->files[fd], talloc_tos(),
2027 &description, &blob);
2029 printf("sys_acl_blob_get_fd failed (%s)\n", strerror(errno));
2030 return map_nt_error_from_unix(errno);
2032 printf("Description: %s\n", description);
2033 for (i = 0; i < blob.length; i++) {
2034 printf("%.2x ", blob.data[i]);
2038 return NT_STATUS_OK;
2043 static NTSTATUS cmd_sys_acl_delete_def_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
2044 int argc, const char **argv)
2047 struct smb_filename *smb_fname = NULL;
2048 struct smb_filename *pathref_fname = NULL;
2052 printf("Usage: sys_acl_delete_def_file <path>\n");
2053 return NT_STATUS_OK;
2056 smb_fname = synthetic_smb_fname_split(mem_ctx,
2058 lp_posix_pathnames());
2059 if (smb_fname == NULL) {
2060 return NT_STATUS_NO_MEMORY;
2062 status = synthetic_pathref(mem_ctx,
2064 smb_fname->base_name,
2070 if (!NT_STATUS_IS_OK(status)) {
2071 TALLOC_FREE(smb_fname);
2074 if (!pathref_fname->fsp->fsp_flags.is_directory) {
2075 printf("sys_acl_delete_def_file - %s is not a directory\n",
2076 smb_fname->base_name);
2077 TALLOC_FREE(smb_fname);
2078 TALLOC_FREE(pathref_fname);
2079 return NT_STATUS_INVALID_PARAMETER;
2081 ret = SMB_VFS_SYS_ACL_DELETE_DEF_FD(pathref_fname->fsp);
2084 printf("sys_acl_delete_def_file failed (%s)\n", strerror(err));
2085 TALLOC_FREE(smb_fname);
2086 TALLOC_FREE(pathref_fname);
2087 return map_nt_error_from_unix(err);
2089 TALLOC_FREE(smb_fname);
2090 TALLOC_FREE(pathref_fname);
2091 return NT_STATUS_OK;
2094 /* Afaik translate name was first introduced with vfs_catia, to be able
2095 to translate unix file/dir-names, containing invalid windows characters,
2096 to valid windows names.
2097 The used translation direction is always unix --> windows
2099 static NTSTATUS cmd_translate_name(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
2100 int argc, const char **argv)
2102 const char *dname = NULL;
2103 char *dname_talloced = NULL;
2106 char *translated = NULL;
2107 struct smb_filename *smb_fname = NULL;
2111 DEBUG(0, ("Usage: translate_name unix_filename\n"));
2112 return NT_STATUS_UNSUCCESSFUL;
2115 smb_fname = synthetic_smb_fname(talloc_tos(),
2121 if (smb_fname == NULL) {
2122 return NT_STATUS_NO_MEMORY;
2125 status = OpenDir(vfs->conn,
2131 if (!NT_STATUS_IS_OK(status)) {
2132 int err = map_errno_from_nt_status(status);
2133 DEBUG(0, ("cmd_translate_name: opendir error=%d (%s)\n",
2134 err, strerror(err)));
2135 TALLOC_FREE(smb_fname);
2137 return NT_STATUS_UNSUCCESSFUL;
2139 vfs->currentdir_offset = 0;
2142 /* ReadDirName() returns Windows "encoding" */
2143 dname = ReadDirName(vfs->currentdir,
2144 &vfs->currentdir_offset,
2147 if (dname == NULL) {
2151 /* Convert Windows "encoding" from ReadDirName() to UNIX */
2152 status = SMB_VFS_TRANSLATE_NAME(vfs->conn,
2154 vfs_translate_to_unix,
2157 if (!NT_STATUS_IS_OK(status)) {
2158 DBG_ERR("file '%s' cannot be translated\n", argv[1]);
2163 * argv[1] uses UNIX "encoding", so compare with translation
2166 if (strcmp(translated, argv[1]) == 0) {
2170 TALLOC_FREE(dname_talloced);
2171 TALLOC_FREE(translated);
2175 DEBUG(0, ("cmd_translate_name: file '%s' not found.\n",
2177 status = NT_STATUS_UNSUCCESSFUL;
2181 /* translation success. But that could also mean
2182 that translating "aaa" to "aaa" was successful :-(
2184 DBG_ERR("file '%s' --> '%s'\n", argv[1], dname);
2185 status = NT_STATUS_OK;
2188 TALLOC_FREE(dname_talloced);
2189 TALLOC_FREE(translated);
2190 TALLOC_FREE(smb_fname);
2191 TALLOC_FREE(vfs->currentdir);
2192 vfs->currentdir_offset = 0;
2197 * This is a quick hack to demonstrate a crash in the full_audit
2198 * module when passing fsp->smb_fname into SMB_VFS_CREATE_FILE leading
2201 * Feel free to expand with more options as needed
2203 static NTSTATUS cmd_create_file(
2204 struct vfs_state *vfs,
2205 TALLOC_CTX *mem_ctx,
2209 struct smb_filename *fname = NULL;
2210 struct files_struct *fsp = NULL;
2215 DBG_ERR("Usage: create_file filename\n");
2216 return NT_STATUS_UNSUCCESSFUL;
2219 fname = synthetic_smb_fname(
2220 talloc_tos(), argv[1], NULL, NULL, 0, 0);
2221 if (fname == NULL) {
2222 return NT_STATUS_NO_MEMORY;
2225 ret = vfs_stat(vfs->conn, fname);
2227 status = map_nt_error_from_unix(errno);
2228 DBG_DEBUG("vfs_stat() failed: %s\n", strerror(errno));
2233 status = openat_pathref_fsp(vfs->conn->cwd_fsp, fname);
2234 if (!NT_STATUS_IS_OK(status)) {
2235 DBG_DEBUG("Could not open %s: %s\n",
2242 status = SMB_VFS_CREATE_FILE(
2248 * Using fname->fsp->fsp_name seems to be legal,
2249 * there's code to handle this in
2250 * create_file_unixpath(). And it is actually very
2251 * worthwhile re-using the fsp_name, we can save quite
2252 * a few copies of smb_filename with that.
2254 fname->fsp->fsp_name,
2258 FILE_NON_DIRECTORY_FILE,
2271 DBG_DEBUG("create_file returned %s\n", nt_errstr(status));
2275 return NT_STATUS_OK;
2278 struct cmd_set vfs_commands[] = {
2280 { .name = "VFS Commands" },
2282 { "load", cmd_load_module, "Load a module", "load <module.so>" },
2283 { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
2284 { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
2285 { "connect", cmd_connect, "VFS connect()", "connect" },
2286 { "disconnect", cmd_disconnect, "VFS disconnect()", "disconnect" },
2287 { "disk_free", cmd_disk_free, "VFS disk_free()", "disk_free <path>" },
2288 { "opendir", cmd_opendir, "VFS opendir()", "opendir <fname>" },
2289 { "readdir", cmd_readdir, "VFS readdir()", "readdir" },
2290 { "mkdir", cmd_mkdir, "VFS mkdir()", "mkdir <path>" },
2291 { "rmdir", cmd_pathfunc, "VFS rmdir()", "rmdir <path>" },
2292 { "closedir", cmd_closedir, "VFS closedir()", "closedir" },
2293 { "open", cmd_open, "VFS open()", "open <fname> <flags> <mode>" },
2294 { "close", cmd_close, "VFS close()", "close <fd>" },
2295 { "read", cmd_read, "VFS read()", "read <fd> <size>" },
2296 { "write", cmd_write, "VFS write()", "write <fd> <size>" },
2297 { "lseek", cmd_lseek, "VFS lseek()", "lseek <fd> <offset> <whence>" },
2298 { "rename", cmd_rename, "VFS rename()", "rename <old> <new>" },
2299 { "fsync", cmd_fsync, "VFS fsync()", "fsync <fd>" },
2300 { "stat", cmd_stat, "VFS stat()", "stat <fname>" },
2301 { "fstat", cmd_fstat, "VFS fstat()", "fstat <fd>" },
2302 { "lstat", cmd_lstat, "VFS lstat()", "lstat <fname>" },
2303 { "unlink", cmd_pathfunc, "VFS unlink()", "unlink <fname>" },
2304 { "chmod", cmd_chmod, "VFS chmod()", "chmod <path> <mode>" },
2305 { "fchmod", cmd_fchmod, "VFS fchmod()", "fchmod <fd> <mode>" },
2306 { "fchown", cmd_fchown, "VFS fchown()", "fchown <fd> <uid> <gid>" },
2307 { "chdir", cmd_pathfunc, "VFS chdir()", "chdir <path>" },
2308 { "getwd", cmd_getwd, "VFS getwd()", "getwd" },
2309 { "utime", cmd_utime, "VFS utime()", "utime <path> <access> <modify>" },
2310 { "ftruncate", cmd_ftruncate, "VFS ftruncate()", "ftruncate <fd> <length>" },
2311 { "lock", cmd_lock, "VFS lock()", "lock <f> <op> <offset> <count> <type>" },
2312 { "symlink", cmd_symlink, "VFS symlink()", "symlink <old> <new>" },
2313 { "readlink", cmd_readlink, "VFS readlink()", "readlink <path>" },
2314 { "link", cmd_link, "VFS link()", "link <oldpath> <newpath>" },
2315 { "mknod", cmd_mknod, "VFS mknod()", "mknod <path> <mode> <dev>" },
2316 { "realpath", cmd_realpath, "VFS realpath()", "realpath <path>" },
2317 { "getxattr", cmd_getxattr, "VFS getxattr()",
2318 "getxattr <path> <name>" },
2319 { "listxattr", cmd_listxattr, "VFS listxattr()",
2320 "listxattr <path>" },
2321 { "fsetxattr", cmd_fsetxattr, "VFS fsetxattr()",
2322 "fsetxattr <path> <name> <value> [<flags>]" },
2323 { "removexattr", cmd_removexattr, "VFS removexattr()",
2324 "removexattr <path> <name>\n" },
2325 { "fget_nt_acl", cmd_fget_nt_acl, "VFS fget_nt_acl()",
2326 "fget_nt_acl <fd>\n" },
2327 { "get_nt_acl", cmd_get_nt_acl, "VFS get_nt_acl()",
2328 "get_nt_acl <path>\n" },
2329 { "fset_nt_acl", cmd_fset_nt_acl, "VFS fset_nt_acl()",
2330 "fset_nt_acl <fd>\n" },
2331 { "set_nt_acl", cmd_set_nt_acl, "VFS open() and fset_nt_acl()",
2332 "set_nt_acl <file>\n" },
2333 { "sys_acl_get_file", cmd_sys_acl_get_file, "VFS sys_acl_get_file()", "sys_acl_get_file <path>" },
2334 { "sys_acl_get_fd", cmd_sys_acl_get_fd, "VFS sys_acl_get_fd()", "sys_acl_get_fd <fd>" },
2335 { "sys_acl_blob_get_file", cmd_sys_acl_blob_get_file,
2336 "VFS sys_acl_blob_get_file()", "sys_acl_blob_get_file <path>" },
2337 { "sys_acl_blob_get_fd", cmd_sys_acl_blob_get_fd,
2338 "VFS sys_acl_blob_get_fd()", "sys_acl_blob_get_fd <path>" },
2339 { "sys_acl_delete_def_file", cmd_sys_acl_delete_def_file, "VFS sys_acl_delete_def_file()", "sys_acl_delete_def_file <path>" },
2342 #if defined(WITH_SMB1SERVER)
2343 { "test_chain", cmd_test_chain, "test chain code",
2346 { "translate_name", cmd_translate_name, "VFS translate_name()", "translate_name unix_filename" },
2349 "VFS create_file()",
2350 "create_file <filename>"