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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "client/client_proto.h"
26 #include "include/rpc_client.h"
31 extern BOOL AllowDebugChange;
32 extern BOOL override_logfile;
34 extern BOOL in_client;
36 pstring cur_dir = "\\";
37 static pstring cd_path = "";
38 static pstring service;
39 static pstring desthost;
40 static pstring username;
41 static pstring calling_name;
42 static BOOL grepable=False;
43 static char *cmdstr = NULL;
45 static int io_bufsize = 64512;
47 static int name_type = 0x20;
48 extern int max_protocol;
50 static int process_tok(pstring tok);
51 static int cmd_help(void);
53 static TALLOC_CTX *ctx;
54 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
57 /* 30 second timeout on most commands */
58 #define CLIENT_TIMEOUT (30*1000)
59 #define SHORT_TIMEOUT (5*1000)
61 /* value for unused fid field in trans2 secondary request */
62 #define FID_UNUSED (0xFFFF)
64 time_t newer_than = 0;
65 static int archive_level = 0;
67 static BOOL translation = False;
70 /* clitar bits insert */
73 extern BOOL tar_reset;
77 static BOOL prompt = True;
79 static BOOL recurse = False;
80 static BOOL showacls = False;
81 BOOL lowercase = False;
83 static struct in_addr dest_ip;
85 #define SEPARATORS " \t\n\r"
87 static BOOL abort_mget = True;
89 static pstring fileselection = "";
91 extern file_info def_finfo;
94 SMB_BIG_UINT get_total_size = 0;
95 unsigned int get_total_time_ms = 0;
96 static SMB_BIG_UINT put_total_size = 0;
97 static unsigned int put_total_time_ms = 0;
100 static double dir_total;
102 /* root cli_state connection */
104 struct cli_state *cli;
106 static char CLI_DIRSEP_CHAR = '\\';
107 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
109 /****************************************************************************
110 Write to a local file with CR/LF->LF translation if appropriate. Return the
111 number taken from the buffer. This may not equal the number written.
112 ****************************************************************************/
114 static int writefile(int f, char *b, int n)
124 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
127 if (write(f, b, 1) != 1) {
137 /****************************************************************************
138 Read from a file with LF->CR/LF translation if appropriate. Return the
139 number read. read approx n bytes.
140 ****************************************************************************/
142 static int readfile(char *b, int n, XFILE *f)
148 return x_fread(b,1,n,f);
151 while (i < (n - 1) && (i < BUFFER_SIZE)) {
152 if ((c = x_getc(f)) == EOF) {
156 if (c == '\n') { /* change all LFs to CR/LF */
166 /****************************************************************************
168 ****************************************************************************/
170 static void send_message(void)
175 if (!cli_message_start(cli, desthost, username, &grp_id)) {
176 d_printf("message start: %s\n", cli_errstr(cli));
181 d_printf("Connected. Type your message, ending it with a Control-D\n");
183 while (!feof(stdin) && total_len < 1600) {
184 int maxlen = MIN(1600 - total_len,127);
191 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
197 if ((total_len > 0) && (strlen(msg) == 0)) {
201 if (!cli_message_text(cli, msg, l, grp_id)) {
202 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
209 if (total_len >= 1600)
210 d_printf("the message was truncated to 1600 bytes\n");
212 d_printf("sent %d bytes\n",total_len);
214 if (!cli_message_end(cli, grp_id)) {
215 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
220 /****************************************************************************
221 Check the space on a device.
222 ****************************************************************************/
224 static int do_dskattr(void)
226 int total, bsize, avail;
227 struct cli_state *targetcli;
230 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
231 d_printf("Error in dskattr: %s\n", cli_errstr(cli));
235 if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
236 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
240 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
241 total, bsize, avail);
246 /****************************************************************************
248 ****************************************************************************/
250 static int cmd_pwd(void)
252 d_printf("Current directory is %s",service);
253 d_printf("%s\n",cur_dir);
257 /****************************************************************************
258 Change directory - inner section.
259 ****************************************************************************/
261 static int do_cd(char *newdir)
267 struct cli_state *targetcli;
268 SMB_STRUCT_STAT sbuf;
274 /* Save the current directory in case the new directory is invalid */
276 pstrcpy(saved_dir, cur_dir);
278 if (*p == CLI_DIRSEP_CHAR) {
284 /* Ensure cur_dir ends in a DIRSEP */
285 if ((cur_dir[0] != '\0') && (*(cur_dir+strlen(cur_dir)-1) != CLI_DIRSEP_CHAR)) {
286 pstrcat(cur_dir, CLI_DIRSEP_STR);
290 pstrcpy( dname, cur_dir );
292 if ( !cli_resolve_path( "", cli, dname, &targetcli, targetpath ) ) {
293 d_printf("cd %s: %s\n", dname, cli_errstr(cli));
294 pstrcpy(cur_dir,saved_dir);
298 if (strequal(targetpath,CLI_DIRSEP_STR )) {
302 /* Use a trans2_qpathinfo to test directories for modern servers.
303 Except Win9x doesn't support the qpathinfo_basic() call..... */
305 if ( targetcli->protocol > PROTOCOL_LANMAN2 && !targetcli->win95 ) {
306 if ( !cli_qpathinfo_basic( targetcli, targetpath, &sbuf, &attributes ) ) {
307 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
308 pstrcpy(cur_dir,saved_dir);
312 if ( !(attributes&FILE_ATTRIBUTE_DIRECTORY) ) {
313 d_printf("cd %s: not a directory\n", dname);
314 pstrcpy(cur_dir,saved_dir);
318 pstrcat( targetpath, CLI_DIRSEP_STR );
319 clean_name( targetpath );
321 if ( !cli_chkpath(targetcli, targetpath) ) {
322 d_printf("cd %s: %s\n", dname, cli_errstr(targetcli));
323 pstrcpy(cur_dir,saved_dir);
332 pstrcpy(cd_path,cur_dir);
336 /****************************************************************************
338 ****************************************************************************/
340 static int cmd_cd(void)
345 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
348 d_printf("Current directory is %s\n",cur_dir);
353 /*******************************************************************
354 Decide if a file should be operated on.
355 ********************************************************************/
357 static BOOL do_this_one(file_info *finfo)
359 if (finfo->mode & aDIR)
362 if (*fileselection &&
363 !mask_match(finfo->name,fileselection,False)) {
364 DEBUG(3,("mask_match %s failed\n", finfo->name));
368 if (newer_than && finfo->mtime_ts.tv_sec < newer_than) {
369 DEBUG(3,("newer_than %s failed\n", finfo->name));
373 if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
374 DEBUG(3,("archive %s failed\n", finfo->name));
381 /****************************************************************************
382 Display info about a file.
383 ****************************************************************************/
385 static void display_finfo(file_info *finfo)
387 if (do_this_one(finfo)) {
388 time_t t = finfo->mtime_ts.tv_sec; /* the time is assumed to be passed as GMT */
390 d_printf(" %-30s%7.7s %8.0f %s",
392 attrib_string(finfo->mode),
395 dir_total += finfo->size;
400 /* skip if this is . or .. */
401 if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
403 /* create absolute filename for cli_nt_create() FIXME */
404 pstrcpy( afname, cwd);
405 pstrcat( afname, CLI_DIRSEP_STR);
406 pstrcat( afname, finfo->name);
407 /* print file meta date header */
408 d_printf( "FILENAME:%s\n", afname);
409 d_printf( "MODE:%s\n", attrib_string(finfo->mode));
410 d_printf( "SIZE:%.0f\n", (double)finfo->size);
411 d_printf( "MTIME:%s", time_to_asc(t));
412 fnum = cli_nt_create(finfo->cli, afname, CREATE_ACCESS_READ);
414 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
416 cli_errstr( finfo->cli)));
419 sd = cli_query_secdesc(finfo->cli, fnum, ctx);
421 DEBUG( 0, ("display_finfo() failed to "
422 "get security descriptor: %s",
423 cli_errstr( finfo->cli)));
425 display_sec_desc(sd);
432 /****************************************************************************
433 Accumulate size of a file.
434 ****************************************************************************/
436 static void do_du(file_info *finfo)
438 if (do_this_one(finfo)) {
439 dir_total += finfo->size;
443 static BOOL do_list_recurse;
444 static BOOL do_list_dirs;
445 static char *do_list_queue = 0;
446 static long do_list_queue_size = 0;
447 static long do_list_queue_start = 0;
448 static long do_list_queue_end = 0;
449 static void (*do_list_fn)(file_info *);
451 /****************************************************************************
452 Functions for do_list_queue.
453 ****************************************************************************/
456 * The do_list_queue is a NUL-separated list of strings stored in a
457 * char*. Since this is a FIFO, we keep track of the beginning and
458 * ending locations of the data in the queue. When we overflow, we
459 * double the size of the char*. When the start of the data passes
460 * the midpoint, we move everything back. This is logically more
461 * complex than a linked list, but easier from a memory management
462 * angle. In any memory error condition, do_list_queue is reset.
463 * Functions check to ensure that do_list_queue is non-NULL before
467 static void reset_do_list_queue(void)
469 SAFE_FREE(do_list_queue);
470 do_list_queue_size = 0;
471 do_list_queue_start = 0;
472 do_list_queue_end = 0;
475 static void init_do_list_queue(void)
477 reset_do_list_queue();
478 do_list_queue_size = 1024;
479 do_list_queue = (char *)SMB_MALLOC(do_list_queue_size);
480 if (do_list_queue == 0) {
481 d_printf("malloc fail for size %d\n",
482 (int)do_list_queue_size);
483 reset_do_list_queue();
485 memset(do_list_queue, 0, do_list_queue_size);
489 static void adjust_do_list_queue(void)
492 * If the starting point of the queue is more than half way through,
493 * move everything toward the beginning.
496 if (do_list_queue == NULL) {
497 DEBUG(4,("do_list_queue is empty\n"));
498 do_list_queue_start = do_list_queue_end = 0;
502 if (do_list_queue_start == do_list_queue_end) {
503 DEBUG(4,("do_list_queue is empty\n"));
504 do_list_queue_start = do_list_queue_end = 0;
505 *do_list_queue = '\0';
506 } else if (do_list_queue_start > (do_list_queue_size / 2)) {
507 DEBUG(4,("sliding do_list_queue backward\n"));
508 memmove(do_list_queue,
509 do_list_queue + do_list_queue_start,
510 do_list_queue_end - do_list_queue_start);
511 do_list_queue_end -= do_list_queue_start;
512 do_list_queue_start = 0;
516 static void add_to_do_list_queue(const char* entry)
518 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
519 while (new_end > do_list_queue_size) {
520 do_list_queue_size *= 2;
521 DEBUG(4,("enlarging do_list_queue to %d\n",
522 (int)do_list_queue_size));
523 do_list_queue = (char *)SMB_REALLOC(do_list_queue, do_list_queue_size);
524 if (! do_list_queue) {
525 d_printf("failure enlarging do_list_queue to %d bytes\n",
526 (int)do_list_queue_size);
527 reset_do_list_queue();
529 memset(do_list_queue + do_list_queue_size / 2,
530 0, do_list_queue_size / 2);
534 safe_strcpy_base(do_list_queue + do_list_queue_end,
535 entry, do_list_queue, do_list_queue_size);
536 do_list_queue_end = new_end;
537 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
538 entry, (int)do_list_queue_start, (int)do_list_queue_end));
542 static char *do_list_queue_head(void)
544 return do_list_queue + do_list_queue_start;
547 static void remove_do_list_queue_head(void)
549 if (do_list_queue_end > do_list_queue_start) {
550 do_list_queue_start += strlen(do_list_queue_head()) + 1;
551 adjust_do_list_queue();
552 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
553 (int)do_list_queue_start, (int)do_list_queue_end));
557 static int do_list_queue_empty(void)
559 return (! (do_list_queue && *do_list_queue));
562 /****************************************************************************
563 A helper for do_list.
564 ****************************************************************************/
566 static void do_list_helper(const char *mntpoint, file_info *f, const char *mask, void *state)
570 /* save the directory */
571 pstrcpy( f->dir, mask );
572 if ( (dir_end = strrchr( f->dir, CLI_DIRSEP_CHAR )) != NULL ) {
576 if (f->mode & aDIR) {
577 if (do_list_dirs && do_this_one(f)) {
580 if (do_list_recurse &&
581 !strequal(f->name,".") &&
582 !strequal(f->name,"..")) {
587 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
591 pstrcpy(mask2, mntpoint);
592 pstrcat(mask2, mask);
593 p = strrchr_m(mask2,CLI_DIRSEP_CHAR);
597 pstrcat(mask2, f->name);
598 pstrcat(mask2,CLI_DIRSEP_STR);
600 add_to_do_list_queue(mask2);
605 if (do_this_one(f)) {
610 /****************************************************************************
611 A wrapper around cli_list that adds recursion.
612 ****************************************************************************/
614 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
616 static int in_do_list = 0;
617 struct cli_state *targetcli;
620 if (in_do_list && rec) {
621 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
627 do_list_recurse = rec;
632 init_do_list_queue();
633 add_to_do_list_queue(mask);
635 while (! do_list_queue_empty()) {
637 * Need to copy head so that it doesn't become
638 * invalid inside the call to cli_list. This
639 * would happen if the list were expanded
641 * Fix from E. Jay Berkenbilt (ejb@ql.org)
644 pstrcpy(head, do_list_queue_head());
648 if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) {
649 d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
650 remove_do_list_queue_head();
654 cli_list(targetcli, targetpath, attribute, do_list_helper, NULL);
655 remove_do_list_queue_head();
656 if ((! do_list_queue_empty()) && (fn == display_finfo)) {
657 char* next_file = do_list_queue_head();
659 if ((strlen(next_file) >= 2) &&
660 (next_file[strlen(next_file) - 1] == '*') &&
661 (next_file[strlen(next_file) - 2] == CLI_DIRSEP_CHAR)) {
662 save_ch = next_file +
663 strlen(next_file) - 2;
665 if (showacls) /* cwd is only used if showacls is on */
666 pstrcpy( cwd, next_file);
668 if (!showacls) /* don't disturbe the showacls output */
669 d_printf("\n%s\n",next_file);
671 *save_ch = CLI_DIRSEP_CHAR;
678 if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) {
679 if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1)
680 d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath);
683 d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli));
688 reset_do_list_queue();
691 /****************************************************************************
692 Get a directory listing.
693 ****************************************************************************/
695 static int cmd_dir(void)
697 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
704 pstrcpy(mask,cur_dir);
706 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
708 if (*p == CLI_DIRSEP_CHAR)
717 /* cwd is only used if showacls is on */
718 pstrcpy(cwd, cur_dir);
721 do_list(mask, attribute, display_finfo, recurse, True);
725 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
730 /****************************************************************************
731 Get a directory listing.
732 ****************************************************************************/
734 static int cmd_du(void)
736 uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
743 pstrcpy(mask,cur_dir);
745 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
747 if (*p == CLI_DIRSEP_CHAR)
755 do_list(mask, attribute, do_du, recurse, True);
759 d_printf("Total number of bytes: %.0f\n", dir_total);
764 /****************************************************************************
765 Get a file from rname to lname
766 ****************************************************************************/
768 static int do_get(char *rname, char *lname, BOOL reget)
770 int handle = 0, fnum;
771 BOOL newhandle = False;
773 struct timeval tp_start;
774 int read_size = io_bufsize;
780 struct cli_state *targetcli;
788 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
789 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
793 GetTimeOfDay(&tp_start);
795 fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
798 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
802 if(!strcmp(lname,"-")) {
803 handle = fileno(stdout);
806 handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
808 start = sys_lseek(handle, 0, SEEK_END);
810 d_printf("Error seeking local file\n");
815 handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
820 d_printf("Error opening local file %s\n",lname);
825 if (!cli_qfileinfo(targetcli, fnum,
826 &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
827 !cli_getattrE(targetcli, fnum,
828 &attr, &size, NULL, NULL, NULL)) {
829 d_printf("getattrib: %s\n",cli_errstr(targetcli));
833 DEBUG(1,("getting file %s of size %.0f as %s ",
834 rname, (double)size, lname));
836 if(!(data = (char *)SMB_MALLOC(read_size))) {
837 d_printf("malloc fail for size %d\n", read_size);
838 cli_close(targetcli, fnum);
843 int n = cli_read(targetcli, fnum, data, nread + start, read_size);
848 if (writefile(handle,data, n) != n) {
849 d_printf("Error writing local file\n");
857 if (nread + start < size) {
858 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
859 rname, (long)nread));
866 if (!cli_close(targetcli, fnum)) {
867 d_printf("Error %s closing remote file\n",cli_errstr(cli));
875 if (archive_level >= 2 && (attr & aARCH)) {
876 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
880 struct timeval tp_end;
883 GetTimeOfDay(&tp_end);
885 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
886 (tp_end.tv_usec - tp_start.tv_usec)/1000;
887 get_total_time_ms += this_time;
888 get_total_size += nread;
890 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
891 nread / (1.024*this_time + 1.0e-4),
892 get_total_size / (1.024*get_total_time_ms)));
898 /****************************************************************************
900 ****************************************************************************/
902 static int cmd_get(void)
908 pstrcpy(rname,cur_dir);
910 p = rname + strlen(rname);
912 if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
913 d_printf("get <filename>\n");
919 next_token_nr(NULL,lname,NULL,sizeof(lname));
921 return do_get(rname, lname, False);
924 /****************************************************************************
925 Do an mget operation on one file.
926 ****************************************************************************/
928 static void do_mget(file_info *finfo)
932 pstring saved_curdir;
935 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
939 d_printf("mget aborted\n");
943 if (finfo->mode & aDIR)
944 slprintf(quest,sizeof(pstring)-1,
945 "Get directory %s? ",finfo->name);
947 slprintf(quest,sizeof(pstring)-1,
948 "Get file %s? ",finfo->name);
950 if (prompt && !yesno(quest))
953 if (!(finfo->mode & aDIR)) {
954 pstrcpy(rname,cur_dir);
955 pstrcat(rname,finfo->name);
956 do_get(rname, finfo->name, False);
960 /* handle directories */
961 pstrcpy(saved_curdir,cur_dir);
963 pstrcat(cur_dir,finfo->name);
964 pstrcat(cur_dir,CLI_DIRSEP_STR);
966 unix_format(finfo->name);
968 strlower_m(finfo->name);
970 if (!directory_exist(finfo->name,NULL) &&
971 mkdir(finfo->name,0777) != 0) {
972 d_printf("failed to create directory %s\n",finfo->name);
973 pstrcpy(cur_dir,saved_curdir);
977 if (chdir(finfo->name) != 0) {
978 d_printf("failed to chdir to directory %s\n",finfo->name);
979 pstrcpy(cur_dir,saved_curdir);
983 pstrcpy(mget_mask,cur_dir);
984 pstrcat(mget_mask,"*");
986 do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
988 pstrcpy(cur_dir,saved_curdir);
991 /****************************************************************************
992 View the file using the pager.
993 ****************************************************************************/
995 static int cmd_more(void)
997 pstring rname,lname,pager_cmd;
1002 pstrcpy(rname,cur_dir);
1004 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
1005 fd = smb_mkstemp(lname);
1007 d_printf("failed to create temporary file for more\n");
1012 if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
1013 d_printf("more <filename>\n");
1019 rc = do_get(rname, lname, False);
1021 pager=getenv("PAGER");
1023 slprintf(pager_cmd,sizeof(pager_cmd)-1,
1024 "%s %s",(pager? pager:PAGER), lname);
1031 /****************************************************************************
1033 ****************************************************************************/
1035 static int cmd_mget(void)
1037 uint16 attribute = aSYSTEM | aHIDDEN;
1049 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1050 pstrcpy(mget_mask,cur_dir);
1052 if (*p == CLI_DIRSEP_CHAR)
1053 pstrcpy(mget_mask,p);
1055 pstrcat(mget_mask,p);
1056 do_list(mget_mask, attribute,do_mget,False,True);
1060 pstrcpy(mget_mask,cur_dir);
1061 pstrcat(mget_mask,"*");
1062 do_list(mget_mask, attribute,do_mget,False,True);
1068 /****************************************************************************
1069 Make a directory of name "name".
1070 ****************************************************************************/
1072 static BOOL do_mkdir(char *name)
1074 struct cli_state *targetcli;
1077 if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) {
1078 d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
1082 if (!cli_mkdir(targetcli, targetname)) {
1083 d_printf("%s making remote directory %s\n",
1084 cli_errstr(targetcli),name);
1091 /****************************************************************************
1092 Show 8.3 name of a file.
1093 ****************************************************************************/
1095 static BOOL do_altname(char *name)
1098 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) {
1099 d_printf("%s getting alt name for %s\n",
1100 cli_errstr(cli),name);
1103 d_printf("%s\n", altname);
1108 /****************************************************************************
1110 ****************************************************************************/
1112 static int cmd_quit(void)
1115 talloc_destroy( ctx);
1121 /****************************************************************************
1123 ****************************************************************************/
1125 static int cmd_mkdir(void)
1131 pstrcpy(mask,cur_dir);
1133 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1135 d_printf("mkdir <dirname>\n");
1143 struct cli_state *targetcli;
1147 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1151 pstrcpy(ddir,targetname);
1152 trim_char(ddir,'.','\0');
1153 p = strtok(ddir,"/\\");
1156 if (!cli_chkpath(targetcli, ddir2)) {
1159 pstrcat(ddir2,CLI_DIRSEP_STR);
1160 p = strtok(NULL,"/\\");
1169 /****************************************************************************
1171 ****************************************************************************/
1173 static int cmd_altname(void)
1179 pstrcpy(name,cur_dir);
1181 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1182 d_printf("altname <file>\n");
1192 /****************************************************************************
1194 ****************************************************************************/
1196 static int do_put(char *rname, char *lname, BOOL reput)
1200 SMB_OFF_T start = 0;
1203 int maxwrite = io_bufsize;
1205 struct timeval tp_start;
1206 struct cli_state *targetcli;
1209 if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) {
1210 d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
1214 GetTimeOfDay(&tp_start);
1217 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
1219 if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1220 !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
1221 d_printf("getattrib: %s\n",cli_errstr(cli));
1226 fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1230 d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
1234 /* allow files to be piped into smbclient
1237 Note that in this case this function will exit(0) rather
1239 if (!strcmp(lname, "-")) {
1241 /* size of file is not known */
1243 f = x_fopen(lname,O_RDONLY, 0);
1245 if (x_tseek(f, start, SEEK_SET) == -1) {
1246 d_printf("Error seeking local file\n");
1253 d_printf("Error opening local file %s\n",lname);
1257 DEBUG(1,("putting file %s as %s ",lname,
1260 buf = (char *)SMB_MALLOC(maxwrite);
1262 d_printf("ERROR: Not enough memory!\n");
1265 while (!x_feof(f)) {
1269 if ((n = readfile(buf,n,f)) < 1) {
1270 if((n == 0) && x_feof(f))
1271 break; /* Empty local file. */
1273 d_printf("Error reading local file: %s\n", strerror(errno));
1278 ret = cli_write(targetcli, fnum, 0, buf, nread + start, n);
1281 d_printf("Error writing file: %s\n", cli_errstr(cli));
1289 if (!cli_close(targetcli, fnum)) {
1290 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1304 struct timeval tp_end;
1307 GetTimeOfDay(&tp_end);
1309 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1310 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1311 put_total_time_ms += this_time;
1312 put_total_size += nread;
1314 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1315 nread / (1.024*this_time + 1.0e-4),
1316 put_total_size / (1.024*put_total_time_ms)));
1327 /****************************************************************************
1329 ****************************************************************************/
1331 static int cmd_put(void)
1338 pstrcpy(rname,cur_dir);
1340 if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1341 d_printf("put <filename>\n");
1346 if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1349 pstrcat(rname,lname);
1355 /* allow '-' to represent stdin
1356 jdblair, 24.jun.98 */
1357 if (!file_exist(lname,&st) &&
1358 (strcmp(lname,"-"))) {
1359 d_printf("%s does not exist\n",lname);
1364 return do_put(rname, lname, False);
1367 /*************************************
1368 File list structure.
1369 *************************************/
1371 static struct file_list {
1372 struct file_list *prev, *next;
1377 /****************************************************************************
1378 Free a file_list structure.
1379 ****************************************************************************/
1381 static void free_file_list (struct file_list *list_head)
1383 struct file_list *list, *next;
1385 for (list = list_head; list; list = next) {
1387 DLIST_REMOVE(list_head, list);
1388 SAFE_FREE(list->file_path);
1393 /****************************************************************************
1394 Seek in a directory/file list until you get something that doesn't start with
1396 ****************************************************************************/
1398 static BOOL seek_list(struct file_list *list, char *name)
1401 trim_string(list->file_path,"./","\n");
1402 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1411 /****************************************************************************
1412 Set the file selection mask.
1413 ****************************************************************************/
1415 static int cmd_select(void)
1417 pstrcpy(fileselection,"");
1418 next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1423 /****************************************************************************
1424 Recursive file matching function act as find
1425 match must be always set to True when calling this function
1426 ****************************************************************************/
1428 static int file_find(struct file_list **list, const char *directory,
1429 const char *expression, BOOL match)
1431 SMB_STRUCT_DIR *dir;
1432 struct file_list *entry;
1433 struct stat statbuf;
1439 dir = sys_opendir(directory);
1443 while ((dname = readdirname(dir))) {
1444 if (!strcmp("..", dname))
1446 if (!strcmp(".", dname))
1449 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1454 if (!match || !gen_fnmatch(expression, dname)) {
1456 ret = stat(path, &statbuf);
1458 if (S_ISDIR(statbuf.st_mode)) {
1460 ret = file_find(list, path, expression, False);
1463 d_printf("file_find: cannot stat file %s\n", path);
1472 entry = SMB_MALLOC_P(struct file_list);
1474 d_printf("Out of memory in file_find\n");
1478 entry->file_path = path;
1479 entry->isdir = isdir;
1480 DLIST_ADD(*list, entry);
1490 /****************************************************************************
1492 ****************************************************************************/
1494 static int cmd_mput(void)
1499 while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1501 struct file_list *temp_list;
1502 char *quest, *lname, *rname;
1506 ret = file_find(&file_list, ".", p, True);
1508 free_file_list(file_list);
1516 for (temp_list = file_list; temp_list;
1517 temp_list = temp_list->next) {
1520 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1522 trim_string(lname, "./", "/");
1524 /* check if it's a directory */
1525 if (temp_list->isdir) {
1526 /* if (!recurse) continue; */
1529 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1530 if (prompt && !yesno(quest)) { /* No */
1531 /* Skip the directory */
1532 lname[strlen(lname)-1] = '/';
1533 if (!seek_list(temp_list, lname))
1537 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1539 if (!cli_chkpath(cli, rname) &&
1541 DEBUG (0, ("Unable to make dir, skipping..."));
1542 /* Skip the directory */
1543 lname[strlen(lname)-1] = '/';
1544 if (!seek_list(temp_list, lname))
1551 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1552 if (prompt && !yesno(quest)) /* No */
1557 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1562 do_put(rname, lname, False);
1564 free_file_list(file_list);
1573 /****************************************************************************
1575 ****************************************************************************/
1577 static int do_cancel(int job)
1579 if (cli_printjob_del(cli, job)) {
1580 d_printf("Job %d cancelled\n",job);
1583 d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
1588 /****************************************************************************
1590 ****************************************************************************/
1592 static int cmd_cancel(void)
1597 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1598 d_printf("cancel <jobid> ...\n");
1604 } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1609 /****************************************************************************
1611 ****************************************************************************/
1613 static int cmd_print(void)
1619 if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1620 d_printf("print <filename>\n");
1624 pstrcpy(rname,lname);
1625 p = strrchr_m(rname,'/');
1627 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)sys_getpid());
1630 if (strequal(lname,"-")) {
1631 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)sys_getpid());
1634 return do_put(rname, lname, False);
1637 /****************************************************************************
1638 Show a print queue entry.
1639 ****************************************************************************/
1641 static void queue_fn(struct print_job_info *p)
1643 d_printf("%-6d %-9d %s\n", (int)p->id, (int)p->size, p->name);
1646 /****************************************************************************
1648 ****************************************************************************/
1650 static int cmd_queue(void)
1652 cli_print_queue(cli, queue_fn);
1657 /****************************************************************************
1659 ****************************************************************************/
1661 static void do_del(file_info *finfo)
1665 pstr_sprintf( mask, "%s%c%s", finfo->dir, CLI_DIRSEP_CHAR, finfo->name );
1667 if (finfo->mode & aDIR)
1670 if (!cli_unlink(finfo->cli, mask)) {
1671 d_printf("%s deleting remote file %s\n",cli_errstr(finfo->cli),mask);
1675 /****************************************************************************
1677 ****************************************************************************/
1679 static int cmd_del(void)
1683 uint16 attribute = aSYSTEM | aHIDDEN;
1688 pstrcpy(mask,cur_dir);
1690 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1691 d_printf("del <filename>\n");
1696 do_list(mask, attribute,do_del,False,False);
1701 /****************************************************************************
1702 Wildcard delete some files.
1703 ****************************************************************************/
1705 static int cmd_wdel(void)
1710 struct cli_state *targetcli;
1713 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1714 d_printf("wdel 0x<attrib> <wcard>\n");
1718 attribute = (uint16)strtol(buf, (char **)NULL, 16);
1720 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1721 d_printf("wdel 0x<attrib> <wcard>\n");
1725 pstrcpy(mask,cur_dir);
1728 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1729 d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
1733 if (!cli_unlink_full(targetcli, targetname, attribute)) {
1734 d_printf("%s deleting remote files %s\n",cli_errstr(targetcli),targetname);
1739 /****************************************************************************
1740 ****************************************************************************/
1742 static int cmd_open(void)
1746 struct cli_state *targetcli;
1750 pstrcpy(mask,cur_dir);
1752 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1753 d_printf("open <filename>\n");
1758 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1759 d_printf("open %s: %s\n", mask, cli_errstr(cli));
1763 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA|FILE_WRITE_DATA);
1765 fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA);
1767 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
1769 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1772 d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
1778 /****************************************************************************
1779 ****************************************************************************/
1781 static int cmd_posix_open(void)
1785 struct cli_state *targetcli;
1790 pstrcpy(mask,cur_dir);
1792 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1793 d_printf("posix_open <filename> 0<mode>\n");
1797 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1798 d_printf("posix_open <filename> 0<mode>\n");
1801 mode = (mode_t)strtol(buf, (char **)NULL, 8);
1803 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1804 d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
1808 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDWR, mode);
1810 fnum = cli_posix_open(targetcli, targetname, O_CREAT|O_RDONLY, mode);
1812 d_printf("posix_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("posix_open file %s: for read/write fnum %d\n", targetname, fnum);
1823 static int cmd_posix_mkdir(void)
1827 struct cli_state *targetcli;
1832 pstrcpy(mask,cur_dir);
1834 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1835 d_printf("posix_mkdir <filename> 0<mode>\n");
1839 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1840 d_printf("posix_mkdir <filename> 0<mode>\n");
1843 mode = (mode_t)strtol(buf, (char **)NULL, 8);
1845 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1846 d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
1850 fnum = cli_posix_mkdir(targetcli, targetname, mode);
1852 d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
1854 d_printf("posix_mkdir created directory %s\n", targetname);
1860 static int cmd_posix_unlink(void)
1864 struct cli_state *targetcli;
1867 pstrcpy(mask,cur_dir);
1869 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1870 d_printf("posix_unlink <filename>\n");
1875 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname )) {
1876 d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
1880 if (!cli_posix_unlink(targetcli, targetname)) {
1881 d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
1883 d_printf("posix_unlink deleted file %s\n", targetname);
1889 static int cmd_posix_rmdir(void)
1893 struct cli_state *targetcli;
1896 pstrcpy(mask,cur_dir);
1898 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1899 d_printf("posix_rmdir <filename>\n");
1904 if (!cli_resolve_path( "", cli, mask, &targetcli, targetname)) {
1905 d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
1909 if (!cli_posix_rmdir(targetcli, targetname)) {
1910 d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
1912 d_printf("posix_rmdir deleted directory %s\n", targetname);
1918 static int cmd_close(void)
1923 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1924 d_printf("close <fnum>\n");
1929 /* We really should use the targetcli here.... */
1930 if (!cli_close(cli, fnum)) {
1931 d_printf("close %d: %s\n", fnum, cli_errstr(cli));
1937 static int cmd_posix(void)
1939 uint16 major, minor;
1940 uint32 caplow, caphigh;
1943 if (!SERVER_HAS_UNIX_CIFS(cli)) {
1944 d_printf("Server doesn't support UNIX CIFS extensions.\n");
1948 if (!cli_unix_extensions_version(cli, &major, &minor, &caplow, &caphigh)) {
1949 d_printf("Can't get UNIX CIFS extensions version from server.\n");
1953 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major, (unsigned int)minor);
1956 if (caplow & CIFS_UNIX_FCNTL_LOCKS_CAP) {
1957 pstrcat(caps, "locks ");
1959 if (caplow & CIFS_UNIX_POSIX_ACLS_CAP) {
1960 pstrcat(caps, "acls ");
1962 if (caplow & CIFS_UNIX_XATTTR_CAP) {
1963 pstrcat(caps, "eas ");
1965 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
1966 pstrcat(caps, "pathnames ");
1968 if (caplow & CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP) {
1969 pstrcat(caps, "posix_path_operations ");
1972 if (strlen(caps) > 0 && caps[strlen(caps)-1] == ' ') {
1973 caps[strlen(caps)-1] = '\0';
1976 if (!cli_set_unix_extensions_capabilities(cli, major, minor, caplow, caphigh)) {
1977 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n", cli_errstr(cli));
1981 d_printf("Selecting server supported CIFS capabilities %s\n", caps);
1983 if (caplow & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
1984 CLI_DIRSEP_CHAR = '/';
1985 *CLI_DIRSEP_STR = '/';
1986 pstrcpy(cur_dir, CLI_DIRSEP_STR);
1993 static int cmd_lock(void)
1996 SMB_BIG_UINT start, len;
1997 enum brl_type lock_type;
2000 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2001 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2006 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2007 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2011 if (*buf == 'r' || *buf == 'R') {
2012 lock_type = READ_LOCK;
2013 } else if (*buf == 'w' || *buf == 'W') {
2014 lock_type = WRITE_LOCK;
2016 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2020 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2021 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2025 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2027 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2028 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2032 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2034 if (!cli_posix_lock(cli, fnum, start, len, True, lock_type)) {
2035 d_printf("lock failed %d: %s\n", fnum, cli_errstr(cli));
2041 static int cmd_unlock(void)
2044 SMB_BIG_UINT start, len;
2047 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2048 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2053 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2054 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2058 start = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2060 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2061 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2065 len = (SMB_BIG_UINT)strtol(buf, (char **)NULL, 16);
2067 if (!cli_posix_unlock(cli, fnum, start, len)) {
2068 d_printf("unlock failed %d: %s\n", fnum, cli_errstr(cli));
2075 /****************************************************************************
2077 ****************************************************************************/
2079 static int cmd_rmdir(void)
2083 struct cli_state *targetcli;
2086 pstrcpy(mask,cur_dir);
2088 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2089 d_printf("rmdir <dirname>\n");
2094 if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
2095 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
2099 if (!cli_rmdir(targetcli, targetname)) {
2100 d_printf("%s removing remote directory file %s\n",
2101 cli_errstr(targetcli),mask);
2107 /****************************************************************************
2109 ****************************************************************************/
2111 static int cmd_link(void)
2113 pstring oldname,newname;
2115 struct cli_state *targetcli;
2118 pstrcpy(oldname,cur_dir);
2119 pstrcpy(newname,cur_dir);
2121 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2122 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2123 d_printf("link <oldname> <newname>\n");
2127 pstrcat(oldname,buf);
2128 pstrcat(newname,buf2);
2130 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
2131 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2135 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2136 d_printf("Server doesn't support UNIX CIFS calls.\n");
2140 if (!cli_unix_hardlink(targetcli, targetname, newname)) {
2141 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli), newname, oldname);
2148 /****************************************************************************
2150 ****************************************************************************/
2152 static int cmd_symlink(void)
2154 pstring oldname,newname;
2156 struct cli_state *targetcli;
2159 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2160 d_printf("Server doesn't support UNIX CIFS calls.\n");
2164 pstrcpy(newname,cur_dir);
2166 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2167 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2168 d_printf("symlink <oldname> <newname>\n");
2172 pstrcpy(oldname,buf);
2173 pstrcat(newname,buf2);
2175 if ( !cli_resolve_path( "", cli, oldname, &targetcli, targetname ) ) {
2176 d_printf("link %s: %s\n", oldname, cli_errstr(cli));
2180 if (!cli_unix_symlink(targetcli, targetname, newname)) {
2181 d_printf("%s symlinking files (%s -> %s)\n",
2182 cli_errstr(targetcli), newname, targetname);
2189 /****************************************************************************
2191 ****************************************************************************/
2193 static int cmd_chmod(void)
2198 struct cli_state *targetcli;
2201 pstrcpy(src,cur_dir);
2203 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2204 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2205 d_printf("chmod mode file\n");
2209 mode = (mode_t)strtol(buf, NULL, 8);
2212 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2213 d_printf("chmod %s: %s\n", src, cli_errstr(cli));
2217 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2218 d_printf("Server doesn't support UNIX CIFS calls.\n");
2222 if (!cli_unix_chmod(targetcli, targetname, mode)) {
2223 d_printf("%s chmod file %s 0%o\n",
2224 cli_errstr(targetcli), src, (unsigned int)mode);
2231 static const char *filetype_to_str(mode_t mode)
2233 if (S_ISREG(mode)) {
2234 return "regular file";
2235 } else if (S_ISDIR(mode)) {
2239 if (S_ISCHR(mode)) {
2240 return "character device";
2244 if (S_ISBLK(mode)) {
2245 return "block device";
2249 if (S_ISFIFO(mode)) {
2254 if (S_ISLNK(mode)) {
2255 return "symbolic link";
2259 if (S_ISSOCK(mode)) {
2266 static char rwx_to_str(mode_t m, mode_t bt, char ret)
2275 static char *unix_mode_to_str(char *s, mode_t m)
2278 const char *str = filetype_to_str(m);
2294 *p++ = str[1] == 'y' ? 'l' : 's';
2301 *p++ = rwx_to_str(m, S_IRUSR, 'r');
2302 *p++ = rwx_to_str(m, S_IWUSR, 'w');
2303 *p++ = rwx_to_str(m, S_IXUSR, 'x');
2304 *p++ = rwx_to_str(m, S_IRGRP, 'r');
2305 *p++ = rwx_to_str(m, S_IWGRP, 'w');
2306 *p++ = rwx_to_str(m, S_IXGRP, 'x');
2307 *p++ = rwx_to_str(m, S_IROTH, 'r');
2308 *p++ = rwx_to_str(m, S_IWOTH, 'w');
2309 *p++ = rwx_to_str(m, S_IXOTH, 'x');
2314 /****************************************************************************
2315 Utility function for UNIX getfacl.
2316 ****************************************************************************/
2318 static char *perms_to_string(fstring permstr, unsigned char perms)
2320 fstrcpy(permstr, "---");
2321 if (perms & SMB_POSIX_ACL_READ) {
2324 if (perms & SMB_POSIX_ACL_WRITE) {
2327 if (perms & SMB_POSIX_ACL_EXECUTE) {
2333 /****************************************************************************
2335 ****************************************************************************/
2337 static int cmd_getfacl(void)
2340 uint16 major, minor;
2341 uint32 caplow, caphigh;
2342 char *retbuf = NULL;
2344 SMB_STRUCT_STAT sbuf;
2345 uint16 num_file_acls = 0;
2346 uint16 num_dir_acls = 0;
2348 struct cli_state *targetcli;
2351 pstrcpy(src,cur_dir);
2353 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2354 d_printf("stat file\n");
2360 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2361 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2365 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2366 d_printf("Server doesn't support UNIX CIFS calls.\n");
2370 if (!cli_unix_extensions_version(targetcli, &major, &minor, &caplow, &caphigh)) {
2371 d_printf("Can't get UNIX CIFS version from server.\n");
2375 if (!(caplow & CIFS_UNIX_POSIX_ACLS_CAP)) {
2376 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
2380 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2381 d_printf("%s getfacl doing a stat on file %s\n",
2382 cli_errstr(targetcli), src);
2386 if (!cli_unix_getfacl(targetcli, targetname, &rb_size, &retbuf)) {
2387 d_printf("%s getfacl file %s\n",
2388 cli_errstr(targetcli), src);
2392 /* ToDo : Print out the ACL values. */
2393 if (SVAL(retbuf,0) != SMB_POSIX_ACL_VERSION || rb_size < 6) {
2394 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2395 src, (unsigned int)CVAL(retbuf,0) );
2400 num_file_acls = SVAL(retbuf,2);
2401 num_dir_acls = SVAL(retbuf,4);
2402 if (rb_size != SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)) {
2403 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2405 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE*(num_file_acls+num_dir_acls)),
2406 (unsigned int)rb_size);
2412 d_printf("# file: %s\n", src);
2413 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf.st_uid, (unsigned int)sbuf.st_gid);
2415 if (num_file_acls == 0 && num_dir_acls == 0) {
2416 d_printf("No acls found.\n");
2419 for (i = 0; i < num_file_acls; i++) {
2422 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
2423 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2426 case SMB_POSIX_ACL_USER_OBJ:
2429 case SMB_POSIX_ACL_USER:
2430 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2431 d_printf("user:%u:", uorg);
2433 case SMB_POSIX_ACL_GROUP_OBJ:
2434 d_printf("group::");
2436 case SMB_POSIX_ACL_GROUP:
2437 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2438 d_printf("group:%u", uorg);
2440 case SMB_POSIX_ACL_MASK:
2443 case SMB_POSIX_ACL_OTHER:
2444 d_printf("other::");
2447 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2448 src, (unsigned int)tagtype );
2453 d_printf("%s\n", perms_to_string(permstring, perms));
2456 for (i = 0; i < num_dir_acls; i++) {
2459 unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
2460 unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
2463 case SMB_POSIX_ACL_USER_OBJ:
2464 d_printf("default:user::");
2466 case SMB_POSIX_ACL_USER:
2467 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2468 d_printf("default:user:%u:", uorg);
2470 case SMB_POSIX_ACL_GROUP_OBJ:
2471 d_printf("default:group::");
2473 case SMB_POSIX_ACL_GROUP:
2474 uorg = IVAL(retbuf,SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+2);
2475 d_printf("default:group:%u", uorg);
2477 case SMB_POSIX_ACL_MASK:
2478 d_printf("default:mask::");
2480 case SMB_POSIX_ACL_OTHER:
2481 d_printf("default:other::");
2484 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2485 src, (unsigned int)tagtype );
2490 d_printf("%s\n", perms_to_string(permstring, perms));
2497 /****************************************************************************
2499 ****************************************************************************/
2501 static int cmd_stat(void)
2505 SMB_STRUCT_STAT sbuf;
2506 struct cli_state *targetcli;
2510 if (!SERVER_HAS_UNIX_CIFS(cli)) {
2511 d_printf("Server doesn't support UNIX CIFS calls.\n");
2515 pstrcpy(src,cur_dir);
2517 if (!next_token_nr(NULL,name,NULL,sizeof(name))) {
2518 d_printf("stat file\n");
2525 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2526 d_printf("stat %s: %s\n", src, cli_errstr(cli));
2530 if (!cli_unix_stat(targetcli, targetname, &sbuf)) {
2531 d_printf("%s stat file %s\n",
2532 cli_errstr(targetcli), src);
2536 /* Print out the stat values. */
2537 d_printf("File: %s\n", src);
2538 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2539 (double)sbuf.st_size,
2540 (unsigned int)sbuf.st_blocks,
2541 filetype_to_str(sbuf.st_mode));
2543 #if defined(S_ISCHR) && defined(S_ISBLK)
2544 if (S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode)) {
2545 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2546 (double)sbuf.st_ino,
2547 (unsigned int)sbuf.st_nlink,
2548 unix_dev_major(sbuf.st_rdev),
2549 unix_dev_minor(sbuf.st_rdev));
2552 d_printf("Inode: %.0f\tLinks: %u\n",
2553 (double)sbuf.st_ino,
2554 (unsigned int)sbuf.st_nlink);
2556 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2557 ((int)sbuf.st_mode & 0777),
2558 unix_mode_to_str(mode_str, sbuf.st_mode),
2559 (unsigned int)sbuf.st_uid,
2560 (unsigned int)sbuf.st_gid);
2562 lt = localtime(&sbuf.st_atime);
2564 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2566 fstrcpy(mode_str, "unknown");
2568 d_printf("Access: %s\n", mode_str);
2570 lt = localtime(&sbuf.st_mtime);
2572 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2574 fstrcpy(mode_str, "unknown");
2576 d_printf("Modify: %s\n", mode_str);
2578 lt = localtime(&sbuf.st_ctime);
2580 strftime(mode_str, sizeof(mode_str), "%Y-%m-%d %T %z", lt);
2582 fstrcpy(mode_str, "unknown");
2584 d_printf("Change: %s\n", mode_str);
2590 /****************************************************************************
2592 ****************************************************************************/
2594 static int cmd_chown(void)
2599 pstring buf, buf2, buf3;
2600 struct cli_state *targetcli;
2603 pstrcpy(src,cur_dir);
2605 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2606 !next_token_nr(NULL,buf2,NULL, sizeof(buf2)) ||
2607 !next_token_nr(NULL,buf3,NULL, sizeof(buf3))) {
2608 d_printf("chown uid gid file\n");
2612 uid = (uid_t)atoi(buf);
2613 gid = (gid_t)atoi(buf2);
2616 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2617 d_printf("chown %s: %s\n", src, cli_errstr(cli));
2621 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2622 d_printf("Server doesn't support UNIX CIFS calls.\n");
2626 if (!cli_unix_chown(targetcli, targetname, uid, gid)) {
2627 d_printf("%s chown file %s uid=%d, gid=%d\n",
2628 cli_errstr(targetcli), src, (int)uid, (int)gid);
2635 /****************************************************************************
2637 ****************************************************************************/
2639 static int cmd_rename(void)
2643 struct cli_state *targetcli;
2647 pstrcpy(src,cur_dir);
2648 pstrcpy(dest,cur_dir);
2650 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2651 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2652 d_printf("rename <src> <dest>\n");
2659 if ( !cli_resolve_path( "", cli, src, &targetcli, targetsrc ) ) {
2660 d_printf("rename %s: %s\n", src, cli_errstr(cli));
2664 if ( !cli_resolve_path( "", cli, dest, &targetcli, targetdest ) ) {
2665 d_printf("rename %s: %s\n", dest, cli_errstr(cli));
2669 if (!cli_rename(targetcli, targetsrc, targetdest)) {
2670 d_printf("%s renaming files %s -> %s \n",
2671 cli_errstr(targetcli),
2680 /****************************************************************************
2681 Print the volume name.
2682 ****************************************************************************/
2684 static int cmd_volume(void)
2690 if (!cli_get_fs_volume_info(cli, volname, &serial_num, &create_date)) {
2691 d_printf("Errr %s getting volume info\n",cli_errstr(cli));
2695 d_printf("Volume: |%s| serial number 0x%x\n", volname, (unsigned int)serial_num);
2699 /****************************************************************************
2700 Hard link files using the NT call.
2701 ****************************************************************************/
2703 static int cmd_hardlink(void)
2707 struct cli_state *targetcli;
2710 pstrcpy(src,cur_dir);
2711 pstrcpy(dest,cur_dir);
2713 if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) ||
2714 !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
2715 d_printf("hardlink <src> <dest>\n");
2722 if ( !cli_resolve_path( "", cli, src, &targetcli, targetname ) ) {
2723 d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
2727 if (!SERVER_HAS_UNIX_CIFS(targetcli)) {
2728 d_printf("Server doesn't support UNIX CIFS calls.\n");
2732 if (!cli_nt_hardlink(targetcli, targetname, dest)) {
2733 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
2740 /****************************************************************************
2741 Toggle the prompt flag.
2742 ****************************************************************************/
2744 static int cmd_prompt(void)
2747 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2752 /****************************************************************************
2753 Set the newer than time.
2754 ****************************************************************************/
2756 static int cmd_newer(void)
2760 SMB_STRUCT_STAT sbuf;
2762 ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2763 if (ok && (sys_stat(buf,&sbuf) == 0)) {
2764 newer_than = sbuf.st_mtime;
2765 DEBUG(1,("Getting files newer than %s",
2766 time_to_asc(newer_than)));
2771 if (ok && newer_than == 0) {
2772 d_printf("Error setting newer-than time\n");
2779 /****************************************************************************
2780 Set the archive level.
2781 ****************************************************************************/
2783 static int cmd_archive(void)
2787 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2788 archive_level = atoi(buf);
2790 d_printf("Archive level is %d\n",archive_level);
2795 /****************************************************************************
2796 Toggle the lowercaseflag.
2797 ****************************************************************************/
2799 static int cmd_lowercase(void)
2801 lowercase = !lowercase;
2802 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2807 /****************************************************************************
2808 Toggle the case sensitive flag.
2809 ****************************************************************************/
2811 static int cmd_setcase(void)
2813 BOOL orig_case_sensitive = cli_set_case_sensitive(cli, False);
2815 cli_set_case_sensitive(cli, !orig_case_sensitive);
2816 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive ?
2822 /****************************************************************************
2823 Toggle the showacls flag.
2824 ****************************************************************************/
2826 static int cmd_showacls(void)
2828 showacls = !showacls;
2829 DEBUG(2,("showacls is now %s\n",showacls?"on":"off"));
2831 if (!ctx && showacls)
2832 ctx = talloc_init("smbclient:showacls");
2834 DEBUG( 0, ("cmd_showacls() out of memory. talloc_init() failed.\n"));
2841 /****************************************************************************
2842 Toggle the recurse flag.
2843 ****************************************************************************/
2845 static int cmd_recurse(void)
2848 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2853 /****************************************************************************
2854 Toggle the translate flag.
2855 ****************************************************************************/
2857 static int cmd_translate(void)
2859 translation = !translation;
2860 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2861 translation?"on":"off"));
2866 /****************************************************************************
2868 ****************************************************************************/
2870 static int cmd_lcd(void)
2875 if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2877 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2882 /****************************************************************************
2883 Get a file restarting at end of local file.
2884 ****************************************************************************/
2886 static int cmd_reget(void)
2889 pstring remote_name;
2892 pstrcpy(remote_name, cur_dir);
2894 p = remote_name + strlen(remote_name);
2896 if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2897 d_printf("reget <filename>\n");
2900 pstrcpy(local_name, p);
2901 clean_name(remote_name);
2903 next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2905 return do_get(remote_name, local_name, True);
2908 /****************************************************************************
2909 Put a file restarting at end of local file.
2910 ****************************************************************************/
2912 static int cmd_reput(void)
2915 pstring remote_name;
2920 pstrcpy(remote_name, cur_dir);
2922 if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2923 d_printf("reput <filename>\n");
2926 pstrcpy(local_name, p);
2928 if (!file_exist(local_name, &st)) {
2929 d_printf("%s does not exist\n", local_name);
2933 if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2934 pstrcat(remote_name, p);
2936 pstrcat(remote_name, local_name);
2938 clean_name(remote_name);
2940 return do_put(remote_name, local_name, True);
2943 /****************************************************************************
2945 ****************************************************************************/
2947 static void browse_fn(const char *name, uint32 m,
2948 const char *comment, void *state)
2956 case STYPE_DISKTREE:
2957 fstrcpy(typestr,"Disk"); break;
2959 fstrcpy(typestr,"Printer"); break;
2961 fstrcpy(typestr,"Device"); break;
2963 fstrcpy(typestr,"IPC"); break;
2965 /* FIXME: If the remote machine returns non-ascii characters
2966 in any of these fields, they can corrupt the output. We
2967 should remove them. */
2969 d_printf("\t%-15s %-10.10s%s\n",
2970 name,typestr,comment);
2972 d_printf ("%s|%s|%s\n",typestr,name,comment);
2976 static BOOL browse_host_rpc(BOOL sort)
2979 struct rpc_pipe_client *pipe_hnd;
2980 TALLOC_CTX *mem_ctx;
2983 SRV_SHARE_INFO_CTR ctr;
2986 mem_ctx = talloc_new(NULL);
2987 if (mem_ctx == NULL) {
2988 DEBUG(0, ("talloc_new failed\n"));
2992 init_enum_hnd(&enum_hnd, 0);
2994 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
2996 if (pipe_hnd == NULL) {
2997 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
2998 nt_errstr(status)));
2999 TALLOC_FREE(mem_ctx);
3003 werr = rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, 1, &ctr,
3004 0xffffffff, &enum_hnd);
3006 if (!W_ERROR_IS_OK(werr)) {
3007 TALLOC_FREE(mem_ctx);
3008 cli_rpc_pipe_close(pipe_hnd);
3012 for (i=0; i<ctr.num_entries; i++) {
3013 SRV_SHARE_INFO_1 *info = &ctr.share.info1[i];
3014 char *name, *comment;
3015 name = rpcstr_pull_unistr2_talloc(
3016 mem_ctx, &info->info_1_str.uni_netname);
3017 comment = rpcstr_pull_unistr2_talloc(
3018 mem_ctx, &info->info_1_str.uni_remark);
3019 browse_fn(name, info->info_1.type, comment, NULL);
3022 TALLOC_FREE(mem_ctx);
3023 cli_rpc_pipe_close(pipe_hnd);
3027 /****************************************************************************
3028 Try and browse available connections on a host.
3029 ****************************************************************************/
3031 static BOOL browse_host(BOOL sort)
3035 d_printf("\n\tSharename Type Comment\n");
3036 d_printf("\t--------- ---- -------\n");
3039 if (browse_host_rpc(sort)) {
3043 if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
3044 d_printf("Error returning browse list: %s\n", cli_errstr(cli));
3049 /****************************************************************************
3051 ****************************************************************************/
3053 static void server_fn(const char *name, uint32 m,
3054 const char *comment, void *state)
3058 d_printf("\t%-16s %s\n", name, comment);
3060 d_printf("%s|%s|%s\n",(char *)state, name, comment);
3064 /****************************************************************************
3065 Try and browse available connections on a host.
3066 ****************************************************************************/
3068 static BOOL list_servers(const char *wk_grp)
3072 if (!cli->server_domain)
3076 d_printf("\n\tServer Comment\n");
3077 d_printf("\t--------- -------\n");
3079 fstrcpy( state, "Server" );
3080 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn,
3084 d_printf("\n\tWorkgroup Master\n");
3085 d_printf("\t--------- -------\n");
3088 fstrcpy( state, "Workgroup" );
3089 cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
3094 /****************************************************************************
3095 Print or set current VUID
3096 ****************************************************************************/
3098 static int cmd_vuid(void)
3102 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3103 d_printf("Current VUID is %d\n", cli->vuid);
3107 cli->vuid = atoi(buf);
3111 /****************************************************************************
3112 Setup a new VUID, by issuing a session setup
3113 ****************************************************************************/
3115 static int cmd_logon(void)
3117 pstring l_username, l_password;
3120 if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3121 d_printf("logon <username> [<password>]\n");
3125 pstrcpy(l_username, buf);
3127 if (!next_token_nr(NULL,buf2,NULL,sizeof(buf)))
3129 char *pass = getpass("Password: ");
3131 pstrcpy(l_password, pass);
3134 pstrcpy(l_password, buf2);
3136 if (!NT_STATUS_IS_OK(cli_session_setup(cli, l_username,
3137 l_password, strlen(l_password),
3138 l_password, strlen(l_password),
3140 d_printf("session setup failed: %s\n", cli_errstr(cli));
3144 d_printf("Current VUID is %d\n", cli->vuid);
3149 /****************************************************************************
3150 list active connections
3151 ****************************************************************************/
3153 static int cmd_list_connect(void)
3160 /****************************************************************************
3161 display the current active client connection
3162 ****************************************************************************/
3164 static int cmd_show_connect( void )
3166 struct cli_state *targetcli;
3169 if ( !cli_resolve_path( "", cli, cur_dir, &targetcli, targetpath ) ) {
3170 d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
3174 d_printf("//%s/%s\n", targetcli->desthost, targetcli->share);
3178 /* Some constants for completing filename arguments */
3180 #define COMPL_NONE 0 /* No completions */
3181 #define COMPL_REMOTE 1 /* Complete remote filename */
3182 #define COMPL_LOCAL 2 /* Complete local filename */
3184 /* This defines the commands supported by this client.
3185 * NOTE: The "!" must be the last one in the list because it's fn pointer
3186 * field is NULL, and NULL in that field is used in process_tok()
3187 * (below) to indicate the end of the list. crh
3193 const char *description;
3194 char compl_args[2]; /* Completion argument info */
3196 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3197 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
3198 {"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}},
3199 {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
3200 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
3201 {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
3202 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
3203 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
3204 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
3205 {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
3206 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3207 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3208 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3209 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3210 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
3211 {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
3212 {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3213 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
3214 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
3215 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
3216 {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
3217 {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3218 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
3219 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
3220 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
3221 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3222 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
3223 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
3224 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
3225 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
3226 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
3227 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
3228 {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
3229 {"posix_open",cmd_posix_open,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3230 {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3231 {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3232 {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
3233 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
3234 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
3235 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
3236 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
3237 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3238 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
3239 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
3240 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3241 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
3242 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
3243 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
3244 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
3245 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3246 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
3247 {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
3248 {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
3249 {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
3250 {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
3251 {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
3252 {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
3253 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
3254 {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
3255 {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
3256 {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}},
3257 {"wdel",cmd_wdel,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
3258 {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}},
3259 {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}},
3260 {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},
3262 /* Yes, this must be here, see crh's comment above. */
3263 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
3264 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
3267 /*******************************************************************
3268 Lookup a command string in the list of commands, including
3270 ******************************************************************/
3272 static int process_tok(pstring tok)
3274 int i = 0, matches = 0;
3276 int tok_len = strlen(tok);
3278 while (commands[i].fn != NULL) {
3279 if (strequal(commands[i].name,tok)) {
3283 } else if (strnequal(commands[i].name, tok, tok_len)) {
3292 else if (matches == 1)
3298 /****************************************************************************
3300 ****************************************************************************/
3302 static int cmd_help(void)
3307 if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
3308 if ((i = process_tok(buf)) >= 0)
3309 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
3311 while (commands[i].description) {
3312 for (j=0; commands[i].description && (j<5); j++) {
3313 d_printf("%-15s",commands[i].name);
3322 /****************************************************************************
3323 Process a -c command string.
3324 ****************************************************************************/
3326 static int process_command_string(char *cmd)
3332 /* establish the connection if not already */
3335 cli = cli_cm_open(desthost, service, True);
3340 while (cmd[0] != '\0') {
3345 if ((p = strchr_m(cmd, ';')) == 0) {
3346 strncpy(line, cmd, 999);
3352 strncpy(line, cmd, p - cmd);
3353 line[p - cmd] = '\0';
3357 /* and get the first part of the command */
3359 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3361 if ((i = process_tok(tok)) >= 0) {
3362 rc = commands[i].fn();
3363 } else if (i == -2) {
3364 d_printf("%s: command abbreviation ambiguous\n",tok);
3366 d_printf("%s: command not found\n",tok);
3373 #define MAX_COMPLETIONS 100
3381 } completion_remote_t;
3383 static void completion_remote_filter(const char *mnt, file_info *f, const char *mask, void *state)
3385 completion_remote_t *info = (completion_remote_t *)state;
3387 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
3388 if ((info->dirmask[0] == 0) && !(f->mode & aDIR))
3389 info->matches[info->count] = SMB_STRDUP(f->name);
3393 if (info->dirmask[0] != 0)
3394 pstrcpy(tmp, info->dirmask);
3397 pstrcat(tmp, f->name);
3400 info->matches[info->count] = SMB_STRDUP(tmp);
3402 if (info->matches[info->count] == NULL)
3405 smb_readline_ca_char(0);
3407 if (info->count == 1)
3408 info->samelen = strlen(info->matches[info->count]);
3410 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
3416 static char **remote_completion(const char *text, int len)
3420 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
3422 /* can't have non-static intialisation on Sun CC, so do it
3428 if (len >= MIN(PATH_MAX,sizeof(pstring))) {
3432 info.matches = SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS);
3433 if (!info.matches) {
3438 * We're leaving matches[0] free to fill it later with the text to
3439 * display: Either the one single match or the longest common subset
3442 info.matches[0] = NULL;
3445 for (i = len-1; i >= 0; i--) {
3446 if ((text[i] == '/') || (text[i] == CLI_DIRSEP_CHAR)) {
3451 info.text = text+i+1;
3452 info.samelen = info.len = len-i-1;
3455 strncpy(info.dirmask, text, i+1);
3456 info.dirmask[i+1] = 0;
3457 pstr_sprintf(dirmask, "%s%*s*", cur_dir, i-1, text);
3459 pstr_sprintf(dirmask, "%s*", cur_dir);
3462 if (cli_list(cli, dirmask, aDIR | aSYSTEM | aHIDDEN, completion_remote_filter, &info) < 0)
3465 if (info.count == 1) {
3468 * No matches at all, NULL indicates there is nothing
3471 SAFE_FREE(info.matches[0]);
3472 SAFE_FREE(info.matches);
3476 if (info.count == 2) {
3479 * Exactly one match in matches[1], indicate this is the one
3483 info.matches[0] = info.matches[1];
3484 info.matches[1] = NULL;
3486 return info.matches;
3490 * We got more than one possible match, set the result to the maximum
3494 info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
3495 info.matches[info.count] = NULL;
3496 return info.matches;
3499 for (i = 0; i < info.count; i++)
3500 free(info.matches[i]);
3505 static char **completion_fn(const char *text, int start, int end)
3507 smb_readline_ca_char(' ');
3510 const char *buf, *sp;
3514 buf = smb_readline_get_line_buffer();
3518 sp = strchr(buf, ' ');
3522 for (i = 0; commands[i].name; i++) {
3523 if ((strncmp(commands[i].name, buf, sp - buf) == 0) &&
3524 (commands[i].name[sp - buf] == 0)) {
3528 if (commands[i].name == NULL)
3534 if (sp == (buf + start))
3535 compl_type = commands[i].compl_args[0];
3537 compl_type = commands[i].compl_args[1];
3539 if (compl_type == COMPL_REMOTE)
3540 return remote_completion(text, end - start);
3541 else /* fall back to local filename completion */
3545 int i, len, samelen = 0, count=1;
3547 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
3554 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
3555 if (strncmp(text, commands[i].name, len) == 0) {
3556 matches[count] = SMB_STRDUP(commands[i].name);
3557 if (!matches[count])
3560 samelen = strlen(matches[count]);
3562 while (strncmp(matches[count], matches[count-1], samelen) != 0)
3569 case 0: /* should never happen */
3573 matches[0] = SMB_STRDUP(matches[1]);
3576 matches[0] = (char *)SMB_MALLOC(samelen+1);
3579 strncpy(matches[0], matches[1], samelen);
3580 matches[0][samelen] = 0;
3582 matches[count] = NULL;
3586 for (i = 0; i < count; i++)
3594 /****************************************************************************
3595 Make sure we swallow keepalives during idle time.
3596 ****************************************************************************/
3598 static void readline_callback(void)
3601 struct timeval timeout;
3602 static time_t last_t;
3618 FD_SET(cli->fd,&fds);
3621 timeout.tv_usec = 0;
3622 sys_select_intr(cli->fd+1,&fds,NULL,NULL,&timeout);
3624 /* We deliberately use receive_smb instead of
3625 client_receive_smb as we want to receive
3626 session keepalives and then drop them here.
3628 if (FD_ISSET(cli->fd,&fds)) {
3629 if (!receive_smb(cli->fd,cli->inbuf,0)) {
3630 DEBUG(0, ("Read from server failed, maybe it closed the "
3637 /* Ping the server to keep the connection alive using SMBecho. */
3639 unsigned char garbage[16];
3640 memset(garbage, 0xf0, sizeof(garbage));
3641 cli_echo(cli, garbage, sizeof(garbage));
3645 /****************************************************************************
3646 Process commands on stdin.
3647 ****************************************************************************/
3649 static int process_stdin(void)
3661 /* display a prompt */
3662 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
3663 cline = smb_readline(the_prompt, readline_callback, completion_fn);
3667 pstrcpy(line, cline);
3669 /* special case - first char is ! */
3675 /* and get the first part of the command */
3677 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
3679 if ((i = process_tok(tok)) >= 0) {
3680 rc = commands[i].fn();
3681 } else if (i == -2) {
3682 d_printf("%s: command abbreviation ambiguous\n",tok);
3684 d_printf("%s: command not found\n",tok);
3690 /****************************************************************************
3691 Process commands from the client.
3692 ****************************************************************************/
3694 static int process(char *base_directory)
3698 cli = cli_cm_open(desthost, service, True);
3703 if (*base_directory) {
3704 rc = do_cd(base_directory);
3712 rc = process_command_string(cmdstr);
3721 /****************************************************************************
3723 ****************************************************************************/
3725 static int do_host_query(char *query_host)
3727 cli = cli_cm_open(query_host, "IPC$", True);
3735 /* Workgroups simply don't make sense over anything
3736 else but port 139... */
3739 cli_cm_set_port( 139 );
3740 cli = cli_cm_open(query_host, "IPC$", True);
3744 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3748 list_servers(lp_workgroup());
3755 /****************************************************************************
3756 Handle a tar operation.
3757 ****************************************************************************/
3759 static int do_tar_op(char *base_directory)
3763 /* do we already have a connection? */
3765 cli = cli_cm_open(desthost, service, True);
3772 if (*base_directory) {
3773 ret = do_cd(base_directory);
3787 /****************************************************************************
3788 Handle a message operation.
3789 ****************************************************************************/
3791 static int do_message_op(void)
3794 struct nmb_name called, calling;
3795 fstring server_name;
3796 char name_type_hex[10];
3800 make_nmb_name(&calling, calling_name, 0x0);
3801 make_nmb_name(&called , desthost, name_type);
3803 fstrcpy(server_name, desthost);
3804 snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
3805 fstrcat(server_name, name_type_hex);
3811 /* we can only do messages over port 139 (to windows clients at least) */
3813 msg_port = port ? port : 139;
3815 if (!(cli=cli_initialise()) || (cli_set_port(cli, msg_port) != msg_port)) {
3816 d_printf("Connection to %s failed\n", desthost);
3820 status = cli_connect(cli, server_name, &ip);
3821 if (!NT_STATUS_IS_OK(status)) {
3822 d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
3826 if (!cli_session_request(cli, &calling, &called)) {
3827 d_printf("session request failed\n");
3839 /****************************************************************************
3841 ****************************************************************************/
3843 int main(int argc,char *argv[])
3845 pstring base_directory;
3849 BOOL message = False;
3851 static const char *new_name_resolve_order = NULL;
3855 fstring new_workgroup;
3856 BOOL tar_opt = False;
3857 BOOL service_opt = False;
3858 struct poptOption long_options[] = {
3861 { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3862 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3863 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3864 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3865 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3866 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3867 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
3868 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3869 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3870 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3871 { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
3872 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3873 { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
3875 POPT_COMMON_CONNECTION
3876 POPT_COMMON_CREDENTIALS
3883 pstrcpy(term_code, KANJI);
3889 *base_directory = 0;
3891 /* initialize the workgroup name so we can determine whether or
3892 not it was set by a command line option */
3894 set_global_myworkgroup( "" );
3895 set_global_myname( "" );
3897 /* set default debug level to 1 regardless of what smb.conf sets */
3898 setup_logging( "smbclient", True );
3899 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
3900 if ((dbf = x_fdup(x_stderr))) {
3901 x_setbuf( dbf, NULL );
3905 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
3906 poptSetOtherOptionHelp(pc, "service <password>");
3908 in_client = True; /* Make sure that we tell lp_load we are */
3910 while ((opt = poptGetNextOpt(pc)) != -1) {
3912 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
3913 /* I see no other way to keep things sane --SSS */
3914 if (tar_opt == True) {
3915 while (poptPeekArg(pc)) {
3921 /* if the service has not yet been specified lets see if it is available in the popt stack */
3922 if (!service_opt && poptPeekArg(pc)) {
3923 pstrcpy(service, poptGetArg(pc));
3927 /* if the service has already been retrieved then check if we have also a password */
3928 if (service_opt && (!cmdline_auth_info.got_pass) && poptPeekArg(pc)) {
3929 pstrcpy(cmdline_auth_info.password, poptGetArg(pc));
3930 cmdline_auth_info.got_pass = True;
3935 /* Messages are sent to NetBIOS name type 0x3
3936 * (Messenger Service). Make sure we default
3937 * to port 139 instead of port 445. srl,crh
3940 cli_cm_set_dest_name_type( name_type );
3941 pstrcpy(desthost,poptGetOptArg(pc));
3943 cli_cm_set_port( 139 );
3948 dest_ip = *interpret_addr2(poptGetOptArg(pc));
3949 if (is_zero_ip(dest_ip))
3953 cli_cm_set_dest_ip( dest_ip );
3961 display_set_stderr();
3965 pstrcpy(query_host, poptGetOptArg(pc));
3968 pstrcpy(term_code, poptGetOptArg(pc));
3971 max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
3974 /* We must use old option processing for this. Find the
3975 * position of the -T option in the raw argv[]. */
3978 for (i = 1; i < argc; i++) {
3979 if (strncmp("-T", argv[i],2)==0)
3983 if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
3984 poptPrintUsage(pc, stderr, 0);
3988 /* this must be the last option, mark we have parsed it so that we know we have */
3992 pstrcpy(base_directory,poptGetOptArg(pc));
4000 /* We may still have some leftovers after the last popt option has been called */
4001 if (tar_opt == True) {
4002 while (poptPeekArg(pc)) {
4008 /* if the service has not yet been specified lets see if it is available in the popt stack */
4009 if (!service_opt && poptPeekArg(pc)) {
4010 pstrcpy(service, poptGetArg(pc));
4014 /* if the service has already been retrieved then check if we have also a password */
4015 if (service_opt && (!cmdline_auth_info.got_pass) && poptPeekArg(pc)) {
4016 pstrcpy(cmdline_auth_info.password, poptGetArg(pc));
4017 cmdline_auth_info.got_pass = True;
4020 /* check for the -P option */
4023 cli_cm_set_port( port );
4026 * Don't load debug level from smb.conf. It should be
4027 * set by cmdline arg or remain default (0)
4029 AllowDebugChange = False;
4031 /* save the workgroup...
4033 FIXME!! do we need to do this for other options as well
4034 (or maybe a generic way to keep lp_load() from overwriting
4037 fstrcpy( new_workgroup, lp_workgroup() );
4038 pstrcpy( calling_name, global_myname() );
4040 if ( override_logfile )
4041 setup_logging( lp_logfile(), False );
4043 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
4044 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
4045 argv[0], dyn_CONFIGFILE);
4051 /* Convert any '/' characters in the service name to '\' characters */
4052 string_replace(service, '/','\\');
4053 if (count_chars(service,'\\') < 3) {
4054 d_printf("\n%s: Not enough '\\' characters in service\n",service);
4055 poptPrintUsage(pc, stderr, 0);
4058 /* Remove trailing slashes */
4059 len = strlen(service);
4060 while(len > 0 && service[len - 1] == '\\') {
4062 service[len] = '\0';
4066 if ( strlen(new_workgroup) != 0 )
4067 set_global_myworkgroup( new_workgroup );
4069 if ( strlen(calling_name) != 0 )
4070 set_global_myname( calling_name );
4072 pstrcpy( calling_name, global_myname() );
4076 if(new_name_resolve_order)
4077 lp_set_name_resolve_order(new_name_resolve_order);
4079 if (!tar_type && !*query_host && !*service && !message) {
4080 poptPrintUsage(pc, stderr, 0);
4084 poptFreeContext(pc);
4086 /* store the username an password for dfs support */
4088 cli_cm_set_credentials( &cmdline_auth_info );
4089 pstrcpy(username, cmdline_auth_info.username);
4091 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING));
4095 process_command_string(cmdstr);
4096 return do_tar_op(base_directory);
4100 char *qhost = query_host;
4103 while (*qhost == '\\' || *qhost == '/')
4106 if ((slash = strchr_m(qhost, '/'))
4107 || (slash = strchr_m(qhost, '\\'))) {
4111 if ((p=strchr_m(qhost, '#'))) {
4114 sscanf(p, "%x", &name_type);
4115 cli_cm_set_dest_name_type( name_type );
4118 return do_host_query(qhost);
4122 return do_message_op();
4125 if (process(base_directory)) {
4129 talloc_destroy( ctx);