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-2004
7 Copyright (C) James J Myers 2003 <myersjj@samba.org>
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 "dynconfig.h"
27 #include "lib/cmdline/popt_common.h"
28 #include "librpc/gen_ndr/ndr_srvsvc.h"
29 #include "librpc/gen_ndr/ndr_lsa.h"
30 #include "libcli/raw/libcliraw.h"
31 #include "system/time.h"
32 #include "system/dir.h"
33 #include "system/filesys.h"
34 #include "dlinklist.h"
40 struct smbcli_state *cli;
41 extern BOOL in_client;
43 pstring cur_dir = "\\";
44 static pstring cd_path = "";
45 static pstring service;
46 static pstring desthost;
47 static pstring username;
48 static pstring domain;
49 static pstring password;
50 static char *cmdstr = NULL;
52 static int io_bufsize = 64512;
54 static int name_type = 0x20;
56 static int process_tok(fstring tok);
57 static int cmd_help(const char **cmd_ptr);
59 /* 30 second timeout on most commands */
60 #define CLIENT_TIMEOUT (30*1000)
61 #define SHORT_TIMEOUT (5*1000)
63 /* value for unused fid field in trans2 secondary request */
64 #define FID_UNUSED (0xFFFF)
66 time_t newer_than = 0;
67 static int archive_level = 0;
69 static BOOL translation = False;
71 /* clitar bits insert */
74 extern BOOL tar_reset;
78 static BOOL prompt = True;
80 static int printmode = 1;
82 static BOOL recurse = False;
83 BOOL lowercase = False;
85 static const char *dest_ip;
87 #define SEPARATORS " \t\n\r"
89 static BOOL abort_mget = True;
91 static pstring fileselection = "";
94 uint64_t get_total_size = 0;
95 uint_t get_total_time_ms = 0;
96 static uint64_t put_total_size = 0;
97 static uint_t put_total_time_ms = 0;
100 static double dir_total;
104 /* some forward declarations */
105 static struct smbcli_state *do_connect(const char *server, const char *share);
108 /*******************************************************************
109 Reduce a file name, removing .. elements.
110 ********************************************************************/
111 void dos_clean_name(char *s)
115 DEBUG(3,("dos_clean_name [%s]\n",s));
117 /* remove any double slashes */
118 all_string_sub(s, "\\\\", "\\", 0);
120 while ((p = strstr(s,"\\..\\")) != NULL) {
126 if ((p=strrchr_m(s,'\\')) != NULL)
133 trim_string(s,NULL,"\\..");
135 all_string_sub(s, "\\.\\", "\\", 0);
138 /****************************************************************************
139 write to a local file with CR/LF->LF translation if appropriate. return the
140 number taken from the buffer. This may not equal the number written.
141 ****************************************************************************/
142 static int writefile(int f, const void *_b, int n)
144 const uint8_t *b = _b;
153 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
156 if (write(f, b, 1) != 1) {
166 /****************************************************************************
167 read from a file with LF->CR/LF translation if appropriate. return the
168 number read. read approx n bytes.
169 ****************************************************************************/
170 static int readfile(void *_b, int n, XFILE *f)
177 return x_fread(b,1,n,f);
180 while (i < (n - 1)) {
181 if ((c = x_getc(f)) == EOF) {
185 if (c == '\n') { /* change all LFs to CR/LF */
196 /****************************************************************************
198 ****************************************************************************/
199 static void send_message(void)
204 if (!smbcli_message_start(cli->tree, desthost, username, &grp_id)) {
205 d_printf("message start: %s\n", smbcli_errstr(cli->tree));
210 d_printf("Connected. Type your message, ending it with a Control-D\n");
212 while (!feof(stdin) && total_len < 1600) {
213 int maxlen = MIN(1600 - total_len,127);
220 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
226 if (!smbcli_message_text(cli->tree, msg, l, grp_id)) {
227 d_printf("SMBsendtxt failed (%s)\n",smbcli_errstr(cli->tree));
234 if (total_len >= 1600)
235 d_printf("the message was truncated to 1600 bytes\n");
237 d_printf("sent %d bytes\n",total_len);
239 if (!smbcli_message_end(cli->tree, grp_id)) {
240 d_printf("SMBsendend failed (%s)\n",smbcli_errstr(cli->tree));
247 /****************************************************************************
248 check the space on a device
249 ****************************************************************************/
250 static int do_dskattr(void)
252 int total, bsize, avail;
254 if (NT_STATUS_IS_ERR(smbcli_dskattr(cli->tree, &bsize, &total, &avail))) {
255 d_printf("Error in dskattr: %s\n",smbcli_errstr(cli->tree));
259 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
260 total, bsize, avail);
265 /****************************************************************************
267 ****************************************************************************/
268 static int cmd_pwd(const char **cmd_ptr)
270 d_printf("Current directory is %s",service);
271 d_printf("%s\n",cur_dir);
276 convert a string to dos format
278 static void dos_format(char *s)
280 string_replace(s, '/', '\\');
283 /****************************************************************************
284 change directory - inner section
285 ****************************************************************************/
286 static int do_cd(char *newdir)
294 /* Save the current directory in case the
295 new directory is invalid */
296 pstrcpy(saved_dir, cur_dir);
301 if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
302 pstrcat(cur_dir, "\\");
304 dos_clean_name(cur_dir);
305 pstrcpy(dname,cur_dir);
306 pstrcat(cur_dir,"\\");
307 dos_clean_name(cur_dir);
309 if (!strequal(cur_dir,"\\")) {
310 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, dname))) {
311 d_printf("cd %s: %s\n", dname, smbcli_errstr(cli->tree));
312 pstrcpy(cur_dir,saved_dir);
316 pstrcpy(cd_path,cur_dir);
321 /****************************************************************************
323 ****************************************************************************/
324 static int cmd_cd(const char **cmd_ptr)
329 if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
332 d_printf("Current directory is %s\n",cur_dir);
338 BOOL mask_match(struct smbcli_state *c, const char *string, char *pattern,
339 BOOL is_case_sensitive)
343 if (strcmp(string,"..") == 0)
345 if (strcmp(pattern,".") == 0)
348 if (is_case_sensitive)
349 return ms_fnmatch(pattern, string,
350 c->transport->negotiate.protocol) == 0;
352 fstrcpy(p2, pattern);
356 return ms_fnmatch(p2, s2, c->transport->negotiate.protocol) == 0;
361 /*******************************************************************
362 decide if a file should be operated on
363 ********************************************************************/
364 static BOOL do_this_one(struct clilist_file_info *finfo)
366 if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY) return(True);
368 if (*fileselection &&
369 !mask_match(cli, finfo->name,fileselection,False)) {
370 DEBUG(3,("mask_match %s failed\n", finfo->name));
374 if (newer_than && finfo->mtime < newer_than) {
375 DEBUG(3,("newer_than %s failed\n", finfo->name));
379 if ((archive_level==1 || archive_level==2) && !(finfo->attrib & FILE_ATTRIBUTE_ARCHIVE)) {
380 DEBUG(3,("archive %s failed\n", finfo->name));
387 /****************************************************************************
388 display info about a file
389 ****************************************************************************/
390 static void display_finfo(struct clilist_file_info *finfo)
392 if (do_this_one(finfo)) {
393 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
394 char *astr = attrib_string(NULL, finfo->attrib);
395 d_printf(" %-30s%7.7s %8.0f %s",
399 asctime(localtime(&t)));
400 dir_total += finfo->size;
406 /****************************************************************************
407 accumulate size of a file
408 ****************************************************************************/
409 static void do_du(struct clilist_file_info *finfo)
411 if (do_this_one(finfo)) {
412 dir_total += finfo->size;
416 static BOOL do_list_recurse;
417 static BOOL do_list_dirs;
418 static char *do_list_queue = 0;
419 static long do_list_queue_size = 0;
420 static long do_list_queue_start = 0;
421 static long do_list_queue_end = 0;
422 static void (*do_list_fn)(struct clilist_file_info *);
424 /****************************************************************************
425 functions for do_list_queue
426 ****************************************************************************/
429 * The do_list_queue is a NUL-separated list of strings stored in a
430 * char*. Since this is a FIFO, we keep track of the beginning and
431 * ending locations of the data in the queue. When we overflow, we
432 * double the size of the char*. When the start of the data passes
433 * the midpoint, we move everything back. This is logically more
434 * complex than a linked list, but easier from a memory management
435 * angle. In any memory error condition, do_list_queue is reset.
436 * Functions check to ensure that do_list_queue is non-NULL before
439 static void reset_do_list_queue(void)
441 SAFE_FREE(do_list_queue);
442 do_list_queue_size = 0;
443 do_list_queue_start = 0;
444 do_list_queue_end = 0;
447 static void init_do_list_queue(void)
449 reset_do_list_queue();
450 do_list_queue_size = 1024;
451 do_list_queue = malloc(do_list_queue_size);
452 if (do_list_queue == 0) {
453 d_printf("malloc fail for size %d\n",
454 (int)do_list_queue_size);
455 reset_do_list_queue();
457 memset(do_list_queue, 0, do_list_queue_size);
461 static void adjust_do_list_queue(void)
464 * If the starting point of the queue is more than half way through,
465 * move everything toward the beginning.
467 if (do_list_queue && (do_list_queue_start == do_list_queue_end))
469 DEBUG(4,("do_list_queue is empty\n"));
470 do_list_queue_start = do_list_queue_end = 0;
471 *do_list_queue = '\0';
473 else if (do_list_queue_start > (do_list_queue_size / 2))
475 DEBUG(4,("sliding do_list_queue backward\n"));
476 memmove(do_list_queue,
477 do_list_queue + do_list_queue_start,
478 do_list_queue_end - do_list_queue_start);
479 do_list_queue_end -= do_list_queue_start;
480 do_list_queue_start = 0;
485 static void add_to_do_list_queue(const char* entry)
488 long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
489 while (new_end > do_list_queue_size)
491 do_list_queue_size *= 2;
492 DEBUG(4,("enlarging do_list_queue to %d\n",
493 (int)do_list_queue_size));
494 dlq = realloc_p(do_list_queue, char, do_list_queue_size);
496 d_printf("failure enlarging do_list_queue to %d bytes\n",
497 (int)do_list_queue_size);
498 reset_do_list_queue();
503 memset(do_list_queue + do_list_queue_size / 2,
504 0, do_list_queue_size / 2);
509 safe_strcpy(do_list_queue + do_list_queue_end, entry,
510 do_list_queue_size - do_list_queue_end - 1);
511 do_list_queue_end = new_end;
512 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
513 entry, (int)do_list_queue_start, (int)do_list_queue_end));
517 static char *do_list_queue_head(void)
519 return do_list_queue + do_list_queue_start;
522 static void remove_do_list_queue_head(void)
524 if (do_list_queue_end > do_list_queue_start)
526 do_list_queue_start += strlen(do_list_queue_head()) + 1;
527 adjust_do_list_queue();
528 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
529 (int)do_list_queue_start, (int)do_list_queue_end));
533 static int do_list_queue_empty(void)
535 return (! (do_list_queue && *do_list_queue));
538 /****************************************************************************
540 ****************************************************************************/
541 static void do_list_helper(struct clilist_file_info *f, const char *mask, void *state)
543 if (f->attrib & FILE_ATTRIBUTE_DIRECTORY) {
544 if (do_list_dirs && do_this_one(f)) {
547 if (do_list_recurse &&
548 !strequal(f->name,".") &&
549 !strequal(f->name,"..")) {
553 pstrcpy(mask2, mask);
554 p = strrchr_m(mask2,'\\');
557 pstrcat(mask2, f->name);
558 pstrcat(mask2,"\\*");
559 add_to_do_list_queue(mask2);
564 if (do_this_one(f)) {
570 /****************************************************************************
571 a wrapper around smbcli_list that adds recursion
572 ****************************************************************************/
573 void do_list(const char *mask,uint16_t attribute,
574 void (*fn)(struct clilist_file_info *),BOOL rec, BOOL dirs)
576 static int in_do_list = 0;
578 if (in_do_list && rec)
580 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
586 do_list_recurse = rec;
592 init_do_list_queue();
593 add_to_do_list_queue(mask);
595 while (! do_list_queue_empty())
598 * Need to copy head so that it doesn't become
599 * invalid inside the call to smbcli_list. This
600 * would happen if the list were expanded
602 * Fix from E. Jay Berkenbilt (ejb@ql.org)
605 pstrcpy(head, do_list_queue_head());
606 smbcli_list(cli->tree, head, attribute, do_list_helper, NULL);
607 remove_do_list_queue_head();
608 if ((! do_list_queue_empty()) && (fn == display_finfo))
610 char* next_file = do_list_queue_head();
612 if ((strlen(next_file) >= 2) &&
613 (next_file[strlen(next_file) - 1] == '*') &&
614 (next_file[strlen(next_file) - 2] == '\\'))
616 save_ch = next_file +
617 strlen(next_file) - 2;
620 d_printf("\n%s\n",next_file);
630 if (smbcli_list(cli->tree, mask, attribute, do_list_helper, NULL) == -1)
632 d_printf("%s listing %s\n", smbcli_errstr(cli->tree), mask);
637 reset_do_list_queue();
640 /****************************************************************************
641 get a directory listing
642 ****************************************************************************/
643 static int cmd_dir(const char **cmd_ptr)
645 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
652 pstrcpy(mask,cur_dir);
653 if(mask[strlen(mask)-1]!='\\')
656 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
664 if (cli->tree->session->transport->negotiate.protocol <=
672 do_list(mask, attribute, display_finfo, recurse, True);
676 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
682 /****************************************************************************
683 get a directory listing
684 ****************************************************************************/
685 static int cmd_du(const char **cmd_ptr)
687 uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
694 pstrcpy(mask,cur_dir);
695 if(mask[strlen(mask)-1]!='\\')
698 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
708 do_list(mask, attribute, do_du, recurse, True);
712 d_printf("Total number of bytes: %.0f\n", dir_total);
718 /****************************************************************************
719 get a file from rname to lname
720 ****************************************************************************/
721 static int do_get(char *rname, const char *lname, BOOL reget)
723 int handle = 0, fnum;
724 BOOL newhandle = False;
726 struct timeval tp_start;
727 int read_size = io_bufsize;
734 GetTimeOfDay(&tp_start);
737 strlower(discard_const_p(char, lname));
740 fnum = smbcli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
743 d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
747 if(!strcmp(lname,"-")) {
748 handle = fileno(stdout);
751 handle = open(lname, O_WRONLY|O_CREAT, 0644);
753 start = lseek(handle, 0, SEEK_END);
755 d_printf("Error seeking local file\n");
760 handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
765 d_printf("Error opening local file %s\n",lname);
770 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum,
771 &attr, &size, NULL, NULL, NULL, NULL, NULL)) &&
772 NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum,
773 &attr, &size, NULL, NULL, NULL))) {
774 d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
778 DEBUG(2,("getting file %s of size %.0f as %s ",
779 rname, (double)size, lname));
781 if(!(data = (uint8_t *)malloc(read_size))) {
782 d_printf("malloc fail for size %d\n", read_size);
783 smbcli_close(cli->tree, fnum);
788 int n = smbcli_read(cli->tree, fnum, data, nread + start, read_size);
792 if (writefile(handle,data, n) != n) {
793 d_printf("Error writing local file\n");
801 if (nread + start < size) {
802 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
803 rname, (long)nread));
810 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
811 d_printf("Error %s closing remote file\n",smbcli_errstr(cli->tree));
819 if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
820 smbcli_setatr(cli->tree, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
824 struct timeval tp_end;
827 GetTimeOfDay(&tp_end);
829 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
830 (tp_end.tv_usec - tp_start.tv_usec)/1000;
831 get_total_time_ms += this_time;
832 get_total_size += nread;
834 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
835 nread / (1.024*this_time + 1.0e-4),
836 get_total_size / (1.024*get_total_time_ms)));
843 /****************************************************************************
845 ****************************************************************************/
846 static int cmd_get(const char **cmd_ptr)
852 pstrcpy(rname,cur_dir);
855 p = rname + strlen(rname);
857 if (!next_token(cmd_ptr,p,NULL,sizeof(rname)-strlen(rname))) {
858 d_printf("get <filename>\n");
862 dos_clean_name(rname);
864 next_token(cmd_ptr,lname,NULL,sizeof(lname));
866 return do_get(rname, lname, False);
869 /****************************************************************************
870 Put up a yes/no prompt.
871 ****************************************************************************/
872 static BOOL yesno(char *p)
877 if (!fgets(ans,sizeof(ans)-1,stdin))
880 if (*ans == 'y' || *ans == 'Y')
886 /****************************************************************************
887 do a mget operation on one file
888 ****************************************************************************/
889 static void do_mget(struct clilist_file_info *finfo)
893 pstring saved_curdir;
896 if (strequal(finfo->name,".") || strequal(finfo->name,".."))
900 d_printf("mget aborted\n");
904 if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)
905 slprintf(quest,sizeof(pstring)-1,
906 "Get directory %s? ",finfo->name);
908 slprintf(quest,sizeof(pstring)-1,
909 "Get file %s? ",finfo->name);
911 if (prompt && !yesno(quest)) return;
913 if (!(finfo->attrib & FILE_ATTRIBUTE_DIRECTORY)) {
914 pstrcpy(rname,cur_dir);
915 pstrcat(rname,finfo->name);
916 do_get(rname, finfo->name, False);
920 /* handle directories */
921 pstrcpy(saved_curdir,cur_dir);
923 pstrcat(cur_dir,finfo->name);
924 pstrcat(cur_dir,"\\");
926 string_replace(discard_const_p(char, finfo->name), '\\', '/');
928 strlower(discard_const_p(char, finfo->name));
931 if (!directory_exist(finfo->name,NULL) &&
932 mkdir(finfo->name,0777) != 0) {
933 d_printf("failed to create directory %s\n",finfo->name);
934 pstrcpy(cur_dir,saved_curdir);
938 if (chdir(finfo->name) != 0) {
939 d_printf("failed to chdir to directory %s\n",finfo->name);
940 pstrcpy(cur_dir,saved_curdir);
944 pstrcpy(mget_mask,cur_dir);
945 pstrcat(mget_mask,"*");
947 do_list(mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,False, True);
949 pstrcpy(cur_dir,saved_curdir);
953 /****************************************************************************
954 view the file using the pager
955 ****************************************************************************/
956 static int cmd_more(const char **cmd_ptr)
958 fstring rname,lname,pager_cmd;
963 fstrcpy(rname,cur_dir);
966 slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
967 fd = smb_mkstemp(lname);
969 d_printf("failed to create temporary file for more\n");
974 if (!next_token(cmd_ptr,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
975 d_printf("more <filename>\n");
979 dos_clean_name(rname);
981 rc = do_get(rname, lname, False);
983 pager=getenv("PAGER");
985 slprintf(pager_cmd,sizeof(pager_cmd)-1,
986 "%s %s",(pager? pager:PAGER), lname);
995 /****************************************************************************
997 ****************************************************************************/
998 static int cmd_mget(const char **cmd_ptr)
1000 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1008 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1012 while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1013 pstrcpy(mget_mask,cur_dir);
1014 if(mget_mask[strlen(mget_mask)-1]!='\\')
1015 pstrcat(mget_mask,"\\");
1018 pstrcpy(mget_mask,p);
1020 pstrcat(mget_mask,p);
1021 do_list(mget_mask, attribute,do_mget,False,True);
1025 pstrcpy(mget_mask,cur_dir);
1026 if(mget_mask[strlen(mget_mask)-1]!='\\')
1027 pstrcat(mget_mask,"\\");
1028 pstrcat(mget_mask,"*");
1029 do_list(mget_mask, attribute,do_mget,False,True);
1036 /****************************************************************************
1037 make a directory of name "name"
1038 ****************************************************************************/
1039 static NTSTATUS do_mkdir(char *name)
1043 if (NT_STATUS_IS_ERR(status = smbcli_mkdir(cli->tree, name))) {
1044 d_printf("%s making remote directory %s\n",
1045 smbcli_errstr(cli->tree),name);
1052 /****************************************************************************
1053 show 8.3 name of a file
1054 ****************************************************************************/
1055 static BOOL do_altname(char *name)
1057 const char *altname;
1058 if (!NT_STATUS_IS_OK(smbcli_qpathinfo_alt_name(cli->tree, name, &altname))) {
1059 d_printf("%s getting alt name for %s\n",
1060 smbcli_errstr(cli->tree),name);
1063 d_printf("%s\n", altname);
1069 /****************************************************************************
1071 ****************************************************************************/
1072 static int cmd_quit(const char **cmd_ptr)
1074 smbcli_shutdown(cli);
1081 /****************************************************************************
1083 ****************************************************************************/
1084 static int cmd_mkdir(const char **cmd_ptr)
1090 pstrcpy(mask,cur_dir);
1092 if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1094 d_printf("mkdir <dirname>\n");
1105 trim_string(ddir,".",NULL);
1106 p = strtok(ddir,"/\\");
1109 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, ddir2))) {
1112 pstrcat(ddir2,"\\");
1113 p = strtok(NULL,"/\\");
1123 /****************************************************************************
1125 ****************************************************************************/
1126 static int cmd_altname(const char **cmd_ptr)
1132 pstrcpy(name,cur_dir);
1134 if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1135 d_printf("altname <file>\n");
1146 /****************************************************************************
1148 ****************************************************************************/
1149 static int do_put(char *rname, char *lname, BOOL reput)
1155 uint8_t *buf = NULL;
1156 int maxwrite = io_bufsize;
1159 struct timeval tp_start;
1160 GetTimeOfDay(&tp_start);
1163 fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT, DENY_NONE);
1165 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL)) &&
1166 NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, NULL, &start, NULL, NULL, NULL))) {
1167 d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
1172 fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT|O_TRUNC,
1177 d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
1181 /* allow files to be piped into smbclient
1184 Note that in this case this function will exit(0) rather
1186 if (!strcmp(lname, "-")) {
1188 /* size of file is not known */
1190 f = x_fopen(lname,O_RDONLY, 0);
1192 if (x_tseek(f, start, SEEK_SET) == -1) {
1193 d_printf("Error seeking local file\n");
1200 d_printf("Error opening local file %s\n",lname);
1205 DEBUG(1,("putting file %s as %s ",lname,
1208 buf = (uint8_t *)malloc(maxwrite);
1210 d_printf("ERROR: Not enough memory!\n");
1213 while (!x_feof(f)) {
1217 if ((n = readfile(buf,n,f)) < 1) {
1218 if((n == 0) && x_feof(f))
1219 break; /* Empty local file. */
1221 d_printf("Error reading local file: %s\n", strerror(errno));
1226 ret = smbcli_write(cli->tree, fnum, 0, buf, nread + start, n);
1229 d_printf("Error writing file: %s\n", smbcli_errstr(cli->tree));
1237 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
1238 d_printf("%s closing remote file %s\n",smbcli_errstr(cli->tree),rname);
1252 struct timeval tp_end;
1255 GetTimeOfDay(&tp_end);
1257 (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1258 (tp_end.tv_usec - tp_start.tv_usec)/1000;
1259 put_total_time_ms += this_time;
1260 put_total_size += nread;
1262 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1263 nread / (1.024*this_time + 1.0e-4),
1264 put_total_size / (1.024*put_total_time_ms)));
1268 smbcli_shutdown(cli);
1277 /****************************************************************************
1279 ****************************************************************************/
1280 static int cmd_put(const char **cmd_ptr)
1287 pstrcpy(rname,cur_dir);
1288 pstrcat(rname,"\\");
1290 if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1291 d_printf("put <filename>\n");
1296 if (next_token(cmd_ptr,p,NULL,sizeof(buf)))
1299 pstrcat(rname,lname);
1301 dos_clean_name(rname);
1305 /* allow '-' to represent stdin
1306 jdblair, 24.jun.98 */
1307 if (!file_exist(lname,&st) &&
1308 (strcmp(lname,"-"))) {
1309 d_printf("%s does not exist\n",lname);
1314 return do_put(rname, lname, False);
1317 /*************************************
1319 *************************************/
1321 static struct file_list {
1322 struct file_list *prev, *next;
1327 /****************************************************************************
1328 Free a file_list structure
1329 ****************************************************************************/
1331 static void free_file_list (struct file_list * list)
1333 struct file_list *tmp;
1338 DLIST_REMOVE(list, list);
1339 SAFE_FREE(tmp->file_path);
1344 /****************************************************************************
1345 seek in a directory/file list until you get something that doesn't start with
1347 ****************************************************************************/
1348 static BOOL seek_list(struct file_list *list, char *name)
1351 trim_string(list->file_path,"./","\n");
1352 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1361 /****************************************************************************
1362 set the file selection mask
1363 ****************************************************************************/
1364 static int cmd_select(const char **cmd_ptr)
1366 pstrcpy(fileselection,"");
1367 next_token(cmd_ptr,fileselection,NULL,sizeof(fileselection));
1372 /*******************************************************************
1373 A readdir wrapper which just returns the file name.
1374 ********************************************************************/
1375 static const char *readdirname(DIR *p)
1383 ptr = (struct dirent *)readdir(p);
1387 dname = ptr->d_name;
1394 #ifdef HAVE_BROKEN_READDIR
1395 /* using /usr/ucb/cc is BAD */
1401 int len = NAMLEN(ptr);
1402 memcpy(buf, dname, len);
1410 /****************************************************************************
1411 Recursive file matching function act as find
1412 match must be always set to True when calling this function
1413 ****************************************************************************/
1414 static int file_find(struct file_list **list, const char *directory,
1415 const char *expression, BOOL match)
1418 struct file_list *entry;
1419 struct stat statbuf;
1425 dir = opendir(directory);
1426 if (!dir) return -1;
1428 while ((dname = readdirname(dir))) {
1429 if (!strcmp("..", dname)) continue;
1430 if (!strcmp(".", dname)) continue;
1432 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1437 if (!match || !gen_fnmatch(expression, dname)) {
1439 ret = stat(path, &statbuf);
1441 if (S_ISDIR(statbuf.st_mode)) {
1443 ret = file_find(list, path, expression, False);
1446 d_printf("file_find: cannot stat file %s\n", path);
1455 entry = malloc_p(struct file_list);
1457 d_printf("Out of memory in file_find\n");
1461 entry->file_path = path;
1462 entry->isdir = isdir;
1463 DLIST_ADD(*list, entry);
1473 /****************************************************************************
1475 ****************************************************************************/
1476 static int cmd_mput(const char **cmd_ptr)
1481 while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
1483 struct file_list *temp_list;
1484 char *quest, *lname, *rname;
1488 ret = file_find(&file_list, ".", p, True);
1490 free_file_list(file_list);
1498 for (temp_list = file_list; temp_list;
1499 temp_list = temp_list->next) {
1502 if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1504 trim_string(lname, "./", "/");
1506 /* check if it's a directory */
1507 if (temp_list->isdir) {
1508 /* if (!recurse) continue; */
1511 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1512 if (prompt && !yesno(quest)) { /* No */
1513 /* Skip the directory */
1514 lname[strlen(lname)-1] = '/';
1515 if (!seek_list(temp_list, lname))
1519 if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1521 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, rname)) &&
1522 NT_STATUS_IS_ERR(do_mkdir(rname))) {
1523 DEBUG (0, ("Unable to make dir, skipping..."));
1524 /* Skip the directory */
1525 lname[strlen(lname)-1] = '/';
1526 if (!seek_list(temp_list, lname))
1533 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1534 if (prompt && !yesno(quest)) /* No */
1539 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1544 do_put(rname, lname, False);
1546 free_file_list(file_list);
1556 /****************************************************************************
1558 ****************************************************************************/
1559 static int do_cancel(int job)
1561 d_printf("REWRITE: print job cancel not implemented\n");
1566 /****************************************************************************
1568 ****************************************************************************/
1569 static int cmd_cancel(const char **cmd_ptr)
1574 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1575 d_printf("cancel <jobid> ...\n");
1581 } while (next_token(cmd_ptr,buf,NULL,sizeof(buf)));
1587 /****************************************************************************
1589 ****************************************************************************/
1590 static int cmd_print(const char **cmd_ptr)
1596 if (!next_token(cmd_ptr,lname,NULL, sizeof(lname))) {
1597 d_printf("print <filename>\n");
1601 pstrcpy(rname,lname);
1602 p = strrchr_m(rname,'/');
1604 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1607 if (strequal(lname,"-")) {
1608 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1611 return do_put(rname, lname, False);
1615 /****************************************************************************
1617 ****************************************************************************/
1618 static int cmd_queue(const char **cmd_ptr)
1620 d_printf("REWRITE: print job queue not implemented\n");
1625 /****************************************************************************
1627 ****************************************************************************/
1628 static int cmd_del(const char **cmd_ptr)
1632 uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1635 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1637 pstrcpy(mask,cur_dir);
1639 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1640 d_printf("del <filename>\n");
1645 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, mask))) {
1646 d_printf("%s deleting remote file %s\n",smbcli_errstr(cli->tree),mask);
1653 /****************************************************************************
1654 delete a whole directory tree
1655 ****************************************************************************/
1656 static int cmd_deltree(const char **cmd_ptr)
1662 pstrcpy(dname,cur_dir);
1664 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1665 d_printf("deltree <dirname>\n");
1670 ret = smbcli_deltree(cli->tree, dname);
1673 printf("Failed to delete tree %s - %s\n", dname, smbcli_errstr(cli->tree));
1677 printf("Deleted %d files in %s\n", ret, dname);
1683 /****************************************************************************
1684 show as much information as possible about a file
1685 ****************************************************************************/
1686 static int cmd_allinfo(const char **cmd_ptr)
1691 TALLOC_CTX *mem_ctx;
1692 union smb_fileinfo finfo;
1695 pstrcpy(fname,cur_dir);
1697 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1698 d_printf("allinfo <filename>\n");
1703 mem_ctx = talloc_init("%s", fname);
1705 /* first a ALL_INFO QPATHINFO */
1706 finfo.generic.level = RAW_FILEINFO_ALL_INFO;
1707 finfo.generic.in.fname = fname;
1708 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1709 if (!NT_STATUS_IS_OK(status)) {
1710 d_printf("%s - %s\n", fname, nt_errstr(status));
1715 d_printf("\tcreate_time: %s\n", nt_time_string(mem_ctx, finfo.all_info.out.create_time));
1716 d_printf("\taccess_time: %s\n", nt_time_string(mem_ctx, finfo.all_info.out.access_time));
1717 d_printf("\twrite_time: %s\n", nt_time_string(mem_ctx, finfo.all_info.out.write_time));
1718 d_printf("\tchange_time: %s\n", nt_time_string(mem_ctx, finfo.all_info.out.change_time));
1719 d_printf("\tattrib: 0x%x\n", finfo.all_info.out.attrib);
1720 d_printf("\talloc_size: %lu\n", (unsigned long)finfo.all_info.out.alloc_size);
1721 d_printf("\tsize: %lu\n", (unsigned long)finfo.all_info.out.size);
1722 d_printf("\tnlink: %u\n", finfo.all_info.out.nlink);
1723 d_printf("\tdelete_pending: %u\n", finfo.all_info.out.delete_pending);
1724 d_printf("\tdirectory: %u\n", finfo.all_info.out.directory);
1725 d_printf("\tea_size: %u\n", finfo.all_info.out.ea_size);
1726 d_printf("\tfname: '%s'\n", finfo.all_info.out.fname.s);
1728 /* 8.3 name if any */
1729 finfo.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
1730 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1731 if (NT_STATUS_IS_OK(status)) {
1732 d_printf("\talt_name: %s\n", finfo.alt_name_info.out.fname.s);
1735 /* file_id if available */
1736 finfo.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
1737 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1738 if (NT_STATUS_IS_OK(status)) {
1739 d_printf("\tfile_id %.0f\n",
1740 (double)finfo.internal_information.out.file_id);
1743 /* the EAs, if any */
1744 finfo.generic.level = RAW_FILEINFO_ALL_EAS;
1745 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1746 if (NT_STATUS_IS_OK(status)) {
1748 for (i=0;i<finfo.all_eas.out.num_eas;i++) {
1749 d_printf("\tEA[%d] flags=%d %s=%*.*s\n", i,
1750 finfo.all_eas.out.eas[i].flags,
1751 finfo.all_eas.out.eas[i].name.s,
1752 finfo.all_eas.out.eas[i].value.length,
1753 finfo.all_eas.out.eas[i].value.length,
1754 finfo.all_eas.out.eas[i].value.data);
1758 /* streams, if available */
1759 finfo.generic.level = RAW_FILEINFO_STREAM_INFO;
1760 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1761 if (NT_STATUS_IS_OK(status)) {
1763 for (i=0;i<finfo.stream_info.out.num_streams;i++) {
1764 d_printf("\tstream %d:\n", i);
1765 d_printf("\t\tsize %ld\n",
1766 (long)finfo.stream_info.out.streams[i].size);
1767 d_printf("\t\talloc size %ld\n",
1768 (long)finfo.stream_info.out.streams[i].alloc_size);
1769 d_printf("\t\tname %s\n", finfo.stream_info.out.streams[i].stream_name.s);
1773 /* dev/inode if available */
1774 finfo.generic.level = RAW_FILEINFO_COMPRESSION_INFORMATION;
1775 status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1776 if (NT_STATUS_IS_OK(status)) {
1777 d_printf("\tcompressed size %ld\n", (long)finfo.compression_info.out.compressed_size);
1778 d_printf("\tformat %ld\n", (long)finfo.compression_info.out.format);
1779 d_printf("\tunit_shift %ld\n", (long)finfo.compression_info.out.unit_shift);
1780 d_printf("\tchunk_shift %ld\n", (long)finfo.compression_info.out.chunk_shift);
1781 d_printf("\tcluster_shift %ld\n", (long)finfo.compression_info.out.cluster_shift);
1784 talloc_destroy(mem_ctx);
1791 /****************************************************************************
1792 show any ACL on a file
1793 ****************************************************************************/
1794 static int cmd_acl(const char **cmd_ptr)
1799 TALLOC_CTX *mem_ctx;
1800 union smb_fileinfo query;
1804 pstrcpy(fname,cur_dir);
1806 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1807 d_printf("acl <filename>\n");
1812 fnum = smbcli_open(cli->tree, fname, O_RDONLY, DENY_NONE);
1814 d_printf("%s - %s\n", fname, smbcli_errstr(cli->tree));
1818 mem_ctx = talloc_init("%s", fname);
1820 query.query_secdesc.level = RAW_FILEINFO_SEC_DESC;
1821 query.query_secdesc.in.fnum = fnum;
1822 query.query_secdesc.in.secinfo_flags = 0x7;
1824 status = smb_raw_fileinfo(cli->tree, mem_ctx, &query);
1825 if (!NT_STATUS_IS_OK(status)) {
1826 d_printf("%s - %s\n", fname, nt_errstr(status));
1831 NDR_PRINT_DEBUG(security_descriptor, query.query_secdesc.out.sd);
1833 talloc_destroy(mem_ctx);
1839 /****************************************************************************
1840 lookup a name or sid
1841 ****************************************************************************/
1842 static int cmd_lookup(const char **cmd_ptr)
1845 TALLOC_CTX *mem_ctx = talloc(NULL, 0);
1847 struct dom_sid *sid;
1849 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1850 d_printf("lookup <name|sid>\n");
1851 talloc_free(mem_ctx);
1855 sid = dom_sid_parse_talloc(mem_ctx, buf);
1858 status = smblsa_lookup_name(cli, buf, mem_ctx, &sidstr);
1859 if (!NT_STATUS_IS_OK(status)) {
1860 d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
1861 talloc_free(mem_ctx);
1865 d_printf("%s\n", sidstr);
1868 status = smblsa_lookup_sid(cli, buf, mem_ctx, &name);
1869 if (!NT_STATUS_IS_OK(status)) {
1870 d_printf("lsa_LookupSids - %s\n", nt_errstr(status));
1871 talloc_free(mem_ctx);
1875 d_printf("%s\n", name);
1878 talloc_free(mem_ctx);
1883 /****************************************************************************
1884 show privileges for a user
1885 ****************************************************************************/
1886 static int cmd_privileges(const char **cmd_ptr)
1889 TALLOC_CTX *mem_ctx = talloc(NULL, 0);
1891 struct dom_sid *sid;
1892 struct lsa_RightSet rights;
1895 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1896 d_printf("privileges <sid|name>\n");
1897 talloc_free(mem_ctx);
1901 sid = dom_sid_parse_talloc(mem_ctx, buf);
1903 const char *sid_str;
1904 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
1905 if (!NT_STATUS_IS_OK(status)) {
1906 d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
1907 talloc_free(mem_ctx);
1910 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
1913 status = smblsa_sid_privileges(cli, sid, mem_ctx, &rights);
1914 if (!NT_STATUS_IS_OK(status)) {
1915 d_printf("lsa_EnumAccountRights - %s\n", nt_errstr(status));
1916 talloc_free(mem_ctx);
1920 for (i=0;i<rights.count;i++) {
1921 d_printf("\t%s\n", rights.names[i].string);
1924 talloc_free(mem_ctx);
1930 /****************************************************************************
1931 add privileges for a user
1932 ****************************************************************************/
1933 static int cmd_addprivileges(const char **cmd_ptr)
1936 TALLOC_CTX *mem_ctx = talloc(NULL, 0);
1938 struct dom_sid *sid;
1939 struct lsa_RightSet rights;
1941 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1942 d_printf("addprivileges <sid> <privilege...>\n");
1943 talloc_free(mem_ctx);
1947 sid = dom_sid_parse_talloc(mem_ctx, buf);
1949 const char *sid_str;
1950 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
1951 if (!NT_STATUS_IS_OK(status)) {
1952 d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
1953 talloc_free(mem_ctx);
1956 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
1959 ZERO_STRUCT(rights);
1960 while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1961 rights.names = talloc_realloc_p(mem_ctx, rights.names,
1962 struct lsa_String, rights.count+1);
1963 rights.names[rights.count].string = talloc_strdup(mem_ctx, buf);
1968 status = smblsa_sid_add_privileges(cli, sid, mem_ctx, &rights);
1969 if (!NT_STATUS_IS_OK(status)) {
1970 d_printf("lsa_AddAccountRights - %s\n", nt_errstr(status));
1971 talloc_free(mem_ctx);
1975 talloc_free(mem_ctx);
1980 /****************************************************************************
1981 delete privileges for a user
1982 ****************************************************************************/
1983 static int cmd_delprivileges(const char **cmd_ptr)
1986 TALLOC_CTX *mem_ctx = talloc(NULL, 0);
1988 struct dom_sid *sid;
1989 struct lsa_RightSet rights;
1991 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
1992 d_printf("delprivileges <sid> <privilege...>\n");
1993 talloc_free(mem_ctx);
1997 sid = dom_sid_parse_talloc(mem_ctx, buf);
1999 const char *sid_str;
2000 status = smblsa_lookup_name(cli, buf, mem_ctx, &sid_str);
2001 if (!NT_STATUS_IS_OK(status)) {
2002 d_printf("lsa_LookupNames - %s\n", nt_errstr(status));
2003 talloc_free(mem_ctx);
2006 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
2009 ZERO_STRUCT(rights);
2010 while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2011 rights.names = talloc_realloc_p(mem_ctx, rights.names,
2012 struct lsa_String, rights.count+1);
2013 rights.names[rights.count].string = talloc_strdup(mem_ctx, buf);
2018 status = smblsa_sid_del_privileges(cli, sid, mem_ctx, &rights);
2019 if (!NT_STATUS_IS_OK(status)) {
2020 d_printf("lsa_RemoveAccountRights - %s\n", nt_errstr(status));
2021 talloc_free(mem_ctx);
2025 talloc_free(mem_ctx);
2031 /****************************************************************************
2032 ****************************************************************************/
2033 static int cmd_open(const char **cmd_ptr)
2038 pstrcpy(mask,cur_dir);
2040 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2041 d_printf("open <filename>\n");
2046 smbcli_open(cli->tree, mask, O_RDWR, DENY_ALL);
2052 /****************************************************************************
2054 ****************************************************************************/
2055 static int cmd_rmdir(const char **cmd_ptr)
2060 pstrcpy(mask,cur_dir);
2062 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2063 d_printf("rmdir <dirname>\n");
2068 if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, mask))) {
2069 d_printf("%s removing remote directory file %s\n",
2070 smbcli_errstr(cli->tree),mask);
2076 /****************************************************************************
2078 ****************************************************************************/
2079 static int cmd_link(const char **cmd_ptr)
2084 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2085 d_printf("Server doesn't support UNIX CIFS calls.\n");
2089 pstrcpy(src,cur_dir);
2090 pstrcpy(dest,cur_dir);
2092 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2093 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2094 d_printf("link <src> <dest>\n");
2101 if (NT_STATUS_IS_ERR(smbcli_unix_hardlink(cli->tree, src, dest))) {
2102 d_printf("%s linking files (%s -> %s)\n", smbcli_errstr(cli->tree), src, dest);
2109 /****************************************************************************
2111 ****************************************************************************/
2113 static int cmd_symlink(const char **cmd_ptr)
2118 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2119 d_printf("Server doesn't support UNIX CIFS calls.\n");
2123 pstrcpy(src,cur_dir);
2124 pstrcpy(dest,cur_dir);
2126 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2127 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2128 d_printf("symlink <src> <dest>\n");
2135 if (NT_STATUS_IS_ERR(smbcli_unix_symlink(cli->tree, src, dest))) {
2136 d_printf("%s symlinking files (%s -> %s)\n",
2137 smbcli_errstr(cli->tree), src, dest);
2144 /****************************************************************************
2146 ****************************************************************************/
2148 static int cmd_chmod(const char **cmd_ptr)
2154 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2155 d_printf("Server doesn't support UNIX CIFS calls.\n");
2159 pstrcpy(src,cur_dir);
2161 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2162 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2163 d_printf("chmod mode file\n");
2167 mode = (mode_t)strtol(buf, NULL, 8);
2170 if (NT_STATUS_IS_ERR(smbcli_unix_chmod(cli->tree, src, mode))) {
2171 d_printf("%s chmod file %s 0%o\n",
2172 smbcli_errstr(cli->tree), src, (uint_t)mode);
2179 /****************************************************************************
2181 ****************************************************************************/
2183 static int cmd_chown(const char **cmd_ptr)
2188 fstring buf, buf2, buf3;
2190 if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
2191 d_printf("Server doesn't support UNIX CIFS calls.\n");
2195 pstrcpy(src,cur_dir);
2197 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2198 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2)) ||
2199 !next_token(cmd_ptr,buf3,NULL, sizeof(buf3))) {
2200 d_printf("chown uid gid file\n");
2204 uid = (uid_t)atoi(buf);
2205 gid = (gid_t)atoi(buf2);
2208 if (NT_STATUS_IS_ERR(smbcli_unix_chown(cli->tree, src, uid, gid))) {
2209 d_printf("%s chown file %s uid=%d, gid=%d\n",
2210 smbcli_errstr(cli->tree), src, (int)uid, (int)gid);
2217 /****************************************************************************
2219 ****************************************************************************/
2220 static int cmd_rename(const char **cmd_ptr)
2225 pstrcpy(src,cur_dir);
2226 pstrcpy(dest,cur_dir);
2228 if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) ||
2229 !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
2230 d_printf("rename <src> <dest>\n");
2237 if (NT_STATUS_IS_ERR(smbcli_rename(cli->tree, src, dest))) {
2238 d_printf("%s renaming files\n",smbcli_errstr(cli->tree));
2246 /****************************************************************************
2247 toggle the prompt flag
2248 ****************************************************************************/
2249 static int cmd_prompt(const char **cmd_ptr)
2252 DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2258 /****************************************************************************
2259 set the newer than time
2260 ****************************************************************************/
2261 static int cmd_newer(const char **cmd_ptr)
2267 ok = next_token(cmd_ptr,buf,NULL,sizeof(buf));
2268 if (ok && (stat(buf,&sbuf) == 0)) {
2269 newer_than = sbuf.st_mtime;
2270 DEBUG(1,("Getting files newer than %s",
2271 asctime(localtime(&newer_than))));
2276 if (ok && newer_than == 0) {
2277 d_printf("Error setting newer-than time\n");
2284 /****************************************************************************
2285 set the archive level
2286 ****************************************************************************/
2287 static int cmd_archive(const char **cmd_ptr)
2291 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2292 archive_level = atoi(buf);
2294 d_printf("Archive level is %d\n",archive_level);
2299 /****************************************************************************
2300 toggle the lowercaseflag
2301 ****************************************************************************/
2302 static int cmd_lowercase(const char **cmd_ptr)
2304 lowercase = !lowercase;
2305 DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2313 /****************************************************************************
2314 toggle the recurse flag
2315 ****************************************************************************/
2316 static int cmd_recurse(const char **cmd_ptr)
2319 DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2324 /****************************************************************************
2325 toggle the translate flag
2326 ****************************************************************************/
2327 static int cmd_translate(const char **cmd_ptr)
2329 translation = !translation;
2330 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2331 translation?"on":"off"));
2337 /****************************************************************************
2338 do a printmode command
2339 ****************************************************************************/
2340 static int cmd_printmode(const char **cmd_ptr)
2345 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2346 if (strequal(buf,"text")) {
2349 if (strequal(buf,"graphics"))
2352 printmode = atoi(buf);
2359 fstrcpy(mode,"text");
2362 fstrcpy(mode,"graphics");
2365 slprintf(mode,sizeof(mode)-1,"%d",printmode);
2369 DEBUG(2,("the printmode is now %s\n",mode));
2374 /****************************************************************************
2376 ****************************************************************************/
2377 static int cmd_lcd(const char **cmd_ptr)
2382 if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
2384 DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2389 /****************************************************************************
2390 get a file restarting at end of local file
2391 ****************************************************************************/
2392 static int cmd_reget(const char **cmd_ptr)
2395 pstring remote_name;
2398 pstrcpy(remote_name, cur_dir);
2399 pstrcat(remote_name, "\\");
2401 p = remote_name + strlen(remote_name);
2403 if (!next_token(cmd_ptr, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2404 d_printf("reget <filename>\n");
2407 pstrcpy(local_name, p);
2408 dos_clean_name(remote_name);
2410 next_token(cmd_ptr, local_name, NULL, sizeof(local_name));
2412 return do_get(remote_name, local_name, True);
2415 /****************************************************************************
2416 put a file restarting at end of local file
2417 ****************************************************************************/
2418 static int cmd_reput(const char **cmd_ptr)
2421 pstring remote_name;
2426 pstrcpy(remote_name, cur_dir);
2427 pstrcat(remote_name, "\\");
2429 if (!next_token(cmd_ptr, p, NULL, sizeof(buf))) {
2430 d_printf("reput <filename>\n");
2433 pstrcpy(local_name, p);
2435 if (!file_exist(local_name, &st)) {
2436 d_printf("%s does not exist\n", local_name);
2440 if (next_token(cmd_ptr, p, NULL, sizeof(buf)))
2441 pstrcat(remote_name, p);
2443 pstrcat(remote_name, local_name);
2445 dos_clean_name(remote_name);
2447 return do_put(remote_name, local_name, True);
2452 return a string representing a share type
2454 static const char *share_type_str(uint32_t type)
2456 switch (type & 0xF) {
2457 case STYPE_DISKTREE:
2472 display a list of shares from a level 1 share enum
2474 static void display_share_result(struct srvsvc_NetShareCtr1 *ctr1)
2478 for (i=0;i<ctr1->count;i++) {
2479 struct srvsvc_NetShareInfo1 *info = ctr1->array+i;
2481 printf("\t%-15s %-10.10s %s\n",
2483 share_type_str(info->type),
2490 /****************************************************************************
2491 try and browse available shares on a host
2492 ****************************************************************************/
2493 static BOOL browse_host(const char *query_host)
2495 struct dcerpc_pipe *p;
2498 struct srvsvc_NetShareEnumAll r;
2499 uint32_t resume_handle = 0;
2500 TALLOC_CTX *mem_ctx = talloc_init("browse_host");
2501 struct srvsvc_NetShareCtr1 ctr1;
2503 binding = talloc_asprintf(mem_ctx, "ncacn_np:%s", query_host);
2505 status = dcerpc_pipe_connect(&p, binding,
2507 DCERPC_SRVSVC_VERSION,
2509 username, password);
2510 if (!NT_STATUS_IS_OK(status)) {
2511 d_printf("Failed to connect to %s - %s\n",
2512 binding, nt_errstr(status));
2513 talloc_destroy(mem_ctx);
2516 talloc_steal(mem_ctx, p);
2518 r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
2520 r.in.ctr.ctr1 = &ctr1;
2521 r.in.max_buffer = ~0;
2522 r.in.resume_handle = &resume_handle;
2524 d_printf("\n\tSharename Type Comment\n");
2525 d_printf("\t--------- ---- -------\n");
2529 status = dcerpc_srvsvc_NetShareEnumAll(p, mem_ctx, &r);
2531 if (NT_STATUS_IS_OK(status) &&
2532 (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA) ||
2533 W_ERROR_IS_OK(r.out.result)) &&
2535 display_share_result(r.out.ctr.ctr1);
2536 resume_handle += r.out.ctr.ctr1->count;
2538 } while (NT_STATUS_IS_OK(status) && W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
2540 talloc_destroy(mem_ctx);
2542 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
2543 d_printf("Failed NetShareEnumAll %s - %s/%s\n",
2544 binding, nt_errstr(status), win_errstr(r.out.result));
2551 /****************************************************************************
2552 try and browse available connections on a host
2553 ****************************************************************************/
2554 static BOOL list_servers(const char *wk_grp)
2556 d_printf("REWRITE: list servers not implemented\n");
2560 /* Some constants for completing filename arguments */
2562 #define COMPL_NONE 0 /* No completions */
2563 #define COMPL_REMOTE 1 /* Complete remote filename */
2564 #define COMPL_LOCAL 2 /* Complete local filename */
2566 /* This defines the commands supported by this client.
2567 * NOTE: The "!" must be the last one in the list because it's fn pointer
2568 * field is NULL, and NULL in that field is used in process_tok()
2569 * (below) to indicate the end of the list. crh
2574 int (*fn)(const char **cmd_ptr);
2575 const char *description;
2576 char compl_args[2]; /* Completion argument info */
2579 {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2580 {"addprivileges",cmd_addprivileges,"<sid|user> <privilege...> add privileges for a user",{COMPL_NONE,COMPL_NONE}},
2581 {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2582 {"acl",cmd_acl,"<file> show file ACL",{COMPL_NONE,COMPL_NONE}},
2583 {"allinfo",cmd_allinfo,"<file> show all possible info about a file",{COMPL_NONE,COMPL_NONE}},
2584 {"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}},
2585 {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2586 {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2587 {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2588 {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2589 {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2590 {"delprivileges",cmd_delprivileges,"<sid|user> <privilege...> remove privileges for a user",{COMPL_NONE,COMPL_NONE}},
2591 {"deltree",cmd_deltree,"<dir> delete a whole directory tree",{COMPL_REMOTE,COMPL_NONE}},
2592 {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2593 {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2594 {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2595 {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2596 {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2597 {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2598 {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2599 {"link",cmd_link,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2600 {"lookup",cmd_lookup,"<name|sid> show SID for name or name for SID",{COMPL_NONE,COMPL_NONE}},
2601 {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
2602 {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2603 {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2604 {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2605 {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2606 {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2607 {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
2608 {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2609 {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2610 {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2611 {"privileges",cmd_privileges,"<user> show privileges for a user",{COMPL_NONE,COMPL_NONE}},
2612 {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2613 {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
2614 {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
2615 {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2616 {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2617 {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2618 {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2619 {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2620 {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2621 {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
2622 {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2623 {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2624 {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2625 {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2626 {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2627 {"symlink",cmd_symlink,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2628 {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2630 /* Yes, this must be here, see crh's comment above. */
2631 {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2632 {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2636 /*******************************************************************
2637 lookup a command string in the list of commands, including
2639 ******************************************************************/
2640 static int process_tok(fstring tok)
2642 int i = 0, matches = 0;
2644 int tok_len = strlen(tok);
2646 while (commands[i].fn != NULL) {
2647 if (strequal(commands[i].name,tok)) {
2651 } else if (strncasecmp(commands[i].name, tok, tok_len) == 0) {
2660 else if (matches == 1)
2666 /****************************************************************************
2668 ****************************************************************************/
2669 static int cmd_help(const char **cmd_ptr)
2674 if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
2675 if ((i = process_tok(buf)) >= 0)
2676 d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2678 while (commands[i].description) {
2679 for (j=0; commands[i].description && (j<5); j++) {
2680 d_printf("%-15s",commands[i].name);
2689 /****************************************************************************
2690 process a -c command string
2691 ****************************************************************************/
2692 static int process_command_string(char *cmd)
2698 /* establish the connection if not already */
2701 cli = do_connect(desthost, service);
2706 while (cmd[0] != '\0') {
2711 if ((p = strchr_m(cmd, ';')) == 0) {
2712 strncpy(line, cmd, 999);
2716 if (p - cmd > 999) p = cmd + 999;
2717 strncpy(line, cmd, p - cmd);
2718 line[p - cmd] = '\0';
2722 /* and get the first part of the command */
2724 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
2726 if ((i = process_tok(tok)) >= 0) {
2727 rc = commands[i].fn(&ptr);
2728 } else if (i == -2) {
2729 d_printf("%s: command abbreviation ambiguous\n",tok);
2731 d_printf("%s: command not found\n",tok);
2738 #define MAX_COMPLETIONS 100
2746 } completion_remote_t;
2748 static void completion_remote_filter(struct clilist_file_info *f, const char *mask, void *state)
2750 completion_remote_t *info = (completion_remote_t *)state;
2752 if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2753 if ((info->dirmask[0] == 0) && !(f->attrib & FILE_ATTRIBUTE_DIRECTORY))
2754 info->matches[info->count] = strdup(f->name);
2758 if (info->dirmask[0] != 0)
2759 pstrcpy(tmp, info->dirmask);
2762 pstrcat(tmp, f->name);
2763 if (f->attrib & FILE_ATTRIBUTE_DIRECTORY)
2765 info->matches[info->count] = strdup(tmp);
2767 if (info->matches[info->count] == NULL)
2769 if (f->attrib & FILE_ATTRIBUTE_DIRECTORY)
2770 smb_readline_ca_char(0);
2772 if (info->count == 1)
2773 info->samelen = strlen(info->matches[info->count]);
2775 while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
2781 static char **remote_completion(const char *text, int len)
2785 completion_remote_t info = { "", NULL, 1, 0, NULL, 0 };
2791 if (len >= PATH_MAX)
2794 info.matches = malloc_array_p(char *, MAX_COMPLETIONS);
2795 if (!info.matches) return NULL;
2796 info.matches[0] = NULL;
2798 for (i = len-1; i >= 0; i--)
2799 if ((text[i] == '/') || (text[i] == '\\'))
2801 info.text = text+i+1;
2802 info.samelen = info.len = len-i-1;
2805 strncpy(info.dirmask, text, i+1);
2806 info.dirmask[i+1] = 0;
2807 snprintf(dirmask, sizeof(dirmask), "%s%*s*", cur_dir, i-1, text);
2809 snprintf(dirmask, sizeof(dirmask), "%s*", cur_dir);
2811 if (smbcli_list(cli->tree, dirmask,
2812 FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
2813 completion_remote_filter, &info) < 0)
2816 if (info.count == 2)
2817 info.matches[0] = strdup(info.matches[1]);
2819 info.matches[0] = malloc(info.samelen+1);
2820 if (!info.matches[0])
2822 strncpy(info.matches[0], info.matches[1], info.samelen);
2823 info.matches[0][info.samelen] = 0;
2825 info.matches[info.count] = NULL;
2826 return info.matches;
2829 for (i = 0; i < info.count; i++)
2830 free(info.matches[i]);
2835 static char **completion_fn(const char *text, int start, int end)
2837 smb_readline_ca_char(' ');
2840 const char *buf, *sp;
2844 buf = smb_readline_get_line_buffer();
2848 sp = strchr(buf, ' ');
2852 for (i = 0; commands[i].name; i++)
2853 if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
2855 if (commands[i].name == NULL)
2861 if (sp == (buf + start))
2862 compl_type = commands[i].compl_args[0];
2864 compl_type = commands[i].compl_args[1];
2866 if (compl_type == COMPL_REMOTE)
2867 return remote_completion(text, end - start);
2868 else /* fall back to local filename completion */
2872 int i, len, samelen = 0, count=1;
2874 matches = malloc_array_p(char *, MAX_COMPLETIONS);
2875 if (!matches) return NULL;
2879 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
2880 if (strncmp(text, commands[i].name, len) == 0) {
2881 matches[count] = strdup(commands[i].name);
2882 if (!matches[count])
2885 samelen = strlen(matches[count]);
2887 while (strncmp(matches[count], matches[count-1], samelen) != 0)
2894 case 0: /* should never happen */
2898 matches[0] = strdup(matches[1]);
2901 matches[0] = malloc(samelen+1);
2904 strncpy(matches[0], matches[1], samelen);
2905 matches[0][samelen] = 0;
2907 matches[count] = NULL;
2920 /****************************************************************************
2921 make sure we swallow keepalives during idle time
2922 ****************************************************************************/
2923 static void readline_callback(void)
2925 static time_t last_t;
2930 if (t - last_t < 5) return;
2934 smbcli_transport_process(cli->transport);
2937 smbcli_chkpath(cli->tree, "\\");
2942 /****************************************************************************
2943 process commands on stdin
2944 ****************************************************************************/
2945 static void process_stdin(void)
2955 /* display a prompt */
2956 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
2957 cline = smb_readline(the_prompt, readline_callback, completion_fn);
2961 pstrcpy(line, cline);
2963 /* special case - first char is ! */
2969 /* and get the first part of the command */
2971 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
2973 if ((i = process_tok(tok)) >= 0) {
2974 commands[i].fn(&ptr);
2975 } else if (i == -2) {
2976 d_printf("%s: command abbreviation ambiguous\n",tok);
2978 d_printf("%s: command not found\n",tok);
2984 /*****************************************************
2985 return a connection to a server
2986 *******************************************************/
2987 static struct smbcli_state *do_connect(const char *server, const char *share)
2989 struct smbcli_state *c;
2990 struct nmb_name called, calling;
2991 const char *server_n;
2992 fstring servicename;
2996 /* make a copy so we don't modify the global string 'service' */
2997 fstrcpy(servicename, share);
2998 sharename = servicename;
2999 if (*sharename == '\\') {
3000 server = sharename+2;
3001 sharename = strchr_m(server,'\\');
3002 if (!sharename) return NULL;
3007 asprintf(&sharename, "\\\\%s\\%s", server, sharename);
3009 server_n = dest_ip?dest_ip:server;
3011 make_nmb_name(&calling, lp_netbios_name(), 0x0);
3012 choose_called_name(&called, server, name_type);
3015 /* have to open a new connection */
3016 if (!(c=smbcli_state_init(NULL)) || !smbcli_socket_connect(c, server_n)) {
3017 d_printf("Connection to %s failed\n", server_n);
3021 if (!smbcli_transport_establish(c, &calling, &called)) {
3023 d_printf("session request to %s failed\n",
3026 if ((p=strchr_m(called.name, '.'))) {
3030 if (strcmp(called.name, "*SMBSERVER")) {
3031 make_nmb_name(&called , "*SMBSERVER", 0x20);
3037 DEBUG(4,(" session request ok\n"));
3039 if (NT_STATUS_IS_ERR(smbcli_negprot(c))) {
3040 d_printf("protocol negotiation failed\n");
3045 status = smbcli_session_setup(c, username, password, domain);
3046 if (NT_STATUS_IS_ERR(status)) {
3047 d_printf("authenticated session setup failed: %s\n", nt_errstr(status));
3048 /* if a password was not supplied then try again with a null username */
3049 if (password[0] || !username[0]) {
3050 status = smbcli_session_setup(c, "", "", lp_workgroup());
3052 if (NT_STATUS_IS_ERR(status)) {
3053 d_printf("session setup failed: %s\n", nt_errstr(status));
3057 d_printf("Anonymous login successful\n");
3060 DEBUG(4,(" session setup ok\n"));
3062 if (NT_STATUS_IS_ERR(smbcli_send_tconX(c, sharename, "?????", password))) {
3063 d_printf("tree connect failed: %s\n", smbcli_errstr(c->tree));
3068 DEBUG(4,(" tconx ok\n"));
3074 /****************************************************************************
3075 process commands from the client
3076 ****************************************************************************/
3077 static int process(char *base_directory)
3081 cli = do_connect(desthost, service);
3086 if (*base_directory) do_cd(base_directory);
3089 rc = process_command_string(cmdstr);
3094 smbcli_shutdown(cli);
3098 /****************************************************************************
3100 ****************************************************************************/
3101 static int do_host_query(char *query_host)
3103 browse_host(query_host);
3104 list_servers(lp_workgroup());
3109 /****************************************************************************
3110 handle a message operation
3111 ****************************************************************************/
3112 static int do_message_op(void)
3114 struct nmb_name called, calling;
3115 const char *server_name;
3117 make_nmb_name(&calling, lp_netbios_name(), 0x0);
3118 choose_called_name(&called, desthost, name_type);
3120 server_name = dest_ip ? dest_ip : desthost;
3122 if (!(cli=smbcli_state_init(NULL)) || !smbcli_socket_connect(cli, server_name)) {
3123 d_printf("Connection to %s failed\n", server_name);
3127 if (!smbcli_transport_establish(cli, &calling, &called)) {
3128 d_printf("session request failed\n");
3129 smbcli_shutdown(cli);
3134 smbcli_shutdown(cli);
3141 * Process "-L hostname" option.
3143 * We don't actually do anything yet -- we just stash the name in a
3144 * global variable and do the query when all options have been read.
3146 static void remember_query_host(const char *arg,
3151 while (*arg == '\\' || *arg == '/')
3153 pstrcpy(query_host, arg);
3154 if ((slash = strchr(query_host, '/'))
3155 || (slash = strchr(query_host, '\\'))) {
3161 /****************************************************************************
3163 ****************************************************************************/
3164 int main(int argc,char *argv[])
3166 fstring base_directory;
3169 BOOL message = False;
3174 TALLOC_CTX *mem_ctx;
3175 struct poptOption long_options[] = {
3178 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
3179 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
3180 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
3181 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
3182 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3183 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
3184 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" },
3185 { "send-buffer", 'b', POPT_ARG_INT, NULL, 'b', "Changes the transmit/send buffer", "BYTES" },
3186 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
3188 POPT_COMMON_CONNECTION
3189 POPT_COMMON_CREDENTIALS
3195 pstrcpy(term_code, KANJI);
3201 *base_directory = 0;
3203 setup_logging(argv[0],DEBUG_STDOUT);
3204 mem_ctx = talloc_init("client.c/main");
3206 d_printf("\nclient.c: Not enough memory\n");
3210 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
3211 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
3212 argv[0], dyn_CONFIGFILE);
3215 pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
3216 poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
3218 in_client = True; /* Make sure that we tell lp_load we are */
3220 while ((opt = poptGetNextOpt(pc)) != -1) {
3223 /* Messages are sent to NetBIOS name type 0x3
3224 * (Messenger Service). Make sure we default
3225 * to port 139 instead of port 445. srl,crh
3228 pstrcpy(desthost,poptGetOptArg(pc));
3229 if( 0 == port ) port = 139;
3233 dest_ip = poptGetOptArg(pc);
3236 setup_logging("client", DEBUG_STDERR);
3240 remember_query_host(poptGetOptArg(pc), query_host);
3243 pstrcpy(term_code, poptGetOptArg(pc));
3246 fstrcpy(base_directory,poptGetOptArg(pc));
3249 io_bufsize = MAX(1, atoi(poptGetOptArg(pc)));
3256 smbclient_init_subsystems;
3258 if(poptPeekArg(pc)) {
3259 pstrcpy(service,poptGetArg(pc));
3260 /* Convert any '/' characters in the service name to '\' characters */
3261 string_replace(service, '/','\\');
3263 if (count_chars(service,'\\') < 3) {
3264 d_printf("\n%s: Not enough '\\' characters in service\n",service);
3265 poptPrintUsage(pc, stderr, 0);
3270 if (poptPeekArg(pc)) {
3271 cmdline_set_userpassword(poptGetArg(pc));
3276 if (!*query_host && !*service && !message) {
3277 poptPrintUsage(pc, stderr, 0);
3281 poptFreeContext(pc);
3283 pstrcpy(username, cmdline_get_username());
3284 pstrcpy(domain, cmdline_get_userdomain());
3285 pstrcpy(password, cmdline_get_userpassword());
3287 DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION_STRING ) );
3289 talloc_destroy(mem_ctx);
3291 if ((p=strchr_m(query_host,'#'))) {
3294 sscanf(p, "%x", &name_type);
3298 return do_host_query(query_host);
3302 return do_message_op();
3305 if (process(base_directory)) {