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_getattrE(cli, fnum, &attr, &size, NULL, NULL, NULL)) {
509 DEBUG(0,("getattrE: %s\n",cli_errstr(cli)));
514 DEBUG(2,("getting file %s of size %.0f as %s ",
515 lname, (double)size, lname));
517 data = (char *)malloc(read_size);
520 int n = cli_read(cli, fnum, data, nread, read_size);
524 if (writefile(handle,data, n) != n) {
525 DEBUG(0,("Error writing local file\n"));
532 if (!cli_close(cli, fnum)) {
533 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
540 if (archive_level >= 2 && (attr & aARCH)) {
541 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
545 struct timeval tp_end;
548 GetTimeOfDay(&tp_end);
550 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
551 (tp_end.tv_usec - tp_start.tv_usec)/1000;
552 get_total_time_ms += this_time;
553 get_total_size += nread;
555 DEBUG(2,("(%g kb/s) (average %g kb/s)\n",
556 nread / (1.024*this_time + 1.0e-4),
557 get_total_size / (1.024*get_total_time_ms)));
562 /****************************************************************************
564 ****************************************************************************/
565 static void cmd_get(void)
571 pstrcpy(rname,cur_dir);
574 p = rname + strlen(rname);
576 if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
577 DEBUG(0,("get <filename>\n"));
581 dos_clean_name(rname);
583 next_token(NULL,lname,NULL,sizeof(lname));
585 do_get(rname, lname);
589 /****************************************************************************
590 do a mget operation on one file
591 ****************************************************************************/
592 static void do_mget(file_info *finfo)
596 pstring saved_curdir;
599 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
603 DEBUG(0,("mget aborted\n"));
607 if (finfo->mode & aDIR)
608 slprintf(quest,sizeof(pstring)-1,
609 "Get directory %s? ",CNV_LANG(finfo->name));
611 slprintf(quest,sizeof(pstring)-1,
612 "Get file %s? ",CNV_LANG(finfo->name));
614 if (prompt && !yesno(quest)) return;
616 if (!(finfo->mode & aDIR)) {
617 pstrcpy(rname,cur_dir);
618 pstrcat(rname,finfo->name);
619 do_get(rname,finfo->name);
623 /* handle directories */
624 pstrcpy(saved_curdir,cur_dir);
626 pstrcat(cur_dir,finfo->name);
627 pstrcat(cur_dir,"\\");
629 unix_format(finfo->name);
631 strlower(finfo->name);
633 if (!directory_exist(finfo->name,NULL) &&
634 dos_mkdir(finfo->name,0777) != 0) {
635 DEBUG(0,("failed to create directory %s\n",CNV_LANG(finfo->name)));
636 pstrcpy(cur_dir,saved_curdir);
640 if (dos_chdir(finfo->name) != 0) {
641 DEBUG(0,("failed to chdir to directory %s\n",CNV_LANG(finfo->name)));
642 pstrcpy(cur_dir,saved_curdir);
646 pstrcpy(mget_mask,cur_dir);
647 pstrcat(mget_mask,"*");
649 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
651 pstrcpy(cur_dir,saved_curdir);
655 /****************************************************************************
656 view the file using the pager
657 ****************************************************************************/
658 static void cmd_more(void)
660 fstring rname,lname,tmpname,pager_cmd;
663 fstrcpy(rname,cur_dir);
667 "%s/smbmore.%d",tmpdir(),(int)getpid());
668 fstrcpy(lname,tmpname);
670 if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
671 DEBUG(0,("more <filename>\n"));
674 dos_clean_name(rname);
678 pager=getenv("PAGER");
680 slprintf(pager_cmd,sizeof(pager_cmd)-1,
681 "%s %s",(pager? pager:PAGER), tmpname);
688 /****************************************************************************
690 ****************************************************************************/
691 static void cmd_mget(void)
693 uint16 attribute = aSYSTEM | aHIDDEN;
705 while (next_token(NULL,p,NULL,sizeof(buf))) {
706 pstrcpy(mget_mask,cur_dir);
707 if(mget_mask[strlen(mget_mask)-1]!='\\')
708 pstrcat(mget_mask,"\\");
711 pstrcpy(mget_mask,p);
713 pstrcat(mget_mask,p);
714 do_list(mget_mask, attribute,do_mget,False,True);
718 pstrcpy(mget_mask,cur_dir);
719 if(mget_mask[strlen(mget_mask)-1]!='\\')
720 pstrcat(mget_mask,"\\");
721 pstrcat(mget_mask,"*");
722 do_list(mget_mask, attribute,do_mget,False,True);
727 /****************************************************************************
728 make a directory of name "name"
729 ****************************************************************************/
730 static BOOL do_mkdir(char *name)
732 if (!cli_mkdir(cli, name)) {
733 DEBUG(0,("%s making remote directory %s\n",
734 cli_errstr(cli),CNV_LANG(name)));
742 /****************************************************************************
743 make a directory of name "name"
744 ****************************************************************************/
745 static void cmd_quit(void)
752 /****************************************************************************
754 ****************************************************************************/
755 static void cmd_mkdir(void)
761 pstrcpy(mask,cur_dir);
763 if (!next_token(NULL,p,NULL,sizeof(buf))) {
765 DEBUG(0,("mkdir <dirname>\n"));
776 trim_string(ddir,".",NULL);
777 p = strtok(ddir,"/\\");
780 if (!cli_chkpath(cli, ddir2)) {
784 p = strtok(NULL,"/\\");
792 /****************************************************************************
794 ****************************************************************************/
795 static void do_put(char *rname,char *lname)
803 struct timeval tp_start;
804 GetTimeOfDay(&tp_start);
806 fnum = cli_open(cli, rname, O_WRONLY|O_CREAT|O_TRUNC, DENY_NONE);
809 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
813 /* allow files to be piped into smbclient
815 if (!strcmp(lname, "-")) {
817 /* size of file is not known */
819 f = fopen(lname,"r");
823 DEBUG(0,("Error opening local file %s\n",lname));
828 DEBUG(1,("putting file %s as %s ",lname,
831 buf = (char *)malloc(maxwrite);
836 if ((n = readfile(buf,1,n,f)) < 1) {
837 DEBUG(0,("Error reading local file\n"));
841 ret = cli_write(cli, fnum, 0, buf, nread, n);
844 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
851 if (!cli_close(cli, fnum)) {
852 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
863 struct timeval tp_end;
866 GetTimeOfDay(&tp_end);
868 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
869 (tp_end.tv_usec - tp_start.tv_usec)/1000;
870 put_total_time_ms += this_time;
871 put_total_size += nread;
873 DEBUG(1,("(%g kb/s) (average %g kb/s)\n",
874 nread / (1.024*this_time + 1.0e-4),
875 put_total_size / (1.024*put_total_time_ms)));
886 /****************************************************************************
888 ****************************************************************************/
889 static void cmd_put(void)
896 pstrcpy(rname,cur_dir);
899 if (!next_token(NULL,p,NULL,sizeof(buf))) {
900 DEBUG(0,("put <filename>\n"));
905 if (next_token(NULL,p,NULL,sizeof(buf)))
908 pstrcat(rname,lname);
910 dos_clean_name(rname);
914 /* allow '-' to represent stdin
915 jdblair, 24.jun.98 */
916 if (!file_exist(lname,&st) &&
917 (strcmp(lname,"-"))) {
918 DEBUG(0,("%s does not exist\n",lname));
927 /****************************************************************************
928 seek in a directory/file list until you get something that doesn't start with
930 ****************************************************************************/
931 static BOOL seek_list(FILE *f,char *name)
935 if (fscanf(f,"%s",s) != 1) return(False);
936 trim_string(s,"./",NULL);
937 if (strncmp(s,name,strlen(name)) != 0) {
947 /****************************************************************************
948 set the file selection mask
949 ****************************************************************************/
950 static void cmd_select(void)
952 pstrcpy(fileselection,"");
953 next_token(NULL,fileselection,NULL,sizeof(fileselection));
957 /****************************************************************************
959 ****************************************************************************/
960 static void cmd_mput(void)
967 while (next_token(NULL,p,NULL,sizeof(buf))) {
973 slprintf(tmpname,sizeof(pstring)-1,
974 "%s/ls.smb.%d",tmpdir(),(int)getpid());
976 slprintf(cmd,sizeof(pstring)-1,
977 "find . -name \"%s\" -print > %s",p,tmpname);
979 slprintf(cmd,sizeof(pstring)-1,
980 "/bin/ls %s > %s",p,tmpname);
983 f = fopen(tmpname,"r");
989 if (fscanf(f,"%s",lname) != 1) break;
990 trim_string(lname,"./",NULL);
994 /* check if it's a directory */
995 if (directory_exist(lname,&st)) {
996 if (!recurse) continue;
997 slprintf(quest,sizeof(pstring)-1,
998 "Put directory %s? ",lname);
999 if (prompt && !yesno(quest)) {
1001 if (!seek_list(f,lname))
1006 pstrcpy(rname,cur_dir);
1007 pstrcat(rname,lname);
1008 if (!cli_chkpath(cli, rname) && !do_mkdir(rname)) {
1010 if (!seek_list(f,lname))
1016 slprintf(quest,sizeof(quest)-1,
1017 "Put file %s? ",lname);
1018 if (prompt && !yesno(quest)) continue;
1020 pstrcpy(rname,cur_dir);
1021 pstrcat(rname,lname);
1026 do_put(rname,lname);
1034 /****************************************************************************
1036 ****************************************************************************/
1037 static void do_cancel(int job)
1039 if (cli_printjob_del(cli, job)) {
1040 printf("Job %d cancelled\n",job);
1042 printf("Error calcelling job %d : %s\n",job,cli_errstr(cli));
1047 /****************************************************************************
1049 ****************************************************************************/
1050 static void cmd_cancel(void)
1055 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1056 printf("cancel <jobid> ...\n");
1062 } while (next_token(NULL,buf,NULL,sizeof(buf)));
1066 /****************************************************************************
1068 ****************************************************************************/
1069 static void cmd_print(void)
1075 if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1076 DEBUG(0,("print <filename>\n"));
1080 pstrcpy(rname,lname);
1081 p = strrchr(rname,'/');
1083 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1086 if (strequal(lname,"-")) {
1087 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1090 do_put(rname, lname);
1094 /****************************************************************************
1095 show a print queue entry
1096 ****************************************************************************/
1097 static void queue_fn(struct print_job_info *p)
1099 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1102 /****************************************************************************
1104 ****************************************************************************/
1105 static void cmd_queue(void)
1107 cli_print_queue(cli, queue_fn);
1110 /****************************************************************************
1112 ****************************************************************************/
1113 static void do_del(file_info *finfo)
1117 pstrcpy(mask,cur_dir);
1118 pstrcat(mask,finfo->name);
1120 if (finfo->mode & aDIR)
1123 if (!cli_unlink(cli, mask)) {
1124 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),CNV_LANG(mask)));
1128 /****************************************************************************
1130 ****************************************************************************/
1131 static void cmd_del(void)
1135 uint16 attribute = aSYSTEM | aHIDDEN;
1140 pstrcpy(mask,cur_dir);
1142 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1143 DEBUG(0,("del <filename>\n"));
1148 do_list(mask, attribute,do_del,False,False);
1152 /****************************************************************************
1154 ****************************************************************************/
1155 static void cmd_rmdir(void)
1160 pstrcpy(mask,cur_dir);
1162 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1163 DEBUG(0,("rmdir <dirname>\n"));
1168 if (!cli_rmdir(cli, mask)) {
1169 DEBUG(0,("%s removing remote directory file %s\n",
1170 cli_errstr(cli),CNV_LANG(mask)));
1174 /****************************************************************************
1176 ****************************************************************************/
1177 static void cmd_rename(void)
1182 pstrcpy(src,cur_dir);
1183 pstrcpy(dest,cur_dir);
1185 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1186 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1187 DEBUG(0,("rename <src> <dest>\n"));
1194 if (!cli_rename(cli, src, dest)) {
1195 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1201 /****************************************************************************
1202 toggle the prompt flag
1203 ****************************************************************************/
1204 static void cmd_prompt(void)
1207 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1211 /****************************************************************************
1212 set the newer than time
1213 ****************************************************************************/
1214 static void cmd_newer(void)
1218 SMB_STRUCT_STAT sbuf;
1220 ok = next_token(NULL,buf,NULL,sizeof(buf));
1221 if (ok && (dos_stat(buf,&sbuf) == 0)) {
1222 newer_than = sbuf.st_mtime;
1223 DEBUG(1,("Getting files newer than %s",
1224 asctime(LocalTime(&newer_than))));
1229 if (ok && newer_than == 0)
1230 DEBUG(0,("Error setting newer-than time\n"));
1233 /****************************************************************************
1234 set the archive level
1235 ****************************************************************************/
1236 static void cmd_archive(void)
1240 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1241 archive_level = atoi(buf);
1243 DEBUG(0,("Archive level is %d\n",archive_level));
1246 /****************************************************************************
1247 toggle the lowercaseflag
1248 ****************************************************************************/
1249 static void cmd_lowercase(void)
1251 lowercase = !lowercase;
1252 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1258 /****************************************************************************
1259 toggle the recurse flag
1260 ****************************************************************************/
1261 static void cmd_recurse(void)
1264 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1267 /****************************************************************************
1268 toggle the translate flag
1269 ****************************************************************************/
1270 static void cmd_translate(void)
1272 translation = !translation;
1273 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1274 translation?"on":"off"));
1278 /****************************************************************************
1279 do a printmode command
1280 ****************************************************************************/
1281 static void cmd_printmode(void)
1286 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1287 if (strequal(buf,"text")) {
1290 if (strequal(buf,"graphics"))
1293 printmode = atoi(buf);
1300 fstrcpy(mode,"text");
1303 fstrcpy(mode,"graphics");
1306 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1310 DEBUG(2,("the printmode is now %s\n",mode));
1313 /****************************************************************************
1315 ****************************************************************************/
1316 static void cmd_lcd(void)
1321 if (next_token(NULL,buf,NULL,sizeof(buf)))
1323 DEBUG(2,("the local directory is now %s\n",GetWd(d)));
1326 /****************************************************************************
1328 ****************************************************************************/
1329 static void browse_fn(const char *name, uint32 m, const char *comment)
1336 case STYPE_DISKTREE:
1337 fstrcpy(typestr,"Disk"); break;
1339 fstrcpy(typestr,"Printer"); break;
1341 fstrcpy(typestr,"Device"); break;
1343 fstrcpy(typestr,"IPC"); break;
1346 printf("\t%-15.15s%-10.10s%s\n",
1347 name, typestr,comment);
1351 /****************************************************************************
1352 try and browse available connections on a host
1353 ****************************************************************************/
1354 static BOOL browse_host(BOOL sort)
1356 printf("\n\tSharename Type Comment\n");
1357 printf("\t--------- ---- -------\n");
1359 return cli_RNetShareEnum(cli, browse_fn);
1362 /****************************************************************************
1364 ****************************************************************************/
1365 static void server_fn(const char *name, uint32 m, const char *comment)
1367 printf("\t%-16.16s %s\n", name, comment);
1370 /****************************************************************************
1371 try and browse available connections on a host
1372 ****************************************************************************/
1373 static BOOL list_servers(char *wk_grp)
1375 printf("\n\tServer Comment\n");
1376 printf("\t--------- -------\n");
1378 cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, server_fn);
1380 printf("\n\tWorkgroup Master\n");
1381 printf("\t--------- -------\n");
1383 cli_NetServerEnum(cli, workgroup, SV_TYPE_DOMAIN_ENUM, server_fn);
1388 /* Some constants for completing filename arguments */
1390 #define COMPL_NONE 0 /* No completions */
1391 #define COMPL_REMOTE 1 /* Complete remote filename */
1392 #define COMPL_LOCAL 2 /* Complete local filename */
1394 /* This defines the commands supported by this client */
1400 char compl_args[2]; /* Completion argument info */
1403 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1404 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1405 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1406 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1407 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1408 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1409 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1410 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1411 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1412 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1413 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1414 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1415 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1416 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1417 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1418 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1419 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1420 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1421 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1422 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1423 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1424 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1425 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1426 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1427 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1428 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1429 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1430 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1431 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1432 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1433 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1434 {"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}},
1435 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1436 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1437 {"tarmode",cmd_tarmode,
1438 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1439 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1440 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1441 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1442 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1443 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1447 /*******************************************************************
1448 lookup a command string in the list of commands, including
1450 ******************************************************************/
1451 static int process_tok(fstring tok)
1453 int i = 0, matches = 0;
1455 int tok_len = strlen(tok);
1457 while (commands[i].fn != NULL) {
1458 if (strequal(commands[i].name,tok)) {
1462 } else if (strnequal(commands[i].name, tok, tok_len)) {
1471 else if (matches == 1)
1477 /****************************************************************************
1479 ****************************************************************************/
1480 static void cmd_help(void)
1485 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1486 if ((i = process_tok(buf)) >= 0)
1487 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1489 while (commands[i].description) {
1490 for (j=0; commands[i].description && (j<5); j++) {
1491 DEBUG(0,("%-15s",commands[i].name));
1499 /****************************************************************************
1500 wait for keyboard activity, swallowing network packets
1501 ****************************************************************************/
1502 static void wait_keyboard(void)
1505 struct timeval timeout;
1509 FD_SET(cli->fd,&fds);
1510 FD_SET(fileno(stdin),&fds);
1512 timeout.tv_sec = 20;
1513 timeout.tv_usec = 0;
1514 sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
1516 if (FD_ISSET(fileno(stdin),&fds))
1519 /* We deliberately use receive_smb instead of
1520 client_receive_smb as we want to receive
1521 session keepalives and then drop them here.
1523 if (FD_ISSET(cli->fd,&fds))
1524 receive_smb(cli->fd,cli->inbuf,0);
1526 cli_chkpath(cli, "\\");
1530 /****************************************************************************
1531 process a -c command string
1532 ****************************************************************************/
1533 static void process_command_string(char *cmd)
1538 while (cmd[0] != '\0') {
1543 if ((p = strchr(cmd, ';')) == 0) {
1544 strncpy(line, cmd, 999);
1548 if (p - cmd > 999) p = cmd + 999;
1549 strncpy(line, cmd, p - cmd);
1550 line[p - cmd] = '\0';
1554 /* input language code to internal one */
1557 /* and get the first part of the command */
1559 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1561 if ((i = process_tok(tok)) >= 0) {
1563 } else if (i == -2) {
1564 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1566 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1571 /****************************************************************************
1572 process commands on stdin
1573 ****************************************************************************/
1574 static void process_stdin(void)
1579 while (!feof(stdin)) {
1583 /* display a prompt */
1584 DEBUG(0,("smb: %s> ", CNV_LANG(cur_dir)));
1589 /* and get a response */
1590 if (!fgets(line,1000,stdin))
1593 /* input language code to internal one */
1596 /* special case - first char is ! */
1602 /* and get the first part of the command */
1604 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1606 if ((i = process_tok(tok)) >= 0) {
1608 } else if (i == -2) {
1609 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1611 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1617 /*****************************************************
1618 return a connection to a server
1619 *******************************************************/
1620 struct cli_state *do_connect(char *server, char *share)
1622 struct cli_state *c;
1623 struct nmb_name called, calling;
1626 extern struct in_addr ipzero;
1628 if (*share == '\\') {
1630 share = strchr(server,'\\');
1631 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;
1881 make_nmb_name(&calling, global_myname, 0x0, "");
1882 make_nmb_name(&called , desthost, name_type, "");
1885 if (have_ip) ip = dest_ip;
1887 if (!(cli=cli_initialise(NULL)) || !cli_connect(cli, desthost, &ip)) {
1888 DEBUG(0,("Connection to %s failed\n", desthost));
1892 if (!cli_session_request(cli, &calling, &called)) {
1893 DEBUG(0,("session request failed\n"));
1905 /****************************************************************************
1907 ****************************************************************************/
1908 int main(int argc,char *argv[])
1910 fstring base_directory;
1911 char *pname = argv[0];
1912 int port = SMB_PORT;
1915 extern char *optarg;
1918 BOOL message = False;
1919 BOOL explicit_user = False;
1920 extern char tar_type;
1921 static pstring servicesf = CONFIGFILE;
1923 pstring new_name_resolve_order;
1927 pstrcpy(term_code, KANJI);
1933 *base_directory = 0;
1935 *new_name_resolve_order = 0;
1939 setup_logging(pname,True);
1942 charset_initialise();
1944 if(!get_myname(myhostname,NULL)) {
1945 DEBUG(0,("Failed to get my hostname.\n"));
1948 in_client = True; /* Make sure that we tell lp_load we are */
1950 if (!lp_load(servicesf,True,False,False)) {
1951 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
1954 codepage_initialise(lp_client_code_page());
1956 interpret_coding_system(term_code);
1962 pstrcpy(workgroup,lp_workgroup());
1968 if (getenv("USER")) {
1969 pstrcpy(username,getenv("USER"));
1971 /* modification to support userid%passwd syntax in the USER var
1972 25.Aug.97, jdblair@uab.edu */
1974 if ((p=strchr(username,'%'))) {
1976 pstrcpy(password,p+1);
1978 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
1983 /* modification to support PASSWD environmental var
1984 25.Aug.97, jdblair@uab.edu */
1985 if (getenv("PASSWD")) {
1986 pstrcpy(password,getenv("PASSWD"));
1990 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
1991 get_password_file();
1995 if (*username == 0 && getenv("LOGNAME")) {
1996 pstrcpy(username,getenv("LOGNAME"));
2000 if (*username == 0) {
2001 pstrcpy(username,"GUEST");
2009 if (*argv[1] != '-') {
2010 pstrcpy(service,argv[1]);
2011 /* Convert any '/' characters in the service name to '\' characters */
2012 string_replace( service, '/','\\');
2016 if (count_chars(service,'\\') < 3) {
2018 printf("\n%s: Not enough '\\' characters in service\n",service);
2022 if (argc > 1 && (*argv[1] != '-')) {
2024 pstrcpy(password,argv[1]);
2025 memset(argv[1],'X',strlen(argv[1]));
2032 getopt(argc, argv,"s:B:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:")) != EOF) {
2035 pstrcpy(servicesf, optarg);
2038 iface_set_default(NULL,optarg,NULL);
2041 pstrcpy(user_socket_options,optarg);
2044 pstrcpy(new_name_resolve_order, optarg);
2047 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2048 pstrcpy(desthost,optarg);
2053 pstrcpy(scope,optarg);
2059 pstrcpy(global_myname,optarg);
2065 DEBUGLEVEL = atoi(optarg);
2068 /* not needed anymore */
2071 port = atoi(optarg);
2074 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2082 dest_ip = *interpret_addr2(optarg);
2083 if (zero_ip(dest_ip))
2094 explicit_user = True;
2095 pstrcpy(username,optarg);
2096 if ((lp=strchr(username,'%'))) {
2098 pstrcpy(password,lp+1);
2100 memset(strchr(optarg,'%')+1,'X',strlen(password));
2105 pstrcpy(query_host,optarg);
2110 pstrcpy(term_code, optarg);
2113 /* no longer supported */
2116 pstrcpy(workgroup,optarg);
2119 if (!tar_parseargs(argc, argv, optarg, optind)) {
2125 pstrcpy(base_directory,optarg);
2137 get_myname((*global_myname)?NULL:global_myname,NULL);
2138 strupper(global_myname);
2140 if(*new_name_resolve_order)
2141 lp_set_name_resolve_order(new_name_resolve_order);
2143 if (!tar_type && !*query_host && !*service && !message) {
2148 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2151 return do_tar_op(port, base_directory);
2154 if ((p=strchr(query_host,'#'))) {
2157 sscanf(p, "%x", &name_type);
2161 return do_host_query(query_host, port);
2165 return do_message_op();
2168 if (!process(base_directory)) {