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;
45 extern struct in_addr ipzero
;
47 static int name_type
= 0x20;
48 static int max_protocol
= PROTOCOL_NT1
;
49 extern pstring user_socket_options
;
51 static int process_tok(fstring tok
);
52 static void cmd_help(void);
54 /* 30 second timeout on most commands */
55 #define CLIENT_TIMEOUT (30*1000)
56 #define SHORT_TIMEOUT (5*1000)
58 /* value for unused fid field in trans2 secondary request */
59 #define FID_UNUSED (0xFFFF)
61 time_t newer_than
= 0;
62 int archive_level
= 0;
64 BOOL translation
= False
;
68 /* clitar bits insert */
71 extern BOOL tar_reset
;
75 mode_t myumask
= 0755;
81 static BOOL recurse
= False
;
82 BOOL lowercase
= False
;
84 struct in_addr dest_ip
;
86 #define SEPARATORS " \t\n\r"
88 BOOL abort_mget
= True
;
90 pstring fileselection
= "";
92 extern file_info def_finfo
;
95 int get_total_size
= 0;
96 int get_total_time_ms
= 0;
97 int put_total_size
= 0;
98 int put_total_time_ms
= 0;
101 static double dir_total
;
105 /****************************************************************************
106 write to a local file with CR/LF->LF translation if appropriate. return the
107 number taken from the buffer. This may not equal the number written.
108 ****************************************************************************/
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 ****************************************************************************/
136 static int readfile(char *b
, int size
, int n
, FILE *f
)
141 if (!translation
|| (size
!= 1))
142 return(fread(b
,size
,n
,f
));
145 while (i
< (n
- 1) && (i
< BUFFER_SIZE
)) {
146 if ((c
= getc(f
)) == EOF
) {
150 if (c
== '\n') { /* change all LFs to CR/LF */
161 /****************************************************************************
163 ****************************************************************************/
164 static void send_message(void)
169 if (!cli_message_start(cli
, desthost
, username
, &grp_id
)) {
170 DEBUG(0,("message start: %s\n", cli_errstr(cli
)));
175 printf("Connected. Type your message, ending it with a Control-D\n");
177 while (!feof(stdin
) && total_len
< 1600) {
178 int maxlen
= MIN(1600 - total_len
,127);
185 for (l
=0;l
<maxlen
&& (c
=fgetc(stdin
))!=EOF
;l
++) {
192 * The message is in UNIX codepage format. Convert to
193 * DOS before sending.
196 unix_to_dos(msg
, True
);
198 if (!cli_message_text(cli
, msg
, l
, grp_id
)) {
199 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli
));
206 if (total_len
>= 1600)
207 printf("the message was truncated to 1600 bytes\n");
209 printf("sent %d bytes\n",total_len
);
211 if (!cli_message_end(cli
, grp_id
)) {
212 printf("SMBsendend failed (%s)\n",cli_errstr(cli
));
219 /****************************************************************************
220 check the space on a device
221 ****************************************************************************/
222 static void do_dskattr(void)
224 int total
, bsize
, avail
;
226 if (!cli_dskattr(cli
, &bsize
, &total
, &avail
)) {
227 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli
)));
231 DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
232 total
, bsize
, avail
));
235 /****************************************************************************
237 ****************************************************************************/
238 static void cmd_pwd(void)
240 DEBUG(0,("Current directory is %s",service
));
241 DEBUG(0,("%s\n",cur_dir
));
245 /****************************************************************************
246 change directory - inner section
247 ****************************************************************************/
248 static void do_cd(char *newdir
)
256 /* Save the current directory in case the
257 new directory is invalid */
258 pstrcpy(saved_dir
, cur_dir
);
263 if (*(cur_dir
+strlen(cur_dir
)-1) != '\\') {
264 pstrcat(cur_dir
, "\\");
266 dos_clean_name(cur_dir
);
267 pstrcpy(dname
,cur_dir
);
268 pstrcat(cur_dir
,"\\");
269 dos_clean_name(cur_dir
);
271 if (!strequal(cur_dir
,"\\")) {
272 if (!cli_chkpath(cli
, dname
)) {
273 DEBUG(0,("cd %s: %s\n", dname
, cli_errstr(cli
)));
274 pstrcpy(cur_dir
,saved_dir
);
278 pstrcpy(cd_path
,cur_dir
);
281 /****************************************************************************
283 ****************************************************************************/
284 static void cmd_cd(void)
288 if (next_token(NULL
,buf
,NULL
,sizeof(buf
)))
291 DEBUG(0,("Current directory is %s\n",cur_dir
));
295 /*******************************************************************
296 decide if a file should be operated on
297 ********************************************************************/
298 static BOOL
do_this_one(file_info
*finfo
)
300 if (finfo
->mode
& aDIR
) return(True
);
302 if (*fileselection
&&
303 !mask_match(finfo
->name
,fileselection
,False
)) {
304 DEBUG(3,("match_match %s failed\n", finfo
->name
));
308 if (newer_than
&& finfo
->mtime
< newer_than
) {
309 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
313 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& aARCH
)) {
314 DEBUG(3,("archive %s failed\n", finfo
->name
));
321 /****************************************************************************
322 display info about a file
323 ****************************************************************************/
324 static void display_finfo(file_info
*finfo
)
326 if (do_this_one(finfo
)) {
327 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
328 DEBUG(0,(" %-30s%7.7s %8.0f %s",
330 attrib_string(finfo
->mode
),
332 asctime(LocalTime(&t
))));
333 dir_total
+= finfo
->size
;
338 /****************************************************************************
339 accumulate size of a file
340 ****************************************************************************/
341 static void do_du(file_info
*finfo
)
343 if (do_this_one(finfo
)) {
344 dir_total
+= finfo
->size
;
348 static BOOL do_list_recurse
;
349 static BOOL do_list_dirs
;
350 static char *do_list_queue
= 0;
351 static long do_list_queue_size
= 0;
352 static long do_list_queue_start
= 0;
353 static long do_list_queue_end
= 0;
354 static void (*do_list_fn
)(file_info
*);
356 /****************************************************************************
357 functions for do_list_queue
358 ****************************************************************************/
361 * The do_list_queue is a NUL-separated list of strings stored in a
362 * char*. Since this is a FIFO, we keep track of the beginning and
363 * ending locations of the data in the queue. When we overflow, we
364 * double the size of the char*. When the start of the data passes
365 * the midpoint, we move everything back. This is logically more
366 * complex than a linked list, but easier from a memory management
367 * angle. In any memory error condition, do_list_queue is reset.
368 * Functions check to ensure that do_list_queue is non-NULL before
371 static void reset_do_list_queue(void)
378 do_list_queue_size
= 0;
379 do_list_queue_start
= 0;
380 do_list_queue_end
= 0;
383 static void init_do_list_queue(void)
385 reset_do_list_queue();
386 do_list_queue_size
= 1024;
387 do_list_queue
= malloc(do_list_queue_size
);
388 if (do_list_queue
== 0) {
389 DEBUG(0,("malloc fail for size %d\n",
390 (int)do_list_queue_size
));
391 reset_do_list_queue();
393 memset(do_list_queue
, 0, do_list_queue_size
);
397 static void adjust_do_list_queue(void)
400 * If the starting point of the queue is more than half way through,
401 * move everything toward the beginning.
403 if (do_list_queue
&& (do_list_queue_start
== do_list_queue_end
))
405 DEBUG(4,("do_list_queue is empty\n"));
406 do_list_queue_start
= do_list_queue_end
= 0;
407 *do_list_queue
= '\0';
409 else if (do_list_queue_start
> (do_list_queue_size
/ 2))
411 DEBUG(4,("sliding do_list_queue backward\n"));
412 memmove(do_list_queue
,
413 do_list_queue
+ do_list_queue_start
,
414 do_list_queue_end
- do_list_queue_start
);
415 do_list_queue_end
-= do_list_queue_start
;
416 do_list_queue_start
= 0;
421 static void add_to_do_list_queue(const char* entry
)
425 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
426 while (new_end
> do_list_queue_size
)
428 do_list_queue_size
*= 2;
429 DEBUG(4,("enlarging do_list_queue to %d\n",
430 (int)do_list_queue_size
));
431 dlq
= Realloc(do_list_queue
, do_list_queue_size
);
433 DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
434 (int)do_list_queue_size
));
435 reset_do_list_queue();
438 memset(do_list_queue
+ do_list_queue_size
/ 2,
439 0, do_list_queue_size
/ 2);
444 pstrcpy(do_list_queue
+ do_list_queue_end
, entry
);
445 do_list_queue_end
= new_end
;
446 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
447 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
451 static char *do_list_queue_head(void)
453 return do_list_queue
+ do_list_queue_start
;
456 static void remove_do_list_queue_head(void)
458 if (do_list_queue_end
> do_list_queue_start
)
460 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
461 adjust_do_list_queue();
462 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
463 (int)do_list_queue_start
, (int)do_list_queue_end
));
467 static int do_list_queue_empty(void)
469 return (! (do_list_queue
&& *do_list_queue
));
472 /****************************************************************************
474 ****************************************************************************/
475 static void do_list_helper(file_info
*f
, const char *mask
, void *state
)
477 if (f
->mode
& aDIR
) {
478 if (do_list_dirs
&& do_this_one(f
)) {
481 if (do_list_recurse
&&
482 !strequal(f
->name
,".") &&
483 !strequal(f
->name
,"..")) {
487 pstrcpy(mask2
, mask
);
488 p
= strrchr(mask2
,'\\');
491 pstrcat(mask2
, f
->name
);
492 pstrcat(mask2
,"\\*");
493 add_to_do_list_queue(mask2
);
498 if (do_this_one(f
)) {
504 /****************************************************************************
505 a wrapper around cli_list that adds recursion
506 ****************************************************************************/
507 void do_list(const char *mask
,uint16 attribute
,void (*fn
)(file_info
*),BOOL rec
, BOOL dirs
)
509 static int in_do_list
= 0;
511 if (in_do_list
&& rec
)
513 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
519 do_list_recurse
= rec
;
525 init_do_list_queue();
526 add_to_do_list_queue(mask
);
528 while (! do_list_queue_empty())
531 * Need to copy head so that it doesn't become
532 * invalid inside the call to cli_list. This
533 * would happen if the list were expanded
535 * Fix from E. Jay Berkenbilt (ejb@ql.org)
538 pstrcpy(head
, do_list_queue_head());
539 cli_list(cli
, head
, attribute
, do_list_helper
, NULL
);
540 remove_do_list_queue_head();
541 if ((! do_list_queue_empty()) && (fn
== display_finfo
))
543 char* next_file
= do_list_queue_head();
545 if ((strlen(next_file
) >= 2) &&
546 (next_file
[strlen(next_file
) - 1] == '*') &&
547 (next_file
[strlen(next_file
) - 2] == '\\'))
549 save_ch
= next_file
+
550 strlen(next_file
) - 2;
553 DEBUG(0,("\n%s\n",next_file
));
563 if (cli_list(cli
, mask
, attribute
, do_list_helper
, NULL
) == -1)
565 DEBUG(0, ("%s listing %s\n", cli_errstr(cli
), mask
));
570 reset_do_list_queue();
573 /****************************************************************************
574 get a directory listing
575 ****************************************************************************/
576 static void cmd_dir(void)
578 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
584 pstrcpy(mask
,cur_dir
);
585 if(mask
[strlen(mask
)-1]!='\\')
588 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
599 do_list(mask
, attribute
, display_finfo
, recurse
, True
);
603 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
607 /****************************************************************************
608 get a directory listing
609 ****************************************************************************/
610 static void cmd_du(void)
612 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
618 pstrcpy(mask
,cur_dir
);
619 if(mask
[strlen(mask
)-1]!='\\')
622 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
632 do_list(mask
, attribute
, do_du
, recurse
, True
);
636 DEBUG(0, ("Total number of bytes: %.0f\n", dir_total
));
640 /****************************************************************************
641 get a file from rname to lname
642 ****************************************************************************/
643 static void do_get(char *rname
,char *lname
)
646 BOOL newhandle
= False
;
648 struct timeval tp_start
;
649 int read_size
= io_bufsize
;
654 GetTimeOfDay(&tp_start
);
660 fnum
= cli_open(cli
, rname
, O_RDONLY
, DENY_NONE
);
663 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli
),rname
));
667 if(!strcmp(lname
,"-")) {
668 handle
= fileno(stdout
);
670 handle
= sys_open(lname
,O_WRONLY
|O_CREAT
|O_TRUNC
,0644);
674 DEBUG(0,("Error opening local file %s\n",lname
));
679 if (!cli_qfileinfo(cli
, fnum
,
680 &attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
681 !cli_getattrE(cli
, fnum
,
682 &attr
, &size
, NULL
, NULL
, NULL
)) {
683 DEBUG(0,("getattrib: %s\n",cli_errstr(cli
)));
687 DEBUG(2,("getting file %s of size %.0f as %s ",
688 lname
, (double)size
, lname
));
690 if(!(data
= (char *)malloc(read_size
))) {
691 DEBUG(0,("malloc fail for size %d\n", read_size
));
692 cli_close(cli
, fnum
);
697 int n
= cli_read(cli
, fnum
, data
, nread
, read_size
);
701 if (writefile(handle
,data
, n
) != n
) {
702 DEBUG(0,("Error writing local file\n"));
710 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
711 rname
, (long)nread
));
716 if (!cli_close(cli
, fnum
)) {
717 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli
)));
724 if (archive_level
>= 2 && (attr
& aARCH
)) {
725 cli_setatr(cli
, rname
, attr
& ~(uint16
)aARCH
, 0);
729 struct timeval tp_end
;
732 GetTimeOfDay(&tp_end
);
734 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
735 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
736 get_total_time_ms
+= this_time
;
737 get_total_size
+= nread
;
739 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
740 nread
/ (1.024*this_time
+ 1.0e-4),
741 get_total_size
/ (1.024*get_total_time_ms
)));
746 /****************************************************************************
748 ****************************************************************************/
749 static void cmd_get(void)
755 pstrcpy(rname
,cur_dir
);
758 p
= rname
+ strlen(rname
);
760 if (!next_token(NULL
,p
,NULL
,sizeof(rname
)-strlen(rname
))) {
761 DEBUG(0,("get <filename>\n"));
765 dos_clean_name(rname
);
767 next_token(NULL
,lname
,NULL
,sizeof(lname
));
769 do_get(rname
, lname
);
773 /****************************************************************************
774 do a mget operation on one file
775 ****************************************************************************/
776 static void do_mget(file_info
*finfo
)
780 pstring saved_curdir
;
783 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
787 DEBUG(0,("mget aborted\n"));
791 if (finfo
->mode
& aDIR
)
792 slprintf(quest
,sizeof(pstring
)-1,
793 "Get directory %s? ",finfo
->name
);
795 slprintf(quest
,sizeof(pstring
)-1,
796 "Get file %s? ",finfo
->name
);
798 if (prompt
&& !yesno(quest
)) return;
800 if (!(finfo
->mode
& aDIR
)) {
801 pstrcpy(rname
,cur_dir
);
802 pstrcat(rname
,finfo
->name
);
803 do_get(rname
,finfo
->name
);
807 /* handle directories */
808 pstrcpy(saved_curdir
,cur_dir
);
810 pstrcat(cur_dir
,finfo
->name
);
811 pstrcat(cur_dir
,"\\");
813 unix_format(finfo
->name
);
815 strlower(finfo
->name
);
817 if (!directory_exist(finfo
->name
,NULL
) &&
818 mkdir(finfo
->name
,0777) != 0) {
819 DEBUG(0,("failed to create directory %s\n",finfo
->name
));
820 pstrcpy(cur_dir
,saved_curdir
);
824 if (chdir(finfo
->name
) != 0) {
825 DEBUG(0,("failed to chdir to directory %s\n",finfo
->name
));
826 pstrcpy(cur_dir
,saved_curdir
);
830 pstrcpy(mget_mask
,cur_dir
);
831 pstrcat(mget_mask
,"*");
833 do_list(mget_mask
, aSYSTEM
| aHIDDEN
| aDIR
,do_mget
,False
, True
);
835 pstrcpy(cur_dir
,saved_curdir
);
839 /****************************************************************************
840 view the file using the pager
841 ****************************************************************************/
842 static void cmd_more(void)
844 fstring rname
,lname
,pager_cmd
;
848 fstrcpy(rname
,cur_dir
);
851 slprintf(lname
,sizeof(lname
)-1, "%s/smbmore.XXXXXX",tmpdir());
852 fd
= smb_mkstemp(lname
);
854 DEBUG(0,("failed to create temporary file for more\n"));
859 if (!next_token(NULL
,rname
+strlen(rname
),NULL
,sizeof(rname
)-strlen(rname
))) {
860 DEBUG(0,("more <filename>\n"));
864 dos_clean_name(rname
);
868 pager
=getenv("PAGER");
870 slprintf(pager_cmd
,sizeof(pager_cmd
)-1,
871 "%s %s",(pager
? pager
:PAGER
), lname
);
878 /****************************************************************************
880 ****************************************************************************/
881 static void cmd_mget(void)
883 uint16 attribute
= aSYSTEM
| aHIDDEN
;
895 while (next_token(NULL
,p
,NULL
,sizeof(buf
))) {
896 pstrcpy(mget_mask
,cur_dir
);
897 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
898 pstrcat(mget_mask
,"\\");
901 pstrcpy(mget_mask
,p
);
903 pstrcat(mget_mask
,p
);
904 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
908 pstrcpy(mget_mask
,cur_dir
);
909 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
910 pstrcat(mget_mask
,"\\");
911 pstrcat(mget_mask
,"*");
912 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
917 /****************************************************************************
918 make a directory of name "name"
919 ****************************************************************************/
920 static BOOL
do_mkdir(char *name
)
922 if (!cli_mkdir(cli
, name
)) {
923 DEBUG(0,("%s making remote directory %s\n",
924 cli_errstr(cli
),name
));
932 /****************************************************************************
934 ****************************************************************************/
935 static void cmd_quit(void)
942 /****************************************************************************
944 ****************************************************************************/
945 static void cmd_mkdir(void)
951 pstrcpy(mask
,cur_dir
);
953 if (!next_token(NULL
,p
,NULL
,sizeof(buf
))) {
955 DEBUG(0,("mkdir <dirname>\n"));
966 trim_string(ddir
,".",NULL
);
967 p
= strtok(ddir
,"/\\");
970 if (!cli_chkpath(cli
, ddir2
)) {
974 p
= strtok(NULL
,"/\\");
982 /****************************************************************************
984 ****************************************************************************/
985 static void do_put(char *rname
,char *lname
)
991 int maxwrite
=io_bufsize
;
993 struct timeval tp_start
;
994 GetTimeOfDay(&tp_start
);
996 fnum
= cli_open(cli
, rname
, O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
);
999 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli
),rname
));
1003 /* allow files to be piped into smbclient
1004 jdblair 24.jun.98 */
1005 if (!strcmp(lname
, "-")) {
1007 /* size of file is not known */
1009 f
= sys_fopen(lname
,"r");
1013 DEBUG(0,("Error opening local file %s\n",lname
));
1018 DEBUG(1,("putting file %s as %s ",lname
,
1021 buf
= (char *)malloc(maxwrite
);
1023 DEBUG(0, ("ERROR: Not enough memory!\n"));
1030 if ((n
= readfile(buf
,1,n
,f
)) < 1) {
1031 if((n
== 0) && feof(f
))
1032 break; /* Empty local file. */
1034 DEBUG(0,("Error reading local file: %s\n", strerror(errno
) ));
1038 ret
= cli_write(cli
, fnum
, 0, buf
, nread
, n
);
1041 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli
)));
1048 if (!cli_close(cli
, fnum
)) {
1049 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli
),rname
));
1060 struct timeval tp_end
;
1063 GetTimeOfDay(&tp_end
);
1065 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
1066 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
1067 put_total_time_ms
+= this_time
;
1068 put_total_size
+= nread
;
1070 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1071 nread
/ (1.024*this_time
+ 1.0e-4),
1072 put_total_size
/ (1.024*put_total_time_ms
)));
1083 /****************************************************************************
1085 ****************************************************************************/
1086 static void cmd_put(void)
1093 pstrcpy(rname
,cur_dir
);
1094 pstrcat(rname
,"\\");
1096 if (!next_token(NULL
,p
,NULL
,sizeof(buf
))) {
1097 DEBUG(0,("put <filename>\n"));
1102 if (next_token(NULL
,p
,NULL
,sizeof(buf
)))
1105 pstrcat(rname
,lname
);
1107 dos_clean_name(rname
);
1111 /* allow '-' to represent stdin
1112 jdblair, 24.jun.98 */
1113 if (!file_exist(lname
,&st
) &&
1114 (strcmp(lname
,"-"))) {
1115 DEBUG(0,("%s does not exist\n",lname
));
1120 do_put(rname
,lname
);
1123 /*************************************
1125 *************************************/
1127 static struct file_list
{
1128 struct file_list
*prev
, *next
;
1133 /****************************************************************************
1134 Free a file_list structure
1135 ****************************************************************************/
1137 static void free_file_list (struct file_list
* list
)
1139 struct file_list
*tmp
;
1144 DLIST_REMOVE(list
, list
);
1145 if (tmp
->file_path
) free(tmp
->file_path
);
1150 /****************************************************************************
1151 seek in a directory/file list until you get something that doesn't start with
1153 ****************************************************************************/
1154 static BOOL
seek_list(struct file_list
*list
, char *name
)
1157 trim_string(list
->file_path
,"./","\n");
1158 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
1167 /****************************************************************************
1168 set the file selection mask
1169 ****************************************************************************/
1170 static void cmd_select(void)
1172 pstrcpy(fileselection
,"");
1173 next_token(NULL
,fileselection
,NULL
,sizeof(fileselection
));
1176 /****************************************************************************
1177 Recursive file matching function act as find
1178 match must be always set to True when calling this function
1179 ****************************************************************************/
1180 static int file_find(struct file_list
**list
, const char *directory
,
1181 const char *expression
, BOOL match
)
1184 struct file_list
*entry
;
1185 struct stat statbuf
;
1191 dir
= opendir(directory
);
1192 if (!dir
) return -1;
1194 while ((dname
= readdirname(dir
))) {
1195 if (!strcmp("..", dname
)) continue;
1196 if (!strcmp(".", dname
)) continue;
1198 if (asprintf(&path
, "%s/%s", directory
, dname
) <= 0) {
1203 if (!match
|| !ms_fnmatch(expression
, dname
)) {
1205 ret
= stat(path
, &statbuf
);
1207 if (S_ISDIR(statbuf
.st_mode
)) {
1209 ret
= file_find(list
, path
, expression
, False
);
1212 DEBUG(0,("file_find: cannot stat file %s\n", path
));
1221 entry
= (struct file_list
*) malloc(sizeof (struct file_list
));
1223 DEBUG(0,("Out of memory in file_find\n"));
1227 entry
->file_path
= path
;
1228 entry
->isdir
= isdir
;
1229 DLIST_ADD(*list
, entry
);
1239 /****************************************************************************
1241 ****************************************************************************/
1242 static void cmd_mput(void)
1247 while (next_token(NULL
,p
,NULL
,sizeof(buf
))) {
1249 struct file_list
*temp_list
;
1250 char *quest
, *lname
, *rname
;
1254 ret
= file_find(&file_list
, ".", p
, True
);
1256 free_file_list(file_list
);
1264 for (temp_list
= file_list
; temp_list
;
1265 temp_list
= temp_list
->next
) {
1267 if (lname
) free(lname
);
1268 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0)
1270 trim_string(lname
, "./", "/");
1272 /* check if it's a directory */
1273 if (temp_list
->isdir
) {
1274 /* if (!recurse) continue; */
1276 if (quest
) free(quest
);
1277 asprintf(&quest
, "Put directory %s? ", lname
);
1278 if (prompt
&& !yesno(quest
)) { /* No */
1279 /* Skip the directory */
1280 lname
[strlen(lname
)-1] = '/';
1281 if (!seek_list(temp_list
, lname
))
1284 if (rname
) free(rname
);
1285 asprintf(&rname
, "%s%s", cur_dir
, lname
);
1287 if (!cli_chkpath(cli
, rname
) &&
1289 DEBUG (0, ("Unable to make dir, skipping..."));
1290 /* Skip the directory */
1291 lname
[strlen(lname
)-1] = '/';
1292 if (!seek_list(temp_list
, lname
))
1298 if (quest
) free(quest
);
1299 asprintf(&quest
,"Put file %s? ", lname
);
1300 if (prompt
&& !yesno(quest
)) /* No */
1304 if (rname
) free(rname
);
1305 asprintf(&rname
, "%s%s", cur_dir
, lname
);
1310 do_put(rname
, lname
);
1312 free_file_list(file_list
);
1313 if (quest
) free(quest
);
1314 if (lname
) free(lname
);
1315 if (rname
) free(rname
);
1320 /****************************************************************************
1322 ****************************************************************************/
1323 static void do_cancel(int job
)
1325 if (cli_printjob_del(cli
, job
)) {
1326 printf("Job %d cancelled\n",job
);
1328 printf("Error cancelling job %d : %s\n",job
,cli_errstr(cli
));
1333 /****************************************************************************
1335 ****************************************************************************/
1336 static void cmd_cancel(void)
1341 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1342 printf("cancel <jobid> ...\n");
1348 } while (next_token(NULL
,buf
,NULL
,sizeof(buf
)));
1352 /****************************************************************************
1354 ****************************************************************************/
1355 static void cmd_print(void)
1361 if (!next_token(NULL
,lname
,NULL
, sizeof(lname
))) {
1362 DEBUG(0,("print <filename>\n"));
1366 pstrcpy(rname
,lname
);
1367 p
= strrchr(rname
,'/');
1369 slprintf(rname
, sizeof(rname
)-1, "%s-%d", p
+1, (int)sys_getpid());
1372 if (strequal(lname
,"-")) {
1373 slprintf(rname
, sizeof(rname
)-1, "stdin-%d", (int)sys_getpid());
1376 do_put(rname
, lname
);
1380 /****************************************************************************
1381 show a print queue entry
1382 ****************************************************************************/
1383 static void queue_fn(struct print_job_info
*p
)
1385 DEBUG(0,("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
));
1388 /****************************************************************************
1390 ****************************************************************************/
1391 static void cmd_queue(void)
1393 cli_print_queue(cli
, queue_fn
);
1396 /****************************************************************************
1398 ****************************************************************************/
1399 static void do_del(file_info
*finfo
)
1403 pstrcpy(mask
,cur_dir
);
1404 pstrcat(mask
,finfo
->name
);
1406 if (finfo
->mode
& aDIR
)
1409 if (!cli_unlink(cli
, mask
)) {
1410 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli
),mask
));
1414 /****************************************************************************
1416 ****************************************************************************/
1417 static void cmd_del(void)
1421 uint16 attribute
= aSYSTEM
| aHIDDEN
;
1426 pstrcpy(mask
,cur_dir
);
1428 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1429 DEBUG(0,("del <filename>\n"));
1434 do_list(mask
, attribute
,do_del
,False
,False
);
1437 /****************************************************************************
1438 ****************************************************************************/
1439 static void cmd_open(void)
1444 pstrcpy(mask
,cur_dir
);
1446 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1447 DEBUG(0,("del <filename>\n"));
1452 cli_open(cli
, mask
, O_RDWR
, DENY_ALL
);
1456 /****************************************************************************
1458 ****************************************************************************/
1459 static void cmd_rmdir(void)
1464 pstrcpy(mask
,cur_dir
);
1466 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1467 DEBUG(0,("rmdir <dirname>\n"));
1472 if (!cli_rmdir(cli
, mask
)) {
1473 DEBUG(0,("%s removing remote directory file %s\n",
1474 cli_errstr(cli
),mask
));
1478 /****************************************************************************
1480 ****************************************************************************/
1481 static void cmd_rename(void)
1486 pstrcpy(src
,cur_dir
);
1487 pstrcpy(dest
,cur_dir
);
1489 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1490 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1491 DEBUG(0,("rename <src> <dest>\n"));
1498 if (!cli_rename(cli
, src
, dest
)) {
1499 DEBUG(0,("%s renaming files\n",cli_errstr(cli
)));
1505 /****************************************************************************
1506 toggle the prompt flag
1507 ****************************************************************************/
1508 static void cmd_prompt(void)
1511 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
1515 /****************************************************************************
1516 set the newer than time
1517 ****************************************************************************/
1518 static void cmd_newer(void)
1522 SMB_STRUCT_STAT sbuf
;
1524 ok
= next_token(NULL
,buf
,NULL
,sizeof(buf
));
1525 if (ok
&& (sys_stat(buf
,&sbuf
) == 0)) {
1526 newer_than
= sbuf
.st_mtime
;
1527 DEBUG(1,("Getting files newer than %s",
1528 asctime(LocalTime(&newer_than
))));
1533 if (ok
&& newer_than
== 0)
1534 DEBUG(0,("Error setting newer-than time\n"));
1537 /****************************************************************************
1538 set the archive level
1539 ****************************************************************************/
1540 static void cmd_archive(void)
1544 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1545 archive_level
= atoi(buf
);
1547 DEBUG(0,("Archive level is %d\n",archive_level
));
1550 /****************************************************************************
1551 toggle the lowercaseflag
1552 ****************************************************************************/
1553 static void cmd_lowercase(void)
1555 lowercase
= !lowercase
;
1556 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
1562 /****************************************************************************
1563 toggle the recurse flag
1564 ****************************************************************************/
1565 static void cmd_recurse(void)
1568 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
1571 /****************************************************************************
1572 toggle the translate flag
1573 ****************************************************************************/
1574 static void cmd_translate(void)
1576 translation
= !translation
;
1577 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1578 translation
?"on":"off"));
1582 /****************************************************************************
1583 do a printmode command
1584 ****************************************************************************/
1585 static void cmd_printmode(void)
1590 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1591 if (strequal(buf
,"text")) {
1594 if (strequal(buf
,"graphics"))
1597 printmode
= atoi(buf
);
1604 fstrcpy(mode
,"text");
1607 fstrcpy(mode
,"graphics");
1610 slprintf(mode
,sizeof(mode
)-1,"%d",printmode
);
1614 DEBUG(2,("the printmode is now %s\n",mode
));
1617 /****************************************************************************
1619 ****************************************************************************/
1620 static void cmd_lcd(void)
1625 if (next_token(NULL
,buf
,NULL
,sizeof(buf
)))
1627 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
1630 /****************************************************************************
1632 ****************************************************************************/
1633 static void browse_fn(const char *name
, uint32 m
,
1634 const char *comment
, void *state
)
1642 case STYPE_DISKTREE
:
1643 fstrcpy(typestr
,"Disk"); break;
1645 fstrcpy(typestr
,"Printer"); break;
1647 fstrcpy(typestr
,"Device"); break;
1649 fstrcpy(typestr
,"IPC"); break;
1652 printf("\t%-15.15s%-10.10s%s\n",
1653 name
, typestr
,comment
);
1657 /****************************************************************************
1658 try and browse available connections on a host
1659 ****************************************************************************/
1660 static BOOL
browse_host(BOOL sort
)
1664 printf("\n\tSharename Type Comment\n");
1665 printf("\t--------- ---- -------\n");
1667 if((ret
= cli_RNetShareEnum(cli
, browse_fn
, NULL
)) == -1)
1668 printf("Error returning browse list: %s\n", cli_errstr(cli
));
1673 /****************************************************************************
1675 ****************************************************************************/
1676 static void server_fn(const char *name
, uint32 m
,
1677 const char *comment
, void *state
)
1679 printf("\t%-16.16s %s\n", name
, comment
);
1682 /****************************************************************************
1683 try and browse available connections on a host
1684 ****************************************************************************/
1685 static BOOL
list_servers(char *wk_grp
)
1687 if (!cli
->server_domain
) return False
;
1689 printf("\n\tServer Comment\n");
1690 printf("\t--------- -------\n");
1692 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
, NULL
);
1694 printf("\n\tWorkgroup Master\n");
1695 printf("\t--------- -------\n");
1697 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
, server_fn
, NULL
);
1701 /* Some constants for completing filename arguments */
1703 #define COMPL_NONE 0 /* No completions */
1704 #define COMPL_REMOTE 1 /* Complete remote filename */
1705 #define COMPL_LOCAL 2 /* Complete local filename */
1707 /* This defines the commands supported by this client */
1713 char compl_args
[2]; /* Completion argument info */
1716 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1717 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1718 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1719 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
1720 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
1721 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
1722 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
1723 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1724 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
1725 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1726 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
1727 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
1728 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
1729 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1730 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
1731 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1732 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
1733 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
1734 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
1735 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
1736 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
1737 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
1738 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
1739 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
1740 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
1741 {"printmode",cmd_printmode
,"<graphics or text> set the print mode",{COMPL_NONE
,COMPL_NONE
}},
1742 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
1743 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
1744 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1745 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1746 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1747 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
1748 {"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
}},
1749 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
1750 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
1751 {"tarmode",cmd_tarmode
,
1752 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
1753 {"setmode",cmd_setmode
,"filename <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
1754 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
1755 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
1756 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
1757 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
1758 {"",NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
1762 /*******************************************************************
1763 lookup a command string in the list of commands, including
1765 ******************************************************************/
1766 static int process_tok(fstring tok
)
1768 int i
= 0, matches
= 0;
1770 int tok_len
= strlen(tok
);
1772 while (commands
[i
].fn
!= NULL
) {
1773 if (strequal(commands
[i
].name
,tok
)) {
1777 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
1786 else if (matches
== 1)
1792 /****************************************************************************
1794 ****************************************************************************/
1795 static void cmd_help(void)
1800 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1801 if ((i
= process_tok(buf
)) >= 0)
1802 DEBUG(0,("HELP %s:\n\t%s\n\n",commands
[i
].name
,commands
[i
].description
));
1804 while (commands
[i
].description
) {
1805 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
1806 DEBUG(0,("%-15s",commands
[i
].name
));
1814 /****************************************************************************
1815 process a -c command string
1816 ****************************************************************************/
1817 static void process_command_string(char *cmd
)
1822 while (cmd
[0] != '\0') {
1827 if ((p
= strchr(cmd
, ';')) == 0) {
1828 strncpy(line
, cmd
, 999);
1832 if (p
- cmd
> 999) p
= cmd
+ 999;
1833 strncpy(line
, cmd
, p
- cmd
);
1834 line
[p
- cmd
] = '\0';
1838 /* and get the first part of the command */
1840 if (!next_token(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
1842 if ((i
= process_tok(tok
)) >= 0) {
1844 } else if (i
== -2) {
1845 DEBUG(0,("%s: command abbreviation ambiguous\n",tok
));
1847 DEBUG(0,("%s: command not found\n",tok
));
1852 /****************************************************************************
1853 handle completion of commands for readline
1854 ****************************************************************************/
1855 static char **completion_fn(const char *text
, int start
, int end
)
1857 #define MAX_COMPLETIONS 100
1861 /* for words not at the start of the line fallback to filename completion */
1862 if (start
) return NULL
;
1864 matches
= (char **)malloc(sizeof(matches
[0])*MAX_COMPLETIONS
);
1865 if (!matches
) return NULL
;
1867 matches
[count
++] = strdup(text
);
1868 if (!matches
[0]) return NULL
;
1870 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
1871 if (strncmp(text
, commands
[i
].name
, strlen(text
)) == 0) {
1872 matches
[count
] = strdup(commands
[i
].name
);
1873 if (!matches
[count
]) return NULL
;
1880 matches
[0] = strdup(matches
[1]);
1882 matches
[count
] = NULL
;
1887 /****************************************************************************
1888 make sure we swallow keepalives during idle time
1889 ****************************************************************************/
1890 static void readline_callback(void)
1893 struct timeval timeout
;
1894 static time_t last_t
;
1899 if (t
- last_t
< 5) return;
1905 FD_SET(cli
->fd
,&fds
);
1908 timeout
.tv_usec
= 0;
1909 sys_select_intr(cli
->fd
+1,&fds
,&timeout
);
1911 /* We deliberately use receive_smb instead of
1912 client_receive_smb as we want to receive
1913 session keepalives and then drop them here.
1915 if (FD_ISSET(cli
->fd
,&fds
)) {
1916 receive_smb(cli
->fd
,cli
->inbuf
,0);
1920 cli_chkpath(cli
, "\\");
1924 /****************************************************************************
1925 process commands on stdin
1926 ****************************************************************************/
1927 static void process_stdin(void)
1937 /* display a prompt */
1938 slprintf(the_prompt
, sizeof(the_prompt
)-1, "smb: %s> ", cur_dir
);
1939 line
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
1943 /* special case - first char is ! */
1949 /* and get the first part of the command */
1951 if (!next_token(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
1953 if ((i
= process_tok(tok
)) >= 0) {
1955 } else if (i
== -2) {
1956 DEBUG(0,("%s: command abbreviation ambiguous\n",tok
));
1958 DEBUG(0,("%s: command not found\n",tok
));
1964 /*****************************************************
1965 return a connection to a server
1966 *******************************************************/
1967 struct cli_state
*do_connect(char *server
, char *share
)
1969 struct cli_state
*c
;
1970 struct nmb_name called
, calling
;
1973 extern struct in_addr ipzero
;
1974 fstring servicename
;
1977 /* make a copy so we don't modify the global string 'service' */
1978 safe_strcpy(servicename
, share
, sizeof(servicename
)-1);
1979 sharename
= servicename
;
1980 if (*sharename
== '\\') {
1981 server
= sharename
+2;
1982 sharename
= strchr(server
,'\\');
1983 if (!sharename
) return NULL
;
1992 make_nmb_name(&calling
, global_myname
, 0x0);
1993 make_nmb_name(&called
, server
, name_type
);
1997 if (have_ip
) ip
= dest_ip
;
1999 /* have to open a new connection */
2000 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, port
) == 0) ||
2001 !cli_connect(c
, server_n
, &ip
)) {
2002 DEBUG(0,("Connection to %s failed\n", server_n
));
2006 c
->protocol
= max_protocol
;
2008 if (!cli_session_request(c
, &calling
, &called
)) {
2010 DEBUG(0,("session request to %s failed (%s)\n",
2011 called
.name
, cli_errstr(c
)));
2013 if ((p
=strchr(called
.name
, '.'))) {
2017 if (strcmp(called
.name
, "*SMBSERVER")) {
2018 make_nmb_name(&called
, "*SMBSERVER", 0x20);
2024 DEBUG(4,(" session request ok\n"));
2026 if (!cli_negprot(c
)) {
2027 DEBUG(0,("protocol negotiation failed\n"));
2033 char *pass
= getpass("Password: ");
2035 pstrcpy(password
, pass
);
2039 if (!cli_session_setup(c
, username
,
2040 password
, strlen(password
),
2041 password
, strlen(password
),
2043 /* if a password was not supplied then try again with a null username */
2044 if (password
[0] || !username
[0] ||
2045 !cli_session_setup(c
, "", "", 0, "", 0, workgroup
)) {
2046 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
2050 DEBUG(0,("Anonymous login successful\n"));
2054 * These next two lines are needed to emulate
2055 * old client behaviour for people who have
2056 * scripts based on client output.
2057 * QUESTION ? Do we want to have a 'client compatibility
2058 * mode to turn these on/off ? JRA.
2061 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
2062 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
2063 c
->server_domain
,c
->server_os
,c
->server_type
));
2065 DEBUG(4,(" session setup ok\n"));
2067 if (!cli_send_tconX(c
, sharename
, "?????",
2068 password
, strlen(password
)+1)) {
2069 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
2074 DEBUG(4,(" tconx ok\n"));
2080 /****************************************************************************
2081 process commands from the client
2082 ****************************************************************************/
2083 static BOOL
process(char *base_directory
)
2085 cli
= do_connect(desthost
, service
);
2090 if (*base_directory
) do_cd(base_directory
);
2093 process_command_string(cmdstr
);
2102 /****************************************************************************
2103 usage on the program
2104 ****************************************************************************/
2105 static void usage(char *pname
)
2107 DEBUG(0,("Usage: %s service <password> [options]", pname
));
2109 DEBUG(0,("\nVersion %s\n",VERSION
));
2110 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
2111 DEBUG(0,("\t-O socket_options socket options to use\n"));
2112 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
2113 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
2114 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
2115 DEBUG(0,("\t-N don't ask for a password\n"));
2116 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
2117 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
2118 DEBUG(0,("\t-P connect to service as a printer\n"));
2119 DEBUG(0,("\t-p port connect to the specified port\n"));
2120 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
2121 DEBUG(0,("\t-h Print this help message.\n"));
2122 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
2123 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
2124 DEBUG(0,("\t-U username set the network username\n"));
2125 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
2126 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
2127 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
2128 DEBUG(0,("\t-A filename get the credentials from a file\n"));
2129 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
2130 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
2131 DEBUG(0,("\t-D directory start from directory\n"));
2132 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
2133 DEBUG(0,("\t-b xmit/send buffer changes the transmit/send buffer (default: 65520)\n"));
2138 /****************************************************************************
2139 get a password from a a file or file descriptor
2141 ****************************************************************************/
2142 static void get_password_file(void)
2146 BOOL close_it
= False
;
2150 if ((p
= getenv("PASSWD_FD")) != NULL
) {
2151 pstrcpy(spec
, "descriptor ");
2153 sscanf(p
, "%d", &fd
);
2155 } else if ((p
= getenv("PASSWD_FILE")) != NULL
) {
2156 fd
= sys_open(p
, O_RDONLY
, 0);
2159 fprintf(stderr
, "Error opening PASSWD_FILE %s: %s\n",
2160 spec
, strerror(errno
));
2166 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
2167 p
&& p
- pass
< sizeof(pass
);) {
2168 switch (read(fd
, p
, 1)) {
2170 if (*p
!= '\n' && *p
!= '\0') {
2171 *++p
= '\0'; /* advance p, and null-terminate pass */
2176 *p
= '\0'; /* null-terminate it, just in case... */
2177 p
= NULL
; /* then force the loop condition to become false */
2180 fprintf(stderr
, "Error reading password from file %s: %s\n",
2181 spec
, "empty password\n");
2186 fprintf(stderr
, "Error reading password from file %s: %s\n",
2187 spec
, strerror(errno
));
2191 pstrcpy(password
, pass
);
2198 /****************************************************************************
2200 ****************************************************************************/
2201 static int do_host_query(char *query_host
)
2203 cli
= do_connect(query_host
, "IPC$");
2208 list_servers(workgroup
);
2216 /****************************************************************************
2217 handle a tar operation
2218 ****************************************************************************/
2219 static int do_tar_op(char *base_directory
)
2222 cli
= do_connect(desthost
, service
);
2228 if (*base_directory
) do_cd(base_directory
);
2237 /****************************************************************************
2238 handle a message operation
2239 ****************************************************************************/
2240 static int do_message_op(void)
2243 struct nmb_name called
, calling
;
2247 make_nmb_name(&calling
, global_myname
, 0x0);
2248 make_nmb_name(&called
, desthost
, name_type
);
2251 if (have_ip
) ip
= dest_ip
;
2253 if (!(cli
=cli_initialise(NULL
)) || (cli_set_port(cli
, port
) == 0) || !cli_connect(cli
, desthost
, &ip
)) {
2254 DEBUG(0,("Connection to %s failed\n", desthost
));
2258 if (!cli_session_request(cli
, &calling
, &called
)) {
2259 DEBUG(0,("session request failed\n"));
2271 /****************************************************************************
2273 ****************************************************************************/
2274 int main(int argc
,char *argv
[])
2276 fstring base_directory
;
2277 char *pname
= argv
[0];
2280 extern char *optarg
;
2283 BOOL message
= False
;
2284 extern char tar_type
;
2285 static pstring servicesf
= CONFIGFILE
;
2287 pstring new_name_resolve_order
;
2292 pstrcpy(term_code
, KANJI
);
2298 *base_directory
= 0;
2300 *new_name_resolve_order
= 0;
2304 setup_logging(pname
,True
);
2307 * If the -E option is given, be careful not to clobber stdout
2308 * before processing the options. 28.Feb.99, richard@hacom.nl.
2309 * Also pre-parse the -s option to get the service file name.
2312 for (opt
= 1; opt
< argc
; opt
++) {
2313 if (strcmp(argv
[opt
], "-E") == 0)
2315 else if(strncmp(argv
[opt
], "-s", 2) == 0) {
2316 if(argv
[opt
][2] != '\0')
2317 pstrcpy(servicesf
, &argv
[opt
][2]);
2318 else if(argv
[opt
+1] != NULL
) {
2320 * At least one more arg left.
2322 pstrcpy(servicesf
, argv
[opt
+1]);
2328 else if(strncmp(argv
[opt
], "-d", 2) == 0) {
2329 AllowDebugChange
= False
;
2330 if(argv
[opt
][2] != '\0') {
2331 if (argv
[opt
][2] == 'A')
2334 DEBUGLEVEL
= atoi(&argv
[opt
][2]);
2335 } else if(argv
[opt
+1] != NULL
) {
2337 * At least one more arg left.
2339 if (argv
[opt
+1][0] == 'A')
2342 DEBUGLEVEL
= atoi(argv
[opt
+1]);
2351 charset_initialise();
2353 in_client
= True
; /* Make sure that we tell lp_load we are */
2355 if (!lp_load(servicesf
,True
,False
,False
)) {
2356 fprintf(stderr
, "Can't load %s - run testparm to debug it\n", servicesf
);
2359 codepage_initialise(lp_client_code_page());
2365 pstrcpy(workgroup
,lp_workgroup());
2371 if (getenv("USER")) {
2372 pstrcpy(username
,getenv("USER"));
2374 /* modification to support userid%passwd syntax in the USER var
2375 25.Aug.97, jdblair@uab.edu */
2377 if ((p
=strchr(username
,'%'))) {
2379 pstrcpy(password
,p
+1);
2381 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password
));
2386 /* modification to support PASSWD environmental var
2387 25.Aug.97, jdblair@uab.edu */
2388 if (getenv("PASSWD")) {
2389 pstrcpy(password
,getenv("PASSWD"));
2393 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2394 get_password_file();
2398 if (*username
== 0 && getenv("LOGNAME")) {
2399 pstrcpy(username
,getenv("LOGNAME"));
2403 if (*username
== 0) {
2404 pstrcpy(username
,"GUEST");
2412 if (*argv
[1] != '-') {
2413 pstrcpy(service
,argv
[1]);
2414 /* Convert any '/' characters in the service name to '\' characters */
2415 string_replace( service
, '/','\\');
2419 if (count_chars(service
,'\\') < 3) {
2421 printf("\n%s: Not enough '\\' characters in service\n",service
);
2425 if (argc
> 1 && (*argv
[1] != '-')) {
2427 pstrcpy(password
,argv
[1]);
2428 memset(argv
[1],'X',strlen(argv
[1]));
2435 getopt(argc
, argv
,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:")) != EOF
) {
2438 pstrcpy(servicesf
, optarg
);
2441 pstrcpy(user_socket_options
,optarg
);
2444 pstrcpy(new_name_resolve_order
, optarg
);
2447 name_type
= 0x03; /* messages are sent to NetBIOS name type 0x3 */
2448 pstrcpy(desthost
,optarg
);
2453 extern pstring global_scope
;
2454 pstrcpy(global_scope
,optarg
);
2455 strupper(global_scope
);
2462 pstrcpy(global_myname
,optarg
);
2468 DEBUGLEVEL
= atoi(optarg
);
2469 AllowDebugChange
= False
;
2472 /* not needed anymore */
2475 port
= atoi(optarg
);
2478 slprintf(logfile
,sizeof(logfile
)-1, "%s.client",optarg
);
2479 lp_set_logfile(logfile
);
2487 dest_ip
= *interpret_addr2(optarg
);
2488 if (zero_ip(dest_ip
))
2499 pstrcpy(username
,optarg
);
2500 if ((lp
=strchr(username
,'%'))) {
2502 pstrcpy(password
,lp
+1);
2504 memset(strchr(optarg
,'%')+1,'X',strlen(password
));
2514 char *ptr
, *val
, *param
;
2516 if ((auth
=sys_fopen(optarg
, "r")) == NULL
)
2518 /* fail if we can't open the credentials file */
2519 DEBUG(0,("ERROR: Unable to open credentials file!\n"));
2525 /* get a line from the file */
2526 if (!fgets (buf
, sizeof(buf
), auth
))
2530 if ((len
) && (buf
[len
-1]=='\n'))
2538 /* break up the line into parameter & value.
2539 will need to eat a little whitespace possibly */
2541 if (!(ptr
= strchr (buf
, '=')))
2546 /* eat leading white space */
2547 while ((*val
!='\0') && ((*val
==' ') || (*val
=='\t')))
2550 if (strwicmp("password", param
) == 0)
2552 pstrcpy(password
, val
);
2555 else if (strwicmp("username", param
) == 0)
2556 pstrcpy(username
, val
);
2557 else if (strwicmp("domain", param
) == 0)
2558 pstrcpy(workgroup
,val
);
2559 memset(buf
, 0, sizeof(buf
));
2567 while(*p
== '\\' || *p
== '/')
2569 pstrcpy(query_host
,p
);
2572 pstrcpy(term_code
, optarg
);
2575 max_protocol
= interpret_protocol(optarg
, max_protocol
);
2578 pstrcpy(workgroup
,optarg
);
2581 if (!tar_parseargs(argc
, argv
, optarg
, optind
)) {
2587 pstrcpy(base_directory
,optarg
);
2593 io_bufsize
= MAX(1, atoi(optarg
));
2601 get_myname((*global_myname
)?NULL
:global_myname
);
2603 if(*new_name_resolve_order
)
2604 lp_set_name_resolve_order(new_name_resolve_order
);
2607 interpret_coding_system(term_code
);
2609 if (!tar_type
&& !*query_host
&& !*service
&& !message
) {
2614 DEBUG( 3, ( "Client started (version %s).\n", VERSION
) );
2618 process_command_string(cmdstr
);
2619 return do_tar_op(base_directory
);
2622 if ((p
=strchr(query_host
,'#'))) {
2625 sscanf(p
, "%x", &name_type
);
2629 return do_host_query(query_host
);
2633 return do_message_op();
2636 if (!process(base_directory
)) {