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) Gerald (Jerry) Carter 2004
8 Copyright (C) Jeremy Allison 1994-2007
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "client/client_proto.h"
26 #include "include/rpc_client.h"
31 extern int do_smb_browse(void); /* mDNS browsing */
33 extern bool AllowDebugChange;
34 extern bool override_logfile;
36 extern bool in_client;
40 static char *desthost;
41 static char *calling_name;
42 static bool grepable = false;
43 static char *cmdstr = NULL;
44 static const char *cmd_ptr = NULL;
46 static int io_bufsize = 64512;
48 static int name_type = 0x20;
49 extern int max_protocol;
51 static int process_tok(char *tok);
52 static int cmd_help(void);
54 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
56 /* 30 second timeout on most commands */
57 #define CLIENT_TIMEOUT (30*1000)
58 #define SHORT_TIMEOUT (5*1000)
60 /* value for unused fid field in trans2 secondary request */
61 #define FID_UNUSED (0xFFFF)
63 time_t newer_than = 0;
64 static int archive_level = 0;
66 static bool translation = false;
69 /* clitar bits insert */
72 extern bool tar_reset;
75 static bool prompt = true;
77 static bool recurse = false;
78 static bool showacls = false;
79 bool lowercase = false;
81 static struct sockaddr_storage dest_ss;
83 #define SEPARATORS " \t\n\r"
85 static bool abort_mget = true;
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;
96 /* root cli_state connection */
98 struct cli_state *cli;
100 static char CLI_DIRSEP_CHAR = '\\';
101 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
103 /* Accessor functions for directory paths. */
104 static char *fileselection;
105 static const char *client_get_fileselection(void)
108 return fileselection;
113 static const char *client_set_fileselection(const char *new_fs)
115 SAFE_FREE(fileselection);
117 fileselection = SMB_STRDUP(new_fs);
119 return client_get_fileselection();
123 static const char *client_get_cwd(void)
128 return CLI_DIRSEP_STR;
131 static const char *client_set_cwd(const char *new_cwd)
135 cwd = SMB_STRDUP(new_cwd);
137 return client_get_cwd();
140 static char *cur_dir;
141 const char *client_get_cur_dir(void)
146 return CLI_DIRSEP_STR;
149 const char *client_set_cur_dir(const char *newdir)
153 cur_dir = SMB_STRDUP(newdir);
155 return client_get_cur_dir();
158 /****************************************************************************
159 Write to a local file with CR/LF->LF translation if appropriate. Return the
160 number taken from the buffer. This may not equal the number written.
161 ****************************************************************************/
163 static int writefile(int f, char *b, int n)
173 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
176 if (write(f, b, 1) != 1) {
186 /****************************************************************************
187 Read from a file with LF->CR/LF translation if appropriate. Return the
188 number read. read approx n bytes.
189 ****************************************************************************/
191 static int readfile(char *b, int n, XFILE *f)
197 return x_fread(b,1,n,f);
200 while (i < (n - 1) && (i < BUFFER_SIZE)) {
201 if ((c = x_getc(f)) == EOF) {
205 if (c == '\n') { /* change all LFs to CR/LF */
215 /****************************************************************************
217 ****************************************************************************/
219 static void send_message(void)
224 if (!cli_message_start(cli, desthost,
225 get_cmdline_auth_info_username(), &grp_id)) {
226 d_printf("message start: %s\n", cli_errstr(cli));
231 d_printf("Connected. Type your message, ending it with a Control-D\n");
233 while (!feof(stdin) && total_len < 1600) {
234 int maxlen = MIN(1600 - total_len,127);
241 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
247 if ((total_len > 0) && (strlen(msg) == 0)) {
251 if (!cli_message_text(cli, msg, l, grp_id)) {
252 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
259 if (total_len >= 1600)
260 d_printf("the message was truncated to 1600 bytes\n");
262 d_printf("sent %d bytes\n",total_len);
264 if (!cli_message_end(cli, grp_id)) {
265 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
270 /****************************************************************************
271 Check the space on a device.
272 ****************************************************************************/
274 static int do_dskattr(void)
276 int total, bsize, avail;
277 struct cli_state *targetcli = NULL;
278 char *targetpath = NULL;
279 TALLOC_CTX *ctx = talloc_tos();
281 if ( !cli_resolve_path(ctx, "", cli, client_get_cur_dir(), &targetcli, &targetpath)) {
282 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
286 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
287 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
291 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
292 total, bsize, avail);
297 /****************************************************************************
299 ****************************************************************************/
301 static int cmd_pwd(void)
303 d_printf("Current directory is %s",service);
304 d_printf("%s\n",client_get_cur_dir());
308 /****************************************************************************
309 Change directory - inner section.
310 ****************************************************************************/
312 static int do_cd(const char *new_dir)
315 char *saved_dir = NULL;
317 char *targetpath = NULL;
318 struct cli_state *targetcli = NULL;
319 SMB_STRUCT_STAT sbuf;
322 TALLOC_CTX *ctx = talloc_stackframe();
324 newdir = talloc_strdup(ctx, new_dir);
329 string_replace(newdir,'/','\\');
331 /* Save the current directory in case the new directory is invalid */
333 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
339 if (*newdir == CLI_DIRSEP_CHAR) {
340 client_set_cur_dir(newdir);
343 new_cd = talloc_asprintf(ctx, "%s%s",
344 client_get_cur_dir(),
349 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
350 new_cd = talloc_asprintf_append(new_cd, CLI_DIRSEP_STR);
355 client_set_cur_dir(new_cd);
361 new_cd = clean_name(ctx, new_cd);
362 client_set_cur_dir(new_cd);
364 if ( !cli_resolve_path(ctx, "", cli, new_cd, &targetcli, &targetpath)) {
365 d_printf("cd %s: %s\n", new_cd, cli_errstr(cli));
366 client_set_cur_dir(saved_dir);
370 if (strequal(targetpath,CLI_DIRSEP_STR )) {
375 /* Use a trans2_qpathinfo to test directories for modern servers.
376 Except Win9x doesn't support the qpathinfo_basic() call..... */
378 if (targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95) {
379 if (!cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
380 d_printf("cd %s: %s\n", new_cd, cli_errstr(targetcli));
381 client_set_cur_dir(saved_dir);
385 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
386 d_printf("cd %s: not a directory\n", new_cd);
387 client_set_cur_dir(saved_dir);
391 targetpath = talloc_asprintf(ctx,
396 client_set_cur_dir(saved_dir);
399 targetpath = clean_name(ctx, targetpath);
401 client_set_cur_dir(saved_dir);
405 if (!cli_chkpath(targetcli, targetpath)) {
406 d_printf("cd %s: %s\n", new_cd, cli_errstr(targetcli));
407 client_set_cur_dir(saved_dir);
420 /****************************************************************************
422 ****************************************************************************/
424 static int cmd_cd(void)
429 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
432 d_printf("Current directory is %s\n",client_get_cur_dir());
438 /****************************************************************************
440 ****************************************************************************/
442 static int cmd_cd_oneup(void)
447 /*******************************************************************
448 Decide if a file should be operated on.
449 ********************************************************************/
451 static bool do_this_one(file_info *finfo)
457 if (finfo->mode & aDIR) {
461 if (*client_get_fileselection() &&
462 !mask_match(finfo->name,client_get_fileselection(),false)) {
463 DEBUG(3,("mask_match %s failed\n", finfo->name));
467 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
468 DEBUG(3,("newer_than %s failed\n", finfo->name));
472 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
473 DEBUG(3,("archive %s failed\n", finfo->name));
480 /****************************************************************************
481 Display info about a file.
482 ****************************************************************************/
484 static void display_finfo(file_info *finfo, const char *dir)
487 TALLOC_CTX *ctx = talloc_tos();
489 if (!do_this_one(finfo)) {
493 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
495 d_printf(" %-30s%7.7s %8.0f %s",
497 attrib_string(finfo->mode),
500 dir_total += finfo->size;
505 /* skip if this is . or .. */
506 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
508 /* create absolute filename for cli_nt_create() FIXME */
509 afname = talloc_asprintf(ctx,
517 /* print file meta date header */
518 d_printf( "FILENAME:%s\n", afname);
519 d_printf( "MODE:%s\n", attrib_string(finfo->mode));
520 d_printf( "SIZE:%.0f\n", (double)finfo->size);
521 d_printf( "MTIME:%s", time_to_asc(t));
522 fnum = cli_nt_create(finfo->cli, afname, CREATE_ACCESS_READ);
524 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
526 cli_errstr( finfo->cli)));
529 sd = cli_query_secdesc(finfo->cli, fnum, ctx);
531 DEBUG( 0, ("display_finfo() failed to "
532 "get security descriptor: %s",
533 cli_errstr( finfo->cli)));
535 display_sec_desc(sd);
543 /****************************************************************************
544 Accumulate size of a file.
545 ****************************************************************************/
547 static void do_du(file_info *finfo, const char *dir)
549 if (do_this_one(finfo)) {
550 dir_total += finfo->size;
554 static bool do_list_recurse;
555 static bool do_list_dirs;
556 static char *do_list_queue = 0;
557 static long do_list_queue_size = 0;
558 static long do_list_queue_start = 0;
559 static long do_list_queue_end = 0;
560 static void (*do_list_fn)(file_info *, const char *dir);
562 /****************************************************************************
563 Functions for do_list_queue.
564 ****************************************************************************/
567 * The do_list_queue is a NUL-separated list of strings stored in a
568 * char*. Since this is a FIFO, we keep track of the beginning and
569 * ending locations of the data in the queue. When we overflow, we
570 * double the size of the char*. When the start of the data passes
571 * the midpoint, we move everything back. This is logically more
572 * complex than a linked list, but easier from a memory management
573 * angle. In any memory error condition, do_list_queue is reset.
574 * Functions check to ensure that do_list_queue is non-NULL before
578 static void reset_do_list_queue(void)
580 SAFE_FREE(do_list_queue);
581 do_list_queue_size = 0;
582 do_list_queue_start = 0;
583 do_list_queue_end = 0;
586 static void init_do_list_queue(void)
588 reset_do_list_queue();
589 do_list_queue_size = 1024;
590 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
591 if (do_list_queue == 0) {
592 d_printf("malloc fail for size %d\n",
593 (int)do_list_queue_size);
594 reset_do_list_queue();
596 memset(do_list_queue, 0, do_list_queue_size);
600 static void adjust_do_list_queue(void)
603 * If the starting point of the queue is more than half way through,
604 * move everything toward the beginning.
607 if (do_list_queue == NULL) {
608 DEBUG(4,("do_list_queue is empty\n"));
609 do_list_queue_start = do_list_queue_end = 0;
613 if (do_list_queue_start == do_list_queue_end) {
614 DEBUG(4,("do_list_queue is empty\n"));
615 do_list_queue_start = do_list_queue_end = 0;
616 *do_list_queue = '\0';
617 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
618 DEBUG(4,("sliding do_list_queue backward\n"));
619 memmove(do_list_queue,
620 do_list_queue + do_list_queue_start,
621 do_list_queue_end - do_list_queue_start);
622 do_list_queue_end -= do_list_queue_start;
623 do_list_queue_start = 0;
627 static void add_to_do_list_queue(const char *entry)
629 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
630 while (new_end > do_list_queue_size) {
631 do_list_queue_size *= 2;
632 DEBUG(4,("enlarging do_list_queue to %d\n",
633 (int)do_list_queue_size));
634 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
635 if (! do_list_queue) {
636 d_printf("failure enlarging do_list_queue to %d bytes\n",
637 (int)do_list_queue_size);
638 reset_do_list_queue();
640 memset(do_list_queue + do_list_queue_size / 2,
641 0, do_list_queue_size / 2);
645 safe_strcpy_base(do_list_queue + do_list_queue_end,
646 entry, do_list_queue, do_list_queue_size);
647 do_list_queue_end = new_end;
648 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
649 entry, (int)do_list_queue_start, (int)do_list_queue_end));
653 static char *do_list_queue_head(void)
655 return do_list_queue + do_list_queue_start;
658 static void remove_do_list_queue_head(void)
660 if (do_list_queue_end > do_list_queue_start) {
661 do_list_queue_start += strlen(do_list_queue_head()) + 1;
662 adjust_do_list_queue();
663 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
664 (int)do_list_queue_start, (int)do_list_queue_end));
668 static int do_list_queue_empty(void)
670 return (! (do_list_queue && *do_list_queue));
673 /****************************************************************************
674 A helper for do_list.
675 ****************************************************************************/
677 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
679 TALLOC_CTX *ctx = talloc_tos();
681 char *dir_end = NULL;
683 /* Work out the directory. */
684 dir = talloc_strdup(ctx, mask);
688 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
692 if (f->mode & aDIR) {
693 if (do_list_dirs && do_this_one(f)) {
696 if (do_list_recurse &&
698 !strequal(f->name,".") &&
699 !strequal(f->name,"..")) {
704 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
709 mask2 = talloc_asprintf(ctx,
717 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
723 mask2 = talloc_asprintf_append(mask2,
731 add_to_do_list_queue(mask2);
738 if (do_this_one(f)) {
744 /****************************************************************************
745 A wrapper around cli_list that adds recursion.
746 ****************************************************************************/
748 void do_list(const char *mask,
750 void (*fn)(file_info *, const char *dir),
754 static int in_do_list = 0;
755 TALLOC_CTX *ctx = talloc_tos();
756 struct cli_state *targetcli = NULL;
757 char *targetpath = NULL;
759 if (in_do_list && rec) {
760 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
766 do_list_recurse = rec;
771 init_do_list_queue();
772 add_to_do_list_queue(mask);
774 while (!do_list_queue_empty()) {
776 * Need to copy head so that it doesn't become
777 * invalid inside the call to cli_list. This
778 * would happen if the list were expanded
780 * Fix from E. Jay Berkenbilt (ejb@ql.org)
782 char *head = talloc_strdup(ctx, do_list_queue_head());
790 if ( !cli_resolve_path(ctx, "", cli, head, &targetcli, &targetpath ) ) {
791 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
792 remove_do_list_queue_head();
796 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
797 remove_do_list_queue_head();
798 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
799 char *next_file = do_list_queue_head();
801 if ((strlen(next_file) >= 2) &&
802 (next_file[strlen(next_file) - 1] == '*') &&
803 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
804 save_ch = next_file +
805 strlen(next_file) - 2;
808 /* cwd is only used if showacls is on */
809 client_set_cwd(next_file);
812 if (!showacls) /* don't disturbe the showacls output */
813 d_printf("\n%s\n",next_file);
815 *save_ch = CLI_DIRSEP_CHAR;
819 TALLOC_FREE(targetpath);
823 if (cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetpath)) {
824 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1) {
825 d_printf("%s listing %s\n",
826 cli_errstr(targetcli), targetpath);
828 TALLOC_FREE(targetpath);
830 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
835 reset_do_list_queue();
838 /****************************************************************************
839 Get a directory listing.
840 ****************************************************************************/
842 static int cmd_dir(void)
844 TALLOC_CTX *ctx = talloc_tos();
845 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
851 if (strcmp(client_get_cur_dir(), CLI_DIRSEP_STR) != 0) {
852 mask = talloc_strdup(ctx, client_get_cur_dir());
856 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
857 mask = talloc_asprintf_append(mask, CLI_DIRSEP_STR);
860 mask = talloc_strdup(ctx, CLI_DIRSEP_STR);
867 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
868 string_replace(buf,'/','\\');
869 if (*buf == CLI_DIRSEP_CHAR) {
870 mask = talloc_strdup(ctx, buf + 1);
872 mask = talloc_asprintf_append(mask, buf);
875 mask = talloc_asprintf_append(mask, "*");
882 /* cwd is only used if showacls is on */
883 client_set_cwd(client_get_cur_dir());
886 do_list(mask, attribute, display_finfo, recurse, true);
890 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
895 /****************************************************************************
896 Get a directory listing.
897 ****************************************************************************/
899 static int cmd_du(void)
901 TALLOC_CTX *ctx = talloc_tos();
902 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
908 mask = talloc_strdup(ctx, client_get_cur_dir());
912 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
913 mask = talloc_asprintf_append(mask, CLI_DIRSEP_STR);
919 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
920 string_replace(buf,'/','\\');
921 if (*buf == CLI_DIRSEP_CHAR) {
922 mask = talloc_strdup(ctx, buf);
924 mask = talloc_asprintf_append(mask, buf);
927 mask = talloc_strdup(ctx, "*");
930 do_list(mask, attribute, do_du, recurse, true);
934 d_printf("Total number of bytes: %.0f\n", dir_total);
939 static int cmd_echo(void)
941 TALLOC_CTX *ctx = talloc_tos();
945 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
946 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
947 d_printf("echo <num> <data>\n");
951 if (!cli_echo(cli, atoi(num), (uint8 *)data, strlen(data))) {
952 d_printf("echo failed: %s\n",
953 nt_errstr(cli_get_nt_error(cli)));
960 /****************************************************************************
961 Get a file from rname to lname
962 ****************************************************************************/
964 static int do_get(const char *rname, const char *lname_in, bool reget)
966 TALLOC_CTX *ctx = talloc_tos();
967 int handle = 0, fnum;
968 bool newhandle = false;
970 struct timeval tp_start;
971 int read_size = io_bufsize;
977 struct cli_state *targetcli = NULL;
978 char *targetname = NULL;
981 lname = talloc_strdup(ctx, lname_in);
990 if (!cli_resolve_path(ctx, "", cli, rname, &targetcli, &targetname ) ) {
991 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
995 GetTimeOfDay(&tp_start);
997 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
1000 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
1004 if(!strcmp(lname,"-")) {
1005 handle = fileno(stdout);
1008 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
1010 start = sys_lseek(handle, 0, SEEK_END);
1012 d_printf("Error seeking local file\n");
1017 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1022 d_printf("Error opening local file %s\n",lname);
1027 if (!cli_qfileinfo(targetcli, fnum,
1028 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
1029 !cli_getattrE(targetcli, fnum,
1030 &attr, &size, NULL, NULL, NULL)) {
1031 d_printf("getattrib: %s\n",cli_errstr(targetcli));
1035 DEBUG(1,("getting file %s of size %.0f as %s ",
1036 rname, (double)size, lname));
1038 if(!(data = (char *)SMB_MALLOC(read_size))) {
1039 d_printf("malloc fail for size %d\n", read_size);
1040 cli_close(targetcli, fnum);
1045 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
1050 if (writefile(handle,data, n) != n) {
1051 d_printf("Error writing local file\n");
1059 if (nread + start < size) {
1060 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
1061 rname, (long)nread));
1068 if (!cli_close(targetcli, fnum)) {
1069 d_printf("Error %s closing remote file\n",cli_errstr(cli));
1077 if (archive_level >= 2 && (attr & aARCH)) {
1078 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
1082 struct timeval tp_end;
1085 GetTimeOfDay(&tp_end);
1087 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1088 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1089 get_total_time_ms += this_time;
1090 get_total_size += nread;
1092 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1093 nread / (1.024*this_time + 1.0e-4),
1094 get_total_size / (1.024*get_total_time_ms)));
1097 TALLOC_FREE(targetname);
1101 /****************************************************************************
1103 ****************************************************************************/
1105 static int cmd_get(void)
1107 TALLOC_CTX *ctx = talloc_tos();
1112 rname = talloc_asprintf(ctx,
1114 client_get_cur_dir(),
1120 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1121 d_printf("get <filename> [localname]\n");
1124 rname = talloc_asprintf_append(rname, fname);
1128 rname = clean_name(ctx, rname);
1133 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1138 return do_get(rname, lname, false);
1141 /****************************************************************************
1142 Do an mget operation on one file.
1143 ****************************************************************************/
1145 static void do_mget(file_info *finfo, const char *dir)
1147 TALLOC_CTX *ctx = talloc_tos();
1150 char *saved_curdir = NULL;
1151 char *mget_mask = NULL;
1152 char *new_cd = NULL;
1158 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1162 d_printf("mget aborted\n");
1166 if (finfo->mode & aDIR) {
1167 if (asprintf(&quest,
1168 "Get directory %s? ",finfo->name) < 0) {
1172 if (asprintf(&quest,
1173 "Get file %s? ",finfo->name) < 0) {
1178 if (prompt && !yesno(quest)) {
1184 if (!(finfo->mode & aDIR)) {
1185 rname = talloc_asprintf(ctx,
1187 client_get_cur_dir(),
1192 do_get(rname, finfo->name, false);
1197 /* handle directories */
1198 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1199 if (!saved_curdir) {
1203 new_cd = talloc_asprintf(ctx,
1205 client_get_cur_dir(),
1211 client_set_cur_dir(new_cd);
1213 string_replace(finfo->name,'\\','/');
1215 strlower_m(finfo->name);
1218 if (!directory_exist(finfo->name,NULL) &&
1219 mkdir(finfo->name,0777) != 0) {
1220 d_printf("failed to create directory %s\n",finfo->name);
1221 client_set_cur_dir(saved_curdir);
1225 if (chdir(finfo->name) != 0) {
1226 d_printf("failed to chdir to directory %s\n",finfo->name);
1227 client_set_cur_dir(saved_curdir);
1231 mget_mask = talloc_asprintf(ctx,
1233 client_get_cur_dir());
1239 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,false, true);
1241 client_set_cur_dir(saved_curdir);
1242 TALLOC_FREE(mget_mask);
1243 TALLOC_FREE(saved_curdir);
1244 TALLOC_FREE(new_cd);
1247 /****************************************************************************
1248 View the file using the pager.
1249 ****************************************************************************/
1251 static int cmd_more(void)
1253 TALLOC_CTX *ctx = talloc_tos();
1257 char *pager_cmd = NULL;
1262 rname = talloc_asprintf(ctx,
1264 client_get_cur_dir(),
1270 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1274 fd = smb_mkstemp(lname);
1276 d_printf("failed to create temporary file for more\n");
1281 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1282 d_printf("more <filename>\n");
1286 rname = talloc_asprintf_append(rname, fname);
1290 rname = clean_name(ctx,rname);
1295 rc = do_get(rname, lname, false);
1297 pager=getenv("PAGER");
1299 pager_cmd = talloc_asprintf(ctx,
1301 (pager? pager:PAGER),
1312 /****************************************************************************
1314 ****************************************************************************/
1316 static int cmd_mget(void)
1318 TALLOC_CTX *ctx = talloc_tos();
1319 uint16 attribute = aSYSTEM | aHIDDEN;
1320 char *mget_mask = NULL;
1329 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1330 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1334 if ((mget_mask[0] != '\0') &&
1335 (mget_mask[strlen(mget_mask)-1]!=CLI_DIRSEP_CHAR)) {
1336 mget_mask = talloc_asprintf_append(mget_mask,
1343 if (*buf == CLI_DIRSEP_CHAR) {
1344 mget_mask = talloc_strdup(ctx, buf);
1346 mget_mask = talloc_asprintf_append(mget_mask,
1352 do_list(mget_mask, attribute, do_mget, false, true);
1356 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1360 if(mget_mask[strlen(mget_mask)-1]!=CLI_DIRSEP_CHAR) {
1361 mget_mask = talloc_asprintf_append(mget_mask,
1367 mget_mask = talloc_asprintf_append(mget_mask, "*");
1371 do_list(mget_mask, attribute, do_mget, false, true);
1377 /****************************************************************************
1378 Make a directory of name "name".
1379 ****************************************************************************/
1381 static bool do_mkdir(const char *name)
1383 TALLOC_CTX *ctx = talloc_tos();
1384 struct cli_state *targetcli;
1385 char *targetname = NULL;
1387 if (!cli_resolve_path(ctx, "", cli, name, &targetcli, &targetname)) {
1388 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1392 if (!cli_mkdir(targetcli, targetname)) {
1393 d_printf("%s making remote directory %s\n",
1394 cli_errstr(targetcli),name);
1401 /****************************************************************************
1402 Show 8.3 name of a file.
1403 ****************************************************************************/
1405 static bool do_altname(const char *name)
1409 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1410 d_printf("%s getting alt name for %s\n",
1411 cli_errstr(cli),name);
1414 d_printf("%s\n", altname);
1419 /****************************************************************************
1421 ****************************************************************************/
1423 static int cmd_quit(void)
1431 /****************************************************************************
1433 ****************************************************************************/
1435 static int cmd_mkdir(void)
1437 TALLOC_CTX *ctx = talloc_tos();
1441 mask = talloc_strdup(ctx, client_get_cur_dir());
1446 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1448 d_printf("mkdir <dirname>\n");
1452 mask = talloc_asprintf_append(mask, buf);
1460 struct cli_state *targetcli;
1461 char *targetname = NULL;
1464 ddir2 = talloc_strdup(ctx, "");
1469 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
1473 ddir = talloc_strdup(ctx, targetname);
1477 trim_char(ddir,'.','\0');
1478 p = strtok(ddir,"/\\");
1480 ddir2 = talloc_asprintf_append(ddir2, p);
1484 if (!cli_chkpath(targetcli, ddir2)) {
1487 ddir2 = talloc_asprintf_append(ddir2, CLI_DIRSEP_STR);
1491 p = strtok(NULL,"/\\");
1500 /****************************************************************************
1502 ****************************************************************************/
1504 static int cmd_altname(void)
1506 TALLOC_CTX *ctx = talloc_tos();
1510 name = talloc_strdup(ctx, client_get_cur_dir());
1515 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1516 d_printf("altname <file>\n");
1519 name = talloc_asprintf_append(name, buf);
1527 /****************************************************************************
1529 ****************************************************************************/
1531 static int do_put(const char *rname, const char *lname, bool reput)
1533 TALLOC_CTX *ctx = talloc_tos();
1536 SMB_OFF_T start = 0;
1539 int maxwrite = io_bufsize;
1541 struct timeval tp_start;
1542 struct cli_state *targetcli;
1543 char *targetname = NULL;
1545 if (!cli_resolve_path(ctx, "", cli, rname, &targetcli, &targetname)) {
1546 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1550 GetTimeOfDay(&tp_start);
1553 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1555 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1556 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1557 d_printf("getattrib: %s\n",cli_errstr(cli));
1562 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1566 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1570 /* allow files to be piped into smbclient
1573 Note that in this case this function will exit(0) rather
1575 if (!strcmp(lname, "-")) {
1577 /* size of file is not known */
1579 f = x_fopen(lname,O_RDONLY, 0);
1581 if (x_tseek(f, start, SEEK_SET) == -1) {
1582 d_printf("Error seeking local file\n");
1589 d_printf("Error opening local file %s\n",lname);
1593 DEBUG(1,("putting file %s as %s ",lname,
1596 buf = (char *)SMB_MALLOC(maxwrite);
1598 d_printf("ERROR: Not enough memory!\n");
1601 while (!x_feof(f)) {
1605 if ((n = readfile(buf,n,f)) < 1) {
1606 if((n == 0) && x_feof(f))
1607 break; /* Empty local file. */
1609 d_printf("Error reading local file: %s\n", strerror(errno));
1614 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1617 d_printf("Error writing file: %s\n", cli_errstr(cli));
1625 if (!cli_close(targetcli, fnum)) {
1626 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1639 struct timeval tp_end;
1642 GetTimeOfDay(&tp_end);
1644 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1645 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1646 put_total_time_ms += this_time;
1647 put_total_size += nread;
1649 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1650 nread / (1.024*this_time + 1.0e-4),
1651 put_total_size / (1.024*put_total_time_ms)));
1662 /****************************************************************************
1664 ****************************************************************************/
1666 static int cmd_put(void)
1668 TALLOC_CTX *ctx = talloc_tos();
1673 rname = talloc_asprintf(ctx,
1675 client_get_cur_dir(),
1681 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1682 d_printf("put <filename>\n");
1686 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1687 rname = talloc_asprintf_append(rname, buf);
1689 rname = talloc_asprintf_append(rname, lname);
1695 rname = clean_name(ctx, rname);
1702 /* allow '-' to represent stdin
1703 jdblair, 24.jun.98 */
1704 if (!file_exist(lname,&st) &&
1705 (strcmp(lname,"-"))) {
1706 d_printf("%s does not exist\n",lname);
1711 return do_put(rname, lname, false);
1714 /*************************************
1715 File list structure.
1716 *************************************/
1718 static struct file_list {
1719 struct file_list *prev, *next;
1724 /****************************************************************************
1725 Free a file_list structure.
1726 ****************************************************************************/
1728 static void free_file_list (struct file_list *list_head)
1730 struct file_list *list, *next;
1732 for (list = list_head; list; list = next) {
1734 DLIST_REMOVE(list_head, list);
1735 SAFE_FREE(list->file_path);
1740 /****************************************************************************
1741 Seek in a directory/file list until you get something that doesn't start with
1743 ****************************************************************************/
1745 static bool seek_list(struct file_list *list, char *name)
1748 trim_string(list->file_path,"./","\n");
1749 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1758 /****************************************************************************
1759 Set the file selection mask.
1760 ****************************************************************************/
1762 static int cmd_select(void)
1764 TALLOC_CTX *ctx = talloc_tos();
1765 char *new_fs = NULL;
1766 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
1769 client_set_fileselection(new_fs);
1771 client_set_fileselection("");
1776 /****************************************************************************
1777 Recursive file matching function act as find
1778 match must be always set to true when calling this function
1779 ****************************************************************************/
1781 static int file_find(struct file_list **list, const char *directory,
1782 const char *expression, bool match)
1784 SMB_STRUCT_DIR *dir;
1785 struct file_list *entry;
1786 struct stat statbuf;
1792 dir = sys_opendir(directory);
1796 while ((dname = readdirname(dir))) {
1797 if (!strcmp("..", dname))
1799 if (!strcmp(".", dname))
1802 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1807 if (!match || !gen_fnmatch(expression, dname)) {
1809 ret = stat(path, &statbuf);
1811 if (S_ISDIR(statbuf.st_mode)) {
1813 ret = file_find(list, path, expression, false);
1816 d_printf("file_find: cannot stat file %s\n", path);
1825 entry = SMB_MALLOC_P(struct file_list);
1827 d_printf("Out of memory in file_find\n");
1831 entry->file_path = path;
1832 entry->isdir = isdir;
1833 DLIST_ADD(*list, entry);
1843 /****************************************************************************
1845 ****************************************************************************/
1847 static int cmd_mput(void)
1849 TALLOC_CTX *ctx = talloc_tos();
1852 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
1854 struct file_list *temp_list;
1855 char *quest, *lname, *rname;
1859 ret = file_find(&file_list, ".", p, true);
1861 free_file_list(file_list);
1869 for (temp_list = file_list; temp_list;
1870 temp_list = temp_list->next) {
1873 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
1876 trim_string(lname, "./", "/");
1878 /* check if it's a directory */
1879 if (temp_list->isdir) {
1880 /* if (!recurse) continue; */
1883 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
1886 if (prompt && !yesno(quest)) { /* No */
1887 /* Skip the directory */
1888 lname[strlen(lname)-1] = '/';
1889 if (!seek_list(temp_list, lname))
1893 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) {
1896 string_replace(rname,'/','\\');
1897 if (!cli_chkpath(cli, rname) &&
1899 DEBUG (0, ("Unable to make dir, skipping..."));
1900 /* Skip the directory */
1901 lname[strlen(lname)-1] = '/';
1902 if (!seek_list(temp_list, lname)) {
1910 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
1913 if (prompt && !yesno(quest)) {
1920 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) {
1925 string_replace(rname,'/','\\');
1927 do_put(rname, lname, false);
1929 free_file_list(file_list);
1938 /****************************************************************************
1940 ****************************************************************************/
1942 static int do_cancel(int job)
1944 if (cli_printjob_del(cli, job)) {
1945 d_printf("Job %d cancelled\n",job);
1948 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1953 /****************************************************************************
1955 ****************************************************************************/
1957 static int cmd_cancel(void)
1959 TALLOC_CTX *ctx = talloc_tos();
1963 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
1964 d_printf("cancel <jobid> ...\n");
1970 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
1975 /****************************************************************************
1977 ****************************************************************************/
1979 static int cmd_print(void)
1981 TALLOC_CTX *ctx = talloc_tos();
1986 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
1987 d_printf("print <filename>\n");
1991 rname = talloc_strdup(ctx, lname);
1995 p = strrchr_m(rname,'/');
1997 rname = talloc_asprintf(ctx,
2002 if (strequal(lname,"-")) {
2003 rname = talloc_asprintf(ctx,
2011 return do_put(rname, lname, false);
2014 /****************************************************************************
2015 Show a print queue entry.
2016 ****************************************************************************/
2018 static void queue_fn(struct print_job_info *p)
2020 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2023 /****************************************************************************
2025 ****************************************************************************/
2027 static int cmd_queue(void)
2029 cli_print_queue(cli, queue_fn);
2033 /****************************************************************************
2035 ****************************************************************************/
2037 static void do_del(file_info *finfo, const char *dir)
2039 TALLOC_CTX *ctx = talloc_tos();
2042 mask = talloc_asprintf(ctx,
2051 if (finfo->mode & aDIR) {
2056 if (!cli_unlink(finfo->cli, mask)) {
2057 d_printf("%s deleting remote file %s\n",
2058 cli_errstr(finfo->cli),mask);
2063 /****************************************************************************
2065 ****************************************************************************/
2067 static int cmd_del(void)
2069 TALLOC_CTX *ctx = talloc_tos();
2072 uint16 attribute = aSYSTEM | aHIDDEN;
2078 mask = talloc_strdup(ctx, client_get_cur_dir());
2082 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2083 d_printf("del <filename>\n");
2086 mask = talloc_asprintf_append(mask, buf);
2091 do_list(mask,attribute,do_del,false,false);
2095 /****************************************************************************
2096 Wildcard delete some files.
2097 ****************************************************************************/
2099 static int cmd_wdel(void)
2101 TALLOC_CTX *ctx = talloc_tos();
2105 struct cli_state *targetcli;
2106 char *targetname = NULL;
2108 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2109 d_printf("wdel 0x<attrib> <wcard>\n");
2113 attribute = (uint16)strtol(buf, (char **)NULL, 16);
2115 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2116 d_printf("wdel 0x<attrib> <wcard>\n");
2120 mask = talloc_asprintf(ctx, "%s%s",
2121 client_get_cur_dir(),
2127 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2128 d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
2132 if (!cli_unlink_full(targetcli, targetname, attribute)) {
2133 d_printf("%s deleting remote files %s\n",cli_errstr(targetcli),targetname);
2138 /****************************************************************************
2139 ****************************************************************************/
2141 static int cmd_open(void)
2143 TALLOC_CTX *ctx = talloc_tos();
2146 char *targetname = NULL;
2147 struct cli_state *targetcli;
2150 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2151 d_printf("open <filename>\n");
2154 mask = talloc_asprintf(ctx,
2156 client_get_cur_dir(),
2162 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2163 d_printf("open %s: %s\n", mask, cli_errstr(cli));
2167 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA|FILE_WRITE_DATA);
2169 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA);
2171 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2173 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2176 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2181 static int cmd_posix_encrypt(void)
2183 TALLOC_CTX *ctx = talloc_tos();
2184 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2186 if (cli->use_kerberos) {
2187 status = cli_gss_smb_encryption_start(cli);
2189 char *domain = NULL;
2191 char *password = NULL;
2193 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2194 d_printf("posix_encrypt domain user password\n");
2198 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2199 d_printf("posix_encrypt domain user password\n");
2203 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2204 d_printf("posix_encrypt domain user password\n");
2208 status = cli_raw_ntlm_smb_encryption_start(cli,
2214 if (!NT_STATUS_IS_OK(status)) {
2215 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2217 d_printf("encryption on\n");
2223 /****************************************************************************
2224 ****************************************************************************/
2226 static int cmd_posix_open(void)
2228 TALLOC_CTX *ctx = talloc_tos();
2231 char *targetname = NULL;
2232 struct cli_state *targetcli;
2236 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2237 d_printf("posix_open <filename> 0<mode>\n");
2240 mask = talloc_asprintf(ctx,
2242 client_get_cur_dir(),
2248 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2249 d_printf("posix_open <filename> 0<mode>\n");
2252 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2254 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2255 d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
2259 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode);
2261 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDONLY, mode);
2263 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
2265 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2268 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
2274 static int cmd_posix_mkdir(void)
2276 TALLOC_CTX *ctx = talloc_tos();
2279 char *targetname = NULL;
2280 struct cli_state *targetcli;
2284 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2285 d_printf("posix_mkdir <filename> 0<mode>\n");
2288 mask = talloc_asprintf(ctx,
2290 client_get_cur_dir(),
2296 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2297 d_printf("posix_mkdir <filename> 0<mode>\n");
2300 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2302 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2303 d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
2307 fnum = cli_posix_mkdir(targetcli, targetname, mode);
2309 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2311 d_printf("posix_mkdir created directory %s\n", targetname);
2316 static int cmd_posix_unlink(void)
2318 TALLOC_CTX *ctx = talloc_tos();
2321 char *targetname = NULL;
2322 struct cli_state *targetcli;
2324 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2325 d_printf("posix_unlink <filename>\n");
2328 mask = talloc_asprintf(ctx,
2330 client_get_cur_dir(),
2336 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2337 d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
2341 if (!cli_posix_unlink(targetcli, targetname)) {
2342 d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
2344 d_printf("posix_unlink deleted file %s\n", targetname);
2350 static int cmd_posix_rmdir(void)
2352 TALLOC_CTX *ctx = talloc_tos();
2355 char *targetname = NULL;
2356 struct cli_state *targetcli;
2358 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2359 d_printf("posix_rmdir <filename>\n");
2362 mask = talloc_asprintf(ctx,
2364 client_get_cur_dir(),
2370 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2371 d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
2375 if (!cli_posix_rmdir(targetcli, targetname)) {
2376 d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
2378 d_printf("posix_rmdir deleted directory %s\n", targetname);
2384 static int cmd_close(void)
2386 TALLOC_CTX *ctx = talloc_tos();
2390 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2391 d_printf("close <fnum>\n");
2396 /* We really should use the targetcli here.... */
2397 if (!cli_close(cli, fnum)) {
2398 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
2404 static int cmd_posix(void)
2406 TALLOC_CTX *ctx = talloc_tos();
2407 uint16 major, minor;
2408 uint32 caplow, caphigh;
2411 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2412 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2416 if (!cli_unix_extensions_version(cli, &major, &minor, &caplow, &caphigh)) {
2417 d_printf("Can't get UNIX CIFS extensions version from server.\n");
2421 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2423 caps = talloc_strdup(ctx, "");
2427 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2428 caps = talloc_asprintf_append(caps, "locks ");
2433 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2434 caps = talloc_asprintf_append(caps, "acls ");
2439 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2440 caps = talloc_asprintf_append(caps, "eas ");
2445 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2446 caps = talloc_asprintf_append(caps, "pathnames ");
2451 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2452 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2457 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2458 caps = talloc_asprintf_append(caps, "large_read ");
2463 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2464 caps = talloc_asprintf_append(caps, "large_write ");
2470 if (*caps && caps[strlen(caps)-1] == ' ') {
2471 caps[strlen(caps)-1] = '\0';
2473 if (!cli_set_unix_extensions_capabilities(cli, major, minor, caplow, caphigh)) {
2474 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n", cli_errstr(cli));
2478 d_printf("Selecting server supported CIFS capabilities %s\n", caps);
2480 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2481 CLI_DIRSEP_CHAR = '/';
2482 *CLI_DIRSEP_STR = '/';
2483 client_set_cur_dir(CLI_DIRSEP_STR);
2489 static int cmd_lock(void)
2491 TALLOC_CTX *ctx = talloc_tos();
2493 SMB_BIG_UINT start, len;
2494 enum brl_type lock_type;
2497 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2498 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2503 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2504 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2508 if (*buf == 'r' || *buf == 'R') {
2509 lock_type = READ_LOCK;
2510 } else if (*buf == 'w' || *buf == 'W') {
2511 lock_type = WRITE_LOCK;
2513 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2517 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2518 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2522 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2524 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2525 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2529 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2531 if (!cli_posix_lock(cli, fnum, start, len, true, lock_type)) {
2532 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
2538 static int cmd_unlock(void)
2540 TALLOC_CTX *ctx = talloc_tos();
2542 SMB_BIG_UINT start, len;
2545 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2546 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2551 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2552 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2556 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2558 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2559 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2563 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2565 if (!cli_posix_unlock(cli, fnum, start, len)) {
2566 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
2573 /****************************************************************************
2575 ****************************************************************************/
2577 static int cmd_rmdir(void)
2579 TALLOC_CTX *ctx = talloc_tos();
2582 char *targetname = NULL;
2583 struct cli_state *targetcli;
2585 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2586 d_printf("rmdir <dirname>\n");
2589 mask = talloc_asprintf(ctx,
2591 client_get_cur_dir(),
2597 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2598 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
2602 if (!cli_rmdir(targetcli, targetname)) {
2603 d_printf("%s removing remote directory file %s\n",
2604 cli_errstr(targetcli),mask);
2610 /****************************************************************************
2612 ****************************************************************************/
2614 static int cmd_link(void)
2616 TALLOC_CTX *ctx = talloc_tos();
2617 char *oldname = NULL;
2618 char *newname = NULL;
2621 char *targetname = NULL;
2622 struct cli_state *targetcli;
2624 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2625 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2626 d_printf("link <oldname> <newname>\n");
2629 oldname = talloc_asprintf(ctx,
2631 client_get_cur_dir(),
2636 newname = talloc_asprintf(ctx,
2638 client_get_cur_dir(),
2644 if (!cli_resolve_path(ctx, "", cli, oldname, &targetcli, &targetname)) {
2645 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2649 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2650 d_printf("Server doesn't support UNIX CIFS calls.\n");
2654 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
2655 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
2661 /****************************************************************************
2663 ****************************************************************************/
2665 static int cmd_symlink(void)
2667 TALLOC_CTX *ctx = talloc_tos();
2668 char *oldname = NULL;
2669 char *newname = NULL;
2672 char *targetname = NULL;
2673 struct cli_state *targetcli;
2675 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2676 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2677 d_printf("symlink <oldname> <newname>\n");
2680 oldname = talloc_asprintf(ctx,
2682 client_get_cur_dir(),
2687 newname = talloc_asprintf(ctx,
2689 client_get_cur_dir(),
2695 if (!cli_resolve_path(ctx, "", cli, oldname, &targetcli, &targetname)) {
2696 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2700 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2701 d_printf("Server doesn't support UNIX CIFS calls.\n");
2705 if (!cli_unix_symlink(targetcli, targetname, newname)) {
2706 d_printf("%s symlinking files (%s -> %s)\n",
2707 cli_errstr(targetcli), newname, targetname);
2714 /****************************************************************************
2716 ****************************************************************************/
2718 static int cmd_chmod(void)
2720 TALLOC_CTX *ctx = talloc_tos();
2724 char *targetname = NULL;
2725 struct cli_state *targetcli;
2728 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2729 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2730 d_printf("chmod mode file\n");
2733 src = talloc_asprintf(ctx,
2735 client_get_cur_dir(),
2741 mode = (mode_t)strtol(buf, NULL, 8);
2743 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
2744 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
2748 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2749 d_printf("Server doesn't support UNIX CIFS calls.\n");
2753 if (!cli_unix_chmod(targetcli, targetname, mode)) {
2754 d_printf("%s chmod file %s 0%o\n",
2755 cli_errstr(targetcli), src, (unsigned int)mode);
2762 static const char *filetype_to_str(mode_t mode)
2764 if (S_ISREG(mode)) {
2765 return "regular file";
2766 } else if (S_ISDIR(mode)) {
2770 if (S_ISCHR(mode)) {
2771 return "character device";
2775 if (S_ISBLK(mode)) {
2776 return "block device";
2780 if (S_ISFIFO(mode)) {
2785 if (S_ISLNK(mode)) {
2786 return "symbolic link";
2790 if (S_ISSOCK(mode)) {
2797 static char rwx_to_str(mode_t m, mode_t bt, char ret)
2806 static char *unix_mode_to_str(char *s, mode_t m)
2809 const char *str = filetype_to_str(m);
2825 *p++ = str[1] == 'y' ? 'l' : 's';
2832 *p++ = rwx_to_str(m, S_IRUSR, 'r');
2833 *p++ = rwx_to_str(m, S_IWUSR, 'w');
2834 *p++ = rwx_to_str(m, S_IXUSR, 'x');
2835 *p++ = rwx_to_str(m, S_IRGRP, 'r');
2836 *p++ = rwx_to_str(m, S_IWGRP, 'w');
2837 *p++ = rwx_to_str(m, S_IXGRP, 'x');
2838 *p++ = rwx_to_str(m, S_IROTH, 'r');
2839 *p++ = rwx_to_str(m, S_IWOTH, 'w');
2840 *p++ = rwx_to_str(m, S_IXOTH, 'x');
2845 /****************************************************************************
2846 Utility function for UNIX getfacl.
2847 ****************************************************************************/
2849 static char *perms_to_string(fstring permstr, unsigned char perms)
2851 fstrcpy(permstr, "---");
2852 if (perms & SMB_POSIX_ACL_READ) {
2855 if (perms & SMB_POSIX_ACL_WRITE) {
2858 if (perms & SMB_POSIX_ACL_EXECUTE) {
2864 /****************************************************************************
2866 ****************************************************************************/
2868 static int cmd_getfacl(void)
2870 TALLOC_CTX *ctx = talloc_tos();
2873 char *targetname = NULL;
2874 struct cli_state *targetcli;
2875 uint16 major, minor;
2876 uint32 caplow, caphigh;
2877 char *retbuf = NULL;
2879 SMB_STRUCT_STAT sbuf;
2880 uint16 num_file_acls = 0;
2881 uint16 num_dir_acls = 0;
2884 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
2885 d_printf("getfacl filename\n");
2888 src = talloc_asprintf(ctx,
2890 client_get_cur_dir(),
2896 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
2897 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2901 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2902 d_printf("Server doesn't support UNIX CIFS calls.\n");
2906 if (!cli_unix_extensions_version(targetcli, &major, &minor,
2907 &caplow, &caphigh)) {
2908 d_printf("Can't get UNIX CIFS version from server.\n");
2912 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
2913 d_printf("This server supports UNIX extensions "
2914 "but doesn't support POSIX ACLs.\n");
2918 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2919 d_printf("%s getfacl doing a stat on file %s\n",
2920 cli_errstr(targetcli), src);
2924 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2925 d_printf("%s getfacl file %s\n",
2926 cli_errstr(targetcli), src);
2930 /* ToDo : Print out the ACL values. */
2931 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2932 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2933 src, (unsigned int)CVAL(retbuf,0) );
2938 num_file_acls = SVAL(retbuf,2);
2939 num_dir_acls = SVAL(retbuf,4);
2940 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2941 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2943 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2944 (unsigned int)rb_size);
2950 d_printf("# file: %s\n", src);
2951 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2953 if (num_file_acls == 0 && num_dir_acls == 0) {
2954 d_printf("No acls found.\n");
2957 for (i = 0; i < num_file_acls; i++) {
2960 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2961 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2964 case SMB_POSIX_ACL_USER_OBJ:
2967 case SMB_POSIX_ACL_USER:
2968 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2969 d_printf("user:%u:", uorg);
2971 case SMB_POSIX_ACL_GROUP_OBJ:
2972 d_printf("group::");
2974 case SMB_POSIX_ACL_GROUP:
2975 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2976 d_printf("group:%u", uorg);
2978 case SMB_POSIX_ACL_MASK:
2981 case SMB_POSIX_ACL_OTHER:
2982 d_printf("other::");
2985 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2986 src, (unsigned int)tagtype );
2991 d_printf("%s\n", perms_to_string(permstring, perms));
2994 for (i = 0; i < num_dir_acls; i++) {
2997 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2998 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3001 case SMB_POSIX_ACL_USER_OBJ:
3002 d_printf("default:user::");
3004 case SMB_POSIX_ACL_USER:
3005 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3006 d_printf("default:user:%u:", uorg);
3008 case SMB_POSIX_ACL_GROUP_OBJ:
3009 d_printf("default:group::");
3011 case SMB_POSIX_ACL_GROUP:
3012 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3013 d_printf("default:group:%u", uorg);
3015 case SMB_POSIX_ACL_MASK:
3016 d_printf("default:mask::");
3018 case SMB_POSIX_ACL_OTHER:
3019 d_printf("default:other::");
3022 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3023 src, (unsigned int)tagtype );
3028 d_printf("%s\n", perms_to_string(permstring, perms));
3035 /****************************************************************************
3037 ****************************************************************************/
3039 static int cmd_stat(void)
3041 TALLOC_CTX *ctx = talloc_tos();
3044 char *targetname = NULL;
3045 struct cli_state *targetcli;
3047 SMB_STRUCT_STAT sbuf;
3050 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3051 d_printf("stat file\n");
3054 src = talloc_asprintf(ctx,
3056 client_get_cur_dir(),
3062 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
3063 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3067 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3068 d_printf("Server doesn't support UNIX CIFS calls.\n");
3072 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
3073 d_printf("%s stat file %s\n",
3074 cli_errstr(targetcli), src);
3078 /* Print out the stat values. */
3079 d_printf("File: %s\n", src);
3080 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3081 (double)sbuf.st_size,
3082 (unsigned int)sbuf.st_blocks,
3083 filetype_to_str(sbuf.st_mode));
3085 #if defined(S_ISCHR) && defined(S_ISBLK)
3086 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
3087 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3088 (double)sbuf.st_ino,
3089 (unsigned int)sbuf.st_nlink,
3090 unix_dev_major(sbuf.st_rdev),
3091 unix_dev_minor(sbuf.st_rdev));
3094 d_printf("Inode: %.0f\tLinks: %u\n",
3095 (double)sbuf.st_ino,
3096 (unsigned int)sbuf.st_nlink);
3098 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3099 ((int)sbuf.st_mode & 0777),
3100 unix_mode_to_str(mode_str, sbuf.st_mode),
3101 (unsigned int)sbuf.st_uid,
3102 (unsigned int)sbuf.st_gid);
3104 lt = localtime(&sbuf.st_atime);
3106 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3108 fstrcpy(mode_str, "unknown");
3110 d_printf("Access: %s\n", mode_str);
3112 lt = localtime(&sbuf.st_mtime);
3114 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3116 fstrcpy(mode_str, "unknown");
3118 d_printf("Modify: %s\n", mode_str);
3120 lt = localtime(&sbuf.st_ctime);
3122 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3124 fstrcpy(mode_str, "unknown");
3126 d_printf("Change: %s\n", mode_str);
3132 /****************************************************************************
3134 ****************************************************************************/
3136 static int cmd_chown(void)
3138 TALLOC_CTX *ctx = talloc_tos();
3142 char *buf, *buf2, *buf3;
3143 struct cli_state *targetcli;
3144 char *targetname = NULL;
3146 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3147 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3148 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3149 d_printf("chown uid gid file\n");
3153 uid = (uid_t)atoi(buf);
3154 gid = (gid_t)atoi(buf2);
3156 src = talloc_asprintf(ctx,
3158 client_get_cur_dir(),
3163 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname) ) {
3164 d_printf("chown %s: %s\n", src, cli_errstr(cli));
3168 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3169 d_printf("Server doesn't support UNIX CIFS calls.\n");
3173 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
3174 d_printf("%s chown file %s uid=%d, gid=%d\n",
3175 cli_errstr(targetcli), src, (int)uid, (int)gid);
3182 /****************************************************************************
3184 ****************************************************************************/
3186 static int cmd_rename(void)
3188 TALLOC_CTX *ctx = talloc_tos();
3191 struct cli_state *targetcli;
3195 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3196 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3197 d_printf("rename <src> <dest>\n");
3201 src = talloc_asprintf(ctx,
3203 client_get_cur_dir(),
3209 dest = talloc_asprintf(ctx,
3211 client_get_cur_dir(),
3217 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetsrc)) {
3218 d_printf("rename %s: %s\n", src, cli_errstr(cli));
3222 if (!cli_resolve_path(ctx, "", cli, dest, &targetcli, &targetdest)) {
3223 d_printf("rename %s: %s\n", dest, cli_errstr(cli));
3227 if (!cli_rename(targetcli, targetsrc, targetdest)) {
3228 d_printf("%s renaming files %s -> %s \n",
3229 cli_errstr(targetcli),
3238 /****************************************************************************
3239 Print the volume name.
3240 ****************************************************************************/
3242 static int cmd_volume(void)
3248 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
3249 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
3253 d_printf("Volume: |%s| serial number 0x%x\n",
3254 volname, (unsigned int)serial_num);
3258 /****************************************************************************
3259 Hard link files using the NT call.
3260 ****************************************************************************/
3262 static int cmd_hardlink(void)
3264 TALLOC_CTX *ctx = talloc_tos();
3267 struct cli_state *targetcli;
3270 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3271 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3272 d_printf("hardlink <src> <dest>\n");
3276 src = talloc_asprintf(ctx,
3278 client_get_cur_dir(),
3284 dest = talloc_asprintf(ctx,
3286 client_get_cur_dir(),
3292 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
3293 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
3297 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
3298 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
3305 /****************************************************************************
3306 Toggle the prompt flag.
3307 ****************************************************************************/
3309 static int cmd_prompt(void)
3312 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
3316 /****************************************************************************
3317 Set the newer than time.
3318 ****************************************************************************/
3320 static int cmd_newer(void)
3322 TALLOC_CTX *ctx = talloc_tos();
3325 SMB_STRUCT_STAT sbuf;
3327 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
3328 if (ok && (sys_stat(buf,&sbuf) == 0)) {
3329 newer_than = sbuf.st_mtime;
3330 DEBUG(1,("Getting files newer than %s",
3331 time_to_asc(newer_than)));
3336 if (ok && newer_than == 0) {
3337 d_printf("Error setting newer-than time\n");
3344 /****************************************************************************
3345 Set the archive level.
3346 ****************************************************************************/
3348 static int cmd_archive(void)
3350 TALLOC_CTX *ctx = talloc_tos();
3353 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3354 archive_level = atoi(buf);
3356 d_printf("Archive level is %d\n",archive_level);
3362 /****************************************************************************
3363 Toggle the lowercaseflag.
3364 ****************************************************************************/
3366 static int cmd_lowercase(void)
3368 lowercase = !lowercase;
3369 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
3373 /****************************************************************************
3374 Toggle the case sensitive flag.
3375 ****************************************************************************/
3377 static int cmd_setcase(void)
3379 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
3381 cli_set_case_sensitive(cli, !orig_case_sensitive);
3382 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
3387 /****************************************************************************
3388 Toggle the showacls flag.
3389 ****************************************************************************/
3391 static int cmd_showacls(void)
3393 showacls = !showacls;
3394 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
3399 /****************************************************************************
3400 Toggle the recurse flag.
3401 ****************************************************************************/
3403 static int cmd_recurse(void)
3406 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
3410 /****************************************************************************
3411 Toggle the translate flag.
3412 ****************************************************************************/
3414 static int cmd_translate(void)
3416 translation = !translation;
3417 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
3418 translation?"on":"off"));
3422 /****************************************************************************
3424 ****************************************************************************/
3426 static int cmd_lcd(void)
3428 TALLOC_CTX *ctx = talloc_tos();
3432 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3435 d = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
3439 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
3443 /****************************************************************************
3444 Get a file restarting at end of local file.
3445 ****************************************************************************/
3447 static int cmd_reget(void)
3449 TALLOC_CTX *ctx = talloc_tos();
3450 char *local_name = NULL;
3451 char *remote_name = NULL;
3455 remote_name = talloc_asprintf(ctx,
3457 client_get_cur_dir(),
3463 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
3464 d_printf("reget <filename>\n");
3467 remote_name = talloc_asprintf_append(remote_name, fname);
3471 remote_name = clean_name(ctx,remote_name);
3477 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
3482 return do_get(remote_name, local_name, true);
3485 /****************************************************************************
3486 Put a file restarting at end of local file.
3487 ****************************************************************************/
3489 static int cmd_reput(void)
3491 TALLOC_CTX *ctx = talloc_tos();
3492 char *local_name = NULL;
3493 char *remote_name = NULL;
3497 remote_name = talloc_asprintf(ctx,
3499 client_get_cur_dir(),
3505 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
3506 d_printf("reput <filename>\n");
3510 if (!file_exist(local_name, &st)) {
3511 d_printf("%s does not exist\n", local_name);
3515 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3516 remote_name = talloc_asprintf_append(remote_name,
3519 remote_name = talloc_asprintf_append(remote_name,
3526 remote_name = clean_name(ctx, remote_name);
3531 return do_put(remote_name, local_name, true);
3534 /****************************************************************************
3536 ****************************************************************************/
3538 static void browse_fn(const char *name, uint32 m,
3539 const char *comment, void *state)
3541 const char *typestr = "";
3544 case STYPE_DISKTREE:
3548 typestr = "Printer";
3557 /* FIXME: If the remote machine returns non-ascii characters
3558 in any of these fields, they can corrupt the output. We
3559 should remove them. */
3561 d_printf("\t%-15s %-10.10s%s\n",
3562 name,typestr,comment);
3564 d_printf ("%s|%s|%s\n",typestr,name,comment);
3568 static bool browse_host_rpc(bool sort)
3571 struct rpc_pipe_client *pipe_hnd;
3572 TALLOC_CTX *frame = talloc_stackframe();
3575 SRV_SHARE_INFO_CTR ctr;
3578 init_enum_hnd(&enum_hnd, 0);
3580 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
3582 if (pipe_hnd == NULL) {
3583 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
3584 nt_errstr(status)));
3589 werr = rpccli_srvsvc_net_share_enum(pipe_hnd, frame, 1, &ctr,
3590 0xffffffff, &enum_hnd);
3592 if (!W_ERROR_IS_OK(werr)) {
3593 cli_rpc_pipe_close(pipe_hnd);
3598 for (i=0; i<ctr.num_entries; i++) {
3599 SRV_SHARE_INFO_1 *info = &ctr.share.info1[i];
3600 char *name, *comment;
3601 name = rpcstr_pull_unistr2_talloc(
3602 frame, &info->info_1_str.uni_netname);
3603 comment = rpcstr_pull_unistr2_talloc(
3604 frame, &info->info_1_str.uni_remark);
3605 browse_fn(name, info->info_1.type, comment, NULL);
3608 cli_rpc_pipe_close(pipe_hnd);
3613 /****************************************************************************
3614 Try and browse available connections on a host.
3615 ****************************************************************************/
3617 static bool browse_host(bool sort)
3621 d_printf("\n\tSharename Type Comment\n");
3622 d_printf("\t--------- ---- -------\n");
3625 if (browse_host_rpc(sort)) {
3629 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
3630 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
3635 /****************************************************************************
3637 ****************************************************************************/
3639 static void server_fn(const char *name, uint32 m,
3640 const char *comment, void *state)
3644 d_printf("\t%-16s %s\n", name, comment);
3646 d_printf("%s|%s|%s\n",(char *)state, name, comment);
3650 /****************************************************************************
3651 Try and browse available connections on a host.
3652 ****************************************************************************/
3654 static bool list_servers(const char *wk_grp)
3658 if (!cli->server_domain)
3662 d_printf("\n\tServer Comment\n");
3663 d_printf("\t--------- -------\n");
3665 fstrcpy( state, "Server" );
3666 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
3670 d_printf("\n\tWorkgroup Master\n");
3671 d_printf("\t--------- -------\n");
3674 fstrcpy( state, "Workgroup" );
3675 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
3680 /****************************************************************************
3681 Print or set current VUID
3682 ****************************************************************************/
3684 static int cmd_vuid(void)
3686 TALLOC_CTX *ctx = talloc_tos();
3689 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3690 d_printf("Current VUID is %d\n", cli->vuid);
3694 cli->vuid = atoi(buf);
3698 /****************************************************************************
3699 Setup a new VUID, by issuing a session setup
3700 ****************************************************************************/
3702 static int cmd_logon(void)
3704 TALLOC_CTX *ctx = talloc_tos();
3705 char *l_username, *l_password;
3707 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
3708 d_printf("logon <username> [<password>]\n");
3712 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
3713 char *pass = getpass("Password: ");
3715 l_password = talloc_strdup(ctx,pass);
3722 if (!NT_STATUS_IS_OK(cli_session_setup(cli, l_username,
3723 l_password, strlen(l_password),
3724 l_password, strlen(l_password),
3726 d_printf("session setup failed: %s\n", cli_errstr(cli));
3730 d_printf("Current VUID is %d\n", cli->vuid);
3735 /****************************************************************************
3736 list active connections
3737 ****************************************************************************/
3739 static int cmd_list_connect(void)
3745 /****************************************************************************
3746 display the current active client connection
3747 ****************************************************************************/
3749 static int cmd_show_connect( void )
3751 TALLOC_CTX *ctx = talloc_tos();
3752 struct cli_state *targetcli;
3755 if (!cli_resolve_path(ctx, "", cli, client_get_cur_dir(),
3756 &targetcli, &targetpath ) ) {
3757 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
3761 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
3765 /****************************************************************************
3767 ***************************************************************************/
3769 int cmd_iosize(void)
3771 TALLOC_CTX *ctx = talloc_tos();
3775 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3776 d_printf("iosize <n> or iosize 0x<n>. "
3777 "Minimum is 16384 (0x4000), "
3778 "max is 16776960 (0xFFFF00)\n");
3782 iosize = strtol(buf,NULL,0);
3783 if (iosize < 0 || iosize > 0xFFFF00) {
3784 d_printf("iosize out of range (min = 16384 (0x4000), "
3785 "max = 16776960 (0x0xFFFF00)");
3789 io_bufsize = iosize;
3790 d_printf("iosize is now %d\n", io_bufsize);
3795 /* Some constants for completing filename arguments */
3797 #define COMPL_NONE 0 /* No completions */
3798 #define COMPL_REMOTE 1 /* Complete remote filename */
3799 #define COMPL_LOCAL 2 /* Complete local filename */
3801 /* This defines the commands supported by this client.
3802 * NOTE: The "!" must be the last one in the list because it's fn pointer
3803 * field is NULL, and NULL in that field is used in process_tok()
3804 * (below) to indicate the end of the list. crh
3809 const char *description;
3810 char compl_args[2]; /* Completion argument info */
3812 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3813 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
3814 {"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}},
3815 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
3816 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
3817 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
3818 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
3819 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
3820 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
3821 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
3822 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3823 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3824 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3825 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
3826 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3827 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
3828 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
3829 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3830 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3831 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
3832 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
3833 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
3834 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3835 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3836 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
3837 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3838 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3839 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
3840 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3841 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
3842 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3843 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
3844 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
3845 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
3846 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
3847 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
3848 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
3849 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3850 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3851 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3852 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3853 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
3854 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
3855 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
3856 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
3857 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3858 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
3859 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3860 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3861 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
3862 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
3863 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
3864 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
3865 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3866 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3867 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
3868 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
3869 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
3870 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
3871 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
3872 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
3873 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
3874 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3875 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
3876 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
3877 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3878 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
3879 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
3880 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
3881 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
3883 /* Yes, this must be here, see crh's comment above. */
3884 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
3885 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
3888 /*******************************************************************
3889 Lookup a command string in the list of commands, including
3891 ******************************************************************/
3893 static int process_tok(char *tok)
3895 int i = 0, matches = 0;
3897 int tok_len = strlen(tok);
3899 while (commands[i].fn != NULL) {
3900 if (strequal(commands[i].name,tok)) {
3904 } else if (strnequal(commands[i].name, tok, tok_len)) {
3913 else if (matches == 1)
3919 /****************************************************************************
3921 ****************************************************************************/
3923 static int cmd_help(void)
3925 TALLOC_CTX *ctx = talloc_tos();
3929 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3930 if ((i = process_tok(buf)) >= 0)
3931 d_printf("HELP %s:\n\t%s\n\n",
3932 commands[i].name,commands[i].description);
3934 while (commands[i].description) {
3935 for (j=0; commands[i].description && (j<5); j++) {
3936 d_printf("%-15s",commands[i].name);
3945 /****************************************************************************
3946 Process a -c command string.
3947 ****************************************************************************/
3949 static int process_command_string(const char *cmd_in)
3951 TALLOC_CTX *ctx = talloc_tos();
3952 char *cmd = talloc_strdup(ctx, cmd_in);
3958 /* establish the connection if not already */
3961 cli = cli_cm_open(talloc_tos(), NULL, desthost, service, true);
3967 while (cmd[0] != '\0') {
3973 if ((p = strchr_m(cmd, ';')) == 0) {
3982 /* and get the first part of the command */
3984 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
3988 if ((i = process_tok(tok)) >= 0) {
3989 rc = commands[i].fn();
3990 } else if (i == -2) {
3991 d_printf("%s: command abbreviation ambiguous\n",tok);
3993 d_printf("%s: command not found\n",tok);
4000 #define MAX_COMPLETIONS 100
4008 } completion_remote_t;
4010 static void completion_remote_filter(const char *mnt,
4015 completion_remote_t *info = (completion_remote_t *)state;
4017 if ((info->count < MAX_COMPLETIONS - 1) &&
4018 (strncmp(info->text, f->name, info->len) == 0) &&
4019 (strcmp(f->name, ".") != 0) &&
4020 (strcmp(f->name, "..") != 0)) {
4021 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
4022 info->matches[info->count] = SMB_STRDUP(f->name);
4024 TALLOC_CTX *ctx = talloc_stackframe();
4027 if (info->dirmask && info->dirmask[0] != 0) {
4028 tmp = talloc_strdup(ctx,info->dirmask);
4030 tmp = talloc_strdup(ctx,"");
4036 tmp = talloc_asprintf_append(tmp, f->name);
4041 if (f->mode & aDIR) {
4042 tmp = talloc_asprintf_append(tmp, "/");
4048 info->matches[info->count] = SMB_STRDUP(tmp);
4051 if (info->matches[info->count] == NULL) {
4054 if (f->mode & aDIR) {
4055 smb_readline_ca_char(0);
4057 if (info->count == 1) {
4058 info->samelen = strlen(info->matches[info->count]);
4060 while (strncmp(info->matches[info->count],
4061 info->matches[info->count-1],
4062 info->samelen) != 0) {
4070 static char **remote_completion(const char *text, int len)
4072 TALLOC_CTX *ctx = talloc_stackframe();
4073 char *dirmask = NULL;
4074 char *targetpath = NULL;
4075 struct cli_state *targetcli = NULL;
4077 completion_remote_t info = { NULL, NULL, 1, 0, NULL, 0 };
4079 /* can't have non-static intialisation on Sun CC, so do it
4085 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
4086 if (!info.matches) {
4092 * We're leaving matches[0] free to fill it later with the text to
4093 * display: Either the one single match or the longest common subset
4096 info.matches[0] = NULL;
4099 for (i = len-1; i >= 0; i--) {
4100 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
4105 info.text = text+i+1;
4106 info.samelen = info.len = len-i-1;
4109 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
4110 if (!info.dirmask) {
4113 strncpy(info.dirmask, text, i+1);
4114 info.dirmask[i+1] = 0;
4115 dirmask = talloc_asprintf(ctx,
4117 client_get_cur_dir(),
4121 info.dirmask = SMB_STRDUP("");
4122 if (!info.dirmask) {
4125 dirmask = talloc_asprintf(ctx,
4127 client_get_cur_dir());
4133 if (!cli_resolve_path(ctx, "", cli, dirmask, &targetcli, &targetpath)) {
4136 if (cli_list(targetcli, targetpath, aDIR | aSYSTEM | aHIDDEN,
4137 completion_remote_filter, (void *)&info) < 0) {
4141 if (info.count == 1) {
4143 * No matches at all, NULL indicates there is nothing
4145 SAFE_FREE(info.matches[0]);
4146 SAFE_FREE(info.matches);
4151 if (info.count == 2) {
4153 * Exactly one match in matches[1], indicate this is the one
4156 info.matches[0] = info.matches[1];
4157 info.matches[1] = NULL;
4160 return info.matches;
4164 * We got more than one possible match, set the result to the maximum
4168 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
4169 info.matches[info.count] = NULL;
4170 return info.matches;
4173 for (i = 0; i < info.count; i++) {
4174 SAFE_FREE(info.matches[i]);
4176 SAFE_FREE(info.matches);
4177 SAFE_FREE(info.dirmask);
4182 static char **completion_fn(const char *text, int start, int end)
4184 smb_readline_ca_char(' ');
4187 const char *buf, *sp;
4191 buf = smb_readline_get_line_buffer();
4195 sp = strchr(buf, ' ');
4199 for (i = 0; commands[i].name; i++) {
4200 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
4201 (commands[i].name[sp - buf] == 0)) {
4205 if (commands[i].name == NULL)
4211 if (sp == (buf + start))
4212 compl_type = commands[i].compl_args[0];
4214 compl_type = commands[i].compl_args[1];
4216 if (compl_type == COMPL_REMOTE)
4217 return remote_completion(text, end - start);
4218 else /* fall back to local filename completion */
4222 int i, len, samelen = 0, count=1;
4224 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
4231 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
4232 if (strncmp(text, commands[i].name, len) == 0) {
4233 matches[count] = SMB_STRDUP(commands[i].name);
4234 if (!matches[count])
4237 samelen = strlen(matches[count]);
4239 while (strncmp(matches[count], matches[count-1], samelen) != 0)
4246 case 0: /* should never happen */
4250 matches[0] = SMB_STRDUP(matches[1]);
4253 matches[0] = (char *)SMB_MALLOC(samelen+1);
4256 strncpy(matches[0], matches[1], samelen);
4257 matches[0][samelen] = 0;
4259 matches[count] = NULL;
4263 for (i = 0; i < count; i++)
4271 /****************************************************************************
4272 Make sure we swallow keepalives during idle time.
4273 ****************************************************************************/
4275 static void readline_callback(void)
4278 struct timeval timeout;
4279 static time_t last_t;
4295 FD_SET(cli->fd,&fds);
4298 timeout.tv_usec = 0;
4299 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
4301 /* We deliberately use receive_smb instead of
4302 client_receive_smb as we want to receive
4303 session keepalives and then drop them here.
4305 if (FD_ISSET(cli->fd,&fds)) {
4306 if (!receive_smb(cli->fd,cli->inbuf,0,&cli->smb_rw_error)) {
4307 DEBUG(0, ("Read from server failed, maybe it closed the "
4314 /* Ping the server to keep the connection alive using SMBecho. */
4316 unsigned char garbage[16];
4317 memset(garbage, 0xf0, sizeof(garbage));
4318 cli_echo(cli, 1, garbage, sizeof(garbage));
4322 /****************************************************************************
4323 Process commands on stdin.
4324 ****************************************************************************/
4326 static int process_stdin(void)
4331 TALLOC_CTX *frame = talloc_stackframe();
4333 char *the_prompt = NULL;
4337 /* display a prompt */
4338 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
4342 line = smb_readline(the_prompt, readline_callback, completion_fn);
4343 SAFE_FREE(the_prompt);
4349 /* special case - first char is ! */
4357 /* and get the first part of the command */
4359 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
4365 if ((i = process_tok(tok)) >= 0) {
4366 rc = commands[i].fn();
4367 } else if (i == -2) {
4368 d_printf("%s: command abbreviation ambiguous\n",tok);
4370 d_printf("%s: command not found\n",tok);
4378 /****************************************************************************
4379 Process commands from the client.
4380 ****************************************************************************/
4382 static int process(const char *base_directory)
4386 cli = cli_cm_open(talloc_tos(), NULL, desthost, service, true);
4391 if (base_directory && *base_directory) {
4392 rc = do_cd(base_directory);
4400 rc = process_command_string(cmdstr);
4409 /****************************************************************************
4411 ****************************************************************************/
4413 static int do_host_query(const char *query_host)
4415 cli = cli_cm_open(talloc_tos(), NULL, query_host, "IPC$", true);
4423 /* Workgroups simply don't make sense over anything
4424 else but port 139... */
4427 cli_cm_set_port( 139 );
4428 cli = cli_cm_open(talloc_tos(), NULL, query_host, "IPC$", true);
4432 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
4436 list_servers(lp_workgroup());
4443 /****************************************************************************
4444 Handle a tar operation.
4445 ****************************************************************************/
4447 static int do_tar_op(const char *base_directory)
4451 /* do we already have a connection? */
4453 cli = cli_cm_open(talloc_tos(), NULL, desthost, service, true);
4460 if (base_directory && *base_directory) {
4461 ret = do_cd(base_directory);
4475 /****************************************************************************
4476 Handle a message operation.
4477 ****************************************************************************/
4479 static int do_message_op(void)
4481 struct sockaddr_storage ss;
4482 struct nmb_name called, calling;
4483 fstring server_name;
4484 char name_type_hex[10];
4488 make_nmb_name(&calling, calling_name, 0x0);
4489 make_nmb_name(&called , desthost, name_type);
4491 fstrcpy(server_name, desthost);
4492 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
4493 fstrcat(server_name, name_type_hex);
4499 /* we can only do messages over port 139 (to windows clients at least) */
4501 msg_port = port ? port : 139;
4503 if (!(cli=cli_initialise()) || (cli_set_port(cli, msg_port) != msg_port)) {
4504 d_printf("Connection to %s failed\n", desthost);
4508 status = cli_connect(cli, server_name, &ss);
4509 if (!NT_STATUS_IS_OK(status)) {
4510 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
4514 if (!cli_session_request(cli, &calling, &called)) {
4515 d_printf("session request failed\n");
4526 /****************************************************************************
4528 ****************************************************************************/
4530 int main(int argc,char *argv[])
4532 char *base_directory = NULL;
4534 char *query_host = NULL;
4535 bool message = false;
4536 char *term_code = NULL;
4537 static const char *new_name_resolve_order = NULL;
4541 fstring new_workgroup;
4542 bool tar_opt = false;
4543 bool service_opt = false;
4544 struct poptOption long_options[] = {
4547 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
4548 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
4549 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
4550 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
4551 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
4552 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
4553 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
4554 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
4555 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
4556 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
4557 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
4558 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
4559 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
4560 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
4562 POPT_COMMON_CONNECTION
4563 POPT_COMMON_CREDENTIALS
4566 TALLOC_CTX *frame = talloc_stackframe();
4568 if (!client_set_cur_dir("\\")) {
4573 term_code = talloc_strdup(frame,KANJI);
4575 term_code = talloc_strdup(frame,"");
4581 /* initialize the workgroup name so we can determine whether or
4582 not it was set by a command line option */
4584 set_global_myworkgroup( "" );
4585 set_global_myname( "" );
4587 /* set default debug level to 0 regardless of what smb.conf sets */
4588 setup_logging( "smbclient", true );
4589 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
4590 if ((dbf = x_fdup(x_stderr))) {
4591 x_setbuf( dbf, NULL );
4597 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
4598 poptSetOtherOptionHelp(pc, "service <password>");
4600 in_client = true; /* Make sure that we tell lp_load we are */
4602 while ((opt = poptGetNextOpt(pc)) != -1) {
4604 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
4605 /* I see no other way to keep things sane --SSS */
4606 if (tar_opt == true) {
4607 while (poptPeekArg(pc)) {
4613 /* if the service has not yet been specified lets see if it is available in the popt stack */
4614 if (!service_opt && poptPeekArg(pc)) {
4615 service = talloc_strdup(frame, poptGetArg(pc));
4622 /* if the service has already been retrieved then check if we have also a password */
4623 if (service_opt && (!get_cmdline_auth_info_got_pass()) && poptPeekArg(pc)) {
4624 set_cmdline_auth_info_password(poptGetArg(pc));
4629 /* Messages are sent to NetBIOS name type 0x3
4630 * (Messenger Service). Make sure we default
4631 * to port 139 instead of port 445. srl,crh
4634 cli_cm_set_dest_name_type( name_type );
4635 desthost = talloc_strdup(frame,poptGetOptArg(pc));
4640 cli_cm_set_port( 139 );
4645 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
4650 cli_cm_set_dest_ss(&dest_ss);
4658 display_set_stderr();
4662 query_host = talloc_strdup(frame, poptGetOptArg(pc));
4668 term_code = talloc_strdup(frame,poptGetOptArg(pc));
4674 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
4677 /* We must use old option processing for this. Find the
4678 * position of the -T option in the raw argv[]. */
4681 for (i = 1; i < argc; i++) {
4682 if (strncmp("-T", argv[i],2)==0)
4686 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
4687 poptPrintUsage(pc, stderr, 0);
4691 /* this must be the last option, mark we have parsed it so that we know we have */
4695 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
4696 if (!base_directory) {
4704 return(do_smb_browse());
4709 /* We may still have some leftovers after the last popt option has been called */
4710 if (tar_opt == true) {
4711 while (poptPeekArg(pc)) {
4717 /* if the service has not yet been specified lets see if it is available in the popt stack */
4718 if (!service_opt && poptPeekArg(pc)) {
4719 service = talloc_strdup(frame,poptGetArg(pc));
4726 /* if the service has already been retrieved then check if we have also a password */
4727 if (service_opt && !get_cmdline_auth_info_got_pass() && poptPeekArg(pc)) {
4728 set_cmdline_auth_info_password(poptGetArg(pc));
4731 /* check for the -P option */
4734 cli_cm_set_port( port );
4737 * Don't load debug level from smb.conf. It should be
4738 * set by cmdline arg or remain default (0)
4740 AllowDebugChange = false;
4742 /* save the workgroup...
4744 FIXME!! do we need to do this for other options as well
4745 (or maybe a generic way to keep lp_load() from overwriting
4748 fstrcpy( new_workgroup, lp_workgroup() );
4749 calling_name = talloc_strdup(frame, global_myname() );
4750 if (!calling_name) {
4754 if ( override_logfile )
4755 setup_logging( lp_logfile(), false );
4757 if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
4758 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
4759 argv[0], get_dyn_CONFIGFILE());
4764 if (service_opt && service) {
4767 /* Convert any '/' characters in the service name to '\' characters */
4768 string_replace(service, '/','\\');
4769 if (count_chars(service,'\\') < 3) {
4770 d_printf("\n%s: Not enough '\\' characters in service\n",service);
4771 poptPrintUsage(pc, stderr, 0);
4774 /* Remove trailing slashes */
4775 len = strlen(service);
4776 while(len > 0 && service[len - 1] == '\\') {
4778 service[len] = '\0';
4782 if ( strlen(new_workgroup) != 0 ) {
4783 set_global_myworkgroup( new_workgroup );
4786 if ( strlen(calling_name) != 0 ) {
4787 set_global_myname( calling_name );
4789 TALLOC_FREE(calling_name);
4790 calling_name = talloc_strdup(frame, global_myname() );
4795 if(new_name_resolve_order)
4796 lp_set_name_resolve_order(new_name_resolve_order);
4798 if (!tar_type && !query_host && !service && !message) {
4799 poptPrintUsage(pc, stderr, 0);
4803 poptFreeContext(pc);
4805 /* Store the username and password for dfs support */
4807 cli_cm_set_credentials();
4809 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
4813 process_command_string(cmdstr);
4814 return do_tar_op(base_directory);
4817 if (query_host && *query_host) {
4818 char *qhost = query_host;
4821 while (*qhost == '\\' || *qhost == '/')
4824 if ((slash = strchr_m(qhost, '/'))
4825 || (slash = strchr_m(qhost, '\\'))) {
4829 if ((p=strchr_m(qhost, '#'))) {
4832 sscanf(p, "%x", &name_type);
4833 cli_cm_set_dest_name_type( name_type );
4836 return do_host_query(qhost);
4840 return do_message_op();
4843 if (process(base_directory)) {