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, argv[1]);
468 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
469 return NT_STATUS_UNSUCCESSFUL;
473 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
474 return NT_STATUS_UNSUCCESSFUL;
477 printf("%s: ok\n", argv[0]);
482 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
487 printf("Usage: close <fd>\n");
492 if (vfs->files[fd] == NULL) {
493 printf("close: error=-1 (invalid file descriptor)\n");
497 ret = SMB_VFS_CLOSE(vfs->files[fd]);
499 printf("close: error=%d (%s)\n", errno, strerror(errno));
501 printf("close: ok\n");
503 TALLOC_FREE(vfs->files[fd]);
504 vfs->files[fd] = NULL;
509 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
515 printf("Usage: read <fd> <size>\n");
519 /* do some error checking on these */
521 size = atoi(argv[2]);
522 vfs->data = talloc_array(mem_ctx, char, size);
523 if (vfs->data == NULL) {
524 printf("read: error=-1 (not enough memory)");
525 return NT_STATUS_UNSUCCESSFUL;
527 vfs->data_size = size;
529 rsize = SMB_VFS_READ(vfs->files[fd], vfs->data, size);
531 printf("read: error=%d (%s)\n", errno, strerror(errno));
532 return NT_STATUS_UNSUCCESSFUL;
535 printf("read: ok\n");
540 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
545 printf("Usage: write <fd> <size>\n");
549 /* some error checking should go here */
551 size = atoi(argv[2]);
552 if (vfs->data == NULL) {
553 printf("write: error=-1 (buffer empty, please populate it before writing)");
554 return NT_STATUS_UNSUCCESSFUL;
557 if (vfs->data_size < size) {
558 printf("write: error=-1 (buffer too small, please put some more data in)");
559 return NT_STATUS_UNSUCCESSFUL;
562 wsize = SMB_VFS_WRITE(vfs->files[fd], vfs->data, size);
565 printf("write: error=%d (%s)\n", errno, strerror(errno));
566 return NT_STATUS_UNSUCCESSFUL;
569 printf("write: ok\n");
574 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
576 int fd, offset, whence;
580 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
585 offset = atoi(argv[2]);
586 whence = atoi(argv[3]);
588 case 1: whence = SEEK_SET; break;
589 case 2: whence = SEEK_CUR; break;
590 default: whence = SEEK_END;
593 pos = SMB_VFS_LSEEK(vfs->files[fd], offset, whence);
594 if (pos == (off_t)-1) {
595 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
596 return NT_STATUS_UNSUCCESSFUL;
599 printf("lseek: ok\n");
604 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
607 struct smb_filename *smb_fname_src = NULL;
608 struct smb_filename *smb_fname_dst = NULL;
611 printf("Usage: rename <old> <new>\n");
615 smb_fname_src = synthetic_smb_fname_split(mem_ctx,
617 lp_posix_pathnames());
618 if (smb_fname_src == NULL) {
619 return NT_STATUS_NO_MEMORY;
622 smb_fname_dst = synthetic_smb_fname_split(mem_ctx,
624 lp_posix_pathnames());
625 if (smb_fname_dst == NULL) {
626 TALLOC_FREE(smb_fname_src);
627 return NT_STATUS_NO_MEMORY;
630 ret = SMB_VFS_RENAME(vfs->conn, smb_fname_src, smb_fname_dst);
631 TALLOC_FREE(smb_fname_src);
632 TALLOC_FREE(smb_fname_dst);
634 printf("rename: error=%d (%s)\n", errno, strerror(errno));
635 return NT_STATUS_UNSUCCESSFUL;
638 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(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");
934 static NTSTATUS cmd_chmod_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
936 struct smb_filename *smb_fname = NULL;
939 printf("Usage: chmod_acl <path> <mode>\n");
943 mode = atoi(argv[2]);
945 smb_fname = synthetic_smb_fname(talloc_tos(),
950 if (smb_fname == NULL) {
951 return NT_STATUS_NO_MEMORY;
954 if (SMB_VFS_CHMOD_ACL(vfs->conn, smb_fname, mode) == -1) {
955 printf("chmod_acl: error=%d (%s)\n", errno, strerror(errno));
956 return NT_STATUS_UNSUCCESSFUL;
959 printf("chmod_acl: ok\n");
964 static NTSTATUS cmd_fchmod_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
969 printf("Usage: fchmod_acl <fd> <mode>\n");
974 mode = atoi(argv[2]);
975 if (fd < 0 || fd >= 1024) {
976 printf("fchmod_acl: error=%d (file descriptor out of range)\n", EBADF);
979 if (vfs->files[fd] == NULL) {
980 printf("fchmod_acl: error=%d (invalid file descriptor)\n", EBADF);
984 if (SMB_VFS_FCHMOD_ACL(vfs->files[fd], mode) == -1) {
985 printf("fchmod_acl: error=%d (%s)\n", errno, strerror(errno));
986 return NT_STATUS_UNSUCCESSFUL;
989 printf("fchmod_acl: ok\n");
994 static NTSTATUS cmd_chown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
996 struct smb_filename *smb_fname = NULL;
1000 printf("Usage: chown <path> <uid> <gid>\n");
1001 return NT_STATUS_OK;
1004 uid = atoi(argv[2]);
1005 gid = atoi(argv[3]);
1007 smb_fname = synthetic_smb_fname(talloc_tos(),
1012 if (smb_fname == NULL) {
1013 return NT_STATUS_NO_MEMORY;
1016 if (SMB_VFS_CHOWN(vfs->conn, smb_fname, uid, gid) == -1) {
1017 printf("chown: error=%d (%s)\n", errno, strerror(errno));
1018 return NT_STATUS_UNSUCCESSFUL;
1021 printf("chown: ok\n");
1022 return NT_STATUS_OK;
1026 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1032 printf("Usage: fchown <fd> <uid> <gid>\n");
1033 return NT_STATUS_OK;
1036 uid = atoi(argv[2]);
1037 gid = atoi(argv[3]);
1039 if (fd < 0 || fd >= 1024) {
1040 printf("fchown: faliure=%d (file descriptor out of range)\n", EBADF);
1041 return NT_STATUS_OK;
1043 if (vfs->files[fd] == NULL) {
1044 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
1045 return NT_STATUS_OK;
1047 if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
1048 printf("fchown error=%d (%s)\n", errno, strerror(errno));
1049 return NT_STATUS_UNSUCCESSFUL;
1052 printf("fchown: ok\n");
1053 return NT_STATUS_OK;
1057 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1059 char *buf = SMB_VFS_GETWD(vfs->conn);
1061 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
1062 return NT_STATUS_UNSUCCESSFUL;
1065 printf("getwd: %s\n", buf);
1067 return NT_STATUS_OK;
1070 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1072 struct smb_file_time ft;
1073 struct smb_filename *smb_fname = NULL;
1076 printf("Usage: utime <path> <access> <modify>\n");
1077 return NT_STATUS_OK;
1082 ft.atime = convert_time_t_to_timespec(atoi(argv[2]));
1083 ft.mtime = convert_time_t_to_timespec(atoi(argv[3]));
1085 smb_fname = synthetic_smb_fname_split(mem_ctx,
1087 lp_posix_pathnames());
1088 if (smb_fname == NULL) {
1089 return NT_STATUS_NO_MEMORY;
1092 if (SMB_VFS_NTIMES(vfs->conn, smb_fname, &ft) != 0) {
1093 printf("utime: error=%d (%s)\n", errno, strerror(errno));
1094 TALLOC_FREE(smb_fname);
1095 return NT_STATUS_UNSUCCESSFUL;
1098 TALLOC_FREE(smb_fname);
1099 printf("utime: ok\n");
1100 return NT_STATUS_OK;
1103 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1108 printf("Usage: ftruncate <fd> <length>\n");
1109 return NT_STATUS_OK;
1113 off = atoi(argv[2]);
1114 if (fd < 0 || fd >= 1024) {
1115 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
1116 return NT_STATUS_OK;
1118 if (vfs->files[fd] == NULL) {
1119 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
1120 return NT_STATUS_OK;
1123 if (SMB_VFS_FTRUNCATE(vfs->files[fd], off) == -1) {
1124 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
1125 return NT_STATUS_UNSUCCESSFUL;
1128 printf("ftruncate: ok\n");
1129 return NT_STATUS_OK;
1132 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1139 const char *typestr;
1142 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
1143 printf(" ops: G = F_GETLK\n");
1144 printf(" S = F_SETLK\n");
1145 printf(" W = F_SETLKW\n");
1146 printf(" type: R = F_RDLCK\n");
1147 printf(" W = F_WRLCK\n");
1148 printf(" U = F_UNLCK\n");
1149 return NT_STATUS_OK;
1152 if (sscanf(argv[1], "%d", &fd) == 0) {
1153 printf("lock: error=-1 (error parsing fd)\n");
1154 return NT_STATUS_UNSUCCESSFUL;
1169 printf("lock: error=-1 (invalid op flag!)\n");
1170 return NT_STATUS_UNSUCCESSFUL;
1173 if (sscanf(argv[3], "%ld", &offset) == 0) {
1174 printf("lock: error=-1 (error parsing fd)\n");
1175 return NT_STATUS_UNSUCCESSFUL;
1178 if (sscanf(argv[4], "%ld", &count) == 0) {
1179 printf("lock: error=-1 (error parsing fd)\n");
1180 return NT_STATUS_UNSUCCESSFUL;
1197 printf("lock: error=-1 (invalid type flag!)\n");
1198 return NT_STATUS_UNSUCCESSFUL;
1203 printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
1205 if (SMB_VFS_LOCK(vfs->files[fd], op, offset, count, type) == False) {
1206 printf("lock: error=%d (%s)\n", errno, strerror(errno));
1207 return NT_STATUS_UNSUCCESSFUL;
1210 printf("lock: ok\n");
1211 return NT_STATUS_OK;
1214 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1216 struct smb_filename *new_smb_fname = NULL;
1219 printf("Usage: symlink <path> <link>\n");
1220 return NT_STATUS_OK;
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;
1229 if (SMB_VFS_SYMLINK(vfs->conn, argv[1], new_smb_fname) == -1) {
1230 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
1231 return NT_STATUS_UNSUCCESSFUL;
1234 printf("symlink: ok\n");
1235 return NT_STATUS_OK;
1239 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1241 char buffer[PATH_MAX];
1242 struct smb_filename *smb_fname = NULL;
1246 printf("Usage: readlink <path>\n");
1247 return NT_STATUS_OK;
1250 smb_fname = synthetic_smb_fname_split(mem_ctx,
1252 lp_posix_pathnames());
1253 if (smb_fname == NULL) {
1254 return NT_STATUS_NO_MEMORY;
1256 if ((size = SMB_VFS_READLINK(vfs->conn, smb_fname,
1257 buffer, PATH_MAX)) == -1) {
1258 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
1259 return NT_STATUS_UNSUCCESSFUL;
1262 buffer[size] = '\0';
1263 printf("readlink: %s\n", buffer);
1264 return NT_STATUS_OK;
1268 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1270 struct smb_filename *old_smb_fname = NULL;
1271 struct smb_filename *new_smb_fname = NULL;
1274 printf("Usage: link <path> <link>\n");
1275 return NT_STATUS_OK;
1278 old_smb_fname = synthetic_smb_fname_split(mem_ctx,
1280 lp_posix_pathnames());
1281 if (old_smb_fname == NULL) {
1282 return NT_STATUS_NO_MEMORY;
1284 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1286 lp_posix_pathnames());
1287 if (new_smb_fname == NULL) {
1288 return NT_STATUS_NO_MEMORY;
1291 if (SMB_VFS_LINK(vfs->conn, old_smb_fname, new_smb_fname) == -1) {
1292 printf("link: error=%d (%s)\n", errno, strerror(errno));
1293 return NT_STATUS_UNSUCCESSFUL;
1296 printf("link: ok\n");
1297 return NT_STATUS_OK;
1300 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1303 unsigned int dev_val;
1305 struct smb_filename *smb_fname = NULL;
1308 printf("Usage: mknod <path> <mode> <dev>\n");
1309 printf(" mode is octal\n");
1310 printf(" dev is hex\n");
1311 return NT_STATUS_OK;
1314 if (sscanf(argv[2], "%ho", (unsigned short *)&mode) == 0) {
1315 printf("open: error=-1 (invalid mode!)\n");
1316 return NT_STATUS_UNSUCCESSFUL;
1319 if (sscanf(argv[3], "%x", &dev_val) == 0) {
1320 printf("open: error=-1 (invalid dev!)\n");
1321 return NT_STATUS_UNSUCCESSFUL;
1323 dev = (SMB_DEV_T)dev_val;
1325 smb_fname = synthetic_smb_fname_split(mem_ctx,
1327 lp_posix_pathnames());
1328 if (smb_fname == NULL) {
1329 return NT_STATUS_NO_MEMORY;
1331 if (SMB_VFS_MKNOD(vfs->conn, smb_fname, mode, dev) == -1) {
1332 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1333 return NT_STATUS_UNSUCCESSFUL;
1336 printf("mknod: ok\n");
1337 return NT_STATUS_OK;
1340 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1343 printf("Usage: realpath <path>\n");
1344 return NT_STATUS_OK;
1347 if (SMB_VFS_REALPATH(vfs->conn, argv[1]) == NULL) {
1348 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1349 return NT_STATUS_UNSUCCESSFUL;
1352 printf("realpath: ok\n");
1353 return NT_STATUS_OK;
1356 static NTSTATUS cmd_getxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1357 int argc, const char **argv)
1361 struct smb_filename *smb_fname = NULL;
1364 printf("Usage: getxattr <path> <xattr>\n");
1365 return NT_STATUS_OK;
1370 smb_fname = synthetic_smb_fname_split(mem_ctx,
1372 lp_posix_pathnames());
1373 if (smb_fname == NULL) {
1374 return NT_STATUS_NO_MEMORY;
1376 ret = SMB_VFS_GETXATTR(vfs->conn, smb_fname, argv[2], buf,
1377 talloc_get_size(buf));
1380 printf("getxattr returned (%s)\n", strerror(err));
1381 return map_nt_error_from_unix(err);
1383 buf = talloc_array(mem_ctx, uint8_t, ret);
1385 return NT_STATUS_NO_MEMORY;
1387 ret = SMB_VFS_GETXATTR(vfs->conn, smb_fname, argv[2], buf,
1388 talloc_get_size(buf));
1391 printf("getxattr returned (%s)\n", strerror(err));
1392 return map_nt_error_from_unix(err);
1394 dump_data_file(buf, talloc_get_size(buf), false, stdout);
1395 return NT_STATUS_OK;
1398 static NTSTATUS cmd_listxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1399 int argc, const char **argv)
1403 struct smb_filename *smb_fname = NULL;
1406 printf("Usage: listxattr <path>\n");
1407 return NT_STATUS_OK;
1412 smb_fname = synthetic_smb_fname_split(mem_ctx,
1414 lp_posix_pathnames());
1415 if (smb_fname == NULL) {
1416 return NT_STATUS_NO_MEMORY;
1418 ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1419 buf, talloc_get_size(buf));
1422 printf("listxattr returned (%s)\n", strerror(err));
1423 return map_nt_error_from_unix(err);
1425 buf = talloc_array(mem_ctx, char, ret);
1427 return NT_STATUS_NO_MEMORY;
1429 ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1430 buf, talloc_get_size(buf));
1433 printf("listxattr returned (%s)\n", strerror(err));
1434 return map_nt_error_from_unix(err);
1437 return NT_STATUS_OK;
1439 if (buf[ret-1] != '\0') {
1440 printf("listxattr returned non 0-terminated strings\n");
1441 return NT_STATUS_INTERNAL_ERROR;
1445 while (p < buf+ret) {
1450 return NT_STATUS_OK;
1453 static NTSTATUS cmd_setxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1454 int argc, const char **argv)
1458 struct smb_filename *smb_fname = NULL;
1460 if ((argc < 4) || (argc > 5)) {
1461 printf("Usage: setxattr <path> <xattr> <value> [flags]\n");
1462 return NT_STATUS_OK;
1466 flags = atoi(argv[4]);
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 ret = SMB_VFS_SETXATTR(vfs->conn, smb_fname, argv[2],
1476 argv[3], strlen(argv[3]), flags);
1479 printf("setxattr returned (%s)\n", strerror(err));
1480 return map_nt_error_from_unix(err);
1482 return NT_STATUS_OK;
1485 static NTSTATUS cmd_removexattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1486 int argc, const char **argv)
1489 struct smb_filename *smb_fname = NULL;
1492 printf("Usage: removexattr <path> <xattr>\n");
1493 return NT_STATUS_OK;
1496 smb_fname = synthetic_smb_fname(talloc_tos(),
1502 if (smb_fname == NULL) {
1503 return NT_STATUS_NO_MEMORY;
1506 ret = SMB_VFS_REMOVEXATTR(vfs->conn, smb_fname, argv[2]);
1509 printf("removexattr returned (%s)\n", strerror(err));
1510 return map_nt_error_from_unix(err);
1512 return NT_STATUS_OK;
1515 static NTSTATUS cmd_fget_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1516 int argc, const char **argv)
1520 struct security_descriptor *sd;
1523 printf("Usage: fget_nt_acl <fd>\n");
1524 return NT_STATUS_OK;
1528 if (fd < 0 || fd >= 1024) {
1529 printf("fget_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1530 return NT_STATUS_OK;
1532 if (vfs->files[fd] == NULL) {
1533 printf("fget_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1534 return NT_STATUS_OK;
1537 status = SMB_VFS_FGET_NT_ACL(vfs->files[fd],
1538 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1540 if (!NT_STATUS_IS_OK(status)) {
1541 printf("fget_nt_acl returned (%s)\n", nt_errstr(status));
1544 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1546 return NT_STATUS_OK;
1549 static NTSTATUS cmd_get_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1550 int argc, const char **argv)
1553 struct security_descriptor *sd;
1554 struct smb_filename *smb_fname = NULL;
1557 printf("Usage: get_nt_acl <path>\n");
1558 return NT_STATUS_OK;
1561 smb_fname = synthetic_smb_fname(talloc_tos(),
1567 if (smb_fname == NULL) {
1568 return NT_STATUS_NO_MEMORY;
1571 status = SMB_VFS_GET_NT_ACL(vfs->conn, smb_fname,
1572 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1574 if (!NT_STATUS_IS_OK(status)) {
1575 printf("get_nt_acl returned (%s)\n", nt_errstr(status));
1578 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1580 return NT_STATUS_OK;
1583 static NTSTATUS cmd_fset_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1584 int argc, const char **argv)
1588 struct security_descriptor *sd;
1591 printf("Usage: fset_nt_acl <fd> <sddl>\n");
1592 return NT_STATUS_OK;
1596 if (fd < 0 || fd >= 1024) {
1597 printf("fset_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1598 return NT_STATUS_OK;
1600 if (vfs->files[fd] == NULL) {
1601 printf("fset_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1602 return NT_STATUS_OK;
1605 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1607 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1608 return NT_STATUS_INVALID_PARAMETER;
1611 status = SMB_VFS_FSET_NT_ACL(vfs->files[fd], SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1612 if (!NT_STATUS_IS_OK(status)) {
1613 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1617 return NT_STATUS_OK;
1620 static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1626 struct smb_filename *smb_fname = NULL;
1628 struct security_descriptor *sd = NULL;
1631 printf("Usage: set_nt_acl <file> <sddl>\n");
1632 return NT_STATUS_OK;
1637 fsp = talloc_zero(vfs, struct files_struct);
1639 return NT_STATUS_NO_MEMORY;
1641 fsp->fh = talloc_zero(fsp, struct fd_handle);
1642 if (fsp->fh == NULL) {
1644 return NT_STATUS_NO_MEMORY;
1646 fsp->conn = vfs->conn;
1648 smb_fname = synthetic_smb_fname_split(NULL,
1650 lp_posix_pathnames());
1651 if (smb_fname == NULL) {
1653 return NT_STATUS_NO_MEMORY;
1656 fsp->fsp_name = smb_fname;
1659 flags = O_RDONLY|O_DIRECTORY;
1661 /* POSIX allows us to open a directory with O_RDONLY. */
1665 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, O_RDWR, mode);
1666 if (fsp->fh->fd == -1 && errno == EISDIR) {
1667 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, flags, mode);
1669 if (fsp->fh->fd == -1) {
1670 printf("open: error=%d (%s)\n", errno, strerror(errno));
1672 TALLOC_FREE(smb_fname);
1673 return NT_STATUS_UNSUCCESSFUL;
1676 status = NT_STATUS_OK;
1677 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
1679 /* If we have an fd, this stat should succeed. */
1680 DEBUG(0,("Error doing fstat on open file %s "
1682 smb_fname_str_dbg(smb_fname),
1684 status = map_nt_error_from_unix(errno);
1687 if (!NT_STATUS_IS_OK(status)) {
1691 fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
1692 fsp->vuid = UID_FIELD_INVALID;
1694 fsp->can_lock = True;
1695 fsp->can_read = True;
1696 fsp->can_write = True;
1697 fsp->print_file = NULL;
1698 fsp->modified = False;
1699 fsp->sent_oplock_break = NO_BREAK_SENT;
1700 fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
1703 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1705 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1706 status = NT_STATUS_INVALID_PARAMETER;
1710 status = SMB_VFS_FSET_NT_ACL(fsp, SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1711 if (!NT_STATUS_IS_OK(status)) {
1712 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1718 ret = SMB_VFS_CLOSE(fsp);
1720 printf("close: error=%d (%s)\n", errno, strerror(errno));
1729 static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1730 int argc, const char **argv)
1737 printf("Usage: sys_acl_get_fd <fd>\n");
1738 return NT_STATUS_OK;
1742 if (fd < 0 || fd >= 1024) {
1743 printf("sys_acl_get_fd: error=%d (file descriptor out of range)\n", EBADF);
1744 return NT_STATUS_OK;
1746 if (vfs->files[fd] == NULL) {
1747 printf("sys_acl_get_fd: error=%d (invalid file descriptor)\n", EBADF);
1748 return NT_STATUS_OK;
1751 acl = SMB_VFS_SYS_ACL_GET_FD(vfs->files[fd], talloc_tos());
1753 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1754 return NT_STATUS_UNSUCCESSFUL;
1756 acl_text = sys_acl_to_text(acl, NULL);
1757 printf("%s", acl_text);
1759 SAFE_FREE(acl_text);
1760 return NT_STATUS_OK;
1763 static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1764 int argc, const char **argv)
1769 struct smb_filename *smb_fname = NULL;
1772 printf("Usage: sys_acl_get_file <path> <type>\n");
1773 return NT_STATUS_OK;
1776 smb_fname = synthetic_smb_fname_split(talloc_tos(),
1778 lp_posix_pathnames());
1779 if (smb_fname == NULL) {
1780 return NT_STATUS_NO_MEMORY;
1782 type = atoi(argv[2]);
1783 acl = SMB_VFS_SYS_ACL_GET_FILE(vfs->conn, smb_fname,
1784 type, talloc_tos());
1786 printf("sys_acl_get_file failed (%s)\n", strerror(errno));
1787 return NT_STATUS_UNSUCCESSFUL;
1789 acl_text = sys_acl_to_text(acl, NULL);
1790 printf("%s", acl_text);
1792 SAFE_FREE(acl_text);
1793 return NT_STATUS_OK;
1796 static NTSTATUS cmd_sys_acl_blob_get_file(struct vfs_state *vfs,
1797 TALLOC_CTX *mem_ctx,
1798 int argc, const char **argv)
1804 struct smb_filename *smb_fname = NULL;
1807 printf("Usage: sys_acl_get_file <path>\n");
1808 return NT_STATUS_OK;
1811 smb_fname = synthetic_smb_fname_split(talloc_tos(),
1813 lp_posix_pathnames());
1814 if (smb_fname == NULL) {
1815 return NT_STATUS_NO_MEMORY;
1817 ret = SMB_VFS_SYS_ACL_BLOB_GET_FILE(vfs->conn, smb_fname, talloc_tos(),
1818 &description, &blob);
1820 printf("sys_acl_blob_get_file failed (%s)\n", strerror(errno));
1821 return map_nt_error_from_unix(errno);
1823 printf("Description: %s\n", description);
1824 for (i = 0; i < blob.length; i++) {
1825 printf("%.2x ", blob.data[i]);
1829 return NT_STATUS_OK;
1832 static NTSTATUS cmd_sys_acl_blob_get_fd(struct vfs_state *vfs,
1833 TALLOC_CTX *mem_ctx,
1834 int argc, const char **argv)
1843 printf("Usage: sys_acl_blob_get_fd <fd>\n");
1844 return NT_STATUS_OK;
1848 if (fd < 0 || fd >= 1024) {
1849 printf("sys_acl_blob_get_fd: error=%d "
1850 "(file descriptor out of range)\n", EBADF);
1851 return NT_STATUS_OK;
1853 if (vfs->files[fd] == NULL) {
1854 printf("sys_acl_blob_get_fd: error=%d "
1855 "(invalid file descriptor)\n", EBADF);
1856 return NT_STATUS_OK;
1859 ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(vfs->files[fd], talloc_tos(),
1860 &description, &blob);
1862 printf("sys_acl_blob_get_fd failed (%s)\n", strerror(errno));
1863 return map_nt_error_from_unix(errno);
1865 printf("Description: %s\n", description);
1866 for (i = 0; i < blob.length; i++) {
1867 printf("%.2x ", blob.data[i]);
1871 return NT_STATUS_OK;
1876 static NTSTATUS cmd_sys_acl_delete_def_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1877 int argc, const char **argv)
1880 struct smb_filename *smb_fname = NULL;
1883 printf("Usage: sys_acl_delete_def_file <path>\n");
1884 return NT_STATUS_OK;
1887 smb_fname = synthetic_smb_fname(talloc_tos(),
1893 if (smb_fname == NULL) {
1894 return NT_STATUS_NO_MEMORY;
1896 ret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(vfs->conn, smb_fname);
1898 printf("sys_acl_delete_def_file failed (%s)\n", strerror(errno));
1899 TALLOC_FREE(smb_fname);
1900 return NT_STATUS_UNSUCCESSFUL;
1902 TALLOC_FREE(smb_fname);
1903 return NT_STATUS_OK;
1906 /* Afaik translate name was first introduced with vfs_catia, to be able
1907 to translate unix file/dir-names, containing invalid windows characters,
1908 to valid windows names.
1909 The used translation direction is always unix --> windows
1911 static NTSTATUS cmd_translate_name(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1912 int argc, const char **argv)
1915 struct dirent *dent = NULL;
1918 char *translated = NULL;
1919 struct smb_filename *smb_fname = NULL;
1923 DEBUG(0, ("Usage: translate_name unix_filename\n"));
1924 return NT_STATUS_UNSUCCESSFUL;
1927 smb_fname = synthetic_smb_fname(talloc_tos(),
1932 if (smb_fname == NULL) {
1933 return NT_STATUS_NO_MEMORY;
1936 vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, smb_fname, NULL, 0);
1937 if (vfs->currentdir == NULL) {
1938 DEBUG(0, ("cmd_translate_name: opendir error=%d (%s)\n",
1939 errno, strerror(errno)));
1940 TALLOC_FREE(smb_fname);
1941 return NT_STATUS_UNSUCCESSFUL;
1945 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir, &st);
1949 if (strcmp (dent->d_name, argv[1]) == 0) {
1956 DEBUG(0, ("cmd_translate_name: file '%s' not found.\n",
1958 status = NT_STATUS_UNSUCCESSFUL;
1961 status = SMB_VFS_TRANSLATE_NAME(vfs->conn, dent->d_name,
1962 vfs_translate_to_windows,
1963 talloc_tos(), &translated);
1964 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
1965 DEBUG(0, ("cmd_translate_name: file '%s' cannot be "
1966 "translated\n", argv[1]));
1967 TALLOC_FREE(translated);
1970 /* translation success. But that could also mean
1971 that translating "aaa" to "aaa" was successful :-(
1973 DEBUG(0, ("cmd_translate_name: file '%s' --> '%s'\n",
1974 argv[1], translated));
1976 TALLOC_FREE(smb_fname);
1977 TALLOC_FREE(translated);
1980 TALLOC_FREE(smb_fname);
1981 ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
1983 DEBUG(0, ("cmd_translate_name: closedir failure: %s\n",
1985 return NT_STATUS_UNSUCCESSFUL;
1988 vfs->currentdir = NULL;
1993 struct cmd_set vfs_commands[] = {
1997 { "load", cmd_load_module, "Load a module", "load <module.so>" },
1998 { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
1999 { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
2000 { "connect", cmd_connect, "VFS connect()", "connect" },
2001 { "disconnect", cmd_disconnect, "VFS disconnect()", "disconnect" },
2002 { "disk_free", cmd_disk_free, "VFS disk_free()", "disk_free <path>" },
2003 { "opendir", cmd_opendir, "VFS opendir()", "opendir <fname>" },
2004 { "readdir", cmd_readdir, "VFS readdir()", "readdir" },
2005 { "mkdir", cmd_mkdir, "VFS mkdir()", "mkdir <path>" },
2006 { "rmdir", cmd_pathfunc, "VFS rmdir()", "rmdir <path>" },
2007 { "closedir", cmd_closedir, "VFS closedir()", "closedir" },
2008 { "open", cmd_open, "VFS open()", "open <fname> <flags> <mode>" },
2009 { "close", cmd_close, "VFS close()", "close <fd>" },
2010 { "read", cmd_read, "VFS read()", "read <fd> <size>" },
2011 { "write", cmd_write, "VFS write()", "write <fd> <size>" },
2012 { "lseek", cmd_lseek, "VFS lseek()", "lseek <fd> <offset> <whence>" },
2013 { "rename", cmd_rename, "VFS rename()", "rename <old> <new>" },
2014 { "fsync", cmd_fsync, "VFS fsync()", "fsync <fd>" },
2015 { "stat", cmd_stat, "VFS stat()", "stat <fname>" },
2016 { "fstat", cmd_fstat, "VFS fstat()", "fstat <fd>" },
2017 { "lstat", cmd_lstat, "VFS lstat()", "lstat <fname>" },
2018 { "unlink", cmd_pathfunc, "VFS unlink()", "unlink <fname>" },
2019 { "chmod", cmd_chmod, "VFS chmod()", "chmod <path> <mode>" },
2020 { "fchmod", cmd_fchmod, "VFS fchmod()", "fchmod <fd> <mode>" },
2021 { "chown", cmd_chown, "VFS chown()", "chown <path> <uid> <gid>" },
2022 { "fchown", cmd_fchown, "VFS fchown()", "fchown <fd> <uid> <gid>" },
2023 { "chdir", cmd_pathfunc, "VFS chdir()", "chdir <path>" },
2024 { "getwd", cmd_getwd, "VFS getwd()", "getwd" },
2025 { "utime", cmd_utime, "VFS utime()", "utime <path> <access> <modify>" },
2026 { "ftruncate", cmd_ftruncate, "VFS ftruncate()", "ftruncate <fd> <length>" },
2027 { "lock", cmd_lock, "VFS lock()", "lock <f> <op> <offset> <count> <type>" },
2028 { "symlink", cmd_symlink, "VFS symlink()", "symlink <old> <new>" },
2029 { "readlink", cmd_readlink, "VFS readlink()", "readlink <path>" },
2030 { "link", cmd_link, "VFS link()", "link <oldpath> <newpath>" },
2031 { "mknod", cmd_mknod, "VFS mknod()", "mknod <path> <mode> <dev>" },
2032 { "realpath", cmd_realpath, "VFS realpath()", "realpath <path>" },
2033 { "getxattr", cmd_getxattr, "VFS getxattr()",
2034 "getxattr <path> <name>" },
2035 { "listxattr", cmd_listxattr, "VFS listxattr()",
2036 "listxattr <path>" },
2037 { "setxattr", cmd_setxattr, "VFS setxattr()",
2038 "setxattr <path> <name> <value> [<flags>]" },
2039 { "removexattr", cmd_removexattr, "VFS removexattr()",
2040 "removexattr <path> <name>\n" },
2041 { "fget_nt_acl", cmd_fget_nt_acl, "VFS fget_nt_acl()",
2042 "fget_nt_acl <fd>\n" },
2043 { "get_nt_acl", cmd_get_nt_acl, "VFS get_nt_acl()",
2044 "get_nt_acl <path>\n" },
2045 { "fset_nt_acl", cmd_fset_nt_acl, "VFS fset_nt_acl()",
2046 "fset_nt_acl <fd>\n" },
2047 { "set_nt_acl", cmd_set_nt_acl, "VFS open() and fset_nt_acl()",
2048 "set_nt_acl <file>\n" },
2049 { "fchmod_acl", cmd_fchmod_acl, "VFS fchmod_acl()", "fchmod_acl <fd> <mode>" },
2050 { "chmod_acl", cmd_chmod_acl, "VFS chmod_acl()", "chmod_acl <path> <mode>" },
2051 { "sys_acl_get_file", cmd_sys_acl_get_file, "VFS sys_acl_get_file()", "sys_acl_get_file <path>" },
2052 { "sys_acl_get_fd", cmd_sys_acl_get_fd, "VFS sys_acl_get_fd()", "sys_acl_get_fd <fd>" },
2053 { "sys_acl_blob_get_file", cmd_sys_acl_blob_get_file,
2054 "VFS sys_acl_blob_get_file()", "sys_acl_blob_get_file <path>" },
2055 { "sys_acl_blob_get_fd", cmd_sys_acl_blob_get_fd,
2056 "VFS sys_acl_blob_get_fd()", "sys_acl_blob_get_fd <path>" },
2057 { "sys_acl_delete_def_file", cmd_sys_acl_delete_def_file, "VFS sys_acl_delete_def_file()", "sys_acl_delete_def_file <path>" },
2060 { "test_chain", cmd_test_chain, "test chain code",
2062 { "translate_name", cmd_translate_name, "VFS translate_name()", "translate_name unix_filename" },