2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) Gerald (Jerry) Carter 2004
8 Copyright (C) Jeremy Allison 1994-2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/filesys.h"
26 #include "popt_common_cmdline.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "client/client_proto.h"
29 #include "client/clitar_proto.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
31 #include "../lib/util/select.h"
32 #include "system/readline.h"
33 #include "../libcli/smbreadline/smbreadline.h"
34 #include "../libcli/security/security.h"
35 #include "system/select.h"
36 #include "libsmb/libsmb.h"
37 #include "libsmb/clirap.h"
39 #include "libsmb/nmblib.h"
40 #include "include/ntioctl.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/util/time_basic.h"
43 #include "lib/util/string_wrappers.h"
49 extern int do_smb_browse(void); /* mDNS browsing */
53 static char *desthost;
54 static bool grepable = false;
55 static bool quiet = false;
56 static char *cmdstr = NULL;
57 const char *cmd_ptr = NULL;
59 static int io_bufsize = 0; /* we use the default size */
60 static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
62 static int name_type = 0x20;
64 static int process_tok(char *tok);
65 static int cmd_help(void);
67 /* value for unused fid field in trans2 secondary request */
68 #define FID_UNUSED (0xFFFF)
70 time_t newer_than = 0;
71 static int archive_level = 0;
73 static bool translation = false;
76 static bool prompt = true;
78 static bool recurse = false;
79 static bool showacls = false;
80 bool lowercase = false;
81 static bool backup_intent = false;
83 static struct sockaddr_storage dest_ss;
84 static char dest_ss_str[INET6_ADDRSTRLEN];
86 #define SEPARATORS " \t\n\r"
89 uint64_t get_total_size = 0;
90 unsigned int get_total_time_ms = 0;
91 static uint64_t put_total_size = 0;
92 static unsigned int put_total_time_ms = 0;
95 static double dir_total;
97 /* root cli_state connection */
99 struct cli_state *cli;
101 static char CLI_DIRSEP_CHAR = '\\';
102 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
104 /* Accessor functions for directory paths. */
105 static char *fileselection;
106 static const char *client_get_fileselection(void)
109 return fileselection;
114 static const char *client_set_fileselection(const char *new_fs)
116 SAFE_FREE(fileselection);
118 fileselection = SMB_STRDUP(new_fs);
120 return client_get_fileselection();
124 static const char *client_get_cwd(void)
129 return CLI_DIRSEP_STR;
132 static const char *client_set_cwd(const char *new_cwd)
136 cwd = SMB_STRDUP(new_cwd);
138 return client_get_cwd();
141 static char *cur_dir;
142 const char *client_get_cur_dir(void)
147 return CLI_DIRSEP_STR;
150 const char *client_set_cur_dir(const char *newdir)
154 cur_dir = SMB_STRDUP(newdir);
156 return client_get_cur_dir();
159 /****************************************************************************
160 Put up a yes/no prompt.
161 ****************************************************************************/
163 static bool yesno(const char *p)
168 if (!fgets(ans,sizeof(ans)-1,stdin))
171 if (*ans == 'y' || *ans == 'Y')
177 /****************************************************************************
178 Write to a local file with CR/LF->LF translation if appropriate. Return the
179 number taken from the buffer. This may not equal the number written.
180 ****************************************************************************/
182 static ssize_t writefile(int f, char *b, size_t n)
196 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
199 if (write(f, b, 1) != 1) {
209 /****************************************************************************
210 Read from a file with LF->CR/LF translation if appropriate. Return the
211 number read. read approx n bytes.
212 ****************************************************************************/
214 static int readfile(uint8_t *b, int n, FILE *f)
220 return fread(b,1,n,f);
223 while (i < (n - 1)) {
224 if ((c = getc(f)) == EOF) {
228 if (c == '\n') { /* change all LFs to CR/LF */
243 static size_t push_source(uint8_t *buf, size_t n, void *priv)
245 struct push_state *state = (struct push_state *)priv;
248 if (feof(state->f)) {
252 result = readfile(buf, n, state->f);
253 state->nread += result;
257 /****************************************************************************
259 ****************************************************************************/
261 static void send_message(const char *username)
267 d_printf("Type your message, ending it with a Control-D\n");
270 while (i<sizeof(buf)-2) {
271 int c = fgetc(stdin);
282 status = cli_message(cli, desthost, username, buf);
283 if (!NT_STATUS_IS_OK(status)) {
284 d_fprintf(stderr, "cli_message returned %s\n",
289 /****************************************************************************
290 Check the space on a device.
291 ****************************************************************************/
293 static int do_dskattr(void)
295 uint64_t total, bsize, avail;
296 struct cli_state *targetcli = NULL;
297 char *targetpath = NULL;
298 TALLOC_CTX *ctx = talloc_tos();
299 struct cli_credentials *creds =
300 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
303 status = cli_resolve_path(ctx,
307 client_get_cur_dir(), &targetcli,
309 if (!NT_STATUS_IS_OK(status)) {
310 d_printf("Error in dskattr: %s\n", nt_errstr(status));
314 status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
315 if (!NT_STATUS_IS_OK(status)) {
316 d_printf("Error in dskattr: %s\n", nt_errstr(status));
320 d_printf("\n\t\t%" PRIu64
321 " blocks of size %" PRIu64
322 ". %" PRIu64 " blocks available\n",
323 total, bsize, avail);
328 /****************************************************************************
330 ****************************************************************************/
332 static int cmd_pwd(void)
334 d_printf("Current directory is %s",service);
335 d_printf("%s\n",client_get_cur_dir());
339 /****************************************************************************
340 Ensure name has correct directory separators.
341 ****************************************************************************/
343 static void normalize_name(char *newdir)
345 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
346 string_replace(newdir,'/','\\');
350 /****************************************************************************
351 Local name cleanup before sending to server. SMB1 allows relative pathnames,
352 but SMB2 does not, so we need to resolve them locally.
353 ****************************************************************************/
355 char *client_clean_name(TALLOC_CTX *ctx, const char *name)
357 char *newname = NULL;
362 /* First ensure any path separators are correct. */
363 newname = talloc_strdup(ctx, name);
364 if (newname == NULL) {
367 normalize_name(newname);
369 /* Now remove any relative (..) path components. */
370 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
371 newname = unix_clean_name(ctx, newname);
373 newname = clean_name(ctx, newname);
375 if (newname == NULL) {
381 /****************************************************************************
382 Change directory - inner section.
383 ****************************************************************************/
385 static int do_cd(const char *new_dir)
388 char *saved_dir = NULL;
390 char *targetpath = NULL;
391 struct cli_state *targetcli = NULL;
392 SMB_STRUCT_STAT sbuf;
395 TALLOC_CTX *ctx = talloc_stackframe();
396 struct cli_credentials *creds =
397 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
400 newdir = talloc_strdup(ctx, new_dir);
406 normalize_name(newdir);
408 /* Save the current directory in case the new directory is invalid */
410 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
416 if (*newdir == CLI_DIRSEP_CHAR) {
417 client_set_cur_dir(newdir);
420 new_cd = talloc_asprintf(ctx, "%s%s",
421 client_get_cur_dir(),
428 /* Ensure cur_dir ends in a DIRSEP */
429 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
430 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
435 client_set_cur_dir(new_cd);
437 new_cd = client_clean_name(ctx, new_cd);
438 client_set_cur_dir(new_cd);
440 status = cli_resolve_path(ctx, "",
442 cli, new_cd, &targetcli, &targetpath);
443 if (!NT_STATUS_IS_OK(status)) {
444 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
445 client_set_cur_dir(saved_dir);
449 if (strequal(targetpath,CLI_DIRSEP_STR )) {
454 /* Use a trans2_qpathinfo to test directories for modern servers.
455 Except Win9x doesn't support the qpathinfo_basic() call..... */
457 if (smbXcli_conn_protocol(targetcli->conn) > PROTOCOL_LANMAN2 && !targetcli->win95) {
459 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
461 if (!NT_STATUS_IS_OK(status)) {
462 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
463 client_set_cur_dir(saved_dir);
467 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
468 d_printf("cd %s: not a directory\n", new_cd);
469 client_set_cur_dir(saved_dir);
474 targetpath = talloc_asprintf(ctx,
479 client_set_cur_dir(saved_dir);
482 targetpath = client_clean_name(ctx, targetpath);
484 client_set_cur_dir(saved_dir);
488 status = cli_chkpath(targetcli, targetpath);
489 if (!NT_STATUS_IS_OK(status)) {
490 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
491 client_set_cur_dir(saved_dir);
504 /****************************************************************************
506 ****************************************************************************/
508 static int cmd_cd(void)
513 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
516 d_printf("Current directory is %s\n",client_get_cur_dir());
522 /****************************************************************************
524 ****************************************************************************/
526 static int cmd_cd_oneup(void)
531 /*******************************************************************
532 Decide if a file should be operated on.
533 ********************************************************************/
535 static bool do_this_one(struct file_info *finfo)
541 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
545 if (*client_get_fileselection() &&
546 !mask_match(finfo->name,client_get_fileselection(),false)) {
547 DEBUG(3,("mask_match %s failed\n", finfo->name));
551 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
552 DEBUG(3,("newer_than %s failed\n", finfo->name));
556 if ((archive_level==1 || archive_level==2) && !(finfo->attr & FILE_ATTRIBUTE_ARCHIVE)) {
557 DEBUG(3,("archive %s failed\n", finfo->name));
564 /****************************************************************************
565 Display info about a file.
566 ****************************************************************************/
568 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
572 TALLOC_CTX *ctx = talloc_tos();
573 NTSTATUS status = NT_STATUS_OK;
575 if (!do_this_one(finfo)) {
579 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
581 d_printf(" %-30s%7.7s %8.0f %s",
583 attrib_string(talloc_tos(), finfo->attr),
586 dir_total += finfo->size;
591 /* skip if this is . or .. */
592 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
594 /* create absolute filename for cli_ntcreate() FIXME */
595 afname = talloc_asprintf(ctx,
601 return NT_STATUS_NO_MEMORY;
603 /* print file meta date header */
604 d_printf( "FILENAME:%s\n", finfo->name);
605 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->attr));
606 d_printf( "SIZE:%.0f\n", (double)finfo->size);
607 d_printf( "MTIME:%s", time_to_asc(t));
608 status = cli_ntcreate(
612 READ_CONTROL_ACCESS, /* DesiredAccess */
613 0, /* FileAttributes */
615 FILE_SHARE_WRITE, /* ShareAccess */
616 FILE_OPEN, /* CreateDisposition */
617 0x0, /* CreateOptions */
618 0x0, /* SecurityFlags */
621 if (!NT_STATUS_IS_OK(status)) {
622 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
623 afname, nt_errstr(status)));
625 struct security_descriptor *sd = NULL;
626 status = cli_query_secdesc(cli_state, fnum,
628 if (!NT_STATUS_IS_OK(status)) {
629 DEBUG( 0, ("display_finfo() failed to "
630 "get security descriptor: %s",
633 display_sec_desc(sd);
642 /****************************************************************************
643 Accumulate size of a file.
644 ****************************************************************************/
646 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
649 if (do_this_one(finfo)) {
650 dir_total += finfo->size;
655 struct do_list_queue_entry {
656 struct do_list_queue_entry *prev, *next;
660 struct do_list_queue {
661 struct do_list_queue_entry *list;
664 static bool do_list_recurse;
665 static bool do_list_dirs;
666 static struct do_list_queue *queue = NULL;
667 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
670 /****************************************************************************
671 Functions for do_list_queue.
672 ****************************************************************************/
674 static void reset_do_list_queue(void)
679 static void init_do_list_queue(void)
682 queue = talloc_zero(NULL, struct do_list_queue);
685 static void add_to_do_list_queue(const char *entry)
687 struct do_list_queue_entry *e = NULL;
688 size_t entry_str_len = strlen(entry)+1;
689 size_t entry_len = offsetof(struct do_list_queue_entry, name);
691 entry_len += entry_str_len;
692 SMB_ASSERT(entry_len >= entry_str_len);
694 e = talloc_size(queue, entry_len);
696 d_printf("talloc failed for entry %s\n", entry);
699 talloc_set_name_const(e, "struct do_list_queue_entry");
701 memcpy(e->name, entry, entry_str_len);
702 DLIST_ADD_END(queue->list, e);
705 static char *do_list_queue_head(void)
707 return queue->list->name;
710 static void remove_do_list_queue_head(void)
712 struct do_list_queue_entry *e = queue->list;
713 DLIST_REMOVE(queue->list, e);
717 static int do_list_queue_empty(void)
719 return (queue == NULL) || (queue->list == NULL);
722 /****************************************************************************
723 A helper for do_list.
724 ****************************************************************************/
726 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
727 const char *mask, void *state)
729 struct cli_state *cli_state = (struct cli_state *)state;
730 TALLOC_CTX *ctx = talloc_tos();
732 char *dir_end = NULL;
733 NTSTATUS status = NT_STATUS_OK;
737 /* Work out the directory. */
738 dir = talloc_strdup(ctx, mask);
740 return NT_STATUS_NO_MEMORY;
742 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
746 if (!(f->attr & FILE_ATTRIBUTE_DIRECTORY)) {
747 if (do_this_one(f)) {
748 status = do_list_fn(cli_state, f, dir);
754 if (do_list_dirs && do_this_one(f)) {
755 status = do_list_fn(cli_state, f, dir);
756 if (!NT_STATUS_IS_OK(status)) {
761 if (!do_list_recurse ||
769 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
771 return NT_STATUS_UNSUCCESSFUL;
774 mask2 = talloc_asprintf(ctx,
780 return NT_STATUS_NO_MEMORY;
782 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
788 mask2 = talloc_asprintf_append(mask2,
794 return NT_STATUS_NO_MEMORY;
796 add_to_do_list_queue(mask2);
803 /****************************************************************************
804 A wrapper around cli_list that adds recursion.
805 ****************************************************************************/
807 NTSTATUS do_list(const char *mask,
809 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
814 static int in_do_list = 0;
815 TALLOC_CTX *ctx = talloc_tos();
816 struct cli_credentials *creds =
817 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
818 NTSTATUS ret_status = NT_STATUS_OK;
819 NTSTATUS status = NT_STATUS_OK;
821 if (in_do_list && rec) {
822 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
828 do_list_recurse = rec;
832 init_do_list_queue();
833 add_to_do_list_queue(mask);
835 while (!do_list_queue_empty()) {
836 const char *head = do_list_queue_head();
837 struct cli_state *targetcli = NULL;
838 char *targetpath = NULL;
842 status = cli_resolve_path(ctx, "",
844 cli, head, &targetcli, &targetpath);
845 if (!NT_STATUS_IS_OK(status)) {
846 d_printf("do_list: [%s] %s\n", head,
848 remove_do_list_queue_head();
852 status = cli_list(targetcli, targetpath, attribute,
853 do_list_helper, targetcli);
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 =
1054 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
1057 lname = talloc_strdup(ctx, lname_in);
1063 if (!strlower_m(lname)) {
1064 d_printf("strlower_m %s failed\n", lname);
1069 status = cli_resolve_path(ctx, "",
1071 cli, rname, &targetcli, &targetname);
1072 if (!NT_STATUS_IS_OK(status)) {
1073 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1077 clock_gettime_mono(&tp_start);
1079 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1080 if (!NT_STATUS_IS_OK(status)) {
1081 d_printf("%s opening remote file %s\n", nt_errstr(status),
1086 if(!strcmp(lname,"-")) {
1087 handle = fileno(stdout);
1090 handle = open(lname, O_WRONLY|O_CREAT, 0644);
1092 start = lseek(handle, 0, SEEK_END);
1094 d_printf("Error seeking local file\n");
1100 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1105 d_printf("Error opening local file %s\n",lname);
1110 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1112 if (!NT_STATUS_IS_OK(status)) {
1113 d_printf("getattrib: %s\n", nt_errstr(status));
1120 DEBUG(1,("getting file %s of size %.0f as %s ",
1121 rname, (double)size, lname));
1123 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1124 writefile_sink, (void *)&handle, &nread);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 d_fprintf(stderr, "parallel_read returned %s\n",
1131 cli_close(targetcli, fnum);
1135 status = cli_close(targetcli, fnum);
1136 if (!NT_STATUS_IS_OK(status)) {
1137 d_printf("Error %s closing remote file\n", nt_errstr(status));
1145 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1146 cli_setatr(cli, rname, attr & ~(uint32_t)FILE_ATTRIBUTE_ARCHIVE, 0);
1150 struct timespec tp_end;
1153 clock_gettime_mono(&tp_end);
1154 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1155 get_total_time_ms += this_time;
1156 get_total_size += nread;
1158 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1159 nread / (1.024*this_time + 1.0e-4),
1160 get_total_size / (1.024*get_total_time_ms)));
1163 TALLOC_FREE(targetname);
1167 /****************************************************************************
1169 ****************************************************************************/
1171 static int cmd_get(void)
1173 TALLOC_CTX *ctx = talloc_tos();
1178 rname = talloc_strdup(ctx, client_get_cur_dir());
1183 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1184 d_printf("get <filename> [localname]\n");
1187 rname = talloc_asprintf_append(rname, "%s", fname);
1191 rname = client_clean_name(ctx, rname);
1196 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1201 return do_get(rname, lname, false);
1204 /****************************************************************************
1205 Do an mget operation on one file.
1206 ****************************************************************************/
1208 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1211 TALLOC_CTX *ctx = talloc_tos();
1212 const char *client_cwd = NULL;
1213 size_t client_cwd_len;
1215 char *local_path = NULL;
1218 return NT_STATUS_OK;
1221 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1222 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 =
1428 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
1431 status = cli_resolve_path(ctx, "",
1433 cli, name, &targetcli, &targetname);
1434 if (!NT_STATUS_IS_OK(status)) {
1435 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1439 status = cli_mkdir(targetcli, targetname);
1440 if (!NT_STATUS_IS_OK(status)) {
1441 d_printf("%s making remote directory %s\n",
1442 nt_errstr(status),name);
1449 /****************************************************************************
1450 Show 8.3 name of a file.
1451 ****************************************************************************/
1453 static bool do_altname(const char *name)
1458 status = cli_qpathinfo_alt_name(cli, name, altname);
1459 if (!NT_STATUS_IS_OK(status)) {
1460 d_printf("%s getting alt name for %s\n",
1461 nt_errstr(status),name);
1464 d_printf("%s\n", altname);
1469 /****************************************************************************
1471 ****************************************************************************/
1473 static int cmd_quit(void)
1476 popt_free_cmdline_auth_info();
1482 /****************************************************************************
1484 ****************************************************************************/
1486 static int cmd_mkdir(void)
1488 TALLOC_CTX *ctx = talloc_tos();
1491 struct cli_credentials *creds =
1492 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
1495 mask = talloc_strdup(ctx, client_get_cur_dir());
1500 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1502 d_printf("mkdir <dirname>\n");
1506 mask = talloc_asprintf_append(mask, "%s", buf);
1510 mask = client_clean_name(ctx, mask);
1518 struct cli_state *targetcli;
1519 char *targetname = NULL;
1523 ddir2 = talloc_strdup(ctx, "");
1528 status = cli_resolve_path(ctx, "",
1531 &targetcli, &targetname);
1532 if (!NT_STATUS_IS_OK(status)) {
1536 ddir = talloc_strdup(ctx, targetname);
1540 trim_char(ddir,'.','\0');
1541 p = strtok_r(ddir, "/\\", &saveptr);
1543 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1547 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1550 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1554 p = strtok_r(NULL, "/\\", &saveptr);
1563 /****************************************************************************
1565 ****************************************************************************/
1567 static int cmd_altname(void)
1569 TALLOC_CTX *ctx = talloc_tos();
1573 name = talloc_strdup(ctx, client_get_cur_dir());
1578 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1579 d_printf("altname <file>\n");
1582 name = talloc_asprintf_append(name, "%s", buf);
1586 name = client_clean_name(ctx, name);
1594 static char *attr_str(TALLOC_CTX *mem_ctx, uint32_t attr)
1596 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1599 if (!(attr & FILE_ATTRIBUTE_NORMAL)) {
1600 if (attr & FILE_ATTRIBUTE_ENCRYPTED) {
1603 if (attr & FILE_ATTRIBUTE_NONINDEXED) {
1606 if (attr & FILE_ATTRIBUTE_OFFLINE) {
1609 if (attr & FILE_ATTRIBUTE_COMPRESSED) {
1612 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1615 if (attr & FILE_ATTRIBUTE_SPARSE) {
1618 if (attr & FILE_ATTRIBUTE_TEMPORARY) {
1621 if (attr & FILE_ATTRIBUTE_NORMAL) {
1624 if (attr & FILE_ATTRIBUTE_READONLY) {
1627 if (attr & FILE_ATTRIBUTE_HIDDEN) {
1630 if (attr & FILE_ATTRIBUTE_SYSTEM) {
1633 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
1636 if (attr & FILE_ATTRIBUTE_ARCHIVE) {
1643 /****************************************************************************
1644 Show all info we can get
1645 ****************************************************************************/
1647 static int do_allinfo(const char *name)
1650 struct timespec b_time, a_time, m_time, c_time;
1655 unsigned int num_streams;
1656 struct stream_struct *streams;
1657 int j, num_snapshots;
1658 char **snapshots = NULL;
1662 status = cli_qpathinfo_alt_name(cli, name, altname);
1663 if (!NT_STATUS_IS_OK(status)) {
1664 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1667 * Ignore not supported or not implemented, it does not
1668 * hurt if we can't list alternate names.
1670 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1671 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1677 d_printf("altname: %s\n", altname);
1679 status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1680 &size, &attr, NULL);
1681 if (!NT_STATUS_IS_OK(status)) {
1682 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1687 tmp = full_timespec_to_nt_time(&b_time);
1688 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1690 tmp = full_timespec_to_nt_time(&a_time);
1691 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1693 tmp = full_timespec_to_nt_time(&m_time);
1694 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1696 tmp = full_timespec_to_nt_time(&c_time);
1697 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1699 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), attr), attr);
1701 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1703 if (!NT_STATUS_IS_OK(status)) {
1704 d_printf("%s getting streams for %s\n", nt_errstr(status),
1709 for (i=0; i<num_streams; i++) {
1710 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1711 (unsigned long long)streams[i].size);
1714 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1715 char *subst, *print;
1718 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1720 if (!NT_STATUS_IS_OK(status)) {
1721 d_fprintf(stderr, "cli_readlink returned %s\n",
1724 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1725 subst, print, flags);
1731 status = cli_ntcreate(cli, name, 0,
1732 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1733 SEC_STD_SYNCHRONIZE, 0,
1734 FILE_SHARE_READ|FILE_SHARE_WRITE
1736 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
1737 if (!NT_STATUS_IS_OK(status)) {
1739 * Ignore failure, it does not hurt if we can't list
1745 * In order to get shadow copy data over SMB1 we
1746 * must call twice, once with 'get_names = false'
1747 * to get the size, then again with 'get_names = true'
1748 * to get the data or a Windows server fails to return
1749 * valid info. Samba doesn't have this bug. JRA.
1752 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1753 false, &snapshots, &num_snapshots);
1754 if (!NT_STATUS_IS_OK(status)) {
1755 cli_close(cli, fnum);
1758 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1759 true, &snapshots, &num_snapshots);
1760 if (!NT_STATUS_IS_OK(status)) {
1761 cli_close(cli, fnum);
1765 for (j=0; j<num_snapshots; j++) {
1768 d_printf("%s\n", snapshots[j]);
1769 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1770 snapshots[j], name);
1771 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1772 &m_time, &c_time, &size,
1774 if (!NT_STATUS_IS_OK(status)) {
1775 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1776 snap_name, nt_errstr(status));
1777 TALLOC_FREE(snap_name);
1780 tmp = unix_timespec_to_nt_time(b_time);
1781 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1782 tmp = unix_timespec_to_nt_time(a_time);
1783 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1784 tmp =unix_timespec_to_nt_time(m_time);
1785 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1786 tmp = unix_timespec_to_nt_time(c_time);
1787 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1788 d_printf("size: %d\n", (int)size);
1791 TALLOC_FREE(snapshots);
1792 cli_close(cli, fnum);
1797 /****************************************************************************
1798 Show all info we can get
1799 ****************************************************************************/
1801 static int cmd_allinfo(void)
1803 TALLOC_CTX *ctx = talloc_tos();
1807 name = talloc_strdup(ctx, client_get_cur_dir());
1812 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1813 d_printf("allinfo <file>\n");
1816 name = talloc_asprintf_append(name, "%s", buf);
1820 name = client_clean_name(ctx, name);
1829 /****************************************************************************
1831 ****************************************************************************/
1833 static int do_put(const char *rname, const char *lname, bool reput)
1835 TALLOC_CTX *ctx = talloc_tos();
1840 struct timespec tp_start;
1841 struct cli_state *targetcli;
1842 char *targetname = NULL;
1843 struct push_state state;
1844 struct cli_credentials *creds =
1845 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
1848 status = cli_resolve_path(ctx, "",
1850 cli, rname, &targetcli, &targetname);
1851 if (!NT_STATUS_IS_OK(status)) {
1852 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1856 clock_gettime_mono(&tp_start);
1859 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1860 if (NT_STATUS_IS_OK(status)) {
1861 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1862 targetcli, fnum, NULL,
1864 NULL, NULL, NULL))) {
1865 d_printf("getattrib: %s\n", nt_errstr(status));
1870 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1873 if (!NT_STATUS_IS_OK(status)) {
1874 d_printf("%s opening remote file %s\n", nt_errstr(status),
1879 /* allow files to be piped into smbclient
1882 Note that in this case this function will exit(0) rather
1884 if (!strcmp(lname, "-")) {
1886 /* size of file is not known */
1888 f = fopen(lname, "r");
1890 if (fseek(f, start, SEEK_SET) == -1) {
1891 d_printf("Error seeking local file\n");
1899 d_printf("Error opening local file %s\n",lname);
1903 DEBUG(1,("putting file %s as %s ",lname,
1906 setvbuf(f, NULL, _IOFBF, io_bufsize);
1911 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1913 if (!NT_STATUS_IS_OK(status)) {
1914 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1918 status = cli_close(targetcli, fnum);
1919 if (!NT_STATUS_IS_OK(status)) {
1920 d_printf("%s closing remote file %s\n", nt_errstr(status),
1933 struct timespec tp_end;
1936 clock_gettime_mono(&tp_end);
1937 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1938 put_total_time_ms += this_time;
1939 put_total_size += state.nread;
1941 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1942 state.nread / (1.024*this_time + 1.0e-4),
1943 put_total_size / (1.024*put_total_time_ms)));
1948 popt_free_cmdline_auth_info();
1955 /****************************************************************************
1957 ****************************************************************************/
1959 static int cmd_put(void)
1961 TALLOC_CTX *ctx = talloc_tos();
1966 rname = talloc_strdup(ctx, client_get_cur_dir());
1971 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1972 d_printf("put <filename>\n");
1976 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1977 rname = talloc_asprintf_append(rname, "%s", buf);
1979 rname = talloc_asprintf_append(rname, "%s", lname);
1985 rname = client_clean_name(ctx, rname);
1992 /* allow '-' to represent stdin
1993 jdblair, 24.jun.98 */
1994 if (!file_exist_stat(lname, &st, false) &&
1995 (strcmp(lname,"-"))) {
1996 d_printf("%s does not exist\n",lname);
2001 return do_put(rname, lname, false);
2004 /*************************************
2005 File list structure.
2006 *************************************/
2008 static struct file_list {
2009 struct file_list *prev, *next;
2014 /****************************************************************************
2015 Free a file_list structure.
2016 ****************************************************************************/
2018 static void free_file_list (struct file_list *l_head)
2020 struct file_list *list, *next;
2022 for (list = l_head; list; list = next) {
2024 DLIST_REMOVE(l_head, list);
2029 /****************************************************************************
2030 Seek in a directory/file list until you get something that doesn't start with
2032 ****************************************************************************/
2034 static bool seek_list(struct file_list *list, char *name)
2037 trim_string(list->file_path,"./","\n");
2038 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2047 /****************************************************************************
2048 Set the file selection mask.
2049 ****************************************************************************/
2051 static int cmd_select(void)
2053 TALLOC_CTX *ctx = talloc_tos();
2054 char *new_fs = NULL;
2055 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2058 client_set_fileselection(new_fs);
2060 client_set_fileselection("");
2065 /****************************************************************************
2066 Recursive file matching function act as find
2067 match must be always set to true when calling this function
2068 ****************************************************************************/
2070 static int file_find(TALLOC_CTX *ctx,
2071 struct file_list **list,
2072 const char *directory,
2073 const char *expression,
2077 struct file_list *entry;
2078 struct stat statbuf;
2084 dir = opendir(directory);
2088 while ((dname = readdirname(dir))) {
2089 if (!strcmp("..", dname))
2091 if (!strcmp(".", dname))
2094 path = talloc_asprintf(ctx, "%s/%s", directory, dname);
2100 if (!match || !gen_fnmatch(expression, dname)) {
2102 ret = stat(path, &statbuf);
2104 if (S_ISDIR(statbuf.st_mode)) {
2106 ret = file_find(ctx,
2113 d_printf("file_find: cannot stat file %s\n", path);
2122 entry = talloc_zero(ctx, struct file_list);
2124 d_printf("Out of memory in file_find\n");
2128 entry->file_path = talloc_move(entry, &path);
2129 entry->isdir = isdir;
2130 DLIST_ADD(*list, entry);
2140 /****************************************************************************
2142 ****************************************************************************/
2144 static int cmd_mput(void)
2146 TALLOC_CTX *ctx = talloc_tos();
2149 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2151 struct file_list *temp_list;
2152 char *quest, *lname, *rname;
2156 ret = file_find(ctx, &file_list, ".", p, true);
2158 free_file_list(file_list);
2166 for (temp_list = file_list; temp_list;
2167 temp_list = temp_list->next) {
2170 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2173 trim_string(lname, "./", "/");
2175 /* check if it's a directory */
2176 if (temp_list->isdir) {
2177 /* if (!recurse) continue; */
2180 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2183 if (prompt && !yesno(quest)) { /* No */
2184 /* Skip the directory */
2185 lname[strlen(lname)-1] = '/';
2186 if (!seek_list(temp_list, lname))
2190 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2193 normalize_name(rname);
2196 client_clean_name(ctx, rname);
2197 if (tmp_rname == NULL) {
2201 rname = smb_xstrdup(tmp_rname);
2202 TALLOC_FREE(tmp_rname);
2203 if (rname == NULL) {
2207 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2209 DEBUG (0, ("Unable to make dir, skipping..."));
2210 /* Skip the directory */
2211 lname[strlen(lname)-1] = '/';
2212 if (!seek_list(temp_list, lname)) {
2220 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2223 if (prompt && !yesno(quest)) {
2230 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2235 normalize_name(rname);
2238 char *tmp_rname = client_clean_name(ctx, rname);
2239 if (tmp_rname == NULL) {
2243 rname = smb_xstrdup(tmp_rname);
2244 TALLOC_FREE(tmp_rname);
2245 if (rname == NULL) {
2249 do_put(rname, lname, false);
2251 free_file_list(file_list);
2260 /****************************************************************************
2262 ****************************************************************************/
2264 static int do_cancel(int job)
2266 if (cli_printjob_del(cli, job)) {
2267 d_printf("Job %d cancelled\n",job);
2270 NTSTATUS status = cli_nt_error(cli);
2271 d_printf("Error cancelling job %d : %s\n",
2272 job, nt_errstr(status));
2277 /****************************************************************************
2279 ****************************************************************************/
2281 static int cmd_cancel(void)
2283 TALLOC_CTX *ctx = talloc_tos();
2287 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2288 d_printf("cancel <jobid> ...\n");
2294 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2299 /****************************************************************************
2301 ****************************************************************************/
2303 static int cmd_print(void)
2305 TALLOC_CTX *ctx = talloc_tos();
2310 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2311 d_printf("print <filename>\n");
2315 rname = talloc_strdup(ctx, lname);
2319 p = strrchr_m(rname,'/');
2321 rname = talloc_asprintf(ctx,
2326 if (strequal(lname,"-")) {
2327 rname = talloc_asprintf(ctx,
2335 return do_put(rname, lname, false);
2338 /****************************************************************************
2339 Show a print queue entry.
2340 ****************************************************************************/
2342 static void queue_fn(struct print_job_info *p)
2344 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2347 /****************************************************************************
2349 ****************************************************************************/
2351 static int cmd_queue(void)
2353 cli_print_queue(cli, queue_fn);
2357 /****************************************************************************
2359 ****************************************************************************/
2361 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2364 TALLOC_CTX *ctx = talloc_tos();
2368 mask = talloc_asprintf(ctx,
2374 return NT_STATUS_NO_MEMORY;
2377 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2379 return NT_STATUS_OK;
2382 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2383 if (!NT_STATUS_IS_OK(status)) {
2384 d_printf("%s deleting remote file %s\n",
2385 nt_errstr(status), mask);
2391 /****************************************************************************
2393 ****************************************************************************/
2395 static int cmd_del(void)
2397 TALLOC_CTX *ctx = talloc_tos();
2400 NTSTATUS status = NT_STATUS_OK;
2401 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2404 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2407 mask = talloc_strdup(ctx, client_get_cur_dir());
2411 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2412 d_printf("del <filename>\n");
2415 mask = talloc_asprintf_append(mask, "%s", buf);
2419 mask = client_clean_name(ctx, mask);
2424 status = do_list(mask,attribute,do_del,false,false);
2425 if (!NT_STATUS_IS_OK(status)) {
2431 /****************************************************************************
2433 ****************************************************************************/
2435 static NTSTATUS delete_remote_files_list(struct cli_state *cli_state,
2436 struct file_list *flist)
2438 NTSTATUS status = NT_STATUS_OK;
2439 struct file_list *deltree_list_iter = NULL;
2441 for (deltree_list_iter = flist;
2442 deltree_list_iter != NULL;
2443 deltree_list_iter = deltree_list_iter->next) {
2444 if (CLI_DIRSEP_CHAR == '/') {
2446 status = cli_posix_unlink(cli_state,
2447 deltree_list_iter->file_path);
2448 } else if (deltree_list_iter->isdir) {
2449 status = cli_rmdir(cli_state,
2450 deltree_list_iter->file_path);
2452 status = cli_unlink(cli_state,
2453 deltree_list_iter->file_path,
2454 FILE_ATTRIBUTE_SYSTEM |
2455 FILE_ATTRIBUTE_HIDDEN);
2457 if (!NT_STATUS_IS_OK(status)) {
2458 d_printf("%s deleting remote %s %s\n",
2460 deltree_list_iter->isdir ?
2461 "directory" : "file",
2462 deltree_list_iter->file_path);
2466 return NT_STATUS_OK;
2469 /****************************************************************************
2470 Save a list of files to delete.
2471 ****************************************************************************/
2473 static struct file_list *deltree_list_head;
2475 static NTSTATUS do_deltree_list(struct cli_state *cli_state,
2476 struct file_info *finfo,
2479 struct file_list **file_list_head_pp = &deltree_list_head;
2480 struct file_list *dt = NULL;
2482 if (!do_this_one(finfo)) {
2483 return NT_STATUS_OK;
2486 /* skip if this is . or .. */
2487 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
2488 return NT_STATUS_OK;
2491 dt = talloc_zero(NULL, struct file_list);
2493 return NT_STATUS_NO_MEMORY;
2496 /* create absolute filename for cli_ntcreate() */
2497 dt->file_path = talloc_asprintf(dt,
2502 if (dt->file_path == NULL) {
2504 return NT_STATUS_NO_MEMORY;
2507 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2511 DLIST_ADD(*file_list_head_pp, dt);
2512 return NT_STATUS_OK;
2515 static int cmd_deltree(void)
2517 TALLOC_CTX *ctx = talloc_tos();
2519 NTSTATUS status = NT_STATUS_OK;
2520 struct file_list *deltree_list_norecurse = NULL;
2521 struct file_list *deltree_list_iter = NULL;
2522 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM |
2523 FILE_ATTRIBUTE_HIDDEN |
2524 FILE_ATTRIBUTE_DIRECTORY;
2526 char *mask = talloc_strdup(ctx, client_get_cur_dir());
2530 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
2532 d_printf("deltree <filename>\n");
2535 mask = talloc_asprintf_append(mask, "%s", buf);
2539 mask = client_clean_name(ctx, mask);
2544 deltree_list_head = NULL;
2547 * Get the list of directories to
2548 * delete (in case mask has a wildcard).
2550 status = do_list(mask, attribute, do_deltree_list, false, true);
2551 if (!NT_STATUS_IS_OK(status)) {
2554 deltree_list_norecurse = deltree_list_head;
2555 deltree_list_head = NULL;
2557 for (deltree_list_iter = deltree_list_norecurse;
2558 deltree_list_iter != NULL;
2559 deltree_list_iter = deltree_list_iter->next) {
2561 if (deltree_list_iter->isdir == false) {
2562 /* Just a regular file. */
2563 if (CLI_DIRSEP_CHAR == '/') {
2565 status = cli_posix_unlink(cli,
2566 deltree_list_iter->file_path);
2568 status = cli_unlink(cli,
2569 deltree_list_iter->file_path,
2570 FILE_ATTRIBUTE_SYSTEM |
2571 FILE_ATTRIBUTE_HIDDEN);
2573 if (!NT_STATUS_IS_OK(status)) {
2580 * Get the list of files or directories to
2581 * delete in depth order.
2583 status = do_list(deltree_list_iter->file_path,
2588 if (!NT_STATUS_IS_OK(status)) {
2591 status = delete_remote_files_list(cli, deltree_list_head);
2592 free_file_list(deltree_list_head);
2593 deltree_list_head = NULL;
2594 if (!NT_STATUS_IS_OK(status)) {
2599 free_file_list(deltree_list_norecurse);
2600 free_file_list(deltree_list_head);
2605 free_file_list(deltree_list_norecurse);
2606 free_file_list(deltree_list_head);
2607 deltree_list_head = NULL;
2612 /****************************************************************************
2613 Wildcard delete some files.
2614 ****************************************************************************/
2616 static int cmd_wdel(void)
2618 TALLOC_CTX *ctx = talloc_tos();
2622 struct cli_state *targetcli;
2623 char *targetname = NULL;
2624 struct cli_credentials *creds =
2625 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
2628 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2629 d_printf("wdel 0x<attrib> <wcard>\n");
2633 attribute = (uint32_t)strtol(buf, (char **)NULL, 16);
2635 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2636 d_printf("wdel 0x<attrib> <wcard>\n");
2640 mask = talloc_asprintf(ctx, "%s%s",
2641 client_get_cur_dir(),
2646 mask = client_clean_name(ctx, mask);
2651 status = cli_resolve_path(ctx, "",
2653 cli, mask, &targetcli, &targetname);
2654 if (!NT_STATUS_IS_OK(status)) {
2655 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2659 status = cli_unlink(targetcli, targetname, attribute);
2660 if (!NT_STATUS_IS_OK(status)) {
2661 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2667 /****************************************************************************
2668 ****************************************************************************/
2670 static int cmd_open(void)
2672 TALLOC_CTX *ctx = talloc_tos();
2675 char *targetname = NULL;
2676 struct cli_state *targetcli;
2677 uint16_t fnum = (uint16_t)-1;
2678 struct cli_credentials *creds =
2679 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
2682 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2683 d_printf("open <filename>\n");
2686 mask = talloc_asprintf(ctx,
2688 client_get_cur_dir(),
2694 mask = client_clean_name(ctx, mask);
2699 status = cli_resolve_path(ctx, "",
2701 cli, mask, &targetcli, &targetname);
2702 if (!NT_STATUS_IS_OK(status)) {
2703 d_printf("open %s: %s\n", mask, nt_errstr(status));
2707 status = cli_ntcreate(targetcli, targetname, 0,
2708 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2709 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2710 0x0, 0x0, &fnum, NULL);
2711 if (!NT_STATUS_IS_OK(status)) {
2712 status = cli_ntcreate(targetcli, targetname, 0,
2714 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2715 0x0, 0x0, &fnum, NULL);
2716 if (NT_STATUS_IS_OK(status)) {
2717 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2719 d_printf("Failed to open file %s. %s\n",
2720 targetname, nt_errstr(status));
2723 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2728 static int cmd_posix_encrypt(void)
2730 TALLOC_CTX *ctx = talloc_tos();
2731 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2732 char *domain = NULL;
2734 char *password = NULL;
2735 struct cli_credentials *creds = NULL;
2736 struct cli_credentials *lcreds = NULL;
2738 if (next_token_talloc(ctx, &cmd_ptr, &domain, NULL)) {
2740 if (!next_token_talloc(ctx, &cmd_ptr, &user, NULL)) {
2741 d_printf("posix_encrypt domain user password\n");
2745 if (!next_token_talloc(ctx, &cmd_ptr, &password, NULL)) {
2746 d_printf("posix_encrypt domain user password\n");
2750 lcreds = cli_session_creds_init(ctx,
2755 false, /* use_kerberos */
2756 false, /* fallback_after_kerberos */
2757 false, /* use_ccache */
2758 false); /* password_is_nt_hash */
2759 if (lcreds == NULL) {
2760 d_printf("cli_session_creds_init() failed.\n");
2765 bool auth_requested = false;
2767 creds = get_cmdline_auth_info_creds(
2768 popt_get_cmdline_auth_info());
2770 auth_requested = cli_credentials_authentication_requested(creds);
2771 if (!auth_requested) {
2772 d_printf("posix_encrypt domain user password\n");
2777 status = cli_smb1_setup_encryption(cli, creds);
2778 /* gensec currently references the creds so we can't free them here */
2779 talloc_unlink(ctx, lcreds);
2780 if (!NT_STATUS_IS_OK(status)) {
2781 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2783 d_printf("encryption on\n");
2784 set_cmdline_auth_info_smb_encrypt(popt_get_cmdline_auth_info());
2790 /****************************************************************************
2791 ****************************************************************************/
2793 static int cmd_posix_open(void)
2795 TALLOC_CTX *ctx = talloc_tos();
2798 char *targetname = NULL;
2799 struct cli_state *targetcli;
2802 struct cli_credentials *creds =
2803 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
2806 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2807 d_printf("posix_open <filename> 0<mode>\n");
2810 mask = talloc_asprintf(ctx,
2812 client_get_cur_dir(),
2817 mask = client_clean_name(ctx, mask);
2822 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2823 d_printf("posix_open <filename> 0<mode>\n");
2826 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2828 status = cli_resolve_path(ctx, "",
2830 cli, mask, &targetcli, &targetname);
2831 if (!NT_STATUS_IS_OK(status)) {
2832 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2836 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2838 if (!NT_STATUS_IS_OK(status)) {
2839 status = cli_posix_open(targetcli, targetname,
2840 O_CREAT|O_RDONLY, mode, &fnum);
2841 if (!NT_STATUS_IS_OK(status)) {
2842 d_printf("Failed to open file %s. %s\n", targetname,
2845 d_printf("posix_open file %s: for readonly fnum %d\n",
2849 d_printf("posix_open file %s: for read/write fnum %d\n",
2856 static int cmd_posix_mkdir(void)
2858 TALLOC_CTX *ctx = talloc_tos();
2861 char *targetname = NULL;
2862 struct cli_state *targetcli;
2864 struct cli_credentials *creds =
2865 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
2868 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2869 d_printf("posix_mkdir <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_mkdir <filename> 0<mode>\n");
2888 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2890 status = cli_resolve_path(ctx, "",
2892 cli, mask, &targetcli, &targetname);
2893 if (!NT_STATUS_IS_OK(status)) {
2894 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2898 status = cli_posix_mkdir(targetcli, targetname, mode);
2899 if (!NT_STATUS_IS_OK(status)) {
2900 d_printf("Failed to open file %s. %s\n",
2901 targetname, nt_errstr(status));
2903 d_printf("posix_mkdir created directory %s\n", targetname);
2908 static int cmd_posix_unlink(void)
2910 TALLOC_CTX *ctx = talloc_tos();
2913 char *targetname = NULL;
2914 struct cli_state *targetcli;
2915 struct cli_credentials *creds =
2916 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
2919 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2920 d_printf("posix_unlink <filename>\n");
2923 mask = talloc_asprintf(ctx,
2925 client_get_cur_dir(),
2930 mask = client_clean_name(ctx, mask);
2935 status = cli_resolve_path(ctx, "",
2937 cli, mask, &targetcli, &targetname);
2938 if (!NT_STATUS_IS_OK(status)) {
2939 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
2943 status = cli_posix_unlink(targetcli, targetname);
2944 if (!NT_STATUS_IS_OK(status)) {
2945 d_printf("Failed to unlink file %s. %s\n",
2946 targetname, nt_errstr(status));
2948 d_printf("posix_unlink deleted file %s\n", targetname);
2954 static int cmd_posix_rmdir(void)
2956 TALLOC_CTX *ctx = talloc_tos();
2959 char *targetname = NULL;
2960 struct cli_state *targetcli;
2961 struct cli_credentials *creds =
2962 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
2965 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2966 d_printf("posix_rmdir <filename>\n");
2969 mask = talloc_asprintf(ctx,
2971 client_get_cur_dir(),
2976 mask = client_clean_name(ctx, mask);
2981 status = cli_resolve_path(ctx, "",
2983 cli, mask, &targetcli, &targetname);
2984 if (!NT_STATUS_IS_OK(status)) {
2985 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
2989 status = cli_posix_rmdir(targetcli, targetname);
2990 if (!NT_STATUS_IS_OK(status)) {
2991 d_printf("Failed to unlink directory %s. %s\n",
2992 targetname, nt_errstr(status));
2994 d_printf("posix_rmdir deleted directory %s\n", targetname);
3000 static int cmd_close(void)
3002 TALLOC_CTX *ctx = talloc_tos();
3007 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3008 d_printf("close <fnum>\n");
3013 /* We really should use the targetcli here.... */
3014 status = cli_close(cli, fnum);
3015 if (!NT_STATUS_IS_OK(status)) {
3016 d_printf("close %d: %s\n", fnum, nt_errstr(status));
3022 static int cmd_posix(void)
3024 TALLOC_CTX *ctx = talloc_tos();
3025 uint16_t major, minor;
3026 uint32_t caplow, caphigh;
3030 if (!SERVER_HAS_UNIX_CIFS(cli)) {
3031 d_printf("Server doesn't support UNIX CIFS extensions.\n");
3035 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
3037 if (!NT_STATUS_IS_OK(status)) {
3038 d_printf("Can't get UNIX CIFS extensions version from "
3039 "server: %s\n", nt_errstr(status));
3043 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
3045 caps = talloc_strdup(ctx, "");
3049 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
3050 caps = talloc_asprintf_append(caps, "locks ");
3055 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
3056 caps = talloc_asprintf_append(caps, "acls ");
3061 if (caplow & CIFS_UNIX_XATTTR_CAP) {
3062 caps = talloc_asprintf_append(caps, "eas ");
3067 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3068 caps = talloc_asprintf_append(caps, "pathnames ");
3073 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
3074 caps = talloc_asprintf_append(caps, "posix_path_operations ");
3079 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
3080 caps = talloc_asprintf_append(caps, "large_read ");
3085 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
3086 caps = talloc_asprintf_append(caps, "large_write ");
3091 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
3092 caps = talloc_asprintf_append(caps, "posix_encrypt ");
3097 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
3098 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
3104 if (*caps && caps[strlen(caps)-1] == ' ') {
3105 caps[strlen(caps)-1] = '\0';
3108 d_printf("Server supports CIFS capabilities %s\n", caps);
3110 status = cli_set_unix_extensions_capabilities(cli, major, minor,
3112 if (!NT_STATUS_IS_OK(status)) {
3113 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
3118 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3119 CLI_DIRSEP_CHAR = '/';
3120 *CLI_DIRSEP_STR = '/';
3121 client_set_cur_dir(CLI_DIRSEP_STR);
3127 static int cmd_lock(void)
3129 TALLOC_CTX *ctx = talloc_tos();
3131 uint64_t start, len;
3132 enum brl_type lock_type;
3136 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3137 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3142 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3143 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3147 if (*buf == 'r' || *buf == 'R') {
3148 lock_type = READ_LOCK;
3149 } else if (*buf == 'w' || *buf == 'W') {
3150 lock_type = WRITE_LOCK;
3152 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3156 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3157 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3161 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3163 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3164 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3168 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3170 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
3171 if (!NT_STATUS_IS_OK(status)) {
3172 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
3178 static int cmd_unlock(void)
3180 TALLOC_CTX *ctx = talloc_tos();
3182 uint64_t start, len;
3186 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3187 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3192 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3193 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3197 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3199 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3200 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3204 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3206 status = cli_posix_unlock(cli, fnum, start, len);
3207 if (!NT_STATUS_IS_OK(status)) {
3208 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
3214 static int cmd_posix_whoami(void)
3216 TALLOC_CTX *ctx = talloc_tos();
3217 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
3220 uint32_t num_gids = 0;
3221 uint32_t num_sids = 0;
3222 uint64_t *gids = NULL;
3223 struct dom_sid *sids = NULL;
3227 status = cli_posix_whoami(cli,
3237 if (!NT_STATUS_IS_OK(status)) {
3238 d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
3242 d_printf("GUEST:%s\n", guest ? "True" : "False");
3243 d_printf("UID:%" PRIu64 "\n", uid);
3244 d_printf("GID:%" PRIu64 "\n", gid);
3245 d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
3246 for (i = 0; i < num_gids; i++) {
3247 d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
3249 d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
3250 for (i = 0; i < num_sids; i++) {
3251 struct dom_sid_buf buf;
3252 d_printf("SIDS[%" PRIu32 "]:%s\n",
3254 dom_sid_str_buf(&sids[i], &buf));
3260 /****************************************************************************
3262 ****************************************************************************/
3264 static int cmd_rmdir(void)
3266 TALLOC_CTX *ctx = talloc_tos();
3269 char *targetname = NULL;
3270 struct cli_state *targetcli;
3271 struct cli_credentials *creds =
3272 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3275 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3276 d_printf("rmdir <dirname>\n");
3279 mask = talloc_asprintf(ctx,
3281 client_get_cur_dir(),
3286 mask = client_clean_name(ctx, mask);
3291 status = cli_resolve_path(ctx, "",
3293 cli, mask, &targetcli, &targetname);
3294 if (!NT_STATUS_IS_OK(status)) {
3295 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3299 status = cli_rmdir(targetcli, targetname);
3300 if (!NT_STATUS_IS_OK(status)) {
3301 d_printf("%s removing remote directory file %s\n",
3302 nt_errstr(status), mask);
3308 /****************************************************************************
3310 ****************************************************************************/
3312 static int cmd_link(void)
3314 TALLOC_CTX *ctx = talloc_tos();
3315 char *oldname = NULL;
3316 char *newname = NULL;
3319 char *targetname = NULL;
3320 struct cli_state *targetcli;
3321 struct cli_credentials *creds =
3322 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3325 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3326 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3327 d_printf("link <oldname> <newname>\n");
3330 oldname = talloc_asprintf(ctx,
3332 client_get_cur_dir(),
3337 oldname = client_clean_name(ctx, oldname);
3338 if (oldname == NULL) {
3341 newname = talloc_asprintf(ctx,
3343 client_get_cur_dir(),
3348 newname = client_clean_name(ctx, newname);
3349 if (newname == NULL) {
3353 status = cli_resolve_path(ctx, "",
3355 cli, oldname, &targetcli, &targetname);
3356 if (!NT_STATUS_IS_OK(status)) {
3357 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3361 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3362 d_printf("Server doesn't support UNIX CIFS calls.\n");
3366 status = cli_posix_hardlink(targetcli, targetname, newname);
3367 if (!NT_STATUS_IS_OK(status)) {
3368 d_printf("%s linking files (%s -> %s)\n",
3369 nt_errstr(status), newname, oldname);
3375 /****************************************************************************
3377 ****************************************************************************/
3379 static int cmd_readlink(void)
3381 TALLOC_CTX *ctx = talloc_tos();
3384 char *targetname = NULL;
3385 char *linkname = NULL;
3386 struct cli_state *targetcli;
3387 struct cli_credentials *creds =
3388 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3391 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3392 d_printf("readlink <name>\n");
3395 name = talloc_asprintf(ctx,
3397 client_get_cur_dir(),
3402 name = client_clean_name(ctx, name);
3407 status = cli_resolve_path(ctx, "",
3409 cli, name, &targetcli, &targetname);
3410 if (!NT_STATUS_IS_OK(status)) {
3411 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3415 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3416 d_printf("Server doesn't support UNIX CIFS calls.\n");
3420 status = cli_posix_readlink(targetcli, name, talloc_tos(), &linkname);
3421 if (!NT_STATUS_IS_OK(status)) {
3422 d_printf("%s readlink on file %s\n",
3423 nt_errstr(status), name);
3427 d_printf("%s -> %s\n", name, linkname);
3429 TALLOC_FREE(linkname);
3435 /****************************************************************************
3437 ****************************************************************************/
3439 static int cmd_symlink(void)
3441 TALLOC_CTX *ctx = talloc_tos();
3442 char *link_target = NULL;
3443 char *newname = NULL;
3446 struct cli_state *newcli;
3447 struct cli_credentials *creds =
3448 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3451 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3452 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3453 d_printf("symlink <link_target> <newname>\n");
3456 /* Oldname (link target) must be an untouched blob. */
3459 if (SERVER_HAS_UNIX_CIFS(cli)) {
3460 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3465 newname = client_clean_name(ctx, newname);
3466 if (newname == NULL) {
3469 /* New name must be present in share namespace. */
3470 status = cli_resolve_path(ctx, "",
3474 if (!NT_STATUS_IS_OK(status)) {
3475 d_printf("link %s: %s\n", newname,
3479 status = cli_posix_symlink(newcli, link_target, newname);
3481 status = cli_symlink(
3482 cli, link_target, buf2,
3483 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3486 if (!NT_STATUS_IS_OK(status)) {
3487 d_printf("%s symlinking files (%s -> %s)\n",
3488 nt_errstr(status), newname, link_target);
3495 /****************************************************************************
3497 ****************************************************************************/
3499 static int cmd_chmod(void)
3501 TALLOC_CTX *ctx = talloc_tos();
3505 char *targetname = NULL;
3506 struct cli_state *targetcli;
3508 struct cli_credentials *creds =
3509 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3512 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3513 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3514 d_printf("chmod mode file\n");
3517 src = talloc_asprintf(ctx,
3519 client_get_cur_dir(),
3524 src = client_clean_name(ctx, src);
3529 mode = (mode_t)strtol(buf, NULL, 8);
3531 status = cli_resolve_path(ctx, "",
3533 cli, src, &targetcli, &targetname);
3534 if (!NT_STATUS_IS_OK(status)) {
3535 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3539 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3540 d_printf("Server doesn't support UNIX CIFS calls.\n");
3544 status = cli_posix_chmod(targetcli, targetname, mode);
3545 if (!NT_STATUS_IS_OK(status)) {
3546 d_printf("%s chmod file %s 0%o\n",
3547 nt_errstr(status), src, (unsigned int)mode);
3554 static const char *filetype_to_str(mode_t mode)
3556 if (S_ISREG(mode)) {
3557 return "regular file";
3558 } else if (S_ISDIR(mode)) {
3562 if (S_ISCHR(mode)) {
3563 return "character device";
3567 if (S_ISBLK(mode)) {
3568 return "block device";
3572 if (S_ISFIFO(mode)) {
3577 if (S_ISLNK(mode)) {
3578 return "symbolic link";
3582 if (S_ISSOCK(mode)) {
3589 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3598 static char *unix_mode_to_str(char *s, mode_t m)
3601 const char *str = filetype_to_str(m);
3617 *p++ = str[1] == 'y' ? 'l' : 's';
3624 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3625 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3626 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3627 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3628 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3629 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3630 *p++ = rwx_to_str(m, S_IROTH, 'r');
3631 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3632 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3637 /****************************************************************************
3638 Utility function for UNIX getfacl.
3639 ****************************************************************************/
3641 static char *perms_to_string(fstring permstr, unsigned char perms)
3643 fstrcpy(permstr, "---");
3644 if (perms & SMB_POSIX_ACL_READ) {
3647 if (perms & SMB_POSIX_ACL_WRITE) {
3650 if (perms & SMB_POSIX_ACL_EXECUTE) {
3656 /****************************************************************************
3658 ****************************************************************************/
3660 static int cmd_getfacl(void)
3662 TALLOC_CTX *ctx = talloc_tos();
3665 char *targetname = NULL;
3666 struct cli_state *targetcli;
3667 uint16_t major, minor;
3668 uint32_t caplow, caphigh;
3669 char *retbuf = NULL;
3671 SMB_STRUCT_STAT sbuf;
3672 size_t num_file_acls = 0;
3673 size_t num_dir_acls = 0;
3674 size_t expected_buflen;
3676 struct cli_credentials *creds =
3677 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3680 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3681 d_printf("getfacl filename\n");
3684 src = talloc_asprintf(ctx,
3686 client_get_cur_dir(),
3691 src = client_clean_name(ctx, src);
3696 status = cli_resolve_path(ctx, "",
3698 cli, src, &targetcli, &targetname);
3699 if (!NT_STATUS_IS_OK(status)) {
3700 d_printf("stat %s: %s\n", src, nt_errstr(status));
3704 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3705 d_printf("Server doesn't support UNIX CIFS calls.\n");
3709 status = cli_unix_extensions_version(targetcli, &major, &minor,
3711 if (!NT_STATUS_IS_OK(status)) {
3712 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3717 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3718 d_printf("This server supports UNIX extensions "
3719 "but doesn't support POSIX ACLs.\n");
3723 status = cli_posix_stat(targetcli, targetname, &sbuf);
3724 if (!NT_STATUS_IS_OK(status)) {
3725 d_printf("%s getfacl doing a stat on file %s\n",
3726 nt_errstr(status), src);
3730 status = cli_posix_getacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3731 if (!NT_STATUS_IS_OK(status)) {
3732 d_printf("%s getfacl file %s\n",
3733 nt_errstr(status), src);
3737 /* ToDo : Print out the ACL values. */
3738 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3739 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3740 src, (unsigned int)CVAL(retbuf,0) );
3744 num_file_acls = SVAL(retbuf,2);
3745 num_dir_acls = SVAL(retbuf,4);
3748 * No overflow check, num_*_acls comes from a 16-bit value,
3749 * and we expect expected_buflen (size_t) to be of at least 32
3752 expected_buflen = SMB_POSIX_ACL_HEADER_SIZE +
3753 SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls);
3755 if (rb_size != expected_buflen) {
3756 d_printf("getfacl file %s, incorrect POSIX acl buffer size "
3757 "(should be %zu, was %zu).\n",
3764 d_printf("# file: %s\n", src);
3765 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3767 if (num_file_acls == 0 && num_dir_acls == 0) {
3768 d_printf("No acls found.\n");
3771 for (i = 0; i < num_file_acls; i++) {
3774 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3775 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3778 case SMB_POSIX_ACL_USER_OBJ:
3781 case SMB_POSIX_ACL_USER:
3782 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3783 d_printf("user:%u:", uorg);
3785 case SMB_POSIX_ACL_GROUP_OBJ:
3786 d_printf("group::");
3788 case SMB_POSIX_ACL_GROUP:
3789 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3790 d_printf("group:%u:", uorg);
3792 case SMB_POSIX_ACL_MASK:
3795 case SMB_POSIX_ACL_OTHER:
3796 d_printf("other::");
3799 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3800 src, (unsigned int)tagtype );
3805 d_printf("%s\n", perms_to_string(permstring, perms));
3808 for (i = 0; i < num_dir_acls; i++) {
3811 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3812 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3815 case SMB_POSIX_ACL_USER_OBJ:
3816 d_printf("default:user::");
3818 case SMB_POSIX_ACL_USER:
3819 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3820 d_printf("default:user:%u:", uorg);
3822 case SMB_POSIX_ACL_GROUP_OBJ:
3823 d_printf("default:group::");
3825 case SMB_POSIX_ACL_GROUP:
3826 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3827 d_printf("default:group:%u:", uorg);
3829 case SMB_POSIX_ACL_MASK:
3830 d_printf("default:mask::");
3832 case SMB_POSIX_ACL_OTHER:
3833 d_printf("default:other::");
3836 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3837 src, (unsigned int)tagtype );
3842 d_printf("%s\n", perms_to_string(permstring, perms));
3848 /****************************************************************************
3849 Get the EA list of a file
3850 ****************************************************************************/
3852 static int cmd_geteas(void)
3854 TALLOC_CTX *ctx = talloc_tos();
3857 char *targetname = NULL;
3858 struct cli_state *targetcli;
3861 struct ea_struct *eas;
3862 struct cli_credentials *creds =
3863 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3865 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3866 d_printf("geteas filename\n");
3869 src = talloc_asprintf(ctx,
3871 client_get_cur_dir(),
3876 src = client_clean_name(ctx, src);
3881 status = cli_resolve_path(ctx, "",
3883 cli, src, &targetcli, &targetname);
3884 if (!NT_STATUS_IS_OK(status)) {
3885 d_printf("stat %s: %s\n", src, nt_errstr(status));
3889 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3891 if (!NT_STATUS_IS_OK(status)) {
3892 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3896 for (i=0; i<num_eas; i++) {
3897 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3898 dump_data_file(eas[i].value.data, eas[i].value.length, false,
3908 /****************************************************************************
3910 ****************************************************************************/
3912 static int cmd_setea(void)
3914 TALLOC_CTX *ctx = talloc_tos();
3917 char *eaname = NULL;
3918 char *eavalue = NULL;
3919 char *targetname = NULL;
3920 struct cli_state *targetcli;
3921 struct cli_credentials *creds =
3922 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3925 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
3926 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
3927 d_printf("setea filename eaname value\n");
3930 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
3931 eavalue = talloc_strdup(ctx, "");
3933 src = talloc_asprintf(ctx,
3935 client_get_cur_dir(),
3940 src = client_clean_name(ctx, src);
3945 status = cli_resolve_path(ctx, "",
3947 cli, src, &targetcli, &targetname);
3948 if (!NT_STATUS_IS_OK(status)) {
3949 d_printf("stat %s: %s\n", src, nt_errstr(status));
3953 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
3955 if (!NT_STATUS_IS_OK(status)) {
3956 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
3963 /****************************************************************************
3965 ****************************************************************************/
3967 static int cmd_stat(void)
3969 TALLOC_CTX *ctx = talloc_tos();
3972 char *targetname = NULL;
3973 struct cli_state *targetcli;
3975 SMB_STRUCT_STAT sbuf;
3978 struct cli_credentials *creds =
3979 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
3982 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3983 d_printf("stat file\n");
3986 src = talloc_asprintf(ctx,
3988 client_get_cur_dir(),
3993 src = client_clean_name(ctx, src);
3998 status = cli_resolve_path(ctx, "",
4000 cli, src, &targetcli, &targetname);
4001 if (!NT_STATUS_IS_OK(status)) {
4002 d_printf("stat %s: %s\n", src, nt_errstr(status));
4006 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4007 d_printf("Server doesn't support UNIX CIFS calls.\n");
4011 status = cli_posix_stat(targetcli, targetname, &sbuf);
4012 if (!NT_STATUS_IS_OK(status)) {
4013 d_printf("%s stat file %s\n",
4014 nt_errstr(status), src);
4018 /* Print out the stat values. */
4019 d_printf("File: %s\n", src);
4020 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
4021 (double)sbuf.st_ex_size,
4022 (unsigned int)sbuf.st_ex_blocks,
4023 filetype_to_str(sbuf.st_ex_mode));
4025 #if defined(S_ISCHR) && defined(S_ISBLK)
4026 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
4027 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
4028 (double)sbuf.st_ex_ino,
4029 (unsigned int)sbuf.st_ex_nlink,
4030 unix_dev_major(sbuf.st_ex_rdev),
4031 unix_dev_minor(sbuf.st_ex_rdev));
4034 d_printf("Inode: %.0f\tLinks: %u\n",
4035 (double)sbuf.st_ex_ino,
4036 (unsigned int)sbuf.st_ex_nlink);
4038 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
4039 ((int)sbuf.st_ex_mode & 0777),
4040 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
4041 (unsigned int)sbuf.st_ex_uid,
4042 (unsigned int)sbuf.st_ex_gid);
4044 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
4045 lt = localtime(&tmp_time);
4047 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4049 fstrcpy(mode_str, "unknown");
4051 d_printf("Access: %s\n", mode_str);
4053 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4054 lt = localtime(&tmp_time);
4056 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4058 fstrcpy(mode_str, "unknown");
4060 d_printf("Modify: %s\n", mode_str);
4062 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
4063 lt = localtime(&tmp_time);
4065 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4067 fstrcpy(mode_str, "unknown");
4069 d_printf("Change: %s\n", mode_str);
4075 /****************************************************************************
4077 ****************************************************************************/
4079 static int cmd_chown(void)
4081 TALLOC_CTX *ctx = talloc_tos();
4085 char *buf, *buf2, *buf3;
4086 struct cli_state *targetcli;
4087 char *targetname = NULL;
4088 struct cli_credentials *creds =
4089 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
4092 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4093 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
4094 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
4095 d_printf("chown uid gid file\n");
4099 uid = (uid_t)atoi(buf);
4100 gid = (gid_t)atoi(buf2);
4102 src = talloc_asprintf(ctx,
4104 client_get_cur_dir(),
4109 src = client_clean_name(ctx, src);
4113 status = cli_resolve_path(ctx, "",
4115 cli, src, &targetcli, &targetname);
4116 if (!NT_STATUS_IS_OK(status)) {
4117 d_printf("chown %s: %s\n", src, nt_errstr(status));
4121 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4122 d_printf("Server doesn't support UNIX CIFS calls.\n");
4126 status = cli_posix_chown(targetcli, targetname, uid, gid);
4127 if (!NT_STATUS_IS_OK(status)) {
4128 d_printf("%s chown file %s uid=%d, gid=%d\n",
4129 nt_errstr(status), src, (int)uid, (int)gid);
4136 /****************************************************************************
4138 ****************************************************************************/
4140 static int cmd_rename(void)
4142 TALLOC_CTX *ctx = talloc_tos();
4145 struct cli_state *targetcli;
4148 struct cli_credentials *creds =
4149 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
4151 bool replace = false;
4153 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4154 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4155 d_printf("rename <src> <dest> [-f]\n");
4159 src = talloc_asprintf(ctx,
4161 client_get_cur_dir(),
4166 src = client_clean_name(ctx, src);
4171 dest = talloc_asprintf(ctx,
4173 client_get_cur_dir(),
4178 dest = client_clean_name(ctx, dest);
4183 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
4184 strcsequal(buf, "-f")) {
4188 status = cli_resolve_path(ctx, "",
4190 cli, src, &targetcli, &targetsrc);
4191 if (!NT_STATUS_IS_OK(status)) {
4192 d_printf("rename %s: %s\n", src, nt_errstr(status));
4196 status = cli_resolve_path(ctx, "",
4198 cli, dest, &targetcli, &targetdest);
4199 if (!NT_STATUS_IS_OK(status)) {
4200 d_printf("rename %s: %s\n", dest, nt_errstr(status));
4204 status = cli_rename(targetcli, targetsrc, targetdest, replace);
4205 if (!NT_STATUS_IS_OK(status)) {
4206 d_printf("%s renaming files %s -> %s \n",
4216 struct scopy_timing {
4217 struct timespec tp_start;
4220 static int scopy_status(off_t written, void *priv)
4222 struct timespec tp_end;
4223 unsigned int scopy_total_time_ms;
4224 struct scopy_timing *st = priv;
4226 clock_gettime_mono(&tp_end);
4227 scopy_total_time_ms = nsec_time_diff(&tp_end,&st->tp_start)/1000000;
4229 DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
4230 (intmax_t)written, written / (1.024*scopy_total_time_ms)));
4235 /****************************************************************************
4236 Server-Side copy some file.
4237 ****************************************************************************/
4239 static int cmd_scopy(void)
4241 TALLOC_CTX *ctx = talloc_tos();
4244 struct cli_state *targetcli;
4247 uint32_t DesiredAccess, ShareAccess, CreateDisposition, CreateOptions;
4248 struct smb_create_returns cr;
4249 uint16_t destfnum = (uint16_t)-1;
4250 uint16_t srcfnum = (uint16_t)-1;
4252 struct scopy_timing st;
4254 struct cli_credentials *creds =
4255 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
4258 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4259 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4260 d_printf("scopy <src> <dest>\n");
4264 src = talloc_asprintf(ctx,
4266 client_get_cur_dir(),
4271 src = client_clean_name(ctx, src);
4276 dest = talloc_asprintf(ctx,
4278 client_get_cur_dir(),
4283 dest = client_clean_name(ctx, dest);
4288 status = cli_resolve_path(ctx, "",
4290 cli, src, &targetcli, &targetsrc);
4291 if (!NT_STATUS_IS_OK(status)) {
4292 d_printf("scopy %s: %s\n", src, nt_errstr(status));
4296 status = cli_resolve_path(ctx, "",
4298 cli, dest, &targetcli, &targetdest);
4299 if (!NT_STATUS_IS_OK(status)) {
4300 d_printf("scopy %s: %s\n", dest, nt_errstr(status));
4305 DesiredAccess = (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES|
4306 READ_CONTROL_ACCESS|SYNCHRONIZE_ACCESS);
4307 ShareAccess = FILE_SHARE_READ|FILE_SHARE_DELETE;
4308 CreateDisposition = FILE_OPEN;
4309 CreateOptions = (FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE|
4310 FILE_OPEN_REPARSE_POINT);
4311 status = cli_ntcreate(targetcli, targetsrc, 0, DesiredAccess, 0,
4312 ShareAccess, CreateDisposition, CreateOptions, 0x0,
4314 if (!NT_STATUS_IS_OK(status)) {
4315 d_printf("Failed to open file %s. %s\n",
4316 targetsrc, nt_errstr(status));
4320 DesiredAccess = (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_READ_EA|
4321 FILE_WRITE_EA|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|
4322 DELETE_ACCESS|READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|SYNCHRONIZE_ACCESS);
4323 ShareAccess = FILE_SHARE_NONE;
4324 CreateDisposition = FILE_CREATE;
4325 CreateOptions = FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE;
4326 status = cli_ntcreate(targetcli, targetdest, 0, DesiredAccess,
4327 FILE_ATTRIBUTE_ARCHIVE, ShareAccess, CreateDisposition,
4328 CreateOptions, 0x0, &destfnum, NULL);
4329 if (!NT_STATUS_IS_OK(status)) {
4330 d_printf("Failed to create file %s. %s\n",
4331 targetdest, nt_errstr(status));
4332 cli_close(targetcli, srcfnum);
4336 clock_gettime_mono(&st.tp_start);
4337 status = cli_splice(targetcli, targetcli, srcfnum, destfnum,
4338 cr.end_of_file, 0, 0, &written, scopy_status, &st);
4339 if (!NT_STATUS_IS_OK(status)) {
4340 d_printf("%s copying file %s -> %s \n",
4347 status = cli_close(targetcli, srcfnum);
4348 if (!NT_STATUS_IS_OK(status)) {
4349 d_printf("Error %s closing remote source file\n", nt_errstr(status));
4352 status = cli_close(targetcli, destfnum);
4353 if (!NT_STATUS_IS_OK(status)) {
4354 d_printf("Error %s closing remote dest file\n", nt_errstr(status));
4361 /****************************************************************************
4362 Print the volume name.
4363 ****************************************************************************/
4365 static int cmd_volume(void)
4368 uint32_t serial_num;
4372 status = cli_get_fs_volume_info(cli, talloc_tos(),
4373 &volname, &serial_num,
4375 if (!NT_STATUS_IS_OK(status)) {
4376 d_printf("Error %s getting volume info\n", nt_errstr(status));
4380 d_printf("Volume: |%s| serial number 0x%x\n",
4381 volname, (unsigned int)serial_num);
4385 /****************************************************************************
4386 Hard link files using the NT call.
4387 ****************************************************************************/
4389 static int cmd_hardlink(void)
4391 TALLOC_CTX *ctx = talloc_tos();
4394 struct cli_state *targetcli;
4396 struct cli_credentials *creds =
4397 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
4400 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4401 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4402 d_printf("hardlink <src> <dest>\n");
4406 src = talloc_asprintf(ctx,
4408 client_get_cur_dir(),
4413 src = client_clean_name(ctx, src);
4418 dest = talloc_asprintf(ctx,
4420 client_get_cur_dir(),
4425 dest = client_clean_name(ctx, dest);
4430 status = cli_resolve_path(ctx, "",
4432 cli, src, &targetcli, &targetname);
4433 if (!NT_STATUS_IS_OK(status)) {
4434 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
4438 status = cli_hardlink(targetcli, targetname, dest);
4439 if (!NT_STATUS_IS_OK(status)) {
4440 d_printf("%s doing an NT hard link of files\n",
4448 /****************************************************************************
4449 Toggle the prompt flag.
4450 ****************************************************************************/
4452 static int cmd_prompt(void)
4455 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
4459 /****************************************************************************
4460 Set the newer than time.
4461 ****************************************************************************/
4463 static int cmd_newer(void)
4465 TALLOC_CTX *ctx = talloc_tos();
4468 SMB_STRUCT_STAT sbuf;
4470 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
4471 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
4472 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4473 DEBUG(1,("Getting files newer than %s",
4474 time_to_asc(newer_than)));
4479 if (ok && newer_than == 0) {
4480 d_printf("Error setting newer-than time\n");
4487 /****************************************************************************
4488 Watch directory changes
4489 ****************************************************************************/
4491 static int cmd_notify(void)
4493 TALLOC_CTX *frame = talloc_stackframe();
4498 name = talloc_strdup(talloc_tos(), client_get_cur_dir());
4502 if (!next_token_talloc(talloc_tos(), &cmd_ptr, &buf, NULL)) {
4505 name = talloc_asprintf_append(name, "%s", buf);
4509 name = client_clean_name(talloc_tos(), name);
4513 status = cli_ntcreate(
4514 cli, name, 0, FILE_READ_DATA, 0,
4515 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4516 FILE_OPEN, 0, 0, &fnum, NULL);
4517 if (!NT_STATUS_IS_OK(status)) {
4518 d_printf("Could not open file: %s\n", nt_errstr(status));
4524 uint32_t num_changes = 0;
4525 struct notify_change *changes = NULL;
4527 status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
4529 talloc_tos(), &num_changes, &changes);
4530 if (NT_STATUS_EQUAL(status, NT_STATUS_NOTIFY_ENUM_DIR)) {
4531 printf("NOTIFY_ENUM_DIR\n");
4532 status = NT_STATUS_OK;
4534 if (!NT_STATUS_IS_OK(status)) {
4535 d_printf("notify returned %s\n",
4539 for (i=0; i<num_changes; i++) {
4540 printf("%4.4x %s\n", changes[i].action,
4543 TALLOC_FREE(changes);
4546 d_printf("notify <dir name>\n");
4552 /****************************************************************************
4553 Set the archive level.
4554 ****************************************************************************/
4556 static int cmd_archive(void)
4558 TALLOC_CTX *ctx = talloc_tos();
4561 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4562 archive_level = atoi(buf);
4564 d_printf("Archive level is %d\n",archive_level);
4570 /****************************************************************************
4571 Toggle the backup_intent state.
4572 ****************************************************************************/
4574 static int cmd_backup(void)
4576 backup_intent = !backup_intent;
4577 cli_set_backup_intent(cli, backup_intent);
4578 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4582 /****************************************************************************
4583 Toggle the lowercaseflag.
4584 ****************************************************************************/
4586 static int cmd_lowercase(void)
4588 lowercase = !lowercase;
4589 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4593 /****************************************************************************
4594 Toggle the case sensitive flag.
4595 ****************************************************************************/
4597 static int cmd_setcase(void)
4599 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4601 cli_set_case_sensitive(cli, !orig_case_sensitive);
4602 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4607 /****************************************************************************
4608 Toggle the showacls flag.
4609 ****************************************************************************/
4611 static int cmd_showacls(void)
4613 showacls = !showacls;
4614 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4619 /****************************************************************************
4620 Toggle the recurse flag.
4621 ****************************************************************************/
4623 static int cmd_recurse(void)
4626 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4630 /****************************************************************************
4631 Toggle the translate flag.
4632 ****************************************************************************/
4634 static int cmd_translate(void)
4636 translation = !translation;
4637 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4638 translation?"on":"off"));
4642 /****************************************************************************
4644 ****************************************************************************/
4646 static int cmd_lcd(void)
4648 TALLOC_CTX *ctx = talloc_tos();
4652 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4653 if (chdir(buf) == -1) {
4654 d_printf("chdir to %s failed (%s)\n",
4655 buf, strerror(errno));
4662 DEBUG(2,("the local directory is now %s\n",d));
4667 /****************************************************************************
4668 Get a file restarting at end of local file.
4669 ****************************************************************************/
4671 static int cmd_reget(void)
4673 TALLOC_CTX *ctx = talloc_tos();
4674 char *local_name = NULL;
4675 char *remote_name = NULL;
4679 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4684 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4685 d_printf("reget <filename>\n");
4688 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4692 remote_name = client_clean_name(ctx,remote_name);
4698 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4703 return do_get(remote_name, local_name, true);
4706 /****************************************************************************
4707 Put a file restarting at end of local file.
4708 ****************************************************************************/
4710 static int cmd_reput(void)
4712 TALLOC_CTX *ctx = talloc_tos();
4713 char *local_name = NULL;
4714 char *remote_name = NULL;
4718 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4723 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4724 d_printf("reput <filename>\n");
4728 if (!file_exist_stat(local_name, &st, false)) {
4729 d_printf("%s does not exist\n", local_name);
4733 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4734 remote_name = talloc_asprintf_append(remote_name,
4737 remote_name = talloc_asprintf_append(remote_name,
4744 remote_name = client_clean_name(ctx, remote_name);
4749 return do_put(remote_name, local_name, true);
4752 /****************************************************************************
4754 ****************************************************************************/
4756 static void browse_fn(const char *name, uint32_t m,
4757 const char *comment, void *state)
4759 const char *typestr = "";
4762 case STYPE_DISKTREE:
4766 typestr = "Printer";
4775 /* FIXME: If the remote machine returns non-ascii characters
4776 in any of these fields, they can corrupt the output. We
4777 should remove them. */
4779 d_printf("\t%-15s %-10.10s%s\n",
4780 name,typestr,comment);
4782 d_printf ("%s|%s|%s\n",typestr,name,comment);
4786 static bool browse_host_rpc(bool sort)
4789 struct rpc_pipe_client *pipe_hnd = NULL;
4790 TALLOC_CTX *frame = talloc_stackframe();
4792 struct srvsvc_NetShareInfoCtr info_ctr;
4793 struct srvsvc_NetShareCtr1 ctr1;
4794 uint32_t resume_handle = 0;
4795 uint32_t total_entries = 0;
4797 struct dcerpc_binding_handle *b;
4799 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
4802 if (!NT_STATUS_IS_OK(status)) {
4803 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4804 nt_errstr(status)));
4809 b = pipe_hnd->binding_handle;
4811 ZERO_STRUCT(info_ctr);
4815 info_ctr.ctr.ctr1 = &ctr1;
4817 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4825 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4826 TALLOC_FREE(pipe_hnd);
4831 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4832 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4833 browse_fn(info.name, info.type, info.comment, NULL);
4836 TALLOC_FREE(pipe_hnd);
4841 /****************************************************************************
4842 Try and browse available connections on a host.
4843 ****************************************************************************/
4845 static bool browse_host(bool sort)
4850 d_printf("\n\tSharename Type Comment\n");
4851 d_printf("\t--------- ---- -------\n");
4854 if (browse_host_rpc(sort)) {
4858 if (smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1) {
4862 ret = cli_RNetShareEnum(cli, browse_fn, NULL);
4864 NTSTATUS status = cli_nt_error(cli);
4865 d_printf("Error returning browse list: %s\n",
4872 /****************************************************************************
4874 ****************************************************************************/
4876 static void server_fn(const char *name, uint32_t m,
4877 const char *comment, void *state)
4881 d_printf("\t%-16s %s\n", name, comment);
4883 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4887 /****************************************************************************
4888 Try and browse available connections on a host.
4889 ****************************************************************************/
4891 static bool list_servers(const char *wk_grp)
4895 if (!cli->server_domain)
4899 d_printf("\n\tServer Comment\n");
4900 d_printf("\t--------- -------\n");
4902 fstrcpy( state, "Server" );
4903 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4907 d_printf("\n\tWorkgroup Master\n");
4908 d_printf("\t--------- -------\n");
4911 fstrcpy( state, "Workgroup" );
4912 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
4917 /****************************************************************************
4918 Print or set current VUID
4919 ****************************************************************************/
4921 static int cmd_vuid(void)
4923 TALLOC_CTX *ctx = talloc_tos();
4926 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4927 d_printf("Current VUID is %d\n",
4928 cli_state_get_uid(cli));
4932 cli_state_set_uid(cli, atoi(buf));
4936 /****************************************************************************
4937 Setup a new VUID, by issuing a session setup
4938 ****************************************************************************/
4940 static int cmd_logon(void)
4942 TALLOC_CTX *ctx = talloc_tos();
4943 char *l_username, *l_password;
4944 struct cli_credentials *creds = NULL;
4947 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
4948 d_printf("logon <username> [<password>]\n");
4952 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
4953 char pwd[256] = {0};
4956 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
4958 l_password = talloc_strdup(ctx, pwd);
4965 creds = cli_session_creds_init(ctx,
4970 false, /* use_kerberos */
4971 false, /* fallback_after_kerberos */
4972 false, /* use_ccache */
4973 false); /* password_is_nt_hash */
4974 if (creds == NULL) {
4975 d_printf("cli_session_creds_init() failed.\n");
4978 nt_status = cli_session_setup_creds(cli, creds);
4980 if (!NT_STATUS_IS_OK(nt_status)) {
4981 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
4985 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
4993 static int cmd_logoff(void)
4997 status = cli_ulogoff(cli);
4998 if (!NT_STATUS_IS_OK(status)) {
4999 d_printf("logoff failed: %s\n", nt_errstr(status));
5003 d_printf("logoff successful\n");
5009 * tree connect (connect to a share)
5012 static int cmd_tcon(void)
5014 TALLOC_CTX *ctx = talloc_tos();
5018 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
5019 d_printf("tcon <sharename>\n");
5027 status = cli_tree_connect(cli, sharename, "?????", NULL);
5028 if (!NT_STATUS_IS_OK(status)) {
5029 d_printf("tcon failed: %s\n", nt_errstr(status));
5033 talloc_free(sharename);
5035 d_printf("tcon to %s successful, tid: %u\n", sharename,
5036 cli_state_get_tid(cli));
5041 * tree disconnect (disconnect from a share)
5044 static int cmd_tdis(void)
5048 status = cli_tdis(cli);
5049 if (!NT_STATUS_IS_OK(status)) {
5050 d_printf("tdis failed: %s\n", nt_errstr(status));
5054 d_printf("tdis successful\n");
5063 static int cmd_tid(void)
5065 TALLOC_CTX *ctx = talloc_tos();
5068 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
5069 if (cli_state_has_tcon(cli)) {
5070 d_printf("current tid is %d\n", cli_state_get_tid(cli));
5072 d_printf("no tcon currently\n");
5075 uint32_t tid = atoi(tid_str);
5076 if (!cli_state_has_tcon(cli)) {
5077 d_printf("no tcon currently\n");
5079 cli_state_set_tid(cli, tid);
5086 /****************************************************************************
5087 list active connections
5088 ****************************************************************************/
5090 static int cmd_list_connect(void)
5092 cli_cm_display(cli);
5096 /****************************************************************************
5097 display the current active client connection
5098 ****************************************************************************/
5100 static int cmd_show_connect( void )
5102 TALLOC_CTX *ctx = talloc_tos();
5103 struct cli_state *targetcli;
5105 struct cli_credentials *creds =
5106 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
5109 status = cli_resolve_path(ctx, "",
5112 client_get_cur_dir(), &targetcli,
5114 if (!NT_STATUS_IS_OK(status)) {
5115 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
5119 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli->conn), targetcli->share);
5124 * cmd_utimes - interactive command to set the four times
5126 * Read a filename and four times from the client command line and update
5127 * the file times. A value of -1 for a time means don't change.
5129 static int cmd_utimes(void)
5133 struct timespec times[4] = {{0}};
5134 struct timeval_buf tbuf[4];
5138 TALLOC_CTX *ctx = talloc_new(NULL);
5145 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5147 d_printf("utimes <filename> <create-time> <access-time> "
5148 "<write-time> <change-time>\n");
5149 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5150 "or -1 for no change\n");
5155 fname = talloc_asprintf(ctx,
5157 client_get_cur_dir(),
5159 if (fname == NULL) {
5163 fname = client_clean_name(ctx, fname);
5164 if (fname == NULL) {
5169 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
5171 const char *s = buf;
5172 struct tm tm = {0,};
5176 if (strlen(s) == 2 && strcmp(s, "-1") == 0) {
5177 times[time_count] = make_omit_timespec();
5182 ret = strptime(s, "%y:%m:%d-%H:%M:%S", &tm);
5185 ret = strptime(s, "%Y:%m:%d-%H:%M:%S", &tm);
5188 /* We could not match all the chars, so print error */
5189 if (ret == NULL || *ret != 0) {
5190 d_printf("Invalid date format: %s\n", s);
5191 d_printf("utimes <filename> <create-time> "
5192 "<access-time> <write-time> <change-time>\n");
5193 d_printf("Dates should be in [YY]YY:MM:DD-HH:MM:SS "
5194 "format or -1 for no change\n");
5199 /* Convert tm to a time_t */
5201 times[time_count] = (struct timespec){.tv_sec = t};
5205 if (time_count < 4) {
5206 d_printf("Insufficient dates: %d\n", time_count);
5207 d_printf("utimes <filename> <create-time> <access-time> "
5208 "<write-time> <change-time>\n");
5209 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5210 "or -1 for no change\n");
5215 DEBUG(10, ("times\nCreate: %sAccess: %s Write: %sChange: %s\n",
5216 timespec_string_buf(×[0], false, &tbuf[0]),
5217 timespec_string_buf(×[1], false, &tbuf[1]),
5218 timespec_string_buf(×[2], false, &tbuf[2]),
5219 timespec_string_buf(×[3], false, &tbuf[3])));
5221 status = cli_setpathinfo_ext(
5222 cli, fname, times[0], times[1], times[2], times[3],
5224 if (!NT_STATUS_IS_OK(status)) {
5225 d_printf("cli_setpathinfo_ext failed: %s\n",
5236 * set_remote_attr - set DOS attributes of a remote file
5237 * @filename: path to the file name
5238 * @new_attr: attribute bit mask to use
5239 * @mode: one of ATTR_SET or ATTR_UNSET
5241 * Update the file attributes with the one provided.
5243 int set_remote_attr(const char *filename, uint32_t new_attr, int mode)
5245 extern struct cli_state *cli;
5249 status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
5250 if (!NT_STATUS_IS_OK(status)) {
5251 d_printf("cli_getatr failed: %s\n", nt_errstr(status));
5255 if (mode == ATTR_SET) {
5256 new_attr |= old_attr;
5258 new_attr = old_attr & ~new_attr;
5261 status = cli_setatr(cli, filename, new_attr, 0);
5262 if (!NT_STATUS_IS_OK(status)) {
5263 d_printf("cli_setatr failed: %s\n", nt_errstr(status));
5271 * cmd_setmode - interactive command to set DOS attributes
5273 * Read a filename and mode from the client command line and update
5274 * the file DOS attributes.
5276 int cmd_setmode(void)
5280 uint32_t attr[2] = {0};
5281 int mode = ATTR_SET;
5284 TALLOC_CTX *ctx = talloc_new(NULL);
5289 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5291 d_printf("setmode <filename> <[+|-]rsha>\n");
5296 fname = talloc_asprintf(ctx,
5298 client_get_cur_dir(),
5300 if (fname == NULL) {
5304 fname = client_clean_name(ctx, fname);
5305 if (fname == NULL) {
5310 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
5311 const char *s = buf;
5322 attr[mode] |= FILE_ATTRIBUTE_READONLY;
5325 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
5328 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
5331 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
5334 d_printf("setmode <filename> <perm=[+|-]rsha>\n");
5341 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
5342 d_printf("setmode <filename> <[+|-]rsha>\n");
5347 DEBUG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
5349 /* ignore return value: server might not store DOS attributes */
5350 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
5351 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
5357 /****************************************************************************
5359 ***************************************************************************/
5361 int cmd_iosize(void)
5363 TALLOC_CTX *ctx = talloc_tos();
5367 get_cmdline_auth_info_smb_encrypt(
5368 popt_get_cmdline_auth_info());
5370 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5371 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5373 d_printf("iosize <n> or iosize 0x<n>. "
5374 "Minimum is 0 (default), "
5375 "max is 16776960 (0xFFFF00)\n");
5377 d_printf("iosize <n> or iosize 0x<n>. "
5378 "(Encrypted connection) ,"
5379 "Minimum is 0 (default), "
5380 "max is 130048 (0x1FC00)\n");
5383 d_printf("iosize <n> or iosize 0x<n>.\n");
5388 iosize = strtol(buf,NULL,0);
5389 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5390 if (smb_encrypt && (iosize < 0 || iosize > 0xFC00)) {
5391 d_printf("iosize out of range for encrypted "
5392 "connection (min = 0 (default), "
5393 "max = 130048 (0x1FC00)\n");
5395 } else if (!smb_encrypt && (iosize < 0 || iosize > 0xFFFF00)) {
5396 d_printf("iosize out of range (min = 0 (default), "
5397 "max = 16776960 (0xFFFF00)\n");
5402 io_bufsize = iosize;
5403 d_printf("iosize is now %d\n", io_bufsize);
5407 /****************************************************************************
5409 ***************************************************************************/
5411 static int cmd_timeout(void)
5413 TALLOC_CTX *ctx = talloc_tos();
5416 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5417 unsigned int old_timeout = cli_set_timeout(cli, 0);
5418 cli_set_timeout(cli, old_timeout);
5419 d_printf("timeout <n> (per-operation timeout "
5420 "in seconds - currently %u).\n",
5425 io_timeout = strtol(buf,NULL,0);
5426 cli_set_timeout(cli, io_timeout*1000);
5427 d_printf("io_timeout per operation is now %d\n", io_timeout);
5432 /****************************************************************************
5434 ****************************************************************************/
5435 static int cmd_history(void)
5437 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
5441 hlist = history_list();
5443 for (i = 0; hlist && hlist[i]; i++) {
5444 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
5447 DEBUG(0,("no history without readline support\n"));
5453 /* Some constants for completing filename arguments */
5455 #define COMPL_NONE 0 /* No completions */
5456 #define COMPL_REMOTE 1 /* Complete remote filename */
5457 #define COMPL_LOCAL 2 /* Complete local filename */
5459 /* This defines the commands supported by this client.
5460 * NOTE: The "!" must be the last one in the list because it's fn pointer
5461 * field is NULL, and NULL in that field is used in process_tok()
5462 * (below) to indicate the end of the list. crh
5467 const char *description;
5468 char compl_args[2]; /* Completion argument info */
5470 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5471 {"allinfo",cmd_allinfo,"<file> show all available info",
5472 {COMPL_REMOTE,COMPL_NONE}},
5473 {"altname",cmd_altname,"<file> show alt name",{COMPL_REMOTE,COMPL_NONE}},
5474 {"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}},
5475 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
5476 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
5477 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
5478 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
5479 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
5480 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_NONE}},
5481 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_NONE}},
5482 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_NONE}},
5483 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5484 {"deltree",cmd_deltree,"<mask> recursively delete all matching files and directories",{COMPL_REMOTE,COMPL_NONE}},
5485 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5486 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5487 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
5488 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5489 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
5490 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_NONE}},
5491 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
5492 {COMPL_REMOTE, COMPL_NONE}},
5493 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5494 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5495 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
5496 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
5497 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
5498 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5499 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5500 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
5501 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5502 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5503 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
5504 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5505 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
5506 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5507 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
5508 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
5509 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
5510 {"notify",cmd_notify,"<file>Get notified of dir changes",{COMPL_REMOTE,COMPL_NONE}},
5511 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
5512 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
5513 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
5514 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5515 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5516 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5517 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5518 {"posix_whoami",cmd_posix_whoami,"return logged on user information "
5519 "using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5520 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
5521 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
5522 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
5523 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
5524 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5525 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
5526 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5527 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5528 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
5529 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
5530 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
5531 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
5532 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
5533 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5534 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_REMOTE,COMPL_NONE}},
5535 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
5536 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
5537 {COMPL_REMOTE, COMPL_LOCAL}},
5538 {"setmode",cmd_setmode,"<file name> <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
5539 {"scopy",cmd_scopy,"<src> <dest> server-side copy file",{COMPL_REMOTE,COMPL_REMOTE}},
5540 {"stat",cmd_stat,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_NONE}},
5541 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5542 {"tar",cmd_tar,"tar <c|x>[IXFvbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
5543 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
5544 {"timeout",cmd_timeout,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE,COMPL_NONE}},
5545 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
5546 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5547 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
5548 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
5549 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5550 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
5551 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
5552 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
5553 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
5554 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
5555 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
5556 {"utimes", cmd_utimes,"<file name> <create_time> <access_time> <mod_time> "
5557 "<ctime> set times", {COMPL_REMOTE,COMPL_NONE}},
5558 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
5559 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
5561 /* Yes, this must be here, see crh's comment above. */
5562 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
5563 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
5566 /*******************************************************************
5567 Lookup a command string in the list of commands, including
5569 ******************************************************************/
5571 static int process_tok(char *tok)
5573 size_t i = 0, matches = 0;
5575 size_t tok_len = strlen(tok);
5577 while (commands[i].fn != NULL) {
5578 if (strequal(commands[i].name,tok)) {
5582 } else if (strnequal(commands[i].name, tok, tok_len)) {
5591 else if (matches == 1)
5597 /****************************************************************************
5599 ****************************************************************************/
5601 static int cmd_help(void)
5603 TALLOC_CTX *ctx = talloc_tos();
5607 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5608 if ((i = process_tok(buf)) >= 0)
5609 d_printf("HELP %s:\n\t%s\n\n",
5610 commands[i].name,commands[i].description);
5612 while (commands[i].description) {
5613 for (j=0; commands[i].description && (j<5); j++) {
5614 d_printf("%-15s",commands[i].name);
5623 /****************************************************************************
5624 Process a -c command string.
5625 ****************************************************************************/
5627 static int process_command_string(const char *cmd_in)
5629 TALLOC_CTX *ctx = talloc_tos();
5630 char *cmd = talloc_strdup(ctx, cmd_in);
5632 struct cli_credentials *creds =
5633 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
5638 /* establish the connection if not already */
5643 status = cli_cm_open(talloc_tos(), NULL,
5647 have_ip ? &dest_ss : NULL, port,
5650 if (!NT_STATUS_IS_OK(status)) {
5653 cli_set_timeout(cli, io_timeout*1000);
5656 while (cmd[0] != '\0') {
5662 if ((p = strchr_m(cmd, ';')) == 0) {
5671 /* and get the first part of the command */
5673 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
5677 if ((i = process_tok(tok)) >= 0) {
5678 rc = commands[i].fn();
5679 } else if (i == -2) {
5680 d_printf("%s: command abbreviation ambiguous\n",tok);
5682 d_printf("%s: command not found\n",tok);
5689 #define MAX_COMPLETIONS 100
5691 struct completion_remote {
5699 static NTSTATUS completion_remote_filter(const char *mnt,
5700 struct file_info *f,
5704 struct completion_remote *info = (struct completion_remote *)state;
5706 if (info->count >= MAX_COMPLETIONS - 1) {
5707 return NT_STATUS_OK;
5709 if (strncmp(info->text, f->name, info->len) != 0) {
5710 return NT_STATUS_OK;
5712 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
5713 return NT_STATUS_OK;
5716 if ((info->dirmask[0] == 0) && !(f->attr & FILE_ATTRIBUTE_DIRECTORY))
5717 info->matches[info->count] = SMB_STRDUP(f->name);
5719 TALLOC_CTX *ctx = talloc_stackframe();
5722 tmp = talloc_strdup(ctx,info->dirmask);
5725 return NT_STATUS_NO_MEMORY;
5727 tmp = talloc_asprintf_append(tmp, "%s", f->name);
5730 return NT_STATUS_NO_MEMORY;
5732 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5733 tmp = talloc_asprintf_append(tmp, "%s",
5738 return NT_STATUS_NO_MEMORY;
5740 info->matches[info->count] = SMB_STRDUP(tmp);
5743 if (info->matches[info->count] == NULL) {
5744 return NT_STATUS_OK;
5746 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5747 smb_readline_ca_char(0);
5749 if (info->count == 1) {
5750 info->samelen = strlen(info->matches[info->count]);
5752 while (strncmp(info->matches[info->count],
5753 info->matches[info->count-1],
5754 info->samelen) != 0) {
5759 return NT_STATUS_OK;
5762 static char **remote_completion(const char *text, int len)
5764 TALLOC_CTX *ctx = talloc_stackframe();
5765 char *dirmask = NULL;
5766 char *targetpath = NULL;
5767 struct cli_state *targetcli = NULL;
5769 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
5770 struct cli_credentials *creds =
5771 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
5774 /* can't have non-static initialisation on Sun CC, so do it
5780 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
5781 if (!info.matches) {
5787 * We're leaving matches[0] free to fill it later with the text to
5788 * display: Either the one single match or the longest common subset
5791 info.matches[0] = NULL;
5794 for (i = len-1; i >= 0; i--) {
5795 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
5800 info.text = text+i+1;
5801 info.samelen = info.len = len-i-1;
5804 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
5805 if (!info.dirmask) {
5808 strncpy(info.dirmask, text, i+1);
5809 info.dirmask[i+1] = 0;
5810 dirmask = talloc_asprintf(ctx,
5812 client_get_cur_dir(),
5816 info.dirmask = SMB_STRDUP("");
5817 if (!info.dirmask) {
5820 dirmask = talloc_asprintf(ctx,
5822 client_get_cur_dir());
5827 dirmask = client_clean_name(ctx, dirmask);
5828 if (dirmask == NULL) {
5832 status = cli_resolve_path(ctx, "",
5834 cli, dirmask, &targetcli, &targetpath);
5835 if (!NT_STATUS_IS_OK(status)) {
5838 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
5839 completion_remote_filter, (void *)&info);
5840 if (!NT_STATUS_IS_OK(status)) {
5844 if (info.count == 1) {
5846 * No matches at all, NULL indicates there is nothing
5848 SAFE_FREE(info.matches[0]);
5849 SAFE_FREE(info.matches);
5854 if (info.count == 2) {
5856 * Exactly one match in matches[1], indicate this is the one
5859 info.matches[0] = info.matches[1];
5860 info.matches[1] = NULL;
5863 return info.matches;
5867 * We got more than one possible match, set the result to the maximum
5871 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
5872 info.matches[info.count] = NULL;
5874 return info.matches;
5877 for (i = 0; i < info.count; i++) {
5878 SAFE_FREE(info.matches[i]);
5880 SAFE_FREE(info.matches);
5881 SAFE_FREE(info.dirmask);
5886 static char **completion_fn(const char *text, int start, int end)
5888 smb_readline_ca_char(' ');
5891 const char *buf, *sp;
5895 buf = smb_readline_get_line_buffer();
5899 sp = strchr(buf, ' ');
5903 for (i = 0; commands[i].name; i++) {
5904 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
5905 (commands[i].name[sp - buf] == 0)) {
5909 if (commands[i].name == NULL)
5915 if (sp == (buf + start))
5916 compl_type = commands[i].compl_args[0];
5918 compl_type = commands[i].compl_args[1];
5920 if (compl_type == COMPL_REMOTE)
5921 return remote_completion(text, end - start);
5922 else /* fall back to local filename completion */
5926 size_t i, len, samelen = 0, count=1;
5928 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
5935 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
5936 if (strncmp(text, commands[i].name, len) == 0) {
5937 matches[count] = SMB_STRDUP(commands[i].name);
5938 if (!matches[count])
5941 samelen = strlen(matches[count]);
5943 while (strncmp(matches[count], matches[count-1], samelen) != 0)
5950 case 0: /* should never happen */
5954 matches[0] = SMB_STRDUP(matches[1]);
5957 matches[0] = (char *)SMB_MALLOC(samelen+1);
5960 strncpy(matches[0], matches[1], samelen);
5961 matches[0][samelen] = 0;
5963 matches[count] = NULL;
5967 for (i = 0; i < count; i++)
5975 static bool finished;
5977 /****************************************************************************
5978 Make sure we swallow keepalives during idle time.
5979 ****************************************************************************/
5981 static void readline_callback(void)
5983 static time_t last_t;
5984 struct timespec now;
5987 unsigned char garbage[16];
5989 clock_gettime_mono(&now);
5997 /* Ping the server to keep the connection alive using SMBecho. */
5998 memset(garbage, 0xf0, sizeof(garbage));
5999 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
6000 if (NT_STATUS_IS_OK(status) ||
6001 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
6003 * Even if server returns NT_STATUS_INVALID_PARAMETER
6004 * it still responded.
6005 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
6010 if (!cli_state_is_connected(cli)) {
6011 DEBUG(0,("SMBecho failed (%s). The connection is "
6012 "disconnected now\n", nt_errstr(status)));
6014 smb_readline_done();
6018 /****************************************************************************
6019 Process commands on stdin.
6020 ****************************************************************************/
6022 static int process_stdin(void)
6027 d_printf("Try \"help\" to get a list of possible commands.\n");
6031 TALLOC_CTX *frame = talloc_stackframe();
6033 char *the_prompt = NULL;
6037 /* display a prompt */
6038 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
6042 line = smb_readline(the_prompt, readline_callback, completion_fn);
6043 SAFE_FREE(the_prompt);
6049 /* special case - first char is ! */
6051 if (system(line + 1) == -1) {
6052 d_printf("system() command %s failed.\n",
6060 /* and get the first part of the command */
6062 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
6068 if ((i = process_tok(tok)) >= 0) {
6069 rc = commands[i].fn();
6070 } else if (i == -2) {
6071 d_printf("%s: command abbreviation ambiguous\n",tok);
6073 d_printf("%s: command not found\n",tok);
6081 /****************************************************************************
6082 Process commands from the client.
6083 ****************************************************************************/
6085 static int process(const char *base_directory)
6089 struct cli_credentials *creds =
6090 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
6092 status = cli_cm_open(talloc_tos(), NULL,
6096 have_ip ? &dest_ss : NULL, port,
6098 if (!NT_STATUS_IS_OK(status)) {
6102 cli_set_timeout(cli, io_timeout*1000);
6104 if (base_directory && *base_directory) {
6105 rc = do_cd(base_directory);
6113 rc = process_command_string(cmdstr);
6122 /****************************************************************************
6124 ****************************************************************************/
6126 static int do_host_query(const char *query_host)
6129 struct cli_credentials *creds =
6130 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
6132 status = cli_cm_open(talloc_tos(), NULL,
6136 have_ip ? &dest_ss : NULL, port,
6138 if (!NT_STATUS_IS_OK(status)) {
6142 cli_set_timeout(cli, io_timeout*1000);
6145 /* Ensure that the host can do IPv4 */
6147 if (!interpret_addr(query_host)) {
6148 struct sockaddr_storage ss;
6149 if (interpret_string_addr(&ss, query_host, 0) &&
6150 (ss.ss_family != AF_INET)) {
6151 d_printf("%s is an IPv6 address -- no workgroup available\n",
6157 if (lp_client_min_protocol() > PROTOCOL_NT1) {
6158 d_printf("SMB1 disabled -- no workgroup available\n");
6162 if (lp_disable_netbios()) {
6163 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
6167 if (port != NBT_SMB_PORT ||
6168 smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1)
6171 * Workgroups simply don't make sense over anything
6172 * else but port 139 and SMB1.
6176 d_printf("Reconnecting with SMB1 for workgroup listing.\n");
6177 status = cli_cm_open(talloc_tos(), NULL,
6181 have_ip ? &dest_ss : NULL, NBT_SMB_PORT,
6183 if (!NT_STATUS_IS_OK(status)) {
6184 d_printf("Unable to connect with SMB1 "
6185 "-- no workgroup available\n");
6190 cli_set_timeout(cli, io_timeout*1000);
6191 list_servers(lp_workgroup());
6198 /****************************************************************************
6199 Handle a tar operation.
6200 ****************************************************************************/
6202 static int do_tar_op(const char *base_directory)
6204 struct tar *tar_ctx = tar_get_ctx();
6206 struct cli_credentials *creds =
6207 get_cmdline_auth_info_creds(popt_get_cmdline_auth_info());
6209 /* do we already have a connection? */
6213 status = cli_cm_open(talloc_tos(), NULL,
6217 have_ip ? &dest_ss : NULL, port,
6219 if (!NT_STATUS_IS_OK(status)) {
6223 cli_set_timeout(cli, io_timeout*1000);
6228 if (base_directory && *base_directory) {
6229 ret = do_cd(base_directory);
6235 ret = tar_process(tar_ctx);
6243 /****************************************************************************
6244 Handle a message operation.
6245 ****************************************************************************/
6247 static int do_message_op(struct user_auth_info *a_info)
6251 if (lp_disable_netbios()) {
6252 d_printf("NetBIOS over TCP disabled.\n");
6256 status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
6257 port ? port : NBT_SMB_PORT, name_type,
6262 if (!NT_STATUS_IS_OK(status)) {
6263 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
6267 cli_set_timeout(cli, io_timeout*1000);
6268 send_message(get_cmdline_auth_info_username(a_info));
6274 /****************************************************************************
6276 ****************************************************************************/
6278 int main(int argc,char *argv[])
6280 const char **const_argv = discard_const_p(const char *, argv);
6281 char *base_directory = NULL;
6283 char *query_host = NULL;
6284 bool message = false;
6285 static const char *new_name_resolve_order = NULL;
6289 bool tar_opt = false;
6290 bool service_opt = false;
6291 struct tar *tar_ctx = tar_get_ctx();
6293 struct poptOption long_options[] = {
6297 .longName = "name-resolve",
6299 .argInfo = POPT_ARG_STRING,
6300 .arg = &new_name_resolve_order,
6302 .descrip = "Use these name resolution services only",
6303 .argDescrip = "NAME-RESOLVE-ORDER",
6306 .longName = "message",
6308 .argInfo = POPT_ARG_STRING,
6311 .descrip = "Send message",
6312 .argDescrip = "HOST",
6315 .longName = "ip-address",
6317 .argInfo = POPT_ARG_STRING,
6320 .descrip = "Use this IP to connect to",
6324 .longName = "stderr",
6326 .argInfo = POPT_ARG_NONE,
6329 .descrip = "Write messages to stderr instead of stdout",
6334 .argInfo = POPT_ARG_STRING,
6337 .descrip = "Get a list of shares available on a host",
6338 .argDescrip = "HOST",
6341 .longName = "max-protocol",
6343 .argInfo = POPT_ARG_STRING,
6346 .descrip = "Set the max protocol level",
6347 .argDescrip = "LEVEL",
6352 .argInfo = POPT_ARG_STRING,
6355 .descrip = "Command line tar",
6356 .argDescrip = "<c|x>IXFvgbNan",
6359 .longName = "directory",
6361 .argInfo = POPT_ARG_STRING,
6364 .descrip = "Start from directory",
6365 .argDescrip = "DIR",
6368 .longName = "command",
6370 .argInfo = POPT_ARG_STRING,
6373 .descrip = "Execute semicolon separated commands",
6376 .longName = "send-buffer",
6378 .argInfo = POPT_ARG_INT,
6381 .descrip = "Changes the transmit/send buffer",
6382 .argDescrip = "BYTES",
6385 .longName = "timeout",
6387 .argInfo = POPT_ARG_INT,
6390 .descrip = "Changes the per-operation timeout",
6391 .argDescrip = "SECONDS",
6396 .argInfo = POPT_ARG_INT,
6399 .descrip = "Port to connect to",
6400 .argDescrip = "PORT",
6403 .longName = "grepable",
6405 .argInfo = POPT_ARG_NONE,
6408 .descrip = "Produce grepable output",
6411 .longName = "quiet",
6413 .argInfo = POPT_ARG_NONE,
6416 .descrip = "Suppress help message",
6419 .longName = "browse",
6421 .argInfo = POPT_ARG_NONE,
6424 .descrip = "Browse SMB servers using DNS",
6427 POPT_COMMON_CONNECTION
6428 POPT_COMMON_CREDENTIALS
6431 TALLOC_CTX *frame = talloc_stackframe();
6433 if (!client_set_cur_dir("\\")) {
6437 /* set default debug level to 1 regardless of what smb.conf sets */
6438 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
6441 lp_set_cmdline("log level", "1");
6443 popt_common_credentials_set_ignore_missing_conf();
6444 popt_common_credentials_set_delay_post();
6447 pc = poptGetContext("smbclient", argc, const_argv, long_options, 0);
6448 poptSetOtherOptionHelp(pc, "service <password>");
6450 while ((opt = poptGetNextOpt(pc)) != -1) {
6453 * if the tar option has been called previously, now
6454 * we need to eat out the leftovers
6456 /* I see no other way to keep things sane --SSS */
6457 if (tar_opt == true) {
6458 while (poptPeekArg(pc)) {
6464 /* if the service has not yet been specified lets see if it is available in the popt stack */
6465 if (!service_opt && poptPeekArg(pc)) {
6466 service = talloc_strdup(frame, poptGetArg(pc));
6473 /* if the service has already been retrieved then check if we have also a password */
6475 && (!get_cmdline_auth_info_got_pass(
6476 popt_get_cmdline_auth_info()))
6477 && poptPeekArg(pc)) {
6478 set_cmdline_auth_info_password(
6479 popt_get_cmdline_auth_info(), poptGetArg(pc));
6485 /* Messages are sent to NetBIOS name type 0x3
6486 * (Messenger Service). Make sure we default
6487 * to port 139 instead of port 445. srl,crh
6490 desthost = talloc_strdup(frame,poptGetOptArg(pc));
6495 port = NBT_SMB_PORT;
6500 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
6504 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
6508 setup_logging("smbclient", DEBUG_STDERR );
6509 display_set_stderr();
6513 query_host = talloc_strdup(frame, poptGetOptArg(pc));
6519 lp_set_cmdline("client max protocol", poptGetOptArg(pc));
6522 /* We must use old option processing for this. Find the
6523 * position of the -T option in the raw argv[]. */
6527 for (i = 1; i < argc; i++) {
6528 if (strncmp("-T", argv[i],2)==0)
6532 if (tar_parse_args(tar_ctx, poptGetOptArg(pc),
6533 const_argv + i, argc - i)) {
6534 poptPrintUsage(pc, stderr, 0);
6538 /* this must be the last option, mark we have parsed it so that we know we have */
6542 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
6543 if (!base_directory) {
6554 return(do_smb_browse());
6559 /* We may still have some leftovers after the last popt option has been called */
6560 if (tar_opt == true) {
6561 while (poptPeekArg(pc)) {
6567 /* if the service has not yet been specified lets see if it is available in the popt stack */
6568 if (!service_opt && poptPeekArg(pc)) {
6569 service = talloc_strdup(frame,poptGetArg(pc));
6576 /* if the service has already been retrieved then check if we have also a password */
6578 && !get_cmdline_auth_info_got_pass(popt_get_cmdline_auth_info())
6579 && poptPeekArg(pc)) {
6580 set_cmdline_auth_info_password(popt_get_cmdline_auth_info(),
6584 if (service_opt && service) {
6587 /* Convert any '/' characters in the service name to '\' characters */
6588 string_replace(service, '/','\\');
6589 if (count_chars(service,'\\') < 3) {
6590 d_printf("\n%s: Not enough '\\' characters in service\n",service);
6591 poptPrintUsage(pc, stderr, 0);
6594 /* Remove trailing slashes */
6595 len = strlen(service);
6596 while(len > 0 && service[len - 1] == '\\') {
6598 service[len] = '\0';
6602 if (!init_names()) {
6603 fprintf(stderr, "init_names() failed\n");
6607 if(new_name_resolve_order)
6608 lp_set_cmdline("name resolve order", new_name_resolve_order);
6610 if (!tar_to_process(tar_ctx) && !query_host && !service && !message) {
6611 poptPrintUsage(pc, stderr, 0);
6615 poptFreeContext(pc);
6616 popt_burn_cmdline_password(argc, argv);
6618 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
6620 /* Ensure we have a password (or equivalent). */
6621 popt_common_credentials_post();
6623 if (tar_to_process(tar_ctx)) {
6625 process_command_string(cmdstr);
6626 rc = do_tar_op(base_directory);
6627 } else if (query_host && *query_host) {
6628 char *qhost = query_host;
6631 while (*qhost == '\\' || *qhost == '/')
6634 if ((slash = strchr_m(qhost, '/'))
6635 || (slash = strchr_m(qhost, '\\'))) {
6639 if ((p=strchr_m(qhost, '#'))) {
6642 sscanf(p, "%x", &name_type);
6645 rc = do_host_query(qhost);
6646 } else if (message) {
6647 rc = do_message_op(popt_get_cmdline_auth_info());
6648 } else if (process(base_directory)) {
6652 popt_free_cmdline_auth_info();