2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 struct cli_state *cli;
31 extern BOOL in_client;
32 static int port = SMB_PORT;
33 pstring cur_dir = "\\";
35 static pstring service;
36 static pstring desthost;
37 extern pstring global_myname;
38 extern pstring myhostname;
39 static pstring password;
40 static pstring username;
41 static pstring workgroup;
44 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;
108 #define CNV_LANG(s) dos_to_unix(s,False)
109 #define CNV_INPUT(s) unix_to_dos(s,True)
112 /****************************************************************************
113 write to a local file with CR/LF->LF translation if appropriate. return the
114 number taken from the buffer. This may not equal the number written.
115 ****************************************************************************/
116 static int writefile(int f, char *b, int n)
126 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
129 if (write(f, b, 1) != 1) {
139 /****************************************************************************
140 read from a file with LF->CR/LF translation if appropriate. return the
141 number read. read approx n bytes.
142 ****************************************************************************/
143 static int readfile(char *b, int size, int n, FILE *f)
148 if (!translation || (size != 1))
149 return(fread(b,size,n,f));
153 if ((c = getc(f)) == EOF) {
157 if (c == '\n') { /* change all LFs to CR/LF */
170 /****************************************************************************
172 ****************************************************************************/
173 static void send_message(void)
178 if (!cli_message_start(cli, desthost, username, &grp_id)) {
179 DEBUG(0,("message start: %s\n", cli_errstr(cli)));
184 printf("Connected. Type your message, ending it with a Control-D\n");
186 while (!feof(stdin) && total_len < 1600) {
187 int maxlen = MIN(1600 - total_len,127);
194 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
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",CNV_LANG(service)));
243 DEBUG(0,("%s\n",CNV_LANG(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",CNV_LANG(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,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",
331 CNV_LANG(finfo->name),
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 int do_list_attr;
353 static void (*do_list_fn)(file_info *);
355 /****************************************************************************
357 ****************************************************************************/
358 static void do_list_helper(file_info *f, const char *mask)
360 if (f->mode & aDIR) {
361 if (do_list_dirs && do_this_one(f)) {
364 if (do_list_recurse &&
365 !strequal(f->name,".") &&
366 !strequal(f->name,"..")) {
370 pstrcpy(mask2, mask);
371 p = strrchr(mask2,'\\');
374 pstrcat(mask2, f->name);
375 if (do_list_fn == display_finfo) {
376 DEBUG(0,("\n%s\n",CNV_LANG(mask2)));
378 pstrcat(mask2,"\\*");
379 do_list(mask2, do_list_attr, do_list_fn, True, True);
384 if (do_this_one(f)) {
390 /****************************************************************************
391 a wrapper around cli_list that adds recursion
392 ****************************************************************************/
393 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
395 do_list_recurse = rec;
398 do_list_attr = attribute;
400 cli_list(cli, mask, attribute, do_list_helper);
403 /****************************************************************************
404 get a directory listing
405 ****************************************************************************/
406 static void cmd_dir(void)
408 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
414 pstrcpy(mask,cur_dir);
415 if(mask[strlen(mask)-1]!='\\')
418 if (next_token(NULL,buf,NULL,sizeof(buf))) {
429 do_list(mask, attribute, display_finfo, recurse, True);
433 DEBUG(3, ("Total bytes listed: %d\n", dir_total));
437 /****************************************************************************
438 get a directory listing
439 ****************************************************************************/
440 static void cmd_du(void)
442 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
448 pstrcpy(mask,cur_dir);
449 if(mask[strlen(mask)-1]!='\\')
452 if (next_token(NULL,buf,NULL,sizeof(buf))) {
462 do_list(mask, attribute, do_du, recurse, True);
466 DEBUG(0, ("Total number of bytes: %d\n", dir_total));
470 /****************************************************************************
471 get a file from rname to lname
472 ****************************************************************************/
473 static void do_get(char *rname,char *lname)
476 BOOL newhandle = False;
478 struct timeval tp_start;
479 int read_size = 65520;
484 GetTimeOfDay(&tp_start);
490 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
493 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
497 if(!strcmp(lname,"-")) {
498 handle = fileno(stdout);
500 handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
504 DEBUG(0,("Error opening local file %s\n",lname));
509 if (!cli_qfileinfo(cli, fnum,
510 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
511 !cli_getattrE(cli, fnum,
512 &attr, &size, NULL, NULL, NULL)) {
513 DEBUG(0,("getattrib: %s\n",cli_errstr(cli)));
517 DEBUG(2,("getting file %s of size %.0f as %s ",
518 lname, (double)size, lname));
520 data = (char *)malloc(read_size);
523 int n = cli_read(cli, fnum, data, nread, read_size);
527 if (writefile(handle,data, n) != n) {
528 DEBUG(0,("Error writing local file\n"));
535 if (!cli_close(cli, fnum)) {
536 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
543 if (archive_level >= 2 && (attr & aARCH)) {
544 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
548 struct timeval tp_end;
551 GetTimeOfDay(&tp_end);
553 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
554 (tp_end.tv_usec - tp_start.tv_usec)/1000;
555 get_total_time_ms += this_time;
556 get_total_size += nread;
558 DEBUG(2,("(%g kb/s) (average %g kb/s)\n",
559 nread / (1.024*this_time + 1.0e-4),
560 get_total_size / (1.024*get_total_time_ms)));
565 /****************************************************************************
567 ****************************************************************************/
568 static void cmd_get(void)
574 pstrcpy(rname,cur_dir);
577 p = rname + strlen(rname);
579 if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
580 DEBUG(0,("get <filename>\n"));
584 dos_clean_name(rname);
586 next_token(NULL,lname,NULL,sizeof(lname));
588 do_get(rname, lname);
592 /****************************************************************************
593 do a mget operation on one file
594 ****************************************************************************/
595 static void do_mget(file_info *finfo)
599 pstring saved_curdir;
602 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
606 DEBUG(0,("mget aborted\n"));
610 if (finfo->mode & aDIR)
611 slprintf(quest,sizeof(pstring)-1,
612 "Get directory %s? ",CNV_LANG(finfo->name));
614 slprintf(quest,sizeof(pstring)-1,
615 "Get file %s? ",CNV_LANG(finfo->name));
617 if (prompt && !yesno(quest)) return;
619 if (!(finfo->mode & aDIR)) {
620 pstrcpy(rname,cur_dir);
621 pstrcat(rname,finfo->name);
622 do_get(rname,finfo->name);
626 /* handle directories */
627 pstrcpy(saved_curdir,cur_dir);
629 pstrcat(cur_dir,finfo->name);
630 pstrcat(cur_dir,"\\");
632 unix_format(finfo->name);
634 strlower(finfo->name);
636 if (!dos_directory_exist(finfo->name,NULL) &&
637 dos_mkdir(finfo->name,0777) != 0) {
638 DEBUG(0,("failed to create directory %s\n",CNV_LANG(finfo->name)));
639 pstrcpy(cur_dir,saved_curdir);
643 if (dos_chdir(finfo->name) != 0) {
644 DEBUG(0,("failed to chdir to directory %s\n",CNV_LANG(finfo->name)));
645 pstrcpy(cur_dir,saved_curdir);
649 pstrcpy(mget_mask,cur_dir);
650 pstrcat(mget_mask,"*");
652 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
654 pstrcpy(cur_dir,saved_curdir);
658 /****************************************************************************
659 view the file using the pager
660 ****************************************************************************/
661 static void cmd_more(void)
663 fstring rname,lname,tmpname,pager_cmd;
666 fstrcpy(rname,cur_dir);
670 "%s/smbmore.%d",tmpdir(),(int)getpid());
671 fstrcpy(lname,tmpname);
673 if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
674 DEBUG(0,("more <filename>\n"));
677 dos_clean_name(rname);
681 pager=getenv("PAGER");
683 slprintf(pager_cmd,sizeof(pager_cmd)-1,
684 "%s %s",(pager? pager:PAGER), tmpname);
691 /****************************************************************************
693 ****************************************************************************/
694 static void cmd_mget(void)
696 uint16 attribute = aSYSTEM | aHIDDEN;
708 while (next_token(NULL,p,NULL,sizeof(buf))) {
709 pstrcpy(mget_mask,cur_dir);
710 if(mget_mask[strlen(mget_mask)-1]!='\\')
711 pstrcat(mget_mask,"\\");
714 pstrcpy(mget_mask,p);
716 pstrcat(mget_mask,p);
717 do_list(mget_mask, attribute,do_mget,False,True);
721 pstrcpy(mget_mask,cur_dir);
722 if(mget_mask[strlen(mget_mask)-1]!='\\')
723 pstrcat(mget_mask,"\\");
724 pstrcat(mget_mask,"*");
725 do_list(mget_mask, attribute,do_mget,False,True);
730 /****************************************************************************
731 make a directory of name "name"
732 ****************************************************************************/
733 static BOOL do_mkdir(char *name)
735 if (!cli_mkdir(cli, name)) {
736 DEBUG(0,("%s making remote directory %s\n",
737 cli_errstr(cli),CNV_LANG(name)));
745 /****************************************************************************
746 make a directory of name "name"
747 ****************************************************************************/
748 static void cmd_quit(void)
755 /****************************************************************************
757 ****************************************************************************/
758 static void cmd_mkdir(void)
764 pstrcpy(mask,cur_dir);
766 if (!next_token(NULL,p,NULL,sizeof(buf))) {
768 DEBUG(0,("mkdir <dirname>\n"));
779 trim_string(ddir,".",NULL);
780 p = strtok(ddir,"/\\");
783 if (!cli_chkpath(cli, ddir2)) {
787 p = strtok(NULL,"/\\");
795 /****************************************************************************
797 ****************************************************************************/
798 static void do_put(char *rname,char *lname)
806 struct timeval tp_start;
807 GetTimeOfDay(&tp_start);
809 fnum = cli_open(cli, rname, O_WRONLY|O_CREAT|O_TRUNC, DENY_NONE);
812 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
816 /* allow files to be piped into smbclient
818 if (!strcmp(lname, "-")) {
820 /* size of file is not known */
822 f = sys_fopen(lname,"r");
826 DEBUG(0,("Error opening local file %s\n",lname));
831 DEBUG(1,("putting file %s as %s ",lname,
834 buf = (char *)malloc(maxwrite);
839 if ((n = readfile(buf,1,n,f)) < 1) {
840 DEBUG(0,("Error reading local file\n"));
844 ret = cli_write(cli, fnum, 0, buf, nread, n);
847 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
854 if (!cli_close(cli, fnum)) {
855 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
866 struct timeval tp_end;
869 GetTimeOfDay(&tp_end);
871 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
872 (tp_end.tv_usec - tp_start.tv_usec)/1000;
873 put_total_time_ms += this_time;
874 put_total_size += nread;
876 DEBUG(1,("(%g kb/s) (average %g kb/s)\n",
877 nread / (1.024*this_time + 1.0e-4),
878 put_total_size / (1.024*put_total_time_ms)));
889 /****************************************************************************
891 ****************************************************************************/
892 static void cmd_put(void)
899 pstrcpy(rname,cur_dir);
902 if (!next_token(NULL,p,NULL,sizeof(buf))) {
903 DEBUG(0,("put <filename>\n"));
908 if (next_token(NULL,p,NULL,sizeof(buf)))
911 pstrcat(rname,lname);
913 dos_clean_name(rname);
917 /* allow '-' to represent stdin
918 jdblair, 24.jun.98 */
919 if (!file_exist(lname,&st) &&
920 (strcmp(lname,"-"))) {
921 DEBUG(0,("%s does not exist\n",lname));
930 /****************************************************************************
931 seek in a directory/file list until you get something that doesn't start with
933 ****************************************************************************/
934 static BOOL seek_list(FILE *f,char *name)
938 if (fscanf(f,"%s",s) != 1) return(False);
939 trim_string(s,"./",NULL);
940 if (strncmp(s,name,strlen(name)) != 0) {
950 /****************************************************************************
951 set the file selection mask
952 ****************************************************************************/
953 static void cmd_select(void)
955 pstrcpy(fileselection,"");
956 next_token(NULL,fileselection,NULL,sizeof(fileselection));
960 /****************************************************************************
962 ****************************************************************************/
963 static void cmd_mput(void)
970 while (next_token(NULL,p,NULL,sizeof(buf))) {
976 slprintf(tmpname,sizeof(pstring)-1,
977 "%s/ls.smb.%d",tmpdir(),(int)getpid());
979 slprintf(cmd,sizeof(pstring)-1,
980 "find . -name \"%s\" -print > %s",p,tmpname);
982 slprintf(cmd,sizeof(pstring)-1,
983 "/bin/ls %s > %s",p,tmpname);
986 f = sys_fopen(tmpname,"r");
992 if (fscanf(f,"%s",lname) != 1) break;
993 trim_string(lname,"./",NULL);
997 /* check if it's a directory */
998 if (directory_exist(lname,&st)) {
999 if (!recurse) continue;
1000 slprintf(quest,sizeof(pstring)-1,
1001 "Put directory %s? ",lname);
1002 if (prompt && !yesno(quest)) {
1004 if (!seek_list(f,lname))
1009 pstrcpy(rname,cur_dir);
1010 pstrcat(rname,lname);
1011 if (!cli_chkpath(cli, rname) && !do_mkdir(rname)) {
1013 if (!seek_list(f,lname))
1019 slprintf(quest,sizeof(quest)-1,
1020 "Put file %s? ",lname);
1021 if (prompt && !yesno(quest)) continue;
1023 pstrcpy(rname,cur_dir);
1024 pstrcat(rname,lname);
1029 do_put(rname,lname);
1037 /****************************************************************************
1039 ****************************************************************************/
1040 static void do_cancel(int job)
1042 if (cli_printjob_del(cli, job)) {
1043 printf("Job %d cancelled\n",job);
1045 printf("Error calcelling job %d : %s\n",job,cli_errstr(cli));
1050 /****************************************************************************
1052 ****************************************************************************/
1053 static void cmd_cancel(void)
1058 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1059 printf("cancel <jobid> ...\n");
1065 } while (next_token(NULL,buf,NULL,sizeof(buf)));
1069 /****************************************************************************
1071 ****************************************************************************/
1072 static void cmd_print(void)
1078 if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1079 DEBUG(0,("print <filename>\n"));
1083 pstrcpy(rname,lname);
1084 p = strrchr(rname,'/');
1086 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1089 if (strequal(lname,"-")) {
1090 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1093 do_put(rname, lname);
1097 /****************************************************************************
1098 show a print queue entry
1099 ****************************************************************************/
1100 static void queue_fn(struct print_job_info *p)
1102 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1105 /****************************************************************************
1107 ****************************************************************************/
1108 static void cmd_queue(void)
1110 cli_print_queue(cli, queue_fn);
1113 /****************************************************************************
1115 ****************************************************************************/
1116 static void do_del(file_info *finfo)
1120 pstrcpy(mask,cur_dir);
1121 pstrcat(mask,finfo->name);
1123 if (finfo->mode & aDIR)
1126 if (!cli_unlink(cli, mask)) {
1127 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),CNV_LANG(mask)));
1131 /****************************************************************************
1133 ****************************************************************************/
1134 static void cmd_del(void)
1138 uint16 attribute = aSYSTEM | aHIDDEN;
1143 pstrcpy(mask,cur_dir);
1145 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1146 DEBUG(0,("del <filename>\n"));
1151 do_list(mask, attribute,do_del,False,False);
1155 /****************************************************************************
1157 ****************************************************************************/
1158 static void cmd_rmdir(void)
1163 pstrcpy(mask,cur_dir);
1165 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1166 DEBUG(0,("rmdir <dirname>\n"));
1171 if (!cli_rmdir(cli, mask)) {
1172 DEBUG(0,("%s removing remote directory file %s\n",
1173 cli_errstr(cli),CNV_LANG(mask)));
1177 /****************************************************************************
1179 ****************************************************************************/
1180 static void cmd_rename(void)
1185 pstrcpy(src,cur_dir);
1186 pstrcpy(dest,cur_dir);
1188 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1189 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1190 DEBUG(0,("rename <src> <dest>\n"));
1197 if (!cli_rename(cli, src, dest)) {
1198 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1204 /****************************************************************************
1205 toggle the prompt flag
1206 ****************************************************************************/
1207 static void cmd_prompt(void)
1210 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1214 /****************************************************************************
1215 set the newer than time
1216 ****************************************************************************/
1217 static void cmd_newer(void)
1221 SMB_STRUCT_STAT sbuf;
1223 ok = next_token(NULL,buf,NULL,sizeof(buf));
1224 if (ok && (dos_stat(buf,&sbuf) == 0)) {
1225 newer_than = sbuf.st_mtime;
1226 DEBUG(1,("Getting files newer than %s",
1227 asctime(LocalTime(&newer_than))));
1232 if (ok && newer_than == 0)
1233 DEBUG(0,("Error setting newer-than time\n"));
1236 /****************************************************************************
1237 set the archive level
1238 ****************************************************************************/
1239 static void cmd_archive(void)
1243 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1244 archive_level = atoi(buf);
1246 DEBUG(0,("Archive level is %d\n",archive_level));
1249 /****************************************************************************
1250 toggle the lowercaseflag
1251 ****************************************************************************/
1252 static void cmd_lowercase(void)
1254 lowercase = !lowercase;
1255 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1261 /****************************************************************************
1262 toggle the recurse flag
1263 ****************************************************************************/
1264 static void cmd_recurse(void)
1267 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1270 /****************************************************************************
1271 toggle the translate flag
1272 ****************************************************************************/
1273 static void cmd_translate(void)
1275 translation = !translation;
1276 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1277 translation?"on":"off"));
1281 /****************************************************************************
1282 do a printmode command
1283 ****************************************************************************/
1284 static void cmd_printmode(void)
1289 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1290 if (strequal(buf,"text")) {
1293 if (strequal(buf,"graphics"))
1296 printmode = atoi(buf);
1303 fstrcpy(mode,"text");
1306 fstrcpy(mode,"graphics");
1309 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1313 DEBUG(2,("the printmode is now %s\n",mode));
1316 /****************************************************************************
1318 ****************************************************************************/
1319 static void cmd_lcd(void)
1324 if (next_token(NULL,buf,NULL,sizeof(buf)))
1326 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
1329 /****************************************************************************
1331 ****************************************************************************/
1332 static void browse_fn(const char *name, uint32 m, const char *comment)
1339 case STYPE_DISKTREE:
1340 fstrcpy(typestr,"Disk"); break;
1342 fstrcpy(typestr,"Printer"); break;
1344 fstrcpy(typestr,"Device"); break;
1346 fstrcpy(typestr,"IPC"); break;
1349 printf("\t%-15.15s%-10.10s%s\n",
1350 name, typestr,comment);
1354 /****************************************************************************
1355 try and browse available connections on a host
1356 ****************************************************************************/
1357 static BOOL browse_host(BOOL sort)
1359 printf("\n\tSharename Type Comment\n");
1360 printf("\t--------- ---- -------\n");
1362 return cli_RNetShareEnum(cli, browse_fn);
1365 /****************************************************************************
1367 ****************************************************************************/
1368 static void server_fn(const char *name, uint32 m, const char *comment)
1370 printf("\t%-16.16s %s\n", name, comment);
1373 /****************************************************************************
1374 try and browse available connections on a host
1375 ****************************************************************************/
1376 static BOOL list_servers(char *wk_grp)
1378 printf("\n\tServer Comment\n");
1379 printf("\t--------- -------\n");
1381 cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, server_fn);
1383 printf("\n\tWorkgroup Master\n");
1384 printf("\t--------- -------\n");
1386 cli_NetServerEnum(cli, workgroup, SV_TYPE_DOMAIN_ENUM, server_fn);
1391 /* Some constants for completing filename arguments */
1393 #define COMPL_NONE 0 /* No completions */
1394 #define COMPL_REMOTE 1 /* Complete remote filename */
1395 #define COMPL_LOCAL 2 /* Complete local filename */
1397 /* This defines the commands supported by this client */
1403 char compl_args[2]; /* Completion argument info */
1406 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1407 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1408 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1409 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1410 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1411 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1412 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1413 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1414 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1415 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1416 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1417 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1418 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1419 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1420 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1421 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1422 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1423 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1424 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1425 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1426 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1427 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1428 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1429 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1430 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1431 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1432 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1433 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1434 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1435 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1436 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1437 {"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}},
1438 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1439 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1440 {"tarmode",cmd_tarmode,
1441 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1442 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1443 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1444 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1445 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1446 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1450 /*******************************************************************
1451 lookup a command string in the list of commands, including
1453 ******************************************************************/
1454 static int process_tok(fstring tok)
1456 int i = 0, matches = 0;
1458 int tok_len = strlen(tok);
1460 while (commands[i].fn != NULL) {
1461 if (strequal(commands[i].name,tok)) {
1465 } else if (strnequal(commands[i].name, tok, tok_len)) {
1474 else if (matches == 1)
1480 /****************************************************************************
1482 ****************************************************************************/
1483 static void cmd_help(void)
1488 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1489 if ((i = process_tok(buf)) >= 0)
1490 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1492 while (commands[i].description) {
1493 for (j=0; commands[i].description && (j<5); j++) {
1494 DEBUG(0,("%-15s",commands[i].name));
1502 /****************************************************************************
1503 wait for keyboard activity, swallowing network packets
1504 ****************************************************************************/
1505 static void wait_keyboard(void)
1508 struct timeval timeout;
1512 FD_SET(cli->fd,&fds);
1513 FD_SET(fileno(stdin),&fds);
1515 timeout.tv_sec = 20;
1516 timeout.tv_usec = 0;
1517 sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
1519 if (FD_ISSET(fileno(stdin),&fds))
1522 /* We deliberately use receive_smb instead of
1523 client_receive_smb as we want to receive
1524 session keepalives and then drop them here.
1526 if (FD_ISSET(cli->fd,&fds))
1527 receive_smb(cli->fd,cli->inbuf,0);
1529 cli_chkpath(cli, "\\");
1533 /****************************************************************************
1534 process a -c command string
1535 ****************************************************************************/
1536 static void process_command_string(char *cmd)
1541 while (cmd[0] != '\0') {
1546 if ((p = strchr(cmd, ';')) == 0) {
1547 strncpy(line, cmd, 999);
1551 if (p - cmd > 999) p = cmd + 999;
1552 strncpy(line, cmd, p - cmd);
1553 line[p - cmd] = '\0';
1557 /* input language code to internal one */
1560 /* and get the first part of the command */
1562 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1564 if ((i = process_tok(tok)) >= 0) {
1566 } else if (i == -2) {
1567 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1569 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1574 /****************************************************************************
1575 process commands on stdin
1576 ****************************************************************************/
1577 static void process_stdin(void)
1582 while (!feof(stdin)) {
1586 /* display a prompt */
1587 DEBUG(0,("smb: %s> ", CNV_LANG(cur_dir)));
1592 /* and get a response */
1593 if (!fgets(line,1000,stdin))
1596 /* input language code to internal one */
1599 /* special case - first char is ! */
1605 /* and get the first part of the command */
1607 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1609 if ((i = process_tok(tok)) >= 0) {
1611 } else if (i == -2) {
1612 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1614 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1620 /*****************************************************
1621 return a connection to a server
1622 *******************************************************/
1623 struct cli_state *do_connect(char *server, char *share)
1625 struct cli_state *c;
1626 struct nmb_name called, calling;
1629 extern struct in_addr ipzero;
1631 if (*share == '\\') {
1633 share = strchr(server,'\\');
1634 if (!share) return NULL;
1643 make_nmb_name(&calling, global_myname, 0x0, "");
1644 make_nmb_name(&called , server, name_type, "");
1647 port = 139; /* If not set, set to 139, FIXME, NUMBERS BAD */
1651 if (have_ip) ip = dest_ip;
1653 /* have to open a new connection */
1654 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) == 0) ||
1655 !cli_connect(c, server_n, &ip)) {
1656 DEBUG(0,("Connection to %s failed\n", server_n));
1660 if (!cli_session_request(c, &calling, &called)) {
1661 DEBUG(0,("session request to %s failed\n", called.name));
1663 if (strcmp(called.name, "*SMBSERVER")) {
1664 make_nmb_name(&called , "*SMBSERVER", 0x20, "");
1670 DEBUG(4,(" session request ok\n"));
1672 if (!cli_negprot(c)) {
1673 DEBUG(0,("protocol negotiation failed\n"));
1679 char *pass = getpass("Password: ");
1681 pstrcpy(password, pass);
1685 if (!cli_session_setup(c, username,
1686 password, strlen(password),
1687 password, strlen(password),
1689 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
1694 * These next two lines are needed to emulate
1695 * old client behaviour for people who have
1696 * scripts based on client output.
1697 * QUESTION ? Do we want to have a 'client compatibility
1698 * mode to turn these on/off ? JRA.
1701 if (*c->server_domain || *c->server_os || *c->server_type)
1702 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
1703 c->server_domain,c->server_os,c->server_type));
1705 DEBUG(4,(" session setup ok\n"));
1707 if (!cli_send_tconX(c, share, "?????",
1708 password, strlen(password)+1)) {
1709 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
1714 DEBUG(4,(" tconx ok\n"));
1720 /****************************************************************************
1721 process commands from the client
1722 ****************************************************************************/
1723 static BOOL process(char *base_directory)
1725 cli = do_connect(desthost, service);
1730 if (*base_directory) do_cd(base_directory);
1733 process_command_string(cmdstr);
1742 /****************************************************************************
1743 usage on the program
1744 ****************************************************************************/
1745 static void usage(char *pname)
1747 DEBUG(0,("Usage: %s service <password> ", pname));
1749 DEBUG(0,("\nVersion %s\n",VERSION));
1750 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
1751 DEBUG(0,("\t-B IP addr broadcast IP address to use\n"));
1752 DEBUG(0,("\t-O socket_options socket options to use\n"));
1753 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
1754 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
1755 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
1756 DEBUG(0,("\t-N don't ask for a password\n"));
1757 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
1758 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
1759 DEBUG(0,("\t-P connect to service as a printer\n"));
1760 DEBUG(0,("\t-p port connect to the specified port\n"));
1761 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
1762 DEBUG(0,("\t-h Print this help message.\n"));
1763 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
1764 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
1765 DEBUG(0,("\t-U username set the network username\n"));
1766 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
1767 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
1768 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
1769 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
1770 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
1771 DEBUG(0,("\t-D directory start from directory\n"));
1772 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
1777 /****************************************************************************
1778 get a password from a a file or file descriptor
1780 ****************************************************************************/
1781 static void get_password_file(void)
1785 BOOL close_it = False;
1789 if ((p = getenv("PASSWD_FD")) != NULL) {
1790 pstrcpy(spec, "descriptor ");
1792 sscanf(p, "%d", &fd);
1794 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
1795 fd = sys_open(p, O_RDONLY, 0);
1798 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
1799 spec, strerror(errno));
1805 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1806 p && p - pass < sizeof(pass);) {
1807 switch (read(fd, p, 1)) {
1809 if (*p != '\n' && *p != '\0') {
1810 *++p = '\0'; /* advance p, and null-terminate pass */
1815 *p = '\0'; /* null-terminate it, just in case... */
1816 p = NULL; /* then force the loop condition to become false */
1819 fprintf(stderr, "Error reading password from file %s: %s\n",
1820 spec, "empty password\n");
1825 fprintf(stderr, "Error reading password from file %s: %s\n",
1826 spec, strerror(errno));
1830 pstrcpy(password, pass);
1837 /****************************************************************************
1839 ****************************************************************************/
1840 static int do_host_query(char *query_host, int port)
1842 cli = do_connect(query_host, "IPC$");
1847 list_servers(workgroup);
1855 /****************************************************************************
1856 handle a tar operation
1857 ****************************************************************************/
1858 static int do_tar_op(int port, char *base_directory)
1861 cli = do_connect(desthost, service);
1867 if (*base_directory) do_cd(base_directory);
1876 /****************************************************************************
1877 handle a message operation
1878 ****************************************************************************/
1879 static int do_message_op(void)
1882 struct nmb_name called, calling;
1886 make_nmb_name(&calling, global_myname, 0x0, "");
1887 make_nmb_name(&called , desthost, name_type, "");
1890 if (have_ip) ip = dest_ip;
1892 if (!(cli=cli_initialise(NULL)) || !cli_connect(cli, desthost, &ip)) {
1893 DEBUG(0,("Connection to %s failed\n", desthost));
1897 if (!cli_session_request(cli, &calling, &called)) {
1898 DEBUG(0,("session request failed\n"));
1910 /****************************************************************************
1912 ****************************************************************************/
1913 int main(int argc,char *argv[])
1915 fstring base_directory;
1916 char *pname = argv[0];
1919 extern char *optarg;
1922 BOOL message = False;
1923 BOOL explicit_user = False;
1924 extern char tar_type;
1925 static pstring servicesf = CONFIGFILE;
1927 pstring new_name_resolve_order;
1931 pstrcpy(term_code, KANJI);
1937 *base_directory = 0;
1939 *new_name_resolve_order = 0;
1943 setup_logging(pname,True);
1946 charset_initialise();
1948 if(!get_myname(myhostname,NULL)) {
1949 DEBUG(0,("Failed to get my hostname.\n"));
1952 in_client = True; /* Make sure that we tell lp_load we are */
1954 if (!lp_load(servicesf,True,False,False)) {
1955 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
1958 codepage_initialise(lp_client_code_page());
1960 interpret_coding_system(term_code);
1966 pstrcpy(workgroup,lp_workgroup());
1972 if (getenv("USER")) {
1973 pstrcpy(username,getenv("USER"));
1975 /* modification to support userid%passwd syntax in the USER var
1976 25.Aug.97, jdblair@uab.edu */
1978 if ((p=strchr(username,'%'))) {
1980 pstrcpy(password,p+1);
1982 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
1987 /* modification to support PASSWD environmental var
1988 25.Aug.97, jdblair@uab.edu */
1989 if (getenv("PASSWD")) {
1990 pstrcpy(password,getenv("PASSWD"));
1994 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
1995 get_password_file();
1999 if (*username == 0 && getenv("LOGNAME")) {
2000 pstrcpy(username,getenv("LOGNAME"));
2004 if (*username == 0) {
2005 pstrcpy(username,"GUEST");
2013 if (*argv[1] != '-') {
2014 pstrcpy(service,argv[1]);
2015 /* Convert any '/' characters in the service name to '\' characters */
2016 string_replace( service, '/','\\');
2020 if (count_chars(service,'\\') < 3) {
2022 printf("\n%s: Not enough '\\' characters in service\n",service);
2026 if (argc > 1 && (*argv[1] != '-')) {
2028 pstrcpy(password,argv[1]);
2029 memset(argv[1],'X',strlen(argv[1]));
2036 getopt(argc, argv,"s:B:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:")) != EOF) {
2039 pstrcpy(servicesf, optarg);
2042 iface_set_default(NULL,optarg,NULL);
2045 pstrcpy(user_socket_options,optarg);
2048 pstrcpy(new_name_resolve_order, optarg);
2051 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2052 pstrcpy(desthost,optarg);
2056 pstrcpy(scope,optarg);
2062 pstrcpy(global_myname,optarg);
2068 DEBUGLEVEL = atoi(optarg);
2071 /* not needed anymore */
2074 port = atoi(optarg);
2077 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2085 dest_ip = *interpret_addr2(optarg);
2086 if (zero_ip(dest_ip))
2097 explicit_user = True;
2098 pstrcpy(username,optarg);
2099 if ((lp=strchr(username,'%'))) {
2101 pstrcpy(password,lp+1);
2103 memset(strchr(optarg,'%')+1,'X',strlen(password));
2108 pstrcpy(query_host,optarg);
2113 pstrcpy(term_code, optarg);
2116 /* no longer supported */
2119 pstrcpy(workgroup,optarg);
2122 if (!tar_parseargs(argc, argv, optarg, optind)) {
2128 pstrcpy(base_directory,optarg);
2140 get_myname((*global_myname)?NULL:global_myname,NULL);
2142 if(*new_name_resolve_order)
2143 lp_set_name_resolve_order(new_name_resolve_order);
2145 if (!tar_type && !*query_host && !*service && !message) {
2150 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2153 return do_tar_op(port, base_directory);
2156 if ((p=strchr(query_host,'#'))) {
2159 sscanf(p, "%x", &name_type);
2163 return do_host_query(query_host, port);
2167 return do_message_op();
2170 if (!process(base_directory)) {