libsmb: Use clistr_smb2_extract_snapshot_token() in cli_smb2_create_fnum_send()
[Samba.git] / source3 / client / client.c
blobf7719db118b21d016bebbb40a8b2f2bfd96e1dd4
1 /*
2 Unix SMB/CIFS implementation.
3 SMB client
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/>.
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "rpc_client/cli_pipe.h"
27 #include "client/client_proto.h"
28 #include "client/clitar_proto.h"
29 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
30 #include "../lib/util/select.h"
31 #include "system/readline.h"
32 #include "../libcli/smbreadline/smbreadline.h"
33 #include "../libcli/security/security.h"
34 #include "system/select.h"
35 #include "libsmb/libsmb.h"
36 #include "libsmb/clirap.h"
37 #include "trans2.h"
38 #include "libsmb/nmblib.h"
39 #include "include/ntioctl.h"
40 #include "../libcli/smb/smbXcli_base.h"
41 #include "lib/util/time_basic.h"
42 #include "lib/util/string_wrappers.h"
43 #include "lib/cmdline/cmdline.h"
45 #ifndef REGISTER
46 #define REGISTER 0
47 #endif
49 extern int do_smb_browse(void); /* mDNS browsing */
51 static int port = 0;
52 static char *service;
53 static char *desthost;
54 static bool grepable = false;
55 static bool quiet = false;
56 static char *cmdstr = NULL;
57 const char *cmd_ptr = NULL;
59 static int io_bufsize = 0; /* we use the default size */
60 static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
62 static int name_type = 0x20;
64 static int process_tok(char *tok);
65 static int cmd_help(void);
67 /* value for unused fid field in trans2 secondary request */
68 #define FID_UNUSED (0xFFFF)
70 time_t newer_than = 0;
71 static int archive_level = 0;
73 static bool translation = false;
74 static bool have_ip;
76 static bool prompt = true;
78 static bool recurse = false;
79 static bool showacls = false;
80 bool lowercase = false;
81 static bool backup_intent = false;
83 static struct sockaddr_storage dest_ss;
84 static char dest_ss_str[INET6_ADDRSTRLEN];
86 #define SEPARATORS " \t\n\r"
88 /* timing globals */
89 uint64_t get_total_size = 0;
90 unsigned int get_total_time_ms = 0;
91 static uint64_t put_total_size = 0;
92 static unsigned int put_total_time_ms = 0;
94 /* totals globals */
95 static double dir_total;
97 /* root cli_state connection */
99 struct cli_state *cli;
101 static char CLI_DIRSEP_CHAR = '\\';
102 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
104 /* Accessor functions for directory paths. */
105 static char *fileselection;
106 static const char *client_get_fileselection(void)
108 if (fileselection) {
109 return fileselection;
111 return "";
114 static const char *client_set_fileselection(const char *new_fs)
116 SAFE_FREE(fileselection);
117 if (new_fs) {
118 fileselection = SMB_STRDUP(new_fs);
120 return client_get_fileselection();
123 static char *cwd;
124 static const char *client_get_cwd(void)
126 if (cwd) {
127 return cwd;
129 return CLI_DIRSEP_STR;
132 static const char *client_set_cwd(const char *new_cwd)
134 SAFE_FREE(cwd);
135 if (new_cwd) {
136 cwd = SMB_STRDUP(new_cwd);
138 return client_get_cwd();
141 static char *cur_dir;
142 const char *client_get_cur_dir(void)
144 if (cur_dir) {
145 return cur_dir;
147 return CLI_DIRSEP_STR;
150 const char *client_set_cur_dir(const char *newdir)
152 SAFE_FREE(cur_dir);
153 if (newdir) {
154 cur_dir = SMB_STRDUP(newdir);
156 return client_get_cur_dir();
159 /****************************************************************************
160 Put up a yes/no prompt.
161 ****************************************************************************/
163 static bool yesno(const char *p)
165 char ans[20];
166 printf("%s",p);
168 if (!fgets(ans,sizeof(ans)-1,stdin))
169 return(False);
171 if (*ans == 'y' || *ans == 'Y')
172 return(True);
174 return(False);
177 /****************************************************************************
178 Write to a local file with CR/LF->LF translation if appropriate. Return the
179 number taken from the buffer. This may not equal the number written.
180 ****************************************************************************/
182 static ssize_t writefile(int f, char *b, size_t n)
184 size_t i = 0;
186 if (n == 0) {
187 errno = EINVAL;
188 return -1;
191 if (!translation) {
192 return write(f,b,n);
195 do {
196 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
197 b++;i++;
199 if (write(f, b, 1) != 1) {
200 break;
202 b++;
203 i++;
204 } while (i < n);
206 return (ssize_t)i;
209 /****************************************************************************
210 Read from a file with LF->CR/LF translation if appropriate. Return the
211 number read. read approx n bytes.
212 ****************************************************************************/
214 static int readfile(uint8_t *b, int n, FILE *f)
216 int i;
217 int c;
219 if (!translation)
220 return fread(b,1,n,f);
222 i = 0;
223 while (i < (n - 1)) {
224 if ((c = getc(f)) == EOF) {
225 break;
228 if (c == '\n') { /* change all LFs to CR/LF */
229 b[i++] = '\r';
232 b[i++] = c;
235 return(i);
238 struct push_state {
239 FILE *f;
240 off_t nread;
243 static size_t push_source(uint8_t *buf, size_t n, void *priv)
245 struct push_state *state = (struct push_state *)priv;
246 int result;
248 if (feof(state->f)) {
249 return 0;
252 result = readfile(buf, n, state->f);
253 state->nread += result;
254 return result;
257 /****************************************************************************
258 Send a message.
259 ****************************************************************************/
261 static void send_message(const char *username)
263 char buf[1600];
264 NTSTATUS status;
265 size_t i;
267 d_printf("Type your message, ending it with a Control-D\n");
269 i = 0;
270 while (i<sizeof(buf)-2) {
271 int c = fgetc(stdin);
272 if (c == EOF) {
273 break;
275 if (c == '\n') {
276 buf[i++] = '\r';
278 buf[i++] = c;
280 buf[i] = '\0';
282 status = cli_message(cli, desthost, username, buf);
283 if (!NT_STATUS_IS_OK(status)) {
284 d_fprintf(stderr, "cli_message returned %s\n",
285 nt_errstr(status));
289 /****************************************************************************
290 Check the space on a device.
291 ****************************************************************************/
293 static int do_dskattr(void)
295 uint64_t total, bsize, avail;
296 struct cli_state *targetcli = NULL;
297 char *targetpath = NULL;
298 TALLOC_CTX *ctx = talloc_tos();
299 struct cli_credentials *creds = samba_cmdline_get_creds();
300 NTSTATUS status;
302 status = cli_resolve_path(ctx,
304 creds,
305 cli,
306 client_get_cur_dir(), &targetcli,
307 &targetpath);
308 if (!NT_STATUS_IS_OK(status)) {
309 d_printf("Error in dskattr: %s\n", nt_errstr(status));
310 return 1;
313 status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
314 if (!NT_STATUS_IS_OK(status)) {
315 d_printf("Error in dskattr: %s\n", nt_errstr(status));
316 return 1;
319 d_printf("\n\t\t%" PRIu64
320 " blocks of size %" PRIu64
321 ". %" PRIu64 " blocks available\n",
322 total, bsize, avail);
324 return 0;
327 /****************************************************************************
328 Show cd/pwd.
329 ****************************************************************************/
331 static int cmd_pwd(void)
333 d_printf("Current directory is %s",service);
334 d_printf("%s\n",client_get_cur_dir());
335 return 0;
338 /****************************************************************************
339 Ensure name has correct directory separators.
340 ****************************************************************************/
342 static void normalize_name(char *newdir)
344 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
345 string_replace(newdir,'/','\\');
349 /****************************************************************************
350 Local name cleanup before sending to server. SMB1 allows relative pathnames,
351 but SMB2 does not, so we need to resolve them locally.
352 ****************************************************************************/
354 char *client_clean_name(TALLOC_CTX *ctx, const char *name)
356 char *newname = NULL;
357 if (name == NULL) {
358 return NULL;
361 /* First ensure any path separators are correct. */
362 newname = talloc_strdup(ctx, name);
363 if (newname == NULL) {
364 return NULL;
366 normalize_name(newname);
368 /* Now remove any relative (..) path components. */
369 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
370 newname = unix_clean_name(ctx, newname);
371 } else {
372 newname = clean_name(ctx, newname);
374 if (newname == NULL) {
375 return NULL;
377 return newname;
380 /****************************************************************************
381 Change directory - inner section.
382 ****************************************************************************/
384 static int do_cd(const char *new_dir)
386 char *newdir = NULL;
387 char *saved_dir = NULL;
388 char *new_cd = NULL;
389 char *targetpath = NULL;
390 struct cli_state *targetcli = NULL;
391 int ret = 1;
392 TALLOC_CTX *ctx = talloc_stackframe();
393 struct cli_credentials *creds = samba_cmdline_get_creds();
394 NTSTATUS status;
396 newdir = talloc_strdup(ctx, new_dir);
397 if (!newdir) {
398 TALLOC_FREE(ctx);
399 return 1;
402 normalize_name(newdir);
404 /* Save the current directory in case the new directory is invalid */
406 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
407 if (!saved_dir) {
408 TALLOC_FREE(ctx);
409 return 1;
412 if (*newdir == CLI_DIRSEP_CHAR) {
413 client_set_cur_dir(newdir);
414 new_cd = newdir;
415 } else {
416 new_cd = talloc_asprintf(ctx, "%s%s",
417 client_get_cur_dir(),
418 newdir);
419 if (!new_cd) {
420 goto out;
424 /* Ensure cur_dir ends in a DIRSEP */
425 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
426 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
427 if (!new_cd) {
428 goto out;
431 client_set_cur_dir(new_cd);
433 new_cd = client_clean_name(ctx, new_cd);
434 client_set_cur_dir(new_cd);
436 status = cli_resolve_path(ctx, "",
437 creds,
438 cli, new_cd, &targetcli, &targetpath);
439 if (!NT_STATUS_IS_OK(status)) {
440 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
441 client_set_cur_dir(saved_dir);
442 goto out;
445 if (strequal(targetpath,CLI_DIRSEP_STR )) {
446 TALLOC_FREE(ctx);
447 return 0;
451 targetpath = talloc_asprintf(
452 ctx, "%s%s", targetpath, CLI_DIRSEP_STR);
453 if (!targetpath) {
454 client_set_cur_dir(saved_dir);
455 goto out;
457 targetpath = client_clean_name(ctx, targetpath);
458 if (!targetpath) {
459 client_set_cur_dir(saved_dir);
460 goto out;
463 status = cli_chkpath(targetcli, targetpath);
464 if (!NT_STATUS_IS_OK(status)) {
465 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
466 client_set_cur_dir(saved_dir);
467 goto out;
470 ret = 0;
472 out:
474 TALLOC_FREE(ctx);
475 return ret;
478 /****************************************************************************
479 Change directory.
480 ****************************************************************************/
482 static int cmd_cd(void)
484 char *buf = NULL;
485 int rc = 0;
487 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
488 rc = do_cd(buf);
489 } else {
490 d_printf("Current directory is %s\n",client_get_cur_dir());
493 return rc;
496 /****************************************************************************
497 Change directory.
498 ****************************************************************************/
500 static int cmd_cd_oneup(void)
502 return do_cd("..");
505 /*******************************************************************
506 Decide if a file should be operated on.
507 ********************************************************************/
509 static bool do_this_one(struct file_info *finfo)
511 if (!finfo->name) {
512 return false;
515 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
516 return true;
519 if (*client_get_fileselection() &&
520 !mask_match(finfo->name,client_get_fileselection(),false)) {
521 DEBUG(3,("mask_match %s failed\n", finfo->name));
522 return false;
525 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
526 DEBUG(3,("newer_than %s failed\n", finfo->name));
527 return false;
530 if ((archive_level==1 || archive_level==2) && !(finfo->attr & FILE_ATTRIBUTE_ARCHIVE)) {
531 DEBUG(3,("archive %s failed\n", finfo->name));
532 return false;
535 return true;
538 /****************************************************************************
539 Display info about a file.
540 ****************************************************************************/
542 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
543 const char *dir)
545 time_t t;
546 TALLOC_CTX *ctx = talloc_tos();
547 NTSTATUS status = NT_STATUS_OK;
549 if (!do_this_one(finfo)) {
550 return NT_STATUS_OK;
553 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
554 if (!showacls) {
555 d_printf(" %-30s%7.7s %8.0f %s",
556 finfo->name,
557 attrib_string(talloc_tos(), finfo->attr),
558 (double)finfo->size,
559 time_to_asc(t));
560 dir_total += finfo->size;
561 } else {
562 struct cli_state *targetcli = NULL;
563 char *targetpath = NULL;
564 char *afname = NULL;
565 uint16_t fnum;
566 struct cli_credentials *creds = samba_cmdline_get_creds();
568 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
569 return NT_STATUS_OK;
571 /* create absolute filename for cli_ntcreate() FIXME */
572 afname = talloc_asprintf(ctx,
573 "%s%s%s",
574 dir,
575 CLI_DIRSEP_STR,
576 finfo->name);
577 if (!afname) {
578 return NT_STATUS_NO_MEMORY;
580 /* print file meta date header */
581 d_printf( "FILENAME:%s\n", finfo->name);
582 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->attr));
583 d_printf( "SIZE:%.0f\n", (double)finfo->size);
584 d_printf( "MTIME:%s", time_to_asc(t));
586 status = cli_resolve_path(
587 ctx,
589 creds,
590 cli_state,
591 afname,
592 &targetcli,
593 &targetpath);
594 if (!NT_STATUS_IS_OK(status)) {
595 DBG_WARNING("display_finfo() Failed to resolve "
596 "%s: %s\n",
597 afname, nt_errstr(status));
598 return status;
601 status = cli_ntcreate(
602 targetcli, /* cli */
603 targetpath, /* fname */
604 0, /* CreatFlags */
605 READ_CONTROL_ACCESS, /* DesiredAccess */
606 0, /* FileAttributes */
607 FILE_SHARE_READ|
608 FILE_SHARE_WRITE, /* ShareAccess */
609 FILE_OPEN, /* CreateDisposition */
610 0x0, /* CreateOptions */
611 0x0, /* SecurityFlags */
612 &fnum, /* pfid */
613 NULL); /* cr */
614 if (!NT_STATUS_IS_OK(status)) {
615 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
616 afname, nt_errstr(status)));
617 } else {
618 struct security_descriptor *sd = NULL;
619 status = cli_query_secdesc(targetcli, fnum,
620 ctx, &sd);
621 if (!NT_STATUS_IS_OK(status)) {
622 DEBUG( 0, ("display_finfo() failed to "
623 "get security descriptor: %s",
624 nt_errstr(status)));
625 } else {
626 display_sec_desc(sd);
628 TALLOC_FREE(sd);
630 TALLOC_FREE(afname);
632 return status;
635 /****************************************************************************
636 Accumulate size of a file.
637 ****************************************************************************/
639 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
640 const char *dir)
642 if (do_this_one(finfo)) {
643 dir_total += finfo->size;
645 return NT_STATUS_OK;
648 struct do_list_queue_entry {
649 struct do_list_queue_entry *prev, *next;
650 char name[];
653 struct do_list_queue {
654 struct do_list_queue_entry *list;
657 static bool do_list_recurse;
658 static bool do_list_dirs;
659 static struct do_list_queue *queue = NULL;
660 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
661 const char *dir);
663 /****************************************************************************
664 Functions for do_list_queue.
665 ****************************************************************************/
667 static void reset_do_list_queue(void)
669 TALLOC_FREE(queue);
672 static void init_do_list_queue(void)
674 TALLOC_FREE(queue);
675 queue = talloc_zero(NULL, struct do_list_queue);
678 static void add_to_do_list_queue(const char *entry)
680 struct do_list_queue_entry *e = NULL;
681 size_t entry_str_len = strlen(entry)+1;
682 size_t entry_len = offsetof(struct do_list_queue_entry, name);
684 entry_len += entry_str_len;
685 SMB_ASSERT(entry_len >= entry_str_len);
687 e = talloc_size(queue, entry_len);
688 if (e == NULL) {
689 d_printf("talloc failed for entry %s\n", entry);
690 return;
692 talloc_set_name_const(e, "struct do_list_queue_entry");
694 memcpy(e->name, entry, entry_str_len);
695 DLIST_ADD_END(queue->list, e);
698 static char *do_list_queue_head(void)
700 return queue->list->name;
703 static void remove_do_list_queue_head(void)
705 struct do_list_queue_entry *e = queue->list;
706 DLIST_REMOVE(queue->list, e);
707 TALLOC_FREE(e);
710 static int do_list_queue_empty(void)
712 return (queue == NULL) || (queue->list == NULL);
715 /****************************************************************************
716 A helper for do_list.
717 ****************************************************************************/
719 struct do_list_helper_state {
720 const char *mask;
721 struct cli_state *cli;
724 static NTSTATUS do_list_helper(
725 struct file_info *f,
726 const char *_mask,
727 void *private_data)
729 struct do_list_helper_state *state = private_data;
730 TALLOC_CTX *ctx = talloc_tos();
731 char *dir = NULL;
732 char *dir_end = NULL;
733 NTSTATUS status = NT_STATUS_OK;
734 char *mask2 = NULL;
736 /* Work out the directory. */
737 dir = talloc_strdup(ctx, state->mask);
738 if (!dir) {
739 return NT_STATUS_NO_MEMORY;
741 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
742 *dir_end = '\0';
745 if (!(f->attr & FILE_ATTRIBUTE_DIRECTORY)) {
746 if (do_this_one(f)) {
747 status = do_list_fn(state->cli, f, dir);
749 TALLOC_FREE(dir);
750 return status;
753 if (do_list_dirs && do_this_one(f)) {
754 status = do_list_fn(state->cli, f, dir);
755 if (!NT_STATUS_IS_OK(status)) {
756 return status;
760 if (!do_list_recurse ||
761 (f->name == NULL) ||
762 ISDOT(f->name) ||
763 ISDOTDOT(f->name)) {
764 return NT_STATUS_OK;
767 if (!f->name[0]) {
768 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
769 TALLOC_FREE(dir);
770 return NT_STATUS_UNSUCCESSFUL;
773 mask2 = talloc_asprintf(ctx,
774 "%s%c%s%c*",
775 dir,
776 CLI_DIRSEP_CHAR,
777 f->name,
778 CLI_DIRSEP_CHAR);
779 if (!mask2) {
780 TALLOC_FREE(dir);
781 return NT_STATUS_NO_MEMORY;
783 add_to_do_list_queue(mask2);
784 TALLOC_FREE(mask2);
786 TALLOC_FREE(dir);
787 return NT_STATUS_OK;
790 /****************************************************************************
791 A wrapper around cli_list that adds recursion.
792 ****************************************************************************/
794 NTSTATUS do_list(const char *mask,
795 uint32_t attribute,
796 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
797 const char *dir),
798 bool rec,
799 bool dirs)
801 struct do_list_helper_state state = { .cli = cli, };
802 static int in_do_list = 0;
803 TALLOC_CTX *ctx = talloc_tos();
804 struct cli_credentials *creds = samba_cmdline_get_creds();
805 NTSTATUS ret_status = NT_STATUS_OK;
806 NTSTATUS status = NT_STATUS_OK;
808 if (in_do_list && rec) {
809 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
810 exit(1);
813 in_do_list = 1;
815 do_list_recurse = rec;
816 do_list_dirs = dirs;
817 do_list_fn = fn;
819 init_do_list_queue();
820 add_to_do_list_queue(mask);
822 while (!do_list_queue_empty()) {
823 struct cli_state *targetcli = NULL;
824 char *targetpath = NULL;
826 state.mask = do_list_queue_head();
828 /* check for dfs */
830 status = cli_resolve_path(
831 ctx,
833 creds,
834 cli,
835 state.mask,
836 &targetcli,
837 &targetpath);
838 if (!NT_STATUS_IS_OK(status)) {
839 d_printf("do_list: [%s] %s\n", state.mask,
840 nt_errstr(status));
841 remove_do_list_queue_head();
842 continue;
845 status = cli_list(
846 targetcli,
847 targetpath,
848 attribute,
849 do_list_helper,
850 &state);
851 if (!NT_STATUS_IS_OK(status)) {
852 d_printf("%s listing %s\n",
853 nt_errstr(status), targetpath);
854 ret_status = status;
856 remove_do_list_queue_head();
857 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
858 char *next_file = do_list_queue_head();
859 char *save_ch = 0;
860 if ((strlen(next_file) >= 2) &&
861 (next_file[strlen(next_file) - 1] == '*') &&
862 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
863 save_ch = next_file +
864 strlen(next_file) - 2;
865 *save_ch = '\0';
866 if (showacls) {
867 /* cwd is only used if showacls is on */
868 client_set_cwd(next_file);
871 if (!showacls) /* don't disturbe the showacls output */
872 d_printf("\n%s\n",next_file);
873 if (save_ch) {
874 *save_ch = CLI_DIRSEP_CHAR;
877 TALLOC_FREE(targetpath);
880 in_do_list = 0;
881 reset_do_list_queue();
882 return ret_status;
885 /****************************************************************************
886 Get a directory listing.
887 ****************************************************************************/
889 static int cmd_dir(void)
891 TALLOC_CTX *ctx = talloc_tos();
892 uint32_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
893 char *mask = NULL;
894 char *buf = NULL;
895 int rc = 1;
896 NTSTATUS status;
898 dir_total = 0;
899 mask = talloc_strdup(ctx, client_get_cur_dir());
900 if (!mask) {
901 return 1;
904 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
905 normalize_name(buf);
906 if (*buf == CLI_DIRSEP_CHAR) {
907 mask = talloc_strdup(ctx, buf);
908 } else {
909 mask = talloc_asprintf_append(mask, "%s", buf);
911 } else {
912 mask = talloc_asprintf_append(mask, "*");
914 if (!mask) {
915 return 1;
918 mask = client_clean_name(ctx, mask);
919 if (mask == NULL) {
920 return 1;
923 if (showacls) {
924 /* cwd is only used if showacls is on */
925 client_set_cwd(client_get_cur_dir());
928 status = do_list(mask, attribute, display_finfo, recurse, true);
929 if (!NT_STATUS_IS_OK(status)) {
930 return 1;
933 rc = do_dskattr();
935 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
937 return rc;
940 /****************************************************************************
941 Get a directory listing.
942 ****************************************************************************/
944 static int cmd_du(void)
946 TALLOC_CTX *ctx = talloc_tos();
947 uint32_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
948 char *mask = NULL;
949 char *buf = NULL;
950 NTSTATUS status;
951 int rc = 1;
953 dir_total = 0;
954 mask = talloc_strdup(ctx, client_get_cur_dir());
955 if (!mask) {
956 return 1;
958 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
959 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
960 if (!mask) {
961 return 1;
965 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
966 normalize_name(buf);
967 if (*buf == CLI_DIRSEP_CHAR) {
968 mask = talloc_strdup(ctx, buf);
969 } else {
970 mask = talloc_asprintf_append(mask, "%s", buf);
972 } else {
973 mask = talloc_strdup(ctx, "*");
975 if (!mask) {
976 return 1;
979 mask = client_clean_name(ctx, mask);
980 if (mask == NULL) {
981 return 1;
984 status = do_list(mask, attribute, do_du, recurse, true);
985 if (!NT_STATUS_IS_OK(status)) {
986 return 1;
989 rc = do_dskattr();
991 d_printf("Total number of bytes: %.0f\n", dir_total);
993 return rc;
996 static int cmd_echo(void)
998 TALLOC_CTX *ctx = talloc_tos();
999 char *num;
1000 char *data;
1001 NTSTATUS status;
1003 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1004 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1005 d_printf("echo <num> <data>\n");
1006 return 1;
1009 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1011 if (!NT_STATUS_IS_OK(status)) {
1012 d_printf("echo failed: %s\n", nt_errstr(status));
1013 return 1;
1016 return 0;
1019 /****************************************************************************
1020 Get a file from rname to lname
1021 ****************************************************************************/
1023 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1025 int *pfd = (int *)priv;
1026 ssize_t rc;
1028 rc = writefile(*pfd, buf, n);
1029 if (rc == -1) {
1030 return map_nt_error_from_unix(errno);
1032 return NT_STATUS_OK;
1035 static int do_get(const char *rname, const char *lname_in, bool reget)
1037 TALLOC_CTX *ctx = talloc_tos();
1038 int handle = 0;
1039 uint16_t fnum;
1040 bool newhandle = false;
1041 struct timespec tp_start;
1042 uint32_t attr;
1043 off_t size;
1044 off_t start = 0;
1045 off_t nread = 0;
1046 int rc = 0;
1047 struct cli_state *targetcli = NULL;
1048 char *targetname = NULL;
1049 char *lname = NULL;
1050 struct cli_credentials *creds = samba_cmdline_get_creds();
1051 NTSTATUS status;
1053 lname = talloc_strdup(ctx, lname_in);
1054 if (!lname) {
1055 return 1;
1058 if (lowercase) {
1059 if (!strlower_m(lname)) {
1060 d_printf("strlower_m %s failed\n", lname);
1061 return 1;
1065 status = cli_resolve_path(ctx, "",
1066 creds,
1067 cli, rname, &targetcli, &targetname);
1068 if (!NT_STATUS_IS_OK(status)) {
1069 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1070 return 1;
1073 clock_gettime_mono(&tp_start);
1075 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 d_printf("%s opening remote file %s\n", nt_errstr(status),
1078 rname);
1079 return 1;
1082 if(!strcmp(lname,"-")) {
1083 handle = fileno(stdout);
1084 } else {
1085 if (reget) {
1086 handle = open(lname, O_WRONLY|O_CREAT, 0644);
1087 if (handle >= 0) {
1088 start = lseek(handle, 0, SEEK_END);
1089 if (start == -1) {
1090 d_printf("Error seeking local file\n");
1091 close(handle);
1092 return 1;
1095 } else {
1096 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1098 newhandle = true;
1100 if (handle < 0) {
1101 d_printf("Error opening local file %s\n",lname);
1102 return 1;
1106 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1107 NULL, NULL, NULL);
1108 if (!NT_STATUS_IS_OK(status)) {
1109 d_printf("getattrib: %s\n", nt_errstr(status));
1110 if (newhandle) {
1111 close(handle);
1113 return 1;
1116 DEBUG(1,("getting file %s of size %.0f as %s ",
1117 rname, (double)size, lname));
1119 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1120 writefile_sink, (void *)&handle, &nread);
1121 if (!NT_STATUS_IS_OK(status)) {
1122 d_fprintf(stderr, "parallel_read returned %s\n",
1123 nt_errstr(status));
1124 if (newhandle) {
1125 close(handle);
1127 cli_close(targetcli, fnum);
1128 return 1;
1131 status = cli_close(targetcli, fnum);
1132 if (!NT_STATUS_IS_OK(status)) {
1133 d_printf("Error %s closing remote file\n", nt_errstr(status));
1134 rc = 1;
1137 if (newhandle) {
1138 close(handle);
1141 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1142 cli_setatr(cli, rname, attr & ~(uint32_t)FILE_ATTRIBUTE_ARCHIVE, 0);
1146 struct timespec tp_end;
1147 int this_time;
1149 clock_gettime_mono(&tp_end);
1150 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1151 get_total_time_ms += this_time;
1152 get_total_size += nread;
1154 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1155 nread / (1.024*this_time + 1.0e-4),
1156 get_total_size / (1.024*get_total_time_ms)));
1159 TALLOC_FREE(targetname);
1160 return rc;
1163 /****************************************************************************
1164 Get a file.
1165 ****************************************************************************/
1167 static int cmd_get(void)
1169 TALLOC_CTX *ctx = talloc_tos();
1170 char *lname = NULL;
1171 char *rname = NULL;
1172 char *fname = NULL;
1174 rname = talloc_strdup(ctx, client_get_cur_dir());
1175 if (!rname) {
1176 return 1;
1179 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1180 d_printf("get <filename> [localname]\n");
1181 return 1;
1183 rname = talloc_asprintf_append(rname, "%s", fname);
1184 if (!rname) {
1185 return 1;
1187 rname = client_clean_name(ctx, rname);
1188 if (!rname) {
1189 return 1;
1192 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1193 if (!lname) {
1194 lname = fname;
1197 return do_get(rname, lname, false);
1200 /****************************************************************************
1201 Do an mget operation on one file.
1202 ****************************************************************************/
1204 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1205 const char *dir)
1207 TALLOC_CTX *ctx = talloc_tos();
1208 const char *client_cwd = NULL;
1209 size_t client_cwd_len;
1210 char *path = NULL;
1211 char *local_path = NULL;
1213 if (!finfo->name) {
1214 return NT_STATUS_OK;
1217 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
1218 return NT_STATUS_OK;
1221 if ((finfo->attr & FILE_ATTRIBUTE_DIRECTORY) && !recurse) {
1222 return NT_STATUS_OK;
1225 if (prompt) {
1226 const char *object = (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) ?
1227 "directory" : "file";
1228 char *quest = NULL;
1229 bool ok;
1231 quest = talloc_asprintf(
1232 ctx, "Get %s %s? ", object, finfo->name);
1233 if (quest == NULL) {
1234 return NT_STATUS_NO_MEMORY;
1237 ok = yesno(quest);
1238 TALLOC_FREE(quest);
1239 if (!ok) {
1240 return NT_STATUS_OK;
1244 path = talloc_asprintf(
1245 ctx, "%s%c%s", dir, CLI_DIRSEP_CHAR, finfo->name);
1246 if (path == NULL) {
1247 return NT_STATUS_NO_MEMORY;
1249 path = client_clean_name(ctx, path);
1250 if (path == NULL) {
1251 return NT_STATUS_NO_MEMORY;
1255 * Skip the path prefix if we've done a remote "cd" when
1256 * creating the local path
1258 client_cwd = client_get_cur_dir();
1259 client_cwd_len = strlen(client_cwd);
1261 local_path = talloc_strdup(ctx, path + client_cwd_len);
1262 if (local_path == NULL) {
1263 TALLOC_FREE(path);
1264 return NT_STATUS_NO_MEMORY;
1266 string_replace(local_path, CLI_DIRSEP_CHAR, '/');
1268 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
1269 int ret = mkdir(local_path, 0777);
1271 if ((ret == -1) && (errno != EEXIST)) {
1272 return map_nt_error_from_unix(errno);
1274 } else {
1275 do_get(path, local_path, false);
1278 return NT_STATUS_OK;
1281 /****************************************************************************
1282 View the file using the pager.
1283 ****************************************************************************/
1285 static int cmd_more(void)
1287 TALLOC_CTX *ctx = talloc_tos();
1288 char *rname = NULL;
1289 char *fname = NULL;
1290 char *lname = NULL;
1291 char *pager_cmd = NULL;
1292 const char *pager;
1293 int fd;
1294 int rc = 0;
1295 mode_t mask;
1297 rname = talloc_strdup(ctx, client_get_cur_dir());
1298 if (!rname) {
1299 return 1;
1302 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1303 if (!lname) {
1304 return 1;
1306 mask = umask(S_IRWXO | S_IRWXG);
1307 fd = mkstemp(lname);
1308 umask(mask);
1309 if (fd == -1) {
1310 d_printf("failed to create temporary file for more\n");
1311 return 1;
1313 close(fd);
1315 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1316 d_printf("more <filename>\n");
1317 unlink(lname);
1318 return 1;
1320 rname = talloc_asprintf_append(rname, "%s", fname);
1321 if (!rname) {
1322 return 1;
1324 rname = client_clean_name(ctx,rname);
1325 if (!rname) {
1326 return 1;
1329 rc = do_get(rname, lname, false);
1331 pager=getenv("PAGER");
1333 pager_cmd = talloc_asprintf(ctx,
1334 "%s %s",
1335 (pager? pager:PAGER),
1336 lname);
1337 if (!pager_cmd) {
1338 return 1;
1340 if (system(pager_cmd) == -1) {
1341 d_printf("system command '%s' returned -1\n",
1342 pager_cmd);
1344 unlink(lname);
1346 return rc;
1349 /****************************************************************************
1350 Do a mget command.
1351 ****************************************************************************/
1353 static int cmd_mget(void)
1355 TALLOC_CTX *ctx = talloc_tos();
1356 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1357 char *mget_mask = NULL;
1358 char *buf = NULL;
1359 NTSTATUS status = NT_STATUS_OK;
1361 if (recurse) {
1362 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1365 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1367 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1368 if (!mget_mask) {
1369 return 1;
1371 if (*buf == CLI_DIRSEP_CHAR) {
1372 mget_mask = talloc_strdup(ctx, buf);
1373 } else {
1374 mget_mask = talloc_asprintf_append(mget_mask,
1375 "%s", buf);
1377 if (!mget_mask) {
1378 return 1;
1380 mget_mask = client_clean_name(ctx, mget_mask);
1381 if (mget_mask == NULL) {
1382 return 1;
1384 status = do_list(mget_mask, attribute, do_mget, recurse, true);
1385 if (!NT_STATUS_IS_OK(status)) {
1386 return 1;
1390 if (mget_mask == NULL) {
1391 d_printf("nothing to mget\n");
1392 return 0;
1395 if (!*mget_mask) {
1396 mget_mask = talloc_asprintf(ctx,
1397 "%s*",
1398 client_get_cur_dir());
1399 if (!mget_mask) {
1400 return 1;
1402 mget_mask = client_clean_name(ctx, mget_mask);
1403 if (mget_mask == NULL) {
1404 return 1;
1406 status = do_list(mget_mask, attribute, do_mget, recurse, true);
1407 if (!NT_STATUS_IS_OK(status)) {
1408 return 1;
1412 return 0;
1415 /****************************************************************************
1416 Make a directory of name "name".
1417 ****************************************************************************/
1419 static bool do_mkdir(const char *name)
1421 TALLOC_CTX *ctx = talloc_tos();
1422 struct cli_state *targetcli;
1423 char *targetname = NULL;
1424 struct cli_credentials *creds = samba_cmdline_get_creds();
1425 NTSTATUS status;
1427 status = cli_resolve_path(ctx, "",
1428 creds,
1429 cli, name, &targetcli, &targetname);
1430 if (!NT_STATUS_IS_OK(status)) {
1431 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1432 return false;
1435 status = cli_mkdir(targetcli, targetname);
1436 if (!NT_STATUS_IS_OK(status)) {
1437 d_printf("%s making remote directory %s\n",
1438 nt_errstr(status),name);
1439 return false;
1442 return true;
1445 /****************************************************************************
1446 Show 8.3 name of a file.
1447 ****************************************************************************/
1449 static bool do_altname(const char *name)
1451 fstring altname;
1452 NTSTATUS status;
1454 status = cli_qpathinfo_alt_name(cli, name, altname);
1455 if (!NT_STATUS_IS_OK(status)) {
1456 d_printf("%s getting alt name for %s\n",
1457 nt_errstr(status),name);
1458 return false;
1460 d_printf("%s\n", altname);
1462 return true;
1465 /****************************************************************************
1466 Exit client.
1467 ****************************************************************************/
1469 static int cmd_quit(void)
1471 cli_shutdown(cli);
1472 exit(0);
1473 /* NOTREACHED */
1474 return 0;
1477 /****************************************************************************
1478 Make a directory.
1479 ****************************************************************************/
1481 static int cmd_mkdir(void)
1483 TALLOC_CTX *ctx = talloc_tos();
1484 char *mask = NULL;
1485 char *buf = NULL;
1486 struct cli_credentials *creds = samba_cmdline_get_creds();
1487 NTSTATUS status;
1489 mask = talloc_strdup(ctx, client_get_cur_dir());
1490 if (!mask) {
1491 return 1;
1494 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1495 if (!recurse) {
1496 d_printf("mkdir <dirname>\n");
1498 return 1;
1500 mask = talloc_asprintf_append(mask, "%s", buf);
1501 if (!mask) {
1502 return 1;
1504 mask = client_clean_name(ctx, mask);
1505 if (mask == NULL) {
1506 return 1;
1509 if (recurse) {
1510 char *ddir = NULL;
1511 char *ddir2 = NULL;
1512 struct cli_state *targetcli;
1513 char *targetname = NULL;
1514 char *p = NULL;
1515 char *saveptr;
1517 ddir2 = talloc_strdup(ctx, "");
1518 if (!ddir2) {
1519 return 1;
1522 status = cli_resolve_path(ctx, "",
1523 creds,
1524 cli, mask,
1525 &targetcli, &targetname);
1526 if (!NT_STATUS_IS_OK(status)) {
1527 return 1;
1530 ddir = talloc_strdup(ctx, targetname);
1531 if (!ddir) {
1532 return 1;
1534 trim_char(ddir,'.','\0');
1535 p = strtok_r(ddir, "/\\", &saveptr);
1536 while (p) {
1537 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1538 if (!ddir2) {
1539 return 1;
1541 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1542 do_mkdir(ddir2);
1544 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1545 if (!ddir2) {
1546 return 1;
1548 p = strtok_r(NULL, "/\\", &saveptr);
1550 } else {
1551 do_mkdir(mask);
1554 return 0;
1557 /****************************************************************************
1558 Show alt name.
1559 ****************************************************************************/
1561 static int cmd_altname(void)
1563 TALLOC_CTX *ctx = talloc_tos();
1564 char *name;
1565 char *buf;
1567 name = talloc_strdup(ctx, client_get_cur_dir());
1568 if (!name) {
1569 return 1;
1572 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1573 d_printf("altname <file>\n");
1574 return 1;
1576 name = talloc_asprintf_append(name, "%s", buf);
1577 if (!name) {
1578 return 1;
1580 name = client_clean_name(ctx, name);
1581 if (name == NULL) {
1582 return 1;
1584 do_altname(name);
1585 return 0;
1588 static char *attr_str(TALLOC_CTX *mem_ctx, uint32_t attr)
1590 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1591 int i = 0;
1593 if (!(attr & FILE_ATTRIBUTE_NORMAL)) {
1594 if (attr & FILE_ATTRIBUTE_ENCRYPTED) {
1595 attrs[i++] = 'E';
1597 if (attr & FILE_ATTRIBUTE_NONINDEXED) {
1598 attrs[i++] = 'N';
1600 if (attr & FILE_ATTRIBUTE_OFFLINE) {
1601 attrs[i++] = 'O';
1603 if (attr & FILE_ATTRIBUTE_COMPRESSED) {
1604 attrs[i++] = 'C';
1606 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1607 attrs[i++] = 'r';
1609 if (attr & FILE_ATTRIBUTE_SPARSE) {
1610 attrs[i++] = 's';
1612 if (attr & FILE_ATTRIBUTE_TEMPORARY) {
1613 attrs[i++] = 'T';
1615 if (attr & FILE_ATTRIBUTE_NORMAL) {
1616 attrs[i++] = 'N';
1618 if (attr & FILE_ATTRIBUTE_READONLY) {
1619 attrs[i++] = 'R';
1621 if (attr & FILE_ATTRIBUTE_HIDDEN) {
1622 attrs[i++] = 'H';
1624 if (attr & FILE_ATTRIBUTE_SYSTEM) {
1625 attrs[i++] = 'S';
1627 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
1628 attrs[i++] = 'D';
1630 if (attr & FILE_ATTRIBUTE_ARCHIVE) {
1631 attrs[i++] = 'A';
1634 return attrs;
1637 /****************************************************************************
1638 Show all info we can get
1639 ****************************************************************************/
1641 static int do_allinfo(const char *name)
1643 fstring altname;
1644 struct timespec b_time, a_time, m_time, c_time;
1645 off_t size;
1646 uint32_t attr;
1647 NTTIME tmp;
1648 uint16_t fnum;
1649 unsigned int num_streams;
1650 struct stream_struct *streams;
1651 int j, num_snapshots;
1652 char **snapshots = NULL;
1653 unsigned int i;
1654 NTSTATUS status;
1656 status = cli_qpathinfo_alt_name(cli, name, altname);
1657 if (!NT_STATUS_IS_OK(status)) {
1658 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1659 name);
1661 * Ignore not supported or not implemented, it does not
1662 * hurt if we can't list alternate names.
1664 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1665 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1666 altname[0] = '\0';
1667 } else {
1668 return false;
1671 d_printf("altname: %s\n", altname);
1673 status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1674 &size, &attr, NULL);
1675 if (!NT_STATUS_IS_OK(status)) {
1676 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1677 name);
1678 return false;
1681 tmp = full_timespec_to_nt_time(&b_time);
1682 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1684 tmp = full_timespec_to_nt_time(&a_time);
1685 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1687 tmp = full_timespec_to_nt_time(&m_time);
1688 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1690 tmp = full_timespec_to_nt_time(&c_time);
1691 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1693 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), attr), attr);
1695 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1696 &streams);
1697 if (!NT_STATUS_IS_OK(status)) {
1698 d_printf("%s getting streams for %s\n", nt_errstr(status),
1699 name);
1700 return false;
1703 for (i=0; i<num_streams; i++) {
1704 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1705 (unsigned long long)streams[i].size);
1708 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1709 char *subst, *print;
1710 uint32_t flags;
1712 status = cli_readlink(cli, name, talloc_tos(), &subst, &print,
1713 &flags);
1714 if (!NT_STATUS_IS_OK(status)) {
1715 d_fprintf(stderr, "cli_readlink returned %s\n",
1716 nt_errstr(status));
1717 } else {
1718 d_printf("symlink: subst=[%s], print=[%s], flags=%x\n",
1719 subst, print, flags);
1720 TALLOC_FREE(subst);
1721 TALLOC_FREE(print);
1725 status = cli_ntcreate(cli, name, 0,
1726 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1727 SEC_STD_SYNCHRONIZE, 0,
1728 FILE_SHARE_READ|FILE_SHARE_WRITE
1729 |FILE_SHARE_DELETE,
1730 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
1731 if (!NT_STATUS_IS_OK(status)) {
1733 * Ignore failure, it does not hurt if we can't list
1734 * snapshots
1736 return 0;
1739 * In order to get shadow copy data over SMB1 we
1740 * must call twice, once with 'get_names = false'
1741 * to get the size, then again with 'get_names = true'
1742 * to get the data or a Windows server fails to return
1743 * valid info. Samba doesn't have this bug. JRA.
1746 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1747 false, &snapshots, &num_snapshots);
1748 if (!NT_STATUS_IS_OK(status)) {
1749 cli_close(cli, fnum);
1750 return 0;
1752 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1753 true, &snapshots, &num_snapshots);
1754 if (!NT_STATUS_IS_OK(status)) {
1755 cli_close(cli, fnum);
1756 return 0;
1759 for (j=0; j<num_snapshots; j++) {
1760 char *snap_name;
1762 d_printf("%s\n", snapshots[j]);
1763 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1764 snapshots[j], name);
1765 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1766 &m_time, &c_time, &size,
1767 NULL, NULL);
1768 if (!NT_STATUS_IS_OK(status)) {
1769 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1770 snap_name, nt_errstr(status));
1771 TALLOC_FREE(snap_name);
1772 continue;
1774 tmp = unix_timespec_to_nt_time(b_time);
1775 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1776 tmp = unix_timespec_to_nt_time(a_time);
1777 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1778 tmp =unix_timespec_to_nt_time(m_time);
1779 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1780 tmp = unix_timespec_to_nt_time(c_time);
1781 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1782 d_printf("size: %d\n", (int)size);
1785 TALLOC_FREE(snapshots);
1786 cli_close(cli, fnum);
1788 return 0;
1791 /****************************************************************************
1792 Show all info we can get
1793 ****************************************************************************/
1795 static int cmd_allinfo(void)
1797 TALLOC_CTX *ctx = talloc_tos();
1798 char *name;
1799 char *buf;
1801 name = talloc_strdup(ctx, client_get_cur_dir());
1802 if (!name) {
1803 return 1;
1806 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1807 d_printf("allinfo <file>\n");
1808 return 1;
1810 name = talloc_asprintf_append(name, "%s", buf);
1811 if (!name) {
1812 return 1;
1814 name = client_clean_name(ctx, name);
1815 if (name == NULL) {
1816 return 1;
1818 do_allinfo(name);
1820 return 0;
1823 /****************************************************************************
1824 Put a single file.
1825 ****************************************************************************/
1827 static int do_put(const char *rname, const char *lname, bool reput)
1829 TALLOC_CTX *ctx = talloc_tos();
1830 uint16_t fnum;
1831 FILE *f;
1832 off_t start = 0;
1833 int rc = 0;
1834 struct timespec tp_start;
1835 struct cli_state *targetcli;
1836 char *targetname = NULL;
1837 struct push_state state;
1838 struct cli_credentials *creds = samba_cmdline_get_creds();
1839 NTSTATUS status;
1841 status = cli_resolve_path(ctx, "",
1842 creds,
1843 cli, rname, &targetcli, &targetname);
1844 if (!NT_STATUS_IS_OK(status)) {
1845 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1846 return 1;
1849 clock_gettime_mono(&tp_start);
1851 if (reput) {
1852 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1853 if (NT_STATUS_IS_OK(status)) {
1854 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1855 targetcli, fnum, NULL,
1856 &start, NULL, NULL,
1857 NULL, NULL, NULL))) {
1858 d_printf("getattrib: %s\n", nt_errstr(status));
1859 return 1;
1862 } else {
1863 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1866 if (!NT_STATUS_IS_OK(status)) {
1867 d_printf("%s opening remote file %s\n", nt_errstr(status),
1868 rname);
1869 return 1;
1872 /* allow files to be piped into smbclient
1873 jdblair 24.jun.98
1875 Note that in this case this function will exit(0) rather
1876 than returning. */
1877 if (!strcmp(lname, "-")) {
1878 f = stdin;
1879 /* size of file is not known */
1880 } else {
1881 f = fopen(lname, "r");
1882 if (f && reput) {
1883 if (fseek(f, start, SEEK_SET) == -1) {
1884 d_printf("Error seeking local file\n");
1885 fclose(f);
1886 return 1;
1891 if (!f) {
1892 d_printf("Error opening local file %s\n",lname);
1893 return 1;
1896 DEBUG(1,("putting file %s as %s ",lname,
1897 rname));
1899 setvbuf(f, NULL, _IOFBF, io_bufsize);
1901 state.f = f;
1902 state.nread = 0;
1904 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1905 &state);
1906 if (!NT_STATUS_IS_OK(status)) {
1907 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1908 rc = 1;
1911 status = cli_close(targetcli, fnum);
1912 if (!NT_STATUS_IS_OK(status)) {
1913 d_printf("%s closing remote file %s\n", nt_errstr(status),
1914 rname);
1915 if (f != stdin) {
1916 fclose(f);
1918 return 1;
1921 if (f != stdin) {
1922 fclose(f);
1926 struct timespec tp_end;
1927 int this_time;
1929 clock_gettime_mono(&tp_end);
1930 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1931 put_total_time_ms += this_time;
1932 put_total_size += state.nread;
1934 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1935 state.nread / (1.024*this_time + 1.0e-4),
1936 put_total_size / (1.024*put_total_time_ms)));
1939 if (f == stdin) {
1940 cli_shutdown(cli);
1941 exit(rc);
1944 return rc;
1947 /****************************************************************************
1948 Put a file.
1949 ****************************************************************************/
1951 static int cmd_put(void)
1953 TALLOC_CTX *ctx = talloc_tos();
1954 char *lname;
1955 char *rname;
1956 char *buf;
1958 rname = talloc_strdup(ctx, client_get_cur_dir());
1959 if (!rname) {
1960 return 1;
1963 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1964 d_printf("put <filename>\n");
1965 return 1;
1968 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1969 rname = talloc_asprintf_append(rname, "%s", buf);
1970 } else {
1971 rname = talloc_asprintf_append(rname, "%s", lname);
1973 if (!rname) {
1974 return 1;
1977 rname = client_clean_name(ctx, rname);
1978 if (!rname) {
1979 return 1;
1983 SMB_STRUCT_STAT st;
1984 /* allow '-' to represent stdin
1985 jdblair, 24.jun.98 */
1986 if (!file_exist_stat(lname, &st, false) &&
1987 (strcmp(lname,"-"))) {
1988 d_printf("%s does not exist\n",lname);
1989 return 1;
1993 return do_put(rname, lname, false);
1996 /*************************************
1997 File list structure.
1998 *************************************/
2000 static struct file_list {
2001 struct file_list *prev, *next;
2002 char *file_path;
2003 bool isdir;
2004 } *file_list;
2006 /****************************************************************************
2007 Free a file_list structure.
2008 ****************************************************************************/
2010 static void free_file_list (struct file_list *l_head)
2012 struct file_list *list, *next;
2014 for (list = l_head; list; list = next) {
2015 next = list->next;
2016 DLIST_REMOVE(l_head, list);
2017 TALLOC_FREE(list);
2021 /****************************************************************************
2022 Seek in a directory/file list until you get something that doesn't start with
2023 the specified name.
2024 ****************************************************************************/
2026 static bool seek_list(struct file_list *list, char *name)
2028 while (list) {
2029 trim_string(list->file_path,"./","\n");
2030 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2031 return true;
2033 list = list->next;
2036 return false;
2039 /****************************************************************************
2040 Set the file selection mask.
2041 ****************************************************************************/
2043 static int cmd_select(void)
2045 TALLOC_CTX *ctx = talloc_tos();
2046 char *new_fs = NULL;
2047 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2049 if (new_fs) {
2050 client_set_fileselection(new_fs);
2051 } else {
2052 client_set_fileselection("");
2054 return 0;
2057 /****************************************************************************
2058 Recursive file matching function act as find
2059 match must be always set to true when calling this function
2060 ****************************************************************************/
2062 static int file_find(TALLOC_CTX *ctx,
2063 struct file_list **list,
2064 const char *directory,
2065 const char *expression,
2066 bool match)
2068 DIR *dir;
2069 struct file_list *entry;
2070 struct stat statbuf;
2071 int ret;
2072 char *path;
2073 bool isdir;
2074 const char *dname;
2076 dir = opendir(directory);
2077 if (!dir)
2078 return -1;
2080 while ((dname = readdirname(dir))) {
2081 if (ISDOT(dname) || ISDOTDOT(dname)) {
2082 continue;
2085 path = talloc_asprintf(ctx, "%s/%s", directory, dname);
2086 if (path == NULL) {
2087 continue;
2090 isdir = false;
2091 if (!match || !gen_fnmatch(expression, dname)) {
2092 if (recurse) {
2093 ret = stat(path, &statbuf);
2094 if (ret == 0) {
2095 if (S_ISDIR(statbuf.st_mode)) {
2096 isdir = true;
2097 ret = file_find(ctx,
2098 list,
2099 path,
2100 expression,
2101 false);
2103 } else {
2104 d_printf("file_find: cannot stat file %s\n", path);
2107 if (ret == -1) {
2108 TALLOC_FREE(path);
2109 closedir(dir);
2110 return -1;
2113 entry = talloc_zero(ctx, struct file_list);
2114 if (!entry) {
2115 d_printf("Out of memory in file_find\n");
2116 closedir(dir);
2117 return -1;
2119 entry->file_path = talloc_move(entry, &path);
2120 entry->isdir = isdir;
2121 DLIST_ADD(*list, entry);
2122 } else {
2123 TALLOC_FREE(path);
2127 closedir(dir);
2128 return 0;
2131 /****************************************************************************
2132 mput some files.
2133 ****************************************************************************/
2135 static int cmd_mput(void)
2137 TALLOC_CTX *ctx = talloc_tos();
2138 char *p = NULL;
2140 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2141 int ret;
2142 struct file_list *temp_list;
2143 char *quest, *lname, *rname;
2145 file_list = NULL;
2147 ret = file_find(ctx, &file_list, ".", p, true);
2148 if (ret) {
2149 free_file_list(file_list);
2150 continue;
2153 quest = NULL;
2154 lname = NULL;
2155 rname = NULL;
2157 for (temp_list = file_list; temp_list;
2158 temp_list = temp_list->next) {
2160 SAFE_FREE(lname);
2161 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2162 continue;
2164 trim_string(lname, "./", "/");
2166 /* check if it's a directory */
2167 if (temp_list->isdir) {
2168 /* if (!recurse) continue; */
2170 SAFE_FREE(quest);
2171 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2172 break;
2174 if (prompt && !yesno(quest)) { /* No */
2175 /* Skip the directory */
2176 lname[strlen(lname)-1] = '/';
2177 if (!seek_list(temp_list, lname))
2178 break;
2179 } else { /* Yes */
2180 SAFE_FREE(rname);
2181 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2182 break;
2184 normalize_name(rname);
2186 char *tmp_rname =
2187 client_clean_name(ctx, rname);
2188 if (tmp_rname == NULL) {
2189 break;
2191 SAFE_FREE(rname);
2192 rname = smb_xstrdup(tmp_rname);
2193 TALLOC_FREE(tmp_rname);
2194 if (rname == NULL) {
2195 break;
2198 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2199 !do_mkdir(rname)) {
2200 DEBUG (0, ("Unable to make dir, skipping..."));
2201 /* Skip the directory */
2202 lname[strlen(lname)-1] = '/';
2203 if (!seek_list(temp_list, lname)) {
2204 break;
2208 continue;
2209 } else {
2210 SAFE_FREE(quest);
2211 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2212 break;
2214 if (prompt && !yesno(quest)) {
2215 /* No */
2216 continue;
2219 /* Yes */
2220 SAFE_FREE(rname);
2221 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2222 break;
2226 normalize_name(rname);
2229 char *tmp_rname = client_clean_name(ctx, rname);
2230 if (tmp_rname == NULL) {
2231 break;
2233 SAFE_FREE(rname);
2234 rname = smb_xstrdup(tmp_rname);
2235 TALLOC_FREE(tmp_rname);
2236 if (rname == NULL) {
2237 break;
2240 do_put(rname, lname, false);
2242 free_file_list(file_list);
2243 SAFE_FREE(quest);
2244 SAFE_FREE(lname);
2245 SAFE_FREE(rname);
2248 return 0;
2251 /****************************************************************************
2252 Cancel a print job.
2253 ****************************************************************************/
2255 static int do_cancel(int job)
2257 if (cli_printjob_del(cli, job)) {
2258 d_printf("Job %d cancelled\n",job);
2259 return 0;
2260 } else {
2261 NTSTATUS status = cli_nt_error(cli);
2262 d_printf("Error cancelling job %d : %s\n",
2263 job, nt_errstr(status));
2264 return 1;
2268 /****************************************************************************
2269 Cancel a print job.
2270 ****************************************************************************/
2272 static int cmd_cancel(void)
2274 TALLOC_CTX *ctx = talloc_tos();
2275 char *buf = NULL;
2276 int job;
2278 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2279 d_printf("cancel <jobid> ...\n");
2280 return 1;
2282 do {
2283 job = atoi(buf);
2284 do_cancel(job);
2285 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2287 return 0;
2290 /****************************************************************************
2291 Print a file.
2292 ****************************************************************************/
2294 static int cmd_print(void)
2296 TALLOC_CTX *ctx = talloc_tos();
2297 char *lname = NULL;
2298 char *rname = NULL;
2299 char *p = NULL;
2301 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2302 d_printf("print <filename>\n");
2303 return 1;
2306 rname = talloc_strdup(ctx, lname);
2307 if (!rname) {
2308 return 1;
2310 p = strrchr_m(rname,'/');
2311 if (p) {
2312 rname = talloc_asprintf(ctx,
2313 "%s-%d",
2314 p+1,
2315 (int)getpid());
2317 if (strequal(lname,"-")) {
2318 rname = talloc_asprintf(ctx,
2319 "stdin-%d",
2320 (int)getpid());
2322 if (!rname) {
2323 return 1;
2326 return do_put(rname, lname, false);
2329 /****************************************************************************
2330 Show a print queue entry.
2331 ****************************************************************************/
2333 static void queue_fn(struct print_job_info *p)
2335 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2338 /****************************************************************************
2339 Show a print queue.
2340 ****************************************************************************/
2342 static int cmd_queue(void)
2344 cli_print_queue(cli, queue_fn);
2345 return 0;
2348 /****************************************************************************
2349 Delete some files.
2350 ****************************************************************************/
2352 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2353 const char *dir)
2355 TALLOC_CTX *ctx = talloc_tos();
2356 char *mask = NULL;
2357 struct cli_state *targetcli = NULL;
2358 char *targetname = NULL;
2359 struct cli_credentials *creds = samba_cmdline_get_creds();
2360 NTSTATUS status;
2362 mask = talloc_asprintf(ctx,
2363 "%s%c%s",
2364 dir,
2365 CLI_DIRSEP_CHAR,
2366 finfo->name);
2367 if (!mask) {
2368 return NT_STATUS_NO_MEMORY;
2371 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2372 TALLOC_FREE(mask);
2373 return NT_STATUS_OK;
2376 status = cli_resolve_path(ctx, "",
2377 creds,
2378 cli, mask, &targetcli, &targetname);
2379 if (!NT_STATUS_IS_OK(status)) {
2380 goto out;
2383 status = cli_unlink(targetcli, targetname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2384 out:
2385 if (!NT_STATUS_IS_OK(status)) {
2386 d_printf("%s deleting remote file %s\n",
2387 nt_errstr(status), mask);
2389 TALLOC_FREE(mask);
2390 return status;
2393 /****************************************************************************
2394 Delete some files.
2395 ****************************************************************************/
2397 static int cmd_del(void)
2399 TALLOC_CTX *ctx = talloc_tos();
2400 char *mask = NULL;
2401 char *buf = NULL;
2402 NTSTATUS status = NT_STATUS_OK;
2403 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2405 if (recurse) {
2406 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2409 mask = talloc_strdup(ctx, client_get_cur_dir());
2410 if (!mask) {
2411 return 1;
2413 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2414 d_printf("del <filename>\n");
2415 return 1;
2417 mask = talloc_asprintf_append(mask, "%s", buf);
2418 if (!mask) {
2419 return 1;
2421 mask = client_clean_name(ctx, mask);
2422 if (mask == NULL) {
2423 return 1;
2426 status = do_list(mask,attribute,do_del,false,false);
2427 if (!NT_STATUS_IS_OK(status)) {
2428 return 1;
2430 return 0;
2433 /****************************************************************************
2434 Delete some files.
2435 ****************************************************************************/
2437 static NTSTATUS delete_remote_files_list(struct cli_state *cli_state,
2438 struct file_list *flist)
2440 NTSTATUS status = NT_STATUS_OK;
2441 struct file_list *deltree_list_iter = NULL;
2442 char *targetname = NULL;
2443 struct cli_state *targetcli = NULL;
2444 struct cli_credentials *creds = samba_cmdline_get_creds();
2445 TALLOC_CTX *ctx = talloc_tos();
2447 for (deltree_list_iter = flist;
2448 deltree_list_iter != NULL;
2449 deltree_list_iter = deltree_list_iter->next) {
2450 status = cli_resolve_path(ctx,
2452 creds,
2453 cli_state,
2454 deltree_list_iter->file_path,
2455 &targetcli,
2456 &targetname);
2457 if (!NT_STATUS_IS_OK(status)) {
2458 d_printf("delete_remote_files %s: %s\n",
2459 deltree_list_iter->file_path,
2460 nt_errstr(status));
2461 return status;
2463 if (CLI_DIRSEP_CHAR == '/') {
2464 /* POSIX. */
2465 status = cli_posix_unlink(targetcli,
2466 targetname);
2467 } else if (deltree_list_iter->isdir) {
2468 status = cli_rmdir(targetcli,
2469 targetname);
2470 } else {
2471 status = cli_unlink(targetcli,
2472 targetname,
2473 FILE_ATTRIBUTE_SYSTEM |
2474 FILE_ATTRIBUTE_HIDDEN);
2476 if (!NT_STATUS_IS_OK(status)) {
2477 d_printf("%s deleting remote %s %s\n",
2478 nt_errstr(status),
2479 deltree_list_iter->isdir ?
2480 "directory" : "file",
2481 deltree_list_iter->file_path);
2482 return status;
2485 return NT_STATUS_OK;
2488 /****************************************************************************
2489 Save a list of files to delete.
2490 ****************************************************************************/
2492 static struct file_list *deltree_list_head;
2494 static NTSTATUS do_deltree_list(struct cli_state *cli_state,
2495 struct file_info *finfo,
2496 const char *dir)
2498 struct file_list **file_list_head_pp = &deltree_list_head;
2499 struct file_list *dt = NULL;
2501 if (!do_this_one(finfo)) {
2502 return NT_STATUS_OK;
2505 /* skip if this is . or .. */
2506 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
2507 return NT_STATUS_OK;
2510 dt = talloc_zero(NULL, struct file_list);
2511 if (dt == NULL) {
2512 return NT_STATUS_NO_MEMORY;
2515 /* create absolute filename for cli_ntcreate() */
2516 dt->file_path = talloc_asprintf(dt,
2517 "%s%s%s",
2518 dir,
2519 CLI_DIRSEP_STR,
2520 finfo->name);
2521 if (dt->file_path == NULL) {
2522 TALLOC_FREE(dt);
2523 return NT_STATUS_NO_MEMORY;
2526 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2527 dt->isdir = true;
2530 DLIST_ADD(*file_list_head_pp, dt);
2531 return NT_STATUS_OK;
2534 static int cmd_deltree(void)
2536 TALLOC_CTX *ctx = talloc_tos();
2537 char *buf = NULL;
2538 NTSTATUS status = NT_STATUS_OK;
2539 struct file_list *deltree_list_norecurse = NULL;
2540 struct file_list *deltree_list_iter = NULL;
2541 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM |
2542 FILE_ATTRIBUTE_HIDDEN |
2543 FILE_ATTRIBUTE_DIRECTORY;
2544 bool ok;
2545 char *mask = talloc_strdup(ctx, client_get_cur_dir());
2546 if (mask == NULL) {
2547 return 1;
2549 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
2550 if (!ok) {
2551 d_printf("deltree <filename>\n");
2552 return 1;
2554 mask = talloc_asprintf_append(mask, "%s", buf);
2555 if (mask == NULL) {
2556 return 1;
2558 mask = client_clean_name(ctx, mask);
2559 if (mask == NULL) {
2560 return 1;
2563 deltree_list_head = NULL;
2566 * Get the list of directories to
2567 * delete (in case mask has a wildcard).
2569 status = do_list(mask, attribute, do_deltree_list, false, true);
2570 if (!NT_STATUS_IS_OK(status)) {
2571 goto err;
2573 deltree_list_norecurse = deltree_list_head;
2574 deltree_list_head = NULL;
2576 for (deltree_list_iter = deltree_list_norecurse;
2577 deltree_list_iter != NULL;
2578 deltree_list_iter = deltree_list_iter->next) {
2580 if (deltree_list_iter->isdir == false) {
2581 char *targetname = NULL;
2582 struct cli_state *targetcli = NULL;
2583 struct cli_credentials *creds = samba_cmdline_get_creds();
2584 status = cli_resolve_path(ctx,
2586 creds,
2587 cli,
2588 deltree_list_iter->file_path,
2589 &targetcli,
2590 &targetname);
2591 if (!NT_STATUS_IS_OK(status)) {
2592 goto err;
2594 /* Just a regular file. */
2595 if (CLI_DIRSEP_CHAR == '/') {
2596 /* POSIX. */
2597 status = cli_posix_unlink(targetcli,
2598 targetname);
2599 } else {
2600 status = cli_unlink(targetcli,
2601 targetname,
2602 FILE_ATTRIBUTE_SYSTEM |
2603 FILE_ATTRIBUTE_HIDDEN);
2605 if (!NT_STATUS_IS_OK(status)) {
2606 goto err;
2608 continue;
2612 * Get the list of files or directories to
2613 * delete in depth order.
2615 status = do_list(deltree_list_iter->file_path,
2616 attribute,
2617 do_deltree_list,
2618 true,
2619 true);
2620 if (!NT_STATUS_IS_OK(status)) {
2621 goto err;
2623 status = delete_remote_files_list(cli, deltree_list_head);
2624 free_file_list(deltree_list_head);
2625 deltree_list_head = NULL;
2626 if (!NT_STATUS_IS_OK(status)) {
2627 goto err;
2631 free_file_list(deltree_list_norecurse);
2632 free_file_list(deltree_list_head);
2633 return 0;
2635 err:
2637 free_file_list(deltree_list_norecurse);
2638 free_file_list(deltree_list_head);
2639 deltree_list_head = NULL;
2640 return 1;
2644 /****************************************************************************
2645 Wildcard delete some files.
2646 ****************************************************************************/
2648 static int cmd_wdel(void)
2650 TALLOC_CTX *ctx = talloc_tos();
2651 char *mask = NULL;
2652 char *buf = NULL;
2653 uint32_t attribute;
2654 struct cli_state *targetcli;
2655 char *targetname = NULL;
2656 struct cli_credentials *creds = samba_cmdline_get_creds();
2657 NTSTATUS status;
2659 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2660 d_printf("wdel 0x<attrib> <wcard>\n");
2661 return 1;
2664 attribute = (uint32_t)strtol(buf, (char **)NULL, 16);
2666 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2667 d_printf("wdel 0x<attrib> <wcard>\n");
2668 return 1;
2671 mask = talloc_asprintf(ctx, "%s%s",
2672 client_get_cur_dir(),
2673 buf);
2674 if (!mask) {
2675 return 1;
2677 mask = client_clean_name(ctx, mask);
2678 if (mask == NULL) {
2679 return 1;
2682 status = cli_resolve_path(ctx, "",
2683 creds,
2684 cli, mask, &targetcli, &targetname);
2685 if (!NT_STATUS_IS_OK(status)) {
2686 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2687 return 1;
2690 status = cli_unlink(targetcli, targetname, attribute);
2691 if (!NT_STATUS_IS_OK(status)) {
2692 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2693 targetname);
2695 return 0;
2698 /****************************************************************************
2699 ****************************************************************************/
2701 static int cmd_open(void)
2703 TALLOC_CTX *ctx = talloc_tos();
2704 char *mask = NULL;
2705 char *buf = NULL;
2706 char *targetname = NULL;
2707 struct cli_state *targetcli;
2708 uint16_t fnum = (uint16_t)-1;
2709 struct cli_credentials *creds = samba_cmdline_get_creds();
2710 NTSTATUS status;
2712 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2713 d_printf("open <filename>\n");
2714 return 1;
2716 mask = talloc_asprintf(ctx,
2717 "%s%s",
2718 client_get_cur_dir(),
2719 buf);
2720 if (!mask) {
2721 return 1;
2724 mask = client_clean_name(ctx, mask);
2725 if (mask == NULL) {
2726 return 1;
2729 status = cli_resolve_path(ctx, "",
2730 creds,
2731 cli, mask, &targetcli, &targetname);
2732 if (!NT_STATUS_IS_OK(status)) {
2733 d_printf("open %s: %s\n", mask, nt_errstr(status));
2734 return 1;
2737 status = cli_ntcreate(targetcli, targetname, 0,
2738 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2739 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2740 0x0, 0x0, &fnum, NULL);
2741 if (!NT_STATUS_IS_OK(status)) {
2742 status = cli_ntcreate(targetcli, targetname, 0,
2743 FILE_READ_DATA, 0,
2744 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2745 0x0, 0x0, &fnum, NULL);
2746 if (NT_STATUS_IS_OK(status)) {
2747 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2748 } else {
2749 d_printf("Failed to open file %s. %s\n",
2750 targetname, nt_errstr(status));
2752 } else {
2753 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2755 return 0;
2758 static int cmd_posix_encrypt(void)
2760 TALLOC_CTX *ctx = talloc_tos();
2761 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2762 char *domain = NULL;
2763 char *user = NULL;
2764 char *password = NULL;
2765 struct cli_credentials *creds = NULL;
2766 struct cli_credentials *lcreds = NULL;
2768 if (next_token_talloc(ctx, &cmd_ptr, &domain, NULL)) {
2770 if (!next_token_talloc(ctx, &cmd_ptr, &user, NULL)) {
2771 d_printf("posix_encrypt domain user password\n");
2772 return 1;
2775 if (!next_token_talloc(ctx, &cmd_ptr, &password, NULL)) {
2776 d_printf("posix_encrypt domain user password\n");
2777 return 1;
2780 lcreds = cli_session_creds_init(ctx,
2781 user,
2782 domain,
2783 NULL, /* realm */
2784 password,
2785 false, /* use_kerberos */
2786 false, /* fallback_after_kerberos */
2787 false, /* use_ccache */
2788 false); /* password_is_nt_hash */
2789 if (lcreds == NULL) {
2790 d_printf("cli_session_creds_init() failed.\n");
2791 return -1;
2793 creds = lcreds;
2794 } else {
2795 bool auth_requested = false;
2797 creds = samba_cmdline_get_creds();
2799 auth_requested = cli_credentials_authentication_requested(creds);
2800 if (!auth_requested) {
2801 d_printf("posix_encrypt domain user password\n");
2802 return 1;
2806 status = cli_smb1_setup_encryption(cli, creds);
2807 /* gensec currently references the creds so we can't free them here */
2808 talloc_unlink(ctx, lcreds);
2809 if (!NT_STATUS_IS_OK(status)) {
2810 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2811 } else {
2812 bool ok;
2814 d_printf("encryption on\n");
2815 ok = cli_credentials_set_smb_encryption(creds,
2816 SMB_ENCRYPTION_REQUIRED,
2817 CRED_SPECIFIED);
2818 SMB_ASSERT(ok);
2821 return 0;
2824 /****************************************************************************
2825 ****************************************************************************/
2827 static int cmd_posix_open(void)
2829 TALLOC_CTX *ctx = talloc_tos();
2830 char *mask = NULL;
2831 char *buf = NULL;
2832 char *targetname = NULL;
2833 struct cli_state *targetcli;
2834 mode_t mode;
2835 uint16_t fnum;
2836 struct cli_credentials *creds = samba_cmdline_get_creds();
2837 NTSTATUS status;
2839 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2840 d_printf("posix_open <filename> 0<mode>\n");
2841 return 1;
2843 mask = talloc_asprintf(ctx,
2844 "%s%s",
2845 client_get_cur_dir(),
2846 buf);
2847 if (!mask) {
2848 return 1;
2850 mask = client_clean_name(ctx, mask);
2851 if (mask == NULL) {
2852 return 1;
2855 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2856 d_printf("posix_open <filename> 0<mode>\n");
2857 return 1;
2859 if (CLI_DIRSEP_CHAR != '/') {
2860 d_printf("Command \"posix\" must be issued before "
2861 "the \"posix_open\" command can be used.\n");
2862 return 1;
2864 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2866 status = cli_resolve_path(ctx, "",
2867 creds,
2868 cli, mask, &targetcli, &targetname);
2869 if (!NT_STATUS_IS_OK(status)) {
2870 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2871 return 1;
2874 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2875 &fnum);
2876 if (!NT_STATUS_IS_OK(status)) {
2877 status = cli_posix_open(targetcli, targetname,
2878 O_CREAT|O_RDONLY, mode, &fnum);
2879 if (!NT_STATUS_IS_OK(status)) {
2880 d_printf("Failed to open file %s. %s\n", targetname,
2881 nt_errstr(status));
2882 } else {
2883 d_printf("posix_open file %s: for readonly fnum %d\n",
2884 targetname, fnum);
2886 } else {
2887 d_printf("posix_open file %s: for read/write fnum %d\n",
2888 targetname, fnum);
2891 return 0;
2894 static int cmd_posix_mkdir(void)
2896 TALLOC_CTX *ctx = talloc_tos();
2897 char *mask = NULL;
2898 char *buf = NULL;
2899 char *targetname = NULL;
2900 struct cli_state *targetcli;
2901 mode_t mode;
2902 struct cli_credentials *creds = samba_cmdline_get_creds();
2903 NTSTATUS status;
2905 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2906 d_printf("posix_mkdir <filename> 0<mode>\n");
2907 return 1;
2909 mask = talloc_asprintf(ctx,
2910 "%s%s",
2911 client_get_cur_dir(),
2912 buf);
2913 if (!mask) {
2914 return 1;
2916 mask = client_clean_name(ctx, mask);
2917 if (mask == NULL) {
2918 return 1;
2921 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2922 d_printf("posix_mkdir <filename> 0<mode>\n");
2923 return 1;
2925 if (CLI_DIRSEP_CHAR != '/') {
2926 d_printf("Command \"posix\" must be issued before "
2927 "the \"posix_mkdir\" command can be used.\n");
2928 return 1;
2930 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2932 status = cli_resolve_path(ctx, "",
2933 creds,
2934 cli, mask, &targetcli, &targetname);
2935 if (!NT_STATUS_IS_OK(status)) {
2936 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2937 return 1;
2940 status = cli_posix_mkdir(targetcli, targetname, mode);
2941 if (!NT_STATUS_IS_OK(status)) {
2942 d_printf("Failed to open file %s. %s\n",
2943 targetname, nt_errstr(status));
2944 } else {
2945 d_printf("posix_mkdir created directory %s\n", targetname);
2947 return 0;
2950 static int cmd_posix_unlink(void)
2952 TALLOC_CTX *ctx = talloc_tos();
2953 char *mask = NULL;
2954 char *buf = NULL;
2955 char *targetname = NULL;
2956 struct cli_state *targetcli;
2957 struct cli_credentials *creds = samba_cmdline_get_creds();
2958 NTSTATUS status;
2960 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2961 d_printf("posix_unlink <filename>\n");
2962 return 1;
2964 if (CLI_DIRSEP_CHAR != '/') {
2965 d_printf("Command \"posix\" must be issued before "
2966 "the \"posix_unlink\" command can be used.\n");
2967 return 1;
2969 mask = talloc_asprintf(ctx,
2970 "%s%s",
2971 client_get_cur_dir(),
2972 buf);
2973 if (!mask) {
2974 return 1;
2976 mask = client_clean_name(ctx, mask);
2977 if (mask == NULL) {
2978 return 1;
2981 status = cli_resolve_path(ctx, "",
2982 creds,
2983 cli, mask, &targetcli, &targetname);
2984 if (!NT_STATUS_IS_OK(status)) {
2985 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
2986 return 1;
2989 status = cli_posix_unlink(targetcli, targetname);
2990 if (!NT_STATUS_IS_OK(status)) {
2991 d_printf("Failed to unlink file %s. %s\n",
2992 targetname, nt_errstr(status));
2993 } else {
2994 d_printf("posix_unlink deleted file %s\n", targetname);
2997 return 0;
3000 static int cmd_posix_rmdir(void)
3002 TALLOC_CTX *ctx = talloc_tos();
3003 char *mask = NULL;
3004 char *buf = NULL;
3005 char *targetname = NULL;
3006 struct cli_state *targetcli;
3007 struct cli_credentials *creds = samba_cmdline_get_creds();
3008 NTSTATUS status;
3010 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3011 d_printf("posix_rmdir <filename>\n");
3012 return 1;
3014 if (CLI_DIRSEP_CHAR != '/') {
3015 d_printf("Command \"posix\" must be issued before "
3016 "the \"posix_rmdir\" command can be used.\n");
3017 return 1;
3019 mask = talloc_asprintf(ctx,
3020 "%s%s",
3021 client_get_cur_dir(),
3022 buf);
3023 if (!mask) {
3024 return 1;
3026 mask = client_clean_name(ctx, mask);
3027 if (mask == NULL) {
3028 return 1;
3031 status = cli_resolve_path(ctx, "",
3032 creds,
3033 cli, mask, &targetcli, &targetname);
3034 if (!NT_STATUS_IS_OK(status)) {
3035 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
3036 return 1;
3039 status = cli_posix_rmdir(targetcli, targetname);
3040 if (!NT_STATUS_IS_OK(status)) {
3041 d_printf("Failed to unlink directory %s. %s\n",
3042 targetname, nt_errstr(status));
3043 } else {
3044 d_printf("posix_rmdir deleted directory %s\n", targetname);
3047 return 0;
3050 static int cmd_close(void)
3052 TALLOC_CTX *ctx = talloc_tos();
3053 char *buf = NULL;
3054 int fnum;
3055 NTSTATUS status;
3057 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3058 d_printf("close <fnum>\n");
3059 return 1;
3062 fnum = atoi(buf);
3063 /* We really should use the targetcli here.... */
3064 status = cli_close(cli, fnum);
3065 if (!NT_STATUS_IS_OK(status)) {
3066 d_printf("close %d: %s\n", fnum, nt_errstr(status));
3067 return 1;
3069 return 0;
3072 static int cmd_posix(void)
3074 TALLOC_CTX *ctx = talloc_tos();
3075 uint16_t major, minor;
3076 uint32_t caplow, caphigh;
3077 char *caps;
3078 NTSTATUS status;
3080 if (!SERVER_HAS_UNIX_CIFS(cli)) {
3081 d_printf("Server doesn't support UNIX CIFS extensions.\n");
3082 return 1;
3085 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
3086 &caphigh);
3087 if (!NT_STATUS_IS_OK(status)) {
3088 d_printf("Can't get UNIX CIFS extensions version from "
3089 "server: %s\n", nt_errstr(status));
3090 return 1;
3093 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
3095 caps = talloc_strdup(ctx, "");
3096 if (!caps) {
3097 return 1;
3099 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
3100 caps = talloc_asprintf_append(caps, "locks ");
3101 if (!caps) {
3102 return 1;
3105 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
3106 caps = talloc_asprintf_append(caps, "acls ");
3107 if (!caps) {
3108 return 1;
3111 if (caplow & CIFS_UNIX_XATTTR_CAP) {
3112 caps = talloc_asprintf_append(caps, "eas ");
3113 if (!caps) {
3114 return 1;
3117 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3118 caps = talloc_asprintf_append(caps, "pathnames ");
3119 if (!caps) {
3120 return 1;
3123 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
3124 caps = talloc_asprintf_append(caps, "posix_path_operations ");
3125 if (!caps) {
3126 return 1;
3129 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
3130 caps = talloc_asprintf_append(caps, "large_read ");
3131 if (!caps) {
3132 return 1;
3135 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
3136 caps = talloc_asprintf_append(caps, "large_write ");
3137 if (!caps) {
3138 return 1;
3141 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
3142 caps = talloc_asprintf_append(caps, "posix_encrypt ");
3143 if (!caps) {
3144 return 1;
3147 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
3148 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
3149 if (!caps) {
3150 return 1;
3154 if (*caps && caps[strlen(caps)-1] == ' ') {
3155 caps[strlen(caps)-1] = '\0';
3158 d_printf("Server supports CIFS capabilities %s\n", caps);
3160 status = cli_set_unix_extensions_capabilities(cli, major, minor,
3161 caplow, caphigh);
3162 if (!NT_STATUS_IS_OK(status)) {
3163 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
3164 nt_errstr(status));
3165 return 1;
3168 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3169 CLI_DIRSEP_CHAR = '/';
3170 *CLI_DIRSEP_STR = '/';
3171 client_set_cur_dir(CLI_DIRSEP_STR);
3174 return 0;
3177 static int cmd_lock(void)
3179 TALLOC_CTX *ctx = talloc_tos();
3180 char *buf = NULL;
3181 uint64_t start, len;
3182 enum brl_type lock_type;
3183 int fnum;
3184 NTSTATUS status;
3186 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3187 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3188 return 1;
3190 fnum = atoi(buf);
3192 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3193 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3194 return 1;
3197 if (*buf == 'r' || *buf == 'R') {
3198 lock_type = READ_LOCK;
3199 } else if (*buf == 'w' || *buf == 'W') {
3200 lock_type = WRITE_LOCK;
3201 } else {
3202 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3203 return 1;
3206 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3207 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3208 return 1;
3211 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3213 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3214 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3215 return 1;
3218 if (CLI_DIRSEP_CHAR != '/') {
3219 d_printf("Command \"posix\" must be issued before "
3220 "the \"lock\" command can be used.\n");
3221 return 1;
3224 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3226 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
3227 if (!NT_STATUS_IS_OK(status)) {
3228 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
3231 return 0;
3234 static int cmd_unlock(void)
3236 TALLOC_CTX *ctx = talloc_tos();
3237 char *buf = NULL;
3238 uint64_t start, len;
3239 int fnum;
3240 NTSTATUS status;
3242 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3243 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3244 return 1;
3246 fnum = atoi(buf);
3248 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3249 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3250 return 1;
3253 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3255 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3256 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3257 return 1;
3260 if (CLI_DIRSEP_CHAR != '/') {
3261 d_printf("Command \"posix\" must be issued before "
3262 "the \"unlock\" command can be used.\n");
3263 return 1;
3266 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3268 status = cli_posix_unlock(cli, fnum, start, len);
3269 if (!NT_STATUS_IS_OK(status)) {
3270 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
3273 return 0;
3276 static int cmd_posix_whoami(void)
3278 TALLOC_CTX *ctx = talloc_tos();
3279 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
3280 uint64_t uid = 0;
3281 uint64_t gid = 0;
3282 uint32_t num_gids = 0;
3283 uint32_t num_sids = 0;
3284 uint64_t *gids = NULL;
3285 struct dom_sid *sids = NULL;
3286 bool guest = false;
3287 uint32_t i;
3289 if (CLI_DIRSEP_CHAR != '/') {
3290 d_printf("Command \"posix\" must be issued before "
3291 "the \"posix_whoami\" command can be used.\n");
3292 return 1;
3295 status = cli_posix_whoami(cli,
3296 ctx,
3297 &uid,
3298 &gid,
3299 &num_gids,
3300 &gids,
3301 &num_sids,
3302 &sids,
3303 &guest);
3305 if (!NT_STATUS_IS_OK(status)) {
3306 d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
3307 return 1;
3310 d_printf("GUEST:%s\n", guest ? "True" : "False");
3311 d_printf("UID:%" PRIu64 "\n", uid);
3312 d_printf("GID:%" PRIu64 "\n", gid);
3313 d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
3314 for (i = 0; i < num_gids; i++) {
3315 d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
3317 d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
3318 for (i = 0; i < num_sids; i++) {
3319 struct dom_sid_buf buf;
3320 d_printf("SIDS[%" PRIu32 "]:%s\n",
3322 dom_sid_str_buf(&sids[i], &buf));
3324 return 0;
3328 /****************************************************************************
3329 Remove a directory.
3330 ****************************************************************************/
3332 static int cmd_rmdir(void)
3334 TALLOC_CTX *ctx = talloc_tos();
3335 char *mask = NULL;
3336 char *buf = NULL;
3337 char *targetname = NULL;
3338 struct cli_state *targetcli;
3339 struct cli_credentials *creds = samba_cmdline_get_creds();
3340 NTSTATUS status;
3342 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3343 d_printf("rmdir <dirname>\n");
3344 return 1;
3346 mask = talloc_asprintf(ctx,
3347 "%s%s",
3348 client_get_cur_dir(),
3349 buf);
3350 if (!mask) {
3351 return 1;
3353 mask = client_clean_name(ctx, mask);
3354 if (mask == NULL) {
3355 return 1;
3358 status = cli_resolve_path(ctx, "",
3359 creds,
3360 cli, mask, &targetcli, &targetname);
3361 if (!NT_STATUS_IS_OK(status)) {
3362 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3363 return 1;
3366 status = cli_rmdir(targetcli, targetname);
3367 if (!NT_STATUS_IS_OK(status)) {
3368 d_printf("%s removing remote directory file %s\n",
3369 nt_errstr(status), mask);
3372 return 0;
3375 /****************************************************************************
3376 UNIX hardlink.
3377 ****************************************************************************/
3379 static int cmd_link(void)
3381 TALLOC_CTX *ctx = talloc_tos();
3382 char *oldname = NULL;
3383 char *newname = NULL;
3384 char *buf = NULL;
3385 char *buf2 = NULL;
3386 char *targetname = NULL;
3387 struct cli_state *targetcli;
3388 struct cli_credentials *creds = samba_cmdline_get_creds();
3389 NTSTATUS status;
3391 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3392 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3393 d_printf("link <oldname> <newname>\n");
3394 return 1;
3396 oldname = talloc_asprintf(ctx,
3397 "%s%s",
3398 client_get_cur_dir(),
3399 buf);
3400 if (!oldname) {
3401 return 1;
3403 oldname = client_clean_name(ctx, oldname);
3404 if (oldname == NULL) {
3405 return 1;
3407 newname = talloc_asprintf(ctx,
3408 "%s%s",
3409 client_get_cur_dir(),
3410 buf2);
3411 if (!newname) {
3412 return 1;
3414 newname = client_clean_name(ctx, newname);
3415 if (newname == NULL) {
3416 return 1;
3419 status = cli_resolve_path(ctx, "",
3420 creds,
3421 cli, oldname, &targetcli, &targetname);
3422 if (!NT_STATUS_IS_OK(status)) {
3423 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3424 return 1;
3427 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3428 d_printf("Server doesn't support UNIX CIFS calls.\n");
3429 return 1;
3432 if (CLI_DIRSEP_CHAR != '/') {
3433 d_printf("Command \"posix\" must be issued before "
3434 "the \"link\" command can be used.\n");
3435 return 1;
3438 status = cli_posix_hardlink(targetcli, targetname, newname);
3439 if (!NT_STATUS_IS_OK(status)) {
3440 d_printf("%s linking files (%s -> %s)\n",
3441 nt_errstr(status), newname, oldname);
3442 return 1;
3444 return 0;
3447 /****************************************************************************
3448 UNIX readlink.
3449 ****************************************************************************/
3451 static int cmd_readlink(void)
3453 TALLOC_CTX *ctx = talloc_tos();
3454 char *name= NULL;
3455 char *buf = NULL;
3456 char *targetname = NULL;
3457 char *linkname = NULL;
3458 char *printname = NULL;
3459 uint32_t flags;
3460 struct cli_state *targetcli;
3461 struct cli_credentials *creds = samba_cmdline_get_creds();
3462 NTSTATUS status;
3464 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3465 d_printf("readlink <name>\n");
3466 return 1;
3468 name = talloc_asprintf(ctx,
3469 "%s%s",
3470 client_get_cur_dir(),
3471 buf);
3472 if (!name) {
3473 return 1;
3475 name = client_clean_name(ctx, name);
3476 if (name == NULL) {
3477 return 1;
3480 status = cli_resolve_path(ctx, "",
3481 creds,
3482 cli, name, &targetcli, &targetname);
3483 if (!NT_STATUS_IS_OK(status)) {
3484 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3485 return 1;
3488 status = cli_readlink(
3489 cli, name, talloc_tos(), &linkname, &printname, &flags);
3490 if (!NT_STATUS_IS_OK(status)) {
3491 d_printf("%s readlink on file %s\n",
3492 nt_errstr(status), name);
3493 return 1;
3496 d_printf("%s -> %s\n", name, linkname);
3498 TALLOC_FREE(linkname);
3500 return 0;
3504 /****************************************************************************
3505 UNIX symlink.
3506 ****************************************************************************/
3508 static int cmd_symlink(void)
3510 TALLOC_CTX *ctx = talloc_tos();
3511 char *link_target = NULL;
3512 char *newname = NULL;
3513 char *buf = NULL;
3514 char *buf2 = NULL;
3515 struct cli_state *newcli;
3516 struct cli_credentials *creds = samba_cmdline_get_creds();
3517 NTSTATUS status;
3519 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3520 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3521 d_printf("symlink <link_target> <newname>\n");
3522 return 1;
3524 /* Oldname (link target) must be an untouched blob. */
3525 link_target = buf;
3527 if (SERVER_HAS_UNIX_CIFS(cli)) {
3528 if (CLI_DIRSEP_CHAR != '/') {
3529 d_printf("Command \"posix\" must be issued before "
3530 "the \"symlink\" command can be used.\n");
3531 return 1;
3533 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3534 buf2);
3535 if (!newname) {
3536 return 1;
3538 newname = client_clean_name(ctx, newname);
3539 if (newname == NULL) {
3540 return 1;
3542 /* New name must be present in share namespace. */
3543 status = cli_resolve_path(ctx, "",
3544 creds,
3545 cli, newname,
3546 &newcli, &newname);
3547 if (!NT_STATUS_IS_OK(status)) {
3548 d_printf("link %s: %s\n", newname,
3549 nt_errstr(status));
3550 return 1;
3552 status = cli_posix_symlink(newcli, link_target, newname);
3553 } else {
3554 status = cli_symlink(
3555 cli, link_target, buf2,
3556 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3559 if (!NT_STATUS_IS_OK(status)) {
3560 d_printf("%s symlinking files (%s -> %s)\n",
3561 nt_errstr(status), newname, link_target);
3562 return 1;
3565 return 0;
3568 /****************************************************************************
3569 UNIX chmod.
3570 ****************************************************************************/
3572 static int cmd_chmod(void)
3574 TALLOC_CTX *ctx = talloc_tos();
3575 char *src = NULL;
3576 char *buf = NULL;
3577 char *buf2 = NULL;
3578 char *targetname = NULL;
3579 struct cli_state *targetcli;
3580 mode_t mode;
3581 struct cli_credentials *creds = samba_cmdline_get_creds();
3582 NTSTATUS status;
3584 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3585 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3586 d_printf("chmod mode file\n");
3587 return 1;
3589 src = talloc_asprintf(ctx,
3590 "%s%s",
3591 client_get_cur_dir(),
3592 buf2);
3593 if (!src) {
3594 return 1;
3596 src = client_clean_name(ctx, src);
3597 if (src == NULL) {
3598 return 1;
3601 mode = (mode_t)strtol(buf, NULL, 8);
3603 status = cli_resolve_path(ctx, "",
3604 creds,
3605 cli, src, &targetcli, &targetname);
3606 if (!NT_STATUS_IS_OK(status)) {
3607 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3608 return 1;
3611 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3612 d_printf("Server doesn't support UNIX CIFS calls.\n");
3613 return 1;
3616 if (CLI_DIRSEP_CHAR != '/') {
3617 d_printf("Command \"posix\" must be issued before "
3618 "the \"chmod\" command can be used.\n");
3619 return 1;
3622 status = cli_posix_chmod(targetcli, targetname, mode);
3623 if (!NT_STATUS_IS_OK(status)) {
3624 d_printf("%s chmod file %s 0%o\n",
3625 nt_errstr(status), src, (unsigned int)mode);
3626 return 1;
3629 return 0;
3632 static const char *filetype_to_str(mode_t mode)
3634 if (S_ISREG(mode)) {
3635 return "regular file";
3636 } else if (S_ISDIR(mode)) {
3637 return "directory";
3638 } else
3639 #ifdef S_ISCHR
3640 if (S_ISCHR(mode)) {
3641 return "character device";
3642 } else
3643 #endif
3644 #ifdef S_ISBLK
3645 if (S_ISBLK(mode)) {
3646 return "block device";
3647 } else
3648 #endif
3649 #ifdef S_ISFIFO
3650 if (S_ISFIFO(mode)) {
3651 return "fifo";
3652 } else
3653 #endif
3654 #ifdef S_ISLNK
3655 if (S_ISLNK(mode)) {
3656 return "symbolic link";
3657 } else
3658 #endif
3659 #ifdef S_ISSOCK
3660 if (S_ISSOCK(mode)) {
3661 return "socket";
3662 } else
3663 #endif
3664 return "";
3667 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3669 if (m & bt) {
3670 return ret;
3671 } else {
3672 return '-';
3676 static char *unix_mode_to_str(char *s, mode_t m)
3678 char *p = s;
3679 const char *str = filetype_to_str(m);
3681 switch(str[0]) {
3682 case 'd':
3683 *p++ = 'd';
3684 break;
3685 case 'c':
3686 *p++ = 'c';
3687 break;
3688 case 'b':
3689 *p++ = 'b';
3690 break;
3691 case 'f':
3692 *p++ = 'p';
3693 break;
3694 case 's':
3695 *p++ = str[1] == 'y' ? 'l' : 's';
3696 break;
3697 case 'r':
3698 default:
3699 *p++ = '-';
3700 break;
3702 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3703 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3704 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3705 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3706 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3707 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3708 *p++ = rwx_to_str(m, S_IROTH, 'r');
3709 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3710 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3711 *p++ = '\0';
3712 return s;
3715 /****************************************************************************
3716 Utility function for UNIX getfacl.
3717 ****************************************************************************/
3719 static char *perms_to_string(fstring permstr, unsigned char perms)
3721 fstrcpy(permstr, "---");
3722 if (perms & SMB_POSIX_ACL_READ) {
3723 permstr[0] = 'r';
3725 if (perms & SMB_POSIX_ACL_WRITE) {
3726 permstr[1] = 'w';
3728 if (perms & SMB_POSIX_ACL_EXECUTE) {
3729 permstr[2] = 'x';
3731 return permstr;
3734 /****************************************************************************
3735 UNIX getfacl.
3736 ****************************************************************************/
3738 static int cmd_getfacl(void)
3740 TALLOC_CTX *ctx = talloc_tos();
3741 char *src = NULL;
3742 char *name = NULL;
3743 char *targetname = NULL;
3744 struct cli_state *targetcli;
3745 uint16_t major, minor;
3746 uint32_t caplow, caphigh;
3747 char *retbuf = NULL;
3748 size_t rb_size = 0;
3749 SMB_STRUCT_STAT sbuf;
3750 size_t num_file_acls = 0;
3751 size_t num_dir_acls = 0;
3752 size_t expected_buflen;
3753 uint16_t i;
3754 struct cli_credentials *creds = samba_cmdline_get_creds();
3755 NTSTATUS status;
3757 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3758 d_printf("getfacl filename\n");
3759 return 1;
3761 src = talloc_asprintf(ctx,
3762 "%s%s",
3763 client_get_cur_dir(),
3764 name);
3765 if (!src) {
3766 return 1;
3768 src = client_clean_name(ctx, src);
3769 if (src == NULL) {
3770 return 1;
3773 status = cli_resolve_path(ctx, "",
3774 creds,
3775 cli, src, &targetcli, &targetname);
3776 if (!NT_STATUS_IS_OK(status)) {
3777 d_printf("stat %s: %s\n", src, nt_errstr(status));
3778 return 1;
3781 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3782 d_printf("Server doesn't support UNIX CIFS calls.\n");
3783 return 1;
3786 if (CLI_DIRSEP_CHAR != '/') {
3787 d_printf("Command \"posix\" must be issued before "
3788 "the \"getfacl\" command can be used.\n");
3789 return 1;
3792 status = cli_unix_extensions_version(targetcli, &major, &minor,
3793 &caplow, &caphigh);
3794 if (!NT_STATUS_IS_OK(status)) {
3795 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3796 nt_errstr(status));
3797 return 1;
3800 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3801 d_printf("This server supports UNIX extensions "
3802 "but doesn't support POSIX ACLs.\n");
3803 return 1;
3806 status = cli_posix_stat(targetcli, targetname, &sbuf);
3807 if (!NT_STATUS_IS_OK(status)) {
3808 d_printf("%s getfacl doing a stat on file %s\n",
3809 nt_errstr(status), src);
3810 return 1;
3813 status = cli_posix_getacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3814 if (!NT_STATUS_IS_OK(status)) {
3815 d_printf("%s getfacl file %s\n",
3816 nt_errstr(status), src);
3817 return 1;
3820 /* ToDo : Print out the ACL values. */
3821 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3822 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3823 src, (unsigned int)CVAL(retbuf,0) );
3824 return 1;
3827 num_file_acls = SVAL(retbuf,2);
3828 num_dir_acls = SVAL(retbuf,4);
3831 * No overflow check, num_*_acls comes from a 16-bit value,
3832 * and we expect expected_buflen (size_t) to be of at least 32
3833 * bit.
3835 expected_buflen = SMB_POSIX_ACL_HEADER_SIZE +
3836 SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls);
3838 if (rb_size != expected_buflen) {
3839 d_printf("getfacl file %s, incorrect POSIX acl buffer size "
3840 "(should be %zu, was %zu).\n",
3841 src,
3842 expected_buflen,
3843 rb_size);
3844 return 1;
3847 d_printf("# file: %s\n", src);
3848 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3850 if (num_file_acls == 0 && num_dir_acls == 0) {
3851 d_printf("No acls found.\n");
3854 for (i = 0; i < num_file_acls; i++) {
3855 uint32_t uorg;
3856 fstring permstring;
3857 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3858 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3860 switch(tagtype) {
3861 case SMB_POSIX_ACL_USER_OBJ:
3862 d_printf("user::");
3863 break;
3864 case SMB_POSIX_ACL_USER:
3865 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3866 d_printf("user:%u:", uorg);
3867 break;
3868 case SMB_POSIX_ACL_GROUP_OBJ:
3869 d_printf("group::");
3870 break;
3871 case SMB_POSIX_ACL_GROUP:
3872 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3873 d_printf("group:%u:", uorg);
3874 break;
3875 case SMB_POSIX_ACL_MASK:
3876 d_printf("mask::");
3877 break;
3878 case SMB_POSIX_ACL_OTHER:
3879 d_printf("other::");
3880 break;
3881 default:
3882 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3883 src, (unsigned int)tagtype );
3884 SAFE_FREE(retbuf);
3885 return 1;
3888 d_printf("%s\n", perms_to_string(permstring, perms));
3891 for (i = 0; i < num_dir_acls; i++) {
3892 uint32_t uorg;
3893 fstring permstring;
3894 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3895 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3897 switch(tagtype) {
3898 case SMB_POSIX_ACL_USER_OBJ:
3899 d_printf("default:user::");
3900 break;
3901 case SMB_POSIX_ACL_USER:
3902 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3903 d_printf("default:user:%u:", uorg);
3904 break;
3905 case SMB_POSIX_ACL_GROUP_OBJ:
3906 d_printf("default:group::");
3907 break;
3908 case SMB_POSIX_ACL_GROUP:
3909 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3910 d_printf("default:group:%u:", uorg);
3911 break;
3912 case SMB_POSIX_ACL_MASK:
3913 d_printf("default:mask::");
3914 break;
3915 case SMB_POSIX_ACL_OTHER:
3916 d_printf("default:other::");
3917 break;
3918 default:
3919 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3920 src, (unsigned int)tagtype );
3921 SAFE_FREE(retbuf);
3922 return 1;
3925 d_printf("%s\n", perms_to_string(permstring, perms));
3928 return 0;
3931 /****************************************************************************
3932 Get the EA list of a file
3933 ****************************************************************************/
3935 static int cmd_geteas(void)
3937 TALLOC_CTX *ctx = talloc_tos();
3938 char *src = NULL;
3939 char *name = NULL;
3940 char *targetname = NULL;
3941 struct cli_state *targetcli;
3942 NTSTATUS status;
3943 size_t i, num_eas;
3944 struct ea_struct *eas;
3945 struct cli_credentials *creds = samba_cmdline_get_creds();
3947 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3948 d_printf("geteas filename\n");
3949 return 1;
3951 src = talloc_asprintf(ctx,
3952 "%s%s",
3953 client_get_cur_dir(),
3954 name);
3955 if (!src) {
3956 return 1;
3958 src = client_clean_name(ctx, src);
3959 if (src == NULL) {
3960 return 1;
3963 status = cli_resolve_path(ctx, "",
3964 creds,
3965 cli, src, &targetcli, &targetname);
3966 if (!NT_STATUS_IS_OK(status)) {
3967 d_printf("stat %s: %s\n", src, nt_errstr(status));
3968 return 1;
3971 status = cli_get_ea_list_path(targetcli, targetname, talloc_tos(),
3972 &num_eas, &eas);
3973 if (!NT_STATUS_IS_OK(status)) {
3974 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
3975 return 1;
3978 for (i=0; i<num_eas; i++) {
3979 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
3980 dump_data_file(eas[i].value.data, eas[i].value.length, false,
3981 stdout);
3982 d_printf("\n");
3985 TALLOC_FREE(eas);
3987 return 0;
3990 /****************************************************************************
3991 Set an EA of a file
3992 ****************************************************************************/
3994 static int cmd_setea(void)
3996 TALLOC_CTX *ctx = talloc_tos();
3997 char *src = NULL;
3998 char *name = NULL;
3999 char *eaname = NULL;
4000 char *eavalue = NULL;
4001 char *targetname = NULL;
4002 struct cli_state *targetcli;
4003 struct cli_credentials *creds = samba_cmdline_get_creds();
4004 NTSTATUS status;
4006 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
4007 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
4008 d_printf("setea filename eaname value\n");
4009 return 1;
4011 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
4012 eavalue = talloc_strdup(ctx, "");
4014 src = talloc_asprintf(ctx,
4015 "%s%s",
4016 client_get_cur_dir(),
4017 name);
4018 if (!src) {
4019 return 1;
4021 src = client_clean_name(ctx, src);
4022 if (src == NULL) {
4023 return 1;
4026 status = cli_resolve_path(ctx, "",
4027 creds,
4028 cli, src, &targetcli, &targetname);
4029 if (!NT_STATUS_IS_OK(status)) {
4030 d_printf("stat %s: %s\n", src, nt_errstr(status));
4031 return 1;
4034 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
4035 strlen(eavalue));
4036 if (!NT_STATUS_IS_OK(status)) {
4037 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
4038 return 1;
4041 return 0;
4044 /****************************************************************************
4045 UNIX stat.
4046 ****************************************************************************/
4048 static int cmd_stat(void)
4050 TALLOC_CTX *ctx = talloc_tos();
4051 char *src = NULL;
4052 char *name = NULL;
4053 char *targetname = NULL;
4054 struct cli_state *targetcli;
4055 fstring mode_str;
4056 SMB_STRUCT_STAT sbuf;
4057 struct tm *lt;
4058 time_t tmp_time;
4059 struct cli_credentials *creds = samba_cmdline_get_creds();
4060 NTSTATUS status;
4062 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
4063 d_printf("stat file\n");
4064 return 1;
4066 src = talloc_asprintf(ctx,
4067 "%s%s",
4068 client_get_cur_dir(),
4069 name);
4070 if (!src) {
4071 return 1;
4073 src = client_clean_name(ctx, src);
4074 if (src == NULL) {
4075 return 1;
4078 status = cli_resolve_path(ctx, "",
4079 creds,
4080 cli, src, &targetcli, &targetname);
4081 if (!NT_STATUS_IS_OK(status)) {
4082 d_printf("stat %s: %s\n", src, nt_errstr(status));
4083 return 1;
4086 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4087 d_printf("Server doesn't support UNIX CIFS calls.\n");
4088 return 1;
4091 if (CLI_DIRSEP_CHAR != '/') {
4092 d_printf("Command \"posix\" must be issued before "
4093 "the \"stat\" command can be used.\n");
4094 return 1;
4097 status = cli_posix_stat(targetcli, targetname, &sbuf);
4098 if (!NT_STATUS_IS_OK(status)) {
4099 d_printf("%s stat file %s\n",
4100 nt_errstr(status), src);
4101 return 1;
4104 /* Print out the stat values. */
4105 d_printf("File: %s\n", src);
4106 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
4107 (double)sbuf.st_ex_size,
4108 (unsigned int)sbuf.st_ex_blocks,
4109 filetype_to_str(sbuf.st_ex_mode));
4111 #if defined(S_ISCHR) && defined(S_ISBLK)
4112 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
4113 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
4114 (double)sbuf.st_ex_ino,
4115 (unsigned int)sbuf.st_ex_nlink,
4116 unix_dev_major(sbuf.st_ex_rdev),
4117 unix_dev_minor(sbuf.st_ex_rdev));
4118 } else
4119 #endif
4120 d_printf("Inode: %.0f\tLinks: %u\n",
4121 (double)sbuf.st_ex_ino,
4122 (unsigned int)sbuf.st_ex_nlink);
4124 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
4125 ((int)sbuf.st_ex_mode & 0777),
4126 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
4127 (unsigned int)sbuf.st_ex_uid,
4128 (unsigned int)sbuf.st_ex_gid);
4130 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
4131 lt = localtime(&tmp_time);
4132 if (lt) {
4133 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4134 } else {
4135 fstrcpy(mode_str, "unknown");
4137 d_printf("Access: %s\n", mode_str);
4139 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4140 lt = localtime(&tmp_time);
4141 if (lt) {
4142 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4143 } else {
4144 fstrcpy(mode_str, "unknown");
4146 d_printf("Modify: %s\n", mode_str);
4148 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
4149 lt = localtime(&tmp_time);
4150 if (lt) {
4151 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4152 } else {
4153 fstrcpy(mode_str, "unknown");
4155 d_printf("Change: %s\n", mode_str);
4157 return 0;
4161 /****************************************************************************
4162 UNIX chown.
4163 ****************************************************************************/
4165 static int cmd_chown(void)
4167 TALLOC_CTX *ctx = talloc_tos();
4168 char *src = NULL;
4169 uid_t uid;
4170 gid_t gid;
4171 char *buf, *buf2, *buf3;
4172 struct cli_state *targetcli;
4173 char *targetname = NULL;
4174 struct cli_credentials *creds = samba_cmdline_get_creds();
4175 NTSTATUS status;
4177 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4178 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
4179 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
4180 d_printf("chown uid gid file\n");
4181 return 1;
4184 uid = (uid_t)atoi(buf);
4185 gid = (gid_t)atoi(buf2);
4187 src = talloc_asprintf(ctx,
4188 "%s%s",
4189 client_get_cur_dir(),
4190 buf3);
4191 if (!src) {
4192 return 1;
4194 src = client_clean_name(ctx, src);
4195 if (src == NULL) {
4196 return 1;
4198 status = cli_resolve_path(ctx, "",
4199 creds,
4200 cli, src, &targetcli, &targetname);
4201 if (!NT_STATUS_IS_OK(status)) {
4202 d_printf("chown %s: %s\n", src, nt_errstr(status));
4203 return 1;
4206 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4207 d_printf("Server doesn't support UNIX CIFS calls.\n");
4208 return 1;
4211 if (CLI_DIRSEP_CHAR != '/') {
4212 d_printf("Command \"posix\" must be issued before "
4213 "the \"chown\" command can be used.\n");
4214 return 1;
4217 status = cli_posix_chown(targetcli, targetname, uid, gid);
4218 if (!NT_STATUS_IS_OK(status)) {
4219 d_printf("%s chown file %s uid=%d, gid=%d\n",
4220 nt_errstr(status), src, (int)uid, (int)gid);
4221 return 1;
4224 return 0;
4227 /****************************************************************************
4228 Rename some file.
4229 ****************************************************************************/
4231 static int cmd_rename(void)
4233 TALLOC_CTX *ctx = talloc_tos();
4234 char *src, *dest;
4235 char *buf, *buf2;
4236 struct cli_state *targetcli;
4237 char *targetsrc;
4238 char *targetdest;
4239 struct cli_credentials *creds = samba_cmdline_get_creds();
4240 NTSTATUS status;
4241 bool replace = false;
4243 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4244 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4245 d_printf("rename <src> <dest> [-f]\n");
4246 return 1;
4249 src = talloc_asprintf(ctx,
4250 "%s%s",
4251 client_get_cur_dir(),
4252 buf);
4253 if (!src) {
4254 return 1;
4256 src = client_clean_name(ctx, src);
4257 if (src == NULL) {
4258 return 1;
4261 dest = talloc_asprintf(ctx,
4262 "%s%s",
4263 client_get_cur_dir(),
4264 buf2);
4265 if (!dest) {
4266 return 1;
4268 dest = client_clean_name(ctx, dest);
4269 if (dest == NULL) {
4270 return 1;
4273 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
4274 strcsequal(buf, "-f")) {
4275 replace = true;
4278 status = cli_resolve_path(ctx, "",
4279 creds,
4280 cli, src, &targetcli, &targetsrc);
4281 if (!NT_STATUS_IS_OK(status)) {
4282 d_printf("rename %s: %s\n", src, nt_errstr(status));
4283 return 1;
4286 status = cli_resolve_path(ctx, "",
4287 creds,
4288 cli, dest, &targetcli, &targetdest);
4289 if (!NT_STATUS_IS_OK(status)) {
4290 d_printf("rename %s: %s\n", dest, nt_errstr(status));
4291 return 1;
4294 status = cli_rename(targetcli, targetsrc, targetdest, replace);
4295 if (!NT_STATUS_IS_OK(status)) {
4296 d_printf("%s renaming files %s -> %s \n",
4297 nt_errstr(status),
4298 targetsrc,
4299 targetdest);
4300 return 1;
4303 return 0;
4306 struct scopy_timing {
4307 struct timespec tp_start;
4310 static int scopy_status(off_t written, void *priv)
4312 struct timespec tp_end;
4313 unsigned int scopy_total_time_ms;
4314 struct scopy_timing *st = priv;
4316 clock_gettime_mono(&tp_end);
4317 scopy_total_time_ms = nsec_time_diff(&tp_end,&st->tp_start)/1000000;
4319 DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
4320 (intmax_t)written, written / (1.024*scopy_total_time_ms)));
4322 return true;
4325 /****************************************************************************
4326 Server-Side copy some file.
4327 ****************************************************************************/
4329 static int cmd_scopy(void)
4331 TALLOC_CTX *ctx = talloc_tos();
4332 char *src, *dest;
4333 char *buf, *buf2;
4334 struct cli_state *targetcli;
4335 char *targetsrc;
4336 char *targetdest;
4337 uint32_t DesiredAccess, ShareAccess, CreateDisposition, CreateOptions;
4338 struct smb_create_returns cr;
4339 uint16_t destfnum = (uint16_t)-1;
4340 uint16_t srcfnum = (uint16_t)-1;
4341 off_t written = 0;
4342 struct scopy_timing st;
4343 int rc = 0;
4344 struct cli_credentials *creds = samba_cmdline_get_creds();
4345 NTSTATUS status;
4347 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4348 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4349 d_printf("scopy <src> <dest>\n");
4350 return 1;
4353 src = talloc_asprintf(ctx,
4354 "%s%s",
4355 client_get_cur_dir(),
4356 buf);
4357 if (!src) {
4358 return 1;
4360 src = client_clean_name(ctx, src);
4361 if (src == NULL) {
4362 return 1;
4365 dest = talloc_asprintf(ctx,
4366 "%s%s",
4367 client_get_cur_dir(),
4368 buf2);
4369 if (!dest) {
4370 return 1;
4372 dest = client_clean_name(ctx, dest);
4373 if (dest == NULL) {
4374 return 1;
4377 status = cli_resolve_path(ctx, "",
4378 creds,
4379 cli, src, &targetcli, &targetsrc);
4380 if (!NT_STATUS_IS_OK(status)) {
4381 d_printf("scopy %s: %s\n", src, nt_errstr(status));
4382 return 1;
4385 status = cli_resolve_path(ctx, "",
4386 creds,
4387 cli, dest, &targetcli, &targetdest);
4388 if (!NT_STATUS_IS_OK(status)) {
4389 d_printf("scopy %s: %s\n", dest, nt_errstr(status));
4390 return 1;
4394 DesiredAccess = (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES|
4395 READ_CONTROL_ACCESS|SYNCHRONIZE_ACCESS);
4396 ShareAccess = FILE_SHARE_READ|FILE_SHARE_DELETE;
4397 CreateDisposition = FILE_OPEN;
4398 CreateOptions = (FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE|
4399 FILE_OPEN_REPARSE_POINT);
4400 status = cli_ntcreate(targetcli, targetsrc, 0, DesiredAccess, 0,
4401 ShareAccess, CreateDisposition, CreateOptions, 0x0,
4402 &srcfnum, &cr);
4403 if (!NT_STATUS_IS_OK(status)) {
4404 d_printf("Failed to open file %s. %s\n",
4405 targetsrc, nt_errstr(status));
4406 return 1;
4409 DesiredAccess = (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_READ_EA|
4410 FILE_WRITE_EA|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|
4411 DELETE_ACCESS|READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|SYNCHRONIZE_ACCESS);
4412 ShareAccess = FILE_SHARE_NONE;
4413 CreateDisposition = FILE_CREATE;
4414 CreateOptions = FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE;
4415 status = cli_ntcreate(targetcli, targetdest, 0, DesiredAccess,
4416 FILE_ATTRIBUTE_ARCHIVE, ShareAccess, CreateDisposition,
4417 CreateOptions, 0x0, &destfnum, NULL);
4418 if (!NT_STATUS_IS_OK(status)) {
4419 d_printf("Failed to create file %s. %s\n",
4420 targetdest, nt_errstr(status));
4421 cli_close(targetcli, srcfnum);
4422 return 1;
4425 clock_gettime_mono(&st.tp_start);
4426 status = cli_splice(targetcli, targetcli, srcfnum, destfnum,
4427 cr.end_of_file, 0, 0, &written, scopy_status, &st);
4428 if (!NT_STATUS_IS_OK(status)) {
4429 d_printf("%s copying file %s -> %s \n",
4430 nt_errstr(status),
4431 targetsrc,
4432 targetdest);
4433 rc = 1;
4436 status = cli_close(targetcli, srcfnum);
4437 if (!NT_STATUS_IS_OK(status)) {
4438 d_printf("Error %s closing remote source file\n", nt_errstr(status));
4439 rc = 1;
4441 status = cli_close(targetcli, destfnum);
4442 if (!NT_STATUS_IS_OK(status)) {
4443 d_printf("Error %s closing remote dest file\n", nt_errstr(status));
4444 rc = 1;
4447 return rc;
4450 /****************************************************************************
4451 Print the volume name.
4452 ****************************************************************************/
4454 static int cmd_volume(void)
4456 char *volname;
4457 uint32_t serial_num;
4458 time_t create_date;
4459 NTSTATUS status;
4461 status = cli_get_fs_volume_info(cli, talloc_tos(),
4462 &volname, &serial_num,
4463 &create_date);
4464 if (!NT_STATUS_IS_OK(status)) {
4465 d_printf("Error %s getting volume info\n", nt_errstr(status));
4466 return 1;
4469 d_printf("Volume: |%s| serial number 0x%x\n",
4470 volname, (unsigned int)serial_num);
4471 return 0;
4474 /****************************************************************************
4475 Hard link files using the NT call.
4476 ****************************************************************************/
4478 static int cmd_hardlink(void)
4480 TALLOC_CTX *ctx = talloc_tos();
4481 char *src, *dest;
4482 char *buf, *buf2;
4483 struct cli_state *targetcli;
4484 char *targetname;
4485 struct cli_credentials *creds = samba_cmdline_get_creds();
4486 NTSTATUS status;
4488 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4489 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4490 d_printf("hardlink <src> <dest>\n");
4491 return 1;
4494 src = talloc_asprintf(ctx,
4495 "%s%s",
4496 client_get_cur_dir(),
4497 buf);
4498 if (!src) {
4499 return 1;
4501 src = client_clean_name(ctx, src);
4502 if (src == NULL) {
4503 return 1;
4506 dest = talloc_asprintf(ctx,
4507 "%s%s",
4508 client_get_cur_dir(),
4509 buf2);
4510 if (!dest) {
4511 return 1;
4513 dest = client_clean_name(ctx, dest);
4514 if (dest == NULL) {
4515 return 1;
4518 status = cli_resolve_path(ctx, "",
4519 creds,
4520 cli, src, &targetcli, &targetname);
4521 if (!NT_STATUS_IS_OK(status)) {
4522 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
4523 return 1;
4526 status = cli_hardlink(targetcli, targetname, dest);
4527 if (!NT_STATUS_IS_OK(status)) {
4528 d_printf("%s doing an NT hard link of files\n",
4529 nt_errstr(status));
4530 return 1;
4533 return 0;
4536 /****************************************************************************
4537 Toggle the prompt flag.
4538 ****************************************************************************/
4540 static int cmd_prompt(void)
4542 prompt = !prompt;
4543 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
4544 return 1;
4547 /****************************************************************************
4548 Set the newer than time.
4549 ****************************************************************************/
4551 static int cmd_newer(void)
4553 TALLOC_CTX *ctx = talloc_tos();
4554 char *buf;
4555 bool ok;
4556 SMB_STRUCT_STAT sbuf;
4558 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
4559 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
4560 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4561 DEBUG(1,("Getting files newer than %s",
4562 time_to_asc(newer_than)));
4563 } else {
4564 newer_than = 0;
4567 if (ok && newer_than == 0) {
4568 d_printf("Error setting newer-than time\n");
4569 return 1;
4572 return 0;
4575 /****************************************************************************
4576 Watch directory changes
4577 ****************************************************************************/
4579 static int cmd_notify(void)
4581 TALLOC_CTX *frame = talloc_stackframe();
4582 char *name, *buf;
4583 NTSTATUS status;
4584 uint16_t fnum;
4586 name = talloc_strdup(talloc_tos(), client_get_cur_dir());
4587 if (name == NULL) {
4588 goto fail;
4590 if (!next_token_talloc(talloc_tos(), &cmd_ptr, &buf, NULL)) {
4591 goto usage;
4593 name = talloc_asprintf_append(name, "%s", buf);
4594 if (name == NULL) {
4595 goto fail;
4597 name = client_clean_name(talloc_tos(), name);
4598 if (name == NULL) {
4599 return 1;
4601 status = cli_ntcreate(
4602 cli, name, 0, FILE_READ_DATA, 0,
4603 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4604 FILE_OPEN, 0, 0, &fnum, NULL);
4605 if (!NT_STATUS_IS_OK(status)) {
4606 d_printf("Could not open file: %s\n", nt_errstr(status));
4607 goto fail;
4610 while (1) {
4611 uint32_t i;
4612 uint32_t num_changes = 0;
4613 struct notify_change *changes = NULL;
4615 status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
4616 true,
4617 talloc_tos(), &num_changes, &changes);
4618 if (NT_STATUS_EQUAL(status, NT_STATUS_NOTIFY_ENUM_DIR)) {
4619 printf("NOTIFY_ENUM_DIR\n");
4620 status = NT_STATUS_OK;
4622 if (!NT_STATUS_IS_OK(status)) {
4623 d_printf("notify returned %s\n",
4624 nt_errstr(status));
4625 goto fail;
4627 for (i=0; i<num_changes; i++) {
4628 printf("%4.4x %s\n", changes[i].action,
4629 changes[i].name);
4631 TALLOC_FREE(changes);
4633 usage:
4634 d_printf("notify <dir name>\n");
4635 fail:
4636 TALLOC_FREE(frame);
4637 return 1;
4640 /****************************************************************************
4641 Set the archive level.
4642 ****************************************************************************/
4644 static int cmd_archive(void)
4646 TALLOC_CTX *ctx = talloc_tos();
4647 char *buf;
4649 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4650 archive_level = atoi(buf);
4651 } else {
4652 d_printf("Archive level is %d\n",archive_level);
4655 return 0;
4658 /****************************************************************************
4659 Toggle the backup_intent state.
4660 ****************************************************************************/
4662 static int cmd_backup(void)
4664 backup_intent = !backup_intent;
4665 cli_set_backup_intent(cli, backup_intent);
4666 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4667 return 1;
4670 /****************************************************************************
4671 Toggle the lowercaseflag.
4672 ****************************************************************************/
4674 static int cmd_lowercase(void)
4676 lowercase = !lowercase;
4677 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4678 return 0;
4681 /****************************************************************************
4682 Toggle the case sensitive flag.
4683 ****************************************************************************/
4685 static int cmd_setcase(void)
4687 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4689 cli_set_case_sensitive(cli, !orig_case_sensitive);
4690 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4691 "on":"off"));
4692 return 0;
4695 /****************************************************************************
4696 Toggle the showacls flag.
4697 ****************************************************************************/
4699 static int cmd_showacls(void)
4701 showacls = !showacls;
4702 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4703 return 0;
4707 /****************************************************************************
4708 Toggle the recurse flag.
4709 ****************************************************************************/
4711 static int cmd_recurse(void)
4713 recurse = !recurse;
4714 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4715 return 0;
4718 /****************************************************************************
4719 Toggle the translate flag.
4720 ****************************************************************************/
4722 static int cmd_translate(void)
4724 translation = !translation;
4725 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4726 translation?"on":"off"));
4727 return 0;
4730 /****************************************************************************
4731 Do the lcd command.
4732 ****************************************************************************/
4734 static int cmd_lcd(void)
4736 TALLOC_CTX *ctx = talloc_tos();
4737 char *buf;
4738 char *d;
4740 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4741 if (chdir(buf) == -1) {
4742 d_printf("chdir to %s failed (%s)\n",
4743 buf, strerror(errno));
4746 d = sys_getwd();
4747 if (!d) {
4748 return 1;
4750 DEBUG(2,("the local directory is now %s\n",d));
4751 SAFE_FREE(d);
4752 return 0;
4755 /****************************************************************************
4756 Get a file restarting at end of local file.
4757 ****************************************************************************/
4759 static int cmd_reget(void)
4761 TALLOC_CTX *ctx = talloc_tos();
4762 char *local_name = NULL;
4763 char *remote_name = NULL;
4764 char *fname = NULL;
4765 char *p = NULL;
4767 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4768 if (!remote_name) {
4769 return 1;
4772 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4773 d_printf("reget <filename>\n");
4774 return 1;
4776 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4777 if (!remote_name) {
4778 return 1;
4780 remote_name = client_clean_name(ctx,remote_name);
4781 if (!remote_name) {
4782 return 1;
4785 local_name = fname;
4786 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4787 if (p) {
4788 local_name = p;
4791 return do_get(remote_name, local_name, true);
4794 /****************************************************************************
4795 Put a file restarting at end of local file.
4796 ****************************************************************************/
4798 static int cmd_reput(void)
4800 TALLOC_CTX *ctx = talloc_tos();
4801 char *local_name = NULL;
4802 char *remote_name = NULL;
4803 char *buf;
4804 SMB_STRUCT_STAT st;
4806 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4807 if (!remote_name) {
4808 return 1;
4811 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4812 d_printf("reput <filename>\n");
4813 return 1;
4816 if (!file_exist_stat(local_name, &st, false)) {
4817 d_printf("%s does not exist\n", local_name);
4818 return 1;
4821 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4822 remote_name = talloc_asprintf_append(remote_name,
4823 "%s", buf);
4824 } else {
4825 remote_name = talloc_asprintf_append(remote_name,
4826 "%s", local_name);
4828 if (!remote_name) {
4829 return 1;
4832 remote_name = client_clean_name(ctx, remote_name);
4833 if (!remote_name) {
4834 return 1;
4837 return do_put(remote_name, local_name, true);
4840 /****************************************************************************
4841 List a share name.
4842 ****************************************************************************/
4844 static void browse_fn(const char *name, uint32_t m,
4845 const char *comment, void *state)
4847 const char *typestr = "";
4849 switch (m & 7) {
4850 case STYPE_DISKTREE:
4851 typestr = "Disk";
4852 break;
4853 case STYPE_PRINTQ:
4854 typestr = "Printer";
4855 break;
4856 case STYPE_DEVICE:
4857 typestr = "Device";
4858 break;
4859 case STYPE_IPC:
4860 typestr = "IPC";
4861 break;
4863 /* FIXME: If the remote machine returns non-ascii characters
4864 in any of these fields, they can corrupt the output. We
4865 should remove them. */
4866 if (!grepable) {
4867 d_printf("\t%-15s %-10.10s%s\n",
4868 name,typestr,comment);
4869 } else {
4870 d_printf ("%s|%s|%s\n",typestr,name,comment);
4874 static bool browse_host_rpc(bool sort)
4876 NTSTATUS status;
4877 struct rpc_pipe_client *pipe_hnd = NULL;
4878 TALLOC_CTX *frame = talloc_stackframe();
4879 WERROR werr;
4880 struct srvsvc_NetShareInfoCtr info_ctr;
4881 struct srvsvc_NetShareCtr1 ctr1;
4882 uint32_t resume_handle = 0;
4883 uint32_t total_entries = 0;
4884 uint32_t i;
4885 struct dcerpc_binding_handle *b;
4887 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
4888 &pipe_hnd);
4890 if (!NT_STATUS_IS_OK(status)) {
4891 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4892 nt_errstr(status)));
4893 TALLOC_FREE(frame);
4894 return false;
4897 b = pipe_hnd->binding_handle;
4899 ZERO_STRUCT(info_ctr);
4900 ZERO_STRUCT(ctr1);
4902 info_ctr.level = 1;
4903 info_ctr.ctr.ctr1 = &ctr1;
4905 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4906 pipe_hnd->desthost,
4907 &info_ctr,
4908 0xffffffff,
4909 &total_entries,
4910 &resume_handle,
4911 &werr);
4913 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4914 TALLOC_FREE(pipe_hnd);
4915 TALLOC_FREE(frame);
4916 return false;
4919 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4920 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4921 browse_fn(info.name, info.type, info.comment, NULL);
4924 TALLOC_FREE(pipe_hnd);
4925 TALLOC_FREE(frame);
4926 return true;
4929 /****************************************************************************
4930 Try and browse available connections on a host.
4931 ****************************************************************************/
4933 static bool browse_host(bool sort)
4935 int ret;
4937 if (!grepable) {
4938 d_printf("\n\tSharename Type Comment\n");
4939 d_printf("\t--------- ---- -------\n");
4942 if (browse_host_rpc(sort)) {
4943 return true;
4946 if (smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1) {
4947 return false;
4950 ret = cli_RNetShareEnum(cli, browse_fn, NULL);
4951 if (ret == -1) {
4952 NTSTATUS status = cli_nt_error(cli);
4953 d_printf("Error returning browse list: %s\n",
4954 nt_errstr(status));
4957 return (ret != -1);
4960 /****************************************************************************
4961 List a server name.
4962 ****************************************************************************/
4964 static void server_fn(const char *name, uint32_t m,
4965 const char *comment, void *state)
4968 if (!grepable){
4969 d_printf("\t%-16s %s\n", name, comment);
4970 } else {
4971 d_printf("%s|%s|%s\n",(char *)state, name, comment);
4975 /****************************************************************************
4976 Try and browse available connections on a host.
4977 ****************************************************************************/
4979 static bool list_servers(const char *wk_grp)
4981 fstring state;
4983 if (!cli->server_domain)
4984 return false;
4986 if (!grepable) {
4987 d_printf("\n\tServer Comment\n");
4988 d_printf("\t--------- -------\n");
4990 fstrcpy( state, "Server" );
4991 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
4992 state);
4994 if (!grepable) {
4995 d_printf("\n\tWorkgroup Master\n");
4996 d_printf("\t--------- -------\n");
4999 fstrcpy( state, "Workgroup" );
5000 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
5001 server_fn, state);
5002 return true;
5005 /****************************************************************************
5006 Print or set current VUID
5007 ****************************************************************************/
5009 static int cmd_vuid(void)
5011 TALLOC_CTX *ctx = talloc_tos();
5012 char *buf;
5014 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5015 d_printf("Current VUID is %d\n",
5016 cli_state_get_uid(cli));
5017 return 0;
5020 cli_state_set_uid(cli, atoi(buf));
5021 return 0;
5024 /****************************************************************************
5025 Setup a new VUID, by issuing a session setup
5026 ****************************************************************************/
5028 static int cmd_logon(void)
5030 TALLOC_CTX *ctx = talloc_tos();
5031 char *l_username, *l_password;
5032 struct cli_credentials *creds = NULL;
5033 NTSTATUS nt_status;
5035 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
5036 d_printf("logon <username> [<password>]\n");
5037 return 0;
5040 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
5041 char pwd[256] = {0};
5042 int rc;
5044 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
5045 if (rc == 0) {
5046 l_password = talloc_strdup(ctx, pwd);
5049 if (!l_password) {
5050 return 1;
5053 creds = cli_session_creds_init(ctx,
5054 l_username,
5055 lp_workgroup(),
5056 NULL, /* realm */
5057 l_password,
5058 false, /* use_kerberos */
5059 false, /* fallback_after_kerberos */
5060 false, /* use_ccache */
5061 false); /* password_is_nt_hash */
5062 if (creds == NULL) {
5063 d_printf("cli_session_creds_init() failed.\n");
5064 return -1;
5066 nt_status = cli_session_setup_creds(cli, creds);
5067 TALLOC_FREE(creds);
5068 if (!NT_STATUS_IS_OK(nt_status)) {
5069 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
5070 return -1;
5073 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
5074 return 0;
5078 * close the session
5081 static int cmd_logoff(void)
5083 NTSTATUS status;
5085 status = cli_ulogoff(cli);
5086 if (!NT_STATUS_IS_OK(status)) {
5087 d_printf("logoff failed: %s\n", nt_errstr(status));
5088 return -1;
5091 d_printf("logoff successful\n");
5092 return 0;
5097 * tree connect (connect to a share)
5100 static int cmd_tcon(void)
5102 TALLOC_CTX *ctx = talloc_tos();
5103 char *sharename;
5104 NTSTATUS status;
5106 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
5107 d_printf("tcon <sharename>\n");
5108 return 0;
5111 if (!sharename) {
5112 return 1;
5115 status = cli_tree_connect(cli, sharename, "?????", NULL);
5116 if (!NT_STATUS_IS_OK(status)) {
5117 d_printf("tcon failed: %s\n", nt_errstr(status));
5118 return -1;
5121 talloc_free(sharename);
5123 d_printf("tcon to %s successful, tid: %u\n", sharename,
5124 cli_state_get_tid(cli));
5125 return 0;
5129 * tree disconnect (disconnect from a share)
5132 static int cmd_tdis(void)
5134 NTSTATUS status;
5136 status = cli_tdis(cli);
5137 if (!NT_STATUS_IS_OK(status)) {
5138 d_printf("tdis failed: %s\n", nt_errstr(status));
5139 return -1;
5142 d_printf("tdis successful\n");
5143 return 0;
5148 * get or set tid
5151 static int cmd_tid(void)
5153 TALLOC_CTX *ctx = talloc_tos();
5154 char *tid_str;
5156 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
5157 if (cli_state_has_tcon(cli)) {
5158 d_printf("current tid is %d\n", cli_state_get_tid(cli));
5159 } else {
5160 d_printf("no tcon currently\n");
5162 } else {
5163 uint32_t tid = atoi(tid_str);
5164 if (!cli_state_has_tcon(cli)) {
5165 d_printf("no tcon currently\n");
5167 cli_state_set_tid(cli, tid);
5170 return 0;
5174 /****************************************************************************
5175 list active connections
5176 ****************************************************************************/
5178 static int cmd_list_connect(void)
5180 cli_cm_display(cli);
5181 return 0;
5184 /****************************************************************************
5185 display the current active client connection
5186 ****************************************************************************/
5188 static int cmd_show_connect( void )
5190 TALLOC_CTX *ctx = talloc_tos();
5191 struct cli_state *targetcli;
5192 char *targetpath;
5193 struct cli_credentials *creds = samba_cmdline_get_creds();
5194 NTSTATUS status;
5196 status = cli_resolve_path(ctx, "",
5197 creds,
5198 cli,
5199 client_get_cur_dir(), &targetcli,
5200 &targetpath);
5201 if (!NT_STATUS_IS_OK(status)) {
5202 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
5203 return 1;
5206 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli->conn), targetcli->share);
5207 return 0;
5211 * cmd_utimes - interactive command to set the four times
5213 * Read a filename and four times from the client command line and update
5214 * the file times. A value of -1 for a time means don't change.
5216 static int cmd_utimes(void)
5218 char *buf;
5219 char *fname = NULL;
5220 struct timespec times[4] = {{0}};
5221 struct timeval_buf tbuf[4];
5222 int time_count = 0;
5223 int err = 0;
5224 bool ok;
5225 TALLOC_CTX *ctx = talloc_new(NULL);
5226 NTSTATUS status;
5228 if (ctx == NULL) {
5229 return 1;
5232 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5233 if (!ok) {
5234 d_printf("utimes <filename> <create-time> <access-time> "
5235 "<write-time> <change-time>\n");
5236 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5237 "or -1 for no change\n");
5238 err = 1;
5239 goto out;
5242 fname = talloc_asprintf(ctx,
5243 "%s%s",
5244 client_get_cur_dir(),
5245 buf);
5246 if (fname == NULL) {
5247 err = 1;
5248 goto out;
5250 fname = client_clean_name(ctx, fname);
5251 if (fname == NULL) {
5252 err = 1;
5253 goto out;
5256 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
5257 time_count < 4) {
5258 const char *s = buf;
5259 struct tm tm = {0,};
5260 time_t t;
5261 char *ret;
5263 if (strlen(s) == 2 && strcmp(s, "-1") == 0) {
5264 times[time_count] = make_omit_timespec();
5265 time_count++;
5266 continue;
5269 ret = strptime(s, "%y:%m:%d-%H:%M:%S", &tm);
5271 if (ret == NULL) {
5272 ret = strptime(s, "%Y:%m:%d-%H:%M:%S", &tm);
5275 /* We could not match all the chars, so print error */
5276 if (ret == NULL || *ret != 0) {
5277 d_printf("Invalid date format: %s\n", s);
5278 d_printf("utimes <filename> <create-time> "
5279 "<access-time> <write-time> <change-time>\n");
5280 d_printf("Dates should be in [YY]YY:MM:DD-HH:MM:SS "
5281 "format or -1 for no change\n");
5282 err = 1;
5283 goto out;
5286 /* Convert tm to a time_t */
5287 t = mktime(&tm);
5288 times[time_count] = (struct timespec){.tv_sec = t};
5289 time_count++;
5292 if (time_count < 4) {
5293 d_printf("Insufficient dates: %d\n", time_count);
5294 d_printf("utimes <filename> <create-time> <access-time> "
5295 "<write-time> <change-time>\n");
5296 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5297 "or -1 for no change\n");
5298 err = 1;
5299 goto out;
5302 DEBUG(10, ("times\nCreate: %sAccess: %s Write: %sChange: %s\n",
5303 timespec_string_buf(&times[0], false, &tbuf[0]),
5304 timespec_string_buf(&times[1], false, &tbuf[1]),
5305 timespec_string_buf(&times[2], false, &tbuf[2]),
5306 timespec_string_buf(&times[3], false, &tbuf[3])));
5308 status = cli_setpathinfo_ext(
5309 cli, fname, times[0], times[1], times[2], times[3],
5310 (uint32_t)-1);
5311 if (!NT_STATUS_IS_OK(status)) {
5312 d_printf("cli_setpathinfo_ext failed: %s\n",
5313 nt_errstr(status));
5314 err = 1;
5315 goto out;
5317 out:
5318 talloc_free(ctx);
5319 return err;
5323 * set_remote_attr - set DOS attributes of a remote file
5324 * @filename: path to the file name
5325 * @new_attr: attribute bit mask to use
5326 * @mode: one of ATTR_SET or ATTR_UNSET
5328 * Update the file attributes with the one provided.
5330 int set_remote_attr(const char *filename, uint32_t new_attr, int mode)
5332 extern struct cli_state *cli;
5333 uint32_t old_attr;
5334 NTSTATUS status;
5336 status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
5337 if (!NT_STATUS_IS_OK(status)) {
5338 d_printf("cli_getatr failed: %s\n", nt_errstr(status));
5339 return 1;
5342 if (mode == ATTR_SET) {
5343 new_attr |= old_attr;
5344 } else {
5345 new_attr = old_attr & ~new_attr;
5348 status = cli_setatr(cli, filename, new_attr, 0);
5349 if (!NT_STATUS_IS_OK(status)) {
5350 d_printf("cli_setatr failed: %s\n", nt_errstr(status));
5351 return 1;
5354 return 0;
5358 * cmd_setmode - interactive command to set DOS attributes
5360 * Read a filename and mode from the client command line and update
5361 * the file DOS attributes.
5363 int cmd_setmode(void)
5365 char *buf;
5366 char *fname = NULL;
5367 uint32_t attr[2] = {0};
5368 int mode = ATTR_SET;
5369 int err = 0;
5370 bool ok;
5371 TALLOC_CTX *ctx = talloc_new(NULL);
5372 if (ctx == NULL) {
5373 return 1;
5376 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5377 if (!ok) {
5378 d_printf("setmode <filename> <[+|-]rsha>\n");
5379 err = 1;
5380 goto out;
5383 fname = talloc_asprintf(ctx,
5384 "%s%s",
5385 client_get_cur_dir(),
5386 buf);
5387 if (fname == NULL) {
5388 err = 1;
5389 goto out;
5391 fname = client_clean_name(ctx, fname);
5392 if (fname == NULL) {
5393 err = 1;
5394 goto out;
5397 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
5398 const char *s = buf;
5400 while (*s) {
5401 switch (*s++) {
5402 case '+':
5403 mode = ATTR_SET;
5404 break;
5405 case '-':
5406 mode = ATTR_UNSET;
5407 break;
5408 case 'r':
5409 attr[mode] |= FILE_ATTRIBUTE_READONLY;
5410 break;
5411 case 'h':
5412 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
5413 break;
5414 case 's':
5415 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
5416 break;
5417 case 'a':
5418 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
5419 break;
5420 default:
5421 d_printf("setmode <filename> <perm=[+|-]rsha>\n");
5422 err = 1;
5423 goto out;
5428 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
5429 d_printf("setmode <filename> <[+|-]rsha>\n");
5430 err = 1;
5431 goto out;
5434 DEBUG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
5436 /* ignore return value: server might not store DOS attributes */
5437 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
5438 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
5439 out:
5440 talloc_free(ctx);
5441 return err;
5444 /****************************************************************************
5445 iosize command
5446 ***************************************************************************/
5448 int cmd_iosize(void)
5450 TALLOC_CTX *ctx = talloc_tos();
5451 char *buf;
5452 int iosize;
5453 struct cli_credentials *creds = samba_cmdline_get_creds();
5454 bool smb_encrypt =
5455 (cli_credentials_get_smb_encryption(creds) ==
5456 SMB_ENCRYPTION_REQUIRED);
5458 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5459 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5460 if (!smb_encrypt) {
5461 d_printf("iosize <n> or iosize 0x<n>. "
5462 "Minimum is 0 (default), "
5463 "max is 16776960 (0xFFFF00)\n");
5464 } else {
5465 d_printf("iosize <n> or iosize 0x<n>. "
5466 "(Encrypted connection) ,"
5467 "Minimum is 0 (default), "
5468 "max is 130048 (0x1FC00)\n");
5470 } else {
5471 d_printf("iosize <n> or iosize 0x<n>.\n");
5473 return 1;
5476 iosize = strtol(buf,NULL,0);
5477 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5478 if (smb_encrypt && (iosize < 0 || iosize > 0xFC00)) {
5479 d_printf("iosize out of range for encrypted "
5480 "connection (min = 0 (default), "
5481 "max = 130048 (0x1FC00)\n");
5482 return 1;
5483 } else if (!smb_encrypt && (iosize < 0 || iosize > 0xFFFF00)) {
5484 d_printf("iosize out of range (min = 0 (default), "
5485 "max = 16776960 (0xFFFF00)\n");
5486 return 1;
5490 io_bufsize = iosize;
5491 d_printf("iosize is now %d\n", io_bufsize);
5492 return 0;
5495 /****************************************************************************
5496 timeout command
5497 ***************************************************************************/
5499 static int cmd_timeout(void)
5501 TALLOC_CTX *ctx = talloc_tos();
5502 char *buf;
5504 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5505 unsigned int old_timeout = cli_set_timeout(cli, 0);
5506 cli_set_timeout(cli, old_timeout);
5507 d_printf("timeout <n> (per-operation timeout "
5508 "in seconds - currently %u).\n",
5509 old_timeout/1000);
5510 return 1;
5513 io_timeout = strtol(buf,NULL,0);
5514 cli_set_timeout(cli, io_timeout*1000);
5515 d_printf("io_timeout per operation is now %d\n", io_timeout);
5516 return 0;
5520 /****************************************************************************
5521 history
5522 ****************************************************************************/
5523 static int cmd_history(void)
5525 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
5526 HIST_ENTRY **hlist;
5527 int i;
5529 hlist = history_list();
5531 for (i = 0; hlist && hlist[i]; i++) {
5532 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
5534 #else
5535 DEBUG(0,("no history without readline support\n"));
5536 #endif
5538 return 0;
5541 /* Some constants for completing filename arguments */
5543 #define COMPL_NONE 0 /* No completions */
5544 #define COMPL_REMOTE 1 /* Complete remote filename */
5545 #define COMPL_LOCAL 2 /* Complete local filename */
5547 /* This defines the commands supported by this client.
5548 * NOTE: The "!" must be the last one in the list because it's fn pointer
5549 * field is NULL, and NULL in that field is used in process_tok()
5550 * (below) to indicate the end of the list. crh
5552 static struct {
5553 const char *name;
5554 int (*fn)(void);
5555 const char *description;
5556 char compl_args[2]; /* Completion argument info */
5557 } commands[] = {
5558 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5559 {"allinfo",cmd_allinfo,"<file> show all available info",
5560 {COMPL_REMOTE,COMPL_NONE}},
5561 {"altname",cmd_altname,"<file> show alt name",{COMPL_REMOTE,COMPL_NONE}},
5562 {"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}},
5563 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
5564 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
5565 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
5566 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
5567 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
5568 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_NONE}},
5569 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_NONE}},
5570 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_NONE}},
5571 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5572 {"deltree",cmd_deltree,"<mask> recursively delete all matching files and directories",{COMPL_REMOTE,COMPL_NONE}},
5573 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5574 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5575 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
5576 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5577 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
5578 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_NONE}},
5579 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
5580 {COMPL_REMOTE, COMPL_NONE}},
5581 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5582 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5583 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
5584 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
5585 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
5586 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5587 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5588 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
5589 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5590 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5591 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
5592 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5593 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
5594 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5595 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
5596 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
5597 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
5598 {"notify",cmd_notify,"<file>Get notified of dir changes",{COMPL_REMOTE,COMPL_NONE}},
5599 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
5600 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
5601 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
5602 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5603 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5604 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5605 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5606 {"posix_whoami",cmd_posix_whoami,"return logged on user information "
5607 "using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5608 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
5609 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
5610 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
5611 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
5612 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5613 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
5614 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5615 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5616 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
5617 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
5618 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
5619 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
5620 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
5621 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5622 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_REMOTE,COMPL_NONE}},
5623 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
5624 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
5625 {COMPL_REMOTE, COMPL_LOCAL}},
5626 {"setmode",cmd_setmode,"<file name> <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
5627 {"scopy",cmd_scopy,"<src> <dest> server-side copy file",{COMPL_REMOTE,COMPL_REMOTE}},
5628 {"stat",cmd_stat,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_NONE}},
5629 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5630 {"tar",cmd_tar,"tar <c|x>[IXFvbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
5631 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
5632 {"timeout",cmd_timeout,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE,COMPL_NONE}},
5633 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
5634 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5635 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
5636 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
5637 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5638 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
5639 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
5640 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
5641 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
5642 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
5643 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
5644 {"utimes", cmd_utimes,"<file name> <create_time> <access_time> <mod_time> "
5645 "<ctime> set times", {COMPL_REMOTE,COMPL_NONE}},
5646 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
5647 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
5649 /* Yes, this must be here, see crh's comment above. */
5650 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
5651 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
5654 /*******************************************************************
5655 Lookup a command string in the list of commands, including
5656 abbreviations.
5657 ******************************************************************/
5659 static int process_tok(char *tok)
5661 size_t i = 0, matches = 0;
5662 size_t cmd=0;
5663 size_t tok_len = strlen(tok);
5665 while (commands[i].fn != NULL) {
5666 if (strequal(commands[i].name,tok)) {
5667 matches = 1;
5668 cmd = i;
5669 break;
5670 } else if (strnequal(commands[i].name, tok, tok_len)) {
5671 matches++;
5672 cmd = i;
5674 i++;
5677 if (matches == 0)
5678 return(-1);
5679 else if (matches == 1)
5680 return(cmd);
5681 else
5682 return(-2);
5685 /****************************************************************************
5686 Help.
5687 ****************************************************************************/
5689 static int cmd_help(void)
5691 TALLOC_CTX *ctx = talloc_tos();
5692 int i=0,j;
5693 char *buf;
5695 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5696 if ((i = process_tok(buf)) >= 0)
5697 d_printf("HELP %s:\n\t%s\n\n",
5698 commands[i].name,commands[i].description);
5699 } else {
5700 while (commands[i].description) {
5701 for (j=0; commands[i].description && (j<5); j++) {
5702 d_printf("%-15s",commands[i].name);
5703 i++;
5705 d_printf("\n");
5708 return 0;
5711 /****************************************************************************
5712 Process a -c command string.
5713 ****************************************************************************/
5715 static int process_command_string(const char *cmd_in)
5717 TALLOC_CTX *ctx = talloc_tos();
5718 char *cmd = talloc_strdup(ctx, cmd_in);
5719 int rc = 0;
5720 struct cli_credentials *creds = samba_cmdline_get_creds();
5722 if (!cmd) {
5723 return 1;
5725 /* establish the connection if not already */
5727 if (!cli) {
5728 NTSTATUS status;
5730 status = cli_cm_open(talloc_tos(), NULL,
5731 desthost,
5732 service,
5733 creds,
5734 have_ip ? &dest_ss : NULL, port,
5735 name_type,
5736 &cli);
5737 if (!NT_STATUS_IS_OK(status)) {
5738 return 1;
5740 cli_set_timeout(cli, io_timeout*1000);
5743 while (cmd[0] != '\0') {
5744 char *line;
5745 char *p;
5746 char *tok;
5747 int i;
5749 if ((p = strchr_m(cmd, ';')) == 0) {
5750 line = cmd;
5751 cmd += strlen(cmd);
5752 } else {
5753 *p = '\0';
5754 line = cmd;
5755 cmd = p + 1;
5758 /* and get the first part of the command */
5759 cmd_ptr = line;
5760 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
5761 continue;
5764 if ((i = process_tok(tok)) >= 0) {
5765 rc = commands[i].fn();
5766 } else if (i == -2) {
5767 d_printf("%s: command abbreviation ambiguous\n",tok);
5768 } else {
5769 d_printf("%s: command not found\n",tok);
5773 return rc;
5776 #define MAX_COMPLETIONS 100
5778 struct completion_remote {
5779 char *dirmask;
5780 char **matches;
5781 int count, samelen;
5782 const char *text;
5783 int len;
5786 static NTSTATUS completion_remote_filter(struct file_info *f,
5787 const char *mask,
5788 void *state)
5790 struct completion_remote *info = (struct completion_remote *)state;
5792 if (info->count >= MAX_COMPLETIONS - 1) {
5793 return NT_STATUS_OK;
5795 if (strncmp(info->text, f->name, info->len) != 0) {
5796 return NT_STATUS_OK;
5798 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
5799 return NT_STATUS_OK;
5802 if ((info->dirmask[0] == 0) && !(f->attr & FILE_ATTRIBUTE_DIRECTORY))
5803 info->matches[info->count] = SMB_STRDUP(f->name);
5804 else {
5805 TALLOC_CTX *ctx = talloc_stackframe();
5806 char *tmp;
5808 tmp = talloc_strdup(ctx,info->dirmask);
5809 if (!tmp) {
5810 TALLOC_FREE(ctx);
5811 return NT_STATUS_NO_MEMORY;
5813 tmp = talloc_asprintf_append(tmp, "%s", f->name);
5814 if (!tmp) {
5815 TALLOC_FREE(ctx);
5816 return NT_STATUS_NO_MEMORY;
5818 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5819 tmp = talloc_asprintf_append(tmp, "%s",
5820 CLI_DIRSEP_STR);
5822 if (!tmp) {
5823 TALLOC_FREE(ctx);
5824 return NT_STATUS_NO_MEMORY;
5826 info->matches[info->count] = SMB_STRDUP(tmp);
5827 TALLOC_FREE(ctx);
5829 if (info->matches[info->count] == NULL) {
5830 return NT_STATUS_OK;
5832 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5833 smb_readline_ca_char(0);
5835 if (info->count == 1) {
5836 info->samelen = strlen(info->matches[info->count]);
5837 } else {
5838 while (strncmp(info->matches[info->count],
5839 info->matches[info->count-1],
5840 info->samelen) != 0) {
5841 info->samelen--;
5844 info->count++;
5845 return NT_STATUS_OK;
5848 static char **remote_completion(const char *text, int len)
5850 TALLOC_CTX *ctx = talloc_stackframe();
5851 char *dirmask = NULL;
5852 char *targetpath = NULL;
5853 struct cli_state *targetcli = NULL;
5854 int i;
5855 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
5856 struct cli_credentials *creds = samba_cmdline_get_creds();
5857 NTSTATUS status;
5859 /* can't have non-static initialisation on Sun CC, so do it
5860 at run time here */
5861 info.samelen = len;
5862 info.text = text;
5863 info.len = len;
5865 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
5866 if (!info.matches) {
5867 TALLOC_FREE(ctx);
5868 return NULL;
5872 * We're leaving matches[0] free to fill it later with the text to
5873 * display: Either the one single match or the longest common subset
5874 * of the matches.
5876 info.matches[0] = NULL;
5877 info.count = 1;
5879 for (i = len-1; i >= 0; i--) {
5880 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
5881 break;
5885 info.text = text+i+1;
5886 info.samelen = info.len = len-i-1;
5888 if (i > 0) {
5889 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
5890 if (!info.dirmask) {
5891 goto cleanup;
5893 strncpy(info.dirmask, text, i+1);
5894 info.dirmask[i+1] = 0;
5895 dirmask = talloc_asprintf(ctx,
5896 "%s%*s*",
5897 client_get_cur_dir(),
5898 i-1,
5899 text);
5900 } else {
5901 info.dirmask = SMB_STRDUP("");
5902 if (!info.dirmask) {
5903 goto cleanup;
5905 dirmask = talloc_asprintf(ctx,
5906 "%s*",
5907 client_get_cur_dir());
5909 if (!dirmask) {
5910 goto cleanup;
5912 dirmask = client_clean_name(ctx, dirmask);
5913 if (dirmask == NULL) {
5914 goto cleanup;
5917 status = cli_resolve_path(ctx, "",
5918 creds,
5919 cli, dirmask, &targetcli, &targetpath);
5920 if (!NT_STATUS_IS_OK(status)) {
5921 goto cleanup;
5923 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
5924 completion_remote_filter, (void *)&info);
5925 if (!NT_STATUS_IS_OK(status)) {
5926 goto cleanup;
5929 if (info.count == 1) {
5931 * No matches at all, NULL indicates there is nothing
5933 SAFE_FREE(info.matches[0]);
5934 SAFE_FREE(info.matches);
5935 TALLOC_FREE(ctx);
5936 return NULL;
5939 if (info.count == 2) {
5941 * Exactly one match in matches[1], indicate this is the one
5942 * in matches[0].
5944 info.matches[0] = info.matches[1];
5945 info.matches[1] = NULL;
5946 info.count -= 1;
5947 TALLOC_FREE(ctx);
5948 return info.matches;
5952 * We got more than one possible match, set the result to the maximum
5953 * common subset
5956 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
5957 info.matches[info.count] = NULL;
5958 TALLOC_FREE(ctx);
5959 return info.matches;
5961 cleanup:
5962 for (i = 0; i < info.count; i++) {
5963 SAFE_FREE(info.matches[i]);
5965 SAFE_FREE(info.matches);
5966 SAFE_FREE(info.dirmask);
5967 TALLOC_FREE(ctx);
5968 return NULL;
5971 static char **completion_fn(const char *text, int start, int end)
5973 smb_readline_ca_char(' ');
5975 if (start) {
5976 const char *buf, *sp;
5977 int i;
5978 char compl_type;
5980 buf = smb_readline_get_line_buffer();
5981 if (buf == NULL)
5982 return NULL;
5984 sp = strchr(buf, ' ');
5985 if (sp == NULL)
5986 return NULL;
5988 for (i = 0; commands[i].name; i++) {
5989 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
5990 (commands[i].name[sp - buf] == 0)) {
5991 break;
5994 if (commands[i].name == NULL)
5995 return NULL;
5997 while (*sp == ' ')
5998 sp++;
6000 if (sp == (buf + start))
6001 compl_type = commands[i].compl_args[0];
6002 else
6003 compl_type = commands[i].compl_args[1];
6005 if (compl_type == COMPL_REMOTE)
6006 return remote_completion(text, end - start);
6007 else /* fall back to local filename completion */
6008 return NULL;
6009 } else {
6010 char **matches;
6011 size_t i, len, samelen = 0, count=1;
6013 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
6014 if (!matches) {
6015 return NULL;
6017 matches[0] = NULL;
6019 len = strlen(text);
6020 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
6021 if (strncmp(text, commands[i].name, len) == 0) {
6022 matches[count] = SMB_STRDUP(commands[i].name);
6023 if (!matches[count])
6024 goto cleanup;
6025 if (count == 1)
6026 samelen = strlen(matches[count]);
6027 else
6028 while (strncmp(matches[count], matches[count-1], samelen) != 0)
6029 samelen--;
6030 count++;
6034 switch (count) {
6035 case 0: /* should never happen */
6036 case 1:
6037 goto cleanup;
6038 case 2:
6039 matches[0] = SMB_STRDUP(matches[1]);
6040 break;
6041 default:
6042 matches[0] = (char *)SMB_MALLOC(samelen+1);
6043 if (!matches[0])
6044 goto cleanup;
6045 strncpy(matches[0], matches[1], samelen);
6046 matches[0][samelen] = 0;
6048 matches[count] = NULL;
6049 return matches;
6051 cleanup:
6052 for (i = 0; i < count; i++)
6053 free(matches[i]);
6055 free(matches);
6056 return NULL;
6060 static bool finished;
6062 /****************************************************************************
6063 Make sure we swallow keepalives during idle time.
6064 ****************************************************************************/
6066 static void readline_callback(void)
6068 static time_t last_t;
6069 struct timespec now;
6070 time_t t;
6071 NTSTATUS status;
6072 unsigned char garbage[16];
6074 clock_gettime_mono(&now);
6075 t = now.tv_sec;
6077 if (t - last_t < 5)
6078 return;
6080 last_t = t;
6082 /* Ping the server to keep the connection alive using SMBecho. */
6083 memset(garbage, 0xf0, sizeof(garbage));
6084 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
6085 if (NT_STATUS_IS_OK(status) ||
6086 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
6088 * Even if server returns NT_STATUS_INVALID_PARAMETER
6089 * it still responded.
6090 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
6092 return;
6095 if (!cli_state_is_connected(cli)) {
6096 DEBUG(0,("SMBecho failed (%s). The connection is "
6097 "disconnected now\n", nt_errstr(status)));
6098 finished = true;
6099 smb_readline_done();
6103 /****************************************************************************
6104 Process commands on stdin.
6105 ****************************************************************************/
6107 static int process_stdin(void)
6109 int rc = 0;
6111 if (!quiet) {
6112 d_printf("Try \"help\" to get a list of possible commands.\n");
6115 while (!finished) {
6116 TALLOC_CTX *frame = talloc_stackframe();
6117 char *tok = NULL;
6118 char *the_prompt = NULL;
6119 char *line = NULL;
6120 int i;
6122 /* display a prompt */
6123 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
6124 TALLOC_FREE(frame);
6125 break;
6127 line = smb_readline(the_prompt, readline_callback, completion_fn);
6128 SAFE_FREE(the_prompt);
6129 if (!line) {
6130 TALLOC_FREE(frame);
6131 break;
6134 /* special case - first char is ! */
6135 if (*line == '!') {
6136 if (system(line + 1) == -1) {
6137 d_printf("system() command %s failed.\n",
6138 line+1);
6140 SAFE_FREE(line);
6141 TALLOC_FREE(frame);
6142 continue;
6145 /* and get the first part of the command */
6146 cmd_ptr = line;
6147 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
6148 TALLOC_FREE(frame);
6149 SAFE_FREE(line);
6150 continue;
6153 if ((i = process_tok(tok)) >= 0) {
6154 rc = commands[i].fn();
6155 } else if (i == -2) {
6156 d_printf("%s: command abbreviation ambiguous\n",tok);
6157 } else {
6158 d_printf("%s: command not found\n",tok);
6160 SAFE_FREE(line);
6161 TALLOC_FREE(frame);
6163 return rc;
6166 /****************************************************************************
6167 Process commands from the client.
6168 ****************************************************************************/
6170 static int process(const char *base_directory)
6172 int rc = 0;
6173 NTSTATUS status;
6174 struct cli_credentials *creds = samba_cmdline_get_creds();
6176 status = cli_cm_open(talloc_tos(), NULL,
6177 desthost,
6178 service,
6179 creds,
6180 have_ip ? &dest_ss : NULL, port,
6181 name_type, &cli);
6182 if (!NT_STATUS_IS_OK(status)) {
6183 return 1;
6186 cli_set_timeout(cli, io_timeout*1000);
6188 if (base_directory && *base_directory) {
6189 rc = do_cd(base_directory);
6190 if (rc) {
6191 cli_shutdown(cli);
6192 return rc;
6196 if (cmdstr) {
6197 rc = process_command_string(cmdstr);
6198 } else {
6199 process_stdin();
6202 cli_shutdown(cli);
6203 return rc;
6206 /****************************************************************************
6207 Handle a -L query.
6208 ****************************************************************************/
6210 static int do_host_query(const char *query_host)
6212 NTSTATUS status;
6213 struct cli_credentials *creds = samba_cmdline_get_creds();
6215 status = cli_cm_open(talloc_tos(), NULL,
6216 query_host,
6217 "IPC$",
6218 creds,
6219 have_ip ? &dest_ss : NULL, port,
6220 name_type, &cli);
6221 if (!NT_STATUS_IS_OK(status)) {
6222 return 1;
6225 cli_set_timeout(cli, io_timeout*1000);
6226 browse_host(true);
6228 /* Ensure that the host can do IPv4 */
6230 if (!interpret_addr(query_host)) {
6231 struct sockaddr_storage ss;
6232 if (interpret_string_addr(&ss, query_host, 0) &&
6233 (ss.ss_family != AF_INET)) {
6234 d_printf("%s is an IPv6 address -- no workgroup available\n",
6235 query_host);
6236 return 1;
6240 if (lp_client_min_protocol() > PROTOCOL_NT1) {
6241 d_printf("SMB1 disabled -- no workgroup available\n");
6242 goto out;
6245 if (lp_disable_netbios()) {
6246 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
6247 goto out;
6250 if (port != NBT_SMB_PORT ||
6251 smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1)
6254 * Workgroups simply don't make sense over anything
6255 * else but port 139 and SMB1.
6258 cli_shutdown(cli);
6259 d_printf("Reconnecting with SMB1 for workgroup listing.\n");
6260 lp_set_cmdline("client max protocol", "NT1");
6261 status = cli_cm_open(talloc_tos(), NULL,
6262 query_host,
6263 "IPC$",
6264 creds,
6265 have_ip ? &dest_ss : NULL, NBT_SMB_PORT,
6266 name_type, &cli);
6267 if (!NT_STATUS_IS_OK(status)) {
6268 d_printf("Unable to connect with SMB1 "
6269 "-- no workgroup available\n");
6270 return 0;
6274 cli_set_timeout(cli, io_timeout*1000);
6275 list_servers(lp_workgroup());
6276 out:
6277 cli_shutdown(cli);
6279 return(0);
6282 /****************************************************************************
6283 Handle a tar operation.
6284 ****************************************************************************/
6286 static int do_tar_op(const char *base_directory)
6288 struct tar *tar_ctx = tar_get_ctx();
6289 int ret = 0;
6290 struct cli_credentials *creds = samba_cmdline_get_creds();
6292 /* do we already have a connection? */
6293 if (!cli) {
6294 NTSTATUS status;
6296 status = cli_cm_open(talloc_tos(), NULL,
6297 desthost,
6298 service,
6299 creds,
6300 have_ip ? &dest_ss : NULL, port,
6301 name_type, &cli);
6302 if (!NT_STATUS_IS_OK(status)) {
6303 ret = 1;
6304 goto out;
6306 cli_set_timeout(cli, io_timeout*1000);
6309 recurse = true;
6311 if (base_directory && *base_directory) {
6312 ret = do_cd(base_directory);
6313 if (ret) {
6314 goto out_cli;
6318 ret = tar_process(tar_ctx);
6320 out_cli:
6321 cli_shutdown(cli);
6322 out:
6323 return ret;
6326 /****************************************************************************
6327 Handle a message operation.
6328 ****************************************************************************/
6330 static int do_message_op(struct cli_credentials *creds)
6332 NTSTATUS status;
6334 if (lp_disable_netbios()) {
6335 d_printf("NetBIOS over TCP disabled.\n");
6336 return 1;
6339 status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
6340 port ? port : NBT_SMB_PORT, name_type,
6341 lp_netbios_name(),
6342 SMB_SIGNING_OFF,
6344 &cli);
6345 if (!NT_STATUS_IS_OK(status)) {
6346 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
6347 return 1;
6350 cli_set_timeout(cli, io_timeout*1000);
6351 send_message(cli_credentials_get_username(creds));
6352 cli_shutdown(cli);
6354 return 0;
6357 /****************************************************************************
6358 main program
6359 ****************************************************************************/
6361 int main(int argc,char *argv[])
6363 const char **const_argv = discard_const_p(const char *, argv);
6364 char *base_directory = NULL;
6365 int opt;
6366 char *query_host = NULL;
6367 bool message = false;
6368 static const char *new_name_resolve_order = NULL;
6369 poptContext pc;
6370 char *p;
6371 int rc = 0;
6372 bool tar_opt = false;
6373 bool service_opt = false;
6374 struct tar *tar_ctx = tar_get_ctx();
6375 bool ok;
6377 struct poptOption long_options[] = {
6378 POPT_AUTOHELP
6381 .longName = "message",
6382 .shortName = 'M',
6383 .argInfo = POPT_ARG_STRING,
6384 .arg = NULL,
6385 .val = 'M',
6386 .descrip = "Send message",
6387 .argDescrip = "HOST",
6390 .longName = "ip-address",
6391 .shortName = 'I',
6392 .argInfo = POPT_ARG_STRING,
6393 .arg = NULL,
6394 .val = 'I',
6395 .descrip = "Use this IP to connect to",
6396 .argDescrip = "IP",
6399 .longName = "stderr",
6400 .shortName = 'E',
6401 .argInfo = POPT_ARG_NONE,
6402 .arg = NULL,
6403 .val = 'E',
6404 .descrip = "Write messages to stderr instead of stdout",
6407 .longName = "list",
6408 .shortName = 'L',
6409 .argInfo = POPT_ARG_STRING,
6410 .arg = NULL,
6411 .val = 'L',
6412 .descrip = "Get a list of shares available on a host",
6413 .argDescrip = "HOST",
6416 .longName = "tar",
6417 .shortName = 'T',
6418 .argInfo = POPT_ARG_STRING,
6419 .arg = NULL,
6420 .val = 'T',
6421 .descrip = "Command line tar",
6422 .argDescrip = "<c|x>IXFvgbNan",
6425 .longName = "directory",
6426 .shortName = 'D',
6427 .argInfo = POPT_ARG_STRING,
6428 .arg = NULL,
6429 .val = 'D',
6430 .descrip = "Start from directory",
6431 .argDescrip = "DIR",
6434 .longName = "command",
6435 .shortName = 'c',
6436 .argInfo = POPT_ARG_STRING,
6437 .arg = &cmdstr,
6438 .val = 'c',
6439 .descrip = "Execute semicolon separated commands",
6442 .longName = "send-buffer",
6443 .shortName = 'b',
6444 .argInfo = POPT_ARG_INT,
6445 .arg = &io_bufsize,
6446 .val = 'b',
6447 .descrip = "Changes the transmit/send buffer",
6448 .argDescrip = "BYTES",
6451 .longName = "timeout",
6452 .shortName = 't',
6453 .argInfo = POPT_ARG_INT,
6454 .arg = &io_timeout,
6455 .val = 'b',
6456 .descrip = "Changes the per-operation timeout",
6457 .argDescrip = "SECONDS",
6460 .longName = "port",
6461 .shortName = 'p',
6462 .argInfo = POPT_ARG_INT,
6463 .arg = &port,
6464 .val = 'p',
6465 .descrip = "Port to connect to",
6466 .argDescrip = "PORT",
6469 .longName = "grepable",
6470 .shortName = 'g',
6471 .argInfo = POPT_ARG_NONE,
6472 .arg = NULL,
6473 .val = 'g',
6474 .descrip = "Produce grepable output",
6477 .longName = "quiet",
6478 .shortName = 'q',
6479 .argInfo = POPT_ARG_NONE,
6480 .arg = NULL,
6481 .val = 'q',
6482 .descrip = "Suppress help message",
6485 .longName = "browse",
6486 .shortName = 'B',
6487 .argInfo = POPT_ARG_NONE,
6488 .arg = NULL,
6489 .val = 'B',
6490 .descrip = "Browse SMB servers using DNS",
6492 POPT_COMMON_SAMBA
6493 POPT_COMMON_CONNECTION
6494 POPT_COMMON_CREDENTIALS
6495 POPT_LEGACY_S3
6496 POPT_COMMON_VERSION
6497 POPT_TABLEEND
6499 TALLOC_CTX *frame = talloc_stackframe();
6500 struct cli_credentials *creds = NULL;
6502 if (!client_set_cur_dir("\\")) {
6503 exit(ENOMEM);
6506 smb_init_locale();
6508 ok = samba_cmdline_init(frame,
6509 SAMBA_CMDLINE_CONFIG_CLIENT,
6510 false /* require_smbconf */);
6511 if (!ok) {
6512 DBG_ERR("Failed to init cmdline parser!\n");
6513 exit(ENOMEM);
6515 lp_set_cmdline("log level", "1");
6517 /* skip argv(0) */
6518 pc = samba_popt_get_context(getprogname(),
6519 argc,
6520 const_argv,
6521 long_options,
6523 if (pc == NULL) {
6524 DBG_ERR("Failed to setup popt context!\n");
6525 exit(1);
6528 poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
6530 creds = samba_cmdline_get_creds();
6531 while ((opt = poptGetNextOpt(pc)) != -1) {
6534 * if the tar option has been called previously, now
6535 * we need to eat out the leftovers
6537 /* I see no other way to keep things sane --SSS */
6538 if (tar_opt == true) {
6539 while (poptPeekArg(pc)) {
6540 poptGetArg(pc);
6542 tar_opt = false;
6545 /* if the service has not yet been specified lets see if it is available in the popt stack */
6546 if (!service_opt && poptPeekArg(pc)) {
6547 service = talloc_strdup(frame, poptGetArg(pc));
6548 if (!service) {
6549 exit(ENOMEM);
6551 service_opt = true;
6554 /* if the service has already been retrieved then check if we have also a password */
6555 if (service_opt &&
6556 cli_credentials_get_password(creds) == NULL &&
6557 poptPeekArg(pc)) {
6558 cli_credentials_set_password(creds,
6559 poptGetArg(pc),
6560 CRED_SPECIFIED);
6564 switch (opt) {
6565 case 'M':
6566 /* Messages are sent to NetBIOS name type 0x3
6567 * (Messenger Service). Make sure we default
6568 * to port 139 instead of port 445. srl,crh
6570 name_type = 0x03;
6571 desthost = talloc_strdup(frame,poptGetOptArg(pc));
6572 if (!desthost) {
6573 exit(ENOMEM);
6575 if( !port )
6576 port = NBT_SMB_PORT;
6577 message = true;
6578 break;
6579 case 'I':
6581 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
6582 exit(1);
6584 have_ip = true;
6585 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
6587 break;
6588 case 'E':
6589 setup_logging("smbclient", DEBUG_STDERR );
6590 display_set_stderr();
6591 break;
6593 case 'L':
6594 query_host = talloc_strdup(frame, poptGetOptArg(pc));
6595 if (!query_host) {
6596 exit(ENOMEM);
6598 break;
6599 case 'T':
6600 /* We must use old option processing for this. Find the
6601 * position of the -T option in the raw argv[]. */
6603 int i;
6605 for (i = 1; i < argc; i++) {
6606 if (strncmp("-T", argv[i],2)==0)
6607 break;
6609 i++;
6610 if (tar_parse_args(tar_ctx, poptGetOptArg(pc),
6611 const_argv + i, argc - i)) {
6612 poptPrintUsage(pc, stderr, 0);
6613 exit(1);
6616 /* this must be the last option, mark we have parsed it so that we know we have */
6617 tar_opt = true;
6618 break;
6619 case 'D':
6620 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
6621 if (!base_directory) {
6622 exit(ENOMEM);
6624 break;
6625 case 'g':
6626 grepable=true;
6627 break;
6628 case 'q':
6629 quiet=true;
6630 break;
6631 case 'B':
6632 return(do_smb_browse());
6633 case POPT_ERROR_BADOPT:
6634 fprintf(stderr, "\nInvalid option %s: %s\n\n",
6635 poptBadOption(pc, 0), poptStrerror(opt));
6636 poptPrintUsage(pc, stderr, 0);
6637 exit(1);
6641 /* We may still have some leftovers after the last popt option has been called */
6642 if (tar_opt == true) {
6643 while (poptPeekArg(pc)) {
6644 poptGetArg(pc);
6646 tar_opt = false;
6649 /* if the service has not yet been specified lets see if it is available in the popt stack */
6650 if (!service_opt && poptPeekArg(pc)) {
6651 service = talloc_strdup(frame,poptGetArg(pc));
6652 if (!service) {
6653 exit(ENOMEM);
6655 service_opt = true;
6658 /* if the service has already been retrieved then check if we have also a password */
6659 if (service_opt &&
6660 cli_credentials_get_password(creds) == NULL &&
6661 poptPeekArg(pc)) {
6662 cli_credentials_set_password(creds,
6663 poptGetArg(pc),
6664 CRED_SPECIFIED);
6667 if (service_opt && service) {
6668 size_t len;
6670 /* Convert any '/' characters in the service name to '\' characters */
6671 string_replace(service, '/','\\');
6672 if (count_chars(service,'\\') < 3) {
6673 d_printf("\n%s: Not enough '\\' characters in service\n",service);
6674 poptPrintUsage(pc, stderr, 0);
6675 exit(1);
6677 /* Remove trailing slashes */
6678 len = strlen(service);
6679 while(len > 0 && service[len - 1] == '\\') {
6680 --len;
6681 service[len] = '\0';
6685 if(new_name_resolve_order)
6686 lp_set_cmdline("name resolve order", new_name_resolve_order);
6688 if (!tar_to_process(tar_ctx) && !query_host && !service && !message) {
6689 poptPrintUsage(pc, stderr, 0);
6690 exit(1);
6693 poptFreeContext(pc);
6694 samba_cmdline_burn(argc, argv);
6696 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
6698 if (tar_to_process(tar_ctx)) {
6699 if (cmdstr)
6700 process_command_string(cmdstr);
6701 rc = do_tar_op(base_directory);
6702 } else if (query_host && *query_host) {
6703 char *qhost = query_host;
6704 char *slash;
6706 while (*qhost == '\\' || *qhost == '/')
6707 qhost++;
6709 if ((slash = strchr_m(qhost, '/'))
6710 || (slash = strchr_m(qhost, '\\'))) {
6711 *slash = 0;
6714 if ((p=strchr_m(qhost, '#'))) {
6715 *p = 0;
6716 p++;
6717 sscanf(p, "%x", &name_type);
6720 rc = do_host_query(qhost);
6721 } else if (message) {
6722 rc = do_message_op(creds);
6723 } else if (process(base_directory)) {
6724 rc = 1;
6727 TALLOC_FREE(frame);
6728 return rc;