s3:utils: Fix 'Usage:' for 'net ads enctypes'
[samba4-gss.git] / source3 / client / client.c
blobe2dd700314c742dcd3eeb168d110fe1c43b87e23
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"
44 #include "libcli/smb/reparse.h"
45 #include "lib/param/param.h"
47 #ifndef REGISTER
48 #define REGISTER 0
49 #endif
51 extern int do_smb_browse(void); /* mDNS browsing */
53 static int port = 0;
54 static char *service;
55 static char *desthost;
56 static bool grepable = false;
57 static bool quiet = false;
58 static char *cmdstr = NULL;
59 const char *cmd_ptr = NULL;
61 static int io_bufsize = 0; /* we use the default size */
62 static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
64 static int name_type = 0x20;
66 static int process_tok(char *tok);
67 static int cmd_help(void);
69 /* value for unused fid field in trans2 secondary request */
70 #define FID_UNUSED (0xFFFF)
72 time_t newer_than = 0;
73 static int archive_level = 0;
75 static bool translation = false;
76 static bool have_ip;
78 static bool prompt = true;
80 static bool recurse = false;
81 static bool showacls = false;
82 bool lowercase = false;
83 static bool backup_intent = false;
85 static struct sockaddr_storage dest_ss;
86 static char dest_ss_str[INET6_ADDRSTRLEN];
88 #define SEPARATORS " \t\n\r"
90 /* timing globals */
91 uint64_t get_total_size = 0;
92 unsigned int get_total_time_ms = 0;
93 static uint64_t put_total_size = 0;
94 static unsigned int put_total_time_ms = 0;
96 /* totals globals */
97 static double dir_total;
99 /* root cli_state connection */
101 struct cli_state *cli;
103 static char CLI_DIRSEP_CHAR = '\\';
104 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
106 /* Accessor functions for directory paths. */
107 static char *fileselection;
108 static const char *client_get_fileselection(void)
110 if (fileselection) {
111 return fileselection;
113 return "";
116 static const char *client_set_fileselection(const char *new_fs)
118 SAFE_FREE(fileselection);
119 if (new_fs) {
120 fileselection = SMB_STRDUP(new_fs);
122 return client_get_fileselection();
125 static char *cwd;
126 static const char *client_get_cwd(void)
128 if (cwd) {
129 return cwd;
131 return CLI_DIRSEP_STR;
134 static const char *client_set_cwd(const char *new_cwd)
136 SAFE_FREE(cwd);
137 if (new_cwd) {
138 cwd = SMB_STRDUP(new_cwd);
140 return client_get_cwd();
143 static char *cur_dir;
144 const char *client_get_cur_dir(void)
146 if (cur_dir) {
147 return cur_dir;
149 return CLI_DIRSEP_STR;
152 const char *client_set_cur_dir(const char *newdir)
154 SAFE_FREE(cur_dir);
155 if (newdir) {
156 cur_dir = SMB_STRDUP(newdir);
158 return client_get_cur_dir();
161 /****************************************************************************
162 Put up a yes/no prompt.
163 ****************************************************************************/
165 static bool yesno(const char *p)
167 char ans[20];
168 printf("%s",p);
170 if (!fgets(ans,sizeof(ans)-1,stdin))
171 return(False);
173 if (*ans == 'y' || *ans == 'Y')
174 return(True);
176 return(False);
179 /****************************************************************************
180 Write to a local file with CR/LF->LF translation if appropriate. Return the
181 number taken from the buffer. This may not equal the number written.
182 ****************************************************************************/
184 static ssize_t writefile(int f, char *b, size_t n)
186 size_t i = 0;
188 if (n == 0) {
189 errno = EINVAL;
190 return -1;
193 if (!translation) {
194 return write(f,b,n);
197 do {
198 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
199 b++;i++;
201 if (write(f, b, 1) != 1) {
202 break;
204 b++;
205 i++;
206 } while (i < n);
208 return (ssize_t)i;
211 /****************************************************************************
212 Read from a file with LF->CR/LF translation if appropriate. Return the
213 number read. read approx n bytes.
214 ****************************************************************************/
216 static int readfile(uint8_t *b, int n, FILE *f)
218 int i;
219 int c;
221 if (!translation)
222 return fread(b,1,n,f);
224 i = 0;
225 while (i < (n - 1)) {
226 if ((c = getc(f)) == EOF) {
227 break;
230 if (c == '\n') { /* change all LFs to CR/LF */
231 b[i++] = '\r';
234 b[i++] = c;
237 return(i);
240 struct push_state {
241 FILE *f;
242 off_t nread;
245 static size_t push_source(uint8_t *buf, size_t n, void *priv)
247 struct push_state *state = (struct push_state *)priv;
248 int result;
250 if (feof(state->f)) {
251 return 0;
254 result = readfile(buf, n, state->f);
255 state->nread += result;
256 return result;
259 /****************************************************************************
260 Send a message.
261 ****************************************************************************/
263 static void send_message(const char *username)
265 char buf[1600];
266 NTSTATUS status;
267 size_t i;
269 d_printf("Type your message, ending it with a Control-D\n");
271 i = 0;
272 while (i<sizeof(buf)-2) {
273 int c = fgetc(stdin);
274 if (c == EOF) {
275 break;
277 if (c == '\n') {
278 buf[i++] = '\r';
280 buf[i++] = c;
282 buf[i] = '\0';
284 status = cli_message(cli, desthost, username, buf);
285 if (!NT_STATUS_IS_OK(status)) {
286 d_fprintf(stderr, "cli_message returned %s\n",
287 nt_errstr(status));
291 /****************************************************************************
292 Check the space on a device.
293 ****************************************************************************/
295 static int do_dskattr(void)
297 uint64_t total, bsize, avail;
298 struct cli_state *targetcli = NULL;
299 char *targetpath = NULL;
300 TALLOC_CTX *ctx = talloc_tos();
301 struct cli_credentials *creds = samba_cmdline_get_creds();
302 NTSTATUS status;
304 status = cli_resolve_path(ctx,
306 creds,
307 cli,
308 client_get_cur_dir(), &targetcli,
309 &targetpath);
310 if (!NT_STATUS_IS_OK(status)) {
311 d_printf("Error in dskattr: %s\n", nt_errstr(status));
312 return 1;
315 status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
316 if (!NT_STATUS_IS_OK(status)) {
317 d_printf("Error in dskattr: %s\n", nt_errstr(status));
318 return 1;
321 d_printf("\n\t\t%" PRIu64
322 " blocks of size %" PRIu64
323 ". %" PRIu64 " blocks available\n",
324 total, bsize, avail);
326 return 0;
329 /****************************************************************************
330 Show cd/pwd.
331 ****************************************************************************/
333 static int cmd_pwd(void)
335 d_printf("Current directory is %s",service);
336 d_printf("%s\n",client_get_cur_dir());
337 return 0;
340 /****************************************************************************
341 Ensure name has correct directory separators.
342 ****************************************************************************/
344 static void normalize_name(char *newdir)
346 if (!(cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
347 string_replace(newdir,'/','\\');
351 /****************************************************************************
352 Local name cleanup before sending to server. SMB1 allows relative pathnames,
353 but SMB2 does not, so we need to resolve them locally.
354 ****************************************************************************/
356 char *client_clean_name(TALLOC_CTX *ctx, const char *name)
358 char *newname = NULL;
359 if (name == NULL) {
360 return NULL;
363 /* First ensure any path separators are correct. */
364 newname = talloc_strdup(ctx, name);
365 if (newname == NULL) {
366 return NULL;
368 normalize_name(newname);
370 /* Now remove any relative (..) path components. */
371 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
372 newname = unix_clean_name(ctx, newname);
373 } else {
374 newname = clean_name(ctx, newname);
376 if (newname == NULL) {
377 return NULL;
379 return newname;
382 /****************************************************************************
383 Change directory - inner section.
384 ****************************************************************************/
386 static int do_cd(const char *new_dir)
388 char *newdir = NULL;
389 char *saved_dir = NULL;
390 char *new_cd = NULL;
391 char *targetpath = NULL;
392 struct cli_state *targetcli = NULL;
393 int ret = 1;
394 TALLOC_CTX *ctx = talloc_stackframe();
395 struct cli_credentials *creds = samba_cmdline_get_creds();
396 NTSTATUS status;
398 newdir = talloc_strdup(ctx, new_dir);
399 if (!newdir) {
400 TALLOC_FREE(ctx);
401 return 1;
404 normalize_name(newdir);
406 /* Save the current directory in case the new directory is invalid */
408 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
409 if (!saved_dir) {
410 TALLOC_FREE(ctx);
411 return 1;
414 if (*newdir == CLI_DIRSEP_CHAR) {
415 client_set_cur_dir(newdir);
416 new_cd = newdir;
417 } else {
418 new_cd = talloc_asprintf(ctx, "%s%s",
419 client_get_cur_dir(),
420 newdir);
421 if (!new_cd) {
422 goto out;
426 /* Ensure cur_dir ends in a DIRSEP */
427 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
428 new_cd = talloc_asprintf_append(new_cd, "%s", CLI_DIRSEP_STR);
429 if (!new_cd) {
430 goto out;
433 client_set_cur_dir(new_cd);
435 new_cd = client_clean_name(ctx, new_cd);
436 client_set_cur_dir(new_cd);
438 status = cli_resolve_path(ctx, "",
439 creds,
440 cli, new_cd, &targetcli, &targetpath);
441 if (!NT_STATUS_IS_OK(status)) {
442 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
443 client_set_cur_dir(saved_dir);
444 goto out;
447 if (strequal(targetpath,CLI_DIRSEP_STR )) {
448 TALLOC_FREE(ctx);
449 return 0;
453 targetpath = talloc_asprintf(
454 ctx, "%s%s", targetpath, CLI_DIRSEP_STR);
455 if (!targetpath) {
456 client_set_cur_dir(saved_dir);
457 goto out;
459 targetpath = client_clean_name(ctx, targetpath);
460 if (!targetpath) {
461 client_set_cur_dir(saved_dir);
462 goto out;
465 status = cli_chkpath(targetcli, targetpath);
466 if (!NT_STATUS_IS_OK(status)) {
467 d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
468 client_set_cur_dir(saved_dir);
469 goto out;
472 ret = 0;
474 out:
476 TALLOC_FREE(ctx);
477 return ret;
480 /****************************************************************************
481 Change directory.
482 ****************************************************************************/
484 static int cmd_cd(void)
486 char *buf = NULL;
487 int rc = 0;
489 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
490 rc = do_cd(buf);
491 } else {
492 d_printf("Current directory is %s\n",client_get_cur_dir());
495 return rc;
498 /****************************************************************************
499 Change directory.
500 ****************************************************************************/
502 static int cmd_cd_oneup(void)
504 return do_cd("..");
507 /*******************************************************************
508 Decide if a file should be operated on.
509 ********************************************************************/
511 static bool do_this_one(struct file_info *finfo)
513 if (!finfo->name) {
514 return false;
517 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
518 return true;
521 if (*client_get_fileselection() &&
522 !mask_match(finfo->name,client_get_fileselection(),false)) {
523 DEBUG(3,("mask_match %s failed\n", finfo->name));
524 return false;
527 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
528 DEBUG(3,("newer_than %s failed\n", finfo->name));
529 return false;
532 if ((archive_level==1 || archive_level==2) && !(finfo->attr & FILE_ATTRIBUTE_ARCHIVE)) {
533 DEBUG(3,("archive %s failed\n", finfo->name));
534 return false;
537 return true;
540 /****************************************************************************
541 Display info about a file.
542 ****************************************************************************/
544 static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *finfo,
545 const char *dir)
547 time_t t;
548 TALLOC_CTX *ctx = talloc_tos();
549 NTSTATUS status = NT_STATUS_OK;
551 if (!do_this_one(finfo)) {
552 return NT_STATUS_OK;
555 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
556 if (!showacls) {
557 d_printf(" %-30s%7.7s %8.0f %s",
558 finfo->name,
559 attrib_string(talloc_tos(), finfo->attr),
560 (double)finfo->size,
561 time_to_asc(t));
562 dir_total += finfo->size;
563 } else {
564 struct cli_state *targetcli = NULL;
565 char *targetpath = NULL;
566 char *afname = NULL;
567 uint16_t fnum;
568 struct cli_credentials *creds = samba_cmdline_get_creds();
570 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
571 return NT_STATUS_OK;
573 /* create absolute filename for cli_ntcreate() FIXME */
574 afname = talloc_asprintf(ctx,
575 "%s%s%s",
576 dir,
577 CLI_DIRSEP_STR,
578 finfo->name);
579 if (!afname) {
580 return NT_STATUS_NO_MEMORY;
582 /* print file meta date header */
583 d_printf( "FILENAME:%s\n", finfo->name);
584 d_printf( "MODE:%s\n", attrib_string(talloc_tos(), finfo->attr));
585 d_printf( "SIZE:%.0f\n", (double)finfo->size);
586 d_printf( "MTIME:%s", time_to_asc(t));
588 status = cli_resolve_path(
589 ctx,
591 creds,
592 cli_state,
593 afname,
594 &targetcli,
595 &targetpath);
596 if (!NT_STATUS_IS_OK(status)) {
597 DBG_WARNING("display_finfo() Failed to resolve "
598 "%s: %s\n",
599 afname, nt_errstr(status));
600 return status;
603 status = cli_ntcreate(
604 targetcli, /* cli */
605 targetpath, /* fname */
606 0, /* CreatFlags */
607 READ_CONTROL_ACCESS, /* DesiredAccess */
608 0, /* FileAttributes */
609 FILE_SHARE_READ|
610 FILE_SHARE_WRITE, /* ShareAccess */
611 FILE_OPEN, /* CreateDisposition */
612 0x0, /* CreateOptions */
613 0x0, /* SecurityFlags */
614 &fnum, /* pfid */
615 NULL); /* cr */
616 if (!NT_STATUS_IS_OK(status)) {
617 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
618 afname, nt_errstr(status)));
619 } else {
620 struct security_descriptor *sd = NULL;
621 status = cli_query_secdesc(targetcli, fnum,
622 ctx, &sd);
623 if (!NT_STATUS_IS_OK(status)) {
624 DEBUG( 0, ("display_finfo() failed to "
625 "get security descriptor: %s\n",
626 nt_errstr(status)));
627 } else {
628 display_sec_desc(sd);
630 TALLOC_FREE(sd);
631 cli_close(targetcli, fnum);
633 TALLOC_FREE(afname);
635 return status;
638 /****************************************************************************
639 Accumulate size of a file.
640 ****************************************************************************/
642 static NTSTATUS do_du(struct cli_state *cli_state, struct file_info *finfo,
643 const char *dir)
645 if (do_this_one(finfo)) {
646 dir_total += finfo->size;
648 return NT_STATUS_OK;
651 struct do_list_queue_entry {
652 struct do_list_queue_entry *prev, *next;
653 char name[];
656 struct do_list_queue {
657 struct do_list_queue_entry *list;
660 static bool do_list_recurse;
661 static bool do_list_dirs;
662 static struct do_list_queue *queue = NULL;
663 static NTSTATUS (*do_list_fn)(struct cli_state *cli_state, struct file_info *,
664 const char *dir);
666 /****************************************************************************
667 Functions for do_list_queue.
668 ****************************************************************************/
670 static void reset_do_list_queue(void)
672 TALLOC_FREE(queue);
675 static void init_do_list_queue(void)
677 TALLOC_FREE(queue);
678 queue = talloc_zero(NULL, struct do_list_queue);
681 static void add_to_do_list_queue(const char *entry)
683 struct do_list_queue_entry *e = NULL;
684 size_t entry_str_len = strlen(entry)+1;
685 size_t entry_len = offsetof(struct do_list_queue_entry, name);
687 entry_len += entry_str_len;
688 SMB_ASSERT(entry_len >= entry_str_len);
690 e = talloc_size(queue, entry_len);
691 if (e == NULL) {
692 d_printf("talloc failed for entry %s\n", entry);
693 return;
695 talloc_set_name_const(e, "struct do_list_queue_entry");
697 memcpy(e->name, entry, entry_str_len);
698 DLIST_ADD_END(queue->list, e);
701 static char *do_list_queue_head(void)
703 return queue->list->name;
706 static void remove_do_list_queue_head(void)
708 struct do_list_queue_entry *e = queue->list;
709 DLIST_REMOVE(queue->list, e);
710 TALLOC_FREE(e);
713 static int do_list_queue_empty(void)
715 return (queue == NULL) || (queue->list == NULL);
718 /****************************************************************************
719 A helper for do_list.
720 ****************************************************************************/
722 struct do_list_helper_state {
723 const char *mask;
724 struct cli_state *cli;
727 static NTSTATUS do_list_helper(
728 struct file_info *f,
729 const char *_mask,
730 void *private_data)
732 struct do_list_helper_state *state = private_data;
733 TALLOC_CTX *ctx = talloc_tos();
734 char *dir = NULL;
735 char *dir_end = NULL;
736 NTSTATUS status = NT_STATUS_OK;
737 char *mask2 = NULL;
739 /* Work out the directory. */
740 dir = talloc_strdup(ctx, state->mask);
741 if (!dir) {
742 return NT_STATUS_NO_MEMORY;
744 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
745 *dir_end = '\0';
748 if (!(f->attr & FILE_ATTRIBUTE_DIRECTORY)) {
749 if (do_this_one(f)) {
750 status = do_list_fn(state->cli, f, dir);
752 TALLOC_FREE(dir);
753 return status;
756 if (do_list_dirs && do_this_one(f)) {
757 status = do_list_fn(state->cli, f, dir);
758 if (!NT_STATUS_IS_OK(status)) {
759 return status;
763 if (!do_list_recurse ||
764 (f->name == NULL) ||
765 ISDOT(f->name) ||
766 ISDOTDOT(f->name)) {
767 return NT_STATUS_OK;
770 if (!f->name[0]) {
771 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
772 TALLOC_FREE(dir);
773 return NT_STATUS_UNSUCCESSFUL;
776 mask2 = talloc_asprintf(ctx,
777 "%s%c%s%c*",
778 dir,
779 CLI_DIRSEP_CHAR,
780 f->name,
781 CLI_DIRSEP_CHAR);
782 if (!mask2) {
783 TALLOC_FREE(dir);
784 return NT_STATUS_NO_MEMORY;
786 add_to_do_list_queue(mask2);
787 TALLOC_FREE(mask2);
789 TALLOC_FREE(dir);
790 return NT_STATUS_OK;
793 /****************************************************************************
794 A wrapper around cli_list that adds recursion.
795 ****************************************************************************/
797 NTSTATUS do_list(const char *mask,
798 uint32_t attribute,
799 NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
800 const char *dir),
801 bool rec,
802 bool dirs)
804 struct do_list_helper_state state = { .cli = cli, };
805 static int in_do_list = 0;
806 TALLOC_CTX *ctx = talloc_tos();
807 struct cli_credentials *creds = samba_cmdline_get_creds();
808 NTSTATUS ret_status = NT_STATUS_OK;
809 NTSTATUS status = NT_STATUS_OK;
811 if (in_do_list && rec) {
812 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
813 exit(1);
816 in_do_list = 1;
818 do_list_recurse = rec;
819 do_list_dirs = dirs;
820 do_list_fn = fn;
822 init_do_list_queue();
823 add_to_do_list_queue(mask);
825 while (!do_list_queue_empty()) {
826 struct cli_state *targetcli = NULL;
827 char *targetpath = NULL;
829 state.mask = do_list_queue_head();
831 /* check for dfs */
833 status = cli_resolve_path(
834 ctx,
836 creds,
837 cli,
838 state.mask,
839 &targetcli,
840 &targetpath);
841 if (!NT_STATUS_IS_OK(status)) {
842 d_printf("do_list: [%s] %s\n", state.mask,
843 nt_errstr(status));
844 remove_do_list_queue_head();
845 continue;
848 status = cli_list(
849 targetcli,
850 targetpath,
851 attribute,
852 do_list_helper,
853 &state);
854 if (!NT_STATUS_IS_OK(status)) {
855 d_printf("%s listing %s\n",
856 nt_errstr(status), targetpath);
857 ret_status = status;
859 remove_do_list_queue_head();
860 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
861 char *next_file = do_list_queue_head();
862 char *save_ch = 0;
863 if ((strlen(next_file) >= 2) &&
864 (next_file[strlen(next_file) - 1] == '*') &&
865 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
866 save_ch = next_file +
867 strlen(next_file) - 2;
868 *save_ch = '\0';
869 if (showacls) {
870 /* cwd is only used if showacls is on */
871 client_set_cwd(next_file);
874 if (!showacls) /* don't disturbe the showacls output */
875 d_printf("\n%s\n",next_file);
876 if (save_ch) {
877 *save_ch = CLI_DIRSEP_CHAR;
880 TALLOC_FREE(targetpath);
883 in_do_list = 0;
884 reset_do_list_queue();
885 return ret_status;
888 /****************************************************************************
889 Get a directory listing.
890 ****************************************************************************/
892 static int cmd_dir(void)
894 TALLOC_CTX *ctx = talloc_tos();
895 uint32_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
896 char *mask = NULL;
897 char *buf = NULL;
898 int rc = 1;
899 NTSTATUS status;
901 dir_total = 0;
902 mask = talloc_strdup(ctx, client_get_cur_dir());
903 if (!mask) {
904 return 1;
907 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
908 normalize_name(buf);
909 if (*buf == CLI_DIRSEP_CHAR) {
910 mask = talloc_strdup(ctx, buf);
911 } else {
912 mask = talloc_asprintf_append(mask, "%s", buf);
914 } else {
915 mask = talloc_asprintf_append(mask, "*");
917 if (!mask) {
918 return 1;
921 mask = client_clean_name(ctx, mask);
922 if (mask == NULL) {
923 return 1;
926 if (showacls) {
927 /* cwd is only used if showacls is on */
928 client_set_cwd(client_get_cur_dir());
931 status = do_list(mask, attribute, display_finfo, recurse, true);
932 if (!NT_STATUS_IS_OK(status)) {
933 return 1;
936 rc = do_dskattr();
938 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
940 return rc;
943 /****************************************************************************
944 Get a directory listing.
945 ****************************************************************************/
947 static int cmd_du(void)
949 TALLOC_CTX *ctx = talloc_tos();
950 uint32_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
951 char *mask = NULL;
952 char *buf = NULL;
953 NTSTATUS status;
954 int rc = 1;
956 dir_total = 0;
957 mask = talloc_strdup(ctx, client_get_cur_dir());
958 if (!mask) {
959 return 1;
961 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
962 mask = talloc_asprintf_append(mask, "%s", CLI_DIRSEP_STR);
963 if (!mask) {
964 return 1;
968 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
969 normalize_name(buf);
970 if (*buf == CLI_DIRSEP_CHAR) {
971 mask = talloc_strdup(ctx, buf);
972 } else {
973 mask = talloc_asprintf_append(mask, "%s", buf);
975 } else {
976 mask = talloc_strdup(ctx, "*");
978 if (!mask) {
979 return 1;
982 mask = client_clean_name(ctx, mask);
983 if (mask == NULL) {
984 return 1;
987 status = do_list(mask, attribute, do_du, recurse, true);
988 if (!NT_STATUS_IS_OK(status)) {
989 return 1;
992 rc = do_dskattr();
994 d_printf("Total number of bytes: %.0f\n", dir_total);
996 return rc;
999 static int cmd_echo(void)
1001 TALLOC_CTX *ctx = talloc_tos();
1002 char *num;
1003 char *data;
1004 NTSTATUS status;
1006 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
1007 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
1008 d_printf("echo <num> <data>\n");
1009 return 1;
1012 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
1014 if (!NT_STATUS_IS_OK(status)) {
1015 d_printf("echo failed: %s\n", nt_errstr(status));
1016 return 1;
1019 return 0;
1022 /****************************************************************************
1023 Get a file from rname to lname
1024 ****************************************************************************/
1026 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
1028 int *pfd = (int *)priv;
1029 ssize_t rc;
1031 rc = writefile(*pfd, buf, n);
1032 if (rc == -1) {
1033 return map_nt_error_from_unix(errno);
1035 return NT_STATUS_OK;
1038 static int do_get(const char *rname, const char *lname_in, bool reget)
1040 TALLOC_CTX *ctx = talloc_tos();
1041 int handle = 0;
1042 uint16_t fnum;
1043 bool newhandle = false;
1044 struct timespec tp_start;
1045 uint32_t attr;
1046 off_t size;
1047 off_t start = 0;
1048 off_t nread = 0;
1049 int rc = 0;
1050 struct cli_state *targetcli = NULL;
1051 char *targetname = NULL;
1052 char *lname = NULL;
1053 struct cli_credentials *creds = samba_cmdline_get_creds();
1054 NTSTATUS status;
1056 lname = talloc_strdup(ctx, lname_in);
1057 if (!lname) {
1058 return 1;
1061 if (lowercase) {
1062 if (!strlower_m(lname)) {
1063 d_printf("strlower_m %s failed\n", lname);
1064 return 1;
1068 status = cli_resolve_path(ctx, "",
1069 creds,
1070 cli, rname, &targetcli, &targetname);
1071 if (!NT_STATUS_IS_OK(status)) {
1072 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1073 return 1;
1076 clock_gettime_mono(&tp_start);
1078 status = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum);
1079 if (!NT_STATUS_IS_OK(status)) {
1080 d_printf("%s opening remote file %s\n", nt_errstr(status),
1081 rname);
1082 return 1;
1085 if(!strcmp(lname,"-")) {
1086 handle = fileno(stdout);
1087 } else {
1088 if (reget) {
1089 handle = open(lname, O_WRONLY|O_CREAT, 0644);
1090 if (handle >= 0) {
1091 start = lseek(handle, 0, SEEK_END);
1092 if (start == -1) {
1093 d_printf("Error seeking local file\n");
1094 close(handle);
1095 return 1;
1098 } else {
1099 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1101 newhandle = true;
1103 if (handle < 0) {
1104 d_printf("Error opening local file %s\n",lname);
1105 return 1;
1109 status = cli_qfileinfo_basic(targetcli, fnum, &attr, &size, NULL, NULL,
1110 NULL, NULL, NULL);
1111 if (!NT_STATUS_IS_OK(status)) {
1112 d_printf("getattrib: %s\n", nt_errstr(status));
1113 if (newhandle) {
1114 close(handle);
1116 return 1;
1119 DEBUG(1,("getting file %s of size %.0f as %s ",
1120 rname, (double)size, lname));
1122 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1123 writefile_sink, (void *)&handle, &nread);
1124 if (!NT_STATUS_IS_OK(status)) {
1125 d_fprintf(stderr, "parallel_read returned %s\n",
1126 nt_errstr(status));
1127 if (newhandle) {
1128 close(handle);
1130 cli_close(targetcli, fnum);
1131 return 1;
1134 status = cli_close(targetcli, fnum);
1135 if (!NT_STATUS_IS_OK(status)) {
1136 d_printf("Error %s closing remote file\n", nt_errstr(status));
1137 rc = 1;
1140 if (newhandle) {
1141 close(handle);
1144 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
1145 cli_setatr(cli, rname, attr & ~(uint32_t)FILE_ATTRIBUTE_ARCHIVE, 0);
1149 struct timespec tp_end;
1150 int this_time;
1152 clock_gettime_mono(&tp_end);
1153 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1154 get_total_time_ms += this_time;
1155 get_total_size += nread;
1157 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1158 nread / (1.024*this_time + 1.0e-4),
1159 get_total_size / (1.024*get_total_time_ms)));
1162 TALLOC_FREE(targetname);
1163 return rc;
1166 /****************************************************************************
1167 Get a file.
1168 ****************************************************************************/
1170 static int cmd_get(void)
1172 TALLOC_CTX *ctx = talloc_tos();
1173 char *lname = NULL;
1174 char *rname = NULL;
1175 char *fname = NULL;
1177 rname = talloc_strdup(ctx, client_get_cur_dir());
1178 if (!rname) {
1179 return 1;
1182 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1183 d_printf("get <filename> [localname]\n");
1184 return 1;
1186 rname = talloc_asprintf_append(rname, "%s", fname);
1187 if (!rname) {
1188 return 1;
1190 rname = client_clean_name(ctx, rname);
1191 if (!rname) {
1192 return 1;
1195 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1196 if (!lname) {
1197 lname = fname;
1200 return do_get(rname, lname, false);
1203 /****************************************************************************
1204 Do an mget operation on one file.
1205 ****************************************************************************/
1207 static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
1208 const char *dir)
1210 TALLOC_CTX *ctx = talloc_tos();
1211 const char *client_cwd = NULL;
1212 size_t client_cwd_len;
1213 char *path = NULL;
1214 char *local_path = NULL;
1216 if (!finfo->name) {
1217 return NT_STATUS_OK;
1220 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
1221 return NT_STATUS_OK;
1224 if ((finfo->attr & FILE_ATTRIBUTE_DIRECTORY) && !recurse) {
1225 return NT_STATUS_OK;
1228 if (prompt) {
1229 const char *object = (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) ?
1230 "directory" : "file";
1231 char *quest = NULL;
1232 bool ok;
1234 quest = talloc_asprintf(
1235 ctx, "Get %s %s? ", object, finfo->name);
1236 if (quest == NULL) {
1237 return NT_STATUS_NO_MEMORY;
1240 ok = yesno(quest);
1241 TALLOC_FREE(quest);
1242 if (!ok) {
1243 return NT_STATUS_OK;
1247 path = talloc_asprintf(
1248 ctx, "%s%c%s", dir, CLI_DIRSEP_CHAR, finfo->name);
1249 if (path == NULL) {
1250 return NT_STATUS_NO_MEMORY;
1252 path = client_clean_name(ctx, path);
1253 if (path == NULL) {
1254 return NT_STATUS_NO_MEMORY;
1258 * Skip the path prefix if we've done a remote "cd" when
1259 * creating the local path
1261 client_cwd = client_get_cur_dir();
1262 client_cwd_len = strlen(client_cwd);
1264 local_path = talloc_strdup(ctx, path + client_cwd_len);
1265 if (local_path == NULL) {
1266 TALLOC_FREE(path);
1267 return NT_STATUS_NO_MEMORY;
1269 string_replace(local_path, CLI_DIRSEP_CHAR, '/');
1271 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
1272 int ret = mkdir(local_path, 0777);
1274 if ((ret == -1) && (errno != EEXIST)) {
1275 return map_nt_error_from_unix(errno);
1277 } else {
1278 do_get(path, local_path, false);
1281 return NT_STATUS_OK;
1284 /****************************************************************************
1285 View the file using the pager.
1286 ****************************************************************************/
1288 static int cmd_more(void)
1290 TALLOC_CTX *ctx = talloc_tos();
1291 char *rname = NULL;
1292 char *fname = NULL;
1293 char *lname = NULL;
1294 char *pager_cmd = NULL;
1295 const char *pager;
1296 int fd;
1297 int rc = 0;
1298 mode_t mask;
1300 rname = talloc_strdup(ctx, client_get_cur_dir());
1301 if (!rname) {
1302 return 1;
1305 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1306 if (!lname) {
1307 return 1;
1309 mask = umask(S_IRWXO | S_IRWXG);
1310 fd = mkstemp(lname);
1311 umask(mask);
1312 if (fd == -1) {
1313 d_printf("failed to create temporary file for more\n");
1314 return 1;
1316 close(fd);
1318 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1319 d_printf("more <filename>\n");
1320 unlink(lname);
1321 return 1;
1323 rname = talloc_asprintf_append(rname, "%s", fname);
1324 if (!rname) {
1325 return 1;
1327 rname = client_clean_name(ctx,rname);
1328 if (!rname) {
1329 return 1;
1332 rc = do_get(rname, lname, false);
1334 pager=getenv("PAGER");
1336 pager_cmd = talloc_asprintf(ctx,
1337 "%s %s",
1338 (pager? pager:PAGER),
1339 lname);
1340 if (!pager_cmd) {
1341 return 1;
1343 if (system(pager_cmd) == -1) {
1344 d_printf("system command '%s' returned -1\n",
1345 pager_cmd);
1347 unlink(lname);
1349 return rc;
1352 /****************************************************************************
1353 Do a mget command.
1354 ****************************************************************************/
1356 static int cmd_mget(void)
1358 TALLOC_CTX *ctx = talloc_tos();
1359 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1360 char *mget_mask = NULL;
1361 char *buf = NULL;
1362 NTSTATUS status = NT_STATUS_OK;
1364 if (recurse) {
1365 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1368 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1370 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1371 if (!mget_mask) {
1372 return 1;
1374 if (*buf == CLI_DIRSEP_CHAR) {
1375 mget_mask = talloc_strdup(ctx, buf);
1376 } else {
1377 mget_mask = talloc_asprintf_append(mget_mask,
1378 "%s", buf);
1380 if (!mget_mask) {
1381 return 1;
1383 mget_mask = client_clean_name(ctx, mget_mask);
1384 if (mget_mask == NULL) {
1385 return 1;
1387 status = do_list(mget_mask, attribute, do_mget, recurse, true);
1388 if (!NT_STATUS_IS_OK(status)) {
1389 return 1;
1393 if (mget_mask == NULL) {
1394 d_printf("nothing to mget\n");
1395 return 0;
1398 if (!*mget_mask) {
1399 mget_mask = talloc_asprintf(ctx,
1400 "%s*",
1401 client_get_cur_dir());
1402 if (!mget_mask) {
1403 return 1;
1405 mget_mask = client_clean_name(ctx, mget_mask);
1406 if (mget_mask == NULL) {
1407 return 1;
1409 status = do_list(mget_mask, attribute, do_mget, recurse, true);
1410 if (!NT_STATUS_IS_OK(status)) {
1411 return 1;
1415 return 0;
1418 /****************************************************************************
1419 Make a directory of name "name".
1420 ****************************************************************************/
1422 static bool do_mkdir(const char *name)
1424 TALLOC_CTX *ctx = talloc_tos();
1425 struct cli_state *targetcli;
1426 char *targetname = NULL;
1427 struct cli_credentials *creds = samba_cmdline_get_creds();
1428 NTSTATUS status;
1430 status = cli_resolve_path(ctx, "",
1431 creds,
1432 cli, name, &targetcli, &targetname);
1433 if (!NT_STATUS_IS_OK(status)) {
1434 d_printf("mkdir %s: %s\n", name, nt_errstr(status));
1435 return false;
1438 status = cli_mkdir(targetcli, targetname);
1439 if (!NT_STATUS_IS_OK(status)) {
1440 d_printf("%s making remote directory %s\n",
1441 nt_errstr(status),name);
1442 return false;
1445 return true;
1448 /****************************************************************************
1449 Show 8.3 name of a file.
1450 ****************************************************************************/
1452 static bool do_altname(const char *name)
1454 fstring altname;
1455 NTSTATUS status;
1457 status = cli_qpathinfo_alt_name(cli, name, altname);
1458 if (!NT_STATUS_IS_OK(status)) {
1459 d_printf("%s getting alt name for %s\n",
1460 nt_errstr(status),name);
1461 return false;
1463 d_printf("%s\n", altname);
1465 return true;
1468 /****************************************************************************
1469 Exit client.
1470 ****************************************************************************/
1472 static int cmd_quit(void)
1474 cli_shutdown(cli);
1475 exit(0);
1476 /* NOTREACHED */
1477 return 0;
1480 /****************************************************************************
1481 Make a directory.
1482 ****************************************************************************/
1484 static int cmd_mkdir(void)
1486 TALLOC_CTX *ctx = talloc_tos();
1487 char *mask = NULL;
1488 char *buf = NULL;
1489 struct cli_credentials *creds = samba_cmdline_get_creds();
1490 NTSTATUS status;
1492 mask = talloc_strdup(ctx, client_get_cur_dir());
1493 if (!mask) {
1494 return 1;
1497 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1498 if (!recurse) {
1499 d_printf("mkdir <dirname>\n");
1501 return 1;
1503 mask = talloc_asprintf_append(mask, "%s", buf);
1504 if (!mask) {
1505 return 1;
1507 mask = client_clean_name(ctx, mask);
1508 if (mask == NULL) {
1509 return 1;
1512 if (recurse) {
1513 char *ddir = NULL;
1514 char *ddir2 = NULL;
1515 struct cli_state *targetcli;
1516 char *targetname = NULL;
1517 char *p = NULL;
1518 char *saveptr;
1520 ddir2 = talloc_strdup(ctx, "");
1521 if (!ddir2) {
1522 return 1;
1525 status = cli_resolve_path(ctx, "",
1526 creds,
1527 cli, mask,
1528 &targetcli, &targetname);
1529 if (!NT_STATUS_IS_OK(status)) {
1530 return 1;
1533 ddir = talloc_strdup(ctx, targetname);
1534 if (!ddir) {
1535 return 1;
1537 trim_char(ddir,'.','\0');
1538 p = strtok_r(ddir, "/\\", &saveptr);
1539 while (p) {
1540 ddir2 = talloc_asprintf_append(ddir2, "%s", p);
1541 if (!ddir2) {
1542 return 1;
1544 if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
1545 do_mkdir(ddir2);
1547 ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
1548 if (!ddir2) {
1549 return 1;
1551 p = strtok_r(NULL, "/\\", &saveptr);
1553 } else {
1554 do_mkdir(mask);
1557 return 0;
1560 /****************************************************************************
1561 Show alt name.
1562 ****************************************************************************/
1564 static int cmd_altname(void)
1566 TALLOC_CTX *ctx = talloc_tos();
1567 char *name;
1568 char *buf;
1570 name = talloc_strdup(ctx, client_get_cur_dir());
1571 if (!name) {
1572 return 1;
1575 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1576 d_printf("altname <file>\n");
1577 return 1;
1579 name = talloc_asprintf_append(name, "%s", buf);
1580 if (!name) {
1581 return 1;
1583 name = client_clean_name(ctx, name);
1584 if (name == NULL) {
1585 return 1;
1587 do_altname(name);
1588 return 0;
1591 static char *attr_str(TALLOC_CTX *mem_ctx, uint32_t attr)
1593 char *attrs = talloc_zero_array(mem_ctx, char, 17);
1594 int i = 0;
1596 if (!(attr & FILE_ATTRIBUTE_NORMAL)) {
1597 if (attr & FILE_ATTRIBUTE_ENCRYPTED) {
1598 attrs[i++] = 'E';
1600 if (attr & FILE_ATTRIBUTE_NONINDEXED) {
1601 attrs[i++] = 'N';
1603 if (attr & FILE_ATTRIBUTE_OFFLINE) {
1604 attrs[i++] = 'O';
1606 if (attr & FILE_ATTRIBUTE_COMPRESSED) {
1607 attrs[i++] = 'C';
1609 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1610 attrs[i++] = 'r';
1612 if (attr & FILE_ATTRIBUTE_SPARSE) {
1613 attrs[i++] = 's';
1615 if (attr & FILE_ATTRIBUTE_TEMPORARY) {
1616 attrs[i++] = 'T';
1618 if (attr & FILE_ATTRIBUTE_NORMAL) {
1619 attrs[i++] = 'N';
1621 if (attr & FILE_ATTRIBUTE_READONLY) {
1622 attrs[i++] = 'R';
1624 if (attr & FILE_ATTRIBUTE_HIDDEN) {
1625 attrs[i++] = 'H';
1627 if (attr & FILE_ATTRIBUTE_SYSTEM) {
1628 attrs[i++] = 'S';
1630 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
1631 attrs[i++] = 'D';
1633 if (attr & FILE_ATTRIBUTE_ARCHIVE) {
1634 attrs[i++] = 'A';
1637 return attrs;
1640 /****************************************************************************
1641 Show all info we can get
1642 ****************************************************************************/
1644 static int do_allinfo(const char *name)
1646 fstring altname;
1647 struct timespec b_time, a_time, m_time, c_time;
1648 off_t size;
1649 uint32_t attr;
1650 NTTIME tmp;
1651 uint16_t fnum;
1652 unsigned int num_streams;
1653 struct stream_struct *streams;
1654 int j, num_snapshots;
1655 char **snapshots = NULL;
1656 unsigned int i;
1657 NTSTATUS status;
1659 status = cli_qpathinfo_alt_name(cli, name, altname);
1660 if (!NT_STATUS_IS_OK(status)) {
1661 d_printf("%s getting alt name for %s\n", nt_errstr(status),
1662 name);
1664 * Ignore not supported or not implemented, it does not
1665 * hurt if we can't list alternate names.
1667 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
1668 NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
1669 altname[0] = '\0';
1670 } else {
1671 return false;
1674 d_printf("altname: %s\n", altname);
1676 status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
1677 &size, &attr, NULL);
1678 if (!NT_STATUS_IS_OK(status)) {
1679 d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
1680 name);
1681 return false;
1684 tmp = full_timespec_to_nt_time(&b_time);
1685 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1687 tmp = full_timespec_to_nt_time(&a_time);
1688 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1690 tmp = full_timespec_to_nt_time(&m_time);
1691 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1693 tmp = full_timespec_to_nt_time(&c_time);
1694 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1696 d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), attr), attr);
1698 status = cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1699 &streams);
1700 if (!NT_STATUS_IS_OK(status)) {
1701 d_fprintf(stderr,
1702 "%s getting streams for %s\n",
1703 nt_errstr(status),
1704 name);
1705 num_streams = 0;
1708 for (i=0; i<num_streams; i++) {
1709 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1710 (unsigned long long)streams[i].size);
1713 if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1714 struct reparse_data_buffer *rep = NULL;
1715 uint8_t *data = NULL;
1716 uint32_t datalen;
1717 char *s = NULL;
1719 rep = talloc_zero(talloc_tos(), struct reparse_data_buffer);
1720 if (rep == NULL) {
1721 d_printf("talloc_zero() failed\n");
1722 return false;
1725 status = cli_get_reparse_data(cli, name, rep, &data, &datalen);
1726 if (!NT_STATUS_IS_OK(status)) {
1727 d_fprintf(stderr,
1728 "cli_get_reparse_data() failed: %s\n",
1729 nt_errstr(status));
1730 TALLOC_FREE(rep);
1731 return false;
1734 status = reparse_data_buffer_parse(rep, rep, data, datalen);
1735 if (!NT_STATUS_IS_OK(status)) {
1736 d_fprintf(stderr,
1737 "reparse_data_buffer_parse() failed: %s\n",
1738 nt_errstr(status));
1739 TALLOC_FREE(rep);
1740 return false;
1743 s = reparse_data_buffer_str(rep, rep);
1744 d_printf("%s", s);
1745 TALLOC_FREE(rep);
1748 status = cli_ntcreate(cli, name, 0,
1749 SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
1750 SEC_STD_SYNCHRONIZE, 0,
1751 FILE_SHARE_READ|FILE_SHARE_WRITE
1752 |FILE_SHARE_DELETE,
1753 FILE_OPEN, 0x0, 0x0, &fnum, NULL);
1754 if (!NT_STATUS_IS_OK(status)) {
1756 * Ignore failure, it does not hurt if we can't list
1757 * snapshots
1759 return 0;
1762 * In order to get shadow copy data over SMB1 we
1763 * must call twice, once with 'get_names = false'
1764 * to get the size, then again with 'get_names = true'
1765 * to get the data or a Windows server fails to return
1766 * valid info. Samba doesn't have this bug. JRA.
1769 status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
1770 false, &snapshots, &num_snapshots);
1771 if (NT_STATUS_IS_OK(status)) {
1772 status = cli_shadow_copy_data(talloc_tos(),
1773 cli,
1774 fnum,
1775 true,
1776 &snapshots,
1777 &num_snapshots);
1779 if (!NT_STATUS_IS_OK(status)) {
1780 d_fprintf(stderr,
1781 "%s getting shadow copy data for %s\n",
1782 nt_errstr(status),
1783 name);
1784 num_snapshots = 0;
1787 for (j=0; j<num_snapshots; j++) {
1788 char *snap_name;
1790 d_printf("%s\n", snapshots[j]);
1791 snap_name = talloc_asprintf(talloc_tos(), "%s%s",
1792 snapshots[j], name);
1793 status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
1794 &m_time, &c_time, &size,
1795 NULL, NULL);
1796 if (!NT_STATUS_IS_OK(status)) {
1797 d_fprintf(stderr, "pathinfo(%s) failed: %s\n",
1798 snap_name, nt_errstr(status));
1799 TALLOC_FREE(snap_name);
1800 continue;
1802 tmp = unix_timespec_to_nt_time(b_time);
1803 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1804 tmp = unix_timespec_to_nt_time(a_time);
1805 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1806 tmp =unix_timespec_to_nt_time(m_time);
1807 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1808 tmp = unix_timespec_to_nt_time(c_time);
1809 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1810 d_printf("size: %d\n", (int)size);
1813 TALLOC_FREE(snapshots);
1814 cli_close(cli, fnum);
1816 return 0;
1819 /****************************************************************************
1820 Show all info we can get
1821 ****************************************************************************/
1823 static int cmd_allinfo(void)
1825 TALLOC_CTX *ctx = talloc_tos();
1826 char *name;
1827 char *buf;
1829 name = talloc_strdup(ctx, client_get_cur_dir());
1830 if (!name) {
1831 return 1;
1834 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1835 d_printf("allinfo <file>\n");
1836 return 1;
1838 name = talloc_asprintf_append(name, "%s", buf);
1839 if (!name) {
1840 return 1;
1842 name = client_clean_name(ctx, name);
1843 if (name == NULL) {
1844 return 1;
1846 do_allinfo(name);
1848 return 0;
1851 /****************************************************************************
1852 Put a single file.
1853 ****************************************************************************/
1855 static int do_put(const char *rname, const char *lname, bool reput)
1857 TALLOC_CTX *ctx = talloc_tos();
1858 uint16_t fnum;
1859 FILE *f;
1860 off_t start = 0;
1861 int rc = 0;
1862 struct timespec tp_start;
1863 struct cli_state *targetcli;
1864 char *targetname = NULL;
1865 struct push_state state;
1866 struct cli_credentials *creds = samba_cmdline_get_creds();
1867 NTSTATUS status;
1869 status = cli_resolve_path(ctx, "",
1870 creds,
1871 cli, rname, &targetcli, &targetname);
1872 if (!NT_STATUS_IS_OK(status)) {
1873 d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
1874 return 1;
1877 clock_gettime_mono(&tp_start);
1879 if (reput) {
1880 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
1881 if (NT_STATUS_IS_OK(status)) {
1882 if (!NT_STATUS_IS_OK(status = cli_qfileinfo_basic(
1883 targetcli, fnum, NULL,
1884 &start, NULL, NULL,
1885 NULL, NULL, NULL))) {
1886 d_printf("getattrib: %s\n", nt_errstr(status));
1887 return 1;
1890 } else {
1891 status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
1894 if (!NT_STATUS_IS_OK(status)) {
1895 d_printf("%s opening remote file %s\n", nt_errstr(status),
1896 rname);
1897 return 1;
1900 /* allow files to be piped into smbclient
1901 jdblair 24.jun.98
1903 Note that in this case this function will exit(0) rather
1904 than returning. */
1905 if (!strcmp(lname, "-")) {
1906 f = stdin;
1907 /* size of file is not known */
1908 } else {
1909 f = fopen(lname, "r");
1910 if (f && reput) {
1911 if (fseek(f, start, SEEK_SET) == -1) {
1912 d_printf("Error seeking local file\n");
1913 fclose(f);
1914 return 1;
1919 if (!f) {
1920 d_printf("Error opening local file %s\n",lname);
1921 return 1;
1924 DEBUG(1,("putting file %s as %s ",lname,
1925 rname));
1927 setvbuf(f, NULL, _IOFBF, io_bufsize);
1929 state.f = f;
1930 state.nread = 0;
1932 status = cli_push(targetcli, fnum, 0, 0, io_bufsize, push_source,
1933 &state);
1934 if (!NT_STATUS_IS_OK(status)) {
1935 d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
1936 rc = 1;
1939 status = cli_close(targetcli, fnum);
1940 if (!NT_STATUS_IS_OK(status)) {
1941 d_printf("%s closing remote file %s\n", nt_errstr(status),
1942 rname);
1943 if (f != stdin) {
1944 fclose(f);
1946 return 1;
1949 if (f != stdin) {
1950 fclose(f);
1954 struct timespec tp_end;
1955 int this_time;
1957 clock_gettime_mono(&tp_end);
1958 this_time = nsec_time_diff(&tp_end,&tp_start)/1000000;
1959 put_total_time_ms += this_time;
1960 put_total_size += state.nread;
1962 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1963 state.nread / (1.024*this_time + 1.0e-4),
1964 put_total_size / (1.024*put_total_time_ms)));
1967 if (f == stdin) {
1968 cli_shutdown(cli);
1969 exit(rc);
1972 return rc;
1975 /****************************************************************************
1976 Put a file.
1977 ****************************************************************************/
1979 static int cmd_put(void)
1981 TALLOC_CTX *ctx = talloc_tos();
1982 char *lname;
1983 char *rname;
1984 char *buf;
1986 rname = talloc_strdup(ctx, client_get_cur_dir());
1987 if (!rname) {
1988 return 1;
1991 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1992 d_printf("put <filename>\n");
1993 return 1;
1996 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1997 rname = talloc_asprintf_append(rname, "%s", buf);
1998 } else {
1999 rname = talloc_asprintf_append(rname, "%s", lname);
2001 if (!rname) {
2002 return 1;
2005 rname = client_clean_name(ctx, rname);
2006 if (!rname) {
2007 return 1;
2011 SMB_STRUCT_STAT st;
2012 /* allow '-' to represent stdin
2013 jdblair, 24.jun.98 */
2014 if (!file_exist_stat(lname, &st, false) &&
2015 (strcmp(lname,"-"))) {
2016 d_printf("%s does not exist\n",lname);
2017 return 1;
2021 return do_put(rname, lname, false);
2024 /*************************************
2025 File list structure.
2026 *************************************/
2028 static struct file_list {
2029 struct file_list *prev, *next;
2030 char *file_path;
2031 bool isdir;
2032 } *file_list;
2034 /****************************************************************************
2035 Free a file_list structure.
2036 ****************************************************************************/
2038 static void free_file_list (struct file_list *l_head)
2040 struct file_list *list, *next;
2042 for (list = l_head; list; list = next) {
2043 next = list->next;
2044 DLIST_REMOVE(l_head, list);
2045 TALLOC_FREE(list);
2049 /****************************************************************************
2050 Seek in a directory/file list until you get something that doesn't start with
2051 the specified name.
2052 ****************************************************************************/
2054 static bool seek_list(struct file_list *list, char *name)
2056 while (list) {
2057 trim_string(list->file_path,"./","\n");
2058 if (strncmp(list->file_path, name, strlen(name)) != 0) {
2059 return true;
2061 list = list->next;
2064 return false;
2067 /****************************************************************************
2068 Set the file selection mask.
2069 ****************************************************************************/
2071 static int cmd_select(void)
2073 TALLOC_CTX *ctx = talloc_tos();
2074 char *new_fs = NULL;
2075 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
2077 if (new_fs) {
2078 client_set_fileselection(new_fs);
2079 } else {
2080 client_set_fileselection("");
2082 return 0;
2085 /****************************************************************************
2086 Recursive file matching function act as find
2087 match must be always set to true when calling this function
2088 ****************************************************************************/
2090 static int file_find(TALLOC_CTX *ctx,
2091 struct file_list **list,
2092 const char *directory,
2093 const char *expression,
2094 bool match)
2096 DIR *dir;
2097 struct file_list *entry;
2098 struct stat statbuf;
2099 int ret;
2100 char *path;
2101 bool isdir;
2102 const char *dname;
2104 dir = opendir(directory);
2105 if (!dir)
2106 return -1;
2108 while ((dname = readdirname(dir))) {
2109 if (ISDOT(dname) || ISDOTDOT(dname)) {
2110 continue;
2113 path = talloc_asprintf(ctx, "%s/%s", directory, dname);
2114 if (path == NULL) {
2115 continue;
2118 isdir = false;
2119 if (!match || !gen_fnmatch(expression, dname)) {
2120 if (recurse) {
2121 ret = stat(path, &statbuf);
2122 if (ret == 0) {
2123 if (S_ISDIR(statbuf.st_mode)) {
2124 isdir = true;
2125 ret = file_find(ctx,
2126 list,
2127 path,
2128 expression,
2129 false);
2131 } else {
2132 d_printf("file_find: cannot stat file %s\n", path);
2135 if (ret == -1) {
2136 TALLOC_FREE(path);
2137 closedir(dir);
2138 return -1;
2141 entry = talloc_zero(ctx, struct file_list);
2142 if (!entry) {
2143 d_printf("Out of memory in file_find\n");
2144 closedir(dir);
2145 return -1;
2147 entry->file_path = talloc_move(entry, &path);
2148 entry->isdir = isdir;
2149 DLIST_ADD(*list, entry);
2150 } else {
2151 TALLOC_FREE(path);
2155 closedir(dir);
2156 return 0;
2159 /****************************************************************************
2160 mput some files.
2161 ****************************************************************************/
2163 static int cmd_mput(void)
2165 TALLOC_CTX *ctx = talloc_tos();
2166 char *p = NULL;
2168 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
2169 int ret;
2170 struct file_list *temp_list;
2171 char *quest, *lname, *rname;
2173 file_list = NULL;
2175 ret = file_find(ctx, &file_list, ".", p, true);
2176 if (ret) {
2177 free_file_list(file_list);
2178 continue;
2181 quest = NULL;
2182 lname = NULL;
2183 rname = NULL;
2185 for (temp_list = file_list; temp_list;
2186 temp_list = temp_list->next) {
2188 SAFE_FREE(lname);
2189 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
2190 continue;
2192 trim_string(lname, "./", "/");
2194 /* check if it's a directory */
2195 if (temp_list->isdir) {
2196 /* if (!recurse) continue; */
2198 SAFE_FREE(quest);
2199 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
2200 break;
2202 if (prompt && !yesno(quest)) { /* No */
2203 /* Skip the directory */
2204 lname[strlen(lname)-1] = '/';
2205 if (!seek_list(temp_list, lname))
2206 break;
2207 } else { /* Yes */
2208 SAFE_FREE(rname);
2209 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2210 break;
2212 normalize_name(rname);
2214 char *tmp_rname =
2215 client_clean_name(ctx, rname);
2216 if (tmp_rname == NULL) {
2217 break;
2219 SAFE_FREE(rname);
2220 rname = smb_xstrdup(tmp_rname);
2221 TALLOC_FREE(tmp_rname);
2222 if (rname == NULL) {
2223 break;
2226 if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
2227 !do_mkdir(rname)) {
2228 DEBUG (0, ("Unable to make dir, skipping...\n"));
2229 /* Skip the directory */
2230 lname[strlen(lname)-1] = '/';
2231 if (!seek_list(temp_list, lname)) {
2232 break;
2236 continue;
2237 } else {
2238 SAFE_FREE(quest);
2239 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
2240 break;
2242 if (prompt && !yesno(quest)) {
2243 /* No */
2244 continue;
2247 /* Yes */
2248 SAFE_FREE(rname);
2249 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
2250 break;
2254 normalize_name(rname);
2257 char *tmp_rname = client_clean_name(ctx, rname);
2258 if (tmp_rname == NULL) {
2259 break;
2261 SAFE_FREE(rname);
2262 rname = smb_xstrdup(tmp_rname);
2263 TALLOC_FREE(tmp_rname);
2264 if (rname == NULL) {
2265 break;
2268 do_put(rname, lname, false);
2270 free_file_list(file_list);
2271 SAFE_FREE(quest);
2272 SAFE_FREE(lname);
2273 SAFE_FREE(rname);
2276 return 0;
2279 /****************************************************************************
2280 Cancel a print job.
2281 ****************************************************************************/
2283 static int do_cancel(int job)
2285 NTSTATUS status = cli_printjob_del(cli, job);
2287 if (NT_STATUS_IS_OK(status)) {
2288 d_printf("Job %d cancelled\n",job);
2289 return 0;
2290 } else {
2291 d_printf("Error cancelling job %d : %s\n",
2292 job, nt_errstr(status));
2293 return 1;
2297 /****************************************************************************
2298 Cancel a print job.
2299 ****************************************************************************/
2301 static int cmd_cancel(void)
2303 TALLOC_CTX *ctx = talloc_tos();
2304 char *buf = NULL;
2305 int job;
2307 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2308 d_printf("cancel <jobid> ...\n");
2309 return 1;
2311 do {
2312 job = atoi(buf);
2313 do_cancel(job);
2314 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2316 return 0;
2319 /****************************************************************************
2320 Print a file.
2321 ****************************************************************************/
2323 static int cmd_print(void)
2325 TALLOC_CTX *ctx = talloc_tos();
2326 char *lname = NULL;
2327 char *rname = NULL;
2328 char *p = NULL;
2330 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2331 d_printf("print <filename>\n");
2332 return 1;
2335 rname = talloc_strdup(ctx, lname);
2336 if (!rname) {
2337 return 1;
2339 p = strrchr_m(rname,'/');
2340 if (p) {
2341 rname = talloc_asprintf(ctx,
2342 "%s-%d",
2343 p+1,
2344 (int)getpid());
2346 if (strequal(lname,"-")) {
2347 rname = talloc_asprintf(ctx,
2348 "stdin-%d",
2349 (int)getpid());
2351 if (!rname) {
2352 return 1;
2355 return do_put(rname, lname, false);
2358 /****************************************************************************
2359 Show a print queue entry.
2360 ****************************************************************************/
2362 static void queue_fn(struct print_job_info *p)
2364 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2367 /****************************************************************************
2368 Show a print queue.
2369 ****************************************************************************/
2371 static int cmd_queue(void)
2373 cli_print_queue(cli, queue_fn);
2374 return 0;
2377 /****************************************************************************
2378 Delete some files.
2379 ****************************************************************************/
2381 static NTSTATUS do_del(struct cli_state *cli_state, struct file_info *finfo,
2382 const char *dir)
2384 TALLOC_CTX *ctx = talloc_tos();
2385 char *mask = NULL;
2386 struct cli_state *targetcli = NULL;
2387 char *targetname = NULL;
2388 struct cli_credentials *creds = samba_cmdline_get_creds();
2389 NTSTATUS status;
2391 mask = talloc_asprintf(ctx,
2392 "%s%c%s",
2393 dir,
2394 CLI_DIRSEP_CHAR,
2395 finfo->name);
2396 if (!mask) {
2397 return NT_STATUS_NO_MEMORY;
2400 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2401 TALLOC_FREE(mask);
2402 return NT_STATUS_OK;
2405 status = cli_resolve_path(ctx, "",
2406 creds,
2407 cli, mask, &targetcli, &targetname);
2408 if (!NT_STATUS_IS_OK(status)) {
2409 goto out;
2412 status = cli_unlink(targetcli, targetname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2413 out:
2414 if (!NT_STATUS_IS_OK(status)) {
2415 d_printf("%s deleting remote file %s\n",
2416 nt_errstr(status), mask);
2418 TALLOC_FREE(mask);
2419 return status;
2422 /****************************************************************************
2423 Delete some files.
2424 ****************************************************************************/
2426 static int cmd_del(void)
2428 TALLOC_CTX *ctx = talloc_tos();
2429 char *mask = NULL;
2430 char *buf = NULL;
2431 NTSTATUS status = NT_STATUS_OK;
2432 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
2434 if (recurse) {
2435 attribute |= FILE_ATTRIBUTE_DIRECTORY;
2438 mask = talloc_strdup(ctx, client_get_cur_dir());
2439 if (!mask) {
2440 return 1;
2442 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2443 d_printf("del <filename>\n");
2444 return 1;
2446 mask = talloc_asprintf_append(mask, "%s", buf);
2447 if (!mask) {
2448 return 1;
2450 mask = client_clean_name(ctx, mask);
2451 if (mask == NULL) {
2452 return 1;
2455 status = do_list(mask,attribute,do_del,false,false);
2456 if (!NT_STATUS_IS_OK(status)) {
2457 return 1;
2459 return 0;
2462 /****************************************************************************
2463 Delete some files.
2464 ****************************************************************************/
2466 static NTSTATUS delete_remote_files_list(struct cli_state *cli_state,
2467 struct file_list *flist)
2469 NTSTATUS status = NT_STATUS_OK;
2470 struct file_list *deltree_list_iter = NULL;
2471 char *targetname = NULL;
2472 struct cli_state *targetcli = NULL;
2473 struct cli_credentials *creds = samba_cmdline_get_creds();
2474 TALLOC_CTX *ctx = talloc_tos();
2476 for (deltree_list_iter = flist;
2477 deltree_list_iter != NULL;
2478 deltree_list_iter = deltree_list_iter->next) {
2479 status = cli_resolve_path(ctx,
2481 creds,
2482 cli_state,
2483 deltree_list_iter->file_path,
2484 &targetcli,
2485 &targetname);
2486 if (!NT_STATUS_IS_OK(status)) {
2487 d_printf("delete_remote_files %s: %s\n",
2488 deltree_list_iter->file_path,
2489 nt_errstr(status));
2490 return status;
2492 if (CLI_DIRSEP_CHAR == '/') {
2493 /* POSIX. */
2494 status = cli_posix_unlink(targetcli,
2495 targetname);
2496 } else if (deltree_list_iter->isdir) {
2497 status = cli_rmdir(targetcli,
2498 targetname);
2499 } else {
2500 status = cli_unlink(targetcli,
2501 targetname,
2502 FILE_ATTRIBUTE_SYSTEM |
2503 FILE_ATTRIBUTE_HIDDEN);
2505 if (!NT_STATUS_IS_OK(status)) {
2506 d_printf("%s deleting remote %s %s\n",
2507 nt_errstr(status),
2508 deltree_list_iter->isdir ?
2509 "directory" : "file",
2510 deltree_list_iter->file_path);
2511 return status;
2514 return NT_STATUS_OK;
2517 /****************************************************************************
2518 Save a list of files to delete.
2519 ****************************************************************************/
2521 static struct file_list *deltree_list_head;
2523 static NTSTATUS do_deltree_list(struct cli_state *cli_state,
2524 struct file_info *finfo,
2525 const char *dir)
2527 struct file_list **file_list_head_pp = &deltree_list_head;
2528 struct file_list *dt = NULL;
2530 if (!do_this_one(finfo)) {
2531 return NT_STATUS_OK;
2534 /* skip if this is . or .. */
2535 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
2536 return NT_STATUS_OK;
2539 dt = talloc_zero(NULL, struct file_list);
2540 if (dt == NULL) {
2541 return NT_STATUS_NO_MEMORY;
2544 /* create absolute filename for cli_ntcreate() */
2545 dt->file_path = talloc_asprintf(dt,
2546 "%s%s%s",
2547 dir,
2548 CLI_DIRSEP_STR,
2549 finfo->name);
2550 if (dt->file_path == NULL) {
2551 TALLOC_FREE(dt);
2552 return NT_STATUS_NO_MEMORY;
2555 if (finfo->attr & FILE_ATTRIBUTE_DIRECTORY) {
2556 dt->isdir = true;
2559 DLIST_ADD(*file_list_head_pp, dt);
2560 return NT_STATUS_OK;
2563 static int cmd_deltree(void)
2565 TALLOC_CTX *ctx = talloc_tos();
2566 char *buf = NULL;
2567 NTSTATUS status = NT_STATUS_OK;
2568 struct file_list *deltree_list_norecurse = NULL;
2569 struct file_list *deltree_list_iter = NULL;
2570 uint32_t attribute = FILE_ATTRIBUTE_SYSTEM |
2571 FILE_ATTRIBUTE_HIDDEN |
2572 FILE_ATTRIBUTE_DIRECTORY;
2573 bool ok;
2574 char *mask = talloc_strdup(ctx, client_get_cur_dir());
2575 if (mask == NULL) {
2576 return 1;
2578 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
2579 if (!ok) {
2580 d_printf("deltree <filename>\n");
2581 return 1;
2583 mask = talloc_asprintf_append(mask, "%s", buf);
2584 if (mask == NULL) {
2585 return 1;
2587 mask = client_clean_name(ctx, mask);
2588 if (mask == NULL) {
2589 return 1;
2592 deltree_list_head = NULL;
2595 * Get the list of directories to
2596 * delete (in case mask has a wildcard).
2598 status = do_list(mask, attribute, do_deltree_list, false, true);
2599 if (!NT_STATUS_IS_OK(status)) {
2600 goto err;
2602 deltree_list_norecurse = deltree_list_head;
2603 deltree_list_head = NULL;
2605 for (deltree_list_iter = deltree_list_norecurse;
2606 deltree_list_iter != NULL;
2607 deltree_list_iter = deltree_list_iter->next) {
2609 if (deltree_list_iter->isdir == false) {
2610 char *targetname = NULL;
2611 struct cli_state *targetcli = NULL;
2612 struct cli_credentials *creds = samba_cmdline_get_creds();
2613 status = cli_resolve_path(ctx,
2615 creds,
2616 cli,
2617 deltree_list_iter->file_path,
2618 &targetcli,
2619 &targetname);
2620 if (!NT_STATUS_IS_OK(status)) {
2621 goto err;
2623 /* Just a regular file. */
2624 if (CLI_DIRSEP_CHAR == '/') {
2625 /* POSIX. */
2626 status = cli_posix_unlink(targetcli,
2627 targetname);
2628 } else {
2629 status = cli_unlink(targetcli,
2630 targetname,
2631 FILE_ATTRIBUTE_SYSTEM |
2632 FILE_ATTRIBUTE_HIDDEN);
2634 if (!NT_STATUS_IS_OK(status)) {
2635 goto err;
2637 continue;
2641 * Get the list of files or directories to
2642 * delete in depth order.
2644 status = do_list(deltree_list_iter->file_path,
2645 attribute,
2646 do_deltree_list,
2647 true,
2648 true);
2649 if (!NT_STATUS_IS_OK(status)) {
2650 goto err;
2652 status = delete_remote_files_list(cli, deltree_list_head);
2653 free_file_list(deltree_list_head);
2654 deltree_list_head = NULL;
2655 if (!NT_STATUS_IS_OK(status)) {
2656 goto err;
2660 free_file_list(deltree_list_norecurse);
2661 free_file_list(deltree_list_head);
2662 return 0;
2664 err:
2666 free_file_list(deltree_list_norecurse);
2667 free_file_list(deltree_list_head);
2668 deltree_list_head = NULL;
2669 return 1;
2673 /****************************************************************************
2674 Wildcard delete some files.
2675 ****************************************************************************/
2677 static int cmd_wdel(void)
2679 TALLOC_CTX *ctx = talloc_tos();
2680 char *mask = NULL;
2681 char *buf = NULL;
2682 uint32_t attribute;
2683 struct cli_state *targetcli;
2684 char *targetname = NULL;
2685 struct cli_credentials *creds = samba_cmdline_get_creds();
2686 NTSTATUS status;
2688 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2689 d_printf("wdel 0x<attrib> <wcard>\n");
2690 return 1;
2693 attribute = (uint32_t)strtol(buf, (char **)NULL, 16);
2695 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2696 d_printf("wdel 0x<attrib> <wcard>\n");
2697 return 1;
2700 mask = talloc_asprintf(ctx, "%s%s",
2701 client_get_cur_dir(),
2702 buf);
2703 if (!mask) {
2704 return 1;
2706 mask = client_clean_name(ctx, mask);
2707 if (mask == NULL) {
2708 return 1;
2711 status = cli_resolve_path(ctx, "",
2712 creds,
2713 cli, mask, &targetcli, &targetname);
2714 if (!NT_STATUS_IS_OK(status)) {
2715 d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
2716 return 1;
2719 status = cli_unlink(targetcli, targetname, attribute);
2720 if (!NT_STATUS_IS_OK(status)) {
2721 d_printf("%s deleting remote files %s\n", nt_errstr(status),
2722 targetname);
2724 return 0;
2727 /****************************************************************************
2728 ****************************************************************************/
2730 static int cmd_open(void)
2732 TALLOC_CTX *ctx = talloc_tos();
2733 char *mask = NULL;
2734 char *buf = NULL;
2735 char *targetname = NULL;
2736 struct cli_state *targetcli;
2737 uint16_t fnum = (uint16_t)-1;
2738 struct cli_credentials *creds = samba_cmdline_get_creds();
2739 NTSTATUS status;
2741 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2742 d_printf("open <filename>\n");
2743 return 1;
2745 mask = talloc_asprintf(ctx,
2746 "%s%s",
2747 client_get_cur_dir(),
2748 buf);
2749 if (!mask) {
2750 return 1;
2753 mask = client_clean_name(ctx, mask);
2754 if (mask == NULL) {
2755 return 1;
2758 status = cli_resolve_path(ctx, "",
2759 creds,
2760 cli, mask, &targetcli, &targetname);
2761 if (!NT_STATUS_IS_OK(status)) {
2762 d_printf("open %s: %s\n", mask, nt_errstr(status));
2763 return 1;
2766 status = cli_ntcreate(targetcli, targetname, 0,
2767 FILE_READ_DATA|FILE_WRITE_DATA, 0,
2768 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2769 0x0, 0x0, &fnum, NULL);
2770 if (!NT_STATUS_IS_OK(status)) {
2771 status = cli_ntcreate(targetcli, targetname, 0,
2772 FILE_READ_DATA, 0,
2773 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
2774 0x0, 0x0, &fnum, NULL);
2775 if (NT_STATUS_IS_OK(status)) {
2776 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2777 } else {
2778 d_printf("Failed to open file %s. %s\n",
2779 targetname, nt_errstr(status));
2781 } else {
2782 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2784 return 0;
2787 static int cmd_posix_encrypt(void)
2789 TALLOC_CTX *ctx = talloc_tos();
2790 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2791 char *domain = NULL;
2792 char *user = NULL;
2793 char *password = NULL;
2794 struct cli_credentials *creds = NULL;
2795 struct cli_credentials *lcreds = NULL;
2797 if (next_token_talloc(ctx, &cmd_ptr, &domain, NULL)) {
2799 if (!next_token_talloc(ctx, &cmd_ptr, &user, NULL)) {
2800 d_printf("posix_encrypt domain user password\n");
2801 return 1;
2804 if (!next_token_talloc(ctx, &cmd_ptr, &password, NULL)) {
2805 d_printf("posix_encrypt domain user password\n");
2806 return 1;
2809 lcreds = cli_session_creds_init(ctx,
2810 user,
2811 domain,
2812 NULL, /* realm */
2813 password,
2814 false, /* use_kerberos */
2815 false, /* fallback_after_kerberos */
2816 false, /* use_ccache */
2817 false); /* password_is_nt_hash */
2818 if (lcreds == NULL) {
2819 d_printf("cli_session_creds_init() failed.\n");
2820 return -1;
2822 creds = lcreds;
2823 } else {
2824 bool auth_requested = false;
2826 creds = samba_cmdline_get_creds();
2828 auth_requested = cli_credentials_authentication_requested(creds);
2829 if (!auth_requested) {
2830 d_printf("posix_encrypt domain user password\n");
2831 return 1;
2835 status = cli_smb1_setup_encryption(cli, creds);
2836 /* gensec currently references the creds so we can't free them here */
2837 talloc_unlink(ctx, lcreds);
2838 if (!NT_STATUS_IS_OK(status)) {
2839 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2840 } else {
2841 bool ok;
2843 d_printf("encryption on\n");
2844 ok = cli_credentials_set_smb_encryption(creds,
2845 SMB_ENCRYPTION_REQUIRED,
2846 CRED_SPECIFIED);
2847 SMB_ASSERT(ok);
2850 return 0;
2853 /****************************************************************************
2854 ****************************************************************************/
2856 static int cmd_posix_open(void)
2858 TALLOC_CTX *ctx = talloc_tos();
2859 char *mask = NULL;
2860 char *buf = NULL;
2861 char *targetname = NULL;
2862 struct cli_state *targetcli;
2863 mode_t mode;
2864 uint16_t fnum;
2865 struct cli_credentials *creds = samba_cmdline_get_creds();
2866 NTSTATUS status;
2868 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2869 d_printf("posix_open <filename> 0<mode>\n");
2870 return 1;
2872 mask = talloc_asprintf(ctx,
2873 "%s%s",
2874 client_get_cur_dir(),
2875 buf);
2876 if (!mask) {
2877 return 1;
2879 mask = client_clean_name(ctx, mask);
2880 if (mask == NULL) {
2881 return 1;
2884 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2885 d_printf("posix_open <filename> 0<mode>\n");
2886 return 1;
2888 if (CLI_DIRSEP_CHAR != '/') {
2889 d_printf("Command \"posix\" must be issued before "
2890 "the \"posix_open\" command can be used.\n");
2891 return 1;
2893 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2895 status = cli_resolve_path(ctx, "",
2896 creds,
2897 cli, mask, &targetcli, &targetname);
2898 if (!NT_STATUS_IS_OK(status)) {
2899 d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
2900 return 1;
2903 status = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode,
2904 &fnum);
2905 if (!NT_STATUS_IS_OK(status)) {
2906 status = cli_posix_open(targetcli, targetname,
2907 O_CREAT|O_RDONLY, mode, &fnum);
2908 if (!NT_STATUS_IS_OK(status)) {
2909 d_printf("Failed to open file %s. %s\n", targetname,
2910 nt_errstr(status));
2911 } else {
2912 d_printf("posix_open file %s: for readonly fnum %d\n",
2913 targetname, fnum);
2915 } else {
2916 d_printf("posix_open file %s: for read/write fnum %d\n",
2917 targetname, fnum);
2920 return 0;
2923 static int cmd_posix_mkdir(void)
2925 TALLOC_CTX *ctx = talloc_tos();
2926 char *mask = NULL;
2927 char *buf = NULL;
2928 char *targetname = NULL;
2929 struct cli_state *targetcli;
2930 mode_t mode;
2931 struct cli_credentials *creds = samba_cmdline_get_creds();
2932 NTSTATUS status;
2934 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2935 d_printf("posix_mkdir <filename> 0<mode>\n");
2936 return 1;
2938 mask = talloc_asprintf(ctx,
2939 "%s%s",
2940 client_get_cur_dir(),
2941 buf);
2942 if (!mask) {
2943 return 1;
2945 mask = client_clean_name(ctx, mask);
2946 if (mask == NULL) {
2947 return 1;
2950 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2951 d_printf("posix_mkdir <filename> 0<mode>\n");
2952 return 1;
2954 if (CLI_DIRSEP_CHAR != '/') {
2955 d_printf("Command \"posix\" must be issued before "
2956 "the \"posix_mkdir\" command can be used.\n");
2957 return 1;
2959 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2961 status = cli_resolve_path(ctx, "",
2962 creds,
2963 cli, mask, &targetcli, &targetname);
2964 if (!NT_STATUS_IS_OK(status)) {
2965 d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
2966 return 1;
2969 status = cli_posix_mkdir(targetcli, targetname, mode);
2970 if (!NT_STATUS_IS_OK(status)) {
2971 d_printf("Failed to open file %s. %s\n",
2972 targetname, nt_errstr(status));
2973 } else {
2974 d_printf("posix_mkdir created directory %s\n", targetname);
2976 return 0;
2979 static int cmd_posix_unlink(void)
2981 TALLOC_CTX *ctx = talloc_tos();
2982 char *mask = NULL;
2983 char *buf = NULL;
2984 char *targetname = NULL;
2985 struct cli_state *targetcli;
2986 struct cli_credentials *creds = samba_cmdline_get_creds();
2987 NTSTATUS status;
2989 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2990 d_printf("posix_unlink <filename>\n");
2991 return 1;
2993 if (CLI_DIRSEP_CHAR != '/') {
2994 d_printf("Command \"posix\" must be issued before "
2995 "the \"posix_unlink\" command can be used.\n");
2996 return 1;
2998 mask = talloc_asprintf(ctx,
2999 "%s%s",
3000 client_get_cur_dir(),
3001 buf);
3002 if (!mask) {
3003 return 1;
3005 mask = client_clean_name(ctx, mask);
3006 if (mask == NULL) {
3007 return 1;
3010 status = cli_resolve_path(ctx, "",
3011 creds,
3012 cli, mask, &targetcli, &targetname);
3013 if (!NT_STATUS_IS_OK(status)) {
3014 d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
3015 return 1;
3018 status = cli_posix_unlink(targetcli, targetname);
3019 if (!NT_STATUS_IS_OK(status)) {
3020 d_printf("Failed to unlink file %s. %s\n",
3021 targetname, nt_errstr(status));
3022 } else {
3023 d_printf("posix_unlink deleted file %s\n", targetname);
3026 return 0;
3029 static int cmd_posix_rmdir(void)
3031 TALLOC_CTX *ctx = talloc_tos();
3032 char *mask = NULL;
3033 char *buf = NULL;
3034 char *targetname = NULL;
3035 struct cli_state *targetcli;
3036 struct cli_credentials *creds = samba_cmdline_get_creds();
3037 NTSTATUS status;
3039 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3040 d_printf("posix_rmdir <filename>\n");
3041 return 1;
3043 if (CLI_DIRSEP_CHAR != '/') {
3044 d_printf("Command \"posix\" must be issued before "
3045 "the \"posix_rmdir\" command can be used.\n");
3046 return 1;
3048 mask = talloc_asprintf(ctx,
3049 "%s%s",
3050 client_get_cur_dir(),
3051 buf);
3052 if (!mask) {
3053 return 1;
3055 mask = client_clean_name(ctx, mask);
3056 if (mask == NULL) {
3057 return 1;
3060 status = cli_resolve_path(ctx, "",
3061 creds,
3062 cli, mask, &targetcli, &targetname);
3063 if (!NT_STATUS_IS_OK(status)) {
3064 d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
3065 return 1;
3068 status = cli_posix_rmdir(targetcli, targetname);
3069 if (!NT_STATUS_IS_OK(status)) {
3070 d_printf("Failed to unlink directory %s. %s\n",
3071 targetname, nt_errstr(status));
3072 } else {
3073 d_printf("posix_rmdir deleted directory %s\n", targetname);
3076 return 0;
3079 static int cmd_mkfifo(void)
3081 TALLOC_CTX *ctx = talloc_tos();
3082 char *mask = NULL;
3083 char *buf = NULL;
3084 char *targetname = NULL;
3085 struct cli_state *targetcli;
3086 mode_t mode;
3087 struct cli_credentials *creds = samba_cmdline_get_creds();
3088 NTSTATUS status;
3090 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3091 d_printf("mkfifo <filename> 0<mode>\n");
3092 return 1;
3094 mask = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(), buf);
3095 if (!mask) {
3096 return 1;
3098 mask = client_clean_name(ctx, mask);
3099 if (mask == NULL) {
3100 return 1;
3103 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3104 d_printf("mkfifo <filename> 0<mode>\n");
3105 return 1;
3108 mode = (mode_t)strtol(buf, (char **)NULL, 8);
3109 if ((mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) != 0) {
3110 d_printf("mode %o can only contain permission bits\n", mode);
3111 return 1;
3114 status = cli_resolve_path(ctx,
3116 creds,
3117 cli,
3118 mask,
3119 &targetcli,
3120 &targetname);
3121 if (!NT_STATUS_IS_OK(status)) {
3122 d_printf("mkfifo %s: %s\n", mask, nt_errstr(status));
3123 return 1;
3126 status = cli_mknod(targetcli, targetname, mode | S_IFIFO, 0);
3127 if (!NT_STATUS_IS_OK(status)) {
3128 d_printf("Failed to open file %s. %s\n",
3129 targetname,
3130 nt_errstr(status));
3131 } else {
3132 d_printf("mkfifo created %s\n", targetname);
3134 return 0;
3137 static int cmd_close(void)
3139 TALLOC_CTX *ctx = talloc_tos();
3140 char *buf = NULL;
3141 int fnum;
3142 NTSTATUS status;
3144 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3145 d_printf("close <fnum>\n");
3146 return 1;
3149 fnum = atoi(buf);
3150 /* We really should use the targetcli here.... */
3151 status = cli_close(cli, fnum);
3152 if (!NT_STATUS_IS_OK(status)) {
3153 d_printf("close %d: %s\n", fnum, nt_errstr(status));
3154 return 1;
3156 return 0;
3159 static int cmd_posix(void)
3161 TALLOC_CTX *ctx = talloc_tos();
3162 uint16_t major, minor;
3163 uint32_t caplow, caphigh;
3164 char *caps;
3165 NTSTATUS status;
3167 if (!smbXcli_conn_have_posix(cli->conn)) {
3168 d_printf("Server doesn't support UNIX CIFS extensions.\n");
3169 return 1;
3172 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB3_11) {
3173 cli->smb2.client_smb311_posix = true;
3174 return 0;
3177 status = cli_unix_extensions_version(cli, &major, &minor, &caplow,
3178 &caphigh);
3179 if (!NT_STATUS_IS_OK(status)) {
3180 d_printf("Can't get UNIX CIFS extensions version from "
3181 "server: %s\n", nt_errstr(status));
3182 return 1;
3185 d_printf("Server supports CIFS extensions %" PRIu16 ".%" PRIu16 "\n",
3186 major,
3187 minor);
3189 caps = talloc_strdup(ctx, "");
3190 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
3191 talloc_asprintf_addbuf(&caps, "locks ");
3193 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
3194 talloc_asprintf_addbuf(&caps, "acls ");
3196 if (caplow & CIFS_UNIX_XATTTR_CAP) {
3197 talloc_asprintf_addbuf(&caps, "eas ");
3199 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3200 talloc_asprintf_addbuf(&caps, "pathnames ");
3202 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
3203 talloc_asprintf_addbuf(&caps, "posix_path_operations ");
3205 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
3206 talloc_asprintf_addbuf(&caps, "large_read ");
3208 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
3209 talloc_asprintf_addbuf(&caps, "large_write ");
3211 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
3212 talloc_asprintf_addbuf(&caps, "posix_encrypt ");
3214 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
3215 talloc_asprintf_addbuf(&caps, "mandatory_posix_encrypt ");
3218 if (caps == NULL) {
3219 return 1;
3222 if (*caps && caps[strlen(caps)-1] == ' ') {
3223 caps[strlen(caps)-1] = '\0';
3226 d_printf("Server supports CIFS capabilities %s\n", caps);
3228 status = cli_set_unix_extensions_capabilities(cli, major, minor,
3229 caplow, caphigh);
3230 if (!NT_STATUS_IS_OK(status)) {
3231 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n",
3232 nt_errstr(status));
3233 return 1;
3236 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3237 CLI_DIRSEP_CHAR = '/';
3238 *CLI_DIRSEP_STR = '/';
3239 client_set_cur_dir(CLI_DIRSEP_STR);
3242 return 0;
3245 static int cmd_lock(void)
3247 TALLOC_CTX *ctx = talloc_tos();
3248 char *buf = NULL;
3249 uint64_t start, len;
3250 enum brl_type lock_type;
3251 int fnum;
3252 NTSTATUS status;
3254 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3255 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3256 return 1;
3258 fnum = atoi(buf);
3260 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3261 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3262 return 1;
3265 if (*buf == 'r' || *buf == 'R') {
3266 lock_type = READ_LOCK;
3267 } else if (*buf == 'w' || *buf == 'W') {
3268 lock_type = WRITE_LOCK;
3269 } else {
3270 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3271 return 1;
3274 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3275 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3276 return 1;
3279 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3281 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3282 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
3283 return 1;
3286 if (CLI_DIRSEP_CHAR != '/') {
3287 d_printf("Command \"posix\" must be issued before "
3288 "the \"lock\" command can be used.\n");
3289 return 1;
3292 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3294 status = cli_posix_lock(cli, fnum, start, len, true, lock_type);
3295 if (!NT_STATUS_IS_OK(status)) {
3296 d_printf("lock failed %d: %s\n", fnum, nt_errstr(status));
3299 return 0;
3302 static int cmd_unlock(void)
3304 TALLOC_CTX *ctx = talloc_tos();
3305 char *buf = NULL;
3306 uint64_t start, len;
3307 int fnum;
3308 NTSTATUS status;
3310 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3311 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3312 return 1;
3314 fnum = atoi(buf);
3316 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3317 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3318 return 1;
3321 start = (uint64_t)strtol(buf, (char **)NULL, 16);
3323 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3324 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
3325 return 1;
3328 if (CLI_DIRSEP_CHAR != '/') {
3329 d_printf("Command \"posix\" must be issued before "
3330 "the \"unlock\" command can be used.\n");
3331 return 1;
3334 len = (uint64_t)strtol(buf, (char **)NULL, 16);
3336 status = cli_posix_unlock(cli, fnum, start, len);
3337 if (!NT_STATUS_IS_OK(status)) {
3338 d_printf("unlock failed %d: %s\n", fnum, nt_errstr(status));
3341 return 0;
3344 static int cmd_posix_whoami(void)
3346 TALLOC_CTX *ctx = talloc_tos();
3347 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
3348 uint64_t uid = 0;
3349 uint64_t gid = 0;
3350 uint32_t num_gids = 0;
3351 uint32_t num_sids = 0;
3352 uint64_t *gids = NULL;
3353 struct dom_sid *sids = NULL;
3354 bool guest = false;
3355 uint32_t i;
3357 if (CLI_DIRSEP_CHAR != '/') {
3358 d_printf("Command \"posix\" must be issued before "
3359 "the \"posix_whoami\" command can be used.\n");
3360 return 1;
3363 status = cli_posix_whoami(cli,
3364 ctx,
3365 &uid,
3366 &gid,
3367 &num_gids,
3368 &gids,
3369 &num_sids,
3370 &sids,
3371 &guest);
3373 if (!NT_STATUS_IS_OK(status)) {
3374 d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
3375 return 1;
3378 d_printf("GUEST:%s\n", guest ? "True" : "False");
3379 d_printf("UID:%" PRIu64 "\n", uid);
3380 d_printf("GID:%" PRIu64 "\n", gid);
3381 d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
3382 for (i = 0; i < num_gids; i++) {
3383 d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
3385 d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
3386 for (i = 0; i < num_sids; i++) {
3387 struct dom_sid_buf buf;
3388 d_printf("SIDS[%" PRIu32 "]:%s\n",
3390 dom_sid_str_buf(&sids[i], &buf));
3392 return 0;
3396 /****************************************************************************
3397 Remove a directory.
3398 ****************************************************************************/
3400 static int cmd_rmdir(void)
3402 TALLOC_CTX *ctx = talloc_tos();
3403 char *mask = NULL;
3404 char *buf = NULL;
3405 char *targetname = NULL;
3406 struct cli_state *targetcli;
3407 struct cli_credentials *creds = samba_cmdline_get_creds();
3408 NTSTATUS status;
3410 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3411 d_printf("rmdir <dirname>\n");
3412 return 1;
3414 mask = talloc_asprintf(ctx,
3415 "%s%s",
3416 client_get_cur_dir(),
3417 buf);
3418 if (!mask) {
3419 return 1;
3421 mask = client_clean_name(ctx, mask);
3422 if (mask == NULL) {
3423 return 1;
3426 status = cli_resolve_path(ctx, "",
3427 creds,
3428 cli, mask, &targetcli, &targetname);
3429 if (!NT_STATUS_IS_OK(status)) {
3430 d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
3431 return 1;
3434 status = cli_rmdir(targetcli, targetname);
3435 if (!NT_STATUS_IS_OK(status)) {
3436 d_printf("%s removing remote directory file %s\n",
3437 nt_errstr(status), mask);
3440 return 0;
3443 /****************************************************************************
3444 UNIX hardlink.
3445 ****************************************************************************/
3447 static int cmd_link(void)
3449 TALLOC_CTX *ctx = talloc_tos();
3450 char *oldname = NULL;
3451 char *newname = NULL;
3452 char *buf = NULL;
3453 char *buf2 = NULL;
3454 char *targetname = NULL;
3455 struct cli_state *targetcli;
3456 struct cli_credentials *creds = samba_cmdline_get_creds();
3457 NTSTATUS status;
3459 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3460 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3461 d_printf("link <oldname> <newname>\n");
3462 return 1;
3464 oldname = talloc_asprintf(ctx,
3465 "%s%s",
3466 client_get_cur_dir(),
3467 buf);
3468 if (!oldname) {
3469 return 1;
3471 oldname = client_clean_name(ctx, oldname);
3472 if (oldname == NULL) {
3473 return 1;
3475 newname = talloc_asprintf(ctx,
3476 "%s%s",
3477 client_get_cur_dir(),
3478 buf2);
3479 if (!newname) {
3480 return 1;
3482 newname = client_clean_name(ctx, newname);
3483 if (newname == NULL) {
3484 return 1;
3487 status = cli_resolve_path(ctx, "",
3488 creds,
3489 cli, oldname, &targetcli, &targetname);
3490 if (!NT_STATUS_IS_OK(status)) {
3491 d_printf("link %s: %s\n", oldname, nt_errstr(status));
3492 return 1;
3495 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3496 d_printf("Server doesn't support UNIX CIFS calls.\n");
3497 return 1;
3500 if (CLI_DIRSEP_CHAR != '/') {
3501 d_printf("Command \"posix\" must be issued before "
3502 "the \"link\" command can be used.\n");
3503 return 1;
3506 status = cli_posix_hardlink(targetcli, targetname, newname);
3507 if (!NT_STATUS_IS_OK(status)) {
3508 d_printf("%s linking files (%s -> %s)\n",
3509 nt_errstr(status), newname, oldname);
3510 return 1;
3512 return 0;
3515 /****************************************************************************
3516 UNIX readlink.
3517 ****************************************************************************/
3519 static int cmd_readlink(void)
3521 TALLOC_CTX *ctx = talloc_tos();
3522 char *name= NULL;
3523 char *buf = NULL;
3524 char *targetname = NULL;
3525 char *linkname = NULL;
3526 char *printname = NULL;
3527 uint32_t flags;
3528 struct cli_state *targetcli;
3529 struct cli_credentials *creds = samba_cmdline_get_creds();
3530 NTSTATUS status;
3532 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3533 d_printf("readlink <name>\n");
3534 return 1;
3536 name = talloc_asprintf(ctx,
3537 "%s%s",
3538 client_get_cur_dir(),
3539 buf);
3540 if (!name) {
3541 return 1;
3543 name = client_clean_name(ctx, name);
3544 if (name == NULL) {
3545 return 1;
3548 status = cli_resolve_path(ctx, "",
3549 creds,
3550 cli, name, &targetcli, &targetname);
3551 if (!NT_STATUS_IS_OK(status)) {
3552 d_printf("readlink %s: %s\n", name, nt_errstr(status));
3553 return 1;
3556 status = cli_readlink(
3557 cli, name, talloc_tos(), &linkname, &printname, &flags);
3558 if (!NT_STATUS_IS_OK(status)) {
3559 d_printf("%s readlink on file %s\n",
3560 nt_errstr(status), name);
3561 return 1;
3564 d_printf("%s -> %s\n", name, linkname);
3566 TALLOC_FREE(linkname);
3568 return 0;
3572 /****************************************************************************
3573 UNIX symlink.
3574 ****************************************************************************/
3576 static int cmd_symlink(void)
3578 TALLOC_CTX *ctx = talloc_tos();
3579 char *link_target = NULL;
3580 char *newname = NULL;
3581 char *buf = NULL;
3582 char *buf2 = NULL;
3583 struct cli_state *newcli;
3584 struct cli_credentials *creds = samba_cmdline_get_creds();
3585 NTSTATUS status;
3587 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3588 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3589 d_printf("symlink <link_target> <newname>\n");
3590 return 1;
3592 /* Oldname (link target) must be an untouched blob. */
3593 link_target = buf;
3595 if (SERVER_HAS_UNIX_CIFS(cli)) {
3596 if (CLI_DIRSEP_CHAR != '/') {
3597 d_printf("Command \"posix\" must be issued before "
3598 "the \"symlink\" command can be used.\n");
3599 return 1;
3601 newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
3602 buf2);
3603 if (!newname) {
3604 return 1;
3606 newname = client_clean_name(ctx, newname);
3607 if (newname == NULL) {
3608 return 1;
3610 /* New name must be present in share namespace. */
3611 status = cli_resolve_path(ctx, "",
3612 creds,
3613 cli, newname,
3614 &newcli, &newname);
3615 if (!NT_STATUS_IS_OK(status)) {
3616 d_printf("link %s: %s\n", newname,
3617 nt_errstr(status));
3618 return 1;
3620 status = cli_posix_symlink(newcli, link_target, newname);
3621 } else {
3622 status = cli_symlink(
3623 cli, link_target, buf2,
3624 buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
3627 if (!NT_STATUS_IS_OK(status)) {
3628 d_printf("%s symlinking files (%s -> %s)\n",
3629 nt_errstr(status), newname, link_target);
3630 return 1;
3633 return 0;
3636 /****************************************************************************
3637 UNIX chmod.
3638 ****************************************************************************/
3640 static int cmd_chmod(void)
3642 TALLOC_CTX *ctx = talloc_tos();
3643 char *src = NULL;
3644 char *buf = NULL;
3645 char *buf2 = NULL;
3646 char *targetname = NULL;
3647 struct cli_state *targetcli;
3648 mode_t mode;
3649 struct cli_credentials *creds = samba_cmdline_get_creds();
3650 NTSTATUS status;
3652 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3653 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3654 d_printf("chmod mode file\n");
3655 return 1;
3657 src = talloc_asprintf(ctx,
3658 "%s%s",
3659 client_get_cur_dir(),
3660 buf2);
3661 if (!src) {
3662 return 1;
3664 src = client_clean_name(ctx, src);
3665 if (src == NULL) {
3666 return 1;
3669 mode = (mode_t)strtol(buf, NULL, 8);
3671 status = cli_resolve_path(ctx, "",
3672 creds,
3673 cli, src, &targetcli, &targetname);
3674 if (!NT_STATUS_IS_OK(status)) {
3675 d_printf("chmod %s: %s\n", src, nt_errstr(status));
3676 return 1;
3679 if (CLI_DIRSEP_CHAR != '/') {
3680 d_printf("Command \"posix\" must be issued before "
3681 "the \"chmod\" command can be used.\n");
3682 return 1;
3685 status = cli_chmod(targetcli, targetname, mode);
3686 if (!NT_STATUS_IS_OK(status)) {
3687 d_printf("%s chmod file %s 0%o\n",
3688 nt_errstr(status), src, (unsigned int)mode);
3689 return 1;
3692 return 0;
3695 static const char *filetype_to_str(mode_t mode)
3697 if (S_ISREG(mode)) {
3698 return "regular file";
3699 } else if (S_ISDIR(mode)) {
3700 return "directory";
3701 } else
3702 #ifdef S_ISCHR
3703 if (S_ISCHR(mode)) {
3704 return "character device";
3705 } else
3706 #endif
3707 #ifdef S_ISBLK
3708 if (S_ISBLK(mode)) {
3709 return "block device";
3710 } else
3711 #endif
3712 #ifdef S_ISFIFO
3713 if (S_ISFIFO(mode)) {
3714 return "fifo";
3715 } else
3716 #endif
3717 #ifdef S_ISLNK
3718 if (S_ISLNK(mode)) {
3719 return "symbolic link";
3720 } else
3721 #endif
3722 #ifdef S_ISSOCK
3723 if (S_ISSOCK(mode)) {
3724 return "socket";
3725 } else
3726 #endif
3727 return "";
3730 static char rwx_to_str(mode_t m, mode_t bt, char ret)
3732 if (m & bt) {
3733 return ret;
3734 } else {
3735 return '-';
3739 static char *unix_mode_to_str(char *s, mode_t m)
3741 char *p = s;
3742 const char *str = filetype_to_str(m);
3744 switch(str[0]) {
3745 case 'd':
3746 *p++ = 'd';
3747 break;
3748 case 'c':
3749 *p++ = 'c';
3750 break;
3751 case 'b':
3752 *p++ = 'b';
3753 break;
3754 case 'f':
3755 *p++ = 'p';
3756 break;
3757 case 's':
3758 *p++ = str[1] == 'y' ? 'l' : 's';
3759 break;
3760 case 'r':
3761 default:
3762 *p++ = '-';
3763 break;
3765 *p++ = rwx_to_str(m, S_IRUSR, 'r');
3766 *p++ = rwx_to_str(m, S_IWUSR, 'w');
3767 *p++ = rwx_to_str(m, S_IXUSR, 'x');
3768 *p++ = rwx_to_str(m, S_IRGRP, 'r');
3769 *p++ = rwx_to_str(m, S_IWGRP, 'w');
3770 *p++ = rwx_to_str(m, S_IXGRP, 'x');
3771 *p++ = rwx_to_str(m, S_IROTH, 'r');
3772 *p++ = rwx_to_str(m, S_IWOTH, 'w');
3773 *p++ = rwx_to_str(m, S_IXOTH, 'x');
3774 *p++ = '\0';
3775 return s;
3778 /****************************************************************************
3779 Utility function for UNIX getfacl.
3780 ****************************************************************************/
3782 static char *perms_to_string(fstring permstr, unsigned char perms)
3784 fstrcpy(permstr, "---");
3785 if (perms & SMB_POSIX_ACL_READ) {
3786 permstr[0] = 'r';
3788 if (perms & SMB_POSIX_ACL_WRITE) {
3789 permstr[1] = 'w';
3791 if (perms & SMB_POSIX_ACL_EXECUTE) {
3792 permstr[2] = 'x';
3794 return permstr;
3797 /****************************************************************************
3798 UNIX getfacl.
3799 ****************************************************************************/
3801 static int cmd_getfacl(void)
3803 TALLOC_CTX *ctx = talloc_tos();
3804 char *src = NULL;
3805 char *name = NULL;
3806 char *targetname = NULL;
3807 struct cli_state *targetcli;
3808 uint16_t major, minor;
3809 uint32_t caplow, caphigh;
3810 char *retbuf = NULL;
3811 size_t rb_size = 0;
3812 SMB_STRUCT_STAT sbuf;
3813 size_t num_file_acls = 0;
3814 size_t num_dir_acls = 0;
3815 size_t expected_buflen;
3816 uint16_t i;
3817 struct cli_credentials *creds = samba_cmdline_get_creds();
3818 NTSTATUS status;
3820 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3821 d_printf("getfacl filename\n");
3822 return 1;
3824 src = talloc_asprintf(ctx,
3825 "%s%s",
3826 client_get_cur_dir(),
3827 name);
3828 if (!src) {
3829 return 1;
3831 src = client_clean_name(ctx, src);
3832 if (src == NULL) {
3833 return 1;
3836 status = cli_resolve_path(ctx, "",
3837 creds,
3838 cli, src, &targetcli, &targetname);
3839 if (!NT_STATUS_IS_OK(status)) {
3840 d_printf("stat %s: %s\n", src, nt_errstr(status));
3841 return 1;
3844 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3845 d_printf("Server doesn't support UNIX CIFS calls.\n");
3846 return 1;
3849 if (CLI_DIRSEP_CHAR != '/') {
3850 d_printf("Command \"posix\" must be issued before "
3851 "the \"getfacl\" command can be used.\n");
3852 return 1;
3855 status = cli_unix_extensions_version(targetcli, &major, &minor,
3856 &caplow, &caphigh);
3857 if (!NT_STATUS_IS_OK(status)) {
3858 d_printf("Can't get UNIX CIFS version from server: %s.\n",
3859 nt_errstr(status));
3860 return 1;
3863 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
3864 d_printf("This server supports UNIX extensions "
3865 "but doesn't support POSIX ACLs.\n");
3866 return 1;
3869 status = cli_posix_stat(targetcli, targetname, &sbuf);
3870 if (!NT_STATUS_IS_OK(status)) {
3871 d_printf("%s getfacl doing a stat on file %s\n",
3872 nt_errstr(status), src);
3873 return 1;
3876 status = cli_posix_getacl(targetcli, targetname, ctx, &rb_size, &retbuf);
3877 if (!NT_STATUS_IS_OK(status)) {
3878 d_printf("%s getfacl file %s\n",
3879 nt_errstr(status), src);
3880 return 1;
3883 /* ToDo : Print out the ACL values. */
3884 if (rb_size < 6 || SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION) {
3885 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
3886 src, (unsigned int)CVAL(retbuf,0) );
3887 return 1;
3890 num_file_acls = SVAL(retbuf,2);
3891 num_dir_acls = SVAL(retbuf,4);
3894 * No overflow check, num_*_acls comes from a 16-bit value,
3895 * and we expect expected_buflen (size_t) to be of at least 32
3896 * bit.
3898 expected_buflen = SMB_POSIX_ACL_HEADER_SIZE +
3899 SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls);
3901 if (rb_size != expected_buflen) {
3902 d_printf("getfacl file %s, incorrect POSIX acl buffer size "
3903 "(should be %zu, was %zu).\n",
3904 src,
3905 expected_buflen,
3906 rb_size);
3907 return 1;
3910 d_printf("# file: %s\n", src);
3911 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_ex_uid, (unsigned int)sbuf.st_ex_gid);
3913 if (num_file_acls == 0 && num_dir_acls == 0) {
3914 d_printf("No acls found.\n");
3917 for (i = 0; i < num_file_acls; i++) {
3918 uint32_t uorg;
3919 fstring permstring;
3920 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3921 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3923 switch(tagtype) {
3924 case SMB_POSIX_ACL_USER_OBJ:
3925 d_printf("user::");
3926 break;
3927 case SMB_POSIX_ACL_USER:
3928 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3929 d_printf("user:%u:", uorg);
3930 break;
3931 case SMB_POSIX_ACL_GROUP_OBJ:
3932 d_printf("group::");
3933 break;
3934 case SMB_POSIX_ACL_GROUP:
3935 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3936 d_printf("group:%u:", uorg);
3937 break;
3938 case SMB_POSIX_ACL_MASK:
3939 d_printf("mask::");
3940 break;
3941 case SMB_POSIX_ACL_OTHER:
3942 d_printf("other::");
3943 break;
3944 default:
3945 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3946 src, (unsigned int)tagtype );
3947 SAFE_FREE(retbuf);
3948 return 1;
3951 d_printf("%s\n", perms_to_string(permstring, perms));
3954 for (i = 0; i < num_dir_acls; i++) {
3955 uint32_t uorg;
3956 fstring permstring;
3957 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3958 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3960 switch(tagtype) {
3961 case SMB_POSIX_ACL_USER_OBJ:
3962 d_printf("default:user::");
3963 break;
3964 case SMB_POSIX_ACL_USER:
3965 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3966 d_printf("default:user:%u:", uorg);
3967 break;
3968 case SMB_POSIX_ACL_GROUP_OBJ:
3969 d_printf("default:group::");
3970 break;
3971 case SMB_POSIX_ACL_GROUP:
3972 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3973 d_printf("default:group:%u:", uorg);
3974 break;
3975 case SMB_POSIX_ACL_MASK:
3976 d_printf("default:mask::");
3977 break;
3978 case SMB_POSIX_ACL_OTHER:
3979 d_printf("default:other::");
3980 break;
3981 default:
3982 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3983 src, (unsigned int)tagtype );
3984 SAFE_FREE(retbuf);
3985 return 1;
3988 d_printf("%s\n", perms_to_string(permstring, perms));
3991 return 0;
3994 /****************************************************************************
3995 Get the EA list of a file
3996 ****************************************************************************/
3998 static int cmd_geteas(void)
4000 TALLOC_CTX *ctx = talloc_tos();
4001 char *src = NULL;
4002 char *name = NULL;
4003 char *targetname = NULL;
4004 struct cli_state *targetcli;
4005 NTSTATUS status;
4006 size_t i, num_eas;
4007 struct ea_struct *eas;
4008 struct cli_credentials *creds = samba_cmdline_get_creds();
4010 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
4011 d_printf("geteas filename\n");
4012 return 1;
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_get_ea_list_path(targetcli, targetname, talloc_tos(),
4035 &num_eas, &eas);
4036 if (!NT_STATUS_IS_OK(status)) {
4037 d_printf("cli_get_ea_list_path: %s\n", nt_errstr(status));
4038 return 1;
4041 for (i=0; i<num_eas; i++) {
4042 d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
4043 dump_data_file(eas[i].value.data, eas[i].value.length, false,
4044 stdout);
4045 d_printf("\n");
4048 TALLOC_FREE(eas);
4050 return 0;
4053 /****************************************************************************
4054 Set an EA of a file
4055 ****************************************************************************/
4057 static int cmd_setea(void)
4059 TALLOC_CTX *ctx = talloc_tos();
4060 char *src = NULL;
4061 char *name = NULL;
4062 char *eaname = NULL;
4063 char *eavalue = NULL;
4064 char *targetname = NULL;
4065 struct cli_state *targetcli;
4066 struct cli_credentials *creds = samba_cmdline_get_creds();
4067 NTSTATUS status;
4069 if (!next_token_talloc(ctx, &cmd_ptr, &name, NULL)
4070 || !next_token_talloc(ctx, &cmd_ptr, &eaname, NULL)) {
4071 d_printf("setea filename eaname value\n");
4072 return 1;
4074 if (!next_token_talloc(ctx, &cmd_ptr, &eavalue, NULL)) {
4075 eavalue = talloc_strdup(ctx, "");
4077 src = talloc_asprintf(ctx,
4078 "%s%s",
4079 client_get_cur_dir(),
4080 name);
4081 if (!src) {
4082 return 1;
4084 src = client_clean_name(ctx, src);
4085 if (src == NULL) {
4086 return 1;
4089 status = cli_resolve_path(ctx, "",
4090 creds,
4091 cli, src, &targetcli, &targetname);
4092 if (!NT_STATUS_IS_OK(status)) {
4093 d_printf("stat %s: %s\n", src, nt_errstr(status));
4094 return 1;
4097 status = cli_set_ea_path(targetcli, targetname, eaname, eavalue,
4098 strlen(eavalue));
4099 if (!NT_STATUS_IS_OK(status)) {
4100 d_printf("set_ea %s: %s\n", src, nt_errstr(status));
4101 return 1;
4104 return 0;
4107 /****************************************************************************
4108 UNIX stat.
4109 ****************************************************************************/
4111 static int cmd_stat(void)
4113 TALLOC_CTX *ctx = talloc_tos();
4114 char *src = NULL;
4115 char *name = NULL;
4116 char *targetname = NULL;
4117 struct cli_state *targetcli;
4118 fstring mode_str;
4119 SMB_STRUCT_STAT sbuf;
4120 struct tm *lt;
4121 time_t tmp_time;
4122 struct cli_credentials *creds = samba_cmdline_get_creds();
4123 NTSTATUS status;
4125 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
4126 d_printf("stat file\n");
4127 return 1;
4129 src = talloc_asprintf(ctx,
4130 "%s%s",
4131 client_get_cur_dir(),
4132 name);
4133 if (!src) {
4134 return 1;
4136 src = client_clean_name(ctx, src);
4137 if (src == NULL) {
4138 return 1;
4141 status = cli_resolve_path(ctx, "",
4142 creds,
4143 cli, src, &targetcli, &targetname);
4144 if (!NT_STATUS_IS_OK(status)) {
4145 d_printf("stat %s: %s\n", src, nt_errstr(status));
4146 return 1;
4149 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4150 d_printf("Server doesn't support UNIX CIFS calls.\n");
4151 return 1;
4154 if (CLI_DIRSEP_CHAR != '/') {
4155 d_printf("Command \"posix\" must be issued before "
4156 "the \"stat\" command can be used.\n");
4157 return 1;
4160 status = cli_posix_stat(targetcli, targetname, &sbuf);
4161 if (!NT_STATUS_IS_OK(status)) {
4162 d_printf("%s stat file %s\n",
4163 nt_errstr(status), src);
4164 return 1;
4167 /* Print out the stat values. */
4168 d_printf("File: %s\n", src);
4169 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
4170 (double)sbuf.st_ex_size,
4171 (unsigned int)sbuf.st_ex_blocks,
4172 filetype_to_str(sbuf.st_ex_mode));
4174 #if defined(S_ISCHR) && defined(S_ISBLK)
4175 if (S_ISCHR(sbuf.st_ex_mode) || S_ISBLK(sbuf.st_ex_mode)) {
4176 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
4177 (double)sbuf.st_ex_ino,
4178 (unsigned int)sbuf.st_ex_nlink,
4179 unix_dev_major(sbuf.st_ex_rdev),
4180 unix_dev_minor(sbuf.st_ex_rdev));
4181 } else
4182 #endif
4183 d_printf("Inode: %.0f\tLinks: %u\n",
4184 (double)sbuf.st_ex_ino,
4185 (unsigned int)sbuf.st_ex_nlink);
4187 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
4188 ((int)sbuf.st_ex_mode & 0777),
4189 unix_mode_to_str(mode_str, sbuf.st_ex_mode),
4190 (unsigned int)sbuf.st_ex_uid,
4191 (unsigned int)sbuf.st_ex_gid);
4193 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_atime);
4194 lt = localtime(&tmp_time);
4195 if (lt) {
4196 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4197 } else {
4198 fstrcpy(mode_str, "unknown");
4200 d_printf("Access: %s\n", mode_str);
4202 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4203 lt = localtime(&tmp_time);
4204 if (lt) {
4205 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4206 } else {
4207 fstrcpy(mode_str, "unknown");
4209 d_printf("Modify: %s\n", mode_str);
4211 tmp_time = convert_timespec_to_time_t(sbuf.st_ex_ctime);
4212 lt = localtime(&tmp_time);
4213 if (lt) {
4214 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
4215 } else {
4216 fstrcpy(mode_str, "unknown");
4218 d_printf("Change: %s\n", mode_str);
4220 return 0;
4224 /****************************************************************************
4225 UNIX chown.
4226 ****************************************************************************/
4228 static int cmd_chown(void)
4230 TALLOC_CTX *ctx = talloc_tos();
4231 char *src = NULL;
4232 uid_t uid;
4233 gid_t gid;
4234 char *buf, *buf2, *buf3;
4235 struct cli_state *targetcli;
4236 char *targetname = NULL;
4237 struct cli_credentials *creds = samba_cmdline_get_creds();
4238 NTSTATUS status;
4240 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4241 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
4242 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
4243 d_printf("chown uid gid file\n");
4244 return 1;
4247 uid = (uid_t)atoi(buf);
4248 gid = (gid_t)atoi(buf2);
4250 src = talloc_asprintf(ctx,
4251 "%s%s",
4252 client_get_cur_dir(),
4253 buf3);
4254 if (!src) {
4255 return 1;
4257 src = client_clean_name(ctx, src);
4258 if (src == NULL) {
4259 return 1;
4261 status = cli_resolve_path(ctx, "",
4262 creds,
4263 cli, src, &targetcli, &targetname);
4264 if (!NT_STATUS_IS_OK(status)) {
4265 d_printf("chown %s: %s\n", src, nt_errstr(status));
4266 return 1;
4269 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
4270 d_printf("Server doesn't support UNIX CIFS calls.\n");
4271 return 1;
4274 if (CLI_DIRSEP_CHAR != '/') {
4275 d_printf("Command \"posix\" must be issued before "
4276 "the \"chown\" command can be used.\n");
4277 return 1;
4280 status = cli_posix_chown(targetcli, targetname, uid, gid);
4281 if (!NT_STATUS_IS_OK(status)) {
4282 d_printf("%s chown file %s uid=%d, gid=%d\n",
4283 nt_errstr(status), src, (int)uid, (int)gid);
4284 return 1;
4287 return 0;
4290 /****************************************************************************
4291 Rename some file.
4292 ****************************************************************************/
4294 static int cmd_rename(void)
4296 TALLOC_CTX *ctx = talloc_tos();
4297 char *src, *dest;
4298 char *buf, *buf2;
4299 struct cli_state *targetcli;
4300 char *targetsrc;
4301 char *targetdest;
4302 struct cli_credentials *creds = samba_cmdline_get_creds();
4303 NTSTATUS status;
4304 bool replace = false;
4306 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4307 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4308 d_printf("rename <src> <dest> [-f]\n");
4309 return 1;
4312 src = talloc_asprintf(ctx,
4313 "%s%s",
4314 client_get_cur_dir(),
4315 buf);
4316 if (!src) {
4317 return 1;
4319 src = client_clean_name(ctx, src);
4320 if (src == NULL) {
4321 return 1;
4324 dest = talloc_asprintf(ctx,
4325 "%s%s",
4326 client_get_cur_dir(),
4327 buf2);
4328 if (!dest) {
4329 return 1;
4331 dest = client_clean_name(ctx, dest);
4332 if (dest == NULL) {
4333 return 1;
4336 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
4337 strcsequal(buf, "-f")) {
4338 replace = true;
4341 status = cli_resolve_path(ctx, "",
4342 creds,
4343 cli, src, &targetcli, &targetsrc);
4344 if (!NT_STATUS_IS_OK(status)) {
4345 d_printf("rename %s: %s\n", src, nt_errstr(status));
4346 return 1;
4349 status = cli_resolve_path(ctx, "",
4350 creds,
4351 cli, dest, &targetcli, &targetdest);
4352 if (!NT_STATUS_IS_OK(status)) {
4353 d_printf("rename %s: %s\n", dest, nt_errstr(status));
4354 return 1;
4357 status = cli_rename(targetcli, targetsrc, targetdest, replace);
4358 if (!NT_STATUS_IS_OK(status)) {
4359 d_printf("%s renaming files %s -> %s \n",
4360 nt_errstr(status),
4361 targetsrc,
4362 targetdest);
4363 return 1;
4366 return 0;
4369 struct scopy_timing {
4370 struct timespec tp_start;
4373 static int scopy_status(off_t written, void *priv)
4375 struct timespec tp_end;
4376 unsigned int scopy_total_time_ms;
4377 struct scopy_timing *st = priv;
4379 clock_gettime_mono(&tp_end);
4380 scopy_total_time_ms = nsec_time_diff(&tp_end,&st->tp_start)/1000000;
4382 DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
4383 (intmax_t)written, written / (1.024*scopy_total_time_ms)));
4385 return true;
4388 /****************************************************************************
4389 Server-Side copy some file.
4390 ****************************************************************************/
4392 static int cmd_scopy(void)
4394 TALLOC_CTX *ctx = talloc_tos();
4395 char *src, *dest;
4396 char *buf, *buf2;
4397 struct cli_state *targetcli;
4398 char *targetsrc;
4399 char *targetdest;
4400 uint32_t DesiredAccess, ShareAccess, CreateDisposition, CreateOptions;
4401 struct smb_create_returns cr;
4402 uint16_t destfnum = (uint16_t)-1;
4403 uint16_t srcfnum = (uint16_t)-1;
4404 off_t written = 0;
4405 struct scopy_timing st;
4406 int rc = 0;
4407 struct cli_credentials *creds = samba_cmdline_get_creds();
4408 NTSTATUS status;
4410 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4411 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4412 d_printf("scopy <src> <dest>\n");
4413 return 1;
4416 src = talloc_asprintf(ctx,
4417 "%s%s",
4418 client_get_cur_dir(),
4419 buf);
4420 if (!src) {
4421 return 1;
4423 src = client_clean_name(ctx, src);
4424 if (src == NULL) {
4425 return 1;
4428 dest = talloc_asprintf(ctx,
4429 "%s%s",
4430 client_get_cur_dir(),
4431 buf2);
4432 if (!dest) {
4433 return 1;
4435 dest = client_clean_name(ctx, dest);
4436 if (dest == NULL) {
4437 return 1;
4440 status = cli_resolve_path(ctx, "",
4441 creds,
4442 cli, src, &targetcli, &targetsrc);
4443 if (!NT_STATUS_IS_OK(status)) {
4444 d_printf("scopy %s: %s\n", src, nt_errstr(status));
4445 return 1;
4448 status = cli_resolve_path(ctx, "",
4449 creds,
4450 cli, dest, &targetcli, &targetdest);
4451 if (!NT_STATUS_IS_OK(status)) {
4452 d_printf("scopy %s: %s\n", dest, nt_errstr(status));
4453 return 1;
4457 DesiredAccess = (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES|
4458 READ_CONTROL_ACCESS|SYNCHRONIZE_ACCESS);
4459 ShareAccess = FILE_SHARE_READ|FILE_SHARE_DELETE;
4460 CreateDisposition = FILE_OPEN;
4461 CreateOptions = (FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE|
4462 FILE_OPEN_REPARSE_POINT);
4463 status = cli_ntcreate(targetcli, targetsrc, 0, DesiredAccess, 0,
4464 ShareAccess, CreateDisposition, CreateOptions, 0x0,
4465 &srcfnum, &cr);
4466 if (!NT_STATUS_IS_OK(status)) {
4467 d_printf("Failed to open file %s. %s\n",
4468 targetsrc, nt_errstr(status));
4469 return 1;
4472 DesiredAccess = (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_READ_EA|
4473 FILE_WRITE_EA|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|
4474 DELETE_ACCESS|READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|SYNCHRONIZE_ACCESS);
4475 ShareAccess = FILE_SHARE_NONE;
4476 CreateDisposition = FILE_CREATE;
4477 CreateOptions = FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE;
4478 status = cli_ntcreate(targetcli, targetdest, 0, DesiredAccess,
4479 FILE_ATTRIBUTE_ARCHIVE, ShareAccess, CreateDisposition,
4480 CreateOptions, 0x0, &destfnum, NULL);
4481 if (!NT_STATUS_IS_OK(status)) {
4482 d_printf("Failed to create file %s. %s\n",
4483 targetdest, nt_errstr(status));
4484 cli_close(targetcli, srcfnum);
4485 return 1;
4488 clock_gettime_mono(&st.tp_start);
4489 status = cli_splice(targetcli, targetcli, srcfnum, destfnum,
4490 cr.end_of_file, 0, 0, &written, scopy_status, &st);
4491 if (!NT_STATUS_IS_OK(status)) {
4492 d_printf("%s copying file %s -> %s \n",
4493 nt_errstr(status),
4494 targetsrc,
4495 targetdest);
4496 rc = 1;
4499 status = cli_close(targetcli, srcfnum);
4500 if (!NT_STATUS_IS_OK(status)) {
4501 d_printf("Error %s closing remote source file\n", nt_errstr(status));
4502 rc = 1;
4504 status = cli_close(targetcli, destfnum);
4505 if (!NT_STATUS_IS_OK(status)) {
4506 d_printf("Error %s closing remote dest file\n", nt_errstr(status));
4507 rc = 1;
4510 return rc;
4513 /****************************************************************************
4514 Print the volume name.
4515 ****************************************************************************/
4517 static int cmd_volume(void)
4519 char *volname;
4520 uint32_t serial_num;
4521 time_t create_date;
4522 NTSTATUS status;
4524 status = cli_get_fs_volume_info(cli, talloc_tos(),
4525 &volname, &serial_num,
4526 &create_date);
4527 if (!NT_STATUS_IS_OK(status)) {
4528 d_printf("Error %s getting volume info\n", nt_errstr(status));
4529 return 1;
4532 d_printf("Volume: |%s| serial number 0x%x\n",
4533 volname, (unsigned int)serial_num);
4534 return 0;
4537 /****************************************************************************
4538 Hard link files using the NT call.
4539 ****************************************************************************/
4541 static int cmd_hardlink(void)
4543 TALLOC_CTX *ctx = talloc_tos();
4544 char *src, *dest;
4545 char *buf, *buf2;
4546 struct cli_state *targetcli;
4547 char *targetname;
4548 struct cli_credentials *creds = samba_cmdline_get_creds();
4549 NTSTATUS status;
4551 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
4552 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
4553 d_printf("hardlink <src> <dest>\n");
4554 return 1;
4557 src = talloc_asprintf(ctx,
4558 "%s%s",
4559 client_get_cur_dir(),
4560 buf);
4561 if (!src) {
4562 return 1;
4564 src = client_clean_name(ctx, src);
4565 if (src == NULL) {
4566 return 1;
4569 dest = talloc_asprintf(ctx,
4570 "%s%s",
4571 client_get_cur_dir(),
4572 buf2);
4573 if (!dest) {
4574 return 1;
4576 dest = client_clean_name(ctx, dest);
4577 if (dest == NULL) {
4578 return 1;
4581 status = cli_resolve_path(ctx, "",
4582 creds,
4583 cli, src, &targetcli, &targetname);
4584 if (!NT_STATUS_IS_OK(status)) {
4585 d_printf("hardlink %s: %s\n", src, nt_errstr(status));
4586 return 1;
4589 status = cli_hardlink(targetcli, targetname, dest);
4590 if (!NT_STATUS_IS_OK(status)) {
4591 d_printf("%s doing an NT hard link of files\n",
4592 nt_errstr(status));
4593 return 1;
4596 return 0;
4599 /****************************************************************************
4600 Toggle the prompt flag.
4601 ****************************************************************************/
4603 static int cmd_prompt(void)
4605 prompt = !prompt;
4606 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
4607 return 1;
4610 /****************************************************************************
4611 Set the newer than time.
4612 ****************************************************************************/
4614 static int cmd_newer(void)
4616 TALLOC_CTX *ctx = talloc_tos();
4617 char *buf;
4618 bool ok;
4619 SMB_STRUCT_STAT sbuf;
4621 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
4622 if (ok && (sys_stat(buf, &sbuf, false) == 0)) {
4623 newer_than = convert_timespec_to_time_t(sbuf.st_ex_mtime);
4624 DEBUG(1,("Getting files newer than %s",
4625 time_to_asc(newer_than)));
4626 } else {
4627 newer_than = 0;
4630 if (ok && newer_than == 0) {
4631 d_printf("Error setting newer-than time\n");
4632 return 1;
4635 return 0;
4638 /****************************************************************************
4639 Watch directory changes
4640 ****************************************************************************/
4642 static int cmd_notify(void)
4644 TALLOC_CTX *frame = talloc_stackframe();
4645 char *name, *buf;
4646 NTSTATUS status;
4647 uint16_t fnum;
4649 name = talloc_strdup(talloc_tos(), client_get_cur_dir());
4650 if (name == NULL) {
4651 goto fail;
4653 if (!next_token_talloc(talloc_tos(), &cmd_ptr, &buf, NULL)) {
4654 goto usage;
4656 name = talloc_asprintf_append(name, "%s", buf);
4657 if (name == NULL) {
4658 goto fail;
4660 name = client_clean_name(talloc_tos(), name);
4661 if (name == NULL) {
4662 return 1;
4664 status = cli_ntcreate(
4665 cli, name, 0, FILE_READ_DATA, 0,
4666 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
4667 FILE_OPEN, 0, 0, &fnum, NULL);
4668 if (!NT_STATUS_IS_OK(status)) {
4669 d_printf("Could not open file: %s\n", nt_errstr(status));
4670 goto fail;
4673 while (1) {
4674 uint32_t i;
4675 uint32_t num_changes = 0;
4676 struct notify_change *changes = NULL;
4678 status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
4679 true,
4680 talloc_tos(), &num_changes, &changes);
4681 if (NT_STATUS_EQUAL(status, NT_STATUS_NOTIFY_ENUM_DIR)) {
4682 printf("NOTIFY_ENUM_DIR\n");
4683 status = NT_STATUS_OK;
4685 if (!NT_STATUS_IS_OK(status)) {
4686 d_printf("notify returned %s\n",
4687 nt_errstr(status));
4688 goto fail;
4690 for (i=0; i<num_changes; i++) {
4691 printf("%4.4x %s\n", changes[i].action,
4692 changes[i].name);
4694 TALLOC_FREE(changes);
4696 usage:
4697 d_printf("notify <dir name>\n");
4698 fail:
4699 TALLOC_FREE(frame);
4700 return 1;
4703 /****************************************************************************
4704 Set the archive level.
4705 ****************************************************************************/
4707 static int cmd_archive(void)
4709 TALLOC_CTX *ctx = talloc_tos();
4710 char *buf;
4712 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4713 archive_level = atoi(buf);
4714 } else {
4715 d_printf("Archive level is %d\n",archive_level);
4718 return 0;
4721 /****************************************************************************
4722 Toggle the backup_intent state.
4723 ****************************************************************************/
4725 static int cmd_backup(void)
4727 backup_intent = !backup_intent;
4728 cli_set_backup_intent(cli, backup_intent);
4729 DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
4730 return 1;
4733 /****************************************************************************
4734 Toggle the lowercaseflag.
4735 ****************************************************************************/
4737 static int cmd_lowercase(void)
4739 lowercase = !lowercase;
4740 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
4741 return 0;
4744 /****************************************************************************
4745 Toggle the case sensitive flag.
4746 ****************************************************************************/
4748 static int cmd_setcase(void)
4750 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
4752 cli_set_case_sensitive(cli, !orig_case_sensitive);
4753 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
4754 "on":"off"));
4755 return 0;
4758 /****************************************************************************
4759 Toggle the showacls flag.
4760 ****************************************************************************/
4762 static int cmd_showacls(void)
4764 showacls = !showacls;
4765 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
4766 return 0;
4770 /****************************************************************************
4771 Toggle the recurse flag.
4772 ****************************************************************************/
4774 static int cmd_recurse(void)
4776 recurse = !recurse;
4777 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
4778 return 0;
4781 /****************************************************************************
4782 Toggle the translate flag.
4783 ****************************************************************************/
4785 static int cmd_translate(void)
4787 translation = !translation;
4788 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
4789 translation?"on":"off"));
4790 return 0;
4793 /****************************************************************************
4794 Do the lcd command.
4795 ****************************************************************************/
4797 static int cmd_lcd(void)
4799 TALLOC_CTX *ctx = talloc_tos();
4800 char *buf;
4801 char *d;
4803 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4804 if (chdir(buf) == -1) {
4805 d_printf("chdir to %s failed (%s)\n",
4806 buf, strerror(errno));
4809 d = sys_getwd();
4810 if (!d) {
4811 return 1;
4813 DEBUG(2,("the local directory is now %s\n",d));
4814 SAFE_FREE(d);
4815 return 0;
4818 /****************************************************************************
4819 Get a file restarting at end of local file.
4820 ****************************************************************************/
4822 static int cmd_reget(void)
4824 TALLOC_CTX *ctx = talloc_tos();
4825 char *local_name = NULL;
4826 char *remote_name = NULL;
4827 char *fname = NULL;
4828 char *p = NULL;
4830 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4831 if (!remote_name) {
4832 return 1;
4835 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
4836 d_printf("reget <filename>\n");
4837 return 1;
4839 remote_name = talloc_asprintf_append(remote_name, "%s", fname);
4840 if (!remote_name) {
4841 return 1;
4843 remote_name = client_clean_name(ctx,remote_name);
4844 if (!remote_name) {
4845 return 1;
4848 local_name = fname;
4849 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
4850 if (p) {
4851 local_name = p;
4854 return do_get(remote_name, local_name, true);
4857 /****************************************************************************
4858 Put a file restarting at end of local file.
4859 ****************************************************************************/
4861 static int cmd_reput(void)
4863 TALLOC_CTX *ctx = talloc_tos();
4864 char *local_name = NULL;
4865 char *remote_name = NULL;
4866 char *buf;
4867 SMB_STRUCT_STAT st;
4869 remote_name = talloc_strdup(ctx, client_get_cur_dir());
4870 if (!remote_name) {
4871 return 1;
4874 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
4875 d_printf("reput <filename>\n");
4876 return 1;
4879 if (!file_exist_stat(local_name, &st, false)) {
4880 d_printf("%s does not exist\n", local_name);
4881 return 1;
4884 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
4885 remote_name = talloc_asprintf_append(remote_name,
4886 "%s", buf);
4887 } else {
4888 remote_name = talloc_asprintf_append(remote_name,
4889 "%s", local_name);
4891 if (!remote_name) {
4892 return 1;
4895 remote_name = client_clean_name(ctx, remote_name);
4896 if (!remote_name) {
4897 return 1;
4900 return do_put(remote_name, local_name, true);
4903 /****************************************************************************
4904 List a share name.
4905 ****************************************************************************/
4907 static void browse_fn(const char *name, uint32_t m,
4908 const char *comment, void *state)
4910 const char *typestr = "";
4912 switch (m & 7) {
4913 case STYPE_DISKTREE:
4914 typestr = "Disk";
4915 break;
4916 case STYPE_PRINTQ:
4917 typestr = "Printer";
4918 break;
4919 case STYPE_DEVICE:
4920 typestr = "Device";
4921 break;
4922 case STYPE_IPC:
4923 typestr = "IPC";
4924 break;
4926 /* FIXME: If the remote machine returns non-ascii characters
4927 in any of these fields, they can corrupt the output. We
4928 should remove them. */
4929 if (!grepable) {
4930 d_printf("\t%-15s %-10.10s%s\n",
4931 name,typestr,comment);
4932 } else {
4933 d_printf ("%s|%s|%s\n",typestr,name,comment);
4937 static bool browse_host_rpc(bool sort)
4939 NTSTATUS status;
4940 struct rpc_pipe_client *pipe_hnd = NULL;
4941 TALLOC_CTX *frame = talloc_stackframe();
4942 WERROR werr;
4943 struct srvsvc_NetShareInfoCtr info_ctr;
4944 struct srvsvc_NetShareCtr1 ctr1;
4945 uint32_t resume_handle = 0;
4946 uint32_t total_entries = 0;
4947 uint32_t i;
4948 struct dcerpc_binding_handle *b;
4950 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
4951 &pipe_hnd);
4953 if (!NT_STATUS_IS_OK(status)) {
4954 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
4955 nt_errstr(status)));
4956 TALLOC_FREE(frame);
4957 return false;
4960 b = pipe_hnd->binding_handle;
4962 ZERO_STRUCT(info_ctr);
4963 ZERO_STRUCT(ctr1);
4965 info_ctr.level = 1;
4966 info_ctr.ctr.ctr1 = &ctr1;
4968 status = dcerpc_srvsvc_NetShareEnumAll(b, frame,
4969 pipe_hnd->desthost,
4970 &info_ctr,
4971 0xffffffff,
4972 &total_entries,
4973 &resume_handle,
4974 &werr);
4976 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
4977 TALLOC_FREE(pipe_hnd);
4978 TALLOC_FREE(frame);
4979 return false;
4982 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
4983 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
4984 browse_fn(info.name, info.type, info.comment, NULL);
4987 TALLOC_FREE(pipe_hnd);
4988 TALLOC_FREE(frame);
4989 return true;
4992 /****************************************************************************
4993 Try and browse available connections on a host.
4994 ****************************************************************************/
4996 static bool browse_host(bool sort)
4998 NTSTATUS status;
5000 if (!grepable) {
5001 d_printf("\n\tSharename Type Comment\n");
5002 d_printf("\t--------- ---- -------\n");
5005 if (browse_host_rpc(sort)) {
5006 return true;
5009 if (smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1) {
5010 return false;
5013 status = cli_RNetShareEnum(cli, browse_fn, NULL);
5014 if (!NT_STATUS_IS_OK(status)) {
5015 d_printf("Error returning browse list: %s\n",
5016 nt_errstr(status));
5017 return false;
5020 return true;
5023 /****************************************************************************
5024 List a server name.
5025 ****************************************************************************/
5027 static void server_fn(const char *name, uint32_t m,
5028 const char *comment, void *state)
5031 if (!grepable){
5032 d_printf("\t%-16s %s\n", name, comment);
5033 } else {
5034 d_printf("%s|%s|%s\n",(char *)state, name, comment);
5038 /****************************************************************************
5039 Try and browse available connections on a host.
5040 ****************************************************************************/
5042 static bool list_servers(const char *wk_grp)
5044 fstring state;
5046 if (!cli->server_domain)
5047 return false;
5049 if (!grepable) {
5050 d_printf("\n\tServer Comment\n");
5051 d_printf("\t--------- -------\n");
5053 fstrcpy( state, "Server" );
5054 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
5055 state);
5057 if (!grepable) {
5058 d_printf("\n\tWorkgroup Master\n");
5059 d_printf("\t--------- -------\n");
5062 fstrcpy( state, "Workgroup" );
5063 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
5064 server_fn, state);
5065 return true;
5068 /****************************************************************************
5069 Print or set current VUID
5070 ****************************************************************************/
5072 static int cmd_vuid(void)
5074 TALLOC_CTX *ctx = talloc_tos();
5075 char *buf;
5077 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5078 d_printf("Current VUID is %d\n",
5079 cli_state_get_uid(cli));
5080 return 0;
5083 cli_state_set_uid(cli, atoi(buf));
5084 return 0;
5087 /****************************************************************************
5088 Setup a new VUID, by issuing a session setup
5089 ****************************************************************************/
5091 static int cmd_logon(void)
5093 TALLOC_CTX *ctx = talloc_tos();
5094 char *l_username, *l_password;
5095 struct cli_credentials *creds = NULL;
5096 NTSTATUS nt_status;
5098 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
5099 d_printf("logon <username> [<password>]\n");
5100 return 0;
5103 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
5104 char pwd[256] = {0};
5105 int rc;
5107 rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
5108 if (rc == 0) {
5109 l_password = talloc_strdup(ctx, pwd);
5112 if (!l_password) {
5113 return 1;
5116 creds = cli_session_creds_init(ctx,
5117 l_username,
5118 lp_workgroup(),
5119 NULL, /* realm */
5120 l_password,
5121 false, /* use_kerberos */
5122 false, /* fallback_after_kerberos */
5123 false, /* use_ccache */
5124 false); /* password_is_nt_hash */
5125 if (creds == NULL) {
5126 d_printf("cli_session_creds_init() failed.\n");
5127 return -1;
5129 nt_status = cli_session_setup_creds(cli, creds);
5130 TALLOC_FREE(creds);
5131 if (!NT_STATUS_IS_OK(nt_status)) {
5132 d_printf("session setup failed: %s\n", nt_errstr(nt_status));
5133 return -1;
5136 d_printf("Current VUID is %d\n", cli_state_get_uid(cli));
5137 return 0;
5141 * close the session
5144 static int cmd_logoff(void)
5146 NTSTATUS status;
5148 status = cli_ulogoff(cli);
5149 if (!NT_STATUS_IS_OK(status)) {
5150 d_printf("logoff failed: %s\n", nt_errstr(status));
5151 return -1;
5154 d_printf("logoff successful\n");
5155 return 0;
5160 * tree connect (connect to a share)
5163 static int cmd_tcon(void)
5165 TALLOC_CTX *ctx = talloc_tos();
5166 char *sharename;
5167 NTSTATUS status;
5169 if (!next_token_talloc(ctx, &cmd_ptr, &sharename, NULL)) {
5170 d_printf("tcon <sharename>\n");
5171 return 0;
5174 if (!sharename) {
5175 return 1;
5178 status = cli_tree_connect(cli, sharename, "?????", NULL);
5179 if (!NT_STATUS_IS_OK(status)) {
5180 d_printf("tcon failed: %s\n", nt_errstr(status));
5181 return -1;
5184 d_printf("tcon to %s successful, tid: %u\n", sharename,
5185 cli_state_get_tid(cli));
5187 talloc_free(sharename);
5189 return 0;
5193 * tree disconnect (disconnect from a share)
5196 static int cmd_tdis(void)
5198 NTSTATUS status;
5200 status = cli_tdis(cli);
5201 if (!NT_STATUS_IS_OK(status)) {
5202 d_printf("tdis failed: %s\n", nt_errstr(status));
5203 return -1;
5206 d_printf("tdis successful\n");
5207 return 0;
5212 * get or set tid
5215 static int cmd_tid(void)
5217 TALLOC_CTX *ctx = talloc_tos();
5218 char *tid_str;
5220 if (!next_token_talloc(ctx, &cmd_ptr, &tid_str, NULL)) {
5221 if (cli_state_has_tcon(cli)) {
5222 d_printf("current tid is %d\n", cli_state_get_tid(cli));
5223 } else {
5224 d_printf("no tcon currently\n");
5226 } else {
5227 uint32_t tid = atoi(tid_str);
5228 if (!cli_state_has_tcon(cli)) {
5229 d_printf("no tcon currently\n");
5231 cli_state_set_tid(cli, tid);
5234 return 0;
5238 /****************************************************************************
5239 list active connections
5240 ****************************************************************************/
5242 static int cmd_list_connect(void)
5244 cli_cm_display(cli);
5245 return 0;
5248 /****************************************************************************
5249 display the current active client connection
5250 ****************************************************************************/
5252 static int cmd_show_connect( void )
5254 TALLOC_CTX *ctx = talloc_tos();
5255 struct cli_state *targetcli;
5256 char *targetpath;
5257 struct cli_credentials *creds = samba_cmdline_get_creds();
5258 NTSTATUS status;
5260 status = cli_resolve_path(ctx, "",
5261 creds,
5262 cli,
5263 client_get_cur_dir(), &targetcli,
5264 &targetpath);
5265 if (!NT_STATUS_IS_OK(status)) {
5266 d_printf("showconnect %s: %s\n", cur_dir, nt_errstr(status));
5267 return 1;
5270 d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli->conn), targetcli->share);
5271 return 0;
5275 * cmd_utimes - interactive command to set the four times
5277 * Read a filename and four times from the client command line and update
5278 * the file times. A value of -1 for a time means don't change.
5280 static int cmd_utimes(void)
5282 char *buf;
5283 char *fname = NULL;
5284 struct timespec times[4] = {{0}};
5285 struct timeval_buf tbuf[4];
5286 int time_count = 0;
5287 int err = 0;
5288 bool ok;
5289 TALLOC_CTX *ctx = talloc_new(NULL);
5290 NTSTATUS status;
5292 if (ctx == NULL) {
5293 return 1;
5296 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5297 if (!ok) {
5298 d_printf("utimes <filename> <create-time> <access-time> "
5299 "<write-time> <change-time>\n");
5300 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5301 "or -1 for no change\n");
5302 err = 1;
5303 goto out;
5306 fname = talloc_asprintf(ctx,
5307 "%s%s",
5308 client_get_cur_dir(),
5309 buf);
5310 if (fname == NULL) {
5311 err = 1;
5312 goto out;
5314 fname = client_clean_name(ctx, fname);
5315 if (fname == NULL) {
5316 err = 1;
5317 goto out;
5320 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
5321 time_count < 4) {
5322 const char *s = buf;
5323 struct tm tm = {0,};
5324 time_t t;
5325 char *ret;
5327 if (strlen(s) == 2 && strcmp(s, "-1") == 0) {
5328 times[time_count] = make_omit_timespec();
5329 time_count++;
5330 continue;
5333 ret = strptime(s, "%y:%m:%d-%H:%M:%S", &tm);
5335 if (ret == NULL) {
5336 ret = strptime(s, "%Y:%m:%d-%H:%M:%S", &tm);
5339 /* We could not match all the chars, so print error */
5340 if (ret == NULL || *ret != 0) {
5341 d_printf("Invalid date format: %s\n", s);
5342 d_printf("utimes <filename> <create-time> "
5343 "<access-time> <write-time> <change-time>\n");
5344 d_printf("Dates should be in [YY]YY:MM:DD-HH:MM:SS "
5345 "format or -1 for no change\n");
5346 err = 1;
5347 goto out;
5350 /* Convert tm to a time_t */
5351 t = mktime(&tm);
5352 times[time_count] = (struct timespec){.tv_sec = t};
5353 time_count++;
5356 if (time_count < 4) {
5357 d_printf("Insufficient dates: %d\n", time_count);
5358 d_printf("utimes <filename> <create-time> <access-time> "
5359 "<write-time> <change-time>\n");
5360 d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
5361 "or -1 for no change\n");
5362 err = 1;
5363 goto out;
5366 DEBUG(10, ("times\nCreate: %sAccess: %s Write: %sChange: %s\n",
5367 timespec_string_buf(&times[0], false, &tbuf[0]),
5368 timespec_string_buf(&times[1], false, &tbuf[1]),
5369 timespec_string_buf(&times[2], false, &tbuf[2]),
5370 timespec_string_buf(&times[3], false, &tbuf[3])));
5372 status = cli_setpathinfo_ext(
5373 cli, fname, times[0], times[1], times[2], times[3],
5374 (uint32_t)-1);
5375 if (!NT_STATUS_IS_OK(status)) {
5376 d_printf("cli_setpathinfo_ext failed: %s\n",
5377 nt_errstr(status));
5378 err = 1;
5379 goto out;
5381 out:
5382 talloc_free(ctx);
5383 return err;
5387 * set_remote_attr - set DOS attributes of a remote file
5388 * @filename: path to the file name
5389 * @new_attr: attribute bit mask to use
5390 * @mode: one of ATTR_SET or ATTR_UNSET
5392 * Update the file attributes with the one provided.
5394 int set_remote_attr(const char *filename, uint32_t new_attr, int mode)
5396 extern struct cli_state *cli;
5397 uint32_t old_attr;
5398 NTSTATUS status;
5400 status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
5401 if (!NT_STATUS_IS_OK(status)) {
5402 d_printf("cli_getatr failed: %s\n", nt_errstr(status));
5403 return 1;
5406 if (mode == ATTR_SET) {
5407 new_attr |= old_attr;
5408 } else {
5409 new_attr = old_attr & ~new_attr;
5412 status = cli_setatr(cli, filename, new_attr, 0);
5413 if (!NT_STATUS_IS_OK(status)) {
5414 d_printf("cli_setatr failed: %s\n", nt_errstr(status));
5415 return 1;
5418 return 0;
5422 * cmd_setmode - interactive command to set DOS attributes
5424 * Read a filename and mode from the client command line and update
5425 * the file DOS attributes.
5427 int cmd_setmode(void)
5429 char *buf;
5430 char *fname = NULL;
5431 uint32_t attr[2] = {0};
5432 int mode = ATTR_SET;
5433 int err = 0;
5434 bool ok;
5435 TALLOC_CTX *ctx = talloc_new(NULL);
5436 if (ctx == NULL) {
5437 return 1;
5440 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
5441 if (!ok) {
5442 d_printf("setmode <filename> <[+|-]rsha>\n");
5443 err = 1;
5444 goto out;
5447 fname = talloc_asprintf(ctx,
5448 "%s%s",
5449 client_get_cur_dir(),
5450 buf);
5451 if (fname == NULL) {
5452 err = 1;
5453 goto out;
5455 fname = client_clean_name(ctx, fname);
5456 if (fname == NULL) {
5457 err = 1;
5458 goto out;
5461 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
5462 const char *s = buf;
5464 while (*s) {
5465 switch (*s++) {
5466 case '+':
5467 mode = ATTR_SET;
5468 break;
5469 case '-':
5470 mode = ATTR_UNSET;
5471 break;
5472 case 'r':
5473 attr[mode] |= FILE_ATTRIBUTE_READONLY;
5474 break;
5475 case 'h':
5476 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
5477 break;
5478 case 's':
5479 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
5480 break;
5481 case 'a':
5482 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
5483 break;
5484 default:
5485 d_printf("setmode <filename> <perm=[+|-]rsha>\n");
5486 err = 1;
5487 goto out;
5492 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
5493 d_printf("setmode <filename> <[+|-]rsha>\n");
5494 err = 1;
5495 goto out;
5498 DEBUG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
5500 /* ignore return value: server might not store DOS attributes */
5501 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
5502 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
5503 out:
5504 talloc_free(ctx);
5505 return err;
5508 /****************************************************************************
5509 iosize command
5510 ***************************************************************************/
5512 int cmd_iosize(void)
5514 TALLOC_CTX *ctx = talloc_tos();
5515 char *buf;
5516 int iosize;
5517 struct cli_credentials *creds = samba_cmdline_get_creds();
5518 bool smb_encrypt =
5519 (cli_credentials_get_smb_encryption(creds) ==
5520 SMB_ENCRYPTION_REQUIRED);
5522 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5523 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5524 if (!smb_encrypt) {
5525 d_printf("iosize <n> or iosize 0x<n>. "
5526 "Minimum is 0 (default), "
5527 "max is 16776960 (0xFFFF00)\n");
5528 } else {
5529 d_printf("iosize <n> or iosize 0x<n>. "
5530 "(Encrypted connection) ,"
5531 "Minimum is 0 (default), "
5532 "max is 130048 (0x1FC00)\n");
5534 } else {
5535 d_printf("iosize <n> or iosize 0x<n>.\n");
5537 return 1;
5540 iosize = strtol(buf,NULL,0);
5541 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
5542 if (smb_encrypt && (iosize < 0 || iosize > 0xFC00)) {
5543 d_printf("iosize out of range for encrypted "
5544 "connection (min = 0 (default), "
5545 "max = 130048 (0x1FC00)\n");
5546 return 1;
5547 } else if (!smb_encrypt && (iosize < 0 || iosize > 0xFFFF00)) {
5548 d_printf("iosize out of range (min = 0 (default), "
5549 "max = 16776960 (0xFFFF00)\n");
5550 return 1;
5554 io_bufsize = iosize;
5555 d_printf("iosize is now %d\n", io_bufsize);
5556 return 0;
5559 /****************************************************************************
5560 timeout command
5561 ***************************************************************************/
5563 static int cmd_timeout(void)
5565 TALLOC_CTX *ctx = talloc_tos();
5566 char *buf;
5568 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5569 unsigned int old_timeout = cli_set_timeout(cli, 0);
5570 cli_set_timeout(cli, old_timeout);
5571 d_printf("timeout <n> (per-operation timeout "
5572 "in seconds - currently %u).\n",
5573 old_timeout/1000);
5574 return 1;
5577 io_timeout = strtol(buf,NULL,0);
5578 cli_set_timeout(cli, io_timeout*1000);
5579 d_printf("io_timeout per operation is now %d\n", io_timeout);
5580 return 0;
5584 /****************************************************************************
5585 history
5586 ****************************************************************************/
5587 static int cmd_history(void)
5589 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
5590 HIST_ENTRY **hlist;
5591 int i;
5593 hlist = history_list();
5595 for (i = 0; hlist && hlist[i]; i++) {
5596 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
5598 #else
5599 DEBUG(0,("no history without readline support\n"));
5600 #endif
5602 return 0;
5605 /* Some constants for completing filename arguments */
5607 #define COMPL_NONE 0 /* No completions */
5608 #define COMPL_REMOTE 1 /* Complete remote filename */
5609 #define COMPL_LOCAL 2 /* Complete local filename */
5611 /* This defines the commands supported by this client.
5612 * NOTE: The "!" must be the last one in the list because it's fn pointer
5613 * field is NULL, and NULL in that field is used in process_tok()
5614 * (below) to indicate the end of the list. crh
5616 static struct {
5617 const char *name;
5618 int (*fn)(void);
5619 const char *description;
5620 char compl_args[2]; /* Completion argument info */
5621 } commands[] = {
5622 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5623 {"allinfo",cmd_allinfo,"<file> show all available info",
5624 {COMPL_REMOTE,COMPL_NONE}},
5625 {"altname",cmd_altname,"<file> show alt name",{COMPL_REMOTE,COMPL_NONE}},
5626 {"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}},
5627 {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
5628 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
5629 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
5630 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
5631 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
5632 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_NONE}},
5633 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_NONE}},
5634 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_NONE}},
5635 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5636 {"deltree",cmd_deltree,"<mask> recursively delete all matching files and directories",{COMPL_REMOTE,COMPL_NONE}},
5637 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5638 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5639 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
5640 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5641 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
5642 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_NONE}},
5643 {"geteas", cmd_geteas, "<file name> get the EA list of a file",
5644 {COMPL_REMOTE, COMPL_NONE}},
5645 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5646 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
5647 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
5648 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
5649 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
5650 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
5651 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5652 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
5653 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5654 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
5655 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
5656 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5657 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
5658 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
5659 {"mkfifo",cmd_mkfifo,"<file mode> make a fifo",{COMPL_NONE,COMPL_NONE}},
5660 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
5661 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
5662 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
5663 {"notify",cmd_notify,"<file>Get notified of dir changes",{COMPL_REMOTE,COMPL_NONE}},
5664 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
5665 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
5666 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
5667 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5668 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5669 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5670 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5671 {"posix_whoami",cmd_posix_whoami,"return logged on user information "
5672 "using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
5673 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
5674 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
5675 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
5676 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
5677 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5678 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
5679 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
5680 {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5681 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
5682 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
5683 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
5684 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
5685 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
5686 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5687 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_REMOTE,COMPL_NONE}},
5688 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
5689 {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
5690 {COMPL_REMOTE, COMPL_LOCAL}},
5691 {"setmode",cmd_setmode,"<file name> <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
5692 {"scopy",cmd_scopy,"<src> <dest> server-side copy file",{COMPL_REMOTE,COMPL_REMOTE}},
5693 {"stat",cmd_stat,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_NONE}},
5694 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
5695 {"tar",cmd_tar,"tar <c|x>[IXFvbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
5696 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
5697 {"timeout",cmd_timeout,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE,COMPL_NONE}},
5698 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
5699 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
5700 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
5701 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
5702 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
5703 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
5704 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
5705 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
5706 {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
5707 {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
5708 {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
5709 {"utimes", cmd_utimes,"<file name> <create_time> <access_time> <mod_time> "
5710 "<ctime> set times", {COMPL_REMOTE,COMPL_NONE}},
5711 {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
5712 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
5714 /* Yes, this must be here, see crh's comment above. */
5715 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
5716 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
5719 /*******************************************************************
5720 Lookup a command string in the list of commands, including
5721 abbreviations.
5722 ******************************************************************/
5724 static int process_tok(char *tok)
5726 size_t i = 0, matches = 0;
5727 size_t cmd=0;
5728 size_t tok_len = strlen(tok);
5730 while (commands[i].fn != NULL) {
5731 if (strequal(commands[i].name,tok)) {
5732 matches = 1;
5733 cmd = i;
5734 break;
5735 } else if (strnequal(commands[i].name, tok, tok_len)) {
5736 matches++;
5737 cmd = i;
5739 i++;
5742 if (matches == 0)
5743 return(-1);
5744 else if (matches == 1)
5745 return(cmd);
5746 else
5747 return(-2);
5750 /****************************************************************************
5751 Help.
5752 ****************************************************************************/
5754 static int cmd_help(void)
5756 TALLOC_CTX *ctx = talloc_tos();
5757 int i=0,j;
5758 char *buf;
5760 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
5761 if ((i = process_tok(buf)) >= 0)
5762 d_printf("HELP %s:\n\t%s\n\n",
5763 commands[i].name,commands[i].description);
5764 } else {
5765 while (commands[i].description) {
5766 for (j=0; commands[i].description && (j<5); j++) {
5767 d_printf("%-15s",commands[i].name);
5768 i++;
5770 d_printf("\n");
5773 return 0;
5776 /****************************************************************************
5777 Process a -c command string.
5778 ****************************************************************************/
5780 static int process_command_string(const char *cmd_in)
5782 TALLOC_CTX *ctx = talloc_tos();
5783 char *cmd = talloc_strdup(ctx, cmd_in);
5784 int rc = 0;
5785 struct cli_credentials *creds = samba_cmdline_get_creds();
5787 if (!cmd) {
5788 return 1;
5790 /* establish the connection if not already */
5792 if (!cli) {
5793 NTSTATUS status;
5795 status = cli_cm_open(talloc_tos(), NULL,
5796 desthost,
5797 service,
5798 creds,
5799 have_ip ? &dest_ss : NULL, port,
5800 name_type,
5801 &cli);
5802 if (!NT_STATUS_IS_OK(status)) {
5803 return 1;
5805 cli_set_timeout(cli, io_timeout*1000);
5808 while (cmd[0] != '\0') {
5809 char *line;
5810 char *p;
5811 char *tok;
5812 int i;
5814 if ((p = strchr_m(cmd, ';')) == 0) {
5815 line = cmd;
5816 cmd += strlen(cmd);
5817 } else {
5818 *p = '\0';
5819 line = cmd;
5820 cmd = p + 1;
5823 /* and get the first part of the command */
5824 cmd_ptr = line;
5825 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
5826 continue;
5829 if ((i = process_tok(tok)) >= 0) {
5830 rc = commands[i].fn();
5831 } else if (i == -2) {
5832 d_printf("%s: command abbreviation ambiguous\n",tok);
5833 } else {
5834 d_printf("%s: command not found\n",tok);
5838 return rc;
5841 #define MAX_COMPLETIONS 100
5843 struct completion_remote {
5844 char *dirmask;
5845 char **matches;
5846 int count, samelen;
5847 const char *text;
5848 int len;
5851 static NTSTATUS completion_remote_filter(struct file_info *f,
5852 const char *mask,
5853 void *state)
5855 struct completion_remote *info = (struct completion_remote *)state;
5857 if (info->count >= MAX_COMPLETIONS - 1) {
5858 return NT_STATUS_OK;
5860 if (strncmp(info->text, f->name, info->len) != 0) {
5861 return NT_STATUS_OK;
5863 if (ISDOT(f->name) || ISDOTDOT(f->name)) {
5864 return NT_STATUS_OK;
5867 if ((info->dirmask[0] == 0) && !(f->attr & FILE_ATTRIBUTE_DIRECTORY))
5868 info->matches[info->count] = SMB_STRDUP(f->name);
5869 else {
5870 TALLOC_CTX *ctx = talloc_stackframe();
5871 char *tmp;
5873 tmp = talloc_strdup(ctx,info->dirmask);
5874 if (!tmp) {
5875 TALLOC_FREE(ctx);
5876 return NT_STATUS_NO_MEMORY;
5878 tmp = talloc_asprintf_append(tmp, "%s", f->name);
5879 if (!tmp) {
5880 TALLOC_FREE(ctx);
5881 return NT_STATUS_NO_MEMORY;
5883 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5884 tmp = talloc_asprintf_append(tmp, "%s",
5885 CLI_DIRSEP_STR);
5887 if (!tmp) {
5888 TALLOC_FREE(ctx);
5889 return NT_STATUS_NO_MEMORY;
5891 info->matches[info->count] = SMB_STRDUP(tmp);
5892 TALLOC_FREE(ctx);
5894 if (info->matches[info->count] == NULL) {
5895 return NT_STATUS_OK;
5897 if (f->attr & FILE_ATTRIBUTE_DIRECTORY) {
5898 smb_readline_ca_char(0);
5900 if (info->count == 1) {
5901 info->samelen = strlen(info->matches[info->count]);
5902 } else {
5903 while (strncmp(info->matches[info->count],
5904 info->matches[info->count-1],
5905 info->samelen) != 0) {
5906 info->samelen--;
5909 info->count++;
5910 return NT_STATUS_OK;
5913 static char **remote_completion(const char *text, int len)
5915 TALLOC_CTX *ctx = talloc_stackframe();
5916 char *dirmask = NULL;
5917 char *targetpath = NULL;
5918 struct cli_state *targetcli = NULL;
5919 int i;
5920 struct completion_remote info = { NULL, NULL, 1, 0, NULL, 0 };
5921 struct cli_credentials *creds = samba_cmdline_get_creds();
5922 NTSTATUS status;
5924 /* can't have non-static initialisation on Sun CC, so do it
5925 at run time here */
5926 info.samelen = len;
5927 info.text = text;
5928 info.len = len;
5930 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
5931 if (!info.matches) {
5932 TALLOC_FREE(ctx);
5933 return NULL;
5937 * We're leaving matches[0] free to fill it later with the text to
5938 * display: Either the one single match or the longest common subset
5939 * of the matches.
5941 info.matches[0] = NULL;
5942 info.count = 1;
5944 for (i = len-1; i >= 0; i--) {
5945 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
5946 break;
5950 info.text = text+i+1;
5951 info.samelen = info.len = len-i-1;
5953 if (i > 0) {
5954 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
5955 if (!info.dirmask) {
5956 goto cleanup;
5958 strncpy(info.dirmask, text, i+1);
5959 info.dirmask[i+1] = 0;
5960 dirmask = talloc_asprintf(ctx,
5961 "%s%*s*",
5962 client_get_cur_dir(),
5963 i-1,
5964 text);
5965 } else {
5966 info.dirmask = SMB_STRDUP("");
5967 if (!info.dirmask) {
5968 goto cleanup;
5970 dirmask = talloc_asprintf(ctx,
5971 "%s*",
5972 client_get_cur_dir());
5974 if (!dirmask) {
5975 goto cleanup;
5977 dirmask = client_clean_name(ctx, dirmask);
5978 if (dirmask == NULL) {
5979 goto cleanup;
5982 status = cli_resolve_path(ctx, "",
5983 creds,
5984 cli, dirmask, &targetcli, &targetpath);
5985 if (!NT_STATUS_IS_OK(status)) {
5986 goto cleanup;
5988 status = cli_list(targetcli, targetpath, FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
5989 completion_remote_filter, (void *)&info);
5990 if (!NT_STATUS_IS_OK(status)) {
5991 goto cleanup;
5994 if (info.count == 1) {
5996 * No matches at all, NULL indicates there is nothing
5998 SAFE_FREE(info.matches[0]);
5999 SAFE_FREE(info.matches);
6000 TALLOC_FREE(ctx);
6001 return NULL;
6004 if (info.count == 2) {
6006 * Exactly one match in matches[1], indicate this is the one
6007 * in matches[0].
6009 info.matches[0] = info.matches[1];
6010 info.matches[1] = NULL;
6011 info.count -= 1;
6012 TALLOC_FREE(ctx);
6013 return info.matches;
6017 * We got more than one possible match, set the result to the maximum
6018 * common subset
6021 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
6022 info.matches[info.count] = NULL;
6023 TALLOC_FREE(ctx);
6024 return info.matches;
6026 cleanup:
6027 for (i = 0; i < info.count; i++) {
6028 SAFE_FREE(info.matches[i]);
6030 SAFE_FREE(info.matches);
6031 SAFE_FREE(info.dirmask);
6032 TALLOC_FREE(ctx);
6033 return NULL;
6036 static char **completion_fn(const char *text, int start, int end)
6038 smb_readline_ca_char(' ');
6040 if (start) {
6041 const char *buf, *sp;
6042 int i;
6043 char compl_type;
6045 buf = smb_readline_get_line_buffer();
6046 if (buf == NULL)
6047 return NULL;
6049 sp = strchr(buf, ' ');
6050 if (sp == NULL)
6051 return NULL;
6053 for (i = 0; commands[i].name; i++) {
6054 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
6055 (commands[i].name[sp - buf] == 0)) {
6056 break;
6059 if (commands[i].name == NULL)
6060 return NULL;
6062 while (*sp == ' ')
6063 sp++;
6065 if (sp == (buf + start))
6066 compl_type = commands[i].compl_args[0];
6067 else
6068 compl_type = commands[i].compl_args[1];
6070 if (compl_type == COMPL_REMOTE)
6071 return remote_completion(text, end - start);
6072 else /* fall back to local filename completion */
6073 return NULL;
6074 } else {
6075 char **matches;
6076 size_t i, len, samelen = 0, count=1;
6078 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
6079 if (!matches) {
6080 return NULL;
6082 matches[0] = NULL;
6084 len = strlen(text);
6085 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
6086 if (strncmp(text, commands[i].name, len) == 0) {
6087 matches[count] = SMB_STRDUP(commands[i].name);
6088 if (!matches[count])
6089 goto cleanup;
6090 if (count == 1)
6091 samelen = strlen(matches[count]);
6092 else
6093 while (strncmp(matches[count], matches[count-1], samelen) != 0)
6094 samelen--;
6095 count++;
6099 switch (count) {
6100 case 0: /* should never happen */
6101 case 1:
6102 goto cleanup;
6103 case 2:
6104 matches[0] = SMB_STRDUP(matches[1]);
6105 break;
6106 default:
6107 matches[0] = (char *)SMB_MALLOC(samelen+1);
6108 if (!matches[0])
6109 goto cleanup;
6110 strncpy(matches[0], matches[1], samelen);
6111 matches[0][samelen] = 0;
6113 matches[count] = NULL;
6114 return matches;
6116 cleanup:
6117 for (i = 0; i < count; i++)
6118 free(matches[i]);
6120 free(matches);
6121 return NULL;
6125 static bool finished;
6127 /****************************************************************************
6128 Make sure we swallow keepalives during idle time.
6129 ****************************************************************************/
6131 static void readline_callback(void)
6133 static time_t last_t;
6134 struct timespec now;
6135 time_t t;
6136 NTSTATUS status;
6137 unsigned char garbage[16];
6139 clock_gettime_mono(&now);
6140 t = now.tv_sec;
6142 if (t - last_t < 5)
6143 return;
6145 last_t = t;
6147 /* Ping the server to keep the connection alive using SMBecho. */
6148 memset(garbage, 0xf0, sizeof(garbage));
6149 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
6150 if (NT_STATUS_IS_OK(status) ||
6151 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
6153 * Even if server returns NT_STATUS_INVALID_PARAMETER
6154 * it still responded.
6155 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
6157 return;
6160 if (!cli_state_is_connected(cli)) {
6161 DEBUG(0,("SMBecho failed (%s). The connection is "
6162 "disconnected now\n", nt_errstr(status)));
6163 finished = true;
6164 smb_readline_done();
6168 /****************************************************************************
6169 Process commands on stdin.
6170 ****************************************************************************/
6172 static int process_stdin(void)
6174 int rc = 0;
6176 if (!quiet) {
6177 d_printf("Try \"help\" to get a list of possible commands.\n");
6180 while (!finished) {
6181 TALLOC_CTX *frame = talloc_stackframe();
6182 char *tok = NULL;
6183 char *the_prompt = NULL;
6184 char *line = NULL;
6185 int i;
6187 /* display a prompt */
6188 the_prompt = talloc_asprintf(frame,
6189 "smb: %s> ",
6190 client_get_cur_dir());
6191 if (the_prompt == NULL) {
6192 TALLOC_FREE(frame);
6193 break;
6195 line = smb_readline(the_prompt, readline_callback, completion_fn);
6196 if (!line) {
6197 TALLOC_FREE(frame);
6198 break;
6201 /* special case - first char is ! */
6202 if (*line == '!') {
6203 if (system(line + 1) == -1) {
6204 d_printf("system() command %s failed.\n",
6205 line+1);
6207 SAFE_FREE(line);
6208 TALLOC_FREE(frame);
6209 continue;
6212 /* and get the first part of the command */
6213 cmd_ptr = line;
6214 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
6215 TALLOC_FREE(frame);
6216 SAFE_FREE(line);
6217 continue;
6220 if ((i = process_tok(tok)) >= 0) {
6221 rc = commands[i].fn();
6222 } else if (i == -2) {
6223 d_printf("%s: command abbreviation ambiguous\n",tok);
6224 } else {
6225 d_printf("%s: command not found\n",tok);
6227 SAFE_FREE(line);
6228 TALLOC_FREE(frame);
6230 return rc;
6233 /****************************************************************************
6234 Process commands from the client.
6235 ****************************************************************************/
6237 static int process(const char *base_directory)
6239 int rc = 0;
6240 NTSTATUS status;
6241 struct cli_credentials *creds = samba_cmdline_get_creds();
6243 status = cli_cm_open(talloc_tos(), NULL,
6244 desthost,
6245 service,
6246 creds,
6247 have_ip ? &dest_ss : NULL, port,
6248 name_type, &cli);
6249 if (!NT_STATUS_IS_OK(status)) {
6250 return 1;
6253 cli_set_timeout(cli, io_timeout*1000);
6255 if (base_directory && *base_directory) {
6256 rc = do_cd(base_directory);
6257 if (rc) {
6258 cli_shutdown(cli);
6259 return rc;
6263 if (cmdstr) {
6264 rc = process_command_string(cmdstr);
6265 } else {
6266 process_stdin();
6269 cli_shutdown(cli);
6270 return rc;
6273 /****************************************************************************
6274 Handle a -L query.
6275 ****************************************************************************/
6277 static int do_host_query(struct loadparm_context *lp_ctx,
6278 const char *query_host)
6280 NTSTATUS status;
6281 struct cli_credentials *creds = samba_cmdline_get_creds();
6283 status = cli_cm_open(talloc_tos(), NULL,
6284 query_host,
6285 "IPC$",
6286 creds,
6287 have_ip ? &dest_ss : NULL, port,
6288 name_type, &cli);
6289 if (!NT_STATUS_IS_OK(status)) {
6290 return 1;
6293 cli_set_timeout(cli, io_timeout*1000);
6294 browse_host(true);
6296 /* Ensure that the host can do IPv4 */
6298 if (!interpret_addr(query_host)) {
6299 struct sockaddr_storage ss;
6300 if (interpret_string_addr(&ss, query_host, 0) &&
6301 (ss.ss_family != AF_INET)) {
6302 d_printf("%s is an IPv6 address -- no workgroup available\n",
6303 query_host);
6304 return 1;
6308 if (lp_client_min_protocol() > PROTOCOL_NT1) {
6309 d_printf("SMB1 disabled -- no workgroup available\n");
6310 goto out;
6313 if (lp_disable_netbios()) {
6314 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
6315 goto out;
6318 if (port != NBT_SMB_PORT ||
6319 smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1)
6322 * Workgroups simply don't make sense over anything
6323 * else but port 139 and SMB1.
6326 cli_shutdown(cli);
6327 d_printf("Reconnecting with SMB1 for workgroup listing.\n");
6328 lpcfg_set_cmdline(lp_ctx, "client max protocol", "NT1");
6329 status = cli_cm_open(talloc_tos(), NULL,
6330 query_host,
6331 "IPC$",
6332 creds,
6333 have_ip ? &dest_ss : NULL, NBT_SMB_PORT,
6334 name_type, &cli);
6335 if (!NT_STATUS_IS_OK(status)) {
6336 d_printf("Unable to connect with SMB1 "
6337 "-- no workgroup available\n");
6338 return 0;
6342 cli_set_timeout(cli, io_timeout*1000);
6343 list_servers(lp_workgroup());
6344 out:
6345 cli_shutdown(cli);
6347 return(0);
6350 /****************************************************************************
6351 Handle a tar operation.
6352 ****************************************************************************/
6354 static int do_tar_op(const char *base_directory)
6356 struct tar *tar_ctx = tar_get_ctx();
6357 int ret = 0;
6358 struct cli_credentials *creds = samba_cmdline_get_creds();
6360 /* do we already have a connection? */
6361 if (!cli) {
6362 NTSTATUS status;
6364 status = cli_cm_open(talloc_tos(), NULL,
6365 desthost,
6366 service,
6367 creds,
6368 have_ip ? &dest_ss : NULL, port,
6369 name_type, &cli);
6370 if (!NT_STATUS_IS_OK(status)) {
6371 ret = 1;
6372 goto out;
6374 cli_set_timeout(cli, io_timeout*1000);
6377 recurse = true;
6379 if (base_directory && *base_directory) {
6380 ret = do_cd(base_directory);
6381 if (ret) {
6382 goto out_cli;
6386 ret = tar_process(tar_ctx);
6388 out_cli:
6389 cli_shutdown(cli);
6390 out:
6391 return ret;
6394 /****************************************************************************
6395 Handle a message operation.
6396 ****************************************************************************/
6398 static int do_message_op(struct cli_credentials *creds)
6400 NTSTATUS status;
6402 if (lp_disable_netbios()) {
6403 d_printf("NetBIOS over TCP disabled.\n");
6404 return 1;
6407 status = cli_connect_nb(talloc_tos(),
6408 desthost, have_ip ? &dest_ss : NULL,
6409 port ? port : NBT_SMB_PORT, name_type,
6410 lp_netbios_name(),
6411 SMB_SIGNING_OFF,
6413 &cli);
6414 if (!NT_STATUS_IS_OK(status)) {
6415 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
6416 return 1;
6419 cli_set_timeout(cli, io_timeout*1000);
6420 send_message(cli_credentials_get_username(creds));
6421 cli_shutdown(cli);
6423 return 0;
6426 /****************************************************************************
6427 main program
6428 ****************************************************************************/
6430 int main(int argc,char *argv[])
6432 const char **const_argv = discard_const_p(const char *, argv);
6433 char *base_directory = NULL;
6434 int opt;
6435 char *query_host = NULL;
6436 bool message = false;
6437 static const char *new_name_resolve_order = NULL;
6438 poptContext pc;
6439 char *p;
6440 int rc = 0;
6441 bool tar_opt = false;
6442 bool service_opt = false;
6443 struct tar *tar_ctx = tar_get_ctx();
6444 bool ok;
6446 struct poptOption long_options[] = {
6447 POPT_AUTOHELP
6450 .longName = "message",
6451 .shortName = 'M',
6452 .argInfo = POPT_ARG_STRING,
6453 .arg = NULL,
6454 .val = 'M',
6455 .descrip = "Send message",
6456 .argDescrip = "HOST",
6459 .longName = "ip-address",
6460 .shortName = 'I',
6461 .argInfo = POPT_ARG_STRING,
6462 .arg = NULL,
6463 .val = 'I',
6464 .descrip = "Use this IP to connect to",
6465 .argDescrip = "IP",
6468 .longName = "stderr",
6469 .shortName = 'E',
6470 .argInfo = POPT_ARG_NONE,
6471 .arg = NULL,
6472 .val = 'E',
6473 .descrip = "Write messages to stderr instead of stdout",
6476 .longName = "list",
6477 .shortName = 'L',
6478 .argInfo = POPT_ARG_STRING,
6479 .arg = NULL,
6480 .val = 'L',
6481 .descrip = "Get a list of shares available on a host",
6482 .argDescrip = "HOST",
6485 .longName = "tar",
6486 .shortName = 'T',
6487 .argInfo = POPT_ARG_STRING,
6488 .arg = NULL,
6489 .val = 'T',
6490 .descrip = "Command line tar",
6491 .argDescrip = "<c|x>IXFvgbNan",
6494 .longName = "directory",
6495 .shortName = 'D',
6496 .argInfo = POPT_ARG_STRING,
6497 .arg = NULL,
6498 .val = 'D',
6499 .descrip = "Start from directory",
6500 .argDescrip = "DIR",
6503 .longName = "command",
6504 .shortName = 'c',
6505 .argInfo = POPT_ARG_STRING,
6506 .arg = &cmdstr,
6507 .val = 'c',
6508 .descrip = "Execute semicolon separated commands",
6511 .longName = "send-buffer",
6512 .shortName = 'b',
6513 .argInfo = POPT_ARG_INT,
6514 .arg = &io_bufsize,
6515 .val = 'b',
6516 .descrip = "Changes the transmit/send buffer",
6517 .argDescrip = "BYTES",
6520 .longName = "timeout",
6521 .shortName = 't',
6522 .argInfo = POPT_ARG_INT,
6523 .arg = &io_timeout,
6524 .val = 'b',
6525 .descrip = "Changes the per-operation timeout",
6526 .argDescrip = "SECONDS",
6529 .longName = "port",
6530 .shortName = 'p',
6531 .argInfo = POPT_ARG_INT,
6532 .arg = &port,
6533 .val = 'p',
6534 .descrip = "Port to connect to",
6535 .argDescrip = "PORT",
6538 .longName = "grepable",
6539 .shortName = 'g',
6540 .argInfo = POPT_ARG_NONE,
6541 .arg = NULL,
6542 .val = 'g',
6543 .descrip = "Produce grepable output",
6546 .longName = "quiet",
6547 .shortName = 'q',
6548 .argInfo = POPT_ARG_NONE,
6549 .arg = NULL,
6550 .val = 'q',
6551 .descrip = "Suppress help message",
6554 .longName = "browse",
6555 .shortName = 'B',
6556 .argInfo = POPT_ARG_NONE,
6557 .arg = NULL,
6558 .val = 'B',
6559 .descrip = "Browse SMB servers using DNS",
6561 POPT_COMMON_SAMBA
6562 POPT_COMMON_CONNECTION
6563 POPT_COMMON_CREDENTIALS
6564 POPT_LEGACY_S3
6565 POPT_COMMON_VERSION
6566 POPT_TABLEEND
6568 TALLOC_CTX *frame = talloc_stackframe();
6569 struct cli_credentials *creds = NULL;
6570 struct loadparm_context *lp_ctx = NULL;
6572 if (!client_set_cur_dir("\\")) {
6573 exit(ENOMEM);
6576 smb_init_locale();
6578 ok = samba_cmdline_init(frame,
6579 SAMBA_CMDLINE_CONFIG_CLIENT,
6580 false /* require_smbconf */);
6581 if (!ok) {
6582 DBG_ERR("Failed to init cmdline parser!\n");
6583 exit(ENOMEM);
6585 lp_ctx = samba_cmdline_get_lp_ctx();
6586 lpcfg_set_cmdline(lp_ctx, "log level", "1");
6588 /* skip argv(0) */
6589 pc = samba_popt_get_context(getprogname(),
6590 argc,
6591 const_argv,
6592 long_options,
6594 if (pc == NULL) {
6595 DBG_ERR("Failed to setup popt context!\n");
6596 exit(1);
6599 poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
6601 creds = samba_cmdline_get_creds();
6602 while ((opt = poptGetNextOpt(pc)) != -1) {
6605 * if the tar option has been called previously, now
6606 * we need to eat out the leftovers
6608 /* I see no other way to keep things sane --SSS */
6609 if (tar_opt == true) {
6610 while (poptPeekArg(pc)) {
6611 poptGetArg(pc);
6613 tar_opt = false;
6616 /* if the service has not yet been specified lets see if it is available in the popt stack */
6617 if (!service_opt && poptPeekArg(pc)) {
6618 service = talloc_strdup(frame, poptGetArg(pc));
6619 if (!service) {
6620 exit(ENOMEM);
6622 service_opt = true;
6625 /* if the service has already been retrieved then check if we have also a password */
6626 if (service_opt &&
6627 cli_credentials_get_password_obtained(creds) != CRED_SPECIFIED &&
6628 poptPeekArg(pc)) {
6629 cli_credentials_set_password(creds,
6630 poptGetArg(pc),
6631 CRED_SPECIFIED);
6635 switch (opt) {
6636 case 'M':
6637 /* Messages are sent to NetBIOS name type 0x3
6638 * (Messenger Service). Make sure we default
6639 * to port 139 instead of port 445. srl,crh
6641 name_type = 0x03;
6642 desthost = talloc_strdup(frame,poptGetOptArg(pc));
6643 if (!desthost) {
6644 exit(ENOMEM);
6646 if( !port )
6647 port = NBT_SMB_PORT;
6648 message = true;
6649 break;
6650 case 'I':
6652 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
6653 exit(1);
6655 have_ip = true;
6656 print_sockaddr(dest_ss_str, sizeof(dest_ss_str), &dest_ss);
6658 break;
6659 case 'E':
6660 setup_logging("smbclient", DEBUG_STDERR );
6661 display_set_stderr();
6662 break;
6664 case 'L':
6665 query_host = talloc_strdup(frame, poptGetOptArg(pc));
6666 if (!query_host) {
6667 exit(ENOMEM);
6669 break;
6670 case 'T':
6671 /* We must use old option processing for this. Find the
6672 * position of the -T option in the raw argv[]. */
6674 int i;
6676 for (i = 1; i < argc; i++) {
6677 if (strncmp("-T", argv[i],2)==0)
6678 break;
6680 i++;
6681 if (tar_parse_args(tar_ctx, poptGetOptArg(pc),
6682 const_argv + i, argc - i)) {
6683 poptPrintUsage(pc, stderr, 0);
6684 exit(1);
6687 /* this must be the last option, mark we have parsed it so that we know we have */
6688 tar_opt = true;
6689 break;
6690 case 'D':
6691 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
6692 if (!base_directory) {
6693 exit(ENOMEM);
6695 break;
6696 case 'g':
6697 grepable=true;
6698 break;
6699 case 'q':
6700 quiet=true;
6701 break;
6702 case 'B':
6703 return(do_smb_browse());
6704 case POPT_ERROR_BADOPT:
6705 fprintf(stderr, "\nInvalid option %s: %s\n\n",
6706 poptBadOption(pc, 0), poptStrerror(opt));
6707 poptPrintUsage(pc, stderr, 0);
6708 exit(1);
6712 /* We may still have some leftovers after the last popt option has been called */
6713 if (tar_opt == true) {
6714 while (poptPeekArg(pc)) {
6715 poptGetArg(pc);
6717 tar_opt = false;
6720 /* if the service has not yet been specified lets see if it is available in the popt stack */
6721 if (!service_opt && poptPeekArg(pc)) {
6722 service = talloc_strdup(frame,poptGetArg(pc));
6723 if (!service) {
6724 exit(ENOMEM);
6726 service_opt = true;
6729 /* if the service has already been retrieved then check if we have also a password */
6730 if (service_opt &&
6731 cli_credentials_get_password_obtained(creds) != CRED_SPECIFIED &&
6732 poptPeekArg(pc)) {
6733 cli_credentials_set_password(creds,
6734 poptGetArg(pc),
6735 CRED_SPECIFIED);
6738 if (service_opt && service) {
6739 size_t len;
6741 /* Convert any '/' characters in the service name to '\' characters */
6742 string_replace(service, '/','\\');
6743 if (count_chars(service,'\\') < 3) {
6744 d_printf("\n%s: Not enough '\\' characters in service\n",service);
6745 poptPrintUsage(pc, stderr, 0);
6746 exit(1);
6748 /* Remove trailing slashes */
6749 len = strlen(service);
6750 while(len > 0 && service[len - 1] == '\\') {
6751 --len;
6752 service[len] = '\0';
6756 if(new_name_resolve_order)
6757 lpcfg_set_cmdline(lp_ctx,
6758 "name resolve order",
6759 new_name_resolve_order);
6761 if (!tar_to_process(tar_ctx) && !query_host && !service && !message) {
6762 poptPrintUsage(pc, stderr, 0);
6763 exit(1);
6766 poptFreeContext(pc);
6767 samba_cmdline_burn(argc, argv);
6769 DEBUG(3,("Client started (version %s).\n", samba_version_string()));
6771 if (tar_to_process(tar_ctx)) {
6772 if (cmdstr)
6773 process_command_string(cmdstr);
6774 rc = do_tar_op(base_directory);
6775 } else if (query_host && *query_host) {
6776 char *qhost = query_host;
6777 char *slash;
6779 while (*qhost == '\\' || *qhost == '/')
6780 qhost++;
6782 if ((slash = strchr_m(qhost, '/'))
6783 || (slash = strchr_m(qhost, '\\'))) {
6784 *slash = 0;
6787 if ((p=strchr_m(qhost, '#'))) {
6788 *p = 0;
6789 p++;
6790 sscanf(p, "%x", &name_type);
6793 rc = do_host_query(lp_ctx, qhost);
6794 } else if (message) {
6795 rc = do_message_op(creds);
6796 } else if (process(base_directory)) {
6797 rc = 1;
6800 gfree_all();
6802 TALLOC_FREE(frame);
6803 return rc;