2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) Gerald (Jerry) Carter 2004
8 Copyright (C) Jeremy Allison 1994-2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/filesys.h"
26 #include "rpc_client/cli_pipe.h"
27 #include "client/client_proto.h"
28 #include "client/clitar_proto.h"
29 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
30 #include "../lib/util/select.h"
31 #include "system/readline.h"
32 #include "../libcli/smbreadline/smbreadline.h"
33 #include "../libcli/security/security.h"
34 #include "system/select.h"
35 #include "libsmb/libsmb.h"
36 #include "libsmb/clirap.h"
38 #include "libsmb/nmblib.h"
39 #include "include/ntioctl.h"
40 #include "../libcli/smb/smbXcli_base.h"
41 #include "lib/util/time_basic.h"
42 #include "lib/util/string_wrappers.h"
43 #include "lib/cmdline/cmdline.h"
44 #include "libcli/smb/reparse.h"
45 #include "lib/param/param.h"
51 extern int do_smb_browse(void); /* mDNS browsing */
55 static char *desthost;
56 static bool grepable = false;
57 static bool quiet = false;
58 static char *cmdstr = NULL;
59 const char *cmd_ptr = NULL;
61 static int io_bufsize = 0; /* we use the default size */
62 static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
64 static int name_type = 0x20;
66 static int process_tok(char *tok);
67 static int cmd_help(void);
69 /* value for unused fid field in trans2 secondary request */
70 #define FID_UNUSED (0xFFFF)
72 time_t newer_than = 0;
73 static int archive_level = 0;
75 static bool translation = false;
78 static bool prompt = true;
80 static bool recurse = false;
81 static bool showacls = false;
82 bool lowercase = false;
83 static bool backup_intent = false;
85 static struct sockaddr_storage dest_ss;
86 static char dest_ss_str[INET6_ADDRSTRLEN];
88 #define SEPARATORS " \t\n\r"
91 uint64_t get_total_size = 0;
92 unsigned int get_total_time_ms = 0;
93 static uint64_t put_total_size = 0;
94 static unsigned int put_total_time_ms = 0;
97 static double dir_total;
99 /* root cli_state connection */
101 struct cli_state *cli;
103 static char CLI_DIRSEP_CHAR = '\\';
104 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
106 /* Accessor functions for directory paths. */
107 static char *fileselection;
108 static const char *client_get_fileselection(void)
111 return fileselection;
116 static const char *client_set_fileselection(const char *new_fs)
118 SAFE_FREE(fileselection);
120 fileselection = SMB_STRDUP(new_fs);
122 return client_get_fileselection();
126 static const char *client_get_cwd(void)
131 return CLI_DIRSEP_STR;
134 static const char *client_set_cwd(const char *new_cwd)
138 cwd = SMB_STRDUP(new_cwd);
140 return client_get_cwd();
143 static char *cur_dir;
144 const char *client_get_cur_dir(void)
149 return CLI_DIRSEP_STR;
152 const char *client_set_cur_dir(const char *newdir)
156 cur_dir = SMB_STRDUP(newdir);
158 return client_get_cur_dir();
161 /****************************************************************************
162 Put up a yes/no prompt.
163 ****************************************************************************/
165 static bool yesno(const char *p)
170 if (!fgets(ans,sizeof(ans)-1,stdin))
173 if (*ans == 'y' || *ans == 'Y')
179 /****************************************************************************
180 Write to a local file with CR/LF->LF translation if appropriate. Return the
181 number taken from the buffer. This may not equal the number written.
182 ****************************************************************************/
184 static ssize_t writefile(int f, char *b, size_t n)
198 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
201 if (write(f, b, 1) != 1) {
211 /****************************************************************************
212 Read from a file with LF->CR/LF translation if appropriate. Return the
213 number read. read approx n bytes.
214 ****************************************************************************/
216 static int readfile(uint8_t *b, int n, FILE *f)
222 return fread(b,1,n,f);
225 while (i < (n - 1)) {
226 if ((c = getc(f)) == EOF) {
230 if (c == '\n') { /* change all LFs to CR/LF */
245 static size_t push_source(uint8_t *buf, size_t n, void *priv)
247 struct push_state *state = (struct push_state *)priv;
250 if (feof(state->f)) {
254 result = readfile(buf, n, state->f);
255 state->nread += result;
259 /****************************************************************************
261 ****************************************************************************/
263 static void send_message(const char *username)
269 d_printf("Type your message, ending it with a Control-D\n");
272 while (i<sizeof(buf)-2) {
273 int c = fgetc(stdin);
284 status = cli_message(cli, desthost, username, buf);
285 if (!NT_STATUS_IS_OK(status)) {
286 d_fprintf(stderr, "cli_message returned %s\n",
291 /****************************************************************************
292 Check the space on a device.
293 ****************************************************************************/
295 static int do_dskattr(void)
297 uint64_t total, bsize, avail;
298 struct cli_state *targetcli = NULL;
299 char *targetpath = NULL;
300 TALLOC_CTX *ctx = talloc_tos();
301 struct cli_credentials *creds = samba_cmdline_get_creds();
304 status = cli_resolve_path(ctx,
308 client_get_cur_dir(), &targetcli,
310 if (!NT_STATUS_IS_OK(status)) {
311 d_printf("Error in dskattr: %s\n", nt_errstr(status));
315 status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
316 if (!NT_STATUS_IS_OK(status)) {
317 d_printf("Error in dskattr: %s\n", nt_errstr(status));
321 d_printf("\n\t\t%" PRIu64
322 " blocks of size %" PRIu64
323 ". %" PRIu64 " blocks available\n",
324 total, bsize, avail);
329 /****************************************************************************
331 ****************************************************************************/
333 static int cmd_pwd(void)
335 d_printf("Current directory is %s",service);
336 d_printf("%s\n",client_get_cur_dir());
340 /****************************************************************************
341 Ensure name has correct directory separators.
342 ****************************************************************************/
344 static void normalize_name(char *newdir)
346 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
347 string_replace(newdir,'/','\\');
351 /****************************************************************************
352 Local name cleanup before sending to server. SMB1 allows relative pathnames,
353 but SMB2 does not, so we need to resolve them locally.
354 ****************************************************************************/
356 char *client_clean_name(TALLOC_CTX *ctx, const char *name)
358 char *newname = NULL;
363 /* First ensure any path separators are correct. */
364 newname = talloc_strdup(ctx, name);
365 if (newname == NULL) {
368 normalize_name(newname);
370 /* Now remove any relative (..) path components. */
371 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
372 newname = unix_clean_name(ctx, newname);
374 newname = clean_name(ctx, newname);
376 if (newname == NULL) {
382 /****************************************************************************
383 Change directory - inner section.
384 ****************************************************************************/
386 static int do_cd(const char *new_dir)
389 char *saved_dir = NULL;
391 char *targetpath = NULL;
392 struct cli_state *targetcli = NULL;
394 TALLOC_CTX *ctx = talloc_stackframe();
395 struct cli_credentials *creds = samba_cmdline_get_creds();
398 newdir = talloc_strdup(ctx, new_dir);
404 normalize_name(newdir);
406 /* Save the current directory in case the new directory is invalid */
408 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
414 if (*newdir == CLI_DIRSEP_CHAR) {
415 client_set_cur_dir(newdir);
418 new_cd = talloc_asprintf(ctx, "%s%s",
419 client_get_cur_dir(),
426 /* Ensure cur_dir ends in a DIRSEP */
427 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
428 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
433 client_set_cur_dir(new_cd);
435 new_cd = client_clean_name(ctx, new_cd);
436 client_set_cur_dir(new_cd);
438 status = cli_resolve_path(ctx, "",
440 cli, new_cd, &targetcli, &targetpath);
441 if (!NT_STATUS_IS_OK(status)) {
442 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
443 client_set_cur_dir(saved_dir);
447 if (strequal(targetpath,CLI_DIRSEP_STR )) {
453 targetpath = talloc_asprintf(
454 ctx, "%s%s", targetpath, CLI_DIRSEP_STR);
456 client_set_cur_dir(saved_dir);
459 targetpath = client_clean_name(ctx, targetpath);
461 client_set_cur_dir(saved_dir);
465 status = cli_chkpath(targetcli, targetpath);
466 if (!NT_STATUS_IS_OK(status)) {
467 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
468 client_set_cur_dir(saved_dir);
480 /****************************************************************************
482 ****************************************************************************/
484 static int cmd_cd(void)
489 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
492 d_printf("Current directory is %s\n",client_get_cur_dir());
498 /****************************************************************************
500 ****************************************************************************/
502 static int cmd_cd_oneup(void)
507 /*******************************************************************
508 Decide if a file should be operated on.
509 ********************************************************************/
511 static bool do_this_one(struct file_info *finfo)
517 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
521 if (*client_get_fileselection() &&
522 !mask_match(finfo->name,client_get_fileselection(),false)) {
523 DEBUG(3,("mask_match %s failed\n", finfo->name));
527 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
528 DEBUG(3,("newer_than %s failed\n", finfo->name));
532 if ((archive_level==1 || archive_level==2) && !(finfo->attr & FILE_ATTRIBUTE_ARCHIVE)) {
533 DEBUG(3,("archive %s failed\n", finfo->name));
540 /****************************************************************************
541 Display info about a file.
542 ****************************************************************************/
544 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
548 TALLOC_CTX *ctx = talloc_tos();
549 NTSTATUS status = NT_STATUS_OK;
551 if (!do_this_one(finfo)) {
555 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
557 d_printf(" %-30s%7.7s %8.0f %s",
559 attrib_string(talloc_tos(), finfo->attr),
562 dir_total += finfo->size;
564 struct cli_state *targetcli = NULL;
565 char *targetpath = NULL;
568 struct cli_credentials *creds = samba_cmdline_get_creds();
570 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
573 /* create absolute filename for cli_ntcreate() FIXME */
574 afname = talloc_asprintf(ctx,
580 return NT_STATUS_NO_MEMORY;
582 /* print file meta date header */
583 d_printf( "FILENAME:%s\n", finfo->name);
584 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->attr));
585 d_printf( "SIZE:%.0f\n", (double)finfo->size);
586 d_printf( "MTIME:%s", time_to_asc(t));
588 status = cli_resolve_path(
596 if (!NT_STATUS_IS_OK(status)) {
597 DBG_WARNING("display_finfo() Failed to resolve "
599 afname, nt_errstr(status));
603 status = cli_ntcreate(
605 targetpath, /* fname */
607 READ_CONTROL_ACCESS, /* DesiredAccess */
608 0, /* FileAttributes */
610 FILE_SHARE_WRITE, /* ShareAccess */
611 FILE_OPEN, /* CreateDisposition */
612 0x0, /* CreateOptions */
613 0x0, /* SecurityFlags */
616 if (!NT_STATUS_IS_OK(status)) {
617 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
618 afname, nt_errstr(status)));
620 struct security_descriptor *sd = NULL;
621 status = cli_query_secdesc(targetcli, fnum,
623 if (!NT_STATUS_IS_OK(status)) {
624 DEBUG( 0, ("display_finfo() failed to "
625 "get security descriptor: %s\n",
628 display_sec_desc(sd);
631 cli_close(targetcli, fnum);
638 /****************************************************************************
639 Accumulate size of a file.
640 ****************************************************************************/
642 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
645 if (do_this_one(finfo)) {
646 dir_total += finfo->size;
651 struct do_list_queue_entry {
652 struct do_list_queue_entry *prev, *next;
656 struct do_list_queue {
657 struct do_list_queue_entry *list;
660 static bool do_list_recurse;
661 static bool do_list_dirs;
662 static struct do_list_queue *queue = NULL;
663 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
666 /****************************************************************************
667 Functions for do_list_queue.
668 ****************************************************************************/
670 static void reset_do_list_queue(void)
675 static void init_do_list_queue(void)
678 queue = talloc_zero(NULL, struct do_list_queue);
681 static void add_to_do_list_queue(const char *entry)
683 struct do_list_queue_entry *e = NULL;
684 size_t entry_str_len = strlen(entry)+1;
685 size_t entry_len = offsetof(struct do_list_queue_entry, name);
687 entry_len += entry_str_len;
688 SMB_ASSERT(entry_len >= entry_str_len);
690 e = talloc_size(queue, entry_len);
692 d_printf("talloc failed for entry %s\n", entry);
695 talloc_set_name_const(e, "struct do_list_queue_entry");
697 memcpy(e->name, entry, entry_str_len);
698 DLIST_ADD_END(queue->list, e);
701 static char *do_list_queue_head(void)
703 return queue->list->name;
706 static void remove_do_list_queue_head(void)
708 struct do_list_queue_entry *e = queue->list;
709 DLIST_REMOVE(queue->list, e);
713 static int do_list_queue_empty(void)
715 return (queue == NULL) || (queue->list == NULL);
718 /****************************************************************************
719 A helper for do_list.
720 ****************************************************************************/
722 struct do_list_helper_state {
724 struct cli_state *cli;
727 static NTSTATUS do_list_helper(
732 struct do_list_helper_state *state = private_data;
733 TALLOC_CTX *ctx = talloc_tos();
735 char *dir_end = NULL;
736 NTSTATUS status = NT_STATUS_OK;
739 /* Work out the directory. */
740 dir = talloc_strdup(ctx, state->mask);
742 return NT_STATUS_NO_MEMORY;
744 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
748 if (!(f->attr & FILE_ATTRIBUTE_DIRECTORY)) {
749 if (do_this_one(f)) {
750 status = do_list_fn(state->cli, f, dir);
756 if (do_list_dirs && do_this_one(f)) {
757 status = do_list_fn(state->cli, f, dir);
758 if (!NT_STATUS_IS_OK(status)) {
763 if (!do_list_recurse ||
771 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
773 return NT_STATUS_UNSUCCESSFUL;
776 mask2 = talloc_asprintf(ctx,
784 return NT_STATUS_NO_MEMORY;
786 add_to_do_list_queue(mask2);
793 /****************************************************************************
794 A wrapper around cli_list that adds recursion.
795 ****************************************************************************/
797 NTSTATUS do_list(const char *mask,
799 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
804 struct do_list_helper_state state = { .cli = cli, };
805 static int in_do_list = 0;
806 TALLOC_CTX *ctx = talloc_tos();
807 struct cli_credentials *creds = samba_cmdline_get_creds();
808 NTSTATUS ret_status = NT_STATUS_OK;
809 NTSTATUS status = NT_STATUS_OK;
811 if (in_do_list && rec) {
812 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
818 do_list_recurse = rec;
822 init_do_list_queue();
823 add_to_do_list_queue(mask);
825 while (!do_list_queue_empty()) {
826 struct cli_state *targetcli = NULL;
827 char *targetpath = NULL;
829 state.mask = do_list_queue_head();
833 status = cli_resolve_path(
841 if (!NT_STATUS_IS_OK(status)) {
842 d_printf("do_list: [%s] %s\n", state.mask,
844 remove_do_list_queue_head();
854 if (!NT_STATUS_IS_OK(status)) {
855 d_printf("%s listing %s\n",
856 nt_errstr(status), targetpath);
859 remove_do_list_queue_head();
860 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
861 char *next_file = do_list_queue_head();
863 if ((strlen(next_file) >= 2) &&
864 (next_file[strlen(next_file) - 1] == '*') &&
865 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
866 save_ch = next_file +
867 strlen(next_file) - 2;
870 /* cwd is only used if showacls is on */
871 client_set_cwd(next_file);
874 if (!showacls) /* don't disturbe the showacls output */
875 d_printf("\n%s\n",next_file);
877 *save_ch = CLI_DIRSEP_CHAR;
880 TALLOC_FREE(targetpath);
884 reset_do_list_queue();
888 /****************************************************************************
889 Get a directory listing.
890 ****************************************************************************/
892 static int cmd_dir(void)
894 TALLOC_CTX *ctx = talloc_tos();
895 uint32_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
902 mask = talloc_strdup(ctx, client_get_cur_dir());
907 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
909 if (*buf == CLI_DIRSEP_CHAR) {
910 mask = talloc_strdup(ctx, buf);
912 mask = talloc_asprintf_append(mask, "%s", buf);
915 mask = talloc_asprintf_append(mask, "*");
921 mask = client_clean_name(ctx, mask);
927 /* cwd is only used if showacls is on */
928 client_set_cwd(client_get_cur_dir());
931 status = do_list(mask, attribute, display_finfo, recurse, true);
932 if (!NT_STATUS_IS_OK(status)) {
938 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
943 /****************************************************************************
944 Get a directory listing.
945 ****************************************************************************/
947 static int cmd_du(void)
949 TALLOC_CTX *ctx = talloc_tos();
950 uint32_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
957 mask = talloc_strdup(ctx, client_get_cur_dir());
961 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
962 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
968 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
970 if (*buf == CLI_DIRSEP_CHAR) {
971 mask = talloc_strdup(ctx, buf);
973 mask = talloc_asprintf_append(mask, "%s", buf);
976 mask = talloc_strdup(ctx, "*");
982 mask = client_clean_name(ctx, mask);
987 status = do_list(mask, attribute, do_du, recurse, true);
988 if (!NT_STATUS_IS_OK(status)) {
994 d_printf("Total number of bytes: %.0f\n", dir_total);
999 static int cmd_echo(void)
1001 TALLOC_CTX *ctx = talloc_tos();
1006 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1007 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1008 d_printf("echo <num> <data>\n");
1012 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1014 if (!NT_STATUS_IS_OK(status)) {
1015 d_printf("echo failed: %s\n", nt_errstr(status));
1022 /****************************************************************************
1023 Get a file from rname to lname
1024 ****************************************************************************/
1026 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1028 int *pfd = (int *)priv;
1031 rc = writefile(*pfd, buf, n);
1033 return map_nt_error_from_unix(errno);
1035 return NT_STATUS_OK;
1038 static int do_get(const char *rname, const char *lname_in, bool reget)
1040 TALLOC_CTX *ctx = talloc_tos();
1043 bool newhandle = false;
1044 struct timespec tp_start;
1050 struct cli_state *targetcli = NULL;
1051 char *targetname = NULL;
1053 struct cli_credentials *creds = samba_cmdline_get_creds();
1056 lname = talloc_strdup(ctx, lname_in);
1062 if (!strlower_m(lname)) {
1063 d_printf("strlower_m %s failed\n", lname);
1068 status = cli_resolve_path(ctx, "",
1070 cli, rname, &targetcli, &targetname);
1071 if (!NT_STATUS_IS_OK(status)) {
1072 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1076 clock_gettime_mono(&tp_start);
1078 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1079 if (!NT_STATUS_IS_OK(status)) {
1080 d_printf("%s opening remote file %s\n", nt_errstr(status),
1085 if(!strcmp(lname,"-")) {
1086 handle = fileno(stdout);
1089 handle = open(lname, O_WRONLY|O_CREAT, 0644);
1091 start = lseek(handle, 0, SEEK_END);
1093 d_printf("Error seeking local file\n");
1099 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1104 d_printf("Error opening local file %s\n",lname);
1109 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1111 if (!NT_STATUS_IS_OK(status)) {
1112 d_printf("getattrib: %s\n", nt_errstr(status));
1119 DEBUG(1,("getting file %s of size %.0f as %s ",
1120 rname, (double)size, lname));
1122 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1123 writefile_sink, (void *)&handle, &nread);
1124 if (!NT_STATUS_IS_OK(status)) {
1125 d_fprintf(stderr, "parallel_read returned %s\n",
1130 cli_close(targetcli, fnum);
1134 status = cli_close(targetcli, fnum);
1135 if (!NT_STATUS_IS_OK(status)) {
1136 d_printf("Error %s closing remote file\n", nt_errstr(status));
1144 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1145 cli_setatr(cli, rname, attr & ~(uint32_t)FILE_ATTRIBUTE_ARCHIVE, 0);
1149 struct timespec tp_end;
1152 clock_gettime_mono(&tp_end);
1153 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1154 get_total_time_ms += this_time;
1155 get_total_size += nread;
1157 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1158 nread / (1.024*this_time + 1.0e-4),
1159 get_total_size / (1.024*get_total_time_ms)));
1162 TALLOC_FREE(targetname);
1166 /****************************************************************************
1168 ****************************************************************************/
1170 static int cmd_get(void)
1172 TALLOC_CTX *ctx = talloc_tos();
1177 rname = talloc_strdup(ctx, client_get_cur_dir());
1182 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1183 d_printf("get <filename> [localname]\n");
1186 rname = talloc_asprintf_append(rname, "%s", fname);
1190 rname = client_clean_name(ctx, rname);
1195 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1200 return do_get(rname, lname, false);
1203 /****************************************************************************
1204 Do an mget operation on one file.
1205 ****************************************************************************/
1207 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1210 TALLOC_CTX *ctx = talloc_tos();
1211 const char *client_cwd = NULL;
1212 size_t client_cwd_len;
1214 char *local_path = NULL;
1217 return NT_STATUS_OK;
1220 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
1221 return NT_STATUS_OK;
1224 if ((finfo->attr & FILE_ATTRIBUTE_DIRECTORY) && !recurse) {
1225 return NT_STATUS_OK;
1229 const char *object = (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) ?
1230 "directory" : "file";
1234 quest = talloc_asprintf(
1235 ctx, "Get %s %s? ", object, finfo->name);
1236 if (quest == NULL) {
1237 return NT_STATUS_NO_MEMORY;
1243 return NT_STATUS_OK;
1247 path = talloc_asprintf(
1248 ctx, "%s%c%s", dir, CLI_DIRSEP_CHAR, finfo->name);
1250 return NT_STATUS_NO_MEMORY;
1252 path = client_clean_name(ctx, path);
1254 return NT_STATUS_NO_MEMORY;
1258 * Skip the path prefix if we've done a remote "cd" when
1259 * creating the local path
1261 client_cwd = client_get_cur_dir();
1262 client_cwd_len = strlen(client_cwd);
1264 local_path = talloc_strdup(ctx, path + client_cwd_len);
1265 if (local_path == NULL) {
1267 return NT_STATUS_NO_MEMORY;
1269 string_replace(local_path, CLI_DIRSEP_CHAR, '/');
1271 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
1272 int ret = mkdir(local_path, 0777);
1274 if ((ret == -1) && (errno != EEXIST)) {
1275 return map_nt_error_from_unix(errno);
1278 do_get(path, local_path, false);
1281 return NT_STATUS_OK;
1284 /****************************************************************************
1285 View the file using the pager.
1286 ****************************************************************************/
1288 static int cmd_more(void)
1290 TALLOC_CTX *ctx = talloc_tos();
1294 char *pager_cmd = NULL;
1300 rname = talloc_strdup(ctx, client_get_cur_dir());
1305 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1309 mask = umask(S_IRWXO | S_IRWXG);
1310 fd = mkstemp(lname);
1313 d_printf("failed to create temporary file for more\n");
1318 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1319 d_printf("more <filename>\n");
1323 rname = talloc_asprintf_append(rname, "%s", fname);
1327 rname = client_clean_name(ctx,rname);
1332 rc = do_get(rname, lname, false);
1334 pager=getenv("PAGER");
1336 pager_cmd = talloc_asprintf(ctx,
1338 (pager? pager:PAGER),
1343 if (system(pager_cmd) == -1) {
1344 d_printf("system command '%s' returned -1\n",
1352 /****************************************************************************
1354 ****************************************************************************/
1356 static int cmd_mget(void)
1358 TALLOC_CTX *ctx = talloc_tos();
1359 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1360 char *mget_mask = NULL;
1362 NTSTATUS status = NT_STATUS_OK;
1365 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1368 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1370 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1374 if (*buf == CLI_DIRSEP_CHAR) {
1375 mget_mask = talloc_strdup(ctx, buf);
1377 mget_mask = talloc_asprintf_append(mget_mask,
1383 mget_mask = client_clean_name(ctx, mget_mask);
1384 if (mget_mask == NULL) {
1387 status = do_list(mget_mask, attribute, do_mget, recurse, true);
1388 if (!NT_STATUS_IS_OK(status)) {
1393 if (mget_mask == NULL) {
1394 d_printf("nothing to mget\n");
1399 mget_mask = talloc_asprintf(ctx,
1401 client_get_cur_dir());
1405 mget_mask = client_clean_name(ctx, mget_mask);
1406 if (mget_mask == NULL) {
1409 status = do_list(mget_mask, attribute, do_mget, recurse, true);
1410 if (!NT_STATUS_IS_OK(status)) {
1418 /****************************************************************************
1419 Make a directory of name "name".
1420 ****************************************************************************/
1422 static bool do_mkdir(const char *name)
1424 TALLOC_CTX *ctx = talloc_tos();
1425 struct cli_state *targetcli;
1426 char *targetname = NULL;
1427 struct cli_credentials *creds = samba_cmdline_get_creds();
1430 status = cli_resolve_path(ctx, "",
1432 cli, name, &targetcli, &targetname);
1433 if (!NT_STATUS_IS_OK(status)) {
1434 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1438 status = cli_mkdir(targetcli, targetname);
1439 if (!NT_STATUS_IS_OK(status)) {
1440 d_printf("%s making remote directory %s\n",
1441 nt_errstr(status),name);
1448 /****************************************************************************
1449 Show 8.3 name of a file.
1450 ****************************************************************************/
1452 static bool do_altname(const char *name)
1457 status = cli_qpathinfo_alt_name(cli, name, altname);
1458 if (!NT_STATUS_IS_OK(status)) {
1459 d_printf("%s getting alt name for %s\n",
1460 nt_errstr(status),name);
1463 d_printf("%s\n", altname);
1468 /****************************************************************************
1470 ****************************************************************************/
1472 static int cmd_quit(void)
1480 /****************************************************************************
1482 ****************************************************************************/
1484 static int cmd_mkdir(void)
1486 TALLOC_CTX *ctx = talloc_tos();
1489 struct cli_credentials *creds = samba_cmdline_get_creds();
1492 mask = talloc_strdup(ctx, client_get_cur_dir());
1497 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1499 d_printf("mkdir <dirname>\n");
1503 mask = talloc_asprintf_append(mask, "%s", buf);
1507 mask = client_clean_name(ctx, mask);
1515 struct cli_state *targetcli;
1516 char *targetname = NULL;
1520 ddir2 = talloc_strdup(ctx, "");
1525 status = cli_resolve_path(ctx, "",
1528 &targetcli, &targetname);
1529 if (!NT_STATUS_IS_OK(status)) {
1533 ddir = talloc_strdup(ctx, targetname);
1537 trim_char(ddir,'.','\0');
1538 p = strtok_r(ddir, "/\\", &saveptr);
1540 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1544 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1547 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1551 p = strtok_r(NULL, "/\\", &saveptr);
1560 /****************************************************************************
1562 ****************************************************************************/
1564 static int cmd_altname(void)
1566 TALLOC_CTX *ctx = talloc_tos();
1570 name = talloc_strdup(ctx, client_get_cur_dir());
1575 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1576 d_printf("altname <file>\n");
1579 name = talloc_asprintf_append(name, "%s", buf);
1583 name = client_clean_name(ctx, name);
1591 static char *attr_str(TALLOC_CTX *mem_ctx, uint32_t attr)
1593 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1596 if (!(attr & FILE_ATTRIBUTE_NORMAL)) {
1597 if (attr & FILE_ATTRIBUTE_ENCRYPTED) {
1600 if (attr & FILE_ATTRIBUTE_NONINDEXED) {
1603 if (attr & FILE_ATTRIBUTE_OFFLINE) {
1606 if (attr & FILE_ATTRIBUTE_COMPRESSED) {
1609 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1612 if (attr & FILE_ATTRIBUTE_SPARSE) {
1615 if (attr & FILE_ATTRIBUTE_TEMPORARY) {
1618 if (attr & FILE_ATTRIBUTE_NORMAL) {
1621 if (attr & FILE_ATTRIBUTE_READONLY) {
1624 if (attr & FILE_ATTRIBUTE_HIDDEN) {
1627 if (attr & FILE_ATTRIBUTE_SYSTEM) {
1630 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
1633 if (attr & FILE_ATTRIBUTE_ARCHIVE) {
1640 /****************************************************************************
1641 Show all info we can get
1642 ****************************************************************************/
1644 static int do_allinfo(const char *name)
1647 struct timespec b_time, a_time, m_time, c_time;
1652 unsigned int num_streams;
1653 struct stream_struct *streams;
1654 int j, num_snapshots;
1655 char **snapshots = NULL;
1659 status = cli_qpathinfo_alt_name(cli, name, altname);
1660 if (!NT_STATUS_IS_OK(status)) {
1661 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1664 * Ignore not supported or not implemented, it does not
1665 * hurt if we can't list alternate names.
1667 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1668 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1674 d_printf("altname: %s\n", altname);
1676 status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1677 &size, &attr, NULL);
1678 if (!NT_STATUS_IS_OK(status)) {
1679 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1684 tmp = full_timespec_to_nt_time(&b_time);
1685 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1687 tmp = full_timespec_to_nt_time(&a_time);
1688 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1690 tmp = full_timespec_to_nt_time(&m_time);
1691 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1693 tmp = full_timespec_to_nt_time(&c_time);
1694 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1696 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), attr), attr);
1698 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1700 if (!NT_STATUS_IS_OK(status)) {
1702 "%s getting streams for %s\n",
1708 for (i=0; i<num_streams; i++) {
1709 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1710 (unsigned long long)streams[i].size);
1713 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1714 struct reparse_data_buffer *rep = NULL;
1715 uint8_t *data = NULL;
1719 rep = talloc_zero(talloc_tos(), struct reparse_data_buffer);
1721 d_printf("talloc_zero() failed\n");
1725 status = cli_get_reparse_data(cli, name, rep, &data, &datalen);
1726 if (!NT_STATUS_IS_OK(status)) {
1728 "cli_get_reparse_data() failed: %s\n",
1734 status = reparse_data_buffer_parse(rep, rep, data, datalen);
1735 if (!NT_STATUS_IS_OK(status)) {
1737 "reparse_data_buffer_parse() failed: %s\n",
1743 s = reparse_data_buffer_str(rep, rep);
1748 status = cli_ntcreate(cli, name, 0,
1749 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1750 SEC_STD_SYNCHRONIZE, 0,
1751 FILE_SHARE_READ|FILE_SHARE_WRITE
1753 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
1754 if (!NT_STATUS_IS_OK(status)) {
1756 * Ignore failure, it does not hurt if we can't list
1762 * In order to get shadow copy data over SMB1 we
1763 * must call twice, once with 'get_names = false'
1764 * to get the size, then again with 'get_names = true'
1765 * to get the data or a Windows server fails to return
1766 * valid info. Samba doesn't have this bug. JRA.
1769 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1770 false, &snapshots, &num_snapshots);
1771 if (NT_STATUS_IS_OK(status)) {
1772 status = cli_shadow_copy_data(talloc_tos(),
1779 if (!NT_STATUS_IS_OK(status)) {
1781 "%s getting shadow copy data for %s\n",
1787 for (j=0; j<num_snapshots; j++) {
1790 d_printf("%s\n", snapshots[j]);
1791 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1792 snapshots[j], name);
1793 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1794 &m_time, &c_time, &size,
1796 if (!NT_STATUS_IS_OK(status)) {
1797 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1798 snap_name, nt_errstr(status));
1799 TALLOC_FREE(snap_name);
1802 tmp = unix_timespec_to_nt_time(b_time);
1803 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1804 tmp = unix_timespec_to_nt_time(a_time);
1805 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1806 tmp =unix_timespec_to_nt_time(m_time);
1807 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1808 tmp = unix_timespec_to_nt_time(c_time);
1809 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1810 d_printf("size: %d\n", (int)size);
1813 TALLOC_FREE(snapshots);
1814 cli_close(cli, fnum);
1819 /****************************************************************************
1820 Show all info we can get
1821 ****************************************************************************/
1823 static int cmd_allinfo(void)
1825 TALLOC_CTX *ctx = talloc_tos();
1829 name = talloc_strdup(ctx, client_get_cur_dir());
1834 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1835 d_printf("allinfo <file>\n");
1838 name = talloc_asprintf_append(name, "%s", buf);
1842 name = client_clean_name(ctx, name);
1851 /****************************************************************************
1853 ****************************************************************************/
1855 static int do_put(const char *rname, const char *lname, bool reput)
1857 TALLOC_CTX *ctx = talloc_tos();
1862 struct timespec tp_start;
1863 struct cli_state *targetcli;
1864 char *targetname = NULL;
1865 struct push_state state;
1866 struct cli_credentials *creds = samba_cmdline_get_creds();
1869 status = cli_resolve_path(ctx, "",
1871 cli, rname, &targetcli, &targetname);
1872 if (!NT_STATUS_IS_OK(status)) {
1873 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1877 clock_gettime_mono(&tp_start);
1880 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1881 if (NT_STATUS_IS_OK(status)) {
1882 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1883 targetcli, fnum, NULL,
1885 NULL, NULL, NULL))) {
1886 d_printf("getattrib: %s\n", nt_errstr(status));
1891 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1894 if (!NT_STATUS_IS_OK(status)) {
1895 d_printf("%s opening remote file %s\n", nt_errstr(status),
1900 /* allow files to be piped into smbclient
1903 Note that in this case this function will exit(0) rather
1905 if (!strcmp(lname, "-")) {
1907 /* size of file is not known */
1909 f = fopen(lname, "r");
1911 if (fseek(f, start, SEEK_SET) == -1) {
1912 d_printf("Error seeking local file\n");
1920 d_printf("Error opening local file %s\n",lname);
1924 DEBUG(1,("putting file %s as %s ",lname,
1927 setvbuf(f, NULL, _IOFBF, io_bufsize);
1932 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1934 if (!NT_STATUS_IS_OK(status)) {
1935 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1939 status = cli_close(targetcli, fnum);
1940 if (!NT_STATUS_IS_OK(status)) {
1941 d_printf("%s closing remote file %s\n", nt_errstr(status),
1954 struct timespec tp_end;
1957 clock_gettime_mono(&tp_end);
1958 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1959 put_total_time_ms += this_time;
1960 put_total_size += state.nread;
1962 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1963 state.nread / (1.024*this_time + 1.0e-4),
1964 put_total_size / (1.024*put_total_time_ms)));
1975 /****************************************************************************
1977 ****************************************************************************/
1979 static int cmd_put(void)
1981 TALLOC_CTX *ctx = talloc_tos();
1986 rname = talloc_strdup(ctx, client_get_cur_dir());
1991 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1992 d_printf("put <filename>\n");
1996 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1997 rname = talloc_asprintf_append(rname, "%s", buf);
1999 rname = talloc_asprintf_append(rname, "%s", lname);
2005 rname = client_clean_name(ctx, rname);
2012 /* allow '-' to represent stdin
2013 jdblair, 24.jun.98 */
2014 if (!file_exist_stat(lname, &st, false) &&
2015 (strcmp(lname,"-"))) {
2016 d_printf("%s does not exist\n",lname);
2021 return do_put(rname, lname, false);
2024 /*************************************
2025 File list structure.
2026 *************************************/
2028 static struct file_list {
2029 struct file_list *prev, *next;
2034 /****************************************************************************
2035 Free a file_list structure.
2036 ****************************************************************************/
2038 static void free_file_list (struct file_list *l_head)
2040 struct file_list *list, *next;
2042 for (list = l_head; list; list = next) {
2044 DLIST_REMOVE(l_head, list);
2049 /****************************************************************************
2050 Seek in a directory/file list until you get something that doesn't start with
2052 ****************************************************************************/
2054 static bool seek_list(struct file_list *list, char *name)
2057 trim_string(list->file_path,"./","\n");
2058 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2067 /****************************************************************************
2068 Set the file selection mask.
2069 ****************************************************************************/
2071 static int cmd_select(void)
2073 TALLOC_CTX *ctx = talloc_tos();
2074 char *new_fs = NULL;
2075 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2078 client_set_fileselection(new_fs);
2080 client_set_fileselection("");
2085 /****************************************************************************
2086 Recursive file matching function act as find
2087 match must be always set to true when calling this function
2088 ****************************************************************************/
2090 static int file_find(TALLOC_CTX *ctx,
2091 struct file_list **list,
2092 const char *directory,
2093 const char *expression,
2097 struct file_list *entry;
2098 struct stat statbuf;
2104 dir = opendir(directory);
2108 while ((dname = readdirname(dir))) {
2109 if (ISDOT(dname) || ISDOTDOT(dname)) {
2113 path = talloc_asprintf(ctx, "%s/%s", directory, dname);
2119 if (!match || !gen_fnmatch(expression, dname)) {
2121 ret = stat(path, &statbuf);
2123 if (S_ISDIR(statbuf.st_mode)) {
2125 ret = file_find(ctx,
2132 d_printf("file_find: cannot stat file %s\n", path);
2141 entry = talloc_zero(ctx, struct file_list);
2143 d_printf("Out of memory in file_find\n");
2147 entry->file_path = talloc_move(entry, &path);
2148 entry->isdir = isdir;
2149 DLIST_ADD(*list, entry);
2159 /****************************************************************************
2161 ****************************************************************************/
2163 static int cmd_mput(void)
2165 TALLOC_CTX *ctx = talloc_tos();
2168 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2170 struct file_list *temp_list;
2171 char *quest, *lname, *rname;
2175 ret = file_find(ctx, &file_list, ".", p, true);
2177 free_file_list(file_list);
2185 for (temp_list = file_list; temp_list;
2186 temp_list = temp_list->next) {
2189 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2192 trim_string(lname, "./", "/");
2194 /* check if it's a directory */
2195 if (temp_list->isdir) {
2196 /* if (!recurse) continue; */
2199 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2202 if (prompt && !yesno(quest)) { /* No */
2203 /* Skip the directory */
2204 lname[strlen(lname)-1] = '/';
2205 if (!seek_list(temp_list, lname))
2209 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2212 normalize_name(rname);
2215 client_clean_name(ctx, rname);
2216 if (tmp_rname == NULL) {
2220 rname = smb_xstrdup(tmp_rname);
2221 TALLOC_FREE(tmp_rname);
2222 if (rname == NULL) {
2226 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2228 DEBUG (0, ("Unable to make dir, skipping...\n"));
2229 /* Skip the directory */
2230 lname[strlen(lname)-1] = '/';
2231 if (!seek_list(temp_list, lname)) {
2239 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2242 if (prompt && !yesno(quest)) {
2249 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2254 normalize_name(rname);
2257 char *tmp_rname = client_clean_name(ctx, rname);
2258 if (tmp_rname == NULL) {
2262 rname = smb_xstrdup(tmp_rname);
2263 TALLOC_FREE(tmp_rname);
2264 if (rname == NULL) {
2268 do_put(rname, lname, false);
2270 free_file_list(file_list);
2279 /****************************************************************************
2281 ****************************************************************************/
2283 static int do_cancel(int job)
2285 NTSTATUS status = cli_printjob_del(cli, job);
2287 if (NT_STATUS_IS_OK(status)) {
2288 d_printf("Job %d cancelled\n",job);
2291 d_printf("Error cancelling job %d : %s\n",
2292 job, nt_errstr(status));
2297 /****************************************************************************
2299 ****************************************************************************/
2301 static int cmd_cancel(void)
2303 TALLOC_CTX *ctx = talloc_tos();
2307 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2308 d_printf("cancel <jobid> ...\n");
2314 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2319 /****************************************************************************
2321 ****************************************************************************/
2323 static int cmd_print(void)
2325 TALLOC_CTX *ctx = talloc_tos();
2330 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2331 d_printf("print <filename>\n");
2335 rname = talloc_strdup(ctx, lname);
2339 p = strrchr_m(rname,'/');
2341 rname = talloc_asprintf(ctx,
2346 if (strequal(lname,"-")) {
2347 rname = talloc_asprintf(ctx,
2355 return do_put(rname, lname, false);
2358 /****************************************************************************
2359 Show a print queue entry.
2360 ****************************************************************************/
2362 static void queue_fn(struct print_job_info *p)
2364 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2367 /****************************************************************************
2369 ****************************************************************************/
2371 static int cmd_queue(void)
2373 cli_print_queue(cli, queue_fn);
2377 /****************************************************************************
2379 ****************************************************************************/
2381 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2384 TALLOC_CTX *ctx = talloc_tos();
2386 struct cli_state *targetcli = NULL;
2387 char *targetname = NULL;
2388 struct cli_credentials *creds = samba_cmdline_get_creds();
2391 mask = talloc_asprintf(ctx,
2397 return NT_STATUS_NO_MEMORY;
2400 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2402 return NT_STATUS_OK;
2405 status = cli_resolve_path(ctx, "",
2407 cli, mask, &targetcli, &targetname);
2408 if (!NT_STATUS_IS_OK(status)) {
2412 status = cli_unlink(targetcli, targetname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2414 if (!NT_STATUS_IS_OK(status)) {
2415 d_printf("%s deleting remote file %s\n",
2416 nt_errstr(status), mask);
2422 /****************************************************************************
2424 ****************************************************************************/
2426 static int cmd_del(void)
2428 TALLOC_CTX *ctx = talloc_tos();
2431 NTSTATUS status = NT_STATUS_OK;
2432 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2435 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2438 mask = talloc_strdup(ctx, client_get_cur_dir());
2442 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2443 d_printf("del <filename>\n");
2446 mask = talloc_asprintf_append(mask, "%s", buf);
2450 mask = client_clean_name(ctx, mask);
2455 status = do_list(mask,attribute,do_del,false,false);
2456 if (!NT_STATUS_IS_OK(status)) {
2462 /****************************************************************************
2464 ****************************************************************************/
2466 static NTSTATUS delete_remote_files_list(struct cli_state *cli_state,
2467 struct file_list *flist)
2469 NTSTATUS status = NT_STATUS_OK;
2470 struct file_list *deltree_list_iter = NULL;
2471 char *targetname = NULL;
2472 struct cli_state *targetcli = NULL;
2473 struct cli_credentials *creds = samba_cmdline_get_creds();
2474 TALLOC_CTX *ctx = talloc_tos();
2476 for (deltree_list_iter = flist;
2477 deltree_list_iter != NULL;
2478 deltree_list_iter = deltree_list_iter->next) {
2479 status = cli_resolve_path(ctx,
2483 deltree_list_iter->file_path,
2486 if (!NT_STATUS_IS_OK(status)) {
2487 d_printf("delete_remote_files %s: %s\n",
2488 deltree_list_iter->file_path,
2492 if (CLI_DIRSEP_CHAR == '/') {
2494 status = cli_posix_unlink(targetcli,
2496 } else if (deltree_list_iter->isdir) {
2497 status = cli_rmdir(targetcli,
2500 status = cli_unlink(targetcli,
2502 FILE_ATTRIBUTE_SYSTEM |
2503 FILE_ATTRIBUTE_HIDDEN);
2505 if (!NT_STATUS_IS_OK(status)) {
2506 d_printf("%s deleting remote %s %s\n",
2508 deltree_list_iter->isdir ?
2509 "directory" : "file",
2510 deltree_list_iter->file_path);
2514 return NT_STATUS_OK;
2517 /****************************************************************************
2518 Save a list of files to delete.
2519 ****************************************************************************/
2521 static struct file_list *deltree_list_head;
2523 static NTSTATUS do_deltree_list(struct cli_state *cli_state,
2524 struct file_info *finfo,
2527 struct file_list **file_list_head_pp = &deltree_list_head;
2528 struct file_list *dt = NULL;
2530 if (!do_this_one(finfo)) {
2531 return NT_STATUS_OK;
2534 /* skip if this is . or .. */
2535 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
2536 return NT_STATUS_OK;
2539 dt = talloc_zero(NULL, struct file_list);
2541 return NT_STATUS_NO_MEMORY;
2544 /* create absolute filename for cli_ntcreate() */
2545 dt->file_path = talloc_asprintf(dt,
2550 if (dt->file_path == NULL) {
2552 return NT_STATUS_NO_MEMORY;
2555 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2559 DLIST_ADD(*file_list_head_pp, dt);
2560 return NT_STATUS_OK;
2563 static int cmd_deltree(void)
2565 TALLOC_CTX *ctx = talloc_tos();
2567 NTSTATUS status = NT_STATUS_OK;
2568 struct file_list *deltree_list_norecurse = NULL;
2569 struct file_list *deltree_list_iter = NULL;
2570 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM |
2571 FILE_ATTRIBUTE_HIDDEN |
2572 FILE_ATTRIBUTE_DIRECTORY;
2574 char *mask = talloc_strdup(ctx, client_get_cur_dir());
2578 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
2580 d_printf("deltree <filename>\n");
2583 mask = talloc_asprintf_append(mask, "%s", buf);
2587 mask = client_clean_name(ctx, mask);
2592 deltree_list_head = NULL;
2595 * Get the list of directories to
2596 * delete (in case mask has a wildcard).
2598 status = do_list(mask, attribute, do_deltree_list, false, true);
2599 if (!NT_STATUS_IS_OK(status)) {
2602 deltree_list_norecurse = deltree_list_head;
2603 deltree_list_head = NULL;
2605 for (deltree_list_iter = deltree_list_norecurse;
2606 deltree_list_iter != NULL;
2607 deltree_list_iter = deltree_list_iter->next) {
2609 if (deltree_list_iter->isdir == false) {
2610 char *targetname = NULL;
2611 struct cli_state *targetcli = NULL;
2612 struct cli_credentials *creds = samba_cmdline_get_creds();
2613 status = cli_resolve_path(ctx,
2617 deltree_list_iter->file_path,
2620 if (!NT_STATUS_IS_OK(status)) {
2623 /* Just a regular file. */
2624 if (CLI_DIRSEP_CHAR == '/') {
2626 status = cli_posix_unlink(targetcli,
2629 status = cli_unlink(targetcli,
2631 FILE_ATTRIBUTE_SYSTEM |
2632 FILE_ATTRIBUTE_HIDDEN);
2634 if (!NT_STATUS_IS_OK(status)) {
2641 * Get the list of files or directories to
2642 * delete in depth order.
2644 status = do_list(deltree_list_iter->file_path,
2649 if (!NT_STATUS_IS_OK(status)) {
2652 status = delete_remote_files_list(cli, deltree_list_head);
2653 free_file_list(deltree_list_head);
2654 deltree_list_head = NULL;
2655 if (!NT_STATUS_IS_OK(status)) {
2660 free_file_list(deltree_list_norecurse);
2661 free_file_list(deltree_list_head);
2666 free_file_list(deltree_list_norecurse);
2667 free_file_list(deltree_list_head);
2668 deltree_list_head = NULL;
2673 /****************************************************************************
2674 Wildcard delete some files.
2675 ****************************************************************************/
2677 static int cmd_wdel(void)
2679 TALLOC_CTX *ctx = talloc_tos();
2683 struct cli_state *targetcli;
2684 char *targetname = NULL;
2685 struct cli_credentials *creds = samba_cmdline_get_creds();
2688 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2689 d_printf("wdel 0x<attrib> <wcard>\n");
2693 attribute = (uint32_t)strtol(buf, (char **)NULL, 16);
2695 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2696 d_printf("wdel 0x<attrib> <wcard>\n");
2700 mask = talloc_asprintf(ctx, "%s%s",
2701 client_get_cur_dir(),
2706 mask = client_clean_name(ctx, mask);
2711 status = cli_resolve_path(ctx, "",
2713 cli, mask, &targetcli, &targetname);
2714 if (!NT_STATUS_IS_OK(status)) {
2715 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2719 status = cli_unlink(targetcli, targetname, attribute);
2720 if (!NT_STATUS_IS_OK(status)) {
2721 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2727 /****************************************************************************
2728 ****************************************************************************/
2730 static int cmd_open(void)
2732 TALLOC_CTX *ctx = talloc_tos();
2735 char *targetname = NULL;
2736 struct cli_state *targetcli;
2737 uint16_t fnum = (uint16_t)-1;
2738 struct cli_credentials *creds = samba_cmdline_get_creds();
2741 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2742 d_printf("open <filename>\n");
2745 mask = talloc_asprintf(ctx,
2747 client_get_cur_dir(),
2753 mask = client_clean_name(ctx, mask);
2758 status = cli_resolve_path(ctx, "",
2760 cli, mask, &targetcli, &targetname);
2761 if (!NT_STATUS_IS_OK(status)) {
2762 d_printf("open %s: %s\n", mask, nt_errstr(status));
2766 status = cli_ntcreate(targetcli, targetname, 0,
2767 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2768 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2769 0x0, 0x0, &fnum, NULL);
2770 if (!NT_STATUS_IS_OK(status)) {
2771 status = cli_ntcreate(targetcli, targetname, 0,
2773 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2774 0x0, 0x0, &fnum, NULL);
2775 if (NT_STATUS_IS_OK(status)) {
2776 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2778 d_printf("Failed to open file %s. %s\n",
2779 targetname, nt_errstr(status));
2782 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2787 static int cmd_posix_encrypt(void)
2789 TALLOC_CTX *ctx = talloc_tos();
2790 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2791 char *domain = NULL;
2793 char *password = NULL;
2794 struct cli_credentials *creds = NULL;
2795 struct cli_credentials *lcreds = NULL;
2797 if (next_token_talloc(ctx, &cmd_ptr, &domain, NULL)) {
2799 if (!next_token_talloc(ctx, &cmd_ptr, &user, NULL)) {
2800 d_printf("posix_encrypt domain user password\n");
2804 if (!next_token_talloc(ctx, &cmd_ptr, &password, NULL)) {
2805 d_printf("posix_encrypt domain user password\n");
2809 lcreds = cli_session_creds_init(ctx,
2814 false, /* use_kerberos */
2815 false, /* fallback_after_kerberos */
2816 false, /* use_ccache */
2817 false); /* password_is_nt_hash */
2818 if (lcreds == NULL) {
2819 d_printf("cli_session_creds_init() failed.\n");
2824 bool auth_requested = false;
2826 creds = samba_cmdline_get_creds();
2828 auth_requested = cli_credentials_authentication_requested(creds);
2829 if (!auth_requested) {
2830 d_printf("posix_encrypt domain user password\n");
2835 status = cli_smb1_setup_encryption(cli, creds);
2836 /* gensec currently references the creds so we can't free them here */
2837 talloc_unlink(ctx, lcreds);
2838 if (!NT_STATUS_IS_OK(status)) {
2839 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2843 d_printf("encryption on\n");
2844 ok = cli_credentials_set_smb_encryption(creds,
2845 SMB_ENCRYPTION_REQUIRED,
2853 /****************************************************************************
2854 ****************************************************************************/
2856 static int cmd_posix_open(void)
2858 TALLOC_CTX *ctx = talloc_tos();
2861 char *targetname = NULL;
2862 struct cli_state *targetcli;
2865 struct cli_credentials *creds = samba_cmdline_get_creds();
2868 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2869 d_printf("posix_open <filename> 0<mode>\n");
2872 mask = talloc_asprintf(ctx,
2874 client_get_cur_dir(),
2879 mask = client_clean_name(ctx, mask);
2884 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2885 d_printf("posix_open <filename> 0<mode>\n");
2888 if (CLI_DIRSEP_CHAR != '/') {
2889 d_printf("Command \"posix\" must be issued before "
2890 "the \"posix_open\" command can be used.\n");
2893 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2895 status = cli_resolve_path(ctx, "",
2897 cli, mask, &targetcli, &targetname);
2898 if (!NT_STATUS_IS_OK(status)) {
2899 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2903 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2905 if (!NT_STATUS_IS_OK(status)) {
2906 status = cli_posix_open(targetcli, targetname,
2907 O_CREAT|O_RDONLY, mode, &fnum);
2908 if (!NT_STATUS_IS_OK(status)) {
2909 d_printf("Failed to open file %s. %s\n", targetname,
2912 d_printf("posix_open file %s: for readonly fnum %d\n",
2916 d_printf("posix_open file %s: for read/write fnum %d\n",
2923 static int cmd_posix_mkdir(void)
2925 TALLOC_CTX *ctx = talloc_tos();
2928 char *targetname = NULL;
2929 struct cli_state *targetcli;
2931 struct cli_credentials *creds = samba_cmdline_get_creds();
2934 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2935 d_printf("posix_mkdir <filename> 0<mode>\n");
2938 mask = talloc_asprintf(ctx,
2940 client_get_cur_dir(),
2945 mask = client_clean_name(ctx, mask);
2950 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2951 d_printf("posix_mkdir <filename> 0<mode>\n");
2954 if (CLI_DIRSEP_CHAR != '/') {
2955 d_printf("Command \"posix\" must be issued before "
2956 "the \"posix_mkdir\" command can be used.\n");
2959 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2961 status = cli_resolve_path(ctx, "",
2963 cli, mask, &targetcli, &targetname);
2964 if (!NT_STATUS_IS_OK(status)) {
2965 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2969 status = cli_posix_mkdir(targetcli, targetname, mode);
2970 if (!NT_STATUS_IS_OK(status)) {
2971 d_printf("Failed to open file %s. %s\n",
2972 targetname, nt_errstr(status));
2974 d_printf("posix_mkdir created directory %s\n", targetname);
2979 static int cmd_posix_unlink(void)
2981 TALLOC_CTX *ctx = talloc_tos();
2984 char *targetname = NULL;
2985 struct cli_state *targetcli;
2986 struct cli_credentials *creds = samba_cmdline_get_creds();
2989 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2990 d_printf("posix_unlink <filename>\n");
2993 if (CLI_DIRSEP_CHAR != '/') {
2994 d_printf("Command \"posix\" must be issued before "
2995 "the \"posix_unlink\" command can be used.\n");
2998 mask = talloc_asprintf(ctx,
3000 client_get_cur_dir(),
3005 mask = client_clean_name(ctx, mask);
3010 status = cli_resolve_path(ctx, "",
3012 cli, mask, &targetcli, &targetname);
3013 if (!NT_STATUS_IS_OK(status)) {
3014 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
3018 status = cli_posix_unlink(targetcli, targetname);
3019 if (!NT_STATUS_IS_OK(status)) {
3020 d_printf("Failed to unlink file %s. %s\n",
3021 targetname, nt_errstr(status));
3023 d_printf("posix_unlink deleted file %s\n", targetname);
3029 static int cmd_posix_rmdir(void)
3031 TALLOC_CTX *ctx = talloc_tos();
3034 char *targetname = NULL;
3035 struct cli_state *targetcli;
3036 struct cli_credentials *creds = samba_cmdline_get_creds();
3039 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3040 d_printf("posix_rmdir <filename>\n");
3043 if (CLI_DIRSEP_CHAR != '/') {
3044 d_printf("Command \"posix\" must be issued before "
3045 "the \"posix_rmdir\" command can be used.\n");
3048 mask = talloc_asprintf(ctx,
3050 client_get_cur_dir(),
3055 mask = client_clean_name(ctx, mask);
3060 status = cli_resolve_path(ctx, "",
3062 cli, mask, &targetcli, &targetname);
3063 if (!NT_STATUS_IS_OK(status)) {
3064 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
3068 status = cli_posix_rmdir(targetcli, targetname);
3069 if (!NT_STATUS_IS_OK(status)) {
3070 d_printf("Failed to unlink directory %s. %s\n",
3071 targetname, nt_errstr(status));
3073 d_printf("posix_rmdir deleted directory %s\n", targetname);
3079 static int cmd_mkfifo(void)
3081 TALLOC_CTX *ctx = talloc_tos();
3084 char *targetname = NULL;
3085 struct cli_state *targetcli;
3087 struct cli_credentials *creds = samba_cmdline_get_creds();
3090 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3091 d_printf("mkfifo <filename> 0<mode>\n");
3094 mask = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(), buf);
3098 mask = client_clean_name(ctx, mask);
3103 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3104 d_printf("mkfifo <filename> 0<mode>\n");
3108 mode = (mode_t)strtol(buf, (char **)NULL, 8);
3109 if ((mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) != 0) {
3110 d_printf("mode %o can only contain permission bits\n", mode);
3114 status = cli_resolve_path(ctx,
3121 if (!NT_STATUS_IS_OK(status)) {
3122 d_printf("mkfifo %s: %s\n", mask, nt_errstr(status));
3126 status = cli_mknod(targetcli, targetname, mode | S_IFIFO, 0);
3127 if (!NT_STATUS_IS_OK(status)) {
3128 d_printf("Failed to open file %s. %s\n",
3132 d_printf("mkfifo created %s\n", targetname);
3137 static int cmd_close(void)
3139 TALLOC_CTX *ctx = talloc_tos();
3144 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3145 d_printf("close <fnum>\n");
3150 /* We really should use the targetcli here.... */
3151 status = cli_close(cli, fnum);
3152 if (!NT_STATUS_IS_OK(status)) {
3153 d_printf("close %d: %s\n", fnum, nt_errstr(status));
3159 static int cmd_posix(void)
3161 TALLOC_CTX *ctx = talloc_tos();
3162 uint16_t major, minor;
3163 uint32_t caplow, caphigh;
3167 if (!smbXcli_conn_have_posix(cli->conn)) {
3168 d_printf("Server doesn't support UNIX CIFS extensions.\n");
3172 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB3_11) {
3173 cli->smb2.client_smb311_posix = true;
3177 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
3179 if (!NT_STATUS_IS_OK(status)) {
3180 d_printf("Can't get UNIX CIFS extensions version from "
3181 "server: %s\n", nt_errstr(status));
3185 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
3187 caps = talloc_strdup(ctx, "");
3188 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
3189 talloc_asprintf_addbuf(&caps, "locks ");
3191 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
3192 talloc_asprintf_addbuf(&caps, "acls ");
3194 if (caplow & CIFS_UNIX_XATTTR_CAP) {
3195 talloc_asprintf_addbuf(&caps, "eas ");
3197 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3198 talloc_asprintf_addbuf(&caps, "pathnames ");
3200 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
3201 talloc_asprintf_addbuf(&caps, "posix_path_operations ");
3203 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
3204 talloc_asprintf_addbuf(&caps, "large_read ");
3206 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
3207 talloc_asprintf_addbuf(&caps, "large_write ");
3209 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
3210 talloc_asprintf_addbuf(&caps, "posix_encrypt ");
3212 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
3213 talloc_asprintf_addbuf(&caps, "mandatory_posix_encrypt ");
3220 if (*caps && caps[strlen(caps)-1] == ' ') {
3221 caps[strlen(caps)-1] = '\0';
3224 d_printf("Server supports CIFS capabilities %s\n", caps);
3226 status = cli_set_unix_extensions_capabilities(cli, major, minor,
3228 if (!NT_STATUS_IS_OK(status)) {
3229 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
3234 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3235 CLI_DIRSEP_CHAR = '/';
3236 *CLI_DIRSEP_STR = '/';
3237 client_set_cur_dir(CLI_DIRSEP_STR);
3243 static int cmd_lock(void)
3245 TALLOC_CTX *ctx = talloc_tos();
3247 uint64_t start, len;
3248 enum brl_type lock_type;
3252 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3253 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3258 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3259 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3263 if (*buf == 'r' || *buf == 'R') {
3264 lock_type = READ_LOCK;
3265 } else if (*buf == 'w' || *buf == 'W') {
3266 lock_type = WRITE_LOCK;
3268 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3272 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3273 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3277 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3279 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3280 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3284 if (CLI_DIRSEP_CHAR != '/') {
3285 d_printf("Command \"posix\" must be issued before "
3286 "the \"lock\" command can be used.\n");
3290 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3292 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
3293 if (!NT_STATUS_IS_OK(status)) {
3294 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
3300 static int cmd_unlock(void)
3302 TALLOC_CTX *ctx = talloc_tos();
3304 uint64_t start, len;
3308 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3309 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3314 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3315 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3319 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3321 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3322 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3326 if (CLI_DIRSEP_CHAR != '/') {
3327 d_printf("Command \"posix\" must be issued before "
3328 "the \"unlock\" command can be used.\n");
3332 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3334 status = cli_posix_unlock(cli, fnum, start, len);
3335 if (!NT_STATUS_IS_OK(status)) {
3336 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
3342 static int cmd_posix_whoami(void)
3344 TALLOC_CTX *ctx = talloc_tos();
3345 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
3348 uint32_t num_gids = 0;
3349 uint32_t num_sids = 0;
3350 uint64_t *gids = NULL;
3351 struct dom_sid *sids = NULL;
3355 if (CLI_DIRSEP_CHAR != '/') {
3356 d_printf("Command \"posix\" must be issued before "
3357 "the \"posix_whoami\" command can be used.\n");
3361 status = cli_posix_whoami(cli,
3371 if (!NT_STATUS_IS_OK(status)) {
3372 d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
3376 d_printf("GUEST:%s\n", guest ? "True" : "False");
3377 d_printf("UID:%" PRIu64 "\n", uid);
3378 d_printf("GID:%" PRIu64 "\n", gid);
3379 d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
3380 for (i = 0; i < num_gids; i++) {
3381 d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
3383 d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
3384 for (i = 0; i < num_sids; i++) {
3385 struct dom_sid_buf buf;
3386 d_printf("SIDS[%" PRIu32 "]:%s\n",
3388 dom_sid_str_buf(&sids[i], &buf));
3394 /****************************************************************************
3396 ****************************************************************************/
3398 static int cmd_rmdir(void)
3400 TALLOC_CTX *ctx = talloc_tos();
3403 char *targetname = NULL;
3404 struct cli_state *targetcli;
3405 struct cli_credentials *creds = samba_cmdline_get_creds();
3408 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3409 d_printf("rmdir <dirname>\n");
3412 mask = talloc_asprintf(ctx,
3414 client_get_cur_dir(),
3419 mask = client_clean_name(ctx, mask);
3424 status = cli_resolve_path(ctx, "",
3426 cli, mask, &targetcli, &targetname);
3427 if (!NT_STATUS_IS_OK(status)) {
3428 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3432 status = cli_rmdir(targetcli, targetname);
3433 if (!NT_STATUS_IS_OK(status)) {
3434 d_printf("%s removing remote directory file %s\n",
3435 nt_errstr(status), mask);
3441 /****************************************************************************
3443 ****************************************************************************/
3445 static int cmd_link(void)
3447 TALLOC_CTX *ctx = talloc_tos();
3448 char *oldname = NULL;
3449 char *newname = NULL;
3452 char *targetname = NULL;
3453 struct cli_state *targetcli;
3454 struct cli_credentials *creds = samba_cmdline_get_creds();
3457 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3458 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3459 d_printf("link <oldname> <newname>\n");
3462 oldname = talloc_asprintf(ctx,
3464 client_get_cur_dir(),
3469 oldname = client_clean_name(ctx, oldname);
3470 if (oldname == NULL) {
3473 newname = talloc_asprintf(ctx,
3475 client_get_cur_dir(),
3480 newname = client_clean_name(ctx, newname);
3481 if (newname == NULL) {
3485 status = cli_resolve_path(ctx, "",
3487 cli, oldname, &targetcli, &targetname);
3488 if (!NT_STATUS_IS_OK(status)) {
3489 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3493 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3494 d_printf("Server doesn't support UNIX CIFS calls.\n");
3498 if (CLI_DIRSEP_CHAR != '/') {
3499 d_printf("Command \"posix\" must be issued before "
3500 "the \"link\" command can be used.\n");
3504 status = cli_posix_hardlink(targetcli, targetname, newname);
3505 if (!NT_STATUS_IS_OK(status)) {
3506 d_printf("%s linking files (%s -> %s)\n",
3507 nt_errstr(status), newname, oldname);
3513 /****************************************************************************
3515 ****************************************************************************/
3517 static int cmd_readlink(void)
3519 TALLOC_CTX *ctx = talloc_tos();
3522 char *targetname = NULL;
3523 char *linkname = NULL;
3524 char *printname = NULL;
3526 struct cli_state *targetcli;
3527 struct cli_credentials *creds = samba_cmdline_get_creds();
3530 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3531 d_printf("readlink <name>\n");
3534 name = talloc_asprintf(ctx,
3536 client_get_cur_dir(),
3541 name = client_clean_name(ctx, name);
3546 status = cli_resolve_path(ctx, "",
3548 cli, name, &targetcli, &targetname);
3549 if (!NT_STATUS_IS_OK(status)) {
3550 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3554 status = cli_readlink(
3555 cli, name, talloc_tos(), &linkname, &printname, &flags);
3556 if (!NT_STATUS_IS_OK(status)) {
3557 d_printf("%s readlink on file %s\n",
3558 nt_errstr(status), name);
3562 d_printf("%s -> %s\n", name, linkname);
3564 TALLOC_FREE(linkname);
3570 /****************************************************************************
3572 ****************************************************************************/
3574 static int cmd_symlink(void)
3576 TALLOC_CTX *ctx = talloc_tos();
3577 char *link_target = NULL;
3578 char *newname = NULL;
3581 struct cli_state *newcli;
3582 struct cli_credentials *creds = samba_cmdline_get_creds();
3585 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3586 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3587 d_printf("symlink <link_target> <newname>\n");
3590 /* Oldname (link target) must be an untouched blob. */
3593 if (SERVER_HAS_UNIX_CIFS(cli)) {
3594 if (CLI_DIRSEP_CHAR != '/') {
3595 d_printf("Command \"posix\" must be issued before "
3596 "the \"symlink\" command can be used.\n");
3599 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3604 newname = client_clean_name(ctx, newname);
3605 if (newname == NULL) {
3608 /* New name must be present in share namespace. */
3609 status = cli_resolve_path(ctx, "",
3613 if (!NT_STATUS_IS_OK(status)) {
3614 d_printf("link %s: %s\n", newname,
3618 status = cli_posix_symlink(newcli, link_target, newname);
3620 status = cli_symlink(
3621 cli, link_target, buf2,
3622 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3625 if (!NT_STATUS_IS_OK(status)) {
3626 d_printf("%s symlinking files (%s -> %s)\n",
3627 nt_errstr(status), newname, link_target);
3634 /****************************************************************************
3636 ****************************************************************************/
3638 static int cmd_chmod(void)
3640 TALLOC_CTX *ctx = talloc_tos();
3644 char *targetname = NULL;
3645 struct cli_state *targetcli;
3647 struct cli_credentials *creds = samba_cmdline_get_creds();
3650 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3651 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3652 d_printf("chmod mode file\n");
3655 src = talloc_asprintf(ctx,
3657 client_get_cur_dir(),
3662 src = client_clean_name(ctx, src);
3667 mode = (mode_t)strtol(buf, NULL, 8);
3669 status = cli_resolve_path(ctx, "",
3671 cli, src, &targetcli, &targetname);
3672 if (!NT_STATUS_IS_OK(status)) {
3673 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3677 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3678 d_printf("Server doesn't support UNIX CIFS calls.\n");
3682 if (CLI_DIRSEP_CHAR != '/') {
3683 d_printf("Command \"posix\" must be issued before "
3684 "the \"chmod\" command can be used.\n");
3688 status = cli_posix_chmod(targetcli, targetname, mode);
3689 if (!NT_STATUS_IS_OK(status)) {
3690 d_printf("%s chmod file %s 0%o\n",
3691 nt_errstr(status), src, (unsigned int)mode);
3698 static const char *filetype_to_str(mode_t mode)
3700 if (S_ISREG(mode)) {
3701 return "regular file";
3702 } else if (S_ISDIR(mode)) {
3706 if (S_ISCHR(mode)) {
3707 return "character device";
3711 if (S_ISBLK(mode)) {
3712 return "block device";
3716 if (S_ISFIFO(mode)) {
3721 if (S_ISLNK(mode)) {
3722 return "symbolic link";
3726 if (S_ISSOCK(mode)) {
3733 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3742 static char *unix_mode_to_str(char *s, mode_t m)
3745 const char *str = filetype_to_str(m);
3761 *p++ = str[1] == 'y' ? 'l' : 's';
3768 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3769 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3770 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3771 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3772 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3773 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3774 *p++ = rwx_to_str(m, S_IROTH, 'r');
3775 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3776 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3781 /****************************************************************************
3782 Utility function for UNIX getfacl.
3783 ****************************************************************************/
3785 static char *perms_to_string(fstring permstr, unsigned char perms)
3787 fstrcpy(permstr, "---");
3788 if (perms & SMB_POSIX_ACL_READ) {
3791 if (perms & SMB_POSIX_ACL_WRITE) {
3794 if (perms & SMB_POSIX_ACL_EXECUTE) {
3800 /****************************************************************************
3802 ****************************************************************************/
3804 static int cmd_getfacl(void)
3806 TALLOC_CTX *ctx = talloc_tos();
3809 char *targetname = NULL;
3810 struct cli_state *targetcli;
3811 uint16_t major, minor;
3812 uint32_t caplow, caphigh;
3813 char *retbuf = NULL;
3815 SMB_STRUCT_STAT sbuf;
3816 size_t num_file_acls = 0;
3817 size_t num_dir_acls = 0;
3818 size_t expected_buflen;
3820 struct cli_credentials *creds = samba_cmdline_get_creds();
3823 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3824 d_printf("getfacl filename\n");
3827 src = talloc_asprintf(ctx,
3829 client_get_cur_dir(),
3834 src = client_clean_name(ctx, src);
3839 status = cli_resolve_path(ctx, "",
3841 cli, src, &targetcli, &targetname);
3842 if (!NT_STATUS_IS_OK(status)) {
3843 d_printf("stat %s: %s\n", src, nt_errstr(status));
3847 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3848 d_printf("Server doesn't support UNIX CIFS calls.\n");
3852 if (CLI_DIRSEP_CHAR != '/') {
3853 d_printf("Command \"posix\" must be issued before "
3854 "the \"getfacl\" command can be used.\n");
3858 status = cli_unix_extensions_version(targetcli, &major, &minor,
3860 if (!NT_STATUS_IS_OK(status)) {
3861 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3866 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3867 d_printf("This server supports UNIX extensions "
3868 "but doesn't support POSIX ACLs.\n");
3872 status = cli_posix_stat(targetcli, targetname, &sbuf);
3873 if (!NT_STATUS_IS_OK(status)) {
3874 d_printf("%s getfacl doing a stat on file %s\n",
3875 nt_errstr(status), src);
3879 status = cli_posix_getacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3880 if (!NT_STATUS_IS_OK(status)) {
3881 d_printf("%s getfacl file %s\n",
3882 nt_errstr(status), src);
3886 /* ToDo : Print out the ACL values. */
3887 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3888 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3889 src, (unsigned int)CVAL(retbuf,0) );
3893 num_file_acls = SVAL(retbuf,2);
3894 num_dir_acls = SVAL(retbuf,4);
3897 * No overflow check, num_*_acls comes from a 16-bit value,
3898 * and we expect expected_buflen (size_t) to be of at least 32
3901 expected_buflen = SMB_POSIX_ACL_HEADER_SIZE +
3902 SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls);
3904 if (rb_size != expected_buflen) {
3905 d_printf("getfacl file %s, incorrect POSIX acl buffer size "
3906 "(should be %zu, was %zu).\n",
3913 d_printf("# file: %s\n", src);
3914 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3916 if (num_file_acls == 0 && num_dir_acls == 0) {
3917 d_printf("No acls found.\n");
3920 for (i = 0; i < num_file_acls; i++) {
3923 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3924 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3927 case SMB_POSIX_ACL_USER_OBJ:
3930 case SMB_POSIX_ACL_USER:
3931 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3932 d_printf("user:%u:", uorg);
3934 case SMB_POSIX_ACL_GROUP_OBJ:
3935 d_printf("group::");
3937 case SMB_POSIX_ACL_GROUP:
3938 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3939 d_printf("group:%u:", uorg);
3941 case SMB_POSIX_ACL_MASK:
3944 case SMB_POSIX_ACL_OTHER:
3945 d_printf("other::");
3948 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3949 src, (unsigned int)tagtype );
3954 d_printf("%s\n", perms_to_string(permstring, perms));
3957 for (i = 0; i < num_dir_acls; i++) {
3960 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3961 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3964 case SMB_POSIX_ACL_USER_OBJ:
3965 d_printf("default:user::");
3967 case SMB_POSIX_ACL_USER:
3968 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3969 d_printf("default:user:%u:", uorg);
3971 case SMB_POSIX_ACL_GROUP_OBJ:
3972 d_printf("default:group::");
3974 case SMB_POSIX_ACL_GROUP:
3975 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3976 d_printf("default:group:%u:", uorg);
3978 case SMB_POSIX_ACL_MASK:
3979 d_printf("default:mask::");
3981 case SMB_POSIX_ACL_OTHER:
3982 d_printf("default:other::");
3985 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3986 src, (unsigned int)tagtype );
3991 d_printf("%s\n", perms_to_string(permstring, perms));
3997 /****************************************************************************
3998 Get the EA list of a file
3999 ****************************************************************************/
4001 static int cmd_geteas(void)
4003 TALLOC_CTX *ctx = talloc_tos();
4006 char *targetname = NULL;
4007 struct cli_state *targetcli;
4010 struct ea_struct *eas;
4011 struct cli_credentials *creds = samba_cmdline_get_creds();
4013 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
4014 d_printf("geteas filename\n");
4017 src = talloc_asprintf(ctx,
4019 client_get_cur_dir(),
4024 src = client_clean_name(ctx, src);
4029 status = cli_resolve_path(ctx, "",
4031 cli, src, &targetcli, &targetname);
4032 if (!NT_STATUS_IS_OK(status)) {
4033 d_printf("stat %s: %s\n", src, nt_errstr(status));
4037 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
4039 if (!NT_STATUS_IS_OK(status)) {
4040 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
4044 for (i=0; i<num_eas; i++) {
4045 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
4046 dump_data_file(eas[i].value.data, eas[i].value.length, false,
4056 /****************************************************************************
4058 ****************************************************************************/
4060 static int cmd_setea(void)
4062 TALLOC_CTX *ctx = talloc_tos();
4065 char *eaname = NULL;
4066 char *eavalue = NULL;
4067 char *targetname = NULL;
4068 struct cli_state *targetcli;
4069 struct cli_credentials *creds = samba_cmdline_get_creds();
4072 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
4073 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
4074 d_printf("setea filename eaname value\n");
4077 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
4078 eavalue = talloc_strdup(ctx, "");
4080 src = talloc_asprintf(ctx,
4082 client_get_cur_dir(),
4087 src = client_clean_name(ctx, src);
4092 status = cli_resolve_path(ctx, "",
4094 cli, src, &targetcli, &targetname);
4095 if (!NT_STATUS_IS_OK(status)) {
4096 d_printf("stat %s: %s\n", src, nt_errstr(status));
4100 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
4102 if (!NT_STATUS_IS_OK(status)) {
4103 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
4110 /****************************************************************************
4112 ****************************************************************************/
4114 static int cmd_stat(void)
4116 TALLOC_CTX *ctx = talloc_tos();
4119 char *targetname = NULL;
4120 struct cli_state *targetcli;
4122 SMB_STRUCT_STAT sbuf;
4125 struct cli_credentials *creds = samba_cmdline_get_creds();
4128 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
4129 d_printf("stat file\n");
4132 src = talloc_asprintf(ctx,
4134 client_get_cur_dir(),
4139 src = client_clean_name(ctx, src);
4144 status = cli_resolve_path(ctx, "",
4146 cli, src, &targetcli, &targetname);
4147 if (!NT_STATUS_IS_OK(status)) {
4148 d_printf("stat %s: %s\n", src, nt_errstr(status));
4152 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4153 d_printf("Server doesn't support UNIX CIFS calls.\n");
4157 if (CLI_DIRSEP_CHAR != '/') {
4158 d_printf("Command \"posix\" must be issued before "
4159 "the \"stat\" command can be used.\n");
4163 status = cli_posix_stat(targetcli, targetname, &sbuf);
4164 if (!NT_STATUS_IS_OK(status)) {
4165 d_printf("%s stat file %s\n",
4166 nt_errstr(status), src);
4170 /* Print out the stat values. */
4171 d_printf("File: %s\n", src);
4172 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
4173 (double)sbuf.st_ex_size,
4174 (unsigned int)sbuf.st_ex_blocks,
4175 filetype_to_str(sbuf.st_ex_mode));
4177 #if defined(S_ISCHR) && defined(S_ISBLK)
4178 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
4179 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
4180 (double)sbuf.st_ex_ino,
4181 (unsigned int)sbuf.st_ex_nlink,
4182 unix_dev_major(sbuf.st_ex_rdev),
4183 unix_dev_minor(sbuf.st_ex_rdev));
4186 d_printf("Inode: %.0f\tLinks: %u\n",
4187 (double)sbuf.st_ex_ino,
4188 (unsigned int)sbuf.st_ex_nlink);
4190 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
4191 ((int)sbuf.st_ex_mode & 0777),
4192 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
4193 (unsigned int)sbuf.st_ex_uid,
4194 (unsigned int)sbuf.st_ex_gid);
4196 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
4197 lt = localtime(&tmp_time);
4199 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4201 fstrcpy(mode_str, "unknown");
4203 d_printf("Access: %s\n", mode_str);
4205 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4206 lt = localtime(&tmp_time);
4208 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4210 fstrcpy(mode_str, "unknown");
4212 d_printf("Modify: %s\n", mode_str);
4214 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
4215 lt = localtime(&tmp_time);
4217 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4219 fstrcpy(mode_str, "unknown");
4221 d_printf("Change: %s\n", mode_str);
4227 /****************************************************************************
4229 ****************************************************************************/
4231 static int cmd_chown(void)
4233 TALLOC_CTX *ctx = talloc_tos();
4237 char *buf, *buf2, *buf3;
4238 struct cli_state *targetcli;
4239 char *targetname = NULL;
4240 struct cli_credentials *creds = samba_cmdline_get_creds();
4243 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4244 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
4245 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
4246 d_printf("chown uid gid file\n");
4250 uid = (uid_t)atoi(buf);
4251 gid = (gid_t)atoi(buf2);
4253 src = talloc_asprintf(ctx,
4255 client_get_cur_dir(),
4260 src = client_clean_name(ctx, src);
4264 status = cli_resolve_path(ctx, "",
4266 cli, src, &targetcli, &targetname);
4267 if (!NT_STATUS_IS_OK(status)) {
4268 d_printf("chown %s: %s\n", src, nt_errstr(status));
4272 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4273 d_printf("Server doesn't support UNIX CIFS calls.\n");
4277 if (CLI_DIRSEP_CHAR != '/') {
4278 d_printf("Command \"posix\" must be issued before "
4279 "the \"chown\" command can be used.\n");
4283 status = cli_posix_chown(targetcli, targetname, uid, gid);
4284 if (!NT_STATUS_IS_OK(status)) {
4285 d_printf("%s chown file %s uid=%d, gid=%d\n",
4286 nt_errstr(status), src, (int)uid, (int)gid);
4293 /****************************************************************************
4295 ****************************************************************************/
4297 static int cmd_rename(void)
4299 TALLOC_CTX *ctx = talloc_tos();
4302 struct cli_state *targetcli;
4305 struct cli_credentials *creds = samba_cmdline_get_creds();
4307 bool replace = false;
4309 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4310 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4311 d_printf("rename <src> <dest> [-f]\n");
4315 src = talloc_asprintf(ctx,
4317 client_get_cur_dir(),
4322 src = client_clean_name(ctx, src);
4327 dest = talloc_asprintf(ctx,
4329 client_get_cur_dir(),
4334 dest = client_clean_name(ctx, dest);
4339 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
4340 strcsequal(buf, "-f")) {
4344 status = cli_resolve_path(ctx, "",
4346 cli, src, &targetcli, &targetsrc);
4347 if (!NT_STATUS_IS_OK(status)) {
4348 d_printf("rename %s: %s\n", src, nt_errstr(status));
4352 status = cli_resolve_path(ctx, "",
4354 cli, dest, &targetcli, &targetdest);
4355 if (!NT_STATUS_IS_OK(status)) {
4356 d_printf("rename %s: %s\n", dest, nt_errstr(status));
4360 status = cli_rename(targetcli, targetsrc, targetdest, replace);
4361 if (!NT_STATUS_IS_OK(status)) {
4362 d_printf("%s renaming files %s -> %s \n",
4372 struct scopy_timing {
4373 struct timespec tp_start;
4376 static int scopy_status(off_t written, void *priv)
4378 struct timespec tp_end;
4379 unsigned int scopy_total_time_ms;
4380 struct scopy_timing *st = priv;
4382 clock_gettime_mono(&tp_end);
4383 scopy_total_time_ms = nsec_time_diff(&tp_end,&st->tp_start)/1000000;
4385 DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
4386 (intmax_t)written, written / (1.024*scopy_total_time_ms)));
4391 /****************************************************************************
4392 Server-Side copy some file.
4393 ****************************************************************************/
4395 static int cmd_scopy(void)
4397 TALLOC_CTX *ctx = talloc_tos();
4400 struct cli_state *targetcli;
4403 uint32_t DesiredAccess, ShareAccess, CreateDisposition, CreateOptions;
4404 struct smb_create_returns cr;
4405 uint16_t destfnum = (uint16_t)-1;
4406 uint16_t srcfnum = (uint16_t)-1;
4408 struct scopy_timing st;
4410 struct cli_credentials *creds = samba_cmdline_get_creds();
4413 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4414 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4415 d_printf("scopy <src> <dest>\n");
4419 src = talloc_asprintf(ctx,
4421 client_get_cur_dir(),
4426 src = client_clean_name(ctx, src);
4431 dest = talloc_asprintf(ctx,
4433 client_get_cur_dir(),
4438 dest = client_clean_name(ctx, dest);
4443 status = cli_resolve_path(ctx, "",
4445 cli, src, &targetcli, &targetsrc);
4446 if (!NT_STATUS_IS_OK(status)) {
4447 d_printf("scopy %s: %s\n", src, nt_errstr(status));
4451 status = cli_resolve_path(ctx, "",
4453 cli, dest, &targetcli, &targetdest);
4454 if (!NT_STATUS_IS_OK(status)) {
4455 d_printf("scopy %s: %s\n", dest, nt_errstr(status));
4460 DesiredAccess = (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES|
4461 READ_CONTROL_ACCESS|SYNCHRONIZE_ACCESS);
4462 ShareAccess = FILE_SHARE_READ|FILE_SHARE_DELETE;
4463 CreateDisposition = FILE_OPEN;
4464 CreateOptions = (FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE|
4465 FILE_OPEN_REPARSE_POINT);
4466 status = cli_ntcreate(targetcli, targetsrc, 0, DesiredAccess, 0,
4467 ShareAccess, CreateDisposition, CreateOptions, 0x0,
4469 if (!NT_STATUS_IS_OK(status)) {
4470 d_printf("Failed to open file %s. %s\n",
4471 targetsrc, nt_errstr(status));
4475 DesiredAccess = (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_READ_EA|
4476 FILE_WRITE_EA|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|
4477 DELETE_ACCESS|READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|SYNCHRONIZE_ACCESS);
4478 ShareAccess = FILE_SHARE_NONE;
4479 CreateDisposition = FILE_CREATE;
4480 CreateOptions = FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE;
4481 status = cli_ntcreate(targetcli, targetdest, 0, DesiredAccess,
4482 FILE_ATTRIBUTE_ARCHIVE, ShareAccess, CreateDisposition,
4483 CreateOptions, 0x0, &destfnum, NULL);
4484 if (!NT_STATUS_IS_OK(status)) {
4485 d_printf("Failed to create file %s. %s\n",
4486 targetdest, nt_errstr(status));
4487 cli_close(targetcli, srcfnum);
4491 clock_gettime_mono(&st.tp_start);
4492 status = cli_splice(targetcli, targetcli, srcfnum, destfnum,
4493 cr.end_of_file, 0, 0, &written, scopy_status, &st);
4494 if (!NT_STATUS_IS_OK(status)) {
4495 d_printf("%s copying file %s -> %s \n",
4502 status = cli_close(targetcli, srcfnum);
4503 if (!NT_STATUS_IS_OK(status)) {
4504 d_printf("Error %s closing remote source file\n", nt_errstr(status));
4507 status = cli_close(targetcli, destfnum);
4508 if (!NT_STATUS_IS_OK(status)) {
4509 d_printf("Error %s closing remote dest file\n", nt_errstr(status));
4516 /****************************************************************************
4517 Print the volume name.
4518 ****************************************************************************/
4520 static int cmd_volume(void)
4523 uint32_t serial_num;
4527 status = cli_get_fs_volume_info(cli, talloc_tos(),
4528 &volname, &serial_num,
4530 if (!NT_STATUS_IS_OK(status)) {
4531 d_printf("Error %s getting volume info\n", nt_errstr(status));
4535 d_printf("Volume: |%s| serial number 0x%x\n",
4536 volname, (unsigned int)serial_num);
4540 /****************************************************************************
4541 Hard link files using the NT call.
4542 ****************************************************************************/
4544 static int cmd_hardlink(void)
4546 TALLOC_CTX *ctx = talloc_tos();
4549 struct cli_state *targetcli;
4551 struct cli_credentials *creds = samba_cmdline_get_creds();
4554 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4555 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4556 d_printf("hardlink <src> <dest>\n");
4560 src = talloc_asprintf(ctx,
4562 client_get_cur_dir(),
4567 src = client_clean_name(ctx, src);
4572 dest = talloc_asprintf(ctx,
4574 client_get_cur_dir(),
4579 dest = client_clean_name(ctx, dest);
4584 status = cli_resolve_path(ctx, "",
4586 cli, src, &targetcli, &targetname);
4587 if (!NT_STATUS_IS_OK(status)) {
4588 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
4592 status = cli_hardlink(targetcli, targetname, dest);
4593 if (!NT_STATUS_IS_OK(status)) {
4594 d_printf("%s doing an NT hard link of files\n",
4602 /****************************************************************************
4603 Toggle the prompt flag.
4604 ****************************************************************************/
4606 static int cmd_prompt(void)
4609 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
4613 /****************************************************************************
4614 Set the newer than time.
4615 ****************************************************************************/
4617 static int cmd_newer(void)
4619 TALLOC_CTX *ctx = talloc_tos();
4622 SMB_STRUCT_STAT sbuf;
4624 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
4625 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
4626 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4627 DEBUG(1,("Getting files newer than %s",
4628 time_to_asc(newer_than)));
4633 if (ok && newer_than == 0) {
4634 d_printf("Error setting newer-than time\n");
4641 /****************************************************************************
4642 Watch directory changes
4643 ****************************************************************************/
4645 static int cmd_notify(void)
4647 TALLOC_CTX *frame = talloc_stackframe();
4652 name = talloc_strdup(talloc_tos(), client_get_cur_dir());
4656 if (!next_token_talloc(talloc_tos(), &cmd_ptr, &buf, NULL)) {
4659 name = talloc_asprintf_append(name, "%s", buf);
4663 name = client_clean_name(talloc_tos(), name);
4667 status = cli_ntcreate(
4668 cli, name, 0, FILE_READ_DATA, 0,
4669 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4670 FILE_OPEN, 0, 0, &fnum, NULL);
4671 if (!NT_STATUS_IS_OK(status)) {
4672 d_printf("Could not open file: %s\n", nt_errstr(status));
4678 uint32_t num_changes = 0;
4679 struct notify_change *changes = NULL;
4681 status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
4683 talloc_tos(), &num_changes, &changes);
4684 if (NT_STATUS_EQUAL(status, NT_STATUS_NOTIFY_ENUM_DIR)) {
4685 printf("NOTIFY_ENUM_DIR\n");
4686 status = NT_STATUS_OK;
4688 if (!NT_STATUS_IS_OK(status)) {
4689 d_printf("notify returned %s\n",
4693 for (i=0; i<num_changes; i++) {
4694 printf("%4.4x %s\n", changes[i].action,
4697 TALLOC_FREE(changes);
4700 d_printf("notify <dir name>\n");
4706 /****************************************************************************
4707 Set the archive level.
4708 ****************************************************************************/
4710 static int cmd_archive(void)
4712 TALLOC_CTX *ctx = talloc_tos();
4715 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4716 archive_level = atoi(buf);
4718 d_printf("Archive level is %d\n",archive_level);
4724 /****************************************************************************
4725 Toggle the backup_intent state.
4726 ****************************************************************************/
4728 static int cmd_backup(void)
4730 backup_intent = !backup_intent;
4731 cli_set_backup_intent(cli, backup_intent);
4732 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4736 /****************************************************************************
4737 Toggle the lowercaseflag.
4738 ****************************************************************************/
4740 static int cmd_lowercase(void)
4742 lowercase = !lowercase;
4743 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4747 /****************************************************************************
4748 Toggle the case sensitive flag.
4749 ****************************************************************************/
4751 static int cmd_setcase(void)
4753 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4755 cli_set_case_sensitive(cli, !orig_case_sensitive);
4756 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4761 /****************************************************************************
4762 Toggle the showacls flag.
4763 ****************************************************************************/
4765 static int cmd_showacls(void)
4767 showacls = !showacls;
4768 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4773 /****************************************************************************
4774 Toggle the recurse flag.
4775 ****************************************************************************/
4777 static int cmd_recurse(void)
4780 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4784 /****************************************************************************
4785 Toggle the translate flag.
4786 ****************************************************************************/
4788 static int cmd_translate(void)
4790 translation = !translation;
4791 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4792 translation?"on":"off"));
4796 /****************************************************************************
4798 ****************************************************************************/
4800 static int cmd_lcd(void)
4802 TALLOC_CTX *ctx = talloc_tos();
4806 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4807 if (chdir(buf) == -1) {
4808 d_printf("chdir to %s failed (%s)\n",
4809 buf, strerror(errno));
4816 DEBUG(2,("the local directory is now %s\n",d));
4821 /****************************************************************************
4822 Get a file restarting at end of local file.
4823 ****************************************************************************/
4825 static int cmd_reget(void)
4827 TALLOC_CTX *ctx = talloc_tos();
4828 char *local_name = NULL;
4829 char *remote_name = NULL;
4833 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4838 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4839 d_printf("reget <filename>\n");
4842 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4846 remote_name = client_clean_name(ctx,remote_name);
4852 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4857 return do_get(remote_name, local_name, true);
4860 /****************************************************************************
4861 Put a file restarting at end of local file.
4862 ****************************************************************************/
4864 static int cmd_reput(void)
4866 TALLOC_CTX *ctx = talloc_tos();
4867 char *local_name = NULL;
4868 char *remote_name = NULL;
4872 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4877 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4878 d_printf("reput <filename>\n");
4882 if (!file_exist_stat(local_name, &st, false)) {
4883 d_printf("%s does not exist\n", local_name);
4887 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4888 remote_name = talloc_asprintf_append(remote_name,
4891 remote_name = talloc_asprintf_append(remote_name,
4898 remote_name = client_clean_name(ctx, remote_name);
4903 return do_put(remote_name, local_name, true);
4906 /****************************************************************************
4908 ****************************************************************************/
4910 static void browse_fn(const char *name, uint32_t m,
4911 const char *comment, void *state)
4913 const char *typestr = "";
4916 case STYPE_DISKTREE:
4920 typestr = "Printer";
4929 /* FIXME: If the remote machine returns non-ascii characters
4930 in any of these fields, they can corrupt the output. We
4931 should remove them. */
4933 d_printf("\t%-15s %-10.10s%s\n",
4934 name,typestr,comment);
4936 d_printf ("%s|%s|%s\n",typestr,name,comment);
4940 static bool browse_host_rpc(bool sort)
4943 struct rpc_pipe_client *pipe_hnd = NULL;
4944 TALLOC_CTX *frame = talloc_stackframe();
4946 struct srvsvc_NetShareInfoCtr info_ctr;
4947 struct srvsvc_NetShareCtr1 ctr1;
4948 uint32_t resume_handle = 0;
4949 uint32_t total_entries = 0;
4951 struct dcerpc_binding_handle *b;
4953 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
4956 if (!NT_STATUS_IS_OK(status)) {
4957 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4958 nt_errstr(status)));
4963 b = pipe_hnd->binding_handle;
4965 ZERO_STRUCT(info_ctr);
4969 info_ctr.ctr.ctr1 = &ctr1;
4971 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4979 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4980 TALLOC_FREE(pipe_hnd);
4985 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4986 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4987 browse_fn(info.name, info.type, info.comment, NULL);
4990 TALLOC_FREE(pipe_hnd);
4995 /****************************************************************************
4996 Try and browse available connections on a host.
4997 ****************************************************************************/
4999 static bool browse_host(bool sort)
5004 d_printf("\n\tSharename Type Comment\n");
5005 d_printf("\t--------- ---- -------\n");
5008 if (browse_host_rpc(sort)) {
5012 if (smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1) {
5016 ret = cli_RNetShareEnum(cli, browse_fn, NULL);
5018 NTSTATUS status = cli_nt_error(cli);
5019 d_printf("Error returning browse list: %s\n",
5026 /****************************************************************************
5028 ****************************************************************************/
5030 static void server_fn(const char *name, uint32_t m,
5031 const char *comment, void *state)
5035 d_printf("\t%-16s %s\n", name, comment);
5037 d_printf("%s|%s|%s\n",(char *)state, name, comment);
5041 /****************************************************************************
5042 Try and browse available connections on a host.
5043 ****************************************************************************/
5045 static bool list_servers(const char *wk_grp)
5049 if (!cli->server_domain)
5053 d_printf("\n\tServer Comment\n");
5054 d_printf("\t--------- -------\n");
5056 fstrcpy( state, "Server" );
5057 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
5061 d_printf("\n\tWorkgroup Master\n");
5062 d_printf("\t--------- -------\n");
5065 fstrcpy( state, "Workgroup" );
5066 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
5071 /****************************************************************************
5072 Print or set current VUID
5073 ****************************************************************************/
5075 static int cmd_vuid(void)
5077 TALLOC_CTX *ctx = talloc_tos();
5080 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5081 d_printf("Current VUID is %d\n",
5082 cli_state_get_uid(cli));
5086 cli_state_set_uid(cli, atoi(buf));
5090 /****************************************************************************
5091 Setup a new VUID, by issuing a session setup
5092 ****************************************************************************/
5094 static int cmd_logon(void)
5096 TALLOC_CTX *ctx = talloc_tos();
5097 char *l_username, *l_password;
5098 struct cli_credentials *creds = NULL;
5101 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
5102 d_printf("logon <username> [<password>]\n");
5106 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
5107 char pwd[256] = {0};
5110 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
5112 l_password = talloc_strdup(ctx, pwd);
5119 creds = cli_session_creds_init(ctx,
5124 false, /* use_kerberos */
5125 false, /* fallback_after_kerberos */
5126 false, /* use_ccache */
5127 false); /* password_is_nt_hash */
5128 if (creds == NULL) {
5129 d_printf("cli_session_creds_init() failed.\n");
5132 nt_status = cli_session_setup_creds(cli, creds);
5134 if (!NT_STATUS_IS_OK(nt_status)) {
5135 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
5139 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
5147 static int cmd_logoff(void)
5151 status = cli_ulogoff(cli);
5152 if (!NT_STATUS_IS_OK(status)) {
5153 d_printf("logoff failed: %s\n", nt_errstr(status));
5157 d_printf("logoff successful\n");
5163 * tree connect (connect to a share)
5166 static int cmd_tcon(void)
5168 TALLOC_CTX *ctx = talloc_tos();
5172 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
5173 d_printf("tcon <sharename>\n");
5181 status = cli_tree_connect(cli, sharename, "?????", NULL);
5182 if (!NT_STATUS_IS_OK(status)) {
5183 d_printf("tcon failed: %s\n", nt_errstr(status));
5187 d_printf("tcon to %s successful, tid: %u\n", sharename,
5188 cli_state_get_tid(cli));
5190 talloc_free(sharename);
5196 * tree disconnect (disconnect from a share)
5199 static int cmd_tdis(void)
5203 status = cli_tdis(cli);
5204 if (!NT_STATUS_IS_OK(status)) {
5205 d_printf("tdis failed: %s\n", nt_errstr(status));
5209 d_printf("tdis successful\n");
5218 static int cmd_tid(void)
5220 TALLOC_CTX *ctx = talloc_tos();
5223 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
5224 if (cli_state_has_tcon(cli)) {
5225 d_printf("current tid is %d\n", cli_state_get_tid(cli));
5227 d_printf("no tcon currently\n");
5230 uint32_t tid = atoi(tid_str);
5231 if (!cli_state_has_tcon(cli)) {
5232 d_printf("no tcon currently\n");
5234 cli_state_set_tid(cli, tid);
5241 /****************************************************************************
5242 list active connections
5243 ****************************************************************************/
5245 static int cmd_list_connect(void)
5247 cli_cm_display(cli);
5251 /****************************************************************************
5252 display the current active client connection
5253 ****************************************************************************/
5255 static int cmd_show_connect( void )
5257 TALLOC_CTX *ctx = talloc_tos();
5258 struct cli_state *targetcli;
5260 struct cli_credentials *creds = samba_cmdline_get_creds();
5263 status = cli_resolve_path(ctx, "",
5266 client_get_cur_dir(), &targetcli,
5268 if (!NT_STATUS_IS_OK(status)) {
5269 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
5273 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli->conn), targetcli->share);
5278 * cmd_utimes - interactive command to set the four times
5280 * Read a filename and four times from the client command line and update
5281 * the file times. A value of -1 for a time means don't change.
5283 static int cmd_utimes(void)
5287 struct timespec times[4] = {{0}};
5288 struct timeval_buf tbuf[4];
5292 TALLOC_CTX *ctx = talloc_new(NULL);
5299 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5301 d_printf("utimes <filename> <create-time> <access-time> "
5302 "<write-time> <change-time>\n");
5303 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5304 "or -1 for no change\n");
5309 fname = talloc_asprintf(ctx,
5311 client_get_cur_dir(),
5313 if (fname == NULL) {
5317 fname = client_clean_name(ctx, fname);
5318 if (fname == NULL) {
5323 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
5325 const char *s = buf;
5326 struct tm tm = {0,};
5330 if (strlen(s) == 2 && strcmp(s, "-1") == 0) {
5331 times[time_count] = make_omit_timespec();
5336 ret = strptime(s, "%y:%m:%d-%H:%M:%S", &tm);
5339 ret = strptime(s, "%Y:%m:%d-%H:%M:%S", &tm);
5342 /* We could not match all the chars, so print error */
5343 if (ret == NULL || *ret != 0) {
5344 d_printf("Invalid date format: %s\n", s);
5345 d_printf("utimes <filename> <create-time> "
5346 "<access-time> <write-time> <change-time>\n");
5347 d_printf("Dates should be in [YY]YY:MM:DD-HH:MM:SS "
5348 "format or -1 for no change\n");
5353 /* Convert tm to a time_t */
5355 times[time_count] = (struct timespec){.tv_sec = t};
5359 if (time_count < 4) {
5360 d_printf("Insufficient dates: %d\n", time_count);
5361 d_printf("utimes <filename> <create-time> <access-time> "
5362 "<write-time> <change-time>\n");
5363 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5364 "or -1 for no change\n");
5369 DEBUG(10, ("times\nCreate: %sAccess: %s Write: %sChange: %s\n",
5370 timespec_string_buf(×[0], false, &tbuf[0]),
5371 timespec_string_buf(×[1], false, &tbuf[1]),
5372 timespec_string_buf(×[2], false, &tbuf[2]),
5373 timespec_string_buf(×[3], false, &tbuf[3])));
5375 status = cli_setpathinfo_ext(
5376 cli, fname, times[0], times[1], times[2], times[3],
5378 if (!NT_STATUS_IS_OK(status)) {
5379 d_printf("cli_setpathinfo_ext failed: %s\n",
5390 * set_remote_attr - set DOS attributes of a remote file
5391 * @filename: path to the file name
5392 * @new_attr: attribute bit mask to use
5393 * @mode: one of ATTR_SET or ATTR_UNSET
5395 * Update the file attributes with the one provided.
5397 int set_remote_attr(const char *filename, uint32_t new_attr, int mode)
5399 extern struct cli_state *cli;
5403 status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
5404 if (!NT_STATUS_IS_OK(status)) {
5405 d_printf("cli_getatr failed: %s\n", nt_errstr(status));
5409 if (mode == ATTR_SET) {
5410 new_attr |= old_attr;
5412 new_attr = old_attr & ~new_attr;
5415 status = cli_setatr(cli, filename, new_attr, 0);
5416 if (!NT_STATUS_IS_OK(status)) {
5417 d_printf("cli_setatr failed: %s\n", nt_errstr(status));
5425 * cmd_setmode - interactive command to set DOS attributes
5427 * Read a filename and mode from the client command line and update
5428 * the file DOS attributes.
5430 int cmd_setmode(void)
5434 uint32_t attr[2] = {0};
5435 int mode = ATTR_SET;
5438 TALLOC_CTX *ctx = talloc_new(NULL);
5443 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5445 d_printf("setmode <filename> <[+|-]rsha>\n");
5450 fname = talloc_asprintf(ctx,
5452 client_get_cur_dir(),
5454 if (fname == NULL) {
5458 fname = client_clean_name(ctx, fname);
5459 if (fname == NULL) {
5464 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
5465 const char *s = buf;
5476 attr[mode] |= FILE_ATTRIBUTE_READONLY;
5479 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
5482 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
5485 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
5488 d_printf("setmode <filename> <perm=[+|-]rsha>\n");
5495 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
5496 d_printf("setmode <filename> <[+|-]rsha>\n");
5501 DEBUG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
5503 /* ignore return value: server might not store DOS attributes */
5504 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
5505 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
5511 /****************************************************************************
5513 ***************************************************************************/
5515 int cmd_iosize(void)
5517 TALLOC_CTX *ctx = talloc_tos();
5520 struct cli_credentials *creds = samba_cmdline_get_creds();
5522 (cli_credentials_get_smb_encryption(creds) ==
5523 SMB_ENCRYPTION_REQUIRED);
5525 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5526 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5528 d_printf("iosize <n> or iosize 0x<n>. "
5529 "Minimum is 0 (default), "
5530 "max is 16776960 (0xFFFF00)\n");
5532 d_printf("iosize <n> or iosize 0x<n>. "
5533 "(Encrypted connection) ,"
5534 "Minimum is 0 (default), "
5535 "max is 130048 (0x1FC00)\n");
5538 d_printf("iosize <n> or iosize 0x<n>.\n");
5543 iosize = strtol(buf,NULL,0);
5544 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5545 if (smb_encrypt && (iosize < 0 || iosize > 0xFC00)) {
5546 d_printf("iosize out of range for encrypted "
5547 "connection (min = 0 (default), "
5548 "max = 130048 (0x1FC00)\n");
5550 } else if (!smb_encrypt && (iosize < 0 || iosize > 0xFFFF00)) {
5551 d_printf("iosize out of range (min = 0 (default), "
5552 "max = 16776960 (0xFFFF00)\n");
5557 io_bufsize = iosize;
5558 d_printf("iosize is now %d\n", io_bufsize);
5562 /****************************************************************************
5564 ***************************************************************************/
5566 static int cmd_timeout(void)
5568 TALLOC_CTX *ctx = talloc_tos();
5571 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5572 unsigned int old_timeout = cli_set_timeout(cli, 0);
5573 cli_set_timeout(cli, old_timeout);
5574 d_printf("timeout <n> (per-operation timeout "
5575 "in seconds - currently %u).\n",
5580 io_timeout = strtol(buf,NULL,0);
5581 cli_set_timeout(cli, io_timeout*1000);
5582 d_printf("io_timeout per operation is now %d\n", io_timeout);
5587 /****************************************************************************
5589 ****************************************************************************/
5590 static int cmd_history(void)
5592 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
5596 hlist = history_list();
5598 for (i = 0; hlist && hlist[i]; i++) {
5599 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
5602 DEBUG(0,("no history without readline support\n"));
5608 /* Some constants for completing filename arguments */
5610 #define COMPL_NONE 0 /* No completions */
5611 #define COMPL_REMOTE 1 /* Complete remote filename */
5612 #define COMPL_LOCAL 2 /* Complete local filename */
5614 /* This defines the commands supported by this client.
5615 * NOTE: The "!" must be the last one in the list because it's fn pointer
5616 * field is NULL, and NULL in that field is used in process_tok()
5617 * (below) to indicate the end of the list. crh
5622 const char *description;
5623 char compl_args[2]; /* Completion argument info */
5625 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5626 {"allinfo",cmd_allinfo,"<file> show all available info",
5627 {COMPL_REMOTE,COMPL_NONE}},
5628 {"altname",cmd_altname,"<file> show alt name",{COMPL_REMOTE,COMPL_NONE}},
5629 {"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}},
5630 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
5631 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
5632 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
5633 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
5634 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
5635 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_NONE}},
5636 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_NONE}},
5637 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_NONE}},
5638 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5639 {"deltree",cmd_deltree,"<mask> recursively delete all matching files and directories",{COMPL_REMOTE,COMPL_NONE}},
5640 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5641 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5642 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
5643 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5644 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
5645 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_NONE}},
5646 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
5647 {COMPL_REMOTE, COMPL_NONE}},
5648 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5649 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5650 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
5651 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
5652 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
5653 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5654 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5655 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
5656 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5657 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5658 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
5659 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5660 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
5661 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5662 {"mkfifo",cmd_mkfifo,"<file mode> make a fifo",{COMPL_NONE,COMPL_NONE}},
5663 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
5664 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
5665 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
5666 {"notify",cmd_notify,"<file>Get notified of dir changes",{COMPL_REMOTE,COMPL_NONE}},
5667 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
5668 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
5669 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
5670 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5671 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5672 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5673 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5674 {"posix_whoami",cmd_posix_whoami,"return logged on user information "
5675 "using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5676 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
5677 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
5678 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
5679 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
5680 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5681 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
5682 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5683 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5684 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
5685 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
5686 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
5687 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
5688 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
5689 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5690 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_REMOTE,COMPL_NONE}},
5691 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
5692 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
5693 {COMPL_REMOTE, COMPL_LOCAL}},
5694 {"setmode",cmd_setmode,"<file name> <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
5695 {"scopy",cmd_scopy,"<src> <dest> server-side copy file",{COMPL_REMOTE,COMPL_REMOTE}},
5696 {"stat",cmd_stat,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_NONE}},
5697 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5698 {"tar",cmd_tar,"tar <c|x>[IXFvbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
5699 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
5700 {"timeout",cmd_timeout,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE,COMPL_NONE}},
5701 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
5702 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5703 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
5704 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
5705 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5706 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
5707 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
5708 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
5709 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
5710 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
5711 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
5712 {"utimes", cmd_utimes,"<file name> <create_time> <access_time> <mod_time> "
5713 "<ctime> set times", {COMPL_REMOTE,COMPL_NONE}},
5714 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
5715 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
5717 /* Yes, this must be here, see crh's comment above. */
5718 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
5719 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
5722 /*******************************************************************
5723 Lookup a command string in the list of commands, including
5725 ******************************************************************/
5727 static int process_tok(char *tok)
5729 size_t i = 0, matches = 0;
5731 size_t tok_len = strlen(tok);
5733 while (commands[i].fn != NULL) {
5734 if (strequal(commands[i].name,tok)) {
5738 } else if (strnequal(commands[i].name, tok, tok_len)) {
5747 else if (matches == 1)
5753 /****************************************************************************
5755 ****************************************************************************/
5757 static int cmd_help(void)
5759 TALLOC_CTX *ctx = talloc_tos();
5763 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5764 if ((i = process_tok(buf)) >= 0)
5765 d_printf("HELP %s:\n\t%s\n\n",
5766 commands[i].name,commands[i].description);
5768 while (commands[i].description) {
5769 for (j=0; commands[i].description && (j<5); j++) {
5770 d_printf("%-15s",commands[i].name);
5779 /****************************************************************************
5780 Process a -c command string.
5781 ****************************************************************************/
5783 static int process_command_string(const char *cmd_in)
5785 TALLOC_CTX *ctx = talloc_tos();
5786 char *cmd = talloc_strdup(ctx, cmd_in);
5788 struct cli_credentials *creds = samba_cmdline_get_creds();
5793 /* establish the connection if not already */
5798 status = cli_cm_open(talloc_tos(), NULL,
5802 have_ip ? &dest_ss : NULL, port,
5805 if (!NT_STATUS_IS_OK(status)) {
5808 cli_set_timeout(cli, io_timeout*1000);
5811 while (cmd[0] != '\0') {
5817 if ((p = strchr_m(cmd, ';')) == 0) {
5826 /* and get the first part of the command */
5828 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
5832 if ((i = process_tok(tok)) >= 0) {
5833 rc = commands[i].fn();
5834 } else if (i == -2) {
5835 d_printf("%s: command abbreviation ambiguous\n",tok);
5837 d_printf("%s: command not found\n",tok);
5844 #define MAX_COMPLETIONS 100
5846 struct completion_remote {
5854 static NTSTATUS completion_remote_filter(struct file_info *f,
5858 struct completion_remote *info = (struct completion_remote *)state;
5860 if (info->count >= MAX_COMPLETIONS - 1) {
5861 return NT_STATUS_OK;
5863 if (strncmp(info->text, f->name, info->len) != 0) {
5864 return NT_STATUS_OK;
5866 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
5867 return NT_STATUS_OK;
5870 if ((info->dirmask[0] == 0) && !(f->attr & FILE_ATTRIBUTE_DIRECTORY))
5871 info->matches[info->count] = SMB_STRDUP(f->name);
5873 TALLOC_CTX *ctx = talloc_stackframe();
5876 tmp = talloc_strdup(ctx,info->dirmask);
5879 return NT_STATUS_NO_MEMORY;
5881 tmp = talloc_asprintf_append(tmp, "%s", f->name);
5884 return NT_STATUS_NO_MEMORY;
5886 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5887 tmp = talloc_asprintf_append(tmp, "%s",
5892 return NT_STATUS_NO_MEMORY;
5894 info->matches[info->count] = SMB_STRDUP(tmp);
5897 if (info->matches[info->count] == NULL) {
5898 return NT_STATUS_OK;
5900 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5901 smb_readline_ca_char(0);
5903 if (info->count == 1) {
5904 info->samelen = strlen(info->matches[info->count]);
5906 while (strncmp(info->matches[info->count],
5907 info->matches[info->count-1],
5908 info->samelen) != 0) {
5913 return NT_STATUS_OK;
5916 static char **remote_completion(const char *text, int len)
5918 TALLOC_CTX *ctx = talloc_stackframe();
5919 char *dirmask = NULL;
5920 char *targetpath = NULL;
5921 struct cli_state *targetcli = NULL;
5923 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
5924 struct cli_credentials *creds = samba_cmdline_get_creds();
5927 /* can't have non-static initialisation on Sun CC, so do it
5933 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
5934 if (!info.matches) {
5940 * We're leaving matches[0] free to fill it later with the text to
5941 * display: Either the one single match or the longest common subset
5944 info.matches[0] = NULL;
5947 for (i = len-1; i >= 0; i--) {
5948 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
5953 info.text = text+i+1;
5954 info.samelen = info.len = len-i-1;
5957 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
5958 if (!info.dirmask) {
5961 strncpy(info.dirmask, text, i+1);
5962 info.dirmask[i+1] = 0;
5963 dirmask = talloc_asprintf(ctx,
5965 client_get_cur_dir(),
5969 info.dirmask = SMB_STRDUP("");
5970 if (!info.dirmask) {
5973 dirmask = talloc_asprintf(ctx,
5975 client_get_cur_dir());
5980 dirmask = client_clean_name(ctx, dirmask);
5981 if (dirmask == NULL) {
5985 status = cli_resolve_path(ctx, "",
5987 cli, dirmask, &targetcli, &targetpath);
5988 if (!NT_STATUS_IS_OK(status)) {
5991 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
5992 completion_remote_filter, (void *)&info);
5993 if (!NT_STATUS_IS_OK(status)) {
5997 if (info.count == 1) {
5999 * No matches at all, NULL indicates there is nothing
6001 SAFE_FREE(info.matches[0]);
6002 SAFE_FREE(info.matches);
6007 if (info.count == 2) {
6009 * Exactly one match in matches[1], indicate this is the one
6012 info.matches[0] = info.matches[1];
6013 info.matches[1] = NULL;
6016 return info.matches;
6020 * We got more than one possible match, set the result to the maximum
6024 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
6025 info.matches[info.count] = NULL;
6027 return info.matches;
6030 for (i = 0; i < info.count; i++) {
6031 SAFE_FREE(info.matches[i]);
6033 SAFE_FREE(info.matches);
6034 SAFE_FREE(info.dirmask);
6039 static char **completion_fn(const char *text, int start, int end)
6041 smb_readline_ca_char(' ');
6044 const char *buf, *sp;
6048 buf = smb_readline_get_line_buffer();
6052 sp = strchr(buf, ' ');
6056 for (i = 0; commands[i].name; i++) {
6057 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
6058 (commands[i].name[sp - buf] == 0)) {
6062 if (commands[i].name == NULL)
6068 if (sp == (buf + start))
6069 compl_type = commands[i].compl_args[0];
6071 compl_type = commands[i].compl_args[1];
6073 if (compl_type == COMPL_REMOTE)
6074 return remote_completion(text, end - start);
6075 else /* fall back to local filename completion */
6079 size_t i, len, samelen = 0, count=1;
6081 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
6088 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
6089 if (strncmp(text, commands[i].name, len) == 0) {
6090 matches[count] = SMB_STRDUP(commands[i].name);
6091 if (!matches[count])
6094 samelen = strlen(matches[count]);
6096 while (strncmp(matches[count], matches[count-1], samelen) != 0)
6103 case 0: /* should never happen */
6107 matches[0] = SMB_STRDUP(matches[1]);
6110 matches[0] = (char *)SMB_MALLOC(samelen+1);
6113 strncpy(matches[0], matches[1], samelen);
6114 matches[0][samelen] = 0;
6116 matches[count] = NULL;
6120 for (i = 0; i < count; i++)
6128 static bool finished;
6130 /****************************************************************************
6131 Make sure we swallow keepalives during idle time.
6132 ****************************************************************************/
6134 static void readline_callback(void)
6136 static time_t last_t;
6137 struct timespec now;
6140 unsigned char garbage[16];
6142 clock_gettime_mono(&now);
6150 /* Ping the server to keep the connection alive using SMBecho. */
6151 memset(garbage, 0xf0, sizeof(garbage));
6152 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
6153 if (NT_STATUS_IS_OK(status) ||
6154 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
6156 * Even if server returns NT_STATUS_INVALID_PARAMETER
6157 * it still responded.
6158 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
6163 if (!cli_state_is_connected(cli)) {
6164 DEBUG(0,("SMBecho failed (%s). The connection is "
6165 "disconnected now\n", nt_errstr(status)));
6167 smb_readline_done();
6171 /****************************************************************************
6172 Process commands on stdin.
6173 ****************************************************************************/
6175 static int process_stdin(void)
6180 d_printf("Try \"help\" to get a list of possible commands.\n");
6184 TALLOC_CTX *frame = talloc_stackframe();
6186 char *the_prompt = NULL;
6190 /* display a prompt */
6191 the_prompt = talloc_asprintf(frame,
6193 client_get_cur_dir());
6194 if (the_prompt == NULL) {
6198 line = smb_readline(the_prompt, readline_callback, completion_fn);
6204 /* special case - first char is ! */
6206 if (system(line + 1) == -1) {
6207 d_printf("system() command %s failed.\n",
6215 /* and get the first part of the command */
6217 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
6223 if ((i = process_tok(tok)) >= 0) {
6224 rc = commands[i].fn();
6225 } else if (i == -2) {
6226 d_printf("%s: command abbreviation ambiguous\n",tok);
6228 d_printf("%s: command not found\n",tok);
6236 /****************************************************************************
6237 Process commands from the client.
6238 ****************************************************************************/
6240 static int process(const char *base_directory)
6244 struct cli_credentials *creds = samba_cmdline_get_creds();
6246 status = cli_cm_open(talloc_tos(), NULL,
6250 have_ip ? &dest_ss : NULL, port,
6252 if (!NT_STATUS_IS_OK(status)) {
6256 cli_set_timeout(cli, io_timeout*1000);
6258 if (base_directory && *base_directory) {
6259 rc = do_cd(base_directory);
6267 rc = process_command_string(cmdstr);
6276 /****************************************************************************
6278 ****************************************************************************/
6280 static int do_host_query(struct loadparm_context *lp_ctx,
6281 const char *query_host)
6284 struct cli_credentials *creds = samba_cmdline_get_creds();
6286 status = cli_cm_open(talloc_tos(), NULL,
6290 have_ip ? &dest_ss : NULL, port,
6292 if (!NT_STATUS_IS_OK(status)) {
6296 cli_set_timeout(cli, io_timeout*1000);
6299 /* Ensure that the host can do IPv4 */
6301 if (!interpret_addr(query_host)) {
6302 struct sockaddr_storage ss;
6303 if (interpret_string_addr(&ss, query_host, 0) &&
6304 (ss.ss_family != AF_INET)) {
6305 d_printf("%s is an IPv6 address -- no workgroup available\n",
6311 if (lp_client_min_protocol() > PROTOCOL_NT1) {
6312 d_printf("SMB1 disabled -- no workgroup available\n");
6316 if (lp_disable_netbios()) {
6317 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
6321 if (port != NBT_SMB_PORT ||
6322 smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1)
6325 * Workgroups simply don't make sense over anything
6326 * else but port 139 and SMB1.
6330 d_printf("Reconnecting with SMB1 for workgroup listing.\n");
6331 lpcfg_set_cmdline(lp_ctx, "client max protocol", "NT1");
6332 status = cli_cm_open(talloc_tos(), NULL,
6336 have_ip ? &dest_ss : NULL, NBT_SMB_PORT,
6338 if (!NT_STATUS_IS_OK(status)) {
6339 d_printf("Unable to connect with SMB1 "
6340 "-- no workgroup available\n");
6345 cli_set_timeout(cli, io_timeout*1000);
6346 list_servers(lp_workgroup());
6353 /****************************************************************************
6354 Handle a tar operation.
6355 ****************************************************************************/
6357 static int do_tar_op(const char *base_directory)
6359 struct tar *tar_ctx = tar_get_ctx();
6361 struct cli_credentials *creds = samba_cmdline_get_creds();
6363 /* do we already have a connection? */
6367 status = cli_cm_open(talloc_tos(), NULL,
6371 have_ip ? &dest_ss : NULL, port,
6373 if (!NT_STATUS_IS_OK(status)) {
6377 cli_set_timeout(cli, io_timeout*1000);
6382 if (base_directory && *base_directory) {
6383 ret = do_cd(base_directory);
6389 ret = tar_process(tar_ctx);
6397 /****************************************************************************
6398 Handle a message operation.
6399 ****************************************************************************/
6401 static int do_message_op(struct cli_credentials *creds)
6405 if (lp_disable_netbios()) {
6406 d_printf("NetBIOS over TCP disabled.\n");
6410 status = cli_connect_nb(talloc_tos(),
6411 desthost, have_ip ? &dest_ss : NULL,
6412 port ? port : NBT_SMB_PORT, name_type,
6417 if (!NT_STATUS_IS_OK(status)) {
6418 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
6422 cli_set_timeout(cli, io_timeout*1000);
6423 send_message(cli_credentials_get_username(creds));
6429 /****************************************************************************
6431 ****************************************************************************/
6433 int main(int argc,char *argv[])
6435 const char **const_argv = discard_const_p(const char *, argv);
6436 char *base_directory = NULL;
6438 char *query_host = NULL;
6439 bool message = false;
6440 static const char *new_name_resolve_order = NULL;
6444 bool tar_opt = false;
6445 bool service_opt = false;
6446 struct tar *tar_ctx = tar_get_ctx();
6449 struct poptOption long_options[] = {
6453 .longName = "message",
6455 .argInfo = POPT_ARG_STRING,
6458 .descrip = "Send message",
6459 .argDescrip = "HOST",
6462 .longName = "ip-address",
6464 .argInfo = POPT_ARG_STRING,
6467 .descrip = "Use this IP to connect to",
6471 .longName = "stderr",
6473 .argInfo = POPT_ARG_NONE,
6476 .descrip = "Write messages to stderr instead of stdout",
6481 .argInfo = POPT_ARG_STRING,
6484 .descrip = "Get a list of shares available on a host",
6485 .argDescrip = "HOST",
6490 .argInfo = POPT_ARG_STRING,
6493 .descrip = "Command line tar",
6494 .argDescrip = "<c|x>IXFvgbNan",
6497 .longName = "directory",
6499 .argInfo = POPT_ARG_STRING,
6502 .descrip = "Start from directory",
6503 .argDescrip = "DIR",
6506 .longName = "command",
6508 .argInfo = POPT_ARG_STRING,
6511 .descrip = "Execute semicolon separated commands",
6514 .longName = "send-buffer",
6516 .argInfo = POPT_ARG_INT,
6519 .descrip = "Changes the transmit/send buffer",
6520 .argDescrip = "BYTES",
6523 .longName = "timeout",
6525 .argInfo = POPT_ARG_INT,
6528 .descrip = "Changes the per-operation timeout",
6529 .argDescrip = "SECONDS",
6534 .argInfo = POPT_ARG_INT,
6537 .descrip = "Port to connect to",
6538 .argDescrip = "PORT",
6541 .longName = "grepable",
6543 .argInfo = POPT_ARG_NONE,
6546 .descrip = "Produce grepable output",
6549 .longName = "quiet",
6551 .argInfo = POPT_ARG_NONE,
6554 .descrip = "Suppress help message",
6557 .longName = "browse",
6559 .argInfo = POPT_ARG_NONE,
6562 .descrip = "Browse SMB servers using DNS",
6565 POPT_COMMON_CONNECTION
6566 POPT_COMMON_CREDENTIALS
6571 TALLOC_CTX *frame = talloc_stackframe();
6572 struct cli_credentials *creds = NULL;
6573 struct loadparm_context *lp_ctx = NULL;
6575 if (!client_set_cur_dir("\\")) {
6581 ok = samba_cmdline_init(frame,
6582 SAMBA_CMDLINE_CONFIG_CLIENT,
6583 false /* require_smbconf */);
6585 DBG_ERR("Failed to init cmdline parser!\n");
6588 lp_ctx = samba_cmdline_get_lp_ctx();
6589 lpcfg_set_cmdline(lp_ctx, "log level", "1");
6592 pc = samba_popt_get_context(getprogname(),
6598 DBG_ERR("Failed to setup popt context!\n");
6602 poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
6604 creds = samba_cmdline_get_creds();
6605 while ((opt = poptGetNextOpt(pc)) != -1) {
6608 * if the tar option has been called previously, now
6609 * we need to eat out the leftovers
6611 /* I see no other way to keep things sane --SSS */
6612 if (tar_opt == true) {
6613 while (poptPeekArg(pc)) {
6619 /* if the service has not yet been specified lets see if it is available in the popt stack */
6620 if (!service_opt && poptPeekArg(pc)) {
6621 service = talloc_strdup(frame, poptGetArg(pc));
6628 /* if the service has already been retrieved then check if we have also a password */
6630 cli_credentials_get_password(creds) == NULL &&
6632 cli_credentials_set_password(creds,
6640 /* Messages are sent to NetBIOS name type 0x3
6641 * (Messenger Service). Make sure we default
6642 * to port 139 instead of port 445. srl,crh
6645 desthost = talloc_strdup(frame,poptGetOptArg(pc));
6650 port = NBT_SMB_PORT;
6655 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
6659 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
6663 setup_logging("smbclient", DEBUG_STDERR );
6664 display_set_stderr();
6668 query_host = talloc_strdup(frame, poptGetOptArg(pc));
6674 /* We must use old option processing for this. Find the
6675 * position of the -T option in the raw argv[]. */
6679 for (i = 1; i < argc; i++) {
6680 if (strncmp("-T", argv[i],2)==0)
6684 if (tar_parse_args(tar_ctx, poptGetOptArg(pc),
6685 const_argv + i, argc - i)) {
6686 poptPrintUsage(pc, stderr, 0);
6690 /* this must be the last option, mark we have parsed it so that we know we have */
6694 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
6695 if (!base_directory) {
6706 return(do_smb_browse());
6707 case POPT_ERROR_BADOPT:
6708 fprintf(stderr, "\nInvalid option %s: %s\n\n",
6709 poptBadOption(pc, 0), poptStrerror(opt));
6710 poptPrintUsage(pc, stderr, 0);
6715 /* We may still have some leftovers after the last popt option has been called */
6716 if (tar_opt == true) {
6717 while (poptPeekArg(pc)) {
6723 /* if the service has not yet been specified lets see if it is available in the popt stack */
6724 if (!service_opt && poptPeekArg(pc)) {
6725 service = talloc_strdup(frame,poptGetArg(pc));
6732 /* if the service has already been retrieved then check if we have also a password */
6734 cli_credentials_get_password(creds) == NULL &&
6736 cli_credentials_set_password(creds,
6741 if (service_opt && service) {
6744 /* Convert any '/' characters in the service name to '\' characters */
6745 string_replace(service, '/','\\');
6746 if (count_chars(service,'\\') < 3) {
6747 d_printf("\n%s: Not enough '\\' characters in service\n",service);
6748 poptPrintUsage(pc, stderr, 0);
6751 /* Remove trailing slashes */
6752 len = strlen(service);
6753 while(len > 0 && service[len - 1] == '\\') {
6755 service[len] = '\0';
6759 if(new_name_resolve_order)
6760 lpcfg_set_cmdline(lp_ctx,
6761 "name resolve order",
6762 new_name_resolve_order);
6764 if (!tar_to_process(tar_ctx) && !query_host && !service && !message) {
6765 poptPrintUsage(pc, stderr, 0);
6769 poptFreeContext(pc);
6770 samba_cmdline_burn(argc, argv);
6772 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
6774 if (tar_to_process(tar_ctx)) {
6776 process_command_string(cmdstr);
6777 rc = do_tar_op(base_directory);
6778 } else if (query_host && *query_host) {
6779 char *qhost = query_host;
6782 while (*qhost == '\\' || *qhost == '/')
6785 if ((slash = strchr_m(qhost, '/'))
6786 || (slash = strchr_m(qhost, '\\'))) {
6790 if ((p=strchr_m(qhost, '#'))) {
6793 sscanf(p, "%x", &name_type);
6796 rc = do_host_query(lp_ctx, qhost);
6797 } else if (message) {
6798 rc = do_message_op(creds);
6799 } else if (process(base_directory)) {