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 "client/clitar_proto.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
31 #include "../lib/util/select.h"
32 #include "system/readline.h"
33 #include "../libcli/smbreadline/smbreadline.h"
34 #include "../libcli/security/security.h"
35 #include "system/select.h"
36 #include "libsmb/libsmb.h"
37 #include "libsmb/clirap.h"
39 #include "libsmb/nmblib.h"
40 #include "include/ntioctl.h"
41 #include "../libcli/smb/smbXcli_base.h"
47 extern int do_smb_browse(void); /* mDNS browsing */
49 extern bool override_logfile;
54 static char *desthost;
55 static bool grepable = false;
56 static char *cmdstr = NULL;
57 const char *cmd_ptr = NULL;
59 static int io_bufsize = 0; /* we use the default size */
60 static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
62 static int name_type = 0x20;
63 static int max_protocol = -1;
65 static int process_tok(char *tok);
66 static int cmd_help(void);
68 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
70 /* value for unused fid field in trans2 secondary request */
71 #define FID_UNUSED (0xFFFF)
73 time_t newer_than = 0;
74 static int archive_level = 0;
76 static bool translation = false;
79 static bool prompt = true;
81 static bool recurse = false;
82 static bool showacls = false;
83 bool lowercase = false;
84 static bool backup_intent = false;
86 static struct sockaddr_storage dest_ss;
87 static char dest_ss_str[INET6_ADDRSTRLEN];
89 #define SEPARATORS " \t\n\r"
91 static bool abort_mget = true;
94 uint64_t get_total_size = 0;
95 unsigned int get_total_time_ms = 0;
96 static uint64_t put_total_size = 0;
97 static unsigned int put_total_time_ms = 0;
100 static double dir_total;
102 /* encrypted state. */
103 static bool smb_encrypt;
105 /* root cli_state connection */
107 struct cli_state *cli;
109 static char CLI_DIRSEP_CHAR = '\\';
110 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
112 /* Authentication for client connections. */
113 struct user_auth_info *auth_info;
115 /* Accessor functions for directory paths. */
116 static char *fileselection;
117 static const char *client_get_fileselection(void)
120 return fileselection;
125 static const char *client_set_fileselection(const char *new_fs)
127 SAFE_FREE(fileselection);
129 fileselection = SMB_STRDUP(new_fs);
131 return client_get_fileselection();
135 static const char *client_get_cwd(void)
140 return CLI_DIRSEP_STR;
143 static const char *client_set_cwd(const char *new_cwd)
147 cwd = SMB_STRDUP(new_cwd);
149 return client_get_cwd();
152 static char *cur_dir;
153 const char *client_get_cur_dir(void)
158 return CLI_DIRSEP_STR;
161 const char *client_set_cur_dir(const char *newdir)
165 cur_dir = SMB_STRDUP(newdir);
167 return client_get_cur_dir();
170 /****************************************************************************
171 Put up a yes/no prompt.
172 ****************************************************************************/
174 static bool yesno(const char *p)
179 if (!fgets(ans,sizeof(ans)-1,stdin))
182 if (*ans == 'y' || *ans == 'Y')
188 /****************************************************************************
189 Write to a local file with CR/LF->LF translation if appropriate. Return the
190 number taken from the buffer. This may not equal the number written.
191 ****************************************************************************/
193 static int writefile(int f, char *b, int n)
203 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
206 if (write(f, b, 1) != 1) {
216 /****************************************************************************
217 Read from a file with LF->CR/LF translation if appropriate. Return the
218 number read. read approx n bytes.
219 ****************************************************************************/
221 static int readfile(uint8_t *b, int n, XFILE *f)
227 return x_fread(b,1,n,f);
230 while (i < (n - 1) && (i < BUFFER_SIZE)) {
231 if ((c = x_getc(f)) == EOF) {
235 if (c == '\n') { /* change all LFs to CR/LF */
250 static size_t push_source(uint8_t *buf, size_t n, void *priv)
252 struct push_state *state = (struct push_state *)priv;
255 if (x_feof(state->f)) {
259 result = readfile(buf, n, state->f);
260 state->nread += result;
264 /****************************************************************************
266 ****************************************************************************/
268 static void send_message(const char *username)
274 d_printf("Type your message, ending it with a Control-D\n");
277 while (i<sizeof(buf)-2) {
278 int c = fgetc(stdin);
289 status = cli_message(cli, desthost, username, buf);
290 if (!NT_STATUS_IS_OK(status)) {
291 d_fprintf(stderr, "cli_message returned %s\n",
296 /****************************************************************************
297 Check the space on a device.
298 ****************************************************************************/
300 static int do_dskattr(void)
302 int total, bsize, avail;
303 struct cli_state *targetcli = NULL;
304 char *targetpath = NULL;
305 TALLOC_CTX *ctx = talloc_tos();
308 status = cli_resolve_path(ctx, "", auth_info, cli,
309 client_get_cur_dir(), &targetcli,
311 if (!NT_STATUS_IS_OK(status)) {
312 d_printf("Error in dskattr: %s\n", nt_errstr(status));
316 status = cli_dskattr(targetcli, &bsize, &total, &avail);
317 if (!NT_STATUS_IS_OK(status)) {
318 d_printf("Error in dskattr: %s\n", nt_errstr(status));
322 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
323 total, bsize, avail);
328 /****************************************************************************
330 ****************************************************************************/
332 static int cmd_pwd(void)
334 d_printf("Current directory is %s",service);
335 d_printf("%s\n",client_get_cur_dir());
339 /****************************************************************************
340 Ensure name has correct directory separators.
341 ****************************************************************************/
343 static void normalize_name(char *newdir)
345 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
346 string_replace(newdir,'/','\\');
350 /****************************************************************************
351 Change directory - inner section.
352 ****************************************************************************/
354 static int do_cd(const char *new_dir)
357 char *saved_dir = NULL;
359 char *targetpath = NULL;
360 struct cli_state *targetcli = NULL;
361 SMB_STRUCT_STAT sbuf;
364 TALLOC_CTX *ctx = talloc_stackframe();
367 newdir = talloc_strdup(ctx, new_dir);
373 normalize_name(newdir);
375 /* Save the current directory in case the new directory is invalid */
377 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
383 if (*newdir == CLI_DIRSEP_CHAR) {
384 client_set_cur_dir(newdir);
387 new_cd = talloc_asprintf(ctx, "%s%s",
388 client_get_cur_dir(),
395 /* Ensure cur_dir ends in a DIRSEP */
396 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
397 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
402 client_set_cur_dir(new_cd);
404 new_cd = clean_name(ctx, new_cd);
405 client_set_cur_dir(new_cd);
407 status = cli_resolve_path(ctx, "", auth_info, cli, new_cd,
408 &targetcli, &targetpath);
409 if (!NT_STATUS_IS_OK(status)) {
410 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
411 client_set_cur_dir(saved_dir);
415 if (strequal(targetpath,CLI_DIRSEP_STR )) {
420 /* Use a trans2_qpathinfo to test directories for modern servers.
421 Except Win9x doesn't support the qpathinfo_basic() call..... */
423 if (smbXcli_conn_protocol(targetcli->conn) > PROTOCOL_LANMAN2 && !targetcli->win95) {
425 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
427 if (!NT_STATUS_IS_OK(status)) {
428 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
429 client_set_cur_dir(saved_dir);
433 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
434 d_printf("cd %s: not a directory\n", new_cd);
435 client_set_cur_dir(saved_dir);
440 targetpath = talloc_asprintf(ctx,
445 client_set_cur_dir(saved_dir);
448 targetpath = clean_name(ctx, targetpath);
450 client_set_cur_dir(saved_dir);
454 status = cli_chkpath(targetcli, targetpath);
455 if (!NT_STATUS_IS_OK(status)) {
456 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
457 client_set_cur_dir(saved_dir);
470 /****************************************************************************
472 ****************************************************************************/
474 static int cmd_cd(void)
479 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
482 d_printf("Current directory is %s\n",client_get_cur_dir());
488 /****************************************************************************
490 ****************************************************************************/
492 static int cmd_cd_oneup(void)
497 /*******************************************************************
498 Decide if a file should be operated on.
499 ********************************************************************/
501 static bool do_this_one(struct file_info *finfo)
507 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
511 if (*client_get_fileselection() &&
512 !mask_match(finfo->name,client_get_fileselection(),false)) {
513 DEBUG(3,("mask_match %s failed\n", finfo->name));
517 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
518 DEBUG(3,("newer_than %s failed\n", finfo->name));
522 if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
523 DEBUG(3,("archive %s failed\n", finfo->name));
530 /****************************************************************************
531 Display info about a file.
532 ****************************************************************************/
534 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
538 TALLOC_CTX *ctx = talloc_tos();
539 NTSTATUS status = NT_STATUS_OK;
541 if (!do_this_one(finfo)) {
545 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
547 d_printf(" %-30s%7.7s %8.0f %s",
549 attrib_string(talloc_tos(), finfo->mode),
552 dir_total += finfo->size;
557 /* skip if this is . or .. */
558 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
560 /* create absolute filename for cli_ntcreate() FIXME */
561 afname = talloc_asprintf(ctx,
567 return NT_STATUS_NO_MEMORY;
569 /* print file meta date header */
570 d_printf( "FILENAME:%s\n", finfo->name);
571 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->mode));
572 d_printf( "SIZE:%.0f\n", (double)finfo->size);
573 d_printf( "MTIME:%s", time_to_asc(t));
574 status = cli_ntcreate(cli_state, afname, 0,
575 CREATE_ACCESS_READ, 0,
576 FILE_SHARE_READ|FILE_SHARE_WRITE,
577 FILE_OPEN, 0x0, 0x0, &fnum);
578 if (!NT_STATUS_IS_OK(status)) {
579 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
580 afname, nt_errstr(status)));
582 struct security_descriptor *sd = NULL;
583 status = cli_query_secdesc(cli_state, fnum,
585 if (!NT_STATUS_IS_OK(status)) {
586 DEBUG( 0, ("display_finfo() failed to "
587 "get security descriptor: %s",
590 display_sec_desc(sd);
599 /****************************************************************************
600 Accumulate size of a file.
601 ****************************************************************************/
603 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
606 if (do_this_one(finfo)) {
607 dir_total += finfo->size;
612 static bool do_list_recurse;
613 static bool do_list_dirs;
614 static char *do_list_queue = 0;
615 static long do_list_queue_size = 0;
616 static long do_list_queue_start = 0;
617 static long do_list_queue_end = 0;
618 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
621 /****************************************************************************
622 Functions for do_list_queue.
623 ****************************************************************************/
626 * The do_list_queue is a NUL-separated list of strings stored in a
627 * char*. Since this is a FIFO, we keep track of the beginning and
628 * ending locations of the data in the queue. When we overflow, we
629 * double the size of the char*. When the start of the data passes
630 * the midpoint, we move everything back. This is logically more
631 * complex than a linked list, but easier from a memory management
632 * angle. In any memory error condition, do_list_queue is reset.
633 * Functions check to ensure that do_list_queue is non-NULL before
637 static void reset_do_list_queue(void)
639 SAFE_FREE(do_list_queue);
640 do_list_queue_size = 0;
641 do_list_queue_start = 0;
642 do_list_queue_end = 0;
645 static void init_do_list_queue(void)
647 reset_do_list_queue();
648 do_list_queue_size = 1024;
649 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
650 if (do_list_queue == 0) {
651 d_printf("malloc fail for size %d\n",
652 (int)do_list_queue_size);
653 reset_do_list_queue();
655 memset(do_list_queue, 0, do_list_queue_size);
659 static void adjust_do_list_queue(void)
662 * If the starting point of the queue is more than half way through,
663 * move everything toward the beginning.
666 if (do_list_queue == NULL) {
667 DEBUG(4,("do_list_queue is empty\n"));
668 do_list_queue_start = do_list_queue_end = 0;
672 if (do_list_queue_start == do_list_queue_end) {
673 DEBUG(4,("do_list_queue is empty\n"));
674 do_list_queue_start = do_list_queue_end = 0;
675 *do_list_queue = '\0';
676 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
677 DEBUG(4,("sliding do_list_queue backward\n"));
678 memmove(do_list_queue,
679 do_list_queue + do_list_queue_start,
680 do_list_queue_end - do_list_queue_start);
681 do_list_queue_end -= do_list_queue_start;
682 do_list_queue_start = 0;
686 static void add_to_do_list_queue(const char *entry)
688 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
689 while (new_end > do_list_queue_size) {
690 do_list_queue_size *= 2;
691 DEBUG(4,("enlarging do_list_queue to %d\n",
692 (int)do_list_queue_size));
693 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
694 if (! do_list_queue) {
695 d_printf("failure enlarging do_list_queue to %d bytes\n",
696 (int)do_list_queue_size);
697 reset_do_list_queue();
699 memset(do_list_queue + do_list_queue_size / 2,
700 0, do_list_queue_size / 2);
704 strlcpy_base(do_list_queue + do_list_queue_end,
705 entry, do_list_queue, do_list_queue_size);
706 do_list_queue_end = new_end;
707 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
708 entry, (int)do_list_queue_start, (int)do_list_queue_end));
712 static char *do_list_queue_head(void)
714 return do_list_queue + do_list_queue_start;
717 static void remove_do_list_queue_head(void)
719 if (do_list_queue_end > do_list_queue_start) {
720 do_list_queue_start += strlen(do_list_queue_head()) + 1;
721 adjust_do_list_queue();
722 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
723 (int)do_list_queue_start, (int)do_list_queue_end));
727 static int do_list_queue_empty(void)
729 return (! (do_list_queue && *do_list_queue));
732 /****************************************************************************
733 A helper for do_list.
734 ****************************************************************************/
736 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
737 const char *mask, void *state)
739 struct cli_state *cli_state = (struct cli_state *)state;
740 TALLOC_CTX *ctx = talloc_tos();
742 char *dir_end = NULL;
743 NTSTATUS status = NT_STATUS_OK;
745 /* Work out the directory. */
746 dir = talloc_strdup(ctx, mask);
748 return NT_STATUS_NO_MEMORY;
750 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
754 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
755 if (do_list_dirs && do_this_one(f)) {
756 status = do_list_fn(cli_state, f, dir);
757 if (!NT_STATUS_IS_OK(status)) {
761 if (do_list_recurse &&
763 !strequal(f->name,".") &&
764 !strequal(f->name,"..")) {
769 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
771 return NT_STATUS_UNSUCCESSFUL;
774 mask2 = talloc_asprintf(ctx,
780 return NT_STATUS_NO_MEMORY;
782 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
788 mask2 = talloc_asprintf_append(mask2,
794 return NT_STATUS_NO_MEMORY;
796 add_to_do_list_queue(mask2);
803 if (do_this_one(f)) {
804 status = do_list_fn(cli_state, f, dir);
810 /****************************************************************************
811 A wrapper around cli_list that adds recursion.
812 ****************************************************************************/
814 NTSTATUS do_list(const char *mask,
816 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
821 static int in_do_list = 0;
822 TALLOC_CTX *ctx = talloc_tos();
823 struct cli_state *targetcli = NULL;
824 char *targetpath = NULL;
825 NTSTATUS ret_status = NT_STATUS_OK;
826 NTSTATUS status = NT_STATUS_OK;
828 if (in_do_list && rec) {
829 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
835 do_list_recurse = rec;
840 init_do_list_queue();
841 add_to_do_list_queue(mask);
843 while (!do_list_queue_empty()) {
845 * Need to copy head so that it doesn't become
846 * invalid inside the call to cli_list. This
847 * would happen if the list were expanded
849 * Fix from E. Jay Berkenbilt (ejb@ql.org)
851 char *head = talloc_strdup(ctx, do_list_queue_head());
854 return NT_STATUS_NO_MEMORY;
859 status = cli_resolve_path(ctx, "", auth_info, cli,
862 if (!NT_STATUS_IS_OK(status)) {
863 d_printf("do_list: [%s] %s\n", head,
865 remove_do_list_queue_head();
869 status = cli_list(targetcli, targetpath, attribute,
870 do_list_helper, targetcli);
871 if (!NT_STATUS_IS_OK(status)) {
872 d_printf("%s listing %s\n",
873 nt_errstr(status), targetpath);
876 remove_do_list_queue_head();
877 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
878 char *next_file = do_list_queue_head();
880 if ((strlen(next_file) >= 2) &&
881 (next_file[strlen(next_file) - 1] == '*') &&
882 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
883 save_ch = next_file +
884 strlen(next_file) - 2;
887 /* cwd is only used if showacls is on */
888 client_set_cwd(next_file);
891 if (!showacls) /* don't disturbe the showacls output */
892 d_printf("\n%s\n",next_file);
894 *save_ch = CLI_DIRSEP_CHAR;
898 TALLOC_FREE(targetpath);
902 status = cli_resolve_path(ctx, "", auth_info, cli, mask,
903 &targetcli, &targetpath);
904 if (NT_STATUS_IS_OK(status)) {
905 status = cli_list(targetcli, targetpath, attribute,
906 do_list_helper, targetcli);
907 if (!NT_STATUS_IS_OK(status)) {
908 d_printf("%s listing %s\n",
909 nt_errstr(status), targetpath);
912 TALLOC_FREE(targetpath);
914 d_printf("do_list: [%s] %s\n", mask, nt_errstr(status));
920 reset_do_list_queue();
924 /****************************************************************************
925 Get a directory listing.
926 ****************************************************************************/
928 static int cmd_dir(void)
930 TALLOC_CTX *ctx = talloc_tos();
931 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
938 mask = talloc_strdup(ctx, client_get_cur_dir());
943 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
945 if (*buf == CLI_DIRSEP_CHAR) {
946 mask = talloc_strdup(ctx, buf);
948 mask = talloc_asprintf_append(mask, "%s", buf);
951 mask = talloc_asprintf_append(mask, "*");
958 /* cwd is only used if showacls is on */
959 client_set_cwd(client_get_cur_dir());
962 status = do_list(mask, attribute, display_finfo, recurse, true);
963 if (!NT_STATUS_IS_OK(status)) {
969 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
974 /****************************************************************************
975 Get a directory listing.
976 ****************************************************************************/
978 static int cmd_du(void)
980 TALLOC_CTX *ctx = talloc_tos();
981 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
988 mask = talloc_strdup(ctx, client_get_cur_dir());
992 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
993 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
999 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1000 normalize_name(buf);
1001 if (*buf == CLI_DIRSEP_CHAR) {
1002 mask = talloc_strdup(ctx, buf);
1004 mask = talloc_asprintf_append(mask, "%s", buf);
1007 mask = talloc_strdup(ctx, "*");
1010 status = do_list(mask, attribute, do_du, recurse, true);
1011 if (!NT_STATUS_IS_OK(status)) {
1017 d_printf("Total number of bytes: %.0f\n", dir_total);
1022 static int cmd_echo(void)
1024 TALLOC_CTX *ctx = talloc_tos();
1029 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1030 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1031 d_printf("echo <num> <data>\n");
1035 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1037 if (!NT_STATUS_IS_OK(status)) {
1038 d_printf("echo failed: %s\n", nt_errstr(status));
1045 /****************************************************************************
1046 Get a file from rname to lname
1047 ****************************************************************************/
1049 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1051 int *pfd = (int *)priv;
1052 if (writefile(*pfd, buf, n) == -1) {
1053 return map_nt_error_from_unix(errno);
1055 return NT_STATUS_OK;
1058 static int do_get(const char *rname, const char *lname_in, bool reget)
1060 TALLOC_CTX *ctx = talloc_tos();
1063 bool newhandle = false;
1064 struct timespec tp_start;
1070 struct cli_state *targetcli = NULL;
1071 char *targetname = NULL;
1075 lname = talloc_strdup(ctx, lname_in);
1081 if (!strlower_m(lname)) {
1082 d_printf("strlower_m %s failed\n", lname);
1087 status = cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli,
1089 if (!NT_STATUS_IS_OK(status)) {
1090 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1094 clock_gettime_mono(&tp_start);
1096 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 d_printf("%s opening remote file %s\n", nt_errstr(status),
1103 if(!strcmp(lname,"-")) {
1104 handle = fileno(stdout);
1107 handle = open(lname, O_WRONLY|O_CREAT, 0644);
1109 start = lseek(handle, 0, SEEK_END);
1111 d_printf("Error seeking local file\n");
1116 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1121 d_printf("Error opening local file %s\n",lname);
1126 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1128 if (!NT_STATUS_IS_OK(status)) {
1129 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1131 if(!NT_STATUS_IS_OK(status)) {
1132 d_printf("getattrib: %s\n", nt_errstr(status));
1137 DEBUG(1,("getting file %s of size %.0f as %s ",
1138 rname, (double)size, lname));
1140 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1141 writefile_sink, (void *)&handle, &nread);
1142 if (!NT_STATUS_IS_OK(status)) {
1143 d_fprintf(stderr, "parallel_read returned %s\n",
1145 cli_close(targetcli, fnum);
1149 status = cli_close(targetcli, fnum);
1150 if (!NT_STATUS_IS_OK(status)) {
1151 d_printf("Error %s closing remote file\n", nt_errstr(status));
1159 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1160 cli_setatr(cli, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
1164 struct timespec tp_end;
1167 clock_gettime_mono(&tp_end);
1168 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1169 get_total_time_ms += this_time;
1170 get_total_size += nread;
1172 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1173 nread / (1.024*this_time + 1.0e-4),
1174 get_total_size / (1.024*get_total_time_ms)));
1177 TALLOC_FREE(targetname);
1181 /****************************************************************************
1183 ****************************************************************************/
1185 static int cmd_get(void)
1187 TALLOC_CTX *ctx = talloc_tos();
1192 rname = talloc_strdup(ctx, client_get_cur_dir());
1197 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1198 d_printf("get <filename> [localname]\n");
1201 rname = talloc_asprintf_append(rname, "%s", fname);
1205 rname = clean_name(ctx, rname);
1210 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1215 return do_get(rname, lname, false);
1218 /****************************************************************************
1219 Do an mget operation on one file.
1220 ****************************************************************************/
1222 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1225 TALLOC_CTX *ctx = talloc_tos();
1226 NTSTATUS status = NT_STATUS_OK;
1229 char *saved_curdir = NULL;
1230 char *mget_mask = NULL;
1231 char *new_cd = NULL;
1234 return NT_STATUS_OK;
1237 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1238 return NT_STATUS_OK;
1241 d_printf("mget aborted\n");
1242 return NT_STATUS_UNSUCCESSFUL;
1245 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
1246 if (asprintf(&quest,
1247 "Get directory %s? ",finfo->name) < 0) {
1248 return NT_STATUS_NO_MEMORY;
1251 if (asprintf(&quest,
1252 "Get file %s? ",finfo->name) < 0) {
1253 return NT_STATUS_NO_MEMORY;
1257 if (prompt && !yesno(quest)) {
1259 return NT_STATUS_OK;
1263 if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
1264 rname = talloc_asprintf(ctx,
1266 client_get_cur_dir(),
1269 return NT_STATUS_NO_MEMORY;
1271 do_get(rname, finfo->name, false);
1273 return NT_STATUS_OK;
1276 /* handle directories */
1277 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1278 if (!saved_curdir) {
1279 return NT_STATUS_NO_MEMORY;
1282 new_cd = talloc_asprintf(ctx,
1284 client_get_cur_dir(),
1288 return NT_STATUS_NO_MEMORY;
1290 client_set_cur_dir(new_cd);
1292 string_replace(finfo->name,'\\','/');
1294 if (!strlower_m(finfo->name)) {
1295 return NT_STATUS_INVALID_PARAMETER;
1299 if (!directory_exist(finfo->name) &&
1300 mkdir(finfo->name,0777) != 0) {
1301 d_printf("failed to create directory %s\n",finfo->name);
1302 client_set_cur_dir(saved_curdir);
1303 return map_nt_error_from_unix(errno);
1306 if (chdir(finfo->name) != 0) {
1307 d_printf("failed to chdir to directory %s\n",finfo->name);
1308 client_set_cur_dir(saved_curdir);
1309 return map_nt_error_from_unix(errno);
1312 mget_mask = talloc_asprintf(ctx,
1314 client_get_cur_dir());
1317 return NT_STATUS_NO_MEMORY;
1320 status = do_list(mget_mask,
1321 (FILE_ATTRIBUTE_SYSTEM
1322 | FILE_ATTRIBUTE_HIDDEN
1323 | FILE_ATTRIBUTE_DIRECTORY),
1324 do_mget, false, true);
1325 if (!NT_STATUS_IS_OK(status)
1326 && !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1328 * Ignore access denied errors to ensure all permitted files are
1334 if (chdir("..") == -1) {
1335 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1337 return map_nt_error_from_unix(errno);
1339 client_set_cur_dir(saved_curdir);
1340 TALLOC_FREE(mget_mask);
1341 TALLOC_FREE(saved_curdir);
1342 TALLOC_FREE(new_cd);
1343 return NT_STATUS_OK;
1346 /****************************************************************************
1347 View the file using the pager.
1348 ****************************************************************************/
1350 static int cmd_more(void)
1352 TALLOC_CTX *ctx = talloc_tos();
1356 char *pager_cmd = NULL;
1362 rname = talloc_strdup(ctx, client_get_cur_dir());
1367 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1371 mask = umask(S_IRWXO | S_IRWXG);
1372 fd = mkstemp(lname);
1375 d_printf("failed to create temporary file for more\n");
1380 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1381 d_printf("more <filename>\n");
1385 rname = talloc_asprintf_append(rname, "%s", fname);
1389 rname = clean_name(ctx,rname);
1394 rc = do_get(rname, lname, false);
1396 pager=getenv("PAGER");
1398 pager_cmd = talloc_asprintf(ctx,
1400 (pager? pager:PAGER),
1405 if (system(pager_cmd) == -1) {
1406 d_printf("system command '%s' returned -1\n",
1414 /****************************************************************************
1416 ****************************************************************************/
1418 static int cmd_mget(void)
1420 TALLOC_CTX *ctx = talloc_tos();
1421 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1422 char *mget_mask = NULL;
1424 NTSTATUS status = NT_STATUS_OK;
1427 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1432 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1434 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1438 if (*buf == CLI_DIRSEP_CHAR) {
1439 mget_mask = talloc_strdup(ctx, buf);
1441 mget_mask = talloc_asprintf_append(mget_mask,
1447 status = do_list(mget_mask, attribute, do_mget, false, true);
1448 if (!NT_STATUS_IS_OK(status)) {
1453 if (mget_mask == NULL) {
1454 d_printf("nothing to mget\n");
1459 mget_mask = talloc_asprintf(ctx,
1461 client_get_cur_dir());
1465 status = do_list(mget_mask, attribute, do_mget, false, true);
1466 if (!NT_STATUS_IS_OK(status)) {
1474 /****************************************************************************
1475 Make a directory of name "name".
1476 ****************************************************************************/
1478 static bool do_mkdir(const char *name)
1480 TALLOC_CTX *ctx = talloc_tos();
1481 struct cli_state *targetcli;
1482 char *targetname = NULL;
1485 status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
1487 if (!NT_STATUS_IS_OK(status)) {
1488 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1492 status = cli_mkdir(targetcli, targetname);
1493 if (!NT_STATUS_IS_OK(status)) {
1494 d_printf("%s making remote directory %s\n",
1495 nt_errstr(status),name);
1502 /****************************************************************************
1503 Show 8.3 name of a file.
1504 ****************************************************************************/
1506 static bool do_altname(const char *name)
1511 status = cli_qpathinfo_alt_name(cli, name, altname);
1512 if (!NT_STATUS_IS_OK(status)) {
1513 d_printf("%s getting alt name for %s\n",
1514 nt_errstr(status),name);
1517 d_printf("%s\n", altname);
1522 /****************************************************************************
1524 ****************************************************************************/
1526 static int cmd_quit(void)
1534 /****************************************************************************
1536 ****************************************************************************/
1538 static int cmd_mkdir(void)
1540 TALLOC_CTX *ctx = talloc_tos();
1545 mask = talloc_strdup(ctx, client_get_cur_dir());
1550 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1552 d_printf("mkdir <dirname>\n");
1556 mask = talloc_asprintf_append(mask, "%s", buf);
1564 struct cli_state *targetcli;
1565 char *targetname = NULL;
1569 ddir2 = talloc_strdup(ctx, "");
1574 status = cli_resolve_path(ctx, "", auth_info, cli, mask,
1575 &targetcli, &targetname);
1576 if (!NT_STATUS_IS_OK(status)) {
1580 ddir = talloc_strdup(ctx, targetname);
1584 trim_char(ddir,'.','\0');
1585 p = strtok_r(ddir, "/\\", &saveptr);
1587 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1591 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1594 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1598 p = strtok_r(NULL, "/\\", &saveptr);
1607 /****************************************************************************
1609 ****************************************************************************/
1611 static int cmd_altname(void)
1613 TALLOC_CTX *ctx = talloc_tos();
1617 name = talloc_strdup(ctx, client_get_cur_dir());
1622 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1623 d_printf("altname <file>\n");
1626 name = talloc_asprintf_append(name, "%s", buf);
1634 static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1636 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1639 if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1640 if (mode & FILE_ATTRIBUTE_ENCRYPTED) {
1643 if (mode & FILE_ATTRIBUTE_NONINDEXED) {
1646 if (mode & FILE_ATTRIBUTE_OFFLINE) {
1649 if (mode & FILE_ATTRIBUTE_COMPRESSED) {
1652 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1655 if (mode & FILE_ATTRIBUTE_SPARSE) {
1658 if (mode & FILE_ATTRIBUTE_TEMPORARY) {
1661 if (mode & FILE_ATTRIBUTE_NORMAL) {
1664 if (mode & FILE_ATTRIBUTE_READONLY) {
1667 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1670 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1673 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1676 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1683 /****************************************************************************
1684 Show all info we can get
1685 ****************************************************************************/
1687 static int do_allinfo(const char *name)
1690 struct timespec b_time, a_time, m_time, c_time;
1695 unsigned int num_streams;
1696 struct stream_struct *streams;
1702 status = cli_qpathinfo_alt_name(cli, name, altname);
1703 if (!NT_STATUS_IS_OK(status)) {
1704 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1707 * Ignore not supported or not implemented, it does not
1708 * hurt if we can't list alternate names.
1710 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1711 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1717 d_printf("altname: %s\n", altname);
1719 status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1720 &size, &mode, NULL);
1721 if (!NT_STATUS_IS_OK(status)) {
1722 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1727 unix_timespec_to_nt_time(&tmp, b_time);
1728 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1730 unix_timespec_to_nt_time(&tmp, a_time);
1731 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1733 unix_timespec_to_nt_time(&tmp, m_time);
1734 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1736 unix_timespec_to_nt_time(&tmp, c_time);
1737 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1739 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
1741 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1743 if (!NT_STATUS_IS_OK(status)) {
1744 d_printf("%s getting streams for %s\n", nt_errstr(status),
1749 for (i=0; i<num_streams; i++) {
1750 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1751 (unsigned long long)streams[i].size);
1754 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1755 char *subst, *print;
1758 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1760 if (!NT_STATUS_IS_OK(status)) {
1761 d_fprintf(stderr, "cli_readlink returned %s\n",
1764 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1765 subst, print, flags);
1771 status = cli_ntcreate(cli, name, 0,
1772 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1773 SEC_STD_SYNCHRONIZE, 0,
1774 FILE_SHARE_READ|FILE_SHARE_WRITE
1776 FILE_OPEN, 0x0, 0x0, &fnum);
1777 if (!NT_STATUS_IS_OK(status)) {
1779 * Ignore failure, it does not hurt if we can't list
1784 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1785 true, &snapshots, &num_snapshots);
1786 if (!NT_STATUS_IS_OK(status)) {
1787 cli_close(cli, fnum);
1791 for (i=0; i<num_snapshots; i++) {
1794 d_printf("%s\n", snapshots[i]);
1795 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1796 snapshots[i], name);
1797 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1798 &m_time, &c_time, &size,
1800 if (!NT_STATUS_IS_OK(status)) {
1801 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1802 snap_name, nt_errstr(status));
1803 TALLOC_FREE(snap_name);
1806 unix_timespec_to_nt_time(&tmp, b_time);
1807 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1808 unix_timespec_to_nt_time(&tmp, a_time);
1809 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1810 unix_timespec_to_nt_time(&tmp, m_time);
1811 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1812 unix_timespec_to_nt_time(&tmp, c_time);
1813 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1814 d_printf("size: %d\n", (int)size);
1817 TALLOC_FREE(snapshots);
1822 /****************************************************************************
1823 Show all info we can get
1824 ****************************************************************************/
1826 static int cmd_allinfo(void)
1828 TALLOC_CTX *ctx = talloc_tos();
1832 name = talloc_strdup(ctx, client_get_cur_dir());
1837 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1838 d_printf("allinfo <file>\n");
1841 name = talloc_asprintf_append(name, "%s", buf);
1851 /****************************************************************************
1853 ****************************************************************************/
1855 static int do_put(const char *rname, const char *lname, bool reput)
1857 TALLOC_CTX *ctx = talloc_tos();
1862 struct timespec tp_start;
1863 struct cli_state *targetcli;
1864 char *targetname = NULL;
1865 struct push_state state;
1868 status = cli_resolve_path(ctx, "", auth_info, cli, rname,
1869 &targetcli, &targetname);
1870 if (!NT_STATUS_IS_OK(status)) {
1871 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1875 clock_gettime_mono(&tp_start);
1878 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1879 if (NT_STATUS_IS_OK(status)) {
1880 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1881 targetcli, fnum, NULL,
1883 NULL, NULL, NULL)) &&
1884 !NT_STATUS_IS_OK(status = cli_getattrE(
1885 targetcli, fnum, NULL,
1888 d_printf("getattrib: %s\n", nt_errstr(status));
1893 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1896 if (!NT_STATUS_IS_OK(status)) {
1897 d_printf("%s opening remote file %s\n", nt_errstr(status),
1902 /* allow files to be piped into smbclient
1905 Note that in this case this function will exit(0) rather
1907 if (!strcmp(lname, "-")) {
1909 /* size of file is not known */
1911 f = x_fopen(lname,O_RDONLY, 0);
1913 if (x_tseek(f, start, SEEK_SET) == -1) {
1914 d_printf("Error seeking local file\n");
1922 d_printf("Error opening local file %s\n",lname);
1926 DEBUG(1,("putting file %s as %s ",lname,
1929 x_setvbuf(f, NULL, X_IOFBF, io_bufsize);
1934 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1936 if (!NT_STATUS_IS_OK(status)) {
1937 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1941 status = cli_close(targetcli, fnum);
1942 if (!NT_STATUS_IS_OK(status)) {
1943 d_printf("%s closing remote file %s\n", nt_errstr(status),
1956 struct timespec tp_end;
1959 clock_gettime_mono(&tp_end);
1960 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1961 put_total_time_ms += this_time;
1962 put_total_size += state.nread;
1964 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1965 state.nread / (1.024*this_time + 1.0e-4),
1966 put_total_size / (1.024*put_total_time_ms)));
1977 /****************************************************************************
1979 ****************************************************************************/
1981 static int cmd_put(void)
1983 TALLOC_CTX *ctx = talloc_tos();
1988 rname = talloc_strdup(ctx, client_get_cur_dir());
1993 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1994 d_printf("put <filename>\n");
1998 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1999 rname = talloc_asprintf_append(rname, "%s", buf);
2001 rname = talloc_asprintf_append(rname, "%s", lname);
2007 rname = clean_name(ctx, rname);
2014 /* allow '-' to represent stdin
2015 jdblair, 24.jun.98 */
2016 if (!file_exist_stat(lname, &st, false) &&
2017 (strcmp(lname,"-"))) {
2018 d_printf("%s does not exist\n",lname);
2023 return do_put(rname, lname, false);
2026 /*************************************
2027 File list structure.
2028 *************************************/
2030 static struct file_list {
2031 struct file_list *prev, *next;
2036 /****************************************************************************
2037 Free a file_list structure.
2038 ****************************************************************************/
2040 static void free_file_list (struct file_list *l_head)
2042 struct file_list *list, *next;
2044 for (list = l_head; list; list = next) {
2046 DLIST_REMOVE(l_head, list);
2047 SAFE_FREE(list->file_path);
2052 /****************************************************************************
2053 Seek in a directory/file list until you get something that doesn't start with
2055 ****************************************************************************/
2057 static bool seek_list(struct file_list *list, char *name)
2060 trim_string(list->file_path,"./","\n");
2061 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2070 /****************************************************************************
2071 Set the file selection mask.
2072 ****************************************************************************/
2074 static int cmd_select(void)
2076 TALLOC_CTX *ctx = talloc_tos();
2077 char *new_fs = NULL;
2078 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2081 client_set_fileselection(new_fs);
2083 client_set_fileselection("");
2088 /****************************************************************************
2089 Recursive file matching function act as find
2090 match must be always set to true when calling this function
2091 ****************************************************************************/
2093 static int file_find(struct file_list **list, const char *directory,
2094 const char *expression, bool match)
2097 struct file_list *entry;
2098 struct stat statbuf;
2104 dir = opendir(directory);
2108 while ((dname = readdirname(dir))) {
2109 if (!strcmp("..", dname))
2111 if (!strcmp(".", dname))
2114 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
2119 if (!match || !gen_fnmatch(expression, dname)) {
2121 ret = stat(path, &statbuf);
2123 if (S_ISDIR(statbuf.st_mode)) {
2125 ret = file_find(list, path, expression, false);
2128 d_printf("file_find: cannot stat file %s\n", path);
2137 entry = SMB_MALLOC_P(struct file_list);
2139 d_printf("Out of memory in file_find\n");
2143 entry->file_path = path;
2144 entry->isdir = isdir;
2145 DLIST_ADD(*list, entry);
2155 /****************************************************************************
2157 ****************************************************************************/
2159 static int cmd_mput(void)
2161 TALLOC_CTX *ctx = talloc_tos();
2164 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2166 struct file_list *temp_list;
2167 char *quest, *lname, *rname;
2171 ret = file_find(&file_list, ".", p, true);
2173 free_file_list(file_list);
2181 for (temp_list = file_list; temp_list;
2182 temp_list = temp_list->next) {
2185 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2188 trim_string(lname, "./", "/");
2190 /* check if it's a directory */
2191 if (temp_list->isdir) {
2192 /* if (!recurse) continue; */
2195 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2198 if (prompt && !yesno(quest)) { /* No */
2199 /* Skip the directory */
2200 lname[strlen(lname)-1] = '/';
2201 if (!seek_list(temp_list, lname))
2205 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2208 normalize_name(rname);
2209 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2211 DEBUG (0, ("Unable to make dir, skipping..."));
2212 /* Skip the directory */
2213 lname[strlen(lname)-1] = '/';
2214 if (!seek_list(temp_list, lname)) {
2222 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2225 if (prompt && !yesno(quest)) {
2232 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2237 normalize_name(rname);
2239 do_put(rname, lname, false);
2241 free_file_list(file_list);
2250 /****************************************************************************
2252 ****************************************************************************/
2254 static int do_cancel(int job)
2256 if (cli_printjob_del(cli, job)) {
2257 d_printf("Job %d cancelled\n",job);
2260 NTSTATUS status = cli_nt_error(cli);
2261 d_printf("Error cancelling job %d : %s\n",
2262 job, nt_errstr(status));
2267 /****************************************************************************
2269 ****************************************************************************/
2271 static int cmd_cancel(void)
2273 TALLOC_CTX *ctx = talloc_tos();
2277 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2278 d_printf("cancel <jobid> ...\n");
2284 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2289 /****************************************************************************
2291 ****************************************************************************/
2293 static int cmd_print(void)
2295 TALLOC_CTX *ctx = talloc_tos();
2300 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2301 d_printf("print <filename>\n");
2305 rname = talloc_strdup(ctx, lname);
2309 p = strrchr_m(rname,'/');
2311 rname = talloc_asprintf(ctx,
2316 if (strequal(lname,"-")) {
2317 rname = talloc_asprintf(ctx,
2325 return do_put(rname, lname, false);
2328 /****************************************************************************
2329 Show a print queue entry.
2330 ****************************************************************************/
2332 static void queue_fn(struct print_job_info *p)
2334 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2337 /****************************************************************************
2339 ****************************************************************************/
2341 static int cmd_queue(void)
2343 cli_print_queue(cli, queue_fn);
2347 /****************************************************************************
2349 ****************************************************************************/
2351 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2354 TALLOC_CTX *ctx = talloc_tos();
2358 mask = talloc_asprintf(ctx,
2364 return NT_STATUS_NO_MEMORY;
2367 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2369 return NT_STATUS_OK;
2372 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2373 if (!NT_STATUS_IS_OK(status)) {
2374 d_printf("%s deleting remote file %s\n",
2375 nt_errstr(status), mask);
2381 /****************************************************************************
2383 ****************************************************************************/
2385 static int cmd_del(void)
2387 TALLOC_CTX *ctx = talloc_tos();
2390 NTSTATUS status = NT_STATUS_OK;
2391 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2394 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2397 mask = talloc_strdup(ctx, client_get_cur_dir());
2401 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2402 d_printf("del <filename>\n");
2405 mask = talloc_asprintf_append(mask, "%s", buf);
2410 status = do_list(mask,attribute,do_del,false,false);
2411 if (!NT_STATUS_IS_OK(status)) {
2417 /****************************************************************************
2418 Wildcard delete some files.
2419 ****************************************************************************/
2421 static int cmd_wdel(void)
2423 TALLOC_CTX *ctx = talloc_tos();
2427 struct cli_state *targetcli;
2428 char *targetname = NULL;
2431 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2432 d_printf("wdel 0x<attrib> <wcard>\n");
2436 attribute = (uint16)strtol(buf, (char **)NULL, 16);
2438 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2439 d_printf("wdel 0x<attrib> <wcard>\n");
2443 mask = talloc_asprintf(ctx, "%s%s",
2444 client_get_cur_dir(),
2450 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2452 if (!NT_STATUS_IS_OK(status)) {
2453 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2457 status = cli_unlink(targetcli, targetname, attribute);
2458 if (!NT_STATUS_IS_OK(status)) {
2459 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2465 /****************************************************************************
2466 ****************************************************************************/
2468 static int cmd_open(void)
2470 TALLOC_CTX *ctx = talloc_tos();
2473 char *targetname = NULL;
2474 struct cli_state *targetcli;
2475 uint16_t fnum = (uint16_t)-1;
2478 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2479 d_printf("open <filename>\n");
2482 mask = talloc_asprintf(ctx,
2484 client_get_cur_dir(),
2490 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2492 if (!NT_STATUS_IS_OK(status)) {
2493 d_printf("open %s: %s\n", mask, nt_errstr(status));
2497 status = cli_ntcreate(targetcli, targetname, 0,
2498 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2499 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2501 if (!NT_STATUS_IS_OK(status)) {
2502 status = cli_ntcreate(targetcli, targetname, 0,
2504 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2506 if (NT_STATUS_IS_OK(status)) {
2507 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2509 d_printf("Failed to open file %s. %s\n",
2510 targetname, nt_errstr(status));
2513 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2518 static int cmd_posix_encrypt(void)
2520 TALLOC_CTX *ctx = talloc_tos();
2521 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2523 if (cli->use_kerberos) {
2524 status = cli_gss_smb_encryption_start(cli);
2526 char *domain = NULL;
2528 char *password = NULL;
2530 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2531 d_printf("posix_encrypt domain user password\n");
2535 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2536 d_printf("posix_encrypt domain user password\n");
2540 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2541 d_printf("posix_encrypt domain user password\n");
2545 status = cli_raw_ntlm_smb_encryption_start(cli,
2551 if (!NT_STATUS_IS_OK(status)) {
2552 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2554 d_printf("encryption on\n");
2561 /****************************************************************************
2562 ****************************************************************************/
2564 static int cmd_posix_open(void)
2566 TALLOC_CTX *ctx = talloc_tos();
2569 char *targetname = NULL;
2570 struct cli_state *targetcli;
2575 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2576 d_printf("posix_open <filename> 0<mode>\n");
2579 mask = talloc_asprintf(ctx,
2581 client_get_cur_dir(),
2587 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2588 d_printf("posix_open <filename> 0<mode>\n");
2591 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2593 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2595 if (!NT_STATUS_IS_OK(status)) {
2596 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2600 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2602 if (!NT_STATUS_IS_OK(status)) {
2603 status = cli_posix_open(targetcli, targetname,
2604 O_CREAT|O_RDONLY, mode, &fnum);
2605 if (!NT_STATUS_IS_OK(status)) {
2606 d_printf("Failed to open file %s. %s\n", targetname,
2609 d_printf("posix_open file %s: for readonly fnum %d\n",
2613 d_printf("posix_open file %s: for read/write fnum %d\n",
2620 static int cmd_posix_mkdir(void)
2622 TALLOC_CTX *ctx = talloc_tos();
2625 char *targetname = NULL;
2626 struct cli_state *targetcli;
2630 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2631 d_printf("posix_mkdir <filename> 0<mode>\n");
2634 mask = talloc_asprintf(ctx,
2636 client_get_cur_dir(),
2642 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2643 d_printf("posix_mkdir <filename> 0<mode>\n");
2646 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2648 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2650 if (!NT_STATUS_IS_OK(status)) {
2651 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2655 status = cli_posix_mkdir(targetcli, targetname, mode);
2656 if (!NT_STATUS_IS_OK(status)) {
2657 d_printf("Failed to open file %s. %s\n",
2658 targetname, nt_errstr(status));
2660 d_printf("posix_mkdir created directory %s\n", targetname);
2665 static int cmd_posix_unlink(void)
2667 TALLOC_CTX *ctx = talloc_tos();
2670 char *targetname = NULL;
2671 struct cli_state *targetcli;
2674 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2675 d_printf("posix_unlink <filename>\n");
2678 mask = talloc_asprintf(ctx,
2680 client_get_cur_dir(),
2686 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2688 if (!NT_STATUS_IS_OK(status)) {
2689 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
2693 status = cli_posix_unlink(targetcli, targetname);
2694 if (!NT_STATUS_IS_OK(status)) {
2695 d_printf("Failed to unlink file %s. %s\n",
2696 targetname, nt_errstr(status));
2698 d_printf("posix_unlink deleted file %s\n", targetname);
2704 static int cmd_posix_rmdir(void)
2706 TALLOC_CTX *ctx = talloc_tos();
2709 char *targetname = NULL;
2710 struct cli_state *targetcli;
2713 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2714 d_printf("posix_rmdir <filename>\n");
2717 mask = talloc_asprintf(ctx,
2719 client_get_cur_dir(),
2725 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2727 if (!NT_STATUS_IS_OK(status)) {
2728 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
2732 status = cli_posix_rmdir(targetcli, targetname);
2733 if (!NT_STATUS_IS_OK(status)) {
2734 d_printf("Failed to unlink directory %s. %s\n",
2735 targetname, nt_errstr(status));
2737 d_printf("posix_rmdir deleted directory %s\n", targetname);
2743 static int cmd_close(void)
2745 TALLOC_CTX *ctx = talloc_tos();
2750 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2751 d_printf("close <fnum>\n");
2756 /* We really should use the targetcli here.... */
2757 status = cli_close(cli, fnum);
2758 if (!NT_STATUS_IS_OK(status)) {
2759 d_printf("close %d: %s\n", fnum, nt_errstr(status));
2765 static int cmd_posix(void)
2767 TALLOC_CTX *ctx = talloc_tos();
2768 uint16 major, minor;
2769 uint32 caplow, caphigh;
2773 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2774 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2778 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
2780 if (!NT_STATUS_IS_OK(status)) {
2781 d_printf("Can't get UNIX CIFS extensions version from "
2782 "server: %s\n", nt_errstr(status));
2786 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2788 caps = talloc_strdup(ctx, "");
2792 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2793 caps = talloc_asprintf_append(caps, "locks ");
2798 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2799 caps = talloc_asprintf_append(caps, "acls ");
2804 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2805 caps = talloc_asprintf_append(caps, "eas ");
2810 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2811 caps = talloc_asprintf_append(caps, "pathnames ");
2816 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2817 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2822 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2823 caps = talloc_asprintf_append(caps, "large_read ");
2828 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2829 caps = talloc_asprintf_append(caps, "large_write ");
2834 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2835 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2840 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2841 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2847 if (*caps && caps[strlen(caps)-1] == ' ') {
2848 caps[strlen(caps)-1] = '\0';
2851 d_printf("Server supports CIFS capabilities %s\n", caps);
2853 status = cli_set_unix_extensions_capabilities(cli, major, minor,
2855 if (!NT_STATUS_IS_OK(status)) {
2856 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2861 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2862 CLI_DIRSEP_CHAR = '/';
2863 *CLI_DIRSEP_STR = '/';
2864 client_set_cur_dir(CLI_DIRSEP_STR);
2870 static int cmd_lock(void)
2872 TALLOC_CTX *ctx = talloc_tos();
2874 uint64_t start, len;
2875 enum brl_type lock_type;
2879 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2880 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2885 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2886 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2890 if (*buf == 'r' || *buf == 'R') {
2891 lock_type = READ_LOCK;
2892 } else if (*buf == 'w' || *buf == 'W') {
2893 lock_type = WRITE_LOCK;
2895 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2899 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2900 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2904 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2906 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2907 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2911 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2913 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
2914 if (!NT_STATUS_IS_OK(status)) {
2915 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
2921 static int cmd_unlock(void)
2923 TALLOC_CTX *ctx = talloc_tos();
2925 uint64_t start, len;
2929 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2930 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2935 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2936 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2940 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2942 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2943 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2947 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2949 status = cli_posix_unlock(cli, fnum, start, len);
2950 if (!NT_STATUS_IS_OK(status)) {
2951 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
2958 /****************************************************************************
2960 ****************************************************************************/
2962 static int cmd_rmdir(void)
2964 TALLOC_CTX *ctx = talloc_tos();
2967 char *targetname = NULL;
2968 struct cli_state *targetcli;
2971 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2972 d_printf("rmdir <dirname>\n");
2975 mask = talloc_asprintf(ctx,
2977 client_get_cur_dir(),
2983 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2985 if (!NT_STATUS_IS_OK(status)) {
2986 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
2990 status = cli_rmdir(targetcli, targetname);
2991 if (!NT_STATUS_IS_OK(status)) {
2992 d_printf("%s removing remote directory file %s\n",
2993 nt_errstr(status), mask);
2999 /****************************************************************************
3001 ****************************************************************************/
3003 static int cmd_link(void)
3005 TALLOC_CTX *ctx = talloc_tos();
3006 char *oldname = NULL;
3007 char *newname = NULL;
3010 char *targetname = NULL;
3011 struct cli_state *targetcli;
3014 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3015 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3016 d_printf("link <oldname> <newname>\n");
3019 oldname = talloc_asprintf(ctx,
3021 client_get_cur_dir(),
3026 newname = talloc_asprintf(ctx,
3028 client_get_cur_dir(),
3034 status = cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli,
3036 if (!NT_STATUS_IS_OK(status)) {
3037 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3041 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3042 d_printf("Server doesn't support UNIX CIFS calls.\n");
3046 status = cli_posix_hardlink(targetcli, targetname, newname);
3047 if (!NT_STATUS_IS_OK(status)) {
3048 d_printf("%s linking files (%s -> %s)\n",
3049 nt_errstr(status), newname, oldname);
3055 /****************************************************************************
3057 ****************************************************************************/
3059 static int cmd_readlink(void)
3061 TALLOC_CTX *ctx = talloc_tos();
3064 char *targetname = NULL;
3065 char linkname[PATH_MAX+1];
3066 struct cli_state *targetcli;
3069 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3070 d_printf("readlink <name>\n");
3073 name = talloc_asprintf(ctx,
3075 client_get_cur_dir(),
3081 status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
3083 if (!NT_STATUS_IS_OK(status)) {
3084 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3088 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3089 d_printf("Server doesn't support UNIX CIFS calls.\n");
3093 status = cli_posix_readlink(targetcli, name, linkname, PATH_MAX+1);
3094 if (!NT_STATUS_IS_OK(status)) {
3095 d_printf("%s readlink on file %s\n",
3096 nt_errstr(status), name);
3100 d_printf("%s -> %s\n", name, linkname);
3106 /****************************************************************************
3108 ****************************************************************************/
3110 static int cmd_symlink(void)
3112 TALLOC_CTX *ctx = talloc_tos();
3113 char *oldname = NULL;
3114 char *newname = NULL;
3117 struct cli_state *newcli;
3120 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3121 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3122 d_printf("symlink <oldname> <newname>\n");
3125 /* Oldname (link target) must be an untouched blob. */
3128 if (SERVER_HAS_UNIX_CIFS(cli)) {
3129 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3134 /* New name must be present in share namespace. */
3135 status = cli_resolve_path(ctx, "", auth_info, cli, newname,
3137 if (!NT_STATUS_IS_OK(status)) {
3138 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3141 status = cli_posix_symlink(newcli, oldname, newname);
3143 status = cli_symlink(
3145 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3148 if (!NT_STATUS_IS_OK(status)) {
3149 d_printf("%s symlinking files (%s -> %s)\n",
3150 nt_errstr(status), oldname, newname);
3157 /****************************************************************************
3159 ****************************************************************************/
3161 static int cmd_chmod(void)
3163 TALLOC_CTX *ctx = talloc_tos();
3167 char *targetname = NULL;
3168 struct cli_state *targetcli;
3172 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3173 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3174 d_printf("chmod mode file\n");
3177 src = talloc_asprintf(ctx,
3179 client_get_cur_dir(),
3185 mode = (mode_t)strtol(buf, NULL, 8);
3187 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3189 if (!NT_STATUS_IS_OK(status)) {
3190 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3194 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3195 d_printf("Server doesn't support UNIX CIFS calls.\n");
3199 status = cli_posix_chmod(targetcli, targetname, mode);
3200 if (!NT_STATUS_IS_OK(status)) {
3201 d_printf("%s chmod file %s 0%o\n",
3202 nt_errstr(status), src, (unsigned int)mode);
3209 static const char *filetype_to_str(mode_t mode)
3211 if (S_ISREG(mode)) {
3212 return "regular file";
3213 } else if (S_ISDIR(mode)) {
3217 if (S_ISCHR(mode)) {
3218 return "character device";
3222 if (S_ISBLK(mode)) {
3223 return "block device";
3227 if (S_ISFIFO(mode)) {
3232 if (S_ISLNK(mode)) {
3233 return "symbolic link";
3237 if (S_ISSOCK(mode)) {
3244 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3253 static char *unix_mode_to_str(char *s, mode_t m)
3256 const char *str = filetype_to_str(m);
3272 *p++ = str[1] == 'y' ? 'l' : 's';
3279 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3280 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3281 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3282 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3283 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3284 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3285 *p++ = rwx_to_str(m, S_IROTH, 'r');
3286 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3287 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3292 /****************************************************************************
3293 Utility function for UNIX getfacl.
3294 ****************************************************************************/
3296 static char *perms_to_string(fstring permstr, unsigned char perms)
3298 fstrcpy(permstr, "---");
3299 if (perms & SMB_POSIX_ACL_READ) {
3302 if (perms & SMB_POSIX_ACL_WRITE) {
3305 if (perms & SMB_POSIX_ACL_EXECUTE) {
3311 /****************************************************************************
3313 ****************************************************************************/
3315 static int cmd_getfacl(void)
3317 TALLOC_CTX *ctx = talloc_tos();
3320 char *targetname = NULL;
3321 struct cli_state *targetcli;
3322 uint16 major, minor;
3323 uint32 caplow, caphigh;
3324 char *retbuf = NULL;
3326 SMB_STRUCT_STAT sbuf;
3327 uint16 num_file_acls = 0;
3328 uint16 num_dir_acls = 0;
3332 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3333 d_printf("getfacl filename\n");
3336 src = talloc_asprintf(ctx,
3338 client_get_cur_dir(),
3344 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3346 if (!NT_STATUS_IS_OK(status)) {
3347 d_printf("stat %s: %s\n", src, nt_errstr(status));
3351 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3352 d_printf("Server doesn't support UNIX CIFS calls.\n");
3356 status = cli_unix_extensions_version(targetcli, &major, &minor,
3358 if (!NT_STATUS_IS_OK(status)) {
3359 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3364 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3365 d_printf("This server supports UNIX extensions "
3366 "but doesn't support POSIX ACLs.\n");
3370 status = cli_posix_stat(targetcli, targetname, &sbuf);
3371 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3372 d_printf("%s getfacl doing a stat on file %s\n",
3373 nt_errstr(status), src);
3377 status = cli_posix_getfacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3378 if (!NT_STATUS_IS_OK(status)) {
3379 d_printf("%s getfacl file %s\n",
3380 nt_errstr(status), src);
3384 /* ToDo : Print out the ACL values. */
3385 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3386 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3387 src, (unsigned int)CVAL(retbuf,0) );
3391 num_file_acls = SVAL(retbuf,2);
3392 num_dir_acls = SVAL(retbuf,4);
3393 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3394 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3396 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3397 (unsigned int)rb_size);
3401 d_printf("# file: %s\n", src);
3402 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3404 if (num_file_acls == 0 && num_dir_acls == 0) {
3405 d_printf("No acls found.\n");
3408 for (i = 0; i < num_file_acls; i++) {
3411 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3412 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3415 case SMB_POSIX_ACL_USER_OBJ:
3418 case SMB_POSIX_ACL_USER:
3419 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3420 d_printf("user:%u:", uorg);
3422 case SMB_POSIX_ACL_GROUP_OBJ:
3423 d_printf("group::");
3425 case SMB_POSIX_ACL_GROUP:
3426 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3427 d_printf("group:%u:", uorg);
3429 case SMB_POSIX_ACL_MASK:
3432 case SMB_POSIX_ACL_OTHER:
3433 d_printf("other::");
3436 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3437 src, (unsigned int)tagtype );
3442 d_printf("%s\n", perms_to_string(permstring, perms));
3445 for (i = 0; i < num_dir_acls; i++) {
3448 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3449 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3452 case SMB_POSIX_ACL_USER_OBJ:
3453 d_printf("default:user::");
3455 case SMB_POSIX_ACL_USER:
3456 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3457 d_printf("default:user:%u:", uorg);
3459 case SMB_POSIX_ACL_GROUP_OBJ:
3460 d_printf("default:group::");
3462 case SMB_POSIX_ACL_GROUP:
3463 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3464 d_printf("default:group:%u:", uorg);
3466 case SMB_POSIX_ACL_MASK:
3467 d_printf("default:mask::");
3469 case SMB_POSIX_ACL_OTHER:
3470 d_printf("default:other::");
3473 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3474 src, (unsigned int)tagtype );
3479 d_printf("%s\n", perms_to_string(permstring, perms));
3485 /****************************************************************************
3486 Get the EA list of a file
3487 ****************************************************************************/
3489 static int cmd_geteas(void)
3491 TALLOC_CTX *ctx = talloc_tos();
3494 char *targetname = NULL;
3495 struct cli_state *targetcli;
3498 struct ea_struct *eas;
3500 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3501 d_printf("geteas filename\n");
3504 src = talloc_asprintf(ctx,
3506 client_get_cur_dir(),
3512 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3514 if (!NT_STATUS_IS_OK(status)) {
3515 d_printf("stat %s: %s\n", src, nt_errstr(status));
3519 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3521 if (!NT_STATUS_IS_OK(status)) {
3522 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3526 for (i=0; i<num_eas; i++) {
3527 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3528 dump_data_file(eas[i].value.data, eas[i].value.length, false,
3538 /****************************************************************************
3540 ****************************************************************************/
3542 static int cmd_setea(void)
3544 TALLOC_CTX *ctx = talloc_tos();
3547 char *eaname = NULL;
3548 char *eavalue = NULL;
3549 char *targetname = NULL;
3550 struct cli_state *targetcli;
3553 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
3554 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
3555 d_printf("setea filename eaname value\n");
3558 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
3559 eavalue = talloc_strdup(ctx, "");
3561 src = talloc_asprintf(ctx,
3563 client_get_cur_dir(),
3569 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3571 if (!NT_STATUS_IS_OK(status)) {
3572 d_printf("stat %s: %s\n", src, nt_errstr(status));
3576 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
3578 if (!NT_STATUS_IS_OK(status)) {
3579 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
3586 /****************************************************************************
3588 ****************************************************************************/
3590 static int cmd_stat(void)
3592 TALLOC_CTX *ctx = talloc_tos();
3595 char *targetname = NULL;
3596 struct cli_state *targetcli;
3598 SMB_STRUCT_STAT sbuf;
3603 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3604 d_printf("stat file\n");
3607 src = talloc_asprintf(ctx,
3609 client_get_cur_dir(),
3615 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3617 if (!NT_STATUS_IS_OK(status)) {
3618 d_printf("stat %s: %s\n", src, nt_errstr(status));
3622 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3623 d_printf("Server doesn't support UNIX CIFS calls.\n");
3627 status = cli_posix_stat(targetcli, targetname, &sbuf);
3628 if (!NT_STATUS_IS_OK(status)) {
3629 d_printf("%s stat file %s\n",
3630 nt_errstr(status), src);
3634 /* Print out the stat values. */
3635 d_printf("File: %s\n", src);
3636 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3637 (double)sbuf.st_ex_size,
3638 (unsigned int)sbuf.st_ex_blocks,
3639 filetype_to_str(sbuf.st_ex_mode));
3641 #if defined(S_ISCHR) && defined(S_ISBLK)
3642 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
3643 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3644 (double)sbuf.st_ex_ino,
3645 (unsigned int)sbuf.st_ex_nlink,
3646 unix_dev_major(sbuf.st_ex_rdev),
3647 unix_dev_minor(sbuf.st_ex_rdev));
3650 d_printf("Inode: %.0f\tLinks: %u\n",
3651 (double)sbuf.st_ex_ino,
3652 (unsigned int)sbuf.st_ex_nlink);
3654 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3655 ((int)sbuf.st_ex_mode & 0777),
3656 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
3657 (unsigned int)sbuf.st_ex_uid,
3658 (unsigned int)sbuf.st_ex_gid);
3660 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
3661 lt = localtime(&tmp_time);
3663 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3665 fstrcpy(mode_str, "unknown");
3667 d_printf("Access: %s\n", mode_str);
3669 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3670 lt = localtime(&tmp_time);
3672 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3674 fstrcpy(mode_str, "unknown");
3676 d_printf("Modify: %s\n", mode_str);
3678 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
3679 lt = localtime(&tmp_time);
3681 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3683 fstrcpy(mode_str, "unknown");
3685 d_printf("Change: %s\n", mode_str);
3691 /****************************************************************************
3693 ****************************************************************************/
3695 static int cmd_chown(void)
3697 TALLOC_CTX *ctx = talloc_tos();
3701 char *buf, *buf2, *buf3;
3702 struct cli_state *targetcli;
3703 char *targetname = NULL;
3706 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3707 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3708 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3709 d_printf("chown uid gid file\n");
3713 uid = (uid_t)atoi(buf);
3714 gid = (gid_t)atoi(buf2);
3716 src = talloc_asprintf(ctx,
3718 client_get_cur_dir(),
3723 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3725 if (!NT_STATUS_IS_OK(status)) {
3726 d_printf("chown %s: %s\n", src, nt_errstr(status));
3730 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3731 d_printf("Server doesn't support UNIX CIFS calls.\n");
3735 status = cli_posix_chown(targetcli, targetname, uid, gid);
3736 if (!NT_STATUS_IS_OK(status)) {
3737 d_printf("%s chown file %s uid=%d, gid=%d\n",
3738 nt_errstr(status), src, (int)uid, (int)gid);
3745 /****************************************************************************
3747 ****************************************************************************/
3749 static int cmd_rename(void)
3751 TALLOC_CTX *ctx = talloc_tos();
3754 struct cli_state *targetcli;
3759 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3760 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3761 d_printf("rename <src> <dest>\n");
3765 src = talloc_asprintf(ctx,
3767 client_get_cur_dir(),
3773 dest = talloc_asprintf(ctx,
3775 client_get_cur_dir(),
3781 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3783 if (!NT_STATUS_IS_OK(status)) {
3784 d_printf("rename %s: %s\n", src, nt_errstr(status));
3788 status = cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli,
3790 if (!NT_STATUS_IS_OK(status)) {
3791 d_printf("rename %s: %s\n", dest, nt_errstr(status));
3795 status = cli_rename(targetcli, targetsrc, targetdest);
3796 if (!NT_STATUS_IS_OK(status)) {
3797 d_printf("%s renaming files %s -> %s \n",
3807 /****************************************************************************
3808 Print the volume name.
3809 ****************************************************************************/
3811 static int cmd_volume(void)
3814 uint32_t serial_num;
3818 status = cli_get_fs_volume_info(cli, talloc_tos(),
3819 &volname, &serial_num,
3821 if (!NT_STATUS_IS_OK(status)) {
3822 d_printf("Error %s getting volume info\n", nt_errstr(status));
3826 d_printf("Volume: |%s| serial number 0x%x\n",
3827 volname, (unsigned int)serial_num);
3831 /****************************************************************************
3832 Hard link files using the NT call.
3833 ****************************************************************************/
3835 static int cmd_hardlink(void)
3837 TALLOC_CTX *ctx = talloc_tos();
3840 struct cli_state *targetcli;
3844 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3845 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3846 d_printf("hardlink <src> <dest>\n");
3850 src = talloc_asprintf(ctx,
3852 client_get_cur_dir(),
3858 dest = talloc_asprintf(ctx,
3860 client_get_cur_dir(),
3866 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3868 if (!NT_STATUS_IS_OK(status)) {
3869 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
3873 status = cli_nt_hardlink(targetcli, targetname, dest);
3874 if (!NT_STATUS_IS_OK(status)) {
3875 d_printf("%s doing an NT hard link of files\n",
3883 /****************************************************************************
3884 Toggle the prompt flag.
3885 ****************************************************************************/
3887 static int cmd_prompt(void)
3890 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
3894 /****************************************************************************
3895 Set the newer than time.
3896 ****************************************************************************/
3898 static int cmd_newer(void)
3900 TALLOC_CTX *ctx = talloc_tos();
3903 SMB_STRUCT_STAT sbuf;
3905 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
3906 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
3907 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3908 DEBUG(1,("Getting files newer than %s",
3909 time_to_asc(newer_than)));
3914 if (ok && newer_than == 0) {
3915 d_printf("Error setting newer-than time\n");
3922 /****************************************************************************
3923 Watch directory changes
3924 ****************************************************************************/
3926 static int cmd_notify(void)
3928 TALLOC_CTX *frame = talloc_stackframe();
3933 name = talloc_strdup(talloc_tos(), client_get_cur_dir());
3937 if (!next_token_talloc(talloc_tos(), &cmd_ptr, &buf, NULL)) {
3940 name = talloc_asprintf_append(name, "%s", buf);
3944 status = cli_ntcreate(
3945 cli, name, 0, FILE_READ_DATA, 0,
3946 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
3947 FILE_OPEN, 0, 0, &fnum);
3948 if (!NT_STATUS_IS_OK(status)) {
3949 d_printf("Could not open file: %s\n", nt_errstr(status));
3954 uint32_t i, num_changes;
3955 struct notify_change *changes;
3957 status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
3959 talloc_tos(), &num_changes, &changes);
3960 if (!NT_STATUS_IS_OK(status)) {
3961 d_printf("notify returned %s\n",
3965 for (i=0; i<num_changes; i++) {
3966 printf("%4.4x %s\n", changes[i].action,
3969 TALLOC_FREE(changes);
3972 d_printf("notify <file>\n");
3978 /****************************************************************************
3979 Set the archive level.
3980 ****************************************************************************/
3982 static int cmd_archive(void)
3984 TALLOC_CTX *ctx = talloc_tos();
3987 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3988 archive_level = atoi(buf);
3990 d_printf("Archive level is %d\n",archive_level);
3996 /****************************************************************************
3997 Toggle the backup_intent state.
3998 ****************************************************************************/
4000 static int cmd_backup(void)
4002 backup_intent = !backup_intent;
4003 cli_set_backup_intent(cli, backup_intent);
4004 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4008 /****************************************************************************
4009 Toggle the lowercaseflag.
4010 ****************************************************************************/
4012 static int cmd_lowercase(void)
4014 lowercase = !lowercase;
4015 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4019 /****************************************************************************
4020 Toggle the case sensitive flag.
4021 ****************************************************************************/
4023 static int cmd_setcase(void)
4025 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4027 cli_set_case_sensitive(cli, !orig_case_sensitive);
4028 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4033 /****************************************************************************
4034 Toggle the showacls flag.
4035 ****************************************************************************/
4037 static int cmd_showacls(void)
4039 showacls = !showacls;
4040 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4045 /****************************************************************************
4046 Toggle the recurse flag.
4047 ****************************************************************************/
4049 static int cmd_recurse(void)
4052 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4056 /****************************************************************************
4057 Toggle the translate flag.
4058 ****************************************************************************/
4060 static int cmd_translate(void)
4062 translation = !translation;
4063 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4064 translation?"on":"off"));
4068 /****************************************************************************
4070 ****************************************************************************/
4072 static int cmd_lcd(void)
4074 TALLOC_CTX *ctx = talloc_tos();
4078 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4079 if (chdir(buf) == -1) {
4080 d_printf("chdir to %s failed (%s)\n",
4081 buf, strerror(errno));
4088 DEBUG(2,("the local directory is now %s\n",d));
4093 /****************************************************************************
4094 Get a file restarting at end of local file.
4095 ****************************************************************************/
4097 static int cmd_reget(void)
4099 TALLOC_CTX *ctx = talloc_tos();
4100 char *local_name = NULL;
4101 char *remote_name = NULL;
4105 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4110 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4111 d_printf("reget <filename>\n");
4114 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4118 remote_name = clean_name(ctx,remote_name);
4124 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4129 return do_get(remote_name, local_name, true);
4132 /****************************************************************************
4133 Put a file restarting at end of local file.
4134 ****************************************************************************/
4136 static int cmd_reput(void)
4138 TALLOC_CTX *ctx = talloc_tos();
4139 char *local_name = NULL;
4140 char *remote_name = NULL;
4144 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4149 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4150 d_printf("reput <filename>\n");
4154 if (!file_exist_stat(local_name, &st, false)) {
4155 d_printf("%s does not exist\n", local_name);
4159 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4160 remote_name = talloc_asprintf_append(remote_name,
4163 remote_name = talloc_asprintf_append(remote_name,
4170 remote_name = clean_name(ctx, remote_name);
4175 return do_put(remote_name, local_name, true);
4178 /****************************************************************************
4180 ****************************************************************************/
4182 static void browse_fn(const char *name, uint32 m,
4183 const char *comment, void *state)
4185 const char *typestr = "";
4188 case STYPE_DISKTREE:
4192 typestr = "Printer";
4201 /* FIXME: If the remote machine returns non-ascii characters
4202 in any of these fields, they can corrupt the output. We
4203 should remove them. */
4205 d_printf("\t%-15s %-10.10s%s\n",
4206 name,typestr,comment);
4208 d_printf ("%s|%s|%s\n",typestr,name,comment);
4212 static bool browse_host_rpc(bool sort)
4215 struct rpc_pipe_client *pipe_hnd = NULL;
4216 TALLOC_CTX *frame = talloc_stackframe();
4218 struct srvsvc_NetShareInfoCtr info_ctr;
4219 struct srvsvc_NetShareCtr1 ctr1;
4220 uint32_t resume_handle = 0;
4221 uint32_t total_entries = 0;
4223 struct dcerpc_binding_handle *b;
4225 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
4228 if (!NT_STATUS_IS_OK(status)) {
4229 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4230 nt_errstr(status)));
4235 b = pipe_hnd->binding_handle;
4237 ZERO_STRUCT(info_ctr);
4241 info_ctr.ctr.ctr1 = &ctr1;
4243 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4251 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4252 TALLOC_FREE(pipe_hnd);
4257 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4258 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4259 browse_fn(info.name, info.type, info.comment, NULL);
4262 TALLOC_FREE(pipe_hnd);
4267 /****************************************************************************
4268 Try and browse available connections on a host.
4269 ****************************************************************************/
4271 static bool browse_host(bool sort)
4275 d_printf("\n\tSharename Type Comment\n");
4276 d_printf("\t--------- ---- -------\n");
4279 if (browse_host_rpc(sort)) {
4283 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1) {
4284 NTSTATUS status = cli_nt_error(cli);
4285 d_printf("Error returning browse list: %s\n",
4292 /****************************************************************************
4294 ****************************************************************************/
4296 static void server_fn(const char *name, uint32 m,
4297 const char *comment, void *state)
4301 d_printf("\t%-16s %s\n", name, comment);
4303 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4307 /****************************************************************************
4308 Try and browse available connections on a host.
4309 ****************************************************************************/
4311 static bool list_servers(const char *wk_grp)
4315 if (!cli->server_domain)
4319 d_printf("\n\tServer Comment\n");
4320 d_printf("\t--------- -------\n");
4322 fstrcpy( state, "Server" );
4323 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4327 d_printf("\n\tWorkgroup Master\n");
4328 d_printf("\t--------- -------\n");
4331 fstrcpy( state, "Workgroup" );
4332 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4337 /****************************************************************************
4338 Print or set current VUID
4339 ****************************************************************************/
4341 static int cmd_vuid(void)
4343 TALLOC_CTX *ctx = talloc_tos();
4346 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4347 d_printf("Current VUID is %d\n",
4348 cli_state_get_uid(cli));
4352 cli_state_set_uid(cli, atoi(buf));
4356 /****************************************************************************
4357 Setup a new VUID, by issuing a session setup
4358 ****************************************************************************/
4360 static int cmd_logon(void)
4362 TALLOC_CTX *ctx = talloc_tos();
4363 char *l_username, *l_password;
4366 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
4367 d_printf("logon <username> [<password>]\n");
4371 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
4372 char pwd[256] = {0};
4375 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
4377 l_password = talloc_strdup(ctx, pwd);
4384 nt_status = cli_session_setup(cli, l_username,
4385 l_password, strlen(l_password),
4386 l_password, strlen(l_password),
4388 if (!NT_STATUS_IS_OK(nt_status)) {
4389 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
4393 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
4401 static int cmd_logoff(void)
4405 status = cli_ulogoff(cli);
4406 if (!NT_STATUS_IS_OK(status)) {
4407 d_printf("logoff failed: %s\n", nt_errstr(status));
4411 d_printf("logoff successful\n");
4417 * tree connect (connect to a share)
4420 static int cmd_tcon(void)
4422 TALLOC_CTX *ctx = talloc_tos();
4426 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
4427 d_printf("tcon <sharename>\n");
4435 status = cli_tree_connect(cli, sharename, "?????", "", 0);
4436 if (!NT_STATUS_IS_OK(status)) {
4437 d_printf("tcon failed: %s\n", nt_errstr(status));
4441 talloc_free(sharename);
4443 d_printf("tcon to %s successful, tid: %u\n", sharename,
4444 cli_state_get_tid(cli));
4449 * tree disconnect (disconnect from a share)
4452 static int cmd_tdis(void)
4456 status = cli_tdis(cli);
4457 if (!NT_STATUS_IS_OK(status)) {
4458 d_printf("tdis failed: %s\n", nt_errstr(status));
4462 d_printf("tdis successful\n");
4471 static int cmd_tid(void)
4473 TALLOC_CTX *ctx = talloc_tos();
4476 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
4477 if (cli_state_has_tcon(cli)) {
4478 d_printf("current tid is %d\n", cli_state_get_tid(cli));
4480 d_printf("no tcon currently\n");
4483 uint16_t tid = atoi(tid_str);
4484 cli_state_set_tid(cli, tid);
4491 /****************************************************************************
4492 list active connections
4493 ****************************************************************************/
4495 static int cmd_list_connect(void)
4497 cli_cm_display(cli);
4501 /****************************************************************************
4502 display the current active client connection
4503 ****************************************************************************/
4505 static int cmd_show_connect( void )
4507 TALLOC_CTX *ctx = talloc_tos();
4508 struct cli_state *targetcli;
4512 status = cli_resolve_path(ctx, "", auth_info, cli,
4513 client_get_cur_dir(), &targetcli,
4515 if (!NT_STATUS_IS_OK(status)) {
4516 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
4520 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli->conn), targetcli->share);
4524 /****************************************************************************
4526 ***************************************************************************/
4528 int cmd_iosize(void)
4530 TALLOC_CTX *ctx = talloc_tos();
4534 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4535 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
4537 d_printf("iosize <n> or iosize 0x<n>. "
4538 "Minimum is 0 (default), "
4539 "max is 16776960 (0xFFFF00)\n");
4541 d_printf("iosize <n> or iosize 0x<n>. "
4542 "(Encrypted connection) ,"
4543 "Minimum is 0 (default), "
4544 "max is 130048 (0x1FC00)\n");
4547 d_printf("iosize <n> or iosize 0x<n>.\n");
4552 iosize = strtol(buf,NULL,0);
4553 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
4554 if (smb_encrypt && (iosize < 0 || iosize > 0xFC00)) {
4555 d_printf("iosize out of range for encrypted "
4556 "connection (min = 0 (default), "
4557 "max = 130048 (0x1FC00)\n");
4559 } else if (!smb_encrypt && (iosize < 0 || iosize > 0xFFFF00)) {
4560 d_printf("iosize out of range (min = 0 (default), "
4561 "max = 16776960 (0xFFFF00)\n");
4566 io_bufsize = iosize;
4567 d_printf("iosize is now %d\n", io_bufsize);
4571 /****************************************************************************
4573 ***************************************************************************/
4575 static int cmd_timeout(void)
4577 TALLOC_CTX *ctx = talloc_tos();
4580 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4581 unsigned int old_timeout = cli_set_timeout(cli, 0);
4582 cli_set_timeout(cli, old_timeout);
4583 d_printf("timeout <n> (per-operation timeout "
4584 "in seconds - currently %u).\n",
4589 io_timeout = strtol(buf,NULL,0);
4590 cli_set_timeout(cli, io_timeout*1000);
4591 d_printf("io_timeout per operation is now %d\n", io_timeout);
4596 /****************************************************************************
4598 ****************************************************************************/
4599 static int cmd_history(void)
4601 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4605 hlist = history_list();
4607 for (i = 0; hlist && hlist[i]; i++) {
4608 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
4611 DEBUG(0,("no history without readline support\n"));
4617 /* Some constants for completing filename arguments */
4619 #define COMPL_NONE 0 /* No completions */
4620 #define COMPL_REMOTE 1 /* Complete remote filename */
4621 #define COMPL_LOCAL 2 /* Complete local filename */
4623 /* This defines the commands supported by this client.
4624 * NOTE: The "!" must be the last one in the list because it's fn pointer
4625 * field is NULL, and NULL in that field is used in process_tok()
4626 * (below) to indicate the end of the list. crh
4631 const char *description;
4632 char compl_args[2]; /* Completion argument info */
4634 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4635 {"allinfo",cmd_allinfo,"<file> show all available info",
4636 {COMPL_NONE,COMPL_NONE}},
4637 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
4638 {"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}},
4639 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
4640 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
4641 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
4642 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
4643 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
4644 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_NONE}},
4645 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_NONE}},
4646 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_NONE}},
4647 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4648 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4649 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4650 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
4651 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4652 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
4653 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_NONE}},
4654 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
4655 {COMPL_REMOTE, COMPL_NONE}},
4656 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4657 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4658 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
4659 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
4660 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
4661 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4662 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4663 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
4664 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4665 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4666 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
4667 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4668 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
4669 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4670 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
4671 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
4672 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
4673 {"notify",cmd_notify,"<file>Get notified of dir changes",{COMPL_REMOTE,COMPL_NONE}},
4674 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
4675 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
4676 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
4677 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4678 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4679 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4680 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4681 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
4682 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
4683 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
4684 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
4685 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4686 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
4687 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4688 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4689 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4690 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
4691 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
4692 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
4693 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
4694 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4695 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_REMOTE,COMPL_NONE}},
4696 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
4697 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
4698 {COMPL_REMOTE, COMPL_LOCAL}},
4699 {"setmode",cmd_setmode,"<file name> <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
4700 {"stat",cmd_stat,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_NONE}},
4701 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4702 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
4703 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
4704 {"timeout",cmd_timeout,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE,COMPL_NONE}},
4705 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
4706 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4707 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
4708 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
4709 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4710 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
4711 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
4712 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
4713 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
4714 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
4715 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
4716 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
4717 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
4719 /* Yes, this must be here, see crh's comment above. */
4720 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
4721 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
4724 /*******************************************************************
4725 Lookup a command string in the list of commands, including
4727 ******************************************************************/
4729 static int process_tok(char *tok)
4731 int i = 0, matches = 0;
4733 int tok_len = strlen(tok);
4735 while (commands[i].fn != NULL) {
4736 if (strequal(commands[i].name,tok)) {
4740 } else if (strnequal(commands[i].name, tok, tok_len)) {
4749 else if (matches == 1)
4755 /****************************************************************************
4757 ****************************************************************************/
4759 static int cmd_help(void)
4761 TALLOC_CTX *ctx = talloc_tos();
4765 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4766 if ((i = process_tok(buf)) >= 0)
4767 d_printf("HELP %s:\n\t%s\n\n",
4768 commands[i].name,commands[i].description);
4770 while (commands[i].description) {
4771 for (j=0; commands[i].description && (j<5); j++) {
4772 d_printf("%-15s",commands[i].name);
4781 /****************************************************************************
4782 Process a -c command string.
4783 ****************************************************************************/
4785 static int process_command_string(const char *cmd_in)
4787 TALLOC_CTX *ctx = talloc_tos();
4788 char *cmd = talloc_strdup(ctx, cmd_in);
4794 /* establish the connection if not already */
4799 status = cli_cm_open(talloc_tos(), NULL,
4800 have_ip ? dest_ss_str : desthost,
4803 max_protocol, port, name_type,
4805 if (!NT_STATUS_IS_OK(status)) {
4808 cli_set_timeout(cli, io_timeout*1000);
4811 while (cmd[0] != '\0') {
4817 if ((p = strchr_m(cmd, ';')) == 0) {
4826 /* and get the first part of the command */
4828 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
4832 if ((i = process_tok(tok)) >= 0) {
4833 rc = commands[i].fn();
4834 } else if (i == -2) {
4835 d_printf("%s: command abbreviation ambiguous\n",tok);
4837 d_printf("%s: command not found\n",tok);
4844 #define MAX_COMPLETIONS 100
4846 struct completion_remote {
4854 static NTSTATUS completion_remote_filter(const char *mnt,
4855 struct file_info *f,
4859 struct completion_remote *info = (struct completion_remote *)state;
4861 if (info->count >= MAX_COMPLETIONS - 1) {
4862 return NT_STATUS_OK;
4864 if (strncmp(info->text, f->name, info->len) != 0) {
4865 return NT_STATUS_OK;
4867 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
4868 return NT_STATUS_OK;
4871 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
4872 info->matches[info->count] = SMB_STRDUP(f->name);
4874 TALLOC_CTX *ctx = talloc_stackframe();
4877 tmp = talloc_strdup(ctx,info->dirmask);
4880 return NT_STATUS_NO_MEMORY;
4882 tmp = talloc_asprintf_append(tmp, "%s", f->name);
4885 return NT_STATUS_NO_MEMORY;
4887 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4888 tmp = talloc_asprintf_append(tmp, "%s",
4893 return NT_STATUS_NO_MEMORY;
4895 info->matches[info->count] = SMB_STRDUP(tmp);
4898 if (info->matches[info->count] == NULL) {
4899 return NT_STATUS_OK;
4901 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
4902 smb_readline_ca_char(0);
4904 if (info->count == 1) {
4905 info->samelen = strlen(info->matches[info->count]);
4907 while (strncmp(info->matches[info->count],
4908 info->matches[info->count-1],
4909 info->samelen) != 0) {
4914 return NT_STATUS_OK;
4917 static char **remote_completion(const char *text, int len)
4919 TALLOC_CTX *ctx = talloc_stackframe();
4920 char *dirmask = NULL;
4921 char *targetpath = NULL;
4922 struct cli_state *targetcli = NULL;
4924 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
4927 /* can't have non-static initialisation on Sun CC, so do it
4933 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
4934 if (!info.matches) {
4940 * We're leaving matches[0] free to fill it later with the text to
4941 * display: Either the one single match or the longest common subset
4944 info.matches[0] = NULL;
4947 for (i = len-1; i >= 0; i--) {
4948 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
4953 info.text = text+i+1;
4954 info.samelen = info.len = len-i-1;
4957 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
4958 if (!info.dirmask) {
4961 strncpy(info.dirmask, text, i+1);
4962 info.dirmask[i+1] = 0;
4963 dirmask = talloc_asprintf(ctx,
4965 client_get_cur_dir(),
4969 info.dirmask = SMB_STRDUP("");
4970 if (!info.dirmask) {
4973 dirmask = talloc_asprintf(ctx,
4975 client_get_cur_dir());
4981 status = cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli,
4983 if (!NT_STATUS_IS_OK(status)) {
4986 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
4987 completion_remote_filter, (void *)&info);
4988 if (!NT_STATUS_IS_OK(status)) {
4992 if (info.count == 1) {
4994 * No matches at all, NULL indicates there is nothing
4996 SAFE_FREE(info.matches[0]);
4997 SAFE_FREE(info.matches);
5002 if (info.count == 2) {
5004 * Exactly one match in matches[1], indicate this is the one
5007 info.matches[0] = info.matches[1];
5008 info.matches[1] = NULL;
5011 return info.matches;
5015 * We got more than one possible match, set the result to the maximum
5019 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
5020 info.matches[info.count] = NULL;
5022 return info.matches;
5025 for (i = 0; i < info.count; i++) {
5026 SAFE_FREE(info.matches[i]);
5028 SAFE_FREE(info.matches);
5029 SAFE_FREE(info.dirmask);
5034 static char **completion_fn(const char *text, int start, int end)
5036 smb_readline_ca_char(' ');
5039 const char *buf, *sp;
5043 buf = smb_readline_get_line_buffer();
5047 sp = strchr(buf, ' ');
5051 for (i = 0; commands[i].name; i++) {
5052 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
5053 (commands[i].name[sp - buf] == 0)) {
5057 if (commands[i].name == NULL)
5063 if (sp == (buf + start))
5064 compl_type = commands[i].compl_args[0];
5066 compl_type = commands[i].compl_args[1];
5068 if (compl_type == COMPL_REMOTE)
5069 return remote_completion(text, end - start);
5070 else /* fall back to local filename completion */
5074 int i, len, samelen = 0, count=1;
5076 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
5083 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
5084 if (strncmp(text, commands[i].name, len) == 0) {
5085 matches[count] = SMB_STRDUP(commands[i].name);
5086 if (!matches[count])
5089 samelen = strlen(matches[count]);
5091 while (strncmp(matches[count], matches[count-1], samelen) != 0)
5098 case 0: /* should never happen */
5102 matches[0] = SMB_STRDUP(matches[1]);
5105 matches[0] = (char *)SMB_MALLOC(samelen+1);
5108 strncpy(matches[0], matches[1], samelen);
5109 matches[0][samelen] = 0;
5111 matches[count] = NULL;
5115 for (i = 0; i < count; i++)
5123 static bool finished;
5125 /****************************************************************************
5126 Make sure we swallow keepalives during idle time.
5127 ****************************************************************************/
5129 static void readline_callback(void)
5131 static time_t last_t;
5132 struct timespec now;
5135 unsigned char garbage[16];
5137 clock_gettime_mono(&now);
5145 /* Ping the server to keep the connection alive using SMBecho. */
5146 memset(garbage, 0xf0, sizeof(garbage));
5147 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
5148 if (NT_STATUS_IS_OK(status)) {
5152 if (!cli_state_is_connected(cli)) {
5153 DEBUG(0,("SMBecho failed (%s). The connection is "
5154 "disconnected now\n", nt_errstr(status)));
5156 smb_readline_done();
5160 /****************************************************************************
5161 Process commands on stdin.
5162 ****************************************************************************/
5164 static int process_stdin(void)
5169 TALLOC_CTX *frame = talloc_stackframe();
5171 char *the_prompt = NULL;
5175 /* display a prompt */
5176 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
5180 line = smb_readline(the_prompt, readline_callback, completion_fn);
5181 SAFE_FREE(the_prompt);
5187 /* special case - first char is ! */
5189 if (system(line + 1) == -1) {
5190 d_printf("system() command %s failed.\n",
5198 /* and get the first part of the command */
5200 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
5206 if ((i = process_tok(tok)) >= 0) {
5207 rc = commands[i].fn();
5208 } else if (i == -2) {
5209 d_printf("%s: command abbreviation ambiguous\n",tok);
5211 d_printf("%s: command not found\n",tok);
5219 /****************************************************************************
5220 Process commands from the client.
5221 ****************************************************************************/
5223 static int process(const char *base_directory)
5228 status = cli_cm_open(talloc_tos(), NULL,
5229 have_ip ? dest_ss_str : desthost,
5230 service, auth_info, true, smb_encrypt,
5231 max_protocol, port, name_type, &cli);
5232 if (!NT_STATUS_IS_OK(status)) {
5236 cli_set_timeout(cli, io_timeout*1000);
5238 if (base_directory && *base_directory) {
5239 rc = do_cd(base_directory);
5247 rc = process_command_string(cmdstr);
5256 /****************************************************************************
5258 ****************************************************************************/
5260 static int do_host_query(const char *query_host)
5264 status = cli_cm_open(talloc_tos(), NULL,
5265 have_ip ? dest_ss_str : query_host,
5266 "IPC$", auth_info, true, smb_encrypt,
5267 max_protocol, port, name_type, &cli);
5268 if (!NT_STATUS_IS_OK(status)) {
5272 cli_set_timeout(cli, io_timeout*1000);
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",
5287 if (port != NBT_SMB_PORT) {
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, NBT_SMB_PORT, name_type,
5298 if (!NT_STATUS_IS_OK(status)) {
5304 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
5308 cli_set_timeout(cli, io_timeout*1000);
5309 list_servers(lp_workgroup());
5316 /****************************************************************************
5317 Handle a tar operation.
5318 ****************************************************************************/
5320 static int do_tar_op(const char *base_directory)
5324 /* do we already have a connection? */
5328 status = cli_cm_open(talloc_tos(), NULL,
5329 have_ip ? dest_ss_str : desthost,
5330 service, auth_info, true, smb_encrypt,
5331 max_protocol, port, name_type, &cli);
5332 if (!NT_STATUS_IS_OK(status)) {
5335 cli_set_timeout(cli, io_timeout*1000);
5340 if (base_directory && *base_directory) {
5341 ret = do_cd(base_directory);
5355 /****************************************************************************
5356 Handle a message operation.
5357 ****************************************************************************/
5359 static int do_message_op(struct user_auth_info *a_info)
5363 status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
5364 port ? port : NBT_SMB_PORT, name_type,
5365 lp_netbios_name(), SMB_SIGNING_DEFAULT, 0, &cli);
5366 if (!NT_STATUS_IS_OK(status)) {
5367 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
5371 cli_set_timeout(cli, io_timeout*1000);
5372 send_message(get_cmdline_auth_info_username(a_info));
5378 /****************************************************************************
5380 ****************************************************************************/
5382 int main(int argc,char *argv[])
5384 const char **const_argv = discard_const_p(const char *, argv);
5385 char *base_directory = NULL;
5387 char *query_host = NULL;
5388 bool message = false;
5389 static const char *new_name_resolve_order = NULL;
5393 bool tar_opt = false;
5394 bool service_opt = false;
5395 struct poptOption long_options[] = {
5398 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
5399 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
5400 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
5401 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
5402 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
5403 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
5404 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
5405 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
5406 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
5407 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
5408 { "timeout", 't', POPT_ARG_INT, &io_timeout, 'b', "Changes the per-operation timeout", "SECONDS" },
5409 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
5410 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
5411 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
5413 POPT_COMMON_CONNECTION
5414 POPT_COMMON_CREDENTIALS
5417 TALLOC_CTX *frame = talloc_stackframe();
5419 if (!client_set_cur_dir("\\")) {
5423 /* set default debug level to 1 regardless of what smb.conf sets */
5424 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
5427 lp_set_cmdline("log level", "1");
5429 auth_info = user_auth_info_init(frame);
5430 if (auth_info == NULL) {
5433 popt_common_set_auth_info(auth_info);
5436 pc = poptGetContext("smbclient", argc, const_argv, long_options, 0);
5437 poptSetOtherOptionHelp(pc, "service <password>");
5439 while ((opt = poptGetNextOpt(pc)) != -1) {
5441 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
5442 /* I see no other way to keep things sane --SSS */
5443 if (tar_opt == true) {
5444 while (poptPeekArg(pc)) {
5450 /* if the service has not yet been specified lets see if it is available in the popt stack */
5451 if (!service_opt && poptPeekArg(pc)) {
5452 service = talloc_strdup(frame, poptGetArg(pc));
5459 /* if the service has already been retrieved then check if we have also a password */
5461 && (!get_cmdline_auth_info_got_pass(auth_info))
5462 && poptPeekArg(pc)) {
5463 set_cmdline_auth_info_password(auth_info,
5470 /* Messages are sent to NetBIOS name type 0x3
5471 * (Messenger Service). Make sure we default
5472 * to port 139 instead of port 445. srl,crh
5475 desthost = talloc_strdup(frame,poptGetOptArg(pc));
5480 port = NBT_SMB_PORT;
5485 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
5489 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
5493 setup_logging("smbclient", DEBUG_STDERR );
5494 display_set_stderr();
5498 query_host = talloc_strdup(frame, poptGetOptArg(pc));
5504 lp_set_cmdline("client max protocol", poptGetOptArg(pc));
5507 /* We must use old option processing for this. Find the
5508 * position of the -T option in the raw argv[]. */
5511 extern struct tar tar_ctx;
5513 for (i = 1; i < argc; i++) {
5514 if (strncmp("-T", argv[i],2)==0)
5518 if (!tar_parse_args(&tar_ctx, poptGetOptArg(pc), argv + i, argc - i)) {
5519 poptPrintUsage(pc, stderr, 0);
5523 /* this must be the last option, mark we have parsed it so that we know we have */
5527 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
5528 if (!base_directory) {
5539 return(do_smb_browse());
5544 /* We may still have some leftovers after the last popt option has been called */
5545 if (tar_opt == true) {
5546 while (poptPeekArg(pc)) {
5552 /* if the service has not yet been specified lets see if it is available in the popt stack */
5553 if (!service_opt && poptPeekArg(pc)) {
5554 service = talloc_strdup(frame,poptGetArg(pc));
5561 /* if the service has already been retrieved then check if we have also a password */
5563 && !get_cmdline_auth_info_got_pass(auth_info)
5564 && poptPeekArg(pc)) {
5565 set_cmdline_auth_info_password(auth_info,
5569 if ( override_logfile )
5570 setup_logging( lp_logfile(talloc_tos()), DEBUG_FILE );
5572 if (!lp_load_client(get_dyn_CONFIGFILE())) {
5573 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
5574 argv[0], get_dyn_CONFIGFILE());
5577 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
5578 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
5584 if (service_opt && service) {
5587 /* Convert any '/' characters in the service name to '\' characters */
5588 string_replace(service, '/','\\');
5589 if (count_chars(service,'\\') < 3) {
5590 d_printf("\n%s: Not enough '\\' characters in service\n",service);
5591 poptPrintUsage(pc, stderr, 0);
5594 /* Remove trailing slashes */
5595 len = strlen(service);
5596 while(len > 0 && service[len - 1] == '\\') {
5598 service[len] = '\0';
5602 smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
5603 if (!init_names()) {
5604 fprintf(stderr, "init_names() failed\n");
5608 if(new_name_resolve_order)
5609 lp_set_cmdline("name resolve order", new_name_resolve_order);
5611 if (!tar_type && !query_host && !service && !message) {
5612 poptPrintUsage(pc, stderr, 0);
5616 poptFreeContext(pc);
5617 popt_burn_cmdline_password(argc, argv);
5619 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5621 /* Ensure we have a password (or equivalent). */
5622 set_cmdline_auth_info_getpass(auth_info);
5624 max_protocol = lp_client_max_protocol();
5628 process_command_string(cmdstr);
5629 rc = do_tar_op(base_directory);
5630 } else if (query_host && *query_host) {
5631 char *qhost = query_host;
5634 while (*qhost == '\\' || *qhost == '/')
5637 if ((slash = strchr_m(qhost, '/'))
5638 || (slash = strchr_m(qhost, '\\'))) {
5642 if ((p=strchr_m(qhost, '#'))) {
5645 sscanf(p, "%x", &name_type);
5648 rc = do_host_query(qhost);
5649 } else if (message) {
5650 rc = do_message_op(auth_info);
5651 } else if (process(base_directory)) {