pyldb: avoid segfault when adding an element with no name
[kai/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, smb_fname);
467                 TALLOC_FREE(smb_fname);
468         } else {
469                 printf("%s: error=%d (invalid function name!)\n", argv[0], errno);
470                 return NT_STATUS_UNSUCCESSFUL;
471         }
472
473         if (ret == -1) {
474                 printf("%s: error=%d (%s)\n", argv[0], errno, strerror(errno));
475                 return NT_STATUS_UNSUCCESSFUL;
476         }
477
478         printf("%s: ok\n", argv[0]);
479         return NT_STATUS_OK;
480 }
481
482
483 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
484 {
485         int fd, ret;
486
487         if (argc != 2) {
488                 printf("Usage: close <fd>\n");
489                 return NT_STATUS_OK;
490         }
491
492         fd = atoi(argv[1]);
493         if (vfs->files[fd] == NULL) {
494                 printf("close: error=-1 (invalid file descriptor)\n");
495                 return NT_STATUS_OK;
496         }
497
498         ret = SMB_VFS_CLOSE(vfs->files[fd]);
499         if (ret == -1 )
500                 printf("close: error=%d (%s)\n", errno, strerror(errno));
501         else
502                 printf("close: ok\n");
503
504         TALLOC_FREE(vfs->files[fd]);
505         vfs->files[fd] = NULL;
506         return NT_STATUS_OK;
507 }
508
509
510 static NTSTATUS cmd_read(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
511 {
512         int fd;
513         size_t size, rsize;
514
515         if (argc != 3) {
516                 printf("Usage: read <fd> <size>\n");
517                 return NT_STATUS_OK;
518         }
519
520         /* do some error checking on these */
521         fd = atoi(argv[1]);
522         size = atoi(argv[2]);
523         vfs->data = talloc_array(mem_ctx, char, size);
524         if (vfs->data == NULL) {
525                 printf("read: error=-1 (not enough memory)");
526                 return NT_STATUS_UNSUCCESSFUL;
527         }
528         vfs->data_size = size;
529
530         rsize = read_file(vfs->files[fd], vfs->data, 0, size);
531         if (rsize == -1) {
532                 printf("read: error=%d (%s)\n", errno, strerror(errno));
533                 return NT_STATUS_UNSUCCESSFUL;
534         }
535
536         printf("read: ok\n");
537         return NT_STATUS_OK;
538 }
539
540
541 static NTSTATUS cmd_write(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
542 {
543         int fd, size, wsize;
544
545         if (argc != 3) {
546                 printf("Usage: write <fd> <size>\n");
547                 return NT_STATUS_OK;
548         }
549
550         /* some error checking should go here */
551         fd = atoi(argv[1]);
552         size = atoi(argv[2]);
553         if (vfs->data == NULL) {
554                 printf("write: error=-1 (buffer empty, please populate it before writing)");
555                 return NT_STATUS_UNSUCCESSFUL;
556         }
557
558         if (vfs->data_size < size) {
559                 printf("write: error=-1 (buffer too small, please put some more data in)");
560                 return NT_STATUS_UNSUCCESSFUL;
561         }
562
563         wsize = write_file(NULL, vfs->files[fd], vfs->data, 0, size);
564
565         if (wsize == -1) {
566                 printf("write: error=%d (%s)\n", errno, strerror(errno));
567                 return NT_STATUS_UNSUCCESSFUL;
568         }
569
570         printf("write: ok\n");
571         return NT_STATUS_OK;
572 }
573
574
575 static NTSTATUS cmd_lseek(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
576 {
577         int fd, offset, whence;
578         off_t pos;
579
580         if (argc != 4) {
581                 printf("Usage: lseek <fd> <offset> <whence>\n...where whence is 1 => SEEK_SET, 2 => SEEK_CUR, 3 => SEEK_END\n");
582                 return NT_STATUS_OK;
583         }
584
585         fd = atoi(argv[1]);
586         offset = atoi(argv[2]);
587         whence = atoi(argv[3]);
588         switch (whence) {
589                 case 1:         whence = SEEK_SET; break;
590                 case 2:         whence = SEEK_CUR; break;
591                 default:        whence = SEEK_END;
592         }
593
594         pos = SMB_VFS_LSEEK(vfs->files[fd], offset, whence);
595         if (pos == (off_t)-1) {
596                 printf("lseek: error=%d (%s)\n", errno, strerror(errno));
597                 return NT_STATUS_UNSUCCESSFUL;
598         }
599
600         printf("lseek: ok\n");
601         return NT_STATUS_OK;
602 }
603
604
605 static NTSTATUS cmd_rename(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
606 {
607         int ret;
608         struct smb_filename *smb_fname_src = NULL;
609         struct smb_filename *smb_fname_dst = NULL;
610
611         if (argc != 3) {
612                 printf("Usage: rename <old> <new>\n");
613                 return NT_STATUS_OK;
614         }
615
616         smb_fname_src = synthetic_smb_fname_split(mem_ctx,
617                                         argv[1],
618                                         lp_posix_pathnames());
619         if (smb_fname_src == NULL) {
620                 return NT_STATUS_NO_MEMORY;
621         }
622
623         smb_fname_dst = synthetic_smb_fname_split(mem_ctx,
624                                         argv[2],
625                                         lp_posix_pathnames());
626         if (smb_fname_dst == NULL) {
627                 TALLOC_FREE(smb_fname_src);
628                 return NT_STATUS_NO_MEMORY;
629         }
630
631         ret = SMB_VFS_RENAME(vfs->conn, smb_fname_src, smb_fname_dst);
632         TALLOC_FREE(smb_fname_src);
633         TALLOC_FREE(smb_fname_dst);
634         if (ret == -1) {
635                 printf("rename: error=%d (%s)\n", errno, strerror(errno));
636                 return NT_STATUS_UNSUCCESSFUL;
637         }
638
639         printf("rename: ok\n");
640         return NT_STATUS_OK;
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_sync(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 static NTSTATUS cmd_chown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
934 {
935         struct smb_filename *smb_fname = NULL;
936         uid_t uid;
937         gid_t gid;
938         if (argc != 4) {
939                 printf("Usage: chown <path> <uid> <gid>\n");
940                 return NT_STATUS_OK;
941         }
942
943         uid = atoi(argv[2]);
944         gid = atoi(argv[3]);
945
946         smb_fname = synthetic_smb_fname(talloc_tos(),
947                                         argv[1],
948                                         NULL,
949                                         NULL,
950                                         ssf_flags());
951         if (smb_fname == NULL) {
952                 return NT_STATUS_NO_MEMORY;
953         }
954
955         if (SMB_VFS_CHOWN(vfs->conn, smb_fname, uid, gid) == -1) {
956                 printf("chown: error=%d (%s)\n", errno, strerror(errno));
957                 return NT_STATUS_UNSUCCESSFUL;
958         }
959
960         printf("chown: ok\n");
961         return NT_STATUS_OK;
962 }
963
964
965 static NTSTATUS cmd_fchown(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
966 {
967         uid_t uid;
968         gid_t gid;
969         int fd;
970         if (argc != 4) {
971                 printf("Usage: fchown <fd> <uid> <gid>\n");
972                 return NT_STATUS_OK;
973         }
974
975         uid = atoi(argv[2]);
976         gid = atoi(argv[3]);
977         fd = atoi(argv[1]);
978         if (fd < 0 || fd >= 1024) {
979                 printf("fchown: faliure=%d (file descriptor out of range)\n", EBADF);
980                 return NT_STATUS_OK;
981         }
982         if (vfs->files[fd] == NULL) {
983                 printf("fchown: error=%d (invalid file descriptor)\n", EBADF);
984                 return NT_STATUS_OK;
985         }
986         if (SMB_VFS_FCHOWN(vfs->files[fd], uid, gid) == -1) {
987                 printf("fchown error=%d (%s)\n", errno, strerror(errno));
988                 return NT_STATUS_UNSUCCESSFUL;
989         }
990
991         printf("fchown: ok\n");
992         return NT_STATUS_OK;
993 }
994
995
996 static NTSTATUS cmd_getwd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
997 {
998         struct smb_filename *smb_fname = SMB_VFS_GETWD(vfs->conn, talloc_tos());
999         if (smb_fname == NULL) {
1000                 printf("getwd: error=%d (%s)\n", errno, strerror(errno));
1001                 return NT_STATUS_UNSUCCESSFUL;
1002         }
1003
1004         printf("getwd: %s\n", smb_fname->base_name);
1005         TALLOC_FREE(smb_fname);
1006         return NT_STATUS_OK;
1007 }
1008
1009 static NTSTATUS cmd_utime(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1010 {
1011         struct smb_file_time ft;
1012         struct smb_filename *smb_fname = NULL;
1013
1014         if (argc != 4) {
1015                 printf("Usage: utime <path> <access> <modify>\n");
1016                 return NT_STATUS_OK;
1017         }
1018
1019         ZERO_STRUCT(ft);
1020
1021         ft.atime = convert_time_t_to_timespec(atoi(argv[2]));
1022         ft.mtime = convert_time_t_to_timespec(atoi(argv[3]));
1023
1024         smb_fname = synthetic_smb_fname_split(mem_ctx,
1025                                         argv[1],
1026                                         lp_posix_pathnames());
1027         if (smb_fname == NULL) {
1028                 return NT_STATUS_NO_MEMORY;
1029         }
1030
1031         if (SMB_VFS_NTIMES(vfs->conn, smb_fname, &ft) != 0) {
1032                 printf("utime: error=%d (%s)\n", errno, strerror(errno));
1033                 TALLOC_FREE(smb_fname);
1034                 return NT_STATUS_UNSUCCESSFUL;
1035         }
1036
1037         TALLOC_FREE(smb_fname);
1038         printf("utime: ok\n");
1039         return NT_STATUS_OK;
1040 }
1041
1042 static NTSTATUS cmd_ftruncate(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1043 {
1044         int fd;
1045         off_t off;
1046         if (argc != 3) {
1047                 printf("Usage: ftruncate <fd> <length>\n");
1048                 return NT_STATUS_OK;
1049         }
1050
1051         fd = atoi(argv[1]);
1052         off = atoi(argv[2]);
1053         if (fd < 0 || fd >= 1024) {
1054                 printf("ftruncate: error=%d (file descriptor out of range)\n", EBADF);
1055                 return NT_STATUS_OK;
1056         }
1057         if (vfs->files[fd] == NULL) {
1058                 printf("ftruncate: error=%d (invalid file descriptor)\n", EBADF);
1059                 return NT_STATUS_OK;
1060         }
1061
1062         if (SMB_VFS_FTRUNCATE(vfs->files[fd], off) == -1) {
1063                 printf("ftruncate: error=%d (%s)\n", errno, strerror(errno));
1064                 return NT_STATUS_UNSUCCESSFUL;
1065         }
1066
1067         printf("ftruncate: ok\n");
1068         return NT_STATUS_OK;
1069 }
1070
1071 static NTSTATUS cmd_lock(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1072 {
1073         int fd;
1074         int op;
1075         long offset;
1076         long count;
1077         int type;
1078         const char *typestr;
1079
1080         if (argc != 6) {
1081                 printf("Usage: lock <fd> <op> <offset> <count> <type>\n");
1082                 printf("  ops: G = F_GETLK\n");
1083                 printf("       S = F_SETLK\n");
1084                 printf("       W = F_SETLKW\n");
1085                 printf("  type: R = F_RDLCK\n");
1086                 printf("        W = F_WRLCK\n");
1087                 printf("        U = F_UNLCK\n");
1088                 return NT_STATUS_OK;
1089         }
1090
1091         if (sscanf(argv[1], "%d", &fd) == 0) {
1092                 printf("lock: error=-1 (error parsing fd)\n");
1093                 return NT_STATUS_UNSUCCESSFUL;
1094         }
1095
1096         op = 0;
1097         switch (*argv[2]) {
1098         case 'G':
1099                 op = F_GETLK;
1100                 break;
1101         case 'S':
1102                 op = F_SETLK;
1103                 break;
1104         case 'W':
1105                 op = F_SETLKW;
1106                 break;
1107         default:
1108                 printf("lock: error=-1 (invalid op flag!)\n");
1109                 return NT_STATUS_UNSUCCESSFUL;
1110         }
1111
1112         if (sscanf(argv[3], "%ld", &offset) == 0) {
1113                 printf("lock: error=-1 (error parsing fd)\n");
1114                 return NT_STATUS_UNSUCCESSFUL;
1115         }
1116
1117         if (sscanf(argv[4], "%ld", &count) == 0) {
1118                 printf("lock: error=-1 (error parsing fd)\n");
1119                 return NT_STATUS_UNSUCCESSFUL;
1120         }
1121
1122         type = 0;
1123         typestr = argv[5];
1124         while(*typestr) {
1125                 switch (*typestr) {
1126                 case 'R':
1127                         type |= F_RDLCK;
1128                         break;
1129                 case 'W':
1130                         type |= F_WRLCK;
1131                         break;
1132                 case 'U':
1133                         type |= F_UNLCK;
1134                         break;
1135                 default:
1136                         printf("lock: error=-1 (invalid type flag!)\n");
1137                         return NT_STATUS_UNSUCCESSFUL;
1138                 }
1139                 typestr++;
1140         }
1141
1142         printf("lock: debug lock(fd=%d, op=%d, offset=%ld, count=%ld, type=%d))\n", fd, op, offset, count, type);
1143
1144         if (SMB_VFS_LOCK(vfs->files[fd], op, offset, count, type) == False) {
1145                 printf("lock: error=%d (%s)\n", errno, strerror(errno));
1146                 return NT_STATUS_UNSUCCESSFUL;
1147         }
1148
1149         printf("lock: ok\n");
1150         return NT_STATUS_OK;
1151 }
1152
1153 static NTSTATUS cmd_symlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1154 {
1155         struct smb_filename *new_smb_fname = NULL;
1156
1157         if (argc != 3) {
1158                 printf("Usage: symlink <path> <link>\n");
1159                 return NT_STATUS_OK;
1160         }
1161
1162         new_smb_fname = synthetic_smb_fname_split(mem_ctx,
1163                                         argv[2],
1164                                         lp_posix_pathnames());
1165         if (new_smb_fname == NULL) {
1166                 return NT_STATUS_NO_MEMORY;
1167         }
1168         if (SMB_VFS_SYMLINK(vfs->conn, argv[1], new_smb_fname) == -1) {
1169                 printf("symlink: error=%d (%s)\n", errno, strerror(errno));
1170                 return NT_STATUS_UNSUCCESSFUL;
1171         }
1172
1173         printf("symlink: ok\n");
1174         return NT_STATUS_OK;
1175 }
1176
1177
1178 static NTSTATUS cmd_readlink(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1179 {
1180         char buffer[PATH_MAX];
1181         struct smb_filename *smb_fname = NULL;
1182         int size;
1183
1184         if (argc != 2) {
1185                 printf("Usage: readlink <path>\n");
1186                 return NT_STATUS_OK;
1187         }
1188
1189         smb_fname = synthetic_smb_fname_split(mem_ctx,
1190                                         argv[1],
1191                                         lp_posix_pathnames());
1192         if (smb_fname == NULL) {
1193                 return NT_STATUS_NO_MEMORY;
1194         }
1195         if ((size = SMB_VFS_READLINK(vfs->conn, smb_fname,
1196                                 buffer, PATH_MAX)) == -1) {
1197                 printf("readlink: error=%d (%s)\n", errno, strerror(errno));
1198                 return NT_STATUS_UNSUCCESSFUL;
1199         }
1200
1201         buffer[size] = '\0';
1202         printf("readlink: %s\n", buffer);
1203         return NT_STATUS_OK;
1204 }
1205
1206
1207 static NTSTATUS cmd_link(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1208 {
1209         struct smb_filename *old_smb_fname = NULL;
1210         struct smb_filename *new_smb_fname = NULL;
1211
1212         if (argc != 3) {
1213                 printf("Usage: link <path> <link>\n");
1214                 return NT_STATUS_OK;
1215         }
1216
1217         old_smb_fname = synthetic_smb_fname_split(mem_ctx,
1218                                         argv[1],
1219                                         lp_posix_pathnames());
1220         if (old_smb_fname == NULL) {
1221                 return NT_STATUS_NO_MEMORY;
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
1230         if (SMB_VFS_LINK(vfs->conn, old_smb_fname, new_smb_fname) == -1) {
1231                 printf("link: error=%d (%s)\n", errno, strerror(errno));
1232                 return NT_STATUS_UNSUCCESSFUL;
1233         }
1234
1235         printf("link: ok\n");
1236         return NT_STATUS_OK;
1237 }
1238
1239 static NTSTATUS cmd_mknod(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1240 {
1241         mode_t mode;
1242         unsigned int dev_val;
1243         SMB_DEV_T dev;
1244         struct smb_filename *smb_fname = NULL;
1245
1246         if (argc != 4) {
1247                 printf("Usage: mknod <path> <mode> <dev>\n");
1248                 printf("  mode is octal\n");
1249                 printf("  dev is hex\n");
1250                 return NT_STATUS_OK;
1251         }
1252
1253         if (sscanf(argv[2], "%ho", (unsigned short *)&mode) == 0) {
1254                 printf("open: error=-1 (invalid mode!)\n");
1255                 return NT_STATUS_UNSUCCESSFUL;
1256         }
1257
1258         if (sscanf(argv[3], "%x", &dev_val) == 0) {
1259                 printf("open: error=-1 (invalid dev!)\n");
1260                 return NT_STATUS_UNSUCCESSFUL;
1261         }
1262         dev = (SMB_DEV_T)dev_val;
1263
1264         smb_fname = synthetic_smb_fname_split(mem_ctx,
1265                                         argv[1],
1266                                         lp_posix_pathnames());
1267         if (smb_fname == NULL) {
1268                 return NT_STATUS_NO_MEMORY;
1269         }
1270         if (SMB_VFS_MKNOD(vfs->conn, smb_fname, mode, dev) == -1) {
1271                 printf("mknod: error=%d (%s)\n", errno, strerror(errno));
1272                 return NT_STATUS_UNSUCCESSFUL;
1273         }
1274
1275         printf("mknod: ok\n");
1276         return NT_STATUS_OK;
1277 }
1278
1279 static NTSTATUS cmd_realpath(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1280 {
1281         struct smb_filename *smb_fname = NULL;
1282
1283         if (argc != 2) {
1284                 printf("Usage: realpath <path>\n");
1285                 return NT_STATUS_OK;
1286         }
1287
1288         smb_fname = synthetic_smb_fname_split(mem_ctx,
1289                                         argv[1],
1290                                         lp_posix_pathnames());
1291         if (smb_fname == NULL) {
1292                 return NT_STATUS_NO_MEMORY;
1293         }
1294         if (SMB_VFS_REALPATH(vfs->conn, mem_ctx, smb_fname) == NULL) {
1295                 printf("realpath: error=%d (%s)\n", errno, strerror(errno));
1296                 return NT_STATUS_UNSUCCESSFUL;
1297         }
1298
1299         printf("realpath: ok\n");
1300         return NT_STATUS_OK;
1301 }
1302
1303 static NTSTATUS cmd_getxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1304                              int argc, const char **argv)
1305 {
1306         uint8_t *buf;
1307         ssize_t ret;
1308         struct smb_filename *smb_fname = NULL;
1309
1310         if (argc != 3) {
1311                 printf("Usage: getxattr <path> <xattr>\n");
1312                 return NT_STATUS_OK;
1313         }
1314
1315         buf = NULL;
1316
1317         smb_fname = synthetic_smb_fname_split(mem_ctx,
1318                                         argv[1],
1319                                         lp_posix_pathnames());
1320         if (smb_fname == NULL) {
1321                 return NT_STATUS_NO_MEMORY;
1322         }
1323         ret = SMB_VFS_GETXATTR(vfs->conn, smb_fname, argv[2], buf,
1324                                talloc_get_size(buf));
1325         if (ret == -1) {
1326                 int err = errno;
1327                 printf("getxattr returned (%s)\n", strerror(err));
1328                 return map_nt_error_from_unix(err);
1329         }
1330         buf = talloc_array(mem_ctx, uint8_t, ret);
1331         if (buf == NULL) {
1332                 return NT_STATUS_NO_MEMORY;
1333         }
1334         ret = SMB_VFS_GETXATTR(vfs->conn, smb_fname, argv[2], buf,
1335                                talloc_get_size(buf));
1336         if (ret == -1) {
1337                 int err = errno;
1338                 printf("getxattr returned (%s)\n", strerror(err));
1339                 return map_nt_error_from_unix(err);
1340         }
1341         dump_data_file(buf, talloc_get_size(buf), false, stdout);
1342         return NT_STATUS_OK;
1343 }
1344
1345 static NTSTATUS cmd_listxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1346                               int argc, const char **argv)
1347 {
1348         char *buf, *p;
1349         ssize_t ret;
1350         struct smb_filename *smb_fname = NULL;
1351
1352         if (argc != 2) {
1353                 printf("Usage: listxattr <path>\n");
1354                 return NT_STATUS_OK;
1355         }
1356
1357         buf = NULL;
1358
1359         smb_fname = synthetic_smb_fname_split(mem_ctx,
1360                                         argv[1],
1361                                         lp_posix_pathnames());
1362         if (smb_fname == NULL) {
1363                 return NT_STATUS_NO_MEMORY;
1364         }
1365         ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1366                                 buf, talloc_get_size(buf));
1367         if (ret == -1) {
1368                 int err = errno;
1369                 printf("listxattr returned (%s)\n", strerror(err));
1370                 return map_nt_error_from_unix(err);
1371         }
1372         buf = talloc_array(mem_ctx, char, ret);
1373         if (buf == NULL) {
1374                 return NT_STATUS_NO_MEMORY;
1375         }
1376         ret = SMB_VFS_LISTXATTR(vfs->conn, smb_fname,
1377                                 buf, talloc_get_size(buf));
1378         if (ret == -1) {
1379                 int err = errno;
1380                 printf("listxattr returned (%s)\n", strerror(err));
1381                 return map_nt_error_from_unix(err);
1382         }
1383         if (ret == 0) {
1384                 return NT_STATUS_OK;
1385         }
1386         if (buf[ret-1] != '\0') {
1387                 printf("listxattr returned non 0-terminated strings\n");
1388                 return NT_STATUS_INTERNAL_ERROR;
1389         }
1390
1391         p = buf;
1392         while (p < buf+ret) {
1393                 printf("%s\n", p);
1394                 p = strchr(p, 0);
1395                 p += 1;
1396         }
1397         return NT_STATUS_OK;
1398 }
1399
1400 static NTSTATUS cmd_setxattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1401                              int argc, const char **argv)
1402 {
1403         ssize_t ret;
1404         int flags = 0;
1405         struct smb_filename *smb_fname = NULL;
1406
1407         if ((argc < 4) || (argc > 5)) {
1408                 printf("Usage: setxattr <path> <xattr> <value> [flags]\n");
1409                 return NT_STATUS_OK;
1410         }
1411
1412         if (argc == 5) {
1413                 flags = atoi(argv[4]);
1414         }
1415
1416         smb_fname = synthetic_smb_fname_split(mem_ctx,
1417                                         argv[1],
1418                                         lp_posix_pathnames());
1419         if (smb_fname == NULL) {
1420                 return NT_STATUS_NO_MEMORY;
1421         }
1422         ret = SMB_VFS_SETXATTR(vfs->conn, smb_fname, argv[2],
1423                                argv[3], strlen(argv[3]), flags);
1424         if (ret == -1) {
1425                 int err = errno;
1426                 printf("setxattr returned (%s)\n", strerror(err));
1427                 return map_nt_error_from_unix(err);
1428         }
1429         return NT_STATUS_OK;
1430 }
1431
1432 static NTSTATUS cmd_removexattr(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1433                                 int argc, const char **argv)
1434 {
1435         ssize_t ret;
1436         struct smb_filename *smb_fname = NULL;
1437
1438         if (argc != 3) {
1439                 printf("Usage: removexattr <path> <xattr>\n");
1440                 return NT_STATUS_OK;
1441         }
1442
1443         smb_fname = synthetic_smb_fname(talloc_tos(),
1444                                         argv[1],
1445                                         NULL,
1446                                         NULL,
1447                                         ssf_flags());
1448
1449         if (smb_fname == NULL) {
1450                 return NT_STATUS_NO_MEMORY;
1451         }
1452
1453         ret = SMB_VFS_REMOVEXATTR(vfs->conn, smb_fname, argv[2]);
1454         if (ret == -1) {
1455                 int err = errno;
1456                 printf("removexattr returned (%s)\n", strerror(err));
1457                 return map_nt_error_from_unix(err);
1458         }
1459         return NT_STATUS_OK;
1460 }
1461
1462 static NTSTATUS cmd_fget_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1463                                 int argc, const char **argv)
1464 {
1465         int fd;
1466         NTSTATUS status;
1467         struct security_descriptor *sd;
1468
1469         if (argc != 2) {
1470                 printf("Usage: fget_nt_acl <fd>\n");
1471                 return NT_STATUS_OK;
1472         }
1473
1474         fd = atoi(argv[1]);
1475         if (fd < 0 || fd >= 1024) {
1476                 printf("fget_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1477                 return NT_STATUS_OK;
1478         }
1479         if (vfs->files[fd] == NULL) {
1480                 printf("fget_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1481                 return NT_STATUS_OK;
1482         }
1483
1484         status = SMB_VFS_FGET_NT_ACL(vfs->files[fd],
1485                                      SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1486                                      talloc_tos(), &sd);
1487         if (!NT_STATUS_IS_OK(status)) {
1488                 printf("fget_nt_acl returned (%s)\n", nt_errstr(status));
1489                 return status;
1490         }
1491         printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1492         TALLOC_FREE(sd);
1493         return NT_STATUS_OK;
1494 }
1495
1496 static NTSTATUS cmd_get_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1497                                int argc, const char **argv)
1498 {
1499         NTSTATUS status;
1500         struct security_descriptor *sd;
1501         struct smb_filename *smb_fname = NULL;
1502
1503         if (argc != 2) {
1504                 printf("Usage: get_nt_acl <path>\n");
1505                 return NT_STATUS_OK;
1506         }
1507
1508         smb_fname = synthetic_smb_fname(talloc_tos(),
1509                                         argv[1],
1510                                         NULL,
1511                                         NULL,
1512                                         ssf_flags());
1513
1514         if (smb_fname == NULL) {
1515                 return NT_STATUS_NO_MEMORY;
1516         }
1517
1518         status = SMB_VFS_GET_NT_ACL(vfs->conn, smb_fname,
1519                                     SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL,
1520                                     talloc_tos(), &sd);
1521         if (!NT_STATUS_IS_OK(status)) {
1522                 printf("get_nt_acl returned (%s)\n", nt_errstr(status));
1523                 return status;
1524         }
1525         printf("%s\n", sddl_encode(talloc_tos(), sd, get_global_sam_sid()));
1526         TALLOC_FREE(sd);
1527         return NT_STATUS_OK;
1528 }
1529
1530 static NTSTATUS cmd_fset_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1531                                 int argc, const char **argv)
1532 {
1533         int fd;
1534         NTSTATUS status;
1535         struct security_descriptor *sd;
1536
1537         if (argc != 3) {
1538                 printf("Usage: fset_nt_acl <fd> <sddl>\n");
1539                 return NT_STATUS_OK;
1540         }
1541
1542         fd = atoi(argv[1]);
1543         if (fd < 0 || fd >= 1024) {
1544                 printf("fset_nt_acl: error=%d (file descriptor out of range)\n", EBADF);
1545                 return NT_STATUS_OK;
1546         }
1547         if (vfs->files[fd] == NULL) {
1548                 printf("fset_nt_acl: error=%d (invalid file descriptor)\n", EBADF);
1549                 return NT_STATUS_OK;
1550         }
1551
1552         sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1553         if (!sd) {
1554                 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1555                 return NT_STATUS_INVALID_PARAMETER;
1556         }
1557
1558         status = SMB_VFS_FSET_NT_ACL(vfs->files[fd], SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1559         if (!NT_STATUS_IS_OK(status)) {
1560                 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1561                 return status;
1562         }
1563         TALLOC_FREE(sd);
1564         return NT_STATUS_OK;
1565 }
1566
1567 static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
1568 {
1569         int flags;
1570         int ret;
1571         mode_t mode;
1572         files_struct *fsp;
1573         struct smb_filename *smb_fname = NULL;
1574         NTSTATUS status;
1575         struct security_descriptor *sd = NULL;
1576
1577         if (argc != 3) {
1578                 printf("Usage: set_nt_acl <file> <sddl>\n");
1579                 return NT_STATUS_OK;
1580         }
1581
1582         mode = 00400;
1583
1584         fsp = talloc_zero(vfs, struct files_struct);
1585         if (fsp == NULL) {
1586                 return NT_STATUS_NO_MEMORY;
1587         }
1588         fsp->fh = talloc_zero(fsp, struct fd_handle);
1589         if (fsp->fh == NULL) {
1590                 TALLOC_FREE(fsp);
1591                 return NT_STATUS_NO_MEMORY;
1592         }
1593         fsp->conn = vfs->conn;
1594
1595         smb_fname = synthetic_smb_fname_split(NULL,
1596                                         argv[1],
1597                                         lp_posix_pathnames());
1598         if (smb_fname == NULL) {
1599                 TALLOC_FREE(fsp);
1600                 return NT_STATUS_NO_MEMORY;
1601         }
1602
1603         fsp->fsp_name = smb_fname;
1604
1605 #ifdef O_DIRECTORY
1606         flags = O_RDONLY|O_DIRECTORY;
1607 #else
1608         /* POSIX allows us to open a directory with O_RDONLY. */
1609         flags = O_RDONLY;
1610 #endif
1611
1612         fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, O_RDWR, mode);
1613         if (fsp->fh->fd == -1 && errno == EISDIR) {
1614                 fsp->fh->fd = SMB_VFS_OPEN(vfs->conn, smb_fname, fsp, flags, mode);
1615         }
1616         if (fsp->fh->fd == -1) {
1617                 printf("open: error=%d (%s)\n", errno, strerror(errno));
1618                 TALLOC_FREE(fsp);
1619                 TALLOC_FREE(smb_fname);
1620                 return NT_STATUS_UNSUCCESSFUL;
1621         }
1622
1623         status = NT_STATUS_OK;
1624         ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
1625         if (ret == -1) {
1626                 /* If we have an fd, this stat should succeed. */
1627                 DEBUG(0,("Error doing fstat on open file %s "
1628                          "(%s)\n",
1629                          smb_fname_str_dbg(smb_fname),
1630                          strerror(errno) ));
1631                 status = map_nt_error_from_unix(errno);
1632         }
1633         
1634         if (!NT_STATUS_IS_OK(status)) {
1635                 goto out;
1636         }
1637
1638         fsp->file_id = vfs_file_id_from_sbuf(vfs->conn, &smb_fname->st);
1639         fsp->vuid = UID_FIELD_INVALID;
1640         fsp->file_pid = 0;
1641         fsp->can_lock = True;
1642         fsp->can_read = True;
1643         fsp->can_write = True;
1644         fsp->print_file = NULL;
1645         fsp->modified = False;
1646         fsp->sent_oplock_break = NO_BREAK_SENT;
1647         fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
1648
1649
1650         sd = sddl_decode(talloc_tos(), argv[2], get_global_sam_sid());
1651         if (!sd) {
1652                 printf("sddl_decode failed to parse %s as SDDL\n", argv[2]);
1653                 status = NT_STATUS_INVALID_PARAMETER;
1654                 goto out;
1655         }
1656
1657         status = SMB_VFS_FSET_NT_ACL(fsp, SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL, sd);
1658         if (!NT_STATUS_IS_OK(status)) {
1659                 printf("fset_nt_acl returned (%s)\n", nt_errstr(status));
1660                 goto out;
1661         }
1662 out:
1663         TALLOC_FREE(sd);
1664
1665         ret = SMB_VFS_CLOSE(fsp);
1666         if (ret == -1 )
1667                 printf("close: error=%d (%s)\n", errno, strerror(errno));
1668
1669         TALLOC_FREE(fsp);
1670
1671         return status;
1672 }
1673
1674
1675
1676 static NTSTATUS cmd_sys_acl_get_fd(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1677                                    int argc, const char **argv)
1678 {
1679         int fd;
1680         SMB_ACL_T acl;
1681         char *acl_text;
1682
1683         if (argc != 2) {
1684                 printf("Usage: sys_acl_get_fd <fd>\n");
1685                 return NT_STATUS_OK;
1686         }
1687
1688         fd = atoi(argv[1]);
1689         if (fd < 0 || fd >= 1024) {
1690                 printf("sys_acl_get_fd: error=%d (file descriptor out of range)\n", EBADF);
1691                 return NT_STATUS_OK;
1692         }
1693         if (vfs->files[fd] == NULL) {
1694                 printf("sys_acl_get_fd: error=%d (invalid file descriptor)\n", EBADF);
1695                 return NT_STATUS_OK;
1696         }
1697
1698         acl = SMB_VFS_SYS_ACL_GET_FD(vfs->files[fd], talloc_tos());
1699         if (!acl) {
1700                 printf("sys_acl_get_fd failed (%s)\n", strerror(errno));
1701                 return NT_STATUS_UNSUCCESSFUL;
1702         }
1703         acl_text = sys_acl_to_text(acl, NULL);
1704         printf("%s", acl_text);
1705         TALLOC_FREE(acl);
1706         SAFE_FREE(acl_text);
1707         return NT_STATUS_OK;
1708 }
1709
1710 static NTSTATUS cmd_sys_acl_get_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1711                                      int argc, const char **argv)
1712 {
1713         SMB_ACL_T acl;
1714         char *acl_text;
1715         int type;
1716         struct smb_filename *smb_fname = NULL;
1717
1718         if (argc != 3) {
1719                 printf("Usage: sys_acl_get_file <path> <type>\n");
1720                 return NT_STATUS_OK;
1721         }
1722
1723         smb_fname = synthetic_smb_fname_split(talloc_tos(),
1724                                         argv[1],
1725                                         lp_posix_pathnames());
1726         if (smb_fname == NULL) {
1727                 return NT_STATUS_NO_MEMORY;
1728         }
1729         type = atoi(argv[2]);
1730         acl = SMB_VFS_SYS_ACL_GET_FILE(vfs->conn, smb_fname,
1731                                 type, talloc_tos());
1732         if (!acl) {
1733                 printf("sys_acl_get_file failed (%s)\n", strerror(errno));
1734                 return NT_STATUS_UNSUCCESSFUL;
1735         }
1736         acl_text = sys_acl_to_text(acl, NULL);
1737         printf("%s", acl_text);
1738         TALLOC_FREE(acl);
1739         SAFE_FREE(acl_text);
1740         return NT_STATUS_OK;
1741 }
1742
1743 static NTSTATUS cmd_sys_acl_blob_get_file(struct vfs_state *vfs,
1744                                           TALLOC_CTX *mem_ctx,
1745                                           int argc, const char **argv)
1746 {
1747         char *description;
1748         DATA_BLOB blob;
1749         int ret;
1750         size_t i;
1751         struct smb_filename *smb_fname = NULL;
1752
1753         if (argc != 2) {
1754                 printf("Usage: sys_acl_get_file <path>\n");
1755                 return NT_STATUS_OK;
1756         }
1757
1758         smb_fname = synthetic_smb_fname_split(talloc_tos(),
1759                                         argv[1],
1760                                         lp_posix_pathnames());
1761         if (smb_fname == NULL) {
1762                 return NT_STATUS_NO_MEMORY;
1763         }
1764         ret = SMB_VFS_SYS_ACL_BLOB_GET_FILE(vfs->conn, smb_fname, talloc_tos(),
1765                                             &description, &blob);
1766         if (ret != 0) {
1767                 printf("sys_acl_blob_get_file failed (%s)\n", strerror(errno));
1768                 return map_nt_error_from_unix(errno);
1769         }
1770         printf("Description: %s\n", description);
1771         for (i = 0; i < blob.length; i++) {
1772                 printf("%.2x ", blob.data[i]);
1773         }
1774         printf("\n");
1775
1776         return NT_STATUS_OK;
1777 }
1778
1779 static NTSTATUS cmd_sys_acl_blob_get_fd(struct vfs_state *vfs,
1780                                         TALLOC_CTX *mem_ctx,
1781                                         int argc, const char **argv)
1782 {
1783         int fd;
1784         char *description;
1785         DATA_BLOB blob;
1786         int ret;
1787         size_t i;
1788
1789         if (argc != 2) {
1790                 printf("Usage: sys_acl_blob_get_fd <fd>\n");
1791                 return NT_STATUS_OK;
1792         }
1793
1794         fd = atoi(argv[1]);
1795         if (fd < 0 || fd >= 1024) {
1796                 printf("sys_acl_blob_get_fd: error=%d "
1797                        "(file descriptor out of range)\n", EBADF);
1798                 return NT_STATUS_OK;
1799         }
1800         if (vfs->files[fd] == NULL) {
1801                 printf("sys_acl_blob_get_fd: error=%d "
1802                        "(invalid file descriptor)\n", EBADF);
1803                 return NT_STATUS_OK;
1804         }
1805
1806         ret = SMB_VFS_SYS_ACL_BLOB_GET_FD(vfs->files[fd], talloc_tos(),
1807                                           &description, &blob);
1808         if (ret != 0) {
1809                 printf("sys_acl_blob_get_fd failed (%s)\n", strerror(errno));
1810                 return map_nt_error_from_unix(errno);
1811         }
1812         printf("Description: %s\n", description);
1813         for (i = 0; i < blob.length; i++) {
1814                 printf("%.2x ", blob.data[i]);
1815         }
1816         printf("\n");
1817
1818         return NT_STATUS_OK;
1819 }
1820
1821
1822
1823 static NTSTATUS cmd_sys_acl_delete_def_file(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1824                                             int argc, const char **argv)
1825 {
1826         int ret;
1827         struct smb_filename *smb_fname = NULL;
1828
1829         if (argc != 2) {
1830                 printf("Usage: sys_acl_delete_def_file <path>\n");
1831                 return NT_STATUS_OK;
1832         }
1833
1834         smb_fname = synthetic_smb_fname(talloc_tos(),
1835                                         argv[1],
1836                                         NULL,
1837                                         NULL,
1838                                         ssf_flags());
1839
1840         if (smb_fname == NULL) {
1841                 return NT_STATUS_NO_MEMORY;
1842         }
1843         ret = SMB_VFS_SYS_ACL_DELETE_DEF_FILE(vfs->conn, smb_fname);
1844         if (ret == -1) {
1845                 printf("sys_acl_delete_def_file failed (%s)\n", strerror(errno));
1846                 TALLOC_FREE(smb_fname);
1847                 return NT_STATUS_UNSUCCESSFUL;
1848         }
1849         TALLOC_FREE(smb_fname);
1850         return NT_STATUS_OK;
1851 }
1852
1853 /* Afaik translate name was first introduced with vfs_catia, to be able
1854    to translate unix file/dir-names, containing invalid windows characters,
1855    to valid windows names.
1856    The used translation direction is always unix --> windows
1857 */
1858 static NTSTATUS cmd_translate_name(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
1859                                             int argc, const char **argv)
1860 {
1861         int ret;
1862         struct dirent *dent = NULL;
1863         SMB_STRUCT_STAT st;
1864         bool found = false;
1865         char *translated = NULL;
1866         struct smb_filename *smb_fname = NULL;
1867         NTSTATUS status;
1868
1869         if (argc != 2) {
1870                 DEBUG(0, ("Usage: translate_name unix_filename\n"));
1871                 return NT_STATUS_UNSUCCESSFUL;
1872         }
1873
1874         smb_fname = synthetic_smb_fname(talloc_tos(),
1875                                         ".",
1876                                         NULL,
1877                                         NULL,
1878                                         ssf_flags());
1879         if (smb_fname == NULL) {
1880                 return NT_STATUS_NO_MEMORY;
1881         }
1882
1883         vfs->currentdir = SMB_VFS_OPENDIR(vfs->conn, smb_fname, NULL, 0);
1884         if (vfs->currentdir == NULL) {
1885                 DEBUG(0, ("cmd_translate_name: opendir error=%d (%s)\n",
1886                           errno, strerror(errno)));
1887                 TALLOC_FREE(smb_fname);
1888                 return NT_STATUS_UNSUCCESSFUL;
1889         }
1890
1891         while (true) {
1892                 dent = SMB_VFS_READDIR(vfs->conn, vfs->currentdir, &st);
1893                 if (dent == NULL) {
1894                         break;
1895                 }
1896                 if (strcmp (dent->d_name, argv[1]) == 0) {
1897                         found = true;
1898                         break;
1899                 }
1900         };
1901
1902         if (!found) {
1903                 DEBUG(0, ("cmd_translate_name: file '%s' not found.\n", 
1904                           argv[1]));
1905                 status = NT_STATUS_UNSUCCESSFUL;
1906                 goto cleanup;
1907         }
1908         status = SMB_VFS_TRANSLATE_NAME(vfs->conn, dent->d_name,
1909                                         vfs_translate_to_windows,
1910                                         talloc_tos(), &translated);
1911         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
1912                 DEBUG(0, ("cmd_translate_name: file '%s' cannot be "
1913                           "translated\n", argv[1]));
1914                 TALLOC_FREE(translated);
1915                 goto cleanup;
1916         }
1917         /* translation success. But that could also mean
1918            that translating "aaa" to "aaa" was successful :-(
1919         */ 
1920         DEBUG(0, ("cmd_translate_name: file '%s' --> '%s'\n", 
1921                   argv[1], translated));
1922
1923         TALLOC_FREE(smb_fname);
1924         TALLOC_FREE(translated);
1925
1926 cleanup:
1927         TALLOC_FREE(smb_fname);
1928         ret = SMB_VFS_CLOSEDIR(vfs->conn, vfs->currentdir);
1929         if (ret == -1) {
1930                 DEBUG(0, ("cmd_translate_name: closedir failure: %s\n",
1931                           strerror(errno)));
1932                 return NT_STATUS_UNSUCCESSFUL;
1933         }
1934
1935         vfs->currentdir = NULL;
1936         return status;;
1937 }
1938
1939
1940 struct cmd_set vfs_commands[] = {
1941
1942         { .name = "VFS Commands" },
1943
1944         { "load", cmd_load_module, "Load a module", "load <module.so>" },
1945         { "populate", cmd_populate, "Populate a data buffer", "populate <char> <size>" },
1946         { "showdata", cmd_show_data, "Show data currently in data buffer", "show_data [<offset> <len>]"},
1947         { "connect",   cmd_connect,   "VFS connect()",    "connect" },
1948         { "disconnect",   cmd_disconnect,   "VFS disconnect()",    "disconnect" },
1949         { "disk_free",   cmd_disk_free,   "VFS disk_free()",    "disk_free <path>" },
1950         { "opendir",   cmd_opendir,   "VFS opendir()",    "opendir <fname>" },
1951         { "readdir",   cmd_readdir,   "VFS readdir()",    "readdir" },
1952         { "mkdir",   cmd_mkdir,   "VFS mkdir()",    "mkdir <path>" },
1953         { "rmdir",   cmd_pathfunc,   "VFS rmdir()",    "rmdir <path>" },
1954         { "closedir",   cmd_closedir,   "VFS closedir()",    "closedir" },
1955         { "open",   cmd_open,   "VFS open()",    "open <fname> <flags> <mode>" },
1956         { "close",   cmd_close,   "VFS close()",    "close <fd>" },
1957         { "read",   cmd_read,   "VFS read()",    "read <fd> <size>" },
1958         { "write",   cmd_write,   "VFS write()",    "write <fd> <size>" },
1959         { "lseek",   cmd_lseek,   "VFS lseek()",    "lseek <fd> <offset> <whence>" },
1960         { "rename",   cmd_rename,   "VFS rename()",    "rename <old> <new>" },
1961         { "fsync",   cmd_fsync,   "VFS fsync()",    "fsync <fd>" },
1962         { "stat",   cmd_stat,   "VFS stat()",    "stat <fname>" },
1963         { "fstat",   cmd_fstat,   "VFS fstat()",    "fstat <fd>" },
1964         { "lstat",   cmd_lstat,   "VFS lstat()",    "lstat <fname>" },
1965         { "unlink",   cmd_pathfunc,   "VFS unlink()",    "unlink <fname>" },
1966         { "chmod",   cmd_chmod,   "VFS chmod()",    "chmod <path> <mode>" },
1967         { "fchmod",   cmd_fchmod,   "VFS fchmod()",    "fchmod <fd> <mode>" },
1968         { "chown",   cmd_chown,   "VFS chown()",    "chown <path> <uid> <gid>" },
1969         { "fchown",   cmd_fchown,   "VFS fchown()",    "fchown <fd> <uid> <gid>" },
1970         { "chdir",   cmd_pathfunc,   "VFS chdir()",    "chdir <path>" },
1971         { "getwd",   cmd_getwd,   "VFS getwd()",    "getwd" },
1972         { "utime",   cmd_utime,   "VFS utime()",    "utime <path> <access> <modify>" },
1973         { "ftruncate",   cmd_ftruncate,   "VFS ftruncate()",    "ftruncate <fd> <length>" },
1974         { "lock",   cmd_lock,   "VFS lock()",    "lock <f> <op> <offset> <count> <type>" },
1975         { "symlink",   cmd_symlink,   "VFS symlink()",    "symlink <old> <new>" },
1976         { "readlink",   cmd_readlink,   "VFS readlink()",    "readlink <path>" },
1977         { "link",   cmd_link,   "VFS link()",    "link <oldpath> <newpath>" },
1978         { "mknod",   cmd_mknod,   "VFS mknod()",    "mknod <path> <mode> <dev>" },
1979         { "realpath",   cmd_realpath,   "VFS realpath()",    "realpath <path>" },
1980         { "getxattr", cmd_getxattr, "VFS getxattr()",
1981           "getxattr <path> <name>" },
1982         { "listxattr", cmd_listxattr, "VFS listxattr()",
1983           "listxattr <path>" },
1984         { "setxattr", cmd_setxattr, "VFS setxattr()",
1985           "setxattr <path> <name> <value> [<flags>]" },
1986         { "removexattr", cmd_removexattr, "VFS removexattr()",
1987           "removexattr <path> <name>\n" },
1988         { "fget_nt_acl", cmd_fget_nt_acl, "VFS fget_nt_acl()", 
1989           "fget_nt_acl <fd>\n" },
1990         { "get_nt_acl", cmd_get_nt_acl, "VFS get_nt_acl()", 
1991           "get_nt_acl <path>\n" },
1992         { "fset_nt_acl", cmd_fset_nt_acl, "VFS fset_nt_acl()", 
1993           "fset_nt_acl <fd>\n" },
1994         { "set_nt_acl", cmd_set_nt_acl, "VFS open() and fset_nt_acl()", 
1995           "set_nt_acl <file>\n" },
1996         { "sys_acl_get_file", cmd_sys_acl_get_file, "VFS sys_acl_get_file()", "sys_acl_get_file <path>" },
1997         { "sys_acl_get_fd", cmd_sys_acl_get_fd, "VFS sys_acl_get_fd()", "sys_acl_get_fd <fd>" },
1998         { "sys_acl_blob_get_file", cmd_sys_acl_blob_get_file,
1999           "VFS sys_acl_blob_get_file()", "sys_acl_blob_get_file <path>" },
2000         { "sys_acl_blob_get_fd", cmd_sys_acl_blob_get_fd,
2001           "VFS sys_acl_blob_get_fd()", "sys_acl_blob_get_fd <path>" },
2002         { "sys_acl_delete_def_file", cmd_sys_acl_delete_def_file, "VFS sys_acl_delete_def_file()", "sys_acl_delete_def_file <path>" },
2003
2004
2005         { "test_chain", cmd_test_chain, "test chain code",
2006           "test_chain" },
2007         { "translate_name", cmd_translate_name, "VFS translate_name()", "translate_name unix_filename" },
2008         { NULL }
2009 };