ndr: avoid object ACE pull overhead for non-object ACE
[Samba.git] / source3 / torture / cmd_vfs.c
blob95b1e21a22f5d63d12fa32d7ec4e747287cfd548
1 /*
2 Unix SMB/CIFS implementation.
3 VFS module functions
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/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "system/passwd.h"
25 #include "system/filesys.h"
26 #include "vfstest.h"
27 #include "../lib/util/util_pw.h"
28 #include "libcli/security/security.h"
29 #include "passdb/machine_sid.h"
30 #include "source3/smbd/dir.h"
32 static const char *null_string = "";
34 static uint32_t ssf_flags(void)
36 return lp_posix_pathnames() ? SMB_FILENAME_POSIX_PATH : 0;
39 static NTSTATUS cmd_load_module(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
41 int i;
43 if (argc < 2) {
44 printf("Usage: load <modules>\n");
45 return NT_STATUS_OK;
48 for (i=argc-1;i>0;i--) {
49 if (!vfs_init_custom(vfs->conn, argv[i])) {
50 DEBUG(0, ("load: (vfs_init_custom failed for %s)\n", argv[i]));
51 return NT_STATUS_UNSUCCESSFUL;
54 printf("load: ok\n");
55 return NT_STATUS_OK;
58 static NTSTATUS cmd_populate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
60 char c;
61 size_t size;
62 if (argc != 3) {
63 printf("Usage: populate <char> <size>\n");
64 return NT_STATUS_OK;
66 c = argv[1][0];
67 size = atoi(argv[2]);
68 vfs->data = talloc_array(mem_ctx, char, size);
69 if (vfs->data == NULL) {
70 printf("populate: error=-1 (not enough memory)");
71 return NT_STATUS_UNSUCCESSFUL;
73 memset(vfs->data, c, size);
74 vfs->data_size = size;
75 return NT_STATUS_OK;
78 static NTSTATUS cmd_show_data(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
80 size_t offset;
81 size_t len;
82 if (argc != 1 && argc != 3) {
83 printf("Usage: showdata [<offset> <len>]\n");
84 return NT_STATUS_OK;
86 if (vfs->data == NULL || vfs->data_size == 0) {
87 printf("show_data: error=-1 (buffer empty)\n");
88 return NT_STATUS_UNSUCCESSFUL;
91 if (argc == 3) {
92 offset = atoi(argv[1]);
93 len = atoi(argv[2]);
94 } else {
95 offset = 0;
96 len = vfs->data_size;
98 if ((offset + len) > vfs->data_size) {
99 printf("show_data: error=-1 (not enough data in buffer)\n");
100 return NT_STATUS_UNSUCCESSFUL;
102 dump_data(0, (uint8_t *)(vfs->data) + offset, len);
103 return NT_STATUS_OK;
106 static NTSTATUS cmd_connect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
108 const struct loadparm_substitution *lp_sub =
109 loadparm_s3_global_substitution();
111 SMB_VFS_CONNECT(vfs->conn, lp_servicename(talloc_tos(), lp_sub, SNUM(vfs->conn)), "vfstest");
112 return NT_STATUS_OK;
115 static NTSTATUS cmd_disconnect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
117 SMB_VFS_DISCONNECT(vfs->conn);
118 return NT_STATUS_OK;
121 static NTSTATUS cmd_disk_free(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
123 struct smb_filename *smb_fname = NULL;
124 uint64_t diskfree, bsize, dfree, dsize;
125 if (argc != 2) {
126 printf("Usage: disk_free <path>\n");
127 return NT_STATUS_OK;
130 smb_fname = synthetic_smb_fname(talloc_tos(),
131 argv[1],
132 NULL,
133 NULL,
135 ssf_flags());
136 if (smb_fname == NULL) {
137 return NT_STATUS_NO_MEMORY;
139 diskfree = SMB_VFS_DISK_FREE(vfs->conn, smb_fname,
140 &bsize, &dfree, &dsize);
141 printf("disk_free: %lu, bsize = %lu, dfree = %lu, dsize = %lu\n",
142 (unsigned long)diskfree,
143 (unsigned long)bsize,
144 (unsigned long)dfree,
145 (unsigned long)dsize);
146 return NT_STATUS_OK;
150 static NTSTATUS cmd_opendir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
152 struct smb_filename *smb_fname = NULL;
153 NTSTATUS status;
155 if (argc != 2) {
156 printf("Usage: opendir <fname>\n");
157 return NT_STATUS_OK;
160 smb_fname = synthetic_smb_fname(talloc_tos(),
161 argv[1],
162 NULL,
163 NULL,
165 ssf_flags());
166 if (smb_fname == NULL) {
167 return NT_STATUS_NO_MEMORY;
170 status = OpenDir(vfs->conn,
171 vfs->conn,
172 smb_fname,
173 NULL,
175 &vfs->currentdir);
176 if (!NT_STATUS_IS_OK(status)) {
177 int err = map_errno_from_nt_status(status);
178 printf("opendir error=%d (%s)\n", err, strerror(err));
179 TALLOC_FREE(smb_fname);
180 errno = err;
181 return NT_STATUS_UNSUCCESSFUL;
184 TALLOC_FREE(smb_fname);
185 printf("opendir: ok\n");
186 return NT_STATUS_OK;
190 static NTSTATUS cmd_readdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
192 struct smb_Dir *currentdir = vfs->currentdir;
193 files_struct *dirfsp = dir_hnd_fetch_fsp(currentdir);
194 connection_struct *conn = dirfsp->conn;
195 SMB_STRUCT_STAT st;
196 const char *dname = NULL;
197 struct smb_filename *fname = NULL;
198 char *talloced = NULL;
199 int ret;
201 if (vfs->currentdir == NULL) {
202 printf("readdir: error=-1 (no open directory)\n");
203 return NT_STATUS_UNSUCCESSFUL;
206 dname = ReadDirName(vfs->currentdir, &talloced);
207 if (dname == NULL) {
208 printf("readdir: NULL\n");
209 return NT_STATUS_OK;
212 fname = synthetic_smb_fname(
213 talloc_tos(), dname, NULL, 0, 0, ssf_flags());
214 if (fname == NULL) {
215 printf("readdir: no memory\n");
216 return NT_STATUS_OK;
219 printf("readdir: %s\n", dname);
221 ret = SMB_VFS_FSTATAT(conn, dirfsp, fname, &st, AT_SYMLINK_NOFOLLOW);
223 if ((ret == 0) && VALID_STAT(st)) {
224 time_t tmp_time;
225 printf(" stat available");
226 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
227 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
228 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
229 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
230 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
231 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
232 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
233 printf(" Size: %10u", (unsigned int)st.st_ex_size);
234 #ifdef HAVE_STAT_ST_BLOCKS
235 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
236 #endif
237 #ifdef HAVE_STAT_ST_BLKSIZE
238 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
239 #endif
240 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
241 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
242 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
243 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
244 printf(" Uid: %5lu Gid: %5lu\n",
245 (unsigned long)st.st_ex_uid,
246 (unsigned long)st.st_ex_gid);
247 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
248 printf(" Access: %s", ctime(&tmp_time));
249 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
250 printf(" Modify: %s", ctime(&tmp_time));
251 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
252 printf(" Change: %s", ctime(&tmp_time));
255 TALLOC_FREE(talloced);
256 return NT_STATUS_OK;
260 static NTSTATUS cmd_mkdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
262 struct smb_filename *smb_fname = NULL;
263 int ret;
265 if (argc != 2) {
266 printf("Usage: mkdir <path>\n");
267 return NT_STATUS_OK;
270 smb_fname = synthetic_smb_fname(talloc_tos(),
271 argv[1],
272 NULL,
273 NULL,
275 ssf_flags());
277 if (smb_fname == NULL) {
278 return NT_STATUS_NO_MEMORY;
281 ret = SMB_VFS_MKDIRAT(vfs->conn,
282 vfs->conn->cwd_fsp,
283 smb_fname,
284 00755);
285 if (ret == -1) {
286 printf("mkdir error=%d (%s)\n", errno, strerror(errno));
287 return NT_STATUS_UNSUCCESSFUL;
290 printf("mkdir: ok\n");
291 return NT_STATUS_OK;
295 static NTSTATUS cmd_closedir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
297 if (vfs->currentdir == NULL) {
298 printf("closedir: failure (no directory open)\n");
299 return NT_STATUS_UNSUCCESSFUL;
302 TALLOC_FREE(vfs->currentdir);
304 printf("closedir: ok\n");
305 return NT_STATUS_OK;
309 static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
311 struct vfs_open_how how = { .mode = 0400, };
312 const char *flagstr;
313 files_struct *fsp;
314 struct files_struct *fspcwd = NULL;
315 struct smb_filename *smb_fname = NULL;
316 NTSTATUS status;
317 int fd;
319 if (argc < 3 || argc > 5) {
320 printf("Usage: open <filename> <flags> <mode>\n");
321 printf(" flags: O = O_RDONLY\n");
322 printf(" R = O_RDWR\n");
323 printf(" W = O_WRONLY\n");
324 printf(" C = O_CREAT\n");
325 printf(" E = O_EXCL\n");
326 printf(" T = O_TRUNC\n");
327 printf(" A = O_APPEND\n");
328 printf(" N = O_NONBLOCK/O_NDELAY\n");
329 #ifdef O_SYNC
330 printf(" S = O_SYNC\n");
331 #endif
332 #ifdef O_NOFOLLOW
333 printf(" F = O_NOFOLLOW\n");
334 #endif
335 printf(" mode: see open.2\n");
336 printf(" mode is ignored if C flag not present\n");
337 printf(" mode defaults to 00400\n");
338 return NT_STATUS_OK;
340 flagstr = argv[2];
341 while (*flagstr) {
342 switch (*flagstr) {
343 case 'O':
344 how.flags |= O_RDONLY;
345 break;
346 case 'R':
347 how.flags |= O_RDWR;
348 break;
349 case 'W':
350 how.flags |= O_WRONLY;
351 break;
352 case 'C':
353 how.flags |= O_CREAT;
354 break;
355 case 'E':
356 how.flags |= O_EXCL;
357 break;
358 case 'T':
359 how.flags |= O_TRUNC;
360 break;
361 case 'A':
362 how.flags |= O_APPEND;
363 break;
364 case 'N':
365 how.flags |= O_NONBLOCK;
366 break;
367 #ifdef O_SYNC
368 case 'S':
369 how.flags |= O_SYNC;
370 break;
371 #endif
372 #ifdef O_NOFOLLOW
373 case 'F':
374 how.flags |= O_NOFOLLOW;
375 break;
376 #endif
377 default:
378 printf("open: error=-1 (invalid flag!)\n");
379 return NT_STATUS_UNSUCCESSFUL;
381 flagstr++;
383 if ((how.flags & O_CREAT) && argc == 4) {
384 short _mode = 0;
386 if (sscanf(argv[3], "%ho", &_mode) == 0) {
387 printf("open: error=-1 (invalid mode!)\n");
388 return NT_STATUS_UNSUCCESSFUL;
391 how.mode = _mode;
394 fsp = talloc_zero(vfs, struct files_struct);
395 if (fsp == NULL) {
396 goto nomem;
398 fsp->fh = fd_handle_create(fsp);
399 if (fsp->fh == NULL) {
400 goto nomem;
402 fsp->conn = vfs->conn;
404 smb_fname = synthetic_smb_fname_split(NULL,
405 argv[1],
406 lp_posix_pathnames());
407 if (smb_fname == NULL) {
408 goto nomem;
411 fsp->fsp_name = smb_fname;
413 status = vfs_at_fspcwd(fsp, vfs->conn, &fspcwd);
414 if (!NT_STATUS_IS_OK(status)) {
415 goto fail;
418 if (is_named_stream(smb_fname)) {
419 struct smb_filename *base_name = NULL;
421 base_name = cp_smb_filename_nostream(NULL, smb_fname);
422 if (base_name == NULL) {
423 goto nomem;
426 status = openat_pathref_fsp(fspcwd, base_name);
427 if (!NT_STATUS_IS_OK(status)) {
428 goto fail;
431 TALLOC_FREE(fspcwd);
433 fsp->base_fsp = base_name->fsp;
436 fd = SMB_VFS_OPENAT(vfs->conn,
437 fspcwd,
438 smb_fname,
439 fsp,
440 &how);
441 if (fd == -1) {
442 printf("open: error=%d (%s)\n", errno, strerror(errno));
443 status = map_nt_error_from_unix(errno);
444 goto fail;
446 fsp_set_fd(fsp, fd);
448 status = vfs_stat_fsp(fsp);
449 if (!NT_STATUS_IS_OK(status)) {
450 /* If we have an fd, this stat should succeed. */
451 DEBUG(0,("Error doing fstat on open file %s "
452 "(%s)\n",
453 smb_fname_str_dbg(smb_fname),
454 nt_errstr(status) ));
455 } else if (S_ISDIR(smb_fname->st.st_ex_mode)) {
456 errno = EISDIR;
457 status = NT_STATUS_FILE_IS_A_DIRECTORY;
460 if (!NT_STATUS_IS_OK(status)) {
461 fd_close(fsp);
462 goto fail;
465 fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
466 fsp->vuid = UID_FIELD_INVALID;
467 fsp->file_pid = 0;
468 fsp->fsp_flags.can_lock = true;
469 fsp->fsp_flags.can_read = true;
470 fsp->fsp_flags.can_write = CAN_WRITE(vfs->conn);
471 fsp->print_file = NULL;
472 fsp->fsp_flags.modified = false;
473 fsp->sent_oplock_break = NO_BREAK_SENT;
474 fsp->fsp_flags.is_directory = false;
476 vfs->files[fsp_get_pathref_fd(fsp)] = fsp;
477 printf("open: fd=%d\n", fsp_get_pathref_fd(fsp));
478 return NT_STATUS_OK;
480 nomem:
481 status = NT_STATUS_NO_MEMORY;
482 fail:
483 TALLOC_FREE(smb_fname);
484 TALLOC_FREE(fsp);
485 return status;
489 static NTSTATUS cmd_pathfunc(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
491 struct smb_filename *smb_fname = NULL;
492 int ret = -1;
494 if (argc != 2) {
495 printf("Usage: %s <path>\n", argv[0]);
496 return NT_STATUS_OK;
499 smb_fname = synthetic_smb_fname(talloc_tos(),
500 argv[1],
501 NULL,
502 NULL,
504 ssf_flags());
506 if (smb_fname == NULL) {
507 return NT_STATUS_NO_MEMORY;
510 if (strcmp("rmdir", argv[0]) == 0 ) {
511 ret = SMB_VFS_UNLINKAT(vfs->conn,
512 vfs->conn->cwd_fsp,
513 smb_fname,
514 AT_REMOVEDIR);
515 TALLOC_FREE(smb_fname);
516 } else if (strcmp("unlink", argv[0]) == 0 ) {
517 TALLOC_FREE(smb_fname);
518 /* unlink can be a stream:name */
519 smb_fname = synthetic_smb_fname_split(talloc_tos(),
520 argv[1],
521 lp_posix_pathnames());
522 if (smb_fname == NULL) {
523 return NT_STATUS_NO_MEMORY;
525 ret = SMB_VFS_UNLINKAT(vfs->conn,
526 vfs->conn->cwd_fsp,
527 smb_fname,
529 TALLOC_FREE(smb_fname);
530 } else if (strcmp("chdir", argv[0]) == 0 ) {
531 ret = SMB_VFS_CHDIR(vfs->conn, smb_fname);
532 TALLOC_FREE(smb_fname);
533 } else {
534 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
535 return NT_STATUS_UNSUCCESSFUL;
538 if (ret == -1) {
539 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
540 return NT_STATUS_UNSUCCESSFUL;
543 printf("%s: ok\n", argv[0]);
544 return NT_STATUS_OK;
548 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
550 int fd;
551 NTSTATUS status;
553 if (argc != 2) {
554 printf("Usage: close <fd>\n");
555 return NT_STATUS_OK;
558 fd = atoi(argv[1]);
559 if (vfs->files[fd] == NULL) {
560 printf("close: error=-1 (invalid file descriptor)\n");
561 return NT_STATUS_OK;
564 status = fd_close(vfs->files[fd]);
565 if (!NT_STATUS_IS_OK(status))
566 printf("close: error=%s\n", nt_errstr(status));
567 else
568 printf("close: ok\n");
570 TALLOC_FREE(vfs->files[fd]);
571 vfs->files[fd] = NULL;
572 return status;
576 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
578 int fd;
579 size_t size;
580 ssize_t rsize;
582 if (argc != 3) {
583 printf("Usage: read <fd> <size>\n");
584 return NT_STATUS_OK;
587 /* do some error checking on these */
588 fd = atoi(argv[1]);
589 size = atoi(argv[2]);
590 vfs->data = talloc_array(mem_ctx, char, size);
591 if (vfs->data == NULL) {
592 printf("read: error=-1 (not enough memory)");
593 return NT_STATUS_UNSUCCESSFUL;
595 vfs->data_size = size;
597 rsize = read_file(vfs->files[fd], vfs->data, 0, size);
598 if (rsize == -1) {
599 printf("read: error=%d (%s)\n", errno, strerror(errno));
600 return NT_STATUS_UNSUCCESSFUL;
603 printf("read: ok\n");
604 return NT_STATUS_OK;
608 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
610 int fd, wsize;
611 size_t size;
613 if (argc != 3) {
614 printf("Usage: write <fd> <size>\n");
615 return NT_STATUS_OK;
618 /* some error checking should go here */
619 fd = atoi(argv[1]);
620 size = atoi(argv[2]);
621 if (vfs->data == NULL) {
622 printf("write: error=-1 (buffer empty, please populate it before writing)");
623 return NT_STATUS_UNSUCCESSFUL;
626 if (vfs->data_size < size) {
627 printf("write: error=-1 (buffer too small, please put some more data in)");
628 return NT_STATUS_UNSUCCESSFUL;
631 wsize = write_file(NULL, vfs->files[fd], vfs->data, 0, size);
633 if (wsize == -1) {
634 printf("write: error=%d (%s)\n", errno, strerror(errno));
635 return NT_STATUS_UNSUCCESSFUL;
638 printf("write: ok\n");
639 return NT_STATUS_OK;
643 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
645 int fd, offset, whence;
646 off_t pos;
648 if (argc != 4) {
649 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
650 return NT_STATUS_OK;
653 fd = atoi(argv[1]);
654 offset = atoi(argv[2]);
655 whence = atoi(argv[3]);
656 switch (whence) {
657 case 1: whence = SEEK_SET; break;
658 case 2: whence = SEEK_CUR; break;
659 default: whence = SEEK_END;
662 pos = SMB_VFS_LSEEK(vfs->files[fd], offset, whence);
663 if (pos == (off_t)-1) {
664 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
665 return NT_STATUS_UNSUCCESSFUL;
668 printf("lseek: ok\n");
669 return NT_STATUS_OK;
673 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
675 int ret;
676 struct smb_filename *smb_fname_src = NULL;
677 struct smb_filename *smb_fname_dst = NULL;
679 if (argc != 3) {
680 printf("Usage: rename <old> <new>\n");
681 return NT_STATUS_OK;
684 smb_fname_src = synthetic_smb_fname_split(mem_ctx,
685 argv[1],
686 lp_posix_pathnames());
687 if (smb_fname_src == NULL) {
688 return NT_STATUS_NO_MEMORY;
691 smb_fname_dst = synthetic_smb_fname_split(mem_ctx,
692 argv[2],
693 lp_posix_pathnames());
694 if (smb_fname_dst == NULL) {
695 TALLOC_FREE(smb_fname_src);
696 return NT_STATUS_NO_MEMORY;
699 ret = SMB_VFS_RENAMEAT(vfs->conn,
700 vfs->conn->cwd_fsp,
701 smb_fname_src,
702 vfs->conn->cwd_fsp,
703 smb_fname_dst);
705 TALLOC_FREE(smb_fname_src);
706 TALLOC_FREE(smb_fname_dst);
707 if (ret == -1) {
708 printf("rename: error=%d (%s)\n", errno, strerror(errno));
709 return NT_STATUS_UNSUCCESSFUL;
712 printf("rename: ok\n");
713 return NT_STATUS_OK;
716 static NTSTATUS cmd_fsync(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
718 int ret, fd;
719 if (argc != 2) {
720 printf("Usage: fsync <fd>\n");
721 return NT_STATUS_OK;
724 fd = atoi(argv[1]);
725 ret = smb_vfs_fsync_sync(vfs->files[fd]);
726 if (ret == -1) {
727 printf("fsync: error=%d (%s)\n", errno, strerror(errno));
728 return NT_STATUS_UNSUCCESSFUL;
731 printf("fsync: ok\n");
732 return NT_STATUS_OK;
736 static NTSTATUS cmd_stat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
738 int ret;
739 const char *user;
740 const char *group;
741 struct passwd *pwd = NULL;
742 struct group *grp = NULL;
743 struct smb_filename *smb_fname = NULL;
744 SMB_STRUCT_STAT st;
745 time_t tmp_time;
747 if (argc != 2) {
748 printf("Usage: stat <fname>\n");
749 return NT_STATUS_OK;
752 smb_fname = synthetic_smb_fname_split(mem_ctx,
753 argv[1],
754 lp_posix_pathnames());
755 if (smb_fname == NULL) {
756 return NT_STATUS_NO_MEMORY;
759 ret = SMB_VFS_STAT(vfs->conn, smb_fname);
760 if (ret == -1) {
761 printf("stat: error=%d (%s)\n", errno, strerror(errno));
762 TALLOC_FREE(smb_fname);
763 return NT_STATUS_UNSUCCESSFUL;
765 st = smb_fname->st;
766 TALLOC_FREE(smb_fname);
768 pwd = getpwuid(st.st_ex_uid);
769 if (pwd != NULL) user = pwd->pw_name;
770 else user = null_string;
771 grp = getgrgid(st.st_ex_gid);
772 if (grp != NULL) group = grp->gr_name;
773 else group = null_string;
775 printf("stat: ok\n");
776 printf(" File: %s", argv[1]);
777 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
778 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
779 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
780 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
781 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
782 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
783 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
784 printf(" Size: %10u", (unsigned int)st.st_ex_size);
785 #ifdef HAVE_STAT_ST_BLOCKS
786 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
787 #endif
788 #ifdef HAVE_STAT_ST_BLKSIZE
789 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
790 #endif
791 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
792 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
793 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
794 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
795 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
796 (unsigned long)st.st_ex_gid, group);
797 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
798 printf(" Access: %s", ctime(&tmp_time));
799 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
800 printf(" Modify: %s", ctime(&tmp_time));
801 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
802 printf(" Change: %s", ctime(&tmp_time));
804 return NT_STATUS_OK;
808 static NTSTATUS cmd_fstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
810 int fd;
811 const char *user;
812 const char *group;
813 struct passwd *pwd = NULL;
814 struct group *grp = NULL;
815 SMB_STRUCT_STAT st;
816 time_t tmp_time;
818 if (argc != 2) {
819 printf("Usage: fstat <fd>\n");
820 return NT_STATUS_OK;
823 fd = atoi(argv[1]);
824 if (fd < 0 || fd >= 1024) {
825 printf("fstat: error=%d (file descriptor out of range)\n", EBADF);
826 return NT_STATUS_OK;
829 if (vfs->files[fd] == NULL) {
830 printf("fstat: error=%d (invalid file descriptor)\n", EBADF);
831 return NT_STATUS_OK;
834 if (SMB_VFS_FSTAT(vfs->files[fd], &st) == -1) {
835 printf("fstat: error=%d (%s)\n", errno, strerror(errno));
836 return NT_STATUS_UNSUCCESSFUL;
839 pwd = getpwuid(st.st_ex_uid);
840 if (pwd != NULL) user = pwd->pw_name;
841 else user = null_string;
842 grp = getgrgid(st.st_ex_gid);
843 if (grp != NULL) group = grp->gr_name;
844 else group = null_string;
846 printf("fstat: ok\n");
847 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
848 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
849 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
850 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
851 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
852 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
853 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
854 printf(" Size: %10u", (unsigned int)st.st_ex_size);
855 #ifdef HAVE_STAT_ST_BLOCKS
856 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
857 #endif
858 #ifdef HAVE_STAT_ST_BLKSIZE
859 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
860 #endif
861 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
862 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
863 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
864 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
865 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
866 (unsigned long)st.st_ex_gid, group);
867 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
868 printf(" Access: %s", ctime(&tmp_time));
869 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
870 printf(" Modify: %s", ctime(&tmp_time));
871 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
872 printf(" Change: %s", ctime(&tmp_time));
874 return NT_STATUS_OK;
878 static NTSTATUS cmd_lstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
880 const char *user;
881 const char *group;
882 struct passwd *pwd = NULL;
883 struct group *grp = NULL;
884 struct smb_filename *smb_fname = NULL;
885 SMB_STRUCT_STAT st;
886 time_t tmp_time;
888 if (argc != 2) {
889 printf("Usage: lstat <path>\n");
890 return NT_STATUS_OK;
893 smb_fname = synthetic_smb_fname_split(mem_ctx,
894 argv[1],
895 lp_posix_pathnames());
896 if (smb_fname == NULL) {
897 return NT_STATUS_NO_MEMORY;
900 if (SMB_VFS_LSTAT(vfs->conn, smb_fname) == -1) {
901 printf("lstat: error=%d (%s)\n", errno, strerror(errno));
902 TALLOC_FREE(smb_fname);
903 return NT_STATUS_UNSUCCESSFUL;
905 st = smb_fname->st;
906 TALLOC_FREE(smb_fname);
908 pwd = getpwuid(st.st_ex_uid);
909 if (pwd != NULL) user = pwd->pw_name;
910 else user = null_string;
911 grp = getgrgid(st.st_ex_gid);
912 if (grp != NULL) group = grp->gr_name;
913 else group = null_string;
915 printf("lstat: ok\n");
916 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
917 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
918 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
919 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
920 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
921 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
922 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
923 printf(" Size: %10u", (unsigned int)st.st_ex_size);
924 #ifdef HAVE_STAT_ST_BLOCKS
925 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
926 #endif
927 #ifdef HAVE_STAT_ST_BLKSIZE
928 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
929 #endif
930 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
931 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
932 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
933 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
934 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
935 (unsigned long)st.st_ex_gid, group);
936 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
937 printf(" Access: %s", ctime(&tmp_time));
938 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
939 printf(" Modify: %s", ctime(&tmp_time));
940 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
941 printf(" Change: %s", ctime(&tmp_time));
943 return NT_STATUS_OK;
947 static NTSTATUS cmd_chmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
949 struct smb_filename *smb_fname = NULL;
950 mode_t mode;
951 struct smb_filename *pathref_fname = NULL;
952 NTSTATUS status;
953 if (argc != 3) {
954 printf("Usage: chmod <path> <mode>\n");
955 return NT_STATUS_OK;
958 mode = atoi(argv[2]);
960 smb_fname = synthetic_smb_fname_split(mem_ctx,
961 argv[1],
962 lp_posix_pathnames());
963 if (smb_fname == NULL) {
964 return NT_STATUS_NO_MEMORY;
967 status = synthetic_pathref(mem_ctx,
968 vfs->conn->cwd_fsp,
969 smb_fname->base_name,
970 NULL,
971 NULL,
972 smb_fname->twrp,
973 smb_fname->flags,
974 &pathref_fname);
975 if (!NT_STATUS_IS_OK(status)) {
976 return status;
978 if (SMB_VFS_FCHMOD(pathref_fname->fsp, mode) == -1) {
979 printf("chmod: error=%d (%s)\n", errno, strerror(errno));
980 return NT_STATUS_UNSUCCESSFUL;
983 printf("chmod: ok\n");
984 return NT_STATUS_OK;
988 static NTSTATUS cmd_fchmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
990 int fd;
991 mode_t mode;
992 if (argc != 3) {
993 printf("Usage: fchmod <fd> <mode>\n");
994 return NT_STATUS_OK;
997 fd = atoi(argv[1]);
998 mode = atoi(argv[2]);
999 if (fd < 0 || fd >= 1024) {
1000 printf("fchmod: error=%d (file descriptor out of range)\n", EBADF);
1001 return NT_STATUS_OK;
1003 if (vfs->files[fd] == NULL) {
1004 printf("fchmod: error=%d (invalid file descriptor)\n", EBADF);
1005 return NT_STATUS_OK;
1008 if (SMB_VFS_FCHMOD(vfs->files[fd], mode) == -1) {
1009 printf("fchmod: error=%d (%s)\n", errno, strerror(errno));
1010 return NT_STATUS_UNSUCCESSFUL;
1013 printf("fchmod: ok\n");
1014 return NT_STATUS_OK;
1017 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1019 uid_t uid;
1020 gid_t gid;
1021 int fd;
1022 if (argc != 4) {
1023 printf("Usage: fchown <fd> <uid> <gid>\n");
1024 return NT_STATUS_OK;
1027 uid = atoi(argv[2]);
1028 gid = atoi(argv[3]);
1029 fd = atoi(argv[1]);
1030 if (fd < 0 || fd >= 1024) {
1031 printf("fchown: failure=%d (file descriptor out of range)\n", EBADF);
1032 return NT_STATUS_OK;
1034 if (vfs->files[fd] == NULL) {
1035 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
1036 return NT_STATUS_OK;
1038 if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
1039 printf("fchown error=%d (%s)\n", errno, strerror(errno));
1040 return NT_STATUS_UNSUCCESSFUL;
1043 printf("fchown: ok\n");
1044 return NT_STATUS_OK;
1048 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1050 struct smb_filename *smb_fname = SMB_VFS_GETWD(vfs->conn, talloc_tos());
1051 if (smb_fname == NULL) {
1052 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
1053 return NT_STATUS_UNSUCCESSFUL;
1056 printf("getwd: %s\n", smb_fname->base_name);
1057 TALLOC_FREE(smb_fname);
1058 return NT_STATUS_OK;
1061 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1063 struct smb_file_time ft;
1064 struct files_struct *dirfsp = NULL;
1065 struct smb_filename *smb_fname = NULL;
1066 NTSTATUS status;
1068 if (argc != 4) {
1069 printf("Usage: utime <path> <access> <modify>\n");
1070 return NT_STATUS_OK;
1073 init_smb_file_time(&ft);
1075 ft.atime = time_t_to_full_timespec(atoi(argv[2]));
1076 ft.mtime = time_t_to_full_timespec(atoi(argv[3]));
1078 status = filename_convert_dirfsp(mem_ctx,
1079 vfs->conn,
1080 argv[1],
1081 0, /* ucf_flags */
1082 0, /* twrp */
1083 &dirfsp,
1084 &smb_fname);
1085 if (!NT_STATUS_IS_OK(status)) {
1086 printf("utime: %s\n", nt_errstr(status));
1087 return status;
1090 if (SMB_VFS_FNTIMES(smb_fname->fsp, &ft) != 0) {
1091 printf("utime: error=%d (%s)\n", errno, strerror(errno));
1092 TALLOC_FREE(smb_fname);
1093 return NT_STATUS_UNSUCCESSFUL;
1096 TALLOC_FREE(smb_fname);
1097 printf("utime: ok\n");
1098 return NT_STATUS_OK;
1101 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1103 int fd;
1104 off_t off;
1105 if (argc != 3) {
1106 printf("Usage: ftruncate <fd> <length>\n");
1107 return NT_STATUS_OK;
1110 fd = atoi(argv[1]);
1111 off = atoi(argv[2]);
1112 if (fd < 0 || fd >= 1024) {
1113 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
1114 return NT_STATUS_OK;
1116 if (vfs->files[fd] == NULL) {
1117 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
1118 return NT_STATUS_OK;
1121 if (SMB_VFS_FTRUNCATE(vfs->files[fd], off) == -1) {
1122 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
1123 return NT_STATUS_UNSUCCESSFUL;
1126 printf("ftruncate: ok\n");
1127 return NT_STATUS_OK;
1130 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1132 int fd;
1133 int op;
1134 long offset;
1135 long count;
1136 int type;
1137 const char *typestr;
1139 if (argc != 6) {
1140 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
1141 printf(" ops: G = F_GETLK\n");
1142 printf(" S = F_SETLK\n");
1143 printf(" W = F_SETLKW\n");
1144 printf(" type: R = F_RDLCK\n");
1145 printf(" W = F_WRLCK\n");
1146 printf(" U = F_UNLCK\n");
1147 return NT_STATUS_OK;
1150 if (sscanf(argv[1], "%d", &fd) == 0) {
1151 printf("lock: error=-1 (error parsing fd)\n");
1152 return NT_STATUS_UNSUCCESSFUL;
1155 op = 0;
1156 switch (*argv[2]) {
1157 case 'G':
1158 op = F_GETLK;
1159 break;
1160 case 'S':
1161 op = F_SETLK;
1162 break;
1163 case 'W':
1164 op = F_SETLKW;
1165 break;
1166 default:
1167 printf("lock: error=-1 (invalid op flag!)\n");
1168 return NT_STATUS_UNSUCCESSFUL;
1171 if (sscanf(argv[3], "%ld", &offset) == 0) {
1172 printf("lock: error=-1 (error parsing fd)\n");
1173 return NT_STATUS_UNSUCCESSFUL;
1176 if (sscanf(argv[4], "%ld", &count) == 0) {
1177 printf("lock: error=-1 (error parsing fd)\n");
1178 return NT_STATUS_UNSUCCESSFUL;
1181 type = 0;
1182 typestr = argv[5];
1183 while(*typestr) {
1184 switch (*typestr) {
1185 case 'R':
1186 type |= F_RDLCK;
1187 break;
1188 case 'W':
1189 type |= F_WRLCK;
1190 break;
1191 case 'U':
1192 type |= F_UNLCK;
1193 break;
1194 default:
1195 printf("lock: error=-1 (invalid type flag!)\n");
1196 return NT_STATUS_UNSUCCESSFUL;
1198 typestr++;
1201 printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
1203 if (SMB_VFS_LOCK(vfs->files[fd], op, offset, count, type) == False) {
1204 printf("lock: error=%d (%s)\n", errno, strerror(errno));
1205 return NT_STATUS_UNSUCCESSFUL;
1208 printf("lock: ok\n");
1209 return NT_STATUS_OK;
1212 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1214 int ret;
1215 char *target = NULL;
1216 struct smb_filename target_fname;
1217 struct smb_filename *new_smb_fname = NULL;
1218 NTSTATUS status;
1220 if (argc != 3) {
1221 printf("Usage: symlink <path> <link>\n");
1222 return NT_STATUS_OK;
1225 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1226 argv[2],
1227 lp_posix_pathnames());
1228 if (new_smb_fname == NULL) {
1229 return NT_STATUS_NO_MEMORY;
1232 target = talloc_strdup(mem_ctx, argv[1]);
1233 if (target == NULL) {
1234 return NT_STATUS_NO_MEMORY;
1237 target_fname = (struct smb_filename) {
1238 .base_name = target,
1241 /* Removes @GMT tokens if any */
1242 status = canonicalize_snapshot_path(&target_fname, UCF_GMT_PATHNAME, 0);
1243 if (!NT_STATUS_IS_OK(status)) {
1244 return status;
1247 ret = SMB_VFS_SYMLINKAT(vfs->conn,
1248 &target_fname,
1249 vfs->conn->cwd_fsp,
1250 new_smb_fname);
1251 if (ret == -1) {
1252 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
1253 return NT_STATUS_UNSUCCESSFUL;
1256 printf("symlink: ok\n");
1257 return NT_STATUS_OK;
1261 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1263 char buffer[PATH_MAX];
1264 struct smb_filename *smb_fname = NULL;
1265 int size;
1267 if (argc != 2) {
1268 printf("Usage: readlink <path>\n");
1269 return NT_STATUS_OK;
1272 smb_fname = synthetic_smb_fname_split(mem_ctx,
1273 argv[1],
1274 lp_posix_pathnames());
1275 if (smb_fname == NULL) {
1276 return NT_STATUS_NO_MEMORY;
1278 size = SMB_VFS_READLINKAT(vfs->conn,
1279 vfs->conn->cwd_fsp,
1280 smb_fname,
1281 buffer,
1282 PATH_MAX);
1284 if (size == -1) {
1285 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
1286 return NT_STATUS_UNSUCCESSFUL;
1289 buffer[size] = '\0';
1290 printf("readlink: %s\n", buffer);
1291 return NT_STATUS_OK;
1295 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1297 struct smb_filename *old_smb_fname = NULL;
1298 struct smb_filename *new_smb_fname = NULL;
1299 int ret;
1301 if (argc != 3) {
1302 printf("Usage: link <path> <link>\n");
1303 return NT_STATUS_OK;
1306 old_smb_fname = synthetic_smb_fname_split(mem_ctx,
1307 argv[1],
1308 lp_posix_pathnames());
1309 if (old_smb_fname == NULL) {
1310 return NT_STATUS_NO_MEMORY;
1312 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1313 argv[2],
1314 lp_posix_pathnames());
1315 if (new_smb_fname == NULL) {
1316 return NT_STATUS_NO_MEMORY;
1319 ret = SMB_VFS_LINKAT(vfs->conn,
1320 vfs->conn->cwd_fsp,
1321 old_smb_fname,
1322 vfs->conn->cwd_fsp,
1323 new_smb_fname,
1325 if (ret == -1) {
1326 printf("link: error=%d (%s)\n", errno, strerror(errno));
1327 return NT_STATUS_UNSUCCESSFUL;
1330 printf("link: ok\n");
1331 return NT_STATUS_OK;
1334 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1336 short _mode = 0;
1337 mode_t mode;
1338 unsigned int dev_val;
1339 SMB_DEV_T dev;
1340 struct smb_filename *smb_fname = NULL;
1341 int ret;
1343 if (argc != 4) {
1344 printf("Usage: mknod <path> <mode> <dev>\n");
1345 printf(" mode is octal\n");
1346 printf(" dev is hex\n");
1347 return NT_STATUS_OK;
1350 if (sscanf(argv[2], "%ho", &_mode) == 0) {
1351 printf("open: error=-1 (invalid mode!)\n");
1352 return NT_STATUS_UNSUCCESSFUL;
1354 mode = _mode;
1356 if (sscanf(argv[3], "%x", &dev_val) == 0) {
1357 printf("open: error=-1 (invalid dev!)\n");
1358 return NT_STATUS_UNSUCCESSFUL;
1360 dev = (SMB_DEV_T)dev_val;
1362 smb_fname = synthetic_smb_fname_split(mem_ctx,
1363 argv[1],
1364 lp_posix_pathnames());
1365 if (smb_fname == NULL) {
1366 return NT_STATUS_NO_MEMORY;
1369 ret = SMB_VFS_MKNODAT(vfs->conn,
1370 vfs->conn->cwd_fsp,
1371 smb_fname,
1372 mode,
1373 dev);
1375 if (ret == -1) {
1376 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1377 return NT_STATUS_UNSUCCESSFUL;
1380 printf("mknod: ok\n");
1381 return NT_STATUS_OK;
1384 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1386 struct smb_filename *smb_fname = NULL;
1388 if (argc != 2) {
1389 printf("Usage: realpath <path>\n");
1390 return NT_STATUS_OK;
1393 smb_fname = synthetic_smb_fname_split(mem_ctx,
1394 argv[1],
1395 lp_posix_pathnames());
1396 if (smb_fname == NULL) {
1397 return NT_STATUS_NO_MEMORY;
1399 if (SMB_VFS_REALPATH(vfs->conn, mem_ctx, smb_fname) == NULL) {
1400 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1401 return NT_STATUS_UNSUCCESSFUL;
1404 printf("realpath: ok\n");
1405 return NT_STATUS_OK;
1408 static NTSTATUS cmd_getxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1409 int argc, const char **argv)
1411 uint8_t *buf;
1412 ssize_t ret;
1413 struct smb_filename *smb_fname = NULL;
1414 struct smb_filename *pathref_fname = NULL;
1415 NTSTATUS status;
1417 if (argc != 3) {
1418 printf("Usage: getxattr <path> <xattr>\n");
1419 return NT_STATUS_OK;
1422 buf = NULL;
1424 smb_fname = synthetic_smb_fname_split(mem_ctx,
1425 argv[1],
1426 lp_posix_pathnames());
1427 if (smb_fname == NULL) {
1428 return NT_STATUS_NO_MEMORY;
1430 status = synthetic_pathref(mem_ctx,
1431 vfs->conn->cwd_fsp,
1432 smb_fname->base_name,
1433 NULL,
1434 NULL,
1435 smb_fname->twrp,
1436 smb_fname->flags,
1437 &pathref_fname);
1438 if (!NT_STATUS_IS_OK(status)) {
1439 return status;
1441 ret = SMB_VFS_FGETXATTR(pathref_fname->fsp,
1442 argv[2],
1443 buf,
1444 talloc_get_size(buf));
1445 if (ret == -1) {
1446 int err = errno;
1447 printf("getxattr returned (%s)\n", strerror(err));
1448 return map_nt_error_from_unix(err);
1450 buf = talloc_array(mem_ctx, uint8_t, ret);
1451 if (buf == NULL) {
1452 return NT_STATUS_NO_MEMORY;
1454 ret = SMB_VFS_FGETXATTR(pathref_fname->fsp,
1455 argv[2],
1456 buf,
1457 talloc_get_size(buf));
1458 if (ret == -1) {
1459 int err = errno;
1460 printf("getxattr returned (%s)\n", strerror(err));
1461 return map_nt_error_from_unix(err);
1463 dump_data_file(buf, talloc_get_size(buf), false, stdout);
1464 return NT_STATUS_OK;
1467 static NTSTATUS cmd_listxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1468 int argc, const char **argv)
1470 char *buf, *p;
1471 ssize_t ret;
1472 struct smb_filename *smb_fname = NULL;
1473 struct smb_filename *pathref_fname = NULL;
1474 NTSTATUS status;
1475 if (argc != 2) {
1476 printf("Usage: listxattr <path>\n");
1477 return NT_STATUS_OK;
1480 buf = NULL;
1482 smb_fname = synthetic_smb_fname_split(mem_ctx,
1483 argv[1],
1484 lp_posix_pathnames());
1485 if (smb_fname == NULL) {
1486 return NT_STATUS_NO_MEMORY;
1488 status = synthetic_pathref(mem_ctx,
1489 vfs->conn->cwd_fsp,
1490 smb_fname->base_name,
1491 NULL,
1492 NULL,
1493 smb_fname->twrp,
1494 smb_fname->flags,
1495 &pathref_fname);
1496 if (!NT_STATUS_IS_OK(status)) {
1497 return status;
1500 ret = SMB_VFS_FLISTXATTR(pathref_fname->fsp,
1501 buf, talloc_get_size(buf));
1502 if (ret == -1) {
1503 int err = errno;
1504 printf("listxattr returned (%s)\n", strerror(err));
1505 return map_nt_error_from_unix(err);
1507 buf = talloc_array(mem_ctx, char, ret);
1508 if (buf == NULL) {
1509 return NT_STATUS_NO_MEMORY;
1511 ret = SMB_VFS_FLISTXATTR(pathref_fname->fsp,
1512 buf, talloc_get_size(buf));
1513 if (ret == -1) {
1514 int err = errno;
1515 printf("listxattr returned (%s)\n", strerror(err));
1516 return map_nt_error_from_unix(err);
1518 if (ret == 0) {
1519 return NT_STATUS_OK;
1521 if (buf[ret-1] != '\0') {
1522 printf("listxattr returned non 0-terminated strings\n");
1523 return NT_STATUS_INTERNAL_ERROR;
1526 p = buf;
1527 while (p < buf+ret) {
1528 printf("%s\n", p);
1529 p = strchr(p, 0);
1530 p += 1;
1532 return NT_STATUS_OK;
1535 static NTSTATUS cmd_fsetxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1536 int argc, const char **argv)
1538 ssize_t ret;
1539 int flags = 0;
1540 struct smb_filename *smb_fname = NULL;
1541 struct smb_filename *pathref_fname = NULL;
1542 NTSTATUS status;
1544 if ((argc < 4) || (argc > 5)) {
1545 printf("Usage: setxattr <path> <xattr> <value> [flags]\n");
1546 return NT_STATUS_OK;
1549 if (argc == 5) {
1550 flags = atoi(argv[4]);
1553 smb_fname = synthetic_smb_fname_split(mem_ctx,
1554 argv[1],
1555 lp_posix_pathnames());
1556 if (smb_fname == NULL) {
1557 return NT_STATUS_NO_MEMORY;
1560 status = synthetic_pathref(mem_ctx,
1561 vfs->conn->cwd_fsp,
1562 smb_fname->base_name,
1563 NULL,
1564 NULL,
1565 smb_fname->twrp,
1566 smb_fname->flags,
1567 &pathref_fname);
1568 if (!NT_STATUS_IS_OK(status)) {
1569 return status;
1572 ret = SMB_VFS_FSETXATTR(pathref_fname->fsp, argv[2],
1573 argv[3], strlen(argv[3]), flags);
1574 if (ret == -1) {
1575 int err = errno;
1576 printf("fsetxattr returned (%s)\n", strerror(err));
1577 return map_nt_error_from_unix(err);
1579 return NT_STATUS_OK;
1582 static NTSTATUS cmd_removexattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1583 int argc, const char **argv)
1585 ssize_t ret;
1586 struct smb_filename *smb_fname = NULL;
1587 struct smb_filename *pathref_fname = NULL;
1588 NTSTATUS status;
1590 if (argc != 3) {
1591 printf("Usage: removexattr <path> <xattr>\n");
1592 return NT_STATUS_OK;
1595 smb_fname = synthetic_smb_fname_split(mem_ctx,
1596 argv[1],
1597 lp_posix_pathnames());
1598 if (smb_fname == NULL) {
1599 return NT_STATUS_NO_MEMORY;
1601 status = synthetic_pathref(mem_ctx,
1602 vfs->conn->cwd_fsp,
1603 smb_fname->base_name,
1604 NULL,
1605 NULL,
1606 smb_fname->twrp,
1607 smb_fname->flags,
1608 &pathref_fname);
1609 if (!NT_STATUS_IS_OK(status)) {
1610 return status;
1612 ret = SMB_VFS_FREMOVEXATTR(pathref_fname->fsp, argv[2]);
1613 if (ret == -1) {
1614 int err = errno;
1615 printf("removexattr returned (%s)\n", strerror(err));
1616 return map_nt_error_from_unix(err);
1618 return NT_STATUS_OK;
1621 static NTSTATUS cmd_fget_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1622 int argc, const char **argv)
1624 int fd;
1625 NTSTATUS status;
1626 struct security_descriptor *sd;
1628 if (argc != 2) {
1629 printf("Usage: fget_nt_acl <fd>\n");
1630 return NT_STATUS_OK;
1633 fd = atoi(argv[1]);
1634 if (fd < 0 || fd >= 1024) {
1635 printf("fget_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1636 return NT_STATUS_OK;
1638 if (vfs->files[fd] == NULL) {
1639 printf("fget_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1640 return NT_STATUS_OK;
1643 status = SMB_VFS_FGET_NT_ACL(metadata_fsp(vfs->files[fd]),
1644 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1645 talloc_tos(), &sd);
1646 if (!NT_STATUS_IS_OK(status)) {
1647 printf("fget_nt_acl returned (%s)\n", nt_errstr(status));
1648 return status;
1650 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1651 TALLOC_FREE(sd);
1652 return NT_STATUS_OK;
1655 static NTSTATUS cmd_get_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1656 int argc, const char **argv)
1658 NTSTATUS status;
1659 struct security_descriptor *sd;
1660 struct smb_filename *smb_fname = NULL;
1661 struct smb_filename *pathref_fname = NULL;
1663 if (argc != 2) {
1664 printf("Usage: get_nt_acl <path>\n");
1665 return NT_STATUS_OK;
1668 smb_fname = synthetic_smb_fname(talloc_tos(),
1669 argv[1],
1670 NULL,
1671 NULL,
1673 ssf_flags());
1675 if (smb_fname == NULL) {
1676 return NT_STATUS_NO_MEMORY;
1679 status = synthetic_pathref(mem_ctx,
1680 vfs->conn->cwd_fsp,
1681 smb_fname->base_name,
1682 NULL,
1683 NULL,
1684 smb_fname->twrp,
1685 smb_fname->flags,
1686 &pathref_fname);
1687 if (!NT_STATUS_IS_OK(status)) {
1688 TALLOC_FREE(smb_fname);
1689 return status;
1691 status = SMB_VFS_FGET_NT_ACL(pathref_fname->fsp,
1692 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1693 talloc_tos(),
1694 &sd);
1695 if (!NT_STATUS_IS_OK(status)) {
1696 printf("get_nt_acl returned (%s)\n", nt_errstr(status));
1697 TALLOC_FREE(smb_fname);
1698 TALLOC_FREE(pathref_fname);
1699 return status;
1701 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1702 TALLOC_FREE(sd);
1703 TALLOC_FREE(smb_fname);
1704 TALLOC_FREE(pathref_fname);
1705 return NT_STATUS_OK;
1708 static NTSTATUS cmd_fset_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1709 int argc, const char **argv)
1711 int fd;
1712 NTSTATUS status;
1713 struct security_descriptor *sd;
1715 if (argc != 3) {
1716 printf("Usage: fset_nt_acl <fd> <sddl>\n");
1717 return NT_STATUS_OK;
1720 fd = atoi(argv[1]);
1721 if (fd < 0 || fd >= 1024) {
1722 printf("fset_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1723 return NT_STATUS_OK;
1725 if (vfs->files[fd] == NULL) {
1726 printf("fset_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1727 return NT_STATUS_OK;
1730 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1731 if (!sd) {
1732 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1733 return NT_STATUS_INVALID_PARAMETER;
1736 status = SMB_VFS_FSET_NT_ACL(
1737 metadata_fsp(vfs->files[fd]),
1738 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1739 sd);
1740 if (!NT_STATUS_IS_OK(status)) {
1741 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1742 return status;
1744 TALLOC_FREE(sd);
1745 return NT_STATUS_OK;
1748 static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1750 struct vfs_open_how how = { .mode = 0400, };
1751 files_struct *fsp;
1752 struct files_struct *fspcwd = NULL;
1753 struct smb_filename *smb_fname = NULL;
1754 NTSTATUS status;
1755 struct security_descriptor *sd = NULL;
1756 int fd;
1758 if (argc != 3) {
1759 printf("Usage: set_nt_acl <file> <sddl>\n");
1760 return NT_STATUS_OK;
1764 fsp = talloc_zero(vfs, struct files_struct);
1765 if (fsp == NULL) {
1766 return NT_STATUS_NO_MEMORY;
1768 fsp->fh = fd_handle_create(fsp);
1769 if (fsp->fh == NULL) {
1770 TALLOC_FREE(fsp);
1771 return NT_STATUS_NO_MEMORY;
1773 fsp->conn = vfs->conn;
1775 smb_fname = synthetic_smb_fname_split(NULL,
1776 argv[1],
1777 lp_posix_pathnames());
1778 if (smb_fname == NULL) {
1779 TALLOC_FREE(fsp);
1780 return NT_STATUS_NO_MEMORY;
1783 fsp->fsp_name = smb_fname;
1785 status = vfs_at_fspcwd(fsp, vfs->conn, &fspcwd);
1786 if (!NT_STATUS_IS_OK(status)) {
1787 return status;
1790 how.flags = O_RDWR;
1791 fd = SMB_VFS_OPENAT(vfs->conn,
1792 fspcwd,
1793 smb_fname,
1794 fsp,
1795 &how);
1796 if (fd == -1 && errno == EISDIR) {
1797 #ifdef O_DIRECTORY
1798 how.flags = O_RDONLY|O_DIRECTORY;
1799 #else
1800 /* POSIX allows us to open a directory with O_RDONLY. */
1801 how.flags = O_RDONLY;
1802 #endif
1803 fd = SMB_VFS_OPENAT(vfs->conn,
1804 fspcwd,
1805 smb_fname,
1806 fsp,
1807 &how);
1809 if (fd == -1) {
1810 printf("open: error=%d (%s)\n", errno, strerror(errno));
1811 TALLOC_FREE(fsp);
1812 TALLOC_FREE(smb_fname);
1813 return NT_STATUS_UNSUCCESSFUL;
1815 fsp_set_fd(fsp, fd);
1817 status = vfs_stat_fsp(fsp);
1818 if (!NT_STATUS_IS_OK(status)) {
1819 /* If we have an fd, this stat should succeed. */
1820 DEBUG(0,("Error doing fstat on open file %s "
1821 "(%s)\n",
1822 smb_fname_str_dbg(smb_fname),
1823 nt_errstr(status) ));
1824 goto out;
1827 fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
1828 fsp->vuid = UID_FIELD_INVALID;
1829 fsp->file_pid = 0;
1830 fsp->fsp_flags.can_lock = true;
1831 fsp->fsp_flags.can_read = true;
1832 fsp->fsp_flags.can_write = true;
1833 fsp->print_file = NULL;
1834 fsp->fsp_flags.modified = false;
1835 fsp->sent_oplock_break = NO_BREAK_SENT;
1836 fsp->fsp_flags.is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
1838 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1839 if (!sd) {
1840 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1841 status = NT_STATUS_INVALID_PARAMETER;
1842 goto out;
1845 status = SMB_VFS_FSET_NT_ACL(
1846 metadata_fsp(fsp),
1847 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1848 sd);
1849 if (!NT_STATUS_IS_OK(status)) {
1850 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1851 goto out;
1853 out:
1854 TALLOC_FREE(sd);
1856 status = fd_close(fsp);
1857 if (!NT_STATUS_IS_OK(status))
1858 printf("close: error= (%s)\n", nt_errstr(status));
1860 TALLOC_FREE(fsp);
1862 return status;
1867 static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1868 int argc, const char **argv)
1870 int fd;
1871 SMB_ACL_T acl;
1872 char *acl_text;
1874 if (argc != 2) {
1875 printf("Usage: sys_acl_get_fd <fd>\n");
1876 return NT_STATUS_OK;
1879 fd = atoi(argv[1]);
1880 if (fd < 0 || fd >= 1024) {
1881 printf("sys_acl_get_fd: error=%d (file descriptor out of range)\n", EBADF);
1882 return NT_STATUS_OK;
1884 if (vfs->files[fd] == NULL) {
1885 printf("sys_acl_get_fd: error=%d (invalid file descriptor)\n", EBADF);
1886 return NT_STATUS_OK;
1889 acl = SMB_VFS_SYS_ACL_GET_FD(vfs->files[fd],
1890 SMB_ACL_TYPE_ACCESS,
1891 talloc_tos());
1892 if (!acl) {
1893 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1894 return NT_STATUS_UNSUCCESSFUL;
1896 acl_text = sys_acl_to_text(acl, NULL);
1897 printf("%s", acl_text);
1898 TALLOC_FREE(acl);
1899 SAFE_FREE(acl_text);
1900 return NT_STATUS_OK;
1903 static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1904 int argc, const char **argv)
1906 SMB_ACL_T acl;
1907 char *acl_text;
1908 int type;
1909 struct smb_filename *smb_fname = NULL;
1910 struct smb_filename *pathref_fname = NULL;
1911 NTSTATUS status;
1913 if (argc != 3) {
1914 printf("Usage: sys_acl_get_file <path> <type>\n");
1915 return NT_STATUS_OK;
1918 smb_fname = synthetic_smb_fname_split(talloc_tos(),
1919 argv[1],
1920 lp_posix_pathnames());
1921 if (smb_fname == NULL) {
1922 return NT_STATUS_NO_MEMORY;
1924 type = atoi(argv[2]);
1926 status = synthetic_pathref(mem_ctx,
1927 vfs->conn->cwd_fsp,
1928 smb_fname->base_name,
1929 NULL,
1930 NULL,
1931 smb_fname->twrp,
1932 smb_fname->flags,
1933 &pathref_fname);
1934 if (!NT_STATUS_IS_OK(status)) {
1935 TALLOC_FREE(smb_fname);
1936 return status;
1939 acl = SMB_VFS_SYS_ACL_GET_FD(pathref_fname->fsp,
1940 type, talloc_tos());
1941 if (!acl) {
1942 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1943 TALLOC_FREE(smb_fname);
1944 TALLOC_FREE(pathref_fname);
1945 return NT_STATUS_UNSUCCESSFUL;
1947 acl_text = sys_acl_to_text(acl, NULL);
1948 printf("%s", acl_text);
1949 TALLOC_FREE(acl);
1950 TALLOC_FREE(smb_fname);
1951 TALLOC_FREE(pathref_fname);
1952 SAFE_FREE(acl_text);
1953 return NT_STATUS_OK;
1956 static NTSTATUS cmd_sys_acl_blob_get_file(struct vfs_state *vfs,
1957 TALLOC_CTX *mem_ctx,
1958 int argc, const char **argv)
1960 char *description;
1961 DATA_BLOB blob;
1962 int ret;
1963 size_t i;
1964 struct smb_filename *smb_fname = NULL;
1965 struct smb_filename *pathref_fname = NULL;
1966 NTSTATUS status;
1968 if (argc != 2) {
1969 printf("Usage: sys_acl_blob_get_file <path>\n");
1970 return NT_STATUS_OK;
1973 smb_fname = synthetic_smb_fname_split(mem_ctx,
1974 argv[1],
1975 lp_posix_pathnames());
1976 if (smb_fname == NULL) {
1977 return NT_STATUS_NO_MEMORY;
1979 status = synthetic_pathref(mem_ctx,
1980 vfs->conn->cwd_fsp,
1981 smb_fname->base_name,
1982 NULL,
1983 NULL,
1984 smb_fname->twrp,
1985 smb_fname->flags,
1986 &pathref_fname);
1987 if (!NT_STATUS_IS_OK(status)) {
1988 TALLOC_FREE(smb_fname);
1989 return status;
1992 ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(pathref_fname->fsp,
1993 talloc_tos(),
1994 &description,
1995 &blob);
1996 if (ret != 0) {
1997 status = map_nt_error_from_unix(errno);
1998 printf("sys_acl_blob_get_file failed (%s)\n", strerror(errno));
1999 TALLOC_FREE(smb_fname);
2000 TALLOC_FREE(pathref_fname);
2001 return status;
2003 printf("Description: %s\n", description);
2004 for (i = 0; i < blob.length; i++) {
2005 printf("%.2x ", blob.data[i]);
2007 printf("\n");
2009 TALLOC_FREE(smb_fname);
2010 TALLOC_FREE(pathref_fname);
2011 return NT_STATUS_OK;
2014 static NTSTATUS cmd_sys_acl_blob_get_fd(struct vfs_state *vfs,
2015 TALLOC_CTX *mem_ctx,
2016 int argc, const char **argv)
2018 int fd;
2019 char *description;
2020 DATA_BLOB blob;
2021 int ret;
2022 size_t i;
2024 if (argc != 2) {
2025 printf("Usage: sys_acl_blob_get_fd <fd>\n");
2026 return NT_STATUS_OK;
2029 fd = atoi(argv[1]);
2030 if (fd < 0 || fd >= 1024) {
2031 printf("sys_acl_blob_get_fd: error=%d "
2032 "(file descriptor out of range)\n", EBADF);
2033 return NT_STATUS_OK;
2035 if (vfs->files[fd] == NULL) {
2036 printf("sys_acl_blob_get_fd: error=%d "
2037 "(invalid file descriptor)\n", EBADF);
2038 return NT_STATUS_OK;
2041 ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(vfs->files[fd], talloc_tos(),
2042 &description, &blob);
2043 if (ret != 0) {
2044 printf("sys_acl_blob_get_fd failed (%s)\n", strerror(errno));
2045 return map_nt_error_from_unix(errno);
2047 printf("Description: %s\n", description);
2048 for (i = 0; i < blob.length; i++) {
2049 printf("%.2x ", blob.data[i]);
2051 printf("\n");
2053 return NT_STATUS_OK;
2058 static NTSTATUS cmd_sys_acl_delete_def_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
2059 int argc, const char **argv)
2061 int ret;
2062 struct smb_filename *smb_fname = NULL;
2063 struct smb_filename *pathref_fname = NULL;
2064 NTSTATUS status;
2066 if (argc != 2) {
2067 printf("Usage: sys_acl_delete_def_file <path>\n");
2068 return NT_STATUS_OK;
2071 smb_fname = synthetic_smb_fname_split(mem_ctx,
2072 argv[1],
2073 lp_posix_pathnames());
2074 if (smb_fname == NULL) {
2075 return NT_STATUS_NO_MEMORY;
2077 status = synthetic_pathref(mem_ctx,
2078 vfs->conn->cwd_fsp,
2079 smb_fname->base_name,
2080 NULL,
2081 NULL,
2082 smb_fname->twrp,
2083 smb_fname->flags,
2084 &pathref_fname);
2085 if (!NT_STATUS_IS_OK(status)) {
2086 TALLOC_FREE(smb_fname);
2087 return status;
2089 if (!pathref_fname->fsp->fsp_flags.is_directory) {
2090 printf("sys_acl_delete_def_file - %s is not a directory\n",
2091 smb_fname->base_name);
2092 TALLOC_FREE(smb_fname);
2093 TALLOC_FREE(pathref_fname);
2094 return NT_STATUS_INVALID_PARAMETER;
2096 ret = SMB_VFS_SYS_ACL_DELETE_DEF_FD(pathref_fname->fsp);
2097 if (ret == -1) {
2098 int err = errno;
2099 printf("sys_acl_delete_def_file failed (%s)\n", strerror(err));
2100 TALLOC_FREE(smb_fname);
2101 TALLOC_FREE(pathref_fname);
2102 return map_nt_error_from_unix(err);
2104 TALLOC_FREE(smb_fname);
2105 TALLOC_FREE(pathref_fname);
2106 return NT_STATUS_OK;
2109 /* Afaik translate name was first introduced with vfs_catia, to be able
2110 to translate unix file/dir-names, containing invalid windows characters,
2111 to valid windows names.
2112 The used translation direction is always unix --> windows
2114 static NTSTATUS cmd_translate_name(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
2115 int argc, const char **argv)
2117 const char *dname = NULL;
2118 char *dname_talloced = NULL;
2119 bool found = false;
2120 char *translated = NULL;
2121 struct smb_filename *smb_fname = NULL;
2122 NTSTATUS status;
2124 if (argc != 2) {
2125 DEBUG(0, ("Usage: translate_name unix_filename\n"));
2126 return NT_STATUS_UNSUCCESSFUL;
2129 smb_fname = synthetic_smb_fname(talloc_tos(),
2130 ".",
2131 NULL,
2132 NULL,
2134 ssf_flags());
2135 if (smb_fname == NULL) {
2136 return NT_STATUS_NO_MEMORY;
2139 status = OpenDir(vfs->conn,
2140 vfs->conn,
2141 smb_fname,
2142 NULL,
2144 &vfs->currentdir);
2145 if (!NT_STATUS_IS_OK(status)) {
2146 int err = map_errno_from_nt_status(status);
2147 DEBUG(0, ("cmd_translate_name: opendir error=%d (%s)\n",
2148 err, strerror(err)));
2149 TALLOC_FREE(smb_fname);
2150 errno = err;
2151 return NT_STATUS_UNSUCCESSFUL;
2154 while (true) {
2155 /* ReadDirName() returns Windows "encoding" */
2156 dname = ReadDirName(vfs->currentdir, &dname_talloced);
2157 if (dname == NULL) {
2158 break;
2161 /* Convert Windows "encoding" from ReadDirName() to UNIX */
2162 status = SMB_VFS_TRANSLATE_NAME(vfs->conn,
2163 dname,
2164 vfs_translate_to_unix,
2165 talloc_tos(),
2166 &translated);
2167 if (!NT_STATUS_IS_OK(status)) {
2168 DBG_ERR("file '%s' cannot be translated\n", argv[1]);
2169 goto cleanup;
2173 * argv[1] uses UNIX "encoding", so compare with translation
2174 * result.
2176 if (strcmp(translated, argv[1]) == 0) {
2177 found = true;
2178 break;
2180 TALLOC_FREE(dname_talloced);
2181 TALLOC_FREE(translated);
2184 if (!found) {
2185 DEBUG(0, ("cmd_translate_name: file '%s' not found.\n",
2186 argv[1]));
2187 status = NT_STATUS_UNSUCCESSFUL;
2188 goto cleanup;
2191 /* translation success. But that could also mean
2192 that translating "aaa" to "aaa" was successful :-(
2194 DBG_ERR("file '%s' --> '%s'\n", argv[1], dname);
2195 status = NT_STATUS_OK;
2197 cleanup:
2198 TALLOC_FREE(dname_talloced);
2199 TALLOC_FREE(translated);
2200 TALLOC_FREE(smb_fname);
2201 TALLOC_FREE(vfs->currentdir);
2202 return status;
2206 * This is a quick hack to demonstrate a crash in the full_audit
2207 * module when passing fsp->smb_fname into SMB_VFS_CREATE_FILE leading
2208 * to an error.
2210 * Feel free to expand with more options as needed
2212 static NTSTATUS cmd_create_file(
2213 struct vfs_state *vfs,
2214 TALLOC_CTX *mem_ctx,
2215 int argc,
2216 const char **argv)
2218 struct smb_filename *fname = NULL;
2219 struct files_struct *fsp = NULL;
2220 int info, ret;
2221 NTSTATUS status;
2223 if (argc != 2) {
2224 DBG_ERR("Usage: create_file filename\n");
2225 return NT_STATUS_UNSUCCESSFUL;
2228 fname = synthetic_smb_fname(
2229 talloc_tos(), argv[1], NULL, NULL, 0, 0);
2230 if (fname == NULL) {
2231 return NT_STATUS_NO_MEMORY;
2234 ret = vfs_stat(vfs->conn, fname);
2235 if (ret != 0) {
2236 status = map_nt_error_from_unix(errno);
2237 DBG_DEBUG("vfs_stat() failed: %s\n", strerror(errno));
2238 TALLOC_FREE(fname);
2239 return status;
2242 status = openat_pathref_fsp(vfs->conn->cwd_fsp, fname);
2243 if (!NT_STATUS_IS_OK(status)) {
2244 DBG_DEBUG("Could not open %s: %s\n",
2245 fname->base_name,
2246 nt_errstr(status));
2247 TALLOC_FREE(fname);
2248 return status;
2251 status = SMB_VFS_CREATE_FILE(
2252 vfs->conn,
2253 NULL,
2254 NULL,
2257 * Using fname->fsp->fsp_name seems to be legal,
2258 * there's code to handle this in
2259 * create_file_unixpath(). And it is actually very
2260 * worthwhile re-using the fsp_name, we can save quite
2261 * a few copies of smb_filename with that.
2263 fname->fsp->fsp_name,
2264 SEC_FILE_ALL,
2265 FILE_SHARE_NONE,
2266 FILE_OPEN,
2267 FILE_NON_DIRECTORY_FILE,
2270 NULL,
2273 NULL,
2274 NULL,
2275 &fsp,
2276 &info,
2277 NULL,
2278 NULL
2280 DBG_DEBUG("create_file returned %s\n", nt_errstr(status));
2282 TALLOC_FREE(fname);
2284 return NT_STATUS_OK;
2287 struct cmd_set vfs_commands[] = {
2289 { .name = "VFS Commands" },
2291 { "load", cmd_load_module, "Load a module", "load <module.so>" },
2292 { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
2293 { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
2294 { "connect", cmd_connect, "VFS connect()", "connect" },
2295 { "disconnect", cmd_disconnect, "VFS disconnect()", "disconnect" },
2296 { "disk_free", cmd_disk_free, "VFS disk_free()", "disk_free <path>" },
2297 { "opendir", cmd_opendir, "VFS opendir()", "opendir <fname>" },
2298 { "readdir", cmd_readdir, "VFS readdir()", "readdir" },
2299 { "mkdir", cmd_mkdir, "VFS mkdir()", "mkdir <path>" },
2300 { "rmdir", cmd_pathfunc, "VFS rmdir()", "rmdir <path>" },
2301 { "closedir", cmd_closedir, "VFS closedir()", "closedir" },
2302 { "open", cmd_open, "VFS open()", "open <fname> <flags> <mode>" },
2303 { "close", cmd_close, "VFS close()", "close <fd>" },
2304 { "read", cmd_read, "VFS read()", "read <fd> <size>" },
2305 { "write", cmd_write, "VFS write()", "write <fd> <size>" },
2306 { "lseek", cmd_lseek, "VFS lseek()", "lseek <fd> <offset> <whence>" },
2307 { "rename", cmd_rename, "VFS rename()", "rename <old> <new>" },
2308 { "fsync", cmd_fsync, "VFS fsync()", "fsync <fd>" },
2309 { "stat", cmd_stat, "VFS stat()", "stat <fname>" },
2310 { "fstat", cmd_fstat, "VFS fstat()", "fstat <fd>" },
2311 { "lstat", cmd_lstat, "VFS lstat()", "lstat <fname>" },
2312 { "unlink", cmd_pathfunc, "VFS unlink()", "unlink <fname>" },
2313 { "chmod", cmd_chmod, "VFS chmod()", "chmod <path> <mode>" },
2314 { "fchmod", cmd_fchmod, "VFS fchmod()", "fchmod <fd> <mode>" },
2315 { "fchown", cmd_fchown, "VFS fchown()", "fchown <fd> <uid> <gid>" },
2316 { "chdir", cmd_pathfunc, "VFS chdir()", "chdir <path>" },
2317 { "getwd", cmd_getwd, "VFS getwd()", "getwd" },
2318 { "utime", cmd_utime, "VFS utime()", "utime <path> <access> <modify>" },
2319 { "ftruncate", cmd_ftruncate, "VFS ftruncate()", "ftruncate <fd> <length>" },
2320 { "lock", cmd_lock, "VFS lock()", "lock <f> <op> <offset> <count> <type>" },
2321 { "symlink", cmd_symlink, "VFS symlink()", "symlink <old> <new>" },
2322 { "readlink", cmd_readlink, "VFS readlink()", "readlink <path>" },
2323 { "link", cmd_link, "VFS link()", "link <oldpath> <newpath>" },
2324 { "mknod", cmd_mknod, "VFS mknod()", "mknod <path> <mode> <dev>" },
2325 { "realpath", cmd_realpath, "VFS realpath()", "realpath <path>" },
2326 { "getxattr", cmd_getxattr, "VFS getxattr()",
2327 "getxattr <path> <name>" },
2328 { "listxattr", cmd_listxattr, "VFS listxattr()",
2329 "listxattr <path>" },
2330 { "fsetxattr", cmd_fsetxattr, "VFS fsetxattr()",
2331 "fsetxattr <path> <name> <value> [<flags>]" },
2332 { "removexattr", cmd_removexattr, "VFS removexattr()",
2333 "removexattr <path> <name>\n" },
2334 { "fget_nt_acl", cmd_fget_nt_acl, "VFS fget_nt_acl()",
2335 "fget_nt_acl <fd>\n" },
2336 { "get_nt_acl", cmd_get_nt_acl, "VFS get_nt_acl()",
2337 "get_nt_acl <path>\n" },
2338 { "fset_nt_acl", cmd_fset_nt_acl, "VFS fset_nt_acl()",
2339 "fset_nt_acl <fd>\n" },
2340 { "set_nt_acl", cmd_set_nt_acl, "VFS open() and fset_nt_acl()",
2341 "set_nt_acl <file>\n" },
2342 { "sys_acl_get_file", cmd_sys_acl_get_file, "VFS sys_acl_get_file()", "sys_acl_get_file <path>" },
2343 { "sys_acl_get_fd", cmd_sys_acl_get_fd, "VFS sys_acl_get_fd()", "sys_acl_get_fd <fd>" },
2344 { "sys_acl_blob_get_file", cmd_sys_acl_blob_get_file,
2345 "VFS sys_acl_blob_get_file()", "sys_acl_blob_get_file <path>" },
2346 { "sys_acl_blob_get_fd", cmd_sys_acl_blob_get_fd,
2347 "VFS sys_acl_blob_get_fd()", "sys_acl_blob_get_fd <path>" },
2348 { "sys_acl_delete_def_file", cmd_sys_acl_delete_def_file, "VFS sys_acl_delete_def_file()", "sys_acl_delete_def_file <path>" },
2351 #if defined(WITH_SMB1SERVER)
2352 { "test_chain", cmd_test_chain, "test chain code",
2353 "test_chain" },
2354 #endif
2355 { "translate_name", cmd_translate_name, "VFS translate_name()", "translate_name unix_filename" },
2356 { "create_file",
2357 cmd_create_file,
2358 "VFS create_file()",
2359 "create_file <filename>"