s3:libsmb/smb_seal: always use SAFE_FREE(buf) in common_free_enc_buffer()
[Samba.git] / source3 / torture / cmd_vfs.c
blob9527a34af4db5350acdaa70019b1ecae8ac7fdbf
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"
29 static const char *null_string = "";
31 static NTSTATUS cmd_load_module(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
33 int i;
35 if (argc < 2) {
36 printf("Usage: load <modules>\n");
37 return NT_STATUS_OK;
40 for (i=argc-1;i>0;i--) {
41 if (!vfs_init_custom(vfs->conn, argv[i])) {
42 DEBUG(0, ("load: (vfs_init_custom failed for %s)\n", argv[i]));
43 return NT_STATUS_UNSUCCESSFUL;
46 printf("load: ok\n");
47 return NT_STATUS_OK;
50 static NTSTATUS cmd_populate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
52 char c;
53 size_t size;
54 if (argc != 3) {
55 printf("Usage: populate <char> <size>\n");
56 return NT_STATUS_OK;
58 c = argv[1][0];
59 size = atoi(argv[2]);
60 vfs->data = talloc_array(mem_ctx, char, size);
61 if (vfs->data == NULL) {
62 printf("populate: error=-1 (not enough memory)");
63 return NT_STATUS_UNSUCCESSFUL;
65 memset(vfs->data, c, size);
66 vfs->data_size = size;
67 return NT_STATUS_OK;
70 static NTSTATUS cmd_show_data(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
72 size_t offset;
73 size_t len;
74 if (argc != 1 && argc != 3) {
75 printf("Usage: showdata [<offset> <len>]\n");
76 return NT_STATUS_OK;
78 if (vfs->data == NULL || vfs->data_size == 0) {
79 printf("show_data: error=-1 (buffer empty)\n");
80 return NT_STATUS_UNSUCCESSFUL;
83 if (argc == 3) {
84 offset = atoi(argv[1]);
85 len = atoi(argv[2]);
86 } else {
87 offset = 0;
88 len = vfs->data_size;
90 if ((offset + len) > vfs->data_size) {
91 printf("show_data: error=-1 (not enough data in buffer)\n");
92 return NT_STATUS_UNSUCCESSFUL;
94 dump_data(0, (uint8 *)(vfs->data) + offset, len);
95 return NT_STATUS_OK;
98 static NTSTATUS cmd_connect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
100 SMB_VFS_CONNECT(vfs->conn, lp_servicename(SNUM(vfs->conn)), "vfstest");
101 return NT_STATUS_OK;
104 static NTSTATUS cmd_disconnect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
106 SMB_VFS_DISCONNECT(vfs->conn);
107 return NT_STATUS_OK;
110 static NTSTATUS cmd_disk_free(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
112 uint64_t diskfree, bsize, dfree, dsize;
113 if (argc != 2) {
114 printf("Usage: disk_free <path>\n");
115 return NT_STATUS_OK;
118 diskfree = SMB_VFS_DISK_FREE(vfs->conn, argv[1], False, &bsize, &dfree, &dsize);
119 printf("disk_free: %lu, bsize = %lu, dfree = %lu, dsize = %lu\n",
120 (unsigned long)diskfree,
121 (unsigned long)bsize,
122 (unsigned long)dfree,
123 (unsigned long)dsize);
124 return NT_STATUS_OK;
128 static NTSTATUS cmd_opendir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
130 if (argc != 2) {
131 printf("Usage: opendir <fname>\n");
132 return NT_STATUS_OK;
135 vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, argv[1], NULL, 0);
136 if (vfs->currentdir == NULL) {
137 printf("opendir error=%d (%s)\n", errno, strerror(errno));
138 return NT_STATUS_UNSUCCESSFUL;
141 printf("opendir: ok\n");
142 return NT_STATUS_OK;
146 static NTSTATUS cmd_readdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
148 SMB_STRUCT_STAT st;
149 SMB_STRUCT_DIRENT *dent = NULL;
151 if (vfs->currentdir == NULL) {
152 printf("readdir: error=-1 (no open directory)\n");
153 return NT_STATUS_UNSUCCESSFUL;
156 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir, &st);
157 if (dent == NULL) {
158 printf("readdir: NULL\n");
159 return NT_STATUS_OK;
162 printf("readdir: %s\n", dent->d_name);
163 if (VALID_STAT(st)) {
164 time_t tmp_time;
165 printf(" stat available");
166 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
167 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
168 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
169 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
170 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
171 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
172 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
173 printf(" Size: %10u", (unsigned int)st.st_ex_size);
174 #ifdef HAVE_STAT_ST_BLOCKS
175 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
176 #endif
177 #ifdef HAVE_STAT_ST_BLKSIZE
178 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
179 #endif
180 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
181 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
182 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
183 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
184 printf(" Uid: %5lu Gid: %5lu\n",
185 (unsigned long)st.st_ex_uid,
186 (unsigned long)st.st_ex_gid);
187 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
188 printf(" Access: %s", ctime(&tmp_time));
189 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
190 printf(" Modify: %s", ctime(&tmp_time));
191 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
192 printf(" Change: %s", ctime(&tmp_time));
195 return NT_STATUS_OK;
199 static NTSTATUS cmd_mkdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
201 if (argc != 2) {
202 printf("Usage: mkdir <path>\n");
203 return NT_STATUS_OK;
206 if (SMB_VFS_MKDIR(vfs->conn, argv[1], 00755) == -1) {
207 printf("mkdir error=%d (%s)\n", errno, strerror(errno));
208 return NT_STATUS_UNSUCCESSFUL;
211 printf("mkdir: ok\n");
212 return NT_STATUS_OK;
216 static NTSTATUS cmd_closedir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
218 int ret;
220 if (vfs->currentdir == NULL) {
221 printf("closedir: failure (no directory open)\n");
222 return NT_STATUS_UNSUCCESSFUL;
225 ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
226 if (ret == -1) {
227 printf("closedir failure: %s\n", strerror(errno));
228 return NT_STATUS_UNSUCCESSFUL;
231 printf("closedir: ok\n");
232 vfs->currentdir = NULL;
233 return NT_STATUS_OK;
237 static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
239 int flags;
240 mode_t mode;
241 const char *flagstr;
242 files_struct *fsp;
243 struct smb_filename *smb_fname = NULL;
244 NTSTATUS status;
246 mode = 00400;
248 if (argc < 3 || argc > 5) {
249 printf("Usage: open <filename> <flags> <mode>\n");
250 printf(" flags: O = O_RDONLY\n");
251 printf(" R = O_RDWR\n");
252 printf(" W = O_WRONLY\n");
253 printf(" C = O_CREAT\n");
254 printf(" E = O_EXCL\n");
255 printf(" T = O_TRUNC\n");
256 printf(" A = O_APPEND\n");
257 printf(" N = O_NONBLOCK/O_NDELAY\n");
258 #ifdef O_SYNC
259 printf(" S = O_SYNC\n");
260 #endif
261 #ifdef O_NOFOLLOW
262 printf(" F = O_NOFOLLOW\n");
263 #endif
264 printf(" mode: see open.2\n");
265 printf(" mode is ignored if C flag not present\n");
266 printf(" mode defaults to 00400\n");
267 return NT_STATUS_OK;
269 flags = 0;
270 flagstr = argv[2];
271 while (*flagstr) {
272 switch (*flagstr) {
273 case 'O':
274 flags |= O_RDONLY;
275 break;
276 case 'R':
277 flags |= O_RDWR;
278 break;
279 case 'W':
280 flags |= O_WRONLY;
281 break;
282 case 'C':
283 flags |= O_CREAT;
284 break;
285 case 'E':
286 flags |= O_EXCL;
287 break;
288 case 'T':
289 flags |= O_TRUNC;
290 break;
291 case 'A':
292 flags |= O_APPEND;
293 break;
294 case 'N':
295 flags |= O_NONBLOCK;
296 break;
297 #ifdef O_SYNC
298 case 'S':
299 flags |= O_SYNC;
300 break;
301 #endif
302 #ifdef O_NOFOLLOW
303 case 'F':
304 flags |= O_NOFOLLOW;
305 break;
306 #endif
307 default:
308 printf("open: error=-1 (invalid flag!)\n");
309 return NT_STATUS_UNSUCCESSFUL;
311 flagstr++;
313 if ((flags & O_CREAT) && argc == 4) {
314 if (sscanf(argv[3], "%ho", (unsigned short *)&mode) == 0) {
315 printf("open: error=-1 (invalid mode!)\n");
316 return NT_STATUS_UNSUCCESSFUL;
320 fsp = SMB_MALLOC_P(struct files_struct);
321 if (fsp == NULL) {
322 return NT_STATUS_NO_MEMORY;
324 fsp->fh = SMB_MALLOC_P(struct fd_handle);
325 if (fsp->fh == NULL) {
326 SAFE_FREE(fsp->fsp_name);
327 SAFE_FREE(fsp);
328 return NT_STATUS_NO_MEMORY;
330 fsp->conn = vfs->conn;
332 status = create_synthetic_smb_fname_split(mem_ctx, argv[1], NULL,
333 &smb_fname);
334 if (!NT_STATUS_IS_OK(status)) {
335 SAFE_FREE(fsp);
336 return status;
339 fsp->fsp_name = smb_fname;
341 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, flags, mode);
342 if (fsp->fh->fd == -1) {
343 printf("open: error=%d (%s)\n", errno, strerror(errno));
344 SAFE_FREE(fsp->fh);
345 SAFE_FREE(fsp);
346 TALLOC_FREE(smb_fname);
347 return NT_STATUS_UNSUCCESSFUL;
350 vfs->files[fsp->fh->fd] = fsp;
351 printf("open: fd=%d\n", fsp->fh->fd);
352 return NT_STATUS_OK;
356 static NTSTATUS cmd_pathfunc(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
358 int ret = -1;
360 if (argc != 2) {
361 printf("Usage: %s <path>\n", argv[0]);
362 return NT_STATUS_OK;
365 if (strcmp("rmdir", argv[0]) == 0 ) {
366 ret = SMB_VFS_RMDIR(vfs->conn, argv[1]);
367 } else if (strcmp("unlink", argv[0]) == 0 ) {
368 struct smb_filename *smb_fname = NULL;
369 NTSTATUS status;
371 status = create_synthetic_smb_fname_split(mem_ctx, argv[1],
372 NULL, &smb_fname);
373 if (!NT_STATUS_IS_OK(status)) {
374 return status;
377 ret = SMB_VFS_UNLINK(vfs->conn, smb_fname);
378 TALLOC_FREE(smb_fname);
379 } else if (strcmp("chdir", argv[0]) == 0 ) {
380 ret = SMB_VFS_CHDIR(vfs->conn, argv[1]);
381 } else {
382 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
383 return NT_STATUS_UNSUCCESSFUL;
386 if (ret == -1) {
387 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
388 return NT_STATUS_UNSUCCESSFUL;
391 printf("%s: ok\n", argv[0]);
392 return NT_STATUS_OK;
396 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
398 int fd, ret;
400 if (argc != 2) {
401 printf("Usage: close <fd>\n");
402 return NT_STATUS_OK;
405 fd = atoi(argv[1]);
406 if (vfs->files[fd] == NULL) {
407 printf("close: error=-1 (invalid file descriptor)\n");
408 return NT_STATUS_OK;
411 ret = SMB_VFS_CLOSE(vfs->files[fd]);
412 if (ret == -1 )
413 printf("close: error=%d (%s)\n", errno, strerror(errno));
414 else
415 printf("close: ok\n");
417 TALLOC_FREE(vfs->files[fd]->fsp_name);
418 SAFE_FREE(vfs->files[fd]->fh);
419 SAFE_FREE(vfs->files[fd]);
420 vfs->files[fd] = NULL;
421 return NT_STATUS_OK;
425 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
427 int fd;
428 size_t size, rsize;
430 if (argc != 3) {
431 printf("Usage: read <fd> <size>\n");
432 return NT_STATUS_OK;
435 /* do some error checking on these */
436 fd = atoi(argv[1]);
437 size = atoi(argv[2]);
438 vfs->data = talloc_array(mem_ctx, char, size);
439 if (vfs->data == NULL) {
440 printf("read: error=-1 (not enough memory)");
441 return NT_STATUS_UNSUCCESSFUL;
443 vfs->data_size = size;
445 rsize = SMB_VFS_READ(vfs->files[fd], vfs->data, size);
446 if (rsize == -1) {
447 printf("read: error=%d (%s)\n", errno, strerror(errno));
448 return NT_STATUS_UNSUCCESSFUL;
451 printf("read: ok\n");
452 return NT_STATUS_OK;
456 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
458 int fd, size, wsize;
460 if (argc != 3) {
461 printf("Usage: write <fd> <size>\n");
462 return NT_STATUS_OK;
465 /* some error checking should go here */
466 fd = atoi(argv[1]);
467 size = atoi(argv[2]);
468 if (vfs->data == NULL) {
469 printf("write: error=-1 (buffer empty, please populate it before writing)");
470 return NT_STATUS_UNSUCCESSFUL;
473 if (vfs->data_size < size) {
474 printf("write: error=-1 (buffer too small, please put some more data in)");
475 return NT_STATUS_UNSUCCESSFUL;
478 wsize = SMB_VFS_WRITE(vfs->files[fd], vfs->data, size);
480 if (wsize == -1) {
481 printf("write: error=%d (%s)\n", errno, strerror(errno));
482 return NT_STATUS_UNSUCCESSFUL;
485 printf("write: ok\n");
486 return NT_STATUS_OK;
490 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
492 int fd, offset, whence;
493 SMB_OFF_T pos;
495 if (argc != 4) {
496 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
497 return NT_STATUS_OK;
500 fd = atoi(argv[1]);
501 offset = atoi(argv[2]);
502 whence = atoi(argv[3]);
503 switch (whence) {
504 case 1: whence = SEEK_SET; break;
505 case 2: whence = SEEK_CUR; break;
506 default: whence = SEEK_END;
509 pos = SMB_VFS_LSEEK(vfs->files[fd], offset, whence);
510 if (pos == (SMB_OFF_T)-1) {
511 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
512 return NT_STATUS_UNSUCCESSFUL;
515 printf("lseek: ok\n");
516 return NT_STATUS_OK;
520 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
522 int ret;
523 struct smb_filename *smb_fname_src = NULL;
524 struct smb_filename *smb_fname_dst = NULL;
525 NTSTATUS status;
527 if (argc != 3) {
528 printf("Usage: rename <old> <new>\n");
529 return NT_STATUS_OK;
532 status = create_synthetic_smb_fname_split(mem_ctx, argv[1], NULL,
533 &smb_fname_src);
534 if (!NT_STATUS_IS_OK(status)) {
535 return status;
538 status = create_synthetic_smb_fname_split(mem_ctx, argv[2], NULL,
539 &smb_fname_dst);
540 if (!NT_STATUS_IS_OK(status)) {
541 TALLOC_FREE(smb_fname_src);
542 return status;
545 ret = SMB_VFS_RENAME(vfs->conn, smb_fname_src, smb_fname_dst);
546 TALLOC_FREE(smb_fname_src);
547 TALLOC_FREE(smb_fname_dst);
548 if (ret == -1) {
549 printf("rename: error=%d (%s)\n", errno, strerror(errno));
550 return NT_STATUS_UNSUCCESSFUL;
553 printf("rename: ok\n");
554 return NT_STATUS_OK;
558 static NTSTATUS cmd_fsync(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
560 int ret, fd;
561 if (argc != 2) {
562 printf("Usage: fsync <fd>\n");
563 return NT_STATUS_OK;
566 fd = atoi(argv[1]);
567 ret = SMB_VFS_FSYNC(vfs->files[fd]);
568 if (ret == -1) {
569 printf("fsync: error=%d (%s)\n", errno, strerror(errno));
570 return NT_STATUS_UNSUCCESSFUL;
573 printf("fsync: ok\n");
574 return NT_STATUS_OK;
578 static NTSTATUS cmd_stat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
580 int ret;
581 const char *user;
582 const char *group;
583 struct passwd *pwd = NULL;
584 struct group *grp = NULL;
585 struct smb_filename *smb_fname = NULL;
586 SMB_STRUCT_STAT st;
587 time_t tmp_time;
588 NTSTATUS status;
590 if (argc != 2) {
591 printf("Usage: stat <fname>\n");
592 return NT_STATUS_OK;
595 status = create_synthetic_smb_fname_split(mem_ctx, argv[1], NULL,
596 &smb_fname);
597 if (!NT_STATUS_IS_OK(status)) {
598 return status;
601 ret = SMB_VFS_STAT(vfs->conn, smb_fname);
602 if (ret == -1) {
603 printf("stat: error=%d (%s)\n", errno, strerror(errno));
604 TALLOC_FREE(smb_fname);
605 return NT_STATUS_UNSUCCESSFUL;
607 st = smb_fname->st;
608 TALLOC_FREE(smb_fname);
610 pwd = sys_getpwuid(st.st_ex_uid);
611 if (pwd != NULL) user = pwd->pw_name;
612 else user = null_string;
613 grp = sys_getgrgid(st.st_ex_gid);
614 if (grp != NULL) group = grp->gr_name;
615 else group = null_string;
617 printf("stat: ok\n");
618 printf(" File: %s", argv[1]);
619 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
620 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
621 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
622 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
623 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
624 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
625 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
626 printf(" Size: %10u", (unsigned int)st.st_ex_size);
627 #ifdef HAVE_STAT_ST_BLOCKS
628 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
629 #endif
630 #ifdef HAVE_STAT_ST_BLKSIZE
631 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
632 #endif
633 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
634 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
635 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
636 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
637 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
638 (unsigned long)st.st_ex_gid, group);
639 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
640 printf(" Access: %s", ctime(&tmp_time));
641 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
642 printf(" Modify: %s", ctime(&tmp_time));
643 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
644 printf(" Change: %s", ctime(&tmp_time));
646 return NT_STATUS_OK;
650 static NTSTATUS cmd_fstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
652 int fd;
653 const char *user;
654 const char *group;
655 struct passwd *pwd = NULL;
656 struct group *grp = NULL;
657 SMB_STRUCT_STAT st;
658 time_t tmp_time;
660 if (argc != 2) {
661 printf("Usage: fstat <fd>\n");
662 return NT_STATUS_OK;
665 fd = atoi(argv[1]);
666 if (fd < 0 || fd >= 1024) {
667 printf("fstat: error=%d (file descriptor out of range)\n", EBADF);
668 return NT_STATUS_OK;
671 if (vfs->files[fd] == NULL) {
672 printf("fstat: error=%d (invalid file descriptor)\n", EBADF);
673 return NT_STATUS_OK;
676 if (SMB_VFS_FSTAT(vfs->files[fd], &st) == -1) {
677 printf("fstat: error=%d (%s)\n", errno, strerror(errno));
678 return NT_STATUS_UNSUCCESSFUL;
681 pwd = sys_getpwuid(st.st_ex_uid);
682 if (pwd != NULL) user = pwd->pw_name;
683 else user = null_string;
684 grp = sys_getgrgid(st.st_ex_gid);
685 if (grp != NULL) group = grp->gr_name;
686 else group = null_string;
688 printf("fstat: ok\n");
689 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
690 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
691 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
692 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
693 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
694 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
695 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
696 printf(" Size: %10u", (unsigned int)st.st_ex_size);
697 #ifdef HAVE_STAT_ST_BLOCKS
698 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
699 #endif
700 #ifdef HAVE_STAT_ST_BLKSIZE
701 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
702 #endif
703 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
704 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
705 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
706 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
707 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
708 (unsigned long)st.st_ex_gid, group);
709 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
710 printf(" Access: %s", ctime(&tmp_time));
711 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
712 printf(" Modify: %s", ctime(&tmp_time));
713 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
714 printf(" Change: %s", ctime(&tmp_time));
716 return NT_STATUS_OK;
720 static NTSTATUS cmd_lstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
722 const char *user;
723 const char *group;
724 struct passwd *pwd = NULL;
725 struct group *grp = NULL;
726 struct smb_filename *smb_fname = NULL;
727 SMB_STRUCT_STAT st;
728 time_t tmp_time;
729 NTSTATUS status;
731 if (argc != 2) {
732 printf("Usage: lstat <path>\n");
733 return NT_STATUS_OK;
736 status = create_synthetic_smb_fname_split(mem_ctx, argv[1], NULL,
737 &smb_fname);
738 if (!NT_STATUS_IS_OK(status)) {
739 return status;
742 if (SMB_VFS_LSTAT(vfs->conn, smb_fname) == -1) {
743 printf("lstat: error=%d (%s)\n", errno, strerror(errno));
744 TALLOC_FREE(smb_fname);
745 return NT_STATUS_UNSUCCESSFUL;
747 st = smb_fname->st;
748 TALLOC_FREE(smb_fname);
750 pwd = sys_getpwuid(st.st_ex_uid);
751 if (pwd != NULL) user = pwd->pw_name;
752 else user = null_string;
753 grp = sys_getgrgid(st.st_ex_gid);
754 if (grp != NULL) group = grp->gr_name;
755 else group = null_string;
757 printf("lstat: ok\n");
758 if (S_ISREG(st.st_ex_mode)) printf(" Regular File\n");
759 else if (S_ISDIR(st.st_ex_mode)) printf(" Directory\n");
760 else if (S_ISCHR(st.st_ex_mode)) printf(" Character Device\n");
761 else if (S_ISBLK(st.st_ex_mode)) printf(" Block Device\n");
762 else if (S_ISFIFO(st.st_ex_mode)) printf(" Fifo\n");
763 else if (S_ISLNK(st.st_ex_mode)) printf(" Symbolic Link\n");
764 else if (S_ISSOCK(st.st_ex_mode)) printf(" Socket\n");
765 printf(" Size: %10u", (unsigned int)st.st_ex_size);
766 #ifdef HAVE_STAT_ST_BLOCKS
767 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
768 #endif
769 #ifdef HAVE_STAT_ST_BLKSIZE
770 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
771 #endif
772 printf(" Device: 0x%10x", (unsigned int)st.st_ex_dev);
773 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
774 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
775 printf(" Access: %05o", (int)((st.st_ex_mode) & 007777));
776 printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
777 (unsigned long)st.st_ex_gid, group);
778 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
779 printf(" Access: %s", ctime(&tmp_time));
780 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
781 printf(" Modify: %s", ctime(&tmp_time));
782 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
783 printf(" Change: %s", ctime(&tmp_time));
785 return NT_STATUS_OK;
789 static NTSTATUS cmd_chmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
791 mode_t mode;
792 if (argc != 3) {
793 printf("Usage: chmod <path> <mode>\n");
794 return NT_STATUS_OK;
797 mode = atoi(argv[2]);
798 if (SMB_VFS_CHMOD(vfs->conn, argv[1], mode) == -1) {
799 printf("chmod: error=%d (%s)\n", errno, strerror(errno));
800 return NT_STATUS_UNSUCCESSFUL;
803 printf("chmod: ok\n");
804 return NT_STATUS_OK;
808 static NTSTATUS cmd_fchmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
810 int fd;
811 mode_t mode;
812 if (argc != 3) {
813 printf("Usage: fchmod <fd> <mode>\n");
814 return NT_STATUS_OK;
817 fd = atoi(argv[1]);
818 mode = atoi(argv[2]);
819 if (fd < 0 || fd >= 1024) {
820 printf("fchmod: error=%d (file descriptor out of range)\n", EBADF);
821 return NT_STATUS_OK;
823 if (vfs->files[fd] == NULL) {
824 printf("fchmod: error=%d (invalid file descriptor)\n", EBADF);
825 return NT_STATUS_OK;
828 if (SMB_VFS_FCHMOD(vfs->files[fd], mode) == -1) {
829 printf("fchmod: error=%d (%s)\n", errno, strerror(errno));
830 return NT_STATUS_UNSUCCESSFUL;
833 printf("fchmod: ok\n");
834 return NT_STATUS_OK;
838 static NTSTATUS cmd_chown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
840 uid_t uid;
841 gid_t gid;
842 if (argc != 4) {
843 printf("Usage: chown <path> <uid> <gid>\n");
844 return NT_STATUS_OK;
847 uid = atoi(argv[2]);
848 gid = atoi(argv[3]);
849 if (SMB_VFS_CHOWN(vfs->conn, argv[1], uid, gid) == -1) {
850 printf("chown: error=%d (%s)\n", errno, strerror(errno));
851 return NT_STATUS_UNSUCCESSFUL;
854 printf("chown: ok\n");
855 return NT_STATUS_OK;
859 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
861 uid_t uid;
862 gid_t gid;
863 int fd;
864 if (argc != 4) {
865 printf("Usage: fchown <fd> <uid> <gid>\n");
866 return NT_STATUS_OK;
869 uid = atoi(argv[2]);
870 gid = atoi(argv[3]);
871 fd = atoi(argv[1]);
872 if (fd < 0 || fd >= 1024) {
873 printf("fchown: faliure=%d (file descriptor out of range)\n", EBADF);
874 return NT_STATUS_OK;
876 if (vfs->files[fd] == NULL) {
877 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
878 return NT_STATUS_OK;
880 if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
881 printf("fchown error=%d (%s)\n", errno, strerror(errno));
882 return NT_STATUS_UNSUCCESSFUL;
885 printf("fchown: ok\n");
886 return NT_STATUS_OK;
890 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
892 char *buf = SMB_VFS_GETWD(vfs->conn);
893 if (buf == NULL) {
894 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
895 return NT_STATUS_UNSUCCESSFUL;
898 printf("getwd: %s\n", buf);
899 SAFE_FREE(buf);
900 return NT_STATUS_OK;
903 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
905 struct smb_file_time ft;
906 struct smb_filename *smb_fname = NULL;
907 NTSTATUS status;
909 if (argc != 4) {
910 printf("Usage: utime <path> <access> <modify>\n");
911 return NT_STATUS_OK;
914 ZERO_STRUCT(ft);
916 ft.atime = convert_time_t_to_timespec(atoi(argv[2]));
917 ft.mtime = convert_time_t_to_timespec(atoi(argv[3]));
919 status = create_synthetic_smb_fname_split(mem_ctx, argv[1],
920 NULL, &smb_fname);
921 if (!NT_STATUS_IS_OK(status)) {
922 return status;
925 if (SMB_VFS_NTIMES(vfs->conn, smb_fname, &ft) != 0) {
926 printf("utime: error=%d (%s)\n", errno, strerror(errno));
927 TALLOC_FREE(smb_fname);
928 return NT_STATUS_UNSUCCESSFUL;
931 TALLOC_FREE(smb_fname);
932 printf("utime: ok\n");
933 return NT_STATUS_OK;
936 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
938 int fd;
939 SMB_OFF_T off;
940 if (argc != 3) {
941 printf("Usage: ftruncate <fd> <length>\n");
942 return NT_STATUS_OK;
945 fd = atoi(argv[1]);
946 off = atoi(argv[2]);
947 if (fd < 0 || fd >= 1024) {
948 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
949 return NT_STATUS_OK;
951 if (vfs->files[fd] == NULL) {
952 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
953 return NT_STATUS_OK;
956 if (SMB_VFS_FTRUNCATE(vfs->files[fd], off) == -1) {
957 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
958 return NT_STATUS_UNSUCCESSFUL;
961 printf("ftruncate: ok\n");
962 return NT_STATUS_OK;
965 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
967 int fd;
968 int op;
969 long offset;
970 long count;
971 int type;
972 const char *typestr;
974 if (argc != 6) {
975 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
976 printf(" ops: G = F_GETLK\n");
977 printf(" S = F_SETLK\n");
978 printf(" W = F_SETLKW\n");
979 printf(" type: R = F_RDLCK\n");
980 printf(" W = F_WRLCK\n");
981 printf(" U = F_UNLCK\n");
982 return NT_STATUS_OK;
985 if (sscanf(argv[1], "%d", &fd) == 0) {
986 printf("lock: error=-1 (error parsing fd)\n");
987 return NT_STATUS_UNSUCCESSFUL;
990 op = 0;
991 switch (*argv[2]) {
992 case 'G':
993 op = F_GETLK;
994 break;
995 case 'S':
996 op = F_SETLK;
997 break;
998 case 'W':
999 op = F_SETLKW;
1000 break;
1001 default:
1002 printf("lock: error=-1 (invalid op flag!)\n");
1003 return NT_STATUS_UNSUCCESSFUL;
1006 if (sscanf(argv[3], "%ld", &offset) == 0) {
1007 printf("lock: error=-1 (error parsing fd)\n");
1008 return NT_STATUS_UNSUCCESSFUL;
1011 if (sscanf(argv[4], "%ld", &count) == 0) {
1012 printf("lock: error=-1 (error parsing fd)\n");
1013 return NT_STATUS_UNSUCCESSFUL;
1016 type = 0;
1017 typestr = argv[5];
1018 while(*typestr) {
1019 switch (*typestr) {
1020 case 'R':
1021 type |= F_RDLCK;
1022 break;
1023 case 'W':
1024 type |= F_WRLCK;
1025 break;
1026 case 'U':
1027 type |= F_UNLCK;
1028 break;
1029 default:
1030 printf("lock: error=-1 (invalid type flag!)\n");
1031 return NT_STATUS_UNSUCCESSFUL;
1033 typestr++;
1036 printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
1038 if (SMB_VFS_LOCK(vfs->files[fd], op, offset, count, type) == False) {
1039 printf("lock: error=%d (%s)\n", errno, strerror(errno));
1040 return NT_STATUS_UNSUCCESSFUL;
1043 printf("lock: ok\n");
1044 return NT_STATUS_OK;
1047 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1049 if (argc != 3) {
1050 printf("Usage: symlink <path> <link>\n");
1051 return NT_STATUS_OK;
1054 if (SMB_VFS_SYMLINK(vfs->conn, argv[1], argv[2]) == -1) {
1055 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
1056 return NT_STATUS_UNSUCCESSFUL;
1059 printf("symlink: ok\n");
1060 return NT_STATUS_OK;
1064 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1066 char buffer[PATH_MAX];
1067 int size;
1069 if (argc != 2) {
1070 printf("Usage: readlink <path>\n");
1071 return NT_STATUS_OK;
1074 if ((size = SMB_VFS_READLINK(vfs->conn, argv[1], buffer, PATH_MAX)) == -1) {
1075 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
1076 return NT_STATUS_UNSUCCESSFUL;
1079 buffer[size] = '\0';
1080 printf("readlink: %s\n", buffer);
1081 return NT_STATUS_OK;
1085 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1087 if (argc != 3) {
1088 printf("Usage: link <path> <link>\n");
1089 return NT_STATUS_OK;
1092 if (SMB_VFS_LINK(vfs->conn, argv[1], argv[2]) == -1) {
1093 printf("link: error=%d (%s)\n", errno, strerror(errno));
1094 return NT_STATUS_UNSUCCESSFUL;
1097 printf("link: ok\n");
1098 return NT_STATUS_OK;
1101 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1103 mode_t mode;
1104 unsigned int dev_val;
1105 SMB_DEV_T dev;
1107 if (argc != 4) {
1108 printf("Usage: mknod <path> <mode> <dev>\n");
1109 printf(" mode is octal\n");
1110 printf(" dev is hex\n");
1111 return NT_STATUS_OK;
1114 if (sscanf(argv[2], "%ho", (unsigned short *)&mode) == 0) {
1115 printf("open: error=-1 (invalid mode!)\n");
1116 return NT_STATUS_UNSUCCESSFUL;
1119 if (sscanf(argv[3], "%x", &dev_val) == 0) {
1120 printf("open: error=-1 (invalid dev!)\n");
1121 return NT_STATUS_UNSUCCESSFUL;
1123 dev = (SMB_DEV_T)dev_val;
1125 if (SMB_VFS_MKNOD(vfs->conn, argv[1], mode, dev) == -1) {
1126 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1127 return NT_STATUS_UNSUCCESSFUL;
1130 printf("mknod: ok\n");
1131 return NT_STATUS_OK;
1134 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1136 if (argc != 2) {
1137 printf("Usage: realpath <path>\n");
1138 return NT_STATUS_OK;
1141 if (SMB_VFS_REALPATH(vfs->conn, argv[1]) == NULL) {
1142 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1143 return NT_STATUS_UNSUCCESSFUL;
1146 printf("realpath: ok\n");
1147 return NT_STATUS_OK;
1150 struct cmd_set vfs_commands[] = {
1152 { "VFS Commands" },
1154 { "load", cmd_load_module, "Load a module", "load <module.so>" },
1155 { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
1156 { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
1157 { "connect", cmd_connect, "VFS connect()", "connect" },
1158 { "disconnect", cmd_disconnect, "VFS disconnect()", "disconnect" },
1159 { "disk_free", cmd_disk_free, "VFS disk_free()", "disk_free <path>" },
1160 { "opendir", cmd_opendir, "VFS opendir()", "opendir <fname>" },
1161 { "readdir", cmd_readdir, "VFS readdir()", "readdir" },
1162 { "mkdir", cmd_mkdir, "VFS mkdir()", "mkdir <path>" },
1163 { "rmdir", cmd_pathfunc, "VFS rmdir()", "rmdir <path>" },
1164 { "closedir", cmd_closedir, "VFS closedir()", "closedir" },
1165 { "open", cmd_open, "VFS open()", "open <fname>" },
1166 { "close", cmd_close, "VFS close()", "close <fd>" },
1167 { "read", cmd_read, "VFS read()", "read <fd> <size>" },
1168 { "write", cmd_write, "VFS write()", "write <fd> <size>" },
1169 { "lseek", cmd_lseek, "VFS lseek()", "lseek <fd> <offset> <whence>" },
1170 { "rename", cmd_rename, "VFS rename()", "rename <old> <new>" },
1171 { "fsync", cmd_fsync, "VFS fsync()", "fsync <fd>" },
1172 { "stat", cmd_stat, "VFS stat()", "stat <fname>" },
1173 { "fstat", cmd_fstat, "VFS fstat()", "fstat <fd>" },
1174 { "lstat", cmd_lstat, "VFS lstat()", "lstat <fname>" },
1175 { "unlink", cmd_pathfunc, "VFS unlink()", "unlink <fname>" },
1176 { "chmod", cmd_chmod, "VFS chmod()", "chmod <path> <mode>" },
1177 { "fchmod", cmd_fchmod, "VFS fchmod()", "fchmod <fd> <mode>" },
1178 { "chown", cmd_chown, "VFS chown()", "chown <path> <uid> <gid>" },
1179 { "fchown", cmd_fchown, "VFS fchown()", "fchown <fd> <uid> <gid>" },
1180 { "chdir", cmd_pathfunc, "VFS chdir()", "chdir <path>" },
1181 { "getwd", cmd_getwd, "VFS getwd()", "getwd" },
1182 { "utime", cmd_utime, "VFS utime()", "utime <path> <access> <modify>" },
1183 { "ftruncate", cmd_ftruncate, "VFS ftruncate()", "ftruncate <fd> <length>" },
1184 { "lock", cmd_lock, "VFS lock()", "lock <f> <op> <offset> <count> <type>" },
1185 { "symlink", cmd_symlink, "VFS symlink()", "symlink <old> <new>" },
1186 { "readlink", cmd_readlink, "VFS readlink()", "readlink <path>" },
1187 { "link", cmd_link, "VFS link()", "link <oldpath> <newpath>" },
1188 { "mknod", cmd_mknod, "VFS mknod()", "mknod <path> <mode> <dev>" },
1189 { "realpath", cmd_realpath, "VFS realpath()", "realpath <path>" },
1190 { NULL }