2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 struct cli_state *cli;
31 extern BOOL in_client;
33 pstring cur_dir = "\\";
35 static pstring service;
36 static pstring desthost;
37 extern pstring global_myname;
38 static pstring password;
39 static pstring username;
40 static pstring workgroup;
43 static int io_bufsize = 64512;
44 extern struct in_addr ipzero;
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 /* value for unused fid field in trans2 secondary request */
58 #define FID_UNUSED (0xFFFF)
60 time_t newer_than = 0;
61 int archive_level = 0;
63 extern pstring debugf;
64 extern int DEBUGLEVEL;
66 BOOL translation = False;
70 /* clitar bits insert */
73 extern BOOL tar_reset;
77 mode_t myumask = 0755;
83 static BOOL recurse = False;
84 BOOL lowercase = False;
86 struct in_addr dest_ip;
88 #define SEPARATORS " \t\n\r"
90 BOOL abort_mget = True;
92 pstring fileselection = "";
94 extern file_info def_finfo;
97 int get_total_size = 0;
98 int get_total_time_ms = 0;
99 int put_total_size = 0;
100 int put_total_time_ms = 0;
103 static double dir_total;
107 /****************************************************************************
108 write to a local file with CR/LF->LF translation if appropriate. return the
109 number taken from the buffer. This may not equal the number written.
110 ****************************************************************************/
111 static int writefile(int f, char *b, int n)
121 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
124 if (write(f, b, 1) != 1) {
134 /****************************************************************************
135 read from a file with LF->CR/LF translation if appropriate. return the
136 number read. read approx n bytes.
137 ****************************************************************************/
138 static int readfile(char *b, int n, XFILE *f)
144 return x_fread(b,1,n,f);
147 while (i < (n - 1) && (i < BUFFER_SIZE)) {
148 if ((c = x_getc(f)) == EOF) {
152 if (c == '\n') { /* change all LFs to CR/LF */
163 /****************************************************************************
165 ****************************************************************************/
166 static void send_message(void)
171 if (!cli_message_start(cli, desthost, username, &grp_id)) {
172 d_printf("message start: %s\n", cli_errstr(cli));
177 d_printf("Connected. Type your message, ending it with a Control-D\n");
179 while (!feof(stdin) && total_len < 1600) {
180 int maxlen = MIN(1600 - total_len,127);
187 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
193 if (!cli_message_text(cli, msg, l, grp_id)) {
194 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
201 if (total_len >= 1600)
202 d_printf("the message was truncated to 1600 bytes\n");
204 d_printf("sent %d bytes\n",total_len);
206 if (!cli_message_end(cli, grp_id)) {
207 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
214 /****************************************************************************
215 check the space on a device
216 ****************************************************************************/
217 static void do_dskattr(void)
219 int total, bsize, avail;
221 if (!cli_dskattr(cli, &bsize, &total, &avail)) {
222 d_printf("Error in dskattr: %s\n",cli_errstr(cli));
226 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
227 total, bsize, avail);
230 /****************************************************************************
232 ****************************************************************************/
233 static void cmd_pwd(void)
235 d_printf("Current directory is %s",service);
236 d_printf("%s\n",cur_dir);
240 /****************************************************************************
241 change directory - inner section
242 ****************************************************************************/
243 static void do_cd(char *newdir)
251 /* Save the current directory in case the
252 new directory is invalid */
253 pstrcpy(saved_dir, cur_dir);
258 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
259 pstrcat(cur_dir, "\\");
261 dos_clean_name(cur_dir);
262 pstrcpy(dname,cur_dir);
263 pstrcat(cur_dir,"\\");
264 dos_clean_name(cur_dir);
266 if (!strequal(cur_dir,"\\")) {
267 if (!cli_chkpath(cli, dname)) {
268 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
269 pstrcpy(cur_dir,saved_dir);
273 pstrcpy(cd_path,cur_dir);
276 /****************************************************************************
278 ****************************************************************************/
279 static void cmd_cd(void)
283 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
286 d_printf("Current directory is %s\n",cur_dir);
290 /*******************************************************************
291 decide if a file should be operated on
292 ********************************************************************/
293 static BOOL do_this_one(file_info *finfo)
295 if (finfo->mode & aDIR) return(True);
297 if (*fileselection &&
298 !mask_match(finfo->name,fileselection,False)) {
299 DEBUG(3,("match_match %s failed\n", finfo->name));
303 if (newer_than && finfo->mtime < newer_than) {
304 DEBUG(3,("newer_than %s failed\n", finfo->name));
308 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
309 DEBUG(3,("archive %s failed\n", finfo->name));
316 /****************************************************************************
317 display info about a file
318 ****************************************************************************/
319 static void display_finfo(file_info *finfo)
321 if (do_this_one(finfo)) {
322 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
323 d_printf(" %-30s%7.7s %8.0f %s",
325 attrib_string(finfo->mode),
327 asctime(LocalTime(&t)));
328 dir_total += finfo->size;
333 /****************************************************************************
334 accumulate size of a file
335 ****************************************************************************/
336 static void do_du(file_info *finfo)
338 if (do_this_one(finfo)) {
339 dir_total += finfo->size;
343 static BOOL do_list_recurse;
344 static BOOL do_list_dirs;
345 static char *do_list_queue = 0;
346 static long do_list_queue_size = 0;
347 static long do_list_queue_start = 0;
348 static long do_list_queue_end = 0;
349 static void (*do_list_fn)(file_info *);
351 /****************************************************************************
352 functions for do_list_queue
353 ****************************************************************************/
356 * The do_list_queue is a NUL-separated list of strings stored in a
357 * char*. Since this is a FIFO, we keep track of the beginning and
358 * ending locations of the data in the queue. When we overflow, we
359 * double the size of the char*. When the start of the data passes
360 * the midpoint, we move everything back. This is logically more
361 * complex than a linked list, but easier from a memory management
362 * angle. In any memory error condition, do_list_queue is reset.
363 * Functions check to ensure that do_list_queue is non-NULL before
366 static void reset_do_list_queue(void)
368 SAFE_FREE(do_list_queue);
369 do_list_queue_size = 0;
370 do_list_queue_start = 0;
371 do_list_queue_end = 0;
374 static void init_do_list_queue(void)
376 reset_do_list_queue();
377 do_list_queue_size = 1024;
378 do_list_queue = malloc(do_list_queue_size);
379 if (do_list_queue == 0) {
380 d_printf("malloc fail for size %d\n",
381 (int)do_list_queue_size);
382 reset_do_list_queue();
384 memset(do_list_queue, 0, do_list_queue_size);
388 static void adjust_do_list_queue(void)
391 * If the starting point of the queue is more than half way through,
392 * move everything toward the beginning.
394 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
396 DEBUG(4,("do_list_queue is empty\n"));
397 do_list_queue_start = do_list_queue_end = 0;
398 *do_list_queue = '\0';
400 else if (do_list_queue_start > (do_list_queue_size / 2))
402 DEBUG(4,("sliding do_list_queue backward\n"));
403 memmove(do_list_queue,
404 do_list_queue + do_list_queue_start,
405 do_list_queue_end - do_list_queue_start);
406 do_list_queue_end -= do_list_queue_start;
407 do_list_queue_start = 0;
412 static void add_to_do_list_queue(const char* entry)
415 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
416 while (new_end > do_list_queue_size)
418 do_list_queue_size *= 2;
419 DEBUG(4,("enlarging do_list_queue to %d\n",
420 (int)do_list_queue_size));
421 dlq = Realloc(do_list_queue, do_list_queue_size);
423 d_printf("failure enlarging do_list_queue to %d bytes\n",
424 (int)do_list_queue_size);
425 reset_do_list_queue();
430 memset(do_list_queue + do_list_queue_size / 2,
431 0, do_list_queue_size / 2);
436 pstrcpy(do_list_queue + do_list_queue_end, entry);
437 do_list_queue_end = new_end;
438 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
439 entry, (int)do_list_queue_start, (int)do_list_queue_end));
443 static char *do_list_queue_head(void)
445 return do_list_queue + do_list_queue_start;
448 static void remove_do_list_queue_head(void)
450 if (do_list_queue_end > do_list_queue_start)
452 do_list_queue_start += strlen(do_list_queue_head()) + 1;
453 adjust_do_list_queue();
454 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
455 (int)do_list_queue_start, (int)do_list_queue_end));
459 static int do_list_queue_empty(void)
461 return (! (do_list_queue && *do_list_queue));
464 /****************************************************************************
466 ****************************************************************************/
467 static void do_list_helper(file_info *f, const char *mask, void *state)
469 if (f->mode & aDIR) {
470 if (do_list_dirs && do_this_one(f)) {
473 if (do_list_recurse &&
474 !strequal(f->name,".") &&
475 !strequal(f->name,"..")) {
479 pstrcpy(mask2, mask);
480 p = strrchr_m(mask2,'\\');
483 pstrcat(mask2, f->name);
484 pstrcat(mask2,"\\*");
485 add_to_do_list_queue(mask2);
490 if (do_this_one(f)) {
496 /****************************************************************************
497 a wrapper around cli_list that adds recursion
498 ****************************************************************************/
499 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
501 static int in_do_list = 0;
503 if (in_do_list && rec)
505 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
511 do_list_recurse = rec;
517 init_do_list_queue();
518 add_to_do_list_queue(mask);
520 while (! do_list_queue_empty())
523 * Need to copy head so that it doesn't become
524 * invalid inside the call to cli_list. This
525 * would happen if the list were expanded
527 * Fix from E. Jay Berkenbilt (ejb@ql.org)
530 pstrcpy(head, do_list_queue_head());
531 cli_list(cli, head, attribute, do_list_helper, NULL);
532 remove_do_list_queue_head();
533 if ((! do_list_queue_empty()) && (fn == display_finfo))
535 char* next_file = do_list_queue_head();
537 if ((strlen(next_file) >= 2) &&
538 (next_file[strlen(next_file) - 1] == '*') &&
539 (next_file[strlen(next_file) - 2] == '\\'))
541 save_ch = next_file +
542 strlen(next_file) - 2;
545 d_printf("\n%s\n",next_file);
555 if (cli_list(cli, mask, attribute, do_list_helper, NULL) == -1)
557 d_printf("%s listing %s\n", cli_errstr(cli), mask);
562 reset_do_list_queue();
565 /****************************************************************************
566 get a directory listing
567 ****************************************************************************/
568 static void cmd_dir(void)
570 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
576 pstrcpy(mask,cur_dir);
577 if(mask[strlen(mask)-1]!='\\')
580 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
591 do_list(mask, attribute, display_finfo, recurse, True);
595 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
599 /****************************************************************************
600 get a directory listing
601 ****************************************************************************/
602 static void cmd_du(void)
604 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
610 pstrcpy(mask,cur_dir);
611 if(mask[strlen(mask)-1]!='\\')
614 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
624 do_list(mask, attribute, do_du, recurse, True);
628 d_printf("Total number of bytes: %.0f\n", dir_total);
632 /****************************************************************************
633 get a file from rname to lname
634 ****************************************************************************/
635 static void do_get(char *rname,char *lname)
638 BOOL newhandle = False;
640 struct timeval tp_start;
641 int read_size = io_bufsize;
646 GetTimeOfDay(&tp_start);
652 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
655 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
659 if(!strcmp(lname,"-")) {
660 handle = fileno(stdout);
662 handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
666 d_printf("Error opening local file %s\n",lname);
671 if (!cli_qfileinfo(cli, fnum,
672 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
673 !cli_getattrE(cli, fnum,
674 &attr, &size, NULL, NULL, NULL)) {
675 d_printf("getattrib: %s\n",cli_errstr(cli));
679 DEBUG(2,("getting file %s of size %.0f as %s ",
680 lname, (double)size, lname));
682 if(!(data = (char *)malloc(read_size))) {
683 d_printf("malloc fail for size %d\n", read_size);
684 cli_close(cli, fnum);
689 int n = cli_read(cli, fnum, data, nread, read_size);
693 if (writefile(handle,data, n) != n) {
694 d_printf("Error writing local file\n");
702 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
703 rname, (long)nread));
708 if (!cli_close(cli, fnum)) {
709 d_printf("Error %s closing remote file\n",cli_errstr(cli));
716 if (archive_level >= 2 && (attr & aARCH)) {
717 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
721 struct timeval tp_end;
724 GetTimeOfDay(&tp_end);
726 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
727 (tp_end.tv_usec - tp_start.tv_usec)/1000;
728 get_total_time_ms += this_time;
729 get_total_size += nread;
731 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
732 nread / (1.024*this_time + 1.0e-4),
733 get_total_size / (1.024*get_total_time_ms)));
738 /****************************************************************************
740 ****************************************************************************/
741 static void cmd_get(void)
747 pstrcpy(rname,cur_dir);
750 p = rname + strlen(rname);
752 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
753 d_printf("get <filename>\n");
757 dos_clean_name(rname);
759 next_token_nr(NULL,lname,NULL,sizeof(lname));
761 do_get(rname, lname);
765 /****************************************************************************
766 do a mget operation on one file
767 ****************************************************************************/
768 static void do_mget(file_info *finfo)
772 pstring saved_curdir;
775 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
779 d_printf("mget aborted\n");
783 if (finfo->mode & aDIR)
784 slprintf(quest,sizeof(pstring)-1,
785 "Get directory %s? ",finfo->name);
787 slprintf(quest,sizeof(pstring)-1,
788 "Get file %s? ",finfo->name);
790 if (prompt && !yesno(quest)) return;
792 if (!(finfo->mode & aDIR)) {
793 pstrcpy(rname,cur_dir);
794 pstrcat(rname,finfo->name);
795 do_get(rname,finfo->name);
799 /* handle directories */
800 pstrcpy(saved_curdir,cur_dir);
802 pstrcat(cur_dir,finfo->name);
803 pstrcat(cur_dir,"\\");
805 unix_format(finfo->name);
807 strlower(finfo->name);
809 if (!directory_exist(finfo->name,NULL) &&
810 mkdir(finfo->name,0777) != 0) {
811 d_printf("failed to create directory %s\n",finfo->name);
812 pstrcpy(cur_dir,saved_curdir);
816 if (chdir(finfo->name) != 0) {
817 d_printf("failed to chdir to directory %s\n",finfo->name);
818 pstrcpy(cur_dir,saved_curdir);
822 pstrcpy(mget_mask,cur_dir);
823 pstrcat(mget_mask,"*");
825 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
827 pstrcpy(cur_dir,saved_curdir);
831 /****************************************************************************
832 view the file using the pager
833 ****************************************************************************/
834 static void cmd_more(void)
836 fstring rname,lname,pager_cmd;
840 fstrcpy(rname,cur_dir);
843 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
844 fd = smb_mkstemp(lname);
846 d_printf("failed to create temporary file for more\n");
851 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
852 d_printf("more <filename>\n");
856 dos_clean_name(rname);
860 pager=getenv("PAGER");
862 slprintf(pager_cmd,sizeof(pager_cmd)-1,
863 "%s %s",(pager? pager:PAGER), lname);
870 /****************************************************************************
872 ****************************************************************************/
873 static void cmd_mget(void)
875 uint16 attribute = aSYSTEM | aHIDDEN;
887 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
888 pstrcpy(mget_mask,cur_dir);
889 if(mget_mask[strlen(mget_mask)-1]!='\\')
890 pstrcat(mget_mask,"\\");
893 pstrcpy(mget_mask,p);
895 pstrcat(mget_mask,p);
896 do_list(mget_mask, attribute,do_mget,False,True);
900 pstrcpy(mget_mask,cur_dir);
901 if(mget_mask[strlen(mget_mask)-1]!='\\')
902 pstrcat(mget_mask,"\\");
903 pstrcat(mget_mask,"*");
904 do_list(mget_mask, attribute,do_mget,False,True);
909 /****************************************************************************
910 make a directory of name "name"
911 ****************************************************************************/
912 static BOOL do_mkdir(char *name)
914 if (!cli_mkdir(cli, name)) {
915 d_printf("%s making remote directory %s\n",
916 cli_errstr(cli),name);
924 /****************************************************************************
926 ****************************************************************************/
927 static void cmd_quit(void)
934 /****************************************************************************
936 ****************************************************************************/
937 static void cmd_mkdir(void)
943 pstrcpy(mask,cur_dir);
945 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
947 d_printf("mkdir <dirname>\n");
958 trim_string(ddir,".",NULL);
959 p = strtok(ddir,"/\\");
962 if (!cli_chkpath(cli, ddir2)) {
966 p = strtok(NULL,"/\\");
974 /****************************************************************************
976 ****************************************************************************/
977 static void do_put(char *rname,char *lname)
983 int maxwrite=io_bufsize;
985 struct timeval tp_start;
986 GetTimeOfDay(&tp_start);
988 fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
991 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
995 /* allow files to be piped into smbclient
997 if (!strcmp(lname, "-")) {
999 /* size of file is not known */
1001 f = x_fopen(lname,O_RDONLY, 0);
1005 d_printf("Error opening local file %s\n",lname);
1010 DEBUG(1,("putting file %s as %s ",lname,
1013 buf = (char *)malloc(maxwrite);
1015 d_printf("ERROR: Not enough memory!\n");
1018 while (!x_feof(f)) {
1022 if ((n = readfile(buf,n,f)) < 1) {
1023 if((n == 0) && x_feof(f))
1024 break; /* Empty local file. */
1026 d_printf("Error reading local file: %s\n", strerror(errno));
1030 ret = cli_write(cli, fnum, 0, buf, nread, n);
1033 d_printf("Error writing file: %s\n", cli_errstr(cli));
1040 if (!cli_close(cli, fnum)) {
1041 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1052 struct timeval tp_end;
1055 GetTimeOfDay(&tp_end);
1057 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1058 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1059 put_total_time_ms += this_time;
1060 put_total_size += nread;
1062 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1063 nread / (1.024*this_time + 1.0e-4),
1064 put_total_size / (1.024*put_total_time_ms)));
1075 /****************************************************************************
1077 ****************************************************************************/
1078 static void cmd_put(void)
1085 pstrcpy(rname,cur_dir);
1086 pstrcat(rname,"\\");
1088 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1089 d_printf("put <filename>\n");
1094 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1097 pstrcat(rname,lname);
1099 dos_clean_name(rname);
1103 /* allow '-' to represent stdin
1104 jdblair, 24.jun.98 */
1105 if (!file_exist(lname,&st) &&
1106 (strcmp(lname,"-"))) {
1107 d_printf("%s does not exist\n",lname);
1112 do_put(rname,lname);
1115 /*************************************
1117 *************************************/
1119 static struct file_list {
1120 struct file_list *prev, *next;
1125 /****************************************************************************
1126 Free a file_list structure
1127 ****************************************************************************/
1129 static void free_file_list (struct file_list * list)
1131 struct file_list *tmp;
1136 DLIST_REMOVE(list, list);
1137 SAFE_FREE(tmp->file_path);
1142 /****************************************************************************
1143 seek in a directory/file list until you get something that doesn't start with
1145 ****************************************************************************/
1146 static BOOL seek_list(struct file_list *list, char *name)
1149 trim_string(list->file_path,"./","\n");
1150 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1159 /****************************************************************************
1160 set the file selection mask
1161 ****************************************************************************/
1162 static void cmd_select(void)
1164 pstrcpy(fileselection,"");
1165 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1168 /****************************************************************************
1169 Recursive file matching function act as find
1170 match must be always set to True when calling this function
1171 ****************************************************************************/
1172 static int file_find(struct file_list **list, const char *directory,
1173 const char *expression, BOOL match)
1176 struct file_list *entry;
1177 struct stat statbuf;
1183 dir = opendir(directory);
1184 if (!dir) return -1;
1186 while ((dname = readdirname(dir))) {
1187 if (!strcmp("..", dname)) continue;
1188 if (!strcmp(".", dname)) continue;
1190 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1195 if (!match || !gen_fnmatch(expression, dname)) {
1197 ret = stat(path, &statbuf);
1199 if (S_ISDIR(statbuf.st_mode)) {
1201 ret = file_find(list, path, expression, False);
1204 d_printf("file_find: cannot stat file %s\n", path);
1213 entry = (struct file_list *) malloc(sizeof (struct file_list));
1215 d_printf("Out of memory in file_find\n");
1219 entry->file_path = path;
1220 entry->isdir = isdir;
1221 DLIST_ADD(*list, entry);
1231 /****************************************************************************
1233 ****************************************************************************/
1234 static void cmd_mput(void)
1239 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1241 struct file_list *temp_list;
1242 char *quest, *lname, *rname;
1246 ret = file_find(&file_list, ".", p, True);
1248 free_file_list(file_list);
1256 for (temp_list = file_list; temp_list;
1257 temp_list = temp_list->next) {
1260 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1262 trim_string(lname, "./", "/");
1264 /* check if it's a directory */
1265 if (temp_list->isdir) {
1266 /* if (!recurse) continue; */
1269 asprintf(&quest, "Put directory %s? ", lname);
1270 if (prompt && !yesno(quest)) { /* No */
1271 /* Skip the directory */
1272 lname[strlen(lname)-1] = '/';
1273 if (!seek_list(temp_list, lname))
1277 asprintf(&rname, "%s%s", cur_dir, lname);
1279 if (!cli_chkpath(cli, rname) &&
1281 DEBUG (0, ("Unable to make dir, skipping..."));
1282 /* Skip the directory */
1283 lname[strlen(lname)-1] = '/';
1284 if (!seek_list(temp_list, lname))
1291 asprintf(&quest,"Put file %s? ", lname);
1292 if (prompt && !yesno(quest)) /* No */
1297 asprintf(&rname, "%s%s", cur_dir, lname);
1302 do_put(rname, lname);
1304 free_file_list(file_list);
1312 /****************************************************************************
1314 ****************************************************************************/
1315 static void do_cancel(int job)
1317 if (cli_printjob_del(cli, job)) {
1318 d_printf("Job %d cancelled\n",job);
1320 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1325 /****************************************************************************
1327 ****************************************************************************/
1328 static void cmd_cancel(void)
1333 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1334 d_printf("cancel <jobid> ...\n");
1340 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1344 /****************************************************************************
1346 ****************************************************************************/
1347 static void cmd_print(void)
1353 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1354 d_printf("print <filename>\n");
1358 pstrcpy(rname,lname);
1359 p = strrchr_m(rname,'/');
1361 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1364 if (strequal(lname,"-")) {
1365 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1368 do_put(rname, lname);
1372 /****************************************************************************
1373 show a print queue entry
1374 ****************************************************************************/
1375 static void queue_fn(struct print_job_info *p)
1377 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1380 /****************************************************************************
1382 ****************************************************************************/
1383 static void cmd_queue(void)
1385 cli_print_queue(cli, queue_fn);
1388 /****************************************************************************
1390 ****************************************************************************/
1391 static void do_del(file_info *finfo)
1395 pstrcpy(mask,cur_dir);
1396 pstrcat(mask,finfo->name);
1398 if (finfo->mode & aDIR)
1401 if (!cli_unlink(cli, mask)) {
1402 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1406 /****************************************************************************
1408 ****************************************************************************/
1409 static void cmd_del(void)
1413 uint16 attribute = aSYSTEM | aHIDDEN;
1418 pstrcpy(mask,cur_dir);
1420 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1421 d_printf("del <filename>\n");
1426 do_list(mask, attribute,do_del,False,False);
1429 /****************************************************************************
1430 ****************************************************************************/
1431 static void cmd_open(void)
1436 pstrcpy(mask,cur_dir);
1438 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1439 d_printf("del <filename>\n");
1444 cli_open(cli, mask, O_RDWR, DENY_ALL);
1448 /****************************************************************************
1450 ****************************************************************************/
1451 static void cmd_rmdir(void)
1456 pstrcpy(mask,cur_dir);
1458 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1459 d_printf("rmdir <dirname>\n");
1464 if (!cli_rmdir(cli, mask)) {
1465 d_printf("%s removing remote directory file %s\n",
1466 cli_errstr(cli),mask);
1470 /****************************************************************************
1472 ****************************************************************************/
1473 static void cmd_rename(void)
1478 pstrcpy(src,cur_dir);
1479 pstrcpy(dest,cur_dir);
1481 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1482 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1483 d_printf("rename <src> <dest>\n");
1490 if (!cli_rename(cli, src, dest)) {
1491 d_printf("%s renaming files\n",cli_errstr(cli));
1497 /****************************************************************************
1498 toggle the prompt flag
1499 ****************************************************************************/
1500 static void cmd_prompt(void)
1503 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1507 /****************************************************************************
1508 set the newer than time
1509 ****************************************************************************/
1510 static void cmd_newer(void)
1514 SMB_STRUCT_STAT sbuf;
1516 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
1517 if (ok && (sys_stat(buf,&sbuf) == 0)) {
1518 newer_than = sbuf.st_mtime;
1519 DEBUG(1,("Getting files newer than %s",
1520 asctime(LocalTime(&newer_than))));
1525 if (ok && newer_than == 0)
1526 d_printf("Error setting newer-than time\n");
1529 /****************************************************************************
1530 set the archive level
1531 ****************************************************************************/
1532 static void cmd_archive(void)
1536 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1537 archive_level = atoi(buf);
1539 d_printf("Archive level is %d\n",archive_level);
1542 /****************************************************************************
1543 toggle the lowercaseflag
1544 ****************************************************************************/
1545 static void cmd_lowercase(void)
1547 lowercase = !lowercase;
1548 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1554 /****************************************************************************
1555 toggle the recurse flag
1556 ****************************************************************************/
1557 static void cmd_recurse(void)
1560 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1563 /****************************************************************************
1564 toggle the translate flag
1565 ****************************************************************************/
1566 static void cmd_translate(void)
1568 translation = !translation;
1569 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1570 translation?"on":"off"));
1574 /****************************************************************************
1575 do a printmode command
1576 ****************************************************************************/
1577 static void cmd_printmode(void)
1582 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1583 if (strequal(buf,"text")) {
1586 if (strequal(buf,"graphics"))
1589 printmode = atoi(buf);
1596 fstrcpy(mode,"text");
1599 fstrcpy(mode,"graphics");
1602 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1606 DEBUG(2,("the printmode is now %s\n",mode));
1609 /****************************************************************************
1611 ****************************************************************************/
1612 static void cmd_lcd(void)
1617 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
1619 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
1622 /****************************************************************************
1624 ****************************************************************************/
1625 static void browse_fn(const char *name, uint32 m,
1626 const char *comment, void *state)
1634 case STYPE_DISKTREE:
1635 fstrcpy(typestr,"Disk"); break;
1637 fstrcpy(typestr,"Printer"); break;
1639 fstrcpy(typestr,"Device"); break;
1641 fstrcpy(typestr,"IPC"); break;
1643 d_printf("\t%-15.15s%-10.10s%s\n",
1644 name,typestr,comment);
1648 /****************************************************************************
1649 try and browse available connections on a host
1650 ****************************************************************************/
1651 static BOOL browse_host(BOOL sort)
1655 d_printf("\n\tSharename Type Comment\n");
1656 d_printf("\t--------- ---- -------\n");
1658 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
1659 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
1664 /****************************************************************************
1666 ****************************************************************************/
1667 static void server_fn(const char *name, uint32 m,
1668 const char *comment, void *state)
1670 d_printf("\t%-16.16s %s\n", name, comment);
1673 /****************************************************************************
1674 try and browse available connections on a host
1675 ****************************************************************************/
1676 static BOOL list_servers(char *wk_grp)
1678 if (!cli->server_domain) return False;
1680 d_printf("\n\tServer Comment\n");
1681 d_printf("\t--------- -------\n");
1683 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn, NULL);
1685 d_printf("\n\tWorkgroup Master\n");
1686 d_printf("\t--------- -------\n");
1688 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM, server_fn, NULL);
1692 /* Some constants for completing filename arguments */
1694 #define COMPL_NONE 0 /* No completions */
1695 #define COMPL_REMOTE 1 /* Complete remote filename */
1696 #define COMPL_LOCAL 2 /* Complete local filename */
1698 /* This defines the commands supported by this client.
1699 * NOTE: The "!" must be the last one in the list because it's fn pointer
1700 * field is NULL, and NULL in that field is used in process_tok()
1701 * (below) to indicate the end of the list. crh
1708 char compl_args[2]; /* Completion argument info */
1711 {"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}},
1712 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1713 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1714 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1715 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1716 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1717 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1718 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1719 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1720 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1721 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
1722 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1723 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1724 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1725 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1726 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1727 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1728 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1729 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1730 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1731 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1732 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
1733 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1734 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1735 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1736 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1737 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1738 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1739 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1740 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1741 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1742 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1743 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1744 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1745 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1746 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1747 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1748 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1749 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1750 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1751 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1752 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1756 /*******************************************************************
1757 lookup a command string in the list of commands, including
1759 ******************************************************************/
1760 static int process_tok(fstring tok)
1762 int i = 0, matches = 0;
1764 int tok_len = strlen(tok);
1766 while (commands[i].fn != NULL) {
1767 if (strequal(commands[i].name,tok)) {
1771 } else if (strnequal(commands[i].name, tok, tok_len)) {
1780 else if (matches == 1)
1786 /****************************************************************************
1788 ****************************************************************************/
1789 static void cmd_help(void)
1794 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1795 if ((i = process_tok(buf)) >= 0)
1796 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
1798 while (commands[i].description) {
1799 for (j=0; commands[i].description && (j<5); j++) {
1800 d_printf("%-15s",commands[i].name);
1809 /****************************************************************************
1810 process a -c command string
1811 ****************************************************************************/
1812 static void process_command_string(char *cmd)
1817 while (cmd[0] != '\0') {
1822 if ((p = strchr_m(cmd, ';')) == 0) {
1823 strncpy(line, cmd, 999);
1827 if (p - cmd > 999) p = cmd + 999;
1828 strncpy(line, cmd, p - cmd);
1829 line[p - cmd] = '\0';
1833 /* and get the first part of the command */
1835 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
1837 if ((i = process_tok(tok)) >= 0) {
1839 } else if (i == -2) {
1840 d_printf("%s: command abbreviation ambiguous\n",tok);
1842 d_printf("%s: command not found\n",tok);
1847 /****************************************************************************
1848 handle completion of commands for readline
1849 ****************************************************************************/
1850 static char **completion_fn(char *text, int start, int end)
1852 #define MAX_COMPLETIONS 100
1856 /* for words not at the start of the line fallback to filename completion */
1857 if (start) return NULL;
1859 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
1860 if (!matches) return NULL;
1862 matches[count++] = strdup(text);
1863 if (!matches[0]) return NULL;
1865 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
1866 if (strncmp(text, commands[i].name, strlen(text)) == 0) {
1867 matches[count] = strdup(commands[i].name);
1868 if (!matches[count]) return NULL;
1874 SAFE_FREE(matches[0]);
1875 matches[0] = strdup(matches[1]);
1877 matches[count] = NULL;
1882 /****************************************************************************
1883 make sure we swallow keepalives during idle time
1884 ****************************************************************************/
1885 static void readline_callback(void)
1888 struct timeval timeout;
1889 static time_t last_t;
1894 if (t - last_t < 5) return;
1900 FD_SET(cli->fd,&fds);
1903 timeout.tv_usec = 0;
1904 sys_select_intr(cli->fd+1,&fds,&timeout);
1906 /* We deliberately use receive_smb instead of
1907 client_receive_smb as we want to receive
1908 session keepalives and then drop them here.
1910 if (FD_ISSET(cli->fd,&fds)) {
1911 receive_smb(cli->fd,cli->inbuf,0);
1915 cli_chkpath(cli, "\\");
1919 /****************************************************************************
1920 process commands on stdin
1921 ****************************************************************************/
1922 static void process_stdin(void)
1933 /* display a prompt */
1934 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
1935 cline = smb_readline(the_prompt, readline_callback, completion_fn);
1939 pstrcpy(line, cline);
1941 /* special case - first char is ! */
1947 /* and get the first part of the command */
1949 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
1951 if ((i = process_tok(tok)) >= 0) {
1953 } else if (i == -2) {
1954 d_printf("%s: command abbreviation ambiguous\n",tok);
1956 d_printf("%s: command not found\n",tok);
1962 /*****************************************************
1963 return a connection to a server
1964 *******************************************************/
1965 struct cli_state *do_connect(const char *server, const char *share)
1967 struct cli_state *c;
1968 struct nmb_name called, calling;
1969 const char *server_n;
1971 extern struct in_addr ipzero;
1972 fstring servicename;
1975 /* make a copy so we don't modify the global string 'service' */
1976 safe_strcpy(servicename, share, sizeof(servicename)-1);
1977 sharename = servicename;
1978 if (*sharename == '\\') {
1979 server = sharename+2;
1980 sharename = strchr_m(server,'\\');
1981 if (!sharename) return NULL;
1990 make_nmb_name(&calling, global_myname, 0x0);
1991 make_nmb_name(&called , server, name_type);
1995 if (have_ip) ip = dest_ip;
1997 /* have to open a new connection */
1998 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) != port) ||
1999 !cli_connect(c, server_n, &ip)) {
2000 d_printf("Connection to %s failed\n", server_n);
2004 c->protocol = max_protocol;
2006 if (!cli_session_request(c, &calling, &called)) {
2008 d_printf("session request to %s failed (%s)\n",
2009 called.name, cli_errstr(c));
2012 if ((p=strchr_m(called.name, '.'))) {
2016 if (strcmp(called.name, "*SMBSERVER")) {
2017 make_nmb_name(&called , "*SMBSERVER", 0x20);
2023 DEBUG(4,(" session request ok\n"));
2025 if (!cli_negprot(c)) {
2026 d_printf("protocol negotiation failed\n");
2033 char *pass = getpass("Password: ");
2035 pstrcpy(password, pass);
2039 if (!cli_session_setup(c, username,
2040 password, strlen(password),
2041 password, strlen(password),
2043 /* if a password was not supplied then try again with a null username */
2044 if (password[0] || !username[0] ||
2045 !cli_session_setup(c, "", "", 0, "", 0, workgroup)) {
2046 d_printf("session setup failed: %s\n", cli_errstr(c));
2051 d_printf("Anonymous login successful\n");
2055 * These next two lines are needed to emulate
2056 * old client behaviour for people who have
2057 * scripts based on client output.
2058 * QUESTION ? Do we want to have a 'client compatibility
2059 * mode to turn these on/off ? JRA.
2062 if (*c->server_domain || *c->server_os || *c->server_type){
2063 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
2064 c->server_domain,c->server_os,c->server_type));
2067 DEBUG(4,(" session setup ok\n"));
2069 if (!cli_send_tconX(c, sharename, "?????",
2070 password, strlen(password)+1)) {
2071 d_printf("tree connect failed: %s\n", cli_errstr(c));
2077 DEBUG(4,(" tconx ok\n"));
2083 /****************************************************************************
2084 process commands from the client
2085 ****************************************************************************/
2086 static BOOL process(char *base_directory)
2088 cli = do_connect(desthost, service);
2093 if (*base_directory) do_cd(base_directory);
2096 process_command_string(cmdstr);
2105 /****************************************************************************
2106 usage on the program
2107 ****************************************************************************/
2108 static void usage(char *pname)
2110 d_printf("Usage: %s service <password> [options]", pname);
2112 d_printf("\nVersion %s\n",VERSION);
2113 d_printf("\t-s smb.conf pathname to smb.conf file\n");
2114 d_printf("\t-O socket_options socket options to use\n");
2115 d_printf("\t-R name resolve order use these name resolution services only\n");
2116 d_printf("\t-M host send a winpopup message to the host\n");
2117 d_printf("\t-i scope use this NetBIOS scope\n");
2118 d_printf("\t-N don't ask for a password\n");
2119 d_printf("\t-n netbios name. Use this name as my netbios name\n");
2120 d_printf("\t-d debuglevel set the debuglevel\n");
2121 d_printf("\t-P connect to service as a printer\n");
2122 d_printf("\t-p port connect to the specified port\n");
2123 d_printf("\t-l log basename. Basename for log/debug files\n");
2124 d_printf("\t-h Print this help message.\n");
2125 d_printf("\t-I dest IP use this IP to connect to\n");
2126 d_printf("\t-E write messages to stderr instead of stdout\n");
2127 d_printf("\t-U username set the network username\n");
2128 d_printf("\t-L host get a list of shares available on a host\n");
2129 d_printf("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
2130 d_printf("\t-m max protocol set the max protocol level\n");
2131 d_printf("\t-A filename get the credentials from a file\n");
2132 d_printf("\t-W workgroup set the workgroup name\n");
2133 d_printf("\t-T<c|x>IXFqgbNan command line tar\n");
2134 d_printf("\t-D directory start from directory\n");
2135 d_printf("\t-c command string execute semicolon separated commands\n");
2136 d_printf("\t-b xmit/send buffer changes the transmit/send buffer (default: 65520)\n");
2141 /****************************************************************************
2142 get a password from a a file or file descriptor
2144 ****************************************************************************/
2145 static void get_password_file(void)
2149 BOOL close_it = False;
2153 if ((p = getenv("PASSWD_FD")) != NULL) {
2154 pstrcpy(spec, "descriptor ");
2156 sscanf(p, "%d", &fd);
2158 } else if ((p = getenv("PASSWD_FILE")) != NULL) {
2159 fd = sys_open(p, O_RDONLY, 0);
2162 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
2163 spec, strerror(errno));
2169 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
2170 p && p - pass < sizeof(pass);) {
2171 switch (read(fd, p, 1)) {
2173 if (*p != '\n' && *p != '\0') {
2174 *++p = '\0'; /* advance p, and null-terminate pass */
2179 *p = '\0'; /* null-terminate it, just in case... */
2180 p = NULL; /* then force the loop condition to become false */
2183 fprintf(stderr, "Error reading password from file %s: %s\n",
2184 spec, "empty password\n");
2189 fprintf(stderr, "Error reading password from file %s: %s\n",
2190 spec, strerror(errno));
2194 pstrcpy(password, pass);
2201 /****************************************************************************
2203 ****************************************************************************/
2204 static int do_host_query(char *query_host)
2206 cli = do_connect(query_host, "IPC$");
2211 list_servers(workgroup);
2219 /****************************************************************************
2220 handle a tar operation
2221 ****************************************************************************/
2222 static int do_tar_op(char *base_directory)
2225 cli = do_connect(desthost, service);
2231 if (*base_directory) do_cd(base_directory);
2240 /****************************************************************************
2241 handle a message operation
2242 ****************************************************************************/
2243 static int do_message_op(void)
2246 struct nmb_name called, calling;
2250 make_nmb_name(&calling, global_myname, 0x0);
2251 make_nmb_name(&called , desthost, name_type);
2254 if (have_ip) ip = dest_ip;
2256 if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, port) != port) || !cli_connect(cli, desthost, &ip)) {
2257 d_printf("Connection to %s failed\n", desthost);
2261 if (!cli_session_request(cli, &calling, &called)) {
2262 d_printf("session request failed\n");
2274 /****************************************************************************
2276 ****************************************************************************/
2277 int main(int argc,char *argv[])
2279 fstring base_directory;
2280 char *pname = argv[0];
2282 extern char *optarg;
2286 BOOL message = False;
2287 extern char tar_type;
2288 static pstring servicesf = CONFIGFILE;
2290 pstring new_name_resolve_order;
2294 pstrcpy(term_code, KANJI);
2300 *base_directory = 0;
2302 *new_name_resolve_order = 0;
2306 setup_logging(pname,True);
2309 * If the -E option is given, be careful not to clobber stdout
2310 * before processing the options. 28.Feb.99, richard@hacom.nl.
2311 * Also pre-parse the -s option to get the service file name.
2314 for (opt = 1; opt < argc; opt++) {
2315 if (strcmp(argv[opt], "-E") == 0)
2317 else if(strncmp(argv[opt], "-s", 2) == 0) {
2318 if(argv[opt][2] != '\0')
2319 pstrcpy(servicesf, &argv[opt][2]);
2320 else if(argv[opt+1] != NULL) {
2322 * At least one more arg left.
2324 pstrcpy(servicesf, argv[opt+1]);
2334 in_client = True; /* Make sure that we tell lp_load we are */
2336 old_debug = DEBUGLEVEL;
2337 if (!lp_load(servicesf,True,False,False)) {
2338 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
2340 DEBUGLEVEL = old_debug;
2346 pstrcpy(workgroup,lp_workgroup());
2352 if (getenv("USER")) {
2353 pstrcpy(username,getenv("USER"));
2355 /* modification to support userid%passwd syntax in the USER var
2356 25.Aug.97, jdblair@uab.edu */
2358 if ((p=strchr_m(username,'%'))) {
2360 pstrcpy(password,p+1);
2362 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(password));
2367 /* modification to support PASSWD environmental var
2368 25.Aug.97, jdblair@uab.edu */
2369 if (getenv("PASSWD")) {
2370 pstrcpy(password,getenv("PASSWD"));
2374 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2375 get_password_file();
2379 if (*username == 0 && getenv("LOGNAME")) {
2380 pstrcpy(username,getenv("LOGNAME"));
2384 if (*username == 0) {
2385 pstrcpy(username,"GUEST");
2393 if (*argv[1] != '-') {
2394 pstrcpy(service,argv[1]);
2395 /* Convert any '/' characters in the service name to '\' characters */
2396 string_replace( service, '/','\\');
2400 if (count_chars(service,'\\') < 3) {
2402 d_printf("\n%s: Not enough '\\' characters in service\n",service);
2406 if (argc > 1 && (*argv[1] != '-')) {
2408 pstrcpy(password,argv[1]);
2409 memset(argv[1],'X',strlen(argv[1]));
2416 getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:")) != EOF) {
2419 pstrcpy(servicesf, optarg);
2422 pstrcpy(user_socket_options,optarg);
2425 pstrcpy(new_name_resolve_order, optarg);
2428 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2429 pstrcpy(desthost,optarg);
2434 extern pstring global_scope;
2435 pstrcpy(global_scope,optarg);
2436 strupper(global_scope);
2443 pstrcpy(global_myname,optarg);
2449 DEBUGLEVEL = atoi(optarg);
2452 /* not needed anymore */
2455 port = atoi(optarg);
2458 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2466 dest_ip = *interpret_addr2(optarg);
2467 if (zero_ip(dest_ip))
2473 display_set_stderr();
2479 pstrcpy(username,optarg);
2480 if ((lp=strchr_m(username,'%'))) {
2482 pstrcpy(password,lp+1);
2484 memset(strchr_m(optarg,'%')+1,'X',strlen(password));
2494 char *ptr, *val, *param;
2496 if ((auth=x_fopen(optarg, O_RDONLY, 0)) == NULL)
2498 /* fail if we can't open the credentials file */
2499 d_printf("ERROR: Unable to open credentials file!\n");
2503 while (!x_feof(auth))
2505 /* get a line from the file */
2506 if (!x_fgets(buf, sizeof(buf), auth))
2510 if ((len) && (buf[len-1]=='\n'))
2518 /* break up the line into parameter & value.
2519 will need to eat a little whitespace possibly */
2521 if (!(ptr = strchr_m (buf, '=')))
2526 /* eat leading white space */
2527 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
2530 if (strwicmp("password", param) == 0)
2532 pstrcpy(password, val);
2535 else if (strwicmp("username", param) == 0)
2536 pstrcpy(username, val);
2538 memset(buf, 0, sizeof(buf));
2546 while(*p == '\\' || *p == '/')
2548 pstrcpy(query_host,p);
2551 pstrcpy(term_code, optarg);
2554 max_protocol = interpret_protocol(optarg, max_protocol);
2557 pstrcpy(workgroup,optarg);
2560 if (!tar_parseargs(argc, argv, optarg, optind)) {
2566 pstrcpy(base_directory,optarg);
2572 io_bufsize = MAX(1, atoi(optarg));
2580 get_myname((*global_myname)?NULL:global_myname);
2582 if(*new_name_resolve_order)
2583 lp_set_name_resolve_order(new_name_resolve_order);
2585 if (!tar_type && !*query_host && !*service && !message) {
2590 DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2594 process_command_string(cmdstr);
2595 return do_tar_op(base_directory);
2598 if ((p=strchr_m(query_host,'#'))) {
2601 sscanf(p, "%x", &name_type);
2605 return do_host_query(query_host);
2609 return do_message_op();
2612 if (!process(base_directory)) {