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 static int port
= SMB_PORT
;
33 pstring cur_dir
= "\\";
35 static pstring service
;
36 static pstring desthost
;
37 extern pstring global_myname
;
38 extern pstring myhostname
;
39 static pstring password
;
40 static pstring username
;
41 static pstring workgroup
;
44 static int io_bufsize
= 65520;
45 extern struct in_addr ipzero
;
48 static int name_type
= 0x20;
50 extern pstring user_socket_options
;
52 static int process_tok(fstring tok
);
53 static void cmd_help(void);
55 /* 30 second timeout on most commands */
56 #define CLIENT_TIMEOUT (30*1000)
57 #define SHORT_TIMEOUT (5*1000)
59 /* value for unused fid field in trans2 secondary request */
60 #define FID_UNUSED (0xFFFF)
62 time_t newer_than
= 0;
63 int archive_level
= 0;
65 extern pstring debugf
;
66 extern int DEBUGLEVEL
;
68 BOOL translation
= False
;
72 /* clitar bits insert */
75 extern BOOL tar_reset
;
79 mode_t myumask
= 0755;
85 static BOOL recurse
= False
;
86 BOOL lowercase
= False
;
88 struct in_addr dest_ip
;
90 #define SEPARATORS " \t\n\r"
92 BOOL abort_mget
= True
;
94 pstring fileselection
= "";
96 extern file_info def_finfo
;
99 int get_total_size
= 0;
100 int get_total_time_ms
= 0;
101 int put_total_size
= 0;
102 int put_total_time_ms
= 0;
105 static double dir_total
;
109 #define CNV_LANG(s) dos_to_unix(s,False)
110 #define CNV_INPUT(s) unix_to_dos(s,True)
113 /****************************************************************************
114 write to a local file with CR/LF->LF translation if appropriate. return the
115 number taken from the buffer. This may not equal the number written.
116 ****************************************************************************/
117 static int writefile(int f
, char *b
, int n
)
127 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
130 if (write(f
, b
, 1) != 1) {
140 /****************************************************************************
141 read from a file with LF->CR/LF translation if appropriate. return the
142 number read. read approx n bytes.
143 ****************************************************************************/
144 static int readfile(char *b
, int size
, int n
, FILE *f
)
149 if (!translation
|| (size
!= 1))
150 return(fread(b
,size
,n
,f
));
153 while (i
< (n
- 1)) {
154 if ((c
= getc(f
)) == EOF
) {
158 if (c
== '\n') { /* change all LFs to CR/LF */
169 /****************************************************************************
171 ****************************************************************************/
172 static void send_message(void)
177 if (!cli_message_start(cli
, desthost
, username
, &grp_id
)) {
178 DEBUG(0,("message start: %s\n", cli_errstr(cli
)));
183 printf("Connected. Type your message, ending it with a Control-D\n");
185 while (!feof(stdin
) && total_len
< 1600) {
186 int maxlen
= MIN(1600 - total_len
,127);
193 for (l
=0;l
<maxlen
&& (c
=fgetc(stdin
))!=EOF
;l
++) {
199 if (!cli_message_text(cli
, msg
, l
, grp_id
)) {
200 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli
));
207 if (total_len
>= 1600)
208 printf("the message was truncated to 1600 bytes\n");
210 printf("sent %d bytes\n",total_len
);
212 if (!cli_message_end(cli
, grp_id
)) {
213 printf("SMBsendend failed (%s)\n",cli_errstr(cli
));
220 /****************************************************************************
221 check the space on a device
222 ****************************************************************************/
223 static void do_dskattr(void)
225 int total
, bsize
, avail
;
227 if (!cli_dskattr(cli
, &bsize
, &total
, &avail
)) {
228 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli
)));
232 DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
233 total
, bsize
, avail
));
236 /****************************************************************************
238 ****************************************************************************/
239 static void cmd_pwd(void)
241 DEBUG(0,("Current directory is %s",CNV_LANG(service
)));
242 DEBUG(0,("%s\n",CNV_LANG(cur_dir
)));
246 /****************************************************************************
247 change directory - inner section
248 ****************************************************************************/
249 static void do_cd(char *newdir
)
257 /* Save the current directory in case the
258 new directory is invalid */
259 pstrcpy(saved_dir
, cur_dir
);
264 if (*(cur_dir
+strlen(cur_dir
)-1) != '\\') {
265 pstrcat(cur_dir
, "\\");
267 dos_clean_name(cur_dir
);
268 pstrcpy(dname
,cur_dir
);
269 pstrcat(cur_dir
,"\\");
270 dos_clean_name(cur_dir
);
272 if (!strequal(cur_dir
,"\\")) {
273 if (!cli_chkpath(cli
, dname
)) {
274 DEBUG(0,("cd %s: %s\n", dname
, cli_errstr(cli
)));
275 pstrcpy(cur_dir
,saved_dir
);
279 pstrcpy(cd_path
,cur_dir
);
282 /****************************************************************************
284 ****************************************************************************/
285 static void cmd_cd(void)
289 if (next_token(NULL
,buf
,NULL
,sizeof(buf
)))
292 DEBUG(0,("Current directory is %s\n",CNV_LANG(cur_dir
)));
296 /*******************************************************************
297 decide if a file should be operated on
298 ********************************************************************/
299 static BOOL
do_this_one(file_info
*finfo
)
301 if (finfo
->mode
& aDIR
) return(True
);
303 if (*fileselection
&&
304 !mask_match(finfo
->name
,fileselection
,False
,False
)) {
305 DEBUG(3,("match_match %s failed\n", finfo
->name
));
309 if (newer_than
&& finfo
->mtime
< newer_than
) {
310 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
314 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& aARCH
)) {
315 DEBUG(3,("archive %s failed\n", finfo
->name
));
322 /****************************************************************************
323 display info about a file
324 ****************************************************************************/
325 static void display_finfo(file_info
*finfo
)
327 if (do_this_one(finfo
)) {
328 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
329 DEBUG(0,(" %-30s%7.7s %8.0f %s",
330 CNV_LANG(finfo
->name
),
331 attrib_string(finfo
->mode
),
333 asctime(LocalTime(&t
))));
334 dir_total
+= finfo
->size
;
339 /****************************************************************************
340 accumulate size of a file
341 ****************************************************************************/
342 static void do_du(file_info
*finfo
)
344 if (do_this_one(finfo
)) {
345 dir_total
+= finfo
->size
;
349 static BOOL do_list_recurse
;
350 static BOOL do_list_dirs
;
351 static char *do_list_queue
= 0;
352 static long do_list_queue_size
= 0;
353 static long do_list_queue_start
= 0;
354 static long do_list_queue_end
= 0;
355 static void (*do_list_fn
)(file_info
*);
357 /****************************************************************************
358 functions for do_list_queue
359 ****************************************************************************/
362 * The do_list_queue is a NUL-separated list of strings stored in a
363 * char*. Since this is a FIFO, we keep track of the beginning and
364 * ending locations of the data in the queue. When we overflow, we
365 * double the size of the char*. When the start of the data passes
366 * the midpoint, we move everything back. This is logically more
367 * complex than a linked list, but easier from a memory management
368 * angle. In any memory error condition, do_list_queue is reset.
369 * Functions check to ensure that do_list_queue is non-NULL before
372 static void reset_do_list_queue(void)
379 do_list_queue_size
= 0;
380 do_list_queue_start
= 0;
381 do_list_queue_end
= 0;
384 static void init_do_list_queue(void)
386 reset_do_list_queue();
387 do_list_queue_size
= 1024;
388 do_list_queue
= malloc(do_list_queue_size
);
389 if (do_list_queue
== 0) {
390 DEBUG(0,("malloc fail for size %d\n",
391 (int)do_list_queue_size
));
392 reset_do_list_queue();
394 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
)
423 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
424 while (new_end
> do_list_queue_size
)
426 do_list_queue_size
*= 2;
427 DEBUG(4,("enlarging do_list_queue to %d\n",
428 (int)do_list_queue_size
));
429 do_list_queue
= Realloc(do_list_queue
, do_list_queue_size
);
430 if (! do_list_queue
) {
431 DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
432 (int)do_list_queue_size
));
433 reset_do_list_queue();
437 memset(do_list_queue
+ do_list_queue_size
/ 2,
438 0, do_list_queue_size
/ 2);
443 pstrcpy(do_list_queue
+ do_list_queue_end
, entry
);
444 do_list_queue_end
= new_end
;
445 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
446 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
450 static char *do_list_queue_head(void)
452 return do_list_queue
+ do_list_queue_start
;
455 static void remove_do_list_queue_head(void)
457 if (do_list_queue_end
> do_list_queue_start
)
459 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
460 adjust_do_list_queue();
461 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
462 (int)do_list_queue_start
, (int)do_list_queue_end
));
466 static int do_list_queue_empty(void)
468 return (! (do_list_queue
&& *do_list_queue
));
471 /****************************************************************************
473 ****************************************************************************/
474 static void do_list_helper(file_info
*f
, const char *mask
)
476 if (f
->mode
& aDIR
) {
477 if (do_list_dirs
&& do_this_one(f
)) {
480 if (do_list_recurse
&&
481 !strequal(f
->name
,".") &&
482 !strequal(f
->name
,"..")) {
486 pstrcpy(mask2
, mask
);
487 p
= strrchr(mask2
,'\\');
490 pstrcat(mask2
, f
->name
);
491 pstrcat(mask2
,"\\*");
492 add_to_do_list_queue(mask2
);
497 if (do_this_one(f
)) {
503 /****************************************************************************
504 a wrapper around cli_list that adds recursion
505 ****************************************************************************/
506 void do_list(const char *mask
,uint16 attribute
,void (*fn
)(file_info
*),BOOL rec
, BOOL dirs
)
508 static int in_do_list
= 0;
510 if (in_do_list
&& rec
)
512 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
518 do_list_recurse
= rec
;
524 init_do_list_queue();
525 add_to_do_list_queue(mask
);
527 while (! do_list_queue_empty())
529 cli_list(cli
, do_list_queue_head(), attribute
,
531 remove_do_list_queue_head();
532 if ((! do_list_queue_empty()) && (fn
== display_finfo
))
534 char* next_file
= do_list_queue_head();
536 if ((strlen(next_file
) >= 2) &&
537 (next_file
[strlen(next_file
) - 1] == '*') &&
538 (next_file
[strlen(next_file
) - 2] == '\\'))
540 save_ch
= next_file
+
541 strlen(next_file
) - 2;
544 DEBUG(0,("\n%s\n",CNV_LANG(next_file
)));
554 cli_list(cli
, mask
, attribute
, do_list_helper
);
558 reset_do_list_queue();
561 /****************************************************************************
562 get a directory listing
563 ****************************************************************************/
564 static void cmd_dir(void)
566 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
572 pstrcpy(mask
,cur_dir
);
573 if(mask
[strlen(mask
)-1]!='\\')
576 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
587 do_list(mask
, attribute
, display_finfo
, recurse
, True
);
591 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
595 /****************************************************************************
596 get a directory listing
597 ****************************************************************************/
598 static void cmd_du(void)
600 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
606 pstrcpy(mask
,cur_dir
);
607 if(mask
[strlen(mask
)-1]!='\\')
610 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
620 do_list(mask
, attribute
, do_du
, recurse
, True
);
624 DEBUG(0, ("Total number of bytes: %.0f\n", dir_total
));
628 /****************************************************************************
629 get a file from rname to lname
630 ****************************************************************************/
631 static void do_get(char *rname
,char *lname
)
634 BOOL newhandle
= False
;
636 struct timeval tp_start
;
637 int read_size
= io_bufsize
;
642 GetTimeOfDay(&tp_start
);
648 fnum
= cli_open(cli
, rname
, O_RDONLY
, DENY_NONE
);
651 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli
),CNV_LANG(rname
)));
655 if(!strcmp(lname
,"-")) {
656 handle
= fileno(stdout
);
658 handle
= sys_open(lname
,O_WRONLY
|O_CREAT
|O_TRUNC
,0644);
662 DEBUG(0,("Error opening local file %s\n",lname
));
667 if (!cli_qfileinfo(cli
, fnum
,
668 &attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
669 !cli_getattrE(cli
, fnum
,
670 &attr
, &size
, NULL
, NULL
, NULL
)) {
671 DEBUG(0,("getattrib: %s\n",cli_errstr(cli
)));
675 DEBUG(2,("getting file %s of size %.0f as %s ",
676 lname
, (double)size
, lname
));
678 if(!(data
= (char *)malloc(read_size
))) {
679 DEBUG(0,("malloc fail for size %d\n", read_size
));
680 cli_close(cli
, fnum
);
685 int n
= cli_read(cli
, fnum
, data
, nread
, read_size
);
689 if (writefile(handle
,data
, n
) != n
) {
690 DEBUG(0,("Error writing local file\n"));
698 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
699 CNV_LANG(rname
), (long)nread
));
704 if (!cli_close(cli
, fnum
)) {
705 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli
)));
712 if (archive_level
>= 2 && (attr
& aARCH
)) {
713 cli_setatr(cli
, rname
, attr
& ~(uint16
)aARCH
, 0);
717 struct timeval tp_end
;
720 GetTimeOfDay(&tp_end
);
722 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
723 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
724 get_total_time_ms
+= this_time
;
725 get_total_size
+= nread
;
727 DEBUG(2,("(%g kb/s) (average %g kb/s)\n",
728 nread
/ (1.024*this_time
+ 1.0e-4),
729 get_total_size
/ (1.024*get_total_time_ms
)));
734 /****************************************************************************
736 ****************************************************************************/
737 static void cmd_get(void)
743 pstrcpy(rname
,cur_dir
);
746 p
= rname
+ strlen(rname
);
748 if (!next_token(NULL
,p
,NULL
,sizeof(rname
)-strlen(rname
))) {
749 DEBUG(0,("get <filename>\n"));
753 dos_clean_name(rname
);
755 next_token(NULL
,lname
,NULL
,sizeof(lname
));
757 do_get(rname
, lname
);
761 /****************************************************************************
762 do a mget operation on one file
763 ****************************************************************************/
764 static void do_mget(file_info
*finfo
)
768 pstring saved_curdir
;
771 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
775 DEBUG(0,("mget aborted\n"));
779 if (finfo
->mode
& aDIR
)
780 slprintf(quest
,sizeof(pstring
)-1,
781 "Get directory %s? ",CNV_LANG(finfo
->name
));
783 slprintf(quest
,sizeof(pstring
)-1,
784 "Get file %s? ",CNV_LANG(finfo
->name
));
786 if (prompt
&& !yesno(quest
)) return;
788 if (!(finfo
->mode
& aDIR
)) {
789 pstrcpy(rname
,cur_dir
);
790 pstrcat(rname
,finfo
->name
);
791 do_get(rname
,finfo
->name
);
795 /* handle directories */
796 pstrcpy(saved_curdir
,cur_dir
);
798 pstrcat(cur_dir
,finfo
->name
);
799 pstrcat(cur_dir
,"\\");
801 unix_format(finfo
->name
);
803 strlower(finfo
->name
);
805 if (!dos_directory_exist(finfo
->name
,NULL
) &&
806 dos_mkdir(finfo
->name
,0777) != 0) {
807 DEBUG(0,("failed to create directory %s\n",CNV_LANG(finfo
->name
)));
808 pstrcpy(cur_dir
,saved_curdir
);
812 if (dos_chdir(finfo
->name
) != 0) {
813 DEBUG(0,("failed to chdir to directory %s\n",CNV_LANG(finfo
->name
)));
814 pstrcpy(cur_dir
,saved_curdir
);
818 pstrcpy(mget_mask
,cur_dir
);
819 pstrcat(mget_mask
,"*");
821 do_list(mget_mask
, aSYSTEM
| aHIDDEN
| aDIR
,do_mget
,False
, True
);
823 pstrcpy(cur_dir
,saved_curdir
);
827 /****************************************************************************
828 view the file using the pager
829 ****************************************************************************/
830 static void cmd_more(void)
832 fstring rname
,lname
,tmpname
,pager_cmd
;
835 fstrcpy(rname
,cur_dir
);
839 "%s/smbmore.%d",tmpdir(),(int)getpid());
840 fstrcpy(lname
,tmpname
);
842 if (!next_token(NULL
,rname
+strlen(rname
),NULL
,sizeof(rname
)-strlen(rname
))) {
843 DEBUG(0,("more <filename>\n"));
846 dos_clean_name(rname
);
850 pager
=getenv("PAGER");
852 slprintf(pager_cmd
,sizeof(pager_cmd
)-1,
853 "%s %s",(pager
? pager
:PAGER
), tmpname
);
860 /****************************************************************************
862 ****************************************************************************/
863 static void cmd_mget(void)
865 uint16 attribute
= aSYSTEM
| aHIDDEN
;
877 while (next_token(NULL
,p
,NULL
,sizeof(buf
))) {
878 pstrcpy(mget_mask
,cur_dir
);
879 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
880 pstrcat(mget_mask
,"\\");
883 pstrcpy(mget_mask
,p
);
885 pstrcat(mget_mask
,p
);
886 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
890 pstrcpy(mget_mask
,cur_dir
);
891 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
892 pstrcat(mget_mask
,"\\");
893 pstrcat(mget_mask
,"*");
894 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
899 /****************************************************************************
900 make a directory of name "name"
901 ****************************************************************************/
902 static BOOL
do_mkdir(char *name
)
904 if (!cli_mkdir(cli
, name
)) {
905 DEBUG(0,("%s making remote directory %s\n",
906 cli_errstr(cli
),CNV_LANG(name
)));
914 /****************************************************************************
916 ****************************************************************************/
917 static void cmd_quit(void)
924 /****************************************************************************
926 ****************************************************************************/
927 static void cmd_mkdir(void)
933 pstrcpy(mask
,cur_dir
);
935 if (!next_token(NULL
,p
,NULL
,sizeof(buf
))) {
937 DEBUG(0,("mkdir <dirname>\n"));
948 trim_string(ddir
,".",NULL
);
949 p
= strtok(ddir
,"/\\");
952 if (!cli_chkpath(cli
, ddir2
)) {
956 p
= strtok(NULL
,"/\\");
964 /****************************************************************************
966 ****************************************************************************/
967 static void do_put(char *rname
,char *lname
)
973 int maxwrite
=io_bufsize
;
975 struct timeval tp_start
;
976 GetTimeOfDay(&tp_start
);
978 fnum
= cli_open(cli
, rname
, O_WRONLY
|O_CREAT
|O_TRUNC
, DENY_NONE
);
981 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli
),CNV_LANG(rname
)));
985 /* allow files to be piped into smbclient
987 if (!strcmp(lname
, "-")) {
989 /* size of file is not known */
991 f
= sys_fopen(lname
,"r");
995 DEBUG(0,("Error opening local file %s\n",lname
));
1000 DEBUG(1,("putting file %s as %s ",lname
,
1003 buf
= (char *)malloc(maxwrite
);
1008 if ((n
= readfile(buf
,1,n
,f
)) < 1) {
1009 DEBUG(0,("Error reading local file\n"));
1013 ret
= cli_write(cli
, fnum
, 0, buf
, nread
, n
);
1016 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli
)));
1023 if (!cli_close(cli
, fnum
)) {
1024 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli
),CNV_LANG(rname
)));
1035 struct timeval tp_end
;
1038 GetTimeOfDay(&tp_end
);
1040 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
1041 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
1042 put_total_time_ms
+= this_time
;
1043 put_total_size
+= nread
;
1045 DEBUG(1,("(%g kb/s) (average %g kb/s)\n",
1046 nread
/ (1.024*this_time
+ 1.0e-4),
1047 put_total_size
/ (1.024*put_total_time_ms
)));
1058 /****************************************************************************
1060 ****************************************************************************/
1061 static void cmd_put(void)
1068 pstrcpy(rname
,cur_dir
);
1069 pstrcat(rname
,"\\");
1071 if (!next_token(NULL
,p
,NULL
,sizeof(buf
))) {
1072 DEBUG(0,("put <filename>\n"));
1077 if (next_token(NULL
,p
,NULL
,sizeof(buf
)))
1080 pstrcat(rname
,lname
);
1082 dos_clean_name(rname
);
1086 /* allow '-' to represent stdin
1087 jdblair, 24.jun.98 */
1088 if (!file_exist(lname
,&st
) &&
1089 (strcmp(lname
,"-"))) {
1090 DEBUG(0,("%s does not exist\n",lname
));
1095 do_put(rname
,lname
);
1099 /****************************************************************************
1100 seek in a directory/file list until you get something that doesn't start with
1102 ****************************************************************************/
1103 static BOOL
seek_list(FILE *f
,char *name
)
1107 if (fscanf(f
,"%s",s
) != 1) return(False
);
1108 trim_string(s
,"./",NULL
);
1109 if (strncmp(s
,name
,strlen(name
)) != 0) {
1119 /****************************************************************************
1120 set the file selection mask
1121 ****************************************************************************/
1122 static void cmd_select(void)
1124 pstrcpy(fileselection
,"");
1125 next_token(NULL
,fileselection
,NULL
,sizeof(fileselection
));
1129 /****************************************************************************
1131 ****************************************************************************/
1132 static void cmd_mput(void)
1139 while (next_token(NULL
,p
,NULL
,sizeof(buf
))) {
1145 slprintf(tmpname
,sizeof(pstring
)-1,
1146 "%s/ls.smb.%d",tmpdir(),(int)getpid());
1148 slprintf(cmd
,sizeof(pstring
)-1,
1149 "find . -name \"%s\" -print > %s",p
,tmpname
);
1151 slprintf(cmd
,sizeof(pstring
)-1,
1152 "/bin/ls %s > %s",p
,tmpname
);
1155 f
= sys_fopen(tmpname
,"r");
1161 if (fscanf(f
,"%s",lname
) != 1) break;
1162 trim_string(lname
,"./",NULL
);
1166 /* check if it's a directory */
1167 if (directory_exist(lname
,&st
)) {
1168 if (!recurse
) continue;
1169 slprintf(quest
,sizeof(pstring
)-1,
1170 "Put directory %s? ",lname
);
1171 if (prompt
&& !yesno(quest
)) {
1173 if (!seek_list(f
,lname
))
1178 pstrcpy(rname
,cur_dir
);
1179 pstrcat(rname
,lname
);
1180 if (!cli_chkpath(cli
, rname
) && !do_mkdir(rname
)) {
1182 if (!seek_list(f
,lname
))
1188 slprintf(quest
,sizeof(quest
)-1,
1189 "Put file %s? ",lname
);
1190 if (prompt
&& !yesno(quest
)) continue;
1192 pstrcpy(rname
,cur_dir
);
1193 pstrcat(rname
,lname
);
1198 do_put(rname
,lname
);
1206 /****************************************************************************
1208 ****************************************************************************/
1209 static void do_cancel(int job
)
1211 if (cli_printjob_del(cli
, job
)) {
1212 printf("Job %d cancelled\n",job
);
1214 printf("Error calcelling job %d : %s\n",job
,cli_errstr(cli
));
1219 /****************************************************************************
1221 ****************************************************************************/
1222 static void cmd_cancel(void)
1227 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1228 printf("cancel <jobid> ...\n");
1234 } while (next_token(NULL
,buf
,NULL
,sizeof(buf
)));
1238 /****************************************************************************
1240 ****************************************************************************/
1241 static void cmd_print(void)
1247 if (!next_token(NULL
,lname
,NULL
, sizeof(lname
))) {
1248 DEBUG(0,("print <filename>\n"));
1252 pstrcpy(rname
,lname
);
1253 p
= strrchr(rname
,'/');
1255 slprintf(rname
, sizeof(rname
)-1, "%s-%d", p
+1, (int)getpid());
1258 if (strequal(lname
,"-")) {
1259 slprintf(rname
, sizeof(rname
)-1, "stdin-%d", (int)getpid());
1262 do_put(rname
, lname
);
1266 /****************************************************************************
1267 show a print queue entry
1268 ****************************************************************************/
1269 static void queue_fn(struct print_job_info
*p
)
1271 DEBUG(0,("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
));
1274 /****************************************************************************
1276 ****************************************************************************/
1277 static void cmd_queue(void)
1279 cli_print_queue(cli
, queue_fn
);
1282 /****************************************************************************
1284 ****************************************************************************/
1285 static void do_del(file_info
*finfo
)
1289 pstrcpy(mask
,cur_dir
);
1290 pstrcat(mask
,finfo
->name
);
1292 if (finfo
->mode
& aDIR
)
1295 if (!cli_unlink(cli
, mask
)) {
1296 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli
),CNV_LANG(mask
)));
1300 /****************************************************************************
1302 ****************************************************************************/
1303 static void cmd_del(void)
1307 uint16 attribute
= aSYSTEM
| aHIDDEN
;
1312 pstrcpy(mask
,cur_dir
);
1314 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1315 DEBUG(0,("del <filename>\n"));
1320 do_list(mask
, attribute
,do_del
,False
,False
);
1323 /****************************************************************************
1324 ****************************************************************************/
1325 static void cmd_open(void)
1330 pstrcpy(mask
,cur_dir
);
1332 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1333 DEBUG(0,("del <filename>\n"));
1338 cli_open(cli
, mask
, O_RDWR
, DENY_ALL
);
1342 /****************************************************************************
1344 ****************************************************************************/
1345 static void cmd_rmdir(void)
1350 pstrcpy(mask
,cur_dir
);
1352 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1353 DEBUG(0,("rmdir <dirname>\n"));
1358 if (!cli_rmdir(cli
, mask
)) {
1359 DEBUG(0,("%s removing remote directory file %s\n",
1360 cli_errstr(cli
),CNV_LANG(mask
)));
1364 /****************************************************************************
1366 ****************************************************************************/
1367 static void cmd_rename(void)
1372 pstrcpy(src
,cur_dir
);
1373 pstrcpy(dest
,cur_dir
);
1375 if (!next_token(NULL
,buf
,NULL
,sizeof(buf
)) ||
1376 !next_token(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1377 DEBUG(0,("rename <src> <dest>\n"));
1384 if (!cli_rename(cli
, src
, dest
)) {
1385 DEBUG(0,("%s renaming files\n",cli_errstr(cli
)));
1391 /****************************************************************************
1392 toggle the prompt flag
1393 ****************************************************************************/
1394 static void cmd_prompt(void)
1397 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
1401 /****************************************************************************
1402 set the newer than time
1403 ****************************************************************************/
1404 static void cmd_newer(void)
1408 SMB_STRUCT_STAT sbuf
;
1410 ok
= next_token(NULL
,buf
,NULL
,sizeof(buf
));
1411 if (ok
&& (dos_stat(buf
,&sbuf
) == 0)) {
1412 newer_than
= sbuf
.st_mtime
;
1413 DEBUG(1,("Getting files newer than %s",
1414 asctime(LocalTime(&newer_than
))));
1419 if (ok
&& newer_than
== 0)
1420 DEBUG(0,("Error setting newer-than time\n"));
1423 /****************************************************************************
1424 set the archive level
1425 ****************************************************************************/
1426 static void cmd_archive(void)
1430 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1431 archive_level
= atoi(buf
);
1433 DEBUG(0,("Archive level is %d\n",archive_level
));
1436 /****************************************************************************
1437 toggle the lowercaseflag
1438 ****************************************************************************/
1439 static void cmd_lowercase(void)
1441 lowercase
= !lowercase
;
1442 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
1448 /****************************************************************************
1449 toggle the recurse flag
1450 ****************************************************************************/
1451 static void cmd_recurse(void)
1454 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
1457 /****************************************************************************
1458 toggle the translate flag
1459 ****************************************************************************/
1460 static void cmd_translate(void)
1462 translation
= !translation
;
1463 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1464 translation
?"on":"off"));
1468 /****************************************************************************
1469 do a printmode command
1470 ****************************************************************************/
1471 static void cmd_printmode(void)
1476 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1477 if (strequal(buf
,"text")) {
1480 if (strequal(buf
,"graphics"))
1483 printmode
= atoi(buf
);
1490 fstrcpy(mode
,"text");
1493 fstrcpy(mode
,"graphics");
1496 slprintf(mode
,sizeof(mode
)-1,"%d",printmode
);
1500 DEBUG(2,("the printmode is now %s\n",mode
));
1503 /****************************************************************************
1505 ****************************************************************************/
1506 static void cmd_lcd(void)
1511 if (next_token(NULL
,buf
,NULL
,sizeof(buf
)))
1513 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
1516 /****************************************************************************
1518 ****************************************************************************/
1519 static void browse_fn(const char *name
, uint32 m
, const char *comment
)
1526 case STYPE_DISKTREE
:
1527 fstrcpy(typestr
,"Disk"); break;
1529 fstrcpy(typestr
,"Printer"); break;
1531 fstrcpy(typestr
,"Device"); break;
1533 fstrcpy(typestr
,"IPC"); break;
1536 printf("\t%-15.15s%-10.10s%s\n",
1537 name
, typestr
,comment
);
1541 /****************************************************************************
1542 try and browse available connections on a host
1543 ****************************************************************************/
1544 static BOOL
browse_host(BOOL sort
)
1546 printf("\n\tSharename Type Comment\n");
1547 printf("\t--------- ---- -------\n");
1549 return cli_RNetShareEnum(cli
, browse_fn
);
1552 /****************************************************************************
1554 ****************************************************************************/
1555 static void server_fn(const char *name
, uint32 m
, const char *comment
)
1557 printf("\t%-16.16s %s\n", name
, comment
);
1560 /****************************************************************************
1561 try and browse available connections on a host
1562 ****************************************************************************/
1563 static BOOL
list_servers(char *wk_grp
)
1565 printf("\n\tServer Comment\n");
1566 printf("\t--------- -------\n");
1568 cli_NetServerEnum(cli
, workgroup
, SV_TYPE_ALL
, server_fn
);
1570 printf("\n\tWorkgroup Master\n");
1571 printf("\t--------- -------\n");
1573 cli_NetServerEnum(cli
, workgroup
, SV_TYPE_DOMAIN_ENUM
, server_fn
);
1578 /* Some constants for completing filename arguments */
1580 #define COMPL_NONE 0 /* No completions */
1581 #define COMPL_REMOTE 1 /* Complete remote filename */
1582 #define COMPL_LOCAL 2 /* Complete local filename */
1584 /* This defines the commands supported by this client */
1590 char compl_args
[2]; /* Completion argument info */
1593 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1594 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1595 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
1596 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
1597 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
1598 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
1599 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
1600 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1601 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
1602 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1603 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
1604 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
1605 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
1606 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1607 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
1608 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
1609 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
1610 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
1611 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
1612 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
1613 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
1614 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
1615 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
1616 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
1617 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
1618 {"printmode",cmd_printmode
,"<graphics or text> set the print mode",{COMPL_NONE
,COMPL_NONE
}},
1619 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
1620 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
1621 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1622 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1623 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
1624 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
1625 {"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
}},
1626 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
1627 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
1628 {"tarmode",cmd_tarmode
,
1629 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
1630 {"setmode",cmd_setmode
,"filename <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
1631 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
1632 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
1633 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
1634 {"",NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
1638 /*******************************************************************
1639 lookup a command string in the list of commands, including
1641 ******************************************************************/
1642 static int process_tok(fstring tok
)
1644 int i
= 0, matches
= 0;
1646 int tok_len
= strlen(tok
);
1648 while (commands
[i
].fn
!= NULL
) {
1649 if (strequal(commands
[i
].name
,tok
)) {
1653 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
1662 else if (matches
== 1)
1668 /****************************************************************************
1670 ****************************************************************************/
1671 static void cmd_help(void)
1676 if (next_token(NULL
,buf
,NULL
,sizeof(buf
))) {
1677 if ((i
= process_tok(buf
)) >= 0)
1678 DEBUG(0,("HELP %s:\n\t%s\n\n",commands
[i
].name
,commands
[i
].description
));
1680 while (commands
[i
].description
) {
1681 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
1682 DEBUG(0,("%-15s",commands
[i
].name
));
1690 /****************************************************************************
1691 wait for keyboard activity, swallowing network packets
1692 ****************************************************************************/
1693 static void wait_keyboard(void)
1696 struct timeval timeout
;
1700 FD_SET(cli
->fd
,&fds
);
1701 FD_SET(fileno(stdin
),&fds
);
1703 timeout
.tv_sec
= 20;
1704 timeout
.tv_usec
= 0;
1705 sys_select(MAX(cli
->fd
,fileno(stdin
))+1,&fds
,&timeout
);
1707 if (FD_ISSET(fileno(stdin
),&fds
))
1710 /* We deliberately use receive_smb instead of
1711 client_receive_smb as we want to receive
1712 session keepalives and then drop them here.
1714 if (FD_ISSET(cli
->fd
,&fds
))
1715 receive_smb(cli
->fd
,cli
->inbuf
,0);
1717 cli_chkpath(cli
, "\\");
1721 /****************************************************************************
1722 process a -c command string
1723 ****************************************************************************/
1724 static void process_command_string(char *cmd
)
1729 while (cmd
[0] != '\0') {
1734 if ((p
= strchr(cmd
, ';')) == 0) {
1735 strncpy(line
, cmd
, 999);
1739 if (p
- cmd
> 999) p
= cmd
+ 999;
1740 strncpy(line
, cmd
, p
- cmd
);
1741 line
[p
- cmd
] = '\0';
1745 /* input language code to internal one */
1748 /* and get the first part of the command */
1750 if (!next_token(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
1752 if ((i
= process_tok(tok
)) >= 0) {
1754 } else if (i
== -2) {
1755 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok
)));
1757 DEBUG(0,("%s: command not found\n",CNV_LANG(tok
)));
1762 /****************************************************************************
1763 process commands on stdin
1764 ****************************************************************************/
1765 static void process_stdin(void)
1770 while (!feof(stdin
)) {
1774 /* display a prompt */
1775 DEBUG(0,("smb: %s> ", CNV_LANG(cur_dir
)));
1780 /* and get a response */
1781 if (!fgets(line
,1000,stdin
))
1784 /* input language code to internal one */
1787 /* special case - first char is ! */
1793 /* and get the first part of the command */
1795 if (!next_token(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
1797 if ((i
= process_tok(tok
)) >= 0) {
1799 } else if (i
== -2) {
1800 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok
)));
1802 DEBUG(0,("%s: command not found\n",CNV_LANG(tok
)));
1808 /*****************************************************
1809 return a connection to a server
1810 *******************************************************/
1811 struct cli_state
*do_connect(char *server
, char *share
)
1813 struct cli_state
*c
;
1814 struct nmb_name called
, calling
;
1817 extern struct in_addr ipzero
;
1819 if (*share
== '\\') {
1821 share
= strchr(server
,'\\');
1822 if (!share
) return NULL
;
1831 make_nmb_name(&calling
, global_myname
, 0x0, "");
1832 make_nmb_name(&called
, server
, name_type
, "");
1836 if (have_ip
) ip
= dest_ip
;
1838 /* have to open a new connection */
1839 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, port
) == 0) ||
1840 !cli_connect(c
, server_n
, &ip
)) {
1841 DEBUG(0,("Connection to %s failed\n", server_n
));
1845 if (!cli_session_request(c
, &calling
, &called
)) {
1846 DEBUG(0,("session request to %s failed\n", called
.name
));
1848 if (strcmp(called
.name
, "*SMBSERVER")) {
1849 make_nmb_name(&called
, "*SMBSERVER", 0x20, "");
1855 DEBUG(4,(" session request ok\n"));
1857 if (!cli_negprot(c
)) {
1858 DEBUG(0,("protocol negotiation failed\n"));
1864 char *pass
= getpass("Password: ");
1866 pstrcpy(password
, pass
);
1870 if (!cli_session_setup(c
, username
,
1871 password
, strlen(password
),
1872 password
, strlen(password
),
1874 DEBUG(0,("session setup failed: %s\n", cli_errstr(c
)));
1879 * These next two lines are needed to emulate
1880 * old client behaviour for people who have
1881 * scripts based on client output.
1882 * QUESTION ? Do we want to have a 'client compatibility
1883 * mode to turn these on/off ? JRA.
1886 if (*c
->server_domain
|| *c
->server_os
|| *c
->server_type
)
1887 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
1888 c
->server_domain
,c
->server_os
,c
->server_type
));
1890 DEBUG(4,(" session setup ok\n"));
1892 if (!cli_send_tconX(c
, share
, "?????",
1893 password
, strlen(password
)+1)) {
1894 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c
)));
1899 DEBUG(4,(" tconx ok\n"));
1905 /****************************************************************************
1906 process commands from the client
1907 ****************************************************************************/
1908 static BOOL
process(char *base_directory
)
1910 cli
= do_connect(desthost
, service
);
1915 if (*base_directory
) do_cd(base_directory
);
1918 process_command_string(cmdstr
);
1927 /****************************************************************************
1928 usage on the program
1929 ****************************************************************************/
1930 static void usage(char *pname
)
1932 DEBUG(0,("Usage: %s service <password> [options]", pname
));
1934 DEBUG(0,("\nVersion %s\n",VERSION
));
1935 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
1936 DEBUG(0,("\t-B IP addr broadcast IP address to use\n"));
1937 DEBUG(0,("\t-O socket_options socket options to use\n"));
1938 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
1939 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
1940 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
1941 DEBUG(0,("\t-N don't ask for a password\n"));
1942 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
1943 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
1944 DEBUG(0,("\t-P connect to service as a printer\n"));
1945 DEBUG(0,("\t-p port connect to the specified port\n"));
1946 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
1947 DEBUG(0,("\t-h Print this help message.\n"));
1948 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
1949 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
1950 DEBUG(0,("\t-U username set the network username\n"));
1951 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
1952 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
1953 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
1954 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
1955 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
1956 DEBUG(0,("\t-D directory start from directory\n"));
1957 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
1958 DEBUG(0,("\t-b xmit/send buffer changes the transmit/send buffer (default: 65520)\n"));
1963 /****************************************************************************
1964 get a password from a a file or file descriptor
1966 ****************************************************************************/
1967 static void get_password_file(void)
1971 BOOL close_it
= False
;
1975 if ((p
= getenv("PASSWD_FD")) != NULL
) {
1976 pstrcpy(spec
, "descriptor ");
1978 sscanf(p
, "%d", &fd
);
1980 } else if ((p
= getenv("PASSWD_FILE")) != NULL
) {
1981 fd
= sys_open(p
, O_RDONLY
, 0);
1984 fprintf(stderr
, "Error opening PASSWD_FILE %s: %s\n",
1985 spec
, strerror(errno
));
1991 for(p
= pass
, *p
= '\0'; /* ensure that pass is null-terminated */
1992 p
&& p
- pass
< sizeof(pass
);) {
1993 switch (read(fd
, p
, 1)) {
1995 if (*p
!= '\n' && *p
!= '\0') {
1996 *++p
= '\0'; /* advance p, and null-terminate pass */
2001 *p
= '\0'; /* null-terminate it, just in case... */
2002 p
= NULL
; /* then force the loop condition to become false */
2005 fprintf(stderr
, "Error reading password from file %s: %s\n",
2006 spec
, "empty password\n");
2011 fprintf(stderr
, "Error reading password from file %s: %s\n",
2012 spec
, strerror(errno
));
2016 pstrcpy(password
, pass
);
2023 /****************************************************************************
2025 ****************************************************************************/
2026 static int do_host_query(char *query_host
)
2028 cli
= do_connect(query_host
, "IPC$");
2033 list_servers(workgroup
);
2041 /****************************************************************************
2042 handle a tar operation
2043 ****************************************************************************/
2044 static int do_tar_op(char *base_directory
)
2047 cli
= do_connect(desthost
, service
);
2053 if (*base_directory
) do_cd(base_directory
);
2062 /****************************************************************************
2063 handle a message operation
2064 ****************************************************************************/
2065 static int do_message_op(void)
2068 struct nmb_name called
, calling
;
2072 make_nmb_name(&calling
, global_myname
, 0x0, "");
2073 make_nmb_name(&called
, desthost
, name_type
, "");
2076 if (have_ip
) ip
= dest_ip
;
2078 if (!(cli
=cli_initialise(NULL
)) || !cli_connect(cli
, desthost
, &ip
)) {
2079 DEBUG(0,("Connection to %s failed\n", desthost
));
2083 if (!cli_session_request(cli
, &calling
, &called
)) {
2084 DEBUG(0,("session request failed\n"));
2096 /****************************************************************************
2098 ****************************************************************************/
2099 int main(int argc
,char *argv
[])
2101 fstring base_directory
;
2102 char *pname
= argv
[0];
2105 extern char *optarg
;
2108 BOOL message
= False
;
2109 BOOL explicit_user
= False
;
2110 extern char tar_type
;
2111 static pstring servicesf
= CONFIGFILE
;
2113 pstring new_name_resolve_order
;
2117 pstrcpy(term_code
, KANJI
);
2123 *base_directory
= 0;
2125 *new_name_resolve_order
= 0;
2129 setup_logging(pname
,True
);
2132 * If the -E option is given, be careful not to clobber stdout
2133 * before processing the options. 28.Feb.99, richard@hacom.nl.
2134 * Also pre-parse the -s option to get the service file name.
2137 for (opt
= 1; opt
< argc
; opt
++) {
2138 if (strcmp(argv
[opt
], "-E") == 0)
2140 else if(strncmp(argv
[opt
], "-s", 2) == 0) {
2141 if(argv
[opt
][2] != '\0')
2142 pstrcpy(servicesf
, &argv
[opt
][2]);
2143 else if(argv
[opt
+1] != NULL
) {
2145 * At least one more arg left.
2147 pstrcpy(servicesf
, argv
[opt
+1]);
2156 charset_initialise();
2158 if(!get_myname(myhostname
,NULL
)) {
2159 DEBUG(0,("Failed to get my hostname.\n"));
2162 in_client
= True
; /* Make sure that we tell lp_load we are */
2164 if (!lp_load(servicesf
,True
,False
,False
)) {
2165 fprintf(stderr
, "Can't load %s - run testparm to debug it\n", servicesf
);
2168 codepage_initialise(lp_client_code_page());
2174 pstrcpy(workgroup
,lp_workgroup());
2180 if (getenv("USER")) {
2181 pstrcpy(username
,getenv("USER"));
2183 /* modification to support userid%passwd syntax in the USER var
2184 25.Aug.97, jdblair@uab.edu */
2186 if ((p
=strchr(username
,'%'))) {
2188 pstrcpy(password
,p
+1);
2190 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password
));
2195 /* modification to support PASSWD environmental var
2196 25.Aug.97, jdblair@uab.edu */
2197 if (getenv("PASSWD")) {
2198 pstrcpy(password
,getenv("PASSWD"));
2202 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2203 get_password_file();
2207 if (*username
== 0 && getenv("LOGNAME")) {
2208 pstrcpy(username
,getenv("LOGNAME"));
2212 if (*username
== 0) {
2213 pstrcpy(username
,"GUEST");
2221 if (*argv
[1] != '-') {
2222 pstrcpy(service
,argv
[1]);
2223 /* Convert any '/' characters in the service name to '\' characters */
2224 string_replace( service
, '/','\\');
2228 if (count_chars(service
,'\\') < 3) {
2230 printf("\n%s: Not enough '\\' characters in service\n",service
);
2234 if (argc
> 1 && (*argv
[1] != '-')) {
2236 pstrcpy(password
,argv
[1]);
2237 memset(argv
[1],'X',strlen(argv
[1]));
2244 getopt(argc
, argv
,"s:B:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:")) != EOF
) {
2247 pstrcpy(servicesf
, optarg
);
2250 iface_set_default(NULL
,optarg
,NULL
);
2253 pstrcpy(user_socket_options
,optarg
);
2256 pstrcpy(new_name_resolve_order
, optarg
);
2259 name_type
= 0x03; /* messages are sent to NetBIOS name type 0x3 */
2260 pstrcpy(desthost
,optarg
);
2264 pstrcpy(scope
,optarg
);
2270 pstrcpy(global_myname
,optarg
);
2276 DEBUGLEVEL
= atoi(optarg
);
2279 /* not needed anymore */
2282 port
= atoi(optarg
);
2285 slprintf(debugf
,sizeof(debugf
)-1, "%s.client",optarg
);
2293 dest_ip
= *interpret_addr2(optarg
);
2294 if (zero_ip(dest_ip
))
2305 explicit_user
= True
;
2306 pstrcpy(username
,optarg
);
2307 if ((lp
=strchr(username
,'%'))) {
2309 pstrcpy(password
,lp
+1);
2311 memset(strchr(optarg
,'%')+1,'X',strlen(password
));
2317 while(*p
== '\\' || *p
== '/')
2319 pstrcpy(query_host
,p
);
2325 pstrcpy(term_code
, optarg
);
2328 /* no longer supported */
2331 pstrcpy(workgroup
,optarg
);
2334 if (!tar_parseargs(argc
, argv
, optarg
, optind
)) {
2340 pstrcpy(base_directory
,optarg
);
2347 io_bufsize
= MAX(1, atoi(optarg
));
2355 get_myname((*global_myname
)?NULL
:global_myname
,NULL
);
2357 if(*new_name_resolve_order
)
2358 lp_set_name_resolve_order(new_name_resolve_order
);
2361 interpret_coding_system(term_code
);
2363 if (!tar_type
&& !*query_host
&& !*service
&& !message
) {
2368 DEBUG( 3, ( "Client started (version %s).\n", VERSION
) );
2371 return do_tar_op(base_directory
);
2374 if ((p
=strchr(query_host
,'#'))) {
2377 sscanf(p
, "%x", &name_type
);
2381 return do_host_query(query_host
);
2385 return do_message_op();
2388 if (!process(base_directory
)) {