2 Unix SMB/CIFS implementation.
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/>.
25 #include "system/filesys.h"
26 #include "popt_common_cmdline.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "client/client_proto.h"
29 #include "client/clitar_proto.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
31 #include "../lib/util/select.h"
32 #include "system/readline.h"
33 #include "../libcli/smbreadline/smbreadline.h"
34 #include "../libcli/security/security.h"
35 #include "system/select.h"
36 #include "libsmb/libsmb.h"
37 #include "libsmb/clirap.h"
39 #include "libsmb/nmblib.h"
40 #include "include/ntioctl.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/util/time_basic.h"
48 extern int do_smb_browse(void); /* mDNS browsing */
50 extern bool override_logfile;
54 static char *desthost;
55 static bool grepable = false;
56 static bool quiet = false;
57 static char *cmdstr = NULL;
58 const char *cmd_ptr = NULL;
60 static int io_bufsize = 0; /* we use the default size */
61 static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
63 static int name_type = 0x20;
64 static int max_protocol = -1;
66 static int process_tok(char *tok);
67 static int cmd_help(void);
69 /* value for unused fid field in trans2 secondary request */
70 #define FID_UNUSED (0xFFFF)
72 time_t newer_than = 0;
73 static int archive_level = 0;
75 static bool translation = false;
78 static bool prompt = true;
80 static bool recurse = false;
81 static bool showacls = false;
82 bool lowercase = false;
83 static bool backup_intent = false;
85 static struct sockaddr_storage dest_ss;
86 static char dest_ss_str[INET6_ADDRSTRLEN];
88 #define SEPARATORS " \t\n\r"
90 static bool abort_mget = true;
93 uint64_t get_total_size = 0;
94 unsigned int get_total_time_ms = 0;
95 static uint64_t put_total_size = 0;
96 static unsigned int put_total_time_ms = 0;
99 static double dir_total;
101 /* encrypted state. */
102 static bool smb_encrypt;
104 /* root cli_state connection */
106 struct cli_state *cli;
108 static char CLI_DIRSEP_CHAR = '\\';
109 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
111 /* Accessor functions for directory paths. */
112 static char *fileselection;
113 static const char *client_get_fileselection(void)
116 return fileselection;
121 static const char *client_set_fileselection(const char *new_fs)
123 SAFE_FREE(fileselection);
125 fileselection = SMB_STRDUP(new_fs);
127 return client_get_fileselection();
131 static const char *client_get_cwd(void)
136 return CLI_DIRSEP_STR;
139 static const char *client_set_cwd(const char *new_cwd)
143 cwd = SMB_STRDUP(new_cwd);
145 return client_get_cwd();
148 static char *cur_dir;
149 const char *client_get_cur_dir(void)
154 return CLI_DIRSEP_STR;
157 const char *client_set_cur_dir(const char *newdir)
161 cur_dir = SMB_STRDUP(newdir);
163 return client_get_cur_dir();
166 /****************************************************************************
167 Put up a yes/no prompt.
168 ****************************************************************************/
170 static bool yesno(const char *p)
175 if (!fgets(ans,sizeof(ans)-1,stdin))
178 if (*ans == 'y' || *ans == 'Y')
184 /****************************************************************************
185 Write to a local file with CR/LF->LF translation if appropriate. Return the
186 number taken from the buffer. This may not equal the number written.
187 ****************************************************************************/
189 static ssize_t writefile(int f, char *b, size_t n)
203 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
206 if (write(f, b, 1) != 1) {
216 /****************************************************************************
217 Read from a file with LF->CR/LF translation if appropriate. Return the
218 number read. read approx n bytes.
219 ****************************************************************************/
221 static int readfile(uint8_t *b, int n, FILE *f)
227 return fread(b,1,n,f);
230 while (i < (n - 1)) {
231 if ((c = getc(f)) == EOF) {
235 if (c == '\n') { /* change all LFs to CR/LF */
250 static size_t push_source(uint8_t *buf, size_t n, void *priv)
252 struct push_state *state = (struct push_state *)priv;
255 if (feof(state->f)) {
259 result = readfile(buf, n, state->f);
260 state->nread += result;
264 /****************************************************************************
266 ****************************************************************************/
268 static void send_message(const char *username)
274 d_printf("Type your message, ending it with a Control-D\n");
277 while (i<sizeof(buf)-2) {
278 int c = fgetc(stdin);
289 status = cli_message(cli, desthost, username, buf);
290 if (!NT_STATUS_IS_OK(status)) {
291 d_fprintf(stderr, "cli_message returned %s\n",
296 /****************************************************************************
297 Check the space on a device.
298 ****************************************************************************/
300 static int do_dskattr(void)
302 uint64_t total, bsize, avail;
303 struct cli_state *targetcli = NULL;
304 char *targetpath = NULL;
305 TALLOC_CTX *ctx = talloc_tos();
308 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(), cli,
309 client_get_cur_dir(), &targetcli,
311 if (!NT_STATUS_IS_OK(status)) {
312 d_printf("Error in dskattr: %s\n", nt_errstr(status));
316 status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
317 if (!NT_STATUS_IS_OK(status)) {
318 d_printf("Error in dskattr: %s\n", nt_errstr(status));
322 d_printf("\n\t\t%" PRIu64
323 " blocks of size %" PRIu64
324 ". %" PRIu64 " blocks available\n",
325 total, bsize, avail);
330 /****************************************************************************
332 ****************************************************************************/
334 static int cmd_pwd(void)
336 d_printf("Current directory is %s",service);
337 d_printf("%s\n",client_get_cur_dir());
341 /****************************************************************************
342 Ensure name has correct directory separators.
343 ****************************************************************************/
345 static void normalize_name(char *newdir)
347 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
348 string_replace(newdir,'/','\\');
352 /****************************************************************************
353 Local name cleanup before sending to server. SMB1 allows relative pathnames,
354 but SMB2 does not, so we need to resolve them locally.
355 ****************************************************************************/
357 char *client_clean_name(TALLOC_CTX *ctx, const char *name)
359 char *newname = NULL;
364 /* First ensure any path separators are correct. */
365 newname = talloc_strdup(ctx, name);
366 if (newname == NULL) {
369 normalize_name(newname);
371 /* Now remove any relative (..) path components. */
372 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
373 newname = unix_clean_name(ctx, newname);
375 newname = clean_name(ctx, newname);
377 if (newname == NULL) {
383 /****************************************************************************
384 Change directory - inner section.
385 ****************************************************************************/
387 static int do_cd(const char *new_dir)
390 char *saved_dir = NULL;
392 char *targetpath = NULL;
393 struct cli_state *targetcli = NULL;
394 SMB_STRUCT_STAT sbuf;
397 TALLOC_CTX *ctx = talloc_stackframe();
400 newdir = talloc_strdup(ctx, new_dir);
406 normalize_name(newdir);
408 /* Save the current directory in case the new directory is invalid */
410 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
416 if (*newdir == CLI_DIRSEP_CHAR) {
417 client_set_cur_dir(newdir);
420 new_cd = talloc_asprintf(ctx, "%s%s",
421 client_get_cur_dir(),
428 /* Ensure cur_dir ends in a DIRSEP */
429 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
430 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
435 client_set_cur_dir(new_cd);
437 new_cd = client_clean_name(ctx, new_cd);
438 client_set_cur_dir(new_cd);
440 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
441 cli, new_cd, &targetcli, &targetpath);
442 if (!NT_STATUS_IS_OK(status)) {
443 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
444 client_set_cur_dir(saved_dir);
448 if (strequal(targetpath,CLI_DIRSEP_STR )) {
453 /* Use a trans2_qpathinfo to test directories for modern servers.
454 Except Win9x doesn't support the qpathinfo_basic() call..... */
456 if (smbXcli_conn_protocol(targetcli->conn) > PROTOCOL_LANMAN2 && !targetcli->win95) {
458 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
460 if (!NT_STATUS_IS_OK(status)) {
461 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
462 client_set_cur_dir(saved_dir);
466 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
467 d_printf("cd %s: not a directory\n", new_cd);
468 client_set_cur_dir(saved_dir);
473 targetpath = talloc_asprintf(ctx,
478 client_set_cur_dir(saved_dir);
481 targetpath = client_clean_name(ctx, targetpath);
483 client_set_cur_dir(saved_dir);
487 status = cli_chkpath(targetcli, targetpath);
488 if (!NT_STATUS_IS_OK(status)) {
489 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
490 client_set_cur_dir(saved_dir);
503 /****************************************************************************
505 ****************************************************************************/
507 static int cmd_cd(void)
512 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
515 d_printf("Current directory is %s\n",client_get_cur_dir());
521 /****************************************************************************
523 ****************************************************************************/
525 static int cmd_cd_oneup(void)
530 /*******************************************************************
531 Decide if a file should be operated on.
532 ********************************************************************/
534 static bool do_this_one(struct file_info *finfo)
540 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
544 if (*client_get_fileselection() &&
545 !mask_match(finfo->name,client_get_fileselection(),false)) {
546 DEBUG(3,("mask_match %s failed\n", finfo->name));
550 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
551 DEBUG(3,("newer_than %s failed\n", finfo->name));
555 if ((archive_level==1 || archive_level==2) && !(finfo->attr & FILE_ATTRIBUTE_ARCHIVE)) {
556 DEBUG(3,("archive %s failed\n", finfo->name));
563 /****************************************************************************
564 Display info about a file.
565 ****************************************************************************/
567 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
571 TALLOC_CTX *ctx = talloc_tos();
572 NTSTATUS status = NT_STATUS_OK;
574 if (!do_this_one(finfo)) {
578 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
580 d_printf(" %-30s%7.7s %8.0f %s",
582 attrib_string(talloc_tos(), finfo->attr),
585 dir_total += finfo->size;
590 /* skip if this is . or .. */
591 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
593 /* create absolute filename for cli_ntcreate() FIXME */
594 afname = talloc_asprintf(ctx,
600 return NT_STATUS_NO_MEMORY;
602 /* print file meta date header */
603 d_printf( "FILENAME:%s\n", finfo->name);
604 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->attr));
605 d_printf( "SIZE:%.0f\n", (double)finfo->size);
606 d_printf( "MTIME:%s", time_to_asc(t));
607 status = cli_ntcreate(
611 READ_CONTROL_ACCESS, /* DesiredAccess */
612 0, /* FileAttributes */
614 FILE_SHARE_WRITE, /* ShareAccess */
615 FILE_OPEN, /* CreateDisposition */
616 0x0, /* CreateOptions */
617 0x0, /* SecurityFlags */
620 if (!NT_STATUS_IS_OK(status)) {
621 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
622 afname, nt_errstr(status)));
624 struct security_descriptor *sd = NULL;
625 status = cli_query_secdesc(cli_state, fnum,
627 if (!NT_STATUS_IS_OK(status)) {
628 DEBUG( 0, ("display_finfo() failed to "
629 "get security descriptor: %s",
632 display_sec_desc(sd);
641 /****************************************************************************
642 Accumulate size of a file.
643 ****************************************************************************/
645 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
648 if (do_this_one(finfo)) {
649 dir_total += finfo->size;
654 static bool do_list_recurse;
655 static bool do_list_dirs;
656 static char *do_list_queue = 0;
657 static long do_list_queue_size = 0;
658 static long do_list_queue_start = 0;
659 static long do_list_queue_end = 0;
660 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
663 /****************************************************************************
664 Functions for do_list_queue.
665 ****************************************************************************/
668 * The do_list_queue is a NUL-separated list of strings stored in a
669 * char*. Since this is a FIFO, we keep track of the beginning and
670 * ending locations of the data in the queue. When we overflow, we
671 * double the size of the char*. When the start of the data passes
672 * the midpoint, we move everything back. This is logically more
673 * complex than a linked list, but easier from a memory management
674 * angle. In any memory error condition, do_list_queue is reset.
675 * Functions check to ensure that do_list_queue is non-NULL before
679 static void reset_do_list_queue(void)
681 SAFE_FREE(do_list_queue);
682 do_list_queue_size = 0;
683 do_list_queue_start = 0;
684 do_list_queue_end = 0;
687 static void init_do_list_queue(void)
689 reset_do_list_queue();
690 do_list_queue_size = 1024;
691 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
692 if (do_list_queue == 0) {
693 d_printf("malloc fail for size %d\n",
694 (int)do_list_queue_size);
695 reset_do_list_queue();
697 memset(do_list_queue, 0, do_list_queue_size);
701 static void adjust_do_list_queue(void)
704 * If the starting point of the queue is more than half way through,
705 * move everything toward the beginning.
708 if (do_list_queue == NULL) {
709 DEBUG(4,("do_list_queue is empty\n"));
710 do_list_queue_start = do_list_queue_end = 0;
714 if (do_list_queue_start == do_list_queue_end) {
715 DEBUG(4,("do_list_queue is empty\n"));
716 do_list_queue_start = do_list_queue_end = 0;
717 *do_list_queue = '\0';
718 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
719 DEBUG(4,("sliding do_list_queue backward\n"));
720 memmove(do_list_queue,
721 do_list_queue + do_list_queue_start,
722 do_list_queue_end - do_list_queue_start);
723 do_list_queue_end -= do_list_queue_start;
724 do_list_queue_start = 0;
728 static void add_to_do_list_queue(const char *entry)
730 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
731 while (new_end > do_list_queue_size) {
732 do_list_queue_size *= 2;
733 DEBUG(4,("enlarging do_list_queue to %d\n",
734 (int)do_list_queue_size));
735 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
736 if (! do_list_queue) {
737 d_printf("failure enlarging do_list_queue to %d bytes\n",
738 (int)do_list_queue_size);
739 reset_do_list_queue();
741 memset(do_list_queue + do_list_queue_size / 2,
742 0, do_list_queue_size / 2);
746 strlcpy_base(do_list_queue + do_list_queue_end,
747 entry, do_list_queue, do_list_queue_size);
748 do_list_queue_end = new_end;
749 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
750 entry, (int)do_list_queue_start, (int)do_list_queue_end));
754 static char *do_list_queue_head(void)
756 return do_list_queue + do_list_queue_start;
759 static void remove_do_list_queue_head(void)
761 if (do_list_queue_end > do_list_queue_start) {
762 do_list_queue_start += strlen(do_list_queue_head()) + 1;
763 adjust_do_list_queue();
764 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
765 (int)do_list_queue_start, (int)do_list_queue_end));
769 static int do_list_queue_empty(void)
771 return (! (do_list_queue && *do_list_queue));
774 /****************************************************************************
775 A helper for do_list.
776 ****************************************************************************/
778 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
779 const char *mask, void *state)
781 struct cli_state *cli_state = (struct cli_state *)state;
782 TALLOC_CTX *ctx = talloc_tos();
784 char *dir_end = NULL;
785 NTSTATUS status = NT_STATUS_OK;
787 /* Work out the directory. */
788 dir = talloc_strdup(ctx, mask);
790 return NT_STATUS_NO_MEMORY;
792 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
796 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
797 if (do_list_dirs && do_this_one(f)) {
798 status = do_list_fn(cli_state, f, dir);
799 if (!NT_STATUS_IS_OK(status)) {
803 if (do_list_recurse &&
805 !strequal(f->name,".") &&
806 !strequal(f->name,"..")) {
811 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
813 return NT_STATUS_UNSUCCESSFUL;
816 mask2 = talloc_asprintf(ctx,
822 return NT_STATUS_NO_MEMORY;
824 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
830 mask2 = talloc_asprintf_append(mask2,
836 return NT_STATUS_NO_MEMORY;
838 add_to_do_list_queue(mask2);
845 if (do_this_one(f)) {
846 status = do_list_fn(cli_state, f, dir);
852 /****************************************************************************
853 A wrapper around cli_list that adds recursion.
854 ****************************************************************************/
856 NTSTATUS do_list(const char *mask,
858 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
863 static int in_do_list = 0;
864 TALLOC_CTX *ctx = talloc_tos();
865 struct cli_state *targetcli = NULL;
866 char *targetpath = NULL;
867 NTSTATUS ret_status = NT_STATUS_OK;
868 NTSTATUS status = NT_STATUS_OK;
870 if (in_do_list && rec) {
871 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
877 do_list_recurse = rec;
882 init_do_list_queue();
883 add_to_do_list_queue(mask);
885 while (!do_list_queue_empty()) {
887 * Need to copy head so that it doesn't become
888 * invalid inside the call to cli_list. This
889 * would happen if the list were expanded
891 * Fix from E. Jay Berkenbilt (ejb@ql.org)
893 char *head = talloc_strdup(ctx, do_list_queue_head());
896 return NT_STATUS_NO_MEMORY;
901 status = cli_resolve_path(ctx, "",
902 popt_get_cmdline_auth_info(),
903 cli, head, &targetcli, &targetpath);
904 if (!NT_STATUS_IS_OK(status)) {
905 d_printf("do_list: [%s] %s\n", head,
907 remove_do_list_queue_head();
911 status = cli_list(targetcli, targetpath, attribute,
912 do_list_helper, targetcli);
913 if (!NT_STATUS_IS_OK(status)) {
914 d_printf("%s listing %s\n",
915 nt_errstr(status), targetpath);
918 remove_do_list_queue_head();
919 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
920 char *next_file = do_list_queue_head();
922 if ((strlen(next_file) >= 2) &&
923 (next_file[strlen(next_file) - 1] == '*') &&
924 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
925 save_ch = next_file +
926 strlen(next_file) - 2;
929 /* cwd is only used if showacls is on */
930 client_set_cwd(next_file);
933 if (!showacls) /* don't disturbe the showacls output */
934 d_printf("\n%s\n",next_file);
936 *save_ch = CLI_DIRSEP_CHAR;
940 TALLOC_FREE(targetpath);
944 status = cli_resolve_path(ctx, "",
945 popt_get_cmdline_auth_info(), cli, mask,
946 &targetcli, &targetpath);
947 if (NT_STATUS_IS_OK(status)) {
948 status = cli_list(targetcli, targetpath, attribute,
949 do_list_helper, targetcli);
950 if (!NT_STATUS_IS_OK(status)) {
951 d_printf("%s listing %s\n",
952 nt_errstr(status), targetpath);
955 TALLOC_FREE(targetpath);
957 d_printf("do_list: [%s] %s\n", mask, nt_errstr(status));
963 reset_do_list_queue();
967 /****************************************************************************
968 Get a directory listing.
969 ****************************************************************************/
971 static int cmd_dir(void)
973 TALLOC_CTX *ctx = talloc_tos();
974 uint32_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
981 mask = talloc_strdup(ctx, client_get_cur_dir());
986 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
988 if (*buf == CLI_DIRSEP_CHAR) {
989 mask = talloc_strdup(ctx, buf);
991 mask = talloc_asprintf_append(mask, "%s", buf);
994 mask = talloc_asprintf_append(mask, "*");
1000 mask = client_clean_name(ctx, mask);
1006 /* cwd is only used if showacls is on */
1007 client_set_cwd(client_get_cur_dir());
1010 status = do_list(mask, attribute, display_finfo, recurse, true);
1011 if (!NT_STATUS_IS_OK(status)) {
1017 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
1022 /****************************************************************************
1023 Get a directory listing.
1024 ****************************************************************************/
1026 static int cmd_du(void)
1028 TALLOC_CTX *ctx = talloc_tos();
1029 uint32_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1036 mask = talloc_strdup(ctx, client_get_cur_dir());
1040 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
1041 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
1047 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1048 normalize_name(buf);
1049 if (*buf == CLI_DIRSEP_CHAR) {
1050 mask = talloc_strdup(ctx, buf);
1052 mask = talloc_asprintf_append(mask, "%s", buf);
1055 mask = talloc_strdup(ctx, "*");
1061 mask = client_clean_name(ctx, mask);
1066 status = do_list(mask, attribute, do_du, recurse, true);
1067 if (!NT_STATUS_IS_OK(status)) {
1073 d_printf("Total number of bytes: %.0f\n", dir_total);
1078 static int cmd_echo(void)
1080 TALLOC_CTX *ctx = talloc_tos();
1085 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1086 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1087 d_printf("echo <num> <data>\n");
1091 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1093 if (!NT_STATUS_IS_OK(status)) {
1094 d_printf("echo failed: %s\n", nt_errstr(status));
1101 /****************************************************************************
1102 Get a file from rname to lname
1103 ****************************************************************************/
1105 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1107 int *pfd = (int *)priv;
1110 rc = writefile(*pfd, buf, n);
1112 return map_nt_error_from_unix(errno);
1114 return NT_STATUS_OK;
1117 static int do_get(const char *rname, const char *lname_in, bool reget)
1119 TALLOC_CTX *ctx = talloc_tos();
1122 bool newhandle = false;
1123 struct timespec tp_start;
1129 struct cli_state *targetcli = NULL;
1130 char *targetname = NULL;
1134 lname = talloc_strdup(ctx, lname_in);
1140 if (!strlower_m(lname)) {
1141 d_printf("strlower_m %s failed\n", lname);
1146 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1147 cli, rname, &targetcli, &targetname);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1153 clock_gettime_mono(&tp_start);
1155 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 d_printf("%s opening remote file %s\n", nt_errstr(status),
1162 if(!strcmp(lname,"-")) {
1163 handle = fileno(stdout);
1166 handle = open(lname, O_WRONLY|O_CREAT, 0644);
1168 start = lseek(handle, 0, SEEK_END);
1170 d_printf("Error seeking local file\n");
1176 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1181 d_printf("Error opening local file %s\n",lname);
1186 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1188 if (!NT_STATUS_IS_OK(status)) {
1189 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1191 if(!NT_STATUS_IS_OK(status)) {
1192 d_printf("getattrib: %s\n", nt_errstr(status));
1200 DEBUG(1,("getting file %s of size %.0f as %s ",
1201 rname, (double)size, lname));
1203 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1204 writefile_sink, (void *)&handle, &nread);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 d_fprintf(stderr, "parallel_read returned %s\n",
1211 cli_close(targetcli, fnum);
1215 status = cli_close(targetcli, fnum);
1216 if (!NT_STATUS_IS_OK(status)) {
1217 d_printf("Error %s closing remote file\n", nt_errstr(status));
1225 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1226 cli_setatr(cli, rname, attr & ~(uint32_t)FILE_ATTRIBUTE_ARCHIVE, 0);
1230 struct timespec tp_end;
1233 clock_gettime_mono(&tp_end);
1234 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1235 get_total_time_ms += this_time;
1236 get_total_size += nread;
1238 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1239 nread / (1.024*this_time + 1.0e-4),
1240 get_total_size / (1.024*get_total_time_ms)));
1243 TALLOC_FREE(targetname);
1247 /****************************************************************************
1249 ****************************************************************************/
1251 static int cmd_get(void)
1253 TALLOC_CTX *ctx = talloc_tos();
1258 rname = talloc_strdup(ctx, client_get_cur_dir());
1263 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1264 d_printf("get <filename> [localname]\n");
1267 rname = talloc_asprintf_append(rname, "%s", fname);
1271 rname = client_clean_name(ctx, rname);
1276 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1281 return do_get(rname, lname, false);
1284 /****************************************************************************
1285 Do an mget operation on one file.
1286 ****************************************************************************/
1288 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1291 TALLOC_CTX *ctx = talloc_tos();
1292 NTSTATUS status = NT_STATUS_OK;
1295 char *saved_curdir = NULL;
1296 char *mget_mask = NULL;
1297 char *new_cd = NULL;
1300 return NT_STATUS_OK;
1303 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1304 return NT_STATUS_OK;
1307 d_printf("mget aborted\n");
1308 return NT_STATUS_UNSUCCESSFUL;
1311 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
1312 if (asprintf(&quest,
1313 "Get directory %s? ",finfo->name) < 0) {
1314 return NT_STATUS_NO_MEMORY;
1317 if (asprintf(&quest,
1318 "Get file %s? ",finfo->name) < 0) {
1319 return NT_STATUS_NO_MEMORY;
1323 if (prompt && !yesno(quest)) {
1325 return NT_STATUS_OK;
1329 if (!(finfo->attr & FILE_ATTRIBUTE_DIRECTORY)) {
1330 rname = talloc_asprintf(ctx,
1332 client_get_cur_dir(),
1335 return NT_STATUS_NO_MEMORY;
1337 rname = client_clean_name(ctx, rname);
1338 if (rname == NULL) {
1339 return NT_STATUS_NO_MEMORY;
1341 do_get(rname, finfo->name, false);
1343 return NT_STATUS_OK;
1346 /* handle directories */
1347 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1348 if (!saved_curdir) {
1349 return NT_STATUS_NO_MEMORY;
1352 new_cd = talloc_asprintf(ctx,
1354 client_get_cur_dir(),
1358 return NT_STATUS_NO_MEMORY;
1360 new_cd = client_clean_name(ctx, new_cd);
1361 if (new_cd == NULL) {
1362 return NT_STATUS_NO_MEMORY;
1364 client_set_cur_dir(new_cd);
1366 string_replace(finfo->name,'\\','/');
1368 if (!strlower_m(finfo->name)) {
1369 return NT_STATUS_INVALID_PARAMETER;
1373 if (!directory_exist(finfo->name) &&
1374 mkdir(finfo->name,0777) != 0) {
1375 d_printf("failed to create directory %s\n",finfo->name);
1376 client_set_cur_dir(saved_curdir);
1377 return map_nt_error_from_unix(errno);
1380 if (chdir(finfo->name) != 0) {
1381 d_printf("failed to chdir to directory %s\n",finfo->name);
1382 client_set_cur_dir(saved_curdir);
1383 return map_nt_error_from_unix(errno);
1386 mget_mask = talloc_asprintf(ctx,
1388 client_get_cur_dir());
1391 return NT_STATUS_NO_MEMORY;
1394 mget_mask = client_clean_name(ctx, mget_mask);
1395 if (mget_mask == NULL) {
1396 return NT_STATUS_NO_MEMORY;
1398 status = do_list(mget_mask,
1399 (FILE_ATTRIBUTE_SYSTEM
1400 | FILE_ATTRIBUTE_HIDDEN
1401 | FILE_ATTRIBUTE_DIRECTORY),
1402 do_mget, false, true);
1403 if (!NT_STATUS_IS_OK(status)
1404 && !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1406 * Ignore access denied errors to ensure all permitted files are
1412 if (chdir("..") == -1) {
1413 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1415 return map_nt_error_from_unix(errno);
1417 client_set_cur_dir(saved_curdir);
1418 TALLOC_FREE(mget_mask);
1419 TALLOC_FREE(saved_curdir);
1420 TALLOC_FREE(new_cd);
1421 return NT_STATUS_OK;
1424 /****************************************************************************
1425 View the file using the pager.
1426 ****************************************************************************/
1428 static int cmd_more(void)
1430 TALLOC_CTX *ctx = talloc_tos();
1434 char *pager_cmd = NULL;
1440 rname = talloc_strdup(ctx, client_get_cur_dir());
1445 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1449 mask = umask(S_IRWXO | S_IRWXG);
1450 fd = mkstemp(lname);
1453 d_printf("failed to create temporary file for more\n");
1458 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1459 d_printf("more <filename>\n");
1463 rname = talloc_asprintf_append(rname, "%s", fname);
1467 rname = client_clean_name(ctx,rname);
1472 rc = do_get(rname, lname, false);
1474 pager=getenv("PAGER");
1476 pager_cmd = talloc_asprintf(ctx,
1478 (pager? pager:PAGER),
1483 if (system(pager_cmd) == -1) {
1484 d_printf("system command '%s' returned -1\n",
1492 /****************************************************************************
1494 ****************************************************************************/
1496 static int cmd_mget(void)
1498 TALLOC_CTX *ctx = talloc_tos();
1499 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1500 char *mget_mask = NULL;
1502 NTSTATUS status = NT_STATUS_OK;
1505 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1510 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1512 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1516 if (*buf == CLI_DIRSEP_CHAR) {
1517 mget_mask = talloc_strdup(ctx, buf);
1519 mget_mask = talloc_asprintf_append(mget_mask,
1525 mget_mask = client_clean_name(ctx, mget_mask);
1526 if (mget_mask == NULL) {
1529 status = do_list(mget_mask, attribute, do_mget, false, true);
1530 if (!NT_STATUS_IS_OK(status)) {
1535 if (mget_mask == NULL) {
1536 d_printf("nothing to mget\n");
1541 mget_mask = talloc_asprintf(ctx,
1543 client_get_cur_dir());
1547 mget_mask = client_clean_name(ctx, mget_mask);
1548 if (mget_mask == NULL) {
1551 status = do_list(mget_mask, attribute, do_mget, false, true);
1552 if (!NT_STATUS_IS_OK(status)) {
1560 /****************************************************************************
1561 Make a directory of name "name".
1562 ****************************************************************************/
1564 static bool do_mkdir(const char *name)
1566 TALLOC_CTX *ctx = talloc_tos();
1567 struct cli_state *targetcli;
1568 char *targetname = NULL;
1571 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1572 cli, name, &targetcli, &targetname);
1573 if (!NT_STATUS_IS_OK(status)) {
1574 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1578 status = cli_mkdir(targetcli, targetname);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 d_printf("%s making remote directory %s\n",
1581 nt_errstr(status),name);
1588 /****************************************************************************
1589 Show 8.3 name of a file.
1590 ****************************************************************************/
1592 static bool do_altname(const char *name)
1597 status = cli_qpathinfo_alt_name(cli, name, altname);
1598 if (!NT_STATUS_IS_OK(status)) {
1599 d_printf("%s getting alt name for %s\n",
1600 nt_errstr(status),name);
1603 d_printf("%s\n", altname);
1608 /****************************************************************************
1610 ****************************************************************************/
1612 static int cmd_quit(void)
1615 popt_free_cmdline_auth_info();
1621 /****************************************************************************
1623 ****************************************************************************/
1625 static int cmd_mkdir(void)
1627 TALLOC_CTX *ctx = talloc_tos();
1632 mask = talloc_strdup(ctx, client_get_cur_dir());
1637 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1639 d_printf("mkdir <dirname>\n");
1643 mask = talloc_asprintf_append(mask, "%s", buf);
1647 mask = client_clean_name(ctx, mask);
1655 struct cli_state *targetcli;
1656 char *targetname = NULL;
1660 ddir2 = talloc_strdup(ctx, "");
1665 status = cli_resolve_path(ctx, "",
1666 popt_get_cmdline_auth_info(), cli, mask,
1667 &targetcli, &targetname);
1668 if (!NT_STATUS_IS_OK(status)) {
1672 ddir = talloc_strdup(ctx, targetname);
1676 trim_char(ddir,'.','\0');
1677 p = strtok_r(ddir, "/\\", &saveptr);
1679 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1683 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1686 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1690 p = strtok_r(NULL, "/\\", &saveptr);
1699 /****************************************************************************
1701 ****************************************************************************/
1703 static int cmd_altname(void)
1705 TALLOC_CTX *ctx = talloc_tos();
1709 name = talloc_strdup(ctx, client_get_cur_dir());
1714 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1715 d_printf("altname <file>\n");
1718 name = talloc_asprintf_append(name, "%s", buf);
1722 name = client_clean_name(ctx, name);
1730 static char *attr_str(TALLOC_CTX *mem_ctx, uint32_t attr)
1732 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1735 if (!(attr & FILE_ATTRIBUTE_NORMAL)) {
1736 if (attr & FILE_ATTRIBUTE_ENCRYPTED) {
1739 if (attr & FILE_ATTRIBUTE_NONINDEXED) {
1742 if (attr & FILE_ATTRIBUTE_OFFLINE) {
1745 if (attr & FILE_ATTRIBUTE_COMPRESSED) {
1748 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1751 if (attr & FILE_ATTRIBUTE_SPARSE) {
1754 if (attr & FILE_ATTRIBUTE_TEMPORARY) {
1757 if (attr & FILE_ATTRIBUTE_NORMAL) {
1760 if (attr & FILE_ATTRIBUTE_READONLY) {
1763 if (attr & FILE_ATTRIBUTE_HIDDEN) {
1766 if (attr & FILE_ATTRIBUTE_SYSTEM) {
1769 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
1772 if (attr & FILE_ATTRIBUTE_ARCHIVE) {
1779 /****************************************************************************
1780 Show all info we can get
1781 ****************************************************************************/
1783 static int do_allinfo(const char *name)
1786 struct timespec b_time, a_time, m_time, c_time;
1791 unsigned int num_streams;
1792 struct stream_struct *streams;
1794 char **snapshots = NULL;
1798 status = cli_qpathinfo_alt_name(cli, name, altname);
1799 if (!NT_STATUS_IS_OK(status)) {
1800 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1803 * Ignore not supported or not implemented, it does not
1804 * hurt if we can't list alternate names.
1806 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1807 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1813 d_printf("altname: %s\n", altname);
1815 status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1816 &size, &attr, NULL);
1817 if (!NT_STATUS_IS_OK(status)) {
1818 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1823 tmp = full_timespec_to_nt_time(&b_time);
1824 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1826 tmp = full_timespec_to_nt_time(&a_time);
1827 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1829 tmp = full_timespec_to_nt_time(&m_time);
1830 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1832 tmp = full_timespec_to_nt_time(&c_time);
1833 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1835 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), attr), attr);
1837 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1839 if (!NT_STATUS_IS_OK(status)) {
1840 d_printf("%s getting streams for %s\n", nt_errstr(status),
1845 for (i=0; i<num_streams; i++) {
1846 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1847 (unsigned long long)streams[i].size);
1850 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1851 char *subst, *print;
1854 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1856 if (!NT_STATUS_IS_OK(status)) {
1857 d_fprintf(stderr, "cli_readlink returned %s\n",
1860 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1861 subst, print, flags);
1867 status = cli_ntcreate(cli, name, 0,
1868 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1869 SEC_STD_SYNCHRONIZE, 0,
1870 FILE_SHARE_READ|FILE_SHARE_WRITE
1872 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
1873 if (!NT_STATUS_IS_OK(status)) {
1875 * Ignore failure, it does not hurt if we can't list
1881 * In order to get shadow copy data over SMB1 we
1882 * must call twice, once with 'get_names = false'
1883 * to get the size, then again with 'get_names = true'
1884 * to get the data or a Windows server fails to return
1885 * valid info. Samba doesn't have this bug. JRA.
1888 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1889 false, &snapshots, &num_snapshots);
1890 if (!NT_STATUS_IS_OK(status)) {
1891 cli_close(cli, fnum);
1894 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1895 true, &snapshots, &num_snapshots);
1896 if (!NT_STATUS_IS_OK(status)) {
1897 cli_close(cli, fnum);
1901 for (i=0; i<num_snapshots; i++) {
1904 d_printf("%s\n", snapshots[i]);
1905 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1906 snapshots[i], name);
1907 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1908 &m_time, &c_time, &size,
1910 if (!NT_STATUS_IS_OK(status)) {
1911 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1912 snap_name, nt_errstr(status));
1913 TALLOC_FREE(snap_name);
1916 tmp = unix_timespec_to_nt_time(b_time);
1917 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1918 tmp = unix_timespec_to_nt_time(a_time);
1919 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1920 tmp =unix_timespec_to_nt_time(m_time);
1921 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1922 tmp = unix_timespec_to_nt_time(c_time);
1923 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1924 d_printf("size: %d\n", (int)size);
1927 TALLOC_FREE(snapshots);
1928 cli_close(cli, fnum);
1933 /****************************************************************************
1934 Show all info we can get
1935 ****************************************************************************/
1937 static int cmd_allinfo(void)
1939 TALLOC_CTX *ctx = talloc_tos();
1943 name = talloc_strdup(ctx, client_get_cur_dir());
1948 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1949 d_printf("allinfo <file>\n");
1952 name = talloc_asprintf_append(name, "%s", buf);
1956 name = client_clean_name(ctx, name);
1965 /****************************************************************************
1967 ****************************************************************************/
1969 static int do_put(const char *rname, const char *lname, bool reput)
1971 TALLOC_CTX *ctx = talloc_tos();
1976 struct timespec tp_start;
1977 struct cli_state *targetcli;
1978 char *targetname = NULL;
1979 struct push_state state;
1982 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1983 cli, rname, &targetcli, &targetname);
1984 if (!NT_STATUS_IS_OK(status)) {
1985 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1989 clock_gettime_mono(&tp_start);
1992 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1993 if (NT_STATUS_IS_OK(status)) {
1994 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1995 targetcli, fnum, NULL,
1997 NULL, NULL, NULL)) &&
1998 !NT_STATUS_IS_OK(status = cli_getattrE(
1999 targetcli, fnum, NULL,
2002 d_printf("getattrib: %s\n", nt_errstr(status));
2007 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
2010 if (!NT_STATUS_IS_OK(status)) {
2011 d_printf("%s opening remote file %s\n", nt_errstr(status),
2016 /* allow files to be piped into smbclient
2019 Note that in this case this function will exit(0) rather
2021 if (!strcmp(lname, "-")) {
2023 /* size of file is not known */
2025 f = fopen(lname, "r");
2027 if (fseek(f, start, SEEK_SET) == -1) {
2028 d_printf("Error seeking local file\n");
2036 d_printf("Error opening local file %s\n",lname);
2040 DEBUG(1,("putting file %s as %s ",lname,
2043 setvbuf(f, NULL, _IOFBF, io_bufsize);
2048 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
2050 if (!NT_STATUS_IS_OK(status)) {
2051 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
2055 status = cli_close(targetcli, fnum);
2056 if (!NT_STATUS_IS_OK(status)) {
2057 d_printf("%s closing remote file %s\n", nt_errstr(status),
2070 struct timespec tp_end;
2073 clock_gettime_mono(&tp_end);
2074 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
2075 put_total_time_ms += this_time;
2076 put_total_size += state.nread;
2078 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
2079 state.nread / (1.024*this_time + 1.0e-4),
2080 put_total_size / (1.024*put_total_time_ms)));
2085 popt_free_cmdline_auth_info();
2092 /****************************************************************************
2094 ****************************************************************************/
2096 static int cmd_put(void)
2098 TALLOC_CTX *ctx = talloc_tos();
2103 rname = talloc_strdup(ctx, client_get_cur_dir());
2108 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
2109 d_printf("put <filename>\n");
2113 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2114 rname = talloc_asprintf_append(rname, "%s", buf);
2116 rname = talloc_asprintf_append(rname, "%s", lname);
2122 rname = client_clean_name(ctx, rname);
2129 /* allow '-' to represent stdin
2130 jdblair, 24.jun.98 */
2131 if (!file_exist_stat(lname, &st, false) &&
2132 (strcmp(lname,"-"))) {
2133 d_printf("%s does not exist\n",lname);
2138 return do_put(rname, lname, false);
2141 /*************************************
2142 File list structure.
2143 *************************************/
2145 static struct file_list {
2146 struct file_list *prev, *next;
2151 /****************************************************************************
2152 Free a file_list structure.
2153 ****************************************************************************/
2155 static void free_file_list (struct file_list *l_head)
2157 struct file_list *list, *next;
2159 for (list = l_head; list; list = next) {
2161 DLIST_REMOVE(l_head, list);
2166 /****************************************************************************
2167 Seek in a directory/file list until you get something that doesn't start with
2169 ****************************************************************************/
2171 static bool seek_list(struct file_list *list, char *name)
2174 trim_string(list->file_path,"./","\n");
2175 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2184 /****************************************************************************
2185 Set the file selection mask.
2186 ****************************************************************************/
2188 static int cmd_select(void)
2190 TALLOC_CTX *ctx = talloc_tos();
2191 char *new_fs = NULL;
2192 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2195 client_set_fileselection(new_fs);
2197 client_set_fileselection("");
2202 /****************************************************************************
2203 Recursive file matching function act as find
2204 match must be always set to true when calling this function
2205 ****************************************************************************/
2207 static int file_find(TALLOC_CTX *ctx,
2208 struct file_list **list,
2209 const char *directory,
2210 const char *expression,
2214 struct file_list *entry;
2215 struct stat statbuf;
2221 dir = opendir(directory);
2225 while ((dname = readdirname(dir))) {
2226 if (!strcmp("..", dname))
2228 if (!strcmp(".", dname))
2231 path = talloc_asprintf(ctx, "%s/%s", directory, dname);
2237 if (!match || !gen_fnmatch(expression, dname)) {
2239 ret = stat(path, &statbuf);
2241 if (S_ISDIR(statbuf.st_mode)) {
2243 ret = file_find(ctx,
2250 d_printf("file_find: cannot stat file %s\n", path);
2259 entry = talloc_zero(ctx, struct file_list);
2261 d_printf("Out of memory in file_find\n");
2265 entry->file_path = talloc_move(entry, &path);
2266 entry->isdir = isdir;
2267 DLIST_ADD(*list, entry);
2277 /****************************************************************************
2279 ****************************************************************************/
2281 static int cmd_mput(void)
2283 TALLOC_CTX *ctx = talloc_tos();
2286 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2288 struct file_list *temp_list;
2289 char *quest, *lname, *rname;
2293 ret = file_find(ctx, &file_list, ".", p, true);
2295 free_file_list(file_list);
2303 for (temp_list = file_list; temp_list;
2304 temp_list = temp_list->next) {
2307 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2310 trim_string(lname, "./", "/");
2312 /* check if it's a directory */
2313 if (temp_list->isdir) {
2314 /* if (!recurse) continue; */
2317 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2320 if (prompt && !yesno(quest)) { /* No */
2321 /* Skip the directory */
2322 lname[strlen(lname)-1] = '/';
2323 if (!seek_list(temp_list, lname))
2327 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2330 normalize_name(rname);
2333 client_clean_name(ctx, rname);
2334 if (tmp_rname == NULL) {
2338 rname = smb_xstrdup(tmp_rname);
2339 TALLOC_FREE(tmp_rname);
2340 if (rname == NULL) {
2344 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2346 DEBUG (0, ("Unable to make dir, skipping..."));
2347 /* Skip the directory */
2348 lname[strlen(lname)-1] = '/';
2349 if (!seek_list(temp_list, lname)) {
2357 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2360 if (prompt && !yesno(quest)) {
2367 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2372 normalize_name(rname);
2375 char *tmp_rname = client_clean_name(ctx, rname);
2376 if (tmp_rname == NULL) {
2380 rname = smb_xstrdup(tmp_rname);
2381 TALLOC_FREE(tmp_rname);
2382 if (rname == NULL) {
2386 do_put(rname, lname, false);
2388 free_file_list(file_list);
2397 /****************************************************************************
2399 ****************************************************************************/
2401 static int do_cancel(int job)
2403 if (cli_printjob_del(cli, job)) {
2404 d_printf("Job %d cancelled\n",job);
2407 NTSTATUS status = cli_nt_error(cli);
2408 d_printf("Error cancelling job %d : %s\n",
2409 job, nt_errstr(status));
2414 /****************************************************************************
2416 ****************************************************************************/
2418 static int cmd_cancel(void)
2420 TALLOC_CTX *ctx = talloc_tos();
2424 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2425 d_printf("cancel <jobid> ...\n");
2431 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2436 /****************************************************************************
2438 ****************************************************************************/
2440 static int cmd_print(void)
2442 TALLOC_CTX *ctx = talloc_tos();
2447 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2448 d_printf("print <filename>\n");
2452 rname = talloc_strdup(ctx, lname);
2456 p = strrchr_m(rname,'/');
2458 rname = talloc_asprintf(ctx,
2463 if (strequal(lname,"-")) {
2464 rname = talloc_asprintf(ctx,
2472 return do_put(rname, lname, false);
2475 /****************************************************************************
2476 Show a print queue entry.
2477 ****************************************************************************/
2479 static void queue_fn(struct print_job_info *p)
2481 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2484 /****************************************************************************
2486 ****************************************************************************/
2488 static int cmd_queue(void)
2490 cli_print_queue(cli, queue_fn);
2494 /****************************************************************************
2496 ****************************************************************************/
2498 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2501 TALLOC_CTX *ctx = talloc_tos();
2505 mask = talloc_asprintf(ctx,
2511 return NT_STATUS_NO_MEMORY;
2514 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2516 return NT_STATUS_OK;
2519 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2520 if (!NT_STATUS_IS_OK(status)) {
2521 d_printf("%s deleting remote file %s\n",
2522 nt_errstr(status), mask);
2528 /****************************************************************************
2530 ****************************************************************************/
2532 static int cmd_del(void)
2534 TALLOC_CTX *ctx = talloc_tos();
2537 NTSTATUS status = NT_STATUS_OK;
2538 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2541 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2544 mask = talloc_strdup(ctx, client_get_cur_dir());
2548 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2549 d_printf("del <filename>\n");
2552 mask = talloc_asprintf_append(mask, "%s", buf);
2556 mask = client_clean_name(ctx, mask);
2561 status = do_list(mask,attribute,do_del,false,false);
2562 if (!NT_STATUS_IS_OK(status)) {
2568 /****************************************************************************
2570 ****************************************************************************/
2572 static NTSTATUS delete_remote_files_list(struct cli_state *cli_state,
2573 struct file_list *flist)
2575 NTSTATUS status = NT_STATUS_OK;
2576 struct file_list *deltree_list_iter = NULL;
2578 for (deltree_list_iter = flist;
2579 deltree_list_iter != NULL;
2580 deltree_list_iter = deltree_list_iter->next) {
2581 if (CLI_DIRSEP_CHAR == '/') {
2583 status = cli_posix_unlink(cli_state,
2584 deltree_list_iter->file_path);
2585 } else if (deltree_list_iter->isdir) {
2586 status = cli_rmdir(cli_state,
2587 deltree_list_iter->file_path);
2589 status = cli_unlink(cli_state,
2590 deltree_list_iter->file_path,
2591 FILE_ATTRIBUTE_SYSTEM |
2592 FILE_ATTRIBUTE_HIDDEN);
2594 if (!NT_STATUS_IS_OK(status)) {
2595 d_printf("%s deleting remote %s %s\n",
2597 deltree_list_iter->isdir ?
2598 "directory" : "file",
2599 deltree_list_iter->file_path);
2603 return NT_STATUS_OK;
2606 /****************************************************************************
2607 Save a list of files to delete.
2608 ****************************************************************************/
2610 static struct file_list *deltree_list_head;
2612 static NTSTATUS do_deltree_list(struct cli_state *cli_state,
2613 struct file_info *finfo,
2616 struct file_list **file_list_head_pp = &deltree_list_head;
2617 struct file_list *dt = NULL;
2619 if (!do_this_one(finfo)) {
2620 return NT_STATUS_OK;
2623 /* skip if this is . or .. */
2624 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
2625 return NT_STATUS_OK;
2628 dt = talloc_zero(NULL, struct file_list);
2630 return NT_STATUS_NO_MEMORY;
2633 /* create absolute filename for cli_ntcreate() */
2634 dt->file_path = talloc_asprintf(dt,
2639 if (dt->file_path == NULL) {
2641 return NT_STATUS_NO_MEMORY;
2644 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2648 DLIST_ADD(*file_list_head_pp, dt);
2649 return NT_STATUS_OK;
2652 static int cmd_deltree(void)
2654 TALLOC_CTX *ctx = talloc_tos();
2656 NTSTATUS status = NT_STATUS_OK;
2657 struct file_list *deltree_list_norecurse = NULL;
2658 struct file_list *deltree_list_iter = NULL;
2659 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM |
2660 FILE_ATTRIBUTE_HIDDEN |
2661 FILE_ATTRIBUTE_DIRECTORY;
2663 char *mask = talloc_strdup(ctx, client_get_cur_dir());
2667 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
2669 d_printf("deltree <filename>\n");
2672 mask = talloc_asprintf_append(mask, "%s", buf);
2676 mask = client_clean_name(ctx, mask);
2681 deltree_list_head = NULL;
2684 * Get the list of directories to
2685 * delete (in case mask has a wildcard).
2687 status = do_list(mask, attribute, do_deltree_list, false, true);
2688 if (!NT_STATUS_IS_OK(status)) {
2691 deltree_list_norecurse = deltree_list_head;
2692 deltree_list_head = NULL;
2694 for (deltree_list_iter = deltree_list_norecurse;
2695 deltree_list_iter != NULL;
2696 deltree_list_iter = deltree_list_iter->next) {
2698 if (deltree_list_iter->isdir == false) {
2699 /* Just a regular file. */
2700 if (CLI_DIRSEP_CHAR == '/') {
2702 status = cli_posix_unlink(cli,
2703 deltree_list_iter->file_path);
2705 status = cli_unlink(cli,
2706 deltree_list_iter->file_path,
2707 FILE_ATTRIBUTE_SYSTEM |
2708 FILE_ATTRIBUTE_HIDDEN);
2710 if (!NT_STATUS_IS_OK(status)) {
2717 * Get the list of files or directories to
2718 * delete in depth order.
2720 status = do_list(deltree_list_iter->file_path,
2725 if (!NT_STATUS_IS_OK(status)) {
2728 status = delete_remote_files_list(cli, deltree_list_head);
2729 free_file_list(deltree_list_head);
2730 deltree_list_head = NULL;
2731 if (!NT_STATUS_IS_OK(status)) {
2736 free_file_list(deltree_list_norecurse);
2737 free_file_list(deltree_list_head);
2742 free_file_list(deltree_list_norecurse);
2743 free_file_list(deltree_list_head);
2744 deltree_list_head = NULL;
2749 /****************************************************************************
2750 Wildcard delete some files.
2751 ****************************************************************************/
2753 static int cmd_wdel(void)
2755 TALLOC_CTX *ctx = talloc_tos();
2759 struct cli_state *targetcli;
2760 char *targetname = NULL;
2763 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2764 d_printf("wdel 0x<attrib> <wcard>\n");
2768 attribute = (uint32_t)strtol(buf, (char **)NULL, 16);
2770 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2771 d_printf("wdel 0x<attrib> <wcard>\n");
2775 mask = talloc_asprintf(ctx, "%s%s",
2776 client_get_cur_dir(),
2781 mask = client_clean_name(ctx, mask);
2786 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2787 cli, mask, &targetcli, &targetname);
2788 if (!NT_STATUS_IS_OK(status)) {
2789 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2793 status = cli_unlink(targetcli, targetname, attribute);
2794 if (!NT_STATUS_IS_OK(status)) {
2795 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2801 /****************************************************************************
2802 ****************************************************************************/
2804 static int cmd_open(void)
2806 TALLOC_CTX *ctx = talloc_tos();
2809 char *targetname = NULL;
2810 struct cli_state *targetcli;
2811 uint16_t fnum = (uint16_t)-1;
2814 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2815 d_printf("open <filename>\n");
2818 mask = talloc_asprintf(ctx,
2820 client_get_cur_dir(),
2826 mask = client_clean_name(ctx, mask);
2831 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2832 cli, mask, &targetcli, &targetname);
2833 if (!NT_STATUS_IS_OK(status)) {
2834 d_printf("open %s: %s\n", mask, nt_errstr(status));
2838 status = cli_ntcreate(targetcli, targetname, 0,
2839 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2840 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2841 0x0, 0x0, &fnum, NULL);
2842 if (!NT_STATUS_IS_OK(status)) {
2843 status = cli_ntcreate(targetcli, targetname, 0,
2845 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2846 0x0, 0x0, &fnum, NULL);
2847 if (NT_STATUS_IS_OK(status)) {
2848 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2850 d_printf("Failed to open file %s. %s\n",
2851 targetname, nt_errstr(status));
2854 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2859 static int cmd_posix_encrypt(void)
2861 TALLOC_CTX *ctx = talloc_tos();
2862 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2863 char *domain = NULL;
2865 char *password = NULL;
2866 struct cli_credentials *creds = NULL;
2867 struct cli_credentials *lcreds = NULL;
2869 if (next_token_talloc(ctx, &cmd_ptr, &domain, NULL)) {
2871 if (!next_token_talloc(ctx, &cmd_ptr, &user, NULL)) {
2872 d_printf("posix_encrypt domain user password\n");
2876 if (!next_token_talloc(ctx, &cmd_ptr, &password, NULL)) {
2877 d_printf("posix_encrypt domain user password\n");
2881 lcreds = cli_session_creds_init(ctx,
2886 false, /* use_kerberos */
2887 false, /* fallback_after_kerberos */
2888 false, /* use_ccache */
2889 false); /* password_is_nt_hash */
2890 if (lcreds == NULL) {
2891 d_printf("cli_session_creds_init() failed.\n");
2896 bool auth_requested = false;
2898 creds = get_cmdline_auth_info_creds(
2899 popt_get_cmdline_auth_info());
2901 auth_requested = cli_credentials_authentication_requested(creds);
2902 if (!auth_requested) {
2903 d_printf("posix_encrypt domain user password\n");
2908 status = cli_smb1_setup_encryption(cli, creds);
2909 /* gensec currently references the creds so we can't free them here */
2910 talloc_unlink(ctx, lcreds);
2911 if (!NT_STATUS_IS_OK(status)) {
2912 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2914 d_printf("encryption on\n");
2921 /****************************************************************************
2922 ****************************************************************************/
2924 static int cmd_posix_open(void)
2926 TALLOC_CTX *ctx = talloc_tos();
2929 char *targetname = NULL;
2930 struct cli_state *targetcli;
2935 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2936 d_printf("posix_open <filename> 0<mode>\n");
2939 mask = talloc_asprintf(ctx,
2941 client_get_cur_dir(),
2946 mask = client_clean_name(ctx, mask);
2951 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2952 d_printf("posix_open <filename> 0<mode>\n");
2955 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2957 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2958 cli, mask, &targetcli, &targetname);
2959 if (!NT_STATUS_IS_OK(status)) {
2960 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2964 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2966 if (!NT_STATUS_IS_OK(status)) {
2967 status = cli_posix_open(targetcli, targetname,
2968 O_CREAT|O_RDONLY, mode, &fnum);
2969 if (!NT_STATUS_IS_OK(status)) {
2970 d_printf("Failed to open file %s. %s\n", targetname,
2973 d_printf("posix_open file %s: for readonly fnum %d\n",
2977 d_printf("posix_open file %s: for read/write fnum %d\n",
2984 static int cmd_posix_mkdir(void)
2986 TALLOC_CTX *ctx = talloc_tos();
2989 char *targetname = NULL;
2990 struct cli_state *targetcli;
2994 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2995 d_printf("posix_mkdir <filename> 0<mode>\n");
2998 mask = talloc_asprintf(ctx,
3000 client_get_cur_dir(),
3005 mask = client_clean_name(ctx, mask);
3010 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3011 d_printf("posix_mkdir <filename> 0<mode>\n");
3014 mode = (mode_t)strtol(buf, (char **)NULL, 8);
3016 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3017 cli, mask, &targetcli, &targetname);
3018 if (!NT_STATUS_IS_OK(status)) {
3019 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
3023 status = cli_posix_mkdir(targetcli, targetname, mode);
3024 if (!NT_STATUS_IS_OK(status)) {
3025 d_printf("Failed to open file %s. %s\n",
3026 targetname, nt_errstr(status));
3028 d_printf("posix_mkdir created directory %s\n", targetname);
3033 static int cmd_posix_unlink(void)
3035 TALLOC_CTX *ctx = talloc_tos();
3038 char *targetname = NULL;
3039 struct cli_state *targetcli;
3042 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3043 d_printf("posix_unlink <filename>\n");
3046 mask = talloc_asprintf(ctx,
3048 client_get_cur_dir(),
3053 mask = client_clean_name(ctx, mask);
3058 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3059 cli, mask, &targetcli, &targetname);
3060 if (!NT_STATUS_IS_OK(status)) {
3061 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
3065 status = cli_posix_unlink(targetcli, targetname);
3066 if (!NT_STATUS_IS_OK(status)) {
3067 d_printf("Failed to unlink file %s. %s\n",
3068 targetname, nt_errstr(status));
3070 d_printf("posix_unlink deleted file %s\n", targetname);
3076 static int cmd_posix_rmdir(void)
3078 TALLOC_CTX *ctx = talloc_tos();
3081 char *targetname = NULL;
3082 struct cli_state *targetcli;
3085 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3086 d_printf("posix_rmdir <filename>\n");
3089 mask = talloc_asprintf(ctx,
3091 client_get_cur_dir(),
3096 mask = client_clean_name(ctx, mask);
3101 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3102 cli, mask, &targetcli, &targetname);
3103 if (!NT_STATUS_IS_OK(status)) {
3104 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
3108 status = cli_posix_rmdir(targetcli, targetname);
3109 if (!NT_STATUS_IS_OK(status)) {
3110 d_printf("Failed to unlink directory %s. %s\n",
3111 targetname, nt_errstr(status));
3113 d_printf("posix_rmdir deleted directory %s\n", targetname);
3119 static int cmd_close(void)
3121 TALLOC_CTX *ctx = talloc_tos();
3126 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3127 d_printf("close <fnum>\n");
3132 /* We really should use the targetcli here.... */
3133 status = cli_close(cli, fnum);
3134 if (!NT_STATUS_IS_OK(status)) {
3135 d_printf("close %d: %s\n", fnum, nt_errstr(status));
3141 static int cmd_posix(void)
3143 TALLOC_CTX *ctx = talloc_tos();
3144 uint16_t major, minor;
3145 uint32_t caplow, caphigh;
3149 if (!SERVER_HAS_UNIX_CIFS(cli)) {
3150 d_printf("Server doesn't support UNIX CIFS extensions.\n");
3154 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
3156 if (!NT_STATUS_IS_OK(status)) {
3157 d_printf("Can't get UNIX CIFS extensions version from "
3158 "server: %s\n", nt_errstr(status));
3162 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
3164 caps = talloc_strdup(ctx, "");
3168 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
3169 caps = talloc_asprintf_append(caps, "locks ");
3174 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
3175 caps = talloc_asprintf_append(caps, "acls ");
3180 if (caplow & CIFS_UNIX_XATTTR_CAP) {
3181 caps = talloc_asprintf_append(caps, "eas ");
3186 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3187 caps = talloc_asprintf_append(caps, "pathnames ");
3192 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
3193 caps = talloc_asprintf_append(caps, "posix_path_operations ");
3198 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
3199 caps = talloc_asprintf_append(caps, "large_read ");
3204 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
3205 caps = talloc_asprintf_append(caps, "large_write ");
3210 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
3211 caps = talloc_asprintf_append(caps, "posix_encrypt ");
3216 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
3217 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
3223 if (*caps && caps[strlen(caps)-1] == ' ') {
3224 caps[strlen(caps)-1] = '\0';
3227 d_printf("Server supports CIFS capabilities %s\n", caps);
3229 status = cli_set_unix_extensions_capabilities(cli, major, minor,
3231 if (!NT_STATUS_IS_OK(status)) {
3232 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
3237 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3238 CLI_DIRSEP_CHAR = '/';
3239 *CLI_DIRSEP_STR = '/';
3240 client_set_cur_dir(CLI_DIRSEP_STR);
3246 static int cmd_lock(void)
3248 TALLOC_CTX *ctx = talloc_tos();
3250 uint64_t start, len;
3251 enum brl_type lock_type;
3255 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3256 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3261 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3262 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3266 if (*buf == 'r' || *buf == 'R') {
3267 lock_type = READ_LOCK;
3268 } else if (*buf == 'w' || *buf == 'W') {
3269 lock_type = WRITE_LOCK;
3271 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3275 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3276 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3280 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3282 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3283 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3287 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3289 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
3290 if (!NT_STATUS_IS_OK(status)) {
3291 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
3297 static int cmd_unlock(void)
3299 TALLOC_CTX *ctx = talloc_tos();
3301 uint64_t start, len;
3305 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3306 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3311 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3312 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3316 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3318 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3319 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3323 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3325 status = cli_posix_unlock(cli, fnum, start, len);
3326 if (!NT_STATUS_IS_OK(status)) {
3327 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
3333 static int cmd_posix_whoami(void)
3335 TALLOC_CTX *ctx = talloc_tos();
3336 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
3339 uint32_t num_gids = 0;
3340 uint32_t num_sids = 0;
3341 uint64_t *gids = NULL;
3342 struct dom_sid *sids = NULL;
3346 status = cli_posix_whoami(cli,
3356 if (!NT_STATUS_IS_OK(status)) {
3357 d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
3361 d_printf("GUEST:%s\n", guest ? "True" : "False");
3362 d_printf("UID:%" PRIu64 "\n", uid);
3363 d_printf("GID:%" PRIu64 "\n", gid);
3364 d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
3365 for (i = 0; i < num_gids; i++) {
3366 d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
3368 d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
3369 for (i = 0; i < num_sids; i++) {
3370 struct dom_sid_buf buf;
3371 d_printf("SIDS[%" PRIu32 "]:%s\n",
3373 dom_sid_str_buf(&sids[i], &buf));
3379 /****************************************************************************
3381 ****************************************************************************/
3383 static int cmd_rmdir(void)
3385 TALLOC_CTX *ctx = talloc_tos();
3388 char *targetname = NULL;
3389 struct cli_state *targetcli;
3392 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3393 d_printf("rmdir <dirname>\n");
3396 mask = talloc_asprintf(ctx,
3398 client_get_cur_dir(),
3403 mask = client_clean_name(ctx, mask);
3408 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3409 cli, mask, &targetcli, &targetname);
3410 if (!NT_STATUS_IS_OK(status)) {
3411 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3415 status = cli_rmdir(targetcli, targetname);
3416 if (!NT_STATUS_IS_OK(status)) {
3417 d_printf("%s removing remote directory file %s\n",
3418 nt_errstr(status), mask);
3424 /****************************************************************************
3426 ****************************************************************************/
3428 static int cmd_link(void)
3430 TALLOC_CTX *ctx = talloc_tos();
3431 char *oldname = NULL;
3432 char *newname = NULL;
3435 char *targetname = NULL;
3436 struct cli_state *targetcli;
3439 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3440 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3441 d_printf("link <oldname> <newname>\n");
3444 oldname = talloc_asprintf(ctx,
3446 client_get_cur_dir(),
3451 oldname = client_clean_name(ctx, oldname);
3452 if (oldname == NULL) {
3455 newname = talloc_asprintf(ctx,
3457 client_get_cur_dir(),
3462 newname = client_clean_name(ctx, newname);
3463 if (newname == NULL) {
3467 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3468 cli, oldname, &targetcli, &targetname);
3469 if (!NT_STATUS_IS_OK(status)) {
3470 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3474 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3475 d_printf("Server doesn't support UNIX CIFS calls.\n");
3479 status = cli_posix_hardlink(targetcli, targetname, newname);
3480 if (!NT_STATUS_IS_OK(status)) {
3481 d_printf("%s linking files (%s -> %s)\n",
3482 nt_errstr(status), newname, oldname);
3488 /****************************************************************************
3490 ****************************************************************************/
3492 static int cmd_readlink(void)
3494 TALLOC_CTX *ctx = talloc_tos();
3497 char *targetname = NULL;
3498 char *linkname = NULL;
3499 struct cli_state *targetcli;
3502 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3503 d_printf("readlink <name>\n");
3506 name = talloc_asprintf(ctx,
3508 client_get_cur_dir(),
3513 name = client_clean_name(ctx, name);
3518 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3519 cli, name, &targetcli, &targetname);
3520 if (!NT_STATUS_IS_OK(status)) {
3521 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3525 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3526 d_printf("Server doesn't support UNIX CIFS calls.\n");
3530 status = cli_posix_readlink(targetcli, name, talloc_tos(), &linkname);
3531 if (!NT_STATUS_IS_OK(status)) {
3532 d_printf("%s readlink on file %s\n",
3533 nt_errstr(status), name);
3537 d_printf("%s -> %s\n", name, linkname);
3539 TALLOC_FREE(linkname);
3545 /****************************************************************************
3547 ****************************************************************************/
3549 static int cmd_symlink(void)
3551 TALLOC_CTX *ctx = talloc_tos();
3552 char *link_target = NULL;
3553 char *newname = NULL;
3556 struct cli_state *newcli;
3559 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3560 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3561 d_printf("symlink <link_target> <newname>\n");
3564 /* Oldname (link target) must be an untouched blob. */
3567 if (SERVER_HAS_UNIX_CIFS(cli)) {
3568 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3573 newname = client_clean_name(ctx, newname);
3574 if (newname == NULL) {
3577 /* New name must be present in share namespace. */
3578 status = cli_resolve_path(ctx, "",
3579 popt_get_cmdline_auth_info(), cli, newname,
3581 if (!NT_STATUS_IS_OK(status)) {
3582 d_printf("link %s: %s\n", newname,
3586 status = cli_posix_symlink(newcli, link_target, newname);
3588 status = cli_symlink(
3589 cli, link_target, buf2,
3590 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3593 if (!NT_STATUS_IS_OK(status)) {
3594 d_printf("%s symlinking files (%s -> %s)\n",
3595 nt_errstr(status), newname, link_target);
3602 /****************************************************************************
3604 ****************************************************************************/
3606 static int cmd_chmod(void)
3608 TALLOC_CTX *ctx = talloc_tos();
3612 char *targetname = NULL;
3613 struct cli_state *targetcli;
3617 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3618 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3619 d_printf("chmod mode file\n");
3622 src = talloc_asprintf(ctx,
3624 client_get_cur_dir(),
3629 src = client_clean_name(ctx, src);
3634 mode = (mode_t)strtol(buf, NULL, 8);
3636 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3637 cli, src, &targetcli, &targetname);
3638 if (!NT_STATUS_IS_OK(status)) {
3639 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3643 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3644 d_printf("Server doesn't support UNIX CIFS calls.\n");
3648 status = cli_posix_chmod(targetcli, targetname, mode);
3649 if (!NT_STATUS_IS_OK(status)) {
3650 d_printf("%s chmod file %s 0%o\n",
3651 nt_errstr(status), src, (unsigned int)mode);
3658 static const char *filetype_to_str(mode_t mode)
3660 if (S_ISREG(mode)) {
3661 return "regular file";
3662 } else if (S_ISDIR(mode)) {
3666 if (S_ISCHR(mode)) {
3667 return "character device";
3671 if (S_ISBLK(mode)) {
3672 return "block device";
3676 if (S_ISFIFO(mode)) {
3681 if (S_ISLNK(mode)) {
3682 return "symbolic link";
3686 if (S_ISSOCK(mode)) {
3693 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3702 static char *unix_mode_to_str(char *s, mode_t m)
3705 const char *str = filetype_to_str(m);
3721 *p++ = str[1] == 'y' ? 'l' : 's';
3728 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3729 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3730 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3731 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3732 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3733 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3734 *p++ = rwx_to_str(m, S_IROTH, 'r');
3735 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3736 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3741 /****************************************************************************
3742 Utility function for UNIX getfacl.
3743 ****************************************************************************/
3745 static char *perms_to_string(fstring permstr, unsigned char perms)
3747 fstrcpy(permstr, "---");
3748 if (perms & SMB_POSIX_ACL_READ) {
3751 if (perms & SMB_POSIX_ACL_WRITE) {
3754 if (perms & SMB_POSIX_ACL_EXECUTE) {
3760 /****************************************************************************
3762 ****************************************************************************/
3764 static int cmd_getfacl(void)
3766 TALLOC_CTX *ctx = talloc_tos();
3769 char *targetname = NULL;
3770 struct cli_state *targetcli;
3771 uint16_t major, minor;
3772 uint32_t caplow, caphigh;
3773 char *retbuf = NULL;
3775 SMB_STRUCT_STAT sbuf;
3776 uint16_t num_file_acls = 0;
3777 uint16_t num_dir_acls = 0;
3781 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3782 d_printf("getfacl filename\n");
3785 src = talloc_asprintf(ctx,
3787 client_get_cur_dir(),
3792 src = client_clean_name(ctx, src);
3797 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3798 cli, src, &targetcli, &targetname);
3799 if (!NT_STATUS_IS_OK(status)) {
3800 d_printf("stat %s: %s\n", src, nt_errstr(status));
3804 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3805 d_printf("Server doesn't support UNIX CIFS calls.\n");
3809 status = cli_unix_extensions_version(targetcli, &major, &minor,
3811 if (!NT_STATUS_IS_OK(status)) {
3812 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3817 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3818 d_printf("This server supports UNIX extensions "
3819 "but doesn't support POSIX ACLs.\n");
3823 status = cli_posix_stat(targetcli, targetname, &sbuf);
3824 if (!NT_STATUS_IS_OK(status)) {
3825 d_printf("%s getfacl doing a stat on file %s\n",
3826 nt_errstr(status), src);
3830 status = cli_posix_getacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3831 if (!NT_STATUS_IS_OK(status)) {
3832 d_printf("%s getfacl file %s\n",
3833 nt_errstr(status), src);
3837 /* ToDo : Print out the ACL values. */
3838 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3839 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3840 src, (unsigned int)CVAL(retbuf,0) );
3844 num_file_acls = SVAL(retbuf,2);
3845 num_dir_acls = SVAL(retbuf,4);
3846 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3847 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3849 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3850 (unsigned int)rb_size);
3854 d_printf("# file: %s\n", src);
3855 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3857 if (num_file_acls == 0 && num_dir_acls == 0) {
3858 d_printf("No acls found.\n");
3861 for (i = 0; i < num_file_acls; i++) {
3864 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3865 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3868 case SMB_POSIX_ACL_USER_OBJ:
3871 case SMB_POSIX_ACL_USER:
3872 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3873 d_printf("user:%u:", uorg);
3875 case SMB_POSIX_ACL_GROUP_OBJ:
3876 d_printf("group::");
3878 case SMB_POSIX_ACL_GROUP:
3879 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3880 d_printf("group:%u:", uorg);
3882 case SMB_POSIX_ACL_MASK:
3885 case SMB_POSIX_ACL_OTHER:
3886 d_printf("other::");
3889 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3890 src, (unsigned int)tagtype );
3895 d_printf("%s\n", perms_to_string(permstring, perms));
3898 for (i = 0; i < num_dir_acls; i++) {
3901 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3902 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3905 case SMB_POSIX_ACL_USER_OBJ:
3906 d_printf("default:user::");
3908 case SMB_POSIX_ACL_USER:
3909 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3910 d_printf("default:user:%u:", uorg);
3912 case SMB_POSIX_ACL_GROUP_OBJ:
3913 d_printf("default:group::");
3915 case SMB_POSIX_ACL_GROUP:
3916 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3917 d_printf("default:group:%u:", uorg);
3919 case SMB_POSIX_ACL_MASK:
3920 d_printf("default:mask::");
3922 case SMB_POSIX_ACL_OTHER:
3923 d_printf("default:other::");
3926 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3927 src, (unsigned int)tagtype );
3932 d_printf("%s\n", perms_to_string(permstring, perms));
3938 /****************************************************************************
3939 Get the EA list of a file
3940 ****************************************************************************/
3942 static int cmd_geteas(void)
3944 TALLOC_CTX *ctx = talloc_tos();
3947 char *targetname = NULL;
3948 struct cli_state *targetcli;
3951 struct ea_struct *eas;
3953 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3954 d_printf("geteas filename\n");
3957 src = talloc_asprintf(ctx,
3959 client_get_cur_dir(),
3964 src = client_clean_name(ctx, src);
3969 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3970 cli, src, &targetcli, &targetname);
3971 if (!NT_STATUS_IS_OK(status)) {
3972 d_printf("stat %s: %s\n", src, nt_errstr(status));
3976 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3978 if (!NT_STATUS_IS_OK(status)) {
3979 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3983 for (i=0; i<num_eas; i++) {
3984 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3985 dump_data_file(eas[i].value.data, eas[i].value.length, false,
3995 /****************************************************************************
3997 ****************************************************************************/
3999 static int cmd_setea(void)
4001 TALLOC_CTX *ctx = talloc_tos();
4004 char *eaname = NULL;
4005 char *eavalue = NULL;
4006 char *targetname = NULL;
4007 struct cli_state *targetcli;
4010 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
4011 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
4012 d_printf("setea filename eaname value\n");
4015 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
4016 eavalue = talloc_strdup(ctx, "");
4018 src = talloc_asprintf(ctx,
4020 client_get_cur_dir(),
4025 src = client_clean_name(ctx, src);
4030 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
4031 cli, src, &targetcli, &targetname);
4032 if (!NT_STATUS_IS_OK(status)) {
4033 d_printf("stat %s: %s\n", src, nt_errstr(status));
4037 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
4039 if (!NT_STATUS_IS_OK(status)) {
4040 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
4047 /****************************************************************************
4049 ****************************************************************************/
4051 static int cmd_stat(void)
4053 TALLOC_CTX *ctx = talloc_tos();
4056 char *targetname = NULL;
4057 struct cli_state *targetcli;
4059 SMB_STRUCT_STAT sbuf;
4064 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
4065 d_printf("stat file\n");
4068 src = talloc_asprintf(ctx,
4070 client_get_cur_dir(),
4075 src = client_clean_name(ctx, src);
4080 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
4081 cli, src, &targetcli, &targetname);
4082 if (!NT_STATUS_IS_OK(status)) {
4083 d_printf("stat %s: %s\n", src, nt_errstr(status));
4087 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4088 d_printf("Server doesn't support UNIX CIFS calls.\n");
4092 status = cli_posix_stat(targetcli, targetname, &sbuf);
4093 if (!NT_STATUS_IS_OK(status)) {
4094 d_printf("%s stat file %s\n",
4095 nt_errstr(status), src);
4099 /* Print out the stat values. */
4100 d_printf("File: %s\n", src);
4101 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
4102 (double)sbuf.st_ex_size,
4103 (unsigned int)sbuf.st_ex_blocks,
4104 filetype_to_str(sbuf.st_ex_mode));
4106 #if defined(S_ISCHR) && defined(S_ISBLK)
4107 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
4108 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
4109 (double)sbuf.st_ex_ino,
4110 (unsigned int)sbuf.st_ex_nlink,
4111 unix_dev_major(sbuf.st_ex_rdev),
4112 unix_dev_minor(sbuf.st_ex_rdev));
4115 d_printf("Inode: %.0f\tLinks: %u\n",
4116 (double)sbuf.st_ex_ino,
4117 (unsigned int)sbuf.st_ex_nlink);
4119 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
4120 ((int)sbuf.st_ex_mode & 0777),
4121 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
4122 (unsigned int)sbuf.st_ex_uid,
4123 (unsigned int)sbuf.st_ex_gid);
4125 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
4126 lt = localtime(&tmp_time);
4128 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4130 fstrcpy(mode_str, "unknown");
4132 d_printf("Access: %s\n", mode_str);
4134 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4135 lt = localtime(&tmp_time);
4137 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4139 fstrcpy(mode_str, "unknown");
4141 d_printf("Modify: %s\n", mode_str);
4143 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
4144 lt = localtime(&tmp_time);
4146 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4148 fstrcpy(mode_str, "unknown");
4150 d_printf("Change: %s\n", mode_str);
4156 /****************************************************************************
4158 ****************************************************************************/
4160 static int cmd_chown(void)
4162 TALLOC_CTX *ctx = talloc_tos();
4166 char *buf, *buf2, *buf3;
4167 struct cli_state *targetcli;
4168 char *targetname = NULL;
4171 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4172 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
4173 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
4174 d_printf("chown uid gid file\n");
4178 uid = (uid_t)atoi(buf);
4179 gid = (gid_t)atoi(buf2);
4181 src = talloc_asprintf(ctx,
4183 client_get_cur_dir(),
4188 src = client_clean_name(ctx, src);
4192 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
4193 cli, src, &targetcli, &targetname);
4194 if (!NT_STATUS_IS_OK(status)) {
4195 d_printf("chown %s: %s\n", src, nt_errstr(status));
4199 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4200 d_printf("Server doesn't support UNIX CIFS calls.\n");
4204 status = cli_posix_chown(targetcli, targetname, uid, gid);
4205 if (!NT_STATUS_IS_OK(status)) {
4206 d_printf("%s chown file %s uid=%d, gid=%d\n",
4207 nt_errstr(status), src, (int)uid, (int)gid);
4214 /****************************************************************************
4216 ****************************************************************************/
4218 static int cmd_rename(void)
4220 TALLOC_CTX *ctx = talloc_tos();
4223 struct cli_state *targetcli;
4227 bool replace = false;
4229 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4230 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4231 d_printf("rename <src> <dest> [-f]\n");
4235 src = talloc_asprintf(ctx,
4237 client_get_cur_dir(),
4242 src = client_clean_name(ctx, src);
4247 dest = talloc_asprintf(ctx,
4249 client_get_cur_dir(),
4254 dest = client_clean_name(ctx, dest);
4259 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
4260 strcsequal(buf, "-f")) {
4264 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
4265 cli, src, &targetcli, &targetsrc);
4266 if (!NT_STATUS_IS_OK(status)) {
4267 d_printf("rename %s: %s\n", src, nt_errstr(status));
4271 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
4272 cli, dest, &targetcli, &targetdest);
4273 if (!NT_STATUS_IS_OK(status)) {
4274 d_printf("rename %s: %s\n", dest, nt_errstr(status));
4278 status = cli_rename(targetcli, targetsrc, targetdest, replace);
4279 if (!NT_STATUS_IS_OK(status)) {
4280 d_printf("%s renaming files %s -> %s \n",
4290 struct scopy_timing {
4291 struct timespec tp_start;
4294 static int scopy_status(off_t written, void *priv)
4296 struct timespec tp_end;
4297 unsigned int scopy_total_time_ms;
4298 struct scopy_timing *st = priv;
4300 clock_gettime_mono(&tp_end);
4301 scopy_total_time_ms = nsec_time_diff(&tp_end,&st->tp_start)/1000000;
4303 DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
4304 (intmax_t)written, written / (1.024*scopy_total_time_ms)));
4309 /****************************************************************************
4310 Server-Side copy some file.
4311 ****************************************************************************/
4313 static int cmd_scopy(void)
4315 TALLOC_CTX *ctx = talloc_tos();
4318 struct cli_state *targetcli;
4321 uint32_t DesiredAccess, ShareAccess, CreateDisposition, CreateOptions;
4322 struct smb_create_returns cr;
4323 uint16_t destfnum = (uint16_t)-1;
4324 uint16_t srcfnum = (uint16_t)-1;
4326 struct scopy_timing st;
4330 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4331 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4332 d_printf("scopy <src> <dest>\n");
4336 src = talloc_asprintf(ctx,
4338 client_get_cur_dir(),
4343 src = client_clean_name(ctx, src);
4348 dest = talloc_asprintf(ctx,
4350 client_get_cur_dir(),
4355 dest = client_clean_name(ctx, dest);
4360 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
4361 cli, src, &targetcli, &targetsrc);
4362 if (!NT_STATUS_IS_OK(status)) {
4363 d_printf("scopy %s: %s\n", src, nt_errstr(status));
4367 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
4368 cli, dest, &targetcli, &targetdest);
4369 if (!NT_STATUS_IS_OK(status)) {
4370 d_printf("scopy %s: %s\n", dest, nt_errstr(status));
4375 DesiredAccess = (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES|
4376 READ_CONTROL_ACCESS|SYNCHRONIZE_ACCESS);
4377 ShareAccess = FILE_SHARE_READ|FILE_SHARE_DELETE;
4378 CreateDisposition = FILE_OPEN;
4379 CreateOptions = (FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE|
4380 FILE_OPEN_REPARSE_POINT);
4381 status = cli_ntcreate(targetcli, targetsrc, 0, DesiredAccess, 0,
4382 ShareAccess, CreateDisposition, CreateOptions, 0x0,
4384 if (!NT_STATUS_IS_OK(status)) {
4385 d_printf("Failed to open file %s. %s\n",
4386 targetsrc, nt_errstr(status));
4390 DesiredAccess = (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_READ_EA|
4391 FILE_WRITE_EA|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|
4392 DELETE_ACCESS|READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|SYNCHRONIZE_ACCESS);
4393 ShareAccess = FILE_SHARE_NONE;
4394 CreateDisposition = FILE_CREATE;
4395 CreateOptions = FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE;
4396 status = cli_ntcreate(targetcli, targetdest, 0, DesiredAccess,
4397 FILE_ATTRIBUTE_ARCHIVE, ShareAccess, CreateDisposition,
4398 CreateOptions, 0x0, &destfnum, NULL);
4399 if (!NT_STATUS_IS_OK(status)) {
4400 d_printf("Failed to create file %s. %s\n",
4401 targetdest, nt_errstr(status));
4402 cli_close(targetcli, srcfnum);
4406 clock_gettime_mono(&st.tp_start);
4407 status = cli_splice(targetcli, targetcli, srcfnum, destfnum,
4408 cr.end_of_file, 0, 0, &written, scopy_status, &st);
4409 if (!NT_STATUS_IS_OK(status)) {
4410 d_printf("%s copying file %s -> %s \n",
4417 status = cli_close(targetcli, srcfnum);
4418 if (!NT_STATUS_IS_OK(status)) {
4419 d_printf("Error %s closing remote source file\n", nt_errstr(status));
4422 status = cli_close(targetcli, destfnum);
4423 if (!NT_STATUS_IS_OK(status)) {
4424 d_printf("Error %s closing remote dest file\n", nt_errstr(status));
4431 /****************************************************************************
4432 Print the volume name.
4433 ****************************************************************************/
4435 static int cmd_volume(void)
4438 uint32_t serial_num;
4442 status = cli_get_fs_volume_info(cli, talloc_tos(),
4443 &volname, &serial_num,
4445 if (!NT_STATUS_IS_OK(status)) {
4446 d_printf("Error %s getting volume info\n", nt_errstr(status));
4450 d_printf("Volume: |%s| serial number 0x%x\n",
4451 volname, (unsigned int)serial_num);
4455 /****************************************************************************
4456 Hard link files using the NT call.
4457 ****************************************************************************/
4459 static int cmd_hardlink(void)
4461 TALLOC_CTX *ctx = talloc_tos();
4464 struct cli_state *targetcli;
4468 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4469 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4470 d_printf("hardlink <src> <dest>\n");
4474 src = talloc_asprintf(ctx,
4476 client_get_cur_dir(),
4481 src = client_clean_name(ctx, src);
4486 dest = talloc_asprintf(ctx,
4488 client_get_cur_dir(),
4493 dest = client_clean_name(ctx, dest);
4498 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
4499 cli, src, &targetcli, &targetname);
4500 if (!NT_STATUS_IS_OK(status)) {
4501 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
4505 status = cli_hardlink(targetcli, targetname, dest);
4506 if (!NT_STATUS_IS_OK(status)) {
4507 d_printf("%s doing an NT hard link of files\n",
4515 /****************************************************************************
4516 Toggle the prompt flag.
4517 ****************************************************************************/
4519 static int cmd_prompt(void)
4522 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
4526 /****************************************************************************
4527 Set the newer than time.
4528 ****************************************************************************/
4530 static int cmd_newer(void)
4532 TALLOC_CTX *ctx = talloc_tos();
4535 SMB_STRUCT_STAT sbuf;
4537 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
4538 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
4539 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4540 DEBUG(1,("Getting files newer than %s",
4541 time_to_asc(newer_than)));
4546 if (ok && newer_than == 0) {
4547 d_printf("Error setting newer-than time\n");
4554 /****************************************************************************
4555 Watch directory changes
4556 ****************************************************************************/
4558 static int cmd_notify(void)
4560 TALLOC_CTX *frame = talloc_stackframe();
4565 name = talloc_strdup(talloc_tos(), client_get_cur_dir());
4569 if (!next_token_talloc(talloc_tos(), &cmd_ptr, &buf, NULL)) {
4572 name = talloc_asprintf_append(name, "%s", buf);
4576 name = client_clean_name(talloc_tos(), name);
4580 status = cli_ntcreate(
4581 cli, name, 0, FILE_READ_DATA, 0,
4582 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4583 FILE_OPEN, 0, 0, &fnum, NULL);
4584 if (!NT_STATUS_IS_OK(status)) {
4585 d_printf("Could not open file: %s\n", nt_errstr(status));
4591 uint32_t num_changes = 0;
4592 struct notify_change *changes = NULL;
4594 status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
4596 talloc_tos(), &num_changes, &changes);
4597 if (NT_STATUS_EQUAL(status, STATUS_NOTIFY_ENUM_DIR)) {
4598 printf("NOTIFY_ENUM_DIR\n");
4599 status = NT_STATUS_OK;
4601 if (!NT_STATUS_IS_OK(status)) {
4602 d_printf("notify returned %s\n",
4606 for (i=0; i<num_changes; i++) {
4607 printf("%4.4x %s\n", changes[i].action,
4610 TALLOC_FREE(changes);
4613 d_printf("notify <dir name>\n");
4619 /****************************************************************************
4620 Set the archive level.
4621 ****************************************************************************/
4623 static int cmd_archive(void)
4625 TALLOC_CTX *ctx = talloc_tos();
4628 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4629 archive_level = atoi(buf);
4631 d_printf("Archive level is %d\n",archive_level);
4637 /****************************************************************************
4638 Toggle the backup_intent state.
4639 ****************************************************************************/
4641 static int cmd_backup(void)
4643 backup_intent = !backup_intent;
4644 cli_set_backup_intent(cli, backup_intent);
4645 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4649 /****************************************************************************
4650 Toggle the lowercaseflag.
4651 ****************************************************************************/
4653 static int cmd_lowercase(void)
4655 lowercase = !lowercase;
4656 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4660 /****************************************************************************
4661 Toggle the case sensitive flag.
4662 ****************************************************************************/
4664 static int cmd_setcase(void)
4666 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4668 cli_set_case_sensitive(cli, !orig_case_sensitive);
4669 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4674 /****************************************************************************
4675 Toggle the showacls flag.
4676 ****************************************************************************/
4678 static int cmd_showacls(void)
4680 showacls = !showacls;
4681 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4686 /****************************************************************************
4687 Toggle the recurse flag.
4688 ****************************************************************************/
4690 static int cmd_recurse(void)
4693 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4697 /****************************************************************************
4698 Toggle the translate flag.
4699 ****************************************************************************/
4701 static int cmd_translate(void)
4703 translation = !translation;
4704 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4705 translation?"on":"off"));
4709 /****************************************************************************
4711 ****************************************************************************/
4713 static int cmd_lcd(void)
4715 TALLOC_CTX *ctx = talloc_tos();
4719 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4720 if (chdir(buf) == -1) {
4721 d_printf("chdir to %s failed (%s)\n",
4722 buf, strerror(errno));
4729 DEBUG(2,("the local directory is now %s\n",d));
4734 /****************************************************************************
4735 Get a file restarting at end of local file.
4736 ****************************************************************************/
4738 static int cmd_reget(void)
4740 TALLOC_CTX *ctx = talloc_tos();
4741 char *local_name = NULL;
4742 char *remote_name = NULL;
4746 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4751 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4752 d_printf("reget <filename>\n");
4755 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4759 remote_name = client_clean_name(ctx,remote_name);
4765 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4770 return do_get(remote_name, local_name, true);
4773 /****************************************************************************
4774 Put a file restarting at end of local file.
4775 ****************************************************************************/
4777 static int cmd_reput(void)
4779 TALLOC_CTX *ctx = talloc_tos();
4780 char *local_name = NULL;
4781 char *remote_name = NULL;
4785 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4790 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4791 d_printf("reput <filename>\n");
4795 if (!file_exist_stat(local_name, &st, false)) {
4796 d_printf("%s does not exist\n", local_name);
4800 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4801 remote_name = talloc_asprintf_append(remote_name,
4804 remote_name = talloc_asprintf_append(remote_name,
4811 remote_name = client_clean_name(ctx, remote_name);
4816 return do_put(remote_name, local_name, true);
4819 /****************************************************************************
4821 ****************************************************************************/
4823 static void browse_fn(const char *name, uint32_t m,
4824 const char *comment, void *state)
4826 const char *typestr = "";
4829 case STYPE_DISKTREE:
4833 typestr = "Printer";
4842 /* FIXME: If the remote machine returns non-ascii characters
4843 in any of these fields, they can corrupt the output. We
4844 should remove them. */
4846 d_printf("\t%-15s %-10.10s%s\n",
4847 name,typestr,comment);
4849 d_printf ("%s|%s|%s\n",typestr,name,comment);
4853 static bool browse_host_rpc(bool sort)
4856 struct rpc_pipe_client *pipe_hnd = NULL;
4857 TALLOC_CTX *frame = talloc_stackframe();
4859 struct srvsvc_NetShareInfoCtr info_ctr;
4860 struct srvsvc_NetShareCtr1 ctr1;
4861 uint32_t resume_handle = 0;
4862 uint32_t total_entries = 0;
4864 struct dcerpc_binding_handle *b;
4866 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
4869 if (!NT_STATUS_IS_OK(status)) {
4870 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4871 nt_errstr(status)));
4876 b = pipe_hnd->binding_handle;
4878 ZERO_STRUCT(info_ctr);
4882 info_ctr.ctr.ctr1 = &ctr1;
4884 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4892 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4893 TALLOC_FREE(pipe_hnd);
4898 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4899 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4900 browse_fn(info.name, info.type, info.comment, NULL);
4903 TALLOC_FREE(pipe_hnd);
4908 /****************************************************************************
4909 Try and browse available connections on a host.
4910 ****************************************************************************/
4912 static bool browse_host(bool sort)
4917 d_printf("\n\tSharename Type Comment\n");
4918 d_printf("\t--------- ---- -------\n");
4921 if (browse_host_rpc(sort)) {
4925 if (smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1) {
4929 ret = cli_RNetShareEnum(cli, browse_fn, NULL);
4931 NTSTATUS status = cli_nt_error(cli);
4932 d_printf("Error returning browse list: %s\n",
4939 /****************************************************************************
4941 ****************************************************************************/
4943 static void server_fn(const char *name, uint32_t m,
4944 const char *comment, void *state)
4948 d_printf("\t%-16s %s\n", name, comment);
4950 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4954 /****************************************************************************
4955 Try and browse available connections on a host.
4956 ****************************************************************************/
4958 static bool list_servers(const char *wk_grp)
4962 if (!cli->server_domain)
4966 d_printf("\n\tServer Comment\n");
4967 d_printf("\t--------- -------\n");
4969 fstrcpy( state, "Server" );
4970 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4974 d_printf("\n\tWorkgroup Master\n");
4975 d_printf("\t--------- -------\n");
4978 fstrcpy( state, "Workgroup" );
4979 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4984 /****************************************************************************
4985 Print or set current VUID
4986 ****************************************************************************/
4988 static int cmd_vuid(void)
4990 TALLOC_CTX *ctx = talloc_tos();
4993 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4994 d_printf("Current VUID is %d\n",
4995 cli_state_get_uid(cli));
4999 cli_state_set_uid(cli, atoi(buf));
5003 /****************************************************************************
5004 Setup a new VUID, by issuing a session setup
5005 ****************************************************************************/
5007 static int cmd_logon(void)
5009 TALLOC_CTX *ctx = talloc_tos();
5010 char *l_username, *l_password;
5011 struct cli_credentials *creds = NULL;
5014 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
5015 d_printf("logon <username> [<password>]\n");
5019 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
5020 char pwd[256] = {0};
5023 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
5025 l_password = talloc_strdup(ctx, pwd);
5032 creds = cli_session_creds_init(ctx,
5037 false, /* use_kerberos */
5038 false, /* fallback_after_kerberos */
5039 false, /* use_ccache */
5040 false); /* password_is_nt_hash */
5041 if (creds == NULL) {
5042 d_printf("cli_session_creds_init() failed.\n");
5045 nt_status = cli_session_setup_creds(cli, creds);
5047 if (!NT_STATUS_IS_OK(nt_status)) {
5048 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
5052 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
5060 static int cmd_logoff(void)
5064 status = cli_ulogoff(cli);
5065 if (!NT_STATUS_IS_OK(status)) {
5066 d_printf("logoff failed: %s\n", nt_errstr(status));
5070 d_printf("logoff successful\n");
5076 * tree connect (connect to a share)
5079 static int cmd_tcon(void)
5081 TALLOC_CTX *ctx = talloc_tos();
5085 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
5086 d_printf("tcon <sharename>\n");
5094 status = cli_tree_connect(cli, sharename, "?????", NULL);
5095 if (!NT_STATUS_IS_OK(status)) {
5096 d_printf("tcon failed: %s\n", nt_errstr(status));
5100 talloc_free(sharename);
5102 d_printf("tcon to %s successful, tid: %u\n", sharename,
5103 cli_state_get_tid(cli));
5108 * tree disconnect (disconnect from a share)
5111 static int cmd_tdis(void)
5115 status = cli_tdis(cli);
5116 if (!NT_STATUS_IS_OK(status)) {
5117 d_printf("tdis failed: %s\n", nt_errstr(status));
5121 d_printf("tdis successful\n");
5130 static int cmd_tid(void)
5132 TALLOC_CTX *ctx = talloc_tos();
5135 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
5136 if (cli_state_has_tcon(cli)) {
5137 d_printf("current tid is %d\n", cli_state_get_tid(cli));
5139 d_printf("no tcon currently\n");
5142 uint32_t tid = atoi(tid_str);
5143 if (!cli_state_has_tcon(cli)) {
5144 d_printf("no tcon currently\n");
5146 cli_state_set_tid(cli, tid);
5153 /****************************************************************************
5154 list active connections
5155 ****************************************************************************/
5157 static int cmd_list_connect(void)
5159 cli_cm_display(cli);
5163 /****************************************************************************
5164 display the current active client connection
5165 ****************************************************************************/
5167 static int cmd_show_connect( void )
5169 TALLOC_CTX *ctx = talloc_tos();
5170 struct cli_state *targetcli;
5174 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(), cli,
5175 client_get_cur_dir(), &targetcli,
5177 if (!NT_STATUS_IS_OK(status)) {
5178 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
5182 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli->conn), targetcli->share);
5187 * cmd_utimes - interactive command to set the four times
5189 * Read a filename and four times from the client command line and update
5190 * the file times. A value of -1 for a time means don't change.
5192 static int cmd_utimes(void)
5196 struct timespec times[4] = {{0}};
5197 struct timeval_buf tbuf[4];
5201 TALLOC_CTX *ctx = talloc_new(NULL);
5208 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5210 d_printf("utimes <filename> <create-time> <access-time> "
5211 "<write-time> <change-time>\n");
5212 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5213 "or -1 for no change\n");
5218 fname = talloc_asprintf(ctx,
5220 client_get_cur_dir(),
5222 if (fname == NULL) {
5226 fname = client_clean_name(ctx, fname);
5227 if (fname == NULL) {
5232 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
5234 const char *s = buf;
5235 struct tm tm = {0,};
5239 if (strlen(s) == 2 && strcmp(s, "-1") == 0) {
5240 times[time_count] = make_omit_timespec();
5245 ret = strptime(s, "%y:%m:%d-%H:%M:%S", &tm);
5248 ret = strptime(s, "%Y:%m:%d-%H:%M:%S", &tm);
5251 /* We could not match all the chars, so print error */
5252 if (ret == NULL || *ret != 0) {
5253 d_printf("Invalid date format: %s\n", s);
5254 d_printf("utimes <filename> <create-time> "
5255 "<access-time> <write-time> <change-time>\n");
5256 d_printf("Dates should be in [YY]YY:MM:DD-HH:MM:SS "
5257 "format or -1 for no change\n");
5262 /* Convert tm to a time_t */
5264 times[time_count] = (struct timespec){.tv_sec = t};
5268 if (time_count < 4) {
5269 d_printf("Insufficient dates: %d\n", time_count);
5270 d_printf("utimes <filename> <create-time> <access-time> "
5271 "<write-time> <change-time>\n");
5272 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5273 "or -1 for no change\n");
5278 DEBUG(10, ("times\nCreate: %sAccess: %s Write: %sChange: %s\n",
5279 timespec_string_buf(×[0], false, &tbuf[0]),
5280 timespec_string_buf(×[1], false, &tbuf[1]),
5281 timespec_string_buf(×[2], false, &tbuf[2]),
5282 timespec_string_buf(×[3], false, &tbuf[3])));
5284 status = cli_setpathinfo_ext(
5285 cli, fname, times[0], times[1], times[2], times[3],
5287 if (!NT_STATUS_IS_OK(status)) {
5288 d_printf("cli_setpathinfo_ext failed: %s\n",
5299 * set_remote_attr - set DOS attributes of a remote file
5300 * @filename: path to the file name
5301 * @new_attr: attribute bit mask to use
5302 * @mode: one of ATTR_SET or ATTR_UNSET
5304 * Update the file attributes with the one provided.
5306 int set_remote_attr(const char *filename, uint32_t new_attr, int mode)
5308 extern struct cli_state *cli;
5312 status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
5313 if (!NT_STATUS_IS_OK(status)) {
5314 d_printf("cli_getatr failed: %s\n", nt_errstr(status));
5318 if (mode == ATTR_SET) {
5319 new_attr |= old_attr;
5321 new_attr = old_attr & ~new_attr;
5324 status = cli_setatr(cli, filename, new_attr, 0);
5325 if (!NT_STATUS_IS_OK(status)) {
5326 d_printf("cli_setatr failed: %s\n", nt_errstr(status));
5334 * cmd_setmode - interactive command to set DOS attributes
5336 * Read a filename and mode from the client command line and update
5337 * the file DOS attributes.
5339 int cmd_setmode(void)
5343 uint32_t attr[2] = {0};
5344 int mode = ATTR_SET;
5347 TALLOC_CTX *ctx = talloc_new(NULL);
5352 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5354 d_printf("setmode <filename> <[+|-]rsha>\n");
5359 fname = talloc_asprintf(ctx,
5361 client_get_cur_dir(),
5363 if (fname == NULL) {
5367 fname = client_clean_name(ctx, fname);
5368 if (fname == NULL) {
5373 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
5374 const char *s = buf;
5385 attr[mode] |= FILE_ATTRIBUTE_READONLY;
5388 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
5391 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
5394 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
5397 d_printf("setmode <filename> <perm=[+|-]rsha>\n");
5404 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
5405 d_printf("setmode <filename> <[+|-]rsha>\n");
5410 DEBUG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
5412 /* ignore return value: server might not store DOS attributes */
5413 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
5414 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
5420 /****************************************************************************
5422 ***************************************************************************/
5424 int cmd_iosize(void)
5426 TALLOC_CTX *ctx = talloc_tos();
5430 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5431 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5433 d_printf("iosize <n> or iosize 0x<n>. "
5434 "Minimum is 0 (default), "
5435 "max is 16776960 (0xFFFF00)\n");
5437 d_printf("iosize <n> or iosize 0x<n>. "
5438 "(Encrypted connection) ,"
5439 "Minimum is 0 (default), "
5440 "max is 130048 (0x1FC00)\n");
5443 d_printf("iosize <n> or iosize 0x<n>.\n");
5448 iosize = strtol(buf,NULL,0);
5449 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5450 if (smb_encrypt && (iosize < 0 || iosize > 0xFC00)) {
5451 d_printf("iosize out of range for encrypted "
5452 "connection (min = 0 (default), "
5453 "max = 130048 (0x1FC00)\n");
5455 } else if (!smb_encrypt && (iosize < 0 || iosize > 0xFFFF00)) {
5456 d_printf("iosize out of range (min = 0 (default), "
5457 "max = 16776960 (0xFFFF00)\n");
5462 io_bufsize = iosize;
5463 d_printf("iosize is now %d\n", io_bufsize);
5467 /****************************************************************************
5469 ***************************************************************************/
5471 static int cmd_timeout(void)
5473 TALLOC_CTX *ctx = talloc_tos();
5476 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5477 unsigned int old_timeout = cli_set_timeout(cli, 0);
5478 cli_set_timeout(cli, old_timeout);
5479 d_printf("timeout <n> (per-operation timeout "
5480 "in seconds - currently %u).\n",
5485 io_timeout = strtol(buf,NULL,0);
5486 cli_set_timeout(cli, io_timeout*1000);
5487 d_printf("io_timeout per operation is now %d\n", io_timeout);
5492 /****************************************************************************
5494 ****************************************************************************/
5495 static int cmd_history(void)
5497 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
5501 hlist = history_list();
5503 for (i = 0; hlist && hlist[i]; i++) {
5504 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
5507 DEBUG(0,("no history without readline support\n"));
5513 /* Some constants for completing filename arguments */
5515 #define COMPL_NONE 0 /* No completions */
5516 #define COMPL_REMOTE 1 /* Complete remote filename */
5517 #define COMPL_LOCAL 2 /* Complete local filename */
5519 /* This defines the commands supported by this client.
5520 * NOTE: The "!" must be the last one in the list because it's fn pointer
5521 * field is NULL, and NULL in that field is used in process_tok()
5522 * (below) to indicate the end of the list. crh
5527 const char *description;
5528 char compl_args[2]; /* Completion argument info */
5530 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5531 {"allinfo",cmd_allinfo,"<file> show all available info",
5532 {COMPL_REMOTE,COMPL_NONE}},
5533 {"altname",cmd_altname,"<file> show alt name",{COMPL_REMOTE,COMPL_NONE}},
5534 {"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}},
5535 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
5536 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
5537 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
5538 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
5539 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
5540 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_NONE}},
5541 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_NONE}},
5542 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_NONE}},
5543 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5544 {"deltree",cmd_deltree,"<mask> recursively delete all matching files and directories",{COMPL_REMOTE,COMPL_NONE}},
5545 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5546 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5547 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
5548 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5549 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
5550 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_NONE}},
5551 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
5552 {COMPL_REMOTE, COMPL_NONE}},
5553 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5554 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5555 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
5556 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
5557 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
5558 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5559 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5560 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
5561 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5562 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5563 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
5564 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5565 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
5566 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5567 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
5568 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
5569 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
5570 {"notify",cmd_notify,"<file>Get notified of dir changes",{COMPL_REMOTE,COMPL_NONE}},
5571 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
5572 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
5573 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
5574 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5575 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5576 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5577 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5578 {"posix_whoami",cmd_posix_whoami,"return logged on user information "
5579 "using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5580 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
5581 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
5582 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
5583 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
5584 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5585 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
5586 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5587 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5588 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
5589 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
5590 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
5591 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
5592 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
5593 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5594 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_REMOTE,COMPL_NONE}},
5595 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
5596 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
5597 {COMPL_REMOTE, COMPL_LOCAL}},
5598 {"setmode",cmd_setmode,"<file name> <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
5599 {"scopy",cmd_scopy,"<src> <dest> server-side copy file",{COMPL_REMOTE,COMPL_REMOTE}},
5600 {"stat",cmd_stat,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_NONE}},
5601 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5602 {"tar",cmd_tar,"tar <c|x>[IXFvbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
5603 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
5604 {"timeout",cmd_timeout,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE,COMPL_NONE}},
5605 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
5606 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5607 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
5608 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
5609 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5610 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
5611 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
5612 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
5613 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
5614 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
5615 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
5616 {"utimes", cmd_utimes,"<file name> <create_time> <access_time> <mod_time> "
5617 "<ctime> set times", {COMPL_REMOTE,COMPL_NONE}},
5618 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
5619 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
5621 /* Yes, this must be here, see crh's comment above. */
5622 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
5623 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
5626 /*******************************************************************
5627 Lookup a command string in the list of commands, including
5629 ******************************************************************/
5631 static int process_tok(char *tok)
5633 size_t i = 0, matches = 0;
5635 size_t tok_len = strlen(tok);
5637 while (commands[i].fn != NULL) {
5638 if (strequal(commands[i].name,tok)) {
5642 } else if (strnequal(commands[i].name, tok, tok_len)) {
5651 else if (matches == 1)
5657 /****************************************************************************
5659 ****************************************************************************/
5661 static int cmd_help(void)
5663 TALLOC_CTX *ctx = talloc_tos();
5667 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5668 if ((i = process_tok(buf)) >= 0)
5669 d_printf("HELP %s:\n\t%s\n\n",
5670 commands[i].name,commands[i].description);
5672 while (commands[i].description) {
5673 for (j=0; commands[i].description && (j<5); j++) {
5674 d_printf("%-15s",commands[i].name);
5683 /****************************************************************************
5684 Process a -c command string.
5685 ****************************************************************************/
5687 static int process_command_string(const char *cmd_in)
5689 TALLOC_CTX *ctx = talloc_tos();
5690 char *cmd = talloc_strdup(ctx, cmd_in);
5696 /* establish the connection if not already */
5701 status = cli_cm_open(talloc_tos(), NULL,
5703 service, popt_get_cmdline_auth_info(),
5706 have_ip ? &dest_ss : NULL, port,
5709 if (!NT_STATUS_IS_OK(status)) {
5712 cli_set_timeout(cli, io_timeout*1000);
5715 while (cmd[0] != '\0') {
5721 if ((p = strchr_m(cmd, ';')) == 0) {
5730 /* and get the first part of the command */
5732 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
5736 if ((i = process_tok(tok)) >= 0) {
5737 rc = commands[i].fn();
5738 } else if (i == -2) {
5739 d_printf("%s: command abbreviation ambiguous\n",tok);
5741 d_printf("%s: command not found\n",tok);
5748 #define MAX_COMPLETIONS 100
5750 struct completion_remote {
5758 static NTSTATUS completion_remote_filter(const char *mnt,
5759 struct file_info *f,
5763 struct completion_remote *info = (struct completion_remote *)state;
5765 if (info->count >= MAX_COMPLETIONS - 1) {
5766 return NT_STATUS_OK;
5768 if (strncmp(info->text, f->name, info->len) != 0) {
5769 return NT_STATUS_OK;
5771 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
5772 return NT_STATUS_OK;
5775 if ((info->dirmask[0] == 0) && !(f->attr & FILE_ATTRIBUTE_DIRECTORY))
5776 info->matches[info->count] = SMB_STRDUP(f->name);
5778 TALLOC_CTX *ctx = talloc_stackframe();
5781 tmp = talloc_strdup(ctx,info->dirmask);
5784 return NT_STATUS_NO_MEMORY;
5786 tmp = talloc_asprintf_append(tmp, "%s", f->name);
5789 return NT_STATUS_NO_MEMORY;
5791 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5792 tmp = talloc_asprintf_append(tmp, "%s",
5797 return NT_STATUS_NO_MEMORY;
5799 info->matches[info->count] = SMB_STRDUP(tmp);
5802 if (info->matches[info->count] == NULL) {
5803 return NT_STATUS_OK;
5805 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5806 smb_readline_ca_char(0);
5808 if (info->count == 1) {
5809 info->samelen = strlen(info->matches[info->count]);
5811 while (strncmp(info->matches[info->count],
5812 info->matches[info->count-1],
5813 info->samelen) != 0) {
5818 return NT_STATUS_OK;
5821 static char **remote_completion(const char *text, int len)
5823 TALLOC_CTX *ctx = talloc_stackframe();
5824 char *dirmask = NULL;
5825 char *targetpath = NULL;
5826 struct cli_state *targetcli = NULL;
5828 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
5831 /* can't have non-static initialisation on Sun CC, so do it
5837 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
5838 if (!info.matches) {
5844 * We're leaving matches[0] free to fill it later with the text to
5845 * display: Either the one single match or the longest common subset
5848 info.matches[0] = NULL;
5851 for (i = len-1; i >= 0; i--) {
5852 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
5857 info.text = text+i+1;
5858 info.samelen = info.len = len-i-1;
5861 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
5862 if (!info.dirmask) {
5865 strncpy(info.dirmask, text, i+1);
5866 info.dirmask[i+1] = 0;
5867 dirmask = talloc_asprintf(ctx,
5869 client_get_cur_dir(),
5873 info.dirmask = SMB_STRDUP("");
5874 if (!info.dirmask) {
5877 dirmask = talloc_asprintf(ctx,
5879 client_get_cur_dir());
5884 dirmask = client_clean_name(ctx, dirmask);
5885 if (dirmask == NULL) {
5889 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
5890 cli, dirmask, &targetcli, &targetpath);
5891 if (!NT_STATUS_IS_OK(status)) {
5894 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
5895 completion_remote_filter, (void *)&info);
5896 if (!NT_STATUS_IS_OK(status)) {
5900 if (info.count == 1) {
5902 * No matches at all, NULL indicates there is nothing
5904 SAFE_FREE(info.matches[0]);
5905 SAFE_FREE(info.matches);
5910 if (info.count == 2) {
5912 * Exactly one match in matches[1], indicate this is the one
5915 info.matches[0] = info.matches[1];
5916 info.matches[1] = NULL;
5919 return info.matches;
5923 * We got more than one possible match, set the result to the maximum
5927 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
5928 info.matches[info.count] = NULL;
5930 return info.matches;
5933 for (i = 0; i < info.count; i++) {
5934 SAFE_FREE(info.matches[i]);
5936 SAFE_FREE(info.matches);
5937 SAFE_FREE(info.dirmask);
5942 static char **completion_fn(const char *text, int start, int end)
5944 smb_readline_ca_char(' ');
5947 const char *buf, *sp;
5951 buf = smb_readline_get_line_buffer();
5955 sp = strchr(buf, ' ');
5959 for (i = 0; commands[i].name; i++) {
5960 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
5961 (commands[i].name[sp - buf] == 0)) {
5965 if (commands[i].name == NULL)
5971 if (sp == (buf + start))
5972 compl_type = commands[i].compl_args[0];
5974 compl_type = commands[i].compl_args[1];
5976 if (compl_type == COMPL_REMOTE)
5977 return remote_completion(text, end - start);
5978 else /* fall back to local filename completion */
5982 size_t i, len, samelen = 0, count=1;
5984 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
5991 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
5992 if (strncmp(text, commands[i].name, len) == 0) {
5993 matches[count] = SMB_STRDUP(commands[i].name);
5994 if (!matches[count])
5997 samelen = strlen(matches[count]);
5999 while (strncmp(matches[count], matches[count-1], samelen) != 0)
6006 case 0: /* should never happen */
6010 matches[0] = SMB_STRDUP(matches[1]);
6013 matches[0] = (char *)SMB_MALLOC(samelen+1);
6016 strncpy(matches[0], matches[1], samelen);
6017 matches[0][samelen] = 0;
6019 matches[count] = NULL;
6023 for (i = 0; i < count; i++)
6031 static bool finished;
6033 /****************************************************************************
6034 Make sure we swallow keepalives during idle time.
6035 ****************************************************************************/
6037 static void readline_callback(void)
6039 static time_t last_t;
6040 struct timespec now;
6043 unsigned char garbage[16];
6045 clock_gettime_mono(&now);
6053 /* Ping the server to keep the connection alive using SMBecho. */
6054 memset(garbage, 0xf0, sizeof(garbage));
6055 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
6056 if (NT_STATUS_IS_OK(status) ||
6057 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
6059 * Even if server returns NT_STATUS_INVALID_PARAMETER
6060 * it still responded.
6061 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
6066 if (!cli_state_is_connected(cli)) {
6067 DEBUG(0,("SMBecho failed (%s). The connection is "
6068 "disconnected now\n", nt_errstr(status)));
6070 smb_readline_done();
6074 /****************************************************************************
6075 Process commands on stdin.
6076 ****************************************************************************/
6078 static int process_stdin(void)
6083 d_printf("Try \"help\" to get a list of possible commands.\n");
6087 TALLOC_CTX *frame = talloc_stackframe();
6089 char *the_prompt = NULL;
6093 /* display a prompt */
6094 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
6098 line = smb_readline(the_prompt, readline_callback, completion_fn);
6099 SAFE_FREE(the_prompt);
6105 /* special case - first char is ! */
6107 if (system(line + 1) == -1) {
6108 d_printf("system() command %s failed.\n",
6116 /* and get the first part of the command */
6118 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
6124 if ((i = process_tok(tok)) >= 0) {
6125 rc = commands[i].fn();
6126 } else if (i == -2) {
6127 d_printf("%s: command abbreviation ambiguous\n",tok);
6129 d_printf("%s: command not found\n",tok);
6137 /****************************************************************************
6138 Process commands from the client.
6139 ****************************************************************************/
6141 static int process(const char *base_directory)
6146 status = cli_cm_open(talloc_tos(), NULL,
6148 service, popt_get_cmdline_auth_info(),
6149 smb_encrypt, max_protocol,
6150 have_ip ? &dest_ss : NULL, port,
6152 if (!NT_STATUS_IS_OK(status)) {
6156 cli_set_timeout(cli, io_timeout*1000);
6158 if (base_directory && *base_directory) {
6159 rc = do_cd(base_directory);
6167 rc = process_command_string(cmdstr);
6176 /****************************************************************************
6178 ****************************************************************************/
6180 static int do_host_query(const char *query_host)
6184 status = cli_cm_open(talloc_tos(), NULL,
6186 "IPC$", popt_get_cmdline_auth_info(),
6187 smb_encrypt, max_protocol,
6188 have_ip ? &dest_ss : NULL, port,
6190 if (!NT_STATUS_IS_OK(status)) {
6194 cli_set_timeout(cli, io_timeout*1000);
6197 /* Ensure that the host can do IPv4 */
6199 if (!interpret_addr(query_host)) {
6200 struct sockaddr_storage ss;
6201 if (interpret_string_addr(&ss, query_host, 0) &&
6202 (ss.ss_family != AF_INET)) {
6203 d_printf("%s is an IPv6 address -- no workgroup available\n",
6209 if (lp_client_min_protocol() > PROTOCOL_NT1) {
6210 d_printf("SMB1 disabled -- no workgroup available\n");
6214 if (lp_disable_netbios()) {
6215 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
6219 if (port != NBT_SMB_PORT ||
6220 smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1)
6222 int max_proto = MIN(max_protocol, PROTOCOL_NT1);
6225 * Workgroups simply don't make sense over anything
6226 * else but port 139 and SMB1.
6230 d_printf("Reconnecting with SMB1 for workgroup listing.\n");
6231 status = cli_cm_open(talloc_tos(), NULL,
6233 "IPC$", popt_get_cmdline_auth_info(),
6234 smb_encrypt, max_proto,
6235 have_ip ? &dest_ss : NULL, NBT_SMB_PORT,
6237 if (!NT_STATUS_IS_OK(status)) {
6238 d_printf("Unable to connect with SMB1 "
6239 "-- no workgroup available\n");
6244 cli_set_timeout(cli, io_timeout*1000);
6245 list_servers(lp_workgroup());
6252 /****************************************************************************
6253 Handle a tar operation.
6254 ****************************************************************************/
6256 static int do_tar_op(const char *base_directory)
6258 struct tar *tar_ctx = tar_get_ctx();
6261 /* do we already have a connection? */
6265 status = cli_cm_open(talloc_tos(), NULL,
6267 service, popt_get_cmdline_auth_info(),
6268 smb_encrypt, max_protocol,
6269 have_ip ? &dest_ss : NULL, port,
6271 if (!NT_STATUS_IS_OK(status)) {
6275 cli_set_timeout(cli, io_timeout*1000);
6280 if (base_directory && *base_directory) {
6281 ret = do_cd(base_directory);
6287 ret = tar_process(tar_ctx);
6295 /****************************************************************************
6296 Handle a message operation.
6297 ****************************************************************************/
6299 static int do_message_op(struct user_auth_info *a_info)
6303 if (lp_disable_netbios()) {
6304 d_printf("NetBIOS over TCP disabled.\n");
6308 status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
6309 port ? port : NBT_SMB_PORT, name_type,
6310 lp_netbios_name(), SMB_SIGNING_DEFAULT, 0, &cli);
6311 if (!NT_STATUS_IS_OK(status)) {
6312 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
6316 cli_set_timeout(cli, io_timeout*1000);
6317 send_message(get_cmdline_auth_info_username(a_info));
6323 /****************************************************************************
6325 ****************************************************************************/
6327 int main(int argc,char *argv[])
6329 const char **const_argv = discard_const_p(const char *, argv);
6330 char *base_directory = NULL;
6332 char *query_host = NULL;
6333 bool message = false;
6334 static const char *new_name_resolve_order = NULL;
6338 bool tar_opt = false;
6339 bool service_opt = false;
6340 struct tar *tar_ctx = tar_get_ctx();
6342 struct poptOption long_options[] = {
6346 .longName = "name-resolve",
6348 .argInfo = POPT_ARG_STRING,
6349 .arg = &new_name_resolve_order,
6351 .descrip = "Use these name resolution services only",
6352 .argDescrip = "NAME-RESOLVE-ORDER",
6355 .longName = "message",
6357 .argInfo = POPT_ARG_STRING,
6360 .descrip = "Send message",
6361 .argDescrip = "HOST",
6364 .longName = "ip-address",
6366 .argInfo = POPT_ARG_STRING,
6369 .descrip = "Use this IP to connect to",
6373 .longName = "stderr",
6375 .argInfo = POPT_ARG_NONE,
6378 .descrip = "Write messages to stderr instead of stdout",
6383 .argInfo = POPT_ARG_STRING,
6386 .descrip = "Get a list of shares available on a host",
6387 .argDescrip = "HOST",
6390 .longName = "max-protocol",
6392 .argInfo = POPT_ARG_STRING,
6395 .descrip = "Set the max protocol level",
6396 .argDescrip = "LEVEL",
6401 .argInfo = POPT_ARG_STRING,
6404 .descrip = "Command line tar",
6405 .argDescrip = "<c|x>IXFvgbNan",
6408 .longName = "directory",
6410 .argInfo = POPT_ARG_STRING,
6413 .descrip = "Start from directory",
6414 .argDescrip = "DIR",
6417 .longName = "command",
6419 .argInfo = POPT_ARG_STRING,
6422 .descrip = "Execute semicolon separated commands",
6425 .longName = "send-buffer",
6427 .argInfo = POPT_ARG_INT,
6430 .descrip = "Changes the transmit/send buffer",
6431 .argDescrip = "BYTES",
6434 .longName = "timeout",
6436 .argInfo = POPT_ARG_INT,
6439 .descrip = "Changes the per-operation timeout",
6440 .argDescrip = "SECONDS",
6445 .argInfo = POPT_ARG_INT,
6448 .descrip = "Port to connect to",
6449 .argDescrip = "PORT",
6452 .longName = "grepable",
6454 .argInfo = POPT_ARG_NONE,
6457 .descrip = "Produce grepable output",
6460 .longName = "quiet",
6462 .argInfo = POPT_ARG_NONE,
6465 .descrip = "Suppress help message",
6468 .longName = "browse",
6470 .argInfo = POPT_ARG_NONE,
6473 .descrip = "Browse SMB servers using DNS",
6476 POPT_COMMON_CONNECTION
6477 POPT_COMMON_CREDENTIALS
6480 TALLOC_CTX *frame = talloc_stackframe();
6482 if (!client_set_cur_dir("\\")) {
6486 /* set default debug level to 1 regardless of what smb.conf sets */
6487 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
6490 lp_set_cmdline("log level", "1");
6492 popt_common_credentials_set_ignore_missing_conf();
6493 popt_common_credentials_set_delay_post();
6496 pc = poptGetContext("smbclient", argc, const_argv, long_options, 0);
6497 poptSetOtherOptionHelp(pc, "service <password>");
6499 while ((opt = poptGetNextOpt(pc)) != -1) {
6502 * if the tar option has been called previously, now
6503 * we need to eat out the leftovers
6505 /* I see no other way to keep things sane --SSS */
6506 if (tar_opt == true) {
6507 while (poptPeekArg(pc)) {
6513 /* if the service has not yet been specified lets see if it is available in the popt stack */
6514 if (!service_opt && poptPeekArg(pc)) {
6515 service = talloc_strdup(frame, poptGetArg(pc));
6522 /* if the service has already been retrieved then check if we have also a password */
6524 && (!get_cmdline_auth_info_got_pass(
6525 popt_get_cmdline_auth_info()))
6526 && poptPeekArg(pc)) {
6527 set_cmdline_auth_info_password(
6528 popt_get_cmdline_auth_info(), poptGetArg(pc));
6534 /* Messages are sent to NetBIOS name type 0x3
6535 * (Messenger Service). Make sure we default
6536 * to port 139 instead of port 445. srl,crh
6539 desthost = talloc_strdup(frame,poptGetOptArg(pc));
6544 port = NBT_SMB_PORT;
6549 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
6553 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
6557 setup_logging("smbclient", DEBUG_STDERR );
6558 display_set_stderr();
6562 query_host = talloc_strdup(frame, poptGetOptArg(pc));
6568 lp_set_cmdline("client max protocol", poptGetOptArg(pc));
6571 /* We must use old option processing for this. Find the
6572 * position of the -T option in the raw argv[]. */
6576 for (i = 1; i < argc; i++) {
6577 if (strncmp("-T", argv[i],2)==0)
6581 if (tar_parse_args(tar_ctx, poptGetOptArg(pc),
6582 const_argv + i, argc - i)) {
6583 poptPrintUsage(pc, stderr, 0);
6587 /* this must be the last option, mark we have parsed it so that we know we have */
6591 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
6592 if (!base_directory) {
6606 return(do_smb_browse());
6611 /* We may still have some leftovers after the last popt option has been called */
6612 if (tar_opt == true) {
6613 while (poptPeekArg(pc)) {
6619 /* if the service has not yet been specified lets see if it is available in the popt stack */
6620 if (!service_opt && poptPeekArg(pc)) {
6621 service = talloc_strdup(frame,poptGetArg(pc));
6628 /* if the service has already been retrieved then check if we have also a password */
6630 && !get_cmdline_auth_info_got_pass(popt_get_cmdline_auth_info())
6631 && poptPeekArg(pc)) {
6632 set_cmdline_auth_info_password(popt_get_cmdline_auth_info(),
6636 if (service_opt && service) {
6639 /* Convert any '/' characters in the service name to '\' characters */
6640 string_replace(service, '/','\\');
6641 if (count_chars(service,'\\') < 3) {
6642 d_printf("\n%s: Not enough '\\' characters in service\n",service);
6643 poptPrintUsage(pc, stderr, 0);
6646 /* Remove trailing slashes */
6647 len = strlen(service);
6648 while(len > 0 && service[len - 1] == '\\') {
6650 service[len] = '\0';
6654 if (!init_names()) {
6655 fprintf(stderr, "init_names() failed\n");
6659 if(new_name_resolve_order)
6660 lp_set_cmdline("name resolve order", new_name_resolve_order);
6662 if (!tar_to_process(tar_ctx) && !query_host && !service && !message) {
6663 poptPrintUsage(pc, stderr, 0);
6667 poptFreeContext(pc);
6668 popt_burn_cmdline_password(argc, argv);
6670 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
6672 /* Ensure we have a password (or equivalent). */
6673 popt_common_credentials_post();
6674 smb_encrypt = get_cmdline_auth_info_smb_encrypt(
6675 popt_get_cmdline_auth_info());
6677 max_protocol = lp_client_max_protocol();
6679 if (tar_to_process(tar_ctx)) {
6681 process_command_string(cmdstr);
6682 rc = do_tar_op(base_directory);
6683 } else if (query_host && *query_host) {
6684 char *qhost = query_host;
6687 while (*qhost == '\\' || *qhost == '/')
6690 if ((slash = strchr_m(qhost, '/'))
6691 || (slash = strchr_m(qhost, '\\'))) {
6695 if ((p=strchr_m(qhost, '#'))) {
6698 sscanf(p, "%x", &name_type);
6701 rc = do_host_query(qhost);
6702 } else if (message) {
6703 rc = do_message_op(popt_get_cmdline_auth_info());
6704 } else if (process(base_directory)) {
6708 popt_free_cmdline_auth_info();