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.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "client/client_proto.h"
29 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
30 #include "../lib/util/select.h"
31 #include "system/readline.h"
32 #include "../libcli/smbreadline/smbreadline.h"
33 #include "../libcli/security/security.h"
34 #include "system/select.h"
35 #include "libsmb/libsmb.h"
36 #include "libsmb/clirap.h"
38 #include "libsmb/nmblib.h"
44 extern int do_smb_browse(void); /* mDNS browsing */
46 extern bool override_logfile
;
51 static char *desthost
;
52 static char *calling_name
;
53 static bool grepable
= false;
54 static char *cmdstr
= NULL
;
55 const char *cmd_ptr
= NULL
;
57 static int io_bufsize
= 524288;
59 static int name_type
= 0x20;
60 static int max_protocol
= PROTOCOL_NT1
;
62 static int process_tok(char *tok
);
63 static int cmd_help(void);
65 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
67 /* 30 second timeout on most commands */
68 #define CLIENT_TIMEOUT (30*1000)
69 #define SHORT_TIMEOUT (5*1000)
71 /* value for unused fid field in trans2 secondary request */
72 #define FID_UNUSED (0xFFFF)
74 time_t newer_than
= 0;
75 static int archive_level
= 0;
77 static bool translation
= false;
80 /* clitar bits insert */
83 extern bool tar_reset
;
86 static bool prompt
= true;
88 static bool recurse
= false;
89 static bool showacls
= false;
90 bool lowercase
= false;
92 static struct sockaddr_storage dest_ss
;
93 static char dest_ss_str
[INET6_ADDRSTRLEN
];
95 #define SEPARATORS " \t\n\r"
97 static bool abort_mget
= true;
100 uint64_t get_total_size
= 0;
101 unsigned int get_total_time_ms
= 0;
102 static uint64_t put_total_size
= 0;
103 static unsigned int put_total_time_ms
= 0;
106 static double dir_total
;
108 /* encrypted state. */
109 static bool smb_encrypt
;
111 /* root cli_state connection */
113 struct cli_state
*cli
;
115 static char CLI_DIRSEP_CHAR
= '\\';
116 static char CLI_DIRSEP_STR
[] = { '\\', '\0' };
118 /* Authentication for client connections. */
119 struct user_auth_info
*auth_info
;
121 /* Accessor functions for directory paths. */
122 static char *fileselection
;
123 static const char *client_get_fileselection(void)
126 return fileselection
;
131 static const char *client_set_fileselection(const char *new_fs
)
133 SAFE_FREE(fileselection
);
135 fileselection
= SMB_STRDUP(new_fs
);
137 return client_get_fileselection();
141 static const char *client_get_cwd(void)
146 return CLI_DIRSEP_STR
;
149 static const char *client_set_cwd(const char *new_cwd
)
153 cwd
= SMB_STRDUP(new_cwd
);
155 return client_get_cwd();
158 static char *cur_dir
;
159 const char *client_get_cur_dir(void)
164 return CLI_DIRSEP_STR
;
167 const char *client_set_cur_dir(const char *newdir
)
171 cur_dir
= SMB_STRDUP(newdir
);
173 return client_get_cur_dir();
176 /****************************************************************************
177 Put up a yes/no prompt.
178 ****************************************************************************/
180 static bool yesno(const char *p
)
185 if (!fgets(ans
,sizeof(ans
)-1,stdin
))
188 if (*ans
== 'y' || *ans
== 'Y')
194 /****************************************************************************
195 Write to a local file with CR/LF->LF translation if appropriate. Return the
196 number taken from the buffer. This may not equal the number written.
197 ****************************************************************************/
199 static int writefile(int f
, char *b
, int n
)
209 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
212 if (write(f
, b
, 1) != 1) {
222 /****************************************************************************
223 Read from a file with LF->CR/LF translation if appropriate. Return the
224 number read. read approx n bytes.
225 ****************************************************************************/
227 static int readfile(uint8_t *b
, int n
, XFILE
*f
)
233 return x_fread(b
,1,n
,f
);
236 while (i
< (n
- 1) && (i
< BUFFER_SIZE
)) {
237 if ((c
= x_getc(f
)) == EOF
) {
241 if (c
== '\n') { /* change all LFs to CR/LF */
256 static size_t push_source(uint8_t *buf
, size_t n
, void *priv
)
258 struct push_state
*state
= (struct push_state
*)priv
;
261 if (x_feof(state
->f
)) {
265 result
= readfile(buf
, n
, state
->f
);
266 state
->nread
+= result
;
270 /****************************************************************************
272 ****************************************************************************/
274 static void send_message(const char *username
)
280 d_printf("Type your message, ending it with a Control-D\n");
283 while (i
<sizeof(buf
)-2) {
284 int c
= fgetc(stdin
);
295 status
= cli_message(cli
, desthost
, username
, buf
);
296 if (!NT_STATUS_IS_OK(status
)) {
297 d_fprintf(stderr
, "cli_message returned %s\n",
302 /****************************************************************************
303 Check the space on a device.
304 ****************************************************************************/
306 static int do_dskattr(void)
308 int total
, bsize
, avail
;
309 struct cli_state
*targetcli
= NULL
;
310 char *targetpath
= NULL
;
311 TALLOC_CTX
*ctx
= talloc_tos();
314 if ( !cli_resolve_path(ctx
, "", auth_info
, cli
, client_get_cur_dir(), &targetcli
, &targetpath
)) {
315 d_printf("Error in dskattr: %s\n", cli_errstr(cli
));
319 status
= cli_dskattr(targetcli
, &bsize
, &total
, &avail
);
320 if (!NT_STATUS_IS_OK(status
)) {
321 d_printf("Error in dskattr: %s\n", nt_errstr(status
));
325 d_printf("\n\t\t%d blocks of size %d. %d 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 Change directory - inner section.
355 ****************************************************************************/
357 static int do_cd(const char *new_dir
)
360 char *saved_dir
= NULL
;
362 char *targetpath
= NULL
;
363 struct cli_state
*targetcli
= NULL
;
364 SMB_STRUCT_STAT sbuf
;
367 TALLOC_CTX
*ctx
= talloc_stackframe();
369 newdir
= talloc_strdup(ctx
, new_dir
);
375 normalize_name(newdir
);
377 /* Save the current directory in case the new directory is invalid */
379 saved_dir
= talloc_strdup(ctx
, client_get_cur_dir());
385 if (*newdir
== CLI_DIRSEP_CHAR
) {
386 client_set_cur_dir(newdir
);
389 new_cd
= talloc_asprintf(ctx
, "%s%s",
390 client_get_cur_dir(),
397 /* Ensure cur_dir ends in a DIRSEP */
398 if ((new_cd
[0] != '\0') && (*(new_cd
+strlen(new_cd
)-1) != CLI_DIRSEP_CHAR
)) {
399 new_cd
= talloc_asprintf_append(new_cd
, "%s", CLI_DIRSEP_STR
);
404 client_set_cur_dir(new_cd
);
406 new_cd
= clean_name(ctx
, new_cd
);
407 client_set_cur_dir(new_cd
);
409 if ( !cli_resolve_path(ctx
, "", auth_info
, cli
, new_cd
, &targetcli
, &targetpath
)) {
410 d_printf("cd %s: %s\n", new_cd
, cli_errstr(cli
));
411 client_set_cur_dir(saved_dir
);
415 if (strequal(targetpath
,CLI_DIRSEP_STR
)) {
420 /* Use a trans2_qpathinfo to test directories for modern servers.
421 Except Win9x doesn't support the qpathinfo_basic() call..... */
423 if (targetcli
->protocol
> PROTOCOL_LANMAN2
&& !targetcli
->win95
) {
426 status
= cli_qpathinfo_basic(targetcli
, targetpath
, &sbuf
,
428 if (!NT_STATUS_IS_OK(status
)) {
429 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
430 client_set_cur_dir(saved_dir
);
434 if (!(attributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
435 d_printf("cd %s: not a directory\n", new_cd
);
436 client_set_cur_dir(saved_dir
);
442 targetpath
= talloc_asprintf(ctx
,
447 client_set_cur_dir(saved_dir
);
450 targetpath
= clean_name(ctx
, targetpath
);
452 client_set_cur_dir(saved_dir
);
456 status
= cli_chkpath(targetcli
, targetpath
);
457 if (!NT_STATUS_IS_OK(status
)) {
458 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
459 client_set_cur_dir(saved_dir
);
472 /****************************************************************************
474 ****************************************************************************/
476 static int cmd_cd(void)
481 if (next_token_talloc(talloc_tos(), &cmd_ptr
, &buf
,NULL
)) {
484 d_printf("Current directory is %s\n",client_get_cur_dir());
490 /****************************************************************************
492 ****************************************************************************/
494 static int cmd_cd_oneup(void)
499 /*******************************************************************
500 Decide if a file should be operated on.
501 ********************************************************************/
503 static bool do_this_one(struct file_info
*finfo
)
509 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
513 if (*client_get_fileselection() &&
514 !mask_match(finfo
->name
,client_get_fileselection(),false)) {
515 DEBUG(3,("mask_match %s failed\n", finfo
->name
));
519 if (newer_than
&& finfo
->mtime_ts
.tv_sec
< newer_than
) {
520 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
524 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& FILE_ATTRIBUTE_ARCHIVE
)) {
525 DEBUG(3,("archive %s failed\n", finfo
->name
));
532 /****************************************************************************
533 Display info about a file.
534 ****************************************************************************/
536 static NTSTATUS
display_finfo(struct cli_state
*cli_state
, struct file_info
*finfo
,
540 TALLOC_CTX
*ctx
= talloc_tos();
541 NTSTATUS status
= NT_STATUS_OK
;
543 if (!do_this_one(finfo
)) {
547 t
= finfo
->mtime_ts
.tv_sec
; /* the time is assumed to be passed as GMT */
549 d_printf(" %-30s%7.7s %8.0f %s",
551 attrib_string(finfo
->mode
),
554 dir_total
+= finfo
->size
;
559 /* skip if this is . or .. */
560 if ( strequal(finfo
->name
,"..") || strequal(finfo
->name
,".") )
562 /* create absolute filename for cli_ntcreate() FIXME */
563 afname
= talloc_asprintf(ctx
,
569 return NT_STATUS_NO_MEMORY
;
571 /* print file meta date header */
572 d_printf( "FILENAME:%s\n", finfo
->name
);
573 d_printf( "MODE:%s\n", attrib_string(finfo
->mode
));
574 d_printf( "SIZE:%.0f\n", (double)finfo
->size
);
575 d_printf( "MTIME:%s", time_to_asc(t
));
576 status
= cli_ntcreate(cli_state
, afname
, 0,
577 CREATE_ACCESS_READ
, 0,
578 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
579 FILE_OPEN
, 0x0, 0x0, &fnum
);
580 if (!NT_STATUS_IS_OK(status
)) {
581 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
582 afname
, nt_errstr(status
)));
584 struct security_descriptor
*sd
= NULL
;
585 sd
= cli_query_secdesc(cli_state
, fnum
, ctx
);
587 DEBUG( 0, ("display_finfo() failed to "
588 "get security descriptor: %s",
589 cli_errstr(cli_state
)));
590 status
= cli_nt_error(cli_state
);
592 display_sec_desc(sd
);
601 /****************************************************************************
602 Accumulate size of a file.
603 ****************************************************************************/
605 static NTSTATUS
do_du(struct cli_state
*cli_state
, struct file_info
*finfo
,
608 if (do_this_one(finfo
)) {
609 dir_total
+= finfo
->size
;
614 static bool do_list_recurse
;
615 static bool do_list_dirs
;
616 static char *do_list_queue
= 0;
617 static long do_list_queue_size
= 0;
618 static long do_list_queue_start
= 0;
619 static long do_list_queue_end
= 0;
620 static NTSTATUS (*do_list_fn
)(struct cli_state
*cli_state
, struct file_info
*,
623 /****************************************************************************
624 Functions for do_list_queue.
625 ****************************************************************************/
628 * The do_list_queue is a NUL-separated list of strings stored in a
629 * char*. Since this is a FIFO, we keep track of the beginning and
630 * ending locations of the data in the queue. When we overflow, we
631 * double the size of the char*. When the start of the data passes
632 * the midpoint, we move everything back. This is logically more
633 * complex than a linked list, but easier from a memory management
634 * angle. In any memory error condition, do_list_queue is reset.
635 * Functions check to ensure that do_list_queue is non-NULL before
639 static void reset_do_list_queue(void)
641 SAFE_FREE(do_list_queue
);
642 do_list_queue_size
= 0;
643 do_list_queue_start
= 0;
644 do_list_queue_end
= 0;
647 static void init_do_list_queue(void)
649 reset_do_list_queue();
650 do_list_queue_size
= 1024;
651 do_list_queue
= (char *)SMB_MALLOC(do_list_queue_size
);
652 if (do_list_queue
== 0) {
653 d_printf("malloc fail for size %d\n",
654 (int)do_list_queue_size
);
655 reset_do_list_queue();
657 memset(do_list_queue
, 0, do_list_queue_size
);
661 static void adjust_do_list_queue(void)
664 * If the starting point of the queue is more than half way through,
665 * move everything toward the beginning.
668 if (do_list_queue
== NULL
) {
669 DEBUG(4,("do_list_queue is empty\n"));
670 do_list_queue_start
= do_list_queue_end
= 0;
674 if (do_list_queue_start
== do_list_queue_end
) {
675 DEBUG(4,("do_list_queue is empty\n"));
676 do_list_queue_start
= do_list_queue_end
= 0;
677 *do_list_queue
= '\0';
678 } else if (do_list_queue_start
> (do_list_queue_size
/ 2)) {
679 DEBUG(4,("sliding do_list_queue backward\n"));
680 memmove(do_list_queue
,
681 do_list_queue
+ do_list_queue_start
,
682 do_list_queue_end
- do_list_queue_start
);
683 do_list_queue_end
-= do_list_queue_start
;
684 do_list_queue_start
= 0;
688 static void add_to_do_list_queue(const char *entry
)
690 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
691 while (new_end
> do_list_queue_size
) {
692 do_list_queue_size
*= 2;
693 DEBUG(4,("enlarging do_list_queue to %d\n",
694 (int)do_list_queue_size
));
695 do_list_queue
= (char *)SMB_REALLOC(do_list_queue
, do_list_queue_size
);
696 if (! do_list_queue
) {
697 d_printf("failure enlarging do_list_queue to %d bytes\n",
698 (int)do_list_queue_size
);
699 reset_do_list_queue();
701 memset(do_list_queue
+ do_list_queue_size
/ 2,
702 0, do_list_queue_size
/ 2);
706 safe_strcpy_base(do_list_queue
+ do_list_queue_end
,
707 entry
, do_list_queue
, do_list_queue_size
);
708 do_list_queue_end
= new_end
;
709 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
710 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
714 static char *do_list_queue_head(void)
716 return do_list_queue
+ do_list_queue_start
;
719 static void remove_do_list_queue_head(void)
721 if (do_list_queue_end
> do_list_queue_start
) {
722 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
723 adjust_do_list_queue();
724 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
725 (int)do_list_queue_start
, (int)do_list_queue_end
));
729 static int do_list_queue_empty(void)
731 return (! (do_list_queue
&& *do_list_queue
));
734 /****************************************************************************
735 A helper for do_list.
736 ****************************************************************************/
738 static NTSTATUS
do_list_helper(const char *mntpoint
, struct file_info
*f
,
739 const char *mask
, void *state
)
741 struct cli_state
*cli_state
= (struct cli_state
*)state
;
742 TALLOC_CTX
*ctx
= talloc_tos();
744 char *dir_end
= NULL
;
745 NTSTATUS status
= NT_STATUS_OK
;
747 /* Work out the directory. */
748 dir
= talloc_strdup(ctx
, mask
);
750 return NT_STATUS_NO_MEMORY
;
752 if ((dir_end
= strrchr(dir
, CLI_DIRSEP_CHAR
)) != NULL
) {
756 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
757 if (do_list_dirs
&& do_this_one(f
)) {
758 status
= do_list_fn(cli_state
, f
, dir
);
759 if (!NT_STATUS_IS_OK(status
)) {
763 if (do_list_recurse
&&
765 !strequal(f
->name
,".") &&
766 !strequal(f
->name
,"..")) {
771 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
773 return NT_STATUS_UNSUCCESSFUL
;
776 mask2
= talloc_asprintf(ctx
,
782 return NT_STATUS_NO_MEMORY
;
784 p
= strrchr_m(mask2
,CLI_DIRSEP_CHAR
);
790 mask2
= talloc_asprintf_append(mask2
,
796 return NT_STATUS_NO_MEMORY
;
798 add_to_do_list_queue(mask2
);
805 if (do_this_one(f
)) {
806 status
= do_list_fn(cli_state
, f
, dir
);
812 /****************************************************************************
813 A wrapper around cli_list that adds recursion.
814 ****************************************************************************/
816 NTSTATUS
do_list(const char *mask
,
818 NTSTATUS (*fn
)(struct cli_state
*cli_state
, struct file_info
*,
823 static int in_do_list
= 0;
824 TALLOC_CTX
*ctx
= talloc_tos();
825 struct cli_state
*targetcli
= NULL
;
826 char *targetpath
= NULL
;
827 NTSTATUS ret_status
= NT_STATUS_OK
;
828 NTSTATUS status
= NT_STATUS_OK
;
830 if (in_do_list
&& rec
) {
831 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
837 do_list_recurse
= rec
;
842 init_do_list_queue();
843 add_to_do_list_queue(mask
);
845 while (!do_list_queue_empty()) {
847 * Need to copy head so that it doesn't become
848 * invalid inside the call to cli_list. This
849 * would happen if the list were expanded
851 * Fix from E. Jay Berkenbilt (ejb@ql.org)
853 char *head
= talloc_strdup(ctx
, do_list_queue_head());
856 return NT_STATUS_NO_MEMORY
;
861 if ( !cli_resolve_path(ctx
, "", auth_info
, cli
, head
, &targetcli
, &targetpath
) ) {
862 d_printf("do_list: [%s] %s\n", head
, cli_errstr(cli
));
863 remove_do_list_queue_head();
867 status
= cli_list(targetcli
, targetpath
, attribute
,
868 do_list_helper
, targetcli
);
869 if (!NT_STATUS_IS_OK(status
)) {
870 d_printf("%s listing %s\n",
871 nt_errstr(status
), targetpath
);
874 remove_do_list_queue_head();
875 if ((! do_list_queue_empty()) && (fn
== display_finfo
)) {
876 char *next_file
= do_list_queue_head();
878 if ((strlen(next_file
) >= 2) &&
879 (next_file
[strlen(next_file
) - 1] == '*') &&
880 (next_file
[strlen(next_file
) - 2] == CLI_DIRSEP_CHAR
)) {
881 save_ch
= next_file
+
882 strlen(next_file
) - 2;
885 /* cwd is only used if showacls is on */
886 client_set_cwd(next_file
);
889 if (!showacls
) /* don't disturbe the showacls output */
890 d_printf("\n%s\n",next_file
);
892 *save_ch
= CLI_DIRSEP_CHAR
;
896 TALLOC_FREE(targetpath
);
900 if (cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetpath
)) {
902 status
= cli_list(targetcli
, targetpath
, attribute
,
903 do_list_helper
, targetcli
);
904 if (!NT_STATUS_IS_OK(status
)) {
905 d_printf("%s listing %s\n",
906 nt_errstr(status
), targetpath
);
909 TALLOC_FREE(targetpath
);
911 d_printf("do_list: [%s] %s\n", mask
, cli_errstr(cli
));
912 ret_status
= cli_nt_error(cli
);
917 reset_do_list_queue();
921 /****************************************************************************
922 Get a directory listing.
923 ****************************************************************************/
925 static int cmd_dir(void)
927 TALLOC_CTX
*ctx
= talloc_tos();
928 uint16 attribute
= FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
935 mask
= talloc_strdup(ctx
, client_get_cur_dir());
940 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
942 if (*buf
== CLI_DIRSEP_CHAR
) {
943 mask
= talloc_strdup(ctx
, buf
);
945 mask
= talloc_asprintf_append(mask
, "%s", buf
);
948 mask
= talloc_asprintf_append(mask
, "*");
955 /* cwd is only used if showacls is on */
956 client_set_cwd(client_get_cur_dir());
959 status
= do_list(mask
, attribute
, display_finfo
, recurse
, true);
960 if (!NT_STATUS_IS_OK(status
)) {
966 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
971 /****************************************************************************
972 Get a directory listing.
973 ****************************************************************************/
975 static int cmd_du(void)
977 TALLOC_CTX
*ctx
= talloc_tos();
978 uint16 attribute
= FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
985 mask
= talloc_strdup(ctx
, client_get_cur_dir());
989 if ((mask
[0] != '\0') && (mask
[strlen(mask
)-1]!=CLI_DIRSEP_CHAR
)) {
990 mask
= talloc_asprintf_append(mask
, "%s", CLI_DIRSEP_STR
);
996 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
998 if (*buf
== CLI_DIRSEP_CHAR
) {
999 mask
= talloc_strdup(ctx
, buf
);
1001 mask
= talloc_asprintf_append(mask
, "%s", buf
);
1004 mask
= talloc_strdup(ctx
, "*");
1007 status
= do_list(mask
, attribute
, do_du
, recurse
, true);
1008 if (!NT_STATUS_IS_OK(status
)) {
1014 d_printf("Total number of bytes: %.0f\n", dir_total
);
1019 static int cmd_echo(void)
1021 TALLOC_CTX
*ctx
= talloc_tos();
1026 if (!next_token_talloc(ctx
, &cmd_ptr
, &num
, NULL
)
1027 || !next_token_talloc(ctx
, &cmd_ptr
, &data
, NULL
)) {
1028 d_printf("echo <num> <data>\n");
1032 status
= cli_echo(cli
, atoi(num
), data_blob_const(data
, strlen(data
)));
1034 if (!NT_STATUS_IS_OK(status
)) {
1035 d_printf("echo failed: %s\n", nt_errstr(status
));
1042 /****************************************************************************
1043 Get a file from rname to lname
1044 ****************************************************************************/
1046 static NTSTATUS
writefile_sink(char *buf
, size_t n
, void *priv
)
1048 int *pfd
= (int *)priv
;
1049 if (writefile(*pfd
, buf
, n
) == -1) {
1050 return map_nt_error_from_unix(errno
);
1052 return NT_STATUS_OK
;
1055 static int do_get(const char *rname
, const char *lname_in
, bool reget
)
1057 TALLOC_CTX
*ctx
= talloc_tos();
1060 bool newhandle
= false;
1061 struct timespec tp_start
;
1065 SMB_OFF_T nread
= 0;
1067 struct cli_state
*targetcli
= NULL
;
1068 char *targetname
= NULL
;
1072 lname
= talloc_strdup(ctx
, lname_in
);
1081 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, rname
, &targetcli
, &targetname
) ) {
1082 d_printf("Failed to open %s: %s\n", rname
, cli_errstr(cli
));
1086 clock_gettime_mono(&tp_start
);
1088 status
= cli_open(targetcli
, targetname
, O_RDONLY
, DENY_NONE
, &fnum
);
1089 if (!NT_STATUS_IS_OK(status
)) {
1090 d_printf("%s opening remote file %s\n", nt_errstr(status
),
1095 if(!strcmp(lname
,"-")) {
1096 handle
= fileno(stdout
);
1099 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
, 0644);
1101 start
= sys_lseek(handle
, 0, SEEK_END
);
1103 d_printf("Error seeking local file\n");
1108 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
1113 d_printf("Error opening local file %s\n",lname
);
1118 status
= cli_qfileinfo_basic(targetcli
, fnum
, &attr
, &size
, NULL
, NULL
,
1120 if (!NT_STATUS_IS_OK(status
)) {
1121 status
= cli_getattrE(targetcli
, fnum
, &attr
, &size
, NULL
, NULL
,
1123 if(!NT_STATUS_IS_OK(status
)) {
1124 d_printf("getattrib: %s\n", nt_errstr(status
));
1129 DEBUG(1,("getting file %s of size %.0f as %s ",
1130 rname
, (double)size
, lname
));
1132 status
= cli_pull(targetcli
, fnum
, start
, size
, io_bufsize
,
1133 writefile_sink
, (void *)&handle
, &nread
);
1134 if (!NT_STATUS_IS_OK(status
)) {
1135 d_fprintf(stderr
, "parallel_read returned %s\n",
1137 cli_close(targetcli
, fnum
);
1141 status
= cli_close(targetcli
, fnum
);
1142 if (!NT_STATUS_IS_OK(status
)) {
1143 d_printf("Error %s closing remote file\n", nt_errstr(status
));
1151 if (archive_level
>= 2 && (attr
& FILE_ATTRIBUTE_ARCHIVE
)) {
1152 cli_setatr(cli
, rname
, attr
& ~(uint16
)FILE_ATTRIBUTE_ARCHIVE
, 0);
1156 struct timespec tp_end
;
1159 clock_gettime_mono(&tp_end
);
1160 this_time
= nsec_time_diff(&tp_end
,&tp_start
)/1000000;
1161 get_total_time_ms
+= this_time
;
1162 get_total_size
+= nread
;
1164 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1165 nread
/ (1.024*this_time
+ 1.0e-4),
1166 get_total_size
/ (1.024*get_total_time_ms
)));
1169 TALLOC_FREE(targetname
);
1173 /****************************************************************************
1175 ****************************************************************************/
1177 static int cmd_get(void)
1179 TALLOC_CTX
*ctx
= talloc_tos();
1184 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1189 if (!next_token_talloc(ctx
, &cmd_ptr
,&fname
,NULL
)) {
1190 d_printf("get <filename> [localname]\n");
1193 rname
= talloc_asprintf_append(rname
, "%s", fname
);
1197 rname
= clean_name(ctx
, rname
);
1202 next_token_talloc(ctx
, &cmd_ptr
,&lname
,NULL
);
1207 return do_get(rname
, lname
, false);
1210 /****************************************************************************
1211 Do an mget operation on one file.
1212 ****************************************************************************/
1214 static NTSTATUS
do_mget(struct cli_state
*cli_state
, struct file_info
*finfo
,
1217 TALLOC_CTX
*ctx
= talloc_tos();
1218 NTSTATUS status
= NT_STATUS_OK
;
1221 char *saved_curdir
= NULL
;
1222 char *mget_mask
= NULL
;
1223 char *new_cd
= NULL
;
1226 return NT_STATUS_OK
;
1229 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
1230 return NT_STATUS_OK
;
1233 d_printf("mget aborted\n");
1234 return NT_STATUS_UNSUCCESSFUL
;
1237 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
1238 if (asprintf(&quest
,
1239 "Get directory %s? ",finfo
->name
) < 0) {
1240 return NT_STATUS_NO_MEMORY
;
1243 if (asprintf(&quest
,
1244 "Get file %s? ",finfo
->name
) < 0) {
1245 return NT_STATUS_NO_MEMORY
;
1249 if (prompt
&& !yesno(quest
)) {
1251 return NT_STATUS_OK
;
1255 if (!(finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
)) {
1256 rname
= talloc_asprintf(ctx
,
1258 client_get_cur_dir(),
1261 return NT_STATUS_NO_MEMORY
;
1263 do_get(rname
, finfo
->name
, false);
1265 return NT_STATUS_OK
;
1268 /* handle directories */
1269 saved_curdir
= talloc_strdup(ctx
, client_get_cur_dir());
1270 if (!saved_curdir
) {
1271 return NT_STATUS_NO_MEMORY
;
1274 new_cd
= talloc_asprintf(ctx
,
1276 client_get_cur_dir(),
1280 return NT_STATUS_NO_MEMORY
;
1282 client_set_cur_dir(new_cd
);
1284 string_replace(finfo
->name
,'\\','/');
1286 strlower_m(finfo
->name
);
1289 if (!directory_exist(finfo
->name
) &&
1290 mkdir(finfo
->name
,0777) != 0) {
1291 d_printf("failed to create directory %s\n",finfo
->name
);
1292 client_set_cur_dir(saved_curdir
);
1293 return map_nt_error_from_unix(errno
);
1296 if (chdir(finfo
->name
) != 0) {
1297 d_printf("failed to chdir to directory %s\n",finfo
->name
);
1298 client_set_cur_dir(saved_curdir
);
1299 return map_nt_error_from_unix(errno
);
1302 mget_mask
= talloc_asprintf(ctx
,
1304 client_get_cur_dir());
1307 return NT_STATUS_NO_MEMORY
;
1310 status
= do_list(mget_mask
,
1311 (FILE_ATTRIBUTE_SYSTEM
1312 | FILE_ATTRIBUTE_HIDDEN
1313 | FILE_ATTRIBUTE_DIRECTORY
),
1314 do_mget
, false, true);
1315 if (!NT_STATUS_IS_OK(status
)
1316 && !NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
1318 * Ignore access denied errors to ensure all permitted files are
1324 if (chdir("..") == -1) {
1325 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1327 return map_nt_error_from_unix(errno
);
1329 client_set_cur_dir(saved_curdir
);
1330 TALLOC_FREE(mget_mask
);
1331 TALLOC_FREE(saved_curdir
);
1332 TALLOC_FREE(new_cd
);
1333 return NT_STATUS_OK
;
1336 /****************************************************************************
1337 View the file using the pager.
1338 ****************************************************************************/
1340 static int cmd_more(void)
1342 TALLOC_CTX
*ctx
= talloc_tos();
1346 char *pager_cmd
= NULL
;
1351 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1356 lname
= talloc_asprintf(ctx
, "%s/smbmore.XXXXXX",tmpdir());
1360 fd
= mkstemp(lname
);
1362 d_printf("failed to create temporary file for more\n");
1367 if (!next_token_talloc(ctx
, &cmd_ptr
,&fname
,NULL
)) {
1368 d_printf("more <filename>\n");
1372 rname
= talloc_asprintf_append(rname
, "%s", fname
);
1376 rname
= clean_name(ctx
,rname
);
1381 rc
= do_get(rname
, lname
, false);
1383 pager
=getenv("PAGER");
1385 pager_cmd
= talloc_asprintf(ctx
,
1387 (pager
? pager
:PAGER
),
1392 if (system(pager_cmd
) == -1) {
1393 d_printf("system command '%s' returned -1\n",
1401 /****************************************************************************
1403 ****************************************************************************/
1405 static int cmd_mget(void)
1407 TALLOC_CTX
*ctx
= talloc_tos();
1408 uint16 attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
1409 char *mget_mask
= NULL
;
1411 NTSTATUS status
= NT_STATUS_OK
;
1414 attribute
|= FILE_ATTRIBUTE_DIRECTORY
;
1419 while (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1421 mget_mask
= talloc_strdup(ctx
, client_get_cur_dir());
1425 if (*buf
== CLI_DIRSEP_CHAR
) {
1426 mget_mask
= talloc_strdup(ctx
, buf
);
1428 mget_mask
= talloc_asprintf_append(mget_mask
,
1434 status
= do_list(mget_mask
, attribute
, do_mget
, false, true);
1435 if (!NT_STATUS_IS_OK(status
)) {
1440 if (mget_mask
== NULL
) {
1441 d_printf("nothing to mget\n");
1446 mget_mask
= talloc_asprintf(ctx
,
1448 client_get_cur_dir());
1452 status
= do_list(mget_mask
, attribute
, do_mget
, false, true);
1453 if (!NT_STATUS_IS_OK(status
)) {
1461 /****************************************************************************
1462 Make a directory of name "name".
1463 ****************************************************************************/
1465 static bool do_mkdir(const char *name
)
1467 TALLOC_CTX
*ctx
= talloc_tos();
1468 struct cli_state
*targetcli
;
1469 char *targetname
= NULL
;
1472 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, name
, &targetcli
, &targetname
)) {
1473 d_printf("mkdir %s: %s\n", name
, cli_errstr(cli
));
1477 status
= cli_mkdir(targetcli
, targetname
);
1478 if (!NT_STATUS_IS_OK(status
)) {
1479 d_printf("%s making remote directory %s\n",
1480 nt_errstr(status
),name
);
1487 /****************************************************************************
1488 Show 8.3 name of a file.
1489 ****************************************************************************/
1491 static bool do_altname(const char *name
)
1496 status
= cli_qpathinfo_alt_name(cli
, name
, altname
);
1497 if (!NT_STATUS_IS_OK(status
)) {
1498 d_printf("%s getting alt name for %s\n",
1499 nt_errstr(status
),name
);
1502 d_printf("%s\n", altname
);
1507 /****************************************************************************
1509 ****************************************************************************/
1511 static int cmd_quit(void)
1519 /****************************************************************************
1521 ****************************************************************************/
1523 static int cmd_mkdir(void)
1525 TALLOC_CTX
*ctx
= talloc_tos();
1529 mask
= talloc_strdup(ctx
, client_get_cur_dir());
1534 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1536 d_printf("mkdir <dirname>\n");
1540 mask
= talloc_asprintf_append(mask
, "%s", buf
);
1548 struct cli_state
*targetcli
;
1549 char *targetname
= NULL
;
1553 ddir2
= talloc_strdup(ctx
, "");
1558 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
1562 ddir
= talloc_strdup(ctx
, targetname
);
1566 trim_char(ddir
,'.','\0');
1567 p
= strtok_r(ddir
, "/\\", &saveptr
);
1569 ddir2
= talloc_asprintf_append(ddir2
, "%s", p
);
1573 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli
, ddir2
))) {
1576 ddir2
= talloc_asprintf_append(ddir2
, "%s", CLI_DIRSEP_STR
);
1580 p
= strtok_r(NULL
, "/\\", &saveptr
);
1589 /****************************************************************************
1591 ****************************************************************************/
1593 static int cmd_altname(void)
1595 TALLOC_CTX
*ctx
= talloc_tos();
1599 name
= talloc_strdup(ctx
, client_get_cur_dir());
1604 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
1605 d_printf("altname <file>\n");
1608 name
= talloc_asprintf_append(name
, "%s", buf
);
1616 static char *attr_str(TALLOC_CTX
*mem_ctx
, uint16_t mode
)
1618 char *attrs
= TALLOC_ZERO_ARRAY(mem_ctx
, char, 17);
1621 if (!(mode
& FILE_ATTRIBUTE_NORMAL
)) {
1622 if (mode
& FILE_ATTRIBUTE_ENCRYPTED
) {
1625 if (mode
& FILE_ATTRIBUTE_NONINDEXED
) {
1628 if (mode
& FILE_ATTRIBUTE_OFFLINE
) {
1631 if (mode
& FILE_ATTRIBUTE_COMPRESSED
) {
1634 if (mode
& FILE_ATTRIBUTE_REPARSE_POINT
) {
1637 if (mode
& FILE_ATTRIBUTE_SPARSE
) {
1640 if (mode
& FILE_ATTRIBUTE_TEMPORARY
) {
1643 if (mode
& FILE_ATTRIBUTE_NORMAL
) {
1646 if (mode
& FILE_ATTRIBUTE_READONLY
) {
1649 if (mode
& FILE_ATTRIBUTE_HIDDEN
) {
1652 if (mode
& FILE_ATTRIBUTE_SYSTEM
) {
1655 if (mode
& FILE_ATTRIBUTE_DIRECTORY
) {
1658 if (mode
& FILE_ATTRIBUTE_ARCHIVE
) {
1665 /****************************************************************************
1666 Show all info we can get
1667 ****************************************************************************/
1669 static int do_allinfo(const char *name
)
1672 struct timespec b_time
, a_time
, m_time
, c_time
;
1678 unsigned int num_streams
;
1679 struct stream_struct
*streams
;
1685 status
= cli_qpathinfo_alt_name(cli
, name
, altname
);
1686 if (!NT_STATUS_IS_OK(status
)) {
1687 d_printf("%s getting alt name for %s\n", nt_errstr(status
),
1691 d_printf("altname: %s\n", altname
);
1693 status
= cli_qpathinfo2(cli
, name
, &b_time
, &a_time
, &m_time
, &c_time
,
1694 &size
, &mode
, &ino
);
1695 if (!NT_STATUS_IS_OK(status
)) {
1696 d_printf("%s getting pathinfo for %s\n", nt_errstr(status
),
1701 unix_timespec_to_nt_time(&tmp
, b_time
);
1702 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1704 unix_timespec_to_nt_time(&tmp
, a_time
);
1705 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1707 unix_timespec_to_nt_time(&tmp
, m_time
);
1708 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1710 unix_timespec_to_nt_time(&tmp
, c_time
);
1711 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1713 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode
), mode
);
1715 status
= cli_qpathinfo_streams(cli
, name
, talloc_tos(), &num_streams
,
1717 if (!NT_STATUS_IS_OK(status
)) {
1718 d_printf("%s getting streams for %s\n", nt_errstr(status
),
1723 for (i
=0; i
<num_streams
; i
++) {
1724 d_printf("stream: [%s], %lld bytes\n", streams
[i
].name
,
1725 (unsigned long long)streams
[i
].size
);
1728 status
= cli_ntcreate(cli
, name
, 0,
1729 SEC_FILE_READ_DATA
| SEC_FILE_READ_ATTRIBUTE
|
1730 SEC_STD_SYNCHRONIZE
, 0,
1731 FILE_SHARE_READ
|FILE_SHARE_WRITE
1733 FILE_OPEN
, 0x0, 0x0, &fnum
);
1734 if (!NT_STATUS_IS_OK(status
)) {
1736 * Ignore failure, it does not hurt if we can't list
1741 status
= cli_shadow_copy_data(talloc_tos(), cli
, fnum
,
1742 true, &snapshots
, &num_snapshots
);
1743 if (!NT_STATUS_IS_OK(status
)) {
1744 cli_close(cli
, fnum
);
1748 for (i
=0; i
<num_snapshots
; i
++) {
1751 d_printf("%s\n", snapshots
[i
]);
1752 snap_name
= talloc_asprintf(talloc_tos(), "%s%s",
1753 snapshots
[i
], name
);
1754 status
= cli_qpathinfo2(cli
, snap_name
, &b_time
, &a_time
,
1755 &m_time
, &c_time
, &size
,
1757 if (!NT_STATUS_IS_OK(status
)) {
1758 d_fprintf(stderr
, "pathinfo(%s) failed: %s\n",
1759 snap_name
, nt_errstr(status
));
1760 TALLOC_FREE(snap_name
);
1763 unix_timespec_to_nt_time(&tmp
, b_time
);
1764 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1765 unix_timespec_to_nt_time(&tmp
, a_time
);
1766 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1767 unix_timespec_to_nt_time(&tmp
, m_time
);
1768 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1769 unix_timespec_to_nt_time(&tmp
, c_time
);
1770 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1771 d_printf("size: %d\n", (int)size
);
1774 TALLOC_FREE(snapshots
);
1779 /****************************************************************************
1780 Show all info we can get
1781 ****************************************************************************/
1783 static int cmd_allinfo(void)
1785 TALLOC_CTX
*ctx
= talloc_tos();
1789 name
= talloc_strdup(ctx
, client_get_cur_dir());
1794 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
1795 d_printf("allinfo <file>\n");
1798 name
= talloc_asprintf_append(name
, "%s", buf
);
1808 /****************************************************************************
1810 ****************************************************************************/
1812 static int do_put(const char *rname
, const char *lname
, bool reput
)
1814 TALLOC_CTX
*ctx
= talloc_tos();
1817 SMB_OFF_T start
= 0;
1819 struct timespec tp_start
;
1820 struct cli_state
*targetcli
;
1821 char *targetname
= NULL
;
1822 struct push_state state
;
1825 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, rname
, &targetcli
, &targetname
)) {
1826 d_printf("Failed to open %s: %s\n", rname
, cli_errstr(cli
));
1830 clock_gettime_mono(&tp_start
);
1833 status
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
, DENY_NONE
, &fnum
);
1834 if (NT_STATUS_IS_OK(status
)) {
1835 if (!NT_STATUS_IS_OK(status
= cli_qfileinfo_basic(
1836 targetcli
, fnum
, NULL
,
1838 NULL
, NULL
, NULL
)) &&
1839 !NT_STATUS_IS_OK(status
= cli_getattrE(
1840 targetcli
, fnum
, NULL
,
1843 d_printf("getattrib: %s\n", nt_errstr(status
));
1848 status
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
, &fnum
);
1851 if (!NT_STATUS_IS_OK(status
)) {
1852 d_printf("%s opening remote file %s\n", nt_errstr(status
),
1857 /* allow files to be piped into smbclient
1860 Note that in this case this function will exit(0) rather
1862 if (!strcmp(lname
, "-")) {
1864 /* size of file is not known */
1866 f
= x_fopen(lname
,O_RDONLY
, 0);
1868 if (x_tseek(f
, start
, SEEK_SET
) == -1) {
1869 d_printf("Error seeking local file\n");
1877 d_printf("Error opening local file %s\n",lname
);
1881 DEBUG(1,("putting file %s as %s ",lname
,
1884 x_setvbuf(f
, NULL
, X_IOFBF
, io_bufsize
);
1889 status
= cli_push(targetcli
, fnum
, 0, 0, io_bufsize
, push_source
,
1891 if (!NT_STATUS_IS_OK(status
)) {
1892 d_fprintf(stderr
, "cli_push returned %s\n", nt_errstr(status
));
1896 status
= cli_close(targetcli
, fnum
);
1897 if (!NT_STATUS_IS_OK(status
)) {
1898 d_printf("%s closing remote file %s\n", nt_errstr(status
),
1911 struct timespec tp_end
;
1914 clock_gettime_mono(&tp_end
);
1915 this_time
= nsec_time_diff(&tp_end
,&tp_start
)/1000000;
1916 put_total_time_ms
+= this_time
;
1917 put_total_size
+= state
.nread
;
1919 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1920 state
.nread
/ (1.024*this_time
+ 1.0e-4),
1921 put_total_size
/ (1.024*put_total_time_ms
)));
1932 /****************************************************************************
1934 ****************************************************************************/
1936 static int cmd_put(void)
1938 TALLOC_CTX
*ctx
= talloc_tos();
1943 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1948 if (!next_token_talloc(ctx
, &cmd_ptr
,&lname
,NULL
)) {
1949 d_printf("put <filename>\n");
1953 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1954 rname
= talloc_asprintf_append(rname
, "%s", buf
);
1956 rname
= talloc_asprintf_append(rname
, "%s", lname
);
1962 rname
= clean_name(ctx
, rname
);
1969 /* allow '-' to represent stdin
1970 jdblair, 24.jun.98 */
1971 if (!file_exist_stat(lname
, &st
, false) &&
1972 (strcmp(lname
,"-"))) {
1973 d_printf("%s does not exist\n",lname
);
1978 return do_put(rname
, lname
, false);
1981 /*************************************
1982 File list structure.
1983 *************************************/
1985 static struct file_list
{
1986 struct file_list
*prev
, *next
;
1991 /****************************************************************************
1992 Free a file_list structure.
1993 ****************************************************************************/
1995 static void free_file_list (struct file_list
*l_head
)
1997 struct file_list
*list
, *next
;
1999 for (list
= l_head
; list
; list
= next
) {
2001 DLIST_REMOVE(l_head
, list
);
2002 SAFE_FREE(list
->file_path
);
2007 /****************************************************************************
2008 Seek in a directory/file list until you get something that doesn't start with
2010 ****************************************************************************/
2012 static bool seek_list(struct file_list
*list
, char *name
)
2015 trim_string(list
->file_path
,"./","\n");
2016 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
2025 /****************************************************************************
2026 Set the file selection mask.
2027 ****************************************************************************/
2029 static int cmd_select(void)
2031 TALLOC_CTX
*ctx
= talloc_tos();
2032 char *new_fs
= NULL
;
2033 next_token_talloc(ctx
, &cmd_ptr
,&new_fs
,NULL
)
2036 client_set_fileselection(new_fs
);
2038 client_set_fileselection("");
2043 /****************************************************************************
2044 Recursive file matching function act as find
2045 match must be always set to true when calling this function
2046 ****************************************************************************/
2048 static int file_find(struct file_list
**list
, const char *directory
,
2049 const char *expression
, bool match
)
2051 SMB_STRUCT_DIR
*dir
;
2052 struct file_list
*entry
;
2053 struct stat statbuf
;
2059 dir
= sys_opendir(directory
);
2063 while ((dname
= readdirname(dir
))) {
2064 if (!strcmp("..", dname
))
2066 if (!strcmp(".", dname
))
2069 if (asprintf(&path
, "%s/%s", directory
, dname
) <= 0) {
2074 if (!match
|| !gen_fnmatch(expression
, dname
)) {
2076 ret
= stat(path
, &statbuf
);
2078 if (S_ISDIR(statbuf
.st_mode
)) {
2080 ret
= file_find(list
, path
, expression
, false);
2083 d_printf("file_find: cannot stat file %s\n", path
);
2092 entry
= SMB_MALLOC_P(struct file_list
);
2094 d_printf("Out of memory in file_find\n");
2098 entry
->file_path
= path
;
2099 entry
->isdir
= isdir
;
2100 DLIST_ADD(*list
, entry
);
2110 /****************************************************************************
2112 ****************************************************************************/
2114 static int cmd_mput(void)
2116 TALLOC_CTX
*ctx
= talloc_tos();
2119 while (next_token_talloc(ctx
, &cmd_ptr
,&p
,NULL
)) {
2121 struct file_list
*temp_list
;
2122 char *quest
, *lname
, *rname
;
2126 ret
= file_find(&file_list
, ".", p
, true);
2128 free_file_list(file_list
);
2136 for (temp_list
= file_list
; temp_list
;
2137 temp_list
= temp_list
->next
) {
2140 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0) {
2143 trim_string(lname
, "./", "/");
2145 /* check if it's a directory */
2146 if (temp_list
->isdir
) {
2147 /* if (!recurse) continue; */
2150 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) {
2153 if (prompt
&& !yesno(quest
)) { /* No */
2154 /* Skip the directory */
2155 lname
[strlen(lname
)-1] = '/';
2156 if (!seek_list(temp_list
, lname
))
2160 if(asprintf(&rname
, "%s%s", client_get_cur_dir(), lname
) < 0) {
2163 normalize_name(rname
);
2164 if (!NT_STATUS_IS_OK(cli_chkpath(cli
, rname
)) &&
2166 DEBUG (0, ("Unable to make dir, skipping..."));
2167 /* Skip the directory */
2168 lname
[strlen(lname
)-1] = '/';
2169 if (!seek_list(temp_list
, lname
)) {
2177 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) {
2180 if (prompt
&& !yesno(quest
)) {
2187 if (asprintf(&rname
, "%s%s", client_get_cur_dir(), lname
) < 0) {
2192 normalize_name(rname
);
2194 do_put(rname
, lname
, false);
2196 free_file_list(file_list
);
2205 /****************************************************************************
2207 ****************************************************************************/
2209 static int do_cancel(int job
)
2211 if (cli_printjob_del(cli
, job
)) {
2212 d_printf("Job %d cancelled\n",job
);
2215 d_printf("Error cancelling job %d : %s\n",job
,cli_errstr(cli
));
2220 /****************************************************************************
2222 ****************************************************************************/
2224 static int cmd_cancel(void)
2226 TALLOC_CTX
*ctx
= talloc_tos();
2230 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
,NULL
)) {
2231 d_printf("cancel <jobid> ...\n");
2237 } while (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
));
2242 /****************************************************************************
2244 ****************************************************************************/
2246 static int cmd_print(void)
2248 TALLOC_CTX
*ctx
= talloc_tos();
2253 if (!next_token_talloc(ctx
, &cmd_ptr
, &lname
,NULL
)) {
2254 d_printf("print <filename>\n");
2258 rname
= talloc_strdup(ctx
, lname
);
2262 p
= strrchr_m(rname
,'/');
2264 rname
= talloc_asprintf(ctx
,
2269 if (strequal(lname
,"-")) {
2270 rname
= talloc_asprintf(ctx
,
2278 return do_put(rname
, lname
, false);
2281 /****************************************************************************
2282 Show a print queue entry.
2283 ****************************************************************************/
2285 static void queue_fn(struct print_job_info
*p
)
2287 d_printf("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
);
2290 /****************************************************************************
2292 ****************************************************************************/
2294 static int cmd_queue(void)
2296 cli_print_queue(cli
, queue_fn
);
2300 /****************************************************************************
2302 ****************************************************************************/
2304 static NTSTATUS
do_del(struct cli_state
*cli_state
, struct file_info
*finfo
,
2307 TALLOC_CTX
*ctx
= talloc_tos();
2311 mask
= talloc_asprintf(ctx
,
2317 return NT_STATUS_NO_MEMORY
;
2320 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
2322 return NT_STATUS_OK
;
2325 status
= cli_unlink(cli_state
, mask
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
2326 if (!NT_STATUS_IS_OK(status
)) {
2327 d_printf("%s deleting remote file %s\n",
2328 nt_errstr(status
), mask
);
2334 /****************************************************************************
2336 ****************************************************************************/
2338 static int cmd_del(void)
2340 TALLOC_CTX
*ctx
= talloc_tos();
2343 NTSTATUS status
= NT_STATUS_OK
;
2344 uint16 attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
2347 attribute
|= FILE_ATTRIBUTE_DIRECTORY
;
2350 mask
= talloc_strdup(ctx
, client_get_cur_dir());
2354 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2355 d_printf("del <filename>\n");
2358 mask
= talloc_asprintf_append(mask
, "%s", buf
);
2363 status
= do_list(mask
,attribute
,do_del
,false,false);
2364 if (!NT_STATUS_IS_OK(status
)) {
2370 /****************************************************************************
2371 Wildcard delete some files.
2372 ****************************************************************************/
2374 static int cmd_wdel(void)
2376 TALLOC_CTX
*ctx
= talloc_tos();
2380 struct cli_state
*targetcli
;
2381 char *targetname
= NULL
;
2384 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2385 d_printf("wdel 0x<attrib> <wcard>\n");
2389 attribute
= (uint16
)strtol(buf
, (char **)NULL
, 16);
2391 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2392 d_printf("wdel 0x<attrib> <wcard>\n");
2396 mask
= talloc_asprintf(ctx
, "%s%s",
2397 client_get_cur_dir(),
2403 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2404 d_printf("cmd_wdel %s: %s\n", mask
, cli_errstr(cli
));
2408 status
= cli_unlink(targetcli
, targetname
, attribute
);
2409 if (!NT_STATUS_IS_OK(status
)) {
2410 d_printf("%s deleting remote files %s\n", nt_errstr(status
),
2416 /****************************************************************************
2417 ****************************************************************************/
2419 static int cmd_open(void)
2421 TALLOC_CTX
*ctx
= talloc_tos();
2424 char *targetname
= NULL
;
2425 struct cli_state
*targetcli
;
2426 uint16_t fnum
= (uint16_t)-1;
2428 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2429 d_printf("open <filename>\n");
2432 mask
= talloc_asprintf(ctx
,
2434 client_get_cur_dir(),
2440 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2441 d_printf("open %s: %s\n", mask
, cli_errstr(cli
));
2445 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli
, targetname
, 0,
2446 FILE_READ_DATA
|FILE_WRITE_DATA
, 0,
2447 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
2448 if (NT_STATUS_IS_OK(cli_ntcreate(targetcli
, targetname
, 0,
2450 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
2451 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
2453 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
2456 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
2461 static int cmd_posix_encrypt(void)
2463 TALLOC_CTX
*ctx
= talloc_tos();
2464 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2466 if (cli
->use_kerberos
) {
2467 status
= cli_gss_smb_encryption_start(cli
);
2469 char *domain
= NULL
;
2471 char *password
= NULL
;
2473 if (!next_token_talloc(ctx
, &cmd_ptr
,&domain
,NULL
)) {
2474 d_printf("posix_encrypt domain user password\n");
2478 if (!next_token_talloc(ctx
, &cmd_ptr
,&user
,NULL
)) {
2479 d_printf("posix_encrypt domain user password\n");
2483 if (!next_token_talloc(ctx
, &cmd_ptr
,&password
,NULL
)) {
2484 d_printf("posix_encrypt domain user password\n");
2488 status
= cli_raw_ntlm_smb_encryption_start(cli
,
2494 if (!NT_STATUS_IS_OK(status
)) {
2495 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status
));
2497 d_printf("encryption on\n");
2504 /****************************************************************************
2505 ****************************************************************************/
2507 static int cmd_posix_open(void)
2509 TALLOC_CTX
*ctx
= talloc_tos();
2512 char *targetname
= NULL
;
2513 struct cli_state
*targetcli
;
2517 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2518 d_printf("posix_open <filename> 0<mode>\n");
2521 mask
= talloc_asprintf(ctx
,
2523 client_get_cur_dir(),
2529 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2530 d_printf("posix_open <filename> 0<mode>\n");
2533 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
2535 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2536 d_printf("posix_open %s: %s\n", mask
, cli_errstr(cli
));
2540 if (!NT_STATUS_IS_OK(cli_posix_open(targetcli
, targetname
, O_CREAT
|O_RDWR
, mode
, &fnum
))) {
2541 if (NT_STATUS_IS_OK(cli_posix_open(targetcli
, targetname
, O_CREAT
|O_RDONLY
, mode
, &fnum
))) {
2542 d_printf("posix_open file %s: for readonly fnum %d\n", targetname
, fnum
);
2544 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
2547 d_printf("posix_open file %s: for read/write fnum %d\n", targetname
, fnum
);
2553 static int cmd_posix_mkdir(void)
2555 TALLOC_CTX
*ctx
= talloc_tos();
2558 char *targetname
= NULL
;
2559 struct cli_state
*targetcli
;
2562 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2563 d_printf("posix_mkdir <filename> 0<mode>\n");
2566 mask
= talloc_asprintf(ctx
,
2568 client_get_cur_dir(),
2574 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2575 d_printf("posix_mkdir <filename> 0<mode>\n");
2578 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
2580 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2581 d_printf("posix_mkdir %s: %s\n", mask
, cli_errstr(cli
));
2585 if (!NT_STATUS_IS_OK(cli_posix_mkdir(targetcli
, targetname
, mode
))) {
2586 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
2588 d_printf("posix_mkdir created directory %s\n", targetname
);
2593 static int cmd_posix_unlink(void)
2595 TALLOC_CTX
*ctx
= talloc_tos();
2598 char *targetname
= NULL
;
2599 struct cli_state
*targetcli
;
2601 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2602 d_printf("posix_unlink <filename>\n");
2605 mask
= talloc_asprintf(ctx
,
2607 client_get_cur_dir(),
2613 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2614 d_printf("posix_unlink %s: %s\n", mask
, cli_errstr(cli
));
2618 if (!NT_STATUS_IS_OK(cli_posix_unlink(targetcli
, targetname
))) {
2619 d_printf("Failed to unlink file %s. %s\n", targetname
, cli_errstr(cli
));
2621 d_printf("posix_unlink deleted file %s\n", targetname
);
2627 static int cmd_posix_rmdir(void)
2629 TALLOC_CTX
*ctx
= talloc_tos();
2632 char *targetname
= NULL
;
2633 struct cli_state
*targetcli
;
2635 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2636 d_printf("posix_rmdir <filename>\n");
2639 mask
= talloc_asprintf(ctx
,
2641 client_get_cur_dir(),
2647 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2648 d_printf("posix_rmdir %s: %s\n", mask
, cli_errstr(cli
));
2652 if (!NT_STATUS_IS_OK(cli_posix_rmdir(targetcli
, targetname
))) {
2653 d_printf("Failed to unlink directory %s. %s\n", targetname
, cli_errstr(cli
));
2655 d_printf("posix_rmdir deleted directory %s\n", targetname
);
2661 static int cmd_close(void)
2663 TALLOC_CTX
*ctx
= talloc_tos();
2667 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2668 d_printf("close <fnum>\n");
2673 /* We really should use the targetcli here.... */
2674 if (!NT_STATUS_IS_OK(cli_close(cli
, fnum
))) {
2675 d_printf("close %d: %s\n", fnum
, cli_errstr(cli
));
2681 static int cmd_posix(void)
2683 TALLOC_CTX
*ctx
= talloc_tos();
2684 uint16 major
, minor
;
2685 uint32 caplow
, caphigh
;
2689 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
2690 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2694 status
= cli_unix_extensions_version(cli
, &major
, &minor
, &caplow
,
2696 if (!NT_STATUS_IS_OK(status
)) {
2697 d_printf("Can't get UNIX CIFS extensions version from "
2698 "server: %s\n", nt_errstr(status
));
2702 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major
, (unsigned int)minor
);
2704 caps
= talloc_strdup(ctx
, "");
2708 if (caplow
& CIFS_UNIX_FCNTL_LOCKS_CAP
) {
2709 caps
= talloc_asprintf_append(caps
, "locks ");
2714 if (caplow
& CIFS_UNIX_POSIX_ACLS_CAP
) {
2715 caps
= talloc_asprintf_append(caps
, "acls ");
2720 if (caplow
& CIFS_UNIX_XATTTR_CAP
) {
2721 caps
= talloc_asprintf_append(caps
, "eas ");
2726 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
2727 caps
= talloc_asprintf_append(caps
, "pathnames ");
2732 if (caplow
& CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP
) {
2733 caps
= talloc_asprintf_append(caps
, "posix_path_operations ");
2738 if (caplow
& CIFS_UNIX_LARGE_READ_CAP
) {
2739 caps
= talloc_asprintf_append(caps
, "large_read ");
2744 if (caplow
& CIFS_UNIX_LARGE_WRITE_CAP
) {
2745 caps
= talloc_asprintf_append(caps
, "large_write ");
2750 if (caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP
) {
2751 caps
= talloc_asprintf_append(caps
, "posix_encrypt ");
2756 if (caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP
) {
2757 caps
= talloc_asprintf_append(caps
, "mandatory_posix_encrypt ");
2763 if (*caps
&& caps
[strlen(caps
)-1] == ' ') {
2764 caps
[strlen(caps
)-1] = '\0';
2767 d_printf("Server supports CIFS capabilities %s\n", caps
);
2769 status
= cli_set_unix_extensions_capabilities(cli
, major
, minor
,
2771 if (!NT_STATUS_IS_OK(status
)) {
2772 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2777 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
2778 CLI_DIRSEP_CHAR
= '/';
2779 *CLI_DIRSEP_STR
= '/';
2780 client_set_cur_dir(CLI_DIRSEP_STR
);
2786 static int cmd_lock(void)
2788 TALLOC_CTX
*ctx
= talloc_tos();
2790 uint64_t start
, len
;
2791 enum brl_type lock_type
;
2794 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2795 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2800 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2801 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2805 if (*buf
== 'r' || *buf
== 'R') {
2806 lock_type
= READ_LOCK
;
2807 } else if (*buf
== 'w' || *buf
== 'W') {
2808 lock_type
= WRITE_LOCK
;
2810 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2814 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2815 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2819 start
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
2821 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2822 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2826 len
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
2828 if (!NT_STATUS_IS_OK(cli_posix_lock(cli
, fnum
, start
, len
, true, lock_type
))) {
2829 d_printf("lock failed %d: %s\n", fnum
, cli_errstr(cli
));
2835 static int cmd_unlock(void)
2837 TALLOC_CTX
*ctx
= talloc_tos();
2839 uint64_t start
, len
;
2842 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2843 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2848 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2849 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2853 start
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
2855 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2856 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2860 len
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
2862 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli
, fnum
, start
, len
))) {
2863 d_printf("unlock failed %d: %s\n", fnum
, cli_errstr(cli
));
2870 /****************************************************************************
2872 ****************************************************************************/
2874 static int cmd_rmdir(void)
2876 TALLOC_CTX
*ctx
= talloc_tos();
2879 char *targetname
= NULL
;
2880 struct cli_state
*targetcli
;
2882 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2883 d_printf("rmdir <dirname>\n");
2886 mask
= talloc_asprintf(ctx
,
2888 client_get_cur_dir(),
2894 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2895 d_printf("rmdir %s: %s\n", mask
, cli_errstr(cli
));
2899 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetname
))) {
2900 d_printf("%s removing remote directory file %s\n",
2901 cli_errstr(targetcli
),mask
);
2907 /****************************************************************************
2909 ****************************************************************************/
2911 static int cmd_link(void)
2913 TALLOC_CTX
*ctx
= talloc_tos();
2914 char *oldname
= NULL
;
2915 char *newname
= NULL
;
2918 char *targetname
= NULL
;
2919 struct cli_state
*targetcli
;
2921 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
2922 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
2923 d_printf("link <oldname> <newname>\n");
2926 oldname
= talloc_asprintf(ctx
,
2928 client_get_cur_dir(),
2933 newname
= talloc_asprintf(ctx
,
2935 client_get_cur_dir(),
2941 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, oldname
, &targetcli
, &targetname
)) {
2942 d_printf("link %s: %s\n", oldname
, cli_errstr(cli
));
2946 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2947 d_printf("Server doesn't support UNIX CIFS calls.\n");
2951 if (!NT_STATUS_IS_OK(cli_posix_hardlink(targetcli
, targetname
, newname
))) {
2952 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli
), newname
, oldname
);
2958 /****************************************************************************
2960 ****************************************************************************/
2962 static int cmd_readlink(void)
2964 TALLOC_CTX
*ctx
= talloc_tos();
2967 char *targetname
= NULL
;
2968 char linkname
[PATH_MAX
+1];
2969 struct cli_state
*targetcli
;
2971 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2972 d_printf("readlink <name>\n");
2975 name
= talloc_asprintf(ctx
,
2977 client_get_cur_dir(),
2983 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, name
, &targetcli
, &targetname
)) {
2984 d_printf("readlink %s: %s\n", name
, cli_errstr(cli
));
2988 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2989 d_printf("Server doesn't support UNIX CIFS calls.\n");
2993 if (!NT_STATUS_IS_OK(cli_posix_readlink(targetcli
, name
,
2994 linkname
, PATH_MAX
+1))) {
2995 d_printf("%s readlink on file %s\n",
2996 cli_errstr(targetcli
), name
);
3000 d_printf("%s -> %s\n", name
, linkname
);
3006 /****************************************************************************
3008 ****************************************************************************/
3010 static int cmd_symlink(void)
3012 TALLOC_CTX
*ctx
= talloc_tos();
3013 char *oldname
= NULL
;
3014 char *newname
= NULL
;
3017 struct cli_state
*newcli
;
3019 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3020 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3021 d_printf("symlink <oldname> <newname>\n");
3024 /* Oldname (link target) must be an untouched blob. */
3027 newname
= talloc_asprintf(ctx
,
3029 client_get_cur_dir(),
3035 /* New name must be present in share namespace. */
3036 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, newname
, &newcli
, &newname
)) {
3037 d_printf("link %s: %s\n", oldname
, cli_errstr(cli
));
3041 if (!SERVER_HAS_UNIX_CIFS(newcli
)) {
3042 d_printf("Server doesn't support UNIX CIFS calls.\n");
3046 if (!NT_STATUS_IS_OK(cli_posix_symlink(newcli
, oldname
, newname
))) {
3047 d_printf("%s symlinking files (%s -> %s)\n",
3048 cli_errstr(newcli
), newname
, newname
);
3055 /****************************************************************************
3057 ****************************************************************************/
3059 static int cmd_chmod(void)
3061 TALLOC_CTX
*ctx
= talloc_tos();
3065 char *targetname
= NULL
;
3066 struct cli_state
*targetcli
;
3069 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3070 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3071 d_printf("chmod mode file\n");
3074 src
= talloc_asprintf(ctx
,
3076 client_get_cur_dir(),
3082 mode
= (mode_t
)strtol(buf
, NULL
, 8);
3084 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
)) {
3085 d_printf("chmod %s: %s\n", src
, cli_errstr(cli
));
3089 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3090 d_printf("Server doesn't support UNIX CIFS calls.\n");
3094 if (!NT_STATUS_IS_OK(cli_posix_chmod(targetcli
, targetname
, mode
))) {
3095 d_printf("%s chmod file %s 0%o\n",
3096 cli_errstr(targetcli
), src
, (unsigned int)mode
);
3103 static const char *filetype_to_str(mode_t mode
)
3105 if (S_ISREG(mode
)) {
3106 return "regular file";
3107 } else if (S_ISDIR(mode
)) {
3111 if (S_ISCHR(mode
)) {
3112 return "character device";
3116 if (S_ISBLK(mode
)) {
3117 return "block device";
3121 if (S_ISFIFO(mode
)) {
3126 if (S_ISLNK(mode
)) {
3127 return "symbolic link";
3131 if (S_ISSOCK(mode
)) {
3138 static char rwx_to_str(mode_t m
, mode_t bt
, char ret
)
3147 static char *unix_mode_to_str(char *s
, mode_t m
)
3150 const char *str
= filetype_to_str(m
);
3166 *p
++ = str
[1] == 'y' ? 'l' : 's';
3173 *p
++ = rwx_to_str(m
, S_IRUSR
, 'r');
3174 *p
++ = rwx_to_str(m
, S_IWUSR
, 'w');
3175 *p
++ = rwx_to_str(m
, S_IXUSR
, 'x');
3176 *p
++ = rwx_to_str(m
, S_IRGRP
, 'r');
3177 *p
++ = rwx_to_str(m
, S_IWGRP
, 'w');
3178 *p
++ = rwx_to_str(m
, S_IXGRP
, 'x');
3179 *p
++ = rwx_to_str(m
, S_IROTH
, 'r');
3180 *p
++ = rwx_to_str(m
, S_IWOTH
, 'w');
3181 *p
++ = rwx_to_str(m
, S_IXOTH
, 'x');
3186 /****************************************************************************
3187 Utility function for UNIX getfacl.
3188 ****************************************************************************/
3190 static char *perms_to_string(fstring permstr
, unsigned char perms
)
3192 fstrcpy(permstr
, "---");
3193 if (perms
& SMB_POSIX_ACL_READ
) {
3196 if (perms
& SMB_POSIX_ACL_WRITE
) {
3199 if (perms
& SMB_POSIX_ACL_EXECUTE
) {
3205 /****************************************************************************
3207 ****************************************************************************/
3209 static int cmd_getfacl(void)
3211 TALLOC_CTX
*ctx
= talloc_tos();
3214 char *targetname
= NULL
;
3215 struct cli_state
*targetcli
;
3216 uint16 major
, minor
;
3217 uint32 caplow
, caphigh
;
3218 char *retbuf
= NULL
;
3220 SMB_STRUCT_STAT sbuf
;
3221 uint16 num_file_acls
= 0;
3222 uint16 num_dir_acls
= 0;
3226 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
3227 d_printf("getfacl filename\n");
3230 src
= talloc_asprintf(ctx
,
3232 client_get_cur_dir(),
3238 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
)) {
3239 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
3243 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3244 d_printf("Server doesn't support UNIX CIFS calls.\n");
3248 status
= cli_unix_extensions_version(targetcli
, &major
, &minor
,
3250 if (!NT_STATUS_IS_OK(status
)) {
3251 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3256 if (!(caplow
& CIFS_UNIX_POSIX_ACLS_CAP
)) {
3257 d_printf("This server supports UNIX extensions "
3258 "but doesn't support POSIX ACLs.\n");
3262 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli
, targetname
, &sbuf
))) {
3263 d_printf("%s getfacl doing a stat on file %s\n",
3264 cli_errstr(targetcli
), src
);
3268 if (!NT_STATUS_IS_OK(cli_posix_getfacl(targetcli
, targetname
, ctx
, &rb_size
, &retbuf
))) {
3269 d_printf("%s getfacl file %s\n",
3270 cli_errstr(targetcli
), src
);
3274 /* ToDo : Print out the ACL values. */
3275 if (rb_size
< 6 || SVAL(retbuf
,0) != SMB_POSIX_ACL_VERSION
) {
3276 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3277 src
, (unsigned int)CVAL(retbuf
,0) );
3281 num_file_acls
= SVAL(retbuf
,2);
3282 num_dir_acls
= SVAL(retbuf
,4);
3283 if (rb_size
!= SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)) {
3284 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3286 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)),
3287 (unsigned int)rb_size
);
3291 d_printf("# file: %s\n", src
);
3292 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf
.st_ex_uid
, (unsigned int)sbuf
.st_ex_gid
);
3294 if (num_file_acls
== 0 && num_dir_acls
== 0) {
3295 d_printf("No acls found.\n");
3298 for (i
= 0; i
< num_file_acls
; i
++) {
3301 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
));
3302 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
3305 case SMB_POSIX_ACL_USER_OBJ
:
3308 case SMB_POSIX_ACL_USER
:
3309 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3310 d_printf("user:%u:", uorg
);
3312 case SMB_POSIX_ACL_GROUP_OBJ
:
3313 d_printf("group::");
3315 case SMB_POSIX_ACL_GROUP
:
3316 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3317 d_printf("group:%u:", uorg
);
3319 case SMB_POSIX_ACL_MASK
:
3322 case SMB_POSIX_ACL_OTHER
:
3323 d_printf("other::");
3326 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3327 src
, (unsigned int)tagtype
);
3332 d_printf("%s\n", perms_to_string(permstring
, perms
));
3335 for (i
= 0; i
< num_dir_acls
; i
++) {
3338 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
));
3339 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
3342 case SMB_POSIX_ACL_USER_OBJ
:
3343 d_printf("default:user::");
3345 case SMB_POSIX_ACL_USER
:
3346 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3347 d_printf("default:user:%u:", uorg
);
3349 case SMB_POSIX_ACL_GROUP_OBJ
:
3350 d_printf("default:group::");
3352 case SMB_POSIX_ACL_GROUP
:
3353 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3354 d_printf("default:group:%u:", uorg
);
3356 case SMB_POSIX_ACL_MASK
:
3357 d_printf("default:mask::");
3359 case SMB_POSIX_ACL_OTHER
:
3360 d_printf("default:other::");
3363 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3364 src
, (unsigned int)tagtype
);
3369 d_printf("%s\n", perms_to_string(permstring
, perms
));
3375 static void printf_cb(const char *buf
, void *private_data
)
3380 /****************************************************************************
3381 Get the EA list of a file
3382 ****************************************************************************/
3384 static int cmd_geteas(void)
3386 TALLOC_CTX
*ctx
= talloc_tos();
3389 char *targetname
= NULL
;
3390 struct cli_state
*targetcli
;
3393 struct ea_struct
*eas
;
3395 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
3396 d_printf("geteas filename\n");
3399 src
= talloc_asprintf(ctx
,
3401 client_get_cur_dir(),
3407 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
,
3409 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
3413 status
= cli_get_ea_list_path(targetcli
, targetname
, talloc_tos(),
3415 if (!NT_STATUS_IS_OK(status
)) {
3416 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status
));
3420 for (i
=0; i
<num_eas
; i
++) {
3421 d_printf("%s (%d) =\n", eas
[i
].name
, (int)eas
[i
].flags
);
3422 dump_data_cb(eas
[i
].value
.data
, eas
[i
].value
.length
, false,
3432 /****************************************************************************
3434 ****************************************************************************/
3436 static int cmd_setea(void)
3438 TALLOC_CTX
*ctx
= talloc_tos();
3441 char *eaname
= NULL
;
3442 char *eavalue
= NULL
;
3443 char *targetname
= NULL
;
3444 struct cli_state
*targetcli
;
3447 if (!next_token_talloc(ctx
, &cmd_ptr
, &name
, NULL
)
3448 || !next_token_talloc(ctx
, &cmd_ptr
, &eaname
, NULL
)) {
3449 d_printf("setea filename eaname value\n");
3452 if (!next_token_talloc(ctx
, &cmd_ptr
, &eavalue
, NULL
)) {
3453 eavalue
= talloc_strdup(ctx
, "");
3455 src
= talloc_asprintf(ctx
,
3457 client_get_cur_dir(),
3463 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
,
3465 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
3469 status
= cli_set_ea_path(targetcli
, targetname
, eaname
, eavalue
,
3471 if (!NT_STATUS_IS_OK(status
)) {
3472 d_printf("set_ea %s: %s\n", src
, nt_errstr(status
));
3479 /****************************************************************************
3481 ****************************************************************************/
3483 static int cmd_stat(void)
3485 TALLOC_CTX
*ctx
= talloc_tos();
3488 char *targetname
= NULL
;
3489 struct cli_state
*targetcli
;
3491 SMB_STRUCT_STAT sbuf
;
3495 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
3496 d_printf("stat file\n");
3499 src
= talloc_asprintf(ctx
,
3501 client_get_cur_dir(),
3507 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
)) {
3508 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
3512 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3513 d_printf("Server doesn't support UNIX CIFS calls.\n");
3517 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli
, targetname
, &sbuf
))) {
3518 d_printf("%s stat file %s\n",
3519 cli_errstr(targetcli
), src
);
3523 /* Print out the stat values. */
3524 d_printf("File: %s\n", src
);
3525 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3526 (double)sbuf
.st_ex_size
,
3527 (unsigned int)sbuf
.st_ex_blocks
,
3528 filetype_to_str(sbuf
.st_ex_mode
));
3530 #if defined(S_ISCHR) && defined(S_ISBLK)
3531 if (S_ISCHR(sbuf
.st_ex_mode
) || S_ISBLK(sbuf
.st_ex_mode
)) {
3532 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3533 (double)sbuf
.st_ex_ino
,
3534 (unsigned int)sbuf
.st_ex_nlink
,
3535 unix_dev_major(sbuf
.st_ex_rdev
),
3536 unix_dev_minor(sbuf
.st_ex_rdev
));
3539 d_printf("Inode: %.0f\tLinks: %u\n",
3540 (double)sbuf
.st_ex_ino
,
3541 (unsigned int)sbuf
.st_ex_nlink
);
3543 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3544 ((int)sbuf
.st_ex_mode
& 0777),
3545 unix_mode_to_str(mode_str
, sbuf
.st_ex_mode
),
3546 (unsigned int)sbuf
.st_ex_uid
,
3547 (unsigned int)sbuf
.st_ex_gid
);
3549 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_atime
);
3550 lt
= localtime(&tmp_time
);
3552 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
3554 fstrcpy(mode_str
, "unknown");
3556 d_printf("Access: %s\n", mode_str
);
3558 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_mtime
);
3559 lt
= localtime(&tmp_time
);
3561 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
3563 fstrcpy(mode_str
, "unknown");
3565 d_printf("Modify: %s\n", mode_str
);
3567 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_ctime
);
3568 lt
= localtime(&tmp_time
);
3570 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
3572 fstrcpy(mode_str
, "unknown");
3574 d_printf("Change: %s\n", mode_str
);
3580 /****************************************************************************
3582 ****************************************************************************/
3584 static int cmd_chown(void)
3586 TALLOC_CTX
*ctx
= talloc_tos();
3590 char *buf
, *buf2
, *buf3
;
3591 struct cli_state
*targetcli
;
3592 char *targetname
= NULL
;
3594 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3595 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
) ||
3596 !next_token_talloc(ctx
, &cmd_ptr
,&buf3
,NULL
)) {
3597 d_printf("chown uid gid file\n");
3601 uid
= (uid_t
)atoi(buf
);
3602 gid
= (gid_t
)atoi(buf2
);
3604 src
= talloc_asprintf(ctx
,
3606 client_get_cur_dir(),
3611 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
) ) {
3612 d_printf("chown %s: %s\n", src
, cli_errstr(cli
));
3616 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3617 d_printf("Server doesn't support UNIX CIFS calls.\n");
3621 if (!NT_STATUS_IS_OK(cli_posix_chown(targetcli
, targetname
, uid
, gid
))) {
3622 d_printf("%s chown file %s uid=%d, gid=%d\n",
3623 cli_errstr(targetcli
), src
, (int)uid
, (int)gid
);
3630 /****************************************************************************
3632 ****************************************************************************/
3634 static int cmd_rename(void)
3636 TALLOC_CTX
*ctx
= talloc_tos();
3639 struct cli_state
*targetcli
;
3643 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3644 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3645 d_printf("rename <src> <dest>\n");
3649 src
= talloc_asprintf(ctx
,
3651 client_get_cur_dir(),
3657 dest
= talloc_asprintf(ctx
,
3659 client_get_cur_dir(),
3665 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetsrc
)) {
3666 d_printf("rename %s: %s\n", src
, cli_errstr(cli
));
3670 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, dest
, &targetcli
, &targetdest
)) {
3671 d_printf("rename %s: %s\n", dest
, cli_errstr(cli
));
3675 if (!NT_STATUS_IS_OK(cli_rename(targetcli
, targetsrc
, targetdest
))) {
3676 d_printf("%s renaming files %s -> %s \n",
3677 cli_errstr(targetcli
),
3686 /****************************************************************************
3687 Print the volume name.
3688 ****************************************************************************/
3690 static int cmd_volume(void)
3697 status
= cli_get_fs_volume_info(cli
, volname
, &serial_num
,
3699 if (!NT_STATUS_IS_OK(status
)) {
3700 d_printf("Error %s getting volume info\n", nt_errstr(status
));
3704 d_printf("Volume: |%s| serial number 0x%x\n",
3705 volname
, (unsigned int)serial_num
);
3709 /****************************************************************************
3710 Hard link files using the NT call.
3711 ****************************************************************************/
3713 static int cmd_hardlink(void)
3715 TALLOC_CTX
*ctx
= talloc_tos();
3718 struct cli_state
*targetcli
;
3721 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3722 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3723 d_printf("hardlink <src> <dest>\n");
3727 src
= talloc_asprintf(ctx
,
3729 client_get_cur_dir(),
3735 dest
= talloc_asprintf(ctx
,
3737 client_get_cur_dir(),
3743 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
)) {
3744 d_printf("hardlink %s: %s\n", src
, cli_errstr(cli
));
3748 if (!NT_STATUS_IS_OK(cli_nt_hardlink(targetcli
, targetname
, dest
))) {
3749 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli
));
3756 /****************************************************************************
3757 Toggle the prompt flag.
3758 ****************************************************************************/
3760 static int cmd_prompt(void)
3763 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
3767 /****************************************************************************
3768 Set the newer than time.
3769 ****************************************************************************/
3771 static int cmd_newer(void)
3773 TALLOC_CTX
*ctx
= talloc_tos();
3776 SMB_STRUCT_STAT sbuf
;
3778 ok
= next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
);
3779 if (ok
&& (sys_stat(buf
, &sbuf
, false) == 0)) {
3780 newer_than
= convert_timespec_to_time_t(sbuf
.st_ex_mtime
);
3781 DEBUG(1,("Getting files newer than %s",
3782 time_to_asc(newer_than
)));
3787 if (ok
&& newer_than
== 0) {
3788 d_printf("Error setting newer-than time\n");
3795 /****************************************************************************
3796 Set the archive level.
3797 ****************************************************************************/
3799 static int cmd_archive(void)
3801 TALLOC_CTX
*ctx
= talloc_tos();
3804 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3805 archive_level
= atoi(buf
);
3807 d_printf("Archive level is %d\n",archive_level
);
3813 /****************************************************************************
3814 Toggle the lowercaseflag.
3815 ****************************************************************************/
3817 static int cmd_lowercase(void)
3819 lowercase
= !lowercase
;
3820 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
3824 /****************************************************************************
3825 Toggle the case sensitive flag.
3826 ****************************************************************************/
3828 static int cmd_setcase(void)
3830 bool orig_case_sensitive
= cli_set_case_sensitive(cli
, false);
3832 cli_set_case_sensitive(cli
, !orig_case_sensitive
);
3833 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive
?
3838 /****************************************************************************
3839 Toggle the showacls flag.
3840 ****************************************************************************/
3842 static int cmd_showacls(void)
3844 showacls
= !showacls
;
3845 DEBUG(2,("showacls is now %s\n",showacls
?"on":"off"));
3850 /****************************************************************************
3851 Toggle the recurse flag.
3852 ****************************************************************************/
3854 static int cmd_recurse(void)
3857 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
3861 /****************************************************************************
3862 Toggle the translate flag.
3863 ****************************************************************************/
3865 static int cmd_translate(void)
3867 translation
= !translation
;
3868 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
3869 translation
?"on":"off"));
3873 /****************************************************************************
3875 ****************************************************************************/
3877 static int cmd_lcd(void)
3879 TALLOC_CTX
*ctx
= talloc_tos();
3883 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3884 if (chdir(buf
) == -1) {
3885 d_printf("chdir to %s failed (%s)\n",
3886 buf
, strerror(errno
));
3889 d
= TALLOC_ARRAY(ctx
, char, PATH_MAX
+1);
3893 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
3897 /****************************************************************************
3898 Get a file restarting at end of local file.
3899 ****************************************************************************/
3901 static int cmd_reget(void)
3903 TALLOC_CTX
*ctx
= talloc_tos();
3904 char *local_name
= NULL
;
3905 char *remote_name
= NULL
;
3909 remote_name
= talloc_strdup(ctx
, client_get_cur_dir());
3914 if (!next_token_talloc(ctx
, &cmd_ptr
, &fname
, NULL
)) {
3915 d_printf("reget <filename>\n");
3918 remote_name
= talloc_asprintf_append(remote_name
, "%s", fname
);
3922 remote_name
= clean_name(ctx
,remote_name
);
3928 next_token_talloc(ctx
, &cmd_ptr
, &p
, NULL
);
3933 return do_get(remote_name
, local_name
, true);
3936 /****************************************************************************
3937 Put a file restarting at end of local file.
3938 ****************************************************************************/
3940 static int cmd_reput(void)
3942 TALLOC_CTX
*ctx
= talloc_tos();
3943 char *local_name
= NULL
;
3944 char *remote_name
= NULL
;
3948 remote_name
= talloc_strdup(ctx
, client_get_cur_dir());
3953 if (!next_token_talloc(ctx
, &cmd_ptr
, &local_name
, NULL
)) {
3954 d_printf("reput <filename>\n");
3958 if (!file_exist_stat(local_name
, &st
, false)) {
3959 d_printf("%s does not exist\n", local_name
);
3963 if (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
3964 remote_name
= talloc_asprintf_append(remote_name
,
3967 remote_name
= talloc_asprintf_append(remote_name
,
3974 remote_name
= clean_name(ctx
, remote_name
);
3979 return do_put(remote_name
, local_name
, true);
3982 /****************************************************************************
3984 ****************************************************************************/
3986 static void browse_fn(const char *name
, uint32 m
,
3987 const char *comment
, void *state
)
3989 const char *typestr
= "";
3992 case STYPE_DISKTREE
:
3996 typestr
= "Printer";
4005 /* FIXME: If the remote machine returns non-ascii characters
4006 in any of these fields, they can corrupt the output. We
4007 should remove them. */
4009 d_printf("\t%-15s %-10.10s%s\n",
4010 name
,typestr
,comment
);
4012 d_printf ("%s|%s|%s\n",typestr
,name
,comment
);
4016 static bool browse_host_rpc(bool sort
)
4019 struct rpc_pipe_client
*pipe_hnd
= NULL
;
4020 TALLOC_CTX
*frame
= talloc_stackframe();
4022 struct srvsvc_NetShareInfoCtr info_ctr
;
4023 struct srvsvc_NetShareCtr1 ctr1
;
4024 uint32_t resume_handle
= 0;
4025 uint32_t total_entries
= 0;
4027 struct dcerpc_binding_handle
*b
;
4029 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
.syntax_id
,
4032 if (!NT_STATUS_IS_OK(status
)) {
4033 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4034 nt_errstr(status
)));
4039 b
= pipe_hnd
->binding_handle
;
4041 ZERO_STRUCT(info_ctr
);
4045 info_ctr
.ctr
.ctr1
= &ctr1
;
4047 status
= dcerpc_srvsvc_NetShareEnumAll(b
, frame
,
4055 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(werr
)) {
4056 TALLOC_FREE(pipe_hnd
);
4061 for (i
=0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
4062 struct srvsvc_NetShareInfo1 info
= info_ctr
.ctr
.ctr1
->array
[i
];
4063 browse_fn(info
.name
, info
.type
, info
.comment
, NULL
);
4066 TALLOC_FREE(pipe_hnd
);
4071 /****************************************************************************
4072 Try and browse available connections on a host.
4073 ****************************************************************************/
4075 static bool browse_host(bool sort
)
4079 d_printf("\n\tSharename Type Comment\n");
4080 d_printf("\t--------- ---- -------\n");
4083 if (browse_host_rpc(sort
)) {
4087 if((ret
= cli_RNetShareEnum(cli
, browse_fn
, NULL
)) == -1)
4088 d_printf("Error returning browse list: %s\n", cli_errstr(cli
));
4093 /****************************************************************************
4095 ****************************************************************************/
4097 static void server_fn(const char *name
, uint32 m
,
4098 const char *comment
, void *state
)
4102 d_printf("\t%-16s %s\n", name
, comment
);
4104 d_printf("%s|%s|%s\n",(char *)state
, name
, comment
);
4108 /****************************************************************************
4109 Try and browse available connections on a host.
4110 ****************************************************************************/
4112 static bool list_servers(const char *wk_grp
)
4116 if (!cli
->server_domain
)
4120 d_printf("\n\tServer Comment\n");
4121 d_printf("\t--------- -------\n");
4123 fstrcpy( state
, "Server" );
4124 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
,
4128 d_printf("\n\tWorkgroup Master\n");
4129 d_printf("\t--------- -------\n");
4132 fstrcpy( state
, "Workgroup" );
4133 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
,
4138 /****************************************************************************
4139 Print or set current VUID
4140 ****************************************************************************/
4142 static int cmd_vuid(void)
4144 TALLOC_CTX
*ctx
= talloc_tos();
4147 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4148 d_printf("Current VUID is %d\n", cli
->vuid
);
4152 cli
->vuid
= atoi(buf
);
4156 /****************************************************************************
4157 Setup a new VUID, by issuing a session setup
4158 ****************************************************************************/
4160 static int cmd_logon(void)
4162 TALLOC_CTX
*ctx
= talloc_tos();
4163 char *l_username
, *l_password
;
4166 if (!next_token_talloc(ctx
, &cmd_ptr
,&l_username
,NULL
)) {
4167 d_printf("logon <username> [<password>]\n");
4171 if (!next_token_talloc(ctx
, &cmd_ptr
,&l_password
,NULL
)) {
4172 char *pass
= getpass("Password: ");
4174 l_password
= talloc_strdup(ctx
,pass
);
4181 nt_status
= cli_session_setup(cli
, l_username
,
4182 l_password
, strlen(l_password
),
4183 l_password
, strlen(l_password
),
4185 if (!NT_STATUS_IS_OK(nt_status
)) {
4186 d_printf("session setup failed: %s\n", nt_errstr(nt_status
));
4190 d_printf("Current VUID is %d\n", cli
->vuid
);
4195 /****************************************************************************
4196 list active connections
4197 ****************************************************************************/
4199 static int cmd_list_connect(void)
4201 cli_cm_display(cli
);
4205 /****************************************************************************
4206 display the current active client connection
4207 ****************************************************************************/
4209 static int cmd_show_connect( void )
4211 TALLOC_CTX
*ctx
= talloc_tos();
4212 struct cli_state
*targetcli
;
4215 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, client_get_cur_dir(),
4216 &targetcli
, &targetpath
) ) {
4217 d_printf("showconnect %s: %s\n", cur_dir
, cli_errstr(cli
));
4221 d_printf("//%s/%s\n", targetcli
->desthost
, targetcli
->share
);
4225 /****************************************************************************
4227 ***************************************************************************/
4229 int cmd_iosize(void)
4231 TALLOC_CTX
*ctx
= talloc_tos();
4235 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4237 d_printf("iosize <n> or iosize 0x<n>. "
4238 "Minimum is 16384 (0x4000), "
4239 "max is 16776960 (0xFFFF00)\n");
4241 d_printf("iosize <n> or iosize 0x<n>. "
4242 "(Encrypted connection) ,"
4243 "Minimum is 16384 (0x4000), "
4244 "max is 130048 (0x1FC00)\n");
4249 iosize
= strtol(buf
,NULL
,0);
4250 if (smb_encrypt
&& (iosize
< 0x4000 || iosize
> 0xFC00)) {
4251 d_printf("iosize out of range for encrypted "
4252 "connection (min = 16384 (0x4000), "
4253 "max = 130048 (0x1FC00)");
4255 } else if (!smb_encrypt
&& (iosize
< 0x4000 || iosize
> 0xFFFF00)) {
4256 d_printf("iosize out of range (min = 16384 (0x4000), "
4257 "max = 16776960 (0xFFFF00)");
4261 io_bufsize
= iosize
;
4262 d_printf("iosize is now %d\n", io_bufsize
);
4266 /****************************************************************************
4268 ****************************************************************************/
4269 static int cmd_history(void)
4271 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4275 hlist
= history_list();
4277 for (i
= 0; hlist
&& hlist
[i
]; i
++) {
4278 DEBUG(0, ("%d: %s\n", i
, hlist
[i
]->line
));
4281 DEBUG(0,("no history without readline support\n"));
4287 /* Some constants for completing filename arguments */
4289 #define COMPL_NONE 0 /* No completions */
4290 #define COMPL_REMOTE 1 /* Complete remote filename */
4291 #define COMPL_LOCAL 2 /* Complete local filename */
4293 /* This defines the commands supported by this client.
4294 * NOTE: The "!" must be the last one in the list because it's fn pointer
4295 * field is NULL, and NULL in that field is used in process_tok()
4296 * (below) to indicate the end of the list. crh
4301 const char *description
;
4302 char compl_args
[2]; /* Completion argument info */
4304 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
4305 {"allinfo",cmd_allinfo
,"<file> show all available info",
4306 {COMPL_NONE
,COMPL_NONE
}},
4307 {"altname",cmd_altname
,"<file> show alt name",{COMPL_NONE
,COMPL_NONE
}},
4308 {"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
}},
4309 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
4310 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
4311 {"case_sensitive",cmd_setcase
,"toggle the case sensitive flag to server",{COMPL_NONE
,COMPL_NONE
}},
4312 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
4313 {"chmod",cmd_chmod
,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE
,COMPL_REMOTE
}},
4314 {"chown",cmd_chown
,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE
,COMPL_REMOTE
}},
4315 {"close",cmd_close
,"<fid> close a file given a fid",{COMPL_REMOTE
,COMPL_REMOTE
}},
4316 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4317 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
4318 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
4319 {"echo",cmd_echo
,"ping the server",{COMPL_NONE
,COMPL_NONE
}},
4320 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
4321 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
4322 {"getfacl",cmd_getfacl
,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE
,COMPL_LOCAL
}},
4323 {"geteas", cmd_geteas
, "<file name> get the EA list of a file",
4324 {COMPL_REMOTE
, COMPL_LOCAL
}},
4325 {"hardlink",cmd_hardlink
,"<src> <dest> create a Windows hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
4326 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
4327 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
4328 {"iosize",cmd_iosize
,"iosize <number> (default 64512)",{COMPL_NONE
,COMPL_NONE
}},
4329 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
4330 {"link",cmd_link
,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
4331 {"lock",cmd_lock
,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
4332 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
4333 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
4334 {"l",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
4335 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
4336 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
4337 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4338 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
4339 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
4340 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4341 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
4342 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
4343 {"posix", cmd_posix
, "turn on all POSIX capabilities", {COMPL_REMOTE
,COMPL_NONE
}},
4344 {"posix_encrypt",cmd_posix_encrypt
,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE
,COMPL_NONE
}},
4345 {"posix_open",cmd_posix_open
,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
4346 {"posix_mkdir",cmd_posix_mkdir
,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
4347 {"posix_rmdir",cmd_posix_rmdir
,"<name> removes a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
4348 {"posix_unlink",cmd_posix_unlink
,"<name> removes a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
4349 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
4350 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
4351 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
4352 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
4353 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
4354 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
4355 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
4356 {"readlink",cmd_readlink
,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
4357 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
4358 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
4359 {"reget",cmd_reget
,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE
,COMPL_LOCAL
}},
4360 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
4361 {"reput",cmd_reput
,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL
,COMPL_REMOTE
}},
4362 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4363 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
4364 {"showacls",cmd_showacls
,"toggle if ACLs are shown or not",{COMPL_NONE
,COMPL_NONE
}},
4365 {"setea", cmd_setea
, "<file name> <eaname> <eaval> Set an EA of a file",
4366 {COMPL_REMOTE
, COMPL_LOCAL
}},
4367 {"setmode",cmd_setmode
,"filename <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
4368 {"stat",cmd_stat
,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE
,COMPL_REMOTE
}},
4369 {"symlink",cmd_symlink
,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
4370 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
4371 {"tarmode",cmd_tarmode
,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
4372 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
4373 {"unlock",cmd_unlock
,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
4374 {"volume",cmd_volume
,"print the volume name",{COMPL_NONE
,COMPL_NONE
}},
4375 {"vuid",cmd_vuid
,"change current vuid",{COMPL_NONE
,COMPL_NONE
}},
4376 {"wdel",cmd_wdel
,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4377 {"logon",cmd_logon
,"establish new logon",{COMPL_NONE
,COMPL_NONE
}},
4378 {"listconnect",cmd_list_connect
,"list open connections",{COMPL_NONE
,COMPL_NONE
}},
4379 {"showconnect",cmd_show_connect
,"display the current active connection",{COMPL_NONE
,COMPL_NONE
}},
4380 {"..",cmd_cd_oneup
,"change the remote directory (up one level)",{COMPL_REMOTE
,COMPL_NONE
}},
4382 /* Yes, this must be here, see crh's comment above. */
4383 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
4384 {NULL
,NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
4387 /*******************************************************************
4388 Lookup a command string in the list of commands, including
4390 ******************************************************************/
4392 static int process_tok(char *tok
)
4394 int i
= 0, matches
= 0;
4396 int tok_len
= strlen(tok
);
4398 while (commands
[i
].fn
!= NULL
) {
4399 if (strequal(commands
[i
].name
,tok
)) {
4403 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
4412 else if (matches
== 1)
4418 /****************************************************************************
4420 ****************************************************************************/
4422 static int cmd_help(void)
4424 TALLOC_CTX
*ctx
= talloc_tos();
4428 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4429 if ((i
= process_tok(buf
)) >= 0)
4430 d_printf("HELP %s:\n\t%s\n\n",
4431 commands
[i
].name
,commands
[i
].description
);
4433 while (commands
[i
].description
) {
4434 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
4435 d_printf("%-15s",commands
[i
].name
);
4444 /****************************************************************************
4445 Process a -c command string.
4446 ****************************************************************************/
4448 static int process_command_string(const char *cmd_in
)
4450 TALLOC_CTX
*ctx
= talloc_tos();
4451 char *cmd
= talloc_strdup(ctx
, cmd_in
);
4457 /* establish the connection if not already */
4460 cli
= cli_cm_open(talloc_tos(), NULL
,
4461 have_ip
? dest_ss_str
: desthost
,
4464 max_protocol
, port
, name_type
);
4470 while (cmd
[0] != '\0') {
4476 if ((p
= strchr_m(cmd
, ';')) == 0) {
4485 /* and get the first part of the command */
4487 if (!next_token_talloc(ctx
, &cmd_ptr
,&tok
,NULL
)) {
4491 if ((i
= process_tok(tok
)) >= 0) {
4492 rc
= commands
[i
].fn();
4493 } else if (i
== -2) {
4494 d_printf("%s: command abbreviation ambiguous\n",tok
);
4496 d_printf("%s: command not found\n",tok
);
4503 #define MAX_COMPLETIONS 100
4505 struct completion_remote
{
4513 static NTSTATUS
completion_remote_filter(const char *mnt
,
4514 struct file_info
*f
,
4518 struct completion_remote
*info
= (struct completion_remote
*)state
;
4520 if (info
->count
>= MAX_COMPLETIONS
- 1) {
4521 return NT_STATUS_OK
;
4523 if (strncmp(info
->text
, f
->name
, info
->len
) != 0) {
4524 return NT_STATUS_OK
;
4526 if (ISDOT(f
->name
) || ISDOTDOT(f
->name
)) {
4527 return NT_STATUS_OK
;
4530 if ((info
->dirmask
[0] == 0) && !(f
->mode
& FILE_ATTRIBUTE_DIRECTORY
))
4531 info
->matches
[info
->count
] = SMB_STRDUP(f
->name
);
4533 TALLOC_CTX
*ctx
= talloc_stackframe();
4536 tmp
= talloc_strdup(ctx
,info
->dirmask
);
4539 return NT_STATUS_NO_MEMORY
;
4541 tmp
= talloc_asprintf_append(tmp
, "%s", f
->name
);
4544 return NT_STATUS_NO_MEMORY
;
4546 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
4547 tmp
= talloc_asprintf_append(tmp
, "%s",
4552 return NT_STATUS_NO_MEMORY
;
4554 info
->matches
[info
->count
] = SMB_STRDUP(tmp
);
4557 if (info
->matches
[info
->count
] == NULL
) {
4558 return NT_STATUS_OK
;
4560 if (f
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
4561 smb_readline_ca_char(0);
4563 if (info
->count
== 1) {
4564 info
->samelen
= strlen(info
->matches
[info
->count
]);
4566 while (strncmp(info
->matches
[info
->count
],
4567 info
->matches
[info
->count
-1],
4568 info
->samelen
) != 0) {
4573 return NT_STATUS_OK
;
4576 static char **remote_completion(const char *text
, int len
)
4578 TALLOC_CTX
*ctx
= talloc_stackframe();
4579 char *dirmask
= NULL
;
4580 char *targetpath
= NULL
;
4581 struct cli_state
*targetcli
= NULL
;
4583 struct completion_remote info
= { NULL
, NULL
, 1, 0, NULL
, 0 };
4586 /* can't have non-static initialisation on Sun CC, so do it
4592 info
.matches
= SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS
);
4593 if (!info
.matches
) {
4599 * We're leaving matches[0] free to fill it later with the text to
4600 * display: Either the one single match or the longest common subset
4603 info
.matches
[0] = NULL
;
4606 for (i
= len
-1; i
>= 0; i
--) {
4607 if ((text
[i
] == '/') || (text
[i
] == CLI_DIRSEP_CHAR
)) {
4612 info
.text
= text
+i
+1;
4613 info
.samelen
= info
.len
= len
-i
-1;
4616 info
.dirmask
= SMB_MALLOC_ARRAY(char, i
+2);
4617 if (!info
.dirmask
) {
4620 strncpy(info
.dirmask
, text
, i
+1);
4621 info
.dirmask
[i
+1] = 0;
4622 dirmask
= talloc_asprintf(ctx
,
4624 client_get_cur_dir(),
4628 info
.dirmask
= SMB_STRDUP("");
4629 if (!info
.dirmask
) {
4632 dirmask
= talloc_asprintf(ctx
,
4634 client_get_cur_dir());
4640 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, dirmask
, &targetcli
, &targetpath
)) {
4643 status
= cli_list(targetcli
, targetpath
, FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
4644 completion_remote_filter
, (void *)&info
);
4645 if (!NT_STATUS_IS_OK(status
)) {
4649 if (info
.count
== 1) {
4651 * No matches at all, NULL indicates there is nothing
4653 SAFE_FREE(info
.matches
[0]);
4654 SAFE_FREE(info
.matches
);
4659 if (info
.count
== 2) {
4661 * Exactly one match in matches[1], indicate this is the one
4664 info
.matches
[0] = info
.matches
[1];
4665 info
.matches
[1] = NULL
;
4668 return info
.matches
;
4672 * We got more than one possible match, set the result to the maximum
4676 info
.matches
[0] = SMB_STRNDUP(info
.matches
[1], info
.samelen
);
4677 info
.matches
[info
.count
] = NULL
;
4678 return info
.matches
;
4681 for (i
= 0; i
< info
.count
; i
++) {
4682 SAFE_FREE(info
.matches
[i
]);
4684 SAFE_FREE(info
.matches
);
4685 SAFE_FREE(info
.dirmask
);
4690 static char **completion_fn(const char *text
, int start
, int end
)
4692 smb_readline_ca_char(' ');
4695 const char *buf
, *sp
;
4699 buf
= smb_readline_get_line_buffer();
4703 sp
= strchr(buf
, ' ');
4707 for (i
= 0; commands
[i
].name
; i
++) {
4708 if ((strncmp(commands
[i
].name
, buf
, sp
- buf
) == 0) &&
4709 (commands
[i
].name
[sp
- buf
] == 0)) {
4713 if (commands
[i
].name
== NULL
)
4719 if (sp
== (buf
+ start
))
4720 compl_type
= commands
[i
].compl_args
[0];
4722 compl_type
= commands
[i
].compl_args
[1];
4724 if (compl_type
== COMPL_REMOTE
)
4725 return remote_completion(text
, end
- start
);
4726 else /* fall back to local filename completion */
4730 int i
, len
, samelen
= 0, count
=1;
4732 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
4739 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
4740 if (strncmp(text
, commands
[i
].name
, len
) == 0) {
4741 matches
[count
] = SMB_STRDUP(commands
[i
].name
);
4742 if (!matches
[count
])
4745 samelen
= strlen(matches
[count
]);
4747 while (strncmp(matches
[count
], matches
[count
-1], samelen
) != 0)
4754 case 0: /* should never happen */
4758 matches
[0] = SMB_STRDUP(matches
[1]);
4761 matches
[0] = (char *)SMB_MALLOC(samelen
+1);
4764 strncpy(matches
[0], matches
[1], samelen
);
4765 matches
[0][samelen
] = 0;
4767 matches
[count
] = NULL
;
4771 for (i
= 0; i
< count
; i
++)
4779 static bool finished
;
4781 /****************************************************************************
4782 Make sure we swallow keepalives during idle time.
4783 ****************************************************************************/
4785 static void readline_callback(void)
4787 static time_t last_t
;
4788 struct timespec now
;
4792 clock_gettime_mono(&now
);
4805 /* We deliberately use receive_smb_raw instead of
4806 client_receive_smb as we want to receive
4807 session keepalives and then drop them here.
4810 ret
= poll_intr_one_fd(cli
->fd
, POLLIN
|POLLHUP
, 0, &revents
);
4812 if ((ret
> 0) && (revents
& (POLLIN
|POLLHUP
|POLLERR
))) {
4816 set_smb_read_error(&cli
->smb_rw_error
, SMB_READ_OK
);
4818 status
= receive_smb_raw(cli
->fd
, cli
->inbuf
, cli
->bufsize
, 0, 0, &len
);
4820 if (!NT_STATUS_IS_OK(status
)) {
4821 DEBUG(0, ("Read from server failed, maybe it closed "
4822 "the connection\n"));
4825 smb_readline_done();
4826 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
4827 set_smb_read_error(&cli
->smb_rw_error
,
4832 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
4833 set_smb_read_error(&cli
->smb_rw_error
,
4838 set_smb_read_error(&cli
->smb_rw_error
, SMB_READ_ERROR
);
4841 if(CVAL(cli
->inbuf
,0) != SMBkeepalive
) {
4842 DEBUG(0, ("Read from server "
4843 "returned unexpected packet!\n"));
4850 /* Ping the server to keep the connection alive using SMBecho. */
4853 unsigned char garbage
[16];
4854 memset(garbage
, 0xf0, sizeof(garbage
));
4855 status
= cli_echo(cli
, 1, data_blob_const(garbage
, sizeof(garbage
)));
4857 if (NT_STATUS_IS_OK(status
)) {
4861 if (!cli_state_is_connected(cli
)) {
4862 DEBUG(0, ("SMBecho failed (%s). The connection is "
4863 "disconnected now\n", nt_errstr(status
)));
4865 smb_readline_done();
4870 /****************************************************************************
4871 Process commands on stdin.
4872 ****************************************************************************/
4874 static int process_stdin(void)
4879 TALLOC_CTX
*frame
= talloc_stackframe();
4881 char *the_prompt
= NULL
;
4885 /* display a prompt */
4886 if (asprintf(&the_prompt
, "smb: %s> ", client_get_cur_dir()) < 0) {
4890 line
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
4891 SAFE_FREE(the_prompt
);
4897 /* special case - first char is ! */
4899 if (system(line
+ 1) == -1) {
4900 d_printf("system() command %s failed.\n",
4908 /* and get the first part of the command */
4910 if (!next_token_talloc(frame
, &cmd_ptr
,&tok
,NULL
)) {
4916 if ((i
= process_tok(tok
)) >= 0) {
4917 rc
= commands
[i
].fn();
4918 } else if (i
== -2) {
4919 d_printf("%s: command abbreviation ambiguous\n",tok
);
4921 d_printf("%s: command not found\n",tok
);
4929 /****************************************************************************
4930 Process commands from the client.
4931 ****************************************************************************/
4933 static int process(const char *base_directory
)
4937 cli
= cli_cm_open(talloc_tos(), NULL
,
4938 have_ip
? dest_ss_str
: desthost
,
4939 service
, auth_info
, true, smb_encrypt
,
4940 max_protocol
, port
, name_type
);
4945 if (base_directory
&& *base_directory
) {
4946 rc
= do_cd(base_directory
);
4954 rc
= process_command_string(cmdstr
);
4963 /****************************************************************************
4965 ****************************************************************************/
4967 static int do_host_query(const char *query_host
)
4969 cli
= cli_cm_open(talloc_tos(), NULL
,
4970 have_ip
? dest_ss_str
: query_host
, "IPC$", auth_info
, true, smb_encrypt
,
4971 max_protocol
, port
, name_type
);
4977 /* Ensure that the host can do IPv4 */
4979 if (!interpret_addr(query_host
)) {
4980 struct sockaddr_storage ss
;
4981 if (interpret_string_addr(&ss
, query_host
, 0) &&
4982 (ss
.ss_family
!= AF_INET
)) {
4983 d_printf("%s is an IPv6 address -- no workgroup available\n",
4991 /* Workgroups simply don't make sense over anything
4992 else but port 139... */
4995 cli
= cli_cm_open(talloc_tos(), NULL
,
4996 have_ip
? dest_ss_str
: query_host
, "IPC$",
4997 auth_info
, true, smb_encrypt
,
4998 max_protocol
, 139, name_type
);
5002 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
5006 list_servers(lp_workgroup());
5013 /****************************************************************************
5014 Handle a tar operation.
5015 ****************************************************************************/
5017 static int do_tar_op(const char *base_directory
)
5021 /* do we already have a connection? */
5023 cli
= cli_cm_open(talloc_tos(), NULL
,
5024 have_ip
? dest_ss_str
: desthost
,
5025 service
, auth_info
, true, smb_encrypt
,
5026 max_protocol
, port
, name_type
);
5033 if (base_directory
&& *base_directory
) {
5034 ret
= do_cd(base_directory
);
5048 /****************************************************************************
5049 Handle a message operation.
5050 ****************************************************************************/
5052 static int do_message_op(struct user_auth_info
*a_info
)
5054 struct sockaddr_storage ss
;
5055 struct nmb_name called
, calling
;
5056 fstring server_name
;
5057 char name_type_hex
[10];
5061 make_nmb_name(&calling
, calling_name
, 0x0);
5062 make_nmb_name(&called
, desthost
, name_type
);
5064 fstrcpy(server_name
, desthost
);
5065 snprintf(name_type_hex
, sizeof(name_type_hex
), "#%X", name_type
);
5066 fstrcat(server_name
, name_type_hex
);
5072 /* we can only do messages over port 139 (to windows clients at least) */
5074 msg_port
= port
? port
: 139;
5076 if (!(cli
=cli_initialise())) {
5077 d_printf("Connection to %s failed\n", desthost
);
5080 cli_set_port(cli
, msg_port
);
5082 status
= cli_connect(cli
, server_name
, &ss
);
5083 if (!NT_STATUS_IS_OK(status
)) {
5084 d_printf("Connection to %s failed. Error %s\n", desthost
, nt_errstr(status
));
5088 if (!cli_session_request(cli
, &calling
, &called
)) {
5089 d_printf("session request failed\n");
5094 send_message(get_cmdline_auth_info_username(a_info
));
5100 /****************************************************************************
5102 ****************************************************************************/
5104 int main(int argc
,char *argv
[])
5106 char *base_directory
= NULL
;
5108 char *query_host
= NULL
;
5109 bool message
= false;
5110 static const char *new_name_resolve_order
= NULL
;
5114 fstring new_workgroup
;
5115 bool tar_opt
= false;
5116 bool service_opt
= false;
5117 struct poptOption long_options
[] = {
5120 { "name-resolve", 'R', POPT_ARG_STRING
, &new_name_resolve_order
, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
5121 { "message", 'M', POPT_ARG_STRING
, NULL
, 'M', "Send message", "HOST" },
5122 { "ip-address", 'I', POPT_ARG_STRING
, NULL
, 'I', "Use this IP to connect to", "IP" },
5123 { "stderr", 'E', POPT_ARG_NONE
, NULL
, 'E', "Write messages to stderr instead of stdout" },
5124 { "list", 'L', POPT_ARG_STRING
, NULL
, 'L', "Get a list of shares available on a host", "HOST" },
5125 { "max-protocol", 'm', POPT_ARG_STRING
, NULL
, 'm', "Set the max protocol level", "LEVEL" },
5126 { "tar", 'T', POPT_ARG_STRING
, NULL
, 'T', "Command line tar", "<c|x>IXFqgbNan" },
5127 { "directory", 'D', POPT_ARG_STRING
, NULL
, 'D', "Start from directory", "DIR" },
5128 { "command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated commands" },
5129 { "send-buffer", 'b', POPT_ARG_INT
, &io_bufsize
, 'b', "Changes the transmit/send buffer", "BYTES" },
5130 { "port", 'p', POPT_ARG_INT
, &port
, 'p', "Port to connect to", "PORT" },
5131 { "grepable", 'g', POPT_ARG_NONE
, NULL
, 'g', "Produce grepable output" },
5132 { "browse", 'B', POPT_ARG_NONE
, NULL
, 'B', "Browse SMB servers using DNS" },
5134 POPT_COMMON_CONNECTION
5135 POPT_COMMON_CREDENTIALS
5138 TALLOC_CTX
*frame
= talloc_stackframe();
5140 if (!client_set_cur_dir("\\")) {
5144 /* initialize the workgroup name so we can determine whether or
5145 not it was set by a command line option */
5147 set_global_myworkgroup( "" );
5148 set_global_myname( "" );
5150 /* set default debug level to 1 regardless of what smb.conf sets */
5151 setup_logging( "smbclient", DEBUG_DEFAULT_STDERR
);
5154 lp_set_cmdline("log level", "1");
5156 auth_info
= user_auth_info_init(frame
);
5157 if (auth_info
== NULL
) {
5160 popt_common_set_auth_info(auth_info
);
5163 pc
= poptGetContext("smbclient", argc
, (const char **) argv
, long_options
, 0);
5164 poptSetOtherOptionHelp(pc
, "service <password>");
5166 lp_set_in_client(true); /* Make sure that we tell lp_load we are */
5168 while ((opt
= poptGetNextOpt(pc
)) != -1) {
5170 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
5171 /* I see no other way to keep things sane --SSS */
5172 if (tar_opt
== true) {
5173 while (poptPeekArg(pc
)) {
5179 /* if the service has not yet been specified lets see if it is available in the popt stack */
5180 if (!service_opt
&& poptPeekArg(pc
)) {
5181 service
= talloc_strdup(frame
, poptGetArg(pc
));
5188 /* if the service has already been retrieved then check if we have also a password */
5190 && (!get_cmdline_auth_info_got_pass(auth_info
))
5191 && poptPeekArg(pc
)) {
5192 set_cmdline_auth_info_password(auth_info
,
5198 /* Messages are sent to NetBIOS name type 0x3
5199 * (Messenger Service). Make sure we default
5200 * to port 139 instead of port 445. srl,crh
5203 desthost
= talloc_strdup(frame
,poptGetOptArg(pc
));
5213 if (!interpret_string_addr(&dest_ss
, poptGetOptArg(pc
), 0)) {
5217 print_sockaddr(dest_ss_str
, sizeof(dest_ss_str
), &dest_ss
);
5221 setup_logging("smbclient", DEBUG_STDERR
);
5222 display_set_stderr();
5226 query_host
= talloc_strdup(frame
, poptGetOptArg(pc
));
5232 max_protocol
= interpret_protocol(poptGetOptArg(pc
), max_protocol
);
5235 /* We must use old option processing for this. Find the
5236 * position of the -T option in the raw argv[]. */
5239 for (i
= 1; i
< argc
; i
++) {
5240 if (strncmp("-T", argv
[i
],2)==0)
5244 if (!tar_parseargs(argc
, argv
, poptGetOptArg(pc
), i
)) {
5245 poptPrintUsage(pc
, stderr
, 0);
5249 /* this must be the last option, mark we have parsed it so that we know we have */
5253 base_directory
= talloc_strdup(frame
, poptGetOptArg(pc
));
5254 if (!base_directory
) {
5265 return(do_smb_browse());
5270 /* We may still have some leftovers after the last popt option has been called */
5271 if (tar_opt
== true) {
5272 while (poptPeekArg(pc
)) {
5278 /* if the service has not yet been specified lets see if it is available in the popt stack */
5279 if (!service_opt
&& poptPeekArg(pc
)) {
5280 service
= talloc_strdup(frame
,poptGetArg(pc
));
5287 /* if the service has already been retrieved then check if we have also a password */
5289 && !get_cmdline_auth_info_got_pass(auth_info
)
5290 && poptPeekArg(pc
)) {
5291 set_cmdline_auth_info_password(auth_info
,
5295 /* save the workgroup...
5297 FIXME!! do we need to do this for other options as well
5298 (or maybe a generic way to keep lp_load() from overwriting
5301 fstrcpy( new_workgroup
, lp_workgroup() );
5302 calling_name
= talloc_strdup(frame
, global_myname() );
5303 if (!calling_name
) {
5307 if ( override_logfile
)
5308 setup_logging( lp_logfile(), DEBUG_FILE
);
5310 if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
5311 fprintf(stderr
, "%s: Can't load %s - run testparm to debug it\n",
5312 argv
[0], get_dyn_CONFIGFILE());
5315 if (get_cmdline_auth_info_use_machine_account(auth_info
) &&
5316 !set_cmdline_auth_info_machine_account_creds(auth_info
)) {
5322 if (service_opt
&& service
) {
5325 /* Convert any '/' characters in the service name to '\' characters */
5326 string_replace(service
, '/','\\');
5327 if (count_chars(service
,'\\') < 3) {
5328 d_printf("\n%s: Not enough '\\' characters in service\n",service
);
5329 poptPrintUsage(pc
, stderr
, 0);
5332 /* Remove trailing slashes */
5333 len
= strlen(service
);
5334 while(len
> 0 && service
[len
- 1] == '\\') {
5336 service
[len
] = '\0';
5340 if ( strlen(new_workgroup
) != 0 ) {
5341 set_global_myworkgroup( new_workgroup
);
5344 if ( strlen(calling_name
) != 0 ) {
5345 set_global_myname( calling_name
);
5347 TALLOC_FREE(calling_name
);
5348 calling_name
= talloc_strdup(frame
, global_myname() );
5351 smb_encrypt
= get_cmdline_auth_info_smb_encrypt(auth_info
);
5352 if (!init_names()) {
5353 fprintf(stderr
, "init_names() failed\n");
5357 if(new_name_resolve_order
)
5358 lp_set_name_resolve_order(new_name_resolve_order
);
5360 if (!tar_type
&& !query_host
&& !service
&& !message
) {
5361 poptPrintUsage(pc
, stderr
, 0);
5365 poptFreeContext(pc
);
5367 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5369 /* Ensure we have a password (or equivalent). */
5370 set_cmdline_auth_info_getpass(auth_info
);
5374 process_command_string(cmdstr
);
5375 return do_tar_op(base_directory
);
5378 if (query_host
&& *query_host
) {
5379 char *qhost
= query_host
;
5382 while (*qhost
== '\\' || *qhost
== '/')
5385 if ((slash
= strchr_m(qhost
, '/'))
5386 || (slash
= strchr_m(qhost
, '\\'))) {
5390 if ((p
=strchr_m(qhost
, '#'))) {
5393 sscanf(p
, "%x", &name_type
);
5396 return do_host_query(qhost
);
5400 return do_message_op(auth_info
);
5403 if (process(base_directory
)) {