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