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 static pstring password;
39 static pstring username;
40 static pstring workgroup;
43 static int io_bufsize = 65520;
44 extern struct in_addr ipzero;
46 static int name_type = 0x20;
48 extern pstring user_socket_options;
50 static int process_tok(fstring tok);
51 static void cmd_help(void);
53 /* 30 second timeout on most commands */
54 #define CLIENT_TIMEOUT (30*1000)
55 #define SHORT_TIMEOUT (5*1000)
57 /* value for unused fid field in trans2 secondary request */
58 #define FID_UNUSED (0xFFFF)
60 time_t newer_than = 0;
61 int archive_level = 0;
63 extern pstring debugf;
64 extern int DEBUGLEVEL;
66 BOOL translation = False;
70 /* clitar bits insert */
73 extern BOOL tar_reset;
77 mode_t myumask = 0755;
83 static BOOL recurse = False;
84 BOOL lowercase = False;
86 struct in_addr dest_ip;
88 #define SEPARATORS " \t\n\r"
90 BOOL abort_mget = True;
92 pstring fileselection = "";
94 extern file_info def_finfo;
97 int get_total_size = 0;
98 int get_total_time_ms = 0;
99 int put_total_size = 0;
100 int put_total_time_ms = 0;
103 static double dir_total;
107 /****************************************************************************
108 write to a local file with CR/LF->LF translation if appropriate. return the
109 number taken from the buffer. This may not equal the number written.
110 ****************************************************************************/
111 static int writefile(int f, char *b, int n)
121 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
124 if (write(f, b, 1) != 1) {
134 /****************************************************************************
135 read from a file with LF->CR/LF translation if appropriate. return the
136 number read. read approx n bytes.
137 ****************************************************************************/
138 static int readfile(char *b, int size, int n, FILE *f)
143 if (!translation || (size != 1))
144 return(fread(b,size,n,f));
147 while (i < (n - 1) && (i < BUFFER_SIZE)) {
148 if ((c = getc(f)) == EOF) {
152 if (c == '\n') { /* change all LFs to CR/LF */
163 /****************************************************************************
165 ****************************************************************************/
166 static void send_message(void)
171 if (!cli_message_start(cli, desthost, username, &grp_id)) {
172 DEBUG(0,("message start: %s\n", cli_errstr(cli)));
177 printf("Connected. Type your message, ending it with a Control-D\n");
179 while (!feof(stdin) && total_len < 1600) {
180 int maxlen = MIN(1600 - total_len,127);
187 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
194 * The message is in UNIX codepage format. Convert to
195 * DOS before sending.
198 unix_to_dos(msg, True);
200 if (!cli_message_text(cli, msg, l, grp_id)) {
201 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
208 if (total_len >= 1600)
209 printf("the message was truncated to 1600 bytes\n");
211 printf("sent %d bytes\n",total_len);
213 if (!cli_message_end(cli, grp_id)) {
214 printf("SMBsendend failed (%s)\n",cli_errstr(cli));
221 /****************************************************************************
222 check the space on a device
223 ****************************************************************************/
224 static void do_dskattr(void)
226 int total, bsize, avail;
228 if (!cli_dskattr(cli, &bsize, &total, &avail)) {
229 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli)));
233 DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
234 total, bsize, avail));
237 /****************************************************************************
239 ****************************************************************************/
240 static void cmd_pwd(void)
242 DEBUG(0,("Current directory is %s",service));
243 DEBUG(0,("%s\n",cur_dir));
247 /****************************************************************************
248 change directory - inner section
249 ****************************************************************************/
250 static void do_cd(char *newdir)
258 /* Save the current directory in case the
259 new directory is invalid */
260 pstrcpy(saved_dir, cur_dir);
265 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
266 pstrcat(cur_dir, "\\");
268 dos_clean_name(cur_dir);
269 pstrcpy(dname,cur_dir);
270 pstrcat(cur_dir,"\\");
271 dos_clean_name(cur_dir);
273 if (!strequal(cur_dir,"\\")) {
274 if (!cli_chkpath(cli, dname)) {
275 DEBUG(0,("cd %s: %s\n", dname, cli_errstr(cli)));
276 pstrcpy(cur_dir,saved_dir);
280 pstrcpy(cd_path,cur_dir);
283 /****************************************************************************
285 ****************************************************************************/
286 static void cmd_cd(void)
290 if (next_token(NULL,buf,NULL,sizeof(buf)))
293 DEBUG(0,("Current directory is %s\n",cur_dir));
297 /*******************************************************************
298 decide if a file should be operated on
299 ********************************************************************/
300 static BOOL do_this_one(file_info *finfo)
302 if (finfo->mode & aDIR) return(True);
304 if (*fileselection &&
305 !mask_match(finfo->name,fileselection,False)) {
306 DEBUG(3,("match_match %s failed\n", finfo->name));
310 if (newer_than && finfo->mtime < newer_than) {
311 DEBUG(3,("newer_than %s failed\n", finfo->name));
315 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
316 DEBUG(3,("archive %s failed\n", finfo->name));
323 /****************************************************************************
324 display info about a file
325 ****************************************************************************/
326 static void display_finfo(file_info *finfo)
328 if (do_this_one(finfo)) {
329 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
330 DEBUG(0,(" %-30s%7.7s %8.0f %s",
332 attrib_string(finfo->mode),
334 asctime(LocalTime(&t))));
335 dir_total += finfo->size;
340 /****************************************************************************
341 accumulate size of a file
342 ****************************************************************************/
343 static void do_du(file_info *finfo)
345 if (do_this_one(finfo)) {
346 dir_total += finfo->size;
350 static BOOL do_list_recurse;
351 static BOOL do_list_dirs;
352 static char *do_list_queue = 0;
353 static long do_list_queue_size = 0;
354 static long do_list_queue_start = 0;
355 static long do_list_queue_end = 0;
356 static void (*do_list_fn)(file_info *);
358 /****************************************************************************
359 functions for do_list_queue
360 ****************************************************************************/
363 * The do_list_queue is a NUL-separated list of strings stored in a
364 * char*. Since this is a FIFO, we keep track of the beginning and
365 * ending locations of the data in the queue. When we overflow, we
366 * double the size of the char*. When the start of the data passes
367 * the midpoint, we move everything back. This is logically more
368 * complex than a linked list, but easier from a memory management
369 * angle. In any memory error condition, do_list_queue is reset.
370 * Functions check to ensure that do_list_queue is non-NULL before
373 static void reset_do_list_queue(void)
380 do_list_queue_size = 0;
381 do_list_queue_start = 0;
382 do_list_queue_end = 0;
385 static void init_do_list_queue(void)
387 reset_do_list_queue();
388 do_list_queue_size = 1024;
389 do_list_queue = malloc(do_list_queue_size);
390 if (do_list_queue == 0) {
391 DEBUG(0,("malloc fail for size %d\n",
392 (int)do_list_queue_size));
393 reset_do_list_queue();
395 memset(do_list_queue, 0, do_list_queue_size);
399 static void adjust_do_list_queue(void)
402 * If the starting point of the queue is more than half way through,
403 * move everything toward the beginning.
405 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
407 DEBUG(4,("do_list_queue is empty\n"));
408 do_list_queue_start = do_list_queue_end = 0;
409 *do_list_queue = '\0';
411 else if (do_list_queue_start > (do_list_queue_size / 2))
413 DEBUG(4,("sliding do_list_queue backward\n"));
414 memmove(do_list_queue,
415 do_list_queue + do_list_queue_start,
416 do_list_queue_end - do_list_queue_start);
417 do_list_queue_end -= do_list_queue_start;
418 do_list_queue_start = 0;
423 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 do_list_queue = Realloc(do_list_queue, do_list_queue_size);
432 if (! do_list_queue) {
433 DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
434 (int)do_list_queue_size));
435 reset_do_list_queue();
439 memset(do_list_queue + do_list_queue_size / 2,
440 0, do_list_queue_size / 2);
445 pstrcpy(do_list_queue + do_list_queue_end, entry);
446 do_list_queue_end = new_end;
447 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
448 entry, (int)do_list_queue_start, (int)do_list_queue_end));
452 static char *do_list_queue_head(void)
454 return do_list_queue + do_list_queue_start;
457 static void remove_do_list_queue_head(void)
459 if (do_list_queue_end > do_list_queue_start)
461 do_list_queue_start += strlen(do_list_queue_head()) + 1;
462 adjust_do_list_queue();
463 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
464 (int)do_list_queue_start, (int)do_list_queue_end));
468 static int do_list_queue_empty(void)
470 return (! (do_list_queue && *do_list_queue));
473 /****************************************************************************
475 ****************************************************************************/
476 static void do_list_helper(file_info *f, const char *mask)
478 if (f->mode & aDIR) {
479 if (do_list_dirs && do_this_one(f)) {
482 if (do_list_recurse &&
483 !strequal(f->name,".") &&
484 !strequal(f->name,"..")) {
488 pstrcpy(mask2, mask);
489 p = strrchr(mask2,'\\');
492 pstrcat(mask2, f->name);
493 pstrcat(mask2,"\\*");
494 add_to_do_list_queue(mask2);
499 if (do_this_one(f)) {
505 /****************************************************************************
506 a wrapper around cli_list that adds recursion
507 ****************************************************************************/
508 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
510 static int in_do_list = 0;
512 if (in_do_list && rec)
514 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
520 do_list_recurse = rec;
526 init_do_list_queue();
527 add_to_do_list_queue(mask);
529 while (! do_list_queue_empty())
532 * Need to copy head so that it doesn't become
533 * invalid inside the call to cli_list. This
534 * would happen if the list were expanded
536 * Fix from E. Jay Berkenbilt (ejb@ql.org)
539 pstrcpy(head, do_list_queue_head());
540 cli_list(cli, head, attribute, do_list_helper);
541 remove_do_list_queue_head();
542 if ((! do_list_queue_empty()) && (fn == display_finfo))
544 char* next_file = do_list_queue_head();
546 if ((strlen(next_file) >= 2) &&
547 (next_file[strlen(next_file) - 1] == '*') &&
548 (next_file[strlen(next_file) - 2] == '\\'))
550 save_ch = next_file +
551 strlen(next_file) - 2;
554 DEBUG(0,("\n%s\n",next_file));
564 if (cli_list(cli, mask, attribute, do_list_helper) == -1)
566 DEBUG(0, ("%s listing %s\n", cli_errstr(cli), mask));
571 reset_do_list_queue();
574 /****************************************************************************
575 get a directory listing
576 ****************************************************************************/
577 static void cmd_dir(void)
579 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
585 pstrcpy(mask,cur_dir);
586 if(mask[strlen(mask)-1]!='\\')
589 if (next_token(NULL,buf,NULL,sizeof(buf))) {
600 do_list(mask, attribute, display_finfo, recurse, True);
604 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
608 /****************************************************************************
609 get a directory listing
610 ****************************************************************************/
611 static void cmd_du(void)
613 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
619 pstrcpy(mask,cur_dir);
620 if(mask[strlen(mask)-1]!='\\')
623 if (next_token(NULL,buf,NULL,sizeof(buf))) {
633 do_list(mask, attribute, do_du, recurse, True);
637 DEBUG(0, ("Total number of bytes: %.0f\n", dir_total));
641 /****************************************************************************
642 get a file from rname to lname
643 ****************************************************************************/
644 static void do_get(char *rname,char *lname)
647 BOOL newhandle = False;
649 struct timeval tp_start;
650 int read_size = io_bufsize;
655 GetTimeOfDay(&tp_start);
661 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
664 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
668 if(!strcmp(lname,"-")) {
669 handle = fileno(stdout);
671 handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
675 DEBUG(0,("Error opening local file %s\n",lname));
680 if (!cli_qfileinfo(cli, fnum,
681 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
682 !cli_getattrE(cli, fnum,
683 &attr, &size, NULL, NULL, NULL)) {
684 DEBUG(0,("getattrib: %s\n",cli_errstr(cli)));
688 DEBUG(2,("getting file %s of size %.0f as %s ",
689 lname, (double)size, lname));
691 if(!(data = (char *)malloc(read_size))) {
692 DEBUG(0,("malloc fail for size %d\n", read_size));
693 cli_close(cli, fnum);
698 int n = cli_read(cli, fnum, data, nread, read_size);
702 if (writefile(handle,data, n) != n) {
703 DEBUG(0,("Error writing local file\n"));
711 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
712 rname, (long)nread));
717 if (!cli_close(cli, fnum)) {
718 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
725 if (archive_level >= 2 && (attr & aARCH)) {
726 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
730 struct timeval tp_end;
733 GetTimeOfDay(&tp_end);
735 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
736 (tp_end.tv_usec - tp_start.tv_usec)/1000;
737 get_total_time_ms += this_time;
738 get_total_size += nread;
740 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
741 nread / (1.024*this_time + 1.0e-4),
742 get_total_size / (1.024*get_total_time_ms)));
747 /****************************************************************************
749 ****************************************************************************/
750 static void cmd_get(void)
756 pstrcpy(rname,cur_dir);
759 p = rname + strlen(rname);
761 if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
762 DEBUG(0,("get <filename>\n"));
766 dos_clean_name(rname);
768 next_token(NULL,lname,NULL,sizeof(lname));
770 do_get(rname, lname);
774 /****************************************************************************
775 do a mget operation on one file
776 ****************************************************************************/
777 static void do_mget(file_info *finfo)
781 pstring saved_curdir;
784 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
788 DEBUG(0,("mget aborted\n"));
792 if (finfo->mode & aDIR)
793 slprintf(quest,sizeof(pstring)-1,
794 "Get directory %s? ",finfo->name);
796 slprintf(quest,sizeof(pstring)-1,
797 "Get file %s? ",finfo->name);
799 if (prompt && !yesno(quest)) return;
801 if (!(finfo->mode & aDIR)) {
802 pstrcpy(rname,cur_dir);
803 pstrcat(rname,finfo->name);
804 do_get(rname,finfo->name);
808 /* handle directories */
809 pstrcpy(saved_curdir,cur_dir);
811 pstrcat(cur_dir,finfo->name);
812 pstrcat(cur_dir,"\\");
814 unix_format(finfo->name);
816 strlower(finfo->name);
818 if (!directory_exist(finfo->name,NULL) &&
819 mkdir(finfo->name,0777) != 0) {
820 DEBUG(0,("failed to create directory %s\n",finfo->name));
821 pstrcpy(cur_dir,saved_curdir);
825 if (chdir(finfo->name) != 0) {
826 DEBUG(0,("failed to chdir to directory %s\n",finfo->name));
827 pstrcpy(cur_dir,saved_curdir);
831 pstrcpy(mget_mask,cur_dir);
832 pstrcat(mget_mask,"*");
834 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
836 pstrcpy(cur_dir,saved_curdir);
840 /****************************************************************************
841 view the file using the pager
842 ****************************************************************************/
843 static void cmd_more(void)
845 fstring rname,lname,tmpname,pager_cmd;
848 fstrcpy(rname,cur_dir);
852 "%s/smbmore.%d",tmpdir(),(int)sys_getpid());
853 fstrcpy(lname,tmpname);
855 if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
856 DEBUG(0,("more <filename>\n"));
859 dos_clean_name(rname);
863 pager=getenv("PAGER");
865 slprintf(pager_cmd,sizeof(pager_cmd)-1,
866 "%s %s",(pager? pager:PAGER), tmpname);
873 /****************************************************************************
875 ****************************************************************************/
876 static void cmd_mget(void)
878 uint16 attribute = aSYSTEM | aHIDDEN;
890 while (next_token(NULL,p,NULL,sizeof(buf))) {
891 pstrcpy(mget_mask,cur_dir);
892 if(mget_mask[strlen(mget_mask)-1]!='\\')
893 pstrcat(mget_mask,"\\");
896 pstrcpy(mget_mask,p);
898 pstrcat(mget_mask,p);
899 do_list(mget_mask, attribute,do_mget,False,True);
903 pstrcpy(mget_mask,cur_dir);
904 if(mget_mask[strlen(mget_mask)-1]!='\\')
905 pstrcat(mget_mask,"\\");
906 pstrcat(mget_mask,"*");
907 do_list(mget_mask, attribute,do_mget,False,True);
912 /****************************************************************************
913 make a directory of name "name"
914 ****************************************************************************/
915 static BOOL do_mkdir(char *name)
917 if (!cli_mkdir(cli, name)) {
918 DEBUG(0,("%s making remote directory %s\n",
919 cli_errstr(cli),name));
927 /****************************************************************************
929 ****************************************************************************/
930 static void cmd_quit(void)
937 /****************************************************************************
939 ****************************************************************************/
940 static void cmd_mkdir(void)
946 pstrcpy(mask,cur_dir);
948 if (!next_token(NULL,p,NULL,sizeof(buf))) {
950 DEBUG(0,("mkdir <dirname>\n"));
961 trim_string(ddir,".",NULL);
962 p = strtok(ddir,"/\\");
965 if (!cli_chkpath(cli, ddir2)) {
969 p = strtok(NULL,"/\\");
977 /****************************************************************************
979 ****************************************************************************/
980 static void do_put(char *rname,char *lname)
986 int maxwrite=io_bufsize;
988 struct timeval tp_start;
989 GetTimeOfDay(&tp_start);
991 fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
994 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
998 /* allow files to be piped into smbclient
1000 if (!strcmp(lname, "-")) {
1002 /* size of file is not known */
1004 f = sys_fopen(lname,"r");
1008 DEBUG(0,("Error opening local file %s\n",lname));
1013 DEBUG(1,("putting file %s as %s ",lname,
1016 buf = (char *)malloc(maxwrite);
1021 if ((n = readfile(buf,1,n,f)) < 1) {
1022 if((n == 0) && feof(f))
1023 break; /* Empty local file. */
1025 DEBUG(0,("Error reading local file: %s\n", strerror(errno) ));
1029 ret = cli_write(cli, fnum, 0, buf, nread, n);
1032 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
1039 if (!cli_close(cli, fnum)) {
1040 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),rname));
1051 struct timeval tp_end;
1054 GetTimeOfDay(&tp_end);
1056 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1057 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1058 put_total_time_ms += this_time;
1059 put_total_size += nread;
1061 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1062 nread / (1.024*this_time + 1.0e-4),
1063 put_total_size / (1.024*put_total_time_ms)));
1074 /****************************************************************************
1076 ****************************************************************************/
1077 static void cmd_put(void)
1084 pstrcpy(rname,cur_dir);
1085 pstrcat(rname,"\\");
1087 if (!next_token(NULL,p,NULL,sizeof(buf))) {
1088 DEBUG(0,("put <filename>\n"));
1093 if (next_token(NULL,p,NULL,sizeof(buf)))
1096 pstrcat(rname,lname);
1098 dos_clean_name(rname);
1102 /* allow '-' to represent stdin
1103 jdblair, 24.jun.98 */
1104 if (!file_exist(lname,&st) &&
1105 (strcmp(lname,"-"))) {
1106 DEBUG(0,("%s does not exist\n",lname));
1111 do_put(rname,lname);
1115 /****************************************************************************
1116 seek in a directory/file list until you get something that doesn't start with
1118 ****************************************************************************/
1119 static BOOL seek_list(FILE *f,char *name)
1123 if (!fgets(s,sizeof(s),f)) return(False);
1124 trim_string(s,"./","\n");
1125 if (strncmp(s,name,strlen(name)) != 0) {
1135 /****************************************************************************
1136 set the file selection mask
1137 ****************************************************************************/
1138 static void cmd_select(void)
1140 pstrcpy(fileselection,"");
1141 next_token(NULL,fileselection,NULL,sizeof(fileselection));
1145 /****************************************************************************
1147 ****************************************************************************/
1148 static void cmd_mput(void)
1155 while (next_token(NULL,p,NULL,sizeof(buf))) {
1161 slprintf(tmpname,sizeof(pstring)-1,
1162 "%s/ls.smb.%d",tmpdir(),(int)sys_getpid());
1164 slprintf(cmd,sizeof(pstring)-1,
1165 "find . -name \"%s\" -print > %s",p,tmpname);
1167 slprintf(cmd,sizeof(pstring)-1,
1168 "find . -maxdepth 1 -name \"%s\" -print > %s",p,tmpname);
1171 f = sys_fopen(tmpname,"r");
1177 if (!fgets(lname,sizeof(lname),f)) break;
1178 trim_string(lname,"./","\n");
1182 /* check if it's a directory */
1183 if (directory_exist(lname,&st)) {
1184 if (!recurse) continue;
1185 slprintf(quest,sizeof(pstring)-1,
1186 "Put directory %s? ",lname);
1187 if (prompt && !yesno(quest)) {
1189 if (!seek_list(f,lname))
1194 pstrcpy(rname,cur_dir);
1195 pstrcat(rname,lname);
1197 if (!cli_chkpath(cli, rname) && !do_mkdir(rname)) {
1199 if (!seek_list(f,lname))
1205 slprintf(quest,sizeof(quest)-1,
1206 "Put file %s? ",lname);
1207 if (prompt && !yesno(quest)) continue;
1209 pstrcpy(rname,cur_dir);
1210 pstrcat(rname,lname);
1215 do_put(rname,lname);
1223 /****************************************************************************
1225 ****************************************************************************/
1226 static void do_cancel(int job)
1228 if (cli_printjob_del(cli, job)) {
1229 printf("Job %d cancelled\n",job);
1231 printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1236 /****************************************************************************
1238 ****************************************************************************/
1239 static void cmd_cancel(void)
1244 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1245 printf("cancel <jobid> ...\n");
1251 } while (next_token(NULL,buf,NULL,sizeof(buf)));
1255 /****************************************************************************
1257 ****************************************************************************/
1258 static void cmd_print(void)
1264 if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1265 DEBUG(0,("print <filename>\n"));
1269 pstrcpy(rname,lname);
1270 p = strrchr(rname,'/');
1272 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1275 if (strequal(lname,"-")) {
1276 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1279 do_put(rname, lname);
1283 /****************************************************************************
1284 show a print queue entry
1285 ****************************************************************************/
1286 static void queue_fn(struct print_job_info *p)
1288 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1291 /****************************************************************************
1293 ****************************************************************************/
1294 static void cmd_queue(void)
1296 cli_print_queue(cli, queue_fn);
1299 /****************************************************************************
1301 ****************************************************************************/
1302 static void do_del(file_info *finfo)
1306 pstrcpy(mask,cur_dir);
1307 pstrcat(mask,finfo->name);
1309 if (finfo->mode & aDIR)
1312 if (!cli_unlink(cli, mask)) {
1313 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),mask));
1317 /****************************************************************************
1319 ****************************************************************************/
1320 static void cmd_del(void)
1324 uint16 attribute = aSYSTEM | aHIDDEN;
1329 pstrcpy(mask,cur_dir);
1331 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1332 DEBUG(0,("del <filename>\n"));
1337 do_list(mask, attribute,do_del,False,False);
1340 /****************************************************************************
1341 ****************************************************************************/
1342 static void cmd_open(void)
1347 pstrcpy(mask,cur_dir);
1349 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1350 DEBUG(0,("del <filename>\n"));
1355 cli_open(cli, mask, O_RDWR, DENY_ALL);
1359 /****************************************************************************
1361 ****************************************************************************/
1362 static void cmd_rmdir(void)
1367 pstrcpy(mask,cur_dir);
1369 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1370 DEBUG(0,("rmdir <dirname>\n"));
1375 if (!cli_rmdir(cli, mask)) {
1376 DEBUG(0,("%s removing remote directory file %s\n",
1377 cli_errstr(cli),mask));
1381 /****************************************************************************
1383 ****************************************************************************/
1384 static void cmd_rename(void)
1389 pstrcpy(src,cur_dir);
1390 pstrcpy(dest,cur_dir);
1392 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1393 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1394 DEBUG(0,("rename <src> <dest>\n"));
1401 if (!cli_rename(cli, src, dest)) {
1402 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1408 /****************************************************************************
1409 toggle the prompt flag
1410 ****************************************************************************/
1411 static void cmd_prompt(void)
1414 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1418 /****************************************************************************
1419 set the newer than time
1420 ****************************************************************************/
1421 static void cmd_newer(void)
1425 SMB_STRUCT_STAT sbuf;
1427 ok = next_token(NULL,buf,NULL,sizeof(buf));
1428 if (ok && (sys_stat(buf,&sbuf) == 0)) {
1429 newer_than = sbuf.st_mtime;
1430 DEBUG(1,("Getting files newer than %s",
1431 asctime(LocalTime(&newer_than))));
1436 if (ok && newer_than == 0)
1437 DEBUG(0,("Error setting newer-than time\n"));
1440 /****************************************************************************
1441 set the archive level
1442 ****************************************************************************/
1443 static void cmd_archive(void)
1447 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1448 archive_level = atoi(buf);
1450 DEBUG(0,("Archive level is %d\n",archive_level));
1453 /****************************************************************************
1454 toggle the lowercaseflag
1455 ****************************************************************************/
1456 static void cmd_lowercase(void)
1458 lowercase = !lowercase;
1459 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1465 /****************************************************************************
1466 toggle the recurse flag
1467 ****************************************************************************/
1468 static void cmd_recurse(void)
1471 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1474 /****************************************************************************
1475 toggle the translate flag
1476 ****************************************************************************/
1477 static void cmd_translate(void)
1479 translation = !translation;
1480 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1481 translation?"on":"off"));
1485 /****************************************************************************
1486 do a printmode command
1487 ****************************************************************************/
1488 static void cmd_printmode(void)
1493 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1494 if (strequal(buf,"text")) {
1497 if (strequal(buf,"graphics"))
1500 printmode = atoi(buf);
1507 fstrcpy(mode,"text");
1510 fstrcpy(mode,"graphics");
1513 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1517 DEBUG(2,("the printmode is now %s\n",mode));
1520 /****************************************************************************
1522 ****************************************************************************/
1523 static void cmd_lcd(void)
1528 if (next_token(NULL,buf,NULL,sizeof(buf)))
1530 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
1533 /****************************************************************************
1535 ****************************************************************************/
1536 static void browse_fn(const char *name, uint32 m,
1537 const char *comment, void *state)
1545 case STYPE_DISKTREE:
1546 fstrcpy(typestr,"Disk"); break;
1548 fstrcpy(typestr,"Printer"); break;
1550 fstrcpy(typestr,"Device"); break;
1552 fstrcpy(typestr,"IPC"); break;
1555 printf("\t%-15.15s%-10.10s%s\n",
1556 name, typestr,comment);
1560 /****************************************************************************
1561 try and browse available connections on a host
1562 ****************************************************************************/
1563 static BOOL browse_host(BOOL sort)
1567 printf("\n\tSharename Type Comment\n");
1568 printf("\t--------- ---- -------\n");
1570 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
1571 printf("Error returning browse list: %s\n", cli_errstr(cli));
1576 /****************************************************************************
1578 ****************************************************************************/
1579 static void server_fn(const char *name, uint32 m,
1580 const char *comment, void *state)
1582 printf("\t%-16.16s %s\n", name, comment);
1585 /****************************************************************************
1586 try and browse available connections on a host
1587 ****************************************************************************/
1588 static BOOL list_servers(char *wk_grp)
1590 if (!cli->server_domain) return False;
1592 printf("\n\tServer Comment\n");
1593 printf("\t--------- -------\n");
1595 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn, NULL);
1597 printf("\n\tWorkgroup Master\n");
1598 printf("\t--------- -------\n");
1600 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM, server_fn, NULL);
1604 #if defined(HAVE_LIBREADLINE)
1605 # if defined(HAVE_READLINE_HISTORY_H) || defined(HAVE_HISTORY_H)
1606 /****************************************************************************
1608 ****************************************************************************/
1609 static void cmd_history(void)
1614 hlist = history_list (); /* Get pointer to history list */
1616 if (hlist) /* If list not empty */
1618 for (i = 0; hlist[i]; i++) /* then display it */
1619 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
1625 /* Some constants for completing filename arguments */
1627 #define COMPL_NONE 0 /* No completions */
1628 #define COMPL_REMOTE 1 /* Complete remote filename */
1629 #define COMPL_LOCAL 2 /* Complete local filename */
1631 /* This defines the commands supported by this client */
1637 char compl_args[2]; /* Completion argument info */
1640 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1641 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1642 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1643 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1644 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1645 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1646 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1647 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1648 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1649 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1650 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1651 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1652 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1653 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1654 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
1655 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1656 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1657 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1658 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1659 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1660 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1661 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1662 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1663 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1664 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1665 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1666 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1667 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1668 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1669 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1670 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1671 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1672 {"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}},
1673 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1674 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1675 {"tarmode",cmd_tarmode,
1676 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1677 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1678 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1679 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1680 #ifdef HAVE_LIBREADLINE
1681 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
1683 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1684 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1688 /*******************************************************************
1689 lookup a command string in the list of commands, including
1691 ******************************************************************/
1692 static int process_tok(fstring tok)
1694 int i = 0, matches = 0;
1696 int tok_len = strlen(tok);
1698 while (commands[i].fn != NULL) {
1699 if (strequal(commands[i].name,tok)) {
1703 } else if (strnequal(commands[i].name, tok, tok_len)) {
1712 else if (matches == 1)
1718 /****************************************************************************
1720 ****************************************************************************/
1721 static void cmd_help(void)
1726 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1727 if ((i = process_tok(buf)) >= 0)
1728 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1730 while (commands[i].description) {
1731 for (j=0; commands[i].description && (j<5); j++) {
1732 DEBUG(0,("%-15s",commands[i].name));
1740 #ifndef HAVE_LIBREADLINE
1741 /****************************************************************************
1742 wait for keyboard activity, swallowing network packets
1743 ****************************************************************************/
1744 static void wait_keyboard(void)
1747 struct timeval timeout;
1751 FD_SET(cli->fd,&fds);
1752 FD_SET(fileno(stdin),&fds);
1754 timeout.tv_sec = 20;
1755 timeout.tv_usec = 0;
1756 sys_select_intr(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
1758 if (FD_ISSET(fileno(stdin),&fds))
1761 /* We deliberately use receive_smb instead of
1762 client_receive_smb as we want to receive
1763 session keepalives and then drop them here.
1765 if (FD_ISSET(cli->fd,&fds))
1766 receive_smb(cli->fd,cli->inbuf,0);
1768 cli_chkpath(cli, "\\");
1773 /****************************************************************************
1774 process a -c command string
1775 ****************************************************************************/
1776 static void process_command_string(char *cmd)
1781 while (cmd[0] != '\0') {
1786 if ((p = strchr(cmd, ';')) == 0) {
1787 strncpy(line, cmd, 999);
1791 if (p - cmd > 999) p = cmd + 999;
1792 strncpy(line, cmd, p - cmd);
1793 line[p - cmd] = '\0';
1797 /* and get the first part of the command */
1799 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1801 if ((i = process_tok(tok)) >= 0) {
1803 } else if (i == -2) {
1804 DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
1806 DEBUG(0,("%s: command not found\n",tok));
1811 /****************************************************************************
1812 process commands on stdin
1813 ****************************************************************************/
1814 static void process_stdin(void)
1819 #ifdef HAVE_LIBREADLINE
1820 /* Minimal readline support, 29Jun1999, s.xenitellis@rhbnc.ac.uk */
1824 #define PROMPTSIZE 2048
1825 char prompt_str[PROMPTSIZE]; /* This holds the buffer "smb: \dir1\> " */
1827 char *temp; /* Gets the buffer from readline() */
1828 temp = (char *)NULL;
1830 while (!feof(stdin)) {
1833 #ifdef HAVE_LIBREADLINE
1834 if ( temp != (char *)NULL )
1836 free( temp ); /* Free memory allocated every time by readline() */
1837 temp = (char *)NULL;
1840 snprintf( prompt_str, PROMPTSIZE - 1, "smb: %s> ", cur_dir );
1842 temp = readline( prompt_str ); /* We read the line here */
1845 break; /* EOF occured */
1847 if ( *temp ) /* If non-empty line, save to history */
1850 strncpy( line, temp, 1023 ); /* Maximum size of (pstring)line. Null is guarranteed. */
1852 /* display a prompt */
1853 DEBUG(0,("smb: %s> ", cur_dir));
1858 /* and get a response */
1859 if (!fgets(line,1000,stdin))
1863 /* special case - first char is ! */
1869 /* and get the first part of the command */
1871 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1873 if ((i = process_tok(tok)) >= 0) {
1875 } else if (i == -2) {
1876 DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
1878 DEBUG(0,("%s: command not found\n",tok));
1884 /*****************************************************
1885 return a connection to a server
1886 *******************************************************/
1887 struct cli_state *do_connect(char *server, char *share)
1889 struct cli_state *c;
1890 struct nmb_name called, calling;
1893 extern struct in_addr ipzero;
1894 fstring servicename;
1897 /* make a copy so we don't modify the global string 'service' */
1898 safe_strcpy(servicename, share, sizeof(servicename)-1);
1899 sharename = servicename;
1900 if (*sharename == '\\') {
1901 server = sharename+2;
1902 sharename = strchr(server,'\\');
1903 if (!sharename) return NULL;
1912 make_nmb_name(&calling, global_myname, 0x0);
1913 make_nmb_name(&called , server, name_type);
1917 if (have_ip) ip = dest_ip;
1919 /* have to open a new connection */
1920 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) == 0) ||
1921 !cli_connect(c, server_n, &ip)) {
1922 DEBUG(0,("Connection to %s failed\n", server_n));
1926 if (!cli_session_request(c, &calling, &called)) {
1928 DEBUG(0,("session request to %s failed (%s)\n",
1929 called.name, cli_errstr(c)));
1932 if ((p=strchr(called.name, '.'))) {
1936 if (strcmp(called.name, "*SMBSERVER")) {
1937 make_nmb_name(&called , "*SMBSERVER", 0x20);
1943 DEBUG(4,(" session request ok\n"));
1945 if (!cli_negprot(c)) {
1946 DEBUG(0,("protocol negotiation failed\n"));
1953 char *pass = getpass("Password: ");
1955 pstrcpy(password, pass);
1959 if (!cli_session_setup(c, username,
1960 password, strlen(password),
1961 password, strlen(password),
1963 /* if a password was not supplied then try again with a null username */
1964 if (password[0] || !username[0] ||
1965 !cli_session_setup(c, "", "", 0, "", 0, workgroup)) {
1966 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
1971 DEBUG(0,("Anonymous login successful\n"));
1975 * These next two lines are needed to emulate
1976 * old client behaviour for people who have
1977 * scripts based on client output.
1978 * QUESTION ? Do we want to have a 'client compatibility
1979 * mode to turn these on/off ? JRA.
1982 if (*c->server_domain || *c->server_os || *c->server_type)
1983 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
1984 c->server_domain,c->server_os,c->server_type));
1986 DEBUG(4,(" session setup ok\n"));
1988 if (!cli_send_tconX(c, sharename, "?????",
1989 password, strlen(password)+1)) {
1990 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
1996 DEBUG(4,(" tconx ok\n"));
2002 /****************************************************************************
2003 process commands from the client
2004 ****************************************************************************/
2005 static BOOL process(char *base_directory)
2007 cli = do_connect(desthost, service);
2012 if (*base_directory) do_cd(base_directory);
2015 process_command_string(cmdstr);
2024 /****************************************************************************
2025 usage on the program
2026 ****************************************************************************/
2027 static void usage(char *pname)
2029 DEBUG(0,("Usage: %s service <password> [options]", pname));
2031 DEBUG(0,("\nVersion %s\n",VERSION));
2032 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
2033 DEBUG(0,("\t-O socket_options socket options to use\n"));
2034 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
2035 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
2036 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
2037 DEBUG(0,("\t-N don't ask for a password\n"));
2038 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
2039 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
2040 DEBUG(0,("\t-P connect to service as a printer\n"));
2041 DEBUG(0,("\t-p port connect to the specified port\n"));
2042 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
2043 DEBUG(0,("\t-h Print this help message.\n"));
2044 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
2045 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
2046 DEBUG(0,("\t-U username set the network username\n"));
2047 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
2048 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
2049 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
2050 DEBUG(0,("\t-A filename get the credentials from a file\n"));
2051 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
2052 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
2053 DEBUG(0,("\t-D directory start from directory\n"));
2054 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
2055 DEBUG(0,("\t-b xmit/send buffer changes the transmit/send buffer (default: 65520)\n"));
2060 /****************************************************************************
2061 get a password from a a file or file descriptor
2063 ****************************************************************************/
2064 static void get_password_file(void)
2068 BOOL close_it = False;
2072 if ((p = getenv("PASSWD_FD")) != NULL) {
2073 pstrcpy(spec, "descriptor ");
2075 sscanf(p, "%d", &fd);
2077 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
2078 fd = sys_open(p, O_RDONLY, 0);
2081 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
2082 spec, strerror(errno));
2088 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
2089 p && p - pass < sizeof(pass);) {
2090 switch (read(fd, p, 1)) {
2092 if (*p != '\n' && *p != '\0') {
2093 *++p = '\0'; /* advance p, and null-terminate pass */
2098 *p = '\0'; /* null-terminate it, just in case... */
2099 p = NULL; /* then force the loop condition to become false */
2102 fprintf(stderr, "Error reading password from file %s: %s\n",
2103 spec, "empty password\n");
2108 fprintf(stderr, "Error reading password from file %s: %s\n",
2109 spec, strerror(errno));
2113 pstrcpy(password, pass);
2120 /****************************************************************************
2122 ****************************************************************************/
2123 static int do_host_query(char *query_host)
2125 cli = do_connect(query_host, "IPC$");
2130 list_servers(workgroup);
2138 /****************************************************************************
2139 handle a tar operation
2140 ****************************************************************************/
2141 static int do_tar_op(char *base_directory)
2144 cli = do_connect(desthost, service);
2150 if (*base_directory) do_cd(base_directory);
2159 /****************************************************************************
2160 handle a message operation
2161 ****************************************************************************/
2162 static int do_message_op(void)
2165 struct nmb_name called, calling;
2169 make_nmb_name(&calling, global_myname, 0x0);
2170 make_nmb_name(&called , desthost, name_type);
2173 if (have_ip) ip = dest_ip;
2175 if (!(cli=cli_initialise(NULL)) || !cli_connect(cli, desthost, &ip)) {
2176 DEBUG(0,("Connection to %s failed\n", desthost));
2180 if (!cli_session_request(cli, &calling, &called)) {
2181 DEBUG(0,("session request failed\n"));
2193 /****************************************************************************
2195 ****************************************************************************/
2196 int main(int argc,char *argv[])
2198 fstring base_directory;
2199 char *pname = argv[0];
2202 extern char *optarg;
2205 BOOL message = False;
2206 extern char tar_type;
2207 static pstring servicesf = CONFIGFILE;
2209 pstring new_name_resolve_order;
2213 pstrcpy(term_code, KANJI);
2219 *base_directory = 0;
2221 *new_name_resolve_order = 0;
2225 #ifdef HAVE_LIBREADLINE
2226 /* Allow conditional parsing of the ~/.inputrc file. */
2227 rl_readline_name = "smbclient";
2229 setup_logging(pname,True);
2232 * If the -E option is given, be careful not to clobber stdout
2233 * before processing the options. 28.Feb.99, richard@hacom.nl.
2234 * Also pre-parse the -s option to get the service file name.
2237 for (opt = 1; opt < argc; opt++) {
2238 if (strcmp(argv[opt], "-E") == 0)
2240 else if(strncmp(argv[opt], "-s", 2) == 0) {
2241 if(argv[opt][2] != '\0')
2242 pstrcpy(servicesf, &argv[opt][2]);
2243 else if(argv[opt+1] != NULL) {
2245 * At least one more arg left.
2247 pstrcpy(servicesf, argv[opt+1]);
2256 charset_initialise();
2258 in_client = True; /* Make sure that we tell lp_load we are */
2260 if (!lp_load(servicesf,True,False,False)) {
2261 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
2264 codepage_initialise(lp_client_code_page());
2270 pstrcpy(workgroup,lp_workgroup());
2276 if (getenv("USER")) {
2277 pstrcpy(username,getenv("USER"));
2279 /* modification to support userid%passwd syntax in the USER var
2280 25.Aug.97, jdblair@uab.edu */
2282 if ((p=strchr(username,'%'))) {
2284 pstrcpy(password,p+1);
2286 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
2291 /* modification to support PASSWD environmental var
2292 25.Aug.97, jdblair@uab.edu */
2293 if (getenv("PASSWD")) {
2294 pstrcpy(password,getenv("PASSWD"));
2298 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2299 get_password_file();
2303 if (*username == 0 && getenv("LOGNAME")) {
2304 pstrcpy(username,getenv("LOGNAME"));
2308 if (*username == 0) {
2309 pstrcpy(username,"GUEST");
2317 if (*argv[1] != '-') {
2318 pstrcpy(service,argv[1]);
2319 /* Convert any '/' characters in the service name to '\' characters */
2320 string_replace( service, '/','\\');
2324 if (count_chars(service,'\\') < 3) {
2326 printf("\n%s: Not enough '\\' characters in service\n",service);
2330 if (argc > 1 && (*argv[1] != '-')) {
2332 pstrcpy(password,argv[1]);
2333 memset(argv[1],'X',strlen(argv[1]));
2340 getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:")) != EOF) {
2343 pstrcpy(servicesf, optarg);
2346 pstrcpy(user_socket_options,optarg);
2349 pstrcpy(new_name_resolve_order, optarg);
2352 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2353 pstrcpy(desthost,optarg);
2358 extern pstring global_scope;
2359 pstrcpy(global_scope,optarg);
2360 strupper(global_scope);
2367 pstrcpy(global_myname,optarg);
2373 DEBUGLEVEL = atoi(optarg);
2376 /* not needed anymore */
2379 port = atoi(optarg);
2382 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2390 dest_ip = *interpret_addr2(optarg);
2391 if (zero_ip(dest_ip))
2402 pstrcpy(username,optarg);
2403 if ((lp=strchr(username,'%'))) {
2405 pstrcpy(password,lp+1);
2407 memset(strchr(optarg,'%')+1,'X',strlen(password));
2417 char *ptr, *val, *param;
2419 if ((auth=sys_fopen(optarg, "r")) == NULL)
2421 /* fail if we can't open the credentials file */
2422 DEBUG(0,("ERROR: Unable to open credentials file!\n"));
2428 /* get a line from the file */
2429 if (!fgets (buf, sizeof(buf), auth))
2433 if ((len) && (buf[len-1]=='\n'))
2441 /* break up the line into parameter & value.
2442 will need to eat a little whitespace possibly */
2444 if (!(ptr = strchr (buf, '=')))
2449 /* eat leading white space */
2450 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
2453 if (strwicmp("password", param) == 0)
2455 pstrcpy(password, val);
2458 else if (strwicmp("username", param) == 0)
2459 pstrcpy(username, val);
2461 memset(buf, 0, sizeof(buf));
2469 while(*p == '\\' || *p == '/')
2471 pstrcpy(query_host,p);
2474 pstrcpy(term_code, optarg);
2477 /* no longer supported */
2480 pstrcpy(workgroup,optarg);
2483 if (!tar_parseargs(argc, argv, optarg, optind)) {
2489 pstrcpy(base_directory,optarg);
2496 io_bufsize = MAX(1, atoi(optarg));
2504 get_myname((*global_myname)?NULL:global_myname);
2506 if(*new_name_resolve_order)
2507 lp_set_name_resolve_order(new_name_resolve_order);
2510 interpret_coding_system(term_code);
2512 if (!tar_type && !*query_host && !*service && !message) {
2517 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2521 process_command_string(cmdstr);
2522 return do_tar_op(base_directory);
2525 if ((p=strchr(query_host,'#'))) {
2528 sscanf(p, "%x", &name_type);
2532 return do_host_query(query_host);
2536 return do_message_op();
2539 if (!process(base_directory)) {