printing: explicitly clear PUBLISHED attribute
[Samba.git] / source3 / client / client.c
blobf6e42f6ffe436ee81c5a9a86f89dc9f080dc82cf
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
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.
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.
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/>.
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"
40 #ifndef REGISTER
41 #define REGISTER 0
42 #endif
44 extern int do_smb_browse(void); /* mDNS browsing */
46 extern bool override_logfile;
47 extern char tar_type;
49 static int port = 0;
50 static char *service;
51 static char *desthost;
52 static char *calling_name;
53 static bool grepable = false;
54 static char *cmdstr = NULL;
55 const char *cmd_ptr = NULL;
57 static int io_bufsize = 524288;
59 static int name_type = 0x20;
60 static int max_protocol = PROTOCOL_NT1;
62 static int process_tok(char *tok);
63 static int cmd_help(void);
65 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
67 /* 30 second timeout on most commands */
68 #define CLIENT_TIMEOUT (30*1000)
69 #define SHORT_TIMEOUT (5*1000)
71 /* value for unused fid field in trans2 secondary request */
72 #define FID_UNUSED (0xFFFF)
74 time_t newer_than = 0;
75 static int archive_level = 0;
77 static bool translation = false;
78 static bool have_ip;
80 /* clitar bits insert */
81 extern int blocksize;
82 extern bool tar_inc;
83 extern bool tar_reset;
84 /* clitar bits end */
86 static bool prompt = true;
88 static bool recurse = false;
89 static bool showacls = false;
90 bool lowercase = false;
92 static struct sockaddr_storage dest_ss;
93 static char dest_ss_str[INET6_ADDRSTRLEN];
95 #define SEPARATORS " \t\n\r"
97 static bool abort_mget = true;
99 /* timing globals */
100 uint64_t get_total_size = 0;
101 unsigned int get_total_time_ms = 0;
102 static uint64_t put_total_size = 0;
103 static unsigned int put_total_time_ms = 0;
105 /* totals globals */
106 static double dir_total;
108 /* encrypted state. */
109 static bool smb_encrypt;
111 /* root cli_state connection */
113 struct cli_state *cli;
115 static char CLI_DIRSEP_CHAR = '\\';
116 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
118 /* Authentication for client connections. */
119 struct user_auth_info *auth_info;
121 /* Accessor functions for directory paths. */
122 static char *fileselection;
123 static const char *client_get_fileselection(void)
125 if (fileselection) {
126 return fileselection;
128 return "";
131 static const char *client_set_fileselection(const char *new_fs)
133 SAFE_FREE(fileselection);
134 if (new_fs) {
135 fileselection = SMB_STRDUP(new_fs);
137 return client_get_fileselection();
140 static char *cwd;
141 static const char *client_get_cwd(void)
143 if (cwd) {
144 return cwd;
146 return CLI_DIRSEP_STR;
149 static const char *client_set_cwd(const char *new_cwd)
151 SAFE_FREE(cwd);
152 if (new_cwd) {
153 cwd = SMB_STRDUP(new_cwd);
155 return client_get_cwd();
158 static char *cur_dir;
159 const char *client_get_cur_dir(void)
161 if (cur_dir) {
162 return cur_dir;
164 return CLI_DIRSEP_STR;
167 const char *client_set_cur_dir(const char *newdir)
169 SAFE_FREE(cur_dir);
170 if (newdir) {
171 cur_dir = SMB_STRDUP(newdir);
173 return client_get_cur_dir();
176 /****************************************************************************
177 Put up a yes/no prompt.
178 ****************************************************************************/
180 static bool yesno(const char *p)
182 char ans[20];
183 printf("%s",p);
185 if (!fgets(ans,sizeof(ans)-1,stdin))
186 return(False);
188 if (*ans == 'y' || *ans == 'Y')
189 return(True);
191 return(False);
194 /****************************************************************************
195 Write to a local file with CR/LF->LF translation if appropriate. Return the
196 number taken from the buffer. This may not equal the number written.
197 ****************************************************************************/
199 static int writefile(int f, char *b, int n)
201 int i;
203 if (!translation) {
204 return write(f,b,n);
207 i = 0;
208 while (i < n) {
209 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
210 b++;i++;
212 if (write(f, b, 1) != 1) {
213 break;
215 b++;
216 i++;
219 return(i);
222 /****************************************************************************
223 Read from a file with LF->CR/LF translation if appropriate. Return the
224 number read. read approx n bytes.
225 ****************************************************************************/
227 static int readfile(uint8_t *b, int n, XFILE *f)
229 int i;
230 int c;
232 if (!translation)
233 return x_fread(b,1,n,f);
235 i = 0;
236 while (i < (n - 1) && (i < BUFFER_SIZE)) {
237 if ((c = x_getc(f)) == EOF) {
238 break;
241 if (c == '\n') { /* change all LFs to CR/LF */
242 b[i++] = '\r';
245 b[i++] = c;
248 return(i);
251 struct push_state {
252 XFILE *f;
253 SMB_OFF_T nread;
256 static size_t push_source(uint8_t *buf, size_t n, void *priv)
258 struct push_state *state = (struct push_state *)priv;
259 int result;
261 if (x_feof(state->f)) {
262 return 0;
265 result = readfile(buf, n, state->f);
266 state->nread += result;
267 return result;
270 /****************************************************************************
271 Send a message.
272 ****************************************************************************/
274 static void send_message(const char *username)
276 char buf[1600];
277 NTSTATUS status;
278 int i;
280 d_printf("Type your message, ending it with a Control-D\n");
282 i = 0;
283 while (i<sizeof(buf)-2) {
284 int c = fgetc(stdin);
285 if (c == EOF) {
286 break;
288 if (c == '\n') {
289 buf[i++] = '\r';
291 buf[i++] = c;
293 buf[i] = '\0';
295 status = cli_message(cli, desthost, username, buf);
296 if (!NT_STATUS_IS_OK(status)) {
297 d_fprintf(stderr, "cli_message returned %s\n",
298 nt_errstr(status));
302 /****************************************************************************
303 Check the space on a device.
304 ****************************************************************************/
306 static int do_dskattr(void)
308 int total, bsize, avail;
309 struct cli_state *targetcli = NULL;
310 char *targetpath = NULL;
311 TALLOC_CTX *ctx = talloc_tos();
312 NTSTATUS status;
314 if ( !cli_resolve_path(ctx, "", auth_info, cli, client_get_cur_dir(), &targetcli, &targetpath)) {
315 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
316 return 1;
319 status = cli_dskattr(targetcli, &bsize, &total, &avail);
320 if (!NT_STATUS_IS_OK(status)) {
321 d_printf("Error in dskattr: %s\n", nt_errstr(status));
322 return 1;
325 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
326 total, bsize, avail);
328 return 0;
331 /****************************************************************************
332 Show cd/pwd.
333 ****************************************************************************/
335 static int cmd_pwd(void)
337 d_printf("Current directory is %s",service);
338 d_printf("%s\n",client_get_cur_dir());
339 return 0;
342 /****************************************************************************
343 Ensure name has correct directory separators.
344 ****************************************************************************/
346 static void normalize_name(char *newdir)
348 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
349 string_replace(newdir,'/','\\');
353 /****************************************************************************
354 Change directory - inner section.
355 ****************************************************************************/
357 static int do_cd(const char *new_dir)
359 char *newdir = NULL;
360 char *saved_dir = NULL;
361 char *new_cd = NULL;
362 char *targetpath = NULL;
363 struct cli_state *targetcli = NULL;
364 SMB_STRUCT_STAT sbuf;
365 uint32 attributes;
366 int ret = 1;
367 TALLOC_CTX *ctx = talloc_stackframe();
369 newdir = talloc_strdup(ctx, new_dir);
370 if (!newdir) {
371 TALLOC_FREE(ctx);
372 return 1;
375 normalize_name(newdir);
377 /* Save the current directory in case the new directory is invalid */
379 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
380 if (!saved_dir) {
381 TALLOC_FREE(ctx);
382 return 1;
385 if (*newdir == CLI_DIRSEP_CHAR) {
386 client_set_cur_dir(newdir);
387 new_cd = newdir;
388 } else {
389 new_cd = talloc_asprintf(ctx, "%s%s",
390 client_get_cur_dir(),
391 newdir);
392 if (!new_cd) {
393 goto out;
397 /* Ensure cur_dir ends in a DIRSEP */
398 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
399 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
400 if (!new_cd) {
401 goto out;
404 client_set_cur_dir(new_cd);
406 new_cd = clean_name(ctx, new_cd);
407 client_set_cur_dir(new_cd);
409 if ( !cli_resolve_path(ctx, "", auth_info, cli, new_cd, &targetcli, &targetpath)) {
410 d_printf("cd %s: %s\n", new_cd, cli_errstr(cli));
411 client_set_cur_dir(saved_dir);
412 goto out;
415 if (strequal(targetpath,CLI_DIRSEP_STR )) {
416 TALLOC_FREE(ctx);
417 return 0;
420 /* Use a trans2_qpathinfo to test directories for modern servers.
421 Except Win9x doesn't support the qpathinfo_basic() call..... */
423 if (targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95) {
424 NTSTATUS status;
426 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
427 &attributes);
428 if (!NT_STATUS_IS_OK(status)) {
429 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
430 client_set_cur_dir(saved_dir);
431 goto out;
434 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
435 d_printf("cd %s: not a directory\n", new_cd);
436 client_set_cur_dir(saved_dir);
437 goto out;
439 } else {
440 NTSTATUS status;
442 targetpath = talloc_asprintf(ctx,
443 "%s%s",
444 targetpath,
445 CLI_DIRSEP_STR );
446 if (!targetpath) {
447 client_set_cur_dir(saved_dir);
448 goto out;
450 targetpath = clean_name(ctx, targetpath);
451 if (!targetpath) {
452 client_set_cur_dir(saved_dir);
453 goto out;
456 status = cli_chkpath(targetcli, targetpath);
457 if (!NT_STATUS_IS_OK(status)) {
458 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
459 client_set_cur_dir(saved_dir);
460 goto out;
464 ret = 0;
466 out:
468 TALLOC_FREE(ctx);
469 return ret;
472 /****************************************************************************
473 Change directory.
474 ****************************************************************************/
476 static int cmd_cd(void)
478 char *buf = NULL;
479 int rc = 0;
481 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
482 rc = do_cd(buf);
483 } else {
484 d_printf("Current directory is %s\n",client_get_cur_dir());
487 return rc;
490 /****************************************************************************
491 Change directory.
492 ****************************************************************************/
494 static int cmd_cd_oneup(void)
496 return do_cd("..");
499 /*******************************************************************
500 Decide if a file should be operated on.
501 ********************************************************************/
503 static bool do_this_one(struct file_info *finfo)
505 if (!finfo->name) {
506 return false;
509 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
510 return true;
513 if (*client_get_fileselection() &&
514 !mask_match(finfo->name,client_get_fileselection(),false)) {
515 DEBUG(3,("mask_match %s failed\n", finfo->name));
516 return false;
519 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
520 DEBUG(3,("newer_than %s failed\n", finfo->name));
521 return false;
524 if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
525 DEBUG(3,("archive %s failed\n", finfo->name));
526 return false;
529 return true;
532 /****************************************************************************
533 Display info about a file.
534 ****************************************************************************/
536 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
537 const char *dir)
539 time_t t;
540 TALLOC_CTX *ctx = talloc_tos();
541 NTSTATUS status = NT_STATUS_OK;
543 if (!do_this_one(finfo)) {
544 return NT_STATUS_OK;
547 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
548 if (!showacls) {
549 d_printf(" %-30s%7.7s %8.0f %s",
550 finfo->name,
551 attrib_string(finfo->mode),
552 (double)finfo->size,
553 time_to_asc(t));
554 dir_total += finfo->size;
555 } else {
556 char *afname = NULL;
557 uint16_t fnum;
559 /* skip if this is . or .. */
560 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
561 return NT_STATUS_OK;
562 /* create absolute filename for cli_ntcreate() FIXME */
563 afname = talloc_asprintf(ctx,
564 "%s%s%s",
565 dir,
566 CLI_DIRSEP_STR,
567 finfo->name);
568 if (!afname) {
569 return NT_STATUS_NO_MEMORY;
571 /* print file meta date header */
572 d_printf( "FILENAME:%s\n", finfo->name);
573 d_printf( "MODE:%s\n", attrib_string(finfo->mode));
574 d_printf( "SIZE:%.0f\n", (double)finfo->size);
575 d_printf( "MTIME:%s", time_to_asc(t));
576 status = cli_ntcreate(cli_state, afname, 0,
577 CREATE_ACCESS_READ, 0,
578 FILE_SHARE_READ|FILE_SHARE_WRITE,
579 FILE_OPEN, 0x0, 0x0, &fnum);
580 if (!NT_STATUS_IS_OK(status)) {
581 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
582 afname, nt_errstr(status)));
583 } else {
584 struct security_descriptor *sd = NULL;
585 sd = cli_query_secdesc(cli_state, fnum, ctx);
586 if (!sd) {
587 DEBUG( 0, ("display_finfo() failed to "
588 "get security descriptor: %s",
589 cli_errstr(cli_state)));
590 status = cli_nt_error(cli_state);
591 } else {
592 display_sec_desc(sd);
594 TALLOC_FREE(sd);
596 TALLOC_FREE(afname);
598 return status;
601 /****************************************************************************
602 Accumulate size of a file.
603 ****************************************************************************/
605 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
606 const char *dir)
608 if (do_this_one(finfo)) {
609 dir_total += finfo->size;
611 return NT_STATUS_OK;
614 static bool do_list_recurse;
615 static bool do_list_dirs;
616 static char *do_list_queue = 0;
617 static long do_list_queue_size = 0;
618 static long do_list_queue_start = 0;
619 static long do_list_queue_end = 0;
620 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
621 const char *dir);
623 /****************************************************************************
624 Functions for do_list_queue.
625 ****************************************************************************/
628 * The do_list_queue is a NUL-separated list of strings stored in a
629 * char*. Since this is a FIFO, we keep track of the beginning and
630 * ending locations of the data in the queue. When we overflow, we
631 * double the size of the char*. When the start of the data passes
632 * the midpoint, we move everything back. This is logically more
633 * complex than a linked list, but easier from a memory management
634 * angle. In any memory error condition, do_list_queue is reset.
635 * Functions check to ensure that do_list_queue is non-NULL before
636 * accessing it.
639 static void reset_do_list_queue(void)
641 SAFE_FREE(do_list_queue);
642 do_list_queue_size = 0;
643 do_list_queue_start = 0;
644 do_list_queue_end = 0;
647 static void init_do_list_queue(void)
649 reset_do_list_queue();
650 do_list_queue_size = 1024;
651 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
652 if (do_list_queue == 0) {
653 d_printf("malloc fail for size %d\n",
654 (int)do_list_queue_size);
655 reset_do_list_queue();
656 } else {
657 memset(do_list_queue, 0, do_list_queue_size);
661 static void adjust_do_list_queue(void)
664 * If the starting point of the queue is more than half way through,
665 * move everything toward the beginning.
668 if (do_list_queue == NULL) {
669 DEBUG(4,("do_list_queue is empty\n"));
670 do_list_queue_start = do_list_queue_end = 0;
671 return;
674 if (do_list_queue_start == do_list_queue_end) {
675 DEBUG(4,("do_list_queue is empty\n"));
676 do_list_queue_start = do_list_queue_end = 0;
677 *do_list_queue = '\0';
678 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
679 DEBUG(4,("sliding do_list_queue backward\n"));
680 memmove(do_list_queue,
681 do_list_queue + do_list_queue_start,
682 do_list_queue_end - do_list_queue_start);
683 do_list_queue_end -= do_list_queue_start;
684 do_list_queue_start = 0;
688 static void add_to_do_list_queue(const char *entry)
690 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
691 while (new_end > do_list_queue_size) {
692 do_list_queue_size *= 2;
693 DEBUG(4,("enlarging do_list_queue to %d\n",
694 (int)do_list_queue_size));
695 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
696 if (! do_list_queue) {
697 d_printf("failure enlarging do_list_queue to %d bytes\n",
698 (int)do_list_queue_size);
699 reset_do_list_queue();
700 } else {
701 memset(do_list_queue + do_list_queue_size / 2,
702 0, do_list_queue_size / 2);
705 if (do_list_queue) {
706 safe_strcpy_base(do_list_queue + do_list_queue_end,
707 entry, do_list_queue, do_list_queue_size);
708 do_list_queue_end = new_end;
709 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
710 entry, (int)do_list_queue_start, (int)do_list_queue_end));
714 static char *do_list_queue_head(void)
716 return do_list_queue + do_list_queue_start;
719 static void remove_do_list_queue_head(void)
721 if (do_list_queue_end > do_list_queue_start) {
722 do_list_queue_start += strlen(do_list_queue_head()) + 1;
723 adjust_do_list_queue();
724 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
725 (int)do_list_queue_start, (int)do_list_queue_end));
729 static int do_list_queue_empty(void)
731 return (! (do_list_queue && *do_list_queue));
734 /****************************************************************************
735 A helper for do_list.
736 ****************************************************************************/
738 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
739 const char *mask, void *state)
741 struct cli_state *cli_state = (struct cli_state *)state;
742 TALLOC_CTX *ctx = talloc_tos();
743 char *dir = NULL;
744 char *dir_end = NULL;
745 NTSTATUS status = NT_STATUS_OK;
747 /* Work out the directory. */
748 dir = talloc_strdup(ctx, mask);
749 if (!dir) {
750 return NT_STATUS_NO_MEMORY;
752 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
753 *dir_end = '\0';
756 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
757 if (do_list_dirs && do_this_one(f)) {
758 status = do_list_fn(cli_state, f, dir);
759 if (!NT_STATUS_IS_OK(status)) {
760 return status;
763 if (do_list_recurse &&
764 f->name &&
765 !strequal(f->name,".") &&
766 !strequal(f->name,"..")) {
767 char *mask2 = NULL;
768 char *p = NULL;
770 if (!f->name[0]) {
771 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
772 TALLOC_FREE(dir);
773 return NT_STATUS_UNSUCCESSFUL;
776 mask2 = talloc_asprintf(ctx,
777 "%s%s",
778 mntpoint,
779 mask);
780 if (!mask2) {
781 TALLOC_FREE(dir);
782 return NT_STATUS_NO_MEMORY;
784 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
785 if (p) {
786 p[1] = 0;
787 } else {
788 mask2[0] = '\0';
790 mask2 = talloc_asprintf_append(mask2,
791 "%s%s*",
792 f->name,
793 CLI_DIRSEP_STR);
794 if (!mask2) {
795 TALLOC_FREE(dir);
796 return NT_STATUS_NO_MEMORY;
798 add_to_do_list_queue(mask2);
799 TALLOC_FREE(mask2);
801 TALLOC_FREE(dir);
802 return NT_STATUS_OK;
805 if (do_this_one(f)) {
806 status = do_list_fn(cli_state, f, dir);
808 TALLOC_FREE(dir);
809 return status;
812 /****************************************************************************
813 A wrapper around cli_list that adds recursion.
814 ****************************************************************************/
816 NTSTATUS do_list(const char *mask,
817 uint16 attribute,
818 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
819 const char *dir),
820 bool rec,
821 bool dirs)
823 static int in_do_list = 0;
824 TALLOC_CTX *ctx = talloc_tos();
825 struct cli_state *targetcli = NULL;
826 char *targetpath = NULL;
827 NTSTATUS ret_status = NT_STATUS_OK;
828 NTSTATUS status = NT_STATUS_OK;
830 if (in_do_list && rec) {
831 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
832 exit(1);
835 in_do_list = 1;
837 do_list_recurse = rec;
838 do_list_dirs = dirs;
839 do_list_fn = fn;
841 if (rec) {
842 init_do_list_queue();
843 add_to_do_list_queue(mask);
845 while (!do_list_queue_empty()) {
847 * Need to copy head so that it doesn't become
848 * invalid inside the call to cli_list. This
849 * would happen if the list were expanded
850 * during the call.
851 * Fix from E. Jay Berkenbilt (ejb@ql.org)
853 char *head = talloc_strdup(ctx, do_list_queue_head());
855 if (!head) {
856 return NT_STATUS_NO_MEMORY;
859 /* check for dfs */
861 if ( !cli_resolve_path(ctx, "", auth_info, cli, head, &targetcli, &targetpath ) ) {
862 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
863 remove_do_list_queue_head();
864 continue;
867 status = cli_list(targetcli, targetpath, attribute,
868 do_list_helper, targetcli);
869 if (!NT_STATUS_IS_OK(status)) {
870 d_printf("%s listing %s\n",
871 nt_errstr(status), targetpath);
872 ret_status = status;
874 remove_do_list_queue_head();
875 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
876 char *next_file = do_list_queue_head();
877 char *save_ch = 0;
878 if ((strlen(next_file) >= 2) &&
879 (next_file[strlen(next_file) - 1] == '*') &&
880 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
881 save_ch = next_file +
882 strlen(next_file) - 2;
883 *save_ch = '\0';
884 if (showacls) {
885 /* cwd is only used if showacls is on */
886 client_set_cwd(next_file);
889 if (!showacls) /* don't disturbe the showacls output */
890 d_printf("\n%s\n",next_file);
891 if (save_ch) {
892 *save_ch = CLI_DIRSEP_CHAR;
895 TALLOC_FREE(head);
896 TALLOC_FREE(targetpath);
898 } else {
899 /* check for dfs */
900 if (cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetpath)) {
902 status = cli_list(targetcli, targetpath, attribute,
903 do_list_helper, targetcli);
904 if (!NT_STATUS_IS_OK(status)) {
905 d_printf("%s listing %s\n",
906 nt_errstr(status), targetpath);
907 ret_status = status;
909 TALLOC_FREE(targetpath);
910 } else {
911 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
912 ret_status = cli_nt_error(cli);
916 in_do_list = 0;
917 reset_do_list_queue();
918 return ret_status;
921 /****************************************************************************
922 Get a directory listing.
923 ****************************************************************************/
925 static int cmd_dir(void)
927 TALLOC_CTX *ctx = talloc_tos();
928 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
929 char *mask = NULL;
930 char *buf = NULL;
931 int rc = 1;
932 NTSTATUS status;
934 dir_total = 0;
935 mask = talloc_strdup(ctx, client_get_cur_dir());
936 if (!mask) {
937 return 1;
940 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
941 normalize_name(buf);
942 if (*buf == CLI_DIRSEP_CHAR) {
943 mask = talloc_strdup(ctx, buf);
944 } else {
945 mask = talloc_asprintf_append(mask, "%s", buf);
947 } else {
948 mask = talloc_asprintf_append(mask, "*");
950 if (!mask) {
951 return 1;
954 if (showacls) {
955 /* cwd is only used if showacls is on */
956 client_set_cwd(client_get_cur_dir());
959 status = do_list(mask, attribute, display_finfo, recurse, true);
960 if (!NT_STATUS_IS_OK(status)) {
961 return 1;
964 rc = do_dskattr();
966 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
968 return rc;
971 /****************************************************************************
972 Get a directory listing.
973 ****************************************************************************/
975 static int cmd_du(void)
977 TALLOC_CTX *ctx = talloc_tos();
978 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
979 char *mask = NULL;
980 char *buf = NULL;
981 NTSTATUS status;
982 int rc = 1;
984 dir_total = 0;
985 mask = talloc_strdup(ctx, client_get_cur_dir());
986 if (!mask) {
987 return 1;
989 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
990 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
991 if (!mask) {
992 return 1;
996 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
997 normalize_name(buf);
998 if (*buf == CLI_DIRSEP_CHAR) {
999 mask = talloc_strdup(ctx, buf);
1000 } else {
1001 mask = talloc_asprintf_append(mask, "%s", buf);
1003 } else {
1004 mask = talloc_strdup(ctx, "*");
1007 status = do_list(mask, attribute, do_du, recurse, true);
1008 if (!NT_STATUS_IS_OK(status)) {
1009 return 1;
1012 rc = do_dskattr();
1014 d_printf("Total number of bytes: %.0f\n", dir_total);
1016 return rc;
1019 static int cmd_echo(void)
1021 TALLOC_CTX *ctx = talloc_tos();
1022 char *num;
1023 char *data;
1024 NTSTATUS status;
1026 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1027 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1028 d_printf("echo <num> <data>\n");
1029 return 1;
1032 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1034 if (!NT_STATUS_IS_OK(status)) {
1035 d_printf("echo failed: %s\n", nt_errstr(status));
1036 return 1;
1039 return 0;
1042 /****************************************************************************
1043 Get a file from rname to lname
1044 ****************************************************************************/
1046 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1048 int *pfd = (int *)priv;
1049 if (writefile(*pfd, buf, n) == -1) {
1050 return map_nt_error_from_unix(errno);
1052 return NT_STATUS_OK;
1055 static int do_get(const char *rname, const char *lname_in, bool reget)
1057 TALLOC_CTX *ctx = talloc_tos();
1058 int handle = 0;
1059 uint16_t fnum;
1060 bool newhandle = false;
1061 struct timespec tp_start;
1062 uint16 attr;
1063 SMB_OFF_T size;
1064 off_t start = 0;
1065 SMB_OFF_T nread = 0;
1066 int rc = 0;
1067 struct cli_state *targetcli = NULL;
1068 char *targetname = NULL;
1069 char *lname = NULL;
1070 NTSTATUS status;
1072 lname = talloc_strdup(ctx, lname_in);
1073 if (!lname) {
1074 return 1;
1077 if (lowercase) {
1078 strlower_m(lname);
1081 if (!cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli, &targetname ) ) {
1082 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1083 return 1;
1086 clock_gettime_mono(&tp_start);
1088 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 d_printf("%s opening remote file %s\n", nt_errstr(status),
1091 rname);
1092 return 1;
1095 if(!strcmp(lname,"-")) {
1096 handle = fileno(stdout);
1097 } else {
1098 if (reget) {
1099 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
1100 if (handle >= 0) {
1101 start = sys_lseek(handle, 0, SEEK_END);
1102 if (start == -1) {
1103 d_printf("Error seeking local file\n");
1104 return 1;
1107 } else {
1108 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1110 newhandle = true;
1112 if (handle < 0) {
1113 d_printf("Error opening local file %s\n",lname);
1114 return 1;
1118 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1119 NULL, NULL, NULL);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1122 NULL);
1123 if(!NT_STATUS_IS_OK(status)) {
1124 d_printf("getattrib: %s\n", nt_errstr(status));
1125 return 1;
1129 DEBUG(1,("getting file %s of size %.0f as %s ",
1130 rname, (double)size, lname));
1132 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1133 writefile_sink, (void *)&handle, &nread);
1134 if (!NT_STATUS_IS_OK(status)) {
1135 d_fprintf(stderr, "parallel_read returned %s\n",
1136 nt_errstr(status));
1137 cli_close(targetcli, fnum);
1138 return 1;
1141 status = cli_close(targetcli, fnum);
1142 if (!NT_STATUS_IS_OK(status)) {
1143 d_printf("Error %s closing remote file\n", nt_errstr(status));
1144 rc = 1;
1147 if (newhandle) {
1148 close(handle);
1151 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1152 cli_setatr(cli, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
1156 struct timespec tp_end;
1157 int this_time;
1159 clock_gettime_mono(&tp_end);
1160 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1161 get_total_time_ms += this_time;
1162 get_total_size += nread;
1164 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1165 nread / (1.024*this_time + 1.0e-4),
1166 get_total_size / (1.024*get_total_time_ms)));
1169 TALLOC_FREE(targetname);
1170 return rc;
1173 /****************************************************************************
1174 Get a file.
1175 ****************************************************************************/
1177 static int cmd_get(void)
1179 TALLOC_CTX *ctx = talloc_tos();
1180 char *lname = NULL;
1181 char *rname = NULL;
1182 char *fname = NULL;
1184 rname = talloc_strdup(ctx, client_get_cur_dir());
1185 if (!rname) {
1186 return 1;
1189 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1190 d_printf("get <filename> [localname]\n");
1191 return 1;
1193 rname = talloc_asprintf_append(rname, "%s", fname);
1194 if (!rname) {
1195 return 1;
1197 rname = clean_name(ctx, rname);
1198 if (!rname) {
1199 return 1;
1202 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1203 if (!lname) {
1204 lname = fname;
1207 return do_get(rname, lname, false);
1210 /****************************************************************************
1211 Do an mget operation on one file.
1212 ****************************************************************************/
1214 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1215 const char *dir)
1217 TALLOC_CTX *ctx = talloc_tos();
1218 NTSTATUS status = NT_STATUS_OK;
1219 char *rname = NULL;
1220 char *quest = NULL;
1221 char *saved_curdir = NULL;
1222 char *mget_mask = NULL;
1223 char *new_cd = NULL;
1225 if (!finfo->name) {
1226 return NT_STATUS_OK;
1229 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1230 return NT_STATUS_OK;
1232 if (abort_mget) {
1233 d_printf("mget aborted\n");
1234 return NT_STATUS_UNSUCCESSFUL;
1237 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
1238 if (asprintf(&quest,
1239 "Get directory %s? ",finfo->name) < 0) {
1240 return NT_STATUS_NO_MEMORY;
1242 } else {
1243 if (asprintf(&quest,
1244 "Get file %s? ",finfo->name) < 0) {
1245 return NT_STATUS_NO_MEMORY;
1249 if (prompt && !yesno(quest)) {
1250 SAFE_FREE(quest);
1251 return NT_STATUS_OK;
1253 SAFE_FREE(quest);
1255 if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
1256 rname = talloc_asprintf(ctx,
1257 "%s%s",
1258 client_get_cur_dir(),
1259 finfo->name);
1260 if (!rname) {
1261 return NT_STATUS_NO_MEMORY;
1263 do_get(rname, finfo->name, false);
1264 TALLOC_FREE(rname);
1265 return NT_STATUS_OK;
1268 /* handle directories */
1269 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1270 if (!saved_curdir) {
1271 return NT_STATUS_NO_MEMORY;
1274 new_cd = talloc_asprintf(ctx,
1275 "%s%s%s",
1276 client_get_cur_dir(),
1277 finfo->name,
1278 CLI_DIRSEP_STR);
1279 if (!new_cd) {
1280 return NT_STATUS_NO_MEMORY;
1282 client_set_cur_dir(new_cd);
1284 string_replace(finfo->name,'\\','/');
1285 if (lowercase) {
1286 strlower_m(finfo->name);
1289 if (!directory_exist(finfo->name) &&
1290 mkdir(finfo->name,0777) != 0) {
1291 d_printf("failed to create directory %s\n",finfo->name);
1292 client_set_cur_dir(saved_curdir);
1293 return map_nt_error_from_unix(errno);
1296 if (chdir(finfo->name) != 0) {
1297 d_printf("failed to chdir to directory %s\n",finfo->name);
1298 client_set_cur_dir(saved_curdir);
1299 return map_nt_error_from_unix(errno);
1302 mget_mask = talloc_asprintf(ctx,
1303 "%s*",
1304 client_get_cur_dir());
1306 if (!mget_mask) {
1307 return NT_STATUS_NO_MEMORY;
1310 status = do_list(mget_mask,
1311 (FILE_ATTRIBUTE_SYSTEM
1312 | FILE_ATTRIBUTE_HIDDEN
1313 | FILE_ATTRIBUTE_DIRECTORY),
1314 do_mget, false, true);
1315 if (!NT_STATUS_IS_OK(status)
1316 && !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1318 * Ignore access denied errors to ensure all permitted files are
1319 * pulled down.
1321 return status;
1324 if (chdir("..") == -1) {
1325 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1326 strerror(errno) );
1327 return map_nt_error_from_unix(errno);
1329 client_set_cur_dir(saved_curdir);
1330 TALLOC_FREE(mget_mask);
1331 TALLOC_FREE(saved_curdir);
1332 TALLOC_FREE(new_cd);
1333 return NT_STATUS_OK;
1336 /****************************************************************************
1337 View the file using the pager.
1338 ****************************************************************************/
1340 static int cmd_more(void)
1342 TALLOC_CTX *ctx = talloc_tos();
1343 char *rname = NULL;
1344 char *fname = NULL;
1345 char *lname = NULL;
1346 char *pager_cmd = NULL;
1347 const char *pager;
1348 int fd;
1349 int rc = 0;
1351 rname = talloc_strdup(ctx, client_get_cur_dir());
1352 if (!rname) {
1353 return 1;
1356 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1357 if (!lname) {
1358 return 1;
1360 fd = mkstemp(lname);
1361 if (fd == -1) {
1362 d_printf("failed to create temporary file for more\n");
1363 return 1;
1365 close(fd);
1367 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1368 d_printf("more <filename>\n");
1369 unlink(lname);
1370 return 1;
1372 rname = talloc_asprintf_append(rname, "%s", fname);
1373 if (!rname) {
1374 return 1;
1376 rname = clean_name(ctx,rname);
1377 if (!rname) {
1378 return 1;
1381 rc = do_get(rname, lname, false);
1383 pager=getenv("PAGER");
1385 pager_cmd = talloc_asprintf(ctx,
1386 "%s %s",
1387 (pager? pager:PAGER),
1388 lname);
1389 if (!pager_cmd) {
1390 return 1;
1392 if (system(pager_cmd) == -1) {
1393 d_printf("system command '%s' returned -1\n",
1394 pager_cmd);
1396 unlink(lname);
1398 return rc;
1401 /****************************************************************************
1402 Do a mget command.
1403 ****************************************************************************/
1405 static int cmd_mget(void)
1407 TALLOC_CTX *ctx = talloc_tos();
1408 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1409 char *mget_mask = NULL;
1410 char *buf = NULL;
1411 NTSTATUS status = NT_STATUS_OK;
1413 if (recurse) {
1414 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1417 abort_mget = false;
1419 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1421 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1422 if (!mget_mask) {
1423 return 1;
1425 if (*buf == CLI_DIRSEP_CHAR) {
1426 mget_mask = talloc_strdup(ctx, buf);
1427 } else {
1428 mget_mask = talloc_asprintf_append(mget_mask,
1429 "%s", buf);
1431 if (!mget_mask) {
1432 return 1;
1434 status = do_list(mget_mask, attribute, do_mget, false, true);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 return 1;
1440 if (mget_mask == NULL) {
1441 d_printf("nothing to mget\n");
1442 return 0;
1445 if (!*mget_mask) {
1446 mget_mask = talloc_asprintf(ctx,
1447 "%s*",
1448 client_get_cur_dir());
1449 if (!mget_mask) {
1450 return 1;
1452 status = do_list(mget_mask, attribute, do_mget, false, true);
1453 if (!NT_STATUS_IS_OK(status)) {
1454 return 1;
1458 return 0;
1461 /****************************************************************************
1462 Make a directory of name "name".
1463 ****************************************************************************/
1465 static bool do_mkdir(const char *name)
1467 TALLOC_CTX *ctx = talloc_tos();
1468 struct cli_state *targetcli;
1469 char *targetname = NULL;
1470 NTSTATUS status;
1472 if (!cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli, &targetname)) {
1473 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1474 return false;
1477 status = cli_mkdir(targetcli, targetname);
1478 if (!NT_STATUS_IS_OK(status)) {
1479 d_printf("%s making remote directory %s\n",
1480 nt_errstr(status),name);
1481 return false;
1484 return true;
1487 /****************************************************************************
1488 Show 8.3 name of a file.
1489 ****************************************************************************/
1491 static bool do_altname(const char *name)
1493 fstring altname;
1494 NTSTATUS status;
1496 status = cli_qpathinfo_alt_name(cli, name, altname);
1497 if (!NT_STATUS_IS_OK(status)) {
1498 d_printf("%s getting alt name for %s\n",
1499 nt_errstr(status),name);
1500 return false;
1502 d_printf("%s\n", altname);
1504 return true;
1507 /****************************************************************************
1508 Exit client.
1509 ****************************************************************************/
1511 static int cmd_quit(void)
1513 cli_shutdown(cli);
1514 exit(0);
1515 /* NOTREACHED */
1516 return 0;
1519 /****************************************************************************
1520 Make a directory.
1521 ****************************************************************************/
1523 static int cmd_mkdir(void)
1525 TALLOC_CTX *ctx = talloc_tos();
1526 char *mask = NULL;
1527 char *buf = NULL;
1529 mask = talloc_strdup(ctx, client_get_cur_dir());
1530 if (!mask) {
1531 return 1;
1534 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1535 if (!recurse) {
1536 d_printf("mkdir <dirname>\n");
1538 return 1;
1540 mask = talloc_asprintf_append(mask, "%s", buf);
1541 if (!mask) {
1542 return 1;
1545 if (recurse) {
1546 char *ddir = NULL;
1547 char *ddir2 = NULL;
1548 struct cli_state *targetcli;
1549 char *targetname = NULL;
1550 char *p = NULL;
1551 char *saveptr;
1553 ddir2 = talloc_strdup(ctx, "");
1554 if (!ddir2) {
1555 return 1;
1558 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
1559 return 1;
1562 ddir = talloc_strdup(ctx, targetname);
1563 if (!ddir) {
1564 return 1;
1566 trim_char(ddir,'.','\0');
1567 p = strtok_r(ddir, "/\\", &saveptr);
1568 while (p) {
1569 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1570 if (!ddir2) {
1571 return 1;
1573 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1574 do_mkdir(ddir2);
1576 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1577 if (!ddir2) {
1578 return 1;
1580 p = strtok_r(NULL, "/\\", &saveptr);
1582 } else {
1583 do_mkdir(mask);
1586 return 0;
1589 /****************************************************************************
1590 Show alt name.
1591 ****************************************************************************/
1593 static int cmd_altname(void)
1595 TALLOC_CTX *ctx = talloc_tos();
1596 char *name;
1597 char *buf;
1599 name = talloc_strdup(ctx, client_get_cur_dir());
1600 if (!name) {
1601 return 1;
1604 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1605 d_printf("altname <file>\n");
1606 return 1;
1608 name = talloc_asprintf_append(name, "%s", buf);
1609 if (!name) {
1610 return 1;
1612 do_altname(name);
1613 return 0;
1616 static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1618 char *attrs = TALLOC_ZERO_ARRAY(mem_ctx, char, 17);
1619 int i = 0;
1621 if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1622 if (mode & FILE_ATTRIBUTE_ENCRYPTED) {
1623 attrs[i++] = 'E';
1625 if (mode & FILE_ATTRIBUTE_NONINDEXED) {
1626 attrs[i++] = 'N';
1628 if (mode & FILE_ATTRIBUTE_OFFLINE) {
1629 attrs[i++] = 'O';
1631 if (mode & FILE_ATTRIBUTE_COMPRESSED) {
1632 attrs[i++] = 'C';
1634 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1635 attrs[i++] = 'r';
1637 if (mode & FILE_ATTRIBUTE_SPARSE) {
1638 attrs[i++] = 's';
1640 if (mode & FILE_ATTRIBUTE_TEMPORARY) {
1641 attrs[i++] = 'T';
1643 if (mode & FILE_ATTRIBUTE_NORMAL) {
1644 attrs[i++] = 'N';
1646 if (mode & FILE_ATTRIBUTE_READONLY) {
1647 attrs[i++] = 'R';
1649 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1650 attrs[i++] = 'H';
1652 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1653 attrs[i++] = 'S';
1655 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1656 attrs[i++] = 'D';
1658 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1659 attrs[i++] = 'A';
1662 return attrs;
1665 /****************************************************************************
1666 Show all info we can get
1667 ****************************************************************************/
1669 static int do_allinfo(const char *name)
1671 fstring altname;
1672 struct timespec b_time, a_time, m_time, c_time;
1673 SMB_OFF_T size;
1674 uint16_t mode;
1675 SMB_INO_T ino;
1676 NTTIME tmp;
1677 uint16_t fnum;
1678 unsigned int num_streams;
1679 struct stream_struct *streams;
1680 int num_snapshots;
1681 char **snapshots;
1682 unsigned int i;
1683 NTSTATUS status;
1685 status = cli_qpathinfo_alt_name(cli, name, altname);
1686 if (!NT_STATUS_IS_OK(status)) {
1687 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1688 name);
1689 return false;
1691 d_printf("altname: %s\n", altname);
1693 status = cli_qpathinfo2(cli, name, &b_time, &a_time, &m_time, &c_time,
1694 &size, &mode, &ino);
1695 if (!NT_STATUS_IS_OK(status)) {
1696 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1697 name);
1698 return false;
1701 unix_timespec_to_nt_time(&tmp, b_time);
1702 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1704 unix_timespec_to_nt_time(&tmp, a_time);
1705 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1707 unix_timespec_to_nt_time(&tmp, m_time);
1708 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1710 unix_timespec_to_nt_time(&tmp, c_time);
1711 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1713 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
1715 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1716 &streams);
1717 if (!NT_STATUS_IS_OK(status)) {
1718 d_printf("%s getting streams for %s\n", nt_errstr(status),
1719 name);
1720 return false;
1723 for (i=0; i<num_streams; i++) {
1724 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1725 (unsigned long long)streams[i].size);
1728 status = cli_ntcreate(cli, name, 0,
1729 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1730 SEC_STD_SYNCHRONIZE, 0,
1731 FILE_SHARE_READ|FILE_SHARE_WRITE
1732 |FILE_SHARE_DELETE,
1733 FILE_OPEN, 0x0, 0x0, &fnum);
1734 if (!NT_STATUS_IS_OK(status)) {
1736 * Ignore failure, it does not hurt if we can't list
1737 * snapshots
1739 return 0;
1741 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1742 true, &snapshots, &num_snapshots);
1743 if (!NT_STATUS_IS_OK(status)) {
1744 cli_close(cli, fnum);
1745 return 0;
1748 for (i=0; i<num_snapshots; i++) {
1749 char *snap_name;
1751 d_printf("%s\n", snapshots[i]);
1752 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1753 snapshots[i], name);
1754 status = cli_qpathinfo2(cli, snap_name, &b_time, &a_time,
1755 &m_time, &c_time, &size,
1756 NULL, NULL);
1757 if (!NT_STATUS_IS_OK(status)) {
1758 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1759 snap_name, nt_errstr(status));
1760 TALLOC_FREE(snap_name);
1761 continue;
1763 unix_timespec_to_nt_time(&tmp, b_time);
1764 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1765 unix_timespec_to_nt_time(&tmp, a_time);
1766 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1767 unix_timespec_to_nt_time(&tmp, m_time);
1768 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1769 unix_timespec_to_nt_time(&tmp, c_time);
1770 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1771 d_printf("size: %d\n", (int)size);
1774 TALLOC_FREE(snapshots);
1776 return 0;
1779 /****************************************************************************
1780 Show all info we can get
1781 ****************************************************************************/
1783 static int cmd_allinfo(void)
1785 TALLOC_CTX *ctx = talloc_tos();
1786 char *name;
1787 char *buf;
1789 name = talloc_strdup(ctx, client_get_cur_dir());
1790 if (!name) {
1791 return 1;
1794 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1795 d_printf("allinfo <file>\n");
1796 return 1;
1798 name = talloc_asprintf_append(name, "%s", buf);
1799 if (!name) {
1800 return 1;
1803 do_allinfo(name);
1805 return 0;
1808 /****************************************************************************
1809 Put a single file.
1810 ****************************************************************************/
1812 static int do_put(const char *rname, const char *lname, bool reput)
1814 TALLOC_CTX *ctx = talloc_tos();
1815 uint16_t fnum;
1816 XFILE *f;
1817 SMB_OFF_T start = 0;
1818 int rc = 0;
1819 struct timespec tp_start;
1820 struct cli_state *targetcli;
1821 char *targetname = NULL;
1822 struct push_state state;
1823 NTSTATUS status;
1825 if (!cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli, &targetname)) {
1826 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1827 return 1;
1830 clock_gettime_mono(&tp_start);
1832 if (reput) {
1833 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1834 if (NT_STATUS_IS_OK(status)) {
1835 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1836 targetcli, fnum, NULL,
1837 &start, NULL, NULL,
1838 NULL, NULL, NULL)) &&
1839 !NT_STATUS_IS_OK(status = cli_getattrE(
1840 targetcli, fnum, NULL,
1841 &start, NULL, NULL,
1842 NULL))) {
1843 d_printf("getattrib: %s\n", nt_errstr(status));
1844 return 1;
1847 } else {
1848 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1851 if (!NT_STATUS_IS_OK(status)) {
1852 d_printf("%s opening remote file %s\n", nt_errstr(status),
1853 rname);
1854 return 1;
1857 /* allow files to be piped into smbclient
1858 jdblair 24.jun.98
1860 Note that in this case this function will exit(0) rather
1861 than returning. */
1862 if (!strcmp(lname, "-")) {
1863 f = x_stdin;
1864 /* size of file is not known */
1865 } else {
1866 f = x_fopen(lname,O_RDONLY, 0);
1867 if (f && reput) {
1868 if (x_tseek(f, start, SEEK_SET) == -1) {
1869 d_printf("Error seeking local file\n");
1870 x_fclose(f);
1871 return 1;
1876 if (!f) {
1877 d_printf("Error opening local file %s\n",lname);
1878 return 1;
1881 DEBUG(1,("putting file %s as %s ",lname,
1882 rname));
1884 x_setvbuf(f, NULL, X_IOFBF, io_bufsize);
1886 state.f = f;
1887 state.nread = 0;
1889 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1890 &state);
1891 if (!NT_STATUS_IS_OK(status)) {
1892 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1893 rc = 1;
1896 status = cli_close(targetcli, fnum);
1897 if (!NT_STATUS_IS_OK(status)) {
1898 d_printf("%s closing remote file %s\n", nt_errstr(status),
1899 rname);
1900 if (f != x_stdin) {
1901 x_fclose(f);
1903 return 1;
1906 if (f != x_stdin) {
1907 x_fclose(f);
1911 struct timespec tp_end;
1912 int this_time;
1914 clock_gettime_mono(&tp_end);
1915 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1916 put_total_time_ms += this_time;
1917 put_total_size += state.nread;
1919 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1920 state.nread / (1.024*this_time + 1.0e-4),
1921 put_total_size / (1.024*put_total_time_ms)));
1924 if (f == x_stdin) {
1925 cli_shutdown(cli);
1926 exit(rc);
1929 return rc;
1932 /****************************************************************************
1933 Put a file.
1934 ****************************************************************************/
1936 static int cmd_put(void)
1938 TALLOC_CTX *ctx = talloc_tos();
1939 char *lname;
1940 char *rname;
1941 char *buf;
1943 rname = talloc_strdup(ctx, client_get_cur_dir());
1944 if (!rname) {
1945 return 1;
1948 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1949 d_printf("put <filename>\n");
1950 return 1;
1953 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1954 rname = talloc_asprintf_append(rname, "%s", buf);
1955 } else {
1956 rname = talloc_asprintf_append(rname, "%s", lname);
1958 if (!rname) {
1959 return 1;
1962 rname = clean_name(ctx, rname);
1963 if (!rname) {
1964 return 1;
1968 SMB_STRUCT_STAT st;
1969 /* allow '-' to represent stdin
1970 jdblair, 24.jun.98 */
1971 if (!file_exist_stat(lname, &st, false) &&
1972 (strcmp(lname,"-"))) {
1973 d_printf("%s does not exist\n",lname);
1974 return 1;
1978 return do_put(rname, lname, false);
1981 /*************************************
1982 File list structure.
1983 *************************************/
1985 static struct file_list {
1986 struct file_list *prev, *next;
1987 char *file_path;
1988 bool isdir;
1989 } *file_list;
1991 /****************************************************************************
1992 Free a file_list structure.
1993 ****************************************************************************/
1995 static void free_file_list (struct file_list *l_head)
1997 struct file_list *list, *next;
1999 for (list = l_head; list; list = next) {
2000 next = list->next;
2001 DLIST_REMOVE(l_head, list);
2002 SAFE_FREE(list->file_path);
2003 SAFE_FREE(list);
2007 /****************************************************************************
2008 Seek in a directory/file list until you get something that doesn't start with
2009 the specified name.
2010 ****************************************************************************/
2012 static bool seek_list(struct file_list *list, char *name)
2014 while (list) {
2015 trim_string(list->file_path,"./","\n");
2016 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2017 return true;
2019 list = list->next;
2022 return false;
2025 /****************************************************************************
2026 Set the file selection mask.
2027 ****************************************************************************/
2029 static int cmd_select(void)
2031 TALLOC_CTX *ctx = talloc_tos();
2032 char *new_fs = NULL;
2033 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2035 if (new_fs) {
2036 client_set_fileselection(new_fs);
2037 } else {
2038 client_set_fileselection("");
2040 return 0;
2043 /****************************************************************************
2044 Recursive file matching function act as find
2045 match must be always set to true when calling this function
2046 ****************************************************************************/
2048 static int file_find(struct file_list **list, const char *directory,
2049 const char *expression, bool match)
2051 SMB_STRUCT_DIR *dir;
2052 struct file_list *entry;
2053 struct stat statbuf;
2054 int ret;
2055 char *path;
2056 bool isdir;
2057 const char *dname;
2059 dir = sys_opendir(directory);
2060 if (!dir)
2061 return -1;
2063 while ((dname = readdirname(dir))) {
2064 if (!strcmp("..", dname))
2065 continue;
2066 if (!strcmp(".", dname))
2067 continue;
2069 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
2070 continue;
2073 isdir = false;
2074 if (!match || !gen_fnmatch(expression, dname)) {
2075 if (recurse) {
2076 ret = stat(path, &statbuf);
2077 if (ret == 0) {
2078 if (S_ISDIR(statbuf.st_mode)) {
2079 isdir = true;
2080 ret = file_find(list, path, expression, false);
2082 } else {
2083 d_printf("file_find: cannot stat file %s\n", path);
2086 if (ret == -1) {
2087 SAFE_FREE(path);
2088 sys_closedir(dir);
2089 return -1;
2092 entry = SMB_MALLOC_P(struct file_list);
2093 if (!entry) {
2094 d_printf("Out of memory in file_find\n");
2095 sys_closedir(dir);
2096 return -1;
2098 entry->file_path = path;
2099 entry->isdir = isdir;
2100 DLIST_ADD(*list, entry);
2101 } else {
2102 SAFE_FREE(path);
2106 sys_closedir(dir);
2107 return 0;
2110 /****************************************************************************
2111 mput some files.
2112 ****************************************************************************/
2114 static int cmd_mput(void)
2116 TALLOC_CTX *ctx = talloc_tos();
2117 char *p = NULL;
2119 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2120 int ret;
2121 struct file_list *temp_list;
2122 char *quest, *lname, *rname;
2124 file_list = NULL;
2126 ret = file_find(&file_list, ".", p, true);
2127 if (ret) {
2128 free_file_list(file_list);
2129 continue;
2132 quest = NULL;
2133 lname = NULL;
2134 rname = NULL;
2136 for (temp_list = file_list; temp_list;
2137 temp_list = temp_list->next) {
2139 SAFE_FREE(lname);
2140 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2141 continue;
2143 trim_string(lname, "./", "/");
2145 /* check if it's a directory */
2146 if (temp_list->isdir) {
2147 /* if (!recurse) continue; */
2149 SAFE_FREE(quest);
2150 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2151 break;
2153 if (prompt && !yesno(quest)) { /* No */
2154 /* Skip the directory */
2155 lname[strlen(lname)-1] = '/';
2156 if (!seek_list(temp_list, lname))
2157 break;
2158 } else { /* Yes */
2159 SAFE_FREE(rname);
2160 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2161 break;
2163 normalize_name(rname);
2164 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2165 !do_mkdir(rname)) {
2166 DEBUG (0, ("Unable to make dir, skipping..."));
2167 /* Skip the directory */
2168 lname[strlen(lname)-1] = '/';
2169 if (!seek_list(temp_list, lname)) {
2170 break;
2174 continue;
2175 } else {
2176 SAFE_FREE(quest);
2177 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2178 break;
2180 if (prompt && !yesno(quest)) {
2181 /* No */
2182 continue;
2185 /* Yes */
2186 SAFE_FREE(rname);
2187 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2188 break;
2192 normalize_name(rname);
2194 do_put(rname, lname, false);
2196 free_file_list(file_list);
2197 SAFE_FREE(quest);
2198 SAFE_FREE(lname);
2199 SAFE_FREE(rname);
2202 return 0;
2205 /****************************************************************************
2206 Cancel a print job.
2207 ****************************************************************************/
2209 static int do_cancel(int job)
2211 if (cli_printjob_del(cli, job)) {
2212 d_printf("Job %d cancelled\n",job);
2213 return 0;
2214 } else {
2215 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
2216 return 1;
2220 /****************************************************************************
2221 Cancel a print job.
2222 ****************************************************************************/
2224 static int cmd_cancel(void)
2226 TALLOC_CTX *ctx = talloc_tos();
2227 char *buf = NULL;
2228 int job;
2230 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2231 d_printf("cancel <jobid> ...\n");
2232 return 1;
2234 do {
2235 job = atoi(buf);
2236 do_cancel(job);
2237 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2239 return 0;
2242 /****************************************************************************
2243 Print a file.
2244 ****************************************************************************/
2246 static int cmd_print(void)
2248 TALLOC_CTX *ctx = talloc_tos();
2249 char *lname = NULL;
2250 char *rname = NULL;
2251 char *p = NULL;
2253 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2254 d_printf("print <filename>\n");
2255 return 1;
2258 rname = talloc_strdup(ctx, lname);
2259 if (!rname) {
2260 return 1;
2262 p = strrchr_m(rname,'/');
2263 if (p) {
2264 rname = talloc_asprintf(ctx,
2265 "%s-%d",
2266 p+1,
2267 (int)sys_getpid());
2269 if (strequal(lname,"-")) {
2270 rname = talloc_asprintf(ctx,
2271 "stdin-%d",
2272 (int)sys_getpid());
2274 if (!rname) {
2275 return 1;
2278 return do_put(rname, lname, false);
2281 /****************************************************************************
2282 Show a print queue entry.
2283 ****************************************************************************/
2285 static void queue_fn(struct print_job_info *p)
2287 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2290 /****************************************************************************
2291 Show a print queue.
2292 ****************************************************************************/
2294 static int cmd_queue(void)
2296 cli_print_queue(cli, queue_fn);
2297 return 0;
2300 /****************************************************************************
2301 Delete some files.
2302 ****************************************************************************/
2304 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2305 const char *dir)
2307 TALLOC_CTX *ctx = talloc_tos();
2308 char *mask = NULL;
2309 NTSTATUS status;
2311 mask = talloc_asprintf(ctx,
2312 "%s%c%s",
2313 dir,
2314 CLI_DIRSEP_CHAR,
2315 finfo->name);
2316 if (!mask) {
2317 return NT_STATUS_NO_MEMORY;
2320 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2321 TALLOC_FREE(mask);
2322 return NT_STATUS_OK;
2325 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2326 if (!NT_STATUS_IS_OK(status)) {
2327 d_printf("%s deleting remote file %s\n",
2328 nt_errstr(status), mask);
2330 TALLOC_FREE(mask);
2331 return status;
2334 /****************************************************************************
2335 Delete some files.
2336 ****************************************************************************/
2338 static int cmd_del(void)
2340 TALLOC_CTX *ctx = talloc_tos();
2341 char *mask = NULL;
2342 char *buf = NULL;
2343 NTSTATUS status = NT_STATUS_OK;
2344 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2346 if (recurse) {
2347 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2350 mask = talloc_strdup(ctx, client_get_cur_dir());
2351 if (!mask) {
2352 return 1;
2354 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2355 d_printf("del <filename>\n");
2356 return 1;
2358 mask = talloc_asprintf_append(mask, "%s", buf);
2359 if (!mask) {
2360 return 1;
2363 status = do_list(mask,attribute,do_del,false,false);
2364 if (!NT_STATUS_IS_OK(status)) {
2365 return 1;
2367 return 0;
2370 /****************************************************************************
2371 Wildcard delete some files.
2372 ****************************************************************************/
2374 static int cmd_wdel(void)
2376 TALLOC_CTX *ctx = talloc_tos();
2377 char *mask = NULL;
2378 char *buf = NULL;
2379 uint16 attribute;
2380 struct cli_state *targetcli;
2381 char *targetname = NULL;
2382 NTSTATUS status;
2384 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2385 d_printf("wdel 0x<attrib> <wcard>\n");
2386 return 1;
2389 attribute = (uint16)strtol(buf, (char **)NULL, 16);
2391 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2392 d_printf("wdel 0x<attrib> <wcard>\n");
2393 return 1;
2396 mask = talloc_asprintf(ctx, "%s%s",
2397 client_get_cur_dir(),
2398 buf);
2399 if (!mask) {
2400 return 1;
2403 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2404 d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
2405 return 1;
2408 status = cli_unlink(targetcli, targetname, attribute);
2409 if (!NT_STATUS_IS_OK(status)) {
2410 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2411 targetname);
2413 return 0;
2416 /****************************************************************************
2417 ****************************************************************************/
2419 static int cmd_open(void)
2421 TALLOC_CTX *ctx = talloc_tos();
2422 char *mask = NULL;
2423 char *buf = NULL;
2424 char *targetname = NULL;
2425 struct cli_state *targetcli;
2426 uint16_t fnum = (uint16_t)-1;
2428 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2429 d_printf("open <filename>\n");
2430 return 1;
2432 mask = talloc_asprintf(ctx,
2433 "%s%s",
2434 client_get_cur_dir(),
2435 buf);
2436 if (!mask) {
2437 return 1;
2440 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2441 d_printf("open %s: %s\n", mask, cli_errstr(cli));
2442 return 1;
2445 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetname, 0,
2446 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2447 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
2448 if (NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetname, 0,
2449 FILE_READ_DATA, 0,
2450 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
2451 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2452 } else {
2453 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2455 } else {
2456 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2458 return 0;
2461 static int cmd_posix_encrypt(void)
2463 TALLOC_CTX *ctx = talloc_tos();
2464 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2466 if (cli->use_kerberos) {
2467 status = cli_gss_smb_encryption_start(cli);
2468 } else {
2469 char *domain = NULL;
2470 char *user = NULL;
2471 char *password = NULL;
2473 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2474 d_printf("posix_encrypt domain user password\n");
2475 return 1;
2478 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2479 d_printf("posix_encrypt domain user password\n");
2480 return 1;
2483 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2484 d_printf("posix_encrypt domain user password\n");
2485 return 1;
2488 status = cli_raw_ntlm_smb_encryption_start(cli,
2489 user,
2490 password,
2491 domain);
2494 if (!NT_STATUS_IS_OK(status)) {
2495 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2496 } else {
2497 d_printf("encryption on\n");
2498 smb_encrypt = true;
2501 return 0;
2504 /****************************************************************************
2505 ****************************************************************************/
2507 static int cmd_posix_open(void)
2509 TALLOC_CTX *ctx = talloc_tos();
2510 char *mask = NULL;
2511 char *buf = NULL;
2512 char *targetname = NULL;
2513 struct cli_state *targetcli;
2514 mode_t mode;
2515 uint16_t fnum;
2517 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2518 d_printf("posix_open <filename> 0<mode>\n");
2519 return 1;
2521 mask = talloc_asprintf(ctx,
2522 "%s%s",
2523 client_get_cur_dir(),
2524 buf);
2525 if (!mask) {
2526 return 1;
2529 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2530 d_printf("posix_open <filename> 0<mode>\n");
2531 return 1;
2533 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2535 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2536 d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
2537 return 1;
2540 if (!NT_STATUS_IS_OK(cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode, &fnum))) {
2541 if (NT_STATUS_IS_OK(cli_posix_open(targetcli, targetname, O_CREAT|O_RDONLY, mode, &fnum))) {
2542 d_printf("posix_open file %s: for readonly fnum %d\n", targetname, fnum);
2543 } else {
2544 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2546 } else {
2547 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
2550 return 0;
2553 static int cmd_posix_mkdir(void)
2555 TALLOC_CTX *ctx = talloc_tos();
2556 char *mask = NULL;
2557 char *buf = NULL;
2558 char *targetname = NULL;
2559 struct cli_state *targetcli;
2560 mode_t mode;
2562 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2563 d_printf("posix_mkdir <filename> 0<mode>\n");
2564 return 1;
2566 mask = talloc_asprintf(ctx,
2567 "%s%s",
2568 client_get_cur_dir(),
2569 buf);
2570 if (!mask) {
2571 return 1;
2574 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2575 d_printf("posix_mkdir <filename> 0<mode>\n");
2576 return 1;
2578 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2580 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2581 d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
2582 return 1;
2585 if (!NT_STATUS_IS_OK(cli_posix_mkdir(targetcli, targetname, mode))) {
2586 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2587 } else {
2588 d_printf("posix_mkdir created directory %s\n", targetname);
2590 return 0;
2593 static int cmd_posix_unlink(void)
2595 TALLOC_CTX *ctx = talloc_tos();
2596 char *mask = NULL;
2597 char *buf = NULL;
2598 char *targetname = NULL;
2599 struct cli_state *targetcli;
2601 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2602 d_printf("posix_unlink <filename>\n");
2603 return 1;
2605 mask = talloc_asprintf(ctx,
2606 "%s%s",
2607 client_get_cur_dir(),
2608 buf);
2609 if (!mask) {
2610 return 1;
2613 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2614 d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
2615 return 1;
2618 if (!NT_STATUS_IS_OK(cli_posix_unlink(targetcli, targetname))) {
2619 d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
2620 } else {
2621 d_printf("posix_unlink deleted file %s\n", targetname);
2624 return 0;
2627 static int cmd_posix_rmdir(void)
2629 TALLOC_CTX *ctx = talloc_tos();
2630 char *mask = NULL;
2631 char *buf = NULL;
2632 char *targetname = NULL;
2633 struct cli_state *targetcli;
2635 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2636 d_printf("posix_rmdir <filename>\n");
2637 return 1;
2639 mask = talloc_asprintf(ctx,
2640 "%s%s",
2641 client_get_cur_dir(),
2642 buf);
2643 if (!mask) {
2644 return 1;
2647 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2648 d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
2649 return 1;
2652 if (!NT_STATUS_IS_OK(cli_posix_rmdir(targetcli, targetname))) {
2653 d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
2654 } else {
2655 d_printf("posix_rmdir deleted directory %s\n", targetname);
2658 return 0;
2661 static int cmd_close(void)
2663 TALLOC_CTX *ctx = talloc_tos();
2664 char *buf = NULL;
2665 int fnum;
2667 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2668 d_printf("close <fnum>\n");
2669 return 1;
2672 fnum = atoi(buf);
2673 /* We really should use the targetcli here.... */
2674 if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
2675 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
2676 return 1;
2678 return 0;
2681 static int cmd_posix(void)
2683 TALLOC_CTX *ctx = talloc_tos();
2684 uint16 major, minor;
2685 uint32 caplow, caphigh;
2686 char *caps;
2687 NTSTATUS status;
2689 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2690 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2691 return 1;
2694 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
2695 &caphigh);
2696 if (!NT_STATUS_IS_OK(status)) {
2697 d_printf("Can't get UNIX CIFS extensions version from "
2698 "server: %s\n", nt_errstr(status));
2699 return 1;
2702 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2704 caps = talloc_strdup(ctx, "");
2705 if (!caps) {
2706 return 1;
2708 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2709 caps = talloc_asprintf_append(caps, "locks ");
2710 if (!caps) {
2711 return 1;
2714 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2715 caps = talloc_asprintf_append(caps, "acls ");
2716 if (!caps) {
2717 return 1;
2720 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2721 caps = talloc_asprintf_append(caps, "eas ");
2722 if (!caps) {
2723 return 1;
2726 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2727 caps = talloc_asprintf_append(caps, "pathnames ");
2728 if (!caps) {
2729 return 1;
2732 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2733 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2734 if (!caps) {
2735 return 1;
2738 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2739 caps = talloc_asprintf_append(caps, "large_read ");
2740 if (!caps) {
2741 return 1;
2744 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2745 caps = talloc_asprintf_append(caps, "large_write ");
2746 if (!caps) {
2747 return 1;
2750 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2751 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2752 if (!caps) {
2753 return 1;
2756 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2757 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2758 if (!caps) {
2759 return 1;
2763 if (*caps && caps[strlen(caps)-1] == ' ') {
2764 caps[strlen(caps)-1] = '\0';
2767 d_printf("Server supports CIFS capabilities %s\n", caps);
2769 status = cli_set_unix_extensions_capabilities(cli, major, minor,
2770 caplow, caphigh);
2771 if (!NT_STATUS_IS_OK(status)) {
2772 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2773 nt_errstr(status));
2774 return 1;
2777 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2778 CLI_DIRSEP_CHAR = '/';
2779 *CLI_DIRSEP_STR = '/';
2780 client_set_cur_dir(CLI_DIRSEP_STR);
2783 return 0;
2786 static int cmd_lock(void)
2788 TALLOC_CTX *ctx = talloc_tos();
2789 char *buf = NULL;
2790 uint64_t start, len;
2791 enum brl_type lock_type;
2792 int fnum;
2794 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2795 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2796 return 1;
2798 fnum = atoi(buf);
2800 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2801 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2802 return 1;
2805 if (*buf == 'r' || *buf == 'R') {
2806 lock_type = READ_LOCK;
2807 } else if (*buf == 'w' || *buf == 'W') {
2808 lock_type = WRITE_LOCK;
2809 } else {
2810 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2811 return 1;
2814 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2815 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2816 return 1;
2819 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2821 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2822 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2823 return 1;
2826 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2828 if (!NT_STATUS_IS_OK(cli_posix_lock(cli, fnum, start, len, true, lock_type))) {
2829 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
2832 return 0;
2835 static int cmd_unlock(void)
2837 TALLOC_CTX *ctx = talloc_tos();
2838 char *buf = NULL;
2839 uint64_t start, len;
2840 int fnum;
2842 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2843 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2844 return 1;
2846 fnum = atoi(buf);
2848 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2849 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2850 return 1;
2853 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2855 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2856 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2857 return 1;
2860 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2862 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli, fnum, start, len))) {
2863 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
2866 return 0;
2870 /****************************************************************************
2871 Remove a directory.
2872 ****************************************************************************/
2874 static int cmd_rmdir(void)
2876 TALLOC_CTX *ctx = talloc_tos();
2877 char *mask = NULL;
2878 char *buf = NULL;
2879 char *targetname = NULL;
2880 struct cli_state *targetcli;
2882 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2883 d_printf("rmdir <dirname>\n");
2884 return 1;
2886 mask = talloc_asprintf(ctx,
2887 "%s%s",
2888 client_get_cur_dir(),
2889 buf);
2890 if (!mask) {
2891 return 1;
2894 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2895 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
2896 return 1;
2899 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetname))) {
2900 d_printf("%s removing remote directory file %s\n",
2901 cli_errstr(targetcli),mask);
2904 return 0;
2907 /****************************************************************************
2908 UNIX hardlink.
2909 ****************************************************************************/
2911 static int cmd_link(void)
2913 TALLOC_CTX *ctx = talloc_tos();
2914 char *oldname = NULL;
2915 char *newname = NULL;
2916 char *buf = NULL;
2917 char *buf2 = NULL;
2918 char *targetname = NULL;
2919 struct cli_state *targetcli;
2921 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2922 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2923 d_printf("link <oldname> <newname>\n");
2924 return 1;
2926 oldname = talloc_asprintf(ctx,
2927 "%s%s",
2928 client_get_cur_dir(),
2929 buf);
2930 if (!oldname) {
2931 return 1;
2933 newname = talloc_asprintf(ctx,
2934 "%s%s",
2935 client_get_cur_dir(),
2936 buf2);
2937 if (!newname) {
2938 return 1;
2941 if (!cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli, &targetname)) {
2942 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2943 return 1;
2946 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2947 d_printf("Server doesn't support UNIX CIFS calls.\n");
2948 return 1;
2951 if (!NT_STATUS_IS_OK(cli_posix_hardlink(targetcli, targetname, newname))) {
2952 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
2953 return 1;
2955 return 0;
2958 /****************************************************************************
2959 UNIX readlink.
2960 ****************************************************************************/
2962 static int cmd_readlink(void)
2964 TALLOC_CTX *ctx = talloc_tos();
2965 char *name= NULL;
2966 char *buf = NULL;
2967 char *targetname = NULL;
2968 char linkname[PATH_MAX+1];
2969 struct cli_state *targetcli;
2971 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2972 d_printf("readlink <name>\n");
2973 return 1;
2975 name = talloc_asprintf(ctx,
2976 "%s%s",
2977 client_get_cur_dir(),
2978 buf);
2979 if (!name) {
2980 return 1;
2983 if (!cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli, &targetname)) {
2984 d_printf("readlink %s: %s\n", name, cli_errstr(cli));
2985 return 1;
2988 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2989 d_printf("Server doesn't support UNIX CIFS calls.\n");
2990 return 1;
2993 if (!NT_STATUS_IS_OK(cli_posix_readlink(targetcli, name,
2994 linkname, PATH_MAX+1))) {
2995 d_printf("%s readlink on file %s\n",
2996 cli_errstr(targetcli), name);
2997 return 1;
3000 d_printf("%s -> %s\n", name, linkname);
3002 return 0;
3006 /****************************************************************************
3007 UNIX symlink.
3008 ****************************************************************************/
3010 static int cmd_symlink(void)
3012 TALLOC_CTX *ctx = talloc_tos();
3013 char *oldname = NULL;
3014 char *newname = NULL;
3015 char *buf = NULL;
3016 char *buf2 = NULL;
3017 struct cli_state *newcli;
3019 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3020 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3021 d_printf("symlink <oldname> <newname>\n");
3022 return 1;
3024 /* Oldname (link target) must be an untouched blob. */
3025 oldname = buf;
3027 newname = talloc_asprintf(ctx,
3028 "%s%s",
3029 client_get_cur_dir(),
3030 buf2);
3031 if (!newname) {
3032 return 1;
3035 /* New name must be present in share namespace. */
3036 if (!cli_resolve_path(ctx, "", auth_info, cli, newname, &newcli, &newname)) {
3037 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
3038 return 1;
3041 if (!SERVER_HAS_UNIX_CIFS(newcli)) {
3042 d_printf("Server doesn't support UNIX CIFS calls.\n");
3043 return 1;
3046 if (!NT_STATUS_IS_OK(cli_posix_symlink(newcli, oldname, newname))) {
3047 d_printf("%s symlinking files (%s -> %s)\n",
3048 cli_errstr(newcli), newname, newname);
3049 return 1;
3052 return 0;
3055 /****************************************************************************
3056 UNIX chmod.
3057 ****************************************************************************/
3059 static int cmd_chmod(void)
3061 TALLOC_CTX *ctx = talloc_tos();
3062 char *src = NULL;
3063 char *buf = NULL;
3064 char *buf2 = NULL;
3065 char *targetname = NULL;
3066 struct cli_state *targetcli;
3067 mode_t mode;
3069 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3070 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3071 d_printf("chmod mode file\n");
3072 return 1;
3074 src = talloc_asprintf(ctx,
3075 "%s%s",
3076 client_get_cur_dir(),
3077 buf2);
3078 if (!src) {
3079 return 1;
3082 mode = (mode_t)strtol(buf, NULL, 8);
3084 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3085 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
3086 return 1;
3089 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3090 d_printf("Server doesn't support UNIX CIFS calls.\n");
3091 return 1;
3094 if (!NT_STATUS_IS_OK(cli_posix_chmod(targetcli, targetname, mode))) {
3095 d_printf("%s chmod file %s 0%o\n",
3096 cli_errstr(targetcli), src, (unsigned int)mode);
3097 return 1;
3100 return 0;
3103 static const char *filetype_to_str(mode_t mode)
3105 if (S_ISREG(mode)) {
3106 return "regular file";
3107 } else if (S_ISDIR(mode)) {
3108 return "directory";
3109 } else
3110 #ifdef S_ISCHR
3111 if (S_ISCHR(mode)) {
3112 return "character device";
3113 } else
3114 #endif
3115 #ifdef S_ISBLK
3116 if (S_ISBLK(mode)) {
3117 return "block device";
3118 } else
3119 #endif
3120 #ifdef S_ISFIFO
3121 if (S_ISFIFO(mode)) {
3122 return "fifo";
3123 } else
3124 #endif
3125 #ifdef S_ISLNK
3126 if (S_ISLNK(mode)) {
3127 return "symbolic link";
3128 } else
3129 #endif
3130 #ifdef S_ISSOCK
3131 if (S_ISSOCK(mode)) {
3132 return "socket";
3133 } else
3134 #endif
3135 return "";
3138 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3140 if (m & bt) {
3141 return ret;
3142 } else {
3143 return '-';
3147 static char *unix_mode_to_str(char *s, mode_t m)
3149 char *p = s;
3150 const char *str = filetype_to_str(m);
3152 switch(str[0]) {
3153 case 'd':
3154 *p++ = 'd';
3155 break;
3156 case 'c':
3157 *p++ = 'c';
3158 break;
3159 case 'b':
3160 *p++ = 'b';
3161 break;
3162 case 'f':
3163 *p++ = 'p';
3164 break;
3165 case 's':
3166 *p++ = str[1] == 'y' ? 'l' : 's';
3167 break;
3168 case 'r':
3169 default:
3170 *p++ = '-';
3171 break;
3173 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3174 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3175 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3176 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3177 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3178 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3179 *p++ = rwx_to_str(m, S_IROTH, 'r');
3180 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3181 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3182 *p++ = '\0';
3183 return s;
3186 /****************************************************************************
3187 Utility function for UNIX getfacl.
3188 ****************************************************************************/
3190 static char *perms_to_string(fstring permstr, unsigned char perms)
3192 fstrcpy(permstr, "---");
3193 if (perms & SMB_POSIX_ACL_READ) {
3194 permstr[0] = 'r';
3196 if (perms & SMB_POSIX_ACL_WRITE) {
3197 permstr[1] = 'w';
3199 if (perms & SMB_POSIX_ACL_EXECUTE) {
3200 permstr[2] = 'x';
3202 return permstr;
3205 /****************************************************************************
3206 UNIX getfacl.
3207 ****************************************************************************/
3209 static int cmd_getfacl(void)
3211 TALLOC_CTX *ctx = talloc_tos();
3212 char *src = NULL;
3213 char *name = NULL;
3214 char *targetname = NULL;
3215 struct cli_state *targetcli;
3216 uint16 major, minor;
3217 uint32 caplow, caphigh;
3218 char *retbuf = NULL;
3219 size_t rb_size = 0;
3220 SMB_STRUCT_STAT sbuf;
3221 uint16 num_file_acls = 0;
3222 uint16 num_dir_acls = 0;
3223 uint16 i;
3224 NTSTATUS status;
3226 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3227 d_printf("getfacl filename\n");
3228 return 1;
3230 src = talloc_asprintf(ctx,
3231 "%s%s",
3232 client_get_cur_dir(),
3233 name);
3234 if (!src) {
3235 return 1;
3238 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3239 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3240 return 1;
3243 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3244 d_printf("Server doesn't support UNIX CIFS calls.\n");
3245 return 1;
3248 status = cli_unix_extensions_version(targetcli, &major, &minor,
3249 &caplow, &caphigh);
3250 if (!NT_STATUS_IS_OK(status)) {
3251 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3252 nt_errstr(status));
3253 return 1;
3256 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3257 d_printf("This server supports UNIX extensions "
3258 "but doesn't support POSIX ACLs.\n");
3259 return 1;
3262 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3263 d_printf("%s getfacl doing a stat on file %s\n",
3264 cli_errstr(targetcli), src);
3265 return 1;
3268 if (!NT_STATUS_IS_OK(cli_posix_getfacl(targetcli, targetname, ctx, &rb_size, &retbuf))) {
3269 d_printf("%s getfacl file %s\n",
3270 cli_errstr(targetcli), src);
3271 return 1;
3274 /* ToDo : Print out the ACL values. */
3275 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3276 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3277 src, (unsigned int)CVAL(retbuf,0) );
3278 return 1;
3281 num_file_acls = SVAL(retbuf,2);
3282 num_dir_acls = SVAL(retbuf,4);
3283 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3284 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3285 src,
3286 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3287 (unsigned int)rb_size);
3288 return 1;
3291 d_printf("# file: %s\n", src);
3292 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3294 if (num_file_acls == 0 && num_dir_acls == 0) {
3295 d_printf("No acls found.\n");
3298 for (i = 0; i < num_file_acls; i++) {
3299 uint32 uorg;
3300 fstring permstring;
3301 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3302 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3304 switch(tagtype) {
3305 case SMB_POSIX_ACL_USER_OBJ:
3306 d_printf("user::");
3307 break;
3308 case SMB_POSIX_ACL_USER:
3309 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3310 d_printf("user:%u:", uorg);
3311 break;
3312 case SMB_POSIX_ACL_GROUP_OBJ:
3313 d_printf("group::");
3314 break;
3315 case SMB_POSIX_ACL_GROUP:
3316 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3317 d_printf("group:%u:", uorg);
3318 break;
3319 case SMB_POSIX_ACL_MASK:
3320 d_printf("mask::");
3321 break;
3322 case SMB_POSIX_ACL_OTHER:
3323 d_printf("other::");
3324 break;
3325 default:
3326 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3327 src, (unsigned int)tagtype );
3328 SAFE_FREE(retbuf);
3329 return 1;
3332 d_printf("%s\n", perms_to_string(permstring, perms));
3335 for (i = 0; i < num_dir_acls; i++) {
3336 uint32 uorg;
3337 fstring permstring;
3338 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3339 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3341 switch(tagtype) {
3342 case SMB_POSIX_ACL_USER_OBJ:
3343 d_printf("default:user::");
3344 break;
3345 case SMB_POSIX_ACL_USER:
3346 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3347 d_printf("default:user:%u:", uorg);
3348 break;
3349 case SMB_POSIX_ACL_GROUP_OBJ:
3350 d_printf("default:group::");
3351 break;
3352 case SMB_POSIX_ACL_GROUP:
3353 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3354 d_printf("default:group:%u:", uorg);
3355 break;
3356 case SMB_POSIX_ACL_MASK:
3357 d_printf("default:mask::");
3358 break;
3359 case SMB_POSIX_ACL_OTHER:
3360 d_printf("default:other::");
3361 break;
3362 default:
3363 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3364 src, (unsigned int)tagtype );
3365 SAFE_FREE(retbuf);
3366 return 1;
3369 d_printf("%s\n", perms_to_string(permstring, perms));
3372 return 0;
3375 static void printf_cb(const char *buf, void *private_data)
3377 printf("%s", buf);
3380 /****************************************************************************
3381 Get the EA list of a file
3382 ****************************************************************************/
3384 static int cmd_geteas(void)
3386 TALLOC_CTX *ctx = talloc_tos();
3387 char *src = NULL;
3388 char *name = NULL;
3389 char *targetname = NULL;
3390 struct cli_state *targetcli;
3391 NTSTATUS status;
3392 size_t i, num_eas;
3393 struct ea_struct *eas;
3395 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3396 d_printf("geteas filename\n");
3397 return 1;
3399 src = talloc_asprintf(ctx,
3400 "%s%s",
3401 client_get_cur_dir(),
3402 name);
3403 if (!src) {
3404 return 1;
3407 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3408 &targetname)) {
3409 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3410 return 1;
3413 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3414 &num_eas, &eas);
3415 if (!NT_STATUS_IS_OK(status)) {
3416 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3417 return 1;
3420 for (i=0; i<num_eas; i++) {
3421 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3422 dump_data_cb(eas[i].value.data, eas[i].value.length, false,
3423 printf_cb, NULL);
3424 d_printf("\n");
3427 TALLOC_FREE(eas);
3429 return 0;
3432 /****************************************************************************
3433 Set an EA of a file
3434 ****************************************************************************/
3436 static int cmd_setea(void)
3438 TALLOC_CTX *ctx = talloc_tos();
3439 char *src = NULL;
3440 char *name = NULL;
3441 char *eaname = NULL;
3442 char *eavalue = NULL;
3443 char *targetname = NULL;
3444 struct cli_state *targetcli;
3445 NTSTATUS status;
3447 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
3448 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
3449 d_printf("setea filename eaname value\n");
3450 return 1;
3452 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
3453 eavalue = talloc_strdup(ctx, "");
3455 src = talloc_asprintf(ctx,
3456 "%s%s",
3457 client_get_cur_dir(),
3458 name);
3459 if (!src) {
3460 return 1;
3463 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3464 &targetname)) {
3465 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3466 return 1;
3469 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
3470 strlen(eavalue));
3471 if (!NT_STATUS_IS_OK(status)) {
3472 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
3473 return 1;
3476 return 0;
3479 /****************************************************************************
3480 UNIX stat.
3481 ****************************************************************************/
3483 static int cmd_stat(void)
3485 TALLOC_CTX *ctx = talloc_tos();
3486 char *src = NULL;
3487 char *name = NULL;
3488 char *targetname = NULL;
3489 struct cli_state *targetcli;
3490 fstring mode_str;
3491 SMB_STRUCT_STAT sbuf;
3492 struct tm *lt;
3493 time_t tmp_time;
3495 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3496 d_printf("stat file\n");
3497 return 1;
3499 src = talloc_asprintf(ctx,
3500 "%s%s",
3501 client_get_cur_dir(),
3502 name);
3503 if (!src) {
3504 return 1;
3507 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3508 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3509 return 1;
3512 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3513 d_printf("Server doesn't support UNIX CIFS calls.\n");
3514 return 1;
3517 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3518 d_printf("%s stat file %s\n",
3519 cli_errstr(targetcli), src);
3520 return 1;
3523 /* Print out the stat values. */
3524 d_printf("File: %s\n", src);
3525 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3526 (double)sbuf.st_ex_size,
3527 (unsigned int)sbuf.st_ex_blocks,
3528 filetype_to_str(sbuf.st_ex_mode));
3530 #if defined(S_ISCHR) && defined(S_ISBLK)
3531 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
3532 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3533 (double)sbuf.st_ex_ino,
3534 (unsigned int)sbuf.st_ex_nlink,
3535 unix_dev_major(sbuf.st_ex_rdev),
3536 unix_dev_minor(sbuf.st_ex_rdev));
3537 } else
3538 #endif
3539 d_printf("Inode: %.0f\tLinks: %u\n",
3540 (double)sbuf.st_ex_ino,
3541 (unsigned int)sbuf.st_ex_nlink);
3543 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3544 ((int)sbuf.st_ex_mode & 0777),
3545 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
3546 (unsigned int)sbuf.st_ex_uid,
3547 (unsigned int)sbuf.st_ex_gid);
3549 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
3550 lt = localtime(&tmp_time);
3551 if (lt) {
3552 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3553 } else {
3554 fstrcpy(mode_str, "unknown");
3556 d_printf("Access: %s\n", mode_str);
3558 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3559 lt = localtime(&tmp_time);
3560 if (lt) {
3561 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3562 } else {
3563 fstrcpy(mode_str, "unknown");
3565 d_printf("Modify: %s\n", mode_str);
3567 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
3568 lt = localtime(&tmp_time);
3569 if (lt) {
3570 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3571 } else {
3572 fstrcpy(mode_str, "unknown");
3574 d_printf("Change: %s\n", mode_str);
3576 return 0;
3580 /****************************************************************************
3581 UNIX chown.
3582 ****************************************************************************/
3584 static int cmd_chown(void)
3586 TALLOC_CTX *ctx = talloc_tos();
3587 char *src = NULL;
3588 uid_t uid;
3589 gid_t gid;
3590 char *buf, *buf2, *buf3;
3591 struct cli_state *targetcli;
3592 char *targetname = NULL;
3594 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3595 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3596 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3597 d_printf("chown uid gid file\n");
3598 return 1;
3601 uid = (uid_t)atoi(buf);
3602 gid = (gid_t)atoi(buf2);
3604 src = talloc_asprintf(ctx,
3605 "%s%s",
3606 client_get_cur_dir(),
3607 buf3);
3608 if (!src) {
3609 return 1;
3611 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname) ) {
3612 d_printf("chown %s: %s\n", src, cli_errstr(cli));
3613 return 1;
3616 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3617 d_printf("Server doesn't support UNIX CIFS calls.\n");
3618 return 1;
3621 if (!NT_STATUS_IS_OK(cli_posix_chown(targetcli, targetname, uid, gid))) {
3622 d_printf("%s chown file %s uid=%d, gid=%d\n",
3623 cli_errstr(targetcli), src, (int)uid, (int)gid);
3624 return 1;
3627 return 0;
3630 /****************************************************************************
3631 Rename some file.
3632 ****************************************************************************/
3634 static int cmd_rename(void)
3636 TALLOC_CTX *ctx = talloc_tos();
3637 char *src, *dest;
3638 char *buf, *buf2;
3639 struct cli_state *targetcli;
3640 char *targetsrc;
3641 char *targetdest;
3643 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3644 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3645 d_printf("rename <src> <dest>\n");
3646 return 1;
3649 src = talloc_asprintf(ctx,
3650 "%s%s",
3651 client_get_cur_dir(),
3652 buf);
3653 if (!src) {
3654 return 1;
3657 dest = talloc_asprintf(ctx,
3658 "%s%s",
3659 client_get_cur_dir(),
3660 buf2);
3661 if (!dest) {
3662 return 1;
3665 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetsrc)) {
3666 d_printf("rename %s: %s\n", src, cli_errstr(cli));
3667 return 1;
3670 if (!cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli, &targetdest)) {
3671 d_printf("rename %s: %s\n", dest, cli_errstr(cli));
3672 return 1;
3675 if (!NT_STATUS_IS_OK(cli_rename(targetcli, targetsrc, targetdest))) {
3676 d_printf("%s renaming files %s -> %s \n",
3677 cli_errstr(targetcli),
3678 targetsrc,
3679 targetdest);
3680 return 1;
3683 return 0;
3686 /****************************************************************************
3687 Print the volume name.
3688 ****************************************************************************/
3690 static int cmd_volume(void)
3692 fstring volname;
3693 uint32 serial_num;
3694 time_t create_date;
3695 NTSTATUS status;
3697 status = cli_get_fs_volume_info(cli, volname, &serial_num,
3698 &create_date);
3699 if (!NT_STATUS_IS_OK(status)) {
3700 d_printf("Error %s getting volume info\n", nt_errstr(status));
3701 return 1;
3704 d_printf("Volume: |%s| serial number 0x%x\n",
3705 volname, (unsigned int)serial_num);
3706 return 0;
3709 /****************************************************************************
3710 Hard link files using the NT call.
3711 ****************************************************************************/
3713 static int cmd_hardlink(void)
3715 TALLOC_CTX *ctx = talloc_tos();
3716 char *src, *dest;
3717 char *buf, *buf2;
3718 struct cli_state *targetcli;
3719 char *targetname;
3721 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3722 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3723 d_printf("hardlink <src> <dest>\n");
3724 return 1;
3727 src = talloc_asprintf(ctx,
3728 "%s%s",
3729 client_get_cur_dir(),
3730 buf);
3731 if (!src) {
3732 return 1;
3735 dest = talloc_asprintf(ctx,
3736 "%s%s",
3737 client_get_cur_dir(),
3738 buf2);
3739 if (!dest) {
3740 return 1;
3743 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3744 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
3745 return 1;
3748 if (!NT_STATUS_IS_OK(cli_nt_hardlink(targetcli, targetname, dest))) {
3749 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
3750 return 1;
3753 return 0;
3756 /****************************************************************************
3757 Toggle the prompt flag.
3758 ****************************************************************************/
3760 static int cmd_prompt(void)
3762 prompt = !prompt;
3763 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
3764 return 1;
3767 /****************************************************************************
3768 Set the newer than time.
3769 ****************************************************************************/
3771 static int cmd_newer(void)
3773 TALLOC_CTX *ctx = talloc_tos();
3774 char *buf;
3775 bool ok;
3776 SMB_STRUCT_STAT sbuf;
3778 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
3779 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
3780 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3781 DEBUG(1,("Getting files newer than %s",
3782 time_to_asc(newer_than)));
3783 } else {
3784 newer_than = 0;
3787 if (ok && newer_than == 0) {
3788 d_printf("Error setting newer-than time\n");
3789 return 1;
3792 return 0;
3795 /****************************************************************************
3796 Set the archive level.
3797 ****************************************************************************/
3799 static int cmd_archive(void)
3801 TALLOC_CTX *ctx = talloc_tos();
3802 char *buf;
3804 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3805 archive_level = atoi(buf);
3806 } else {
3807 d_printf("Archive level is %d\n",archive_level);
3810 return 0;
3813 /****************************************************************************
3814 Toggle the lowercaseflag.
3815 ****************************************************************************/
3817 static int cmd_lowercase(void)
3819 lowercase = !lowercase;
3820 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
3821 return 0;
3824 /****************************************************************************
3825 Toggle the case sensitive flag.
3826 ****************************************************************************/
3828 static int cmd_setcase(void)
3830 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
3832 cli_set_case_sensitive(cli, !orig_case_sensitive);
3833 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
3834 "on":"off"));
3835 return 0;
3838 /****************************************************************************
3839 Toggle the showacls flag.
3840 ****************************************************************************/
3842 static int cmd_showacls(void)
3844 showacls = !showacls;
3845 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
3846 return 0;
3850 /****************************************************************************
3851 Toggle the recurse flag.
3852 ****************************************************************************/
3854 static int cmd_recurse(void)
3856 recurse = !recurse;
3857 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
3858 return 0;
3861 /****************************************************************************
3862 Toggle the translate flag.
3863 ****************************************************************************/
3865 static int cmd_translate(void)
3867 translation = !translation;
3868 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
3869 translation?"on":"off"));
3870 return 0;
3873 /****************************************************************************
3874 Do the lcd command.
3875 ****************************************************************************/
3877 static int cmd_lcd(void)
3879 TALLOC_CTX *ctx = talloc_tos();
3880 char *buf;
3881 char *d;
3883 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3884 if (chdir(buf) == -1) {
3885 d_printf("chdir to %s failed (%s)\n",
3886 buf, strerror(errno));
3889 d = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
3890 if (!d) {
3891 return 1;
3893 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
3894 return 0;
3897 /****************************************************************************
3898 Get a file restarting at end of local file.
3899 ****************************************************************************/
3901 static int cmd_reget(void)
3903 TALLOC_CTX *ctx = talloc_tos();
3904 char *local_name = NULL;
3905 char *remote_name = NULL;
3906 char *fname = NULL;
3907 char *p = NULL;
3909 remote_name = talloc_strdup(ctx, client_get_cur_dir());
3910 if (!remote_name) {
3911 return 1;
3914 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
3915 d_printf("reget <filename>\n");
3916 return 1;
3918 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
3919 if (!remote_name) {
3920 return 1;
3922 remote_name = clean_name(ctx,remote_name);
3923 if (!remote_name) {
3924 return 1;
3927 local_name = fname;
3928 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
3929 if (p) {
3930 local_name = p;
3933 return do_get(remote_name, local_name, true);
3936 /****************************************************************************
3937 Put a file restarting at end of local file.
3938 ****************************************************************************/
3940 static int cmd_reput(void)
3942 TALLOC_CTX *ctx = talloc_tos();
3943 char *local_name = NULL;
3944 char *remote_name = NULL;
3945 char *buf;
3946 SMB_STRUCT_STAT st;
3948 remote_name = talloc_strdup(ctx, client_get_cur_dir());
3949 if (!remote_name) {
3950 return 1;
3953 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
3954 d_printf("reput <filename>\n");
3955 return 1;
3958 if (!file_exist_stat(local_name, &st, false)) {
3959 d_printf("%s does not exist\n", local_name);
3960 return 1;
3963 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3964 remote_name = talloc_asprintf_append(remote_name,
3965 "%s", buf);
3966 } else {
3967 remote_name = talloc_asprintf_append(remote_name,
3968 "%s", local_name);
3970 if (!remote_name) {
3971 return 1;
3974 remote_name = clean_name(ctx, remote_name);
3975 if (!remote_name) {
3976 return 1;
3979 return do_put(remote_name, local_name, true);
3982 /****************************************************************************
3983 List a share name.
3984 ****************************************************************************/
3986 static void browse_fn(const char *name, uint32 m,
3987 const char *comment, void *state)
3989 const char *typestr = "";
3991 switch (m & 7) {
3992 case STYPE_DISKTREE:
3993 typestr = "Disk";
3994 break;
3995 case STYPE_PRINTQ:
3996 typestr = "Printer";
3997 break;
3998 case STYPE_DEVICE:
3999 typestr = "Device";
4000 break;
4001 case STYPE_IPC:
4002 typestr = "IPC";
4003 break;
4005 /* FIXME: If the remote machine returns non-ascii characters
4006 in any of these fields, they can corrupt the output. We
4007 should remove them. */
4008 if (!grepable) {
4009 d_printf("\t%-15s %-10.10s%s\n",
4010 name,typestr,comment);
4011 } else {
4012 d_printf ("%s|%s|%s\n",typestr,name,comment);
4016 static bool browse_host_rpc(bool sort)
4018 NTSTATUS status;
4019 struct rpc_pipe_client *pipe_hnd = NULL;
4020 TALLOC_CTX *frame = talloc_stackframe();
4021 WERROR werr;
4022 struct srvsvc_NetShareInfoCtr info_ctr;
4023 struct srvsvc_NetShareCtr1 ctr1;
4024 uint32_t resume_handle = 0;
4025 uint32_t total_entries = 0;
4026 int i;
4027 struct dcerpc_binding_handle *b;
4029 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
4030 &pipe_hnd);
4032 if (!NT_STATUS_IS_OK(status)) {
4033 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4034 nt_errstr(status)));
4035 TALLOC_FREE(frame);
4036 return false;
4039 b = pipe_hnd->binding_handle;
4041 ZERO_STRUCT(info_ctr);
4042 ZERO_STRUCT(ctr1);
4044 info_ctr.level = 1;
4045 info_ctr.ctr.ctr1 = &ctr1;
4047 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4048 pipe_hnd->desthost,
4049 &info_ctr,
4050 0xffffffff,
4051 &total_entries,
4052 &resume_handle,
4053 &werr);
4055 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4056 TALLOC_FREE(pipe_hnd);
4057 TALLOC_FREE(frame);
4058 return false;
4061 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4062 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4063 browse_fn(info.name, info.type, info.comment, NULL);
4066 TALLOC_FREE(pipe_hnd);
4067 TALLOC_FREE(frame);
4068 return true;
4071 /****************************************************************************
4072 Try and browse available connections on a host.
4073 ****************************************************************************/
4075 static bool browse_host(bool sort)
4077 int ret;
4078 if (!grepable) {
4079 d_printf("\n\tSharename Type Comment\n");
4080 d_printf("\t--------- ---- -------\n");
4083 if (browse_host_rpc(sort)) {
4084 return true;
4087 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
4088 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
4090 return (ret != -1);
4093 /****************************************************************************
4094 List a server name.
4095 ****************************************************************************/
4097 static void server_fn(const char *name, uint32 m,
4098 const char *comment, void *state)
4101 if (!grepable){
4102 d_printf("\t%-16s %s\n", name, comment);
4103 } else {
4104 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4108 /****************************************************************************
4109 Try and browse available connections on a host.
4110 ****************************************************************************/
4112 static bool list_servers(const char *wk_grp)
4114 fstring state;
4116 if (!cli->server_domain)
4117 return false;
4119 if (!grepable) {
4120 d_printf("\n\tServer Comment\n");
4121 d_printf("\t--------- -------\n");
4123 fstrcpy( state, "Server" );
4124 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4125 state);
4127 if (!grepable) {
4128 d_printf("\n\tWorkgroup Master\n");
4129 d_printf("\t--------- -------\n");
4132 fstrcpy( state, "Workgroup" );
4133 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4134 server_fn, state);
4135 return true;
4138 /****************************************************************************
4139 Print or set current VUID
4140 ****************************************************************************/
4142 static int cmd_vuid(void)
4144 TALLOC_CTX *ctx = talloc_tos();
4145 char *buf;
4147 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4148 d_printf("Current VUID is %d\n", cli->vuid);
4149 return 0;
4152 cli->vuid = atoi(buf);
4153 return 0;
4156 /****************************************************************************
4157 Setup a new VUID, by issuing a session setup
4158 ****************************************************************************/
4160 static int cmd_logon(void)
4162 TALLOC_CTX *ctx = talloc_tos();
4163 char *l_username, *l_password;
4164 NTSTATUS nt_status;
4166 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
4167 d_printf("logon <username> [<password>]\n");
4168 return 0;
4171 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
4172 char *pass = getpass("Password: ");
4173 if (pass) {
4174 l_password = talloc_strdup(ctx,pass);
4177 if (!l_password) {
4178 return 1;
4181 nt_status = cli_session_setup(cli, l_username,
4182 l_password, strlen(l_password),
4183 l_password, strlen(l_password),
4184 lp_workgroup());
4185 if (!NT_STATUS_IS_OK(nt_status)) {
4186 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
4187 return -1;
4190 d_printf("Current VUID is %d\n", cli->vuid);
4191 return 0;
4195 /****************************************************************************
4196 list active connections
4197 ****************************************************************************/
4199 static int cmd_list_connect(void)
4201 cli_cm_display(cli);
4202 return 0;
4205 /****************************************************************************
4206 display the current active client connection
4207 ****************************************************************************/
4209 static int cmd_show_connect( void )
4211 TALLOC_CTX *ctx = talloc_tos();
4212 struct cli_state *targetcli;
4213 char *targetpath;
4215 if (!cli_resolve_path(ctx, "", auth_info, cli, client_get_cur_dir(),
4216 &targetcli, &targetpath ) ) {
4217 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
4218 return 1;
4221 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
4222 return 0;
4225 /****************************************************************************
4226 iosize command
4227 ***************************************************************************/
4229 int cmd_iosize(void)
4231 TALLOC_CTX *ctx = talloc_tos();
4232 char *buf;
4233 int iosize;
4235 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4236 if (!smb_encrypt) {
4237 d_printf("iosize <n> or iosize 0x<n>. "
4238 "Minimum is 16384 (0x4000), "
4239 "max is 16776960 (0xFFFF00)\n");
4240 } else {
4241 d_printf("iosize <n> or iosize 0x<n>. "
4242 "(Encrypted connection) ,"
4243 "Minimum is 16384 (0x4000), "
4244 "max is 130048 (0x1FC00)\n");
4246 return 1;
4249 iosize = strtol(buf,NULL,0);
4250 if (smb_encrypt && (iosize < 0x4000 || iosize > 0xFC00)) {
4251 d_printf("iosize out of range for encrypted "
4252 "connection (min = 16384 (0x4000), "
4253 "max = 130048 (0x1FC00)");
4254 return 1;
4255 } else if (!smb_encrypt && (iosize < 0x4000 || iosize > 0xFFFF00)) {
4256 d_printf("iosize out of range (min = 16384 (0x4000), "
4257 "max = 16776960 (0xFFFF00)");
4258 return 1;
4261 io_bufsize = iosize;
4262 d_printf("iosize is now %d\n", io_bufsize);
4263 return 0;
4266 /****************************************************************************
4267 history
4268 ****************************************************************************/
4269 static int cmd_history(void)
4271 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4272 HIST_ENTRY **hlist;
4273 int i;
4275 hlist = history_list();
4277 for (i = 0; hlist && hlist[i]; i++) {
4278 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
4280 #else
4281 DEBUG(0,("no history without readline support\n"));
4282 #endif
4284 return 0;
4287 /* Some constants for completing filename arguments */
4289 #define COMPL_NONE 0 /* No completions */
4290 #define COMPL_REMOTE 1 /* Complete remote filename */
4291 #define COMPL_LOCAL 2 /* Complete local filename */
4293 /* This defines the commands supported by this client.
4294 * NOTE: The "!" must be the last one in the list because it's fn pointer
4295 * field is NULL, and NULL in that field is used in process_tok()
4296 * (below) to indicate the end of the list. crh
4298 static struct {
4299 const char *name;
4300 int (*fn)(void);
4301 const char *description;
4302 char compl_args[2]; /* Completion argument info */
4303 } commands[] = {
4304 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4305 {"allinfo",cmd_allinfo,"<file> show all available info",
4306 {COMPL_NONE,COMPL_NONE}},
4307 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
4308 {"archive",cmd_archive,"<level>\n0=ignore archive bit\n1=only get archive files\n2=only get archive files and reset archive bit\n3=get all files and reset archive bit",{COMPL_NONE,COMPL_NONE}},
4309 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
4310 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
4311 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
4312 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
4313 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
4314 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
4315 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
4316 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4317 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4318 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4319 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
4320 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4321 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
4322 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
4323 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
4324 {COMPL_REMOTE, COMPL_LOCAL}},
4325 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4326 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4327 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
4328 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
4329 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
4330 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4331 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4332 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
4333 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4334 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4335 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
4336 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4337 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
4338 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4339 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
4340 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
4341 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
4342 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
4343 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
4344 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
4345 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4346 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4347 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4348 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4349 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
4350 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
4351 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
4352 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
4353 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4354 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
4355 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4356 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4357 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4358 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
4359 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
4360 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
4361 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
4362 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4363 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4364 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
4365 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
4366 {COMPL_REMOTE, COMPL_LOCAL}},
4367 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
4368 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
4369 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4370 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
4371 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
4372 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
4373 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4374 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
4375 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
4376 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4377 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
4378 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
4379 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
4380 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
4382 /* Yes, this must be here, see crh's comment above. */
4383 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
4384 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
4387 /*******************************************************************
4388 Lookup a command string in the list of commands, including
4389 abbreviations.
4390 ******************************************************************/
4392 static int process_tok(char *tok)
4394 int i = 0, matches = 0;
4395 int cmd=0;
4396 int tok_len = strlen(tok);
4398 while (commands[i].fn != NULL) {
4399 if (strequal(commands[i].name,tok)) {
4400 matches = 1;
4401 cmd = i;
4402 break;
4403 } else if (strnequal(commands[i].name, tok, tok_len)) {
4404 matches++;
4405 cmd = i;
4407 i++;
4410 if (matches == 0)
4411 return(-1);
4412 else if (matches == 1)
4413 return(cmd);
4414 else
4415 return(-2);
4418 /****************************************************************************
4419 Help.
4420 ****************************************************************************/
4422 static int cmd_help(void)
4424 TALLOC_CTX *ctx = talloc_tos();
4425 int i=0,j;
4426 char *buf;
4428 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4429 if ((i = process_tok(buf)) >= 0)
4430 d_printf("HELP %s:\n\t%s\n\n",
4431 commands[i].name,commands[i].description);
4432 } else {
4433 while (commands[i].description) {
4434 for (j=0; commands[i].description && (j<5); j++) {
4435 d_printf("%-15s",commands[i].name);
4436 i++;
4438 d_printf("\n");
4441 return 0;
4444 /****************************************************************************
4445 Process a -c command string.
4446 ****************************************************************************/
4448 static int process_command_string(const char *cmd_in)
4450 TALLOC_CTX *ctx = talloc_tos();
4451 char *cmd = talloc_strdup(ctx, cmd_in);
4452 int rc = 0;
4454 if (!cmd) {
4455 return 1;
4457 /* establish the connection if not already */
4459 if (!cli) {
4460 cli = cli_cm_open(talloc_tos(), NULL,
4461 have_ip ? dest_ss_str : desthost,
4462 service, auth_info,
4463 true, smb_encrypt,
4464 max_protocol, port, name_type);
4465 if (!cli) {
4466 return 1;
4470 while (cmd[0] != '\0') {
4471 char *line;
4472 char *p;
4473 char *tok;
4474 int i;
4476 if ((p = strchr_m(cmd, ';')) == 0) {
4477 line = cmd;
4478 cmd += strlen(cmd);
4479 } else {
4480 *p = '\0';
4481 line = cmd;
4482 cmd = p + 1;
4485 /* and get the first part of the command */
4486 cmd_ptr = line;
4487 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
4488 continue;
4491 if ((i = process_tok(tok)) >= 0) {
4492 rc = commands[i].fn();
4493 } else if (i == -2) {
4494 d_printf("%s: command abbreviation ambiguous\n",tok);
4495 } else {
4496 d_printf("%s: command not found\n",tok);
4500 return rc;
4503 #define MAX_COMPLETIONS 100
4505 struct completion_remote {
4506 char *dirmask;
4507 char **matches;
4508 int count, samelen;
4509 const char *text;
4510 int len;
4513 static NTSTATUS completion_remote_filter(const char *mnt,
4514 struct file_info *f,
4515 const char *mask,
4516 void *state)
4518 struct completion_remote *info = (struct completion_remote *)state;
4520 if (info->count >= MAX_COMPLETIONS - 1) {
4521 return NT_STATUS_OK;
4523 if (strncmp(info->text, f->name, info->len) != 0) {
4524 return NT_STATUS_OK;
4526 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
4527 return NT_STATUS_OK;
4530 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
4531 info->matches[info->count] = SMB_STRDUP(f->name);
4532 else {
4533 TALLOC_CTX *ctx = talloc_stackframe();
4534 char *tmp;
4536 tmp = talloc_strdup(ctx,info->dirmask);
4537 if (!tmp) {
4538 TALLOC_FREE(ctx);
4539 return NT_STATUS_NO_MEMORY;
4541 tmp = talloc_asprintf_append(tmp, "%s", f->name);
4542 if (!tmp) {
4543 TALLOC_FREE(ctx);
4544 return NT_STATUS_NO_MEMORY;
4546 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4547 tmp = talloc_asprintf_append(tmp, "%s",
4548 CLI_DIRSEP_STR);
4550 if (!tmp) {
4551 TALLOC_FREE(ctx);
4552 return NT_STATUS_NO_MEMORY;
4554 info->matches[info->count] = SMB_STRDUP(tmp);
4555 TALLOC_FREE(ctx);
4557 if (info->matches[info->count] == NULL) {
4558 return NT_STATUS_OK;
4560 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4561 smb_readline_ca_char(0);
4563 if (info->count == 1) {
4564 info->samelen = strlen(info->matches[info->count]);
4565 } else {
4566 while (strncmp(info->matches[info->count],
4567 info->matches[info->count-1],
4568 info->samelen) != 0) {
4569 info->samelen--;
4572 info->count++;
4573 return NT_STATUS_OK;
4576 static char **remote_completion(const char *text, int len)
4578 TALLOC_CTX *ctx = talloc_stackframe();
4579 char *dirmask = NULL;
4580 char *targetpath = NULL;
4581 struct cli_state *targetcli = NULL;
4582 int i;
4583 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
4584 NTSTATUS status;
4586 /* can't have non-static initialisation on Sun CC, so do it
4587 at run time here */
4588 info.samelen = len;
4589 info.text = text;
4590 info.len = len;
4592 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
4593 if (!info.matches) {
4594 TALLOC_FREE(ctx);
4595 return NULL;
4599 * We're leaving matches[0] free to fill it later with the text to
4600 * display: Either the one single match or the longest common subset
4601 * of the matches.
4603 info.matches[0] = NULL;
4604 info.count = 1;
4606 for (i = len-1; i >= 0; i--) {
4607 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
4608 break;
4612 info.text = text+i+1;
4613 info.samelen = info.len = len-i-1;
4615 if (i > 0) {
4616 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
4617 if (!info.dirmask) {
4618 goto cleanup;
4620 strncpy(info.dirmask, text, i+1);
4621 info.dirmask[i+1] = 0;
4622 dirmask = talloc_asprintf(ctx,
4623 "%s%*s*",
4624 client_get_cur_dir(),
4625 i-1,
4626 text);
4627 } else {
4628 info.dirmask = SMB_STRDUP("");
4629 if (!info.dirmask) {
4630 goto cleanup;
4632 dirmask = talloc_asprintf(ctx,
4633 "%s*",
4634 client_get_cur_dir());
4636 if (!dirmask) {
4637 goto cleanup;
4640 if (!cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli, &targetpath)) {
4641 goto cleanup;
4643 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
4644 completion_remote_filter, (void *)&info);
4645 if (!NT_STATUS_IS_OK(status)) {
4646 goto cleanup;
4649 if (info.count == 1) {
4651 * No matches at all, NULL indicates there is nothing
4653 SAFE_FREE(info.matches[0]);
4654 SAFE_FREE(info.matches);
4655 TALLOC_FREE(ctx);
4656 return NULL;
4659 if (info.count == 2) {
4661 * Exactly one match in matches[1], indicate this is the one
4662 * in matches[0].
4664 info.matches[0] = info.matches[1];
4665 info.matches[1] = NULL;
4666 info.count -= 1;
4667 TALLOC_FREE(ctx);
4668 return info.matches;
4672 * We got more than one possible match, set the result to the maximum
4673 * common subset
4676 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
4677 info.matches[info.count] = NULL;
4678 return info.matches;
4680 cleanup:
4681 for (i = 0; i < info.count; i++) {
4682 SAFE_FREE(info.matches[i]);
4684 SAFE_FREE(info.matches);
4685 SAFE_FREE(info.dirmask);
4686 TALLOC_FREE(ctx);
4687 return NULL;
4690 static char **completion_fn(const char *text, int start, int end)
4692 smb_readline_ca_char(' ');
4694 if (start) {
4695 const char *buf, *sp;
4696 int i;
4697 char compl_type;
4699 buf = smb_readline_get_line_buffer();
4700 if (buf == NULL)
4701 return NULL;
4703 sp = strchr(buf, ' ');
4704 if (sp == NULL)
4705 return NULL;
4707 for (i = 0; commands[i].name; i++) {
4708 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
4709 (commands[i].name[sp - buf] == 0)) {
4710 break;
4713 if (commands[i].name == NULL)
4714 return NULL;
4716 while (*sp == ' ')
4717 sp++;
4719 if (sp == (buf + start))
4720 compl_type = commands[i].compl_args[0];
4721 else
4722 compl_type = commands[i].compl_args[1];
4724 if (compl_type == COMPL_REMOTE)
4725 return remote_completion(text, end - start);
4726 else /* fall back to local filename completion */
4727 return NULL;
4728 } else {
4729 char **matches;
4730 int i, len, samelen = 0, count=1;
4732 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
4733 if (!matches) {
4734 return NULL;
4736 matches[0] = NULL;
4738 len = strlen(text);
4739 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
4740 if (strncmp(text, commands[i].name, len) == 0) {
4741 matches[count] = SMB_STRDUP(commands[i].name);
4742 if (!matches[count])
4743 goto cleanup;
4744 if (count == 1)
4745 samelen = strlen(matches[count]);
4746 else
4747 while (strncmp(matches[count], matches[count-1], samelen) != 0)
4748 samelen--;
4749 count++;
4753 switch (count) {
4754 case 0: /* should never happen */
4755 case 1:
4756 goto cleanup;
4757 case 2:
4758 matches[0] = SMB_STRDUP(matches[1]);
4759 break;
4760 default:
4761 matches[0] = (char *)SMB_MALLOC(samelen+1);
4762 if (!matches[0])
4763 goto cleanup;
4764 strncpy(matches[0], matches[1], samelen);
4765 matches[0][samelen] = 0;
4767 matches[count] = NULL;
4768 return matches;
4770 cleanup:
4771 for (i = 0; i < count; i++)
4772 free(matches[i]);
4774 free(matches);
4775 return NULL;
4779 static bool finished;
4781 /****************************************************************************
4782 Make sure we swallow keepalives during idle time.
4783 ****************************************************************************/
4785 static void readline_callback(void)
4787 static time_t last_t;
4788 struct timespec now;
4789 time_t t;
4790 int ret, revents;
4792 clock_gettime_mono(&now);
4793 t = now.tv_sec;
4795 if (t - last_t < 5)
4796 return;
4798 last_t = t;
4800 again:
4802 if (cli->fd == -1)
4803 return;
4805 /* We deliberately use receive_smb_raw instead of
4806 client_receive_smb as we want to receive
4807 session keepalives and then drop them here.
4810 ret = poll_intr_one_fd(cli->fd, POLLIN|POLLHUP, 0, &revents);
4812 if ((ret > 0) && (revents & (POLLIN|POLLHUP|POLLERR))) {
4813 NTSTATUS status;
4814 size_t len;
4816 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
4818 status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize, 0, 0, &len);
4820 if (!NT_STATUS_IS_OK(status)) {
4821 DEBUG(0, ("Read from server failed, maybe it closed "
4822 "the connection\n"));
4824 finished = true;
4825 smb_readline_done();
4826 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
4827 set_smb_read_error(&cli->smb_rw_error,
4828 SMB_READ_EOF);
4829 return;
4832 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
4833 set_smb_read_error(&cli->smb_rw_error,
4834 SMB_READ_TIMEOUT);
4835 return;
4838 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
4839 return;
4841 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
4842 DEBUG(0, ("Read from server "
4843 "returned unexpected packet!\n"));
4844 return;
4847 goto again;
4850 /* Ping the server to keep the connection alive using SMBecho. */
4852 NTSTATUS status;
4853 unsigned char garbage[16];
4854 memset(garbage, 0xf0, sizeof(garbage));
4855 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
4857 if (NT_STATUS_IS_OK(status)) {
4858 return;
4861 if (!cli_state_is_connected(cli)) {
4862 DEBUG(0, ("SMBecho failed (%s). The connection is "
4863 "disconnected now\n", nt_errstr(status)));
4864 finished = true;
4865 smb_readline_done();
4870 /****************************************************************************
4871 Process commands on stdin.
4872 ****************************************************************************/
4874 static int process_stdin(void)
4876 int rc = 0;
4878 while (!finished) {
4879 TALLOC_CTX *frame = talloc_stackframe();
4880 char *tok = NULL;
4881 char *the_prompt = NULL;
4882 char *line = NULL;
4883 int i;
4885 /* display a prompt */
4886 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
4887 TALLOC_FREE(frame);
4888 break;
4890 line = smb_readline(the_prompt, readline_callback, completion_fn);
4891 SAFE_FREE(the_prompt);
4892 if (!line) {
4893 TALLOC_FREE(frame);
4894 break;
4897 /* special case - first char is ! */
4898 if (*line == '!') {
4899 if (system(line + 1) == -1) {
4900 d_printf("system() command %s failed.\n",
4901 line+1);
4903 SAFE_FREE(line);
4904 TALLOC_FREE(frame);
4905 continue;
4908 /* and get the first part of the command */
4909 cmd_ptr = line;
4910 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
4911 TALLOC_FREE(frame);
4912 SAFE_FREE(line);
4913 continue;
4916 if ((i = process_tok(tok)) >= 0) {
4917 rc = commands[i].fn();
4918 } else if (i == -2) {
4919 d_printf("%s: command abbreviation ambiguous\n",tok);
4920 } else {
4921 d_printf("%s: command not found\n",tok);
4923 SAFE_FREE(line);
4924 TALLOC_FREE(frame);
4926 return rc;
4929 /****************************************************************************
4930 Process commands from the client.
4931 ****************************************************************************/
4933 static int process(const char *base_directory)
4935 int rc = 0;
4937 cli = cli_cm_open(talloc_tos(), NULL,
4938 have_ip ? dest_ss_str : desthost,
4939 service, auth_info, true, smb_encrypt,
4940 max_protocol, port, name_type);
4941 if (!cli) {
4942 return 1;
4945 if (base_directory && *base_directory) {
4946 rc = do_cd(base_directory);
4947 if (rc) {
4948 cli_shutdown(cli);
4949 return rc;
4953 if (cmdstr) {
4954 rc = process_command_string(cmdstr);
4955 } else {
4956 process_stdin();
4959 cli_shutdown(cli);
4960 return rc;
4963 /****************************************************************************
4964 Handle a -L query.
4965 ****************************************************************************/
4967 static int do_host_query(const char *query_host)
4969 cli = cli_cm_open(talloc_tos(), NULL,
4970 have_ip ? dest_ss_str : query_host, "IPC$", auth_info, true, smb_encrypt,
4971 max_protocol, port, name_type);
4972 if (!cli)
4973 return 1;
4975 browse_host(true);
4977 /* Ensure that the host can do IPv4 */
4979 if (!interpret_addr(query_host)) {
4980 struct sockaddr_storage ss;
4981 if (interpret_string_addr(&ss, query_host, 0) &&
4982 (ss.ss_family != AF_INET)) {
4983 d_printf("%s is an IPv6 address -- no workgroup available\n",
4984 query_host);
4985 return 1;
4989 if (port != 139) {
4991 /* Workgroups simply don't make sense over anything
4992 else but port 139... */
4994 cli_shutdown(cli);
4995 cli = cli_cm_open(talloc_tos(), NULL,
4996 have_ip ? dest_ss_str : query_host, "IPC$",
4997 auth_info, true, smb_encrypt,
4998 max_protocol, 139, name_type);
5001 if (cli == NULL) {
5002 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
5003 return 1;
5006 list_servers(lp_workgroup());
5008 cli_shutdown(cli);
5010 return(0);
5013 /****************************************************************************
5014 Handle a tar operation.
5015 ****************************************************************************/
5017 static int do_tar_op(const char *base_directory)
5019 int ret;
5021 /* do we already have a connection? */
5022 if (!cli) {
5023 cli = cli_cm_open(talloc_tos(), NULL,
5024 have_ip ? dest_ss_str : desthost,
5025 service, auth_info, true, smb_encrypt,
5026 max_protocol, port, name_type);
5027 if (!cli)
5028 return 1;
5031 recurse=true;
5033 if (base_directory && *base_directory) {
5034 ret = do_cd(base_directory);
5035 if (ret) {
5036 cli_shutdown(cli);
5037 return ret;
5041 ret=process_tar();
5043 cli_shutdown(cli);
5045 return(ret);
5048 /****************************************************************************
5049 Handle a message operation.
5050 ****************************************************************************/
5052 static int do_message_op(struct user_auth_info *a_info)
5054 struct sockaddr_storage ss;
5055 struct nmb_name called, calling;
5056 fstring server_name;
5057 char name_type_hex[10];
5058 int msg_port;
5059 NTSTATUS status;
5061 make_nmb_name(&calling, calling_name, 0x0);
5062 make_nmb_name(&called , desthost, name_type);
5064 fstrcpy(server_name, desthost);
5065 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
5066 fstrcat(server_name, name_type_hex);
5068 zero_sockaddr(&ss);
5069 if (have_ip)
5070 ss = dest_ss;
5072 /* we can only do messages over port 139 (to windows clients at least) */
5074 msg_port = port ? port : 139;
5076 if (!(cli=cli_initialise())) {
5077 d_printf("Connection to %s failed\n", desthost);
5078 return 1;
5080 cli_set_port(cli, msg_port);
5082 status = cli_connect(cli, server_name, &ss);
5083 if (!NT_STATUS_IS_OK(status)) {
5084 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
5085 return 1;
5088 if (!cli_session_request(cli, &calling, &called)) {
5089 d_printf("session request failed\n");
5090 cli_shutdown(cli);
5091 return 1;
5094 send_message(get_cmdline_auth_info_username(a_info));
5095 cli_shutdown(cli);
5097 return 0;
5100 /****************************************************************************
5101 main program
5102 ****************************************************************************/
5104 int main(int argc,char *argv[])
5106 char *base_directory = NULL;
5107 int opt;
5108 char *query_host = NULL;
5109 bool message = false;
5110 static const char *new_name_resolve_order = NULL;
5111 poptContext pc;
5112 char *p;
5113 int rc = 0;
5114 fstring new_workgroup;
5115 bool tar_opt = false;
5116 bool service_opt = false;
5117 struct poptOption long_options[] = {
5118 POPT_AUTOHELP
5120 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
5121 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
5122 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
5123 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
5124 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
5125 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
5126 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
5127 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
5128 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
5129 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
5130 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
5131 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
5132 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
5133 POPT_COMMON_SAMBA
5134 POPT_COMMON_CONNECTION
5135 POPT_COMMON_CREDENTIALS
5136 POPT_TABLEEND
5138 TALLOC_CTX *frame = talloc_stackframe();
5140 if (!client_set_cur_dir("\\")) {
5141 exit(ENOMEM);
5144 /* initialize the workgroup name so we can determine whether or
5145 not it was set by a command line option */
5147 set_global_myworkgroup( "" );
5148 set_global_myname( "" );
5150 /* set default debug level to 1 regardless of what smb.conf sets */
5151 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
5152 load_case_tables();
5154 lp_set_cmdline("log level", "1");
5156 auth_info = user_auth_info_init(frame);
5157 if (auth_info == NULL) {
5158 exit(1);
5160 popt_common_set_auth_info(auth_info);
5162 /* skip argv(0) */
5163 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
5164 poptSetOtherOptionHelp(pc, "service <password>");
5166 lp_set_in_client(true); /* Make sure that we tell lp_load we are */
5168 while ((opt = poptGetNextOpt(pc)) != -1) {
5170 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
5171 /* I see no other way to keep things sane --SSS */
5172 if (tar_opt == true) {
5173 while (poptPeekArg(pc)) {
5174 poptGetArg(pc);
5176 tar_opt = false;
5179 /* if the service has not yet been specified lets see if it is available in the popt stack */
5180 if (!service_opt && poptPeekArg(pc)) {
5181 service = talloc_strdup(frame, poptGetArg(pc));
5182 if (!service) {
5183 exit(ENOMEM);
5185 service_opt = true;
5188 /* if the service has already been retrieved then check if we have also a password */
5189 if (service_opt
5190 && (!get_cmdline_auth_info_got_pass(auth_info))
5191 && poptPeekArg(pc)) {
5192 set_cmdline_auth_info_password(auth_info,
5193 poptGetArg(pc));
5196 switch (opt) {
5197 case 'M':
5198 /* Messages are sent to NetBIOS name type 0x3
5199 * (Messenger Service). Make sure we default
5200 * to port 139 instead of port 445. srl,crh
5202 name_type = 0x03;
5203 desthost = talloc_strdup(frame,poptGetOptArg(pc));
5204 if (!desthost) {
5205 exit(ENOMEM);
5207 if( !port )
5208 port = 139;
5209 message = true;
5210 break;
5211 case 'I':
5213 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
5214 exit(1);
5216 have_ip = true;
5217 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
5219 break;
5220 case 'E':
5221 setup_logging("smbclient", DEBUG_STDERR );
5222 display_set_stderr();
5223 break;
5225 case 'L':
5226 query_host = talloc_strdup(frame, poptGetOptArg(pc));
5227 if (!query_host) {
5228 exit(ENOMEM);
5230 break;
5231 case 'm':
5232 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
5233 break;
5234 case 'T':
5235 /* We must use old option processing for this. Find the
5236 * position of the -T option in the raw argv[]. */
5238 int i;
5239 for (i = 1; i < argc; i++) {
5240 if (strncmp("-T", argv[i],2)==0)
5241 break;
5243 i++;
5244 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
5245 poptPrintUsage(pc, stderr, 0);
5246 exit(1);
5249 /* this must be the last option, mark we have parsed it so that we know we have */
5250 tar_opt = true;
5251 break;
5252 case 'D':
5253 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
5254 if (!base_directory) {
5255 exit(ENOMEM);
5257 break;
5258 case 'g':
5259 grepable=true;
5260 break;
5261 case 'e':
5262 smb_encrypt=true;
5263 break;
5264 case 'B':
5265 return(do_smb_browse());
5270 /* We may still have some leftovers after the last popt option has been called */
5271 if (tar_opt == true) {
5272 while (poptPeekArg(pc)) {
5273 poptGetArg(pc);
5275 tar_opt = false;
5278 /* if the service has not yet been specified lets see if it is available in the popt stack */
5279 if (!service_opt && poptPeekArg(pc)) {
5280 service = talloc_strdup(frame,poptGetArg(pc));
5281 if (!service) {
5282 exit(ENOMEM);
5284 service_opt = true;
5287 /* if the service has already been retrieved then check if we have also a password */
5288 if (service_opt
5289 && !get_cmdline_auth_info_got_pass(auth_info)
5290 && poptPeekArg(pc)) {
5291 set_cmdline_auth_info_password(auth_info,
5292 poptGetArg(pc));
5295 /* save the workgroup...
5297 FIXME!! do we need to do this for other options as well
5298 (or maybe a generic way to keep lp_load() from overwriting
5299 everything)? */
5301 fstrcpy( new_workgroup, lp_workgroup() );
5302 calling_name = talloc_strdup(frame, global_myname() );
5303 if (!calling_name) {
5304 exit(ENOMEM);
5307 if ( override_logfile )
5308 setup_logging( lp_logfile(), DEBUG_FILE );
5310 if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
5311 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
5312 argv[0], get_dyn_CONFIGFILE());
5315 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
5316 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
5317 exit(-1);
5320 load_interfaces();
5322 if (service_opt && service) {
5323 size_t len;
5325 /* Convert any '/' characters in the service name to '\' characters */
5326 string_replace(service, '/','\\');
5327 if (count_chars(service,'\\') < 3) {
5328 d_printf("\n%s: Not enough '\\' characters in service\n",service);
5329 poptPrintUsage(pc, stderr, 0);
5330 exit(1);
5332 /* Remove trailing slashes */
5333 len = strlen(service);
5334 while(len > 0 && service[len - 1] == '\\') {
5335 --len;
5336 service[len] = '\0';
5340 if ( strlen(new_workgroup) != 0 ) {
5341 set_global_myworkgroup( new_workgroup );
5344 if ( strlen(calling_name) != 0 ) {
5345 set_global_myname( calling_name );
5346 } else {
5347 TALLOC_FREE(calling_name);
5348 calling_name = talloc_strdup(frame, global_myname() );
5351 smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
5352 if (!init_names()) {
5353 fprintf(stderr, "init_names() failed\n");
5354 exit(1);
5357 if(new_name_resolve_order)
5358 lp_set_name_resolve_order(new_name_resolve_order);
5360 if (!tar_type && !query_host && !service && !message) {
5361 poptPrintUsage(pc, stderr, 0);
5362 exit(1);
5365 poptFreeContext(pc);
5367 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5369 /* Ensure we have a password (or equivalent). */
5370 set_cmdline_auth_info_getpass(auth_info);
5372 if (tar_type) {
5373 if (cmdstr)
5374 process_command_string(cmdstr);
5375 return do_tar_op(base_directory);
5378 if (query_host && *query_host) {
5379 char *qhost = query_host;
5380 char *slash;
5382 while (*qhost == '\\' || *qhost == '/')
5383 qhost++;
5385 if ((slash = strchr_m(qhost, '/'))
5386 || (slash = strchr_m(qhost, '\\'))) {
5387 *slash = 0;
5390 if ((p=strchr_m(qhost, '#'))) {
5391 *p = 0;
5392 p++;
5393 sscanf(p, "%x", &name_type);
5396 return do_host_query(qhost);
5399 if (message) {
5400 return do_message_op(auth_info);
5403 if (process(base_directory)) {
5404 return 1;
5407 TALLOC_FREE(frame);
5408 return rc;