532e2884426de66a9f2eac1583f3590e7872af0f
[bbaumbach/samba-autobuild/.git] / source3 / client / client.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB client
4    Copyright (C) Andrew Tridgell          1994-1998
5    Copyright (C) Simo Sorce               2001-2002
6    Copyright (C) Jelmer Vernooij          2003
7    Copyright (C) Gerald (Jerry) Carter    2004
8    Copyright (C) Jeremy Allison           1994-2007
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "popt_common.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "client/client_proto.h"
29 #include "client/clitar_proto.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
31 #include "../lib/util/select.h"
32 #include "system/readline.h"
33 #include "../libcli/smbreadline/smbreadline.h"
34 #include "../libcli/security/security.h"
35 #include "system/select.h"
36 #include "libsmb/libsmb.h"
37 #include "libsmb/clirap.h"
38 #include "trans2.h"
39 #include "libsmb/nmblib.h"
40 #include "include/ntioctl.h"
41 #include "../libcli/smb/smbXcli_base.h"
42
43 #ifndef REGISTER
44 #define REGISTER 0
45 #endif
46
47 extern int do_smb_browse(void); /* mDNS browsing */
48
49 extern bool override_logfile;
50
51 static int port = 0;
52 static char *service;
53 static char *desthost;
54 static bool grepable = false;
55 static char *cmdstr = NULL;
56 const char *cmd_ptr = NULL;
57
58 static int io_bufsize = 0; /* we use the default size */
59 static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
60
61 static int name_type = 0x20;
62 static int max_protocol = -1;
63
64 static int process_tok(char *tok);
65 static int cmd_help(void);
66
67 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
68
69 /* value for unused fid field in trans2 secondary request */
70 #define FID_UNUSED (0xFFFF)
71
72 time_t newer_than = 0;
73 static int archive_level = 0;
74
75 static bool translation = false;
76 static bool have_ip;
77
78 static bool prompt = true;
79
80 static bool recurse = false;
81 static bool showacls = false;
82 bool lowercase = false;
83 static bool backup_intent = false;
84
85 static struct sockaddr_storage dest_ss;
86 static char dest_ss_str[INET6_ADDRSTRLEN];
87
88 #define SEPARATORS " \t\n\r"
89
90 static bool abort_mget = true;
91
92 /* timing globals */
93 uint64_t get_total_size = 0;
94 unsigned int get_total_time_ms = 0;
95 static uint64_t put_total_size = 0;
96 static unsigned int put_total_time_ms = 0;
97
98 /* totals globals */
99 static double dir_total;
100
101 /* encrypted state. */
102 static bool smb_encrypt;
103
104 /* root cli_state connection */
105
106 struct cli_state *cli;
107
108 static char CLI_DIRSEP_CHAR = '\\';
109 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
110
111 /* Accessor functions for directory paths. */
112 static char *fileselection;
113 static const char *client_get_fileselection(void)
114 {
115         if (fileselection) {
116                 return fileselection;
117         }
118         return "";
119 }
120
121 static const char *client_set_fileselection(const char *new_fs)
122 {
123         SAFE_FREE(fileselection);
124         if (new_fs) {
125                 fileselection = SMB_STRDUP(new_fs);
126         }
127         return client_get_fileselection();
128 }
129
130 static char *cwd;
131 static const char *client_get_cwd(void)
132 {
133         if (cwd) {
134                 return cwd;
135         }
136         return CLI_DIRSEP_STR;
137 }
138
139 static const char *client_set_cwd(const char *new_cwd)
140 {
141         SAFE_FREE(cwd);
142         if (new_cwd) {
143                 cwd = SMB_STRDUP(new_cwd);
144         }
145         return client_get_cwd();
146 }
147
148 static char *cur_dir;
149 const char *client_get_cur_dir(void)
150 {
151         if (cur_dir) {
152                 return cur_dir;
153         }
154         return CLI_DIRSEP_STR;
155 }
156
157 const char *client_set_cur_dir(const char *newdir)
158 {
159         SAFE_FREE(cur_dir);
160         if (newdir) {
161                 cur_dir = SMB_STRDUP(newdir);
162         }
163         return client_get_cur_dir();
164 }
165
166 /****************************************************************************
167  Put up a yes/no prompt.
168 ****************************************************************************/
169
170 static bool yesno(const char *p)
171 {
172         char ans[20];
173         printf("%s",p);
174
175         if (!fgets(ans,sizeof(ans)-1,stdin))
176                 return(False);
177
178         if (*ans == 'y' || *ans == 'Y')
179                 return(True);
180
181         return(False);
182 }
183
184 /****************************************************************************
185  Write to a local file with CR/LF->LF translation if appropriate. Return the
186  number taken from the buffer. This may not equal the number written.
187 ****************************************************************************/
188
189 static int writefile(int f, char *b, int n)
190 {
191         int i;
192
193         if (!translation) {
194                 return write(f,b,n);
195         }
196
197         i = 0;
198         while (i < n) {
199                 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
200                         b++;i++;
201                 }
202                 if (write(f, b, 1) != 1) {
203                         break;
204                 }
205                 b++;
206                 i++;
207         }
208
209         return(i);
210 }
211
212 /****************************************************************************
213  Read from a file with LF->CR/LF translation if appropriate. Return the
214  number read. read approx n bytes.
215 ****************************************************************************/
216
217 static int readfile(uint8_t *b, int n, FILE *f)
218 {
219         int i;
220         int c;
221
222         if (!translation)
223                 return fread(b,1,n,f);
224
225         i = 0;
226         while (i < (n - 1)) {
227                 if ((c = getc(f)) == EOF) {
228                         break;
229                 }
230
231                 if (c == '\n') { /* change all LFs to CR/LF */
232                         b[i++] = '\r';
233                 }
234
235                 b[i++] = c;
236         }
237
238         return(i);
239 }
240
241 struct push_state {
242         FILE *f;
243         off_t nread;
244 };
245
246 static size_t push_source(uint8_t *buf, size_t n, void *priv)
247 {
248         struct push_state *state = (struct push_state *)priv;
249         int result;
250
251         if (feof(state->f)) {
252                 return 0;
253         }
254
255         result = readfile(buf, n, state->f);
256         state->nread += result;
257         return result;
258 }
259
260 /****************************************************************************
261  Send a message.
262 ****************************************************************************/
263
264 static void send_message(const char *username)
265 {
266         char buf[1600];
267         NTSTATUS status;
268         int i;
269
270         d_printf("Type your message, ending it with a Control-D\n");
271
272         i = 0;
273         while (i<sizeof(buf)-2) {
274                 int c = fgetc(stdin);
275                 if (c == EOF) {
276                         break;
277                 }
278                 if (c == '\n') {
279                         buf[i++] = '\r';
280                 }
281                 buf[i++] = c;
282         }
283         buf[i] = '\0';
284
285         status = cli_message(cli, desthost, username, buf);
286         if (!NT_STATUS_IS_OK(status)) {
287                 d_fprintf(stderr, "cli_message returned %s\n",
288                           nt_errstr(status));
289         }
290 }
291
292 /****************************************************************************
293  Check the space on a device.
294 ****************************************************************************/
295
296 static int do_dskattr(void)
297 {
298         uint64_t total, bsize, avail;
299         struct cli_state *targetcli = NULL;
300         char *targetpath = NULL;
301         TALLOC_CTX *ctx = talloc_tos();
302         NTSTATUS status;
303
304         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(), cli,
305                                   client_get_cur_dir(), &targetcli,
306                                   &targetpath);
307         if (!NT_STATUS_IS_OK(status)) {
308                 d_printf("Error in dskattr: %s\n", nt_errstr(status));
309                 return 1;
310         }
311
312         status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
313         if (!NT_STATUS_IS_OK(status)) {
314                 d_printf("Error in dskattr: %s\n", nt_errstr(status));
315                 return 1;
316         }
317
318         d_printf("\n\t\t%" PRIu64
319                 " blocks of size %" PRIu64
320                 ". %" PRIu64 " blocks available\n",
321                 total, bsize, avail);
322
323         return 0;
324 }
325
326 /****************************************************************************
327  Show cd/pwd.
328 ****************************************************************************/
329
330 static int cmd_pwd(void)
331 {
332         d_printf("Current directory is %s",service);
333         d_printf("%s\n",client_get_cur_dir());
334         return 0;
335 }
336
337 /****************************************************************************
338  Ensure name has correct directory separators.
339 ****************************************************************************/
340
341 static void normalize_name(char *newdir)
342 {
343         if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
344                 string_replace(newdir,'/','\\');
345         }
346 }
347
348 /****************************************************************************
349  Change directory - inner section.
350 ****************************************************************************/
351
352 static int do_cd(const char *new_dir)
353 {
354         char *newdir = NULL;
355         char *saved_dir = NULL;
356         char *new_cd = NULL;
357         char *targetpath = NULL;
358         struct cli_state *targetcli = NULL;
359         SMB_STRUCT_STAT sbuf;
360         uint32_t attributes;
361         int ret = 1;
362         TALLOC_CTX *ctx = talloc_stackframe();
363         NTSTATUS status;
364
365         newdir = talloc_strdup(ctx, new_dir);
366         if (!newdir) {
367                 TALLOC_FREE(ctx);
368                 return 1;
369         }
370
371         normalize_name(newdir);
372
373         /* Save the current directory in case the new directory is invalid */
374
375         saved_dir = talloc_strdup(ctx, client_get_cur_dir());
376         if (!saved_dir) {
377                 TALLOC_FREE(ctx);
378                 return 1;
379         }
380
381         if (*newdir == CLI_DIRSEP_CHAR) {
382                 client_set_cur_dir(newdir);
383                 new_cd = newdir;
384         } else {
385                 new_cd = talloc_asprintf(ctx, "%s%s",
386                                 client_get_cur_dir(),
387                                 newdir);
388                 if (!new_cd) {
389                         goto out;
390                 }
391         }
392
393         /* Ensure cur_dir ends in a DIRSEP */
394         if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
395                 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
396                 if (!new_cd) {
397                         goto out;
398                 }
399         }
400         client_set_cur_dir(new_cd);
401
402         new_cd = clean_name(ctx, new_cd);
403         client_set_cur_dir(new_cd);
404
405         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
406                                 cli, new_cd, &targetcli, &targetpath);
407         if (!NT_STATUS_IS_OK(status)) {
408                 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
409                 client_set_cur_dir(saved_dir);
410                 goto out;
411         }
412
413         if (strequal(targetpath,CLI_DIRSEP_STR )) {
414                 TALLOC_FREE(ctx);
415                 return 0;
416         }
417
418         /* Use a trans2_qpathinfo to test directories for modern servers.
419            Except Win9x doesn't support the qpathinfo_basic() call..... */
420
421         if (smbXcli_conn_protocol(targetcli->conn) > PROTOCOL_LANMAN2 && !targetcli->win95) {
422
423                 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
424                                              &attributes);
425                 if (!NT_STATUS_IS_OK(status)) {
426                         d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
427                         client_set_cur_dir(saved_dir);
428                         goto out;
429                 }
430
431                 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
432                         d_printf("cd %s: not a directory\n", new_cd);
433                         client_set_cur_dir(saved_dir);
434                         goto out;
435                 }
436         } else {
437
438                 targetpath = talloc_asprintf(ctx,
439                                 "%s%s",
440                                 targetpath,
441                                 CLI_DIRSEP_STR );
442                 if (!targetpath) {
443                         client_set_cur_dir(saved_dir);
444                         goto out;
445                 }
446                 targetpath = clean_name(ctx, targetpath);
447                 if (!targetpath) {
448                         client_set_cur_dir(saved_dir);
449                         goto out;
450                 }
451
452                 status = cli_chkpath(targetcli, targetpath);
453                 if (!NT_STATUS_IS_OK(status)) {
454                         d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
455                         client_set_cur_dir(saved_dir);
456                         goto out;
457                 }
458         }
459
460         ret = 0;
461
462 out:
463
464         TALLOC_FREE(ctx);
465         return ret;
466 }
467
468 /****************************************************************************
469  Change directory.
470 ****************************************************************************/
471
472 static int cmd_cd(void)
473 {
474         char *buf = NULL;
475         int rc = 0;
476
477         if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
478                 rc = do_cd(buf);
479         } else {
480                 d_printf("Current directory is %s\n",client_get_cur_dir());
481         }
482
483         return rc;
484 }
485
486 /****************************************************************************
487  Change directory.
488 ****************************************************************************/
489
490 static int cmd_cd_oneup(void)
491 {
492         return do_cd("..");
493 }
494
495 /*******************************************************************
496  Decide if a file should be operated on.
497 ********************************************************************/
498
499 static bool do_this_one(struct file_info *finfo)
500 {
501         if (!finfo->name) {
502                 return false;
503         }
504
505         if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
506                 return true;
507         }
508
509         if (*client_get_fileselection() &&
510             !mask_match(finfo->name,client_get_fileselection(),false)) {
511                 DEBUG(3,("mask_match %s failed\n", finfo->name));
512                 return false;
513         }
514
515         if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
516                 DEBUG(3,("newer_than %s failed\n", finfo->name));
517                 return false;
518         }
519
520         if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
521                 DEBUG(3,("archive %s failed\n", finfo->name));
522                 return false;
523         }
524
525         return true;
526 }
527
528 /****************************************************************************
529  Display info about a file.
530 ****************************************************************************/
531
532 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
533                           const char *dir)
534 {
535         time_t t;
536         TALLOC_CTX *ctx = talloc_tos();
537         NTSTATUS status = NT_STATUS_OK;
538
539         if (!do_this_one(finfo)) {
540                 return NT_STATUS_OK;
541         }
542
543         t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
544         if (!showacls) {
545                 d_printf("  %-30s%7.7s %8.0f  %s",
546                          finfo->name,
547                          attrib_string(talloc_tos(), finfo->mode),
548                         (double)finfo->size,
549                         time_to_asc(t));
550                 dir_total += finfo->size;
551         } else {
552                 char *afname = NULL;
553                 uint16_t fnum;
554
555                 /* skip if this is . or .. */
556                 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
557                         return NT_STATUS_OK;
558                 /* create absolute filename for cli_ntcreate() FIXME */
559                 afname = talloc_asprintf(ctx,
560                                         "%s%s%s",
561                                         dir,
562                                         CLI_DIRSEP_STR,
563                                         finfo->name);
564                 if (!afname) {
565                         return NT_STATUS_NO_MEMORY;
566                 }
567                 /* print file meta date header */
568                 d_printf( "FILENAME:%s\n", finfo->name);
569                 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->mode));
570                 d_printf( "SIZE:%.0f\n", (double)finfo->size);
571                 d_printf( "MTIME:%s", time_to_asc(t));
572                 status = cli_ntcreate(cli_state, afname, 0,
573                                       CREATE_ACCESS_READ, 0,
574                                       FILE_SHARE_READ|FILE_SHARE_WRITE,
575                                       FILE_OPEN, 0x0, 0x0, &fnum, NULL);
576                 if (!NT_STATUS_IS_OK(status)) {
577                         DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
578                                    afname, nt_errstr(status)));
579                 } else {
580                         struct security_descriptor *sd = NULL;
581                         status = cli_query_secdesc(cli_state, fnum,
582                                                    ctx, &sd);
583                         if (!NT_STATUS_IS_OK(status)) {
584                                 DEBUG( 0, ("display_finfo() failed to "
585                                            "get security descriptor: %s",
586                                            nt_errstr(status)));
587                         } else {
588                                 display_sec_desc(sd);
589                         }
590                         TALLOC_FREE(sd);
591                 }
592                 TALLOC_FREE(afname);
593         }
594         return status;
595 }
596
597 /****************************************************************************
598  Accumulate size of a file.
599 ****************************************************************************/
600
601 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
602                   const char *dir)
603 {
604         if (do_this_one(finfo)) {
605                 dir_total += finfo->size;
606         }
607         return NT_STATUS_OK;
608 }
609
610 static bool do_list_recurse;
611 static bool do_list_dirs;
612 static char *do_list_queue = 0;
613 static long do_list_queue_size = 0;
614 static long do_list_queue_start = 0;
615 static long do_list_queue_end = 0;
616 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
617                           const char *dir);
618
619 /****************************************************************************
620  Functions for do_list_queue.
621 ****************************************************************************/
622
623 /*
624  * The do_list_queue is a NUL-separated list of strings stored in a
625  * char*.  Since this is a FIFO, we keep track of the beginning and
626  * ending locations of the data in the queue.  When we overflow, we
627  * double the size of the char*.  When the start of the data passes
628  * the midpoint, we move everything back.  This is logically more
629  * complex than a linked list, but easier from a memory management
630  * angle.  In any memory error condition, do_list_queue is reset.
631  * Functions check to ensure that do_list_queue is non-NULL before
632  * accessing it.
633  */
634
635 static void reset_do_list_queue(void)
636 {
637         SAFE_FREE(do_list_queue);
638         do_list_queue_size = 0;
639         do_list_queue_start = 0;
640         do_list_queue_end = 0;
641 }
642
643 static void init_do_list_queue(void)
644 {
645         reset_do_list_queue();
646         do_list_queue_size = 1024;
647         do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
648         if (do_list_queue == 0) {
649                 d_printf("malloc fail for size %d\n",
650                          (int)do_list_queue_size);
651                 reset_do_list_queue();
652         } else {
653                 memset(do_list_queue, 0, do_list_queue_size);
654         }
655 }
656
657 static void adjust_do_list_queue(void)
658 {
659         /*
660          * If the starting point of the queue is more than half way through,
661          * move everything toward the beginning.
662          */
663
664         if (do_list_queue == NULL) {
665                 DEBUG(4,("do_list_queue is empty\n"));
666                 do_list_queue_start = do_list_queue_end = 0;
667                 return;
668         }
669
670         if (do_list_queue_start == do_list_queue_end) {
671                 DEBUG(4,("do_list_queue is empty\n"));
672                 do_list_queue_start = do_list_queue_end = 0;
673                 *do_list_queue = '\0';
674         } else if (do_list_queue_start > (do_list_queue_size / 2)) {
675                 DEBUG(4,("sliding do_list_queue backward\n"));
676                 memmove(do_list_queue,
677                         do_list_queue + do_list_queue_start,
678                         do_list_queue_end - do_list_queue_start);
679                 do_list_queue_end -= do_list_queue_start;
680                 do_list_queue_start = 0;
681         }
682 }
683
684 static void add_to_do_list_queue(const char *entry)
685 {
686         long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
687         while (new_end > do_list_queue_size) {
688                 do_list_queue_size *= 2;
689                 DEBUG(4,("enlarging do_list_queue to %d\n",
690                          (int)do_list_queue_size));
691                 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
692                 if (! do_list_queue) {
693                         d_printf("failure enlarging do_list_queue to %d bytes\n",
694                                  (int)do_list_queue_size);
695                         reset_do_list_queue();
696                 } else {
697                         memset(do_list_queue + do_list_queue_size / 2,
698                                0, do_list_queue_size / 2);
699                 }
700         }
701         if (do_list_queue) {
702                 strlcpy_base(do_list_queue + do_list_queue_end,
703                                  entry, do_list_queue, do_list_queue_size);
704                 do_list_queue_end = new_end;
705                 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
706                          entry, (int)do_list_queue_start, (int)do_list_queue_end));
707         }
708 }
709
710 static char *do_list_queue_head(void)
711 {
712         return do_list_queue + do_list_queue_start;
713 }
714
715 static void remove_do_list_queue_head(void)
716 {
717         if (do_list_queue_end > do_list_queue_start) {
718                 do_list_queue_start += strlen(do_list_queue_head()) + 1;
719                 adjust_do_list_queue();
720                 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
721                          (int)do_list_queue_start, (int)do_list_queue_end));
722         }
723 }
724
725 static int do_list_queue_empty(void)
726 {
727         return (! (do_list_queue && *do_list_queue));
728 }
729
730 /****************************************************************************
731  A helper for do_list.
732 ****************************************************************************/
733
734 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
735                            const char *mask, void *state)
736 {
737         struct cli_state *cli_state = (struct cli_state *)state;
738         TALLOC_CTX *ctx = talloc_tos();
739         char *dir = NULL;
740         char *dir_end = NULL;
741         NTSTATUS status = NT_STATUS_OK;
742
743         /* Work out the directory. */
744         dir = talloc_strdup(ctx, mask);
745         if (!dir) {
746                 return NT_STATUS_NO_MEMORY;
747         }
748         if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
749                 *dir_end = '\0';
750         }
751
752         if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
753                 if (do_list_dirs && do_this_one(f)) {
754                         status = do_list_fn(cli_state, f, dir);
755                         if (!NT_STATUS_IS_OK(status)) {
756                                 return status;
757                         }
758                 }
759                 if (do_list_recurse &&
760                     f->name &&
761                     !strequal(f->name,".") &&
762                     !strequal(f->name,"..")) {
763                         char *mask2 = NULL;
764                         char *p = NULL;
765
766                         if (!f->name[0]) {
767                                 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
768                                 TALLOC_FREE(dir);
769                                 return NT_STATUS_UNSUCCESSFUL;
770                         }
771
772                         mask2 = talloc_asprintf(ctx,
773                                         "%s%s",
774                                         mntpoint,
775                                         mask);
776                         if (!mask2) {
777                                 TALLOC_FREE(dir);
778                                 return NT_STATUS_NO_MEMORY;
779                         }
780                         p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
781                         if (p) {
782                                 p[1] = 0;
783                         } else {
784                                 mask2[0] = '\0';
785                         }
786                         mask2 = talloc_asprintf_append(mask2,
787                                         "%s%s*",
788                                         f->name,
789                                         CLI_DIRSEP_STR);
790                         if (!mask2) {
791                                 TALLOC_FREE(dir);
792                                 return NT_STATUS_NO_MEMORY;
793                         }
794                         add_to_do_list_queue(mask2);
795                         TALLOC_FREE(mask2);
796                 }
797                 TALLOC_FREE(dir);
798                 return NT_STATUS_OK;
799         }
800
801         if (do_this_one(f)) {
802                 status = do_list_fn(cli_state, f, dir);
803         }
804         TALLOC_FREE(dir);
805         return status;
806 }
807
808 /****************************************************************************
809  A wrapper around cli_list that adds recursion.
810 ****************************************************************************/
811
812 NTSTATUS do_list(const char *mask,
813                         uint16_t attribute,
814                         NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
815                                    const char *dir),
816                         bool rec,
817                         bool dirs)
818 {
819         static int in_do_list = 0;
820         TALLOC_CTX *ctx = talloc_tos();
821         struct cli_state *targetcli = NULL;
822         char *targetpath = NULL;
823         NTSTATUS ret_status = NT_STATUS_OK;
824         NTSTATUS status = NT_STATUS_OK;
825
826         if (in_do_list && rec) {
827                 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
828                 exit(1);
829         }
830
831         in_do_list = 1;
832
833         do_list_recurse = rec;
834         do_list_dirs = dirs;
835         do_list_fn = fn;
836
837         if (rec) {
838                 init_do_list_queue();
839                 add_to_do_list_queue(mask);
840
841                 while (!do_list_queue_empty()) {
842                         /*
843                          * Need to copy head so that it doesn't become
844                          * invalid inside the call to cli_list.  This
845                          * would happen if the list were expanded
846                          * during the call.
847                          * Fix from E. Jay Berkenbilt (ejb@ql.org)
848                          */
849                         char *head = talloc_strdup(ctx, do_list_queue_head());
850
851                         if (!head) {
852                                 return NT_STATUS_NO_MEMORY;
853                         }
854
855                         /* check for dfs */
856
857                         status = cli_resolve_path(ctx, "",
858                                         popt_get_cmdline_auth_info(),
859                                         cli, head, &targetcli, &targetpath);
860                         if (!NT_STATUS_IS_OK(status)) {
861                                 d_printf("do_list: [%s] %s\n", head,
862                                          nt_errstr(status));
863                                 remove_do_list_queue_head();
864                                 continue;
865                         }
866
867                         status = cli_list(targetcli, targetpath, attribute,
868                                  do_list_helper, targetcli);
869                         if (!NT_STATUS_IS_OK(status)) {
870                                 d_printf("%s listing %s\n",
871                                          nt_errstr(status), targetpath);
872                                 ret_status = status;
873                         }
874                         remove_do_list_queue_head();
875                         if ((! do_list_queue_empty()) && (fn == display_finfo)) {
876                                 char *next_file = do_list_queue_head();
877                                 char *save_ch = 0;
878                                 if ((strlen(next_file) >= 2) &&
879                                     (next_file[strlen(next_file) - 1] == '*') &&
880                                     (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
881                                         save_ch = next_file +
882                                                 strlen(next_file) - 2;
883                                         *save_ch = '\0';
884                                         if (showacls) {
885                                                 /* cwd is only used if showacls is on */
886                                                 client_set_cwd(next_file);
887                                         }
888                                 }
889                                 if (!showacls) /* don't disturbe the showacls output */
890                                         d_printf("\n%s\n",next_file);
891                                 if (save_ch) {
892                                         *save_ch = CLI_DIRSEP_CHAR;
893                                 }
894                         }
895                         TALLOC_FREE(head);
896                         TALLOC_FREE(targetpath);
897                 }
898         } else {
899                 /* check for dfs */
900                 status = cli_resolve_path(ctx, "",
901                                 popt_get_cmdline_auth_info(), cli, mask,
902                                   &targetcli, &targetpath);
903                 if (NT_STATUS_IS_OK(status)) {
904                         status = cli_list(targetcli, targetpath, attribute,
905                                           do_list_helper, targetcli);
906                         if (!NT_STATUS_IS_OK(status)) {
907                                 d_printf("%s listing %s\n",
908                                          nt_errstr(status), targetpath);
909                                 ret_status = status;
910                         }
911                         TALLOC_FREE(targetpath);
912                 } else {
913                         d_printf("do_list: [%s] %s\n", mask, nt_errstr(status));
914                         ret_status = status;
915                 }
916         }
917
918         in_do_list = 0;
919         reset_do_list_queue();
920         return ret_status;
921 }
922
923 /****************************************************************************
924  Get a directory listing.
925 ****************************************************************************/
926
927 static int cmd_dir(void)
928 {
929         TALLOC_CTX *ctx = talloc_tos();
930         uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
931         char *mask = NULL;
932         char *buf = NULL;
933         int rc = 1;
934         NTSTATUS status;
935
936         dir_total = 0;
937         mask = talloc_strdup(ctx, client_get_cur_dir());
938         if (!mask) {
939                 return 1;
940         }
941
942         if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
943                 normalize_name(buf);
944                 if (*buf == CLI_DIRSEP_CHAR) {
945                         mask = talloc_strdup(ctx, buf);
946                 } else {
947                         mask = talloc_asprintf_append(mask, "%s", buf);
948                 }
949         } else {
950                 mask = talloc_asprintf_append(mask, "*");
951         }
952         if (!mask) {
953                 return 1;
954         }
955
956         if (showacls) {
957                 /* cwd is only used if showacls is on */
958                 client_set_cwd(client_get_cur_dir());
959         }
960
961         status = do_list(mask, attribute, display_finfo, recurse, true);
962         if (!NT_STATUS_IS_OK(status)) {
963                 return 1;
964         }
965
966         rc = do_dskattr();
967
968         DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
969
970         return rc;
971 }
972
973 /****************************************************************************
974  Get a directory listing.
975 ****************************************************************************/
976
977 static int cmd_du(void)
978 {
979         TALLOC_CTX *ctx = talloc_tos();
980         uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
981         char *mask = NULL;
982         char *buf = NULL;
983         NTSTATUS status;
984         int rc = 1;
985
986         dir_total = 0;
987         mask = talloc_strdup(ctx, client_get_cur_dir());
988         if (!mask) {
989                 return 1;
990         }
991         if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
992                 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
993                 if (!mask) {
994                         return 1;
995                 }
996         }
997
998         if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
999                 normalize_name(buf);
1000                 if (*buf == CLI_DIRSEP_CHAR) {
1001                         mask = talloc_strdup(ctx, buf);
1002                 } else {
1003                         mask = talloc_asprintf_append(mask, "%s", buf);
1004                 }
1005         } else {
1006                 mask = talloc_strdup(ctx, "*");
1007         }
1008
1009         status = do_list(mask, attribute, do_du, recurse, true);
1010         if (!NT_STATUS_IS_OK(status)) {
1011                 return 1;
1012         }
1013
1014         rc = do_dskattr();
1015
1016         d_printf("Total number of bytes: %.0f\n", dir_total);
1017
1018         return rc;
1019 }
1020
1021 static int cmd_echo(void)
1022 {
1023         TALLOC_CTX *ctx = talloc_tos();
1024         char *num;
1025         char *data;
1026         NTSTATUS status;
1027
1028         if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1029             || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1030                 d_printf("echo <num> <data>\n");
1031                 return 1;
1032         }
1033
1034         status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1035
1036         if (!NT_STATUS_IS_OK(status)) {
1037                 d_printf("echo failed: %s\n", nt_errstr(status));
1038                 return 1;
1039         }
1040
1041         return 0;
1042 }
1043
1044 /****************************************************************************
1045  Get a file from rname to lname
1046 ****************************************************************************/
1047
1048 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1049 {
1050         int *pfd = (int *)priv;
1051         if (writefile(*pfd, buf, n) == -1) {
1052                 return map_nt_error_from_unix(errno);
1053         }
1054         return NT_STATUS_OK;
1055 }
1056
1057 static int do_get(const char *rname, const char *lname_in, bool reget)
1058 {
1059         TALLOC_CTX *ctx = talloc_tos();
1060         int handle = 0;
1061         uint16_t fnum;
1062         bool newhandle = false;
1063         struct timespec tp_start;
1064         uint16_t attr;
1065         off_t size;
1066         off_t start = 0;
1067         off_t nread = 0;
1068         int rc = 0;
1069         struct cli_state *targetcli = NULL;
1070         char *targetname = NULL;
1071         char *lname = NULL;
1072         NTSTATUS status;
1073
1074         lname = talloc_strdup(ctx, lname_in);
1075         if (!lname) {
1076                 return 1;
1077         }
1078
1079         if (lowercase) {
1080                 if (!strlower_m(lname)) {
1081                         d_printf("strlower_m %s failed\n", lname);
1082                         return 1;
1083                 }
1084         }
1085
1086         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1087                                 cli, rname, &targetcli, &targetname);
1088         if (!NT_STATUS_IS_OK(status)) {
1089                 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1090                 return 1;
1091         }
1092
1093         clock_gettime_mono(&tp_start);
1094
1095         status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1096         if (!NT_STATUS_IS_OK(status)) {
1097                 d_printf("%s opening remote file %s\n", nt_errstr(status),
1098                          rname);
1099                 return 1;
1100         }
1101
1102         if(!strcmp(lname,"-")) {
1103                 handle = fileno(stdout);
1104         } else {
1105                 if (reget) {
1106                         handle = open(lname, O_WRONLY|O_CREAT, 0644);
1107                         if (handle >= 0) {
1108                                 start = lseek(handle, 0, SEEK_END);
1109                                 if (start == -1) {
1110                                         d_printf("Error seeking local file\n");
1111                                         return 1;
1112                                 }
1113                         }
1114                 } else {
1115                         handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1116                 }
1117                 newhandle = true;
1118         }
1119         if (handle < 0) {
1120                 d_printf("Error opening local file %s\n",lname);
1121                 return 1;
1122         }
1123
1124
1125         status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1126                                      NULL, NULL, NULL);
1127         if (!NT_STATUS_IS_OK(status)) {
1128                 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1129                                       NULL);
1130                 if(!NT_STATUS_IS_OK(status)) {
1131                         d_printf("getattrib: %s\n", nt_errstr(status));
1132                         return 1;
1133                 }
1134         }
1135
1136         DEBUG(1,("getting file %s of size %.0f as %s ",
1137                  rname, (double)size, lname));
1138
1139         status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1140                           writefile_sink, (void *)&handle, &nread);
1141         if (!NT_STATUS_IS_OK(status)) {
1142                 d_fprintf(stderr, "parallel_read returned %s\n",
1143                           nt_errstr(status));
1144                 cli_close(targetcli, fnum);
1145                 return 1;
1146         }
1147
1148         status = cli_close(targetcli, fnum);
1149         if (!NT_STATUS_IS_OK(status)) {
1150                 d_printf("Error %s closing remote file\n", nt_errstr(status));
1151                 rc = 1;
1152         }
1153
1154         if (newhandle) {
1155                 close(handle);
1156         }
1157
1158         if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1159                 cli_setatr(cli, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
1160         }
1161
1162         {
1163                 struct timespec tp_end;
1164                 int this_time;
1165
1166                 clock_gettime_mono(&tp_end);
1167                 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1168                 get_total_time_ms += this_time;
1169                 get_total_size += nread;
1170
1171                 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1172                          nread / (1.024*this_time + 1.0e-4),
1173                          get_total_size / (1.024*get_total_time_ms)));
1174         }
1175
1176         TALLOC_FREE(targetname);
1177         return rc;
1178 }
1179
1180 /****************************************************************************
1181  Get a file.
1182 ****************************************************************************/
1183
1184 static int cmd_get(void)
1185 {
1186         TALLOC_CTX *ctx = talloc_tos();
1187         char *lname = NULL;
1188         char *rname = NULL;
1189         char *fname = NULL;
1190
1191         rname = talloc_strdup(ctx, client_get_cur_dir());
1192         if (!rname) {
1193                 return 1;
1194         }
1195
1196         if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1197                 d_printf("get <filename> [localname]\n");
1198                 return 1;
1199         }
1200         rname = talloc_asprintf_append(rname, "%s", fname);
1201         if (!rname) {
1202                 return 1;
1203         }
1204         rname = clean_name(ctx, rname);
1205         if (!rname) {
1206                 return 1;
1207         }
1208
1209         next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1210         if (!lname) {
1211                 lname = fname;
1212         }
1213
1214         return do_get(rname, lname, false);
1215 }
1216
1217 /****************************************************************************
1218  Do an mget operation on one file.
1219 ****************************************************************************/
1220
1221 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1222                     const char *dir)
1223 {
1224         TALLOC_CTX *ctx = talloc_tos();
1225         NTSTATUS status = NT_STATUS_OK;
1226         char *rname = NULL;
1227         char *quest = NULL;
1228         char *saved_curdir = NULL;
1229         char *mget_mask = NULL;
1230         char *new_cd = NULL;
1231
1232         if (!finfo->name) {
1233                 return NT_STATUS_OK;
1234         }
1235
1236         if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1237                 return NT_STATUS_OK;
1238
1239         if (abort_mget) {
1240                 d_printf("mget aborted\n");
1241                 return NT_STATUS_UNSUCCESSFUL;
1242         }
1243
1244         if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
1245                 if (asprintf(&quest,
1246                          "Get directory %s? ",finfo->name) < 0) {
1247                         return NT_STATUS_NO_MEMORY;
1248                 }
1249         } else {
1250                 if (asprintf(&quest,
1251                          "Get file %s? ",finfo->name) < 0) {
1252                         return NT_STATUS_NO_MEMORY;
1253                 }
1254         }
1255
1256         if (prompt && !yesno(quest)) {
1257                 SAFE_FREE(quest);
1258                 return NT_STATUS_OK;
1259         }
1260         SAFE_FREE(quest);
1261
1262         if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
1263                 rname = talloc_asprintf(ctx,
1264                                 "%s%s",
1265                                 client_get_cur_dir(),
1266                                 finfo->name);
1267                 if (!rname) {
1268                         return NT_STATUS_NO_MEMORY;
1269                 }
1270                 do_get(rname, finfo->name, false);
1271                 TALLOC_FREE(rname);
1272                 return NT_STATUS_OK;
1273         }
1274
1275         /* handle directories */
1276         saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1277         if (!saved_curdir) {
1278                 return NT_STATUS_NO_MEMORY;
1279         }
1280
1281         new_cd = talloc_asprintf(ctx,
1282                                 "%s%s%s",
1283                                 client_get_cur_dir(),
1284                                 finfo->name,
1285                                 CLI_DIRSEP_STR);
1286         if (!new_cd) {
1287                 return NT_STATUS_NO_MEMORY;
1288         }
1289         client_set_cur_dir(new_cd);
1290
1291         string_replace(finfo->name,'\\','/');
1292         if (lowercase) {
1293                 if (!strlower_m(finfo->name)) {
1294                         return NT_STATUS_INVALID_PARAMETER;
1295                 }
1296         }
1297
1298         if (!directory_exist(finfo->name) &&
1299             mkdir(finfo->name,0777) != 0) {
1300                 d_printf("failed to create directory %s\n",finfo->name);
1301                 client_set_cur_dir(saved_curdir);
1302                 return map_nt_error_from_unix(errno);
1303         }
1304
1305         if (chdir(finfo->name) != 0) {
1306                 d_printf("failed to chdir to directory %s\n",finfo->name);
1307                 client_set_cur_dir(saved_curdir);
1308                 return map_nt_error_from_unix(errno);
1309         }
1310
1311         mget_mask = talloc_asprintf(ctx,
1312                         "%s*",
1313                         client_get_cur_dir());
1314
1315         if (!mget_mask) {
1316                 return NT_STATUS_NO_MEMORY;
1317         }
1318
1319         status = do_list(mget_mask,
1320                          (FILE_ATTRIBUTE_SYSTEM
1321                           | FILE_ATTRIBUTE_HIDDEN
1322                           | FILE_ATTRIBUTE_DIRECTORY),
1323                          do_mget, false, true);
1324         if (!NT_STATUS_IS_OK(status)
1325          && !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1326                 /*
1327                  * Ignore access denied errors to ensure all permitted files are
1328                  * pulled down.
1329                  */
1330                 return status;
1331         }
1332
1333         if (chdir("..") == -1) {
1334                 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1335                         strerror(errno) );
1336                 return map_nt_error_from_unix(errno);
1337         }
1338         client_set_cur_dir(saved_curdir);
1339         TALLOC_FREE(mget_mask);
1340         TALLOC_FREE(saved_curdir);
1341         TALLOC_FREE(new_cd);
1342         return NT_STATUS_OK;
1343 }
1344
1345 /****************************************************************************
1346  View the file using the pager.
1347 ****************************************************************************/
1348
1349 static int cmd_more(void)
1350 {
1351         TALLOC_CTX *ctx = talloc_tos();
1352         char *rname = NULL;
1353         char *fname = NULL;
1354         char *lname = NULL;
1355         char *pager_cmd = NULL;
1356         const char *pager;
1357         int fd;
1358         int rc = 0;
1359         mode_t mask;
1360
1361         rname = talloc_strdup(ctx, client_get_cur_dir());
1362         if (!rname) {
1363                 return 1;
1364         }
1365
1366         lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1367         if (!lname) {
1368                 return 1;
1369         }
1370         mask = umask(S_IRWXO | S_IRWXG);
1371         fd = mkstemp(lname);
1372         umask(mask);
1373         if (fd == -1) {
1374                 d_printf("failed to create temporary file for more\n");
1375                 return 1;
1376         }
1377         close(fd);
1378
1379         if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1380                 d_printf("more <filename>\n");
1381                 unlink(lname);
1382                 return 1;
1383         }
1384         rname = talloc_asprintf_append(rname, "%s", fname);
1385         if (!rname) {
1386                 return 1;
1387         }
1388         rname = clean_name(ctx,rname);
1389         if (!rname) {
1390                 return 1;
1391         }
1392
1393         rc = do_get(rname, lname, false);
1394
1395         pager=getenv("PAGER");
1396
1397         pager_cmd = talloc_asprintf(ctx,
1398                                 "%s %s",
1399                                 (pager? pager:PAGER),
1400                                 lname);
1401         if (!pager_cmd) {
1402                 return 1;
1403         }
1404         if (system(pager_cmd) == -1) {
1405                 d_printf("system command '%s' returned -1\n",
1406                         pager_cmd);
1407         }
1408         unlink(lname);
1409
1410         return rc;
1411 }
1412
1413 /****************************************************************************
1414  Do a mget command.
1415 ****************************************************************************/
1416
1417 static int cmd_mget(void)
1418 {
1419         TALLOC_CTX *ctx = talloc_tos();
1420         uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1421         char *mget_mask = NULL;
1422         char *buf = NULL;
1423         NTSTATUS status = NT_STATUS_OK;
1424
1425         if (recurse) {
1426                 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1427         }
1428
1429         abort_mget = false;
1430
1431         while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1432
1433                 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1434                 if (!mget_mask) {
1435                         return 1;
1436                 }
1437                 if (*buf == CLI_DIRSEP_CHAR) {
1438                         mget_mask = talloc_strdup(ctx, buf);
1439                 } else {
1440                         mget_mask = talloc_asprintf_append(mget_mask,
1441                                                         "%s", buf);
1442                 }
1443                 if (!mget_mask) {
1444                         return 1;
1445                 }
1446                 status = do_list(mget_mask, attribute, do_mget, false, true);
1447                 if (!NT_STATUS_IS_OK(status)) {
1448                         return 1;
1449                 }
1450         }
1451
1452         if (mget_mask == NULL) {
1453                 d_printf("nothing to mget\n");
1454                 return 0;
1455         }
1456
1457         if (!*mget_mask) {
1458                 mget_mask = talloc_asprintf(ctx,
1459                                         "%s*",
1460                                         client_get_cur_dir());
1461                 if (!mget_mask) {
1462                         return 1;
1463                 }
1464                 status = do_list(mget_mask, attribute, do_mget, false, true);
1465                 if (!NT_STATUS_IS_OK(status)) {
1466                         return 1;
1467                 }
1468         }
1469
1470         return 0;
1471 }
1472
1473 /****************************************************************************
1474  Make a directory of name "name".
1475 ****************************************************************************/
1476
1477 static bool do_mkdir(const char *name)
1478 {
1479         TALLOC_CTX *ctx = talloc_tos();
1480         struct cli_state *targetcli;
1481         char *targetname = NULL;
1482         NTSTATUS status;
1483
1484         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1485                                 cli, name, &targetcli, &targetname);
1486         if (!NT_STATUS_IS_OK(status)) {
1487                 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1488                 return false;
1489         }
1490
1491         status = cli_mkdir(targetcli, targetname);
1492         if (!NT_STATUS_IS_OK(status)) {
1493                 d_printf("%s making remote directory %s\n",
1494                          nt_errstr(status),name);
1495                 return false;
1496         }
1497
1498         return true;
1499 }
1500
1501 /****************************************************************************
1502  Show 8.3 name of a file.
1503 ****************************************************************************/
1504
1505 static bool do_altname(const char *name)
1506 {
1507         fstring altname;
1508         NTSTATUS status;
1509
1510         status = cli_qpathinfo_alt_name(cli, name, altname);
1511         if (!NT_STATUS_IS_OK(status)) {
1512                 d_printf("%s getting alt name for %s\n",
1513                          nt_errstr(status),name);
1514                 return false;
1515         }
1516         d_printf("%s\n", altname);
1517
1518         return true;
1519 }
1520
1521 /****************************************************************************
1522  Exit client.
1523 ****************************************************************************/
1524
1525 static int cmd_quit(void)
1526 {
1527         cli_shutdown(cli);
1528         exit(0);
1529         /* NOTREACHED */
1530         return 0;
1531 }
1532
1533 /****************************************************************************
1534  Make a directory.
1535 ****************************************************************************/
1536
1537 static int cmd_mkdir(void)
1538 {
1539         TALLOC_CTX *ctx = talloc_tos();
1540         char *mask = NULL;
1541         char *buf = NULL;
1542         NTSTATUS status;
1543
1544         mask = talloc_strdup(ctx, client_get_cur_dir());
1545         if (!mask) {
1546                 return 1;
1547         }
1548
1549         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1550                 if (!recurse) {
1551                         d_printf("mkdir <dirname>\n");
1552                 }
1553                 return 1;
1554         }
1555         mask = talloc_asprintf_append(mask, "%s", buf);
1556         if (!mask) {
1557                 return 1;
1558         }
1559
1560         if (recurse) {
1561                 char *ddir = NULL;
1562                 char *ddir2 = NULL;
1563                 struct cli_state *targetcli;
1564                 char *targetname = NULL;
1565                 char *p = NULL;
1566                 char *saveptr;
1567
1568                 ddir2 = talloc_strdup(ctx, "");
1569                 if (!ddir2) {
1570                         return 1;
1571                 }
1572
1573                 status = cli_resolve_path(ctx, "",
1574                                 popt_get_cmdline_auth_info(), cli, mask,
1575                                 &targetcli, &targetname);
1576                 if (!NT_STATUS_IS_OK(status)) {
1577                         return 1;
1578                 }
1579
1580                 ddir = talloc_strdup(ctx, targetname);
1581                 if (!ddir) {
1582                         return 1;
1583                 }
1584                 trim_char(ddir,'.','\0');
1585                 p = strtok_r(ddir, "/\\", &saveptr);
1586                 while (p) {
1587                         ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1588                         if (!ddir2) {
1589                                 return 1;
1590                         }
1591                         if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1592                                 do_mkdir(ddir2);
1593                         }
1594                         ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1595                         if (!ddir2) {
1596                                 return 1;
1597                         }
1598                         p = strtok_r(NULL, "/\\", &saveptr);
1599                 }
1600         } else {
1601                 do_mkdir(mask);
1602         }
1603
1604         return 0;
1605 }
1606
1607 /****************************************************************************
1608  Show alt name.
1609 ****************************************************************************/
1610
1611 static int cmd_altname(void)
1612 {
1613         TALLOC_CTX *ctx = talloc_tos();
1614         char *name;
1615         char *buf;
1616
1617         name = talloc_strdup(ctx, client_get_cur_dir());
1618         if (!name) {
1619                 return 1;
1620         }
1621
1622         if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1623                 d_printf("altname <file>\n");
1624                 return 1;
1625         }
1626         name = talloc_asprintf_append(name, "%s", buf);
1627         if (!name) {
1628                 return 1;
1629         }
1630         do_altname(name);
1631         return 0;
1632 }
1633
1634 static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1635 {
1636         char *attrs = talloc_zero_array(mem_ctx, char, 17);
1637         int i = 0;
1638
1639         if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1640                 if (mode & FILE_ATTRIBUTE_ENCRYPTED) {
1641                         attrs[i++] = 'E';
1642                 }
1643                 if (mode & FILE_ATTRIBUTE_NONINDEXED) {
1644                         attrs[i++] = 'N';
1645                 }
1646                 if (mode & FILE_ATTRIBUTE_OFFLINE) {
1647                         attrs[i++] = 'O';
1648                 }
1649                 if (mode & FILE_ATTRIBUTE_COMPRESSED) {
1650                         attrs[i++] = 'C';
1651                 }
1652                 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1653                         attrs[i++] = 'r';
1654                 }
1655                 if (mode & FILE_ATTRIBUTE_SPARSE) {
1656                         attrs[i++] = 's';
1657                 }
1658                 if (mode & FILE_ATTRIBUTE_TEMPORARY) {
1659                         attrs[i++] = 'T';
1660                 }
1661                 if (mode & FILE_ATTRIBUTE_NORMAL) {
1662                         attrs[i++] = 'N';
1663                 }
1664                 if (mode & FILE_ATTRIBUTE_READONLY) {
1665                         attrs[i++] = 'R';
1666                 }
1667                 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1668                         attrs[i++] = 'H';
1669                 }
1670                 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1671                         attrs[i++] = 'S';
1672                 }
1673                 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1674                         attrs[i++] = 'D';
1675                 }
1676                 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1677                         attrs[i++] = 'A';
1678                 }
1679         }
1680         return attrs;
1681 }
1682
1683 /****************************************************************************
1684  Show all info we can get
1685 ****************************************************************************/
1686
1687 static int do_allinfo(const char *name)
1688 {
1689         fstring altname;
1690         struct timespec b_time, a_time, m_time, c_time;
1691         off_t size;
1692         uint16_t mode;
1693         NTTIME tmp;
1694         uint16_t fnum;
1695         unsigned int num_streams;
1696         struct stream_struct *streams;
1697         int num_snapshots;
1698         char **snapshots = NULL;
1699         unsigned int i;
1700         NTSTATUS status;
1701
1702         status = cli_qpathinfo_alt_name(cli, name, altname);
1703         if (!NT_STATUS_IS_OK(status)) {
1704                 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1705                          name);
1706                 /*
1707                  * Ignore not supported or not implemented, it does not
1708                  * hurt if we can't list alternate names.
1709                  */
1710                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1711                     NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1712                         altname[0] = '\0';
1713                 } else {
1714                         return false;
1715                 }
1716         }
1717         d_printf("altname: %s\n", altname);
1718
1719         status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1720                                 &size, &mode, NULL);
1721         if (!NT_STATUS_IS_OK(status)) {
1722                 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1723                          name);
1724                 return false;
1725         }
1726
1727         tmp = unix_timespec_to_nt_time(b_time);
1728         d_printf("create_time:    %s\n", nt_time_string(talloc_tos(), tmp));
1729
1730         tmp = unix_timespec_to_nt_time(a_time);
1731         d_printf("access_time:    %s\n", nt_time_string(talloc_tos(), tmp));
1732
1733         tmp = unix_timespec_to_nt_time(m_time);
1734         d_printf("write_time:     %s\n", nt_time_string(talloc_tos(), tmp));
1735
1736         tmp = unix_timespec_to_nt_time(c_time);
1737         d_printf("change_time:    %s\n", nt_time_string(talloc_tos(), tmp));
1738
1739         d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
1740
1741         status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1742                                        &streams);
1743         if (!NT_STATUS_IS_OK(status)) {
1744                 d_printf("%s getting streams for %s\n", nt_errstr(status),
1745                          name);
1746                 return false;
1747         }
1748
1749         for (i=0; i<num_streams; i++) {
1750                 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1751                          (unsigned long long)streams[i].size);
1752         }
1753
1754         if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1755                 char *subst, *print;
1756                 uint32_t flags;
1757
1758                 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1759                                       &flags);
1760                 if (!NT_STATUS_IS_OK(status)) {
1761                         d_fprintf(stderr, "cli_readlink returned %s\n",
1762                                   nt_errstr(status));
1763                 } else {
1764                         d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1765                                  subst, print, flags);
1766                         TALLOC_FREE(subst);
1767                         TALLOC_FREE(print);
1768                 }
1769         }
1770
1771         status = cli_ntcreate(cli, name, 0,
1772                               SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1773                               SEC_STD_SYNCHRONIZE, 0,
1774                               FILE_SHARE_READ|FILE_SHARE_WRITE
1775                               |FILE_SHARE_DELETE,
1776                               FILE_OPEN, 0x0, 0x0, &fnum, NULL);
1777         if (!NT_STATUS_IS_OK(status)) {
1778                 /*
1779                  * Ignore failure, it does not hurt if we can't list
1780                  * snapshots
1781                  */
1782                 return 0;
1783         }
1784         /*
1785          * In order to get shadow copy data over SMB1 we
1786          * must call twice, once with 'get_names = false'
1787          * to get the size, then again with 'get_names = true'
1788          * to get the data or a Windows server fails to return
1789          * valid info. Samba doesn't have this bug. JRA.
1790          */
1791
1792         status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1793                                       false, &snapshots, &num_snapshots);
1794         if (!NT_STATUS_IS_OK(status)) {
1795                 cli_close(cli, fnum);
1796                 return 0;
1797         }
1798         status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1799                                       true, &snapshots, &num_snapshots);
1800         if (!NT_STATUS_IS_OK(status)) {
1801                 cli_close(cli, fnum);
1802                 return 0;
1803         }
1804
1805         for (i=0; i<num_snapshots; i++) {
1806                 char *snap_name;
1807
1808                 d_printf("%s\n", snapshots[i]);
1809                 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1810                                             snapshots[i], name);
1811                 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1812                                         &m_time, &c_time, &size,
1813                                         NULL, NULL);
1814                 if (!NT_STATUS_IS_OK(status)) {
1815                         d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1816                                   snap_name, nt_errstr(status));
1817                         TALLOC_FREE(snap_name);
1818                         continue;
1819                 }
1820                 tmp = unix_timespec_to_nt_time(b_time);
1821                 d_printf("create_time:    %s\n", nt_time_string(talloc_tos(), tmp));
1822                 tmp = unix_timespec_to_nt_time(a_time);
1823                 d_printf("access_time:    %s\n", nt_time_string(talloc_tos(), tmp));
1824                 tmp =unix_timespec_to_nt_time(m_time);
1825                 d_printf("write_time:     %s\n", nt_time_string(talloc_tos(), tmp));
1826                 tmp = unix_timespec_to_nt_time(c_time);
1827                 d_printf("change_time:    %s\n", nt_time_string(talloc_tos(), tmp));
1828                 d_printf("size: %d\n", (int)size);
1829         }
1830
1831         TALLOC_FREE(snapshots);
1832         cli_close(cli, fnum);
1833
1834         return 0;
1835 }
1836
1837 /****************************************************************************
1838  Show all info we can get
1839 ****************************************************************************/
1840
1841 static int cmd_allinfo(void)
1842 {
1843         TALLOC_CTX *ctx = talloc_tos();
1844         char *name;
1845         char *buf;
1846
1847         name = talloc_strdup(ctx, client_get_cur_dir());
1848         if (!name) {
1849                 return 1;
1850         }
1851
1852         if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1853                 d_printf("allinfo <file>\n");
1854                 return 1;
1855         }
1856         name = talloc_asprintf_append(name, "%s", buf);
1857         if (!name) {
1858                 return 1;
1859         }
1860
1861         do_allinfo(name);
1862
1863         return 0;
1864 }
1865
1866 /****************************************************************************
1867  Put a single file.
1868 ****************************************************************************/
1869
1870 static int do_put(const char *rname, const char *lname, bool reput)
1871 {
1872         TALLOC_CTX *ctx = talloc_tos();
1873         uint16_t fnum;
1874         FILE *f;
1875         off_t start = 0;
1876         int rc = 0;
1877         struct timespec tp_start;
1878         struct cli_state *targetcli;
1879         char *targetname = NULL;
1880         struct push_state state;
1881         NTSTATUS status;
1882
1883         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1884                                 cli, rname, &targetcli, &targetname);
1885         if (!NT_STATUS_IS_OK(status)) {
1886                 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1887                 return 1;
1888         }
1889
1890         clock_gettime_mono(&tp_start);
1891
1892         if (reput) {
1893                 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1894                 if (NT_STATUS_IS_OK(status)) {
1895                         if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1896                                                      targetcli, fnum, NULL,
1897                                                      &start, NULL, NULL,
1898                                                      NULL, NULL, NULL)) &&
1899                             !NT_STATUS_IS_OK(status = cli_getattrE(
1900                                                      targetcli, fnum, NULL,
1901                                                      &start, NULL, NULL,
1902                                                      NULL))) {
1903                                 d_printf("getattrib: %s\n", nt_errstr(status));
1904                                 return 1;
1905                         }
1906                 }
1907         } else {
1908                 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1909         }
1910
1911         if (!NT_STATUS_IS_OK(status)) {
1912                 d_printf("%s opening remote file %s\n", nt_errstr(status),
1913                          rname);
1914                 return 1;
1915         }
1916
1917         /* allow files to be piped into smbclient
1918            jdblair 24.jun.98
1919
1920            Note that in this case this function will exit(0) rather
1921            than returning. */
1922         if (!strcmp(lname, "-")) {
1923                 f = stdin;
1924                 /* size of file is not known */
1925         } else {
1926                 f = fopen(lname, "r");
1927                 if (f && reput) {
1928                         if (fseek(f, start, SEEK_SET) == -1) {
1929                                 d_printf("Error seeking local file\n");
1930                                 fclose(f);
1931                                 return 1;
1932                         }
1933                 }
1934         }
1935
1936         if (!f) {
1937                 d_printf("Error opening local file %s\n",lname);
1938                 return 1;
1939         }
1940
1941         DEBUG(1,("putting file %s as %s ",lname,
1942                  rname));
1943
1944         setvbuf(f, NULL, _IOFBF, io_bufsize);
1945
1946         state.f = f;
1947         state.nread = 0;
1948
1949         status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1950                           &state);
1951         if (!NT_STATUS_IS_OK(status)) {
1952                 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1953                 rc = 1;
1954         }
1955
1956         status = cli_close(targetcli, fnum);
1957         if (!NT_STATUS_IS_OK(status)) {
1958                 d_printf("%s closing remote file %s\n", nt_errstr(status),
1959                          rname);
1960                 if (f != stdin) {
1961                         fclose(f);
1962                 }
1963                 return 1;
1964         }
1965
1966         if (f != stdin) {
1967                 fclose(f);
1968         }
1969
1970         {
1971                 struct timespec tp_end;
1972                 int this_time;
1973
1974                 clock_gettime_mono(&tp_end);
1975                 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1976                 put_total_time_ms += this_time;
1977                 put_total_size += state.nread;
1978
1979                 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1980                          state.nread / (1.024*this_time + 1.0e-4),
1981                          put_total_size / (1.024*put_total_time_ms)));
1982         }
1983
1984         if (f == stdin) {
1985                 cli_shutdown(cli);
1986                 exit(rc);
1987         }
1988
1989         return rc;
1990 }
1991
1992 /****************************************************************************
1993  Put a file.
1994 ****************************************************************************/
1995
1996 static int cmd_put(void)
1997 {
1998         TALLOC_CTX *ctx = talloc_tos();
1999         char *lname;
2000         char *rname;
2001         char *buf;
2002
2003         rname = talloc_strdup(ctx, client_get_cur_dir());
2004         if (!rname) {
2005                 return 1;
2006         }
2007
2008         if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
2009                 d_printf("put <filename>\n");
2010                 return 1;
2011         }
2012
2013         if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2014                 rname = talloc_asprintf_append(rname, "%s", buf);
2015         } else {
2016                 rname = talloc_asprintf_append(rname, "%s", lname);
2017         }
2018         if (!rname) {
2019                 return 1;
2020         }
2021
2022         rname = clean_name(ctx, rname);
2023         if (!rname) {
2024                 return 1;
2025         }
2026
2027         {
2028                 SMB_STRUCT_STAT st;
2029                 /* allow '-' to represent stdin
2030                    jdblair, 24.jun.98 */
2031                 if (!file_exist_stat(lname, &st, false) &&
2032                     (strcmp(lname,"-"))) {
2033                         d_printf("%s does not exist\n",lname);
2034                         return 1;
2035                 }
2036         }
2037
2038         return do_put(rname, lname, false);
2039 }
2040
2041 /*************************************
2042  File list structure.
2043 *************************************/
2044
2045 static struct file_list {
2046         struct file_list *prev, *next;
2047         char *file_path;
2048         bool isdir;
2049 } *file_list;
2050
2051 /****************************************************************************
2052  Free a file_list structure.
2053 ****************************************************************************/
2054
2055 static void free_file_list (struct file_list *l_head)
2056 {
2057         struct file_list *list, *next;
2058
2059         for (list = l_head; list; list = next) {
2060                 next = list->next;
2061                 DLIST_REMOVE(l_head, list);
2062                 SAFE_FREE(list->file_path);
2063                 SAFE_FREE(list);
2064         }
2065 }
2066
2067 /****************************************************************************
2068  Seek in a directory/file list until you get something that doesn't start with
2069  the specified name.
2070 ****************************************************************************/
2071
2072 static bool seek_list(struct file_list *list, char *name)
2073 {
2074         while (list) {
2075                 trim_string(list->file_path,"./","\n");
2076                 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2077                         return true;
2078                 }
2079                 list = list->next;
2080         }
2081
2082         return false;
2083 }
2084
2085 /****************************************************************************
2086  Set the file selection mask.
2087 ****************************************************************************/
2088
2089 static int cmd_select(void)
2090 {
2091         TALLOC_CTX *ctx = talloc_tos();
2092         char *new_fs = NULL;
2093         next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2094                 ;
2095         if (new_fs) {
2096                 client_set_fileselection(new_fs);
2097         } else {
2098                 client_set_fileselection("");
2099         }
2100         return 0;
2101 }
2102
2103 /****************************************************************************
2104   Recursive file matching function act as find
2105   match must be always set to true when calling this function
2106 ****************************************************************************/
2107
2108 static int file_find(struct file_list **list, const char *directory,
2109                       const char *expression, bool match)
2110 {
2111         DIR *dir;
2112         struct file_list *entry;
2113         struct stat statbuf;
2114         int ret;
2115         char *path;
2116         bool isdir;
2117         const char *dname;
2118
2119         dir = opendir(directory);
2120         if (!dir)
2121                 return -1;
2122
2123         while ((dname = readdirname(dir))) {
2124                 if (!strcmp("..", dname))
2125                         continue;
2126                 if (!strcmp(".", dname))
2127                         continue;
2128
2129                 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
2130                         continue;
2131                 }
2132
2133                 isdir = false;
2134                 if (!match || !gen_fnmatch(expression, dname)) {
2135                         if (recurse) {
2136                                 ret = stat(path, &statbuf);
2137                                 if (ret == 0) {
2138                                         if (S_ISDIR(statbuf.st_mode)) {
2139                                                 isdir = true;
2140                                                 ret = file_find(list, path, expression, false);
2141                                         }
2142                                 } else {
2143                                         d_printf("file_find: cannot stat file %s\n", path);
2144                                 }
2145
2146                                 if (ret == -1) {
2147                                         SAFE_FREE(path);
2148                                         closedir(dir);
2149                                         return -1;
2150                                 }
2151                         }
2152                         entry = SMB_MALLOC_P(struct file_list);
2153                         if (!entry) {
2154                                 d_printf("Out of memory in file_find\n");
2155                                 closedir(dir);
2156                                 return -1;
2157                         }
2158                         entry->file_path = path;
2159                         entry->isdir = isdir;
2160                         DLIST_ADD(*list, entry);
2161                 } else {
2162                         SAFE_FREE(path);
2163                 }
2164         }
2165
2166         closedir(dir);
2167         return 0;
2168 }
2169
2170 /****************************************************************************
2171  mput some files.
2172 ****************************************************************************/
2173
2174 static int cmd_mput(void)
2175 {
2176         TALLOC_CTX *ctx = talloc_tos();
2177         char *p = NULL;
2178
2179         while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2180                 int ret;
2181                 struct file_list *temp_list;
2182                 char *quest, *lname, *rname;
2183
2184                 file_list = NULL;
2185
2186                 ret = file_find(&file_list, ".", p, true);
2187                 if (ret) {
2188                         free_file_list(file_list);
2189                         continue;
2190                 }
2191
2192                 quest = NULL;
2193                 lname = NULL;
2194                 rname = NULL;
2195
2196                 for (temp_list = file_list; temp_list;
2197                      temp_list = temp_list->next) {
2198
2199                         SAFE_FREE(lname);
2200                         if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2201                                 continue;
2202                         }
2203                         trim_string(lname, "./", "/");
2204
2205                         /* check if it's a directory */
2206                         if (temp_list->isdir) {
2207                                 /* if (!recurse) continue; */
2208
2209                                 SAFE_FREE(quest);
2210                                 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2211                                         break;
2212                                 }
2213                                 if (prompt && !yesno(quest)) { /* No */
2214                                         /* Skip the directory */
2215                                         lname[strlen(lname)-1] = '/';
2216                                         if (!seek_list(temp_list, lname))
2217                                                 break;
2218                                 } else { /* Yes */
2219                                         SAFE_FREE(rname);
2220                                         if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2221                                                 break;
2222                                         }
2223                                         normalize_name(rname);
2224                                         if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2225                                             !do_mkdir(rname)) {
2226                                                 DEBUG (0, ("Unable to make dir, skipping..."));
2227                                                 /* Skip the directory */
2228                                                 lname[strlen(lname)-1] = '/';
2229                                                 if (!seek_list(temp_list, lname)) {
2230                                                         break;
2231                                                 }
2232                                         }
2233                                 }
2234                                 continue;
2235                         } else {
2236                                 SAFE_FREE(quest);
2237                                 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2238                                         break;
2239                                 }
2240                                 if (prompt && !yesno(quest)) {
2241                                         /* No */
2242                                         continue;
2243                                 }
2244
2245                                 /* Yes */
2246                                 SAFE_FREE(rname);
2247                                 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2248                                         break;
2249                                 }
2250                         }
2251
2252                         normalize_name(rname);
2253
2254                         do_put(rname, lname, false);
2255                 }
2256                 free_file_list(file_list);
2257                 SAFE_FREE(quest);
2258                 SAFE_FREE(lname);
2259                 SAFE_FREE(rname);
2260         }
2261
2262         return 0;
2263 }
2264
2265 /****************************************************************************
2266  Cancel a print job.
2267 ****************************************************************************/
2268
2269 static int do_cancel(int job)
2270 {
2271         if (cli_printjob_del(cli, job)) {
2272                 d_printf("Job %d cancelled\n",job);
2273                 return 0;
2274         } else {
2275                 NTSTATUS status = cli_nt_error(cli);
2276                 d_printf("Error cancelling job %d : %s\n",
2277                          job, nt_errstr(status));
2278                 return 1;
2279         }
2280 }
2281
2282 /****************************************************************************
2283  Cancel a print job.
2284 ****************************************************************************/
2285
2286 static int cmd_cancel(void)
2287 {
2288         TALLOC_CTX *ctx = talloc_tos();
2289         char *buf = NULL;
2290         int job;
2291
2292         if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2293                 d_printf("cancel <jobid> ...\n");
2294                 return 1;
2295         }
2296         do {
2297                 job = atoi(buf);
2298                 do_cancel(job);
2299         } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2300
2301         return 0;
2302 }
2303
2304 /****************************************************************************
2305  Print a file.
2306 ****************************************************************************/
2307
2308 static int cmd_print(void)
2309 {
2310         TALLOC_CTX *ctx = talloc_tos();
2311         char *lname = NULL;
2312         char *rname = NULL;
2313         char *p = NULL;
2314
2315         if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2316                 d_printf("print <filename>\n");
2317                 return 1;
2318         }
2319
2320         rname = talloc_strdup(ctx, lname);
2321         if (!rname) {
2322                 return 1;
2323         }
2324         p = strrchr_m(rname,'/');
2325         if (p) {
2326                 rname = talloc_asprintf(ctx,
2327                                         "%s-%d",
2328                                         p+1,
2329                                         (int)getpid());
2330         }
2331         if (strequal(lname,"-")) {
2332                 rname = talloc_asprintf(ctx,
2333                                 "stdin-%d",
2334                                 (int)getpid());
2335         }
2336         if (!rname) {
2337                 return 1;
2338         }
2339
2340         return do_put(rname, lname, false);
2341 }
2342
2343 /****************************************************************************
2344  Show a print queue entry.
2345 ****************************************************************************/
2346
2347 static void queue_fn(struct print_job_info *p)
2348 {
2349         d_printf("%-6d   %-9d    %s\n", (int)p->id, (int)p->size, p->name);
2350 }
2351
2352 /****************************************************************************
2353  Show a print queue.
2354 ****************************************************************************/
2355
2356 static int cmd_queue(void)
2357 {
2358         cli_print_queue(cli, queue_fn);
2359         return 0;
2360 }
2361
2362 /****************************************************************************
2363  Delete some files.
2364 ****************************************************************************/
2365
2366 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2367                    const char *dir)
2368 {
2369         TALLOC_CTX *ctx = talloc_tos();
2370         char *mask = NULL;
2371         NTSTATUS status;
2372
2373         mask = talloc_asprintf(ctx,
2374                                 "%s%c%s",
2375                                 dir,
2376                                 CLI_DIRSEP_CHAR,
2377                                 finfo->name);
2378         if (!mask) {
2379                 return NT_STATUS_NO_MEMORY;
2380         }
2381
2382         if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2383                 TALLOC_FREE(mask);
2384                 return NT_STATUS_OK;
2385         }
2386
2387         status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2388         if (!NT_STATUS_IS_OK(status)) {
2389                 d_printf("%s deleting remote file %s\n",
2390                          nt_errstr(status), mask);
2391         }
2392         TALLOC_FREE(mask);
2393         return status;
2394 }
2395
2396 /****************************************************************************
2397  Delete some files.
2398 ****************************************************************************/
2399
2400 static int cmd_del(void)
2401 {
2402         TALLOC_CTX *ctx = talloc_tos();
2403         char *mask = NULL;
2404         char *buf = NULL;
2405         NTSTATUS status = NT_STATUS_OK;
2406         uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2407
2408         if (recurse) {
2409                 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2410         }
2411
2412         mask = talloc_strdup(ctx, client_get_cur_dir());
2413         if (!mask) {
2414                 return 1;
2415         }
2416         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2417                 d_printf("del <filename>\n");
2418                 return 1;
2419         }
2420         mask = talloc_asprintf_append(mask, "%s", buf);
2421         if (!mask) {
2422                 return 1;
2423         }
2424
2425         status = do_list(mask,attribute,do_del,false,false);
2426         if (!NT_STATUS_IS_OK(status)) {
2427                 return 1;
2428         }
2429         return 0;
2430 }
2431
2432 /****************************************************************************
2433  Wildcard delete some files.
2434 ****************************************************************************/
2435
2436 static int cmd_wdel(void)
2437 {
2438         TALLOC_CTX *ctx = talloc_tos();
2439         char *mask = NULL;
2440         char *buf = NULL;
2441         uint16_t attribute;
2442         struct cli_state *targetcli;
2443         char *targetname = NULL;
2444         NTSTATUS status;
2445
2446         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2447                 d_printf("wdel 0x<attrib> <wcard>\n");
2448                 return 1;
2449         }
2450
2451         attribute = (uint16_t)strtol(buf, (char **)NULL, 16);
2452
2453         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2454                 d_printf("wdel 0x<attrib> <wcard>\n");
2455                 return 1;
2456         }
2457
2458         mask = talloc_asprintf(ctx, "%s%s",
2459                         client_get_cur_dir(),
2460                         buf);
2461         if (!mask) {
2462                 return 1;
2463         }
2464
2465         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2466                                 cli, mask, &targetcli, &targetname);
2467         if (!NT_STATUS_IS_OK(status)) {
2468                 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2469                 return 1;
2470         }
2471
2472         status = cli_unlink(targetcli, targetname, attribute);
2473         if (!NT_STATUS_IS_OK(status)) {
2474                 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2475                          targetname);
2476         }
2477         return 0;
2478 }
2479
2480 /****************************************************************************
2481 ****************************************************************************/
2482
2483 static int cmd_open(void)
2484 {
2485         TALLOC_CTX *ctx = talloc_tos();
2486         char *mask = NULL;
2487         char *buf = NULL;
2488         char *targetname = NULL;
2489         struct cli_state *targetcli;
2490         uint16_t fnum = (uint16_t)-1;
2491         NTSTATUS status;
2492
2493         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2494                 d_printf("open <filename>\n");
2495                 return 1;
2496         }
2497         mask = talloc_asprintf(ctx,
2498                         "%s%s",
2499                         client_get_cur_dir(),
2500                         buf);
2501         if (!mask) {
2502                 return 1;
2503         }
2504
2505         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2506                         cli, mask, &targetcli, &targetname);
2507         if (!NT_STATUS_IS_OK(status)) {
2508                 d_printf("open %s: %s\n", mask, nt_errstr(status));
2509                 return 1;
2510         }
2511
2512         status = cli_ntcreate(targetcli, targetname, 0,
2513                         FILE_READ_DATA|FILE_WRITE_DATA, 0,
2514                         FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2515                         0x0, 0x0, &fnum, NULL);
2516         if (!NT_STATUS_IS_OK(status)) {
2517                 status = cli_ntcreate(targetcli, targetname, 0,
2518                                 FILE_READ_DATA, 0,
2519                                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2520                                 0x0, 0x0, &fnum, NULL);
2521                 if (NT_STATUS_IS_OK(status)) {
2522                         d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2523                 } else {
2524                         d_printf("Failed to open file %s. %s\n",
2525                                  targetname, nt_errstr(status));
2526                 }
2527         } else {
2528                 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2529         }
2530         return 0;
2531 }
2532
2533 static int cmd_posix_encrypt(void)
2534 {
2535         TALLOC_CTX *ctx = talloc_tos();
2536         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2537         char *domain = NULL;
2538         char *user = NULL;
2539         char *password = NULL;
2540         struct cli_credentials *creds = NULL;
2541         struct cli_credentials *lcreds = NULL;
2542
2543         if (next_token_talloc(ctx, &cmd_ptr, &domain, NULL)) {
2544
2545                 if (!next_token_talloc(ctx, &cmd_ptr, &user, NULL)) {
2546                         d_printf("posix_encrypt domain user password\n");
2547                         return 1;
2548                 }
2549
2550                 if (!next_token_talloc(ctx, &cmd_ptr, &password, NULL)) {
2551                         d_printf("posix_encrypt domain user password\n");
2552                         return 1;
2553                 }
2554
2555                 lcreds = cli_session_creds_init(ctx,
2556                                                 user,
2557                                                 domain,
2558                                                 NULL, /* realm */
2559                                                 password,
2560                                                 false, /* use_kerberos */
2561                                                 false, /* fallback_after_kerberos */
2562                                                 false, /* use_ccache */
2563                                                 false); /* password_is_nt_hash */
2564                 if (lcreds == NULL) {
2565                         d_printf("cli_session_creds_init() failed.\n");
2566                         return -1;
2567                 }
2568                 creds = lcreds;
2569         } else {
2570                 bool auth_requested = false;
2571
2572                 creds = get_cmdline_auth_info_creds(
2573                                 popt_get_cmdline_auth_info());
2574
2575                 auth_requested = cli_credentials_authentication_requested(creds);
2576                 if (!auth_requested) {
2577                         d_printf("posix_encrypt domain user password\n");
2578                         return 1;
2579                 }
2580         }
2581
2582         status = cli_smb1_setup_encryption(cli, creds);
2583         /* gensec currently references the creds so we can't free them here */
2584         talloc_unlink(ctx, lcreds);
2585         if (!NT_STATUS_IS_OK(status)) {
2586                 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2587         } else {
2588                 d_printf("encryption on\n");
2589                 smb_encrypt = true;
2590         }
2591
2592         return 0;
2593 }
2594
2595 /****************************************************************************
2596 ****************************************************************************/
2597
2598 static int cmd_posix_open(void)
2599 {
2600         TALLOC_CTX *ctx = talloc_tos();
2601         char *mask = NULL;
2602         char *buf = NULL;
2603         char *targetname = NULL;
2604         struct cli_state *targetcli;
2605         mode_t mode;
2606         uint16_t fnum;
2607         NTSTATUS status;
2608
2609         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2610                 d_printf("posix_open <filename> 0<mode>\n");
2611                 return 1;
2612         }
2613         mask = talloc_asprintf(ctx,
2614                         "%s%s",
2615                         client_get_cur_dir(),
2616                         buf);
2617         if (!mask) {
2618                 return 1;
2619         }
2620
2621         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2622                 d_printf("posix_open <filename> 0<mode>\n");
2623                 return 1;
2624         }
2625         mode = (mode_t)strtol(buf, (char **)NULL, 8);
2626
2627         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2628                                 cli, mask, &targetcli, &targetname);
2629         if (!NT_STATUS_IS_OK(status)) {
2630                 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2631                 return 1;
2632         }
2633
2634         status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2635                                 &fnum);
2636         if (!NT_STATUS_IS_OK(status)) {
2637                 status = cli_posix_open(targetcli, targetname,
2638                                         O_CREAT|O_RDONLY, mode, &fnum);
2639                 if (!NT_STATUS_IS_OK(status)) {
2640                         d_printf("Failed to open file %s. %s\n", targetname,
2641                                  nt_errstr(status));
2642                 } else {
2643                         d_printf("posix_open file %s: for readonly fnum %d\n",
2644                                  targetname, fnum);
2645                 }
2646         } else {
2647                 d_printf("posix_open file %s: for read/write fnum %d\n",
2648                          targetname, fnum);
2649         }
2650
2651         return 0;
2652 }
2653
2654 static int cmd_posix_mkdir(void)
2655 {
2656         TALLOC_CTX *ctx = talloc_tos();
2657         char *mask = NULL;
2658         char *buf = NULL;
2659         char *targetname = NULL;
2660         struct cli_state *targetcli;
2661         mode_t mode;
2662         NTSTATUS status;
2663
2664         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2665                 d_printf("posix_mkdir <filename> 0<mode>\n");
2666                 return 1;
2667         }
2668         mask = talloc_asprintf(ctx,
2669                         "%s%s",
2670                         client_get_cur_dir(),
2671                         buf);
2672         if (!mask) {
2673                 return 1;
2674         }
2675
2676         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2677                 d_printf("posix_mkdir <filename> 0<mode>\n");
2678                 return 1;
2679         }
2680         mode = (mode_t)strtol(buf, (char **)NULL, 8);
2681
2682         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2683                                 cli, mask, &targetcli, &targetname);
2684         if (!NT_STATUS_IS_OK(status)) {
2685                 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2686                 return 1;
2687         }
2688
2689         status = cli_posix_mkdir(targetcli, targetname, mode);
2690         if (!NT_STATUS_IS_OK(status)) {
2691                 d_printf("Failed to open file %s. %s\n",
2692                          targetname, nt_errstr(status));
2693         } else {
2694                 d_printf("posix_mkdir created directory %s\n", targetname);
2695         }
2696         return 0;
2697 }
2698
2699 static int cmd_posix_unlink(void)
2700 {
2701         TALLOC_CTX *ctx = talloc_tos();
2702         char *mask = NULL;
2703         char *buf = NULL;
2704         char *targetname = NULL;
2705         struct cli_state *targetcli;
2706         NTSTATUS status;
2707
2708         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2709                 d_printf("posix_unlink <filename>\n");
2710                 return 1;
2711         }
2712         mask = talloc_asprintf(ctx,
2713                         "%s%s",
2714                         client_get_cur_dir(),
2715                         buf);
2716         if (!mask) {
2717                 return 1;
2718         }
2719
2720         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2721                                 cli, mask, &targetcli, &targetname);
2722         if (!NT_STATUS_IS_OK(status)) {
2723                 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
2724                 return 1;
2725         }
2726
2727         status = cli_posix_unlink(targetcli, targetname);
2728         if (!NT_STATUS_IS_OK(status)) {
2729                 d_printf("Failed to unlink file %s. %s\n",
2730                          targetname, nt_errstr(status));
2731         } else {
2732                 d_printf("posix_unlink deleted file %s\n", targetname);
2733         }
2734
2735         return 0;
2736 }
2737
2738 static int cmd_posix_rmdir(void)
2739 {
2740         TALLOC_CTX *ctx = talloc_tos();
2741         char *mask = NULL;
2742         char *buf = NULL;
2743         char *targetname = NULL;
2744         struct cli_state *targetcli;
2745         NTSTATUS status;
2746
2747         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2748                 d_printf("posix_rmdir <filename>\n");
2749                 return 1;
2750         }
2751         mask = talloc_asprintf(ctx,
2752                         "%s%s",
2753                         client_get_cur_dir(),
2754                         buf);
2755         if (!mask) {
2756                 return 1;
2757         }
2758
2759         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2760                         cli, mask, &targetcli, &targetname);
2761         if (!NT_STATUS_IS_OK(status)) {
2762                 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
2763                 return 1;
2764         }
2765
2766         status = cli_posix_rmdir(targetcli, targetname);
2767         if (!NT_STATUS_IS_OK(status)) {
2768                 d_printf("Failed to unlink directory %s. %s\n",
2769                          targetname, nt_errstr(status));
2770         } else {
2771                 d_printf("posix_rmdir deleted directory %s\n", targetname);
2772         }
2773
2774         return 0;
2775 }
2776
2777 static int cmd_close(void)
2778 {
2779         TALLOC_CTX *ctx = talloc_tos();
2780         char *buf = NULL;
2781         int fnum;
2782         NTSTATUS status;
2783
2784         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2785                 d_printf("close <fnum>\n");
2786                 return 1;
2787         }
2788
2789         fnum = atoi(buf);
2790         /* We really should use the targetcli here.... */
2791         status = cli_close(cli, fnum);
2792         if (!NT_STATUS_IS_OK(status)) {
2793                 d_printf("close %d: %s\n", fnum, nt_errstr(status));
2794                 return 1;
2795         }
2796         return 0;
2797 }
2798
2799 static int cmd_posix(void)
2800 {
2801         TALLOC_CTX *ctx = talloc_tos();
2802         uint16_t major, minor;
2803         uint32_t caplow, caphigh;
2804         char *caps;
2805         NTSTATUS status;
2806
2807         if (!SERVER_HAS_UNIX_CIFS(cli)) {
2808                 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2809                 return 1;
2810         }
2811
2812         status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
2813                                              &caphigh);
2814         if (!NT_STATUS_IS_OK(status)) {
2815                 d_printf("Can't get UNIX CIFS extensions version from "
2816                          "server: %s\n", nt_errstr(status));
2817                 return 1;
2818         }
2819
2820         d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2821
2822         caps = talloc_strdup(ctx, "");
2823         if (!caps) {
2824                 return 1;
2825         }
2826         if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2827                 caps = talloc_asprintf_append(caps, "locks ");
2828                 if (!caps) {
2829                         return 1;
2830                 }
2831         }
2832         if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2833                 caps = talloc_asprintf_append(caps, "acls ");
2834                 if (!caps) {
2835                         return 1;
2836                 }
2837         }
2838         if (caplow & CIFS_UNIX_XATTTR_CAP) {
2839                 caps = talloc_asprintf_append(caps, "eas ");
2840                 if (!caps) {
2841                         return 1;
2842                 }
2843         }
2844         if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2845                 caps = talloc_asprintf_append(caps, "pathnames ");
2846                 if (!caps) {
2847                         return 1;
2848                 }
2849         }
2850         if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2851                 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2852                 if (!caps) {
2853                         return 1;
2854                 }
2855         }
2856         if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2857                 caps = talloc_asprintf_append(caps, "large_read ");
2858                 if (!caps) {
2859                         return 1;
2860                 }
2861         }
2862         if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2863                 caps = talloc_asprintf_append(caps, "large_write ");
2864                 if (!caps) {
2865                         return 1;
2866                 }
2867         }
2868         if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2869                 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2870                 if (!caps) {
2871                         return 1;
2872                 }
2873         }
2874         if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2875                 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2876                 if (!caps) {
2877                         return 1;
2878                 }
2879         }
2880
2881         if (*caps && caps[strlen(caps)-1] == ' ') {
2882                 caps[strlen(caps)-1] = '\0';
2883         }
2884
2885         d_printf("Server supports CIFS capabilities %s\n", caps);
2886
2887         status = cli_set_unix_extensions_capabilities(cli, major, minor,
2888                                                       caplow, caphigh);
2889         if (!NT_STATUS_IS_OK(status)) {
2890                 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2891                          nt_errstr(status));
2892                 return 1;
2893         }
2894
2895         if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2896                 CLI_DIRSEP_CHAR = '/';
2897                 *CLI_DIRSEP_STR = '/';
2898                 client_set_cur_dir(CLI_DIRSEP_STR);
2899         }
2900
2901         return 0;
2902 }
2903
2904 static int cmd_lock(void)
2905 {
2906         TALLOC_CTX *ctx = talloc_tos();
2907         char *buf = NULL;
2908         uint64_t start, len;
2909         enum brl_type lock_type;
2910         int fnum;
2911         NTSTATUS status;
2912
2913         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2914                 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2915                 return 1;
2916         }
2917         fnum = atoi(buf);
2918
2919         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2920                 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2921                 return 1;
2922         }
2923
2924         if (*buf == 'r' || *buf == 'R') {
2925                 lock_type = READ_LOCK;
2926         } else if (*buf == 'w' || *buf == 'W') {
2927                 lock_type = WRITE_LOCK;
2928         } else {
2929                 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2930                 return 1;
2931         }
2932
2933         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2934                 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2935                 return 1;
2936         }
2937
2938         start = (uint64_t)strtol(buf, (char **)NULL, 16);
2939
2940         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2941                 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2942                 return 1;
2943         }
2944
2945         len = (uint64_t)strtol(buf, (char **)NULL, 16);
2946
2947         status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
2948         if (!NT_STATUS_IS_OK(status)) {
2949                 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
2950         }
2951
2952         return 0;
2953 }
2954
2955 static int cmd_unlock(void)
2956 {
2957         TALLOC_CTX *ctx = talloc_tos();
2958         char *buf = NULL;
2959         uint64_t start, len;
2960         int fnum;
2961         NTSTATUS status;
2962
2963         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2964                 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2965                 return 1;
2966         }
2967         fnum = atoi(buf);
2968
2969         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2970                 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2971                 return 1;
2972         }
2973
2974         start = (uint64_t)strtol(buf, (char **)NULL, 16);
2975
2976         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2977                 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2978                 return 1;
2979         }
2980
2981         len = (uint64_t)strtol(buf, (char **)NULL, 16);
2982
2983         status = cli_posix_unlock(cli, fnum, start, len);
2984         if (!NT_STATUS_IS_OK(status)) {
2985                 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
2986         }
2987
2988         return 0;
2989 }
2990
2991 static int cmd_posix_whoami(void)
2992 {
2993         TALLOC_CTX *ctx = talloc_tos();
2994         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2995         uint64_t uid = 0;
2996         uint64_t gid = 0;
2997         uint32_t num_gids = 0;
2998         uint32_t num_sids = 0;
2999         uint64_t *gids = NULL;
3000         struct dom_sid *sids = NULL;
3001         bool guest = false;
3002         uint32_t i;
3003
3004         status = cli_posix_whoami(cli,
3005                         ctx,
3006                         &uid,
3007                         &gid,
3008                         &num_gids,
3009                         &gids,
3010                         &num_sids,
3011                         &sids,
3012                         &guest);
3013
3014         if (!NT_STATUS_IS_OK(status)) {
3015                 d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
3016                 return 1;
3017         }
3018
3019         d_printf("GUEST:%s\n", guest ? "True" : "False");
3020         d_printf("UID:%" PRIu64 "\n", uid);
3021         d_printf("GID:%" PRIu64 "\n", gid);
3022         d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
3023         for (i = 0; i < num_gids; i++) {
3024                 d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
3025         }
3026         d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
3027         for (i = 0; i < num_sids; i++) {
3028                 char *sid_str = dom_sid_string(ctx, &sids[i]);
3029                 d_printf("SIDS[%" PRIu32 "]:%s\n", i, sid_str);
3030                 TALLOC_FREE(sid_str);
3031         }
3032         return 0;
3033 }
3034
3035
3036 /****************************************************************************
3037  Remove a directory.
3038 ****************************************************************************/
3039
3040 static int cmd_rmdir(void)
3041 {
3042         TALLOC_CTX *ctx = talloc_tos();
3043         char *mask = NULL;
3044         char *buf = NULL;
3045         char *targetname = NULL;
3046         struct cli_state *targetcli;
3047         NTSTATUS status;
3048
3049         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3050                 d_printf("rmdir <dirname>\n");
3051                 return 1;
3052         }
3053         mask = talloc_asprintf(ctx,
3054                         "%s%s",
3055                         client_get_cur_dir(),
3056                         buf);
3057         if (!mask) {
3058                 return 1;
3059         }
3060
3061         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3062                         cli, mask, &targetcli, &targetname);
3063         if (!NT_STATUS_IS_OK(status)) {
3064                 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3065                 return 1;
3066         }
3067
3068         status = cli_rmdir(targetcli, targetname);
3069         if (!NT_STATUS_IS_OK(status)) {
3070                 d_printf("%s removing remote directory file %s\n",
3071                          nt_errstr(status), mask);
3072         }
3073
3074         return 0;
3075 }
3076
3077 /****************************************************************************
3078  UNIX hardlink.
3079 ****************************************************************************/
3080
3081 static int cmd_link(void)
3082 {
3083         TALLOC_CTX *ctx = talloc_tos();
3084         char *oldname = NULL;
3085         char *newname = NULL;
3086         char *buf = NULL;
3087         char *buf2 = NULL;
3088         char *targetname = NULL;
3089         struct cli_state *targetcli;
3090         NTSTATUS status;
3091
3092         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3093             !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3094                 d_printf("link <oldname> <newname>\n");
3095                 return 1;
3096         }
3097         oldname = talloc_asprintf(ctx,
3098                         "%s%s",
3099                         client_get_cur_dir(),
3100                         buf);
3101         if (!oldname) {
3102                 return 1;
3103         }
3104         newname = talloc_asprintf(ctx,
3105                         "%s%s",
3106                         client_get_cur_dir(),
3107                         buf2);
3108         if (!newname) {
3109                 return 1;
3110         }
3111
3112         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3113                         cli, oldname, &targetcli, &targetname);
3114         if (!NT_STATUS_IS_OK(status)) {
3115                 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3116                 return 1;
3117         }
3118
3119         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3120                 d_printf("Server doesn't support UNIX CIFS calls.\n");
3121                 return 1;
3122         }
3123
3124         status = cli_posix_hardlink(targetcli, targetname, newname);
3125         if (!NT_STATUS_IS_OK(status)) {
3126                 d_printf("%s linking files (%s -> %s)\n",
3127                          nt_errstr(status), newname, oldname);
3128                 return 1;
3129         }
3130         return 0;
3131 }
3132
3133 /****************************************************************************
3134  UNIX readlink.
3135 ****************************************************************************/
3136
3137 static int cmd_readlink(void)
3138 {
3139         TALLOC_CTX *ctx = talloc_tos();
3140         char *name= NULL;
3141         char *buf = NULL;
3142         char *targetname = NULL;
3143         char linkname[PATH_MAX+1];
3144         struct cli_state *targetcli;
3145         NTSTATUS status;
3146
3147         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3148                 d_printf("readlink <name>\n");
3149                 return 1;
3150         }
3151         name = talloc_asprintf(ctx,
3152                         "%s%s",
3153                         client_get_cur_dir(),
3154                         buf);
3155         if (!name) {
3156                 return 1;
3157         }
3158
3159         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3160                         cli, name, &targetcli, &targetname);
3161         if (!NT_STATUS_IS_OK(status)) {
3162                 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3163                 return 1;
3164         }
3165
3166         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3167                 d_printf("Server doesn't support UNIX CIFS calls.\n");
3168                 return 1;
3169         }
3170
3171         status = cli_posix_readlink(targetcli, name, linkname, PATH_MAX+1);
3172         if (!NT_STATUS_IS_OK(status)) {
3173                 d_printf("%s readlink on file %s\n",
3174                          nt_errstr(status), name);
3175                 return 1;
3176         }
3177
3178         d_printf("%s -> %s\n", name, linkname);
3179
3180         return 0;
3181 }
3182
3183
3184 /****************************************************************************
3185  UNIX symlink.
3186 ****************************************************************************/
3187
3188 static int cmd_symlink(void)
3189 {
3190         TALLOC_CTX *ctx = talloc_tos();
3191         char *oldname = NULL;
3192         char *newname = NULL;
3193         char *buf = NULL;
3194         char *buf2 = NULL;
3195         struct cli_state *newcli;
3196         NTSTATUS status;
3197
3198         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3199             !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3200                 d_printf("symlink <oldname> <newname>\n");
3201                 return 1;
3202         }
3203         /* Oldname (link target) must be an untouched blob. */
3204         oldname = buf;
3205
3206         if (SERVER_HAS_UNIX_CIFS(cli)) {
3207                 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3208                                           buf2);
3209                 if (!newname) {
3210                         return 1;
3211                 }
3212                 /* New name must be present in share namespace. */
3213                 status = cli_resolve_path(ctx, "",
3214                                 popt_get_cmdline_auth_info(), cli, newname,
3215                                 &newcli, &newname);
3216                 if (!NT_STATUS_IS_OK(status)) {
3217                         d_printf("link %s: %s\n", oldname, nt_errstr(status));
3218                         return 1;
3219                 }
3220                 status = cli_posix_symlink(newcli, oldname, newname);
3221         } else {
3222                 status = cli_symlink(
3223                         cli, oldname, buf2,
3224                         buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3225         }
3226
3227         if (!NT_STATUS_IS_OK(status)) {
3228                 d_printf("%s symlinking files (%s -> %s)\n",
3229                          nt_errstr(status), oldname, newname);
3230                 return 1;
3231         }
3232
3233         return 0;
3234 }
3235
3236 /****************************************************************************
3237  UNIX chmod.
3238 ****************************************************************************/
3239
3240 static int cmd_chmod(void)
3241 {
3242         TALLOC_CTX *ctx = talloc_tos();
3243         char *src = NULL;
3244         char *buf = NULL;
3245         char *buf2 = NULL;
3246         char *targetname = NULL;
3247         struct cli_state *targetcli;
3248         mode_t mode;
3249         NTSTATUS status;
3250
3251         if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3252             !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3253                 d_printf("chmod mode file\n");
3254                 return 1;
3255         }
3256         src = talloc_asprintf(ctx,
3257                         "%s%s",
3258                         client_get_cur_dir(),
3259                         buf2);
3260         if (!src) {
3261                 return 1;
3262         }
3263
3264         mode = (mode_t)strtol(buf, NULL, 8);
3265
3266         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3267                         cli, src, &targetcli, &targetname);
3268         if (!NT_STATUS_IS_OK(status)) {
3269                 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3270                 return 1;
3271         }
3272
3273         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3274                 d_printf("Server doesn't support UNIX CIFS calls.\n");
3275                 return 1;
3276         }
3277
3278         status = cli_posix_chmod(targetcli, targetname, mode);
3279         if (!NT_STATUS_IS_OK(status)) {
3280                 d_printf("%s chmod file %s 0%o\n",
3281                          nt_errstr(status), src, (unsigned int)mode);
3282                 return 1;
3283         }
3284
3285         return 0;
3286 }
3287
3288 static const char *filetype_to_str(mode_t mode)
3289 {
3290         if (S_ISREG(mode)) {
3291                 return "regular file";
3292         } else if (S_ISDIR(mode)) {
3293                 return "directory";
3294         } else
3295 #ifdef S_ISCHR
3296         if (S_ISCHR(mode)) {
3297                 return "character device";
3298         } else
3299 #endif
3300 #ifdef S_ISBLK
3301         if (S_ISBLK(mode)) {
3302                 return "block device";
3303         } else
3304 #endif
3305 #ifdef S_ISFIFO
3306         if (S_ISFIFO(mode)) {
3307                 return "fifo";
3308         } else
3309 #endif
3310 #ifdef S_ISLNK
3311         if (S_ISLNK(mode)) {
3312                 return "symbolic link";
3313         } else
3314 #endif
3315 #ifdef S_ISSOCK
3316         if (S_ISSOCK(mode)) {
3317                 return "socket";
3318         } else
3319 #endif
3320         return "";
3321 }
3322
3323 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3324 {
3325         if (m & bt) {
3326                 return ret;
3327         } else {
3328                 return '-';
3329         }
3330 }
3331
3332 static char *unix_mode_to_str(char *s, mode_t m)
3333 {
3334         char *p = s;
3335         const char *str = filetype_to_str(m);
3336
3337         switch(str[0]) {
3338                 case 'd':
3339                         *p++ = 'd';
3340                         break;
3341                 case 'c':
3342                         *p++ = 'c';
3343                         break;
3344                 case 'b':
3345                         *p++ = 'b';
3346                         break;
3347                 case 'f':
3348                         *p++ = 'p';
3349                         break;
3350                 case 's':
3351                         *p++ = str[1] == 'y' ? 'l' : 's';
3352                         break;
3353                 case 'r':
3354                 default:
3355                         *p++ = '-';
3356                         break;
3357         }
3358         *p++ = rwx_to_str(m, S_IRUSR, 'r');
3359         *p++ = rwx_to_str(m, S_IWUSR, 'w');
3360         *p++ = rwx_to_str(m, S_IXUSR, 'x');
3361         *p++ = rwx_to_str(m, S_IRGRP, 'r');
3362         *p++ = rwx_to_str(m, S_IWGRP, 'w');
3363         *p++ = rwx_to_str(m, S_IXGRP, 'x');
3364         *p++ = rwx_to_str(m, S_IROTH, 'r');
3365         *p++ = rwx_to_str(m, S_IWOTH, 'w');
3366         *p++ = rwx_to_str(m, S_IXOTH, 'x');
3367         *p++ = '\0';
3368         return s;
3369 }
3370
3371 /****************************************************************************
3372  Utility function for UNIX getfacl.
3373 ****************************************************************************/
3374
3375 static char *perms_to_string(fstring permstr, unsigned char perms)
3376 {
3377         fstrcpy(permstr, "---");
3378         if (perms & SMB_POSIX_ACL_READ) {
3379                 permstr[0] = 'r';
3380         }
3381         if (perms & SMB_POSIX_ACL_WRITE) {
3382                 permstr[1] = 'w';
3383         }
3384         if (perms & SMB_POSIX_ACL_EXECUTE) {
3385                 permstr[2] = 'x';
3386         }
3387         return permstr;
3388 }
3389
3390 /****************************************************************************
3391  UNIX getfacl.
3392 ****************************************************************************/
3393
3394 static int cmd_getfacl(void)
3395 {
3396         TALLOC_CTX *ctx = talloc_tos();
3397         char *src = NULL;
3398         char *name = NULL;
3399         char *targetname = NULL;
3400         struct cli_state *targetcli;
3401         uint16_t major, minor;
3402         uint32_t caplow, caphigh;
3403         char *retbuf = NULL;
3404         size_t rb_size = 0;
3405         SMB_STRUCT_STAT sbuf;
3406         uint16_t num_file_acls = 0;
3407         uint16_t num_dir_acls = 0;
3408         uint16_t i;
3409         NTSTATUS status;
3410
3411         if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3412                 d_printf("getfacl filename\n");
3413                 return 1;
3414         }
3415         src = talloc_asprintf(ctx,
3416                         "%s%s",
3417                         client_get_cur_dir(),
3418                         name);
3419         if (!src) {
3420                 return 1;
3421         }
3422
3423         status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3424                         cli, src, &targetcli, &targetname);
3425         if (!NT_STATUS_IS_OK(status)) {
3426                 d_printf("stat %s: %s\n", src, nt_errstr(status));
3427                 return 1;
3428         }
3429
3430         if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3431                 d_printf("Server doesn't support UNIX CIFS calls.\n");
3432                 return 1;
3433         }
3434
3435         status = cli_unix_extensions_version(targetcli, &major, &minor,
3436                                              &caplow, &caphigh);
3437         if (!NT_STATUS_IS_OK(status)) {
3438                 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3439                          nt_errstr(status));
3440                 return 1;
3441         }
3442
3443         if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3444                 d_printf("This server supports UNIX extensions "
3445                         "but doesn't support POSIX ACLs.\n");
3446                 return 1;
3447         }
3448
3449         status = cli_posix_stat(targetcli, targetname, &sbuf);
3450         if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3451                 d_printf("%s getfacl doing a stat on file %s\n",
3452                          nt_errstr(status), src);
3453                 return 1;
3454         }
3455
3456         status = cli_posix_getacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3457         if (!NT_STATUS_IS_OK(status)) {
3458                 d_printf("%s getfacl file %s\n",
3459                          nt_errstr(status), src);
3460                 return 1;
3461         }
3462
3463         /* ToDo : Print out the ACL values. */
3464         if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3465                 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3466                         src, (unsigned int)CVAL(retbuf,0) );
3467                 return 1;
3468         }
3469
3470         num_file_acls = SVAL(retbuf,2);
3471         num_dir_acls = SVAL(retbuf,4);
3472         if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3473                 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3474                         src,
3475                         (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3476                         (unsigned int)rb_size);
3477                 return 1;
3478         }
3479
3480         d_printf("# file: %s\n", src);
3481         d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3482
3483         if (num_file_acls == 0 && num_dir_acls == 0) {
3484                 d_printf("No acls found.\n");
3485         }
3486
3487         for (i = 0; i < num_file_acls; i++) {
3488                 uint32_t uorg;
3489                 fstring permstring;
3490                 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3491                 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3492
3493                 switch(tagtype) {
3494                         case SMB_POSIX_ACL_USER_OBJ:
3495                                 d_printf("user::");
3496                                 break;
3497                         case SMB_POSIX_ACL_USER:
3498                                 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3499                                 d_printf("user:%u:", uorg);
3500                                 break;
3501                         case SMB_POSIX_ACL_GROUP_OBJ:
3502                                 d_printf("group::");
3503                                 break;
3504                         case SMB_POSIX_ACL_GROUP:
3505                                 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3506                                 d_printf("group:%u:", uorg);
3507                                 break;
3508                         case SMB_POSIX_ACL_MASK:
3509                                 d_printf("mask::");
3510                                 break;
3511                         case SMB_POSIX_ACL_OTHER:
3512                                 d_printf("other::");
3513                                 break;
3514                         default:
3515                                 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3516                                         src, (unsigned int)tagtype );
3517                                 SAFE_FREE(retbuf);
3518                                 return 1;
3519                 }
3520
3521                 d_printf("%s\n", perms_to_string(permstring, perms));
3522         }
3523
3524         for (i = 0; i < num_dir_acls; i++) {
3525                 uint32_t uorg;
3526                 fstring permstring;
3527                 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3528                 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3529
3530                 switch(tagtype) {
3531                         case SMB_POSIX_ACL_USER_OBJ:
3532                                 d_printf("default:user::");
3533                                 break;
3534                         case SMB_POSIX_ACL_USER:
3535                                 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3536                                 d_printf("default:user:%u:", uorg);
3537                                 break;
3538                         case SMB_POSIX_ACL_GROUP_OBJ:
3539                                 d_printf("default:group::");
3540                                 break;
3541                         case SMB_POSIX_ACL_GROUP:
3542                                 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3543                                 d_printf("default:group:%u:", uorg);
3544                                 break;
3545                         case SMB_POSIX_ACL_MASK:
3546                                 d_printf("default:mask::");
3547                                 break;
3548                         case SMB_POSIX_ACL_OTHER:
3549                                 d_printf("default:other::");
3550                                 break;
3551                         default:
3552                                 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3553                                         src, (unsigned int)tagtype );
3554                                 SAFE_FREE(retbuf);
3555                                 return 1;
3556                 }
3557
3558                 d_printf("%s\n", perms_to_string(permstring, perms));
3559         }
3560
3561         return 0;
3562 }
3563
3564 /****************************************************************************
3565  Get the EA list of a file
3566 ****************************************************************************/