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->tree, desthost, username, &grp_id)) {
197 d_printf("message start: %s\n", cli_errstr(cli->tree));
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->tree, msg, l, grp_id)) {
219 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli->tree));
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->tree, grp_id)) {
232 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli->tree));
239 /****************************************************************************
240 check the space on a device
241 ****************************************************************************/
242 static int do_dskattr(void)
244 int total, bsize, avail;
246 if (NT_STATUS_IS_ERR(cli_dskattr(cli->tree, &bsize, &total, &avail))) {
247 d_printf("Error in dskattr: %s\n",cli_errstr(cli->tree));
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 (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, dname))) {
296 d_printf("cd %s: %s\n", dname, cli_errstr(cli->tree));
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->tree, 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->tree, mask, attribute, do_list_helper, NULL) == -1)
629 d_printf("%s listing %s\n", cli_errstr(cli->tree), 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->tree, rname, O_RDONLY, DENY_NONE);
735 d_printf("%s opening remote file %s\n",cli_errstr(cli->tree),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 (NT_STATUS_IS_ERR(cli_qfileinfo(cli->tree, fnum,
763 &attr, &size, NULL, NULL, NULL, NULL, NULL)) &&
764 NT_STATUS_IS_ERR(cli_getattrE(cli->tree, fnum,
765 &attr, &size, NULL, NULL, NULL))) {
766 d_printf("getattrib: %s\n",cli_errstr(cli->tree));
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->tree, fnum);
780 int n = cli_read(cli->tree, 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 (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
803 d_printf("Error %s closing remote file\n",cli_errstr(cli->tree));
811 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
812 cli_setatr(cli->tree, 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 NTSTATUS do_mkdir(char *name)
1018 if (NT_STATUS_IS_ERR(status = cli_mkdir(cli->tree, name))) {
1019 d_printf("%s making remote directory %s\n",
1020 cli_errstr(cli->tree),name);
1027 /****************************************************************************
1028 show 8.3 name of a file
1029 ****************************************************************************/
1030 static BOOL do_altname(char *name)
1032 const char *altname;
1033 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli->tree, name, &altname))) {
1034 d_printf("%s getting alt name for %s\n",
1035 cli_errstr(cli->tree),name);
1038 d_printf("%s\n", altname);
1044 /****************************************************************************
1046 ****************************************************************************/
1047 static int cmd_quit(void)
1056 /****************************************************************************
1058 ****************************************************************************/
1059 static int cmd_mkdir(void)
1065 pstrcpy(mask,cur_dir);
1067 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1069 d_printf("mkdir <dirname>\n");
1080 trim_string(ddir,".",NULL);
1081 p = strtok(ddir,"/\\");
1084 if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, ddir2))) {
1087 pstrcat(ddir2,"\\");
1088 p = strtok(NULL,"/\\");
1098 /****************************************************************************
1100 ****************************************************************************/
1101 static int cmd_altname(void)
1107 pstrcpy(name,cur_dir);
1109 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1110 d_printf("altname <file>\n");
1121 /****************************************************************************
1123 ****************************************************************************/
1124 static int do_put(char *rname, char *lname, BOOL reput)
1131 int maxwrite = io_bufsize;
1134 struct timeval tp_start;
1135 GetTimeOfDay(&tp_start);
1138 fnum = cli_open(cli->tree, rname, O_RDWR|O_CREAT, DENY_NONE);
1140 if (NT_STATUS_IS_ERR(cli_qfileinfo(cli->tree, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL)) &&
1141 NT_STATUS_IS_ERR(cli_getattrE(cli->tree, fnum, NULL, &start, NULL, NULL, NULL))) {
1142 d_printf("getattrib: %s\n",cli_errstr(cli->tree));
1147 fnum = cli_open(cli->tree, rname, O_RDWR|O_CREAT|O_TRUNC,
1152 d_printf("%s opening remote file %s\n",cli_errstr(cli->tree),rname);
1156 /* allow files to be piped into smbclient
1159 Note that in this case this function will exit(0) rather
1161 if (!strcmp(lname, "-")) {
1163 /* size of file is not known */
1165 f = x_fopen(lname,O_RDONLY, 0);
1167 if (x_tseek(f, start, SEEK_SET) == -1) {
1168 d_printf("Error seeking local file\n");
1175 d_printf("Error opening local file %s\n",lname);
1180 DEBUG(1,("putting file %s as %s ",lname,
1183 buf = (char *)malloc(maxwrite);
1185 d_printf("ERROR: Not enough memory!\n");
1188 while (!x_feof(f)) {
1192 if ((n = readfile(buf,n,f)) < 1) {
1193 if((n == 0) && x_feof(f))
1194 break; /* Empty local file. */
1196 d_printf("Error reading local file: %s\n", strerror(errno));
1201 ret = cli_write(cli->tree, fnum, 0, buf, nread + start, n);
1204 d_printf("Error writing file: %s\n", cli_errstr(cli->tree));
1212 if (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
1213 d_printf("%s closing remote file %s\n",cli_errstr(cli->tree),rname);
1227 struct timeval tp_end;
1230 GetTimeOfDay(&tp_end);
1232 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1233 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1234 put_total_time_ms += this_time;
1235 put_total_size += nread;
1237 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1238 nread / (1.024*this_time + 1.0e-4),
1239 put_total_size / (1.024*put_total_time_ms)));
1252 /****************************************************************************
1254 ****************************************************************************/
1255 static int cmd_put(void)
1262 pstrcpy(rname,cur_dir);
1263 pstrcat(rname,"\\");
1265 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1266 d_printf("put <filename>\n");
1271 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1274 pstrcat(rname,lname);
1276 dos_clean_name(rname);
1280 /* allow '-' to represent stdin
1281 jdblair, 24.jun.98 */
1282 if (!file_exist(lname,&st) &&
1283 (strcmp(lname,"-"))) {
1284 d_printf("%s does not exist\n",lname);
1289 return do_put(rname, lname, False);
1292 /*************************************
1294 *************************************/
1296 static struct file_list {
1297 struct file_list *prev, *next;
1302 /****************************************************************************
1303 Free a file_list structure
1304 ****************************************************************************/
1306 static void free_file_list (struct file_list * list)
1308 struct file_list *tmp;
1313 DLIST_REMOVE(list, list);
1314 SAFE_FREE(tmp->file_path);
1319 /****************************************************************************
1320 seek in a directory/file list until you get something that doesn't start with
1322 ****************************************************************************/
1323 static BOOL seek_list(struct file_list *list, char *name)
1326 trim_string(list->file_path,"./","\n");
1327 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1336 /****************************************************************************
1337 set the file selection mask
1338 ****************************************************************************/
1339 static int cmd_select(void)
1341 pstrcpy(fileselection,"");
1342 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1347 /****************************************************************************
1348 Recursive file matching function act as find
1349 match must be always set to True when calling this function
1350 ****************************************************************************/
1351 static int file_find(struct file_list **list, const char *directory,
1352 const char *expression, BOOL match)
1355 struct file_list *entry;
1356 struct stat statbuf;
1362 dir = opendir(directory);
1363 if (!dir) return -1;
1365 while ((dname = readdirname(dir))) {
1366 if (!strcmp("..", dname)) continue;
1367 if (!strcmp(".", dname)) continue;
1369 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1374 if (!match || !gen_fnmatch(expression, dname)) {
1376 ret = stat(path, &statbuf);
1378 if (S_ISDIR(statbuf.st_mode)) {
1380 ret = file_find(list, path, expression, False);
1383 d_printf("file_find: cannot stat file %s\n", path);
1392 entry = (struct file_list *) malloc(sizeof (struct file_list));
1394 d_printf("Out of memory in file_find\n");
1398 entry->file_path = path;
1399 entry->isdir = isdir;
1400 DLIST_ADD(*list, entry);
1410 /****************************************************************************
1412 ****************************************************************************/
1413 static int cmd_mput(void)
1418 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1420 struct file_list *temp_list;
1421 char *quest, *lname, *rname;
1425 ret = file_find(&file_list, ".", p, True);
1427 free_file_list(file_list);
1435 for (temp_list = file_list; temp_list;
1436 temp_list = temp_list->next) {
1439 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1441 trim_string(lname, "./", "/");
1443 /* check if it's a directory */
1444 if (temp_list->isdir) {
1445 /* if (!recurse) continue; */
1448 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1449 if (prompt && !yesno(quest)) { /* No */
1450 /* Skip the directory */
1451 lname[strlen(lname)-1] = '/';
1452 if (!seek_list(temp_list, lname))
1456 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1458 if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, rname)) &&
1459 NT_STATUS_IS_ERR(do_mkdir(rname))) {
1460 DEBUG (0, ("Unable to make dir, skipping..."));
1461 /* Skip the directory */
1462 lname[strlen(lname)-1] = '/';
1463 if (!seek_list(temp_list, lname))
1470 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1471 if (prompt && !yesno(quest)) /* No */
1476 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1481 do_put(rname, lname, False);
1483 free_file_list(file_list);
1493 /****************************************************************************
1495 ****************************************************************************/
1496 static int do_cancel(int job)
1498 d_printf("REWRITE: print job cancel not implemented\n");
1503 /****************************************************************************
1505 ****************************************************************************/
1506 static int cmd_cancel(void)
1511 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1512 d_printf("cancel <jobid> ...\n");
1518 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1524 /****************************************************************************
1526 ****************************************************************************/
1527 static int cmd_print(void)
1533 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1534 d_printf("print <filename>\n");
1538 pstrcpy(rname,lname);
1539 p = strrchr_m(rname,'/');
1541 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1544 if (strequal(lname,"-")) {
1545 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1548 return do_put(rname, lname, False);
1552 /****************************************************************************
1554 ****************************************************************************/
1555 static int cmd_queue(void)
1557 d_printf("REWRITE: print job queue not implemented\n");
1562 /****************************************************************************
1564 ****************************************************************************/
1565 static void do_del(file_info *finfo)
1569 pstrcpy(mask,cur_dir);
1570 pstrcat(mask,finfo->name);
1572 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY)
1575 if (NT_STATUS_IS_ERR(cli_unlink(cli->tree, mask))) {
1576 d_printf("%s deleting remote file %s\n",cli_errstr(cli->tree),mask);
1580 /****************************************************************************
1582 ****************************************************************************/
1583 static int cmd_del(void)
1587 uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1590 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1592 pstrcpy(mask,cur_dir);
1594 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1595 d_printf("del <filename>\n");
1600 do_list(mask, attribute,do_del,False,False);
1606 /****************************************************************************
1607 delete a whole directory tree
1608 ****************************************************************************/
1609 static int cmd_deltree(void)
1615 pstrcpy(dname,cur_dir);
1617 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1618 d_printf("deltree <dirname>\n");
1623 ret = cli_deltree(cli->tree, dname);
1626 printf("Failed to delete tree %s - %s\n", dname, cli_errstr(cli->tree));
1630 printf("Deleted %d files in %s\n", ret, dname);
1636 /****************************************************************************
1637 show as much information as possible about a file
1638 ****************************************************************************/
1639 static int cmd_allinfo(void)
1644 TALLOC_CTX *mem_ctx;
1645 union smb_fileinfo finfo;
1648 pstrcpy(fname,cur_dir);
1650 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1651 d_printf("allinfo <filename>\n");
1656 mem_ctx = talloc_init(fname);
1658 /* first a ALL_INFO QPATHINFO */
1659 finfo.generic.level = RAW_FILEINFO_ALL_INFO;
1660 finfo.generic.in.fname = fname;
1661 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1662 if (!NT_STATUS_IS_OK(status)) {
1663 d_printf("%s - %s\n", fname, nt_errstr(status));
1668 d_printf("\tcreate_time: %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.create_time));
1669 d_printf("\taccess_time: %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.access_time));
1670 d_printf("\twrite_time: %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.write_time));
1671 d_printf("\tchange_time: %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.change_time));
1672 d_printf("\tattrib: 0x%x\n", finfo.all_info.out.attrib);
1673 d_printf("\talloc_size: %lu\n", (unsigned long)finfo.all_info.out.alloc_size);
1674 d_printf("\tsize: %lu\n", (unsigned long)finfo.all_info.out.size);
1675 d_printf("\tnlink: %u\n", finfo.all_info.out.nlink);
1676 d_printf("\tdelete_pending: %u\n", finfo.all_info.out.delete_pending);
1677 d_printf("\tdirectory: %u\n", finfo.all_info.out.directory);
1678 d_printf("\tea_size: %u\n", finfo.all_info.out.ea_size);
1679 d_printf("\tfname: '%s'\n", finfo.all_info.out.fname.s);
1681 /* 8.3 name if any */
1682 finfo.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
1683 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1684 if (NT_STATUS_IS_OK(status)) {
1685 d_printf("\talt_name: %s\n", finfo.alt_name_info.out.fname.s);
1688 /* file_id if available */
1689 finfo.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
1690 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1691 if (NT_STATUS_IS_OK(status)) {
1692 d_printf("\tfile_id %.0f\n",
1693 (double)finfo.internal_information.out.file_id);
1696 /* the EAs, if any */
1697 finfo.generic.level = RAW_FILEINFO_ALL_EAS;
1698 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1699 if (NT_STATUS_IS_OK(status)) {
1701 for (i=0;i<finfo.all_eas.out.num_eas;i++) {
1702 d_printf("\tEA[%d] flags=%d %s=%*.*s\n", i,
1703 finfo.all_eas.out.eas[i].flags,
1704 finfo.all_eas.out.eas[i].name.s,
1705 finfo.all_eas.out.eas[i].value.length,
1706 finfo.all_eas.out.eas[i].value.length,
1707 finfo.all_eas.out.eas[i].value.data);
1711 /* streams, if available */
1712 finfo.generic.level = RAW_FILEINFO_STREAM_INFO;
1713 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1714 if (NT_STATUS_IS_OK(status)) {
1716 for (i=0;i<finfo.stream_info.out.num_streams;i++) {
1717 d_printf("\tstream %d:\n", i);
1718 d_printf("\t\tsize %ld\n",
1719 (long)finfo.stream_info.out.streams[i].size);
1720 d_printf("\t\talloc size %ld\n",
1721 (long)finfo.stream_info.out.streams[i].alloc_size);
1722 d_printf("\t\tname %s\n", finfo.stream_info.out.streams[i].stream_name.s);
1726 /* dev/inode if available */
1727 finfo.generic.level = RAW_FILEINFO_COMPRESSION_INFORMATION;
1728 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1729 if (NT_STATUS_IS_OK(status)) {
1730 d_printf("\tcompressed size %ld\n", (long)finfo.compression_info.out.compressed_size);
1731 d_printf("\tformat %ld\n", (long)finfo.compression_info.out.format);
1732 d_printf("\tunit_shift %ld\n", (long)finfo.compression_info.out.unit_shift);
1733 d_printf("\tchunk_shift %ld\n", (long)finfo.compression_info.out.chunk_shift);
1734 d_printf("\tcluster_shift %ld\n", (long)finfo.compression_info.out.cluster_shift);
1737 talloc_destroy(mem_ctx);
1744 /****************************************************************************
1745 show any ACL on a file
1746 ****************************************************************************/
1747 static int cmd_acl(void)
1752 TALLOC_CTX *mem_ctx;
1753 struct smb_query_secdesc query;
1757 pstrcpy(fname,cur_dir);
1759 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1760 d_printf("acl <filename>\n");
1765 fnum = cli_open(cli->tree, fname, O_RDONLY, DENY_NONE);
1767 d_printf("%s - %s\n", fname, cli_errstr(cli->tree));
1771 mem_ctx = talloc_init(fname);
1773 query.in.fnum = fnum;
1774 query.in.secinfo_flags = 0x7;
1776 status = smb_raw_query_secdesc(cli->tree, mem_ctx, &query);
1777 if (!NT_STATUS_IS_OK(status)) {
1778 d_printf("%s - %s\n", fname, nt_errstr(status));
1783 talloc_destroy(mem_ctx);
1790 /****************************************************************************
1791 ****************************************************************************/
1792 static int cmd_open(void)
1797 pstrcpy(mask,cur_dir);
1799 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1800 d_printf("open <filename>\n");
1805 cli_open(cli->tree, mask, O_RDWR, DENY_ALL);
1811 /****************************************************************************
1813 ****************************************************************************/
1814 static int cmd_rmdir(void)
1819 pstrcpy(mask,cur_dir);
1821 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1822 d_printf("rmdir <dirname>\n");
1827 if (NT_STATUS_IS_ERR(cli_rmdir(cli->tree, mask))) {
1828 d_printf("%s removing remote directory file %s\n",
1829 cli_errstr(cli->tree),mask);
1835 /****************************************************************************
1837 ****************************************************************************/
1838 static int cmd_link(void)
1843 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1844 d_printf("Server doesn't support UNIX CIFS calls.\n");
1848 pstrcpy(src,cur_dir);
1849 pstrcpy(dest,cur_dir);
1851 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1852 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1853 d_printf("link <src> <dest>\n");
1860 if (NT_STATUS_IS_ERR(cli_unix_hardlink(cli->tree, src, dest))) {
1861 d_printf("%s linking files (%s -> %s)\n", cli_errstr(cli->tree), src, dest);
1868 /****************************************************************************
1870 ****************************************************************************/
1872 static int cmd_symlink(void)
1877 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1878 d_printf("Server doesn't support UNIX CIFS calls.\n");
1882 pstrcpy(src,cur_dir);
1883 pstrcpy(dest,cur_dir);
1885 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1886 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1887 d_printf("symlink <src> <dest>\n");
1894 if (NT_STATUS_IS_ERR(cli_unix_symlink(cli->tree, src, dest))) {
1895 d_printf("%s symlinking files (%s -> %s)\n",
1896 cli_errstr(cli->tree), src, dest);
1903 /****************************************************************************
1905 ****************************************************************************/
1907 static int cmd_chmod(void)
1913 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1914 d_printf("Server doesn't support UNIX CIFS calls.\n");
1918 pstrcpy(src,cur_dir);
1920 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1921 !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1922 d_printf("chmod mode file\n");
1926 mode = (mode_t)strtol(buf, NULL, 8);
1929 if (NT_STATUS_IS_ERR(cli_unix_chmod(cli->tree, src, mode))) {
1930 d_printf("%s chmod file %s 0%o\n",
1931 cli_errstr(cli->tree), src, (unsigned int)mode);
1938 /****************************************************************************
1940 ****************************************************************************/
1942 static int cmd_chown(void)
1947 fstring buf, buf2, buf3;
1949 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1950 d_printf("Server doesn't support UNIX CIFS calls.\n");
1954 pstrcpy(src,cur_dir);
1956 if (!next_token(NULL,buf,NULL,sizeof(buf)) ||
1957 !next_token(NULL,buf2,NULL, sizeof(buf2)) ||
1958 !next_token(NULL,buf3,NULL, sizeof(buf3))) {
1959 d_printf("chown uid gid file\n");
1963 uid = (uid_t)atoi(buf);
1964 gid = (gid_t)atoi(buf2);
1967 if (NT_STATUS_IS_ERR(cli_unix_chown(cli->tree, src, uid, gid))) {
1968 d_printf("%s chown file %s uid=%d, gid=%d\n",
1969 cli_errstr(cli->tree), src, (int)uid, (int)gid);
1976 /****************************************************************************
1978 ****************************************************************************/
1979 static int cmd_rename(void)
1984 pstrcpy(src,cur_dir);
1985 pstrcpy(dest,cur_dir);
1987 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
1988 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1989 d_printf("rename <src> <dest>\n");
1996 if (NT_STATUS_IS_ERR(cli_rename(cli->tree, src, dest))) {
1997 d_printf("%s renaming files\n",cli_errstr(cli->tree));
2005 /****************************************************************************
2006 toggle the prompt flag
2007 ****************************************************************************/
2008 static int cmd_prompt(void)
2011 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2017 /****************************************************************************
2018 set the newer than time
2019 ****************************************************************************/
2020 static int cmd_newer(void)
2024 SMB_STRUCT_STAT sbuf;
2026 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2027 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2028 newer_than = sbuf.st_mtime;
2029 DEBUG(1,("Getting files newer than %s",
2030 asctime(LocalTime(&newer_than))));
2035 if (ok && newer_than == 0) {
2036 d_printf("Error setting newer-than time\n");
2043 /****************************************************************************
2044 set the archive level
2045 ****************************************************************************/
2046 static int cmd_archive(void)
2050 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2051 archive_level = atoi(buf);
2053 d_printf("Archive level is %d\n",archive_level);
2058 /****************************************************************************
2059 toggle the lowercaseflag
2060 ****************************************************************************/
2061 static int cmd_lowercase(void)
2063 lowercase = !lowercase;
2064 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2072 /****************************************************************************
2073 toggle the recurse flag
2074 ****************************************************************************/
2075 static int cmd_recurse(void)
2078 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2083 /****************************************************************************
2084 toggle the translate flag
2085 ****************************************************************************/
2086 static int cmd_translate(void)
2088 translation = !translation;
2089 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2090 translation?"on":"off"));
2096 /****************************************************************************
2097 do a printmode command
2098 ****************************************************************************/
2099 static int cmd_printmode(void)
2104 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2105 if (strequal(buf,"text")) {
2108 if (strequal(buf,"graphics"))
2111 printmode = atoi(buf);
2118 fstrcpy(mode,"text");
2121 fstrcpy(mode,"graphics");
2124 slprintf(mode,sizeof(mode)-1,"%d",printmode);
2128 DEBUG(2,("the printmode is now %s\n",mode));
2133 /****************************************************************************
2135 ****************************************************************************/
2136 static int cmd_lcd(void)
2141 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2143 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2148 /****************************************************************************
2149 get a file restarting at end of local file
2150 ****************************************************************************/
2151 static int cmd_reget(void)
2154 pstring remote_name;
2157 pstrcpy(remote_name, cur_dir);
2158 pstrcat(remote_name, "\\");
2160 p = remote_name + strlen(remote_name);
2162 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2163 d_printf("reget <filename>\n");
2166 pstrcpy(local_name, p);
2167 dos_clean_name(remote_name);
2169 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2171 return do_get(remote_name, local_name, True);
2174 /****************************************************************************
2175 put a file restarting at end of local file
2176 ****************************************************************************/
2177 static int cmd_reput(void)
2180 pstring remote_name;
2185 pstrcpy(remote_name, cur_dir);
2186 pstrcat(remote_name, "\\");
2188 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2189 d_printf("reput <filename>\n");
2192 pstrcpy(local_name, p);
2194 if (!file_exist(local_name, &st)) {
2195 d_printf("%s does not exist\n", local_name);
2199 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2200 pstrcat(remote_name, p);
2202 pstrcat(remote_name, local_name);
2204 dos_clean_name(remote_name);
2206 return do_put(remote_name, local_name, True);
2210 /****************************************************************************
2211 try and browse available connections on a host
2212 ****************************************************************************/
2213 static BOOL browse_host(BOOL sort)
2215 d_printf("REWRITE: host browsing not implemented\n");
2220 /****************************************************************************
2221 try and browse available connections on a host
2222 ****************************************************************************/
2223 static BOOL list_servers(char *wk_grp)
2225 d_printf("REWRITE: list servers not implemented\n");
2229 /* Some constants for completing filename arguments */
2231 #define COMPL_NONE 0 /* No completions */
2232 #define COMPL_REMOTE 1 /* Complete remote filename */
2233 #define COMPL_LOCAL 2 /* Complete local filename */
2235 /* This defines the commands supported by this client.
2236 * NOTE: The "!" must be the last one in the list because it's fn pointer
2237 * field is NULL, and NULL in that field is used in process_tok()
2238 * (below) to indicate the end of the list. crh
2244 const char *description;
2245 char compl_args[2]; /* Completion argument info */
2248 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2249 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2250 {"acl",cmd_acl,"<file> show file ACL",{COMPL_NONE,COMPL_NONE}},
2251 {"allinfo",cmd_allinfo,"<file> show all possible info about a file",{COMPL_NONE,COMPL_NONE}},
2252 {"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}},
2253 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2254 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2255 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2256 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2257 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2258 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2259 {"deltree",cmd_deltree,"<dir> delete a whole directory tree",{COMPL_REMOTE,COMPL_NONE}},
2260 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2261 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2262 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2263 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2264 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2265 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2266 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2267 {"link",cmd_link,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2268 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2269 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2270 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2271 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2272 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2273 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2274 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
2275 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2276 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2277 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2278 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2279 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
2280 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2281 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2282 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2283 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2284 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2285 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2286 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2287 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2288 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2289 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2290 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2291 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2292 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2293 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
2294 {"symlink",cmd_symlink,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2295 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2296 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2297 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2299 /* Yes, this must be here, see crh's comment above. */
2300 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2301 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2305 /*******************************************************************
2306 lookup a command string in the list of commands, including
2308 ******************************************************************/
2309 static int process_tok(fstring tok)
2311 int i = 0, matches = 0;
2313 int tok_len = strlen(tok);
2315 while (commands[i].fn != NULL) {
2316 if (strequal(commands[i].name,tok)) {
2320 } else if (strnequal(commands[i].name, tok, tok_len)) {
2329 else if (matches == 1)
2335 /****************************************************************************
2337 ****************************************************************************/
2338 static int cmd_help(void)
2343 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2344 if ((i = process_tok(buf)) >= 0)
2345 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2347 while (commands[i].description) {
2348 for (j=0; commands[i].description && (j<5); j++) {
2349 d_printf("%-15s",commands[i].name);
2358 /****************************************************************************
2359 process a -c command string
2360 ****************************************************************************/
2361 static int process_command_string(char *cmd)
2367 /* establish the connection if not already */
2370 cli = do_connect(desthost, service);
2375 while (cmd[0] != '\0') {
2380 if ((p = strchr_m(cmd, ';')) == 0) {
2381 strncpy(line, cmd, 999);
2385 if (p - cmd > 999) p = cmd + 999;
2386 strncpy(line, cmd, p - cmd);
2387 line[p - cmd] = '\0';
2391 /* and get the first part of the command */
2393 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2395 if ((i = process_tok(tok)) >= 0) {
2396 rc = commands[i].fn();
2397 } else if (i == -2) {
2398 d_printf("%s: command abbreviation ambiguous\n",tok);
2400 d_printf("%s: command not found\n",tok);
2407 #define MAX_COMPLETIONS 100
2415 } completion_remote_t;
2417 static void completion_remote_filter(file_info *f, const char *mask, void *state)
2419 completion_remote_t *info = (completion_remote_t *)state;
2421 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2422 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
2423 info->matches[info->count] = strdup(f->name);
2427 if (info->dirmask[0] != 0)
2428 pstrcpy(tmp, info->dirmask);
2431 pstrcat(tmp, f->name);
2432 if (f->mode & FILE_ATTRIBUTE_DIRECTORY)
2434 info->matches[info->count] = strdup(tmp);
2436 if (info->matches[info->count] == NULL)
2438 if (f->mode & FILE_ATTRIBUTE_DIRECTORY)
2439 smb_readline_ca_char(0);
2441 if (info->count == 1)
2442 info->samelen = strlen(info->matches[info->count]);
2444 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
2450 static char **remote_completion(const char *text, int len)
2454 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
2460 if (len >= PATH_MAX)
2463 info.matches = (char **)malloc(sizeof(info.matches[0])*MAX_COMPLETIONS);
2464 if (!info.matches) return NULL;
2465 info.matches[0] = NULL;
2467 for (i = len-1; i >= 0; i--)
2468 if ((text[i] == '/') || (text[i] == '\\'))
2470 info.text = text+i+1;
2471 info.samelen = info.len = len-i-1;
2474 strncpy(info.dirmask, text, i+1);
2475 info.dirmask[i+1] = 0;
2476 snprintf(dirmask, sizeof(dirmask), "%s%*s*", cur_dir, i-1, text);
2478 snprintf(dirmask, sizeof(dirmask), "%s*", cur_dir);
2480 if (cli_list(cli->tree, dirmask,
2481 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
2482 completion_remote_filter, &info) < 0)
2485 if (info.count == 2)
2486 info.matches[0] = strdup(info.matches[1]);
2488 info.matches[0] = malloc(info.samelen+1);
2489 if (!info.matches[0])
2491 strncpy(info.matches[0], info.matches[1], info.samelen);
2492 info.matches[0][info.samelen] = 0;
2494 info.matches[info.count] = NULL;
2495 return info.matches;
2498 for (i = 0; i < info.count; i++)
2499 free(info.matches[i]);
2504 static char **completion_fn(const char *text, int start, int end)
2506 smb_readline_ca_char(' ');
2509 const char *buf, *sp;
2513 buf = smb_readline_get_line_buffer();
2517 sp = strchr(buf, ' ');
2521 for (i = 0; commands[i].name; i++)
2522 if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
2524 if (commands[i].name == NULL)
2530 if (sp == (buf + start))
2531 compl_type = commands[i].compl_args[0];
2533 compl_type = commands[i].compl_args[1];
2535 if (compl_type == COMPL_REMOTE)
2536 return remote_completion(text, end - start);
2537 else /* fall back to local filename completion */
2541 int i, len, samelen, count=1;
2543 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
2544 if (!matches) return NULL;
2548 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
2549 if (strncmp(text, commands[i].name, len) == 0) {
2550 matches[count] = strdup(commands[i].name);
2551 if (!matches[count])
2554 samelen = strlen(matches[count]);
2556 while (strncmp(matches[count], matches[count-1], samelen) != 0)
2563 case 0: /* should never happen */
2567 matches[0] = strdup(matches[1]);
2570 matches[0] = malloc(samelen+1);
2573 strncpy(matches[0], matches[1], samelen);
2574 matches[0][samelen] = 0;
2576 matches[count] = NULL;
2589 /****************************************************************************
2590 make sure we swallow keepalives during idle time
2591 ****************************************************************************/
2592 static void readline_callback(void)
2595 struct timeval timeout;
2596 static time_t last_t;
2601 if (t - last_t < 5) return;
2606 if (cli->transport->socket->fd == -1)
2610 FD_SET(cli->transport->socket->fd, &fds);
2613 timeout.tv_usec = 0;
2614 sys_select_intr(cli->transport->socket->fd+1,&fds,NULL,NULL,&timeout);
2616 /* We deliberately use cli_swallow_keepalives instead of
2617 client_receive_smb as we want to receive
2618 session keepalives and then drop them here.
2620 if (FD_ISSET(cli->transport->socket->fd, &fds)) {
2621 if (!cli_request_receive_next(cli->transport)) {
2622 d_printf("Lost connection to server\n");
2629 cli_chkpath(cli->tree, "\\");
2634 /****************************************************************************
2635 process commands on stdin
2636 ****************************************************************************/
2637 static void process_stdin(void)
2648 /* display a prompt */
2649 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
2650 cline = smb_readline(the_prompt, readline_callback, completion_fn);
2654 pstrcpy(line, cline);
2656 /* special case - first char is ! */
2662 /* and get the first part of the command */
2664 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2666 if ((i = process_tok(tok)) >= 0) {
2668 } else if (i == -2) {
2669 d_printf("%s: command abbreviation ambiguous\n",tok);
2671 d_printf("%s: command not found\n",tok);
2677 /*****************************************************
2678 return a connection to a server
2679 *******************************************************/
2680 static struct cli_state *do_connect(const char *server, const char *share)
2682 struct cli_state *c;
2683 struct nmb_name called, calling;
2684 const char *server_n;
2686 fstring servicename;
2689 /* make a copy so we don't modify the global string 'service' */
2690 fstrcpy(servicename, share);
2691 sharename = servicename;
2692 if (*sharename == '\\') {
2693 server = sharename+2;
2694 sharename = strchr_m(server,'\\');
2695 if (!sharename) return NULL;
2700 asprintf(&sharename, "\\\\%s\\%s", server, sharename);
2706 make_nmb_name(&calling, lp_netbios_name(), 0x0);
2707 make_nmb_name(&called , server, name_type);
2711 if (have_ip) ip = dest_ip;
2713 /* have to open a new connection */
2714 if (!(c=cli_state_init()) || !cli_socket_connect(c, server_n, &ip)) {
2715 d_printf("Connection to %s failed\n", server_n);
2719 if (!cli_transport_establish(c, &calling, &called)) {
2721 d_printf("session request to %s failed (%s)\n",
2722 called.name, cli_errstr(c->tree));
2724 if ((p=strchr_m(called.name, '.'))) {
2728 if (strcmp(called.name, "*SMBSERVER")) {
2729 make_nmb_name(&called , "*SMBSERVER", 0x20);
2735 DEBUG(4,(" session request ok\n"));
2737 if (NT_STATUS_IS_ERR(cli_negprot(c))) {
2738 d_printf("protocol negotiation failed\n");
2744 const char *pass = getpass("Password: ");
2746 pstrcpy(password, pass);
2750 if (NT_STATUS_IS_ERR(cli_session_setup(c, username, password,
2752 /* if a password was not supplied then try again with a null username */
2753 if (password[0] || !username[0] || use_kerberos ||
2754 NT_STATUS_IS_ERR(cli_session_setup(c, "", "", lp_workgroup()))) {
2755 d_printf("session setup failed: %s\n", cli_errstr(c->tree));
2759 d_printf("Anonymous login successful\n");
2762 DEBUG(4,(" session setup ok\n"));
2764 if (NT_STATUS_IS_ERR(cli_send_tconX(c, sharename, "?????", password))) {
2765 d_printf("tree connect failed: %s\n", cli_errstr(c->tree));
2770 DEBUG(4,(" tconx ok\n"));
2776 /****************************************************************************
2777 process commands from the client
2778 ****************************************************************************/
2779 static int process(char *base_directory)
2783 cli = do_connect(desthost, service);
2788 if (*base_directory) do_cd(base_directory);
2791 rc = process_command_string(cmdstr);
2800 /****************************************************************************
2802 ****************************************************************************/
2803 static int do_host_query(char *query_host)
2805 cli = do_connect(query_host, "IPC$");
2810 list_servers(lp_workgroup());
2818 /****************************************************************************
2819 handle a tar operation
2820 ****************************************************************************/
2821 static int do_tar_op(char *base_directory)
2825 /* do we already have a connection? */
2827 cli = do_connect(desthost, service);
2834 if (*base_directory) do_cd(base_directory);
2843 /****************************************************************************
2844 handle a message operation
2845 ****************************************************************************/
2846 static int do_message_op(void)
2849 struct nmb_name called, calling;
2850 fstring server_name;
2851 char name_type_hex[10];
2853 make_nmb_name(&calling, lp_netbios_name(), 0x0);
2854 make_nmb_name(&called , desthost, name_type);
2856 fstrcpy(server_name, desthost);
2857 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
2858 fstrcat(server_name, name_type_hex);
2861 if (have_ip) ip = dest_ip;
2863 if (!(cli=cli_state_init()) || !cli_socket_connect(cli, server_name, &ip)) {
2864 d_printf("Connection to %s failed\n", desthost);
2868 if (!cli_transport_establish(cli, &calling, &called)) {
2869 d_printf("session request failed\n");
2882 * Process "-L hostname" option.
2884 * We don't actually do anything yet -- we just stash the name in a
2885 * global variable and do the query when all options have been read.
2887 static void remember_query_host(const char *arg,
2892 while (*arg == '\\' || *arg == '/')
2894 pstrcpy(query_host, arg);
2895 if ((slash = strchr(query_host, '/'))
2896 || (slash = strchr(query_host, '\\'))) {
2902 /****************************************************************************
2904 ****************************************************************************/
2905 int main(int argc,char *argv[])
2907 fstring base_directory;
2910 BOOL message = False;
2911 extern char tar_type;
2916 TALLOC_CTX *mem_ctx;
2917 struct poptOption long_options[] = {
2920 { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
2921 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
2922 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
2923 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
2924 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
2925 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
2926 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
2927 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
2928 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
2929 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
2930 { "send-buffer", 'b', POPT_ARG_INT, NULL, 'b', "Changes the transmit/send buffer", "BYTES" },
2931 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
2933 POPT_COMMON_CONNECTION
2934 POPT_COMMON_CREDENTIALS
2940 pstrcpy(term_code, KANJI);
2946 *base_directory = 0;
2948 setup_logging(argv[0],DEBUG_STDOUT);
2949 mem_ctx = talloc_init("client.c/main");
2951 d_printf("\nclient.c: Not enough memory\n");
2955 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
2956 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
2957 argv[0], dyn_CONFIGFILE);
2960 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options,
2961 POPT_CONTEXT_KEEP_FIRST);
2962 poptSetOtherOptionHelp(pc, "service <password>");
2964 in_client = True; /* Make sure that we tell lp_load we are */
2966 while ((opt = poptGetNextOpt(pc)) != -1) {
2969 /* Messages are sent to NetBIOS name type 0x3
2970 * (Messenger Service). Make sure we default
2971 * to port 139 instead of port 445. srl,crh
2974 pstrcpy(desthost,poptGetOptArg(pc));
2975 if( 0 == port ) port = 139;
2980 dest_ip = *interpret_addr2(mem_ctx, poptGetOptArg(pc));
2981 if (is_zero_ip(dest_ip))
2987 setup_logging("client", DEBUG_STDERR);
2991 remember_query_host(poptGetOptArg(pc), query_host);
2994 pstrcpy(term_code, poptGetOptArg(pc));
2997 lp_set_cmdline("max protocol", poptGetOptArg(pc));
3000 lp_set_cmdline("name resolve order", poptGetOptArg(pc));
3003 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), optind)) {
3004 poptPrintUsage(pc, stderr, 0);
3009 fstrcpy(base_directory,poptGetOptArg(pc));
3012 io_bufsize = MAX(1, atoi(poptGetOptArg(pc)));
3021 if(poptPeekArg(pc)) {
3022 pstrcpy(service,poptGetArg(pc));
3023 /* Convert any '/' characters in the service name to '\' characters */
3024 string_replace(service, '/','\\');
3026 if (count_chars(service,'\\') < 3) {
3027 d_printf("\n%s: Not enough '\\' characters in service\n",service);
3028 poptPrintUsage(pc, stderr, 0);
3033 if (poptPeekArg(pc)) {
3034 cmdline_auth_info.got_pass = True;
3035 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));
3040 if (!tar_type && !*query_host && !*service && !message) {
3041 poptPrintUsage(pc, stderr, 0);
3045 poptFreeContext(pc);
3047 pstrcpy(username, cmdline_auth_info.username);
3048 pstrcpy(password, cmdline_auth_info.password);
3049 use_kerberos = cmdline_auth_info.use_kerberos;
3050 got_pass = cmdline_auth_info.got_pass;
3052 DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION_STRING ) );
3054 talloc_destroy(mem_ctx);
3057 process_command_string(cmdstr);
3058 return do_tar_op(base_directory);
3061 if ((p=strchr_m(query_host,'#'))) {
3064 sscanf(p, "%x", &name_type);
3068 return do_host_query(query_host);
3072 return do_message_op();
3075 if (process(base_directory)) {