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 pstring cur_dir = "\\";
34 static pstring service;
35 static pstring desthost;
36 extern pstring global_myname;
37 extern pstring myhostname;
38 static pstring password;
39 static pstring username;
40 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,int 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 int 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 int 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 = open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
504 DEBUG(0,("Error opening local file %s\n",lname));
509 if (!cli_getattrE(cli, fnum, &attr, &size, NULL, NULL, NULL)) {
510 DEBUG(0,("getattrE: %s\n",cli_errstr(cli)));
515 DEBUG(2,("getting file %s of size %.0f as %s ",
516 lname, (double)size, lname));
518 data = (char *)malloc(read_size);
521 int n = cli_read(cli, fnum, data, nread, read_size);
525 if (writefile(handle,data, n) != n) {
526 DEBUG(0,("Error writing local file\n"));
533 if (!cli_close(cli, fnum)) {
534 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
541 if (archive_level >= 2 && (attr & aARCH)) {
542 cli_setatr(cli, rname, attr & ~aARCH, 0);
546 struct timeval tp_end;
549 GetTimeOfDay(&tp_end);
551 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
552 (tp_end.tv_usec - tp_start.tv_usec)/1000;
553 get_total_time_ms += this_time;
554 get_total_size += nread;
556 DEBUG(2,("(%g kb/s) (average %g kb/s)\n",
557 nread / (1.024*this_time + 1.0e-4),
558 get_total_size / (1.024*get_total_time_ms)));
563 /****************************************************************************
565 ****************************************************************************/
566 static void cmd_get(void)
572 pstrcpy(rname,cur_dir);
575 p = rname + strlen(rname);
577 if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
578 DEBUG(0,("get <filename>\n"));
582 dos_clean_name(rname);
584 next_token(NULL,lname,NULL,sizeof(lname));
586 do_get(rname, lname);
590 /****************************************************************************
591 do a mget operation on one file
592 ****************************************************************************/
593 static void do_mget(file_info *finfo)
597 pstring saved_curdir;
600 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
604 DEBUG(0,("mget aborted\n"));
608 if (finfo->mode & aDIR)
609 slprintf(quest,sizeof(pstring)-1,
610 "Get directory %s? ",CNV_LANG(finfo->name));
612 slprintf(quest,sizeof(pstring)-1,
613 "Get file %s? ",CNV_LANG(finfo->name));
615 if (prompt && !yesno(quest)) return;
617 if (!(finfo->mode & aDIR)) {
618 pstrcpy(rname,cur_dir);
619 pstrcat(rname,finfo->name);
620 do_get(rname,finfo->name);
624 /* handle directories */
625 pstrcpy(saved_curdir,cur_dir);
627 pstrcat(cur_dir,finfo->name);
628 pstrcat(cur_dir,"\\");
630 unix_format(finfo->name);
632 strlower(finfo->name);
634 if (!directory_exist(finfo->name,NULL) &&
635 dos_mkdir(finfo->name,0777) != 0) {
636 DEBUG(0,("failed to create directory %s\n",CNV_LANG(finfo->name)));
637 pstrcpy(cur_dir,saved_curdir);
641 if (dos_chdir(finfo->name) != 0) {
642 DEBUG(0,("failed to chdir to directory %s\n",CNV_LANG(finfo->name)));
643 pstrcpy(cur_dir,saved_curdir);
647 pstrcpy(mget_mask,cur_dir);
648 pstrcat(mget_mask,"*.*");
650 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
652 pstrcpy(cur_dir,saved_curdir);
656 /****************************************************************************
657 view the file using the pager
658 ****************************************************************************/
659 static void cmd_more(void)
661 fstring rname,lname,tmpname,pager_cmd;
664 fstrcpy(rname,cur_dir);
668 "%s/smbmore.%d",tmpdir(),(int)getpid());
669 fstrcpy(lname,tmpname);
671 if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
672 DEBUG(0,("more <filename>\n"));
675 dos_clean_name(rname);
679 pager=getenv("PAGER");
681 slprintf(pager_cmd,sizeof(pager_cmd)-1,
682 "%s %s",(pager? pager:PAGER), tmpname);
689 /****************************************************************************
691 ****************************************************************************/
692 static void cmd_mget(void)
694 int attribute = aSYSTEM | aHIDDEN;
706 while (next_token(NULL,p,NULL,sizeof(buf))) {
707 pstrcpy(mget_mask,cur_dir);
708 if(mget_mask[strlen(mget_mask)-1]!='\\')
709 pstrcat(mget_mask,"\\");
712 pstrcpy(mget_mask,p);
714 pstrcat(mget_mask,p);
715 do_list(mget_mask, attribute,do_mget,False,True);
719 pstrcpy(mget_mask,cur_dir);
720 if(mget_mask[strlen(mget_mask)-1]!='\\')
721 pstrcat(mget_mask,"\\");
722 pstrcat(mget_mask,"*.*");
723 do_list(mget_mask, attribute,do_mget,False,True);
728 /****************************************************************************
729 make a directory of name "name"
730 ****************************************************************************/
731 static BOOL do_mkdir(char *name)
733 if (!cli_mkdir(cli, name)) {
734 DEBUG(0,("%s making remote directory %s\n",
735 cli_errstr(cli),CNV_LANG(name)));
743 /****************************************************************************
744 make a directory of name "name"
745 ****************************************************************************/
746 static void cmd_quit(void)
753 /****************************************************************************
755 ****************************************************************************/
756 static void cmd_mkdir(void)
762 pstrcpy(mask,cur_dir);
764 if (!next_token(NULL,p,NULL,sizeof(buf))) {
766 DEBUG(0,("mkdir <dirname>\n"));
777 trim_string(ddir,".",NULL);
778 p = strtok(ddir,"/\\");
781 if (!cli_chkpath(cli, ddir2)) {
785 p = strtok(NULL,"/\\");
793 /****************************************************************************
795 ****************************************************************************/
796 static void do_put(char *rname,char *lname)
804 struct timeval tp_start;
805 GetTimeOfDay(&tp_start);
807 fnum = cli_open(cli, rname, O_WRONLY|O_CREAT|O_TRUNC, DENY_NONE);
810 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
814 /* allow files to be piped into smbclient
816 if (!strcmp(lname, "-")) {
818 /* size of file is not known */
820 f = fopen(lname,"r");
824 DEBUG(0,("Error opening local file %s\n",lname));
829 DEBUG(1,("putting file %s as %s ",lname,
832 buf = (char *)malloc(maxwrite);
837 if ((n = readfile(buf,1,n,f)) < 1) {
838 DEBUG(0,("Error reading local file\n"));
842 ret = cli_write(cli, fnum, 0, buf, nread, n);
845 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
852 if (!cli_close(cli, fnum)) {
853 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
864 struct timeval tp_end;
867 GetTimeOfDay(&tp_end);
869 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
870 (tp_end.tv_usec - tp_start.tv_usec)/1000;
871 put_total_time_ms += this_time;
872 put_total_size += nread;
874 DEBUG(1,("(%g kb/s) (average %g kb/s)\n",
875 nread / (1.024*this_time + 1.0e-4),
876 put_total_size / (1.024*put_total_time_ms)));
887 /****************************************************************************
889 ****************************************************************************/
890 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));
924 finfo.mtime = st.st_mtime;
931 /****************************************************************************
932 seek in a directory/file list until you get something that doesn't start with
934 ****************************************************************************/
935 static BOOL seek_list(FILE *f,char *name)
939 if (fscanf(f,"%s",s) != 1) return(False);
940 trim_string(s,"./",NULL);
941 if (strncmp(s,name,strlen(name)) != 0) {
951 /****************************************************************************
952 set the file selection mask
953 ****************************************************************************/
954 static void cmd_select(void)
956 pstrcpy(fileselection,"");
957 next_token(NULL,fileselection,NULL,sizeof(fileselection));
961 /****************************************************************************
963 ****************************************************************************/
964 static void cmd_mput(void)
974 while (next_token(NULL,p,NULL,sizeof(buf))) {
980 slprintf(tmpname,sizeof(pstring)-1,
981 "%s/ls.smb.%d",tmpdir(),(int)getpid());
983 slprintf(cmd,sizeof(pstring)-1,
984 "find . -name \"%s\" -print > %s",p,tmpname);
986 slprintf(cmd,sizeof(pstring)-1,
987 "/bin/ls %s > %s",p,tmpname);
990 f = fopen(tmpname,"r");
996 if (fscanf(f,"%s",lname) != 1) break;
997 trim_string(lname,"./",NULL);
1001 /* check if it's a directory */
1002 if (directory_exist(lname,&st)) {
1003 if (!recurse) continue;
1004 slprintf(quest,sizeof(pstring)-1,
1005 "Put directory %s? ",lname);
1006 if (prompt && !yesno(quest)) {
1008 if (!seek_list(f,lname))
1013 pstrcpy(rname,cur_dir);
1014 pstrcat(rname,lname);
1015 if (!cli_chkpath(cli, rname) && !do_mkdir(rname)) {
1017 if (!seek_list(f,lname))
1023 slprintf(quest,sizeof(quest)-1,
1024 "Put file %s? ",lname);
1025 if (prompt && !yesno(quest)) continue;
1027 pstrcpy(rname,cur_dir);
1028 pstrcat(rname,lname);
1033 /* null size so do_put knows to ignore it */
1036 /* set the date on the file */
1037 finfo.mtime = st.st_mtime;
1039 do_put(rname,lname);
1047 /****************************************************************************
1049 ****************************************************************************/
1050 static void do_cancel(int job)
1052 if (cli_printjob_del(cli, job)) {
1053 printf("Job %d cancelled\n",job);
1055 printf("Error calcelling job %d : %s\n",job,cli_errstr(cli));
1060 /****************************************************************************
1062 ****************************************************************************/
1063 static void cmd_cancel(void)
1068 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1069 printf("cancel <jobid> ...\n");
1075 } while (next_token(NULL,buf,NULL,sizeof(buf)));
1079 /****************************************************************************
1081 ****************************************************************************/
1082 static void cmd_print(void)
1088 if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1089 DEBUG(0,("print <filename>\n"));
1093 pstrcpy(rname,lname);
1094 p = strrchr(rname,'/');
1096 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1099 if (strequal(lname,"-")) {
1100 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1103 do_put(rname, lname);
1107 /****************************************************************************
1108 show a print queue entry
1109 ****************************************************************************/
1110 static void queue_fn(struct print_job_info *p)
1112 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1115 /****************************************************************************
1117 ****************************************************************************/
1118 static void cmd_queue(void)
1120 cli_print_queue(cli, queue_fn);
1123 /****************************************************************************
1125 ****************************************************************************/
1126 static void do_del(file_info *finfo)
1130 pstrcpy(mask,cur_dir);
1131 pstrcat(mask,finfo->name);
1133 if (finfo->mode & aDIR)
1136 if (!cli_unlink(cli, mask)) {
1137 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),CNV_LANG(mask)));
1141 /****************************************************************************
1143 ****************************************************************************/
1144 static void cmd_del(void)
1148 int attribute = aSYSTEM | aHIDDEN;
1153 pstrcpy(mask,cur_dir);
1155 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1156 DEBUG(0,("del <filename>\n"));
1161 do_list(mask, attribute,do_del,False,False);
1165 /****************************************************************************
1167 ****************************************************************************/
1168 static void cmd_rmdir(void)
1173 pstrcpy(mask,cur_dir);
1175 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1176 DEBUG(0,("rmdir <dirname>\n"));
1181 if (!cli_rmdir(cli, mask)) {
1182 DEBUG(0,("%s removing remote directory file %s\n",
1183 cli_errstr(cli),CNV_LANG(mask)));
1187 /****************************************************************************
1189 ****************************************************************************/
1190 static void cmd_rename(void)
1195 pstrcpy(src,cur_dir);
1196 pstrcpy(dest,cur_dir);
1198 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1199 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1200 DEBUG(0,("rename <src> <dest>\n"));
1207 if (!cli_rename(cli, src, dest)) {
1208 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1214 /****************************************************************************
1215 toggle the prompt flag
1216 ****************************************************************************/
1217 static void cmd_prompt(void)
1220 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1224 /****************************************************************************
1225 set the newer than time
1226 ****************************************************************************/
1227 static void cmd_newer(void)
1231 SMB_STRUCT_STAT sbuf;
1233 ok = next_token(NULL,buf,NULL,sizeof(buf));
1234 if (ok && (dos_stat(buf,&sbuf) == 0)) {
1235 newer_than = sbuf.st_mtime;
1236 DEBUG(1,("Getting files newer than %s",
1237 asctime(LocalTime(&newer_than))));
1242 if (ok && newer_than == 0)
1243 DEBUG(0,("Error setting newer-than time\n"));
1246 /****************************************************************************
1247 set the archive level
1248 ****************************************************************************/
1249 static void cmd_archive(void)
1253 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1254 archive_level = atoi(buf);
1256 DEBUG(0,("Archive level is %d\n",archive_level));
1259 /****************************************************************************
1260 toggle the lowercaseflag
1261 ****************************************************************************/
1262 static void cmd_lowercase(void)
1264 lowercase = !lowercase;
1265 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1271 /****************************************************************************
1272 toggle the recurse flag
1273 ****************************************************************************/
1274 static void cmd_recurse(void)
1277 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1280 /****************************************************************************
1281 toggle the translate flag
1282 ****************************************************************************/
1283 static void cmd_translate(void)
1285 translation = !translation;
1286 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1287 translation?"on":"off"));
1291 /****************************************************************************
1292 do a printmode command
1293 ****************************************************************************/
1294 static void cmd_printmode(void)
1299 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1300 if (strequal(buf,"text")) {
1303 if (strequal(buf,"graphics"))
1306 printmode = atoi(buf);
1313 fstrcpy(mode,"text");
1316 fstrcpy(mode,"graphics");
1319 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1323 DEBUG(2,("the printmode is now %s\n",mode));
1326 /****************************************************************************
1328 ****************************************************************************/
1329 static void cmd_lcd(void)
1334 if (next_token(NULL,buf,NULL,sizeof(buf)))
1336 DEBUG(2,("the local directory is now %s\n",GetWd(d)));
1339 /****************************************************************************
1341 ****************************************************************************/
1342 static void browse_fn(const char *name, uint32 m, const char *comment)
1349 case STYPE_DISKTREE:
1350 fstrcpy(typestr,"Disk"); break;
1352 fstrcpy(typestr,"Printer"); break;
1354 fstrcpy(typestr,"Device"); break;
1356 fstrcpy(typestr,"IPC"); break;
1359 printf("\t%-15.15s%-10.10s%s\n",
1360 name, typestr,comment);
1364 /****************************************************************************
1365 try and browse available connections on a host
1366 ****************************************************************************/
1367 static BOOL browse_host(BOOL sort)
1369 printf("\n\tSharename Type Comment\n");
1370 printf("\t--------- ---- -------\n");
1372 return cli_RNetShareEnum(cli, browse_fn);
1375 /****************************************************************************
1377 ****************************************************************************/
1378 static void server_fn(const char *name, uint32 m, const char *comment)
1380 printf("\t%-16.16s %s\n", name, comment);
1383 /****************************************************************************
1384 try and browse available connections on a host
1385 ****************************************************************************/
1386 static BOOL list_servers(char *wk_grp)
1388 printf("\n\tServer Comment\n");
1389 printf("\t--------- -------\n");
1391 cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, server_fn);
1393 printf("\n\tWorkgroup Master\n");
1394 printf("\t--------- -------\n");
1396 cli_NetServerEnum(cli, workgroup, SV_TYPE_DOMAIN_ENUM, server_fn);
1401 /* Some constants for completing filename arguments */
1403 #define COMPL_NONE 0 /* No completions */
1404 #define COMPL_REMOTE 1 /* Complete remote filename */
1405 #define COMPL_LOCAL 2 /* Complete local filename */
1407 /* This defines the commands supported by this client */
1413 char compl_args[2]; /* Completion argument info */
1416 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1417 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1418 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1419 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1420 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1421 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1422 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1423 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1424 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1425 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1426 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1427 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1428 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1429 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1430 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1431 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1432 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1433 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1434 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1435 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1436 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1437 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1438 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1439 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1440 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1441 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1442 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1443 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1444 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1445 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1446 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1447 {"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}},
1448 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1449 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1450 {"tarmode",cmd_tarmode,
1451 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1452 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1453 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1454 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1455 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1456 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1460 /*******************************************************************
1461 lookup a command string in the list of commands, including
1463 ******************************************************************/
1464 static int process_tok(fstring tok)
1466 int i = 0, matches = 0;
1468 int tok_len = strlen(tok);
1470 while (commands[i].fn != NULL) {
1471 if (strequal(commands[i].name,tok)) {
1475 } else if (strnequal(commands[i].name, tok, tok_len)) {
1484 else if (matches == 1)
1490 /****************************************************************************
1492 ****************************************************************************/
1493 static void cmd_help(void)
1498 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1499 if ((i = process_tok(buf)) >= 0)
1500 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1502 while (commands[i].description) {
1503 for (j=0; commands[i].description && (j<5); j++) {
1504 DEBUG(0,("%-15s",commands[i].name));
1512 /****************************************************************************
1513 wait for keyboard activity, swallowing network packets
1514 ****************************************************************************/
1515 static void wait_keyboard(void)
1518 struct timeval timeout;
1522 FD_SET(cli->fd,&fds);
1523 FD_SET(fileno(stdin),&fds);
1525 timeout.tv_sec = 20;
1526 timeout.tv_usec = 0;
1527 sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
1529 if (FD_ISSET(fileno(stdin),&fds))
1532 /* We deliberately use receive_smb instead of
1533 client_receive_smb as we want to receive
1534 session keepalives and then drop them here.
1536 if (FD_ISSET(cli->fd,&fds))
1537 receive_smb(cli->fd,cli->inbuf,0);
1539 cli_chkpath(cli, "\\");
1543 /****************************************************************************
1544 process a -c command string
1545 ****************************************************************************/
1546 static void process_command_string(char *cmd)
1551 while (cmd[0] != '\0') {
1556 if ((p = strchr(cmd, ';')) == 0) {
1557 strncpy(line, cmd, 999);
1561 if (p - cmd > 999) p = cmd + 999;
1562 strncpy(line, cmd, p - cmd);
1563 line[p - cmd] = '\0';
1567 /* input language code to internal one */
1570 /* and get the first part of the command */
1572 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1574 if ((i = process_tok(tok)) >= 0) {
1576 } else if (i == -2) {
1577 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1579 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1584 /****************************************************************************
1585 process commands on stdin
1586 ****************************************************************************/
1587 static void process_stdin(void)
1592 while (!feof(stdin)) {
1596 /* display a prompt */
1597 DEBUG(0,("smb: %s> ", CNV_LANG(cur_dir)));
1602 /* and get a response */
1603 if (!fgets(line,1000,stdin))
1606 /* input language code to internal one */
1609 /* special case - first char is ! */
1615 /* and get the first part of the command */
1617 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1619 if ((i = process_tok(tok)) >= 0) {
1621 } else if (i == -2) {
1622 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1624 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1630 /*****************************************************
1631 return a connection to a server
1632 *******************************************************/
1633 struct cli_state *do_connect(char *server, char *share)
1635 struct cli_state *c;
1636 struct nmb_name called, calling;
1639 extern struct in_addr ipzero;
1641 if (*share == '\\') {
1643 share = strchr(server,'\\');
1644 if (!share) return NULL;
1655 make_nmb_name(&calling, global_myname, 0x0, "");
1656 make_nmb_name(&called , server, name_type, "");
1660 if (have_ip) ip = dest_ip;
1662 /* have to open a new connection */
1663 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
1664 DEBUG(0,("Connection to %s failed\n", server_n));
1668 if (!cli_session_request(c, &calling, &called)) {
1669 DEBUG(0,("session request to %s failed\n", called.name));
1671 if (strcmp(called.name, "*SMBSERVER")) {
1672 make_nmb_name(&called , "*SMBSERVER", 0x20, "");
1678 DEBUG(4,(" session request ok\n"));
1680 if (!cli_negprot(c)) {
1681 DEBUG(0,("protocol negotiation failed\n"));
1687 char *pass = getpass("Password: ");
1689 pstrcpy(password, pass);
1693 if (!cli_session_setup(c, username,
1694 password, strlen(password),
1695 password, strlen(password),
1697 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
1701 DEBUG(4,(" session setup ok\n"));
1703 if (!cli_send_tconX(c, share, "?????",
1704 password, strlen(password)+1)) {
1705 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
1710 DEBUG(4,(" tconx ok\n"));
1716 /****************************************************************************
1717 process commands from the client
1718 ****************************************************************************/
1719 static BOOL process(char *base_directory)
1721 cli = do_connect(desthost, service);
1727 if (*base_directory) do_cd(base_directory);
1730 process_command_string(cmdstr);
1739 /****************************************************************************
1740 usage on the program
1741 ****************************************************************************/
1742 static void usage(char *pname)
1744 DEBUG(0,("Usage: %s service <password> ", pname));
1746 DEBUG(0,("\nVersion %s\n",VERSION));
1747 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
1748 DEBUG(0,("\t-B IP addr broadcast IP address to use\n"));
1749 DEBUG(0,("\t-O socket_options socket options to use\n"));
1750 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
1751 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
1752 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
1753 DEBUG(0,("\t-N don't ask for a password\n"));
1754 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
1755 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
1756 DEBUG(0,("\t-P connect to service as a printer\n"));
1757 DEBUG(0,("\t-p port connect to the specified port\n"));
1758 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
1759 DEBUG(0,("\t-h Print this help message.\n"));
1760 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
1761 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
1762 DEBUG(0,("\t-U username set the network username\n"));
1763 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
1764 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
1765 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
1766 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
1767 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
1768 DEBUG(0,("\t-D directory start from directory\n"));
1769 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
1774 /****************************************************************************
1775 get a password from a a file or file descriptor
1777 ****************************************************************************/
1778 static void get_password_file(void)
1782 BOOL close_it = False;
1786 if ((p = getenv("PASSWD_FD")) != NULL) {
1787 pstrcpy(spec, "descriptor ");
1789 sscanf(p, "%d", &fd);
1791 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
1792 fd = open(p, O_RDONLY);
1795 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
1796 spec, strerror(errno));
1802 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1803 p && p - pass < sizeof(pass);) {
1804 switch (read(fd, p, 1)) {
1806 if (*p != '\n' && *p != '\0') {
1807 *++p = '\0'; /* advance p, and null-terminate pass */
1812 *p = '\0'; /* null-terminate it, just in case... */
1813 p = NULL; /* then force the loop condition to become false */
1816 fprintf(stderr, "Error reading password from file %s: %s\n",
1817 spec, "empty password\n");
1822 fprintf(stderr, "Error reading password from file %s: %s\n",
1823 spec, strerror(errno));
1827 pstrcpy(password, pass);
1834 /****************************************************************************
1836 ****************************************************************************/
1837 static int do_host_query(char *query_host, int port)
1839 cli = do_connect(query_host, "IPC$");
1843 list_servers(workgroup);
1851 /****************************************************************************
1852 handle a tar operation
1853 ****************************************************************************/
1854 static int do_tar_op(int port, char *base_directory)
1857 cli = do_connect(desthost, service);
1861 if (*base_directory) do_cd(base_directory);
1870 /****************************************************************************
1871 handle a message operation
1872 ****************************************************************************/
1873 static int do_message_op(void)
1876 struct nmb_name called, calling;
1882 make_nmb_name(&calling, global_myname, 0x0, "");
1883 make_nmb_name(&called , desthost, name_type, "");
1886 if (have_ip) ip = dest_ip;
1888 if (!(cli=cli_initialise(NULL)) || !cli_connect(cli, desthost, &ip)) {
1889 DEBUG(0,("Connection to %s failed\n", desthost));
1893 if (!cli_session_request(cli, &calling, &called)) {
1894 DEBUG(0,("session request failed\n"));
1906 /****************************************************************************
1908 ****************************************************************************/
1909 int main(int argc,char *argv[])
1911 fstring base_directory;
1912 char *pname = argv[0];
1913 int port = SMB_PORT;
1916 extern char *optarg;
1919 BOOL message = False;
1920 BOOL explicit_user = False;
1921 extern char tar_type;
1922 static pstring servicesf = CONFIGFILE;
1924 pstring new_name_resolve_order;
1928 pstrcpy(term_code, KANJI);
1934 *base_directory = 0;
1936 *new_name_resolve_order = 0;
1940 setup_logging(pname,True);
1943 charset_initialise();
1945 if(!get_myname(myhostname,NULL)) {
1946 DEBUG(0,("Failed to get my hostname.\n"));
1949 in_client = True; /* Make sure that we tell lp_load we are */
1951 if (!lp_load(servicesf,True,False,False)) {
1952 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
1955 codepage_initialise(lp_client_code_page());
1957 interpret_coding_system(term_code);
1963 pstrcpy(workgroup,lp_workgroup());
1969 if (getenv("USER")) {
1970 pstrcpy(username,getenv("USER"));
1972 /* modification to support userid%passwd syntax in the USER var
1973 25.Aug.97, jdblair@uab.edu */
1975 if ((p=strchr(username,'%'))) {
1977 pstrcpy(password,p+1);
1979 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
1984 /* modification to support PASSWD environmental var
1985 25.Aug.97, jdblair@uab.edu */
1986 if (getenv("PASSWD")) {
1987 pstrcpy(password,getenv("PASSWD"));
1991 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
1992 get_password_file();
1996 if (*username == 0 && getenv("LOGNAME")) {
1997 pstrcpy(username,getenv("LOGNAME"));
2001 if (*username == 0) {
2002 pstrcpy(username,"GUEST");
2010 if (*argv[1] != '-') {
2011 pstrcpy(service,argv[1]);
2012 /* Convert any '/' characters in the service name to '\' characters */
2013 string_replace( service, '/','\\');
2017 if (count_chars(service,'\\') < 3) {
2019 printf("\n%s: Not enough '\\' characters in service\n",service);
2023 if (argc > 1 && (*argv[1] != '-')) {
2025 pstrcpy(password,argv[1]);
2026 memset(argv[1],'X',strlen(argv[1]));
2033 getopt(argc, argv,"s:B:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:")) != EOF) {
2036 pstrcpy(servicesf, optarg);
2039 iface_set_default(NULL,optarg,NULL);
2042 pstrcpy(user_socket_options,optarg);
2045 pstrcpy(new_name_resolve_order, optarg);
2048 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2049 pstrcpy(desthost,optarg);
2054 pstrcpy(scope,optarg);
2061 pstrcpy(global_myname,optarg);
2067 DEBUGLEVEL = atoi(optarg);
2070 /* not needed anymore */
2073 port = atoi(optarg);
2076 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2084 dest_ip = *interpret_addr2(optarg);
2085 if (zero_ip(dest_ip))
2096 explicit_user = True;
2097 pstrcpy(username,optarg);
2098 if ((lp=strchr(username,'%'))) {
2100 pstrcpy(password,lp+1);
2102 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);
2141 strupper(global_myname);
2143 if(*new_name_resolve_order)
2144 lp_set_name_resolve_order(new_name_resolve_order);
2146 if (!tar_type && !*query_host && !*service && !message) {
2151 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2154 return do_tar_op(port, base_directory);
2157 if ((p=strchr(query_host,'#'))) {
2160 sscanf(p, "%x", &name_type);
2164 return do_host_query(query_host, port);
2168 return do_message_op();
2171 if (!process(base_directory)) {