2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) Gerald (Jerry) Carter 2004
8 Copyright (C) Jeremy Allison 1994-2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/filesys.h"
26 #include "rpc_client/cli_pipe.h"
27 #include "client/client_proto.h"
28 #include "client/clitar_proto.h"
29 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
30 #include "../lib/util/select.h"
31 #include "system/readline.h"
32 #include "../libcli/smbreadline/smbreadline.h"
33 #include "../libcli/security/security.h"
34 #include "system/select.h"
35 #include "libsmb/libsmb.h"
36 #include "libsmb/clirap.h"
38 #include "libsmb/nmblib.h"
39 #include "include/ntioctl.h"
40 #include "../libcli/smb/smbXcli_base.h"
41 #include "lib/util/time_basic.h"
42 #include "lib/util/string_wrappers.h"
43 #include "lib/cmdline/cmdline.h"
44 #include "libcli/smb/reparse.h"
45 #include "lib/param/param.h"
51 extern int do_smb_browse(void); /* mDNS browsing */
55 static char *desthost
;
56 static bool grepable
= false;
57 static bool quiet
= false;
58 static char *cmdstr
= NULL
;
59 const char *cmd_ptr
= NULL
;
61 static int io_bufsize
= 0; /* we use the default size */
62 static int io_timeout
= (CLIENT_TIMEOUT
/1000); /* Per operation timeout (in seconds). */
64 static int name_type
= 0x20;
66 static int process_tok(char *tok
);
67 static int cmd_help(void);
69 /* value for unused fid field in trans2 secondary request */
70 #define FID_UNUSED (0xFFFF)
72 time_t newer_than
= 0;
73 static int archive_level
= 0;
75 static bool translation
= false;
78 static bool prompt
= true;
80 static bool recurse
= false;
81 static bool showacls
= false;
82 bool lowercase
= false;
83 static bool backup_intent
= false;
85 static struct sockaddr_storage dest_ss
;
86 static char dest_ss_str
[INET6_ADDRSTRLEN
];
88 #define SEPARATORS " \t\n\r"
91 uint64_t get_total_size
= 0;
92 unsigned int get_total_time_ms
= 0;
93 static uint64_t put_total_size
= 0;
94 static unsigned int put_total_time_ms
= 0;
97 static double dir_total
;
99 /* root cli_state connection */
101 struct cli_state
*cli
;
103 static char CLI_DIRSEP_CHAR
= '\\';
104 static char CLI_DIRSEP_STR
[] = { '\\', '\0' };
106 /* Accessor functions for directory paths. */
107 static char *fileselection
;
108 static const char *client_get_fileselection(void)
111 return fileselection
;
116 static const char *client_set_fileselection(const char *new_fs
)
118 SAFE_FREE(fileselection
);
120 fileselection
= SMB_STRDUP(new_fs
);
122 return client_get_fileselection();
126 static const char *client_get_cwd(void)
131 return CLI_DIRSEP_STR
;
134 static const char *client_set_cwd(const char *new_cwd
)
138 cwd
= SMB_STRDUP(new_cwd
);
140 return client_get_cwd();
143 static char *cur_dir
;
144 const char *client_get_cur_dir(void)
149 return CLI_DIRSEP_STR
;
152 const char *client_set_cur_dir(const char *newdir
)
156 cur_dir
= SMB_STRDUP(newdir
);
158 return client_get_cur_dir();
161 /****************************************************************************
162 Put up a yes/no prompt.
163 ****************************************************************************/
165 static bool yesno(const char *p
)
170 if (!fgets(ans
,sizeof(ans
)-1,stdin
))
173 if (*ans
== 'y' || *ans
== 'Y')
179 /****************************************************************************
180 Write to a local file with CR/LF->LF translation if appropriate. Return the
181 number taken from the buffer. This may not equal the number written.
182 ****************************************************************************/
184 static ssize_t
writefile(int f
, char *b
, size_t n
)
198 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
201 if (write(f
, b
, 1) != 1) {
211 /****************************************************************************
212 Read from a file with LF->CR/LF translation if appropriate. Return the
213 number read. read approx n bytes.
214 ****************************************************************************/
216 static int readfile(uint8_t *b
, int n
, FILE *f
)
222 return fread(b
,1,n
,f
);
225 while (i
< (n
- 1)) {
226 if ((c
= getc(f
)) == EOF
) {
230 if (c
== '\n') { /* change all LFs to CR/LF */
245 static size_t push_source(uint8_t *buf
, size_t n
, void *priv
)
247 struct push_state
*state
= (struct push_state
*)priv
;
250 if (feof(state
->f
)) {
254 result
= readfile(buf
, n
, state
->f
);
255 state
->nread
+= result
;
259 /****************************************************************************
261 ****************************************************************************/
263 static void send_message(const char *username
)
269 d_printf("Type your message, ending it with a Control-D\n");
272 while (i
<sizeof(buf
)-2) {
273 int c
= fgetc(stdin
);
284 status
= cli_message(cli
, desthost
, username
, buf
);
285 if (!NT_STATUS_IS_OK(status
)) {
286 d_fprintf(stderr
, "cli_message returned %s\n",
291 /****************************************************************************
292 Check the space on a device.
293 ****************************************************************************/
295 static int do_dskattr(void)
297 uint64_t total
, bsize
, avail
;
298 struct cli_state
*targetcli
= NULL
;
299 char *targetpath
= NULL
;
300 TALLOC_CTX
*ctx
= talloc_tos();
301 struct cli_credentials
*creds
= samba_cmdline_get_creds();
304 status
= cli_resolve_path(ctx
,
308 client_get_cur_dir(), &targetcli
,
310 if (!NT_STATUS_IS_OK(status
)) {
311 d_printf("Error in dskattr: %s\n", nt_errstr(status
));
315 status
= cli_disk_size(targetcli
, targetpath
, &bsize
, &total
, &avail
);
316 if (!NT_STATUS_IS_OK(status
)) {
317 d_printf("Error in dskattr: %s\n", nt_errstr(status
));
321 d_printf("\n\t\t%" PRIu64
322 " blocks of size %" PRIu64
323 ". %" PRIu64
" blocks available\n",
324 total
, bsize
, avail
);
329 /****************************************************************************
331 ****************************************************************************/
333 static int cmd_pwd(void)
335 d_printf("Current directory is %s",service
);
336 d_printf("%s\n",client_get_cur_dir());
340 /****************************************************************************
341 Ensure name has correct directory separators.
342 ****************************************************************************/
344 static void normalize_name(char *newdir
)
346 if (!(cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
)) {
347 string_replace(newdir
,'/','\\');
351 /****************************************************************************
352 Local name cleanup before sending to server. SMB1 allows relative pathnames,
353 but SMB2 does not, so we need to resolve them locally.
354 ****************************************************************************/
356 char *client_clean_name(TALLOC_CTX
*ctx
, const char *name
)
358 char *newname
= NULL
;
363 /* First ensure any path separators are correct. */
364 newname
= talloc_strdup(ctx
, name
);
365 if (newname
== NULL
) {
368 normalize_name(newname
);
370 /* Now remove any relative (..) path components. */
371 if (cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
372 newname
= unix_clean_name(ctx
, newname
);
374 newname
= clean_name(ctx
, newname
);
376 if (newname
== NULL
) {
382 /****************************************************************************
383 Change directory - inner section.
384 ****************************************************************************/
386 static int do_cd(const char *new_dir
)
389 char *saved_dir
= NULL
;
391 char *targetpath
= NULL
;
392 struct cli_state
*targetcli
= NULL
;
394 TALLOC_CTX
*ctx
= talloc_stackframe();
395 struct cli_credentials
*creds
= samba_cmdline_get_creds();
398 newdir
= talloc_strdup(ctx
, new_dir
);
404 normalize_name(newdir
);
406 /* Save the current directory in case the new directory is invalid */
408 saved_dir
= talloc_strdup(ctx
, client_get_cur_dir());
414 if (*newdir
== CLI_DIRSEP_CHAR
) {
415 client_set_cur_dir(newdir
);
418 new_cd
= talloc_asprintf(ctx
, "%s%s",
419 client_get_cur_dir(),
426 /* Ensure cur_dir ends in a DIRSEP */
427 if ((new_cd
[0] != '\0') && (*(new_cd
+strlen(new_cd
)-1) != CLI_DIRSEP_CHAR
)) {
428 new_cd
= talloc_asprintf_append(new_cd
, "%s", CLI_DIRSEP_STR
);
433 client_set_cur_dir(new_cd
);
435 new_cd
= client_clean_name(ctx
, new_cd
);
436 client_set_cur_dir(new_cd
);
438 status
= cli_resolve_path(ctx
, "",
440 cli
, new_cd
, &targetcli
, &targetpath
);
441 if (!NT_STATUS_IS_OK(status
)) {
442 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
443 client_set_cur_dir(saved_dir
);
447 if (strequal(targetpath
,CLI_DIRSEP_STR
)) {
453 targetpath
= talloc_asprintf(
454 ctx
, "%s%s", targetpath
, CLI_DIRSEP_STR
);
456 client_set_cur_dir(saved_dir
);
459 targetpath
= client_clean_name(ctx
, targetpath
);
461 client_set_cur_dir(saved_dir
);
465 status
= cli_chkpath(targetcli
, targetpath
);
466 if (!NT_STATUS_IS_OK(status
)) {
467 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
468 client_set_cur_dir(saved_dir
);
480 /****************************************************************************
482 ****************************************************************************/
484 static int cmd_cd(void)
489 if (next_token_talloc(talloc_tos(), &cmd_ptr
, &buf
,NULL
)) {
492 d_printf("Current directory is %s\n",client_get_cur_dir());
498 /****************************************************************************
500 ****************************************************************************/
502 static int cmd_cd_oneup(void)
507 /*******************************************************************
508 Decide if a file should be operated on.
509 ********************************************************************/
511 static bool do_this_one(struct file_info
*finfo
)
517 if (finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
) {
521 if (*client_get_fileselection() &&
522 !mask_match(finfo
->name
,client_get_fileselection(),false)) {
523 DEBUG(3,("mask_match %s failed\n", finfo
->name
));
527 if (newer_than
&& finfo
->mtime_ts
.tv_sec
< newer_than
) {
528 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
532 if ((archive_level
==1 || archive_level
==2) && !(finfo
->attr
& FILE_ATTRIBUTE_ARCHIVE
)) {
533 DEBUG(3,("archive %s failed\n", finfo
->name
));
540 /****************************************************************************
541 Display info about a file.
542 ****************************************************************************/
544 static NTSTATUS
display_finfo(struct cli_state
*cli_state
, struct file_info
*finfo
,
548 TALLOC_CTX
*ctx
= talloc_tos();
549 NTSTATUS status
= NT_STATUS_OK
;
551 if (!do_this_one(finfo
)) {
555 t
= finfo
->mtime_ts
.tv_sec
; /* the time is assumed to be passed as GMT */
557 d_printf(" %-30s%7.7s %8.0f %s",
559 attrib_string(talloc_tos(), finfo
->attr
),
562 dir_total
+= finfo
->size
;
564 struct cli_state
*targetcli
= NULL
;
565 char *targetpath
= NULL
;
568 struct cli_credentials
*creds
= samba_cmdline_get_creds();
570 if (ISDOT(finfo
->name
) || ISDOTDOT(finfo
->name
)) {
573 /* create absolute filename for cli_ntcreate() FIXME */
574 afname
= talloc_asprintf(ctx
,
580 return NT_STATUS_NO_MEMORY
;
582 /* print file meta date header */
583 d_printf( "FILENAME:%s\n", finfo
->name
);
584 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo
->attr
));
585 d_printf( "SIZE:%.0f\n", (double)finfo
->size
);
586 d_printf( "MTIME:%s", time_to_asc(t
));
588 status
= cli_resolve_path(
596 if (!NT_STATUS_IS_OK(status
)) {
597 DBG_WARNING("display_finfo() Failed to resolve "
599 afname
, nt_errstr(status
));
603 status
= cli_ntcreate(
605 targetpath
, /* fname */
607 READ_CONTROL_ACCESS
, /* DesiredAccess */
608 0, /* FileAttributes */
610 FILE_SHARE_WRITE
, /* ShareAccess */
611 FILE_OPEN
, /* CreateDisposition */
612 0x0, /* CreateOptions */
613 0x0, /* SecurityFlags */
616 if (!NT_STATUS_IS_OK(status
)) {
617 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
618 afname
, nt_errstr(status
)));
620 struct security_descriptor
*sd
= NULL
;
621 status
= cli_query_secdesc(targetcli
, fnum
,
623 if (!NT_STATUS_IS_OK(status
)) {
624 DEBUG( 0, ("display_finfo() failed to "
625 "get security descriptor: %s\n",
628 display_sec_desc(sd
);
631 cli_close(targetcli
, fnum
);
638 /****************************************************************************
639 Accumulate size of a file.
640 ****************************************************************************/
642 static NTSTATUS
do_du(struct cli_state
*cli_state
, struct file_info
*finfo
,
645 if (do_this_one(finfo
)) {
646 dir_total
+= finfo
->size
;
651 struct do_list_queue_entry
{
652 struct do_list_queue_entry
*prev
, *next
;
656 struct do_list_queue
{
657 struct do_list_queue_entry
*list
;
660 static bool do_list_recurse
;
661 static bool do_list_dirs
;
662 static struct do_list_queue
*queue
= NULL
;
663 static NTSTATUS (*do_list_fn
)(struct cli_state
*cli_state
, struct file_info
*,
666 /****************************************************************************
667 Functions for do_list_queue.
668 ****************************************************************************/
670 static void reset_do_list_queue(void)
675 static void init_do_list_queue(void)
678 queue
= talloc_zero(NULL
, struct do_list_queue
);
681 static void add_to_do_list_queue(const char *entry
)
683 struct do_list_queue_entry
*e
= NULL
;
684 size_t entry_str_len
= strlen(entry
)+1;
685 size_t entry_len
= offsetof(struct do_list_queue_entry
, name
);
687 entry_len
+= entry_str_len
;
688 SMB_ASSERT(entry_len
>= entry_str_len
);
690 e
= talloc_size(queue
, entry_len
);
692 d_printf("talloc failed for entry %s\n", entry
);
695 talloc_set_name_const(e
, "struct do_list_queue_entry");
697 memcpy(e
->name
, entry
, entry_str_len
);
698 DLIST_ADD_END(queue
->list
, e
);
701 static char *do_list_queue_head(void)
703 return queue
->list
->name
;
706 static void remove_do_list_queue_head(void)
708 struct do_list_queue_entry
*e
= queue
->list
;
709 DLIST_REMOVE(queue
->list
, e
);
713 static int do_list_queue_empty(void)
715 return (queue
== NULL
) || (queue
->list
== NULL
);
718 /****************************************************************************
719 A helper for do_list.
720 ****************************************************************************/
722 struct do_list_helper_state
{
724 struct cli_state
*cli
;
727 static NTSTATUS
do_list_helper(
732 struct do_list_helper_state
*state
= private_data
;
733 TALLOC_CTX
*ctx
= talloc_tos();
735 char *dir_end
= NULL
;
736 NTSTATUS status
= NT_STATUS_OK
;
739 /* Work out the directory. */
740 dir
= talloc_strdup(ctx
, state
->mask
);
742 return NT_STATUS_NO_MEMORY
;
744 if ((dir_end
= strrchr(dir
, CLI_DIRSEP_CHAR
)) != NULL
) {
748 if (!(f
->attr
& FILE_ATTRIBUTE_DIRECTORY
)) {
749 if (do_this_one(f
)) {
750 status
= do_list_fn(state
->cli
, f
, dir
);
756 if (do_list_dirs
&& do_this_one(f
)) {
757 status
= do_list_fn(state
->cli
, f
, dir
);
758 if (!NT_STATUS_IS_OK(status
)) {
763 if (!do_list_recurse
||
771 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
773 return NT_STATUS_UNSUCCESSFUL
;
776 mask2
= talloc_asprintf(ctx
,
784 return NT_STATUS_NO_MEMORY
;
786 add_to_do_list_queue(mask2
);
793 /****************************************************************************
794 A wrapper around cli_list that adds recursion.
795 ****************************************************************************/
797 NTSTATUS
do_list(const char *mask
,
799 NTSTATUS (*fn
)(struct cli_state
*cli_state
, struct file_info
*,
804 struct do_list_helper_state state
= { .cli
= cli
, };
805 static int in_do_list
= 0;
806 TALLOC_CTX
*ctx
= talloc_tos();
807 struct cli_credentials
*creds
= samba_cmdline_get_creds();
808 NTSTATUS ret_status
= NT_STATUS_OK
;
809 NTSTATUS status
= NT_STATUS_OK
;
811 if (in_do_list
&& rec
) {
812 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
818 do_list_recurse
= rec
;
822 init_do_list_queue();
823 add_to_do_list_queue(mask
);
825 while (!do_list_queue_empty()) {
826 struct cli_state
*targetcli
= NULL
;
827 char *targetpath
= NULL
;
829 state
.mask
= do_list_queue_head();
833 status
= cli_resolve_path(
841 if (!NT_STATUS_IS_OK(status
)) {
842 d_printf("do_list: [%s] %s\n", state
.mask
,
844 remove_do_list_queue_head();
854 if (!NT_STATUS_IS_OK(status
)) {
855 d_printf("%s listing %s\n",
856 nt_errstr(status
), targetpath
);
859 remove_do_list_queue_head();
860 if ((! do_list_queue_empty()) && (fn
== display_finfo
)) {
861 char *next_file
= do_list_queue_head();
863 if ((strlen(next_file
) >= 2) &&
864 (next_file
[strlen(next_file
) - 1] == '*') &&
865 (next_file
[strlen(next_file
) - 2] == CLI_DIRSEP_CHAR
)) {
866 save_ch
= next_file
+
867 strlen(next_file
) - 2;
870 /* cwd is only used if showacls is on */
871 client_set_cwd(next_file
);
874 if (!showacls
) /* don't disturbe the showacls output */
875 d_printf("\n%s\n",next_file
);
877 *save_ch
= CLI_DIRSEP_CHAR
;
880 TALLOC_FREE(targetpath
);
884 reset_do_list_queue();
888 /****************************************************************************
889 Get a directory listing.
890 ****************************************************************************/
892 static int cmd_dir(void)
894 TALLOC_CTX
*ctx
= talloc_tos();
895 uint32_t attribute
= FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
902 mask
= talloc_strdup(ctx
, client_get_cur_dir());
907 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
909 if (*buf
== CLI_DIRSEP_CHAR
) {
910 mask
= talloc_strdup(ctx
, buf
);
912 mask
= talloc_asprintf_append(mask
, "%s", buf
);
915 mask
= talloc_asprintf_append(mask
, "*");
921 mask
= client_clean_name(ctx
, mask
);
927 /* cwd is only used if showacls is on */
928 client_set_cwd(client_get_cur_dir());
931 status
= do_list(mask
, attribute
, display_finfo
, recurse
, true);
932 if (!NT_STATUS_IS_OK(status
)) {
938 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
943 /****************************************************************************
944 Get a directory listing.
945 ****************************************************************************/
947 static int cmd_du(void)
949 TALLOC_CTX
*ctx
= talloc_tos();
950 uint32_t attribute
= FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
957 mask
= talloc_strdup(ctx
, client_get_cur_dir());
961 if ((mask
[0] != '\0') && (mask
[strlen(mask
)-1]!=CLI_DIRSEP_CHAR
)) {
962 mask
= talloc_asprintf_append(mask
, "%s", CLI_DIRSEP_STR
);
968 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
970 if (*buf
== CLI_DIRSEP_CHAR
) {
971 mask
= talloc_strdup(ctx
, buf
);
973 mask
= talloc_asprintf_append(mask
, "%s", buf
);
976 mask
= talloc_strdup(ctx
, "*");
982 mask
= client_clean_name(ctx
, mask
);
987 status
= do_list(mask
, attribute
, do_du
, recurse
, true);
988 if (!NT_STATUS_IS_OK(status
)) {
994 d_printf("Total number of bytes: %.0f\n", dir_total
);
999 static int cmd_echo(void)
1001 TALLOC_CTX
*ctx
= talloc_tos();
1006 if (!next_token_talloc(ctx
, &cmd_ptr
, &num
, NULL
)
1007 || !next_token_talloc(ctx
, &cmd_ptr
, &data
, NULL
)) {
1008 d_printf("echo <num> <data>\n");
1012 status
= cli_echo(cli
, atoi(num
), data_blob_const(data
, strlen(data
)));
1014 if (!NT_STATUS_IS_OK(status
)) {
1015 d_printf("echo failed: %s\n", nt_errstr(status
));
1022 /****************************************************************************
1023 Get a file from rname to lname
1024 ****************************************************************************/
1026 static NTSTATUS
writefile_sink(char *buf
, size_t n
, void *priv
)
1028 int *pfd
= (int *)priv
;
1031 rc
= writefile(*pfd
, buf
, n
);
1033 return map_nt_error_from_unix(errno
);
1035 return NT_STATUS_OK
;
1038 static int do_get(const char *rname
, const char *lname_in
, bool reget
)
1040 TALLOC_CTX
*ctx
= talloc_tos();
1043 bool newhandle
= false;
1044 struct timespec tp_start
;
1050 struct cli_state
*targetcli
= NULL
;
1051 char *targetname
= NULL
;
1053 struct cli_credentials
*creds
= samba_cmdline_get_creds();
1056 lname
= talloc_strdup(ctx
, lname_in
);
1062 if (!strlower_m(lname
)) {
1063 d_printf("strlower_m %s failed\n", lname
);
1068 status
= cli_resolve_path(ctx
, "",
1070 cli
, rname
, &targetcli
, &targetname
);
1071 if (!NT_STATUS_IS_OK(status
)) {
1072 d_printf("Failed to open %s: %s\n", rname
, nt_errstr(status
));
1076 clock_gettime_mono(&tp_start
);
1078 status
= cli_open(targetcli
, targetname
, O_RDONLY
, DENY_NONE
, &fnum
);
1079 if (!NT_STATUS_IS_OK(status
)) {
1080 d_printf("%s opening remote file %s\n", nt_errstr(status
),
1085 if(!strcmp(lname
,"-")) {
1086 handle
= fileno(stdout
);
1089 handle
= open(lname
, O_WRONLY
|O_CREAT
, 0644);
1091 start
= lseek(handle
, 0, SEEK_END
);
1093 d_printf("Error seeking local file\n");
1099 handle
= open(lname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
1104 d_printf("Error opening local file %s\n",lname
);
1109 status
= cli_qfileinfo_basic(targetcli
, fnum
, &attr
, &size
, NULL
, NULL
,
1111 if (!NT_STATUS_IS_OK(status
)) {
1112 d_printf("getattrib: %s\n", nt_errstr(status
));
1119 DEBUG(1,("getting file %s of size %.0f as %s ",
1120 rname
, (double)size
, lname
));
1122 status
= cli_pull(targetcli
, fnum
, start
, size
, io_bufsize
,
1123 writefile_sink
, (void *)&handle
, &nread
);
1124 if (!NT_STATUS_IS_OK(status
)) {
1125 d_fprintf(stderr
, "parallel_read returned %s\n",
1130 cli_close(targetcli
, fnum
);
1134 status
= cli_close(targetcli
, fnum
);
1135 if (!NT_STATUS_IS_OK(status
)) {
1136 d_printf("Error %s closing remote file\n", nt_errstr(status
));
1144 if (archive_level
>= 2 && (attr
& FILE_ATTRIBUTE_ARCHIVE
)) {
1145 cli_setatr(cli
, rname
, attr
& ~(uint32_t)FILE_ATTRIBUTE_ARCHIVE
, 0);
1149 struct timespec tp_end
;
1152 clock_gettime_mono(&tp_end
);
1153 this_time
= nsec_time_diff(&tp_end
,&tp_start
)/1000000;
1154 get_total_time_ms
+= this_time
;
1155 get_total_size
+= nread
;
1157 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1158 nread
/ (1.024*this_time
+ 1.0e-4),
1159 get_total_size
/ (1.024*get_total_time_ms
)));
1162 TALLOC_FREE(targetname
);
1166 /****************************************************************************
1168 ****************************************************************************/
1170 static int cmd_get(void)
1172 TALLOC_CTX
*ctx
= talloc_tos();
1177 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1182 if (!next_token_talloc(ctx
, &cmd_ptr
,&fname
,NULL
)) {
1183 d_printf("get <filename> [localname]\n");
1186 rname
= talloc_asprintf_append(rname
, "%s", fname
);
1190 rname
= client_clean_name(ctx
, rname
);
1195 next_token_talloc(ctx
, &cmd_ptr
,&lname
,NULL
);
1200 return do_get(rname
, lname
, false);
1203 /****************************************************************************
1204 Do an mget operation on one file.
1205 ****************************************************************************/
1207 static NTSTATUS
do_mget(struct cli_state
*cli_state
, struct file_info
*finfo
,
1210 TALLOC_CTX
*ctx
= talloc_tos();
1211 const char *client_cwd
= NULL
;
1212 size_t client_cwd_len
;
1214 char *local_path
= NULL
;
1217 return NT_STATUS_OK
;
1220 if (ISDOT(finfo
->name
) || ISDOTDOT(finfo
->name
)) {
1221 return NT_STATUS_OK
;
1224 if ((finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
) && !recurse
) {
1225 return NT_STATUS_OK
;
1229 const char *object
= (finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
) ?
1230 "directory" : "file";
1234 quest
= talloc_asprintf(
1235 ctx
, "Get %s %s? ", object
, finfo
->name
);
1236 if (quest
== NULL
) {
1237 return NT_STATUS_NO_MEMORY
;
1243 return NT_STATUS_OK
;
1247 path
= talloc_asprintf(
1248 ctx
, "%s%c%s", dir
, CLI_DIRSEP_CHAR
, finfo
->name
);
1250 return NT_STATUS_NO_MEMORY
;
1252 path
= client_clean_name(ctx
, path
);
1254 return NT_STATUS_NO_MEMORY
;
1258 * Skip the path prefix if we've done a remote "cd" when
1259 * creating the local path
1261 client_cwd
= client_get_cur_dir();
1262 client_cwd_len
= strlen(client_cwd
);
1264 local_path
= talloc_strdup(ctx
, path
+ client_cwd_len
);
1265 if (local_path
== NULL
) {
1267 return NT_STATUS_NO_MEMORY
;
1269 string_replace(local_path
, CLI_DIRSEP_CHAR
, '/');
1271 if (finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
) {
1272 int ret
= mkdir(local_path
, 0777);
1274 if ((ret
== -1) && (errno
!= EEXIST
)) {
1275 return map_nt_error_from_unix(errno
);
1278 do_get(path
, local_path
, false);
1281 return NT_STATUS_OK
;
1284 /****************************************************************************
1285 View the file using the pager.
1286 ****************************************************************************/
1288 static int cmd_more(void)
1290 TALLOC_CTX
*ctx
= talloc_tos();
1294 char *pager_cmd
= NULL
;
1300 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1305 lname
= talloc_asprintf(ctx
, "%s/smbmore.XXXXXX",tmpdir());
1309 mask
= umask(S_IRWXO
| S_IRWXG
);
1310 fd
= mkstemp(lname
);
1313 d_printf("failed to create temporary file for more\n");
1318 if (!next_token_talloc(ctx
, &cmd_ptr
,&fname
,NULL
)) {
1319 d_printf("more <filename>\n");
1323 rname
= talloc_asprintf_append(rname
, "%s", fname
);
1327 rname
= client_clean_name(ctx
,rname
);
1332 rc
= do_get(rname
, lname
, false);
1334 pager
=getenv("PAGER");
1336 pager_cmd
= talloc_asprintf(ctx
,
1338 (pager
? pager
:PAGER
),
1343 if (system(pager_cmd
) == -1) {
1344 d_printf("system command '%s' returned -1\n",
1352 /****************************************************************************
1354 ****************************************************************************/
1356 static int cmd_mget(void)
1358 TALLOC_CTX
*ctx
= talloc_tos();
1359 uint32_t attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
1360 char *mget_mask
= NULL
;
1362 NTSTATUS status
= NT_STATUS_OK
;
1365 attribute
|= FILE_ATTRIBUTE_DIRECTORY
;
1368 while (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1370 mget_mask
= talloc_strdup(ctx
, client_get_cur_dir());
1374 if (*buf
== CLI_DIRSEP_CHAR
) {
1375 mget_mask
= talloc_strdup(ctx
, buf
);
1377 mget_mask
= talloc_asprintf_append(mget_mask
,
1383 mget_mask
= client_clean_name(ctx
, mget_mask
);
1384 if (mget_mask
== NULL
) {
1387 status
= do_list(mget_mask
, attribute
, do_mget
, recurse
, true);
1388 if (!NT_STATUS_IS_OK(status
)) {
1393 if (mget_mask
== NULL
) {
1394 d_printf("nothing to mget\n");
1399 mget_mask
= talloc_asprintf(ctx
,
1401 client_get_cur_dir());
1405 mget_mask
= client_clean_name(ctx
, mget_mask
);
1406 if (mget_mask
== NULL
) {
1409 status
= do_list(mget_mask
, attribute
, do_mget
, recurse
, true);
1410 if (!NT_STATUS_IS_OK(status
)) {
1418 /****************************************************************************
1419 Make a directory of name "name".
1420 ****************************************************************************/
1422 static bool do_mkdir(const char *name
)
1424 TALLOC_CTX
*ctx
= talloc_tos();
1425 struct cli_state
*targetcli
;
1426 char *targetname
= NULL
;
1427 struct cli_credentials
*creds
= samba_cmdline_get_creds();
1430 status
= cli_resolve_path(ctx
, "",
1432 cli
, name
, &targetcli
, &targetname
);
1433 if (!NT_STATUS_IS_OK(status
)) {
1434 d_printf("mkdir %s: %s\n", name
, nt_errstr(status
));
1438 status
= cli_mkdir(targetcli
, targetname
);
1439 if (!NT_STATUS_IS_OK(status
)) {
1440 d_printf("%s making remote directory %s\n",
1441 nt_errstr(status
),name
);
1448 /****************************************************************************
1449 Show 8.3 name of a file.
1450 ****************************************************************************/
1452 static bool do_altname(const char *name
)
1457 status
= cli_qpathinfo_alt_name(cli
, name
, altname
);
1458 if (!NT_STATUS_IS_OK(status
)) {
1459 d_printf("%s getting alt name for %s\n",
1460 nt_errstr(status
),name
);
1463 d_printf("%s\n", altname
);
1468 /****************************************************************************
1470 ****************************************************************************/
1472 static int cmd_quit(void)
1480 /****************************************************************************
1482 ****************************************************************************/
1484 static int cmd_mkdir(void)
1486 TALLOC_CTX
*ctx
= talloc_tos();
1489 struct cli_credentials
*creds
= samba_cmdline_get_creds();
1492 mask
= talloc_strdup(ctx
, client_get_cur_dir());
1497 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1499 d_printf("mkdir <dirname>\n");
1503 mask
= talloc_asprintf_append(mask
, "%s", buf
);
1507 mask
= client_clean_name(ctx
, mask
);
1515 struct cli_state
*targetcli
;
1516 char *targetname
= NULL
;
1520 ddir2
= talloc_strdup(ctx
, "");
1525 status
= cli_resolve_path(ctx
, "",
1528 &targetcli
, &targetname
);
1529 if (!NT_STATUS_IS_OK(status
)) {
1533 ddir
= talloc_strdup(ctx
, targetname
);
1537 trim_char(ddir
,'.','\0');
1538 p
= strtok_r(ddir
, "/\\", &saveptr
);
1540 ddir2
= talloc_asprintf_append(ddir2
, "%s", p
);
1544 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli
, ddir2
))) {
1547 ddir2
= talloc_asprintf_append(ddir2
, "%s", CLI_DIRSEP_STR
);
1551 p
= strtok_r(NULL
, "/\\", &saveptr
);
1560 /****************************************************************************
1562 ****************************************************************************/
1564 static int cmd_altname(void)
1566 TALLOC_CTX
*ctx
= talloc_tos();
1570 name
= talloc_strdup(ctx
, client_get_cur_dir());
1575 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
1576 d_printf("altname <file>\n");
1579 name
= talloc_asprintf_append(name
, "%s", buf
);
1583 name
= client_clean_name(ctx
, name
);
1591 static char *attr_str(TALLOC_CTX
*mem_ctx
, uint32_t attr
)
1593 char *attrs
= talloc_zero_array(mem_ctx
, char, 17);
1596 if (!(attr
& FILE_ATTRIBUTE_NORMAL
)) {
1597 if (attr
& FILE_ATTRIBUTE_ENCRYPTED
) {
1600 if (attr
& FILE_ATTRIBUTE_NONINDEXED
) {
1603 if (attr
& FILE_ATTRIBUTE_OFFLINE
) {
1606 if (attr
& FILE_ATTRIBUTE_COMPRESSED
) {
1609 if (attr
& FILE_ATTRIBUTE_REPARSE_POINT
) {
1612 if (attr
& FILE_ATTRIBUTE_SPARSE
) {
1615 if (attr
& FILE_ATTRIBUTE_TEMPORARY
) {
1618 if (attr
& FILE_ATTRIBUTE_NORMAL
) {
1621 if (attr
& FILE_ATTRIBUTE_READONLY
) {
1624 if (attr
& FILE_ATTRIBUTE_HIDDEN
) {
1627 if (attr
& FILE_ATTRIBUTE_SYSTEM
) {
1630 if (attr
& FILE_ATTRIBUTE_DIRECTORY
) {
1633 if (attr
& FILE_ATTRIBUTE_ARCHIVE
) {
1640 /****************************************************************************
1641 Show all info we can get
1642 ****************************************************************************/
1644 static int do_allinfo(const char *name
)
1647 struct timespec b_time
, a_time
, m_time
, c_time
;
1652 unsigned int num_streams
;
1653 struct stream_struct
*streams
;
1654 int j
, num_snapshots
;
1655 char **snapshots
= NULL
;
1659 status
= cli_qpathinfo_alt_name(cli
, name
, altname
);
1660 if (!NT_STATUS_IS_OK(status
)) {
1661 d_printf("%s getting alt name for %s\n", nt_errstr(status
),
1664 * Ignore not supported or not implemented, it does not
1665 * hurt if we can't list alternate names.
1667 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
) ||
1668 NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)) {
1674 d_printf("altname: %s\n", altname
);
1676 status
= cli_qpathinfo3(cli
, name
, &b_time
, &a_time
, &m_time
, &c_time
,
1677 &size
, &attr
, NULL
);
1678 if (!NT_STATUS_IS_OK(status
)) {
1679 d_printf("%s getting pathinfo for %s\n", nt_errstr(status
),
1684 tmp
= full_timespec_to_nt_time(&b_time
);
1685 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1687 tmp
= full_timespec_to_nt_time(&a_time
);
1688 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1690 tmp
= full_timespec_to_nt_time(&m_time
);
1691 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1693 tmp
= full_timespec_to_nt_time(&c_time
);
1694 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1696 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), attr
), attr
);
1698 status
= cli_qpathinfo_streams(cli
, name
, talloc_tos(), &num_streams
,
1700 if (!NT_STATUS_IS_OK(status
)) {
1702 "%s getting streams for %s\n",
1708 for (i
=0; i
<num_streams
; i
++) {
1709 d_printf("stream: [%s], %lld bytes\n", streams
[i
].name
,
1710 (unsigned long long)streams
[i
].size
);
1713 if (attr
& FILE_ATTRIBUTE_REPARSE_POINT
) {
1714 struct reparse_data_buffer
*rep
= NULL
;
1715 uint8_t *data
= NULL
;
1719 rep
= talloc_zero(talloc_tos(), struct reparse_data_buffer
);
1721 d_printf("talloc_zero() failed\n");
1725 status
= cli_get_reparse_data(cli
, name
, rep
, &data
, &datalen
);
1726 if (!NT_STATUS_IS_OK(status
)) {
1728 "cli_get_reparse_data() failed: %s\n",
1734 status
= reparse_data_buffer_parse(rep
, rep
, data
, datalen
);
1735 if (!NT_STATUS_IS_OK(status
)) {
1737 "reparse_data_buffer_parse() failed: %s\n",
1743 s
= reparse_data_buffer_str(rep
, rep
);
1748 status
= cli_ntcreate(cli
, name
, 0,
1749 SEC_FILE_READ_DATA
| SEC_FILE_READ_ATTRIBUTE
|
1750 SEC_STD_SYNCHRONIZE
, 0,
1751 FILE_SHARE_READ
|FILE_SHARE_WRITE
1753 FILE_OPEN
, 0x0, 0x0, &fnum
, NULL
);
1754 if (!NT_STATUS_IS_OK(status
)) {
1756 * Ignore failure, it does not hurt if we can't list
1762 * In order to get shadow copy data over SMB1 we
1763 * must call twice, once with 'get_names = false'
1764 * to get the size, then again with 'get_names = true'
1765 * to get the data or a Windows server fails to return
1766 * valid info. Samba doesn't have this bug. JRA.
1769 status
= cli_shadow_copy_data(talloc_tos(), cli
, fnum
,
1770 false, &snapshots
, &num_snapshots
);
1771 if (NT_STATUS_IS_OK(status
)) {
1772 status
= cli_shadow_copy_data(talloc_tos(),
1779 if (!NT_STATUS_IS_OK(status
)) {
1781 "%s getting shadow copy data for %s\n",
1787 for (j
=0; j
<num_snapshots
; j
++) {
1790 d_printf("%s\n", snapshots
[j
]);
1791 snap_name
= talloc_asprintf(talloc_tos(), "%s%s",
1792 snapshots
[j
], name
);
1793 status
= cli_qpathinfo3(cli
, snap_name
, &b_time
, &a_time
,
1794 &m_time
, &c_time
, &size
,
1796 if (!NT_STATUS_IS_OK(status
)) {
1797 d_fprintf(stderr
, "pathinfo(%s) failed: %s\n",
1798 snap_name
, nt_errstr(status
));
1799 TALLOC_FREE(snap_name
);
1802 tmp
= unix_timespec_to_nt_time(b_time
);
1803 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1804 tmp
= unix_timespec_to_nt_time(a_time
);
1805 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1806 tmp
=unix_timespec_to_nt_time(m_time
);
1807 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1808 tmp
= unix_timespec_to_nt_time(c_time
);
1809 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1810 d_printf("size: %d\n", (int)size
);
1813 TALLOC_FREE(snapshots
);
1814 cli_close(cli
, fnum
);
1819 /****************************************************************************
1820 Show all info we can get
1821 ****************************************************************************/
1823 static int cmd_allinfo(void)
1825 TALLOC_CTX
*ctx
= talloc_tos();
1829 name
= talloc_strdup(ctx
, client_get_cur_dir());
1834 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
1835 d_printf("allinfo <file>\n");
1838 name
= talloc_asprintf_append(name
, "%s", buf
);
1842 name
= client_clean_name(ctx
, name
);
1851 /****************************************************************************
1853 ****************************************************************************/
1855 static int do_put(const char *rname
, const char *lname
, bool reput
)
1857 TALLOC_CTX
*ctx
= talloc_tos();
1862 struct timespec tp_start
;
1863 struct cli_state
*targetcli
;
1864 char *targetname
= NULL
;
1865 struct push_state state
;
1866 struct cli_credentials
*creds
= samba_cmdline_get_creds();
1869 status
= cli_resolve_path(ctx
, "",
1871 cli
, rname
, &targetcli
, &targetname
);
1872 if (!NT_STATUS_IS_OK(status
)) {
1873 d_printf("Failed to open %s: %s\n", rname
, nt_errstr(status
));
1877 clock_gettime_mono(&tp_start
);
1880 status
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
, DENY_NONE
, &fnum
);
1881 if (NT_STATUS_IS_OK(status
)) {
1882 if (!NT_STATUS_IS_OK(status
= cli_qfileinfo_basic(
1883 targetcli
, fnum
, NULL
,
1885 NULL
, NULL
, NULL
))) {
1886 d_printf("getattrib: %s\n", nt_errstr(status
));
1891 status
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
, &fnum
);
1894 if (!NT_STATUS_IS_OK(status
)) {
1895 d_printf("%s opening remote file %s\n", nt_errstr(status
),
1900 /* allow files to be piped into smbclient
1903 Note that in this case this function will exit(0) rather
1905 if (!strcmp(lname
, "-")) {
1907 /* size of file is not known */
1909 f
= fopen(lname
, "r");
1911 if (fseek(f
, start
, SEEK_SET
) == -1) {
1912 d_printf("Error seeking local file\n");
1920 d_printf("Error opening local file %s\n",lname
);
1924 DEBUG(1,("putting file %s as %s ",lname
,
1927 setvbuf(f
, NULL
, _IOFBF
, io_bufsize
);
1932 status
= cli_push(targetcli
, fnum
, 0, 0, io_bufsize
, push_source
,
1934 if (!NT_STATUS_IS_OK(status
)) {
1935 d_fprintf(stderr
, "cli_push returned %s\n", nt_errstr(status
));
1939 status
= cli_close(targetcli
, fnum
);
1940 if (!NT_STATUS_IS_OK(status
)) {
1941 d_printf("%s closing remote file %s\n", nt_errstr(status
),
1954 struct timespec tp_end
;
1957 clock_gettime_mono(&tp_end
);
1958 this_time
= nsec_time_diff(&tp_end
,&tp_start
)/1000000;
1959 put_total_time_ms
+= this_time
;
1960 put_total_size
+= state
.nread
;
1962 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1963 state
.nread
/ (1.024*this_time
+ 1.0e-4),
1964 put_total_size
/ (1.024*put_total_time_ms
)));
1975 /****************************************************************************
1977 ****************************************************************************/
1979 static int cmd_put(void)
1981 TALLOC_CTX
*ctx
= talloc_tos();
1986 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1991 if (!next_token_talloc(ctx
, &cmd_ptr
,&lname
,NULL
)) {
1992 d_printf("put <filename>\n");
1996 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1997 rname
= talloc_asprintf_append(rname
, "%s", buf
);
1999 rname
= talloc_asprintf_append(rname
, "%s", lname
);
2005 rname
= client_clean_name(ctx
, rname
);
2012 /* allow '-' to represent stdin
2013 jdblair, 24.jun.98 */
2014 if (!file_exist_stat(lname
, &st
, false) &&
2015 (strcmp(lname
,"-"))) {
2016 d_printf("%s does not exist\n",lname
);
2021 return do_put(rname
, lname
, false);
2024 /*************************************
2025 File list structure.
2026 *************************************/
2028 static struct file_list
{
2029 struct file_list
*prev
, *next
;
2034 /****************************************************************************
2035 Free a file_list structure.
2036 ****************************************************************************/
2038 static void free_file_list (struct file_list
*l_head
)
2040 struct file_list
*list
, *next
;
2042 for (list
= l_head
; list
; list
= next
) {
2044 DLIST_REMOVE(l_head
, list
);
2049 /****************************************************************************
2050 Seek in a directory/file list until you get something that doesn't start with
2052 ****************************************************************************/
2054 static bool seek_list(struct file_list
*list
, char *name
)
2057 trim_string(list
->file_path
,"./","\n");
2058 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
2067 /****************************************************************************
2068 Set the file selection mask.
2069 ****************************************************************************/
2071 static int cmd_select(void)
2073 TALLOC_CTX
*ctx
= talloc_tos();
2074 char *new_fs
= NULL
;
2075 next_token_talloc(ctx
, &cmd_ptr
,&new_fs
,NULL
)
2078 client_set_fileselection(new_fs
);
2080 client_set_fileselection("");
2085 /****************************************************************************
2086 Recursive file matching function act as find
2087 match must be always set to true when calling this function
2088 ****************************************************************************/
2090 static int file_find(TALLOC_CTX
*ctx
,
2091 struct file_list
**list
,
2092 const char *directory
,
2093 const char *expression
,
2097 struct file_list
*entry
;
2098 struct stat statbuf
;
2104 dir
= opendir(directory
);
2108 while ((dname
= readdirname(dir
))) {
2109 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
2113 path
= talloc_asprintf(ctx
, "%s/%s", directory
, dname
);
2119 if (!match
|| !gen_fnmatch(expression
, dname
)) {
2121 ret
= stat(path
, &statbuf
);
2123 if (S_ISDIR(statbuf
.st_mode
)) {
2125 ret
= file_find(ctx
,
2132 d_printf("file_find: cannot stat file %s\n", path
);
2141 entry
= talloc_zero(ctx
, struct file_list
);
2143 d_printf("Out of memory in file_find\n");
2147 entry
->file_path
= talloc_move(entry
, &path
);
2148 entry
->isdir
= isdir
;
2149 DLIST_ADD(*list
, entry
);
2159 /****************************************************************************
2161 ****************************************************************************/
2163 static int cmd_mput(void)
2165 TALLOC_CTX
*ctx
= talloc_tos();
2168 while (next_token_talloc(ctx
, &cmd_ptr
,&p
,NULL
)) {
2170 struct file_list
*temp_list
;
2171 char *quest
, *lname
, *rname
;
2175 ret
= file_find(ctx
, &file_list
, ".", p
, true);
2177 free_file_list(file_list
);
2185 for (temp_list
= file_list
; temp_list
;
2186 temp_list
= temp_list
->next
) {
2189 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0) {
2192 trim_string(lname
, "./", "/");
2194 /* check if it's a directory */
2195 if (temp_list
->isdir
) {
2196 /* if (!recurse) continue; */
2199 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) {
2202 if (prompt
&& !yesno(quest
)) { /* No */
2203 /* Skip the directory */
2204 lname
[strlen(lname
)-1] = '/';
2205 if (!seek_list(temp_list
, lname
))
2209 if(asprintf(&rname
, "%s%s", client_get_cur_dir(), lname
) < 0) {
2212 normalize_name(rname
);
2215 client_clean_name(ctx
, rname
);
2216 if (tmp_rname
== NULL
) {
2220 rname
= smb_xstrdup(tmp_rname
);
2221 TALLOC_FREE(tmp_rname
);
2222 if (rname
== NULL
) {
2226 if (!NT_STATUS_IS_OK(cli_chkpath(cli
, rname
)) &&
2228 DEBUG (0, ("Unable to make dir, skipping...\n"));
2229 /* Skip the directory */
2230 lname
[strlen(lname
)-1] = '/';
2231 if (!seek_list(temp_list
, lname
)) {
2239 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) {
2242 if (prompt
&& !yesno(quest
)) {
2249 if (asprintf(&rname
, "%s%s", client_get_cur_dir(), lname
) < 0) {
2254 normalize_name(rname
);
2257 char *tmp_rname
= client_clean_name(ctx
, rname
);
2258 if (tmp_rname
== NULL
) {
2262 rname
= smb_xstrdup(tmp_rname
);
2263 TALLOC_FREE(tmp_rname
);
2264 if (rname
== NULL
) {
2268 do_put(rname
, lname
, false);
2270 free_file_list(file_list
);
2279 /****************************************************************************
2281 ****************************************************************************/
2283 static int do_cancel(int job
)
2285 NTSTATUS status
= cli_printjob_del(cli
, job
);
2287 if (NT_STATUS_IS_OK(status
)) {
2288 d_printf("Job %d cancelled\n",job
);
2291 d_printf("Error cancelling job %d : %s\n",
2292 job
, nt_errstr(status
));
2297 /****************************************************************************
2299 ****************************************************************************/
2301 static int cmd_cancel(void)
2303 TALLOC_CTX
*ctx
= talloc_tos();
2307 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
,NULL
)) {
2308 d_printf("cancel <jobid> ...\n");
2314 } while (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
));
2319 /****************************************************************************
2321 ****************************************************************************/
2323 static int cmd_print(void)
2325 TALLOC_CTX
*ctx
= talloc_tos();
2330 if (!next_token_talloc(ctx
, &cmd_ptr
, &lname
,NULL
)) {
2331 d_printf("print <filename>\n");
2335 rname
= talloc_strdup(ctx
, lname
);
2339 p
= strrchr_m(rname
,'/');
2341 rname
= talloc_asprintf(ctx
,
2346 if (strequal(lname
,"-")) {
2347 rname
= talloc_asprintf(ctx
,
2355 return do_put(rname
, lname
, false);
2358 /****************************************************************************
2359 Show a print queue entry.
2360 ****************************************************************************/
2362 static void queue_fn(struct print_job_info
*p
)
2364 d_printf("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
);
2367 /****************************************************************************
2369 ****************************************************************************/
2371 static int cmd_queue(void)
2373 cli_print_queue(cli
, queue_fn
);
2377 /****************************************************************************
2379 ****************************************************************************/
2381 static NTSTATUS
do_del(struct cli_state
*cli_state
, struct file_info
*finfo
,
2384 TALLOC_CTX
*ctx
= talloc_tos();
2386 struct cli_state
*targetcli
= NULL
;
2387 char *targetname
= NULL
;
2388 struct cli_credentials
*creds
= samba_cmdline_get_creds();
2391 mask
= talloc_asprintf(ctx
,
2397 return NT_STATUS_NO_MEMORY
;
2400 if (finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
) {
2402 return NT_STATUS_OK
;
2405 status
= cli_resolve_path(ctx
, "",
2407 cli
, mask
, &targetcli
, &targetname
);
2408 if (!NT_STATUS_IS_OK(status
)) {
2412 status
= cli_unlink(targetcli
, targetname
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2414 if (!NT_STATUS_IS_OK(status
)) {
2415 d_printf("%s deleting remote file %s\n",
2416 nt_errstr(status
), mask
);
2422 /****************************************************************************
2424 ****************************************************************************/
2426 static int cmd_del(void)
2428 TALLOC_CTX
*ctx
= talloc_tos();
2431 NTSTATUS status
= NT_STATUS_OK
;
2432 uint32_t attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
2435 attribute
|= FILE_ATTRIBUTE_DIRECTORY
;
2438 mask
= talloc_strdup(ctx
, client_get_cur_dir());
2442 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2443 d_printf("del <filename>\n");
2446 mask
= talloc_asprintf_append(mask
, "%s", buf
);
2450 mask
= client_clean_name(ctx
, mask
);
2455 status
= do_list(mask
,attribute
,do_del
,false,false);
2456 if (!NT_STATUS_IS_OK(status
)) {
2462 /****************************************************************************
2464 ****************************************************************************/
2466 static NTSTATUS
delete_remote_files_list(struct cli_state
*cli_state
,
2467 struct file_list
*flist
)
2469 NTSTATUS status
= NT_STATUS_OK
;
2470 struct file_list
*deltree_list_iter
= NULL
;
2471 char *targetname
= NULL
;
2472 struct cli_state
*targetcli
= NULL
;
2473 struct cli_credentials
*creds
= samba_cmdline_get_creds();
2474 TALLOC_CTX
*ctx
= talloc_tos();
2476 for (deltree_list_iter
= flist
;
2477 deltree_list_iter
!= NULL
;
2478 deltree_list_iter
= deltree_list_iter
->next
) {
2479 status
= cli_resolve_path(ctx
,
2483 deltree_list_iter
->file_path
,
2486 if (!NT_STATUS_IS_OK(status
)) {
2487 d_printf("delete_remote_files %s: %s\n",
2488 deltree_list_iter
->file_path
,
2492 if (CLI_DIRSEP_CHAR
== '/') {
2494 status
= cli_posix_unlink(targetcli
,
2496 } else if (deltree_list_iter
->isdir
) {
2497 status
= cli_rmdir(targetcli
,
2500 status
= cli_unlink(targetcli
,
2502 FILE_ATTRIBUTE_SYSTEM
|
2503 FILE_ATTRIBUTE_HIDDEN
);
2505 if (!NT_STATUS_IS_OK(status
)) {
2506 d_printf("%s deleting remote %s %s\n",
2508 deltree_list_iter
->isdir
?
2509 "directory" : "file",
2510 deltree_list_iter
->file_path
);
2514 return NT_STATUS_OK
;
2517 /****************************************************************************
2518 Save a list of files to delete.
2519 ****************************************************************************/
2521 static struct file_list
*deltree_list_head
;
2523 static NTSTATUS
do_deltree_list(struct cli_state
*cli_state
,
2524 struct file_info
*finfo
,
2527 struct file_list
**file_list_head_pp
= &deltree_list_head
;
2528 struct file_list
*dt
= NULL
;
2530 if (!do_this_one(finfo
)) {
2531 return NT_STATUS_OK
;
2534 /* skip if this is . or .. */
2535 if (ISDOT(finfo
->name
) || ISDOTDOT(finfo
->name
)) {
2536 return NT_STATUS_OK
;
2539 dt
= talloc_zero(NULL
, struct file_list
);
2541 return NT_STATUS_NO_MEMORY
;
2544 /* create absolute filename for cli_ntcreate() */
2545 dt
->file_path
= talloc_asprintf(dt
,
2550 if (dt
->file_path
== NULL
) {
2552 return NT_STATUS_NO_MEMORY
;
2555 if (finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
) {
2559 DLIST_ADD(*file_list_head_pp
, dt
);
2560 return NT_STATUS_OK
;
2563 static int cmd_deltree(void)
2565 TALLOC_CTX
*ctx
= talloc_tos();
2567 NTSTATUS status
= NT_STATUS_OK
;
2568 struct file_list
*deltree_list_norecurse
= NULL
;
2569 struct file_list
*deltree_list_iter
= NULL
;
2570 uint32_t attribute
= FILE_ATTRIBUTE_SYSTEM
|
2571 FILE_ATTRIBUTE_HIDDEN
|
2572 FILE_ATTRIBUTE_DIRECTORY
;
2574 char *mask
= talloc_strdup(ctx
, client_get_cur_dir());
2578 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
2580 d_printf("deltree <filename>\n");
2583 mask
= talloc_asprintf_append(mask
, "%s", buf
);
2587 mask
= client_clean_name(ctx
, mask
);
2592 deltree_list_head
= NULL
;
2595 * Get the list of directories to
2596 * delete (in case mask has a wildcard).
2598 status
= do_list(mask
, attribute
, do_deltree_list
, false, true);
2599 if (!NT_STATUS_IS_OK(status
)) {
2602 deltree_list_norecurse
= deltree_list_head
;
2603 deltree_list_head
= NULL
;
2605 for (deltree_list_iter
= deltree_list_norecurse
;
2606 deltree_list_iter
!= NULL
;
2607 deltree_list_iter
= deltree_list_iter
->next
) {
2609 if (deltree_list_iter
->isdir
== false) {
2610 char *targetname
= NULL
;
2611 struct cli_state
*targetcli
= NULL
;
2612 struct cli_credentials
*creds
= samba_cmdline_get_creds();
2613 status
= cli_resolve_path(ctx
,
2617 deltree_list_iter
->file_path
,
2620 if (!NT_STATUS_IS_OK(status
)) {
2623 /* Just a regular file. */
2624 if (CLI_DIRSEP_CHAR
== '/') {
2626 status
= cli_posix_unlink(targetcli
,
2629 status
= cli_unlink(targetcli
,
2631 FILE_ATTRIBUTE_SYSTEM
|
2632 FILE_ATTRIBUTE_HIDDEN
);
2634 if (!NT_STATUS_IS_OK(status
)) {
2641 * Get the list of files or directories to
2642 * delete in depth order.
2644 status
= do_list(deltree_list_iter
->file_path
,
2649 if (!NT_STATUS_IS_OK(status
)) {
2652 status
= delete_remote_files_list(cli
, deltree_list_head
);
2653 free_file_list(deltree_list_head
);
2654 deltree_list_head
= NULL
;
2655 if (!NT_STATUS_IS_OK(status
)) {
2660 free_file_list(deltree_list_norecurse
);
2661 free_file_list(deltree_list_head
);
2666 free_file_list(deltree_list_norecurse
);
2667 free_file_list(deltree_list_head
);
2668 deltree_list_head
= NULL
;
2673 /****************************************************************************
2674 Wildcard delete some files.
2675 ****************************************************************************/
2677 static int cmd_wdel(void)
2679 TALLOC_CTX
*ctx
= talloc_tos();
2683 struct cli_state
*targetcli
;
2684 char *targetname
= NULL
;
2685 struct cli_credentials
*creds
= samba_cmdline_get_creds();
2688 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2689 d_printf("wdel 0x<attrib> <wcard>\n");
2693 attribute
= (uint32_t)strtol(buf
, (char **)NULL
, 16);
2695 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2696 d_printf("wdel 0x<attrib> <wcard>\n");
2700 mask
= talloc_asprintf(ctx
, "%s%s",
2701 client_get_cur_dir(),
2706 mask
= client_clean_name(ctx
, mask
);
2711 status
= cli_resolve_path(ctx
, "",
2713 cli
, mask
, &targetcli
, &targetname
);
2714 if (!NT_STATUS_IS_OK(status
)) {
2715 d_printf("cmd_wdel %s: %s\n", mask
, nt_errstr(status
));
2719 status
= cli_unlink(targetcli
, targetname
, attribute
);
2720 if (!NT_STATUS_IS_OK(status
)) {
2721 d_printf("%s deleting remote files %s\n", nt_errstr(status
),
2727 /****************************************************************************
2728 ****************************************************************************/
2730 static int cmd_open(void)
2732 TALLOC_CTX
*ctx
= talloc_tos();
2735 char *targetname
= NULL
;
2736 struct cli_state
*targetcli
;
2737 uint16_t fnum
= (uint16_t)-1;
2738 struct cli_credentials
*creds
= samba_cmdline_get_creds();
2741 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2742 d_printf("open <filename>\n");
2745 mask
= talloc_asprintf(ctx
,
2747 client_get_cur_dir(),
2753 mask
= client_clean_name(ctx
, mask
);
2758 status
= cli_resolve_path(ctx
, "",
2760 cli
, mask
, &targetcli
, &targetname
);
2761 if (!NT_STATUS_IS_OK(status
)) {
2762 d_printf("open %s: %s\n", mask
, nt_errstr(status
));
2766 status
= cli_ntcreate(targetcli
, targetname
, 0,
2767 FILE_READ_DATA
|FILE_WRITE_DATA
, 0,
2768 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
2769 0x0, 0x0, &fnum
, NULL
);
2770 if (!NT_STATUS_IS_OK(status
)) {
2771 status
= cli_ntcreate(targetcli
, targetname
, 0,
2773 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
2774 0x0, 0x0, &fnum
, NULL
);
2775 if (NT_STATUS_IS_OK(status
)) {
2776 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
2778 d_printf("Failed to open file %s. %s\n",
2779 targetname
, nt_errstr(status
));
2782 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
2787 static int cmd_posix_encrypt(void)
2789 TALLOC_CTX
*ctx
= talloc_tos();
2790 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2791 char *domain
= NULL
;
2793 char *password
= NULL
;
2794 struct cli_credentials
*creds
= NULL
;
2795 struct cli_credentials
*lcreds
= NULL
;
2797 if (next_token_talloc(ctx
, &cmd_ptr
, &domain
, NULL
)) {
2799 if (!next_token_talloc(ctx
, &cmd_ptr
, &user
, NULL
)) {
2800 d_printf("posix_encrypt domain user password\n");
2804 if (!next_token_talloc(ctx
, &cmd_ptr
, &password
, NULL
)) {
2805 d_printf("posix_encrypt domain user password\n");
2809 lcreds
= cli_session_creds_init(ctx
,
2814 false, /* use_kerberos */
2815 false, /* fallback_after_kerberos */
2816 false, /* use_ccache */
2817 false); /* password_is_nt_hash */
2818 if (lcreds
== NULL
) {
2819 d_printf("cli_session_creds_init() failed.\n");
2824 bool auth_requested
= false;
2826 creds
= samba_cmdline_get_creds();
2828 auth_requested
= cli_credentials_authentication_requested(creds
);
2829 if (!auth_requested
) {
2830 d_printf("posix_encrypt domain user password\n");
2835 status
= cli_smb1_setup_encryption(cli
, creds
);
2836 /* gensec currently references the creds so we can't free them here */
2837 talloc_unlink(ctx
, lcreds
);
2838 if (!NT_STATUS_IS_OK(status
)) {
2839 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status
));
2843 d_printf("encryption on\n");
2844 ok
= cli_credentials_set_smb_encryption(creds
,
2845 SMB_ENCRYPTION_REQUIRED
,
2853 /****************************************************************************
2854 ****************************************************************************/
2856 static int cmd_posix_open(void)
2858 TALLOC_CTX
*ctx
= talloc_tos();
2861 char *targetname
= NULL
;
2862 struct cli_state
*targetcli
;
2865 struct cli_credentials
*creds
= samba_cmdline_get_creds();
2868 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2869 d_printf("posix_open <filename> 0<mode>\n");
2872 mask
= talloc_asprintf(ctx
,
2874 client_get_cur_dir(),
2879 mask
= client_clean_name(ctx
, mask
);
2884 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2885 d_printf("posix_open <filename> 0<mode>\n");
2888 if (CLI_DIRSEP_CHAR
!= '/') {
2889 d_printf("Command \"posix\" must be issued before "
2890 "the \"posix_open\" command can be used.\n");
2893 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
2895 status
= cli_resolve_path(ctx
, "",
2897 cli
, mask
, &targetcli
, &targetname
);
2898 if (!NT_STATUS_IS_OK(status
)) {
2899 d_printf("posix_open %s: %s\n", mask
, nt_errstr(status
));
2903 status
= cli_posix_open(targetcli
, targetname
, O_CREAT
|O_RDWR
, mode
,
2905 if (!NT_STATUS_IS_OK(status
)) {
2906 status
= cli_posix_open(targetcli
, targetname
,
2907 O_CREAT
|O_RDONLY
, mode
, &fnum
);
2908 if (!NT_STATUS_IS_OK(status
)) {
2909 d_printf("Failed to open file %s. %s\n", targetname
,
2912 d_printf("posix_open file %s: for readonly fnum %d\n",
2916 d_printf("posix_open file %s: for read/write fnum %d\n",
2923 static int cmd_posix_mkdir(void)
2925 TALLOC_CTX
*ctx
= talloc_tos();
2928 char *targetname
= NULL
;
2929 struct cli_state
*targetcli
;
2931 struct cli_credentials
*creds
= samba_cmdline_get_creds();
2934 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2935 d_printf("posix_mkdir <filename> 0<mode>\n");
2938 mask
= talloc_asprintf(ctx
,
2940 client_get_cur_dir(),
2945 mask
= client_clean_name(ctx
, mask
);
2950 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2951 d_printf("posix_mkdir <filename> 0<mode>\n");
2954 if (CLI_DIRSEP_CHAR
!= '/') {
2955 d_printf("Command \"posix\" must be issued before "
2956 "the \"posix_mkdir\" command can be used.\n");
2959 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
2961 status
= cli_resolve_path(ctx
, "",
2963 cli
, mask
, &targetcli
, &targetname
);
2964 if (!NT_STATUS_IS_OK(status
)) {
2965 d_printf("posix_mkdir %s: %s\n", mask
, nt_errstr(status
));
2969 status
= cli_posix_mkdir(targetcli
, targetname
, mode
);
2970 if (!NT_STATUS_IS_OK(status
)) {
2971 d_printf("Failed to open file %s. %s\n",
2972 targetname
, nt_errstr(status
));
2974 d_printf("posix_mkdir created directory %s\n", targetname
);
2979 static int cmd_posix_unlink(void)
2981 TALLOC_CTX
*ctx
= talloc_tos();
2984 char *targetname
= NULL
;
2985 struct cli_state
*targetcli
;
2986 struct cli_credentials
*creds
= samba_cmdline_get_creds();
2989 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2990 d_printf("posix_unlink <filename>\n");
2993 if (CLI_DIRSEP_CHAR
!= '/') {
2994 d_printf("Command \"posix\" must be issued before "
2995 "the \"posix_unlink\" command can be used.\n");
2998 mask
= talloc_asprintf(ctx
,
3000 client_get_cur_dir(),
3005 mask
= client_clean_name(ctx
, mask
);
3010 status
= cli_resolve_path(ctx
, "",
3012 cli
, mask
, &targetcli
, &targetname
);
3013 if (!NT_STATUS_IS_OK(status
)) {
3014 d_printf("posix_unlink %s: %s\n", mask
, nt_errstr(status
));
3018 status
= cli_posix_unlink(targetcli
, targetname
);
3019 if (!NT_STATUS_IS_OK(status
)) {
3020 d_printf("Failed to unlink file %s. %s\n",
3021 targetname
, nt_errstr(status
));
3023 d_printf("posix_unlink deleted file %s\n", targetname
);
3029 static int cmd_posix_rmdir(void)
3031 TALLOC_CTX
*ctx
= talloc_tos();
3034 char *targetname
= NULL
;
3035 struct cli_state
*targetcli
;
3036 struct cli_credentials
*creds
= samba_cmdline_get_creds();
3039 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3040 d_printf("posix_rmdir <filename>\n");
3043 if (CLI_DIRSEP_CHAR
!= '/') {
3044 d_printf("Command \"posix\" must be issued before "
3045 "the \"posix_rmdir\" command can be used.\n");
3048 mask
= talloc_asprintf(ctx
,
3050 client_get_cur_dir(),
3055 mask
= client_clean_name(ctx
, mask
);
3060 status
= cli_resolve_path(ctx
, "",
3062 cli
, mask
, &targetcli
, &targetname
);
3063 if (!NT_STATUS_IS_OK(status
)) {
3064 d_printf("posix_rmdir %s: %s\n", mask
, nt_errstr(status
));
3068 status
= cli_posix_rmdir(targetcli
, targetname
);
3069 if (!NT_STATUS_IS_OK(status
)) {
3070 d_printf("Failed to unlink directory %s. %s\n",
3071 targetname
, nt_errstr(status
));
3073 d_printf("posix_rmdir deleted directory %s\n", targetname
);
3079 static int cmd_mkfifo(void)
3081 TALLOC_CTX
*ctx
= talloc_tos();
3084 char *targetname
= NULL
;
3085 struct cli_state
*targetcli
;
3087 struct cli_credentials
*creds
= samba_cmdline_get_creds();
3090 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
3091 d_printf("mkfifo <filename> 0<mode>\n");
3094 mask
= talloc_asprintf(ctx
, "%s%s", client_get_cur_dir(), buf
);
3098 mask
= client_clean_name(ctx
, mask
);
3103 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
3104 d_printf("mkfifo <filename> 0<mode>\n");
3108 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
3109 if ((mode
& ~(S_IRWXU
| S_IRWXG
| S_IRWXO
)) != 0) {
3110 d_printf("mode %o can only contain permission bits\n", mode
);
3114 status
= cli_resolve_path(ctx
,
3121 if (!NT_STATUS_IS_OK(status
)) {
3122 d_printf("mkfifo %s: %s\n", mask
, nt_errstr(status
));
3126 status
= cli_mknod(targetcli
, targetname
, mode
| S_IFIFO
, 0);
3127 if (!NT_STATUS_IS_OK(status
)) {
3128 d_printf("Failed to open file %s. %s\n",
3132 d_printf("mkfifo created %s\n", targetname
);
3137 static int cmd_close(void)
3139 TALLOC_CTX
*ctx
= talloc_tos();
3144 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3145 d_printf("close <fnum>\n");
3150 /* We really should use the targetcli here.... */
3151 status
= cli_close(cli
, fnum
);
3152 if (!NT_STATUS_IS_OK(status
)) {
3153 d_printf("close %d: %s\n", fnum
, nt_errstr(status
));
3159 static int cmd_posix(void)
3161 TALLOC_CTX
*ctx
= talloc_tos();
3162 uint16_t major
, minor
;
3163 uint32_t caplow
, caphigh
;
3167 if (!smbXcli_conn_have_posix(cli
->conn
)) {
3168 d_printf("Server doesn't support UNIX CIFS extensions.\n");
3172 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB3_11
) {
3173 cli
->smb2
.client_smb311_posix
= true;
3177 status
= cli_unix_extensions_version(cli
, &major
, &minor
, &caplow
,
3179 if (!NT_STATUS_IS_OK(status
)) {
3180 d_printf("Can't get UNIX CIFS extensions version from "
3181 "server: %s\n", nt_errstr(status
));
3185 d_printf("Server supports CIFS extensions %" PRIu16
".%" PRIu16
"\n",
3189 caps
= talloc_strdup(ctx
, "");
3190 if (caplow
& CIFS_UNIX_FCNTL_LOCKS_CAP
) {
3191 talloc_asprintf_addbuf(&caps
, "locks ");
3193 if (caplow
& CIFS_UNIX_POSIX_ACLS_CAP
) {
3194 talloc_asprintf_addbuf(&caps
, "acls ");
3196 if (caplow
& CIFS_UNIX_XATTTR_CAP
) {
3197 talloc_asprintf_addbuf(&caps
, "eas ");
3199 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
3200 talloc_asprintf_addbuf(&caps
, "pathnames ");
3202 if (caplow
& CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP
) {
3203 talloc_asprintf_addbuf(&caps
, "posix_path_operations ");
3205 if (caplow
& CIFS_UNIX_LARGE_READ_CAP
) {
3206 talloc_asprintf_addbuf(&caps
, "large_read ");
3208 if (caplow
& CIFS_UNIX_LARGE_WRITE_CAP
) {
3209 talloc_asprintf_addbuf(&caps
, "large_write ");
3211 if (caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP
) {
3212 talloc_asprintf_addbuf(&caps
, "posix_encrypt ");
3214 if (caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP
) {
3215 talloc_asprintf_addbuf(&caps
, "mandatory_posix_encrypt ");
3222 if (*caps
&& caps
[strlen(caps
)-1] == ' ') {
3223 caps
[strlen(caps
)-1] = '\0';
3226 d_printf("Server supports CIFS capabilities %s\n", caps
);
3228 status
= cli_set_unix_extensions_capabilities(cli
, major
, minor
,
3230 if (!NT_STATUS_IS_OK(status
)) {
3231 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
3236 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
3237 CLI_DIRSEP_CHAR
= '/';
3238 *CLI_DIRSEP_STR
= '/';
3239 client_set_cur_dir(CLI_DIRSEP_STR
);
3245 static int cmd_lock(void)
3247 TALLOC_CTX
*ctx
= talloc_tos();
3249 uint64_t start
, len
;
3250 enum brl_type lock_type
;
3254 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3255 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3260 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3261 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3265 if (*buf
== 'r' || *buf
== 'R') {
3266 lock_type
= READ_LOCK
;
3267 } else if (*buf
== 'w' || *buf
== 'W') {
3268 lock_type
= WRITE_LOCK
;
3270 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3274 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3275 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3279 start
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
3281 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3282 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3286 if (CLI_DIRSEP_CHAR
!= '/') {
3287 d_printf("Command \"posix\" must be issued before "
3288 "the \"lock\" command can be used.\n");
3292 len
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
3294 status
= cli_posix_lock(cli
, fnum
, start
, len
, true, lock_type
);
3295 if (!NT_STATUS_IS_OK(status
)) {
3296 d_printf("lock failed %d: %s\n", fnum
, nt_errstr(status
));
3302 static int cmd_unlock(void)
3304 TALLOC_CTX
*ctx
= talloc_tos();
3306 uint64_t start
, len
;
3310 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3311 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3316 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3317 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3321 start
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
3323 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3324 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3328 if (CLI_DIRSEP_CHAR
!= '/') {
3329 d_printf("Command \"posix\" must be issued before "
3330 "the \"unlock\" command can be used.\n");
3334 len
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
3336 status
= cli_posix_unlock(cli
, fnum
, start
, len
);
3337 if (!NT_STATUS_IS_OK(status
)) {
3338 d_printf("unlock failed %d: %s\n", fnum
, nt_errstr(status
));
3344 static int cmd_posix_whoami(void)
3346 TALLOC_CTX
*ctx
= talloc_tos();
3347 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
3350 uint32_t num_gids
= 0;
3351 uint32_t num_sids
= 0;
3352 uint64_t *gids
= NULL
;
3353 struct dom_sid
*sids
= NULL
;
3357 if (CLI_DIRSEP_CHAR
!= '/') {
3358 d_printf("Command \"posix\" must be issued before "
3359 "the \"posix_whoami\" command can be used.\n");
3363 status
= cli_posix_whoami(cli
,
3373 if (!NT_STATUS_IS_OK(status
)) {
3374 d_printf("posix_whoami failed with error %s\n", nt_errstr(status
));
3378 d_printf("GUEST:%s\n", guest
? "True" : "False");
3379 d_printf("UID:%" PRIu64
"\n", uid
);
3380 d_printf("GID:%" PRIu64
"\n", gid
);
3381 d_printf("NUM_GIDS:%" PRIu32
"\n", num_gids
);
3382 for (i
= 0; i
< num_gids
; i
++) {
3383 d_printf("GIDS[%" PRIu32
"]:%" PRIu64
"\n", i
, gids
[i
]);
3385 d_printf("NUM_SIDS:%" PRIu32
"\n", num_sids
);
3386 for (i
= 0; i
< num_sids
; i
++) {
3387 struct dom_sid_buf buf
;
3388 d_printf("SIDS[%" PRIu32
"]:%s\n",
3390 dom_sid_str_buf(&sids
[i
], &buf
));
3396 /****************************************************************************
3398 ****************************************************************************/
3400 static int cmd_rmdir(void)
3402 TALLOC_CTX
*ctx
= talloc_tos();
3405 char *targetname
= NULL
;
3406 struct cli_state
*targetcli
;
3407 struct cli_credentials
*creds
= samba_cmdline_get_creds();
3410 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3411 d_printf("rmdir <dirname>\n");
3414 mask
= talloc_asprintf(ctx
,
3416 client_get_cur_dir(),
3421 mask
= client_clean_name(ctx
, mask
);
3426 status
= cli_resolve_path(ctx
, "",
3428 cli
, mask
, &targetcli
, &targetname
);
3429 if (!NT_STATUS_IS_OK(status
)) {
3430 d_printf("rmdir %s: %s\n", mask
, nt_errstr(status
));
3434 status
= cli_rmdir(targetcli
, targetname
);
3435 if (!NT_STATUS_IS_OK(status
)) {
3436 d_printf("%s removing remote directory file %s\n",
3437 nt_errstr(status
), mask
);
3443 /****************************************************************************
3445 ****************************************************************************/
3447 static int cmd_link(void)
3449 TALLOC_CTX
*ctx
= talloc_tos();
3450 char *oldname
= NULL
;
3451 char *newname
= NULL
;
3454 char *targetname
= NULL
;
3455 struct cli_state
*targetcli
;
3456 struct cli_credentials
*creds
= samba_cmdline_get_creds();
3459 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3460 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3461 d_printf("link <oldname> <newname>\n");
3464 oldname
= talloc_asprintf(ctx
,
3466 client_get_cur_dir(),
3471 oldname
= client_clean_name(ctx
, oldname
);
3472 if (oldname
== NULL
) {
3475 newname
= talloc_asprintf(ctx
,
3477 client_get_cur_dir(),
3482 newname
= client_clean_name(ctx
, newname
);
3483 if (newname
== NULL
) {
3487 status
= cli_resolve_path(ctx
, "",
3489 cli
, oldname
, &targetcli
, &targetname
);
3490 if (!NT_STATUS_IS_OK(status
)) {
3491 d_printf("link %s: %s\n", oldname
, nt_errstr(status
));
3495 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3496 d_printf("Server doesn't support UNIX CIFS calls.\n");
3500 if (CLI_DIRSEP_CHAR
!= '/') {
3501 d_printf("Command \"posix\" must be issued before "
3502 "the \"link\" command can be used.\n");
3506 status
= cli_posix_hardlink(targetcli
, targetname
, newname
);
3507 if (!NT_STATUS_IS_OK(status
)) {
3508 d_printf("%s linking files (%s -> %s)\n",
3509 nt_errstr(status
), newname
, oldname
);
3515 /****************************************************************************
3517 ****************************************************************************/
3519 static int cmd_readlink(void)
3521 TALLOC_CTX
*ctx
= talloc_tos();
3524 char *targetname
= NULL
;
3525 char *linkname
= NULL
;
3526 char *printname
= NULL
;
3528 struct cli_state
*targetcli
;
3529 struct cli_credentials
*creds
= samba_cmdline_get_creds();
3532 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3533 d_printf("readlink <name>\n");
3536 name
= talloc_asprintf(ctx
,
3538 client_get_cur_dir(),
3543 name
= client_clean_name(ctx
, name
);
3548 status
= cli_resolve_path(ctx
, "",
3550 cli
, name
, &targetcli
, &targetname
);
3551 if (!NT_STATUS_IS_OK(status
)) {
3552 d_printf("readlink %s: %s\n", name
, nt_errstr(status
));
3556 status
= cli_readlink(
3557 cli
, name
, talloc_tos(), &linkname
, &printname
, &flags
);
3558 if (!NT_STATUS_IS_OK(status
)) {
3559 d_printf("%s readlink on file %s\n",
3560 nt_errstr(status
), name
);
3564 d_printf("%s -> %s\n", name
, linkname
);
3566 TALLOC_FREE(linkname
);
3572 /****************************************************************************
3574 ****************************************************************************/
3576 static int cmd_symlink(void)
3578 TALLOC_CTX
*ctx
= talloc_tos();
3579 char *link_target
= NULL
;
3580 char *newname
= NULL
;
3583 struct cli_state
*newcli
;
3584 struct cli_credentials
*creds
= samba_cmdline_get_creds();
3587 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3588 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3589 d_printf("symlink <link_target> <newname>\n");
3592 /* Oldname (link target) must be an untouched blob. */
3595 if (SERVER_HAS_UNIX_CIFS(cli
)) {
3596 if (CLI_DIRSEP_CHAR
!= '/') {
3597 d_printf("Command \"posix\" must be issued before "
3598 "the \"symlink\" command can be used.\n");
3601 newname
= talloc_asprintf(ctx
, "%s%s", client_get_cur_dir(),
3606 newname
= client_clean_name(ctx
, newname
);
3607 if (newname
== NULL
) {
3610 /* New name must be present in share namespace. */
3611 status
= cli_resolve_path(ctx
, "",
3615 if (!NT_STATUS_IS_OK(status
)) {
3616 d_printf("link %s: %s\n", newname
,
3620 status
= cli_posix_symlink(newcli
, link_target
, newname
);
3622 status
= cli_symlink(
3623 cli
, link_target
, buf2
,
3624 buf2
[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE
);
3627 if (!NT_STATUS_IS_OK(status
)) {
3628 d_printf("%s symlinking files (%s -> %s)\n",
3629 nt_errstr(status
), newname
, link_target
);
3636 /****************************************************************************
3638 ****************************************************************************/
3640 static int cmd_chmod(void)
3642 TALLOC_CTX
*ctx
= talloc_tos();
3646 char *targetname
= NULL
;
3647 struct cli_state
*targetcli
;
3649 struct cli_credentials
*creds
= samba_cmdline_get_creds();
3652 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3653 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3654 d_printf("chmod mode file\n");
3657 src
= talloc_asprintf(ctx
,
3659 client_get_cur_dir(),
3664 src
= client_clean_name(ctx
, src
);
3669 mode
= (mode_t
)strtol(buf
, NULL
, 8);
3671 status
= cli_resolve_path(ctx
, "",
3673 cli
, src
, &targetcli
, &targetname
);
3674 if (!NT_STATUS_IS_OK(status
)) {
3675 d_printf("chmod %s: %s\n", src
, nt_errstr(status
));
3679 if (CLI_DIRSEP_CHAR
!= '/') {
3680 d_printf("Command \"posix\" must be issued before "
3681 "the \"chmod\" command can be used.\n");
3685 status
= cli_chmod(targetcli
, targetname
, mode
);
3686 if (!NT_STATUS_IS_OK(status
)) {
3687 d_printf("%s chmod file %s 0%o\n",
3688 nt_errstr(status
), src
, (unsigned int)mode
);
3695 static const char *filetype_to_str(mode_t mode
)
3697 if (S_ISREG(mode
)) {
3698 return "regular file";
3699 } else if (S_ISDIR(mode
)) {
3703 if (S_ISCHR(mode
)) {
3704 return "character device";
3708 if (S_ISBLK(mode
)) {
3709 return "block device";
3713 if (S_ISFIFO(mode
)) {
3718 if (S_ISLNK(mode
)) {
3719 return "symbolic link";
3723 if (S_ISSOCK(mode
)) {
3730 static char rwx_to_str(mode_t m
, mode_t bt
, char ret
)
3739 static char *unix_mode_to_str(char *s
, mode_t m
)
3742 const char *str
= filetype_to_str(m
);
3758 *p
++ = str
[1] == 'y' ? 'l' : 's';
3765 *p
++ = rwx_to_str(m
, S_IRUSR
, 'r');
3766 *p
++ = rwx_to_str(m
, S_IWUSR
, 'w');
3767 *p
++ = rwx_to_str(m
, S_IXUSR
, 'x');
3768 *p
++ = rwx_to_str(m
, S_IRGRP
, 'r');
3769 *p
++ = rwx_to_str(m
, S_IWGRP
, 'w');
3770 *p
++ = rwx_to_str(m
, S_IXGRP
, 'x');
3771 *p
++ = rwx_to_str(m
, S_IROTH
, 'r');
3772 *p
++ = rwx_to_str(m
, S_IWOTH
, 'w');
3773 *p
++ = rwx_to_str(m
, S_IXOTH
, 'x');
3778 /****************************************************************************
3779 Utility function for UNIX getfacl.
3780 ****************************************************************************/
3782 static char *perms_to_string(fstring permstr
, unsigned char perms
)
3784 fstrcpy(permstr
, "---");
3785 if (perms
& SMB_POSIX_ACL_READ
) {
3788 if (perms
& SMB_POSIX_ACL_WRITE
) {
3791 if (perms
& SMB_POSIX_ACL_EXECUTE
) {
3797 /****************************************************************************
3799 ****************************************************************************/
3801 static int cmd_getfacl(void)
3803 TALLOC_CTX
*ctx
= talloc_tos();
3806 char *targetname
= NULL
;
3807 struct cli_state
*targetcli
;
3808 uint16_t major
, minor
;
3809 uint32_t caplow
, caphigh
;
3810 char *retbuf
= NULL
;
3812 SMB_STRUCT_STAT sbuf
;
3813 size_t num_file_acls
= 0;
3814 size_t num_dir_acls
= 0;
3815 size_t expected_buflen
;
3817 struct cli_credentials
*creds
= samba_cmdline_get_creds();
3820 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
3821 d_printf("getfacl filename\n");
3824 src
= talloc_asprintf(ctx
,
3826 client_get_cur_dir(),
3831 src
= client_clean_name(ctx
, src
);
3836 status
= cli_resolve_path(ctx
, "",
3838 cli
, src
, &targetcli
, &targetname
);
3839 if (!NT_STATUS_IS_OK(status
)) {
3840 d_printf("stat %s: %s\n", src
, nt_errstr(status
));
3844 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3845 d_printf("Server doesn't support UNIX CIFS calls.\n");
3849 if (CLI_DIRSEP_CHAR
!= '/') {
3850 d_printf("Command \"posix\" must be issued before "
3851 "the \"getfacl\" command can be used.\n");
3855 status
= cli_unix_extensions_version(targetcli
, &major
, &minor
,
3857 if (!NT_STATUS_IS_OK(status
)) {
3858 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3863 if (!(caplow
& CIFS_UNIX_POSIX_ACLS_CAP
)) {
3864 d_printf("This server supports UNIX extensions "
3865 "but doesn't support POSIX ACLs.\n");
3869 status
= cli_posix_stat(targetcli
, targetname
, &sbuf
);
3870 if (!NT_STATUS_IS_OK(status
)) {
3871 d_printf("%s getfacl doing a stat on file %s\n",
3872 nt_errstr(status
), src
);
3876 status
= cli_posix_getacl(targetcli
, targetname
, ctx
, &rb_size
, &retbuf
);
3877 if (!NT_STATUS_IS_OK(status
)) {
3878 d_printf("%s getfacl file %s\n",
3879 nt_errstr(status
), src
);
3883 /* ToDo : Print out the ACL values. */
3884 if (rb_size
< 6 || SVAL(retbuf
,0) != SMB_POSIX_ACL_VERSION
) {
3885 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3886 src
, (unsigned int)CVAL(retbuf
,0) );
3890 num_file_acls
= SVAL(retbuf
,2);
3891 num_dir_acls
= SVAL(retbuf
,4);
3894 * No overflow check, num_*_acls comes from a 16-bit value,
3895 * and we expect expected_buflen (size_t) to be of at least 32
3898 expected_buflen
= SMB_POSIX_ACL_HEADER_SIZE
+
3899 SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
);
3901 if (rb_size
!= expected_buflen
) {
3902 d_printf("getfacl file %s, incorrect POSIX acl buffer size "
3903 "(should be %zu, was %zu).\n",
3910 d_printf("# file: %s\n", src
);
3911 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf
.st_ex_uid
, (unsigned int)sbuf
.st_ex_gid
);
3913 if (num_file_acls
== 0 && num_dir_acls
== 0) {
3914 d_printf("No acls found.\n");
3917 for (i
= 0; i
< num_file_acls
; i
++) {
3920 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
));
3921 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
3924 case SMB_POSIX_ACL_USER_OBJ
:
3927 case SMB_POSIX_ACL_USER
:
3928 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3929 d_printf("user:%u:", uorg
);
3931 case SMB_POSIX_ACL_GROUP_OBJ
:
3932 d_printf("group::");
3934 case SMB_POSIX_ACL_GROUP
:
3935 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3936 d_printf("group:%u:", uorg
);
3938 case SMB_POSIX_ACL_MASK
:
3941 case SMB_POSIX_ACL_OTHER
:
3942 d_printf("other::");
3945 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3946 src
, (unsigned int)tagtype
);
3951 d_printf("%s\n", perms_to_string(permstring
, perms
));
3954 for (i
= 0; i
< num_dir_acls
; i
++) {
3957 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
));
3958 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
3961 case SMB_POSIX_ACL_USER_OBJ
:
3962 d_printf("default:user::");
3964 case SMB_POSIX_ACL_USER
:
3965 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3966 d_printf("default:user:%u:", uorg
);
3968 case SMB_POSIX_ACL_GROUP_OBJ
:
3969 d_printf("default:group::");
3971 case SMB_POSIX_ACL_GROUP
:
3972 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3973 d_printf("default:group:%u:", uorg
);
3975 case SMB_POSIX_ACL_MASK
:
3976 d_printf("default:mask::");
3978 case SMB_POSIX_ACL_OTHER
:
3979 d_printf("default:other::");
3982 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3983 src
, (unsigned int)tagtype
);
3988 d_printf("%s\n", perms_to_string(permstring
, perms
));
3994 /****************************************************************************
3995 Get the EA list of a file
3996 ****************************************************************************/
3998 static int cmd_geteas(void)
4000 TALLOC_CTX
*ctx
= talloc_tos();
4003 char *targetname
= NULL
;
4004 struct cli_state
*targetcli
;
4007 struct ea_struct
*eas
;
4008 struct cli_credentials
*creds
= samba_cmdline_get_creds();
4010 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
4011 d_printf("geteas filename\n");
4014 src
= talloc_asprintf(ctx
,
4016 client_get_cur_dir(),
4021 src
= client_clean_name(ctx
, src
);
4026 status
= cli_resolve_path(ctx
, "",
4028 cli
, src
, &targetcli
, &targetname
);
4029 if (!NT_STATUS_IS_OK(status
)) {
4030 d_printf("stat %s: %s\n", src
, nt_errstr(status
));
4034 status
= cli_get_ea_list_path(targetcli
, targetname
, talloc_tos(),
4036 if (!NT_STATUS_IS_OK(status
)) {
4037 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status
));
4041 for (i
=0; i
<num_eas
; i
++) {
4042 d_printf("%s (%d) =\n", eas
[i
].name
, (int)eas
[i
].flags
);
4043 dump_data_file(eas
[i
].value
.data
, eas
[i
].value
.length
, false,
4053 /****************************************************************************
4055 ****************************************************************************/
4057 static int cmd_setea(void)
4059 TALLOC_CTX
*ctx
= talloc_tos();
4062 char *eaname
= NULL
;
4063 char *eavalue
= NULL
;
4064 char *targetname
= NULL
;
4065 struct cli_state
*targetcli
;
4066 struct cli_credentials
*creds
= samba_cmdline_get_creds();
4069 if (!next_token_talloc(ctx
, &cmd_ptr
, &name
, NULL
)
4070 || !next_token_talloc(ctx
, &cmd_ptr
, &eaname
, NULL
)) {
4071 d_printf("setea filename eaname value\n");
4074 if (!next_token_talloc(ctx
, &cmd_ptr
, &eavalue
, NULL
)) {
4075 eavalue
= talloc_strdup(ctx
, "");
4077 src
= talloc_asprintf(ctx
,
4079 client_get_cur_dir(),
4084 src
= client_clean_name(ctx
, src
);
4089 status
= cli_resolve_path(ctx
, "",
4091 cli
, src
, &targetcli
, &targetname
);
4092 if (!NT_STATUS_IS_OK(status
)) {
4093 d_printf("stat %s: %s\n", src
, nt_errstr(status
));
4097 status
= cli_set_ea_path(targetcli
, targetname
, eaname
, eavalue
,
4099 if (!NT_STATUS_IS_OK(status
)) {
4100 d_printf("set_ea %s: %s\n", src
, nt_errstr(status
));
4107 /****************************************************************************
4109 ****************************************************************************/
4111 static int cmd_stat(void)
4113 TALLOC_CTX
*ctx
= talloc_tos();
4116 char *targetname
= NULL
;
4117 struct cli_state
*targetcli
;
4119 SMB_STRUCT_STAT sbuf
;
4122 struct cli_credentials
*creds
= samba_cmdline_get_creds();
4125 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
4126 d_printf("stat file\n");
4129 src
= talloc_asprintf(ctx
,
4131 client_get_cur_dir(),
4136 src
= client_clean_name(ctx
, src
);
4141 status
= cli_resolve_path(ctx
, "",
4143 cli
, src
, &targetcli
, &targetname
);
4144 if (!NT_STATUS_IS_OK(status
)) {
4145 d_printf("stat %s: %s\n", src
, nt_errstr(status
));
4149 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
4150 d_printf("Server doesn't support UNIX CIFS calls.\n");
4154 if (CLI_DIRSEP_CHAR
!= '/') {
4155 d_printf("Command \"posix\" must be issued before "
4156 "the \"stat\" command can be used.\n");
4160 status
= cli_posix_stat(targetcli
, targetname
, &sbuf
);
4161 if (!NT_STATUS_IS_OK(status
)) {
4162 d_printf("%s stat file %s\n",
4163 nt_errstr(status
), src
);
4167 /* Print out the stat values. */
4168 d_printf("File: %s\n", src
);
4169 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
4170 (double)sbuf
.st_ex_size
,
4171 (unsigned int)sbuf
.st_ex_blocks
,
4172 filetype_to_str(sbuf
.st_ex_mode
));
4174 #if defined(S_ISCHR) && defined(S_ISBLK)
4175 if (S_ISCHR(sbuf
.st_ex_mode
) || S_ISBLK(sbuf
.st_ex_mode
)) {
4176 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
4177 (double)sbuf
.st_ex_ino
,
4178 (unsigned int)sbuf
.st_ex_nlink
,
4179 unix_dev_major(sbuf
.st_ex_rdev
),
4180 unix_dev_minor(sbuf
.st_ex_rdev
));
4183 d_printf("Inode: %.0f\tLinks: %u\n",
4184 (double)sbuf
.st_ex_ino
,
4185 (unsigned int)sbuf
.st_ex_nlink
);
4187 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
4188 ((int)sbuf
.st_ex_mode
& 0777),
4189 unix_mode_to_str(mode_str
, sbuf
.st_ex_mode
),
4190 (unsigned int)sbuf
.st_ex_uid
,
4191 (unsigned int)sbuf
.st_ex_gid
);
4193 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_atime
);
4194 lt
= localtime(&tmp_time
);
4196 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
4198 fstrcpy(mode_str
, "unknown");
4200 d_printf("Access: %s\n", mode_str
);
4202 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_mtime
);
4203 lt
= localtime(&tmp_time
);
4205 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
4207 fstrcpy(mode_str
, "unknown");
4209 d_printf("Modify: %s\n", mode_str
);
4211 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_ctime
);
4212 lt
= localtime(&tmp_time
);
4214 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
4216 fstrcpy(mode_str
, "unknown");
4218 d_printf("Change: %s\n", mode_str
);
4224 /****************************************************************************
4226 ****************************************************************************/
4228 static int cmd_chown(void)
4230 TALLOC_CTX
*ctx
= talloc_tos();
4234 char *buf
, *buf2
, *buf3
;
4235 struct cli_state
*targetcli
;
4236 char *targetname
= NULL
;
4237 struct cli_credentials
*creds
= samba_cmdline_get_creds();
4240 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
4241 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
) ||
4242 !next_token_talloc(ctx
, &cmd_ptr
,&buf3
,NULL
)) {
4243 d_printf("chown uid gid file\n");
4247 uid
= (uid_t
)atoi(buf
);
4248 gid
= (gid_t
)atoi(buf2
);
4250 src
= talloc_asprintf(ctx
,
4252 client_get_cur_dir(),
4257 src
= client_clean_name(ctx
, src
);
4261 status
= cli_resolve_path(ctx
, "",
4263 cli
, src
, &targetcli
, &targetname
);
4264 if (!NT_STATUS_IS_OK(status
)) {
4265 d_printf("chown %s: %s\n", src
, nt_errstr(status
));
4269 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
4270 d_printf("Server doesn't support UNIX CIFS calls.\n");
4274 if (CLI_DIRSEP_CHAR
!= '/') {
4275 d_printf("Command \"posix\" must be issued before "
4276 "the \"chown\" command can be used.\n");
4280 status
= cli_posix_chown(targetcli
, targetname
, uid
, gid
);
4281 if (!NT_STATUS_IS_OK(status
)) {
4282 d_printf("%s chown file %s uid=%d, gid=%d\n",
4283 nt_errstr(status
), src
, (int)uid
, (int)gid
);
4290 /****************************************************************************
4292 ****************************************************************************/
4294 static int cmd_rename(void)
4296 TALLOC_CTX
*ctx
= talloc_tos();
4299 struct cli_state
*targetcli
;
4302 struct cli_credentials
*creds
= samba_cmdline_get_creds();
4304 bool replace
= false;
4306 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
4307 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
4308 d_printf("rename <src> <dest> [-f]\n");
4312 src
= talloc_asprintf(ctx
,
4314 client_get_cur_dir(),
4319 src
= client_clean_name(ctx
, src
);
4324 dest
= talloc_asprintf(ctx
,
4326 client_get_cur_dir(),
4331 dest
= client_clean_name(ctx
, dest
);
4336 if (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
) &&
4337 strcsequal(buf
, "-f")) {
4341 status
= cli_resolve_path(ctx
, "",
4343 cli
, src
, &targetcli
, &targetsrc
);
4344 if (!NT_STATUS_IS_OK(status
)) {
4345 d_printf("rename %s: %s\n", src
, nt_errstr(status
));
4349 status
= cli_resolve_path(ctx
, "",
4351 cli
, dest
, &targetcli
, &targetdest
);
4352 if (!NT_STATUS_IS_OK(status
)) {
4353 d_printf("rename %s: %s\n", dest
, nt_errstr(status
));
4357 status
= cli_rename(targetcli
, targetsrc
, targetdest
, replace
);
4358 if (!NT_STATUS_IS_OK(status
)) {
4359 d_printf("%s renaming files %s -> %s \n",
4369 struct scopy_timing
{
4370 struct timespec tp_start
;
4373 static int scopy_status(off_t written
, void *priv
)
4375 struct timespec tp_end
;
4376 unsigned int scopy_total_time_ms
;
4377 struct scopy_timing
*st
= priv
;
4379 clock_gettime_mono(&tp_end
);
4380 scopy_total_time_ms
= nsec_time_diff(&tp_end
,&st
->tp_start
)/1000000;
4382 DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
4383 (intmax_t)written
, written
/ (1.024*scopy_total_time_ms
)));
4388 /****************************************************************************
4389 Server-Side copy some file.
4390 ****************************************************************************/
4392 static int cmd_scopy(void)
4394 TALLOC_CTX
*ctx
= talloc_tos();
4397 struct cli_state
*targetcli
;
4400 uint32_t DesiredAccess
, ShareAccess
, CreateDisposition
, CreateOptions
;
4401 struct smb_create_returns cr
;
4402 uint16_t destfnum
= (uint16_t)-1;
4403 uint16_t srcfnum
= (uint16_t)-1;
4405 struct scopy_timing st
;
4407 struct cli_credentials
*creds
= samba_cmdline_get_creds();
4410 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
4411 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
4412 d_printf("scopy <src> <dest>\n");
4416 src
= talloc_asprintf(ctx
,
4418 client_get_cur_dir(),
4423 src
= client_clean_name(ctx
, src
);
4428 dest
= talloc_asprintf(ctx
,
4430 client_get_cur_dir(),
4435 dest
= client_clean_name(ctx
, dest
);
4440 status
= cli_resolve_path(ctx
, "",
4442 cli
, src
, &targetcli
, &targetsrc
);
4443 if (!NT_STATUS_IS_OK(status
)) {
4444 d_printf("scopy %s: %s\n", src
, nt_errstr(status
));
4448 status
= cli_resolve_path(ctx
, "",
4450 cli
, dest
, &targetcli
, &targetdest
);
4451 if (!NT_STATUS_IS_OK(status
)) {
4452 d_printf("scopy %s: %s\n", dest
, nt_errstr(status
));
4457 DesiredAccess
= (FILE_READ_DATA
|FILE_READ_EA
|FILE_READ_ATTRIBUTES
|
4458 READ_CONTROL_ACCESS
|SYNCHRONIZE_ACCESS
);
4459 ShareAccess
= FILE_SHARE_READ
|FILE_SHARE_DELETE
;
4460 CreateDisposition
= FILE_OPEN
;
4461 CreateOptions
= (FILE_SEQUENTIAL_ONLY
|FILE_NON_DIRECTORY_FILE
|
4462 FILE_OPEN_REPARSE_POINT
);
4463 status
= cli_ntcreate(targetcli
, targetsrc
, 0, DesiredAccess
, 0,
4464 ShareAccess
, CreateDisposition
, CreateOptions
, 0x0,
4466 if (!NT_STATUS_IS_OK(status
)) {
4467 d_printf("Failed to open file %s. %s\n",
4468 targetsrc
, nt_errstr(status
));
4472 DesiredAccess
= (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_READ_EA
|
4473 FILE_WRITE_EA
|FILE_READ_ATTRIBUTES
|FILE_WRITE_ATTRIBUTES
|
4474 DELETE_ACCESS
|READ_CONTROL_ACCESS
|WRITE_DAC_ACCESS
|SYNCHRONIZE_ACCESS
);
4475 ShareAccess
= FILE_SHARE_NONE
;
4476 CreateDisposition
= FILE_CREATE
;
4477 CreateOptions
= FILE_SEQUENTIAL_ONLY
|FILE_NON_DIRECTORY_FILE
;
4478 status
= cli_ntcreate(targetcli
, targetdest
, 0, DesiredAccess
,
4479 FILE_ATTRIBUTE_ARCHIVE
, ShareAccess
, CreateDisposition
,
4480 CreateOptions
, 0x0, &destfnum
, NULL
);
4481 if (!NT_STATUS_IS_OK(status
)) {
4482 d_printf("Failed to create file %s. %s\n",
4483 targetdest
, nt_errstr(status
));
4484 cli_close(targetcli
, srcfnum
);
4488 clock_gettime_mono(&st
.tp_start
);
4489 status
= cli_splice(targetcli
, targetcli
, srcfnum
, destfnum
,
4490 cr
.end_of_file
, 0, 0, &written
, scopy_status
, &st
);
4491 if (!NT_STATUS_IS_OK(status
)) {
4492 d_printf("%s copying file %s -> %s \n",
4499 status
= cli_close(targetcli
, srcfnum
);
4500 if (!NT_STATUS_IS_OK(status
)) {
4501 d_printf("Error %s closing remote source file\n", nt_errstr(status
));
4504 status
= cli_close(targetcli
, destfnum
);
4505 if (!NT_STATUS_IS_OK(status
)) {
4506 d_printf("Error %s closing remote dest file\n", nt_errstr(status
));
4513 /****************************************************************************
4514 Print the volume name.
4515 ****************************************************************************/
4517 static int cmd_volume(void)
4520 uint32_t serial_num
;
4524 status
= cli_get_fs_volume_info(cli
, talloc_tos(),
4525 &volname
, &serial_num
,
4527 if (!NT_STATUS_IS_OK(status
)) {
4528 d_printf("Error %s getting volume info\n", nt_errstr(status
));
4532 d_printf("Volume: |%s| serial number 0x%x\n",
4533 volname
, (unsigned int)serial_num
);
4537 /****************************************************************************
4538 Hard link files using the NT call.
4539 ****************************************************************************/
4541 static int cmd_hardlink(void)
4543 TALLOC_CTX
*ctx
= talloc_tos();
4546 struct cli_state
*targetcli
;
4548 struct cli_credentials
*creds
= samba_cmdline_get_creds();
4551 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
4552 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
4553 d_printf("hardlink <src> <dest>\n");
4557 src
= talloc_asprintf(ctx
,
4559 client_get_cur_dir(),
4564 src
= client_clean_name(ctx
, src
);
4569 dest
= talloc_asprintf(ctx
,
4571 client_get_cur_dir(),
4576 dest
= client_clean_name(ctx
, dest
);
4581 status
= cli_resolve_path(ctx
, "",
4583 cli
, src
, &targetcli
, &targetname
);
4584 if (!NT_STATUS_IS_OK(status
)) {
4585 d_printf("hardlink %s: %s\n", src
, nt_errstr(status
));
4589 status
= cli_hardlink(targetcli
, targetname
, dest
);
4590 if (!NT_STATUS_IS_OK(status
)) {
4591 d_printf("%s doing an NT hard link of files\n",
4599 /****************************************************************************
4600 Toggle the prompt flag.
4601 ****************************************************************************/
4603 static int cmd_prompt(void)
4606 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
4610 /****************************************************************************
4611 Set the newer than time.
4612 ****************************************************************************/
4614 static int cmd_newer(void)
4616 TALLOC_CTX
*ctx
= talloc_tos();
4619 SMB_STRUCT_STAT sbuf
;
4621 ok
= next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
);
4622 if (ok
&& (sys_stat(buf
, &sbuf
, false) == 0)) {
4623 newer_than
= convert_timespec_to_time_t(sbuf
.st_ex_mtime
);
4624 DEBUG(1,("Getting files newer than %s",
4625 time_to_asc(newer_than
)));
4630 if (ok
&& newer_than
== 0) {
4631 d_printf("Error setting newer-than time\n");
4638 /****************************************************************************
4639 Watch directory changes
4640 ****************************************************************************/
4642 static int cmd_notify(void)
4644 TALLOC_CTX
*frame
= talloc_stackframe();
4649 name
= talloc_strdup(talloc_tos(), client_get_cur_dir());
4653 if (!next_token_talloc(talloc_tos(), &cmd_ptr
, &buf
, NULL
)) {
4656 name
= talloc_asprintf_append(name
, "%s", buf
);
4660 name
= client_clean_name(talloc_tos(), name
);
4664 status
= cli_ntcreate(
4665 cli
, name
, 0, FILE_READ_DATA
, 0,
4666 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
4667 FILE_OPEN
, 0, 0, &fnum
, NULL
);
4668 if (!NT_STATUS_IS_OK(status
)) {
4669 d_printf("Could not open file: %s\n", nt_errstr(status
));
4675 uint32_t num_changes
= 0;
4676 struct notify_change
*changes
= NULL
;
4678 status
= cli_notify(cli
, fnum
, 1000, FILE_NOTIFY_CHANGE_ALL
,
4680 talloc_tos(), &num_changes
, &changes
);
4681 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOTIFY_ENUM_DIR
)) {
4682 printf("NOTIFY_ENUM_DIR\n");
4683 status
= NT_STATUS_OK
;
4685 if (!NT_STATUS_IS_OK(status
)) {
4686 d_printf("notify returned %s\n",
4690 for (i
=0; i
<num_changes
; i
++) {
4691 printf("%4.4x %s\n", changes
[i
].action
,
4694 TALLOC_FREE(changes
);
4697 d_printf("notify <dir name>\n");
4703 /****************************************************************************
4704 Set the archive level.
4705 ****************************************************************************/
4707 static int cmd_archive(void)
4709 TALLOC_CTX
*ctx
= talloc_tos();
4712 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4713 archive_level
= atoi(buf
);
4715 d_printf("Archive level is %d\n",archive_level
);
4721 /****************************************************************************
4722 Toggle the backup_intent state.
4723 ****************************************************************************/
4725 static int cmd_backup(void)
4727 backup_intent
= !backup_intent
;
4728 cli_set_backup_intent(cli
, backup_intent
);
4729 DEBUG(2,("backup intent is now %s\n",backup_intent
?"on":"off"));
4733 /****************************************************************************
4734 Toggle the lowercaseflag.
4735 ****************************************************************************/
4737 static int cmd_lowercase(void)
4739 lowercase
= !lowercase
;
4740 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
4744 /****************************************************************************
4745 Toggle the case sensitive flag.
4746 ****************************************************************************/
4748 static int cmd_setcase(void)
4750 bool orig_case_sensitive
= cli_set_case_sensitive(cli
, false);
4752 cli_set_case_sensitive(cli
, !orig_case_sensitive
);
4753 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive
?
4758 /****************************************************************************
4759 Toggle the showacls flag.
4760 ****************************************************************************/
4762 static int cmd_showacls(void)
4764 showacls
= !showacls
;
4765 DEBUG(2,("showacls is now %s\n",showacls
?"on":"off"));
4770 /****************************************************************************
4771 Toggle the recurse flag.
4772 ****************************************************************************/
4774 static int cmd_recurse(void)
4777 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
4781 /****************************************************************************
4782 Toggle the translate flag.
4783 ****************************************************************************/
4785 static int cmd_translate(void)
4787 translation
= !translation
;
4788 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4789 translation
?"on":"off"));
4793 /****************************************************************************
4795 ****************************************************************************/
4797 static int cmd_lcd(void)
4799 TALLOC_CTX
*ctx
= talloc_tos();
4803 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4804 if (chdir(buf
) == -1) {
4805 d_printf("chdir to %s failed (%s)\n",
4806 buf
, strerror(errno
));
4813 DEBUG(2,("the local directory is now %s\n",d
));
4818 /****************************************************************************
4819 Get a file restarting at end of local file.
4820 ****************************************************************************/
4822 static int cmd_reget(void)
4824 TALLOC_CTX
*ctx
= talloc_tos();
4825 char *local_name
= NULL
;
4826 char *remote_name
= NULL
;
4830 remote_name
= talloc_strdup(ctx
, client_get_cur_dir());
4835 if (!next_token_talloc(ctx
, &cmd_ptr
, &fname
, NULL
)) {
4836 d_printf("reget <filename>\n");
4839 remote_name
= talloc_asprintf_append(remote_name
, "%s", fname
);
4843 remote_name
= client_clean_name(ctx
,remote_name
);
4849 next_token_talloc(ctx
, &cmd_ptr
, &p
, NULL
);
4854 return do_get(remote_name
, local_name
, true);
4857 /****************************************************************************
4858 Put a file restarting at end of local file.
4859 ****************************************************************************/
4861 static int cmd_reput(void)
4863 TALLOC_CTX
*ctx
= talloc_tos();
4864 char *local_name
= NULL
;
4865 char *remote_name
= NULL
;
4869 remote_name
= talloc_strdup(ctx
, client_get_cur_dir());
4874 if (!next_token_talloc(ctx
, &cmd_ptr
, &local_name
, NULL
)) {
4875 d_printf("reput <filename>\n");
4879 if (!file_exist_stat(local_name
, &st
, false)) {
4880 d_printf("%s does not exist\n", local_name
);
4884 if (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
4885 remote_name
= talloc_asprintf_append(remote_name
,
4888 remote_name
= talloc_asprintf_append(remote_name
,
4895 remote_name
= client_clean_name(ctx
, remote_name
);
4900 return do_put(remote_name
, local_name
, true);
4903 /****************************************************************************
4905 ****************************************************************************/
4907 static void browse_fn(const char *name
, uint32_t m
,
4908 const char *comment
, void *state
)
4910 const char *typestr
= "";
4913 case STYPE_DISKTREE
:
4917 typestr
= "Printer";
4926 /* FIXME: If the remote machine returns non-ascii characters
4927 in any of these fields, they can corrupt the output. We
4928 should remove them. */
4930 d_printf("\t%-15s %-10.10s%s\n",
4931 name
,typestr
,comment
);
4933 d_printf ("%s|%s|%s\n",typestr
,name
,comment
);
4937 static bool browse_host_rpc(bool sort
)
4940 struct rpc_pipe_client
*pipe_hnd
= NULL
;
4941 TALLOC_CTX
*frame
= talloc_stackframe();
4943 struct srvsvc_NetShareInfoCtr info_ctr
;
4944 struct srvsvc_NetShareCtr1 ctr1
;
4945 uint32_t resume_handle
= 0;
4946 uint32_t total_entries
= 0;
4948 struct dcerpc_binding_handle
*b
;
4950 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
,
4953 if (!NT_STATUS_IS_OK(status
)) {
4954 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4955 nt_errstr(status
)));
4960 b
= pipe_hnd
->binding_handle
;
4962 ZERO_STRUCT(info_ctr
);
4966 info_ctr
.ctr
.ctr1
= &ctr1
;
4968 status
= dcerpc_srvsvc_NetShareEnumAll(b
, frame
,
4976 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(werr
)) {
4977 TALLOC_FREE(pipe_hnd
);
4982 for (i
=0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
4983 struct srvsvc_NetShareInfo1 info
= info_ctr
.ctr
.ctr1
->array
[i
];
4984 browse_fn(info
.name
, info
.type
, info
.comment
, NULL
);
4987 TALLOC_FREE(pipe_hnd
);
4992 /****************************************************************************
4993 Try and browse available connections on a host.
4994 ****************************************************************************/
4996 static bool browse_host(bool sort
)
5001 d_printf("\n\tSharename Type Comment\n");
5002 d_printf("\t--------- ---- -------\n");
5005 if (browse_host_rpc(sort
)) {
5009 if (smbXcli_conn_protocol(cli
->conn
) > PROTOCOL_NT1
) {
5013 status
= cli_RNetShareEnum(cli
, browse_fn
, NULL
);
5014 if (!NT_STATUS_IS_OK(status
)) {
5015 d_printf("Error returning browse list: %s\n",
5023 /****************************************************************************
5025 ****************************************************************************/
5027 static void server_fn(const char *name
, uint32_t m
,
5028 const char *comment
, void *state
)
5032 d_printf("\t%-16s %s\n", name
, comment
);
5034 d_printf("%s|%s|%s\n",(char *)state
, name
, comment
);
5038 /****************************************************************************
5039 Try and browse available connections on a host.
5040 ****************************************************************************/
5042 static bool list_servers(const char *wk_grp
)
5046 if (!cli
->server_domain
)
5050 d_printf("\n\tServer Comment\n");
5051 d_printf("\t--------- -------\n");
5053 fstrcpy( state
, "Server" );
5054 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
,
5058 d_printf("\n\tWorkgroup Master\n");
5059 d_printf("\t--------- -------\n");
5062 fstrcpy( state
, "Workgroup" );
5063 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
,
5068 /****************************************************************************
5069 Print or set current VUID
5070 ****************************************************************************/
5072 static int cmd_vuid(void)
5074 TALLOC_CTX
*ctx
= talloc_tos();
5077 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
5078 d_printf("Current VUID is %d\n",
5079 cli_state_get_uid(cli
));
5083 cli_state_set_uid(cli
, atoi(buf
));
5087 /****************************************************************************
5088 Setup a new VUID, by issuing a session setup
5089 ****************************************************************************/
5091 static int cmd_logon(void)
5093 TALLOC_CTX
*ctx
= talloc_tos();
5094 char *l_username
, *l_password
;
5095 struct cli_credentials
*creds
= NULL
;
5098 if (!next_token_talloc(ctx
, &cmd_ptr
,&l_username
,NULL
)) {
5099 d_printf("logon <username> [<password>]\n");
5103 if (!next_token_talloc(ctx
, &cmd_ptr
,&l_password
,NULL
)) {
5104 char pwd
[256] = {0};
5107 rc
= samba_getpass("Password: ", pwd
, sizeof(pwd
), false, false);
5109 l_password
= talloc_strdup(ctx
, pwd
);
5116 creds
= cli_session_creds_init(ctx
,
5121 false, /* use_kerberos */
5122 false, /* fallback_after_kerberos */
5123 false, /* use_ccache */
5124 false); /* password_is_nt_hash */
5125 if (creds
== NULL
) {
5126 d_printf("cli_session_creds_init() failed.\n");
5129 nt_status
= cli_session_setup_creds(cli
, creds
);
5131 if (!NT_STATUS_IS_OK(nt_status
)) {
5132 d_printf("session setup failed: %s\n", nt_errstr(nt_status
));
5136 d_printf("Current VUID is %d\n", cli_state_get_uid(cli
));
5144 static int cmd_logoff(void)
5148 status
= cli_ulogoff(cli
);
5149 if (!NT_STATUS_IS_OK(status
)) {
5150 d_printf("logoff failed: %s\n", nt_errstr(status
));
5154 d_printf("logoff successful\n");
5160 * tree connect (connect to a share)
5163 static int cmd_tcon(void)
5165 TALLOC_CTX
*ctx
= talloc_tos();
5169 if (!next_token_talloc(ctx
, &cmd_ptr
, &sharename
, NULL
)) {
5170 d_printf("tcon <sharename>\n");
5178 status
= cli_tree_connect(cli
, sharename
, "?????", NULL
);
5179 if (!NT_STATUS_IS_OK(status
)) {
5180 d_printf("tcon failed: %s\n", nt_errstr(status
));
5184 d_printf("tcon to %s successful, tid: %u\n", sharename
,
5185 cli_state_get_tid(cli
));
5187 talloc_free(sharename
);
5193 * tree disconnect (disconnect from a share)
5196 static int cmd_tdis(void)
5200 status
= cli_tdis(cli
);
5201 if (!NT_STATUS_IS_OK(status
)) {
5202 d_printf("tdis failed: %s\n", nt_errstr(status
));
5206 d_printf("tdis successful\n");
5215 static int cmd_tid(void)
5217 TALLOC_CTX
*ctx
= talloc_tos();
5220 if (!next_token_talloc(ctx
, &cmd_ptr
, &tid_str
, NULL
)) {
5221 if (cli_state_has_tcon(cli
)) {
5222 d_printf("current tid is %d\n", cli_state_get_tid(cli
));
5224 d_printf("no tcon currently\n");
5227 uint32_t tid
= atoi(tid_str
);
5228 if (!cli_state_has_tcon(cli
)) {
5229 d_printf("no tcon currently\n");
5231 cli_state_set_tid(cli
, tid
);
5238 /****************************************************************************
5239 list active connections
5240 ****************************************************************************/
5242 static int cmd_list_connect(void)
5244 cli_cm_display(cli
);
5248 /****************************************************************************
5249 display the current active client connection
5250 ****************************************************************************/
5252 static int cmd_show_connect( void )
5254 TALLOC_CTX
*ctx
= talloc_tos();
5255 struct cli_state
*targetcli
;
5257 struct cli_credentials
*creds
= samba_cmdline_get_creds();
5260 status
= cli_resolve_path(ctx
, "",
5263 client_get_cur_dir(), &targetcli
,
5265 if (!NT_STATUS_IS_OK(status
)) {
5266 d_printf("showconnect %s: %s\n", cur_dir
, nt_errstr(status
));
5270 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli
->conn
), targetcli
->share
);
5275 * cmd_utimes - interactive command to set the four times
5277 * Read a filename and four times from the client command line and update
5278 * the file times. A value of -1 for a time means don't change.
5280 static int cmd_utimes(void)
5284 struct timespec times
[4] = {{0}};
5285 struct timeval_buf tbuf
[4];
5289 TALLOC_CTX
*ctx
= talloc_new(NULL
);
5296 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
5298 d_printf("utimes <filename> <create-time> <access-time> "
5299 "<write-time> <change-time>\n");
5300 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5301 "or -1 for no change\n");
5306 fname
= talloc_asprintf(ctx
,
5308 client_get_cur_dir(),
5310 if (fname
== NULL
) {
5314 fname
= client_clean_name(ctx
, fname
);
5315 if (fname
== NULL
) {
5320 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
) &&
5322 const char *s
= buf
;
5323 struct tm tm
= {0,};
5327 if (strlen(s
) == 2 && strcmp(s
, "-1") == 0) {
5328 times
[time_count
] = make_omit_timespec();
5333 ret
= strptime(s
, "%y:%m:%d-%H:%M:%S", &tm
);
5336 ret
= strptime(s
, "%Y:%m:%d-%H:%M:%S", &tm
);
5339 /* We could not match all the chars, so print error */
5340 if (ret
== NULL
|| *ret
!= 0) {
5341 d_printf("Invalid date format: %s\n", s
);
5342 d_printf("utimes <filename> <create-time> "
5343 "<access-time> <write-time> <change-time>\n");
5344 d_printf("Dates should be in [YY]YY:MM:DD-HH:MM:SS "
5345 "format or -1 for no change\n");
5350 /* Convert tm to a time_t */
5352 times
[time_count
] = (struct timespec
){.tv_sec
= t
};
5356 if (time_count
< 4) {
5357 d_printf("Insufficient dates: %d\n", time_count
);
5358 d_printf("utimes <filename> <create-time> <access-time> "
5359 "<write-time> <change-time>\n");
5360 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5361 "or -1 for no change\n");
5366 DEBUG(10, ("times\nCreate: %sAccess: %s Write: %sChange: %s\n",
5367 timespec_string_buf(×
[0], false, &tbuf
[0]),
5368 timespec_string_buf(×
[1], false, &tbuf
[1]),
5369 timespec_string_buf(×
[2], false, &tbuf
[2]),
5370 timespec_string_buf(×
[3], false, &tbuf
[3])));
5372 status
= cli_setpathinfo_ext(
5373 cli
, fname
, times
[0], times
[1], times
[2], times
[3],
5375 if (!NT_STATUS_IS_OK(status
)) {
5376 d_printf("cli_setpathinfo_ext failed: %s\n",
5387 * set_remote_attr - set DOS attributes of a remote file
5388 * @filename: path to the file name
5389 * @new_attr: attribute bit mask to use
5390 * @mode: one of ATTR_SET or ATTR_UNSET
5392 * Update the file attributes with the one provided.
5394 int set_remote_attr(const char *filename
, uint32_t new_attr
, int mode
)
5396 extern struct cli_state
*cli
;
5400 status
= cli_getatr(cli
, filename
, &old_attr
, NULL
, NULL
);
5401 if (!NT_STATUS_IS_OK(status
)) {
5402 d_printf("cli_getatr failed: %s\n", nt_errstr(status
));
5406 if (mode
== ATTR_SET
) {
5407 new_attr
|= old_attr
;
5409 new_attr
= old_attr
& ~new_attr
;
5412 status
= cli_setatr(cli
, filename
, new_attr
, 0);
5413 if (!NT_STATUS_IS_OK(status
)) {
5414 d_printf("cli_setatr failed: %s\n", nt_errstr(status
));
5422 * cmd_setmode - interactive command to set DOS attributes
5424 * Read a filename and mode from the client command line and update
5425 * the file DOS attributes.
5427 int cmd_setmode(void)
5431 uint32_t attr
[2] = {0};
5432 int mode
= ATTR_SET
;
5435 TALLOC_CTX
*ctx
= talloc_new(NULL
);
5440 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
5442 d_printf("setmode <filename> <[+|-]rsha>\n");
5447 fname
= talloc_asprintf(ctx
,
5449 client_get_cur_dir(),
5451 if (fname
== NULL
) {
5455 fname
= client_clean_name(ctx
, fname
);
5456 if (fname
== NULL
) {
5461 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
5462 const char *s
= buf
;
5473 attr
[mode
] |= FILE_ATTRIBUTE_READONLY
;
5476 attr
[mode
] |= FILE_ATTRIBUTE_HIDDEN
;
5479 attr
[mode
] |= FILE_ATTRIBUTE_SYSTEM
;
5482 attr
[mode
] |= FILE_ATTRIBUTE_ARCHIVE
;
5485 d_printf("setmode <filename> <perm=[+|-]rsha>\n");
5492 if (attr
[ATTR_SET
] == 0 && attr
[ATTR_UNSET
] == 0) {
5493 d_printf("setmode <filename> <[+|-]rsha>\n");
5498 DEBUG(2, ("perm set %d %d\n", attr
[ATTR_SET
], attr
[ATTR_UNSET
]));
5500 /* ignore return value: server might not store DOS attributes */
5501 set_remote_attr(fname
, attr
[ATTR_SET
], ATTR_SET
);
5502 set_remote_attr(fname
, attr
[ATTR_UNSET
], ATTR_UNSET
);
5508 /****************************************************************************
5510 ***************************************************************************/
5512 int cmd_iosize(void)
5514 TALLOC_CTX
*ctx
= talloc_tos();
5517 struct cli_credentials
*creds
= samba_cmdline_get_creds();
5519 (cli_credentials_get_smb_encryption(creds
) ==
5520 SMB_ENCRYPTION_REQUIRED
);
5522 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
5523 if (smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_SMB2_02
) {
5525 d_printf("iosize <n> or iosize 0x<n>. "
5526 "Minimum is 0 (default), "
5527 "max is 16776960 (0xFFFF00)\n");
5529 d_printf("iosize <n> or iosize 0x<n>. "
5530 "(Encrypted connection) ,"
5531 "Minimum is 0 (default), "
5532 "max is 130048 (0x1FC00)\n");
5535 d_printf("iosize <n> or iosize 0x<n>.\n");
5540 iosize
= strtol(buf
,NULL
,0);
5541 if (smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_SMB2_02
) {
5542 if (smb_encrypt
&& (iosize
< 0 || iosize
> 0xFC00)) {
5543 d_printf("iosize out of range for encrypted "
5544 "connection (min = 0 (default), "
5545 "max = 130048 (0x1FC00)\n");
5547 } else if (!smb_encrypt
&& (iosize
< 0 || iosize
> 0xFFFF00)) {
5548 d_printf("iosize out of range (min = 0 (default), "
5549 "max = 16776960 (0xFFFF00)\n");
5554 io_bufsize
= iosize
;
5555 d_printf("iosize is now %d\n", io_bufsize
);
5559 /****************************************************************************
5561 ***************************************************************************/
5563 static int cmd_timeout(void)
5565 TALLOC_CTX
*ctx
= talloc_tos();
5568 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
5569 unsigned int old_timeout
= cli_set_timeout(cli
, 0);
5570 cli_set_timeout(cli
, old_timeout
);
5571 d_printf("timeout <n> (per-operation timeout "
5572 "in seconds - currently %u).\n",
5577 io_timeout
= strtol(buf
,NULL
,0);
5578 cli_set_timeout(cli
, io_timeout
*1000);
5579 d_printf("io_timeout per operation is now %d\n", io_timeout
);
5584 /****************************************************************************
5586 ****************************************************************************/
5587 static int cmd_history(void)
5589 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
5593 hlist
= history_list();
5595 for (i
= 0; hlist
&& hlist
[i
]; i
++) {
5596 DEBUG(0, ("%d: %s\n", i
, hlist
[i
]->line
));
5599 DEBUG(0,("no history without readline support\n"));
5605 /* Some constants for completing filename arguments */
5607 #define COMPL_NONE 0 /* No completions */
5608 #define COMPL_REMOTE 1 /* Complete remote filename */
5609 #define COMPL_LOCAL 2 /* Complete local filename */
5611 /* This defines the commands supported by this client.
5612 * NOTE: The "!" must be the last one in the list because it's fn pointer
5613 * field is NULL, and NULL in that field is used in process_tok()
5614 * (below) to indicate the end of the list. crh
5619 const char *description
;
5620 char compl_args
[2]; /* Completion argument info */
5622 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
5623 {"allinfo",cmd_allinfo
,"<file> show all available info",
5624 {COMPL_REMOTE
,COMPL_NONE
}},
5625 {"altname",cmd_altname
,"<file> show alt name",{COMPL_REMOTE
,COMPL_NONE
}},
5626 {"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
}},
5627 {"backup",cmd_backup
,"toggle backup intent state",{COMPL_NONE
,COMPL_NONE
}},
5628 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
5629 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
5630 {"case_sensitive",cmd_setcase
,"toggle the case sensitive flag to server",{COMPL_NONE
,COMPL_NONE
}},
5631 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
5632 {"chmod",cmd_chmod
,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE
,COMPL_NONE
}},
5633 {"chown",cmd_chown
,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE
,COMPL_NONE
}},
5634 {"close",cmd_close
,"<fid> close a file given a fid",{COMPL_REMOTE
,COMPL_NONE
}},
5635 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5636 {"deltree",cmd_deltree
,"<mask> recursively delete all matching files and directories",{COMPL_REMOTE
,COMPL_NONE
}},
5637 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
5638 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
5639 {"echo",cmd_echo
,"ping the server",{COMPL_NONE
,COMPL_NONE
}},
5640 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
5641 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
5642 {"getfacl",cmd_getfacl
,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE
,COMPL_NONE
}},
5643 {"geteas", cmd_geteas
, "<file name> get the EA list of a file",
5644 {COMPL_REMOTE
, COMPL_NONE
}},
5645 {"hardlink",cmd_hardlink
,"<src> <dest> create a Windows hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
5646 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
5647 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
5648 {"iosize",cmd_iosize
,"iosize <number> (default 64512)",{COMPL_NONE
,COMPL_NONE
}},
5649 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
5650 {"link",cmd_link
,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
5651 {"lock",cmd_lock
,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
5652 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
5653 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
5654 {"l",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
5655 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
5656 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
5657 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5658 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
5659 {"mkfifo",cmd_mkfifo
,"<file mode> make a fifo",{COMPL_NONE
,COMPL_NONE
}},
5660 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
5661 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5662 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
5663 {"notify",cmd_notify
,"<file>Get notified of dir changes",{COMPL_REMOTE
,COMPL_NONE
}},
5664 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
5665 {"posix", cmd_posix
, "turn on all POSIX capabilities", {COMPL_REMOTE
,COMPL_NONE
}},
5666 {"posix_encrypt",cmd_posix_encrypt
,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE
,COMPL_NONE
}},
5667 {"posix_open",cmd_posix_open
,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5668 {"posix_mkdir",cmd_posix_mkdir
,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5669 {"posix_rmdir",cmd_posix_rmdir
,"<name> removes a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5670 {"posix_unlink",cmd_posix_unlink
,"<name> removes a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5671 {"posix_whoami",cmd_posix_whoami
,"return logged on user information "
5672 "using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5673 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
5674 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
5675 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
5676 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
5677 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
5678 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
5679 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
5680 {"readlink",cmd_readlink
,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
5681 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
5682 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
5683 {"reget",cmd_reget
,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE
,COMPL_LOCAL
}},
5684 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
5685 {"reput",cmd_reput
,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL
,COMPL_REMOTE
}},
5686 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5687 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_REMOTE
,COMPL_NONE
}},
5688 {"showacls",cmd_showacls
,"toggle if ACLs are shown or not",{COMPL_NONE
,COMPL_NONE
}},
5689 {"setea", cmd_setea
, "<file name> <eaname> <eaval> Set an EA of a file",
5690 {COMPL_REMOTE
, COMPL_LOCAL
}},
5691 {"setmode",cmd_setmode
,"<file name> <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
5692 {"scopy",cmd_scopy
,"<src> <dest> server-side copy file",{COMPL_REMOTE
,COMPL_REMOTE
}},
5693 {"stat",cmd_stat
,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE
,COMPL_NONE
}},
5694 {"symlink",cmd_symlink
,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
5695 {"tar",cmd_tar
,"tar <c|x>[IXFvbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
5696 {"tarmode",cmd_tarmode
,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
5697 {"timeout",cmd_timeout
,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE
,COMPL_NONE
}},
5698 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
5699 {"unlock",cmd_unlock
,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
5700 {"volume",cmd_volume
,"print the volume name",{COMPL_NONE
,COMPL_NONE
}},
5701 {"vuid",cmd_vuid
,"change current vuid",{COMPL_NONE
,COMPL_NONE
}},
5702 {"wdel",cmd_wdel
,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5703 {"logon",cmd_logon
,"establish new logon",{COMPL_NONE
,COMPL_NONE
}},
5704 {"listconnect",cmd_list_connect
,"list open connections",{COMPL_NONE
,COMPL_NONE
}},
5705 {"showconnect",cmd_show_connect
,"display the current active connection",{COMPL_NONE
,COMPL_NONE
}},
5706 {"tcon",cmd_tcon
,"connect to a share" ,{COMPL_NONE
,COMPL_NONE
}},
5707 {"tdis",cmd_tdis
,"disconnect from a share",{COMPL_NONE
,COMPL_NONE
}},
5708 {"tid",cmd_tid
,"show or set the current tid (tree-id)",{COMPL_NONE
,COMPL_NONE
}},
5709 {"utimes", cmd_utimes
,"<file name> <create_time> <access_time> <mod_time> "
5710 "<ctime> set times", {COMPL_REMOTE
,COMPL_NONE
}},
5711 {"logoff",cmd_logoff
,"log off (close the session)",{COMPL_NONE
,COMPL_NONE
}},
5712 {"..",cmd_cd_oneup
,"change the remote directory (up one level)",{COMPL_REMOTE
,COMPL_NONE
}},
5714 /* Yes, this must be here, see crh's comment above. */
5715 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
5716 {NULL
,NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
5719 /*******************************************************************
5720 Lookup a command string in the list of commands, including
5722 ******************************************************************/
5724 static int process_tok(char *tok
)
5726 size_t i
= 0, matches
= 0;
5728 size_t tok_len
= strlen(tok
);
5730 while (commands
[i
].fn
!= NULL
) {
5731 if (strequal(commands
[i
].name
,tok
)) {
5735 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
5744 else if (matches
== 1)
5750 /****************************************************************************
5752 ****************************************************************************/
5754 static int cmd_help(void)
5756 TALLOC_CTX
*ctx
= talloc_tos();
5760 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
5761 if ((i
= process_tok(buf
)) >= 0)
5762 d_printf("HELP %s:\n\t%s\n\n",
5763 commands
[i
].name
,commands
[i
].description
);
5765 while (commands
[i
].description
) {
5766 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
5767 d_printf("%-15s",commands
[i
].name
);
5776 /****************************************************************************
5777 Process a -c command string.
5778 ****************************************************************************/
5780 static int process_command_string(const char *cmd_in
)
5782 TALLOC_CTX
*ctx
= talloc_tos();
5783 char *cmd
= talloc_strdup(ctx
, cmd_in
);
5785 struct cli_credentials
*creds
= samba_cmdline_get_creds();
5790 /* establish the connection if not already */
5795 status
= cli_cm_open(talloc_tos(), NULL
,
5799 have_ip
? &dest_ss
: NULL
, port
,
5802 if (!NT_STATUS_IS_OK(status
)) {
5805 cli_set_timeout(cli
, io_timeout
*1000);
5808 while (cmd
[0] != '\0') {
5814 if ((p
= strchr_m(cmd
, ';')) == 0) {
5823 /* and get the first part of the command */
5825 if (!next_token_talloc(ctx
, &cmd_ptr
,&tok
,NULL
)) {
5829 if ((i
= process_tok(tok
)) >= 0) {
5830 rc
= commands
[i
].fn();
5831 } else if (i
== -2) {
5832 d_printf("%s: command abbreviation ambiguous\n",tok
);
5834 d_printf("%s: command not found\n",tok
);
5841 #define MAX_COMPLETIONS 100
5843 struct completion_remote
{
5851 static NTSTATUS
completion_remote_filter(struct file_info
*f
,
5855 struct completion_remote
*info
= (struct completion_remote
*)state
;
5857 if (info
->count
>= MAX_COMPLETIONS
- 1) {
5858 return NT_STATUS_OK
;
5860 if (strncmp(info
->text
, f
->name
, info
->len
) != 0) {
5861 return NT_STATUS_OK
;
5863 if (ISDOT(f
->name
) || ISDOTDOT(f
->name
)) {
5864 return NT_STATUS_OK
;
5867 if ((info
->dirmask
[0] == 0) && !(f
->attr
& FILE_ATTRIBUTE_DIRECTORY
))
5868 info
->matches
[info
->count
] = SMB_STRDUP(f
->name
);
5870 TALLOC_CTX
*ctx
= talloc_stackframe();
5873 tmp
= talloc_strdup(ctx
,info
->dirmask
);
5876 return NT_STATUS_NO_MEMORY
;
5878 tmp
= talloc_asprintf_append(tmp
, "%s", f
->name
);
5881 return NT_STATUS_NO_MEMORY
;
5883 if (f
->attr
& FILE_ATTRIBUTE_DIRECTORY
) {
5884 tmp
= talloc_asprintf_append(tmp
, "%s",
5889 return NT_STATUS_NO_MEMORY
;
5891 info
->matches
[info
->count
] = SMB_STRDUP(tmp
);
5894 if (info
->matches
[info
->count
] == NULL
) {
5895 return NT_STATUS_OK
;
5897 if (f
->attr
& FILE_ATTRIBUTE_DIRECTORY
) {
5898 smb_readline_ca_char(0);
5900 if (info
->count
== 1) {
5901 info
->samelen
= strlen(info
->matches
[info
->count
]);
5903 while (strncmp(info
->matches
[info
->count
],
5904 info
->matches
[info
->count
-1],
5905 info
->samelen
) != 0) {
5910 return NT_STATUS_OK
;
5913 static char **remote_completion(const char *text
, int len
)
5915 TALLOC_CTX
*ctx
= talloc_stackframe();
5916 char *dirmask
= NULL
;
5917 char *targetpath
= NULL
;
5918 struct cli_state
*targetcli
= NULL
;
5920 struct completion_remote info
= { NULL
, NULL
, 1, 0, NULL
, 0 };
5921 struct cli_credentials
*creds
= samba_cmdline_get_creds();
5924 /* can't have non-static initialisation on Sun CC, so do it
5930 info
.matches
= SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS
);
5931 if (!info
.matches
) {
5937 * We're leaving matches[0] free to fill it later with the text to
5938 * display: Either the one single match or the longest common subset
5941 info
.matches
[0] = NULL
;
5944 for (i
= len
-1; i
>= 0; i
--) {
5945 if ((text
[i
] == '/') || (text
[i
] == CLI_DIRSEP_CHAR
)) {
5950 info
.text
= text
+i
+1;
5951 info
.samelen
= info
.len
= len
-i
-1;
5954 info
.dirmask
= SMB_MALLOC_ARRAY(char, i
+2);
5955 if (!info
.dirmask
) {
5958 strncpy(info
.dirmask
, text
, i
+1);
5959 info
.dirmask
[i
+1] = 0;
5960 dirmask
= talloc_asprintf(ctx
,
5962 client_get_cur_dir(),
5966 info
.dirmask
= SMB_STRDUP("");
5967 if (!info
.dirmask
) {
5970 dirmask
= talloc_asprintf(ctx
,
5972 client_get_cur_dir());
5977 dirmask
= client_clean_name(ctx
, dirmask
);
5978 if (dirmask
== NULL
) {
5982 status
= cli_resolve_path(ctx
, "",
5984 cli
, dirmask
, &targetcli
, &targetpath
);
5985 if (!NT_STATUS_IS_OK(status
)) {
5988 status
= cli_list(targetcli
, targetpath
, FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
5989 completion_remote_filter
, (void *)&info
);
5990 if (!NT_STATUS_IS_OK(status
)) {
5994 if (info
.count
== 1) {
5996 * No matches at all, NULL indicates there is nothing
5998 SAFE_FREE(info
.matches
[0]);
5999 SAFE_FREE(info
.matches
);
6004 if (info
.count
== 2) {
6006 * Exactly one match in matches[1], indicate this is the one
6009 info
.matches
[0] = info
.matches
[1];
6010 info
.matches
[1] = NULL
;
6013 return info
.matches
;
6017 * We got more than one possible match, set the result to the maximum
6021 info
.matches
[0] = SMB_STRNDUP(info
.matches
[1], info
.samelen
);
6022 info
.matches
[info
.count
] = NULL
;
6024 return info
.matches
;
6027 for (i
= 0; i
< info
.count
; i
++) {
6028 SAFE_FREE(info
.matches
[i
]);
6030 SAFE_FREE(info
.matches
);
6031 SAFE_FREE(info
.dirmask
);
6036 static char **completion_fn(const char *text
, int start
, int end
)
6038 smb_readline_ca_char(' ');
6041 const char *buf
, *sp
;
6045 buf
= smb_readline_get_line_buffer();
6049 sp
= strchr(buf
, ' ');
6053 for (i
= 0; commands
[i
].name
; i
++) {
6054 if ((strncmp(commands
[i
].name
, buf
, sp
- buf
) == 0) &&
6055 (commands
[i
].name
[sp
- buf
] == 0)) {
6059 if (commands
[i
].name
== NULL
)
6065 if (sp
== (buf
+ start
))
6066 compl_type
= commands
[i
].compl_args
[0];
6068 compl_type
= commands
[i
].compl_args
[1];
6070 if (compl_type
== COMPL_REMOTE
)
6071 return remote_completion(text
, end
- start
);
6072 else /* fall back to local filename completion */
6076 size_t i
, len
, samelen
= 0, count
=1;
6078 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
6085 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
6086 if (strncmp(text
, commands
[i
].name
, len
) == 0) {
6087 matches
[count
] = SMB_STRDUP(commands
[i
].name
);
6088 if (!matches
[count
])
6091 samelen
= strlen(matches
[count
]);
6093 while (strncmp(matches
[count
], matches
[count
-1], samelen
) != 0)
6100 case 0: /* should never happen */
6104 matches
[0] = SMB_STRDUP(matches
[1]);
6107 matches
[0] = (char *)SMB_MALLOC(samelen
+1);
6110 strncpy(matches
[0], matches
[1], samelen
);
6111 matches
[0][samelen
] = 0;
6113 matches
[count
] = NULL
;
6117 for (i
= 0; i
< count
; i
++)
6125 static bool finished
;
6127 /****************************************************************************
6128 Make sure we swallow keepalives during idle time.
6129 ****************************************************************************/
6131 static void readline_callback(void)
6133 static time_t last_t
;
6134 struct timespec now
;
6137 unsigned char garbage
[16];
6139 clock_gettime_mono(&now
);
6147 /* Ping the server to keep the connection alive using SMBecho. */
6148 memset(garbage
, 0xf0, sizeof(garbage
));
6149 status
= cli_echo(cli
, 1, data_blob_const(garbage
, sizeof(garbage
)));
6150 if (NT_STATUS_IS_OK(status
) ||
6151 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
6153 * Even if server returns NT_STATUS_INVALID_PARAMETER
6154 * it still responded.
6155 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
6160 if (!cli_state_is_connected(cli
)) {
6161 DEBUG(0,("SMBecho failed (%s). The connection is "
6162 "disconnected now\n", nt_errstr(status
)));
6164 smb_readline_done();
6168 /****************************************************************************
6169 Process commands on stdin.
6170 ****************************************************************************/
6172 static int process_stdin(void)
6177 d_printf("Try \"help\" to get a list of possible commands.\n");
6181 TALLOC_CTX
*frame
= talloc_stackframe();
6183 char *the_prompt
= NULL
;
6187 /* display a prompt */
6188 the_prompt
= talloc_asprintf(frame
,
6190 client_get_cur_dir());
6191 if (the_prompt
== NULL
) {
6195 line
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
6201 /* special case - first char is ! */
6203 if (system(line
+ 1) == -1) {
6204 d_printf("system() command %s failed.\n",
6212 /* and get the first part of the command */
6214 if (!next_token_talloc(frame
, &cmd_ptr
,&tok
,NULL
)) {
6220 if ((i
= process_tok(tok
)) >= 0) {
6221 rc
= commands
[i
].fn();
6222 } else if (i
== -2) {
6223 d_printf("%s: command abbreviation ambiguous\n",tok
);
6225 d_printf("%s: command not found\n",tok
);
6233 /****************************************************************************
6234 Process commands from the client.
6235 ****************************************************************************/
6237 static int process(const char *base_directory
)
6241 struct cli_credentials
*creds
= samba_cmdline_get_creds();
6243 status
= cli_cm_open(talloc_tos(), NULL
,
6247 have_ip
? &dest_ss
: NULL
, port
,
6249 if (!NT_STATUS_IS_OK(status
)) {
6253 cli_set_timeout(cli
, io_timeout
*1000);
6255 if (base_directory
&& *base_directory
) {
6256 rc
= do_cd(base_directory
);
6264 rc
= process_command_string(cmdstr
);
6273 /****************************************************************************
6275 ****************************************************************************/
6277 static int do_host_query(struct loadparm_context
*lp_ctx
,
6278 const char *query_host
)
6281 struct cli_credentials
*creds
= samba_cmdline_get_creds();
6283 status
= cli_cm_open(talloc_tos(), NULL
,
6287 have_ip
? &dest_ss
: NULL
, port
,
6289 if (!NT_STATUS_IS_OK(status
)) {
6293 cli_set_timeout(cli
, io_timeout
*1000);
6296 /* Ensure that the host can do IPv4 */
6298 if (!interpret_addr(query_host
)) {
6299 struct sockaddr_storage ss
;
6300 if (interpret_string_addr(&ss
, query_host
, 0) &&
6301 (ss
.ss_family
!= AF_INET
)) {
6302 d_printf("%s is an IPv6 address -- no workgroup available\n",
6308 if (lp_client_min_protocol() > PROTOCOL_NT1
) {
6309 d_printf("SMB1 disabled -- no workgroup available\n");
6313 if (lp_disable_netbios()) {
6314 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
6318 if (port
!= NBT_SMB_PORT
||
6319 smbXcli_conn_protocol(cli
->conn
) > PROTOCOL_NT1
)
6322 * Workgroups simply don't make sense over anything
6323 * else but port 139 and SMB1.
6327 d_printf("Reconnecting with SMB1 for workgroup listing.\n");
6328 lpcfg_set_cmdline(lp_ctx
, "client max protocol", "NT1");
6329 status
= cli_cm_open(talloc_tos(), NULL
,
6333 have_ip
? &dest_ss
: NULL
, NBT_SMB_PORT
,
6335 if (!NT_STATUS_IS_OK(status
)) {
6336 d_printf("Unable to connect with SMB1 "
6337 "-- no workgroup available\n");
6342 cli_set_timeout(cli
, io_timeout
*1000);
6343 list_servers(lp_workgroup());
6350 /****************************************************************************
6351 Handle a tar operation.
6352 ****************************************************************************/
6354 static int do_tar_op(const char *base_directory
)
6356 struct tar
*tar_ctx
= tar_get_ctx();
6358 struct cli_credentials
*creds
= samba_cmdline_get_creds();
6360 /* do we already have a connection? */
6364 status
= cli_cm_open(talloc_tos(), NULL
,
6368 have_ip
? &dest_ss
: NULL
, port
,
6370 if (!NT_STATUS_IS_OK(status
)) {
6374 cli_set_timeout(cli
, io_timeout
*1000);
6379 if (base_directory
&& *base_directory
) {
6380 ret
= do_cd(base_directory
);
6386 ret
= tar_process(tar_ctx
);
6394 /****************************************************************************
6395 Handle a message operation.
6396 ****************************************************************************/
6398 static int do_message_op(struct cli_credentials
*creds
)
6402 if (lp_disable_netbios()) {
6403 d_printf("NetBIOS over TCP disabled.\n");
6407 status
= cli_connect_nb(talloc_tos(),
6408 desthost
, have_ip
? &dest_ss
: NULL
,
6409 port
? port
: NBT_SMB_PORT
, name_type
,
6414 if (!NT_STATUS_IS_OK(status
)) {
6415 d_printf("Connection to %s failed. Error %s\n", desthost
, nt_errstr(status
));
6419 cli_set_timeout(cli
, io_timeout
*1000);
6420 send_message(cli_credentials_get_username(creds
));
6426 /****************************************************************************
6428 ****************************************************************************/
6430 int main(int argc
,char *argv
[])
6432 const char **const_argv
= discard_const_p(const char *, argv
);
6433 char *base_directory
= NULL
;
6435 char *query_host
= NULL
;
6436 bool message
= false;
6437 static const char *new_name_resolve_order
= NULL
;
6441 bool tar_opt
= false;
6442 bool service_opt
= false;
6443 struct tar
*tar_ctx
= tar_get_ctx();
6446 struct poptOption long_options
[] = {
6450 .longName
= "message",
6452 .argInfo
= POPT_ARG_STRING
,
6455 .descrip
= "Send message",
6456 .argDescrip
= "HOST",
6459 .longName
= "ip-address",
6461 .argInfo
= POPT_ARG_STRING
,
6464 .descrip
= "Use this IP to connect to",
6468 .longName
= "stderr",
6470 .argInfo
= POPT_ARG_NONE
,
6473 .descrip
= "Write messages to stderr instead of stdout",
6478 .argInfo
= POPT_ARG_STRING
,
6481 .descrip
= "Get a list of shares available on a host",
6482 .argDescrip
= "HOST",
6487 .argInfo
= POPT_ARG_STRING
,
6490 .descrip
= "Command line tar",
6491 .argDescrip
= "<c|x>IXFvgbNan",
6494 .longName
= "directory",
6496 .argInfo
= POPT_ARG_STRING
,
6499 .descrip
= "Start from directory",
6500 .argDescrip
= "DIR",
6503 .longName
= "command",
6505 .argInfo
= POPT_ARG_STRING
,
6508 .descrip
= "Execute semicolon separated commands",
6511 .longName
= "send-buffer",
6513 .argInfo
= POPT_ARG_INT
,
6516 .descrip
= "Changes the transmit/send buffer",
6517 .argDescrip
= "BYTES",
6520 .longName
= "timeout",
6522 .argInfo
= POPT_ARG_INT
,
6525 .descrip
= "Changes the per-operation timeout",
6526 .argDescrip
= "SECONDS",
6531 .argInfo
= POPT_ARG_INT
,
6534 .descrip
= "Port to connect to",
6535 .argDescrip
= "PORT",
6538 .longName
= "grepable",
6540 .argInfo
= POPT_ARG_NONE
,
6543 .descrip
= "Produce grepable output",
6546 .longName
= "quiet",
6548 .argInfo
= POPT_ARG_NONE
,
6551 .descrip
= "Suppress help message",
6554 .longName
= "browse",
6556 .argInfo
= POPT_ARG_NONE
,
6559 .descrip
= "Browse SMB servers using DNS",
6562 POPT_COMMON_CONNECTION
6563 POPT_COMMON_CREDENTIALS
6568 TALLOC_CTX
*frame
= talloc_stackframe();
6569 struct cli_credentials
*creds
= NULL
;
6570 struct loadparm_context
*lp_ctx
= NULL
;
6572 if (!client_set_cur_dir("\\")) {
6578 ok
= samba_cmdline_init(frame
,
6579 SAMBA_CMDLINE_CONFIG_CLIENT
,
6580 false /* require_smbconf */);
6582 DBG_ERR("Failed to init cmdline parser!\n");
6585 lp_ctx
= samba_cmdline_get_lp_ctx();
6586 lpcfg_set_cmdline(lp_ctx
, "log level", "1");
6589 pc
= samba_popt_get_context(getprogname(),
6595 DBG_ERR("Failed to setup popt context!\n");
6599 poptSetOtherOptionHelp(pc
, "[OPTIONS] service <password>");
6601 creds
= samba_cmdline_get_creds();
6602 while ((opt
= poptGetNextOpt(pc
)) != -1) {
6605 * if the tar option has been called previously, now
6606 * we need to eat out the leftovers
6608 /* I see no other way to keep things sane --SSS */
6609 if (tar_opt
== true) {
6610 while (poptPeekArg(pc
)) {
6616 /* if the service has not yet been specified lets see if it is available in the popt stack */
6617 if (!service_opt
&& poptPeekArg(pc
)) {
6618 service
= talloc_strdup(frame
, poptGetArg(pc
));
6625 /* if the service has already been retrieved then check if we have also a password */
6627 cli_credentials_get_password_obtained(creds
) != CRED_SPECIFIED
&&
6629 cli_credentials_set_password(creds
,
6637 /* Messages are sent to NetBIOS name type 0x3
6638 * (Messenger Service). Make sure we default
6639 * to port 139 instead of port 445. srl,crh
6642 desthost
= talloc_strdup(frame
,poptGetOptArg(pc
));
6647 port
= NBT_SMB_PORT
;
6652 if (!interpret_string_addr(&dest_ss
, poptGetOptArg(pc
), 0)) {
6656 print_sockaddr(dest_ss_str
, sizeof(dest_ss_str
), &dest_ss
);
6660 setup_logging("smbclient", DEBUG_STDERR
);
6661 display_set_stderr();
6665 query_host
= talloc_strdup(frame
, poptGetOptArg(pc
));
6671 /* We must use old option processing for this. Find the
6672 * position of the -T option in the raw argv[]. */
6676 for (i
= 1; i
< argc
; i
++) {
6677 if (strncmp("-T", argv
[i
],2)==0)
6681 if (tar_parse_args(tar_ctx
, poptGetOptArg(pc
),
6682 const_argv
+ i
, argc
- i
)) {
6683 poptPrintUsage(pc
, stderr
, 0);
6687 /* this must be the last option, mark we have parsed it so that we know we have */
6691 base_directory
= talloc_strdup(frame
, poptGetOptArg(pc
));
6692 if (!base_directory
) {
6703 return(do_smb_browse());
6704 case POPT_ERROR_BADOPT
:
6705 fprintf(stderr
, "\nInvalid option %s: %s\n\n",
6706 poptBadOption(pc
, 0), poptStrerror(opt
));
6707 poptPrintUsage(pc
, stderr
, 0);
6712 /* We may still have some leftovers after the last popt option has been called */
6713 if (tar_opt
== true) {
6714 while (poptPeekArg(pc
)) {
6720 /* if the service has not yet been specified lets see if it is available in the popt stack */
6721 if (!service_opt
&& poptPeekArg(pc
)) {
6722 service
= talloc_strdup(frame
,poptGetArg(pc
));
6729 /* if the service has already been retrieved then check if we have also a password */
6731 cli_credentials_get_password_obtained(creds
) != CRED_SPECIFIED
&&
6733 cli_credentials_set_password(creds
,
6738 if (service_opt
&& service
) {
6741 /* Convert any '/' characters in the service name to '\' characters */
6742 string_replace(service
, '/','\\');
6743 if (count_chars(service
,'\\') < 3) {
6744 d_printf("\n%s: Not enough '\\' characters in service\n",service
);
6745 poptPrintUsage(pc
, stderr
, 0);
6748 /* Remove trailing slashes */
6749 len
= strlen(service
);
6750 while(len
> 0 && service
[len
- 1] == '\\') {
6752 service
[len
] = '\0';
6756 if(new_name_resolve_order
)
6757 lpcfg_set_cmdline(lp_ctx
,
6758 "name resolve order",
6759 new_name_resolve_order
);
6761 if (!tar_to_process(tar_ctx
) && !query_host
&& !service
&& !message
) {
6762 poptPrintUsage(pc
, stderr
, 0);
6766 poptFreeContext(pc
);
6767 samba_cmdline_burn(argc
, argv
);
6769 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
6771 if (tar_to_process(tar_ctx
)) {
6773 process_command_string(cmdstr
);
6774 rc
= do_tar_op(base_directory
);
6775 } else if (query_host
&& *query_host
) {
6776 char *qhost
= query_host
;
6779 while (*qhost
== '\\' || *qhost
== '/')
6782 if ((slash
= strchr_m(qhost
, '/'))
6783 || (slash
= strchr_m(qhost
, '\\'))) {
6787 if ((p
=strchr_m(qhost
, '#'))) {
6790 sscanf(p
, "%x", &name_type
);
6793 rc
= do_host_query(lp_ctx
, qhost
);
6794 } else if (message
) {
6795 rc
= do_message_op(creds
);
6796 } else if (process(base_directory
)) {