s3: VFS: Change SMB_VFS_MKNOD to use const struct smb_filename * instead of const...
[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         struct smb_filename *smb_fname = NULL;
1264
1265         if (argc != 4) {
1266                 printf("Usage: mknod <path> <mode> <dev>\n");
1267                 printf("  mode is octal\n");
1268                 printf("  dev is hex\n");
1269                 return NT_STATUS_OK;
1270         }
1271
1272         if (sscanf(argv[2], "%ho", (unsigned short *)&mode) == 0) {
1273                 printf("open: error=-1 (invalid mode!)\n");
1274                 return NT_STATUS_UNSUCCESSFUL;
1275         }
1276
1277         if (sscanf(argv[3], "%x", &dev_val) == 0) {
1278                 printf("open: error=-1 (invalid dev!)\n");
1279                 return NT_STATUS_UNSUCCESSFUL;
1280         }
1281         dev = (SMB_DEV_T)dev_val;
1282
1283         smb_fname = synthetic_smb_fname_split(mem_ctx,
1284                                         argv[1],
1285                                         lp_posix_pathnames());
1286         if (smb_fname == NULL) {
1287                 return NT_STATUS_NO_MEMORY;
1288         }
1289         if (SMB_VFS_MKNOD(vfs->conn, smb_fname, mode, dev) == -1) {
1290                 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1291                 return NT_STATUS_UNSUCCESSFUL;
1292         }
1293
1294         printf("mknod: ok\n");
1295         return NT_STATUS_OK;
1296 }
1297
1298 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1299 {
1300         if (argc != 2) {
1301                 printf("Usage: realpath <path>\n");
1302                 return NT_STATUS_OK;
1303         }
1304
1305         if (SMB_VFS_REALPATH(vfs->conn, argv[1]) == NULL) {
1306                 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1307                 return NT_STATUS_UNSUCCESSFUL;
1308         }
1309
1310         printf("realpath: ok\n");
1311         return NT_STATUS_OK;
1312 }
1313
1314 static NTSTATUS cmd_getxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1315                              int argc, const char **argv)
1316 {
1317         uint8_t *buf;
1318         ssize_t ret;
1319         struct smb_filename *smb_fname = NULL;
1320
1321         if (argc != 3) {
1322                 printf("Usage: getxattr <path> <xattr>\n");
1323                 return NT_STATUS_OK;
1324         }
1325
1326         buf = NULL;
1327
1328         smb_fname = synthetic_smb_fname_split(mem_ctx,
1329                                         argv[1],
1330                                         lp_posix_pathnames());
1331         if (smb_fname == NULL) {
1332                 return NT_STATUS_NO_MEMORY;
1333         }
1334         ret = SMB_VFS_GETXATTR(vfs->conn, smb_fname, argv[2], buf,
1335                                talloc_get_size(buf));
1336         if (ret == -1) {
1337                 int err = errno;
1338                 printf("getxattr returned (%s)\n", strerror(err));
1339                 return map_nt_error_from_unix(err);
1340         }
1341         buf = talloc_array(mem_ctx, uint8_t, ret);
1342         if (buf == NULL) {
1343                 return NT_STATUS_NO_MEMORY;
1344         }
1345         ret = SMB_VFS_GETXATTR(vfs->conn, smb_fname, argv[2], buf,
1346                                talloc_get_size(buf));
1347         if (ret == -1) {
1348                 int err = errno;
1349                 printf("getxattr returned (%s)\n", strerror(err));
1350                 return map_nt_error_from_unix(err);
1351         }
1352         dump_data_file(buf, talloc_get_size(buf), false, stdout);
1353         return NT_STATUS_OK;
1354 }
1355
1356 static NTSTATUS cmd_listxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1357                               int argc, const char **argv)
1358 {
1359         char *buf, *p;
1360         ssize_t ret;
1361         struct smb_filename *smb_fname = NULL;
1362
1363         if (argc != 2) {
1364                 printf("Usage: listxattr <path>\n");
1365                 return NT_STATUS_OK;
1366         }
1367
1368         buf = NULL;
1369
1370         smb_fname = synthetic_smb_fname_split(mem_ctx,
1371                                         argv[1],
1372                                         lp_posix_pathnames());
1373         if (smb_fname == NULL) {
1374                 return NT_STATUS_NO_MEMORY;
1375         }
1376         ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1377                                 buf, talloc_get_size(buf));
1378         if (ret == -1) {
1379                 int err = errno;
1380                 printf("listxattr returned (%s)\n", strerror(err));
1381                 return map_nt_error_from_unix(err);
1382         }
1383         buf = talloc_array(mem_ctx, char, ret);
1384         if (buf == NULL) {
1385                 return NT_STATUS_NO_MEMORY;
1386         }
1387         ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1388                                 buf, talloc_get_size(buf));
1389         if (ret == -1) {
1390                 int err = errno;
1391                 printf("listxattr returned (%s)\n", strerror(err));
1392                 return map_nt_error_from_unix(err);
1393         }
1394         if (ret == 0) {
1395                 return NT_STATUS_OK;
1396         }
1397         if (buf[ret-1] != '\0') {
1398                 printf("listxattr returned non 0-terminated strings\n");
1399                 return NT_STATUS_INTERNAL_ERROR;
1400         }
1401
1402         p = buf;
1403         while (p < buf+ret) {
1404                 printf("%s\n", p);
1405                 p = strchr(p, 0);
1406                 p += 1;
1407         }
1408         return NT_STATUS_OK;
1409 }
1410
1411 static NTSTATUS cmd_setxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1412                              int argc, const char **argv)
1413 {
1414         ssize_t ret;
1415         int flags = 0;
1416         struct smb_filename *smb_fname = NULL;
1417
1418         if ((argc < 4) || (argc > 5)) {
1419                 printf("Usage: setxattr <path> <xattr> <value> [flags]\n");
1420                 return NT_STATUS_OK;
1421         }
1422
1423         if (argc == 5) {
1424                 flags = atoi(argv[4]);
1425         }
1426
1427         smb_fname = synthetic_smb_fname_split(mem_ctx,
1428                                         argv[1],
1429                                         lp_posix_pathnames());
1430         if (smb_fname == NULL) {
1431                 return NT_STATUS_NO_MEMORY;
1432         }
1433         ret = SMB_VFS_SETXATTR(vfs->conn, smb_fname, argv[2],
1434                                argv[3], strlen(argv[3]), flags);
1435         if (ret == -1) {
1436                 int err = errno;
1437                 printf("setxattr returned (%s)\n", strerror(err));
1438                 return map_nt_error_from_unix(err);
1439         }
1440         return NT_STATUS_OK;
1441 }
1442
1443 static NTSTATUS cmd_removexattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1444                                 int argc, const char **argv)
1445 {
1446         ssize_t ret;
1447         struct smb_filename *smb_fname = NULL;
1448
1449         if (argc != 3) {
1450                 printf("Usage: removexattr <path> <xattr>\n");
1451                 return NT_STATUS_OK;
1452         }
1453
1454         smb_fname = synthetic_smb_fname(talloc_tos(),
1455                                         argv[1],
1456                                         NULL,
1457                                         NULL,
1458                                         ssf_flags());
1459
1460         if (smb_fname == NULL) {
1461                 return NT_STATUS_NO_MEMORY;
1462         }
1463
1464         ret = SMB_VFS_REMOVEXATTR(vfs->conn, smb_fname, argv[2]);
1465         if (ret == -1) {
1466                 int err = errno;
1467                 printf("removexattr returned (%s)\n", strerror(err));
1468                 return map_nt_error_from_unix(err);
1469         }
1470         return NT_STATUS_OK;
1471 }
1472
1473 static NTSTATUS cmd_fget_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1474                                 int argc, const char **argv)
1475 {
1476         int fd;
1477         NTSTATUS status;
1478         struct security_descriptor *sd;
1479
1480         if (argc != 2) {
1481                 printf("Usage: fget_nt_acl <fd>\n");
1482                 return NT_STATUS_OK;
1483         }
1484
1485         fd = atoi(argv[1]);
1486         if (fd < 0 || fd >= 1024) {
1487                 printf("fget_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1488                 return NT_STATUS_OK;
1489         }
1490         if (vfs->files[fd] == NULL) {
1491                 printf("fget_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1492                 return NT_STATUS_OK;
1493         }
1494
1495         status = SMB_VFS_FGET_NT_ACL(vfs->files[fd],
1496                                      SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1497                                      talloc_tos(), &sd);
1498         if (!NT_STATUS_IS_OK(status)) {
1499                 printf("fget_nt_acl returned (%s)\n", nt_errstr(status));
1500                 return status;
1501         }
1502         printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1503         TALLOC_FREE(sd);
1504         return NT_STATUS_OK;
1505 }
1506
1507 static NTSTATUS cmd_get_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1508                                int argc, const char **argv)
1509 {
1510         NTSTATUS status;
1511         struct security_descriptor *sd;
1512         struct smb_filename *smb_fname = NULL;
1513
1514         if (argc != 2) {
1515                 printf("Usage: get_nt_acl <path>\n");
1516                 return NT_STATUS_OK;
1517         }
1518
1519         smb_fname = synthetic_smb_fname(talloc_tos(),
1520                                         argv[1],
1521                                         NULL,
1522                                         NULL,
1523                                         ssf_flags());
1524
1525         if (smb_fname == NULL) {
1526                 return NT_STATUS_NO_MEMORY;
1527         }
1528
1529         status = SMB_VFS_GET_NT_ACL(vfs->conn, smb_fname,
1530                                     SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1531                                     talloc_tos(), &sd);
1532         if (!NT_STATUS_IS_OK(status)) {
1533                 printf("get_nt_acl returned (%s)\n", nt_errstr(status));
1534                 return status;
1535         }
1536         printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1537         TALLOC_FREE(sd);
1538         return NT_STATUS_OK;
1539 }
1540
1541 static NTSTATUS cmd_fset_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1542                                 int argc, const char **argv)
1543 {
1544         int fd;
1545         NTSTATUS status;
1546         struct security_descriptor *sd;
1547
1548         if (argc != 3) {
1549                 printf("Usage: fset_nt_acl <fd> <sddl>\n");
1550                 return NT_STATUS_OK;
1551         }
1552
1553         fd = atoi(argv[1]);
1554         if (fd < 0 || fd >= 1024) {
1555                 printf("fset_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1556                 return NT_STATUS_OK;
1557         }
1558         if (vfs->files[fd] == NULL) {
1559                 printf("fset_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1560                 return NT_STATUS_OK;
1561         }
1562
1563         sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1564         if (!sd) {
1565                 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1566                 return NT_STATUS_INVALID_PARAMETER;
1567         }
1568
1569         status = SMB_VFS_FSET_NT_ACL(vfs->files[fd], SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1570         if (!NT_STATUS_IS_OK(status)) {
1571                 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1572                 return status;
1573         }
1574         TALLOC_FREE(sd);
1575         return NT_STATUS_OK;
1576 }
1577
1578 static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1579 {
1580         int flags;
1581         int ret;
1582         mode_t mode;
1583         files_struct *fsp;
1584         struct smb_filename *smb_fname = NULL;
1585         NTSTATUS status;
1586         struct security_descriptor *sd = NULL;
1587
1588         if (argc != 3) {
1589                 printf("Usage: set_nt_acl <file> <sddl>\n");
1590                 return NT_STATUS_OK;
1591         }
1592
1593         mode = 00400;
1594
1595         fsp = talloc_zero(vfs, struct files_struct);
1596         if (fsp == NULL) {
1597                 return NT_STATUS_NO_MEMORY;
1598         }
1599         fsp->fh = talloc_zero(fsp, struct fd_handle);
1600         if (fsp->fh == NULL) {
1601                 TALLOC_FREE(fsp);
1602                 return NT_STATUS_NO_MEMORY;
1603         }
1604         fsp->conn = vfs->conn;
1605
1606         smb_fname = synthetic_smb_fname_split(NULL,
1607                                         argv[1],
1608                                         lp_posix_pathnames());
1609         if (smb_fname == NULL) {
1610                 TALLOC_FREE(fsp);
1611                 return NT_STATUS_NO_MEMORY;
1612         }
1613
1614         fsp->fsp_name = smb_fname;
1615
1616 #ifdef O_DIRECTORY
1617         flags = O_RDONLY|O_DIRECTORY;
1618 #else
1619         /* POSIX allows us to open a directory with O_RDONLY. */
1620         flags = O_RDONLY;
1621 #endif
1622
1623         fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, O_RDWR, mode);
1624         if (fsp->fh->fd == -1 && errno == EISDIR) {
1625                 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, flags, mode);
1626         }
1627         if (fsp->fh->fd == -1) {
1628                 printf("open: error=%d (%s)\n", errno, strerror(errno));
1629                 TALLOC_FREE(fsp);
1630                 TALLOC_FREE(smb_fname);
1631                 return NT_STATUS_UNSUCCESSFUL;
1632         }
1633
1634         status = NT_STATUS_OK;
1635         ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
1636         if (ret == -1) {
1637                 /* If we have an fd, this stat should succeed. */
1638                 DEBUG(0,("Error doing fstat on open file %s "
1639                          "(%s)\n",
1640                          smb_fname_str_dbg(smb_fname),
1641                          strerror(errno) ));
1642                 status = map_nt_error_from_unix(errno);
1643         }
1644         
1645         if (!NT_STATUS_IS_OK(status)) {
1646                 goto out;
1647         }
1648
1649         fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
1650         fsp->vuid = UID_FIELD_INVALID;
1651         fsp->file_pid = 0;
1652         fsp->can_lock = True;
1653         fsp->can_read = True;
1654         fsp->can_write = True;
1655         fsp->print_file = NULL;
1656         fsp->modified = False;
1657         fsp->sent_oplock_break = NO_BREAK_SENT;
1658         fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
1659
1660
1661         sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1662         if (!sd) {
1663                 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1664                 status = NT_STATUS_INVALID_PARAMETER;
1665                 goto out;
1666         }
1667
1668         status = SMB_VFS_FSET_NT_ACL(fsp, SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1669         if (!NT_STATUS_IS_OK(status)) {
1670                 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1671                 goto out;
1672         }
1673 out:
1674         TALLOC_FREE(sd);
1675
1676         ret = SMB_VFS_CLOSE(fsp);
1677         if (ret == -1 )
1678                 printf("close: error=%d (%s)\n", errno, strerror(errno));
1679
1680         TALLOC_FREE(fsp);
1681
1682         return status;
1683 }
1684
1685
1686
1687 static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1688                                    int argc, const char **argv)
1689 {
1690         int fd;
1691         SMB_ACL_T acl;
1692         char *acl_text;
1693
1694         if (argc != 2) {
1695                 printf("Usage: sys_acl_get_fd <fd>\n");
1696                 return NT_STATUS_OK;
1697         }
1698
1699         fd = atoi(argv[1]);
1700         if (fd < 0 || fd >= 1024) {
1701                 printf("sys_acl_get_fd: error=%d (file descriptor out of range)\n", EBADF);
1702                 return NT_STATUS_OK;
1703         }
1704         if (vfs->files[fd] == NULL) {
1705                 printf("sys_acl_get_fd: error=%d (invalid file descriptor)\n", EBADF);
1706                 return NT_STATUS_OK;
1707         }
1708
1709         acl = SMB_VFS_SYS_ACL_GET_FD(vfs->files[fd], talloc_tos());
1710         if (!acl) {
1711                 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1712                 return NT_STATUS_UNSUCCESSFUL;
1713         }
1714         acl_text = sys_acl_to_text(acl, NULL);
1715         printf("%s", acl_text);
1716         TALLOC_FREE(acl);
1717         SAFE_FREE(acl_text);
1718         return NT_STATUS_OK;
1719 }
1720
1721 static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1722                                      int argc, const char **argv)
1723 {
1724         SMB_ACL_T acl;
1725         char *acl_text;
1726         int type;
1727         struct smb_filename *smb_fname = NULL;
1728
1729         if (argc != 3) {
1730                 printf("Usage: sys_acl_get_file <path> <type>\n");
1731                 return NT_STATUS_OK;
1732         }
1733
1734         smb_fname = synthetic_smb_fname_split(talloc_tos(),
1735                                         argv[1],
1736                                         lp_posix_pathnames());
1737         if (smb_fname == NULL) {
1738                 return NT_STATUS_NO_MEMORY;
1739         }
1740         type = atoi(argv[2]);
1741         acl = SMB_VFS_SYS_ACL_GET_FILE(vfs->conn, smb_fname,
1742                                 type, talloc_tos());
1743         if (!acl) {
1744                 printf("sys_acl_get_file failed (%s)\n", strerror(errno));
1745                 return NT_STATUS_UNSUCCESSFUL;
1746         }
1747         acl_text = sys_acl_to_text(acl, NULL);
1748         printf("%s", acl_text);
1749         TALLOC_FREE(acl);
1750         SAFE_FREE(acl_text);
1751         return NT_STATUS_OK;
1752 }
1753
1754 static NTSTATUS cmd_sys_acl_blob_get_file(struct vfs_state *vfs,
1755                                           TALLOC_CTX *mem_ctx,
1756                                           int argc, const char **argv)
1757 {
1758         char *description;
1759         DATA_BLOB blob;
1760         int ret;
1761         size_t i;
1762         struct smb_filename *smb_fname = NULL;
1763
1764         if (argc != 2) {
1765                 printf("Usage: sys_acl_get_file <path>\n");
1766                 return NT_STATUS_OK;
1767         }
1768
1769         smb_fname = synthetic_smb_fname_split(talloc_tos(),
1770                                         argv[1],
1771                                         lp_posix_pathnames());
1772         if (smb_fname == NULL) {
1773                 return NT_STATUS_NO_MEMORY;
1774         }
1775         ret = SMB_VFS_SYS_ACL_BLOB_GET_FILE(vfs->conn, smb_fname, talloc_tos(),
1776                                             &description, &blob);
1777         if (ret != 0) {
1778                 printf("sys_acl_blob_get_file failed (%s)\n", strerror(errno));
1779                 return map_nt_error_from_unix(errno);
1780         }
1781         printf("Description: %s\n", description);
1782         for (i = 0; i < blob.length; i++) {
1783                 printf("%.2x ", blob.data[i]);
1784         }
1785         printf("\n");
1786
1787         return NT_STATUS_OK;
1788 }
1789
1790 static NTSTATUS cmd_sys_acl_blob_get_fd(struct vfs_state *vfs,
1791                                         TALLOC_CTX *mem_ctx,
1792                                         int argc, const char **argv)
1793 {
1794         int fd;
1795         char *description;
1796         DATA_BLOB blob;
1797         int ret;
1798         size_t i;
1799
1800         if (argc != 2) {
1801                 printf("Usage: sys_acl_blob_get_fd <fd>\n");
1802                 return NT_STATUS_OK;
1803         }
1804
1805         fd = atoi(argv[1]);
1806         if (fd < 0 || fd >= 1024) {
1807                 printf("sys_acl_blob_get_fd: error=%d "
1808                        "(file descriptor out of range)\n", EBADF);
1809                 return NT_STATUS_OK;
1810         }
1811         if (vfs->files[fd] == NULL) {
1812                 printf("sys_acl_blob_get_fd: error=%d "
1813                        "(invalid file descriptor)\n", EBADF);
1814                 return NT_STATUS_OK;
1815         }
1816
1817         ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(vfs->files[fd], talloc_tos(),
1818                                           &description, &blob);
1819         if (ret != 0) {
1820                 printf("sys_acl_blob_get_fd failed (%s)\n", strerror(errno));
1821                 return map_nt_error_from_unix(errno);
1822         }
1823         printf("Description: %s\n", description);
1824         for (i = 0; i < blob.length; i++) {
1825                 printf("%.2x ", blob.data[i]);
1826         }
1827         printf("\n");
1828
1829         return NT_STATUS_OK;
1830 }
1831
1832
1833
1834 static NTSTATUS cmd_sys_acl_delete_def_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1835                                             int argc, const char **argv)
1836 {
1837         int ret;
1838         struct smb_filename *smb_fname = NULL;
1839
1840         if (argc != 2) {
1841                 printf("Usage: sys_acl_delete_def_file <path>\n");
1842                 return NT_STATUS_OK;
1843         }
1844
1845         smb_fname = synthetic_smb_fname(talloc_tos(),
1846                                         argv[1],
1847                                         NULL,
1848                                         NULL,
1849                                         ssf_flags());
1850
1851         if (smb_fname == NULL) {
1852                 return NT_STATUS_NO_MEMORY;
1853         }
1854         ret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(vfs->conn, smb_fname);
1855         if (ret == -1) {
1856                 printf("sys_acl_delete_def_file failed (%s)\n", strerror(errno));
1857                 TALLOC_FREE(smb_fname);
1858                 return NT_STATUS_UNSUCCESSFUL;
1859         }
1860         TALLOC_FREE(smb_fname);
1861         return NT_STATUS_OK;
1862 }
1863
1864 /* Afaik translate name was first introduced with vfs_catia, to be able
1865    to translate unix file/dir-names, containing invalid windows characters,
1866    to valid windows names.
1867    The used translation direction is always unix --> windows
1868 */
1869 static NTSTATUS cmd_translate_name(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1870                                             int argc, const char **argv)
1871 {
1872         int ret;
1873         struct dirent *dent = NULL;
1874         SMB_STRUCT_STAT st;
1875         bool found = false;
1876         char *translated = NULL;
1877         struct smb_filename *smb_fname = NULL;
1878         NTSTATUS status;
1879
1880         if (argc != 2) {
1881                 DEBUG(0, ("Usage: translate_name unix_filename\n"));
1882                 return NT_STATUS_UNSUCCESSFUL;
1883         }
1884
1885         smb_fname = synthetic_smb_fname(talloc_tos(),
1886                                         ".",
1887                                         NULL,
1888                                         NULL,
1889                                         ssf_flags());
1890         if (smb_fname == NULL) {
1891                 return NT_STATUS_NO_MEMORY;
1892         }
1893
1894         vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, smb_fname, NULL, 0);
1895         if (vfs->currentdir == NULL) {
1896                 DEBUG(0, ("cmd_translate_name: opendir error=%d (%s)\n",
1897                           errno, strerror(errno)));
1898                 TALLOC_FREE(smb_fname);
1899                 return NT_STATUS_UNSUCCESSFUL;
1900         }
1901
1902         while (true) {
1903                 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir, &st);
1904                 if (dent == NULL) {
1905                         break;
1906                 }
1907                 if (strcmp (dent->d_name, argv[1]) == 0) {
1908                         found = true;
1909                         break;
1910                 }
1911         };
1912
1913         if (!found) {
1914                 DEBUG(0, ("cmd_translate_name: file '%s' not found.\n", 
1915                           argv[1]));
1916                 status = NT_STATUS_UNSUCCESSFUL;
1917                 goto cleanup;
1918         }
1919         status = SMB_VFS_TRANSLATE_NAME(vfs->conn, dent->d_name,
1920                                         vfs_translate_to_windows,
1921                                         talloc_tos(), &translated);
1922         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
1923                 DEBUG(0, ("cmd_translate_name: file '%s' cannot be "
1924                           "translated\n", argv[1]));
1925                 TALLOC_FREE(translated);
1926                 goto cleanup;
1927         }
1928         /* translation success. But that could also mean
1929            that translating "aaa" to "aaa" was successful :-(
1930         */ 
1931         DEBUG(0, ("cmd_translate_name: file '%s' --> '%s'\n", 
1932                   argv[1], translated));
1933
1934         TALLOC_FREE(smb_fname);
1935         TALLOC_FREE(translated);
1936
1937 cleanup:
1938         TALLOC_FREE(smb_fname);
1939         ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
1940         if (ret == -1) {
1941                 DEBUG(0, ("cmd_translate_name: closedir failure: %s\n",
1942                           strerror(errno)));
1943                 return NT_STATUS_UNSUCCESSFUL;
1944         }
1945
1946         vfs->currentdir = NULL;
1947         return status;;
1948 }
1949
1950
1951 struct cmd_set vfs_commands[] = {
1952
1953         { "VFS Commands" },
1954
1955         { "load", cmd_load_module, "Load a module", "load <module.so>" },
1956         { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
1957         { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
1958         { "connect",   cmd_connect,   "VFS connect()",    "connect" },
1959         { "disconnect",   cmd_disconnect,   "VFS disconnect()",    "disconnect" },
1960         { "disk_free",   cmd_disk_free,   "VFS disk_free()",    "disk_free <path>" },
1961         { "opendir",   cmd_opendir,   "VFS opendir()",    "opendir <fname>" },
1962         { "readdir",   cmd_readdir,   "VFS readdir()",    "readdir" },
1963         { "mkdir",   cmd_mkdir,   "VFS mkdir()",    "mkdir <path>" },
1964         { "rmdir",   cmd_pathfunc,   "VFS rmdir()",    "rmdir <path>" },
1965         { "closedir",   cmd_closedir,   "VFS closedir()",    "closedir" },
1966         { "open",   cmd_open,   "VFS open()",    "open <fname> <flags> <mode>" },
1967         { "close",   cmd_close,   "VFS close()",    "close <fd>" },
1968         { "read",   cmd_read,   "VFS read()",    "read <fd> <size>" },
1969         { "write",   cmd_write,   "VFS write()",    "write <fd> <size>" },
1970         { "lseek",   cmd_lseek,   "VFS lseek()",    "lseek <fd> <offset> <whence>" },
1971         { "rename",   cmd_rename,   "VFS rename()",    "rename <old> <new>" },
1972         { "fsync",   cmd_fsync,   "VFS fsync()",    "fsync <fd>" },
1973         { "stat",   cmd_stat,   "VFS stat()",    "stat <fname>" },
1974         { "fstat",   cmd_fstat,   "VFS fstat()",    "fstat <fd>" },
1975         { "lstat",   cmd_lstat,   "VFS lstat()",    "lstat <fname>" },
1976         { "unlink",   cmd_pathfunc,   "VFS unlink()",    "unlink <fname>" },
1977         { "chmod",   cmd_chmod,   "VFS chmod()",    "chmod <path> <mode>" },
1978         { "fchmod",   cmd_fchmod,   "VFS fchmod()",    "fchmod <fd> <mode>" },
1979         { "chown",   cmd_chown,   "VFS chown()",    "chown <path> <uid> <gid>" },
1980         { "fchown",   cmd_fchown,   "VFS fchown()",    "fchown <fd> <uid> <gid>" },
1981         { "chdir",   cmd_pathfunc,   "VFS chdir()",    "chdir <path>" },
1982         { "getwd",   cmd_getwd,   "VFS getwd()",    "getwd" },
1983         { "utime",   cmd_utime,   "VFS utime()",    "utime <path> <access> <modify>" },
1984         { "ftruncate",   cmd_ftruncate,   "VFS ftruncate()",    "ftruncate <fd> <length>" },
1985         { "lock",   cmd_lock,   "VFS lock()",    "lock <f> <op> <offset> <count> <type>" },
1986         { "symlink",   cmd_symlink,   "VFS symlink()",    "symlink <old> <new>" },
1987         { "readlink",   cmd_readlink,   "VFS readlink()",    "readlink <path>" },
1988         { "link",   cmd_link,   "VFS link()",    "link <oldpath> <newpath>" },
1989         { "mknod",   cmd_mknod,   "VFS mknod()",    "mknod <path> <mode> <dev>" },
1990         { "realpath",   cmd_realpath,   "VFS realpath()",    "realpath <path>" },
1991         { "getxattr", cmd_getxattr, "VFS getxattr()",
1992           "getxattr <path> <name>" },
1993         { "listxattr", cmd_listxattr, "VFS listxattr()",
1994           "listxattr <path>" },
1995         { "setxattr", cmd_setxattr, "VFS setxattr()",
1996           "setxattr <path> <name> <value> [<flags>]" },
1997         { "removexattr", cmd_removexattr, "VFS removexattr()",
1998           "removexattr <path> <name>\n" },
1999         { "fget_nt_acl", cmd_fget_nt_acl, "VFS fget_nt_acl()", 
2000           "fget_nt_acl <fd>\n" },
2001         { "get_nt_acl", cmd_get_nt_acl, "VFS get_nt_acl()", 
2002           "get_nt_acl <path>\n" },
2003         { "fset_nt_acl", cmd_fset_nt_acl, "VFS fset_nt_acl()", 
2004           "fset_nt_acl <fd>\n" },
2005         { "set_nt_acl", cmd_set_nt_acl, "VFS open() and fset_nt_acl()", 
2006           "set_nt_acl <file>\n" },
2007         { "fchmod_acl",   cmd_fchmod_acl,   "VFS fchmod_acl()",    "fchmod_acl <fd> <mode>" },
2008         { "chmod_acl",   cmd_chmod_acl,   "VFS chmod_acl()",    "chmod_acl <path> <mode>" },
2009         { "sys_acl_get_file", cmd_sys_acl_get_file, "VFS sys_acl_get_file()", "sys_acl_get_file <path>" },
2010         { "sys_acl_get_fd", cmd_sys_acl_get_fd, "VFS sys_acl_get_fd()", "sys_acl_get_fd <fd>" },
2011         { "sys_acl_blob_get_file", cmd_sys_acl_blob_get_file,
2012           "VFS sys_acl_blob_get_file()", "sys_acl_blob_get_file <path>" },
2013         { "sys_acl_blob_get_fd", cmd_sys_acl_blob_get_fd,
2014           "VFS sys_acl_blob_get_fd()", "sys_acl_blob_get_fd <path>" },
2015         { "sys_acl_delete_def_file", cmd_sys_acl_delete_def_file, "VFS sys_acl_delete_def_file()", "sys_acl_delete_def_file <path>" },
2016
2017
2018         { "test_chain", cmd_test_chain, "test chain code",
2019           "test_chain" },
2020         { "translate_name", cmd_translate_name, "VFS translate_name()", "translate_name unix_filename" },
2021         { NULL }
2022 };