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-2004
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.
27 #include "lib/cmdline/popt_common.h"
28 #include "librpc/gen_ndr/ndr_srvsvc.h"
29 #include "librpc/gen_ndr/ndr_lsa.h"
30 #include "libcli/raw/libcliraw.h"
31 #include "system/time.h"
32 #include "system/dir.h"
33 #include "system/filesys.h"
34 #include "dlinklist.h"
40 static struct smbcli_state *cli;
42 static pstring cur_dir = "\\";
43 static pstring cd_path = "";
44 static pstring service;
45 static pstring desthost;
46 static char *cmdstr = NULL;
48 static int io_bufsize = 64512;
50 static int name_type = 0x20;
52 static int process_tok(fstring tok);
53 static int cmd_help(const char **cmd_ptr);
55 /* 30 second timeout on most commands */
56 #define CLIENT_TIMEOUT (30*1000)
57 #define SHORT_TIMEOUT (5*1000)
59 /* value for unused fid field in trans2 secondary request */
60 #define FID_UNUSED (0xFFFF)
62 time_t newer_than = 0;
63 static int archive_level = 0;
65 static BOOL translation = False;
67 /* clitar bits insert */
70 extern BOOL tar_reset;
74 static BOOL prompt = True;
76 static int printmode = 1;
78 static BOOL recurse = False;
79 BOOL lowercase = False;
81 static const char *dest_ip;
83 #define SEPARATORS " \t\n\r"
85 static BOOL abort_mget = True;
87 static pstring fileselection = "";
90 uint64_t get_total_size = 0;
91 uint_t get_total_time_ms = 0;
92 static uint64_t put_total_size = 0;
93 static uint_t put_total_time_ms = 0;
96 static double dir_total;
100 /* some forward declarations */
101 static struct smbcli_state *do_connect(const char *server, const char *share, struct cli_credentials *cred);
104 /*******************************************************************
105 Reduce a file name, removing .. elements.
106 ********************************************************************/
107 void dos_clean_name(char *s)
111 DEBUG(3,("dos_clean_name [%s]\n",s));
113 /* remove any double slashes */
114 all_string_sub(s, "\\\\", "\\", 0);
116 while ((p = strstr(s,"\\..\\")) != NULL) {
122 if ((p=strrchr_m(s,'\\')) != NULL)
129 trim_string(s,NULL,"\\..");
131 all_string_sub(s, "\\.\\", "\\", 0);
134 /****************************************************************************
135 write to a local file with CR/LF->LF translation if appropriate. return the
136 number taken from the buffer. This may not equal the number written.
137 ****************************************************************************/
138 static int writefile(int f, const void *_b, int n)
140 const uint8_t *b = _b;
149 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
152 if (write(f, b, 1) != 1) {
162 /****************************************************************************
163 read from a file with LF->CR/LF translation if appropriate. return the
164 number read. read approx n bytes.
165 ****************************************************************************/
166 static int readfile(void *_b, int n, XFILE *f)
173 return x_fread(b,1,n,f);
176 while (i < (n - 1)) {
177 if ((c = x_getc(f)) == EOF) {
181 if (c == '\n') { /* change all LFs to CR/LF */
192 /****************************************************************************
194 ****************************************************************************/
195 static void send_message(void)
200 if (!smbcli_message_start(cli->tree, desthost, cli_credentials_get_username(cmdline_credentials), &grp_id)) {
201 d_printf("message start: %s\n", smbcli_errstr(cli->tree));
206 d_printf("Connected. Type your message, ending it with a Control-D\n");
208 while (!feof(stdin) && total_len < 1600) {
209 int maxlen = MIN(1600 - total_len,127);
216 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
222 if (!smbcli_message_text(cli->tree, msg, l, grp_id)) {
223 d_printf("SMBsendtxt failed (%s)\n",smbcli_errstr(cli->tree));
230 if (total_len >= 1600)
231 d_printf("the message was truncated to 1600 bytes\n");
233 d_printf("sent %d bytes\n",total_len);
235 if (!smbcli_message_end(cli->tree, grp_id)) {
236 d_printf("SMBsendend failed (%s)\n",smbcli_errstr(cli->tree));
243 /****************************************************************************
244 check the space on a device
245 ****************************************************************************/
246 static int do_dskattr(void)
248 int total, bsize, avail;
250 if (NT_STATUS_IS_ERR(smbcli_dskattr(cli->tree, &bsize, &total, &avail))) {
251 d_printf("Error in dskattr: %s\n",smbcli_errstr(cli->tree));
255 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
256 total, bsize, avail);
261 /****************************************************************************
263 ****************************************************************************/
264 static int cmd_pwd(const char **cmd_ptr)
266 d_printf("Current directory is %s",service);
267 d_printf("%s\n",cur_dir);
272 convert a string to dos format
274 static void dos_format(char *s)
276 string_replace(s, '/', '\\');
279 /****************************************************************************
280 change directory - inner section
281 ****************************************************************************/
282 static int do_cd(char *newdir)
290 /* Save the current directory in case the
291 new directory is invalid */
292 pstrcpy(saved_dir, cur_dir);
297 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
298 pstrcat(cur_dir, "\\");
300 dos_clean_name(cur_dir);
301 pstrcpy(dname,cur_dir);
302 pstrcat(cur_dir,"\\");
303 dos_clean_name(cur_dir);
305 if (!strequal(cur_dir,"\\")) {
306 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, dname))) {
307 d_printf("cd %s: %s\n", dname, smbcli_errstr(cli->tree));
308 pstrcpy(cur_dir,saved_dir);
312 pstrcpy(cd_path,cur_dir);
317 /****************************************************************************
319 ****************************************************************************/
320 static int cmd_cd(const char **cmd_ptr)
325 if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
328 d_printf("Current directory is %s\n",cur_dir);
334 BOOL mask_match(struct smbcli_state *c, const char *string, char *pattern,
335 BOOL is_case_sensitive)
339 if (strcmp(string,"..") == 0)
341 if (strcmp(pattern,".") == 0)
344 if (is_case_sensitive)
345 return ms_fnmatch(pattern, string,
346 c->transport->negotiate.protocol) == 0;
348 fstrcpy(p2, pattern);
352 return ms_fnmatch(p2, s2, c->transport->negotiate.protocol) == 0;
357 /*******************************************************************
358 decide if a file should be operated on
359 ********************************************************************/
360 static BOOL do_this_one(struct clilist_file_info *finfo)
362 if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY) return(True);
364 if (*fileselection &&
365 !mask_match(cli, finfo->name,fileselection,False)) {
366 DEBUG(3,("mask_match %s failed\n", finfo->name));
370 if (newer_than && finfo->mtime < newer_than) {
371 DEBUG(3,("newer_than %s failed\n", finfo->name));
375 if ((archive_level==1 || archive_level==2) && !(finfo->attrib & FILE_ATTRIBUTE_ARCHIVE)) {
376 DEBUG(3,("archive %s failed\n", finfo->name));
383 /****************************************************************************
384 display info about a file
385 ****************************************************************************/
386 static void display_finfo(struct clilist_file_info *finfo)
388 if (do_this_one(finfo)) {
389 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
390 char *astr = attrib_string(NULL, finfo->attrib);
391 d_printf(" %-30s%7.7s %8.0f %s",
395 asctime(localtime(&t)));
396 dir_total += finfo->size;
402 /****************************************************************************
403 accumulate size of a file
404 ****************************************************************************/
405 static void do_du(struct clilist_file_info *finfo)
407 if (do_this_one(finfo)) {
408 dir_total += finfo->size;
412 static BOOL do_list_recurse;
413 static BOOL do_list_dirs;
414 static char *do_list_queue = 0;
415 static long do_list_queue_size = 0;
416 static long do_list_queue_start = 0;
417 static long do_list_queue_end = 0;
418 static void (*do_list_fn)(struct clilist_file_info *);
420 /****************************************************************************
421 functions for do_list_queue
422 ****************************************************************************/
425 * The do_list_queue is a NUL-separated list of strings stored in a
426 * char*. Since this is a FIFO, we keep track of the beginning and
427 * ending locations of the data in the queue. When we overflow, we
428 * double the size of the char*. When the start of the data passes
429 * the midpoint, we move everything back. This is logically more
430 * complex than a linked list, but easier from a memory management
431 * angle. In any memory error condition, do_list_queue is reset.
432 * Functions check to ensure that do_list_queue is non-NULL before
435 static void reset_do_list_queue(void)
437 SAFE_FREE(do_list_queue);
438 do_list_queue_size = 0;
439 do_list_queue_start = 0;
440 do_list_queue_end = 0;
443 static void init_do_list_queue(void)
445 reset_do_list_queue();
446 do_list_queue_size = 1024;
447 do_list_queue = malloc(do_list_queue_size);
448 if (do_list_queue == 0) {
449 d_printf("malloc fail for size %d\n",
450 (int)do_list_queue_size);
451 reset_do_list_queue();
453 memset(do_list_queue, 0, do_list_queue_size);
457 static void adjust_do_list_queue(void)
460 * If the starting point of the queue is more than half way through,
461 * move everything toward the beginning.
463 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
465 DEBUG(4,("do_list_queue is empty\n"));
466 do_list_queue_start = do_list_queue_end = 0;
467 *do_list_queue = '\0';
469 else if (do_list_queue_start > (do_list_queue_size / 2))
471 DEBUG(4,("sliding do_list_queue backward\n"));
472 memmove(do_list_queue,
473 do_list_queue + do_list_queue_start,
474 do_list_queue_end - do_list_queue_start);
475 do_list_queue_end -= do_list_queue_start;
476 do_list_queue_start = 0;
481 static void add_to_do_list_queue(const char* entry)
484 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
485 while (new_end > do_list_queue_size)
487 do_list_queue_size *= 2;
488 DEBUG(4,("enlarging do_list_queue to %d\n",
489 (int)do_list_queue_size));
490 dlq = realloc_p(do_list_queue, char, do_list_queue_size);
492 d_printf("failure enlarging do_list_queue to %d bytes\n",
493 (int)do_list_queue_size);
494 reset_do_list_queue();
499 memset(do_list_queue + do_list_queue_size / 2,
500 0, do_list_queue_size / 2);
505 safe_strcpy(do_list_queue + do_list_queue_end, entry,
506 do_list_queue_size - do_list_queue_end - 1);
507 do_list_queue_end = new_end;
508 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
509 entry, (int)do_list_queue_start, (int)do_list_queue_end));
513 static char *do_list_queue_head(void)
515 return do_list_queue + do_list_queue_start;
518 static void remove_do_list_queue_head(void)
520 if (do_list_queue_end > do_list_queue_start)
522 do_list_queue_start += strlen(do_list_queue_head()) + 1;
523 adjust_do_list_queue();
524 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
525 (int)do_list_queue_start, (int)do_list_queue_end));
529 static int do_list_queue_empty(void)
531 return (! (do_list_queue && *do_list_queue));
534 /****************************************************************************
536 ****************************************************************************/
537 static void do_list_helper(struct clilist_file_info *f, const char *mask, void *state)
539 if (f->attrib & FILE_ATTRIBUTE_DIRECTORY) {
540 if (do_list_dirs && do_this_one(f)) {
543 if (do_list_recurse &&
544 !strequal(f->name,".") &&
545 !strequal(f->name,"..")) {
549 pstrcpy(mask2, mask);
550 p = strrchr_m(mask2,'\\');
553 pstrcat(mask2, f->name);
554 pstrcat(mask2,"\\*");
555 add_to_do_list_queue(mask2);
560 if (do_this_one(f)) {
566 /****************************************************************************
567 a wrapper around smbcli_list that adds recursion
568 ****************************************************************************/
569 void do_list(const char *mask,uint16_t attribute,
570 void (*fn)(struct clilist_file_info *),BOOL rec, BOOL dirs)
572 static int in_do_list = 0;
574 if (in_do_list && rec)
576 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
582 do_list_recurse = rec;
588 init_do_list_queue();
589 add_to_do_list_queue(mask);
591 while (! do_list_queue_empty())
594 * Need to copy head so that it doesn't become
595 * invalid inside the call to smbcli_list. This
596 * would happen if the list were expanded
598 * Fix from E. Jay Berkenbilt (ejb@ql.org)
601 pstrcpy(head, do_list_queue_head());
602 smbcli_list(cli->tree, head, attribute, do_list_helper, NULL);
603 remove_do_list_queue_head();
604 if ((! do_list_queue_empty()) && (fn == display_finfo))
606 char* next_file = do_list_queue_head();
608 if ((strlen(next_file) >= 2) &&
609 (next_file[strlen(next_file) - 1] == '*') &&
610 (next_file[strlen(next_file) - 2] == '\\'))
612 save_ch = next_file +
613 strlen(next_file) - 2;
616 d_printf("\n%s\n",next_file);
626 if (smbcli_list(cli->tree, mask, attribute, do_list_helper, NULL) == -1)
628 d_printf("%s listing %s\n", smbcli_errstr(cli->tree), mask);
633 reset_do_list_queue();
636 /****************************************************************************
637 get a directory listing
638 ****************************************************************************/
639 static int cmd_dir(const char **cmd_ptr)
641 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
648 pstrcpy(mask,cur_dir);
649 if(mask[strlen(mask)-1]!='\\')
652 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
660 if (cli->tree->session->transport->negotiate.protocol <=
668 do_list(mask, attribute, display_finfo, recurse, True);
672 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
678 /****************************************************************************
679 get a directory listing
680 ****************************************************************************/
681 static int cmd_du(const char **cmd_ptr)
683 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
690 pstrcpy(mask,cur_dir);
691 if(mask[strlen(mask)-1]!='\\')
694 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
704 do_list(mask, attribute, do_du, recurse, True);
708 d_printf("Total number of bytes: %.0f\n", dir_total);
714 /****************************************************************************
715 get a file from rname to lname
716 ****************************************************************************/
717 static int do_get(char *rname, const char *lname, BOOL reget)
719 int handle = 0, fnum;
720 BOOL newhandle = False;
722 struct timeval tp_start;
723 int read_size = io_bufsize;
730 GetTimeOfDay(&tp_start);
733 strlower(discard_const_p(char, lname));
736 fnum = smbcli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
739 d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
743 if(!strcmp(lname,"-")) {
744 handle = fileno(stdout);
747 handle = open(lname, O_WRONLY|O_CREAT, 0644);
749 start = lseek(handle, 0, SEEK_END);
751 d_printf("Error seeking local file\n");
756 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
761 d_printf("Error opening local file %s\n",lname);
766 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum,
767 &attr, &size, NULL, NULL, NULL, NULL, NULL)) &&
768 NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum,
769 &attr, &size, NULL, NULL, NULL))) {
770 d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
774 DEBUG(2,("getting file %s of size %.0f as %s ",
775 rname, (double)size, lname));
777 if(!(data = (uint8_t *)malloc(read_size))) {
778 d_printf("malloc fail for size %d\n", read_size);
779 smbcli_close(cli->tree, fnum);
784 int n = smbcli_read(cli->tree, fnum, data, nread + start, read_size);
788 if (writefile(handle,data, n) != n) {
789 d_printf("Error writing local file\n");
797 if (nread + start < size) {
798 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
799 rname, (long)nread));
806 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
807 d_printf("Error %s closing remote file\n",smbcli_errstr(cli->tree));
815 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
816 smbcli_setatr(cli->tree, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
820 struct timeval tp_end;
823 GetTimeOfDay(&tp_end);
825 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
826 (tp_end.tv_usec - tp_start.tv_usec)/1000;
827 get_total_time_ms += this_time;
828 get_total_size += nread;
830 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
831 nread / (1.024*this_time + 1.0e-4),
832 get_total_size / (1.024*get_total_time_ms)));
839 /****************************************************************************
841 ****************************************************************************/
842 static int cmd_get(const char **cmd_ptr)
848 pstrcpy(rname,cur_dir);
851 p = rname + strlen(rname);
853 if (!next_token(cmd_ptr,p,NULL,sizeof(rname)-strlen(rname))) {
854 d_printf("get <filename>\n");
858 dos_clean_name(rname);
860 next_token(cmd_ptr,lname,NULL,sizeof(lname));
862 return do_get(rname, lname, False);
865 /****************************************************************************
866 Put up a yes/no prompt.
867 ****************************************************************************/
868 static BOOL yesno(char *p)
873 if (!fgets(ans,sizeof(ans)-1,stdin))
876 if (*ans == 'y' || *ans == 'Y')
882 /****************************************************************************
883 do a mget operation on one file
884 ****************************************************************************/
885 static void do_mget(struct clilist_file_info *finfo)
889 pstring saved_curdir;
892 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
896 d_printf("mget aborted\n");
900 if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)
901 slprintf(quest,sizeof(pstring)-1,
902 "Get directory %s? ",finfo->name);
904 slprintf(quest,sizeof(pstring)-1,
905 "Get file %s? ",finfo->name);
907 if (prompt && !yesno(quest)) return;
909 if (!(finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)) {
910 pstrcpy(rname,cur_dir);
911 pstrcat(rname,finfo->name);
912 do_get(rname, finfo->name, False);
916 /* handle directories */
917 pstrcpy(saved_curdir,cur_dir);
919 pstrcat(cur_dir,finfo->name);
920 pstrcat(cur_dir,"\\");
922 string_replace(discard_const_p(char, finfo->name), '\\', '/');
924 strlower(discard_const_p(char, finfo->name));
927 if (!directory_exist(finfo->name) &&
928 mkdir(finfo->name,0777) != 0) {
929 d_printf("failed to create directory %s\n",finfo->name);
930 pstrcpy(cur_dir,saved_curdir);
934 if (chdir(finfo->name) != 0) {
935 d_printf("failed to chdir to directory %s\n",finfo->name);
936 pstrcpy(cur_dir,saved_curdir);
940 pstrcpy(mget_mask,cur_dir);
941 pstrcat(mget_mask,"*");
943 do_list(mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,False, True);
945 pstrcpy(cur_dir,saved_curdir);
949 /****************************************************************************
950 view the file using the pager
951 ****************************************************************************/
952 static int cmd_more(const char **cmd_ptr)
954 fstring rname,lname,pager_cmd;
959 fstrcpy(rname,cur_dir);
962 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
963 fd = smb_mkstemp(lname);
965 d_printf("failed to create temporary file for more\n");
970 if (!next_token(cmd_ptr,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
971 d_printf("more <filename>\n");
975 dos_clean_name(rname);
977 rc = do_get(rname, lname, False);
979 pager=getenv("PAGER");
981 slprintf(pager_cmd,sizeof(pager_cmd)-1,
982 "%s %s",(pager? pager:PAGER), lname);
991 /****************************************************************************
993 ****************************************************************************/
994 static int cmd_mget(const char **cmd_ptr)
996 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1004 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1008 while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1009 pstrcpy(mget_mask,cur_dir);
1010 if(mget_mask[strlen(mget_mask)-1]!='\\')
1011 pstrcat(mget_mask,"\\");
1014 pstrcpy(mget_mask,p);
1016 pstrcat(mget_mask,p);
1017 do_list(mget_mask, attribute,do_mget,False,True);
1021 pstrcpy(mget_mask,cur_dir);
1022 if(mget_mask[strlen(mget_mask)-1]!='\\')
1023 pstrcat(mget_mask,"\\");
1024 pstrcat(mget_mask,"*");
1025 do_list(mget_mask, attribute,do_mget,False,True);
1032 /****************************************************************************
1033 make a directory of name "name"
1034 ****************************************************************************/
1035 static NTSTATUS do_mkdir(char *name)
1039 if (NT_STATUS_IS_ERR(status = smbcli_mkdir(cli->tree, name))) {
1040 d_printf("%s making remote directory %s\n",
1041 smbcli_errstr(cli->tree),name);
1048 /****************************************************************************
1049 show 8.3 name of a file
1050 ****************************************************************************/
1051 static BOOL do_altname(char *name)
1053 const char *altname;
1054 if (!NT_STATUS_IS_OK(smbcli_qpathinfo_alt_name(cli->tree, name, &altname))) {
1055 d_printf("%s getting alt name for %s\n",
1056 smbcli_errstr(cli->tree),name);
1059 d_printf("%s\n", altname);
1065 /****************************************************************************
1067 ****************************************************************************/
1068 static int cmd_quit(const char **cmd_ptr)
1077 /****************************************************************************
1079 ****************************************************************************/
1080 static int cmd_mkdir(const char **cmd_ptr)
1086 pstrcpy(mask,cur_dir);
1088 if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1090 d_printf("mkdir <dirname>\n");
1101 trim_string(ddir,".",NULL);
1102 p = strtok(ddir,"/\\");
1105 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, ddir2))) {
1108 pstrcat(ddir2,"\\");
1109 p = strtok(NULL,"/\\");
1119 /****************************************************************************
1121 ****************************************************************************/
1122 static int cmd_altname(const char **cmd_ptr)
1128 pstrcpy(name,cur_dir);
1130 if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1131 d_printf("altname <file>\n");
1142 /****************************************************************************
1144 ****************************************************************************/
1145 static int do_put(char *rname, char *lname, BOOL reput)
1151 uint8_t *buf = NULL;
1152 int maxwrite = io_bufsize;
1155 struct timeval tp_start;
1156 GetTimeOfDay(&tp_start);
1159 fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT, DENY_NONE);
1161 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL)) &&
1162 NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, NULL, &start, NULL, NULL, NULL))) {
1163 d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
1168 fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT|O_TRUNC,
1173 d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
1177 /* allow files to be piped into smbclient
1180 Note that in this case this function will exit(0) rather
1182 if (!strcmp(lname, "-")) {
1184 /* size of file is not known */
1186 f = x_fopen(lname,O_RDONLY, 0);
1188 if (x_tseek(f, start, SEEK_SET) == -1) {
1189 d_printf("Error seeking local file\n");
1196 d_printf("Error opening local file %s\n",lname);
1201 DEBUG(1,("putting file %s as %s ",lname,
1204 buf = (uint8_t *)malloc(maxwrite);
1206 d_printf("ERROR: Not enough memory!\n");
1209 while (!x_feof(f)) {
1213 if ((n = readfile(buf,n,f)) < 1) {
1214 if((n == 0) && x_feof(f))
1215 break; /* Empty local file. */
1217 d_printf("Error reading local file: %s\n", strerror(errno));
1222 ret = smbcli_write(cli->tree, fnum, 0, buf, nread + start, n);
1225 d_printf("Error writing file: %s\n", smbcli_errstr(cli->tree));
1233 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
1234 d_printf("%s closing remote file %s\n",smbcli_errstr(cli->tree),rname);
1248 struct timeval tp_end;
1251 GetTimeOfDay(&tp_end);
1253 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1254 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1255 put_total_time_ms += this_time;
1256 put_total_size += nread;
1258 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1259 nread / (1.024*this_time + 1.0e-4),
1260 put_total_size / (1.024*put_total_time_ms)));
1273 /****************************************************************************
1275 ****************************************************************************/
1276 static int cmd_put(const char **cmd_ptr)
1283 pstrcpy(rname,cur_dir);
1284 pstrcat(rname,"\\");
1286 if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1287 d_printf("put <filename>\n");
1292 if (next_token(cmd_ptr,p,NULL,sizeof(buf)))
1295 pstrcat(rname,lname);
1297 dos_clean_name(rname);
1299 /* allow '-' to represent stdin
1300 jdblair, 24.jun.98 */
1301 if (!file_exist(lname) && (strcmp(lname,"-"))) {
1302 d_printf("%s does not exist\n",lname);
1306 return do_put(rname, lname, False);
1309 /*************************************
1311 *************************************/
1313 static struct file_list {
1314 struct file_list *prev, *next;
1319 /****************************************************************************
1320 Free a file_list structure
1321 ****************************************************************************/
1323 static void free_file_list (struct file_list * list)
1325 struct file_list *tmp;
1330 DLIST_REMOVE(list, list);
1331 SAFE_FREE(tmp->file_path);
1336 /****************************************************************************
1337 seek in a directory/file list until you get something that doesn't start with
1339 ****************************************************************************/
1340 static BOOL seek_list(struct file_list *list, char *name)
1343 trim_string(list->file_path,"./","\n");
1344 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1353 /****************************************************************************
1354 set the file selection mask
1355 ****************************************************************************/
1356 static int cmd_select(const char **cmd_ptr)
1358 pstrcpy(fileselection,"");
1359 next_token(cmd_ptr,fileselection,NULL,sizeof(fileselection));
1364 /*******************************************************************
1365 A readdir wrapper which just returns the file name.
1366 ********************************************************************/
1367 static const char *readdirname(DIR *p)
1375 ptr = (struct dirent *)readdir(p);
1379 dname = ptr->d_name;
1386 #ifdef HAVE_BROKEN_READDIR
1387 /* using /usr/ucb/cc is BAD */
1393 int len = NAMLEN(ptr);
1394 memcpy(buf, dname, len);
1402 /****************************************************************************
1403 Recursive file matching function act as find
1404 match must be always set to True when calling this function
1405 ****************************************************************************/
1406 static int file_find(struct file_list **list, const char *directory,
1407 const char *expression, BOOL match)
1410 struct file_list *entry;
1411 struct stat statbuf;
1417 dir = opendir(directory);
1418 if (!dir) return -1;
1420 while ((dname = readdirname(dir))) {
1421 if (!strcmp("..", dname)) continue;
1422 if (!strcmp(".", dname)) continue;
1424 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1429 if (!match || !gen_fnmatch(expression, dname)) {
1431 ret = stat(path, &statbuf);
1433 if (S_ISDIR(statbuf.st_mode)) {
1435 ret = file_find(list, path, expression, False);
1438 d_printf("file_find: cannot stat file %s\n", path);
1447 entry = malloc_p(struct file_list);
1449 d_printf("Out of memory in file_find\n");
1453 entry->file_path = path;
1454 entry->isdir = isdir;
1455 DLIST_ADD(*list, entry);
1465 /****************************************************************************
1467 ****************************************************************************/
1468 static int cmd_mput(const char **cmd_ptr)
1473 while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1475 struct file_list *temp_list;
1476 char *quest, *lname, *rname;
1480 ret = file_find(&file_list, ".", p, True);
1482 free_file_list(file_list);
1490 for (temp_list = file_list; temp_list;
1491 temp_list = temp_list->next) {
1494 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1496 trim_string(lname, "./", "/");
1498 /* check if it's a directory */
1499 if (temp_list->isdir) {
1500 /* if (!recurse) continue; */
1503 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1504 if (prompt && !yesno(quest)) { /* No */
1505 /* Skip the directory */
1506 lname[strlen(lname)-1] = '/';
1507 if (!seek_list(temp_list, lname))
1511 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1513 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, rname)) &&
1514 NT_STATUS_IS_ERR(do_mkdir(rname))) {
1515 DEBUG (0, ("Unable to make dir, skipping..."));
1516 /* Skip the directory */
1517 lname[strlen(lname)-1] = '/';
1518 if (!seek_list(temp_list, lname))
1525 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1526 if (prompt && !yesno(quest)) /* No */
1531 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1536 do_put(rname, lname, False);
1538 free_file_list(file_list);
1548 /****************************************************************************
1550 ****************************************************************************/
1551 static int do_cancel(int job)
1553 d_printf("REWRITE: print job cancel not implemented\n");
1558 /****************************************************************************
1560 ****************************************************************************/
1561 static int cmd_cancel(const char **cmd_ptr)
1566 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1567 d_printf("cancel <jobid> ...\n");
1573 } while (next_token(cmd_ptr,buf,NULL,sizeof(buf)));
1579 /****************************************************************************
1581 ****************************************************************************/
1582 static int cmd_print(const char **cmd_ptr)
1588 if (!next_token(cmd_ptr,lname,NULL, sizeof(lname))) {
1589 d_printf("print <filename>\n");
1593 pstrcpy(rname,lname);
1594 p = strrchr_m(rname,'/');
1596 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1599 if (strequal(lname,"-")) {
1600 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1603 return do_put(rname, lname, False);
1607 /****************************************************************************
1609 ****************************************************************************/
1610 static int cmd_queue(const char **cmd_ptr)
1612 d_printf("REWRITE: print job queue not implemented\n");
1617 /****************************************************************************
1619 ****************************************************************************/
1620 static int cmd_del(const char **cmd_ptr)
1624 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1627 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1629 pstrcpy(mask,cur_dir);
1631 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1632 d_printf("del <filename>\n");
1637 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, mask))) {
1638 d_printf("%s deleting remote file %s\n",smbcli_errstr(cli->tree),mask);
1645 /****************************************************************************
1646 delete a whole directory tree
1647 ****************************************************************************/
1648 static int cmd_deltree(const char **cmd_ptr)
1654 pstrcpy(dname,cur_dir);
1656 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1657 d_printf("deltree <dirname>\n");
1662 ret = smbcli_deltree(cli->tree, dname);
1665 printf("Failed to delete tree %s - %s\n", dname, smbcli_errstr(cli->tree));
1669 printf("Deleted %d files in %s\n", ret, dname);
1675 const char *level_name;
1676 enum smb_fsinfo_level level;
1679 fsinfo_level_t fsinfo_levels[] = {
1680 {"dskattr", RAW_QFS_DSKATTR},
1681 {"allocation", RAW_QFS_ALLOCATION},
1682 {"volume", RAW_QFS_VOLUME},
1683 {"volumeinfo", RAW_QFS_VOLUME_INFO},
1684 {"sizeinfo", RAW_QFS_SIZE_INFO},
1685 {"deviceinfo", RAW_QFS_DEVICE_INFO},
1686 {"attributeinfo", RAW_QFS_ATTRIBUTE_INFO},
1687 {"unixinfo", RAW_QFS_UNIX_INFO},
1688 {"volume-information", RAW_QFS_VOLUME_INFORMATION},
1689 {"size-information", RAW_QFS_SIZE_INFORMATION},
1690 {"device-information", RAW_QFS_DEVICE_INFORMATION},
1691 {"attribute-information", RAW_QFS_ATTRIBUTE_INFORMATION},
1692 {"quota-information", RAW_QFS_QUOTA_INFORMATION},
1693 {"fullsize-information", RAW_QFS_FULL_SIZE_INFORMATION},
1694 {"objectid", RAW_QFS_OBJECTID_INFORMATION},
1695 {NULL, RAW_QFS_GENERIC}
1699 static int cmd_fsinfo(const char **cmd_ptr)
1703 union smb_fsinfo fsinfo;
1705 TALLOC_CTX *mem_ctx;
1706 fsinfo_level_t *fsinfo_level;
1708 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1709 d_printf("fsinfo <level>, where level is one of following:\n");
1710 fsinfo_level = fsinfo_levels;
1711 while(fsinfo_level->level_name) {
1712 d_printf("%s\n", fsinfo_level->level_name);
1718 fsinfo_level = fsinfo_levels;
1719 while(fsinfo_level->level_name && !strequal(buf,fsinfo_level->level_name)) {
1723 if (!fsinfo_level->level_name) {
1724 d_printf("wrong level name!\n");
1728 mem_ctx = talloc_init("fsinfo-level-%s", fsinfo_level->level_name);
1729 fsinfo.generic.level = fsinfo_level->level;
1730 status = smb_raw_fsinfo(cli->tree, mem_ctx, &fsinfo);
1731 if (!NT_STATUS_IS_OK(status)) {
1732 d_printf("fsinfo-level-%s - %s\n", fsinfo_level->level_name, nt_errstr(status));
1737 d_printf("fsinfo-level-%s:\n", fsinfo_level->level_name);
1738 switch(fsinfo.generic.level) {
1739 case RAW_QFS_DSKATTR:
1740 d_printf("\tunits_total: %hu\n",
1741 (unsigned short) fsinfo.dskattr.out.units_total);
1742 d_printf("\tblocks_per_unit: %hu\n",
1743 (unsigned short) fsinfo.dskattr.out.blocks_per_unit);
1744 d_printf("\tblocks_size: %hu\n",
1745 (unsigned short) fsinfo.dskattr.out.block_size);
1746 d_printf("\tunits_free: %hu\n",
1747 (unsigned short) fsinfo.dskattr.out.units_free);
1749 case RAW_QFS_ALLOCATION:
1750 d_printf("\tfs_id: %lu\n",
1751 (unsigned long) fsinfo.allocation.out.fs_id);
1752 d_printf("\tsectors_per_unit: %lu\n",
1753 (unsigned long) fsinfo.allocation.out.sectors_per_unit);
1754 d_printf("\ttotal_alloc_units: %lu\n",
1755 (unsigned long) fsinfo.allocation.out.total_alloc_units);
1756 d_printf("\tavail_alloc_units: %lu\n",
1757 (unsigned long) fsinfo.allocation.out.avail_alloc_units);
1758 d_printf("\tbytes_per_sector: %hu\n",
1759 (unsigned short) fsinfo.allocation.out.bytes_per_sector);
1761 case RAW_QFS_VOLUME:
1762 d_printf("\tserial_number: %lu\n",
1763 (unsigned long) fsinfo.volume.out.serial_number);
1764 d_printf("\tvolume_name: %s\n", fsinfo.volume.out.volume_name.s);
1766 case RAW_QFS_VOLUME_INFO:
1767 case RAW_QFS_VOLUME_INFORMATION:
1768 d_printf("\tcreate_time: %s\n",
1769 nt_time_string(mem_ctx,fsinfo.volume_info.out.create_time));
1770 d_printf("\tserial_number: %lu\n",
1771 (unsigned long) fsinfo.volume_info.out.serial_number);
1772 d_printf("\tvolume_name: %s\n", fsinfo.volume_info.out.volume_name.s);
1774 case RAW_QFS_SIZE_INFO:
1775 case RAW_QFS_SIZE_INFORMATION:
1776 d_printf("\ttotal_alloc_units: %llu\n",
1777 (unsigned long long) fsinfo.size_info.out.total_alloc_units);
1778 d_printf("\tavail_alloc_units: %llu\n",
1779 (unsigned long long) fsinfo.size_info.out.avail_alloc_units);
1780 d_printf("\tsectors_per_unit: %lu\n",
1781 (unsigned long) fsinfo.size_info.out.sectors_per_unit);
1782 d_printf("\tbytes_per_sector: %lu\n",
1783 (unsigned long) fsinfo.size_info.out.bytes_per_sector);
1785 case RAW_QFS_DEVICE_INFO:
1786 case RAW_QFS_DEVICE_INFORMATION:
1787 d_printf("\tdevice_type: %lu\n",
1788 (unsigned long) fsinfo.device_info.out.device_type);
1789 d_printf("\tcharacteristics: 0x%lx\n",
1790 (unsigned long) fsinfo.device_info.out.characteristics);
1792 case RAW_QFS_ATTRIBUTE_INFORMATION:
1793 case RAW_QFS_ATTRIBUTE_INFO:
1794 d_printf("\tfs_attr: 0x%lx\n",
1795 (unsigned long) fsinfo.attribute_info.out.fs_attr);
1796 d_printf("\tmax_file_component_length: %lu\n",
1797 (unsigned long) fsinfo.attribute_info.out.max_file_component_length);
1798 d_printf("\tfs_type: %s\n", fsinfo.attribute_info.out.fs_type.s);
1800 case RAW_QFS_UNIX_INFO:
1801 d_printf("\tmajor_version: %hu\n",
1802 (unsigned short) fsinfo.unix_info.out.major_version);
1803 d_printf("\tminor_version: %hu\n",
1804 (unsigned short) fsinfo.unix_info.out.minor_version);
1805 d_printf("\tcapability: 0x%llx\n",
1806 (unsigned long long) fsinfo.unix_info.out.capability);
1808 case RAW_QFS_QUOTA_INFORMATION:
1809 d_printf("\tunknown[3]: [%llu,%llu,%llu]\n",
1810 (unsigned long long) fsinfo.quota_information.out.unknown[0],
1811 (unsigned long long) fsinfo.quota_information.out.unknown[1],
1812 (unsigned long long) fsinfo.quota_information.out.unknown[2]);
1813 d_printf("\tquota_soft: %llu\n",
1814 (unsigned long long) fsinfo.quota_information.out.quota_soft);
1815 d_printf("\tquota_hard: %llu\n",
1816 (unsigned long long) fsinfo.quota_information.out.quota_hard);
1817 d_printf("\tquota_flags: 0x%llx\n",
1818 (unsigned long long) fsinfo.quota_information.out.quota_flags);
1820 case RAW_QFS_FULL_SIZE_INFORMATION:
1821 d_printf("\ttotal_alloc_units: %llu\n",
1822 (unsigned long long) fsinfo.full_size_information.out.total_alloc_units);
1823 d_printf("\tcall_avail_alloc_units: %llu\n",
1824 (unsigned long long) fsinfo.full_size_information.out.call_avail_alloc_units);
1825 d_printf("\tactual_avail_alloc_units: %llu\n",
1826 (unsigned long long) fsinfo.full_size_information.out.actual_avail_alloc_units);
1827 d_printf("\tsectors_per_unit: %lu\n",
1828 (unsigned long) fsinfo.full_size_information.out.sectors_per_unit);
1829 d_printf("\tbytes_per_sector: %lu\n",
1830 (unsigned long) fsinfo.full_size_information.out.bytes_per_sector);
1832 case RAW_QFS_OBJECTID_INFORMATION:
1833 d_printf("\tGUID: %s\n",
1834 GUID_string(mem_ctx,&fsinfo.objectid_information.out.guid));
1835 d_printf("\tunknown[6]: [%llu,%llu,%llu,%llu,%llu,%llu]\n",
1836 (unsigned long long) fsinfo.objectid_information.out.unknown[0],
1837 (unsigned long long) fsinfo.objectid_information.out.unknown[2],
1838 (unsigned long long) fsinfo.objectid_information.out.unknown[3],
1839 (unsigned long long) fsinfo.objectid_information.out.unknown[4],
1840 (unsigned long long) fsinfo.objectid_information.out.unknown[5],
1841 (unsigned long long) fsinfo.objectid_information.out.unknown[6] );
1846 talloc_free(mem_ctx);
1850 /****************************************************************************
1851 show as much information as possible about a file
1852 ****************************************************************************/
1853 static int cmd_allinfo(const char **cmd_ptr)
1858 TALLOC_CTX *mem_ctx;
1859 union smb_fileinfo finfo;
1862 pstrcpy(fname,cur_dir);
1864 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1865 d_printf("allinfo <filename>\n");
1870 mem_ctx = talloc_init("%s", fname);
1872 /* first a ALL_INFO QPATHINFO */
1873 finfo.generic.level = RAW_FILEINFO_ALL_INFO;
1874 finfo.generic.in.fname = fname;
1875 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1876 if (!NT_STATUS_IS_OK(status)) {
1877 d_printf("%s - %s\n", fname, nt_errstr(status));
1882 d_printf("\tcreate_time: %s\n", nt_time_string(mem_ctx, finfo.all_info.out.create_time));
1883 d_printf("\taccess_time: %s\n", nt_time_string(mem_ctx, finfo.all_info.out.access_time));
1884 d_printf("\twrite_time: %s\n", nt_time_string(mem_ctx, finfo.all_info.out.write_time));
1885 d_printf("\tchange_time: %s\n", nt_time_string(mem_ctx, finfo.all_info.out.change_time));
1886 d_printf("\tattrib: 0x%x\n", finfo.all_info.out.attrib);
1887 d_printf("\talloc_size: %lu\n", (unsigned long)finfo.all_info.out.alloc_size);
1888 d_printf("\tsize: %lu\n", (unsigned long)finfo.all_info.out.size);
1889 d_printf("\tnlink: %u\n", finfo.all_info.out.nlink);
1890 d_printf("\tdelete_pending: %u\n", finfo.all_info.out.delete_pending);
1891 d_printf("\tdirectory: %u\n", finfo.all_info.out.directory);
1892 d_printf("\tea_size: %u\n", finfo.all_info.out.ea_size);
1893 d_printf("\tfname: '%s'\n", finfo.all_info.out.fname.s);
1895 /* 8.3 name if any */
1896 finfo.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
1897 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1898 if (NT_STATUS_IS_OK(status)) {
1899 d_printf("\talt_name: %s\n", finfo.alt_name_info.out.fname.s);
1902 /* file_id if available */
1903 finfo.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
1904 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1905 if (NT_STATUS_IS_OK(status)) {
1906 d_printf("\tfile_id %.0f\n",
1907 (double)finfo.internal_information.out.file_id);
1910 /* the EAs, if any */
1911 finfo.generic.level = RAW_FILEINFO_ALL_EAS;
1912 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1913 if (NT_STATUS_IS_OK(status)) {
1915 for (i=0;i<finfo.all_eas.out.num_eas;i++) {
1916 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
1917 finfo.all_eas.out.eas[i].flags,
1918 (int)finfo.all_eas.out.eas[i].value.length,
1919 finfo.all_eas.out.eas[i].name.s);
1923 /* streams, if available */
1924 finfo.generic.level = RAW_FILEINFO_STREAM_INFO;
1925 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1926 if (NT_STATUS_IS_OK(status)) {
1928 for (i=0;i<finfo.stream_info.out.num_streams;i++) {
1929 d_printf("\tstream %d:\n", i);
1930 d_printf("\t\tsize %ld\n",
1931 (long)finfo.stream_info.out.streams[i].size);
1932 d_printf("\t\talloc size %ld\n",
1933 (long)finfo.stream_info.out.streams[i].alloc_size);
1934 d_printf("\t\tname %s\n", finfo.stream_info.out.streams[i].stream_name.s);
1938 /* dev/inode if available */
1939 finfo.generic.level = RAW_FILEINFO_COMPRESSION_INFORMATION;
1940 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1941 if (NT_STATUS_IS_OK(status)) {
1942 d_printf("\tcompressed size %ld\n", (long)finfo.compression_info.out.compressed_size);
1943 d_printf("\tformat %ld\n", (long)finfo.compression_info.out.format);
1944 d_printf("\tunit_shift %ld\n", (long)finfo.compression_info.out.unit_shift);
1945 d_printf("\tchunk_shift %ld\n", (long)finfo.compression_info.out.chunk_shift);
1946 d_printf("\tcluster_shift %ld\n", (long)finfo.compression_info.out.cluster_shift);
1949 talloc_free(mem_ctx);
1956 /****************************************************************************
1958 ****************************************************************************/
1959 static int cmd_eainfo(const char **cmd_ptr)
1964 TALLOC_CTX *mem_ctx;
1965 union smb_fileinfo finfo;
1969 pstrcpy(fname,cur_dir);
1971 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1972 d_printf("eainfo <filename>\n");
1977 mem_ctx = talloc_init("%s", fname);
1979 finfo.generic.in.fname = fname;
1980 finfo.generic.level = RAW_FILEINFO_ALL_EAS;
1981 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1983 if (!NT_STATUS_IS_OK(status)) {
1984 d_printf("RAW_FILEINFO_ALL_EAS - %s\n", nt_errstr(status));
1985 talloc_free(mem_ctx);
1989 d_printf("%s has %d EAs\n", fname, finfo.all_eas.out.num_eas);
1991 for (i=0;i<finfo.all_eas.out.num_eas;i++) {
1992 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i,
1993 finfo.all_eas.out.eas[i].flags,
1994 (int)finfo.all_eas.out.eas[i].value.length,
1995 finfo.all_eas.out.eas[i].name.s);
1998 finfo.all_eas.out.eas[i].value.data,
1999 finfo.all_eas.out.eas[i].value.length);
2002 talloc_free(mem_ctx);
2008 /****************************************************************************
2009 show any ACL on a file
2010 ****************************************************************************/
2011 static int cmd_acl(const char **cmd_ptr)
2016 TALLOC_CTX *mem_ctx;
2017 union smb_fileinfo query;
2021 pstrcpy(fname,cur_dir);
2023 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2024 d_printf("acl <filename>\n");
2029 fnum = smbcli_nt_create_full(cli->tree, fname, 0,
2030 SEC_STD_READ_CONTROL,
2032 NTCREATEX_SHARE_ACCESS_DELETE|
2033 NTCREATEX_SHARE_ACCESS_READ|
2034 NTCREATEX_SHARE_ACCESS_WRITE,
2035 NTCREATEX_DISP_OPEN,
2038 d_printf("%s - %s\n", fname, smbcli_errstr(cli->tree));
2042 mem_ctx = talloc_init("%s", fname);
2044 query.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
2045 query.query_secdesc.in.fnum = fnum;
2046 query.query_secdesc.secinfo_flags = 0x7;
2048 status = smb_raw_fileinfo(cli->tree, mem_ctx, &query);
2049 if (!NT_STATUS_IS_OK(status)) {
2050 d_printf("%s - %s\n", fname, nt_errstr(status));
2055 NDR_PRINT_DEBUG(security_descriptor, query.query_secdesc.out.sd);
2057 talloc_free(mem_ctx);
2063 /****************************************************************************
2064 lookup a name or sid
2065 ****************************************************************************/
2066 static int cmd_lookup(const char **cmd_ptr)
2069 TALLOC_CTX *mem_ctx = talloc_new(NULL);
2071 struct dom_sid *sid;
2073 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2074 d_printf("lookup <sid|name>\n");
2075 talloc_free(mem_ctx);
2079 sid = dom_sid_parse_talloc(mem_ctx, buf);
2082 status = smblsa_lookup_name(cli, buf, mem_ctx, &sidstr);
2083 if (!NT_STATUS_IS_OK(status)) {
2084 d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2085 talloc_free(mem_ctx);
2089 d_printf("%s\n", sidstr);
2092 status = smblsa_lookup_sid(cli, buf, mem_ctx, &name);
2093 if (!NT_STATUS_IS_OK(status)) {
2094 d_printf("lsa_LookupSids - %s\n", nt_errstr(status));
2095 talloc_free(mem_ctx);
2099 d_printf("%s\n", name);
2102 talloc_free(mem_ctx);
2107 /****************************************************************************
2108 show privileges for a user
2109 ****************************************************************************/
2110 static int cmd_privileges(const char **cmd_ptr)
2113 TALLOC_CTX *mem_ctx = talloc_new(NULL);
2115 struct dom_sid *sid;
2116 struct lsa_RightSet rights;
2119 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2120 d_printf("privileges <sid|name>\n");
2121 talloc_free(mem_ctx);
2125 sid = dom_sid_parse_talloc(mem_ctx, buf);
2127 const char *sid_str;
2128 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
2129 if (!NT_STATUS_IS_OK(status)) {
2130 d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2131 talloc_free(mem_ctx);
2134 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
2137 status = smblsa_sid_privileges(cli, sid, mem_ctx, &rights);
2138 if (!NT_STATUS_IS_OK(status)) {
2139 d_printf("lsa_EnumAccountRights - %s\n", nt_errstr(status));
2140 talloc_free(mem_ctx);
2144 for (i=0;i<rights.count;i++) {
2145 d_printf("\t%s\n", rights.names[i].string);
2148 talloc_free(mem_ctx);
2154 /****************************************************************************
2155 add privileges for a user
2156 ****************************************************************************/
2157 static int cmd_addprivileges(const char **cmd_ptr)
2160 TALLOC_CTX *mem_ctx = talloc_new(NULL);
2162 struct dom_sid *sid;
2163 struct lsa_RightSet rights;
2165 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2166 d_printf("addprivileges <sid|name> <privilege...>\n");
2167 talloc_free(mem_ctx);
2171 sid = dom_sid_parse_talloc(mem_ctx, buf);
2173 const char *sid_str;
2174 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
2175 if (!NT_STATUS_IS_OK(status)) {
2176 d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2177 talloc_free(mem_ctx);
2180 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
2183 ZERO_STRUCT(rights);
2184 while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2185 rights.names = talloc_realloc(mem_ctx, rights.names,
2186 struct lsa_String, rights.count+1);
2187 rights.names[rights.count].string = talloc_strdup(mem_ctx, buf);
2192 status = smblsa_sid_add_privileges(cli, sid, mem_ctx, &rights);
2193 if (!NT_STATUS_IS_OK(status)) {
2194 d_printf("lsa_AddAccountRights - %s\n", nt_errstr(status));
2195 talloc_free(mem_ctx);
2199 talloc_free(mem_ctx);
2204 /****************************************************************************
2205 delete privileges for a user
2206 ****************************************************************************/
2207 static int cmd_delprivileges(const char **cmd_ptr)
2210 TALLOC_CTX *mem_ctx = talloc_new(NULL);
2212 struct dom_sid *sid;
2213 struct lsa_RightSet rights;
2215 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2216 d_printf("delprivileges <sid|name> <privilege...>\n");
2217 talloc_free(mem_ctx);
2221 sid = dom_sid_parse_talloc(mem_ctx, buf);
2223 const char *sid_str;
2224 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
2225 if (!NT_STATUS_IS_OK(status)) {
2226 d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2227 talloc_free(mem_ctx);
2230 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
2233 ZERO_STRUCT(rights);
2234 while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2235 rights.names = talloc_realloc(mem_ctx, rights.names,
2236 struct lsa_String, rights.count+1);
2237 rights.names[rights.count].string = talloc_strdup(mem_ctx, buf);
2242 status = smblsa_sid_del_privileges(cli, sid, mem_ctx, &rights);
2243 if (!NT_STATUS_IS_OK(status)) {
2244 d_printf("lsa_RemoveAccountRights - %s\n", nt_errstr(status));
2245 talloc_free(mem_ctx);
2249 talloc_free(mem_ctx);
2255 /****************************************************************************
2256 ****************************************************************************/
2257 static int cmd_open(const char **cmd_ptr)
2262 pstrcpy(mask,cur_dir);
2264 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2265 d_printf("open <filename>\n");
2270 smbcli_open(cli->tree, mask, O_RDWR, DENY_ALL);
2276 /****************************************************************************
2278 ****************************************************************************/
2279 static int cmd_rmdir(const char **cmd_ptr)
2284 pstrcpy(mask,cur_dir);
2286 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2287 d_printf("rmdir <dirname>\n");
2292 if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, mask))) {
2293 d_printf("%s removing remote directory file %s\n",
2294 smbcli_errstr(cli->tree),mask);
2300 /****************************************************************************
2302 ****************************************************************************/
2303 static int cmd_link(const char **cmd_ptr)
2308 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2309 d_printf("Server doesn't support UNIX CIFS calls.\n");
2313 pstrcpy(src,cur_dir);
2314 pstrcpy(dest,cur_dir);
2316 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2317 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2318 d_printf("link <src> <dest>\n");
2325 if (NT_STATUS_IS_ERR(smbcli_unix_hardlink(cli->tree, src, dest))) {
2326 d_printf("%s linking files (%s -> %s)\n", smbcli_errstr(cli->tree), src, dest);
2333 /****************************************************************************
2335 ****************************************************************************/
2337 static int cmd_symlink(const char **cmd_ptr)
2342 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2343 d_printf("Server doesn't support UNIX CIFS calls.\n");
2347 pstrcpy(src,cur_dir);
2348 pstrcpy(dest,cur_dir);
2350 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2351 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2352 d_printf("symlink <src> <dest>\n");
2359 if (NT_STATUS_IS_ERR(smbcli_unix_symlink(cli->tree, src, dest))) {
2360 d_printf("%s symlinking files (%s -> %s)\n",
2361 smbcli_errstr(cli->tree), src, dest);
2368 /****************************************************************************
2370 ****************************************************************************/
2372 static int cmd_chmod(const char **cmd_ptr)
2378 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2379 d_printf("Server doesn't support UNIX CIFS calls.\n");
2383 pstrcpy(src,cur_dir);
2385 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2386 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2387 d_printf("chmod mode file\n");
2391 mode = (mode_t)strtol(buf, NULL, 8);
2394 if (NT_STATUS_IS_ERR(smbcli_unix_chmod(cli->tree, src, mode))) {
2395 d_printf("%s chmod file %s 0%o\n",
2396 smbcli_errstr(cli->tree), src, (uint_t)mode);
2403 /****************************************************************************
2405 ****************************************************************************/
2407 static int cmd_chown(const char **cmd_ptr)
2412 fstring buf, buf2, buf3;
2414 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2415 d_printf("Server doesn't support UNIX CIFS calls.\n");
2419 pstrcpy(src,cur_dir);
2421 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2422 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2)) ||
2423 !next_token(cmd_ptr,buf3,NULL, sizeof(buf3))) {
2424 d_printf("chown uid gid file\n");
2428 uid = (uid_t)atoi(buf);
2429 gid = (gid_t)atoi(buf2);
2432 if (NT_STATUS_IS_ERR(smbcli_unix_chown(cli->tree, src, uid, gid))) {
2433 d_printf("%s chown file %s uid=%d, gid=%d\n",
2434 smbcli_errstr(cli->tree), src, (int)uid, (int)gid);
2441 /****************************************************************************
2443 ****************************************************************************/
2444 static int cmd_rename(const char **cmd_ptr)
2449 pstrcpy(src,cur_dir);
2450 pstrcpy(dest,cur_dir);
2452 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2453 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2454 d_printf("rename <src> <dest>\n");
2461 if (NT_STATUS_IS_ERR(smbcli_rename(cli->tree, src, dest))) {
2462 d_printf("%s renaming files\n",smbcli_errstr(cli->tree));
2470 /****************************************************************************
2471 toggle the prompt flag
2472 ****************************************************************************/
2473 static int cmd_prompt(const char **cmd_ptr)
2476 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2482 /****************************************************************************
2483 set the newer than time
2484 ****************************************************************************/
2485 static int cmd_newer(const char **cmd_ptr)
2491 ok = next_token(cmd_ptr,buf,NULL,sizeof(buf));
2492 if (ok && (stat(buf,&sbuf) == 0)) {
2493 newer_than = sbuf.st_mtime;
2494 DEBUG(1,("Getting files newer than %s",
2495 asctime(localtime(&newer_than))));
2500 if (ok && newer_than == 0) {
2501 d_printf("Error setting newer-than time\n");
2508 /****************************************************************************
2509 set the archive level
2510 ****************************************************************************/
2511 static int cmd_archive(const char **cmd_ptr)
2515 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2516 archive_level = atoi(buf);
2518 d_printf("Archive level is %d\n",archive_level);
2523 /****************************************************************************
2524 toggle the lowercaseflag
2525 ****************************************************************************/
2526 static int cmd_lowercase(const char **cmd_ptr)
2528 lowercase = !lowercase;
2529 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2537 /****************************************************************************
2538 toggle the recurse flag
2539 ****************************************************************************/
2540 static int cmd_recurse(const char **cmd_ptr)
2543 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2548 /****************************************************************************
2549 toggle the translate flag
2550 ****************************************************************************/
2551 static int cmd_translate(const char **cmd_ptr)
2553 translation = !translation;
2554 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2555 translation?"on":"off"));
2561 /****************************************************************************
2562 do a printmode command
2563 ****************************************************************************/
2564 static int cmd_printmode(const char **cmd_ptr)
2569 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2570 if (strequal(buf,"text")) {
2573 if (strequal(buf,"graphics"))
2576 printmode = atoi(buf);
2583 fstrcpy(mode,"text");
2586 fstrcpy(mode,"graphics");
2589 slprintf(mode,sizeof(mode)-1,"%d",printmode);
2593 DEBUG(2,("the printmode is now %s\n",mode));
2598 /****************************************************************************
2600 ****************************************************************************/
2601 static int cmd_lcd(const char **cmd_ptr)
2606 if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
2608 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2613 /****************************************************************************
2614 get a file restarting at end of local file
2615 ****************************************************************************/
2616 static int cmd_reget(const char **cmd_ptr)
2619 pstring remote_name;
2622 pstrcpy(remote_name, cur_dir);
2623 pstrcat(remote_name, "\\");
2625 p = remote_name + strlen(remote_name);
2627 if (!next_token(cmd_ptr, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2628 d_printf("reget <filename>\n");
2631 pstrcpy(local_name, p);
2632 dos_clean_name(remote_name);
2634 next_token(cmd_ptr, local_name, NULL, sizeof(local_name));
2636 return do_get(remote_name, local_name, True);
2639 /****************************************************************************
2640 put a file restarting at end of local file
2641 ****************************************************************************/
2642 static int cmd_reput(const char **cmd_ptr)
2645 pstring remote_name;
2649 pstrcpy(remote_name, cur_dir);
2650 pstrcat(remote_name, "\\");
2652 if (!next_token(cmd_ptr, p, NULL, sizeof(buf))) {
2653 d_printf("reput <filename>\n");
2656 pstrcpy(local_name, p);
2658 if (!file_exist(local_name)) {
2659 d_printf("%s does not exist\n", local_name);
2663 if (next_token(cmd_ptr, p, NULL, sizeof(buf)))
2664 pstrcat(remote_name, p);
2666 pstrcat(remote_name, local_name);
2668 dos_clean_name(remote_name);
2670 return do_put(remote_name, local_name, True);
2675 return a string representing a share type
2677 static const char *share_type_str(uint32_t type)
2679 switch (type & 0xF) {
2680 case STYPE_DISKTREE:
2695 display a list of shares from a level 1 share enum
2697 static void display_share_result(struct srvsvc_NetShareCtr1 *ctr1)
2701 for (i=0;i<ctr1->count;i++) {
2702 struct srvsvc_NetShareInfo1 *info = ctr1->array+i;
2704 printf("\t%-15s %-10.10s %s\n",
2706 share_type_str(info->type),
2713 /****************************************************************************
2714 try and browse available shares on a host
2715 ****************************************************************************/
2716 static BOOL browse_host(const char *query_host)
2718 struct dcerpc_pipe *p;
2721 struct srvsvc_NetShareEnumAll r;
2722 uint32_t resume_handle = 0;
2723 TALLOC_CTX *mem_ctx = talloc_init("browse_host");
2724 struct srvsvc_NetShareCtr1 ctr1;
2726 binding = talloc_asprintf(mem_ctx, "ncacn_np:%s", query_host);
2728 status = dcerpc_pipe_connect(mem_ctx, &p, binding,
2730 DCERPC_SRVSVC_VERSION,
2731 cmdline_credentials, NULL);
2732 if (!NT_STATUS_IS_OK(status)) {
2733 d_printf("Failed to connect to %s - %s\n",
2734 binding, nt_errstr(status));
2735 talloc_free(mem_ctx);
2739 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
2741 r.in.ctr.ctr1 = &ctr1;
2742 r.in.max_buffer = ~0;
2743 r.in.resume_handle = &resume_handle;
2745 d_printf("\n\tSharename Type Comment\n");
2746 d_printf("\t--------- ---- -------\n");
2750 status = dcerpc_srvsvc_NetShareEnumAll(p, mem_ctx, &r);
2752 if (NT_STATUS_IS_OK(status) &&
2753 (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA) ||
2754 W_ERROR_IS_OK(r.out.result)) &&
2756 display_share_result(r.out.ctr.ctr1);
2757 resume_handle += r.out.ctr.ctr1->count;
2759 } while (NT_STATUS_IS_OK(status) && W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
2761 talloc_free(mem_ctx);
2763 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2764 d_printf("Failed NetShareEnumAll %s - %s/%s\n",
2765 binding, nt_errstr(status), win_errstr(r.out.result));
2772 /****************************************************************************
2773 try and browse available connections on a host
2774 ****************************************************************************/
2775 static BOOL list_servers(const char *wk_grp)
2777 d_printf("REWRITE: list servers not implemented\n");
2781 /* Some constants for completing filename arguments */
2783 #define COMPL_NONE 0 /* No completions */
2784 #define COMPL_REMOTE 1 /* Complete remote filename */
2785 #define COMPL_LOCAL 2 /* Complete local filename */
2787 /* This defines the commands supported by this client.
2788 * NOTE: The "!" must be the last one in the list because it's fn pointer
2789 * field is NULL, and NULL in that field is used in process_tok()
2790 * (below) to indicate the end of the list. crh
2795 int (*fn)(const char **cmd_ptr);
2796 const char *description;
2797 char compl_args[2]; /* Completion argument info */
2800 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2801 {"addprivileges",cmd_addprivileges,"<sid|name> <privilege...> add privileges for a user",{COMPL_NONE,COMPL_NONE}},
2802 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2803 {"acl",cmd_acl,"<file> show file ACL",{COMPL_NONE,COMPL_NONE}},
2804 {"allinfo",cmd_allinfo,"<file> show all possible info about a file",{COMPL_NONE,COMPL_NONE}},
2805 {"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}},
2806 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2807 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2808 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2809 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2810 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2811 {"delprivileges",cmd_delprivileges,"<sid|name> <privilege...> remove privileges for a user",{COMPL_NONE,COMPL_NONE}},
2812 {"deltree",cmd_deltree,"<dir> delete a whole directory tree",{COMPL_REMOTE,COMPL_NONE}},
2813 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2814 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2815 {"eainfo",cmd_eainfo,"<file> show EA contents for a file",{COMPL_NONE,COMPL_NONE}},
2816 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2817 {"fsinfo",cmd_fsinfo,"query file system info",{COMPL_NONE,COMPL_NONE}},
2818 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2819 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2820 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2821 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2822 {"link",cmd_link,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2823 {"lookup",cmd_lookup,"<sid|name> show SID for name or name for SID",{COMPL_NONE,COMPL_NONE}},
2824 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2825 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2826 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2827 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2828 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2829 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2830 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
2831 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2832 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2833 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2834 {"privileges",cmd_privileges,"<user> show privileges for a user",{COMPL_NONE,COMPL_NONE}},
2835 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2836 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
2837 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2838 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2839 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2840 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2841 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2842 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2843 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2844 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2845 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2846 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2847 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2848 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2849 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2850 {"symlink",cmd_symlink,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2851 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2853 /* Yes, this must be here, see crh's comment above. */
2854 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2855 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2859 /*******************************************************************
2860 lookup a command string in the list of commands, including
2862 ******************************************************************/
2863 static int process_tok(fstring tok)
2865 int i = 0, matches = 0;
2867 int tok_len = strlen(tok);
2869 while (commands[i].fn != NULL) {
2870 if (strequal(commands[i].name,tok)) {
2874 } else if (strncasecmp(commands[i].name, tok, tok_len) == 0) {
2883 else if (matches == 1)
2889 /****************************************************************************
2891 ****************************************************************************/
2892 static int cmd_help(const char **cmd_ptr)
2897 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2898 if ((i = process_tok(buf)) >= 0)
2899 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2901 while (commands[i].description) {
2902 for (j=0; commands[i].description && (j<5); j++) {
2903 d_printf("%-15s",commands[i].name);
2912 /****************************************************************************
2913 process a -c command string
2914 ****************************************************************************/
2915 static int process_command_string(char *cmd)
2921 /* establish the connection if not already */
2924 cli = do_connect(desthost, service, cmdline_credentials);
2929 while (cmd[0] != '\0') {
2934 if ((p = strchr_m(cmd, ';')) == 0) {
2935 strncpy(line, cmd, 999);
2939 if (p - cmd > 999) p = cmd + 999;
2940 strncpy(line, cmd, p - cmd);
2941 line[p - cmd] = '\0';
2945 /* and get the first part of the command */
2947 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
2949 if ((i = process_tok(tok)) >= 0) {
2950 rc = commands[i].fn(&ptr);
2951 } else if (i == -2) {
2952 d_printf("%s: command abbreviation ambiguous\n",tok);
2954 d_printf("%s: command not found\n",tok);
2961 #define MAX_COMPLETIONS 100
2969 } completion_remote_t;
2971 static void completion_remote_filter(struct clilist_file_info *f, const char *mask, void *state)
2973 completion_remote_t *info = (completion_remote_t *)state;
2975 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2976 if ((info->dirmask[0] == 0) && !(f->attrib & FILE_ATTRIBUTE_DIRECTORY))
2977 info->matches[info->count] = strdup(f->name);
2981 if (info->dirmask[0] != 0)
2982 pstrcpy(tmp, info->dirmask);
2985 pstrcat(tmp, f->name);
2986 if (f->attrib & FILE_ATTRIBUTE_DIRECTORY)
2988 info->matches[info->count] = strdup(tmp);
2990 if (info->matches[info->count] == NULL)
2992 if (f->attrib & FILE_ATTRIBUTE_DIRECTORY)
2993 smb_readline_ca_char(0);
2995 if (info->count == 1)
2996 info->samelen = strlen(info->matches[info->count]);
2998 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
3004 static char **remote_completion(const char *text, int len)
3008 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
3014 if (len >= PATH_MAX)
3017 info.matches = malloc_array_p(char *, MAX_COMPLETIONS);
3018 if (!info.matches) return NULL;
3019 info.matches[0] = NULL;
3021 for (i = len-1; i >= 0; i--)
3022 if ((text[i] == '/') || (text[i] == '\\'))
3024 info.text = text+i+1;
3025 info.samelen = info.len = len-i-1;
3028 strncpy(info.dirmask, text, i+1);
3029 info.dirmask[i+1] = 0;
3030 snprintf(dirmask, sizeof(dirmask), "%s%*s*", cur_dir, i-1, text);
3032 snprintf(dirmask, sizeof(dirmask), "%s*", cur_dir);
3034 if (smbcli_list(cli->tree, dirmask,
3035 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
3036 completion_remote_filter, &info) < 0)
3039 if (info.count == 2)
3040 info.matches[0] = strdup(info.matches[1]);
3042 info.matches[0] = malloc(info.samelen+1);
3043 if (!info.matches[0])
3045 strncpy(info.matches[0], info.matches[1], info.samelen);
3046 info.matches[0][info.samelen] = 0;
3048 info.matches[info.count] = NULL;
3049 return info.matches;
3052 for (i = 0; i < info.count; i++)
3053 free(info.matches[i]);
3058 static char **completion_fn(const char *text, int start, int end)
3060 smb_readline_ca_char(' ');
3063 const char *buf, *sp;
3067 buf = smb_readline_get_line_buffer();
3071 sp = strchr(buf, ' ');
3075 for (i = 0; commands[i].name; i++)
3076 if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
3078 if (commands[i].name == NULL)
3084 if (sp == (buf + start))
3085 compl_type = commands[i].compl_args[0];
3087 compl_type = commands[i].compl_args[1];
3089 if (compl_type == COMPL_REMOTE)
3090 return remote_completion(text, end - start);
3091 else /* fall back to local filename completion */
3095 int i, len, samelen = 0, count=1;
3097 matches = malloc_array_p(char *, MAX_COMPLETIONS);
3098 if (!matches) return NULL;
3102 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3103 if (strncmp(text, commands[i].name, len) == 0) {
3104 matches[count] = strdup(commands[i].name);
3105 if (!matches[count])
3108 samelen = strlen(matches[count]);
3110 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3117 case 0: /* should never happen */
3121 matches[0] = strdup(matches[1]);
3124 matches[0] = malloc(samelen+1);
3127 strncpy(matches[0], matches[1], samelen);
3128 matches[0][samelen] = 0;
3130 matches[count] = NULL;
3143 /****************************************************************************
3144 make sure we swallow keepalives during idle time
3145 ****************************************************************************/
3146 static void readline_callback(void)
3148 static time_t last_t;
3153 if (t - last_t < 5) return;
3157 smbcli_transport_process(cli->transport);
3160 smbcli_chkpath(cli->tree, "\\");
3165 /****************************************************************************
3166 process commands on stdin
3167 ****************************************************************************/
3168 static void process_stdin(void)
3178 /* display a prompt */
3179 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3180 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3184 pstrcpy(line, cline);
3186 /* special case - first char is ! */
3192 /* and get the first part of the command */
3194 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
3196 if ((i = process_tok(tok)) >= 0) {
3197 commands[i].fn(&ptr);
3198 } else if (i == -2) {
3199 d_printf("%s: command abbreviation ambiguous\n",tok);
3201 d_printf("%s: command not found\n",tok);
3207 /*****************************************************
3208 return a connection to a server
3209 *******************************************************/
3210 static struct smbcli_state *do_connect(const char *server, const char *share, struct cli_credentials *cred)
3212 struct smbcli_state *c;
3215 if (strncmp(share, "\\\\", 2) == 0 ||
3216 strncmp(share, "//", 2) == 0) {
3217 smbcli_parse_unc(share, NULL, &server, &share);
3220 status = smbcli_full_connection(NULL, &c, server,
3221 share, NULL, cred, NULL);
3222 if (!NT_STATUS_IS_OK(status)) {
3223 d_printf("Connection to \\\\%s\\%s failed - %s\n",
3224 server, share, nt_errstr(status));
3232 /****************************************************************************
3233 process commands from the client
3234 ****************************************************************************/
3235 static int process(char *base_directory)
3239 cli = do_connect(desthost, service, cmdline_credentials);
3244 if (*base_directory) do_cd(base_directory);
3247 rc = process_command_string(cmdstr);
3256 /****************************************************************************
3258 ****************************************************************************/
3259 static int do_host_query(char *query_host)
3261 browse_host(query_host);
3262 list_servers(lp_workgroup());
3267 /****************************************************************************
3268 handle a message operation
3269 ****************************************************************************/
3270 static int do_message_op(void)
3272 struct nbt_name called, calling;
3273 const char *server_name;
3275 make_nbt_name_client(&calling, lp_netbios_name());
3277 nbt_choose_called_name(NULL, &called, desthost, name_type);
3279 server_name = dest_ip ? dest_ip : desthost;
3281 if (!(cli=smbcli_state_init(NULL)) || !smbcli_socket_connect(cli, server_name)) {
3282 d_printf("Connection to %s failed\n", server_name);
3286 if (!smbcli_transport_establish(cli, &calling, &called)) {
3287 d_printf("session request failed\n");
3300 * Process "-L hostname" option.
3302 * We don't actually do anything yet -- we just stash the name in a
3303 * global variable and do the query when all options have been read.
3305 static void remember_query_host(const char *arg,
3310 while (*arg == '\\' || *arg == '/')
3312 pstrcpy(query_host, arg);
3313 if ((slash = strchr(query_host, '/'))
3314 || (slash = strchr(query_host, '\\'))) {
3320 /****************************************************************************
3322 ****************************************************************************/
3323 int main(int argc,char *argv[])
3325 fstring base_directory;
3328 BOOL message = False;
3333 TALLOC_CTX *mem_ctx;
3334 struct poptOption long_options[] = {
3337 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3338 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3339 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3340 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3341 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3342 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3343 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3344 { "send-buffer", 'b', POPT_ARG_INT, NULL, 'b', "Changes the transmit/send buffer", "BYTES" },
3345 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3347 POPT_COMMON_CONNECTION
3348 POPT_COMMON_CREDENTIALS
3354 pstrcpy(term_code, KANJI);
3360 *base_directory = 0;
3362 mem_ctx = talloc_init("client.c/main");
3364 d_printf("\nclient.c: Not enough memory\n");
3368 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
3369 poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
3371 while ((opt = poptGetNextOpt(pc)) != -1) {
3374 /* Messages are sent to NetBIOS name type 0x3
3375 * (Messenger Service). Make sure we default
3376 * to port 139 instead of port 445. srl,crh
3379 pstrcpy(desthost,poptGetOptArg(pc));
3380 if( 0 == port ) port = 139;
3384 dest_ip = poptGetOptArg(pc);
3387 remember_query_host(poptGetOptArg(pc), query_host);
3390 pstrcpy(term_code, poptGetOptArg(pc));
3393 fstrcpy(base_directory,poptGetOptArg(pc));
3396 io_bufsize = MAX(1, atoi(poptGetOptArg(pc)));
3401 smbclient_init_subsystems;
3403 if(poptPeekArg(pc)) {
3404 pstrcpy(service,poptGetArg(pc));
3405 /* Convert any '/' characters in the service name to '\' characters */
3406 string_replace(service, '/','\\');
3408 if (count_chars(service,'\\') < 3) {
3409 d_printf("\n%s: Not enough '\\' characters in service\n",service);
3410 poptPrintUsage(pc, stderr, 0);
3415 if (poptPeekArg(pc)) {
3416 cli_credentials_set_password(cmdline_credentials, poptGetArg(pc), CRED_SPECIFIED);
3421 if (!*query_host && !*service && !message) {
3422 poptPrintUsage(pc, stderr, 0);
3426 poptFreeContext(pc);
3428 DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION_STRING ) );
3430 talloc_free(mem_ctx);
3432 if ((p=strchr_m(query_host,'#'))) {
3435 sscanf(p, "%x", &name_type);
3439 return do_host_query(query_host);
3443 return do_message_op();
3446 if (process(base_directory)) {