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 "system/filesys.h"
26 #include "popt_common_cmdline.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "client/client_proto.h"
29 #include "client/clitar_proto.h"
30 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
31 #include "../lib/util/select.h"
32 #include "system/readline.h"
33 #include "../libcli/smbreadline/smbreadline.h"
34 #include "../libcli/security/security.h"
35 #include "system/select.h"
36 #include "libsmb/libsmb.h"
37 #include "libsmb/clirap.h"
39 #include "libsmb/nmblib.h"
40 #include "include/ntioctl.h"
41 #include "../libcli/smb/smbXcli_base.h"
42 #include "lib/util/time_basic.h"
48 extern int do_smb_browse(void); /* mDNS browsing */
50 extern bool override_logfile;
54 static char *desthost;
55 static bool grepable = false;
56 static bool quiet = false;
57 static char *cmdstr = NULL;
58 const char *cmd_ptr = NULL;
60 static int io_bufsize = 0; /* we use the default size */
61 static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
63 static int name_type = 0x20;
64 static int max_protocol = -1;
66 static int process_tok(char *tok);
67 static int cmd_help(void);
69 /* value for unused fid field in trans2 secondary request */
70 #define FID_UNUSED (0xFFFF)
72 time_t newer_than = 0;
73 static int archive_level = 0;
75 static bool translation = false;
78 static bool prompt = true;
80 static bool recurse = false;
81 static bool showacls = false;
82 bool lowercase = false;
83 static bool backup_intent = false;
85 static struct sockaddr_storage dest_ss;
86 static char dest_ss_str[INET6_ADDRSTRLEN];
88 #define SEPARATORS " \t\n\r"
90 static bool abort_mget = true;
93 uint64_t get_total_size = 0;
94 unsigned int get_total_time_ms = 0;
95 static uint64_t put_total_size = 0;
96 static unsigned int put_total_time_ms = 0;
99 static double dir_total;
101 /* encrypted state. */
102 static bool smb_encrypt;
104 /* root cli_state connection */
106 struct cli_state *cli;
108 static char CLI_DIRSEP_CHAR = '\\';
109 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
111 /* Accessor functions for directory paths. */
112 static char *fileselection;
113 static const char *client_get_fileselection(void)
116 return fileselection;
121 static const char *client_set_fileselection(const char *new_fs)
123 SAFE_FREE(fileselection);
125 fileselection = SMB_STRDUP(new_fs);
127 return client_get_fileselection();
131 static const char *client_get_cwd(void)
136 return CLI_DIRSEP_STR;
139 static const char *client_set_cwd(const char *new_cwd)
143 cwd = SMB_STRDUP(new_cwd);
145 return client_get_cwd();
148 static char *cur_dir;
149 const char *client_get_cur_dir(void)
154 return CLI_DIRSEP_STR;
157 const char *client_set_cur_dir(const char *newdir)
161 cur_dir = SMB_STRDUP(newdir);
163 return client_get_cur_dir();
166 /****************************************************************************
167 Put up a yes/no prompt.
168 ****************************************************************************/
170 static bool yesno(const char *p)
175 if (!fgets(ans,sizeof(ans)-1,stdin))
178 if (*ans == 'y' || *ans == 'Y')
184 /****************************************************************************
185 Write to a local file with CR/LF->LF translation if appropriate. Return the
186 number taken from the buffer. This may not equal the number written.
187 ****************************************************************************/
189 static ssize_t writefile(int f, char *b, size_t n)
203 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
206 if (write(f, b, 1) != 1) {
216 /****************************************************************************
217 Read from a file with LF->CR/LF translation if appropriate. Return the
218 number read. read approx n bytes.
219 ****************************************************************************/
221 static int readfile(uint8_t *b, int n, FILE *f)
227 return fread(b,1,n,f);
230 while (i < (n - 1)) {
231 if ((c = getc(f)) == EOF) {
235 if (c == '\n') { /* change all LFs to CR/LF */
250 static size_t push_source(uint8_t *buf, size_t n, void *priv)
252 struct push_state *state = (struct push_state *)priv;
255 if (feof(state->f)) {
259 result = readfile(buf, n, state->f);
260 state->nread += result;
264 /****************************************************************************
266 ****************************************************************************/
268 static void send_message(const char *username)
274 d_printf("Type your message, ending it with a Control-D\n");
277 while (i<sizeof(buf)-2) {
278 int c = fgetc(stdin);
289 status = cli_message(cli, desthost, username, buf);
290 if (!NT_STATUS_IS_OK(status)) {
291 d_fprintf(stderr, "cli_message returned %s\n",
296 /****************************************************************************
297 Check the space on a device.
298 ****************************************************************************/
300 static int do_dskattr(void)
302 uint64_t total, bsize, avail;
303 struct cli_state *targetcli = NULL;
304 char *targetpath = NULL;
305 TALLOC_CTX *ctx = talloc_tos();
308 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(), cli,
309 client_get_cur_dir(), &targetcli,
311 if (!NT_STATUS_IS_OK(status)) {
312 d_printf("Error in dskattr: %s\n", nt_errstr(status));
316 status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
317 if (!NT_STATUS_IS_OK(status)) {
318 d_printf("Error in dskattr: %s\n", nt_errstr(status));
322 d_printf("\n\t\t%" PRIu64
323 " blocks of size %" PRIu64
324 ". %" PRIu64 " blocks available\n",
325 total, bsize, avail);
330 /****************************************************************************
332 ****************************************************************************/
334 static int cmd_pwd(void)
336 d_printf("Current directory is %s",service);
337 d_printf("%s\n",client_get_cur_dir());
341 /****************************************************************************
342 Ensure name has correct directory separators.
343 ****************************************************************************/
345 static void normalize_name(char *newdir)
347 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
348 string_replace(newdir,'/','\\');
352 /****************************************************************************
353 Local name cleanup before sending to server. SMB1 allows relative pathnames,
354 but SMB2 does not, so we need to resolve them locally.
355 ****************************************************************************/
357 char *client_clean_name(TALLOC_CTX *ctx, const char *name)
359 char *newname = NULL;
364 /* First ensure any path separators are correct. */
365 newname = talloc_strdup(ctx, name);
366 if (newname == NULL) {
369 normalize_name(newname);
371 /* Now remove any relative (..) path components. */
372 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
373 newname = unix_clean_name(ctx, newname);
375 newname = clean_name(ctx, newname);
377 if (newname == NULL) {
383 /****************************************************************************
384 Change directory - inner section.
385 ****************************************************************************/
387 static int do_cd(const char *new_dir)
390 char *saved_dir = NULL;
392 char *targetpath = NULL;
393 struct cli_state *targetcli = NULL;
394 SMB_STRUCT_STAT sbuf;
397 TALLOC_CTX *ctx = talloc_stackframe();
400 newdir = talloc_strdup(ctx, new_dir);
406 normalize_name(newdir);
408 /* Save the current directory in case the new directory is invalid */
410 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
416 if (*newdir == CLI_DIRSEP_CHAR) {
417 client_set_cur_dir(newdir);
420 new_cd = talloc_asprintf(ctx, "%s%s",
421 client_get_cur_dir(),
428 /* Ensure cur_dir ends in a DIRSEP */
429 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
430 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
435 client_set_cur_dir(new_cd);
437 new_cd = client_clean_name(ctx, new_cd);
438 client_set_cur_dir(new_cd);
440 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
441 cli, new_cd, &targetcli, &targetpath);
442 if (!NT_STATUS_IS_OK(status)) {
443 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
444 client_set_cur_dir(saved_dir);
448 if (strequal(targetpath,CLI_DIRSEP_STR )) {
453 /* Use a trans2_qpathinfo to test directories for modern servers.
454 Except Win9x doesn't support the qpathinfo_basic() call..... */
456 if (smbXcli_conn_protocol(targetcli->conn) > PROTOCOL_LANMAN2 && !targetcli->win95) {
458 status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
460 if (!NT_STATUS_IS_OK(status)) {
461 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
462 client_set_cur_dir(saved_dir);
466 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
467 d_printf("cd %s: not a directory\n", new_cd);
468 client_set_cur_dir(saved_dir);
473 targetpath = talloc_asprintf(ctx,
478 client_set_cur_dir(saved_dir);
481 targetpath = client_clean_name(ctx, targetpath);
483 client_set_cur_dir(saved_dir);
487 status = cli_chkpath(targetcli, targetpath);
488 if (!NT_STATUS_IS_OK(status)) {
489 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
490 client_set_cur_dir(saved_dir);
503 /****************************************************************************
505 ****************************************************************************/
507 static int cmd_cd(void)
512 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
515 d_printf("Current directory is %s\n",client_get_cur_dir());
521 /****************************************************************************
523 ****************************************************************************/
525 static int cmd_cd_oneup(void)
530 /*******************************************************************
531 Decide if a file should be operated on.
532 ********************************************************************/
534 static bool do_this_one(struct file_info *finfo)
540 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
544 if (*client_get_fileselection() &&
545 !mask_match(finfo->name,client_get_fileselection(),false)) {
546 DEBUG(3,("mask_match %s failed\n", finfo->name));
550 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
551 DEBUG(3,("newer_than %s failed\n", finfo->name));
555 if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
556 DEBUG(3,("archive %s failed\n", finfo->name));
563 /****************************************************************************
564 Display info about a file.
565 ****************************************************************************/
567 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
571 TALLOC_CTX *ctx = talloc_tos();
572 NTSTATUS status = NT_STATUS_OK;
574 if (!do_this_one(finfo)) {
578 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
580 d_printf(" %-30s%7.7s %8.0f %s",
582 attrib_string(talloc_tos(), finfo->mode),
585 dir_total += finfo->size;
590 /* skip if this is . or .. */
591 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
593 /* create absolute filename for cli_ntcreate() FIXME */
594 afname = talloc_asprintf(ctx,
600 return NT_STATUS_NO_MEMORY;
602 /* print file meta date header */
603 d_printf( "FILENAME:%s\n", finfo->name);
604 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->mode));
605 d_printf( "SIZE:%.0f\n", (double)finfo->size);
606 d_printf( "MTIME:%s", time_to_asc(t));
607 status = cli_ntcreate(
611 READ_CONTROL_ACCESS, /* DesiredAccess */
612 0, /* FileAttributes */
614 FILE_SHARE_WRITE, /* ShareAccess */
615 FILE_OPEN, /* CreateDisposition */
616 0x0, /* CreateOptions */
617 0x0, /* SecurityFlags */
620 if (!NT_STATUS_IS_OK(status)) {
621 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
622 afname, nt_errstr(status)));
624 struct security_descriptor *sd = NULL;
625 status = cli_query_secdesc(cli_state, fnum,
627 if (!NT_STATUS_IS_OK(status)) {
628 DEBUG( 0, ("display_finfo() failed to "
629 "get security descriptor: %s",
632 display_sec_desc(sd);
641 /****************************************************************************
642 Accumulate size of a file.
643 ****************************************************************************/
645 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
648 if (do_this_one(finfo)) {
649 dir_total += finfo->size;
654 static bool do_list_recurse;
655 static bool do_list_dirs;
656 static char *do_list_queue = 0;
657 static long do_list_queue_size = 0;
658 static long do_list_queue_start = 0;
659 static long do_list_queue_end = 0;
660 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
663 /****************************************************************************
664 Functions for do_list_queue.
665 ****************************************************************************/
668 * The do_list_queue is a NUL-separated list of strings stored in a
669 * char*. Since this is a FIFO, we keep track of the beginning and
670 * ending locations of the data in the queue. When we overflow, we
671 * double the size of the char*. When the start of the data passes
672 * the midpoint, we move everything back. This is logically more
673 * complex than a linked list, but easier from a memory management
674 * angle. In any memory error condition, do_list_queue is reset.
675 * Functions check to ensure that do_list_queue is non-NULL before
679 static void reset_do_list_queue(void)
681 SAFE_FREE(do_list_queue);
682 do_list_queue_size = 0;
683 do_list_queue_start = 0;
684 do_list_queue_end = 0;
687 static void init_do_list_queue(void)
689 reset_do_list_queue();
690 do_list_queue_size = 1024;
691 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
692 if (do_list_queue == 0) {
693 d_printf("malloc fail for size %d\n",
694 (int)do_list_queue_size);
695 reset_do_list_queue();
697 memset(do_list_queue, 0, do_list_queue_size);
701 static void adjust_do_list_queue(void)
704 * If the starting point of the queue is more than half way through,
705 * move everything toward the beginning.
708 if (do_list_queue == NULL) {
709 DEBUG(4,("do_list_queue is empty\n"));
710 do_list_queue_start = do_list_queue_end = 0;
714 if (do_list_queue_start == do_list_queue_end) {
715 DEBUG(4,("do_list_queue is empty\n"));
716 do_list_queue_start = do_list_queue_end = 0;
717 *do_list_queue = '\0';
718 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
719 DEBUG(4,("sliding do_list_queue backward\n"));
720 memmove(do_list_queue,
721 do_list_queue + do_list_queue_start,
722 do_list_queue_end - do_list_queue_start);
723 do_list_queue_end -= do_list_queue_start;
724 do_list_queue_start = 0;
728 static void add_to_do_list_queue(const char *entry)
730 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
731 while (new_end > do_list_queue_size) {
732 do_list_queue_size *= 2;
733 DEBUG(4,("enlarging do_list_queue to %d\n",
734 (int)do_list_queue_size));
735 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
736 if (! do_list_queue) {
737 d_printf("failure enlarging do_list_queue to %d bytes\n",
738 (int)do_list_queue_size);
739 reset_do_list_queue();
741 memset(do_list_queue + do_list_queue_size / 2,
742 0, do_list_queue_size / 2);
746 strlcpy_base(do_list_queue + do_list_queue_end,
747 entry, do_list_queue, do_list_queue_size);
748 do_list_queue_end = new_end;
749 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
750 entry, (int)do_list_queue_start, (int)do_list_queue_end));
754 static char *do_list_queue_head(void)
756 return do_list_queue + do_list_queue_start;
759 static void remove_do_list_queue_head(void)
761 if (do_list_queue_end > do_list_queue_start) {
762 do_list_queue_start += strlen(do_list_queue_head()) + 1;
763 adjust_do_list_queue();
764 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
765 (int)do_list_queue_start, (int)do_list_queue_end));
769 static int do_list_queue_empty(void)
771 return (! (do_list_queue && *do_list_queue));
774 /****************************************************************************
775 A helper for do_list.
776 ****************************************************************************/
778 static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
779 const char *mask, void *state)
781 struct cli_state *cli_state = (struct cli_state *)state;
782 TALLOC_CTX *ctx = talloc_tos();
784 char *dir_end = NULL;
785 NTSTATUS status = NT_STATUS_OK;
787 /* Work out the directory. */
788 dir = talloc_strdup(ctx, mask);
790 return NT_STATUS_NO_MEMORY;
792 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
796 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
797 if (do_list_dirs && do_this_one(f)) {
798 status = do_list_fn(cli_state, f, dir);
799 if (!NT_STATUS_IS_OK(status)) {
803 if (do_list_recurse &&
805 !strequal(f->name,".") &&
806 !strequal(f->name,"..")) {
811 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
813 return NT_STATUS_UNSUCCESSFUL;
816 mask2 = talloc_asprintf(ctx,
822 return NT_STATUS_NO_MEMORY;
824 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
830 mask2 = talloc_asprintf_append(mask2,
836 return NT_STATUS_NO_MEMORY;
838 add_to_do_list_queue(mask2);
845 if (do_this_one(f)) {
846 status = do_list_fn(cli_state, f, dir);
852 /****************************************************************************
853 A wrapper around cli_list that adds recursion.
854 ****************************************************************************/
856 NTSTATUS do_list(const char *mask,
858 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
863 static int in_do_list = 0;
864 TALLOC_CTX *ctx = talloc_tos();
865 struct cli_state *targetcli = NULL;
866 char *targetpath = NULL;
867 NTSTATUS ret_status = NT_STATUS_OK;
868 NTSTATUS status = NT_STATUS_OK;
870 if (in_do_list && rec) {
871 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
877 do_list_recurse = rec;
882 init_do_list_queue();
883 add_to_do_list_queue(mask);
885 while (!do_list_queue_empty()) {
887 * Need to copy head so that it doesn't become
888 * invalid inside the call to cli_list. This
889 * would happen if the list were expanded
891 * Fix from E. Jay Berkenbilt (ejb@ql.org)
893 char *head = talloc_strdup(ctx, do_list_queue_head());
896 return NT_STATUS_NO_MEMORY;
901 status = cli_resolve_path(ctx, "",
902 popt_get_cmdline_auth_info(),
903 cli, head, &targetcli, &targetpath);
904 if (!NT_STATUS_IS_OK(status)) {
905 d_printf("do_list: [%s] %s\n", head,
907 remove_do_list_queue_head();
911 status = cli_list(targetcli, targetpath, attribute,
912 do_list_helper, targetcli);
913 if (!NT_STATUS_IS_OK(status)) {
914 d_printf("%s listing %s\n",
915 nt_errstr(status), targetpath);
918 remove_do_list_queue_head();
919 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
920 char *next_file = do_list_queue_head();
922 if ((strlen(next_file) >= 2) &&
923 (next_file[strlen(next_file) - 1] == '*') &&
924 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
925 save_ch = next_file +
926 strlen(next_file) - 2;
929 /* cwd is only used if showacls is on */
930 client_set_cwd(next_file);
933 if (!showacls) /* don't disturbe the showacls output */
934 d_printf("\n%s\n",next_file);
936 *save_ch = CLI_DIRSEP_CHAR;
940 TALLOC_FREE(targetpath);
944 status = cli_resolve_path(ctx, "",
945 popt_get_cmdline_auth_info(), cli, mask,
946 &targetcli, &targetpath);
947 if (NT_STATUS_IS_OK(status)) {
948 status = cli_list(targetcli, targetpath, attribute,
949 do_list_helper, targetcli);
950 if (!NT_STATUS_IS_OK(status)) {
951 d_printf("%s listing %s\n",
952 nt_errstr(status), targetpath);
955 TALLOC_FREE(targetpath);
957 d_printf("do_list: [%s] %s\n", mask, nt_errstr(status));
963 reset_do_list_queue();
967 /****************************************************************************
968 Get a directory listing.
969 ****************************************************************************/
971 static int cmd_dir(void)
973 TALLOC_CTX *ctx = talloc_tos();
974 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
981 mask = talloc_strdup(ctx, client_get_cur_dir());
986 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
988 if (*buf == CLI_DIRSEP_CHAR) {
989 mask = talloc_strdup(ctx, buf);
991 mask = talloc_asprintf_append(mask, "%s", buf);
994 mask = talloc_asprintf_append(mask, "*");
1000 mask = client_clean_name(ctx, mask);
1006 /* cwd is only used if showacls is on */
1007 client_set_cwd(client_get_cur_dir());
1010 status = do_list(mask, attribute, display_finfo, recurse, true);
1011 if (!NT_STATUS_IS_OK(status)) {
1017 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
1022 /****************************************************************************
1023 Get a directory listing.
1024 ****************************************************************************/
1026 static int cmd_du(void)
1028 TALLOC_CTX *ctx = talloc_tos();
1029 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1036 mask = talloc_strdup(ctx, client_get_cur_dir());
1040 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
1041 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
1047 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1048 normalize_name(buf);
1049 if (*buf == CLI_DIRSEP_CHAR) {
1050 mask = talloc_strdup(ctx, buf);
1052 mask = talloc_asprintf_append(mask, "%s", buf);
1055 mask = talloc_strdup(ctx, "*");
1061 mask = client_clean_name(ctx, mask);
1066 status = do_list(mask, attribute, do_du, recurse, true);
1067 if (!NT_STATUS_IS_OK(status)) {
1073 d_printf("Total number of bytes: %.0f\n", dir_total);
1078 static int cmd_echo(void)
1080 TALLOC_CTX *ctx = talloc_tos();
1085 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1086 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1087 d_printf("echo <num> <data>\n");
1091 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1093 if (!NT_STATUS_IS_OK(status)) {
1094 d_printf("echo failed: %s\n", nt_errstr(status));
1101 /****************************************************************************
1102 Get a file from rname to lname
1103 ****************************************************************************/
1105 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1107 int *pfd = (int *)priv;
1110 rc = writefile(*pfd, buf, n);
1112 return map_nt_error_from_unix(errno);
1114 return NT_STATUS_OK;
1117 static int do_get(const char *rname, const char *lname_in, bool reget)
1119 TALLOC_CTX *ctx = talloc_tos();
1122 bool newhandle = false;
1123 struct timespec tp_start;
1129 struct cli_state *targetcli = NULL;
1130 char *targetname = NULL;
1134 lname = talloc_strdup(ctx, lname_in);
1140 if (!strlower_m(lname)) {
1141 d_printf("strlower_m %s failed\n", lname);
1146 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1147 cli, rname, &targetcli, &targetname);
1148 if (!NT_STATUS_IS_OK(status)) {
1149 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1153 clock_gettime_mono(&tp_start);
1155 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 d_printf("%s opening remote file %s\n", nt_errstr(status),
1162 if(!strcmp(lname,"-")) {
1163 handle = fileno(stdout);
1166 handle = open(lname, O_WRONLY|O_CREAT, 0644);
1168 start = lseek(handle, 0, SEEK_END);
1170 d_printf("Error seeking local file\n");
1176 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1181 d_printf("Error opening local file %s\n",lname);
1186 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1188 if (!NT_STATUS_IS_OK(status)) {
1189 status = cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL,
1191 if(!NT_STATUS_IS_OK(status)) {
1192 d_printf("getattrib: %s\n", nt_errstr(status));
1200 DEBUG(1,("getting file %s of size %.0f as %s ",
1201 rname, (double)size, lname));
1203 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1204 writefile_sink, (void *)&handle, &nread);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 d_fprintf(stderr, "parallel_read returned %s\n",
1211 cli_close(targetcli, fnum);
1215 status = cli_close(targetcli, fnum);
1216 if (!NT_STATUS_IS_OK(status)) {
1217 d_printf("Error %s closing remote file\n", nt_errstr(status));
1225 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1226 cli_setatr(cli, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
1230 struct timespec tp_end;
1233 clock_gettime_mono(&tp_end);
1234 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1235 get_total_time_ms += this_time;
1236 get_total_size += nread;
1238 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1239 nread / (1.024*this_time + 1.0e-4),
1240 get_total_size / (1.024*get_total_time_ms)));
1243 TALLOC_FREE(targetname);
1247 /****************************************************************************
1249 ****************************************************************************/
1251 static int cmd_get(void)
1253 TALLOC_CTX *ctx = talloc_tos();
1258 rname = talloc_strdup(ctx, client_get_cur_dir());
1263 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1264 d_printf("get <filename> [localname]\n");
1267 rname = talloc_asprintf_append(rname, "%s", fname);
1271 rname = client_clean_name(ctx, rname);
1276 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1281 return do_get(rname, lname, false);
1284 /****************************************************************************
1285 Do an mget operation on one file.
1286 ****************************************************************************/
1288 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1291 TALLOC_CTX *ctx = talloc_tos();
1292 NTSTATUS status = NT_STATUS_OK;
1295 char *saved_curdir = NULL;
1296 char *mget_mask = NULL;
1297 char *new_cd = NULL;
1300 return NT_STATUS_OK;
1303 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1304 return NT_STATUS_OK;
1307 d_printf("mget aborted\n");
1308 return NT_STATUS_UNSUCCESSFUL;
1311 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
1312 if (asprintf(&quest,
1313 "Get directory %s? ",finfo->name) < 0) {
1314 return NT_STATUS_NO_MEMORY;
1317 if (asprintf(&quest,
1318 "Get file %s? ",finfo->name) < 0) {
1319 return NT_STATUS_NO_MEMORY;
1323 if (prompt && !yesno(quest)) {
1325 return NT_STATUS_OK;
1329 if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
1330 rname = talloc_asprintf(ctx,
1332 client_get_cur_dir(),
1335 return NT_STATUS_NO_MEMORY;
1337 rname = client_clean_name(ctx, rname);
1338 if (rname == NULL) {
1339 return NT_STATUS_NO_MEMORY;
1341 do_get(rname, finfo->name, false);
1343 return NT_STATUS_OK;
1346 /* handle directories */
1347 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1348 if (!saved_curdir) {
1349 return NT_STATUS_NO_MEMORY;
1352 new_cd = talloc_asprintf(ctx,
1354 client_get_cur_dir(),
1358 return NT_STATUS_NO_MEMORY;
1360 new_cd = client_clean_name(ctx, new_cd);
1361 if (new_cd == NULL) {
1362 return NT_STATUS_NO_MEMORY;
1364 client_set_cur_dir(new_cd);
1366 string_replace(finfo->name,'\\','/');
1368 if (!strlower_m(finfo->name)) {
1369 return NT_STATUS_INVALID_PARAMETER;
1373 if (!directory_exist(finfo->name) &&
1374 mkdir(finfo->name,0777) != 0) {
1375 d_printf("failed to create directory %s\n",finfo->name);
1376 client_set_cur_dir(saved_curdir);
1377 return map_nt_error_from_unix(errno);
1380 if (chdir(finfo->name) != 0) {
1381 d_printf("failed to chdir to directory %s\n",finfo->name);
1382 client_set_cur_dir(saved_curdir);
1383 return map_nt_error_from_unix(errno);
1386 mget_mask = talloc_asprintf(ctx,
1388 client_get_cur_dir());
1391 return NT_STATUS_NO_MEMORY;
1394 mget_mask = client_clean_name(ctx, mget_mask);
1395 if (mget_mask == NULL) {
1396 return NT_STATUS_NO_MEMORY;
1398 status = do_list(mget_mask,
1399 (FILE_ATTRIBUTE_SYSTEM
1400 | FILE_ATTRIBUTE_HIDDEN
1401 | FILE_ATTRIBUTE_DIRECTORY),
1402 do_mget, false, true);
1403 if (!NT_STATUS_IS_OK(status)
1404 && !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1406 * Ignore access denied errors to ensure all permitted files are
1412 if (chdir("..") == -1) {
1413 d_printf("do_mget: failed to chdir to .. (error %s)\n",
1415 return map_nt_error_from_unix(errno);
1417 client_set_cur_dir(saved_curdir);
1418 TALLOC_FREE(mget_mask);
1419 TALLOC_FREE(saved_curdir);
1420 TALLOC_FREE(new_cd);
1421 return NT_STATUS_OK;
1424 /****************************************************************************
1425 View the file using the pager.
1426 ****************************************************************************/
1428 static int cmd_more(void)
1430 TALLOC_CTX *ctx = talloc_tos();
1434 char *pager_cmd = NULL;
1440 rname = talloc_strdup(ctx, client_get_cur_dir());
1445 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1449 mask = umask(S_IRWXO | S_IRWXG);
1450 fd = mkstemp(lname);
1453 d_printf("failed to create temporary file for more\n");
1458 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1459 d_printf("more <filename>\n");
1463 rname = talloc_asprintf_append(rname, "%s", fname);
1467 rname = client_clean_name(ctx,rname);
1472 rc = do_get(rname, lname, false);
1474 pager=getenv("PAGER");
1476 pager_cmd = talloc_asprintf(ctx,
1478 (pager? pager:PAGER),
1483 if (system(pager_cmd) == -1) {
1484 d_printf("system command '%s' returned -1\n",
1492 /****************************************************************************
1494 ****************************************************************************/
1496 static int cmd_mget(void)
1498 TALLOC_CTX *ctx = talloc_tos();
1499 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1500 char *mget_mask = NULL;
1502 NTSTATUS status = NT_STATUS_OK;
1505 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1510 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1512 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1516 if (*buf == CLI_DIRSEP_CHAR) {
1517 mget_mask = talloc_strdup(ctx, buf);
1519 mget_mask = talloc_asprintf_append(mget_mask,
1525 mget_mask = client_clean_name(ctx, mget_mask);
1526 if (mget_mask == NULL) {
1529 status = do_list(mget_mask, attribute, do_mget, false, true);
1530 if (!NT_STATUS_IS_OK(status)) {
1535 if (mget_mask == NULL) {
1536 d_printf("nothing to mget\n");
1541 mget_mask = talloc_asprintf(ctx,
1543 client_get_cur_dir());
1547 mget_mask = client_clean_name(ctx, mget_mask);
1548 if (mget_mask == NULL) {
1551 status = do_list(mget_mask, attribute, do_mget, false, true);
1552 if (!NT_STATUS_IS_OK(status)) {
1560 /****************************************************************************
1561 Make a directory of name "name".
1562 ****************************************************************************/
1564 static bool do_mkdir(const char *name)
1566 TALLOC_CTX *ctx = talloc_tos();
1567 struct cli_state *targetcli;
1568 char *targetname = NULL;
1571 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1572 cli, name, &targetcli, &targetname);
1573 if (!NT_STATUS_IS_OK(status)) {
1574 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1578 status = cli_mkdir(targetcli, targetname);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 d_printf("%s making remote directory %s\n",
1581 nt_errstr(status),name);
1588 /****************************************************************************
1589 Show 8.3 name of a file.
1590 ****************************************************************************/
1592 static bool do_altname(const char *name)
1597 status = cli_qpathinfo_alt_name(cli, name, altname);
1598 if (!NT_STATUS_IS_OK(status)) {
1599 d_printf("%s getting alt name for %s\n",
1600 nt_errstr(status),name);
1603 d_printf("%s\n", altname);
1608 /****************************************************************************
1610 ****************************************************************************/
1612 static int cmd_quit(void)
1615 popt_free_cmdline_auth_info();
1621 /****************************************************************************
1623 ****************************************************************************/
1625 static int cmd_mkdir(void)
1627 TALLOC_CTX *ctx = talloc_tos();
1632 mask = talloc_strdup(ctx, client_get_cur_dir());
1637 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1639 d_printf("mkdir <dirname>\n");
1643 mask = talloc_asprintf_append(mask, "%s", buf);
1647 mask = client_clean_name(ctx, mask);
1655 struct cli_state *targetcli;
1656 char *targetname = NULL;
1660 ddir2 = talloc_strdup(ctx, "");
1665 status = cli_resolve_path(ctx, "",
1666 popt_get_cmdline_auth_info(), cli, mask,
1667 &targetcli, &targetname);
1668 if (!NT_STATUS_IS_OK(status)) {
1672 ddir = talloc_strdup(ctx, targetname);
1676 trim_char(ddir,'.','\0');
1677 p = strtok_r(ddir, "/\\", &saveptr);
1679 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1683 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1686 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1690 p = strtok_r(NULL, "/\\", &saveptr);
1699 /****************************************************************************
1701 ****************************************************************************/
1703 static int cmd_altname(void)
1705 TALLOC_CTX *ctx = talloc_tos();
1709 name = talloc_strdup(ctx, client_get_cur_dir());
1714 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1715 d_printf("altname <file>\n");
1718 name = talloc_asprintf_append(name, "%s", buf);
1722 name = client_clean_name(ctx, name);
1730 static char *attr_str(TALLOC_CTX *mem_ctx, uint16_t mode)
1732 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1735 if (!(mode & FILE_ATTRIBUTE_NORMAL)) {
1736 if (mode & FILE_ATTRIBUTE_ENCRYPTED) {
1739 if (mode & FILE_ATTRIBUTE_NONINDEXED) {
1742 if (mode & FILE_ATTRIBUTE_OFFLINE) {
1745 if (mode & FILE_ATTRIBUTE_COMPRESSED) {
1748 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1751 if (mode & FILE_ATTRIBUTE_SPARSE) {
1754 if (mode & FILE_ATTRIBUTE_TEMPORARY) {
1757 if (mode & FILE_ATTRIBUTE_NORMAL) {
1760 if (mode & FILE_ATTRIBUTE_READONLY) {
1763 if (mode & FILE_ATTRIBUTE_HIDDEN) {
1766 if (mode & FILE_ATTRIBUTE_SYSTEM) {
1769 if (mode & FILE_ATTRIBUTE_DIRECTORY) {
1772 if (mode & FILE_ATTRIBUTE_ARCHIVE) {
1779 /****************************************************************************
1780 Show all info we can get
1781 ****************************************************************************/
1783 static int do_allinfo(const char *name)
1786 struct timespec b_time, a_time, m_time, c_time;
1791 unsigned int num_streams;
1792 struct stream_struct *streams;
1794 char **snapshots = NULL;
1798 status = cli_qpathinfo_alt_name(cli, name, altname);
1799 if (!NT_STATUS_IS_OK(status)) {
1800 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1803 * Ignore not supported or not implemented, it does not
1804 * hurt if we can't list alternate names.
1806 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1807 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1813 d_printf("altname: %s\n", altname);
1815 status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1816 &size, &mode, NULL);
1817 if (!NT_STATUS_IS_OK(status)) {
1818 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1823 tmp = full_timespec_to_nt_time(&b_time);
1824 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1826 tmp = full_timespec_to_nt_time(&a_time);
1827 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1829 tmp = full_timespec_to_nt_time(&m_time);
1830 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1832 tmp = full_timespec_to_nt_time(&c_time);
1833 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1835 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
1837 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1839 if (!NT_STATUS_IS_OK(status)) {
1840 d_printf("%s getting streams for %s\n", nt_errstr(status),
1845 for (i=0; i<num_streams; i++) {
1846 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1847 (unsigned long long)streams[i].size);
1850 if (mode & FILE_ATTRIBUTE_REPARSE_POINT) {
1851 char *subst, *print;
1854 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1856 if (!NT_STATUS_IS_OK(status)) {
1857 d_fprintf(stderr, "cli_readlink returned %s\n",
1860 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1861 subst, print, flags);
1867 status = cli_ntcreate(cli, name, 0,
1868 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1869 SEC_STD_SYNCHRONIZE, 0,
1870 FILE_SHARE_READ|FILE_SHARE_WRITE
1872 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
1873 if (!NT_STATUS_IS_OK(status)) {
1875 * Ignore failure, it does not hurt if we can't list
1881 * In order to get shadow copy data over SMB1 we
1882 * must call twice, once with 'get_names = false'
1883 * to get the size, then again with 'get_names = true'
1884 * to get the data or a Windows server fails to return
1885 * valid info. Samba doesn't have this bug. JRA.
1888 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1889 false, &snapshots, &num_snapshots);
1890 if (!NT_STATUS_IS_OK(status)) {
1891 cli_close(cli, fnum);
1894 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1895 true, &snapshots, &num_snapshots);
1896 if (!NT_STATUS_IS_OK(status)) {
1897 cli_close(cli, fnum);
1901 for (i=0; i<num_snapshots; i++) {
1904 d_printf("%s\n", snapshots[i]);
1905 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1906 snapshots[i], name);
1907 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1908 &m_time, &c_time, &size,
1910 if (!NT_STATUS_IS_OK(status)) {
1911 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1912 snap_name, nt_errstr(status));
1913 TALLOC_FREE(snap_name);
1916 tmp = unix_timespec_to_nt_time(b_time);
1917 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1918 tmp = unix_timespec_to_nt_time(a_time);
1919 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1920 tmp =unix_timespec_to_nt_time(m_time);
1921 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1922 tmp = unix_timespec_to_nt_time(c_time);
1923 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1924 d_printf("size: %d\n", (int)size);
1927 TALLOC_FREE(snapshots);
1928 cli_close(cli, fnum);
1933 /****************************************************************************
1934 Show all info we can get
1935 ****************************************************************************/
1937 static int cmd_allinfo(void)
1939 TALLOC_CTX *ctx = talloc_tos();
1943 name = talloc_strdup(ctx, client_get_cur_dir());
1948 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1949 d_printf("allinfo <file>\n");
1952 name = talloc_asprintf_append(name, "%s", buf);
1956 name = client_clean_name(ctx, name);
1965 /****************************************************************************
1967 ****************************************************************************/
1969 static int do_put(const char *rname, const char *lname, bool reput)
1971 TALLOC_CTX *ctx = talloc_tos();
1976 struct timespec tp_start;
1977 struct cli_state *targetcli;
1978 char *targetname = NULL;
1979 struct push_state state;
1982 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
1983 cli, rname, &targetcli, &targetname);
1984 if (!NT_STATUS_IS_OK(status)) {
1985 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1989 clock_gettime_mono(&tp_start);
1992 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1993 if (NT_STATUS_IS_OK(status)) {
1994 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1995 targetcli, fnum, NULL,
1997 NULL, NULL, NULL)) &&
1998 !NT_STATUS_IS_OK(status = cli_getattrE(
1999 targetcli, fnum, NULL,
2002 d_printf("getattrib: %s\n", nt_errstr(status));
2007 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
2010 if (!NT_STATUS_IS_OK(status)) {
2011 d_printf("%s opening remote file %s\n", nt_errstr(status),
2016 /* allow files to be piped into smbclient
2019 Note that in this case this function will exit(0) rather
2021 if (!strcmp(lname, "-")) {
2023 /* size of file is not known */
2025 f = fopen(lname, "r");
2027 if (fseek(f, start, SEEK_SET) == -1) {
2028 d_printf("Error seeking local file\n");
2036 d_printf("Error opening local file %s\n",lname);
2040 DEBUG(1,("putting file %s as %s ",lname,
2043 setvbuf(f, NULL, _IOFBF, io_bufsize);
2048 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
2050 if (!NT_STATUS_IS_OK(status)) {
2051 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
2055 status = cli_close(targetcli, fnum);
2056 if (!NT_STATUS_IS_OK(status)) {
2057 d_printf("%s closing remote file %s\n", nt_errstr(status),
2070 struct timespec tp_end;
2073 clock_gettime_mono(&tp_end);
2074 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
2075 put_total_time_ms += this_time;
2076 put_total_size += state.nread;
2078 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
2079 state.nread / (1.024*this_time + 1.0e-4),
2080 put_total_size / (1.024*put_total_time_ms)));
2085 popt_free_cmdline_auth_info();
2092 /****************************************************************************
2094 ****************************************************************************/
2096 static int cmd_put(void)
2098 TALLOC_CTX *ctx = talloc_tos();
2103 rname = talloc_strdup(ctx, client_get_cur_dir());
2108 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
2109 d_printf("put <filename>\n");
2113 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2114 rname = talloc_asprintf_append(rname, "%s", buf);
2116 rname = talloc_asprintf_append(rname, "%s", lname);
2122 rname = client_clean_name(ctx, rname);
2129 /* allow '-' to represent stdin
2130 jdblair, 24.jun.98 */
2131 if (!file_exist_stat(lname, &st, false) &&
2132 (strcmp(lname,"-"))) {
2133 d_printf("%s does not exist\n",lname);
2138 return do_put(rname, lname, false);
2141 /*************************************
2142 File list structure.
2143 *************************************/
2145 static struct file_list {
2146 struct file_list *prev, *next;
2151 /****************************************************************************
2152 Free a file_list structure.
2153 ****************************************************************************/
2155 static void free_file_list (struct file_list *l_head)
2157 struct file_list *list, *next;
2159 for (list = l_head; list; list = next) {
2161 DLIST_REMOVE(l_head, list);
2166 /****************************************************************************
2167 Seek in a directory/file list until you get something that doesn't start with
2169 ****************************************************************************/
2171 static bool seek_list(struct file_list *list, char *name)
2174 trim_string(list->file_path,"./","\n");
2175 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2184 /****************************************************************************
2185 Set the file selection mask.
2186 ****************************************************************************/
2188 static int cmd_select(void)
2190 TALLOC_CTX *ctx = talloc_tos();
2191 char *new_fs = NULL;
2192 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2195 client_set_fileselection(new_fs);
2197 client_set_fileselection("");
2202 /****************************************************************************
2203 Recursive file matching function act as find
2204 match must be always set to true when calling this function
2205 ****************************************************************************/
2207 static int file_find(TALLOC_CTX *ctx,
2208 struct file_list **list,
2209 const char *directory,
2210 const char *expression,
2214 struct file_list *entry;
2215 struct stat statbuf;
2221 dir = opendir(directory);
2225 while ((dname = readdirname(dir))) {
2226 if (!strcmp("..", dname))
2228 if (!strcmp(".", dname))
2231 path = talloc_asprintf(ctx, "%s/%s", directory, dname);
2237 if (!match || !gen_fnmatch(expression, dname)) {
2239 ret = stat(path, &statbuf);
2241 if (S_ISDIR(statbuf.st_mode)) {
2243 ret = file_find(ctx,
2250 d_printf("file_find: cannot stat file %s\n", path);
2259 entry = talloc_zero(ctx, struct file_list);
2261 d_printf("Out of memory in file_find\n");
2265 entry->file_path = talloc_move(entry, &path);
2266 entry->isdir = isdir;
2267 DLIST_ADD(*list, entry);
2277 /****************************************************************************
2279 ****************************************************************************/
2281 static int cmd_mput(void)
2283 TALLOC_CTX *ctx = talloc_tos();
2286 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2288 struct file_list *temp_list;
2289 char *quest, *lname, *rname;
2293 ret = file_find(ctx, &file_list, ".", p, true);
2295 free_file_list(file_list);
2303 for (temp_list = file_list; temp_list;
2304 temp_list = temp_list->next) {
2307 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2310 trim_string(lname, "./", "/");
2312 /* check if it's a directory */
2313 if (temp_list->isdir) {
2314 /* if (!recurse) continue; */
2317 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2320 if (prompt && !yesno(quest)) { /* No */
2321 /* Skip the directory */
2322 lname[strlen(lname)-1] = '/';
2323 if (!seek_list(temp_list, lname))
2327 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2330 normalize_name(rname);
2333 client_clean_name(ctx, rname);
2334 if (tmp_rname == NULL) {
2338 rname = smb_xstrdup(tmp_rname);
2339 TALLOC_FREE(tmp_rname);
2340 if (rname == NULL) {
2344 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2346 DEBUG (0, ("Unable to make dir, skipping..."));
2347 /* Skip the directory */
2348 lname[strlen(lname)-1] = '/';
2349 if (!seek_list(temp_list, lname)) {
2357 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2360 if (prompt && !yesno(quest)) {
2367 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2372 normalize_name(rname);
2375 char *tmp_rname = client_clean_name(ctx, rname);
2376 if (tmp_rname == NULL) {
2380 rname = smb_xstrdup(tmp_rname);
2381 TALLOC_FREE(tmp_rname);
2382 if (rname == NULL) {
2386 do_put(rname, lname, false);
2388 free_file_list(file_list);
2397 /****************************************************************************
2399 ****************************************************************************/
2401 static int do_cancel(int job)
2403 if (cli_printjob_del(cli, job)) {
2404 d_printf("Job %d cancelled\n",job);
2407 NTSTATUS status = cli_nt_error(cli);
2408 d_printf("Error cancelling job %d : %s\n",
2409 job, nt_errstr(status));
2414 /****************************************************************************
2416 ****************************************************************************/
2418 static int cmd_cancel(void)
2420 TALLOC_CTX *ctx = talloc_tos();
2424 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2425 d_printf("cancel <jobid> ...\n");
2431 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2436 /****************************************************************************
2438 ****************************************************************************/
2440 static int cmd_print(void)
2442 TALLOC_CTX *ctx = talloc_tos();
2447 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2448 d_printf("print <filename>\n");
2452 rname = talloc_strdup(ctx, lname);
2456 p = strrchr_m(rname,'/');
2458 rname = talloc_asprintf(ctx,
2463 if (strequal(lname,"-")) {
2464 rname = talloc_asprintf(ctx,
2472 return do_put(rname, lname, false);
2475 /****************************************************************************
2476 Show a print queue entry.
2477 ****************************************************************************/
2479 static void queue_fn(struct print_job_info *p)
2481 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2484 /****************************************************************************
2486 ****************************************************************************/
2488 static int cmd_queue(void)
2490 cli_print_queue(cli, queue_fn);
2494 /****************************************************************************
2496 ****************************************************************************/
2498 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2501 TALLOC_CTX *ctx = talloc_tos();
2505 mask = talloc_asprintf(ctx,
2511 return NT_STATUS_NO_MEMORY;
2514 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2516 return NT_STATUS_OK;
2519 status = cli_unlink(cli_state, mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2520 if (!NT_STATUS_IS_OK(status)) {
2521 d_printf("%s deleting remote file %s\n",
2522 nt_errstr(status), mask);
2528 /****************************************************************************
2530 ****************************************************************************/
2532 static int cmd_del(void)
2534 TALLOC_CTX *ctx = talloc_tos();
2537 NTSTATUS status = NT_STATUS_OK;
2538 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2541 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2544 mask = talloc_strdup(ctx, client_get_cur_dir());
2548 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2549 d_printf("del <filename>\n");
2552 mask = talloc_asprintf_append(mask, "%s", buf);
2556 mask = client_clean_name(ctx, mask);
2561 status = do_list(mask,attribute,do_del,false,false);
2562 if (!NT_STATUS_IS_OK(status)) {
2568 /****************************************************************************
2570 ****************************************************************************/
2572 static NTSTATUS delete_remote_files_list(struct cli_state *cli_state,
2573 struct file_list *flist)
2575 NTSTATUS status = NT_STATUS_OK;
2576 struct file_list *deltree_list_iter = NULL;
2578 for (deltree_list_iter = flist;
2579 deltree_list_iter != NULL;
2580 deltree_list_iter = deltree_list_iter->next) {
2581 if (CLI_DIRSEP_CHAR == '/') {
2583 status = cli_posix_unlink(cli_state,
2584 deltree_list_iter->file_path);
2585 } else if (deltree_list_iter->isdir) {
2586 status = cli_rmdir(cli_state,
2587 deltree_list_iter->file_path);
2589 status = cli_unlink(cli_state,
2590 deltree_list_iter->file_path,
2591 FILE_ATTRIBUTE_SYSTEM |
2592 FILE_ATTRIBUTE_HIDDEN);
2594 if (!NT_STATUS_IS_OK(status)) {
2595 d_printf("%s deleting remote %s %s\n",
2597 deltree_list_iter->isdir ?
2598 "directory" : "file",
2599 deltree_list_iter->file_path);
2603 return NT_STATUS_OK;
2606 /****************************************************************************
2607 Save a list of files to delete.
2608 ****************************************************************************/
2610 static struct file_list *deltree_list_head;
2612 static NTSTATUS do_deltree_list(struct cli_state *cli_state,
2613 struct file_info *finfo,
2616 struct file_list **file_list_head_pp = &deltree_list_head;
2617 struct file_list *dt = NULL;
2619 if (!do_this_one(finfo)) {
2620 return NT_STATUS_OK;
2623 /* skip if this is . or .. */
2624 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
2625 return NT_STATUS_OK;
2628 dt = talloc_zero(NULL, struct file_list);
2630 return NT_STATUS_NO_MEMORY;
2633 /* create absolute filename for cli_ntcreate() */
2634 dt->file_path = talloc_asprintf(dt,
2639 if (dt->file_path == NULL) {
2641 return NT_STATUS_NO_MEMORY;
2644 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
2648 DLIST_ADD(*file_list_head_pp, dt);
2649 return NT_STATUS_OK;
2652 static int cmd_deltree(void)
2654 TALLOC_CTX *ctx = talloc_tos();
2656 NTSTATUS status = NT_STATUS_OK;
2657 struct file_list *deltree_list_norecurse = NULL;
2658 struct file_list *deltree_list_iter = NULL;
2659 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM |
2660 FILE_ATTRIBUTE_HIDDEN |
2661 FILE_ATTRIBUTE_DIRECTORY;
2663 char *mask = talloc_strdup(ctx, client_get_cur_dir());
2667 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
2669 d_printf("deltree <filename>\n");
2672 mask = talloc_asprintf_append(mask, "%s", buf);
2676 mask = client_clean_name(ctx, mask);
2681 deltree_list_head = NULL;
2684 * Get the list of directories to
2685 * delete (in case mask has a wildcard).
2687 status = do_list(mask, attribute, do_deltree_list, false, true);
2688 if (!NT_STATUS_IS_OK(status)) {
2691 deltree_list_norecurse = deltree_list_head;
2692 deltree_list_head = NULL;
2694 for (deltree_list_iter = deltree_list_norecurse;
2695 deltree_list_iter != NULL;
2696 deltree_list_iter = deltree_list_iter->next) {
2698 if (deltree_list_iter->isdir == false) {
2699 /* Just a regular file. */
2700 if (CLI_DIRSEP_CHAR == '/') {
2702 status = cli_posix_unlink(cli,
2703 deltree_list_iter->file_path);
2705 status = cli_unlink(cli,
2706 deltree_list_iter->file_path,
2707 FILE_ATTRIBUTE_SYSTEM |
2708 FILE_ATTRIBUTE_HIDDEN);
2710 if (!NT_STATUS_IS_OK(status)) {
2717 * Get the list of files or directories to
2718 * delete in depth order.
2720 status = do_list(deltree_list_iter->file_path,
2725 if (!NT_STATUS_IS_OK(status)) {
2728 status = delete_remote_files_list(cli, deltree_list_head);
2729 free_file_list(deltree_list_head);
2730 deltree_list_head = NULL;
2731 if (!NT_STATUS_IS_OK(status)) {
2736 free_file_list(deltree_list_norecurse);
2737 free_file_list(deltree_list_head);
2742 free_file_list(deltree_list_norecurse);
2743 free_file_list(deltree_list_head);
2744 deltree_list_head = NULL;
2749 /****************************************************************************
2750 Wildcard delete some files.
2751 ****************************************************************************/
2753 static int cmd_wdel(void)
2755 TALLOC_CTX *ctx = talloc_tos();
2759 struct cli_state *targetcli;
2760 char *targetname = NULL;
2763 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2764 d_printf("wdel 0x<attrib> <wcard>\n");
2768 attribute = (uint16_t)strtol(buf, (char **)NULL, 16);
2770 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2771 d_printf("wdel 0x<attrib> <wcard>\n");
2775 mask = talloc_asprintf(ctx, "%s%s",
2776 client_get_cur_dir(),
2781 mask = client_clean_name(ctx, mask);
2786 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2787 cli, mask, &targetcli, &targetname);
2788 if (!NT_STATUS_IS_OK(status)) {
2789 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2793 status = cli_unlink(targetcli, targetname, attribute);
2794 if (!NT_STATUS_IS_OK(status)) {
2795 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2801 /****************************************************************************
2802 ****************************************************************************/
2804 static int cmd_open(void)
2806 TALLOC_CTX *ctx = talloc_tos();
2809 char *targetname = NULL;
2810 struct cli_state *targetcli;
2811 uint16_t fnum = (uint16_t)-1;
2814 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2815 d_printf("open <filename>\n");
2818 mask = talloc_asprintf(ctx,
2820 client_get_cur_dir(),
2826 mask = client_clean_name(ctx, mask);
2831 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2832 cli, mask, &targetcli, &targetname);
2833 if (!NT_STATUS_IS_OK(status)) {
2834 d_printf("open %s: %s\n", mask, nt_errstr(status));
2838 status = cli_ntcreate(targetcli, targetname, 0,
2839 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2840 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2841 0x0, 0x0, &fnum, NULL);
2842 if (!NT_STATUS_IS_OK(status)) {
2843 status = cli_ntcreate(targetcli, targetname, 0,
2845 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2846 0x0, 0x0, &fnum, NULL);
2847 if (NT_STATUS_IS_OK(status)) {
2848 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2850 d_printf("Failed to open file %s. %s\n",
2851 targetname, nt_errstr(status));
2854 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2859 static int cmd_posix_encrypt(void)
2861 TALLOC_CTX *ctx = talloc_tos();
2862 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2863 char *domain = NULL;
2865 char *password = NULL;
2866 struct cli_credentials *creds = NULL;
2867 struct cli_credentials *lcreds = NULL;
2869 if (next_token_talloc(ctx, &cmd_ptr, &domain, NULL)) {
2871 if (!next_token_talloc(ctx, &cmd_ptr, &user, NULL)) {
2872 d_printf("posix_encrypt domain user password\n");
2876 if (!next_token_talloc(ctx, &cmd_ptr, &password, NULL)) {
2877 d_printf("posix_encrypt domain user password\n");
2881 lcreds = cli_session_creds_init(ctx,
2886 false, /* use_kerberos */
2887 false, /* fallback_after_kerberos */
2888 false, /* use_ccache */
2889 false); /* password_is_nt_hash */
2890 if (lcreds == NULL) {
2891 d_printf("cli_session_creds_init() failed.\n");
2896 bool auth_requested = false;
2898 creds = get_cmdline_auth_info_creds(
2899 popt_get_cmdline_auth_info());
2901 auth_requested = cli_credentials_authentication_requested(creds);
2902 if (!auth_requested) {
2903 d_printf("posix_encrypt domain user password\n");
2908 status = cli_smb1_setup_encryption(cli, creds);
2909 /* gensec currently references the creds so we can't free them here */
2910 talloc_unlink(ctx, lcreds);
2911 if (!NT_STATUS_IS_OK(status)) {
2912 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2914 d_printf("encryption on\n");
2921 /****************************************************************************
2922 ****************************************************************************/
2924 static int cmd_posix_open(void)
2926 TALLOC_CTX *ctx = talloc_tos();
2929 char *targetname = NULL;
2930 struct cli_state *targetcli;
2935 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2936 d_printf("posix_open <filename> 0<mode>\n");
2939 mask = talloc_asprintf(ctx,
2941 client_get_cur_dir(),
2946 mask = client_clean_name(ctx, mask);
2951 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2952 d_printf("posix_open <filename> 0<mode>\n");
2955 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2957 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
2958 cli, mask, &targetcli, &targetname);
2959 if (!NT_STATUS_IS_OK(status)) {
2960 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2964 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2966 if (!NT_STATUS_IS_OK(status)) {
2967 status = cli_posix_open(targetcli, targetname,
2968 O_CREAT|O_RDONLY, mode, &fnum);
2969 if (!NT_STATUS_IS_OK(status)) {
2970 d_printf("Failed to open file %s. %s\n", targetname,
2973 d_printf("posix_open file %s: for readonly fnum %d\n",
2977 d_printf("posix_open file %s: for read/write fnum %d\n",
2984 static int cmd_posix_mkdir(void)
2986 TALLOC_CTX *ctx = talloc_tos();
2989 char *targetname = NULL;
2990 struct cli_state *targetcli;
2994 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2995 d_printf("posix_mkdir <filename> 0<mode>\n");
2998 mask = talloc_asprintf(ctx,
3000 client_get_cur_dir(),
3005 mask = client_clean_name(ctx, mask);
3010 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3011 d_printf("posix_mkdir <filename> 0<mode>\n");
3014 mode = (mode_t)strtol(buf, (char **)NULL, 8);
3016 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3017 cli, mask, &targetcli, &targetname);
3018 if (!NT_STATUS_IS_OK(status)) {
3019 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
3023 status = cli_posix_mkdir(targetcli, targetname, mode);
3024 if (!NT_STATUS_IS_OK(status)) {
3025 d_printf("Failed to open file %s. %s\n",
3026 targetname, nt_errstr(status));
3028 d_printf("posix_mkdir created directory %s\n", targetname);
3033 static int cmd_posix_unlink(void)
3035 TALLOC_CTX *ctx = talloc_tos();
3038 char *targetname = NULL;
3039 struct cli_state *targetcli;
3042 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3043 d_printf("posix_unlink <filename>\n");
3046 mask = talloc_asprintf(ctx,
3048 client_get_cur_dir(),
3053 mask = client_clean_name(ctx, mask);
3058 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3059 cli, mask, &targetcli, &targetname);
3060 if (!NT_STATUS_IS_OK(status)) {
3061 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
3065 status = cli_posix_unlink(targetcli, targetname);
3066 if (!NT_STATUS_IS_OK(status)) {
3067 d_printf("Failed to unlink file %s. %s\n",
3068 targetname, nt_errstr(status));
3070 d_printf("posix_unlink deleted file %s\n", targetname);
3076 static int cmd_posix_rmdir(void)
3078 TALLOC_CTX *ctx = talloc_tos();
3081 char *targetname = NULL;
3082 struct cli_state *targetcli;
3085 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3086 d_printf("posix_rmdir <filename>\n");
3089 mask = talloc_asprintf(ctx,
3091 client_get_cur_dir(),
3096 mask = client_clean_name(ctx, mask);
3101 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3102 cli, mask, &targetcli, &targetname);
3103 if (!NT_STATUS_IS_OK(status)) {
3104 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
3108 status = cli_posix_rmdir(targetcli, targetname);
3109 if (!NT_STATUS_IS_OK(status)) {
3110 d_printf("Failed to unlink directory %s. %s\n",
3111 targetname, nt_errstr(status));
3113 d_printf("posix_rmdir deleted directory %s\n", targetname);
3119 static int cmd_close(void)
3121 TALLOC_CTX *ctx = talloc_tos();
3126 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3127 d_printf("close <fnum>\n");
3132 /* We really should use the targetcli here.... */
3133 status = cli_close(cli, fnum);
3134 if (!NT_STATUS_IS_OK(status)) {
3135 d_printf("close %d: %s\n", fnum, nt_errstr(status));
3141 static int cmd_posix(void)
3143 TALLOC_CTX *ctx = talloc_tos();
3144 uint16_t major, minor;
3145 uint32_t caplow, caphigh;
3149 if (!SERVER_HAS_UNIX_CIFS(cli)) {
3150 d_printf("Server doesn't support UNIX CIFS extensions.\n");
3154 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
3156 if (!NT_STATUS_IS_OK(status)) {
3157 d_printf("Can't get UNIX CIFS extensions version from "
3158 "server: %s\n", nt_errstr(status));
3162 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
3164 caps = talloc_strdup(ctx, "");
3168 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
3169 caps = talloc_asprintf_append(caps, "locks ");
3174 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
3175 caps = talloc_asprintf_append(caps, "acls ");
3180 if (caplow & CIFS_UNIX_XATTTR_CAP) {
3181 caps = talloc_asprintf_append(caps, "eas ");
3186 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3187 caps = talloc_asprintf_append(caps, "pathnames ");
3192 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
3193 caps = talloc_asprintf_append(caps, "posix_path_operations ");
3198 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
3199 caps = talloc_asprintf_append(caps, "large_read ");
3204 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
3205 caps = talloc_asprintf_append(caps, "large_write ");
3210 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
3211 caps = talloc_asprintf_append(caps, "posix_encrypt ");
3216 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
3217 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
3223 if (*caps && caps[strlen(caps)-1] == ' ') {
3224 caps[strlen(caps)-1] = '\0';
3227 d_printf("Server supports CIFS capabilities %s\n", caps);
3229 status = cli_set_unix_extensions_capabilities(cli, major, minor,
3231 if (!NT_STATUS_IS_OK(status)) {
3232 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
3237 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3238 CLI_DIRSEP_CHAR = '/';
3239 *CLI_DIRSEP_STR = '/';
3240 client_set_cur_dir(CLI_DIRSEP_STR);
3246 static int cmd_lock(void)
3248 TALLOC_CTX *ctx = talloc_tos();
3250 uint64_t start, len;
3251 enum brl_type lock_type;
3255 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3256 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3261 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3262 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3266 if (*buf == 'r' || *buf == 'R') {
3267 lock_type = READ_LOCK;
3268 } else if (*buf == 'w' || *buf == 'W') {
3269 lock_type = WRITE_LOCK;
3271 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3275 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3276 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3280 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3282 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3283 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3287 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3289 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
3290 if (!NT_STATUS_IS_OK(status)) {
3291 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
3297 static int cmd_unlock(void)
3299 TALLOC_CTX *ctx = talloc_tos();
3301 uint64_t start, len;
3305 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3306 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3311 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3312 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3316 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3318 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3319 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3323 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3325 status = cli_posix_unlock(cli, fnum, start, len);
3326 if (!NT_STATUS_IS_OK(status)) {
3327 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
3333 static int cmd_posix_whoami(void)
3335 TALLOC_CTX *ctx = talloc_tos();
3336 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
3339 uint32_t num_gids = 0;
3340 uint32_t num_sids = 0;
3341 uint64_t *gids = NULL;
3342 struct dom_sid *sids = NULL;
3346 status = cli_posix_whoami(cli,
3356 if (!NT_STATUS_IS_OK(status)) {
3357 d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
3361 d_printf("GUEST:%s\n", guest ? "True" : "False");
3362 d_printf("UID:%" PRIu64 "\n", uid);
3363 d_printf("GID:%" PRIu64 "\n", gid);
3364 d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
3365 for (i = 0; i < num_gids; i++) {
3366 d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
3368 d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
3369 for (i = 0; i < num_sids; i++) {
3370 struct dom_sid_buf buf;
3371 d_printf("SIDS[%" PRIu32 "]:%s\n",
3373 dom_sid_str_buf(&sids[i], &buf));
3379 /****************************************************************************
3381 ****************************************************************************/
3383 static int cmd_rmdir(void)
3385 TALLOC_CTX *ctx = talloc_tos();
3388 char *targetname = NULL;
3389 struct cli_state *targetcli;
3392 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3393 d_printf("rmdir <dirname>\n");
3396 mask = talloc_asprintf(ctx,
3398 client_get_cur_dir(),
3403 mask = client_clean_name(ctx, mask);
3408 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3409 cli, mask, &targetcli, &targetname);
3410 if (!NT_STATUS_IS_OK(status)) {
3411 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3415 status = cli_rmdir(targetcli, targetname);
3416 if (!NT_STATUS_IS_OK(status)) {
3417 d_printf("%s removing remote directory file %s\n",
3418 nt_errstr(status), mask);
3424 /****************************************************************************
3426 ****************************************************************************/
3428 static int cmd_link(void)
3430 TALLOC_CTX *ctx = talloc_tos();
3431 char *oldname = NULL;
3432 char *newname = NULL;
3435 char *targetname = NULL;
3436 struct cli_state *targetcli;
3439 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3440 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3441 d_printf("link <oldname> <newname>\n");
3444 oldname = talloc_asprintf(ctx,
3446 client_get_cur_dir(),
3451 oldname = client_clean_name(ctx, oldname);
3452 if (oldname == NULL) {
3455 newname = talloc_asprintf(ctx,
3457 client_get_cur_dir(),
3462 newname = client_clean_name(ctx, newname);
3463 if (newname == NULL) {
3467 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3468 cli, oldname, &targetcli, &targetname);
3469 if (!NT_STATUS_IS_OK(status)) {
3470 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3474 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3475 d_printf("Server doesn't support UNIX CIFS calls.\n");
3479 status = cli_posix_hardlink(targetcli, targetname, newname);
3480 if (!NT_STATUS_IS_OK(status)) {
3481 d_printf("%s linking files (%s -> %s)\n",
3482 nt_errstr(status), newname, oldname);
3488 /****************************************************************************
3490 ****************************************************************************/
3492 static int cmd_readlink(void)
3494 TALLOC_CTX *ctx = talloc_tos();
3497 char *targetname = NULL;
3498 char *linkname = NULL;
3499 struct cli_state *targetcli;
3502 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3503 d_printf("readlink <name>\n");
3506 name = talloc_asprintf(ctx,
3508 client_get_cur_dir(),
3513 name = client_clean_name(ctx, name);
3518 status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
3519 cli, name, &targetcli, &targetname);
3520 if (!NT_STATUS_IS_OK(status)) {
3521 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3525 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3526 d_printf("Server doesn't support UNIX CIFS calls.\n");
3530 status = cli_posix_readlink(targetcli, name, talloc_tos(), &linkname);
3531 if (!NT_STATUS_IS_OK(status)) {
3532 d_printf("%s readlink on file %s\n",
3533 nt_errstr(status), name);
3537 d_printf("%s -> %s\n", name, linkname);
3539 TALLOC_FREE(linkname);
3545 /****************************************************************************
3547 ****************************************************************************/
3549 static int cmd_symlink(void)
3551 TALLOC_CTX *ctx = talloc_tos();