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_LEVEL) ||
403 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
404 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
405 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
406 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
407 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
415 return cli_open(cli, fname, flags, share_mode_in, pfnum);
418 /****************************************************************************
419 Check the space on a device.
420 ****************************************************************************/
422 static int do_dskattr(void)
424 int total, bsize, avail;
425 struct cli_state *targetcli = NULL;
426 char *targetpath = NULL;
427 TALLOC_CTX *ctx = talloc_tos();
430 status = cli_resolve_path(ctx, "", auth_info, cli,
431 client_get_cur_dir(), &targetcli,
433 if (!NT_STATUS_IS_OK(status)) {
434 d_printf("Error in dskattr: %s\n", nt_errstr(status));
438 status = cli_dskattr(targetcli, &bsize, &total, &avail);
439 if (!NT_STATUS_IS_OK(status)) {
440 d_printf("Error in dskattr: %s\n", nt_errstr(status));
444 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
445 total, bsize, avail);
450 /****************************************************************************
452 ****************************************************************************/
454 static int cmd_pwd(void)
456 d_printf("Current directory is %s",service);
457 d_printf("%s\n",client_get_cur_dir());
461 /****************************************************************************
462 Ensure name has correct directory separators.
463 ****************************************************************************/
465 static void normalize_name(char *newdir)
467 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
468 string_replace(newdir,'/','\\');
472 /****************************************************************************
473 Change directory - inner section.
474 ****************************************************************************/
476 static int do_cd(const char *new_dir)
479 char *saved_dir = NULL;
481 char *targetpath = NULL;
482 struct cli_state *targetcli = NULL;
483 SMB_STRUCT_STAT sbuf;
486 TALLOC_CTX *ctx = talloc_stackframe();
489 newdir = talloc_strdup(ctx, new_dir);
495 normalize_name(newdir);
497 /* Save the current directory in case the new directory is invalid */
499 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
505 if (*newdir == CLI_DIRSEP_CHAR) {
506 client_set_cur_dir(newdir);
509 new_cd = talloc_asprintf(ctx, "%s%s",
510 client_get_cur_dir(),
517 /* Ensure cur_dir ends in a DIRSEP */
518 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
519 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
524 client_set_cur_dir(new_cd);
526 new_cd = clean_name(ctx, new_cd);
527 client_set_cur_dir(new_cd);
529 status = cli_resolve_path(ctx, "", auth_info, cli, new_cd,
530 &targetcli, &targetpath);
531 if (!NT_STATUS_IS_OK(status)) {
532 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
533 client_set_cur_dir(saved_dir);
537 if (strequal(targetpath,CLI_DIRSEP_STR )) {
542 /* Use a trans2_qpathinfo to test directories for modern servers.
543 Except Win9x doesn't support the qpathinfo_basic() call..... */
545 if (cli_state_protocol(targetcli) > PROTOCOL_LANMAN2 && !targetcli->win95) {
547 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
549 if (!NT_STATUS_IS_OK(status)) {
550 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
551 client_set_cur_dir(saved_dir);
555 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
556 d_printf("cd %s: not a directory\n", new_cd);
557 client_set_cur_dir(saved_dir);
562 targetpath = talloc_asprintf(ctx,
567 client_set_cur_dir(saved_dir);
570 targetpath = clean_name(ctx, targetpath);
572 client_set_cur_dir(saved_dir);
576 status = cli_chkpath(targetcli, targetpath);
577 if (!NT_STATUS_IS_OK(status)) {
578 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
579 client_set_cur_dir(saved_dir);
592 /****************************************************************************
594 ****************************************************************************/
596 static int cmd_cd(void)
601 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
604 d_printf("Current directory is %s\n",client_get_cur_dir());
610 /****************************************************************************
612 ****************************************************************************/
614 static int cmd_cd_oneup(void)
619 /*******************************************************************
620 Decide if a file should be operated on.
621 ********************************************************************/
623 static bool do_this_one(struct file_info *finfo)
629 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
633 if (*client_get_fileselection() &&
634 !mask_match(finfo->name,client_get_fileselection(),false)) {
635 DEBUG(3,("mask_match %s failed\n", finfo->name));
639 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
640 DEBUG(3,("newer_than %s failed\n", finfo->name));
644 if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
645 DEBUG(3,("archive %s failed\n", finfo->name));
652 /****************************************************************************
653 Display info about a file.
654 ****************************************************************************/
656 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
660 TALLOC_CTX *ctx = talloc_tos();
661 NTSTATUS status = NT_STATUS_OK;
663 if (!do_this_one(finfo)) {
667 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
669 d_printf(" %-30s%7.7s %8.0f %s",
671 attrib_string(talloc_tos(), finfo->mode),
674 dir_total += finfo->size;
679 /* skip if this is . or .. */
680 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
682 /* create absolute filename for cli_ntcreate() FIXME */
683 afname = talloc_asprintf(ctx,
689 return NT_STATUS_NO_MEMORY;
691 /* print file meta date header */
692 d_printf( "FILENAME:%s\n", finfo->name);
693 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->mode));
694 d_printf( "SIZE:%.0f\n", (double)finfo->size);
695 d_printf( "MTIME:%s", time_to_asc(t));
696 status = cli_ntcreate(cli_state, afname, 0,
697 CREATE_ACCESS_READ, 0,
698 FILE_SHARE_READ|FILE_SHARE_WRITE,
699 FILE_OPEN, 0x0, 0x0, &fnum);
700 if (!NT_STATUS_IS_OK(status)) {
701 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
702 afname, nt_errstr(status)));
704 struct security_descriptor *sd = NULL;
705 status = cli_query_secdesc(cli_state, fnum,
707 if (!NT_STATUS_IS_OK(status)) {
708 DEBUG( 0, ("display_finfo() failed to "
709 "get security descriptor: %s",
712 display_sec_desc(sd);
721 /****************************************************************************
722 Accumulate size of a file.
723 ****************************************************************************/
725 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
728 if (do_this_one(finfo)) {
729 dir_total += finfo->size;
734 static bool do_list_recurse;
735 static bool do_list_dirs;
736 static char *do_list_queue = 0;
737 static long do_list_queue_size = 0;
738 static long do_list_queue_start = 0;
739 static long do_list_queue_end = 0;
740 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
743 /****************************************************************************
744 Functions for do_list_queue.
745 ****************************************************************************/
748 * The do_list_queue is a NUL-separated list of strings stored in a
749 * char*. Since this is a FIFO, we keep track of the beginning and
750 * ending locations of the data in the queue. When we overflow, we
751 * double the size of the char*. When the start of the data passes
752 * the midpoint, we move everything back. This is logically more
753 * complex than a linked list, but easier from a memory management
754 * angle. In any memory error condition, do_list_queue is reset.
755 * Functions check to ensure that do_list_queue is non-NULL before
759 static void reset_do_list_queue(void)
761 SAFE_FREE(do_list_queue);
762 do_list_queue_size = 0;
763 do_list_queue_start = 0;
764 do_list_queue_end = 0;
767 static void init_do_list_queue(void)
769 reset_do_list_queue();
770 do_list_queue_size = 1024;
771 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
772 if (do_list_queue == 0) {
773 d_printf("malloc fail for size %d\n",
774 (int)do_list_queue_size);
775 reset_do_list_queue();
777 memset(do_list_queue, 0, do_list_queue_size);
781 static void adjust_do_list_queue(void)
784 * If the starting point of the queue is more than half way through,
785 * move everything toward the beginning.
788 if (do_list_queue == NULL) {
789 DEBUG(4,("do_list_queue is empty\n"));
790 do_list_queue_start = do_list_queue_end = 0;
794 if (do_list_queue_start == do_list_queue_end) {
795 DEBUG(4,("do_list_queue is empty\n"));
796 do_list_queue_start = do_list_queue_end = 0;
797 *do_list_queue = '\0';
798 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
799 DEBUG(4,("sliding do_list_queue backward\n"));
800 memmove(do_list_queue,
801 do_list_queue + do_list_queue_start,
802 do_list_queue_end - do_list_queue_start);
803 do_list_queue_end -= do_list_queue_start;
804 do_list_queue_start = 0;
808 static void add_to_do_list_queue(const char *entry)
810 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
811 while (new_end > do_list_queue_size) {
812 do_list_queue_size *= 2;
813 DEBUG(4,("enlarging do_list_queue to %d\n",
814 (int)do_list_queue_size));
815 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
816 if (! do_list_queue) {
817 d_printf("failure enlarging do_list_queue to %d bytes\n",
818 (int)do_list_queue_size);
819 reset_do_list_queue();
821 memset(do_list_queue + do_list_queue_size / 2,
822 0, do_list_queue_size / 2);
826 strlcpy_base(do_list_queue + do_list_queue_end,
827 entry, do_list_queue, do_list_queue_size);
828 do_list_queue_end = new_end;
829 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
830 entry, (int)do_list_queue_start, (int)do_list_queue_end));
834 static char *do_list_queue_head(void)
836 return do_list_queue + do_list_queue_start;
839 static void remove_do_list_queue_head(void)
841 if (do_list_queue_end > do_list_queue_start) {
842 do_list_queue_start += strlen(do_list_queue_head()) + 1;
843 adjust_do_list_queue();
844 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
845 (int)do_list_queue_start, (int)do_list_queue_end));
849 static int do_list_queue_empty(void)
851 return (! (do_list_queue && *do_list_queue));
854 /****************************************************************************
855 A helper for do_list.
856 ****************************************************************************/
858 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
859 const char *mask, void *state)
861 struct cli_state *cli_state = (struct cli_state *)state;
862 TALLOC_CTX *ctx = talloc_tos();
864 char *dir_end = NULL;
865 NTSTATUS status = NT_STATUS_OK;
867 /* Work out the directory. */
868 dir = talloc_strdup(ctx, mask);
870 return NT_STATUS_NO_MEMORY;
872 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
876 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
877 if (do_list_dirs && do_this_one(f)) {
878 status = do_list_fn(cli_state, f, dir);
879 if (!NT_STATUS_IS_OK(status)) {
883 if (do_list_recurse &&
885 !strequal(f->name,".") &&
886 !strequal(f->name,"..")) {
891 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
893 return NT_STATUS_UNSUCCESSFUL;
896 mask2 = talloc_asprintf(ctx,
902 return NT_STATUS_NO_MEMORY;
904 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
910 mask2 = talloc_asprintf_append(mask2,
916 return NT_STATUS_NO_MEMORY;
918 add_to_do_list_queue(mask2);
925 if (do_this_one(f)) {
926 status = do_list_fn(cli_state, f, dir);
932 /****************************************************************************
933 A wrapper around cli_list that adds recursion.
934 ****************************************************************************/
936 NTSTATUS do_list(const char *mask,
938 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
943 static int in_do_list = 0;
944 TALLOC_CTX *ctx = talloc_tos();
945 struct cli_state *targetcli = NULL;
946 char *targetpath = NULL;
947 NTSTATUS ret_status = NT_STATUS_OK;
948 NTSTATUS status = NT_STATUS_OK;
950 if (in_do_list && rec) {
951 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
957 do_list_recurse = rec;
962 init_do_list_queue();
963 add_to_do_list_queue(mask);
965 while (!do_list_queue_empty()) {
967 * Need to copy head so that it doesn't become
968 * invalid inside the call to cli_list. This
969 * would happen if the list were expanded
971 * Fix from E. Jay Berkenbilt (ejb@ql.org)
973 char *head = talloc_strdup(ctx, do_list_queue_head());
976 return NT_STATUS_NO_MEMORY;
981 status = cli_resolve_path(ctx, "", auth_info, cli,
984 if (!NT_STATUS_IS_OK(status)) {
985 d_printf("do_list: [%s] %s\n", head,
987 remove_do_list_queue_head();
991 status = cli_list(targetcli, targetpath, attribute,
992 do_list_helper, targetcli);
993 if (!NT_STATUS_IS_OK(status)) {
994 d_printf("%s listing %s\n",
995 nt_errstr(status), targetpath);
998 remove_do_list_queue_head();
999 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
1000 char *next_file = do_list_queue_head();
1002 if ((strlen(next_file) >= 2) &&
1003 (next_file[strlen(next_file) - 1] == '*') &&
1004 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
1005 save_ch = next_file +
1006 strlen(next_file) - 2;
1009 /* cwd is only used if showacls is on */
1010 client_set_cwd(next_file);
1013 if (!showacls) /* don't disturbe the showacls output */
1014 d_printf("\n%s\n",next_file);
1016 *save_ch = CLI_DIRSEP_CHAR;
1020 TALLOC_FREE(targetpath);
1024 status = cli_resolve_path(ctx, "", auth_info, cli, mask,
1025 &targetcli, &targetpath);
1026 if (NT_STATUS_IS_OK(status)) {
1027 status = cli_list(targetcli, targetpath, attribute,
1028 do_list_helper, targetcli);
1029 if (!NT_STATUS_IS_OK(status)) {
1030 d_printf("%s listing %s\n",
1031 nt_errstr(status), targetpath);
1032 ret_status = status;
1034 TALLOC_FREE(targetpath);
1036 d_printf("do_list: [%s] %s\n", mask, nt_errstr(status));
1037 ret_status = status;
1042 reset_do_list_queue();
1046 /****************************************************************************
1047 Get a directory listing.
1048 ****************************************************************************/
1050 static int cmd_dir(void)
1052 TALLOC_CTX *ctx = talloc_tos();
1053 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1060 mask = talloc_strdup(ctx, client_get_cur_dir());
1065 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1066 normalize_name(buf);
1067 if (*buf == CLI_DIRSEP_CHAR) {
1068 mask = talloc_strdup(ctx, buf);
1070 mask = talloc_asprintf_append(mask, "%s", buf);
1073 mask = talloc_asprintf_append(mask, "*");
1080 /* cwd is only used if showacls is on */
1081 client_set_cwd(client_get_cur_dir());
1084 status = do_list(mask, attribute, display_finfo, recurse, true);
1085 if (!NT_STATUS_IS_OK(status)) {
1091 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
1096 /****************************************************************************
1097 Get a directory listing.
1098 ****************************************************************************/
1100 static int cmd_du(void)
1102 TALLOC_CTX *ctx = talloc_tos();
1103 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1110 mask = talloc_strdup(ctx, client_get_cur_dir());
1114 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
1115 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
1121 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1122 normalize_name(buf);
1123 if (*buf == CLI_DIRSEP_CHAR) {
1124 mask = talloc_strdup(ctx, buf);
1126 mask = talloc_asprintf_append(mask, "%s", buf);
1129 mask = talloc_strdup(ctx, "*");
1132 status = do_list(mask, attribute, do_du, recurse, true);
1133 if (!NT_STATUS_IS_OK(status)) {
1139 d_printf("Total number of bytes: %.0f\n", dir_total);
1144 static int cmd_echo(void)
1146 TALLOC_CTX *ctx = talloc_tos();
1151 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1152 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1153 d_printf("echo <num> <data>\n");
1157 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1159 if (!NT_STATUS_IS_OK(status)) {
1160 d_printf("echo failed: %s\n", nt_errstr(status));
1167 /****************************************************************************
1168 Get a file from rname to lname
1169 ****************************************************************************/
1171 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1173 int *pfd = (int *)priv;
1174 if (writefile(*pfd, buf, n) == -1) {
1175 return map_nt_error_from_unix(errno);
1177 return NT_STATUS_OK;
1180 static int do_get(const char *rname, const char *lname_in, bool reget)
1182 TALLOC_CTX *ctx = talloc_tos();
1185 bool newhandle = false;
1186 struct timespec tp_start;
1190 SMB_OFF_T nread = 0;
1192 struct cli_state *targetcli = NULL;
1193 char *targetname = NULL;
1197 lname = talloc_strdup(ctx, lname_in);
1206 status = cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli,
1208 if (!NT_STATUS_IS_OK(status)) {
1209 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1213 clock_gettime_mono(&tp_start);
1215 status = smbclient_cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1216 if (!NT_STATUS_IS_OK(status)) {
1217 d_printf("%s opening remote file %s\n", nt_errstr(status),
1222 if(!strcmp(lname,"-")) {
1223 handle = fileno(stdout);
1226 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
1228 start = sys_lseek(handle, 0, SEEK_END);
1230 d_printf("Error seeking local file\n");
1235 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1240 d_printf("Error opening local file %s\n",lname);
1245 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1247 if (!NT_STATUS_IS_OK(status)) {
1248 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1250 if(!NT_STATUS_IS_OK(status)) {
1251 d_printf("getattrib: %s\n", nt_errstr(status));
1256 DEBUG(1,("getting file %s of size %.0f as %s ",
1257 rname, (double)size, lname));
1259 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1260 writefile_sink, (void *)&handle, &nread);
1261 if (!NT_STATUS_IS_OK(status)) {
1262 d_fprintf(stderr, "parallel_read returned %s\n",
1264 cli_close(targetcli, fnum);
1268 status = cli_close(targetcli, fnum);
1269 if (!NT_STATUS_IS_OK(status)) {
1270 d_printf("Error %s closing remote file\n", nt_errstr(status));
1278 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1279 cli_setatr(cli, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
1283 struct timespec tp_end;
1286 clock_gettime_mono(&tp_end);
1287 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1288 get_total_time_ms += this_time;
1289 get_total_size += nread;
1291 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1292 nread / (1.024*this_time + 1.0e-4),
1293 get_total_size / (1.024*get_total_time_ms)));
1296 TALLOC_FREE(targetname);
1300 /****************************************************************************
1302 ****************************************************************************/
1304 static int cmd_get(void)
1306 TALLOC_CTX *ctx = talloc_tos();
1311 rname = talloc_strdup(ctx, client_get_cur_dir());
1316 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1317 d_printf("get <filename> [localname]\n");
1320 rname = talloc_asprintf_append(rname, "%s", fname);
1324 rname = clean_name(ctx, rname);
1329 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1334 return do_get(rname, lname, false);
1337 /****************************************************************************
1338 Do an mget operation on one file.
1339 ****************************************************************************/
1341 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1344 TALLOC_CTX *ctx = talloc_tos();
1345 NTSTATUS status = NT_STATUS_OK;
1348 char *saved_curdir = NULL;
1349 char *mget_mask = NULL;
1350 char *new_cd = NULL;
1353 return NT_STATUS_OK;
1356 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1357 return NT_STATUS_OK;
1360 d_printf("mget aborted\n");
1361 return NT_STATUS_UNSUCCESSFUL;
1364 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
1365 if (asprintf(&quest,
1366 "Get directory %s? ",finfo->name) < 0) {
1367 return NT_STATUS_NO_MEMORY;
1370 if (asprintf(&quest,
1371 "Get file %s? ",finfo->name) < 0) {
1372 return NT_STATUS_NO_MEMORY;
1376 if (prompt && !yesno(quest)) {
1378 return NT_STATUS_OK;
1382 if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
1383 rname = talloc_asprintf(ctx,
1385 client_get_cur_dir(),
1388 return NT_STATUS_NO_MEMORY;
1390 do_get(rname, finfo->name, false);
1392 return NT_STATUS_OK;
1395 /* handle directories */
1396 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1397 if (!saved_curdir) {
1398 return NT_STATUS_NO_MEMORY;
1401 new_cd = talloc_asprintf(ctx,
1403 client_get_cur_dir(),
1407 return NT_STATUS_NO_MEMORY;
1409 client_set_cur_dir(new_cd);
1411 string_replace(finfo->name,'\\','/');
1413 strlower_m(finfo->name);
1416 if (!directory_exist(finfo->name) &&
1417 mkdir(finfo->name,0777) != 0) {
1418 d_printf("failed to create directory %s\n",finfo->name);
1419 client_set_cur_dir(saved_curdir);
1420 return map_nt_error_from_unix(errno);
1423 if (chdir(finfo->name) != 0) {
1424 d_printf("failed to chdir to directory %s\n",finfo->name);
1425 client_set_cur_dir(saved_curdir);
1426 return map_nt_error_from_unix(errno);
1429 mget_mask = talloc_asprintf(ctx,
1431 client_get_cur_dir());
1434 return NT_STATUS_NO_MEMORY;
1437 status = do_list(mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,false, true);
1438 if (!NT_STATUS_IS_OK(status)) {
1442 if (chdir("..") == -1) {
1443 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1445 return map_nt_error_from_unix(errno);
1447 client_set_cur_dir(saved_curdir);
1448 TALLOC_FREE(mget_mask);
1449 TALLOC_FREE(saved_curdir);
1450 TALLOC_FREE(new_cd);
1451 return NT_STATUS_OK;
1454 /****************************************************************************
1455 View the file using the pager.
1456 ****************************************************************************/
1458 static int cmd_more(void)
1460 TALLOC_CTX *ctx = talloc_tos();
1464 char *pager_cmd = NULL;
1469 rname = talloc_strdup(ctx, client_get_cur_dir());
1474 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1478 fd = mkstemp(lname);
1480 d_printf("failed to create temporary file for more\n");
1485 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1486 d_printf("more <filename>\n");
1490 rname = talloc_asprintf_append(rname, "%s", fname);
1494 rname = clean_name(ctx,rname);
1499 rc = do_get(rname, lname, false);
1501 pager=getenv("PAGER");
1503 pager_cmd = talloc_asprintf(ctx,
1505 (pager? pager:PAGER),
1510 if (system(pager_cmd) == -1) {
1511 d_printf("system command '%s' returned -1\n",
1519 /****************************************************************************
1521 ****************************************************************************/
1523 static int cmd_mget(void)
1525 TALLOC_CTX *ctx = talloc_tos();
1526 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1527 char *mget_mask = NULL;
1529 NTSTATUS status = NT_STATUS_OK;
1532 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1537 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1539 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1543 if (*buf == CLI_DIRSEP_CHAR) {
1544 mget_mask = talloc_strdup(ctx, buf);
1546 mget_mask = talloc_asprintf_append(mget_mask,
1552 status = do_list(mget_mask, attribute, do_mget, false, true);
1553 if (!NT_STATUS_IS_OK(status)) {
1558 if (mget_mask == NULL) {
1559 d_printf("nothing to mget\n");
1564 mget_mask = talloc_asprintf(ctx,
1566 client_get_cur_dir());
1570 status = do_list(mget_mask, attribute, do_mget, false, true);
1571 if (!NT_STATUS_IS_OK(status)) {
1579 /****************************************************************************
1580 Make a directory of name "name".
1581 ****************************************************************************/
1583 static bool do_mkdir(const char *name)
1585 TALLOC_CTX *ctx = talloc_tos();
1586 struct cli_state *targetcli;
1587 char *targetname = NULL;
1590 status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
1592 if (!NT_STATUS_IS_OK(status)) {
1593 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1597 status = cli_mkdir(targetcli, targetname);
1598 if (!NT_STATUS_IS_OK(status)) {
1599 d_printf("%s making remote directory %s\n",
1600 nt_errstr(status),name);
1607 /****************************************************************************
1608 Show 8.3 name of a file.
1609 ****************************************************************************/
1611 static bool do_altname(const char *name)
1616 status = cli_qpathinfo_alt_name(cli, name, altname);
1617 if (!NT_STATUS_IS_OK(status)) {
1618 d_printf("%s getting alt name for %s\n",
1619 nt_errstr(status),name);
1622 d_printf("%s\n", altname);
1627 /****************************************************************************
1629 ****************************************************************************/
1631 static int cmd_quit(void)
1639 /****************************************************************************
1641 ****************************************************************************/
1643 static int cmd_mkdir(void)
1645 TALLOC_CTX *ctx = talloc_tos();
1650 mask = talloc_strdup(ctx, client_get_cur_dir());
1655 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1657 d_printf("mkdir <dirname>\n");
1661 mask = talloc_asprintf_append(mask, "%s", buf);
1669 struct cli_state *targetcli;
1670 char *targetname = NULL;
1674 ddir2 = talloc_strdup(ctx, "");
1679 status = cli_resolve_path(ctx, "", auth_info, cli, mask,
1680 &targetcli, &targetname);
1681 if (!NT_STATUS_IS_OK(status)) {
1685 ddir = talloc_strdup(ctx, targetname);
1689 trim_char(ddir,'.','\0');
1690 p = strtok_r(ddir, "/\\", &saveptr);
1692 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1696 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1699 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1703 p = strtok_r(NULL, "/\\", &saveptr);
1712 /****************************************************************************
1714 ****************************************************************************/
1716 static int cmd_altname(void)
1718 TALLOC_CTX *ctx = talloc_tos();
1722 name = talloc_strdup(ctx, client_get_cur_dir());
1727 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1728 d_printf("altname <file>\n");
1731 name = talloc_asprintf_append(name, "%s", buf);
1739 static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1741 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1744 if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1745 if (mode & FILE_ATTRIBUTE_ENCRYPTED) {
1748 if (mode & FILE_ATTRIBUTE_NONINDEXED) {
1751 if (mode & FILE_ATTRIBUTE_OFFLINE) {
1754 if (mode & FILE_ATTRIBUTE_COMPRESSED) {
1757 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1760 if (mode & FILE_ATTRIBUTE_SPARSE) {
1763 if (mode & FILE_ATTRIBUTE_TEMPORARY) {
1766 if (mode & FILE_ATTRIBUTE_NORMAL) {
1769 if (mode & FILE_ATTRIBUTE_READONLY) {
1772 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1775 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1778 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1781 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1788 /****************************************************************************
1789 Show all info we can get
1790 ****************************************************************************/
1792 static int do_allinfo(const char *name)
1795 struct timespec b_time, a_time, m_time, c_time;
1801 unsigned int num_streams;
1802 struct stream_struct *streams;
1808 status = cli_qpathinfo_alt_name(cli, name, altname);
1809 if (!NT_STATUS_IS_OK(status)) {
1810 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1814 d_printf("altname: %s\n", altname);
1816 status = cli_qpathinfo2(cli, name, &b_time, &a_time, &m_time, &c_time,
1817 &size, &mode, &ino);
1818 if (!NT_STATUS_IS_OK(status)) {
1819 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1824 unix_timespec_to_nt_time(&tmp, b_time);
1825 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1827 unix_timespec_to_nt_time(&tmp, a_time);
1828 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1830 unix_timespec_to_nt_time(&tmp, m_time);
1831 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1833 unix_timespec_to_nt_time(&tmp, c_time);
1834 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1836 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
1838 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1840 if (!NT_STATUS_IS_OK(status)) {
1841 d_printf("%s getting streams for %s\n", nt_errstr(status),
1846 for (i=0; i<num_streams; i++) {
1847 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1848 (unsigned long long)streams[i].size);
1851 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1852 char *subst, *print;
1855 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1857 if (!NT_STATUS_IS_OK(status)) {
1858 d_fprintf(stderr, "cli_readlink returned %s\n",
1861 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1862 subst, print, flags);
1868 status = cli_ntcreate(cli, name, 0,
1869 CREATE_ACCESS_READ, 0,
1870 FILE_SHARE_READ|FILE_SHARE_WRITE
1872 FILE_OPEN, 0x0, 0x0, &fnum);
1873 if (!NT_STATUS_IS_OK(status)) {
1875 * Ignore failure, it does not hurt if we can't list
1880 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1881 true, &snapshots, &num_snapshots);
1882 if (!NT_STATUS_IS_OK(status)) {
1883 cli_close(cli, fnum);
1887 for (i=0; i<num_snapshots; i++) {
1890 d_printf("%s\n", snapshots[i]);
1891 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1892 snapshots[i], name);
1893 status = cli_qpathinfo2(cli, snap_name, &b_time, &a_time,
1894 &m_time, &c_time, &size,
1896 if (!NT_STATUS_IS_OK(status)) {
1897 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1898 snap_name, nt_errstr(status));
1899 TALLOC_FREE(snap_name);
1902 unix_timespec_to_nt_time(&tmp, b_time);
1903 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1904 unix_timespec_to_nt_time(&tmp, a_time);
1905 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1906 unix_timespec_to_nt_time(&tmp, m_time);
1907 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1908 unix_timespec_to_nt_time(&tmp, c_time);
1909 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1910 d_printf("size: %d\n", (int)size);
1913 TALLOC_FREE(snapshots);
1918 /****************************************************************************
1919 Show all info we can get
1920 ****************************************************************************/
1922 static int cmd_allinfo(void)
1924 TALLOC_CTX *ctx = talloc_tos();
1928 name = talloc_strdup(ctx, client_get_cur_dir());
1933 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1934 d_printf("allinfo <file>\n");
1937 name = talloc_asprintf_append(name, "%s", buf);
1947 /****************************************************************************
1949 ****************************************************************************/
1951 static int do_put(const char *rname, const char *lname, bool reput)
1953 TALLOC_CTX *ctx = talloc_tos();
1956 SMB_OFF_T start = 0;
1958 struct timespec tp_start;
1959 struct cli_state *targetcli;
1960 char *targetname = NULL;
1961 struct push_state state;
1964 status = cli_resolve_path(ctx, "", auth_info, cli, rname,
1965 &targetcli, &targetname);
1966 if (!NT_STATUS_IS_OK(status)) {
1967 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1971 clock_gettime_mono(&tp_start);
1974 status = smbclient_cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1975 if (NT_STATUS_IS_OK(status)) {
1976 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1977 targetcli, fnum, NULL,
1979 NULL, NULL, NULL)) &&
1980 !NT_STATUS_IS_OK(status = cli_getattrE(
1981 targetcli, fnum, NULL,
1984 d_printf("getattrib: %s\n", nt_errstr(status));
1989 status = smbclient_cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1992 if (!NT_STATUS_IS_OK(status)) {
1993 d_printf("%s opening remote file %s\n", nt_errstr(status),
1998 /* allow files to be piped into smbclient
2001 Note that in this case this function will exit(0) rather
2003 if (!strcmp(lname, "-")) {
2005 /* size of file is not known */
2007 f = x_fopen(lname,O_RDONLY, 0);
2009 if (x_tseek(f, start, SEEK_SET) == -1) {
2010 d_printf("Error seeking local file\n");
2018 d_printf("Error opening local file %s\n",lname);
2022 DEBUG(1,("putting file %s as %s ",lname,
2025 x_setvbuf(f, NULL, X_IOFBF, io_bufsize);
2030 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
2032 if (!NT_STATUS_IS_OK(status)) {
2033 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
2037 status = cli_close(targetcli, fnum);
2038 if (!NT_STATUS_IS_OK(status)) {
2039 d_printf("%s closing remote file %s\n", nt_errstr(status),
2052 struct timespec tp_end;
2055 clock_gettime_mono(&tp_end);
2056 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
2057 put_total_time_ms += this_time;
2058 put_total_size += state.nread;
2060 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
2061 state.nread / (1.024*this_time + 1.0e-4),
2062 put_total_size / (1.024*put_total_time_ms)));
2073 /****************************************************************************
2075 ****************************************************************************/
2077 static int cmd_put(void)
2079 TALLOC_CTX *ctx = talloc_tos();
2084 rname = talloc_strdup(ctx, client_get_cur_dir());
2089 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
2090 d_printf("put <filename>\n");
2094 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2095 rname = talloc_asprintf_append(rname, "%s", buf);
2097 rname = talloc_asprintf_append(rname, "%s", lname);
2103 rname = clean_name(ctx, rname);
2110 /* allow '-' to represent stdin
2111 jdblair, 24.jun.98 */
2112 if (!file_exist_stat(lname, &st, false) &&
2113 (strcmp(lname,"-"))) {
2114 d_printf("%s does not exist\n",lname);
2119 return do_put(rname, lname, false);
2122 /*************************************
2123 File list structure.
2124 *************************************/
2126 static struct file_list {
2127 struct file_list *prev, *next;
2132 /****************************************************************************
2133 Free a file_list structure.
2134 ****************************************************************************/
2136 static void free_file_list (struct file_list *l_head)
2138 struct file_list *list, *next;
2140 for (list = l_head; list; list = next) {
2142 DLIST_REMOVE(l_head, list);
2143 SAFE_FREE(list->file_path);
2148 /****************************************************************************
2149 Seek in a directory/file list until you get something that doesn't start with
2151 ****************************************************************************/
2153 static bool seek_list(struct file_list *list, char *name)
2156 trim_string(list->file_path,"./","\n");
2157 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2166 /****************************************************************************
2167 Set the file selection mask.
2168 ****************************************************************************/
2170 static int cmd_select(void)
2172 TALLOC_CTX *ctx = talloc_tos();
2173 char *new_fs = NULL;
2174 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2177 client_set_fileselection(new_fs);
2179 client_set_fileselection("");
2184 /****************************************************************************
2185 Recursive file matching function act as find
2186 match must be always set to true when calling this function
2187 ****************************************************************************/
2189 static int file_find(struct file_list **list, const char *directory,
2190 const char *expression, bool match)
2192 SMB_STRUCT_DIR *dir;
2193 struct file_list *entry;
2194 struct stat statbuf;
2200 dir = sys_opendir(directory);
2204 while ((dname = readdirname(dir))) {
2205 if (!strcmp("..", dname))
2207 if (!strcmp(".", dname))
2210 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
2215 if (!match || !gen_fnmatch(expression, dname)) {
2217 ret = stat(path, &statbuf);
2219 if (S_ISDIR(statbuf.st_mode)) {
2221 ret = file_find(list, path, expression, false);
2224 d_printf("file_find: cannot stat file %s\n", path);
2233 entry = SMB_MALLOC_P(struct file_list);
2235 d_printf("Out of memory in file_find\n");
2239 entry->file_path = path;
2240 entry->isdir = isdir;
2241 DLIST_ADD(*list, entry);
2251 /****************************************************************************
2253 ****************************************************************************/
2255 static int cmd_mput(void)
2257 TALLOC_CTX *ctx = talloc_tos();
2260 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2262 struct file_list *temp_list;
2263 char *quest, *lname, *rname;
2267 ret = file_find(&file_list, ".", p, true);
2269 free_file_list(file_list);
2277 for (temp_list = file_list; temp_list;
2278 temp_list = temp_list->next) {
2281 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2284 trim_string(lname, "./", "/");
2286 /* check if it's a directory */
2287 if (temp_list->isdir) {
2288 /* if (!recurse) continue; */
2291 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2294 if (prompt && !yesno(quest)) { /* No */
2295 /* Skip the directory */
2296 lname[strlen(lname)-1] = '/';
2297 if (!seek_list(temp_list, lname))
2301 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2304 normalize_name(rname);
2305 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2307 DEBUG (0, ("Unable to make dir, skipping..."));
2308 /* Skip the directory */
2309 lname[strlen(lname)-1] = '/';
2310 if (!seek_list(temp_list, lname)) {
2318 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2321 if (prompt && !yesno(quest)) {
2328 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2333 normalize_name(rname);
2335 do_put(rname, lname, false);
2337 free_file_list(file_list);
2346 /****************************************************************************
2348 ****************************************************************************/
2350 static int do_cancel(int job)
2352 if (cli_printjob_del(cli, job)) {
2353 d_printf("Job %d cancelled\n",job);
2356 NTSTATUS status = cli_nt_error(cli);
2357 d_printf("Error cancelling job %d : %s\n",
2358 job, nt_errstr(status));
2363 /****************************************************************************
2365 ****************************************************************************/
2367 static int cmd_cancel(void)
2369 TALLOC_CTX *ctx = talloc_tos();
2373 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2374 d_printf("cancel <jobid> ...\n");
2380 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2385 /****************************************************************************
2387 ****************************************************************************/
2389 static int cmd_print(void)
2391 TALLOC_CTX *ctx = talloc_tos();
2396 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2397 d_printf("print <filename>\n");
2401 rname = talloc_strdup(ctx, lname);
2405 p = strrchr_m(rname,'/');
2407 rname = talloc_asprintf(ctx,
2412 if (strequal(lname,"-")) {
2413 rname = talloc_asprintf(ctx,
2421 return do_put(rname, lname, false);
2424 /****************************************************************************
2425 Show a print queue entry.
2426 ****************************************************************************/
2428 static void queue_fn(struct print_job_info *p)
2430 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2433 /****************************************************************************
2435 ****************************************************************************/
2437 static int cmd_queue(void)
2439 cli_print_queue(cli, queue_fn);
2443 /****************************************************************************
2445 ****************************************************************************/
2447 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2450 TALLOC_CTX *ctx = talloc_tos();
2454 mask = talloc_asprintf(ctx,
2460 return NT_STATUS_NO_MEMORY;
2463 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2465 return NT_STATUS_OK;
2468 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2469 if (!NT_STATUS_IS_OK(status)) {
2470 d_printf("%s deleting remote file %s\n",
2471 nt_errstr(status), mask);
2477 /****************************************************************************
2479 ****************************************************************************/
2481 static int cmd_del(void)
2483 TALLOC_CTX *ctx = talloc_tos();
2486 NTSTATUS status = NT_STATUS_OK;
2487 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2490 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2493 mask = talloc_strdup(ctx, client_get_cur_dir());
2497 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2498 d_printf("del <filename>\n");
2501 mask = talloc_asprintf_append(mask, "%s", buf);
2506 status = do_list(mask,attribute,do_del,false,false);
2507 if (!NT_STATUS_IS_OK(status)) {
2513 /****************************************************************************
2514 Wildcard delete some files.
2515 ****************************************************************************/
2517 static int cmd_wdel(void)
2519 TALLOC_CTX *ctx = talloc_tos();
2523 struct cli_state *targetcli;
2524 char *targetname = NULL;
2527 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2528 d_printf("wdel 0x<attrib> <wcard>\n");
2532 attribute = (uint16)strtol(buf, (char **)NULL, 16);
2534 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2535 d_printf("wdel 0x<attrib> <wcard>\n");
2539 mask = talloc_asprintf(ctx, "%s%s",
2540 client_get_cur_dir(),
2546 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2548 if (!NT_STATUS_IS_OK(status)) {
2549 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2553 status = cli_unlink(targetcli, targetname, attribute);
2554 if (!NT_STATUS_IS_OK(status)) {
2555 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2561 /****************************************************************************
2562 ****************************************************************************/
2564 static int cmd_open(void)
2566 TALLOC_CTX *ctx = talloc_tos();
2569 char *targetname = NULL;
2570 struct cli_state *targetcli;
2571 uint16_t fnum = (uint16_t)-1;
2574 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2575 d_printf("open <filename>\n");
2578 mask = talloc_asprintf(ctx,
2580 client_get_cur_dir(),
2586 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2588 if (!NT_STATUS_IS_OK(status)) {
2589 d_printf("open %s: %s\n", mask, nt_errstr(status));
2593 status = cli_ntcreate(targetcli, targetname, 0,
2594 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2595 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2597 if (!NT_STATUS_IS_OK(status)) {
2598 status = cli_ntcreate(targetcli, targetname, 0,
2600 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2602 if (NT_STATUS_IS_OK(status)) {
2603 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2605 d_printf("Failed to open file %s. %s\n",
2606 targetname, nt_errstr(status));
2609 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2614 static int cmd_posix_encrypt(void)
2616 TALLOC_CTX *ctx = talloc_tos();
2617 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2619 if (cli->use_kerberos) {
2620 status = cli_gss_smb_encryption_start(cli);
2622 char *domain = NULL;
2624 char *password = NULL;
2626 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2627 d_printf("posix_encrypt domain user password\n");
2631 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2632 d_printf("posix_encrypt domain user password\n");
2636 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2637 d_printf("posix_encrypt domain user password\n");
2641 status = cli_raw_ntlm_smb_encryption_start(cli,
2647 if (!NT_STATUS_IS_OK(status)) {
2648 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2650 d_printf("encryption on\n");
2657 /****************************************************************************
2658 ****************************************************************************/
2660 static int cmd_posix_open(void)
2662 TALLOC_CTX *ctx = talloc_tos();
2665 char *targetname = NULL;
2666 struct cli_state *targetcli;
2671 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2672 d_printf("posix_open <filename> 0<mode>\n");
2675 mask = talloc_asprintf(ctx,
2677 client_get_cur_dir(),
2683 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2684 d_printf("posix_open <filename> 0<mode>\n");
2687 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2689 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2691 if (!NT_STATUS_IS_OK(status)) {
2692 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2696 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2698 if (!NT_STATUS_IS_OK(status)) {
2699 status = cli_posix_open(targetcli, targetname,
2700 O_CREAT|O_RDONLY, mode, &fnum);
2701 if (!NT_STATUS_IS_OK(status)) {
2702 d_printf("Failed to open file %s. %s\n", targetname,
2705 d_printf("posix_open file %s: for readonly fnum %d\n",
2709 d_printf("posix_open file %s: for read/write fnum %d\n",
2716 static int cmd_posix_mkdir(void)
2718 TALLOC_CTX *ctx = talloc_tos();
2721 char *targetname = NULL;
2722 struct cli_state *targetcli;
2726 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2727 d_printf("posix_mkdir <filename> 0<mode>\n");
2730 mask = talloc_asprintf(ctx,
2732 client_get_cur_dir(),
2738 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2739 d_printf("posix_mkdir <filename> 0<mode>\n");
2742 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2744 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2746 if (!NT_STATUS_IS_OK(status)) {
2747 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2751 status = cli_posix_mkdir(targetcli, targetname, mode);
2752 if (!NT_STATUS_IS_OK(status)) {
2753 d_printf("Failed to open file %s. %s\n",
2754 targetname, nt_errstr(status));
2756 d_printf("posix_mkdir created directory %s\n", targetname);
2761 static int cmd_posix_unlink(void)
2763 TALLOC_CTX *ctx = talloc_tos();
2766 char *targetname = NULL;
2767 struct cli_state *targetcli;
2770 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2771 d_printf("posix_unlink <filename>\n");
2774 mask = talloc_asprintf(ctx,
2776 client_get_cur_dir(),
2782 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2784 if (!NT_STATUS_IS_OK(status)) {
2785 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
2789 status = cli_posix_unlink(targetcli, targetname);
2790 if (!NT_STATUS_IS_OK(status)) {
2791 d_printf("Failed to unlink file %s. %s\n",
2792 targetname, nt_errstr(status));
2794 d_printf("posix_unlink deleted file %s\n", targetname);
2800 static int cmd_posix_rmdir(void)
2802 TALLOC_CTX *ctx = talloc_tos();
2805 char *targetname = NULL;
2806 struct cli_state *targetcli;
2809 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2810 d_printf("posix_rmdir <filename>\n");
2813 mask = talloc_asprintf(ctx,
2815 client_get_cur_dir(),
2821 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2823 if (!NT_STATUS_IS_OK(status)) {
2824 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
2828 status = cli_posix_rmdir(targetcli, targetname);
2829 if (!NT_STATUS_IS_OK(status)) {
2830 d_printf("Failed to unlink directory %s. %s\n",
2831 targetname, nt_errstr(status));
2833 d_printf("posix_rmdir deleted directory %s\n", targetname);
2839 static int cmd_close(void)
2841 TALLOC_CTX *ctx = talloc_tos();
2846 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2847 d_printf("close <fnum>\n");
2852 /* We really should use the targetcli here.... */
2853 status = cli_close(cli, fnum);
2854 if (!NT_STATUS_IS_OK(status)) {
2855 d_printf("close %d: %s\n", fnum, nt_errstr(status));
2861 static int cmd_posix(void)
2863 TALLOC_CTX *ctx = talloc_tos();
2864 uint16 major, minor;
2865 uint32 caplow, caphigh;
2869 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2870 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2874 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
2876 if (!NT_STATUS_IS_OK(status)) {
2877 d_printf("Can't get UNIX CIFS extensions version from "
2878 "server: %s\n", nt_errstr(status));
2882 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2884 caps = talloc_strdup(ctx, "");
2888 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2889 caps = talloc_asprintf_append(caps, "locks ");
2894 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2895 caps = talloc_asprintf_append(caps, "acls ");
2900 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2901 caps = talloc_asprintf_append(caps, "eas ");
2906 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2907 caps = talloc_asprintf_append(caps, "pathnames ");
2912 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2913 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2918 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2919 caps = talloc_asprintf_append(caps, "large_read ");
2924 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2925 caps = talloc_asprintf_append(caps, "large_write ");
2930 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2931 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2936 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2937 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2943 if (*caps && caps[strlen(caps)-1] == ' ') {
2944 caps[strlen(caps)-1] = '\0';
2947 d_printf("Server supports CIFS capabilities %s\n", caps);
2949 status = cli_set_unix_extensions_capabilities(cli, major, minor,
2951 if (!NT_STATUS_IS_OK(status)) {
2952 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2957 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2958 CLI_DIRSEP_CHAR = '/';
2959 *CLI_DIRSEP_STR = '/';
2960 client_set_cur_dir(CLI_DIRSEP_STR);
2966 static int cmd_lock(void)
2968 TALLOC_CTX *ctx = talloc_tos();
2970 uint64_t start, len;
2971 enum brl_type lock_type;
2975 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2976 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2981 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2982 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2986 if (*buf == 'r' || *buf == 'R') {
2987 lock_type = READ_LOCK;
2988 } else if (*buf == 'w' || *buf == 'W') {
2989 lock_type = WRITE_LOCK;
2991 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2995 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2996 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3000 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3002 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3003 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3007 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3009 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
3010 if (!NT_STATUS_IS_OK(status)) {
3011 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
3017 static int cmd_unlock(void)
3019 TALLOC_CTX *ctx = talloc_tos();
3021 uint64_t start, len;
3025 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3026 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3031 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3032 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3036 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3038 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3039 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3043 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3045 status = cli_posix_unlock(cli, fnum, start, len);
3046 if (!NT_STATUS_IS_OK(status)) {
3047 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
3054 /****************************************************************************
3056 ****************************************************************************/
3058 static int cmd_rmdir(void)
3060 TALLOC_CTX *ctx = talloc_tos();
3063 char *targetname = NULL;
3064 struct cli_state *targetcli;
3067 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3068 d_printf("rmdir <dirname>\n");
3071 mask = talloc_asprintf(ctx,
3073 client_get_cur_dir(),
3079 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
3081 if (!NT_STATUS_IS_OK(status)) {
3082 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3086 status = cli_rmdir(targetcli, targetname);
3087 if (!NT_STATUS_IS_OK(status)) {
3088 d_printf("%s removing remote directory file %s\n",
3089 nt_errstr(status), mask);
3095 /****************************************************************************
3097 ****************************************************************************/
3099 static int cmd_link(void)
3101 TALLOC_CTX *ctx = talloc_tos();
3102 char *oldname = NULL;
3103 char *newname = NULL;
3106 char *targetname = NULL;
3107 struct cli_state *targetcli;
3110 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3111 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3112 d_printf("link <oldname> <newname>\n");
3115 oldname = talloc_asprintf(ctx,
3117 client_get_cur_dir(),
3122 newname = talloc_asprintf(ctx,
3124 client_get_cur_dir(),
3130 status = cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli,
3132 if (!NT_STATUS_IS_OK(status)) {
3133 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3137 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3138 d_printf("Server doesn't support UNIX CIFS calls.\n");
3142 status = cli_posix_hardlink(targetcli, targetname, newname);
3143 if (!NT_STATUS_IS_OK(status)) {
3144 d_printf("%s linking files (%s -> %s)\n",
3145 nt_errstr(status), newname, oldname);
3151 /****************************************************************************
3153 ****************************************************************************/
3155 static int cmd_readlink(void)
3157 TALLOC_CTX *ctx = talloc_tos();
3160 char *targetname = NULL;
3161 char linkname[PATH_MAX+1];
3162 struct cli_state *targetcli;
3165 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3166 d_printf("readlink <name>\n");
3169 name = talloc_asprintf(ctx,
3171 client_get_cur_dir(),
3177 status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
3179 if (!NT_STATUS_IS_OK(status)) {
3180 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3184 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3185 d_printf("Server doesn't support UNIX CIFS calls.\n");
3189 status = cli_posix_readlink(targetcli, name, linkname, PATH_MAX+1);
3190 if (!NT_STATUS_IS_OK(status)) {
3191 d_printf("%s readlink on file %s\n",
3192 nt_errstr(status), name);
3196 d_printf("%s -> %s\n", name, linkname);
3202 /****************************************************************************
3204 ****************************************************************************/
3206 static int cmd_symlink(void)
3208 TALLOC_CTX *ctx = talloc_tos();
3209 char *oldname = NULL;
3210 char *newname = NULL;
3213 struct cli_state *newcli;
3216 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3217 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3218 d_printf("symlink <oldname> <newname>\n");
3221 /* Oldname (link target) must be an untouched blob. */
3224 if (SERVER_HAS_UNIX_CIFS(cli)) {
3225 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3230 /* New name must be present in share namespace. */
3231 status = cli_resolve_path(ctx, "", auth_info, cli, newname,
3233 if (!NT_STATUS_IS_OK(status)) {
3234 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3237 status = cli_posix_symlink(newcli, oldname, newname);
3239 status = cli_symlink(
3241 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3244 if (!NT_STATUS_IS_OK(status)) {
3245 d_printf("%s symlinking files (%s -> %s)\n",
3246 nt_errstr(status), oldname, newname);
3253 /****************************************************************************
3255 ****************************************************************************/
3257 static int cmd_chmod(void)
3259 TALLOC_CTX *ctx = talloc_tos();
3263 char *targetname = NULL;
3264 struct cli_state *targetcli;
3268 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3269 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3270 d_printf("chmod mode file\n");
3273 src = talloc_asprintf(ctx,
3275 client_get_cur_dir(),
3281 mode = (mode_t)strtol(buf, NULL, 8);
3283 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3285 if (!NT_STATUS_IS_OK(status)) {
3286 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3290 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3291 d_printf("Server doesn't support UNIX CIFS calls.\n");
3295 status = cli_posix_chmod(targetcli, targetname, mode);
3296 if (!NT_STATUS_IS_OK(status)) {
3297 d_printf("%s chmod file %s 0%o\n",
3298 nt_errstr(status), src, (unsigned int)mode);
3305 static const char *filetype_to_str(mode_t mode)
3307 if (S_ISREG(mode)) {
3308 return "regular file";
3309 } else if (S_ISDIR(mode)) {
3313 if (S_ISCHR(mode)) {
3314 return "character device";
3318 if (S_ISBLK(mode)) {
3319 return "block device";
3323 if (S_ISFIFO(mode)) {
3328 if (S_ISLNK(mode)) {
3329 return "symbolic link";
3333 if (S_ISSOCK(mode)) {
3340 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3349 static char *unix_mode_to_str(char *s, mode_t m)
3352 const char *str = filetype_to_str(m);
3368 *p++ = str[1] == 'y' ? 'l' : 's';
3375 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3376 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3377 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3378 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3379 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3380 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3381 *p++ = rwx_to_str(m, S_IROTH, 'r');
3382 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3383 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3388 /****************************************************************************
3389 Utility function for UNIX getfacl.
3390 ****************************************************************************/
3392 static char *perms_to_string(fstring permstr, unsigned char perms)
3394 fstrcpy(permstr, "---");
3395 if (perms & SMB_POSIX_ACL_READ) {
3398 if (perms & SMB_POSIX_ACL_WRITE) {
3401 if (perms & SMB_POSIX_ACL_EXECUTE) {
3407 /****************************************************************************
3409 ****************************************************************************/
3411 static int cmd_getfacl(void)
3413 TALLOC_CTX *ctx = talloc_tos();
3416 char *targetname = NULL;
3417 struct cli_state *targetcli;
3418 uint16 major, minor;
3419 uint32 caplow, caphigh;
3420 char *retbuf = NULL;
3422 SMB_STRUCT_STAT sbuf;
3423 uint16 num_file_acls = 0;
3424 uint16 num_dir_acls = 0;
3428 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3429 d_printf("getfacl filename\n");
3432 src = talloc_asprintf(ctx,
3434 client_get_cur_dir(),
3440 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3442 if (!NT_STATUS_IS_OK(status)) {
3443 d_printf("stat %s: %s\n", src, nt_errstr(status));
3447 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3448 d_printf("Server doesn't support UNIX CIFS calls.\n");
3452 status = cli_unix_extensions_version(targetcli, &major, &minor,
3454 if (!NT_STATUS_IS_OK(status)) {
3455 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3460 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3461 d_printf("This server supports UNIX extensions "
3462 "but doesn't support POSIX ACLs.\n");
3466 status = cli_posix_stat(targetcli, targetname, &sbuf);
3467 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3468 d_printf("%s getfacl doing a stat on file %s\n",
3469 nt_errstr(status), src);
3473 status = cli_posix_getfacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3474 if (!NT_STATUS_IS_OK(status)) {
3475 d_printf("%s getfacl file %s\n",
3476 nt_errstr(status), src);
3480 /* ToDo : Print out the ACL values. */
3481 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3482 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3483 src, (unsigned int)CVAL(retbuf,0) );
3487 num_file_acls = SVAL(retbuf,2);
3488 num_dir_acls = SVAL(retbuf,4);
3489 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3490 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3492 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3493 (unsigned int)rb_size);
3497 d_printf("# file: %s\n", src);
3498 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3500 if (num_file_acls == 0 && num_dir_acls == 0) {
3501 d_printf("No acls found.\n");
3504 for (i = 0; i < num_file_acls; i++) {
3507 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3508 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3511 case SMB_POSIX_ACL_USER_OBJ:
3514 case SMB_POSIX_ACL_USER:
3515 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3516 d_printf("user:%u:", uorg);
3518 case SMB_POSIX_ACL_GROUP_OBJ:
3519 d_printf("group::");
3521 case SMB_POSIX_ACL_GROUP:
3522 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3523 d_printf("group:%u:", uorg);
3525 case SMB_POSIX_ACL_MASK:
3528 case SMB_POSIX_ACL_OTHER:
3529 d_printf("other::");
3532 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3533 src, (unsigned int)tagtype );
3538 d_printf("%s\n", perms_to_string(permstring, perms));
3541 for (i = 0; i < num_dir_acls; i++) {
3544 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3545 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3548 case SMB_POSIX_ACL_USER_OBJ:
3549 d_printf("default:user::");
3551 case SMB_POSIX_ACL_USER:
3552 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3553 d_printf("default:user:%u:", uorg);
3555 case SMB_POSIX_ACL_GROUP_OBJ:
3556 d_printf("default:group::");
3558 case SMB_POSIX_ACL_GROUP:
3559 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3560 d_printf("default:group:%u:", uorg);
3562 case SMB_POSIX_ACL_MASK:
3563 d_printf("default:mask::");
3565 case SMB_POSIX_ACL_OTHER:
3566 d_printf("default:other::");
3569 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3570 src, (unsigned int)tagtype );
3575 d_printf("%s\n", perms_to_string(permstring, perms));
3581 static void printf_cb(const char *buf, void *private_data)
3586 /****************************************************************************
3587 Get the EA list of a file
3588 ****************************************************************************/
3590 static int cmd_geteas(void)
3592 TALLOC_CTX *ctx = talloc_tos();
3595 char *targetname = NULL;
3596 struct cli_state *targetcli;
3599 struct ea_struct *eas;
3601 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3602 d_printf("geteas filename\n");
3605 src = talloc_asprintf(ctx,
3607 client_get_cur_dir(),
3613 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3615 if (!NT_STATUS_IS_OK(status)) {
3616 d_printf("stat %s: %s\n", src, nt_errstr(status));
3620 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3622 if (!NT_STATUS_IS_OK(status)) {
3623 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3627 for (i=0; i<num_eas; i++) {
3628 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3629 dump_data_cb(eas[i].value.data, eas[i].value.length, false,
3639 /****************************************************************************
3641 ****************************************************************************/
3643 static int cmd_setea(void)
3645 TALLOC_CTX *ctx = talloc_tos();
3648 char *eaname = NULL;
3649 char *eavalue = NULL;
3650 char *targetname = NULL;
3651 struct cli_state *targetcli;
3654 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
3655 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
3656 d_printf("setea filename eaname value\n");
3659 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
3660 eavalue = talloc_strdup(ctx, "");
3662 src = talloc_asprintf(ctx,
3664 client_get_cur_dir(),
3670 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3672 if (!NT_STATUS_IS_OK(status)) {
3673 d_printf("stat %s: %s\n", src, nt_errstr(status));
3677 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
3679 if (!NT_STATUS_IS_OK(status)) {
3680 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
3687 /****************************************************************************
3689 ****************************************************************************/
3691 static int cmd_stat(void)
3693 TALLOC_CTX *ctx = talloc_tos();
3696 char *targetname = NULL;
3697 struct cli_state *targetcli;
3699 SMB_STRUCT_STAT sbuf;
3704 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3705 d_printf("stat file\n");
3708 src = talloc_asprintf(ctx,
3710 client_get_cur_dir(),
3716 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3718 if (!NT_STATUS_IS_OK(status)) {
3719 d_printf("stat %s: %s\n", src, nt_errstr(status));
3723 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3724 d_printf("Server doesn't support UNIX CIFS calls.\n");
3728 status = cli_posix_stat(targetcli, targetname, &sbuf);
3729 if (!NT_STATUS_IS_OK(status)) {
3730 d_printf("%s stat file %s\n",
3731 nt_errstr(status), src);
3735 /* Print out the stat values. */
3736 d_printf("File: %s\n", src);
3737 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3738 (double)sbuf.st_ex_size,
3739 (unsigned int)sbuf.st_ex_blocks,
3740 filetype_to_str(sbuf.st_ex_mode));
3742 #if defined(S_ISCHR) && defined(S_ISBLK)
3743 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
3744 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3745 (double)sbuf.st_ex_ino,
3746 (unsigned int)sbuf.st_ex_nlink,
3747 unix_dev_major(sbuf.st_ex_rdev),
3748 unix_dev_minor(sbuf.st_ex_rdev));
3751 d_printf("Inode: %.0f\tLinks: %u\n",
3752 (double)sbuf.st_ex_ino,
3753 (unsigned int)sbuf.st_ex_nlink);
3755 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3756 ((int)sbuf.st_ex_mode & 0777),
3757 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
3758 (unsigned int)sbuf.st_ex_uid,
3759 (unsigned int)sbuf.st_ex_gid);
3761 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
3762 lt = localtime(&tmp_time);
3764 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3766 fstrcpy(mode_str, "unknown");
3768 d_printf("Access: %s\n", mode_str);
3770 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3771 lt = localtime(&tmp_time);
3773 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3775 fstrcpy(mode_str, "unknown");
3777 d_printf("Modify: %s\n", mode_str);
3779 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
3780 lt = localtime(&tmp_time);
3782 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3784 fstrcpy(mode_str, "unknown");
3786 d_printf("Change: %s\n", mode_str);
3792 /****************************************************************************
3794 ****************************************************************************/
3796 static int cmd_chown(void)
3798 TALLOC_CTX *ctx = talloc_tos();
3802 char *buf, *buf2, *buf3;
3803 struct cli_state *targetcli;
3804 char *targetname = NULL;
3807 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3808 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3809 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3810 d_printf("chown uid gid file\n");
3814 uid = (uid_t)atoi(buf);
3815 gid = (gid_t)atoi(buf2);
3817 src = talloc_asprintf(ctx,
3819 client_get_cur_dir(),
3824 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3826 if (!NT_STATUS_IS_OK(status)) {
3827 d_printf("chown %s: %s\n", src, nt_errstr(status));
3831 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3832 d_printf("Server doesn't support UNIX CIFS calls.\n");
3836 status = cli_posix_chown(targetcli, targetname, uid, gid);
3837 if (!NT_STATUS_IS_OK(status)) {
3838 d_printf("%s chown file %s uid=%d, gid=%d\n",
3839 nt_errstr(status), src, (int)uid, (int)gid);
3846 /****************************************************************************
3848 ****************************************************************************/
3850 static int cmd_rename(void)
3852 TALLOC_CTX *ctx = talloc_tos();
3855 struct cli_state *targetcli;
3860 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3861 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3862 d_printf("rename <src> <dest>\n");
3866 src = talloc_asprintf(ctx,
3868 client_get_cur_dir(),
3874 dest = talloc_asprintf(ctx,
3876 client_get_cur_dir(),
3882 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3884 if (!NT_STATUS_IS_OK(status)) {
3885 d_printf("rename %s: %s\n", src, nt_errstr(status));
3889 status = cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli,
3891 if (!NT_STATUS_IS_OK(status)) {
3892 d_printf("rename %s: %s\n", dest, nt_errstr(status));
3896 status = cli_rename(targetcli, targetsrc, targetdest);
3897 if (!NT_STATUS_IS_OK(status)) {
3898 d_printf("%s renaming files %s -> %s \n",
3908 /****************************************************************************
3909 Print the volume name.
3910 ****************************************************************************/
3912 static int cmd_volume(void)
3915 uint32_t serial_num;
3919 status = cli_get_fs_volume_info(cli, talloc_tos(),
3920 &volname, &serial_num,
3922 if (!NT_STATUS_IS_OK(status)) {
3923 d_printf("Error %s getting volume info\n", nt_errstr(status));
3927 d_printf("Volume: |%s| serial number 0x%x\n",
3928 volname, (unsigned int)serial_num);
3932 /****************************************************************************
3933 Hard link files using the NT call.
3934 ****************************************************************************/
3936 static int cmd_hardlink(void)
3938 TALLOC_CTX *ctx = talloc_tos();
3941 struct cli_state *targetcli;
3945 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3946 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3947 d_printf("hardlink <src> <dest>\n");
3951 src = talloc_asprintf(ctx,
3953 client_get_cur_dir(),
3959 dest = talloc_asprintf(ctx,
3961 client_get_cur_dir(),
3967 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3969 if (!NT_STATUS_IS_OK(status)) {
3970 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
3974 status = cli_nt_hardlink(targetcli, targetname, dest);
3975 if (!NT_STATUS_IS_OK(status)) {
3976 d_printf("%s doing an NT hard link of files\n",
3984 /****************************************************************************
3985 Toggle the prompt flag.
3986 ****************************************************************************/
3988 static int cmd_prompt(void)
3991 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
3995 /****************************************************************************
3996 Set the newer than time.
3997 ****************************************************************************/
3999 static int cmd_newer(void)
4001 TALLOC_CTX *ctx = talloc_tos();
4004 SMB_STRUCT_STAT sbuf;
4006 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
4007 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
4008 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4009 DEBUG(1,("Getting files newer than %s",
4010 time_to_asc(newer_than)));
4015 if (ok && newer_than == 0) {
4016 d_printf("Error setting newer-than time\n");
4023 /****************************************************************************
4024 Set the archive level.
4025 ****************************************************************************/
4027 static int cmd_archive(void)
4029 TALLOC_CTX *ctx = talloc_tos();
4032 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4033 archive_level = atoi(buf);
4035 d_printf("Archive level is %d\n",archive_level);
4041 /****************************************************************************
4042 Toggle the backup_intent state.
4043 ****************************************************************************/
4045 static int cmd_backup(void)
4047 backup_intent = !backup_intent;
4048 cli_set_backup_intent(cli, backup_intent);
4049 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4053 /****************************************************************************
4054 Toggle the lowercaseflag.
4055 ****************************************************************************/
4057 static int cmd_lowercase(void)
4059 lowercase = !lowercase;
4060 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4064 /****************************************************************************
4065 Toggle the case sensitive flag.
4066 ****************************************************************************/
4068 static int cmd_setcase(void)
4070 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4072 cli_set_case_sensitive(cli, !orig_case_sensitive);
4073 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4078 /****************************************************************************
4079 Toggle the showacls flag.
4080 ****************************************************************************/
4082 static int cmd_showacls(void)
4084 showacls = !showacls;
4085 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4090 /****************************************************************************
4091 Toggle the recurse flag.
4092 ****************************************************************************/
4094 static int cmd_recurse(void)
4097 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4101 /****************************************************************************
4102 Toggle the translate flag.
4103 ****************************************************************************/
4105 static int cmd_translate(void)
4107 translation = !translation;
4108 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4109 translation?"on":"off"));
4113 /****************************************************************************
4115 ****************************************************************************/
4117 static int cmd_lcd(void)
4119 TALLOC_CTX *ctx = talloc_tos();
4123 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4124 if (chdir(buf) == -1) {
4125 d_printf("chdir to %s failed (%s)\n",
4126 buf, strerror(errno));
4133 DEBUG(2,("the local directory is now %s\n",d));
4138 /****************************************************************************
4139 Get a file restarting at end of local file.
4140 ****************************************************************************/
4142 static int cmd_reget(void)
4144 TALLOC_CTX *ctx = talloc_tos();
4145 char *local_name = NULL;
4146 char *remote_name = NULL;
4150 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4155 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4156 d_printf("reget <filename>\n");
4159 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4163 remote_name = clean_name(ctx,remote_name);
4169 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4174 return do_get(remote_name, local_name, true);
4177 /****************************************************************************
4178 Put a file restarting at end of local file.
4179 ****************************************************************************/
4181 static int cmd_reput(void)
4183 TALLOC_CTX *ctx = talloc_tos();
4184 char *local_name = NULL;
4185 char *remote_name = NULL;
4189 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4194 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4195 d_printf("reput <filename>\n");
4199 if (!file_exist_stat(local_name, &st, false)) {
4200 d_printf("%s does not exist\n", local_name);
4204 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4205 remote_name = talloc_asprintf_append(remote_name,
4208 remote_name = talloc_asprintf_append(remote_name,
4215 remote_name = clean_name(ctx, remote_name);
4220 return do_put(remote_name, local_name, true);
4223 /****************************************************************************
4225 ****************************************************************************/
4227 static void browse_fn(const char *name, uint32 m,
4228 const char *comment, void *state)
4230 const char *typestr = "";
4233 case STYPE_DISKTREE:
4237 typestr = "Printer";
4246 /* FIXME: If the remote machine returns non-ascii characters
4247 in any of these fields, they can corrupt the output. We
4248 should remove them. */
4250 d_printf("\t%-15s %-10.10s%s\n",
4251 name,typestr,comment);
4253 d_printf ("%s|%s|%s\n",typestr,name,comment);
4257 static bool browse_host_rpc(bool sort)
4260 struct rpc_pipe_client *pipe_hnd = NULL;
4261 TALLOC_CTX *frame = talloc_stackframe();
4263 struct srvsvc_NetShareInfoCtr info_ctr;
4264 struct srvsvc_NetShareCtr1 ctr1;
4265 uint32_t resume_handle = 0;
4266 uint32_t total_entries = 0;
4268 struct dcerpc_binding_handle *b;
4270 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
4273 if (!NT_STATUS_IS_OK(status)) {
4274 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4275 nt_errstr(status)));
4280 b = pipe_hnd->binding_handle;
4282 ZERO_STRUCT(info_ctr);
4286 info_ctr.ctr.ctr1 = &ctr1;
4288 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4296 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4297 TALLOC_FREE(pipe_hnd);
4302 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4303 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4304 browse_fn(info.name, info.type, info.comment, NULL);
4307 TALLOC_FREE(pipe_hnd);
4312 /****************************************************************************
4313 Try and browse available connections on a host.
4314 ****************************************************************************/
4316 static bool browse_host(bool sort)
4320 d_printf("\n\tSharename Type Comment\n");
4321 d_printf("\t--------- ---- -------\n");
4324 if (browse_host_rpc(sort)) {
4328 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1) {
4329 NTSTATUS status = cli_nt_error(cli);
4330 d_printf("Error returning browse list: %s\n",
4337 /****************************************************************************
4339 ****************************************************************************/
4341 static void server_fn(const char *name, uint32 m,
4342 const char *comment, void *state)
4346 d_printf("\t%-16s %s\n", name, comment);
4348 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4352 /****************************************************************************
4353 Try and browse available connections on a host.
4354 ****************************************************************************/
4356 static bool list_servers(const char *wk_grp)
4360 if (!cli->server_domain)
4364 d_printf("\n\tServer Comment\n");
4365 d_printf("\t--------- -------\n");
4367 fstrcpy( state, "Server" );
4368 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4372 d_printf("\n\tWorkgroup Master\n");
4373 d_printf("\t--------- -------\n");
4376 fstrcpy( state, "Workgroup" );
4377 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4382 /****************************************************************************
4383 Print or set current VUID
4384 ****************************************************************************/
4386 static int cmd_vuid(void)
4388 TALLOC_CTX *ctx = talloc_tos();
4391 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4392 d_printf("Current VUID is %d\n",
4393 cli_state_get_uid(cli));
4397 cli_state_set_uid(cli, atoi(buf));
4401 /****************************************************************************
4402 Setup a new VUID, by issuing a session setup
4403 ****************************************************************************/
4405 static int cmd_logon(void)
4407 TALLOC_CTX *ctx = talloc_tos();
4408 char *l_username, *l_password;
4411 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
4412 d_printf("logon <username> [<password>]\n");
4416 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
4417 char *pass = getpass("Password: ");
4419 l_password = talloc_strdup(ctx,pass);
4426 nt_status = cli_session_setup(cli, l_username,
4427 l_password, strlen(l_password),
4428 l_password, strlen(l_password),
4430 if (!NT_STATUS_IS_OK(nt_status)) {
4431 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
4435 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
4443 static int cmd_logoff(void)
4447 status = cli_ulogoff(cli);
4448 if (!NT_STATUS_IS_OK(status)) {
4449 d_printf("logoff failed: %s\n", nt_errstr(status));
4453 d_printf("logoff successful\n");
4459 * tree connect (connect to a share)
4462 static int cmd_tcon(void)
4464 TALLOC_CTX *ctx = talloc_tos();
4468 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
4469 d_printf("tcon <sharename>\n");
4477 status = cli_tree_connect(cli, sharename, "?????", "", 0);
4478 if (!NT_STATUS_IS_OK(status)) {
4479 d_printf("tcon failed: %s\n", nt_errstr(status));
4483 talloc_free(sharename);
4485 d_printf("tcon to %s successful, tid: %u\n", sharename,
4486 cli_state_get_tid(cli));
4491 * tree disconnect (disconnect from a share)
4494 static int cmd_tdis(void)
4498 status = cli_tdis(cli);
4499 if (!NT_STATUS_IS_OK(status)) {
4500 d_printf("tdis failed: %s\n", nt_errstr(status));
4504 d_printf("tdis successful\n");
4513 static int cmd_tid(void)
4515 TALLOC_CTX *ctx = talloc_tos();
4518 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
4519 if (cli_state_has_tcon(cli)) {
4520 d_printf("current tid is %d\n", cli_state_get_tid(cli));
4522 d_printf("no tcon currently\n");
4525 uint16_t tid = atoi(tid_str);
4526 cli_state_set_tid(cli, tid);
4533 /****************************************************************************
4534 list active connections
4535 ****************************************************************************/
4537 static int cmd_list_connect(void)
4539 cli_cm_display(cli);
4543 /****************************************************************************
4544 display the current active client connection
4545 ****************************************************************************/
4547 static int cmd_show_connect( void )
4549 TALLOC_CTX *ctx = talloc_tos();
4550 struct cli_state *targetcli;
4554 status = cli_resolve_path(ctx, "", auth_info, cli,
4555 client_get_cur_dir(), &targetcli,
4557 if (!NT_STATUS_IS_OK(status)) {
4558 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
4562 d_printf("//%s/%s\n", cli_state_remote_name(targetcli), targetcli->share);
4566 /****************************************************************************
4568 ***************************************************************************/
4570 int cmd_iosize(void)
4572 TALLOC_CTX *ctx = talloc_tos();
4576 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4578 d_printf("iosize <n> or iosize 0x<n>. "
4579 "Minimum is 16384 (0x4000), "
4580 "max is 16776960 (0xFFFF00)\n");
4582 d_printf("iosize <n> or iosize 0x<n>. "
4583 "(Encrypted connection) ,"
4584 "Minimum is 16384 (0x4000), "
4585 "max is 130048 (0x1FC00)\n");
4590 iosize = strtol(buf,NULL,0);
4591 if (smb_encrypt && (iosize < 0x4000 || iosize > 0xFC00)) {
4592 d_printf("iosize out of range for encrypted "
4593 "connection (min = 16384 (0x4000), "
4594 "max = 130048 (0x1FC00)");
4596 } else if (!smb_encrypt && (iosize < 0x4000 || iosize > 0xFFFF00)) {
4597 d_printf("iosize out of range (min = 16384 (0x4000), "
4598 "max = 16776960 (0xFFFF00)");
4602 io_bufsize = iosize;
4603 d_printf("iosize is now %d\n", io_bufsize);
4607 /****************************************************************************
4609 ****************************************************************************/
4610 static int cmd_history(void)
4612 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4616 hlist = history_list();
4618 for (i = 0; hlist && hlist[i]; i++) {
4619 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
4622 DEBUG(0,("no history without readline support\n"));
4628 /* Some constants for completing filename arguments */
4630 #define COMPL_NONE 0 /* No completions */
4631 #define COMPL_REMOTE 1 /* Complete remote filename */
4632 #define COMPL_LOCAL 2 /* Complete local filename */
4634 /* This defines the commands supported by this client.
4635 * NOTE: The "!" must be the last one in the list because it's fn pointer
4636 * field is NULL, and NULL in that field is used in process_tok()
4637 * (below) to indicate the end of the list. crh
4642 const char *description;
4643 char compl_args[2]; /* Completion argument info */
4645 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4646 {"allinfo",cmd_allinfo,"<file> show all available info",
4647 {COMPL_NONE,COMPL_NONE}},
4648 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
4649 {"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}},
4650 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
4651 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
4652 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
4653 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
4654 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
4655 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
4656 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
4657 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
4658 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4659 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4660 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4661 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
4662 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4663 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
4664 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
4665 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
4666 {COMPL_REMOTE, COMPL_LOCAL}},
4667 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4668 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4669 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
4670 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
4671 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
4672 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4673 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4674 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
4675 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4676 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4677 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
4678 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4679 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
4680 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4681 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
4682 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
4683 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
4684 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
4685 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
4686 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
4687 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4688 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4689 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4690 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4691 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
4692 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
4693 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
4694 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
4695 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4696 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
4697 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4698 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4699 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4700 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
4701 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
4702 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
4703 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
4704 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4705 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4706 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
4707 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
4708 {COMPL_REMOTE, COMPL_LOCAL}},
4709 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
4710 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
4711 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4712 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
4713 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
4714 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
4715 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4716 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
4717 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
4718 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4719 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
4720 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
4721 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
4722 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
4723 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
4724 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
4725 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
4726 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
4728 /* Yes, this must be here, see crh's comment above. */
4729 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
4730 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
4733 /*******************************************************************
4734 Lookup a command string in the list of commands, including
4736 ******************************************************************/
4738 static int process_tok(char *tok)
4740 int i = 0, matches = 0;
4742 int tok_len = strlen(tok);
4744 while (commands[i].fn != NULL) {
4745 if (strequal(commands[i].name,tok)) {
4749 } else if (strnequal(commands[i].name, tok, tok_len)) {
4758 else if (matches == 1)
4764 /****************************************************************************
4766 ****************************************************************************/
4768 static int cmd_help(void)
4770 TALLOC_CTX *ctx = talloc_tos();
4774 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4775 if ((i = process_tok(buf)) >= 0)
4776 d_printf("HELP %s:\n\t%s\n\n",
4777 commands[i].name,commands[i].description);
4779 while (commands[i].description) {
4780 for (j=0; commands[i].description && (j<5); j++) {
4781 d_printf("%-15s",commands[i].name);
4790 /****************************************************************************
4791 Process a -c command string.
4792 ****************************************************************************/
4794 static int process_command_string(const char *cmd_in)
4796 TALLOC_CTX *ctx = talloc_tos();
4797 char *cmd = talloc_strdup(ctx, cmd_in);
4803 /* establish the connection if not already */
4808 status = cli_cm_open(talloc_tos(), NULL,
4809 have_ip ? dest_ss_str : desthost,
4812 max_protocol, port, name_type,
4814 if (!NT_STATUS_IS_OK(status)) {
4819 while (cmd[0] != '\0') {
4825 if ((p = strchr_m(cmd, ';')) == 0) {
4834 /* and get the first part of the command */
4836 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
4840 if ((i = process_tok(tok)) >= 0) {
4841 rc = commands[i].fn();
4842 } else if (i == -2) {
4843 d_printf("%s: command abbreviation ambiguous\n",tok);
4845 d_printf("%s: command not found\n",tok);
4852 #define MAX_COMPLETIONS 100
4854 struct completion_remote {
4862 static NTSTATUS completion_remote_filter(const char *mnt,
4863 struct file_info *f,
4867 struct completion_remote *info = (struct completion_remote *)state;
4869 if (info->count >= MAX_COMPLETIONS - 1) {
4870 return NT_STATUS_OK;
4872 if (strncmp(info->text, f->name, info->len) != 0) {
4873 return NT_STATUS_OK;
4875 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
4876 return NT_STATUS_OK;
4879 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
4880 info->matches[info->count] = SMB_STRDUP(f->name);
4882 TALLOC_CTX *ctx = talloc_stackframe();
4885 tmp = talloc_strdup(ctx,info->dirmask);
4888 return NT_STATUS_NO_MEMORY;
4890 tmp = talloc_asprintf_append(tmp, "%s", f->name);
4893 return NT_STATUS_NO_MEMORY;
4895 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4896 tmp = talloc_asprintf_append(tmp, "%s",
4901 return NT_STATUS_NO_MEMORY;
4903 info->matches[info->count] = SMB_STRDUP(tmp);
4906 if (info->matches[info->count] == NULL) {
4907 return NT_STATUS_OK;
4909 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4910 smb_readline_ca_char(0);
4912 if (info->count == 1) {
4913 info->samelen = strlen(info->matches[info->count]);
4915 while (strncmp(info->matches[info->count],
4916 info->matches[info->count-1],
4917 info->samelen) != 0) {
4922 return NT_STATUS_OK;
4925 static char **remote_completion(const char *text, int len)
4927 TALLOC_CTX *ctx = talloc_stackframe();
4928 char *dirmask = NULL;
4929 char *targetpath = NULL;
4930 struct cli_state *targetcli = NULL;
4932 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
4935 /* can't have non-static initialisation on Sun CC, so do it
4941 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
4942 if (!info.matches) {
4948 * We're leaving matches[0] free to fill it later with the text to
4949 * display: Either the one single match or the longest common subset
4952 info.matches[0] = NULL;
4955 for (i = len-1; i >= 0; i--) {
4956 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
4961 info.text = text+i+1;
4962 info.samelen = info.len = len-i-1;
4965 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
4966 if (!info.dirmask) {
4969 strncpy(info.dirmask, text, i+1);
4970 info.dirmask[i+1] = 0;
4971 dirmask = talloc_asprintf(ctx,
4973 client_get_cur_dir(),
4977 info.dirmask = SMB_STRDUP("");
4978 if (!info.dirmask) {
4981 dirmask = talloc_asprintf(ctx,
4983 client_get_cur_dir());
4989 status = cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli,
4991 if (!NT_STATUS_IS_OK(status)) {
4994 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
4995 completion_remote_filter, (void *)&info);
4996 if (!NT_STATUS_IS_OK(status)) {
5000 if (info.count == 1) {
5002 * No matches at all, NULL indicates there is nothing
5004 SAFE_FREE(info.matches[0]);
5005 SAFE_FREE(info.matches);
5010 if (info.count == 2) {
5012 * Exactly one match in matches[1], indicate this is the one
5015 info.matches[0] = info.matches[1];
5016 info.matches[1] = NULL;
5019 return info.matches;
5023 * We got more than one possible match, set the result to the maximum
5027 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
5028 info.matches[info.count] = NULL;
5029 return info.matches;
5032 for (i = 0; i < info.count; i++) {
5033 SAFE_FREE(info.matches[i]);
5035 SAFE_FREE(info.matches);
5036 SAFE_FREE(info.dirmask);
5041 static char **completion_fn(const char *text, int start, int end)
5043 smb_readline_ca_char(' ');
5046 const char *buf, *sp;
5050 buf = smb_readline_get_line_buffer();
5054 sp = strchr(buf, ' ');
5058 for (i = 0; commands[i].name; i++) {
5059 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
5060 (commands[i].name[sp - buf] == 0)) {
5064 if (commands[i].name == NULL)
5070 if (sp == (buf + start))
5071 compl_type = commands[i].compl_args[0];
5073 compl_type = commands[i].compl_args[1];
5075 if (compl_type == COMPL_REMOTE)
5076 return remote_completion(text, end - start);
5077 else /* fall back to local filename completion */
5081 int i, len, samelen = 0, count=1;
5083 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
5090 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
5091 if (strncmp(text, commands[i].name, len) == 0) {
5092 matches[count] = SMB_STRDUP(commands[i].name);
5093 if (!matches[count])
5096 samelen = strlen(matches[count]);
5098 while (strncmp(matches[count], matches[count-1], samelen) != 0)
5105 case 0: /* should never happen */
5109 matches[0] = SMB_STRDUP(matches[1]);
5112 matches[0] = (char *)SMB_MALLOC(samelen+1);
5115 strncpy(matches[0], matches[1], samelen);
5116 matches[0][samelen] = 0;
5118 matches[count] = NULL;
5122 for (i = 0; i < count; i++)
5130 static bool finished;
5132 /****************************************************************************
5133 Make sure we swallow keepalives during idle time.
5134 ****************************************************************************/
5136 static void readline_callback(void)
5138 static time_t last_t;
5139 struct timespec now;
5142 unsigned char garbage[16];
5144 clock_gettime_mono(&now);
5152 /* Ping the server to keep the connection alive using SMBecho. */
5153 memset(garbage, 0xf0, sizeof(garbage));
5154 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
5155 if (!NT_STATUS_IS_OK(status)) {
5156 DEBUG(0, ("SMBecho failed (%s). Maybe server has closed "
5157 "the connection\n", nt_errstr(status)));
5159 smb_readline_done();
5163 /****************************************************************************
5164 Process commands on stdin.
5165 ****************************************************************************/
5167 static int process_stdin(void)
5172 TALLOC_CTX *frame = talloc_stackframe();
5174 char *the_prompt = NULL;
5178 /* display a prompt */
5179 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
5183 line = smb_readline(the_prompt, readline_callback, completion_fn);
5184 SAFE_FREE(the_prompt);
5190 /* special case - first char is ! */
5192 if (system(line + 1) == -1) {
5193 d_printf("system() command %s failed.\n",
5201 /* and get the first part of the command */
5203 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
5209 if ((i = process_tok(tok)) >= 0) {
5210 rc = commands[i].fn();
5211 } else if (i == -2) {
5212 d_printf("%s: command abbreviation ambiguous\n",tok);
5214 d_printf("%s: command not found\n",tok);
5222 /****************************************************************************
5223 Process commands from the client.
5224 ****************************************************************************/
5226 static int process(const char *base_directory)
5231 status = cli_cm_open(talloc_tos(), NULL,
5232 have_ip ? dest_ss_str : desthost,
5233 service, auth_info, true, smb_encrypt,
5234 max_protocol, port, name_type, &cli);
5235 if (!NT_STATUS_IS_OK(status)) {
5239 if (base_directory && *base_directory) {
5240 rc = do_cd(base_directory);
5248 rc = process_command_string(cmdstr);
5257 /****************************************************************************
5259 ****************************************************************************/
5261 static int do_host_query(const char *query_host)
5265 status = cli_cm_open(talloc_tos(), NULL,
5266 have_ip ? dest_ss_str : query_host,
5267 "IPC$", auth_info, true, smb_encrypt,
5268 max_protocol, port, name_type, &cli);
5269 if (!NT_STATUS_IS_OK(status)) {
5275 /* Ensure that the host can do IPv4 */
5277 if (!interpret_addr(query_host)) {
5278 struct sockaddr_storage ss;
5279 if (interpret_string_addr(&ss, query_host, 0) &&
5280 (ss.ss_family != AF_INET)) {
5281 d_printf("%s is an IPv6 address -- no workgroup available\n",
5289 /* Workgroups simply don't make sense over anything
5290 else but port 139... */
5293 status = cli_cm_open(talloc_tos(), NULL,
5294 have_ip ? dest_ss_str : query_host,
5295 "IPC$", auth_info, true, smb_encrypt,
5296 max_protocol, 139, name_type, &cli);
5297 if (!NT_STATUS_IS_OK(status)) {
5303 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
5307 list_servers(lp_workgroup());
5314 /****************************************************************************
5315 Handle a tar operation.
5316 ****************************************************************************/
5318 static int do_tar_op(const char *base_directory)
5322 /* do we already have a connection? */
5326 status = cli_cm_open(talloc_tos(), NULL,
5327 have_ip ? dest_ss_str : desthost,
5328 service, auth_info, true, smb_encrypt,
5329 max_protocol, port, name_type, &cli);
5330 if (!NT_STATUS_IS_OK(status)) {
5337 if (base_directory && *base_directory) {
5338 ret = do_cd(base_directory);
5352 /****************************************************************************
5353 Handle a message operation.
5354 ****************************************************************************/
5356 static int do_message_op(struct user_auth_info *a_info)
5360 status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
5361 port ? port : 139, name_type,
5362 lp_netbios_name(), SMB_SIGNING_DEFAULT, 0, &cli);
5363 if (!NT_STATUS_IS_OK(status)) {
5364 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
5368 send_message(get_cmdline_auth_info_username(a_info));
5374 /****************************************************************************
5376 ****************************************************************************/
5378 int main(int argc,char *argv[])
5380 char *base_directory = NULL;
5382 char *query_host = NULL;
5383 bool message = false;
5384 static const char *new_name_resolve_order = NULL;
5388 bool tar_opt = false;
5389 bool service_opt = false;
5390 struct poptOption long_options[] = {
5393 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
5394 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
5395 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
5396 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
5397 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
5398 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
5399 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
5400 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
5401 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
5402 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
5403 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
5404 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
5405 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
5407 POPT_COMMON_CONNECTION
5408 POPT_COMMON_CREDENTIALS
5411 TALLOC_CTX *frame = talloc_stackframe();
5413 if (!client_set_cur_dir("\\")) {
5417 /* set default debug level to 1 regardless of what smb.conf sets */
5418 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
5421 lp_set_cmdline("log level", "1");
5423 auth_info = user_auth_info_init(frame);
5424 if (auth_info == NULL) {
5427 popt_common_set_auth_info(auth_info);
5430 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
5431 poptSetOtherOptionHelp(pc, "service <password>");
5433 while ((opt = poptGetNextOpt(pc)) != -1) {
5435 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
5436 /* I see no other way to keep things sane --SSS */
5437 if (tar_opt == true) {
5438 while (poptPeekArg(pc)) {
5444 /* if the service has not yet been specified lets see if it is available in the popt stack */
5445 if (!service_opt && poptPeekArg(pc)) {
5446 service = talloc_strdup(frame, poptGetArg(pc));
5453 /* if the service has already been retrieved then check if we have also a password */
5455 && (!get_cmdline_auth_info_got_pass(auth_info))
5456 && poptPeekArg(pc)) {
5457 set_cmdline_auth_info_password(auth_info,
5463 /* Messages are sent to NetBIOS name type 0x3
5464 * (Messenger Service). Make sure we default
5465 * to port 139 instead of port 445. srl,crh
5468 desthost = talloc_strdup(frame,poptGetOptArg(pc));
5478 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
5482 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
5486 setup_logging("smbclient", DEBUG_STDERR );
5487 display_set_stderr();
5491 query_host = talloc_strdup(frame, poptGetOptArg(pc));
5497 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
5500 /* We must use old option processing for this. Find the
5501 * position of the -T option in the raw argv[]. */
5504 for (i = 1; i < argc; i++) {
5505 if (strncmp("-T", argv[i],2)==0)
5509 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
5510 poptPrintUsage(pc, stderr, 0);
5514 /* this must be the last option, mark we have parsed it so that we know we have */
5518 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
5519 if (!base_directory) {
5530 return(do_smb_browse());
5535 /* We may still have some leftovers after the last popt option has been called */
5536 if (tar_opt == true) {
5537 while (poptPeekArg(pc)) {
5543 /* if the service has not yet been specified lets see if it is available in the popt stack */
5544 if (!service_opt && poptPeekArg(pc)) {
5545 service = talloc_strdup(frame,poptGetArg(pc));
5552 /* if the service has already been retrieved then check if we have also a password */
5554 && !get_cmdline_auth_info_got_pass(auth_info)
5555 && poptPeekArg(pc)) {
5556 set_cmdline_auth_info_password(auth_info,
5560 if ( override_logfile )
5561 setup_logging( lp_logfile(), DEBUG_FILE );
5563 if (!lp_load_client(get_dyn_CONFIGFILE())) {
5564 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
5565 argv[0], get_dyn_CONFIGFILE());
5568 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
5569 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
5575 if (service_opt && service) {
5578 /* Convert any '/' characters in the service name to '\' characters */
5579 string_replace(service, '/','\\');
5580 if (count_chars(service,'\\') < 3) {
5581 d_printf("\n%s: Not enough '\\' characters in service\n",service);
5582 poptPrintUsage(pc, stderr, 0);
5585 /* Remove trailing slashes */
5586 len = strlen(service);
5587 while(len > 0 && service[len - 1] == '\\') {
5589 service[len] = '\0';
5593 smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
5594 if (!init_names()) {
5595 fprintf(stderr, "init_names() failed\n");
5599 if(new_name_resolve_order)
5600 lp_set_name_resolve_order(new_name_resolve_order);
5602 if (!tar_type && !query_host && !service && !message) {
5603 poptPrintUsage(pc, stderr, 0);
5607 poptFreeContext(pc);
5609 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5611 /* Ensure we have a password (or equivalent). */
5612 set_cmdline_auth_info_getpass(auth_info);
5616 process_command_string(cmdstr);
5617 return do_tar_op(base_directory);
5620 if (query_host && *query_host) {
5621 char *qhost = query_host;
5624 while (*qhost == '\\' || *qhost == '/')
5627 if ((slash = strchr_m(qhost, '/'))
5628 || (slash = strchr_m(qhost, '\\'))) {
5632 if ((p=strchr_m(qhost, '#'))) {
5635 sscanf(p, "%x", &name_type);
5638 return do_host_query(qhost);
5642 return do_message_op(auth_info);
5645 if (process(base_directory)) {