transfer minor fixes from the 2.0 branch
[Samba/gbeck.git] / source / client / client.c
blob4f72386e9d222185b4e3e1a55d8dd7ba0262dc37
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB client
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.
22 #define NO_SYSLOG
24 #include "includes.h"
26 #ifndef REGISTER
27 #define REGISTER 0
28 #endif
30 struct cli_state *cli;
31 extern BOOL in_client;
32 static int port = SMB_PORT;
33 pstring cur_dir = "\\";
34 pstring cd_path = "";
35 static pstring service;
36 static pstring desthost;
37 extern pstring global_myname;
38 extern pstring myhostname;
39 static pstring password;
40 static pstring username;
41 static pstring workgroup;
42 static char *cmdstr;
43 static BOOL got_pass;
44 static int io_bufsize = 65520;
45 extern struct in_addr ipzero;
46 extern pstring scope;
48 static int name_type = 0x20;
50 extern pstring user_socket_options;
52 static int process_tok(fstring tok);
53 static void cmd_help(void);
55 /* 30 second timeout on most commands */
56 #define CLIENT_TIMEOUT (30*1000)
57 #define SHORT_TIMEOUT (5*1000)
59 /* value for unused fid field in trans2 secondary request */
60 #define FID_UNUSED (0xFFFF)
62 time_t newer_than = 0;
63 int archive_level = 0;
65 extern pstring debugf;
66 extern int DEBUGLEVEL;
68 BOOL translation = False;
70 static BOOL have_ip;
72 /* clitar bits insert */
73 extern int blocksize;
74 extern BOOL tar_inc;
75 extern BOOL tar_reset;
76 /* clitar bits end */
79 mode_t myumask = 0755;
81 BOOL prompt = True;
83 int printmode = 1;
85 static BOOL recurse = False;
86 BOOL lowercase = False;
88 struct in_addr dest_ip;
90 #define SEPARATORS " \t\n\r"
92 BOOL abort_mget = True;
94 pstring fileselection = "";
96 extern file_info def_finfo;
98 /* timing globals */
99 int get_total_size = 0;
100 int get_total_time_ms = 0;
101 int put_total_size = 0;
102 int put_total_time_ms = 0;
104 /* totals globals */
105 static double dir_total;
107 #define USENMB
109 #define CNV_LANG(s) dos_to_unix(s,False)
110 #define CNV_INPUT(s) unix_to_dos(s,True)
113 /****************************************************************************
114 write to a local file with CR/LF->LF translation if appropriate. return the
115 number taken from the buffer. This may not equal the number written.
116 ****************************************************************************/
117 static int writefile(int f, char *b, int n)
119 int i;
121 if (!translation) {
122 return write(f,b,n);
125 i = 0;
126 while (i < n) {
127 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
128 b++;i++;
130 if (write(f, b, 1) != 1) {
131 break;
133 b++;
134 i++;
137 return(i);
140 /****************************************************************************
141 read from a file with LF->CR/LF translation if appropriate. return the
142 number read. read approx n bytes.
143 ****************************************************************************/
144 static int readfile(char *b, int size, int n, FILE *f)
146 int i;
147 int c;
149 if (!translation || (size != 1))
150 return(fread(b,size,n,f));
152 i = 0;
153 while (i < (n - 1)) {
154 if ((c = getc(f)) == EOF) {
155 break;
158 if (c == '\n') { /* change all LFs to CR/LF */
159 b[i++] = '\r';
162 b[i++] = c;
165 return(i);
169 /****************************************************************************
170 send a message
171 ****************************************************************************/
172 static void send_message(void)
174 int total_len = 0;
175 int grp_id;
177 if (!cli_message_start(cli, desthost, username, &grp_id)) {
178 DEBUG(0,("message start: %s\n", cli_errstr(cli)));
179 return;
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);
187 pstring msg;
188 int l=0;
189 int c;
191 ZERO_ARRAY(msg);
193 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
194 if (c == '\n')
195 msg[l++] = '\r';
196 msg[l] = c;
199 if (!cli_message_text(cli, msg, l, grp_id)) {
200 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
201 return;
204 total_len += l;
207 if (total_len >= 1600)
208 printf("the message was truncated to 1600 bytes\n");
209 else
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));
214 return;
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)));
229 return;
232 DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
233 total, bsize, avail));
236 /****************************************************************************
237 show cd/pwd
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)
251 char *p = newdir;
252 pstring saved_dir;
253 pstring dname;
255 dos_format(newdir);
257 /* Save the current directory in case the
258 new directory is invalid */
259 pstrcpy(saved_dir, cur_dir);
260 if (*p == '\\')
261 pstrcpy(cur_dir,p);
262 else
263 pstrcat(cur_dir,p);
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 /****************************************************************************
283 change directory
284 ****************************************************************************/
285 static void cmd_cd(void)
287 fstring buf;
289 if (next_token(NULL,buf,NULL,sizeof(buf)))
290 do_cd(buf);
291 else
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));
306 return False;
309 if (newer_than && finfo->mtime < newer_than) {
310 DEBUG(3,("newer_than %s failed\n", finfo->name));
311 return(False);
314 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
315 DEBUG(3,("archive %s failed\n", finfo->name));
316 return(False);
319 return(True);
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),
332 (double)finfo->size,
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 char *do_list_queue = 0;
352 static long do_list_queue_size = 0;
353 static long do_list_queue_start = 0;
354 static long do_list_queue_end = 0;
355 static void (*do_list_fn)(file_info *);
357 /****************************************************************************
358 functions for do_list_queue
359 ****************************************************************************/
362 * The do_list_queue is a NUL-separated list of strings stored in a
363 * char*. Since this is a FIFO, we keep track of the beginning and
364 * ending locations of the data in the queue. When we overflow, we
365 * double the size of the char*. When the start of the data passes
366 * the midpoint, we move everything back. This is logically more
367 * complex than a linked list, but easier from a memory management
368 * angle. In any memory error condition, do_list_queue is reset.
369 * Functions check to ensure that do_list_queue is non-NULL before
370 * accessing it.
372 static void reset_do_list_queue(void)
374 if (do_list_queue)
376 free(do_list_queue);
378 do_list_queue = 0;
379 do_list_queue_size = 0;
380 do_list_queue_start = 0;
381 do_list_queue_end = 0;
384 static void init_do_list_queue(void)
386 reset_do_list_queue();
387 do_list_queue_size = 1024;
388 do_list_queue = malloc(do_list_queue_size);
389 if (do_list_queue == 0) {
390 DEBUG(0,("malloc fail for size %d\n",
391 (int)do_list_queue_size));
392 reset_do_list_queue();
394 memset(do_list_queue, 0, do_list_queue_size);
397 static void adjust_do_list_queue(void)
400 * If the starting point of the queue is more than half way through,
401 * move everything toward the beginning.
403 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
405 DEBUG(4,("do_list_queue is empty\n"));
406 do_list_queue_start = do_list_queue_end = 0;
407 *do_list_queue = '\0';
409 else if (do_list_queue_start > (do_list_queue_size / 2))
411 DEBUG(4,("sliding do_list_queue backward\n"));
412 memmove(do_list_queue,
413 do_list_queue + do_list_queue_start,
414 do_list_queue_end - do_list_queue_start);
415 do_list_queue_end -= do_list_queue_start;
416 do_list_queue_start = 0;
421 static void add_to_do_list_queue(const char* entry)
423 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
424 while (new_end > do_list_queue_size)
426 do_list_queue_size *= 2;
427 DEBUG(4,("enlarging do_list_queue to %d\n",
428 (int)do_list_queue_size));
429 do_list_queue = Realloc(do_list_queue, do_list_queue_size);
430 if (! do_list_queue) {
431 DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
432 (int)do_list_queue_size));
433 reset_do_list_queue();
435 else
437 memset(do_list_queue + do_list_queue_size / 2,
438 0, do_list_queue_size / 2);
441 if (do_list_queue)
443 pstrcpy(do_list_queue + do_list_queue_end, entry);
444 do_list_queue_end = new_end;
445 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
446 entry, (int)do_list_queue_start, (int)do_list_queue_end));
450 static char *do_list_queue_head(void)
452 return do_list_queue + do_list_queue_start;
455 static void remove_do_list_queue_head(void)
457 if (do_list_queue_end > do_list_queue_start)
459 do_list_queue_start += strlen(do_list_queue_head()) + 1;
460 adjust_do_list_queue();
461 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
462 (int)do_list_queue_start, (int)do_list_queue_end));
466 static int do_list_queue_empty(void)
468 return (! (do_list_queue && *do_list_queue));
471 /****************************************************************************
472 a helper for do_list
473 ****************************************************************************/
474 static void do_list_helper(file_info *f, const char *mask)
476 if (f->mode & aDIR) {
477 if (do_list_dirs && do_this_one(f)) {
478 do_list_fn(f);
480 if (do_list_recurse &&
481 !strequal(f->name,".") &&
482 !strequal(f->name,"..")) {
483 pstring mask2;
484 char *p;
486 pstrcpy(mask2, mask);
487 p = strrchr(mask2,'\\');
488 if (!p) return;
489 p[1] = 0;
490 pstrcat(mask2, f->name);
491 pstrcat(mask2,"\\*");
492 add_to_do_list_queue(mask2);
494 return;
497 if (do_this_one(f)) {
498 do_list_fn(f);
503 /****************************************************************************
504 a wrapper around cli_list that adds recursion
505 ****************************************************************************/
506 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
508 static int in_do_list = 0;
510 if (in_do_list && rec)
512 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
513 exit(1);
516 in_do_list = 1;
518 do_list_recurse = rec;
519 do_list_dirs = dirs;
520 do_list_fn = fn;
522 if (rec)
524 init_do_list_queue();
525 add_to_do_list_queue(mask);
527 while (! do_list_queue_empty())
529 cli_list(cli, do_list_queue_head(), attribute,
530 do_list_helper);
531 remove_do_list_queue_head();
532 if ((! do_list_queue_empty()) && (fn == display_finfo))
534 char* next_file = do_list_queue_head();
535 char* save_ch = 0;
536 if ((strlen(next_file) >= 2) &&
537 (next_file[strlen(next_file) - 1] == '*') &&
538 (next_file[strlen(next_file) - 2] == '\\'))
540 save_ch = next_file +
541 strlen(next_file) - 2;
542 *save_ch = '\0';
544 DEBUG(0,("\n%s\n",CNV_LANG(next_file)));
545 if (save_ch)
547 *save_ch = '\\';
552 else
554 cli_list(cli, mask, attribute, do_list_helper);
557 in_do_list = 0;
558 reset_do_list_queue();
561 /****************************************************************************
562 get a directory listing
563 ****************************************************************************/
564 static void cmd_dir(void)
566 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
567 pstring mask;
568 fstring buf;
569 char *p=buf;
571 dir_total = 0;
572 pstrcpy(mask,cur_dir);
573 if(mask[strlen(mask)-1]!='\\')
574 pstrcat(mask,"\\");
576 if (next_token(NULL,buf,NULL,sizeof(buf))) {
577 dos_format(p);
578 if (*p == '\\')
579 pstrcpy(mask,p);
580 else
581 pstrcat(mask,p);
583 else {
584 pstrcat(mask,"*");
587 do_list(mask, attribute, display_finfo, recurse, True);
589 do_dskattr();
591 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
595 /****************************************************************************
596 get a directory listing
597 ****************************************************************************/
598 static void cmd_du(void)
600 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
601 pstring mask;
602 fstring buf;
603 char *p=buf;
605 dir_total = 0;
606 pstrcpy(mask,cur_dir);
607 if(mask[strlen(mask)-1]!='\\')
608 pstrcat(mask,"\\");
610 if (next_token(NULL,buf,NULL,sizeof(buf))) {
611 dos_format(p);
612 if (*p == '\\')
613 pstrcpy(mask,p);
614 else
615 pstrcat(mask,p);
616 } else {
617 pstrcat(mask,"*");
620 do_list(mask, attribute, do_du, recurse, True);
622 do_dskattr();
624 DEBUG(0, ("Total number of bytes: %.0f\n", dir_total));
628 /****************************************************************************
629 get a file from rname to lname
630 ****************************************************************************/
631 static void do_get(char *rname,char *lname)
633 int handle=0,fnum;
634 BOOL newhandle = False;
635 char *data;
636 struct timeval tp_start;
637 int read_size = io_bufsize;
638 uint16 attr;
639 size_t size;
640 off_t nread = 0;
642 GetTimeOfDay(&tp_start);
644 if (lowercase) {
645 strlower(lname);
648 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
650 if (fnum == -1) {
651 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
652 return;
655 if(!strcmp(lname,"-")) {
656 handle = fileno(stdout);
657 } else {
658 handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
659 newhandle = True;
661 if (handle < 0) {
662 DEBUG(0,("Error opening local file %s\n",lname));
663 return;
667 if (!cli_qfileinfo(cli, fnum,
668 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
669 !cli_getattrE(cli, fnum,
670 &attr, &size, NULL, NULL, NULL)) {
671 DEBUG(0,("getattrib: %s\n",cli_errstr(cli)));
672 return;
675 DEBUG(2,("getting file %s of size %.0f as %s ",
676 lname, (double)size, lname));
678 if(!(data = (char *)malloc(read_size))) {
679 DEBUG(0,("malloc fail for size %d\n", read_size));
680 cli_close(cli, fnum);
681 return;
684 while (1) {
685 int n = cli_read(cli, fnum, data, nread, read_size);
687 if (n <= 0) break;
689 if (writefile(handle,data, n) != n) {
690 DEBUG(0,("Error writing local file\n"));
691 break;
694 nread += n;
697 if (nread < size) {
698 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
699 CNV_LANG(rname), (long)nread));
702 free(data);
704 if (!cli_close(cli, fnum)) {
705 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
708 if (newhandle) {
709 close(handle);
712 if (archive_level >= 2 && (attr & aARCH)) {
713 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
717 struct timeval tp_end;
718 int this_time;
720 GetTimeOfDay(&tp_end);
721 this_time =
722 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
723 (tp_end.tv_usec - tp_start.tv_usec)/1000;
724 get_total_time_ms += this_time;
725 get_total_size += nread;
727 DEBUG(2,("(%g kb/s) (average %g kb/s)\n",
728 nread / (1.024*this_time + 1.0e-4),
729 get_total_size / (1.024*get_total_time_ms)));
734 /****************************************************************************
735 get a file
736 ****************************************************************************/
737 static void cmd_get(void)
739 pstring lname;
740 pstring rname;
741 char *p;
743 pstrcpy(rname,cur_dir);
744 pstrcat(rname,"\\");
746 p = rname + strlen(rname);
748 if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
749 DEBUG(0,("get <filename>\n"));
750 return;
752 pstrcpy(lname,p);
753 dos_clean_name(rname);
755 next_token(NULL,lname,NULL,sizeof(lname));
757 do_get(rname, lname);
761 /****************************************************************************
762 do a mget operation on one file
763 ****************************************************************************/
764 static void do_mget(file_info *finfo)
766 pstring rname;
767 pstring quest;
768 pstring saved_curdir;
769 pstring mget_mask;
771 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
772 return;
774 if (abort_mget) {
775 DEBUG(0,("mget aborted\n"));
776 return;
779 if (finfo->mode & aDIR)
780 slprintf(quest,sizeof(pstring)-1,
781 "Get directory %s? ",CNV_LANG(finfo->name));
782 else
783 slprintf(quest,sizeof(pstring)-1,
784 "Get file %s? ",CNV_LANG(finfo->name));
786 if (prompt && !yesno(quest)) return;
788 if (!(finfo->mode & aDIR)) {
789 pstrcpy(rname,cur_dir);
790 pstrcat(rname,finfo->name);
791 do_get(rname,finfo->name);
792 return;
795 /* handle directories */
796 pstrcpy(saved_curdir,cur_dir);
798 pstrcat(cur_dir,finfo->name);
799 pstrcat(cur_dir,"\\");
801 unix_format(finfo->name);
802 if (lowercase)
803 strlower(finfo->name);
805 if (!dos_directory_exist(finfo->name,NULL) &&
806 dos_mkdir(finfo->name,0777) != 0) {
807 DEBUG(0,("failed to create directory %s\n",CNV_LANG(finfo->name)));
808 pstrcpy(cur_dir,saved_curdir);
809 return;
812 if (dos_chdir(finfo->name) != 0) {
813 DEBUG(0,("failed to chdir to directory %s\n",CNV_LANG(finfo->name)));
814 pstrcpy(cur_dir,saved_curdir);
815 return;
818 pstrcpy(mget_mask,cur_dir);
819 pstrcat(mget_mask,"*");
821 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
822 chdir("..");
823 pstrcpy(cur_dir,saved_curdir);
827 /****************************************************************************
828 view the file using the pager
829 ****************************************************************************/
830 static void cmd_more(void)
832 fstring rname,lname,tmpname,pager_cmd;
833 char *pager;
835 fstrcpy(rname,cur_dir);
836 fstrcat(rname,"\\");
837 slprintf(tmpname,
838 sizeof(fstring)-1,
839 "%s/smbmore.%d",tmpdir(),(int)getpid());
840 fstrcpy(lname,tmpname);
842 if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
843 DEBUG(0,("more <filename>\n"));
844 return;
846 dos_clean_name(rname);
848 do_get(rname,lname);
850 pager=getenv("PAGER");
852 slprintf(pager_cmd,sizeof(pager_cmd)-1,
853 "%s %s",(pager? pager:PAGER), tmpname);
854 system(pager_cmd);
855 unlink(tmpname);
860 /****************************************************************************
861 do a mget command
862 ****************************************************************************/
863 static void cmd_mget(void)
865 uint16 attribute = aSYSTEM | aHIDDEN;
866 pstring mget_mask;
867 fstring buf;
868 char *p=buf;
870 *mget_mask = 0;
872 if (recurse)
873 attribute |= aDIR;
875 abort_mget = False;
877 while (next_token(NULL,p,NULL,sizeof(buf))) {
878 pstrcpy(mget_mask,cur_dir);
879 if(mget_mask[strlen(mget_mask)-1]!='\\')
880 pstrcat(mget_mask,"\\");
882 if (*p == '\\')
883 pstrcpy(mget_mask,p);
884 else
885 pstrcat(mget_mask,p);
886 do_list(mget_mask, attribute,do_mget,False,True);
889 if (!*mget_mask) {
890 pstrcpy(mget_mask,cur_dir);
891 if(mget_mask[strlen(mget_mask)-1]!='\\')
892 pstrcat(mget_mask,"\\");
893 pstrcat(mget_mask,"*");
894 do_list(mget_mask, attribute,do_mget,False,True);
899 /****************************************************************************
900 make a directory of name "name"
901 ****************************************************************************/
902 static BOOL do_mkdir(char *name)
904 if (!cli_mkdir(cli, name)) {
905 DEBUG(0,("%s making remote directory %s\n",
906 cli_errstr(cli),CNV_LANG(name)));
907 return(False);
910 return(True);
914 /****************************************************************************
915 Exit client.
916 ****************************************************************************/
917 static void cmd_quit(void)
919 cli_shutdown(cli);
920 exit(0);
924 /****************************************************************************
925 make a directory
926 ****************************************************************************/
927 static void cmd_mkdir(void)
929 pstring mask;
930 fstring buf;
931 char *p=buf;
933 pstrcpy(mask,cur_dir);
935 if (!next_token(NULL,p,NULL,sizeof(buf))) {
936 if (!recurse)
937 DEBUG(0,("mkdir <dirname>\n"));
938 return;
940 pstrcat(mask,p);
942 if (recurse) {
943 pstring ddir;
944 pstring ddir2;
945 *ddir2 = 0;
947 pstrcpy(ddir,mask);
948 trim_string(ddir,".",NULL);
949 p = strtok(ddir,"/\\");
950 while (p) {
951 pstrcat(ddir2,p);
952 if (!cli_chkpath(cli, ddir2)) {
953 do_mkdir(ddir2);
955 pstrcat(ddir2,"\\");
956 p = strtok(NULL,"/\\");
958 } else {
959 do_mkdir(mask);
964 /****************************************************************************
965 put a single file
966 ****************************************************************************/
967 static void do_put(char *rname,char *lname)
969 int fnum;
970 FILE *f;
971 int nread=0;
972 char *buf=NULL;
973 int maxwrite=io_bufsize;
975 struct timeval tp_start;
976 GetTimeOfDay(&tp_start);
978 fnum = cli_open(cli, rname, O_WRONLY|O_CREAT|O_TRUNC, DENY_NONE);
980 if (fnum == -1) {
981 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
982 return;
985 /* allow files to be piped into smbclient
986 jdblair 24.jun.98 */
987 if (!strcmp(lname, "-")) {
988 f = stdin;
989 /* size of file is not known */
990 } else {
991 f = sys_fopen(lname,"r");
994 if (!f) {
995 DEBUG(0,("Error opening local file %s\n",lname));
996 return;
1000 DEBUG(1,("putting file %s as %s ",lname,
1001 CNV_LANG(rname)));
1003 buf = (char *)malloc(maxwrite);
1004 while (!feof(f)) {
1005 int n = maxwrite;
1006 int ret;
1008 if ((n = readfile(buf,1,n,f)) < 1) {
1009 DEBUG(0,("Error reading local file\n"));
1010 break;
1013 ret = cli_write(cli, fnum, 0, buf, nread, n);
1015 if (n != ret) {
1016 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
1017 break;
1020 nread += n;
1023 if (!cli_close(cli, fnum)) {
1024 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),CNV_LANG(rname)));
1025 fclose(f);
1026 if (buf) free(buf);
1027 return;
1031 fclose(f);
1032 if (buf) free(buf);
1035 struct timeval tp_end;
1036 int this_time;
1038 GetTimeOfDay(&tp_end);
1039 this_time =
1040 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1041 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1042 put_total_time_ms += this_time;
1043 put_total_size += nread;
1045 DEBUG(1,("(%g kb/s) (average %g kb/s)\n",
1046 nread / (1.024*this_time + 1.0e-4),
1047 put_total_size / (1.024*put_total_time_ms)));
1050 if (f == stdin) {
1051 cli_shutdown(cli);
1052 exit(0);
1058 /****************************************************************************
1059 put a file
1060 ****************************************************************************/
1061 static void cmd_put(void)
1063 pstring lname;
1064 pstring rname;
1065 fstring buf;
1066 char *p=buf;
1068 pstrcpy(rname,cur_dir);
1069 pstrcat(rname,"\\");
1071 if (!next_token(NULL,p,NULL,sizeof(buf))) {
1072 DEBUG(0,("put <filename>\n"));
1073 return;
1075 pstrcpy(lname,p);
1077 if (next_token(NULL,p,NULL,sizeof(buf)))
1078 pstrcat(rname,p);
1079 else
1080 pstrcat(rname,lname);
1082 dos_clean_name(rname);
1085 SMB_STRUCT_STAT st;
1086 /* allow '-' to represent stdin
1087 jdblair, 24.jun.98 */
1088 if (!file_exist(lname,&st) &&
1089 (strcmp(lname,"-"))) {
1090 DEBUG(0,("%s does not exist\n",lname));
1091 return;
1095 do_put(rname,lname);
1099 /****************************************************************************
1100 seek in a directory/file list until you get something that doesn't start with
1101 the specified name
1102 ****************************************************************************/
1103 static BOOL seek_list(FILE *f,char *name)
1105 pstring s;
1106 while (!feof(f)) {
1107 if (fscanf(f,"%s",s) != 1) return(False);
1108 trim_string(s,"./",NULL);
1109 if (strncmp(s,name,strlen(name)) != 0) {
1110 pstrcpy(name,s);
1111 return(True);
1115 return(False);
1119 /****************************************************************************
1120 set the file selection mask
1121 ****************************************************************************/
1122 static void cmd_select(void)
1124 pstrcpy(fileselection,"");
1125 next_token(NULL,fileselection,NULL,sizeof(fileselection));
1129 /****************************************************************************
1130 mput some files
1131 ****************************************************************************/
1132 static void cmd_mput(void)
1134 pstring lname;
1135 pstring rname;
1136 fstring buf;
1137 char *p=buf;
1139 while (next_token(NULL,p,NULL,sizeof(buf))) {
1140 SMB_STRUCT_STAT st;
1141 pstring cmd;
1142 pstring tmpname;
1143 FILE *f;
1145 slprintf(tmpname,sizeof(pstring)-1,
1146 "%s/ls.smb.%d",tmpdir(),(int)getpid());
1147 if (recurse)
1148 slprintf(cmd,sizeof(pstring)-1,
1149 "find . -name \"%s\" -print > %s",p,tmpname);
1150 else
1151 slprintf(cmd,sizeof(pstring)-1,
1152 "/bin/ls %s > %s",p,tmpname);
1153 system(cmd);
1155 f = sys_fopen(tmpname,"r");
1156 if (!f) continue;
1158 while (!feof(f)) {
1159 pstring quest;
1161 if (fscanf(f,"%s",lname) != 1) break;
1162 trim_string(lname,"./",NULL);
1164 again1:
1166 /* check if it's a directory */
1167 if (directory_exist(lname,&st)) {
1168 if (!recurse) continue;
1169 slprintf(quest,sizeof(pstring)-1,
1170 "Put directory %s? ",lname);
1171 if (prompt && !yesno(quest)) {
1172 pstrcat(lname,"/");
1173 if (!seek_list(f,lname))
1174 break;
1175 goto again1;
1178 pstrcpy(rname,cur_dir);
1179 pstrcat(rname,lname);
1180 if (!cli_chkpath(cli, rname) && !do_mkdir(rname)) {
1181 pstrcat(lname,"/");
1182 if (!seek_list(f,lname))
1183 break;
1184 goto again1;
1186 continue;
1187 } else {
1188 slprintf(quest,sizeof(quest)-1,
1189 "Put file %s? ",lname);
1190 if (prompt && !yesno(quest)) continue;
1192 pstrcpy(rname,cur_dir);
1193 pstrcat(rname,lname);
1196 dos_format(rname);
1198 do_put(rname,lname);
1200 fclose(f);
1201 unlink(tmpname);
1206 /****************************************************************************
1207 cancel a print job
1208 ****************************************************************************/
1209 static void do_cancel(int job)
1211 if (cli_printjob_del(cli, job)) {
1212 printf("Job %d cancelled\n",job);
1213 } else {
1214 printf("Error calcelling job %d : %s\n",job,cli_errstr(cli));
1219 /****************************************************************************
1220 cancel a print job
1221 ****************************************************************************/
1222 static void cmd_cancel(void)
1224 fstring buf;
1225 int job;
1227 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1228 printf("cancel <jobid> ...\n");
1229 return;
1231 do {
1232 job = atoi(buf);
1233 do_cancel(job);
1234 } while (next_token(NULL,buf,NULL,sizeof(buf)));
1238 /****************************************************************************
1239 print a file
1240 ****************************************************************************/
1241 static void cmd_print(void)
1243 pstring lname;
1244 pstring rname;
1245 char *p;
1247 if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1248 DEBUG(0,("print <filename>\n"));
1249 return;
1252 pstrcpy(rname,lname);
1253 p = strrchr(rname,'/');
1254 if (p) {
1255 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1258 if (strequal(lname,"-")) {
1259 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1262 do_put(rname, lname);
1266 /****************************************************************************
1267 show a print queue entry
1268 ****************************************************************************/
1269 static void queue_fn(struct print_job_info *p)
1271 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1274 /****************************************************************************
1275 show a print queue
1276 ****************************************************************************/
1277 static void cmd_queue(void)
1279 cli_print_queue(cli, queue_fn);
1282 /****************************************************************************
1283 delete some files
1284 ****************************************************************************/
1285 static void do_del(file_info *finfo)
1287 pstring mask;
1289 pstrcpy(mask,cur_dir);
1290 pstrcat(mask,finfo->name);
1292 if (finfo->mode & aDIR)
1293 return;
1295 if (!cli_unlink(cli, mask)) {
1296 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),CNV_LANG(mask)));
1300 /****************************************************************************
1301 delete some files
1302 ****************************************************************************/
1303 static void cmd_del(void)
1305 pstring mask;
1306 fstring buf;
1307 uint16 attribute = aSYSTEM | aHIDDEN;
1309 if (recurse)
1310 attribute |= aDIR;
1312 pstrcpy(mask,cur_dir);
1314 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1315 DEBUG(0,("del <filename>\n"));
1316 return;
1318 pstrcat(mask,buf);
1320 do_list(mask, attribute,do_del,False,False);
1323 /****************************************************************************
1324 ****************************************************************************/
1325 static void cmd_open(void)
1327 pstring mask;
1328 fstring buf;
1330 pstrcpy(mask,cur_dir);
1332 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1333 DEBUG(0,("del <filename>\n"));
1334 return;
1336 pstrcat(mask,buf);
1338 cli_open(cli, mask, O_RDWR, DENY_ALL);
1342 /****************************************************************************
1343 remove a directory
1344 ****************************************************************************/
1345 static void cmd_rmdir(void)
1347 pstring mask;
1348 fstring buf;
1350 pstrcpy(mask,cur_dir);
1352 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1353 DEBUG(0,("rmdir <dirname>\n"));
1354 return;
1356 pstrcat(mask,buf);
1358 if (!cli_rmdir(cli, mask)) {
1359 DEBUG(0,("%s removing remote directory file %s\n",
1360 cli_errstr(cli),CNV_LANG(mask)));
1364 /****************************************************************************
1365 rename some files
1366 ****************************************************************************/
1367 static void cmd_rename(void)
1369 pstring src,dest;
1370 fstring buf,buf2;
1372 pstrcpy(src,cur_dir);
1373 pstrcpy(dest,cur_dir);
1375 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1376 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1377 DEBUG(0,("rename <src> <dest>\n"));
1378 return;
1381 pstrcat(src,buf);
1382 pstrcat(dest,buf2);
1384 if (!cli_rename(cli, src, dest)) {
1385 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1386 return;
1391 /****************************************************************************
1392 toggle the prompt flag
1393 ****************************************************************************/
1394 static void cmd_prompt(void)
1396 prompt = !prompt;
1397 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1401 /****************************************************************************
1402 set the newer than time
1403 ****************************************************************************/
1404 static void cmd_newer(void)
1406 fstring buf;
1407 BOOL ok;
1408 SMB_STRUCT_STAT sbuf;
1410 ok = next_token(NULL,buf,NULL,sizeof(buf));
1411 if (ok && (dos_stat(buf,&sbuf) == 0)) {
1412 newer_than = sbuf.st_mtime;
1413 DEBUG(1,("Getting files newer than %s",
1414 asctime(LocalTime(&newer_than))));
1415 } else {
1416 newer_than = 0;
1419 if (ok && newer_than == 0)
1420 DEBUG(0,("Error setting newer-than time\n"));
1423 /****************************************************************************
1424 set the archive level
1425 ****************************************************************************/
1426 static void cmd_archive(void)
1428 fstring buf;
1430 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1431 archive_level = atoi(buf);
1432 } else
1433 DEBUG(0,("Archive level is %d\n",archive_level));
1436 /****************************************************************************
1437 toggle the lowercaseflag
1438 ****************************************************************************/
1439 static void cmd_lowercase(void)
1441 lowercase = !lowercase;
1442 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1448 /****************************************************************************
1449 toggle the recurse flag
1450 ****************************************************************************/
1451 static void cmd_recurse(void)
1453 recurse = !recurse;
1454 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1457 /****************************************************************************
1458 toggle the translate flag
1459 ****************************************************************************/
1460 static void cmd_translate(void)
1462 translation = !translation;
1463 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1464 translation?"on":"off"));
1468 /****************************************************************************
1469 do a printmode command
1470 ****************************************************************************/
1471 static void cmd_printmode(void)
1473 fstring buf;
1474 fstring mode;
1476 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1477 if (strequal(buf,"text")) {
1478 printmode = 0;
1479 } else {
1480 if (strequal(buf,"graphics"))
1481 printmode = 1;
1482 else
1483 printmode = atoi(buf);
1487 switch(printmode)
1489 case 0:
1490 fstrcpy(mode,"text");
1491 break;
1492 case 1:
1493 fstrcpy(mode,"graphics");
1494 break;
1495 default:
1496 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1497 break;
1500 DEBUG(2,("the printmode is now %s\n",mode));
1503 /****************************************************************************
1504 do the lcd command
1505 ****************************************************************************/
1506 static void cmd_lcd(void)
1508 fstring buf;
1509 pstring d;
1511 if (next_token(NULL,buf,NULL,sizeof(buf)))
1512 chdir(buf);
1513 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
1516 /****************************************************************************
1517 list a share name
1518 ****************************************************************************/
1519 static void browse_fn(const char *name, uint32 m, const char *comment)
1521 fstring typestr;
1522 *typestr=0;
1524 switch (m)
1526 case STYPE_DISKTREE:
1527 fstrcpy(typestr,"Disk"); break;
1528 case STYPE_PRINTQ:
1529 fstrcpy(typestr,"Printer"); break;
1530 case STYPE_DEVICE:
1531 fstrcpy(typestr,"Device"); break;
1532 case STYPE_IPC:
1533 fstrcpy(typestr,"IPC"); break;
1536 printf("\t%-15.15s%-10.10s%s\n",
1537 name, typestr,comment);
1541 /****************************************************************************
1542 try and browse available connections on a host
1543 ****************************************************************************/
1544 static BOOL browse_host(BOOL sort)
1546 printf("\n\tSharename Type Comment\n");
1547 printf("\t--------- ---- -------\n");
1549 return cli_RNetShareEnum(cli, browse_fn);
1552 /****************************************************************************
1553 list a server name
1554 ****************************************************************************/
1555 static void server_fn(const char *name, uint32 m, const char *comment)
1557 printf("\t%-16.16s %s\n", name, comment);
1560 /****************************************************************************
1561 try and browse available connections on a host
1562 ****************************************************************************/
1563 static BOOL list_servers(char *wk_grp)
1565 printf("\n\tServer Comment\n");
1566 printf("\t--------- -------\n");
1568 cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, server_fn);
1570 printf("\n\tWorkgroup Master\n");
1571 printf("\t--------- -------\n");
1573 cli_NetServerEnum(cli, workgroup, SV_TYPE_DOMAIN_ENUM, server_fn);
1574 return True;
1578 /* Some constants for completing filename arguments */
1580 #define COMPL_NONE 0 /* No completions */
1581 #define COMPL_REMOTE 1 /* Complete remote filename */
1582 #define COMPL_LOCAL 2 /* Complete local filename */
1584 /* This defines the commands supported by this client */
1585 struct
1587 char *name;
1588 void (*fn)(void);
1589 char *description;
1590 char compl_args[2]; /* Completion argument info */
1591 } commands[] =
1593 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1594 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1595 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1596 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1597 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1598 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1599 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1600 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1601 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1602 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1603 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1604 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1605 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1606 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1607 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
1608 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1609 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1610 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1611 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1612 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1613 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1614 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1615 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1616 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1617 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1618 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1619 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1620 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1621 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1622 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1623 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1624 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1625 {"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}},
1626 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1627 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1628 {"tarmode",cmd_tarmode,
1629 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1630 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1631 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1632 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1633 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1634 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1638 /*******************************************************************
1639 lookup a command string in the list of commands, including
1640 abbreviations
1641 ******************************************************************/
1642 static int process_tok(fstring tok)
1644 int i = 0, matches = 0;
1645 int cmd=0;
1646 int tok_len = strlen(tok);
1648 while (commands[i].fn != NULL) {
1649 if (strequal(commands[i].name,tok)) {
1650 matches = 1;
1651 cmd = i;
1652 break;
1653 } else if (strnequal(commands[i].name, tok, tok_len)) {
1654 matches++;
1655 cmd = i;
1657 i++;
1660 if (matches == 0)
1661 return(-1);
1662 else if (matches == 1)
1663 return(cmd);
1664 else
1665 return(-2);
1668 /****************************************************************************
1669 help
1670 ****************************************************************************/
1671 static void cmd_help(void)
1673 int i=0,j;
1674 fstring buf;
1676 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1677 if ((i = process_tok(buf)) >= 0)
1678 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1679 } else {
1680 while (commands[i].description) {
1681 for (j=0; commands[i].description && (j<5); j++) {
1682 DEBUG(0,("%-15s",commands[i].name));
1683 i++;
1685 DEBUG(0,("\n"));
1690 /****************************************************************************
1691 wait for keyboard activity, swallowing network packets
1692 ****************************************************************************/
1693 static void wait_keyboard(void)
1695 fd_set fds;
1696 struct timeval timeout;
1698 while (1) {
1699 FD_ZERO(&fds);
1700 FD_SET(cli->fd,&fds);
1701 FD_SET(fileno(stdin),&fds);
1703 timeout.tv_sec = 20;
1704 timeout.tv_usec = 0;
1705 sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
1707 if (FD_ISSET(fileno(stdin),&fds))
1708 return;
1710 /* We deliberately use receive_smb instead of
1711 client_receive_smb as we want to receive
1712 session keepalives and then drop them here.
1714 if (FD_ISSET(cli->fd,&fds))
1715 receive_smb(cli->fd,cli->inbuf,0);
1717 cli_chkpath(cli, "\\");
1721 /****************************************************************************
1722 process a -c command string
1723 ****************************************************************************/
1724 static void process_command_string(char *cmd)
1726 pstring line;
1727 char *ptr;
1729 while (cmd[0] != '\0') {
1730 char *p;
1731 fstring tok;
1732 int i;
1734 if ((p = strchr(cmd, ';')) == 0) {
1735 strncpy(line, cmd, 999);
1736 line[1000] = '\0';
1737 cmd += strlen(cmd);
1738 } else {
1739 if (p - cmd > 999) p = cmd + 999;
1740 strncpy(line, cmd, p - cmd);
1741 line[p - cmd] = '\0';
1742 cmd = p + 1;
1745 /* input language code to internal one */
1746 CNV_INPUT (line);
1748 /* and get the first part of the command */
1749 ptr = line;
1750 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1752 if ((i = process_tok(tok)) >= 0) {
1753 commands[i].fn();
1754 } else if (i == -2) {
1755 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1756 } else {
1757 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1762 /****************************************************************************
1763 process commands on stdin
1764 ****************************************************************************/
1765 static void process_stdin(void)
1767 pstring line;
1768 char *ptr;
1770 while (!feof(stdin)) {
1771 fstring tok;
1772 int i;
1774 /* display a prompt */
1775 DEBUG(0,("smb: %s> ", CNV_LANG(cur_dir)));
1776 dbgflush( );
1778 wait_keyboard();
1780 /* and get a response */
1781 if (!fgets(line,1000,stdin))
1782 break;
1784 /* input language code to internal one */
1785 CNV_INPUT (line);
1787 /* special case - first char is ! */
1788 if (*line == '!') {
1789 system(line + 1);
1790 continue;
1793 /* and get the first part of the command */
1794 ptr = line;
1795 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1797 if ((i = process_tok(tok)) >= 0) {
1798 commands[i].fn();
1799 } else if (i == -2) {
1800 DEBUG(0,("%s: command abbreviation ambiguous\n",CNV_LANG(tok)));
1801 } else {
1802 DEBUG(0,("%s: command not found\n",CNV_LANG(tok)));
1808 /*****************************************************
1809 return a connection to a server
1810 *******************************************************/
1811 struct cli_state *do_connect(char *server, char *share)
1813 struct cli_state *c;
1814 struct nmb_name called, calling;
1815 char *server_n;
1816 struct in_addr ip;
1817 extern struct in_addr ipzero;
1819 if (*share == '\\') {
1820 server = share+2;
1821 share = strchr(server,'\\');
1822 if (!share) return NULL;
1823 *share = 0;
1824 share++;
1827 server_n = server;
1829 ip = ipzero;
1831 make_nmb_name(&calling, global_myname, 0x0, "");
1832 make_nmb_name(&called , server, name_type, "");
1834 again:
1835 ip = ipzero;
1836 if (have_ip) ip = dest_ip;
1838 /* have to open a new connection */
1839 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) == 0) ||
1840 !cli_connect(c, server_n, &ip)) {
1841 DEBUG(0,("Connection to %s failed\n", server_n));
1842 return NULL;
1845 if (!cli_session_request(c, &calling, &called)) {
1846 DEBUG(0,("session request to %s failed\n", called.name));
1847 cli_shutdown(c);
1848 if (strcmp(called.name, "*SMBSERVER")) {
1849 make_nmb_name(&called , "*SMBSERVER", 0x20, "");
1850 goto again;
1852 return NULL;
1855 DEBUG(4,(" session request ok\n"));
1857 if (!cli_negprot(c)) {
1858 DEBUG(0,("protocol negotiation failed\n"));
1859 cli_shutdown(c);
1860 return NULL;
1863 if (!got_pass) {
1864 char *pass = getpass("Password: ");
1865 if (pass) {
1866 pstrcpy(password, pass);
1870 if (!cli_session_setup(c, username,
1871 password, strlen(password),
1872 password, strlen(password),
1873 workgroup)) {
1874 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
1875 return NULL;
1879 * These next two lines are needed to emulate
1880 * old client behaviour for people who have
1881 * scripts based on client output.
1882 * QUESTION ? Do we want to have a 'client compatibility
1883 * mode to turn these on/off ? JRA.
1886 if (*c->server_domain || *c->server_os || *c->server_type)
1887 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
1888 c->server_domain,c->server_os,c->server_type));
1890 DEBUG(4,(" session setup ok\n"));
1892 if (!cli_send_tconX(c, share, "?????",
1893 password, strlen(password)+1)) {
1894 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
1895 cli_shutdown(c);
1896 return NULL;
1899 DEBUG(4,(" tconx ok\n"));
1901 return c;
1905 /****************************************************************************
1906 process commands from the client
1907 ****************************************************************************/
1908 static BOOL process(char *base_directory)
1910 cli = do_connect(desthost, service);
1911 if (!cli) {
1912 return(False);
1915 if (*base_directory) do_cd(base_directory);
1917 if (cmdstr) {
1918 process_command_string(cmdstr);
1919 } else {
1920 process_stdin();
1923 cli_shutdown(cli);
1924 return(True);
1927 /****************************************************************************
1928 usage on the program
1929 ****************************************************************************/
1930 static void usage(char *pname)
1932 DEBUG(0,("Usage: %s service <password> [options]", pname));
1934 DEBUG(0,("\nVersion %s\n",VERSION));
1935 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
1936 DEBUG(0,("\t-B IP addr broadcast IP address to use\n"));
1937 DEBUG(0,("\t-O socket_options socket options to use\n"));
1938 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
1939 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
1940 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
1941 DEBUG(0,("\t-N don't ask for a password\n"));
1942 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
1943 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
1944 DEBUG(0,("\t-P connect to service as a printer\n"));
1945 DEBUG(0,("\t-p port connect to the specified port\n"));
1946 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
1947 DEBUG(0,("\t-h Print this help message.\n"));
1948 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
1949 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
1950 DEBUG(0,("\t-U username set the network username\n"));
1951 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
1952 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
1953 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
1954 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
1955 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
1956 DEBUG(0,("\t-D directory start from directory\n"));
1957 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
1958 DEBUG(0,("\t-b xmit/send buffer changes the transmit/send buffer (default: 65520)\n"));
1959 DEBUG(0,("\n"));
1963 /****************************************************************************
1964 get a password from a a file or file descriptor
1965 exit on failure
1966 ****************************************************************************/
1967 static void get_password_file(void)
1969 int fd = -1;
1970 char *p;
1971 BOOL close_it = False;
1972 pstring spec;
1973 char pass[128];
1975 if ((p = getenv("PASSWD_FD")) != NULL) {
1976 pstrcpy(spec, "descriptor ");
1977 pstrcat(spec, p);
1978 sscanf(p, "%d", &fd);
1979 close_it = False;
1980 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
1981 fd = sys_open(p, O_RDONLY, 0);
1982 pstrcpy(spec, p);
1983 if (fd < 0) {
1984 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
1985 spec, strerror(errno));
1986 exit(1);
1988 close_it = True;
1991 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
1992 p && p - pass < sizeof(pass);) {
1993 switch (read(fd, p, 1)) {
1994 case 1:
1995 if (*p != '\n' && *p != '\0') {
1996 *++p = '\0'; /* advance p, and null-terminate pass */
1997 break;
1999 case 0:
2000 if (p - pass) {
2001 *p = '\0'; /* null-terminate it, just in case... */
2002 p = NULL; /* then force the loop condition to become false */
2003 break;
2004 } else {
2005 fprintf(stderr, "Error reading password from file %s: %s\n",
2006 spec, "empty password\n");
2007 exit(1);
2010 default:
2011 fprintf(stderr, "Error reading password from file %s: %s\n",
2012 spec, strerror(errno));
2013 exit(1);
2016 pstrcpy(password, pass);
2017 if (close_it)
2018 close(fd);
2023 /****************************************************************************
2024 handle a -L query
2025 ****************************************************************************/
2026 static int do_host_query(char *query_host)
2028 cli = do_connect(query_host, "IPC$");
2029 if (!cli)
2030 return 1;
2032 browse_host(True);
2033 list_servers(workgroup);
2035 cli_shutdown(cli);
2037 return(0);
2041 /****************************************************************************
2042 handle a tar operation
2043 ****************************************************************************/
2044 static int do_tar_op(char *base_directory)
2046 int ret;
2047 cli = do_connect(desthost, service);
2048 if (!cli)
2049 return 1;
2051 recurse=True;
2053 if (*base_directory) do_cd(base_directory);
2055 ret=process_tar();
2057 cli_shutdown(cli);
2059 return(ret);
2062 /****************************************************************************
2063 handle a message operation
2064 ****************************************************************************/
2065 static int do_message_op(void)
2067 struct in_addr ip;
2068 struct nmb_name called, calling;
2070 ip = ipzero;
2072 make_nmb_name(&calling, global_myname, 0x0, "");
2073 make_nmb_name(&called , desthost, name_type, "");
2075 ip = ipzero;
2076 if (have_ip) ip = dest_ip;
2078 if (!(cli=cli_initialise(NULL)) || !cli_connect(cli, desthost, &ip)) {
2079 DEBUG(0,("Connection to %s failed\n", desthost));
2080 return 1;
2083 if (!cli_session_request(cli, &calling, &called)) {
2084 DEBUG(0,("session request failed\n"));
2085 cli_shutdown(cli);
2086 return 1;
2089 send_message();
2090 cli_shutdown(cli);
2092 return 0;
2096 /****************************************************************************
2097 main program
2098 ****************************************************************************/
2099 int main(int argc,char *argv[])
2101 fstring base_directory;
2102 char *pname = argv[0];
2103 int opt;
2104 extern FILE *dbf;
2105 extern char *optarg;
2106 extern int optind;
2107 pstring query_host;
2108 BOOL message = False;
2109 BOOL explicit_user = False;
2110 extern char tar_type;
2111 static pstring servicesf = CONFIGFILE;
2112 pstring term_code;
2113 pstring new_name_resolve_order;
2114 char *p;
2116 #ifdef KANJI
2117 pstrcpy(term_code, KANJI);
2118 #else /* KANJI */
2119 *term_code = 0;
2120 #endif /* KANJI */
2122 *query_host = 0;
2123 *base_directory = 0;
2125 *new_name_resolve_order = 0;
2127 DEBUGLEVEL = 2;
2129 setup_logging(pname,True);
2132 * If the -E option is given, be careful not to clobber stdout
2133 * before processing the options. 28.Feb.99, richard@hacom.nl.
2134 * Also pre-parse the -s option to get the service file name.
2137 for (opt = 1; opt < argc; opt++) {
2138 if (strcmp(argv[opt], "-E") == 0)
2139 dbf = stderr;
2140 else if(strncmp(argv[opt], "-s", 2) == 0) {
2141 if(argv[opt][2] != '\0')
2142 pstrcpy(servicesf, &argv[opt][2]);
2143 else if(argv[opt+1] != NULL) {
2145 * At least one more arg left.
2147 pstrcpy(servicesf, argv[opt+1]);
2148 } else {
2149 usage(pname);
2150 exit(1);
2155 TimeInit();
2156 charset_initialise();
2158 if(!get_myname(myhostname,NULL)) {
2159 DEBUG(0,("Failed to get my hostname.\n"));
2162 in_client = True; /* Make sure that we tell lp_load we are */
2164 if (!lp_load(servicesf,True,False,False)) {
2165 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
2168 codepage_initialise(lp_client_code_page());
2170 #ifdef WITH_SSL
2171 sslutil_init(0);
2172 #endif
2174 pstrcpy(workgroup,lp_workgroup());
2176 load_interfaces();
2177 myumask = umask(0);
2178 umask(myumask);
2180 if (getenv("USER")) {
2181 pstrcpy(username,getenv("USER"));
2183 /* modification to support userid%passwd syntax in the USER var
2184 25.Aug.97, jdblair@uab.edu */
2186 if ((p=strchr(username,'%'))) {
2187 *p = 0;
2188 pstrcpy(password,p+1);
2189 got_pass = True;
2190 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
2192 strupper(username);
2195 /* modification to support PASSWD environmental var
2196 25.Aug.97, jdblair@uab.edu */
2197 if (getenv("PASSWD")) {
2198 pstrcpy(password,getenv("PASSWD"));
2199 got_pass = True;
2202 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2203 get_password_file();
2204 got_pass = True;
2207 if (*username == 0 && getenv("LOGNAME")) {
2208 pstrcpy(username,getenv("LOGNAME"));
2209 strupper(username);
2212 if (*username == 0) {
2213 pstrcpy(username,"GUEST");
2216 if (argc < 2) {
2217 usage(pname);
2218 exit(1);
2221 if (*argv[1] != '-') {
2222 pstrcpy(service,argv[1]);
2223 /* Convert any '/' characters in the service name to '\' characters */
2224 string_replace( service, '/','\\');
2225 argc--;
2226 argv++;
2228 if (count_chars(service,'\\') < 3) {
2229 usage(pname);
2230 printf("\n%s: Not enough '\\' characters in service\n",service);
2231 exit(1);
2234 if (argc > 1 && (*argv[1] != '-')) {
2235 got_pass = True;
2236 pstrcpy(password,argv[1]);
2237 memset(argv[1],'X',strlen(argv[1]));
2238 argc--;
2239 argv++;
2243 while ((opt =
2244 getopt(argc, argv,"s:B:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:")) != EOF) {
2245 switch (opt) {
2246 case 's':
2247 pstrcpy(servicesf, optarg);
2248 break;
2249 case 'B':
2250 iface_set_default(NULL,optarg,NULL);
2251 break;
2252 case 'O':
2253 pstrcpy(user_socket_options,optarg);
2254 break;
2255 case 'R':
2256 pstrcpy(new_name_resolve_order, optarg);
2257 break;
2258 case 'M':
2259 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2260 pstrcpy(desthost,optarg);
2261 message = True;
2262 break;
2263 case 'i':
2264 pstrcpy(scope,optarg);
2265 break;
2266 case 'N':
2267 got_pass = True;
2268 break;
2269 case 'n':
2270 pstrcpy(global_myname,optarg);
2271 break;
2272 case 'd':
2273 if (*optarg == 'A')
2274 DEBUGLEVEL = 10000;
2275 else
2276 DEBUGLEVEL = atoi(optarg);
2277 break;
2278 case 'P':
2279 /* not needed anymore */
2280 break;
2281 case 'p':
2282 port = atoi(optarg);
2283 break;
2284 case 'l':
2285 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2286 break;
2287 case 'h':
2288 usage(pname);
2289 exit(0);
2290 break;
2291 case 'I':
2293 dest_ip = *interpret_addr2(optarg);
2294 if (zero_ip(dest_ip))
2295 exit(1);
2296 have_ip = True;
2298 break;
2299 case 'E':
2300 dbf = stderr;
2301 break;
2302 case 'U':
2304 char *lp;
2305 explicit_user = True;
2306 pstrcpy(username,optarg);
2307 if ((lp=strchr(username,'%'))) {
2308 *lp = 0;
2309 pstrcpy(password,lp+1);
2310 got_pass = True;
2311 memset(strchr(optarg,'%')+1,'X',strlen(password));
2314 break;
2315 case 'L':
2316 p = optarg;
2317 while(*p == '\\' || *p == '/')
2318 p++;
2319 pstrcpy(query_host,p);
2321 if(!explicit_user)
2322 *username = '\0';
2323 break;
2324 case 't':
2325 pstrcpy(term_code, optarg);
2326 break;
2327 case 'm':
2328 /* no longer supported */
2329 break;
2330 case 'W':
2331 pstrcpy(workgroup,optarg);
2332 break;
2333 case 'T':
2334 if (!tar_parseargs(argc, argv, optarg, optind)) {
2335 usage(pname);
2336 exit(1);
2338 break;
2339 case 'D':
2340 pstrcpy(base_directory,optarg);
2341 break;
2342 case 'c':
2343 cmdstr = optarg;
2344 got_pass = True;
2345 break;
2346 case 'b':
2347 io_bufsize = MAX(1, atoi(optarg));
2348 break;
2349 default:
2350 usage(pname);
2351 exit(1);
2355 get_myname((*global_myname)?NULL:global_myname,NULL);
2357 if(*new_name_resolve_order)
2358 lp_set_name_resolve_order(new_name_resolve_order);
2360 if (*term_code)
2361 interpret_coding_system(term_code);
2363 if (!tar_type && !*query_host && !*service && !message) {
2364 usage(pname);
2365 exit(1);
2368 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2370 if (tar_type) {
2371 return do_tar_op(base_directory);
2374 if ((p=strchr(query_host,'#'))) {
2375 *p = 0;
2376 p++;
2377 sscanf(p, "%x", &name_type);
2380 if (*query_host) {
2381 return do_host_query(query_host);
2384 if (message) {
2385 return do_message_op();
2388 if (!process(base_directory)) {
2389 return(1);
2392 return(0);