Remove smb_np_struct
[Samba/gebeck_regimport.git] / source3 / client / client.c
blobeb9f31bdf2736a436206f6d0aebadbcaae16ca08
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 "client/client_proto.h"
26 #include "include/rpc_client.h"
27 #ifndef REGISTER
28 #define REGISTER 0
29 #endif
31 extern int do_smb_browse(void); /* mDNS browsing */
33 extern bool AllowDebugChange;
34 extern bool override_logfile;
35 extern char tar_type;
37 static int port = 0;
38 static char *service;
39 static char *desthost;
40 static char *calling_name;
41 static bool grepable = false;
42 static char *cmdstr = NULL;
43 const char *cmd_ptr = NULL;
45 static int io_bufsize = 524288;
47 static int name_type = 0x20;
48 extern int max_protocol;
50 static int process_tok(char *tok);
51 static int cmd_help(void);
53 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
55 /* 30 second timeout on most commands */
56 #define CLIENT_TIMEOUT (30*1000)
57 #define SHORT_TIMEOUT (5*1000)
59 /* value for unused fid field in trans2 secondary request */
60 #define FID_UNUSED (0xFFFF)
62 time_t newer_than = 0;
63 static int archive_level = 0;
65 static bool translation = false;
66 static bool have_ip;
68 /* clitar bits insert */
69 extern int blocksize;
70 extern bool tar_inc;
71 extern bool tar_reset;
72 /* clitar bits end */
74 static bool prompt = true;
76 static bool recurse = false;
77 static bool showacls = false;
78 bool lowercase = false;
80 static struct sockaddr_storage dest_ss;
82 #define SEPARATORS " \t\n\r"
84 static bool abort_mget = true;
86 /* timing globals */
87 SMB_BIG_UINT get_total_size = 0;
88 unsigned int get_total_time_ms = 0;
89 static SMB_BIG_UINT put_total_size = 0;
90 static unsigned int put_total_time_ms = 0;
92 /* totals globals */
93 static double dir_total;
95 /* encrypted state. */
96 static bool smb_encrypt;
98 /* root cli_state connection */
100 struct cli_state *cli;
102 static char CLI_DIRSEP_CHAR = '\\';
103 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
105 /* Accessor functions for directory paths. */
106 static char *fileselection;
107 static const char *client_get_fileselection(void)
109 if (fileselection) {
110 return fileselection;
112 return "";
115 static const char *client_set_fileselection(const char *new_fs)
117 SAFE_FREE(fileselection);
118 if (new_fs) {
119 fileselection = SMB_STRDUP(new_fs);
121 return client_get_fileselection();
124 static char *cwd;
125 static const char *client_get_cwd(void)
127 if (cwd) {
128 return cwd;
130 return CLI_DIRSEP_STR;
133 static const char *client_set_cwd(const char *new_cwd)
135 SAFE_FREE(cwd);
136 if (new_cwd) {
137 cwd = SMB_STRDUP(new_cwd);
139 return client_get_cwd();
142 static char *cur_dir;
143 const char *client_get_cur_dir(void)
145 if (cur_dir) {
146 return cur_dir;
148 return CLI_DIRSEP_STR;
151 const char *client_set_cur_dir(const char *newdir)
153 SAFE_FREE(cur_dir);
154 if (newdir) {
155 cur_dir = SMB_STRDUP(newdir);
157 return client_get_cur_dir();
160 /****************************************************************************
161 Write to a local file with CR/LF->LF translation if appropriate. Return the
162 number taken from the buffer. This may not equal the number written.
163 ****************************************************************************/
165 static int writefile(int f, char *b, int n)
167 int i;
169 if (!translation) {
170 return write(f,b,n);
173 i = 0;
174 while (i < n) {
175 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
176 b++;i++;
178 if (write(f, b, 1) != 1) {
179 break;
181 b++;
182 i++;
185 return(i);
188 /****************************************************************************
189 Read from a file with LF->CR/LF translation if appropriate. Return the
190 number read. read approx n bytes.
191 ****************************************************************************/
193 static int readfile(char *b, int n, XFILE *f)
195 int i;
196 int c;
198 if (!translation)
199 return x_fread(b,1,n,f);
201 i = 0;
202 while (i < (n - 1) && (i < BUFFER_SIZE)) {
203 if ((c = x_getc(f)) == EOF) {
204 break;
207 if (c == '\n') { /* change all LFs to CR/LF */
208 b[i++] = '\r';
211 b[i++] = c;
214 return(i);
217 /****************************************************************************
218 Send a message.
219 ****************************************************************************/
221 static void send_message(void)
223 int total_len = 0;
224 int grp_id;
226 if (!cli_message_start(cli, desthost,
227 get_cmdline_auth_info_username(), &grp_id)) {
228 d_printf("message start: %s\n", cli_errstr(cli));
229 return;
233 d_printf("Connected. Type your message, ending it with a Control-D\n");
235 while (!feof(stdin) && total_len < 1600) {
236 int maxlen = MIN(1600 - total_len,127);
237 char msg[1024];
238 int l=0;
239 int c;
241 ZERO_ARRAY(msg);
243 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
244 if (c == '\n')
245 msg[l++] = '\r';
246 msg[l] = c;
249 if ((total_len > 0) && (strlen(msg) == 0)) {
250 break;
253 if (!cli_message_text(cli, msg, l, grp_id)) {
254 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
255 return;
258 total_len += l;
261 if (total_len >= 1600)
262 d_printf("the message was truncated to 1600 bytes\n");
263 else
264 d_printf("sent %d bytes\n",total_len);
266 if (!cli_message_end(cli, grp_id)) {
267 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
268 return;
272 /****************************************************************************
273 Check the space on a device.
274 ****************************************************************************/
276 static int do_dskattr(void)
278 int total, bsize, avail;
279 struct cli_state *targetcli = NULL;
280 char *targetpath = NULL;
281 TALLOC_CTX *ctx = talloc_tos();
283 if ( !cli_resolve_path(ctx, "", cli, client_get_cur_dir(), &targetcli, &targetpath)) {
284 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
285 return 1;
288 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
289 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
290 return 1;
293 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
294 total, bsize, avail);
296 return 0;
299 /****************************************************************************
300 Show cd/pwd.
301 ****************************************************************************/
303 static int cmd_pwd(void)
305 d_printf("Current directory is %s",service);
306 d_printf("%s\n",client_get_cur_dir());
307 return 0;
310 /****************************************************************************
311 Ensure name has correct directory separators.
312 ****************************************************************************/
314 static void normalize_name(char *newdir)
316 if (!(cli->posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP)) {
317 string_replace(newdir,'/','\\');
321 /****************************************************************************
322 Change directory - inner section.
323 ****************************************************************************/
325 static int do_cd(const char *new_dir)
327 char *newdir = NULL;
328 char *saved_dir = NULL;
329 char *new_cd = NULL;
330 char *targetpath = NULL;
331 struct cli_state *targetcli = NULL;
332 SMB_STRUCT_STAT sbuf;
333 uint32 attributes;
334 int ret = 1;
335 TALLOC_CTX *ctx = talloc_stackframe();
337 newdir = talloc_strdup(ctx, new_dir);
338 if (!newdir) {
339 TALLOC_FREE(ctx);
340 return 1;
343 normalize_name(newdir);
345 /* Save the current directory in case the new directory is invalid */
347 saved_dir = talloc_strdup(ctx, client_get_cur_dir());
348 if (!saved_dir) {
349 TALLOC_FREE(ctx);
350 return 1;
353 if (*newdir == CLI_DIRSEP_CHAR) {
354 client_set_cur_dir(newdir);
355 new_cd = newdir;
356 } else {
357 new_cd = talloc_asprintf(ctx, "%s%s",
358 client_get_cur_dir(),
359 newdir);
360 if (!new_cd) {
361 goto out;
365 /* Ensure cur_dir ends in a DIRSEP */
366 if ((new_cd[0] != '\0') && (*(new_cd+strlen(new_cd)-1) != CLI_DIRSEP_CHAR)) {
367 new_cd = talloc_asprintf_append(new_cd, CLI_DIRSEP_STR);
368 if (!new_cd) {
369 goto out;
372 client_set_cur_dir(new_cd);
374 new_cd = clean_name(ctx, new_cd);
375 client_set_cur_dir(new_cd);
377 if ( !cli_resolve_path(ctx, "", cli, new_cd, &targetcli, &targetpath)) {
378 d_printf("cd %s: %s\n", new_cd, cli_errstr(cli));
379 client_set_cur_dir(saved_dir);
380 goto out;
383 if (strequal(targetpath,CLI_DIRSEP_STR )) {
384 TALLOC_FREE(ctx);
385 return 0;
388 /* Use a trans2_qpathinfo to test directories for modern servers.
389 Except Win9x doesn't support the qpathinfo_basic() call..... */
391 if (targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95) {
392 if (!cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
393 d_printf("cd %s: %s\n", new_cd, cli_errstr(targetcli));
394 client_set_cur_dir(saved_dir);
395 goto out;
398 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
399 d_printf("cd %s: not a directory\n", new_cd);
400 client_set_cur_dir(saved_dir);
401 goto out;
403 } else {
404 targetpath = talloc_asprintf(ctx,
405 "%s%s",
406 targetpath,
407 CLI_DIRSEP_STR );
408 if (!targetpath) {
409 client_set_cur_dir(saved_dir);
410 goto out;
412 targetpath = clean_name(ctx, targetpath);
413 if (!targetpath) {
414 client_set_cur_dir(saved_dir);
415 goto out;
418 if (!cli_chkpath(targetcli, targetpath)) {
419 d_printf("cd %s: %s\n", new_cd, cli_errstr(targetcli));
420 client_set_cur_dir(saved_dir);
421 goto out;
425 ret = 0;
427 out:
429 TALLOC_FREE(ctx);
430 return ret;
433 /****************************************************************************
434 Change directory.
435 ****************************************************************************/
437 static int cmd_cd(void)
439 char *buf = NULL;
440 int rc = 0;
442 if (next_token_talloc(talloc_tos(), &cmd_ptr, &buf,NULL)) {
443 rc = do_cd(buf);
444 } else {
445 d_printf("Current directory is %s\n",client_get_cur_dir());
448 return rc;
451 /****************************************************************************
452 Change directory.
453 ****************************************************************************/
455 static int cmd_cd_oneup(void)
457 return do_cd("..");
460 /*******************************************************************
461 Decide if a file should be operated on.
462 ********************************************************************/
464 static bool do_this_one(file_info *finfo)
466 if (!finfo->name) {
467 return false;
470 if (finfo->mode & aDIR) {
471 return true;
474 if (*client_get_fileselection() &&
475 !mask_match(finfo->name,client_get_fileselection(),false)) {
476 DEBUG(3,("mask_match %s failed\n", finfo->name));
477 return false;
480 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
481 DEBUG(3,("newer_than %s failed\n", finfo->name));
482 return false;
485 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
486 DEBUG(3,("archive %s failed\n", finfo->name));
487 return false;
490 return true;
493 /****************************************************************************
494 Display info about a file.
495 ****************************************************************************/
497 static void display_finfo(file_info *finfo, const char *dir)
499 time_t t;
500 TALLOC_CTX *ctx = talloc_tos();
502 if (!do_this_one(finfo)) {
503 return;
506 t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
507 if (!showacls) {
508 d_printf(" %-30s%7.7s %8.0f %s",
509 finfo->name,
510 attrib_string(finfo->mode),
511 (double)finfo->size,
512 time_to_asc(t));
513 dir_total += finfo->size;
514 } else {
515 char *afname = NULL;
516 int fnum;
518 /* skip if this is . or .. */
519 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
520 return;
521 /* create absolute filename for cli_nt_create() FIXME */
522 afname = talloc_asprintf(ctx,
523 "%s%s%s",
524 dir,
525 CLI_DIRSEP_STR,
526 finfo->name);
527 if (!afname) {
528 return;
530 /* print file meta date header */
531 d_printf( "FILENAME:%s\n", finfo->name);
532 d_printf( "MODE:%s\n", attrib_string(finfo->mode));
533 d_printf( "SIZE:%.0f\n", (double)finfo->size);
534 d_printf( "MTIME:%s", time_to_asc(t));
535 fnum = cli_nt_create(finfo->cli, afname, CREATE_ACCESS_READ);
536 if (fnum == -1) {
537 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
538 afname,
539 cli_errstr( finfo->cli)));
540 } else {
541 SEC_DESC *sd = NULL;
542 sd = cli_query_secdesc(finfo->cli, fnum, ctx);
543 if (!sd) {
544 DEBUG( 0, ("display_finfo() failed to "
545 "get security descriptor: %s",
546 cli_errstr( finfo->cli)));
547 } else {
548 display_sec_desc(sd);
550 TALLOC_FREE(sd);
552 TALLOC_FREE(afname);
556 /****************************************************************************
557 Accumulate size of a file.
558 ****************************************************************************/
560 static void do_du(file_info *finfo, const char *dir)
562 if (do_this_one(finfo)) {
563 dir_total += finfo->size;
567 static bool do_list_recurse;
568 static bool do_list_dirs;
569 static char *do_list_queue = 0;
570 static long do_list_queue_size = 0;
571 static long do_list_queue_start = 0;
572 static long do_list_queue_end = 0;
573 static void (*do_list_fn)(file_info *, const char *dir);
575 /****************************************************************************
576 Functions for do_list_queue.
577 ****************************************************************************/
580 * The do_list_queue is a NUL-separated list of strings stored in a
581 * char*. Since this is a FIFO, we keep track of the beginning and
582 * ending locations of the data in the queue. When we overflow, we
583 * double the size of the char*. When the start of the data passes
584 * the midpoint, we move everything back. This is logically more
585 * complex than a linked list, but easier from a memory management
586 * angle. In any memory error condition, do_list_queue is reset.
587 * Functions check to ensure that do_list_queue is non-NULL before
588 * accessing it.
591 static void reset_do_list_queue(void)
593 SAFE_FREE(do_list_queue);
594 do_list_queue_size = 0;
595 do_list_queue_start = 0;
596 do_list_queue_end = 0;
599 static void init_do_list_queue(void)
601 reset_do_list_queue();
602 do_list_queue_size = 1024;
603 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
604 if (do_list_queue == 0) {
605 d_printf("malloc fail for size %d\n",
606 (int)do_list_queue_size);
607 reset_do_list_queue();
608 } else {
609 memset(do_list_queue, 0, do_list_queue_size);
613 static void adjust_do_list_queue(void)
616 * If the starting point of the queue is more than half way through,
617 * move everything toward the beginning.
620 if (do_list_queue == NULL) {
621 DEBUG(4,("do_list_queue is empty\n"));
622 do_list_queue_start = do_list_queue_end = 0;
623 return;
626 if (do_list_queue_start == do_list_queue_end) {
627 DEBUG(4,("do_list_queue is empty\n"));
628 do_list_queue_start = do_list_queue_end = 0;
629 *do_list_queue = '\0';
630 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
631 DEBUG(4,("sliding do_list_queue backward\n"));
632 memmove(do_list_queue,
633 do_list_queue + do_list_queue_start,
634 do_list_queue_end - do_list_queue_start);
635 do_list_queue_end -= do_list_queue_start;
636 do_list_queue_start = 0;
640 static void add_to_do_list_queue(const char *entry)
642 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
643 while (new_end > do_list_queue_size) {
644 do_list_queue_size *= 2;
645 DEBUG(4,("enlarging do_list_queue to %d\n",
646 (int)do_list_queue_size));
647 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
648 if (! do_list_queue) {
649 d_printf("failure enlarging do_list_queue to %d bytes\n",
650 (int)do_list_queue_size);
651 reset_do_list_queue();
652 } else {
653 memset(do_list_queue + do_list_queue_size / 2,
654 0, do_list_queue_size / 2);
657 if (do_list_queue) {
658 safe_strcpy_base(do_list_queue + do_list_queue_end,
659 entry, do_list_queue, do_list_queue_size);
660 do_list_queue_end = new_end;
661 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
662 entry, (int)do_list_queue_start, (int)do_list_queue_end));
666 static char *do_list_queue_head(void)
668 return do_list_queue + do_list_queue_start;
671 static void remove_do_list_queue_head(void)
673 if (do_list_queue_end > do_list_queue_start) {
674 do_list_queue_start += strlen(do_list_queue_head()) + 1;
675 adjust_do_list_queue();
676 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
677 (int)do_list_queue_start, (int)do_list_queue_end));
681 static int do_list_queue_empty(void)
683 return (! (do_list_queue && *do_list_queue));
686 /****************************************************************************
687 A helper for do_list.
688 ****************************************************************************/
690 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
692 TALLOC_CTX *ctx = talloc_tos();
693 char *dir = NULL;
694 char *dir_end = NULL;
696 /* Work out the directory. */
697 dir = talloc_strdup(ctx, mask);
698 if (!dir) {
699 return;
701 if ((dir_end = strrchr(dir, CLI_DIRSEP_CHAR)) != NULL) {
702 *dir_end = '\0';
705 if (f->mode & aDIR) {
706 if (do_list_dirs && do_this_one(f)) {
707 do_list_fn(f, dir);
709 if (do_list_recurse &&
710 f->name &&
711 !strequal(f->name,".") &&
712 !strequal(f->name,"..")) {
713 char *mask2 = NULL;
714 char *p = NULL;
716 if (!f->name[0]) {
717 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
718 TALLOC_FREE(dir);
719 return;
722 mask2 = talloc_asprintf(ctx,
723 "%s%s",
724 mntpoint,
725 mask);
726 if (!mask2) {
727 TALLOC_FREE(dir);
728 return;
730 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
731 if (!p) {
732 TALLOC_FREE(dir);
733 return;
735 p[1] = 0;
736 mask2 = talloc_asprintf_append(mask2,
737 "%s%s*",
738 f->name,
739 CLI_DIRSEP_STR);
740 if (!mask2) {
741 TALLOC_FREE(dir);
742 return;
744 add_to_do_list_queue(mask2);
745 TALLOC_FREE(mask2);
747 TALLOC_FREE(dir);
748 return;
751 if (do_this_one(f)) {
752 do_list_fn(f,dir);
754 TALLOC_FREE(dir);
757 /****************************************************************************
758 A wrapper around cli_list that adds recursion.
759 ****************************************************************************/
761 void do_list(const char *mask,
762 uint16 attribute,
763 void (*fn)(file_info *, const char *dir),
764 bool rec,
765 bool dirs)
767 static int in_do_list = 0;
768 TALLOC_CTX *ctx = talloc_tos();
769 struct cli_state *targetcli = NULL;
770 char *targetpath = NULL;
772 if (in_do_list && rec) {
773 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
774 exit(1);
777 in_do_list = 1;
779 do_list_recurse = rec;
780 do_list_dirs = dirs;
781 do_list_fn = fn;
783 if (rec) {
784 init_do_list_queue();
785 add_to_do_list_queue(mask);
787 while (!do_list_queue_empty()) {
789 * Need to copy head so that it doesn't become
790 * invalid inside the call to cli_list. This
791 * would happen if the list were expanded
792 * during the call.
793 * Fix from E. Jay Berkenbilt (ejb@ql.org)
795 char *head = talloc_strdup(ctx, do_list_queue_head());
797 if (!head) {
798 return;
801 /* check for dfs */
803 if ( !cli_resolve_path(ctx, "", cli, head, &targetcli, &targetpath ) ) {
804 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
805 remove_do_list_queue_head();
806 continue;
809 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
810 remove_do_list_queue_head();
811 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
812 char *next_file = do_list_queue_head();
813 char *save_ch = 0;
814 if ((strlen(next_file) >= 2) &&
815 (next_file[strlen(next_file) - 1] == '*') &&
816 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
817 save_ch = next_file +
818 strlen(next_file) - 2;
819 *save_ch = '\0';
820 if (showacls) {
821 /* cwd is only used if showacls is on */
822 client_set_cwd(next_file);
825 if (!showacls) /* don't disturbe the showacls output */
826 d_printf("\n%s\n",next_file);
827 if (save_ch) {
828 *save_ch = CLI_DIRSEP_CHAR;
831 TALLOC_FREE(head);
832 TALLOC_FREE(targetpath);
834 } else {
835 /* check for dfs */
836 if (cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetpath)) {
837 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1) {
838 d_printf("%s listing %s\n",
839 cli_errstr(targetcli), targetpath);
841 TALLOC_FREE(targetpath);
842 } else {
843 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
847 in_do_list = 0;
848 reset_do_list_queue();
851 /****************************************************************************
852 Get a directory listing.
853 ****************************************************************************/
855 static int cmd_dir(void)
857 TALLOC_CTX *ctx = talloc_tos();
858 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
859 char *mask = NULL;
860 char *buf = NULL;
861 int rc = 1;
863 dir_total = 0;
864 mask = talloc_strdup(ctx, client_get_cur_dir());
865 if (!mask) {
866 return 1;
869 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
870 normalize_name(buf);
871 if (*buf == CLI_DIRSEP_CHAR) {
872 mask = talloc_strdup(ctx, buf);
873 } else {
874 mask = talloc_asprintf_append(mask, buf);
876 } else {
877 mask = talloc_asprintf_append(mask, "*");
879 if (!mask) {
880 return 1;
883 if (showacls) {
884 /* cwd is only used if showacls is on */
885 client_set_cwd(client_get_cur_dir());
888 do_list(mask, attribute, display_finfo, recurse, true);
890 rc = do_dskattr();
892 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
894 return rc;
897 /****************************************************************************
898 Get a directory listing.
899 ****************************************************************************/
901 static int cmd_du(void)
903 TALLOC_CTX *ctx = talloc_tos();
904 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
905 char *mask = NULL;
906 char *buf = NULL;
907 int rc = 1;
909 dir_total = 0;
910 mask = talloc_strdup(ctx, client_get_cur_dir());
911 if (!mask) {
912 return 1;
914 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR)) {
915 mask = talloc_asprintf_append(mask, CLI_DIRSEP_STR);
916 if (!mask) {
917 return 1;
921 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
922 normalize_name(buf);
923 if (*buf == CLI_DIRSEP_CHAR) {
924 mask = talloc_strdup(ctx, buf);
925 } else {
926 mask = talloc_asprintf_append(mask, buf);
928 } else {
929 mask = talloc_strdup(ctx, "*");
932 do_list(mask, attribute, do_du, recurse, true);
934 rc = do_dskattr();
936 d_printf("Total number of bytes: %.0f\n", dir_total);
938 return rc;
941 static int cmd_echo(void)
943 TALLOC_CTX *ctx = talloc_tos();
944 char *num;
945 char *data;
946 NTSTATUS status;
948 if (!next_token_talloc(ctx, &cmd_ptr, &num, NULL)
949 || !next_token_talloc(ctx, &cmd_ptr, &data, NULL)) {
950 d_printf("echo <num> <data>\n");
951 return 1;
954 status = cli_echo(cli, atoi(num), data_blob_const(data, strlen(data)));
956 if (!NT_STATUS_IS_OK(status)) {
957 d_printf("echo failed: %s\n", nt_errstr(status));
958 return 1;
961 return 0;
964 /****************************************************************************
965 Get a file from rname to lname
966 ****************************************************************************/
968 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
970 int *pfd = (int *)priv;
971 if (writefile(*pfd, buf, n) == -1) {
972 return map_nt_error_from_unix(errno);
974 return NT_STATUS_OK;
977 static int do_get(const char *rname, const char *lname_in, bool reget)
979 TALLOC_CTX *ctx = talloc_tos();
980 int handle = 0, fnum;
981 bool newhandle = false;
982 struct timeval tp_start;
983 uint16 attr;
984 SMB_OFF_T size;
985 off_t start = 0;
986 SMB_OFF_T nread = 0;
987 int rc = 0;
988 struct cli_state *targetcli = NULL;
989 char *targetname = NULL;
990 char *lname = NULL;
991 NTSTATUS status;
993 lname = talloc_strdup(ctx, lname_in);
994 if (!lname) {
995 return 1;
998 if (lowercase) {
999 strlower_m(lname);
1002 if (!cli_resolve_path(ctx, "", cli, rname, &targetcli, &targetname ) ) {
1003 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1004 return 1;
1007 GetTimeOfDay(&tp_start);
1009 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
1011 if (fnum == -1) {
1012 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
1013 return 1;
1016 if(!strcmp(lname,"-")) {
1017 handle = fileno(stdout);
1018 } else {
1019 if (reget) {
1020 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
1021 if (handle >= 0) {
1022 start = sys_lseek(handle, 0, SEEK_END);
1023 if (start == -1) {
1024 d_printf("Error seeking local file\n");
1025 return 1;
1028 } else {
1029 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1031 newhandle = true;
1033 if (handle < 0) {
1034 d_printf("Error opening local file %s\n",lname);
1035 return 1;
1039 if (!cli_qfileinfo(targetcli, fnum,
1040 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
1041 !cli_getattrE(targetcli, fnum,
1042 &attr, &size, NULL, NULL, NULL)) {
1043 d_printf("getattrib: %s\n",cli_errstr(targetcli));
1044 return 1;
1047 DEBUG(1,("getting file %s of size %.0f as %s ",
1048 rname, (double)size, lname));
1050 status = cli_pull(targetcli, fnum, start, size, io_bufsize,
1051 writefile_sink, (void *)&handle, &nread);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 d_fprintf(stderr, "parallel_read returned %s\n",
1054 nt_errstr(status));
1055 cli_close(targetcli, fnum);
1056 return 1;
1059 if (!cli_close(targetcli, fnum)) {
1060 d_printf("Error %s closing remote file\n",cli_errstr(cli));
1061 rc = 1;
1064 if (newhandle) {
1065 close(handle);
1068 if (archive_level >= 2 && (attr & aARCH)) {
1069 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
1073 struct timeval tp_end;
1074 int this_time;
1076 GetTimeOfDay(&tp_end);
1077 this_time =
1078 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1079 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1080 get_total_time_ms += this_time;
1081 get_total_size += nread;
1083 DEBUG(1,("(%3.1f KiloBytes/sec) (average %3.1f KiloBytes/sec)\n",
1084 nread / (1.024*this_time + 1.0e-4),
1085 get_total_size / (1.024*get_total_time_ms)));
1088 TALLOC_FREE(targetname);
1089 return rc;
1092 /****************************************************************************
1093 Get a file.
1094 ****************************************************************************/
1096 static int cmd_get(void)
1098 TALLOC_CTX *ctx = talloc_tos();
1099 char *lname = NULL;
1100 char *rname = NULL;
1101 char *fname = NULL;
1103 rname = talloc_strdup(ctx, client_get_cur_dir());
1104 if (!rname) {
1105 return 1;
1108 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1109 d_printf("get <filename> [localname]\n");
1110 return 1;
1112 rname = talloc_asprintf_append(rname, fname);
1113 if (!rname) {
1114 return 1;
1116 rname = clean_name(ctx, rname);
1117 if (!rname) {
1118 return 1;
1121 next_token_talloc(ctx, &cmd_ptr,&lname,NULL);
1122 if (!lname) {
1123 lname = fname;
1126 return do_get(rname, lname, false);
1129 /****************************************************************************
1130 Do an mget operation on one file.
1131 ****************************************************************************/
1133 static void do_mget(file_info *finfo, const char *dir)
1135 TALLOC_CTX *ctx = talloc_tos();
1136 char *rname = NULL;
1137 char *quest = NULL;
1138 char *saved_curdir = NULL;
1139 char *mget_mask = NULL;
1140 char *new_cd = NULL;
1142 if (!finfo->name) {
1143 return;
1146 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
1147 return;
1149 if (abort_mget) {
1150 d_printf("mget aborted\n");
1151 return;
1154 if (finfo->mode & aDIR) {
1155 if (asprintf(&quest,
1156 "Get directory %s? ",finfo->name) < 0) {
1157 return;
1159 } else {
1160 if (asprintf(&quest,
1161 "Get file %s? ",finfo->name) < 0) {
1162 return;
1166 if (prompt && !yesno(quest)) {
1167 SAFE_FREE(quest);
1168 return;
1170 SAFE_FREE(quest);
1172 if (!(finfo->mode & aDIR)) {
1173 rname = talloc_asprintf(ctx,
1174 "%s%s",
1175 client_get_cur_dir(),
1176 finfo->name);
1177 if (!rname) {
1178 return;
1180 do_get(rname, finfo->name, false);
1181 TALLOC_FREE(rname);
1182 return;
1185 /* handle directories */
1186 saved_curdir = talloc_strdup(ctx, client_get_cur_dir());
1187 if (!saved_curdir) {
1188 return;
1191 new_cd = talloc_asprintf(ctx,
1192 "%s%s%s",
1193 client_get_cur_dir(),
1194 finfo->name,
1195 CLI_DIRSEP_STR);
1196 if (!new_cd) {
1197 return;
1199 client_set_cur_dir(new_cd);
1201 string_replace(finfo->name,'\\','/');
1202 if (lowercase) {
1203 strlower_m(finfo->name);
1206 if (!directory_exist(finfo->name,NULL) &&
1207 mkdir(finfo->name,0777) != 0) {
1208 d_printf("failed to create directory %s\n",finfo->name);
1209 client_set_cur_dir(saved_curdir);
1210 return;
1213 if (chdir(finfo->name) != 0) {
1214 d_printf("failed to chdir to directory %s\n",finfo->name);
1215 client_set_cur_dir(saved_curdir);
1216 return;
1219 mget_mask = talloc_asprintf(ctx,
1220 "%s*",
1221 client_get_cur_dir());
1223 if (!mget_mask) {
1224 return;
1227 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,false, true);
1228 chdir("..");
1229 client_set_cur_dir(saved_curdir);
1230 TALLOC_FREE(mget_mask);
1231 TALLOC_FREE(saved_curdir);
1232 TALLOC_FREE(new_cd);
1235 /****************************************************************************
1236 View the file using the pager.
1237 ****************************************************************************/
1239 static int cmd_more(void)
1241 TALLOC_CTX *ctx = talloc_tos();
1242 char *rname = NULL;
1243 char *fname = NULL;
1244 char *lname = NULL;
1245 char *pager_cmd = NULL;
1246 const char *pager;
1247 int fd;
1248 int rc = 0;
1250 rname = talloc_strdup(ctx, client_get_cur_dir());
1251 if (!rname) {
1252 return 1;
1255 lname = talloc_asprintf(ctx, "%s/smbmore.XXXXXX",tmpdir());
1256 if (!lname) {
1257 return 1;
1259 fd = smb_mkstemp(lname);
1260 if (fd == -1) {
1261 d_printf("failed to create temporary file for more\n");
1262 return 1;
1264 close(fd);
1266 if (!next_token_talloc(ctx, &cmd_ptr,&fname,NULL)) {
1267 d_printf("more <filename>\n");
1268 unlink(lname);
1269 return 1;
1271 rname = talloc_asprintf_append(rname, fname);
1272 if (!rname) {
1273 return 1;
1275 rname = clean_name(ctx,rname);
1276 if (!rname) {
1277 return 1;
1280 rc = do_get(rname, lname, false);
1282 pager=getenv("PAGER");
1284 pager_cmd = talloc_asprintf(ctx,
1285 "%s %s",
1286 (pager? pager:PAGER),
1287 lname);
1288 if (!pager_cmd) {
1289 return 1;
1291 system(pager_cmd);
1292 unlink(lname);
1294 return rc;
1297 /****************************************************************************
1298 Do a mget command.
1299 ****************************************************************************/
1301 static int cmd_mget(void)
1303 TALLOC_CTX *ctx = talloc_tos();
1304 uint16 attribute = aSYSTEM | aHIDDEN;
1305 char *mget_mask = NULL;
1306 char *buf = NULL;
1308 if (recurse) {
1309 attribute |= aDIR;
1312 abort_mget = false;
1314 while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1315 mget_mask = talloc_strdup(ctx, client_get_cur_dir());
1316 if (!mget_mask) {
1317 return 1;
1319 if (*buf == CLI_DIRSEP_CHAR) {
1320 mget_mask = talloc_strdup(ctx, buf);
1321 } else {
1322 mget_mask = talloc_asprintf_append(mget_mask,
1323 buf);
1325 if (!mget_mask) {
1326 return 1;
1328 do_list(mget_mask, attribute, do_mget, false, true);
1331 if (!*mget_mask) {
1332 mget_mask = talloc_asprintf(ctx,
1333 "%s*",
1334 client_get_cur_dir());
1335 if (!mget_mask) {
1336 return 1;
1338 do_list(mget_mask, attribute, do_mget, false, true);
1341 return 0;
1344 /****************************************************************************
1345 Make a directory of name "name".
1346 ****************************************************************************/
1348 static bool do_mkdir(const char *name)
1350 TALLOC_CTX *ctx = talloc_tos();
1351 struct cli_state *targetcli;
1352 char *targetname = NULL;
1354 if (!cli_resolve_path(ctx, "", cli, name, &targetcli, &targetname)) {
1355 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1356 return false;
1359 if (!cli_mkdir(targetcli, targetname)) {
1360 d_printf("%s making remote directory %s\n",
1361 cli_errstr(targetcli),name);
1362 return false;
1365 return true;
1368 /****************************************************************************
1369 Show 8.3 name of a file.
1370 ****************************************************************************/
1372 static bool do_altname(const char *name)
1374 fstring altname;
1376 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1377 d_printf("%s getting alt name for %s\n",
1378 cli_errstr(cli),name);
1379 return false;
1381 d_printf("%s\n", altname);
1383 return true;
1386 /****************************************************************************
1387 Exit client.
1388 ****************************************************************************/
1390 static int cmd_quit(void)
1392 cli_cm_shutdown();
1393 exit(0);
1394 /* NOTREACHED */
1395 return 0;
1398 /****************************************************************************
1399 Make a directory.
1400 ****************************************************************************/
1402 static int cmd_mkdir(void)
1404 TALLOC_CTX *ctx = talloc_tos();
1405 char *mask = NULL;
1406 char *buf = NULL;
1408 mask = talloc_strdup(ctx, client_get_cur_dir());
1409 if (!mask) {
1410 return 1;
1413 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1414 if (!recurse) {
1415 d_printf("mkdir <dirname>\n");
1417 return 1;
1419 mask = talloc_asprintf_append(mask, buf);
1420 if (!mask) {
1421 return 1;
1424 if (recurse) {
1425 char *ddir = NULL;
1426 char *ddir2 = NULL;
1427 struct cli_state *targetcli;
1428 char *targetname = NULL;
1429 char *p = NULL;
1430 char *saveptr;
1432 ddir2 = talloc_strdup(ctx, "");
1433 if (!ddir2) {
1434 return 1;
1437 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
1438 return 1;
1441 ddir = talloc_strdup(ctx, targetname);
1442 if (!ddir) {
1443 return 1;
1445 trim_char(ddir,'.','\0');
1446 p = strtok_r(ddir, "/\\", &saveptr);
1447 while (p) {
1448 ddir2 = talloc_asprintf_append(ddir2, p);
1449 if (!ddir2) {
1450 return 1;
1452 if (!cli_chkpath(targetcli, ddir2)) {
1453 do_mkdir(ddir2);
1455 ddir2 = talloc_asprintf_append(ddir2, CLI_DIRSEP_STR);
1456 if (!ddir2) {
1457 return 1;
1459 p = strtok_r(NULL, "/\\", &saveptr);
1461 } else {
1462 do_mkdir(mask);
1465 return 0;
1468 /****************************************************************************
1469 Show alt name.
1470 ****************************************************************************/
1472 static int cmd_altname(void)
1474 TALLOC_CTX *ctx = talloc_tos();
1475 char *name;
1476 char *buf;
1478 name = talloc_strdup(ctx, client_get_cur_dir());
1479 if (!name) {
1480 return 1;
1483 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1484 d_printf("altname <file>\n");
1485 return 1;
1487 name = talloc_asprintf_append(name, buf);
1488 if (!name) {
1489 return 1;
1491 do_altname(name);
1492 return 0;
1495 /****************************************************************************
1496 Show all info we can get
1497 ****************************************************************************/
1499 static int do_allinfo(const char *name)
1501 fstring altname;
1502 struct timespec b_time, a_time, m_time, c_time;
1503 SMB_OFF_T size;
1504 uint16_t mode;
1505 SMB_INO_T ino;
1506 NTTIME tmp;
1507 unsigned int num_streams;
1508 struct stream_struct *streams;
1509 unsigned int i;
1511 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1512 d_printf("%s getting alt name for %s\n",
1513 cli_errstr(cli),name);
1514 return false;
1516 d_printf("altname: %s\n", altname);
1518 if (!cli_qpathinfo2(cli, name, &b_time, &a_time, &m_time, &c_time,
1519 &size, &mode, &ino)) {
1520 d_printf("%s getting pathinfo for %s\n",
1521 cli_errstr(cli),name);
1522 return false;
1525 unix_timespec_to_nt_time(&tmp, b_time);
1526 d_printf("create_time: %s\n", nt_time_string(talloc_tos(), tmp));
1528 unix_timespec_to_nt_time(&tmp, a_time);
1529 d_printf("access_time: %s\n", nt_time_string(talloc_tos(), tmp));
1531 unix_timespec_to_nt_time(&tmp, m_time);
1532 d_printf("write_time: %s\n", nt_time_string(talloc_tos(), tmp));
1534 unix_timespec_to_nt_time(&tmp, c_time);
1535 d_printf("change_time: %s\n", nt_time_string(talloc_tos(), tmp));
1537 if (!cli_qpathinfo_streams(cli, name, talloc_tos(), &num_streams,
1538 &streams)) {
1539 d_printf("%s getting streams for %s\n",
1540 cli_errstr(cli),name);
1541 return false;
1544 for (i=0; i<num_streams; i++) {
1545 d_printf("stream: [%s], %lld bytes\n", streams[i].name,
1546 (unsigned long long)streams[i].size);
1549 return 0;
1552 /****************************************************************************
1553 Show all info we can get
1554 ****************************************************************************/
1556 static int cmd_allinfo(void)
1558 TALLOC_CTX *ctx = talloc_tos();
1559 char *name;
1560 char *buf;
1562 name = talloc_strdup(ctx, client_get_cur_dir());
1563 if (!name) {
1564 return 1;
1567 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1568 d_printf("allinfo <file>\n");
1569 return 1;
1571 name = talloc_asprintf_append(name, buf);
1572 if (!name) {
1573 return 1;
1576 do_allinfo(name);
1578 return 0;
1581 /****************************************************************************
1582 Put a single file.
1583 ****************************************************************************/
1585 static int do_put(const char *rname, const char *lname, bool reput)
1587 TALLOC_CTX *ctx = talloc_tos();
1588 int fnum;
1589 XFILE *f;
1590 SMB_OFF_T start = 0;
1591 off_t nread = 0;
1592 char *buf = NULL;
1593 int maxwrite = io_bufsize;
1594 int rc = 0;
1595 struct timeval tp_start;
1596 struct cli_state *targetcli;
1597 char *targetname = NULL;
1599 if (!cli_resolve_path(ctx, "", cli, rname, &targetcli, &targetname)) {
1600 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1601 return 1;
1604 GetTimeOfDay(&tp_start);
1606 if (reput) {
1607 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1608 if (fnum >= 0) {
1609 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1610 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1611 d_printf("getattrib: %s\n",cli_errstr(cli));
1612 return 1;
1615 } else {
1616 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1619 if (fnum == -1) {
1620 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1621 return 1;
1624 /* allow files to be piped into smbclient
1625 jdblair 24.jun.98
1627 Note that in this case this function will exit(0) rather
1628 than returning. */
1629 if (!strcmp(lname, "-")) {
1630 f = x_stdin;
1631 /* size of file is not known */
1632 } else {
1633 f = x_fopen(lname,O_RDONLY, 0);
1634 if (f && reput) {
1635 if (x_tseek(f, start, SEEK_SET) == -1) {
1636 d_printf("Error seeking local file\n");
1637 return 1;
1642 if (!f) {
1643 d_printf("Error opening local file %s\n",lname);
1644 return 1;
1647 DEBUG(1,("putting file %s as %s ",lname,
1648 rname));
1650 buf = (char *)SMB_MALLOC(maxwrite);
1651 if (!buf) {
1652 d_printf("ERROR: Not enough memory!\n");
1653 return 1;
1655 while (!x_feof(f)) {
1656 int n = maxwrite;
1657 int ret;
1659 if ((n = readfile(buf,n,f)) < 1) {
1660 if((n == 0) && x_feof(f))
1661 break; /* Empty local file. */
1663 d_printf("Error reading local file: %s\n", strerror(errno));
1664 rc = 1;
1665 break;
1668 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1670 if (n != ret) {
1671 d_printf("Error writing file: %s\n", cli_errstr(cli));
1672 rc = 1;
1673 break;
1676 nread += n;
1679 if (!cli_close(targetcli, fnum)) {
1680 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1681 x_fclose(f);
1682 SAFE_FREE(buf);
1683 return 1;
1686 if (f != x_stdin) {
1687 x_fclose(f);
1690 SAFE_FREE(buf);
1693 struct timeval tp_end;
1694 int this_time;
1696 GetTimeOfDay(&tp_end);
1697 this_time =
1698 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1699 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1700 put_total_time_ms += this_time;
1701 put_total_size += nread;
1703 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1704 nread / (1.024*this_time + 1.0e-4),
1705 put_total_size / (1.024*put_total_time_ms)));
1708 if (f == x_stdin) {
1709 cli_cm_shutdown();
1710 exit(0);
1713 return rc;
1716 /****************************************************************************
1717 Put a file.
1718 ****************************************************************************/
1720 static int cmd_put(void)
1722 TALLOC_CTX *ctx = talloc_tos();
1723 char *lname;
1724 char *rname;
1725 char *buf;
1727 rname = talloc_strdup(ctx, client_get_cur_dir());
1728 if (!rname) {
1729 return 1;
1732 if (!next_token_talloc(ctx, &cmd_ptr,&lname,NULL)) {
1733 d_printf("put <filename>\n");
1734 return 1;
1737 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
1738 rname = talloc_asprintf_append(rname, buf);
1739 } else {
1740 rname = talloc_asprintf_append(rname, lname);
1742 if (!rname) {
1743 return 1;
1746 rname = clean_name(ctx, rname);
1747 if (!rname) {
1748 return 1;
1752 SMB_STRUCT_STAT st;
1753 /* allow '-' to represent stdin
1754 jdblair, 24.jun.98 */
1755 if (!file_exist(lname,&st) &&
1756 (strcmp(lname,"-"))) {
1757 d_printf("%s does not exist\n",lname);
1758 return 1;
1762 return do_put(rname, lname, false);
1765 /*************************************
1766 File list structure.
1767 *************************************/
1769 static struct file_list {
1770 struct file_list *prev, *next;
1771 char *file_path;
1772 bool isdir;
1773 } *file_list;
1775 /****************************************************************************
1776 Free a file_list structure.
1777 ****************************************************************************/
1779 static void free_file_list (struct file_list *list_head)
1781 struct file_list *list, *next;
1783 for (list = list_head; list; list = next) {
1784 next = list->next;
1785 DLIST_REMOVE(list_head, list);
1786 SAFE_FREE(list->file_path);
1787 SAFE_FREE(list);
1791 /****************************************************************************
1792 Seek in a directory/file list until you get something that doesn't start with
1793 the specified name.
1794 ****************************************************************************/
1796 static bool seek_list(struct file_list *list, char *name)
1798 while (list) {
1799 trim_string(list->file_path,"./","\n");
1800 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1801 return true;
1803 list = list->next;
1806 return false;
1809 /****************************************************************************
1810 Set the file selection mask.
1811 ****************************************************************************/
1813 static int cmd_select(void)
1815 TALLOC_CTX *ctx = talloc_tos();
1816 char *new_fs = NULL;
1817 next_token_talloc(ctx, &cmd_ptr,&new_fs,NULL)
1819 if (new_fs) {
1820 client_set_fileselection(new_fs);
1821 } else {
1822 client_set_fileselection("");
1824 return 0;
1827 /****************************************************************************
1828 Recursive file matching function act as find
1829 match must be always set to true when calling this function
1830 ****************************************************************************/
1832 static int file_find(struct file_list **list, const char *directory,
1833 const char *expression, bool match)
1835 SMB_STRUCT_DIR *dir;
1836 struct file_list *entry;
1837 struct stat statbuf;
1838 int ret;
1839 char *path;
1840 bool isdir;
1841 const char *dname;
1843 dir = sys_opendir(directory);
1844 if (!dir)
1845 return -1;
1847 while ((dname = readdirname(dir))) {
1848 if (!strcmp("..", dname))
1849 continue;
1850 if (!strcmp(".", dname))
1851 continue;
1853 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1854 continue;
1857 isdir = false;
1858 if (!match || !gen_fnmatch(expression, dname)) {
1859 if (recurse) {
1860 ret = stat(path, &statbuf);
1861 if (ret == 0) {
1862 if (S_ISDIR(statbuf.st_mode)) {
1863 isdir = true;
1864 ret = file_find(list, path, expression, false);
1866 } else {
1867 d_printf("file_find: cannot stat file %s\n", path);
1870 if (ret == -1) {
1871 SAFE_FREE(path);
1872 sys_closedir(dir);
1873 return -1;
1876 entry = SMB_MALLOC_P(struct file_list);
1877 if (!entry) {
1878 d_printf("Out of memory in file_find\n");
1879 sys_closedir(dir);
1880 return -1;
1882 entry->file_path = path;
1883 entry->isdir = isdir;
1884 DLIST_ADD(*list, entry);
1885 } else {
1886 SAFE_FREE(path);
1890 sys_closedir(dir);
1891 return 0;
1894 /****************************************************************************
1895 mput some files.
1896 ****************************************************************************/
1898 static int cmd_mput(void)
1900 TALLOC_CTX *ctx = talloc_tos();
1901 char *p = NULL;
1903 while (next_token_talloc(ctx, &cmd_ptr,&p,NULL)) {
1904 int ret;
1905 struct file_list *temp_list;
1906 char *quest, *lname, *rname;
1908 file_list = NULL;
1910 ret = file_find(&file_list, ".", p, true);
1911 if (ret) {
1912 free_file_list(file_list);
1913 continue;
1916 quest = NULL;
1917 lname = NULL;
1918 rname = NULL;
1920 for (temp_list = file_list; temp_list;
1921 temp_list = temp_list->next) {
1923 SAFE_FREE(lname);
1924 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0) {
1925 continue;
1927 trim_string(lname, "./", "/");
1929 /* check if it's a directory */
1930 if (temp_list->isdir) {
1931 /* if (!recurse) continue; */
1933 SAFE_FREE(quest);
1934 if (asprintf(&quest, "Put directory %s? ", lname) < 0) {
1935 break;
1937 if (prompt && !yesno(quest)) { /* No */
1938 /* Skip the directory */
1939 lname[strlen(lname)-1] = '/';
1940 if (!seek_list(temp_list, lname))
1941 break;
1942 } else { /* Yes */
1943 SAFE_FREE(rname);
1944 if(asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
1945 break;
1947 normalize_name(rname);
1948 if (!cli_chkpath(cli, rname) &&
1949 !do_mkdir(rname)) {
1950 DEBUG (0, ("Unable to make dir, skipping..."));
1951 /* Skip the directory */
1952 lname[strlen(lname)-1] = '/';
1953 if (!seek_list(temp_list, lname)) {
1954 break;
1958 continue;
1959 } else {
1960 SAFE_FREE(quest);
1961 if (asprintf(&quest,"Put file %s? ", lname) < 0) {
1962 break;
1964 if (prompt && !yesno(quest)) {
1965 /* No */
1966 continue;
1969 /* Yes */
1970 SAFE_FREE(rname);
1971 if (asprintf(&rname, "%s%s", client_get_cur_dir(), lname) < 0) {
1972 break;
1976 normalize_name(rname);
1978 do_put(rname, lname, false);
1980 free_file_list(file_list);
1981 SAFE_FREE(quest);
1982 SAFE_FREE(lname);
1983 SAFE_FREE(rname);
1986 return 0;
1989 /****************************************************************************
1990 Cancel a print job.
1991 ****************************************************************************/
1993 static int do_cancel(int job)
1995 if (cli_printjob_del(cli, job)) {
1996 d_printf("Job %d cancelled\n",job);
1997 return 0;
1998 } else {
1999 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
2000 return 1;
2004 /****************************************************************************
2005 Cancel a print job.
2006 ****************************************************************************/
2008 static int cmd_cancel(void)
2010 TALLOC_CTX *ctx = talloc_tos();
2011 char *buf = NULL;
2012 int job;
2014 if (!next_token_talloc(ctx, &cmd_ptr, &buf,NULL)) {
2015 d_printf("cancel <jobid> ...\n");
2016 return 1;
2018 do {
2019 job = atoi(buf);
2020 do_cancel(job);
2021 } while (next_token_talloc(ctx, &cmd_ptr,&buf,NULL));
2023 return 0;
2026 /****************************************************************************
2027 Print a file.
2028 ****************************************************************************/
2030 static int cmd_print(void)
2032 TALLOC_CTX *ctx = talloc_tos();
2033 char *lname = NULL;
2034 char *rname = NULL;
2035 char *p = NULL;
2037 if (!next_token_talloc(ctx, &cmd_ptr, &lname,NULL)) {
2038 d_printf("print <filename>\n");
2039 return 1;
2042 rname = talloc_strdup(ctx, lname);
2043 if (!rname) {
2044 return 1;
2046 p = strrchr_m(rname,'/');
2047 if (p) {
2048 rname = talloc_asprintf(ctx,
2049 "%s-%d",
2050 p+1,
2051 (int)sys_getpid());
2053 if (strequal(lname,"-")) {
2054 rname = talloc_asprintf(ctx,
2055 "stdin-%d",
2056 (int)sys_getpid());
2058 if (!rname) {
2059 return 1;
2062 return do_put(rname, lname, false);
2065 /****************************************************************************
2066 Show a print queue entry.
2067 ****************************************************************************/
2069 static void queue_fn(struct print_job_info *p)
2071 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
2074 /****************************************************************************
2075 Show a print queue.
2076 ****************************************************************************/
2078 static int cmd_queue(void)
2080 cli_print_queue(cli, queue_fn);
2081 return 0;
2084 /****************************************************************************
2085 Delete some files.
2086 ****************************************************************************/
2088 static void do_del(file_info *finfo, const char *dir)
2090 TALLOC_CTX *ctx = talloc_tos();
2091 char *mask = NULL;
2093 mask = talloc_asprintf(ctx,
2094 "%s%c%s",
2095 dir,
2096 CLI_DIRSEP_CHAR,
2097 finfo->name);
2098 if (!mask) {
2099 return;
2102 if (finfo->mode & aDIR) {
2103 TALLOC_FREE(mask);
2104 return;
2107 if (!cli_unlink(finfo->cli, mask)) {
2108 d_printf("%s deleting remote file %s\n",
2109 cli_errstr(finfo->cli),mask);
2111 TALLOC_FREE(mask);
2114 /****************************************************************************
2115 Delete some files.
2116 ****************************************************************************/
2118 static int cmd_del(void)
2120 TALLOC_CTX *ctx = talloc_tos();
2121 char *mask = NULL;
2122 char *buf = NULL;
2123 uint16 attribute = aSYSTEM | aHIDDEN;
2125 if (recurse) {
2126 attribute |= aDIR;
2129 mask = talloc_strdup(ctx, client_get_cur_dir());
2130 if (!mask) {
2131 return 1;
2133 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2134 d_printf("del <filename>\n");
2135 return 1;
2137 mask = talloc_asprintf_append(mask, buf);
2138 if (!mask) {
2139 return 1;
2142 do_list(mask,attribute,do_del,false,false);
2143 return 0;
2146 /****************************************************************************
2147 Wildcard delete some files.
2148 ****************************************************************************/
2150 static int cmd_wdel(void)
2152 TALLOC_CTX *ctx = talloc_tos();
2153 char *mask = NULL;
2154 char *buf = NULL;
2155 uint16 attribute;
2156 struct cli_state *targetcli;
2157 char *targetname = NULL;
2159 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2160 d_printf("wdel 0x<attrib> <wcard>\n");
2161 return 1;
2164 attribute = (uint16)strtol(buf, (char **)NULL, 16);
2166 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2167 d_printf("wdel 0x<attrib> <wcard>\n");
2168 return 1;
2171 mask = talloc_asprintf(ctx, "%s%s",
2172 client_get_cur_dir(),
2173 buf);
2174 if (!mask) {
2175 return 1;
2178 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2179 d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
2180 return 1;
2183 if (!cli_unlink_full(targetcli, targetname, attribute)) {
2184 d_printf("%s deleting remote files %s\n",cli_errstr(targetcli),targetname);
2186 return 0;
2189 /****************************************************************************
2190 ****************************************************************************/
2192 static int cmd_open(void)
2194 TALLOC_CTX *ctx = talloc_tos();
2195 char *mask = NULL;
2196 char *buf = NULL;
2197 char *targetname = NULL;
2198 struct cli_state *targetcli;
2199 int fnum;
2201 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2202 d_printf("open <filename>\n");
2203 return 1;
2205 mask = talloc_asprintf(ctx,
2206 "%s%s",
2207 client_get_cur_dir(),
2208 buf);
2209 if (!mask) {
2210 return 1;
2213 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2214 d_printf("open %s: %s\n", mask, cli_errstr(cli));
2215 return 1;
2218 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA|FILE_WRITE_DATA);
2219 if (fnum == -1) {
2220 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA);
2221 if (fnum != -1) {
2222 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2223 } else {
2224 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2226 } else {
2227 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
2229 return 0;
2232 static int cmd_posix_encrypt(void)
2234 TALLOC_CTX *ctx = talloc_tos();
2235 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2237 if (cli->use_kerberos) {
2238 status = cli_gss_smb_encryption_start(cli);
2239 } else {
2240 char *domain = NULL;
2241 char *user = NULL;
2242 char *password = NULL;
2244 if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
2245 d_printf("posix_encrypt domain user password\n");
2246 return 1;
2249 if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
2250 d_printf("posix_encrypt domain user password\n");
2251 return 1;
2254 if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
2255 d_printf("posix_encrypt domain user password\n");
2256 return 1;
2259 status = cli_raw_ntlm_smb_encryption_start(cli,
2260 user,
2261 password,
2262 domain);
2265 if (!NT_STATUS_IS_OK(status)) {
2266 d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
2267 } else {
2268 d_printf("encryption on\n");
2269 smb_encrypt = true;
2272 return 0;
2275 /****************************************************************************
2276 ****************************************************************************/
2278 static int cmd_posix_open(void)
2280 TALLOC_CTX *ctx = talloc_tos();
2281 char *mask = NULL;
2282 char *buf = NULL;
2283 char *targetname = NULL;
2284 struct cli_state *targetcli;
2285 mode_t mode;
2286 int fnum;
2288 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2289 d_printf("posix_open <filename> 0<mode>\n");
2290 return 1;
2292 mask = talloc_asprintf(ctx,
2293 "%s%s",
2294 client_get_cur_dir(),
2295 buf);
2296 if (!mask) {
2297 return 1;
2300 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2301 d_printf("posix_open <filename> 0<mode>\n");
2302 return 1;
2304 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2306 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2307 d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
2308 return 1;
2311 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode);
2312 if (fnum == -1) {
2313 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDONLY, mode);
2314 if (fnum != -1) {
2315 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
2316 } else {
2317 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2319 } else {
2320 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
2323 return 0;
2326 static int cmd_posix_mkdir(void)
2328 TALLOC_CTX *ctx = talloc_tos();
2329 char *mask = NULL;
2330 char *buf = NULL;
2331 char *targetname = NULL;
2332 struct cli_state *targetcli;
2333 mode_t mode;
2334 int fnum;
2336 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2337 d_printf("posix_mkdir <filename> 0<mode>\n");
2338 return 1;
2340 mask = talloc_asprintf(ctx,
2341 "%s%s",
2342 client_get_cur_dir(),
2343 buf);
2344 if (!mask) {
2345 return 1;
2348 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2349 d_printf("posix_mkdir <filename> 0<mode>\n");
2350 return 1;
2352 mode = (mode_t)strtol(buf, (char **)NULL, 8);
2354 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2355 d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
2356 return 1;
2359 fnum = cli_posix_mkdir(targetcli, targetname, mode);
2360 if (fnum == -1) {
2361 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
2362 } else {
2363 d_printf("posix_mkdir created directory %s\n", targetname);
2365 return 0;
2368 static int cmd_posix_unlink(void)
2370 TALLOC_CTX *ctx = talloc_tos();
2371 char *mask = NULL;
2372 char *buf = NULL;
2373 char *targetname = NULL;
2374 struct cli_state *targetcli;
2376 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2377 d_printf("posix_unlink <filename>\n");
2378 return 1;
2380 mask = talloc_asprintf(ctx,
2381 "%s%s",
2382 client_get_cur_dir(),
2383 buf);
2384 if (!mask) {
2385 return 1;
2388 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2389 d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
2390 return 1;
2393 if (!cli_posix_unlink(targetcli, targetname)) {
2394 d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
2395 } else {
2396 d_printf("posix_unlink deleted file %s\n", targetname);
2399 return 0;
2402 static int cmd_posix_rmdir(void)
2404 TALLOC_CTX *ctx = talloc_tos();
2405 char *mask = NULL;
2406 char *buf = NULL;
2407 char *targetname = NULL;
2408 struct cli_state *targetcli;
2410 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2411 d_printf("posix_rmdir <filename>\n");
2412 return 1;
2414 mask = talloc_asprintf(ctx,
2415 "%s%s",
2416 client_get_cur_dir(),
2417 buf);
2418 if (!mask) {
2419 return 1;
2422 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2423 d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
2424 return 1;
2427 if (!cli_posix_rmdir(targetcli, targetname)) {
2428 d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
2429 } else {
2430 d_printf("posix_rmdir deleted directory %s\n", targetname);
2433 return 0;
2436 static int cmd_close(void)
2438 TALLOC_CTX *ctx = talloc_tos();
2439 char *buf = NULL;
2440 int fnum;
2442 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2443 d_printf("close <fnum>\n");
2444 return 1;
2447 fnum = atoi(buf);
2448 /* We really should use the targetcli here.... */
2449 if (!cli_close(cli, fnum)) {
2450 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
2451 return 1;
2453 return 0;
2456 static int cmd_posix(void)
2458 TALLOC_CTX *ctx = talloc_tos();
2459 uint16 major, minor;
2460 uint32 caplow, caphigh;
2461 char *caps;
2463 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2464 d_printf("Server doesn't support UNIX CIFS extensions.\n");
2465 return 1;
2468 if (!cli_unix_extensions_version(cli, &major, &minor, &caplow, &caphigh)) {
2469 d_printf("Can't get UNIX CIFS extensions version from server.\n");
2470 return 1;
2473 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2475 caps = talloc_strdup(ctx, "");
2476 if (!caps) {
2477 return 1;
2479 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2480 caps = talloc_asprintf_append(caps, "locks ");
2481 if (!caps) {
2482 return 1;
2485 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2486 caps = talloc_asprintf_append(caps, "acls ");
2487 if (!caps) {
2488 return 1;
2491 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2492 caps = talloc_asprintf_append(caps, "eas ");
2493 if (!caps) {
2494 return 1;
2497 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2498 caps = talloc_asprintf_append(caps, "pathnames ");
2499 if (!caps) {
2500 return 1;
2503 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2504 caps = talloc_asprintf_append(caps, "posix_path_operations ");
2505 if (!caps) {
2506 return 1;
2509 if (caplow & CIFS_UNIX_LARGE_READ_CAP) {
2510 caps = talloc_asprintf_append(caps, "large_read ");
2511 if (!caps) {
2512 return 1;
2515 if (caplow & CIFS_UNIX_LARGE_WRITE_CAP) {
2516 caps = talloc_asprintf_append(caps, "large_write ");
2517 if (!caps) {
2518 return 1;
2521 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) {
2522 caps = talloc_asprintf_append(caps, "posix_encrypt ");
2523 if (!caps) {
2524 return 1;
2527 if (caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) {
2528 caps = talloc_asprintf_append(caps, "mandatory_posix_encrypt ");
2529 if (!caps) {
2530 return 1;
2534 if (*caps && caps[strlen(caps)-1] == ' ') {
2535 caps[strlen(caps)-1] = '\0';
2538 d_printf("Server supports CIFS capabilities %s\n", caps);
2540 if (!cli_set_unix_extensions_capabilities(cli, major, minor, caplow, caphigh)) {
2541 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n", cli_errstr(cli));
2542 return 1;
2545 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2546 CLI_DIRSEP_CHAR = '/';
2547 *CLI_DIRSEP_STR = '/';
2548 client_set_cur_dir(CLI_DIRSEP_STR);
2551 return 0;
2554 static int cmd_lock(void)
2556 TALLOC_CTX *ctx = talloc_tos();
2557 char *buf = NULL;
2558 SMB_BIG_UINT start, len;
2559 enum brl_type lock_type;
2560 int fnum;
2562 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2563 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2564 return 1;
2566 fnum = atoi(buf);
2568 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2569 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2570 return 1;
2573 if (*buf == 'r' || *buf == 'R') {
2574 lock_type = READ_LOCK;
2575 } else if (*buf == 'w' || *buf == 'W') {
2576 lock_type = WRITE_LOCK;
2577 } else {
2578 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2579 return 1;
2582 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2583 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2584 return 1;
2587 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2589 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2590 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2591 return 1;
2594 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2596 if (!cli_posix_lock(cli, fnum, start, len, true, lock_type)) {
2597 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
2600 return 0;
2603 static int cmd_unlock(void)
2605 TALLOC_CTX *ctx = talloc_tos();
2606 char *buf = NULL;
2607 SMB_BIG_UINT start, len;
2608 int fnum;
2610 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2611 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2612 return 1;
2614 fnum = atoi(buf);
2616 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2617 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2618 return 1;
2621 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2623 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2624 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2625 return 1;
2628 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2630 if (!cli_posix_unlock(cli, fnum, start, len)) {
2631 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
2634 return 0;
2638 /****************************************************************************
2639 Remove a directory.
2640 ****************************************************************************/
2642 static int cmd_rmdir(void)
2644 TALLOC_CTX *ctx = talloc_tos();
2645 char *mask = NULL;
2646 char *buf = NULL;
2647 char *targetname = NULL;
2648 struct cli_state *targetcli;
2650 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
2651 d_printf("rmdir <dirname>\n");
2652 return 1;
2654 mask = talloc_asprintf(ctx,
2655 "%s%s",
2656 client_get_cur_dir(),
2657 buf);
2658 if (!mask) {
2659 return 1;
2662 if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
2663 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
2664 return 1;
2667 if (!cli_rmdir(targetcli, targetname)) {
2668 d_printf("%s removing remote directory file %s\n",
2669 cli_errstr(targetcli),mask);
2672 return 0;
2675 /****************************************************************************
2676 UNIX hardlink.
2677 ****************************************************************************/
2679 static int cmd_link(void)
2681 TALLOC_CTX *ctx = talloc_tos();
2682 char *oldname = NULL;
2683 char *newname = NULL;
2684 char *buf = NULL;
2685 char *buf2 = NULL;
2686 char *targetname = NULL;
2687 struct cli_state *targetcli;
2689 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2690 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2691 d_printf("link <oldname> <newname>\n");
2692 return 1;
2694 oldname = talloc_asprintf(ctx,
2695 "%s%s",
2696 client_get_cur_dir(),
2697 buf);
2698 if (!oldname) {
2699 return 1;
2701 newname = talloc_asprintf(ctx,
2702 "%s%s",
2703 client_get_cur_dir(),
2704 buf2);
2705 if (!newname) {
2706 return 1;
2709 if (!cli_resolve_path(ctx, "", cli, oldname, &targetcli, &targetname)) {
2710 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2711 return 1;
2714 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2715 d_printf("Server doesn't support UNIX CIFS calls.\n");
2716 return 1;
2719 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
2720 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
2721 return 1;
2723 return 0;
2726 /****************************************************************************
2727 UNIX symlink.
2728 ****************************************************************************/
2730 static int cmd_symlink(void)
2732 TALLOC_CTX *ctx = talloc_tos();
2733 char *oldname = NULL;
2734 char *newname = NULL;
2735 char *buf = NULL;
2736 char *buf2 = NULL;
2737 char *targetname = NULL;
2738 struct cli_state *targetcli;
2740 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2741 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2742 d_printf("symlink <oldname> <newname>\n");
2743 return 1;
2745 oldname = talloc_asprintf(ctx,
2746 "%s%s",
2747 client_get_cur_dir(),
2748 buf);
2749 if (!oldname) {
2750 return 1;
2752 newname = talloc_asprintf(ctx,
2753 "%s%s",
2754 client_get_cur_dir(),
2755 buf2);
2756 if (!newname) {
2757 return 1;
2760 if (!cli_resolve_path(ctx, "", cli, oldname, &targetcli, &targetname)) {
2761 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2762 return 1;
2765 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2766 d_printf("Server doesn't support UNIX CIFS calls.\n");
2767 return 1;
2770 if (!cli_unix_symlink(targetcli, targetname, newname)) {
2771 d_printf("%s symlinking files (%s -> %s)\n",
2772 cli_errstr(targetcli), newname, targetname);
2773 return 1;
2776 return 0;
2779 /****************************************************************************
2780 UNIX chmod.
2781 ****************************************************************************/
2783 static int cmd_chmod(void)
2785 TALLOC_CTX *ctx = talloc_tos();
2786 char *src = NULL;
2787 char *buf = NULL;
2788 char *buf2 = NULL;
2789 char *targetname = NULL;
2790 struct cli_state *targetcli;
2791 mode_t mode;
2793 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
2794 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
2795 d_printf("chmod mode file\n");
2796 return 1;
2798 src = talloc_asprintf(ctx,
2799 "%s%s",
2800 client_get_cur_dir(),
2801 buf2);
2802 if (!src) {
2803 return 1;
2806 mode = (mode_t)strtol(buf, NULL, 8);
2808 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
2809 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
2810 return 1;
2813 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2814 d_printf("Server doesn't support UNIX CIFS calls.\n");
2815 return 1;
2818 if (!cli_unix_chmod(targetcli, targetname, mode)) {
2819 d_printf("%s chmod file %s 0%o\n",
2820 cli_errstr(targetcli), src, (unsigned int)mode);
2821 return 1;
2824 return 0;
2827 static const char *filetype_to_str(mode_t mode)
2829 if (S_ISREG(mode)) {
2830 return "regular file";
2831 } else if (S_ISDIR(mode)) {
2832 return "directory";
2833 } else
2834 #ifdef S_ISCHR
2835 if (S_ISCHR(mode)) {
2836 return "character device";
2837 } else
2838 #endif
2839 #ifdef S_ISBLK
2840 if (S_ISBLK(mode)) {
2841 return "block device";
2842 } else
2843 #endif
2844 #ifdef S_ISFIFO
2845 if (S_ISFIFO(mode)) {
2846 return "fifo";
2847 } else
2848 #endif
2849 #ifdef S_ISLNK
2850 if (S_ISLNK(mode)) {
2851 return "symbolic link";
2852 } else
2853 #endif
2854 #ifdef S_ISSOCK
2855 if (S_ISSOCK(mode)) {
2856 return "socket";
2857 } else
2858 #endif
2859 return "";
2862 static char rwx_to_str(mode_t m, mode_t bt, char ret)
2864 if (m & bt) {
2865 return ret;
2866 } else {
2867 return '-';
2871 static char *unix_mode_to_str(char *s, mode_t m)
2873 char *p = s;
2874 const char *str = filetype_to_str(m);
2876 switch(str[0]) {
2877 case 'd':
2878 *p++ = 'd';
2879 break;
2880 case 'c':
2881 *p++ = 'c';
2882 break;
2883 case 'b':
2884 *p++ = 'b';
2885 break;
2886 case 'f':
2887 *p++ = 'p';
2888 break;
2889 case 's':
2890 *p++ = str[1] == 'y' ? 'l' : 's';
2891 break;
2892 case 'r':
2893 default:
2894 *p++ = '-';
2895 break;
2897 *p++ = rwx_to_str(m, S_IRUSR, 'r');
2898 *p++ = rwx_to_str(m, S_IWUSR, 'w');
2899 *p++ = rwx_to_str(m, S_IXUSR, 'x');
2900 *p++ = rwx_to_str(m, S_IRGRP, 'r');
2901 *p++ = rwx_to_str(m, S_IWGRP, 'w');
2902 *p++ = rwx_to_str(m, S_IXGRP, 'x');
2903 *p++ = rwx_to_str(m, S_IROTH, 'r');
2904 *p++ = rwx_to_str(m, S_IWOTH, 'w');
2905 *p++ = rwx_to_str(m, S_IXOTH, 'x');
2906 *p++ = '\0';
2907 return s;
2910 /****************************************************************************
2911 Utility function for UNIX getfacl.
2912 ****************************************************************************/
2914 static char *perms_to_string(fstring permstr, unsigned char perms)
2916 fstrcpy(permstr, "---");
2917 if (perms & SMB_POSIX_ACL_READ) {
2918 permstr[0] = 'r';
2920 if (perms & SMB_POSIX_ACL_WRITE) {
2921 permstr[1] = 'w';
2923 if (perms & SMB_POSIX_ACL_EXECUTE) {
2924 permstr[2] = 'x';
2926 return permstr;
2929 /****************************************************************************
2930 UNIX getfacl.
2931 ****************************************************************************/
2933 static int cmd_getfacl(void)
2935 TALLOC_CTX *ctx = talloc_tos();
2936 char *src = NULL;
2937 char *name = NULL;
2938 char *targetname = NULL;
2939 struct cli_state *targetcli;
2940 uint16 major, minor;
2941 uint32 caplow, caphigh;
2942 char *retbuf = NULL;
2943 size_t rb_size = 0;
2944 SMB_STRUCT_STAT sbuf;
2945 uint16 num_file_acls = 0;
2946 uint16 num_dir_acls = 0;
2947 uint16 i;
2949 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
2950 d_printf("getfacl filename\n");
2951 return 1;
2953 src = talloc_asprintf(ctx,
2954 "%s%s",
2955 client_get_cur_dir(),
2956 name);
2957 if (!src) {
2958 return 1;
2961 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
2962 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2963 return 1;
2966 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2967 d_printf("Server doesn't support UNIX CIFS calls.\n");
2968 return 1;
2971 if (!cli_unix_extensions_version(targetcli, &major, &minor,
2972 &caplow, &caphigh)) {
2973 d_printf("Can't get UNIX CIFS version from server.\n");
2974 return 1;
2977 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
2978 d_printf("This server supports UNIX extensions "
2979 "but doesn't support POSIX ACLs.\n");
2980 return 1;
2983 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2984 d_printf("%s getfacl doing a stat on file %s\n",
2985 cli_errstr(targetcli), src);
2986 return 1;
2989 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2990 d_printf("%s getfacl file %s\n",
2991 cli_errstr(targetcli), src);
2992 return 1;
2995 /* ToDo : Print out the ACL values. */
2996 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2997 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2998 src, (unsigned int)CVAL(retbuf,0) );
2999 SAFE_FREE(retbuf);
3000 return 1;
3003 num_file_acls = SVAL(retbuf,2);
3004 num_dir_acls = SVAL(retbuf,4);
3005 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
3006 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
3007 src,
3008 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
3009 (unsigned int)rb_size);
3011 SAFE_FREE(retbuf);
3012 return 1;
3015 d_printf("# file: %s\n", src);
3016 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
3018 if (num_file_acls == 0 && num_dir_acls == 0) {
3019 d_printf("No acls found.\n");
3022 for (i = 0; i < num_file_acls; i++) {
3023 uint32 uorg;
3024 fstring permstring;
3025 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
3026 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3028 switch(tagtype) {
3029 case SMB_POSIX_ACL_USER_OBJ:
3030 d_printf("user::");
3031 break;
3032 case SMB_POSIX_ACL_USER:
3033 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3034 d_printf("user:%u:", uorg);
3035 break;
3036 case SMB_POSIX_ACL_GROUP_OBJ:
3037 d_printf("group::");
3038 break;
3039 case SMB_POSIX_ACL_GROUP:
3040 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3041 d_printf("group:%u:", uorg);
3042 break;
3043 case SMB_POSIX_ACL_MASK:
3044 d_printf("mask::");
3045 break;
3046 case SMB_POSIX_ACL_OTHER:
3047 d_printf("other::");
3048 break;
3049 default:
3050 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3051 src, (unsigned int)tagtype );
3052 SAFE_FREE(retbuf);
3053 return 1;
3056 d_printf("%s\n", perms_to_string(permstring, perms));
3059 for (i = 0; i < num_dir_acls; i++) {
3060 uint32 uorg;
3061 fstring permstring;
3062 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
3063 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
3065 switch(tagtype) {
3066 case SMB_POSIX_ACL_USER_OBJ:
3067 d_printf("default:user::");
3068 break;
3069 case SMB_POSIX_ACL_USER:
3070 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3071 d_printf("default:user:%u:", uorg);
3072 break;
3073 case SMB_POSIX_ACL_GROUP_OBJ:
3074 d_printf("default:group::");
3075 break;
3076 case SMB_POSIX_ACL_GROUP:
3077 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
3078 d_printf("default:group:%u:", uorg);
3079 break;
3080 case SMB_POSIX_ACL_MASK:
3081 d_printf("default:mask::");
3082 break;
3083 case SMB_POSIX_ACL_OTHER:
3084 d_printf("default:other::");
3085 break;
3086 default:
3087 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
3088 src, (unsigned int)tagtype );
3089 SAFE_FREE(retbuf);
3090 return 1;
3093 d_printf("%s\n", perms_to_string(permstring, perms));
3096 SAFE_FREE(retbuf);
3097 return 0;
3100 /****************************************************************************
3101 UNIX stat.
3102 ****************************************************************************/
3104 static int cmd_stat(void)
3106 TALLOC_CTX *ctx = talloc_tos();
3107 char *src = NULL;
3108 char *name = NULL;
3109 char *targetname = NULL;
3110 struct cli_state *targetcli;
3111 fstring mode_str;
3112 SMB_STRUCT_STAT sbuf;
3113 struct tm *lt;
3115 if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
3116 d_printf("stat file\n");
3117 return 1;
3119 src = talloc_asprintf(ctx,
3120 "%s%s",
3121 client_get_cur_dir(),
3122 name);
3123 if (!src) {
3124 return 1;
3127 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
3128 d_printf("stat %s: %s\n", src, cli_errstr(cli));
3129 return 1;
3132 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3133 d_printf("Server doesn't support UNIX CIFS calls.\n");
3134 return 1;
3137 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
3138 d_printf("%s stat file %s\n",
3139 cli_errstr(targetcli), src);
3140 return 1;
3143 /* Print out the stat values. */
3144 d_printf("File: %s\n", src);
3145 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
3146 (double)sbuf.st_size,
3147 (unsigned int)sbuf.st_blocks,
3148 filetype_to_str(sbuf.st_mode));
3150 #if defined(S_ISCHR) && defined(S_ISBLK)
3151 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
3152 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
3153 (double)sbuf.st_ino,
3154 (unsigned int)sbuf.st_nlink,
3155 unix_dev_major(sbuf.st_rdev),
3156 unix_dev_minor(sbuf.st_rdev));
3157 } else
3158 #endif
3159 d_printf("Inode: %.0f\tLinks: %u\n",
3160 (double)sbuf.st_ino,
3161 (unsigned int)sbuf.st_nlink);
3163 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
3164 ((int)sbuf.st_mode & 0777),
3165 unix_mode_to_str(mode_str, sbuf.st_mode),
3166 (unsigned int)sbuf.st_uid,
3167 (unsigned int)sbuf.st_gid);
3169 lt = localtime(&sbuf.st_atime);
3170 if (lt) {
3171 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3172 } else {
3173 fstrcpy(mode_str, "unknown");
3175 d_printf("Access: %s\n", mode_str);
3177 lt = localtime(&sbuf.st_mtime);
3178 if (lt) {
3179 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3180 } else {
3181 fstrcpy(mode_str, "unknown");
3183 d_printf("Modify: %s\n", mode_str);
3185 lt = localtime(&sbuf.st_ctime);
3186 if (lt) {
3187 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
3188 } else {
3189 fstrcpy(mode_str, "unknown");
3191 d_printf("Change: %s\n", mode_str);
3193 return 0;
3197 /****************************************************************************
3198 UNIX chown.
3199 ****************************************************************************/
3201 static int cmd_chown(void)
3203 TALLOC_CTX *ctx = talloc_tos();
3204 char *src = NULL;
3205 uid_t uid;
3206 gid_t gid;
3207 char *buf, *buf2, *buf3;
3208 struct cli_state *targetcli;
3209 char *targetname = NULL;
3211 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3212 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL) ||
3213 !next_token_talloc(ctx, &cmd_ptr,&buf3,NULL)) {
3214 d_printf("chown uid gid file\n");
3215 return 1;
3218 uid = (uid_t)atoi(buf);
3219 gid = (gid_t)atoi(buf2);
3221 src = talloc_asprintf(ctx,
3222 "%s%s",
3223 client_get_cur_dir(),
3224 buf3);
3225 if (!src) {
3226 return 1;
3228 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname) ) {
3229 d_printf("chown %s: %s\n", src, cli_errstr(cli));
3230 return 1;
3233 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
3234 d_printf("Server doesn't support UNIX CIFS calls.\n");
3235 return 1;
3238 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
3239 d_printf("%s chown file %s uid=%d, gid=%d\n",
3240 cli_errstr(targetcli), src, (int)uid, (int)gid);
3241 return 1;
3244 return 0;
3247 /****************************************************************************
3248 Rename some file.
3249 ****************************************************************************/
3251 static int cmd_rename(void)
3253 TALLOC_CTX *ctx = talloc_tos();
3254 char *src, *dest;
3255 char *buf, *buf2;
3256 struct cli_state *targetcli;
3257 char *targetsrc;
3258 char *targetdest;
3260 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3261 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3262 d_printf("rename <src> <dest>\n");
3263 return 1;
3266 src = talloc_asprintf(ctx,
3267 "%s%s",
3268 client_get_cur_dir(),
3269 buf);
3270 if (!src) {
3271 return 1;
3274 dest = talloc_asprintf(ctx,
3275 "%s%s",
3276 client_get_cur_dir(),
3277 buf2);
3278 if (!dest) {
3279 return 1;
3282 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetsrc)) {
3283 d_printf("rename %s: %s\n", src, cli_errstr(cli));
3284 return 1;
3287 if (!cli_resolve_path(ctx, "", cli, dest, &targetcli, &targetdest)) {
3288 d_printf("rename %s: %s\n", dest, cli_errstr(cli));
3289 return 1;
3292 if (!cli_rename(targetcli, targetsrc, targetdest)) {
3293 d_printf("%s renaming files %s -> %s \n",
3294 cli_errstr(targetcli),
3295 targetsrc,
3296 targetdest);
3297 return 1;
3300 return 0;
3303 /****************************************************************************
3304 Print the volume name.
3305 ****************************************************************************/
3307 static int cmd_volume(void)
3309 fstring volname;
3310 uint32 serial_num;
3311 time_t create_date;
3313 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
3314 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
3315 return 1;
3318 d_printf("Volume: |%s| serial number 0x%x\n",
3319 volname, (unsigned int)serial_num);
3320 return 0;
3323 /****************************************************************************
3324 Hard link files using the NT call.
3325 ****************************************************************************/
3327 static int cmd_hardlink(void)
3329 TALLOC_CTX *ctx = talloc_tos();
3330 char *src, *dest;
3331 char *buf, *buf2;
3332 struct cli_state *targetcli;
3333 char *targetname;
3335 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
3336 !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
3337 d_printf("hardlink <src> <dest>\n");
3338 return 1;
3341 src = talloc_asprintf(ctx,
3342 "%s%s",
3343 client_get_cur_dir(),
3344 buf);
3345 if (!src) {
3346 return 1;
3349 dest = talloc_asprintf(ctx,
3350 "%s%s",
3351 client_get_cur_dir(),
3352 buf2);
3353 if (!dest) {
3354 return 1;
3357 if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
3358 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
3359 return 1;
3362 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
3363 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
3364 return 1;
3367 return 0;
3370 /****************************************************************************
3371 Toggle the prompt flag.
3372 ****************************************************************************/
3374 static int cmd_prompt(void)
3376 prompt = !prompt;
3377 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
3378 return 1;
3381 /****************************************************************************
3382 Set the newer than time.
3383 ****************************************************************************/
3385 static int cmd_newer(void)
3387 TALLOC_CTX *ctx = talloc_tos();
3388 char *buf;
3389 bool ok;
3390 SMB_STRUCT_STAT sbuf;
3392 ok = next_token_talloc(ctx, &cmd_ptr,&buf,NULL);
3393 if (ok && (sys_stat(buf,&sbuf) == 0)) {
3394 newer_than = sbuf.st_mtime;
3395 DEBUG(1,("Getting files newer than %s",
3396 time_to_asc(newer_than)));
3397 } else {
3398 newer_than = 0;
3401 if (ok && newer_than == 0) {
3402 d_printf("Error setting newer-than time\n");
3403 return 1;
3406 return 0;
3409 /****************************************************************************
3410 Set the archive level.
3411 ****************************************************************************/
3413 static int cmd_archive(void)
3415 TALLOC_CTX *ctx = talloc_tos();
3416 char *buf;
3418 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3419 archive_level = atoi(buf);
3420 } else {
3421 d_printf("Archive level is %d\n",archive_level);
3424 return 0;
3427 /****************************************************************************
3428 Toggle the lowercaseflag.
3429 ****************************************************************************/
3431 static int cmd_lowercase(void)
3433 lowercase = !lowercase;
3434 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
3435 return 0;
3438 /****************************************************************************
3439 Toggle the case sensitive flag.
3440 ****************************************************************************/
3442 static int cmd_setcase(void)
3444 bool orig_case_sensitive = cli_set_case_sensitive(cli, false);
3446 cli_set_case_sensitive(cli, !orig_case_sensitive);
3447 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
3448 "on":"off"));
3449 return 0;
3452 /****************************************************************************
3453 Toggle the showacls flag.
3454 ****************************************************************************/
3456 static int cmd_showacls(void)
3458 showacls = !showacls;
3459 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
3460 return 0;
3464 /****************************************************************************
3465 Toggle the recurse flag.
3466 ****************************************************************************/
3468 static int cmd_recurse(void)
3470 recurse = !recurse;
3471 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
3472 return 0;
3475 /****************************************************************************
3476 Toggle the translate flag.
3477 ****************************************************************************/
3479 static int cmd_translate(void)
3481 translation = !translation;
3482 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
3483 translation?"on":"off"));
3484 return 0;
3487 /****************************************************************************
3488 Do the lcd command.
3489 ****************************************************************************/
3491 static int cmd_lcd(void)
3493 TALLOC_CTX *ctx = talloc_tos();
3494 char *buf;
3495 char *d;
3497 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3498 chdir(buf);
3500 d = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
3501 if (!d) {
3502 return 1;
3504 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
3505 return 0;
3508 /****************************************************************************
3509 Get a file restarting at end of local file.
3510 ****************************************************************************/
3512 static int cmd_reget(void)
3514 TALLOC_CTX *ctx = talloc_tos();
3515 char *local_name = NULL;
3516 char *remote_name = NULL;
3517 char *fname = NULL;
3518 char *p = NULL;
3520 remote_name = talloc_strdup(ctx, client_get_cur_dir());
3521 if (!remote_name) {
3522 return 1;
3525 if (!next_token_talloc(ctx, &cmd_ptr, &fname, NULL)) {
3526 d_printf("reget <filename>\n");
3527 return 1;
3529 remote_name = talloc_asprintf_append(remote_name, fname);
3530 if (!remote_name) {
3531 return 1;
3533 remote_name = clean_name(ctx,remote_name);
3534 if (!remote_name) {
3535 return 1;
3538 local_name = fname;
3539 next_token_talloc(ctx, &cmd_ptr, &p, NULL);
3540 if (p) {
3541 local_name = p;
3544 return do_get(remote_name, local_name, true);
3547 /****************************************************************************
3548 Put a file restarting at end of local file.
3549 ****************************************************************************/
3551 static int cmd_reput(void)
3553 TALLOC_CTX *ctx = talloc_tos();
3554 char *local_name = NULL;
3555 char *remote_name = NULL;
3556 char *buf;
3557 SMB_STRUCT_STAT st;
3559 remote_name = talloc_strdup(ctx, client_get_cur_dir());
3560 if (!remote_name) {
3561 return 1;
3564 if (!next_token_talloc(ctx, &cmd_ptr, &local_name, NULL)) {
3565 d_printf("reput <filename>\n");
3566 return 1;
3569 if (!file_exist(local_name, &st)) {
3570 d_printf("%s does not exist\n", local_name);
3571 return 1;
3574 if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
3575 remote_name = talloc_asprintf_append(remote_name,
3576 buf);
3577 } else {
3578 remote_name = talloc_asprintf_append(remote_name,
3579 local_name);
3581 if (!remote_name) {
3582 return 1;
3585 remote_name = clean_name(ctx, remote_name);
3586 if (!remote_name) {
3587 return 1;
3590 return do_put(remote_name, local_name, true);
3593 /****************************************************************************
3594 List a share name.
3595 ****************************************************************************/
3597 static void browse_fn(const char *name, uint32 m,
3598 const char *comment, void *state)
3600 const char *typestr = "";
3602 switch (m & 7) {
3603 case STYPE_DISKTREE:
3604 typestr = "Disk";
3605 break;
3606 case STYPE_PRINTQ:
3607 typestr = "Printer";
3608 break;
3609 case STYPE_DEVICE:
3610 typestr = "Device";
3611 break;
3612 case STYPE_IPC:
3613 typestr = "IPC";
3614 break;
3616 /* FIXME: If the remote machine returns non-ascii characters
3617 in any of these fields, they can corrupt the output. We
3618 should remove them. */
3619 if (!grepable) {
3620 d_printf("\t%-15s %-10.10s%s\n",
3621 name,typestr,comment);
3622 } else {
3623 d_printf ("%s|%s|%s\n",typestr,name,comment);
3627 static bool browse_host_rpc(bool sort)
3629 NTSTATUS status;
3630 struct rpc_pipe_client *pipe_hnd;
3631 TALLOC_CTX *frame = talloc_stackframe();
3632 WERROR werr;
3633 struct srvsvc_NetShareInfoCtr info_ctr;
3634 struct srvsvc_NetShareCtr1 ctr1;
3635 uint32_t resume_handle = 0;
3636 uint32_t total_entries = 0;
3637 int i;
3639 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
3640 &pipe_hnd);
3642 if (!NT_STATUS_IS_OK(status)) {
3643 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
3644 nt_errstr(status)));
3645 TALLOC_FREE(frame);
3646 return false;
3649 ZERO_STRUCT(info_ctr);
3650 ZERO_STRUCT(ctr1);
3652 info_ctr.level = 1;
3653 info_ctr.ctr.ctr1 = &ctr1;
3655 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, frame,
3656 pipe_hnd->desthost,
3657 &info_ctr,
3658 0xffffffff,
3659 &total_entries,
3660 &resume_handle,
3661 &werr);
3663 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
3664 TALLOC_FREE(pipe_hnd);
3665 TALLOC_FREE(frame);
3666 return false;
3669 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
3670 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
3671 browse_fn(info.name, info.type, info.comment, NULL);
3674 TALLOC_FREE(pipe_hnd);
3675 TALLOC_FREE(frame);
3676 return true;
3679 /****************************************************************************
3680 Try and browse available connections on a host.
3681 ****************************************************************************/
3683 static bool browse_host(bool sort)
3685 int ret;
3686 if (!grepable) {
3687 d_printf("\n\tSharename Type Comment\n");
3688 d_printf("\t--------- ---- -------\n");
3691 if (browse_host_rpc(sort)) {
3692 return true;
3695 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
3696 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
3698 return (ret != -1);
3701 /****************************************************************************
3702 List a server name.
3703 ****************************************************************************/
3705 static void server_fn(const char *name, uint32 m,
3706 const char *comment, void *state)
3709 if (!grepable){
3710 d_printf("\t%-16s %s\n", name, comment);
3711 } else {
3712 d_printf("%s|%s|%s\n",(char *)state, name, comment);
3716 /****************************************************************************
3717 Try and browse available connections on a host.
3718 ****************************************************************************/
3720 static bool list_servers(const char *wk_grp)
3722 fstring state;
3724 if (!cli->server_domain)
3725 return false;
3727 if (!grepable) {
3728 d_printf("\n\tServer Comment\n");
3729 d_printf("\t--------- -------\n");
3731 fstrcpy( state, "Server" );
3732 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
3733 state);
3735 if (!grepable) {
3736 d_printf("\n\tWorkgroup Master\n");
3737 d_printf("\t--------- -------\n");
3740 fstrcpy( state, "Workgroup" );
3741 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
3742 server_fn, state);
3743 return true;
3746 /****************************************************************************
3747 Print or set current VUID
3748 ****************************************************************************/
3750 static int cmd_vuid(void)
3752 TALLOC_CTX *ctx = talloc_tos();
3753 char *buf;
3755 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3756 d_printf("Current VUID is %d\n", cli->vuid);
3757 return 0;
3760 cli->vuid = atoi(buf);
3761 return 0;
3764 /****************************************************************************
3765 Setup a new VUID, by issuing a session setup
3766 ****************************************************************************/
3768 static int cmd_logon(void)
3770 TALLOC_CTX *ctx = talloc_tos();
3771 char *l_username, *l_password;
3773 if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
3774 d_printf("logon <username> [<password>]\n");
3775 return 0;
3778 if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
3779 char *pass = getpass("Password: ");
3780 if (pass) {
3781 l_password = talloc_strdup(ctx,pass);
3784 if (!l_password) {
3785 return 1;
3788 if (!NT_STATUS_IS_OK(cli_session_setup(cli, l_username,
3789 l_password, strlen(l_password),
3790 l_password, strlen(l_password),
3791 lp_workgroup()))) {
3792 d_printf("session setup failed: %s\n", cli_errstr(cli));
3793 return -1;
3796 d_printf("Current VUID is %d\n", cli->vuid);
3797 return 0;
3801 /****************************************************************************
3802 list active connections
3803 ****************************************************************************/
3805 static int cmd_list_connect(void)
3807 cli_cm_display();
3808 return 0;
3811 /****************************************************************************
3812 display the current active client connection
3813 ****************************************************************************/
3815 static int cmd_show_connect( void )
3817 TALLOC_CTX *ctx = talloc_tos();
3818 struct cli_state *targetcli;
3819 char *targetpath;
3821 if (!cli_resolve_path(ctx, "", cli, client_get_cur_dir(),
3822 &targetcli, &targetpath ) ) {
3823 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
3824 return 1;
3827 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
3828 return 0;
3831 /****************************************************************************
3832 iosize command
3833 ***************************************************************************/
3835 int cmd_iosize(void)
3837 TALLOC_CTX *ctx = talloc_tos();
3838 char *buf;
3839 int iosize;
3841 if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
3842 if (!smb_encrypt) {
3843 d_printf("iosize <n> or iosize 0x<n>. "
3844 "Minimum is 16384 (0x4000), "
3845 "max is 16776960 (0xFFFF00)\n");
3846 } else {
3847 d_printf("iosize <n> or iosize 0x<n>. "
3848 "(Encrypted connection) ,"
3849 "Minimum is 16384 (0x4000), "
3850 "max is 130048 (0x1FC00)\n");
3852 return 1;
3855 iosize = strtol(buf,NULL,0);
3856 if (smb_encrypt && (iosize < 0x4000 || iosize > 0xFC00)) {
3857 d_printf("iosize out of range for encrypted "
3858 "connection (min = 16384 (0x4000), "
3859 "max = 130048 (0x1FC00)");
3860 return 1;
3861 } else if (!smb_encrypt && (iosize < 0x4000 || iosize > 0xFFFF00)) {
3862 d_printf("iosize out of range (min = 16384 (0x4000), "
3863 "max = 16776960 (0xFFFF00)");
3864 return 1;
3867 io_bufsize = iosize;
3868 d_printf("iosize is now %d\n", io_bufsize);
3869 return 0;
3873 /* Some constants for completing filename arguments */
3875 #define COMPL_NONE 0 /* No completions */
3876 #define COMPL_REMOTE 1 /* Complete remote filename */
3877 #define COMPL_LOCAL 2 /* Complete local filename */
3879 /* This defines the commands supported by this client.
3880 * NOTE: The "!" must be the last one in the list because it's fn pointer
3881 * field is NULL, and NULL in that field is used in process_tok()
3882 * (below) to indicate the end of the list. crh
3884 static struct {
3885 const char *name;
3886 int (*fn)(void);
3887 const char *description;
3888 char compl_args[2]; /* Completion argument info */
3889 } commands[] = {
3890 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3891 {"allinfo",cmd_allinfo,"<file> show all available info",
3892 {COMPL_NONE,COMPL_NONE}},
3893 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
3894 {"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}},
3895 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
3896 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
3897 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
3898 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
3899 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
3900 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
3901 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
3902 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3903 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3904 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3905 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
3906 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3907 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
3908 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
3909 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3910 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3911 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
3912 {"iosize",cmd_iosize,"iosize <number> (default 64512)",{COMPL_NONE,COMPL_NONE}},
3913 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
3914 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3915 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3916 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
3917 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3918 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3919 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
3920 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3921 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
3922 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3923 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
3924 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
3925 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
3926 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
3927 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
3928 {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
3929 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3930 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3931 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3932 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3933 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
3934 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
3935 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
3936 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
3937 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3938 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
3939 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3940 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3941 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
3942 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
3943 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
3944 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
3945 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3946 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3947 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
3948 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
3949 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
3950 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
3951 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
3952 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
3953 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
3954 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3955 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
3956 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
3957 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3958 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
3959 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
3960 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
3961 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
3963 /* Yes, this must be here, see crh's comment above. */
3964 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
3965 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
3968 /*******************************************************************
3969 Lookup a command string in the list of commands, including
3970 abbreviations.
3971 ******************************************************************/
3973 static int process_tok(char *tok)
3975 int i = 0, matches = 0;
3976 int cmd=0;
3977 int tok_len = strlen(tok);
3979 while (commands[i].fn != NULL) {
3980 if (strequal(commands[i].name,tok)) {
3981 matches = 1;
3982 cmd = i;
3983 break;
3984 } else if (strnequal(commands[i].name, tok, tok_len)) {
3985 matches++;
3986 cmd = i;
3988 i++;
3991 if (matches == 0)
3992 return(-1);
3993 else if (matches == 1)
3994 return(cmd);
3995 else
3996 return(-2);
3999 /****************************************************************************
4000 Help.
4001 ****************************************************************************/
4003 static int cmd_help(void)
4005 TALLOC_CTX *ctx = talloc_tos();
4006 int i=0,j;
4007 char *buf;
4009 if (next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
4010 if ((i = process_tok(buf)) >= 0)
4011 d_printf("HELP %s:\n\t%s\n\n",
4012 commands[i].name,commands[i].description);
4013 } else {
4014 while (commands[i].description) {
4015 for (j=0; commands[i].description && (j<5); j++) {
4016 d_printf("%-15s",commands[i].name);
4017 i++;
4019 d_printf("\n");
4022 return 0;
4025 /****************************************************************************
4026 Process a -c command string.
4027 ****************************************************************************/
4029 static int process_command_string(const char *cmd_in)
4031 TALLOC_CTX *ctx = talloc_tos();
4032 char *cmd = talloc_strdup(ctx, cmd_in);
4033 int rc = 0;
4035 if (!cmd) {
4036 return 1;
4038 /* establish the connection if not already */
4040 if (!cli) {
4041 cli = cli_cm_open(talloc_tos(), NULL, desthost,
4042 service, true, smb_encrypt);
4043 if (!cli) {
4044 return 1;
4048 while (cmd[0] != '\0') {
4049 char *line;
4050 char *p;
4051 char *tok;
4052 int i;
4054 if ((p = strchr_m(cmd, ';')) == 0) {
4055 line = cmd;
4056 cmd += strlen(cmd);
4057 } else {
4058 *p = '\0';
4059 line = cmd;
4060 cmd = p + 1;
4063 /* and get the first part of the command */
4064 cmd_ptr = line;
4065 if (!next_token_talloc(ctx, &cmd_ptr,&tok,NULL)) {
4066 continue;
4069 if ((i = process_tok(tok)) >= 0) {
4070 rc = commands[i].fn();
4071 } else if (i == -2) {
4072 d_printf("%s: command abbreviation ambiguous\n",tok);
4073 } else {
4074 d_printf("%s: command not found\n",tok);
4078 return rc;
4081 #define MAX_COMPLETIONS 100
4083 typedef struct {
4084 char *dirmask;
4085 char **matches;
4086 int count, samelen;
4087 const char *text;
4088 int len;
4089 } completion_remote_t;
4091 static void completion_remote_filter(const char *mnt,
4092 file_info *f,
4093 const char *mask,
4094 void *state)
4096 completion_remote_t *info = (completion_remote_t *)state;
4098 if ((info->count < MAX_COMPLETIONS - 1) &&
4099 (strncmp(info->text, f->name, info->len) == 0) &&
4100 (strcmp(f->name, ".") != 0) &&
4101 (strcmp(f->name, "..") != 0)) {
4102 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
4103 info->matches[info->count] = SMB_STRDUP(f->name);
4104 else {
4105 TALLOC_CTX *ctx = talloc_stackframe();
4106 char *tmp;
4108 tmp = talloc_strdup(ctx,info->dirmask);
4109 if (!tmp) {
4110 TALLOC_FREE(ctx);
4111 return;
4113 tmp = talloc_asprintf_append(tmp, f->name);
4114 if (!tmp) {
4115 TALLOC_FREE(ctx);
4116 return;
4118 if (f->mode & aDIR) {
4119 tmp = talloc_asprintf_append(tmp, CLI_DIRSEP_STR);
4121 if (!tmp) {
4122 TALLOC_FREE(ctx);
4123 return;
4125 info->matches[info->count] = SMB_STRDUP(tmp);
4126 TALLOC_FREE(ctx);
4128 if (info->matches[info->count] == NULL) {
4129 return;
4131 if (f->mode & aDIR) {
4132 smb_readline_ca_char(0);
4134 if (info->count == 1) {
4135 info->samelen = strlen(info->matches[info->count]);
4136 } else {
4137 while (strncmp(info->matches[info->count],
4138 info->matches[info->count-1],
4139 info->samelen) != 0) {
4140 info->samelen--;
4143 info->count++;
4147 static char **remote_completion(const char *text, int len)
4149 TALLOC_CTX *ctx = talloc_stackframe();
4150 char *dirmask = NULL;
4151 char *targetpath = NULL;
4152 struct cli_state *targetcli = NULL;
4153 int i;
4154 completion_remote_t info = { NULL, NULL, 1, 0, NULL, 0 };
4156 /* can't have non-static intialisation on Sun CC, so do it
4157 at run time here */
4158 info.samelen = len;
4159 info.text = text;
4160 info.len = len;
4162 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
4163 if (!info.matches) {
4164 TALLOC_FREE(ctx);
4165 return NULL;
4169 * We're leaving matches[0] free to fill it later with the text to
4170 * display: Either the one single match or the longest common subset
4171 * of the matches.
4173 info.matches[0] = NULL;
4174 info.count = 1;
4176 for (i = len-1; i >= 0; i--) {
4177 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
4178 break;
4182 info.text = text+i+1;
4183 info.samelen = info.len = len-i-1;
4185 if (i > 0) {
4186 info.dirmask = SMB_MALLOC_ARRAY(char, i+2);
4187 if (!info.dirmask) {
4188 goto cleanup;
4190 strncpy(info.dirmask, text, i+1);
4191 info.dirmask[i+1] = 0;
4192 dirmask = talloc_asprintf(ctx,
4193 "%s%*s*",
4194 client_get_cur_dir(),
4195 i-1,
4196 text);
4197 } else {
4198 info.dirmask = SMB_STRDUP("");
4199 if (!info.dirmask) {
4200 goto cleanup;
4202 dirmask = talloc_asprintf(ctx,
4203 "%s*",
4204 client_get_cur_dir());
4206 if (!dirmask) {
4207 goto cleanup;
4210 if (!cli_resolve_path(ctx, "", cli, dirmask, &targetcli, &targetpath)) {
4211 goto cleanup;
4213 if (cli_list(targetcli, targetpath, aDIR | aSYSTEM | aHIDDEN,
4214 completion_remote_filter, (void *)&info) < 0) {
4215 goto cleanup;
4218 if (info.count == 1) {
4220 * No matches at all, NULL indicates there is nothing
4222 SAFE_FREE(info.matches[0]);
4223 SAFE_FREE(info.matches);
4224 TALLOC_FREE(ctx);
4225 return NULL;
4228 if (info.count == 2) {
4230 * Exactly one match in matches[1], indicate this is the one
4231 * in matches[0].
4233 info.matches[0] = info.matches[1];
4234 info.matches[1] = NULL;
4235 info.count -= 1;
4236 TALLOC_FREE(ctx);
4237 return info.matches;
4241 * We got more than one possible match, set the result to the maximum
4242 * common subset
4245 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
4246 info.matches[info.count] = NULL;
4247 return info.matches;
4249 cleanup:
4250 for (i = 0; i < info.count; i++) {
4251 SAFE_FREE(info.matches[i]);
4253 SAFE_FREE(info.matches);
4254 SAFE_FREE(info.dirmask);
4255 TALLOC_FREE(ctx);
4256 return NULL;
4259 static char **completion_fn(const char *text, int start, int end)
4261 smb_readline_ca_char(' ');
4263 if (start) {
4264 const char *buf, *sp;
4265 int i;
4266 char compl_type;
4268 buf = smb_readline_get_line_buffer();
4269 if (buf == NULL)
4270 return NULL;
4272 sp = strchr(buf, ' ');
4273 if (sp == NULL)
4274 return NULL;
4276 for (i = 0; commands[i].name; i++) {
4277 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
4278 (commands[i].name[sp - buf] == 0)) {
4279 break;
4282 if (commands[i].name == NULL)
4283 return NULL;
4285 while (*sp == ' ')
4286 sp++;
4288 if (sp == (buf + start))
4289 compl_type = commands[i].compl_args[0];
4290 else
4291 compl_type = commands[i].compl_args[1];
4293 if (compl_type == COMPL_REMOTE)
4294 return remote_completion(text, end - start);
4295 else /* fall back to local filename completion */
4296 return NULL;
4297 } else {
4298 char **matches;
4299 int i, len, samelen = 0, count=1;
4301 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
4302 if (!matches) {
4303 return NULL;
4305 matches[0] = NULL;
4307 len = strlen(text);
4308 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
4309 if (strncmp(text, commands[i].name, len) == 0) {
4310 matches[count] = SMB_STRDUP(commands[i].name);
4311 if (!matches[count])
4312 goto cleanup;
4313 if (count == 1)
4314 samelen = strlen(matches[count]);
4315 else
4316 while (strncmp(matches[count], matches[count-1], samelen) != 0)
4317 samelen--;
4318 count++;
4322 switch (count) {
4323 case 0: /* should never happen */
4324 case 1:
4325 goto cleanup;
4326 case 2:
4327 matches[0] = SMB_STRDUP(matches[1]);
4328 break;
4329 default:
4330 matches[0] = (char *)SMB_MALLOC(samelen+1);
4331 if (!matches[0])
4332 goto cleanup;
4333 strncpy(matches[0], matches[1], samelen);
4334 matches[0][samelen] = 0;
4336 matches[count] = NULL;
4337 return matches;
4339 cleanup:
4340 for (i = 0; i < count; i++)
4341 free(matches[i]);
4343 free(matches);
4344 return NULL;
4348 static bool finished;
4350 /****************************************************************************
4351 Make sure we swallow keepalives during idle time.
4352 ****************************************************************************/
4354 static void readline_callback(void)
4356 fd_set fds;
4357 struct timeval timeout;
4358 static time_t last_t;
4359 time_t t;
4361 t = time(NULL);
4363 if (t - last_t < 5)
4364 return;
4366 last_t = t;
4368 again:
4370 if (cli->fd == -1)
4371 return;
4373 FD_ZERO(&fds);
4374 FD_SET(cli->fd,&fds);
4376 timeout.tv_sec = 0;
4377 timeout.tv_usec = 0;
4378 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
4380 /* We deliberately use receive_smb_raw instead of
4381 client_receive_smb as we want to receive
4382 session keepalives and then drop them here.
4384 if (FD_ISSET(cli->fd,&fds)) {
4385 NTSTATUS status;
4386 size_t len;
4388 set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
4390 status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize, 0, 0, &len);
4392 if (!NT_STATUS_IS_OK(status)) {
4393 DEBUG(0, ("Read from server failed, maybe it closed "
4394 "the connection\n"));
4396 finished = true;
4397 smb_readline_done();
4398 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
4399 set_smb_read_error(&cli->smb_rw_error,
4400 SMB_READ_EOF);
4401 return;
4404 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
4405 set_smb_read_error(&cli->smb_rw_error,
4406 SMB_READ_TIMEOUT);
4407 return;
4410 set_smb_read_error(&cli->smb_rw_error, SMB_READ_ERROR);
4411 return;
4413 if(CVAL(cli->inbuf,0) != SMBkeepalive) {
4414 DEBUG(0, ("Read from server "
4415 "returned unexpected packet!\n"));
4416 return;
4419 goto again;
4422 /* Ping the server to keep the connection alive using SMBecho. */
4424 NTSTATUS status;
4425 unsigned char garbage[16];
4426 memset(garbage, 0xf0, sizeof(garbage));
4427 status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
4429 if (!NT_STATUS_IS_OK(status)) {
4430 DEBUG(0, ("SMBecho failed. Maybe server has closed "
4431 "the connection\n"));
4432 finished = true;
4433 smb_readline_done();
4438 /****************************************************************************
4439 Process commands on stdin.
4440 ****************************************************************************/
4442 static int process_stdin(void)
4444 int rc = 0;
4446 while (!finished) {
4447 TALLOC_CTX *frame = talloc_stackframe();
4448 char *tok = NULL;
4449 char *the_prompt = NULL;
4450 char *line = NULL;
4451 int i;
4453 /* display a prompt */
4454 if (asprintf(&the_prompt, "smb: %s> ", client_get_cur_dir()) < 0) {
4455 TALLOC_FREE(frame);
4456 break;
4458 line = smb_readline(the_prompt, readline_callback, completion_fn);
4459 SAFE_FREE(the_prompt);
4460 if (!line) {
4461 TALLOC_FREE(frame);
4462 break;
4465 /* special case - first char is ! */
4466 if (*line == '!') {
4467 system(line + 1);
4468 SAFE_FREE(line);
4469 TALLOC_FREE(frame);
4470 continue;
4473 /* and get the first part of the command */
4474 cmd_ptr = line;
4475 if (!next_token_talloc(frame, &cmd_ptr,&tok,NULL)) {
4476 TALLOC_FREE(frame);
4477 SAFE_FREE(line);
4478 continue;
4481 if ((i = process_tok(tok)) >= 0) {
4482 rc = commands[i].fn();
4483 } else if (i == -2) {
4484 d_printf("%s: command abbreviation ambiguous\n",tok);
4485 } else {
4486 d_printf("%s: command not found\n",tok);
4488 SAFE_FREE(line);
4489 TALLOC_FREE(frame);
4491 return rc;
4494 /****************************************************************************
4495 Process commands from the client.
4496 ****************************************************************************/
4498 static int process(const char *base_directory)
4500 int rc = 0;
4502 cli = cli_cm_open(talloc_tos(), NULL,
4503 desthost, service, true, smb_encrypt);
4504 if (!cli) {
4505 return 1;
4508 if (base_directory && *base_directory) {
4509 rc = do_cd(base_directory);
4510 if (rc) {
4511 cli_cm_shutdown();
4512 return rc;
4516 if (cmdstr) {
4517 rc = process_command_string(cmdstr);
4518 } else {
4519 process_stdin();
4522 cli_cm_shutdown();
4523 return rc;
4526 /****************************************************************************
4527 Handle a -L query.
4528 ****************************************************************************/
4530 static int do_host_query(const char *query_host)
4532 struct sockaddr_storage ss;
4534 cli = cli_cm_open(talloc_tos(), NULL,
4535 query_host, "IPC$", true, smb_encrypt);
4536 if (!cli)
4537 return 1;
4539 browse_host(true);
4541 if (interpret_string_addr(&ss, query_host, 0) && (ss.ss_family != AF_INET)) {
4542 d_printf("%s is an IPv6 address -- no workgroup available\n",
4543 query_host);
4544 return 1;
4547 if (port != 139) {
4549 /* Workgroups simply don't make sense over anything
4550 else but port 139... */
4552 cli_cm_shutdown();
4553 cli_cm_set_port( 139 );
4554 cli = cli_cm_open(talloc_tos(), NULL,
4555 query_host, "IPC$", true, smb_encrypt);
4558 if (cli == NULL) {
4559 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
4560 return 1;
4563 list_servers(lp_workgroup());
4565 cli_cm_shutdown();
4567 return(0);
4570 /****************************************************************************
4571 Handle a tar operation.
4572 ****************************************************************************/
4574 static int do_tar_op(const char *base_directory)
4576 int ret;
4578 /* do we already have a connection? */
4579 if (!cli) {
4580 cli = cli_cm_open(talloc_tos(), NULL,
4581 desthost, service, true, smb_encrypt);
4582 if (!cli)
4583 return 1;
4586 recurse=true;
4588 if (base_directory && *base_directory) {
4589 ret = do_cd(base_directory);
4590 if (ret) {
4591 cli_cm_shutdown();
4592 return ret;
4596 ret=process_tar();
4598 cli_cm_shutdown();
4600 return(ret);
4603 /****************************************************************************
4604 Handle a message operation.
4605 ****************************************************************************/
4607 static int do_message_op(void)
4609 struct sockaddr_storage ss;
4610 struct nmb_name called, calling;
4611 fstring server_name;
4612 char name_type_hex[10];
4613 int msg_port;
4614 NTSTATUS status;
4616 make_nmb_name(&calling, calling_name, 0x0);
4617 make_nmb_name(&called , desthost, name_type);
4619 fstrcpy(server_name, desthost);
4620 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
4621 fstrcat(server_name, name_type_hex);
4623 zero_addr(&ss);
4624 if (have_ip)
4625 ss = dest_ss;
4627 /* we can only do messages over port 139 (to windows clients at least) */
4629 msg_port = port ? port : 139;
4631 if (!(cli=cli_initialise()) || (cli_set_port(cli, msg_port) != msg_port)) {
4632 d_printf("Connection to %s failed\n", desthost);
4633 return 1;
4636 status = cli_connect(cli, server_name, &ss);
4637 if (!NT_STATUS_IS_OK(status)) {
4638 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
4639 return 1;
4642 if (!cli_session_request(cli, &calling, &called)) {
4643 d_printf("session request failed\n");
4644 cli_cm_shutdown();
4645 return 1;
4648 send_message();
4649 cli_cm_shutdown();
4651 return 0;
4654 /****************************************************************************
4655 main program
4656 ****************************************************************************/
4658 int main(int argc,char *argv[])
4660 char *base_directory = NULL;
4661 int opt;
4662 char *query_host = NULL;
4663 bool message = false;
4664 char *term_code = NULL;
4665 static const char *new_name_resolve_order = NULL;
4666 poptContext pc;
4667 char *p;
4668 int rc = 0;
4669 fstring new_workgroup;
4670 bool tar_opt = false;
4671 bool service_opt = false;
4672 struct poptOption long_options[] = {
4673 POPT_AUTOHELP
4675 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
4676 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
4677 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
4678 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
4679 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
4680 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
4681 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
4682 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
4683 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
4684 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
4685 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
4686 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
4687 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
4688 { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
4689 POPT_COMMON_SAMBA
4690 POPT_COMMON_CONNECTION
4691 POPT_COMMON_CREDENTIALS
4692 POPT_TABLEEND
4694 TALLOC_CTX *frame = talloc_stackframe();
4696 if (!client_set_cur_dir("\\")) {
4697 exit(ENOMEM);
4700 #ifdef KANJI
4701 term_code = talloc_strdup(frame,KANJI);
4702 #else /* KANJI */
4703 term_code = talloc_strdup(frame,"");
4704 #endif /* KANJI */
4705 if (!term_code) {
4706 exit(ENOMEM);
4709 /* initialize the workgroup name so we can determine whether or
4710 not it was set by a command line option */
4712 set_global_myworkgroup( "" );
4713 set_global_myname( "" );
4715 /* set default debug level to 1 regardless of what smb.conf sets */
4716 setup_logging( "smbclient", true );
4717 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
4718 if ((dbf = x_fdup(x_stderr))) {
4719 x_setbuf( dbf, NULL );
4722 load_case_tables();
4724 /* skip argv(0) */
4725 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
4726 poptSetOtherOptionHelp(pc, "service <password>");
4728 lp_set_in_client(true); /* Make sure that we tell lp_load we are */
4730 while ((opt = poptGetNextOpt(pc)) != -1) {
4732 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
4733 /* I see no other way to keep things sane --SSS */
4734 if (tar_opt == true) {
4735 while (poptPeekArg(pc)) {
4736 poptGetArg(pc);
4738 tar_opt = false;
4741 /* if the service has not yet been specified lets see if it is available in the popt stack */
4742 if (!service_opt && poptPeekArg(pc)) {
4743 service = talloc_strdup(frame, poptGetArg(pc));
4744 if (!service) {
4745 exit(ENOMEM);
4747 service_opt = true;
4750 /* if the service has already been retrieved then check if we have also a password */
4751 if (service_opt && (!get_cmdline_auth_info_got_pass()) && poptPeekArg(pc)) {
4752 set_cmdline_auth_info_password(poptGetArg(pc));
4755 switch (opt) {
4756 case 'M':
4757 /* Messages are sent to NetBIOS name type 0x3
4758 * (Messenger Service). Make sure we default
4759 * to port 139 instead of port 445. srl,crh
4761 name_type = 0x03;
4762 cli_cm_set_dest_name_type( name_type );
4763 desthost = talloc_strdup(frame,poptGetOptArg(pc));
4764 if (!desthost) {
4765 exit(ENOMEM);
4767 if( !port )
4768 cli_cm_set_port( 139 );
4769 message = true;
4770 break;
4771 case 'I':
4773 if (!interpret_string_addr(&dest_ss, poptGetOptArg(pc), 0)) {
4774 exit(1);
4776 have_ip = true;
4778 cli_cm_set_dest_ss(&dest_ss);
4780 break;
4781 case 'E':
4782 if (dbf) {
4783 x_fclose(dbf);
4785 dbf = x_stderr;
4786 display_set_stderr();
4787 break;
4789 case 'L':
4790 query_host = talloc_strdup(frame, poptGetOptArg(pc));
4791 if (!query_host) {
4792 exit(ENOMEM);
4794 break;
4795 case 't':
4796 term_code = talloc_strdup(frame,poptGetOptArg(pc));
4797 if (!term_code) {
4798 exit(ENOMEM);
4800 break;
4801 case 'm':
4802 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
4803 break;
4804 case 'T':
4805 /* We must use old option processing for this. Find the
4806 * position of the -T option in the raw argv[]. */
4808 int i;
4809 for (i = 1; i < argc; i++) {
4810 if (strncmp("-T", argv[i],2)==0)
4811 break;
4813 i++;
4814 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
4815 poptPrintUsage(pc, stderr, 0);
4816 exit(1);
4819 /* this must be the last option, mark we have parsed it so that we know we have */
4820 tar_opt = true;
4821 break;
4822 case 'D':
4823 base_directory = talloc_strdup(frame, poptGetOptArg(pc));
4824 if (!base_directory) {
4825 exit(ENOMEM);
4827 break;
4828 case 'g':
4829 grepable=true;
4830 break;
4831 case 'e':
4832 smb_encrypt=true;
4833 break;
4834 case 'B':
4835 return(do_smb_browse());
4840 /* We may still have some leftovers after the last popt option has been called */
4841 if (tar_opt == true) {
4842 while (poptPeekArg(pc)) {
4843 poptGetArg(pc);
4845 tar_opt = false;
4848 /* if the service has not yet been specified lets see if it is available in the popt stack */
4849 if (!service_opt && poptPeekArg(pc)) {
4850 service = talloc_strdup(frame,poptGetArg(pc));
4851 if (!service) {
4852 exit(ENOMEM);
4854 service_opt = true;
4857 /* if the service has already been retrieved then check if we have also a password */
4858 if (service_opt && !get_cmdline_auth_info_got_pass() && poptPeekArg(pc)) {
4859 set_cmdline_auth_info_password(poptGetArg(pc));
4862 /* check for the -P option */
4864 if ( port != 0 )
4865 cli_cm_set_port( port );
4868 * Don't load debug level from smb.conf. It should be
4869 * set by cmdline arg or remain default (0)
4871 AllowDebugChange = false;
4873 /* save the workgroup...
4875 FIXME!! do we need to do this for other options as well
4876 (or maybe a generic way to keep lp_load() from overwriting
4877 everything)? */
4879 fstrcpy( new_workgroup, lp_workgroup() );
4880 calling_name = talloc_strdup(frame, global_myname() );
4881 if (!calling_name) {
4882 exit(ENOMEM);
4885 if ( override_logfile )
4886 setup_logging( lp_logfile(), false );
4888 if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
4889 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
4890 argv[0], get_dyn_CONFIGFILE());
4893 if (get_cmdline_auth_info_use_machine_account() &&
4894 !set_cmdline_auth_info_machine_account_creds()) {
4895 exit(-1);
4898 load_interfaces();
4900 if (service_opt && service) {
4901 size_t len;
4903 /* Convert any '/' characters in the service name to '\' characters */
4904 string_replace(service, '/','\\');
4905 if (count_chars(service,'\\') < 3) {
4906 d_printf("\n%s: Not enough '\\' characters in service\n",service);
4907 poptPrintUsage(pc, stderr, 0);
4908 exit(1);
4910 /* Remove trailing slashes */
4911 len = strlen(service);
4912 while(len > 0 && service[len - 1] == '\\') {
4913 --len;
4914 service[len] = '\0';
4918 if ( strlen(new_workgroup) != 0 ) {
4919 set_global_myworkgroup( new_workgroup );
4922 if ( strlen(calling_name) != 0 ) {
4923 set_global_myname( calling_name );
4924 } else {
4925 TALLOC_FREE(calling_name);
4926 calling_name = talloc_strdup(frame, global_myname() );
4929 smb_encrypt = get_cmdline_auth_info_smb_encrypt();
4930 if (!init_names()) {
4931 fprintf(stderr, "init_names() failed\n");
4932 exit(1);
4935 if(new_name_resolve_order)
4936 lp_set_name_resolve_order(new_name_resolve_order);
4938 if (!tar_type && !query_host && !service && !message) {
4939 poptPrintUsage(pc, stderr, 0);
4940 exit(1);
4943 poptFreeContext(pc);
4945 /* Store the username and password for dfs support */
4947 cli_cm_set_credentials();
4949 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
4951 if (tar_type) {
4952 if (cmdstr)
4953 process_command_string(cmdstr);
4954 return do_tar_op(base_directory);
4957 if (query_host && *query_host) {
4958 char *qhost = query_host;
4959 char *slash;
4961 while (*qhost == '\\' || *qhost == '/')
4962 qhost++;
4964 if ((slash = strchr_m(qhost, '/'))
4965 || (slash = strchr_m(qhost, '\\'))) {
4966 *slash = 0;
4969 if ((p=strchr_m(qhost, '#'))) {
4970 *p = 0;
4971 p++;
4972 sscanf(p, "%x", &name_type);
4973 cli_cm_set_dest_name_type( name_type );
4976 return do_host_query(qhost);
4979 if (message) {
4980 return do_message_op();
4983 if (process(base_directory)) {
4984 return 1;
4987 TALLOC_FREE(frame);
4988 return rc;