2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) Gerald (Jerry) Carter 2004
8 Copyright (C) Jeremy Allison 1994-2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/filesys.h"
26 #include "popt_common_cmdline.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "client/client_proto.h"
29 #include "client/clitar_proto.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
31 #include "../lib/util/select.h"
32 #include "system/readline.h"
33 #include "../libcli/smbreadline/smbreadline.h"
34 #include "../libcli/security/security.h"
35 #include "system/select.h"
36 #include "libsmb/libsmb.h"
37 #include "libsmb/clirap.h"
39 #include "libsmb/nmblib.h"
40 #include "include/ntioctl.h"
41 #include "../libcli/smb/smbXcli_base.h"
47 extern int do_smb_browse(void); /* mDNS browsing */
49 extern bool override_logfile
;
53 static char *desthost
;
54 static bool grepable
= false;
55 static bool quiet
= false;
56 static char *cmdstr
= NULL
;
57 const char *cmd_ptr
= NULL
;
59 static int io_bufsize
= 0; /* we use the default size */
60 static int io_timeout
= (CLIENT_TIMEOUT
/1000); /* Per operation timeout (in seconds). */
62 static int name_type
= 0x20;
63 static int max_protocol
= -1;
65 static int process_tok(char *tok
);
66 static int cmd_help(void);
68 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
70 /* value for unused fid field in trans2 secondary request */
71 #define FID_UNUSED (0xFFFF)
73 time_t newer_than
= 0;
74 static int archive_level
= 0;
76 static bool translation
= false;
79 static bool prompt
= true;
81 static bool recurse
= false;
82 static bool showacls
= false;
83 bool lowercase
= false;
84 static bool backup_intent
= false;
86 static struct sockaddr_storage dest_ss
;
87 static char dest_ss_str
[INET6_ADDRSTRLEN
];
89 #define SEPARATORS " \t\n\r"
91 static bool abort_mget
= true;
94 uint64_t get_total_size
= 0;
95 unsigned int get_total_time_ms
= 0;
96 static uint64_t put_total_size
= 0;
97 static unsigned int put_total_time_ms
= 0;
100 static double dir_total
;
102 /* encrypted state. */
103 static bool smb_encrypt
;
105 /* root cli_state connection */
107 struct cli_state
*cli
;
109 static char CLI_DIRSEP_CHAR
= '\\';
110 static char CLI_DIRSEP_STR
[] = { '\\', '\0' };
112 /* Accessor functions for directory paths. */
113 static char *fileselection
;
114 static const char *client_get_fileselection(void)
117 return fileselection
;
122 static const char *client_set_fileselection(const char *new_fs
)
124 SAFE_FREE(fileselection
);
126 fileselection
= SMB_STRDUP(new_fs
);
128 return client_get_fileselection();
132 static const char *client_get_cwd(void)
137 return CLI_DIRSEP_STR
;
140 static const char *client_set_cwd(const char *new_cwd
)
144 cwd
= SMB_STRDUP(new_cwd
);
146 return client_get_cwd();
149 static char *cur_dir
;
150 const char *client_get_cur_dir(void)
155 return CLI_DIRSEP_STR
;
158 const char *client_set_cur_dir(const char *newdir
)
162 cur_dir
= SMB_STRDUP(newdir
);
164 return client_get_cur_dir();
167 /****************************************************************************
168 Put up a yes/no prompt.
169 ****************************************************************************/
171 static bool yesno(const char *p
)
176 if (!fgets(ans
,sizeof(ans
)-1,stdin
))
179 if (*ans
== 'y' || *ans
== 'Y')
185 /****************************************************************************
186 Write to a local file with CR/LF->LF translation if appropriate. Return the
187 number taken from the buffer. This may not equal the number written.
188 ****************************************************************************/
190 static ssize_t
writefile(int f
, char *b
, size_t n
)
204 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
207 if (write(f
, b
, 1) != 1) {
217 /****************************************************************************
218 Read from a file with LF->CR/LF translation if appropriate. Return the
219 number read. read approx n bytes.
220 ****************************************************************************/
222 static int readfile(uint8_t *b
, int n
, FILE *f
)
228 return fread(b
,1,n
,f
);
231 while (i
< (n
- 1)) {
232 if ((c
= getc(f
)) == EOF
) {
236 if (c
== '\n') { /* change all LFs to CR/LF */
251 static size_t push_source(uint8_t *buf
, size_t n
, void *priv
)
253 struct push_state
*state
= (struct push_state
*)priv
;
256 if (feof(state
->f
)) {
260 result
= readfile(buf
, n
, state
->f
);
261 state
->nread
+= result
;
265 /****************************************************************************
267 ****************************************************************************/
269 static void send_message(const char *username
)
275 d_printf("Type your message, ending it with a Control-D\n");
278 while (i
<sizeof(buf
)-2) {
279 int c
= fgetc(stdin
);
290 status
= cli_message(cli
, desthost
, username
, buf
);
291 if (!NT_STATUS_IS_OK(status
)) {
292 d_fprintf(stderr
, "cli_message returned %s\n",
297 /****************************************************************************
298 Check the space on a device.
299 ****************************************************************************/
301 static int do_dskattr(void)
303 uint64_t total
, bsize
, avail
;
304 struct cli_state
*targetcli
= NULL
;
305 char *targetpath
= NULL
;
306 TALLOC_CTX
*ctx
= talloc_tos();
309 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(), cli
,
310 client_get_cur_dir(), &targetcli
,
312 if (!NT_STATUS_IS_OK(status
)) {
313 d_printf("Error in dskattr: %s\n", nt_errstr(status
));
317 status
= cli_disk_size(targetcli
, targetpath
, &bsize
, &total
, &avail
);
318 if (!NT_STATUS_IS_OK(status
)) {
319 d_printf("Error in dskattr: %s\n", nt_errstr(status
));
323 d_printf("\n\t\t%" PRIu64
324 " blocks of size %" PRIu64
325 ". %" PRIu64
" blocks available\n",
326 total
, bsize
, avail
);
331 /****************************************************************************
333 ****************************************************************************/
335 static int cmd_pwd(void)
337 d_printf("Current directory is %s",service
);
338 d_printf("%s\n",client_get_cur_dir());
342 /****************************************************************************
343 Ensure name has correct directory separators.
344 ****************************************************************************/
346 static void normalize_name(char *newdir
)
348 if (!(cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
)) {
349 string_replace(newdir
,'/','\\');
353 /****************************************************************************
354 Local name cleanup before sending to server. SMB1 allows relative pathnames,
355 but SMB2 does not, so we need to resolve them locally.
356 ****************************************************************************/
358 char *client_clean_name(TALLOC_CTX
*ctx
, const char *name
)
360 char *newname
= NULL
;
365 /* First ensure any path separators are correct. */
366 newname
= talloc_strdup(ctx
, name
);
367 if (newname
== NULL
) {
370 normalize_name(newname
);
372 /* Now remove any relative (..) path components. */
373 if (cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
374 newname
= unix_clean_name(ctx
, newname
);
376 newname
= clean_name(ctx
, newname
);
378 if (newname
== NULL
) {
384 /****************************************************************************
385 Change directory - inner section.
386 ****************************************************************************/
388 static int do_cd(const char *new_dir
)
391 char *saved_dir
= NULL
;
393 char *targetpath
= NULL
;
394 struct cli_state
*targetcli
= NULL
;
395 SMB_STRUCT_STAT sbuf
;
398 TALLOC_CTX
*ctx
= talloc_stackframe();
401 newdir
= talloc_strdup(ctx
, new_dir
);
407 normalize_name(newdir
);
409 /* Save the current directory in case the new directory is invalid */
411 saved_dir
= talloc_strdup(ctx
, client_get_cur_dir());
417 if (*newdir
== CLI_DIRSEP_CHAR
) {
418 client_set_cur_dir(newdir
);
421 new_cd
= talloc_asprintf(ctx
, "%s%s",
422 client_get_cur_dir(),
429 /* Ensure cur_dir ends in a DIRSEP */
430 if ((new_cd
[0] != '\0') && (*(new_cd
+strlen(new_cd
)-1) != CLI_DIRSEP_CHAR
)) {
431 new_cd
= talloc_asprintf_append(new_cd
, "%s", CLI_DIRSEP_STR
);
436 client_set_cur_dir(new_cd
);
438 new_cd
= client_clean_name(ctx
, new_cd
);
439 client_set_cur_dir(new_cd
);
441 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
442 cli
, new_cd
, &targetcli
, &targetpath
);
443 if (!NT_STATUS_IS_OK(status
)) {
444 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
445 client_set_cur_dir(saved_dir
);
449 if (strequal(targetpath
,CLI_DIRSEP_STR
)) {
454 /* Use a trans2_qpathinfo to test directories for modern servers.
455 Except Win9x doesn't support the qpathinfo_basic() call..... */
457 if (smbXcli_conn_protocol(targetcli
->conn
) > PROTOCOL_LANMAN2
&& !targetcli
->win95
) {
459 status
= cli_qpathinfo_basic(targetcli
, targetpath
, &sbuf
,
461 if (!NT_STATUS_IS_OK(status
)) {
462 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
463 client_set_cur_dir(saved_dir
);
467 if (!(attributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
468 d_printf("cd %s: not a directory\n", new_cd
);
469 client_set_cur_dir(saved_dir
);
474 targetpath
= talloc_asprintf(ctx
,
479 client_set_cur_dir(saved_dir
);
482 targetpath
= client_clean_name(ctx
, targetpath
);
484 client_set_cur_dir(saved_dir
);
488 status
= cli_chkpath(targetcli
, targetpath
);
489 if (!NT_STATUS_IS_OK(status
)) {
490 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
491 client_set_cur_dir(saved_dir
);
504 /****************************************************************************
506 ****************************************************************************/
508 static int cmd_cd(void)
513 if (next_token_talloc(talloc_tos(), &cmd_ptr
, &buf
,NULL
)) {
516 d_printf("Current directory is %s\n",client_get_cur_dir());
522 /****************************************************************************
524 ****************************************************************************/
526 static int cmd_cd_oneup(void)
531 /*******************************************************************
532 Decide if a file should be operated on.
533 ********************************************************************/
535 static bool do_this_one(struct file_info
*finfo
)
541 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
545 if (*client_get_fileselection() &&
546 !mask_match(finfo
->name
,client_get_fileselection(),false)) {
547 DEBUG(3,("mask_match %s failed\n", finfo
->name
));
551 if (newer_than
&& finfo
->mtime_ts
.tv_sec
< newer_than
) {
552 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
556 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& FILE_ATTRIBUTE_ARCHIVE
)) {
557 DEBUG(3,("archive %s failed\n", finfo
->name
));
564 /****************************************************************************
565 Display info about a file.
566 ****************************************************************************/
568 static NTSTATUS
display_finfo(struct cli_state
*cli_state
, struct file_info
*finfo
,
572 TALLOC_CTX
*ctx
= talloc_tos();
573 NTSTATUS status
= NT_STATUS_OK
;
575 if (!do_this_one(finfo
)) {
579 t
= finfo
->mtime_ts
.tv_sec
; /* the time is assumed to be passed as GMT */
581 d_printf(" %-30s%7.7s %8.0f %s",
583 attrib_string(talloc_tos(), finfo
->mode
),
586 dir_total
+= finfo
->size
;
591 /* skip if this is . or .. */
592 if ( strequal(finfo
->name
,"..") || strequal(finfo
->name
,".") )
594 /* create absolute filename for cli_ntcreate() FIXME */
595 afname
= talloc_asprintf(ctx
,
601 return NT_STATUS_NO_MEMORY
;
603 /* print file meta date header */
604 d_printf( "FILENAME:%s\n", finfo
->name
);
605 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo
->mode
));
606 d_printf( "SIZE:%.0f\n", (double)finfo
->size
);
607 d_printf( "MTIME:%s", time_to_asc(t
));
608 status
= cli_ntcreate(cli_state
, afname
, 0,
609 CREATE_ACCESS_READ
, 0,
610 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
611 FILE_OPEN
, 0x0, 0x0, &fnum
, NULL
);
612 if (!NT_STATUS_IS_OK(status
)) {
613 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
614 afname
, nt_errstr(status
)));
616 struct security_descriptor
*sd
= NULL
;
617 status
= cli_query_secdesc(cli_state
, fnum
,
619 if (!NT_STATUS_IS_OK(status
)) {
620 DEBUG( 0, ("display_finfo() failed to "
621 "get security descriptor: %s",
624 display_sec_desc(sd
);
633 /****************************************************************************
634 Accumulate size of a file.
635 ****************************************************************************/
637 static NTSTATUS
do_du(struct cli_state
*cli_state
, struct file_info
*finfo
,
640 if (do_this_one(finfo
)) {
641 dir_total
+= finfo
->size
;
646 static bool do_list_recurse
;
647 static bool do_list_dirs
;
648 static char *do_list_queue
= 0;
649 static long do_list_queue_size
= 0;
650 static long do_list_queue_start
= 0;
651 static long do_list_queue_end
= 0;
652 static NTSTATUS (*do_list_fn
)(struct cli_state
*cli_state
, struct file_info
*,
655 /****************************************************************************
656 Functions for do_list_queue.
657 ****************************************************************************/
660 * The do_list_queue is a NUL-separated list of strings stored in a
661 * char*. Since this is a FIFO, we keep track of the beginning and
662 * ending locations of the data in the queue. When we overflow, we
663 * double the size of the char*. When the start of the data passes
664 * the midpoint, we move everything back. This is logically more
665 * complex than a linked list, but easier from a memory management
666 * angle. In any memory error condition, do_list_queue is reset.
667 * Functions check to ensure that do_list_queue is non-NULL before
671 static void reset_do_list_queue(void)
673 SAFE_FREE(do_list_queue
);
674 do_list_queue_size
= 0;
675 do_list_queue_start
= 0;
676 do_list_queue_end
= 0;
679 static void init_do_list_queue(void)
681 reset_do_list_queue();
682 do_list_queue_size
= 1024;
683 do_list_queue
= (char *)SMB_MALLOC(do_list_queue_size
);
684 if (do_list_queue
== 0) {
685 d_printf("malloc fail for size %d\n",
686 (int)do_list_queue_size
);
687 reset_do_list_queue();
689 memset(do_list_queue
, 0, do_list_queue_size
);
693 static void adjust_do_list_queue(void)
696 * If the starting point of the queue is more than half way through,
697 * move everything toward the beginning.
700 if (do_list_queue
== NULL
) {
701 DEBUG(4,("do_list_queue is empty\n"));
702 do_list_queue_start
= do_list_queue_end
= 0;
706 if (do_list_queue_start
== do_list_queue_end
) {
707 DEBUG(4,("do_list_queue is empty\n"));
708 do_list_queue_start
= do_list_queue_end
= 0;
709 *do_list_queue
= '\0';
710 } else if (do_list_queue_start
> (do_list_queue_size
/ 2)) {
711 DEBUG(4,("sliding do_list_queue backward\n"));
712 memmove(do_list_queue
,
713 do_list_queue
+ do_list_queue_start
,
714 do_list_queue_end
- do_list_queue_start
);
715 do_list_queue_end
-= do_list_queue_start
;
716 do_list_queue_start
= 0;
720 static void add_to_do_list_queue(const char *entry
)
722 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
723 while (new_end
> do_list_queue_size
) {
724 do_list_queue_size
*= 2;
725 DEBUG(4,("enlarging do_list_queue to %d\n",
726 (int)do_list_queue_size
));
727 do_list_queue
= (char *)SMB_REALLOC(do_list_queue
, do_list_queue_size
);
728 if (! do_list_queue
) {
729 d_printf("failure enlarging do_list_queue to %d bytes\n",
730 (int)do_list_queue_size
);
731 reset_do_list_queue();
733 memset(do_list_queue
+ do_list_queue_size
/ 2,
734 0, do_list_queue_size
/ 2);
738 strlcpy_base(do_list_queue
+ do_list_queue_end
,
739 entry
, do_list_queue
, do_list_queue_size
);
740 do_list_queue_end
= new_end
;
741 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
742 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
746 static char *do_list_queue_head(void)
748 return do_list_queue
+ do_list_queue_start
;
751 static void remove_do_list_queue_head(void)
753 if (do_list_queue_end
> do_list_queue_start
) {
754 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
755 adjust_do_list_queue();
756 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
757 (int)do_list_queue_start
, (int)do_list_queue_end
));
761 static int do_list_queue_empty(void)
763 return (! (do_list_queue
&& *do_list_queue
));
766 /****************************************************************************
767 A helper for do_list.
768 ****************************************************************************/
770 static NTSTATUS
do_list_helper(const char *mntpoint
, struct file_info
*f
,
771 const char *mask
, void *state
)
773 struct cli_state
*cli_state
= (struct cli_state
*)state
;
774 TALLOC_CTX
*ctx
= talloc_tos();
776 char *dir_end
= NULL
;
777 NTSTATUS status
= NT_STATUS_OK
;
779 /* Work out the directory. */
780 dir
= talloc_strdup(ctx
, mask
);
782 return NT_STATUS_NO_MEMORY
;
784 if ((dir_end
= strrchr(dir
, CLI_DIRSEP_CHAR
)) != NULL
) {
788 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
789 if (do_list_dirs
&& do_this_one(f
)) {
790 status
= do_list_fn(cli_state
, f
, dir
);
791 if (!NT_STATUS_IS_OK(status
)) {
795 if (do_list_recurse
&&
797 !strequal(f
->name
,".") &&
798 !strequal(f
->name
,"..")) {
803 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
805 return NT_STATUS_UNSUCCESSFUL
;
808 mask2
= talloc_asprintf(ctx
,
814 return NT_STATUS_NO_MEMORY
;
816 p
= strrchr_m(mask2
,CLI_DIRSEP_CHAR
);
822 mask2
= talloc_asprintf_append(mask2
,
828 return NT_STATUS_NO_MEMORY
;
830 add_to_do_list_queue(mask2
);
837 if (do_this_one(f
)) {
838 status
= do_list_fn(cli_state
, f
, dir
);
844 /****************************************************************************
845 A wrapper around cli_list that adds recursion.
846 ****************************************************************************/
848 NTSTATUS
do_list(const char *mask
,
850 NTSTATUS (*fn
)(struct cli_state
*cli_state
, struct file_info
*,
855 static int in_do_list
= 0;
856 TALLOC_CTX
*ctx
= talloc_tos();
857 struct cli_state
*targetcli
= NULL
;
858 char *targetpath
= NULL
;
859 NTSTATUS ret_status
= NT_STATUS_OK
;
860 NTSTATUS status
= NT_STATUS_OK
;
862 if (in_do_list
&& rec
) {
863 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
869 do_list_recurse
= rec
;
874 init_do_list_queue();
875 add_to_do_list_queue(mask
);
877 while (!do_list_queue_empty()) {
879 * Need to copy head so that it doesn't become
880 * invalid inside the call to cli_list. This
881 * would happen if the list were expanded
883 * Fix from E. Jay Berkenbilt (ejb@ql.org)
885 char *head
= talloc_strdup(ctx
, do_list_queue_head());
888 return NT_STATUS_NO_MEMORY
;
893 status
= cli_resolve_path(ctx
, "",
894 popt_get_cmdline_auth_info(),
895 cli
, head
, &targetcli
, &targetpath
);
896 if (!NT_STATUS_IS_OK(status
)) {
897 d_printf("do_list: [%s] %s\n", head
,
899 remove_do_list_queue_head();
903 status
= cli_list(targetcli
, targetpath
, attribute
,
904 do_list_helper
, targetcli
);
905 if (!NT_STATUS_IS_OK(status
)) {
906 d_printf("%s listing %s\n",
907 nt_errstr(status
), targetpath
);
910 remove_do_list_queue_head();
911 if ((! do_list_queue_empty()) && (fn
== display_finfo
)) {
912 char *next_file
= do_list_queue_head();
914 if ((strlen(next_file
) >= 2) &&
915 (next_file
[strlen(next_file
) - 1] == '*') &&
916 (next_file
[strlen(next_file
) - 2] == CLI_DIRSEP_CHAR
)) {
917 save_ch
= next_file
+
918 strlen(next_file
) - 2;
921 /* cwd is only used if showacls is on */
922 client_set_cwd(next_file
);
925 if (!showacls
) /* don't disturbe the showacls output */
926 d_printf("\n%s\n",next_file
);
928 *save_ch
= CLI_DIRSEP_CHAR
;
932 TALLOC_FREE(targetpath
);
936 status
= cli_resolve_path(ctx
, "",
937 popt_get_cmdline_auth_info(), cli
, mask
,
938 &targetcli
, &targetpath
);
939 if (NT_STATUS_IS_OK(status
)) {
940 status
= cli_list(targetcli
, targetpath
, attribute
,
941 do_list_helper
, targetcli
);
942 if (!NT_STATUS_IS_OK(status
)) {
943 d_printf("%s listing %s\n",
944 nt_errstr(status
), targetpath
);
947 TALLOC_FREE(targetpath
);
949 d_printf("do_list: [%s] %s\n", mask
, nt_errstr(status
));
955 reset_do_list_queue();
959 /****************************************************************************
960 Get a directory listing.
961 ****************************************************************************/
963 static int cmd_dir(void)
965 TALLOC_CTX
*ctx
= talloc_tos();
966 uint16_t attribute
= FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
973 mask
= talloc_strdup(ctx
, client_get_cur_dir());
978 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
980 if (*buf
== CLI_DIRSEP_CHAR
) {
981 mask
= talloc_strdup(ctx
, buf
);
983 mask
= talloc_asprintf_append(mask
, "%s", buf
);
986 mask
= talloc_asprintf_append(mask
, "*");
992 mask
= client_clean_name(ctx
, mask
);
998 /* cwd is only used if showacls is on */
999 client_set_cwd(client_get_cur_dir());
1002 status
= do_list(mask
, attribute
, display_finfo
, recurse
, true);
1003 if (!NT_STATUS_IS_OK(status
)) {
1009 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
1014 /****************************************************************************
1015 Get a directory listing.
1016 ****************************************************************************/
1018 static int cmd_du(void)
1020 TALLOC_CTX
*ctx
= talloc_tos();
1021 uint16_t attribute
= FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
1028 mask
= talloc_strdup(ctx
, client_get_cur_dir());
1032 if ((mask
[0] != '\0') && (mask
[strlen(mask
)-1]!=CLI_DIRSEP_CHAR
)) {
1033 mask
= talloc_asprintf_append(mask
, "%s", CLI_DIRSEP_STR
);
1039 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1040 normalize_name(buf
);
1041 if (*buf
== CLI_DIRSEP_CHAR
) {
1042 mask
= talloc_strdup(ctx
, buf
);
1044 mask
= talloc_asprintf_append(mask
, "%s", buf
);
1047 mask
= talloc_strdup(ctx
, "*");
1053 mask
= client_clean_name(ctx
, mask
);
1058 status
= do_list(mask
, attribute
, do_du
, recurse
, true);
1059 if (!NT_STATUS_IS_OK(status
)) {
1065 d_printf("Total number of bytes: %.0f\n", dir_total
);
1070 static int cmd_echo(void)
1072 TALLOC_CTX
*ctx
= talloc_tos();
1077 if (!next_token_talloc(ctx
, &cmd_ptr
, &num
, NULL
)
1078 || !next_token_talloc(ctx
, &cmd_ptr
, &data
, NULL
)) {
1079 d_printf("echo <num> <data>\n");
1083 status
= cli_echo(cli
, atoi(num
), data_blob_const(data
, strlen(data
)));
1085 if (!NT_STATUS_IS_OK(status
)) {
1086 d_printf("echo failed: %s\n", nt_errstr(status
));
1093 /****************************************************************************
1094 Get a file from rname to lname
1095 ****************************************************************************/
1097 static NTSTATUS
writefile_sink(char *buf
, size_t n
, void *priv
)
1099 int *pfd
= (int *)priv
;
1102 rc
= writefile(*pfd
, buf
, n
);
1104 return map_nt_error_from_unix(errno
);
1106 return NT_STATUS_OK
;
1109 static int do_get(const char *rname
, const char *lname_in
, bool reget
)
1111 TALLOC_CTX
*ctx
= talloc_tos();
1114 bool newhandle
= false;
1115 struct timespec tp_start
;
1121 struct cli_state
*targetcli
= NULL
;
1122 char *targetname
= NULL
;
1126 lname
= talloc_strdup(ctx
, lname_in
);
1132 if (!strlower_m(lname
)) {
1133 d_printf("strlower_m %s failed\n", lname
);
1138 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
1139 cli
, rname
, &targetcli
, &targetname
);
1140 if (!NT_STATUS_IS_OK(status
)) {
1141 d_printf("Failed to open %s: %s\n", rname
, nt_errstr(status
));
1145 clock_gettime_mono(&tp_start
);
1147 status
= cli_open(targetcli
, targetname
, O_RDONLY
, DENY_NONE
, &fnum
);
1148 if (!NT_STATUS_IS_OK(status
)) {
1149 d_printf("%s opening remote file %s\n", nt_errstr(status
),
1154 if(!strcmp(lname
,"-")) {
1155 handle
= fileno(stdout
);
1158 handle
= open(lname
, O_WRONLY
|O_CREAT
, 0644);
1160 start
= lseek(handle
, 0, SEEK_END
);
1162 d_printf("Error seeking local file\n");
1168 handle
= open(lname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
1173 d_printf("Error opening local file %s\n",lname
);
1178 status
= cli_qfileinfo_basic(targetcli
, fnum
, &attr
, &size
, NULL
, NULL
,
1180 if (!NT_STATUS_IS_OK(status
)) {
1181 status
= cli_getattrE(targetcli
, fnum
, &attr
, &size
, NULL
, NULL
,
1183 if(!NT_STATUS_IS_OK(status
)) {
1184 d_printf("getattrib: %s\n", nt_errstr(status
));
1192 DEBUG(1,("getting file %s of size %.0f as %s ",
1193 rname
, (double)size
, lname
));
1195 status
= cli_pull(targetcli
, fnum
, start
, size
, io_bufsize
,
1196 writefile_sink
, (void *)&handle
, &nread
);
1197 if (!NT_STATUS_IS_OK(status
)) {
1198 d_fprintf(stderr
, "parallel_read returned %s\n",
1203 cli_close(targetcli
, fnum
);
1207 status
= cli_close(targetcli
, fnum
);
1208 if (!NT_STATUS_IS_OK(status
)) {
1209 d_printf("Error %s closing remote file\n", nt_errstr(status
));
1217 if (archive_level
>= 2 && (attr
& FILE_ATTRIBUTE_ARCHIVE
)) {
1218 cli_setatr(cli
, rname
, attr
& ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE
, 0);
1222 struct timespec tp_end
;
1225 clock_gettime_mono(&tp_end
);
1226 this_time
= nsec_time_diff(&tp_end
,&tp_start
)/1000000;
1227 get_total_time_ms
+= this_time
;
1228 get_total_size
+= nread
;
1230 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1231 nread
/ (1.024*this_time
+ 1.0e-4),
1232 get_total_size
/ (1.024*get_total_time_ms
)));
1235 TALLOC_FREE(targetname
);
1239 /****************************************************************************
1241 ****************************************************************************/
1243 static int cmd_get(void)
1245 TALLOC_CTX
*ctx
= talloc_tos();
1250 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1255 if (!next_token_talloc(ctx
, &cmd_ptr
,&fname
,NULL
)) {
1256 d_printf("get <filename> [localname]\n");
1259 rname
= talloc_asprintf_append(rname
, "%s", fname
);
1263 rname
= client_clean_name(ctx
, rname
);
1268 next_token_talloc(ctx
, &cmd_ptr
,&lname
,NULL
);
1273 return do_get(rname
, lname
, false);
1276 /****************************************************************************
1277 Do an mget operation on one file.
1278 ****************************************************************************/
1280 static NTSTATUS
do_mget(struct cli_state
*cli_state
, struct file_info
*finfo
,
1283 TALLOC_CTX
*ctx
= talloc_tos();
1284 NTSTATUS status
= NT_STATUS_OK
;
1287 char *saved_curdir
= NULL
;
1288 char *mget_mask
= NULL
;
1289 char *new_cd
= NULL
;
1292 return NT_STATUS_OK
;
1295 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
1296 return NT_STATUS_OK
;
1299 d_printf("mget aborted\n");
1300 return NT_STATUS_UNSUCCESSFUL
;
1303 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
1304 if (asprintf(&quest
,
1305 "Get directory %s? ",finfo
->name
) < 0) {
1306 return NT_STATUS_NO_MEMORY
;
1309 if (asprintf(&quest
,
1310 "Get file %s? ",finfo
->name
) < 0) {
1311 return NT_STATUS_NO_MEMORY
;
1315 if (prompt
&& !yesno(quest
)) {
1317 return NT_STATUS_OK
;
1321 if (!(finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
)) {
1322 rname
= talloc_asprintf(ctx
,
1324 client_get_cur_dir(),
1327 return NT_STATUS_NO_MEMORY
;
1329 rname
= client_clean_name(ctx
, rname
);
1330 if (rname
== NULL
) {
1331 return NT_STATUS_NO_MEMORY
;
1333 do_get(rname
, finfo
->name
, false);
1335 return NT_STATUS_OK
;
1338 /* handle directories */
1339 saved_curdir
= talloc_strdup(ctx
, client_get_cur_dir());
1340 if (!saved_curdir
) {
1341 return NT_STATUS_NO_MEMORY
;
1344 new_cd
= talloc_asprintf(ctx
,
1346 client_get_cur_dir(),
1350 return NT_STATUS_NO_MEMORY
;
1352 new_cd
= client_clean_name(ctx
, new_cd
);
1353 if (new_cd
== NULL
) {
1354 return NT_STATUS_NO_MEMORY
;
1356 client_set_cur_dir(new_cd
);
1358 string_replace(finfo
->name
,'\\','/');
1360 if (!strlower_m(finfo
->name
)) {
1361 return NT_STATUS_INVALID_PARAMETER
;
1365 if (!directory_exist(finfo
->name
) &&
1366 mkdir(finfo
->name
,0777) != 0) {
1367 d_printf("failed to create directory %s\n",finfo
->name
);
1368 client_set_cur_dir(saved_curdir
);
1369 return map_nt_error_from_unix(errno
);
1372 if (chdir(finfo
->name
) != 0) {
1373 d_printf("failed to chdir to directory %s\n",finfo
->name
);
1374 client_set_cur_dir(saved_curdir
);
1375 return map_nt_error_from_unix(errno
);
1378 mget_mask
= talloc_asprintf(ctx
,
1380 client_get_cur_dir());
1383 return NT_STATUS_NO_MEMORY
;
1386 mget_mask
= client_clean_name(ctx
, mget_mask
);
1387 if (mget_mask
== NULL
) {
1388 return NT_STATUS_NO_MEMORY
;
1390 status
= do_list(mget_mask
,
1391 (FILE_ATTRIBUTE_SYSTEM
1392 | FILE_ATTRIBUTE_HIDDEN
1393 | FILE_ATTRIBUTE_DIRECTORY
),
1394 do_mget
, false, true);
1395 if (!NT_STATUS_IS_OK(status
)
1396 && !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
1398 * Ignore access denied errors to ensure all permitted files are
1404 if (chdir("..") == -1) {
1405 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1407 return map_nt_error_from_unix(errno
);
1409 client_set_cur_dir(saved_curdir
);
1410 TALLOC_FREE(mget_mask
);
1411 TALLOC_FREE(saved_curdir
);
1412 TALLOC_FREE(new_cd
);
1413 return NT_STATUS_OK
;
1416 /****************************************************************************
1417 View the file using the pager.
1418 ****************************************************************************/
1420 static int cmd_more(void)
1422 TALLOC_CTX
*ctx
= talloc_tos();
1426 char *pager_cmd
= NULL
;
1432 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1437 lname
= talloc_asprintf(ctx
, "%s/smbmore.XXXXXX",tmpdir());
1441 mask
= umask(S_IRWXO
| S_IRWXG
);
1442 fd
= mkstemp(lname
);
1445 d_printf("failed to create temporary file for more\n");
1450 if (!next_token_talloc(ctx
, &cmd_ptr
,&fname
,NULL
)) {
1451 d_printf("more <filename>\n");
1455 rname
= talloc_asprintf_append(rname
, "%s", fname
);
1459 rname
= client_clean_name(ctx
,rname
);
1464 rc
= do_get(rname
, lname
, false);
1466 pager
=getenv("PAGER");
1468 pager_cmd
= talloc_asprintf(ctx
,
1470 (pager
? pager
:PAGER
),
1475 if (system(pager_cmd
) == -1) {
1476 d_printf("system command '%s' returned -1\n",
1484 /****************************************************************************
1486 ****************************************************************************/
1488 static int cmd_mget(void)
1490 TALLOC_CTX
*ctx
= talloc_tos();
1491 uint16_t attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
1492 char *mget_mask
= NULL
;
1494 NTSTATUS status
= NT_STATUS_OK
;
1497 attribute
|= FILE_ATTRIBUTE_DIRECTORY
;
1502 while (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1504 mget_mask
= talloc_strdup(ctx
, client_get_cur_dir());
1508 if (*buf
== CLI_DIRSEP_CHAR
) {
1509 mget_mask
= talloc_strdup(ctx
, buf
);
1511 mget_mask
= talloc_asprintf_append(mget_mask
,
1517 mget_mask
= client_clean_name(ctx
, mget_mask
);
1518 if (mget_mask
== NULL
) {
1521 status
= do_list(mget_mask
, attribute
, do_mget
, false, true);
1522 if (!NT_STATUS_IS_OK(status
)) {
1527 if (mget_mask
== NULL
) {
1528 d_printf("nothing to mget\n");
1533 mget_mask
= talloc_asprintf(ctx
,
1535 client_get_cur_dir());
1539 mget_mask
= client_clean_name(ctx
, mget_mask
);
1540 if (mget_mask
== NULL
) {
1543 status
= do_list(mget_mask
, attribute
, do_mget
, false, true);
1544 if (!NT_STATUS_IS_OK(status
)) {
1552 /****************************************************************************
1553 Make a directory of name "name".
1554 ****************************************************************************/
1556 static bool do_mkdir(const char *name
)
1558 TALLOC_CTX
*ctx
= talloc_tos();
1559 struct cli_state
*targetcli
;
1560 char *targetname
= NULL
;
1563 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
1564 cli
, name
, &targetcli
, &targetname
);
1565 if (!NT_STATUS_IS_OK(status
)) {
1566 d_printf("mkdir %s: %s\n", name
, nt_errstr(status
));
1570 status
= cli_mkdir(targetcli
, targetname
);
1571 if (!NT_STATUS_IS_OK(status
)) {
1572 d_printf("%s making remote directory %s\n",
1573 nt_errstr(status
),name
);
1580 /****************************************************************************
1581 Show 8.3 name of a file.
1582 ****************************************************************************/
1584 static bool do_altname(const char *name
)
1589 status
= cli_qpathinfo_alt_name(cli
, name
, altname
);
1590 if (!NT_STATUS_IS_OK(status
)) {
1591 d_printf("%s getting alt name for %s\n",
1592 nt_errstr(status
),name
);
1595 d_printf("%s\n", altname
);
1600 /****************************************************************************
1602 ****************************************************************************/
1604 static int cmd_quit(void)
1607 popt_free_cmdline_auth_info();
1613 /****************************************************************************
1615 ****************************************************************************/
1617 static int cmd_mkdir(void)
1619 TALLOC_CTX
*ctx
= talloc_tos();
1624 mask
= talloc_strdup(ctx
, client_get_cur_dir());
1629 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1631 d_printf("mkdir <dirname>\n");
1635 mask
= talloc_asprintf_append(mask
, "%s", buf
);
1639 mask
= client_clean_name(ctx
, mask
);
1647 struct cli_state
*targetcli
;
1648 char *targetname
= NULL
;
1652 ddir2
= talloc_strdup(ctx
, "");
1657 status
= cli_resolve_path(ctx
, "",
1658 popt_get_cmdline_auth_info(), cli
, mask
,
1659 &targetcli
, &targetname
);
1660 if (!NT_STATUS_IS_OK(status
)) {
1664 ddir
= talloc_strdup(ctx
, targetname
);
1668 trim_char(ddir
,'.','\0');
1669 p
= strtok_r(ddir
, "/\\", &saveptr
);
1671 ddir2
= talloc_asprintf_append(ddir2
, "%s", p
);
1675 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli
, ddir2
))) {
1678 ddir2
= talloc_asprintf_append(ddir2
, "%s", CLI_DIRSEP_STR
);
1682 p
= strtok_r(NULL
, "/\\", &saveptr
);
1691 /****************************************************************************
1693 ****************************************************************************/
1695 static int cmd_altname(void)
1697 TALLOC_CTX
*ctx
= talloc_tos();
1701 name
= talloc_strdup(ctx
, client_get_cur_dir());
1706 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
1707 d_printf("altname <file>\n");
1710 name
= talloc_asprintf_append(name
, "%s", buf
);
1714 name
= client_clean_name(ctx
, name
);
1722 static char *attr_str(TALLOC_CTX
*mem_ctx
, uint16_t mode
)
1724 char *attrs
= talloc_zero_array(mem_ctx
, char, 17);
1727 if (!(mode
& FILE_ATTRIBUTE_NORMAL
)) {
1728 if (mode
& FILE_ATTRIBUTE_ENCRYPTED
) {
1731 if (mode
& FILE_ATTRIBUTE_NONINDEXED
) {
1734 if (mode
& FILE_ATTRIBUTE_OFFLINE
) {
1737 if (mode
& FILE_ATTRIBUTE_COMPRESSED
) {
1740 if (mode
& FILE_ATTRIBUTE_REPARSE_POINT
) {
1743 if (mode
& FILE_ATTRIBUTE_SPARSE
) {
1746 if (mode
& FILE_ATTRIBUTE_TEMPORARY
) {
1749 if (mode
& FILE_ATTRIBUTE_NORMAL
) {
1752 if (mode
& FILE_ATTRIBUTE_READONLY
) {
1755 if (mode
& FILE_ATTRIBUTE_HIDDEN
) {
1758 if (mode
& FILE_ATTRIBUTE_SYSTEM
) {
1761 if (mode
& FILE_ATTRIBUTE_DIRECTORY
) {
1764 if (mode
& FILE_ATTRIBUTE_ARCHIVE
) {
1771 /****************************************************************************
1772 Show all info we can get
1773 ****************************************************************************/
1775 static int do_allinfo(const char *name
)
1778 struct timespec b_time
, a_time
, m_time
, c_time
;
1783 unsigned int num_streams
;
1784 struct stream_struct
*streams
;
1786 char **snapshots
= NULL
;
1790 status
= cli_qpathinfo_alt_name(cli
, name
, altname
);
1791 if (!NT_STATUS_IS_OK(status
)) {
1792 d_printf("%s getting alt name for %s\n", nt_errstr(status
),
1795 * Ignore not supported or not implemented, it does not
1796 * hurt if we can't list alternate names.
1798 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
) ||
1799 NT_STATUS_EQUAL(status
, NT_STATUS_NOT_IMPLEMENTED
)) {
1805 d_printf("altname: %s\n", altname
);
1807 status
= cli_qpathinfo3(cli
, name
, &b_time
, &a_time
, &m_time
, &c_time
,
1808 &size
, &mode
, NULL
);
1809 if (!NT_STATUS_IS_OK(status
)) {
1810 d_printf("%s getting pathinfo for %s\n", nt_errstr(status
),
1815 tmp
= unix_timespec_to_nt_time(b_time
);
1816 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1818 tmp
= unix_timespec_to_nt_time(a_time
);
1819 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1821 tmp
= unix_timespec_to_nt_time(m_time
);
1822 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1824 tmp
= unix_timespec_to_nt_time(c_time
);
1825 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1827 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode
), mode
);
1829 status
= cli_qpathinfo_streams(cli
, name
, talloc_tos(), &num_streams
,
1831 if (!NT_STATUS_IS_OK(status
)) {
1832 d_printf("%s getting streams for %s\n", nt_errstr(status
),
1837 for (i
=0; i
<num_streams
; i
++) {
1838 d_printf("stream: [%s], %lld bytes\n", streams
[i
].name
,
1839 (unsigned long long)streams
[i
].size
);
1842 if (mode
& FILE_ATTRIBUTE_REPARSE_POINT
) {
1843 char *subst
, *print
;
1846 status
= cli_readlink(cli
, name
, talloc_tos(), &subst
, &print
,
1848 if (!NT_STATUS_IS_OK(status
)) {
1849 d_fprintf(stderr
, "cli_readlink returned %s\n",
1852 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1853 subst
, print
, flags
);
1859 status
= cli_ntcreate(cli
, name
, 0,
1860 SEC_FILE_READ_DATA
| SEC_FILE_READ_ATTRIBUTE
|
1861 SEC_STD_SYNCHRONIZE
, 0,
1862 FILE_SHARE_READ
|FILE_SHARE_WRITE
1864 FILE_OPEN
, 0x0, 0x0, &fnum
, NULL
);
1865 if (!NT_STATUS_IS_OK(status
)) {
1867 * Ignore failure, it does not hurt if we can't list
1873 * In order to get shadow copy data over SMB1 we
1874 * must call twice, once with 'get_names = false'
1875 * to get the size, then again with 'get_names = true'
1876 * to get the data or a Windows server fails to return
1877 * valid info. Samba doesn't have this bug. JRA.
1880 status
= cli_shadow_copy_data(talloc_tos(), cli
, fnum
,
1881 false, &snapshots
, &num_snapshots
);
1882 if (!NT_STATUS_IS_OK(status
)) {
1883 cli_close(cli
, fnum
);
1886 status
= cli_shadow_copy_data(talloc_tos(), cli
, fnum
,
1887 true, &snapshots
, &num_snapshots
);
1888 if (!NT_STATUS_IS_OK(status
)) {
1889 cli_close(cli
, fnum
);
1893 for (i
=0; i
<num_snapshots
; i
++) {
1896 d_printf("%s\n", snapshots
[i
]);
1897 snap_name
= talloc_asprintf(talloc_tos(), "%s%s",
1898 snapshots
[i
], name
);
1899 status
= cli_qpathinfo3(cli
, snap_name
, &b_time
, &a_time
,
1900 &m_time
, &c_time
, &size
,
1902 if (!NT_STATUS_IS_OK(status
)) {
1903 d_fprintf(stderr
, "pathinfo(%s) failed: %s\n",
1904 snap_name
, nt_errstr(status
));
1905 TALLOC_FREE(snap_name
);
1908 tmp
= unix_timespec_to_nt_time(b_time
);
1909 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1910 tmp
= unix_timespec_to_nt_time(a_time
);
1911 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1912 tmp
=unix_timespec_to_nt_time(m_time
);
1913 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1914 tmp
= unix_timespec_to_nt_time(c_time
);
1915 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1916 d_printf("size: %d\n", (int)size
);
1919 TALLOC_FREE(snapshots
);
1920 cli_close(cli
, fnum
);
1925 /****************************************************************************
1926 Show all info we can get
1927 ****************************************************************************/
1929 static int cmd_allinfo(void)
1931 TALLOC_CTX
*ctx
= talloc_tos();
1935 name
= talloc_strdup(ctx
, client_get_cur_dir());
1940 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
1941 d_printf("allinfo <file>\n");
1944 name
= talloc_asprintf_append(name
, "%s", buf
);
1948 name
= client_clean_name(ctx
, name
);
1957 /****************************************************************************
1959 ****************************************************************************/
1961 static int do_put(const char *rname
, const char *lname
, bool reput
)
1963 TALLOC_CTX
*ctx
= talloc_tos();
1968 struct timespec tp_start
;
1969 struct cli_state
*targetcli
;
1970 char *targetname
= NULL
;
1971 struct push_state state
;
1974 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
1975 cli
, rname
, &targetcli
, &targetname
);
1976 if (!NT_STATUS_IS_OK(status
)) {
1977 d_printf("Failed to open %s: %s\n", rname
, nt_errstr(status
));
1981 clock_gettime_mono(&tp_start
);
1984 status
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
, DENY_NONE
, &fnum
);
1985 if (NT_STATUS_IS_OK(status
)) {
1986 if (!NT_STATUS_IS_OK(status
= cli_qfileinfo_basic(
1987 targetcli
, fnum
, NULL
,
1989 NULL
, NULL
, NULL
)) &&
1990 !NT_STATUS_IS_OK(status
= cli_getattrE(
1991 targetcli
, fnum
, NULL
,
1994 d_printf("getattrib: %s\n", nt_errstr(status
));
1999 status
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
, &fnum
);
2002 if (!NT_STATUS_IS_OK(status
)) {
2003 d_printf("%s opening remote file %s\n", nt_errstr(status
),
2008 /* allow files to be piped into smbclient
2011 Note that in this case this function will exit(0) rather
2013 if (!strcmp(lname
, "-")) {
2015 /* size of file is not known */
2017 f
= fopen(lname
, "r");
2019 if (fseek(f
, start
, SEEK_SET
) == -1) {
2020 d_printf("Error seeking local file\n");
2028 d_printf("Error opening local file %s\n",lname
);
2032 DEBUG(1,("putting file %s as %s ",lname
,
2035 setvbuf(f
, NULL
, _IOFBF
, io_bufsize
);
2040 status
= cli_push(targetcli
, fnum
, 0, 0, io_bufsize
, push_source
,
2042 if (!NT_STATUS_IS_OK(status
)) {
2043 d_fprintf(stderr
, "cli_push returned %s\n", nt_errstr(status
));
2047 status
= cli_close(targetcli
, fnum
);
2048 if (!NT_STATUS_IS_OK(status
)) {
2049 d_printf("%s closing remote file %s\n", nt_errstr(status
),
2062 struct timespec tp_end
;
2065 clock_gettime_mono(&tp_end
);
2066 this_time
= nsec_time_diff(&tp_end
,&tp_start
)/1000000;
2067 put_total_time_ms
+= this_time
;
2068 put_total_size
+= state
.nread
;
2070 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
2071 state
.nread
/ (1.024*this_time
+ 1.0e-4),
2072 put_total_size
/ (1.024*put_total_time_ms
)));
2077 popt_free_cmdline_auth_info();
2084 /****************************************************************************
2086 ****************************************************************************/
2088 static int cmd_put(void)
2090 TALLOC_CTX
*ctx
= talloc_tos();
2095 rname
= talloc_strdup(ctx
, client_get_cur_dir());
2100 if (!next_token_talloc(ctx
, &cmd_ptr
,&lname
,NULL
)) {
2101 d_printf("put <filename>\n");
2105 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2106 rname
= talloc_asprintf_append(rname
, "%s", buf
);
2108 rname
= talloc_asprintf_append(rname
, "%s", lname
);
2114 rname
= client_clean_name(ctx
, rname
);
2121 /* allow '-' to represent stdin
2122 jdblair, 24.jun.98 */
2123 if (!file_exist_stat(lname
, &st
, false) &&
2124 (strcmp(lname
,"-"))) {
2125 d_printf("%s does not exist\n",lname
);
2130 return do_put(rname
, lname
, false);
2133 /*************************************
2134 File list structure.
2135 *************************************/
2137 static struct file_list
{
2138 struct file_list
*prev
, *next
;
2143 /****************************************************************************
2144 Free a file_list structure.
2145 ****************************************************************************/
2147 static void free_file_list (struct file_list
*l_head
)
2149 struct file_list
*list
, *next
;
2151 for (list
= l_head
; list
; list
= next
) {
2153 DLIST_REMOVE(l_head
, list
);
2158 /****************************************************************************
2159 Seek in a directory/file list until you get something that doesn't start with
2161 ****************************************************************************/
2163 static bool seek_list(struct file_list
*list
, char *name
)
2166 trim_string(list
->file_path
,"./","\n");
2167 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
2176 /****************************************************************************
2177 Set the file selection mask.
2178 ****************************************************************************/
2180 static int cmd_select(void)
2182 TALLOC_CTX
*ctx
= talloc_tos();
2183 char *new_fs
= NULL
;
2184 next_token_talloc(ctx
, &cmd_ptr
,&new_fs
,NULL
)
2187 client_set_fileselection(new_fs
);
2189 client_set_fileselection("");
2194 /****************************************************************************
2195 Recursive file matching function act as find
2196 match must be always set to true when calling this function
2197 ****************************************************************************/
2199 static int file_find(TALLOC_CTX
*ctx
,
2200 struct file_list
**list
,
2201 const char *directory
,
2202 const char *expression
,
2206 struct file_list
*entry
;
2207 struct stat statbuf
;
2213 dir
= opendir(directory
);
2217 while ((dname
= readdirname(dir
))) {
2218 if (!strcmp("..", dname
))
2220 if (!strcmp(".", dname
))
2223 path
= talloc_asprintf(ctx
, "%s/%s", directory
, dname
);
2229 if (!match
|| !gen_fnmatch(expression
, dname
)) {
2231 ret
= stat(path
, &statbuf
);
2233 if (S_ISDIR(statbuf
.st_mode
)) {
2235 ret
= file_find(ctx
,
2242 d_printf("file_find: cannot stat file %s\n", path
);
2251 entry
= talloc_zero(ctx
, struct file_list
);
2253 d_printf("Out of memory in file_find\n");
2257 entry
->file_path
= talloc_move(entry
, &path
);
2258 entry
->isdir
= isdir
;
2259 DLIST_ADD(*list
, entry
);
2269 /****************************************************************************
2271 ****************************************************************************/
2273 static int cmd_mput(void)
2275 TALLOC_CTX
*ctx
= talloc_tos();
2278 while (next_token_talloc(ctx
, &cmd_ptr
,&p
,NULL
)) {
2280 struct file_list
*temp_list
;
2281 char *quest
, *lname
, *rname
;
2285 ret
= file_find(ctx
, &file_list
, ".", p
, true);
2287 free_file_list(file_list
);
2295 for (temp_list
= file_list
; temp_list
;
2296 temp_list
= temp_list
->next
) {
2299 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0) {
2302 trim_string(lname
, "./", "/");
2304 /* check if it's a directory */
2305 if (temp_list
->isdir
) {
2306 /* if (!recurse) continue; */
2309 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) {
2312 if (prompt
&& !yesno(quest
)) { /* No */
2313 /* Skip the directory */
2314 lname
[strlen(lname
)-1] = '/';
2315 if (!seek_list(temp_list
, lname
))
2319 if(asprintf(&rname
, "%s%s", client_get_cur_dir(), lname
) < 0) {
2322 normalize_name(rname
);
2325 client_clean_name(ctx
, rname
);
2326 if (tmp_rname
== NULL
) {
2330 rname
= smb_xstrdup(tmp_rname
);
2331 TALLOC_FREE(tmp_rname
);
2332 if (rname
== NULL
) {
2336 if (!NT_STATUS_IS_OK(cli_chkpath(cli
, rname
)) &&
2338 DEBUG (0, ("Unable to make dir, skipping..."));
2339 /* Skip the directory */
2340 lname
[strlen(lname
)-1] = '/';
2341 if (!seek_list(temp_list
, lname
)) {
2349 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) {
2352 if (prompt
&& !yesno(quest
)) {
2359 if (asprintf(&rname
, "%s%s", client_get_cur_dir(), lname
) < 0) {
2364 normalize_name(rname
);
2367 char *tmp_rname
= client_clean_name(ctx
, rname
);
2368 if (tmp_rname
== NULL
) {
2372 rname
= smb_xstrdup(tmp_rname
);
2373 TALLOC_FREE(tmp_rname
);
2374 if (rname
== NULL
) {
2378 do_put(rname
, lname
, false);
2380 free_file_list(file_list
);
2389 /****************************************************************************
2391 ****************************************************************************/
2393 static int do_cancel(int job
)
2395 if (cli_printjob_del(cli
, job
)) {
2396 d_printf("Job %d cancelled\n",job
);
2399 NTSTATUS status
= cli_nt_error(cli
);
2400 d_printf("Error cancelling job %d : %s\n",
2401 job
, nt_errstr(status
));
2406 /****************************************************************************
2408 ****************************************************************************/
2410 static int cmd_cancel(void)
2412 TALLOC_CTX
*ctx
= talloc_tos();
2416 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
,NULL
)) {
2417 d_printf("cancel <jobid> ...\n");
2423 } while (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
));
2428 /****************************************************************************
2430 ****************************************************************************/
2432 static int cmd_print(void)
2434 TALLOC_CTX
*ctx
= talloc_tos();
2439 if (!next_token_talloc(ctx
, &cmd_ptr
, &lname
,NULL
)) {
2440 d_printf("print <filename>\n");
2444 rname
= talloc_strdup(ctx
, lname
);
2448 p
= strrchr_m(rname
,'/');
2450 rname
= talloc_asprintf(ctx
,
2455 if (strequal(lname
,"-")) {
2456 rname
= talloc_asprintf(ctx
,
2464 return do_put(rname
, lname
, false);
2467 /****************************************************************************
2468 Show a print queue entry.
2469 ****************************************************************************/
2471 static void queue_fn(struct print_job_info
*p
)
2473 d_printf("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
);
2476 /****************************************************************************
2478 ****************************************************************************/
2480 static int cmd_queue(void)
2482 cli_print_queue(cli
, queue_fn
);
2486 /****************************************************************************
2488 ****************************************************************************/
2490 static NTSTATUS
do_del(struct cli_state
*cli_state
, struct file_info
*finfo
,
2493 TALLOC_CTX
*ctx
= talloc_tos();
2497 mask
= talloc_asprintf(ctx
,
2503 return NT_STATUS_NO_MEMORY
;
2506 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
2508 return NT_STATUS_OK
;
2511 status
= cli_unlink(cli_state
, mask
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2512 if (!NT_STATUS_IS_OK(status
)) {
2513 d_printf("%s deleting remote file %s\n",
2514 nt_errstr(status
), mask
);
2520 /****************************************************************************
2522 ****************************************************************************/
2524 static int cmd_del(void)
2526 TALLOC_CTX
*ctx
= talloc_tos();
2529 NTSTATUS status
= NT_STATUS_OK
;
2530 uint16_t attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
2533 attribute
|= FILE_ATTRIBUTE_DIRECTORY
;
2536 mask
= talloc_strdup(ctx
, client_get_cur_dir());
2540 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2541 d_printf("del <filename>\n");
2544 mask
= talloc_asprintf_append(mask
, "%s", buf
);
2548 mask
= client_clean_name(ctx
, mask
);
2553 status
= do_list(mask
,attribute
,do_del
,false,false);
2554 if (!NT_STATUS_IS_OK(status
)) {
2560 /****************************************************************************
2562 ****************************************************************************/
2564 static NTSTATUS
delete_remote_files_list(struct cli_state
*cli_state
,
2565 struct file_list
*flist
)
2567 NTSTATUS status
= NT_STATUS_OK
;
2568 struct file_list
*deltree_list_iter
= NULL
;
2570 for (deltree_list_iter
= flist
;
2571 deltree_list_iter
!= NULL
;
2572 deltree_list_iter
= deltree_list_iter
->next
) {
2573 if (CLI_DIRSEP_CHAR
== '/') {
2575 status
= cli_posix_unlink(cli_state
,
2576 deltree_list_iter
->file_path
);
2577 } else if (deltree_list_iter
->isdir
) {
2578 status
= cli_rmdir(cli_state
,
2579 deltree_list_iter
->file_path
);
2581 status
= cli_unlink(cli_state
,
2582 deltree_list_iter
->file_path
,
2583 FILE_ATTRIBUTE_SYSTEM
|
2584 FILE_ATTRIBUTE_HIDDEN
);
2586 if (!NT_STATUS_IS_OK(status
)) {
2587 d_printf("%s deleting remote %s %s\n",
2589 deltree_list_iter
->isdir
?
2590 "directory" : "file",
2591 deltree_list_iter
->file_path
);
2595 return NT_STATUS_OK
;
2598 /****************************************************************************
2599 Save a list of files to delete.
2600 ****************************************************************************/
2602 static struct file_list
*deltree_list_head
;
2604 static NTSTATUS
do_deltree_list(struct cli_state
*cli_state
,
2605 struct file_info
*finfo
,
2608 struct file_list
**file_list_head_pp
= &deltree_list_head
;
2609 struct file_list
*dt
= NULL
;
2611 if (!do_this_one(finfo
)) {
2612 return NT_STATUS_OK
;
2615 /* skip if this is . or .. */
2616 if (ISDOT(finfo
->name
) || ISDOTDOT(finfo
->name
)) {
2617 return NT_STATUS_OK
;
2620 dt
= talloc_zero(NULL
, struct file_list
);
2622 return NT_STATUS_NO_MEMORY
;
2625 /* create absolute filename for cli_ntcreate() */
2626 dt
->file_path
= talloc_asprintf(dt
,
2631 if (dt
->file_path
== NULL
) {
2633 return NT_STATUS_NO_MEMORY
;
2636 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
2640 DLIST_ADD(*file_list_head_pp
, dt
);
2641 return NT_STATUS_OK
;
2644 static int cmd_deltree(void)
2646 TALLOC_CTX
*ctx
= talloc_tos();
2648 NTSTATUS status
= NT_STATUS_OK
;
2649 struct file_list
*deltree_list_norecurse
= NULL
;
2650 struct file_list
*deltree_list_iter
= NULL
;
2651 uint16_t attribute
= FILE_ATTRIBUTE_SYSTEM
|
2652 FILE_ATTRIBUTE_HIDDEN
|
2653 FILE_ATTRIBUTE_DIRECTORY
;
2655 char *mask
= talloc_strdup(ctx
, client_get_cur_dir());
2659 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
2661 d_printf("deltree <filename>\n");
2664 mask
= talloc_asprintf_append(mask
, "%s", buf
);
2668 mask
= client_clean_name(ctx
, mask
);
2673 deltree_list_head
= NULL
;
2676 * Get the list of directories to
2677 * delete (in case mask has a wildcard).
2679 status
= do_list(mask
, attribute
, do_deltree_list
, false, true);
2680 if (!NT_STATUS_IS_OK(status
)) {
2683 deltree_list_norecurse
= deltree_list_head
;
2684 deltree_list_head
= NULL
;
2686 for (deltree_list_iter
= deltree_list_norecurse
;
2687 deltree_list_iter
!= NULL
;
2688 deltree_list_iter
= deltree_list_iter
->next
) {
2690 if (deltree_list_iter
->isdir
== false) {
2691 /* Just a regular file. */
2692 if (CLI_DIRSEP_CHAR
== '/') {
2694 status
= cli_posix_unlink(cli
,
2695 deltree_list_iter
->file_path
);
2697 status
= cli_unlink(cli
,
2698 deltree_list_iter
->file_path
,
2699 FILE_ATTRIBUTE_SYSTEM
|
2700 FILE_ATTRIBUTE_HIDDEN
);
2702 if (!NT_STATUS_IS_OK(status
)) {
2709 * Get the list of files or directories to
2710 * delete in depth order.
2712 status
= do_list(deltree_list_iter
->file_path
,
2717 if (!NT_STATUS_IS_OK(status
)) {
2720 status
= delete_remote_files_list(cli
, deltree_list_head
);
2721 free_file_list(deltree_list_head
);
2722 deltree_list_head
= NULL
;
2723 if (!NT_STATUS_IS_OK(status
)) {
2728 free_file_list(deltree_list_norecurse
);
2729 free_file_list(deltree_list_head
);
2734 free_file_list(deltree_list_norecurse
);
2735 free_file_list(deltree_list_head
);
2736 deltree_list_head
= NULL
;
2741 /****************************************************************************
2742 Wildcard delete some files.
2743 ****************************************************************************/
2745 static int cmd_wdel(void)
2747 TALLOC_CTX
*ctx
= talloc_tos();
2751 struct cli_state
*targetcli
;
2752 char *targetname
= NULL
;
2755 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2756 d_printf("wdel 0x<attrib> <wcard>\n");
2760 attribute
= (uint16_t)strtol(buf
, (char **)NULL
, 16);
2762 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2763 d_printf("wdel 0x<attrib> <wcard>\n");
2767 mask
= talloc_asprintf(ctx
, "%s%s",
2768 client_get_cur_dir(),
2773 mask
= client_clean_name(ctx
, mask
);
2778 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
2779 cli
, mask
, &targetcli
, &targetname
);
2780 if (!NT_STATUS_IS_OK(status
)) {
2781 d_printf("cmd_wdel %s: %s\n", mask
, nt_errstr(status
));
2785 status
= cli_unlink(targetcli
, targetname
, attribute
);
2786 if (!NT_STATUS_IS_OK(status
)) {
2787 d_printf("%s deleting remote files %s\n", nt_errstr(status
),
2793 /****************************************************************************
2794 ****************************************************************************/
2796 static int cmd_open(void)
2798 TALLOC_CTX
*ctx
= talloc_tos();
2801 char *targetname
= NULL
;
2802 struct cli_state
*targetcli
;
2803 uint16_t fnum
= (uint16_t)-1;
2806 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2807 d_printf("open <filename>\n");
2810 mask
= talloc_asprintf(ctx
,
2812 client_get_cur_dir(),
2818 mask
= client_clean_name(ctx
, mask
);
2823 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
2824 cli
, mask
, &targetcli
, &targetname
);
2825 if (!NT_STATUS_IS_OK(status
)) {
2826 d_printf("open %s: %s\n", mask
, nt_errstr(status
));
2830 status
= cli_ntcreate(targetcli
, targetname
, 0,
2831 FILE_READ_DATA
|FILE_WRITE_DATA
, 0,
2832 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
2833 0x0, 0x0, &fnum
, NULL
);
2834 if (!NT_STATUS_IS_OK(status
)) {
2835 status
= cli_ntcreate(targetcli
, targetname
, 0,
2837 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
,
2838 0x0, 0x0, &fnum
, NULL
);
2839 if (NT_STATUS_IS_OK(status
)) {
2840 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
2842 d_printf("Failed to open file %s. %s\n",
2843 targetname
, nt_errstr(status
));
2846 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
2851 static int cmd_posix_encrypt(void)
2853 TALLOC_CTX
*ctx
= talloc_tos();
2854 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2855 char *domain
= NULL
;
2857 char *password
= NULL
;
2858 struct cli_credentials
*creds
= NULL
;
2859 struct cli_credentials
*lcreds
= NULL
;
2861 if (next_token_talloc(ctx
, &cmd_ptr
, &domain
, NULL
)) {
2863 if (!next_token_talloc(ctx
, &cmd_ptr
, &user
, NULL
)) {
2864 d_printf("posix_encrypt domain user password\n");
2868 if (!next_token_talloc(ctx
, &cmd_ptr
, &password
, NULL
)) {
2869 d_printf("posix_encrypt domain user password\n");
2873 lcreds
= cli_session_creds_init(ctx
,
2878 false, /* use_kerberos */
2879 false, /* fallback_after_kerberos */
2880 false, /* use_ccache */
2881 false); /* password_is_nt_hash */
2882 if (lcreds
== NULL
) {
2883 d_printf("cli_session_creds_init() failed.\n");
2888 bool auth_requested
= false;
2890 creds
= get_cmdline_auth_info_creds(
2891 popt_get_cmdline_auth_info());
2893 auth_requested
= cli_credentials_authentication_requested(creds
);
2894 if (!auth_requested
) {
2895 d_printf("posix_encrypt domain user password\n");
2900 status
= cli_smb1_setup_encryption(cli
, creds
);
2901 /* gensec currently references the creds so we can't free them here */
2902 talloc_unlink(ctx
, lcreds
);
2903 if (!NT_STATUS_IS_OK(status
)) {
2904 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status
));
2906 d_printf("encryption on\n");
2913 /****************************************************************************
2914 ****************************************************************************/
2916 static int cmd_posix_open(void)
2918 TALLOC_CTX
*ctx
= talloc_tos();
2921 char *targetname
= NULL
;
2922 struct cli_state
*targetcli
;
2927 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2928 d_printf("posix_open <filename> 0<mode>\n");
2931 mask
= talloc_asprintf(ctx
,
2933 client_get_cur_dir(),
2938 mask
= client_clean_name(ctx
, mask
);
2943 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2944 d_printf("posix_open <filename> 0<mode>\n");
2947 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
2949 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
2950 cli
, mask
, &targetcli
, &targetname
);
2951 if (!NT_STATUS_IS_OK(status
)) {
2952 d_printf("posix_open %s: %s\n", mask
, nt_errstr(status
));
2956 status
= cli_posix_open(targetcli
, targetname
, O_CREAT
|O_RDWR
, mode
,
2958 if (!NT_STATUS_IS_OK(status
)) {
2959 status
= cli_posix_open(targetcli
, targetname
,
2960 O_CREAT
|O_RDONLY
, mode
, &fnum
);
2961 if (!NT_STATUS_IS_OK(status
)) {
2962 d_printf("Failed to open file %s. %s\n", targetname
,
2965 d_printf("posix_open file %s: for readonly fnum %d\n",
2969 d_printf("posix_open file %s: for read/write fnum %d\n",
2976 static int cmd_posix_mkdir(void)
2978 TALLOC_CTX
*ctx
= talloc_tos();
2981 char *targetname
= NULL
;
2982 struct cli_state
*targetcli
;
2986 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2987 d_printf("posix_mkdir <filename> 0<mode>\n");
2990 mask
= talloc_asprintf(ctx
,
2992 client_get_cur_dir(),
2997 mask
= client_clean_name(ctx
, mask
);
3002 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3003 d_printf("posix_mkdir <filename> 0<mode>\n");
3006 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
3008 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3009 cli
, mask
, &targetcli
, &targetname
);
3010 if (!NT_STATUS_IS_OK(status
)) {
3011 d_printf("posix_mkdir %s: %s\n", mask
, nt_errstr(status
));
3015 status
= cli_posix_mkdir(targetcli
, targetname
, mode
);
3016 if (!NT_STATUS_IS_OK(status
)) {
3017 d_printf("Failed to open file %s. %s\n",
3018 targetname
, nt_errstr(status
));
3020 d_printf("posix_mkdir created directory %s\n", targetname
);
3025 static int cmd_posix_unlink(void)
3027 TALLOC_CTX
*ctx
= talloc_tos();
3030 char *targetname
= NULL
;
3031 struct cli_state
*targetcli
;
3034 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3035 d_printf("posix_unlink <filename>\n");
3038 mask
= talloc_asprintf(ctx
,
3040 client_get_cur_dir(),
3045 mask
= client_clean_name(ctx
, mask
);
3050 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3051 cli
, mask
, &targetcli
, &targetname
);
3052 if (!NT_STATUS_IS_OK(status
)) {
3053 d_printf("posix_unlink %s: %s\n", mask
, nt_errstr(status
));
3057 status
= cli_posix_unlink(targetcli
, targetname
);
3058 if (!NT_STATUS_IS_OK(status
)) {
3059 d_printf("Failed to unlink file %s. %s\n",
3060 targetname
, nt_errstr(status
));
3062 d_printf("posix_unlink deleted file %s\n", targetname
);
3068 static int cmd_posix_rmdir(void)
3070 TALLOC_CTX
*ctx
= talloc_tos();
3073 char *targetname
= NULL
;
3074 struct cli_state
*targetcli
;
3077 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3078 d_printf("posix_rmdir <filename>\n");
3081 mask
= talloc_asprintf(ctx
,
3083 client_get_cur_dir(),
3088 mask
= client_clean_name(ctx
, mask
);
3093 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3094 cli
, mask
, &targetcli
, &targetname
);
3095 if (!NT_STATUS_IS_OK(status
)) {
3096 d_printf("posix_rmdir %s: %s\n", mask
, nt_errstr(status
));
3100 status
= cli_posix_rmdir(targetcli
, targetname
);
3101 if (!NT_STATUS_IS_OK(status
)) {
3102 d_printf("Failed to unlink directory %s. %s\n",
3103 targetname
, nt_errstr(status
));
3105 d_printf("posix_rmdir deleted directory %s\n", targetname
);
3111 static int cmd_close(void)
3113 TALLOC_CTX
*ctx
= talloc_tos();
3118 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3119 d_printf("close <fnum>\n");
3124 /* We really should use the targetcli here.... */
3125 status
= cli_close(cli
, fnum
);
3126 if (!NT_STATUS_IS_OK(status
)) {
3127 d_printf("close %d: %s\n", fnum
, nt_errstr(status
));
3133 static int cmd_posix(void)
3135 TALLOC_CTX
*ctx
= talloc_tos();
3136 uint16_t major
, minor
;
3137 uint32_t caplow
, caphigh
;
3141 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
3142 d_printf("Server doesn't support UNIX CIFS extensions.\n");
3146 status
= cli_unix_extensions_version(cli
, &major
, &minor
, &caplow
,
3148 if (!NT_STATUS_IS_OK(status
)) {
3149 d_printf("Can't get UNIX CIFS extensions version from "
3150 "server: %s\n", nt_errstr(status
));
3154 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major
, (unsigned int)minor
);
3156 caps
= talloc_strdup(ctx
, "");
3160 if (caplow
& CIFS_UNIX_FCNTL_LOCKS_CAP
) {
3161 caps
= talloc_asprintf_append(caps
, "locks ");
3166 if (caplow
& CIFS_UNIX_POSIX_ACLS_CAP
) {
3167 caps
= talloc_asprintf_append(caps
, "acls ");
3172 if (caplow
& CIFS_UNIX_XATTTR_CAP
) {
3173 caps
= talloc_asprintf_append(caps
, "eas ");
3178 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
3179 caps
= talloc_asprintf_append(caps
, "pathnames ");
3184 if (caplow
& CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP
) {
3185 caps
= talloc_asprintf_append(caps
, "posix_path_operations ");
3190 if (caplow
& CIFS_UNIX_LARGE_READ_CAP
) {
3191 caps
= talloc_asprintf_append(caps
, "large_read ");
3196 if (caplow
& CIFS_UNIX_LARGE_WRITE_CAP
) {
3197 caps
= talloc_asprintf_append(caps
, "large_write ");
3202 if (caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP
) {
3203 caps
= talloc_asprintf_append(caps
, "posix_encrypt ");
3208 if (caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP
) {
3209 caps
= talloc_asprintf_append(caps
, "mandatory_posix_encrypt ");
3215 if (*caps
&& caps
[strlen(caps
)-1] == ' ') {
3216 caps
[strlen(caps
)-1] = '\0';
3219 d_printf("Server supports CIFS capabilities %s\n", caps
);
3221 status
= cli_set_unix_extensions_capabilities(cli
, major
, minor
,
3223 if (!NT_STATUS_IS_OK(status
)) {
3224 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
3229 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
3230 CLI_DIRSEP_CHAR
= '/';
3231 *CLI_DIRSEP_STR
= '/';
3232 client_set_cur_dir(CLI_DIRSEP_STR
);
3238 static int cmd_lock(void)
3240 TALLOC_CTX
*ctx
= talloc_tos();
3242 uint64_t start
, len
;
3243 enum brl_type lock_type
;
3247 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3248 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3253 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3254 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3258 if (*buf
== 'r' || *buf
== 'R') {
3259 lock_type
= READ_LOCK
;
3260 } else if (*buf
== 'w' || *buf
== 'W') {
3261 lock_type
= WRITE_LOCK
;
3263 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3267 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3268 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3272 start
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
3274 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3275 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3279 len
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
3281 status
= cli_posix_lock(cli
, fnum
, start
, len
, true, lock_type
);
3282 if (!NT_STATUS_IS_OK(status
)) {
3283 d_printf("lock failed %d: %s\n", fnum
, nt_errstr(status
));
3289 static int cmd_unlock(void)
3291 TALLOC_CTX
*ctx
= talloc_tos();
3293 uint64_t start
, len
;
3297 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3298 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3303 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3304 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3308 start
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
3310 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3311 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3315 len
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
3317 status
= cli_posix_unlock(cli
, fnum
, start
, len
);
3318 if (!NT_STATUS_IS_OK(status
)) {
3319 d_printf("unlock failed %d: %s\n", fnum
, nt_errstr(status
));
3325 static int cmd_posix_whoami(void)
3327 TALLOC_CTX
*ctx
= talloc_tos();
3328 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
3331 uint32_t num_gids
= 0;
3332 uint32_t num_sids
= 0;
3333 uint64_t *gids
= NULL
;
3334 struct dom_sid
*sids
= NULL
;
3338 status
= cli_posix_whoami(cli
,
3348 if (!NT_STATUS_IS_OK(status
)) {
3349 d_printf("posix_whoami failed with error %s\n", nt_errstr(status
));
3353 d_printf("GUEST:%s\n", guest
? "True" : "False");
3354 d_printf("UID:%" PRIu64
"\n", uid
);
3355 d_printf("GID:%" PRIu64
"\n", gid
);
3356 d_printf("NUM_GIDS:%" PRIu32
"\n", num_gids
);
3357 for (i
= 0; i
< num_gids
; i
++) {
3358 d_printf("GIDS[%" PRIu32
"]:%" PRIu64
"\n", i
, gids
[i
]);
3360 d_printf("NUM_SIDS:%" PRIu32
"\n", num_sids
);
3361 for (i
= 0; i
< num_sids
; i
++) {
3362 char *sid_str
= dom_sid_string(ctx
, &sids
[i
]);
3363 d_printf("SIDS[%" PRIu32
"]:%s\n", i
, sid_str
);
3364 TALLOC_FREE(sid_str
);
3370 /****************************************************************************
3372 ****************************************************************************/
3374 static int cmd_rmdir(void)
3376 TALLOC_CTX
*ctx
= talloc_tos();
3379 char *targetname
= NULL
;
3380 struct cli_state
*targetcli
;
3383 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3384 d_printf("rmdir <dirname>\n");
3387 mask
= talloc_asprintf(ctx
,
3389 client_get_cur_dir(),
3394 mask
= client_clean_name(ctx
, mask
);
3399 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3400 cli
, mask
, &targetcli
, &targetname
);
3401 if (!NT_STATUS_IS_OK(status
)) {
3402 d_printf("rmdir %s: %s\n", mask
, nt_errstr(status
));
3406 status
= cli_rmdir(targetcli
, targetname
);
3407 if (!NT_STATUS_IS_OK(status
)) {
3408 d_printf("%s removing remote directory file %s\n",
3409 nt_errstr(status
), mask
);
3415 /****************************************************************************
3417 ****************************************************************************/
3419 static int cmd_link(void)
3421 TALLOC_CTX
*ctx
= talloc_tos();
3422 char *oldname
= NULL
;
3423 char *newname
= NULL
;
3426 char *targetname
= NULL
;
3427 struct cli_state
*targetcli
;
3430 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3431 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3432 d_printf("link <oldname> <newname>\n");
3435 oldname
= talloc_asprintf(ctx
,
3437 client_get_cur_dir(),
3442 oldname
= client_clean_name(ctx
, oldname
);
3443 if (oldname
== NULL
) {
3446 newname
= talloc_asprintf(ctx
,
3448 client_get_cur_dir(),
3453 newname
= client_clean_name(ctx
, newname
);
3454 if (newname
== NULL
) {
3458 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3459 cli
, oldname
, &targetcli
, &targetname
);
3460 if (!NT_STATUS_IS_OK(status
)) {
3461 d_printf("link %s: %s\n", oldname
, nt_errstr(status
));
3465 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3466 d_printf("Server doesn't support UNIX CIFS calls.\n");
3470 status
= cli_posix_hardlink(targetcli
, targetname
, newname
);
3471 if (!NT_STATUS_IS_OK(status
)) {
3472 d_printf("%s linking files (%s -> %s)\n",
3473 nt_errstr(status
), newname
, oldname
);
3479 /****************************************************************************
3481 ****************************************************************************/
3483 static int cmd_readlink(void)
3485 TALLOC_CTX
*ctx
= talloc_tos();
3488 char *targetname
= NULL
;
3489 char linkname
[PATH_MAX
+1];
3490 struct cli_state
*targetcli
;
3493 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3494 d_printf("readlink <name>\n");
3497 name
= talloc_asprintf(ctx
,
3499 client_get_cur_dir(),
3504 name
= client_clean_name(ctx
, name
);
3509 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3510 cli
, name
, &targetcli
, &targetname
);
3511 if (!NT_STATUS_IS_OK(status
)) {
3512 d_printf("readlink %s: %s\n", name
, nt_errstr(status
));
3516 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3517 d_printf("Server doesn't support UNIX CIFS calls.\n");
3521 status
= cli_posix_readlink(targetcli
, name
, linkname
, PATH_MAX
+1);
3522 if (!NT_STATUS_IS_OK(status
)) {
3523 d_printf("%s readlink on file %s\n",
3524 nt_errstr(status
), name
);
3528 d_printf("%s -> %s\n", name
, linkname
);
3534 /****************************************************************************
3536 ****************************************************************************/
3538 static int cmd_symlink(void)
3540 TALLOC_CTX
*ctx
= talloc_tos();
3541 char *link_target
= NULL
;
3542 char *newname
= NULL
;
3545 struct cli_state
*newcli
;
3548 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3549 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3550 d_printf("symlink <link_target> <newname>\n");
3553 /* Oldname (link target) must be an untouched blob. */
3556 if (SERVER_HAS_UNIX_CIFS(cli
)) {
3557 newname
= talloc_asprintf(ctx
, "%s%s", client_get_cur_dir(),
3562 newname
= client_clean_name(ctx
, newname
);
3563 if (newname
== NULL
) {
3566 /* New name must be present in share namespace. */
3567 status
= cli_resolve_path(ctx
, "",
3568 popt_get_cmdline_auth_info(), cli
, newname
,
3570 if (!NT_STATUS_IS_OK(status
)) {
3571 d_printf("link %s: %s\n", newname
,
3575 status
= cli_posix_symlink(newcli
, link_target
, newname
);
3577 status
= cli_symlink(
3578 cli
, link_target
, buf2
,
3579 buf2
[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE
);
3582 if (!NT_STATUS_IS_OK(status
)) {
3583 d_printf("%s symlinking files (%s -> %s)\n",
3584 nt_errstr(status
), newname
, link_target
);
3591 /****************************************************************************
3593 ****************************************************************************/
3595 static int cmd_chmod(void)
3597 TALLOC_CTX
*ctx
= talloc_tos();
3601 char *targetname
= NULL
;
3602 struct cli_state
*targetcli
;
3606 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3607 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3608 d_printf("chmod mode file\n");
3611 src
= talloc_asprintf(ctx
,
3613 client_get_cur_dir(),
3618 src
= client_clean_name(ctx
, src
);
3623 mode
= (mode_t
)strtol(buf
, NULL
, 8);
3625 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3626 cli
, src
, &targetcli
, &targetname
);
3627 if (!NT_STATUS_IS_OK(status
)) {
3628 d_printf("chmod %s: %s\n", src
, nt_errstr(status
));
3632 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3633 d_printf("Server doesn't support UNIX CIFS calls.\n");
3637 status
= cli_posix_chmod(targetcli
, targetname
, mode
);
3638 if (!NT_STATUS_IS_OK(status
)) {
3639 d_printf("%s chmod file %s 0%o\n",
3640 nt_errstr(status
), src
, (unsigned int)mode
);
3647 static const char *filetype_to_str(mode_t mode
)
3649 if (S_ISREG(mode
)) {
3650 return "regular file";
3651 } else if (S_ISDIR(mode
)) {
3655 if (S_ISCHR(mode
)) {
3656 return "character device";
3660 if (S_ISBLK(mode
)) {
3661 return "block device";
3665 if (S_ISFIFO(mode
)) {
3670 if (S_ISLNK(mode
)) {
3671 return "symbolic link";
3675 if (S_ISSOCK(mode
)) {
3682 static char rwx_to_str(mode_t m
, mode_t bt
, char ret
)
3691 static char *unix_mode_to_str(char *s
, mode_t m
)
3694 const char *str
= filetype_to_str(m
);
3710 *p
++ = str
[1] == 'y' ? 'l' : 's';
3717 *p
++ = rwx_to_str(m
, S_IRUSR
, 'r');
3718 *p
++ = rwx_to_str(m
, S_IWUSR
, 'w');
3719 *p
++ = rwx_to_str(m
, S_IXUSR
, 'x');
3720 *p
++ = rwx_to_str(m
, S_IRGRP
, 'r');
3721 *p
++ = rwx_to_str(m
, S_IWGRP
, 'w');
3722 *p
++ = rwx_to_str(m
, S_IXGRP
, 'x');
3723 *p
++ = rwx_to_str(m
, S_IROTH
, 'r');
3724 *p
++ = rwx_to_str(m
, S_IWOTH
, 'w');
3725 *p
++ = rwx_to_str(m
, S_IXOTH
, 'x');
3730 /****************************************************************************
3731 Utility function for UNIX getfacl.
3732 ****************************************************************************/
3734 static char *perms_to_string(fstring permstr
, unsigned char perms
)
3736 fstrcpy(permstr
, "---");
3737 if (perms
& SMB_POSIX_ACL_READ
) {
3740 if (perms
& SMB_POSIX_ACL_WRITE
) {
3743 if (perms
& SMB_POSIX_ACL_EXECUTE
) {
3749 /****************************************************************************
3751 ****************************************************************************/
3753 static int cmd_getfacl(void)
3755 TALLOC_CTX
*ctx
= talloc_tos();
3758 char *targetname
= NULL
;
3759 struct cli_state
*targetcli
;
3760 uint16_t major
, minor
;
3761 uint32_t caplow
, caphigh
;
3762 char *retbuf
= NULL
;
3764 SMB_STRUCT_STAT sbuf
;
3765 uint16_t num_file_acls
= 0;
3766 uint16_t num_dir_acls
= 0;
3770 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
3771 d_printf("getfacl filename\n");
3774 src
= talloc_asprintf(ctx
,
3776 client_get_cur_dir(),
3781 src
= client_clean_name(ctx
, src
);
3786 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3787 cli
, src
, &targetcli
, &targetname
);
3788 if (!NT_STATUS_IS_OK(status
)) {
3789 d_printf("stat %s: %s\n", src
, nt_errstr(status
));
3793 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3794 d_printf("Server doesn't support UNIX CIFS calls.\n");
3798 status
= cli_unix_extensions_version(targetcli
, &major
, &minor
,
3800 if (!NT_STATUS_IS_OK(status
)) {
3801 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3806 if (!(caplow
& CIFS_UNIX_POSIX_ACLS_CAP
)) {
3807 d_printf("This server supports UNIX extensions "
3808 "but doesn't support POSIX ACLs.\n");
3812 status
= cli_posix_stat(targetcli
, targetname
, &sbuf
);
3813 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli
, targetname
, &sbuf
))) {
3814 d_printf("%s getfacl doing a stat on file %s\n",
3815 nt_errstr(status
), src
);
3819 status
= cli_posix_getacl(targetcli
, targetname
, ctx
, &rb_size
, &retbuf
);
3820 if (!NT_STATUS_IS_OK(status
)) {
3821 d_printf("%s getfacl file %s\n",
3822 nt_errstr(status
), src
);
3826 /* ToDo : Print out the ACL values. */
3827 if (rb_size
< 6 || SVAL(retbuf
,0) != SMB_POSIX_ACL_VERSION
) {
3828 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3829 src
, (unsigned int)CVAL(retbuf
,0) );
3833 num_file_acls
= SVAL(retbuf
,2);
3834 num_dir_acls
= SVAL(retbuf
,4);
3835 if (rb_size
!= SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)) {
3836 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3838 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)),
3839 (unsigned int)rb_size
);
3843 d_printf("# file: %s\n", src
);
3844 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf
.st_ex_uid
, (unsigned int)sbuf
.st_ex_gid
);
3846 if (num_file_acls
== 0 && num_dir_acls
== 0) {
3847 d_printf("No acls found.\n");
3850 for (i
= 0; i
< num_file_acls
; i
++) {
3853 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
));
3854 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
3857 case SMB_POSIX_ACL_USER_OBJ
:
3860 case SMB_POSIX_ACL_USER
:
3861 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3862 d_printf("user:%u:", uorg
);
3864 case SMB_POSIX_ACL_GROUP_OBJ
:
3865 d_printf("group::");
3867 case SMB_POSIX_ACL_GROUP
:
3868 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3869 d_printf("group:%u:", uorg
);
3871 case SMB_POSIX_ACL_MASK
:
3874 case SMB_POSIX_ACL_OTHER
:
3875 d_printf("other::");
3878 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3879 src
, (unsigned int)tagtype
);
3884 d_printf("%s\n", perms_to_string(permstring
, perms
));
3887 for (i
= 0; i
< num_dir_acls
; i
++) {
3890 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
));
3891 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
3894 case SMB_POSIX_ACL_USER_OBJ
:
3895 d_printf("default:user::");
3897 case SMB_POSIX_ACL_USER
:
3898 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3899 d_printf("default:user:%u:", uorg
);
3901 case SMB_POSIX_ACL_GROUP_OBJ
:
3902 d_printf("default:group::");
3904 case SMB_POSIX_ACL_GROUP
:
3905 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3906 d_printf("default:group:%u:", uorg
);
3908 case SMB_POSIX_ACL_MASK
:
3909 d_printf("default:mask::");
3911 case SMB_POSIX_ACL_OTHER
:
3912 d_printf("default:other::");
3915 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3916 src
, (unsigned int)tagtype
);
3921 d_printf("%s\n", perms_to_string(permstring
, perms
));
3927 /****************************************************************************
3928 Get the EA list of a file
3929 ****************************************************************************/
3931 static int cmd_geteas(void)
3933 TALLOC_CTX
*ctx
= talloc_tos();
3936 char *targetname
= NULL
;
3937 struct cli_state
*targetcli
;
3940 struct ea_struct
*eas
;
3942 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
3943 d_printf("geteas filename\n");
3946 src
= talloc_asprintf(ctx
,
3948 client_get_cur_dir(),
3953 src
= client_clean_name(ctx
, src
);
3958 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
3959 cli
, src
, &targetcli
, &targetname
);
3960 if (!NT_STATUS_IS_OK(status
)) {
3961 d_printf("stat %s: %s\n", src
, nt_errstr(status
));
3965 status
= cli_get_ea_list_path(targetcli
, targetname
, talloc_tos(),
3967 if (!NT_STATUS_IS_OK(status
)) {
3968 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status
));
3972 for (i
=0; i
<num_eas
; i
++) {
3973 d_printf("%s (%d) =\n", eas
[i
].name
, (int)eas
[i
].flags
);
3974 dump_data_file(eas
[i
].value
.data
, eas
[i
].value
.length
, false,
3984 /****************************************************************************
3986 ****************************************************************************/
3988 static int cmd_setea(void)
3990 TALLOC_CTX
*ctx
= talloc_tos();
3993 char *eaname
= NULL
;
3994 char *eavalue
= NULL
;
3995 char *targetname
= NULL
;
3996 struct cli_state
*targetcli
;
3999 if (!next_token_talloc(ctx
, &cmd_ptr
, &name
, NULL
)
4000 || !next_token_talloc(ctx
, &cmd_ptr
, &eaname
, NULL
)) {
4001 d_printf("setea filename eaname value\n");
4004 if (!next_token_talloc(ctx
, &cmd_ptr
, &eavalue
, NULL
)) {
4005 eavalue
= talloc_strdup(ctx
, "");
4007 src
= talloc_asprintf(ctx
,
4009 client_get_cur_dir(),
4014 src
= client_clean_name(ctx
, src
);
4019 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
4020 cli
, src
, &targetcli
, &targetname
);
4021 if (!NT_STATUS_IS_OK(status
)) {
4022 d_printf("stat %s: %s\n", src
, nt_errstr(status
));
4026 status
= cli_set_ea_path(targetcli
, targetname
, eaname
, eavalue
,
4028 if (!NT_STATUS_IS_OK(status
)) {
4029 d_printf("set_ea %s: %s\n", src
, nt_errstr(status
));
4036 /****************************************************************************
4038 ****************************************************************************/
4040 static int cmd_stat(void)
4042 TALLOC_CTX
*ctx
= talloc_tos();
4045 char *targetname
= NULL
;
4046 struct cli_state
*targetcli
;
4048 SMB_STRUCT_STAT sbuf
;
4053 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
4054 d_printf("stat file\n");
4057 src
= talloc_asprintf(ctx
,
4059 client_get_cur_dir(),
4064 src
= client_clean_name(ctx
, src
);
4069 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
4070 cli
, src
, &targetcli
, &targetname
);
4071 if (!NT_STATUS_IS_OK(status
)) {
4072 d_printf("stat %s: %s\n", src
, nt_errstr(status
));
4076 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
4077 d_printf("Server doesn't support UNIX CIFS calls.\n");
4081 status
= cli_posix_stat(targetcli
, targetname
, &sbuf
);
4082 if (!NT_STATUS_IS_OK(status
)) {
4083 d_printf("%s stat file %s\n",
4084 nt_errstr(status
), src
);
4088 /* Print out the stat values. */
4089 d_printf("File: %s\n", src
);
4090 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
4091 (double)sbuf
.st_ex_size
,
4092 (unsigned int)sbuf
.st_ex_blocks
,
4093 filetype_to_str(sbuf
.st_ex_mode
));
4095 #if defined(S_ISCHR) && defined(S_ISBLK)
4096 if (S_ISCHR(sbuf
.st_ex_mode
) || S_ISBLK(sbuf
.st_ex_mode
)) {
4097 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
4098 (double)sbuf
.st_ex_ino
,
4099 (unsigned int)sbuf
.st_ex_nlink
,
4100 unix_dev_major(sbuf
.st_ex_rdev
),
4101 unix_dev_minor(sbuf
.st_ex_rdev
));
4104 d_printf("Inode: %.0f\tLinks: %u\n",
4105 (double)sbuf
.st_ex_ino
,
4106 (unsigned int)sbuf
.st_ex_nlink
);
4108 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
4109 ((int)sbuf
.st_ex_mode
& 0777),
4110 unix_mode_to_str(mode_str
, sbuf
.st_ex_mode
),
4111 (unsigned int)sbuf
.st_ex_uid
,
4112 (unsigned int)sbuf
.st_ex_gid
);
4114 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_atime
);
4115 lt
= localtime(&tmp_time
);
4117 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
4119 fstrcpy(mode_str
, "unknown");
4121 d_printf("Access: %s\n", mode_str
);
4123 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_mtime
);
4124 lt
= localtime(&tmp_time
);
4126 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
4128 fstrcpy(mode_str
, "unknown");
4130 d_printf("Modify: %s\n", mode_str
);
4132 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_ctime
);
4133 lt
= localtime(&tmp_time
);
4135 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
4137 fstrcpy(mode_str
, "unknown");
4139 d_printf("Change: %s\n", mode_str
);
4145 /****************************************************************************
4147 ****************************************************************************/
4149 static int cmd_chown(void)
4151 TALLOC_CTX
*ctx
= talloc_tos();
4155 char *buf
, *buf2
, *buf3
;
4156 struct cli_state
*targetcli
;
4157 char *targetname
= NULL
;
4160 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
4161 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
) ||
4162 !next_token_talloc(ctx
, &cmd_ptr
,&buf3
,NULL
)) {
4163 d_printf("chown uid gid file\n");
4167 uid
= (uid_t
)atoi(buf
);
4168 gid
= (gid_t
)atoi(buf2
);
4170 src
= talloc_asprintf(ctx
,
4172 client_get_cur_dir(),
4177 src
= client_clean_name(ctx
, src
);
4181 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
4182 cli
, src
, &targetcli
, &targetname
);
4183 if (!NT_STATUS_IS_OK(status
)) {
4184 d_printf("chown %s: %s\n", src
, nt_errstr(status
));
4188 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
4189 d_printf("Server doesn't support UNIX CIFS calls.\n");
4193 status
= cli_posix_chown(targetcli
, targetname
, uid
, gid
);
4194 if (!NT_STATUS_IS_OK(status
)) {
4195 d_printf("%s chown file %s uid=%d, gid=%d\n",
4196 nt_errstr(status
), src
, (int)uid
, (int)gid
);
4203 /****************************************************************************
4205 ****************************************************************************/
4207 static int cmd_rename(void)
4209 TALLOC_CTX
*ctx
= talloc_tos();
4212 struct cli_state
*targetcli
;
4216 bool replace
= false;
4218 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
4219 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
4220 d_printf("rename <src> <dest> [-f]\n");
4224 src
= talloc_asprintf(ctx
,
4226 client_get_cur_dir(),
4231 src
= client_clean_name(ctx
, src
);
4236 dest
= talloc_asprintf(ctx
,
4238 client_get_cur_dir(),
4243 dest
= client_clean_name(ctx
, dest
);
4248 if (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
) &&
4249 strcsequal(buf
, "-f")) {
4253 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
4254 cli
, src
, &targetcli
, &targetsrc
);
4255 if (!NT_STATUS_IS_OK(status
)) {
4256 d_printf("rename %s: %s\n", src
, nt_errstr(status
));
4260 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
4261 cli
, dest
, &targetcli
, &targetdest
);
4262 if (!NT_STATUS_IS_OK(status
)) {
4263 d_printf("rename %s: %s\n", dest
, nt_errstr(status
));
4267 status
= cli_rename(targetcli
, targetsrc
, targetdest
, replace
);
4268 if (!NT_STATUS_IS_OK(status
)) {
4269 d_printf("%s renaming files %s -> %s \n",
4279 struct scopy_timing
{
4280 struct timespec tp_start
;
4283 static int scopy_status(off_t written
, void *priv
)
4285 struct timespec tp_end
;
4286 unsigned int scopy_total_time_ms
;
4287 struct scopy_timing
*st
= priv
;
4289 clock_gettime_mono(&tp_end
);
4290 scopy_total_time_ms
= nsec_time_diff(&tp_end
,&st
->tp_start
)/1000000;
4292 DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
4293 (intmax_t)written
, written
/ (1.024*scopy_total_time_ms
)));
4298 /****************************************************************************
4299 Server-Side copy some file.
4300 ****************************************************************************/
4302 static int cmd_scopy(void)
4304 TALLOC_CTX
*ctx
= talloc_tos();
4307 struct cli_state
*targetcli
;
4310 uint32_t DesiredAccess
, ShareAccess
, CreateDisposition
, CreateOptions
;
4311 struct smb_create_returns cr
;
4312 uint16_t destfnum
= (uint16_t)-1;
4313 uint16_t srcfnum
= (uint16_t)-1;
4315 struct scopy_timing st
;
4319 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
4320 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
4321 d_printf("scopy <src> <dest>\n");
4325 src
= talloc_asprintf(ctx
,
4327 client_get_cur_dir(),
4332 src
= client_clean_name(ctx
, src
);
4337 dest
= talloc_asprintf(ctx
,
4339 client_get_cur_dir(),
4344 dest
= client_clean_name(ctx
, dest
);
4349 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
4350 cli
, src
, &targetcli
, &targetsrc
);
4351 if (!NT_STATUS_IS_OK(status
)) {
4352 d_printf("scopy %s: %s\n", src
, nt_errstr(status
));
4356 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
4357 cli
, dest
, &targetcli
, &targetdest
);
4358 if (!NT_STATUS_IS_OK(status
)) {
4359 d_printf("scopy %s: %s\n", dest
, nt_errstr(status
));
4364 DesiredAccess
= (FILE_READ_DATA
|FILE_READ_EA
|FILE_READ_ATTRIBUTES
|
4365 READ_CONTROL_ACCESS
|SYNCHRONIZE_ACCESS
);
4366 ShareAccess
= FILE_SHARE_READ
|FILE_SHARE_DELETE
;
4367 CreateDisposition
= FILE_OPEN
;
4368 CreateOptions
= (FILE_SEQUENTIAL_ONLY
|FILE_NON_DIRECTORY_FILE
|
4369 FILE_OPEN_REPARSE_POINT
);
4370 status
= cli_ntcreate(targetcli
, targetsrc
, 0, DesiredAccess
, 0,
4371 ShareAccess
, CreateDisposition
, CreateOptions
, 0x0,
4373 if (!NT_STATUS_IS_OK(status
)) {
4374 d_printf("Failed to open file %s. %s\n",
4375 targetsrc
, nt_errstr(status
));
4379 DesiredAccess
= (FILE_READ_DATA
|FILE_WRITE_DATA
|FILE_APPEND_DATA
|FILE_READ_EA
|
4380 FILE_WRITE_EA
|FILE_READ_ATTRIBUTES
|FILE_WRITE_ATTRIBUTES
|
4381 DELETE_ACCESS
|READ_CONTROL_ACCESS
|WRITE_DAC_ACCESS
|SYNCHRONIZE_ACCESS
);
4382 ShareAccess
= FILE_SHARE_NONE
;
4383 CreateDisposition
= FILE_CREATE
;
4384 CreateOptions
= FILE_SEQUENTIAL_ONLY
|FILE_NON_DIRECTORY_FILE
;
4385 status
= cli_ntcreate(targetcli
, targetdest
, 0, DesiredAccess
,
4386 FILE_ATTRIBUTE_ARCHIVE
, ShareAccess
, CreateDisposition
,
4387 CreateOptions
, 0x0, &destfnum
, NULL
);
4388 if (!NT_STATUS_IS_OK(status
)) {
4389 d_printf("Failed to create file %s. %s\n",
4390 targetdest
, nt_errstr(status
));
4391 cli_close(targetcli
, srcfnum
);
4395 clock_gettime_mono(&st
.tp_start
);
4396 status
= cli_splice(targetcli
, targetcli
, srcfnum
, destfnum
,
4397 cr
.end_of_file
, 0, 0, &written
, scopy_status
, &st
);
4398 if (!NT_STATUS_IS_OK(status
)) {
4399 d_printf("%s copying file %s -> %s \n",
4406 status
= cli_close(targetcli
, srcfnum
);
4407 if (!NT_STATUS_IS_OK(status
)) {
4408 d_printf("Error %s closing remote source file\n", nt_errstr(status
));
4411 status
= cli_close(targetcli
, destfnum
);
4412 if (!NT_STATUS_IS_OK(status
)) {
4413 d_printf("Error %s closing remote dest file\n", nt_errstr(status
));
4420 /****************************************************************************
4421 Print the volume name.
4422 ****************************************************************************/
4424 static int cmd_volume(void)
4427 uint32_t serial_num
;
4431 status
= cli_get_fs_volume_info(cli
, talloc_tos(),
4432 &volname
, &serial_num
,
4434 if (!NT_STATUS_IS_OK(status
)) {
4435 d_printf("Error %s getting volume info\n", nt_errstr(status
));
4439 d_printf("Volume: |%s| serial number 0x%x\n",
4440 volname
, (unsigned int)serial_num
);
4444 /****************************************************************************
4445 Hard link files using the NT call.
4446 ****************************************************************************/
4448 static int cmd_hardlink(void)
4450 TALLOC_CTX
*ctx
= talloc_tos();
4453 struct cli_state
*targetcli
;
4457 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
4458 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
4459 d_printf("hardlink <src> <dest>\n");
4463 src
= talloc_asprintf(ctx
,
4465 client_get_cur_dir(),
4470 src
= client_clean_name(ctx
, src
);
4475 dest
= talloc_asprintf(ctx
,
4477 client_get_cur_dir(),
4482 dest
= client_clean_name(ctx
, dest
);
4487 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
4488 cli
, src
, &targetcli
, &targetname
);
4489 if (!NT_STATUS_IS_OK(status
)) {
4490 d_printf("hardlink %s: %s\n", src
, nt_errstr(status
));
4494 status
= cli_nt_hardlink(targetcli
, targetname
, dest
);
4495 if (!NT_STATUS_IS_OK(status
)) {
4496 d_printf("%s doing an NT hard link of files\n",
4504 /****************************************************************************
4505 Toggle the prompt flag.
4506 ****************************************************************************/
4508 static int cmd_prompt(void)
4511 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
4515 /****************************************************************************
4516 Set the newer than time.
4517 ****************************************************************************/
4519 static int cmd_newer(void)
4521 TALLOC_CTX
*ctx
= talloc_tos();
4524 SMB_STRUCT_STAT sbuf
;
4526 ok
= next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
);
4527 if (ok
&& (sys_stat(buf
, &sbuf
, false) == 0)) {
4528 newer_than
= convert_timespec_to_time_t(sbuf
.st_ex_mtime
);
4529 DEBUG(1,("Getting files newer than %s",
4530 time_to_asc(newer_than
)));
4535 if (ok
&& newer_than
== 0) {
4536 d_printf("Error setting newer-than time\n");
4543 /****************************************************************************
4544 Watch directory changes
4545 ****************************************************************************/
4547 static int cmd_notify(void)
4549 TALLOC_CTX
*frame
= talloc_stackframe();
4554 name
= talloc_strdup(talloc_tos(), client_get_cur_dir());
4558 if (!next_token_talloc(talloc_tos(), &cmd_ptr
, &buf
, NULL
)) {
4561 name
= talloc_asprintf_append(name
, "%s", buf
);
4565 name
= client_clean_name(talloc_tos(), name
);
4569 status
= cli_ntcreate(
4570 cli
, name
, 0, FILE_READ_DATA
, 0,
4571 FILE_SHARE_READ
|FILE_SHARE_WRITE
|FILE_SHARE_DELETE
,
4572 FILE_OPEN
, 0, 0, &fnum
, NULL
);
4573 if (!NT_STATUS_IS_OK(status
)) {
4574 d_printf("Could not open file: %s\n", nt_errstr(status
));
4580 uint32_t num_changes
= 0;
4581 struct notify_change
*changes
= NULL
;
4583 status
= cli_notify(cli
, fnum
, 1000, FILE_NOTIFY_CHANGE_ALL
,
4585 talloc_tos(), &num_changes
, &changes
);
4586 if (NT_STATUS_EQUAL(status
, STATUS_NOTIFY_ENUM_DIR
)) {
4587 printf("NOTIFY_ENUM_DIR\n");
4588 status
= NT_STATUS_OK
;
4590 if (!NT_STATUS_IS_OK(status
)) {
4591 d_printf("notify returned %s\n",
4595 for (i
=0; i
<num_changes
; i
++) {
4596 printf("%4.4x %s\n", changes
[i
].action
,
4599 TALLOC_FREE(changes
);
4602 d_printf("notify <dir name>\n");
4608 /****************************************************************************
4609 Set the archive level.
4610 ****************************************************************************/
4612 static int cmd_archive(void)
4614 TALLOC_CTX
*ctx
= talloc_tos();
4617 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4618 archive_level
= atoi(buf
);
4620 d_printf("Archive level is %d\n",archive_level
);
4626 /****************************************************************************
4627 Toggle the backup_intent state.
4628 ****************************************************************************/
4630 static int cmd_backup(void)
4632 backup_intent
= !backup_intent
;
4633 cli_set_backup_intent(cli
, backup_intent
);
4634 DEBUG(2,("backup intent is now %s\n",backup_intent
?"on":"off"));
4638 /****************************************************************************
4639 Toggle the lowercaseflag.
4640 ****************************************************************************/
4642 static int cmd_lowercase(void)
4644 lowercase
= !lowercase
;
4645 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
4649 /****************************************************************************
4650 Toggle the case sensitive flag.
4651 ****************************************************************************/
4653 static int cmd_setcase(void)
4655 bool orig_case_sensitive
= cli_set_case_sensitive(cli
, false);
4657 cli_set_case_sensitive(cli
, !orig_case_sensitive
);
4658 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive
?
4663 /****************************************************************************
4664 Toggle the showacls flag.
4665 ****************************************************************************/
4667 static int cmd_showacls(void)
4669 showacls
= !showacls
;
4670 DEBUG(2,("showacls is now %s\n",showacls
?"on":"off"));
4675 /****************************************************************************
4676 Toggle the recurse flag.
4677 ****************************************************************************/
4679 static int cmd_recurse(void)
4682 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
4686 /****************************************************************************
4687 Toggle the translate flag.
4688 ****************************************************************************/
4690 static int cmd_translate(void)
4692 translation
= !translation
;
4693 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4694 translation
?"on":"off"));
4698 /****************************************************************************
4700 ****************************************************************************/
4702 static int cmd_lcd(void)
4704 TALLOC_CTX
*ctx
= talloc_tos();
4708 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4709 if (chdir(buf
) == -1) {
4710 d_printf("chdir to %s failed (%s)\n",
4711 buf
, strerror(errno
));
4718 DEBUG(2,("the local directory is now %s\n",d
));
4723 /****************************************************************************
4724 Get a file restarting at end of local file.
4725 ****************************************************************************/
4727 static int cmd_reget(void)
4729 TALLOC_CTX
*ctx
= talloc_tos();
4730 char *local_name
= NULL
;
4731 char *remote_name
= NULL
;
4735 remote_name
= talloc_strdup(ctx
, client_get_cur_dir());
4740 if (!next_token_talloc(ctx
, &cmd_ptr
, &fname
, NULL
)) {
4741 d_printf("reget <filename>\n");
4744 remote_name
= talloc_asprintf_append(remote_name
, "%s", fname
);
4748 remote_name
= client_clean_name(ctx
,remote_name
);
4754 next_token_talloc(ctx
, &cmd_ptr
, &p
, NULL
);
4759 return do_get(remote_name
, local_name
, true);
4762 /****************************************************************************
4763 Put a file restarting at end of local file.
4764 ****************************************************************************/
4766 static int cmd_reput(void)
4768 TALLOC_CTX
*ctx
= talloc_tos();
4769 char *local_name
= NULL
;
4770 char *remote_name
= NULL
;
4774 remote_name
= talloc_strdup(ctx
, client_get_cur_dir());
4779 if (!next_token_talloc(ctx
, &cmd_ptr
, &local_name
, NULL
)) {
4780 d_printf("reput <filename>\n");
4784 if (!file_exist_stat(local_name
, &st
, false)) {
4785 d_printf("%s does not exist\n", local_name
);
4789 if (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
4790 remote_name
= talloc_asprintf_append(remote_name
,
4793 remote_name
= talloc_asprintf_append(remote_name
,
4800 remote_name
= client_clean_name(ctx
, remote_name
);
4805 return do_put(remote_name
, local_name
, true);
4808 /****************************************************************************
4810 ****************************************************************************/
4812 static void browse_fn(const char *name
, uint32_t m
,
4813 const char *comment
, void *state
)
4815 const char *typestr
= "";
4818 case STYPE_DISKTREE
:
4822 typestr
= "Printer";
4831 /* FIXME: If the remote machine returns non-ascii characters
4832 in any of these fields, they can corrupt the output. We
4833 should remove them. */
4835 d_printf("\t%-15s %-10.10s%s\n",
4836 name
,typestr
,comment
);
4838 d_printf ("%s|%s|%s\n",typestr
,name
,comment
);
4842 static bool browse_host_rpc(bool sort
)
4845 struct rpc_pipe_client
*pipe_hnd
= NULL
;
4846 TALLOC_CTX
*frame
= talloc_stackframe();
4848 struct srvsvc_NetShareInfoCtr info_ctr
;
4849 struct srvsvc_NetShareCtr1 ctr1
;
4850 uint32_t resume_handle
= 0;
4851 uint32_t total_entries
= 0;
4853 struct dcerpc_binding_handle
*b
;
4855 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
,
4858 if (!NT_STATUS_IS_OK(status
)) {
4859 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4860 nt_errstr(status
)));
4865 b
= pipe_hnd
->binding_handle
;
4867 ZERO_STRUCT(info_ctr
);
4871 info_ctr
.ctr
.ctr1
= &ctr1
;
4873 status
= dcerpc_srvsvc_NetShareEnumAll(b
, frame
,
4881 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(werr
)) {
4882 TALLOC_FREE(pipe_hnd
);
4887 for (i
=0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
4888 struct srvsvc_NetShareInfo1 info
= info_ctr
.ctr
.ctr1
->array
[i
];
4889 browse_fn(info
.name
, info
.type
, info
.comment
, NULL
);
4892 TALLOC_FREE(pipe_hnd
);
4897 /****************************************************************************
4898 Try and browse available connections on a host.
4899 ****************************************************************************/
4901 static bool browse_host(bool sort
)
4906 d_printf("\n\tSharename Type Comment\n");
4907 d_printf("\t--------- ---- -------\n");
4910 if (browse_host_rpc(sort
)) {
4914 if (lp_client_min_protocol() > PROTOCOL_NT1
) {
4918 ret
= cli_RNetShareEnum(cli
, browse_fn
, NULL
);
4920 NTSTATUS status
= cli_nt_error(cli
);
4921 d_printf("Error returning browse list: %s\n",
4928 /****************************************************************************
4930 ****************************************************************************/
4932 static void server_fn(const char *name
, uint32_t m
,
4933 const char *comment
, void *state
)
4937 d_printf("\t%-16s %s\n", name
, comment
);
4939 d_printf("%s|%s|%s\n",(char *)state
, name
, comment
);
4943 /****************************************************************************
4944 Try and browse available connections on a host.
4945 ****************************************************************************/
4947 static bool list_servers(const char *wk_grp
)
4951 if (!cli
->server_domain
)
4955 d_printf("\n\tServer Comment\n");
4956 d_printf("\t--------- -------\n");
4958 fstrcpy( state
, "Server" );
4959 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
,
4963 d_printf("\n\tWorkgroup Master\n");
4964 d_printf("\t--------- -------\n");
4967 fstrcpy( state
, "Workgroup" );
4968 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
,
4973 /****************************************************************************
4974 Print or set current VUID
4975 ****************************************************************************/
4977 static int cmd_vuid(void)
4979 TALLOC_CTX
*ctx
= talloc_tos();
4982 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4983 d_printf("Current VUID is %d\n",
4984 cli_state_get_uid(cli
));
4988 cli_state_set_uid(cli
, atoi(buf
));
4992 /****************************************************************************
4993 Setup a new VUID, by issuing a session setup
4994 ****************************************************************************/
4996 static int cmd_logon(void)
4998 TALLOC_CTX
*ctx
= talloc_tos();
4999 char *l_username
, *l_password
;
5000 struct cli_credentials
*creds
= NULL
;
5003 if (!next_token_talloc(ctx
, &cmd_ptr
,&l_username
,NULL
)) {
5004 d_printf("logon <username> [<password>]\n");
5008 if (!next_token_talloc(ctx
, &cmd_ptr
,&l_password
,NULL
)) {
5009 char pwd
[256] = {0};
5012 rc
= samba_getpass("Password: ", pwd
, sizeof(pwd
), false, false);
5014 l_password
= talloc_strdup(ctx
, pwd
);
5021 creds
= cli_session_creds_init(ctx
,
5026 false, /* use_kerberos */
5027 false, /* fallback_after_kerberos */
5028 false, /* use_ccache */
5029 false); /* password_is_nt_hash */
5030 if (creds
== NULL
) {
5031 d_printf("cli_session_creds_init() failed.\n");
5034 nt_status
= cli_session_setup_creds(cli
, creds
);
5036 if (!NT_STATUS_IS_OK(nt_status
)) {
5037 d_printf("session setup failed: %s\n", nt_errstr(nt_status
));
5041 d_printf("Current VUID is %d\n", cli_state_get_uid(cli
));
5049 static int cmd_logoff(void)
5053 status
= cli_ulogoff(cli
);
5054 if (!NT_STATUS_IS_OK(status
)) {
5055 d_printf("logoff failed: %s\n", nt_errstr(status
));
5059 d_printf("logoff successful\n");
5065 * tree connect (connect to a share)
5068 static int cmd_tcon(void)
5070 TALLOC_CTX
*ctx
= talloc_tos();
5074 if (!next_token_talloc(ctx
, &cmd_ptr
, &sharename
, NULL
)) {
5075 d_printf("tcon <sharename>\n");
5083 status
= cli_tree_connect(cli
, sharename
, "?????", NULL
);
5084 if (!NT_STATUS_IS_OK(status
)) {
5085 d_printf("tcon failed: %s\n", nt_errstr(status
));
5089 talloc_free(sharename
);
5091 d_printf("tcon to %s successful, tid: %u\n", sharename
,
5092 cli_state_get_tid(cli
));
5097 * tree disconnect (disconnect from a share)
5100 static int cmd_tdis(void)
5104 status
= cli_tdis(cli
);
5105 if (!NT_STATUS_IS_OK(status
)) {
5106 d_printf("tdis failed: %s\n", nt_errstr(status
));
5110 d_printf("tdis successful\n");
5119 static int cmd_tid(void)
5121 TALLOC_CTX
*ctx
= talloc_tos();
5124 if (!next_token_talloc(ctx
, &cmd_ptr
, &tid_str
, NULL
)) {
5125 if (cli_state_has_tcon(cli
)) {
5126 d_printf("current tid is %d\n", cli_state_get_tid(cli
));
5128 d_printf("no tcon currently\n");
5131 uint32_t tid
= atoi(tid_str
);
5132 if (!cli_state_has_tcon(cli
)) {
5133 d_printf("no tcon currently\n");
5135 cli_state_set_tid(cli
, tid
);
5142 /****************************************************************************
5143 list active connections
5144 ****************************************************************************/
5146 static int cmd_list_connect(void)
5148 cli_cm_display(cli
);
5152 /****************************************************************************
5153 display the current active client connection
5154 ****************************************************************************/
5156 static int cmd_show_connect( void )
5158 TALLOC_CTX
*ctx
= talloc_tos();
5159 struct cli_state
*targetcli
;
5163 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(), cli
,
5164 client_get_cur_dir(), &targetcli
,
5166 if (!NT_STATUS_IS_OK(status
)) {
5167 d_printf("showconnect %s: %s\n", cur_dir
, nt_errstr(status
));
5171 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli
->conn
), targetcli
->share
);
5176 * set_remote_times - set times of a remote file
5177 * @filename: path to the file name
5178 * @create_time: New create time
5179 * @access_time: New access time
5180 * @write_time: New write time
5181 * @change_time: New metadata change time
5183 * Update the file times with the ones provided.
5185 static int set_remote_times(const char *filename
, time_t create_time
,
5186 time_t access_time
, time_t write_time
,
5189 extern struct cli_state
*cli
;
5192 status
= cli_setpathinfo_basic(cli
, filename
, create_time
,
5193 access_time
, write_time
,
5195 if (!NT_STATUS_IS_OK(status
)) {
5196 d_printf("cli_setpathinfo_basic failed: %s\n",
5205 * cmd_utimes - interactive command to set the four times
5207 * Read a filename and four times from the client command line and update
5208 * the file times. A value of -1 for a time means don't change.
5210 static int cmd_utimes(void)
5212 const extern char *cmd_ptr
;
5215 time_t times
[4] = {0, 0, 0, 0};
5219 TALLOC_CTX
*ctx
= talloc_new(NULL
);
5224 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
5226 d_printf("utimes <filename> <create-time> <access-time> "
5227 "<write-time> <change-time>\n");
5228 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5229 "or -1 for no change\n");
5234 fname
= talloc_asprintf(ctx
,
5236 client_get_cur_dir(),
5238 if (fname
== NULL
) {
5242 fname
= client_clean_name(ctx
, fname
);
5243 if (fname
== NULL
) {
5248 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
) &&
5250 const char *s
= buf
;
5251 struct tm tm
= {0,};
5254 if (strlen(s
) == 2 && strcmp(s
, "-1") == 0) {
5255 times
[time_count
] = 0;
5259 ret
= strptime(s
, "%y:%m:%d-%H:%M:%S", &tm
);
5262 /* We could not match all the chars, so print error */
5263 if (ret
== NULL
|| *ret
!= 0) {
5264 d_printf("Invalid date format: %s\n", s
);
5265 d_printf("utimes <filename> <create-time> "
5266 "<access-time> <write-time> <change-time>\n");
5267 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5268 "or -1 for no change\n");
5273 /* Convert tm to a time_t */
5274 times
[time_count
] = mktime(&tm
);
5278 if (time_count
< 4) {
5279 d_printf("Insufficient dates: %d\n", time_count
);
5280 d_printf("utimes <filename> <create-time> <access-time> "
5281 "<write-time> <change-time>\n");
5282 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5283 "or -1 for no change\n");
5288 DEBUG(10, ("times\nCreate: %sAccess: %s Write: %sChange: %s\n",
5289 talloc_strdup(ctx
, ctime(×
[0])),
5290 talloc_strdup(ctx
, ctime(×
[1])),
5291 talloc_strdup(ctx
, ctime(×
[2])),
5292 talloc_strdup(ctx
, ctime(×
[3]))));
5294 set_remote_times(fname
, times
[0], times
[1], times
[2], times
[3]);
5301 * set_remote_attr - set DOS attributes of a remote file
5302 * @filename: path to the file name
5303 * @new_attr: attribute bit mask to use
5304 * @mode: one of ATTR_SET or ATTR_UNSET
5306 * Update the file attributes with the one provided.
5308 int set_remote_attr(const char *filename
, uint16_t new_attr
, int mode
)
5310 extern struct cli_state
*cli
;
5314 status
= cli_getatr(cli
, filename
, &old_attr
, NULL
, NULL
);
5315 if (!NT_STATUS_IS_OK(status
)) {
5316 d_printf("cli_getatr failed: %s\n", nt_errstr(status
));
5320 if (mode
== ATTR_SET
) {
5321 new_attr
|= old_attr
;
5323 new_attr
= old_attr
& ~new_attr
;
5326 status
= cli_setatr(cli
, filename
, new_attr
, 0);
5327 if (!NT_STATUS_IS_OK(status
)) {
5328 d_printf("cli_setatr failed: %s\n", nt_errstr(status
));
5336 * cmd_setmode - interactive command to set DOS attributes
5338 * Read a filename and mode from the client command line and update
5339 * the file DOS attributes.
5341 int cmd_setmode(void)
5343 const extern char *cmd_ptr
;
5346 uint16_t attr
[2] = {0};
5347 int mode
= ATTR_SET
;
5350 TALLOC_CTX
*ctx
= talloc_new(NULL
);
5355 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
5357 d_printf("setmode <filename> <[+|-]rsha>\n");
5362 fname
= talloc_asprintf(ctx
,
5364 client_get_cur_dir(),
5366 if (fname
== NULL
) {
5370 fname
= client_clean_name(ctx
, fname
);
5371 if (fname
== NULL
) {
5376 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
5377 const char *s
= buf
;
5388 attr
[mode
] |= FILE_ATTRIBUTE_READONLY
;
5391 attr
[mode
] |= FILE_ATTRIBUTE_HIDDEN
;
5394 attr
[mode
] |= FILE_ATTRIBUTE_SYSTEM
;
5397 attr
[mode
] |= FILE_ATTRIBUTE_ARCHIVE
;
5400 d_printf("setmode <filename> <perm=[+|-]rsha>\n");
5407 if (attr
[ATTR_SET
] == 0 && attr
[ATTR_UNSET
] == 0) {
5408 d_printf("setmode <filename> <[+|-]rsha>\n");
5413 DEBUG(2, ("perm set %d %d\n", attr
[ATTR_SET
], attr
[ATTR_UNSET
]));
5415 /* ignore return value: server might not store DOS attributes */
5416 set_remote_attr(fname
, attr
[ATTR_SET
], ATTR_SET
);
5417 set_remote_attr(fname
, attr
[ATTR_UNSET
], ATTR_UNSET
);
5423 /****************************************************************************
5425 ***************************************************************************/
5427 int cmd_iosize(void)
5429 TALLOC_CTX
*ctx
= talloc_tos();
5433 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
5434 if (smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_SMB2_02
) {
5436 d_printf("iosize <n> or iosize 0x<n>. "
5437 "Minimum is 0 (default), "
5438 "max is 16776960 (0xFFFF00)\n");
5440 d_printf("iosize <n> or iosize 0x<n>. "
5441 "(Encrypted connection) ,"
5442 "Minimum is 0 (default), "
5443 "max is 130048 (0x1FC00)\n");
5446 d_printf("iosize <n> or iosize 0x<n>.\n");
5451 iosize
= strtol(buf
,NULL
,0);
5452 if (smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_SMB2_02
) {
5453 if (smb_encrypt
&& (iosize
< 0 || iosize
> 0xFC00)) {
5454 d_printf("iosize out of range for encrypted "
5455 "connection (min = 0 (default), "
5456 "max = 130048 (0x1FC00)\n");
5458 } else if (!smb_encrypt
&& (iosize
< 0 || iosize
> 0xFFFF00)) {
5459 d_printf("iosize out of range (min = 0 (default), "
5460 "max = 16776960 (0xFFFF00)\n");
5465 io_bufsize
= iosize
;
5466 d_printf("iosize is now %d\n", io_bufsize
);
5470 /****************************************************************************
5472 ***************************************************************************/
5474 static int cmd_timeout(void)
5476 TALLOC_CTX
*ctx
= talloc_tos();
5479 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
5480 unsigned int old_timeout
= cli_set_timeout(cli
, 0);
5481 cli_set_timeout(cli
, old_timeout
);
5482 d_printf("timeout <n> (per-operation timeout "
5483 "in seconds - currently %u).\n",
5488 io_timeout
= strtol(buf
,NULL
,0);
5489 cli_set_timeout(cli
, io_timeout
*1000);
5490 d_printf("io_timeout per operation is now %d\n", io_timeout
);
5495 /****************************************************************************
5497 ****************************************************************************/
5498 static int cmd_history(void)
5500 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
5504 hlist
= history_list();
5506 for (i
= 0; hlist
&& hlist
[i
]; i
++) {
5507 DEBUG(0, ("%d: %s\n", i
, hlist
[i
]->line
));
5510 DEBUG(0,("no history without readline support\n"));
5516 /* Some constants for completing filename arguments */
5518 #define COMPL_NONE 0 /* No completions */
5519 #define COMPL_REMOTE 1 /* Complete remote filename */
5520 #define COMPL_LOCAL 2 /* Complete local filename */
5522 /* This defines the commands supported by this client.
5523 * NOTE: The "!" must be the last one in the list because it's fn pointer
5524 * field is NULL, and NULL in that field is used in process_tok()
5525 * (below) to indicate the end of the list. crh
5530 const char *description
;
5531 char compl_args
[2]; /* Completion argument info */
5533 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
5534 {"allinfo",cmd_allinfo
,"<file> show all available info",
5535 {COMPL_NONE
,COMPL_NONE
}},
5536 {"altname",cmd_altname
,"<file> show alt name",{COMPL_NONE
,COMPL_NONE
}},
5537 {"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
}},
5538 {"backup",cmd_backup
,"toggle backup intent state",{COMPL_NONE
,COMPL_NONE
}},
5539 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
5540 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
5541 {"case_sensitive",cmd_setcase
,"toggle the case sensitive flag to server",{COMPL_NONE
,COMPL_NONE
}},
5542 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
5543 {"chmod",cmd_chmod
,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE
,COMPL_NONE
}},
5544 {"chown",cmd_chown
,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE
,COMPL_NONE
}},
5545 {"close",cmd_close
,"<fid> close a file given a fid",{COMPL_REMOTE
,COMPL_NONE
}},
5546 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5547 {"deltree",cmd_deltree
,"<mask> recursively delete all matching files and directories",{COMPL_REMOTE
,COMPL_NONE
}},
5548 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
5549 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
5550 {"echo",cmd_echo
,"ping the server",{COMPL_NONE
,COMPL_NONE
}},
5551 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
5552 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
5553 {"getfacl",cmd_getfacl
,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE
,COMPL_NONE
}},
5554 {"geteas", cmd_geteas
, "<file name> get the EA list of a file",
5555 {COMPL_REMOTE
, COMPL_NONE
}},
5556 {"hardlink",cmd_hardlink
,"<src> <dest> create a Windows hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
5557 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
5558 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
5559 {"iosize",cmd_iosize
,"iosize <number> (default 64512)",{COMPL_NONE
,COMPL_NONE
}},
5560 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
5561 {"link",cmd_link
,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
5562 {"lock",cmd_lock
,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
5563 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
5564 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
5565 {"l",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
5566 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
5567 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
5568 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5569 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
5570 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
5571 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5572 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
5573 {"notify",cmd_notify
,"<file>Get notified of dir changes",{COMPL_REMOTE
,COMPL_NONE
}},
5574 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
5575 {"posix", cmd_posix
, "turn on all POSIX capabilities", {COMPL_REMOTE
,COMPL_NONE
}},
5576 {"posix_encrypt",cmd_posix_encrypt
,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE
,COMPL_NONE
}},
5577 {"posix_open",cmd_posix_open
,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5578 {"posix_mkdir",cmd_posix_mkdir
,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5579 {"posix_rmdir",cmd_posix_rmdir
,"<name> removes a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5580 {"posix_unlink",cmd_posix_unlink
,"<name> removes a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5581 {"posix_whoami",cmd_posix_whoami
,"return logged on user information "
5582 "using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
5583 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
5584 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
5585 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
5586 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
5587 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
5588 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
5589 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
5590 {"readlink",cmd_readlink
,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
5591 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
5592 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
5593 {"reget",cmd_reget
,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE
,COMPL_LOCAL
}},
5594 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
5595 {"reput",cmd_reput
,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL
,COMPL_REMOTE
}},
5596 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5597 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_REMOTE
,COMPL_NONE
}},
5598 {"showacls",cmd_showacls
,"toggle if ACLs are shown or not",{COMPL_NONE
,COMPL_NONE
}},
5599 {"setea", cmd_setea
, "<file name> <eaname> <eaval> Set an EA of a file",
5600 {COMPL_REMOTE
, COMPL_LOCAL
}},
5601 {"setmode",cmd_setmode
,"<file name> <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
5602 {"scopy",cmd_scopy
,"<src> <dest> server-side copy file",{COMPL_REMOTE
,COMPL_REMOTE
}},
5603 {"stat",cmd_stat
,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE
,COMPL_NONE
}},
5604 {"symlink",cmd_symlink
,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
5605 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
5606 {"tarmode",cmd_tarmode
,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
5607 {"timeout",cmd_timeout
,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE
,COMPL_NONE
}},
5608 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
5609 {"unlock",cmd_unlock
,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
5610 {"volume",cmd_volume
,"print the volume name",{COMPL_NONE
,COMPL_NONE
}},
5611 {"vuid",cmd_vuid
,"change current vuid",{COMPL_NONE
,COMPL_NONE
}},
5612 {"wdel",cmd_wdel
,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
5613 {"logon",cmd_logon
,"establish new logon",{COMPL_NONE
,COMPL_NONE
}},
5614 {"listconnect",cmd_list_connect
,"list open connections",{COMPL_NONE
,COMPL_NONE
}},
5615 {"showconnect",cmd_show_connect
,"display the current active connection",{COMPL_NONE
,COMPL_NONE
}},
5616 {"tcon",cmd_tcon
,"connect to a share" ,{COMPL_NONE
,COMPL_NONE
}},
5617 {"tdis",cmd_tdis
,"disconnect from a share",{COMPL_NONE
,COMPL_NONE
}},
5618 {"tid",cmd_tid
,"show or set the current tid (tree-id)",{COMPL_NONE
,COMPL_NONE
}},
5619 {"utimes", cmd_utimes
,"<file name> <create_time> <access_time> <mod_time> "
5620 "<ctime> set times", {COMPL_REMOTE
,COMPL_NONE
}},
5621 {"logoff",cmd_logoff
,"log off (close the session)",{COMPL_NONE
,COMPL_NONE
}},
5622 {"..",cmd_cd_oneup
,"change the remote directory (up one level)",{COMPL_REMOTE
,COMPL_NONE
}},
5624 /* Yes, this must be here, see crh's comment above. */
5625 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
5626 {NULL
,NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
5629 /*******************************************************************
5630 Lookup a command string in the list of commands, including
5632 ******************************************************************/
5634 static int process_tok(char *tok
)
5636 size_t i
= 0, matches
= 0;
5638 size_t tok_len
= strlen(tok
);
5640 while (commands
[i
].fn
!= NULL
) {
5641 if (strequal(commands
[i
].name
,tok
)) {
5645 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
5654 else if (matches
== 1)
5660 /****************************************************************************
5662 ****************************************************************************/
5664 static int cmd_help(void)
5666 TALLOC_CTX
*ctx
= talloc_tos();
5670 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
5671 if ((i
= process_tok(buf
)) >= 0)
5672 d_printf("HELP %s:\n\t%s\n\n",
5673 commands
[i
].name
,commands
[i
].description
);
5675 while (commands
[i
].description
) {
5676 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
5677 d_printf("%-15s",commands
[i
].name
);
5686 /****************************************************************************
5687 Process a -c command string.
5688 ****************************************************************************/
5690 static int process_command_string(const char *cmd_in
)
5692 TALLOC_CTX
*ctx
= talloc_tos();
5693 char *cmd
= talloc_strdup(ctx
, cmd_in
);
5699 /* establish the connection if not already */
5704 status
= cli_cm_open(talloc_tos(), NULL
,
5705 have_ip
? dest_ss_str
: desthost
,
5706 service
, popt_get_cmdline_auth_info(),
5708 max_protocol
, port
, name_type
,
5710 if (!NT_STATUS_IS_OK(status
)) {
5713 cli_set_timeout(cli
, io_timeout
*1000);
5716 while (cmd
[0] != '\0') {
5722 if ((p
= strchr_m(cmd
, ';')) == 0) {
5731 /* and get the first part of the command */
5733 if (!next_token_talloc(ctx
, &cmd_ptr
,&tok
,NULL
)) {
5737 if ((i
= process_tok(tok
)) >= 0) {
5738 rc
= commands
[i
].fn();
5739 } else if (i
== -2) {
5740 d_printf("%s: command abbreviation ambiguous\n",tok
);
5742 d_printf("%s: command not found\n",tok
);
5749 #define MAX_COMPLETIONS 100
5751 struct completion_remote
{
5759 static NTSTATUS
completion_remote_filter(const char *mnt
,
5760 struct file_info
*f
,
5764 struct completion_remote
*info
= (struct completion_remote
*)state
;
5766 if (info
->count
>= MAX_COMPLETIONS
- 1) {
5767 return NT_STATUS_OK
;
5769 if (strncmp(info
->text
, f
->name
, info
->len
) != 0) {
5770 return NT_STATUS_OK
;
5772 if (ISDOT(f
->name
) || ISDOTDOT(f
->name
)) {
5773 return NT_STATUS_OK
;
5776 if ((info
->dirmask
[0] == 0) && !(f
->mode
& FILE_ATTRIBUTE_DIRECTORY
))
5777 info
->matches
[info
->count
] = SMB_STRDUP(f
->name
);
5779 TALLOC_CTX
*ctx
= talloc_stackframe();
5782 tmp
= talloc_strdup(ctx
,info
->dirmask
);
5785 return NT_STATUS_NO_MEMORY
;
5787 tmp
= talloc_asprintf_append(tmp
, "%s", f
->name
);
5790 return NT_STATUS_NO_MEMORY
;
5792 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
5793 tmp
= talloc_asprintf_append(tmp
, "%s",
5798 return NT_STATUS_NO_MEMORY
;
5800 info
->matches
[info
->count
] = SMB_STRDUP(tmp
);
5803 if (info
->matches
[info
->count
] == NULL
) {
5804 return NT_STATUS_OK
;
5806 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
5807 smb_readline_ca_char(0);
5809 if (info
->count
== 1) {
5810 info
->samelen
= strlen(info
->matches
[info
->count
]);
5812 while (strncmp(info
->matches
[info
->count
],
5813 info
->matches
[info
->count
-1],
5814 info
->samelen
) != 0) {
5819 return NT_STATUS_OK
;
5822 static char **remote_completion(const char *text
, int len
)
5824 TALLOC_CTX
*ctx
= talloc_stackframe();
5825 char *dirmask
= NULL
;
5826 char *targetpath
= NULL
;
5827 struct cli_state
*targetcli
= NULL
;
5829 struct completion_remote info
= { NULL
, NULL
, 1, 0, NULL
, 0 };
5832 /* can't have non-static initialisation on Sun CC, so do it
5838 info
.matches
= SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS
);
5839 if (!info
.matches
) {
5845 * We're leaving matches[0] free to fill it later with the text to
5846 * display: Either the one single match or the longest common subset
5849 info
.matches
[0] = NULL
;
5852 for (i
= len
-1; i
>= 0; i
--) {
5853 if ((text
[i
] == '/') || (text
[i
] == CLI_DIRSEP_CHAR
)) {
5858 info
.text
= text
+i
+1;
5859 info
.samelen
= info
.len
= len
-i
-1;
5862 info
.dirmask
= SMB_MALLOC_ARRAY(char, i
+2);
5863 if (!info
.dirmask
) {
5866 strncpy(info
.dirmask
, text
, i
+1);
5867 info
.dirmask
[i
+1] = 0;
5868 dirmask
= talloc_asprintf(ctx
,
5870 client_get_cur_dir(),
5874 info
.dirmask
= SMB_STRDUP("");
5875 if (!info
.dirmask
) {
5878 dirmask
= talloc_asprintf(ctx
,
5880 client_get_cur_dir());
5885 dirmask
= client_clean_name(ctx
, dirmask
);
5886 if (dirmask
== NULL
) {
5890 status
= cli_resolve_path(ctx
, "", popt_get_cmdline_auth_info(),
5891 cli
, dirmask
, &targetcli
, &targetpath
);
5892 if (!NT_STATUS_IS_OK(status
)) {
5895 status
= cli_list(targetcli
, targetpath
, FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
5896 completion_remote_filter
, (void *)&info
);
5897 if (!NT_STATUS_IS_OK(status
)) {
5901 if (info
.count
== 1) {
5903 * No matches at all, NULL indicates there is nothing
5905 SAFE_FREE(info
.matches
[0]);
5906 SAFE_FREE(info
.matches
);
5911 if (info
.count
== 2) {
5913 * Exactly one match in matches[1], indicate this is the one
5916 info
.matches
[0] = info
.matches
[1];
5917 info
.matches
[1] = NULL
;
5920 return info
.matches
;
5924 * We got more than one possible match, set the result to the maximum
5928 info
.matches
[0] = SMB_STRNDUP(info
.matches
[1], info
.samelen
);
5929 info
.matches
[info
.count
] = NULL
;
5931 return info
.matches
;
5934 for (i
= 0; i
< info
.count
; i
++) {
5935 SAFE_FREE(info
.matches
[i
]);
5937 SAFE_FREE(info
.matches
);
5938 SAFE_FREE(info
.dirmask
);
5943 static char **completion_fn(const char *text
, int start
, int end
)
5945 smb_readline_ca_char(' ');
5948 const char *buf
, *sp
;
5952 buf
= smb_readline_get_line_buffer();
5956 sp
= strchr(buf
, ' ');
5960 for (i
= 0; commands
[i
].name
; i
++) {
5961 if ((strncmp(commands
[i
].name
, buf
, sp
- buf
) == 0) &&
5962 (commands
[i
].name
[sp
- buf
] == 0)) {
5966 if (commands
[i
].name
== NULL
)
5972 if (sp
== (buf
+ start
))
5973 compl_type
= commands
[i
].compl_args
[0];
5975 compl_type
= commands
[i
].compl_args
[1];
5977 if (compl_type
== COMPL_REMOTE
)
5978 return remote_completion(text
, end
- start
);
5979 else /* fall back to local filename completion */
5983 size_t i
, len
, samelen
= 0, count
=1;
5985 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
5992 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
5993 if (strncmp(text
, commands
[i
].name
, len
) == 0) {
5994 matches
[count
] = SMB_STRDUP(commands
[i
].name
);
5995 if (!matches
[count
])
5998 samelen
= strlen(matches
[count
]);
6000 while (strncmp(matches
[count
], matches
[count
-1], samelen
) != 0)
6007 case 0: /* should never happen */
6011 matches
[0] = SMB_STRDUP(matches
[1]);
6014 matches
[0] = (char *)SMB_MALLOC(samelen
+1);
6017 strncpy(matches
[0], matches
[1], samelen
);
6018 matches
[0][samelen
] = 0;
6020 matches
[count
] = NULL
;
6024 for (i
= 0; i
< count
; i
++)
6032 static bool finished
;
6034 /****************************************************************************
6035 Make sure we swallow keepalives during idle time.
6036 ****************************************************************************/
6038 static void readline_callback(void)
6040 static time_t last_t
;
6041 struct timespec now
;
6044 unsigned char garbage
[16];
6046 clock_gettime_mono(&now
);
6054 /* Ping the server to keep the connection alive using SMBecho. */
6055 memset(garbage
, 0xf0, sizeof(garbage
));
6056 status
= cli_echo(cli
, 1, data_blob_const(garbage
, sizeof(garbage
)));
6057 if (NT_STATUS_IS_OK(status
) ||
6058 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
6060 * Even if server returns NT_STATUS_INVALID_PARAMETER
6061 * it still responded.
6062 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
6067 if (!cli_state_is_connected(cli
)) {
6068 DEBUG(0,("SMBecho failed (%s). The connection is "
6069 "disconnected now\n", nt_errstr(status
)));
6071 smb_readline_done();
6075 /****************************************************************************
6076 Process commands on stdin.
6077 ****************************************************************************/
6079 static int process_stdin(void)
6084 d_printf("Try \"help\" to get a list of possible commands.\n");
6088 TALLOC_CTX
*frame
= talloc_stackframe();
6090 char *the_prompt
= NULL
;
6094 /* display a prompt */
6095 if (asprintf(&the_prompt
, "smb: %s> ", client_get_cur_dir()) < 0) {
6099 line
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
6100 SAFE_FREE(the_prompt
);
6106 /* special case - first char is ! */
6108 if (system(line
+ 1) == -1) {
6109 d_printf("system() command %s failed.\n",
6117 /* and get the first part of the command */
6119 if (!next_token_talloc(frame
, &cmd_ptr
,&tok
,NULL
)) {
6125 if ((i
= process_tok(tok
)) >= 0) {
6126 rc
= commands
[i
].fn();
6127 } else if (i
== -2) {
6128 d_printf("%s: command abbreviation ambiguous\n",tok
);
6130 d_printf("%s: command not found\n",tok
);
6138 /****************************************************************************
6139 Process commands from the client.
6140 ****************************************************************************/
6142 static int process(const char *base_directory
)
6147 status
= cli_cm_open(talloc_tos(), NULL
,
6148 have_ip
? dest_ss_str
: desthost
,
6149 service
, popt_get_cmdline_auth_info(),
6150 smb_encrypt
, max_protocol
, port
,
6152 if (!NT_STATUS_IS_OK(status
)) {
6156 cli_set_timeout(cli
, io_timeout
*1000);
6158 if (base_directory
&& *base_directory
) {
6159 rc
= do_cd(base_directory
);
6167 rc
= process_command_string(cmdstr
);
6176 /****************************************************************************
6178 ****************************************************************************/
6180 static int do_host_query(const char *query_host
)
6184 status
= cli_cm_open(talloc_tos(), NULL
,
6185 have_ip
? dest_ss_str
: query_host
,
6186 "IPC$", popt_get_cmdline_auth_info(),
6187 smb_encrypt
, max_protocol
, port
,
6189 if (!NT_STATUS_IS_OK(status
)) {
6193 cli_set_timeout(cli
, io_timeout
*1000);
6196 /* Ensure that the host can do IPv4 */
6198 if (!interpret_addr(query_host
)) {
6199 struct sockaddr_storage ss
;
6200 if (interpret_string_addr(&ss
, query_host
, 0) &&
6201 (ss
.ss_family
!= AF_INET
)) {
6202 d_printf("%s is an IPv6 address -- no workgroup available\n",
6208 if (lp_client_min_protocol() > PROTOCOL_NT1
) {
6209 d_printf("SMB1 disabled -- no workgroup available\n");
6213 if (lp_disable_netbios()) {
6214 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
6218 if (port
!= NBT_SMB_PORT
||
6219 smbXcli_conn_protocol(cli
->conn
) > PROTOCOL_NT1
)
6221 int max_proto
= MIN(max_protocol
, PROTOCOL_NT1
);
6224 * Workgroups simply don't make sense over anything
6225 * else but port 139 and SMB1.
6229 d_printf("Reconnecting with SMB1 for workgroup listing.\n");
6230 status
= cli_cm_open(talloc_tos(), NULL
,
6231 have_ip
? dest_ss_str
: query_host
,
6232 "IPC$", popt_get_cmdline_auth_info(),
6233 smb_encrypt
, max_proto
,
6234 NBT_SMB_PORT
, name_type
, &cli
);
6235 if (!NT_STATUS_IS_OK(status
)) {
6236 d_printf("Unable to connect with SMB1 "
6237 "-- no workgroup available\n");
6242 cli_set_timeout(cli
, io_timeout
*1000);
6243 list_servers(lp_workgroup());
6250 /****************************************************************************
6251 Handle a tar operation.
6252 ****************************************************************************/
6254 static int do_tar_op(const char *base_directory
)
6256 struct tar
*tar_ctx
= tar_get_ctx();
6259 /* do we already have a connection? */
6263 status
= cli_cm_open(talloc_tos(), NULL
,
6264 have_ip
? dest_ss_str
: desthost
,
6265 service
, popt_get_cmdline_auth_info(),
6266 smb_encrypt
, max_protocol
,
6267 port
, name_type
, &cli
);
6268 if (!NT_STATUS_IS_OK(status
)) {
6272 cli_set_timeout(cli
, io_timeout
*1000);
6277 if (base_directory
&& *base_directory
) {
6278 ret
= do_cd(base_directory
);
6284 ret
= tar_process(tar_ctx
);
6292 /****************************************************************************
6293 Handle a message operation.
6294 ****************************************************************************/
6296 static int do_message_op(struct user_auth_info
*a_info
)
6300 if (lp_disable_netbios()) {
6301 d_printf("NetBIOS over TCP disabled.\n");
6305 status
= cli_connect_nb(desthost
, have_ip
? &dest_ss
: NULL
,
6306 port
? port
: NBT_SMB_PORT
, name_type
,
6307 lp_netbios_name(), SMB_SIGNING_DEFAULT
, 0, &cli
);
6308 if (!NT_STATUS_IS_OK(status
)) {
6309 d_printf("Connection to %s failed. Error %s\n", desthost
, nt_errstr(status
));
6313 cli_set_timeout(cli
, io_timeout
*1000);
6314 send_message(get_cmdline_auth_info_username(a_info
));
6320 /****************************************************************************
6322 ****************************************************************************/
6324 int main(int argc
,char *argv
[])
6326 const char **const_argv
= discard_const_p(const char *, argv
);
6327 char *base_directory
= NULL
;
6329 char *query_host
= NULL
;
6330 bool message
= false;
6331 static const char *new_name_resolve_order
= NULL
;
6335 bool tar_opt
= false;
6336 bool service_opt
= false;
6337 struct tar
*tar_ctx
= tar_get_ctx();
6339 struct poptOption long_options
[] = {
6342 { "name-resolve", 'R', POPT_ARG_STRING
, &new_name_resolve_order
, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
6343 { "message", 'M', POPT_ARG_STRING
, NULL
, 'M', "Send message", "HOST" },
6344 { "ip-address", 'I', POPT_ARG_STRING
, NULL
, 'I', "Use this IP to connect to", "IP" },
6345 { "stderr", 'E', POPT_ARG_NONE
, NULL
, 'E', "Write messages to stderr instead of stdout" },
6346 { "list", 'L', POPT_ARG_STRING
, NULL
, 'L', "Get a list of shares available on a host", "HOST" },
6347 { "max-protocol", 'm', POPT_ARG_STRING
, NULL
, 'm', "Set the max protocol level", "LEVEL" },
6348 { "tar", 'T', POPT_ARG_STRING
, NULL
, 'T', "Command line tar", "<c|x>IXFqgbNan" },
6349 { "directory", 'D', POPT_ARG_STRING
, NULL
, 'D', "Start from directory", "DIR" },
6350 { "command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated commands" },
6351 { "send-buffer", 'b', POPT_ARG_INT
, &io_bufsize
, 'b', "Changes the transmit/send buffer", "BYTES" },
6352 { "timeout", 't', POPT_ARG_INT
, &io_timeout
, 'b', "Changes the per-operation timeout", "SECONDS" },
6353 { "port", 'p', POPT_ARG_INT
, &port
, 'p', "Port to connect to", "PORT" },
6354 { "grepable", 'g', POPT_ARG_NONE
, NULL
, 'g', "Produce grepable output" },
6355 { "quiet", 'q', POPT_ARG_NONE
, NULL
, 'q', "Suppress help message" },
6356 { "browse", 'B', POPT_ARG_NONE
, NULL
, 'B', "Browse SMB servers using DNS" },
6358 POPT_COMMON_CONNECTION
6359 POPT_COMMON_CREDENTIALS
6362 TALLOC_CTX
*frame
= talloc_stackframe();
6364 if (!client_set_cur_dir("\\")) {
6368 /* set default debug level to 1 regardless of what smb.conf sets */
6369 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR
);
6372 lp_set_cmdline("log level", "1");
6374 popt_common_credentials_set_ignore_missing_conf();
6375 popt_common_credentials_set_delay_post();
6378 pc
= poptGetContext("smbclient", argc
, const_argv
, long_options
, 0);
6379 poptSetOtherOptionHelp(pc
, "service <password>");
6381 while ((opt
= poptGetNextOpt(pc
)) != -1) {
6383 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
6384 /* I see no other way to keep things sane --SSS */
6385 if (tar_opt
== true) {
6386 while (poptPeekArg(pc
)) {
6392 /* if the service has not yet been specified lets see if it is available in the popt stack */
6393 if (!service_opt
&& poptPeekArg(pc
)) {
6394 service
= talloc_strdup(frame
, poptGetArg(pc
));
6401 /* if the service has already been retrieved then check if we have also a password */
6403 && (!get_cmdline_auth_info_got_pass(
6404 popt_get_cmdline_auth_info()))
6405 && poptPeekArg(pc
)) {
6406 set_cmdline_auth_info_password(
6407 popt_get_cmdline_auth_info(), poptGetArg(pc
));
6413 /* Messages are sent to NetBIOS name type 0x3
6414 * (Messenger Service). Make sure we default
6415 * to port 139 instead of port 445. srl,crh
6418 desthost
= talloc_strdup(frame
,poptGetOptArg(pc
));
6423 port
= NBT_SMB_PORT
;
6428 if (!interpret_string_addr(&dest_ss
, poptGetOptArg(pc
), 0)) {
6432 print_sockaddr(dest_ss_str
, sizeof(dest_ss_str
), &dest_ss
);
6436 setup_logging("smbclient", DEBUG_STDERR
);
6437 display_set_stderr();
6441 query_host
= talloc_strdup(frame
, poptGetOptArg(pc
));
6447 lp_set_cmdline("client max protocol", poptGetOptArg(pc
));
6450 /* We must use old option processing for this. Find the
6451 * position of the -T option in the raw argv[]. */
6455 for (i
= 1; i
< argc
; i
++) {
6456 if (strncmp("-T", argv
[i
],2)==0)
6460 if (tar_parse_args(tar_ctx
, poptGetOptArg(pc
),
6461 const_argv
+ i
, argc
- i
)) {
6462 poptPrintUsage(pc
, stderr
, 0);
6466 /* this must be the last option, mark we have parsed it so that we know we have */
6470 base_directory
= talloc_strdup(frame
, poptGetOptArg(pc
));
6471 if (!base_directory
) {
6485 return(do_smb_browse());
6490 /* We may still have some leftovers after the last popt option has been called */
6491 if (tar_opt
== true) {
6492 while (poptPeekArg(pc
)) {
6498 /* if the service has not yet been specified lets see if it is available in the popt stack */
6499 if (!service_opt
&& poptPeekArg(pc
)) {
6500 service
= talloc_strdup(frame
,poptGetArg(pc
));
6507 /* if the service has already been retrieved then check if we have also a password */
6509 && !get_cmdline_auth_info_got_pass(popt_get_cmdline_auth_info())
6510 && poptPeekArg(pc
)) {
6511 set_cmdline_auth_info_password(popt_get_cmdline_auth_info(),
6515 if (service_opt
&& service
) {
6518 /* Convert any '/' characters in the service name to '\' characters */
6519 string_replace(service
, '/','\\');
6520 if (count_chars(service
,'\\') < 3) {
6521 d_printf("\n%s: Not enough '\\' characters in service\n",service
);
6522 poptPrintUsage(pc
, stderr
, 0);
6525 /* Remove trailing slashes */
6526 len
= strlen(service
);
6527 while(len
> 0 && service
[len
- 1] == '\\') {
6529 service
[len
] = '\0';
6533 if (!init_names()) {
6534 fprintf(stderr
, "init_names() failed\n");
6538 if(new_name_resolve_order
)
6539 lp_set_cmdline("name resolve order", new_name_resolve_order
);
6541 if (!tar_to_process(tar_ctx
) && !query_host
&& !service
&& !message
) {
6542 poptPrintUsage(pc
, stderr
, 0);
6546 poptFreeContext(pc
);
6547 popt_burn_cmdline_password(argc
, argv
);
6549 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
6551 /* Ensure we have a password (or equivalent). */
6552 popt_common_credentials_post();
6553 smb_encrypt
= get_cmdline_auth_info_smb_encrypt(
6554 popt_get_cmdline_auth_info());
6556 max_protocol
= lp_client_max_protocol();
6558 if (tar_to_process(tar_ctx
)) {
6560 process_command_string(cmdstr
);
6561 rc
= do_tar_op(base_directory
);
6562 } else if (query_host
&& *query_host
) {
6563 char *qhost
= query_host
;
6566 while (*qhost
== '\\' || *qhost
== '/')
6569 if ((slash
= strchr_m(qhost
, '/'))
6570 || (slash
= strchr_m(qhost
, '\\'))) {
6574 if ((p
=strchr_m(qhost
, '#'))) {
6577 sscanf(p
, "%x", &name_type
);
6580 rc
= do_host_query(qhost
);
6581 } else if (message
) {
6582 rc
= do_message_op(popt_get_cmdline_auth_info());
6583 } else if (process(base_directory
)) {
6587 popt_free_cmdline_auth_info();