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 "popt_common.h"
26 #include "client/client_proto.h"
27 #include "../librpc/gen_ndr/cli_srvsvc.h"
28 #include "../lib/util/select.h"
29 #include "system/readline.h"
30 #include "../libcli/smbreadline/smbreadline.h"
36 extern int do_smb_browse(void); /* mDNS browsing */
38 extern bool AllowDebugChange
;
39 extern bool override_logfile
;
44 static char *desthost
;
45 static char *calling_name
;
46 static bool grepable
= false;
47 static char *cmdstr
= NULL
;
48 const char *cmd_ptr
= NULL
;
50 static int io_bufsize
= 524288;
52 static int name_type
= 0x20;
53 static int max_protocol
= PROTOCOL_NT1
;
55 static int process_tok(char *tok
);
56 static int cmd_help(void);
58 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
60 /* 30 second timeout on most commands */
61 #define CLIENT_TIMEOUT (30*1000)
62 #define SHORT_TIMEOUT (5*1000)
64 /* value for unused fid field in trans2 secondary request */
65 #define FID_UNUSED (0xFFFF)
67 time_t newer_than
= 0;
68 static int archive_level
= 0;
70 static bool translation
= false;
73 /* clitar bits insert */
76 extern bool tar_reset
;
79 static bool prompt
= true;
81 static bool recurse
= false;
82 static bool showacls
= false;
83 bool lowercase
= false;
85 static struct sockaddr_storage dest_ss
;
86 static char dest_ss_str
[INET6_ADDRSTRLEN
];
88 #define SEPARATORS " \t\n\r"
90 static bool abort_mget
= true;
93 uint64_t get_total_size
= 0;
94 unsigned int get_total_time_ms
= 0;
95 static uint64_t put_total_size
= 0;
96 static unsigned int put_total_time_ms
= 0;
99 static double dir_total
;
101 /* encrypted state. */
102 static bool smb_encrypt
;
104 /* root cli_state connection */
106 struct cli_state
*cli
;
108 static char CLI_DIRSEP_CHAR
= '\\';
109 static char CLI_DIRSEP_STR
[] = { '\\', '\0' };
111 /* Authentication for client connections. */
112 struct user_auth_info
*auth_info
;
114 /* Accessor functions for directory paths. */
115 static char *fileselection
;
116 static const char *client_get_fileselection(void)
119 return fileselection
;
124 static const char *client_set_fileselection(const char *new_fs
)
126 SAFE_FREE(fileselection
);
128 fileselection
= SMB_STRDUP(new_fs
);
130 return client_get_fileselection();
134 static const char *client_get_cwd(void)
139 return CLI_DIRSEP_STR
;
142 static const char *client_set_cwd(const char *new_cwd
)
146 cwd
= SMB_STRDUP(new_cwd
);
148 return client_get_cwd();
151 static char *cur_dir
;
152 const char *client_get_cur_dir(void)
157 return CLI_DIRSEP_STR
;
160 const char *client_set_cur_dir(const char *newdir
)
164 cur_dir
= SMB_STRDUP(newdir
);
166 return client_get_cur_dir();
169 /****************************************************************************
170 Put up a yes/no prompt.
171 ****************************************************************************/
173 static bool yesno(const char *p
)
178 if (!fgets(ans
,sizeof(ans
)-1,stdin
))
181 if (*ans
== 'y' || *ans
== 'Y')
187 /****************************************************************************
188 Write to a local file with CR/LF->LF translation if appropriate. Return the
189 number taken from the buffer. This may not equal the number written.
190 ****************************************************************************/
192 static int writefile(int f
, char *b
, int n
)
202 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
205 if (write(f
, b
, 1) != 1) {
215 /****************************************************************************
216 Read from a file with LF->CR/LF translation if appropriate. Return the
217 number read. read approx n bytes.
218 ****************************************************************************/
220 static int readfile(uint8_t *b
, int n
, XFILE
*f
)
226 return x_fread(b
,1,n
,f
);
229 while (i
< (n
- 1) && (i
< BUFFER_SIZE
)) {
230 if ((c
= x_getc(f
)) == EOF
) {
234 if (c
== '\n') { /* change all LFs to CR/LF */
249 static size_t push_source(uint8_t *buf
, size_t n
, void *priv
)
251 struct push_state
*state
= (struct push_state
*)priv
;
254 if (x_feof(state
->f
)) {
258 result
= readfile(buf
, n
, state
->f
);
259 state
->nread
+= result
;
263 /****************************************************************************
265 ****************************************************************************/
267 static void send_message(const char *username
)
273 d_printf("Type your message, ending it with a Control-D\n");
276 while (i
<sizeof(buf
)-2) {
277 int c
= fgetc(stdin
);
288 status
= cli_message(cli
, desthost
, username
, buf
);
289 if (!NT_STATUS_IS_OK(status
)) {
290 d_fprintf(stderr
, "cli_message returned %s\n",
295 /****************************************************************************
296 Check the space on a device.
297 ****************************************************************************/
299 static int do_dskattr(void)
301 int total
, bsize
, avail
;
302 struct cli_state
*targetcli
= NULL
;
303 char *targetpath
= NULL
;
304 TALLOC_CTX
*ctx
= talloc_tos();
307 if ( !cli_resolve_path(ctx
, "", auth_info
, cli
, client_get_cur_dir(), &targetcli
, &targetpath
)) {
308 d_printf("Error in dskattr: %s\n", cli_errstr(cli
));
312 status
= cli_dskattr(targetcli
, &bsize
, &total
, &avail
);
313 if (!NT_STATUS_IS_OK(status
)) {
314 d_printf("Error in dskattr: %s\n", nt_errstr(status
));
318 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
319 total
, bsize
, avail
);
324 /****************************************************************************
326 ****************************************************************************/
328 static int cmd_pwd(void)
330 d_printf("Current directory is %s",service
);
331 d_printf("%s\n",client_get_cur_dir());
335 /****************************************************************************
336 Ensure name has correct directory separators.
337 ****************************************************************************/
339 static void normalize_name(char *newdir
)
341 if (!(cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
)) {
342 string_replace(newdir
,'/','\\');
346 /****************************************************************************
347 Change directory - inner section.
348 ****************************************************************************/
350 static int do_cd(const char *new_dir
)
353 char *saved_dir
= NULL
;
355 char *targetpath
= NULL
;
356 struct cli_state
*targetcli
= NULL
;
357 SMB_STRUCT_STAT sbuf
;
360 TALLOC_CTX
*ctx
= talloc_stackframe();
362 newdir
= talloc_strdup(ctx
, new_dir
);
368 normalize_name(newdir
);
370 /* Save the current directory in case the new directory is invalid */
372 saved_dir
= talloc_strdup(ctx
, client_get_cur_dir());
378 if (*newdir
== CLI_DIRSEP_CHAR
) {
379 client_set_cur_dir(newdir
);
382 new_cd
= talloc_asprintf(ctx
, "%s%s",
383 client_get_cur_dir(),
390 /* Ensure cur_dir ends in a DIRSEP */
391 if ((new_cd
[0] != '\0') && (*(new_cd
+strlen(new_cd
)-1) != CLI_DIRSEP_CHAR
)) {
392 new_cd
= talloc_asprintf_append(new_cd
, "%s", CLI_DIRSEP_STR
);
397 client_set_cur_dir(new_cd
);
399 new_cd
= clean_name(ctx
, new_cd
);
400 client_set_cur_dir(new_cd
);
402 if ( !cli_resolve_path(ctx
, "", auth_info
, cli
, new_cd
, &targetcli
, &targetpath
)) {
403 d_printf("cd %s: %s\n", new_cd
, cli_errstr(cli
));
404 client_set_cur_dir(saved_dir
);
408 if (strequal(targetpath
,CLI_DIRSEP_STR
)) {
413 /* Use a trans2_qpathinfo to test directories for modern servers.
414 Except Win9x doesn't support the qpathinfo_basic() call..... */
416 if (targetcli
->protocol
> PROTOCOL_LANMAN2
&& !targetcli
->win95
) {
419 status
= cli_qpathinfo_basic(targetcli
, targetpath
, &sbuf
,
421 if (!NT_STATUS_IS_OK(status
)) {
422 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
423 client_set_cur_dir(saved_dir
);
427 if (!(attributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
428 d_printf("cd %s: not a directory\n", new_cd
);
429 client_set_cur_dir(saved_dir
);
435 targetpath
= talloc_asprintf(ctx
,
440 client_set_cur_dir(saved_dir
);
443 targetpath
= clean_name(ctx
, targetpath
);
445 client_set_cur_dir(saved_dir
);
449 status
= cli_chkpath(targetcli
, targetpath
);
450 if (!NT_STATUS_IS_OK(status
)) {
451 d_printf("cd %s: %s\n", new_cd
, nt_errstr(status
));
452 client_set_cur_dir(saved_dir
);
465 /****************************************************************************
467 ****************************************************************************/
469 static int cmd_cd(void)
474 if (next_token_talloc(talloc_tos(), &cmd_ptr
, &buf
,NULL
)) {
477 d_printf("Current directory is %s\n",client_get_cur_dir());
483 /****************************************************************************
485 ****************************************************************************/
487 static int cmd_cd_oneup(void)
492 /*******************************************************************
493 Decide if a file should be operated on.
494 ********************************************************************/
496 static bool do_this_one(struct file_info
*finfo
)
502 if (finfo
->mode
& aDIR
) {
506 if (*client_get_fileselection() &&
507 !mask_match(finfo
->name
,client_get_fileselection(),false)) {
508 DEBUG(3,("mask_match %s failed\n", finfo
->name
));
512 if (newer_than
&& finfo
->mtime_ts
.tv_sec
< newer_than
) {
513 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
517 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& aARCH
)) {
518 DEBUG(3,("archive %s failed\n", finfo
->name
));
525 /****************************************************************************
526 Display info about a file.
527 ****************************************************************************/
529 static void display_finfo(struct cli_state
*cli_state
, struct file_info
*finfo
,
533 TALLOC_CTX
*ctx
= talloc_tos();
535 if (!do_this_one(finfo
)) {
539 t
= finfo
->mtime_ts
.tv_sec
; /* the time is assumed to be passed as GMT */
541 d_printf(" %-30s%7.7s %8.0f %s",
543 attrib_string(finfo
->mode
),
546 dir_total
+= finfo
->size
;
552 /* skip if this is . or .. */
553 if ( strequal(finfo
->name
,"..") || strequal(finfo
->name
,".") )
555 /* create absolute filename for cli_ntcreate() FIXME */
556 afname
= talloc_asprintf(ctx
,
564 /* print file meta date header */
565 d_printf( "FILENAME:%s\n", finfo
->name
);
566 d_printf( "MODE:%s\n", attrib_string(finfo
->mode
));
567 d_printf( "SIZE:%.0f\n", (double)finfo
->size
);
568 d_printf( "MTIME:%s", time_to_asc(t
));
569 status
= cli_ntcreate(cli_state
, afname
, 0,
570 CREATE_ACCESS_READ
, 0,
571 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
572 FILE_OPEN
, 0x0, 0x0, &fnum
);
573 if (!NT_STATUS_IS_OK(status
)) {
574 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
575 afname
, nt_errstr(status
)));
577 struct security_descriptor
*sd
= NULL
;
578 sd
= cli_query_secdesc(cli_state
, fnum
, ctx
);
580 DEBUG( 0, ("display_finfo() failed to "
581 "get security descriptor: %s",
582 cli_errstr(cli_state
)));
584 display_sec_desc(sd
);
592 /****************************************************************************
593 Accumulate size of a file.
594 ****************************************************************************/
596 static void do_du(struct cli_state
*cli_state
, struct file_info
*finfo
,
599 if (do_this_one(finfo
)) {
600 dir_total
+= finfo
->size
;
604 static bool do_list_recurse
;
605 static bool do_list_dirs
;
606 static char *do_list_queue
= 0;
607 static long do_list_queue_size
= 0;
608 static long do_list_queue_start
= 0;
609 static long do_list_queue_end
= 0;
610 static void (*do_list_fn
)(struct cli_state
*cli_state
, struct file_info
*,
613 /****************************************************************************
614 Functions for do_list_queue.
615 ****************************************************************************/
618 * The do_list_queue is a NUL-separated list of strings stored in a
619 * char*. Since this is a FIFO, we keep track of the beginning and
620 * ending locations of the data in the queue. When we overflow, we
621 * double the size of the char*. When the start of the data passes
622 * the midpoint, we move everything back. This is logically more
623 * complex than a linked list, but easier from a memory management
624 * angle. In any memory error condition, do_list_queue is reset.
625 * Functions check to ensure that do_list_queue is non-NULL before
629 static void reset_do_list_queue(void)
631 SAFE_FREE(do_list_queue
);
632 do_list_queue_size
= 0;
633 do_list_queue_start
= 0;
634 do_list_queue_end
= 0;
637 static void init_do_list_queue(void)
639 reset_do_list_queue();
640 do_list_queue_size
= 1024;
641 do_list_queue
= (char *)SMB_MALLOC(do_list_queue_size
);
642 if (do_list_queue
== 0) {
643 d_printf("malloc fail for size %d\n",
644 (int)do_list_queue_size
);
645 reset_do_list_queue();
647 memset(do_list_queue
, 0, do_list_queue_size
);
651 static void adjust_do_list_queue(void)
654 * If the starting point of the queue is more than half way through,
655 * move everything toward the beginning.
658 if (do_list_queue
== NULL
) {
659 DEBUG(4,("do_list_queue is empty\n"));
660 do_list_queue_start
= do_list_queue_end
= 0;
664 if (do_list_queue_start
== do_list_queue_end
) {
665 DEBUG(4,("do_list_queue is empty\n"));
666 do_list_queue_start
= do_list_queue_end
= 0;
667 *do_list_queue
= '\0';
668 } else if (do_list_queue_start
> (do_list_queue_size
/ 2)) {
669 DEBUG(4,("sliding do_list_queue backward\n"));
670 memmove(do_list_queue
,
671 do_list_queue
+ do_list_queue_start
,
672 do_list_queue_end
- do_list_queue_start
);
673 do_list_queue_end
-= do_list_queue_start
;
674 do_list_queue_start
= 0;
678 static void add_to_do_list_queue(const char *entry
)
680 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
681 while (new_end
> do_list_queue_size
) {
682 do_list_queue_size
*= 2;
683 DEBUG(4,("enlarging do_list_queue to %d\n",
684 (int)do_list_queue_size
));
685 do_list_queue
= (char *)SMB_REALLOC(do_list_queue
, do_list_queue_size
);
686 if (! do_list_queue
) {
687 d_printf("failure enlarging do_list_queue to %d bytes\n",
688 (int)do_list_queue_size
);
689 reset_do_list_queue();
691 memset(do_list_queue
+ do_list_queue_size
/ 2,
692 0, do_list_queue_size
/ 2);
696 safe_strcpy_base(do_list_queue
+ do_list_queue_end
,
697 entry
, do_list_queue
, do_list_queue_size
);
698 do_list_queue_end
= new_end
;
699 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
700 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
704 static char *do_list_queue_head(void)
706 return do_list_queue
+ do_list_queue_start
;
709 static void remove_do_list_queue_head(void)
711 if (do_list_queue_end
> do_list_queue_start
) {
712 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
713 adjust_do_list_queue();
714 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
715 (int)do_list_queue_start
, (int)do_list_queue_end
));
719 static int do_list_queue_empty(void)
721 return (! (do_list_queue
&& *do_list_queue
));
724 /****************************************************************************
725 A helper for do_list.
726 ****************************************************************************/
728 static void do_list_helper(const char *mntpoint
, struct file_info
*f
,
729 const char *mask
, void *state
)
731 struct cli_state
*cli_state
= (struct cli_state
*)state
;
732 TALLOC_CTX
*ctx
= talloc_tos();
734 char *dir_end
= NULL
;
736 /* Work out the directory. */
737 dir
= talloc_strdup(ctx
, mask
);
741 if ((dir_end
= strrchr(dir
, CLI_DIRSEP_CHAR
)) != NULL
) {
745 if (f
->mode
& aDIR
) {
746 if (do_list_dirs
&& do_this_one(f
)) {
747 do_list_fn(cli_state
, f
, dir
);
749 if (do_list_recurse
&&
751 !strequal(f
->name
,".") &&
752 !strequal(f
->name
,"..")) {
757 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
762 mask2
= talloc_asprintf(ctx
,
770 p
= strrchr_m(mask2
,CLI_DIRSEP_CHAR
);
776 mask2
= talloc_asprintf_append(mask2
,
784 add_to_do_list_queue(mask2
);
791 if (do_this_one(f
)) {
792 do_list_fn(cli_state
, f
, dir
);
797 /****************************************************************************
798 A wrapper around cli_list that adds recursion.
799 ****************************************************************************/
801 void do_list(const char *mask
,
803 void (*fn
)(struct cli_state
*cli_state
, struct file_info
*,
808 static int in_do_list
= 0;
809 TALLOC_CTX
*ctx
= talloc_tos();
810 struct cli_state
*targetcli
= NULL
;
811 char *targetpath
= NULL
;
813 if (in_do_list
&& rec
) {
814 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
820 do_list_recurse
= rec
;
825 init_do_list_queue();
826 add_to_do_list_queue(mask
);
828 while (!do_list_queue_empty()) {
830 * Need to copy head so that it doesn't become
831 * invalid inside the call to cli_list. This
832 * would happen if the list were expanded
834 * Fix from E. Jay Berkenbilt (ejb@ql.org)
836 char *head
= talloc_strdup(ctx
, do_list_queue_head());
844 if ( !cli_resolve_path(ctx
, "", auth_info
, cli
, head
, &targetcli
, &targetpath
) ) {
845 d_printf("do_list: [%s] %s\n", head
, cli_errstr(cli
));
846 remove_do_list_queue_head();
850 cli_list(targetcli
, targetpath
, attribute
,
851 do_list_helper
, targetcli
);
852 remove_do_list_queue_head();
853 if ((! do_list_queue_empty()) && (fn
== display_finfo
)) {
854 char *next_file
= do_list_queue_head();
856 if ((strlen(next_file
) >= 2) &&
857 (next_file
[strlen(next_file
) - 1] == '*') &&
858 (next_file
[strlen(next_file
) - 2] == CLI_DIRSEP_CHAR
)) {
859 save_ch
= next_file
+
860 strlen(next_file
) - 2;
863 /* cwd is only used if showacls is on */
864 client_set_cwd(next_file
);
867 if (!showacls
) /* don't disturbe the showacls output */
868 d_printf("\n%s\n",next_file
);
870 *save_ch
= CLI_DIRSEP_CHAR
;
874 TALLOC_FREE(targetpath
);
878 if (cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetpath
)) {
881 status
= cli_list(targetcli
, targetpath
, attribute
,
882 do_list_helper
, targetcli
);
883 if (!NT_STATUS_IS_OK(status
)) {
884 d_printf("%s listing %s\n",
885 nt_errstr(status
), targetpath
);
887 TALLOC_FREE(targetpath
);
889 d_printf("do_list: [%s] %s\n", mask
, cli_errstr(cli
));
894 reset_do_list_queue();
897 /****************************************************************************
898 Get a directory listing.
899 ****************************************************************************/
901 static int cmd_dir(void)
903 TALLOC_CTX
*ctx
= talloc_tos();
904 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
910 mask
= talloc_strdup(ctx
, client_get_cur_dir());
915 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
917 if (*buf
== CLI_DIRSEP_CHAR
) {
918 mask
= talloc_strdup(ctx
, buf
);
920 mask
= talloc_asprintf_append(mask
, "%s", buf
);
923 mask
= talloc_asprintf_append(mask
, "*");
930 /* cwd is only used if showacls is on */
931 client_set_cwd(client_get_cur_dir());
934 do_list(mask
, attribute
, display_finfo
, recurse
, true);
938 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
943 /****************************************************************************
944 Get a directory listing.
945 ****************************************************************************/
947 static int cmd_du(void)
949 TALLOC_CTX
*ctx
= talloc_tos();
950 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
956 mask
= talloc_strdup(ctx
, client_get_cur_dir());
960 if ((mask
[0] != '\0') && (mask
[strlen(mask
)-1]!=CLI_DIRSEP_CHAR
)) {
961 mask
= talloc_asprintf_append(mask
, "%s", CLI_DIRSEP_STR
);
967 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
969 if (*buf
== CLI_DIRSEP_CHAR
) {
970 mask
= talloc_strdup(ctx
, buf
);
972 mask
= talloc_asprintf_append(mask
, "%s", buf
);
975 mask
= talloc_strdup(ctx
, "*");
978 do_list(mask
, attribute
, do_du
, recurse
, true);
982 d_printf("Total number of bytes: %.0f\n", dir_total
);
987 static int cmd_echo(void)
989 TALLOC_CTX
*ctx
= talloc_tos();
994 if (!next_token_talloc(ctx
, &cmd_ptr
, &num
, NULL
)
995 || !next_token_talloc(ctx
, &cmd_ptr
, &data
, NULL
)) {
996 d_printf("echo <num> <data>\n");
1000 status
= cli_echo(cli
, atoi(num
), data_blob_const(data
, strlen(data
)));
1002 if (!NT_STATUS_IS_OK(status
)) {
1003 d_printf("echo failed: %s\n", nt_errstr(status
));
1010 /****************************************************************************
1011 Get a file from rname to lname
1012 ****************************************************************************/
1014 static NTSTATUS
writefile_sink(char *buf
, size_t n
, void *priv
)
1016 int *pfd
= (int *)priv
;
1017 if (writefile(*pfd
, buf
, n
) == -1) {
1018 return map_nt_error_from_unix(errno
);
1020 return NT_STATUS_OK
;
1023 static int do_get(const char *rname
, const char *lname_in
, bool reget
)
1025 TALLOC_CTX
*ctx
= talloc_tos();
1028 bool newhandle
= false;
1029 struct timespec tp_start
;
1033 SMB_OFF_T nread
= 0;
1035 struct cli_state
*targetcli
= NULL
;
1036 char *targetname
= NULL
;
1040 lname
= talloc_strdup(ctx
, lname_in
);
1049 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, rname
, &targetcli
, &targetname
) ) {
1050 d_printf("Failed to open %s: %s\n", rname
, cli_errstr(cli
));
1054 clock_gettime_mono(&tp_start
);
1056 status
= cli_open(targetcli
, targetname
, O_RDONLY
, DENY_NONE
, &fnum
);
1057 if (!NT_STATUS_IS_OK(status
)) {
1058 d_printf("%s opening remote file %s\n", nt_errstr(status
),
1063 if(!strcmp(lname
,"-")) {
1064 handle
= fileno(stdout
);
1067 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
, 0644);
1069 start
= sys_lseek(handle
, 0, SEEK_END
);
1071 d_printf("Error seeking local file\n");
1076 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
1081 d_printf("Error opening local file %s\n",lname
);
1086 if (!cli_qfileinfo(targetcli
, fnum
,
1087 &attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
1088 !NT_STATUS_IS_OK(cli_getattrE(targetcli
, fnum
,
1089 &attr
, &size
, NULL
, NULL
, NULL
))) {
1090 d_printf("getattrib: %s\n",cli_errstr(targetcli
));
1094 DEBUG(1,("getting file %s of size %.0f as %s ",
1095 rname
, (double)size
, lname
));
1097 status
= cli_pull(targetcli
, fnum
, start
, size
, io_bufsize
,
1098 writefile_sink
, (void *)&handle
, &nread
);
1099 if (!NT_STATUS_IS_OK(status
)) {
1100 d_fprintf(stderr
, "parallel_read returned %s\n",
1102 cli_close(targetcli
, fnum
);
1106 status
= cli_close(targetcli
, fnum
);
1107 if (!NT_STATUS_IS_OK(status
)) {
1108 d_printf("Error %s closing remote file\n", nt_errstr(status
));
1116 if (archive_level
>= 2 && (attr
& aARCH
)) {
1117 cli_setatr(cli
, rname
, attr
& ~(uint16
)aARCH
, 0);
1121 struct timespec tp_end
;
1124 clock_gettime_mono(&tp_end
);
1125 this_time
= nsec_time_diff(&tp_end
,&tp_start
)/1000000;
1126 get_total_time_ms
+= this_time
;
1127 get_total_size
+= nread
;
1129 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1130 nread
/ (1.024*this_time
+ 1.0e-4),
1131 get_total_size
/ (1.024*get_total_time_ms
)));
1134 TALLOC_FREE(targetname
);
1138 /****************************************************************************
1140 ****************************************************************************/
1142 static int cmd_get(void)
1144 TALLOC_CTX
*ctx
= talloc_tos();
1149 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1154 if (!next_token_talloc(ctx
, &cmd_ptr
,&fname
,NULL
)) {
1155 d_printf("get <filename> [localname]\n");
1158 rname
= talloc_asprintf_append(rname
, "%s", fname
);
1162 rname
= clean_name(ctx
, rname
);
1167 next_token_talloc(ctx
, &cmd_ptr
,&lname
,NULL
);
1172 return do_get(rname
, lname
, false);
1175 /****************************************************************************
1176 Do an mget operation on one file.
1177 ****************************************************************************/
1179 static void do_mget(struct cli_state
*cli_state
, struct file_info
*finfo
,
1182 TALLOC_CTX
*ctx
= talloc_tos();
1185 char *saved_curdir
= NULL
;
1186 char *mget_mask
= NULL
;
1187 char *new_cd
= NULL
;
1193 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
1197 d_printf("mget aborted\n");
1201 if (finfo
->mode
& aDIR
) {
1202 if (asprintf(&quest
,
1203 "Get directory %s? ",finfo
->name
) < 0) {
1207 if (asprintf(&quest
,
1208 "Get file %s? ",finfo
->name
) < 0) {
1213 if (prompt
&& !yesno(quest
)) {
1219 if (!(finfo
->mode
& aDIR
)) {
1220 rname
= talloc_asprintf(ctx
,
1222 client_get_cur_dir(),
1227 do_get(rname
, finfo
->name
, false);
1232 /* handle directories */
1233 saved_curdir
= talloc_strdup(ctx
, client_get_cur_dir());
1234 if (!saved_curdir
) {
1238 new_cd
= talloc_asprintf(ctx
,
1240 client_get_cur_dir(),
1246 client_set_cur_dir(new_cd
);
1248 string_replace(finfo
->name
,'\\','/');
1250 strlower_m(finfo
->name
);
1253 if (!directory_exist(finfo
->name
) &&
1254 mkdir(finfo
->name
,0777) != 0) {
1255 d_printf("failed to create directory %s\n",finfo
->name
);
1256 client_set_cur_dir(saved_curdir
);
1260 if (chdir(finfo
->name
) != 0) {
1261 d_printf("failed to chdir to directory %s\n",finfo
->name
);
1262 client_set_cur_dir(saved_curdir
);
1266 mget_mask
= talloc_asprintf(ctx
,
1268 client_get_cur_dir());
1274 do_list(mget_mask
, aSYSTEM
| aHIDDEN
| aDIR
,do_mget
,false, true);
1275 if (chdir("..") == -1) {
1276 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1279 client_set_cur_dir(saved_curdir
);
1280 TALLOC_FREE(mget_mask
);
1281 TALLOC_FREE(saved_curdir
);
1282 TALLOC_FREE(new_cd
);
1285 /****************************************************************************
1286 View the file using the pager.
1287 ****************************************************************************/
1289 static int cmd_more(void)
1291 TALLOC_CTX
*ctx
= talloc_tos();
1295 char *pager_cmd
= NULL
;
1300 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1305 lname
= talloc_asprintf(ctx
, "%s/smbmore.XXXXXX",tmpdir());
1309 fd
= mkstemp(lname
);
1311 d_printf("failed to create temporary file for more\n");
1316 if (!next_token_talloc(ctx
, &cmd_ptr
,&fname
,NULL
)) {
1317 d_printf("more <filename>\n");
1321 rname
= talloc_asprintf_append(rname
, "%s", fname
);
1325 rname
= clean_name(ctx
,rname
);
1330 rc
= do_get(rname
, lname
, false);
1332 pager
=getenv("PAGER");
1334 pager_cmd
= talloc_asprintf(ctx
,
1336 (pager
? pager
:PAGER
),
1341 if (system(pager_cmd
) == -1) {
1342 d_printf("system command '%s' returned -1\n",
1350 /****************************************************************************
1352 ****************************************************************************/
1354 static int cmd_mget(void)
1356 TALLOC_CTX
*ctx
= talloc_tos();
1357 uint16 attribute
= aSYSTEM
| aHIDDEN
;
1358 char *mget_mask
= NULL
;
1367 while (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1368 mget_mask
= talloc_strdup(ctx
, client_get_cur_dir());
1372 if (*buf
== CLI_DIRSEP_CHAR
) {
1373 mget_mask
= talloc_strdup(ctx
, buf
);
1375 mget_mask
= talloc_asprintf_append(mget_mask
,
1381 do_list(mget_mask
, attribute
, do_mget
, false, true);
1384 if (mget_mask
== NULL
) {
1385 d_printf("nothing to mget\n");
1390 mget_mask
= talloc_asprintf(ctx
,
1392 client_get_cur_dir());
1396 do_list(mget_mask
, attribute
, do_mget
, false, true);
1402 /****************************************************************************
1403 Make a directory of name "name".
1404 ****************************************************************************/
1406 static bool do_mkdir(const char *name
)
1408 TALLOC_CTX
*ctx
= talloc_tos();
1409 struct cli_state
*targetcli
;
1410 char *targetname
= NULL
;
1413 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, name
, &targetcli
, &targetname
)) {
1414 d_printf("mkdir %s: %s\n", name
, cli_errstr(cli
));
1418 status
= cli_mkdir(targetcli
, targetname
);
1419 if (!NT_STATUS_IS_OK(status
)) {
1420 d_printf("%s making remote directory %s\n",
1421 nt_errstr(status
),name
);
1428 /****************************************************************************
1429 Show 8.3 name of a file.
1430 ****************************************************************************/
1432 static bool do_altname(const char *name
)
1437 status
= cli_qpathinfo_alt_name(cli
, name
, altname
);
1438 if (!NT_STATUS_IS_OK(status
)) {
1439 d_printf("%s getting alt name for %s\n",
1440 nt_errstr(status
),name
);
1443 d_printf("%s\n", altname
);
1448 /****************************************************************************
1450 ****************************************************************************/
1452 static int cmd_quit(void)
1460 /****************************************************************************
1462 ****************************************************************************/
1464 static int cmd_mkdir(void)
1466 TALLOC_CTX
*ctx
= talloc_tos();
1470 mask
= talloc_strdup(ctx
, client_get_cur_dir());
1475 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1477 d_printf("mkdir <dirname>\n");
1481 mask
= talloc_asprintf_append(mask
, "%s", buf
);
1489 struct cli_state
*targetcli
;
1490 char *targetname
= NULL
;
1494 ddir2
= talloc_strdup(ctx
, "");
1499 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
1503 ddir
= talloc_strdup(ctx
, targetname
);
1507 trim_char(ddir
,'.','\0');
1508 p
= strtok_r(ddir
, "/\\", &saveptr
);
1510 ddir2
= talloc_asprintf_append(ddir2
, "%s", p
);
1514 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli
, ddir2
))) {
1517 ddir2
= talloc_asprintf_append(ddir2
, "%s", CLI_DIRSEP_STR
);
1521 p
= strtok_r(NULL
, "/\\", &saveptr
);
1530 /****************************************************************************
1532 ****************************************************************************/
1534 static int cmd_altname(void)
1536 TALLOC_CTX
*ctx
= talloc_tos();
1540 name
= talloc_strdup(ctx
, client_get_cur_dir());
1545 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
1546 d_printf("altname <file>\n");
1549 name
= talloc_asprintf_append(name
, "%s", buf
);
1557 static char *attr_str(TALLOC_CTX
*mem_ctx
, uint16_t mode
)
1559 char *attrs
= TALLOC_ZERO_ARRAY(mem_ctx
, char, 17);
1562 if (!(mode
& FILE_ATTRIBUTE_NORMAL
)) {
1563 if (mode
& FILE_ATTRIBUTE_READONLY
) {
1566 if (mode
& FILE_ATTRIBUTE_HIDDEN
) {
1569 if (mode
& FILE_ATTRIBUTE_SYSTEM
) {
1572 if (mode
& FILE_ATTRIBUTE_DIRECTORY
) {
1575 if (mode
& FILE_ATTRIBUTE_ARCHIVE
) {
1582 /****************************************************************************
1583 Show all info we can get
1584 ****************************************************************************/
1586 static int do_allinfo(const char *name
)
1589 struct timespec b_time
, a_time
, m_time
, c_time
;
1594 unsigned int num_streams
;
1595 struct stream_struct
*streams
;
1599 status
= cli_qpathinfo_alt_name(cli
, name
, altname
);
1600 if (!NT_STATUS_IS_OK(status
)) {
1601 d_printf("%s getting alt name for %s\n", nt_errstr(status
),
1605 d_printf("altname: %s\n", altname
);
1607 status
= cli_qpathinfo2(cli
, name
, &b_time
, &a_time
, &m_time
, &c_time
,
1608 &size
, &mode
, &ino
);
1609 if (!NT_STATUS_IS_OK(status
)) {
1610 d_printf("%s getting pathinfo for %s\n", nt_errstr(status
),
1615 unix_timespec_to_nt_time(&tmp
, b_time
);
1616 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1618 unix_timespec_to_nt_time(&tmp
, a_time
);
1619 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1621 unix_timespec_to_nt_time(&tmp
, m_time
);
1622 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1624 unix_timespec_to_nt_time(&tmp
, c_time
);
1625 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp
));
1627 d_printf("attributes: %s\n", attr_str(talloc_tos(), mode
));
1629 status
= cli_qpathinfo_streams(cli
, name
, talloc_tos(), &num_streams
,
1631 if (!NT_STATUS_IS_OK(status
)) {
1632 d_printf("%s getting streams for %s\n", nt_errstr(status
),
1637 for (i
=0; i
<num_streams
; i
++) {
1638 d_printf("stream: [%s], %lld bytes\n", streams
[i
].name
,
1639 (unsigned long long)streams
[i
].size
);
1645 /****************************************************************************
1646 Show all info we can get
1647 ****************************************************************************/
1649 static int cmd_allinfo(void)
1651 TALLOC_CTX
*ctx
= talloc_tos();
1655 name
= talloc_strdup(ctx
, client_get_cur_dir());
1660 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
1661 d_printf("allinfo <file>\n");
1664 name
= talloc_asprintf_append(name
, "%s", buf
);
1674 /****************************************************************************
1676 ****************************************************************************/
1678 static int do_put(const char *rname
, const char *lname
, bool reput
)
1680 TALLOC_CTX
*ctx
= talloc_tos();
1683 SMB_OFF_T start
= 0;
1685 struct timespec tp_start
;
1686 struct cli_state
*targetcli
;
1687 char *targetname
= NULL
;
1688 struct push_state state
;
1691 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, rname
, &targetcli
, &targetname
)) {
1692 d_printf("Failed to open %s: %s\n", rname
, cli_errstr(cli
));
1696 clock_gettime_mono(&tp_start
);
1699 status
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
, DENY_NONE
, &fnum
);
1700 if (NT_STATUS_IS_OK(status
)) {
1701 if (!cli_qfileinfo(targetcli
, fnum
, NULL
, &start
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
1702 !NT_STATUS_IS_OK(cli_getattrE(targetcli
, fnum
, NULL
, &start
, NULL
, NULL
, NULL
))) {
1703 d_printf("getattrib: %s\n",cli_errstr(cli
));
1708 status
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
, &fnum
);
1711 if (!NT_STATUS_IS_OK(status
)) {
1712 d_printf("%s opening remote file %s\n",cli_errstr(targetcli
),rname
);
1716 /* allow files to be piped into smbclient
1719 Note that in this case this function will exit(0) rather
1721 if (!strcmp(lname
, "-")) {
1723 /* size of file is not known */
1725 f
= x_fopen(lname
,O_RDONLY
, 0);
1727 if (x_tseek(f
, start
, SEEK_SET
) == -1) {
1728 d_printf("Error seeking local file\n");
1736 d_printf("Error opening local file %s\n",lname
);
1740 DEBUG(1,("putting file %s as %s ",lname
,
1743 x_setvbuf(f
, NULL
, X_IOFBF
, io_bufsize
);
1748 status
= cli_push(targetcli
, fnum
, 0, 0, io_bufsize
, push_source
,
1750 if (!NT_STATUS_IS_OK(status
)) {
1751 d_fprintf(stderr
, "cli_push returned %s\n", nt_errstr(status
));
1755 status
= cli_close(targetcli
, fnum
);
1756 if (!NT_STATUS_IS_OK(status
)) {
1757 d_printf("%s closing remote file %s\n", nt_errstr(status
),
1770 struct timespec tp_end
;
1773 clock_gettime_mono(&tp_end
);
1774 this_time
= nsec_time_diff(&tp_end
,&tp_start
)/1000000;
1775 put_total_time_ms
+= this_time
;
1776 put_total_size
+= state
.nread
;
1778 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1779 state
.nread
/ (1.024*this_time
+ 1.0e-4),
1780 put_total_size
/ (1.024*put_total_time_ms
)));
1791 /****************************************************************************
1793 ****************************************************************************/
1795 static int cmd_put(void)
1797 TALLOC_CTX
*ctx
= talloc_tos();
1802 rname
= talloc_strdup(ctx
, client_get_cur_dir());
1807 if (!next_token_talloc(ctx
, &cmd_ptr
,&lname
,NULL
)) {
1808 d_printf("put <filename>\n");
1812 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
1813 rname
= talloc_asprintf_append(rname
, "%s", buf
);
1815 rname
= talloc_asprintf_append(rname
, "%s", lname
);
1821 rname
= clean_name(ctx
, rname
);
1828 /* allow '-' to represent stdin
1829 jdblair, 24.jun.98 */
1830 if (!file_exist_stat(lname
, &st
, false) &&
1831 (strcmp(lname
,"-"))) {
1832 d_printf("%s does not exist\n",lname
);
1837 return do_put(rname
, lname
, false);
1840 /*************************************
1841 File list structure.
1842 *************************************/
1844 static struct file_list
{
1845 struct file_list
*prev
, *next
;
1850 /****************************************************************************
1851 Free a file_list structure.
1852 ****************************************************************************/
1854 static void free_file_list (struct file_list
*l_head
)
1856 struct file_list
*list
, *next
;
1858 for (list
= l_head
; list
; list
= next
) {
1860 DLIST_REMOVE(l_head
, list
);
1861 SAFE_FREE(list
->file_path
);
1866 /****************************************************************************
1867 Seek in a directory/file list until you get something that doesn't start with
1869 ****************************************************************************/
1871 static bool seek_list(struct file_list
*list
, char *name
)
1874 trim_string(list
->file_path
,"./","\n");
1875 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
1884 /****************************************************************************
1885 Set the file selection mask.
1886 ****************************************************************************/
1888 static int cmd_select(void)
1890 TALLOC_CTX
*ctx
= talloc_tos();
1891 char *new_fs
= NULL
;
1892 next_token_talloc(ctx
, &cmd_ptr
,&new_fs
,NULL
)
1895 client_set_fileselection(new_fs
);
1897 client_set_fileselection("");
1902 /****************************************************************************
1903 Recursive file matching function act as find
1904 match must be always set to true when calling this function
1905 ****************************************************************************/
1907 static int file_find(struct file_list
**list
, const char *directory
,
1908 const char *expression
, bool match
)
1910 SMB_STRUCT_DIR
*dir
;
1911 struct file_list
*entry
;
1912 struct stat statbuf
;
1918 dir
= sys_opendir(directory
);
1922 while ((dname
= readdirname(dir
))) {
1923 if (!strcmp("..", dname
))
1925 if (!strcmp(".", dname
))
1928 if (asprintf(&path
, "%s/%s", directory
, dname
) <= 0) {
1933 if (!match
|| !gen_fnmatch(expression
, dname
)) {
1935 ret
= stat(path
, &statbuf
);
1937 if (S_ISDIR(statbuf
.st_mode
)) {
1939 ret
= file_find(list
, path
, expression
, false);
1942 d_printf("file_find: cannot stat file %s\n", path
);
1951 entry
= SMB_MALLOC_P(struct file_list
);
1953 d_printf("Out of memory in file_find\n");
1957 entry
->file_path
= path
;
1958 entry
->isdir
= isdir
;
1959 DLIST_ADD(*list
, entry
);
1969 /****************************************************************************
1971 ****************************************************************************/
1973 static int cmd_mput(void)
1975 TALLOC_CTX
*ctx
= talloc_tos();
1978 while (next_token_talloc(ctx
, &cmd_ptr
,&p
,NULL
)) {
1980 struct file_list
*temp_list
;
1981 char *quest
, *lname
, *rname
;
1985 ret
= file_find(&file_list
, ".", p
, true);
1987 free_file_list(file_list
);
1995 for (temp_list
= file_list
; temp_list
;
1996 temp_list
= temp_list
->next
) {
1999 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0) {
2002 trim_string(lname
, "./", "/");
2004 /* check if it's a directory */
2005 if (temp_list
->isdir
) {
2006 /* if (!recurse) continue; */
2009 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) {
2012 if (prompt
&& !yesno(quest
)) { /* No */
2013 /* Skip the directory */
2014 lname
[strlen(lname
)-1] = '/';
2015 if (!seek_list(temp_list
, lname
))
2019 if(asprintf(&rname
, "%s%s", client_get_cur_dir(), lname
) < 0) {
2022 normalize_name(rname
);
2023 if (!NT_STATUS_IS_OK(cli_chkpath(cli
, rname
)) &&
2025 DEBUG (0, ("Unable to make dir, skipping..."));
2026 /* Skip the directory */
2027 lname
[strlen(lname
)-1] = '/';
2028 if (!seek_list(temp_list
, lname
)) {
2036 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) {
2039 if (prompt
&& !yesno(quest
)) {
2046 if (asprintf(&rname
, "%s%s", client_get_cur_dir(), lname
) < 0) {
2051 normalize_name(rname
);
2053 do_put(rname
, lname
, false);
2055 free_file_list(file_list
);
2064 /****************************************************************************
2066 ****************************************************************************/
2068 static int do_cancel(int job
)
2070 if (cli_printjob_del(cli
, job
)) {
2071 d_printf("Job %d cancelled\n",job
);
2074 d_printf("Error cancelling job %d : %s\n",job
,cli_errstr(cli
));
2079 /****************************************************************************
2081 ****************************************************************************/
2083 static int cmd_cancel(void)
2085 TALLOC_CTX
*ctx
= talloc_tos();
2089 if (!next_token_talloc(ctx
, &cmd_ptr
, &buf
,NULL
)) {
2090 d_printf("cancel <jobid> ...\n");
2096 } while (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
));
2101 /****************************************************************************
2103 ****************************************************************************/
2105 static int cmd_print(void)
2107 TALLOC_CTX
*ctx
= talloc_tos();
2112 if (!next_token_talloc(ctx
, &cmd_ptr
, &lname
,NULL
)) {
2113 d_printf("print <filename>\n");
2117 rname
= talloc_strdup(ctx
, lname
);
2121 p
= strrchr_m(rname
,'/');
2123 rname
= talloc_asprintf(ctx
,
2128 if (strequal(lname
,"-")) {
2129 rname
= talloc_asprintf(ctx
,
2137 return do_put(rname
, lname
, false);
2140 /****************************************************************************
2141 Show a print queue entry.
2142 ****************************************************************************/
2144 static void queue_fn(struct print_job_info
*p
)
2146 d_printf("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
);
2149 /****************************************************************************
2151 ****************************************************************************/
2153 static int cmd_queue(void)
2155 cli_print_queue(cli
, queue_fn
);
2159 /****************************************************************************
2161 ****************************************************************************/
2163 static void do_del(struct cli_state
*cli_state
, struct file_info
*finfo
,
2166 TALLOC_CTX
*ctx
= talloc_tos();
2170 mask
= talloc_asprintf(ctx
,
2179 if (finfo
->mode
& aDIR
) {
2184 status
= cli_unlink(cli_state
, mask
, aSYSTEM
| aHIDDEN
);
2185 if (!NT_STATUS_IS_OK(status
)) {
2186 d_printf("%s deleting remote file %s\n",
2187 nt_errstr(status
), mask
);
2192 /****************************************************************************
2194 ****************************************************************************/
2196 static int cmd_del(void)
2198 TALLOC_CTX
*ctx
= talloc_tos();
2201 uint16 attribute
= aSYSTEM
| aHIDDEN
;
2207 mask
= talloc_strdup(ctx
, client_get_cur_dir());
2211 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2212 d_printf("del <filename>\n");
2215 mask
= talloc_asprintf_append(mask
, "%s", buf
);
2220 do_list(mask
,attribute
,do_del
,false,false);
2224 /****************************************************************************
2225 Wildcard delete some files.
2226 ****************************************************************************/
2228 static int cmd_wdel(void)
2230 TALLOC_CTX
*ctx
= talloc_tos();
2234 struct cli_state
*targetcli
;
2235 char *targetname
= NULL
;
2238 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2239 d_printf("wdel 0x<attrib> <wcard>\n");
2243 attribute
= (uint16
)strtol(buf
, (char **)NULL
, 16);
2245 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2246 d_printf("wdel 0x<attrib> <wcard>\n");
2250 mask
= talloc_asprintf(ctx
, "%s%s",
2251 client_get_cur_dir(),
2257 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2258 d_printf("cmd_wdel %s: %s\n", mask
, cli_errstr(cli
));
2262 status
= cli_unlink(targetcli
, targetname
, attribute
);
2263 if (!NT_STATUS_IS_OK(status
)) {
2264 d_printf("%s deleting remote files %s\n", nt_errstr(status
),
2270 /****************************************************************************
2271 ****************************************************************************/
2273 static int cmd_open(void)
2275 TALLOC_CTX
*ctx
= talloc_tos();
2278 char *targetname
= NULL
;
2279 struct cli_state
*targetcli
;
2280 uint16_t fnum
= (uint16_t)-1;
2282 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2283 d_printf("open <filename>\n");
2286 mask
= talloc_asprintf(ctx
,
2288 client_get_cur_dir(),
2294 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2295 d_printf("open %s: %s\n", mask
, cli_errstr(cli
));
2299 if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli
, targetname
, 0,
2300 FILE_READ_DATA
|FILE_WRITE_DATA
, 0,
2301 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
2302 if (NT_STATUS_IS_OK(cli_ntcreate(targetcli
, targetname
, 0,
2304 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
2305 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
2307 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
2310 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
2315 static int cmd_posix_encrypt(void)
2317 TALLOC_CTX
*ctx
= talloc_tos();
2318 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2320 if (cli
->use_kerberos
) {
2321 status
= cli_gss_smb_encryption_start(cli
);
2323 char *domain
= NULL
;
2325 char *password
= NULL
;
2327 if (!next_token_talloc(ctx
, &cmd_ptr
,&domain
,NULL
)) {
2328 d_printf("posix_encrypt domain user password\n");
2332 if (!next_token_talloc(ctx
, &cmd_ptr
,&user
,NULL
)) {
2333 d_printf("posix_encrypt domain user password\n");
2337 if (!next_token_talloc(ctx
, &cmd_ptr
,&password
,NULL
)) {
2338 d_printf("posix_encrypt domain user password\n");
2342 status
= cli_raw_ntlm_smb_encryption_start(cli
,
2348 if (!NT_STATUS_IS_OK(status
)) {
2349 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status
));
2351 d_printf("encryption on\n");
2358 /****************************************************************************
2359 ****************************************************************************/
2361 static int cmd_posix_open(void)
2363 TALLOC_CTX
*ctx
= talloc_tos();
2366 char *targetname
= NULL
;
2367 struct cli_state
*targetcli
;
2371 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2372 d_printf("posix_open <filename> 0<mode>\n");
2375 mask
= talloc_asprintf(ctx
,
2377 client_get_cur_dir(),
2383 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2384 d_printf("posix_open <filename> 0<mode>\n");
2387 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
2389 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2390 d_printf("posix_open %s: %s\n", mask
, cli_errstr(cli
));
2394 if (!NT_STATUS_IS_OK(cli_posix_open(targetcli
, targetname
, O_CREAT
|O_RDWR
, mode
, &fnum
))) {
2395 if (!NT_STATUS_IS_OK(cli_posix_open(targetcli
, targetname
, O_CREAT
|O_RDONLY
, mode
, &fnum
))) {
2396 d_printf("posix_open file %s: for read/write fnum %d\n", targetname
, fnum
);
2398 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
2401 d_printf("posix_open file %s: for read/write fnum %d\n", targetname
, fnum
);
2407 static int cmd_posix_mkdir(void)
2409 TALLOC_CTX
*ctx
= talloc_tos();
2412 char *targetname
= NULL
;
2413 struct cli_state
*targetcli
;
2416 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2417 d_printf("posix_mkdir <filename> 0<mode>\n");
2420 mask
= talloc_asprintf(ctx
,
2422 client_get_cur_dir(),
2428 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2429 d_printf("posix_mkdir <filename> 0<mode>\n");
2432 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
2434 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2435 d_printf("posix_mkdir %s: %s\n", mask
, cli_errstr(cli
));
2439 if (!NT_STATUS_IS_OK(cli_posix_mkdir(targetcli
, targetname
, mode
))) {
2440 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
2442 d_printf("posix_mkdir created directory %s\n", targetname
);
2447 static int cmd_posix_unlink(void)
2449 TALLOC_CTX
*ctx
= talloc_tos();
2452 char *targetname
= NULL
;
2453 struct cli_state
*targetcli
;
2455 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2456 d_printf("posix_unlink <filename>\n");
2459 mask
= talloc_asprintf(ctx
,
2461 client_get_cur_dir(),
2467 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2468 d_printf("posix_unlink %s: %s\n", mask
, cli_errstr(cli
));
2472 if (!NT_STATUS_IS_OK(cli_posix_unlink(targetcli
, targetname
))) {
2473 d_printf("Failed to unlink file %s. %s\n", targetname
, cli_errstr(cli
));
2475 d_printf("posix_unlink deleted file %s\n", targetname
);
2481 static int cmd_posix_rmdir(void)
2483 TALLOC_CTX
*ctx
= talloc_tos();
2486 char *targetname
= NULL
;
2487 struct cli_state
*targetcli
;
2489 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2490 d_printf("posix_rmdir <filename>\n");
2493 mask
= talloc_asprintf(ctx
,
2495 client_get_cur_dir(),
2501 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2502 d_printf("posix_rmdir %s: %s\n", mask
, cli_errstr(cli
));
2506 if (!NT_STATUS_IS_OK(cli_posix_rmdir(targetcli
, targetname
))) {
2507 d_printf("Failed to unlink directory %s. %s\n", targetname
, cli_errstr(cli
));
2509 d_printf("posix_rmdir deleted directory %s\n", targetname
);
2515 static int cmd_close(void)
2517 TALLOC_CTX
*ctx
= talloc_tos();
2521 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2522 d_printf("close <fnum>\n");
2527 /* We really should use the targetcli here.... */
2528 if (!NT_STATUS_IS_OK(cli_close(cli
, fnum
))) {
2529 d_printf("close %d: %s\n", fnum
, cli_errstr(cli
));
2535 static int cmd_posix(void)
2537 TALLOC_CTX
*ctx
= talloc_tos();
2538 uint16 major
, minor
;
2539 uint32 caplow
, caphigh
;
2543 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
2544 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2548 status
= cli_unix_extensions_version(cli
, &major
, &minor
, &caplow
,
2550 if (!NT_STATUS_IS_OK(status
)) {
2551 d_printf("Can't get UNIX CIFS extensions version from "
2552 "server: %s\n", nt_errstr(status
));
2556 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major
, (unsigned int)minor
);
2558 caps
= talloc_strdup(ctx
, "");
2562 if (caplow
& CIFS_UNIX_FCNTL_LOCKS_CAP
) {
2563 caps
= talloc_asprintf_append(caps
, "locks ");
2568 if (caplow
& CIFS_UNIX_POSIX_ACLS_CAP
) {
2569 caps
= talloc_asprintf_append(caps
, "acls ");
2574 if (caplow
& CIFS_UNIX_XATTTR_CAP
) {
2575 caps
= talloc_asprintf_append(caps
, "eas ");
2580 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
2581 caps
= talloc_asprintf_append(caps
, "pathnames ");
2586 if (caplow
& CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP
) {
2587 caps
= talloc_asprintf_append(caps
, "posix_path_operations ");
2592 if (caplow
& CIFS_UNIX_LARGE_READ_CAP
) {
2593 caps
= talloc_asprintf_append(caps
, "large_read ");
2598 if (caplow
& CIFS_UNIX_LARGE_WRITE_CAP
) {
2599 caps
= talloc_asprintf_append(caps
, "large_write ");
2604 if (caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP
) {
2605 caps
= talloc_asprintf_append(caps
, "posix_encrypt ");
2610 if (caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP
) {
2611 caps
= talloc_asprintf_append(caps
, "mandatory_posix_encrypt ");
2617 if (*caps
&& caps
[strlen(caps
)-1] == ' ') {
2618 caps
[strlen(caps
)-1] = '\0';
2621 d_printf("Server supports CIFS capabilities %s\n", caps
);
2623 status
= cli_set_unix_extensions_capabilities(cli
, major
, minor
,
2625 if (!NT_STATUS_IS_OK(status
)) {
2626 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
2631 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
2632 CLI_DIRSEP_CHAR
= '/';
2633 *CLI_DIRSEP_STR
= '/';
2634 client_set_cur_dir(CLI_DIRSEP_STR
);
2640 static int cmd_lock(void)
2642 TALLOC_CTX
*ctx
= talloc_tos();
2644 uint64_t start
, len
;
2645 enum brl_type lock_type
;
2648 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2649 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2654 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2655 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2659 if (*buf
== 'r' || *buf
== 'R') {
2660 lock_type
= READ_LOCK
;
2661 } else if (*buf
== 'w' || *buf
== 'W') {
2662 lock_type
= WRITE_LOCK
;
2664 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2668 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2669 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2673 start
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
2675 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2676 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2680 len
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
2682 if (!NT_STATUS_IS_OK(cli_posix_lock(cli
, fnum
, start
, len
, true, lock_type
))) {
2683 d_printf("lock failed %d: %s\n", fnum
, cli_errstr(cli
));
2689 static int cmd_unlock(void)
2691 TALLOC_CTX
*ctx
= talloc_tos();
2693 uint64_t start
, len
;
2696 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2697 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2702 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2703 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2707 start
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
2709 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2710 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2714 len
= (uint64_t)strtol(buf
, (char **)NULL
, 16);
2716 if (!NT_STATUS_IS_OK(cli_posix_unlock(cli
, fnum
, start
, len
))) {
2717 d_printf("unlock failed %d: %s\n", fnum
, cli_errstr(cli
));
2724 /****************************************************************************
2726 ****************************************************************************/
2728 static int cmd_rmdir(void)
2730 TALLOC_CTX
*ctx
= talloc_tos();
2733 char *targetname
= NULL
;
2734 struct cli_state
*targetcli
;
2736 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2737 d_printf("rmdir <dirname>\n");
2740 mask
= talloc_asprintf(ctx
,
2742 client_get_cur_dir(),
2748 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, mask
, &targetcli
, &targetname
)) {
2749 d_printf("rmdir %s: %s\n", mask
, cli_errstr(cli
));
2753 if (!NT_STATUS_IS_OK(cli_rmdir(targetcli
, targetname
))) {
2754 d_printf("%s removing remote directory file %s\n",
2755 cli_errstr(targetcli
),mask
);
2761 /****************************************************************************
2763 ****************************************************************************/
2765 static int cmd_link(void)
2767 TALLOC_CTX
*ctx
= talloc_tos();
2768 char *oldname
= NULL
;
2769 char *newname
= NULL
;
2772 char *targetname
= NULL
;
2773 struct cli_state
*targetcli
;
2775 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
2776 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
2777 d_printf("link <oldname> <newname>\n");
2780 oldname
= talloc_asprintf(ctx
,
2782 client_get_cur_dir(),
2787 newname
= talloc_asprintf(ctx
,
2789 client_get_cur_dir(),
2795 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, oldname
, &targetcli
, &targetname
)) {
2796 d_printf("link %s: %s\n", oldname
, cli_errstr(cli
));
2800 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2801 d_printf("Server doesn't support UNIX CIFS calls.\n");
2805 if (!NT_STATUS_IS_OK(cli_posix_hardlink(targetcli
, targetname
, newname
))) {
2806 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli
), newname
, oldname
);
2812 /****************************************************************************
2814 ****************************************************************************/
2816 static int cmd_readlink(void)
2818 TALLOC_CTX
*ctx
= talloc_tos();
2821 char *targetname
= NULL
;
2822 char linkname
[PATH_MAX
+1];
2823 struct cli_state
*targetcli
;
2825 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
2826 d_printf("readlink <name>\n");
2829 name
= talloc_asprintf(ctx
,
2831 client_get_cur_dir(),
2837 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, name
, &targetcli
, &targetname
)) {
2838 d_printf("readlink %s: %s\n", name
, cli_errstr(cli
));
2842 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2843 d_printf("Server doesn't support UNIX CIFS calls.\n");
2847 if (!NT_STATUS_IS_OK(cli_posix_readlink(targetcli
, name
,
2848 linkname
, PATH_MAX
+1))) {
2849 d_printf("%s readlink on file %s\n",
2850 cli_errstr(targetcli
), name
);
2854 d_printf("%s -> %s\n", name
, linkname
);
2860 /****************************************************************************
2862 ****************************************************************************/
2864 static int cmd_symlink(void)
2866 TALLOC_CTX
*ctx
= talloc_tos();
2867 char *oldname
= NULL
;
2868 char *newname
= NULL
;
2871 struct cli_state
*newcli
;
2873 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
2874 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
2875 d_printf("symlink <oldname> <newname>\n");
2878 /* Oldname (link target) must be an untouched blob. */
2881 newname
= talloc_asprintf(ctx
,
2883 client_get_cur_dir(),
2889 /* New name must be present in share namespace. */
2890 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, newname
, &newcli
, &newname
)) {
2891 d_printf("link %s: %s\n", oldname
, cli_errstr(cli
));
2895 if (!SERVER_HAS_UNIX_CIFS(newcli
)) {
2896 d_printf("Server doesn't support UNIX CIFS calls.\n");
2900 if (!NT_STATUS_IS_OK(cli_posix_symlink(newcli
, oldname
, newname
))) {
2901 d_printf("%s symlinking files (%s -> %s)\n",
2902 cli_errstr(newcli
), newname
, newname
);
2909 /****************************************************************************
2911 ****************************************************************************/
2913 static int cmd_chmod(void)
2915 TALLOC_CTX
*ctx
= talloc_tos();
2919 char *targetname
= NULL
;
2920 struct cli_state
*targetcli
;
2923 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
2924 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
2925 d_printf("chmod mode file\n");
2928 src
= talloc_asprintf(ctx
,
2930 client_get_cur_dir(),
2936 mode
= (mode_t
)strtol(buf
, NULL
, 8);
2938 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
)) {
2939 d_printf("chmod %s: %s\n", src
, cli_errstr(cli
));
2943 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2944 d_printf("Server doesn't support UNIX CIFS calls.\n");
2948 if (!NT_STATUS_IS_OK(cli_posix_chmod(targetcli
, targetname
, mode
))) {
2949 d_printf("%s chmod file %s 0%o\n",
2950 cli_errstr(targetcli
), src
, (unsigned int)mode
);
2957 static const char *filetype_to_str(mode_t mode
)
2959 if (S_ISREG(mode
)) {
2960 return "regular file";
2961 } else if (S_ISDIR(mode
)) {
2965 if (S_ISCHR(mode
)) {
2966 return "character device";
2970 if (S_ISBLK(mode
)) {
2971 return "block device";
2975 if (S_ISFIFO(mode
)) {
2980 if (S_ISLNK(mode
)) {
2981 return "symbolic link";
2985 if (S_ISSOCK(mode
)) {
2992 static char rwx_to_str(mode_t m
, mode_t bt
, char ret
)
3001 static char *unix_mode_to_str(char *s
, mode_t m
)
3004 const char *str
= filetype_to_str(m
);
3020 *p
++ = str
[1] == 'y' ? 'l' : 's';
3027 *p
++ = rwx_to_str(m
, S_IRUSR
, 'r');
3028 *p
++ = rwx_to_str(m
, S_IWUSR
, 'w');
3029 *p
++ = rwx_to_str(m
, S_IXUSR
, 'x');
3030 *p
++ = rwx_to_str(m
, S_IRGRP
, 'r');
3031 *p
++ = rwx_to_str(m
, S_IWGRP
, 'w');
3032 *p
++ = rwx_to_str(m
, S_IXGRP
, 'x');
3033 *p
++ = rwx_to_str(m
, S_IROTH
, 'r');
3034 *p
++ = rwx_to_str(m
, S_IWOTH
, 'w');
3035 *p
++ = rwx_to_str(m
, S_IXOTH
, 'x');
3040 /****************************************************************************
3041 Utility function for UNIX getfacl.
3042 ****************************************************************************/
3044 static char *perms_to_string(fstring permstr
, unsigned char perms
)
3046 fstrcpy(permstr
, "---");
3047 if (perms
& SMB_POSIX_ACL_READ
) {
3050 if (perms
& SMB_POSIX_ACL_WRITE
) {
3053 if (perms
& SMB_POSIX_ACL_EXECUTE
) {
3059 /****************************************************************************
3061 ****************************************************************************/
3063 static int cmd_getfacl(void)
3065 TALLOC_CTX
*ctx
= talloc_tos();
3068 char *targetname
= NULL
;
3069 struct cli_state
*targetcli
;
3070 uint16 major
, minor
;
3071 uint32 caplow
, caphigh
;
3072 char *retbuf
= NULL
;
3074 SMB_STRUCT_STAT sbuf
;
3075 uint16 num_file_acls
= 0;
3076 uint16 num_dir_acls
= 0;
3080 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
3081 d_printf("getfacl filename\n");
3084 src
= talloc_asprintf(ctx
,
3086 client_get_cur_dir(),
3092 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
)) {
3093 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
3097 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3098 d_printf("Server doesn't support UNIX CIFS calls.\n");
3102 status
= cli_unix_extensions_version(targetcli
, &major
, &minor
,
3104 if (!NT_STATUS_IS_OK(status
)) {
3105 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3110 if (!(caplow
& CIFS_UNIX_POSIX_ACLS_CAP
)) {
3111 d_printf("This server supports UNIX extensions "
3112 "but doesn't support POSIX ACLs.\n");
3116 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli
, targetname
, &sbuf
))) {
3117 d_printf("%s getfacl doing a stat on file %s\n",
3118 cli_errstr(targetcli
), src
);
3122 if (!NT_STATUS_IS_OK(cli_posix_getfacl(targetcli
, targetname
, ctx
, &rb_size
, &retbuf
))) {
3123 d_printf("%s getfacl file %s\n",
3124 cli_errstr(targetcli
), src
);
3128 /* ToDo : Print out the ACL values. */
3129 if (rb_size
< 6 || SVAL(retbuf
,0) != SMB_POSIX_ACL_VERSION
) {
3130 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3131 src
, (unsigned int)CVAL(retbuf
,0) );
3135 num_file_acls
= SVAL(retbuf
,2);
3136 num_dir_acls
= SVAL(retbuf
,4);
3137 if (rb_size
!= SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)) {
3138 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3140 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)),
3141 (unsigned int)rb_size
);
3145 d_printf("# file: %s\n", src
);
3146 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf
.st_ex_uid
, (unsigned int)sbuf
.st_ex_gid
);
3148 if (num_file_acls
== 0 && num_dir_acls
== 0) {
3149 d_printf("No acls found.\n");
3152 for (i
= 0; i
< num_file_acls
; i
++) {
3155 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
));
3156 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
3159 case SMB_POSIX_ACL_USER_OBJ
:
3162 case SMB_POSIX_ACL_USER
:
3163 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3164 d_printf("user:%u:", uorg
);
3166 case SMB_POSIX_ACL_GROUP_OBJ
:
3167 d_printf("group::");
3169 case SMB_POSIX_ACL_GROUP
:
3170 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3171 d_printf("group:%u:", uorg
);
3173 case SMB_POSIX_ACL_MASK
:
3176 case SMB_POSIX_ACL_OTHER
:
3177 d_printf("other::");
3180 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3181 src
, (unsigned int)tagtype
);
3186 d_printf("%s\n", perms_to_string(permstring
, perms
));
3189 for (i
= 0; i
< num_dir_acls
; i
++) {
3192 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
));
3193 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
3196 case SMB_POSIX_ACL_USER_OBJ
:
3197 d_printf("default:user::");
3199 case SMB_POSIX_ACL_USER
:
3200 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3201 d_printf("default:user:%u:", uorg
);
3203 case SMB_POSIX_ACL_GROUP_OBJ
:
3204 d_printf("default:group::");
3206 case SMB_POSIX_ACL_GROUP
:
3207 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
3208 d_printf("default:group:%u:", uorg
);
3210 case SMB_POSIX_ACL_MASK
:
3211 d_printf("default:mask::");
3213 case SMB_POSIX_ACL_OTHER
:
3214 d_printf("default:other::");
3217 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3218 src
, (unsigned int)tagtype
);
3223 d_printf("%s\n", perms_to_string(permstring
, perms
));
3229 /****************************************************************************
3231 ****************************************************************************/
3233 static int cmd_stat(void)
3235 TALLOC_CTX
*ctx
= talloc_tos();
3238 char *targetname
= NULL
;
3239 struct cli_state
*targetcli
;
3241 SMB_STRUCT_STAT sbuf
;
3245 if (!next_token_talloc(ctx
, &cmd_ptr
,&name
,NULL
)) {
3246 d_printf("stat file\n");
3249 src
= talloc_asprintf(ctx
,
3251 client_get_cur_dir(),
3257 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
)) {
3258 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
3262 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3263 d_printf("Server doesn't support UNIX CIFS calls.\n");
3267 if (!NT_STATUS_IS_OK(cli_posix_stat(targetcli
, targetname
, &sbuf
))) {
3268 d_printf("%s stat file %s\n",
3269 cli_errstr(targetcli
), src
);
3273 /* Print out the stat values. */
3274 d_printf("File: %s\n", src
);
3275 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3276 (double)sbuf
.st_ex_size
,
3277 (unsigned int)sbuf
.st_ex_blocks
,
3278 filetype_to_str(sbuf
.st_ex_mode
));
3280 #if defined(S_ISCHR) && defined(S_ISBLK)
3281 if (S_ISCHR(sbuf
.st_ex_mode
) || S_ISBLK(sbuf
.st_ex_mode
)) {
3282 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3283 (double)sbuf
.st_ex_ino
,
3284 (unsigned int)sbuf
.st_ex_nlink
,
3285 unix_dev_major(sbuf
.st_ex_rdev
),
3286 unix_dev_minor(sbuf
.st_ex_rdev
));
3289 d_printf("Inode: %.0f\tLinks: %u\n",
3290 (double)sbuf
.st_ex_ino
,
3291 (unsigned int)sbuf
.st_ex_nlink
);
3293 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3294 ((int)sbuf
.st_ex_mode
& 0777),
3295 unix_mode_to_str(mode_str
, sbuf
.st_ex_mode
),
3296 (unsigned int)sbuf
.st_ex_uid
,
3297 (unsigned int)sbuf
.st_ex_gid
);
3299 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_atime
);
3300 lt
= localtime(&tmp_time
);
3302 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
3304 fstrcpy(mode_str
, "unknown");
3306 d_printf("Access: %s\n", mode_str
);
3308 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_mtime
);
3309 lt
= localtime(&tmp_time
);
3311 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
3313 fstrcpy(mode_str
, "unknown");
3315 d_printf("Modify: %s\n", mode_str
);
3317 tmp_time
= convert_timespec_to_time_t(sbuf
.st_ex_ctime
);
3318 lt
= localtime(&tmp_time
);
3320 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
3322 fstrcpy(mode_str
, "unknown");
3324 d_printf("Change: %s\n", mode_str
);
3330 /****************************************************************************
3332 ****************************************************************************/
3334 static int cmd_chown(void)
3336 TALLOC_CTX
*ctx
= talloc_tos();
3340 char *buf
, *buf2
, *buf3
;
3341 struct cli_state
*targetcli
;
3342 char *targetname
= NULL
;
3344 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3345 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
) ||
3346 !next_token_talloc(ctx
, &cmd_ptr
,&buf3
,NULL
)) {
3347 d_printf("chown uid gid file\n");
3351 uid
= (uid_t
)atoi(buf
);
3352 gid
= (gid_t
)atoi(buf2
);
3354 src
= talloc_asprintf(ctx
,
3356 client_get_cur_dir(),
3361 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
) ) {
3362 d_printf("chown %s: %s\n", src
, cli_errstr(cli
));
3366 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
3367 d_printf("Server doesn't support UNIX CIFS calls.\n");
3371 if (!NT_STATUS_IS_OK(cli_posix_chown(targetcli
, targetname
, uid
, gid
))) {
3372 d_printf("%s chown file %s uid=%d, gid=%d\n",
3373 cli_errstr(targetcli
), src
, (int)uid
, (int)gid
);
3380 /****************************************************************************
3382 ****************************************************************************/
3384 static int cmd_rename(void)
3386 TALLOC_CTX
*ctx
= talloc_tos();
3389 struct cli_state
*targetcli
;
3393 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3394 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3395 d_printf("rename <src> <dest>\n");
3399 src
= talloc_asprintf(ctx
,
3401 client_get_cur_dir(),
3407 dest
= talloc_asprintf(ctx
,
3409 client_get_cur_dir(),
3415 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetsrc
)) {
3416 d_printf("rename %s: %s\n", src
, cli_errstr(cli
));
3420 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, dest
, &targetcli
, &targetdest
)) {
3421 d_printf("rename %s: %s\n", dest
, cli_errstr(cli
));
3425 if (!NT_STATUS_IS_OK(cli_rename(targetcli
, targetsrc
, targetdest
))) {
3426 d_printf("%s renaming files %s -> %s \n",
3427 cli_errstr(targetcli
),
3436 /****************************************************************************
3437 Print the volume name.
3438 ****************************************************************************/
3440 static int cmd_volume(void)
3447 status
= cli_get_fs_volume_info(cli
, volname
, &serial_num
,
3449 if (!NT_STATUS_IS_OK(status
)) {
3450 d_printf("Error %s getting volume info\n", nt_errstr(status
));
3454 d_printf("Volume: |%s| serial number 0x%x\n",
3455 volname
, (unsigned int)serial_num
);
3459 /****************************************************************************
3460 Hard link files using the NT call.
3461 ****************************************************************************/
3463 static int cmd_hardlink(void)
3465 TALLOC_CTX
*ctx
= talloc_tos();
3468 struct cli_state
*targetcli
;
3471 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
) ||
3472 !next_token_talloc(ctx
, &cmd_ptr
,&buf2
,NULL
)) {
3473 d_printf("hardlink <src> <dest>\n");
3477 src
= talloc_asprintf(ctx
,
3479 client_get_cur_dir(),
3485 dest
= talloc_asprintf(ctx
,
3487 client_get_cur_dir(),
3493 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, src
, &targetcli
, &targetname
)) {
3494 d_printf("hardlink %s: %s\n", src
, cli_errstr(cli
));
3498 if (!NT_STATUS_IS_OK(cli_nt_hardlink(targetcli
, targetname
, dest
))) {
3499 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli
));
3506 /****************************************************************************
3507 Toggle the prompt flag.
3508 ****************************************************************************/
3510 static int cmd_prompt(void)
3513 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
3517 /****************************************************************************
3518 Set the newer than time.
3519 ****************************************************************************/
3521 static int cmd_newer(void)
3523 TALLOC_CTX
*ctx
= talloc_tos();
3526 SMB_STRUCT_STAT sbuf
;
3528 ok
= next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
);
3529 if (ok
&& (sys_stat(buf
, &sbuf
, false) == 0)) {
3530 newer_than
= convert_timespec_to_time_t(sbuf
.st_ex_mtime
);
3531 DEBUG(1,("Getting files newer than %s",
3532 time_to_asc(newer_than
)));
3537 if (ok
&& newer_than
== 0) {
3538 d_printf("Error setting newer-than time\n");
3545 /****************************************************************************
3546 Set the archive level.
3547 ****************************************************************************/
3549 static int cmd_archive(void)
3551 TALLOC_CTX
*ctx
= talloc_tos();
3554 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3555 archive_level
= atoi(buf
);
3557 d_printf("Archive level is %d\n",archive_level
);
3563 /****************************************************************************
3564 Toggle the lowercaseflag.
3565 ****************************************************************************/
3567 static int cmd_lowercase(void)
3569 lowercase
= !lowercase
;
3570 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
3574 /****************************************************************************
3575 Toggle the case sensitive flag.
3576 ****************************************************************************/
3578 static int cmd_setcase(void)
3580 bool orig_case_sensitive
= cli_set_case_sensitive(cli
, false);
3582 cli_set_case_sensitive(cli
, !orig_case_sensitive
);
3583 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive
?
3588 /****************************************************************************
3589 Toggle the showacls flag.
3590 ****************************************************************************/
3592 static int cmd_showacls(void)
3594 showacls
= !showacls
;
3595 DEBUG(2,("showacls is now %s\n",showacls
?"on":"off"));
3600 /****************************************************************************
3601 Toggle the recurse flag.
3602 ****************************************************************************/
3604 static int cmd_recurse(void)
3607 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
3611 /****************************************************************************
3612 Toggle the translate flag.
3613 ****************************************************************************/
3615 static int cmd_translate(void)
3617 translation
= !translation
;
3618 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
3619 translation
?"on":"off"));
3623 /****************************************************************************
3625 ****************************************************************************/
3627 static int cmd_lcd(void)
3629 TALLOC_CTX
*ctx
= talloc_tos();
3633 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3634 if (chdir(buf
) == -1) {
3635 d_printf("chdir to %s failed (%s)\n",
3636 buf
, strerror(errno
));
3639 d
= TALLOC_ARRAY(ctx
, char, PATH_MAX
+1);
3643 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
3647 /****************************************************************************
3648 Get a file restarting at end of local file.
3649 ****************************************************************************/
3651 static int cmd_reget(void)
3653 TALLOC_CTX
*ctx
= talloc_tos();
3654 char *local_name
= NULL
;
3655 char *remote_name
= NULL
;
3659 remote_name
= talloc_strdup(ctx
, client_get_cur_dir());
3664 if (!next_token_talloc(ctx
, &cmd_ptr
, &fname
, NULL
)) {
3665 d_printf("reget <filename>\n");
3668 remote_name
= talloc_asprintf_append(remote_name
, "%s", fname
);
3672 remote_name
= clean_name(ctx
,remote_name
);
3678 next_token_talloc(ctx
, &cmd_ptr
, &p
, NULL
);
3683 return do_get(remote_name
, local_name
, true);
3686 /****************************************************************************
3687 Put a file restarting at end of local file.
3688 ****************************************************************************/
3690 static int cmd_reput(void)
3692 TALLOC_CTX
*ctx
= talloc_tos();
3693 char *local_name
= NULL
;
3694 char *remote_name
= NULL
;
3698 remote_name
= talloc_strdup(ctx
, client_get_cur_dir());
3703 if (!next_token_talloc(ctx
, &cmd_ptr
, &local_name
, NULL
)) {
3704 d_printf("reput <filename>\n");
3708 if (!file_exist_stat(local_name
, &st
, false)) {
3709 d_printf("%s does not exist\n", local_name
);
3713 if (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
3714 remote_name
= talloc_asprintf_append(remote_name
,
3717 remote_name
= talloc_asprintf_append(remote_name
,
3724 remote_name
= clean_name(ctx
, remote_name
);
3729 return do_put(remote_name
, local_name
, true);
3732 /****************************************************************************
3734 ****************************************************************************/
3736 static void browse_fn(const char *name
, uint32 m
,
3737 const char *comment
, void *state
)
3739 const char *typestr
= "";
3742 case STYPE_DISKTREE
:
3746 typestr
= "Printer";
3755 /* FIXME: If the remote machine returns non-ascii characters
3756 in any of these fields, they can corrupt the output. We
3757 should remove them. */
3759 d_printf("\t%-15s %-10.10s%s\n",
3760 name
,typestr
,comment
);
3762 d_printf ("%s|%s|%s\n",typestr
,name
,comment
);
3766 static bool browse_host_rpc(bool sort
)
3769 struct rpc_pipe_client
*pipe_hnd
= NULL
;
3770 TALLOC_CTX
*frame
= talloc_stackframe();
3772 struct srvsvc_NetShareInfoCtr info_ctr
;
3773 struct srvsvc_NetShareCtr1 ctr1
;
3774 uint32_t resume_handle
= 0;
3775 uint32_t total_entries
= 0;
3778 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_srvsvc
.syntax_id
,
3781 if (!NT_STATUS_IS_OK(status
)) {
3782 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
3783 nt_errstr(status
)));
3788 ZERO_STRUCT(info_ctr
);
3792 info_ctr
.ctr
.ctr1
= &ctr1
;
3794 status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, frame
,
3802 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(werr
)) {
3803 TALLOC_FREE(pipe_hnd
);
3808 for (i
=0; i
< info_ctr
.ctr
.ctr1
->count
; i
++) {
3809 struct srvsvc_NetShareInfo1 info
= info_ctr
.ctr
.ctr1
->array
[i
];
3810 browse_fn(info
.name
, info
.type
, info
.comment
, NULL
);
3813 TALLOC_FREE(pipe_hnd
);
3818 /****************************************************************************
3819 Try and browse available connections on a host.
3820 ****************************************************************************/
3822 static bool browse_host(bool sort
)
3826 d_printf("\n\tSharename Type Comment\n");
3827 d_printf("\t--------- ---- -------\n");
3830 if (browse_host_rpc(sort
)) {
3834 if((ret
= cli_RNetShareEnum(cli
, browse_fn
, NULL
)) == -1)
3835 d_printf("Error returning browse list: %s\n", cli_errstr(cli
));
3840 /****************************************************************************
3842 ****************************************************************************/
3844 static void server_fn(const char *name
, uint32 m
,
3845 const char *comment
, void *state
)
3849 d_printf("\t%-16s %s\n", name
, comment
);
3851 d_printf("%s|%s|%s\n",(char *)state
, name
, comment
);
3855 /****************************************************************************
3856 Try and browse available connections on a host.
3857 ****************************************************************************/
3859 static bool list_servers(const char *wk_grp
)
3863 if (!cli
->server_domain
)
3867 d_printf("\n\tServer Comment\n");
3868 d_printf("\t--------- -------\n");
3870 fstrcpy( state
, "Server" );
3871 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
,
3875 d_printf("\n\tWorkgroup Master\n");
3876 d_printf("\t--------- -------\n");
3879 fstrcpy( state
, "Workgroup" );
3880 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
,
3885 /****************************************************************************
3886 Print or set current VUID
3887 ****************************************************************************/
3889 static int cmd_vuid(void)
3891 TALLOC_CTX
*ctx
= talloc_tos();
3894 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3895 d_printf("Current VUID is %d\n", cli
->vuid
);
3899 cli
->vuid
= atoi(buf
);
3903 /****************************************************************************
3904 Setup a new VUID, by issuing a session setup
3905 ****************************************************************************/
3907 static int cmd_logon(void)
3909 TALLOC_CTX
*ctx
= talloc_tos();
3910 char *l_username
, *l_password
;
3912 if (!next_token_talloc(ctx
, &cmd_ptr
,&l_username
,NULL
)) {
3913 d_printf("logon <username> [<password>]\n");
3917 if (!next_token_talloc(ctx
, &cmd_ptr
,&l_password
,NULL
)) {
3918 char *pass
= getpass("Password: ");
3920 l_password
= talloc_strdup(ctx
,pass
);
3927 if (!NT_STATUS_IS_OK(cli_session_setup(cli
, l_username
,
3928 l_password
, strlen(l_password
),
3929 l_password
, strlen(l_password
),
3931 d_printf("session setup failed: %s\n", cli_errstr(cli
));
3935 d_printf("Current VUID is %d\n", cli
->vuid
);
3940 /****************************************************************************
3941 list active connections
3942 ****************************************************************************/
3944 static int cmd_list_connect(void)
3946 cli_cm_display(cli
);
3950 /****************************************************************************
3951 display the current active client connection
3952 ****************************************************************************/
3954 static int cmd_show_connect( void )
3956 TALLOC_CTX
*ctx
= talloc_tos();
3957 struct cli_state
*targetcli
;
3960 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, client_get_cur_dir(),
3961 &targetcli
, &targetpath
) ) {
3962 d_printf("showconnect %s: %s\n", cur_dir
, cli_errstr(cli
));
3966 d_printf("//%s/%s\n", targetcli
->desthost
, targetcli
->share
);
3970 /****************************************************************************
3972 ***************************************************************************/
3974 int cmd_iosize(void)
3976 TALLOC_CTX
*ctx
= talloc_tos();
3980 if (!next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
3982 d_printf("iosize <n> or iosize 0x<n>. "
3983 "Minimum is 16384 (0x4000), "
3984 "max is 16776960 (0xFFFF00)\n");
3986 d_printf("iosize <n> or iosize 0x<n>. "
3987 "(Encrypted connection) ,"
3988 "Minimum is 16384 (0x4000), "
3989 "max is 130048 (0x1FC00)\n");
3994 iosize
= strtol(buf
,NULL
,0);
3995 if (smb_encrypt
&& (iosize
< 0x4000 || iosize
> 0xFC00)) {
3996 d_printf("iosize out of range for encrypted "
3997 "connection (min = 16384 (0x4000), "
3998 "max = 130048 (0x1FC00)");
4000 } else if (!smb_encrypt
&& (iosize
< 0x4000 || iosize
> 0xFFFF00)) {
4001 d_printf("iosize out of range (min = 16384 (0x4000), "
4002 "max = 16776960 (0xFFFF00)");
4006 io_bufsize
= iosize
;
4007 d_printf("iosize is now %d\n", io_bufsize
);
4011 /****************************************************************************
4013 ****************************************************************************/
4014 static int cmd_history(void)
4016 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
4020 hlist
= history_list();
4022 for (i
= 0; hlist
&& hlist
[i
]; i
++) {
4023 DEBUG(0, ("%d: %s\n", i
, hlist
[i
]->line
));
4026 DEBUG(0,("no history without readline support\n"));
4032 /* Some constants for completing filename arguments */
4034 #define COMPL_NONE 0 /* No completions */
4035 #define COMPL_REMOTE 1 /* Complete remote filename */
4036 #define COMPL_LOCAL 2 /* Complete local filename */
4038 /* This defines the commands supported by this client.
4039 * NOTE: The "!" must be the last one in the list because it's fn pointer
4040 * field is NULL, and NULL in that field is used in process_tok()
4041 * (below) to indicate the end of the list. crh
4046 const char *description
;
4047 char compl_args
[2]; /* Completion argument info */
4049 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
4050 {"allinfo",cmd_allinfo
,"<file> show all available info",
4051 {COMPL_NONE
,COMPL_NONE
}},
4052 {"altname",cmd_altname
,"<file> show alt name",{COMPL_NONE
,COMPL_NONE
}},
4053 {"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
}},
4054 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
4055 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
4056 {"case_sensitive",cmd_setcase
,"toggle the case sensitive flag to server",{COMPL_NONE
,COMPL_NONE
}},
4057 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
4058 {"chmod",cmd_chmod
,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE
,COMPL_REMOTE
}},
4059 {"chown",cmd_chown
,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE
,COMPL_REMOTE
}},
4060 {"close",cmd_close
,"<fid> close a file given a fid",{COMPL_REMOTE
,COMPL_REMOTE
}},
4061 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4062 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
4063 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
4064 {"echo",cmd_echo
,"ping the server",{COMPL_NONE
,COMPL_NONE
}},
4065 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
4066 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
4067 {"getfacl",cmd_getfacl
,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE
,COMPL_LOCAL
}},
4068 {"hardlink",cmd_hardlink
,"<src> <dest> create a Windows hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
4069 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
4070 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
4071 {"iosize",cmd_iosize
,"iosize <number> (default 64512)",{COMPL_NONE
,COMPL_NONE
}},
4072 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
4073 {"link",cmd_link
,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
4074 {"lock",cmd_lock
,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
4075 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
4076 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
4077 {"l",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
4078 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
4079 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
4080 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4081 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
4082 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
4083 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4084 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
4085 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
4086 {"posix", cmd_posix
, "turn on all POSIX capabilities", {COMPL_REMOTE
,COMPL_NONE
}},
4087 {"posix_encrypt",cmd_posix_encrypt
,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE
,COMPL_NONE
}},
4088 {"posix_open",cmd_posix_open
,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
4089 {"posix_mkdir",cmd_posix_mkdir
,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
4090 {"posix_rmdir",cmd_posix_rmdir
,"<name> removes a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
4091 {"posix_unlink",cmd_posix_unlink
,"<name> removes a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
4092 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
4093 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
4094 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
4095 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
4096 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
4097 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
4098 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
4099 {"readlink",cmd_readlink
,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
4100 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
4101 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
4102 {"reget",cmd_reget
,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE
,COMPL_LOCAL
}},
4103 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
4104 {"reput",cmd_reput
,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL
,COMPL_REMOTE
}},
4105 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4106 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
4107 {"showacls",cmd_showacls
,"toggle if ACLs are shown or not",{COMPL_NONE
,COMPL_NONE
}},
4108 {"setmode",cmd_setmode
,"filename <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
4109 {"stat",cmd_stat
,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE
,COMPL_REMOTE
}},
4110 {"symlink",cmd_symlink
,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
4111 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
4112 {"tarmode",cmd_tarmode
,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
4113 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
4114 {"unlock",cmd_unlock
,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
4115 {"volume",cmd_volume
,"print the volume name",{COMPL_NONE
,COMPL_NONE
}},
4116 {"vuid",cmd_vuid
,"change current vuid",{COMPL_NONE
,COMPL_NONE
}},
4117 {"wdel",cmd_wdel
,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
4118 {"logon",cmd_logon
,"establish new logon",{COMPL_NONE
,COMPL_NONE
}},
4119 {"listconnect",cmd_list_connect
,"list open connections",{COMPL_NONE
,COMPL_NONE
}},
4120 {"showconnect",cmd_show_connect
,"display the current active connection",{COMPL_NONE
,COMPL_NONE
}},
4121 {"..",cmd_cd_oneup
,"change the remote directory (up one level)",{COMPL_REMOTE
,COMPL_NONE
}},
4123 /* Yes, this must be here, see crh's comment above. */
4124 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
4125 {NULL
,NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
4128 /*******************************************************************
4129 Lookup a command string in the list of commands, including
4131 ******************************************************************/
4133 static int process_tok(char *tok
)
4135 int i
= 0, matches
= 0;
4137 int tok_len
= strlen(tok
);
4139 while (commands
[i
].fn
!= NULL
) {
4140 if (strequal(commands
[i
].name
,tok
)) {
4144 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
4153 else if (matches
== 1)
4159 /****************************************************************************
4161 ****************************************************************************/
4163 static int cmd_help(void)
4165 TALLOC_CTX
*ctx
= talloc_tos();
4169 if (next_token_talloc(ctx
, &cmd_ptr
,&buf
,NULL
)) {
4170 if ((i
= process_tok(buf
)) >= 0)
4171 d_printf("HELP %s:\n\t%s\n\n",
4172 commands
[i
].name
,commands
[i
].description
);
4174 while (commands
[i
].description
) {
4175 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
4176 d_printf("%-15s",commands
[i
].name
);
4185 /****************************************************************************
4186 Process a -c command string.
4187 ****************************************************************************/
4189 static int process_command_string(const char *cmd_in
)
4191 TALLOC_CTX
*ctx
= talloc_tos();
4192 char *cmd
= talloc_strdup(ctx
, cmd_in
);
4198 /* establish the connection if not already */
4201 cli
= cli_cm_open(talloc_tos(), NULL
,
4202 have_ip
? dest_ss_str
: desthost
,
4205 max_protocol
, port
, name_type
);
4211 while (cmd
[0] != '\0') {
4217 if ((p
= strchr_m(cmd
, ';')) == 0) {
4226 /* and get the first part of the command */
4228 if (!next_token_talloc(ctx
, &cmd_ptr
,&tok
,NULL
)) {
4232 if ((i
= process_tok(tok
)) >= 0) {
4233 rc
= commands
[i
].fn();
4234 } else if (i
== -2) {
4235 d_printf("%s: command abbreviation ambiguous\n",tok
);
4237 d_printf("%s: command not found\n",tok
);
4244 #define MAX_COMPLETIONS 100
4246 struct completion_remote
{
4254 static void completion_remote_filter(const char *mnt
,
4255 struct file_info
*f
,
4259 struct completion_remote
*info
= (struct completion_remote
*)state
;
4261 if (info
->count
>= MAX_COMPLETIONS
- 1) {
4264 if (strncmp(info
->text
, f
->name
, info
->len
) != 0) {
4267 if (ISDOT(f
->name
) || ISDOTDOT(f
->name
)) {
4271 if ((info
->dirmask
[0] == 0) && !(f
->mode
& aDIR
))
4272 info
->matches
[info
->count
] = SMB_STRDUP(f
->name
);
4274 TALLOC_CTX
*ctx
= talloc_stackframe();
4277 tmp
= talloc_strdup(ctx
,info
->dirmask
);
4282 tmp
= talloc_asprintf_append(tmp
, "%s", f
->name
);
4287 if (f
->mode
& aDIR
) {
4288 tmp
= talloc_asprintf_append(tmp
, "%s",
4295 info
->matches
[info
->count
] = SMB_STRDUP(tmp
);
4298 if (info
->matches
[info
->count
] == NULL
) {
4301 if (f
->mode
& aDIR
) {
4302 smb_readline_ca_char(0);
4304 if (info
->count
== 1) {
4305 info
->samelen
= strlen(info
->matches
[info
->count
]);
4307 while (strncmp(info
->matches
[info
->count
],
4308 info
->matches
[info
->count
-1],
4309 info
->samelen
) != 0) {
4316 static char **remote_completion(const char *text
, int len
)
4318 TALLOC_CTX
*ctx
= talloc_stackframe();
4319 char *dirmask
= NULL
;
4320 char *targetpath
= NULL
;
4321 struct cli_state
*targetcli
= NULL
;
4323 struct completion_remote info
= { NULL
, NULL
, 1, 0, NULL
, 0 };
4326 /* can't have non-static initialisation on Sun CC, so do it
4332 info
.matches
= SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS
);
4333 if (!info
.matches
) {
4339 * We're leaving matches[0] free to fill it later with the text to
4340 * display: Either the one single match or the longest common subset
4343 info
.matches
[0] = NULL
;
4346 for (i
= len
-1; i
>= 0; i
--) {
4347 if ((text
[i
] == '/') || (text
[i
] == CLI_DIRSEP_CHAR
)) {
4352 info
.text
= text
+i
+1;
4353 info
.samelen
= info
.len
= len
-i
-1;
4356 info
.dirmask
= SMB_MALLOC_ARRAY(char, i
+2);
4357 if (!info
.dirmask
) {
4360 strncpy(info
.dirmask
, text
, i
+1);
4361 info
.dirmask
[i
+1] = 0;
4362 dirmask
= talloc_asprintf(ctx
,
4364 client_get_cur_dir(),
4368 info
.dirmask
= SMB_STRDUP("");
4369 if (!info
.dirmask
) {
4372 dirmask
= talloc_asprintf(ctx
,
4374 client_get_cur_dir());
4380 if (!cli_resolve_path(ctx
, "", auth_info
, cli
, dirmask
, &targetcli
, &targetpath
)) {
4383 status
= cli_list(targetcli
, targetpath
, aDIR
| aSYSTEM
| aHIDDEN
,
4384 completion_remote_filter
, (void *)&info
);
4385 if (!NT_STATUS_IS_OK(status
)) {
4389 if (info
.count
== 1) {
4391 * No matches at all, NULL indicates there is nothing
4393 SAFE_FREE(info
.matches
[0]);
4394 SAFE_FREE(info
.matches
);
4399 if (info
.count
== 2) {
4401 * Exactly one match in matches[1], indicate this is the one
4404 info
.matches
[0] = info
.matches
[1];
4405 info
.matches
[1] = NULL
;
4408 return info
.matches
;
4412 * We got more than one possible match, set the result to the maximum
4416 info
.matches
[0] = SMB_STRNDUP(info
.matches
[1], info
.samelen
);
4417 info
.matches
[info
.count
] = NULL
;
4418 return info
.matches
;
4421 for (i
= 0; i
< info
.count
; i
++) {
4422 SAFE_FREE(info
.matches
[i
]);
4424 SAFE_FREE(info
.matches
);
4425 SAFE_FREE(info
.dirmask
);
4430 static char **completion_fn(const char *text
, int start
, int end
)
4432 smb_readline_ca_char(' ');
4435 const char *buf
, *sp
;
4439 buf
= smb_readline_get_line_buffer();
4443 sp
= strchr(buf
, ' ');
4447 for (i
= 0; commands
[i
].name
; i
++) {
4448 if ((strncmp(commands
[i
].name
, buf
, sp
- buf
) == 0) &&
4449 (commands
[i
].name
[sp
- buf
] == 0)) {
4453 if (commands
[i
].name
== NULL
)
4459 if (sp
== (buf
+ start
))
4460 compl_type
= commands
[i
].compl_args
[0];
4462 compl_type
= commands
[i
].compl_args
[1];
4464 if (compl_type
== COMPL_REMOTE
)
4465 return remote_completion(text
, end
- start
);
4466 else /* fall back to local filename completion */
4470 int i
, len
, samelen
= 0, count
=1;
4472 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
4479 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
4480 if (strncmp(text
, commands
[i
].name
, len
) == 0) {
4481 matches
[count
] = SMB_STRDUP(commands
[i
].name
);
4482 if (!matches
[count
])
4485 samelen
= strlen(matches
[count
]);
4487 while (strncmp(matches
[count
], matches
[count
-1], samelen
) != 0)
4494 case 0: /* should never happen */
4498 matches
[0] = SMB_STRDUP(matches
[1]);
4501 matches
[0] = (char *)SMB_MALLOC(samelen
+1);
4504 strncpy(matches
[0], matches
[1], samelen
);
4505 matches
[0][samelen
] = 0;
4507 matches
[count
] = NULL
;
4511 for (i
= 0; i
< count
; i
++)
4519 static bool finished
;
4521 /****************************************************************************
4522 Make sure we swallow keepalives during idle time.
4523 ****************************************************************************/
4525 static void readline_callback(void)
4528 struct timeval timeout
;
4529 static time_t last_t
;
4530 struct timespec now
;
4533 clock_gettime_mono(&now
);
4547 FD_SET(cli
->fd
,&fds
);
4550 timeout
.tv_usec
= 0;
4551 sys_select_intr(cli
->fd
+1,&fds
,NULL
,NULL
,&timeout
);
4553 /* We deliberately use receive_smb_raw instead of
4554 client_receive_smb as we want to receive
4555 session keepalives and then drop them here.
4557 if (FD_ISSET(cli
->fd
,&fds
)) {
4561 set_smb_read_error(&cli
->smb_rw_error
, SMB_READ_OK
);
4563 status
= receive_smb_raw(cli
->fd
, cli
->inbuf
, cli
->bufsize
, 0, 0, &len
);
4565 if (!NT_STATUS_IS_OK(status
)) {
4566 DEBUG(0, ("Read from server failed, maybe it closed "
4567 "the connection\n"));
4570 smb_readline_done();
4571 if (NT_STATUS_EQUAL(status
, NT_STATUS_END_OF_FILE
)) {
4572 set_smb_read_error(&cli
->smb_rw_error
,
4577 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
4578 set_smb_read_error(&cli
->smb_rw_error
,
4583 set_smb_read_error(&cli
->smb_rw_error
, SMB_READ_ERROR
);
4586 if(CVAL(cli
->inbuf
,0) != SMBkeepalive
) {
4587 DEBUG(0, ("Read from server "
4588 "returned unexpected packet!\n"));
4595 /* Ping the server to keep the connection alive using SMBecho. */
4598 unsigned char garbage
[16];
4599 memset(garbage
, 0xf0, sizeof(garbage
));
4600 status
= cli_echo(cli
, 1, data_blob_const(garbage
, sizeof(garbage
)));
4602 if (!NT_STATUS_IS_OK(status
)) {
4603 DEBUG(0, ("SMBecho failed. Maybe server has closed "
4604 "the connection\n"));
4606 smb_readline_done();
4611 /****************************************************************************
4612 Process commands on stdin.
4613 ****************************************************************************/
4615 static int process_stdin(void)
4620 TALLOC_CTX
*frame
= talloc_stackframe();
4622 char *the_prompt
= NULL
;
4626 /* display a prompt */
4627 if (asprintf(&the_prompt
, "smb: %s> ", client_get_cur_dir()) < 0) {
4631 line
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
4632 SAFE_FREE(the_prompt
);
4638 /* special case - first char is ! */
4640 if (system(line
+ 1) == -1) {
4641 d_printf("system() command %s failed.\n",
4649 /* and get the first part of the command */
4651 if (!next_token_talloc(frame
, &cmd_ptr
,&tok
,NULL
)) {
4657 if ((i
= process_tok(tok
)) >= 0) {
4658 rc
= commands
[i
].fn();
4659 } else if (i
== -2) {
4660 d_printf("%s: command abbreviation ambiguous\n",tok
);
4662 d_printf("%s: command not found\n",tok
);
4670 /****************************************************************************
4671 Process commands from the client.
4672 ****************************************************************************/
4674 static int process(const char *base_directory
)
4678 cli
= cli_cm_open(talloc_tos(), NULL
,
4679 have_ip
? dest_ss_str
: desthost
,
4680 service
, auth_info
, true, smb_encrypt
,
4681 max_protocol
, port
, name_type
);
4686 if (base_directory
&& *base_directory
) {
4687 rc
= do_cd(base_directory
);
4695 rc
= process_command_string(cmdstr
);
4704 /****************************************************************************
4706 ****************************************************************************/
4708 static int do_host_query(const char *query_host
)
4710 cli
= cli_cm_open(talloc_tos(), NULL
,
4711 have_ip
? dest_ss_str
: query_host
, "IPC$", auth_info
, true, smb_encrypt
,
4712 max_protocol
, port
, name_type
);
4718 /* Ensure that the host can do IPv4 */
4720 if (!interpret_addr(query_host
)) {
4721 struct sockaddr_storage ss
;
4722 if (interpret_string_addr(&ss
, query_host
, 0) &&
4723 (ss
.ss_family
!= AF_INET
)) {
4724 d_printf("%s is an IPv6 address -- no workgroup available\n",
4732 /* Workgroups simply don't make sense over anything
4733 else but port 139... */
4736 cli
= cli_cm_open(talloc_tos(), NULL
,
4737 have_ip
? dest_ss_str
: query_host
, "IPC$",
4738 auth_info
, true, smb_encrypt
,
4739 max_protocol
, 139, name_type
);
4743 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
4747 list_servers(lp_workgroup());
4754 /****************************************************************************
4755 Handle a tar operation.
4756 ****************************************************************************/
4758 static int do_tar_op(const char *base_directory
)
4762 /* do we already have a connection? */
4764 cli
= cli_cm_open(talloc_tos(), NULL
,
4765 have_ip
? dest_ss_str
: desthost
,
4766 service
, auth_info
, true, smb_encrypt
,
4767 max_protocol
, port
, name_type
);
4774 if (base_directory
&& *base_directory
) {
4775 ret
= do_cd(base_directory
);
4789 /****************************************************************************
4790 Handle a message operation.
4791 ****************************************************************************/
4793 static int do_message_op(struct user_auth_info
*a_info
)
4795 struct sockaddr_storage ss
;
4796 struct nmb_name called
, calling
;
4797 fstring server_name
;
4798 char name_type_hex
[10];
4802 make_nmb_name(&calling
, calling_name
, 0x0);
4803 make_nmb_name(&called
, desthost
, name_type
);
4805 fstrcpy(server_name
, desthost
);
4806 snprintf(name_type_hex
, sizeof(name_type_hex
), "#%X", name_type
);
4807 fstrcat(server_name
, name_type_hex
);
4813 /* we can only do messages over port 139 (to windows clients at least) */
4815 msg_port
= port
? port
: 139;
4817 if (!(cli
=cli_initialise())) {
4818 d_printf("Connection to %s failed\n", desthost
);
4821 cli_set_port(cli
, msg_port
);
4823 status
= cli_connect(cli
, server_name
, &ss
);
4824 if (!NT_STATUS_IS_OK(status
)) {
4825 d_printf("Connection to %s failed. Error %s\n", desthost
, nt_errstr(status
));
4829 if (!cli_session_request(cli
, &calling
, &called
)) {
4830 d_printf("session request failed\n");
4835 send_message(get_cmdline_auth_info_username(a_info
));
4841 /****************************************************************************
4843 ****************************************************************************/
4845 int main(int argc
,char *argv
[])
4847 char *base_directory
= NULL
;
4849 char *query_host
= NULL
;
4850 bool message
= false;
4851 static const char *new_name_resolve_order
= NULL
;
4855 fstring new_workgroup
;
4856 bool tar_opt
= false;
4857 bool service_opt
= false;
4858 struct poptOption long_options
[] = {
4861 { "name-resolve", 'R', POPT_ARG_STRING
, &new_name_resolve_order
, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
4862 { "message", 'M', POPT_ARG_STRING
, NULL
, 'M', "Send message", "HOST" },
4863 { "ip-address", 'I', POPT_ARG_STRING
, NULL
, 'I', "Use this IP to connect to", "IP" },
4864 { "stderr", 'E', POPT_ARG_NONE
, NULL
, 'E', "Write messages to stderr instead of stdout" },
4865 { "list", 'L', POPT_ARG_STRING
, NULL
, 'L', "Get a list of shares available on a host", "HOST" },
4866 { "max-protocol", 'm', POPT_ARG_STRING
, NULL
, 'm', "Set the max protocol level", "LEVEL" },
4867 { "tar", 'T', POPT_ARG_STRING
, NULL
, 'T', "Command line tar", "<c|x>IXFqgbNan" },
4868 { "directory", 'D', POPT_ARG_STRING
, NULL
, 'D', "Start from directory", "DIR" },
4869 { "command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated commands" },
4870 { "send-buffer", 'b', POPT_ARG_INT
, &io_bufsize
, 'b', "Changes the transmit/send buffer", "BYTES" },
4871 { "port", 'p', POPT_ARG_INT
, &port
, 'p', "Port to connect to", "PORT" },
4872 { "grepable", 'g', POPT_ARG_NONE
, NULL
, 'g', "Produce grepable output" },
4873 { "browse", 'B', POPT_ARG_NONE
, NULL
, 'B', "Browse SMB servers using DNS" },
4875 POPT_COMMON_CONNECTION
4876 POPT_COMMON_CREDENTIALS
4879 TALLOC_CTX
*frame
= talloc_stackframe();
4881 if (!client_set_cur_dir("\\")) {
4885 /* initialize the workgroup name so we can determine whether or
4886 not it was set by a command line option */
4888 set_global_myworkgroup( "" );
4889 set_global_myname( "" );
4891 /* set default debug level to 1 regardless of what smb.conf sets */
4892 setup_logging( "smbclient", true );
4893 DEBUGLEVEL_CLASS
[DBGC_ALL
] = 1;
4894 if ((dbf
= x_fdup(x_stderr
))) {
4895 x_setbuf( dbf
, NULL
);
4900 auth_info
= user_auth_info_init(frame
);
4901 if (auth_info
== NULL
) {
4904 popt_common_set_auth_info(auth_info
);
4907 pc
= poptGetContext("smbclient", argc
, (const char **) argv
, long_options
, 0);
4908 poptSetOtherOptionHelp(pc
, "service <password>");
4910 lp_set_in_client(true); /* Make sure that we tell lp_load we are */
4912 while ((opt
= poptGetNextOpt(pc
)) != -1) {
4914 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
4915 /* I see no other way to keep things sane --SSS */
4916 if (tar_opt
== true) {
4917 while (poptPeekArg(pc
)) {
4923 /* if the service has not yet been specified lets see if it is available in the popt stack */
4924 if (!service_opt
&& poptPeekArg(pc
)) {
4925 service
= talloc_strdup(frame
, poptGetArg(pc
));
4932 /* if the service has already been retrieved then check if we have also a password */
4934 && (!get_cmdline_auth_info_got_pass(auth_info
))
4935 && poptPeekArg(pc
)) {
4936 set_cmdline_auth_info_password(auth_info
,
4942 /* Messages are sent to NetBIOS name type 0x3
4943 * (Messenger Service). Make sure we default
4944 * to port 139 instead of port 445. srl,crh
4947 desthost
= talloc_strdup(frame
,poptGetOptArg(pc
));
4957 if (!interpret_string_addr(&dest_ss
, poptGetOptArg(pc
), 0)) {
4961 print_sockaddr(dest_ss_str
, sizeof(dest_ss_str
), &dest_ss
);
4969 display_set_stderr();
4973 query_host
= talloc_strdup(frame
, poptGetOptArg(pc
));
4979 max_protocol
= interpret_protocol(poptGetOptArg(pc
), max_protocol
);
4982 /* We must use old option processing for this. Find the
4983 * position of the -T option in the raw argv[]. */
4986 for (i
= 1; i
< argc
; i
++) {
4987 if (strncmp("-T", argv
[i
],2)==0)
4991 if (!tar_parseargs(argc
, argv
, poptGetOptArg(pc
), i
)) {
4992 poptPrintUsage(pc
, stderr
, 0);
4996 /* this must be the last option, mark we have parsed it so that we know we have */
5000 base_directory
= talloc_strdup(frame
, poptGetOptArg(pc
));
5001 if (!base_directory
) {
5012 return(do_smb_browse());
5017 /* We may still have some leftovers after the last popt option has been called */
5018 if (tar_opt
== true) {
5019 while (poptPeekArg(pc
)) {
5025 /* if the service has not yet been specified lets see if it is available in the popt stack */
5026 if (!service_opt
&& poptPeekArg(pc
)) {
5027 service
= talloc_strdup(frame
,poptGetArg(pc
));
5034 /* if the service has already been retrieved then check if we have also a password */
5036 && !get_cmdline_auth_info_got_pass(auth_info
)
5037 && poptPeekArg(pc
)) {
5038 set_cmdline_auth_info_password(auth_info
,
5043 * Don't load debug level from smb.conf. It should be
5044 * set by cmdline arg or remain default (0)
5046 AllowDebugChange
= false;
5048 /* save the workgroup...
5050 FIXME!! do we need to do this for other options as well
5051 (or maybe a generic way to keep lp_load() from overwriting
5054 fstrcpy( new_workgroup
, lp_workgroup() );
5055 calling_name
= talloc_strdup(frame
, global_myname() );
5056 if (!calling_name
) {
5060 if ( override_logfile
)
5061 setup_logging( lp_logfile(), false );
5063 if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
5064 fprintf(stderr
, "%s: Can't load %s - run testparm to debug it\n",
5065 argv
[0], get_dyn_CONFIGFILE());
5068 if (get_cmdline_auth_info_use_machine_account(auth_info
) &&
5069 !set_cmdline_auth_info_machine_account_creds(auth_info
)) {
5075 if (service_opt
&& service
) {
5078 /* Convert any '/' characters in the service name to '\' characters */
5079 string_replace(service
, '/','\\');
5080 if (count_chars(service
,'\\') < 3) {
5081 d_printf("\n%s: Not enough '\\' characters in service\n",service
);
5082 poptPrintUsage(pc
, stderr
, 0);
5085 /* Remove trailing slashes */
5086 len
= strlen(service
);
5087 while(len
> 0 && service
[len
- 1] == '\\') {
5089 service
[len
] = '\0';
5093 if ( strlen(new_workgroup
) != 0 ) {
5094 set_global_myworkgroup( new_workgroup
);
5097 if ( strlen(calling_name
) != 0 ) {
5098 set_global_myname( calling_name
);
5100 TALLOC_FREE(calling_name
);
5101 calling_name
= talloc_strdup(frame
, global_myname() );
5104 smb_encrypt
= get_cmdline_auth_info_smb_encrypt(auth_info
);
5105 if (!init_names()) {
5106 fprintf(stderr
, "init_names() failed\n");
5110 if(new_name_resolve_order
)
5111 lp_set_name_resolve_order(new_name_resolve_order
);
5113 if (!tar_type
&& !query_host
&& !service
&& !message
) {
5114 poptPrintUsage(pc
, stderr
, 0);
5118 poptFreeContext(pc
);
5120 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
5122 /* Ensure we have a password (or equivalent). */
5123 set_cmdline_auth_info_getpass(auth_info
);
5127 process_command_string(cmdstr
);
5128 return do_tar_op(base_directory
);
5131 if (query_host
&& *query_host
) {
5132 char *qhost
= query_host
;
5135 while (*qhost
== '\\' || *qhost
== '/')
5138 if ((slash
= strchr_m(qhost
, '/'))
5139 || (slash
= strchr_m(qhost
, '\\'))) {
5143 if ((p
=strchr_m(qhost
, '#'))) {
5146 sscanf(p
, "%x", &name_type
);
5149 return do_host_query(qhost
);
5153 return do_message_op(auth_info
);
5156 if (process(base_directory
)) {