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.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "client/client_proto.h"
29 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
30 #include "../lib/util/select.h"
31 #include "system/readline.h"
32 #include "../libcli/smbreadline/smbreadline.h"
33 #include "../libcli/security/security.h"
34 #include "system/select.h"
35 #include "libsmb/libsmb.h"
36 #include "libsmb/clirap.h"
38 #include "libsmb/nmblib.h"
39 #include "include/ntioctl.h"
45 extern int do_smb_browse(void); /* mDNS browsing */
47 extern bool override_logfile;
52 static char *desthost;
53 static bool grepable = false;
54 static char *cmdstr = NULL;
55 const char *cmd_ptr = NULL;
57 static int io_bufsize = 524288;
59 static int name_type = 0x20;
60 static int max_protocol = PROTOCOL_NT1;
62 static int process_tok(char *tok);
63 static int cmd_help(void);
65 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
67 /* 30 second timeout on most commands */
68 #define CLIENT_TIMEOUT (30*1000)
69 #define SHORT_TIMEOUT (5*1000)
71 /* value for unused fid field in trans2 secondary request */
72 #define FID_UNUSED (0xFFFF)
74 time_t newer_than = 0;
75 static int archive_level = 0;
77 static bool translation = false;
80 /* clitar bits insert */
83 extern bool tar_reset;
86 static bool prompt = true;
88 static bool recurse = false;
89 static bool showacls = false;
90 bool lowercase = false;
91 static bool backup_intent = false;
93 static struct sockaddr_storage dest_ss;
94 static char dest_ss_str[INET6_ADDRSTRLEN];
96 #define SEPARATORS " \t\n\r"
98 static bool abort_mget = true;
101 uint64_t get_total_size = 0;
102 unsigned int get_total_time_ms = 0;
103 static uint64_t put_total_size = 0;
104 static unsigned int put_total_time_ms = 0;
107 static double dir_total;
109 /* encrypted state. */
110 static bool smb_encrypt;
112 /* root cli_state connection */
114 struct cli_state *cli;
116 static char CLI_DIRSEP_CHAR = '\\';
117 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
119 /* Authentication for client connections. */
120 struct user_auth_info *auth_info;
122 /* Accessor functions for directory paths. */
123 static char *fileselection;
124 static const char *client_get_fileselection(void)
127 return fileselection;
132 static const char *client_set_fileselection(const char *new_fs)
134 SAFE_FREE(fileselection);
136 fileselection = SMB_STRDUP(new_fs);
138 return client_get_fileselection();
142 static const char *client_get_cwd(void)
147 return CLI_DIRSEP_STR;
150 static const char *client_set_cwd(const char *new_cwd)
154 cwd = SMB_STRDUP(new_cwd);
156 return client_get_cwd();
159 static char *cur_dir;
160 const char *client_get_cur_dir(void)
165 return CLI_DIRSEP_STR;
168 const char *client_set_cur_dir(const char *newdir)
172 cur_dir = SMB_STRDUP(newdir);
174 return client_get_cur_dir();
177 /****************************************************************************
178 Put up a yes/no prompt.
179 ****************************************************************************/
181 static bool yesno(const char *p)
186 if (!fgets(ans,sizeof(ans)-1,stdin))
189 if (*ans == 'y' || *ans == 'Y')
195 /****************************************************************************
196 Write to a local file with CR/LF->LF translation if appropriate. Return the
197 number taken from the buffer. This may not equal the number written.
198 ****************************************************************************/
200 static int writefile(int f, char *b, int n)
210 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
213 if (write(f, b, 1) != 1) {
223 /****************************************************************************
224 Read from a file with LF->CR/LF translation if appropriate. Return the
225 number read. read approx n bytes.
226 ****************************************************************************/
228 static int readfile(uint8_t *b, int n, XFILE *f)
234 return x_fread(b,1,n,f);
237 while (i < (n - 1) && (i < BUFFER_SIZE)) {
238 if ((c = x_getc(f)) == EOF) {
242 if (c == '\n') { /* change all LFs to CR/LF */
257 static size_t push_source(uint8_t *buf, size_t n, void *priv)
259 struct push_state *state = (struct push_state *)priv;
262 if (x_feof(state->f)) {
266 result = readfile(buf, n, state->f);
267 state->nread += result;
271 /****************************************************************************
273 ****************************************************************************/
275 static void send_message(const char *username)
281 d_printf("Type your message, ending it with a Control-D\n");
284 while (i<sizeof(buf)-2) {
285 int c = fgetc(stdin);
296 status = cli_message(cli, desthost, username, buf);
297 if (!NT_STATUS_IS_OK(status)) {
298 d_fprintf(stderr, "cli_message returned %s\n",
303 /****************************************************************************
304 Wrapper function around cli_open() that does an NtCreateX open by preference.
305 ****************************************************************************/
307 NTSTATUS smbclient_cli_open(struct cli_state *cli, const char *fname, int flags,
308 int share_mode_in, uint16_t *pfnum)
311 unsigned int openfn = 0;
312 unsigned int dos_deny = 0;
313 uint32_t access_mask, share_mode, create_disposition, create_options;
315 /* Do the initial mapping into OpenX parameters. */
316 if (flags & O_CREAT) {
319 if (!(flags & O_EXCL)) {
326 dos_deny = (share_mode_in<<4);
328 if ((flags & O_ACCMODE) == O_RDWR) {
330 } else if ((flags & O_ACCMODE) == O_WRONLY) {
335 if ((flags & O_SYNC) == O_SYNC) {
340 if (share_mode_in == DENY_FCB) {
345 /* Hmmm. This is what I think the above code
346 should look like if it's using the constants
349 if (flags & O_CREAT) {
350 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
352 if (!(flags & O_EXCL)) {
354 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
356 openfn |= OPENX_FILE_EXISTS_OPEN;
359 dos_deny = SET_DENY_MODE(share_mode_in);
361 if ((flags & O_ACCMODE) == O_RDWR) {
362 dos_deny |= DOS_OPEN_RDWR;
363 } else if ((flags & O_ACCMODE) == O_WRONLY) {
364 dos_deny |= DOS_OPEN_WRONLY;
368 if ((flags & O_SYNC) == O_SYNC) {
369 dos_deny |= FILE_SYNC_OPENMODE;
373 if (share_mode_in == DENY_FCB) {
378 if (!map_open_params_to_ntcreate(fname, dos_deny,
379 openfn, &access_mask,
380 &share_mode, &create_disposition,
381 &create_options, NULL)) {
385 status = cli_ntcreate(cli,
396 /* Try and cope will all varients of "we don't do this call"
397 and fall back to openX. */
399 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
400 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
401 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
402 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
403 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
404 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
412 return cli_open(cli, fname, flags, share_mode_in, pfnum);
415 /****************************************************************************
416 Check the space on a device.
417 ****************************************************************************/
419 static int do_dskattr(void)
421 int total, bsize, avail;
422 struct cli_state *targetcli = NULL;
423 char *targetpath = NULL;
424 TALLOC_CTX *ctx = talloc_tos();
427 status = cli_resolve_path(ctx, "", auth_info, cli,
428 client_get_cur_dir(), &targetcli,
430 if (!NT_STATUS_IS_OK(status)) {
431 d_printf("Error in dskattr: %s\n", nt_errstr(status));
435 status = cli_dskattr(targetcli, &bsize, &total, &avail);
436 if (!NT_STATUS_IS_OK(status)) {
437 d_printf("Error in dskattr: %s\n", nt_errstr(status));
441 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
442 total, bsize, avail);
447 /****************************************************************************
449 ****************************************************************************/
451 static int cmd_pwd(void)
453 d_printf("Current directory is %s",service);
454 d_printf("%s\n",client_get_cur_dir());
458 /****************************************************************************
459 Ensure name has correct directory separators.
460 ****************************************************************************/
462 static void normalize_name(char *newdir)
464 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
465 string_replace(newdir,'/','\\');
469 /****************************************************************************
470 Change directory - inner section.
471 ****************************************************************************/
473 static int do_cd(const char *new_dir)
476 char *saved_dir = NULL;
478 char *targetpath = NULL;
479 struct cli_state *targetcli = NULL;
480 SMB_STRUCT_STAT sbuf;
483 TALLOC_CTX *ctx = talloc_stackframe();
486 newdir = talloc_strdup(ctx, new_dir);
492 normalize_name(newdir);
494 /* Save the current directory in case the new directory is invalid */
496 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
502 if (*newdir == CLI_DIRSEP_CHAR) {
503 client_set_cur_dir(newdir);
506 new_cd = talloc_asprintf(ctx, "%s%s",
507 client_get_cur_dir(),
514 /* Ensure cur_dir ends in a DIRSEP */
515 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
516 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
521 client_set_cur_dir(new_cd);
523 new_cd = clean_name(ctx, new_cd);
524 client_set_cur_dir(new_cd);
526 status = cli_resolve_path(ctx, "", auth_info, cli, new_cd,
527 &targetcli, &targetpath);
528 if (!NT_STATUS_IS_OK(status)) {
529 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
530 client_set_cur_dir(saved_dir);
534 if (strequal(targetpath,CLI_DIRSEP_STR )) {
539 /* Use a trans2_qpathinfo to test directories for modern servers.
540 Except Win9x doesn't support the qpathinfo_basic() call..... */
542 if (cli_state_protocol(targetcli) > PROTOCOL_LANMAN2 && !targetcli->win95) {
544 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
546 if (!NT_STATUS_IS_OK(status)) {
547 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
548 client_set_cur_dir(saved_dir);
552 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
553 d_printf("cd %s: not a directory\n", new_cd);
554 client_set_cur_dir(saved_dir);
559 targetpath = talloc_asprintf(ctx,
564 client_set_cur_dir(saved_dir);
567 targetpath = clean_name(ctx, targetpath);
569 client_set_cur_dir(saved_dir);
573 status = cli_chkpath(targetcli, targetpath);
574 if (!NT_STATUS_IS_OK(status)) {
575 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
576 client_set_cur_dir(saved_dir);
589 /****************************************************************************
591 ****************************************************************************/
593 static int cmd_cd(void)
598 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
601 d_printf("Current directory is %s\n",client_get_cur_dir());
607 /****************************************************************************
609 ****************************************************************************/
611 static int cmd_cd_oneup(void)
616 /*******************************************************************
617 Decide if a file should be operated on.
618 ********************************************************************/
620 static bool do_this_one(struct file_info *finfo)
626 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
630 if (*client_get_fileselection() &&
631 !mask_match(finfo->name,client_get_fileselection(),false)) {
632 DEBUG(3,("mask_match %s failed\n", finfo->name));
636 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
637 DEBUG(3,("newer_than %s failed\n", finfo->name));
641 if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
642 DEBUG(3,("archive %s failed\n", finfo->name));
649 /****************************************************************************
650 Display info about a file.
651 ****************************************************************************/
653 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
657 TALLOC_CTX *ctx = talloc_tos();
658 NTSTATUS status = NT_STATUS_OK;
660 if (!do_this_one(finfo)) {
664 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
666 d_printf(" %-30s%7.7s %8.0f %s",
668 attrib_string(talloc_tos(), finfo->mode),
671 dir_total += finfo->size;
676 /* skip if this is . or .. */
677 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
679 /* create absolute filename for cli_ntcreate() FIXME */
680 afname = talloc_asprintf(ctx,
686 return NT_STATUS_NO_MEMORY;
688 /* print file meta date header */
689 d_printf( "FILENAME:%s\n", finfo->name);
690 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->mode));
691 d_printf( "SIZE:%.0f\n", (double)finfo->size);
692 d_printf( "MTIME:%s", time_to_asc(t));
693 status = cli_ntcreate(cli_state, afname, 0,
694 CREATE_ACCESS_READ, 0,
695 FILE_SHARE_READ|FILE_SHARE_WRITE,
696 FILE_OPEN, 0x0, 0x0, &fnum);
697 if (!NT_STATUS_IS_OK(status)) {
698 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
699 afname, nt_errstr(status)));
701 struct security_descriptor *sd = NULL;
702 status = cli_query_secdesc(cli_state, fnum,
704 if (!NT_STATUS_IS_OK(status)) {
705 DEBUG( 0, ("display_finfo() failed to "
706 "get security descriptor: %s",
709 display_sec_desc(sd);
718 /****************************************************************************
719 Accumulate size of a file.
720 ****************************************************************************/
722 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
725 if (do_this_one(finfo)) {
726 dir_total += finfo->size;
731 static bool do_list_recurse;
732 static bool do_list_dirs;
733 static char *do_list_queue = 0;
734 static long do_list_queue_size = 0;
735 static long do_list_queue_start = 0;
736 static long do_list_queue_end = 0;
737 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
740 /****************************************************************************
741 Functions for do_list_queue.
742 ****************************************************************************/
745 * The do_list_queue is a NUL-separated list of strings stored in a
746 * char*. Since this is a FIFO, we keep track of the beginning and
747 * ending locations of the data in the queue. When we overflow, we
748 * double the size of the char*. When the start of the data passes
749 * the midpoint, we move everything back. This is logically more
750 * complex than a linked list, but easier from a memory management
751 * angle. In any memory error condition, do_list_queue is reset.
752 * Functions check to ensure that do_list_queue is non-NULL before
756 static void reset_do_list_queue(void)
758 SAFE_FREE(do_list_queue);
759 do_list_queue_size = 0;
760 do_list_queue_start = 0;
761 do_list_queue_end = 0;
764 static void init_do_list_queue(void)
766 reset_do_list_queue();
767 do_list_queue_size = 1024;
768 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
769 if (do_list_queue == 0) {
770 d_printf("malloc fail for size %d\n",
771 (int)do_list_queue_size);
772 reset_do_list_queue();
774 memset(do_list_queue, 0, do_list_queue_size);
778 static void adjust_do_list_queue(void)
781 * If the starting point of the queue is more than half way through,
782 * move everything toward the beginning.
785 if (do_list_queue == NULL) {
786 DEBUG(4,("do_list_queue is empty\n"));
787 do_list_queue_start = do_list_queue_end = 0;
791 if (do_list_queue_start == do_list_queue_end) {
792 DEBUG(4,("do_list_queue is empty\n"));
793 do_list_queue_start = do_list_queue_end = 0;
794 *do_list_queue = '\0';
795 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
796 DEBUG(4,("sliding do_list_queue backward\n"));
797 memmove(do_list_queue,
798 do_list_queue + do_list_queue_start,
799 do_list_queue_end - do_list_queue_start);
800 do_list_queue_end -= do_list_queue_start;
801 do_list_queue_start = 0;
805 static void add_to_do_list_queue(const char *entry)
807 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
808 while (new_end > do_list_queue_size) {
809 do_list_queue_size *= 2;
810 DEBUG(4,("enlarging do_list_queue to %d\n",
811 (int)do_list_queue_size));
812 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
813 if (! do_list_queue) {
814 d_printf("failure enlarging do_list_queue to %d bytes\n",
815 (int)do_list_queue_size);
816 reset_do_list_queue();
818 memset(do_list_queue + do_list_queue_size / 2,
819 0, do_list_queue_size / 2);
823 strlcpy_base(do_list_queue + do_list_queue_end,
824 entry, do_list_queue, do_list_queue_size);
825 do_list_queue_end = new_end;
826 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
827 entry, (int)do_list_queue_start, (int)do_list_queue_end));
831 static char *do_list_queue_head(void)
833 return do_list_queue + do_list_queue_start;
836 static void remove_do_list_queue_head(void)
838 if (do_list_queue_end > do_list_queue_start) {
839 do_list_queue_start += strlen(do_list_queue_head()) + 1;
840 adjust_do_list_queue();
841 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
842 (int)do_list_queue_start, (int)do_list_queue_end));
846 static int do_list_queue_empty(void)
848 return (! (do_list_queue && *do_list_queue));
851 /****************************************************************************
852 A helper for do_list.
853 ****************************************************************************/
855 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
856 const char *mask, void *state)
858 struct cli_state *cli_state = (struct cli_state *)state;
859 TALLOC_CTX *ctx = talloc_tos();
861 char *dir_end = NULL;
862 NTSTATUS status = NT_STATUS_OK;
864 /* Work out the directory. */
865 dir = talloc_strdup(ctx, mask);
867 return NT_STATUS_NO_MEMORY;
869 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
873 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
874 if (do_list_dirs && do_this_one(f)) {
875 status = do_list_fn(cli_state, f, dir);
876 if (!NT_STATUS_IS_OK(status)) {
880 if (do_list_recurse &&
882 !strequal(f->name,".") &&
883 !strequal(f->name,"..")) {
888 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
890 return NT_STATUS_UNSUCCESSFUL;
893 mask2 = talloc_asprintf(ctx,
899 return NT_STATUS_NO_MEMORY;
901 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
907 mask2 = talloc_asprintf_append(mask2,
913 return NT_STATUS_NO_MEMORY;
915 add_to_do_list_queue(mask2);
922 if (do_this_one(f)) {
923 status = do_list_fn(cli_state, f, dir);
929 /****************************************************************************
930 A wrapper around cli_list that adds recursion.
931 ****************************************************************************/
933 NTSTATUS do_list(const char *mask,
935 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
940 static int in_do_list = 0;
941 TALLOC_CTX *ctx = talloc_tos();
942 struct cli_state *targetcli = NULL;
943 char *targetpath = NULL;
944 NTSTATUS ret_status = NT_STATUS_OK;
945 NTSTATUS status = NT_STATUS_OK;
947 if (in_do_list && rec) {
948 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
954 do_list_recurse = rec;
959 init_do_list_queue();
960 add_to_do_list_queue(mask);
962 while (!do_list_queue_empty()) {
964 * Need to copy head so that it doesn't become
965 * invalid inside the call to cli_list. This
966 * would happen if the list were expanded
968 * Fix from E. Jay Berkenbilt (ejb@ql.org)
970 char *head = talloc_strdup(ctx, do_list_queue_head());
973 return NT_STATUS_NO_MEMORY;
978 status = cli_resolve_path(ctx, "", auth_info, cli,
981 if (!NT_STATUS_IS_OK(status)) {
982 d_printf("do_list: [%s] %s\n", head,
984 remove_do_list_queue_head();
988 status = cli_list(targetcli, targetpath, attribute,
989 do_list_helper, targetcli);
990 if (!NT_STATUS_IS_OK(status)) {
991 d_printf("%s listing %s\n",
992 nt_errstr(status), targetpath);
995 remove_do_list_queue_head();
996 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
997 char *next_file = do_list_queue_head();
999 if ((strlen(next_file) >= 2) &&
1000 (next_file[strlen(next_file) - 1] == '*') &&
1001 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
1002 save_ch = next_file +
1003 strlen(next_file) - 2;
1006 /* cwd is only used if showacls is on */
1007 client_set_cwd(next_file);
1010 if (!showacls) /* don't disturbe the showacls output */
1011 d_printf("\n%s\n",next_file);
1013 *save_ch = CLI_DIRSEP_CHAR;
1017 TALLOC_FREE(targetpath);
1021 status = cli_resolve_path(ctx, "", auth_info, cli, mask,
1022 &targetcli, &targetpath);
1023 if (NT_STATUS_IS_OK(status)) {
1024 status = cli_list(targetcli, targetpath, attribute,
1025 do_list_helper, targetcli);
1026 if (!NT_STATUS_IS_OK(status)) {
1027 d_printf("%s listing %s\n",
1028 nt_errstr(status), targetpath);
1029 ret_status = status;
1031 TALLOC_FREE(targetpath);
1033 d_printf("do_list: [%s] %s\n", mask, nt_errstr(status));
1034 ret_status = status;
1039 reset_do_list_queue();
1043 /****************************************************************************
1044 Get a directory listing.
1045 ****************************************************************************/
1047 static int cmd_dir(void)
1049 TALLOC_CTX *ctx = talloc_tos();
1050 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1057 mask = talloc_strdup(ctx, client_get_cur_dir());
1062 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1063 normalize_name(buf);
1064 if (*buf == CLI_DIRSEP_CHAR) {
1065 mask = talloc_strdup(ctx, buf);
1067 mask = talloc_asprintf_append(mask, "%s", buf);
1070 mask = talloc_asprintf_append(mask, "*");
1077 /* cwd is only used if showacls is on */
1078 client_set_cwd(client_get_cur_dir());
1081 status = do_list(mask, attribute, display_finfo, recurse, true);
1082 if (!NT_STATUS_IS_OK(status)) {
1088 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
1093 /****************************************************************************
1094 Get a directory listing.
1095 ****************************************************************************/
1097 static int cmd_du(void)
1099 TALLOC_CTX *ctx = talloc_tos();
1100 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1107 mask = talloc_strdup(ctx, client_get_cur_dir());
1111 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
1112 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
1118 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1119 normalize_name(buf);
1120 if (*buf == CLI_DIRSEP_CHAR) {
1121 mask = talloc_strdup(ctx, buf);
1123 mask = talloc_asprintf_append(mask, "%s", buf);
1126 mask = talloc_strdup(ctx, "*");
1129 status = do_list(mask, attribute, do_du, recurse, true);
1130 if (!NT_STATUS_IS_OK(status)) {
1136 d_printf("Total number of bytes: %.0f\n", dir_total);
1141 static int cmd_echo(void)
1143 TALLOC_CTX *ctx = talloc_tos();
1148 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1149 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1150 d_printf("echo <num> <data>\n");
1154 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1156 if (!NT_STATUS_IS_OK(status)) {
1157 d_printf("echo failed: %s\n", nt_errstr(status));
1164 /****************************************************************************
1165 Get a file from rname to lname
1166 ****************************************************************************/
1168 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1170 int *pfd = (int *)priv;
1171 if (writefile(*pfd, buf, n) == -1) {
1172 return map_nt_error_from_unix(errno);
1174 return NT_STATUS_OK;
1177 static int do_get(const char *rname, const char *lname_in, bool reget)
1179 TALLOC_CTX *ctx = talloc_tos();
1182 bool newhandle = false;
1183 struct timespec tp_start;
1187 SMB_OFF_T nread = 0;
1189 struct cli_state *targetcli = NULL;
1190 char *targetname = NULL;
1194 lname = talloc_strdup(ctx, lname_in);
1203 status = cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli,
1205 if (!NT_STATUS_IS_OK(status)) {
1206 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1210 clock_gettime_mono(&tp_start);
1212 status = smbclient_cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1213 if (!NT_STATUS_IS_OK(status)) {
1214 d_printf("%s opening remote file %s\n", nt_errstr(status),
1219 if(!strcmp(lname,"-")) {
1220 handle = fileno(stdout);
1223 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
1225 start = sys_lseek(handle, 0, SEEK_END);
1227 d_printf("Error seeking local file\n");
1232 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1237 d_printf("Error opening local file %s\n",lname);
1242 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1244 if (!NT_STATUS_IS_OK(status)) {
1245 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1247 if(!NT_STATUS_IS_OK(status)) {
1248 d_printf("getattrib: %s\n", nt_errstr(status));
1253 DEBUG(1,("getting file %s of size %.0f as %s ",
1254 rname, (double)size, lname));
1256 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1257 writefile_sink, (void *)&handle, &nread);
1258 if (!NT_STATUS_IS_OK(status)) {
1259 d_fprintf(stderr, "parallel_read returned %s\n",
1261 cli_close(targetcli, fnum);
1265 status = cli_close(targetcli, fnum);
1266 if (!NT_STATUS_IS_OK(status)) {
1267 d_printf("Error %s closing remote file\n", nt_errstr(status));
1275 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1276 cli_setatr(cli, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
1280 struct timespec tp_end;
1283 clock_gettime_mono(&tp_end);
1284 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1285 get_total_time_ms += this_time;
1286 get_total_size += nread;
1288 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1289 nread / (1.024*this_time + 1.0e-4),
1290 get_total_size / (1.024*get_total_time_ms)));
1293 TALLOC_FREE(targetname);
1297 /****************************************************************************
1299 ****************************************************************************/
1301 static int cmd_get(void)
1303 TALLOC_CTX *ctx = talloc_tos();
1308 rname = talloc_strdup(ctx, client_get_cur_dir());
1313 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1314 d_printf("get <filename> [localname]\n");
1317 rname = talloc_asprintf_append(rname, "%s", fname);
1321 rname = clean_name(ctx, rname);
1326 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1331 return do_get(rname, lname, false);
1334 /****************************************************************************
1335 Do an mget operation on one file.
1336 ****************************************************************************/
1338 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1341 TALLOC_CTX *ctx = talloc_tos();
1342 NTSTATUS status = NT_STATUS_OK;
1345 char *saved_curdir = NULL;
1346 char *mget_mask = NULL;
1347 char *new_cd = NULL;
1350 return NT_STATUS_OK;
1353 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1354 return NT_STATUS_OK;
1357 d_printf("mget aborted\n");
1358 return NT_STATUS_UNSUCCESSFUL;
1361 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
1362 if (asprintf(&quest,
1363 "Get directory %s? ",finfo->name) < 0) {
1364 return NT_STATUS_NO_MEMORY;
1367 if (asprintf(&quest,
1368 "Get file %s? ",finfo->name) < 0) {
1369 return NT_STATUS_NO_MEMORY;
1373 if (prompt && !yesno(quest)) {
1375 return NT_STATUS_OK;
1379 if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
1380 rname = talloc_asprintf(ctx,
1382 client_get_cur_dir(),
1385 return NT_STATUS_NO_MEMORY;
1387 do_get(rname, finfo->name, false);
1389 return NT_STATUS_OK;
1392 /* handle directories */
1393 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1394 if (!saved_curdir) {
1395 return NT_STATUS_NO_MEMORY;
1398 new_cd = talloc_asprintf(ctx,
1400 client_get_cur_dir(),
1404 return NT_STATUS_NO_MEMORY;
1406 client_set_cur_dir(new_cd);
1408 string_replace(finfo->name,'\\','/');
1410 strlower_m(finfo->name);
1413 if (!directory_exist(finfo->name) &&
1414 mkdir(finfo->name,0777) != 0) {
1415 d_printf("failed to create directory %s\n",finfo->name);
1416 client_set_cur_dir(saved_curdir);
1417 return map_nt_error_from_unix(errno);
1420 if (chdir(finfo->name) != 0) {
1421 d_printf("failed to chdir to directory %s\n",finfo->name);
1422 client_set_cur_dir(saved_curdir);
1423 return map_nt_error_from_unix(errno);
1426 mget_mask = talloc_asprintf(ctx,
1428 client_get_cur_dir());
1431 return NT_STATUS_NO_MEMORY;
1434 status = do_list(mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,false, true);
1435 if (!NT_STATUS_IS_OK(status)) {
1439 if (chdir("..") == -1) {
1440 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1442 return map_nt_error_from_unix(errno);
1444 client_set_cur_dir(saved_curdir);
1445 TALLOC_FREE(mget_mask);
1446 TALLOC_FREE(saved_curdir);
1447 TALLOC_FREE(new_cd);
1448 return NT_STATUS_OK;
1451 /****************************************************************************
1452 View the file using the pager.
1453 ****************************************************************************/
1455 static int cmd_more(void)
1457 TALLOC_CTX *ctx = talloc_tos();
1461 char *pager_cmd = NULL;
1466 rname = talloc_strdup(ctx, client_get_cur_dir());
1471 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1475 fd = mkstemp(lname);
1477 d_printf("failed to create temporary file for more\n");
1482 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1483 d_printf("more <filename>\n");
1487 rname = talloc_asprintf_append(rname, "%s", fname);
1491 rname = clean_name(ctx,rname);
1496 rc = do_get(rname, lname, false);
1498 pager=getenv("PAGER");
1500 pager_cmd = talloc_asprintf(ctx,
1502 (pager? pager:PAGER),
1507 if (system(pager_cmd) == -1) {
1508 d_printf("system command '%s' returned -1\n",
1516 /****************************************************************************
1518 ****************************************************************************/
1520 static int cmd_mget(void)
1522 TALLOC_CTX *ctx = talloc_tos();
1523 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1524 char *mget_mask = NULL;
1526 NTSTATUS status = NT_STATUS_OK;
1529 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1534 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1536 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1540 if (*buf == CLI_DIRSEP_CHAR) {
1541 mget_mask = talloc_strdup(ctx, buf);
1543 mget_mask = talloc_asprintf_append(mget_mask,
1549 status = do_list(mget_mask, attribute, do_mget, false, true);
1550 if (!NT_STATUS_IS_OK(status)) {
1555 if (mget_mask == NULL) {
1556 d_printf("nothing to mget\n");
1561 mget_mask = talloc_asprintf(ctx,
1563 client_get_cur_dir());
1567 status = do_list(mget_mask, attribute, do_mget, false, true);
1568 if (!NT_STATUS_IS_OK(status)) {
1576 /****************************************************************************
1577 Make a directory of name "name".
1578 ****************************************************************************/
1580 static bool do_mkdir(const char *name)
1582 TALLOC_CTX *ctx = talloc_tos();
1583 struct cli_state *targetcli;
1584 char *targetname = NULL;
1587 status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
1589 if (!NT_STATUS_IS_OK(status)) {
1590 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1594 status = cli_mkdir(targetcli, targetname);
1595 if (!NT_STATUS_IS_OK(status)) {
1596 d_printf("%s making remote directory %s\n",
1597 nt_errstr(status),name);
1604 /****************************************************************************
1605 Show 8.3 name of a file.
1606 ****************************************************************************/
1608 static bool do_altname(const char *name)
1613 status = cli_qpathinfo_alt_name(cli, name, altname);
1614 if (!NT_STATUS_IS_OK(status)) {
1615 d_printf("%s getting alt name for %s\n",
1616 nt_errstr(status),name);
1619 d_printf("%s\n", altname);
1624 /****************************************************************************
1626 ****************************************************************************/
1628 static int cmd_quit(void)
1636 /****************************************************************************
1638 ****************************************************************************/
1640 static int cmd_mkdir(void)
1642 TALLOC_CTX *ctx = talloc_tos();
1647 mask = talloc_strdup(ctx, client_get_cur_dir());
1652 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1654 d_printf("mkdir <dirname>\n");
1658 mask = talloc_asprintf_append(mask, "%s", buf);
1666 struct cli_state *targetcli;
1667 char *targetname = NULL;
1671 ddir2 = talloc_strdup(ctx, "");
1676 status = cli_resolve_path(ctx, "", auth_info, cli, mask,
1677 &targetcli, &targetname);
1678 if (!NT_STATUS_IS_OK(status)) {
1682 ddir = talloc_strdup(ctx, targetname);
1686 trim_char(ddir,'.','\0');
1687 p = strtok_r(ddir, "/\\", &saveptr);
1689 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1693 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1696 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1700 p = strtok_r(NULL, "/\\", &saveptr);
1709 /****************************************************************************
1711 ****************************************************************************/
1713 static int cmd_altname(void)
1715 TALLOC_CTX *ctx = talloc_tos();
1719 name = talloc_strdup(ctx, client_get_cur_dir());
1724 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1725 d_printf("altname <file>\n");
1728 name = talloc_asprintf_append(name, "%s", buf);
1736 static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1738 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1741 if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1742 if (mode & FILE_ATTRIBUTE_ENCRYPTED) {
1745 if (mode & FILE_ATTRIBUTE_NONINDEXED) {
1748 if (mode & FILE_ATTRIBUTE_OFFLINE) {
1751 if (mode & FILE_ATTRIBUTE_COMPRESSED) {
1754 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1757 if (mode & FILE_ATTRIBUTE_SPARSE) {
1760 if (mode & FILE_ATTRIBUTE_TEMPORARY) {
1763 if (mode & FILE_ATTRIBUTE_NORMAL) {
1766 if (mode & FILE_ATTRIBUTE_READONLY) {
1769 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1772 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1775 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1778 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1785 /****************************************************************************
1786 Show all info we can get
1787 ****************************************************************************/
1789 static int do_allinfo(const char *name)
1792 struct timespec b_time, a_time, m_time, c_time;
1798 unsigned int num_streams;
1799 struct stream_struct *streams;
1805 status = cli_qpathinfo_alt_name(cli, name, altname);
1806 if (!NT_STATUS_IS_OK(status)) {
1807 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1811 d_printf("altname: %s\n", altname);
1813 status = cli_qpathinfo2(cli, name, &b_time, &a_time, &m_time, &c_time,
1814 &size, &mode, &ino);
1815 if (!NT_STATUS_IS_OK(status)) {
1816 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1821 unix_timespec_to_nt_time(&tmp, b_time);
1822 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1824 unix_timespec_to_nt_time(&tmp, a_time);
1825 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1827 unix_timespec_to_nt_time(&tmp, m_time);
1828 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1830 unix_timespec_to_nt_time(&tmp, c_time);
1831 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1833 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
1835 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1837 if (!NT_STATUS_IS_OK(status)) {
1838 d_printf("%s getting streams for %s\n", nt_errstr(status),
1843 for (i=0; i<num_streams; i++) {
1844 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1845 (unsigned long long)streams[i].size);
1848 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1849 char *subst, *print;
1852 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1854 if (!NT_STATUS_IS_OK(status)) {
1855 d_fprintf(stderr, "cli_readlink returned %s\n",
1858 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1859 subst, print, flags);
1865 status = cli_ntcreate(cli, name, 0,
1866 CREATE_ACCESS_READ, 0,
1867 FILE_SHARE_READ|FILE_SHARE_WRITE
1869 FILE_OPEN, 0x0, 0x0, &fnum);
1870 if (!NT_STATUS_IS_OK(status)) {
1872 * Ignore failure, it does not hurt if we can't list
1877 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1878 true, &snapshots, &num_snapshots);
1879 if (!NT_STATUS_IS_OK(status)) {
1880 cli_close(cli, fnum);
1884 for (i=0; i<num_snapshots; i++) {
1887 d_printf("%s\n", snapshots[i]);
1888 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1889 snapshots[i], name);
1890 status = cli_qpathinfo2(cli, snap_name, &b_time, &a_time,
1891 &m_time, &c_time, &size,
1893 if (!NT_STATUS_IS_OK(status)) {
1894 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1895 snap_name, nt_errstr(status));
1896 TALLOC_FREE(snap_name);
1899 unix_timespec_to_nt_time(&tmp, b_time);
1900 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1901 unix_timespec_to_nt_time(&tmp, a_time);
1902 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1903 unix_timespec_to_nt_time(&tmp, m_time);
1904 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1905 unix_timespec_to_nt_time(&tmp, c_time);
1906 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1907 d_printf("size: %d\n", (int)size);
1910 TALLOC_FREE(snapshots);
1915 /****************************************************************************
1916 Show all info we can get
1917 ****************************************************************************/
1919 static int cmd_allinfo(void)
1921 TALLOC_CTX *ctx = talloc_tos();
1925 name = talloc_strdup(ctx, client_get_cur_dir());
1930 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1931 d_printf("allinfo <file>\n");
1934 name = talloc_asprintf_append(name, "%s", buf);
1944 /****************************************************************************
1946 ****************************************************************************/
1948 static int do_put(const char *rname, const char *lname, bool reput)
1950 TALLOC_CTX *ctx = talloc_tos();
1953 SMB_OFF_T start = 0;
1955 struct timespec tp_start;
1956 struct cli_state *targetcli;
1957 char *targetname = NULL;
1958 struct push_state state;
1961 status = cli_resolve_path(ctx, "", auth_info, cli, rname,
1962 &targetcli, &targetname);
1963 if (!NT_STATUS_IS_OK(status)) {
1964 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1968 clock_gettime_mono(&tp_start);
1971 status = smbclient_cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1972 if (NT_STATUS_IS_OK(status)) {
1973 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1974 targetcli, fnum, NULL,
1976 NULL, NULL, NULL)) &&
1977 !NT_STATUS_IS_OK(status = cli_getattrE(
1978 targetcli, fnum, NULL,
1981 d_printf("getattrib: %s\n", nt_errstr(status));
1986 status = smbclient_cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1989 if (!NT_STATUS_IS_OK(status)) {
1990 d_printf("%s opening remote file %s\n", nt_errstr(status),
1995 /* allow files to be piped into smbclient
1998 Note that in this case this function will exit(0) rather
2000 if (!strcmp(lname, "-")) {
2002 /* size of file is not known */
2004 f = x_fopen(lname,O_RDONLY, 0);
2006 if (x_tseek(f, start, SEEK_SET) == -1) {
2007 d_printf("Error seeking local file\n");
2015 d_printf("Error opening local file %s\n",lname);
2019 DEBUG(1,("putting file %s as %s ",lname,
2022 x_setvbuf(f, NULL, X_IOFBF, io_bufsize);
2027 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
2029 if (!NT_STATUS_IS_OK(status)) {
2030 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
2034 status = cli_close(targetcli, fnum);
2035 if (!NT_STATUS_IS_OK(status)) {
2036 d_printf("%s closing remote file %s\n", nt_errstr(status),
2049 struct timespec tp_end;
2052 clock_gettime_mono(&tp_end);
2053 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
2054 put_total_time_ms += this_time;
2055 put_total_size += state.nread;
2057 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
2058 state.nread / (1.024*this_time + 1.0e-4),
2059 put_total_size / (1.024*put_total_time_ms)));
2070 /****************************************************************************
2072 ****************************************************************************/
2074 static int cmd_put(void)
2076 TALLOC_CTX *ctx = talloc_tos();
2081 rname = talloc_strdup(ctx, client_get_cur_dir());
2086 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
2087 d_printf("put <filename>\n");
2091 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2092 rname = talloc_asprintf_append(rname, "%s", buf);
2094 rname = talloc_asprintf_append(rname, "%s", lname);
2100 rname = clean_name(ctx, rname);
2107 /* allow '-' to represent stdin
2108 jdblair, 24.jun.98 */
2109 if (!file_exist_stat(lname, &st, false) &&
2110 (strcmp(lname,"-"))) {
2111 d_printf("%s does not exist\n",lname);
2116 return do_put(rname, lname, false);
2119 /*************************************
2120 File list structure.
2121 *************************************/
2123 static struct file_list {
2124 struct file_list *prev, *next;
2129 /****************************************************************************
2130 Free a file_list structure.
2131 ****************************************************************************/
2133 static void free_file_list (struct file_list *l_head)
2135 struct file_list *list, *next;
2137 for (list = l_head; list; list = next) {
2139 DLIST_REMOVE(l_head, list);
2140 SAFE_FREE(list->file_path);
2145 /****************************************************************************
2146 Seek in a directory/file list until you get something that doesn't start with
2148 ****************************************************************************/
2150 static bool seek_list(struct file_list *list, char *name)
2153 trim_string(list->file_path,"./","\n");
2154 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2163 /****************************************************************************
2164 Set the file selection mask.
2165 ****************************************************************************/
2167 static int cmd_select(void)
2169 TALLOC_CTX *ctx = talloc_tos();
2170 char *new_fs = NULL;
2171 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2174 client_set_fileselection(new_fs);
2176 client_set_fileselection("");
2181 /****************************************************************************
2182 Recursive file matching function act as find
2183 match must be always set to true when calling this function
2184 ****************************************************************************/
2186 static int file_find(struct file_list **list, const char *directory,
2187 const char *expression, bool match)
2189 SMB_STRUCT_DIR *dir;
2190 struct file_list *entry;
2191 struct stat statbuf;
2197 dir = sys_opendir(directory);
2201 while ((dname = readdirname(dir))) {
2202 if (!strcmp("..", dname))
2204 if (!strcmp(".", dname))
2207 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
2212 if (!match || !gen_fnmatch(expression, dname)) {
2214 ret = stat(path, &statbuf);
2216 if (S_ISDIR(statbuf.st_mode)) {
2218 ret = file_find(list, path, expression, false);
2221 d_printf("file_find: cannot stat file %s\n", path);
2230 entry = SMB_MALLOC_P(struct file_list);
2232 d_printf("Out of memory in file_find\n");
2236 entry->file_path = path;
2237 entry->isdir = isdir;
2238 DLIST_ADD(*list, entry);
2248 /****************************************************************************
2250 ****************************************************************************/
2252 static int cmd_mput(void)
2254 TALLOC_CTX *ctx = talloc_tos();
2257 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2259 struct file_list *temp_list;
2260 char *quest, *lname, *rname;
2264 ret = file_find(&file_list, ".", p, true);
2266 free_file_list(file_list);
2274 for (temp_list = file_list; temp_list;
2275 temp_list = temp_list->next) {
2278 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2281 trim_string(lname, "./", "/");
2283 /* check if it's a directory */
2284 if (temp_list->isdir) {
2285 /* if (!recurse) continue; */
2288 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2291 if (prompt && !yesno(quest)) { /* No */
2292 /* Skip the directory */
2293 lname[strlen(lname)-1] = '/';
2294 if (!seek_list(temp_list, lname))
2298 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2301 normalize_name(rname);
2302 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2304 DEBUG (0, ("Unable to make dir, skipping..."));
2305 /* Skip the directory */
2306 lname[strlen(lname)-1] = '/';
2307 if (!seek_list(temp_list, lname)) {
2315 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2318 if (prompt && !yesno(quest)) {
2325 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2330 normalize_name(rname);
2332 do_put(rname, lname, false);
2334 free_file_list(file_list);
2343 /****************************************************************************
2345 ****************************************************************************/
2347 static int do_cancel(int job)
2349 if (cli_printjob_del(cli, job)) {
2350 d_printf("Job %d cancelled\n",job);
2353 NTSTATUS status = cli_nt_error(cli);
2354 d_printf("Error cancelling job %d : %s\n",
2355 job, nt_errstr(status));
2360 /****************************************************************************
2362 ****************************************************************************/
2364 static int cmd_cancel(void)
2366 TALLOC_CTX *ctx = talloc_tos();
2370 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2371 d_printf("cancel <jobid> ...\n");
2377 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2382 /****************************************************************************
2384 ****************************************************************************/
2386 static int cmd_print(void)
2388 TALLOC_CTX *ctx = talloc_tos();
2393 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2394 d_printf("print <filename>\n");
2398 rname = talloc_strdup(ctx, lname);
2402 p = strrchr_m(rname,'/');
2404 rname = talloc_asprintf(ctx,
2409 if (strequal(lname,"-")) {
2410 rname = talloc_asprintf(ctx,
2418 return do_put(rname, lname, false);
2421 /****************************************************************************
2422 Show a print queue entry.
2423 ****************************************************************************/
2425 static void queue_fn(struct print_job_info *p)
2427 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2430 /****************************************************************************
2432 ****************************************************************************/
2434 static int cmd_queue(void)
2436 cli_print_queue(cli, queue_fn);
2440 /****************************************************************************
2442 ****************************************************************************/
2444 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2447 TALLOC_CTX *ctx = talloc_tos();
2451 mask = talloc_asprintf(ctx,
2457 return NT_STATUS_NO_MEMORY;
2460 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2462 return NT_STATUS_OK;
2465 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2466 if (!NT_STATUS_IS_OK(status)) {
2467 d_printf("%s deleting remote file %s\n",
2468 nt_errstr(status), mask);
2474 /****************************************************************************
2476 ****************************************************************************/
2478 static int cmd_del(void)
2480 TALLOC_CTX *ctx = talloc_tos();
2483 NTSTATUS status = NT_STATUS_OK;
2484 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2487 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2490 mask = talloc_strdup(ctx, client_get_cur_dir());
2494 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2495 d_printf("del <filename>\n");
2498 mask = talloc_asprintf_append(mask, "%s", buf);
2503 status = do_list(mask,attribute,do_del,false,false);
2504 if (!NT_STATUS_IS_OK(status)) {
2510 /****************************************************************************
2511 Wildcard delete some files.
2512 ****************************************************************************/
2514 static int cmd_wdel(void)
2516 TALLOC_CTX *ctx = talloc_tos();
2520 struct cli_state *targetcli;
2521 char *targetname = NULL;
2524 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2525 d_printf("wdel 0x<attrib> <wcard>\n");
2529 attribute = (uint16)strtol(buf, (char **)NULL, 16);
2531 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2532 d_printf("wdel 0x<attrib> <wcard>\n");
2536 mask = talloc_asprintf(ctx, "%s%s",
2537 client_get_cur_dir(),
2543 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2545 if (!NT_STATUS_IS_OK(status)) {
2546 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2550 status = cli_unlink(targetcli, targetname, attribute);
2551 if (!NT_STATUS_IS_OK(status)) {
2552 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2558 /****************************************************************************
2559 ****************************************************************************/
2561 static int cmd_open(void)
2563 TALLOC_CTX *ctx = talloc_tos();
2566 char *targetname = NULL;
2567 struct cli_state *targetcli;
2568 uint16_t fnum = (uint16_t)-1;
2571 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2572 d_printf("open <filename>\n");
2575 mask = talloc_asprintf(ctx,
2577 client_get_cur_dir(),
2583 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2585 if (!NT_STATUS_IS_OK(status)) {
2586 d_printf("open %s: %s\n", mask, nt_errstr(status));
2590 status = cli_ntcreate(targetcli, targetname, 0,
2591 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2592 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2594 if (!NT_STATUS_IS_OK(status)) {
2595 status = cli_ntcreate(targetcli, targetname, 0,
2597 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2599 if (NT_STATUS_IS_OK(status)) {
2600 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2602 d_printf("Failed to open file %s. %s\n",
2603 targetname, nt_errstr(status));
2606 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2611 static int cmd_posix_encrypt(void)
2613 TALLOC_CTX *ctx = talloc_tos();
2614 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2616 if (cli->use_kerberos) {
2617 status = cli_gss_smb_encryption_start(cli);
2619 char *domain = NULL;
2621 char *password = NULL;
2623 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2624 d_printf("posix_encrypt domain user password\n");
2628 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2629 d_printf("posix_encrypt domain user password\n");
2633 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2634 d_printf("posix_encrypt domain user password\n");
2638 status = cli_raw_ntlm_smb_encryption_start(cli,
2644 if (!NT_STATUS_IS_OK(status)) {
2645 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2647 d_printf("encryption on\n");
2654 /****************************************************************************
2655 ****************************************************************************/
2657 static int cmd_posix_open(void)
2659 TALLOC_CTX *ctx = talloc_tos();
2662 char *targetname = NULL;
2663 struct cli_state *targetcli;
2668 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2669 d_printf("posix_open <filename> 0<mode>\n");
2672 mask = talloc_asprintf(ctx,
2674 client_get_cur_dir(),
2680 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2681 d_printf("posix_open <filename> 0<mode>\n");
2684 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2686 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2688 if (!NT_STATUS_IS_OK(status)) {
2689 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2693 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2695 if (!NT_STATUS_IS_OK(status)) {
2696 status = cli_posix_open(targetcli, targetname,
2697 O_CREAT|O_RDONLY, mode, &fnum);
2698 if (!NT_STATUS_IS_OK(status)) {
2699 d_printf("Failed to open file %s. %s\n", targetname,
2702 d_printf("posix_open file %s: for readonly fnum %d\n",
2706 d_printf("posix_open file %s: for read/write fnum %d\n",
2713 static int cmd_posix_mkdir(void)
2715 TALLOC_CTX *ctx = talloc_tos();
2718 char *targetname = NULL;
2719 struct cli_state *targetcli;
2723 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2724 d_printf("posix_mkdir <filename> 0<mode>\n");
2727 mask = talloc_asprintf(ctx,
2729 client_get_cur_dir(),
2735 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2736 d_printf("posix_mkdir <filename> 0<mode>\n");
2739 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2741 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2743 if (!NT_STATUS_IS_OK(status)) {
2744 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2748 status = cli_posix_mkdir(targetcli, targetname, mode);
2749 if (!NT_STATUS_IS_OK(status)) {
2750 d_printf("Failed to open file %s. %s\n",
2751 targetname, nt_errstr(status));
2753 d_printf("posix_mkdir created directory %s\n", targetname);
2758 static int cmd_posix_unlink(void)
2760 TALLOC_CTX *ctx = talloc_tos();
2763 char *targetname = NULL;
2764 struct cli_state *targetcli;
2767 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2768 d_printf("posix_unlink <filename>\n");
2771 mask = talloc_asprintf(ctx,
2773 client_get_cur_dir(),
2779 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2781 if (!NT_STATUS_IS_OK(status)) {
2782 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
2786 status = cli_posix_unlink(targetcli, targetname);
2787 if (!NT_STATUS_IS_OK(status)) {
2788 d_printf("Failed to unlink file %s. %s\n",
2789 targetname, nt_errstr(status));
2791 d_printf("posix_unlink deleted file %s\n", targetname);
2797 static int cmd_posix_rmdir(void)
2799 TALLOC_CTX *ctx = talloc_tos();
2802 char *targetname = NULL;
2803 struct cli_state *targetcli;
2806 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2807 d_printf("posix_rmdir <filename>\n");
2810 mask = talloc_asprintf(ctx,
2812 client_get_cur_dir(),
2818 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2820 if (!NT_STATUS_IS_OK(status)) {
2821 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
2825 status = cli_posix_rmdir(targetcli, targetname);
2826 if (!NT_STATUS_IS_OK(status)) {
2827 d_printf("Failed to unlink directory %s. %s\n",
2828 targetname, nt_errstr(status));
2830 d_printf("posix_rmdir deleted directory %s\n", targetname);
2836 static int cmd_close(void)
2838 TALLOC_CTX *ctx = talloc_tos();
2843 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2844 d_printf("close <fnum>\n");
2849 /* We really should use the targetcli here.... */
2850 status = cli_close(cli, fnum);
2851 if (!NT_STATUS_IS_OK(status)) {
2852 d_printf("close %d: %s\n", fnum, nt_errstr(status));
2858 static int cmd_posix(void)
2860 TALLOC_CTX *ctx = talloc_tos();
2861 uint16 major, minor;
2862 uint32 caplow, caphigh;
2866 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2867 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2871 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
2873 if (!NT_STATUS_IS_OK(status)) {
2874 d_printf("Can't get UNIX CIFS extensions version from "
2875 "server: %s\n", nt_errstr(status));
2879 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2881 caps = talloc_strdup(ctx, "");
2885 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2886 caps = talloc_asprintf_append(caps, "locks ");
2891 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2892 caps = talloc_asprintf_append(caps, "acls ");
2897 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2898 caps = talloc_asprintf_append(caps, "eas ");
2903 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2904 caps = talloc_asprintf_append(caps, "pathnames ");
2909 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2910 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2915 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2916 caps = talloc_asprintf_append(caps, "large_read ");
2921 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2922 caps = talloc_asprintf_append(caps, "large_write ");
2927 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2928 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2933 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2934 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2940 if (*caps && caps[strlen(caps)-1] == ' ') {
2941 caps[strlen(caps)-1] = '\0';
2944 d_printf("Server supports CIFS capabilities %s\n", caps);
2946 status = cli_set_unix_extensions_capabilities(cli, major, minor,
2948 if (!NT_STATUS_IS_OK(status)) {
2949 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2954 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2955 CLI_DIRSEP_CHAR = '/';
2956 *CLI_DIRSEP_STR = '/';
2957 client_set_cur_dir(CLI_DIRSEP_STR);
2963 static int cmd_lock(void)
2965 TALLOC_CTX *ctx = talloc_tos();
2967 uint64_t start, len;
2968 enum brl_type lock_type;
2972 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2973 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2978 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2979 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2983 if (*buf == 'r' || *buf == 'R') {
2984 lock_type = READ_LOCK;
2985 } else if (*buf == 'w' || *buf == 'W') {
2986 lock_type = WRITE_LOCK;
2988 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2992 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2993 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2997 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2999 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3000 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3004 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3006 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
3007 if (!NT_STATUS_IS_OK(status)) {
3008 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
3014 static int cmd_unlock(void)
3016 TALLOC_CTX *ctx = talloc_tos();
3018 uint64_t start, len;
3022 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3023 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3028 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3029 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3033 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3035 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3036 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3040 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3042 status = cli_posix_unlock(cli, fnum, start, len);
3043 if (!NT_STATUS_IS_OK(status)) {
3044 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
3051 /****************************************************************************
3053 ****************************************************************************/
3055 static int cmd_rmdir(void)
3057 TALLOC_CTX *ctx = talloc_tos();
3060 char *targetname = NULL;
3061 struct cli_state *targetcli;
3064 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3065 d_printf("rmdir <dirname>\n");
3068 mask = talloc_asprintf(ctx,
3070 client_get_cur_dir(),
3076 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
3078 if (!NT_STATUS_IS_OK(status)) {
3079 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3083 status = cli_rmdir(targetcli, targetname);
3084 if (!NT_STATUS_IS_OK(status)) {
3085 d_printf("%s removing remote directory file %s\n",
3086 nt_errstr(status), mask);
3092 /****************************************************************************
3094 ****************************************************************************/
3096 static int cmd_link(void)
3098 TALLOC_CTX *ctx = talloc_tos();
3099 char *oldname = NULL;
3100 char *newname = NULL;
3103 char *targetname = NULL;
3104 struct cli_state *targetcli;
3107 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3108 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3109 d_printf("link <oldname> <newname>\n");
3112 oldname = talloc_asprintf(ctx,
3114 client_get_cur_dir(),
3119 newname = talloc_asprintf(ctx,
3121 client_get_cur_dir(),
3127 status = cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli,
3129 if (!NT_STATUS_IS_OK(status)) {
3130 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3134 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3135 d_printf("Server doesn't support UNIX CIFS calls.\n");
3139 status = cli_posix_hardlink(targetcli, targetname, newname);
3140 if (!NT_STATUS_IS_OK(status)) {
3141 d_printf("%s linking files (%s -> %s)\n",
3142 nt_errstr(status), newname, oldname);
3148 /****************************************************************************
3150 ****************************************************************************/
3152 static int cmd_readlink(void)
3154 TALLOC_CTX *ctx = talloc_tos();
3157 char *targetname = NULL;
3158 char linkname[PATH_MAX+1];
3159 struct cli_state *targetcli;
3162 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3163 d_printf("readlink <name>\n");
3166 name = talloc_asprintf(ctx,
3168 client_get_cur_dir(),
3174 status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
3176 if (!NT_STATUS_IS_OK(status)) {
3177 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3181 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3182 d_printf("Server doesn't support UNIX CIFS calls.\n");
3186 status = cli_posix_readlink(targetcli, name, linkname, PATH_MAX+1);
3187 if (!NT_STATUS_IS_OK(status)) {
3188 d_printf("%s readlink on file %s\n",
3189 nt_errstr(status), name);
3193 d_printf("%s -> %s\n", name, linkname);
3199 /****************************************************************************
3201 ****************************************************************************/
3203 static int cmd_symlink(void)
3205 TALLOC_CTX *ctx = talloc_tos();
3206 char *oldname = NULL;
3207 char *newname = NULL;
3210 struct cli_state *newcli;
3213 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3214 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3215 d_printf("symlink <oldname> <newname>\n");
3218 /* Oldname (link target) must be an untouched blob. */
3221 if (SERVER_HAS_UNIX_CIFS(cli)) {
3222 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3227 /* New name must be present in share namespace. */
3228 status = cli_resolve_path(ctx, "", auth_info, cli, newname,
3230 if (!NT_STATUS_IS_OK(status)) {
3231 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3234 status = cli_posix_symlink(newcli, oldname, newname);
3236 status = cli_symlink(
3238 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3241 if (!NT_STATUS_IS_OK(status)) {
3242 d_printf("%s symlinking files (%s -> %s)\n",
3243 nt_errstr(status), oldname, newname);
3250 /****************************************************************************
3252 ****************************************************************************/
3254 static int cmd_chmod(void)
3256 TALLOC_CTX *ctx = talloc_tos();
3260 char *targetname = NULL;
3261 struct cli_state *targetcli;
3265 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3266 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3267 d_printf("chmod mode file\n");
3270 src = talloc_asprintf(ctx,
3272 client_get_cur_dir(),
3278 mode = (mode_t)strtol(buf, NULL, 8);
3280 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3282 if (!NT_STATUS_IS_OK(status)) {
3283 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3287 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3288 d_printf("Server doesn't support UNIX CIFS calls.\n");
3292 status = cli_posix_chmod(targetcli, targetname, mode);
3293 if (!NT_STATUS_IS_OK(status)) {
3294 d_printf("%s chmod file %s 0%o\n",
3295 nt_errstr(status), src, (unsigned int)mode);
3302 static const char *filetype_to_str(mode_t mode)
3304 if (S_ISREG(mode)) {
3305 return "regular file";
3306 } else if (S_ISDIR(mode)) {
3310 if (S_ISCHR(mode)) {
3311 return "character device";
3315 if (S_ISBLK(mode)) {
3316 return "block device";
3320 if (S_ISFIFO(mode)) {
3325 if (S_ISLNK(mode)) {
3326 return "symbolic link";
3330 if (S_ISSOCK(mode)) {
3337 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3346 static char *unix_mode_to_str(char *s, mode_t m)
3349 const char *str = filetype_to_str(m);
3365 *p++ = str[1] == 'y' ? 'l' : 's';
3372 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3373 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3374 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3375 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3376 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3377 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3378 *p++ = rwx_to_str(m, S_IROTH, 'r');
3379 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3380 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3385 /****************************************************************************
3386 Utility function for UNIX getfacl.
3387 ****************************************************************************/
3389 static char *perms_to_string(fstring permstr, unsigned char perms)
3391 fstrcpy(permstr, "---");
3392 if (perms & SMB_POSIX_ACL_READ) {
3395 if (perms & SMB_POSIX_ACL_WRITE) {
3398 if (perms & SMB_POSIX_ACL_EXECUTE) {
3404 /****************************************************************************
3406 ****************************************************************************/
3408 static int cmd_getfacl(void)
3410 TALLOC_CTX *ctx = talloc_tos();
3413 char *targetname = NULL;
3414 struct cli_state *targetcli;
3415 uint16 major, minor;
3416 uint32 caplow, caphigh;
3417 char *retbuf = NULL;
3419 SMB_STRUCT_STAT sbuf;
3420 uint16 num_file_acls = 0;
3421 uint16 num_dir_acls = 0;
3425 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3426 d_printf("getfacl filename\n");
3429 src = talloc_asprintf(ctx,
3431 client_get_cur_dir(),
3437 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3439 if (!NT_STATUS_IS_OK(status)) {
3440 d_printf("stat %s: %s\n", src, nt_errstr(status));
3444 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3445 d_printf("Server doesn't support UNIX CIFS calls.\n");
3449 status = cli_unix_extensions_version(targetcli, &major, &minor,
3451 if (!NT_STATUS_IS_OK(status)) {
3452 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3457 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3458 d_printf("This server supports UNIX extensions "
3459 "but doesn't support POSIX ACLs.\n");
3463 status = cli_posix_stat(targetcli, targetname, &sbuf);
3464 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3465 d_printf("%s getfacl doing a stat on file %s\n",
3466 nt_errstr(status), src);
3470 status = cli_posix_getfacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3471 if (!NT_STATUS_IS_OK(status)) {
3472 d_printf("%s getfacl file %s\n",
3473 nt_errstr(status), src);
3477 /* ToDo : Print out the ACL values. */
3478 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3479 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3480 src, (unsigned int)CVAL(retbuf,0) );
3484 num_file_acls = SVAL(retbuf,2);
3485 num_dir_acls = SVAL(retbuf,4);
3486 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3487 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3489 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3490 (unsigned int)rb_size);
3494 d_printf("# file: %s\n", src);
3495 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3497 if (num_file_acls == 0 && num_dir_acls == 0) {
3498 d_printf("No acls found.\n");
3501 for (i = 0; i < num_file_acls; i++) {
3504 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3505 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3508 case SMB_POSIX_ACL_USER_OBJ:
3511 case SMB_POSIX_ACL_USER:
3512 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3513 d_printf("user:%u:", uorg);
3515 case SMB_POSIX_ACL_GROUP_OBJ:
3516 d_printf("group::");
3518 case SMB_POSIX_ACL_GROUP:
3519 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3520 d_printf("group:%u:", uorg);
3522 case SMB_POSIX_ACL_MASK:
3525 case SMB_POSIX_ACL_OTHER:
3526 d_printf("other::");
3529 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3530 src, (unsigned int)tagtype );
3535 d_printf("%s\n", perms_to_string(permstring, perms));
3538 for (i = 0; i < num_dir_acls; i++) {
3541 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3542 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3545 case SMB_POSIX_ACL_USER_OBJ:
3546 d_printf("default:user::");
3548 case SMB_POSIX_ACL_USER:
3549 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3550 d_printf("default:user:%u:", uorg);
3552 case SMB_POSIX_ACL_GROUP_OBJ:
3553 d_printf("default:group::");
3555 case SMB_POSIX_ACL_GROUP:
3556 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3557 d_printf("default:group:%u:", uorg);
3559 case SMB_POSIX_ACL_MASK:
3560 d_printf("default:mask::");
3562 case SMB_POSIX_ACL_OTHER:
3563 d_printf("default:other::");
3566 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3567 src, (unsigned int)tagtype );
3572 d_printf("%s\n", perms_to_string(permstring, perms));
3578 static void printf_cb(const char *buf, void *private_data)
3583 /****************************************************************************
3584 Get the EA list of a file
3585 ****************************************************************************/
3587 static int cmd_geteas(void)
3589 TALLOC_CTX *ctx = talloc_tos();
3592 char *targetname = NULL;
3593 struct cli_state *targetcli;
3596 struct ea_struct *eas;
3598 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3599 d_printf("geteas filename\n");
3602 src = talloc_asprintf(ctx,
3604 client_get_cur_dir(),
3610 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3612 if (!NT_STATUS_IS_OK(status)) {
3613 d_printf("stat %s: %s\n", src, nt_errstr(status));
3617 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3619 if (!NT_STATUS_IS_OK(status)) {
3620 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3624 for (i=0; i<num_eas; i++) {
3625 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3626 dump_data_cb(eas[i].value.data, eas[i].value.length, false,
3636 /****************************************************************************
3638 ****************************************************************************/
3640 static int cmd_setea(void)
3642 TALLOC_CTX *ctx = talloc_tos();
3645 char *eaname = NULL;
3646 char *eavalue = NULL;
3647 char *targetname = NULL;
3648 struct cli_state *targetcli;
3651 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
3652 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
3653 d_printf("setea filename eaname value\n");
3656 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
3657 eavalue = talloc_strdup(ctx, "");
3659 src = talloc_asprintf(ctx,
3661 client_get_cur_dir(),
3667 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3669 if (!NT_STATUS_IS_OK(status)) {
3670 d_printf("stat %s: %s\n", src, nt_errstr(status));
3674 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
3676 if (!NT_STATUS_IS_OK(status)) {
3677 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
3684 /****************************************************************************
3686 ****************************************************************************/
3688 static int cmd_stat(void)
3690 TALLOC_CTX *ctx = talloc_tos();
3693 char *targetname = NULL;
3694 struct cli_state *targetcli;
3696 SMB_STRUCT_STAT sbuf;
3701 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3702 d_printf("stat file\n");
3705 src = talloc_asprintf(ctx,
3707 client_get_cur_dir(),
3713 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3715 if (!NT_STATUS_IS_OK(status)) {
3716 d_printf("stat %s: %s\n", src, nt_errstr(status));
3720 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3721 d_printf("Server doesn't support UNIX CIFS calls.\n");
3725 status = cli_posix_stat(targetcli, targetname, &sbuf);
3726 if (!NT_STATUS_IS_OK(status)) {
3727 d_printf("%s stat file %s\n",
3728 nt_errstr(status), src);
3732 /* Print out the stat values. */
3733 d_printf("File: %s\n", src);
3734 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3735 (double)sbuf.st_ex_size,
3736 (unsigned int)sbuf.st_ex_blocks,
3737 filetype_to_str(sbuf.st_ex_mode));
3739 #if defined(S_ISCHR) && defined(S_ISBLK)
3740 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
3741 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3742 (double)sbuf.st_ex_ino,
3743 (unsigned int)sbuf.st_ex_nlink,
3744 unix_dev_major(sbuf.st_ex_rdev),
3745 unix_dev_minor(sbuf.st_ex_rdev));
3748 d_printf("Inode: %.0f\tLinks: %u\n",
3749 (double)sbuf.st_ex_ino,
3750 (unsigned int)sbuf.st_ex_nlink);
3752 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3753 ((int)sbuf.st_ex_mode & 0777),
3754 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
3755 (unsigned int)sbuf.st_ex_uid,
3756 (unsigned int)sbuf.st_ex_gid);
3758 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
3759 lt = localtime(&tmp_time);
3761 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3763 fstrcpy(mode_str, "unknown");
3765 d_printf("Access: %s\n", mode_str);
3767 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3768 lt = localtime(&tmp_time);
3770 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3772 fstrcpy(mode_str, "unknown");
3774 d_printf("Modify: %s\n", mode_str);
3776 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
3777 lt = localtime(&tmp_time);
3779 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3781 fstrcpy(mode_str, "unknown");
3783 d_printf("Change: %s\n", mode_str);
3789 /****************************************************************************
3791 ****************************************************************************/
3793 static int cmd_chown(void)
3795 TALLOC_CTX *ctx = talloc_tos();
3799 char *buf, *buf2, *buf3;
3800 struct cli_state *targetcli;
3801 char *targetname = NULL;
3804 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3805 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3806 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3807 d_printf("chown uid gid file\n");
3811 uid = (uid_t)atoi(buf);
3812 gid = (gid_t)atoi(buf2);
3814 src = talloc_asprintf(ctx,
3816 client_get_cur_dir(),
3821 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3823 if (!NT_STATUS_IS_OK(status)) {
3824 d_printf("chown %s: %s\n", src, nt_errstr(status));
3828 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3829 d_printf("Server doesn't support UNIX CIFS calls.\n");
3833 status = cli_posix_chown(targetcli, targetname, uid, gid);
3834 if (!NT_STATUS_IS_OK(status)) {
3835 d_printf("%s chown file %s uid=%d, gid=%d\n",
3836 nt_errstr(status), src, (int)uid, (int)gid);
3843 /****************************************************************************
3845 ****************************************************************************/
3847 static int cmd_rename(void)
3849 TALLOC_CTX *ctx = talloc_tos();
3852 struct cli_state *targetcli;
3857 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3858 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3859 d_printf("rename <src> <dest>\n");
3863 src = talloc_asprintf(ctx,
3865 client_get_cur_dir(),
3871 dest = talloc_asprintf(ctx,
3873 client_get_cur_dir(),
3879 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3881 if (!NT_STATUS_IS_OK(status)) {
3882 d_printf("rename %s: %s\n", src, nt_errstr(status));
3886 status = cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli,
3888 if (!NT_STATUS_IS_OK(status)) {
3889 d_printf("rename %s: %s\n", dest, nt_errstr(status));
3893 status = cli_rename(targetcli, targetsrc, targetdest);
3894 if (!NT_STATUS_IS_OK(status)) {
3895 d_printf("%s renaming files %s -> %s \n",
3905 /****************************************************************************
3906 Print the volume name.
3907 ****************************************************************************/
3909 static int cmd_volume(void)
3912 uint32_t serial_num;
3916 status = cli_get_fs_volume_info(cli, talloc_tos(),
3917 &volname, &serial_num,
3919 if (!NT_STATUS_IS_OK(status)) {
3920 d_printf("Error %s getting volume info\n", nt_errstr(status));
3924 d_printf("Volume: |%s| serial number 0x%x\n",
3925 volname, (unsigned int)serial_num);
3929 /****************************************************************************
3930 Hard link files using the NT call.
3931 ****************************************************************************/
3933 static int cmd_hardlink(void)
3935 TALLOC_CTX *ctx = talloc_tos();
3938 struct cli_state *targetcli;
3942 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3943 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3944 d_printf("hardlink <src> <dest>\n");
3948 src = talloc_asprintf(ctx,
3950 client_get_cur_dir(),
3956 dest = talloc_asprintf(ctx,
3958 client_get_cur_dir(),
3964 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3966 if (!NT_STATUS_IS_OK(status)) {
3967 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
3971 status = cli_nt_hardlink(targetcli, targetname, dest);
3972 if (!NT_STATUS_IS_OK(status)) {
3973 d_printf("%s doing an NT hard link of files\n",
3981 /****************************************************************************
3982 Toggle the prompt flag.
3983 ****************************************************************************/
3985 static int cmd_prompt(void)
3988 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
3992 /****************************************************************************
3993 Set the newer than time.
3994 ****************************************************************************/
3996 static int cmd_newer(void)
3998 TALLOC_CTX *ctx = talloc_tos();
4001 SMB_STRUCT_STAT sbuf;
4003 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
4004 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
4005 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4006 DEBUG(1,("Getting files newer than %s",
4007 time_to_asc(newer_than)));
4012 if (ok && newer_than == 0) {
4013 d_printf("Error setting newer-than time\n");
4020 /****************************************************************************
4021 Set the archive level.
4022 ****************************************************************************/
4024 static int cmd_archive(void)
4026 TALLOC_CTX *ctx = talloc_tos();
4029 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4030 archive_level = atoi(buf);
4032 d_printf("Archive level is %d\n",archive_level);
4038 /****************************************************************************
4039 Toggle the backup_intent state.
4040 ****************************************************************************/
4042 static int cmd_backup(void)
4044 backup_intent = !backup_intent;
4045 cli_set_backup_intent(cli, backup_intent);
4046 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4050 /****************************************************************************
4051 Toggle the lowercaseflag.
4052 ****************************************************************************/
4054 static int cmd_lowercase(void)
4056 lowercase = !lowercase;
4057 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4061 /****************************************************************************
4062 Toggle the case sensitive flag.
4063 ****************************************************************************/
4065 static int cmd_setcase(void)
4067 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4069 cli_set_case_sensitive(cli, !orig_case_sensitive);
4070 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4075 /****************************************************************************
4076 Toggle the showacls flag.
4077 ****************************************************************************/
4079 static int cmd_showacls(void)
4081 showacls = !showacls;
4082 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4087 /****************************************************************************
4088 Toggle the recurse flag.
4089 ****************************************************************************/
4091 static int cmd_recurse(void)
4094 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4098 /****************************************************************************
4099 Toggle the translate flag.
4100 ****************************************************************************/
4102 static int cmd_translate(void)
4104 translation = !translation;
4105 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4106 translation?"on":"off"));
4110 /****************************************************************************
4112 ****************************************************************************/
4114 static int cmd_lcd(void)
4116 TALLOC_CTX *ctx = talloc_tos();
4120 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4121 if (chdir(buf) == -1) {
4122 d_printf("chdir to %s failed (%s)\n",
4123 buf, strerror(errno));
4130 DEBUG(2,("the local directory is now %s\n",d));
4135 /****************************************************************************
4136 Get a file restarting at end of local file.
4137 ****************************************************************************/
4139 static int cmd_reget(void)
4141 TALLOC_CTX *ctx = talloc_tos();
4142 char *local_name = NULL;
4143 char *remote_name = NULL;
4147 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4152 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4153 d_printf("reget <filename>\n");
4156 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4160 remote_name = clean_name(ctx,remote_name);
4166 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4171 return do_get(remote_name, local_name, true);
4174 /****************************************************************************
4175 Put a file restarting at end of local file.
4176 ****************************************************************************/
4178 static int cmd_reput(void)
4180 TALLOC_CTX *ctx = talloc_tos();
4181 char *local_name = NULL;
4182 char *remote_name = NULL;
4186 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4191 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4192 d_printf("reput <filename>\n");
4196 if (!file_exist_stat(local_name, &st, false)) {
4197 d_printf("%s does not exist\n", local_name);
4201 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4202 remote_name = talloc_asprintf_append(remote_name,
4205 remote_name = talloc_asprintf_append(remote_name,
4212 remote_name = clean_name(ctx, remote_name);
4217 return do_put(remote_name, local_name, true);
4220 /****************************************************************************
4222 ****************************************************************************/
4224 static void browse_fn(const char *name, uint32 m,
4225 const char *comment, void *state)
4227 const char *typestr = "";
4230 case STYPE_DISKTREE:
4234 typestr = "Printer";
4243 /* FIXME: If the remote machine returns non-ascii characters
4244 in any of these fields, they can corrupt the output. We
4245 should remove them. */
4247 d_printf("\t%-15s %-10.10s%s\n",
4248 name,typestr,comment);
4250 d_printf ("%s|%s|%s\n",typestr,name,comment);
4254 static bool browse_host_rpc(bool sort)
4257 struct rpc_pipe_client *pipe_hnd = NULL;
4258 TALLOC_CTX *frame = talloc_stackframe();
4260 struct srvsvc_NetShareInfoCtr info_ctr;
4261 struct srvsvc_NetShareCtr1 ctr1;
4262 uint32_t resume_handle = 0;
4263 uint32_t total_entries = 0;
4265 struct dcerpc_binding_handle *b;
4267 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
4270 if (!NT_STATUS_IS_OK(status)) {
4271 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4272 nt_errstr(status)));
4277 b = pipe_hnd->binding_handle;
4279 ZERO_STRUCT(info_ctr);
4283 info_ctr.ctr.ctr1 = &ctr1;
4285 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4293 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4294 TALLOC_FREE(pipe_hnd);
4299 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4300 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4301 browse_fn(info.name, info.type, info.comment, NULL);
4304 TALLOC_FREE(pipe_hnd);
4309 /****************************************************************************
4310 Try and browse available connections on a host.
4311 ****************************************************************************/
4313 static bool browse_host(bool sort)
4317 d_printf("\n\tSharename Type Comment\n");
4318 d_printf("\t--------- ---- -------\n");
4321 if (browse_host_rpc(sort)) {
4325 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1) {
4326 NTSTATUS status = cli_nt_error(cli);
4327 d_printf("Error returning browse list: %s\n",
4334 /****************************************************************************
4336 ****************************************************************************/
4338 static void server_fn(const char *name, uint32 m,
4339 const char *comment, void *state)
4343 d_printf("\t%-16s %s\n", name, comment);
4345 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4349 /****************************************************************************
4350 Try and browse available connections on a host.
4351 ****************************************************************************/
4353 static bool list_servers(const char *wk_grp)
4357 if (!cli->server_domain)
4361 d_printf("\n\tServer Comment\n");
4362 d_printf("\t--------- -------\n");
4364 fstrcpy( state, "Server" );
4365 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4369 d_printf("\n\tWorkgroup Master\n");
4370 d_printf("\t--------- -------\n");
4373 fstrcpy( state, "Workgroup" );
4374 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4379 /****************************************************************************
4380 Print or set current VUID
4381 ****************************************************************************/
4383 static int cmd_vuid(void)
4385 TALLOC_CTX *ctx = talloc_tos();
4388 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4389 d_printf("Current VUID is %d\n",
4390 cli_state_get_uid(cli));
4394 cli_state_set_uid(cli, atoi(buf));
4398 /****************************************************************************
4399 Setup a new VUID, by issuing a session setup
4400 ****************************************************************************/
4402 static int cmd_logon(void)
4404 TALLOC_CTX *ctx = talloc_tos();
4405 char *l_username, *l_password;
4408 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
4409 d_printf("logon <username> [<password>]\n");
4413 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
4414 char *pass = getpass("Password: ");
4416 l_password = talloc_strdup(ctx,pass);
4423 nt_status = cli_session_setup(cli, l_username,
4424 l_password, strlen(l_password),
4425 l_password, strlen(l_password),
4427 if (!NT_STATUS_IS_OK(nt_status)) {
4428 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
4432 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
4440 static int cmd_logoff(void)
4444 status = cli_ulogoff(cli);
4445 if (!NT_STATUS_IS_OK(status)) {
4446 d_printf("logoff failed: %s\n", nt_errstr(status));
4450 d_printf("logoff successful\n");
4456 * tree connect (connect to a share)
4459 static int cmd_tcon(void)
4461 TALLOC_CTX *ctx = talloc_tos();
4465 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
4466 d_printf("tcon <sharename>\n");
4474 status = cli_tree_connect(cli, sharename, "?????", "", 0);
4475 if (!NT_STATUS_IS_OK(status)) {
4476 d_printf("tcon failed: %s\n", nt_errstr(status));
4480 talloc_free(sharename);
4482 d_printf("tcon to %s successful, tid: %u\n", sharename,
4483 cli_state_get_tid(cli));
4488 * tree disconnect (disconnect from a share)
4491 static int cmd_tdis(void)
4495 status = cli_tdis(cli);
4496 if (!NT_STATUS_IS_OK(status)) {
4497 d_printf("tdis failed: %s\n", nt_errstr(status));
4501 d_printf("tdis successful\n");
4510 static int cmd_tid(void)
4512 TALLOC_CTX *ctx = talloc_tos();
4515 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
4516 if (cli_state_has_tcon(cli)) {
4517 d_printf("current tid is %d\n", cli_state_get_tid(cli));
4519 d_printf("no tcon currently\n");
4522 uint16_t tid = atoi(tid_str);
4523 cli_state_set_tid(cli, tid);
4530 /****************************************************************************
4531 list active connections
4532 ****************************************************************************/
4534 static int cmd_list_connect(void)
4536 cli_cm_display(cli);
4540 /****************************************************************************
4541 display the current active client connection
4542 ****************************************************************************/
4544 static int cmd_show_connect( void )
4546 TALLOC_CTX *ctx = talloc_tos();
4547 struct cli_state *targetcli;
4551 status = cli_resolve_path(ctx, "", auth_info, cli,
4552 client_get_cur_dir(), &targetcli,
4554 if (!NT_STATUS_IS_OK(status)) {
4555 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
4559 d_printf("//%s/%s\n", cli_state_remote_name(targetcli), targetcli->share);
4563 /****************************************************************************
4565 ***************************************************************************/
4567 int cmd_iosize(void)
4569 TALLOC_CTX *ctx = talloc_tos();
4573 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4575 d_printf("iosize <n> or iosize 0x<n>. "
4576 "Minimum is 16384 (0x4000), "
4577 "max is 16776960 (0xFFFF00)\n");
4579 d_printf("iosize <n> or iosize 0x<n>. "
4580 "(Encrypted connection) ,"
4581 "Minimum is 16384 (0x4000), "
4582 "max is 130048 (0x1FC00)\n");
4587 iosize = strtol(buf,NULL,0);
4588 if (smb_encrypt && (iosize < 0x4000 || iosize > 0xFC00)) {
4589 d_printf("iosize out of range for encrypted "
4590 "connection (min = 16384 (0x4000), "
4591 "max = 130048 (0x1FC00)");
4593 } else if (!smb_encrypt && (iosize < 0x4000 || iosize > 0xFFFF00)) {
4594 d_printf("iosize out of range (min = 16384 (0x4000), "
4595 "max = 16776960 (0xFFFF00)");
4599 io_bufsize = iosize;
4600 d_printf("iosize is now %d\n", io_bufsize);
4604 /****************************************************************************
4606 ****************************************************************************/
4607 static int cmd_history(void)
4609 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4613 hlist = history_list();
4615 for (i = 0; hlist && hlist[i]; i++) {
4616 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
4619 DEBUG(0,("no history without readline support\n"));
4625 /* Some constants for completing filename arguments */
4627 #define COMPL_NONE 0 /* No completions */
4628 #define COMPL_REMOTE 1 /* Complete remote filename */
4629 #define COMPL_LOCAL 2 /* Complete local filename */
4631 /* This defines the commands supported by this client.
4632 * NOTE: The "!" must be the last one in the list because it's fn pointer
4633 * field is NULL, and NULL in that field is used in process_tok()
4634 * (below) to indicate the end of the list. crh
4639 const char *description;
4640 char compl_args[2]; /* Completion argument info */
4642 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4643 {"allinfo",cmd_allinfo,"<file> show all available info",
4644 {COMPL_NONE,COMPL_NONE}},
4645 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
4646 {"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}},
4647 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
4648 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
4649 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
4650 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
4651 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
4652 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
4653 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
4654 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
4655 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4656 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4657 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4658 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
4659 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4660 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
4661 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
4662 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
4663 {COMPL_REMOTE, COMPL_LOCAL}},
4664 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4665 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4666 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
4667 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
4668 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
4669 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4670 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4671 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
4672 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4673 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4674 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
4675 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4676 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
4677 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4678 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
4679 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
4680 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
4681 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
4682 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
4683 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
4684 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4685 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4686 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4687 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4688 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
4689 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
4690 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
4691 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
4692 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4693 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
4694 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4695 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4696 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4697 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
4698 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
4699 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
4700 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
4701 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4702 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4703 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
4704 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
4705 {COMPL_REMOTE, COMPL_LOCAL}},
4706 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
4707 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
4708 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4709 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
4710 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
4711 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
4712 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4713 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
4714 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
4715 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4716 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
4717 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
4718 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
4719 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
4720 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
4721 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
4722 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
4723 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
4725 /* Yes, this must be here, see crh's comment above. */
4726 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
4727 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
4730 /*******************************************************************
4731 Lookup a command string in the list of commands, including
4733 ******************************************************************/
4735 static int process_tok(char *tok)
4737 int i = 0, matches = 0;
4739 int tok_len = strlen(tok);
4741 while (commands[i].fn != NULL) {
4742 if (strequal(commands[i].name,tok)) {
4746 } else if (strnequal(commands[i].name, tok, tok_len)) {
4755 else if (matches == 1)
4761 /****************************************************************************
4763 ****************************************************************************/
4765 static int cmd_help(void)
4767 TALLOC_CTX *ctx = talloc_tos();
4771 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4772 if ((i = process_tok(buf)) >= 0)
4773 d_printf("HELP %s:\n\t%s\n\n",
4774 commands[i].name,commands[i].description);
4776 while (commands[i].description) {
4777 for (j=0; commands[i].description && (j<5); j++) {
4778 d_printf("%-15s",commands[i].name);
4787 /****************************************************************************
4788 Process a -c command string.
4789 ****************************************************************************/
4791 static int process_command_string(const char *cmd_in)
4793 TALLOC_CTX *ctx = talloc_tos();
4794 char *cmd = talloc_strdup(ctx, cmd_in);
4800 /* establish the connection if not already */
4805 status = cli_cm_open(talloc_tos(), NULL,
4806 have_ip ? dest_ss_str : desthost,
4809 max_protocol, port, name_type,
4811 if (!NT_STATUS_IS_OK(status)) {
4816 while (cmd[0] != '\0') {
4822 if ((p = strchr_m(cmd, ';')) == 0) {
4831 /* and get the first part of the command */
4833 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
4837 if ((i = process_tok(tok)) >= 0) {
4838 rc = commands[i].fn();
4839 } else if (i == -2) {
4840 d_printf("%s: command abbreviation ambiguous\n",tok);
4842 d_printf("%s: command not found\n",tok);
4849 #define MAX_COMPLETIONS 100
4851 struct completion_remote {
4859 static NTSTATUS completion_remote_filter(const char *mnt,
4860 struct file_info *f,
4864 struct completion_remote *info = (struct completion_remote *)state;
4866 if (info->count >= MAX_COMPLETIONS - 1) {
4867 return NT_STATUS_OK;
4869 if (strncmp(info->text, f->name, info->len) != 0) {
4870 return NT_STATUS_OK;
4872 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
4873 return NT_STATUS_OK;
4876 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
4877 info->matches[info->count] = SMB_STRDUP(f->name);
4879 TALLOC_CTX *ctx = talloc_stackframe();
4882 tmp = talloc_strdup(ctx,info->dirmask);
4885 return NT_STATUS_NO_MEMORY;
4887 tmp = talloc_asprintf_append(tmp, "%s", f->name);
4890 return NT_STATUS_NO_MEMORY;
4892 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4893 tmp = talloc_asprintf_append(tmp, "%s",
4898 return NT_STATUS_NO_MEMORY;
4900 info->matches[info->count] = SMB_STRDUP(tmp);
4903 if (info->matches[info->count] == NULL) {
4904 return NT_STATUS_OK;
4906 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4907 smb_readline_ca_char(0);
4909 if (info->count == 1) {
4910 info->samelen = strlen(info->matches[info->count]);
4912 while (strncmp(info->matches[info->count],
4913 info->matches[info->count-1],
4914 info->samelen) != 0) {
4919 return NT_STATUS_OK;
4922 static char **remote_completion(const char *text, int len)
4924 TALLOC_CTX *ctx = talloc_stackframe();
4925 char *dirmask = NULL;
4926 char *targetpath = NULL;
4927 struct cli_state *targetcli = NULL;
4929 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
4932 /* can't have non-static initialisation on Sun CC, so do it
4938 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
4939 if (!info.matches) {
4945 * We're leaving matches[0] free to fill it later with the text to
4946 * display: Either the one single match or the longest common subset
4949 info.matches[0] = NULL;
4952 for (i = len-1; i >= 0; i--) {
4953 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
4958 info.text = text+i+1;
4959 info.samelen = info.len = len-i-1;
4962 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
4963 if (!info.dirmask) {
4966 strncpy(info.dirmask, text, i+1);
4967 info.dirmask[i+1] = 0;
4968 dirmask = talloc_asprintf(ctx,
4970 client_get_cur_dir(),
4974 info.dirmask = SMB_STRDUP("");
4975 if (!info.dirmask) {
4978 dirmask = talloc_asprintf(ctx,
4980 client_get_cur_dir());
4986 status = cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli,
4988 if (!NT_STATUS_IS_OK(status)) {
4991 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
4992 completion_remote_filter, (void *)&info);
4993 if (!NT_STATUS_IS_OK(status)) {
4997 if (info.count == 1) {
4999 * No matches at all, NULL indicates there is nothing
5001 SAFE_FREE(info.matches[0]);
5002 SAFE_FREE(info.matches);
5007 if (info.count == 2) {
5009 * Exactly one match in matches[1], indicate this is the one
5012 info.matches[0] = info.matches[1];
5013 info.matches[1] = NULL;
5016 return info.matches;
5020 * We got more than one possible match, set the result to the maximum
5024 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
5025 info.matches[info.count] = NULL;
5026 return info.matches;
5029 for (i = 0; i < info.count; i++) {
5030 SAFE_FREE(info.matches[i]);
5032 SAFE_FREE(info.matches);
5033 SAFE_FREE(info.dirmask);
5038 static char **completion_fn(const char *text, int start, int end)
5040 smb_readline_ca_char(' ');
5043 const char *buf, *sp;
5047 buf = smb_readline_get_line_buffer();
5051 sp = strchr(buf, ' ');
5055 for (i = 0; commands[i].name; i++) {
5056 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
5057 (commands[i].name[sp - buf] == 0)) {
5061 if (commands[i].name == NULL)
5067 if (sp == (buf + start))
5068 compl_type = commands[i].compl_args[0];
5070 compl_type = commands[i].compl_args[1];
5072 if (compl_type == COMPL_REMOTE)
5073 return remote_completion(text, end - start);
5074 else /* fall back to local filename completion */
5078 int i, len, samelen = 0, count=1;
5080 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
5087 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
5088 if (strncmp(text, commands[i].name, len) == 0) {
5089 matches[count] = SMB_STRDUP(commands[i].name);
5090 if (!matches[count])
5093 samelen = strlen(matches[count]);
5095 while (strncmp(matches[count], matches[count-1], samelen) != 0)
5102 case 0: /* should never happen */
5106 matches[0] = SMB_STRDUP(matches[1]);
5109 matches[0] = (char *)SMB_MALLOC(samelen+1);
5112 strncpy(matches[0], matches[1], samelen);
5113 matches[0][samelen] = 0;
5115 matches[count] = NULL;
5119 for (i = 0; i < count; i++)
5127 static bool finished;
5129 /****************************************************************************
5130 Make sure we swallow keepalives during idle time.
5131 ****************************************************************************/
5133 static void readline_callback(void)
5135 static time_t last_t;
5136 struct timespec now;
5139 unsigned char garbage[16];
5141 clock_gettime_mono(&now);
5149 /* Ping the server to keep the connection alive using SMBecho. */
5150 memset(garbage, 0xf0, sizeof(garbage));
5151 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
5152 if (!NT_STATUS_IS_OK(status)) {
5153 DEBUG(0, ("SMBecho failed (%s). Maybe server has closed "
5154 "the connection\n", nt_errstr(status)));
5156 smb_readline_done();
5160 /****************************************************************************
5161 Process commands on stdin.
5162 ****************************************************************************/
5164 static int process_stdin(void)
5169 TALLOC_CTX *frame = talloc_stackframe();
5171 char *the_prompt = NULL;
5175 /* display a prompt */
5176 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
5180 line = smb_readline(the_prompt, readline_callback, completion_fn);
5181 SAFE_FREE(the_prompt);
5187 /* special case - first char is ! */
5189 if (system(line + 1) == -1) {
5190 d_printf("system() command %s failed.\n",
5198 /* and get the first part of the command */
5200 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
5206 if ((i = process_tok(tok)) >= 0) {
5207 rc = commands[i].fn();
5208 } else if (i == -2) {
5209 d_printf("%s: command abbreviation ambiguous\n",tok);
5211 d_printf("%s: command not found\n",tok);
5219 /****************************************************************************
5220 Process commands from the client.
5221 ****************************************************************************/
5223 static int process(const char *base_directory)
5228 status = cli_cm_open(talloc_tos(), NULL,
5229 have_ip ? dest_ss_str : desthost,
5230 service, auth_info, true, smb_encrypt,
5231 max_protocol, port, name_type, &cli);
5232 if (!NT_STATUS_IS_OK(status)) {
5236 if (base_directory && *base_directory) {
5237 rc = do_cd(base_directory);
5245 rc = process_command_string(cmdstr);
5254 /****************************************************************************
5256 ****************************************************************************/
5258 static int do_host_query(const char *query_host)
5262 status = cli_cm_open(talloc_tos(), NULL,
5263 have_ip ? dest_ss_str : query_host,
5264 "IPC$", auth_info, true, smb_encrypt,
5265 max_protocol, port, name_type, &cli);
5266 if (!NT_STATUS_IS_OK(status)) {
5272 /* Ensure that the host can do IPv4 */
5274 if (!interpret_addr(query_host)) {
5275 struct sockaddr_storage ss;
5276 if (interpret_string_addr(&ss, query_host, 0) &&
5277 (ss.ss_family != AF_INET)) {
5278 d_printf("%s is an IPv6 address -- no workgroup available\n",
5286 /* Workgroups simply don't make sense over anything
5287 else but port 139... */
5290 status = cli_cm_open(talloc_tos(), NULL,
5291 have_ip ? dest_ss_str : query_host,
5292 "IPC$", auth_info, true, smb_encrypt,
5293 max_protocol, 139, name_type, &cli);
5294 if (!NT_STATUS_IS_OK(status)) {
5300 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
5304 list_servers(lp_workgroup());
5311 /****************************************************************************
5312 Handle a tar operation.
5313 ****************************************************************************/
5315 static int do_tar_op(const char *base_directory)
5319 /* do we already have a connection? */
5323 status = cli_cm_open(talloc_tos(), NULL,
5324 have_ip ? dest_ss_str : desthost,
5325 service, auth_info, true, smb_encrypt,
5326 max_protocol, port, name_type, &cli);
5327 if (!NT_STATUS_IS_OK(status)) {
5334 if (base_directory && *base_directory) {
5335 ret = do_cd(base_directory);
5349 /****************************************************************************
5350 Handle a message operation.
5351 ****************************************************************************/
5353 static int do_message_op(struct user_auth_info *a_info)
5357 status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
5358 port ? port : 139, name_type,
5359 lp_netbios_name(), SMB_SIGNING_DEFAULT, 0, &cli);
5360 if (!NT_STATUS_IS_OK(status)) {
5361 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
5365 send_message(get_cmdline_auth_info_username(a_info));
5371 /****************************************************************************
5373 ****************************************************************************/
5375 int main(int argc,char *argv[])
5377 char *base_directory = NULL;
5379 char *query_host = NULL;
5380 bool message = false;
5381 static const char *new_name_resolve_order = NULL;
5385 bool tar_opt = false;
5386 bool service_opt = false;
5387 struct poptOption long_options[] = {
5390 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
5391 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
5392 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
5393 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
5394 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
5395 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
5396 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
5397 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
5398 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
5399 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
5400 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
5401 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
5402 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
5404 POPT_COMMON_CONNECTION
5405 POPT_COMMON_CREDENTIALS
5408 TALLOC_CTX *frame = talloc_stackframe();
5410 if (!client_set_cur_dir("\\")) {
5414 /* set default debug level to 1 regardless of what smb.conf sets */
5415 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
5418 lp_set_cmdline("log level", "1");
5420 auth_info = user_auth_info_init(frame);
5421 if (auth_info == NULL) {
5424 popt_common_set_auth_info(auth_info);
5427 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
5428 poptSetOtherOptionHelp(pc, "service <password>");
5430 while ((opt = poptGetNextOpt(pc)) != -1) {
5432 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
5433 /* I see no other way to keep things sane --SSS */
5434 if (tar_opt == true) {
5435 while (poptPeekArg(pc)) {
5441 /* if the service has not yet been specified lets see if it is available in the popt stack */
5442 if (!service_opt && poptPeekArg(pc)) {
5443 service = talloc_strdup(frame, poptGetArg(pc));
5450 /* if the service has already been retrieved then check if we have also a password */
5452 && (!get_cmdline_auth_info_got_pass(auth_info))
5453 && poptPeekArg(pc)) {
5454 set_cmdline_auth_info_password(auth_info,
5460 /* Messages are sent to NetBIOS name type 0x3
5461 * (Messenger Service). Make sure we default
5462 * to port 139 instead of port 445. srl,crh
5465 desthost = talloc_strdup(frame,poptGetOptArg(pc));
5475 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
5479 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
5483 setup_logging("smbclient", DEBUG_STDERR );
5484 display_set_stderr();
5488 query_host = talloc_strdup(frame, poptGetOptArg(pc));
5494 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
5497 /* We must use old option processing for this. Find the
5498 * position of the -T option in the raw argv[]. */
5501 for (i = 1; i < argc; i++) {
5502 if (strncmp("-T", argv[i],2)==0)
5506 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
5507 poptPrintUsage(pc, stderr, 0);
5511 /* this must be the last option, mark we have parsed it so that we know we have */
5515 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
5516 if (!base_directory) {
5527 return(do_smb_browse());
5532 /* We may still have some leftovers after the last popt option has been called */
5533 if (tar_opt == true) {
5534 while (poptPeekArg(pc)) {
5540 /* if the service has not yet been specified lets see if it is available in the popt stack */
5541 if (!service_opt && poptPeekArg(pc)) {
5542 service = talloc_strdup(frame,poptGetArg(pc));
5549 /* if the service has already been retrieved then check if we have also a password */
5551 && !get_cmdline_auth_info_got_pass(auth_info)
5552 && poptPeekArg(pc)) {
5553 set_cmdline_auth_info_password(auth_info,
5557 if ( override_logfile )
5558 setup_logging( lp_logfile(), DEBUG_FILE );
5560 if (!lp_load_client(get_dyn_CONFIGFILE())) {
5561 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
5562 argv[0], get_dyn_CONFIGFILE());
5565 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
5566 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
5572 if (service_opt && service) {
5575 /* Convert any '/' characters in the service name to '\' characters */
5576 string_replace(service, '/','\\');
5577 if (count_chars(service,'\\') < 3) {
5578 d_printf("\n%s: Not enough '\\' characters in service\n",service);
5579 poptPrintUsage(pc, stderr, 0);
5582 /* Remove trailing slashes */
5583 len = strlen(service);
5584 while(len > 0 && service[len - 1] == '\\') {
5586 service[len] = '\0';
5590 smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
5591 if (!init_names()) {
5592 fprintf(stderr, "init_names() failed\n");
5596 if(new_name_resolve_order)
5597 lp_set_name_resolve_order(new_name_resolve_order);
5599 if (!tar_type && !query_host && !service && !message) {
5600 poptPrintUsage(pc, stderr, 0);
5604 poptFreeContext(pc);
5606 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5608 /* Ensure we have a password (or equivalent). */
5609 set_cmdline_auth_info_getpass(auth_info);
5613 process_command_string(cmdstr);
5614 return do_tar_op(base_directory);
5617 if (query_host && *query_host) {
5618 char *qhost = query_host;
5621 while (*qhost == '\\' || *qhost == '/')
5624 if ((slash = strchr_m(qhost, '/'))
5625 || (slash = strchr_m(qhost, '\\'))) {
5629 if ((p=strchr_m(qhost, '#'))) {
5632 sscanf(p, "%x", &name_type);
5635 return do_host_query(qhost);
5639 return do_message_op(auth_info);
5642 if (process(base_directory)) {