2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) James J Myers 2003 <myersjj@samba.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 struct cli_state *cli;
30 extern BOOL in_client;
32 pstring cur_dir = "\\";
33 static pstring cd_path = "";
34 static pstring service;
35 static pstring desthost;
36 static pstring username;
37 static pstring password;
38 static BOOL use_kerberos;
40 static char *cmdstr = NULL;
42 static int io_bufsize = 64512;
44 static int name_type = 0x20;
46 static int process_tok(fstring tok);
47 static int cmd_help(void);
49 /* 30 second timeout on most commands */
50 #define CLIENT_TIMEOUT (30*1000)
51 #define SHORT_TIMEOUT (5*1000)
53 /* value for unused fid field in trans2 secondary request */
54 #define FID_UNUSED (0xFFFF)
56 time_t newer_than = 0;
57 static int archive_level = 0;
59 static BOOL translation = False;
63 /* clitar bits insert */
66 extern BOOL tar_reset;
70 static BOOL prompt = True;
72 static int printmode = 1;
74 static BOOL recurse = False;
75 BOOL lowercase = False;
77 static struct in_addr dest_ip;
79 #define SEPARATORS " \t\n\r"
81 static BOOL abort_mget = True;
83 static pstring fileselection = "";
85 extern file_info def_finfo;
88 SMB_BIG_UINT get_total_size = 0;
89 unsigned int get_total_time_ms = 0;
90 static SMB_BIG_UINT put_total_size = 0;
91 static unsigned int put_total_time_ms = 0;
94 static double dir_total;
98 /* some forward declarations */
99 static struct cli_state *do_connect(const char *server, const char *share);
102 /*******************************************************************
103 Reduce a file name, removing .. elements.
104 ********************************************************************/
105 void dos_clean_name(char *s)
109 DEBUG(3,("dos_clean_name [%s]\n",s));
111 /* remove any double slashes */
112 all_string_sub(s, "\\\\", "\\", 0);
114 while ((p = strstr(s,"\\..\\")) != NULL) {
120 if ((p=strrchr_m(s,'\\')) != NULL)
127 trim_string(s,NULL,"\\..");
129 all_string_sub(s, "\\.\\", "\\", 0);
132 /****************************************************************************
133 write to a local file with CR/LF->LF translation if appropriate. return the
134 number taken from the buffer. This may not equal the number written.
135 ****************************************************************************/
136 static int writefile(int f, char *b, int n)
146 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
149 if (write(f, b, 1) != 1) {
159 /****************************************************************************
160 read from a file with LF->CR/LF translation if appropriate. return the
161 number read. read approx n bytes.
162 ****************************************************************************/
163 static int readfile(char *b, int n, XFILE *f)
169 return x_fread(b,1,n,f);
172 while (i < (n - 1) && (i < CLI_BUFFER_SIZE)) {
173 if ((c = x_getc(f)) == EOF) {
177 if (c == '\n') { /* change all LFs to CR/LF */
188 /****************************************************************************
190 ****************************************************************************/
191 static void send_message(void)
196 if (!cli_message_start(cli, desthost, username, &grp_id)) {
197 d_printf("message start: %s\n", cli_errstr(cli));
202 d_printf("Connected. Type your message, ending it with a Control-D\n");
204 while (!feof(stdin) && total_len < 1600) {
205 int maxlen = MIN(1600 - total_len,127);
212 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
218 if (!cli_message_text(cli, msg, l, grp_id)) {
219 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
226 if (total_len >= 1600)
227 d_printf("the message was truncated to 1600 bytes\n");
229 d_printf("sent %d bytes\n",total_len);
231 if (!cli_message_end(cli, grp_id)) {
232 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
239 /****************************************************************************
240 check the space on a device
241 ****************************************************************************/
242 static int do_dskattr(void)
244 int total, bsize, avail;
246 if (!cli_dskattr(cli, &bsize, &total, &avail)) {
247 d_printf("Error in dskattr: %s\n",cli_errstr(cli));
251 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
252 total, bsize, avail);
257 /****************************************************************************
259 ****************************************************************************/
260 static int cmd_pwd(void)
262 d_printf("Current directory is %s",service);
263 d_printf("%s\n",cur_dir);
268 /****************************************************************************
269 change directory - inner section
270 ****************************************************************************/
271 static int do_cd(char *newdir)
279 /* Save the current directory in case the
280 new directory is invalid */
281 pstrcpy(saved_dir, cur_dir);
286 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
287 pstrcat(cur_dir, "\\");
289 dos_clean_name(cur_dir);
290 pstrcpy(dname,cur_dir);
291 pstrcat(cur_dir,"\\");
292 dos_clean_name(cur_dir);
294 if (!strequal(cur_dir,"\\")) {
295 if (!cli_chkpath(cli, dname)) {
296 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
297 pstrcpy(cur_dir,saved_dir);
301 pstrcpy(cd_path,cur_dir);
306 /****************************************************************************
308 ****************************************************************************/
309 static int cmd_cd(void)
314 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
317 d_printf("Current directory is %s\n",cur_dir);
323 /*******************************************************************
324 decide if a file should be operated on
325 ********************************************************************/
326 static BOOL do_this_one(file_info *finfo)
328 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) return(True);
330 if (*fileselection &&
331 !mask_match(cli, finfo->name,fileselection,False)) {
332 DEBUG(3,("mask_match %s failed\n", finfo->name));
336 if (newer_than && finfo->mtime < newer_than) {
337 DEBUG(3,("newer_than %s failed\n", finfo->name));
341 if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
342 DEBUG(3,("archive %s failed\n", finfo->name));
349 /*******************************************************************
350 Return a string representing an attribute for a file.
351 ********************************************************************/
352 static const char *attrib_string(uint16 mode)
354 static fstring attrstr;
360 {'V', FILE_ATTRIBUTE_VOLUME},
361 {'D', FILE_ATTRIBUTE_DIRECTORY},
362 {'A', FILE_ATTRIBUTE_ARCHIVE},
363 {'H', FILE_ATTRIBUTE_HIDDEN},
364 {'S', FILE_ATTRIBUTE_SYSTEM},
365 {'R', FILE_ATTRIBUTE_READONLY},
366 {'d', FILE_ATTRIBUTE_DEVICE},
367 {'t', FILE_ATTRIBUTE_TEMPORARY},
368 {'s', FILE_ATTRIBUTE_SPARSE},
369 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
370 {'c', FILE_ATTRIBUTE_COMPRESSED},
371 {'o', FILE_ATTRIBUTE_OFFLINE},
372 {'n', FILE_ATTRIBUTE_NONINDEXED},
373 {'e', FILE_ATTRIBUTE_ENCRYPTED}
376 for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
377 if (mode & attr_strs[i].attr) {
378 attrstr[len++] = attr_strs[i].c;
387 /****************************************************************************
388 display info about a file
389 ****************************************************************************/
390 static void display_finfo(file_info *finfo)
392 if (do_this_one(finfo)) {
393 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
394 d_printf(" %-30s%7.7s %8.0f %s",
396 attrib_string(finfo->mode),
398 asctime(LocalTime(&t)));
399 dir_total += finfo->size;
404 /****************************************************************************
405 accumulate size of a file
406 ****************************************************************************/
407 static void do_du(file_info *finfo)
409 if (do_this_one(finfo)) {
410 dir_total += finfo->size;
414 static BOOL do_list_recurse;
415 static BOOL do_list_dirs;
416 static char *do_list_queue = 0;
417 static long do_list_queue_size = 0;
418 static long do_list_queue_start = 0;
419 static long do_list_queue_end = 0;
420 static void (*do_list_fn)(file_info *);
422 /****************************************************************************
423 functions for do_list_queue
424 ****************************************************************************/
427 * The do_list_queue is a NUL-separated list of strings stored in a
428 * char*. Since this is a FIFO, we keep track of the beginning and
429 * ending locations of the data in the queue. When we overflow, we
430 * double the size of the char*. When the start of the data passes
431 * the midpoint, we move everything back. This is logically more
432 * complex than a linked list, but easier from a memory management
433 * angle. In any memory error condition, do_list_queue is reset.
434 * Functions check to ensure that do_list_queue is non-NULL before
437 static void reset_do_list_queue(void)
439 SAFE_FREE(do_list_queue);
440 do_list_queue_size = 0;
441 do_list_queue_start = 0;
442 do_list_queue_end = 0;
445 static void init_do_list_queue(void)
447 reset_do_list_queue();
448 do_list_queue_size = 1024;
449 do_list_queue = malloc(do_list_queue_size);
450 if (do_list_queue == 0) {
451 d_printf("malloc fail for size %d\n",
452 (int)do_list_queue_size);
453 reset_do_list_queue();
455 memset(do_list_queue, 0, do_list_queue_size);
459 static void adjust_do_list_queue(void)
462 * If the starting point of the queue is more than half way through,
463 * move everything toward the beginning.
465 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
467 DEBUG(4,("do_list_queue is empty\n"));
468 do_list_queue_start = do_list_queue_end = 0;
469 *do_list_queue = '\0';
471 else if (do_list_queue_start > (do_list_queue_size / 2))
473 DEBUG(4,("sliding do_list_queue backward\n"));
474 memmove(do_list_queue,
475 do_list_queue + do_list_queue_start,
476 do_list_queue_end - do_list_queue_start);
477 do_list_queue_end -= do_list_queue_start;
478 do_list_queue_start = 0;
483 static void add_to_do_list_queue(const char* entry)
486 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
487 while (new_end > do_list_queue_size)
489 do_list_queue_size *= 2;
490 DEBUG(4,("enlarging do_list_queue to %d\n",
491 (int)do_list_queue_size));
492 dlq = Realloc(do_list_queue, do_list_queue_size);
494 d_printf("failure enlarging do_list_queue to %d bytes\n",
495 (int)do_list_queue_size);
496 reset_do_list_queue();
501 memset(do_list_queue + do_list_queue_size / 2,
502 0, do_list_queue_size / 2);
507 safe_strcpy(do_list_queue + do_list_queue_end, entry,
508 do_list_queue_size - do_list_queue_end - 1);
509 do_list_queue_end = new_end;
510 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
511 entry, (int)do_list_queue_start, (int)do_list_queue_end));
515 static char *do_list_queue_head(void)
517 return do_list_queue + do_list_queue_start;
520 static void remove_do_list_queue_head(void)
522 if (do_list_queue_end > do_list_queue_start)
524 do_list_queue_start += strlen(do_list_queue_head()) + 1;
525 adjust_do_list_queue();
526 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
527 (int)do_list_queue_start, (int)do_list_queue_end));
531 static int do_list_queue_empty(void)
533 return (! (do_list_queue && *do_list_queue));
536 /****************************************************************************
538 ****************************************************************************/
539 static void do_list_helper(file_info *f, const char *mask, void *state)
541 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
542 if (do_list_dirs && do_this_one(f)) {
545 if (do_list_recurse &&
546 !strequal(f->name,".") &&
547 !strequal(f->name,"..")) {
551 pstrcpy(mask2, mask);
552 p = strrchr_m(mask2,'\\');
555 pstrcat(mask2, f->name);
556 pstrcat(mask2,"\\*");
557 add_to_do_list_queue(mask2);
562 if (do_this_one(f)) {
568 /****************************************************************************
569 a wrapper around cli_list that adds recursion
570 ****************************************************************************/
571 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
573 static int in_do_list = 0;
575 if (in_do_list && rec)
577 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
583 do_list_recurse = rec;
589 init_do_list_queue();
590 add_to_do_list_queue(mask);
592 while (! do_list_queue_empty())
595 * Need to copy head so that it doesn't become
596 * invalid inside the call to cli_list. This
597 * would happen if the list were expanded
599 * Fix from E. Jay Berkenbilt (ejb@ql.org)
602 pstrcpy(head, do_list_queue_head());
603 cli_list(cli, head, attribute, do_list_helper, NULL);
604 remove_do_list_queue_head();
605 if ((! do_list_queue_empty()) && (fn == display_finfo))
607 char* next_file = do_list_queue_head();
609 if ((strlen(next_file) >= 2) &&
610 (next_file[strlen(next_file) - 1] == '*') &&
611 (next_file[strlen(next_file) - 2] == '\\'))
613 save_ch = next_file +
614 strlen(next_file) - 2;
617 d_printf("\n%s\n",next_file);
627 if (cli_list(cli, mask, attribute, do_list_helper, NULL) == -1)
629 d_printf("%s listing %s\n", cli_errstr(cli), mask);
634 reset_do_list_queue();
637 /****************************************************************************
638 get a directory listing
639 ****************************************************************************/
640 static int cmd_dir(void)
642 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
649 pstrcpy(mask,cur_dir);
650 if(mask[strlen(mask)-1]!='\\')
653 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
664 do_list(mask, attribute, display_finfo, recurse, True);
668 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
674 /****************************************************************************
675 get a directory listing
676 ****************************************************************************/
677 static int cmd_du(void)
679 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
686 pstrcpy(mask,cur_dir);
687 if(mask[strlen(mask)-1]!='\\')
690 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
700 do_list(mask, attribute, do_du, recurse, True);
704 d_printf("Total number of bytes: %.0f\n", dir_total);
710 /****************************************************************************
711 get a file from rname to lname
712 ****************************************************************************/
713 static int do_get(char *rname, const char *lname, BOOL reget)
715 int handle = 0, fnum;
716 BOOL newhandle = False;
718 struct timeval tp_start;
719 int read_size = io_bufsize;
726 GetTimeOfDay(&tp_start);
732 fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
735 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
739 if(!strcmp(lname,"-")) {
740 handle = fileno(stdout);
743 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
745 start = sys_lseek(handle, 0, SEEK_END);
747 d_printf("Error seeking local file\n");
752 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
757 d_printf("Error opening local file %s\n",lname);
762 if (!cli_qfileinfo(cli, fnum,
763 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
764 !cli_getattrE(cli, fnum,
765 &attr, &size, NULL, NULL, NULL)) {
766 d_printf("getattrib: %s\n",cli_errstr(cli));
770 DEBUG(2,("getting file %s of size %.0f as %s ",
771 rname, (double)size, lname));
773 if(!(data = (char *)malloc(read_size))) {
774 d_printf("malloc fail for size %d\n", read_size);
775 cli_close(cli, fnum);
780 int n = cli_read(cli, fnum, data, nread + start, read_size);
784 if (writefile(handle,data, n) != n) {
785 d_printf("Error writing local file\n");
793 if (nread + start < size) {
794 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
795 rname, (long)nread));
802 if (!cli_close(cli, fnum)) {
803 d_printf("Error %s closing remote file\n",cli_errstr(cli));
811 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
812 cli_setatr(cli, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
816 struct timeval tp_end;
819 GetTimeOfDay(&tp_end);
821 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
822 (tp_end.tv_usec - tp_start.tv_usec)/1000;
823 get_total_time_ms += this_time;
824 get_total_size += nread;
826 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
827 nread / (1.024*this_time + 1.0e-4),
828 get_total_size / (1.024*get_total_time_ms)));
835 /****************************************************************************
837 ****************************************************************************/
838 static int cmd_get(void)
844 pstrcpy(rname,cur_dir);
847 p = rname + strlen(rname);
849 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
850 d_printf("get <filename>\n");
854 dos_clean_name(rname);
856 next_token_nr(NULL,lname,NULL,sizeof(lname));
858 return do_get(rname, lname, False);
862 /****************************************************************************
863 do a mget operation on one file
864 ****************************************************************************/
865 static void do_mget(file_info *finfo)
869 pstring saved_curdir;
872 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
876 d_printf("mget aborted\n");
880 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY)
881 slprintf(quest,sizeof(pstring)-1,
882 "Get directory %s? ",finfo->name);
884 slprintf(quest,sizeof(pstring)-1,
885 "Get file %s? ",finfo->name);
887 if (prompt && !yesno(quest)) return;
889 if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
890 pstrcpy(rname,cur_dir);
891 pstrcat(rname,finfo->name);
892 do_get(rname, finfo->name, False);
896 /* handle directories */
897 pstrcpy(saved_curdir,cur_dir);
899 pstrcat(cur_dir,finfo->name);
900 pstrcat(cur_dir,"\\");
902 unix_format(finfo->name);
904 strlower(finfo->name);
906 if (!directory_exist(finfo->name,NULL) &&
907 mkdir(finfo->name,0777) != 0) {
908 d_printf("failed to create directory %s\n",finfo->name);
909 pstrcpy(cur_dir,saved_curdir);
913 if (chdir(finfo->name) != 0) {
914 d_printf("failed to chdir to directory %s\n",finfo->name);
915 pstrcpy(cur_dir,saved_curdir);
919 pstrcpy(mget_mask,cur_dir);
920 pstrcat(mget_mask,"*");
922 do_list(mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,False, True);
924 pstrcpy(cur_dir,saved_curdir);
928 /****************************************************************************
929 view the file using the pager
930 ****************************************************************************/
931 static int cmd_more(void)
933 fstring rname,lname,pager_cmd;
938 fstrcpy(rname,cur_dir);
941 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
942 fd = smb_mkstemp(lname);
944 d_printf("failed to create temporary file for more\n");
949 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
950 d_printf("more <filename>\n");
954 dos_clean_name(rname);
956 rc = do_get(rname, lname, False);
958 pager=getenv("PAGER");
960 slprintf(pager_cmd,sizeof(pager_cmd)-1,
961 "%s %s",(pager? pager:PAGER), lname);
970 /****************************************************************************
972 ****************************************************************************/
973 static int cmd_mget(void)
975 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
983 attribute |= FILE_ATTRIBUTE_DIRECTORY;
987 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
988 pstrcpy(mget_mask,cur_dir);
989 if(mget_mask[strlen(mget_mask)-1]!='\\')
990 pstrcat(mget_mask,"\\");
993 pstrcpy(mget_mask,p);
995 pstrcat(mget_mask,p);
996 do_list(mget_mask, attribute,do_mget,False,True);
1000 pstrcpy(mget_mask,cur_dir);
1001 if(mget_mask[strlen(mget_mask)-1]!='\\')
1002 pstrcat(mget_mask,"\\");
1003 pstrcat(mget_mask,"*");
1004 do_list(mget_mask, attribute,do_mget,False,True);
1011 /****************************************************************************
1012 make a directory of name "name"
1013 ****************************************************************************/
1014 static BOOL do_mkdir(char *name)
1016 if (!cli_mkdir(cli, name)) {
1017 d_printf("%s making remote directory %s\n",
1018 cli_errstr(cli),name);
1025 /****************************************************************************
1026 show 8.3 name of a file
1027 ****************************************************************************/
1028 static BOOL do_altname(char *name)
1030 const char *altname;
1031 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, &altname))) {
1032 d_printf("%s getting alt name for %s\n",
1033 cli_errstr(cli),name);
1036 d_printf("%s\n", altname);
1042 /****************************************************************************
1044 ****************************************************************************/
1045 static int cmd_quit(void)
1054 /****************************************************************************
1056 ****************************************************************************/
1057 static int cmd_mkdir(void)
1063 pstrcpy(mask,cur_dir);
1065 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1067 d_printf("mkdir <dirname>\n");
1078 trim_string(ddir,".",NULL);
1079 p = strtok(ddir,"/\\");
1082 if (!cli_chkpath(cli, ddir2)) {
1085 pstrcat(ddir2,"\\");
1086 p = strtok(NULL,"/\\");
1096 /****************************************************************************
1098 ****************************************************************************/
1099 static int cmd_altname(void)
1105 pstrcpy(name,cur_dir);
1107 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1108 d_printf("altname <file>\n");
1119 /****************************************************************************
1121 ****************************************************************************/
1122 static int do_put(char *rname, char *lname, BOOL reput)
1129 int maxwrite = io_bufsize;
1132 struct timeval tp_start;
1133 GetTimeOfDay(&tp_start);
1136 fnum = cli_open(cli, rname, O_RDWR|O_CREAT, DENY_NONE);
1138 if (!cli_qfileinfo(cli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1139 !cli_getattrE(cli, fnum, NULL, &start, NULL, NULL, NULL)) {
1140 d_printf("getattrib: %s\n",cli_errstr(cli));
1145 fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1149 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
1153 /* allow files to be piped into smbclient
1156 Note that in this case this function will exit(0) rather
1158 if (!strcmp(lname, "-")) {
1160 /* size of file is not known */
1162 f = x_fopen(lname,O_RDONLY, 0);
1164 if (x_tseek(f, start, SEEK_SET) == -1) {
1165 d_printf("Error seeking local file\n");
1172 d_printf("Error opening local file %s\n",lname);
1177 DEBUG(1,("putting file %s as %s ",lname,
1180 buf = (char *)malloc(maxwrite);
1182 d_printf("ERROR: Not enough memory!\n");
1185 while (!x_feof(f)) {
1189 if ((n = readfile(buf,n,f)) < 1) {
1190 if((n == 0) && x_feof(f))
1191 break; /* Empty local file. */
1193 d_printf("Error reading local file: %s\n", strerror(errno));
1198 ret = cli_write(cli, fnum, 0, buf, nread + start, n);
1201 d_printf("Error writing file: %s\n", cli_errstr(cli));
1209 if (!cli_close(cli, fnum)) {
1210 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1224 struct timeval tp_end;
1227 GetTimeOfDay(&tp_end);
1229 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1230 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1231 put_total_time_ms += this_time;
1232 put_total_size += nread;
1234 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1235 nread / (1.024*this_time + 1.0e-4),
1236 put_total_size / (1.024*put_total_time_ms)));
1249 /****************************************************************************
1251 ****************************************************************************/
1252 static int cmd_put(void)
1259 pstrcpy(rname,cur_dir);
1260 pstrcat(rname,"\\");
1262 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1263 d_printf("put <filename>\n");
1268 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1271 pstrcat(rname,lname);
1273 dos_clean_name(rname);
1277 /* allow '-' to represent stdin
1278 jdblair, 24.jun.98 */
1279 if (!file_exist(lname,&st) &&
1280 (strcmp(lname,"-"))) {
1281 d_printf("%s does not exist\n",lname);
1286 return do_put(rname, lname, False);
1289 /*************************************
1291 *************************************/
1293 static struct file_list {
1294 struct file_list *prev, *next;
1299 /****************************************************************************
1300 Free a file_list structure
1301 ****************************************************************************/
1303 static void free_file_list (struct file_list * list)
1305 struct file_list *tmp;
1310 DLIST_REMOVE(list, list);
1311 SAFE_FREE(tmp->file_path);
1316 /****************************************************************************
1317 seek in a directory/file list until you get something that doesn't start with
1319 ****************************************************************************/
1320 static BOOL seek_list(struct file_list *list, char *name)
1323 trim_string(list->file_path,"./","\n");
1324 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1333 /****************************************************************************
1334 set the file selection mask
1335 ****************************************************************************/
1336 static int cmd_select(void)
1338 pstrcpy(fileselection,"");
1339 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1344 /****************************************************************************
1345 Recursive file matching function act as find
1346 match must be always set to True when calling this function
1347 ****************************************************************************/
1348 static int file_find(struct file_list **list, const char *directory,
1349 const char *expression, BOOL match)
1352 struct file_list *entry;
1353 struct stat statbuf;
1359 dir = opendir(directory);
1360 if (!dir) return -1;
1362 while ((dname = readdirname(dir))) {
1363 if (!strcmp("..", dname)) continue;
1364 if (!strcmp(".", dname)) continue;
1366 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1371 if (!match || !gen_fnmatch(expression, dname)) {
1373 ret = stat(path, &statbuf);
1375 if (S_ISDIR(statbuf.st_mode)) {
1377 ret = file_find(list, path, expression, False);
1380 d_printf("file_find: cannot stat file %s\n", path);
1389 entry = (struct file_list *) malloc(sizeof (struct file_list));
1391 d_printf("Out of memory in file_find\n");
1395 entry->file_path = path;
1396 entry->isdir = isdir;
1397 DLIST_ADD(*list, entry);
1407 /****************************************************************************
1409 ****************************************************************************/
1410 static int cmd_mput(void)
1415 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1417 struct file_list *temp_list;
1418 char *quest, *lname, *rname;
1422 ret = file_find(&file_list, ".", p, True);
1424 free_file_list(file_list);
1432 for (temp_list = file_list; temp_list;
1433 temp_list = temp_list->next) {
1436 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1438 trim_string(lname, "./", "/");
1440 /* check if it's a directory */
1441 if (temp_list->isdir) {
1442 /* if (!recurse) continue; */
1445 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1446 if (prompt && !yesno(quest)) { /* No */
1447 /* Skip the directory */
1448 lname[strlen(lname)-1] = '/';
1449 if (!seek_list(temp_list, lname))
1453 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1455 if (!cli_chkpath(cli, rname) &&
1457 DEBUG (0, ("Unable to make dir, skipping..."));
1458 /* Skip the directory */
1459 lname[strlen(lname)-1] = '/';
1460 if (!seek_list(temp_list, lname))
1467 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1468 if (prompt && !yesno(quest)) /* No */
1473 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1478 do_put(rname, lname, False);
1480 free_file_list(file_list);
1490 /****************************************************************************
1492 ****************************************************************************/
1493 static int do_cancel(int job)
1495 d_printf("REWRITE: print job cancel not implemented\n");
1500 /****************************************************************************
1502 ****************************************************************************/
1503 static int cmd_cancel(void)
1508 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1509 d_printf("cancel <jobid> ...\n");
1515 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1521 /****************************************************************************
1523 ****************************************************************************/
1524 static int cmd_print(void)
1530 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1531 d_printf("print <filename>\n");
1535 pstrcpy(rname,lname);
1536 p = strrchr_m(rname,'/');
1538 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1541 if (strequal(lname,"-")) {
1542 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1545 return do_put(rname, lname, False);
1549 /****************************************************************************
1551 ****************************************************************************/
1552 static int cmd_queue(void)
1554 d_printf("REWRITE: print job queue not implemented\n");
1559 /****************************************************************************
1561 ****************************************************************************/
1562 static void do_del(file_info *finfo)
1566 pstrcpy(mask,cur_dir);
1567 pstrcat(mask,finfo->name);
1569 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY)
1572 if (!cli_unlink(cli, mask)) {
1573 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1577 /****************************************************************************
1579 ****************************************************************************/
1580 static int cmd_del(void)
1584 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1587 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1589 pstrcpy(mask,cur_dir);
1591 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1592 d_printf("del <filename>\n");
1597 do_list(mask, attribute,do_del,False,False);
1603 /****************************************************************************
1604 delete a whole directory tree
1605 ****************************************************************************/
1606 static int cmd_deltree(void)
1612 pstrcpy(dname,cur_dir);
1614 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1615 d_printf("deltree <dirname>\n");
1620 ret = cli_deltree(cli, dname);
1623 printf("Failed to delete tree %s - %s\n", dname, cli_errstr(cli));
1627 printf("Deleted %d files in %s\n", ret, dname);
1633 /****************************************************************************
1634 show as much information as possible about a file
1635 ****************************************************************************/
1636 static int cmd_allinfo(void)
1641 TALLOC_CTX *mem_ctx;
1642 union smb_fileinfo finfo;
1645 pstrcpy(fname,cur_dir);
1647 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1648 d_printf("allinfo <filename>\n");
1653 mem_ctx = talloc_init(fname);
1655 /* first a ALL_INFO QPATHINFO */
1656 finfo.generic.level = RAW_FILEINFO_ALL_INFO;
1657 finfo.generic.in.fname = fname;
1658 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1659 if (!NT_STATUS_IS_OK(status)) {
1660 d_printf("%s - %s\n", fname, nt_errstr(status));
1665 d_printf("\tcreate_time: %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.create_time));
1666 d_printf("\taccess_time: %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.access_time));
1667 d_printf("\twrite_time: %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.write_time));
1668 d_printf("\tchange_time: %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.change_time));
1669 d_printf("\tattrib: 0x%x\n", finfo.all_info.out.attrib);
1670 d_printf("\talloc_size: %lu\n", (unsigned long)finfo.all_info.out.alloc_size);
1671 d_printf("\tsize: %lu\n", (unsigned long)finfo.all_info.out.size);
1672 d_printf("\tnlink: %u\n", finfo.all_info.out.nlink);
1673 d_printf("\tdelete_pending: %u\n", finfo.all_info.out.delete_pending);
1674 d_printf("\tdirectory: %u\n", finfo.all_info.out.directory);
1675 d_printf("\tea_size: %u\n", finfo.all_info.out.ea_size);
1676 d_printf("\tfname: '%s'\n", finfo.all_info.out.fname.s);
1678 /* 8.3 name if any */
1679 finfo.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
1680 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1681 if (NT_STATUS_IS_OK(status)) {
1682 d_printf("\talt_name: %s\n", finfo.alt_name_info.out.fname.s);
1685 /* file_id if available */
1686 finfo.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
1687 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1688 if (NT_STATUS_IS_OK(status)) {
1689 d_printf("\tfile_id %.0f\n", finfo.internal_information.out.file_id);
1692 /* the EAs, if any */
1693 finfo.generic.level = RAW_FILEINFO_ALL_EAS;
1694 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1695 if (NT_STATUS_IS_OK(status)) {
1697 for (i=0;i<finfo.all_eas.out.num_eas;i++) {
1698 d_printf("\tEA[%d] flags=%d %s=%*.*s\n", i,
1699 finfo.all_eas.out.eas[i].flags,
1700 finfo.all_eas.out.eas[i].name.s,
1701 finfo.all_eas.out.eas[i].value.length,
1702 finfo.all_eas.out.eas[i].value.length,
1703 finfo.all_eas.out.eas[i].value.data);
1707 /* streams, if available */
1708 finfo.generic.level = RAW_FILEINFO_STREAM_INFO;
1709 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1710 if (NT_STATUS_IS_OK(status)) {
1712 for (i=0;i<finfo.stream_info.out.num_streams;i++) {
1713 d_printf("\tstream %d:\n", i);
1714 d_printf("\t\tsize %ld\n",
1715 (long)finfo.stream_info.out.streams[i].size);
1716 d_printf("\t\talloc size %ld\n",
1717 (long)finfo.stream_info.out.streams[i].alloc_size);
1718 d_printf("\t\tname %s\n", finfo.stream_info.out.streams[i].stream_name.s);
1722 /* dev/inode if available */
1723 finfo.generic.level = RAW_FILEINFO_COMPRESSION_INFORMATION;
1724 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1725 if (NT_STATUS_IS_OK(status)) {
1726 d_printf("\tcompressed size %ld\n", (long)finfo.compression_info.out.compressed_size);
1727 d_printf("\tformat %ld\n", (long)finfo.compression_info.out.format);
1728 d_printf("\tunit_shift %ld\n", (long)finfo.compression_info.out.unit_shift);
1729 d_printf("\tchunk_shift %ld\n", (long)finfo.compression_info.out.chunk_shift);
1730 d_printf("\tcluster_shift %ld\n", (long)finfo.compression_info.out.cluster_shift);
1733 talloc_destroy(mem_ctx);
1740 /****************************************************************************
1741 show any ACL on a file
1742 ****************************************************************************/
1743 static int cmd_acl(void)
1748 TALLOC_CTX *mem_ctx;
1749 struct smb_query_secdesc query;
1753 pstrcpy(fname,cur_dir);
1755 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1756 d_printf("acl <filename>\n");
1761 fnum = cli_open(cli, fname, O_RDONLY, DENY_NONE);
1763 d_printf("%s - %s\n", fname, cli_errstr(cli));
1767 mem_ctx = talloc_init(fname);
1769 query.in.fnum = fnum;
1770 query.in.secinfo_flags = 0x7;
1772 status = smb_raw_query_secdesc(cli->tree, mem_ctx, &query);
1773 if (!NT_STATUS_IS_OK(status)) {
1774 d_printf("%s - %s\n", fname, nt_errstr(status));
1779 talloc_destroy(mem_ctx);
1786 /****************************************************************************
1787 ****************************************************************************/
1788 static int cmd_open(void)
1793 pstrcpy(mask,cur_dir);
1795 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1796 d_printf("open <filename>\n");
1801 cli_open(cli, mask, O_RDWR, DENY_ALL);
1807 /****************************************************************************
1809 ****************************************************************************/
1810 static int cmd_rmdir(void)
1815 pstrcpy(mask,cur_dir);
1817 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1818 d_printf("rmdir <dirname>\n");
1823 if (!cli_rmdir(cli, mask)) {
1824 d_printf("%s removing remote directory file %s\n",
1825 cli_errstr(cli),mask);
1831 /****************************************************************************
1833 ****************************************************************************/
1834 static int cmd_link(void)
1839 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1840 d_printf("Server doesn't support UNIX CIFS calls.\n");
1844 pstrcpy(src,cur_dir);
1845 pstrcpy(dest,cur_dir);
1847 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1848 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1849 d_printf("link <src> <dest>\n");
1856 if (!cli_unix_hardlink(cli, src, dest)) {
1857 d_printf("%s linking files (%s -> %s)\n", cli_errstr(cli), src, dest);
1864 /****************************************************************************
1866 ****************************************************************************/
1868 static int cmd_symlink(void)
1873 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1874 d_printf("Server doesn't support UNIX CIFS calls.\n");
1878 pstrcpy(src,cur_dir);
1879 pstrcpy(dest,cur_dir);
1881 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1882 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1883 d_printf("symlink <src> <dest>\n");
1890 if (!cli_unix_symlink(cli, src, dest)) {
1891 d_printf("%s symlinking files (%s -> %s)\n",
1892 cli_errstr(cli), src, dest);
1899 /****************************************************************************
1901 ****************************************************************************/
1903 static int cmd_chmod(void)
1909 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1910 d_printf("Server doesn't support UNIX CIFS calls.\n");
1914 pstrcpy(src,cur_dir);
1916 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1917 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1918 d_printf("chmod mode file\n");
1922 mode = (mode_t)strtol(buf, NULL, 8);
1925 if (!cli_unix_chmod(cli, src, mode)) {
1926 d_printf("%s chmod file %s 0%o\n",
1927 cli_errstr(cli), src, (unsigned int)mode);
1934 /****************************************************************************
1936 ****************************************************************************/
1938 static int cmd_chown(void)
1943 fstring buf, buf2, buf3;
1945 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1946 d_printf("Server doesn't support UNIX CIFS calls.\n");
1950 pstrcpy(src,cur_dir);
1952 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1953 !next_token(NULL,buf2,NULL, sizeof(buf2)) ||
1954 !next_token(NULL,buf3,NULL, sizeof(buf3))) {
1955 d_printf("chown uid gid file\n");
1959 uid = (uid_t)atoi(buf);
1960 gid = (gid_t)atoi(buf2);
1963 if (!cli_unix_chown(cli, src, uid, gid)) {
1964 d_printf("%s chown file %s uid=%d, gid=%d\n",
1965 cli_errstr(cli), src, (int)uid, (int)gid);
1972 /****************************************************************************
1974 ****************************************************************************/
1975 static int cmd_rename(void)
1980 pstrcpy(src,cur_dir);
1981 pstrcpy(dest,cur_dir);
1983 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1984 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1985 d_printf("rename <src> <dest>\n");
1992 if (!cli_rename(cli, src, dest)) {
1993 d_printf("%s renaming files\n",cli_errstr(cli));
2001 /****************************************************************************
2002 toggle the prompt flag
2003 ****************************************************************************/
2004 static int cmd_prompt(void)
2007 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2013 /****************************************************************************
2014 set the newer than time
2015 ****************************************************************************/
2016 static int cmd_newer(void)
2020 SMB_STRUCT_STAT sbuf;
2022 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2023 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2024 newer_than = sbuf.st_mtime;
2025 DEBUG(1,("Getting files newer than %s",
2026 asctime(LocalTime(&newer_than))));
2031 if (ok && newer_than == 0) {
2032 d_printf("Error setting newer-than time\n");
2039 /****************************************************************************
2040 set the archive level
2041 ****************************************************************************/
2042 static int cmd_archive(void)
2046 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2047 archive_level = atoi(buf);
2049 d_printf("Archive level is %d\n",archive_level);
2054 /****************************************************************************
2055 toggle the lowercaseflag
2056 ****************************************************************************/
2057 static int cmd_lowercase(void)
2059 lowercase = !lowercase;
2060 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2068 /****************************************************************************
2069 toggle the recurse flag
2070 ****************************************************************************/
2071 static int cmd_recurse(void)
2074 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2079 /****************************************************************************
2080 toggle the translate flag
2081 ****************************************************************************/
2082 static int cmd_translate(void)
2084 translation = !translation;
2085 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2086 translation?"on":"off"));
2092 /****************************************************************************
2093 do a printmode command
2094 ****************************************************************************/
2095 static int cmd_printmode(void)
2100 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2101 if (strequal(buf,"text")) {
2104 if (strequal(buf,"graphics"))
2107 printmode = atoi(buf);
2114 fstrcpy(mode,"text");
2117 fstrcpy(mode,"graphics");
2120 slprintf(mode,sizeof(mode)-1,"%d",printmode);
2124 DEBUG(2,("the printmode is now %s\n",mode));
2129 /****************************************************************************
2131 ****************************************************************************/
2132 static int cmd_lcd(void)
2137 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2139 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2144 /****************************************************************************
2145 get a file restarting at end of local file
2146 ****************************************************************************/
2147 static int cmd_reget(void)
2150 pstring remote_name;
2153 pstrcpy(remote_name, cur_dir);
2154 pstrcat(remote_name, "\\");
2156 p = remote_name + strlen(remote_name);
2158 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2159 d_printf("reget <filename>\n");
2162 pstrcpy(local_name, p);
2163 dos_clean_name(remote_name);
2165 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2167 return do_get(remote_name, local_name, True);
2170 /****************************************************************************
2171 put a file restarting at end of local file
2172 ****************************************************************************/
2173 static int cmd_reput(void)
2176 pstring remote_name;
2181 pstrcpy(remote_name, cur_dir);
2182 pstrcat(remote_name, "\\");
2184 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2185 d_printf("reput <filename>\n");
2188 pstrcpy(local_name, p);
2190 if (!file_exist(local_name, &st)) {
2191 d_printf("%s does not exist\n", local_name);
2195 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2196 pstrcat(remote_name, p);
2198 pstrcat(remote_name, local_name);
2200 dos_clean_name(remote_name);
2202 return do_put(remote_name, local_name, True);
2206 /****************************************************************************
2207 try and browse available connections on a host
2208 ****************************************************************************/
2209 static BOOL browse_host(BOOL sort)
2211 d_printf("REWRITE: host browsing not implemented\n");
2216 /****************************************************************************
2217 try and browse available connections on a host
2218 ****************************************************************************/
2219 static BOOL list_servers(char *wk_grp)
2221 d_printf("REWRITE: list servers not implemented\n");
2225 /* Some constants for completing filename arguments */
2227 #define COMPL_NONE 0 /* No completions */
2228 #define COMPL_REMOTE 1 /* Complete remote filename */
2229 #define COMPL_LOCAL 2 /* Complete local filename */
2231 /* This defines the commands supported by this client.
2232 * NOTE: The "!" must be the last one in the list because it's fn pointer
2233 * field is NULL, and NULL in that field is used in process_tok()
2234 * (below) to indicate the end of the list. crh
2240 const char *description;
2241 char compl_args[2]; /* Completion argument info */
2244 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2245 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2246 {"acl",cmd_acl,"<file> show file ACL",{COMPL_NONE,COMPL_NONE}},
2247 {"allinfo",cmd_allinfo,"<file> show all possible info about a file",{COMPL_NONE,COMPL_NONE}},
2248 {"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}},
2249 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2250 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2251 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2252 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2253 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2254 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2255 {"deltree",cmd_deltree,"<dir> delete a whole directory tree",{COMPL_REMOTE,COMPL_NONE}},
2256 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2257 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2258 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2259 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2260 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2261 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2262 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2263 {"link",cmd_link,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2264 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2265 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2266 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2267 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2268 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2269 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2270 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
2271 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2272 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2273 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2274 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2275 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
2276 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2277 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2278 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2279 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2280 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2281 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2282 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2283 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2284 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2285 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2286 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2287 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2288 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2289 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
2290 {"symlink",cmd_symlink,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2291 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2292 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2293 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2295 /* Yes, this must be here, see crh's comment above. */
2296 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2297 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2301 /*******************************************************************
2302 lookup a command string in the list of commands, including
2304 ******************************************************************/
2305 static int process_tok(fstring tok)
2307 int i = 0, matches = 0;
2309 int tok_len = strlen(tok);
2311 while (commands[i].fn != NULL) {
2312 if (strequal(commands[i].name,tok)) {
2316 } else if (strnequal(commands[i].name, tok, tok_len)) {
2325 else if (matches == 1)
2331 /****************************************************************************
2333 ****************************************************************************/
2334 static int cmd_help(void)
2339 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2340 if ((i = process_tok(buf)) >= 0)
2341 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2343 while (commands[i].description) {
2344 for (j=0; commands[i].description && (j<5); j++) {
2345 d_printf("%-15s",commands[i].name);
2354 /****************************************************************************
2355 process a -c command string
2356 ****************************************************************************/
2357 static int process_command_string(char *cmd)
2363 /* establish the connection if not already */
2366 cli = do_connect(desthost, service);
2371 while (cmd[0] != '\0') {
2376 if ((p = strchr_m(cmd, ';')) == 0) {
2377 strncpy(line, cmd, 999);
2381 if (p - cmd > 999) p = cmd + 999;
2382 strncpy(line, cmd, p - cmd);
2383 line[p - cmd] = '\0';
2387 /* and get the first part of the command */
2389 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2391 if ((i = process_tok(tok)) >= 0) {
2392 rc = commands[i].fn();
2393 } else if (i == -2) {
2394 d_printf("%s: command abbreviation ambiguous\n",tok);
2396 d_printf("%s: command not found\n",tok);
2403 #define MAX_COMPLETIONS 100
2411 } completion_remote_t;
2413 static void completion_remote_filter(file_info *f, const char *mask, void *state)
2415 completion_remote_t *info = (completion_remote_t *)state;
2417 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2418 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
2419 info->matches[info->count] = strdup(f->name);
2423 if (info->dirmask[0] != 0)
2424 pstrcpy(tmp, info->dirmask);
2427 pstrcat(tmp, f->name);
2428 if (f->mode & FILE_ATTRIBUTE_DIRECTORY)
2430 info->matches[info->count] = strdup(tmp);
2432 if (info->matches[info->count] == NULL)
2434 if (f->mode & FILE_ATTRIBUTE_DIRECTORY)
2435 smb_readline_ca_char(0);
2437 if (info->count == 1)
2438 info->samelen = strlen(info->matches[info->count]);
2440 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
2446 static char **remote_completion(const char *text, int len)
2450 completion_remote_t info = { "", NULL, 1, NULL, NULL, NULL };
2456 if (len >= PATH_MAX)
2459 info.matches = (char **)malloc(sizeof(info.matches[0])*MAX_COMPLETIONS);
2460 if (!info.matches) return NULL;
2461 info.matches[0] = NULL;
2463 for (i = len-1; i >= 0; i--)
2464 if ((text[i] == '/') || (text[i] == '\\'))
2466 info.text = text+i+1;
2467 info.samelen = info.len = len-i-1;
2470 strncpy(info.dirmask, text, i+1);
2471 info.dirmask[i+1] = 0;
2472 snprintf(dirmask, sizeof(dirmask), "%s%*s*", cur_dir, i-1, text);
2474 snprintf(dirmask, sizeof(dirmask), "%s*", cur_dir);
2476 if (cli_list(cli, dirmask,
2477 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
2478 completion_remote_filter, &info) < 0)
2481 if (info.count == 2)
2482 info.matches[0] = strdup(info.matches[1]);
2484 info.matches[0] = malloc(info.samelen+1);
2485 if (!info.matches[0])
2487 strncpy(info.matches[0], info.matches[1], info.samelen);
2488 info.matches[0][info.samelen] = 0;
2490 info.matches[info.count] = NULL;
2491 return info.matches;
2494 for (i = 0; i < info.count; i++)
2495 free(info.matches[i]);
2500 static char **completion_fn(const char *text, int start, int end)
2502 smb_readline_ca_char(' ');
2505 const char *buf, *sp;
2509 buf = smb_readline_get_line_buffer();
2513 sp = strchr(buf, ' ');
2517 for (i = 0; commands[i].name; i++)
2518 if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
2520 if (commands[i].name == NULL)
2526 if (sp == (buf + start))
2527 compl_type = commands[i].compl_args[0];
2529 compl_type = commands[i].compl_args[1];
2531 if (compl_type == COMPL_REMOTE)
2532 return remote_completion(text, end - start);
2533 else /* fall back to local filename completion */
2537 int i, len, samelen, count=1;
2539 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
2540 if (!matches) return NULL;
2544 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
2545 if (strncmp(text, commands[i].name, len) == 0) {
2546 matches[count] = strdup(commands[i].name);
2547 if (!matches[count])
2550 samelen = strlen(matches[count]);
2552 while (strncmp(matches[count], matches[count-1], samelen) != 0)
2559 case 0: /* should never happen */
2563 matches[0] = strdup(matches[1]);
2566 matches[0] = malloc(samelen+1);
2569 strncpy(matches[0], matches[1], samelen);
2570 matches[0][samelen] = 0;
2572 matches[count] = NULL;
2585 /****************************************************************************
2586 make sure we swallow keepalives during idle time
2587 ****************************************************************************/
2588 static void readline_callback(void)
2591 struct timeval timeout;
2592 static time_t last_t;
2597 if (t - last_t < 5) return;
2602 if (cli->transport->socket->fd == -1)
2606 FD_SET(cli->transport->socket->fd, &fds);
2609 timeout.tv_usec = 0;
2610 sys_select_intr(cli->transport->socket->fd+1,&fds,NULL,NULL,&timeout);
2612 /* We deliberately use cli_swallow_keepalives instead of
2613 client_receive_smb as we want to receive
2614 session keepalives and then drop them here.
2616 if (FD_ISSET(cli->transport->socket->fd, &fds)) {
2617 if (!cli_request_receive_next(cli->transport)) {
2618 d_printf("Lost connection to server\n");
2625 cli_chkpath(cli, "\\");
2630 /****************************************************************************
2631 process commands on stdin
2632 ****************************************************************************/
2633 static void process_stdin(void)
2644 /* display a prompt */
2645 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
2646 cline = smb_readline(the_prompt, readline_callback, completion_fn);
2650 pstrcpy(line, cline);
2652 /* special case - first char is ! */
2658 /* and get the first part of the command */
2660 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2662 if ((i = process_tok(tok)) >= 0) {
2664 } else if (i == -2) {
2665 d_printf("%s: command abbreviation ambiguous\n",tok);
2667 d_printf("%s: command not found\n",tok);
2673 /*****************************************************
2674 return a connection to a server
2675 *******************************************************/
2676 static struct cli_state *do_connect(const char *server, const char *share)
2678 struct cli_state *c;
2679 struct nmb_name called, calling;
2680 const char *server_n;
2682 fstring servicename;
2685 /* make a copy so we don't modify the global string 'service' */
2686 fstrcpy(servicename, share);
2687 sharename = servicename;
2688 if (*sharename == '\\') {
2689 server = sharename+2;
2690 sharename = strchr_m(server,'\\');
2691 if (!sharename) return NULL;
2696 asprintf(&sharename, "\\\\%s\\%s", server, sharename);
2702 make_nmb_name(&calling, lp_netbios_name(), 0x0);
2703 make_nmb_name(&called , server, name_type);
2707 if (have_ip) ip = dest_ip;
2709 /* have to open a new connection */
2710 if (!(c=cli_state_init()) || !cli_socket_connect(c, server_n, &ip)) {
2711 d_printf("Connection to %s failed\n", server_n);
2715 if (!cli_transport_establish(c, &calling, &called)) {
2717 d_printf("session request to %s failed (%s)\n",
2718 called.name, cli_errstr(c));
2720 if ((p=strchr_m(called.name, '.'))) {
2724 if (strcmp(called.name, "*SMBSERVER")) {
2725 make_nmb_name(&called , "*SMBSERVER", 0x20);
2731 DEBUG(4,(" session request ok\n"));
2733 if (!cli_negprot(c)) {
2734 d_printf("protocol negotiation failed\n");
2740 const char *pass = getpass("Password: ");
2742 pstrcpy(password, pass);
2746 if (!cli_session_setup(c, username, password,
2748 /* if a password was not supplied then try again with a null username */
2749 if (password[0] || !username[0] || use_kerberos ||
2750 !cli_session_setup(c, "", "", lp_workgroup())) {
2751 d_printf("session setup failed: %s\n", cli_errstr(c));
2755 d_printf("Anonymous login successful\n");
2758 DEBUG(4,(" session setup ok\n"));
2760 if (!cli_send_tconX(c, sharename, "?????", password)) {
2761 d_printf("tree connect failed: %s\n", cli_errstr(c));
2766 DEBUG(4,(" tconx ok\n"));
2772 /****************************************************************************
2773 process commands from the client
2774 ****************************************************************************/
2775 static int process(char *base_directory)
2779 cli = do_connect(desthost, service);
2784 if (*base_directory) do_cd(base_directory);
2787 rc = process_command_string(cmdstr);
2796 /****************************************************************************
2798 ****************************************************************************/
2799 static int do_host_query(char *query_host)
2801 cli = do_connect(query_host, "IPC$");
2806 list_servers(lp_workgroup());
2814 /****************************************************************************
2815 handle a tar operation
2816 ****************************************************************************/
2817 static int do_tar_op(char *base_directory)
2821 /* do we already have a connection? */
2823 cli = do_connect(desthost, service);
2830 if (*base_directory) do_cd(base_directory);
2839 /****************************************************************************
2840 handle a message operation
2841 ****************************************************************************/
2842 static int do_message_op(void)
2845 struct nmb_name called, calling;
2846 fstring server_name;
2847 char name_type_hex[10];
2849 make_nmb_name(&calling, lp_netbios_name(), 0x0);
2850 make_nmb_name(&called , desthost, name_type);
2852 fstrcpy(server_name, desthost);
2853 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
2854 fstrcat(server_name, name_type_hex);
2857 if (have_ip) ip = dest_ip;
2859 if (!(cli=cli_state_init()) || !cli_socket_connect(cli, server_name, &ip)) {
2860 d_printf("Connection to %s failed\n", desthost);
2864 if (!cli_transport_establish(cli, &calling, &called)) {
2865 d_printf("session request failed\n");
2878 * Process "-L hostname" option.
2880 * We don't actually do anything yet -- we just stash the name in a
2881 * global variable and do the query when all options have been read.
2883 static void remember_query_host(const char *arg,
2888 while (*arg == '\\' || *arg == '/')
2890 pstrcpy(query_host, arg);
2891 if ((slash = strchr(query_host, '/'))
2892 || (slash = strchr(query_host, '\\'))) {
2898 /****************************************************************************
2900 ****************************************************************************/
2901 int main(int argc,char *argv[])
2903 fstring base_directory;
2906 BOOL message = False;
2907 extern char tar_type;
2912 TALLOC_CTX *mem_ctx;
2913 struct poptOption long_options[] = {
2916 { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
2917 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
2918 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
2919 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
2920 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
2921 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
2922 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
2923 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
2924 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
2925 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
2926 { "send-buffer", 'b', POPT_ARG_INT, NULL, 'b', "Changes the transmit/send buffer", "BYTES" },
2927 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
2929 POPT_COMMON_CONNECTION
2930 POPT_COMMON_CREDENTIALS
2936 pstrcpy(term_code, KANJI);
2942 *base_directory = 0;
2944 setup_logging(argv[0],DEBUG_STDOUT);
2945 mem_ctx = talloc_init("client.c/main");
2947 d_printf("\nclient.c: Not enough memory\n");
2951 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
2952 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
2953 argv[0], dyn_CONFIGFILE);
2956 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options,
2957 POPT_CONTEXT_KEEP_FIRST);
2958 poptSetOtherOptionHelp(pc, "service <password>");
2960 in_client = True; /* Make sure that we tell lp_load we are */
2962 while ((opt = poptGetNextOpt(pc)) != -1) {
2965 /* Messages are sent to NetBIOS name type 0x3
2966 * (Messenger Service). Make sure we default
2967 * to port 139 instead of port 445. srl,crh
2970 pstrcpy(desthost,poptGetOptArg(pc));
2971 if( 0 == port ) port = 139;
2976 dest_ip = *interpret_addr2(mem_ctx, poptGetOptArg(pc));
2977 if (is_zero_ip(dest_ip))
2983 setup_logging("client", DEBUG_STDERR);
2987 remember_query_host(poptGetOptArg(pc), query_host);
2990 pstrcpy(term_code, poptGetOptArg(pc));
2993 lp_set_cmdline("max protocol", poptGetOptArg(pc));
2996 lp_set_cmdline("name resolve order", poptGetOptArg(pc));
2999 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), optind)) {
3000 poptPrintUsage(pc, stderr, 0);
3005 fstrcpy(base_directory,poptGetOptArg(pc));
3008 io_bufsize = MAX(1, atoi(poptGetOptArg(pc)));
3017 if(poptPeekArg(pc)) {
3018 pstrcpy(service,poptGetArg(pc));
3019 /* Convert any '/' characters in the service name to '\' characters */
3020 string_replace(service, '/','\\');
3022 if (count_chars(service,'\\') < 3) {
3023 d_printf("\n%s: Not enough '\\' characters in service\n",service);
3024 poptPrintUsage(pc, stderr, 0);
3029 if (poptPeekArg(pc)) {
3030 cmdline_auth_info.got_pass = True;
3031 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));
3036 if (!tar_type && !*query_host && !*service && !message) {
3037 poptPrintUsage(pc, stderr, 0);
3041 poptFreeContext(pc);
3043 pstrcpy(username, cmdline_auth_info.username);
3044 pstrcpy(password, cmdline_auth_info.password);
3045 use_kerberos = cmdline_auth_info.use_kerberos;
3046 got_pass = cmdline_auth_info.got_pass;
3048 DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION ) );
3050 talloc_destroy(mem_ctx);
3053 process_command_string(cmdstr);
3054 return do_tar_op(base_directory);
3057 if ((p=strchr_m(query_host,'#'))) {
3060 sscanf(p, "%x", &name_type);
3064 return do_host_query(query_host);
3068 return do_message_op();
3071 if (process(base_directory)) {