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;
43 extern struct in_addr ipzero;
46 static int name_type = 0x20;
48 extern pstring user_socket_options;
50 static int process_tok(fstring tok);
51 static void cmd_help(void);
53 /* 30 second timeout on most commands */
54 #define CLIENT_TIMEOUT (30*1000)
55 #define SHORT_TIMEOUT (5*1000)
57 /* value for unused fid field in trans2 secondary request */
58 #define FID_UNUSED (0xFFFF)
60 time_t newer_than = 0;
61 int archive_level = 0;
63 extern pstring debugf;
64 extern int DEBUGLEVEL;
66 BOOL translation = False;
70 /* clitar bits insert */
73 extern BOOL tar_reset;
77 mode_t myumask = 0755;
83 static BOOL recurse = False;
84 BOOL lowercase = False;
86 struct in_addr dest_ip;
88 #define SEPARATORS " \t\n\r"
90 BOOL abort_mget = True;
92 pstring fileselection = "";
94 extern file_info def_finfo;
97 int get_total_size = 0;
98 int get_total_time_ms = 0;
99 int put_total_size = 0;
100 int put_total_time_ms = 0;
107 #define CNV_LANG(s) dos_to_unix(s,False)
108 #define CNV_INPUT(s) unix_to_dos(s,True)
111 /****************************************************************************
112 write to a local file with CR/LF->LF translation if appropriate. return the
113 number taken from the buffer. This may not equal the number written.
114 ****************************************************************************/
115 static int writefile(int f, char *b, int n)
125 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
128 if (write(f, b, 1) != 1) {
138 /****************************************************************************
139 read from a file with LF->CR/LF translation if appropriate. return the
140 number read. read approx n bytes.
141 ****************************************************************************/
142 static int readfile(char *b, int size, int n, FILE *f)
147 if (!translation || (size != 1))
148 return(fread(b,size,n,f));
152 if ((c = getc(f)) == EOF) {
156 if (c == '\n') { /* change all LFs to CR/LF */
169 /****************************************************************************
171 ****************************************************************************/
172 static void send_message(void)
177 if (!cli_message_start(cli, desthost, username, &grp_id)) {
178 DEBUG(0,("message start: %s\n", cli_errstr(cli)));
183 printf("Connected. Type your message, ending it with a Control-D\n");
185 while (!feof(stdin) && total_len < 1600) {
186 int maxlen = MIN(1600 - total_len,127);
193 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
199 if (!cli_message_text(cli, msg, l, grp_id)) {
200 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
207 if (total_len >= 1600)
208 printf("the message was truncated to 1600 bytes\n");
210 printf("sent %d bytes\n",total_len);
212 if (!cli_message_end(cli, grp_id)) {
213 printf("SMBsendend failed (%s)\n",cli_errstr(cli));
220 /****************************************************************************
221 check the space on a device
222 ****************************************************************************/
223 static void do_dskattr(void)
225 int total, bsize, avail;
227 if (!cli_dskattr(cli, &bsize, &total, &avail)) {
228 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli)));
232 DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
233 total, bsize, avail));
236 /****************************************************************************
238 ****************************************************************************/
239 static void cmd_pwd(void)
241 DEBUG(0,("Current directory is %s",CNV_LANG(service)));
242 DEBUG(0,("%s\n",CNV_LANG(cur_dir)));
246 /****************************************************************************
247 change directory - inner section
248 ****************************************************************************/
249 static void do_cd(char *newdir)
257 /* Save the current directory in case the
258 new directory is invalid */
259 pstrcpy(saved_dir, cur_dir);
264 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
265 pstrcat(cur_dir, "\\");
267 dos_clean_name(cur_dir);
268 pstrcpy(dname,cur_dir);
269 pstrcat(cur_dir,"\\");
270 dos_clean_name(cur_dir);
272 if (!strequal(cur_dir,"\\")) {
273 if (!cli_chkpath(cli, dname)) {
274 DEBUG(0,("cd %s: %s\n", dname, cli_errstr(cli)));
275 pstrcpy(cur_dir,saved_dir);
279 pstrcpy(cd_path,cur_dir);
282 /****************************************************************************
284 ****************************************************************************/
285 static void cmd_cd(void)
289 if (next_token(NULL,buf,NULL,sizeof(buf)))
292 DEBUG(0,("Current directory is %s\n",CNV_LANG(cur_dir)));
296 /*******************************************************************
297 decide if a file should be operated on
298 ********************************************************************/
299 static BOOL do_this_one(file_info *finfo)
301 if (finfo->mode & aDIR) return(True);
303 if (*fileselection &&
304 !mask_match(finfo->name,fileselection,False,False)) {
305 DEBUG(3,("match_match %s failed\n", finfo->name));
309 if (newer_than && finfo->mtime < newer_than) {
310 DEBUG(3,("newer_than %s failed\n", finfo->name));
314 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
315 DEBUG(3,("archive %s failed\n", finfo->name));
322 /****************************************************************************
323 display info about a file
324 ****************************************************************************/
325 static void display_finfo(file_info *finfo)
327 if (do_this_one(finfo)) {
328 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
329 DEBUG(0,(" %-30s%7.7s%8.0f %s",
330 CNV_LANG(finfo->name),
331 attrib_string(finfo->mode),
333 asctime(LocalTime(&t))));
334 dir_total += finfo->size;
339 /****************************************************************************
340 accumulate size of a file
341 ****************************************************************************/
342 static void do_du(file_info *finfo)
344 if (do_this_one(finfo)) {
345 dir_total += finfo->size;
349 static BOOL do_list_recurse;
350 static BOOL do_list_dirs;
351 static int do_list_attr;
352 static void (*do_list_fn)(file_info *);
354 /****************************************************************************
356 ****************************************************************************/
357 static void do_list_helper(file_info *f, const char *mask)
359 if (f->mode & aDIR) {
360 if (do_list_dirs && do_this_one(f)) {
363 if (do_list_recurse &&
364 !strequal(f->name,".") &&
365 !strequal(f->name,"..")) {
369 pstrcpy(mask2, mask);
370 p = strrchr(mask2,'\\');
373 pstrcat(mask2, f->name);
374 if (do_list_fn == display_finfo) {
375 DEBUG(0,("\n%s\n",CNV_LANG(mask2)));
377 pstrcat(mask2,"\\*");
378 do_list(mask2, do_list_attr, do_list_fn, True, True);
383 if (do_this_one(f)) {
389 /****************************************************************************
390 a wrapper around cli_list that adds recursion
391 ****************************************************************************/
392 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
394 do_list_recurse = rec;
397 do_list_attr = attribute;
399 cli_list(cli, mask, attribute, do_list_helper);
402 /****************************************************************************
403 get a directory listing
404 ****************************************************************************/
405 static void cmd_dir(void)
407 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
413 pstrcpy(mask,cur_dir);
414 if(mask[strlen(mask)-1]!='\\')
417 if (next_token(NULL,buf,NULL,sizeof(buf))) {
428 do_list(mask, attribute, display_finfo, recurse, True);
432 DEBUG(3, ("Total bytes listed: %d\n", dir_total));
436 /****************************************************************************
437 get a directory listing
438 ****************************************************************************/
439 static void cmd_du(void)
441 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
447 pstrcpy(mask,cur_dir);
448 if(mask[strlen(mask)-1]!='\\')
451 if (next_token(NULL,buf,NULL,sizeof(buf))) {
461 do_list(mask, attribute, do_du, recurse, True);
465 DEBUG(0, ("Total number of bytes: %d\n", dir_total));
469 /****************************************************************************
470 get a file from rname to lname
471 ****************************************************************************/
472 static void do_get(char *rname,char *lname)
475 BOOL newhandle = False;
477 struct timeval tp_start;
478 int read_size = 65520;
483 GetTimeOfDay(&tp_start);
489 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
492 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
496 if(!strcmp(lname,"-")) {
497 handle = fileno(stdout);
499 handle = open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
503 DEBUG(0,("Error opening local file %s\n",lname));
508 if (!cli_qfileinfo(cli, fnum,
509 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
510 !cli_getattrE(cli, fnum,
511 &attr, &size, NULL, NULL, NULL)) {
512 DEBUG(0,("getattrib: %s\n",cli_errstr(cli)));
516 DEBUG(2,("getting file %s of size %.0f as %s ",
517 lname, (double)size, lname));
519 data = (char *)malloc(read_size);
522 int n = cli_read(cli, fnum, data, nread, read_size);
526 if (writefile(handle,data, n) != n) {
527 DEBUG(0,("Error writing local file\n"));
534 if (!cli_close(cli, fnum)) {
535 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
542 if (archive_level >= 2 && (attr & aARCH)) {
543 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
547 struct timeval tp_end;
550 GetTimeOfDay(&tp_end);
552 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
553 (tp_end.tv_usec - tp_start.tv_usec)/1000;
554 get_total_time_ms += this_time;
555 get_total_size += nread;
557 DEBUG(2,("(%g kb/s) (average %g kb/s)\n",
558 nread / (1.024*this_time + 1.0e-4),
559 get_total_size / (1.024*get_total_time_ms)));
564 /****************************************************************************
566 ****************************************************************************/
567 static void cmd_get(void)
573 pstrcpy(rname,cur_dir);
576 p = rname + strlen(rname);
578 if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
579 DEBUG(0,("get <filename>\n"));
583 dos_clean_name(rname);
585 next_token(NULL,lname,NULL,sizeof(lname));
587 do_get(rname, lname);
591 /****************************************************************************
592 do a mget operation on one file
593 ****************************************************************************/
594 static void do_mget(file_info *finfo)
598 pstring saved_curdir;
601 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
605 DEBUG(0,("mget aborted\n"));
609 if (finfo->mode & aDIR)
610 slprintf(quest,sizeof(pstring)-1,
611 "Get directory %s? ",CNV_LANG(finfo->name));
613 slprintf(quest,sizeof(pstring)-1,
614 "Get file %s? ",CNV_LANG(finfo->name));
616 if (prompt && !yesno(quest)) return;
618 if (!(finfo->mode & aDIR)) {
619 pstrcpy(rname,cur_dir);
620 pstrcat(rname,finfo->name);
621 do_get(rname,finfo->name);
625 /* handle directories */
626 pstrcpy(saved_curdir,cur_dir);
628 pstrcat(cur_dir,finfo->name);
629 pstrcat(cur_dir,"\\");
631 unix_format(finfo->name);
633 strlower(finfo->name);
635 if (!directory_exist(finfo->name,NULL) &&
636 dos_mkdir(finfo->name,0777) != 0) {
637 DEBUG(0,("failed to create directory %s\n",CNV_LANG(finfo->name)));
638 pstrcpy(cur_dir,saved_curdir);
642 if (dos_chdir(finfo->name) != 0) {
643 DEBUG(0,("failed to chdir to directory %s\n",CNV_LANG(finfo->name)));
644 pstrcpy(cur_dir,saved_curdir);
648 pstrcpy(mget_mask,cur_dir);
649 pstrcat(mget_mask,"*");
651 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
653 pstrcpy(cur_dir,saved_curdir);
657 /****************************************************************************
658 view the file using the pager
659 ****************************************************************************/
660 static void cmd_more(void)
662 fstring rname,lname,tmpname,pager_cmd;
665 fstrcpy(rname,cur_dir);
669 "%s/smbmore.%d",tmpdir(),(int)getpid());
670 fstrcpy(lname,tmpname);
672 if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
673 DEBUG(0,("more <filename>\n"));
676 dos_clean_name(rname);
680 pager=getenv("PAGER");
682 slprintf(pager_cmd,sizeof(pager_cmd)-1,
683 "%s %s",(pager? pager:PAGER), tmpname);
690 /****************************************************************************
692 ****************************************************************************/
693 static void cmd_mget(void)
695 uint16 attribute = aSYSTEM | aHIDDEN;
707 while (next_token(NULL,p,NULL,sizeof(buf))) {
708 pstrcpy(mget_mask,cur_dir);
709 if(mget_mask[strlen(mget_mask)-1]!='\\')
710 pstrcat(mget_mask,"\\");
713 pstrcpy(mget_mask,p);
715 pstrcat(mget_mask,p);
716 do_list(mget_mask, attribute,do_mget,False,True);
720 pstrcpy(mget_mask,cur_dir);
721 if(mget_mask[strlen(mget_mask)-1]!='\\')
722 pstrcat(mget_mask,"\\");
723 pstrcat(mget_mask,"*");
724 do_list(mget_mask, attribute,do_mget,False,True);
729 /****************************************************************************
730 make a directory of name "name"
731 ****************************************************************************/
732 static BOOL do_mkdir(char *name)
734 if (!cli_mkdir(cli, name)) {
735 DEBUG(0,("%s making remote directory %s\n",
736 cli_errstr(cli),CNV_LANG(name)));
744 /****************************************************************************
745 make a directory of name "name"
746 ****************************************************************************/
747 static void cmd_quit(void)
754 /****************************************************************************
756 ****************************************************************************/
757 static void cmd_mkdir(void)
763 pstrcpy(mask,cur_dir);
765 if (!next_token(NULL,p,NULL,sizeof(buf))) {
767 DEBUG(0,("mkdir <dirname>\n"));
778 trim_string(ddir,".",NULL);
779 p = strtok(ddir,"/\\");
782 if (!cli_chkpath(cli, ddir2)) {
786 p = strtok(NULL,"/\\");
794 /****************************************************************************
796 ****************************************************************************/
797 static void do_put(char *rname,char *lname)
805 struct timeval tp_start;
806 GetTimeOfDay(&tp_start);
808 fnum = cli_open(cli, rname, O_WRONLY|O_CREAT|O_TRUNC, DENY_NONE);
811 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
815 /* allow files to be piped into smbclient
817 if (!strcmp(lname, "-")) {
819 /* size of file is not known */
821 f = fopen(lname,"r");
825 DEBUG(0,("Error opening local file %s\n",lname));
830 DEBUG(1,("putting file %s as %s ",lname,
833 buf = (char *)malloc(maxwrite);
838 if ((n = readfile(buf,1,n,f)) < 1) {
839 DEBUG(0,("Error reading local file\n"));
843 ret = cli_write(cli, fnum, 0, buf, nread, n);
846 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
853 if (!cli_close(cli, fnum)) {
854 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
865 struct timeval tp_end;
868 GetTimeOfDay(&tp_end);
870 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
871 (tp_end.tv_usec - tp_start.tv_usec)/1000;
872 put_total_time_ms += this_time;
873 put_total_size += nread;
875 DEBUG(1,("(%g kb/s) (average %g kb/s)\n",
876 nread / (1.024*this_time + 1.0e-4),
877 put_total_size / (1.024*put_total_time_ms)));
888 /****************************************************************************
890 ****************************************************************************/
891 static void cmd_put(void)
898 pstrcpy(rname,cur_dir);
901 if (!next_token(NULL,p,NULL,sizeof(buf))) {
902 DEBUG(0,("put <filename>\n"));
907 if (next_token(NULL,p,NULL,sizeof(buf)))
910 pstrcat(rname,lname);
912 dos_clean_name(rname);
916 /* allow '-' to represent stdin
917 jdblair, 24.jun.98 */
918 if (!file_exist(lname,&st) &&
919 (strcmp(lname,"-"))) {
920 DEBUG(0,("%s does not exist\n",lname));
929 /****************************************************************************
930 seek in a directory/file list until you get something that doesn't start with
932 ****************************************************************************/
933 static BOOL seek_list(FILE *f,char *name)
937 if (fscanf(f,"%s",s) != 1) return(False);
938 trim_string(s,"./",NULL);
939 if (strncmp(s,name,strlen(name)) != 0) {
949 /****************************************************************************
950 set the file selection mask
951 ****************************************************************************/
952 static void cmd_select(void)
954 pstrcpy(fileselection,"");
955 next_token(NULL,fileselection,NULL,sizeof(fileselection));
959 /****************************************************************************
961 ****************************************************************************/
962 static void cmd_mput(void)
969 while (next_token(NULL,p,NULL,sizeof(buf))) {
975 slprintf(tmpname,sizeof(pstring)-1,
976 "%s/ls.smb.%d",tmpdir(),(int)getpid());
978 slprintf(cmd,sizeof(pstring)-1,
979 "find . -name \"%s\" -print > %s",p,tmpname);
981 slprintf(cmd,sizeof(pstring)-1,
982 "/bin/ls %s > %s",p,tmpname);
985 f = fopen(tmpname,"r");
991 if (fscanf(f,"%s",lname) != 1) break;
992 trim_string(lname,"./",NULL);
996 /* check if it's a directory */
997 if (directory_exist(lname,&st)) {
998 if (!recurse) continue;
999 slprintf(quest,sizeof(pstring)-1,
1000 "Put directory %s? ",lname);
1001 if (prompt && !yesno(quest)) {
1003 if (!seek_list(f,lname))
1008 pstrcpy(rname,cur_dir);
1009 pstrcat(rname,lname);
1010 if (!cli_chkpath(cli, rname) && !do_mkdir(rname)) {
1012 if (!seek_list(f,lname))
1018 slprintf(quest,sizeof(quest)-1,
1019 "Put file %s? ",lname);
1020 if (prompt && !yesno(quest)) continue;
1022 pstrcpy(rname,cur_dir);
1023 pstrcat(rname,lname);
1028 do_put(rname,lname);
1036 /****************************************************************************
1038 ****************************************************************************/
1039 static void do_cancel(int job)
1041 if (cli_printjob_del(cli, job)) {
1042 printf("Job %d cancelled\n",job);
1044 printf("Error calcelling job %d : %s\n",job,cli_errstr(cli));
1049 /****************************************************************************
1051 ****************************************************************************/
1052 static void cmd_cancel(void)
1057 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1058 printf("cancel <jobid> ...\n");
1064 } while (next_token(NULL,buf,NULL,sizeof(buf)));
1068 /****************************************************************************
1070 ****************************************************************************/
1071 static void cmd_print(void)
1077 if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1078 DEBUG(0,("print <filename>\n"));
1082 pstrcpy(rname,lname);
1083 p = strrchr(rname,'/');
1085 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1088 if (strequal(lname,"-")) {
1089 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1092 do_put(rname, lname);
1096 /****************************************************************************
1097 show a print queue entry
1098 ****************************************************************************/
1099 static void queue_fn(struct print_job_info *p)
1101 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1104 /****************************************************************************
1106 ****************************************************************************/
1107 static void cmd_queue(void)
1109 cli_print_queue(cli, queue_fn);
1112 /****************************************************************************
1114 ****************************************************************************/
1115 static void do_del(file_info *finfo)
1119 pstrcpy(mask,cur_dir);
1120 pstrcat(mask,finfo->name);
1122 if (finfo->mode & aDIR)
1125 if (!cli_unlink(cli, mask)) {
1126 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),CNV_LANG(mask)));
1130 /****************************************************************************
1132 ****************************************************************************/
1133 static void cmd_del(void)
1137 uint16 attribute = aSYSTEM | aHIDDEN;
1142 pstrcpy(mask,cur_dir);
1144 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1145 DEBUG(0,("del <filename>\n"));
1150 do_list(mask, attribute,do_del,False,False);
1154 /****************************************************************************
1156 ****************************************************************************/
1157 static void cmd_rmdir(void)
1162 pstrcpy(mask,cur_dir);
1164 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1165 DEBUG(0,("rmdir <dirname>\n"));
1170 if (!cli_rmdir(cli, mask)) {
1171 DEBUG(0,("%s removing remote directory file %s\n",
1172 cli_errstr(cli),CNV_LANG(mask)));
1176 /****************************************************************************
1178 ****************************************************************************/
1179 static void cmd_rename(void)
1184 pstrcpy(src,cur_dir);
1185 pstrcpy(dest,cur_dir);
1187 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1188 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1189 DEBUG(0,("rename <src> <dest>\n"));
1196 if (!cli_rename(cli, src, dest)) {
1197 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1203 /****************************************************************************
1204 toggle the prompt flag
1205 ****************************************************************************/
1206 static void cmd_prompt(void)
1209 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1213 /****************************************************************************
1214 set the newer than time
1215 ****************************************************************************/
1216 static void cmd_newer(void)
1220 SMB_STRUCT_STAT sbuf;
1222 ok = next_token(NULL,buf,NULL,sizeof(buf));
1223 if (ok && (dos_stat(buf,&sbuf) == 0)) {
1224 newer_than = sbuf.st_mtime;
1225 DEBUG(1,("Getting files newer than %s",
1226 asctime(LocalTime(&newer_than))));
1231 if (ok && newer_than == 0)
1232 DEBUG(0,("Error setting newer-than time\n"));
1235 /****************************************************************************
1236 set the archive level
1237 ****************************************************************************/
1238 static void cmd_archive(void)
1242 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1243 archive_level = atoi(buf);
1245 DEBUG(0,("Archive level is %d\n",archive_level));
1248 /****************************************************************************
1249 toggle the lowercaseflag
1250 ****************************************************************************/
1251 static void cmd_lowercase(void)
1253 lowercase = !lowercase;
1254 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1260 /****************************************************************************
1261 toggle the recurse flag
1262 ****************************************************************************/
1263 static void cmd_recurse(void)
1266 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1269 /****************************************************************************
1270 toggle the translate flag
1271 ****************************************************************************/
1272 static void cmd_translate(void)
1274 translation = !translation;
1275 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1276 translation?"on":"off"));
1280 /****************************************************************************
1281 do a printmode command
1282 ****************************************************************************/
1283 static void cmd_printmode(void)
1288 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1289 if (strequal(buf,"text")) {
1292 if (strequal(buf,"graphics"))
1295 printmode = atoi(buf);
1302 fstrcpy(mode,"text");
1305 fstrcpy(mode,"graphics");
1308 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1312 DEBUG(2,("the printmode is now %s\n",mode));
1315 /****************************************************************************
1317 ****************************************************************************/
1318 static void cmd_lcd(void)
1323 if (next_token(NULL,buf,NULL,sizeof(buf)))
1325 DEBUG(2,("the local directory is now %s\n",GetWd(d)));
1328 /****************************************************************************
1330 ****************************************************************************/
1331 static void browse_fn(const char *name, uint32 m, const char *comment)
1338 case STYPE_DISKTREE:
1339 fstrcpy(typestr,"Disk"); break;
1341 fstrcpy(typestr,"Printer"); break;
1343 fstrcpy(typestr,"Device"); break;
1345 fstrcpy(typestr,"IPC"); break;
1348 printf("\t%-15.15s%-10.10s%s\n",
1349 name, typestr,comment);
1353 /****************************************************************************
1354 try and browse available connections on a host
1355 ****************************************************************************/
1356 static BOOL browse_host(BOOL sort)
1358 printf("\n\tSharename Type Comment\n");
1359 printf("\t--------- ---- -------\n");
1361 return cli_RNetShareEnum(cli, browse_fn);
1364 /****************************************************************************
1366 ****************************************************************************/
1367 static void server_fn(const char *name, uint32 m, const char *comment)
1369 printf("\t%-16.16s %s\n", name, comment);
1372 /****************************************************************************
1373 try and browse available connections on a host
1374 ****************************************************************************/
1375 static BOOL list_servers(char *wk_grp)
1377 printf("\n\tServer Comment\n");
1378 printf("\t--------- -------\n");
1380 cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, server_fn);
1382 printf("\n\tWorkgroup Master\n");
1383 printf("\t--------- -------\n");
1385 cli_NetServerEnum(cli, workgroup, SV_TYPE_DOMAIN_ENUM, server_fn);
1390 /* Some constants for completing filename arguments */
1392 #define COMPL_NONE 0 /* No completions */
1393 #define COMPL_REMOTE 1 /* Complete remote filename */
1394 #define COMPL_LOCAL 2 /* Complete local filename */
1396 /* This defines the commands supported by this client */
1402 char compl_args[2]; /* Completion argument info */
1405 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1406 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1407 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1408 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1409 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1410 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1411 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1412 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1413 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1414 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1415 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1416 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1417 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1418 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1419 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1420 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1421 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1422 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1423 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1424 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1425 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1426 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1427 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1428 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1429 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1430 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1431 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1432 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1433 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1434 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1435 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1436 {"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}},
1437 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1438 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1439 {"tarmode",cmd_tarmode,
1440 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1441 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1442 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1443 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1444 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1445 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1449 /*******************************************************************
1450 lookup a command string in the list of commands, including
1452 ******************************************************************/
1453 static int process_tok(fstring tok)
1455 int i = 0, matches = 0;
1457 int tok_len = strlen(tok);
1459 while (commands[i].fn != NULL) {
1460 if (strequal(commands[i].name,tok)) {
1464 } else if (strnequal(commands[i].name, tok, tok_len)) {
1473 else if (matches == 1)
1479 /****************************************************************************
1481 ****************************************************************************/
1482 static void cmd_help(void)
1487 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1488 if ((i = process_tok(buf)) >= 0)
1489 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1491 while (commands[i].description) {
1492 for (j=0; commands[i].description && (j<5); j++) {
1493 DEBUG(0,("%-15s",commands[i].name));
1501 /****************************************************************************
1502 wait for keyboard activity, swallowing network packets
1503 ****************************************************************************/
1504 static void wait_keyboard(void)
1507 struct timeval timeout;
1511 FD_SET(cli->fd,&fds);
1512 FD_SET(fileno(stdin),&fds);
1514 timeout.tv_sec = 20;
1515 timeout.tv_usec = 0;
1516 sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
1518 if (FD_ISSET(fileno(stdin),&fds))
1521 /* We deliberately use receive_smb instead of
1522 client_receive_smb as we want to receive
1523 session keepalives and then drop them here.
1525 if (FD_ISSET(cli->fd,&fds))
1526 receive_smb(cli->fd,cli->inbuf,0);
1528 cli_chkpath(cli, "\\");
1532 /****************************************************************************
1533 process a -c command string
1534 ****************************************************************************/
1535 static void process_command_string(char *cmd)
1540 while (cmd[0] != '\0') {
1545 if ((p = strchr(cmd, ';')) == 0) {
1546 strncpy(line, cmd, 999);
1550 if (p - cmd > 999) p = cmd + 999;
1551 strncpy(line, cmd, p - cmd);
1552 line[p - cmd] = '\0';
1556 /* input language code to internal one */
1559 /* and get the first part of the command */
1561 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1563 if ((i = process_tok(tok)) >= 0) {
1565 } else if (i == -2) {
1566 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1568 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1573 /****************************************************************************
1574 process commands on stdin
1575 ****************************************************************************/
1576 static void process_stdin(void)
1581 while (!feof(stdin)) {
1585 /* display a prompt */
1586 DEBUG(0,("smb: %s> ", CNV_LANG(cur_dir)));
1591 /* and get a response */
1592 if (!fgets(line,1000,stdin))
1595 /* input language code to internal one */
1598 /* special case - first char is ! */
1604 /* and get the first part of the command */
1606 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1608 if ((i = process_tok(tok)) >= 0) {
1610 } else if (i == -2) {
1611 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1613 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1619 /*****************************************************
1620 return a connection to a server
1621 *******************************************************/
1622 struct cli_state *do_connect(char *server, char *share)
1624 struct cli_state *c;
1625 struct nmb_name called, calling;
1628 extern struct in_addr ipzero;
1630 if (*share == '\\') {
1632 share = strchr(server,'\\');
1633 if (!share) return NULL;
1642 make_nmb_name(&calling, global_myname, 0x0, "");
1643 make_nmb_name(&called , server, name_type, "");
1647 if (have_ip) ip = dest_ip;
1649 /* have to open a new connection */
1650 if (!(c=cli_initialise(NULL)) || !cli_connect(c, server_n, &ip)) {
1651 DEBUG(0,("Connection to %s failed\n", server_n));
1655 if (!cli_session_request(c, &calling, &called)) {
1656 DEBUG(0,("session request to %s failed\n", called.name));
1658 if (strcmp(called.name, "*SMBSERVER")) {
1659 make_nmb_name(&called , "*SMBSERVER", 0x20, "");
1665 DEBUG(4,(" session request ok\n"));
1667 if (!cli_negprot(c)) {
1668 DEBUG(0,("protocol negotiation failed\n"));
1674 char *pass = getpass("Password: ");
1676 pstrcpy(password, pass);
1680 if (!cli_session_setup(c, username,
1681 password, strlen(password),
1682 password, strlen(password),
1684 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
1689 * These next two lines are needed to emulate
1690 * old client behaviour for people who have
1691 * scripts based on client output.
1692 * QUESTION ? Do we want to have a 'client compatibility
1693 * mode to turn these on/off ? JRA.
1696 if (*c->server_domain || *c->server_os || *c->server_type)
1697 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
1698 c->server_domain,c->server_os,c->server_type));
1700 DEBUG(4,(" session setup ok\n"));
1702 if (!cli_send_tconX(c, share, "?????",
1703 password, strlen(password)+1)) {
1704 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
1709 DEBUG(4,(" tconx ok\n"));
1715 /****************************************************************************
1716 process commands from the client
1717 ****************************************************************************/
1718 static BOOL process(char *base_directory)
1720 cli = do_connect(desthost, service);
1726 if (*base_directory) do_cd(base_directory);
1729 process_command_string(cmdstr);
1738 /****************************************************************************
1739 usage on the program
1740 ****************************************************************************/
1741 static void usage(char *pname)
1743 DEBUG(0,("Usage: %s service <password> ", pname));
1745 DEBUG(0,("\nVersion %s\n",VERSION));
1746 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
1747 DEBUG(0,("\t-B IP addr broadcast IP address to use\n"));
1748 DEBUG(0,("\t-O socket_options socket options to use\n"));
1749 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
1750 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
1751 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
1752 DEBUG(0,("\t-N don't ask for a password\n"));
1753 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
1754 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
1755 DEBUG(0,("\t-P connect to service as a printer\n"));
1756 DEBUG(0,("\t-p port connect to the specified port\n"));
1757 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
1758 DEBUG(0,("\t-h Print this help message.\n"));
1759 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
1760 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
1761 DEBUG(0,("\t-U username set the network username\n"));
1762 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
1763 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
1764 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
1765 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
1766 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
1767 DEBUG(0,("\t-D directory start from directory\n"));
1768 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
1773 /****************************************************************************
1774 get a password from a a file or file descriptor
1776 ****************************************************************************/
1777 static void get_password_file(void)
1781 BOOL close_it = False;
1785 if ((p = getenv("PASSWD_FD")) != NULL) {
1786 pstrcpy(spec, "descriptor ");
1788 sscanf(p, "%d", &fd);
1790 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
1791 fd = open(p, O_RDONLY);
1794 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
1795 spec, strerror(errno));
1801 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1802 p && p - pass < sizeof(pass);) {
1803 switch (read(fd, p, 1)) {
1805 if (*p != '\n' && *p != '\0') {
1806 *++p = '\0'; /* advance p, and null-terminate pass */
1811 *p = '\0'; /* null-terminate it, just in case... */
1812 p = NULL; /* then force the loop condition to become false */
1815 fprintf(stderr, "Error reading password from file %s: %s\n",
1816 spec, "empty password\n");
1821 fprintf(stderr, "Error reading password from file %s: %s\n",
1822 spec, strerror(errno));
1826 pstrcpy(password, pass);
1833 /****************************************************************************
1835 ****************************************************************************/
1836 static int do_host_query(char *query_host, int port)
1838 cli = do_connect(query_host, "IPC$");
1842 list_servers(workgroup);
1850 /****************************************************************************
1851 handle a tar operation
1852 ****************************************************************************/
1853 static int do_tar_op(int port, char *base_directory)
1856 cli = do_connect(desthost, service);
1860 if (*base_directory) do_cd(base_directory);
1869 /****************************************************************************
1870 handle a message operation
1871 ****************************************************************************/
1872 static int do_message_op(void)
1875 struct nmb_name called, calling;
1879 make_nmb_name(&calling, global_myname, 0x0, "");
1880 make_nmb_name(&called , desthost, name_type, "");
1883 if (have_ip) ip = dest_ip;
1885 if (!(cli=cli_initialise(NULL)) || !cli_connect(cli, desthost, &ip)) {
1886 DEBUG(0,("Connection to %s failed\n", desthost));
1890 if (!cli_session_request(cli, &calling, &called)) {
1891 DEBUG(0,("session request failed\n"));
1903 /****************************************************************************
1905 ****************************************************************************/
1906 int main(int argc,char *argv[])
1908 fstring base_directory;
1909 char *pname = argv[0];
1910 int port = SMB_PORT;
1913 extern char *optarg;
1916 BOOL message = False;
1917 BOOL explicit_user = False;
1918 extern char tar_type;
1919 static pstring servicesf = CONFIGFILE;
1921 pstring new_name_resolve_order;
1925 pstrcpy(term_code, KANJI);
1931 *base_directory = 0;
1933 *new_name_resolve_order = 0;
1937 setup_logging(pname,True);
1940 charset_initialise();
1942 if(!get_myname(myhostname,NULL)) {
1943 DEBUG(0,("Failed to get my hostname.\n"));
1946 in_client = True; /* Make sure that we tell lp_load we are */
1948 if (!lp_load(servicesf,True,False,False)) {
1949 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
1952 codepage_initialise(lp_client_code_page());
1954 interpret_coding_system(term_code);
1960 pstrcpy(workgroup,lp_workgroup());
1966 if (getenv("USER")) {
1967 pstrcpy(username,getenv("USER"));
1969 /* modification to support userid%passwd syntax in the USER var
1970 25.Aug.97, jdblair@uab.edu */
1972 if ((p=strchr(username,'%'))) {
1974 pstrcpy(password,p+1);
1976 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
1981 /* modification to support PASSWD environmental var
1982 25.Aug.97, jdblair@uab.edu */
1983 if (getenv("PASSWD")) {
1984 pstrcpy(password,getenv("PASSWD"));
1988 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
1989 get_password_file();
1993 if (*username == 0 && getenv("LOGNAME")) {
1994 pstrcpy(username,getenv("LOGNAME"));
1998 if (*username == 0) {
1999 pstrcpy(username,"GUEST");
2007 if (*argv[1] != '-') {
2008 pstrcpy(service,argv[1]);
2009 /* Convert any '/' characters in the service name to '\' characters */
2010 string_replace( service, '/','\\');
2014 if (count_chars(service,'\\') < 3) {
2016 printf("\n%s: Not enough '\\' characters in service\n",service);
2020 if (argc > 1 && (*argv[1] != '-')) {
2022 pstrcpy(password,argv[1]);
2023 memset(argv[1],'X',strlen(argv[1]));
2030 getopt(argc, argv,"s:B:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:")) != EOF) {
2033 pstrcpy(servicesf, optarg);
2036 iface_set_default(NULL,optarg,NULL);
2039 pstrcpy(user_socket_options,optarg);
2042 pstrcpy(new_name_resolve_order, optarg);
2045 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2046 pstrcpy(desthost,optarg);
2050 pstrcpy(scope,optarg);
2056 pstrcpy(global_myname,optarg);
2062 DEBUGLEVEL = atoi(optarg);
2065 /* not needed anymore */
2068 port = atoi(optarg);
2071 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2079 dest_ip = *interpret_addr2(optarg);
2080 if (zero_ip(dest_ip))
2091 explicit_user = True;
2092 pstrcpy(username,optarg);
2093 if ((lp=strchr(username,'%'))) {
2095 pstrcpy(password,lp+1);
2097 memset(strchr(optarg,'%')+1,'X',strlen(password));
2102 pstrcpy(query_host,optarg);
2107 pstrcpy(term_code, optarg);
2110 /* no longer supported */
2113 pstrcpy(workgroup,optarg);
2116 if (!tar_parseargs(argc, argv, optarg, optind)) {
2122 pstrcpy(base_directory,optarg);
2134 get_myname((*global_myname)?NULL:global_myname,NULL);
2136 if(*new_name_resolve_order)
2137 lp_set_name_resolve_order(new_name_resolve_order);
2139 if (!tar_type && !*query_host && !*service && !message) {
2144 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2147 return do_tar_op(port, base_directory);
2150 if ((p=strchr(query_host,'#'))) {
2153 sscanf(p, "%x", &name_type);
2157 return do_host_query(query_host, port);
2161 return do_message_op();
2164 if (!process(base_directory)) {