2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 struct cli_state
*cli
;
31 extern BOOL in_client
;
32 extern BOOL AllowDebugChange
;
33 static int port
= SMB_PORT
;
34 pstring cur_dir
= "\\";
36 static pstring service
;
37 static pstring desthost
;
38 extern pstring global_myname
;
39 static pstring password
;
40 static pstring username
;
41 static pstring workgroup
;
44 static int io_bufsize
= 64512;
46 static int name_type
= 0x20;
47 static int max_protocol
= PROTOCOL_NT1
;
48 extern pstring user_socket_options
;
50 static int process_tok(fstring tok
);
51 static void cmd_help(void);
53 /* 30 second timeout on most commands */
54 #define CLIENT_TIMEOUT (30*1000)
55 #define SHORT_TIMEOUT (5*1000)
57 /* value for unused fid field in trans2 secondary request */
58 #define FID_UNUSED (0xFFFF)
60 time_t newer_than
= 0;
61 int archive_level
= 0;
63 BOOL translation
= False
;
67 /* clitar bits insert */
70 extern BOOL tar_reset
;
74 mode_t myumask
= 0755;
80 static BOOL recurse
= False
;
81 BOOL lowercase
= False
;
83 struct in_addr dest_ip
;
85 #define SEPARATORS " \t\n\r"
87 BOOL abort_mget
= True
;
89 pstring fileselection
= "";
91 extern file_info def_finfo
;
94 int get_total_size
= 0;
95 int get_total_time_ms
= 0;
96 int put_total_size
= 0;
97 int put_total_time_ms
= 0;
100 static double dir_total
;
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 ****************************************************************************/
108 static int writefile(int f
, char *b
, int n
)
118 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
121 if (write(f
, b
, 1) != 1) {
131 /****************************************************************************
132 read from a file with LF->CR/LF translation if appropriate. return the
133 number read. read approx n bytes.
134 ****************************************************************************/
135 static int readfile(char *b
, int size
, int n
, FILE *f
)
140 if (!translation
|| (size
!= 1))
141 return(fread(b
,size
,n
,f
));
144 while (i
< (n
- 1) && (i
< BUFFER_SIZE
)) {
145 if ((c
= getc(f
)) == EOF
) {
149 if (c
== '\n') { /* change all LFs to CR/LF */
160 /****************************************************************************
162 ****************************************************************************/
163 static void send_message(void)
168 if (!cli_message_start(cli
, desthost
, username
, &grp_id
)) {
169 DEBUG(0,("message start: %s\n", cli_errstr(cli
)));
174 printf("Connected. Type your message, ending it with a Control-D\n");
176 while (!feof(stdin
) && total_len
< 1600) {
177 int maxlen
= MIN(1600 - total_len
,127);
184 for (l
=0;l
<maxlen
&& (c
=fgetc(stdin
))!=EOF
;l
++) {
191 * The message is in UNIX codepage format. Convert to
192 * DOS before sending.
197 if (!cli_message_text(cli
, msg
, l
, grp_id
)) {
198 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli
));
205 if (total_len
>= 1600)
206 printf("the message was truncated to 1600 bytes\n");
208 printf("sent %d bytes\n",total_len
);
210 if (!cli_message_end(cli
, grp_id
)) {
211 printf("SMBsendend failed (%s)\n",cli_errstr(cli
));
218 /****************************************************************************
219 check the space on a device
220 ****************************************************************************/
221 static void do_dskattr(void)
223 int total
, bsize
, avail
;
225 if (!cli_dskattr(cli
, &bsize
, &total
, &avail
)) {
226 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli
)));
230 DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
231 total
, bsize
, avail
));
234 /****************************************************************************
236 ****************************************************************************/
237 static void cmd_pwd(void)
239 DEBUG(0,("Current directory is %s",service
));
240 DEBUG(0,("%s\n",cur_dir
));
244 /****************************************************************************
245 change directory - inner section
246 ****************************************************************************/
247 static void do_cd(char *newdir
)
255 /* Save the current directory in case the
256 new directory is invalid */
257 pstrcpy(saved_dir
, cur_dir
);
262 if (*(cur_dir
+strlen(cur_dir
)-1) != '\\') {
263 pstrcat(cur_dir
, "\\");
265 dos_clean_name(cur_dir
);
266 pstrcpy(dname
,cur_dir
);
267 pstrcat(cur_dir
,"\\");
268 dos_clean_name(cur_dir
);
270 if (!strequal(cur_dir
,"\\")) {
271 if (!cli_chkpath(cli
, dname
)) {
272 DEBUG(0,("cd %s: %s\n", dname
, cli_errstr(cli
)));
273 pstrcpy(cur_dir
,saved_dir
);
277 pstrcpy(cd_path
,cur_dir
);
280 /****************************************************************************
282 ****************************************************************************/
283 static void cmd_cd(void)
287 if (next_token(NULL
,buf
,NULL
,sizeof(buf
)))
290 DEBUG(0,("Current directory is %s\n",cur_dir
));
294 /*******************************************************************
295 decide if a file should be operated on
296 ********************************************************************/
297 static BOOL
do_this_one(file_info
*finfo
)
299 if (finfo
->mode
& aDIR
) return(True
);
301 if (*fileselection
&&
302 !mask_match(finfo
->name
,fileselection
,False
)) {
303 DEBUG(3,("match_match %s failed\n", finfo
->name
));
307 if (newer_than
&& finfo
->mtime
< newer_than
) {
308 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
312 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& aARCH
)) {
313 DEBUG(3,("archive %s failed\n", finfo
->name
));
320 /****************************************************************************
321 display info about a file
322 ****************************************************************************/
323 static void display_finfo(file_info
*finfo
)
325 if (do_this_one(finfo
)) {
326 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
327 DEBUG(0,(" %-30s%7.7s %8.0f %s",
329 attrib_string(finfo
->mode
),
331 asctime(LocalTime(&t
))));
332 dir_total
+= finfo
->size
;
337 /****************************************************************************
338 accumulate size of a file
339 ****************************************************************************/
340 static void do_du(file_info
*finfo
)
342 if (do_this_one(finfo
)) {
343 dir_total
+= finfo
->size
;
347 static BOOL do_list_recurse
;
348 static BOOL do_list_dirs
;
349 static char *do_list_queue
= 0;
350 static long do_list_queue_size
= 0;
351 static long do_list_queue_start
= 0;
352 static long do_list_queue_end
= 0;
353 static void (*do_list_fn
)(file_info
*);
355 /****************************************************************************
356 functions for do_list_queue
357 ****************************************************************************/
360 * The do_list_queue is a NUL-separated list of strings stored in a
361 * char*. Since this is a FIFO, we keep track of the beginning and
362 * ending locations of the data in the queue. When we overflow, we
363 * double the size of the char*. When the start of the data passes
364 * the midpoint, we move everything back. This is logically more
365 * complex than a linked list, but easier from a memory management
366 * angle. In any memory error condition, do_list_queue is reset.
367 * Functions check to ensure that do_list_queue is non-NULL before
370 static void reset_do_list_queue(void)
372 SAFE_FREE(do_list_queue
);
374 do_list_queue_size
= 0;
375 do_list_queue_start
= 0;
376 do_list_queue_end
= 0;
379 static void init_do_list_queue(void)
381 reset_do_list_queue();
382 do_list_queue_size
= 1024;
383 do_list_queue
= malloc(do_list_queue_size
);
384 if (do_list_queue
== 0) {
385 DEBUG(0,("malloc fail for size %d\n",
386 (int)do_list_queue_size
));
387 reset_do_list_queue();
389 memset(do_list_queue
, 0, do_list_queue_size
);
393 static void adjust_do_list_queue(void)
396 * If the starting point of the queue is more than half way through,
397 * move everything toward the beginning.
399 if (do_list_queue
&& (do_list_queue_start
== do_list_queue_end
))
401 DEBUG(4,("do_list_queue is empty\n"));
402 do_list_queue_start
= do_list_queue_end
= 0;
403 *do_list_queue
= '\0';
405 else if (do_list_queue_start
> (do_list_queue_size
/ 2))
407 DEBUG(4,("sliding do_list_queue backward\n"));
408 memmove(do_list_queue
,
409 do_list_queue
+ do_list_queue_start
,
410 do_list_queue_end
- do_list_queue_start
);
411 do_list_queue_end
-= do_list_queue_start
;
412 do_list_queue_start
= 0;
417 static void add_to_do_list_queue(const char* entry
)
421 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
422 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 DEBUG(0,("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);
440 pstrcpy(do_list_queue
+ do_list_queue_end
, entry
);
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
)
456 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
457 adjust_do_list_queue();
458 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
459 (int)do_list_queue_start
, (int)do_list_queue_end
));
463 static int do_list_queue_empty(void)
465 return (! (do_list_queue
&& *do_list_queue
));
468 /****************************************************************************
470 ****************************************************************************/
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
,"..")) {
483 pstrcpy(mask2
, mask
);
484 p
= strrchr(mask2
,'\\');
487 pstrcat(mask2
, f
->name
);
488 pstrcat(mask2
,"\\*");
489 add_to_do_list_queue(mask2
);
494 if (do_this_one(f
)) {
500 /****************************************************************************
501 a wrapper around cli_list that adds recursion
502 ****************************************************************************/
503 void do_list(const char *mask
,uint16 attribute
,void (*fn
)(file_info
*),BOOL rec
, BOOL dirs
)
505 static int in_do_list
= 0;
507 if (in_do_list
&& rec
)
509 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
515 do_list_recurse
= rec
;
521 init_do_list_queue();
522 add_to_do_list_queue(mask
);
524 while (! do_list_queue_empty())
527 * Need to copy head so that it doesn't become
528 * invalid inside the call to cli_list. This
529 * would happen if the list were expanded
531 * Fix from E. Jay Berkenbilt (ejb@ql.org)
534 pstrcpy(head
, do_list_queue_head());
535 cli_list(cli
, head
, attribute
, do_list_helper
, NULL
);
536 remove_do_list_queue_head();
537 if ((! do_list_queue_empty()) && (fn
== display_finfo
))
539 char* next_file
= do_list_queue_head();
541 if ((strlen(next_file
) >= 2) &&
542 (next_file
[strlen(next_file
) - 1] == '*') &&
543 (next_file
[strlen(next_file
) - 2] == '\\'))
545 save_ch
= next_file
+
546 strlen(next_file
) - 2;
549 DEBUG(0,("\n%s\n",next_file
));
559 if (cli_list(cli
, mask
, attribute
, do_list_helper
, NULL
) == -1)
561 DEBUG(0, ("%s listing %s\n", cli_errstr(cli
), mask
));
566 reset_do_list_queue();
569 /****************************************************************************
570 get a directory listing
571 ****************************************************************************/
572 static void cmd_dir(void)
574 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
580 pstrcpy(mask
,cur_dir
);
581 if(mask
[strlen(mask
)-1]!='\\')
584 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
595 do_list(mask
, attribute
, display_finfo
, recurse
, True
);
599 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
603 /****************************************************************************
604 get a directory listing
605 ****************************************************************************/
606 static void cmd_du(void)
608 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
614 pstrcpy(mask
,cur_dir
);
615 if(mask
[strlen(mask
)-1]!='\\')
618 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
628 do_list(mask
, attribute
, do_du
, recurse
, True
);
632 DEBUG(0, ("Total number of bytes: %.0f\n", dir_total
));
636 /****************************************************************************
637 get a file from rname to lname
638 ****************************************************************************/
639 static void do_get(char *rname
,char *lname
)
642 BOOL newhandle
= False
;
644 struct timeval tp_start
;
645 int read_size
= io_bufsize
;
650 GetTimeOfDay(&tp_start
);
656 fnum
= cli_open(cli
, rname
, O_RDONLY
, DENY_NONE
);
659 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli
),rname
));
663 if(!strcmp(lname
,"-")) {
664 handle
= fileno(stdout
);
666 handle
= sys_open(lname
,O_WRONLY
|O_CREAT
|O_TRUNC
,0644);
670 DEBUG(0,("Error opening local file %s\n",lname
));
675 if (!cli_qfileinfo(cli
, fnum
,
676 &attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
677 !cli_getattrE(cli
, fnum
,
678 &attr
, &size
, NULL
, NULL
, NULL
)) {
679 DEBUG(0,("getattrib: %s\n",cli_errstr(cli
)));
683 DEBUG(2,("getting file %s of size %.0f as %s ",
684 lname
, (double)size
, lname
));
686 if(!(data
= (char *)malloc(read_size
))) {
687 DEBUG(0,("malloc fail for size %d\n", read_size
));
688 cli_close(cli
, fnum
);
693 int n
= cli_read(cli
, fnum
, data
, nread
, read_size
);
697 if (writefile(handle
,data
, n
) != n
) {
698 DEBUG(0,("Error writing local file\n"));
706 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
707 rname
, (long)nread
));
712 if (!cli_close(cli
, fnum
)) {
713 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli
)));
720 if (archive_level
>= 2 && (attr
& aARCH
)) {
721 cli_setatr(cli
, rname
, attr
& ~(uint16
)aARCH
, 0);
725 struct timeval tp_end
;
728 GetTimeOfDay(&tp_end
);
730 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
731 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
732 get_total_time_ms
+= this_time
;
733 get_total_size
+= nread
;
735 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
736 nread
/ (1.024*this_time
+ 1.0e-4),
737 get_total_size
/ (1.024*get_total_time_ms
)));
742 /****************************************************************************
744 ****************************************************************************/
745 static void cmd_get(void)
751 pstrcpy(rname
,cur_dir
);
754 p
= rname
+ strlen(rname
);
756 if (!next_token(NULL
,p
,NULL
,sizeof(rname
)-strlen(rname
))) {
757 DEBUG(0,("get <filename>\n"));
761 dos_clean_name(rname
);
763 next_token(NULL
,lname
,NULL
,sizeof(lname
));
765 do_get(rname
, lname
);
769 /****************************************************************************
770 do a mget operation on one file
771 ****************************************************************************/
772 static void do_mget(file_info
*finfo
)
776 pstring saved_curdir
;
779 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
783 DEBUG(0,("mget aborted\n"));
787 if (finfo
->mode
& aDIR
)
788 slprintf(quest
,sizeof(pstring
)-1,
789 "Get directory %s? ",finfo
->name
);
791 slprintf(quest
,sizeof(pstring
)-1,
792 "Get file %s? ",finfo
->name
);
794 if (prompt
&& !yesno(quest
)) return;
796 if (!(finfo
->mode
& aDIR
)) {
797 pstrcpy(rname
,cur_dir
);
798 pstrcat(rname
,finfo
->name
);
799 do_get(rname
,finfo
->name
);
803 /* handle directories */
804 pstrcpy(saved_curdir
,cur_dir
);
806 pstrcat(cur_dir
,finfo
->name
);
807 pstrcat(cur_dir
,"\\");
809 unix_format(finfo
->name
);
811 strlower(finfo
->name
);
813 if (!directory_exist(finfo
->name
,NULL
) &&
814 mkdir(finfo
->name
,0777) != 0) {
815 DEBUG(0,("failed to create directory %s\n",finfo
->name
));
816 pstrcpy(cur_dir
,saved_curdir
);
820 if (chdir(finfo
->name
) != 0) {
821 DEBUG(0,("failed to chdir to directory %s\n",finfo
->name
));
822 pstrcpy(cur_dir
,saved_curdir
);
826 pstrcpy(mget_mask
,cur_dir
);
827 pstrcat(mget_mask
,"*");
829 do_list(mget_mask
, aSYSTEM
| aHIDDEN
| aDIR
,do_mget
,False
, True
);
831 pstrcpy(cur_dir
,saved_curdir
);
835 /****************************************************************************
836 view the file using the pager
837 ****************************************************************************/
838 static void cmd_more(void)
840 fstring rname
,lname
,pager_cmd
;
844 fstrcpy(rname
,cur_dir
);
847 slprintf(lname
,sizeof(lname
)-1, "%s/smbmore.XXXXXX",tmpdir());
848 fd
= smb_mkstemp(lname
);
850 DEBUG(0,("failed to create temporary file for more\n"));
855 if (!next_token(NULL
,rname
+strlen(rname
),NULL
,sizeof(rname
)-strlen(rname
))) {
856 DEBUG(0,("more <filename>\n"));
860 dos_clean_name(rname
);
864 pager
=getenv("PAGER");
866 slprintf(pager_cmd
,sizeof(pager_cmd
)-1,
867 "%s %s",(pager
? pager
:PAGER
), lname
);
874 /****************************************************************************
876 ****************************************************************************/
877 static void cmd_mget(void)
879 uint16 attribute
= aSYSTEM
| aHIDDEN
;
891 while (next_token(NULL
,p
,NULL
,sizeof(buf
))) {
892 pstrcpy(mget_mask
,cur_dir
);
893 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
894 pstrcat(mget_mask
,"\\");
897 pstrcpy(mget_mask
,p
);
899 pstrcat(mget_mask
,p
);
900 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
904 pstrcpy(mget_mask
,cur_dir
);
905 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
906 pstrcat(mget_mask
,"\\");
907 pstrcat(mget_mask
,"*");
908 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
913 /****************************************************************************
914 make a directory of name "name"
915 ****************************************************************************/
916 static BOOL
do_mkdir(char *name
)
918 if (!cli_mkdir(cli
, name
)) {
919 DEBUG(0,("%s making remote directory %s\n",
920 cli_errstr(cli
),name
));
927 /****************************************************************************
928 Show 8.3 name of a file.
929 ****************************************************************************/
931 static BOOL
do_altname(char *name
)
934 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli
, name
, altname
))) {
935 DEBUG(0,("%s getting alt name for %s\n", cli_errstr(cli
),name
));
938 DEBUG(0,("%s\n", altname
));
943 /****************************************************************************
945 ****************************************************************************/
946 static void cmd_quit(void)
953 /****************************************************************************
955 ****************************************************************************/
956 static void cmd_mkdir(void)
962 pstrcpy(mask
,cur_dir
);
964 if (!next_token(NULL
,p
,NULL
,sizeof(buf
))) {
966 DEBUG(0,("mkdir <dirname>\n"));
977 trim_string(ddir
,".",NULL
);
978 p
= strtok(ddir
,"/\\");
981 if (!cli_chkpath(cli
, ddir2
)) {
985 p
= strtok(NULL
,"/\\");
992 /****************************************************************************
994 ****************************************************************************/
996 static void cmd_altname(void)
1002 pstrcpy(name
,cur_dir
);
1004 if (!next_token(NULL
,p
,NULL
,sizeof(buf
))) {
1005 DEBUG(0,("altname <file>\n"));
1013 /****************************************************************************
1015 ****************************************************************************/
1016 static void do_put(char *rname
,char *lname
)
1022 int maxwrite
=io_bufsize
;
1024 struct timeval tp_start
;
1025 GetTimeOfDay(&tp_start
);
1027 fnum
= cli_open(cli
, rname
, O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
);
1030 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli
),rname
));
1034 /* allow files to be piped into smbclient
1035 jdblair 24.jun.98 */
1036 if (!strcmp(lname
, "-")) {
1038 /* size of file is not known */
1040 f
= sys_fopen(lname
,"r");
1044 DEBUG(0,("Error opening local file %s\n",lname
));
1049 DEBUG(1,("putting file %s as %s ",lname
,
1052 buf
= (char *)malloc(maxwrite
);
1054 DEBUG(0, ("ERROR: Not enough memory!\n"));
1061 if ((n
= readfile(buf
,1,n
,f
)) < 1) {
1062 if((n
== 0) && feof(f
))
1063 break; /* Empty local file. */
1065 DEBUG(0,("Error reading local file: %s\n", strerror(errno
) ));
1069 ret
= cli_write(cli
, fnum
, 0, buf
, nread
, n
);
1072 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli
)));
1079 if (!cli_close(cli
, fnum
)) {
1080 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli
),rname
));
1091 struct timeval tp_end
;
1094 GetTimeOfDay(&tp_end
);
1096 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
1097 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
1098 put_total_time_ms
+= this_time
;
1099 put_total_size
+= nread
;
1101 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1102 nread
/ (1.024*this_time
+ 1.0e-4),
1103 put_total_size
/ (1.024*put_total_time_ms
)));
1114 /****************************************************************************
1116 ****************************************************************************/
1117 static void cmd_put(void)
1124 pstrcpy(rname
,cur_dir
);
1125 pstrcat(rname
,"\\");
1127 if (!next_token(NULL
,p
,NULL
,sizeof(buf
))) {
1128 DEBUG(0,("put <filename>\n"));
1133 if (next_token(NULL
,p
,NULL
,sizeof(buf
)))
1136 pstrcat(rname
,lname
);
1138 dos_clean_name(rname
);
1142 /* allow '-' to represent stdin
1143 jdblair, 24.jun.98 */
1144 if (!file_exist(lname
,&st
) &&
1145 (strcmp(lname
,"-"))) {
1146 DEBUG(0,("%s does not exist\n",lname
));
1151 do_put(rname
,lname
);
1154 /*************************************
1156 *************************************/
1158 static struct file_list
{
1159 struct file_list
*prev
, *next
;
1164 /****************************************************************************
1165 Free a file_list structure
1166 ****************************************************************************/
1168 static void free_file_list (struct file_list
* list
)
1170 struct file_list
*tmp
;
1175 DLIST_REMOVE(list
, list
);
1176 SAFE_FREE(tmp
->file_path
);
1181 /****************************************************************************
1182 seek in a directory/file list until you get something that doesn't start with
1184 ****************************************************************************/
1185 static BOOL
seek_list(struct file_list
*list
, char *name
)
1188 trim_string(list
->file_path
,"./","\n");
1189 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
1198 /****************************************************************************
1199 set the file selection mask
1200 ****************************************************************************/
1201 static void cmd_select(void)
1203 pstrcpy(fileselection
,"");
1204 next_token(NULL
,fileselection
,NULL
,sizeof(fileselection
));
1207 /****************************************************************************
1208 Recursive file matching function act as find
1209 match must be always set to True when calling this function
1210 ****************************************************************************/
1211 static int file_find(struct file_list
**list
, const char *directory
,
1212 const char *expression
, BOOL match
)
1215 struct file_list
*entry
;
1216 struct stat statbuf
;
1222 dir
= opendir(directory
);
1223 if (!dir
) return -1;
1225 while ((dname
= readdirname(dir
))) {
1226 if (!strcmp("..", dname
)) continue;
1227 if (!strcmp(".", dname
)) continue;
1229 if (asprintf(&path
, "%s/%s", directory
, dname
) <= 0) {
1234 if (!match
|| !ms_fnmatch(expression
, dname
)) {
1236 ret
= stat(path
, &statbuf
);
1238 if (S_ISDIR(statbuf
.st_mode
)) {
1240 ret
= file_find(list
, path
, expression
, False
);
1243 DEBUG(0,("file_find: cannot stat file %s\n", path
));
1252 entry
= (struct file_list
*) malloc(sizeof (struct file_list
));
1254 DEBUG(0,("Out of memory in file_find\n"));
1258 entry
->file_path
= path
;
1259 entry
->isdir
= isdir
;
1260 DLIST_ADD(*list
, entry
);
1270 /****************************************************************************
1272 ****************************************************************************/
1273 static void cmd_mput(void)
1278 while (next_token(NULL
,p
,NULL
,sizeof(buf
))) {
1280 struct file_list
*temp_list
;
1281 char *quest
, *lname
, *rname
;
1285 ret
= file_find(&file_list
, ".", p
, True
);
1287 free_file_list(file_list
);
1295 for (temp_list
= file_list
; temp_list
;
1296 temp_list
= temp_list
->next
) {
1299 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0)
1301 trim_string(lname
, "./", "/");
1303 /* check if it's a directory */
1304 if (temp_list
->isdir
) {
1305 /* if (!recurse) continue; */
1308 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) break;
1309 if (prompt
&& !yesno(quest
)) { /* No */
1310 /* Skip the directory */
1311 lname
[strlen(lname
)-1] = '/';
1312 if (!seek_list(temp_list
, lname
))
1316 if (asprintf(&rname
, "%s%s", cur_dir
, lname
) < 0) break;
1318 if (!cli_chkpath(cli
, rname
) &&
1320 DEBUG (0, ("Unable to make dir, skipping..."));
1321 /* Skip the directory */
1322 lname
[strlen(lname
)-1] = '/';
1323 if (!seek_list(temp_list
, lname
))
1330 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) break;
1331 if (prompt
&& !yesno(quest
)) /* No */
1336 if (asprintf(&rname
, "%s%s", cur_dir
, lname
) < 0) break;
1341 do_put(rname
, lname
);
1343 free_file_list(file_list
);
1351 /****************************************************************************
1353 ****************************************************************************/
1354 static void do_cancel(int job
)
1356 if (cli_printjob_del(cli
, job
)) {
1357 printf("Job %d cancelled\n",job
);
1359 printf("Error cancelling job %d : %s\n",job
,cli_errstr(cli
));
1364 /****************************************************************************
1366 ****************************************************************************/
1367 static void cmd_cancel(void)
1372 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1373 printf("cancel <jobid> ...\n");
1379 } while (next_token(NULL
,buf
,NULL
,sizeof(buf
)));
1383 /****************************************************************************
1385 ****************************************************************************/
1386 static void cmd_print(void)
1392 if (!next_token(NULL
,lname
,NULL
, sizeof(lname
))) {
1393 DEBUG(0,("print <filename>\n"));
1397 pstrcpy(rname
,lname
);
1398 p
= strrchr(rname
,'/');
1400 slprintf(rname
, sizeof(rname
)-1, "%s-%d", p
+1, (int)sys_getpid());
1403 if (strequal(lname
,"-")) {
1404 slprintf(rname
, sizeof(rname
)-1, "stdin-%d", (int)sys_getpid());
1407 do_put(rname
, lname
);
1411 /****************************************************************************
1412 show a print queue entry
1413 ****************************************************************************/
1414 static void queue_fn(struct print_job_info
*p
)
1416 DEBUG(0,("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
));
1419 /****************************************************************************
1421 ****************************************************************************/
1422 static void cmd_queue(void)
1424 cli_print_queue(cli
, queue_fn
);
1427 /****************************************************************************
1429 ****************************************************************************/
1430 static void do_del(file_info
*finfo
)
1434 pstrcpy(mask
,cur_dir
);
1435 pstrcat(mask
,finfo
->name
);
1437 if (finfo
->mode
& aDIR
)
1440 if (!cli_unlink(cli
, mask
)) {
1441 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli
),mask
));
1445 /****************************************************************************
1447 ****************************************************************************/
1448 static void cmd_del(void)
1452 uint16 attribute
= aSYSTEM
| aHIDDEN
;
1457 pstrcpy(mask
,cur_dir
);
1459 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1460 DEBUG(0,("del <filename>\n"));
1465 do_list(mask
, attribute
,do_del
,False
,False
);
1468 /****************************************************************************
1469 ****************************************************************************/
1470 static void cmd_open(void)
1475 pstrcpy(mask
,cur_dir
);
1477 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1478 DEBUG(0,("del <filename>\n"));
1483 cli_open(cli
, mask
, O_RDWR
, DENY_ALL
);
1487 /****************************************************************************
1489 ****************************************************************************/
1490 static void cmd_rmdir(void)
1495 pstrcpy(mask
,cur_dir
);
1497 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1498 DEBUG(0,("rmdir <dirname>\n"));
1503 if (!cli_rmdir(cli
, mask
)) {
1504 DEBUG(0,("%s removing remote directory file %s\n",
1505 cli_errstr(cli
),mask
));
1509 /****************************************************************************
1511 ****************************************************************************/
1513 static void cmd_link(void)
1518 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1519 DEBUG(0,("Server doesn't support UNIX CIFS calls.\n"));
1523 pstrcpy(src
,cur_dir
);
1524 pstrcpy(dest
,cur_dir
);
1526 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1527 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1528 DEBUG(0,("link <src> <dest>\n"));
1535 if (!cli_unix_hardlink(cli
, src
, dest
)) {
1536 DEBUG(0,("%s linking files (%s -> %s)\n",
1537 cli_errstr(cli
), src
, dest
));
1542 /****************************************************************************
1544 ****************************************************************************/
1546 static void cmd_symlink(void)
1551 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1552 DEBUG(0,("Server doesn't support UNIX CIFS calls.\n"));
1556 pstrcpy(src
,cur_dir
);
1557 pstrcpy(dest
,cur_dir
);
1559 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1560 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1561 DEBUG(0,("symlink <src> <dest>\n"));
1568 if (!cli_unix_symlink(cli
, src
, dest
)) {
1569 DEBUG(0,("%s symlinking files (%s -> %s)\n",
1570 cli_errstr(cli
), src
, dest
));
1575 /****************************************************************************
1577 ****************************************************************************/
1579 static void cmd_chmod(void)
1585 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1586 DEBUG(0,("Server doesn't support UNIX CIFS calls.\n"));
1590 pstrcpy(src
,cur_dir
);
1592 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1593 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1594 DEBUG(0,("chmod mode file\n"));
1598 mode
= (mode_t
)strtol(buf
, NULL
, 8);
1601 if (!cli_unix_chmod(cli
, src
, mode
)) {
1602 DEBUG(0,("%s chmod file %s 0%o\n",
1603 cli_errstr(cli
), src
, (unsigned int)mode
));
1608 /****************************************************************************
1610 ****************************************************************************/
1612 static void cmd_chown(void)
1617 fstring buf
, buf2
, buf3
;
1619 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1620 DEBUG(0,("Server doesn't support UNIX CIFS calls.\n"));
1624 pstrcpy(src
,cur_dir
);
1626 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1627 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
)) ||
1628 !next_token(NULL
,buf3
,NULL
, sizeof(buf3
))) {
1629 DEBUG(0,("chown uid gid file\n"));
1633 uid
= (uid_t
)atoi(buf
);
1634 gid
= (gid_t
)atoi(buf2
);
1637 if (!cli_unix_chown(cli
, src
, uid
, gid
)) {
1638 DEBUG(0,("%s chown file %s uid=%d, gid=%d\n",
1639 cli_errstr(cli
), src
, (int)uid
, (int)gid
));
1644 /****************************************************************************
1646 ****************************************************************************/
1647 static void cmd_rename(void)
1652 pstrcpy(src
,cur_dir
);
1653 pstrcpy(dest
,cur_dir
);
1655 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1656 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1657 DEBUG(0,("rename <src> <dest>\n"));
1664 if (!cli_rename(cli
, src
, dest
)) {
1665 DEBUG(0,("%s renaming files\n",cli_errstr(cli
)));
1671 /****************************************************************************
1672 toggle the prompt flag
1673 ****************************************************************************/
1674 static void cmd_prompt(void)
1677 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
1681 /****************************************************************************
1682 set the newer than time
1683 ****************************************************************************/
1684 static void cmd_newer(void)
1688 SMB_STRUCT_STAT sbuf
;
1690 ok
= next_token(NULL
,buf
,NULL
,sizeof(buf
));
1691 if (ok
&& (sys_stat(buf
,&sbuf
) == 0)) {
1692 newer_than
= sbuf
.st_mtime
;
1693 DEBUG(1,("Getting files newer than %s",
1694 asctime(LocalTime(&newer_than
))));
1699 if (ok
&& newer_than
== 0)
1700 DEBUG(0,("Error setting newer-than time\n"));
1703 /****************************************************************************
1704 set the archive level
1705 ****************************************************************************/
1706 static void cmd_archive(void)
1710 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1711 archive_level
= atoi(buf
);
1713 DEBUG(0,("Archive level is %d\n",archive_level
));
1716 /****************************************************************************
1717 toggle the lowercaseflag
1718 ****************************************************************************/
1719 static void cmd_lowercase(void)
1721 lowercase
= !lowercase
;
1722 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
1728 /****************************************************************************
1729 toggle the recurse flag
1730 ****************************************************************************/
1731 static void cmd_recurse(void)
1734 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
1737 /****************************************************************************
1738 toggle the translate flag
1739 ****************************************************************************/
1740 static void cmd_translate(void)
1742 translation
= !translation
;
1743 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1744 translation
?"on":"off"));
1748 /****************************************************************************
1749 do a printmode command
1750 ****************************************************************************/
1751 static void cmd_printmode(void)
1756 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1757 if (strequal(buf
,"text")) {
1760 if (strequal(buf
,"graphics"))
1763 printmode
= atoi(buf
);
1770 fstrcpy(mode
,"text");
1773 fstrcpy(mode
,"graphics");
1776 slprintf(mode
,sizeof(mode
)-1,"%d",printmode
);
1780 DEBUG(2,("the printmode is now %s\n",mode
));
1783 /****************************************************************************
1785 ****************************************************************************/
1786 static void cmd_lcd(void)
1791 if (next_token(NULL
,buf
,NULL
,sizeof(buf
)))
1793 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
1796 /****************************************************************************
1798 ****************************************************************************/
1799 static void browse_fn(const char *name
, uint32 m
,
1800 const char *comment
, void *state
)
1808 case STYPE_DISKTREE
:
1809 fstrcpy(typestr
,"Disk"); break;
1811 fstrcpy(typestr
,"Printer"); break;
1813 fstrcpy(typestr
,"Device"); break;
1815 fstrcpy(typestr
,"IPC"); break;
1818 printf("\t%-15.15s%-10.10s%s\n",
1819 name
, typestr
,comment
);
1823 /****************************************************************************
1824 try and browse available connections on a host
1825 ****************************************************************************/
1826 static BOOL
browse_host(BOOL sort
)
1830 printf("\n\tSharename Type Comment\n");
1831 printf("\t--------- ---- -------\n");
1833 if((ret
= cli_RNetShareEnum(cli
, browse_fn
, NULL
)) == -1)
1834 printf("Error returning browse list: %s\n", cli_errstr(cli
));
1839 /****************************************************************************
1841 ****************************************************************************/
1842 static void server_fn(const char *name
, uint32 m
,
1843 const char *comment
, void *state
)
1845 printf("\t%-16.16s %s\n", name
, comment
);
1848 /****************************************************************************
1849 try and browse available connections on a host
1850 ****************************************************************************/
1851 static BOOL
list_servers(char *wk_grp
)
1853 if (!cli
->server_domain
) return False
;
1855 printf("\n\tServer Comment\n");
1856 printf("\t--------- -------\n");
1858 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
, NULL
);
1860 printf("\n\tWorkgroup Master\n");
1861 printf("\t--------- -------\n");
1863 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
, server_fn
, NULL
);
1867 /* Some constants for completing filename arguments */
1869 #define COMPL_NONE 0 /* No completions */
1870 #define COMPL_REMOTE 1 /* Complete remote filename */
1871 #define COMPL_LOCAL 2 /* Complete local filename */
1873 /* This defines the commands supported by this client.
1874 * NOTE: The "!" must be the last one in the list because it's fn pointer
1875 * field is NULL, and NULL in that field is used in process_tok()
1876 * (below) to indicate the end of the list. crh
1883 char compl_args
[2]; /* Completion argument info */
1886 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
1887 {"altname",cmd_altname
,"<file> show alt name",{COMPL_NONE
,COMPL_NONE
}},
1888 {"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
}},
1889 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
1890 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
1891 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
1892 {"chmod",cmd_chmod
,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE
,COMPL_REMOTE
}},
1893 {"chown",cmd_chown
,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE
,COMPL_REMOTE
}},
1894 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1895 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1896 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1897 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1898 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
1899 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
1900 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
1901 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
1902 {"link",cmd_link
,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
1903 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
1904 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1905 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
1906 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
1907 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1908 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
1909 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
1910 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1911 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
1912 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
1913 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
1914 {"printmode",cmd_printmode
,"<graphics or text> set the print mode",{COMPL_NONE
,COMPL_NONE
}},
1915 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
1916 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
1917 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
1918 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1919 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
1920 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1921 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
1922 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
1923 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
1924 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1925 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
1926 {"setmode",cmd_setmode
,"filename <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
1927 {"symlink",cmd_symlink
,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
1928 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
1929 {"tarmode",cmd_tarmode
,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
1930 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
1932 /* Yes, this must be here, see crh's comment above. */
1933 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
1934 {"",NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
1938 /*******************************************************************
1939 lookup a command string in the list of commands, including
1941 ******************************************************************/
1942 static int process_tok(fstring tok
)
1944 int i
= 0, matches
= 0;
1946 int tok_len
= strlen(tok
);
1948 while (commands
[i
].fn
!= NULL
) {
1949 if (strequal(commands
[i
].name
,tok
)) {
1953 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
1962 else if (matches
== 1)
1968 /****************************************************************************
1970 ****************************************************************************/
1971 static void cmd_help(void)
1976 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1977 if ((i
= process_tok(buf
)) >= 0)
1978 DEBUG(0,("HELP %s:\n\t%s\n\n",commands
[i
].name
,commands
[i
].description
));
1980 while (commands
[i
].description
) {
1981 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
1982 DEBUG(0,("%-15s",commands
[i
].name
));
1990 /****************************************************************************
1991 process a -c command string
1992 ****************************************************************************/
1993 static void process_command_string(char *cmd
)
1998 /* establish the connection if not already */
2001 cli
= do_connect(desthost
, service
);
2006 while (cmd
[0] != '\0') {
2011 if ((p
= strchr(cmd
, ';')) == 0) {
2012 strncpy(line
, cmd
, 999);
2016 if (p
- cmd
> 999) p
= cmd
+ 999;
2017 strncpy(line
, cmd
, p
- cmd
);
2018 line
[p
- cmd
] = '\0';
2022 /* and get the first part of the command */
2024 if (!next_token(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
2026 if ((i
= process_tok(tok
)) >= 0) {
2028 } else if (i
== -2) {
2029 DEBUG(0,("%s: command abbreviation ambiguous\n",tok
));
2031 DEBUG(0,("%s: command not found\n",tok
));
2036 /****************************************************************************
2037 handle completion of commands for readline
2038 ****************************************************************************/
2039 static char **completion_fn(const char *text
, int start
, int end
)
2041 #define MAX_COMPLETIONS 100
2045 /* for words not at the start of the line fallback to filename completion */
2046 if (start
) return NULL
;
2048 matches
= (char **)malloc(sizeof(matches
[0])*MAX_COMPLETIONS
);
2049 if (!matches
) return NULL
;
2051 matches
[count
++] = strdup(text
);
2052 if (!matches
[0]) return NULL
;
2054 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
2055 if (strncmp(text
, commands
[i
].name
, strlen(text
)) == 0) {
2056 matches
[count
] = strdup(commands
[i
].name
);
2057 if (!matches
[count
]) return NULL
;
2063 SAFE_FREE(matches
[0]);
2064 matches
[0] = strdup(matches
[1]);
2066 matches
[count
] = NULL
;
2071 /****************************************************************************
2072 make sure we swallow keepalives during idle time
2073 ****************************************************************************/
2074 static void readline_callback(void)
2077 struct timeval timeout
;
2078 static time_t last_t
;
2083 if (t
- last_t
< 5) return;
2089 FD_SET(cli
->fd
,&fds
);
2092 timeout
.tv_usec
= 0;
2093 sys_select_intr(cli
->fd
+1,&fds
,NULL
,NULL
,&timeout
);
2095 /* We deliberately use receive_smb instead of
2096 client_receive_smb as we want to receive
2097 session keepalives and then drop them here.
2099 if (FD_ISSET(cli
->fd
,&fds
)) {
2100 receive_smb(cli
->fd
,cli
->inbuf
,0);
2104 cli_chkpath(cli
, "\\");
2108 /****************************************************************************
2109 process commands on stdin
2110 ****************************************************************************/
2111 static void process_stdin(void)
2121 /* display a prompt */
2122 slprintf(the_prompt
, sizeof(the_prompt
)-1, "smb: %s> ", cur_dir
);
2123 line
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
2127 /* special case - first char is ! */
2133 /* and get the first part of the command */
2135 if (!next_token(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
2137 if ((i
= process_tok(tok
)) >= 0) {
2139 } else if (i
== -2) {
2140 DEBUG(0,("%s: command abbreviation ambiguous\n",tok
));
2142 DEBUG(0,("%s: command not found\n",tok
));
2148 /*****************************************************
2149 return a connection to a server
2150 *******************************************************/
2151 struct cli_state
*do_connect(char *server
, char *share
)
2153 struct cli_state
*c
;
2154 struct nmb_name called
, calling
;
2157 fstring servicename
;
2160 /* make a copy so we don't modify the global string 'service' */
2161 safe_strcpy(servicename
, share
, sizeof(servicename
)-1);
2162 sharename
= servicename
;
2163 if (*sharename
== '\\') {
2164 server
= sharename
+2;
2165 sharename
= strchr(server
,'\\');
2166 if (!sharename
) return NULL
;
2175 make_nmb_name(&calling
, global_myname
, 0x0);
2176 make_nmb_name(&called
, server
, name_type
);
2180 if (have_ip
) ip
= dest_ip
;
2182 /* have to open a new connection */
2183 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, port
) == 0) ||
2184 !cli_connect(c
, server_n
, &ip
)) {
2185 DEBUG(0,("Connection to %s failed\n", server_n
));
2189 c
->protocol
= max_protocol
;
2191 if (!cli_session_request(c
, &calling
, &called
)) {
2193 DEBUG(0,("session request to %s failed (%s)\n",
2194 called
.name
, cli_errstr(c
)));
2196 if ((p
=strchr(called
.name
, '.'))) {
2200 if (strcmp(called
.name
, "*SMBSERVER")) {
2201 make_nmb_name(&called
, "*SMBSERVER", 0x20);
2207 DEBUG(4,(" session request ok\n"));
2209 if (!cli_negprot(c
)) {
2210 DEBUG(0,("protocol negotiation failed\n"));
2216 char *pass
= getpass("Password: ");
2218 pstrcpy(password
, pass
);
2222 if (!cli_session_setup(c
, username
,
2223 password
, strlen(password
),
2224 password
, strlen(password
),
2226 /* if a password was not supplied then try again with a null username */
2227 if (password
[0] || !username
[0] ||
2228 !cli_session_setup(c
, "", "", 0, "", 0, workgroup
)) {
2229 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
2233 DEBUG(0,("Anonymous login successful\n"));
2237 * These next two lines are needed to emulate
2238 * old client behaviour for people who have
2239 * scripts based on client output.
2240 * QUESTION ? Do we want to have a 'client compatibility
2241 * mode to turn these on/off ? JRA.
2244 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
2245 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
2246 c
->server_domain
,c
->server_os
,c
->server_type
));
2248 DEBUG(4,(" session setup ok\n"));
2250 if (!cli_send_tconX(c
, sharename
, "?????",
2251 password
, strlen(password
)+1)) {
2252 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
2257 DEBUG(4,(" tconx ok\n"));
2263 /****************************************************************************
2264 process commands from the client
2265 ****************************************************************************/
2266 static BOOL
process(char *base_directory
)
2268 cli
= do_connect(desthost
, service
);
2273 if (*base_directory
) do_cd(base_directory
);
2276 process_command_string(cmdstr
);
2285 /****************************************************************************
2286 usage on the program
2287 ****************************************************************************/
2288 static void usage(char *pname
)
2290 DEBUG(0,("Usage: %s service <password> [options]", pname
));
2292 DEBUG(0,("\nVersion %s\n",VERSION
));
2293 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
2294 DEBUG(0,("\t-O socket_options socket options to use\n"));
2295 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
2296 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
2297 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
2298 DEBUG(0,("\t-N don't ask for a password\n"));
2299 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
2300 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
2301 DEBUG(0,("\t-P connect to service as a printer\n"));
2302 DEBUG(0,("\t-p port connect to the specified port\n"));
2303 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
2304 DEBUG(0,("\t-h Print this help message.\n"));
2305 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
2306 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
2307 DEBUG(0,("\t-U username set the network username\n"));
2308 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
2309 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
2310 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
2311 DEBUG(0,("\t-A filename get the credentials from a file\n"));
2312 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
2313 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
2314 DEBUG(0,("\t-D directory start from directory\n"));
2315 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
2316 DEBUG(0,("\t-b xmit/send buffer changes the transmit/send buffer (default: 65520)\n"));
2321 /****************************************************************************
2322 get a password from a a file or file descriptor
2324 ****************************************************************************/
2325 static void get_password_file(void)
2329 BOOL close_it
= False
;
2333 if ((p
= getenv("PASSWD_FD")) != NULL
) {
2334 pstrcpy(spec
, "descriptor ");
2336 sscanf(p
, "%d", &fd
);
2338 } else if ((p
= getenv("PASSWD_FILE")) != NULL
) {
2339 fd
= sys_open(p
, O_RDONLY
, 0);
2342 fprintf(stderr
, "Error opening PASSWD_FILE %s: %s\n",
2343 spec
, strerror(errno
));
2349 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
2350 p
&& p
- pass
< sizeof(pass
);) {
2351 switch (read(fd
, p
, 1)) {
2353 if (*p
!= '\n' && *p
!= '\0') {
2354 *++p
= '\0'; /* advance p, and null-terminate pass */
2359 *p
= '\0'; /* null-terminate it, just in case... */
2360 p
= NULL
; /* then force the loop condition to become false */
2363 fprintf(stderr
, "Error reading password from file %s: %s\n",
2364 spec
, "empty password\n");
2369 fprintf(stderr
, "Error reading password from file %s: %s\n",
2370 spec
, strerror(errno
));
2374 pstrcpy(password
, pass
);
2381 /****************************************************************************
2383 ****************************************************************************/
2384 static int do_host_query(char *query_host
)
2386 cli
= do_connect(query_host
, "IPC$");
2391 list_servers(workgroup
);
2399 /****************************************************************************
2400 handle a tar operation
2401 ****************************************************************************/
2402 static int do_tar_op(char *base_directory
)
2406 /* do we already have a connection? */
2408 cli
= do_connect(desthost
, service
);
2415 if (*base_directory
) do_cd(base_directory
);
2424 /****************************************************************************
2425 handle a message operation
2426 ****************************************************************************/
2427 static int do_message_op(void)
2430 struct nmb_name called
, calling
;
2434 make_nmb_name(&calling
, global_myname
, 0x0);
2435 make_nmb_name(&called
, desthost
, name_type
);
2438 if (have_ip
) ip
= dest_ip
;
2440 if (!(cli
=cli_initialise(NULL
)) || (cli_set_port(cli
, port
) == 0) || !cli_connect(cli
, desthost
, &ip
)) {
2441 DEBUG(0,("Connection to %s failed\n", desthost
));
2445 if (!cli_session_request(cli
, &calling
, &called
)) {
2446 DEBUG(0,("session request failed\n"));
2458 /****************************************************************************
2460 ****************************************************************************/
2461 int main(int argc
,char *argv
[])
2463 fstring base_directory
;
2464 char *pname
= argv
[0];
2467 extern char *optarg
;
2470 BOOL message
= False
;
2471 extern char tar_type
;
2472 static pstring servicesf
= CONFIGFILE
;
2474 pstring new_name_resolve_order
;
2479 pstrcpy(term_code
, KANJI
);
2485 *base_directory
= 0;
2487 *new_name_resolve_order
= 0;
2490 AllowDebugChange
= False
;
2492 setup_logging(pname
,True
);
2495 * If the -E option is given, be careful not to clobber stdout
2496 * before processing the options. 28.Feb.99, richard@hacom.nl.
2497 * Also pre-parse the -s option to get the service file name.
2500 for (opt
= 1; opt
< argc
; opt
++) {
2501 if (strcmp(argv
[opt
], "-E") == 0)
2503 else if(strncmp(argv
[opt
], "-s", 2) == 0) {
2504 if(argv
[opt
][2] != '\0')
2505 pstrcpy(servicesf
, &argv
[opt
][2]);
2506 else if(argv
[opt
+1] != NULL
) {
2508 * At least one more arg left.
2510 pstrcpy(servicesf
, argv
[opt
+1]);
2516 else if(strncmp(argv
[opt
], "-d", 2) == 0) {
2517 if(argv
[opt
][2] != '\0') {
2518 if (argv
[opt
][2] == 'A')
2521 DEBUGLEVEL
= atoi(&argv
[opt
][2]);
2522 } else if(argv
[opt
+1] != NULL
) {
2524 * At least one more arg left.
2526 if (argv
[opt
+1][0] == 'A')
2529 DEBUGLEVEL
= atoi(argv
[opt
+1]);
2538 charset_initialise();
2540 in_client
= True
; /* Make sure that we tell lp_load we are */
2542 if (!lp_load(servicesf
,True
,False
,False
)) {
2543 fprintf(stderr
, "Can't load %s - run testparm to debug it\n", servicesf
);
2546 codepage_initialise(lp_client_code_page());
2552 pstrcpy(workgroup
,lp_workgroup());
2558 if (getenv("USER")) {
2559 pstrcpy(username
,getenv("USER"));
2561 /* modification to support userid%passwd syntax in the USER var
2562 25.Aug.97, jdblair@uab.edu */
2564 if ((p
=strchr(username
,'%'))) {
2566 pstrcpy(password
,p
+1);
2568 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password
));
2573 /* modification to support PASSWD environmental var
2574 25.Aug.97, jdblair@uab.edu */
2575 if (getenv("PASSWD")) {
2576 pstrcpy(password
,getenv("PASSWD"));
2580 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2581 get_password_file();
2585 if (*username
== 0 && getenv("LOGNAME")) {
2586 pstrcpy(username
,getenv("LOGNAME"));
2590 if (*username
== 0) {
2591 pstrcpy(username
,"GUEST");
2599 if (*argv
[1] != '-') {
2600 pstrcpy(service
,argv
[1]);
2601 /* Convert any '/' characters in the service name to '\' characters */
2602 string_replace( service
, '/','\\');
2606 if (count_chars(service
,'\\') < 3) {
2608 printf("\n%s: Not enough '\\' characters in service\n",service
);
2612 if (argc
> 1 && (*argv
[1] != '-')) {
2614 pstrcpy(password
,argv
[1]);
2615 memset(argv
[1],'X',strlen(argv
[1]));
2622 getopt(argc
, argv
,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:")) != EOF
) {
2625 pstrcpy(servicesf
, optarg
);
2628 pstrcpy(user_socket_options
,optarg
);
2631 pstrcpy(new_name_resolve_order
, optarg
);
2634 name_type
= 0x03; /* messages are sent to NetBIOS name type 0x3 */
2635 pstrcpy(desthost
,optarg
);
2640 extern pstring global_scope
;
2641 pstrcpy(global_scope
,optarg
);
2642 strupper(global_scope
);
2649 pstrcpy(global_myname
,optarg
);
2655 DEBUGLEVEL
= atoi(optarg
);
2658 /* not needed anymore */
2661 port
= atoi(optarg
);
2664 slprintf(logfile
,sizeof(logfile
)-1, "%s.client",optarg
);
2665 lp_set_logfile(logfile
);
2673 dest_ip
= *interpret_addr2(optarg
);
2674 if (is_zero_ip(dest_ip
))
2685 pstrcpy(username
,optarg
);
2686 if ((lp
=strchr(username
,'%'))) {
2688 pstrcpy(password
,lp
+1);
2690 memset(strchr(optarg
,'%')+1,'X',strlen(password
));
2700 char *ptr
, *val
, *param
;
2702 if ((auth
=sys_fopen(optarg
, "r")) == NULL
)
2704 /* fail if we can't open the credentials file */
2705 DEBUG(0,("ERROR: Unable to open credentials file!\n"));
2711 /* get a line from the file */
2712 if (!fgets (buf
, sizeof(buf
), auth
))
2716 if ((len
) && (buf
[len
-1]=='\n'))
2724 /* break up the line into parameter & value.
2725 will need to eat a little whitespace possibly */
2727 if (!(ptr
= strchr (buf
, '=')))
2732 /* eat leading white space */
2733 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
2736 if (strwicmp("password", param
) == 0)
2738 pstrcpy(password
, val
);
2741 else if (strwicmp("username", param
) == 0)
2742 pstrcpy(username
, val
);
2743 else if (strwicmp("domain", param
) == 0)
2744 pstrcpy(workgroup
,val
);
2745 memset(buf
, 0, sizeof(buf
));
2753 while(*p
== '\\' || *p
== '/')
2755 pstrcpy(query_host
,p
);
2758 pstrcpy(term_code
, optarg
);
2761 max_protocol
= interpret_protocol(optarg
, max_protocol
);
2764 pstrcpy(workgroup
,optarg
);
2767 if (!tar_parseargs(argc
, argv
, optarg
, optind
)) {
2773 pstrcpy(base_directory
,optarg
);
2779 io_bufsize
= MAX(1, atoi(optarg
));
2787 get_myname((*global_myname
)?NULL
:global_myname
);
2789 if(*new_name_resolve_order
)
2790 lp_set_name_resolve_order(new_name_resolve_order
);
2793 interpret_coding_system(term_code
);
2795 if (!tar_type
&& !*query_host
&& !*service
&& !message
) {
2800 DEBUG( 3, ( "Client started (version %s).\n", VERSION
) );
2804 process_command_string(cmdstr
);
2805 return do_tar_op(base_directory
);
2808 if ((p
=strchr(query_host
,'#'))) {
2811 sscanf(p
, "%x", &name_type
);
2815 return do_host_query(query_host
);
2819 return do_message_op();
2822 if (!process(base_directory
)) {