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