2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) Gerald (Jerry) Carter 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "client/client_proto.h"
25 #include "include/rpc_client.h"
30 extern bool AllowDebugChange;
31 extern bool override_logfile;
33 extern bool in_client;
35 pstring cur_dir = "\\";
36 static pstring cd_path = "";
37 static pstring service;
38 static pstring desthost;
39 static pstring username;
40 static pstring calling_name;
41 static bool grepable=False;
42 static char *cmdstr = NULL;
44 static int io_bufsize = 64512;
46 static int name_type = 0x20;
47 extern int max_protocol;
49 static int process_tok(pstring tok);
50 static int cmd_help(void);
52 static TALLOC_CTX *ctx;
53 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
56 /* 30 second timeout on most commands */
57 #define CLIENT_TIMEOUT (30*1000)
58 #define SHORT_TIMEOUT (5*1000)
60 /* value for unused fid field in trans2 secondary request */
61 #define FID_UNUSED (0xFFFF)
63 time_t newer_than = 0;
64 static int archive_level = 0;
66 static bool translation = False;
69 /* clitar bits insert */
72 extern bool tar_reset;
76 static bool prompt = True;
78 static bool recurse = False;
79 static bool showacls = False;
80 bool lowercase = False;
82 static struct in_addr dest_ip;
84 #define SEPARATORS " \t\n\r"
86 static bool abort_mget = True;
88 static pstring fileselection = "";
90 extern file_info def_finfo;
93 SMB_BIG_UINT get_total_size = 0;
94 unsigned int get_total_time_ms = 0;
95 static SMB_BIG_UINT put_total_size = 0;
96 static unsigned int put_total_time_ms = 0;
99 static double dir_total;
101 /* root cli_state connection */
103 struct cli_state *cli;
105 static char CLI_DIRSEP_CHAR = '\\';
106 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
108 /****************************************************************************
109 Write to a local file with CR/LF->LF translation if appropriate. Return the
110 number taken from the buffer. This may not equal the number written.
111 ****************************************************************************/
113 static int writefile(int f, char *b, int n)
123 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
126 if (write(f, b, 1) != 1) {
136 /****************************************************************************
137 Read from a file with LF->CR/LF translation if appropriate. Return the
138 number read. read approx n bytes.
139 ****************************************************************************/
141 static int readfile(char *b, int n, XFILE *f)
147 return x_fread(b,1,n,f);
150 while (i < (n - 1) && (i < BUFFER_SIZE)) {
151 if ((c = x_getc(f)) == EOF) {
155 if (c == '\n') { /* change all LFs to CR/LF */
165 /****************************************************************************
167 ****************************************************************************/
169 static void send_message(void)
174 if (!cli_message_start(cli, desthost, username, &grp_id)) {
175 d_printf("message start: %s\n", cli_errstr(cli));
180 d_printf("Connected. Type your message, ending it with a Control-D\n");
182 while (!feof(stdin) && total_len < 1600) {
183 int maxlen = MIN(1600 - total_len,127);
190 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
196 if ((total_len > 0) && (strlen(msg) == 0)) {
200 if (!cli_message_text(cli, msg, l, grp_id)) {
201 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
208 if (total_len >= 1600)
209 d_printf("the message was truncated to 1600 bytes\n");
211 d_printf("sent %d bytes\n",total_len);
213 if (!cli_message_end(cli, grp_id)) {
214 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
219 /****************************************************************************
220 Check the space on a device.
221 ****************************************************************************/
223 static int do_dskattr(void)
225 int total, bsize, avail;
226 struct cli_state *targetcli;
229 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
230 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
234 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
235 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
239 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
240 total, bsize, avail);
245 /****************************************************************************
247 ****************************************************************************/
249 static int cmd_pwd(void)
251 d_printf("Current directory is %s",service);
252 d_printf("%s\n",cur_dir);
256 /****************************************************************************
257 Change directory - inner section.
258 ****************************************************************************/
260 static int do_cd(char *newdir)
266 struct cli_state *targetcli;
267 SMB_STRUCT_STAT sbuf;
273 /* Save the current directory in case the new directory is invalid */
275 pstrcpy(saved_dir, cur_dir);
277 if (*p == CLI_DIRSEP_CHAR) {
281 if ((cur_dir[0] != '\0') && (*(cur_dir+strlen(cur_dir)-1) != CLI_DIRSEP_CHAR)) {
282 pstrcat(cur_dir, CLI_DIRSEP_STR);
287 pstrcpy( dname, cur_dir );
289 if ( !cli_resolve_path( "", cli, dname, &targetcli, targetpath ) ) {
290 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
291 pstrcpy(cur_dir,saved_dir);
295 if (strequal(targetpath,CLI_DIRSEP_STR )) {
299 /* Use a trans2_qpathinfo to test directories for modern servers.
300 Except Win9x doesn't support the qpathinfo_basic() call..... */
302 if ( targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95 ) {
303 if ( !cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
304 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
305 pstrcpy(cur_dir,saved_dir);
309 if ( !(attributes&FILE_ATTRIBUTE_DIRECTORY) ) {
310 d_printf("cd %s: not a directory\n", dname);
311 pstrcpy(cur_dir,saved_dir);
315 pstrcat( targetpath, CLI_DIRSEP_STR );
316 clean_name( targetpath );
318 if ( !cli_chkpath(targetcli, targetpath) ) {
319 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
320 pstrcpy(cur_dir,saved_dir);
329 pstrcpy(cd_path,cur_dir);
333 /****************************************************************************
335 ****************************************************************************/
337 static int cmd_cd(void)
342 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
345 d_printf("Current directory is %s\n",cur_dir);
350 /****************************************************************************
352 ****************************************************************************/
354 static int cmd_cd_oneup(void)
363 /*******************************************************************
364 Decide if a file should be operated on.
365 ********************************************************************/
367 static bool do_this_one(file_info *finfo)
369 if (finfo->mode & aDIR)
372 if (*fileselection &&
373 !mask_match(finfo->name,fileselection,False)) {
374 DEBUG(3,("mask_match %s failed\n", finfo->name));
378 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
379 DEBUG(3,("newer_than %s failed\n", finfo->name));
383 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
384 DEBUG(3,("archive %s failed\n", finfo->name));
391 /****************************************************************************
392 Display info about a file.
393 ****************************************************************************/
395 static void display_finfo(file_info *finfo)
397 if (do_this_one(finfo)) {
398 time_t t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
400 d_printf(" %-30s%7.7s %8.0f %s",
402 attrib_string(finfo->mode),
405 dir_total += finfo->size;
410 /* skip if this is . or .. */
411 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
413 /* create absolute filename for cli_nt_create() FIXME */
414 pstrcpy( afname, cwd);
415 pstrcat( afname, CLI_DIRSEP_STR);
416 pstrcat( afname, finfo->name);
417 /* print file meta date header */
418 d_printf( "FILENAME:%s\n", afname);
419 d_printf( "MODE:%s\n", attrib_string(finfo->mode));
420 d_printf( "SIZE:%.0f\n", (double)finfo->size);
421 d_printf( "MTIME:%s", time_to_asc(t));
422 fnum = cli_nt_create(finfo->cli, afname, CREATE_ACCESS_READ);
424 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
426 cli_errstr( finfo->cli)));
429 sd = cli_query_secdesc(finfo->cli, fnum, ctx);
431 DEBUG( 0, ("display_finfo() failed to "
432 "get security descriptor: %s",
433 cli_errstr( finfo->cli)));
435 display_sec_desc(sd);
442 /****************************************************************************
443 Accumulate size of a file.
444 ****************************************************************************/
446 static void do_du(file_info *finfo)
448 if (do_this_one(finfo)) {
449 dir_total += finfo->size;
453 static bool do_list_recurse;
454 static bool do_list_dirs;
455 static char *do_list_queue = 0;
456 static long do_list_queue_size = 0;
457 static long do_list_queue_start = 0;
458 static long do_list_queue_end = 0;
459 static void (*do_list_fn)(file_info *);
461 /****************************************************************************
462 Functions for do_list_queue.
463 ****************************************************************************/
466 * The do_list_queue is a NUL-separated list of strings stored in a
467 * char*. Since this is a FIFO, we keep track of the beginning and
468 * ending locations of the data in the queue. When we overflow, we
469 * double the size of the char*. When the start of the data passes
470 * the midpoint, we move everything back. This is logically more
471 * complex than a linked list, but easier from a memory management
472 * angle. In any memory error condition, do_list_queue is reset.
473 * Functions check to ensure that do_list_queue is non-NULL before
477 static void reset_do_list_queue(void)
479 SAFE_FREE(do_list_queue);
480 do_list_queue_size = 0;
481 do_list_queue_start = 0;
482 do_list_queue_end = 0;
485 static void init_do_list_queue(void)
487 reset_do_list_queue();
488 do_list_queue_size = 1024;
489 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
490 if (do_list_queue == 0) {
491 d_printf("malloc fail for size %d\n",
492 (int)do_list_queue_size);
493 reset_do_list_queue();
495 memset(do_list_queue, 0, do_list_queue_size);
499 static void adjust_do_list_queue(void)
502 * If the starting point of the queue is more than half way through,
503 * move everything toward the beginning.
506 if (do_list_queue == NULL) {
507 DEBUG(4,("do_list_queue is empty\n"));
508 do_list_queue_start = do_list_queue_end = 0;
512 if (do_list_queue_start == do_list_queue_end) {
513 DEBUG(4,("do_list_queue is empty\n"));
514 do_list_queue_start = do_list_queue_end = 0;
515 *do_list_queue = '\0';
516 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
517 DEBUG(4,("sliding do_list_queue backward\n"));
518 memmove(do_list_queue,
519 do_list_queue + do_list_queue_start,
520 do_list_queue_end - do_list_queue_start);
521 do_list_queue_end -= do_list_queue_start;
522 do_list_queue_start = 0;
526 static void add_to_do_list_queue(const char* entry)
528 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
529 while (new_end > do_list_queue_size) {
530 do_list_queue_size *= 2;
531 DEBUG(4,("enlarging do_list_queue to %d\n",
532 (int)do_list_queue_size));
533 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
534 if (! do_list_queue) {
535 d_printf("failure enlarging do_list_queue to %d bytes\n",
536 (int)do_list_queue_size);
537 reset_do_list_queue();
539 memset(do_list_queue + do_list_queue_size / 2,
540 0, do_list_queue_size / 2);
544 safe_strcpy_base(do_list_queue + do_list_queue_end,
545 entry, do_list_queue, do_list_queue_size);
546 do_list_queue_end = new_end;
547 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
548 entry, (int)do_list_queue_start, (int)do_list_queue_end));
552 static char *do_list_queue_head(void)
554 return do_list_queue + do_list_queue_start;
557 static void remove_do_list_queue_head(void)
559 if (do_list_queue_end > do_list_queue_start) {
560 do_list_queue_start += strlen(do_list_queue_head()) + 1;
561 adjust_do_list_queue();
562 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
563 (int)do_list_queue_start, (int)do_list_queue_end));
567 static int do_list_queue_empty(void)
569 return (! (do_list_queue && *do_list_queue));
572 /****************************************************************************
573 A helper for do_list.
574 ****************************************************************************/
576 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
580 /* save the directory */
581 pstrcpy( f->dir, mask );
582 if ( (dir_end = strrchr( f->dir, CLI_DIRSEP_CHAR )) != NULL ) {
586 if (f->mode & aDIR) {
587 if (do_list_dirs && do_this_one(f)) {
590 if (do_list_recurse &&
591 !strequal(f->name,".") &&
592 !strequal(f->name,"..")) {
597 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
601 pstrcpy(mask2, mntpoint);
602 pstrcat(mask2, mask);
603 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
607 pstrcat(mask2, f->name);
608 pstrcat(mask2,CLI_DIRSEP_STR);
610 add_to_do_list_queue(mask2);
615 if (do_this_one(f)) {
620 /****************************************************************************
621 A wrapper around cli_list that adds recursion.
622 ****************************************************************************/
624 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),bool rec, bool dirs)
626 static int in_do_list = 0;
627 struct cli_state *targetcli;
630 if (in_do_list && rec) {
631 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
637 do_list_recurse = rec;
642 init_do_list_queue();
643 add_to_do_list_queue(mask);
645 while (! do_list_queue_empty()) {
647 * Need to copy head so that it doesn't become
648 * invalid inside the call to cli_list. This
649 * would happen if the list were expanded
651 * Fix from E. Jay Berkenbilt (ejb@ql.org)
654 pstrcpy(head, do_list_queue_head());
658 if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
659 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
660 remove_do_list_queue_head();
664 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
665 remove_do_list_queue_head();
666 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
667 char* next_file = do_list_queue_head();
669 if ((strlen(next_file) >= 2) &&
670 (next_file[strlen(next_file) - 1] == '*') &&
671 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
672 save_ch = next_file +
673 strlen(next_file) - 2;
675 if (showacls) /* cwd is only used if showacls is on */
676 pstrcpy( cwd, next_file);
678 if (!showacls) /* don't disturbe the showacls output */
679 d_printf("\n%s\n",next_file);
681 *save_ch = CLI_DIRSEP_CHAR;
688 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
689 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1)
690 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
693 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
698 reset_do_list_queue();
701 /****************************************************************************
702 Get a directory listing.
703 ****************************************************************************/
705 static int cmd_dir(void)
707 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
714 if (strcmp(cur_dir, CLI_DIRSEP_STR) != 0) {
715 pstrcpy(mask,cur_dir);
716 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR))
717 pstrcat(mask,CLI_DIRSEP_STR);
719 pstrcpy(mask, CLI_DIRSEP_STR);
722 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
724 if (*p == CLI_DIRSEP_CHAR)
733 /* cwd is only used if showacls is on */
734 pstrcpy(cwd, cur_dir);
737 do_list(mask, attribute, display_finfo, recurse, True);
741 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
746 /****************************************************************************
747 Get a directory listing.
748 ****************************************************************************/
750 static int cmd_du(void)
752 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
759 pstrcpy(mask,cur_dir);
760 if ((mask[0] != '\0') && (mask[strlen(mask)-1]!=CLI_DIRSEP_CHAR))
761 pstrcat(mask,CLI_DIRSEP_STR);
763 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
765 if (*p == CLI_DIRSEP_CHAR)
773 do_list(mask, attribute, do_du, recurse, True);
777 d_printf("Total number of bytes: %.0f\n", dir_total);
782 static int cmd_echo(void)
787 if (!next_token_nr(NULL, num, NULL, sizeof(num))
788 || !next_token_nr(NULL, data, NULL, sizeof(data))) {
789 d_printf("echo <num> <data>\n");
793 if (!cli_echo(cli, atoi(num), (uint8 *)data, strlen(data))) {
794 d_printf("echo failed: %s\n",
795 nt_errstr(cli_get_nt_error(cli)));
802 /****************************************************************************
803 Get a file from rname to lname
804 ****************************************************************************/
806 static int do_get(char *rname, char *lname, bool reget)
808 int handle = 0, fnum;
809 bool newhandle = False;
811 struct timeval tp_start;
812 int read_size = io_bufsize;
818 struct cli_state *targetcli;
826 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
827 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
831 GetTimeOfDay(&tp_start);
833 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
836 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
840 if(!strcmp(lname,"-")) {
841 handle = fileno(stdout);
844 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
846 start = sys_lseek(handle, 0, SEEK_END);
848 d_printf("Error seeking local file\n");
853 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
858 d_printf("Error opening local file %s\n",lname);
863 if (!cli_qfileinfo(targetcli, fnum,
864 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
865 !cli_getattrE(targetcli, fnum,
866 &attr, &size, NULL, NULL, NULL)) {
867 d_printf("getattrib: %s\n",cli_errstr(targetcli));
871 DEBUG(1,("getting file %s of size %.0f as %s ",
872 rname, (double)size, lname));
874 if(!(data = (char *)SMB_MALLOC(read_size))) {
875 d_printf("malloc fail for size %d\n", read_size);
876 cli_close(targetcli, fnum);
881 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
886 if (writefile(handle,data, n) != n) {
887 d_printf("Error writing local file\n");
895 if (nread + start < size) {
896 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
897 rname, (long)nread));
904 if (!cli_close(targetcli, fnum)) {
905 d_printf("Error %s closing remote file\n",cli_errstr(cli));
913 if (archive_level >= 2 && (attr & aARCH)) {
914 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
918 struct timeval tp_end;
921 GetTimeOfDay(&tp_end);
923 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
924 (tp_end.tv_usec - tp_start.tv_usec)/1000;
925 get_total_time_ms += this_time;
926 get_total_size += nread;
928 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
929 nread / (1.024*this_time + 1.0e-4),
930 get_total_size / (1.024*get_total_time_ms)));
936 /****************************************************************************
938 ****************************************************************************/
940 static int cmd_get(void)
946 pstrcpy(rname,cur_dir);
947 pstrcat(rname,CLI_DIRSEP_STR);
949 p = rname + strlen(rname);
951 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
952 d_printf("get <filename>\n");
958 next_token_nr(NULL,lname,NULL,sizeof(lname));
960 return do_get(rname, lname, False);
963 /****************************************************************************
964 Do an mget operation on one file.
965 ****************************************************************************/
967 static void do_mget(file_info *finfo)
971 pstring saved_curdir;
974 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
978 d_printf("mget aborted\n");
982 if (finfo->mode & aDIR)
983 slprintf(quest,sizeof(pstring)-1,
984 "Get directory %s? ",finfo->name);
986 slprintf(quest,sizeof(pstring)-1,
987 "Get file %s? ",finfo->name);
989 if (prompt && !yesno(quest))
992 if (!(finfo->mode & aDIR)) {
993 pstrcpy(rname,cur_dir);
994 pstrcat(rname,finfo->name);
995 do_get(rname, finfo->name, False);
999 /* handle directories */
1000 pstrcpy(saved_curdir,cur_dir);
1002 pstrcat(cur_dir,finfo->name);
1003 pstrcat(cur_dir,CLI_DIRSEP_STR);
1005 unix_format(finfo->name);
1007 strlower_m(finfo->name);
1009 if (!directory_exist(finfo->name,NULL) &&
1010 mkdir(finfo->name,0777) != 0) {
1011 d_printf("failed to create directory %s\n",finfo->name);
1012 pstrcpy(cur_dir,saved_curdir);
1016 if (chdir(finfo->name) != 0) {
1017 d_printf("failed to chdir to directory %s\n",finfo->name);
1018 pstrcpy(cur_dir,saved_curdir);
1022 pstrcpy(mget_mask,cur_dir);
1023 pstrcat(mget_mask,"*");
1025 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
1027 pstrcpy(cur_dir,saved_curdir);
1030 /****************************************************************************
1031 View the file using the pager.
1032 ****************************************************************************/
1034 static int cmd_more(void)
1036 pstring rname,lname,pager_cmd;
1041 pstrcpy(rname,cur_dir);
1042 pstrcat(rname,CLI_DIRSEP_STR);
1044 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
1045 fd = smb_mkstemp(lname);
1047 d_printf("failed to create temporary file for more\n");
1052 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
1053 d_printf("more <filename>\n");
1059 rc = do_get(rname, lname, False);
1061 pager=getenv("PAGER");
1063 slprintf(pager_cmd,sizeof(pager_cmd)-1,
1064 "%s %s",(pager? pager:PAGER), lname);
1071 /****************************************************************************
1073 ****************************************************************************/
1075 static int cmd_mget(void)
1077 uint16 attribute = aSYSTEM | aHIDDEN;
1089 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1090 pstrcpy(mget_mask,cur_dir);
1091 if ((mget_mask[0] != '\0') && (mget_mask[strlen(mget_mask)-1]!=CLI_DIRSEP_CHAR))
1092 pstrcat(mget_mask,CLI_DIRSEP_STR);
1094 if (*p == CLI_DIRSEP_CHAR)
1095 pstrcpy(mget_mask,p);
1097 pstrcat(mget_mask,p);
1098 do_list(mget_mask, attribute,do_mget,False,True);
1102 pstrcpy(mget_mask,cur_dir);
1103 if(mget_mask[strlen(mget_mask)-1]!=CLI_DIRSEP_CHAR)
1104 pstrcat(mget_mask,CLI_DIRSEP_STR);
1105 pstrcat(mget_mask,"*");
1106 do_list(mget_mask, attribute,do_mget,False,True);
1112 /****************************************************************************
1113 Make a directory of name "name".
1114 ****************************************************************************/
1116 static bool do_mkdir(char *name)
1118 struct cli_state *targetcli;
1121 if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) {
1122 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1126 if (!cli_mkdir(targetcli, targetname)) {
1127 d_printf("%s making remote directory %s\n",
1128 cli_errstr(targetcli),name);
1135 /****************************************************************************
1136 Show 8.3 name of a file.
1137 ****************************************************************************/
1139 static bool do_altname(char *name)
1142 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1143 d_printf("%s getting alt name for %s\n",
1144 cli_errstr(cli),name);
1147 d_printf("%s\n", altname);
1152 /****************************************************************************
1154 ****************************************************************************/
1156 static int cmd_quit(void)
1159 talloc_destroy( ctx);
1165 /****************************************************************************
1167 ****************************************************************************/
1169 static int cmd_mkdir(void)
1175 pstrcpy(mask,cur_dir);
1177 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1179 d_printf("mkdir <dirname>\n");
1187 struct cli_state *targetcli;
1191 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1195 pstrcpy(ddir,targetname);
1196 trim_char(ddir,'.','\0');
1197 p = strtok(ddir,"/\\");
1200 if (!cli_chkpath(targetcli, ddir2)) {
1203 pstrcat(ddir2,CLI_DIRSEP_STR);
1204 p = strtok(NULL,"/\\");
1213 /****************************************************************************
1215 ****************************************************************************/
1217 static int cmd_altname(void)
1223 pstrcpy(name,cur_dir);
1225 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1226 d_printf("altname <file>\n");
1236 /****************************************************************************
1238 ****************************************************************************/
1240 static int do_put(char *rname, char *lname, bool reput)
1244 SMB_OFF_T start = 0;
1247 int maxwrite = io_bufsize;
1249 struct timeval tp_start;
1250 struct cli_state *targetcli;
1253 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
1254 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1258 GetTimeOfDay(&tp_start);
1261 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1263 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1264 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1265 d_printf("getattrib: %s\n",cli_errstr(cli));
1270 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1274 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1278 /* allow files to be piped into smbclient
1281 Note that in this case this function will exit(0) rather
1283 if (!strcmp(lname, "-")) {
1285 /* size of file is not known */
1287 f = x_fopen(lname,O_RDONLY, 0);
1289 if (x_tseek(f, start, SEEK_SET) == -1) {
1290 d_printf("Error seeking local file\n");
1297 d_printf("Error opening local file %s\n",lname);
1301 DEBUG(1,("putting file %s as %s ",lname,
1304 buf = (char *)SMB_MALLOC(maxwrite);
1306 d_printf("ERROR: Not enough memory!\n");
1309 while (!x_feof(f)) {
1313 if ((n = readfile(buf,n,f)) < 1) {
1314 if((n == 0) && x_feof(f))
1315 break; /* Empty local file. */
1317 d_printf("Error reading local file: %s\n", strerror(errno));
1322 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1325 d_printf("Error writing file: %s\n", cli_errstr(cli));
1333 if (!cli_close(targetcli, fnum)) {
1334 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1348 struct timeval tp_end;
1351 GetTimeOfDay(&tp_end);
1353 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1354 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1355 put_total_time_ms += this_time;
1356 put_total_size += nread;
1358 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1359 nread / (1.024*this_time + 1.0e-4),
1360 put_total_size / (1.024*put_total_time_ms)));
1371 /****************************************************************************
1373 ****************************************************************************/
1375 static int cmd_put(void)
1382 pstrcpy(rname,cur_dir);
1383 pstrcat(rname,CLI_DIRSEP_STR);
1385 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1386 d_printf("put <filename>\n");
1391 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1394 pstrcat(rname,lname);
1400 /* allow '-' to represent stdin
1401 jdblair, 24.jun.98 */
1402 if (!file_exist(lname,&st) &&
1403 (strcmp(lname,"-"))) {
1404 d_printf("%s does not exist\n",lname);
1409 return do_put(rname, lname, False);
1412 /*************************************
1413 File list structure.
1414 *************************************/
1416 static struct file_list {
1417 struct file_list *prev, *next;
1422 /****************************************************************************
1423 Free a file_list structure.
1424 ****************************************************************************/
1426 static void free_file_list (struct file_list *list_head)
1428 struct file_list *list, *next;
1430 for (list = list_head; list; list = next) {
1432 DLIST_REMOVE(list_head, list);
1433 SAFE_FREE(list->file_path);
1438 /****************************************************************************
1439 Seek in a directory/file list until you get something that doesn't start with
1441 ****************************************************************************/
1443 static bool seek_list(struct file_list *list, char *name)
1446 trim_string(list->file_path,"./","\n");
1447 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1456 /****************************************************************************
1457 Set the file selection mask.
1458 ****************************************************************************/
1460 static int cmd_select(void)
1462 pstrcpy(fileselection,"");
1463 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1468 /****************************************************************************
1469 Recursive file matching function act as find
1470 match must be always set to True when calling this function
1471 ****************************************************************************/
1473 static int file_find(struct file_list **list, const char *directory,
1474 const char *expression, bool match)
1476 SMB_STRUCT_DIR *dir;
1477 struct file_list *entry;
1478 struct stat statbuf;
1484 dir = sys_opendir(directory);
1488 while ((dname = readdirname(dir))) {
1489 if (!strcmp("..", dname))
1491 if (!strcmp(".", dname))
1494 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1499 if (!match || !gen_fnmatch(expression, dname)) {
1501 ret = stat(path, &statbuf);
1503 if (S_ISDIR(statbuf.st_mode)) {
1505 ret = file_find(list, path, expression, False);
1508 d_printf("file_find: cannot stat file %s\n", path);
1517 entry = SMB_MALLOC_P(struct file_list);
1519 d_printf("Out of memory in file_find\n");
1523 entry->file_path = path;
1524 entry->isdir = isdir;
1525 DLIST_ADD(*list, entry);
1535 /****************************************************************************
1537 ****************************************************************************/
1539 static int cmd_mput(void)
1544 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1546 struct file_list *temp_list;
1547 char *quest, *lname, *rname;
1551 ret = file_find(&file_list, ".", p, True);
1553 free_file_list(file_list);
1561 for (temp_list = file_list; temp_list;
1562 temp_list = temp_list->next) {
1565 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1567 trim_string(lname, "./", "/");
1569 /* check if it's a directory */
1570 if (temp_list->isdir) {
1571 /* if (!recurse) continue; */
1574 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1575 if (prompt && !yesno(quest)) { /* No */
1576 /* Skip the directory */
1577 lname[strlen(lname)-1] = '/';
1578 if (!seek_list(temp_list, lname))
1582 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1584 if (!cli_chkpath(cli, rname) &&
1586 DEBUG (0, ("Unable to make dir, skipping..."));
1587 /* Skip the directory */
1588 lname[strlen(lname)-1] = '/';
1589 if (!seek_list(temp_list, lname))
1596 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1597 if (prompt && !yesno(quest)) /* No */
1602 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1607 do_put(rname, lname, False);
1609 free_file_list(file_list);
1618 /****************************************************************************
1620 ****************************************************************************/
1622 static int do_cancel(int job)
1624 if (cli_printjob_del(cli, job)) {
1625 d_printf("Job %d cancelled\n",job);
1628 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1633 /****************************************************************************
1635 ****************************************************************************/
1637 static int cmd_cancel(void)
1642 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1643 d_printf("cancel <jobid> ...\n");
1649 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1654 /****************************************************************************
1656 ****************************************************************************/
1658 static int cmd_print(void)
1664 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1665 d_printf("print <filename>\n");
1669 pstrcpy(rname,lname);
1670 p = strrchr_m(rname,'/');
1672 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1675 if (strequal(lname,"-")) {
1676 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1679 return do_put(rname, lname, False);
1682 /****************************************************************************
1683 Show a print queue entry.
1684 ****************************************************************************/
1686 static void queue_fn(struct print_job_info *p)
1688 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1691 /****************************************************************************
1693 ****************************************************************************/
1695 static int cmd_queue(void)
1697 cli_print_queue(cli, queue_fn);
1702 /****************************************************************************
1704 ****************************************************************************/
1706 static void do_del(file_info *finfo)
1710 pstr_sprintf( mask, "%s%c%s", finfo->dir, CLI_DIRSEP_CHAR, finfo->name );
1712 if (finfo->mode & aDIR)
1715 if (!cli_unlink(finfo->cli, mask)) {
1716 d_printf("%s deleting remote file %s\n",cli_errstr(finfo->cli),mask);
1720 /****************************************************************************
1722 ****************************************************************************/
1724 static int cmd_del(void)
1728 uint16 attribute = aSYSTEM | aHIDDEN;
1733 pstrcpy(mask,cur_dir);
1735 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1736 d_printf("del <filename>\n");
1741 do_list(mask, attribute,do_del,False,False);
1746 /****************************************************************************
1747 Wildcard delete some files.
1748 ****************************************************************************/
1750 static int cmd_wdel(void)
1755 struct cli_state *targetcli;
1758 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1759 d_printf("wdel 0x<attrib> <wcard>\n");
1763 attribute = (uint16)strtol(buf, (char **)NULL, 16);
1765 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1766 d_printf("wdel 0x<attrib> <wcard>\n");
1770 pstrcpy(mask,cur_dir);
1773 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1774 d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
1778 if (!cli_unlink_full(targetcli, targetname, attribute)) {
1779 d_printf("%s deleting remote files %s\n",cli_errstr(targetcli),targetname);
1784 /****************************************************************************
1785 ****************************************************************************/
1787 static int cmd_open(void)
1791 struct cli_state *targetcli;
1795 pstrcpy(mask,cur_dir);
1797 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1798 d_printf("open <filename>\n");
1803 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1804 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1808 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA|FILE_WRITE_DATA);
1810 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1812 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
1814 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1817 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
1823 /****************************************************************************
1824 ****************************************************************************/
1826 static int cmd_posix_open(void)
1830 struct cli_state *targetcli;
1835 pstrcpy(mask,cur_dir);
1837 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1838 d_printf("posix_open <filename> 0<mode>\n");
1842 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1843 d_printf("posix_open <filename> 0<mode>\n");
1846 mode = (mode_t)strtol(buf, (char **)NULL, 8);
1848 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1849 d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
1853 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode);
1855 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDONLY, mode);
1857 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
1859 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1862 d_printf("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
1868 static int cmd_posix_mkdir(void)
1872 struct cli_state *targetcli;
1877 pstrcpy(mask,cur_dir);
1879 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1880 d_printf("posix_mkdir <filename> 0<mode>\n");
1884 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1885 d_printf("posix_mkdir <filename> 0<mode>\n");
1888 mode = (mode_t)strtol(buf, (char **)NULL, 8);
1890 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1891 d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
1895 fnum = cli_posix_mkdir(targetcli, targetname, mode);
1897 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1899 d_printf("posix_mkdir created directory %s\n", targetname);
1905 static int cmd_posix_unlink(void)
1909 struct cli_state *targetcli;
1912 pstrcpy(mask,cur_dir);
1914 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1915 d_printf("posix_unlink <filename>\n");
1920 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1921 d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
1925 if (!cli_posix_unlink(targetcli, targetname)) {
1926 d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
1928 d_printf("posix_unlink deleted file %s\n", targetname);
1934 static int cmd_posix_rmdir(void)
1938 struct cli_state *targetcli;
1941 pstrcpy(mask,cur_dir);
1943 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1944 d_printf("posix_rmdir <filename>\n");
1949 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname)) {
1950 d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
1954 if (!cli_posix_rmdir(targetcli, targetname)) {
1955 d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
1957 d_printf("posix_rmdir deleted directory %s\n", targetname);
1963 static int cmd_close(void)
1968 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1969 d_printf("close <fnum>\n");
1974 /* We really should use the targetcli here.... */
1975 if (!cli_close(cli, fnum)) {
1976 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
1982 static int cmd_posix(void)
1984 uint16 major, minor;
1985 uint32 caplow, caphigh;
1988 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1989 d_printf("Server doesn't support UNIX CIFS extensions.\n");
1993 if (!cli_unix_extensions_version(cli, &major, &minor, &caplow, &caphigh)) {
1994 d_printf("Can't get UNIX CIFS extensions version from server.\n");
1998 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
2001 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
2002 pstrcat(caps, "locks ");
2004 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
2005 pstrcat(caps, "acls ");
2007 if (caplow & CIFS_UNIX_XATTTR_CAP) {
2008 pstrcat(caps, "eas ");
2010 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2011 pstrcat(caps, "pathnames ");
2013 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
2014 pstrcat(caps, "posix_path_operations ");
2017 if (strlen(caps) > 0 && caps[strlen(caps)-1] == ' ') {
2018 caps[strlen(caps)-1] = '\0';
2021 if (!cli_set_unix_extensions_capabilities(cli, major, minor, caplow, caphigh)) {
2022 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n", cli_errstr(cli));
2026 d_printf("Selecting server supported CIFS capabilities %s\n", caps);
2028 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2029 CLI_DIRSEP_CHAR = '/';
2030 *CLI_DIRSEP_STR = '/';
2031 pstrcpy(cur_dir, CLI_DIRSEP_STR);
2037 static int cmd_lock(void)
2040 SMB_BIG_UINT start, len;
2041 enum brl_type lock_type;
2044 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2045 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2050 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2051 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2055 if (*buf == 'r' || *buf == 'R') {
2056 lock_type = READ_LOCK;
2057 } else if (*buf == 'w' || *buf == 'W') {
2058 lock_type = WRITE_LOCK;
2060 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2064 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2065 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2069 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2071 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2072 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2076 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2078 if (!cli_posix_lock(cli, fnum, start, len, True, lock_type)) {
2079 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
2085 static int cmd_unlock(void)
2088 SMB_BIG_UINT start, len;
2091 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2092 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2097 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2098 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2102 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2104 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2105 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2109 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2111 if (!cli_posix_unlock(cli, fnum, start, len)) {
2112 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
2119 /****************************************************************************
2121 ****************************************************************************/
2123 static int cmd_rmdir(void)
2127 struct cli_state *targetcli;
2130 pstrcpy(mask,cur_dir);
2132 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2133 d_printf("rmdir <dirname>\n");
2138 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
2139 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
2143 if (!cli_rmdir(targetcli, targetname)) {
2144 d_printf("%s removing remote directory file %s\n",
2145 cli_errstr(targetcli),mask);
2151 /****************************************************************************
2153 ****************************************************************************/
2155 static int cmd_link(void)
2157 pstring oldname,newname;
2159 struct cli_state *targetcli;
2162 pstrcpy(oldname,cur_dir);
2163 pstrcpy(newname,cur_dir);
2165 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2166 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2167 d_printf("link <oldname> <newname>\n");
2171 pstrcat(oldname,buf);
2172 pstrcat(newname,buf2);
2174 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
2175 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2179 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2180 d_printf("Server doesn't support UNIX CIFS calls.\n");
2184 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
2185 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
2192 /****************************************************************************
2194 ****************************************************************************/
2196 static int cmd_symlink(void)
2198 pstring oldname,newname;
2200 struct cli_state *targetcli;
2203 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2204 d_printf("Server doesn't support UNIX CIFS calls.\n");
2208 pstrcpy(newname,cur_dir);
2210 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2211 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2212 d_printf("symlink <oldname> <newname>\n");
2216 pstrcpy(oldname,buf);
2217 pstrcat(newname,buf2);
2219 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
2220 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2224 if (!cli_unix_symlink(targetcli, targetname, newname)) {
2225 d_printf("%s symlinking files (%s -> %s)\n",
2226 cli_errstr(targetcli), newname, targetname);
2233 /****************************************************************************
2235 ****************************************************************************/
2237 static int cmd_chmod(void)
2242 struct cli_state *targetcli;
2245 pstrcpy(src,cur_dir);
2247 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2248 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2249 d_printf("chmod mode file\n");
2253 mode = (mode_t)strtol(buf, NULL, 8);
2256 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2257 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
2261 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2262 d_printf("Server doesn't support UNIX CIFS calls.\n");
2266 if (!cli_unix_chmod(targetcli, targetname, mode)) {
2267 d_printf("%s chmod file %s 0%o\n",
2268 cli_errstr(targetcli), src, (unsigned int)mode);
2275 static const char *filetype_to_str(mode_t mode)
2277 if (S_ISREG(mode)) {
2278 return "regular file";
2279 } else if (S_ISDIR(mode)) {
2283 if (S_ISCHR(mode)) {
2284 return "character device";
2288 if (S_ISBLK(mode)) {
2289 return "block device";
2293 if (S_ISFIFO(mode)) {
2298 if (S_ISLNK(mode)) {
2299 return "symbolic link";
2303 if (S_ISSOCK(mode)) {
2310 static char rwx_to_str(mode_t m, mode_t bt, char ret)
2319 static char *unix_mode_to_str(char *s, mode_t m)
2322 const char *str = filetype_to_str(m);
2338 *p++ = str[1] == 'y' ? 'l' : 's';
2345 *p++ = rwx_to_str(m, S_IRUSR, 'r');
2346 *p++ = rwx_to_str(m, S_IWUSR, 'w');
2347 *p++ = rwx_to_str(m, S_IXUSR, 'x');
2348 *p++ = rwx_to_str(m, S_IRGRP, 'r');
2349 *p++ = rwx_to_str(m, S_IWGRP, 'w');
2350 *p++ = rwx_to_str(m, S_IXGRP, 'x');
2351 *p++ = rwx_to_str(m, S_IROTH, 'r');
2352 *p++ = rwx_to_str(m, S_IWOTH, 'w');
2353 *p++ = rwx_to_str(m, S_IXOTH, 'x');
2358 /****************************************************************************
2359 Utility function for UNIX getfacl.
2360 ****************************************************************************/
2362 static char *perms_to_string(fstring permstr, unsigned char perms)
2364 fstrcpy(permstr, "---");
2365 if (perms & SMB_POSIX_ACL_READ) {
2368 if (perms & SMB_POSIX_ACL_WRITE) {
2371 if (perms & SMB_POSIX_ACL_EXECUTE) {
2377 /****************************************************************************
2379 ****************************************************************************/
2381 static int cmd_getfacl(void)
2384 uint16 major, minor;
2385 uint32 caplow, caphigh;
2386 char *retbuf = NULL;
2388 SMB_STRUCT_STAT sbuf;
2389 uint16 num_file_acls = 0;
2390 uint16 num_dir_acls = 0;
2392 struct cli_state *targetcli;
2395 pstrcpy(src,cur_dir);
2397 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2398 d_printf("stat file\n");
2404 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2405 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2409 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2410 d_printf("Server doesn't support UNIX CIFS calls.\n");
2414 if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
2415 d_printf("Can't get UNIX CIFS version from server.\n");
2419 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
2420 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
2424 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2425 d_printf("%s getfacl doing a stat on file %s\n",
2426 cli_errstr(targetcli), src);
2430 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2431 d_printf("%s getfacl file %s\n",
2432 cli_errstr(targetcli), src);
2436 /* ToDo : Print out the ACL values. */
2437 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2438 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2439 src, (unsigned int)CVAL(retbuf,0) );
2444 num_file_acls = SVAL(retbuf,2);
2445 num_dir_acls = SVAL(retbuf,4);
2446 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2447 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2449 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2450 (unsigned int)rb_size);
2456 d_printf("# file: %s\n", src);
2457 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2459 if (num_file_acls == 0 && num_dir_acls == 0) {
2460 d_printf("No acls found.\n");
2463 for (i = 0; i < num_file_acls; i++) {
2466 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2467 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2470 case SMB_POSIX_ACL_USER_OBJ:
2473 case SMB_POSIX_ACL_USER:
2474 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2475 d_printf("user:%u:", uorg);
2477 case SMB_POSIX_ACL_GROUP_OBJ:
2478 d_printf("group::");
2480 case SMB_POSIX_ACL_GROUP:
2481 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2482 d_printf("group:%u", uorg);
2484 case SMB_POSIX_ACL_MASK:
2487 case SMB_POSIX_ACL_OTHER:
2488 d_printf("other::");
2491 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2492 src, (unsigned int)tagtype );
2497 d_printf("%s\n", perms_to_string(permstring, perms));
2500 for (i = 0; i < num_dir_acls; i++) {
2503 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2504 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2507 case SMB_POSIX_ACL_USER_OBJ:
2508 d_printf("default:user::");
2510 case SMB_POSIX_ACL_USER:
2511 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2512 d_printf("default:user:%u:", uorg);
2514 case SMB_POSIX_ACL_GROUP_OBJ:
2515 d_printf("default:group::");
2517 case SMB_POSIX_ACL_GROUP:
2518 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2519 d_printf("default:group:%u", uorg);
2521 case SMB_POSIX_ACL_MASK:
2522 d_printf("default:mask::");
2524 case SMB_POSIX_ACL_OTHER:
2525 d_printf("default:other::");
2528 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2529 src, (unsigned int)tagtype );
2534 d_printf("%s\n", perms_to_string(permstring, perms));
2541 /****************************************************************************
2543 ****************************************************************************/
2545 static int cmd_stat(void)
2549 SMB_STRUCT_STAT sbuf;
2550 struct cli_state *targetcli;
2554 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2555 d_printf("Server doesn't support UNIX CIFS calls.\n");
2559 pstrcpy(src,cur_dir);
2561 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2562 d_printf("stat file\n");
2569 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2570 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2574 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2575 d_printf("%s stat file %s\n",
2576 cli_errstr(targetcli), src);
2580 /* Print out the stat values. */
2581 d_printf("File: %s\n", src);
2582 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2583 (double)sbuf.st_size,
2584 (unsigned int)sbuf.st_blocks,
2585 filetype_to_str(sbuf.st_mode));
2587 #if defined(S_ISCHR) && defined(S_ISBLK)
2588 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2589 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2590 (double)sbuf.st_ino,
2591 (unsigned int)sbuf.st_nlink,
2592 unix_dev_major(sbuf.st_rdev),
2593 unix_dev_minor(sbuf.st_rdev));
2596 d_printf("Inode: %.0f\tLinks: %u\n",
2597 (double)sbuf.st_ino,
2598 (unsigned int)sbuf.st_nlink);
2600 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2601 ((int)sbuf.st_mode & 0777),
2602 unix_mode_to_str(mode_str, sbuf.st_mode),
2603 (unsigned int)sbuf.st_uid,
2604 (unsigned int)sbuf.st_gid);
2606 lt = localtime(&sbuf.st_atime);
2608 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2610 fstrcpy(mode_str, "unknown");
2612 d_printf("Access: %s\n", mode_str);
2614 lt = localtime(&sbuf.st_mtime);
2616 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2618 fstrcpy(mode_str, "unknown");
2620 d_printf("Modify: %s\n", mode_str);
2622 lt = localtime(&sbuf.st_ctime);
2624 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2626 fstrcpy(mode_str, "unknown");
2628 d_printf("Change: %s\n", mode_str);
2634 /****************************************************************************
2636 ****************************************************************************/
2638 static int cmd_chown(void)
2643 pstring buf, buf2, buf3;
2644 struct cli_state *targetcli;
2647 pstrcpy(src,cur_dir);
2649 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2650 !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2651 !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2652 d_printf("chown uid gid file\n");
2656 uid = (uid_t)atoi(buf);
2657 gid = (gid_t)atoi(buf2);
2660 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2661 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2665 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2666 d_printf("Server doesn't support UNIX CIFS calls.\n");
2670 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2671 d_printf("%s chown file %s uid=%d, gid=%d\n",
2672 cli_errstr(targetcli), src, (int)uid, (int)gid);
2679 /****************************************************************************
2681 ****************************************************************************/
2683 static int cmd_rename(void)
2687 struct cli_state *targetcli;
2691 pstrcpy(src,cur_dir);
2692 pstrcpy(dest,cur_dir);
2694 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2695 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2696 d_printf("rename <src> <dest>\n");
2703 if ( !cli_resolve_path( "", cli, src, &targetcli, targetsrc ) ) {
2704 d_printf("rename %s: %s\n", src, cli_errstr(cli));
2708 if ( !cli_resolve_path( "", cli, dest, &targetcli, targetdest ) ) {
2709 d_printf("rename %s: %s\n", dest, cli_errstr(cli));
2713 if (!cli_rename(targetcli, targetsrc, targetdest)) {
2714 d_printf("%s renaming files %s -> %s \n",
2715 cli_errstr(targetcli),
2724 /****************************************************************************
2725 Print the volume name.
2726 ****************************************************************************/
2728 static int cmd_volume(void)
2734 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2735 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2739 d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2743 /****************************************************************************
2744 Hard link files using the NT call.
2745 ****************************************************************************/
2747 static int cmd_hardlink(void)
2751 struct cli_state *targetcli;
2754 pstrcpy(src,cur_dir);
2755 pstrcpy(dest,cur_dir);
2757 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2758 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2759 d_printf("hardlink <src> <dest>\n");
2766 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2767 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2771 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2772 d_printf("Server doesn't support UNIX CIFS calls.\n");
2776 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2777 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2784 /****************************************************************************
2785 Toggle the prompt flag.
2786 ****************************************************************************/
2788 static int cmd_prompt(void)
2791 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2796 /****************************************************************************
2797 Set the newer than time.
2798 ****************************************************************************/
2800 static int cmd_newer(void)
2804 SMB_STRUCT_STAT sbuf;
2806 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2807 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2808 newer_than = sbuf.st_mtime;
2809 DEBUG(1,("Getting files newer than %s",
2810 time_to_asc(newer_than)));
2815 if (ok && newer_than == 0) {
2816 d_printf("Error setting newer-than time\n");
2823 /****************************************************************************
2824 Set the archive level.
2825 ****************************************************************************/
2827 static int cmd_archive(void)
2831 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2832 archive_level = atoi(buf);
2834 d_printf("Archive level is %d\n",archive_level);
2839 /****************************************************************************
2840 Toggle the lowercaseflag.
2841 ****************************************************************************/
2843 static int cmd_lowercase(void)
2845 lowercase = !lowercase;
2846 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2851 /****************************************************************************
2852 Toggle the case sensitive flag.
2853 ****************************************************************************/
2855 static int cmd_setcase(void)
2857 bool orig_case_sensitive = cli_set_case_sensitive(cli, False);
2859 cli_set_case_sensitive(cli, !orig_case_sensitive);
2860 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2866 /****************************************************************************
2867 Toggle the showacls flag.
2868 ****************************************************************************/
2870 static int cmd_showacls(void)
2872 showacls = !showacls;
2873 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
2875 if (!ctx && showacls)
2876 ctx = talloc_init("smbclient:showacls");
2878 DEBUG( 0, ("cmd_showacls() out of memory. talloc_init() failed.\n"));
2885 /****************************************************************************
2886 Toggle the recurse flag.
2887 ****************************************************************************/
2889 static int cmd_recurse(void)
2892 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2897 /****************************************************************************
2898 Toggle the translate flag.
2899 ****************************************************************************/
2901 static int cmd_translate(void)
2903 translation = !translation;
2904 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2905 translation?"on":"off"));
2910 /****************************************************************************
2912 ****************************************************************************/
2914 static int cmd_lcd(void)
2919 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2921 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2926 /****************************************************************************
2927 Get a file restarting at end of local file.
2928 ****************************************************************************/
2930 static int cmd_reget(void)
2933 pstring remote_name;
2936 pstrcpy(remote_name, cur_dir);
2937 pstrcat(remote_name, CLI_DIRSEP_STR);
2939 p = remote_name + strlen(remote_name);
2941 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2942 d_printf("reget <filename>\n");
2945 pstrcpy(local_name, p);
2946 clean_name(remote_name);
2948 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2950 return do_get(remote_name, local_name, True);
2953 /****************************************************************************
2954 Put a file restarting at end of local file.
2955 ****************************************************************************/
2957 static int cmd_reput(void)
2960 pstring remote_name;
2965 pstrcpy(remote_name, cur_dir);
2966 pstrcat(remote_name, CLI_DIRSEP_STR);
2968 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2969 d_printf("reput <filename>\n");
2972 pstrcpy(local_name, p);
2974 if (!file_exist(local_name, &st)) {
2975 d_printf("%s does not exist\n", local_name);
2979 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2980 pstrcat(remote_name, p);
2982 pstrcat(remote_name, local_name);
2984 clean_name(remote_name);
2986 return do_put(remote_name, local_name, True);
2989 /****************************************************************************
2991 ****************************************************************************/
2993 static void browse_fn(const char *name, uint32 m,
2994 const char *comment, void *state)
3002 case STYPE_DISKTREE:
3003 fstrcpy(typestr,"Disk"); break;
3005 fstrcpy(typestr,"Printer"); break;
3007 fstrcpy(typestr,"Device"); break;
3009 fstrcpy(typestr,"IPC"); break;
3011 /* FIXME: If the remote machine returns non-ascii characters
3012 in any of these fields, they can corrupt the output. We
3013 should remove them. */
3015 d_printf("\t%-15s %-10.10s%s\n",
3016 name,typestr,comment);
3018 d_printf ("%s|%s|%s\n",typestr,name,comment);
3022 static bool browse_host_rpc(bool sort)
3025 struct rpc_pipe_client *pipe_hnd;
3026 TALLOC_CTX *mem_ctx;
3029 SRV_SHARE_INFO_CTR ctr;
3032 mem_ctx = talloc_new(NULL);
3033 if (mem_ctx == NULL) {
3034 DEBUG(0, ("talloc_new failed\n"));
3038 init_enum_hnd(&enum_hnd, 0);
3040 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
3042 if (pipe_hnd == NULL) {
3043 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
3044 nt_errstr(status)));
3045 TALLOC_FREE(mem_ctx);
3049 werr = rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, 1, &ctr,
3050 0xffffffff, &enum_hnd);
3052 if (!W_ERROR_IS_OK(werr)) {
3053 TALLOC_FREE(mem_ctx);
3054 cli_rpc_pipe_close(pipe_hnd);
3058 for (i=0; i<ctr.num_entries; i++) {
3059 SRV_SHARE_INFO_1 *info = &ctr.share.info1[i];
3060 char *name, *comment;
3061 name = rpcstr_pull_unistr2_talloc(
3062 mem_ctx, &info->info_1_str.uni_netname);
3063 comment = rpcstr_pull_unistr2_talloc(
3064 mem_ctx, &info->info_1_str.uni_remark);
3065 browse_fn(name, info->info_1.type, comment, NULL);
3068 TALLOC_FREE(mem_ctx);
3069 cli_rpc_pipe_close(pipe_hnd);
3073 /****************************************************************************
3074 Try and browse available connections on a host.
3075 ****************************************************************************/
3077 static bool browse_host(bool sort)
3081 d_printf("\n\tSharename Type Comment\n");
3082 d_printf("\t--------- ---- -------\n");
3085 if (browse_host_rpc(sort)) {
3089 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
3090 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
3095 /****************************************************************************
3097 ****************************************************************************/
3099 static void server_fn(const char *name, uint32 m,
3100 const char *comment, void *state)
3104 d_printf("\t%-16s %s\n", name, comment);
3106 d_printf("%s|%s|%s\n",(char *)state, name, comment);
3110 /****************************************************************************
3111 Try and browse available connections on a host.
3112 ****************************************************************************/
3114 static bool list_servers(const char *wk_grp)
3118 if (!cli->server_domain)
3122 d_printf("\n\tServer Comment\n");
3123 d_printf("\t--------- -------\n");
3125 fstrcpy( state, "Server" );
3126 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
3130 d_printf("\n\tWorkgroup Master\n");
3131 d_printf("\t--------- -------\n");
3134 fstrcpy( state, "Workgroup" );
3135 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
3140 /****************************************************************************
3141 Print or set current VUID
3142 ****************************************************************************/
3144 static int cmd_vuid(void)
3148 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3149 d_printf("Current VUID is %d\n", cli->vuid);
3153 cli->vuid = atoi(buf);
3157 /****************************************************************************
3158 Setup a new VUID, by issuing a session setup
3159 ****************************************************************************/
3161 static int cmd_logon(void)
3163 pstring l_username, l_password;
3166 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3167 d_printf("logon <username> [<password>]\n");
3171 pstrcpy(l_username, buf);
3173 if (!next_token_nr(NULL,buf2,NULL,sizeof(buf)))
3175 char *pass = getpass("Password: ");
3177 pstrcpy(l_password, pass);
3180 pstrcpy(l_password, buf2);
3182 if (!NT_STATUS_IS_OK(cli_session_setup(cli, l_username,
3183 l_password, strlen(l_password),
3184 l_password, strlen(l_password),
3186 d_printf("session setup failed: %s\n", cli_errstr(cli));
3190 d_printf("Current VUID is %d\n", cli->vuid);
3195 /****************************************************************************
3196 list active connections
3197 ****************************************************************************/
3199 static int cmd_list_connect(void)
3206 /****************************************************************************
3207 display the current active client connection
3208 ****************************************************************************/
3210 static int cmd_show_connect( void )
3212 struct cli_state *targetcli;
3215 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
3216 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
3220 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
3224 /* Some constants for completing filename arguments */
3226 #define COMPL_NONE 0 /* No completions */
3227 #define COMPL_REMOTE 1 /* Complete remote filename */
3228 #define COMPL_LOCAL 2 /* Complete local filename */
3230 /* This defines the commands supported by this client.
3231 * NOTE: The "!" must be the last one in the list because it's fn pointer
3232 * field is NULL, and NULL in that field is used in process_tok()
3233 * (below) to indicate the end of the list. crh
3239 const char *description;
3240 char compl_args[2]; /* Completion argument info */
3242 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3243 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
3244 {"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}},
3245 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
3246 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
3247 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
3248 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
3249 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
3250 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
3251 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
3252 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3253 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3254 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3255 {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
3256 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3257 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
3258 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
3259 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3260 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3261 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
3262 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
3263 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3264 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3265 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
3266 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3267 {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3268 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
3269 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3270 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
3271 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3272 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
3273 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
3274 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
3275 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
3276 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
3277 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3278 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3279 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3280 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3281 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
3282 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
3283 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
3284 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
3285 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3286 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
3287 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3288 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3289 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
3290 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
3291 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
3292 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
3293 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3294 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3295 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
3296 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
3297 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
3298 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
3299 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
3300 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
3301 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
3302 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3303 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
3304 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
3305 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3306 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
3307 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
3308 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
3309 {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
3311 /* Yes, this must be here, see crh's comment above. */
3312 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
3313 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
3316 /*******************************************************************
3317 Lookup a command string in the list of commands, including
3319 ******************************************************************/
3321 static int process_tok(pstring tok)
3323 int i = 0, matches = 0;
3325 int tok_len = strlen(tok);
3327 while (commands[i].fn != NULL) {
3328 if (strequal(commands[i].name,tok)) {
3332 } else if (strnequal(commands[i].name, tok, tok_len)) {
3341 else if (matches == 1)
3347 /****************************************************************************
3349 ****************************************************************************/
3351 static int cmd_help(void)
3356 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3357 if ((i = process_tok(buf)) >= 0)
3358 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
3360 while (commands[i].description) {
3361 for (j=0; commands[i].description && (j<5); j++) {
3362 d_printf("%-15s",commands[i].name);
3371 /****************************************************************************
3372 Process a -c command string.
3373 ****************************************************************************/
3375 static int process_command_string(char *cmd)
3381 /* establish the connection if not already */
3384 cli = cli_cm_open(desthost, service, True);
3389 while (cmd[0] != '\0') {
3394 if ((p = strchr_m(cmd, ';')) == 0) {
3395 strncpy(line, cmd, 999);
3401 strncpy(line, cmd, p - cmd);
3402 line[p - cmd] = '\0';
3406 /* and get the first part of the command */
3408 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3410 if ((i = process_tok(tok)) >= 0) {
3411 rc = commands[i].fn();
3412 } else if (i == -2) {
3413 d_printf("%s: command abbreviation ambiguous\n",tok);
3415 d_printf("%s: command not found\n",tok);
3422 #define MAX_COMPLETIONS 100
3430 } completion_remote_t;
3432 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
3434 completion_remote_t *info = (completion_remote_t *)state;
3436 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
3437 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
3438 info->matches[info->count] = SMB_STRDUP(f->name);
3442 if (info->dirmask[0] != 0)
3443 pstrcpy(tmp, info->dirmask);
3446 pstrcat(tmp, f->name);
3449 info->matches[info->count] = SMB_STRDUP(tmp);
3451 if (info->matches[info->count] == NULL)
3454 smb_readline_ca_char(0);
3456 if (info->count == 1)
3457 info->samelen = strlen(info->matches[info->count]);
3459 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
3465 static char **remote_completion(const char *text, int len)
3469 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
3471 /* can't have non-static intialisation on Sun CC, so do it
3477 if (len >= MIN(PATH_MAX,sizeof(pstring))) {
3481 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
3482 if (!info.matches) {
3487 * We're leaving matches[0] free to fill it later with the text to
3488 * display: Either the one single match or the longest common subset
3491 info.matches[0] = NULL;
3494 for (i = len-1; i >= 0; i--) {
3495 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
3500 info.text = text+i+1;
3501 info.samelen = info.len = len-i-1;
3504 strncpy(info.dirmask, text, i+1);
3505 info.dirmask[i+1] = 0;
3506 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
3508 pstr_sprintf(dirmask, "%s*", cur_dir);
3511 if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
3514 if (info.count == 1) {
3517 * No matches at all, NULL indicates there is nothing
3520 SAFE_FREE(info.matches[0]);
3521 SAFE_FREE(info.matches);
3525 if (info.count == 2) {
3528 * Exactly one match in matches[1], indicate this is the one
3532 info.matches[0] = info.matches[1];
3533 info.matches[1] = NULL;
3535 return info.matches;
3539 * We got more than one possible match, set the result to the maximum
3543 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
3544 info.matches[info.count] = NULL;
3545 return info.matches;
3548 for (i = 0; i < info.count; i++)
3549 free(info.matches[i]);
3554 static char **completion_fn(const char *text, int start, int end)
3556 smb_readline_ca_char(' ');
3559 const char *buf, *sp;
3563 buf = smb_readline_get_line_buffer();
3567 sp = strchr(buf, ' ');
3571 for (i = 0; commands[i].name; i++) {
3572 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
3573 (commands[i].name[sp - buf] == 0)) {
3577 if (commands[i].name == NULL)
3583 if (sp == (buf + start))
3584 compl_type = commands[i].compl_args[0];
3586 compl_type = commands[i].compl_args[1];
3588 if (compl_type == COMPL_REMOTE)
3589 return remote_completion(text, end - start);
3590 else /* fall back to local filename completion */
3594 int i, len, samelen = 0, count=1;
3596 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3603 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3604 if (strncmp(text, commands[i].name, len) == 0) {
3605 matches[count] = SMB_STRDUP(commands[i].name);
3606 if (!matches[count])
3609 samelen = strlen(matches[count]);
3611 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3618 case 0: /* should never happen */
3622 matches[0] = SMB_STRDUP(matches[1]);
3625 matches[0] = (char *)SMB_MALLOC(samelen+1);
3628 strncpy(matches[0], matches[1], samelen);
3629 matches[0][samelen] = 0;
3631 matches[count] = NULL;
3635 for (i = 0; i < count; i++)
3643 /****************************************************************************
3644 Make sure we swallow keepalives during idle time.
3645 ****************************************************************************/
3647 static void readline_callback(void)
3650 struct timeval timeout;
3651 static time_t last_t;
3667 FD_SET(cli->fd,&fds);
3670 timeout.tv_usec = 0;
3671 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3673 /* We deliberately use receive_smb instead of
3674 client_receive_smb as we want to receive
3675 session keepalives and then drop them here.
3677 if (FD_ISSET(cli->fd,&fds)) {
3678 if (!receive_smb(cli->fd,cli->inbuf,0)) {
3679 DEBUG(0, ("Read from server failed, maybe it closed the "
3686 /* Ping the server to keep the connection alive using SMBecho. */
3688 unsigned char garbage[16];
3689 memset(garbage, 0xf0, sizeof(garbage));
3690 cli_echo(cli, 1, garbage, sizeof(garbage));
3694 /****************************************************************************
3695 Process commands on stdin.
3696 ****************************************************************************/
3698 static int process_stdin(void)
3710 /* display a prompt */
3711 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3712 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3716 pstrcpy(line, cline);
3718 /* special case - first char is ! */
3724 /* and get the first part of the command */
3726 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3728 if ((i = process_tok(tok)) >= 0) {
3729 rc = commands[i].fn();
3730 } else if (i == -2) {
3731 d_printf("%s: command abbreviation ambiguous\n",tok);
3733 d_printf("%s: command not found\n",tok);
3739 /****************************************************************************
3740 Process commands from the client.
3741 ****************************************************************************/
3743 static int process(char *base_directory)
3747 cli = cli_cm_open(desthost, service, True);
3752 if (*base_directory) {
3753 rc = do_cd(base_directory);
3761 rc = process_command_string(cmdstr);
3770 /****************************************************************************
3772 ****************************************************************************/
3774 static int do_host_query(char *query_host)
3776 cli = cli_cm_open(query_host, "IPC$", True);
3784 /* Workgroups simply don't make sense over anything
3785 else but port 139... */
3788 cli_cm_set_port( 139 );
3789 cli = cli_cm_open(query_host, "IPC$", True);
3793 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3797 list_servers(lp_workgroup());
3804 /****************************************************************************
3805 Handle a tar operation.
3806 ****************************************************************************/
3808 static int do_tar_op(char *base_directory)
3812 /* do we already have a connection? */
3814 cli = cli_cm_open(desthost, service, True);
3821 if (*base_directory) {
3822 ret = do_cd(base_directory);
3836 /****************************************************************************
3837 Handle a message operation.
3838 ****************************************************************************/
3840 static int do_message_op(void)
3843 struct nmb_name called, calling;
3844 fstring server_name;
3845 char name_type_hex[10];
3849 make_nmb_name(&calling, calling_name, 0x0);
3850 make_nmb_name(&called , desthost, name_type);
3852 fstrcpy(server_name, desthost);
3853 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3854 fstrcat(server_name, name_type_hex);
3860 /* we can only do messages over port 139 (to windows clients at least) */
3862 msg_port = port ? port : 139;
3864 if (!(cli=cli_initialise()) || (cli_set_port(cli, msg_port) != msg_port)) {
3865 d_printf("Connection to %s failed\n", desthost);
3869 status = cli_connect(cli, server_name, &ip);
3870 if (!NT_STATUS_IS_OK(status)) {
3871 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
3875 if (!cli_session_request(cli, &calling, &called)) {
3876 d_printf("session request failed\n");
3888 /****************************************************************************
3890 ****************************************************************************/
3892 int main(int argc,char *argv[])
3894 pstring base_directory;
3897 bool message = False;
3899 static const char *new_name_resolve_order = NULL;
3903 fstring new_workgroup;
3904 bool tar_opt = False;
3905 bool service_opt = False;
3906 struct poptOption long_options[] = {
3909 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3910 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3911 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3912 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3913 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3914 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3915 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3916 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3917 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3918 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3919 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3920 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3921 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3923 POPT_COMMON_CONNECTION
3924 POPT_COMMON_CREDENTIALS
3931 pstrcpy(term_code, KANJI);
3937 *base_directory = 0;
3939 /* initialize the workgroup name so we can determine whether or
3940 not it was set by a command line option */
3942 set_global_myworkgroup( "" );
3943 set_global_myname( "" );
3945 /* set default debug level to 0 regardless of what smb.conf sets */
3946 setup_logging( "smbclient", True );
3947 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3948 if ((dbf = x_fdup(x_stderr))) {
3949 x_setbuf( dbf, NULL );
3953 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
3954 poptSetOtherOptionHelp(pc, "service <password>");
3956 in_client = True; /* Make sure that we tell lp_load we are */
3958 while ((opt = poptGetNextOpt(pc)) != -1) {
3960 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
3961 /* I see no other way to keep things sane --SSS */
3962 if (tar_opt == True) {
3963 while (poptPeekArg(pc)) {
3969 /* if the service has not yet been specified lets see if it is available in the popt stack */
3970 if (!service_opt && poptPeekArg(pc)) {
3971 pstrcpy(service, poptGetArg(pc));
3975 /* if the service has already been retrieved then check if we have also a password */
3976 if (service_opt && (!cmdline_auth_info.got_pass) && poptPeekArg(pc)) {
3977 pstrcpy(cmdline_auth_info.password, poptGetArg(pc));
3978 cmdline_auth_info.got_pass = True;
3983 /* Messages are sent to NetBIOS name type 0x3
3984 * (Messenger Service). Make sure we default
3985 * to port 139 instead of port 445. srl,crh
3988 cli_cm_set_dest_name_type( name_type );
3989 pstrcpy(desthost,poptGetOptArg(pc));
3991 cli_cm_set_port( 139 );
3996 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3997 if (is_zero_ip_v4(dest_ip))
4001 cli_cm_set_dest_ip( dest_ip );
4009 display_set_stderr();
4013 pstrcpy(query_host, poptGetOptArg(pc));
4016 pstrcpy(term_code, poptGetOptArg(pc));
4019 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
4022 /* We must use old option processing for this. Find the
4023 * position of the -T option in the raw argv[]. */
4026 for (i = 1; i < argc; i++) {
4027 if (strncmp("-T", argv[i],2)==0)
4031 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
4032 poptPrintUsage(pc, stderr, 0);
4036 /* this must be the last option, mark we have parsed it so that we know we have */
4040 pstrcpy(base_directory,poptGetOptArg(pc));
4048 /* We may still have some leftovers after the last popt option has been called */
4049 if (tar_opt == True) {
4050 while (poptPeekArg(pc)) {
4056 /* if the service has not yet been specified lets see if it is available in the popt stack */
4057 if (!service_opt && poptPeekArg(pc)) {
4058 pstrcpy(service, poptGetArg(pc));
4062 /* if the service has already been retrieved then check if we have also a password */
4063 if (service_opt && (!cmdline_auth_info.got_pass) && poptPeekArg(pc)) {
4064 pstrcpy(cmdline_auth_info.password, poptGetArg(pc));
4065 cmdline_auth_info.got_pass = True;
4068 /* check for the -P option */
4071 cli_cm_set_port( port );
4074 * Don't load debug level from smb.conf. It should be
4075 * set by cmdline arg or remain default (0)
4077 AllowDebugChange = False;
4079 /* save the workgroup...
4081 FIXME!! do we need to do this for other options as well
4082 (or maybe a generic way to keep lp_load() from overwriting
4085 fstrcpy( new_workgroup, lp_workgroup() );
4086 pstrcpy( calling_name, global_myname() );
4088 if ( override_logfile )
4089 setup_logging( lp_logfile(), False );
4091 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
4092 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
4093 argv[0], dyn_CONFIGFILE);
4099 /* Convert any '/' characters in the service name to '\' characters */
4100 string_replace(service, '/','\\');
4101 if (count_chars(service,'\\') < 3) {
4102 d_printf("\n%s: Not enough '\\' characters in service\n",service);
4103 poptPrintUsage(pc, stderr, 0);
4108 if ( strlen(new_workgroup) != 0 )
4109 set_global_myworkgroup( new_workgroup );
4111 if ( strlen(calling_name) != 0 )
4112 set_global_myname( calling_name );
4114 pstrcpy( calling_name, global_myname() );
4118 if(new_name_resolve_order)
4119 lp_set_name_resolve_order(new_name_resolve_order);
4121 if (!tar_type && !*query_host && !*service && !message) {
4122 poptPrintUsage(pc, stderr, 0);
4126 poptFreeContext(pc);
4128 /* store the username an password for dfs support */
4130 cli_cm_set_credentials( &cmdline_auth_info );
4131 pstrcpy(username, cmdline_auth_info.username);
4133 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
4137 process_command_string(cmdstr);
4138 return do_tar_op(base_directory);
4142 char *qhost = query_host;
4145 while (*qhost == '\\' || *qhost == '/')
4148 if ((slash = strchr_m(qhost, '/'))
4149 || (slash = strchr_m(qhost, '\\'))) {
4153 if ((p=strchr_m(qhost, '#'))) {
4156 sscanf(p, "%x", &name_type);
4157 cli_cm_set_dest_name_type( name_type );
4160 return do_host_query(qhost);
4164 return do_message_op();
4167 if (process(base_directory)) {
4171 talloc_destroy( ctx);