7155ac3a170ef58a20399a1e1e9305a3362bd0a6
[samba.git] / source3 / torture / cmd_vfs.c
1 /*
2    Unix SMB/CIFS implementation.
3    VFS module functions
4
5    Copyright (C) Simo Sorce 2002
6    Copyright (C) Eric Lorimer 2002
7
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.
12
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.
17
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/>.
20 */
21
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
31 static const char *null_string = "";
32
33 static uint32_t ssf_flags(void)
34 {
35         return lp_posix_pathnames() ? SMB_FILENAME_POSIX_PATH : 0;
36 }
37
38 static NTSTATUS cmd_load_module(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
39 {
40         int i;
41
42         if (argc < 2) {
43                 printf("Usage: load <modules>\n");
44                 return NT_STATUS_OK;
45         }
46
47         for (i=argc-1;i>0;i--) {
48                 if (!vfs_init_custom(vfs->conn, argv[i])) {
49                         DEBUG(0, ("load: (vfs_init_custom failed for %s)\n", argv[i]));
50                         return NT_STATUS_UNSUCCESSFUL;
51                 }
52         }
53         printf("load: ok\n");
54         return NT_STATUS_OK;
55 }
56
57 static NTSTATUS cmd_populate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
58 {
59         char c;
60         size_t size;
61         if (argc != 3) {
62                 printf("Usage: populate <char> <size>\n");
63                 return NT_STATUS_OK;
64         }
65         c = argv[1][0];
66         size = atoi(argv[2]);
67         vfs->data = talloc_array(mem_ctx, char, size);
68         if (vfs->data == NULL) {
69                 printf("populate: error=-1 (not enough memory)");
70                 return NT_STATUS_UNSUCCESSFUL;
71         }
72         memset(vfs->data, c, size);
73         vfs->data_size = size;
74         return NT_STATUS_OK;
75 }
76
77 static NTSTATUS cmd_show_data(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
78 {
79         size_t offset;
80         size_t len;
81         if (argc != 1 && argc != 3) {
82                 printf("Usage: showdata [<offset> <len>]\n");
83                 return NT_STATUS_OK;
84         }
85         if (vfs->data == NULL || vfs->data_size == 0) {
86                 printf("show_data: error=-1 (buffer empty)\n");
87                 return NT_STATUS_UNSUCCESSFUL;
88         }
89
90         if (argc == 3) {
91                 offset = atoi(argv[1]);
92                 len = atoi(argv[2]);
93         } else {
94                 offset = 0;
95                 len = vfs->data_size;
96         }
97         if ((offset + len) > vfs->data_size) {
98                 printf("show_data: error=-1 (not enough data in buffer)\n");
99                 return NT_STATUS_UNSUCCESSFUL;
100         }
101         dump_data(0, (uint8_t *)(vfs->data) + offset, len);
102         return NT_STATUS_OK;
103 }
104
105 static NTSTATUS cmd_connect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
106 {
107         SMB_VFS_CONNECT(vfs->conn, lp_servicename(talloc_tos(), SNUM(vfs->conn)), "vfstest");
108         return NT_STATUS_OK;
109 }
110
111 static NTSTATUS cmd_disconnect(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
112 {
113         SMB_VFS_DISCONNECT(vfs->conn);
114         return NT_STATUS_OK;
115 }
116
117 static NTSTATUS cmd_disk_free(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
118 {
119         uint64_t diskfree, bsize, dfree, dsize;
120         if (argc != 2) {
121                 printf("Usage: disk_free <path>\n");
122                 return NT_STATUS_OK;
123         }
124
125         diskfree = SMB_VFS_DISK_FREE(vfs->conn, argv[1], &bsize, &dfree, &dsize);
126         printf("disk_free: %lu, bsize = %lu, dfree = %lu, dsize = %lu\n",
127                         (unsigned long)diskfree,
128                         (unsigned long)bsize,
129                         (unsigned long)dfree,
130                         (unsigned long)dsize);
131         return NT_STATUS_OK;
132 }
133
134
135 static NTSTATUS cmd_opendir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
136 {
137         struct smb_filename *smb_fname = NULL;
138
139         if (argc != 2) {
140                 printf("Usage: opendir <fname>\n");
141                 return NT_STATUS_OK;
142         }
143
144         smb_fname = synthetic_smb_fname(talloc_tos(),
145                                         argv[1],
146                                         NULL,
147                                         NULL,
148                                         ssf_flags());
149         if (smb_fname == NULL) {
150                 return NT_STATUS_NO_MEMORY;
151         }
152
153         vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, smb_fname, NULL, 0);
154         if (vfs->currentdir == NULL) {
155                 printf("opendir error=%d (%s)\n", errno, strerror(errno));
156                 TALLOC_FREE(smb_fname);
157                 return NT_STATUS_UNSUCCESSFUL;
158         }
159
160         TALLOC_FREE(smb_fname);
161         printf("opendir: ok\n");
162         return NT_STATUS_OK;
163 }
164
165
166 static NTSTATUS cmd_readdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
167 {
168         SMB_STRUCT_STAT st;
169         struct dirent *dent = NULL;
170
171         if (vfs->currentdir == NULL) {
172                 printf("readdir: error=-1 (no open directory)\n");
173                 return NT_STATUS_UNSUCCESSFUL;
174         }
175
176         dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir, &st);
177         if (dent == NULL) {
178                 printf("readdir: NULL\n");
179                 return NT_STATUS_OK;
180         }
181
182         printf("readdir: %s\n", dent->d_name);
183         if (VALID_STAT(st)) {
184                 time_t tmp_time;
185                 printf("  stat available");
186                 if (S_ISREG(st.st_ex_mode)) printf("  Regular File\n");
187                 else if (S_ISDIR(st.st_ex_mode)) printf("  Directory\n");
188                 else if (S_ISCHR(st.st_ex_mode)) printf("  Character Device\n");
189                 else if (S_ISBLK(st.st_ex_mode)) printf("  Block Device\n");
190                 else if (S_ISFIFO(st.st_ex_mode)) printf("  Fifo\n");
191                 else if (S_ISLNK(st.st_ex_mode)) printf("  Symbolic Link\n");
192                 else if (S_ISSOCK(st.st_ex_mode)) printf("  Socket\n");
193                 printf("  Size: %10u", (unsigned int)st.st_ex_size);
194 #ifdef HAVE_STAT_ST_BLOCKS
195                 printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
196 #endif
197 #ifdef HAVE_STAT_ST_BLKSIZE
198                 printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
199 #endif
200                 printf("  Device: 0x%10x", (unsigned int)st.st_ex_dev);
201                 printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
202                 printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
203                 printf("  Access: %05o", (int)((st.st_ex_mode) & 007777));
204                 printf(" Uid: %5lu Gid: %5lu\n",
205                        (unsigned long)st.st_ex_uid,
206                        (unsigned long)st.st_ex_gid);
207                 tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
208                 printf("  Access: %s", ctime(&tmp_time));
209                 tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
210                 printf("  Modify: %s", ctime(&tmp_time));
211                 tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
212                 printf("  Change: %s", ctime(&tmp_time));
213         }
214
215         return NT_STATUS_OK;
216 }
217
218
219 static NTSTATUS cmd_mkdir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
220 {
221         struct smb_filename *smb_fname = NULL;
222
223         if (argc != 2) {
224                 printf("Usage: mkdir <path>\n");
225                 return NT_STATUS_OK;
226         }
227
228         smb_fname = synthetic_smb_fname(talloc_tos(),
229                                         argv[1],
230                                         NULL,
231                                         NULL,
232                                         ssf_flags());
233
234         if (smb_fname == NULL) {
235                 return NT_STATUS_NO_MEMORY;
236         }
237
238         if (SMB_VFS_MKDIR(vfs->conn, smb_fname, 00755) == -1) {
239                 printf("mkdir error=%d (%s)\n", errno, strerror(errno));
240                 return NT_STATUS_UNSUCCESSFUL;
241         }
242
243         printf("mkdir: ok\n");
244         return NT_STATUS_OK;
245 }
246
247
248 static NTSTATUS cmd_closedir(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
249 {
250         int ret;
251
252         if (vfs->currentdir == NULL) {
253                 printf("closedir: failure (no directory open)\n");
254                 return NT_STATUS_UNSUCCESSFUL;
255         }
256
257         ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
258         if (ret == -1) {
259                 printf("closedir failure: %s\n", strerror(errno));
260                 return NT_STATUS_UNSUCCESSFUL;
261         }
262
263         printf("closedir: ok\n");
264         vfs->currentdir = NULL;
265         return NT_STATUS_OK;
266 }
267
268
269 static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
270 {
271         int flags;
272         mode_t mode;
273         const char *flagstr;
274         files_struct *fsp;
275         struct smb_filename *smb_fname = NULL;
276         NTSTATUS status;
277         int ret;
278
279         mode = 00400;
280
281         if (argc < 3 || argc > 5) {
282                 printf("Usage: open <filename> <flags> <mode>\n");
283                 printf("  flags: O = O_RDONLY\n");
284                 printf("         R = O_RDWR\n");
285                 printf("         W = O_WRONLY\n");
286                 printf("         C = O_CREAT\n");
287                 printf("         E = O_EXCL\n");
288                 printf("         T = O_TRUNC\n");
289                 printf("         A = O_APPEND\n");
290                 printf("         N = O_NONBLOCK/O_NDELAY\n");
291 #ifdef O_SYNC
292                 printf("         S = O_SYNC\n");
293 #endif
294 #ifdef O_NOFOLLOW
295                 printf("         F = O_NOFOLLOW\n");
296 #endif
297                 printf("  mode: see open.2\n");
298                 printf("        mode is ignored if C flag not present\n");
299                 printf("        mode defaults to 00400\n");
300                 return NT_STATUS_OK;
301         }
302         flags = 0;
303         flagstr = argv[2];
304         while (*flagstr) {
305                 switch (*flagstr) {
306                 case 'O':
307                         flags |= O_RDONLY;
308                         break;
309                 case 'R':
310                         flags |= O_RDWR;
311                         break;
312                 case 'W':
313                         flags |= O_WRONLY;
314                         break;
315                 case 'C':
316                         flags |= O_CREAT;
317                         break;
318                 case 'E':
319                         flags |= O_EXCL;
320                         break;
321                 case 'T':
322                         flags |= O_TRUNC;
323                         break;
324                 case 'A':
325                         flags |= O_APPEND;
326                         break;
327                 case 'N':
328                         flags |= O_NONBLOCK;
329                         break;
330 #ifdef O_SYNC
331                 case 'S':
332                         flags |= O_SYNC;
333                         break;
334 #endif
335 #ifdef O_NOFOLLOW
336                 case 'F':
337                         flags |= O_NOFOLLOW;
338                         break;
339 #endif
340                 default:
341                         printf("open: error=-1 (invalid flag!)\n");
342                         return NT_STATUS_UNSUCCESSFUL;
343                 }
344                 flagstr++;
345         }
346         if ((flags & O_CREAT) && argc == 4) {
347                 if (sscanf(argv[3], "%ho", (unsigned short *)&mode) == 0) {
348                         printf("open: error=-1 (invalid mode!)\n");
349                         return NT_STATUS_UNSUCCESSFUL;
350                 }
351         }
352
353         fsp = talloc_zero(vfs, struct files_struct);
354         if (fsp == NULL) {
355                 return NT_STATUS_NO_MEMORY;
356         }
357         fsp->fh = talloc_zero(fsp, struct fd_handle);
358         if (fsp->fh == NULL) {
359                 TALLOC_FREE(fsp);
360                 return NT_STATUS_NO_MEMORY;
361         }
362         fsp->conn = vfs->conn;
363
364         smb_fname = synthetic_smb_fname_split(NULL,
365                                         argv[1],
366                                         lp_posix_pathnames());
367         if (smb_fname == NULL) {
368                 TALLOC_FREE(fsp);
369                 return NT_STATUS_NO_MEMORY;
370         }
371
372         fsp->fsp_name = smb_fname;
373
374         fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, flags, mode);
375         if (fsp->fh->fd == -1) {
376                 printf("open: error=%d (%s)\n", errno, strerror(errno));
377                 TALLOC_FREE(fsp);
378                 TALLOC_FREE(smb_fname);
379                 return NT_STATUS_UNSUCCESSFUL;
380         }
381
382         status = NT_STATUS_OK;
383         ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
384         if (ret == -1) {
385                 /* If we have an fd, this stat should succeed. */
386                 DEBUG(0,("Error doing fstat on open file %s "
387                          "(%s)\n",
388                          smb_fname_str_dbg(smb_fname),
389                          strerror(errno) ));
390                 status = map_nt_error_from_unix(errno);
391         } else if (S_ISDIR(smb_fname->st.st_ex_mode)) {
392                 errno = EISDIR;
393                 status = NT_STATUS_FILE_IS_A_DIRECTORY;
394         }
395
396         if (!NT_STATUS_IS_OK(status)) {
397                 SMB_VFS_CLOSE(fsp);
398                 TALLOC_FREE(fsp);
399                 TALLOC_FREE(smb_fname);
400                 return status;
401         }
402
403         fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
404         fsp->vuid = UID_FIELD_INVALID;
405         fsp->file_pid = 0;
406         fsp->can_lock = True;
407         fsp->can_read = True;
408         fsp->can_write =
409                 CAN_WRITE(vfs->conn);
410         fsp->print_file = NULL;
411         fsp->modified = False;
412         fsp->sent_oplock_break = NO_BREAK_SENT;
413         fsp->is_directory = False;
414
415         vfs->files[fsp->fh->fd] = fsp;
416         printf("open: fd=%d\n", fsp->fh->fd);
417         return NT_STATUS_OK;
418 }
419
420
421 static NTSTATUS cmd_pathfunc(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
422 {
423         struct smb_filename *smb_fname = NULL;
424         int ret = -1;
425
426         if (argc != 2) {
427                 printf("Usage: %s <path>\n", argv[0]);
428                 return NT_STATUS_OK;
429         }
430
431         smb_fname = synthetic_smb_fname(talloc_tos(),
432                                         argv[1],
433                                         NULL,
434                                         NULL,
435                                         ssf_flags());
436
437         if (smb_fname == NULL) {
438                 return NT_STATUS_NO_MEMORY;
439         }
440
441         if (strcmp("rmdir", argv[0]) == 0 ) {
442                 ret = SMB_VFS_RMDIR(vfs->conn, smb_fname);
443                 TALLOC_FREE(smb_fname);
444         } else if (strcmp("unlink", argv[0]) == 0 ) {
445                 TALLOC_FREE(smb_fname);
446                 /* unlink can be a stream:name */
447                 smb_fname = synthetic_smb_fname_split(talloc_tos(),
448                                         argv[1],
449                                         lp_posix_pathnames());
450                 if (smb_fname == NULL) {
451                         return NT_STATUS_NO_MEMORY;
452                 }
453                 ret = SMB_VFS_UNLINK(vfs->conn, smb_fname);
454                 TALLOC_FREE(smb_fname);
455         } else if (strcmp("chdir", argv[0]) == 0 ) {
456                 ret = SMB_VFS_CHDIR(vfs->conn, argv[1]);
457         } else {
458                 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
459                 return NT_STATUS_UNSUCCESSFUL;
460         }
461
462         if (ret == -1) {
463                 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
464                 return NT_STATUS_UNSUCCESSFUL;
465         }
466
467         printf("%s: ok\n", argv[0]);
468         return NT_STATUS_OK;
469 }
470
471
472 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
473 {
474         int fd, ret;
475
476         if (argc != 2) {
477                 printf("Usage: close <fd>\n");
478                 return NT_STATUS_OK;
479         }
480
481         fd = atoi(argv[1]);
482         if (vfs->files[fd] == NULL) {
483                 printf("close: error=-1 (invalid file descriptor)\n");
484                 return NT_STATUS_OK;
485         }
486
487         ret = SMB_VFS_CLOSE(vfs->files[fd]);
488         if (ret == -1 )
489                 printf("close: error=%d (%s)\n", errno, strerror(errno));
490         else
491                 printf("close: ok\n");
492
493         TALLOC_FREE(vfs->files[fd]);
494         vfs->files[fd] = NULL;
495         return NT_STATUS_OK;
496 }
497
498
499 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
500 {
501         int fd;
502         size_t size, rsize;
503
504         if (argc != 3) {
505                 printf("Usage: read <fd> <size>\n");
506                 return NT_STATUS_OK;
507         }
508
509         /* do some error checking on these */
510         fd = atoi(argv[1]);
511         size = atoi(argv[2]);
512         vfs->data = talloc_array(mem_ctx, char, size);
513         if (vfs->data == NULL) {
514                 printf("read: error=-1 (not enough memory)");
515                 return NT_STATUS_UNSUCCESSFUL;
516         }
517         vfs->data_size = size;
518
519         rsize = SMB_VFS_READ(vfs->files[fd], vfs->data, size);
520         if (rsize == -1) {
521                 printf("read: error=%d (%s)\n", errno, strerror(errno));
522                 return NT_STATUS_UNSUCCESSFUL;
523         }
524
525         printf("read: ok\n");
526         return NT_STATUS_OK;
527 }
528
529
530 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
531 {
532         int fd, size, wsize;
533
534         if (argc != 3) {
535                 printf("Usage: write <fd> <size>\n");
536                 return NT_STATUS_OK;
537         }
538
539         /* some error checking should go here */
540         fd = atoi(argv[1]);
541         size = atoi(argv[2]);
542         if (vfs->data == NULL) {
543                 printf("write: error=-1 (buffer empty, please populate it before writing)");
544                 return NT_STATUS_UNSUCCESSFUL;
545         }
546
547         if (vfs->data_size < size) {
548                 printf("write: error=-1 (buffer too small, please put some more data in)");
549                 return NT_STATUS_UNSUCCESSFUL;
550         }
551
552         wsize = SMB_VFS_WRITE(vfs->files[fd], vfs->data, size);
553
554         if (wsize == -1) {
555                 printf("write: error=%d (%s)\n", errno, strerror(errno));
556                 return NT_STATUS_UNSUCCESSFUL;
557         }
558
559         printf("write: ok\n");
560         return NT_STATUS_OK;
561 }
562
563
564 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
565 {
566         int fd, offset, whence;
567         off_t pos;
568
569         if (argc != 4) {
570                 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
571                 return NT_STATUS_OK;
572         }
573
574         fd = atoi(argv[1]);
575         offset = atoi(argv[2]);
576         whence = atoi(argv[3]);
577         switch (whence) {
578                 case 1:         whence = SEEK_SET; break;
579                 case 2:         whence = SEEK_CUR; break;
580                 default:        whence = SEEK_END;
581         }
582
583         pos = SMB_VFS_LSEEK(vfs->files[fd], offset, whence);
584         if (pos == (off_t)-1) {
585                 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
586                 return NT_STATUS_UNSUCCESSFUL;
587         }
588
589         printf("lseek: ok\n");
590         return NT_STATUS_OK;
591 }
592
593
594 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
595 {
596         int ret;
597         struct smb_filename *smb_fname_src = NULL;
598         struct smb_filename *smb_fname_dst = NULL;
599
600         if (argc != 3) {
601                 printf("Usage: rename <old> <new>\n");
602                 return NT_STATUS_OK;
603         }
604
605         smb_fname_src = synthetic_smb_fname_split(mem_ctx,
606                                         argv[1],
607                                         lp_posix_pathnames());
608         if (smb_fname_src == NULL) {
609                 return NT_STATUS_NO_MEMORY;
610         }
611
612         smb_fname_dst = synthetic_smb_fname_split(mem_ctx,
613                                         argv[2],
614                                         lp_posix_pathnames());
615         if (smb_fname_dst == NULL) {
616                 TALLOC_FREE(smb_fname_src);
617                 return NT_STATUS_NO_MEMORY;
618         }
619
620         ret = SMB_VFS_RENAME(vfs->conn, smb_fname_src, smb_fname_dst);
621         TALLOC_FREE(smb_fname_src);
622         TALLOC_FREE(smb_fname_dst);
623         if (ret == -1) {
624                 printf("rename: error=%d (%s)\n", errno, strerror(errno));
625                 return NT_STATUS_UNSUCCESSFUL;
626         }
627
628         printf("rename: ok\n");
629         return NT_STATUS_OK;
630 }
631
632
633 static NTSTATUS cmd_fsync(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
634 {
635         int ret, fd;
636         if (argc != 2) {
637                 printf("Usage: fsync <fd>\n");
638                 return NT_STATUS_OK;
639         }
640
641         fd = atoi(argv[1]);
642         ret = SMB_VFS_FSYNC(vfs->files[fd]);
643         if (ret == -1) {
644                 printf("fsync: error=%d (%s)\n", errno, strerror(errno));
645                 return NT_STATUS_UNSUCCESSFUL;
646         }
647
648         printf("fsync: ok\n");
649         return NT_STATUS_OK;
650 }
651
652
653 static NTSTATUS cmd_stat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
654 {
655         int ret;
656         const char *user;
657         const char *group;
658         struct passwd *pwd = NULL;
659         struct group *grp = NULL;
660         struct smb_filename *smb_fname = NULL;
661         SMB_STRUCT_STAT st;
662         time_t tmp_time;
663
664         if (argc != 2) {
665                 printf("Usage: stat <fname>\n");
666                 return NT_STATUS_OK;
667         }
668
669         smb_fname = synthetic_smb_fname_split(mem_ctx,
670                                         argv[1],
671                                         lp_posix_pathnames());
672         if (smb_fname == NULL) {
673                 return NT_STATUS_NO_MEMORY;
674         }
675
676         ret = SMB_VFS_STAT(vfs->conn, smb_fname);
677         if (ret == -1) {
678                 printf("stat: error=%d (%s)\n", errno, strerror(errno));
679                 TALLOC_FREE(smb_fname);
680                 return NT_STATUS_UNSUCCESSFUL;
681         }
682         st = smb_fname->st;
683         TALLOC_FREE(smb_fname);
684
685         pwd = getpwuid(st.st_ex_uid);
686         if (pwd != NULL) user = pwd->pw_name;
687         else user = null_string;
688         grp = getgrgid(st.st_ex_gid);
689         if (grp != NULL) group = grp->gr_name;
690         else group = null_string;
691
692         printf("stat: ok\n");
693         printf("  File: %s", argv[1]);
694         if (S_ISREG(st.st_ex_mode)) printf("  Regular File\n");
695         else if (S_ISDIR(st.st_ex_mode)) printf("  Directory\n");
696         else if (S_ISCHR(st.st_ex_mode)) printf("  Character Device\n");
697         else if (S_ISBLK(st.st_ex_mode)) printf("  Block Device\n");
698         else if (S_ISFIFO(st.st_ex_mode)) printf("  Fifo\n");
699         else if (S_ISLNK(st.st_ex_mode)) printf("  Symbolic Link\n");
700         else if (S_ISSOCK(st.st_ex_mode)) printf("  Socket\n");
701         printf("  Size: %10u", (unsigned int)st.st_ex_size);
702 #ifdef HAVE_STAT_ST_BLOCKS
703         printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
704 #endif
705 #ifdef HAVE_STAT_ST_BLKSIZE
706         printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
707 #endif
708         printf("  Device: 0x%10x", (unsigned int)st.st_ex_dev);
709         printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
710         printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
711         printf("  Access: %05o", (int)((st.st_ex_mode) & 007777));
712         printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
713                (unsigned long)st.st_ex_gid, group);
714         tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
715         printf("  Access: %s", ctime(&tmp_time));
716         tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
717         printf("  Modify: %s", ctime(&tmp_time));
718         tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
719         printf("  Change: %s", ctime(&tmp_time));
720
721         return NT_STATUS_OK;
722 }
723
724
725 static NTSTATUS cmd_fstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
726 {
727         int fd;
728         const char *user;
729         const char *group;
730         struct passwd *pwd = NULL;
731         struct group *grp = NULL;
732         SMB_STRUCT_STAT st;
733         time_t tmp_time;
734
735         if (argc != 2) {
736                 printf("Usage: fstat <fd>\n");
737                 return NT_STATUS_OK;
738         }
739
740         fd = atoi(argv[1]);
741         if (fd < 0 || fd >= 1024) {
742                 printf("fstat: error=%d (file descriptor out of range)\n", EBADF);
743                 return NT_STATUS_OK;
744         }
745
746         if (vfs->files[fd] == NULL) {
747                 printf("fstat: error=%d (invalid file descriptor)\n", EBADF);
748                 return NT_STATUS_OK;
749         }
750
751         if (SMB_VFS_FSTAT(vfs->files[fd], &st) == -1) {
752                 printf("fstat: error=%d (%s)\n", errno, strerror(errno));
753                 return NT_STATUS_UNSUCCESSFUL;
754         }
755
756         pwd = getpwuid(st.st_ex_uid);
757         if (pwd != NULL) user = pwd->pw_name;
758         else user = null_string;
759         grp = getgrgid(st.st_ex_gid);
760         if (grp != NULL) group = grp->gr_name;
761         else group = null_string;
762
763         printf("fstat: ok\n");
764         if (S_ISREG(st.st_ex_mode)) printf("  Regular File\n");
765         else if (S_ISDIR(st.st_ex_mode)) printf("  Directory\n");
766         else if (S_ISCHR(st.st_ex_mode)) printf("  Character Device\n");
767         else if (S_ISBLK(st.st_ex_mode)) printf("  Block Device\n");
768         else if (S_ISFIFO(st.st_ex_mode)) printf("  Fifo\n");
769         else if (S_ISLNK(st.st_ex_mode)) printf("  Symbolic Link\n");
770         else if (S_ISSOCK(st.st_ex_mode)) printf("  Socket\n");
771         printf("  Size: %10u", (unsigned int)st.st_ex_size);
772 #ifdef HAVE_STAT_ST_BLOCKS
773         printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
774 #endif
775 #ifdef HAVE_STAT_ST_BLKSIZE
776         printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
777 #endif
778         printf("  Device: 0x%10x", (unsigned int)st.st_ex_dev);
779         printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
780         printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
781         printf("  Access: %05o", (int)((st.st_ex_mode) & 007777));
782         printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
783                (unsigned long)st.st_ex_gid, group);
784         tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
785         printf("  Access: %s", ctime(&tmp_time));
786         tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
787         printf("  Modify: %s", ctime(&tmp_time));
788         tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
789         printf("  Change: %s", ctime(&tmp_time));
790
791         return NT_STATUS_OK;
792 }
793
794
795 static NTSTATUS cmd_lstat(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
796 {
797         const char *user;
798         const char *group;
799         struct passwd *pwd = NULL;
800         struct group *grp = NULL;
801         struct smb_filename *smb_fname = NULL;
802         SMB_STRUCT_STAT st;
803         time_t tmp_time;
804
805         if (argc != 2) {
806                 printf("Usage: lstat <path>\n");
807                 return NT_STATUS_OK;
808         }
809
810         smb_fname = synthetic_smb_fname_split(mem_ctx,
811                                         argv[1],
812                                         lp_posix_pathnames());
813         if (smb_fname == NULL) {
814                 return NT_STATUS_NO_MEMORY;
815         }
816
817         if (SMB_VFS_LSTAT(vfs->conn, smb_fname) == -1) {
818                 printf("lstat: error=%d (%s)\n", errno, strerror(errno));
819                 TALLOC_FREE(smb_fname);
820                 return NT_STATUS_UNSUCCESSFUL;
821         }
822         st = smb_fname->st;
823         TALLOC_FREE(smb_fname);
824
825         pwd = getpwuid(st.st_ex_uid);
826         if (pwd != NULL) user = pwd->pw_name;
827         else user = null_string;
828         grp = getgrgid(st.st_ex_gid);
829         if (grp != NULL) group = grp->gr_name;
830         else group = null_string;
831
832         printf("lstat: ok\n");
833         if (S_ISREG(st.st_ex_mode)) printf("  Regular File\n");
834         else if (S_ISDIR(st.st_ex_mode)) printf("  Directory\n");
835         else if (S_ISCHR(st.st_ex_mode)) printf("  Character Device\n");
836         else if (S_ISBLK(st.st_ex_mode)) printf("  Block Device\n");
837         else if (S_ISFIFO(st.st_ex_mode)) printf("  Fifo\n");
838         else if (S_ISLNK(st.st_ex_mode)) printf("  Symbolic Link\n");
839         else if (S_ISSOCK(st.st_ex_mode)) printf("  Socket\n");
840         printf("  Size: %10u", (unsigned int)st.st_ex_size);
841 #ifdef HAVE_STAT_ST_BLOCKS
842         printf(" Blocks: %9u", (unsigned int)st.st_ex_blocks);
843 #endif
844 #ifdef HAVE_STAT_ST_BLKSIZE
845         printf(" IO Block: %u\n", (unsigned int)st.st_ex_blksize);
846 #endif
847         printf("  Device: 0x%10x", (unsigned int)st.st_ex_dev);
848         printf(" Inode: %10u", (unsigned int)st.st_ex_ino);
849         printf(" Links: %10u\n", (unsigned int)st.st_ex_nlink);
850         printf("  Access: %05o", (int)((st.st_ex_mode) & 007777));
851         printf(" Uid: %5lu/%.16s Gid: %5lu/%.16s\n", (unsigned long)st.st_ex_uid, user,
852                (unsigned long)st.st_ex_gid, group);
853         tmp_time = convert_timespec_to_time_t(st.st_ex_atime);
854         printf("  Access: %s", ctime(&tmp_time));
855         tmp_time = convert_timespec_to_time_t(st.st_ex_mtime);
856         printf("  Modify: %s", ctime(&tmp_time));
857         tmp_time = convert_timespec_to_time_t(st.st_ex_ctime);
858         printf("  Change: %s", ctime(&tmp_time));
859
860         return NT_STATUS_OK;
861 }
862
863
864 static NTSTATUS cmd_chmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
865 {
866         struct smb_filename *smb_fname = NULL;
867         mode_t mode;
868         if (argc != 3) {
869                 printf("Usage: chmod <path> <mode>\n");
870                 return NT_STATUS_OK;
871         }
872
873         mode = atoi(argv[2]);
874
875         smb_fname = synthetic_smb_fname(talloc_tos(),
876                                         argv[1],
877                                         NULL,
878                                         NULL,
879                                         ssf_flags());
880         if (smb_fname == NULL) {
881                 return NT_STATUS_NO_MEMORY;
882         }
883
884         if (SMB_VFS_CHMOD(vfs->conn, smb_fname, mode) == -1) {
885                 printf("chmod: error=%d (%s)\n", errno, strerror(errno));
886                 return NT_STATUS_UNSUCCESSFUL;
887         }
888
889         printf("chmod: ok\n");
890         return NT_STATUS_OK;
891 }
892
893
894 static NTSTATUS cmd_fchmod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
895 {
896         int fd;
897         mode_t mode;
898         if (argc != 3) {
899                 printf("Usage: fchmod <fd> <mode>\n");
900                 return NT_STATUS_OK;
901         }
902
903         fd = atoi(argv[1]);
904         mode = atoi(argv[2]);
905         if (fd < 0 || fd >= 1024) {
906                 printf("fchmod: error=%d (file descriptor out of range)\n", EBADF);
907                 return NT_STATUS_OK;
908         }
909         if (vfs->files[fd] == NULL) {
910                 printf("fchmod: error=%d (invalid file descriptor)\n", EBADF);
911                 return NT_STATUS_OK;
912         }
913
914         if (SMB_VFS_FCHMOD(vfs->files[fd], mode) == -1) {
915                 printf("fchmod: error=%d (%s)\n", errno, strerror(errno));
916                 return NT_STATUS_UNSUCCESSFUL;
917         }
918
919         printf("fchmod: ok\n");
920         return NT_STATUS_OK;
921 }
922
923
924 static NTSTATUS cmd_chmod_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
925 {
926         struct smb_filename *smb_fname = NULL;
927         mode_t mode;
928         if (argc != 3) {
929                 printf("Usage: chmod_acl <path> <mode>\n");
930                 return NT_STATUS_OK;
931         }
932
933         mode = atoi(argv[2]);
934
935         smb_fname = synthetic_smb_fname(talloc_tos(),
936                                         argv[1],
937                                         NULL,
938                                         NULL,
939                                         ssf_flags());
940         if (smb_fname == NULL) {
941                 return NT_STATUS_NO_MEMORY;
942         }
943
944         if (SMB_VFS_CHMOD_ACL(vfs->conn, smb_fname, mode) == -1) {
945                 printf("chmod_acl: error=%d (%s)\n", errno, strerror(errno));
946                 return NT_STATUS_UNSUCCESSFUL;
947         }
948
949         printf("chmod_acl: ok\n");
950         return NT_STATUS_OK;
951 }
952
953
954 static NTSTATUS cmd_fchmod_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
955 {
956         int fd;
957         mode_t mode;
958         if (argc != 3) {
959                 printf("Usage: fchmod_acl <fd> <mode>\n");
960                 return NT_STATUS_OK;
961         }
962
963         fd = atoi(argv[1]);
964         mode = atoi(argv[2]);
965         if (fd < 0 || fd >= 1024) {
966                 printf("fchmod_acl: error=%d (file descriptor out of range)\n", EBADF);
967                 return NT_STATUS_OK;
968         }
969         if (vfs->files[fd] == NULL) {
970                 printf("fchmod_acl: error=%d (invalid file descriptor)\n", EBADF);
971                 return NT_STATUS_OK;
972         }
973
974         if (SMB_VFS_FCHMOD_ACL(vfs->files[fd], mode) == -1) {
975                 printf("fchmod_acl: error=%d (%s)\n", errno, strerror(errno));
976                 return NT_STATUS_UNSUCCESSFUL;
977         }
978
979         printf("fchmod_acl: ok\n");
980         return NT_STATUS_OK;
981 }
982
983
984 static NTSTATUS cmd_chown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
985 {
986         struct smb_filename *smb_fname = NULL;
987         uid_t uid;
988         gid_t gid;
989         if (argc != 4) {
990                 printf("Usage: chown <path> <uid> <gid>\n");
991                 return NT_STATUS_OK;
992         }
993
994         uid = atoi(argv[2]);
995         gid = atoi(argv[3]);
996
997         smb_fname = synthetic_smb_fname(talloc_tos(),
998                                         argv[1],
999                                         NULL,
1000                                         NULL,
1001                                         ssf_flags());
1002         if (smb_fname == NULL) {
1003                 return NT_STATUS_NO_MEMORY;
1004         }
1005
1006         if (SMB_VFS_CHOWN(vfs->conn, smb_fname, uid, gid) == -1) {
1007                 printf("chown: error=%d (%s)\n", errno, strerror(errno));
1008                 return NT_STATUS_UNSUCCESSFUL;
1009         }
1010
1011         printf("chown: ok\n");
1012         return NT_STATUS_OK;
1013 }
1014
1015
1016 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1017 {
1018         uid_t uid;
1019         gid_t gid;
1020         int fd;
1021         if (argc != 4) {
1022                 printf("Usage: fchown <fd> <uid> <gid>\n");
1023                 return NT_STATUS_OK;
1024         }
1025
1026         uid = atoi(argv[2]);
1027         gid = atoi(argv[3]);
1028         fd = atoi(argv[1]);
1029         if (fd < 0 || fd >= 1024) {
1030                 printf("fchown: faliure=%d (file descriptor out of range)\n", EBADF);
1031                 return NT_STATUS_OK;
1032         }
1033         if (vfs->files[fd] == NULL) {
1034                 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
1035                 return NT_STATUS_OK;
1036         }
1037         if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
1038                 printf("fchown error=%d (%s)\n", errno, strerror(errno));
1039                 return NT_STATUS_UNSUCCESSFUL;
1040         }
1041
1042         printf("fchown: ok\n");
1043         return NT_STATUS_OK;
1044 }
1045
1046
1047 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1048 {
1049         char *buf = SMB_VFS_GETWD(vfs->conn);
1050         if (buf == NULL) {
1051                 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
1052                 return NT_STATUS_UNSUCCESSFUL;
1053         }
1054
1055         printf("getwd: %s\n", buf);
1056         SAFE_FREE(buf);
1057         return NT_STATUS_OK;
1058 }
1059
1060 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1061 {
1062         struct smb_file_time ft;
1063         struct smb_filename *smb_fname = NULL;
1064
1065         if (argc != 4) {
1066                 printf("Usage: utime <path> <access> <modify>\n");
1067                 return NT_STATUS_OK;
1068         }
1069
1070         ZERO_STRUCT(ft);
1071
1072         ft.atime = convert_time_t_to_timespec(atoi(argv[2]));
1073         ft.mtime = convert_time_t_to_timespec(atoi(argv[3]));
1074
1075         smb_fname = synthetic_smb_fname_split(mem_ctx,
1076                                         argv[1],
1077                                         lp_posix_pathnames());
1078         if (smb_fname == NULL) {
1079                 return NT_STATUS_NO_MEMORY;
1080         }
1081
1082         if (SMB_VFS_NTIMES(vfs->conn, smb_fname, &ft) != 0) {
1083                 printf("utime: error=%d (%s)\n", errno, strerror(errno));
1084                 TALLOC_FREE(smb_fname);
1085                 return NT_STATUS_UNSUCCESSFUL;
1086         }
1087
1088         TALLOC_FREE(smb_fname);
1089         printf("utime: ok\n");
1090         return NT_STATUS_OK;
1091 }
1092
1093 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1094 {
1095         int fd;
1096         off_t off;
1097         if (argc != 3) {
1098                 printf("Usage: ftruncate <fd> <length>\n");
1099                 return NT_STATUS_OK;
1100         }
1101
1102         fd = atoi(argv[1]);
1103         off = atoi(argv[2]);
1104         if (fd < 0 || fd >= 1024) {
1105                 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
1106                 return NT_STATUS_OK;
1107         }
1108         if (vfs->files[fd] == NULL) {
1109                 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
1110                 return NT_STATUS_OK;
1111         }
1112
1113         if (SMB_VFS_FTRUNCATE(vfs->files[fd], off) == -1) {
1114                 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
1115                 return NT_STATUS_UNSUCCESSFUL;
1116         }
1117
1118         printf("ftruncate: ok\n");
1119         return NT_STATUS_OK;
1120 }
1121
1122 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1123 {
1124         int fd;
1125         int op;
1126         long offset;
1127         long count;
1128         int type;
1129         const char *typestr;
1130
1131         if (argc != 6) {
1132                 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
1133                 printf("  ops: G = F_GETLK\n");
1134                 printf("       S = F_SETLK\n");
1135                 printf("       W = F_SETLKW\n");
1136                 printf("  type: R = F_RDLCK\n");
1137                 printf("        W = F_WRLCK\n");
1138                 printf("        U = F_UNLCK\n");
1139                 return NT_STATUS_OK;
1140         }
1141
1142         if (sscanf(argv[1], "%d", &fd) == 0) {
1143                 printf("lock: error=-1 (error parsing fd)\n");
1144                 return NT_STATUS_UNSUCCESSFUL;
1145         }
1146
1147         op = 0;
1148         switch (*argv[2]) {
1149         case 'G':
1150                 op = F_GETLK;
1151                 break;
1152         case 'S':
1153                 op = F_SETLK;
1154                 break;
1155         case 'W':
1156                 op = F_SETLKW;
1157                 break;
1158         default:
1159                 printf("lock: error=-1 (invalid op flag!)\n");
1160                 return NT_STATUS_UNSUCCESSFUL;
1161         }
1162
1163         if (sscanf(argv[3], "%ld", &offset) == 0) {
1164                 printf("lock: error=-1 (error parsing fd)\n");
1165                 return NT_STATUS_UNSUCCESSFUL;
1166         }
1167
1168         if (sscanf(argv[4], "%ld", &count) == 0) {
1169                 printf("lock: error=-1 (error parsing fd)\n");
1170                 return NT_STATUS_UNSUCCESSFUL;
1171         }
1172
1173         type = 0;
1174         typestr = argv[5];
1175         while(*typestr) {
1176                 switch (*typestr) {
1177                 case 'R':
1178                         type |= F_RDLCK;
1179                         break;
1180                 case 'W':
1181                         type |= F_WRLCK;
1182                         break;
1183                 case 'U':
1184                         type |= F_UNLCK;
1185                         break;
1186                 default:
1187                         printf("lock: error=-1 (invalid type flag!)\n");
1188                         return NT_STATUS_UNSUCCESSFUL;
1189                 }
1190                 typestr++;
1191         }
1192
1193         printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
1194
1195         if (SMB_VFS_LOCK(vfs->files[fd], op, offset, count, type) == False) {
1196                 printf("lock: error=%d (%s)\n", errno, strerror(errno));
1197                 return NT_STATUS_UNSUCCESSFUL;
1198         }
1199
1200         printf("lock: ok\n");
1201         return NT_STATUS_OK;
1202 }
1203
1204 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1205 {
1206         if (argc != 3) {
1207                 printf("Usage: symlink <path> <link>\n");
1208                 return NT_STATUS_OK;
1209         }
1210
1211         if (SMB_VFS_SYMLINK(vfs->conn, argv[1], argv[2]) == -1) {
1212                 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
1213                 return NT_STATUS_UNSUCCESSFUL;
1214         }
1215
1216         printf("symlink: ok\n");
1217         return NT_STATUS_OK;
1218 }
1219
1220
1221 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1222 {
1223         char buffer[PATH_MAX];
1224         int size;
1225
1226         if (argc != 2) {
1227                 printf("Usage: readlink <path>\n");
1228                 return NT_STATUS_OK;
1229         }
1230
1231         if ((size = SMB_VFS_READLINK(vfs->conn, argv[1], buffer, PATH_MAX)) == -1) {
1232                 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
1233                 return NT_STATUS_UNSUCCESSFUL;
1234         }
1235
1236         buffer[size] = '\0';
1237         printf("readlink: %s\n", buffer);
1238         return NT_STATUS_OK;
1239 }
1240
1241
1242 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1243 {
1244         if (argc != 3) {
1245                 printf("Usage: link <path> <link>\n");
1246                 return NT_STATUS_OK;
1247         }
1248
1249         if (SMB_VFS_LINK(vfs->conn, argv[1], argv[2]) == -1) {
1250                 printf("link: error=%d (%s)\n", errno, strerror(errno));
1251                 return NT_STATUS_UNSUCCESSFUL;
1252         }
1253
1254         printf("link: ok\n");
1255         return NT_STATUS_OK;
1256 }
1257
1258 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1259 {
1260         mode_t mode;
1261         unsigned int dev_val;
1262         SMB_DEV_T dev;
1263
1264         if (argc != 4) {
1265                 printf("Usage: mknod <path> <mode> <dev>\n");
1266                 printf("  mode is octal\n");
1267                 printf("  dev is hex\n");
1268                 return NT_STATUS_OK;
1269         }
1270
1271         if (sscanf(argv[2], "%ho", (unsigned short *)&mode) == 0) {
1272                 printf("open: error=-1 (invalid mode!)\n");
1273                 return NT_STATUS_UNSUCCESSFUL;
1274         }
1275
1276         if (sscanf(argv[3], "%x", &dev_val) == 0) {
1277                 printf("open: error=-1 (invalid dev!)\n");
1278                 return NT_STATUS_UNSUCCESSFUL;
1279         }
1280         dev = (SMB_DEV_T)dev_val;
1281
1282         if (SMB_VFS_MKNOD(vfs->conn, argv[1], mode, dev) == -1) {
1283                 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1284                 return NT_STATUS_UNSUCCESSFUL;
1285         }
1286
1287         printf("mknod: ok\n");
1288         return NT_STATUS_OK;
1289 }
1290
1291 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1292 {
1293         if (argc != 2) {
1294                 printf("Usage: realpath <path>\n");
1295                 return NT_STATUS_OK;
1296         }
1297
1298         if (SMB_VFS_REALPATH(vfs->conn, argv[1]) == NULL) {
1299                 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1300                 return NT_STATUS_UNSUCCESSFUL;
1301         }
1302
1303         printf("realpath: ok\n");
1304         return NT_STATUS_OK;
1305 }
1306
1307 static NTSTATUS cmd_getxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1308                              int argc, const char **argv)
1309 {
1310         uint8_t *buf;
1311         ssize_t ret;
1312
1313         if (argc != 3) {
1314                 printf("Usage: getxattr <path> <xattr>\n");
1315                 return NT_STATUS_OK;
1316         }
1317
1318         buf = NULL;
1319
1320         ret = SMB_VFS_GETXATTR(vfs->conn, argv[1], argv[2], buf,
1321                                talloc_get_size(buf));
1322         if (ret == -1) {
1323                 int err = errno;
1324                 printf("getxattr returned (%s)\n", strerror(err));
1325                 return map_nt_error_from_unix(err);
1326         }
1327         buf = talloc_array(mem_ctx, uint8_t, ret);
1328         if (buf == NULL) {
1329                 return NT_STATUS_NO_MEMORY;
1330         }
1331         ret = SMB_VFS_GETXATTR(vfs->conn, argv[1], argv[2], buf,
1332                                talloc_get_size(buf));
1333         if (ret == -1) {
1334                 int err = errno;
1335                 printf("getxattr returned (%s)\n", strerror(err));
1336                 return map_nt_error_from_unix(err);
1337         }
1338         dump_data_file(buf, talloc_get_size(buf), false, stdout);
1339         return NT_STATUS_OK;
1340 }
1341
1342 static NTSTATUS cmd_listxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1343                               int argc, const char **argv)
1344 {
1345         char *buf, *p;
1346         ssize_t ret;
1347         struct smb_filename *smb_fname = NULL;
1348
1349         if (argc != 2) {
1350                 printf("Usage: listxattr <path>\n");
1351                 return NT_STATUS_OK;
1352         }
1353
1354         buf = NULL;
1355
1356         smb_fname = synthetic_smb_fname_split(mem_ctx,
1357                                         argv[1],
1358                                         lp_posix_pathnames());
1359         if (smb_fname == NULL) {
1360                 return NT_STATUS_NO_MEMORY;
1361         }
1362         ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1363                                 buf, talloc_get_size(buf));
1364         if (ret == -1) {
1365                 int err = errno;
1366                 printf("listxattr returned (%s)\n", strerror(err));
1367                 return map_nt_error_from_unix(err);
1368         }
1369         buf = talloc_array(mem_ctx, char, ret);
1370         if (buf == NULL) {
1371                 return NT_STATUS_NO_MEMORY;
1372         }
1373         ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1374                                 buf, talloc_get_size(buf));
1375         if (ret == -1) {
1376                 int err = errno;
1377                 printf("listxattr returned (%s)\n", strerror(err));
1378                 return map_nt_error_from_unix(err);
1379         }
1380         if (ret == 0) {
1381                 return NT_STATUS_OK;
1382         }
1383         if (buf[ret-1] != '\0') {
1384                 printf("listxattr returned non 0-terminated strings\n");
1385                 return NT_STATUS_INTERNAL_ERROR;
1386         }
1387
1388         p = buf;
1389         while (p < buf+ret) {
1390                 printf("%s\n", p);
1391                 p = strchr(p, 0);
1392                 p += 1;
1393         }
1394         return NT_STATUS_OK;
1395 }
1396
1397 static NTSTATUS cmd_setxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1398                              int argc, const char **argv)
1399 {
1400         ssize_t ret;
1401         int flags = 0;
1402
1403         if ((argc < 4) || (argc > 5)) {
1404                 printf("Usage: setxattr <path> <xattr> <value> [flags]\n");
1405                 return NT_STATUS_OK;
1406         }
1407
1408         if (argc == 5) {
1409                 flags = atoi(argv[4]);
1410         }
1411
1412         ret = SMB_VFS_SETXATTR(vfs->conn, argv[1], argv[2],
1413                                argv[3], strlen(argv[3]), flags);
1414         if (ret == -1) {
1415                 int err = errno;
1416                 printf("setxattr returned (%s)\n", strerror(err));
1417                 return map_nt_error_from_unix(err);
1418         }
1419         return NT_STATUS_OK;
1420 }
1421
1422 static NTSTATUS cmd_removexattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1423                                 int argc, const char **argv)
1424 {
1425         ssize_t ret;
1426
1427         if (argc != 3) {
1428                 printf("Usage: removexattr <path> <xattr>\n");
1429                 return NT_STATUS_OK;
1430         }
1431
1432         ret = SMB_VFS_REMOVEXATTR(vfs->conn, argv[1], argv[2]);
1433         if (ret == -1) {
1434                 int err = errno;
1435                 printf("removexattr returned (%s)\n", strerror(err));
1436                 return map_nt_error_from_unix(err);
1437         }
1438         return NT_STATUS_OK;
1439 }
1440
1441 static NTSTATUS cmd_fget_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1442                                 int argc, const char **argv)
1443 {
1444         int fd;
1445         NTSTATUS status;
1446         struct security_descriptor *sd;
1447
1448         if (argc != 2) {
1449                 printf("Usage: fget_nt_acl <fd>\n");
1450                 return NT_STATUS_OK;
1451         }
1452
1453         fd = atoi(argv[1]);
1454         if (fd < 0 || fd >= 1024) {
1455                 printf("fget_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1456                 return NT_STATUS_OK;
1457         }
1458         if (vfs->files[fd] == NULL) {
1459                 printf("fget_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1460                 return NT_STATUS_OK;
1461         }
1462
1463         status = SMB_VFS_FGET_NT_ACL(vfs->files[fd],
1464                                      SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1465                                      talloc_tos(), &sd);
1466         if (!NT_STATUS_IS_OK(status)) {
1467                 printf("fget_nt_acl returned (%s)\n", nt_errstr(status));
1468                 return status;
1469         }
1470         printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1471         TALLOC_FREE(sd);
1472         return NT_STATUS_OK;
1473 }
1474
1475 static NTSTATUS cmd_get_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1476                                int argc, const char **argv)
1477 {
1478         NTSTATUS status;
1479         struct security_descriptor *sd;
1480         struct smb_filename *smb_fname = NULL;
1481
1482         if (argc != 2) {
1483                 printf("Usage: get_nt_acl <path>\n");
1484                 return NT_STATUS_OK;
1485         }
1486
1487         smb_fname = synthetic_smb_fname(talloc_tos(),
1488                                         argv[1],
1489                                         NULL,
1490                                         NULL,
1491                                         ssf_flags());
1492
1493         if (smb_fname == NULL) {
1494                 return NT_STATUS_NO_MEMORY;
1495         }
1496
1497         status = SMB_VFS_GET_NT_ACL(vfs->conn, smb_fname,
1498                                     SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1499                                     talloc_tos(), &sd);
1500         if (!NT_STATUS_IS_OK(status)) {
1501                 printf("get_nt_acl returned (%s)\n", nt_errstr(status));
1502                 return status;
1503         }
1504         printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1505         TALLOC_FREE(sd);
1506         return NT_STATUS_OK;
1507 }
1508
1509 static NTSTATUS cmd_fset_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1510                                 int argc, const char **argv)
1511 {
1512         int fd;
1513         NTSTATUS status;
1514         struct security_descriptor *sd;
1515
1516         if (argc != 3) {
1517                 printf("Usage: fset_nt_acl <fd> <sddl>\n");
1518                 return NT_STATUS_OK;
1519         }
1520
1521         fd = atoi(argv[1]);
1522         if (fd < 0 || fd >= 1024) {
1523                 printf("fset_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1524                 return NT_STATUS_OK;
1525         }
1526         if (vfs->files[fd] == NULL) {
1527                 printf("fset_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1528                 return NT_STATUS_OK;
1529         }
1530
1531         sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1532         if (!sd) {
1533                 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1534                 return NT_STATUS_INVALID_PARAMETER;
1535         }
1536
1537         status = SMB_VFS_FSET_NT_ACL(vfs->files[fd], SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1538         if (!NT_STATUS_IS_OK(status)) {
1539                 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1540                 return status;
1541         }
1542         TALLOC_FREE(sd);
1543         return NT_STATUS_OK;
1544 }
1545
1546 static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1547 {
1548         int flags;
1549         int ret;
1550         mode_t mode;
1551         files_struct *fsp;
1552         struct smb_filename *smb_fname = NULL;
1553         NTSTATUS status;
1554         struct security_descriptor *sd = NULL;
1555
1556         if (argc != 3) {
1557                 printf("Usage: set_nt_acl <file> <sddl>\n");
1558                 return NT_STATUS_OK;
1559         }
1560
1561         mode = 00400;
1562
1563         fsp = talloc_zero(vfs, struct files_struct);
1564         if (fsp == NULL) {
1565                 return NT_STATUS_NO_MEMORY;
1566         }
1567         fsp->fh = talloc_zero(fsp, struct fd_handle);
1568         if (fsp->fh == NULL) {
1569                 TALLOC_FREE(fsp);
1570                 return NT_STATUS_NO_MEMORY;
1571         }
1572         fsp->conn = vfs->conn;
1573
1574         smb_fname = synthetic_smb_fname_split(NULL,
1575                                         argv[1],
1576                                         lp_posix_pathnames());
1577         if (smb_fname == NULL) {
1578                 TALLOC_FREE(fsp);
1579                 return NT_STATUS_NO_MEMORY;
1580         }
1581
1582         fsp->fsp_name = smb_fname;
1583
1584 #ifdef O_DIRECTORY
1585         flags = O_RDONLY|O_DIRECTORY;
1586 #else
1587         /* POSIX allows us to open a directory with O_RDONLY. */
1588         flags = O_RDONLY;
1589 #endif
1590
1591         fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, O_RDWR, mode);
1592         if (fsp->fh->fd == -1 && errno == EISDIR) {
1593                 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, flags, mode);
1594         }
1595         if (fsp->fh->fd == -1) {
1596                 printf("open: error=%d (%s)\n", errno, strerror(errno));
1597                 TALLOC_FREE(fsp);
1598                 TALLOC_FREE(smb_fname);
1599                 return NT_STATUS_UNSUCCESSFUL;
1600         }
1601
1602         status = NT_STATUS_OK;
1603         ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
1604         if (ret == -1) {
1605                 /* If we have an fd, this stat should succeed. */
1606                 DEBUG(0,("Error doing fstat on open file %s "
1607                          "(%s)\n",
1608                          smb_fname_str_dbg(smb_fname),
1609                          strerror(errno) ));
1610                 status = map_nt_error_from_unix(errno);
1611         }
1612         
1613         if (!NT_STATUS_IS_OK(status)) {
1614                 goto out;
1615         }
1616
1617         fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
1618         fsp->vuid = UID_FIELD_INVALID;
1619         fsp->file_pid = 0;
1620         fsp->can_lock = True;
1621         fsp->can_read = True;
1622         fsp->can_write = True;
1623         fsp->print_file = NULL;
1624         fsp->modified = False;
1625         fsp->sent_oplock_break = NO_BREAK_SENT;
1626         fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
1627
1628
1629         sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1630         if (!sd) {
1631                 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1632                 status = NT_STATUS_INVALID_PARAMETER;
1633                 goto out;
1634         }
1635
1636         status = SMB_VFS_FSET_NT_ACL(fsp, SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1637         if (!NT_STATUS_IS_OK(status)) {
1638                 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1639                 goto out;
1640         }
1641 out:
1642         TALLOC_FREE(sd);
1643
1644         ret = SMB_VFS_CLOSE(fsp);
1645         if (ret == -1 )
1646                 printf("close: error=%d (%s)\n", errno, strerror(errno));
1647
1648         TALLOC_FREE(fsp);
1649
1650         return status;
1651 }
1652
1653
1654
1655 static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1656                                    int argc, const char **argv)
1657 {
1658         int fd;
1659         SMB_ACL_T acl;
1660         char *acl_text;
1661
1662         if (argc != 2) {
1663                 printf("Usage: sys_acl_get_fd <fd>\n");
1664                 return NT_STATUS_OK;
1665         }
1666
1667         fd = atoi(argv[1]);
1668         if (fd < 0 || fd >= 1024) {
1669                 printf("sys_acl_get_fd: error=%d (file descriptor out of range)\n", EBADF);
1670                 return NT_STATUS_OK;
1671         }
1672         if (vfs->files[fd] == NULL) {
1673                 printf("sys_acl_get_fd: error=%d (invalid file descriptor)\n", EBADF);
1674                 return NT_STATUS_OK;
1675         }
1676
1677         acl = SMB_VFS_SYS_ACL_GET_FD(vfs->files[fd], talloc_tos());
1678         if (!acl) {
1679                 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1680                 return NT_STATUS_UNSUCCESSFUL;
1681         }
1682         acl_text = sys_acl_to_text(acl, NULL);
1683         printf("%s", acl_text);
1684         TALLOC_FREE(acl);
1685         SAFE_FREE(acl_text);
1686         return NT_STATUS_OK;
1687 }
1688
1689 static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1690                                      int argc, const char **argv)
1691 {
1692         SMB_ACL_T acl;
1693         char *acl_text;
1694         int type;
1695         struct smb_filename *smb_fname = NULL;
1696
1697         if (argc != 3) {
1698                 printf("Usage: sys_acl_get_file <path> <type>\n");
1699                 return NT_STATUS_OK;
1700         }
1701
1702         smb_fname = synthetic_smb_fname_split(talloc_tos(),
1703                                         argv[1],
1704                                         lp_posix_pathnames());
1705         if (smb_fname == NULL) {
1706                 return NT_STATUS_NO_MEMORY;
1707         }
1708         type = atoi(argv[2]);
1709         acl = SMB_VFS_SYS_ACL_GET_FILE(vfs->conn, smb_fname,
1710                                 type, talloc_tos());
1711         if (!acl) {
1712                 printf("sys_acl_get_file failed (%s)\n", strerror(errno));
1713                 return NT_STATUS_UNSUCCESSFUL;
1714         }
1715         acl_text = sys_acl_to_text(acl, NULL);
1716         printf("%s", acl_text);
1717         TALLOC_FREE(acl);
1718         SAFE_FREE(acl_text);
1719         return NT_STATUS_OK;
1720 }
1721
1722 static NTSTATUS cmd_sys_acl_blob_get_file(struct vfs_state *vfs,
1723                                           TALLOC_CTX *mem_ctx,
1724                                           int argc, const char **argv)
1725 {
1726         char *description;
1727         DATA_BLOB blob;
1728         int ret;
1729         size_t i;
1730         struct smb_filename *smb_fname = NULL;
1731
1732         if (argc != 2) {
1733                 printf("Usage: sys_acl_get_file <path>\n");
1734                 return NT_STATUS_OK;
1735         }
1736
1737         smb_fname = synthetic_smb_fname_split(talloc_tos(),
1738                                         argv[1],
1739                                         lp_posix_pathnames());
1740         if (smb_fname == NULL) {
1741                 return NT_STATUS_NO_MEMORY;
1742         }
1743         ret = SMB_VFS_SYS_ACL_BLOB_GET_FILE(vfs->conn, smb_fname, talloc_tos(),
1744                                             &description, &blob);
1745         if (ret != 0) {
1746                 printf("sys_acl_blob_get_file failed (%s)\n", strerror(errno));
1747                 return map_nt_error_from_unix(errno);
1748         }
1749         printf("Description: %s\n", description);
1750         for (i = 0; i < blob.length; i++) {
1751                 printf("%.2x ", blob.data[i]);
1752         }
1753         printf("\n");
1754
1755         return NT_STATUS_OK;
1756 }
1757
1758 static NTSTATUS cmd_sys_acl_blob_get_fd(struct vfs_state *vfs,
1759                                         TALLOC_CTX *mem_ctx,
1760                                         int argc, const char **argv)
1761 {
1762         int fd;
1763         char *description;
1764         DATA_BLOB blob;
1765         int ret;
1766         size_t i;
1767
1768         if (argc != 2) {
1769                 printf("Usage: sys_acl_blob_get_fd <fd>\n");
1770                 return NT_STATUS_OK;
1771         }
1772
1773         fd = atoi(argv[1]);
1774         if (fd < 0 || fd >= 1024) {
1775                 printf("sys_acl_blob_get_fd: error=%d "
1776                        "(file descriptor out of range)\n", EBADF);
1777                 return NT_STATUS_OK;
1778         }
1779         if (vfs->files[fd] == NULL) {
1780                 printf("sys_acl_blob_get_fd: error=%d "
1781                        "(invalid file descriptor)\n", EBADF);
1782                 return NT_STATUS_OK;
1783         }
1784
1785         ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(vfs->files[fd], talloc_tos(),
1786                                           &description, &blob);
1787         if (ret != 0) {
1788                 printf("sys_acl_blob_get_fd failed (%s)\n", strerror(errno));
1789                 return map_nt_error_from_unix(errno);
1790         }
1791         printf("Description: %s\n", description);
1792         for (i = 0; i < blob.length; i++) {
1793                 printf("%.2x ", blob.data[i]);
1794         }
1795         printf("\n");
1796
1797         return NT_STATUS_OK;
1798 }
1799
1800
1801
1802 static NTSTATUS cmd_sys_acl_delete_def_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1803                                             int argc, const char **argv)
1804 {
1805         int ret;
1806         struct smb_filename *smb_fname = NULL;
1807
1808         if (argc != 2) {
1809                 printf("Usage: sys_acl_delete_def_file <path>\n");
1810                 return NT_STATUS_OK;
1811         }
1812
1813         smb_fname = synthetic_smb_fname(talloc_tos(),
1814                                         argv[1],
1815                                         NULL,
1816                                         NULL,
1817                                         ssf_flags());
1818
1819         if (smb_fname == NULL) {
1820                 return NT_STATUS_NO_MEMORY;
1821         }
1822         ret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(vfs->conn, smb_fname);
1823         if (ret == -1) {
1824                 printf("sys_acl_delete_def_file failed (%s)\n", strerror(errno));
1825                 TALLOC_FREE(smb_fname);
1826                 return NT_STATUS_UNSUCCESSFUL;
1827         }
1828         TALLOC_FREE(smb_fname);
1829         return NT_STATUS_OK;
1830 }
1831
1832 /* Afaik translate name was first introduced with vfs_catia, to be able
1833    to translate unix file/dir-names, containing invalid windows characters,
1834    to valid windows names.
1835    The used translation direction is always unix --> windows
1836 */
1837 static NTSTATUS cmd_translate_name(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1838                                             int argc, const char **argv)
1839 {
1840         int ret;
1841         struct dirent *dent = NULL;
1842         SMB_STRUCT_STAT st;
1843         bool found = false;
1844         char *translated = NULL;
1845         struct smb_filename *smb_fname = NULL;
1846         NTSTATUS status;
1847
1848         if (argc != 2) {
1849                 DEBUG(0, ("Usage: translate_name unix_filename\n"));
1850                 return NT_STATUS_UNSUCCESSFUL;
1851         }
1852
1853         smb_fname = synthetic_smb_fname(talloc_tos(),
1854                                         ".",
1855                                         NULL,
1856                                         NULL,
1857                                         ssf_flags());
1858         if (smb_fname == NULL) {
1859                 return NT_STATUS_NO_MEMORY;
1860         }
1861
1862         vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, smb_fname, NULL, 0);
1863         if (vfs->currentdir == NULL) {
1864                 DEBUG(0, ("cmd_translate_name: opendir error=%d (%s)\n",
1865                           errno, strerror(errno)));
1866                 TALLOC_FREE(smb_fname);
1867                 return NT_STATUS_UNSUCCESSFUL;
1868         }
1869
1870         while (true) {
1871                 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir, &st);
1872                 if (dent == NULL) {
1873                         break;
1874                 }
1875                 if (strcmp (dent->d_name, argv[1]) == 0) {
1876                         found = true;
1877                         break;
1878                 }
1879         };
1880
1881         if (!found) {
1882                 DEBUG(0, ("cmd_translate_name: file '%s' not found.\n", 
1883                           argv[1]));
1884                 status = NT_STATUS_UNSUCCESSFUL;
1885                 goto cleanup;
1886         }
1887         status = SMB_VFS_TRANSLATE_NAME(vfs->conn, dent->d_name,
1888                                         vfs_translate_to_windows,
1889                                         talloc_tos(), &translated);
1890         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
1891                 DEBUG(0, ("cmd_translate_name: file '%s' cannot be "
1892                           "translated\n", argv[1]));
1893                 TALLOC_FREE(translated);
1894                 goto cleanup;
1895         }
1896         /* translation success. But that could also mean
1897            that translating "aaa" to "aaa" was successful :-(
1898         */ 
1899         DEBUG(0, ("cmd_translate_name: file '%s' --> '%s'\n", 
1900                   argv[1], translated));
1901
1902         TALLOC_FREE(smb_fname);
1903         TALLOC_FREE(translated);
1904
1905 cleanup:
1906         TALLOC_FREE(smb_fname);
1907         ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
1908         if (ret == -1) {
1909                 DEBUG(0, ("cmd_translate_name: closedir failure: %s\n",
1910                           strerror(errno)));
1911                 return NT_STATUS_UNSUCCESSFUL;
1912         }
1913
1914         vfs->currentdir = NULL;
1915         return status;;
1916 }
1917
1918
1919 struct cmd_set vfs_commands[] = {
1920
1921         { "VFS Commands" },
1922
1923         { "load", cmd_load_module, "Load a module", "load <module.so>" },
1924         { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
1925         { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
1926         { "connect",   cmd_connect,   "VFS connect()",    "connect" },
1927         { "disconnect",   cmd_disconnect,   "VFS disconnect()",    "disconnect" },
1928         { "disk_free",   cmd_disk_free,   "VFS disk_free()",    "disk_free <path>" },
1929         { "opendir",   cmd_opendir,   "VFS opendir()",    "opendir <fname>" },
1930         { "readdir",   cmd_readdir,   "VFS readdir()",    "readdir" },
1931         { "mkdir",   cmd_mkdir,   "VFS mkdir()",    "mkdir <path>" },
1932         { "rmdir",   cmd_pathfunc,   "VFS rmdir()",    "rmdir <path>" },
1933         { "closedir",   cmd_closedir,   "VFS closedir()",    "closedir" },
1934         { "open",   cmd_open,   "VFS open()",    "open <fname> <flags> <mode>" },
1935         { "close",   cmd_close,   "VFS close()",    "close <fd>" },
1936         { "read",   cmd_read,   "VFS read()",    "read <fd> <size>" },
1937         { "write",   cmd_write,   "VFS write()",    "write <fd> <size>" },
1938         { "lseek",   cmd_lseek,   "VFS lseek()",    "lseek <fd> <offset> <whence>" },
1939         { "rename",   cmd_rename,   "VFS rename()",    "rename <old> <new>" },
1940         { "fsync",   cmd_fsync,   "VFS fsync()",    "fsync <fd>" },
1941         { "stat",   cmd_stat,   "VFS stat()",    "stat <fname>" },
1942         { "fstat",   cmd_fstat,   "VFS fstat()",    "fstat <fd>" },
1943         { "lstat",   cmd_lstat,   "VFS lstat()",    "lstat <fname>" },
1944         { "unlink",   cmd_pathfunc,   "VFS unlink()",    "unlink <fname>" },
1945         { "chmod",   cmd_chmod,   "VFS chmod()",    "chmod <path> <mode>" },
1946         { "fchmod",   cmd_fchmod,   "VFS fchmod()",    "fchmod <fd> <mode>" },
1947         { "chown",   cmd_chown,   "VFS chown()",    "chown <path> <uid> <gid>" },
1948         { "fchown",   cmd_fchown,   "VFS fchown()",    "fchown <fd> <uid> <gid>" },
1949         { "chdir",   cmd_pathfunc,   "VFS chdir()",    "chdir <path>" },
1950         { "getwd",   cmd_getwd,   "VFS getwd()",    "getwd" },
1951         { "utime",   cmd_utime,   "VFS utime()",    "utime <path> <access> <modify>" },
1952         { "ftruncate",   cmd_ftruncate,   "VFS ftruncate()",    "ftruncate <fd> <length>" },
1953         { "lock",   cmd_lock,   "VFS lock()",    "lock <f> <op> <offset> <count> <type>" },
1954         { "symlink",   cmd_symlink,   "VFS symlink()",    "symlink <old> <new>" },
1955         { "readlink",   cmd_readlink,   "VFS readlink()",    "readlink <path>" },
1956         { "link",   cmd_link,   "VFS link()",    "link <oldpath> <newpath>" },
1957         { "mknod",   cmd_mknod,   "VFS mknod()",    "mknod <path> <mode> <dev>" },
1958         { "realpath",   cmd_realpath,   "VFS realpath()",    "realpath <path>" },
1959         { "getxattr", cmd_getxattr, "VFS getxattr()",
1960           "getxattr <path> <name>" },
1961         { "listxattr", cmd_listxattr, "VFS listxattr()",
1962           "listxattr <path>" },
1963         { "setxattr", cmd_setxattr, "VFS setxattr()",
1964           "setxattr <path> <name> <value> [<flags>]" },
1965         { "removexattr", cmd_removexattr, "VFS removexattr()",
1966           "removexattr <path> <name>\n" },
1967         { "fget_nt_acl", cmd_fget_nt_acl, "VFS fget_nt_acl()", 
1968           "fget_nt_acl <fd>\n" },
1969         { "get_nt_acl", cmd_get_nt_acl, "VFS get_nt_acl()", 
1970           "get_nt_acl <path>\n" },
1971         { "fset_nt_acl", cmd_fset_nt_acl, "VFS fset_nt_acl()", 
1972           "fset_nt_acl <fd>\n" },
1973         { "set_nt_acl", cmd_set_nt_acl, "VFS open() and fset_nt_acl()", 
1974           "set_nt_acl <file>\n" },
1975         { "fchmod_acl",   cmd_fchmod_acl,   "VFS fchmod_acl()",    "fchmod_acl <fd> <mode>" },
1976         { "chmod_acl",   cmd_chmod_acl,   "VFS chmod_acl()",    "chmod_acl <path> <mode>" },
1977         { "sys_acl_get_file", cmd_sys_acl_get_file, "VFS sys_acl_get_file()", "sys_acl_get_file <path>" },
1978         { "sys_acl_get_fd", cmd_sys_acl_get_fd, "VFS sys_acl_get_fd()", "sys_acl_get_fd <fd>" },
1979         { "sys_acl_blob_get_file", cmd_sys_acl_blob_get_file,
1980           "VFS sys_acl_blob_get_file()", "sys_acl_blob_get_file <path>" },
1981         { "sys_acl_blob_get_fd", cmd_sys_acl_blob_get_fd,
1982           "VFS sys_acl_blob_get_fd()", "sys_acl_blob_get_fd <path>" },
1983         { "sys_acl_delete_def_file", cmd_sys_acl_delete_def_file, "VFS sys_acl_delete_def_file()", "sys_acl_delete_def_file <path>" },
1984
1985
1986         { "test_chain", cmd_test_chain, "test chain code",
1987           "test_chain" },
1988         { "translate_name", cmd_translate_name, "VFS translate_name()", "translate_name unix_filename" },
1989         { NULL }
1990 };