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;
47 static int name_type = 0x20;
49 extern pstring user_socket_options;
51 static int process_tok(fstring tok);
52 static void cmd_help(void);
54 /* 30 second timeout on most commands */
55 #define CLIENT_TIMEOUT (30*1000)
56 #define SHORT_TIMEOUT (5*1000)
58 /* value for unused fid field in trans2 secondary request */
59 #define FID_UNUSED (0xFFFF)
61 time_t newer_than = 0;
62 int archive_level = 0;
64 extern pstring debugf;
65 extern int DEBUGLEVEL;
67 BOOL translation = False;
71 /* clitar bits insert */
74 extern BOOL tar_reset;
78 mode_t myumask = 0755;
84 static BOOL recurse = False;
85 BOOL lowercase = False;
87 struct in_addr dest_ip;
89 #define SEPARATORS " \t\n\r"
91 BOOL abort_mget = True;
93 pstring fileselection = "";
95 extern file_info def_finfo;
98 int get_total_size = 0;
99 int get_total_time_ms = 0;
100 int put_total_size = 0;
101 int put_total_time_ms = 0;
104 static double dir_total;
108 /****************************************************************************
109 write to a local file with CR/LF->LF translation if appropriate. return the
110 number taken from the buffer. This may not equal the number written.
111 ****************************************************************************/
112 static int writefile(int f, char *b, int n)
122 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
125 if (write(f, b, 1) != 1) {
135 /****************************************************************************
136 read from a file with LF->CR/LF translation if appropriate. return the
137 number read. read approx n bytes.
138 ****************************************************************************/
139 static int readfile(char *b, int size, int n, FILE *f)
144 if (!translation || (size != 1))
145 return(fread(b,size,n,f));
148 while (i < (n - 1) && (i < BUFFER_SIZE)) {
149 if ((c = getc(f)) == EOF) {
153 if (c == '\n') { /* change all LFs to CR/LF */
164 /****************************************************************************
166 ****************************************************************************/
167 static void send_message(void)
172 if (!cli_message_start(cli, desthost, username, &grp_id)) {
173 DEBUG(0,("message start: %s\n", cli_errstr(cli)));
178 printf("Connected. Type your message, ending it with a Control-D\n");
180 while (!feof(stdin) && total_len < 1600) {
181 int maxlen = MIN(1600 - total_len,127);
188 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
194 if (!cli_message_text(cli, msg, l, grp_id)) {
195 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
202 if (total_len >= 1600)
203 printf("the message was truncated to 1600 bytes\n");
205 printf("sent %d bytes\n",total_len);
207 if (!cli_message_end(cli, grp_id)) {
208 printf("SMBsendend failed (%s)\n",cli_errstr(cli));
215 /****************************************************************************
216 check the space on a device
217 ****************************************************************************/
218 static void do_dskattr(void)
220 int total, bsize, avail;
222 if (!cli_dskattr(cli, &bsize, &total, &avail)) {
223 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli)));
227 DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
228 total, bsize, avail));
231 /****************************************************************************
233 ****************************************************************************/
234 static void cmd_pwd(void)
236 DEBUG(0,("Current directory is %s",service));
237 DEBUG(0,("%s\n",cur_dir));
241 /****************************************************************************
242 change directory - inner section
243 ****************************************************************************/
244 static void do_cd(char *newdir)
252 /* Save the current directory in case the
253 new directory is invalid */
254 pstrcpy(saved_dir, cur_dir);
259 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
260 pstrcat(cur_dir, "\\");
262 dos_clean_name(cur_dir);
263 pstrcpy(dname,cur_dir);
264 pstrcat(cur_dir,"\\");
265 dos_clean_name(cur_dir);
267 if (!strequal(cur_dir,"\\")) {
268 if (!cli_chkpath(cli, dname)) {
269 DEBUG(0,("cd %s: %s\n", dname, cli_errstr(cli)));
270 pstrcpy(cur_dir,saved_dir);
274 pstrcpy(cd_path,cur_dir);
277 /****************************************************************************
279 ****************************************************************************/
280 static void cmd_cd(void)
284 if (next_token(NULL,buf,NULL,sizeof(buf)))
287 DEBUG(0,("Current directory is %s\n",cur_dir));
291 /*******************************************************************
292 decide if a file should be operated on
293 ********************************************************************/
294 static BOOL do_this_one(file_info *finfo)
296 if (finfo->mode & aDIR) return(True);
298 if (*fileselection &&
299 !mask_match(finfo->name,fileselection,False,False)) {
300 DEBUG(3,("match_match %s failed\n", finfo->name));
304 if (newer_than && finfo->mtime < newer_than) {
305 DEBUG(3,("newer_than %s failed\n", finfo->name));
309 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
310 DEBUG(3,("archive %s failed\n", finfo->name));
317 /****************************************************************************
318 display info about a file
319 ****************************************************************************/
320 static void display_finfo(file_info *finfo)
322 if (do_this_one(finfo)) {
323 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
324 DEBUG(0,(" %-30s%7.7s %8.0f %s",
326 attrib_string(finfo->mode),
328 asctime(LocalTime(&t))));
329 dir_total += finfo->size;
334 /****************************************************************************
335 accumulate size of a file
336 ****************************************************************************/
337 static void do_du(file_info *finfo)
339 if (do_this_one(finfo)) {
340 dir_total += finfo->size;
344 static BOOL do_list_recurse;
345 static BOOL do_list_dirs;
346 static char *do_list_queue = 0;
347 static long do_list_queue_size = 0;
348 static long do_list_queue_start = 0;
349 static long do_list_queue_end = 0;
350 static void (*do_list_fn)(file_info *);
352 /****************************************************************************
353 functions for do_list_queue
354 ****************************************************************************/
357 * The do_list_queue is a NUL-separated list of strings stored in a
358 * char*. Since this is a FIFO, we keep track of the beginning and
359 * ending locations of the data in the queue. When we overflow, we
360 * double the size of the char*. When the start of the data passes
361 * the midpoint, we move everything back. This is logically more
362 * complex than a linked list, but easier from a memory management
363 * angle. In any memory error condition, do_list_queue is reset.
364 * Functions check to ensure that do_list_queue is non-NULL before
367 static void reset_do_list_queue(void)
374 do_list_queue_size = 0;
375 do_list_queue_start = 0;
376 do_list_queue_end = 0;
379 static void init_do_list_queue(void)
381 reset_do_list_queue();
382 do_list_queue_size = 1024;
383 do_list_queue = malloc(do_list_queue_size);
384 if (do_list_queue == 0) {
385 DEBUG(0,("malloc fail for size %d\n",
386 (int)do_list_queue_size));
387 reset_do_list_queue();
389 memset(do_list_queue, 0, do_list_queue_size);
393 static void adjust_do_list_queue(void)
396 * If the starting point of the queue is more than half way through,
397 * move everything toward the beginning.
399 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
401 DEBUG(4,("do_list_queue is empty\n"));
402 do_list_queue_start = do_list_queue_end = 0;
403 *do_list_queue = '\0';
405 else if (do_list_queue_start > (do_list_queue_size / 2))
407 DEBUG(4,("sliding do_list_queue backward\n"));
408 memmove(do_list_queue,
409 do_list_queue + do_list_queue_start,
410 do_list_queue_end - do_list_queue_start);
411 do_list_queue_end -= do_list_queue_start;
412 do_list_queue_start = 0;
417 static void add_to_do_list_queue(const char* entry)
419 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
420 while (new_end > do_list_queue_size)
422 do_list_queue_size *= 2;
423 DEBUG(4,("enlarging do_list_queue to %d\n",
424 (int)do_list_queue_size));
425 do_list_queue = Realloc(do_list_queue, do_list_queue_size);
426 if (! do_list_queue) {
427 DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
428 (int)do_list_queue_size));
429 reset_do_list_queue();
433 memset(do_list_queue + do_list_queue_size / 2,
434 0, do_list_queue_size / 2);
439 pstrcpy(do_list_queue + do_list_queue_end, entry);
440 do_list_queue_end = new_end;
441 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
442 entry, (int)do_list_queue_start, (int)do_list_queue_end));
446 static char *do_list_queue_head(void)
448 return do_list_queue + do_list_queue_start;
451 static void remove_do_list_queue_head(void)
453 if (do_list_queue_end > do_list_queue_start)
455 do_list_queue_start += strlen(do_list_queue_head()) + 1;
456 adjust_do_list_queue();
457 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
458 (int)do_list_queue_start, (int)do_list_queue_end));
462 static int do_list_queue_empty(void)
464 return (! (do_list_queue && *do_list_queue));
467 /****************************************************************************
469 ****************************************************************************/
470 static void do_list_helper(file_info *f, const char *mask)
472 if (f->mode & aDIR) {
473 if (do_list_dirs && do_this_one(f)) {
476 if (do_list_recurse &&
477 !strequal(f->name,".") &&
478 !strequal(f->name,"..")) {
482 pstrcpy(mask2, mask);
483 p = strrchr(mask2,'\\');
486 pstrcat(mask2, f->name);
487 pstrcat(mask2,"\\*");
488 add_to_do_list_queue(mask2);
493 if (do_this_one(f)) {
499 /****************************************************************************
500 a wrapper around cli_list that adds recursion
501 ****************************************************************************/
502 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
504 static int in_do_list = 0;
506 if (in_do_list && rec)
508 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
514 do_list_recurse = rec;
520 init_do_list_queue();
521 add_to_do_list_queue(mask);
523 while (! do_list_queue_empty())
526 * Need to copy head so that it doesn't become
527 * invalid inside the call to cli_list. This
528 * would happen if the list were expanded
530 * Fix from E. Jay Berkenbilt (ejb@ql.org)
533 pstrcpy(head, do_list_queue_head());
534 cli_list(cli, head, attribute, do_list_helper);
535 remove_do_list_queue_head();
536 if ((! do_list_queue_empty()) && (fn == display_finfo))
538 char* next_file = do_list_queue_head();
540 if ((strlen(next_file) >= 2) &&
541 (next_file[strlen(next_file) - 1] == '*') &&
542 (next_file[strlen(next_file) - 2] == '\\'))
544 save_ch = next_file +
545 strlen(next_file) - 2;
548 DEBUG(0,("\n%s\n",next_file));
558 if (cli_list(cli, mask, attribute, do_list_helper) == -1)
560 DEBUG(0, ("%s listing %s\n", cli_errstr(cli), mask));
565 reset_do_list_queue();
568 /****************************************************************************
569 get a directory listing
570 ****************************************************************************/
571 static void cmd_dir(void)
573 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
579 pstrcpy(mask,cur_dir);
580 if(mask[strlen(mask)-1]!='\\')
583 if (next_token(NULL,buf,NULL,sizeof(buf))) {
594 do_list(mask, attribute, display_finfo, recurse, True);
598 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
602 /****************************************************************************
603 get a directory listing
604 ****************************************************************************/
605 static void cmd_du(void)
607 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
613 pstrcpy(mask,cur_dir);
614 if(mask[strlen(mask)-1]!='\\')
617 if (next_token(NULL,buf,NULL,sizeof(buf))) {
627 do_list(mask, attribute, do_du, recurse, True);
631 DEBUG(0, ("Total number of bytes: %.0f\n", dir_total));
635 /****************************************************************************
636 get a file from rname to lname
637 ****************************************************************************/
638 static void do_get(char *rname,char *lname)
641 BOOL newhandle = False;
643 struct timeval tp_start;
644 int read_size = io_bufsize;
649 GetTimeOfDay(&tp_start);
655 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
658 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
662 if(!strcmp(lname,"-")) {
663 handle = fileno(stdout);
665 handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
669 DEBUG(0,("Error opening local file %s\n",lname));
674 if (!cli_qfileinfo(cli, fnum,
675 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
676 !cli_getattrE(cli, fnum,
677 &attr, &size, NULL, NULL, NULL)) {
678 DEBUG(0,("getattrib: %s\n",cli_errstr(cli)));
682 DEBUG(2,("getting file %s of size %.0f as %s ",
683 lname, (double)size, lname));
685 if(!(data = (char *)malloc(read_size))) {
686 DEBUG(0,("malloc fail for size %d\n", read_size));
687 cli_close(cli, fnum);
692 int n = cli_read(cli, fnum, data, nread, read_size);
696 if (writefile(handle,data, n) != n) {
697 DEBUG(0,("Error writing local file\n"));
705 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
706 rname, (long)nread));
711 if (!cli_close(cli, fnum)) {
712 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
719 if (archive_level >= 2 && (attr & aARCH)) {
720 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
724 struct timeval tp_end;
727 GetTimeOfDay(&tp_end);
729 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
730 (tp_end.tv_usec - tp_start.tv_usec)/1000;
731 get_total_time_ms += this_time;
732 get_total_size += nread;
734 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
735 nread / (1.024*this_time + 1.0e-4),
736 get_total_size / (1.024*get_total_time_ms)));
741 /****************************************************************************
743 ****************************************************************************/
744 static void cmd_get(void)
750 pstrcpy(rname,cur_dir);
753 p = rname + strlen(rname);
755 if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
756 DEBUG(0,("get <filename>\n"));
760 dos_clean_name(rname);
762 next_token(NULL,lname,NULL,sizeof(lname));
764 do_get(rname, lname);
768 /****************************************************************************
769 do a mget operation on one file
770 ****************************************************************************/
771 static void do_mget(file_info *finfo)
775 pstring saved_curdir;
778 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
782 DEBUG(0,("mget aborted\n"));
786 if (finfo->mode & aDIR)
787 slprintf(quest,sizeof(pstring)-1,
788 "Get directory %s? ",finfo->name);
790 slprintf(quest,sizeof(pstring)-1,
791 "Get file %s? ",finfo->name);
793 if (prompt && !yesno(quest)) return;
795 if (!(finfo->mode & aDIR)) {
796 pstrcpy(rname,cur_dir);
797 pstrcat(rname,finfo->name);
798 do_get(rname,finfo->name);
802 /* handle directories */
803 pstrcpy(saved_curdir,cur_dir);
805 pstrcat(cur_dir,finfo->name);
806 pstrcat(cur_dir,"\\");
808 unix_format(finfo->name);
810 strlower(finfo->name);
812 if (!dos_directory_exist(finfo->name,NULL) &&
813 dos_mkdir(finfo->name,0777) != 0) {
814 DEBUG(0,("failed to create directory %s\n",finfo->name));
815 pstrcpy(cur_dir,saved_curdir);
819 if (dos_chdir(finfo->name) != 0) {
820 DEBUG(0,("failed to chdir to directory %s\n",finfo->name));
821 pstrcpy(cur_dir,saved_curdir);
825 pstrcpy(mget_mask,cur_dir);
826 pstrcat(mget_mask,"*");
828 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
830 pstrcpy(cur_dir,saved_curdir);
834 /****************************************************************************
835 view the file using the pager
836 ****************************************************************************/
837 static void cmd_more(void)
839 fstring rname,lname,tmpname,pager_cmd;
842 fstrcpy(rname,cur_dir);
846 "%s/smbmore.%d",tmpdir(),(int)getpid());
847 fstrcpy(lname,tmpname);
849 if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
850 DEBUG(0,("more <filename>\n"));
853 dos_clean_name(rname);
857 pager=getenv("PAGER");
859 slprintf(pager_cmd,sizeof(pager_cmd)-1,
860 "%s %s",(pager? pager:PAGER), tmpname);
867 /****************************************************************************
869 ****************************************************************************/
870 static void cmd_mget(void)
872 uint16 attribute = aSYSTEM | aHIDDEN;
884 while (next_token(NULL,p,NULL,sizeof(buf))) {
885 pstrcpy(mget_mask,cur_dir);
886 if(mget_mask[strlen(mget_mask)-1]!='\\')
887 pstrcat(mget_mask,"\\");
890 pstrcpy(mget_mask,p);
892 pstrcat(mget_mask,p);
893 do_list(mget_mask, attribute,do_mget,False,True);
897 pstrcpy(mget_mask,cur_dir);
898 if(mget_mask[strlen(mget_mask)-1]!='\\')
899 pstrcat(mget_mask,"\\");
900 pstrcat(mget_mask,"*");
901 do_list(mget_mask, attribute,do_mget,False,True);
906 /****************************************************************************
907 make a directory of name "name"
908 ****************************************************************************/
909 static BOOL do_mkdir(char *name)
911 if (!cli_mkdir(cli, name)) {
912 DEBUG(0,("%s making remote directory %s\n",
913 cli_errstr(cli),name));
921 /****************************************************************************
923 ****************************************************************************/
924 static void cmd_quit(void)
931 /****************************************************************************
933 ****************************************************************************/
934 static void cmd_mkdir(void)
940 pstrcpy(mask,cur_dir);
942 if (!next_token(NULL,p,NULL,sizeof(buf))) {
944 DEBUG(0,("mkdir <dirname>\n"));
955 trim_string(ddir,".",NULL);
956 p = strtok(ddir,"/\\");
959 if (!cli_chkpath(cli, ddir2)) {
963 p = strtok(NULL,"/\\");
971 /****************************************************************************
973 ****************************************************************************/
974 static void do_put(char *rname,char *lname)
980 int maxwrite=io_bufsize;
982 struct timeval tp_start;
983 GetTimeOfDay(&tp_start);
985 fnum = cli_open(cli, rname, O_WRONLY|O_CREAT|O_TRUNC, DENY_NONE);
988 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
992 /* allow files to be piped into smbclient
994 if (!strcmp(lname, "-")) {
996 /* size of file is not known */
998 f = sys_fopen(lname,"r");
1002 DEBUG(0,("Error opening local file %s\n",lname));
1007 DEBUG(1,("putting file %s as %s ",lname,
1010 buf = (char *)malloc(maxwrite);
1015 if ((n = readfile(buf,1,n,f)) < 1) {
1016 DEBUG(0,("Error reading local file\n"));
1020 ret = cli_write(cli, fnum, 0, buf, nread, n);
1023 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
1030 if (!cli_close(cli, fnum)) {
1031 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),rname));
1042 struct timeval tp_end;
1045 GetTimeOfDay(&tp_end);
1047 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1048 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1049 put_total_time_ms += this_time;
1050 put_total_size += nread;
1052 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1053 nread / (1.024*this_time + 1.0e-4),
1054 put_total_size / (1.024*put_total_time_ms)));
1065 /****************************************************************************
1067 ****************************************************************************/
1068 static void cmd_put(void)
1075 pstrcpy(rname,cur_dir);
1076 pstrcat(rname,"\\");
1078 if (!next_token(NULL,p,NULL,sizeof(buf))) {
1079 DEBUG(0,("put <filename>\n"));
1084 if (next_token(NULL,p,NULL,sizeof(buf)))
1087 pstrcat(rname,lname);
1089 dos_clean_name(rname);
1093 /* allow '-' to represent stdin
1094 jdblair, 24.jun.98 */
1095 if (!file_exist(lname,&st) &&
1096 (strcmp(lname,"-"))) {
1097 DEBUG(0,("%s does not exist\n",lname));
1102 do_put(rname,lname);
1106 /****************************************************************************
1107 seek in a directory/file list until you get something that doesn't start with
1109 ****************************************************************************/
1110 static BOOL seek_list(FILE *f,char *name)
1114 if (fscanf(f,"%s",s) != 1) return(False);
1115 trim_string(s,"./",NULL);
1116 if (strncmp(s,name,strlen(name)) != 0) {
1126 /****************************************************************************
1127 set the file selection mask
1128 ****************************************************************************/
1129 static void cmd_select(void)
1131 pstrcpy(fileselection,"");
1132 next_token(NULL,fileselection,NULL,sizeof(fileselection));
1136 /****************************************************************************
1138 ****************************************************************************/
1139 static void cmd_mput(void)
1146 while (next_token(NULL,p,NULL,sizeof(buf))) {
1152 slprintf(tmpname,sizeof(pstring)-1,
1153 "%s/ls.smb.%d",tmpdir(),(int)getpid());
1155 slprintf(cmd,sizeof(pstring)-1,
1156 "find . -name \"%s\" -print > %s",p,tmpname);
1158 slprintf(cmd,sizeof(pstring)-1,
1159 "/bin/ls %s > %s",p,tmpname);
1162 f = sys_fopen(tmpname,"r");
1168 if (fscanf(f,"%s",lname) != 1) break;
1169 trim_string(lname,"./",NULL);
1173 /* check if it's a directory */
1174 if (directory_exist(lname,&st)) {
1175 if (!recurse) continue;
1176 slprintf(quest,sizeof(pstring)-1,
1177 "Put directory %s? ",lname);
1178 if (prompt && !yesno(quest)) {
1180 if (!seek_list(f,lname))
1185 pstrcpy(rname,cur_dir);
1186 pstrcat(rname,lname);
1187 if (!cli_chkpath(cli, rname) && !do_mkdir(rname)) {
1189 if (!seek_list(f,lname))
1195 slprintf(quest,sizeof(quest)-1,
1196 "Put file %s? ",lname);
1197 if (prompt && !yesno(quest)) continue;
1199 pstrcpy(rname,cur_dir);
1200 pstrcat(rname,lname);
1205 do_put(rname,lname);
1213 /****************************************************************************
1215 ****************************************************************************/
1216 static void do_cancel(int job)
1218 if (cli_printjob_del(cli, job)) {
1219 printf("Job %d cancelled\n",job);
1221 printf("Error calcelling job %d : %s\n",job,cli_errstr(cli));
1226 /****************************************************************************
1228 ****************************************************************************/
1229 static void cmd_cancel(void)
1234 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1235 printf("cancel <jobid> ...\n");
1241 } while (next_token(NULL,buf,NULL,sizeof(buf)));
1245 /****************************************************************************
1247 ****************************************************************************/
1248 static void cmd_print(void)
1254 if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1255 DEBUG(0,("print <filename>\n"));
1259 pstrcpy(rname,lname);
1260 p = strrchr(rname,'/');
1262 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1265 if (strequal(lname,"-")) {
1266 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1269 do_put(rname, lname);
1273 /****************************************************************************
1274 show a print queue entry
1275 ****************************************************************************/
1276 static void queue_fn(struct print_job_info *p)
1278 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1281 /****************************************************************************
1283 ****************************************************************************/
1284 static void cmd_queue(void)
1286 cli_print_queue(cli, queue_fn);
1289 /****************************************************************************
1291 ****************************************************************************/
1292 static void do_del(file_info *finfo)
1296 pstrcpy(mask,cur_dir);
1297 pstrcat(mask,finfo->name);
1299 if (finfo->mode & aDIR)
1302 if (!cli_unlink(cli, mask)) {
1303 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),mask));
1307 /****************************************************************************
1309 ****************************************************************************/
1310 static void cmd_del(void)
1314 uint16 attribute = aSYSTEM | aHIDDEN;
1319 pstrcpy(mask,cur_dir);
1321 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1322 DEBUG(0,("del <filename>\n"));
1327 do_list(mask, attribute,do_del,False,False);
1330 /****************************************************************************
1331 ****************************************************************************/
1332 static void cmd_open(void)
1337 pstrcpy(mask,cur_dir);
1339 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1340 DEBUG(0,("del <filename>\n"));
1345 cli_open(cli, mask, O_RDWR, DENY_ALL);
1349 /****************************************************************************
1351 ****************************************************************************/
1352 static void cmd_rmdir(void)
1357 pstrcpy(mask,cur_dir);
1359 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1360 DEBUG(0,("rmdir <dirname>\n"));
1365 if (!cli_rmdir(cli, mask)) {
1366 DEBUG(0,("%s removing remote directory file %s\n",
1367 cli_errstr(cli),mask));
1371 /****************************************************************************
1373 ****************************************************************************/
1374 static void cmd_rename(void)
1379 pstrcpy(src,cur_dir);
1380 pstrcpy(dest,cur_dir);
1382 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1383 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1384 DEBUG(0,("rename <src> <dest>\n"));
1391 if (!cli_rename(cli, src, dest)) {
1392 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1398 /****************************************************************************
1399 toggle the prompt flag
1400 ****************************************************************************/
1401 static void cmd_prompt(void)
1404 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1408 /****************************************************************************
1409 set the newer than time
1410 ****************************************************************************/
1411 static void cmd_newer(void)
1415 SMB_STRUCT_STAT sbuf;
1417 ok = next_token(NULL,buf,NULL,sizeof(buf));
1418 if (ok && (dos_stat(buf,&sbuf) == 0)) {
1419 newer_than = sbuf.st_mtime;
1420 DEBUG(1,("Getting files newer than %s",
1421 asctime(LocalTime(&newer_than))));
1426 if (ok && newer_than == 0)
1427 DEBUG(0,("Error setting newer-than time\n"));
1430 /****************************************************************************
1431 set the archive level
1432 ****************************************************************************/
1433 static void cmd_archive(void)
1437 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1438 archive_level = atoi(buf);
1440 DEBUG(0,("Archive level is %d\n",archive_level));
1443 /****************************************************************************
1444 toggle the lowercaseflag
1445 ****************************************************************************/
1446 static void cmd_lowercase(void)
1448 lowercase = !lowercase;
1449 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1455 /****************************************************************************
1456 toggle the recurse flag
1457 ****************************************************************************/
1458 static void cmd_recurse(void)
1461 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1464 /****************************************************************************
1465 toggle the translate flag
1466 ****************************************************************************/
1467 static void cmd_translate(void)
1469 translation = !translation;
1470 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1471 translation?"on":"off"));
1475 /****************************************************************************
1476 do a printmode command
1477 ****************************************************************************/
1478 static void cmd_printmode(void)
1483 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1484 if (strequal(buf,"text")) {
1487 if (strequal(buf,"graphics"))
1490 printmode = atoi(buf);
1497 fstrcpy(mode,"text");
1500 fstrcpy(mode,"graphics");
1503 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1507 DEBUG(2,("the printmode is now %s\n",mode));
1510 /****************************************************************************
1512 ****************************************************************************/
1513 static void cmd_lcd(void)
1518 if (next_token(NULL,buf,NULL,sizeof(buf)))
1520 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
1523 /****************************************************************************
1525 ****************************************************************************/
1526 static void browse_fn(const char *name, uint32 m, const char *comment)
1534 case STYPE_DISKTREE:
1535 fstrcpy(typestr,"Disk"); break;
1537 fstrcpy(typestr,"Printer"); break;
1539 fstrcpy(typestr,"Device"); break;
1541 fstrcpy(typestr,"IPC"); break;
1544 printf("\t%-15.15s%-10.10s%s\n",
1545 name, typestr,comment);
1549 /****************************************************************************
1550 try and browse available connections on a host
1551 ****************************************************************************/
1552 static BOOL browse_host(BOOL sort)
1554 printf("\n\tSharename Type Comment\n");
1555 printf("\t--------- ---- -------\n");
1557 return cli_RNetShareEnum(cli, browse_fn);
1560 /****************************************************************************
1562 ****************************************************************************/
1563 static void server_fn(const char *name, uint32 m, const char *comment)
1565 printf("\t%-16.16s %s\n", name, comment);
1568 /****************************************************************************
1569 try and browse available connections on a host
1570 ****************************************************************************/
1571 static BOOL list_servers(char *wk_grp)
1573 if (!cli->server_domain) return False;
1575 printf("\n\tServer Comment\n");
1576 printf("\t--------- -------\n");
1578 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn);
1580 printf("\n\tWorkgroup Master\n");
1581 printf("\t--------- -------\n");
1583 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM, server_fn);
1587 #if defined(HAVE_LIBREADLINE)
1588 # if defined(HAVE_READLINE_HISTORY_H) || defined(HAVE_HISTORY_H)
1589 /****************************************************************************
1591 ****************************************************************************/
1592 static void cmd_history(void)
1597 hlist = history_list (); /* Get pointer to history list */
1599 if (hlist) /* If list not empty */
1601 for (i = 0; hlist[i]; i++) /* then display it */
1602 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
1608 /* Some constants for completing filename arguments */
1610 #define COMPL_NONE 0 /* No completions */
1611 #define COMPL_REMOTE 1 /* Complete remote filename */
1612 #define COMPL_LOCAL 2 /* Complete local filename */
1614 /* This defines the commands supported by this client */
1620 char compl_args[2]; /* Completion argument info */
1623 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1624 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1625 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1626 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1627 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1628 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1629 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1630 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1631 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1632 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1633 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1634 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1635 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1636 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1637 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
1638 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1639 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1640 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1641 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1642 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1643 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1644 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1645 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1646 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1647 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1648 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1649 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1650 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1651 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1652 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1653 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1654 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1655 {"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}},
1656 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1657 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1658 {"tarmode",cmd_tarmode,
1659 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1660 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1661 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1662 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1663 #ifdef HAVE_LIBREADLINE
1664 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
1666 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1667 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1671 /*******************************************************************
1672 lookup a command string in the list of commands, including
1674 ******************************************************************/
1675 static int process_tok(fstring tok)
1677 int i = 0, matches = 0;
1679 int tok_len = strlen(tok);
1681 while (commands[i].fn != NULL) {
1682 if (strequal(commands[i].name,tok)) {
1686 } else if (strnequal(commands[i].name, tok, tok_len)) {
1695 else if (matches == 1)
1701 /****************************************************************************
1703 ****************************************************************************/
1704 static void cmd_help(void)
1709 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1710 if ((i = process_tok(buf)) >= 0)
1711 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1713 while (commands[i].description) {
1714 for (j=0; commands[i].description && (j<5); j++) {
1715 DEBUG(0,("%-15s",commands[i].name));
1723 #ifndef HAVE_LIBREADLINE
1724 /****************************************************************************
1725 wait for keyboard activity, swallowing network packets
1726 ****************************************************************************/
1727 static void wait_keyboard(void)
1730 struct timeval timeout;
1734 FD_SET(cli->fd,&fds);
1735 FD_SET(fileno(stdin),&fds);
1737 timeout.tv_sec = 20;
1738 timeout.tv_usec = 0;
1739 sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
1741 if (FD_ISSET(fileno(stdin),&fds))
1744 /* We deliberately use receive_smb instead of
1745 client_receive_smb as we want to receive
1746 session keepalives and then drop them here.
1748 if (FD_ISSET(cli->fd,&fds))
1749 receive_smb(cli->fd,cli->inbuf,0);
1751 cli_chkpath(cli, "\\");
1756 /****************************************************************************
1757 process a -c command string
1758 ****************************************************************************/
1759 static void process_command_string(char *cmd)
1764 while (cmd[0] != '\0') {
1769 if ((p = strchr(cmd, ';')) == 0) {
1770 strncpy(line, cmd, 999);
1774 if (p - cmd > 999) p = cmd + 999;
1775 strncpy(line, cmd, p - cmd);
1776 line[p - cmd] = '\0';
1780 /* and get the first part of the command */
1782 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1784 if ((i = process_tok(tok)) >= 0) {
1786 } else if (i == -2) {
1787 DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
1789 DEBUG(0,("%s: command not found\n",tok));
1794 /****************************************************************************
1795 process commands on stdin
1796 ****************************************************************************/
1797 static void process_stdin(void)
1802 #ifdef HAVE_LIBREADLINE
1803 /* Minimal readline support, 29Jun1999, s.xenitellis@rhbnc.ac.uk */
1807 #define PROMPTSIZE 2048
1808 char prompt_str[PROMPTSIZE]; /* This holds the buffer "smb: \dir1\> " */
1810 char *temp; /* Gets the buffer from readline() */
1811 temp = (char *)NULL;
1813 while (!feof(stdin)) {
1816 #ifdef HAVE_LIBREADLINE
1817 if ( temp != (char *)NULL )
1819 free( temp ); /* Free memory allocated every time by readline() */
1820 temp = (char *)NULL;
1823 snprintf( prompt_str, PROMPTSIZE - 1, "smb: %s> ", cur_dir );
1825 temp = readline( prompt_str ); /* We read the line here */
1828 break; /* EOF occured */
1830 if ( *temp ) /* If non-empty line, save to history */
1833 strncpy( line, temp, 1023 ); /* Maximum size of (pstring)line. Null is guarranteed. */
1835 /* display a prompt */
1836 DEBUG(0,("smb: %s> ", cur_dir));
1841 /* and get a response */
1842 if (!fgets(line,1000,stdin))
1846 /* special case - first char is ! */
1852 /* and get the first part of the command */
1854 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1856 if ((i = process_tok(tok)) >= 0) {
1858 } else if (i == -2) {
1859 DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
1861 DEBUG(0,("%s: command not found\n",tok));
1867 /*****************************************************
1868 return a connection to a server
1869 *******************************************************/
1870 struct cli_state *do_connect(char *server, char *share)
1872 struct cli_state *c;
1873 struct nmb_name called, calling;
1876 extern struct in_addr ipzero;
1878 if (*share == '\\') {
1880 share = strchr(server,'\\');
1881 if (!share) return NULL;
1890 make_nmb_name(&calling, global_myname, 0x0, "");
1891 make_nmb_name(&called , server, name_type, "");
1895 if (have_ip) ip = dest_ip;
1897 /* have to open a new connection */
1898 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) == 0) ||
1899 !cli_connect(c, server_n, &ip)) {
1900 DEBUG(0,("Connection to %s failed\n", server_n));
1904 if (!cli_session_request(c, &calling, &called)) {
1906 DEBUG(0,("session request to %s failed (%s)\n",
1907 called.name, cli_errstr(c)));
1909 if ((p=strchr(called.name, '.'))) {
1913 if (strcmp(called.name, "*SMBSERVER")) {
1914 make_nmb_name(&called , "*SMBSERVER", 0x20, "");
1920 DEBUG(4,(" session request ok\n"));
1922 if (!cli_negprot(c)) {
1923 DEBUG(0,("protocol negotiation failed\n"));
1929 char *pass = getpass("Password: ");
1931 pstrcpy(password, pass);
1935 if (!cli_session_setup(c, username,
1936 password, strlen(password),
1937 password, strlen(password),
1939 /* if a password was not supplied then try again with a null username */
1940 if (password[0] || !username[0] ||
1941 !cli_session_setup(c, "", "", 0, "", 0, workgroup)) {
1942 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
1945 DEBUG(0,("Anonymous login successful\n"));
1949 * These next two lines are needed to emulate
1950 * old client behaviour for people who have
1951 * scripts based on client output.
1952 * QUESTION ? Do we want to have a 'client compatibility
1953 * mode to turn these on/off ? JRA.
1956 if (*c->server_domain || *c->server_os || *c->server_type)
1957 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
1958 c->server_domain,c->server_os,c->server_type));
1960 DEBUG(4,(" session setup ok\n"));
1962 if (!cli_send_tconX(c, share, "?????",
1963 password, strlen(password)+1)) {
1964 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
1969 DEBUG(4,(" tconx ok\n"));
1975 /****************************************************************************
1976 process commands from the client
1977 ****************************************************************************/
1978 static BOOL process(char *base_directory)
1980 cli = do_connect(desthost, service);
1985 if (*base_directory) do_cd(base_directory);
1988 process_command_string(cmdstr);
1997 /****************************************************************************
1998 usage on the program
1999 ****************************************************************************/
2000 static void usage(char *pname)
2002 DEBUG(0,("Usage: %s service <password> [options]", pname));
2004 DEBUG(0,("\nVersion %s\n",VERSION));
2005 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
2006 DEBUG(0,("\t-O socket_options socket options to use\n"));
2007 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
2008 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
2009 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
2010 DEBUG(0,("\t-N don't ask for a password\n"));
2011 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
2012 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
2013 DEBUG(0,("\t-P connect to service as a printer\n"));
2014 DEBUG(0,("\t-p port connect to the specified port\n"));
2015 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
2016 DEBUG(0,("\t-h Print this help message.\n"));
2017 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
2018 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
2019 DEBUG(0,("\t-U username set the network username\n"));
2020 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
2021 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
2022 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
2023 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
2024 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
2025 DEBUG(0,("\t-D directory start from directory\n"));
2026 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
2027 DEBUG(0,("\t-b xmit/send buffer changes the transmit/send buffer (default: 65520)\n"));
2032 /****************************************************************************
2033 get a password from a a file or file descriptor
2035 ****************************************************************************/
2036 static void get_password_file(void)
2040 BOOL close_it = False;
2044 if ((p = getenv("PASSWD_FD")) != NULL) {
2045 pstrcpy(spec, "descriptor ");
2047 sscanf(p, "%d", &fd);
2049 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
2050 fd = sys_open(p, O_RDONLY, 0);
2053 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
2054 spec, strerror(errno));
2060 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
2061 p && p - pass < sizeof(pass);) {
2062 switch (read(fd, p, 1)) {
2064 if (*p != '\n' && *p != '\0') {
2065 *++p = '\0'; /* advance p, and null-terminate pass */
2070 *p = '\0'; /* null-terminate it, just in case... */
2071 p = NULL; /* then force the loop condition to become false */
2074 fprintf(stderr, "Error reading password from file %s: %s\n",
2075 spec, "empty password\n");
2080 fprintf(stderr, "Error reading password from file %s: %s\n",
2081 spec, strerror(errno));
2085 pstrcpy(password, pass);
2092 /****************************************************************************
2094 ****************************************************************************/
2095 static int do_host_query(char *query_host)
2097 cli = do_connect(query_host, "IPC$");
2102 list_servers(workgroup);
2110 /****************************************************************************
2111 handle a tar operation
2112 ****************************************************************************/
2113 static int do_tar_op(char *base_directory)
2116 cli = do_connect(desthost, service);
2122 if (*base_directory) do_cd(base_directory);
2131 /****************************************************************************
2132 handle a message operation
2133 ****************************************************************************/
2134 static int do_message_op(void)
2137 struct nmb_name called, calling;
2141 make_nmb_name(&calling, global_myname, 0x0, "");
2142 make_nmb_name(&called , desthost, name_type, "");
2145 if (have_ip) ip = dest_ip;
2147 if (!(cli=cli_initialise(NULL)) || !cli_connect(cli, desthost, &ip)) {
2148 DEBUG(0,("Connection to %s failed\n", desthost));
2152 if (!cli_session_request(cli, &calling, &called)) {
2153 DEBUG(0,("session request failed\n"));
2165 /****************************************************************************
2167 ****************************************************************************/
2168 int main(int argc,char *argv[])
2170 fstring base_directory;
2171 char *pname = argv[0];
2174 extern char *optarg;
2177 BOOL message = False;
2178 extern char tar_type;
2179 static pstring servicesf = CONFIGFILE;
2181 pstring new_name_resolve_order;
2185 pstrcpy(term_code, KANJI);
2191 *base_directory = 0;
2193 *new_name_resolve_order = 0;
2197 #ifdef HAVE_LIBREADLINE
2198 /* Allow conditional parsing of the ~/.inputrc file. */
2199 rl_readline_name = "smbclient";
2201 setup_logging(pname,True);
2204 * If the -E option is given, be careful not to clobber stdout
2205 * before processing the options. 28.Feb.99, richard@hacom.nl.
2206 * Also pre-parse the -s option to get the service file name.
2209 for (opt = 1; opt < argc; opt++) {
2210 if (strcmp(argv[opt], "-E") == 0)
2212 else if(strncmp(argv[opt], "-s", 2) == 0) {
2213 if(argv[opt][2] != '\0')
2214 pstrcpy(servicesf, &argv[opt][2]);
2215 else if(argv[opt+1] != NULL) {
2217 * At least one more arg left.
2219 pstrcpy(servicesf, argv[opt+1]);
2228 charset_initialise();
2230 in_client = True; /* Make sure that we tell lp_load we are */
2232 if (!lp_load(servicesf,True,False,False)) {
2233 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
2236 codepage_initialise(lp_client_code_page());
2242 pstrcpy(workgroup,lp_workgroup());
2248 if (getenv("USER")) {
2249 pstrcpy(username,getenv("USER"));
2251 /* modification to support userid%passwd syntax in the USER var
2252 25.Aug.97, jdblair@uab.edu */
2254 if ((p=strchr(username,'%'))) {
2256 pstrcpy(password,p+1);
2258 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
2263 /* modification to support PASSWD environmental var
2264 25.Aug.97, jdblair@uab.edu */
2265 if (getenv("PASSWD")) {
2266 pstrcpy(password,getenv("PASSWD"));
2270 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2271 get_password_file();
2275 if (*username == 0 && getenv("LOGNAME")) {
2276 pstrcpy(username,getenv("LOGNAME"));
2280 if (*username == 0) {
2281 pstrcpy(username,"GUEST");
2289 if (*argv[1] != '-') {
2290 pstrcpy(service,argv[1]);
2291 /* Convert any '/' characters in the service name to '\' characters */
2292 string_replace( service, '/','\\');
2296 if (count_chars(service,'\\') < 3) {
2298 printf("\n%s: Not enough '\\' characters in service\n",service);
2302 if (argc > 1 && (*argv[1] != '-')) {
2304 pstrcpy(password,argv[1]);
2305 memset(argv[1],'X',strlen(argv[1]));
2312 getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:")) != EOF) {
2315 pstrcpy(servicesf, optarg);
2318 pstrcpy(user_socket_options,optarg);
2321 pstrcpy(new_name_resolve_order, optarg);
2324 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2325 pstrcpy(desthost,optarg);
2329 pstrcpy(scope,optarg);
2335 pstrcpy(global_myname,optarg);
2341 DEBUGLEVEL = atoi(optarg);
2344 /* not needed anymore */
2347 port = atoi(optarg);
2350 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2358 dest_ip = *interpret_addr2(optarg);
2359 if (zero_ip(dest_ip))
2370 pstrcpy(username,optarg);
2371 if ((lp=strchr(username,'%'))) {
2373 pstrcpy(password,lp+1);
2375 memset(strchr(optarg,'%')+1,'X',strlen(password));
2381 while(*p == '\\' || *p == '/')
2383 pstrcpy(query_host,p);
2386 pstrcpy(term_code, optarg);
2389 /* no longer supported */
2392 pstrcpy(workgroup,optarg);
2395 if (!tar_parseargs(argc, argv, optarg, optind)) {
2401 pstrcpy(base_directory,optarg);
2408 io_bufsize = MAX(1, atoi(optarg));
2416 get_myname((*global_myname)?NULL:global_myname);
2418 if(*new_name_resolve_order)
2419 lp_set_name_resolve_order(new_name_resolve_order);
2422 interpret_coding_system(term_code);
2424 if (!tar_type && !*query_host && !*service && !message) {
2429 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2432 return do_tar_op(base_directory);
2435 if ((p=strchr(query_host,'#'))) {
2438 sscanf(p, "%x", &name_type);
2442 return do_host_query(query_host);
2446 return do_message_op();
2449 if (!process(base_directory)) {