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"
42 #include "lib/util/xfile.h"
48 extern int do_smb_browse(void); /* mDNS browsing */
50 extern bool override_logfile;
54 static char *desthost;
55 static bool grepable = false;
56 static 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)) {
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 uint64_t total, bsize, avail;
303 struct cli_state *targetcli = NULL;
304 char *targetpath = NULL;
305 TALLOC_CTX *ctx = talloc_tos();
308 status = cli_resolve_path(ctx, "", auth_info, cli,
309 client_get_cur_dir(), &targetcli,
311 if (!NT_STATUS_IS_OK(status)) {
312 d_printf("Error in dskattr: %s\n", nt_errstr(status));
316 status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
317 if (!NT_STATUS_IS_OK(status)) {
318 d_printf("Error in dskattr: %s\n", nt_errstr(status));
322 d_printf("\n\t\t%" PRIu64
323 " blocks of size %" PRIu64
324 ". %" PRIu64 " blocks available\n",
325 total, bsize, avail);
330 /****************************************************************************
332 ****************************************************************************/
334 static int cmd_pwd(void)
336 d_printf("Current directory is %s",service);
337 d_printf("%s\n",client_get_cur_dir());
341 /****************************************************************************
342 Ensure name has correct directory separators.
343 ****************************************************************************/
345 static void normalize_name(char *newdir)
347 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
348 string_replace(newdir,'/','\\');
352 /****************************************************************************
353 Change directory - inner section.
354 ****************************************************************************/
356 static int do_cd(const char *new_dir)
359 char *saved_dir = NULL;
361 char *targetpath = NULL;
362 struct cli_state *targetcli = NULL;
363 SMB_STRUCT_STAT sbuf;
366 TALLOC_CTX *ctx = talloc_stackframe();
369 newdir = talloc_strdup(ctx, new_dir);
375 normalize_name(newdir);
377 /* Save the current directory in case the new directory is invalid */
379 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
385 if (*newdir == CLI_DIRSEP_CHAR) {
386 client_set_cur_dir(newdir);
389 new_cd = talloc_asprintf(ctx, "%s%s",
390 client_get_cur_dir(),
397 /* Ensure cur_dir ends in a DIRSEP */
398 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
399 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
404 client_set_cur_dir(new_cd);
406 new_cd = clean_name(ctx, new_cd);
407 client_set_cur_dir(new_cd);
409 status = cli_resolve_path(ctx, "", auth_info, cli, new_cd,
410 &targetcli, &targetpath);
411 if (!NT_STATUS_IS_OK(status)) {
412 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
413 client_set_cur_dir(saved_dir);
417 if (strequal(targetpath,CLI_DIRSEP_STR )) {
422 /* Use a trans2_qpathinfo to test directories for modern servers.
423 Except Win9x doesn't support the qpathinfo_basic() call..... */
425 if (smbXcli_conn_protocol(targetcli->conn) > PROTOCOL_LANMAN2 && !targetcli->win95) {
427 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
429 if (!NT_STATUS_IS_OK(status)) {
430 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
431 client_set_cur_dir(saved_dir);
435 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
436 d_printf("cd %s: not a directory\n", new_cd);
437 client_set_cur_dir(saved_dir);
442 targetpath = talloc_asprintf(ctx,
447 client_set_cur_dir(saved_dir);
450 targetpath = clean_name(ctx, targetpath);
452 client_set_cur_dir(saved_dir);
456 status = cli_chkpath(targetcli, targetpath);
457 if (!NT_STATUS_IS_OK(status)) {
458 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
459 client_set_cur_dir(saved_dir);
472 /****************************************************************************
474 ****************************************************************************/
476 static int cmd_cd(void)
481 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
484 d_printf("Current directory is %s\n",client_get_cur_dir());
490 /****************************************************************************
492 ****************************************************************************/
494 static int cmd_cd_oneup(void)
499 /*******************************************************************
500 Decide if a file should be operated on.
501 ********************************************************************/
503 static bool do_this_one(struct file_info *finfo)
509 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
513 if (*client_get_fileselection() &&
514 !mask_match(finfo->name,client_get_fileselection(),false)) {
515 DEBUG(3,("mask_match %s failed\n", finfo->name));
519 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
520 DEBUG(3,("newer_than %s failed\n", finfo->name));
524 if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
525 DEBUG(3,("archive %s failed\n", finfo->name));
532 /****************************************************************************
533 Display info about a file.
534 ****************************************************************************/
536 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
540 TALLOC_CTX *ctx = talloc_tos();
541 NTSTATUS status = NT_STATUS_OK;
543 if (!do_this_one(finfo)) {
547 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
549 d_printf(" %-30s%7.7s %8.0f %s",
551 attrib_string(talloc_tos(), finfo->mode),
554 dir_total += finfo->size;
559 /* skip if this is . or .. */
560 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
562 /* create absolute filename for cli_ntcreate() FIXME */
563 afname = talloc_asprintf(ctx,
569 return NT_STATUS_NO_MEMORY;
571 /* print file meta date header */
572 d_printf( "FILENAME:%s\n", finfo->name);
573 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->mode));
574 d_printf( "SIZE:%.0f\n", (double)finfo->size);
575 d_printf( "MTIME:%s", time_to_asc(t));
576 status = cli_ntcreate(cli_state, afname, 0,
577 CREATE_ACCESS_READ, 0,
578 FILE_SHARE_READ|FILE_SHARE_WRITE,
579 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
580 if (!NT_STATUS_IS_OK(status)) {
581 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
582 afname, nt_errstr(status)));
584 struct security_descriptor *sd = NULL;
585 status = cli_query_secdesc(cli_state, fnum,
587 if (!NT_STATUS_IS_OK(status)) {
588 DEBUG( 0, ("display_finfo() failed to "
589 "get security descriptor: %s",
592 display_sec_desc(sd);
601 /****************************************************************************
602 Accumulate size of a file.
603 ****************************************************************************/
605 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
608 if (do_this_one(finfo)) {
609 dir_total += finfo->size;
614 static bool do_list_recurse;
615 static bool do_list_dirs;
616 static char *do_list_queue = 0;
617 static long do_list_queue_size = 0;
618 static long do_list_queue_start = 0;
619 static long do_list_queue_end = 0;
620 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
623 /****************************************************************************
624 Functions for do_list_queue.
625 ****************************************************************************/
628 * The do_list_queue is a NUL-separated list of strings stored in a
629 * char*. Since this is a FIFO, we keep track of the beginning and
630 * ending locations of the data in the queue. When we overflow, we
631 * double the size of the char*. When the start of the data passes
632 * the midpoint, we move everything back. This is logically more
633 * complex than a linked list, but easier from a memory management
634 * angle. In any memory error condition, do_list_queue is reset.
635 * Functions check to ensure that do_list_queue is non-NULL before
639 static void reset_do_list_queue(void)
641 SAFE_FREE(do_list_queue);
642 do_list_queue_size = 0;
643 do_list_queue_start = 0;
644 do_list_queue_end = 0;
647 static void init_do_list_queue(void)
649 reset_do_list_queue();
650 do_list_queue_size = 1024;
651 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
652 if (do_list_queue == 0) {
653 d_printf("malloc fail for size %d\n",
654 (int)do_list_queue_size);
655 reset_do_list_queue();
657 memset(do_list_queue, 0, do_list_queue_size);
661 static void adjust_do_list_queue(void)
664 * If the starting point of the queue is more than half way through,
665 * move everything toward the beginning.
668 if (do_list_queue == NULL) {
669 DEBUG(4,("do_list_queue is empty\n"));
670 do_list_queue_start = do_list_queue_end = 0;
674 if (do_list_queue_start == do_list_queue_end) {
675 DEBUG(4,("do_list_queue is empty\n"));
676 do_list_queue_start = do_list_queue_end = 0;
677 *do_list_queue = '\0';
678 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
679 DEBUG(4,("sliding do_list_queue backward\n"));
680 memmove(do_list_queue,
681 do_list_queue + do_list_queue_start,
682 do_list_queue_end - do_list_queue_start);
683 do_list_queue_end -= do_list_queue_start;
684 do_list_queue_start = 0;
688 static void add_to_do_list_queue(const char *entry)
690 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
691 while (new_end > do_list_queue_size) {
692 do_list_queue_size *= 2;
693 DEBUG(4,("enlarging do_list_queue to %d\n",
694 (int)do_list_queue_size));
695 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
696 if (! do_list_queue) {
697 d_printf("failure enlarging do_list_queue to %d bytes\n",
698 (int)do_list_queue_size);
699 reset_do_list_queue();
701 memset(do_list_queue + do_list_queue_size / 2,
702 0, do_list_queue_size / 2);
706 strlcpy_base(do_list_queue + do_list_queue_end,
707 entry, do_list_queue, do_list_queue_size);
708 do_list_queue_end = new_end;
709 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
710 entry, (int)do_list_queue_start, (int)do_list_queue_end));
714 static char *do_list_queue_head(void)
716 return do_list_queue + do_list_queue_start;
719 static void remove_do_list_queue_head(void)
721 if (do_list_queue_end > do_list_queue_start) {
722 do_list_queue_start += strlen(do_list_queue_head()) + 1;
723 adjust_do_list_queue();
724 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
725 (int)do_list_queue_start, (int)do_list_queue_end));
729 static int do_list_queue_empty(void)
731 return (! (do_list_queue && *do_list_queue));
734 /****************************************************************************
735 A helper for do_list.
736 ****************************************************************************/
738 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
739 const char *mask, void *state)
741 struct cli_state *cli_state = (struct cli_state *)state;
742 TALLOC_CTX *ctx = talloc_tos();
744 char *dir_end = NULL;
745 NTSTATUS status = NT_STATUS_OK;
747 /* Work out the directory. */
748 dir = talloc_strdup(ctx, mask);
750 return NT_STATUS_NO_MEMORY;
752 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
756 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
757 if (do_list_dirs && do_this_one(f)) {
758 status = do_list_fn(cli_state, f, dir);
759 if (!NT_STATUS_IS_OK(status)) {
763 if (do_list_recurse &&
765 !strequal(f->name,".") &&
766 !strequal(f->name,"..")) {
771 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
773 return NT_STATUS_UNSUCCESSFUL;
776 mask2 = talloc_asprintf(ctx,
782 return NT_STATUS_NO_MEMORY;
784 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
790 mask2 = talloc_asprintf_append(mask2,
796 return NT_STATUS_NO_MEMORY;
798 add_to_do_list_queue(mask2);
805 if (do_this_one(f)) {
806 status = do_list_fn(cli_state, f, dir);
812 /****************************************************************************
813 A wrapper around cli_list that adds recursion.
814 ****************************************************************************/
816 NTSTATUS do_list(const char *mask,
818 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
823 static int in_do_list = 0;
824 TALLOC_CTX *ctx = talloc_tos();
825 struct cli_state *targetcli = NULL;
826 char *targetpath = NULL;
827 NTSTATUS ret_status = NT_STATUS_OK;
828 NTSTATUS status = NT_STATUS_OK;
830 if (in_do_list && rec) {
831 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
837 do_list_recurse = rec;
842 init_do_list_queue();
843 add_to_do_list_queue(mask);
845 while (!do_list_queue_empty()) {
847 * Need to copy head so that it doesn't become
848 * invalid inside the call to cli_list. This
849 * would happen if the list were expanded
851 * Fix from E. Jay Berkenbilt (ejb@ql.org)
853 char *head = talloc_strdup(ctx, do_list_queue_head());
856 return NT_STATUS_NO_MEMORY;
861 status = cli_resolve_path(ctx, "", auth_info, cli,
864 if (!NT_STATUS_IS_OK(status)) {
865 d_printf("do_list: [%s] %s\n", head,
867 remove_do_list_queue_head();
871 status = cli_list(targetcli, targetpath, attribute,
872 do_list_helper, targetcli);
873 if (!NT_STATUS_IS_OK(status)) {
874 d_printf("%s listing %s\n",
875 nt_errstr(status), targetpath);
878 remove_do_list_queue_head();
879 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
880 char *next_file = do_list_queue_head();
882 if ((strlen(next_file) >= 2) &&
883 (next_file[strlen(next_file) - 1] == '*') &&
884 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
885 save_ch = next_file +
886 strlen(next_file) - 2;
889 /* cwd is only used if showacls is on */
890 client_set_cwd(next_file);
893 if (!showacls) /* don't disturbe the showacls output */
894 d_printf("\n%s\n",next_file);
896 *save_ch = CLI_DIRSEP_CHAR;
900 TALLOC_FREE(targetpath);
904 status = cli_resolve_path(ctx, "", auth_info, cli, mask,
905 &targetcli, &targetpath);
906 if (NT_STATUS_IS_OK(status)) {
907 status = cli_list(targetcli, targetpath, attribute,
908 do_list_helper, targetcli);
909 if (!NT_STATUS_IS_OK(status)) {
910 d_printf("%s listing %s\n",
911 nt_errstr(status), targetpath);
914 TALLOC_FREE(targetpath);
916 d_printf("do_list: [%s] %s\n", mask, nt_errstr(status));
922 reset_do_list_queue();
926 /****************************************************************************
927 Get a directory listing.
928 ****************************************************************************/
930 static int cmd_dir(void)
932 TALLOC_CTX *ctx = talloc_tos();
933 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
940 mask = talloc_strdup(ctx, client_get_cur_dir());
945 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
947 if (*buf == CLI_DIRSEP_CHAR) {
948 mask = talloc_strdup(ctx, buf);
950 mask = talloc_asprintf_append(mask, "%s", buf);
953 mask = talloc_asprintf_append(mask, "*");
960 /* cwd is only used if showacls is on */
961 client_set_cwd(client_get_cur_dir());
964 status = do_list(mask, attribute, display_finfo, recurse, true);
965 if (!NT_STATUS_IS_OK(status)) {
971 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
976 /****************************************************************************
977 Get a directory listing.
978 ****************************************************************************/
980 static int cmd_du(void)
982 TALLOC_CTX *ctx = talloc_tos();
983 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
990 mask = talloc_strdup(ctx, client_get_cur_dir());
994 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
995 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
1001 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1002 normalize_name(buf);
1003 if (*buf == CLI_DIRSEP_CHAR) {
1004 mask = talloc_strdup(ctx, buf);
1006 mask = talloc_asprintf_append(mask, "%s", buf);
1009 mask = talloc_strdup(ctx, "*");
1012 status = do_list(mask, attribute, do_du, recurse, true);
1013 if (!NT_STATUS_IS_OK(status)) {
1019 d_printf("Total number of bytes: %.0f\n", dir_total);
1024 static int cmd_echo(void)
1026 TALLOC_CTX *ctx = talloc_tos();
1031 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1032 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1033 d_printf("echo <num> <data>\n");
1037 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1039 if (!NT_STATUS_IS_OK(status)) {
1040 d_printf("echo failed: %s\n", nt_errstr(status));
1047 /****************************************************************************
1048 Get a file from rname to lname
1049 ****************************************************************************/
1051 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1053 int *pfd = (int *)priv;
1054 if (writefile(*pfd, buf, n) == -1) {
1055 return map_nt_error_from_unix(errno);
1057 return NT_STATUS_OK;
1060 static int do_get(const char *rname, const char *lname_in, bool reget)
1062 TALLOC_CTX *ctx = talloc_tos();
1065 bool newhandle = false;
1066 struct timespec tp_start;
1072 struct cli_state *targetcli = NULL;
1073 char *targetname = NULL;
1077 lname = talloc_strdup(ctx, lname_in);
1083 if (!strlower_m(lname)) {
1084 d_printf("strlower_m %s failed\n", lname);
1089 status = cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli,
1091 if (!NT_STATUS_IS_OK(status)) {
1092 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1096 clock_gettime_mono(&tp_start);
1098 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 d_printf("%s opening remote file %s\n", nt_errstr(status),
1105 if(!strcmp(lname,"-")) {
1106 handle = fileno(stdout);
1109 handle = open(lname, O_WRONLY|O_CREAT, 0644);
1111 start = lseek(handle, 0, SEEK_END);
1113 d_printf("Error seeking local file\n");
1118 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1123 d_printf("Error opening local file %s\n",lname);
1128 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1130 if (!NT_STATUS_IS_OK(status)) {
1131 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1133 if(!NT_STATUS_IS_OK(status)) {
1134 d_printf("getattrib: %s\n", nt_errstr(status));
1139 DEBUG(1,("getting file %s of size %.0f as %s ",
1140 rname, (double)size, lname));
1142 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1143 writefile_sink, (void *)&handle, &nread);
1144 if (!NT_STATUS_IS_OK(status)) {
1145 d_fprintf(stderr, "parallel_read returned %s\n",
1147 cli_close(targetcli, fnum);
1151 status = cli_close(targetcli, fnum);
1152 if (!NT_STATUS_IS_OK(status)) {
1153 d_printf("Error %s closing remote file\n", nt_errstr(status));
1161 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1162 cli_setatr(cli, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
1166 struct timespec tp_end;
1169 clock_gettime_mono(&tp_end);
1170 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1171 get_total_time_ms += this_time;
1172 get_total_size += nread;
1174 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1175 nread / (1.024*this_time + 1.0e-4),
1176 get_total_size / (1.024*get_total_time_ms)));
1179 TALLOC_FREE(targetname);
1183 /****************************************************************************
1185 ****************************************************************************/
1187 static int cmd_get(void)
1189 TALLOC_CTX *ctx = talloc_tos();
1194 rname = talloc_strdup(ctx, client_get_cur_dir());
1199 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1200 d_printf("get <filename> [localname]\n");
1203 rname = talloc_asprintf_append(rname, "%s", fname);
1207 rname = clean_name(ctx, rname);
1212 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1217 return do_get(rname, lname, false);
1220 /****************************************************************************
1221 Do an mget operation on one file.
1222 ****************************************************************************/
1224 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1227 TALLOC_CTX *ctx = talloc_tos();
1228 NTSTATUS status = NT_STATUS_OK;
1231 char *saved_curdir = NULL;
1232 char *mget_mask = NULL;
1233 char *new_cd = NULL;
1236 return NT_STATUS_OK;
1239 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1240 return NT_STATUS_OK;
1243 d_printf("mget aborted\n");
1244 return NT_STATUS_UNSUCCESSFUL;
1247 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
1248 if (asprintf(&quest,
1249 "Get directory %s? ",finfo->name) < 0) {
1250 return NT_STATUS_NO_MEMORY;
1253 if (asprintf(&quest,
1254 "Get file %s? ",finfo->name) < 0) {
1255 return NT_STATUS_NO_MEMORY;
1259 if (prompt && !yesno(quest)) {
1261 return NT_STATUS_OK;
1265 if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
1266 rname = talloc_asprintf(ctx,
1268 client_get_cur_dir(),
1271 return NT_STATUS_NO_MEMORY;
1273 do_get(rname, finfo->name, false);
1275 return NT_STATUS_OK;
1278 /* handle directories */
1279 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1280 if (!saved_curdir) {
1281 return NT_STATUS_NO_MEMORY;
1284 new_cd = talloc_asprintf(ctx,
1286 client_get_cur_dir(),
1290 return NT_STATUS_NO_MEMORY;
1292 client_set_cur_dir(new_cd);
1294 string_replace(finfo->name,'\\','/');
1296 if (!strlower_m(finfo->name)) {
1297 return NT_STATUS_INVALID_PARAMETER;
1301 if (!directory_exist(finfo->name) &&
1302 mkdir(finfo->name,0777) != 0) {
1303 d_printf("failed to create directory %s\n",finfo->name);
1304 client_set_cur_dir(saved_curdir);
1305 return map_nt_error_from_unix(errno);
1308 if (chdir(finfo->name) != 0) {
1309 d_printf("failed to chdir to directory %s\n",finfo->name);
1310 client_set_cur_dir(saved_curdir);
1311 return map_nt_error_from_unix(errno);
1314 mget_mask = talloc_asprintf(ctx,
1316 client_get_cur_dir());
1319 return NT_STATUS_NO_MEMORY;
1322 status = do_list(mget_mask,
1323 (FILE_ATTRIBUTE_SYSTEM
1324 | FILE_ATTRIBUTE_HIDDEN
1325 | FILE_ATTRIBUTE_DIRECTORY),
1326 do_mget, false, true);
1327 if (!NT_STATUS_IS_OK(status)
1328 && !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1330 * Ignore access denied errors to ensure all permitted files are
1336 if (chdir("..") == -1) {
1337 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1339 return map_nt_error_from_unix(errno);
1341 client_set_cur_dir(saved_curdir);
1342 TALLOC_FREE(mget_mask);
1343 TALLOC_FREE(saved_curdir);
1344 TALLOC_FREE(new_cd);
1345 return NT_STATUS_OK;
1348 /****************************************************************************
1349 View the file using the pager.
1350 ****************************************************************************/
1352 static int cmd_more(void)
1354 TALLOC_CTX *ctx = talloc_tos();
1358 char *pager_cmd = NULL;
1364 rname = talloc_strdup(ctx, client_get_cur_dir());
1369 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1373 mask = umask(S_IRWXO | S_IRWXG);
1374 fd = mkstemp(lname);
1377 d_printf("failed to create temporary file for more\n");
1382 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1383 d_printf("more <filename>\n");
1387 rname = talloc_asprintf_append(rname, "%s", fname);
1391 rname = clean_name(ctx,rname);
1396 rc = do_get(rname, lname, false);
1398 pager=getenv("PAGER");
1400 pager_cmd = talloc_asprintf(ctx,
1402 (pager? pager:PAGER),
1407 if (system(pager_cmd) == -1) {
1408 d_printf("system command '%s' returned -1\n",
1416 /****************************************************************************
1418 ****************************************************************************/
1420 static int cmd_mget(void)
1422 TALLOC_CTX *ctx = talloc_tos();
1423 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1424 char *mget_mask = NULL;
1426 NTSTATUS status = NT_STATUS_OK;
1429 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1434 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1436 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1440 if (*buf == CLI_DIRSEP_CHAR) {
1441 mget_mask = talloc_strdup(ctx, buf);
1443 mget_mask = talloc_asprintf_append(mget_mask,
1449 status = do_list(mget_mask, attribute, do_mget, false, true);
1450 if (!NT_STATUS_IS_OK(status)) {
1455 if (mget_mask == NULL) {
1456 d_printf("nothing to mget\n");
1461 mget_mask = talloc_asprintf(ctx,
1463 client_get_cur_dir());
1467 status = do_list(mget_mask, attribute, do_mget, false, true);
1468 if (!NT_STATUS_IS_OK(status)) {
1476 /****************************************************************************
1477 Make a directory of name "name".
1478 ****************************************************************************/
1480 static bool do_mkdir(const char *name)
1482 TALLOC_CTX *ctx = talloc_tos();
1483 struct cli_state *targetcli;
1484 char *targetname = NULL;
1487 status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
1489 if (!NT_STATUS_IS_OK(status)) {
1490 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1494 status = cli_mkdir(targetcli, targetname);
1495 if (!NT_STATUS_IS_OK(status)) {
1496 d_printf("%s making remote directory %s\n",
1497 nt_errstr(status),name);
1504 /****************************************************************************
1505 Show 8.3 name of a file.
1506 ****************************************************************************/
1508 static bool do_altname(const char *name)
1513 status = cli_qpathinfo_alt_name(cli, name, altname);
1514 if (!NT_STATUS_IS_OK(status)) {
1515 d_printf("%s getting alt name for %s\n",
1516 nt_errstr(status),name);
1519 d_printf("%s\n", altname);
1524 /****************************************************************************
1526 ****************************************************************************/
1528 static int cmd_quit(void)
1536 /****************************************************************************
1538 ****************************************************************************/
1540 static int cmd_mkdir(void)
1542 TALLOC_CTX *ctx = talloc_tos();
1547 mask = talloc_strdup(ctx, client_get_cur_dir());
1552 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1554 d_printf("mkdir <dirname>\n");
1558 mask = talloc_asprintf_append(mask, "%s", buf);
1566 struct cli_state *targetcli;
1567 char *targetname = NULL;
1571 ddir2 = talloc_strdup(ctx, "");
1576 status = cli_resolve_path(ctx, "", auth_info, cli, mask,
1577 &targetcli, &targetname);
1578 if (!NT_STATUS_IS_OK(status)) {
1582 ddir = talloc_strdup(ctx, targetname);
1586 trim_char(ddir,'.','\0');
1587 p = strtok_r(ddir, "/\\", &saveptr);
1589 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1593 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1596 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1600 p = strtok_r(NULL, "/\\", &saveptr);
1609 /****************************************************************************
1611 ****************************************************************************/
1613 static int cmd_altname(void)
1615 TALLOC_CTX *ctx = talloc_tos();
1619 name = talloc_strdup(ctx, client_get_cur_dir());
1624 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1625 d_printf("altname <file>\n");
1628 name = talloc_asprintf_append(name, "%s", buf);
1636 static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1638 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1641 if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1642 if (mode & FILE_ATTRIBUTE_ENCRYPTED) {
1645 if (mode & FILE_ATTRIBUTE_NONINDEXED) {
1648 if (mode & FILE_ATTRIBUTE_OFFLINE) {
1651 if (mode & FILE_ATTRIBUTE_COMPRESSED) {
1654 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1657 if (mode & FILE_ATTRIBUTE_SPARSE) {
1660 if (mode & FILE_ATTRIBUTE_TEMPORARY) {
1663 if (mode & FILE_ATTRIBUTE_NORMAL) {
1666 if (mode & FILE_ATTRIBUTE_READONLY) {
1669 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1672 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1675 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1678 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1685 /****************************************************************************
1686 Show all info we can get
1687 ****************************************************************************/
1689 static int do_allinfo(const char *name)
1692 struct timespec b_time, a_time, m_time, c_time;
1697 unsigned int num_streams;
1698 struct stream_struct *streams;
1700 char **snapshots = NULL;
1704 status = cli_qpathinfo_alt_name(cli, name, altname);
1705 if (!NT_STATUS_IS_OK(status)) {
1706 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1709 * Ignore not supported or not implemented, it does not
1710 * hurt if we can't list alternate names.
1712 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1713 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1719 d_printf("altname: %s\n", altname);
1721 status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1722 &size, &mode, NULL);
1723 if (!NT_STATUS_IS_OK(status)) {
1724 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1729 tmp = unix_timespec_to_nt_time(b_time);
1730 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1732 tmp = unix_timespec_to_nt_time(a_time);
1733 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1735 tmp = unix_timespec_to_nt_time(m_time);
1736 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1738 tmp = unix_timespec_to_nt_time(c_time);
1739 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1741 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
1743 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1745 if (!NT_STATUS_IS_OK(status)) {
1746 d_printf("%s getting streams for %s\n", nt_errstr(status),
1751 for (i=0; i<num_streams; i++) {
1752 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1753 (unsigned long long)streams[i].size);
1756 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1757 char *subst, *print;
1760 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1762 if (!NT_STATUS_IS_OK(status)) {
1763 d_fprintf(stderr, "cli_readlink returned %s\n",
1766 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1767 subst, print, flags);
1773 status = cli_ntcreate(cli, name, 0,
1774 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1775 SEC_STD_SYNCHRONIZE, 0,
1776 FILE_SHARE_READ|FILE_SHARE_WRITE
1778 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
1779 if (!NT_STATUS_IS_OK(status)) {
1781 * Ignore failure, it does not hurt if we can't list
1787 * In order to get shadow copy data over SMB1 we
1788 * must call twice, once with 'get_names = false'
1789 * to get the size, then again with 'get_names = true'
1790 * to get the data or a Windows server fails to return
1791 * valid info. Samba doesn't have this bug. JRA.
1794 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1795 false, &snapshots, &num_snapshots);
1796 if (!NT_STATUS_IS_OK(status)) {
1797 cli_close(cli, fnum);
1800 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1801 true, &snapshots, &num_snapshots);
1802 if (!NT_STATUS_IS_OK(status)) {
1803 cli_close(cli, fnum);
1807 for (i=0; i<num_snapshots; i++) {
1810 d_printf("%s\n", snapshots[i]);
1811 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1812 snapshots[i], name);
1813 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1814 &m_time, &c_time, &size,
1816 if (!NT_STATUS_IS_OK(status)) {
1817 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1818 snap_name, nt_errstr(status));
1819 TALLOC_FREE(snap_name);
1822 tmp = unix_timespec_to_nt_time(b_time);
1823 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1824 tmp = unix_timespec_to_nt_time(a_time);
1825 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1826 tmp =unix_timespec_to_nt_time(m_time);
1827 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1828 tmp = unix_timespec_to_nt_time(c_time);
1829 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1830 d_printf("size: %d\n", (int)size);
1833 TALLOC_FREE(snapshots);
1834 cli_close(cli, fnum);
1839 /****************************************************************************
1840 Show all info we can get
1841 ****************************************************************************/
1843 static int cmd_allinfo(void)
1845 TALLOC_CTX *ctx = talloc_tos();
1849 name = talloc_strdup(ctx, client_get_cur_dir());
1854 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1855 d_printf("allinfo <file>\n");
1858 name = talloc_asprintf_append(name, "%s", buf);
1868 /****************************************************************************
1870 ****************************************************************************/
1872 static int do_put(const char *rname, const char *lname, bool reput)
1874 TALLOC_CTX *ctx = talloc_tos();
1879 struct timespec tp_start;
1880 struct cli_state *targetcli;
1881 char *targetname = NULL;
1882 struct push_state state;
1885 status = cli_resolve_path(ctx, "", auth_info, cli, rname,
1886 &targetcli, &targetname);
1887 if (!NT_STATUS_IS_OK(status)) {
1888 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1892 clock_gettime_mono(&tp_start);
1895 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1896 if (NT_STATUS_IS_OK(status)) {
1897 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1898 targetcli, fnum, NULL,
1900 NULL, NULL, NULL)) &&
1901 !NT_STATUS_IS_OK(status = cli_getattrE(
1902 targetcli, fnum, NULL,
1905 d_printf("getattrib: %s\n", nt_errstr(status));
1910 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1913 if (!NT_STATUS_IS_OK(status)) {
1914 d_printf("%s opening remote file %s\n", nt_errstr(status),
1919 /* allow files to be piped into smbclient
1922 Note that in this case this function will exit(0) rather
1924 if (!strcmp(lname, "-")) {
1926 /* size of file is not known */
1928 f = x_fopen(lname,O_RDONLY, 0);
1930 if (x_tseek(f, start, SEEK_SET) == -1) {
1931 d_printf("Error seeking local file\n");
1939 d_printf("Error opening local file %s\n",lname);
1943 DEBUG(1,("putting file %s as %s ",lname,
1946 x_setvbuf(f, NULL, X_IOFBF, io_bufsize);
1951 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1953 if (!NT_STATUS_IS_OK(status)) {
1954 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1958 status = cli_close(targetcli, fnum);
1959 if (!NT_STATUS_IS_OK(status)) {
1960 d_printf("%s closing remote file %s\n", nt_errstr(status),
1973 struct timespec tp_end;
1976 clock_gettime_mono(&tp_end);
1977 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1978 put_total_time_ms += this_time;
1979 put_total_size += state.nread;
1981 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1982 state.nread / (1.024*this_time + 1.0e-4),
1983 put_total_size / (1.024*put_total_time_ms)));
1994 /****************************************************************************
1996 ****************************************************************************/
1998 static int cmd_put(void)
2000 TALLOC_CTX *ctx = talloc_tos();
2005 rname = talloc_strdup(ctx, client_get_cur_dir());
2010 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
2011 d_printf("put <filename>\n");
2015 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2016 rname = talloc_asprintf_append(rname, "%s", buf);
2018 rname = talloc_asprintf_append(rname, "%s", lname);
2024 rname = clean_name(ctx, rname);
2031 /* allow '-' to represent stdin
2032 jdblair, 24.jun.98 */
2033 if (!file_exist_stat(lname, &st, false) &&
2034 (strcmp(lname,"-"))) {
2035 d_printf("%s does not exist\n",lname);
2040 return do_put(rname, lname, false);
2043 /*************************************
2044 File list structure.
2045 *************************************/
2047 static struct file_list {
2048 struct file_list *prev, *next;
2053 /****************************************************************************
2054 Free a file_list structure.
2055 ****************************************************************************/
2057 static void free_file_list (struct file_list *l_head)
2059 struct file_list *list, *next;
2061 for (list = l_head; list; list = next) {
2063 DLIST_REMOVE(l_head, list);
2064 SAFE_FREE(list->file_path);
2069 /****************************************************************************
2070 Seek in a directory/file list until you get something that doesn't start with
2072 ****************************************************************************/
2074 static bool seek_list(struct file_list *list, char *name)
2077 trim_string(list->file_path,"./","\n");
2078 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2087 /****************************************************************************
2088 Set the file selection mask.
2089 ****************************************************************************/
2091 static int cmd_select(void)
2093 TALLOC_CTX *ctx = talloc_tos();
2094 char *new_fs = NULL;
2095 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2098 client_set_fileselection(new_fs);
2100 client_set_fileselection("");
2105 /****************************************************************************
2106 Recursive file matching function act as find
2107 match must be always set to true when calling this function
2108 ****************************************************************************/
2110 static int file_find(struct file_list **list, const char *directory,
2111 const char *expression, bool match)
2114 struct file_list *entry;
2115 struct stat statbuf;
2121 dir = opendir(directory);
2125 while ((dname = readdirname(dir))) {
2126 if (!strcmp("..", dname))
2128 if (!strcmp(".", dname))
2131 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
2136 if (!match || !gen_fnmatch(expression, dname)) {
2138 ret = stat(path, &statbuf);
2140 if (S_ISDIR(statbuf.st_mode)) {
2142 ret = file_find(list, path, expression, false);
2145 d_printf("file_find: cannot stat file %s\n", path);
2154 entry = SMB_MALLOC_P(struct file_list);
2156 d_printf("Out of memory in file_find\n");
2160 entry->file_path = path;
2161 entry->isdir = isdir;
2162 DLIST_ADD(*list, entry);
2172 /****************************************************************************
2174 ****************************************************************************/
2176 static int cmd_mput(void)
2178 TALLOC_CTX *ctx = talloc_tos();
2181 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2183 struct file_list *temp_list;
2184 char *quest, *lname, *rname;
2188 ret = file_find(&file_list, ".", p, true);
2190 free_file_list(file_list);
2198 for (temp_list = file_list; temp_list;
2199 temp_list = temp_list->next) {
2202 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2205 trim_string(lname, "./", "/");
2207 /* check if it's a directory */
2208 if (temp_list->isdir) {
2209 /* if (!recurse) continue; */
2212 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2215 if (prompt && !yesno(quest)) { /* No */
2216 /* Skip the directory */
2217 lname[strlen(lname)-1] = '/';
2218 if (!seek_list(temp_list, lname))
2222 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2225 normalize_name(rname);
2226 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2228 DEBUG (0, ("Unable to make dir, skipping..."));
2229 /* Skip the directory */
2230 lname[strlen(lname)-1] = '/';
2231 if (!seek_list(temp_list, lname)) {
2239 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2242 if (prompt && !yesno(quest)) {
2249 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2254 normalize_name(rname);
2256 do_put(rname, lname, false);
2258 free_file_list(file_list);
2267 /****************************************************************************
2269 ****************************************************************************/
2271 static int do_cancel(int job)
2273 if (cli_printjob_del(cli, job)) {
2274 d_printf("Job %d cancelled\n",job);
2277 NTSTATUS status = cli_nt_error(cli);
2278 d_printf("Error cancelling job %d : %s\n",
2279 job, nt_errstr(status));
2284 /****************************************************************************
2286 ****************************************************************************/
2288 static int cmd_cancel(void)
2290 TALLOC_CTX *ctx = talloc_tos();
2294 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2295 d_printf("cancel <jobid> ...\n");
2301 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2306 /****************************************************************************
2308 ****************************************************************************/
2310 static int cmd_print(void)
2312 TALLOC_CTX *ctx = talloc_tos();
2317 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2318 d_printf("print <filename>\n");
2322 rname = talloc_strdup(ctx, lname);
2326 p = strrchr_m(rname,'/');
2328 rname = talloc_asprintf(ctx,
2333 if (strequal(lname,"-")) {
2334 rname = talloc_asprintf(ctx,
2342 return do_put(rname, lname, false);
2345 /****************************************************************************
2346 Show a print queue entry.
2347 ****************************************************************************/
2349 static void queue_fn(struct print_job_info *p)
2351 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2354 /****************************************************************************
2356 ****************************************************************************/
2358 static int cmd_queue(void)
2360 cli_print_queue(cli, queue_fn);
2364 /****************************************************************************
2366 ****************************************************************************/
2368 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2371 TALLOC_CTX *ctx = talloc_tos();
2375 mask = talloc_asprintf(ctx,
2381 return NT_STATUS_NO_MEMORY;
2384 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2386 return NT_STATUS_OK;
2389 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2390 if (!NT_STATUS_IS_OK(status)) {
2391 d_printf("%s deleting remote file %s\n",
2392 nt_errstr(status), mask);
2398 /****************************************************************************
2400 ****************************************************************************/
2402 static int cmd_del(void)
2404 TALLOC_CTX *ctx = talloc_tos();
2407 NTSTATUS status = NT_STATUS_OK;
2408 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2411 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2414 mask = talloc_strdup(ctx, client_get_cur_dir());
2418 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2419 d_printf("del <filename>\n");
2422 mask = talloc_asprintf_append(mask, "%s", buf);
2427 status = do_list(mask,attribute,do_del,false,false);
2428 if (!NT_STATUS_IS_OK(status)) {
2434 /****************************************************************************
2435 Wildcard delete some files.
2436 ****************************************************************************/
2438 static int cmd_wdel(void)
2440 TALLOC_CTX *ctx = talloc_tos();
2444 struct cli_state *targetcli;
2445 char *targetname = NULL;
2448 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2449 d_printf("wdel 0x<attrib> <wcard>\n");
2453 attribute = (uint16_t)strtol(buf, (char **)NULL, 16);
2455 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2456 d_printf("wdel 0x<attrib> <wcard>\n");
2460 mask = talloc_asprintf(ctx, "%s%s",
2461 client_get_cur_dir(),
2467 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2469 if (!NT_STATUS_IS_OK(status)) {
2470 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2474 status = cli_unlink(targetcli, targetname, attribute);
2475 if (!NT_STATUS_IS_OK(status)) {
2476 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2482 /****************************************************************************
2483 ****************************************************************************/
2485 static int cmd_open(void)
2487 TALLOC_CTX *ctx = talloc_tos();
2490 char *targetname = NULL;
2491 struct cli_state *targetcli;
2492 uint16_t fnum = (uint16_t)-1;
2495 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2496 d_printf("open <filename>\n");
2499 mask = talloc_asprintf(ctx,
2501 client_get_cur_dir(),
2507 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2509 if (!NT_STATUS_IS_OK(status)) {
2510 d_printf("open %s: %s\n", mask, nt_errstr(status));
2514 status = cli_ntcreate(targetcli, targetname, 0,
2515 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2516 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2517 0x0, 0x0, &fnum, NULL);
2518 if (!NT_STATUS_IS_OK(status)) {
2519 status = cli_ntcreate(targetcli, targetname, 0,
2521 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2522 0x0, 0x0, &fnum, NULL);
2523 if (NT_STATUS_IS_OK(status)) {
2524 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2526 d_printf("Failed to open file %s. %s\n",
2527 targetname, nt_errstr(status));
2530 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2535 static int cmd_posix_encrypt(void)
2537 TALLOC_CTX *ctx = talloc_tos();
2538 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2540 if (cli->use_kerberos) {
2541 status = cli_gss_smb_encryption_start(cli);
2543 char *domain = NULL;
2545 char *password = NULL;
2547 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2548 d_printf("posix_encrypt domain user password\n");
2552 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2553 d_printf("posix_encrypt domain user password\n");
2557 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2558 d_printf("posix_encrypt domain user password\n");
2562 status = cli_raw_ntlm_smb_encryption_start(cli,
2568 if (!NT_STATUS_IS_OK(status)) {
2569 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2571 d_printf("encryption on\n");
2578 /****************************************************************************
2579 ****************************************************************************/
2581 static int cmd_posix_open(void)
2583 TALLOC_CTX *ctx = talloc_tos();
2586 char *targetname = NULL;
2587 struct cli_state *targetcli;
2592 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2593 d_printf("posix_open <filename> 0<mode>\n");
2596 mask = talloc_asprintf(ctx,
2598 client_get_cur_dir(),
2604 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2605 d_printf("posix_open <filename> 0<mode>\n");
2608 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2610 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2612 if (!NT_STATUS_IS_OK(status)) {
2613 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2617 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2619 if (!NT_STATUS_IS_OK(status)) {
2620 status = cli_posix_open(targetcli, targetname,
2621 O_CREAT|O_RDONLY, mode, &fnum);
2622 if (!NT_STATUS_IS_OK(status)) {
2623 d_printf("Failed to open file %s. %s\n", targetname,
2626 d_printf("posix_open file %s: for readonly fnum %d\n",
2630 d_printf("posix_open file %s: for read/write fnum %d\n",
2637 static int cmd_posix_mkdir(void)
2639 TALLOC_CTX *ctx = talloc_tos();
2642 char *targetname = NULL;
2643 struct cli_state *targetcli;
2647 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2648 d_printf("posix_mkdir <filename> 0<mode>\n");
2651 mask = talloc_asprintf(ctx,
2653 client_get_cur_dir(),
2659 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2660 d_printf("posix_mkdir <filename> 0<mode>\n");
2663 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2665 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2667 if (!NT_STATUS_IS_OK(status)) {
2668 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2672 status = cli_posix_mkdir(targetcli, targetname, mode);
2673 if (!NT_STATUS_IS_OK(status)) {
2674 d_printf("Failed to open file %s. %s\n",
2675 targetname, nt_errstr(status));
2677 d_printf("posix_mkdir created directory %s\n", targetname);
2682 static int cmd_posix_unlink(void)
2684 TALLOC_CTX *ctx = talloc_tos();
2687 char *targetname = NULL;
2688 struct cli_state *targetcli;
2691 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2692 d_printf("posix_unlink <filename>\n");
2695 mask = talloc_asprintf(ctx,
2697 client_get_cur_dir(),
2703 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2705 if (!NT_STATUS_IS_OK(status)) {
2706 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
2710 status = cli_posix_unlink(targetcli, targetname);
2711 if (!NT_STATUS_IS_OK(status)) {
2712 d_printf("Failed to unlink file %s. %s\n",
2713 targetname, nt_errstr(status));
2715 d_printf("posix_unlink deleted file %s\n", targetname);
2721 static int cmd_posix_rmdir(void)
2723 TALLOC_CTX *ctx = talloc_tos();
2726 char *targetname = NULL;
2727 struct cli_state *targetcli;
2730 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2731 d_printf("posix_rmdir <filename>\n");
2734 mask = talloc_asprintf(ctx,
2736 client_get_cur_dir(),
2742 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
2744 if (!NT_STATUS_IS_OK(status)) {
2745 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
2749 status = cli_posix_rmdir(targetcli, targetname);
2750 if (!NT_STATUS_IS_OK(status)) {
2751 d_printf("Failed to unlink directory %s. %s\n",
2752 targetname, nt_errstr(status));
2754 d_printf("posix_rmdir deleted directory %s\n", targetname);
2760 static int cmd_close(void)
2762 TALLOC_CTX *ctx = talloc_tos();
2767 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2768 d_printf("close <fnum>\n");
2773 /* We really should use the targetcli here.... */
2774 status = cli_close(cli, fnum);
2775 if (!NT_STATUS_IS_OK(status)) {
2776 d_printf("close %d: %s\n", fnum, nt_errstr(status));
2782 static int cmd_posix(void)
2784 TALLOC_CTX *ctx = talloc_tos();
2785 uint16_t major, minor;
2786 uint32_t caplow, caphigh;
2790 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2791 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2795 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
2797 if (!NT_STATUS_IS_OK(status)) {
2798 d_printf("Can't get UNIX CIFS extensions version from "
2799 "server: %s\n", nt_errstr(status));
2803 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2805 caps = talloc_strdup(ctx, "");
2809 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2810 caps = talloc_asprintf_append(caps, "locks ");
2815 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2816 caps = talloc_asprintf_append(caps, "acls ");
2821 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2822 caps = talloc_asprintf_append(caps, "eas ");
2827 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2828 caps = talloc_asprintf_append(caps, "pathnames ");
2833 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2834 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2839 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2840 caps = talloc_asprintf_append(caps, "large_read ");
2845 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2846 caps = talloc_asprintf_append(caps, "large_write ");
2851 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2852 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2857 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2858 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2864 if (*caps && caps[strlen(caps)-1] == ' ') {
2865 caps[strlen(caps)-1] = '\0';
2868 d_printf("Server supports CIFS capabilities %s\n", caps);
2870 status = cli_set_unix_extensions_capabilities(cli, major, minor,
2872 if (!NT_STATUS_IS_OK(status)) {
2873 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2878 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2879 CLI_DIRSEP_CHAR = '/';
2880 *CLI_DIRSEP_STR = '/';
2881 client_set_cur_dir(CLI_DIRSEP_STR);
2887 static int cmd_lock(void)
2889 TALLOC_CTX *ctx = talloc_tos();
2891 uint64_t start, len;
2892 enum brl_type lock_type;
2896 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2897 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2902 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2903 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2907 if (*buf == 'r' || *buf == 'R') {
2908 lock_type = READ_LOCK;
2909 } else if (*buf == 'w' || *buf == 'W') {
2910 lock_type = WRITE_LOCK;
2912 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2916 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2917 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2921 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2923 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2924 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2928 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2930 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
2931 if (!NT_STATUS_IS_OK(status)) {
2932 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
2938 static int cmd_unlock(void)
2940 TALLOC_CTX *ctx = talloc_tos();
2942 uint64_t start, len;
2946 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2947 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2952 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2953 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2957 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2959 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2960 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2964 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2966 status = cli_posix_unlock(cli, fnum, start, len);
2967 if (!NT_STATUS_IS_OK(status)) {
2968 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
2974 static int cmd_posix_whoami(void)
2976 TALLOC_CTX *ctx = talloc_tos();
2977 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2980 uint32_t num_gids = 0;
2981 uint32_t num_sids = 0;
2982 uint64_t *gids = NULL;
2983 struct dom_sid *sids = NULL;
2987 status = cli_posix_whoami(cli,
2997 if (!NT_STATUS_IS_OK(status)) {
2998 d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
3002 d_printf("GUEST:%s\n", guest ? "True" : "False");
3003 d_printf("UID:%" PRIu64 "\n", uid);
3004 d_printf("GID:%" PRIu64 "\n", gid);
3005 d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
3006 for (i = 0; i < num_gids; i++) {
3007 d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
3009 d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
3010 for (i = 0; i < num_sids; i++) {
3011 char *sid_str = dom_sid_string(ctx, &sids[i]);
3012 d_printf("SIDS[%" PRIu32 "]:%s\n", i, sid_str);
3013 TALLOC_FREE(sid_str);
3019 /****************************************************************************
3021 ****************************************************************************/
3023 static int cmd_rmdir(void)
3025 TALLOC_CTX *ctx = talloc_tos();
3028 char *targetname = NULL;
3029 struct cli_state *targetcli;
3032 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3033 d_printf("rmdir <dirname>\n");
3036 mask = talloc_asprintf(ctx,
3038 client_get_cur_dir(),
3044 status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
3046 if (!NT_STATUS_IS_OK(status)) {
3047 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3051 status = cli_rmdir(targetcli, targetname);
3052 if (!NT_STATUS_IS_OK(status)) {
3053 d_printf("%s removing remote directory file %s\n",
3054 nt_errstr(status), mask);
3060 /****************************************************************************
3062 ****************************************************************************/
3064 static int cmd_link(void)
3066 TALLOC_CTX *ctx = talloc_tos();
3067 char *oldname = NULL;
3068 char *newname = NULL;
3071 char *targetname = NULL;
3072 struct cli_state *targetcli;
3075 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3076 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3077 d_printf("link <oldname> <newname>\n");
3080 oldname = talloc_asprintf(ctx,
3082 client_get_cur_dir(),
3087 newname = talloc_asprintf(ctx,
3089 client_get_cur_dir(),
3095 status = cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli,
3097 if (!NT_STATUS_IS_OK(status)) {
3098 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3102 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3103 d_printf("Server doesn't support UNIX CIFS calls.\n");
3107 status = cli_posix_hardlink(targetcli, targetname, newname);
3108 if (!NT_STATUS_IS_OK(status)) {
3109 d_printf("%s linking files (%s -> %s)\n",
3110 nt_errstr(status), newname, oldname);
3116 /****************************************************************************
3118 ****************************************************************************/
3120 static int cmd_readlink(void)
3122 TALLOC_CTX *ctx = talloc_tos();
3125 char *targetname = NULL;
3126 char linkname[PATH_MAX+1];
3127 struct cli_state *targetcli;
3130 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3131 d_printf("readlink <name>\n");
3134 name = talloc_asprintf(ctx,
3136 client_get_cur_dir(),
3142 status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
3144 if (!NT_STATUS_IS_OK(status)) {
3145 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3149 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3150 d_printf("Server doesn't support UNIX CIFS calls.\n");
3154 status = cli_posix_readlink(targetcli, name, linkname, PATH_MAX+1);
3155 if (!NT_STATUS_IS_OK(status)) {
3156 d_printf("%s readlink on file %s\n",
3157 nt_errstr(status), name);
3161 d_printf("%s -> %s\n", name, linkname);
3167 /****************************************************************************
3169 ****************************************************************************/
3171 static int cmd_symlink(void)
3173 TALLOC_CTX *ctx = talloc_tos();
3174 char *oldname = NULL;
3175 char *newname = NULL;
3178 struct cli_state *newcli;
3181 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3182 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3183 d_printf("symlink <oldname> <newname>\n");
3186 /* Oldname (link target) must be an untouched blob. */
3189 if (SERVER_HAS_UNIX_CIFS(cli)) {
3190 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3195 /* New name must be present in share namespace. */
3196 status = cli_resolve_path(ctx, "", auth_info, cli, newname,
3198 if (!NT_STATUS_IS_OK(status)) {
3199 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3202 status = cli_posix_symlink(newcli, oldname, newname);
3204 status = cli_symlink(
3206 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3209 if (!NT_STATUS_IS_OK(status)) {
3210 d_printf("%s symlinking files (%s -> %s)\n",
3211 nt_errstr(status), oldname, newname);
3218 /****************************************************************************
3220 ****************************************************************************/
3222 static int cmd_chmod(void)
3224 TALLOC_CTX *ctx = talloc_tos();
3228 char *targetname = NULL;
3229 struct cli_state *targetcli;
3233 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3234 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3235 d_printf("chmod mode file\n");
3238 src = talloc_asprintf(ctx,
3240 client_get_cur_dir(),
3246 mode = (mode_t)strtol(buf, NULL, 8);
3248 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3250 if (!NT_STATUS_IS_OK(status)) {
3251 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3255 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3256 d_printf("Server doesn't support UNIX CIFS calls.\n");
3260 status = cli_posix_chmod(targetcli, targetname, mode);
3261 if (!NT_STATUS_IS_OK(status)) {
3262 d_printf("%s chmod file %s 0%o\n",
3263 nt_errstr(status), src, (unsigned int)mode);
3270 static const char *filetype_to_str(mode_t mode)
3272 if (S_ISREG(mode)) {
3273 return "regular file";
3274 } else if (S_ISDIR(mode)) {
3278 if (S_ISCHR(mode)) {
3279 return "character device";
3283 if (S_ISBLK(mode)) {
3284 return "block device";
3288 if (S_ISFIFO(mode)) {
3293 if (S_ISLNK(mode)) {
3294 return "symbolic link";
3298 if (S_ISSOCK(mode)) {
3305 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3314 static char *unix_mode_to_str(char *s, mode_t m)
3317 const char *str = filetype_to_str(m);
3333 *p++ = str[1] == 'y' ? 'l' : 's';
3340 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3341 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3342 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3343 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3344 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3345 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3346 *p++ = rwx_to_str(m, S_IROTH, 'r');
3347 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3348 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3353 /****************************************************************************
3354 Utility function for UNIX getfacl.
3355 ****************************************************************************/
3357 static char *perms_to_string(fstring permstr, unsigned char perms)
3359 fstrcpy(permstr, "---");
3360 if (perms & SMB_POSIX_ACL_READ) {
3363 if (perms & SMB_POSIX_ACL_WRITE) {
3366 if (perms & SMB_POSIX_ACL_EXECUTE) {
3372 /****************************************************************************
3374 ****************************************************************************/
3376 static int cmd_getfacl(void)
3378 TALLOC_CTX *ctx = talloc_tos();
3381 char *targetname = NULL;
3382 struct cli_state *targetcli;
3383 uint16_t major, minor;
3384 uint32_t caplow, caphigh;
3385 char *retbuf = NULL;
3387 SMB_STRUCT_STAT sbuf;
3388 uint16_t num_file_acls = 0;
3389 uint16_t num_dir_acls = 0;
3393 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3394 d_printf("getfacl filename\n");
3397 src = talloc_asprintf(ctx,
3399 client_get_cur_dir(),
3405 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3407 if (!NT_STATUS_IS_OK(status)) {
3408 d_printf("stat %s: %s\n", src, nt_errstr(status));
3412 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3413 d_printf("Server doesn't support UNIX CIFS calls.\n");
3417 status = cli_unix_extensions_version(targetcli, &major, &minor,
3419 if (!NT_STATUS_IS_OK(status)) {
3420 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3425 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3426 d_printf("This server supports UNIX extensions "
3427 "but doesn't support POSIX ACLs.\n");
3431 status = cli_posix_stat(targetcli, targetname, &sbuf);
3432 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3433 d_printf("%s getfacl doing a stat on file %s\n",
3434 nt_errstr(status), src);
3438 status = cli_posix_getacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3439 if (!NT_STATUS_IS_OK(status)) {
3440 d_printf("%s getfacl file %s\n",
3441 nt_errstr(status), src);
3445 /* ToDo : Print out the ACL values. */
3446 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3447 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3448 src, (unsigned int)CVAL(retbuf,0) );
3452 num_file_acls = SVAL(retbuf,2);
3453 num_dir_acls = SVAL(retbuf,4);
3454 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3455 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3457 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3458 (unsigned int)rb_size);
3462 d_printf("# file: %s\n", src);
3463 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3465 if (num_file_acls == 0 && num_dir_acls == 0) {
3466 d_printf("No acls found.\n");
3469 for (i = 0; i < num_file_acls; i++) {
3472 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3473 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3476 case SMB_POSIX_ACL_USER_OBJ:
3479 case SMB_POSIX_ACL_USER:
3480 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3481 d_printf("user:%u:", uorg);
3483 case SMB_POSIX_ACL_GROUP_OBJ:
3484 d_printf("group::");
3486 case SMB_POSIX_ACL_GROUP:
3487 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3488 d_printf("group:%u:", uorg);
3490 case SMB_POSIX_ACL_MASK:
3493 case SMB_POSIX_ACL_OTHER:
3494 d_printf("other::");
3497 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3498 src, (unsigned int)tagtype );
3503 d_printf("%s\n", perms_to_string(permstring, perms));
3506 for (i = 0; i < num_dir_acls; i++) {
3509 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3510 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3513 case SMB_POSIX_ACL_USER_OBJ:
3514 d_printf("default:user::");
3516 case SMB_POSIX_ACL_USER:
3517 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3518 d_printf("default:user:%u:", uorg);
3520 case SMB_POSIX_ACL_GROUP_OBJ:
3521 d_printf("default:group::");
3523 case SMB_POSIX_ACL_GROUP:
3524 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3525 d_printf("default:group:%u:", uorg);
3527 case SMB_POSIX_ACL_MASK:
3528 d_printf("default:mask::");
3530 case SMB_POSIX_ACL_OTHER:
3531 d_printf("default:other::");
3534 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3535 src, (unsigned int)tagtype );
3540 d_printf("%s\n", perms_to_string(permstring, perms));
3546 /****************************************************************************
3547 Get the EA list of a file
3548 ****************************************************************************/
3550 static int cmd_geteas(void)
3552 TALLOC_CTX *ctx = talloc_tos();
3555 char *targetname = NULL;
3556 struct cli_state *targetcli;
3559 struct ea_struct *eas;
3561 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3562 d_printf("geteas filename\n");
3565 src = talloc_asprintf(ctx,
3567 client_get_cur_dir(),
3573 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3575 if (!NT_STATUS_IS_OK(status)) {
3576 d_printf("stat %s: %s\n", src, nt_errstr(status));
3580 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3582 if (!NT_STATUS_IS_OK(status)) {
3583 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3587 for (i=0; i<num_eas; i++) {
3588 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3589 dump_data_file(eas[i].value.data, eas[i].value.length, false,
3599 /****************************************************************************
3601 ****************************************************************************/
3603 static int cmd_setea(void)
3605 TALLOC_CTX *ctx = talloc_tos();
3608 char *eaname = NULL;
3609 char *eavalue = NULL;
3610 char *targetname = NULL;
3611 struct cli_state *targetcli;
3614 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
3615 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
3616 d_printf("setea filename eaname value\n");
3619 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
3620 eavalue = talloc_strdup(ctx, "");
3622 src = talloc_asprintf(ctx,
3624 client_get_cur_dir(),
3630 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3632 if (!NT_STATUS_IS_OK(status)) {
3633 d_printf("stat %s: %s\n", src, nt_errstr(status));
3637 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
3639 if (!NT_STATUS_IS_OK(status)) {
3640 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
3647 /****************************************************************************
3649 ****************************************************************************/
3651 static int cmd_stat(void)
3653 TALLOC_CTX *ctx = talloc_tos();
3656 char *targetname = NULL;
3657 struct cli_state *targetcli;
3659 SMB_STRUCT_STAT sbuf;
3664 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3665 d_printf("stat file\n");
3668 src = talloc_asprintf(ctx,
3670 client_get_cur_dir(),
3676 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3678 if (!NT_STATUS_IS_OK(status)) {
3679 d_printf("stat %s: %s\n", src, nt_errstr(status));
3683 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3684 d_printf("Server doesn't support UNIX CIFS calls.\n");
3688 status = cli_posix_stat(targetcli, targetname, &sbuf);
3689 if (!NT_STATUS_IS_OK(status)) {
3690 d_printf("%s stat file %s\n",
3691 nt_errstr(status), src);
3695 /* Print out the stat values. */
3696 d_printf("File: %s\n", src);
3697 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3698 (double)sbuf.st_ex_size,
3699 (unsigned int)sbuf.st_ex_blocks,
3700 filetype_to_str(sbuf.st_ex_mode));
3702 #if defined(S_ISCHR) && defined(S_ISBLK)
3703 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
3704 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3705 (double)sbuf.st_ex_ino,
3706 (unsigned int)sbuf.st_ex_nlink,
3707 unix_dev_major(sbuf.st_ex_rdev),
3708 unix_dev_minor(sbuf.st_ex_rdev));
3711 d_printf("Inode: %.0f\tLinks: %u\n",
3712 (double)sbuf.st_ex_ino,
3713 (unsigned int)sbuf.st_ex_nlink);
3715 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3716 ((int)sbuf.st_ex_mode & 0777),
3717 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
3718 (unsigned int)sbuf.st_ex_uid,
3719 (unsigned int)sbuf.st_ex_gid);
3721 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
3722 lt = localtime(&tmp_time);
3724 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3726 fstrcpy(mode_str, "unknown");
3728 d_printf("Access: %s\n", mode_str);
3730 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3731 lt = localtime(&tmp_time);
3733 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3735 fstrcpy(mode_str, "unknown");
3737 d_printf("Modify: %s\n", mode_str);
3739 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
3740 lt = localtime(&tmp_time);
3742 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3744 fstrcpy(mode_str, "unknown");
3746 d_printf("Change: %s\n", mode_str);
3752 /****************************************************************************
3754 ****************************************************************************/
3756 static int cmd_chown(void)
3758 TALLOC_CTX *ctx = talloc_tos();
3762 char *buf, *buf2, *buf3;
3763 struct cli_state *targetcli;
3764 char *targetname = NULL;
3767 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3768 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3769 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3770 d_printf("chown uid gid file\n");
3774 uid = (uid_t)atoi(buf);
3775 gid = (gid_t)atoi(buf2);
3777 src = talloc_asprintf(ctx,
3779 client_get_cur_dir(),
3784 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3786 if (!NT_STATUS_IS_OK(status)) {
3787 d_printf("chown %s: %s\n", src, nt_errstr(status));
3791 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3792 d_printf("Server doesn't support UNIX CIFS calls.\n");
3796 status = cli_posix_chown(targetcli, targetname, uid, gid);
3797 if (!NT_STATUS_IS_OK(status)) {
3798 d_printf("%s chown file %s uid=%d, gid=%d\n",
3799 nt_errstr(status), src, (int)uid, (int)gid);
3806 /****************************************************************************
3808 ****************************************************************************/
3810 static int cmd_rename(void)
3812 TALLOC_CTX *ctx = talloc_tos();
3815 struct cli_state *targetcli;
3820 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3821 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3822 d_printf("rename <src> <dest>\n");
3826 src = talloc_asprintf(ctx,
3828 client_get_cur_dir(),
3834 dest = talloc_asprintf(ctx,
3836 client_get_cur_dir(),
3842 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3844 if (!NT_STATUS_IS_OK(status)) {
3845 d_printf("rename %s: %s\n", src, nt_errstr(status));
3849 status = cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli,
3851 if (!NT_STATUS_IS_OK(status)) {
3852 d_printf("rename %s: %s\n", dest, nt_errstr(status));
3856 status = cli_rename(targetcli, targetsrc, targetdest);
3857 if (!NT_STATUS_IS_OK(status)) {
3858 d_printf("%s renaming files %s -> %s \n",
3868 struct scopy_timing {
3869 struct timespec tp_start;
3872 static int scopy_status(off_t written, void *priv)
3874 struct timespec tp_end;
3875 unsigned int scopy_total_time_ms;
3876 struct scopy_timing *st = priv;
3878 clock_gettime_mono(&tp_end);
3879 scopy_total_time_ms = nsec_time_diff(&tp_end,&st->tp_start)/1000000;
3881 DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
3882 (intmax_t)written, written / (1.024*scopy_total_time_ms)));
3887 /****************************************************************************
3888 Server-Side copy some file.
3889 ****************************************************************************/
3891 static int cmd_scopy(void)
3893 TALLOC_CTX *ctx = talloc_tos();
3896 struct cli_state *targetcli;
3899 uint32_t DesiredAccess, ShareAccess, CreateDisposition, CreateOptions;
3900 struct smb_create_returns cr;
3901 uint16_t destfnum = (uint16_t)-1;
3902 uint16_t srcfnum = (uint16_t)-1;
3904 struct scopy_timing st;
3908 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3909 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3910 d_printf("scopy <src> <dest>\n");
3914 src = talloc_asprintf(ctx,
3916 client_get_cur_dir(),
3922 dest = talloc_asprintf(ctx,
3924 client_get_cur_dir(),
3930 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3932 if (!NT_STATUS_IS_OK(status)) {
3933 d_printf("scopy %s: %s\n", src, nt_errstr(status));
3937 status = cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli,
3939 if (!NT_STATUS_IS_OK(status)) {
3940 d_printf("scopy %s: %s\n", dest, nt_errstr(status));
3945 DesiredAccess = (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES|
3946 READ_CONTROL_ACCESS|SYNCHRONIZE_ACCESS);
3947 ShareAccess = FILE_SHARE_READ|FILE_SHARE_DELETE;
3948 CreateDisposition = FILE_OPEN;
3949 CreateOptions = (FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE|
3950 FILE_OPEN_REPARSE_POINT);
3951 status = cli_ntcreate(targetcli, targetsrc, 0, DesiredAccess, 0,
3952 ShareAccess, CreateDisposition, CreateOptions, 0x0,
3954 if (!NT_STATUS_IS_OK(status)) {
3955 d_printf("Failed to open file %s. %s\n",
3956 targetsrc, nt_errstr(status));
3960 DesiredAccess = (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_READ_EA|
3961 FILE_WRITE_EA|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|
3962 DELETE_ACCESS|READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|SYNCHRONIZE_ACCESS);
3963 ShareAccess = FILE_SHARE_NONE;
3964 CreateDisposition = FILE_CREATE;
3965 CreateOptions = FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE;
3966 status = cli_ntcreate(targetcli, targetdest, 0, DesiredAccess,
3967 FILE_ATTRIBUTE_ARCHIVE, ShareAccess, CreateDisposition,
3968 CreateOptions, 0x0, &destfnum, NULL);
3969 if (!NT_STATUS_IS_OK(status)) {
3970 d_printf("Failed to create file %s. %s\n",
3971 targetdest, nt_errstr(status));
3972 cli_close(targetcli, srcfnum);
3976 clock_gettime_mono(&st.tp_start);
3977 status = cli_splice(targetcli, targetcli, srcfnum, destfnum,
3978 cr.end_of_file, 0, 0, &written, scopy_status, &st);
3979 if (!NT_STATUS_IS_OK(status)) {
3980 d_printf("%s copying file %s -> %s \n",
3987 status = cli_close(targetcli, srcfnum);
3988 if (!NT_STATUS_IS_OK(status)) {
3989 d_printf("Error %s closing remote source file\n", nt_errstr(status));
3992 status = cli_close(targetcli, destfnum);
3993 if (!NT_STATUS_IS_OK(status)) {
3994 d_printf("Error %s closing remote dest file\n", nt_errstr(status));
4001 /****************************************************************************
4002 Print the volume name.
4003 ****************************************************************************/
4005 static int cmd_volume(void)
4008 uint32_t serial_num;
4012 status = cli_get_fs_volume_info(cli, talloc_tos(),
4013 &volname, &serial_num,
4015 if (!NT_STATUS_IS_OK(status)) {
4016 d_printf("Error %s getting volume info\n", nt_errstr(status));
4020 d_printf("Volume: |%s| serial number 0x%x\n",
4021 volname, (unsigned int)serial_num);
4025 /****************************************************************************
4026 Hard link files using the NT call.
4027 ****************************************************************************/
4029 static int cmd_hardlink(void)
4031 TALLOC_CTX *ctx = talloc_tos();
4034 struct cli_state *targetcli;
4038 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4039 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4040 d_printf("hardlink <src> <dest>\n");
4044 src = talloc_asprintf(ctx,
4046 client_get_cur_dir(),
4052 dest = talloc_asprintf(ctx,
4054 client_get_cur_dir(),
4060 status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
4062 if (!NT_STATUS_IS_OK(status)) {
4063 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
4067 status = cli_nt_hardlink(targetcli, targetname, dest);
4068 if (!NT_STATUS_IS_OK(status)) {
4069 d_printf("%s doing an NT hard link of files\n",
4077 /****************************************************************************
4078 Toggle the prompt flag.
4079 ****************************************************************************/
4081 static int cmd_prompt(void)
4084 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
4088 /****************************************************************************
4089 Set the newer than time.
4090 ****************************************************************************/
4092 static int cmd_newer(void)
4094 TALLOC_CTX *ctx = talloc_tos();
4097 SMB_STRUCT_STAT sbuf;
4099 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
4100 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
4101 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4102 DEBUG(1,("Getting files newer than %s",
4103 time_to_asc(newer_than)));
4108 if (ok && newer_than == 0) {
4109 d_printf("Error setting newer-than time\n");
4116 /****************************************************************************
4117 Watch directory changes
4118 ****************************************************************************/
4120 static int cmd_notify(void)
4122 TALLOC_CTX *frame = talloc_stackframe();
4127 name = talloc_strdup(talloc_tos(), client_get_cur_dir());
4131 if (!next_token_talloc(talloc_tos(), &cmd_ptr, &buf, NULL)) {
4134 name = talloc_asprintf_append(name, "%s", buf);
4138 status = cli_ntcreate(
4139 cli, name, 0, FILE_READ_DATA, 0,
4140 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4141 FILE_OPEN, 0, 0, &fnum, NULL);
4142 if (!NT_STATUS_IS_OK(status)) {
4143 d_printf("Could not open file: %s\n", nt_errstr(status));
4148 uint32_t i, num_changes;
4149 struct notify_change *changes;
4151 status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
4153 talloc_tos(), &num_changes, &changes);
4154 if (!NT_STATUS_IS_OK(status)) {
4155 d_printf("notify returned %s\n",
4159 for (i=0; i<num_changes; i++) {
4160 printf("%4.4x %s\n", changes[i].action,
4163 TALLOC_FREE(changes);
4166 d_printf("notify <dir name>\n");
4172 /****************************************************************************
4173 Set the archive level.
4174 ****************************************************************************/
4176 static int cmd_archive(void)
4178 TALLOC_CTX *ctx = talloc_tos();
4181 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4182 archive_level = atoi(buf);
4184 d_printf("Archive level is %d\n",archive_level);
4190 /****************************************************************************
4191 Toggle the backup_intent state.
4192 ****************************************************************************/
4194 static int cmd_backup(void)
4196 backup_intent = !backup_intent;
4197 cli_set_backup_intent(cli, backup_intent);
4198 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4202 /****************************************************************************
4203 Toggle the lowercaseflag.
4204 ****************************************************************************/
4206 static int cmd_lowercase(void)
4208 lowercase = !lowercase;
4209 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4213 /****************************************************************************
4214 Toggle the case sensitive flag.
4215 ****************************************************************************/
4217 static int cmd_setcase(void)
4219 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4221 cli_set_case_sensitive(cli, !orig_case_sensitive);
4222 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4227 /****************************************************************************
4228 Toggle the showacls flag.
4229 ****************************************************************************/
4231 static int cmd_showacls(void)
4233 showacls = !showacls;
4234 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4239 /****************************************************************************
4240 Toggle the recurse flag.
4241 ****************************************************************************/
4243 static int cmd_recurse(void)
4246 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4250 /****************************************************************************
4251 Toggle the translate flag.
4252 ****************************************************************************/
4254 static int cmd_translate(void)
4256 translation = !translation;
4257 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4258 translation?"on":"off"));
4262 /****************************************************************************
4264 ****************************************************************************/
4266 static int cmd_lcd(void)
4268 TALLOC_CTX *ctx = talloc_tos();
4272 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4273 if (chdir(buf) == -1) {
4274 d_printf("chdir to %s failed (%s)\n",
4275 buf, strerror(errno));
4282 DEBUG(2,("the local directory is now %s\n",d));
4287 /****************************************************************************
4288 Get a file restarting at end of local file.
4289 ****************************************************************************/
4291 static int cmd_reget(void)
4293 TALLOC_CTX *ctx = talloc_tos();
4294 char *local_name = NULL;
4295 char *remote_name = NULL;
4299 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4304 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4305 d_printf("reget <filename>\n");
4308 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4312 remote_name = clean_name(ctx,remote_name);
4318 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4323 return do_get(remote_name, local_name, true);
4326 /****************************************************************************
4327 Put a file restarting at end of local file.
4328 ****************************************************************************/
4330 static int cmd_reput(void)
4332 TALLOC_CTX *ctx = talloc_tos();
4333 char *local_name = NULL;
4334 char *remote_name = NULL;
4338 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4343 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4344 d_printf("reput <filename>\n");
4348 if (!file_exist_stat(local_name, &st, false)) {
4349 d_printf("%s does not exist\n", local_name);
4353 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4354 remote_name = talloc_asprintf_append(remote_name,
4357 remote_name = talloc_asprintf_append(remote_name,
4364 remote_name = clean_name(ctx, remote_name);
4369 return do_put(remote_name, local_name, true);
4372 /****************************************************************************
4374 ****************************************************************************/
4376 static void browse_fn(const char *name, uint32_t m,
4377 const char *comment, void *state)
4379 const char *typestr = "";
4382 case STYPE_DISKTREE:
4386 typestr = "Printer";
4395 /* FIXME: If the remote machine returns non-ascii characters
4396 in any of these fields, they can corrupt the output. We
4397 should remove them. */
4399 d_printf("\t%-15s %-10.10s%s\n",
4400 name,typestr,comment);
4402 d_printf ("%s|%s|%s\n",typestr,name,comment);
4406 static bool browse_host_rpc(bool sort)
4409 struct rpc_pipe_client *pipe_hnd = NULL;
4410 TALLOC_CTX *frame = talloc_stackframe();
4412 struct srvsvc_NetShareInfoCtr info_ctr;
4413 struct srvsvc_NetShareCtr1 ctr1;
4414 uint32_t resume_handle = 0;
4415 uint32_t total_entries = 0;
4417 struct dcerpc_binding_handle *b;
4419 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
4422 if (!NT_STATUS_IS_OK(status)) {
4423 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4424 nt_errstr(status)));
4429 b = pipe_hnd->binding_handle;
4431 ZERO_STRUCT(info_ctr);
4435 info_ctr.ctr.ctr1 = &ctr1;
4437 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4445 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4446 TALLOC_FREE(pipe_hnd);
4451 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4452 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4453 browse_fn(info.name, info.type, info.comment, NULL);
4456 TALLOC_FREE(pipe_hnd);
4461 /****************************************************************************
4462 Try and browse available connections on a host.
4463 ****************************************************************************/
4465 static bool browse_host(bool sort)
4469 d_printf("\n\tSharename Type Comment\n");
4470 d_printf("\t--------- ---- -------\n");
4473 if (browse_host_rpc(sort)) {
4477 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1) {
4478 NTSTATUS status = cli_nt_error(cli);
4479 d_printf("Error returning browse list: %s\n",
4486 /****************************************************************************
4488 ****************************************************************************/
4490 static void server_fn(const char *name, uint32_t m,
4491 const char *comment, void *state)
4495 d_printf("\t%-16s %s\n", name, comment);
4497 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4501 /****************************************************************************
4502 Try and browse available connections on a host.
4503 ****************************************************************************/
4505 static bool list_servers(const char *wk_grp)
4509 if (!cli->server_domain)
4513 d_printf("\n\tServer Comment\n");
4514 d_printf("\t--------- -------\n");
4516 fstrcpy( state, "Server" );
4517 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4521 d_printf("\n\tWorkgroup Master\n");
4522 d_printf("\t--------- -------\n");
4525 fstrcpy( state, "Workgroup" );
4526 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4531 /****************************************************************************
4532 Print or set current VUID
4533 ****************************************************************************/
4535 static int cmd_vuid(void)
4537 TALLOC_CTX *ctx = talloc_tos();
4540 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4541 d_printf("Current VUID is %d\n",
4542 cli_state_get_uid(cli));
4546 cli_state_set_uid(cli, atoi(buf));
4550 /****************************************************************************
4551 Setup a new VUID, by issuing a session setup
4552 ****************************************************************************/
4554 static int cmd_logon(void)
4556 TALLOC_CTX *ctx = talloc_tos();
4557 char *l_username, *l_password;
4558 struct cli_credentials *creds = NULL;
4561 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
4562 d_printf("logon <username> [<password>]\n");
4566 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
4567 char pwd[256] = {0};
4570 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
4572 l_password = talloc_strdup(ctx, pwd);
4579 creds = cli_session_creds_init(ctx,
4584 false, /* use_kerberos */
4585 false, /* fallback_after_kerberos */
4586 false, /* use_ccache */
4587 false); /* password_is_nt_hash */
4588 if (creds == NULL) {
4589 d_printf("cli_session_creds_init() failed.\n");
4592 nt_status = cli_session_setup_creds(cli, creds);
4594 if (!NT_STATUS_IS_OK(nt_status)) {
4595 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
4599 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
4607 static int cmd_logoff(void)
4611 status = cli_ulogoff(cli);
4612 if (!NT_STATUS_IS_OK(status)) {
4613 d_printf("logoff failed: %s\n", nt_errstr(status));
4617 d_printf("logoff successful\n");
4623 * tree connect (connect to a share)
4626 static int cmd_tcon(void)
4628 TALLOC_CTX *ctx = talloc_tos();
4632 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
4633 d_printf("tcon <sharename>\n");
4641 status = cli_tree_connect(cli, sharename, "?????", NULL);
4642 if (!NT_STATUS_IS_OK(status)) {
4643 d_printf("tcon failed: %s\n", nt_errstr(status));
4647 talloc_free(sharename);
4649 d_printf("tcon to %s successful, tid: %u\n", sharename,
4650 cli_state_get_tid(cli));
4655 * tree disconnect (disconnect from a share)
4658 static int cmd_tdis(void)
4662 status = cli_tdis(cli);
4663 if (!NT_STATUS_IS_OK(status)) {
4664 d_printf("tdis failed: %s\n", nt_errstr(status));
4668 d_printf("tdis successful\n");
4677 static int cmd_tid(void)
4679 TALLOC_CTX *ctx = talloc_tos();
4682 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
4683 if (cli_state_has_tcon(cli)) {
4684 d_printf("current tid is %d\n", cli_state_get_tid(cli));
4686 d_printf("no tcon currently\n");
4689 uint16_t tid = atoi(tid_str);
4690 cli_state_set_tid(cli, tid);
4697 /****************************************************************************
4698 list active connections
4699 ****************************************************************************/
4701 static int cmd_list_connect(void)
4703 cli_cm_display(cli);
4707 /****************************************************************************
4708 display the current active client connection
4709 ****************************************************************************/
4711 static int cmd_show_connect( void )
4713 TALLOC_CTX *ctx = talloc_tos();
4714 struct cli_state *targetcli;
4718 status = cli_resolve_path(ctx, "", auth_info, cli,
4719 client_get_cur_dir(), &targetcli,
4721 if (!NT_STATUS_IS_OK(status)) {
4722 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
4726 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli->conn), targetcli->share);
4731 * set_remote_attr - set DOS attributes of a remote file
4732 * @filename: path to the file name
4733 * @new_attr: attribute bit mask to use
4734 * @mode: one of ATTR_SET or ATTR_UNSET
4736 * Update the file attributes with the one provided.
4738 int set_remote_attr(const char *filename, uint16_t new_attr, int mode)
4740 extern struct cli_state *cli;
4744 status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
4745 if (!NT_STATUS_IS_OK(status)) {
4746 d_printf("cli_getatr failed: %s\n", nt_errstr(status));
4750 if (mode == ATTR_SET) {
4751 new_attr |= old_attr;
4753 new_attr = old_attr & ~new_attr;
4756 status = cli_setatr(cli, filename, new_attr, 0);
4757 if (!NT_STATUS_IS_OK(status)) {
4758 d_printf("cli_setatr failed: %s\n", nt_errstr(status));
4766 * cmd_setmode - interactive command to set DOS attributes
4768 * Read a filename and mode from the client command line and update
4769 * the file DOS attributes.
4771 int cmd_setmode(void)
4773 const extern char *cmd_ptr;
4776 uint16_t attr[2] = {0};
4777 int mode = ATTR_SET;
4780 TALLOC_CTX *ctx = talloc_new(NULL);
4785 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
4787 d_printf("setmode <filename> <[+|-]rsha>\n");
4792 fname = talloc_asprintf(ctx,
4794 client_get_cur_dir(),
4796 if (fname == NULL) {
4801 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4802 const char *s = buf;
4813 attr[mode] |= FILE_ATTRIBUTE_READONLY;
4816 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
4819 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
4822 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
4825 d_printf("setmode <filename> <perm=[+|-]rsha>\n");
4832 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
4833 d_printf("setmode <filename> <[+|-]rsha>\n");
4838 DEBUG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
4840 /* ignore return value: server might not store DOS attributes */
4841 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
4842 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
4848 /****************************************************************************
4850 ***************************************************************************/
4852 int cmd_iosize(void)
4854 TALLOC_CTX *ctx = talloc_tos();
4858 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4859 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
4861 d_printf("iosize <n> or iosize 0x<n>. "
4862 "Minimum is 0 (default), "
4863 "max is 16776960 (0xFFFF00)\n");
4865 d_printf("iosize <n> or iosize 0x<n>. "
4866 "(Encrypted connection) ,"
4867 "Minimum is 0 (default), "
4868 "max is 130048 (0x1FC00)\n");
4871 d_printf("iosize <n> or iosize 0x<n>.\n");
4876 iosize = strtol(buf,NULL,0);
4877 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
4878 if (smb_encrypt && (iosize < 0 || iosize > 0xFC00)) {
4879 d_printf("iosize out of range for encrypted "
4880 "connection (min = 0 (default), "
4881 "max = 130048 (0x1FC00)\n");
4883 } else if (!smb_encrypt && (iosize < 0 || iosize > 0xFFFF00)) {
4884 d_printf("iosize out of range (min = 0 (default), "
4885 "max = 16776960 (0xFFFF00)\n");
4890 io_bufsize = iosize;
4891 d_printf("iosize is now %d\n", io_bufsize);
4895 /****************************************************************************
4897 ***************************************************************************/
4899 static int cmd_timeout(void)
4901 TALLOC_CTX *ctx = talloc_tos();
4904 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4905 unsigned int old_timeout = cli_set_timeout(cli, 0);
4906 cli_set_timeout(cli, old_timeout);
4907 d_printf("timeout <n> (per-operation timeout "
4908 "in seconds - currently %u).\n",
4913 io_timeout = strtol(buf,NULL,0);
4914 cli_set_timeout(cli, io_timeout*1000);
4915 d_printf("io_timeout per operation is now %d\n", io_timeout);
4920 /****************************************************************************
4922 ****************************************************************************/
4923 static int cmd_history(void)
4925 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4929 hlist = history_list();
4931 for (i = 0; hlist && hlist[i]; i++) {
4932 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
4935 DEBUG(0,("no history without readline support\n"));
4941 /* Some constants for completing filename arguments */
4943 #define COMPL_NONE 0 /* No completions */
4944 #define COMPL_REMOTE 1 /* Complete remote filename */
4945 #define COMPL_LOCAL 2 /* Complete local filename */
4947 /* This defines the commands supported by this client.
4948 * NOTE: The "!" must be the last one in the list because it's fn pointer
4949 * field is NULL, and NULL in that field is used in process_tok()
4950 * (below) to indicate the end of the list. crh
4955 const char *description;
4956 char compl_args[2]; /* Completion argument info */
4958 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4959 {"allinfo",cmd_allinfo,"<file> show all available info",
4960 {COMPL_NONE,COMPL_NONE}},
4961 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
4962 {"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}},
4963 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
4964 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
4965 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
4966 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
4967 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
4968 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_NONE}},
4969 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_NONE}},
4970 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_NONE}},
4971 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4972 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4973 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4974 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
4975 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4976 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
4977 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_NONE}},
4978 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
4979 {COMPL_REMOTE, COMPL_NONE}},
4980 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4981 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4982 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
4983 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
4984 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
4985 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4986 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4987 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
4988 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4989 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4990 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
4991 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4992 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
4993 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4994 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
4995 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
4996 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
4997 {"notify",cmd_notify,"<file>Get notified of dir changes",{COMPL_REMOTE,COMPL_NONE}},
4998 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
4999 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
5000 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
5001 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5002 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5003 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5004 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5005 {"posix_whoami",cmd_posix_whoami,"retun logged on user information "
5006 "using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5007 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
5008 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
5009 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
5010 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
5011 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5012 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
5013 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5014 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5015 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
5016 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
5017 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
5018 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
5019 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
5020 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5021 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_REMOTE,COMPL_NONE}},
5022 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
5023 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
5024 {COMPL_REMOTE, COMPL_LOCAL}},
5025 {"setmode",cmd_setmode,"<file name> <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
5026 {"scopy",cmd_scopy,"<src> <dest> server-side copy file",{COMPL_REMOTE,COMPL_REMOTE}},
5027 {"stat",cmd_stat,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_NONE}},
5028 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5029 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
5030 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
5031 {"timeout",cmd_timeout,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE,COMPL_NONE}},
5032 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
5033 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5034 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
5035 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
5036 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5037 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
5038 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
5039 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
5040 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
5041 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
5042 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
5043 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
5044 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
5046 /* Yes, this must be here, see crh's comment above. */
5047 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
5048 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
5051 /*******************************************************************
5052 Lookup a command string in the list of commands, including
5054 ******************************************************************/
5056 static int process_tok(char *tok)
5058 int i = 0, matches = 0;
5060 int tok_len = strlen(tok);
5062 while (commands[i].fn != NULL) {
5063 if (strequal(commands[i].name,tok)) {
5067 } else if (strnequal(commands[i].name, tok, tok_len)) {
5076 else if (matches == 1)
5082 /****************************************************************************
5084 ****************************************************************************/
5086 static int cmd_help(void)
5088 TALLOC_CTX *ctx = talloc_tos();
5092 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5093 if ((i = process_tok(buf)) >= 0)
5094 d_printf("HELP %s:\n\t%s\n\n",
5095 commands[i].name,commands[i].description);
5097 while (commands[i].description) {
5098 for (j=0; commands[i].description && (j<5); j++) {
5099 d_printf("%-15s",commands[i].name);
5108 /****************************************************************************
5109 Process a -c command string.
5110 ****************************************************************************/
5112 static int process_command_string(const char *cmd_in)
5114 TALLOC_CTX *ctx = talloc_tos();
5115 char *cmd = talloc_strdup(ctx, cmd_in);
5121 /* establish the connection if not already */
5126 status = cli_cm_open(talloc_tos(), NULL,
5127 have_ip ? dest_ss_str : desthost,
5130 max_protocol, port, name_type,
5132 if (!NT_STATUS_IS_OK(status)) {
5135 cli_set_timeout(cli, io_timeout*1000);
5138 while (cmd[0] != '\0') {
5144 if ((p = strchr_m(cmd, ';')) == 0) {
5153 /* and get the first part of the command */
5155 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
5159 if ((i = process_tok(tok)) >= 0) {
5160 rc = commands[i].fn();
5161 } else if (i == -2) {
5162 d_printf("%s: command abbreviation ambiguous\n",tok);
5164 d_printf("%s: command not found\n",tok);
5171 #define MAX_COMPLETIONS 100
5173 struct completion_remote {
5181 static NTSTATUS completion_remote_filter(const char *mnt,
5182 struct file_info *f,
5186 struct completion_remote *info = (struct completion_remote *)state;
5188 if (info->count >= MAX_COMPLETIONS - 1) {
5189 return NT_STATUS_OK;
5191 if (strncmp(info->text, f->name, info->len) != 0) {
5192 return NT_STATUS_OK;
5194 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
5195 return NT_STATUS_OK;
5198 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
5199 info->matches[info->count] = SMB_STRDUP(f->name);
5201 TALLOC_CTX *ctx = talloc_stackframe();
5204 tmp = talloc_strdup(ctx,info->dirmask);
5207 return NT_STATUS_NO_MEMORY;
5209 tmp = talloc_asprintf_append(tmp, "%s", f->name);
5212 return NT_STATUS_NO_MEMORY;
5214 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
5215 tmp = talloc_asprintf_append(tmp, "%s",
5220 return NT_STATUS_NO_MEMORY;
5222 info->matches[info->count] = SMB_STRDUP(tmp);
5225 if (info->matches[info->count] == NULL) {
5226 return NT_STATUS_OK;
5228 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
5229 smb_readline_ca_char(0);
5231 if (info->count == 1) {
5232 info->samelen = strlen(info->matches[info->count]);
5234 while (strncmp(info->matches[info->count],
5235 info->matches[info->count-1],
5236 info->samelen) != 0) {
5241 return NT_STATUS_OK;
5244 static char **remote_completion(const char *text, int len)
5246 TALLOC_CTX *ctx = talloc_stackframe();
5247 char *dirmask = NULL;
5248 char *targetpath = NULL;
5249 struct cli_state *targetcli = NULL;
5251 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
5254 /* can't have non-static initialisation on Sun CC, so do it
5260 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
5261 if (!info.matches) {
5267 * We're leaving matches[0] free to fill it later with the text to
5268 * display: Either the one single match or the longest common subset
5271 info.matches[0] = NULL;
5274 for (i = len-1; i >= 0; i--) {
5275 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
5280 info.text = text+i+1;
5281 info.samelen = info.len = len-i-1;
5284 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
5285 if (!info.dirmask) {
5288 strncpy(info.dirmask, text, i+1);
5289 info.dirmask[i+1] = 0;
5290 dirmask = talloc_asprintf(ctx,
5292 client_get_cur_dir(),
5296 info.dirmask = SMB_STRDUP("");
5297 if (!info.dirmask) {
5300 dirmask = talloc_asprintf(ctx,
5302 client_get_cur_dir());
5308 status = cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli,
5310 if (!NT_STATUS_IS_OK(status)) {
5313 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
5314 completion_remote_filter, (void *)&info);
5315 if (!NT_STATUS_IS_OK(status)) {
5319 if (info.count == 1) {
5321 * No matches at all, NULL indicates there is nothing
5323 SAFE_FREE(info.matches[0]);
5324 SAFE_FREE(info.matches);
5329 if (info.count == 2) {
5331 * Exactly one match in matches[1], indicate this is the one
5334 info.matches[0] = info.matches[1];
5335 info.matches[1] = NULL;
5338 return info.matches;
5342 * We got more than one possible match, set the result to the maximum
5346 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
5347 info.matches[info.count] = NULL;
5349 return info.matches;
5352 for (i = 0; i < info.count; i++) {
5353 SAFE_FREE(info.matches[i]);
5355 SAFE_FREE(info.matches);
5356 SAFE_FREE(info.dirmask);
5361 static char **completion_fn(const char *text, int start, int end)
5363 smb_readline_ca_char(' ');
5366 const char *buf, *sp;
5370 buf = smb_readline_get_line_buffer();
5374 sp = strchr(buf, ' ');
5378 for (i = 0; commands[i].name; i++) {
5379 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
5380 (commands[i].name[sp - buf] == 0)) {
5384 if (commands[i].name == NULL)
5390 if (sp == (buf + start))
5391 compl_type = commands[i].compl_args[0];
5393 compl_type = commands[i].compl_args[1];
5395 if (compl_type == COMPL_REMOTE)
5396 return remote_completion(text, end - start);
5397 else /* fall back to local filename completion */
5401 int i, len, samelen = 0, count=1;
5403 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
5410 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
5411 if (strncmp(text, commands[i].name, len) == 0) {
5412 matches[count] = SMB_STRDUP(commands[i].name);
5413 if (!matches[count])
5416 samelen = strlen(matches[count]);
5418 while (strncmp(matches[count], matches[count-1], samelen) != 0)
5425 case 0: /* should never happen */
5429 matches[0] = SMB_STRDUP(matches[1]);
5432 matches[0] = (char *)SMB_MALLOC(samelen+1);
5435 strncpy(matches[0], matches[1], samelen);
5436 matches[0][samelen] = 0;
5438 matches[count] = NULL;
5442 for (i = 0; i < count; i++)
5450 static bool finished;
5452 /****************************************************************************
5453 Make sure we swallow keepalives during idle time.
5454 ****************************************************************************/
5456 static void readline_callback(void)
5458 static time_t last_t;
5459 struct timespec now;
5462 unsigned char garbage[16];
5464 clock_gettime_mono(&now);
5472 /* Ping the server to keep the connection alive using SMBecho. */
5473 memset(garbage, 0xf0, sizeof(garbage));
5474 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
5475 if (NT_STATUS_IS_OK(status)) {
5479 if (!cli_state_is_connected(cli)) {
5480 DEBUG(0,("SMBecho failed (%s). The connection is "
5481 "disconnected now\n", nt_errstr(status)));
5483 smb_readline_done();
5487 /****************************************************************************
5488 Process commands on stdin.
5489 ****************************************************************************/
5491 static int process_stdin(void)
5496 TALLOC_CTX *frame = talloc_stackframe();
5498 char *the_prompt = NULL;
5502 /* display a prompt */
5503 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
5507 line = smb_readline(the_prompt, readline_callback, completion_fn);
5508 SAFE_FREE(the_prompt);
5514 /* special case - first char is ! */
5516 if (system(line + 1) == -1) {
5517 d_printf("system() command %s failed.\n",
5525 /* and get the first part of the command */
5527 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
5533 if ((i = process_tok(tok)) >= 0) {
5534 rc = commands[i].fn();
5535 } else if (i == -2) {
5536 d_printf("%s: command abbreviation ambiguous\n",tok);
5538 d_printf("%s: command not found\n",tok);
5546 /****************************************************************************
5547 Process commands from the client.
5548 ****************************************************************************/
5550 static int process(const char *base_directory)
5555 status = cli_cm_open(talloc_tos(), NULL,
5556 have_ip ? dest_ss_str : desthost,
5557 service, auth_info, true, smb_encrypt,
5558 max_protocol, port, name_type, &cli);
5559 if (!NT_STATUS_IS_OK(status)) {
5563 cli_set_timeout(cli, io_timeout*1000);
5565 if (base_directory && *base_directory) {
5566 rc = do_cd(base_directory);
5574 rc = process_command_string(cmdstr);
5583 /****************************************************************************
5585 ****************************************************************************/
5587 static int do_host_query(const char *query_host)
5591 status = cli_cm_open(talloc_tos(), NULL,
5592 have_ip ? dest_ss_str : query_host,
5593 "IPC$", auth_info, true, smb_encrypt,
5594 max_protocol, port, name_type, &cli);
5595 if (!NT_STATUS_IS_OK(status)) {
5599 cli_set_timeout(cli, io_timeout*1000);
5602 /* Ensure that the host can do IPv4 */
5604 if (!interpret_addr(query_host)) {
5605 struct sockaddr_storage ss;
5606 if (interpret_string_addr(&ss, query_host, 0) &&
5607 (ss.ss_family != AF_INET)) {
5608 d_printf("%s is an IPv6 address -- no workgroup available\n",
5614 if (lp_disable_netbios()) {
5618 if (port != NBT_SMB_PORT) {
5620 /* Workgroups simply don't make sense over anything
5621 else but port 139... */
5624 status = cli_cm_open(talloc_tos(), NULL,
5625 have_ip ? dest_ss_str : query_host,
5626 "IPC$", auth_info, true, smb_encrypt,
5627 max_protocol, NBT_SMB_PORT, name_type,
5629 if (!NT_STATUS_IS_OK(status)) {
5635 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
5639 cli_set_timeout(cli, io_timeout*1000);
5640 list_servers(lp_workgroup());
5647 /****************************************************************************
5648 Handle a tar operation.
5649 ****************************************************************************/
5651 static int do_tar_op(const char *base_directory)
5653 struct tar *tar_ctx = tar_get_ctx();
5656 /* do we already have a connection? */
5660 status = cli_cm_open(talloc_tos(), NULL,
5661 have_ip ? dest_ss_str : desthost,
5662 service, auth_info, true, smb_encrypt,
5663 max_protocol, port, name_type, &cli);
5664 if (!NT_STATUS_IS_OK(status)) {
5668 cli_set_timeout(cli, io_timeout*1000);
5673 if (base_directory && *base_directory) {
5674 ret = do_cd(base_directory);
5680 ret = tar_process(tar_ctx);
5688 /****************************************************************************
5689 Handle a message operation.
5690 ****************************************************************************/
5692 static int do_message_op(struct user_auth_info *a_info)
5696 if (lp_disable_netbios()) {
5697 d_printf("NetBIOS over TCP disabled.\n");
5701 status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
5702 port ? port : NBT_SMB_PORT, name_type,
5703 lp_netbios_name(), SMB_SIGNING_DEFAULT, 0, &cli);
5704 if (!NT_STATUS_IS_OK(status)) {
5705 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
5709 cli_set_timeout(cli, io_timeout*1000);
5710 send_message(get_cmdline_auth_info_username(a_info));
5716 /****************************************************************************
5718 ****************************************************************************/
5720 int main(int argc,char *argv[])
5722 const char **const_argv = discard_const_p(const char *, argv);
5723 char *base_directory = NULL;
5725 char *query_host = NULL;
5726 bool message = false;
5727 static const char *new_name_resolve_order = NULL;
5731 bool tar_opt = false;
5732 bool service_opt = false;
5733 struct tar *tar_ctx = tar_get_ctx();
5735 struct poptOption long_options[] = {
5738 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
5739 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
5740 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
5741 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
5742 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
5743 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
5744 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
5745 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
5746 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
5747 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
5748 { "timeout", 't', POPT_ARG_INT, &io_timeout, 'b', "Changes the per-operation timeout", "SECONDS" },
5749 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
5750 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
5751 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
5753 POPT_COMMON_CONNECTION
5754 POPT_COMMON_CREDENTIALS
5757 TALLOC_CTX *frame = talloc_stackframe();
5759 if (!client_set_cur_dir("\\")) {
5763 /* set default debug level to 1 regardless of what smb.conf sets */
5764 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
5767 lp_set_cmdline("log level", "1");
5769 popt_common_credentials_set_delay_post();
5772 pc = poptGetContext("smbclient", argc, const_argv, long_options, 0);
5773 poptSetOtherOptionHelp(pc, "service <password>");
5775 while ((opt = poptGetNextOpt(pc)) != -1) {
5777 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
5778 /* I see no other way to keep things sane --SSS */
5779 if (tar_opt == true) {
5780 while (poptPeekArg(pc)) {
5786 /* if the service has not yet been specified lets see if it is available in the popt stack */
5787 if (!service_opt && poptPeekArg(pc)) {
5788 service = talloc_strdup(frame, poptGetArg(pc));
5795 /* if the service has already been retrieved then check if we have also a password */
5797 && (!get_cmdline_auth_info_got_pass(cmdline_auth_info))
5798 && poptPeekArg(pc)) {
5799 set_cmdline_auth_info_password(cmdline_auth_info,
5806 /* Messages are sent to NetBIOS name type 0x3
5807 * (Messenger Service). Make sure we default
5808 * to port 139 instead of port 445. srl,crh
5811 desthost = talloc_strdup(frame,poptGetOptArg(pc));
5816 port = NBT_SMB_PORT;
5821 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
5825 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
5829 setup_logging("smbclient", DEBUG_STDERR );
5830 display_set_stderr();
5834 query_host = talloc_strdup(frame, poptGetOptArg(pc));
5840 lp_set_cmdline("client max protocol", poptGetOptArg(pc));
5843 /* We must use old option processing for this. Find the
5844 * position of the -T option in the raw argv[]. */
5848 for (i = 1; i < argc; i++) {
5849 if (strncmp("-T", argv[i],2)==0)
5853 if (tar_parse_args(tar_ctx, poptGetOptArg(pc),
5854 const_argv + i, argc - i)) {
5855 poptPrintUsage(pc, stderr, 0);
5859 /* this must be the last option, mark we have parsed it so that we know we have */
5863 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
5864 if (!base_directory) {
5875 return(do_smb_browse());
5880 /* We may still have some leftovers after the last popt option has been called */
5881 if (tar_opt == true) {
5882 while (poptPeekArg(pc)) {
5888 /* if the service has not yet been specified lets see if it is available in the popt stack */
5889 if (!service_opt && poptPeekArg(pc)) {
5890 service = talloc_strdup(frame,poptGetArg(pc));
5897 /* if the service has already been retrieved then check if we have also a password */
5899 && !get_cmdline_auth_info_got_pass(cmdline_auth_info)
5900 && poptPeekArg(pc)) {
5901 set_cmdline_auth_info_password(cmdline_auth_info,
5905 if ( override_logfile )
5906 setup_logging( lp_logfile(talloc_tos()), DEBUG_FILE );
5908 if (!lp_load_client(get_dyn_CONFIGFILE())) {
5909 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
5910 argv[0], get_dyn_CONFIGFILE());
5915 if (service_opt && service) {
5918 /* Convert any '/' characters in the service name to '\' characters */
5919 string_replace(service, '/','\\');
5920 if (count_chars(service,'\\') < 3) {
5921 d_printf("\n%s: Not enough '\\' characters in service\n",service);
5922 poptPrintUsage(pc, stderr, 0);
5925 /* Remove trailing slashes */
5926 len = strlen(service);
5927 while(len > 0 && service[len - 1] == '\\') {
5929 service[len] = '\0';
5933 if (!init_names()) {
5934 fprintf(stderr, "init_names() failed\n");
5938 if(new_name_resolve_order)
5939 lp_set_cmdline("name resolve order", new_name_resolve_order);
5941 if (!tar_to_process(tar_ctx) && !query_host && !service && !message) {
5942 poptPrintUsage(pc, stderr, 0);
5946 poptFreeContext(pc);
5947 popt_burn_cmdline_password(argc, argv);
5949 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5951 /* Ensure we have a password (or equivalent). */
5952 popt_common_credentials_post();
5953 auth_info = cmdline_auth_info;
5954 smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
5956 max_protocol = lp_client_max_protocol();
5958 if (tar_to_process(tar_ctx)) {
5960 process_command_string(cmdstr);
5961 rc = do_tar_op(base_directory);
5962 } else if (query_host && *query_host) {
5963 char *qhost = query_host;
5966 while (*qhost == '\\' || *qhost == '/')
5969 if ((slash = strchr_m(qhost, '/'))
5970 || (slash = strchr_m(qhost, '\\'))) {
5974 if ((p=strchr_m(qhost, '#'))) {
5977 sscanf(p, "%x", &name_type);
5980 rc = do_host_query(qhost);
5981 } else if (message) {
5982 rc = do_message_op(auth_info);
5983 } else if (process(base_directory)) {