fix restart option (noticed by Stanford K. Baldwin)
[Samba.git] / source / client / client.c
blob89d5e685f48fb9e45658086fcf6aa99a9e0b04e8
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 extern BOOL AllowDebugChange;
33 static int port = SMB_PORT;
34 pstring cur_dir = "\\";
35 pstring cd_path = "";
36 static pstring service;
37 static pstring desthost;
38 extern pstring global_myname;
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 = 64512;
46 static int name_type = 0x20;
47 static int max_protocol = PROTOCOL_NT1;
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 int timeout_msec = 0;
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 BOOL translation = False;
67 static BOOL have_ip;
69 /* clitar bits insert */
70 extern int blocksize;
71 extern BOOL tar_inc;
72 extern BOOL tar_reset;
73 /* clitar bits end */
76 BOOL prompt = True;
78 int printmode = 1;
80 static BOOL recurse = False;
81 BOOL lowercase = False;
83 struct in_addr dest_ip;
85 #define SEPARATORS " \t\n\r"
87 BOOL abort_mget = True;
89 pstring fileselection = "";
91 extern file_info def_finfo;
93 /* timing globals */
94 off_t get_total_size = 0;
95 unsigned int get_total_time_ms = 0;
96 off_t put_total_size = 0;
97 unsigned int put_total_time_ms = 0;
99 /* totals globals */
100 static double dir_total;
102 #define USENMB
104 /****************************************************************************
105 write to a local file with CR/LF->LF translation if appropriate. return the
106 number taken from the buffer. This may not equal the number written.
107 ****************************************************************************/
108 static int writefile(int f, char *b, int n)
110 int i;
112 if (!translation) {
113 return write(f,b,n);
116 i = 0;
117 while (i < n) {
118 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
119 b++;i++;
121 if (write(f, b, 1) != 1) {
122 break;
124 b++;
125 i++;
128 return(i);
131 /****************************************************************************
132 read from a file with LF->CR/LF translation if appropriate. return the
133 number read. read approx n bytes.
134 ****************************************************************************/
135 static int readfile(char *b, int size, int n, FILE *f)
137 int i;
138 int c;
140 if (!translation || (size != 1))
141 return(fread(b,size,n,f));
143 i = 0;
144 while (i < (n - 1) && (i < BUFFER_SIZE)) {
145 if ((c = getc(f)) == EOF) {
146 break;
149 if (c == '\n') { /* change all LFs to CR/LF */
150 b[i++] = '\r';
153 b[i++] = c;
156 return(i);
160 /****************************************************************************
161 send a message
162 ****************************************************************************/
163 static void send_message(void)
165 int total_len = 0;
166 int grp_id;
168 if (!cli_message_start(cli, desthost, username, &grp_id)) {
169 DEBUG(0,("message start: %s\n", cli_errstr(cli)));
170 return;
174 printf("Connected. Type your message, ending it with a Control-D\n");
176 while (!feof(stdin) && total_len < 1600) {
177 int maxlen = MIN(1600 - total_len,127);
178 pstring msg;
179 int l=0;
180 int c;
182 ZERO_ARRAY(msg);
184 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
185 if (c == '\n')
186 msg[l++] = '\r';
187 msg[l] = c;
191 * The message is in UNIX codepage format. Convert to
192 * DOS before sending.
195 unix_to_dos(msg);
197 if (!cli_message_text(cli, msg, l, grp_id)) {
198 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
199 return;
202 total_len += l;
205 if (total_len >= 1600)
206 printf("the message was truncated to 1600 bytes\n");
207 else
208 printf("sent %d bytes\n",total_len);
210 if (!cli_message_end(cli, grp_id)) {
211 printf("SMBsendend failed (%s)\n",cli_errstr(cli));
212 return;
218 /****************************************************************************
219 check the space on a device
220 ****************************************************************************/
221 static void do_dskattr(void)
223 int total, bsize, avail;
225 if (!cli_dskattr(cli, &bsize, &total, &avail)) {
226 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli)));
227 return;
230 DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
231 total, bsize, avail));
234 /****************************************************************************
235 show cd/pwd
236 ****************************************************************************/
237 static void cmd_pwd(void)
239 DEBUG(0,("Current directory is %s",service));
240 DEBUG(0,("%s\n",cur_dir));
244 /****************************************************************************
245 change directory - inner section
246 ****************************************************************************/
247 static void do_cd(char *newdir)
249 char *p = newdir;
250 pstring saved_dir;
251 pstring dname;
253 dos_format(newdir);
255 /* Save the current directory in case the
256 new directory is invalid */
257 pstrcpy(saved_dir, cur_dir);
258 if (*p == '\\')
259 pstrcpy(cur_dir,p);
260 else
261 pstrcat(cur_dir,p);
262 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
263 pstrcat(cur_dir, "\\");
265 dos_clean_name(cur_dir);
266 pstrcpy(dname,cur_dir);
267 pstrcat(cur_dir,"\\");
268 dos_clean_name(cur_dir);
270 if (!strequal(cur_dir,"\\")) {
271 if (!cli_chkpath(cli, dname)) {
272 DEBUG(0,("cd %s: %s\n", dname, cli_errstr(cli)));
273 pstrcpy(cur_dir,saved_dir);
277 pstrcpy(cd_path,cur_dir);
280 /****************************************************************************
281 change directory
282 ****************************************************************************/
283 static void cmd_cd(void)
285 fstring buf;
287 if (next_token(NULL,buf,NULL,sizeof(buf)))
288 do_cd(buf);
289 else
290 DEBUG(0,("Current directory is %s\n",cur_dir));
294 /*******************************************************************
295 decide if a file should be operated on
296 ********************************************************************/
297 static BOOL do_this_one(file_info *finfo)
299 if (finfo->mode & aDIR) return(True);
301 if (*fileselection &&
302 !mask_match(finfo->name,fileselection,False)) {
303 DEBUG(3,("mask_match %s failed\n", finfo->name));
304 return False;
307 if (newer_than && finfo->mtime < newer_than) {
308 DEBUG(3,("newer_than %s failed\n", finfo->name));
309 return(False);
312 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
313 DEBUG(3,("archive %s failed\n", finfo->name));
314 return(False);
317 return(True);
320 /****************************************************************************
321 display info about a file
322 ****************************************************************************/
323 static void display_finfo(file_info *finfo)
325 if (do_this_one(finfo)) {
326 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
327 DEBUG(0,(" %-30s%7.7s %8.0f %s",
328 finfo->name,
329 attrib_string(finfo->mode),
330 (double)finfo->size,
331 asctime(LocalTime(&t))));
332 dir_total += finfo->size;
337 /****************************************************************************
338 accumulate size of a file
339 ****************************************************************************/
340 static void do_du(file_info *finfo)
342 if (do_this_one(finfo)) {
343 dir_total += finfo->size;
347 static BOOL do_list_recurse;
348 static BOOL do_list_dirs;
349 static char *do_list_queue = 0;
350 static long do_list_queue_size = 0;
351 static long do_list_queue_start = 0;
352 static long do_list_queue_end = 0;
353 static void (*do_list_fn)(file_info *);
355 /****************************************************************************
356 functions for do_list_queue
357 ****************************************************************************/
360 * The do_list_queue is a NUL-separated list of strings stored in a
361 * char*. Since this is a FIFO, we keep track of the beginning and
362 * ending locations of the data in the queue. When we overflow, we
363 * double the size of the char*. When the start of the data passes
364 * the midpoint, we move everything back. This is logically more
365 * complex than a linked list, but easier from a memory management
366 * angle. In any memory error condition, do_list_queue is reset.
367 * Functions check to ensure that do_list_queue is non-NULL before
368 * accessing it.
370 static void reset_do_list_queue(void)
372 SAFE_FREE(do_list_queue);
373 do_list_queue = 0;
374 do_list_queue_size = 0;
375 do_list_queue_start = 0;
376 do_list_queue_end = 0;
379 static void init_do_list_queue(void)
381 reset_do_list_queue();
382 do_list_queue_size = 1024;
383 do_list_queue = malloc(do_list_queue_size);
384 if (do_list_queue == 0) {
385 DEBUG(0,("malloc fail for size %d\n",
386 (int)do_list_queue_size));
387 reset_do_list_queue();
388 } else {
389 memset(do_list_queue, 0, do_list_queue_size);
393 static void adjust_do_list_queue(void)
396 * If the starting point of the queue is more than half way through,
397 * move everything toward the beginning.
399 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
401 DEBUG(4,("do_list_queue is empty\n"));
402 do_list_queue_start = do_list_queue_end = 0;
403 *do_list_queue = '\0';
405 else if (do_list_queue_start > (do_list_queue_size / 2))
407 DEBUG(4,("sliding do_list_queue backward\n"));
408 memmove(do_list_queue,
409 do_list_queue + do_list_queue_start,
410 do_list_queue_end - do_list_queue_start);
411 do_list_queue_end -= do_list_queue_start;
412 do_list_queue_start = 0;
417 static void add_to_do_list_queue(const char* entry)
419 char *dlq;
421 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
422 while (new_end > do_list_queue_size)
424 do_list_queue_size *= 2;
425 DEBUG(4,("enlarging do_list_queue to %d\n",
426 (int)do_list_queue_size));
427 dlq = Realloc(do_list_queue, do_list_queue_size);
428 if (!dlq) {
429 DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
430 (int)do_list_queue_size));
431 reset_do_list_queue();
432 } else {
433 do_list_queue = dlq;
434 memset(do_list_queue + do_list_queue_size / 2,
435 0, do_list_queue_size / 2);
438 if (do_list_queue)
440 pstrcpy(do_list_queue + do_list_queue_end, entry);
441 do_list_queue_end = new_end;
442 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
443 entry, (int)do_list_queue_start, (int)do_list_queue_end));
447 static char *do_list_queue_head(void)
449 return do_list_queue + do_list_queue_start;
452 static void remove_do_list_queue_head(void)
454 if (do_list_queue_end > do_list_queue_start)
456 do_list_queue_start += strlen(do_list_queue_head()) + 1;
457 adjust_do_list_queue();
458 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
459 (int)do_list_queue_start, (int)do_list_queue_end));
463 static int do_list_queue_empty(void)
465 return (! (do_list_queue && *do_list_queue));
468 /****************************************************************************
469 a helper for do_list
470 ****************************************************************************/
471 static void do_list_helper(file_info *f, const char *mask, void *state)
473 if (f->mode & aDIR) {
474 if (do_list_dirs && do_this_one(f)) {
475 do_list_fn(f);
477 if (do_list_recurse &&
478 !strequal(f->name,".") &&
479 !strequal(f->name,"..")) {
480 pstring mask2;
481 char *p;
483 pstrcpy(mask2, mask);
484 p = strrchr(mask2,'\\');
485 if (!p) return;
486 p[1] = 0;
487 pstrcat(mask2, f->name);
488 pstrcat(mask2,"\\*");
489 add_to_do_list_queue(mask2);
491 return;
494 if (do_this_one(f)) {
495 do_list_fn(f);
500 /****************************************************************************
501 a wrapper around cli_list that adds recursion
502 ****************************************************************************/
503 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
505 static int in_do_list = 0;
507 if (in_do_list && rec)
509 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
510 exit(1);
513 in_do_list = 1;
515 do_list_recurse = rec;
516 do_list_dirs = dirs;
517 do_list_fn = fn;
519 if (rec)
521 init_do_list_queue();
522 add_to_do_list_queue(mask);
524 while (! do_list_queue_empty())
527 * Need to copy head so that it doesn't become
528 * invalid inside the call to cli_list. This
529 * would happen if the list were expanded
530 * during the call.
531 * Fix from E. Jay Berkenbilt (ejb@ql.org)
533 pstring head;
534 pstrcpy(head, do_list_queue_head());
535 cli_list(cli, head, attribute, do_list_helper, NULL);
536 remove_do_list_queue_head();
537 if ((! do_list_queue_empty()) && (fn == display_finfo))
539 char* next_file = do_list_queue_head();
540 char* save_ch = 0;
541 if ((strlen(next_file) >= 2) &&
542 (next_file[strlen(next_file) - 1] == '*') &&
543 (next_file[strlen(next_file) - 2] == '\\'))
545 save_ch = next_file +
546 strlen(next_file) - 2;
547 *save_ch = '\0';
549 DEBUG(0,("\n%s\n",next_file));
550 if (save_ch)
552 *save_ch = '\\';
557 else
559 if (cli_list(cli, mask, attribute, do_list_helper, NULL) == -1)
561 DEBUG(0, ("%s listing %s\n", cli_errstr(cli), mask));
565 in_do_list = 0;
566 reset_do_list_queue();
569 /****************************************************************************
570 get a directory listing
571 ****************************************************************************/
572 static void cmd_dir(void)
574 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
575 pstring mask;
576 fstring buf;
577 char *p=buf;
579 dir_total = 0;
580 pstrcpy(mask,cur_dir);
581 if(mask[strlen(mask)-1]!='\\')
582 pstrcat(mask,"\\");
584 if (next_token(NULL,buf,NULL,sizeof(buf))) {
585 dos_format(p);
586 if (*p == '\\')
587 pstrcpy(mask,p);
588 else
589 pstrcat(mask,p);
591 else {
592 pstrcat(mask,"*");
595 do_list(mask, attribute, display_finfo, recurse, True);
597 do_dskattr();
599 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
603 /****************************************************************************
604 get a directory listing
605 ****************************************************************************/
606 static void cmd_du(void)
608 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
609 pstring mask;
610 fstring buf;
611 char *p=buf;
613 dir_total = 0;
614 pstrcpy(mask,cur_dir);
615 if(mask[strlen(mask)-1]!='\\')
616 pstrcat(mask,"\\");
618 if (next_token(NULL,buf,NULL,sizeof(buf))) {
619 dos_format(p);
620 if (*p == '\\')
621 pstrcpy(mask,p);
622 else
623 pstrcat(mask,p);
624 } else {
625 pstrcat(mask,"*");
628 do_list(mask, attribute, do_du, recurse, True);
630 do_dskattr();
632 DEBUG(0, ("Total number of bytes: %.0f\n", dir_total));
636 /****************************************************************************
637 get a file from rname to lname
638 ****************************************************************************/
639 static void do_get(char *rname,char *lname)
641 int handle=0,fnum;
642 BOOL newhandle = False;
643 char *data;
644 struct timeval tp_start;
645 int read_size = io_bufsize;
646 uint16 attr;
647 size_t old_size = 0;
648 SMB_BIG_UINT size = 0;
649 SMB_BIG_UINT nread = 0;
651 GetTimeOfDay(&tp_start);
653 if (lowercase) {
654 strlower(lname);
657 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
659 if (fnum == -1) {
660 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
661 return;
664 if(!strcmp(lname,"-")) {
665 handle = fileno(stdout);
666 } else {
667 handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
668 newhandle = True;
670 if (handle < 0) {
671 DEBUG(0,("Error opening local file %s\n",lname));
672 return;
675 if (!cli_qfileinfo(cli, fnum,
676 &attr, &old_size, NULL, NULL, NULL, NULL, NULL) &&
677 !cli_getattrE(cli, fnum,
678 &attr, &size, NULL, NULL, NULL)) {
679 DEBUG(0,("getattrib: %s\n",cli_errstr(cli)));
680 return;
683 DEBUG(2,("getting file %s of size %.0f as %s ",
684 rname, (double)size, lname));
686 if(!(data = (char *)malloc(read_size))) {
687 DEBUG(0,("malloc fail for size %d\n", read_size));
688 cli_close(cli, fnum);
689 return;
692 while (1) {
693 int n = cli_read(cli, fnum, data, nread, read_size);
695 if (n <= 0) break;
697 if (writefile(handle,data, n) != n) {
698 DEBUG(0,("Error writing local file\n"));
699 break;
702 nread += n;
705 if (nread < size) {
706 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
707 rname, (long)nread));
710 SAFE_FREE(data);
712 if (!cli_close(cli, fnum)) {
713 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
716 if (newhandle) {
717 close(handle);
720 if (archive_level >= 2 && (attr & aARCH)) {
721 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
725 struct timeval tp_end;
726 int this_time;
728 GetTimeOfDay(&tp_end);
729 this_time =
730 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
731 (tp_end.tv_usec - tp_start.tv_usec)/1000;
732 get_total_time_ms += this_time;
733 get_total_size += nread;
735 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
736 nread / (1.024*this_time + 1.0e-4),
737 get_total_size / (1.024*get_total_time_ms)));
742 /****************************************************************************
743 get a file
744 ****************************************************************************/
745 static void cmd_get(void)
747 pstring lname;
748 pstring rname;
749 char *p;
751 pstrcpy(rname,cur_dir);
752 pstrcat(rname,"\\");
754 p = rname + strlen(rname);
756 if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
757 DEBUG(0,("get <filename>\n"));
758 return;
760 pstrcpy(lname,p);
761 dos_clean_name(rname);
763 next_token(NULL,lname,NULL,sizeof(lname));
765 do_get(rname, lname);
769 /****************************************************************************
770 do a mget operation on one file
771 ****************************************************************************/
772 static void do_mget(file_info *finfo)
774 pstring rname;
775 pstring quest;
776 pstring saved_curdir;
777 pstring mget_mask;
779 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
780 return;
782 if (abort_mget) {
783 DEBUG(0,("mget aborted\n"));
784 return;
787 if (finfo->mode & aDIR)
788 slprintf(quest,sizeof(pstring)-1,
789 "Get directory %s? ",finfo->name);
790 else
791 slprintf(quest,sizeof(pstring)-1,
792 "Get file %s? ",finfo->name);
794 if (prompt && !yesno(quest)) return;
796 if (!(finfo->mode & aDIR)) {
797 pstrcpy(rname,cur_dir);
798 pstrcat(rname,finfo->name);
799 do_get(rname,finfo->name);
800 return;
803 /* handle directories */
804 pstrcpy(saved_curdir,cur_dir);
806 pstrcat(cur_dir,finfo->name);
807 pstrcat(cur_dir,"\\");
809 unix_format(finfo->name);
810 if (lowercase)
811 strlower(finfo->name);
813 if (!directory_exist(finfo->name,NULL) &&
814 mkdir(finfo->name,0777) != 0) {
815 DEBUG(0,("failed to create directory %s\n",finfo->name));
816 pstrcpy(cur_dir,saved_curdir);
817 return;
820 if (chdir(finfo->name) != 0) {
821 DEBUG(0,("failed to chdir to directory %s\n",finfo->name));
822 pstrcpy(cur_dir,saved_curdir);
823 return;
826 pstrcpy(mget_mask,cur_dir);
827 pstrcat(mget_mask,"*");
829 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
830 chdir("..");
831 pstrcpy(cur_dir,saved_curdir);
835 /****************************************************************************
836 view the file using the pager
837 ****************************************************************************/
838 static void cmd_more(void)
840 fstring rname,lname,pager_cmd;
841 char *pager;
842 int fd;
844 fstrcpy(rname,cur_dir);
845 fstrcat(rname,"\\");
847 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
848 fd = smb_mkstemp(lname);
849 if (fd == -1) {
850 DEBUG(0,("failed to create temporary file for more\n"));
851 return;
853 close(fd);
855 if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
856 DEBUG(0,("more <filename>\n"));
857 unlink(lname);
858 return;
860 dos_clean_name(rname);
862 do_get(rname,lname);
864 pager=getenv("PAGER");
866 slprintf(pager_cmd,sizeof(pager_cmd)-1,
867 "%s %s",(pager? pager:PAGER), lname);
868 system(pager_cmd);
869 unlink(lname);
874 /****************************************************************************
875 do a mget command
876 ****************************************************************************/
877 static void cmd_mget(void)
879 uint16 attribute = aSYSTEM | aHIDDEN;
880 pstring mget_mask;
881 fstring buf;
882 char *p=buf;
884 *mget_mask = 0;
886 if (recurse)
887 attribute |= aDIR;
889 abort_mget = False;
891 while (next_token(NULL,p,NULL,sizeof(buf))) {
892 pstrcpy(mget_mask,cur_dir);
893 if(mget_mask[strlen(mget_mask)-1]!='\\')
894 pstrcat(mget_mask,"\\");
896 if (*p == '\\')
897 pstrcpy(mget_mask,p);
898 else
899 pstrcat(mget_mask,p);
900 do_list(mget_mask, attribute,do_mget,False,True);
903 if (!*mget_mask) {
904 pstrcpy(mget_mask,cur_dir);
905 if(mget_mask[strlen(mget_mask)-1]!='\\')
906 pstrcat(mget_mask,"\\");
907 pstrcat(mget_mask,"*");
908 do_list(mget_mask, attribute,do_mget,False,True);
913 /****************************************************************************
914 make a directory of name "name"
915 ****************************************************************************/
916 static BOOL do_mkdir(char *name)
918 if (!cli_mkdir(cli, name)) {
919 DEBUG(0,("%s making remote directory %s\n",
920 cli_errstr(cli),name));
921 return(False);
924 return(True);
927 /****************************************************************************
928 Show 8.3 name of a file.
929 ****************************************************************************/
931 static BOOL do_altname(char *name)
933 fstring altname;
934 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
935 DEBUG(0,("%s getting alt name for %s\n", cli_errstr(cli),name));
936 return(False);
938 DEBUG(0,("%s\n", altname));
940 return(True);
943 /****************************************************************************
944 Exit client.
945 ****************************************************************************/
946 static void cmd_quit(void)
948 cli_shutdown(cli);
949 exit(0);
953 /****************************************************************************
954 make a directory
955 ****************************************************************************/
956 static void cmd_mkdir(void)
958 pstring mask;
959 fstring buf;
960 char *p=buf;
962 pstrcpy(mask,cur_dir);
964 if (!next_token(NULL,p,NULL,sizeof(buf))) {
965 if (!recurse)
966 DEBUG(0,("mkdir <dirname>\n"));
967 return;
969 pstrcat(mask,p);
971 if (recurse) {
972 pstring ddir;
973 pstring ddir2;
974 *ddir2 = 0;
976 pstrcpy(ddir,mask);
977 trim_string(ddir,".",NULL);
978 p = strtok(ddir,"/\\");
979 while (p) {
980 pstrcat(ddir2,p);
981 if (!cli_chkpath(cli, ddir2)) {
982 do_mkdir(ddir2);
984 pstrcat(ddir2,"\\");
985 p = strtok(NULL,"/\\");
987 } else {
988 do_mkdir(mask);
992 /****************************************************************************
993 show alt name
994 ****************************************************************************/
996 static void cmd_altname(void)
998 pstring name;
999 fstring buf;
1000 char *p=buf;
1002 pstrcpy(name,cur_dir);
1004 if (!next_token(NULL,p,NULL,sizeof(buf))) {
1005 DEBUG(0,("altname <file>\n"));
1006 return;
1008 pstrcat(name,p);
1010 do_altname(name);
1013 /****************************************************************************
1014 put a single file
1015 ****************************************************************************/
1016 static void do_put(char *rname,char *lname)
1018 int fnum;
1019 FILE *f;
1020 off_t nread=0;
1021 char *buf=NULL;
1022 int maxwrite=io_bufsize;
1024 struct timeval tp_start;
1025 GetTimeOfDay(&tp_start);
1027 fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1029 if (fnum == -1) {
1030 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
1031 return;
1034 /* allow files to be piped into smbclient
1035 jdblair 24.jun.98 */
1036 if (!strcmp(lname, "-")) {
1037 f = stdin;
1038 /* size of file is not known */
1039 } else {
1040 f = sys_fopen(lname,"r");
1043 if (!f) {
1044 DEBUG(0,("Error opening local file %s\n",lname));
1045 return;
1049 DEBUG(1,("putting file %s as %s ",lname,
1050 rname));
1052 buf = (char *)malloc(maxwrite);
1053 if (!buf) {
1054 DEBUG(0, ("ERROR: Not enough memory!\n"));
1055 return;
1057 while (!feof(f)) {
1058 int n = maxwrite;
1059 int ret;
1061 if ((n = readfile(buf,1,n,f)) < 1) {
1062 if((n == 0) && feof(f))
1063 break; /* Empty local file. */
1065 DEBUG(0,("Error reading local file: %s\n", strerror(errno) ));
1066 break;
1069 ret = cli_write(cli, fnum, 0, buf, nread, n);
1071 if (n != ret) {
1072 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
1073 break;
1076 nread += n;
1079 if (!cli_close(cli, fnum)) {
1080 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),rname));
1081 fclose(f);
1082 SAFE_FREE(buf);
1083 return;
1087 fclose(f);
1088 SAFE_FREE(buf);
1091 struct timeval tp_end;
1092 int this_time;
1094 GetTimeOfDay(&tp_end);
1095 this_time =
1096 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1097 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1098 put_total_time_ms += this_time;
1099 put_total_size += nread;
1101 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1102 nread / (1.024*this_time + 1.0e-4),
1103 put_total_size / (1.024*put_total_time_ms)));
1106 if (f == stdin) {
1107 cli_shutdown(cli);
1108 exit(0);
1114 /****************************************************************************
1115 put a file
1116 ****************************************************************************/
1117 static void cmd_put(void)
1119 pstring lname;
1120 pstring rname;
1121 fstring buf;
1122 char *p=buf;
1124 pstrcpy(rname,cur_dir);
1125 pstrcat(rname,"\\");
1127 if (!next_token(NULL,p,NULL,sizeof(buf))) {
1128 DEBUG(0,("put <filename>\n"));
1129 return;
1131 pstrcpy(lname,p);
1133 if (next_token(NULL,p,NULL,sizeof(buf)))
1134 pstrcat(rname,p);
1135 else
1136 pstrcat(rname,lname);
1138 dos_clean_name(rname);
1141 SMB_STRUCT_STAT st;
1142 /* allow '-' to represent stdin
1143 jdblair, 24.jun.98 */
1144 if (!file_exist(lname,&st) &&
1145 (strcmp(lname,"-"))) {
1146 DEBUG(0,("%s does not exist\n",lname));
1147 return;
1151 do_put(rname,lname);
1154 /*************************************
1155 File list structure
1156 *************************************/
1158 static struct file_list {
1159 struct file_list *prev, *next;
1160 char *file_path;
1161 BOOL isdir;
1162 } *file_list;
1164 /****************************************************************************
1165 Free a file_list structure
1166 ****************************************************************************/
1168 static void free_file_list (struct file_list * list)
1170 struct file_list *tmp;
1172 while (list)
1174 tmp = list;
1175 DLIST_REMOVE(list, list);
1176 SAFE_FREE(tmp->file_path);
1177 SAFE_FREE(tmp);
1181 /****************************************************************************
1182 seek in a directory/file list until you get something that doesn't start with
1183 the specified name
1184 ****************************************************************************/
1185 static BOOL seek_list(struct file_list *list, char *name)
1187 while (list) {
1188 trim_string(list->file_path,"./","\n");
1189 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1190 return(True);
1192 list = list->next;
1195 return(False);
1198 /****************************************************************************
1199 set the file selection mask
1200 ****************************************************************************/
1201 static void cmd_select(void)
1203 pstrcpy(fileselection,"");
1204 next_token(NULL,fileselection,NULL,sizeof(fileselection));
1207 /****************************************************************************
1208 Recursive file matching function act as find
1209 match must be always set to True when calling this function
1210 ****************************************************************************/
1211 static int file_find(struct file_list **list, const char *directory,
1212 const char *expression, BOOL match)
1214 DIR *dir;
1215 struct file_list *entry;
1216 struct stat statbuf;
1217 int ret;
1218 char *path;
1219 BOOL isdir;
1220 char *dname;
1222 dir = opendir(directory);
1223 if (!dir) return -1;
1225 while ((dname = readdirname(dir))) {
1226 if (!strcmp("..", dname)) continue;
1227 if (!strcmp(".", dname)) continue;
1229 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1230 continue;
1233 isdir = False;
1234 if (!match || !ms_fnmatch(expression, dname)) {
1235 if (recurse) {
1236 ret = stat(path, &statbuf);
1237 if (ret == 0) {
1238 if (S_ISDIR(statbuf.st_mode)) {
1239 isdir = True;
1240 ret = file_find(list, path, expression, False);
1242 } else {
1243 DEBUG(0,("file_find: cannot stat file %s\n", path));
1246 if (ret == -1) {
1247 SAFE_FREE(path);
1248 closedir(dir);
1249 return -1;
1252 entry = (struct file_list *) malloc(sizeof (struct file_list));
1253 if (!entry) {
1254 DEBUG(0,("Out of memory in file_find\n"));
1255 closedir(dir);
1256 return -1;
1258 entry->file_path = path;
1259 entry->isdir = isdir;
1260 DLIST_ADD(*list, entry);
1261 } else {
1262 SAFE_FREE(path);
1266 closedir(dir);
1267 return 0;
1270 /****************************************************************************
1271 mput some files
1272 ****************************************************************************/
1273 static void cmd_mput(void)
1275 fstring buf;
1276 char *p=buf;
1278 while (next_token(NULL,p,NULL,sizeof(buf))) {
1279 int ret;
1280 struct file_list *temp_list;
1281 char *quest, *lname, *rname;
1283 file_list = NULL;
1285 ret = file_find(&file_list, ".", p, True);
1286 if (ret) {
1287 free_file_list(file_list);
1288 continue;
1291 quest = NULL;
1292 lname = NULL;
1293 rname = NULL;
1295 for (temp_list = file_list; temp_list;
1296 temp_list = temp_list->next) {
1298 SAFE_FREE(lname);
1299 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1300 continue;
1301 trim_string(lname, "./", "/");
1303 /* check if it's a directory */
1304 if (temp_list->isdir) {
1305 /* if (!recurse) continue; */
1307 SAFE_FREE(quest);
1308 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1309 if (prompt && !yesno(quest)) { /* No */
1310 /* Skip the directory */
1311 lname[strlen(lname)-1] = '/';
1312 if (!seek_list(temp_list, lname))
1313 break;
1314 } else { /* Yes */
1315 SAFE_FREE(rname);
1316 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1317 dos_format(rname);
1318 if (!cli_chkpath(cli, rname) &&
1319 !do_mkdir(rname)) {
1320 DEBUG (0, ("Unable to make dir, skipping..."));
1321 /* Skip the directory */
1322 lname[strlen(lname)-1] = '/';
1323 if (!seek_list(temp_list, lname))
1324 break;
1327 continue;
1328 } else {
1329 SAFE_FREE(quest);
1330 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1331 if (prompt && !yesno(quest)) /* No */
1332 continue;
1334 /* Yes */
1335 SAFE_FREE(rname);
1336 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1339 dos_format(rname);
1341 do_put(rname, lname);
1343 free_file_list(file_list);
1344 SAFE_FREE(quest);
1345 SAFE_FREE(lname);
1346 SAFE_FREE(rname);
1351 /****************************************************************************
1352 cancel a print job
1353 ****************************************************************************/
1354 static void do_cancel(int job)
1356 if (cli_printjob_del(cli, job)) {
1357 printf("Job %d cancelled\n",job);
1358 } else {
1359 printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1364 /****************************************************************************
1365 cancel a print job
1366 ****************************************************************************/
1367 static void cmd_cancel(void)
1369 fstring buf;
1370 int job;
1372 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1373 printf("cancel <jobid> ...\n");
1374 return;
1376 do {
1377 job = atoi(buf);
1378 do_cancel(job);
1379 } while (next_token(NULL,buf,NULL,sizeof(buf)));
1383 /****************************************************************************
1384 print a file
1385 ****************************************************************************/
1386 static void cmd_print(void)
1388 pstring lname;
1389 pstring rname;
1390 char *p;
1392 if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1393 DEBUG(0,("print <filename>\n"));
1394 return;
1397 pstrcpy(rname,lname);
1398 p = strrchr(rname,'/');
1399 if (p) {
1400 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1403 if (strequal(lname,"-")) {
1404 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1407 do_put(rname, lname);
1411 /****************************************************************************
1412 show a print queue entry
1413 ****************************************************************************/
1414 static void queue_fn(struct print_job_info *p)
1416 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1419 /****************************************************************************
1420 show a print queue
1421 ****************************************************************************/
1422 static void cmd_queue(void)
1424 cli_print_queue(cli, queue_fn);
1427 /****************************************************************************
1428 delete some files
1429 ****************************************************************************/
1430 static void do_del(file_info *finfo)
1432 pstring mask;
1434 pstrcpy(mask,cur_dir);
1435 pstrcat(mask,finfo->name);
1437 if (finfo->mode & aDIR)
1438 return;
1440 if (!cli_unlink(cli, mask)) {
1441 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),mask));
1445 /****************************************************************************
1446 delete some files
1447 ****************************************************************************/
1448 static void cmd_del(void)
1450 pstring mask;
1451 fstring buf;
1452 uint16 attribute = aSYSTEM | aHIDDEN;
1454 if (recurse)
1455 attribute |= aDIR;
1457 pstrcpy(mask,cur_dir);
1459 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1460 DEBUG(0,("del <filename>\n"));
1461 return;
1463 pstrcat(mask,buf);
1465 do_list(mask, attribute,do_del,False,False);
1468 /****************************************************************************
1469 ****************************************************************************/
1470 static void cmd_open(void)
1472 pstring mask;
1473 fstring buf;
1475 pstrcpy(mask,cur_dir);
1477 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1478 DEBUG(0,("open <filename>\n"));
1479 return;
1481 pstrcat(mask,buf);
1483 cli_open(cli, mask, O_RDWR, DENY_ALL);
1487 /****************************************************************************
1488 remove a directory
1489 ****************************************************************************/
1490 static void cmd_rmdir(void)
1492 pstring mask;
1493 fstring buf;
1495 pstrcpy(mask,cur_dir);
1497 if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1498 DEBUG(0,("rmdir <dirname>\n"));
1499 return;
1501 pstrcat(mask,buf);
1503 if (!cli_rmdir(cli, mask)) {
1504 DEBUG(0,("%s removing remote directory file %s\n",
1505 cli_errstr(cli),mask));
1509 /****************************************************************************
1510 UNIX hardlink.
1511 ****************************************************************************/
1513 static void cmd_link(void)
1515 pstring src,dest;
1516 fstring buf,buf2;
1518 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1519 DEBUG(0,("Server doesn't support UNIX CIFS calls.\n"));
1520 return;
1523 pstrcpy(src,cur_dir);
1524 pstrcpy(dest,cur_dir);
1526 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1527 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1528 DEBUG(0,("link <src> <dest>\n"));
1529 return;
1532 pstrcat(src,buf);
1533 pstrcat(dest,buf2);
1535 if (!cli_unix_hardlink(cli, src, dest)) {
1536 DEBUG(0,("%s linking files (%s -> %s)\n",
1537 cli_errstr(cli), src, dest));
1538 return;
1542 /****************************************************************************
1543 UNIX symlink.
1544 ****************************************************************************/
1546 static void cmd_symlink(void)
1548 pstring src,dest;
1549 fstring buf,buf2;
1551 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1552 DEBUG(0,("Server doesn't support UNIX CIFS calls.\n"));
1553 return;
1556 pstrcpy(src,cur_dir);
1557 pstrcpy(dest,cur_dir);
1559 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1560 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1561 DEBUG(0,("symlink <src> <dest>\n"));
1562 return;
1565 pstrcat(src,buf);
1566 pstrcat(dest,buf2);
1568 if (!cli_unix_symlink(cli, src, dest)) {
1569 DEBUG(0,("%s symlinking files (%s -> %s)\n",
1570 cli_errstr(cli), src, dest));
1571 return;
1575 /****************************************************************************
1576 UNIX chmod.
1577 ****************************************************************************/
1579 static void cmd_chmod(void)
1581 pstring src;
1582 mode_t mode;
1583 fstring buf, buf2;
1585 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1586 DEBUG(0,("Server doesn't support UNIX CIFS calls.\n"));
1587 return;
1590 pstrcpy(src,cur_dir);
1592 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1593 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1594 DEBUG(0,("chmod mode file\n"));
1595 return;
1598 mode = (mode_t)strtol(buf, NULL, 8);
1599 pstrcat(src,buf2);
1601 if (!cli_unix_chmod(cli, src, mode)) {
1602 DEBUG(0,("%s chmod file %s 0%o\n",
1603 cli_errstr(cli), src, (unsigned int)mode));
1604 return;
1608 /****************************************************************************
1609 UNIX chown.
1610 ****************************************************************************/
1612 static void cmd_chown(void)
1614 pstring src;
1615 uid_t uid;
1616 gid_t gid;
1617 fstring buf, buf2, buf3;
1619 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1620 DEBUG(0,("Server doesn't support UNIX CIFS calls.\n"));
1621 return;
1624 pstrcpy(src,cur_dir);
1626 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1627 !next_token(NULL,buf2,NULL, sizeof(buf2)) ||
1628 !next_token(NULL,buf3,NULL, sizeof(buf3))) {
1629 DEBUG(0,("chown uid gid file\n"));
1630 return;
1633 uid = (uid_t)atoi(buf);
1634 gid = (gid_t)atoi(buf2);
1635 pstrcat(src,buf3);
1637 if (!cli_unix_chown(cli, src, uid, gid)) {
1638 DEBUG(0,("%s chown file %s uid=%d, gid=%d\n",
1639 cli_errstr(cli), src, (int)uid, (int)gid));
1640 return;
1644 /****************************************************************************
1645 rename some files
1646 ****************************************************************************/
1647 static void cmd_rename(void)
1649 pstring src,dest;
1650 fstring buf,buf2;
1652 pstrcpy(src,cur_dir);
1653 pstrcpy(dest,cur_dir);
1655 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1656 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1657 DEBUG(0,("rename <src> <dest>\n"));
1658 return;
1661 pstrcat(src,buf);
1662 pstrcat(dest,buf2);
1664 if (!cli_rename(cli, src, dest)) {
1665 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1666 return;
1671 /****************************************************************************
1672 toggle the prompt flag
1673 ****************************************************************************/
1674 static void cmd_prompt(void)
1676 prompt = !prompt;
1677 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1681 /****************************************************************************
1682 set the newer than time
1683 ****************************************************************************/
1684 static void cmd_newer(void)
1686 fstring buf;
1687 BOOL ok;
1688 SMB_STRUCT_STAT sbuf;
1690 ok = next_token(NULL,buf,NULL,sizeof(buf));
1691 if (ok && (sys_stat(buf,&sbuf) == 0)) {
1692 newer_than = sbuf.st_mtime;
1693 DEBUG(1,("Getting files newer than %s",
1694 asctime(LocalTime(&newer_than))));
1695 } else {
1696 newer_than = 0;
1699 if (ok && newer_than == 0)
1700 DEBUG(0,("Error setting newer-than time\n"));
1703 /****************************************************************************
1704 set the archive level
1705 ****************************************************************************/
1706 static void cmd_archive(void)
1708 fstring buf;
1710 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1711 archive_level = atoi(buf);
1712 } else
1713 DEBUG(0,("Archive level is %d\n",archive_level));
1716 /****************************************************************************
1717 toggle the lowercaseflag
1718 ****************************************************************************/
1719 static void cmd_lowercase(void)
1721 lowercase = !lowercase;
1722 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1728 /****************************************************************************
1729 toggle the recurse flag
1730 ****************************************************************************/
1731 static void cmd_recurse(void)
1733 recurse = !recurse;
1734 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1737 /****************************************************************************
1738 toggle the translate flag
1739 ****************************************************************************/
1740 static void cmd_translate(void)
1742 translation = !translation;
1743 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1744 translation?"on":"off"));
1748 /****************************************************************************
1749 do a printmode command
1750 ****************************************************************************/
1751 static void cmd_printmode(void)
1753 fstring buf;
1754 fstring mode;
1756 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1757 if (strequal(buf,"text")) {
1758 printmode = 0;
1759 } else {
1760 if (strequal(buf,"graphics"))
1761 printmode = 1;
1762 else
1763 printmode = atoi(buf);
1767 switch(printmode)
1769 case 0:
1770 fstrcpy(mode,"text");
1771 break;
1772 case 1:
1773 fstrcpy(mode,"graphics");
1774 break;
1775 default:
1776 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1777 break;
1780 DEBUG(2,("the printmode is now %s\n",mode));
1783 /****************************************************************************
1784 do the lcd command
1785 ****************************************************************************/
1786 static void cmd_lcd(void)
1788 fstring buf;
1789 pstring d;
1791 if (next_token(NULL,buf,NULL,sizeof(buf)))
1792 chdir(buf);
1793 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
1796 /****************************************************************************
1797 list a share name
1798 ****************************************************************************/
1799 static void browse_fn(const char *name, uint32 m,
1800 const char *comment, void *state)
1802 fstring typestr;
1804 *typestr=0;
1806 switch (m)
1808 case STYPE_DISKTREE:
1809 fstrcpy(typestr,"Disk"); break;
1810 case STYPE_PRINTQ:
1811 fstrcpy(typestr,"Printer"); break;
1812 case STYPE_DEVICE:
1813 fstrcpy(typestr,"Device"); break;
1814 case STYPE_IPC:
1815 fstrcpy(typestr,"IPC"); break;
1818 printf("\t%-15.15s%-10.10s%s\n",
1819 name, typestr,comment);
1823 /****************************************************************************
1824 try and browse available connections on a host
1825 ****************************************************************************/
1826 static BOOL browse_host(BOOL sort)
1828 int ret;
1830 printf("\n\tSharename Type Comment\n");
1831 printf("\t--------- ---- -------\n");
1833 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
1834 printf("Error returning browse list: %s\n", cli_errstr(cli));
1836 return (ret != -1);
1839 /****************************************************************************
1840 list a server name
1841 ****************************************************************************/
1842 static void server_fn(const char *name, uint32 m,
1843 const char *comment, void *state)
1845 printf("\t%-16.16s %s\n", name, comment);
1848 /****************************************************************************
1849 try and browse available connections on a host
1850 ****************************************************************************/
1851 static BOOL list_servers(char *wk_grp)
1853 if (!cli->server_domain) return False;
1855 printf("\n\tServer Comment\n");
1856 printf("\t--------- -------\n");
1858 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn, NULL);
1860 printf("\n\tWorkgroup Master\n");
1861 printf("\t--------- -------\n");
1863 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM, server_fn, NULL);
1864 return True;
1867 /* Some constants for completing filename arguments */
1869 #define COMPL_NONE 0 /* No completions */
1870 #define COMPL_REMOTE 1 /* Complete remote filename */
1871 #define COMPL_LOCAL 2 /* Complete local filename */
1873 /* This defines the commands supported by this client.
1874 * NOTE: The "!" must be the last one in the list because it's fn pointer
1875 * field is NULL, and NULL in that field is used in process_tok()
1876 * (below) to indicate the end of the list. crh
1878 struct
1880 const char *name;
1881 void (*fn)(void);
1882 const char *description;
1883 const char compl_args[2]; /* Completion argument info */
1884 } commands[] =
1886 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1887 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
1888 {"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}},
1889 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1890 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1891 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1892 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
1893 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
1894 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1895 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1896 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1897 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1898 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1899 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1900 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
1901 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1902 {"link",cmd_link,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
1903 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1904 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1905 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1906 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1907 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1908 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1909 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1910 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1911 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1912 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
1913 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1914 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1915 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1916 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1917 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1918 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1919 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1920 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1921 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1922 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1923 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1924 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1925 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1926 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1927 {"symlink",cmd_symlink,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
1928 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1929 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1930 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1932 /* Yes, this must be here, see crh's comment above. */
1933 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1934 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1938 /*******************************************************************
1939 lookup a command string in the list of commands, including
1940 abbreviations
1941 ******************************************************************/
1942 static int process_tok(fstring tok)
1944 int i = 0, matches = 0;
1945 int cmd=0;
1946 int tok_len = strlen(tok);
1948 while (commands[i].fn != NULL) {
1949 if (strequal(commands[i].name,tok)) {
1950 matches = 1;
1951 cmd = i;
1952 break;
1953 } else if (strnequal(commands[i].name, tok, tok_len)) {
1954 matches++;
1955 cmd = i;
1957 i++;
1960 if (matches == 0)
1961 return(-1);
1962 else if (matches == 1)
1963 return(cmd);
1964 else
1965 return(-2);
1968 /****************************************************************************
1969 help
1970 ****************************************************************************/
1971 static void cmd_help(void)
1973 int i=0,j;
1974 fstring buf;
1976 if (next_token(NULL,buf,NULL,sizeof(buf))) {
1977 if ((i = process_tok(buf)) >= 0)
1978 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1979 } else {
1980 while (commands[i].description) {
1981 for (j=0; commands[i].description && (j<5); j++) {
1982 DEBUG(0,("%-15s",commands[i].name));
1983 i++;
1985 DEBUG(0,("\n"));
1990 /****************************************************************************
1991 process a -c command string
1992 ****************************************************************************/
1993 static void process_command_string(char *cmd)
1995 pstring line;
1996 const char *ptr;
1998 /* establish the connection if not already */
2000 if (!cli) {
2001 cli = do_connect(desthost, service);
2002 if (!cli)
2003 return;
2006 while (cmd[0] != '\0') {
2007 char *p;
2008 fstring tok;
2009 int i;
2011 if ((p = strchr(cmd, ';')) == 0) {
2012 strncpy(line, cmd, 999);
2013 line[1000] = '\0';
2014 cmd += strlen(cmd);
2015 } else {
2016 if (p - cmd > 999) p = cmd + 999;
2017 strncpy(line, cmd, p - cmd);
2018 line[p - cmd] = '\0';
2019 cmd = p + 1;
2022 /* and get the first part of the command */
2023 ptr = line;
2024 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
2026 if ((i = process_tok(tok)) >= 0) {
2027 commands[i].fn();
2028 } else if (i == -2) {
2029 DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
2030 } else {
2031 DEBUG(0,("%s: command not found\n",tok));
2036 /****************************************************************************
2037 handle completion of commands for readline
2038 ****************************************************************************/
2039 static char **completion_fn(const char *text, int start, int end)
2041 #define MAX_COMPLETIONS 100
2042 char **matches;
2043 int i, count=0;
2045 /* for words not at the start of the line fallback to filename completion */
2046 if (start) return NULL;
2048 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
2049 if (!matches) return NULL;
2051 matches[count++] = strdup(text);
2052 if (!matches[0]) return NULL;
2054 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
2055 if (strncmp(text, commands[i].name, strlen(text)) == 0) {
2056 matches[count] = strdup(commands[i].name);
2057 if (!matches[count]) return NULL;
2058 count++;
2062 if (count == 2) {
2063 SAFE_FREE(matches[0]);
2064 matches[0] = strdup(matches[1]);
2066 matches[count] = NULL;
2067 return matches;
2071 /****************************************************************************
2072 make sure we swallow keepalives during idle time
2073 ****************************************************************************/
2074 static void readline_callback(void)
2076 fd_set fds;
2077 struct timeval timeout;
2078 static time_t last_t;
2079 time_t t;
2081 t = time(NULL);
2083 if (t - last_t < 5) return;
2085 last_t = t;
2087 again:
2088 FD_ZERO(&fds);
2089 FD_SET(cli->fd,&fds);
2091 timeout.tv_sec = 0;
2092 timeout.tv_usec = 0;
2093 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
2095 /* We deliberately use receive_smb instead of
2096 client_receive_smb as we want to receive
2097 session keepalives and then drop them here.
2099 if (FD_ISSET(cli->fd,&fds)) {
2100 receive_smb(cli->fd,cli->inbuf,0);
2101 goto again;
2104 cli_chkpath(cli, "\\");
2108 /****************************************************************************
2109 process commands on stdin
2110 ****************************************************************************/
2111 static void process_stdin(void)
2113 const char *ptr;
2115 while (1) {
2116 fstring tok;
2117 fstring the_prompt;
2118 char *line;
2119 int i;
2121 /* display a prompt */
2122 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
2123 line = smb_readline(the_prompt, readline_callback, completion_fn);
2125 if (!line) break;
2127 /* special case - first char is ! */
2128 if (*line == '!') {
2129 system(line + 1);
2130 continue;
2133 /* and get the first part of the command */
2134 ptr = line;
2135 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
2137 if ((i = process_tok(tok)) >= 0) {
2138 commands[i].fn();
2139 } else if (i == -2) {
2140 DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
2141 } else {
2142 DEBUG(0,("%s: command not found\n",tok));
2148 /*****************************************************
2149 return a connection to a server
2150 *******************************************************/
2151 struct cli_state *do_connect(const char *server, const char *share)
2153 struct cli_state *c;
2154 struct nmb_name called, calling;
2155 const char *server_n;
2156 struct in_addr ip;
2157 fstring servicename;
2158 char *sharename;
2160 /* make a copy so we don't modify the global string 'service' */
2161 safe_strcpy(servicename, share, sizeof(servicename)-1);
2162 sharename = servicename;
2163 if (*sharename == '\\') {
2164 server = sharename+2;
2165 sharename = strchr(server,'\\');
2166 if (!sharename) return NULL;
2167 *sharename = 0;
2168 sharename++;
2171 server_n = server;
2173 zero_ip(&ip);
2175 make_nmb_name(&calling, global_myname, 0x0);
2176 make_nmb_name(&called , server, name_type);
2178 again:
2179 zero_ip(&ip);
2180 if (have_ip) ip = dest_ip;
2182 /* have to open a new connection */
2183 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) == 0) ||
2184 !cli_connect(c, server_n, &ip)) {
2185 DEBUG(0,("Connection to %s failed\n", server_n));
2186 return NULL;
2189 c->protocol = max_protocol;
2190 if (timeout_msec)
2191 c->timeout = timeout_msec;
2193 if (!cli_session_request(c, &calling, &called)) {
2194 char *p;
2195 DEBUG(0,("session request to %s failed (%s)\n",
2196 called.name, cli_errstr(c)));
2197 cli_shutdown(c);
2198 if ((p=strchr(called.name, '.'))) {
2199 *p = 0;
2200 goto again;
2202 if (strcmp(called.name, "*SMBSERVER")) {
2203 make_nmb_name(&called , "*SMBSERVER", 0x20);
2204 goto again;
2206 return NULL;
2209 DEBUG(4,(" session request ok\n"));
2211 if (!cli_negprot(c)) {
2212 DEBUG(0,("protocol negotiation failed\n"));
2213 cli_shutdown(c);
2214 return NULL;
2217 if (!got_pass) {
2218 char *pass = getpass("Password: ");
2219 if (pass) {
2220 pstrcpy(password, pass);
2224 if (!cli_session_setup(c, username,
2225 password, strlen(password),
2226 password, strlen(password),
2227 workgroup)) {
2228 /* if a password was not supplied then try again with a null username */
2229 if (password[0] || !username[0] ||
2230 !cli_session_setup(c, "", "", 0, "", 0, workgroup)) {
2231 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
2232 cli_shutdown(c);
2233 return NULL;
2235 DEBUG(0,("Anonymous login successful\n"));
2239 * These next two lines are needed to emulate
2240 * old client behaviour for people who have
2241 * scripts based on client output.
2242 * QUESTION ? Do we want to have a 'client compatibility
2243 * mode to turn these on/off ? JRA.
2246 if (*c->server_domain || *c->server_os || *c->server_type)
2247 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
2248 c->server_domain,c->server_os,c->server_type));
2250 DEBUG(4,(" session setup ok\n"));
2252 if (!cli_send_tconX(c, sharename, "?????",
2253 password, strlen(password)+1)) {
2254 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
2255 cli_shutdown(c);
2256 return NULL;
2259 DEBUG(4,(" tconx ok\n"));
2261 return c;
2265 /****************************************************************************
2266 process commands from the client
2267 ****************************************************************************/
2268 static BOOL process(char *base_directory)
2270 cli = do_connect(desthost, service);
2271 if (!cli) {
2272 return(False);
2275 if (*base_directory) do_cd(base_directory);
2277 if (cmdstr) {
2278 process_command_string(cmdstr);
2279 } else {
2280 process_stdin();
2283 cli_shutdown(cli);
2284 return(True);
2287 /****************************************************************************
2288 usage on the program
2289 ****************************************************************************/
2290 static void usage(char *pname)
2292 DEBUG(0,("Usage: %s service <password> [options]", pname));
2294 DEBUG(0,("\nVersion %s\n",VERSION));
2295 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
2296 DEBUG(0,("\t-O socket_options socket options to use\n"));
2297 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
2298 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
2299 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
2300 DEBUG(0,("\t-N don't ask for a password\n"));
2301 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
2302 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
2303 DEBUG(0,("\t-P connect to service as a printer\n"));
2304 DEBUG(0,("\t-p port connect to the specified port\n"));
2305 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
2306 DEBUG(0,("\t-h Print this help message.\n"));
2307 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
2308 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
2309 DEBUG(0,("\t-U username set the network username\n"));
2310 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
2311 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
2312 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
2313 DEBUG(0,("\t-A filename get the credentials from a file\n"));
2314 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
2315 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
2316 DEBUG(0,("\t-D directory start from directory\n"));
2317 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
2318 DEBUG(0,("\t-b xmit/send buffer changes the transmit/send buffer (default: 65520)\n"));
2319 DEBUG(0,("\n"));
2323 /****************************************************************************
2324 get a password from a a file or file descriptor
2325 exit on failure
2326 ****************************************************************************/
2327 static void get_password_file(void)
2329 int fd = -1;
2330 char *p;
2331 BOOL close_it = False;
2332 pstring spec;
2333 char pass[128];
2335 if ((p = getenv("PASSWD_FD")) != NULL) {
2336 pstrcpy(spec, "descriptor ");
2337 pstrcat(spec, p);
2338 sscanf(p, "%d", &fd);
2339 close_it = False;
2340 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
2341 fd = sys_open(p, O_RDONLY, 0);
2342 pstrcpy(spec, p);
2343 if (fd < 0) {
2344 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
2345 spec, strerror(errno));
2346 exit(1);
2348 close_it = True;
2351 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
2352 p && p - pass < sizeof(pass);) {
2353 switch (read(fd, p, 1)) {
2354 case 1:
2355 if (*p != '\n' && *p != '\0') {
2356 *++p = '\0'; /* advance p, and null-terminate pass */
2357 break;
2359 case 0:
2360 if (p - pass) {
2361 *p = '\0'; /* null-terminate it, just in case... */
2362 p = NULL; /* then force the loop condition to become false */
2363 break;
2364 } else {
2365 fprintf(stderr, "Error reading password from file %s: %s\n",
2366 spec, "empty password\n");
2367 exit(1);
2370 default:
2371 fprintf(stderr, "Error reading password from file %s: %s\n",
2372 spec, strerror(errno));
2373 exit(1);
2376 pstrcpy(password, pass);
2377 if (close_it)
2378 close(fd);
2383 /****************************************************************************
2384 handle a -L query
2385 ****************************************************************************/
2386 static int do_host_query(char *query_host)
2388 cli = do_connect(query_host, "IPC$");
2389 if (!cli)
2390 return 1;
2392 browse_host(True);
2393 list_servers(workgroup);
2395 cli_shutdown(cli);
2397 return(0);
2401 /****************************************************************************
2402 handle a tar operation
2403 ****************************************************************************/
2404 static int do_tar_op(char *base_directory)
2406 int ret;
2408 /* do we already have a connection? */
2409 if (!cli) {
2410 cli = do_connect(desthost, service);
2411 if (!cli)
2412 return 1;
2415 recurse=True;
2417 if (*base_directory) do_cd(base_directory);
2419 ret=process_tar();
2421 cli_shutdown(cli);
2423 return(ret);
2426 /****************************************************************************
2427 Handle a message operation.
2428 ****************************************************************************/
2430 static int do_message_op(void)
2432 struct in_addr ip;
2433 struct nmb_name called, calling;
2435 zero_ip(&ip);
2437 make_nmb_name(&calling, global_myname, 0x0);
2438 make_nmb_name(&called , desthost, name_type);
2440 zero_ip(&ip);
2441 if (have_ip)
2442 ip = dest_ip;
2443 else if (name_type != 0x20) {
2444 /* We must do our own resolve name here as the nametype is #0x3, not #0x20. */
2445 if (!resolve_name(desthost, &ip, name_type)) {
2446 DEBUG(0,("Cannot resolve name %s#0x%x\n", desthost, name_type));
2447 return 1;
2451 if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, port) == 0) || !cli_connect(cli, desthost, &ip)) {
2452 DEBUG(0,("Connection to %s failed\n", desthost));
2453 return 1;
2456 if (!cli_session_request(cli, &calling, &called)) {
2457 DEBUG(0,("session request failed\n"));
2458 cli_shutdown(cli);
2459 return 1;
2462 send_message();
2463 cli_shutdown(cli);
2465 return 0;
2469 /****************************************************************************
2470 main program
2471 ****************************************************************************/
2472 int main(int argc,char *argv[])
2474 fstring base_directory;
2475 char *pname = argv[0];
2476 int opt;
2477 extern FILE *dbf;
2478 extern char *optarg;
2479 extern int optind;
2480 pstring query_host;
2481 BOOL message = False;
2482 extern char tar_type;
2483 static pstring servicesf = CONFIGFILE;
2484 pstring term_code;
2485 pstring new_name_resolve_order;
2486 pstring logfile;
2487 char *p;
2489 #ifdef KANJI
2490 pstrcpy(term_code, KANJI);
2491 #else /* KANJI */
2492 *term_code = 0;
2493 #endif /* KANJI */
2495 *query_host = 0;
2496 *base_directory = 0;
2498 *new_name_resolve_order = 0;
2500 DEBUGLEVEL = 2;
2501 AllowDebugChange = False;
2503 setup_logging(pname,True);
2506 * If the -E option is given, be careful not to clobber stdout
2507 * before processing the options. 28.Feb.99, richard@hacom.nl.
2508 * Also pre-parse the -s option to get the service file name.
2511 for (opt = 1; opt < argc; opt++) {
2512 if (strcmp(argv[opt], "-E") == 0)
2513 dbf = stderr;
2514 else if(strncmp(argv[opt], "-s", 2) == 0) {
2515 if(argv[opt][2] != '\0')
2516 pstrcpy(servicesf, &argv[opt][2]);
2517 else if(argv[opt+1] != NULL) {
2519 * At least one more arg left.
2521 pstrcpy(servicesf, argv[opt+1]);
2522 } else {
2523 usage(pname);
2524 exit(1);
2527 else if(strncmp(argv[opt], "-d", 2) == 0) {
2528 if(argv[opt][2] != '\0') {
2529 if (argv[opt][2] == 'A')
2530 DEBUGLEVEL = 10000;
2531 else
2532 DEBUGLEVEL = atoi(&argv[opt][2]);
2533 } else if(argv[opt+1] != NULL) {
2535 * At least one more arg left.
2537 if (argv[opt+1][0] == 'A')
2538 DEBUGLEVEL = 10000;
2539 else
2540 DEBUGLEVEL = atoi(argv[opt+1]);
2541 } else {
2542 usage(pname);
2543 exit(1);
2548 TimeInit();
2549 charset_initialise();
2551 in_client = True; /* Make sure that we tell lp_load we are */
2553 if (!lp_load(servicesf,True,False,False)) {
2554 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
2557 codepage_initialise(lp_client_code_page());
2559 #ifdef WITH_SSL
2560 sslutil_init(0);
2561 #endif
2563 pstrcpy(workgroup,lp_workgroup());
2565 load_interfaces();
2567 if (getenv("USER")) {
2568 pstrcpy(username,getenv("USER"));
2570 /* modification to support userid%passwd syntax in the USER var
2571 25.Aug.97, jdblair@uab.edu */
2573 if ((p=strchr(username,'%'))) {
2574 *p = 0;
2575 pstrcpy(password,p+1);
2576 got_pass = True;
2577 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
2579 strupper(username);
2582 /* modification to support PASSWD environmental var
2583 25.Aug.97, jdblair@uab.edu */
2584 if (getenv("PASSWD")) {
2585 pstrcpy(password,getenv("PASSWD"));
2586 got_pass = True;
2589 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2590 get_password_file();
2591 got_pass = True;
2594 if (*username == 0 && getenv("LOGNAME")) {
2595 pstrcpy(username,getenv("LOGNAME"));
2596 strupper(username);
2599 if (*username == 0) {
2600 pstrcpy(username,"GUEST");
2603 if (argc < 2) {
2604 usage(pname);
2605 exit(1);
2608 if (*argv[1] != '-') {
2609 pstrcpy(service,argv[1]);
2610 /* Convert any '/' characters in the service name to '\' characters */
2611 string_replace( service, '/','\\');
2612 argc--;
2613 argv++;
2615 if (count_chars(service,'\\') < 3) {
2616 usage(pname);
2617 printf("\n%s: Not enough '\\' characters in service\n",service);
2618 exit(1);
2621 if (argc > 1 && (*argv[1] != '-')) {
2622 got_pass = True;
2623 pstrcpy(password,argv[1]);
2624 memset(argv[1],'X',strlen(argv[1]));
2625 argc--;
2626 argv++;
2630 while ((opt =
2631 getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:z:")) != EOF) {
2632 switch (opt) {
2633 case 'z':
2634 timeout_msec = atoi(optarg);
2635 break;
2636 case 's':
2637 pstrcpy(servicesf, optarg);
2638 break;
2639 case 'O':
2640 pstrcpy(user_socket_options,optarg);
2641 break;
2642 case 'R':
2643 pstrcpy(new_name_resolve_order, optarg);
2644 break;
2645 case 'M':
2646 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2647 pstrcpy(desthost,optarg);
2648 message = True;
2649 break;
2650 case 'i':
2652 extern pstring global_scope;
2653 pstrcpy(global_scope,optarg);
2654 strupper(global_scope);
2656 break;
2657 case 'N':
2658 got_pass = True;
2659 break;
2660 case 'n':
2661 pstrcpy(global_myname,optarg);
2662 break;
2663 case 'd':
2664 if (*optarg == 'A')
2665 DEBUGLEVEL = 10000;
2666 else
2667 DEBUGLEVEL = atoi(optarg);
2668 break;
2669 case 'P':
2670 /* not needed anymore */
2671 break;
2672 case 'p':
2673 port = atoi(optarg);
2674 break;
2675 case 'l':
2676 slprintf(logfile,sizeof(logfile)-1, "%s.client",optarg);
2677 lp_set_logfile(logfile);
2678 break;
2679 case 'h':
2680 usage(pname);
2681 exit(0);
2682 break;
2683 case 'I':
2685 dest_ip = *interpret_addr2(optarg);
2686 if (is_zero_ip(dest_ip))
2687 exit(1);
2688 have_ip = True;
2690 break;
2691 case 'E':
2692 dbf = stderr;
2693 break;
2694 case 'U':
2696 char *lp;
2697 pstrcpy(username,optarg);
2698 if ((lp=strchr(username,'%'))) {
2699 *lp = 0;
2700 pstrcpy(password,lp+1);
2701 got_pass = True;
2702 memset(strchr(optarg,'%')+1,'X',strlen(password));
2705 break;
2707 case 'A':
2709 FILE *auth;
2710 fstring buf;
2711 uint16 len = 0;
2712 char *ptr, *val, *param;
2714 if ((auth=sys_fopen(optarg, "r")) == NULL)
2716 /* fail if we can't open the credentials file */
2717 DEBUG(0,("ERROR: Unable to open credentials file!\n"));
2718 exit (-1);
2721 while (!feof(auth))
2723 /* get a line from the file */
2724 if (!fgets (buf, sizeof(buf), auth))
2725 continue;
2726 len = strlen(buf);
2728 if ((len) && (buf[len-1]=='\n'))
2730 buf[len-1] = '\0';
2731 len--;
2733 if (len == 0)
2734 continue;
2736 /* break up the line into parameter & value.
2737 will need to eat a little whitespace possibly */
2738 param = buf;
2739 if (!(ptr = strchr (buf, '=')))
2740 continue;
2741 val = ptr+1;
2742 *ptr = '\0';
2744 /* eat leading white space */
2745 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
2746 val++;
2748 if (strwicmp("password", param) == 0)
2750 pstrcpy(password, val);
2751 got_pass = True;
2753 else if (strwicmp("username", param) == 0)
2754 pstrcpy(username, val);
2755 else if (strwicmp("domain", param) == 0)
2756 pstrcpy(workgroup,val);
2757 memset(buf, 0, sizeof(buf));
2759 fclose(auth);
2761 break;
2763 case 'L':
2764 p = optarg;
2765 while(*p == '\\' || *p == '/')
2766 p++;
2767 pstrcpy(query_host,p);
2768 break;
2769 case 't':
2770 pstrcpy(term_code, optarg);
2771 break;
2772 case 'm':
2773 max_protocol = interpret_protocol(optarg, max_protocol);
2774 break;
2775 case 'W':
2776 pstrcpy(workgroup,optarg);
2777 break;
2778 case 'T':
2779 if (!tar_parseargs(argc, argv, optarg, optind)) {
2780 usage(pname);
2781 exit(1);
2783 break;
2784 case 'D':
2785 pstrcpy(base_directory,optarg);
2786 break;
2787 case 'c':
2788 cmdstr = optarg;
2789 break;
2790 case 'b':
2791 io_bufsize = MAX(1, atoi(optarg));
2792 break;
2793 default:
2794 usage(pname);
2795 exit(1);
2799 get_myname((*global_myname)?NULL:global_myname);
2801 if(*new_name_resolve_order)
2802 lp_set_name_resolve_order(new_name_resolve_order);
2804 if (*term_code)
2805 interpret_coding_system(term_code);
2807 if (!tar_type && !*query_host && !*service && !message) {
2808 usage(pname);
2809 exit(1);
2812 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2814 if (tar_type) {
2815 if (cmdstr)
2816 process_command_string(cmdstr);
2817 return do_tar_op(base_directory);
2820 if ((p=strchr(query_host,'#'))) {
2821 *p = 0;
2822 p++;
2823 sscanf(p, "%x", &name_type);
2826 if (*query_host) {
2827 return do_host_query(query_host);
2830 if (message) {
2831 return do_message_op();
2834 if (!process(base_directory)) {
2835 return(1);
2838 return(0);