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