2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 struct cli_state *cli;
31 extern BOOL in_client;
32 static int port = SMB_PORT;
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 size, int n, FILE *f)
143 if (!translation || (size != 1))
144 return(fread(b,size,n,f));
147 while (i < (n - 1) && (i < BUFFER_SIZE)) {
148 if ((c = 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 DEBUG(0,("message start: %s\n", cli_errstr(cli)));
177 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 printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
201 if (total_len >= 1600)
202 printf("the message was truncated to 1600 bytes\n");
204 printf("sent %d bytes\n",total_len);
206 if (!cli_message_end(cli, grp_id)) {
207 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 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli)));
226 DEBUG(0,("\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 DEBUG(0,("Current directory is %s",service));
236 DEBUG(0,("%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 DEBUG(0,("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 DEBUG(0,("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 DEBUG(0,(" %-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)
373 do_list_queue_size = 0;
374 do_list_queue_start = 0;
375 do_list_queue_end = 0;
378 static void init_do_list_queue(void)
380 reset_do_list_queue();
381 do_list_queue_size = 1024;
382 do_list_queue = malloc(do_list_queue_size);
383 if (do_list_queue == 0) {
384 DEBUG(0,("malloc fail for size %d\n",
385 (int)do_list_queue_size));
386 reset_do_list_queue();
388 memset(do_list_queue, 0, do_list_queue_size);
392 static void adjust_do_list_queue(void)
395 * If the starting point of the queue is more than half way through,
396 * move everything toward the beginning.
398 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
400 DEBUG(4,("do_list_queue is empty\n"));
401 do_list_queue_start = do_list_queue_end = 0;
402 *do_list_queue = '\0';
404 else if (do_list_queue_start > (do_list_queue_size / 2))
406 DEBUG(4,("sliding do_list_queue backward\n"));
407 memmove(do_list_queue,
408 do_list_queue + do_list_queue_start,
409 do_list_queue_end - do_list_queue_start);
410 do_list_queue_end -= do_list_queue_start;
411 do_list_queue_start = 0;
416 static void add_to_do_list_queue(const char* entry)
419 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
420 while (new_end > do_list_queue_size)
422 do_list_queue_size *= 2;
423 DEBUG(4,("enlarging do_list_queue to %d\n",
424 (int)do_list_queue_size));
425 dlq = Realloc(do_list_queue, do_list_queue_size);
427 DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
428 (int)do_list_queue_size));
429 reset_do_list_queue();
434 memset(do_list_queue + do_list_queue_size / 2,
435 0, do_list_queue_size / 2);
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 /****************************************************************************
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)) {
477 if (do_list_recurse &&
478 !strequal(f->name,".") &&
479 !strequal(f->name,"..")) {
483 pstrcpy(mask2, mask);
484 p = strrchr_m(mask2,'\\');
487 pstrcat(mask2, f->name);
488 pstrcat(mask2,"\\*");
489 add_to_do_list_queue(mask2);
494 if (do_this_one(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");
515 do_list_recurse = 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
531 * Fix from E. Jay Berkenbilt (ejb@ql.org)
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();
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;
549 DEBUG(0,("\n%s\n",next_file));
559 if (cli_list(cli, mask, attribute, do_list_helper, NULL) == -1)
561 DEBUG(0, ("%s listing %s\n", cli_errstr(cli), mask));
566 reset_do_list_queue();
569 /****************************************************************************
570 get a directory listing
571 ****************************************************************************/
572 static void cmd_dir(void)
574 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
580 pstrcpy(mask,cur_dir);
581 if(mask[strlen(mask)-1]!='\\')
584 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
595 do_list(mask, attribute, display_finfo, recurse, True);
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;
614 pstrcpy(mask,cur_dir);
615 if(mask[strlen(mask)-1]!='\\')
618 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
628 do_list(mask, attribute, do_du, recurse, True);
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)
642 BOOL newhandle = False;
644 struct timeval tp_start;
645 int read_size = io_bufsize;
650 GetTimeOfDay(&tp_start);
656 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
659 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
663 if(!strcmp(lname,"-")) {
664 handle = fileno(stdout);
666 handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
670 DEBUG(0,("Error opening local file %s\n",lname));
675 if (!cli_qfileinfo(cli, fnum,
676 &attr, &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)));
683 DEBUG(2,("getting file %s of size %.0f as %s ",
684 lname, (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);
693 int n = cli_read(cli, fnum, data, nread, read_size);
697 if (writefile(handle,data, n) != n) {
698 DEBUG(0,("Error writing local file\n"));
706 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
707 rname, (long)nread));
712 if (!cli_close(cli, fnum)) {
713 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
720 if (archive_level >= 2 && (attr & aARCH)) {
721 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
725 struct timeval tp_end;
728 GetTimeOfDay(&tp_end);
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 /****************************************************************************
744 ****************************************************************************/
745 static void cmd_get(void)
751 pstrcpy(rname,cur_dir);
754 p = rname + strlen(rname);
756 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
757 DEBUG(0,("get <filename>\n"));
761 dos_clean_name(rname);
763 next_token_nr(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)
776 pstring saved_curdir;
779 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
783 DEBUG(0,("mget aborted\n"));
787 if (finfo->mode & aDIR)
788 slprintf(quest,sizeof(pstring)-1,
789 "Get directory %s? ",finfo->name);
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);
803 /* handle directories */
804 pstrcpy(saved_curdir,cur_dir);
806 pstrcat(cur_dir,finfo->name);
807 pstrcat(cur_dir,"\\");
809 unix_format(finfo->name);
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);
820 if (chdir(finfo->name) != 0) {
821 DEBUG(0,("failed to chdir to directory %s\n",finfo->name));
822 pstrcpy(cur_dir,saved_curdir);
826 pstrcpy(mget_mask,cur_dir);
827 pstrcat(mget_mask,"*");
829 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
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;
844 fstrcpy(rname,cur_dir);
847 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
848 fd = smb_mkstemp(lname);
850 DEBUG(0,("failed to create temporary file for more\n"));
855 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
856 DEBUG(0,("more <filename>\n"));
860 dos_clean_name(rname);
864 pager=getenv("PAGER");
866 slprintf(pager_cmd,sizeof(pager_cmd)-1,
867 "%s %s",(pager? pager:PAGER), lname);
874 /****************************************************************************
876 ****************************************************************************/
877 static void cmd_mget(void)
879 uint16 attribute = aSYSTEM | aHIDDEN;
891 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
892 pstrcpy(mget_mask,cur_dir);
893 if(mget_mask[strlen(mget_mask)-1]!='\\')
894 pstrcat(mget_mask,"\\");
897 pstrcpy(mget_mask,p);
899 pstrcat(mget_mask,p);
900 do_list(mget_mask, attribute,do_mget,False,True);
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));
928 /****************************************************************************
930 ****************************************************************************/
931 static void cmd_quit(void)
938 /****************************************************************************
940 ****************************************************************************/
941 static void cmd_mkdir(void)
947 pstrcpy(mask,cur_dir);
949 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
951 DEBUG(0,("mkdir <dirname>\n"));
962 trim_string(ddir,".",NULL);
963 p = strtok(ddir,"/\\");
966 if (!cli_chkpath(cli, ddir2)) {
970 p = strtok(NULL,"/\\");
978 /****************************************************************************
980 ****************************************************************************/
981 static void do_put(char *rname,char *lname)
987 int maxwrite=io_bufsize;
989 struct timeval tp_start;
990 GetTimeOfDay(&tp_start);
992 fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
995 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
999 /* allow files to be piped into smbclient
1000 jdblair 24.jun.98 */
1001 if (!strcmp(lname, "-")) {
1003 /* size of file is not known */
1005 f = sys_fopen(lname,"r");
1009 DEBUG(0,("Error opening local file %s\n",lname));
1014 DEBUG(1,("putting file %s as %s ",lname,
1017 buf = (char *)malloc(maxwrite);
1019 DEBUG(0, ("ERROR: Not enough memory!\n"));
1026 if ((n = readfile(buf,1,n,f)) < 1) {
1027 if((n == 0) && feof(f))
1028 break; /* Empty local file. */
1030 DEBUG(0,("Error reading local file: %s\n", strerror(errno) ));
1034 ret = cli_write(cli, fnum, 0, buf, nread, n);
1037 DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
1044 if (!cli_close(cli, fnum)) {
1045 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),rname));
1056 struct timeval tp_end;
1059 GetTimeOfDay(&tp_end);
1061 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1062 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1063 put_total_time_ms += this_time;
1064 put_total_size += nread;
1066 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1067 nread / (1.024*this_time + 1.0e-4),
1068 put_total_size / (1.024*put_total_time_ms)));
1079 /****************************************************************************
1081 ****************************************************************************/
1082 static void cmd_put(void)
1089 pstrcpy(rname,cur_dir);
1090 pstrcat(rname,"\\");
1092 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1093 DEBUG(0,("put <filename>\n"));
1098 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1101 pstrcat(rname,lname);
1103 dos_clean_name(rname);
1107 /* allow '-' to represent stdin
1108 jdblair, 24.jun.98 */
1109 if (!file_exist(lname,&st) &&
1110 (strcmp(lname,"-"))) {
1111 DEBUG(0,("%s does not exist\n",lname));
1116 do_put(rname,lname);
1119 /*************************************
1121 *************************************/
1123 static struct file_list {
1124 struct file_list *prev, *next;
1129 /****************************************************************************
1130 Free a file_list structure
1131 ****************************************************************************/
1133 static void free_file_list (struct file_list * list)
1135 struct file_list *tmp;
1140 DLIST_REMOVE(list, list);
1141 if (tmp->file_path) free(tmp->file_path);
1146 /****************************************************************************
1147 seek in a directory/file list until you get something that doesn't start with
1149 ****************************************************************************/
1150 static BOOL seek_list(struct file_list *list, char *name)
1153 trim_string(list->file_path,"./","\n");
1154 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1163 /****************************************************************************
1164 set the file selection mask
1165 ****************************************************************************/
1166 static void cmd_select(void)
1168 pstrcpy(fileselection,"");
1169 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1172 /****************************************************************************
1173 Recursive file matching function act as find
1174 match must be always set to True when calling this function
1175 ****************************************************************************/
1176 static int file_find(struct file_list **list, const char *directory,
1177 const char *expression, BOOL match)
1180 struct file_list *entry;
1181 struct stat statbuf;
1187 dir = opendir(directory);
1188 if (!dir) return -1;
1190 while ((dname = readdirname(dir))) {
1191 if (!strcmp("..", dname)) continue;
1192 if (!strcmp(".", dname)) continue;
1194 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1199 if (!match || !gen_fnmatch(expression, dname)) {
1201 ret = stat(path, &statbuf);
1203 if (S_ISDIR(statbuf.st_mode)) {
1205 ret = file_find(list, path, expression, False);
1208 DEBUG(0,("file_find: cannot stat file %s\n", path));
1217 entry = (struct file_list *) malloc(sizeof (struct file_list));
1219 DEBUG(0,("Out of memory in file_find\n"));
1223 entry->file_path = path;
1224 entry->isdir = isdir;
1225 DLIST_ADD(*list, entry);
1235 /****************************************************************************
1237 ****************************************************************************/
1238 static void cmd_mput(void)
1243 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1245 struct file_list *temp_list;
1246 char *quest, *lname, *rname;
1250 ret = file_find(&file_list, ".", p, True);
1252 free_file_list(file_list);
1260 for (temp_list = file_list; temp_list;
1261 temp_list = temp_list->next) {
1263 if (lname) free(lname);
1264 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1266 trim_string(lname, "./", "/");
1268 /* check if it's a directory */
1269 if (temp_list->isdir) {
1270 /* if (!recurse) continue; */
1272 if (quest) free(quest);
1273 asprintf(&quest, "Put directory %s? ", lname);
1274 if (prompt && !yesno(quest)) { /* No */
1275 /* Skip the directory */
1276 lname[strlen(lname)-1] = '/';
1277 if (!seek_list(temp_list, lname))
1280 if (rname) free(rname);
1281 asprintf(&rname, "%s%s", cur_dir, lname);
1283 if (!cli_chkpath(cli, rname) &&
1285 DEBUG (0, ("Unable to make dir, skipping..."));
1286 /* Skip the directory */
1287 lname[strlen(lname)-1] = '/';
1288 if (!seek_list(temp_list, lname))
1294 if (quest) free(quest);
1295 asprintf(&quest,"Put file %s? ", lname);
1296 if (prompt && !yesno(quest)) /* No */
1300 if (rname) free(rname);
1301 asprintf(&rname, "%s%s", cur_dir, lname);
1306 do_put(rname, lname);
1308 free_file_list(file_list);
1309 if (quest) free(quest);
1310 if (lname) free(lname);
1311 if (rname) free(rname);
1316 /****************************************************************************
1318 ****************************************************************************/
1319 static void do_cancel(int job)
1321 if (cli_printjob_del(cli, job)) {
1322 printf("Job %d cancelled\n",job);
1324 printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1329 /****************************************************************************
1331 ****************************************************************************/
1332 static void cmd_cancel(void)
1337 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1338 printf("cancel <jobid> ...\n");
1344 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1348 /****************************************************************************
1350 ****************************************************************************/
1351 static void cmd_print(void)
1357 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1358 DEBUG(0,("print <filename>\n"));
1362 pstrcpy(rname,lname);
1363 p = strrchr_m(rname,'/');
1365 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1368 if (strequal(lname,"-")) {
1369 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1372 do_put(rname, lname);
1376 /****************************************************************************
1377 show a print queue entry
1378 ****************************************************************************/
1379 static void queue_fn(struct print_job_info *p)
1381 DEBUG(0,("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name));
1384 /****************************************************************************
1386 ****************************************************************************/
1387 static void cmd_queue(void)
1389 cli_print_queue(cli, queue_fn);
1392 /****************************************************************************
1394 ****************************************************************************/
1395 static void do_del(file_info *finfo)
1399 pstrcpy(mask,cur_dir);
1400 pstrcat(mask,finfo->name);
1402 if (finfo->mode & aDIR)
1405 if (!cli_unlink(cli, mask)) {
1406 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),mask));
1410 /****************************************************************************
1412 ****************************************************************************/
1413 static void cmd_del(void)
1417 uint16 attribute = aSYSTEM | aHIDDEN;
1422 pstrcpy(mask,cur_dir);
1424 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1425 DEBUG(0,("del <filename>\n"));
1430 do_list(mask, attribute,do_del,False,False);
1433 /****************************************************************************
1434 ****************************************************************************/
1435 static void cmd_open(void)
1440 pstrcpy(mask,cur_dir);
1442 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1443 DEBUG(0,("del <filename>\n"));
1448 cli_open(cli, mask, O_RDWR, DENY_ALL);
1452 /****************************************************************************
1454 ****************************************************************************/
1455 static void cmd_rmdir(void)
1460 pstrcpy(mask,cur_dir);
1462 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1463 DEBUG(0,("rmdir <dirname>\n"));
1468 if (!cli_rmdir(cli, mask)) {
1469 DEBUG(0,("%s removing remote directory file %s\n",
1470 cli_errstr(cli),mask));
1474 /****************************************************************************
1476 ****************************************************************************/
1477 static void cmd_rename(void)
1482 pstrcpy(src,cur_dir);
1483 pstrcpy(dest,cur_dir);
1485 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1486 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1487 DEBUG(0,("rename <src> <dest>\n"));
1494 if (!cli_rename(cli, src, dest)) {
1495 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1501 /****************************************************************************
1502 toggle the prompt flag
1503 ****************************************************************************/
1504 static void cmd_prompt(void)
1507 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1511 /****************************************************************************
1512 set the newer than time
1513 ****************************************************************************/
1514 static void cmd_newer(void)
1518 SMB_STRUCT_STAT sbuf;
1520 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
1521 if (ok && (sys_stat(buf,&sbuf) == 0)) {
1522 newer_than = sbuf.st_mtime;
1523 DEBUG(1,("Getting files newer than %s",
1524 asctime(LocalTime(&newer_than))));
1529 if (ok && newer_than == 0)
1530 DEBUG(0,("Error setting newer-than time\n"));
1533 /****************************************************************************
1534 set the archive level
1535 ****************************************************************************/
1536 static void cmd_archive(void)
1540 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1541 archive_level = atoi(buf);
1543 DEBUG(0,("Archive level is %d\n",archive_level));
1546 /****************************************************************************
1547 toggle the lowercaseflag
1548 ****************************************************************************/
1549 static void cmd_lowercase(void)
1551 lowercase = !lowercase;
1552 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1558 /****************************************************************************
1559 toggle the recurse flag
1560 ****************************************************************************/
1561 static void cmd_recurse(void)
1564 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1567 /****************************************************************************
1568 toggle the translate flag
1569 ****************************************************************************/
1570 static void cmd_translate(void)
1572 translation = !translation;
1573 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1574 translation?"on":"off"));
1578 /****************************************************************************
1579 do a printmode command
1580 ****************************************************************************/
1581 static void cmd_printmode(void)
1586 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1587 if (strequal(buf,"text")) {
1590 if (strequal(buf,"graphics"))
1593 printmode = atoi(buf);
1600 fstrcpy(mode,"text");
1603 fstrcpy(mode,"graphics");
1606 slprintf(mode,sizeof(mode)-1,"%d",printmode);
1610 DEBUG(2,("the printmode is now %s\n",mode));
1613 /****************************************************************************
1615 ****************************************************************************/
1616 static void cmd_lcd(void)
1621 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
1623 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
1626 /****************************************************************************
1628 ****************************************************************************/
1629 static void browse_fn(const char *name, uint32 m,
1630 const char *comment, void *state)
1638 case STYPE_DISKTREE:
1639 fstrcpy(typestr,"Disk"); break;
1641 fstrcpy(typestr,"Printer"); break;
1643 fstrcpy(typestr,"Device"); break;
1645 fstrcpy(typestr,"IPC"); break;
1647 printf("\t%-15.15s%-10.10s%s\n",
1648 name,typestr,comment);
1652 /****************************************************************************
1653 try and browse available connections on a host
1654 ****************************************************************************/
1655 static BOOL browse_host(BOOL sort)
1659 printf("\n\tSharename Type Comment\n");
1660 printf("\t--------- ---- -------\n");
1662 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
1663 printf("Error returning browse list: %s\n", cli_errstr(cli));
1668 /****************************************************************************
1670 ****************************************************************************/
1671 static void server_fn(const char *name, uint32 m,
1672 const char *comment, void *state)
1674 printf("\t%-16.16s %s\n", name, comment);
1677 /****************************************************************************
1678 try and browse available connections on a host
1679 ****************************************************************************/
1680 static BOOL list_servers(char *wk_grp)
1682 if (!cli->server_domain) return False;
1684 printf("\n\tServer Comment\n");
1685 printf("\t--------- -------\n");
1687 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn, NULL);
1689 printf("\n\tWorkgroup Master\n");
1690 printf("\t--------- -------\n");
1692 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM, server_fn, NULL);
1696 /* Some constants for completing filename arguments */
1698 #define COMPL_NONE 0 /* No completions */
1699 #define COMPL_REMOTE 1 /* Complete remote filename */
1700 #define COMPL_LOCAL 2 /* Complete local filename */
1702 /* This defines the commands supported by this client */
1708 char compl_args[2]; /* Completion argument info */
1711 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1712 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1713 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1714 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1715 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1716 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1717 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1718 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1719 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1720 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1721 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1722 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
1723 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1724 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1725 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
1726 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1727 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1728 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1729 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1730 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1731 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
1732 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
1733 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
1734 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
1735 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1736 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1737 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1738 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1739 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1740 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1741 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1742 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1743 {"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}},
1744 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1745 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1746 {"tarmode",cmd_tarmode,
1747 "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1748 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1749 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1750 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1751 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
1752 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1753 {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1757 /*******************************************************************
1758 lookup a command string in the list of commands, including
1760 ******************************************************************/
1761 static int process_tok(fstring tok)
1763 int i = 0, matches = 0;
1765 int tok_len = strlen(tok);
1767 while (commands[i].fn != NULL) {
1768 if (strequal(commands[i].name,tok)) {
1772 } else if (strnequal(commands[i].name, tok, tok_len)) {
1781 else if (matches == 1)
1787 /****************************************************************************
1789 ****************************************************************************/
1790 static void cmd_help(void)
1795 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1796 if ((i = process_tok(buf)) >= 0)
1797 DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));
1799 while (commands[i].description) {
1800 for (j=0; commands[i].description && (j<5); j++) {
1801 DEBUG(0,("%-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 DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
1842 DEBUG(0,("%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;
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 DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
1956 DEBUG(0,("%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) == 0) ||
1999 !cli_connect(c, server_n, &ip)) {
2000 DEBUG(0,("Connection to %s failed\n", server_n));
2004 c->protocol = max_protocol;
2006 if (!cli_session_request(c, &calling, &called)) {
2008 DEBUG(0,("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 DEBUG(0,("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 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
2051 DEBUG(0,("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 DEBUG(0,("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 DEBUG(0,("Usage: %s service <password> [options]", pname));
2112 DEBUG(0,("\nVersion %s\n",VERSION));
2113 DEBUG(0,("\t-s smb.conf pathname to smb.conf file\n"));
2114 DEBUG(0,("\t-O socket_options socket options to use\n"));
2115 DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
2116 DEBUG(0,("\t-M host send a winpopup message to the host\n"));
2117 DEBUG(0,("\t-i scope use this NetBIOS scope\n"));
2118 DEBUG(0,("\t-N don't ask for a password\n"));
2119 DEBUG(0,("\t-n netbios name. Use this name as my netbios name\n"));
2120 DEBUG(0,("\t-d debuglevel set the debuglevel\n"));
2121 DEBUG(0,("\t-P connect to service as a printer\n"));
2122 DEBUG(0,("\t-p port connect to the specified port\n"));
2123 DEBUG(0,("\t-l log basename. Basename for log/debug files\n"));
2124 DEBUG(0,("\t-h Print this help message.\n"));
2125 DEBUG(0,("\t-I dest IP use this IP to connect to\n"));
2126 DEBUG(0,("\t-E write messages to stderr instead of stdout\n"));
2127 DEBUG(0,("\t-U username set the network username\n"));
2128 DEBUG(0,("\t-L host get a list of shares available on a host\n"));
2129 DEBUG(0,("\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
2130 DEBUG(0,("\t-m max protocol set the max protocol level\n"));
2131 DEBUG(0,("\t-A filename get the credentials from a file\n"));
2132 DEBUG(0,("\t-W workgroup set the workgroup name\n"));
2133 DEBUG(0,("\t-T<c|x>IXFqgbNan command line tar\n"));
2134 DEBUG(0,("\t-D directory start from directory\n"));
2135 DEBUG(0,("\t-c command string execute semicolon separated commands\n"));
2136 DEBUG(0,("\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) == 0) || !cli_connect(cli, desthost, &ip)) {
2257 DEBUG(0,("Connection to %s failed\n", desthost));
2261 if (!cli_session_request(cli, &calling, &called)) {
2262 DEBUG(0,("session request failed\n"));
2274 /****************************************************************************
2276 ****************************************************************************/
2277 int main(int argc,char *argv[])
2279 fstring base_directory;
2280 char *pname = argv[0];
2283 extern char *optarg;
2287 BOOL message = False;
2288 extern char tar_type;
2289 static pstring servicesf = CONFIGFILE;
2291 pstring new_name_resolve_order;
2295 pstrcpy(term_code, KANJI);
2301 *base_directory = 0;
2303 *new_name_resolve_order = 0;
2307 setup_logging(pname,True);
2310 * If the -E option is given, be careful not to clobber stdout
2311 * before processing the options. 28.Feb.99, richard@hacom.nl.
2312 * Also pre-parse the -s option to get the service file name.
2315 for (opt = 1; opt < argc; opt++) {
2316 if (strcmp(argv[opt], "-E") == 0)
2318 else if(strncmp(argv[opt], "-s", 2) == 0) {
2319 if(argv[opt][2] != '\0')
2320 pstrcpy(servicesf, &argv[opt][2]);
2321 else if(argv[opt+1] != NULL) {
2323 * At least one more arg left.
2325 pstrcpy(servicesf, argv[opt+1]);
2335 in_client = True; /* Make sure that we tell lp_load we are */
2337 old_debug = DEBUGLEVEL;
2338 if (!lp_load(servicesf,True,False,False)) {
2339 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
2341 DEBUGLEVEL = old_debug;
2347 pstrcpy(workgroup,lp_workgroup());
2353 if (getenv("USER")) {
2354 pstrcpy(username,getenv("USER"));
2356 /* modification to support userid%passwd syntax in the USER var
2357 25.Aug.97, jdblair@uab.edu */
2359 if ((p=strchr_m(username,'%'))) {
2361 pstrcpy(password,p+1);
2363 memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(password));
2368 /* modification to support PASSWD environmental var
2369 25.Aug.97, jdblair@uab.edu */
2370 if (getenv("PASSWD")) {
2371 pstrcpy(password,getenv("PASSWD"));
2375 if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2376 get_password_file();
2380 if (*username == 0 && getenv("LOGNAME")) {
2381 pstrcpy(username,getenv("LOGNAME"));
2385 if (*username == 0) {
2386 pstrcpy(username,"GUEST");
2394 if (*argv[1] != '-') {
2395 pstrcpy(service,argv[1]);
2396 /* Convert any '/' characters in the service name to '\' characters */
2397 string_replace( service, '/','\\');
2401 if (count_chars(service,'\\') < 3) {
2403 printf("\n%s: Not enough '\\' characters in service\n",service);
2407 if (argc > 1 && (*argv[1] != '-')) {
2409 pstrcpy(password,argv[1]);
2410 memset(argv[1],'X',strlen(argv[1]));
2417 getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:A:")) != EOF) {
2420 pstrcpy(servicesf, optarg);
2423 pstrcpy(user_socket_options,optarg);
2426 pstrcpy(new_name_resolve_order, optarg);
2429 name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2430 pstrcpy(desthost,optarg);
2435 extern pstring global_scope;
2436 pstrcpy(global_scope,optarg);
2437 strupper(global_scope);
2444 pstrcpy(global_myname,optarg);
2450 DEBUGLEVEL = atoi(optarg);
2453 /* not needed anymore */
2456 port = atoi(optarg);
2459 slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2467 dest_ip = *interpret_addr2(optarg);
2468 if (zero_ip(dest_ip))
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=sys_fopen(optarg, "r")) == NULL)
2498 /* fail if we can't open the credentials file */
2499 DEBUG(0,("ERROR: Unable to open credentials file!\n"));
2505 /* get a line from the file */
2506 if (!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)) {