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 "popt_common.h"
26 #include "client/client_proto.h"
27 #include "../librpc/gen_ndr/cli_srvsvc.h"
28 #include "../lib/util/select.h"
29 #include "system/readline.h"
30 #include "../libcli/smbreadline/smbreadline.h"
31 #include "../libcli/security/security.h"
37 extern int do_smb_browse(void); /* mDNS browsing */
39 extern bool override_logfile;
44 static char *desthost;
45 static char *calling_name;
46 static bool grepable = false;
47 static char *cmdstr = NULL;
48 const char *cmd_ptr = NULL;
50 static int io_bufsize = 524288;
52 static int name_type = 0x20;
53 static int max_protocol = PROTOCOL_NT1;
55 static int process_tok(char *tok);
56 static int cmd_help(void);
58 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
60 /* 30 second timeout on most commands */
61 #define CLIENT_TIMEOUT (30*1000)
62 #define SHORT_TIMEOUT (5*1000)
64 /* value for unused fid field in trans2 secondary request */
65 #define FID_UNUSED (0xFFFF)
67 time_t newer_than = 0;
68 static int archive_level = 0;
70 static bool translation = false;
73 /* clitar bits insert */
76 extern bool tar_reset;
79 static bool prompt = true;
81 static bool recurse = false;
82 static bool showacls = false;
83 bool lowercase = false;
85 static struct sockaddr_storage dest_ss;
86 static char dest_ss_str[INET6_ADDRSTRLEN];
88 #define SEPARATORS " \t\n\r"
90 static bool abort_mget = true;
93 uint64_t get_total_size = 0;
94 unsigned int get_total_time_ms = 0;
95 static uint64_t put_total_size = 0;
96 static unsigned int put_total_time_ms = 0;
99 static double dir_total;
101 /* encrypted state. */
102 static bool smb_encrypt;
104 /* root cli_state connection */
106 struct cli_state *cli;
108 static char CLI_DIRSEP_CHAR = '\\';
109 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
111 /* Authentication for client connections. */
112 struct user_auth_info *auth_info;
114 /* Accessor functions for directory paths. */
115 static char *fileselection;
116 static const char *client_get_fileselection(void)
119 return fileselection;
124 static const char *client_set_fileselection(const char *new_fs)
126 SAFE_FREE(fileselection);
128 fileselection = SMB_STRDUP(new_fs);
130 return client_get_fileselection();
134 static const char *client_get_cwd(void)
139 return CLI_DIRSEP_STR;
142 static const char *client_set_cwd(const char *new_cwd)
146 cwd = SMB_STRDUP(new_cwd);
148 return client_get_cwd();
151 static char *cur_dir;
152 const char *client_get_cur_dir(void)
157 return CLI_DIRSEP_STR;
160 const char *client_set_cur_dir(const char *newdir)
164 cur_dir = SMB_STRDUP(newdir);
166 return client_get_cur_dir();
169 /****************************************************************************
170 Put up a yes/no prompt.
171 ****************************************************************************/
173 static bool yesno(const char *p)
178 if (!fgets(ans,sizeof(ans)-1,stdin))
181 if (*ans == 'y' || *ans == 'Y')
187 /****************************************************************************
188 Write to a local file with CR/LF->LF translation if appropriate. Return the
189 number taken from the buffer. This may not equal the number written.
190 ****************************************************************************/
192 static int writefile(int f, char *b, int n)
202 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
205 if (write(f, b, 1) != 1) {
215 /****************************************************************************
216 Read from a file with LF->CR/LF translation if appropriate. Return the
217 number read. read approx n bytes.
218 ****************************************************************************/
220 static int readfile(uint8_t *b, int n, XFILE *f)
226 return x_fread(b,1,n,f);
229 while (i < (n - 1) && (i < BUFFER_SIZE)) {
230 if ((c = x_getc(f)) == EOF) {
234 if (c == '\n') { /* change all LFs to CR/LF */
249 static size_t push_source(uint8_t *buf, size_t n, void *priv)
251 struct push_state *state = (struct push_state *)priv;
254 if (x_feof(state->f)) {
258 result = readfile(buf, n, state->f);
259 state->nread += result;
263 /****************************************************************************
265 ****************************************************************************/
267 static void send_message(const char *username)
273 d_printf("Type your message, ending it with a Control-D\n");
276 while (i<sizeof(buf)-2) {
277 int c = fgetc(stdin);
288 status = cli_message(cli, desthost, username, buf);
289 if (!NT_STATUS_IS_OK(status)) {
290 d_fprintf(stderr, "cli_message returned %s\n",
295 /****************************************************************************
296 Check the space on a device.
297 ****************************************************************************/
299 static int do_dskattr(void)
301 int total, bsize, avail;
302 struct cli_state *targetcli = NULL;
303 char *targetpath = NULL;
304 TALLOC_CTX *ctx = talloc_tos();
307 if ( !cli_resolve_path(ctx, "", auth_info, cli, client_get_cur_dir(), &targetcli, &targetpath)) {
308 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
312 status = cli_dskattr(targetcli, &bsize, &total, &avail);
313 if (!NT_STATUS_IS_OK(status)) {
314 d_printf("Error in dskattr: %s\n", nt_errstr(status));
318 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
319 total, bsize, avail);
324 /****************************************************************************
326 ****************************************************************************/
328 static int cmd_pwd(void)
330 d_printf("Current directory is %s",service);
331 d_printf("%s\n",client_get_cur_dir());
335 /****************************************************************************
336 Ensure name has correct directory separators.
337 ****************************************************************************/
339 static void normalize_name(char *newdir)
341 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
342 string_replace(newdir,'/','\\');
346 /****************************************************************************
347 Change directory - inner section.
348 ****************************************************************************/
350 static int do_cd(const char *new_dir)
353 char *saved_dir = NULL;
355 char *targetpath = NULL;
356 struct cli_state *targetcli = NULL;
357 SMB_STRUCT_STAT sbuf;
360 TALLOC_CTX *ctx = talloc_stackframe();
362 newdir = talloc_strdup(ctx, new_dir);
368 normalize_name(newdir);
370 /* Save the current directory in case the new directory is invalid */
372 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
378 if (*newdir == CLI_DIRSEP_CHAR) {
379 client_set_cur_dir(newdir);
382 new_cd = talloc_asprintf(ctx, "%s%s",
383 client_get_cur_dir(),
390 /* Ensure cur_dir ends in a DIRSEP */
391 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
392 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
397 client_set_cur_dir(new_cd);
399 new_cd = clean_name(ctx, new_cd);
400 client_set_cur_dir(new_cd);
402 if ( !cli_resolve_path(ctx, "", auth_info, cli, new_cd, &targetcli, &targetpath)) {
403 d_printf("cd %s: %s\n", new_cd, cli_errstr(cli));
404 client_set_cur_dir(saved_dir);
408 if (strequal(targetpath,CLI_DIRSEP_STR )) {
413 /* Use a trans2_qpathinfo to test directories for modern servers.
414 Except Win9x doesn't support the qpathinfo_basic() call..... */
416 if (targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95) {
419 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
421 if (!NT_STATUS_IS_OK(status)) {
422 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
423 client_set_cur_dir(saved_dir);
427 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
428 d_printf("cd %s: not a directory\n", new_cd);
429 client_set_cur_dir(saved_dir);
435 targetpath = talloc_asprintf(ctx,
440 client_set_cur_dir(saved_dir);
443 targetpath = clean_name(ctx, targetpath);
445 client_set_cur_dir(saved_dir);
449 status = cli_chkpath(targetcli, targetpath);
450 if (!NT_STATUS_IS_OK(status)) {
451 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
452 client_set_cur_dir(saved_dir);
465 /****************************************************************************
467 ****************************************************************************/
469 static int cmd_cd(void)
474 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
477 d_printf("Current directory is %s\n",client_get_cur_dir());
483 /****************************************************************************
485 ****************************************************************************/
487 static int cmd_cd_oneup(void)
492 /*******************************************************************
493 Decide if a file should be operated on.
494 ********************************************************************/
496 static bool do_this_one(struct file_info *finfo)
502 if (finfo->mode & aDIR) {
506 if (*client_get_fileselection() &&
507 !mask_match(finfo->name,client_get_fileselection(),false)) {
508 DEBUG(3,("mask_match %s failed\n", finfo->name));
512 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
513 DEBUG(3,("newer_than %s failed\n", finfo->name));
517 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
518 DEBUG(3,("archive %s failed\n", finfo->name));
525 /****************************************************************************
526 Display info about a file.
527 ****************************************************************************/
529 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
533 TALLOC_CTX *ctx = talloc_tos();
534 NTSTATUS status = NT_STATUS_OK;
536 if (!do_this_one(finfo)) {
540 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
542 d_printf(" %-30s%7.7s %8.0f %s",
544 attrib_string(finfo->mode),
547 dir_total += finfo->size;
552 /* skip if this is . or .. */
553 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
555 /* create absolute filename for cli_ntcreate() FIXME */
556 afname = talloc_asprintf(ctx,
562 return NT_STATUS_NO_MEMORY;
564 /* print file meta date header */
565 d_printf( "FILENAME:%s\n", finfo->name);
566 d_printf( "MODE:%s\n", attrib_string(finfo->mode));
567 d_printf( "SIZE:%.0f\n", (double)finfo->size);
568 d_printf( "MTIME:%s", time_to_asc(t));
569 status = cli_ntcreate(cli_state, afname, 0,
570 CREATE_ACCESS_READ, 0,
571 FILE_SHARE_READ|FILE_SHARE_WRITE,
572 FILE_OPEN, 0x0, 0x0, &fnum);
573 if (!NT_STATUS_IS_OK(status)) {
574 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
575 afname, nt_errstr(status)));
577 struct security_descriptor *sd = NULL;
578 sd = cli_query_secdesc(cli_state, fnum, ctx);
580 DEBUG( 0, ("display_finfo() failed to "
581 "get security descriptor: %s",
582 cli_errstr(cli_state)));
583 status = cli_nt_error(cli_state);
585 display_sec_desc(sd);
594 /****************************************************************************
595 Accumulate size of a file.
596 ****************************************************************************/
598 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
601 if (do_this_one(finfo)) {
602 dir_total += finfo->size;
607 static bool do_list_recurse;
608 static bool do_list_dirs;
609 static char *do_list_queue = 0;
610 static long do_list_queue_size = 0;
611 static long do_list_queue_start = 0;
612 static long do_list_queue_end = 0;
613 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
616 /****************************************************************************
617 Functions for do_list_queue.
618 ****************************************************************************/
621 * The do_list_queue is a NUL-separated list of strings stored in a
622 * char*. Since this is a FIFO, we keep track of the beginning and
623 * ending locations of the data in the queue. When we overflow, we
624 * double the size of the char*. When the start of the data passes
625 * the midpoint, we move everything back. This is logically more
626 * complex than a linked list, but easier from a memory management
627 * angle. In any memory error condition, do_list_queue is reset.
628 * Functions check to ensure that do_list_queue is non-NULL before
632 static void reset_do_list_queue(void)
634 SAFE_FREE(do_list_queue);
635 do_list_queue_size = 0;
636 do_list_queue_start = 0;
637 do_list_queue_end = 0;
640 static void init_do_list_queue(void)
642 reset_do_list_queue();
643 do_list_queue_size = 1024;
644 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
645 if (do_list_queue == 0) {
646 d_printf("malloc fail for size %d\n",
647 (int)do_list_queue_size);
648 reset_do_list_queue();
650 memset(do_list_queue, 0, do_list_queue_size);
654 static void adjust_do_list_queue(void)
657 * If the starting point of the queue is more than half way through,
658 * move everything toward the beginning.
661 if (do_list_queue == NULL) {
662 DEBUG(4,("do_list_queue is empty\n"));
663 do_list_queue_start = do_list_queue_end = 0;
667 if (do_list_queue_start == do_list_queue_end) {
668 DEBUG(4,("do_list_queue is empty\n"));
669 do_list_queue_start = do_list_queue_end = 0;
670 *do_list_queue = '\0';
671 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
672 DEBUG(4,("sliding do_list_queue backward\n"));
673 memmove(do_list_queue,
674 do_list_queue + do_list_queue_start,
675 do_list_queue_end - do_list_queue_start);
676 do_list_queue_end -= do_list_queue_start;
677 do_list_queue_start = 0;
681 static void add_to_do_list_queue(const char *entry)
683 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
684 while (new_end > do_list_queue_size) {
685 do_list_queue_size *= 2;
686 DEBUG(4,("enlarging do_list_queue to %d\n",
687 (int)do_list_queue_size));
688 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
689 if (! do_list_queue) {
690 d_printf("failure enlarging do_list_queue to %d bytes\n",
691 (int)do_list_queue_size);
692 reset_do_list_queue();
694 memset(do_list_queue + do_list_queue_size / 2,
695 0, do_list_queue_size / 2);
699 safe_strcpy_base(do_list_queue + do_list_queue_end,
700 entry, do_list_queue, do_list_queue_size);
701 do_list_queue_end = new_end;
702 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
703 entry, (int)do_list_queue_start, (int)do_list_queue_end));
707 static char *do_list_queue_head(void)
709 return do_list_queue + do_list_queue_start;
712 static void remove_do_list_queue_head(void)
714 if (do_list_queue_end > do_list_queue_start) {
715 do_list_queue_start += strlen(do_list_queue_head()) + 1;
716 adjust_do_list_queue();
717 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
718 (int)do_list_queue_start, (int)do_list_queue_end));
722 static int do_list_queue_empty(void)
724 return (! (do_list_queue && *do_list_queue));
727 /****************************************************************************
728 A helper for do_list.
729 ****************************************************************************/
731 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
732 const char *mask, void *state)
734 struct cli_state *cli_state = (struct cli_state *)state;
735 TALLOC_CTX *ctx = talloc_tos();
737 char *dir_end = NULL;
738 NTSTATUS status = NT_STATUS_OK;
740 /* Work out the directory. */
741 dir = talloc_strdup(ctx, mask);
743 return NT_STATUS_NO_MEMORY;
745 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
749 if (f->mode & aDIR) {
750 if (do_list_dirs && do_this_one(f)) {
751 status = do_list_fn(cli_state, f, dir);
752 if (!NT_STATUS_IS_OK(status)) {
756 if (do_list_recurse &&
758 !strequal(f->name,".") &&
759 !strequal(f->name,"..")) {
764 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
766 return NT_STATUS_UNSUCCESSFUL;
769 mask2 = talloc_asprintf(ctx,
775 return NT_STATUS_NO_MEMORY;
777 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
783 mask2 = talloc_asprintf_append(mask2,
789 return NT_STATUS_NO_MEMORY;
791 add_to_do_list_queue(mask2);
798 if (do_this_one(f)) {
799 status = do_list_fn(cli_state, f, dir);
805 /****************************************************************************
806 A wrapper around cli_list that adds recursion.
807 ****************************************************************************/
809 NTSTATUS do_list(const char *mask,
811 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
816 static int in_do_list = 0;
817 TALLOC_CTX *ctx = talloc_tos();
818 struct cli_state *targetcli = NULL;
819 char *targetpath = NULL;
820 NTSTATUS ret_status = NT_STATUS_OK;
821 NTSTATUS status = NT_STATUS_OK;
823 if (in_do_list && rec) {
824 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
830 do_list_recurse = rec;
835 init_do_list_queue();
836 add_to_do_list_queue(mask);
838 while (!do_list_queue_empty()) {
840 * Need to copy head so that it doesn't become
841 * invalid inside the call to cli_list. This
842 * would happen if the list were expanded
844 * Fix from E. Jay Berkenbilt (ejb@ql.org)
846 char *head = talloc_strdup(ctx, do_list_queue_head());
849 return NT_STATUS_NO_MEMORY;
854 if ( !cli_resolve_path(ctx, "", auth_info, cli, head, &targetcli, &targetpath ) ) {
855 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
856 remove_do_list_queue_head();
860 status = cli_list(targetcli, targetpath, attribute,
861 do_list_helper, targetcli);
862 if (!NT_STATUS_IS_OK(status)) {
863 d_printf("%s listing %s\n",
864 nt_errstr(status), targetpath);
867 remove_do_list_queue_head();
868 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
869 char *next_file = do_list_queue_head();
871 if ((strlen(next_file) >= 2) &&
872 (next_file[strlen(next_file) - 1] == '*') &&
873 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
874 save_ch = next_file +
875 strlen(next_file) - 2;
878 /* cwd is only used if showacls is on */
879 client_set_cwd(next_file);
882 if (!showacls) /* don't disturbe the showacls output */
883 d_printf("\n%s\n",next_file);
885 *save_ch = CLI_DIRSEP_CHAR;
889 TALLOC_FREE(targetpath);
893 if (cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetpath)) {
895 status = cli_list(targetcli, targetpath, attribute,
896 do_list_helper, targetcli);
897 if (!NT_STATUS_IS_OK(status)) {
898 d_printf("%s listing %s\n",
899 nt_errstr(status), targetpath);
902 TALLOC_FREE(targetpath);
904 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
905 ret_status = cli_nt_error(cli);
910 reset_do_list_queue();
914 /****************************************************************************
915 Get a directory listing.
916 ****************************************************************************/
918 static int cmd_dir(void)
920 TALLOC_CTX *ctx = talloc_tos();
921 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
928 mask = talloc_strdup(ctx, client_get_cur_dir());
933 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
935 if (*buf == CLI_DIRSEP_CHAR) {
936 mask = talloc_strdup(ctx, buf);
938 mask = talloc_asprintf_append(mask, "%s", buf);
941 mask = talloc_asprintf_append(mask, "*");
948 /* cwd is only used if showacls is on */
949 client_set_cwd(client_get_cur_dir());
952 status = do_list(mask, attribute, display_finfo, recurse, true);
953 if (!NT_STATUS_IS_OK(status)) {
959 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
964 /****************************************************************************
965 Get a directory listing.
966 ****************************************************************************/
968 static int cmd_du(void)
970 TALLOC_CTX *ctx = talloc_tos();
971 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
978 mask = talloc_strdup(ctx, client_get_cur_dir());
982 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
983 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
989 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
991 if (*buf == CLI_DIRSEP_CHAR) {
992 mask = talloc_strdup(ctx, buf);
994 mask = talloc_asprintf_append(mask, "%s", buf);
997 mask = talloc_strdup(ctx, "*");
1000 status = do_list(mask, attribute, do_du, recurse, true);
1001 if (!NT_STATUS_IS_OK(status)) {
1007 d_printf("Total number of bytes: %.0f\n", dir_total);
1012 static int cmd_echo(void)
1014 TALLOC_CTX *ctx = talloc_tos();
1019 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1020 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1021 d_printf("echo <num> <data>\n");
1025 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1027 if (!NT_STATUS_IS_OK(status)) {
1028 d_printf("echo failed: %s\n", nt_errstr(status));
1035 /****************************************************************************
1036 Get a file from rname to lname
1037 ****************************************************************************/
1039 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1041 int *pfd = (int *)priv;
1042 if (writefile(*pfd, buf, n) == -1) {
1043 return map_nt_error_from_unix(errno);
1045 return NT_STATUS_OK;
1048 static int do_get(const char *rname, const char *lname_in, bool reget)
1050 TALLOC_CTX *ctx = talloc_tos();
1053 bool newhandle = false;
1054 struct timespec tp_start;
1058 SMB_OFF_T nread = 0;
1060 struct cli_state *targetcli = NULL;
1061 char *targetname = NULL;
1065 lname = talloc_strdup(ctx, lname_in);
1074 if (!cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli, &targetname ) ) {
1075 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1079 clock_gettime_mono(&tp_start);
1081 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1082 if (!NT_STATUS_IS_OK(status)) {
1083 d_printf("%s opening remote file %s\n", nt_errstr(status),
1088 if(!strcmp(lname,"-")) {
1089 handle = fileno(stdout);
1092 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
1094 start = sys_lseek(handle, 0, SEEK_END);
1096 d_printf("Error seeking local file\n");
1101 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1106 d_printf("Error opening local file %s\n",lname);
1111 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
1112 targetcli, fnum, &attr, &size, NULL, NULL,
1113 NULL, NULL, NULL)) &&
1114 !NT_STATUS_IS_OK(cli_getattrE(targetcli, fnum,
1115 &attr, &size, NULL, NULL, NULL))) {
1116 d_printf("getattrib: %s\n",cli_errstr(targetcli));
1120 DEBUG(1,("getting file %s of size %.0f as %s ",
1121 rname, (double)size, lname));
1123 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1124 writefile_sink, (void *)&handle, &nread);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 d_fprintf(stderr, "parallel_read returned %s\n",
1128 cli_close(targetcli, fnum);
1132 status = cli_close(targetcli, fnum);
1133 if (!NT_STATUS_IS_OK(status)) {
1134 d_printf("Error %s closing remote file\n", nt_errstr(status));
1142 if (archive_level >= 2 && (attr & aARCH)) {
1143 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
1147 struct timespec tp_end;
1150 clock_gettime_mono(&tp_end);
1151 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1152 get_total_time_ms += this_time;
1153 get_total_size += nread;
1155 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1156 nread / (1.024*this_time + 1.0e-4),
1157 get_total_size / (1.024*get_total_time_ms)));
1160 TALLOC_FREE(targetname);
1164 /****************************************************************************
1166 ****************************************************************************/
1168 static int cmd_get(void)
1170 TALLOC_CTX *ctx = talloc_tos();
1175 rname = talloc_strdup(ctx, client_get_cur_dir());
1180 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1181 d_printf("get <filename> [localname]\n");
1184 rname = talloc_asprintf_append(rname, "%s", fname);
1188 rname = clean_name(ctx, rname);
1193 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1198 return do_get(rname, lname, false);
1201 /****************************************************************************
1202 Do an mget operation on one file.
1203 ****************************************************************************/
1205 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1208 TALLOC_CTX *ctx = talloc_tos();
1209 NTSTATUS status = NT_STATUS_OK;
1212 char *saved_curdir = NULL;
1213 char *mget_mask = NULL;
1214 char *new_cd = NULL;
1217 return NT_STATUS_OK;
1220 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1221 return NT_STATUS_OK;
1224 d_printf("mget aborted\n");
1225 return NT_STATUS_UNSUCCESSFUL;
1228 if (finfo->mode & aDIR) {
1229 if (asprintf(&quest,
1230 "Get directory %s? ",finfo->name) < 0) {
1231 return NT_STATUS_NO_MEMORY;
1234 if (asprintf(&quest,
1235 "Get file %s? ",finfo->name) < 0) {
1236 return NT_STATUS_NO_MEMORY;
1240 if (prompt && !yesno(quest)) {
1242 return NT_STATUS_OK;
1246 if (!(finfo->mode & aDIR)) {
1247 rname = talloc_asprintf(ctx,
1249 client_get_cur_dir(),
1252 return NT_STATUS_NO_MEMORY;
1254 do_get(rname, finfo->name, false);
1256 return NT_STATUS_OK;
1259 /* handle directories */
1260 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1261 if (!saved_curdir) {
1262 return NT_STATUS_NO_MEMORY;
1265 new_cd = talloc_asprintf(ctx,
1267 client_get_cur_dir(),
1271 return NT_STATUS_NO_MEMORY;
1273 client_set_cur_dir(new_cd);
1275 string_replace(finfo->name,'\\','/');
1277 strlower_m(finfo->name);
1280 if (!directory_exist(finfo->name) &&
1281 mkdir(finfo->name,0777) != 0) {
1282 d_printf("failed to create directory %s\n",finfo->name);
1283 client_set_cur_dir(saved_curdir);
1284 return map_nt_error_from_unix(errno);
1287 if (chdir(finfo->name) != 0) {
1288 d_printf("failed to chdir to directory %s\n",finfo->name);
1289 client_set_cur_dir(saved_curdir);
1290 return map_nt_error_from_unix(errno);
1293 mget_mask = talloc_asprintf(ctx,
1295 client_get_cur_dir());
1298 return NT_STATUS_NO_MEMORY;
1301 status = do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,false, true);
1302 if (!NT_STATUS_IS_OK(status)) {
1306 if (chdir("..") == -1) {
1307 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1309 return map_nt_error_from_unix(errno);
1311 client_set_cur_dir(saved_curdir);
1312 TALLOC_FREE(mget_mask);
1313 TALLOC_FREE(saved_curdir);
1314 TALLOC_FREE(new_cd);
1315 return NT_STATUS_OK;
1318 /****************************************************************************
1319 View the file using the pager.
1320 ****************************************************************************/
1322 static int cmd_more(void)
1324 TALLOC_CTX *ctx = talloc_tos();
1328 char *pager_cmd = NULL;
1333 rname = talloc_strdup(ctx, client_get_cur_dir());
1338 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1342 fd = mkstemp(lname);
1344 d_printf("failed to create temporary file for more\n");
1349 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1350 d_printf("more <filename>\n");
1354 rname = talloc_asprintf_append(rname, "%s", fname);
1358 rname = clean_name(ctx,rname);
1363 rc = do_get(rname, lname, false);
1365 pager=getenv("PAGER");
1367 pager_cmd = talloc_asprintf(ctx,
1369 (pager? pager:PAGER),
1374 if (system(pager_cmd) == -1) {
1375 d_printf("system command '%s' returned -1\n",
1383 /****************************************************************************
1385 ****************************************************************************/
1387 static int cmd_mget(void)
1389 TALLOC_CTX *ctx = talloc_tos();
1390 uint16 attribute = aSYSTEM | aHIDDEN;
1391 char *mget_mask = NULL;
1393 NTSTATUS status = NT_STATUS_OK;
1401 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1403 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1407 if (*buf == CLI_DIRSEP_CHAR) {
1408 mget_mask = talloc_strdup(ctx, buf);
1410 mget_mask = talloc_asprintf_append(mget_mask,
1416 status = do_list(mget_mask, attribute, do_mget, false, true);
1417 if (!NT_STATUS_IS_OK(status)) {
1422 if (mget_mask == NULL) {
1423 d_printf("nothing to mget\n");
1428 mget_mask = talloc_asprintf(ctx,
1430 client_get_cur_dir());
1434 status = do_list(mget_mask, attribute, do_mget, false, true);
1435 if (!NT_STATUS_IS_OK(status)) {
1443 /****************************************************************************
1444 Make a directory of name "name".
1445 ****************************************************************************/
1447 static bool do_mkdir(const char *name)
1449 TALLOC_CTX *ctx = talloc_tos();
1450 struct cli_state *targetcli;
1451 char *targetname = NULL;
1454 if (!cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli, &targetname)) {
1455 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1459 status = cli_mkdir(targetcli, targetname);
1460 if (!NT_STATUS_IS_OK(status)) {
1461 d_printf("%s making remote directory %s\n",
1462 nt_errstr(status),name);
1469 /****************************************************************************
1470 Show 8.3 name of a file.
1471 ****************************************************************************/
1473 static bool do_altname(const char *name)
1478 status = cli_qpathinfo_alt_name(cli, name, altname);
1479 if (!NT_STATUS_IS_OK(status)) {
1480 d_printf("%s getting alt name for %s\n",
1481 nt_errstr(status),name);
1484 d_printf("%s\n", altname);
1489 /****************************************************************************
1491 ****************************************************************************/
1493 static int cmd_quit(void)
1501 /****************************************************************************
1503 ****************************************************************************/
1505 static int cmd_mkdir(void)
1507 TALLOC_CTX *ctx = talloc_tos();
1511 mask = talloc_strdup(ctx, client_get_cur_dir());
1516 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1518 d_printf("mkdir <dirname>\n");
1522 mask = talloc_asprintf_append(mask, "%s", buf);
1530 struct cli_state *targetcli;
1531 char *targetname = NULL;
1535 ddir2 = talloc_strdup(ctx, "");
1540 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
1544 ddir = talloc_strdup(ctx, targetname);
1548 trim_char(ddir,'.','\0');
1549 p = strtok_r(ddir, "/\\", &saveptr);
1551 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1555 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1558 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1562 p = strtok_r(NULL, "/\\", &saveptr);
1571 /****************************************************************************
1573 ****************************************************************************/
1575 static int cmd_altname(void)
1577 TALLOC_CTX *ctx = talloc_tos();
1581 name = talloc_strdup(ctx, client_get_cur_dir());
1586 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1587 d_printf("altname <file>\n");
1590 name = talloc_asprintf_append(name, "%s", buf);
1598 static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1600 char *attrs = TALLOC_ZERO_ARRAY(mem_ctx, char, 17);
1603 if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1604 if (mode & FILE_ATTRIBUTE_READONLY) {
1607 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1610 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1613 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1616 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1623 /****************************************************************************
1624 Show all info we can get
1625 ****************************************************************************/
1627 static int do_allinfo(const char *name)
1630 struct timespec b_time, a_time, m_time, c_time;
1636 unsigned int num_streams;
1637 struct stream_struct *streams;
1643 status = cli_qpathinfo_alt_name(cli, name, altname);
1644 if (!NT_STATUS_IS_OK(status)) {
1645 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1649 d_printf("altname: %s\n", altname);
1651 status = cli_qpathinfo2(cli, name, &b_time, &a_time, &m_time, &c_time,
1652 &size, &mode, &ino);
1653 if (!NT_STATUS_IS_OK(status)) {
1654 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1659 unix_timespec_to_nt_time(&tmp, b_time);
1660 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1662 unix_timespec_to_nt_time(&tmp, a_time);
1663 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1665 unix_timespec_to_nt_time(&tmp, m_time);
1666 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1668 unix_timespec_to_nt_time(&tmp, c_time);
1669 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1671 d_printf("attributes: %s\n", attr_str(talloc_tos(), mode));
1673 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1675 if (!NT_STATUS_IS_OK(status)) {
1676 d_printf("%s getting streams for %s\n", nt_errstr(status),
1681 for (i=0; i<num_streams; i++) {
1682 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1683 (unsigned long long)streams[i].size);
1686 status = cli_open(cli, name, O_RDONLY, DENY_NONE, &fnum);
1687 if (!NT_STATUS_IS_OK(status)) {
1689 * Ignore failure, it does not hurt if we can't list
1694 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1695 true, &snapshots, &num_snapshots);
1696 if (!NT_STATUS_IS_OK(status)) {
1697 cli_close(cli, fnum);
1701 for (i=0; i<num_snapshots; i++) {
1704 d_printf("%s\n", snapshots[i]);
1705 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1706 snapshots[i], name);
1707 status = cli_qpathinfo2(cli, snap_name, &b_time, &a_time,
1708 &m_time, &c_time, &size,
1710 if (!NT_STATUS_IS_OK(status)) {
1711 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1712 snap_name, nt_errstr(status));
1713 TALLOC_FREE(snap_name);
1716 unix_timespec_to_nt_time(&tmp, b_time);
1717 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1718 unix_timespec_to_nt_time(&tmp, a_time);
1719 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1720 unix_timespec_to_nt_time(&tmp, m_time);
1721 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1722 unix_timespec_to_nt_time(&tmp, c_time);
1723 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1724 d_printf("size: %d\n", (int)size);
1727 TALLOC_FREE(snapshots);
1732 /****************************************************************************
1733 Show all info we can get
1734 ****************************************************************************/
1736 static int cmd_allinfo(void)
1738 TALLOC_CTX *ctx = talloc_tos();
1742 name = talloc_strdup(ctx, client_get_cur_dir());
1747 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1748 d_printf("allinfo <file>\n");
1751 name = talloc_asprintf_append(name, "%s", buf);
1761 /****************************************************************************
1763 ****************************************************************************/
1765 static int do_put(const char *rname, const char *lname, bool reput)
1767 TALLOC_CTX *ctx = talloc_tos();
1770 SMB_OFF_T start = 0;
1772 struct timespec tp_start;
1773 struct cli_state *targetcli;
1774 char *targetname = NULL;
1775 struct push_state state;
1778 if (!cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli, &targetname)) {
1779 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1783 clock_gettime_mono(&tp_start);
1786 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1787 if (NT_STATUS_IS_OK(status)) {
1788 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
1789 targetcli, fnum, NULL,
1791 NULL, NULL, NULL)) &&
1792 !NT_STATUS_IS_OK(cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL))) {
1793 d_printf("getattrib: %s\n",cli_errstr(cli));
1798 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1801 if (!NT_STATUS_IS_OK(status)) {
1802 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1806 /* allow files to be piped into smbclient
1809 Note that in this case this function will exit(0) rather
1811 if (!strcmp(lname, "-")) {
1813 /* size of file is not known */
1815 f = x_fopen(lname,O_RDONLY, 0);
1817 if (x_tseek(f, start, SEEK_SET) == -1) {
1818 d_printf("Error seeking local file\n");
1826 d_printf("Error opening local file %s\n",lname);
1830 DEBUG(1,("putting file %s as %s ",lname,
1833 x_setvbuf(f, NULL, X_IOFBF, io_bufsize);
1838 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1840 if (!NT_STATUS_IS_OK(status)) {
1841 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1845 status = cli_close(targetcli, fnum);
1846 if (!NT_STATUS_IS_OK(status)) {
1847 d_printf("%s closing remote file %s\n", nt_errstr(status),
1860 struct timespec tp_end;
1863 clock_gettime_mono(&tp_end);
1864 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1865 put_total_time_ms += this_time;
1866 put_total_size += state.nread;
1868 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1869 state.nread / (1.024*this_time + 1.0e-4),
1870 put_total_size / (1.024*put_total_time_ms)));
1881 /****************************************************************************
1883 ****************************************************************************/
1885 static int cmd_put(void)
1887 TALLOC_CTX *ctx = talloc_tos();
1892 rname = talloc_strdup(ctx, client_get_cur_dir());
1897 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1898 d_printf("put <filename>\n");
1902 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1903 rname = talloc_asprintf_append(rname, "%s", buf);
1905 rname = talloc_asprintf_append(rname, "%s", lname);
1911 rname = clean_name(ctx, rname);
1918 /* allow '-' to represent stdin
1919 jdblair, 24.jun.98 */
1920 if (!file_exist_stat(lname, &st, false) &&
1921 (strcmp(lname,"-"))) {
1922 d_printf("%s does not exist\n",lname);
1927 return do_put(rname, lname, false);
1930 /*************************************
1931 File list structure.
1932 *************************************/
1934 static struct file_list {
1935 struct file_list *prev, *next;
1940 /****************************************************************************
1941 Free a file_list structure.
1942 ****************************************************************************/
1944 static void free_file_list (struct file_list *l_head)
1946 struct file_list *list, *next;
1948 for (list = l_head; list; list = next) {
1950 DLIST_REMOVE(l_head, list);
1951 SAFE_FREE(list->file_path);
1956 /****************************************************************************
1957 Seek in a directory/file list until you get something that doesn't start with
1959 ****************************************************************************/
1961 static bool seek_list(struct file_list *list, char *name)
1964 trim_string(list->file_path,"./","\n");
1965 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1974 /****************************************************************************
1975 Set the file selection mask.
1976 ****************************************************************************/
1978 static int cmd_select(void)
1980 TALLOC_CTX *ctx = talloc_tos();
1981 char *new_fs = NULL;
1982 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
1985 client_set_fileselection(new_fs);
1987 client_set_fileselection("");
1992 /****************************************************************************
1993 Recursive file matching function act as find
1994 match must be always set to true when calling this function
1995 ****************************************************************************/
1997 static int file_find(struct file_list **list, const char *directory,
1998 const char *expression, bool match)
2000 SMB_STRUCT_DIR *dir;
2001 struct file_list *entry;
2002 struct stat statbuf;
2008 dir = sys_opendir(directory);
2012 while ((dname = readdirname(dir))) {
2013 if (!strcmp("..", dname))
2015 if (!strcmp(".", dname))
2018 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
2023 if (!match || !gen_fnmatch(expression, dname)) {
2025 ret = stat(path, &statbuf);
2027 if (S_ISDIR(statbuf.st_mode)) {
2029 ret = file_find(list, path, expression, false);
2032 d_printf("file_find: cannot stat file %s\n", path);
2041 entry = SMB_MALLOC_P(struct file_list);
2043 d_printf("Out of memory in file_find\n");
2047 entry->file_path = path;
2048 entry->isdir = isdir;
2049 DLIST_ADD(*list, entry);
2059 /****************************************************************************
2061 ****************************************************************************/
2063 static int cmd_mput(void)
2065 TALLOC_CTX *ctx = talloc_tos();
2068 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2070 struct file_list *temp_list;
2071 char *quest, *lname, *rname;
2075 ret = file_find(&file_list, ".", p, true);
2077 free_file_list(file_list);
2085 for (temp_list = file_list; temp_list;
2086 temp_list = temp_list->next) {
2089 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2092 trim_string(lname, "./", "/");
2094 /* check if it's a directory */
2095 if (temp_list->isdir) {
2096 /* if (!recurse) continue; */
2099 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2102 if (prompt && !yesno(quest)) { /* No */
2103 /* Skip the directory */
2104 lname[strlen(lname)-1] = '/';
2105 if (!seek_list(temp_list, lname))
2109 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2112 normalize_name(rname);
2113 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2115 DEBUG (0, ("Unable to make dir, skipping..."));
2116 /* Skip the directory */
2117 lname[strlen(lname)-1] = '/';
2118 if (!seek_list(temp_list, lname)) {
2126 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2129 if (prompt && !yesno(quest)) {
2136 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2141 normalize_name(rname);
2143 do_put(rname, lname, false);
2145 free_file_list(file_list);
2154 /****************************************************************************
2156 ****************************************************************************/
2158 static int do_cancel(int job)
2160 if (cli_printjob_del(cli, job)) {
2161 d_printf("Job %d cancelled\n",job);
2164 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
2169 /****************************************************************************
2171 ****************************************************************************/
2173 static int cmd_cancel(void)
2175 TALLOC_CTX *ctx = talloc_tos();
2179 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2180 d_printf("cancel <jobid> ...\n");
2186 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2191 /****************************************************************************
2193 ****************************************************************************/
2195 static int cmd_print(void)
2197 TALLOC_CTX *ctx = talloc_tos();
2202 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2203 d_printf("print <filename>\n");
2207 rname = talloc_strdup(ctx, lname);
2211 p = strrchr_m(rname,'/');
2213 rname = talloc_asprintf(ctx,
2218 if (strequal(lname,"-")) {
2219 rname = talloc_asprintf(ctx,
2227 return do_put(rname, lname, false);
2230 /****************************************************************************
2231 Show a print queue entry.
2232 ****************************************************************************/
2234 static void queue_fn(struct print_job_info *p)
2236 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2239 /****************************************************************************
2241 ****************************************************************************/
2243 static int cmd_queue(void)
2245 cli_print_queue(cli, queue_fn);
2249 /****************************************************************************
2251 ****************************************************************************/
2253 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2256 TALLOC_CTX *ctx = talloc_tos();
2260 mask = talloc_asprintf(ctx,
2266 return NT_STATUS_NO_MEMORY;
2269 if (finfo->mode & aDIR) {
2271 return NT_STATUS_OK;
2274 status = cli_unlink(cli_state, mask, aSYSTEM | aHIDDEN);
2275 if (!NT_STATUS_IS_OK(status)) {
2276 d_printf("%s deleting remote file %s\n",
2277 nt_errstr(status), mask);
2283 /****************************************************************************
2285 ****************************************************************************/
2287 static int cmd_del(void)
2289 TALLOC_CTX *ctx = talloc_tos();
2292 NTSTATUS status = NT_STATUS_OK;
2293 uint16 attribute = aSYSTEM | aHIDDEN;
2299 mask = talloc_strdup(ctx, client_get_cur_dir());
2303 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2304 d_printf("del <filename>\n");
2307 mask = talloc_asprintf_append(mask, "%s", buf);
2312 status = do_list(mask,attribute,do_del,false,false);
2313 if (!NT_STATUS_IS_OK(status)) {
2319 /****************************************************************************
2320 Wildcard delete some files.
2321 ****************************************************************************/
2323 static int cmd_wdel(void)
2325 TALLOC_CTX *ctx = talloc_tos();
2329 struct cli_state *targetcli;
2330 char *targetname = NULL;
2333 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2334 d_printf("wdel 0x<attrib> <wcard>\n");
2338 attribute = (uint16)strtol(buf, (char **)NULL, 16);
2340 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2341 d_printf("wdel 0x<attrib> <wcard>\n");
2345 mask = talloc_asprintf(ctx, "%s%s",
2346 client_get_cur_dir(),
2352 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2353 d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
2357 status = cli_unlink(targetcli, targetname, attribute);
2358 if (!NT_STATUS_IS_OK(status)) {
2359 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2365 /****************************************************************************
2366 ****************************************************************************/
2368 static int cmd_open(void)
2370 TALLOC_CTX *ctx = talloc_tos();
2373 char *targetname = NULL;
2374 struct cli_state *targetcli;
2375 uint16_t fnum = (uint16_t)-1;
2377 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2378 d_printf("open <filename>\n");
2381 mask = talloc_asprintf(ctx,
2383 client_get_cur_dir(),
2389 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2390 d_printf("open %s: %s\n", mask, cli_errstr(cli));
2394 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetname, 0,
2395 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2396 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
2397 if (NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetname, 0,
2399 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
2400 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2402 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2405 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2410 static int cmd_posix_encrypt(void)
2412 TALLOC_CTX *ctx = talloc_tos();
2413 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2415 if (cli->use_kerberos) {
2416 status = cli_gss_smb_encryption_start(cli);
2418 char *domain = NULL;
2420 char *password = NULL;
2422 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2423 d_printf("posix_encrypt domain user password\n");
2427 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2428 d_printf("posix_encrypt domain user password\n");
2432 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2433 d_printf("posix_encrypt domain user password\n");
2437 status = cli_raw_ntlm_smb_encryption_start(cli,
2443 if (!NT_STATUS_IS_OK(status)) {
2444 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2446 d_printf("encryption on\n");
2453 /****************************************************************************
2454 ****************************************************************************/
2456 static int cmd_posix_open(void)
2458 TALLOC_CTX *ctx = talloc_tos();
2461 char *targetname = NULL;
2462 struct cli_state *targetcli;
2466 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2467 d_printf("posix_open <filename> 0<mode>\n");
2470 mask = talloc_asprintf(ctx,
2472 client_get_cur_dir(),
2478 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2479 d_printf("posix_open <filename> 0<mode>\n");
2482 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2484 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2485 d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
2489 if (!NT_STATUS_IS_OK(cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode, &fnum))) {
2490 if (!NT_STATUS_IS_OK(cli_posix_open(targetcli, targetname, O_CREAT|O_RDONLY, mode, &fnum))) {
2491 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
2493 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2496 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
2502 static int cmd_posix_mkdir(void)
2504 TALLOC_CTX *ctx = talloc_tos();
2507 char *targetname = NULL;
2508 struct cli_state *targetcli;
2511 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2512 d_printf("posix_mkdir <filename> 0<mode>\n");
2515 mask = talloc_asprintf(ctx,
2517 client_get_cur_dir(),
2523 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2524 d_printf("posix_mkdir <filename> 0<mode>\n");
2527 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2529 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2530 d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
2534 if (!NT_STATUS_IS_OK(cli_posix_mkdir(targetcli, targetname, mode))) {
2535 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2537 d_printf("posix_mkdir created directory %s\n", targetname);
2542 static int cmd_posix_unlink(void)
2544 TALLOC_CTX *ctx = talloc_tos();
2547 char *targetname = NULL;
2548 struct cli_state *targetcli;
2550 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2551 d_printf("posix_unlink <filename>\n");
2554 mask = talloc_asprintf(ctx,
2556 client_get_cur_dir(),
2562 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2563 d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
2567 if (!NT_STATUS_IS_OK(cli_posix_unlink(targetcli, targetname))) {
2568 d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
2570 d_printf("posix_unlink deleted file %s\n", targetname);
2576 static int cmd_posix_rmdir(void)
2578 TALLOC_CTX *ctx = talloc_tos();
2581 char *targetname = NULL;
2582 struct cli_state *targetcli;
2584 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2585 d_printf("posix_rmdir <filename>\n");
2588 mask = talloc_asprintf(ctx,
2590 client_get_cur_dir(),
2596 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2597 d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
2601 if (!NT_STATUS_IS_OK(cli_posix_rmdir(targetcli, targetname))) {
2602 d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
2604 d_printf("posix_rmdir deleted directory %s\n", targetname);
2610 static int cmd_close(void)
2612 TALLOC_CTX *ctx = talloc_tos();
2616 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2617 d_printf("close <fnum>\n");
2622 /* We really should use the targetcli here.... */
2623 if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
2624 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
2630 static int cmd_posix(void)
2632 TALLOC_CTX *ctx = talloc_tos();
2633 uint16 major, minor;
2634 uint32 caplow, caphigh;
2638 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2639 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2643 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
2645 if (!NT_STATUS_IS_OK(status)) {
2646 d_printf("Can't get UNIX CIFS extensions version from "
2647 "server: %s\n", nt_errstr(status));
2651 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2653 caps = talloc_strdup(ctx, "");
2657 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2658 caps = talloc_asprintf_append(caps, "locks ");
2663 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2664 caps = talloc_asprintf_append(caps, "acls ");
2669 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2670 caps = talloc_asprintf_append(caps, "eas ");
2675 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2676 caps = talloc_asprintf_append(caps, "pathnames ");
2681 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2682 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2687 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2688 caps = talloc_asprintf_append(caps, "large_read ");
2693 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2694 caps = talloc_asprintf_append(caps, "large_write ");
2699 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2700 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2705 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2706 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2712 if (*caps && caps[strlen(caps)-1] == ' ') {
2713 caps[strlen(caps)-1] = '\0';
2716 d_printf("Server supports CIFS capabilities %s\n", caps);
2718 status = cli_set_unix_extensions_capabilities(cli, major, minor,
2720 if (!NT_STATUS_IS_OK(status)) {
2721 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2726 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2727 CLI_DIRSEP_CHAR = '/';
2728 *CLI_DIRSEP_STR = '/';
2729 client_set_cur_dir(CLI_DIRSEP_STR);
2735 static int cmd_lock(void)
2737 TALLOC_CTX *ctx = talloc_tos();
2739 uint64_t start, len;
2740 enum brl_type lock_type;
2743 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2744 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2749 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2750 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2754 if (*buf == 'r' || *buf == 'R') {
2755 lock_type = READ_LOCK;
2756 } else if (*buf == 'w' || *buf == 'W') {
2757 lock_type = WRITE_LOCK;
2759 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2763 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2764 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2768 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2770 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2771 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2775 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2777 if (!NT_STATUS_IS_OK(cli_posix_lock(cli, fnum, start, len, true, lock_type))) {
2778 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
2784 static int cmd_unlock(void)
2786 TALLOC_CTX *ctx = talloc_tos();
2788 uint64_t start, len;
2791 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2792 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2797 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2798 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2802 start = (uint64_t)strtol(buf, (char **)NULL, 16);
2804 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2805 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2809 len = (uint64_t)strtol(buf, (char **)NULL, 16);
2811 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli, fnum, start, len))) {
2812 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
2819 /****************************************************************************
2821 ****************************************************************************/
2823 static int cmd_rmdir(void)
2825 TALLOC_CTX *ctx = talloc_tos();
2828 char *targetname = NULL;
2829 struct cli_state *targetcli;
2831 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2832 d_printf("rmdir <dirname>\n");
2835 mask = talloc_asprintf(ctx,
2837 client_get_cur_dir(),
2843 if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
2844 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
2848 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetname))) {
2849 d_printf("%s removing remote directory file %s\n",
2850 cli_errstr(targetcli),mask);
2856 /****************************************************************************
2858 ****************************************************************************/
2860 static int cmd_link(void)
2862 TALLOC_CTX *ctx = talloc_tos();
2863 char *oldname = NULL;
2864 char *newname = NULL;
2867 char *targetname = NULL;
2868 struct cli_state *targetcli;
2870 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2871 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2872 d_printf("link <oldname> <newname>\n");
2875 oldname = talloc_asprintf(ctx,
2877 client_get_cur_dir(),
2882 newname = talloc_asprintf(ctx,
2884 client_get_cur_dir(),
2890 if (!cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli, &targetname)) {
2891 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2895 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2896 d_printf("Server doesn't support UNIX CIFS calls.\n");
2900 if (!NT_STATUS_IS_OK(cli_posix_hardlink(targetcli, targetname, newname))) {
2901 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
2907 /****************************************************************************
2909 ****************************************************************************/
2911 static int cmd_readlink(void)
2913 TALLOC_CTX *ctx = talloc_tos();
2916 char *targetname = NULL;
2917 char linkname[PATH_MAX+1];
2918 struct cli_state *targetcli;
2920 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2921 d_printf("readlink <name>\n");
2924 name = talloc_asprintf(ctx,
2926 client_get_cur_dir(),
2932 if (!cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli, &targetname)) {
2933 d_printf("readlink %s: %s\n", name, cli_errstr(cli));
2937 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2938 d_printf("Server doesn't support UNIX CIFS calls.\n");
2942 if (!NT_STATUS_IS_OK(cli_posix_readlink(targetcli, name,
2943 linkname, PATH_MAX+1))) {
2944 d_printf("%s readlink on file %s\n",
2945 cli_errstr(targetcli), name);
2949 d_printf("%s -> %s\n", name, linkname);
2955 /****************************************************************************
2957 ****************************************************************************/
2959 static int cmd_symlink(void)
2961 TALLOC_CTX *ctx = talloc_tos();
2962 char *oldname = NULL;
2963 char *newname = NULL;
2966 struct cli_state *newcli;
2968 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2969 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2970 d_printf("symlink <oldname> <newname>\n");
2973 /* Oldname (link target) must be an untouched blob. */
2976 newname = talloc_asprintf(ctx,
2978 client_get_cur_dir(),
2984 /* New name must be present in share namespace. */
2985 if (!cli_resolve_path(ctx, "", auth_info, cli, newname, &newcli, &newname)) {
2986 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2990 if (!SERVER_HAS_UNIX_CIFS(newcli)) {
2991 d_printf("Server doesn't support UNIX CIFS calls.\n");
2995 if (!NT_STATUS_IS_OK(cli_posix_symlink(newcli, oldname, newname))) {
2996 d_printf("%s symlinking files (%s -> %s)\n",
2997 cli_errstr(newcli), newname, newname);
3004 /****************************************************************************
3006 ****************************************************************************/
3008 static int cmd_chmod(void)
3010 TALLOC_CTX *ctx = talloc_tos();
3014 char *targetname = NULL;
3015 struct cli_state *targetcli;
3018 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3019 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3020 d_printf("chmod mode file\n");
3023 src = talloc_asprintf(ctx,
3025 client_get_cur_dir(),
3031 mode = (mode_t)strtol(buf, NULL, 8);
3033 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3034 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
3038 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3039 d_printf("Server doesn't support UNIX CIFS calls.\n");
3043 if (!NT_STATUS_IS_OK(cli_posix_chmod(targetcli, targetname, mode))) {
3044 d_printf("%s chmod file %s 0%o\n",
3045 cli_errstr(targetcli), src, (unsigned int)mode);
3052 static const char *filetype_to_str(mode_t mode)
3054 if (S_ISREG(mode)) {
3055 return "regular file";
3056 } else if (S_ISDIR(mode)) {
3060 if (S_ISCHR(mode)) {
3061 return "character device";
3065 if (S_ISBLK(mode)) {
3066 return "block device";
3070 if (S_ISFIFO(mode)) {
3075 if (S_ISLNK(mode)) {
3076 return "symbolic link";
3080 if (S_ISSOCK(mode)) {
3087 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3096 static char *unix_mode_to_str(char *s, mode_t m)
3099 const char *str = filetype_to_str(m);
3115 *p++ = str[1] == 'y' ? 'l' : 's';
3122 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3123 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3124 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3125 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3126 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3127 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3128 *p++ = rwx_to_str(m, S_IROTH, 'r');
3129 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3130 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3135 /****************************************************************************
3136 Utility function for UNIX getfacl.
3137 ****************************************************************************/
3139 static char *perms_to_string(fstring permstr, unsigned char perms)
3141 fstrcpy(permstr, "---");
3142 if (perms & SMB_POSIX_ACL_READ) {
3145 if (perms & SMB_POSIX_ACL_WRITE) {
3148 if (perms & SMB_POSIX_ACL_EXECUTE) {
3154 /****************************************************************************
3156 ****************************************************************************/
3158 static int cmd_getfacl(void)
3160 TALLOC_CTX *ctx = talloc_tos();
3163 char *targetname = NULL;
3164 struct cli_state *targetcli;
3165 uint16 major, minor;
3166 uint32 caplow, caphigh;
3167 char *retbuf = NULL;
3169 SMB_STRUCT_STAT sbuf;
3170 uint16 num_file_acls = 0;
3171 uint16 num_dir_acls = 0;
3175 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3176 d_printf("getfacl filename\n");
3179 src = talloc_asprintf(ctx,
3181 client_get_cur_dir(),
3187 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3188 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3192 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3193 d_printf("Server doesn't support UNIX CIFS calls.\n");
3197 status = cli_unix_extensions_version(targetcli, &major, &minor,
3199 if (!NT_STATUS_IS_OK(status)) {
3200 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3205 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3206 d_printf("This server supports UNIX extensions "
3207 "but doesn't support POSIX ACLs.\n");
3211 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3212 d_printf("%s getfacl doing a stat on file %s\n",
3213 cli_errstr(targetcli), src);
3217 if (!NT_STATUS_IS_OK(cli_posix_getfacl(targetcli, targetname, ctx, &rb_size, &retbuf))) {
3218 d_printf("%s getfacl file %s\n",
3219 cli_errstr(targetcli), src);
3223 /* ToDo : Print out the ACL values. */
3224 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3225 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3226 src, (unsigned int)CVAL(retbuf,0) );
3230 num_file_acls = SVAL(retbuf,2);
3231 num_dir_acls = SVAL(retbuf,4);
3232 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3233 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3235 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3236 (unsigned int)rb_size);
3240 d_printf("# file: %s\n", src);
3241 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3243 if (num_file_acls == 0 && num_dir_acls == 0) {
3244 d_printf("No acls found.\n");
3247 for (i = 0; i < num_file_acls; i++) {
3250 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3251 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3254 case SMB_POSIX_ACL_USER_OBJ:
3257 case SMB_POSIX_ACL_USER:
3258 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3259 d_printf("user:%u:", uorg);
3261 case SMB_POSIX_ACL_GROUP_OBJ:
3262 d_printf("group::");
3264 case SMB_POSIX_ACL_GROUP:
3265 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3266 d_printf("group:%u:", uorg);
3268 case SMB_POSIX_ACL_MASK:
3271 case SMB_POSIX_ACL_OTHER:
3272 d_printf("other::");
3275 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3276 src, (unsigned int)tagtype );
3281 d_printf("%s\n", perms_to_string(permstring, perms));
3284 for (i = 0; i < num_dir_acls; i++) {
3287 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3288 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3291 case SMB_POSIX_ACL_USER_OBJ:
3292 d_printf("default:user::");
3294 case SMB_POSIX_ACL_USER:
3295 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3296 d_printf("default:user:%u:", uorg);
3298 case SMB_POSIX_ACL_GROUP_OBJ:
3299 d_printf("default:group::");
3301 case SMB_POSIX_ACL_GROUP:
3302 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3303 d_printf("default:group:%u:", uorg);
3305 case SMB_POSIX_ACL_MASK:
3306 d_printf("default:mask::");
3308 case SMB_POSIX_ACL_OTHER:
3309 d_printf("default:other::");
3312 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3313 src, (unsigned int)tagtype );
3318 d_printf("%s\n", perms_to_string(permstring, perms));
3324 static void printf_cb(const char *buf, void *private_data)
3329 /****************************************************************************
3330 Get the EA list of a file
3331 ****************************************************************************/
3333 static int cmd_geteas(void)
3335 TALLOC_CTX *ctx = talloc_tos();
3338 char *targetname = NULL;
3339 struct cli_state *targetcli;
3342 struct ea_struct *eas;
3344 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3345 d_printf("geteas filename\n");
3348 src = talloc_asprintf(ctx,
3350 client_get_cur_dir(),
3356 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3358 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3362 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3364 if (!NT_STATUS_IS_OK(status)) {
3365 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3369 for (i=0; i<num_eas; i++) {
3370 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3371 dump_data_cb(eas[i].value.data, eas[i].value.length, false,
3381 /****************************************************************************
3383 ****************************************************************************/
3385 static int cmd_setea(void)
3387 TALLOC_CTX *ctx = talloc_tos();
3390 char *eaname = NULL;
3391 char *eavalue = NULL;
3392 char *targetname = NULL;
3393 struct cli_state *targetcli;
3396 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
3397 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
3398 d_printf("setea filename eaname value\n");
3401 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
3402 eavalue = talloc_strdup(ctx, "");
3404 src = talloc_asprintf(ctx,
3406 client_get_cur_dir(),
3412 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
3414 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3418 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
3420 if (!NT_STATUS_IS_OK(status)) {
3421 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
3428 /****************************************************************************
3430 ****************************************************************************/
3432 static int cmd_stat(void)
3434 TALLOC_CTX *ctx = talloc_tos();
3437 char *targetname = NULL;
3438 struct cli_state *targetcli;
3440 SMB_STRUCT_STAT sbuf;
3444 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3445 d_printf("stat file\n");
3448 src = talloc_asprintf(ctx,
3450 client_get_cur_dir(),
3456 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3457 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3461 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3462 d_printf("Server doesn't support UNIX CIFS calls.\n");
3466 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli, targetname, &sbuf))) {
3467 d_printf("%s stat file %s\n",
3468 cli_errstr(targetcli), src);
3472 /* Print out the stat values. */
3473 d_printf("File: %s\n", src);
3474 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3475 (double)sbuf.st_ex_size,
3476 (unsigned int)sbuf.st_ex_blocks,
3477 filetype_to_str(sbuf.st_ex_mode));
3479 #if defined(S_ISCHR) && defined(S_ISBLK)
3480 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
3481 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3482 (double)sbuf.st_ex_ino,
3483 (unsigned int)sbuf.st_ex_nlink,
3484 unix_dev_major(sbuf.st_ex_rdev),
3485 unix_dev_minor(sbuf.st_ex_rdev));
3488 d_printf("Inode: %.0f\tLinks: %u\n",
3489 (double)sbuf.st_ex_ino,
3490 (unsigned int)sbuf.st_ex_nlink);
3492 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3493 ((int)sbuf.st_ex_mode & 0777),
3494 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
3495 (unsigned int)sbuf.st_ex_uid,
3496 (unsigned int)sbuf.st_ex_gid);
3498 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
3499 lt = localtime(&tmp_time);
3501 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3503 fstrcpy(mode_str, "unknown");
3505 d_printf("Access: %s\n", mode_str);
3507 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3508 lt = localtime(&tmp_time);
3510 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3512 fstrcpy(mode_str, "unknown");
3514 d_printf("Modify: %s\n", mode_str);
3516 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
3517 lt = localtime(&tmp_time);
3519 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3521 fstrcpy(mode_str, "unknown");
3523 d_printf("Change: %s\n", mode_str);
3529 /****************************************************************************
3531 ****************************************************************************/
3533 static int cmd_chown(void)
3535 TALLOC_CTX *ctx = talloc_tos();
3539 char *buf, *buf2, *buf3;
3540 struct cli_state *targetcli;
3541 char *targetname = NULL;
3543 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3544 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3545 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3546 d_printf("chown uid gid file\n");
3550 uid = (uid_t)atoi(buf);
3551 gid = (gid_t)atoi(buf2);
3553 src = talloc_asprintf(ctx,
3555 client_get_cur_dir(),
3560 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname) ) {
3561 d_printf("chown %s: %s\n", src, cli_errstr(cli));
3565 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3566 d_printf("Server doesn't support UNIX CIFS calls.\n");
3570 if (!NT_STATUS_IS_OK(cli_posix_chown(targetcli, targetname, uid, gid))) {
3571 d_printf("%s chown file %s uid=%d, gid=%d\n",
3572 cli_errstr(targetcli), src, (int)uid, (int)gid);
3579 /****************************************************************************
3581 ****************************************************************************/
3583 static int cmd_rename(void)
3585 TALLOC_CTX *ctx = talloc_tos();
3588 struct cli_state *targetcli;
3592 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3593 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3594 d_printf("rename <src> <dest>\n");
3598 src = talloc_asprintf(ctx,
3600 client_get_cur_dir(),
3606 dest = talloc_asprintf(ctx,
3608 client_get_cur_dir(),
3614 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetsrc)) {
3615 d_printf("rename %s: %s\n", src, cli_errstr(cli));
3619 if (!cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli, &targetdest)) {
3620 d_printf("rename %s: %s\n", dest, cli_errstr(cli));
3624 if (!NT_STATUS_IS_OK(cli_rename(targetcli, targetsrc, targetdest))) {
3625 d_printf("%s renaming files %s -> %s \n",
3626 cli_errstr(targetcli),
3635 /****************************************************************************
3636 Print the volume name.
3637 ****************************************************************************/
3639 static int cmd_volume(void)
3646 status = cli_get_fs_volume_info(cli, volname, &serial_num,
3648 if (!NT_STATUS_IS_OK(status)) {
3649 d_printf("Error %s getting volume info\n", nt_errstr(status));
3653 d_printf("Volume: |%s| serial number 0x%x\n",
3654 volname, (unsigned int)serial_num);
3658 /****************************************************************************
3659 Hard link files using the NT call.
3660 ****************************************************************************/
3662 static int cmd_hardlink(void)
3664 TALLOC_CTX *ctx = talloc_tos();
3667 struct cli_state *targetcli;
3670 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3671 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3672 d_printf("hardlink <src> <dest>\n");
3676 src = talloc_asprintf(ctx,
3678 client_get_cur_dir(),
3684 dest = talloc_asprintf(ctx,
3686 client_get_cur_dir(),
3692 if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
3693 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
3697 if (!NT_STATUS_IS_OK(cli_nt_hardlink(targetcli, targetname, dest))) {
3698 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
3705 /****************************************************************************
3706 Toggle the prompt flag.
3707 ****************************************************************************/
3709 static int cmd_prompt(void)
3712 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
3716 /****************************************************************************
3717 Set the newer than time.
3718 ****************************************************************************/
3720 static int cmd_newer(void)
3722 TALLOC_CTX *ctx = talloc_tos();
3725 SMB_STRUCT_STAT sbuf;
3727 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
3728 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
3729 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
3730 DEBUG(1,("Getting files newer than %s",
3731 time_to_asc(newer_than)));
3736 if (ok && newer_than == 0) {
3737 d_printf("Error setting newer-than time\n");
3744 /****************************************************************************
3745 Set the archive level.
3746 ****************************************************************************/
3748 static int cmd_archive(void)
3750 TALLOC_CTX *ctx = talloc_tos();
3753 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3754 archive_level = atoi(buf);
3756 d_printf("Archive level is %d\n",archive_level);
3762 /****************************************************************************
3763 Toggle the lowercaseflag.
3764 ****************************************************************************/
3766 static int cmd_lowercase(void)
3768 lowercase = !lowercase;
3769 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
3773 /****************************************************************************
3774 Toggle the case sensitive flag.
3775 ****************************************************************************/
3777 static int cmd_setcase(void)
3779 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
3781 cli_set_case_sensitive(cli, !orig_case_sensitive);
3782 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
3787 /****************************************************************************
3788 Toggle the showacls flag.
3789 ****************************************************************************/
3791 static int cmd_showacls(void)
3793 showacls = !showacls;
3794 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
3799 /****************************************************************************
3800 Toggle the recurse flag.
3801 ****************************************************************************/
3803 static int cmd_recurse(void)
3806 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
3810 /****************************************************************************
3811 Toggle the translate flag.
3812 ****************************************************************************/
3814 static int cmd_translate(void)
3816 translation = !translation;
3817 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
3818 translation?"on":"off"));
3822 /****************************************************************************
3824 ****************************************************************************/
3826 static int cmd_lcd(void)
3828 TALLOC_CTX *ctx = talloc_tos();
3832 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3833 if (chdir(buf) == -1) {
3834 d_printf("chdir to %s failed (%s)\n",
3835 buf, strerror(errno));
3838 d = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
3842 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
3846 /****************************************************************************
3847 Get a file restarting at end of local file.
3848 ****************************************************************************/
3850 static int cmd_reget(void)
3852 TALLOC_CTX *ctx = talloc_tos();
3853 char *local_name = NULL;
3854 char *remote_name = NULL;
3858 remote_name = talloc_strdup(ctx, client_get_cur_dir());
3863 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
3864 d_printf("reget <filename>\n");
3867 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
3871 remote_name = clean_name(ctx,remote_name);
3877 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
3882 return do_get(remote_name, local_name, true);
3885 /****************************************************************************
3886 Put a file restarting at end of local file.
3887 ****************************************************************************/
3889 static int cmd_reput(void)
3891 TALLOC_CTX *ctx = talloc_tos();
3892 char *local_name = NULL;
3893 char *remote_name = NULL;
3897 remote_name = talloc_strdup(ctx, client_get_cur_dir());
3902 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
3903 d_printf("reput <filename>\n");
3907 if (!file_exist_stat(local_name, &st, false)) {
3908 d_printf("%s does not exist\n", local_name);
3912 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3913 remote_name = talloc_asprintf_append(remote_name,
3916 remote_name = talloc_asprintf_append(remote_name,
3923 remote_name = clean_name(ctx, remote_name);
3928 return do_put(remote_name, local_name, true);
3931 /****************************************************************************
3933 ****************************************************************************/
3935 static void browse_fn(const char *name, uint32 m,
3936 const char *comment, void *state)
3938 const char *typestr = "";
3941 case STYPE_DISKTREE:
3945 typestr = "Printer";
3954 /* FIXME: If the remote machine returns non-ascii characters
3955 in any of these fields, they can corrupt the output. We
3956 should remove them. */
3958 d_printf("\t%-15s %-10.10s%s\n",
3959 name,typestr,comment);
3961 d_printf ("%s|%s|%s\n",typestr,name,comment);
3965 static bool browse_host_rpc(bool sort)
3968 struct rpc_pipe_client *pipe_hnd = NULL;
3969 TALLOC_CTX *frame = talloc_stackframe();
3971 struct srvsvc_NetShareInfoCtr info_ctr;
3972 struct srvsvc_NetShareCtr1 ctr1;
3973 uint32_t resume_handle = 0;
3974 uint32_t total_entries = 0;
3977 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
3980 if (!NT_STATUS_IS_OK(status)) {
3981 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
3982 nt_errstr(status)));
3987 ZERO_STRUCT(info_ctr);
3991 info_ctr.ctr.ctr1 = &ctr1;
3993 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, frame,
4001 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4002 TALLOC_FREE(pipe_hnd);
4007 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4008 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4009 browse_fn(info.name, info.type, info.comment, NULL);
4012 TALLOC_FREE(pipe_hnd);
4017 /****************************************************************************
4018 Try and browse available connections on a host.
4019 ****************************************************************************/
4021 static bool browse_host(bool sort)
4025 d_printf("\n\tSharename Type Comment\n");
4026 d_printf("\t--------- ---- -------\n");
4029 if (browse_host_rpc(sort)) {
4033 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
4034 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
4039 /****************************************************************************
4041 ****************************************************************************/
4043 static void server_fn(const char *name, uint32 m,
4044 const char *comment, void *state)
4048 d_printf("\t%-16s %s\n", name, comment);
4050 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4054 /****************************************************************************
4055 Try and browse available connections on a host.
4056 ****************************************************************************/
4058 static bool list_servers(const char *wk_grp)
4062 if (!cli->server_domain)
4066 d_printf("\n\tServer Comment\n");
4067 d_printf("\t--------- -------\n");
4069 fstrcpy( state, "Server" );
4070 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4074 d_printf("\n\tWorkgroup Master\n");
4075 d_printf("\t--------- -------\n");
4078 fstrcpy( state, "Workgroup" );
4079 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4084 /****************************************************************************
4085 Print or set current VUID
4086 ****************************************************************************/
4088 static int cmd_vuid(void)
4090 TALLOC_CTX *ctx = talloc_tos();
4093 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4094 d_printf("Current VUID is %d\n", cli->vuid);
4098 cli->vuid = atoi(buf);
4102 /****************************************************************************
4103 Setup a new VUID, by issuing a session setup
4104 ****************************************************************************/
4106 static int cmd_logon(void)
4108 TALLOC_CTX *ctx = talloc_tos();
4109 char *l_username, *l_password;
4111 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
4112 d_printf("logon <username> [<password>]\n");
4116 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
4117 char *pass = getpass("Password: ");
4119 l_password = talloc_strdup(ctx,pass);
4126 if (!NT_STATUS_IS_OK(cli_session_setup(cli, l_username,
4127 l_password, strlen(l_password),
4128 l_password, strlen(l_password),
4130 d_printf("session setup failed: %s\n", cli_errstr(cli));
4134 d_printf("Current VUID is %d\n", cli->vuid);
4139 /****************************************************************************
4140 list active connections
4141 ****************************************************************************/
4143 static int cmd_list_connect(void)
4145 cli_cm_display(cli);
4149 /****************************************************************************
4150 display the current active client connection
4151 ****************************************************************************/
4153 static int cmd_show_connect( void )
4155 TALLOC_CTX *ctx = talloc_tos();
4156 struct cli_state *targetcli;
4159 if (!cli_resolve_path(ctx, "", auth_info, cli, client_get_cur_dir(),
4160 &targetcli, &targetpath ) ) {
4161 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
4165 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
4169 /****************************************************************************
4171 ***************************************************************************/
4173 int cmd_iosize(void)
4175 TALLOC_CTX *ctx = talloc_tos();
4179 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4181 d_printf("iosize <n> or iosize 0x<n>. "
4182 "Minimum is 16384 (0x4000), "
4183 "max is 16776960 (0xFFFF00)\n");
4185 d_printf("iosize <n> or iosize 0x<n>. "
4186 "(Encrypted connection) ,"
4187 "Minimum is 16384 (0x4000), "
4188 "max is 130048 (0x1FC00)\n");
4193 iosize = strtol(buf,NULL,0);
4194 if (smb_encrypt && (iosize < 0x4000 || iosize > 0xFC00)) {
4195 d_printf("iosize out of range for encrypted "
4196 "connection (min = 16384 (0x4000), "
4197 "max = 130048 (0x1FC00)");
4199 } else if (!smb_encrypt && (iosize < 0x4000 || iosize > 0xFFFF00)) {
4200 d_printf("iosize out of range (min = 16384 (0x4000), "
4201 "max = 16776960 (0xFFFF00)");
4205 io_bufsize = iosize;
4206 d_printf("iosize is now %d\n", io_bufsize);
4210 /****************************************************************************
4212 ****************************************************************************/
4213 static int cmd_history(void)
4215 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4219 hlist = history_list();
4221 for (i = 0; hlist && hlist[i]; i++) {
4222 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
4225 DEBUG(0,("no history without readline support\n"));
4231 /* Some constants for completing filename arguments */
4233 #define COMPL_NONE 0 /* No completions */
4234 #define COMPL_REMOTE 1 /* Complete remote filename */
4235 #define COMPL_LOCAL 2 /* Complete local filename */
4237 /* This defines the commands supported by this client.
4238 * NOTE: The "!" must be the last one in the list because it's fn pointer
4239 * field is NULL, and NULL in that field is used in process_tok()
4240 * (below) to indicate the end of the list. crh
4245 const char *description;
4246 char compl_args[2]; /* Completion argument info */
4248 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4249 {"allinfo",cmd_allinfo,"<file> show all available info",
4250 {COMPL_NONE,COMPL_NONE}},
4251 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
4252 {"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}},
4253 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
4254 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
4255 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
4256 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
4257 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
4258 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
4259 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
4260 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4261 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4262 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4263 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
4264 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4265 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
4266 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
4267 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
4268 {COMPL_REMOTE, COMPL_LOCAL}},
4269 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4270 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
4271 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
4272 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
4273 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
4274 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
4275 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4276 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
4277 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4278 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
4279 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
4280 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4281 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
4282 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
4283 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
4284 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
4285 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
4286 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
4287 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
4288 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
4289 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4290 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4291 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4292 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
4293 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
4294 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
4295 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
4296 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
4297 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4298 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
4299 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
4300 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4301 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4302 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
4303 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
4304 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
4305 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
4306 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4307 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
4308 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
4309 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
4310 {COMPL_REMOTE, COMPL_LOCAL}},
4311 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
4312 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
4313 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
4314 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
4315 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
4316 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
4317 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
4318 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
4319 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
4320 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
4321 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
4322 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
4323 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
4324 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
4326 /* Yes, this must be here, see crh's comment above. */
4327 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
4328 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
4331 /*******************************************************************
4332 Lookup a command string in the list of commands, including
4334 ******************************************************************/
4336 static int process_tok(char *tok)
4338 int i = 0, matches = 0;
4340 int tok_len = strlen(tok);
4342 while (commands[i].fn != NULL) {
4343 if (strequal(commands[i].name,tok)) {
4347 } else if (strnequal(commands[i].name, tok, tok_len)) {
4356 else if (matches == 1)
4362 /****************************************************************************
4364 ****************************************************************************/
4366 static int cmd_help(void)
4368 TALLOC_CTX *ctx = talloc_tos();
4372 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4373 if ((i = process_tok(buf)) >= 0)
4374 d_printf("HELP %s:\n\t%s\n\n",
4375 commands[i].name,commands[i].description);
4377 while (commands[i].description) {
4378 for (j=0; commands[i].description && (j<5); j++) {
4379 d_printf("%-15s",commands[i].name);
4388 /****************************************************************************
4389 Process a -c command string.
4390 ****************************************************************************/
4392 static int process_command_string(const char *cmd_in)
4394 TALLOC_CTX *ctx = talloc_tos();
4395 char *cmd = talloc_strdup(ctx, cmd_in);
4401 /* establish the connection if not already */
4404 cli = cli_cm_open(talloc_tos(), NULL,
4405 have_ip ? dest_ss_str : desthost,
4408 max_protocol, port, name_type);
4414 while (cmd[0] != '\0') {
4420 if ((p = strchr_m(cmd, ';')) == 0) {
4429 /* and get the first part of the command */
4431 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
4435 if ((i = process_tok(tok)) >= 0) {
4436 rc = commands[i].fn();
4437 } else if (i == -2) {
4438 d_printf("%s: command abbreviation ambiguous\n",tok);
4440 d_printf("%s: command not found\n",tok);
4447 #define MAX_COMPLETIONS 100
4449 struct completion_remote {
4457 static NTSTATUS completion_remote_filter(const char *mnt,
4458 struct file_info *f,
4462 struct completion_remote *info = (struct completion_remote *)state;
4464 if (info->count >= MAX_COMPLETIONS - 1) {
4465 return NT_STATUS_OK;
4467 if (strncmp(info->text, f->name, info->len) != 0) {
4468 return NT_STATUS_OK;
4470 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
4471 return NT_STATUS_OK;
4474 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
4475 info->matches[info->count] = SMB_STRDUP(f->name);
4477 TALLOC_CTX *ctx = talloc_stackframe();
4480 tmp = talloc_strdup(ctx,info->dirmask);
4483 return NT_STATUS_NO_MEMORY;
4485 tmp = talloc_asprintf_append(tmp, "%s", f->name);
4488 return NT_STATUS_NO_MEMORY;
4490 if (f->mode & aDIR) {
4491 tmp = talloc_asprintf_append(tmp, "%s",
4496 return NT_STATUS_NO_MEMORY;
4498 info->matches[info->count] = SMB_STRDUP(tmp);
4501 if (info->matches[info->count] == NULL) {
4502 return NT_STATUS_OK;
4504 if (f->mode & aDIR) {
4505 smb_readline_ca_char(0);
4507 if (info->count == 1) {
4508 info->samelen = strlen(info->matches[info->count]);
4510 while (strncmp(info->matches[info->count],
4511 info->matches[info->count-1],
4512 info->samelen) != 0) {
4517 return NT_STATUS_OK;
4520 static char **remote_completion(const char *text, int len)
4522 TALLOC_CTX *ctx = talloc_stackframe();
4523 char *dirmask = NULL;
4524 char *targetpath = NULL;
4525 struct cli_state *targetcli = NULL;
4527 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
4530 /* can't have non-static initialisation on Sun CC, so do it
4536 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
4537 if (!info.matches) {
4543 * We're leaving matches[0] free to fill it later with the text to
4544 * display: Either the one single match or the longest common subset
4547 info.matches[0] = NULL;
4550 for (i = len-1; i >= 0; i--) {
4551 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
4556 info.text = text+i+1;
4557 info.samelen = info.len = len-i-1;
4560 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
4561 if (!info.dirmask) {
4564 strncpy(info.dirmask, text, i+1);
4565 info.dirmask[i+1] = 0;
4566 dirmask = talloc_asprintf(ctx,
4568 client_get_cur_dir(),
4572 info.dirmask = SMB_STRDUP("");
4573 if (!info.dirmask) {
4576 dirmask = talloc_asprintf(ctx,
4578 client_get_cur_dir());
4584 if (!cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli, &targetpath)) {
4587 status = cli_list(targetcli, targetpath, aDIR | aSYSTEM | aHIDDEN,
4588 completion_remote_filter, (void *)&info);
4589 if (!NT_STATUS_IS_OK(status)) {
4593 if (info.count == 1) {
4595 * No matches at all, NULL indicates there is nothing
4597 SAFE_FREE(info.matches[0]);
4598 SAFE_FREE(info.matches);
4603 if (info.count == 2) {
4605 * Exactly one match in matches[1], indicate this is the one
4608 info.matches[0] = info.matches[1];
4609 info.matches[1] = NULL;
4612 return info.matches;
4616 * We got more than one possible match, set the result to the maximum
4620 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
4621 info.matches[info.count] = NULL;
4622 return info.matches;
4625 for (i = 0; i < info.count; i++) {
4626 SAFE_FREE(info.matches[i]);
4628 SAFE_FREE(info.matches);
4629 SAFE_FREE(info.dirmask);
4634 static char **completion_fn(const char *text, int start, int end)
4636 smb_readline_ca_char(' ');
4639 const char *buf, *sp;
4643 buf = smb_readline_get_line_buffer();
4647 sp = strchr(buf, ' ');
4651 for (i = 0; commands[i].name; i++) {
4652 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
4653 (commands[i].name[sp - buf] == 0)) {
4657 if (commands[i].name == NULL)
4663 if (sp == (buf + start))
4664 compl_type = commands[i].compl_args[0];
4666 compl_type = commands[i].compl_args[1];
4668 if (compl_type == COMPL_REMOTE)
4669 return remote_completion(text, end - start);
4670 else /* fall back to local filename completion */
4674 int i, len, samelen = 0, count=1;
4676 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
4683 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
4684 if (strncmp(text, commands[i].name, len) == 0) {
4685 matches[count] = SMB_STRDUP(commands[i].name);
4686 if (!matches[count])
4689 samelen = strlen(matches[count]);
4691 while (strncmp(matches[count], matches[count-1], samelen) != 0)
4698 case 0: /* should never happen */
4702 matches[0] = SMB_STRDUP(matches[1]);
4705 matches[0] = (char *)SMB_MALLOC(samelen+1);
4708 strncpy(matches[0], matches[1], samelen);
4709 matches[0][samelen] = 0;
4711 matches[count] = NULL;
4715 for (i = 0; i < count; i++)
4723 static bool finished;
4725 /****************************************************************************
4726 Make sure we swallow keepalives during idle time.
4727 ****************************************************************************/
4729 static void readline_callback(void)
4732 struct timeval timeout;
4733 static time_t last_t;
4734 struct timespec now;
4737 clock_gettime_mono(&now);
4751 FD_SET(cli->fd,&fds);
4754 timeout.tv_usec = 0;
4755 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
4757 /* We deliberately use receive_smb_raw instead of
4758 client_receive_smb as we want to receive
4759 session keepalives and then drop them here.
4761 if (FD_ISSET(cli->fd,&fds)) {
4765 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
4767 status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize, 0, 0, &len);
4769 if (!NT_STATUS_IS_OK(status)) {
4770 DEBUG(0, ("Read from server failed, maybe it closed "
4771 "the connection\n"));
4774 smb_readline_done();
4775 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
4776 set_smb_read_error(&cli->smb_rw_error,
4781 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
4782 set_smb_read_error(&cli->smb_rw_error,
4787 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
4790 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
4791 DEBUG(0, ("Read from server "
4792 "returned unexpected packet!\n"));
4799 /* Ping the server to keep the connection alive using SMBecho. */
4802 unsigned char garbage[16];
4803 memset(garbage, 0xf0, sizeof(garbage));
4804 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
4806 if (!NT_STATUS_IS_OK(status)) {
4807 DEBUG(0, ("SMBecho failed. Maybe server has closed "
4808 "the connection\n"));
4810 smb_readline_done();
4815 /****************************************************************************
4816 Process commands on stdin.
4817 ****************************************************************************/
4819 static int process_stdin(void)
4824 TALLOC_CTX *frame = talloc_stackframe();
4826 char *the_prompt = NULL;
4830 /* display a prompt */
4831 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
4835 line = smb_readline(the_prompt, readline_callback, completion_fn);
4836 SAFE_FREE(the_prompt);
4842 /* special case - first char is ! */
4844 if (system(line + 1) == -1) {
4845 d_printf("system() command %s failed.\n",
4853 /* and get the first part of the command */
4855 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
4861 if ((i = process_tok(tok)) >= 0) {
4862 rc = commands[i].fn();
4863 } else if (i == -2) {
4864 d_printf("%s: command abbreviation ambiguous\n",tok);
4866 d_printf("%s: command not found\n",tok);
4874 /****************************************************************************
4875 Process commands from the client.
4876 ****************************************************************************/
4878 static int process(const char *base_directory)
4882 cli = cli_cm_open(talloc_tos(), NULL,
4883 have_ip ? dest_ss_str : desthost,
4884 service, auth_info, true, smb_encrypt,
4885 max_protocol, port, name_type);
4890 if (base_directory && *base_directory) {
4891 rc = do_cd(base_directory);
4899 rc = process_command_string(cmdstr);
4908 /****************************************************************************
4910 ****************************************************************************/
4912 static int do_host_query(const char *query_host)
4914 cli = cli_cm_open(talloc_tos(), NULL,
4915 have_ip ? dest_ss_str : query_host, "IPC$", auth_info, true, smb_encrypt,
4916 max_protocol, port, name_type);
4922 /* Ensure that the host can do IPv4 */
4924 if (!interpret_addr(query_host)) {
4925 struct sockaddr_storage ss;
4926 if (interpret_string_addr(&ss, query_host, 0) &&
4927 (ss.ss_family != AF_INET)) {
4928 d_printf("%s is an IPv6 address -- no workgroup available\n",
4936 /* Workgroups simply don't make sense over anything
4937 else but port 139... */
4940 cli = cli_cm_open(talloc_tos(), NULL,
4941 have_ip ? dest_ss_str : query_host, "IPC$",
4942 auth_info, true, smb_encrypt,
4943 max_protocol, 139, name_type);
4947 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
4951 list_servers(lp_workgroup());
4958 /****************************************************************************
4959 Handle a tar operation.
4960 ****************************************************************************/
4962 static int do_tar_op(const char *base_directory)
4966 /* do we already have a connection? */
4968 cli = cli_cm_open(talloc_tos(), NULL,
4969 have_ip ? dest_ss_str : desthost,
4970 service, auth_info, true, smb_encrypt,
4971 max_protocol, port, name_type);
4978 if (base_directory && *base_directory) {
4979 ret = do_cd(base_directory);
4993 /****************************************************************************
4994 Handle a message operation.
4995 ****************************************************************************/
4997 static int do_message_op(struct user_auth_info *a_info)
4999 struct sockaddr_storage ss;
5000 struct nmb_name called, calling;
5001 fstring server_name;
5002 char name_type_hex[10];
5006 make_nmb_name(&calling, calling_name, 0x0);
5007 make_nmb_name(&called , desthost, name_type);
5009 fstrcpy(server_name, desthost);
5010 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
5011 fstrcat(server_name, name_type_hex);
5017 /* we can only do messages over port 139 (to windows clients at least) */
5019 msg_port = port ? port : 139;
5021 if (!(cli=cli_initialise())) {
5022 d_printf("Connection to %s failed\n", desthost);
5025 cli_set_port(cli, msg_port);
5027 status = cli_connect(cli, server_name, &ss);
5028 if (!NT_STATUS_IS_OK(status)) {
5029 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
5033 if (!cli_session_request(cli, &calling, &called)) {
5034 d_printf("session request failed\n");
5039 send_message(get_cmdline_auth_info_username(a_info));
5045 /****************************************************************************
5047 ****************************************************************************/
5049 int main(int argc,char *argv[])
5051 char *base_directory = NULL;
5053 char *query_host = NULL;
5054 bool message = false;
5055 static const char *new_name_resolve_order = NULL;
5059 fstring new_workgroup;
5060 bool tar_opt = false;
5061 bool service_opt = false;
5062 struct poptOption long_options[] = {
5065 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
5066 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
5067 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
5068 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
5069 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
5070 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
5071 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
5072 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
5073 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
5074 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
5075 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
5076 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
5077 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
5079 POPT_COMMON_CONNECTION
5080 POPT_COMMON_CREDENTIALS
5083 TALLOC_CTX *frame = talloc_stackframe();
5085 if (!client_set_cur_dir("\\")) {
5089 /* initialize the workgroup name so we can determine whether or
5090 not it was set by a command line option */
5092 set_global_myworkgroup( "" );
5093 set_global_myname( "" );
5095 /* set default debug level to 1 regardless of what smb.conf sets */
5096 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
5099 lp_set_cmdline("log level", "1");
5101 auth_info = user_auth_info_init(frame);
5102 if (auth_info == NULL) {
5105 popt_common_set_auth_info(auth_info);
5108 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
5109 poptSetOtherOptionHelp(pc, "service <password>");
5111 lp_set_in_client(true); /* Make sure that we tell lp_load we are */
5113 while ((opt = poptGetNextOpt(pc)) != -1) {
5115 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
5116 /* I see no other way to keep things sane --SSS */
5117 if (tar_opt == true) {
5118 while (poptPeekArg(pc)) {
5124 /* if the service has not yet been specified lets see if it is available in the popt stack */
5125 if (!service_opt && poptPeekArg(pc)) {
5126 service = talloc_strdup(frame, poptGetArg(pc));
5133 /* if the service has already been retrieved then check if we have also a password */
5135 && (!get_cmdline_auth_info_got_pass(auth_info))
5136 && poptPeekArg(pc)) {
5137 set_cmdline_auth_info_password(auth_info,
5143 /* Messages are sent to NetBIOS name type 0x3
5144 * (Messenger Service). Make sure we default
5145 * to port 139 instead of port 445. srl,crh
5148 desthost = talloc_strdup(frame,poptGetOptArg(pc));
5158 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
5162 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
5166 setup_logging("smbclient", DEBUG_STDERR );
5167 display_set_stderr();
5171 query_host = talloc_strdup(frame, poptGetOptArg(pc));
5177 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
5180 /* We must use old option processing for this. Find the
5181 * position of the -T option in the raw argv[]. */
5184 for (i = 1; i < argc; i++) {
5185 if (strncmp("-T", argv[i],2)==0)
5189 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
5190 poptPrintUsage(pc, stderr, 0);
5194 /* this must be the last option, mark we have parsed it so that we know we have */
5198 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
5199 if (!base_directory) {
5210 return(do_smb_browse());
5215 /* We may still have some leftovers after the last popt option has been called */
5216 if (tar_opt == true) {
5217 while (poptPeekArg(pc)) {
5223 /* if the service has not yet been specified lets see if it is available in the popt stack */
5224 if (!service_opt && poptPeekArg(pc)) {
5225 service = talloc_strdup(frame,poptGetArg(pc));
5232 /* if the service has already been retrieved then check if we have also a password */
5234 && !get_cmdline_auth_info_got_pass(auth_info)
5235 && poptPeekArg(pc)) {
5236 set_cmdline_auth_info_password(auth_info,
5240 /* save the workgroup...
5242 FIXME!! do we need to do this for other options as well
5243 (or maybe a generic way to keep lp_load() from overwriting
5246 fstrcpy( new_workgroup, lp_workgroup() );
5247 calling_name = talloc_strdup(frame, global_myname() );
5248 if (!calling_name) {
5252 if ( override_logfile )
5253 setup_logging( lp_logfile(), DEBUG_FILE );
5255 if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
5256 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
5257 argv[0], get_dyn_CONFIGFILE());
5260 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
5261 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
5267 if (service_opt && service) {
5270 /* Convert any '/' characters in the service name to '\' characters */
5271 string_replace(service, '/','\\');
5272 if (count_chars(service,'\\') < 3) {
5273 d_printf("\n%s: Not enough '\\' characters in service\n",service);
5274 poptPrintUsage(pc, stderr, 0);
5277 /* Remove trailing slashes */
5278 len = strlen(service);
5279 while(len > 0 && service[len - 1] == '\\') {
5281 service[len] = '\0';
5285 if ( strlen(new_workgroup) != 0 ) {
5286 set_global_myworkgroup( new_workgroup );
5289 if ( strlen(calling_name) != 0 ) {
5290 set_global_myname( calling_name );
5292 TALLOC_FREE(calling_name);
5293 calling_name = talloc_strdup(frame, global_myname() );
5296 smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
5297 if (!init_names()) {
5298 fprintf(stderr, "init_names() failed\n");
5302 if(new_name_resolve_order)
5303 lp_set_name_resolve_order(new_name_resolve_order);
5305 if (!tar_type && !query_host && !service && !message) {
5306 poptPrintUsage(pc, stderr, 0);
5310 poptFreeContext(pc);
5312 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5314 /* Ensure we have a password (or equivalent). */
5315 set_cmdline_auth_info_getpass(auth_info);
5319 process_command_string(cmdstr);
5320 return do_tar_op(base_directory);
5323 if (query_host && *query_host) {
5324 char *qhost = query_host;
5327 while (*qhost == '\\' || *qhost == '/')
5330 if ((slash = strchr_m(qhost, '/'))
5331 || (slash = strchr_m(qhost, '\\'))) {
5335 if ((p=strchr_m(qhost, '#'))) {
5338 sscanf(p, "%x", &name_type);
5341 return do_host_query(qhost);
5345 return do_message_op(auth_info);
5348 if (process(base_directory)) {