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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "../client/client_proto.h"
31 struct cli_state
*cli
;
32 extern BOOL in_client
;
34 pstring cur_dir
= "\\";
35 static pstring cd_path
= "";
36 static pstring service
;
37 static pstring desthost
;
38 static pstring username
;
39 static pstring password
;
40 static BOOL use_kerberos
;
42 static char *cmdstr
= NULL
;
44 static int io_bufsize
= 64512;
46 static int name_type
= 0x20;
47 static int max_protocol
= PROTOCOL_NT1
;
49 static int process_tok(fstring tok
);
50 static int cmd_help(void);
52 /* 30 second timeout on most commands */
53 #define CLIENT_TIMEOUT (30*1000)
54 #define SHORT_TIMEOUT (5*1000)
56 /* value for unused fid field in trans2 secondary request */
57 #define FID_UNUSED (0xFFFF)
59 time_t newer_than
= 0;
60 static int archive_level
= 0;
62 static BOOL translation
= False
;
66 /* clitar bits insert */
69 extern BOOL tar_reset
;
73 static BOOL prompt
= True
;
75 static int printmode
= 1;
77 static BOOL recurse
= False
;
78 BOOL lowercase
= False
;
80 static struct in_addr dest_ip
;
82 #define SEPARATORS " \t\n\r"
84 static BOOL abort_mget
= True
;
86 static pstring fileselection
= "";
88 extern file_info def_finfo
;
91 SMB_BIG_UINT get_total_size
= 0;
92 unsigned int get_total_time_ms
= 0;
93 static SMB_BIG_UINT put_total_size
= 0;
94 static unsigned int put_total_time_ms
= 0;
97 static double dir_total
;
101 /* some forward declarations */
102 static struct cli_state
*do_connect(const char *server
, const char *share
);
104 /****************************************************************************
105 Write to a local file with CR/LF->LF translation if appropriate. Return the
106 number taken from the buffer. This may not equal the number written.
107 ****************************************************************************/
109 static int writefile(int f
, char *b
, int n
)
119 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
122 if (write(f
, b
, 1) != 1) {
132 /****************************************************************************
133 Read from a file with LF->CR/LF translation if appropriate. Return the
134 number read. read approx n bytes.
135 ****************************************************************************/
137 static int readfile(char *b
, int n
, XFILE
*f
)
143 return x_fread(b
,1,n
,f
);
146 while (i
< (n
- 1) && (i
< BUFFER_SIZE
)) {
147 if ((c
= x_getc(f
)) == EOF
) {
151 if (c
== '\n') { /* change all LFs to CR/LF */
161 /****************************************************************************
163 ****************************************************************************/
165 static void send_message(void)
170 if (!cli_message_start(cli
, desthost
, username
, &grp_id
)) {
171 d_printf("message start: %s\n", cli_errstr(cli
));
176 d_printf("Connected. Type your message, ending it with a Control-D\n");
178 while (!feof(stdin
) && total_len
< 1600) {
179 int maxlen
= MIN(1600 - total_len
,127);
186 for (l
=0;l
<maxlen
&& (c
=fgetc(stdin
))!=EOF
;l
++) {
192 if (!cli_message_text(cli
, msg
, l
, grp_id
)) {
193 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli
));
200 if (total_len
>= 1600)
201 d_printf("the message was truncated to 1600 bytes\n");
203 d_printf("sent %d bytes\n",total_len
);
205 if (!cli_message_end(cli
, grp_id
)) {
206 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli
));
211 /****************************************************************************
212 Check the space on a device.
213 ****************************************************************************/
215 static int do_dskattr(void)
217 int total
, bsize
, avail
;
219 if (!cli_dskattr(cli
, &bsize
, &total
, &avail
)) {
220 d_printf("Error in dskattr: %s\n",cli_errstr(cli
));
224 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
225 total
, bsize
, avail
);
230 /****************************************************************************
232 ****************************************************************************/
234 static int cmd_pwd(void)
236 d_printf("Current directory is %s",service
);
237 d_printf("%s\n",cur_dir
);
241 /****************************************************************************
242 Change directory - inner section.
243 ****************************************************************************/
245 static int do_cd(char *newdir
)
253 /* Save the current directory in case the
254 new directory is invalid */
255 pstrcpy(saved_dir
, cur_dir
);
260 if (*(cur_dir
+strlen(cur_dir
)-1) != '\\') {
261 pstrcat(cur_dir
, "\\");
263 dos_clean_name(cur_dir
);
264 pstrcpy(dname
,cur_dir
);
265 pstrcat(cur_dir
,"\\");
266 dos_clean_name(cur_dir
);
268 if (!strequal(cur_dir
,"\\")) {
269 if (!cli_chkpath(cli
, dname
)) {
270 d_printf("cd %s: %s\n", dname
, cli_errstr(cli
));
271 pstrcpy(cur_dir
,saved_dir
);
275 pstrcpy(cd_path
,cur_dir
);
280 /****************************************************************************
282 ****************************************************************************/
284 static int cmd_cd(void)
289 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)))
292 d_printf("Current directory is %s\n",cur_dir
);
297 /*******************************************************************
298 Decide if a file should be operated on.
299 ********************************************************************/
301 static BOOL
do_this_one(file_info
*finfo
)
303 if (finfo
->mode
& aDIR
)
306 if (*fileselection
&&
307 !mask_match(finfo
->name
,fileselection
,False
)) {
308 DEBUG(3,("mask_match %s failed\n", finfo
->name
));
312 if (newer_than
&& finfo
->mtime
< newer_than
) {
313 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
317 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& aARCH
)) {
318 DEBUG(3,("archive %s failed\n", finfo
->name
));
325 /****************************************************************************
326 Display info about a file.
327 ****************************************************************************/
329 static void display_finfo(file_info
*finfo
)
331 if (do_this_one(finfo
)) {
332 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
333 d_printf(" %-30s%7.7s %8.0f %s",
335 attrib_string(finfo
->mode
),
337 asctime(LocalTime(&t
)));
338 dir_total
+= finfo
->size
;
342 /****************************************************************************
343 Accumulate size of a file.
344 ****************************************************************************/
346 static void do_du(file_info
*finfo
)
348 if (do_this_one(finfo
)) {
349 dir_total
+= finfo
->size
;
353 static BOOL do_list_recurse
;
354 static BOOL do_list_dirs
;
355 static char *do_list_queue
= 0;
356 static long do_list_queue_size
= 0;
357 static long do_list_queue_start
= 0;
358 static long do_list_queue_end
= 0;
359 static void (*do_list_fn
)(file_info
*);
361 /****************************************************************************
362 Functions for do_list_queue.
363 ****************************************************************************/
366 * The do_list_queue is a NUL-separated list of strings stored in a
367 * char*. Since this is a FIFO, we keep track of the beginning and
368 * ending locations of the data in the queue. When we overflow, we
369 * double the size of the char*. When the start of the data passes
370 * the midpoint, we move everything back. This is logically more
371 * complex than a linked list, but easier from a memory management
372 * angle. In any memory error condition, do_list_queue is reset.
373 * Functions check to ensure that do_list_queue is non-NULL before
377 static void reset_do_list_queue(void)
379 SAFE_FREE(do_list_queue
);
380 do_list_queue_size
= 0;
381 do_list_queue_start
= 0;
382 do_list_queue_end
= 0;
385 static void init_do_list_queue(void)
387 reset_do_list_queue();
388 do_list_queue_size
= 1024;
389 do_list_queue
= malloc(do_list_queue_size
);
390 if (do_list_queue
== 0) {
391 d_printf("malloc fail for size %d\n",
392 (int)do_list_queue_size
);
393 reset_do_list_queue();
395 memset(do_list_queue
, 0, do_list_queue_size
);
399 static void adjust_do_list_queue(void)
402 * If the starting point of the queue is more than half way through,
403 * move everything toward the beginning.
405 if (do_list_queue
&& (do_list_queue_start
== do_list_queue_end
)) {
406 DEBUG(4,("do_list_queue is empty\n"));
407 do_list_queue_start
= do_list_queue_end
= 0;
408 *do_list_queue
= '\0';
409 } else if (do_list_queue_start
> (do_list_queue_size
/ 2)) {
410 DEBUG(4,("sliding do_list_queue backward\n"));
411 memmove(do_list_queue
,
412 do_list_queue
+ do_list_queue_start
,
413 do_list_queue_end
- do_list_queue_start
);
414 do_list_queue_end
-= do_list_queue_start
;
415 do_list_queue_start
= 0;
419 static void add_to_do_list_queue(const char* entry
)
422 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
423 while (new_end
> do_list_queue_size
) {
424 do_list_queue_size
*= 2;
425 DEBUG(4,("enlarging do_list_queue to %d\n",
426 (int)do_list_queue_size
));
427 dlq
= Realloc(do_list_queue
, do_list_queue_size
);
429 d_printf("failure enlarging do_list_queue to %d bytes\n",
430 (int)do_list_queue_size
);
431 reset_do_list_queue();
434 memset(do_list_queue
+ do_list_queue_size
/ 2,
435 0, do_list_queue_size
/ 2);
439 safe_strcpy_base(do_list_queue
+ do_list_queue_end
,
440 entry
, do_list_queue
, do_list_queue_size
);
441 do_list_queue_end
= new_end
;
442 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
443 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
447 static char *do_list_queue_head(void)
449 return do_list_queue
+ do_list_queue_start
;
452 static void remove_do_list_queue_head(void)
454 if (do_list_queue_end
> do_list_queue_start
) {
455 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
456 adjust_do_list_queue();
457 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
458 (int)do_list_queue_start
, (int)do_list_queue_end
));
462 static int do_list_queue_empty(void)
464 return (! (do_list_queue
&& *do_list_queue
));
467 /****************************************************************************
468 A helper for do_list.
469 ****************************************************************************/
471 static void do_list_helper(file_info
*f
, const char *mask
, void *state
)
473 if (f
->mode
& aDIR
) {
474 if (do_list_dirs
&& do_this_one(f
)) {
477 if (do_list_recurse
&&
478 !strequal(f
->name
,".") &&
479 !strequal(f
->name
,"..")) {
484 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
488 pstrcpy(mask2
, mask
);
489 p
= strrchr_m(mask2
,'\\');
493 pstrcat(mask2
, f
->name
);
494 pstrcat(mask2
,"\\*");
495 add_to_do_list_queue(mask2
);
500 if (do_this_one(f
)) {
505 /****************************************************************************
506 A wrapper around cli_list that adds recursion.
507 ****************************************************************************/
509 void do_list(const char *mask
,uint16 attribute
,void (*fn
)(file_info
*),BOOL rec
, BOOL dirs
)
511 static int in_do_list
= 0;
513 if (in_do_list
&& rec
) {
514 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
520 do_list_recurse
= rec
;
525 init_do_list_queue();
526 add_to_do_list_queue(mask
);
528 while (! do_list_queue_empty()) {
530 * Need to copy head so that it doesn't become
531 * invalid inside the call to cli_list. This
532 * would happen if the list were expanded
534 * Fix from E. Jay Berkenbilt (ejb@ql.org)
537 pstrcpy(head
, do_list_queue_head());
538 cli_list(cli
, head
, attribute
, do_list_helper
, NULL
);
539 remove_do_list_queue_head();
540 if ((! do_list_queue_empty()) && (fn
== display_finfo
)) {
541 char* next_file
= do_list_queue_head();
543 if ((strlen(next_file
) >= 2) &&
544 (next_file
[strlen(next_file
) - 1] == '*') &&
545 (next_file
[strlen(next_file
) - 2] == '\\')) {
546 save_ch
= next_file
+
547 strlen(next_file
) - 2;
550 d_printf("\n%s\n",next_file
);
557 if (cli_list(cli
, mask
, attribute
, do_list_helper
, NULL
) == -1) {
558 d_printf("%s listing %s\n", cli_errstr(cli
), mask
);
563 reset_do_list_queue();
566 /****************************************************************************
567 Get a directory listing.
568 ****************************************************************************/
570 static int cmd_dir(void)
572 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
579 pstrcpy(mask
,cur_dir
);
580 if(mask
[strlen(mask
)-1]!='\\')
583 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
593 do_list(mask
, attribute
, display_finfo
, recurse
, True
);
597 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
602 /****************************************************************************
603 Get a directory listing.
604 ****************************************************************************/
606 static int cmd_du(void)
608 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
615 pstrcpy(mask
,cur_dir
);
616 if(mask
[strlen(mask
)-1]!='\\')
619 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
629 do_list(mask
, attribute
, do_du
, recurse
, True
);
633 d_printf("Total number of bytes: %.0f\n", dir_total
);
638 /****************************************************************************
639 Get a file from rname to lname
640 ****************************************************************************/
642 static int do_get(char *rname
, char *lname
, BOOL reget
)
644 int handle
= 0, fnum
;
645 BOOL newhandle
= False
;
647 struct timeval tp_start
;
648 int read_size
= io_bufsize
;
655 GetTimeOfDay(&tp_start
);
661 fnum
= cli_open(cli
, rname
, O_RDONLY
, DENY_NONE
);
664 d_printf("%s opening remote file %s\n",cli_errstr(cli
),rname
);
668 if(!strcmp(lname
,"-")) {
669 handle
= fileno(stdout
);
672 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
, 0644);
674 start
= sys_lseek(handle
, 0, SEEK_END
);
676 d_printf("Error seeking local file\n");
681 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
686 d_printf("Error opening local file %s\n",lname
);
691 if (!cli_qfileinfo(cli
, fnum
,
692 &attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
693 !cli_getattrE(cli
, fnum
,
694 &attr
, &size
, NULL
, NULL
, NULL
)) {
695 d_printf("getattrib: %s\n",cli_errstr(cli
));
699 DEBUG(2,("getting file %s of size %.0f as %s ",
700 rname
, (double)size
, lname
));
702 if(!(data
= (char *)malloc(read_size
))) {
703 d_printf("malloc fail for size %d\n", read_size
);
704 cli_close(cli
, fnum
);
709 int n
= cli_read(cli
, fnum
, data
, nread
+ start
, read_size
);
714 if (writefile(handle
,data
, n
) != n
) {
715 d_printf("Error writing local file\n");
723 if (nread
+ start
< size
) {
724 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
725 rname
, (long)nread
));
732 if (!cli_close(cli
, fnum
)) {
733 d_printf("Error %s closing remote file\n",cli_errstr(cli
));
741 if (archive_level
>= 2 && (attr
& aARCH
)) {
742 cli_setatr(cli
, rname
, attr
& ~(uint16
)aARCH
, 0);
746 struct timeval tp_end
;
749 GetTimeOfDay(&tp_end
);
751 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
752 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
753 get_total_time_ms
+= this_time
;
754 get_total_size
+= nread
;
756 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
757 nread
/ (1.024*this_time
+ 1.0e-4),
758 get_total_size
/ (1.024*get_total_time_ms
)));
764 /****************************************************************************
766 ****************************************************************************/
768 static int cmd_get(void)
774 pstrcpy(rname
,cur_dir
);
777 p
= rname
+ strlen(rname
);
779 if (!next_token_nr(NULL
,p
,NULL
,sizeof(rname
)-strlen(rname
))) {
780 d_printf("get <filename>\n");
784 dos_clean_name(rname
);
786 next_token_nr(NULL
,lname
,NULL
,sizeof(lname
));
788 return do_get(rname
, lname
, False
);
791 /****************************************************************************
792 Do an mget operation on one file.
793 ****************************************************************************/
795 static void do_mget(file_info
*finfo
)
799 pstring saved_curdir
;
802 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
806 d_printf("mget aborted\n");
810 if (finfo
->mode
& aDIR
)
811 slprintf(quest
,sizeof(pstring
)-1,
812 "Get directory %s? ",finfo
->name
);
814 slprintf(quest
,sizeof(pstring
)-1,
815 "Get file %s? ",finfo
->name
);
817 if (prompt
&& !yesno(quest
))
820 if (!(finfo
->mode
& aDIR
)) {
821 pstrcpy(rname
,cur_dir
);
822 pstrcat(rname
,finfo
->name
);
823 do_get(rname
, finfo
->name
, False
);
827 /* handle directories */
828 pstrcpy(saved_curdir
,cur_dir
);
830 pstrcat(cur_dir
,finfo
->name
);
831 pstrcat(cur_dir
,"\\");
833 unix_format(finfo
->name
);
835 strlower_m(finfo
->name
);
837 if (!directory_exist(finfo
->name
,NULL
) &&
838 mkdir(finfo
->name
,0777) != 0) {
839 d_printf("failed to create directory %s\n",finfo
->name
);
840 pstrcpy(cur_dir
,saved_curdir
);
844 if (chdir(finfo
->name
) != 0) {
845 d_printf("failed to chdir to directory %s\n",finfo
->name
);
846 pstrcpy(cur_dir
,saved_curdir
);
850 pstrcpy(mget_mask
,cur_dir
);
851 pstrcat(mget_mask
,"*");
853 do_list(mget_mask
, aSYSTEM
| aHIDDEN
| aDIR
,do_mget
,False
, True
);
855 pstrcpy(cur_dir
,saved_curdir
);
858 /****************************************************************************
859 View the file using the pager.
860 ****************************************************************************/
862 static int cmd_more(void)
864 fstring rname
,lname
,pager_cmd
;
869 fstrcpy(rname
,cur_dir
);
872 slprintf(lname
,sizeof(lname
)-1, "%s/smbmore.XXXXXX",tmpdir());
873 fd
= smb_mkstemp(lname
);
875 d_printf("failed to create temporary file for more\n");
880 if (!next_token_nr(NULL
,rname
+strlen(rname
),NULL
,sizeof(rname
)-strlen(rname
))) {
881 d_printf("more <filename>\n");
885 dos_clean_name(rname
);
887 rc
= do_get(rname
, lname
, False
);
889 pager
=getenv("PAGER");
891 slprintf(pager_cmd
,sizeof(pager_cmd
)-1,
892 "%s %s",(pager
? pager
:PAGER
), lname
);
899 /****************************************************************************
901 ****************************************************************************/
903 static int cmd_mget(void)
905 uint16 attribute
= aSYSTEM
| aHIDDEN
;
917 while (next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
918 pstrcpy(mget_mask
,cur_dir
);
919 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
920 pstrcat(mget_mask
,"\\");
923 pstrcpy(mget_mask
,p
);
925 pstrcat(mget_mask
,p
);
926 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
930 pstrcpy(mget_mask
,cur_dir
);
931 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
932 pstrcat(mget_mask
,"\\");
933 pstrcat(mget_mask
,"*");
934 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
940 /****************************************************************************
941 Make a directory of name "name".
942 ****************************************************************************/
944 static BOOL
do_mkdir(char *name
)
946 if (!cli_mkdir(cli
, name
)) {
947 d_printf("%s making remote directory %s\n",
948 cli_errstr(cli
),name
);
955 /****************************************************************************
956 Show 8.3 name of a file.
957 ****************************************************************************/
959 static BOOL
do_altname(char *name
)
962 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli
, name
, altname
))) {
963 d_printf("%s getting alt name for %s\n",
964 cli_errstr(cli
),name
);
967 d_printf("%s\n", altname
);
972 /****************************************************************************
974 ****************************************************************************/
976 static int cmd_quit(void)
984 /****************************************************************************
986 ****************************************************************************/
988 static int cmd_mkdir(void)
994 pstrcpy(mask
,cur_dir
);
996 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
998 d_printf("mkdir <dirname>\n");
1009 trim_char(ddir
,'.','\0');
1010 p
= strtok(ddir
,"/\\");
1013 if (!cli_chkpath(cli
, ddir2
)) {
1016 pstrcat(ddir2
,"\\");
1017 p
= strtok(NULL
,"/\\");
1026 /****************************************************************************
1028 ****************************************************************************/
1030 static int cmd_altname(void)
1036 pstrcpy(name
,cur_dir
);
1038 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1039 d_printf("altname <file>\n");
1049 /****************************************************************************
1051 ****************************************************************************/
1053 static int do_put(char *rname
, char *lname
, BOOL reput
)
1060 int maxwrite
= io_bufsize
;
1063 struct timeval tp_start
;
1064 GetTimeOfDay(&tp_start
);
1067 fnum
= cli_open(cli
, rname
, O_RDWR
|O_CREAT
, DENY_NONE
);
1069 if (!cli_qfileinfo(cli
, fnum
, NULL
, &start
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
1070 !cli_getattrE(cli
, fnum
, NULL
, &start
, NULL
, NULL
, NULL
)) {
1071 d_printf("getattrib: %s\n",cli_errstr(cli
));
1076 fnum
= cli_open(cli
, rname
, O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
);
1080 d_printf("%s opening remote file %s\n",cli_errstr(cli
),rname
);
1084 /* allow files to be piped into smbclient
1087 Note that in this case this function will exit(0) rather
1089 if (!strcmp(lname
, "-")) {
1091 /* size of file is not known */
1093 f
= x_fopen(lname
,O_RDONLY
, 0);
1095 if (x_tseek(f
, start
, SEEK_SET
) == -1) {
1096 d_printf("Error seeking local file\n");
1103 d_printf("Error opening local file %s\n",lname
);
1107 DEBUG(1,("putting file %s as %s ",lname
,
1110 buf
= (char *)malloc(maxwrite
);
1112 d_printf("ERROR: Not enough memory!\n");
1115 while (!x_feof(f
)) {
1119 if ((n
= readfile(buf
,n
,f
)) < 1) {
1120 if((n
== 0) && x_feof(f
))
1121 break; /* Empty local file. */
1123 d_printf("Error reading local file: %s\n", strerror(errno
));
1128 ret
= cli_write(cli
, fnum
, 0, buf
, nread
+ start
, n
);
1131 d_printf("Error writing file: %s\n", cli_errstr(cli
));
1139 if (!cli_close(cli
, fnum
)) {
1140 d_printf("%s closing remote file %s\n",cli_errstr(cli
),rname
);
1154 struct timeval tp_end
;
1157 GetTimeOfDay(&tp_end
);
1159 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
1160 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
1161 put_total_time_ms
+= this_time
;
1162 put_total_size
+= nread
;
1164 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1165 nread
/ (1.024*this_time
+ 1.0e-4),
1166 put_total_size
/ (1.024*put_total_time_ms
)));
1177 /****************************************************************************
1179 ****************************************************************************/
1181 static int cmd_put(void)
1188 pstrcpy(rname
,cur_dir
);
1189 pstrcat(rname
,"\\");
1191 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1192 d_printf("put <filename>\n");
1197 if (next_token_nr(NULL
,p
,NULL
,sizeof(buf
)))
1200 pstrcat(rname
,lname
);
1202 dos_clean_name(rname
);
1206 /* allow '-' to represent stdin
1207 jdblair, 24.jun.98 */
1208 if (!file_exist(lname
,&st
) &&
1209 (strcmp(lname
,"-"))) {
1210 d_printf("%s does not exist\n",lname
);
1215 return do_put(rname
, lname
, False
);
1218 /*************************************
1219 File list structure.
1220 *************************************/
1222 static struct file_list
{
1223 struct file_list
*prev
, *next
;
1228 /****************************************************************************
1229 Free a file_list structure.
1230 ****************************************************************************/
1232 static void free_file_list (struct file_list
* list
)
1234 struct file_list
*tmp
;
1238 DLIST_REMOVE(list
, list
);
1239 SAFE_FREE(tmp
->file_path
);
1244 /****************************************************************************
1245 Seek in a directory/file list until you get something that doesn't start with
1247 ****************************************************************************/
1249 static BOOL
seek_list(struct file_list
*list
, char *name
)
1252 trim_string(list
->file_path
,"./","\n");
1253 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
1262 /****************************************************************************
1263 Set the file selection mask.
1264 ****************************************************************************/
1266 static int cmd_select(void)
1268 pstrcpy(fileselection
,"");
1269 next_token_nr(NULL
,fileselection
,NULL
,sizeof(fileselection
));
1274 /****************************************************************************
1275 Recursive file matching function act as find
1276 match must be always set to True when calling this function
1277 ****************************************************************************/
1279 static int file_find(struct file_list
**list
, const char *directory
,
1280 const char *expression
, BOOL match
)
1283 struct file_list
*entry
;
1284 struct stat statbuf
;
1290 dir
= opendir(directory
);
1294 while ((dname
= readdirname(dir
))) {
1295 if (!strcmp("..", dname
))
1297 if (!strcmp(".", dname
))
1300 if (asprintf(&path
, "%s/%s", directory
, dname
) <= 0) {
1305 if (!match
|| !gen_fnmatch(expression
, dname
)) {
1307 ret
= stat(path
, &statbuf
);
1309 if (S_ISDIR(statbuf
.st_mode
)) {
1311 ret
= file_find(list
, path
, expression
, False
);
1314 d_printf("file_find: cannot stat file %s\n", path
);
1323 entry
= (struct file_list
*) malloc(sizeof (struct file_list
));
1325 d_printf("Out of memory in file_find\n");
1329 entry
->file_path
= path
;
1330 entry
->isdir
= isdir
;
1331 DLIST_ADD(*list
, entry
);
1341 /****************************************************************************
1343 ****************************************************************************/
1345 static int cmd_mput(void)
1350 while (next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1352 struct file_list
*temp_list
;
1353 char *quest
, *lname
, *rname
;
1357 ret
= file_find(&file_list
, ".", p
, True
);
1359 free_file_list(file_list
);
1367 for (temp_list
= file_list
; temp_list
;
1368 temp_list
= temp_list
->next
) {
1371 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0)
1373 trim_string(lname
, "./", "/");
1375 /* check if it's a directory */
1376 if (temp_list
->isdir
) {
1377 /* if (!recurse) continue; */
1380 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) break;
1381 if (prompt
&& !yesno(quest
)) { /* No */
1382 /* Skip the directory */
1383 lname
[strlen(lname
)-1] = '/';
1384 if (!seek_list(temp_list
, lname
))
1388 if(asprintf(&rname
, "%s%s", cur_dir
, lname
) < 0) break;
1390 if (!cli_chkpath(cli
, rname
) &&
1392 DEBUG (0, ("Unable to make dir, skipping..."));
1393 /* Skip the directory */
1394 lname
[strlen(lname
)-1] = '/';
1395 if (!seek_list(temp_list
, lname
))
1402 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) break;
1403 if (prompt
&& !yesno(quest
)) /* No */
1408 if (asprintf(&rname
, "%s%s", cur_dir
, lname
) < 0) break;
1413 do_put(rname
, lname
, False
);
1415 free_file_list(file_list
);
1424 /****************************************************************************
1426 ****************************************************************************/
1428 static int do_cancel(int job
)
1430 if (cli_printjob_del(cli
, job
)) {
1431 d_printf("Job %d cancelled\n",job
);
1434 d_printf("Error cancelling job %d : %s\n",job
,cli_errstr(cli
));
1439 /****************************************************************************
1441 ****************************************************************************/
1443 static int cmd_cancel(void)
1448 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1449 d_printf("cancel <jobid> ...\n");
1455 } while (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)));
1460 /****************************************************************************
1462 ****************************************************************************/
1464 static int cmd_print(void)
1470 if (!next_token_nr(NULL
,lname
,NULL
, sizeof(lname
))) {
1471 d_printf("print <filename>\n");
1475 pstrcpy(rname
,lname
);
1476 p
= strrchr_m(rname
,'/');
1478 slprintf(rname
, sizeof(rname
)-1, "%s-%d", p
+1, (int)sys_getpid());
1481 if (strequal(lname
,"-")) {
1482 slprintf(rname
, sizeof(rname
)-1, "stdin-%d", (int)sys_getpid());
1485 return do_put(rname
, lname
, False
);
1488 /****************************************************************************
1489 Show a print queue entry.
1490 ****************************************************************************/
1492 static void queue_fn(struct print_job_info
*p
)
1494 d_printf("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
);
1497 /****************************************************************************
1499 ****************************************************************************/
1501 static int cmd_queue(void)
1503 cli_print_queue(cli
, queue_fn
);
1508 /****************************************************************************
1510 ****************************************************************************/
1512 static void do_del(file_info
*finfo
)
1516 pstrcpy(mask
,cur_dir
);
1517 pstrcat(mask
,finfo
->name
);
1519 if (finfo
->mode
& aDIR
)
1522 if (!cli_unlink(cli
, mask
)) {
1523 d_printf("%s deleting remote file %s\n",cli_errstr(cli
),mask
);
1527 /****************************************************************************
1529 ****************************************************************************/
1531 static int cmd_del(void)
1535 uint16 attribute
= aSYSTEM
| aHIDDEN
;
1540 pstrcpy(mask
,cur_dir
);
1542 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1543 d_printf("del <filename>\n");
1548 do_list(mask
, attribute
,do_del
,False
,False
);
1553 /****************************************************************************
1554 ****************************************************************************/
1556 static int cmd_open(void)
1561 pstrcpy(mask
,cur_dir
);
1563 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1564 d_printf("open <filename>\n");
1569 cli_open(cli
, mask
, O_RDWR
, DENY_ALL
);
1575 /****************************************************************************
1577 ****************************************************************************/
1579 static int cmd_rmdir(void)
1584 pstrcpy(mask
,cur_dir
);
1586 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1587 d_printf("rmdir <dirname>\n");
1592 if (!cli_rmdir(cli
, mask
)) {
1593 d_printf("%s removing remote directory file %s\n",
1594 cli_errstr(cli
),mask
);
1600 /****************************************************************************
1602 ****************************************************************************/
1604 static int cmd_link(void)
1609 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1610 d_printf("Server doesn't support UNIX CIFS calls.\n");
1614 pstrcpy(src
,cur_dir
);
1615 pstrcpy(dest
,cur_dir
);
1617 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1618 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1619 d_printf("link <src> <dest>\n");
1626 if (!cli_unix_hardlink(cli
, src
, dest
)) {
1627 d_printf("%s linking files (%s -> %s)\n", cli_errstr(cli
), src
, dest
);
1634 /****************************************************************************
1636 ****************************************************************************/
1638 static int cmd_symlink(void)
1643 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1644 d_printf("Server doesn't support UNIX CIFS calls.\n");
1648 pstrcpy(src
,cur_dir
);
1649 pstrcpy(dest
,cur_dir
);
1651 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1652 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1653 d_printf("symlink <src> <dest>\n");
1660 if (!cli_unix_symlink(cli
, src
, dest
)) {
1661 d_printf("%s symlinking files (%s -> %s)\n",
1662 cli_errstr(cli
), src
, dest
);
1669 /****************************************************************************
1671 ****************************************************************************/
1673 static int cmd_chmod(void)
1679 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1680 d_printf("Server doesn't support UNIX CIFS calls.\n");
1684 pstrcpy(src
,cur_dir
);
1686 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1687 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1688 d_printf("chmod mode file\n");
1692 mode
= (mode_t
)strtol(buf
, NULL
, 8);
1695 if (!cli_unix_chmod(cli
, src
, mode
)) {
1696 d_printf("%s chmod file %s 0%o\n",
1697 cli_errstr(cli
), src
, (unsigned int)mode
);
1704 /****************************************************************************
1706 ****************************************************************************/
1708 static int cmd_chown(void)
1713 fstring buf
, buf2
, buf3
;
1715 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1716 d_printf("Server doesn't support UNIX CIFS calls.\n");
1720 pstrcpy(src
,cur_dir
);
1722 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1723 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
)) ||
1724 !next_token(NULL
,buf3
,NULL
, sizeof(buf3
))) {
1725 d_printf("chown uid gid file\n");
1729 uid
= (uid_t
)atoi(buf
);
1730 gid
= (gid_t
)atoi(buf2
);
1733 if (!cli_unix_chown(cli
, src
, uid
, gid
)) {
1734 d_printf("%s chown file %s uid=%d, gid=%d\n",
1735 cli_errstr(cli
), src
, (int)uid
, (int)gid
);
1742 /****************************************************************************
1744 ****************************************************************************/
1746 static int cmd_rename(void)
1751 pstrcpy(src
,cur_dir
);
1752 pstrcpy(dest
,cur_dir
);
1754 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
1755 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1756 d_printf("rename <src> <dest>\n");
1763 if (!cli_rename(cli
, src
, dest
)) {
1764 d_printf("%s renaming files\n",cli_errstr(cli
));
1771 /****************************************************************************
1772 Toggle the prompt flag.
1773 ****************************************************************************/
1775 static int cmd_prompt(void)
1778 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
1783 /****************************************************************************
1784 Set the newer than time.
1785 ****************************************************************************/
1787 static int cmd_newer(void)
1791 SMB_STRUCT_STAT sbuf
;
1793 ok
= next_token_nr(NULL
,buf
,NULL
,sizeof(buf
));
1794 if (ok
&& (sys_stat(buf
,&sbuf
) == 0)) {
1795 newer_than
= sbuf
.st_mtime
;
1796 DEBUG(1,("Getting files newer than %s",
1797 asctime(LocalTime(&newer_than
))));
1802 if (ok
&& newer_than
== 0) {
1803 d_printf("Error setting newer-than time\n");
1810 /****************************************************************************
1811 Set the archive level.
1812 ****************************************************************************/
1814 static int cmd_archive(void)
1818 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1819 archive_level
= atoi(buf
);
1821 d_printf("Archive level is %d\n",archive_level
);
1826 /****************************************************************************
1827 Toggle the lowercaseflag.
1828 ****************************************************************************/
1830 static int cmd_lowercase(void)
1832 lowercase
= !lowercase
;
1833 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
1838 /****************************************************************************
1839 Toggle the recurse flag.
1840 ****************************************************************************/
1842 static int cmd_recurse(void)
1845 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
1850 /****************************************************************************
1851 Toggle the translate flag.
1852 ****************************************************************************/
1854 static int cmd_translate(void)
1856 translation
= !translation
;
1857 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1858 translation
?"on":"off"));
1863 /****************************************************************************
1864 Do a printmode command.
1865 ****************************************************************************/
1867 static int cmd_printmode(void)
1872 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1873 if (strequal(buf
,"text")) {
1876 if (strequal(buf
,"graphics"))
1879 printmode
= atoi(buf
);
1885 fstrcpy(mode
,"text");
1888 fstrcpy(mode
,"graphics");
1891 slprintf(mode
,sizeof(mode
)-1,"%d",printmode
);
1895 DEBUG(2,("the printmode is now %s\n",mode
));
1900 /****************************************************************************
1902 ****************************************************************************/
1904 static int cmd_lcd(void)
1909 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)))
1911 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
1916 /****************************************************************************
1917 Get a file restarting at end of local file.
1918 ****************************************************************************/
1920 static int cmd_reget(void)
1923 pstring remote_name
;
1926 pstrcpy(remote_name
, cur_dir
);
1927 pstrcat(remote_name
, "\\");
1929 p
= remote_name
+ strlen(remote_name
);
1931 if (!next_token_nr(NULL
, p
, NULL
, sizeof(remote_name
) - strlen(remote_name
))) {
1932 d_printf("reget <filename>\n");
1935 pstrcpy(local_name
, p
);
1936 dos_clean_name(remote_name
);
1938 next_token_nr(NULL
, local_name
, NULL
, sizeof(local_name
));
1940 return do_get(remote_name
, local_name
, True
);
1943 /****************************************************************************
1944 Put a file restarting at end of local file.
1945 ****************************************************************************/
1947 static int cmd_reput(void)
1950 pstring remote_name
;
1955 pstrcpy(remote_name
, cur_dir
);
1956 pstrcat(remote_name
, "\\");
1958 if (!next_token_nr(NULL
, p
, NULL
, sizeof(buf
))) {
1959 d_printf("reput <filename>\n");
1962 pstrcpy(local_name
, p
);
1964 if (!file_exist(local_name
, &st
)) {
1965 d_printf("%s does not exist\n", local_name
);
1969 if (next_token_nr(NULL
, p
, NULL
, sizeof(buf
)))
1970 pstrcat(remote_name
, p
);
1972 pstrcat(remote_name
, local_name
);
1974 dos_clean_name(remote_name
);
1976 return do_put(remote_name
, local_name
, True
);
1979 /****************************************************************************
1981 ****************************************************************************/
1983 static void browse_fn(const char *name
, uint32 m
,
1984 const char *comment
, void *state
)
1992 case STYPE_DISKTREE
:
1993 fstrcpy(typestr
,"Disk"); break;
1995 fstrcpy(typestr
,"Printer"); break;
1997 fstrcpy(typestr
,"Device"); break;
1999 fstrcpy(typestr
,"IPC"); break;
2001 /* FIXME: If the remote machine returns non-ascii characters
2002 in any of these fields, they can corrupt the output. We
2003 should remove them. */
2004 d_printf("\t%-15.15s%-10.10s%s\n",
2005 name
,typestr
,comment
);
2008 /****************************************************************************
2009 Try and browse available connections on a host.
2010 ****************************************************************************/
2012 static BOOL
browse_host(BOOL sort
)
2016 d_printf("\n\tSharename Type Comment\n");
2017 d_printf("\t--------- ---- -------\n");
2019 if((ret
= cli_RNetShareEnum(cli
, browse_fn
, NULL
)) == -1)
2020 d_printf("Error returning browse list: %s\n", cli_errstr(cli
));
2025 /****************************************************************************
2027 ****************************************************************************/
2029 static void server_fn(const char *name
, uint32 m
,
2030 const char *comment
, void *state
)
2032 d_printf("\t%-16.16s %s\n", name
, comment
);
2035 /****************************************************************************
2036 Try and browse available connections on a host.
2037 ****************************************************************************/
2039 static BOOL
list_servers(char *wk_grp
)
2041 if (!cli
->server_domain
)
2044 d_printf("\n\tServer Comment\n");
2045 d_printf("\t--------- -------\n");
2047 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
, NULL
);
2049 d_printf("\n\tWorkgroup Master\n");
2050 d_printf("\t--------- -------\n");
2052 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
, server_fn
, NULL
);
2056 /* Some constants for completing filename arguments */
2058 #define COMPL_NONE 0 /* No completions */
2059 #define COMPL_REMOTE 1 /* Complete remote filename */
2060 #define COMPL_LOCAL 2 /* Complete local filename */
2062 /* This defines the commands supported by this client.
2063 * NOTE: The "!" must be the last one in the list because it's fn pointer
2064 * field is NULL, and NULL in that field is used in process_tok()
2065 * (below) to indicate the end of the list. crh
2071 const char *description
;
2072 char compl_args
[2]; /* Completion argument info */
2074 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
2075 {"altname",cmd_altname
,"<file> show alt name",{COMPL_NONE
,COMPL_NONE
}},
2076 {"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
}},
2077 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
2078 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
2079 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
2080 {"chmod",cmd_chmod
,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE
,COMPL_REMOTE
}},
2081 {"chown",cmd_chown
,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE
,COMPL_REMOTE
}},
2082 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2083 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
2084 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
2085 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2086 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
2087 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
2088 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
2089 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
2090 {"link",cmd_link
,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
2091 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
2092 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
2093 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
2094 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
2095 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2096 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
2097 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
2098 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2099 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
2100 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
2101 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
2102 {"printmode",cmd_printmode
,"<graphics or text> set the print mode",{COMPL_NONE
,COMPL_NONE
}},
2103 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
2104 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
2105 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
2106 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2107 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
2108 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2109 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
2110 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
2111 {"reget",cmd_reget
,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE
,COMPL_LOCAL
}},
2112 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
2113 {"reput",cmd_reput
,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL
,COMPL_REMOTE
}},
2114 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2115 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
2116 {"setmode",cmd_setmode
,"filename <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
2117 {"symlink",cmd_symlink
,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
2118 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
2119 {"tarmode",cmd_tarmode
,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
2120 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
2122 /* Yes, this must be here, see crh's comment above. */
2123 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
2124 {NULL
,NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
2127 /*******************************************************************
2128 Lookup a command string in the list of commands, including
2130 ******************************************************************/
2132 static int process_tok(fstring tok
)
2134 int i
= 0, matches
= 0;
2136 int tok_len
= strlen(tok
);
2138 while (commands
[i
].fn
!= NULL
) {
2139 if (strequal(commands
[i
].name
,tok
)) {
2143 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
2152 else if (matches
== 1)
2158 /****************************************************************************
2160 ****************************************************************************/
2162 static int cmd_help(void)
2167 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2168 if ((i
= process_tok(buf
)) >= 0)
2169 d_printf("HELP %s:\n\t%s\n\n",commands
[i
].name
,commands
[i
].description
);
2171 while (commands
[i
].description
) {
2172 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
2173 d_printf("%-15s",commands
[i
].name
);
2182 /****************************************************************************
2183 Process a -c command string.
2184 ****************************************************************************/
2186 static int process_command_string(char *cmd
)
2192 /* establish the connection if not already */
2195 cli
= do_connect(desthost
, service
);
2200 while (cmd
[0] != '\0') {
2205 if ((p
= strchr_m(cmd
, ';')) == 0) {
2206 strncpy(line
, cmd
, 999);
2212 strncpy(line
, cmd
, p
- cmd
);
2213 line
[p
- cmd
] = '\0';
2217 /* and get the first part of the command */
2219 if (!next_token_nr(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
2221 if ((i
= process_tok(tok
)) >= 0) {
2222 rc
= commands
[i
].fn();
2223 } else if (i
== -2) {
2224 d_printf("%s: command abbreviation ambiguous\n",tok
);
2226 d_printf("%s: command not found\n",tok
);
2233 #define MAX_COMPLETIONS 100
2241 } completion_remote_t
;
2243 static void completion_remote_filter(file_info
*f
, const char *mask
, void *state
)
2245 completion_remote_t
*info
= (completion_remote_t
*)state
;
2247 if ((info
->count
< MAX_COMPLETIONS
- 1) && (strncmp(info
->text
, f
->name
, info
->len
) == 0) && (strcmp(f
->name
, ".") != 0) && (strcmp(f
->name
, "..") != 0)) {
2248 if ((info
->dirmask
[0] == 0) && !(f
->mode
& aDIR
))
2249 info
->matches
[info
->count
] = strdup(f
->name
);
2253 if (info
->dirmask
[0] != 0)
2254 pstrcpy(tmp
, info
->dirmask
);
2257 pstrcat(tmp
, f
->name
);
2260 info
->matches
[info
->count
] = strdup(tmp
);
2262 if (info
->matches
[info
->count
] == NULL
)
2265 smb_readline_ca_char(0);
2267 if (info
->count
== 1)
2268 info
->samelen
= strlen(info
->matches
[info
->count
]);
2270 while (strncmp(info
->matches
[info
->count
], info
->matches
[info
->count
-1], info
->samelen
) != 0)
2276 static char **remote_completion(const char *text
, int len
)
2280 completion_remote_t info
= { "", NULL
, 1, 0, NULL
, 0 };
2282 /* can't have non-static intialisation on Sun CC, so do it
2288 if (len
>= PATH_MAX
)
2291 info
.matches
= (char **)malloc(sizeof(info
.matches
[0])*MAX_COMPLETIONS
);
2292 if (!info
.matches
) return NULL
;
2293 info
.matches
[0] = NULL
;
2295 for (i
= len
-1; i
>= 0; i
--)
2296 if ((text
[i
] == '/') || (text
[i
] == '\\'))
2298 info
.text
= text
+i
+1;
2299 info
.samelen
= info
.len
= len
-i
-1;
2302 strncpy(info
.dirmask
, text
, i
+1);
2303 info
.dirmask
[i
+1] = 0;
2304 pstr_sprintf(dirmask
, "%s%*s*", cur_dir
, i
-1, text
);
2306 pstr_sprintf(dirmask
, "%s*", cur_dir
);
2308 if (cli_list(cli
, dirmask
, aDIR
| aSYSTEM
| aHIDDEN
, completion_remote_filter
, &info
) < 0)
2311 if (info
.count
== 2)
2312 info
.matches
[0] = strdup(info
.matches
[1]);
2314 info
.matches
[0] = malloc(info
.samelen
+1);
2315 if (!info
.matches
[0])
2317 strncpy(info
.matches
[0], info
.matches
[1], info
.samelen
);
2318 info
.matches
[0][info
.samelen
] = 0;
2320 info
.matches
[info
.count
] = NULL
;
2321 return info
.matches
;
2324 for (i
= 0; i
< info
.count
; i
++)
2325 free(info
.matches
[i
]);
2330 static char **completion_fn(const char *text
, int start
, int end
)
2332 smb_readline_ca_char(' ');
2335 const char *buf
, *sp
;
2339 buf
= smb_readline_get_line_buffer();
2343 sp
= strchr(buf
, ' ');
2347 for (i
= 0; commands
[i
].name
; i
++)
2348 if ((strncmp(commands
[i
].name
, text
, sp
- buf
) == 0) && (commands
[i
].name
[sp
- buf
] == 0))
2350 if (commands
[i
].name
== NULL
)
2356 if (sp
== (buf
+ start
))
2357 compl_type
= commands
[i
].compl_args
[0];
2359 compl_type
= commands
[i
].compl_args
[1];
2361 if (compl_type
== COMPL_REMOTE
)
2362 return remote_completion(text
, end
- start
);
2363 else /* fall back to local filename completion */
2367 int i
, len
, samelen
, count
=1;
2369 matches
= (char **)malloc(sizeof(matches
[0])*MAX_COMPLETIONS
);
2370 if (!matches
) return NULL
;
2374 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
2375 if (strncmp(text
, commands
[i
].name
, len
) == 0) {
2376 matches
[count
] = strdup(commands
[i
].name
);
2377 if (!matches
[count
])
2380 samelen
= strlen(matches
[count
]);
2382 while (strncmp(matches
[count
], matches
[count
-1], samelen
) != 0)
2389 case 0: /* should never happen */
2393 matches
[0] = strdup(matches
[1]);
2396 matches
[0] = malloc(samelen
+1);
2399 strncpy(matches
[0], matches
[1], samelen
);
2400 matches
[0][samelen
] = 0;
2402 matches
[count
] = NULL
;
2415 /****************************************************************************
2416 Make sure we swallow keepalives during idle time.
2417 ****************************************************************************/
2419 static void readline_callback(void)
2422 struct timeval timeout
;
2423 static time_t last_t
;
2439 FD_SET(cli
->fd
,&fds
);
2442 timeout
.tv_usec
= 0;
2443 sys_select_intr(cli
->fd
+1,&fds
,NULL
,NULL
,&timeout
);
2445 /* We deliberately use receive_smb instead of
2446 client_receive_smb as we want to receive
2447 session keepalives and then drop them here.
2449 if (FD_ISSET(cli
->fd
,&fds
)) {
2450 receive_smb(cli
->fd
,cli
->inbuf
,0);
2454 cli_chkpath(cli
, "\\");
2457 /****************************************************************************
2458 Process commands on stdin.
2459 ****************************************************************************/
2461 static void process_stdin(void)
2472 /* display a prompt */
2473 slprintf(the_prompt
, sizeof(the_prompt
)-1, "smb: %s> ", cur_dir
);
2474 cline
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
2478 pstrcpy(line
, cline
);
2480 /* special case - first char is ! */
2486 /* and get the first part of the command */
2488 if (!next_token_nr(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
2490 if ((i
= process_tok(tok
)) >= 0) {
2492 } else if (i
== -2) {
2493 d_printf("%s: command abbreviation ambiguous\n",tok
);
2495 d_printf("%s: command not found\n",tok
);
2500 /*****************************************************
2501 Return a connection to a server.
2502 *******************************************************/
2504 static struct cli_state
*do_connect(const char *server
, const char *share
)
2506 struct cli_state
*c
;
2507 struct nmb_name called
, calling
;
2508 const char *server_n
;
2510 fstring servicename
;
2513 /* make a copy so we don't modify the global string 'service' */
2514 fstrcpy(servicename
, share
);
2515 sharename
= servicename
;
2516 if (*sharename
== '\\') {
2517 server
= sharename
+2;
2518 sharename
= strchr_m(server
,'\\');
2519 if (!sharename
) return NULL
;
2528 make_nmb_name(&calling
, global_myname(), 0x0);
2529 make_nmb_name(&called
, server
, name_type
);
2533 if (have_ip
) ip
= dest_ip
;
2535 /* have to open a new connection */
2536 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, port
) != port
) ||
2537 !cli_connect(c
, server_n
, &ip
)) {
2538 d_printf("Connection to %s failed\n", server_n
);
2542 c
->protocol
= max_protocol
;
2543 c
->use_kerberos
= use_kerberos
;
2544 cli_setup_signing_state(c
, cmdline_auth_info
.signing_state
);
2547 if (!cli_session_request(c
, &calling
, &called
)) {
2549 d_printf("session request to %s failed (%s)\n",
2550 called
.name
, cli_errstr(c
));
2552 if ((p
=strchr_m(called
.name
, '.'))) {
2556 if (strcmp(called
.name
, "*SMBSERVER")) {
2557 make_nmb_name(&called
, "*SMBSERVER", 0x20);
2563 DEBUG(4,(" session request ok\n"));
2565 if (!cli_negprot(c
)) {
2566 d_printf("protocol negotiation failed\n");
2572 char *pass
= getpass("Password: ");
2574 pstrcpy(password
, pass
);
2579 if (!cli_session_setup(c
, username
,
2580 password
, strlen(password
),
2581 password
, strlen(password
),
2583 /* if a password was not supplied then try again with a null username */
2584 if (password
[0] || !username
[0] || use_kerberos
||
2585 !cli_session_setup(c
, "", "", 0, "", 0, lp_workgroup())) {
2586 d_printf("session setup failed: %s\n", cli_errstr(c
));
2587 if (NT_STATUS_V(cli_nt_error(c
)) ==
2588 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED
))
2589 d_printf("did you forget to run kinit?\n");
2593 d_printf("Anonymous login successful\n");
2596 if (*c
->server_domain
) {
2597 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
2598 c
->server_domain
,c
->server_os
,c
->server_type
));
2599 } else if (*c
->server_os
|| *c
->server_type
){
2600 DEBUG(1,("OS=[%s] Server=[%s]\n",
2601 c
->server_os
,c
->server_type
));
2604 DEBUG(4,(" session setup ok\n"));
2606 if (!cli_send_tconX(c
, sharename
, "?????",
2607 password
, strlen(password
)+1)) {
2608 d_printf("tree connect failed: %s\n", cli_errstr(c
));
2613 DEBUG(4,(" tconx ok\n"));
2618 /****************************************************************************
2619 Process commands from the client.
2620 ****************************************************************************/
2622 static int process(char *base_directory
)
2626 cli
= do_connect(desthost
, service
);
2631 if (*base_directory
) do_cd(base_directory
);
2634 rc
= process_command_string(cmdstr
);
2643 /****************************************************************************
2645 ****************************************************************************/
2647 static int do_host_query(char *query_host
)
2649 cli
= do_connect(query_host
, "IPC$");
2657 /* Workgroups simply don't make sense over anything
2658 else but port 139... */
2662 cli
= do_connect(query_host
, "IPC$");
2666 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
2670 list_servers(lp_workgroup());
2678 /****************************************************************************
2679 Handle a tar operation.
2680 ****************************************************************************/
2682 static int do_tar_op(char *base_directory
)
2686 /* do we already have a connection? */
2688 cli
= do_connect(desthost
, service
);
2695 if (*base_directory
) do_cd(base_directory
);
2704 /****************************************************************************
2705 Handle a message operation.
2706 ****************************************************************************/
2708 static int do_message_op(void)
2711 struct nmb_name called
, calling
;
2712 fstring server_name
;
2713 char name_type_hex
[10];
2715 make_nmb_name(&calling
, global_myname(), 0x0);
2716 make_nmb_name(&called
, desthost
, name_type
);
2718 fstrcpy(server_name
, desthost
);
2719 snprintf(name_type_hex
, sizeof(name_type_hex
), "#%X", name_type
);
2720 fstrcat(server_name
, name_type_hex
);
2723 if (have_ip
) ip
= dest_ip
;
2725 if (!(cli
=cli_initialise(NULL
)) || (cli_set_port(cli
, port
) != port
) ||
2726 !cli_connect(cli
, server_name
, &ip
)) {
2727 d_printf("Connection to %s failed\n", desthost
);
2731 if (!cli_session_request(cli
, &calling
, &called
)) {
2732 d_printf("session request failed\n");
2745 * Process "-L hostname" option.
2747 * We don't actually do anything yet -- we just stash the name in a
2748 * global variable and do the query when all options have been read.
2751 static void remember_query_host(const char *arg
,
2756 while (*arg
== '\\' || *arg
== '/')
2758 pstrcpy(query_host
, arg
);
2759 if ((slash
= strchr(query_host
, '/'))
2760 || (slash
= strchr(query_host
, '\\'))) {
2765 /****************************************************************************
2767 ****************************************************************************/
2769 int main(int argc
,char *argv
[])
2771 extern BOOL AllowDebugChange
;
2772 fstring base_directory
;
2775 BOOL message
= False
;
2776 extern char tar_type
;
2778 static const char *new_name_resolve_order
= NULL
;
2782 fstring new_workgroup
;
2783 struct poptOption long_options
[] = {
2786 { "name-resolve", 'R', POPT_ARG_STRING
, &new_name_resolve_order
, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
2787 { "message", 'M', POPT_ARG_STRING
, NULL
, 'M', "Send message", "HOST" },
2788 { "ip-address", 'I', POPT_ARG_STRING
, NULL
, 'I', "Use this IP to connect to", "IP" },
2789 { "stderr", 'E', POPT_ARG_NONE
, NULL
, 'E', "Write messages to stderr instead of stdout" },
2790 { "list", 'L', POPT_ARG_STRING
, NULL
, 'L', "Get a list of shares available on a host", "HOST" },
2791 { "terminal", 't', POPT_ARG_STRING
, NULL
, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
2792 { "max-protocol", 'm', POPT_ARG_STRING
, NULL
, 'm', "Set the max protocol level", "LEVEL" },
2793 { "tar", 'T', POPT_ARG_STRING
, NULL
, 'T', "Command line tar", "<c|x>IXFqgbNan" },
2794 { "directory", 'D', POPT_ARG_STRING
, NULL
, 'D', "Start from directory", "DIR" },
2795 { "command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated commands" },
2796 { "send-buffer", 'b', POPT_ARG_INT
, &io_bufsize
, 'b', "Changes the transmit/send buffer", "BYTES" },
2797 { "port", 'p', POPT_ARG_INT
, &port
, 'p', "Port to connect to", "PORT" },
2799 POPT_COMMON_CONNECTION
2800 POPT_COMMON_CREDENTIALS
2806 pstrcpy(term_code
, KANJI
);
2812 *base_directory
= 0;
2814 /* initialize the workgroup name so we can determine whether or
2815 not it was set by a command line option */
2817 set_global_myworkgroup( "" );
2819 /* set default debug level to 0 regardless of what smb.conf sets */
2820 DEBUGLEVEL_CLASS
[DBGC_ALL
] = 0;
2822 x_setbuf( x_stderr
, NULL
);
2824 pc
= poptGetContext("smbclient", argc
, (const char **) argv
, long_options
,
2825 POPT_CONTEXT_KEEP_FIRST
);
2826 poptSetOtherOptionHelp(pc
, "service <password>");
2828 in_client
= True
; /* Make sure that we tell lp_load we are */
2830 while ((opt
= poptGetNextOpt(pc
)) != -1) {
2833 /* Messages are sent to NetBIOS name type 0x3
2834 * (Messenger Service). Make sure we default
2835 * to port 139 instead of port 445. srl,crh
2838 pstrcpy(desthost
,poptGetOptArg(pc
));
2845 dest_ip
= *interpret_addr2(poptGetOptArg(pc
));
2846 if (is_zero_ip(dest_ip
))
2853 display_set_stderr();
2857 remember_query_host(poptGetOptArg(pc
), query_host
);
2860 pstrcpy(term_code
, poptGetOptArg(pc
));
2863 max_protocol
= interpret_protocol(poptGetOptArg(pc
), max_protocol
);
2866 /* We must use old option processing for this. Find the
2867 * position of the -T option in the raw argv[]. */
2870 for (i
= 1; i
< argc
; i
++) {
2871 if (strncmp("-T", argv
[i
],2)==0)
2875 if (!(optnum
= tar_parseargs(argc
, argv
, poptGetOptArg(pc
), i
))) {
2876 poptPrintUsage(pc
, stderr
, 0);
2879 /* Now we must eat (optnum - i) options - they have
2880 * been processed by tar_parseargs().
2883 for (i
= 0; i
< optnum
; i
++)
2888 fstrcpy(base_directory
,poptGetOptArg(pc
));
2896 * Don't load debug level from smb.conf. It should be
2897 * set by cmdline arg or remain default (0)
2899 AllowDebugChange
= False
;
2901 /* save the workgroup...
2903 FIXME!! do we need to do tyhis for other options as well
2904 (or maybe a generic way to keep lp_load() from overwriting
2907 fstrcpy( new_workgroup
, lp_workgroup() );
2909 if (!lp_load(dyn_CONFIGFILE
,True
,False
,False
)) {
2910 fprintf(stderr
, "%s: Can't load %s - run testparm to debug it\n",
2911 argv
[0], dyn_CONFIGFILE
);
2916 if ( strlen(new_workgroup
) != 0 )
2917 set_global_myworkgroup( new_workgroup
);
2919 if(poptPeekArg(pc
)) {
2920 pstrcpy(service
,poptGetArg(pc
));
2921 /* Convert any '/' characters in the service name to '\' characters */
2922 string_replace(service
, '/','\\');
2924 if (count_chars(service
,'\\') < 3) {
2925 d_printf("\n%s: Not enough '\\' characters in service\n",service
);
2926 poptPrintUsage(pc
, stderr
, 0);
2931 if (poptPeekArg(pc
) && !cmdline_auth_info
.got_pass
) {
2932 cmdline_auth_info
.got_pass
= True
;
2933 pstrcpy(cmdline_auth_info
.password
,poptGetArg(pc
));
2938 if(new_name_resolve_order
)
2939 lp_set_name_resolve_order(new_name_resolve_order
);
2941 if (!tar_type
&& !*query_host
&& !*service
&& !message
) {
2942 poptPrintUsage(pc
, stderr
, 0);
2946 poptFreeContext(pc
);
2948 pstrcpy(username
, cmdline_auth_info
.username
);
2949 pstrcpy(password
, cmdline_auth_info
.password
);
2951 use_kerberos
= cmdline_auth_info
.use_kerberos
;
2952 got_pass
= cmdline_auth_info
.got_pass
;
2954 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING
));
2958 process_command_string(cmdstr
);
2959 return do_tar_op(base_directory
);
2962 if ((p
=strchr_m(query_host
,'#'))) {
2965 sscanf(p
, "%x", &name_type
);
2969 return do_host_query(query_host
);
2973 return do_message_op();
2976 if (process(base_directory
)) {