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