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 SMB_VFS_CONNECT(vfs->conn, lp_servicename(talloc_tos(), SNUM(vfs->conn)), "vfstest");
111 static NTSTATUS cmd_disconnect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
113 SMB_VFS_DISCONNECT(vfs->conn);
117 static NTSTATUS cmd_disk_free(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
119 struct smb_filename *smb_fname = NULL;
120 uint64_t diskfree, bsize, dfree, dsize;
122 printf("Usage: disk_free <path>\n");
126 smb_fname = synthetic_smb_fname(talloc_tos(),
131 if (smb_fname == NULL) {
132 return NT_STATUS_NO_MEMORY;
134 diskfree = SMB_VFS_DISK_FREE(vfs->conn, smb_fname,
135 &bsize, &dfree, &dsize);
136 printf("disk_free: %lu, bsize = %lu, dfree = %lu, dsize = %lu\n",
137 (unsigned long)diskfree,
138 (unsigned long)bsize,
139 (unsigned long)dfree,
140 (unsigned long)dsize);
145 static NTSTATUS cmd_opendir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
147 struct smb_filename *smb_fname = NULL;
150 printf("Usage: opendir <fname>\n");
154 smb_fname = synthetic_smb_fname(talloc_tos(),
159 if (smb_fname == NULL) {
160 return NT_STATUS_NO_MEMORY;
163 vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, smb_fname, NULL, 0);
164 if (vfs->currentdir == NULL) {
165 printf("opendir error=%d (%s)\n", errno, strerror(errno));
166 TALLOC_FREE(smb_fname);
167 return NT_STATUS_UNSUCCESSFUL;
170 TALLOC_FREE(smb_fname);
171 printf("opendir: ok\n");
176 static NTSTATUS cmd_readdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
179 struct dirent *dent = NULL;
181 if (vfs->currentdir == NULL) {
182 printf("readdir: error=-1 (no open directory)\n");
183 return NT_STATUS_UNSUCCESSFUL;
186 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir, &st);
188 printf("readdir: NULL\n");
192 printf("readdir: %s\n", dent->d_name);
193 if (VALID_STAT(st)) {
195 printf(" stat available");
196 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
197 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
198 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
199 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
200 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
201 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
202 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
203 printf(" Size: %10u", (unsigned int)st.st_ex_size);
204 #ifdef HAVE_STAT_ST_BLOCKS
205 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
207 #ifdef HAVE_STAT_ST_BLKSIZE
208 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
210 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
211 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
212 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
213 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
214 printf(" Uid: %5lu Gid: %5lu\n",
215 (unsigned long)st.st_ex_uid,
216 (unsigned long)st.st_ex_gid);
217 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
218 printf(" Access: %s", ctime(&tmp_time));
219 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
220 printf(" Modify: %s", ctime(&tmp_time));
221 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
222 printf(" Change: %s", ctime(&tmp_time));
229 static NTSTATUS cmd_mkdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
231 struct smb_filename *smb_fname = NULL;
234 printf("Usage: mkdir <path>\n");
238 smb_fname = synthetic_smb_fname(talloc_tos(),
244 if (smb_fname == NULL) {
245 return NT_STATUS_NO_MEMORY;
248 if (SMB_VFS_MKDIR(vfs->conn, smb_fname, 00755) == -1) {
249 printf("mkdir error=%d (%s)\n", errno, strerror(errno));
250 return NT_STATUS_UNSUCCESSFUL;
253 printf("mkdir: ok\n");
258 static NTSTATUS cmd_closedir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
262 if (vfs->currentdir == NULL) {
263 printf("closedir: failure (no directory open)\n");
264 return NT_STATUS_UNSUCCESSFUL;
267 ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
269 printf("closedir failure: %s\n", strerror(errno));
270 return NT_STATUS_UNSUCCESSFUL;
273 printf("closedir: ok\n");
274 vfs->currentdir = NULL;
279 static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
285 struct smb_filename *smb_fname = NULL;
291 if (argc < 3 || argc > 5) {
292 printf("Usage: open <filename> <flags> <mode>\n");
293 printf(" flags: O = O_RDONLY\n");
294 printf(" R = O_RDWR\n");
295 printf(" W = O_WRONLY\n");
296 printf(" C = O_CREAT\n");
297 printf(" E = O_EXCL\n");
298 printf(" T = O_TRUNC\n");
299 printf(" A = O_APPEND\n");
300 printf(" N = O_NONBLOCK/O_NDELAY\n");
302 printf(" S = O_SYNC\n");
305 printf(" F = O_NOFOLLOW\n");
307 printf(" mode: see open.2\n");
308 printf(" mode is ignored if C flag not present\n");
309 printf(" mode defaults to 00400\n");
351 printf("open: error=-1 (invalid flag!)\n");
352 return NT_STATUS_UNSUCCESSFUL;
356 if ((flags & O_CREAT) && argc == 4) {
357 if (sscanf(argv[3], "%ho", (unsigned short *)&mode) == 0) {
358 printf("open: error=-1 (invalid mode!)\n");
359 return NT_STATUS_UNSUCCESSFUL;
363 fsp = talloc_zero(vfs, struct files_struct);
365 return NT_STATUS_NO_MEMORY;
367 fsp->fh = talloc_zero(fsp, struct fd_handle);
368 if (fsp->fh == NULL) {
370 return NT_STATUS_NO_MEMORY;
372 fsp->conn = vfs->conn;
374 smb_fname = synthetic_smb_fname_split(NULL,
376 lp_posix_pathnames());
377 if (smb_fname == NULL) {
379 return NT_STATUS_NO_MEMORY;
382 fsp->fsp_name = smb_fname;
384 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, flags, mode);
385 if (fsp->fh->fd == -1) {
386 printf("open: error=%d (%s)\n", errno, strerror(errno));
388 TALLOC_FREE(smb_fname);
389 return NT_STATUS_UNSUCCESSFUL;
392 status = NT_STATUS_OK;
393 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
395 /* If we have an fd, this stat should succeed. */
396 DEBUG(0,("Error doing fstat on open file %s "
398 smb_fname_str_dbg(smb_fname),
400 status = map_nt_error_from_unix(errno);
401 } else if (S_ISDIR(smb_fname->st.st_ex_mode)) {
403 status = NT_STATUS_FILE_IS_A_DIRECTORY;
406 if (!NT_STATUS_IS_OK(status)) {
409 TALLOC_FREE(smb_fname);
413 fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
414 fsp->vuid = UID_FIELD_INVALID;
416 fsp->can_lock = True;
417 fsp->can_read = True;
419 CAN_WRITE(vfs->conn);
420 fsp->print_file = NULL;
421 fsp->modified = False;
422 fsp->sent_oplock_break = NO_BREAK_SENT;
423 fsp->is_directory = False;
425 vfs->files[fsp->fh->fd] = fsp;
426 printf("open: fd=%d\n", fsp->fh->fd);
431 static NTSTATUS cmd_pathfunc(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
433 struct smb_filename *smb_fname = NULL;
437 printf("Usage: %s <path>\n", argv[0]);
441 smb_fname = synthetic_smb_fname(talloc_tos(),
447 if (smb_fname == NULL) {
448 return NT_STATUS_NO_MEMORY;
451 if (strcmp("rmdir", argv[0]) == 0 ) {
452 ret = SMB_VFS_RMDIR(vfs->conn, smb_fname);
453 TALLOC_FREE(smb_fname);
454 } else if (strcmp("unlink", argv[0]) == 0 ) {
455 TALLOC_FREE(smb_fname);
456 /* unlink can be a stream:name */
457 smb_fname = synthetic_smb_fname_split(talloc_tos(),
459 lp_posix_pathnames());
460 if (smb_fname == NULL) {
461 return NT_STATUS_NO_MEMORY;
463 ret = SMB_VFS_UNLINK(vfs->conn, smb_fname);
464 TALLOC_FREE(smb_fname);
465 } else if (strcmp("chdir", argv[0]) == 0 ) {
466 ret = SMB_VFS_CHDIR(vfs->conn, smb_fname);
467 TALLOC_FREE(smb_fname);
469 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
470 return NT_STATUS_UNSUCCESSFUL;
474 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
475 return NT_STATUS_UNSUCCESSFUL;
478 printf("%s: ok\n", argv[0]);
483 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
488 printf("Usage: close <fd>\n");
493 if (vfs->files[fd] == NULL) {
494 printf("close: error=-1 (invalid file descriptor)\n");
498 ret = SMB_VFS_CLOSE(vfs->files[fd]);
500 printf("close: error=%d (%s)\n", errno, strerror(errno));
502 printf("close: ok\n");
504 TALLOC_FREE(vfs->files[fd]);
505 vfs->files[fd] = NULL;
510 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
516 printf("Usage: read <fd> <size>\n");
520 /* do some error checking on these */
522 size = atoi(argv[2]);
523 vfs->data = talloc_array(mem_ctx, char, size);
524 if (vfs->data == NULL) {
525 printf("read: error=-1 (not enough memory)");
526 return NT_STATUS_UNSUCCESSFUL;
528 vfs->data_size = size;
530 rsize = read_file(vfs->files[fd], vfs->data, 0, size);
532 printf("read: error=%d (%s)\n", errno, strerror(errno));
533 return NT_STATUS_UNSUCCESSFUL;
536 printf("read: ok\n");
541 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
546 printf("Usage: write <fd> <size>\n");
550 /* some error checking should go here */
552 size = atoi(argv[2]);
553 if (vfs->data == NULL) {
554 printf("write: error=-1 (buffer empty, please populate it before writing)");
555 return NT_STATUS_UNSUCCESSFUL;
558 if (vfs->data_size < size) {
559 printf("write: error=-1 (buffer too small, please put some more data in)");
560 return NT_STATUS_UNSUCCESSFUL;
563 wsize = write_file(NULL, vfs->files[fd], vfs->data, 0, size);
566 printf("write: error=%d (%s)\n", errno, strerror(errno));
567 return NT_STATUS_UNSUCCESSFUL;
570 printf("write: ok\n");
575 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
577 int fd, offset, whence;
581 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
586 offset = atoi(argv[2]);
587 whence = atoi(argv[3]);
589 case 1: whence = SEEK_SET; break;
590 case 2: whence = SEEK_CUR; break;
591 default: whence = SEEK_END;
594 pos = SMB_VFS_LSEEK(vfs->files[fd], offset, whence);
595 if (pos == (off_t)-1) {
596 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
597 return NT_STATUS_UNSUCCESSFUL;
600 printf("lseek: ok\n");
605 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
608 struct smb_filename *smb_fname_src = NULL;
609 struct smb_filename *smb_fname_dst = NULL;
612 printf("Usage: rename <old> <new>\n");
616 smb_fname_src = synthetic_smb_fname_split(mem_ctx,
618 lp_posix_pathnames());
619 if (smb_fname_src == NULL) {
620 return NT_STATUS_NO_MEMORY;
623 smb_fname_dst = synthetic_smb_fname_split(mem_ctx,
625 lp_posix_pathnames());
626 if (smb_fname_dst == NULL) {
627 TALLOC_FREE(smb_fname_src);
628 return NT_STATUS_NO_MEMORY;
631 ret = SMB_VFS_RENAME(vfs->conn, smb_fname_src, smb_fname_dst);
632 TALLOC_FREE(smb_fname_src);
633 TALLOC_FREE(smb_fname_dst);
635 printf("rename: error=%d (%s)\n", errno, strerror(errno));
636 return NT_STATUS_UNSUCCESSFUL;
639 printf("rename: ok\n");
643 static NTSTATUS cmd_fsync(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
647 printf("Usage: fsync <fd>\n");
652 ret = smb_vfs_fsync_sync(vfs->files[fd]);
654 printf("fsync: error=%d (%s)\n", errno, strerror(errno));
655 return NT_STATUS_UNSUCCESSFUL;
658 printf("fsync: ok\n");
663 static NTSTATUS cmd_stat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
668 struct passwd *pwd = NULL;
669 struct group *grp = NULL;
670 struct smb_filename *smb_fname = NULL;
675 printf("Usage: stat <fname>\n");
679 smb_fname = synthetic_smb_fname_split(mem_ctx,
681 lp_posix_pathnames());
682 if (smb_fname == NULL) {
683 return NT_STATUS_NO_MEMORY;
686 ret = SMB_VFS_STAT(vfs->conn, smb_fname);
688 printf("stat: error=%d (%s)\n", errno, strerror(errno));
689 TALLOC_FREE(smb_fname);
690 return NT_STATUS_UNSUCCESSFUL;
693 TALLOC_FREE(smb_fname);
695 pwd = getpwuid(st.st_ex_uid);
696 if (pwd != NULL) user = pwd->pw_name;
697 else user = null_string;
698 grp = getgrgid(st.st_ex_gid);
699 if (grp != NULL) group = grp->gr_name;
700 else group = null_string;
702 printf("stat: ok\n");
703 printf(" File: %s", argv[1]);
704 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
705 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
706 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
707 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
708 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
709 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
710 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
711 printf(" Size: %10u", (unsigned int)st.st_ex_size);
712 #ifdef HAVE_STAT_ST_BLOCKS
713 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
715 #ifdef HAVE_STAT_ST_BLKSIZE
716 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
718 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
719 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
720 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
721 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
722 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
723 (unsigned long)st.st_ex_gid, group);
724 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
725 printf(" Access: %s", ctime(&tmp_time));
726 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
727 printf(" Modify: %s", ctime(&tmp_time));
728 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
729 printf(" Change: %s", ctime(&tmp_time));
735 static NTSTATUS cmd_fstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
740 struct passwd *pwd = NULL;
741 struct group *grp = NULL;
746 printf("Usage: fstat <fd>\n");
751 if (fd < 0 || fd >= 1024) {
752 printf("fstat: error=%d (file descriptor out of range)\n", EBADF);
756 if (vfs->files[fd] == NULL) {
757 printf("fstat: error=%d (invalid file descriptor)\n", EBADF);
761 if (SMB_VFS_FSTAT(vfs->files[fd], &st) == -1) {
762 printf("fstat: error=%d (%s)\n", errno, strerror(errno));
763 return NT_STATUS_UNSUCCESSFUL;
766 pwd = getpwuid(st.st_ex_uid);
767 if (pwd != NULL) user = pwd->pw_name;
768 else user = null_string;
769 grp = getgrgid(st.st_ex_gid);
770 if (grp != NULL) group = grp->gr_name;
771 else group = null_string;
773 printf("fstat: ok\n");
774 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
775 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
776 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
777 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
778 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
779 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
780 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
781 printf(" Size: %10u", (unsigned int)st.st_ex_size);
782 #ifdef HAVE_STAT_ST_BLOCKS
783 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
785 #ifdef HAVE_STAT_ST_BLKSIZE
786 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
788 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
789 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
790 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
791 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
792 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
793 (unsigned long)st.st_ex_gid, group);
794 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
795 printf(" Access: %s", ctime(&tmp_time));
796 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
797 printf(" Modify: %s", ctime(&tmp_time));
798 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
799 printf(" Change: %s", ctime(&tmp_time));
805 static NTSTATUS cmd_lstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
809 struct passwd *pwd = NULL;
810 struct group *grp = NULL;
811 struct smb_filename *smb_fname = NULL;
816 printf("Usage: lstat <path>\n");
820 smb_fname = synthetic_smb_fname_split(mem_ctx,
822 lp_posix_pathnames());
823 if (smb_fname == NULL) {
824 return NT_STATUS_NO_MEMORY;
827 if (SMB_VFS_LSTAT(vfs->conn, smb_fname) == -1) {
828 printf("lstat: error=%d (%s)\n", errno, strerror(errno));
829 TALLOC_FREE(smb_fname);
830 return NT_STATUS_UNSUCCESSFUL;
833 TALLOC_FREE(smb_fname);
835 pwd = getpwuid(st.st_ex_uid);
836 if (pwd != NULL) user = pwd->pw_name;
837 else user = null_string;
838 grp = getgrgid(st.st_ex_gid);
839 if (grp != NULL) group = grp->gr_name;
840 else group = null_string;
842 printf("lstat: ok\n");
843 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
844 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
845 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
846 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
847 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
848 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
849 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
850 printf(" Size: %10u", (unsigned int)st.st_ex_size);
851 #ifdef HAVE_STAT_ST_BLOCKS
852 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
854 #ifdef HAVE_STAT_ST_BLKSIZE
855 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
857 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
858 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
859 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
860 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
861 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
862 (unsigned long)st.st_ex_gid, group);
863 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
864 printf(" Access: %s", ctime(&tmp_time));
865 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
866 printf(" Modify: %s", ctime(&tmp_time));
867 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
868 printf(" Change: %s", ctime(&tmp_time));
874 static NTSTATUS cmd_chmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
876 struct smb_filename *smb_fname = NULL;
879 printf("Usage: chmod <path> <mode>\n");
883 mode = atoi(argv[2]);
885 smb_fname = synthetic_smb_fname(talloc_tos(),
890 if (smb_fname == NULL) {
891 return NT_STATUS_NO_MEMORY;
894 if (SMB_VFS_CHMOD(vfs->conn, smb_fname, mode) == -1) {
895 printf("chmod: error=%d (%s)\n", errno, strerror(errno));
896 return NT_STATUS_UNSUCCESSFUL;
899 printf("chmod: ok\n");
904 static NTSTATUS cmd_fchmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
909 printf("Usage: fchmod <fd> <mode>\n");
914 mode = atoi(argv[2]);
915 if (fd < 0 || fd >= 1024) {
916 printf("fchmod: error=%d (file descriptor out of range)\n", EBADF);
919 if (vfs->files[fd] == NULL) {
920 printf("fchmod: error=%d (invalid file descriptor)\n", EBADF);
924 if (SMB_VFS_FCHMOD(vfs->files[fd], mode) == -1) {
925 printf("fchmod: error=%d (%s)\n", errno, strerror(errno));
926 return NT_STATUS_UNSUCCESSFUL;
929 printf("fchmod: ok\n");
933 static NTSTATUS cmd_chown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
935 struct smb_filename *smb_fname = NULL;
939 printf("Usage: chown <path> <uid> <gid>\n");
946 smb_fname = synthetic_smb_fname(talloc_tos(),
951 if (smb_fname == NULL) {
952 return NT_STATUS_NO_MEMORY;
955 if (SMB_VFS_CHOWN(vfs->conn, smb_fname, uid, gid) == -1) {
956 printf("chown: error=%d (%s)\n", errno, strerror(errno));
957 return NT_STATUS_UNSUCCESSFUL;
960 printf("chown: ok\n");
965 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
971 printf("Usage: fchown <fd> <uid> <gid>\n");
978 if (fd < 0 || fd >= 1024) {
979 printf("fchown: faliure=%d (file descriptor out of range)\n", EBADF);
982 if (vfs->files[fd] == NULL) {
983 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
986 if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
987 printf("fchown error=%d (%s)\n", errno, strerror(errno));
988 return NT_STATUS_UNSUCCESSFUL;
991 printf("fchown: ok\n");
996 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
998 struct smb_filename *smb_fname = SMB_VFS_GETWD(vfs->conn, talloc_tos());
999 if (smb_fname == NULL) {
1000 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
1001 return NT_STATUS_UNSUCCESSFUL;
1004 printf("getwd: %s\n", smb_fname->base_name);
1005 TALLOC_FREE(smb_fname);
1006 return NT_STATUS_OK;
1009 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1011 struct smb_file_time ft;
1012 struct smb_filename *smb_fname = NULL;
1015 printf("Usage: utime <path> <access> <modify>\n");
1016 return NT_STATUS_OK;
1021 ft.atime = convert_time_t_to_timespec(atoi(argv[2]));
1022 ft.mtime = convert_time_t_to_timespec(atoi(argv[3]));
1024 smb_fname = synthetic_smb_fname_split(mem_ctx,
1026 lp_posix_pathnames());
1027 if (smb_fname == NULL) {
1028 return NT_STATUS_NO_MEMORY;
1031 if (SMB_VFS_NTIMES(vfs->conn, smb_fname, &ft) != 0) {
1032 printf("utime: error=%d (%s)\n", errno, strerror(errno));
1033 TALLOC_FREE(smb_fname);
1034 return NT_STATUS_UNSUCCESSFUL;
1037 TALLOC_FREE(smb_fname);
1038 printf("utime: ok\n");
1039 return NT_STATUS_OK;
1042 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1047 printf("Usage: ftruncate <fd> <length>\n");
1048 return NT_STATUS_OK;
1052 off = atoi(argv[2]);
1053 if (fd < 0 || fd >= 1024) {
1054 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
1055 return NT_STATUS_OK;
1057 if (vfs->files[fd] == NULL) {
1058 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
1059 return NT_STATUS_OK;
1062 if (SMB_VFS_FTRUNCATE(vfs->files[fd], off) == -1) {
1063 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
1064 return NT_STATUS_UNSUCCESSFUL;
1067 printf("ftruncate: ok\n");
1068 return NT_STATUS_OK;
1071 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1078 const char *typestr;
1081 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
1082 printf(" ops: G = F_GETLK\n");
1083 printf(" S = F_SETLK\n");
1084 printf(" W = F_SETLKW\n");
1085 printf(" type: R = F_RDLCK\n");
1086 printf(" W = F_WRLCK\n");
1087 printf(" U = F_UNLCK\n");
1088 return NT_STATUS_OK;
1091 if (sscanf(argv[1], "%d", &fd) == 0) {
1092 printf("lock: error=-1 (error parsing fd)\n");
1093 return NT_STATUS_UNSUCCESSFUL;
1108 printf("lock: error=-1 (invalid op flag!)\n");
1109 return NT_STATUS_UNSUCCESSFUL;
1112 if (sscanf(argv[3], "%ld", &offset) == 0) {
1113 printf("lock: error=-1 (error parsing fd)\n");
1114 return NT_STATUS_UNSUCCESSFUL;
1117 if (sscanf(argv[4], "%ld", &count) == 0) {
1118 printf("lock: error=-1 (error parsing fd)\n");
1119 return NT_STATUS_UNSUCCESSFUL;
1136 printf("lock: error=-1 (invalid type flag!)\n");
1137 return NT_STATUS_UNSUCCESSFUL;
1142 printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
1144 if (SMB_VFS_LOCK(vfs->files[fd], op, offset, count, type) == False) {
1145 printf("lock: error=%d (%s)\n", errno, strerror(errno));
1146 return NT_STATUS_UNSUCCESSFUL;
1149 printf("lock: ok\n");
1150 return NT_STATUS_OK;
1153 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1155 struct smb_filename *new_smb_fname = NULL;
1158 printf("Usage: symlink <path> <link>\n");
1159 return NT_STATUS_OK;
1162 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1164 lp_posix_pathnames());
1165 if (new_smb_fname == NULL) {
1166 return NT_STATUS_NO_MEMORY;
1168 if (SMB_VFS_SYMLINK(vfs->conn, argv[1], new_smb_fname) == -1) {
1169 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
1170 return NT_STATUS_UNSUCCESSFUL;
1173 printf("symlink: ok\n");
1174 return NT_STATUS_OK;
1178 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1180 char buffer[PATH_MAX];
1181 struct smb_filename *smb_fname = NULL;
1185 printf("Usage: readlink <path>\n");
1186 return NT_STATUS_OK;
1189 smb_fname = synthetic_smb_fname_split(mem_ctx,
1191 lp_posix_pathnames());
1192 if (smb_fname == NULL) {
1193 return NT_STATUS_NO_MEMORY;
1195 if ((size = SMB_VFS_READLINK(vfs->conn, smb_fname,
1196 buffer, PATH_MAX)) == -1) {
1197 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
1198 return NT_STATUS_UNSUCCESSFUL;
1201 buffer[size] = '\0';
1202 printf("readlink: %s\n", buffer);
1203 return NT_STATUS_OK;
1207 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1209 struct smb_filename *old_smb_fname = NULL;
1210 struct smb_filename *new_smb_fname = NULL;
1213 printf("Usage: link <path> <link>\n");
1214 return NT_STATUS_OK;
1217 old_smb_fname = synthetic_smb_fname_split(mem_ctx,
1219 lp_posix_pathnames());
1220 if (old_smb_fname == NULL) {
1221 return NT_STATUS_NO_MEMORY;
1223 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1225 lp_posix_pathnames());
1226 if (new_smb_fname == NULL) {
1227 return NT_STATUS_NO_MEMORY;
1230 if (SMB_VFS_LINK(vfs->conn, old_smb_fname, new_smb_fname) == -1) {
1231 printf("link: error=%d (%s)\n", errno, strerror(errno));
1232 return NT_STATUS_UNSUCCESSFUL;
1235 printf("link: ok\n");
1236 return NT_STATUS_OK;
1239 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1242 unsigned int dev_val;
1244 struct smb_filename *smb_fname = NULL;
1247 printf("Usage: mknod <path> <mode> <dev>\n");
1248 printf(" mode is octal\n");
1249 printf(" dev is hex\n");
1250 return NT_STATUS_OK;
1253 if (sscanf(argv[2], "%ho", (unsigned short *)&mode) == 0) {
1254 printf("open: error=-1 (invalid mode!)\n");
1255 return NT_STATUS_UNSUCCESSFUL;
1258 if (sscanf(argv[3], "%x", &dev_val) == 0) {
1259 printf("open: error=-1 (invalid dev!)\n");
1260 return NT_STATUS_UNSUCCESSFUL;
1262 dev = (SMB_DEV_T)dev_val;
1264 smb_fname = synthetic_smb_fname_split(mem_ctx,
1266 lp_posix_pathnames());
1267 if (smb_fname == NULL) {
1268 return NT_STATUS_NO_MEMORY;
1270 if (SMB_VFS_MKNOD(vfs->conn, smb_fname, mode, dev) == -1) {
1271 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1272 return NT_STATUS_UNSUCCESSFUL;
1275 printf("mknod: ok\n");
1276 return NT_STATUS_OK;
1279 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1281 struct smb_filename *smb_fname = NULL;
1284 printf("Usage: realpath <path>\n");
1285 return NT_STATUS_OK;
1288 smb_fname = synthetic_smb_fname_split(mem_ctx,
1290 lp_posix_pathnames());
1291 if (smb_fname == NULL) {
1292 return NT_STATUS_NO_MEMORY;
1294 if (SMB_VFS_REALPATH(vfs->conn, mem_ctx, smb_fname) == NULL) {
1295 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1296 return NT_STATUS_UNSUCCESSFUL;
1299 printf("realpath: ok\n");
1300 return NT_STATUS_OK;
1303 static NTSTATUS cmd_getxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1304 int argc, const char **argv)
1308 struct smb_filename *smb_fname = NULL;
1311 printf("Usage: getxattr <path> <xattr>\n");
1312 return NT_STATUS_OK;
1317 smb_fname = synthetic_smb_fname_split(mem_ctx,
1319 lp_posix_pathnames());
1320 if (smb_fname == NULL) {
1321 return NT_STATUS_NO_MEMORY;
1323 ret = SMB_VFS_GETXATTR(vfs->conn, smb_fname, argv[2], buf,
1324 talloc_get_size(buf));
1327 printf("getxattr returned (%s)\n", strerror(err));
1328 return map_nt_error_from_unix(err);
1330 buf = talloc_array(mem_ctx, uint8_t, ret);
1332 return NT_STATUS_NO_MEMORY;
1334 ret = SMB_VFS_GETXATTR(vfs->conn, smb_fname, argv[2], buf,
1335 talloc_get_size(buf));
1338 printf("getxattr returned (%s)\n", strerror(err));
1339 return map_nt_error_from_unix(err);
1341 dump_data_file(buf, talloc_get_size(buf), false, stdout);
1342 return NT_STATUS_OK;
1345 static NTSTATUS cmd_listxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1346 int argc, const char **argv)
1350 struct smb_filename *smb_fname = NULL;
1353 printf("Usage: listxattr <path>\n");
1354 return NT_STATUS_OK;
1359 smb_fname = synthetic_smb_fname_split(mem_ctx,
1361 lp_posix_pathnames());
1362 if (smb_fname == NULL) {
1363 return NT_STATUS_NO_MEMORY;
1365 ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1366 buf, talloc_get_size(buf));
1369 printf("listxattr returned (%s)\n", strerror(err));
1370 return map_nt_error_from_unix(err);
1372 buf = talloc_array(mem_ctx, char, ret);
1374 return NT_STATUS_NO_MEMORY;
1376 ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1377 buf, talloc_get_size(buf));
1380 printf("listxattr returned (%s)\n", strerror(err));
1381 return map_nt_error_from_unix(err);
1384 return NT_STATUS_OK;
1386 if (buf[ret-1] != '\0') {
1387 printf("listxattr returned non 0-terminated strings\n");
1388 return NT_STATUS_INTERNAL_ERROR;
1392 while (p < buf+ret) {
1397 return NT_STATUS_OK;
1400 static NTSTATUS cmd_setxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1401 int argc, const char **argv)
1405 struct smb_filename *smb_fname = NULL;
1407 if ((argc < 4) || (argc > 5)) {
1408 printf("Usage: setxattr <path> <xattr> <value> [flags]\n");
1409 return NT_STATUS_OK;
1413 flags = atoi(argv[4]);
1416 smb_fname = synthetic_smb_fname_split(mem_ctx,
1418 lp_posix_pathnames());
1419 if (smb_fname == NULL) {
1420 return NT_STATUS_NO_MEMORY;
1422 ret = SMB_VFS_SETXATTR(vfs->conn, smb_fname, argv[2],
1423 argv[3], strlen(argv[3]), flags);
1426 printf("setxattr returned (%s)\n", strerror(err));
1427 return map_nt_error_from_unix(err);
1429 return NT_STATUS_OK;
1432 static NTSTATUS cmd_removexattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1433 int argc, const char **argv)
1436 struct smb_filename *smb_fname = NULL;
1439 printf("Usage: removexattr <path> <xattr>\n");
1440 return NT_STATUS_OK;
1443 smb_fname = synthetic_smb_fname(talloc_tos(),
1449 if (smb_fname == NULL) {
1450 return NT_STATUS_NO_MEMORY;
1453 ret = SMB_VFS_REMOVEXATTR(vfs->conn, smb_fname, argv[2]);
1456 printf("removexattr returned (%s)\n", strerror(err));
1457 return map_nt_error_from_unix(err);
1459 return NT_STATUS_OK;
1462 static NTSTATUS cmd_fget_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1463 int argc, const char **argv)
1467 struct security_descriptor *sd;
1470 printf("Usage: fget_nt_acl <fd>\n");
1471 return NT_STATUS_OK;
1475 if (fd < 0 || fd >= 1024) {
1476 printf("fget_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1477 return NT_STATUS_OK;
1479 if (vfs->files[fd] == NULL) {
1480 printf("fget_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1481 return NT_STATUS_OK;
1484 status = SMB_VFS_FGET_NT_ACL(vfs->files[fd],
1485 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1487 if (!NT_STATUS_IS_OK(status)) {
1488 printf("fget_nt_acl returned (%s)\n", nt_errstr(status));
1491 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1493 return NT_STATUS_OK;
1496 static NTSTATUS cmd_get_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1497 int argc, const char **argv)
1500 struct security_descriptor *sd;
1501 struct smb_filename *smb_fname = NULL;
1504 printf("Usage: get_nt_acl <path>\n");
1505 return NT_STATUS_OK;
1508 smb_fname = synthetic_smb_fname(talloc_tos(),
1514 if (smb_fname == NULL) {
1515 return NT_STATUS_NO_MEMORY;
1518 status = SMB_VFS_GET_NT_ACL(vfs->conn, smb_fname,
1519 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1521 if (!NT_STATUS_IS_OK(status)) {
1522 printf("get_nt_acl returned (%s)\n", nt_errstr(status));
1525 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1527 return NT_STATUS_OK;
1530 static NTSTATUS cmd_fset_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1531 int argc, const char **argv)
1535 struct security_descriptor *sd;
1538 printf("Usage: fset_nt_acl <fd> <sddl>\n");
1539 return NT_STATUS_OK;
1543 if (fd < 0 || fd >= 1024) {
1544 printf("fset_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1545 return NT_STATUS_OK;
1547 if (vfs->files[fd] == NULL) {
1548 printf("fset_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1549 return NT_STATUS_OK;
1552 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1554 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1555 return NT_STATUS_INVALID_PARAMETER;
1558 status = SMB_VFS_FSET_NT_ACL(vfs->files[fd], SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1559 if (!NT_STATUS_IS_OK(status)) {
1560 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1564 return NT_STATUS_OK;
1567 static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1573 struct smb_filename *smb_fname = NULL;
1575 struct security_descriptor *sd = NULL;
1578 printf("Usage: set_nt_acl <file> <sddl>\n");
1579 return NT_STATUS_OK;
1584 fsp = talloc_zero(vfs, struct files_struct);
1586 return NT_STATUS_NO_MEMORY;
1588 fsp->fh = talloc_zero(fsp, struct fd_handle);
1589 if (fsp->fh == NULL) {
1591 return NT_STATUS_NO_MEMORY;
1593 fsp->conn = vfs->conn;
1595 smb_fname = synthetic_smb_fname_split(NULL,
1597 lp_posix_pathnames());
1598 if (smb_fname == NULL) {
1600 return NT_STATUS_NO_MEMORY;
1603 fsp->fsp_name = smb_fname;
1606 flags = O_RDONLY|O_DIRECTORY;
1608 /* POSIX allows us to open a directory with O_RDONLY. */
1612 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, O_RDWR, mode);
1613 if (fsp->fh->fd == -1 && errno == EISDIR) {
1614 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, flags, mode);
1616 if (fsp->fh->fd == -1) {
1617 printf("open: error=%d (%s)\n", errno, strerror(errno));
1619 TALLOC_FREE(smb_fname);
1620 return NT_STATUS_UNSUCCESSFUL;
1623 status = NT_STATUS_OK;
1624 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
1626 /* If we have an fd, this stat should succeed. */
1627 DEBUG(0,("Error doing fstat on open file %s "
1629 smb_fname_str_dbg(smb_fname),
1631 status = map_nt_error_from_unix(errno);
1634 if (!NT_STATUS_IS_OK(status)) {
1638 fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
1639 fsp->vuid = UID_FIELD_INVALID;
1641 fsp->can_lock = True;
1642 fsp->can_read = True;
1643 fsp->can_write = True;
1644 fsp->print_file = NULL;
1645 fsp->modified = False;
1646 fsp->sent_oplock_break = NO_BREAK_SENT;
1647 fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
1650 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1652 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1653 status = NT_STATUS_INVALID_PARAMETER;
1657 status = SMB_VFS_FSET_NT_ACL(fsp, SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1658 if (!NT_STATUS_IS_OK(status)) {
1659 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1665 ret = SMB_VFS_CLOSE(fsp);
1667 printf("close: error=%d (%s)\n", errno, strerror(errno));
1676 static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1677 int argc, const char **argv)
1684 printf("Usage: sys_acl_get_fd <fd>\n");
1685 return NT_STATUS_OK;
1689 if (fd < 0 || fd >= 1024) {
1690 printf("sys_acl_get_fd: error=%d (file descriptor out of range)\n", EBADF);
1691 return NT_STATUS_OK;
1693 if (vfs->files[fd] == NULL) {
1694 printf("sys_acl_get_fd: error=%d (invalid file descriptor)\n", EBADF);
1695 return NT_STATUS_OK;
1698 acl = SMB_VFS_SYS_ACL_GET_FD(vfs->files[fd], talloc_tos());
1700 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1701 return NT_STATUS_UNSUCCESSFUL;
1703 acl_text = sys_acl_to_text(acl, NULL);
1704 printf("%s", acl_text);
1706 SAFE_FREE(acl_text);
1707 return NT_STATUS_OK;
1710 static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1711 int argc, const char **argv)
1716 struct smb_filename *smb_fname = NULL;
1719 printf("Usage: sys_acl_get_file <path> <type>\n");
1720 return NT_STATUS_OK;
1723 smb_fname = synthetic_smb_fname_split(talloc_tos(),
1725 lp_posix_pathnames());
1726 if (smb_fname == NULL) {
1727 return NT_STATUS_NO_MEMORY;
1729 type = atoi(argv[2]);
1730 acl = SMB_VFS_SYS_ACL_GET_FILE(vfs->conn, smb_fname,
1731 type, talloc_tos());
1733 printf("sys_acl_get_file failed (%s)\n", strerror(errno));
1734 return NT_STATUS_UNSUCCESSFUL;
1736 acl_text = sys_acl_to_text(acl, NULL);
1737 printf("%s", acl_text);
1739 SAFE_FREE(acl_text);
1740 return NT_STATUS_OK;
1743 static NTSTATUS cmd_sys_acl_blob_get_file(struct vfs_state *vfs,
1744 TALLOC_CTX *mem_ctx,
1745 int argc, const char **argv)
1751 struct smb_filename *smb_fname = NULL;
1754 printf("Usage: sys_acl_get_file <path>\n");
1755 return NT_STATUS_OK;
1758 smb_fname = synthetic_smb_fname_split(talloc_tos(),
1760 lp_posix_pathnames());
1761 if (smb_fname == NULL) {
1762 return NT_STATUS_NO_MEMORY;
1764 ret = SMB_VFS_SYS_ACL_BLOB_GET_FILE(vfs->conn, smb_fname, talloc_tos(),
1765 &description, &blob);
1767 printf("sys_acl_blob_get_file failed (%s)\n", strerror(errno));
1768 return map_nt_error_from_unix(errno);
1770 printf("Description: %s\n", description);
1771 for (i = 0; i < blob.length; i++) {
1772 printf("%.2x ", blob.data[i]);
1776 return NT_STATUS_OK;
1779 static NTSTATUS cmd_sys_acl_blob_get_fd(struct vfs_state *vfs,
1780 TALLOC_CTX *mem_ctx,
1781 int argc, const char **argv)
1790 printf("Usage: sys_acl_blob_get_fd <fd>\n");
1791 return NT_STATUS_OK;
1795 if (fd < 0 || fd >= 1024) {
1796 printf("sys_acl_blob_get_fd: error=%d "
1797 "(file descriptor out of range)\n", EBADF);
1798 return NT_STATUS_OK;
1800 if (vfs->files[fd] == NULL) {
1801 printf("sys_acl_blob_get_fd: error=%d "
1802 "(invalid file descriptor)\n", EBADF);
1803 return NT_STATUS_OK;
1806 ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(vfs->files[fd], talloc_tos(),
1807 &description, &blob);
1809 printf("sys_acl_blob_get_fd failed (%s)\n", strerror(errno));
1810 return map_nt_error_from_unix(errno);
1812 printf("Description: %s\n", description);
1813 for (i = 0; i < blob.length; i++) {
1814 printf("%.2x ", blob.data[i]);
1818 return NT_STATUS_OK;
1823 static NTSTATUS cmd_sys_acl_delete_def_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1824 int argc, const char **argv)
1827 struct smb_filename *smb_fname = NULL;
1830 printf("Usage: sys_acl_delete_def_file <path>\n");
1831 return NT_STATUS_OK;
1834 smb_fname = synthetic_smb_fname(talloc_tos(),
1840 if (smb_fname == NULL) {
1841 return NT_STATUS_NO_MEMORY;
1843 ret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(vfs->conn, smb_fname);
1845 printf("sys_acl_delete_def_file failed (%s)\n", strerror(errno));
1846 TALLOC_FREE(smb_fname);
1847 return NT_STATUS_UNSUCCESSFUL;
1849 TALLOC_FREE(smb_fname);
1850 return NT_STATUS_OK;
1853 /* Afaik translate name was first introduced with vfs_catia, to be able
1854 to translate unix file/dir-names, containing invalid windows characters,
1855 to valid windows names.
1856 The used translation direction is always unix --> windows
1858 static NTSTATUS cmd_translate_name(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1859 int argc, const char **argv)
1862 struct dirent *dent = NULL;
1865 char *translated = NULL;
1866 struct smb_filename *smb_fname = NULL;
1870 DEBUG(0, ("Usage: translate_name unix_filename\n"));
1871 return NT_STATUS_UNSUCCESSFUL;
1874 smb_fname = synthetic_smb_fname(talloc_tos(),
1879 if (smb_fname == NULL) {
1880 return NT_STATUS_NO_MEMORY;
1883 vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, smb_fname, NULL, 0);
1884 if (vfs->currentdir == NULL) {
1885 DEBUG(0, ("cmd_translate_name: opendir error=%d (%s)\n",
1886 errno, strerror(errno)));
1887 TALLOC_FREE(smb_fname);
1888 return NT_STATUS_UNSUCCESSFUL;
1892 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir, &st);
1896 if (strcmp (dent->d_name, argv[1]) == 0) {
1903 DEBUG(0, ("cmd_translate_name: file '%s' not found.\n",
1905 status = NT_STATUS_UNSUCCESSFUL;
1908 status = SMB_VFS_TRANSLATE_NAME(vfs->conn, dent->d_name,
1909 vfs_translate_to_windows,
1910 talloc_tos(), &translated);
1911 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
1912 DEBUG(0, ("cmd_translate_name: file '%s' cannot be "
1913 "translated\n", argv[1]));
1914 TALLOC_FREE(translated);
1917 /* translation success. But that could also mean
1918 that translating "aaa" to "aaa" was successful :-(
1920 DEBUG(0, ("cmd_translate_name: file '%s' --> '%s'\n",
1921 argv[1], translated));
1923 TALLOC_FREE(smb_fname);
1924 TALLOC_FREE(translated);
1927 TALLOC_FREE(smb_fname);
1928 ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
1930 DEBUG(0, ("cmd_translate_name: closedir failure: %s\n",
1932 return NT_STATUS_UNSUCCESSFUL;
1935 vfs->currentdir = NULL;
1940 struct cmd_set vfs_commands[] = {
1942 { .name = "VFS Commands" },
1944 { "load", cmd_load_module, "Load a module", "load <module.so>" },
1945 { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
1946 { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
1947 { "connect", cmd_connect, "VFS connect()", "connect" },
1948 { "disconnect", cmd_disconnect, "VFS disconnect()", "disconnect" },
1949 { "disk_free", cmd_disk_free, "VFS disk_free()", "disk_free <path>" },
1950 { "opendir", cmd_opendir, "VFS opendir()", "opendir <fname>" },
1951 { "readdir", cmd_readdir, "VFS readdir()", "readdir" },
1952 { "mkdir", cmd_mkdir, "VFS mkdir()", "mkdir <path>" },
1953 { "rmdir", cmd_pathfunc, "VFS rmdir()", "rmdir <path>" },
1954 { "closedir", cmd_closedir, "VFS closedir()", "closedir" },
1955 { "open", cmd_open, "VFS open()", "open <fname> <flags> <mode>" },
1956 { "close", cmd_close, "VFS close()", "close <fd>" },
1957 { "read", cmd_read, "VFS read()", "read <fd> <size>" },
1958 { "write", cmd_write, "VFS write()", "write <fd> <size>" },
1959 { "lseek", cmd_lseek, "VFS lseek()", "lseek <fd> <offset> <whence>" },
1960 { "rename", cmd_rename, "VFS rename()", "rename <old> <new>" },
1961 { "fsync", cmd_fsync, "VFS fsync()", "fsync <fd>" },
1962 { "stat", cmd_stat, "VFS stat()", "stat <fname>" },
1963 { "fstat", cmd_fstat, "VFS fstat()", "fstat <fd>" },
1964 { "lstat", cmd_lstat, "VFS lstat()", "lstat <fname>" },
1965 { "unlink", cmd_pathfunc, "VFS unlink()", "unlink <fname>" },
1966 { "chmod", cmd_chmod, "VFS chmod()", "chmod <path> <mode>" },
1967 { "fchmod", cmd_fchmod, "VFS fchmod()", "fchmod <fd> <mode>" },
1968 { "chown", cmd_chown, "VFS chown()", "chown <path> <uid> <gid>" },
1969 { "fchown", cmd_fchown, "VFS fchown()", "fchown <fd> <uid> <gid>" },
1970 { "chdir", cmd_pathfunc, "VFS chdir()", "chdir <path>" },
1971 { "getwd", cmd_getwd, "VFS getwd()", "getwd" },
1972 { "utime", cmd_utime, "VFS utime()", "utime <path> <access> <modify>" },
1973 { "ftruncate", cmd_ftruncate, "VFS ftruncate()", "ftruncate <fd> <length>" },
1974 { "lock", cmd_lock, "VFS lock()", "lock <f> <op> <offset> <count> <type>" },
1975 { "symlink", cmd_symlink, "VFS symlink()", "symlink <old> <new>" },
1976 { "readlink", cmd_readlink, "VFS readlink()", "readlink <path>" },
1977 { "link", cmd_link, "VFS link()", "link <oldpath> <newpath>" },
1978 { "mknod", cmd_mknod, "VFS mknod()", "mknod <path> <mode> <dev>" },
1979 { "realpath", cmd_realpath, "VFS realpath()", "realpath <path>" },
1980 { "getxattr", cmd_getxattr, "VFS getxattr()",
1981 "getxattr <path> <name>" },
1982 { "listxattr", cmd_listxattr, "VFS listxattr()",
1983 "listxattr <path>" },
1984 { "setxattr", cmd_setxattr, "VFS setxattr()",
1985 "setxattr <path> <name> <value> [<flags>]" },
1986 { "removexattr", cmd_removexattr, "VFS removexattr()",
1987 "removexattr <path> <name>\n" },
1988 { "fget_nt_acl", cmd_fget_nt_acl, "VFS fget_nt_acl()",
1989 "fget_nt_acl <fd>\n" },
1990 { "get_nt_acl", cmd_get_nt_acl, "VFS get_nt_acl()",
1991 "get_nt_acl <path>\n" },
1992 { "fset_nt_acl", cmd_fset_nt_acl, "VFS fset_nt_acl()",
1993 "fset_nt_acl <fd>\n" },
1994 { "set_nt_acl", cmd_set_nt_acl, "VFS open() and fset_nt_acl()",
1995 "set_nt_acl <file>\n" },
1996 { "sys_acl_get_file", cmd_sys_acl_get_file, "VFS sys_acl_get_file()", "sys_acl_get_file <path>" },
1997 { "sys_acl_get_fd", cmd_sys_acl_get_fd, "VFS sys_acl_get_fd()", "sys_acl_get_fd <fd>" },
1998 { "sys_acl_blob_get_file", cmd_sys_acl_blob_get_file,
1999 "VFS sys_acl_blob_get_file()", "sys_acl_blob_get_file <path>" },
2000 { "sys_acl_blob_get_fd", cmd_sys_acl_blob_get_fd,
2001 "VFS sys_acl_blob_get_fd()", "sys_acl_blob_get_fd <path>" },
2002 { "sys_acl_delete_def_file", cmd_sys_acl_delete_def_file, "VFS sys_acl_delete_def_file()", "sys_acl_delete_def_file <path>" },
2005 { "test_chain", cmd_test_chain, "test chain code",
2007 { "translate_name", cmd_translate_name, "VFS translate_name()", "translate_name unix_filename" },