smbd: Make reopen_from_fsp() public
[samba4-gss.git] / source3 / torture / cmd_vfs.c
blobd9b921b01e80040595e605a90d9d753873889883
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;
678 struct vfs_rename_how rhow = { .flags = 0, };
680 if (argc != 3) {
681 printf("Usage: rename <old> <new>\n");
682 return NT_STATUS_OK;
685 smb_fname_src = synthetic_smb_fname_split(mem_ctx,
686 argv[1],
687 lp_posix_pathnames());
688 if (smb_fname_src == NULL) {
689 return NT_STATUS_NO_MEMORY;
692 smb_fname_dst = synthetic_smb_fname_split(mem_ctx,
693 argv[2],
694 lp_posix_pathnames());
695 if (smb_fname_dst == NULL) {
696 TALLOC_FREE(smb_fname_src);
697 return NT_STATUS_NO_MEMORY;
700 ret = SMB_VFS_RENAMEAT(vfs->conn,
701 vfs->conn->cwd_fsp,
702 smb_fname_src,
703 vfs->conn->cwd_fsp,
704 smb_fname_dst,
705 &rhow);
707 TALLOC_FREE(smb_fname_src);
708 TALLOC_FREE(smb_fname_dst);
709 if (ret == -1) {
710 printf("rename: error=%d (%s)\n", errno, strerror(errno));
711 return NT_STATUS_UNSUCCESSFUL;
714 printf("rename: ok\n");
715 return NT_STATUS_OK;
718 static NTSTATUS cmd_fsync(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
720 int ret, fd;
721 if (argc != 2) {
722 printf("Usage: fsync <fd>\n");
723 return NT_STATUS_OK;
726 fd = atoi(argv[1]);
727 ret = smb_vfs_fsync_sync(vfs->files[fd]);
728 if (ret == -1) {
729 printf("fsync: error=%d (%s)\n", errno, strerror(errno));
730 return NT_STATUS_UNSUCCESSFUL;
733 printf("fsync: ok\n");
734 return NT_STATUS_OK;
738 static NTSTATUS cmd_stat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
740 int ret;
741 const char *user;
742 const char *group;
743 struct passwd *pwd = NULL;
744 struct group *grp = NULL;
745 struct smb_filename *smb_fname = NULL;
746 SMB_STRUCT_STAT st;
747 time_t tmp_time;
749 if (argc != 2) {
750 printf("Usage: stat <fname>\n");
751 return NT_STATUS_OK;
754 smb_fname = synthetic_smb_fname_split(mem_ctx,
755 argv[1],
756 lp_posix_pathnames());
757 if (smb_fname == NULL) {
758 return NT_STATUS_NO_MEMORY;
761 ret = SMB_VFS_STAT(vfs->conn, smb_fname);
762 if (ret == -1) {
763 printf("stat: error=%d (%s)\n", errno, strerror(errno));
764 TALLOC_FREE(smb_fname);
765 return NT_STATUS_UNSUCCESSFUL;
767 st = smb_fname->st;
768 TALLOC_FREE(smb_fname);
770 pwd = getpwuid(st.st_ex_uid);
771 if (pwd != NULL) user = pwd->pw_name;
772 else user = null_string;
773 grp = getgrgid(st.st_ex_gid);
774 if (grp != NULL) group = grp->gr_name;
775 else group = null_string;
777 printf("stat: ok\n");
778 printf(" File: %s", argv[1]);
779 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
780 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
781 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
782 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
783 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
784 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
785 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
786 printf(" Size: %10u", (unsigned int)st.st_ex_size);
787 #ifdef HAVE_STAT_ST_BLOCKS
788 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
789 #endif
790 #ifdef HAVE_STAT_ST_BLKSIZE
791 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
792 #endif
793 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
794 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
795 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
796 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
797 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
798 (unsigned long)st.st_ex_gid, group);
799 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
800 printf(" Access: %s", ctime(&tmp_time));
801 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
802 printf(" Modify: %s", ctime(&tmp_time));
803 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
804 printf(" Change: %s", ctime(&tmp_time));
806 return NT_STATUS_OK;
810 static NTSTATUS cmd_fstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
812 int fd;
813 const char *user;
814 const char *group;
815 struct passwd *pwd = NULL;
816 struct group *grp = NULL;
817 SMB_STRUCT_STAT st;
818 time_t tmp_time;
820 if (argc != 2) {
821 printf("Usage: fstat <fd>\n");
822 return NT_STATUS_OK;
825 fd = atoi(argv[1]);
826 if (fd < 0 || fd >= 1024) {
827 printf("fstat: error=%d (file descriptor out of range)\n", EBADF);
828 return NT_STATUS_OK;
831 if (vfs->files[fd] == NULL) {
832 printf("fstat: error=%d (invalid file descriptor)\n", EBADF);
833 return NT_STATUS_OK;
836 if (SMB_VFS_FSTAT(vfs->files[fd], &st) == -1) {
837 printf("fstat: error=%d (%s)\n", errno, strerror(errno));
838 return NT_STATUS_UNSUCCESSFUL;
841 pwd = getpwuid(st.st_ex_uid);
842 if (pwd != NULL) user = pwd->pw_name;
843 else user = null_string;
844 grp = getgrgid(st.st_ex_gid);
845 if (grp != NULL) group = grp->gr_name;
846 else group = null_string;
848 printf("fstat: ok\n");
849 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
850 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
851 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
852 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
853 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
854 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
855 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
856 printf(" Size: %10u", (unsigned int)st.st_ex_size);
857 #ifdef HAVE_STAT_ST_BLOCKS
858 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
859 #endif
860 #ifdef HAVE_STAT_ST_BLKSIZE
861 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
862 #endif
863 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
864 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
865 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
866 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
867 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
868 (unsigned long)st.st_ex_gid, group);
869 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
870 printf(" Access: %s", ctime(&tmp_time));
871 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
872 printf(" Modify: %s", ctime(&tmp_time));
873 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
874 printf(" Change: %s", ctime(&tmp_time));
876 return NT_STATUS_OK;
880 static NTSTATUS cmd_lstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
882 const char *user;
883 const char *group;
884 struct passwd *pwd = NULL;
885 struct group *grp = NULL;
886 struct smb_filename *smb_fname = NULL;
887 SMB_STRUCT_STAT st;
888 time_t tmp_time;
890 if (argc != 2) {
891 printf("Usage: lstat <path>\n");
892 return NT_STATUS_OK;
895 smb_fname = synthetic_smb_fname_split(mem_ctx,
896 argv[1],
897 lp_posix_pathnames());
898 if (smb_fname == NULL) {
899 return NT_STATUS_NO_MEMORY;
902 if (SMB_VFS_LSTAT(vfs->conn, smb_fname) == -1) {
903 printf("lstat: error=%d (%s)\n", errno, strerror(errno));
904 TALLOC_FREE(smb_fname);
905 return NT_STATUS_UNSUCCESSFUL;
907 st = smb_fname->st;
908 TALLOC_FREE(smb_fname);
910 pwd = getpwuid(st.st_ex_uid);
911 if (pwd != NULL) user = pwd->pw_name;
912 else user = null_string;
913 grp = getgrgid(st.st_ex_gid);
914 if (grp != NULL) group = grp->gr_name;
915 else group = null_string;
917 printf("lstat: ok\n");
918 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
919 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
920 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
921 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
922 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
923 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
924 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
925 printf(" Size: %10u", (unsigned int)st.st_ex_size);
926 #ifdef HAVE_STAT_ST_BLOCKS
927 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
928 #endif
929 #ifdef HAVE_STAT_ST_BLKSIZE
930 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
931 #endif
932 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
933 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
934 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
935 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
936 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
937 (unsigned long)st.st_ex_gid, group);
938 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
939 printf(" Access: %s", ctime(&tmp_time));
940 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
941 printf(" Modify: %s", ctime(&tmp_time));
942 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
943 printf(" Change: %s", ctime(&tmp_time));
945 return NT_STATUS_OK;
949 static NTSTATUS cmd_chmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
951 struct smb_filename *smb_fname = NULL;
952 mode_t mode;
953 struct smb_filename *pathref_fname = NULL;
954 NTSTATUS status;
955 if (argc != 3) {
956 printf("Usage: chmod <path> <mode>\n");
957 return NT_STATUS_OK;
960 mode = atoi(argv[2]);
962 smb_fname = synthetic_smb_fname_split(mem_ctx,
963 argv[1],
964 lp_posix_pathnames());
965 if (smb_fname == NULL) {
966 return NT_STATUS_NO_MEMORY;
969 status = synthetic_pathref(mem_ctx,
970 vfs->conn->cwd_fsp,
971 smb_fname->base_name,
972 NULL,
973 NULL,
974 smb_fname->twrp,
975 smb_fname->flags,
976 &pathref_fname);
977 if (!NT_STATUS_IS_OK(status)) {
978 return status;
980 if (SMB_VFS_FCHMOD(pathref_fname->fsp, mode) == -1) {
981 printf("chmod: error=%d (%s)\n", errno, strerror(errno));
982 return NT_STATUS_UNSUCCESSFUL;
985 printf("chmod: ok\n");
986 return NT_STATUS_OK;
990 static NTSTATUS cmd_fchmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
992 int fd;
993 mode_t mode;
994 if (argc != 3) {
995 printf("Usage: fchmod <fd> <mode>\n");
996 return NT_STATUS_OK;
999 fd = atoi(argv[1]);
1000 mode = atoi(argv[2]);
1001 if (fd < 0 || fd >= 1024) {
1002 printf("fchmod: error=%d (file descriptor out of range)\n", EBADF);
1003 return NT_STATUS_OK;
1005 if (vfs->files[fd] == NULL) {
1006 printf("fchmod: error=%d (invalid file descriptor)\n", EBADF);
1007 return NT_STATUS_OK;
1010 if (SMB_VFS_FCHMOD(vfs->files[fd], mode) == -1) {
1011 printf("fchmod: error=%d (%s)\n", errno, strerror(errno));
1012 return NT_STATUS_UNSUCCESSFUL;
1015 printf("fchmod: ok\n");
1016 return NT_STATUS_OK;
1019 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1021 uid_t uid;
1022 gid_t gid;
1023 int fd;
1024 if (argc != 4) {
1025 printf("Usage: fchown <fd> <uid> <gid>\n");
1026 return NT_STATUS_OK;
1029 uid = atoi(argv[2]);
1030 gid = atoi(argv[3]);
1031 fd = atoi(argv[1]);
1032 if (fd < 0 || fd >= 1024) {
1033 printf("fchown: failure=%d (file descriptor out of range)\n", EBADF);
1034 return NT_STATUS_OK;
1036 if (vfs->files[fd] == NULL) {
1037 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
1038 return NT_STATUS_OK;
1040 if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
1041 printf("fchown error=%d (%s)\n", errno, strerror(errno));
1042 return NT_STATUS_UNSUCCESSFUL;
1045 printf("fchown: ok\n");
1046 return NT_STATUS_OK;
1050 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1052 struct smb_filename *smb_fname = SMB_VFS_GETWD(vfs->conn, talloc_tos());
1053 if (smb_fname == NULL) {
1054 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
1055 return NT_STATUS_UNSUCCESSFUL;
1058 printf("getwd: %s\n", smb_fname->base_name);
1059 TALLOC_FREE(smb_fname);
1060 return NT_STATUS_OK;
1063 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1065 struct smb_file_time ft;
1066 struct files_struct *dirfsp = NULL;
1067 struct smb_filename *smb_fname = NULL;
1068 NTSTATUS status;
1070 if (argc != 4) {
1071 printf("Usage: utime <path> <access> <modify>\n");
1072 return NT_STATUS_OK;
1075 init_smb_file_time(&ft);
1077 ft.atime = time_t_to_full_timespec(atoi(argv[2]));
1078 ft.mtime = time_t_to_full_timespec(atoi(argv[3]));
1080 status = filename_convert_dirfsp(mem_ctx,
1081 vfs->conn,
1082 argv[1],
1083 0, /* ucf_flags */
1084 0, /* twrp */
1085 &dirfsp,
1086 &smb_fname);
1087 if (!NT_STATUS_IS_OK(status)) {
1088 printf("utime: %s\n", nt_errstr(status));
1089 return status;
1092 if (SMB_VFS_FNTIMES(smb_fname->fsp, &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)
1105 int fd;
1106 off_t off;
1107 if (argc != 3) {
1108 printf("Usage: ftruncate <fd> <length>\n");
1109 return NT_STATUS_OK;
1112 fd = atoi(argv[1]);
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)
1134 int fd;
1135 int op;
1136 long offset;
1137 long count;
1138 int type;
1139 const char *typestr;
1141 if (argc != 6) {
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;
1157 op = 0;
1158 switch (*argv[2]) {
1159 case 'G':
1160 op = F_GETLK;
1161 break;
1162 case 'S':
1163 op = F_SETLK;
1164 break;
1165 case 'W':
1166 op = F_SETLKW;
1167 break;
1168 default:
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;
1183 type = 0;
1184 typestr = argv[5];
1185 while(*typestr) {
1186 switch (*typestr) {
1187 case 'R':
1188 type |= F_RDLCK;
1189 break;
1190 case 'W':
1191 type |= F_WRLCK;
1192 break;
1193 case 'U':
1194 type |= F_UNLCK;
1195 break;
1196 default:
1197 printf("lock: error=-1 (invalid type flag!)\n");
1198 return NT_STATUS_UNSUCCESSFUL;
1200 typestr++;
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 int ret;
1217 char *target = NULL;
1218 struct smb_filename target_fname;
1219 struct smb_filename *new_smb_fname = NULL;
1220 NTSTATUS status;
1222 if (argc != 3) {
1223 printf("Usage: symlink <path> <link>\n");
1224 return NT_STATUS_OK;
1227 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1228 argv[2],
1229 lp_posix_pathnames());
1230 if (new_smb_fname == NULL) {
1231 return NT_STATUS_NO_MEMORY;
1234 target = talloc_strdup(mem_ctx, argv[1]);
1235 if (target == NULL) {
1236 return NT_STATUS_NO_MEMORY;
1239 target_fname = (struct smb_filename) {
1240 .base_name = target,
1243 /* Removes @GMT tokens if any */
1244 status = canonicalize_snapshot_path(&target_fname, UCF_GMT_PATHNAME, 0);
1245 if (!NT_STATUS_IS_OK(status)) {
1246 return status;
1249 ret = SMB_VFS_SYMLINKAT(vfs->conn,
1250 &target_fname,
1251 vfs->conn->cwd_fsp,
1252 new_smb_fname);
1253 if (ret == -1) {
1254 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
1255 return NT_STATUS_UNSUCCESSFUL;
1258 printf("symlink: ok\n");
1259 return NT_STATUS_OK;
1263 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1265 char buffer[PATH_MAX];
1266 struct smb_filename *smb_fname = NULL;
1267 int size;
1269 if (argc != 2) {
1270 printf("Usage: readlink <path>\n");
1271 return NT_STATUS_OK;
1274 smb_fname = synthetic_smb_fname_split(mem_ctx,
1275 argv[1],
1276 lp_posix_pathnames());
1277 if (smb_fname == NULL) {
1278 return NT_STATUS_NO_MEMORY;
1280 size = SMB_VFS_READLINKAT(vfs->conn,
1281 vfs->conn->cwd_fsp,
1282 smb_fname,
1283 buffer,
1284 PATH_MAX);
1286 if (size == -1) {
1287 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
1288 return NT_STATUS_UNSUCCESSFUL;
1291 buffer[size] = '\0';
1292 printf("readlink: %s\n", buffer);
1293 return NT_STATUS_OK;
1297 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1299 struct smb_filename *old_smb_fname = NULL;
1300 struct smb_filename *new_smb_fname = NULL;
1301 int ret;
1303 if (argc != 3) {
1304 printf("Usage: link <path> <link>\n");
1305 return NT_STATUS_OK;
1308 old_smb_fname = synthetic_smb_fname_split(mem_ctx,
1309 argv[1],
1310 lp_posix_pathnames());
1311 if (old_smb_fname == NULL) {
1312 return NT_STATUS_NO_MEMORY;
1314 new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1315 argv[2],
1316 lp_posix_pathnames());
1317 if (new_smb_fname == NULL) {
1318 return NT_STATUS_NO_MEMORY;
1321 ret = SMB_VFS_LINKAT(vfs->conn,
1322 vfs->conn->cwd_fsp,
1323 old_smb_fname,
1324 vfs->conn->cwd_fsp,
1325 new_smb_fname,
1327 if (ret == -1) {
1328 printf("link: error=%d (%s)\n", errno, strerror(errno));
1329 return NT_STATUS_UNSUCCESSFUL;
1332 printf("link: ok\n");
1333 return NT_STATUS_OK;
1336 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1338 short _mode = 0;
1339 mode_t mode;
1340 unsigned int dev_val;
1341 SMB_DEV_T dev;
1342 struct smb_filename *smb_fname = NULL;
1343 int ret;
1345 if (argc != 4) {
1346 printf("Usage: mknod <path> <mode> <dev>\n");
1347 printf(" mode is octal\n");
1348 printf(" dev is hex\n");
1349 return NT_STATUS_OK;
1352 if (sscanf(argv[2], "%ho", &_mode) == 0) {
1353 printf("open: error=-1 (invalid mode!)\n");
1354 return NT_STATUS_UNSUCCESSFUL;
1356 mode = _mode;
1358 if (sscanf(argv[3], "%x", &dev_val) == 0) {
1359 printf("open: error=-1 (invalid dev!)\n");
1360 return NT_STATUS_UNSUCCESSFUL;
1362 dev = (SMB_DEV_T)dev_val;
1364 smb_fname = synthetic_smb_fname_split(mem_ctx,
1365 argv[1],
1366 lp_posix_pathnames());
1367 if (smb_fname == NULL) {
1368 return NT_STATUS_NO_MEMORY;
1371 ret = SMB_VFS_MKNODAT(vfs->conn,
1372 vfs->conn->cwd_fsp,
1373 smb_fname,
1374 mode,
1375 dev);
1377 if (ret == -1) {
1378 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1379 return NT_STATUS_UNSUCCESSFUL;
1382 printf("mknod: ok\n");
1383 return NT_STATUS_OK;
1386 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1388 struct smb_filename *smb_fname = NULL;
1390 if (argc != 2) {
1391 printf("Usage: realpath <path>\n");
1392 return NT_STATUS_OK;
1395 smb_fname = synthetic_smb_fname_split(mem_ctx,
1396 argv[1],
1397 lp_posix_pathnames());
1398 if (smb_fname == NULL) {
1399 return NT_STATUS_NO_MEMORY;
1401 if (SMB_VFS_REALPATH(vfs->conn, mem_ctx, smb_fname) == NULL) {
1402 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1403 return NT_STATUS_UNSUCCESSFUL;
1406 printf("realpath: ok\n");
1407 return NT_STATUS_OK;
1410 static NTSTATUS cmd_getxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1411 int argc, const char **argv)
1413 uint8_t *buf;
1414 ssize_t ret;
1415 struct smb_filename *smb_fname = NULL;
1416 struct smb_filename *pathref_fname = NULL;
1417 NTSTATUS status;
1419 if (argc != 3) {
1420 printf("Usage: getxattr <path> <xattr>\n");
1421 return NT_STATUS_OK;
1424 buf = NULL;
1426 smb_fname = synthetic_smb_fname_split(mem_ctx,
1427 argv[1],
1428 lp_posix_pathnames());
1429 if (smb_fname == NULL) {
1430 return NT_STATUS_NO_MEMORY;
1432 status = synthetic_pathref(mem_ctx,
1433 vfs->conn->cwd_fsp,
1434 smb_fname->base_name,
1435 NULL,
1436 NULL,
1437 smb_fname->twrp,
1438 smb_fname->flags,
1439 &pathref_fname);
1440 if (!NT_STATUS_IS_OK(status)) {
1441 return status;
1443 ret = SMB_VFS_FGETXATTR(pathref_fname->fsp,
1444 argv[2],
1445 buf,
1446 talloc_get_size(buf));
1447 if (ret == -1) {
1448 int err = errno;
1449 printf("getxattr returned (%s)\n", strerror(err));
1450 return map_nt_error_from_unix(err);
1452 buf = talloc_array(mem_ctx, uint8_t, ret);
1453 if (buf == NULL) {
1454 return NT_STATUS_NO_MEMORY;
1456 ret = SMB_VFS_FGETXATTR(pathref_fname->fsp,
1457 argv[2],
1458 buf,
1459 talloc_get_size(buf));
1460 if (ret == -1) {
1461 int err = errno;
1462 printf("getxattr returned (%s)\n", strerror(err));
1463 return map_nt_error_from_unix(err);
1465 dump_data_file(buf, talloc_get_size(buf), false, stdout);
1466 return NT_STATUS_OK;
1469 static NTSTATUS cmd_listxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1470 int argc, const char **argv)
1472 char *buf, *p;
1473 ssize_t ret;
1474 struct smb_filename *smb_fname = NULL;
1475 struct smb_filename *pathref_fname = NULL;
1476 NTSTATUS status;
1477 if (argc != 2) {
1478 printf("Usage: listxattr <path>\n");
1479 return NT_STATUS_OK;
1482 buf = NULL;
1484 smb_fname = synthetic_smb_fname_split(mem_ctx,
1485 argv[1],
1486 lp_posix_pathnames());
1487 if (smb_fname == NULL) {
1488 return NT_STATUS_NO_MEMORY;
1490 status = synthetic_pathref(mem_ctx,
1491 vfs->conn->cwd_fsp,
1492 smb_fname->base_name,
1493 NULL,
1494 NULL,
1495 smb_fname->twrp,
1496 smb_fname->flags,
1497 &pathref_fname);
1498 if (!NT_STATUS_IS_OK(status)) {
1499 return status;
1502 ret = SMB_VFS_FLISTXATTR(pathref_fname->fsp,
1503 buf, talloc_get_size(buf));
1504 if (ret == -1) {
1505 int err = errno;
1506 printf("listxattr returned (%s)\n", strerror(err));
1507 return map_nt_error_from_unix(err);
1509 buf = talloc_array(mem_ctx, char, ret);
1510 if (buf == NULL) {
1511 return NT_STATUS_NO_MEMORY;
1513 ret = SMB_VFS_FLISTXATTR(pathref_fname->fsp,
1514 buf, talloc_get_size(buf));
1515 if (ret == -1) {
1516 int err = errno;
1517 printf("listxattr returned (%s)\n", strerror(err));
1518 return map_nt_error_from_unix(err);
1520 if (ret == 0) {
1521 return NT_STATUS_OK;
1523 if (buf[ret-1] != '\0') {
1524 printf("listxattr returned non 0-terminated strings\n");
1525 return NT_STATUS_INTERNAL_ERROR;
1528 p = buf;
1529 while (p < buf+ret) {
1530 printf("%s\n", p);
1531 p = strchr(p, 0);
1532 p += 1;
1534 return NT_STATUS_OK;
1537 static NTSTATUS cmd_fsetxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1538 int argc, const char **argv)
1540 ssize_t ret;
1541 int flags = 0;
1542 struct smb_filename *smb_fname = NULL;
1543 struct smb_filename *pathref_fname = NULL;
1544 NTSTATUS status;
1546 if ((argc < 4) || (argc > 5)) {
1547 printf("Usage: setxattr <path> <xattr> <value> [flags]\n");
1548 return NT_STATUS_OK;
1551 if (argc == 5) {
1552 flags = atoi(argv[4]);
1555 smb_fname = synthetic_smb_fname_split(mem_ctx,
1556 argv[1],
1557 lp_posix_pathnames());
1558 if (smb_fname == NULL) {
1559 return NT_STATUS_NO_MEMORY;
1562 status = synthetic_pathref(mem_ctx,
1563 vfs->conn->cwd_fsp,
1564 smb_fname->base_name,
1565 NULL,
1566 NULL,
1567 smb_fname->twrp,
1568 smb_fname->flags,
1569 &pathref_fname);
1570 if (!NT_STATUS_IS_OK(status)) {
1571 return status;
1574 ret = SMB_VFS_FSETXATTR(pathref_fname->fsp, argv[2],
1575 argv[3], strlen(argv[3]), flags);
1576 if (ret == -1) {
1577 int err = errno;
1578 printf("fsetxattr returned (%s)\n", strerror(err));
1579 return map_nt_error_from_unix(err);
1581 return NT_STATUS_OK;
1584 static NTSTATUS cmd_removexattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1585 int argc, const char **argv)
1587 ssize_t ret;
1588 struct smb_filename *smb_fname = NULL;
1589 struct smb_filename *pathref_fname = NULL;
1590 NTSTATUS status;
1592 if (argc != 3) {
1593 printf("Usage: removexattr <path> <xattr>\n");
1594 return NT_STATUS_OK;
1597 smb_fname = synthetic_smb_fname_split(mem_ctx,
1598 argv[1],
1599 lp_posix_pathnames());
1600 if (smb_fname == NULL) {
1601 return NT_STATUS_NO_MEMORY;
1603 status = synthetic_pathref(mem_ctx,
1604 vfs->conn->cwd_fsp,
1605 smb_fname->base_name,
1606 NULL,
1607 NULL,
1608 smb_fname->twrp,
1609 smb_fname->flags,
1610 &pathref_fname);
1611 if (!NT_STATUS_IS_OK(status)) {
1612 return status;
1614 ret = SMB_VFS_FREMOVEXATTR(pathref_fname->fsp, argv[2]);
1615 if (ret == -1) {
1616 int err = errno;
1617 printf("removexattr returned (%s)\n", strerror(err));
1618 return map_nt_error_from_unix(err);
1620 return NT_STATUS_OK;
1623 static NTSTATUS cmd_fget_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1624 int argc, const char **argv)
1626 int fd;
1627 NTSTATUS status;
1628 struct security_descriptor *sd;
1630 if (argc != 2) {
1631 printf("Usage: fget_nt_acl <fd>\n");
1632 return NT_STATUS_OK;
1635 fd = atoi(argv[1]);
1636 if (fd < 0 || fd >= 1024) {
1637 printf("fget_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1638 return NT_STATUS_OK;
1640 if (vfs->files[fd] == NULL) {
1641 printf("fget_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1642 return NT_STATUS_OK;
1645 status = SMB_VFS_FGET_NT_ACL(metadata_fsp(vfs->files[fd]),
1646 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1647 talloc_tos(), &sd);
1648 if (!NT_STATUS_IS_OK(status)) {
1649 printf("fget_nt_acl returned (%s)\n", nt_errstr(status));
1650 return status;
1652 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1653 TALLOC_FREE(sd);
1654 return NT_STATUS_OK;
1657 static NTSTATUS cmd_get_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1658 int argc, const char **argv)
1660 NTSTATUS status;
1661 struct security_descriptor *sd;
1662 struct smb_filename *smb_fname = NULL;
1663 struct smb_filename *pathref_fname = NULL;
1665 if (argc != 2) {
1666 printf("Usage: get_nt_acl <path>\n");
1667 return NT_STATUS_OK;
1670 smb_fname = synthetic_smb_fname(talloc_tos(),
1671 argv[1],
1672 NULL,
1673 NULL,
1675 ssf_flags());
1677 if (smb_fname == NULL) {
1678 return NT_STATUS_NO_MEMORY;
1681 status = synthetic_pathref(mem_ctx,
1682 vfs->conn->cwd_fsp,
1683 smb_fname->base_name,
1684 NULL,
1685 NULL,
1686 smb_fname->twrp,
1687 smb_fname->flags,
1688 &pathref_fname);
1689 if (!NT_STATUS_IS_OK(status)) {
1690 TALLOC_FREE(smb_fname);
1691 return status;
1693 status = SMB_VFS_FGET_NT_ACL(pathref_fname->fsp,
1694 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1695 talloc_tos(),
1696 &sd);
1697 if (!NT_STATUS_IS_OK(status)) {
1698 printf("get_nt_acl returned (%s)\n", nt_errstr(status));
1699 TALLOC_FREE(smb_fname);
1700 TALLOC_FREE(pathref_fname);
1701 return status;
1703 printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1704 TALLOC_FREE(sd);
1705 TALLOC_FREE(smb_fname);
1706 TALLOC_FREE(pathref_fname);
1707 return NT_STATUS_OK;
1710 static NTSTATUS cmd_fset_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1711 int argc, const char **argv)
1713 int fd;
1714 NTSTATUS status;
1715 struct security_descriptor *sd;
1717 if (argc != 3) {
1718 printf("Usage: fset_nt_acl <fd> <sddl>\n");
1719 return NT_STATUS_OK;
1722 fd = atoi(argv[1]);
1723 if (fd < 0 || fd >= 1024) {
1724 printf("fset_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1725 return NT_STATUS_OK;
1727 if (vfs->files[fd] == NULL) {
1728 printf("fset_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1729 return NT_STATUS_OK;
1732 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1733 if (!sd) {
1734 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1735 return NT_STATUS_INVALID_PARAMETER;
1738 status = SMB_VFS_FSET_NT_ACL(
1739 metadata_fsp(vfs->files[fd]),
1740 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1741 sd);
1742 if (!NT_STATUS_IS_OK(status)) {
1743 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1744 return status;
1746 TALLOC_FREE(sd);
1747 return NT_STATUS_OK;
1750 static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1752 struct vfs_open_how how = { .mode = 0400, };
1753 files_struct *fsp;
1754 struct files_struct *fspcwd = NULL;
1755 struct smb_filename *smb_fname = NULL;
1756 NTSTATUS status;
1757 struct security_descriptor *sd = NULL;
1758 int fd;
1760 if (argc != 3) {
1761 printf("Usage: set_nt_acl <file> <sddl>\n");
1762 return NT_STATUS_OK;
1766 fsp = talloc_zero(vfs, struct files_struct);
1767 if (fsp == NULL) {
1768 return NT_STATUS_NO_MEMORY;
1770 fsp->fh = fd_handle_create(fsp);
1771 if (fsp->fh == NULL) {
1772 TALLOC_FREE(fsp);
1773 return NT_STATUS_NO_MEMORY;
1775 fsp->conn = vfs->conn;
1777 smb_fname = synthetic_smb_fname_split(NULL,
1778 argv[1],
1779 lp_posix_pathnames());
1780 if (smb_fname == NULL) {
1781 TALLOC_FREE(fsp);
1782 return NT_STATUS_NO_MEMORY;
1785 fsp->fsp_name = smb_fname;
1787 status = vfs_at_fspcwd(fsp, vfs->conn, &fspcwd);
1788 if (!NT_STATUS_IS_OK(status)) {
1789 return status;
1792 how.flags = O_RDWR;
1793 fd = SMB_VFS_OPENAT(vfs->conn,
1794 fspcwd,
1795 smb_fname,
1796 fsp,
1797 &how);
1798 if (fd == -1 && errno == EISDIR) {
1799 #ifdef O_DIRECTORY
1800 how.flags = O_RDONLY|O_DIRECTORY;
1801 #else
1802 /* POSIX allows us to open a directory with O_RDONLY. */
1803 how.flags = O_RDONLY;
1804 #endif
1805 fd = SMB_VFS_OPENAT(vfs->conn,
1806 fspcwd,
1807 smb_fname,
1808 fsp,
1809 &how);
1811 if (fd == -1) {
1812 printf("open: error=%d (%s)\n", errno, strerror(errno));
1813 TALLOC_FREE(fsp);
1814 TALLOC_FREE(smb_fname);
1815 return NT_STATUS_UNSUCCESSFUL;
1817 fsp_set_fd(fsp, fd);
1819 status = vfs_stat_fsp(fsp);
1820 if (!NT_STATUS_IS_OK(status)) {
1821 /* If we have an fd, this stat should succeed. */
1822 DEBUG(0,("Error doing fstat on open file %s "
1823 "(%s)\n",
1824 smb_fname_str_dbg(smb_fname),
1825 nt_errstr(status) ));
1826 goto out;
1829 fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
1830 fsp->vuid = UID_FIELD_INVALID;
1831 fsp->file_pid = 0;
1832 fsp->fsp_flags.can_lock = true;
1833 fsp->fsp_flags.can_read = true;
1834 fsp->fsp_flags.can_write = true;
1835 fsp->print_file = NULL;
1836 fsp->fsp_flags.modified = false;
1837 fsp->sent_oplock_break = NO_BREAK_SENT;
1838 fsp->fsp_flags.is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
1840 sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1841 if (!sd) {
1842 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1843 status = NT_STATUS_INVALID_PARAMETER;
1844 goto out;
1847 status = SMB_VFS_FSET_NT_ACL(
1848 metadata_fsp(fsp),
1849 SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1850 sd);
1851 if (!NT_STATUS_IS_OK(status)) {
1852 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1853 goto out;
1855 out:
1856 TALLOC_FREE(sd);
1858 status = fd_close(fsp);
1859 if (!NT_STATUS_IS_OK(status))
1860 printf("close: error= (%s)\n", nt_errstr(status));
1862 TALLOC_FREE(fsp);
1864 return status;
1869 static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1870 int argc, const char **argv)
1872 int fd;
1873 SMB_ACL_T acl;
1874 char *acl_text;
1876 if (argc != 2) {
1877 printf("Usage: sys_acl_get_fd <fd>\n");
1878 return NT_STATUS_OK;
1881 fd = atoi(argv[1]);
1882 if (fd < 0 || fd >= 1024) {
1883 printf("sys_acl_get_fd: error=%d (file descriptor out of range)\n", EBADF);
1884 return NT_STATUS_OK;
1886 if (vfs->files[fd] == NULL) {
1887 printf("sys_acl_get_fd: error=%d (invalid file descriptor)\n", EBADF);
1888 return NT_STATUS_OK;
1891 acl = SMB_VFS_SYS_ACL_GET_FD(vfs->files[fd],
1892 SMB_ACL_TYPE_ACCESS,
1893 talloc_tos());
1894 if (!acl) {
1895 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1896 return NT_STATUS_UNSUCCESSFUL;
1898 acl_text = sys_acl_to_text(acl, NULL);
1899 printf("%s", acl_text);
1900 TALLOC_FREE(acl);
1901 SAFE_FREE(acl_text);
1902 return NT_STATUS_OK;
1905 static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1906 int argc, const char **argv)
1908 SMB_ACL_T acl;
1909 char *acl_text;
1910 int type;
1911 struct smb_filename *smb_fname = NULL;
1912 struct smb_filename *pathref_fname = NULL;
1913 NTSTATUS status;
1915 if (argc != 3) {
1916 printf("Usage: sys_acl_get_file <path> <type>\n");
1917 return NT_STATUS_OK;
1920 smb_fname = synthetic_smb_fname_split(talloc_tos(),
1921 argv[1],
1922 lp_posix_pathnames());
1923 if (smb_fname == NULL) {
1924 return NT_STATUS_NO_MEMORY;
1926 type = atoi(argv[2]);
1928 status = synthetic_pathref(mem_ctx,
1929 vfs->conn->cwd_fsp,
1930 smb_fname->base_name,
1931 NULL,
1932 NULL,
1933 smb_fname->twrp,
1934 smb_fname->flags,
1935 &pathref_fname);
1936 if (!NT_STATUS_IS_OK(status)) {
1937 TALLOC_FREE(smb_fname);
1938 return status;
1941 acl = SMB_VFS_SYS_ACL_GET_FD(pathref_fname->fsp,
1942 type, talloc_tos());
1943 if (!acl) {
1944 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1945 TALLOC_FREE(smb_fname);
1946 TALLOC_FREE(pathref_fname);
1947 return NT_STATUS_UNSUCCESSFUL;
1949 acl_text = sys_acl_to_text(acl, NULL);
1950 printf("%s", acl_text);
1951 TALLOC_FREE(acl);
1952 TALLOC_FREE(smb_fname);
1953 TALLOC_FREE(pathref_fname);
1954 SAFE_FREE(acl_text);
1955 return NT_STATUS_OK;
1958 static NTSTATUS cmd_sys_acl_blob_get_file(struct vfs_state *vfs,
1959 TALLOC_CTX *mem_ctx,
1960 int argc, const char **argv)
1962 char *description;
1963 DATA_BLOB blob;
1964 int ret;
1965 size_t i;
1966 struct smb_filename *smb_fname = NULL;
1967 struct smb_filename *pathref_fname = NULL;
1968 NTSTATUS status;
1970 if (argc != 2) {
1971 printf("Usage: sys_acl_blob_get_file <path>\n");
1972 return NT_STATUS_OK;
1975 smb_fname = synthetic_smb_fname_split(mem_ctx,
1976 argv[1],
1977 lp_posix_pathnames());
1978 if (smb_fname == NULL) {
1979 return NT_STATUS_NO_MEMORY;
1981 status = synthetic_pathref(mem_ctx,
1982 vfs->conn->cwd_fsp,
1983 smb_fname->base_name,
1984 NULL,
1985 NULL,
1986 smb_fname->twrp,
1987 smb_fname->flags,
1988 &pathref_fname);
1989 if (!NT_STATUS_IS_OK(status)) {
1990 TALLOC_FREE(smb_fname);
1991 return status;
1994 ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(pathref_fname->fsp,
1995 talloc_tos(),
1996 &description,
1997 &blob);
1998 if (ret != 0) {
1999 status = map_nt_error_from_unix(errno);
2000 printf("sys_acl_blob_get_file failed (%s)\n", strerror(errno));
2001 TALLOC_FREE(smb_fname);
2002 TALLOC_FREE(pathref_fname);
2003 return status;
2005 printf("Description: %s\n", description);
2006 for (i = 0; i < blob.length; i++) {
2007 printf("%.2x ", blob.data[i]);
2009 printf("\n");
2011 TALLOC_FREE(smb_fname);
2012 TALLOC_FREE(pathref_fname);
2013 return NT_STATUS_OK;
2016 static NTSTATUS cmd_sys_acl_blob_get_fd(struct vfs_state *vfs,
2017 TALLOC_CTX *mem_ctx,
2018 int argc, const char **argv)
2020 int fd;
2021 char *description;
2022 DATA_BLOB blob;
2023 int ret;
2024 size_t i;
2026 if (argc != 2) {
2027 printf("Usage: sys_acl_blob_get_fd <fd>\n");
2028 return NT_STATUS_OK;
2031 fd = atoi(argv[1]);
2032 if (fd < 0 || fd >= 1024) {
2033 printf("sys_acl_blob_get_fd: error=%d "
2034 "(file descriptor out of range)\n", EBADF);
2035 return NT_STATUS_OK;
2037 if (vfs->files[fd] == NULL) {
2038 printf("sys_acl_blob_get_fd: error=%d "
2039 "(invalid file descriptor)\n", EBADF);
2040 return NT_STATUS_OK;
2043 ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(vfs->files[fd], talloc_tos(),
2044 &description, &blob);
2045 if (ret != 0) {
2046 printf("sys_acl_blob_get_fd failed (%s)\n", strerror(errno));
2047 return map_nt_error_from_unix(errno);
2049 printf("Description: %s\n", description);
2050 for (i = 0; i < blob.length; i++) {
2051 printf("%.2x ", blob.data[i]);
2053 printf("\n");
2055 return NT_STATUS_OK;
2060 static NTSTATUS cmd_sys_acl_delete_def_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
2061 int argc, const char **argv)
2063 int ret;
2064 struct smb_filename *smb_fname = NULL;
2065 struct smb_filename *pathref_fname = NULL;
2066 NTSTATUS status;
2068 if (argc != 2) {
2069 printf("Usage: sys_acl_delete_def_file <path>\n");
2070 return NT_STATUS_OK;
2073 smb_fname = synthetic_smb_fname_split(mem_ctx,
2074 argv[1],
2075 lp_posix_pathnames());
2076 if (smb_fname == NULL) {
2077 return NT_STATUS_NO_MEMORY;
2079 status = synthetic_pathref(mem_ctx,
2080 vfs->conn->cwd_fsp,
2081 smb_fname->base_name,
2082 NULL,
2083 NULL,
2084 smb_fname->twrp,
2085 smb_fname->flags,
2086 &pathref_fname);
2087 if (!NT_STATUS_IS_OK(status)) {
2088 TALLOC_FREE(smb_fname);
2089 return status;
2091 if (!pathref_fname->fsp->fsp_flags.is_directory) {
2092 printf("sys_acl_delete_def_file - %s is not a directory\n",
2093 smb_fname->base_name);
2094 TALLOC_FREE(smb_fname);
2095 TALLOC_FREE(pathref_fname);
2096 return NT_STATUS_INVALID_PARAMETER;
2098 ret = SMB_VFS_SYS_ACL_DELETE_DEF_FD(pathref_fname->fsp);
2099 if (ret == -1) {
2100 int err = errno;
2101 printf("sys_acl_delete_def_file failed (%s)\n", strerror(err));
2102 TALLOC_FREE(smb_fname);
2103 TALLOC_FREE(pathref_fname);
2104 return map_nt_error_from_unix(err);
2106 TALLOC_FREE(smb_fname);
2107 TALLOC_FREE(pathref_fname);
2108 return NT_STATUS_OK;
2111 /* Afaik translate name was first introduced with vfs_catia, to be able
2112 to translate unix file/dir-names, containing invalid windows characters,
2113 to valid windows names.
2114 The used translation direction is always unix --> windows
2116 static NTSTATUS cmd_translate_name(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
2117 int argc, const char **argv)
2119 const char *dname = NULL;
2120 char *dname_talloced = NULL;
2121 bool found = false;
2122 char *translated = NULL;
2123 struct smb_filename *smb_fname = NULL;
2124 NTSTATUS status;
2126 if (argc != 2) {
2127 DEBUG(0, ("Usage: translate_name unix_filename\n"));
2128 return NT_STATUS_UNSUCCESSFUL;
2131 smb_fname = synthetic_smb_fname(talloc_tos(),
2132 ".",
2133 NULL,
2134 NULL,
2136 ssf_flags());
2137 if (smb_fname == NULL) {
2138 return NT_STATUS_NO_MEMORY;
2141 status = OpenDir(vfs->conn,
2142 vfs->conn,
2143 smb_fname,
2144 NULL,
2146 &vfs->currentdir);
2147 if (!NT_STATUS_IS_OK(status)) {
2148 int err = map_errno_from_nt_status(status);
2149 DEBUG(0, ("cmd_translate_name: opendir error=%d (%s)\n",
2150 err, strerror(err)));
2151 TALLOC_FREE(smb_fname);
2152 errno = err;
2153 return NT_STATUS_UNSUCCESSFUL;
2156 while (true) {
2157 /* ReadDirName() returns Windows "encoding" */
2158 dname = ReadDirName(vfs->currentdir, &dname_talloced);
2159 if (dname == NULL) {
2160 break;
2163 /* Convert Windows "encoding" from ReadDirName() to UNIX */
2164 status = SMB_VFS_TRANSLATE_NAME(vfs->conn,
2165 dname,
2166 vfs_translate_to_unix,
2167 talloc_tos(),
2168 &translated);
2169 if (!NT_STATUS_IS_OK(status)) {
2170 DBG_ERR("file '%s' cannot be translated\n", argv[1]);
2171 goto cleanup;
2175 * argv[1] uses UNIX "encoding", so compare with translation
2176 * result.
2178 if (strcmp(translated, argv[1]) == 0) {
2179 found = true;
2180 break;
2182 TALLOC_FREE(dname_talloced);
2183 TALLOC_FREE(translated);
2186 if (!found) {
2187 DEBUG(0, ("cmd_translate_name: file '%s' not found.\n",
2188 argv[1]));
2189 status = NT_STATUS_UNSUCCESSFUL;
2190 goto cleanup;
2193 /* translation success. But that could also mean
2194 that translating "aaa" to "aaa" was successful :-(
2196 DBG_ERR("file '%s' --> '%s'\n", argv[1], dname);
2197 status = NT_STATUS_OK;
2199 cleanup:
2200 TALLOC_FREE(dname_talloced);
2201 TALLOC_FREE(translated);
2202 TALLOC_FREE(smb_fname);
2203 TALLOC_FREE(vfs->currentdir);
2204 return status;
2208 * This is a quick hack to demonstrate a crash in the full_audit
2209 * module when passing fsp->smb_fname into SMB_VFS_CREATE_FILE leading
2210 * to an error.
2212 * Feel free to expand with more options as needed
2214 static NTSTATUS cmd_create_file(
2215 struct vfs_state *vfs,
2216 TALLOC_CTX *mem_ctx,
2217 int argc,
2218 const char **argv)
2220 struct smb_filename *fname = NULL;
2221 struct files_struct *fsp = NULL;
2222 int info, ret;
2223 NTSTATUS status;
2225 if (argc != 2) {
2226 DBG_ERR("Usage: create_file filename\n");
2227 return NT_STATUS_UNSUCCESSFUL;
2230 fname = synthetic_smb_fname(
2231 talloc_tos(), argv[1], NULL, NULL, 0, 0);
2232 if (fname == NULL) {
2233 return NT_STATUS_NO_MEMORY;
2236 ret = vfs_stat(vfs->conn, fname);
2237 if (ret != 0) {
2238 status = map_nt_error_from_unix(errno);
2239 DBG_DEBUG("vfs_stat() failed: %s\n", strerror(errno));
2240 TALLOC_FREE(fname);
2241 return status;
2244 status = openat_pathref_fsp(vfs->conn->cwd_fsp, fname);
2245 if (!NT_STATUS_IS_OK(status)) {
2246 DBG_DEBUG("Could not open %s: %s\n",
2247 fname->base_name,
2248 nt_errstr(status));
2249 TALLOC_FREE(fname);
2250 return status;
2253 status = SMB_VFS_CREATE_FILE(
2254 vfs->conn,
2255 NULL,
2256 NULL,
2259 * Using fname->fsp->fsp_name seems to be legal,
2260 * there's code to handle this in
2261 * create_file_unixpath(). And it is actually very
2262 * worthwhile re-using the fsp_name, we can save quite
2263 * a few copies of smb_filename with that.
2265 fname->fsp->fsp_name,
2266 SEC_FILE_ALL,
2267 FILE_SHARE_NONE,
2268 FILE_OPEN,
2269 FILE_NON_DIRECTORY_FILE,
2272 NULL,
2275 NULL,
2276 NULL,
2277 &fsp,
2278 &info,
2279 NULL,
2280 NULL
2282 DBG_DEBUG("create_file returned %s\n", nt_errstr(status));
2284 TALLOC_FREE(fname);
2286 return NT_STATUS_OK;
2289 struct cmd_set vfs_commands[] = {
2291 { .name = "VFS Commands" },
2293 { "load", cmd_load_module, "Load a module", "load <module.so>" },
2294 { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
2295 { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
2296 { "connect", cmd_connect, "VFS connect()", "connect" },
2297 { "disconnect", cmd_disconnect, "VFS disconnect()", "disconnect" },
2298 { "disk_free", cmd_disk_free, "VFS disk_free()", "disk_free <path>" },
2299 { "opendir", cmd_opendir, "VFS opendir()", "opendir <fname>" },
2300 { "readdir", cmd_readdir, "VFS readdir()", "readdir" },
2301 { "mkdir", cmd_mkdir, "VFS mkdir()", "mkdir <path>" },
2302 { "rmdir", cmd_pathfunc, "VFS rmdir()", "rmdir <path>" },
2303 { "closedir", cmd_closedir, "VFS closedir()", "closedir" },
2304 { "open", cmd_open, "VFS open()", "open <fname> <flags> <mode>" },
2305 { "close", cmd_close, "VFS close()", "close <fd>" },
2306 { "read", cmd_read, "VFS read()", "read <fd> <size>" },
2307 { "write", cmd_write, "VFS write()", "write <fd> <size>" },
2308 { "lseek", cmd_lseek, "VFS lseek()", "lseek <fd> <offset> <whence>" },
2309 { "rename", cmd_rename, "VFS rename()", "rename <old> <new>" },
2310 { "fsync", cmd_fsync, "VFS fsync()", "fsync <fd>" },
2311 { "stat", cmd_stat, "VFS stat()", "stat <fname>" },
2312 { "fstat", cmd_fstat, "VFS fstat()", "fstat <fd>" },
2313 { "lstat", cmd_lstat, "VFS lstat()", "lstat <fname>" },
2314 { "unlink", cmd_pathfunc, "VFS unlink()", "unlink <fname>" },
2315 { "chmod", cmd_chmod, "VFS chmod()", "chmod <path> <mode>" },
2316 { "fchmod", cmd_fchmod, "VFS fchmod()", "fchmod <fd> <mode>" },
2317 { "fchown", cmd_fchown, "VFS fchown()", "fchown <fd> <uid> <gid>" },
2318 { "chdir", cmd_pathfunc, "VFS chdir()", "chdir <path>" },
2319 { "getwd", cmd_getwd, "VFS getwd()", "getwd" },
2320 { "utime", cmd_utime, "VFS utime()", "utime <path> <access> <modify>" },
2321 { "ftruncate", cmd_ftruncate, "VFS ftruncate()", "ftruncate <fd> <length>" },
2322 { "lock", cmd_lock, "VFS lock()", "lock <f> <op> <offset> <count> <type>" },
2323 { "symlink", cmd_symlink, "VFS symlink()", "symlink <old> <new>" },
2324 { "readlink", cmd_readlink, "VFS readlink()", "readlink <path>" },
2325 { "link", cmd_link, "VFS link()", "link <oldpath> <newpath>" },
2326 { "mknod", cmd_mknod, "VFS mknod()", "mknod <path> <mode> <dev>" },
2327 { "realpath", cmd_realpath, "VFS realpath()", "realpath <path>" },
2328 { "getxattr", cmd_getxattr, "VFS getxattr()",
2329 "getxattr <path> <name>" },
2330 { "listxattr", cmd_listxattr, "VFS listxattr()",
2331 "listxattr <path>" },
2332 { "fsetxattr", cmd_fsetxattr, "VFS fsetxattr()",
2333 "fsetxattr <path> <name> <value> [<flags>]" },
2334 { "removexattr", cmd_removexattr, "VFS removexattr()",
2335 "removexattr <path> <name>\n" },
2336 { "fget_nt_acl", cmd_fget_nt_acl, "VFS fget_nt_acl()",
2337 "fget_nt_acl <fd>\n" },
2338 { "get_nt_acl", cmd_get_nt_acl, "VFS get_nt_acl()",
2339 "get_nt_acl <path>\n" },
2340 { "fset_nt_acl", cmd_fset_nt_acl, "VFS fset_nt_acl()",
2341 "fset_nt_acl <fd>\n" },
2342 { "set_nt_acl", cmd_set_nt_acl, "VFS open() and fset_nt_acl()",
2343 "set_nt_acl <file>\n" },
2344 { "sys_acl_get_file", cmd_sys_acl_get_file, "VFS sys_acl_get_file()", "sys_acl_get_file <path>" },
2345 { "sys_acl_get_fd", cmd_sys_acl_get_fd, "VFS sys_acl_get_fd()", "sys_acl_get_fd <fd>" },
2346 { "sys_acl_blob_get_file", cmd_sys_acl_blob_get_file,
2347 "VFS sys_acl_blob_get_file()", "sys_acl_blob_get_file <path>" },
2348 { "sys_acl_blob_get_fd", cmd_sys_acl_blob_get_fd,
2349 "VFS sys_acl_blob_get_fd()", "sys_acl_blob_get_fd <path>" },
2350 { "sys_acl_delete_def_file", cmd_sys_acl_delete_def_file, "VFS sys_acl_delete_def_file()", "sys_acl_delete_def_file <path>" },
2353 #if defined(WITH_SMB1SERVER)
2354 { "test_chain", cmd_test_chain, "test chain code",
2355 "test_chain" },
2356 #endif
2357 { "translate_name", cmd_translate_name, "VFS translate_name()", "translate_name unix_filename" },
2358 { "create_file",
2359 cmd_create_file,
2360 "VFS create_file()",
2361 "create_file <filename>"