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.
26 #include "dynconfig.h"
28 #include "lib/cmdline/popt_common.h"
29 #include "librpc/gen_ndr/ndr_srvsvc.h"
30 #include "librpc/gen_ndr/ndr_lsa.h"
31 #include "libcli/raw/libcliraw.h"
32 #include "system/time.h"
33 #include "system/dir.h"
34 #include "system/filesys.h"
35 #include "dlinklist.h"
41 struct smbcli_state
*cli
;
42 extern BOOL in_client
;
44 pstring cur_dir
= "\\";
45 static pstring cd_path
= "";
46 static pstring service
;
47 static pstring desthost
;
48 static char *cmdstr
= NULL
;
50 static int io_bufsize
= 64512;
52 static int name_type
= 0x20;
54 static int process_tok(fstring tok
);
55 static int cmd_help(const char **cmd_ptr
);
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
;
69 /* clitar bits insert */
72 extern BOOL tar_reset
;
76 static BOOL prompt
= True
;
78 static int printmode
= 1;
80 static BOOL recurse
= False
;
81 BOOL lowercase
= False
;
83 static const char *dest_ip
;
85 #define SEPARATORS " \t\n\r"
87 static BOOL abort_mget
= True
;
89 static pstring fileselection
= "";
92 uint64_t get_total_size
= 0;
93 uint_t get_total_time_ms
= 0;
94 static uint64_t put_total_size
= 0;
95 static uint_t put_total_time_ms
= 0;
98 static double dir_total
;
102 /* some forward declarations */
103 static struct smbcli_state
*do_connect(const char *server
, const char *share
, struct cli_credentials
*cred
);
106 /*******************************************************************
107 Reduce a file name, removing .. elements.
108 ********************************************************************/
109 void dos_clean_name(char *s
)
113 DEBUG(3,("dos_clean_name [%s]\n",s
));
115 /* remove any double slashes */
116 all_string_sub(s
, "\\\\", "\\", 0);
118 while ((p
= strstr(s
,"\\..\\")) != NULL
) {
124 if ((p
=strrchr_m(s
,'\\')) != NULL
)
131 trim_string(s
,NULL
,"\\..");
133 all_string_sub(s
, "\\.\\", "\\", 0);
136 /****************************************************************************
137 write to a local file with CR/LF->LF translation if appropriate. return the
138 number taken from the buffer. This may not equal the number written.
139 ****************************************************************************/
140 static int writefile(int f
, const void *_b
, int n
)
142 const uint8_t *b
= _b
;
151 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
154 if (write(f
, b
, 1) != 1) {
164 /****************************************************************************
165 read from a file with LF->CR/LF translation if appropriate. return the
166 number read. read approx n bytes.
167 ****************************************************************************/
168 static int readfile(void *_b
, int n
, XFILE
*f
)
175 return x_fread(b
,1,n
,f
);
178 while (i
< (n
- 1)) {
179 if ((c
= x_getc(f
)) == EOF
) {
183 if (c
== '\n') { /* change all LFs to CR/LF */
194 /****************************************************************************
196 ****************************************************************************/
197 static void send_message(void)
202 if (!smbcli_message_start(cli
->tree
, desthost
, cli_credentials_get_username(cmdline_credentials
), &grp_id
)) {
203 d_printf("message start: %s\n", smbcli_errstr(cli
->tree
));
208 d_printf("Connected. Type your message, ending it with a Control-D\n");
210 while (!feof(stdin
) && total_len
< 1600) {
211 int maxlen
= MIN(1600 - total_len
,127);
218 for (l
=0;l
<maxlen
&& (c
=fgetc(stdin
))!=EOF
;l
++) {
224 if (!smbcli_message_text(cli
->tree
, msg
, l
, grp_id
)) {
225 d_printf("SMBsendtxt failed (%s)\n",smbcli_errstr(cli
->tree
));
232 if (total_len
>= 1600)
233 d_printf("the message was truncated to 1600 bytes\n");
235 d_printf("sent %d bytes\n",total_len
);
237 if (!smbcli_message_end(cli
->tree
, grp_id
)) {
238 d_printf("SMBsendend failed (%s)\n",smbcli_errstr(cli
->tree
));
245 /****************************************************************************
246 check the space on a device
247 ****************************************************************************/
248 static int do_dskattr(void)
250 int total
, bsize
, avail
;
252 if (NT_STATUS_IS_ERR(smbcli_dskattr(cli
->tree
, &bsize
, &total
, &avail
))) {
253 d_printf("Error in dskattr: %s\n",smbcli_errstr(cli
->tree
));
257 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
258 total
, bsize
, avail
);
263 /****************************************************************************
265 ****************************************************************************/
266 static int cmd_pwd(const char **cmd_ptr
)
268 d_printf("Current directory is %s",service
);
269 d_printf("%s\n",cur_dir
);
274 convert a string to dos format
276 static void dos_format(char *s
)
278 string_replace(s
, '/', '\\');
281 /****************************************************************************
282 change directory - inner section
283 ****************************************************************************/
284 static int do_cd(char *newdir
)
292 /* Save the current directory in case the
293 new directory is invalid */
294 pstrcpy(saved_dir
, cur_dir
);
299 if (*(cur_dir
+strlen(cur_dir
)-1) != '\\') {
300 pstrcat(cur_dir
, "\\");
302 dos_clean_name(cur_dir
);
303 pstrcpy(dname
,cur_dir
);
304 pstrcat(cur_dir
,"\\");
305 dos_clean_name(cur_dir
);
307 if (!strequal(cur_dir
,"\\")) {
308 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli
->tree
, dname
))) {
309 d_printf("cd %s: %s\n", dname
, smbcli_errstr(cli
->tree
));
310 pstrcpy(cur_dir
,saved_dir
);
314 pstrcpy(cd_path
,cur_dir
);
319 /****************************************************************************
321 ****************************************************************************/
322 static int cmd_cd(const char **cmd_ptr
)
327 if (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
)))
330 d_printf("Current directory is %s\n",cur_dir
);
336 BOOL
mask_match(struct smbcli_state
*c
, const char *string
, char *pattern
,
337 BOOL is_case_sensitive
)
341 if (strcmp(string
,"..") == 0)
343 if (strcmp(pattern
,".") == 0)
346 if (is_case_sensitive
)
347 return ms_fnmatch(pattern
, string
,
348 c
->transport
->negotiate
.protocol
) == 0;
350 fstrcpy(p2
, pattern
);
354 return ms_fnmatch(p2
, s2
, c
->transport
->negotiate
.protocol
) == 0;
359 /*******************************************************************
360 decide if a file should be operated on
361 ********************************************************************/
362 static BOOL
do_this_one(struct clilist_file_info
*finfo
)
364 if (finfo
->attrib
& FILE_ATTRIBUTE_DIRECTORY
) return(True
);
366 if (*fileselection
&&
367 !mask_match(cli
, finfo
->name
,fileselection
,False
)) {
368 DEBUG(3,("mask_match %s failed\n", finfo
->name
));
372 if (newer_than
&& finfo
->mtime
< newer_than
) {
373 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
377 if ((archive_level
==1 || archive_level
==2) && !(finfo
->attrib
& FILE_ATTRIBUTE_ARCHIVE
)) {
378 DEBUG(3,("archive %s failed\n", finfo
->name
));
385 /****************************************************************************
386 display info about a file
387 ****************************************************************************/
388 static void display_finfo(struct clilist_file_info
*finfo
)
390 if (do_this_one(finfo
)) {
391 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
392 char *astr
= attrib_string(NULL
, finfo
->attrib
);
393 d_printf(" %-30s%7.7s %8.0f %s",
397 asctime(localtime(&t
)));
398 dir_total
+= finfo
->size
;
404 /****************************************************************************
405 accumulate size of a file
406 ****************************************************************************/
407 static void do_du(struct clilist_file_info
*finfo
)
409 if (do_this_one(finfo
)) {
410 dir_total
+= finfo
->size
;
414 static BOOL do_list_recurse
;
415 static BOOL do_list_dirs
;
416 static char *do_list_queue
= 0;
417 static long do_list_queue_size
= 0;
418 static long do_list_queue_start
= 0;
419 static long do_list_queue_end
= 0;
420 static void (*do_list_fn
)(struct clilist_file_info
*);
422 /****************************************************************************
423 functions for do_list_queue
424 ****************************************************************************/
427 * The do_list_queue is a NUL-separated list of strings stored in a
428 * char*. Since this is a FIFO, we keep track of the beginning and
429 * ending locations of the data in the queue. When we overflow, we
430 * double the size of the char*. When the start of the data passes
431 * the midpoint, we move everything back. This is logically more
432 * complex than a linked list, but easier from a memory management
433 * angle. In any memory error condition, do_list_queue is reset.
434 * Functions check to ensure that do_list_queue is non-NULL before
437 static void reset_do_list_queue(void)
439 SAFE_FREE(do_list_queue
);
440 do_list_queue_size
= 0;
441 do_list_queue_start
= 0;
442 do_list_queue_end
= 0;
445 static void init_do_list_queue(void)
447 reset_do_list_queue();
448 do_list_queue_size
= 1024;
449 do_list_queue
= malloc(do_list_queue_size
);
450 if (do_list_queue
== 0) {
451 d_printf("malloc fail for size %d\n",
452 (int)do_list_queue_size
);
453 reset_do_list_queue();
455 memset(do_list_queue
, 0, do_list_queue_size
);
459 static void adjust_do_list_queue(void)
462 * If the starting point of the queue is more than half way through,
463 * move everything toward the beginning.
465 if (do_list_queue
&& (do_list_queue_start
== do_list_queue_end
))
467 DEBUG(4,("do_list_queue is empty\n"));
468 do_list_queue_start
= do_list_queue_end
= 0;
469 *do_list_queue
= '\0';
471 else if (do_list_queue_start
> (do_list_queue_size
/ 2))
473 DEBUG(4,("sliding do_list_queue backward\n"));
474 memmove(do_list_queue
,
475 do_list_queue
+ do_list_queue_start
,
476 do_list_queue_end
- do_list_queue_start
);
477 do_list_queue_end
-= do_list_queue_start
;
478 do_list_queue_start
= 0;
483 static void add_to_do_list_queue(const char* entry
)
486 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
487 while (new_end
> do_list_queue_size
)
489 do_list_queue_size
*= 2;
490 DEBUG(4,("enlarging do_list_queue to %d\n",
491 (int)do_list_queue_size
));
492 dlq
= realloc_p(do_list_queue
, char, do_list_queue_size
);
494 d_printf("failure enlarging do_list_queue to %d bytes\n",
495 (int)do_list_queue_size
);
496 reset_do_list_queue();
501 memset(do_list_queue
+ do_list_queue_size
/ 2,
502 0, do_list_queue_size
/ 2);
507 safe_strcpy(do_list_queue
+ do_list_queue_end
, entry
,
508 do_list_queue_size
- do_list_queue_end
- 1);
509 do_list_queue_end
= new_end
;
510 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
511 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
515 static char *do_list_queue_head(void)
517 return do_list_queue
+ do_list_queue_start
;
520 static void remove_do_list_queue_head(void)
522 if (do_list_queue_end
> do_list_queue_start
)
524 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
525 adjust_do_list_queue();
526 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
527 (int)do_list_queue_start
, (int)do_list_queue_end
));
531 static int do_list_queue_empty(void)
533 return (! (do_list_queue
&& *do_list_queue
));
536 /****************************************************************************
538 ****************************************************************************/
539 static void do_list_helper(struct clilist_file_info
*f
, const char *mask
, void *state
)
541 if (f
->attrib
& FILE_ATTRIBUTE_DIRECTORY
) {
542 if (do_list_dirs
&& do_this_one(f
)) {
545 if (do_list_recurse
&&
546 !strequal(f
->name
,".") &&
547 !strequal(f
->name
,"..")) {
551 pstrcpy(mask2
, mask
);
552 p
= strrchr_m(mask2
,'\\');
555 pstrcat(mask2
, f
->name
);
556 pstrcat(mask2
,"\\*");
557 add_to_do_list_queue(mask2
);
562 if (do_this_one(f
)) {
568 /****************************************************************************
569 a wrapper around smbcli_list that adds recursion
570 ****************************************************************************/
571 void do_list(const char *mask
,uint16_t attribute
,
572 void (*fn
)(struct clilist_file_info
*),BOOL rec
, BOOL dirs
)
574 static int in_do_list
= 0;
576 if (in_do_list
&& rec
)
578 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
584 do_list_recurse
= rec
;
590 init_do_list_queue();
591 add_to_do_list_queue(mask
);
593 while (! do_list_queue_empty())
596 * Need to copy head so that it doesn't become
597 * invalid inside the call to smbcli_list. This
598 * would happen if the list were expanded
600 * Fix from E. Jay Berkenbilt (ejb@ql.org)
603 pstrcpy(head
, do_list_queue_head());
604 smbcli_list(cli
->tree
, head
, attribute
, do_list_helper
, NULL
);
605 remove_do_list_queue_head();
606 if ((! do_list_queue_empty()) && (fn
== display_finfo
))
608 char* next_file
= do_list_queue_head();
610 if ((strlen(next_file
) >= 2) &&
611 (next_file
[strlen(next_file
) - 1] == '*') &&
612 (next_file
[strlen(next_file
) - 2] == '\\'))
614 save_ch
= next_file
+
615 strlen(next_file
) - 2;
618 d_printf("\n%s\n",next_file
);
628 if (smbcli_list(cli
->tree
, mask
, attribute
, do_list_helper
, NULL
) == -1)
630 d_printf("%s listing %s\n", smbcli_errstr(cli
->tree
), mask
);
635 reset_do_list_queue();
638 /****************************************************************************
639 get a directory listing
640 ****************************************************************************/
641 static int cmd_dir(const char **cmd_ptr
)
643 uint16_t attribute
= FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
650 pstrcpy(mask
,cur_dir
);
651 if(mask
[strlen(mask
)-1]!='\\')
654 if (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
662 if (cli
->tree
->session
->transport
->negotiate
.protocol
<=
670 do_list(mask
, attribute
, display_finfo
, recurse
, True
);
674 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
680 /****************************************************************************
681 get a directory listing
682 ****************************************************************************/
683 static int cmd_du(const char **cmd_ptr
)
685 uint16_t attribute
= FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
692 pstrcpy(mask
,cur_dir
);
693 if(mask
[strlen(mask
)-1]!='\\')
696 if (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
706 do_list(mask
, attribute
, do_du
, recurse
, True
);
710 d_printf("Total number of bytes: %.0f\n", dir_total
);
716 /****************************************************************************
717 get a file from rname to lname
718 ****************************************************************************/
719 static int do_get(char *rname
, const char *lname
, BOOL reget
)
721 int handle
= 0, fnum
;
722 BOOL newhandle
= False
;
724 struct timeval tp_start
;
725 int read_size
= io_bufsize
;
732 GetTimeOfDay(&tp_start
);
735 strlower(discard_const_p(char, lname
));
738 fnum
= smbcli_open(cli
->tree
, rname
, O_RDONLY
, DENY_NONE
);
741 d_printf("%s opening remote file %s\n",smbcli_errstr(cli
->tree
),rname
);
745 if(!strcmp(lname
,"-")) {
746 handle
= fileno(stdout
);
749 handle
= open(lname
, O_WRONLY
|O_CREAT
, 0644);
751 start
= lseek(handle
, 0, SEEK_END
);
753 d_printf("Error seeking local file\n");
758 handle
= open(lname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
763 d_printf("Error opening local file %s\n",lname
);
768 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli
->tree
, fnum
,
769 &attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
)) &&
770 NT_STATUS_IS_ERR(smbcli_getattrE(cli
->tree
, fnum
,
771 &attr
, &size
, NULL
, NULL
, NULL
))) {
772 d_printf("getattrib: %s\n",smbcli_errstr(cli
->tree
));
776 DEBUG(2,("getting file %s of size %.0f as %s ",
777 rname
, (double)size
, lname
));
779 if(!(data
= (uint8_t *)malloc(read_size
))) {
780 d_printf("malloc fail for size %d\n", read_size
);
781 smbcli_close(cli
->tree
, fnum
);
786 int n
= smbcli_read(cli
->tree
, fnum
, data
, nread
+ start
, read_size
);
790 if (writefile(handle
,data
, n
) != n
) {
791 d_printf("Error writing local file\n");
799 if (nread
+ start
< size
) {
800 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
801 rname
, (long)nread
));
808 if (NT_STATUS_IS_ERR(smbcli_close(cli
->tree
, fnum
))) {
809 d_printf("Error %s closing remote file\n",smbcli_errstr(cli
->tree
));
817 if (archive_level
>= 2 && (attr
& FILE_ATTRIBUTE_ARCHIVE
)) {
818 smbcli_setatr(cli
->tree
, rname
, attr
& ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE
, 0);
822 struct timeval tp_end
;
825 GetTimeOfDay(&tp_end
);
827 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
828 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
829 get_total_time_ms
+= this_time
;
830 get_total_size
+= nread
;
832 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
833 nread
/ (1.024*this_time
+ 1.0e-4),
834 get_total_size
/ (1.024*get_total_time_ms
)));
841 /****************************************************************************
843 ****************************************************************************/
844 static int cmd_get(const char **cmd_ptr
)
850 pstrcpy(rname
,cur_dir
);
853 p
= rname
+ strlen(rname
);
855 if (!next_token(cmd_ptr
,p
,NULL
,sizeof(rname
)-strlen(rname
))) {
856 d_printf("get <filename>\n");
860 dos_clean_name(rname
);
862 next_token(cmd_ptr
,lname
,NULL
,sizeof(lname
));
864 return do_get(rname
, lname
, False
);
867 /****************************************************************************
868 Put up a yes/no prompt.
869 ****************************************************************************/
870 static BOOL
yesno(char *p
)
875 if (!fgets(ans
,sizeof(ans
)-1,stdin
))
878 if (*ans
== 'y' || *ans
== 'Y')
884 /****************************************************************************
885 do a mget operation on one file
886 ****************************************************************************/
887 static void do_mget(struct clilist_file_info
*finfo
)
891 pstring saved_curdir
;
894 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
898 d_printf("mget aborted\n");
902 if (finfo
->attrib
& FILE_ATTRIBUTE_DIRECTORY
)
903 slprintf(quest
,sizeof(pstring
)-1,
904 "Get directory %s? ",finfo
->name
);
906 slprintf(quest
,sizeof(pstring
)-1,
907 "Get file %s? ",finfo
->name
);
909 if (prompt
&& !yesno(quest
)) return;
911 if (!(finfo
->attrib
& FILE_ATTRIBUTE_DIRECTORY
)) {
912 pstrcpy(rname
,cur_dir
);
913 pstrcat(rname
,finfo
->name
);
914 do_get(rname
, finfo
->name
, False
);
918 /* handle directories */
919 pstrcpy(saved_curdir
,cur_dir
);
921 pstrcat(cur_dir
,finfo
->name
);
922 pstrcat(cur_dir
,"\\");
924 string_replace(discard_const_p(char, finfo
->name
), '\\', '/');
926 strlower(discard_const_p(char, finfo
->name
));
929 if (!directory_exist(finfo
->name
) &&
930 mkdir(finfo
->name
,0777) != 0) {
931 d_printf("failed to create directory %s\n",finfo
->name
);
932 pstrcpy(cur_dir
,saved_curdir
);
936 if (chdir(finfo
->name
) != 0) {
937 d_printf("failed to chdir to directory %s\n",finfo
->name
);
938 pstrcpy(cur_dir
,saved_curdir
);
942 pstrcpy(mget_mask
,cur_dir
);
943 pstrcat(mget_mask
,"*");
945 do_list(mget_mask
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_DIRECTORY
,do_mget
,False
, True
);
947 pstrcpy(cur_dir
,saved_curdir
);
951 /****************************************************************************
952 view the file using the pager
953 ****************************************************************************/
954 static int cmd_more(const char **cmd_ptr
)
956 fstring rname
,lname
,pager_cmd
;
961 fstrcpy(rname
,cur_dir
);
964 slprintf(lname
,sizeof(lname
)-1, "%s/smbmore.XXXXXX",tmpdir());
965 fd
= smb_mkstemp(lname
);
967 d_printf("failed to create temporary file for more\n");
972 if (!next_token(cmd_ptr
,rname
+strlen(rname
),NULL
,sizeof(rname
)-strlen(rname
))) {
973 d_printf("more <filename>\n");
977 dos_clean_name(rname
);
979 rc
= do_get(rname
, lname
, False
);
981 pager
=getenv("PAGER");
983 slprintf(pager_cmd
,sizeof(pager_cmd
)-1,
984 "%s %s",(pager
? pager
:PAGER
), lname
);
993 /****************************************************************************
995 ****************************************************************************/
996 static int cmd_mget(const char **cmd_ptr
)
998 uint16_t attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
1006 attribute
|= FILE_ATTRIBUTE_DIRECTORY
;
1010 while (next_token(cmd_ptr
,p
,NULL
,sizeof(buf
))) {
1011 pstrcpy(mget_mask
,cur_dir
);
1012 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
1013 pstrcat(mget_mask
,"\\");
1016 pstrcpy(mget_mask
,p
);
1018 pstrcat(mget_mask
,p
);
1019 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
1023 pstrcpy(mget_mask
,cur_dir
);
1024 if(mget_mask
[strlen(mget_mask
)-1]!='\\')
1025 pstrcat(mget_mask
,"\\");
1026 pstrcat(mget_mask
,"*");
1027 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
1034 /****************************************************************************
1035 make a directory of name "name"
1036 ****************************************************************************/
1037 static NTSTATUS
do_mkdir(char *name
)
1041 if (NT_STATUS_IS_ERR(status
= smbcli_mkdir(cli
->tree
, name
))) {
1042 d_printf("%s making remote directory %s\n",
1043 smbcli_errstr(cli
->tree
),name
);
1050 /****************************************************************************
1051 show 8.3 name of a file
1052 ****************************************************************************/
1053 static BOOL
do_altname(char *name
)
1055 const char *altname
;
1056 if (!NT_STATUS_IS_OK(smbcli_qpathinfo_alt_name(cli
->tree
, name
, &altname
))) {
1057 d_printf("%s getting alt name for %s\n",
1058 smbcli_errstr(cli
->tree
),name
);
1061 d_printf("%s\n", altname
);
1067 /****************************************************************************
1069 ****************************************************************************/
1070 static int cmd_quit(const char **cmd_ptr
)
1072 smbcli_shutdown(cli
);
1079 /****************************************************************************
1081 ****************************************************************************/
1082 static int cmd_mkdir(const char **cmd_ptr
)
1088 pstrcpy(mask
,cur_dir
);
1090 if (!next_token(cmd_ptr
,p
,NULL
,sizeof(buf
))) {
1092 d_printf("mkdir <dirname>\n");
1103 trim_string(ddir
,".",NULL
);
1104 p
= strtok(ddir
,"/\\");
1107 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli
->tree
, ddir2
))) {
1110 pstrcat(ddir2
,"\\");
1111 p
= strtok(NULL
,"/\\");
1121 /****************************************************************************
1123 ****************************************************************************/
1124 static int cmd_altname(const char **cmd_ptr
)
1130 pstrcpy(name
,cur_dir
);
1132 if (!next_token(cmd_ptr
,p
,NULL
,sizeof(buf
))) {
1133 d_printf("altname <file>\n");
1144 /****************************************************************************
1146 ****************************************************************************/
1147 static int do_put(char *rname
, char *lname
, BOOL reput
)
1153 uint8_t *buf
= NULL
;
1154 int maxwrite
= io_bufsize
;
1157 struct timeval tp_start
;
1158 GetTimeOfDay(&tp_start
);
1161 fnum
= smbcli_open(cli
->tree
, rname
, O_RDWR
|O_CREAT
, DENY_NONE
);
1163 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli
->tree
, fnum
, NULL
, &start
, NULL
, NULL
, NULL
, NULL
, NULL
)) &&
1164 NT_STATUS_IS_ERR(smbcli_getattrE(cli
->tree
, fnum
, NULL
, &start
, NULL
, NULL
, NULL
))) {
1165 d_printf("getattrib: %s\n",smbcli_errstr(cli
->tree
));
1170 fnum
= smbcli_open(cli
->tree
, rname
, O_RDWR
|O_CREAT
|O_TRUNC
,
1175 d_printf("%s opening remote file %s\n",smbcli_errstr(cli
->tree
),rname
);
1179 /* allow files to be piped into smbclient
1182 Note that in this case this function will exit(0) rather
1184 if (!strcmp(lname
, "-")) {
1186 /* size of file is not known */
1188 f
= x_fopen(lname
,O_RDONLY
, 0);
1190 if (x_tseek(f
, start
, SEEK_SET
) == -1) {
1191 d_printf("Error seeking local file\n");
1198 d_printf("Error opening local file %s\n",lname
);
1203 DEBUG(1,("putting file %s as %s ",lname
,
1206 buf
= (uint8_t *)malloc(maxwrite
);
1208 d_printf("ERROR: Not enough memory!\n");
1211 while (!x_feof(f
)) {
1215 if ((n
= readfile(buf
,n
,f
)) < 1) {
1216 if((n
== 0) && x_feof(f
))
1217 break; /* Empty local file. */
1219 d_printf("Error reading local file: %s\n", strerror(errno
));
1224 ret
= smbcli_write(cli
->tree
, fnum
, 0, buf
, nread
+ start
, n
);
1227 d_printf("Error writing file: %s\n", smbcli_errstr(cli
->tree
));
1235 if (NT_STATUS_IS_ERR(smbcli_close(cli
->tree
, fnum
))) {
1236 d_printf("%s closing remote file %s\n",smbcli_errstr(cli
->tree
),rname
);
1250 struct timeval tp_end
;
1253 GetTimeOfDay(&tp_end
);
1255 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
1256 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
1257 put_total_time_ms
+= this_time
;
1258 put_total_size
+= nread
;
1260 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1261 nread
/ (1.024*this_time
+ 1.0e-4),
1262 put_total_size
/ (1.024*put_total_time_ms
)));
1266 smbcli_shutdown(cli
);
1275 /****************************************************************************
1277 ****************************************************************************/
1278 static int cmd_put(const char **cmd_ptr
)
1285 pstrcpy(rname
,cur_dir
);
1286 pstrcat(rname
,"\\");
1288 if (!next_token(cmd_ptr
,p
,NULL
,sizeof(buf
))) {
1289 d_printf("put <filename>\n");
1294 if (next_token(cmd_ptr
,p
,NULL
,sizeof(buf
)))
1297 pstrcat(rname
,lname
);
1299 dos_clean_name(rname
);
1301 /* allow '-' to represent stdin
1302 jdblair, 24.jun.98 */
1303 if (!file_exist(lname
) && (strcmp(lname
,"-"))) {
1304 d_printf("%s does not exist\n",lname
);
1308 return do_put(rname
, lname
, False
);
1311 /*************************************
1313 *************************************/
1315 static struct file_list
{
1316 struct file_list
*prev
, *next
;
1321 /****************************************************************************
1322 Free a file_list structure
1323 ****************************************************************************/
1325 static void free_file_list (struct file_list
* list
)
1327 struct file_list
*tmp
;
1332 DLIST_REMOVE(list
, list
);
1333 SAFE_FREE(tmp
->file_path
);
1338 /****************************************************************************
1339 seek in a directory/file list until you get something that doesn't start with
1341 ****************************************************************************/
1342 static BOOL
seek_list(struct file_list
*list
, char *name
)
1345 trim_string(list
->file_path
,"./","\n");
1346 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
1355 /****************************************************************************
1356 set the file selection mask
1357 ****************************************************************************/
1358 static int cmd_select(const char **cmd_ptr
)
1360 pstrcpy(fileselection
,"");
1361 next_token(cmd_ptr
,fileselection
,NULL
,sizeof(fileselection
));
1366 /*******************************************************************
1367 A readdir wrapper which just returns the file name.
1368 ********************************************************************/
1369 static const char *readdirname(DIR *p
)
1377 ptr
= (struct dirent
*)readdir(p
);
1381 dname
= ptr
->d_name
;
1388 #ifdef HAVE_BROKEN_READDIR
1389 /* using /usr/ucb/cc is BAD */
1395 int len
= NAMLEN(ptr
);
1396 memcpy(buf
, dname
, len
);
1404 /****************************************************************************
1405 Recursive file matching function act as find
1406 match must be always set to True when calling this function
1407 ****************************************************************************/
1408 static int file_find(struct file_list
**list
, const char *directory
,
1409 const char *expression
, BOOL match
)
1412 struct file_list
*entry
;
1413 struct stat statbuf
;
1419 dir
= opendir(directory
);
1420 if (!dir
) return -1;
1422 while ((dname
= readdirname(dir
))) {
1423 if (!strcmp("..", dname
)) continue;
1424 if (!strcmp(".", dname
)) continue;
1426 if (asprintf(&path
, "%s/%s", directory
, dname
) <= 0) {
1431 if (!match
|| !gen_fnmatch(expression
, dname
)) {
1433 ret
= stat(path
, &statbuf
);
1435 if (S_ISDIR(statbuf
.st_mode
)) {
1437 ret
= file_find(list
, path
, expression
, False
);
1440 d_printf("file_find: cannot stat file %s\n", path
);
1449 entry
= malloc_p(struct file_list
);
1451 d_printf("Out of memory in file_find\n");
1455 entry
->file_path
= path
;
1456 entry
->isdir
= isdir
;
1457 DLIST_ADD(*list
, entry
);
1467 /****************************************************************************
1469 ****************************************************************************/
1470 static int cmd_mput(const char **cmd_ptr
)
1475 while (next_token(cmd_ptr
,p
,NULL
,sizeof(buf
))) {
1477 struct file_list
*temp_list
;
1478 char *quest
, *lname
, *rname
;
1482 ret
= file_find(&file_list
, ".", p
, True
);
1484 free_file_list(file_list
);
1492 for (temp_list
= file_list
; temp_list
;
1493 temp_list
= temp_list
->next
) {
1496 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0)
1498 trim_string(lname
, "./", "/");
1500 /* check if it's a directory */
1501 if (temp_list
->isdir
) {
1502 /* if (!recurse) continue; */
1505 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) break;
1506 if (prompt
&& !yesno(quest
)) { /* No */
1507 /* Skip the directory */
1508 lname
[strlen(lname
)-1] = '/';
1509 if (!seek_list(temp_list
, lname
))
1513 if(asprintf(&rname
, "%s%s", cur_dir
, lname
) < 0) break;
1515 if (NT_STATUS_IS_ERR(smbcli_chkpath(cli
->tree
, rname
)) &&
1516 NT_STATUS_IS_ERR(do_mkdir(rname
))) {
1517 DEBUG (0, ("Unable to make dir, skipping..."));
1518 /* Skip the directory */
1519 lname
[strlen(lname
)-1] = '/';
1520 if (!seek_list(temp_list
, lname
))
1527 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) break;
1528 if (prompt
&& !yesno(quest
)) /* No */
1533 if (asprintf(&rname
, "%s%s", cur_dir
, lname
) < 0) break;
1538 do_put(rname
, lname
, False
);
1540 free_file_list(file_list
);
1550 /****************************************************************************
1552 ****************************************************************************/
1553 static int do_cancel(int job
)
1555 d_printf("REWRITE: print job cancel not implemented\n");
1560 /****************************************************************************
1562 ****************************************************************************/
1563 static int cmd_cancel(const char **cmd_ptr
)
1568 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1569 d_printf("cancel <jobid> ...\n");
1575 } while (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
)));
1581 /****************************************************************************
1583 ****************************************************************************/
1584 static int cmd_print(const char **cmd_ptr
)
1590 if (!next_token(cmd_ptr
,lname
,NULL
, sizeof(lname
))) {
1591 d_printf("print <filename>\n");
1595 pstrcpy(rname
,lname
);
1596 p
= strrchr_m(rname
,'/');
1598 slprintf(rname
, sizeof(rname
)-1, "%s-%d", p
+1, (int)getpid());
1601 if (strequal(lname
,"-")) {
1602 slprintf(rname
, sizeof(rname
)-1, "stdin-%d", (int)getpid());
1605 return do_put(rname
, lname
, False
);
1609 /****************************************************************************
1611 ****************************************************************************/
1612 static int cmd_queue(const char **cmd_ptr
)
1614 d_printf("REWRITE: print job queue not implemented\n");
1619 /****************************************************************************
1621 ****************************************************************************/
1622 static int cmd_del(const char **cmd_ptr
)
1626 uint16_t attribute
= FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
;
1629 attribute
|= FILE_ATTRIBUTE_DIRECTORY
;
1631 pstrcpy(mask
,cur_dir
);
1633 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1634 d_printf("del <filename>\n");
1639 if (NT_STATUS_IS_ERR(smbcli_unlink(cli
->tree
, mask
))) {
1640 d_printf("%s deleting remote file %s\n",smbcli_errstr(cli
->tree
),mask
);
1647 /****************************************************************************
1648 delete a whole directory tree
1649 ****************************************************************************/
1650 static int cmd_deltree(const char **cmd_ptr
)
1656 pstrcpy(dname
,cur_dir
);
1658 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1659 d_printf("deltree <dirname>\n");
1664 ret
= smbcli_deltree(cli
->tree
, dname
);
1667 printf("Failed to delete tree %s - %s\n", dname
, smbcli_errstr(cli
->tree
));
1671 printf("Deleted %d files in %s\n", ret
, dname
);
1677 /****************************************************************************
1678 show as much information as possible about a file
1679 ****************************************************************************/
1680 static int cmd_allinfo(const char **cmd_ptr
)
1685 TALLOC_CTX
*mem_ctx
;
1686 union smb_fileinfo finfo
;
1689 pstrcpy(fname
,cur_dir
);
1691 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1692 d_printf("allinfo <filename>\n");
1697 mem_ctx
= talloc_init("%s", fname
);
1699 /* first a ALL_INFO QPATHINFO */
1700 finfo
.generic
.level
= RAW_FILEINFO_ALL_INFO
;
1701 finfo
.generic
.in
.fname
= fname
;
1702 status
= smb_raw_pathinfo(cli
->tree
, mem_ctx
, &finfo
);
1703 if (!NT_STATUS_IS_OK(status
)) {
1704 d_printf("%s - %s\n", fname
, nt_errstr(status
));
1709 d_printf("\tcreate_time: %s\n", nt_time_string(mem_ctx
, finfo
.all_info
.out
.create_time
));
1710 d_printf("\taccess_time: %s\n", nt_time_string(mem_ctx
, finfo
.all_info
.out
.access_time
));
1711 d_printf("\twrite_time: %s\n", nt_time_string(mem_ctx
, finfo
.all_info
.out
.write_time
));
1712 d_printf("\tchange_time: %s\n", nt_time_string(mem_ctx
, finfo
.all_info
.out
.change_time
));
1713 d_printf("\tattrib: 0x%x\n", finfo
.all_info
.out
.attrib
);
1714 d_printf("\talloc_size: %lu\n", (unsigned long)finfo
.all_info
.out
.alloc_size
);
1715 d_printf("\tsize: %lu\n", (unsigned long)finfo
.all_info
.out
.size
);
1716 d_printf("\tnlink: %u\n", finfo
.all_info
.out
.nlink
);
1717 d_printf("\tdelete_pending: %u\n", finfo
.all_info
.out
.delete_pending
);
1718 d_printf("\tdirectory: %u\n", finfo
.all_info
.out
.directory
);
1719 d_printf("\tea_size: %u\n", finfo
.all_info
.out
.ea_size
);
1720 d_printf("\tfname: '%s'\n", finfo
.all_info
.out
.fname
.s
);
1722 /* 8.3 name if any */
1723 finfo
.generic
.level
= RAW_FILEINFO_ALT_NAME_INFO
;
1724 status
= smb_raw_pathinfo(cli
->tree
, mem_ctx
, &finfo
);
1725 if (NT_STATUS_IS_OK(status
)) {
1726 d_printf("\talt_name: %s\n", finfo
.alt_name_info
.out
.fname
.s
);
1729 /* file_id if available */
1730 finfo
.generic
.level
= RAW_FILEINFO_INTERNAL_INFORMATION
;
1731 status
= smb_raw_pathinfo(cli
->tree
, mem_ctx
, &finfo
);
1732 if (NT_STATUS_IS_OK(status
)) {
1733 d_printf("\tfile_id %.0f\n",
1734 (double)finfo
.internal_information
.out
.file_id
);
1737 /* the EAs, if any */
1738 finfo
.generic
.level
= RAW_FILEINFO_ALL_EAS
;
1739 status
= smb_raw_pathinfo(cli
->tree
, mem_ctx
, &finfo
);
1740 if (NT_STATUS_IS_OK(status
)) {
1742 for (i
=0;i
<finfo
.all_eas
.out
.num_eas
;i
++) {
1743 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i
,
1744 finfo
.all_eas
.out
.eas
[i
].flags
,
1745 finfo
.all_eas
.out
.eas
[i
].value
.length
,
1746 finfo
.all_eas
.out
.eas
[i
].name
.s
);
1750 /* streams, if available */
1751 finfo
.generic
.level
= RAW_FILEINFO_STREAM_INFO
;
1752 status
= smb_raw_pathinfo(cli
->tree
, mem_ctx
, &finfo
);
1753 if (NT_STATUS_IS_OK(status
)) {
1755 for (i
=0;i
<finfo
.stream_info
.out
.num_streams
;i
++) {
1756 d_printf("\tstream %d:\n", i
);
1757 d_printf("\t\tsize %ld\n",
1758 (long)finfo
.stream_info
.out
.streams
[i
].size
);
1759 d_printf("\t\talloc size %ld\n",
1760 (long)finfo
.stream_info
.out
.streams
[i
].alloc_size
);
1761 d_printf("\t\tname %s\n", finfo
.stream_info
.out
.streams
[i
].stream_name
.s
);
1765 /* dev/inode if available */
1766 finfo
.generic
.level
= RAW_FILEINFO_COMPRESSION_INFORMATION
;
1767 status
= smb_raw_pathinfo(cli
->tree
, mem_ctx
, &finfo
);
1768 if (NT_STATUS_IS_OK(status
)) {
1769 d_printf("\tcompressed size %ld\n", (long)finfo
.compression_info
.out
.compressed_size
);
1770 d_printf("\tformat %ld\n", (long)finfo
.compression_info
.out
.format
);
1771 d_printf("\tunit_shift %ld\n", (long)finfo
.compression_info
.out
.unit_shift
);
1772 d_printf("\tchunk_shift %ld\n", (long)finfo
.compression_info
.out
.chunk_shift
);
1773 d_printf("\tcluster_shift %ld\n", (long)finfo
.compression_info
.out
.cluster_shift
);
1776 talloc_free(mem_ctx
);
1783 /****************************************************************************
1785 ****************************************************************************/
1786 static int cmd_eainfo(const char **cmd_ptr
)
1791 TALLOC_CTX
*mem_ctx
;
1792 union smb_fileinfo finfo
;
1796 pstrcpy(fname
,cur_dir
);
1798 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1799 d_printf("eainfo <filename>\n");
1804 mem_ctx
= talloc_init("%s", fname
);
1806 finfo
.generic
.in
.fname
= fname
;
1807 finfo
.generic
.level
= RAW_FILEINFO_ALL_EAS
;
1808 status
= smb_raw_pathinfo(cli
->tree
, mem_ctx
, &finfo
);
1810 if (!NT_STATUS_IS_OK(status
)) {
1811 d_printf("RAW_FILEINFO_ALL_EAS - %s\n", nt_errstr(status
));
1812 talloc_free(mem_ctx
);
1816 d_printf("%s has %d EAs\n", fname
, finfo
.all_eas
.out
.num_eas
);
1818 for (i
=0;i
<finfo
.all_eas
.out
.num_eas
;i
++) {
1819 d_printf("\tEA[%d] flags=%d len=%d '%s'\n", i
,
1820 finfo
.all_eas
.out
.eas
[i
].flags
,
1821 finfo
.all_eas
.out
.eas
[i
].value
.length
,
1822 finfo
.all_eas
.out
.eas
[i
].name
.s
);
1825 finfo
.all_eas
.out
.eas
[i
].value
.data
,
1826 finfo
.all_eas
.out
.eas
[i
].value
.length
);
1829 talloc_free(mem_ctx
);
1835 /****************************************************************************
1836 show any ACL on a file
1837 ****************************************************************************/
1838 static int cmd_acl(const char **cmd_ptr
)
1843 TALLOC_CTX
*mem_ctx
;
1844 union smb_fileinfo query
;
1848 pstrcpy(fname
,cur_dir
);
1850 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1851 d_printf("acl <filename>\n");
1856 fnum
= smbcli_nt_create_full(cli
->tree
, fname
, 0,
1857 SEC_STD_READ_CONTROL
,
1859 NTCREATEX_SHARE_ACCESS_DELETE
|
1860 NTCREATEX_SHARE_ACCESS_READ
|
1861 NTCREATEX_SHARE_ACCESS_WRITE
,
1862 NTCREATEX_DISP_OPEN
,
1865 d_printf("%s - %s\n", fname
, smbcli_errstr(cli
->tree
));
1869 mem_ctx
= talloc_init("%s", fname
);
1871 query
.query_secdesc
.level
= RAW_FILEINFO_SEC_DESC
;
1872 query
.query_secdesc
.in
.fnum
= fnum
;
1873 query
.query_secdesc
.in
.secinfo_flags
= 0x7;
1875 status
= smb_raw_fileinfo(cli
->tree
, mem_ctx
, &query
);
1876 if (!NT_STATUS_IS_OK(status
)) {
1877 d_printf("%s - %s\n", fname
, nt_errstr(status
));
1882 NDR_PRINT_DEBUG(security_descriptor
, query
.query_secdesc
.out
.sd
);
1884 talloc_free(mem_ctx
);
1890 /****************************************************************************
1891 lookup a name or sid
1892 ****************************************************************************/
1893 static int cmd_lookup(const char **cmd_ptr
)
1896 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
1898 struct dom_sid
*sid
;
1900 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1901 d_printf("lookup <sid|name>\n");
1902 talloc_free(mem_ctx
);
1906 sid
= dom_sid_parse_talloc(mem_ctx
, buf
);
1909 status
= smblsa_lookup_name(cli
, buf
, mem_ctx
, &sidstr
);
1910 if (!NT_STATUS_IS_OK(status
)) {
1911 d_printf("lsa_LookupNames - %s\n", nt_errstr(status
));
1912 talloc_free(mem_ctx
);
1916 d_printf("%s\n", sidstr
);
1919 status
= smblsa_lookup_sid(cli
, buf
, mem_ctx
, &name
);
1920 if (!NT_STATUS_IS_OK(status
)) {
1921 d_printf("lsa_LookupSids - %s\n", nt_errstr(status
));
1922 talloc_free(mem_ctx
);
1926 d_printf("%s\n", name
);
1929 talloc_free(mem_ctx
);
1934 /****************************************************************************
1935 show privileges for a user
1936 ****************************************************************************/
1937 static int cmd_privileges(const char **cmd_ptr
)
1940 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
1942 struct dom_sid
*sid
;
1943 struct lsa_RightSet rights
;
1946 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1947 d_printf("privileges <sid|name>\n");
1948 talloc_free(mem_ctx
);
1952 sid
= dom_sid_parse_talloc(mem_ctx
, buf
);
1954 const char *sid_str
;
1955 status
= smblsa_lookup_name(cli
, buf
, mem_ctx
, &sid_str
);
1956 if (!NT_STATUS_IS_OK(status
)) {
1957 d_printf("lsa_LookupNames - %s\n", nt_errstr(status
));
1958 talloc_free(mem_ctx
);
1961 sid
= dom_sid_parse_talloc(mem_ctx
, sid_str
);
1964 status
= smblsa_sid_privileges(cli
, sid
, mem_ctx
, &rights
);
1965 if (!NT_STATUS_IS_OK(status
)) {
1966 d_printf("lsa_EnumAccountRights - %s\n", nt_errstr(status
));
1967 talloc_free(mem_ctx
);
1971 for (i
=0;i
<rights
.count
;i
++) {
1972 d_printf("\t%s\n", rights
.names
[i
].string
);
1975 talloc_free(mem_ctx
);
1981 /****************************************************************************
1982 add privileges for a user
1983 ****************************************************************************/
1984 static int cmd_addprivileges(const char **cmd_ptr
)
1987 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
1989 struct dom_sid
*sid
;
1990 struct lsa_RightSet rights
;
1992 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
1993 d_printf("addprivileges <sid|name> <privilege...>\n");
1994 talloc_free(mem_ctx
);
1998 sid
= dom_sid_parse_talloc(mem_ctx
, buf
);
2000 const char *sid_str
;
2001 status
= smblsa_lookup_name(cli
, buf
, mem_ctx
, &sid_str
);
2002 if (!NT_STATUS_IS_OK(status
)) {
2003 d_printf("lsa_LookupNames - %s\n", nt_errstr(status
));
2004 talloc_free(mem_ctx
);
2007 sid
= dom_sid_parse_talloc(mem_ctx
, sid_str
);
2010 ZERO_STRUCT(rights
);
2011 while (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
2012 rights
.names
= talloc_realloc(mem_ctx
, rights
.names
,
2013 struct lsa_String
, rights
.count
+1);
2014 rights
.names
[rights
.count
].string
= talloc_strdup(mem_ctx
, buf
);
2019 status
= smblsa_sid_add_privileges(cli
, sid
, mem_ctx
, &rights
);
2020 if (!NT_STATUS_IS_OK(status
)) {
2021 d_printf("lsa_AddAccountRights - %s\n", nt_errstr(status
));
2022 talloc_free(mem_ctx
);
2026 talloc_free(mem_ctx
);
2031 /****************************************************************************
2032 delete privileges for a user
2033 ****************************************************************************/
2034 static int cmd_delprivileges(const char **cmd_ptr
)
2037 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
2039 struct dom_sid
*sid
;
2040 struct lsa_RightSet rights
;
2042 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
2043 d_printf("delprivileges <sid|name> <privilege...>\n");
2044 talloc_free(mem_ctx
);
2048 sid
= dom_sid_parse_talloc(mem_ctx
, buf
);
2050 const char *sid_str
;
2051 status
= smblsa_lookup_name(cli
, buf
, mem_ctx
, &sid_str
);
2052 if (!NT_STATUS_IS_OK(status
)) {
2053 d_printf("lsa_LookupNames - %s\n", nt_errstr(status
));
2054 talloc_free(mem_ctx
);
2057 sid
= dom_sid_parse_talloc(mem_ctx
, sid_str
);
2060 ZERO_STRUCT(rights
);
2061 while (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
2062 rights
.names
= talloc_realloc(mem_ctx
, rights
.names
,
2063 struct lsa_String
, rights
.count
+1);
2064 rights
.names
[rights
.count
].string
= talloc_strdup(mem_ctx
, buf
);
2069 status
= smblsa_sid_del_privileges(cli
, sid
, mem_ctx
, &rights
);
2070 if (!NT_STATUS_IS_OK(status
)) {
2071 d_printf("lsa_RemoveAccountRights - %s\n", nt_errstr(status
));
2072 talloc_free(mem_ctx
);
2076 talloc_free(mem_ctx
);
2082 /****************************************************************************
2083 ****************************************************************************/
2084 static int cmd_open(const char **cmd_ptr
)
2089 pstrcpy(mask
,cur_dir
);
2091 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
2092 d_printf("open <filename>\n");
2097 smbcli_open(cli
->tree
, mask
, O_RDWR
, DENY_ALL
);
2103 /****************************************************************************
2105 ****************************************************************************/
2106 static int cmd_rmdir(const char **cmd_ptr
)
2111 pstrcpy(mask
,cur_dir
);
2113 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
2114 d_printf("rmdir <dirname>\n");
2119 if (NT_STATUS_IS_ERR(smbcli_rmdir(cli
->tree
, mask
))) {
2120 d_printf("%s removing remote directory file %s\n",
2121 smbcli_errstr(cli
->tree
),mask
);
2127 /****************************************************************************
2129 ****************************************************************************/
2130 static int cmd_link(const char **cmd_ptr
)
2135 if (!(cli
->transport
->negotiate
.capabilities
& CAP_UNIX
)) {
2136 d_printf("Server doesn't support UNIX CIFS calls.\n");
2140 pstrcpy(src
,cur_dir
);
2141 pstrcpy(dest
,cur_dir
);
2143 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
)) ||
2144 !next_token(cmd_ptr
,buf2
,NULL
, sizeof(buf2
))) {
2145 d_printf("link <src> <dest>\n");
2152 if (NT_STATUS_IS_ERR(smbcli_unix_hardlink(cli
->tree
, src
, dest
))) {
2153 d_printf("%s linking files (%s -> %s)\n", smbcli_errstr(cli
->tree
), src
, dest
);
2160 /****************************************************************************
2162 ****************************************************************************/
2164 static int cmd_symlink(const char **cmd_ptr
)
2169 if (!(cli
->transport
->negotiate
.capabilities
& CAP_UNIX
)) {
2170 d_printf("Server doesn't support UNIX CIFS calls.\n");
2174 pstrcpy(src
,cur_dir
);
2175 pstrcpy(dest
,cur_dir
);
2177 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
)) ||
2178 !next_token(cmd_ptr
,buf2
,NULL
, sizeof(buf2
))) {
2179 d_printf("symlink <src> <dest>\n");
2186 if (NT_STATUS_IS_ERR(smbcli_unix_symlink(cli
->tree
, src
, dest
))) {
2187 d_printf("%s symlinking files (%s -> %s)\n",
2188 smbcli_errstr(cli
->tree
), src
, dest
);
2195 /****************************************************************************
2197 ****************************************************************************/
2199 static int cmd_chmod(const char **cmd_ptr
)
2205 if (!(cli
->transport
->negotiate
.capabilities
& CAP_UNIX
)) {
2206 d_printf("Server doesn't support UNIX CIFS calls.\n");
2210 pstrcpy(src
,cur_dir
);
2212 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
)) ||
2213 !next_token(cmd_ptr
,buf2
,NULL
, sizeof(buf2
))) {
2214 d_printf("chmod mode file\n");
2218 mode
= (mode_t
)strtol(buf
, NULL
, 8);
2221 if (NT_STATUS_IS_ERR(smbcli_unix_chmod(cli
->tree
, src
, mode
))) {
2222 d_printf("%s chmod file %s 0%o\n",
2223 smbcli_errstr(cli
->tree
), src
, (uint_t
)mode
);
2230 /****************************************************************************
2232 ****************************************************************************/
2234 static int cmd_chown(const char **cmd_ptr
)
2239 fstring buf
, buf2
, buf3
;
2241 if (!(cli
->transport
->negotiate
.capabilities
& CAP_UNIX
)) {
2242 d_printf("Server doesn't support UNIX CIFS calls.\n");
2246 pstrcpy(src
,cur_dir
);
2248 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
)) ||
2249 !next_token(cmd_ptr
,buf2
,NULL
, sizeof(buf2
)) ||
2250 !next_token(cmd_ptr
,buf3
,NULL
, sizeof(buf3
))) {
2251 d_printf("chown uid gid file\n");
2255 uid
= (uid_t
)atoi(buf
);
2256 gid
= (gid_t
)atoi(buf2
);
2259 if (NT_STATUS_IS_ERR(smbcli_unix_chown(cli
->tree
, src
, uid
, gid
))) {
2260 d_printf("%s chown file %s uid=%d, gid=%d\n",
2261 smbcli_errstr(cli
->tree
), src
, (int)uid
, (int)gid
);
2268 /****************************************************************************
2270 ****************************************************************************/
2271 static int cmd_rename(const char **cmd_ptr
)
2276 pstrcpy(src
,cur_dir
);
2277 pstrcpy(dest
,cur_dir
);
2279 if (!next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
)) ||
2280 !next_token(cmd_ptr
,buf2
,NULL
, sizeof(buf2
))) {
2281 d_printf("rename <src> <dest>\n");
2288 if (NT_STATUS_IS_ERR(smbcli_rename(cli
->tree
, src
, dest
))) {
2289 d_printf("%s renaming files\n",smbcli_errstr(cli
->tree
));
2297 /****************************************************************************
2298 toggle the prompt flag
2299 ****************************************************************************/
2300 static int cmd_prompt(const char **cmd_ptr
)
2303 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
2309 /****************************************************************************
2310 set the newer than time
2311 ****************************************************************************/
2312 static int cmd_newer(const char **cmd_ptr
)
2318 ok
= next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
));
2319 if (ok
&& (stat(buf
,&sbuf
) == 0)) {
2320 newer_than
= sbuf
.st_mtime
;
2321 DEBUG(1,("Getting files newer than %s",
2322 asctime(localtime(&newer_than
))));
2327 if (ok
&& newer_than
== 0) {
2328 d_printf("Error setting newer-than time\n");
2335 /****************************************************************************
2336 set the archive level
2337 ****************************************************************************/
2338 static int cmd_archive(const char **cmd_ptr
)
2342 if (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
2343 archive_level
= atoi(buf
);
2345 d_printf("Archive level is %d\n",archive_level
);
2350 /****************************************************************************
2351 toggle the lowercaseflag
2352 ****************************************************************************/
2353 static int cmd_lowercase(const char **cmd_ptr
)
2355 lowercase
= !lowercase
;
2356 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
2364 /****************************************************************************
2365 toggle the recurse flag
2366 ****************************************************************************/
2367 static int cmd_recurse(const char **cmd_ptr
)
2370 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
2375 /****************************************************************************
2376 toggle the translate flag
2377 ****************************************************************************/
2378 static int cmd_translate(const char **cmd_ptr
)
2380 translation
= !translation
;
2381 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2382 translation
?"on":"off"));
2388 /****************************************************************************
2389 do a printmode command
2390 ****************************************************************************/
2391 static int cmd_printmode(const char **cmd_ptr
)
2396 if (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
2397 if (strequal(buf
,"text")) {
2400 if (strequal(buf
,"graphics"))
2403 printmode
= atoi(buf
);
2410 fstrcpy(mode
,"text");
2413 fstrcpy(mode
,"graphics");
2416 slprintf(mode
,sizeof(mode
)-1,"%d",printmode
);
2420 DEBUG(2,("the printmode is now %s\n",mode
));
2425 /****************************************************************************
2427 ****************************************************************************/
2428 static int cmd_lcd(const char **cmd_ptr
)
2433 if (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
)))
2435 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
2440 /****************************************************************************
2441 get a file restarting at end of local file
2442 ****************************************************************************/
2443 static int cmd_reget(const char **cmd_ptr
)
2446 pstring remote_name
;
2449 pstrcpy(remote_name
, cur_dir
);
2450 pstrcat(remote_name
, "\\");
2452 p
= remote_name
+ strlen(remote_name
);
2454 if (!next_token(cmd_ptr
, p
, NULL
, sizeof(remote_name
) - strlen(remote_name
))) {
2455 d_printf("reget <filename>\n");
2458 pstrcpy(local_name
, p
);
2459 dos_clean_name(remote_name
);
2461 next_token(cmd_ptr
, local_name
, NULL
, sizeof(local_name
));
2463 return do_get(remote_name
, local_name
, True
);
2466 /****************************************************************************
2467 put a file restarting at end of local file
2468 ****************************************************************************/
2469 static int cmd_reput(const char **cmd_ptr
)
2472 pstring remote_name
;
2476 pstrcpy(remote_name
, cur_dir
);
2477 pstrcat(remote_name
, "\\");
2479 if (!next_token(cmd_ptr
, p
, NULL
, sizeof(buf
))) {
2480 d_printf("reput <filename>\n");
2483 pstrcpy(local_name
, p
);
2485 if (!file_exist(local_name
)) {
2486 d_printf("%s does not exist\n", local_name
);
2490 if (next_token(cmd_ptr
, p
, NULL
, sizeof(buf
)))
2491 pstrcat(remote_name
, p
);
2493 pstrcat(remote_name
, local_name
);
2495 dos_clean_name(remote_name
);
2497 return do_put(remote_name
, local_name
, True
);
2502 return a string representing a share type
2504 static const char *share_type_str(uint32_t type
)
2506 switch (type
& 0xF) {
2507 case STYPE_DISKTREE
:
2522 display a list of shares from a level 1 share enum
2524 static void display_share_result(struct srvsvc_NetShareCtr1
*ctr1
)
2528 for (i
=0;i
<ctr1
->count
;i
++) {
2529 struct srvsvc_NetShareInfo1
*info
= ctr1
->array
+i
;
2531 printf("\t%-15s %-10.10s %s\n",
2533 share_type_str(info
->type
),
2540 /****************************************************************************
2541 try and browse available shares on a host
2542 ****************************************************************************/
2543 static BOOL
browse_host(const char *query_host
)
2545 struct dcerpc_pipe
*p
;
2548 struct srvsvc_NetShareEnumAll r
;
2549 uint32_t resume_handle
= 0;
2550 TALLOC_CTX
*mem_ctx
= talloc_init("browse_host");
2551 struct srvsvc_NetShareCtr1 ctr1
;
2553 binding
= talloc_asprintf(mem_ctx
, "ncacn_np:%s", query_host
);
2555 status
= dcerpc_pipe_connect(mem_ctx
, &p
, binding
,
2557 DCERPC_SRVSVC_VERSION
,
2558 cmdline_credentials
);
2559 if (!NT_STATUS_IS_OK(status
)) {
2560 d_printf("Failed to connect to %s - %s\n",
2561 binding
, nt_errstr(status
));
2562 talloc_free(mem_ctx
);
2566 r
.in
.server_unc
= talloc_asprintf(mem_ctx
,"\\\\%s",dcerpc_server_name(p
));
2568 r
.in
.ctr
.ctr1
= &ctr1
;
2569 r
.in
.max_buffer
= ~0;
2570 r
.in
.resume_handle
= &resume_handle
;
2572 d_printf("\n\tSharename Type Comment\n");
2573 d_printf("\t--------- ---- -------\n");
2577 status
= dcerpc_srvsvc_NetShareEnumAll(p
, mem_ctx
, &r
);
2579 if (NT_STATUS_IS_OK(status
) &&
2580 (W_ERROR_EQUAL(r
.out
.result
, WERR_MORE_DATA
) ||
2581 W_ERROR_IS_OK(r
.out
.result
)) &&
2583 display_share_result(r
.out
.ctr
.ctr1
);
2584 resume_handle
+= r
.out
.ctr
.ctr1
->count
;
2586 } while (NT_STATUS_IS_OK(status
) && W_ERROR_EQUAL(r
.out
.result
, WERR_MORE_DATA
));
2588 talloc_free(mem_ctx
);
2590 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(r
.out
.result
)) {
2591 d_printf("Failed NetShareEnumAll %s - %s/%s\n",
2592 binding
, nt_errstr(status
), win_errstr(r
.out
.result
));
2599 /****************************************************************************
2600 try and browse available connections on a host
2601 ****************************************************************************/
2602 static BOOL
list_servers(const char *wk_grp
)
2604 d_printf("REWRITE: list servers not implemented\n");
2608 /* Some constants for completing filename arguments */
2610 #define COMPL_NONE 0 /* No completions */
2611 #define COMPL_REMOTE 1 /* Complete remote filename */
2612 #define COMPL_LOCAL 2 /* Complete local filename */
2614 /* This defines the commands supported by this client.
2615 * NOTE: The "!" must be the last one in the list because it's fn pointer
2616 * field is NULL, and NULL in that field is used in process_tok()
2617 * (below) to indicate the end of the list. crh
2622 int (*fn
)(const char **cmd_ptr
);
2623 const char *description
;
2624 char compl_args
[2]; /* Completion argument info */
2627 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
2628 {"addprivileges",cmd_addprivileges
,"<sid|name> <privilege...> add privileges for a user",{COMPL_NONE
,COMPL_NONE
}},
2629 {"altname",cmd_altname
,"<file> show alt name",{COMPL_NONE
,COMPL_NONE
}},
2630 {"acl",cmd_acl
,"<file> show file ACL",{COMPL_NONE
,COMPL_NONE
}},
2631 {"allinfo",cmd_allinfo
,"<file> show all possible info about a file",{COMPL_NONE
,COMPL_NONE
}},
2632 {"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
}},
2633 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
2634 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
2635 {"chmod",cmd_chmod
,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE
,COMPL_REMOTE
}},
2636 {"chown",cmd_chown
,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE
,COMPL_REMOTE
}},
2637 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2638 {"delprivileges",cmd_delprivileges
,"<sid|name> <privilege...> remove privileges for a user",{COMPL_NONE
,COMPL_NONE
}},
2639 {"deltree",cmd_deltree
,"<dir> delete a whole directory tree",{COMPL_REMOTE
,COMPL_NONE
}},
2640 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
2641 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
2642 {"eainfo",cmd_eainfo
,"<file> show EA contents for a file",{COMPL_NONE
,COMPL_NONE
}},
2643 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2644 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
2645 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
2646 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
2647 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
2648 {"link",cmd_link
,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
2649 {"lookup",cmd_lookup
,"<sid|name> show SID for name or name for SID",{COMPL_NONE
,COMPL_NONE
}},
2650 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
2651 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
2652 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
2653 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
2654 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2655 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
2656 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
2657 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2658 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
2659 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
2660 {"privileges",cmd_privileges
,"<user> show privileges for a user",{COMPL_NONE
,COMPL_NONE
}},
2661 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
2662 {"printmode",cmd_printmode
,"<graphics or text> set the print mode",{COMPL_NONE
,COMPL_NONE
}},
2663 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
2664 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
2665 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
2666 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2667 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
2668 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2669 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
2670 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
2671 {"reget",cmd_reget
,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE
,COMPL_LOCAL
}},
2672 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
2673 {"reput",cmd_reput
,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL
,COMPL_REMOTE
}},
2674 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2675 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
2676 {"symlink",cmd_symlink
,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
2677 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
2679 /* Yes, this must be here, see crh's comment above. */
2680 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
2681 {NULL
,NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
2685 /*******************************************************************
2686 lookup a command string in the list of commands, including
2688 ******************************************************************/
2689 static int process_tok(fstring tok
)
2691 int i
= 0, matches
= 0;
2693 int tok_len
= strlen(tok
);
2695 while (commands
[i
].fn
!= NULL
) {
2696 if (strequal(commands
[i
].name
,tok
)) {
2700 } else if (strncasecmp(commands
[i
].name
, tok
, tok_len
) == 0) {
2709 else if (matches
== 1)
2715 /****************************************************************************
2717 ****************************************************************************/
2718 static int cmd_help(const char **cmd_ptr
)
2723 if (next_token(cmd_ptr
,buf
,NULL
,sizeof(buf
))) {
2724 if ((i
= process_tok(buf
)) >= 0)
2725 d_printf("HELP %s:\n\t%s\n\n",commands
[i
].name
,commands
[i
].description
);
2727 while (commands
[i
].description
) {
2728 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
2729 d_printf("%-15s",commands
[i
].name
);
2738 /****************************************************************************
2739 process a -c command string
2740 ****************************************************************************/
2741 static int process_command_string(char *cmd
)
2747 /* establish the connection if not already */
2750 cli
= do_connect(desthost
, service
, cmdline_credentials
);
2755 while (cmd
[0] != '\0') {
2760 if ((p
= strchr_m(cmd
, ';')) == 0) {
2761 strncpy(line
, cmd
, 999);
2765 if (p
- cmd
> 999) p
= cmd
+ 999;
2766 strncpy(line
, cmd
, p
- cmd
);
2767 line
[p
- cmd
] = '\0';
2771 /* and get the first part of the command */
2773 if (!next_token(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
2775 if ((i
= process_tok(tok
)) >= 0) {
2776 rc
= commands
[i
].fn(&ptr
);
2777 } else if (i
== -2) {
2778 d_printf("%s: command abbreviation ambiguous\n",tok
);
2780 d_printf("%s: command not found\n",tok
);
2787 #define MAX_COMPLETIONS 100
2795 } completion_remote_t
;
2797 static void completion_remote_filter(struct clilist_file_info
*f
, const char *mask
, void *state
)
2799 completion_remote_t
*info
= (completion_remote_t
*)state
;
2801 if ((info
->count
< MAX_COMPLETIONS
- 1) && (strncmp(info
->text
, f
->name
, info
->len
) == 0) && (strcmp(f
->name
, ".") != 0) && (strcmp(f
->name
, "..") != 0)) {
2802 if ((info
->dirmask
[0] == 0) && !(f
->attrib
& FILE_ATTRIBUTE_DIRECTORY
))
2803 info
->matches
[info
->count
] = strdup(f
->name
);
2807 if (info
->dirmask
[0] != 0)
2808 pstrcpy(tmp
, info
->dirmask
);
2811 pstrcat(tmp
, f
->name
);
2812 if (f
->attrib
& FILE_ATTRIBUTE_DIRECTORY
)
2814 info
->matches
[info
->count
] = strdup(tmp
);
2816 if (info
->matches
[info
->count
] == NULL
)
2818 if (f
->attrib
& FILE_ATTRIBUTE_DIRECTORY
)
2819 smb_readline_ca_char(0);
2821 if (info
->count
== 1)
2822 info
->samelen
= strlen(info
->matches
[info
->count
]);
2824 while (strncmp(info
->matches
[info
->count
], info
->matches
[info
->count
-1], info
->samelen
) != 0)
2830 static char **remote_completion(const char *text
, int len
)
2834 completion_remote_t info
= { "", NULL
, 1, 0, NULL
, 0 };
2840 if (len
>= PATH_MAX
)
2843 info
.matches
= malloc_array_p(char *, MAX_COMPLETIONS
);
2844 if (!info
.matches
) return NULL
;
2845 info
.matches
[0] = NULL
;
2847 for (i
= len
-1; i
>= 0; i
--)
2848 if ((text
[i
] == '/') || (text
[i
] == '\\'))
2850 info
.text
= text
+i
+1;
2851 info
.samelen
= info
.len
= len
-i
-1;
2854 strncpy(info
.dirmask
, text
, i
+1);
2855 info
.dirmask
[i
+1] = 0;
2856 snprintf(dirmask
, sizeof(dirmask
), "%s%*s*", cur_dir
, i
-1, text
);
2858 snprintf(dirmask
, sizeof(dirmask
), "%s*", cur_dir
);
2860 if (smbcli_list(cli
->tree
, dirmask
,
2861 FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
2862 completion_remote_filter
, &info
) < 0)
2865 if (info
.count
== 2)
2866 info
.matches
[0] = strdup(info
.matches
[1]);
2868 info
.matches
[0] = malloc(info
.samelen
+1);
2869 if (!info
.matches
[0])
2871 strncpy(info
.matches
[0], info
.matches
[1], info
.samelen
);
2872 info
.matches
[0][info
.samelen
] = 0;
2874 info
.matches
[info
.count
] = NULL
;
2875 return info
.matches
;
2878 for (i
= 0; i
< info
.count
; i
++)
2879 free(info
.matches
[i
]);
2884 static char **completion_fn(const char *text
, int start
, int end
)
2886 smb_readline_ca_char(' ');
2889 const char *buf
, *sp
;
2893 buf
= smb_readline_get_line_buffer();
2897 sp
= strchr(buf
, ' ');
2901 for (i
= 0; commands
[i
].name
; i
++)
2902 if ((strncmp(commands
[i
].name
, text
, sp
- buf
) == 0) && (commands
[i
].name
[sp
- buf
] == 0))
2904 if (commands
[i
].name
== NULL
)
2910 if (sp
== (buf
+ start
))
2911 compl_type
= commands
[i
].compl_args
[0];
2913 compl_type
= commands
[i
].compl_args
[1];
2915 if (compl_type
== COMPL_REMOTE
)
2916 return remote_completion(text
, end
- start
);
2917 else /* fall back to local filename completion */
2921 int i
, len
, samelen
= 0, count
=1;
2923 matches
= malloc_array_p(char *, MAX_COMPLETIONS
);
2924 if (!matches
) return NULL
;
2928 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
2929 if (strncmp(text
, commands
[i
].name
, len
) == 0) {
2930 matches
[count
] = strdup(commands
[i
].name
);
2931 if (!matches
[count
])
2934 samelen
= strlen(matches
[count
]);
2936 while (strncmp(matches
[count
], matches
[count
-1], samelen
) != 0)
2943 case 0: /* should never happen */
2947 matches
[0] = strdup(matches
[1]);
2950 matches
[0] = malloc(samelen
+1);
2953 strncpy(matches
[0], matches
[1], samelen
);
2954 matches
[0][samelen
] = 0;
2956 matches
[count
] = NULL
;
2969 /****************************************************************************
2970 make sure we swallow keepalives during idle time
2971 ****************************************************************************/
2972 static void readline_callback(void)
2974 static time_t last_t
;
2979 if (t
- last_t
< 5) return;
2983 smbcli_transport_process(cli
->transport
);
2986 smbcli_chkpath(cli
->tree
, "\\");
2991 /****************************************************************************
2992 process commands on stdin
2993 ****************************************************************************/
2994 static void process_stdin(void)
3004 /* display a prompt */
3005 slprintf(the_prompt
, sizeof(the_prompt
)-1, "smb: %s> ", cur_dir
);
3006 cline
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
3010 pstrcpy(line
, cline
);
3012 /* special case - first char is ! */
3018 /* and get the first part of the command */
3020 if (!next_token(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
3022 if ((i
= process_tok(tok
)) >= 0) {
3023 commands
[i
].fn(&ptr
);
3024 } else if (i
== -2) {
3025 d_printf("%s: command abbreviation ambiguous\n",tok
);
3027 d_printf("%s: command not found\n",tok
);
3033 /*****************************************************
3034 return a connection to a server
3035 *******************************************************/
3036 static struct smbcli_state
*do_connect(const char *server
, const char *share
, struct cli_credentials
*cred
)
3038 struct smbcli_state
*c
;
3041 if (strncmp(share
, "\\\\", 2) == 0 ||
3042 strncmp(share
, "//", 2) == 0) {
3043 smbcli_parse_unc(share
, NULL
, &server
, &share
);
3046 status
= smbcli_full_connection(NULL
, &c
, server
,
3048 if (!NT_STATUS_IS_OK(status
)) {
3049 d_printf("Connection to \\\\%s\\%s failed - %s\n",
3050 server
, share
, nt_errstr(status
));
3058 /****************************************************************************
3059 process commands from the client
3060 ****************************************************************************/
3061 static int process(char *base_directory
)
3065 cli
= do_connect(desthost
, service
, cmdline_credentials
);
3070 if (*base_directory
) do_cd(base_directory
);
3073 rc
= process_command_string(cmdstr
);
3078 smbcli_shutdown(cli
);
3082 /****************************************************************************
3084 ****************************************************************************/
3085 static int do_host_query(char *query_host
)
3087 browse_host(query_host
);
3088 list_servers(lp_workgroup());
3093 /****************************************************************************
3094 handle a message operation
3095 ****************************************************************************/
3096 static int do_message_op(void)
3098 struct nbt_name called
, calling
;
3099 const char *server_name
;
3101 calling
.name
= lp_netbios_name();
3102 calling
.type
= NBT_NAME_CLIENT
;
3103 calling
.scope
= NULL
;
3105 nbt_choose_called_name(NULL
, &called
, desthost
, name_type
);
3107 server_name
= dest_ip
? dest_ip
: desthost
;
3109 if (!(cli
=smbcli_state_init(NULL
)) || !smbcli_socket_connect(cli
, server_name
)) {
3110 d_printf("Connection to %s failed\n", server_name
);
3114 if (!smbcli_transport_establish(cli
, &calling
, &called
)) {
3115 d_printf("session request failed\n");
3116 smbcli_shutdown(cli
);
3121 smbcli_shutdown(cli
);
3128 * Process "-L hostname" option.
3130 * We don't actually do anything yet -- we just stash the name in a
3131 * global variable and do the query when all options have been read.
3133 static void remember_query_host(const char *arg
,
3138 while (*arg
== '\\' || *arg
== '/')
3140 pstrcpy(query_host
, arg
);
3141 if ((slash
= strchr(query_host
, '/'))
3142 || (slash
= strchr(query_host
, '\\'))) {
3148 /****************************************************************************
3150 ****************************************************************************/
3151 int main(int argc
,char *argv
[])
3153 fstring base_directory
;
3156 BOOL message
= False
;
3161 TALLOC_CTX
*mem_ctx
;
3162 struct poptOption long_options
[] = {
3165 { "message", 'M', POPT_ARG_STRING
, NULL
, 'M', "Send message", "HOST" },
3166 { "ip-address", 'I', POPT_ARG_STRING
, NULL
, 'I', "Use this IP to connect to", "IP" },
3167 { "stderr", 'E', POPT_ARG_NONE
, NULL
, 'E', "Write messages to stderr instead of stdout" },
3168 { "list", 'L', POPT_ARG_STRING
, NULL
, 'L', "Get a list of shares available on a host", "HOST" },
3169 { "terminal", 't', POPT_ARG_STRING
, NULL
, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3170 { "directory", 'D', POPT_ARG_STRING
, NULL
, 'D', "Start from directory", "DIR" },
3171 { "command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated commands" },
3172 { "send-buffer", 'b', POPT_ARG_INT
, NULL
, 'b', "Changes the transmit/send buffer", "BYTES" },
3173 { "port", 'p', POPT_ARG_INT
, &port
, 'p', "Port to connect to", "PORT" },
3175 POPT_COMMON_CONNECTION
3176 POPT_COMMON_CREDENTIALS
3182 pstrcpy(term_code
, KANJI
);
3188 *base_directory
= 0;
3190 setup_logging(argv
[0],DEBUG_STDOUT
);
3191 mem_ctx
= talloc_init("client.c/main");
3193 d_printf("\nclient.c: Not enough memory\n");
3197 if (!lp_load(dyn_CONFIGFILE
,True
,False
,False
)) {
3198 fprintf(stderr
, "%s: Can't load %s - run testparm to debug it\n",
3199 argv
[0], dyn_CONFIGFILE
);
3202 pc
= poptGetContext("smbclient", argc
, (const char **) argv
, long_options
, 0);
3203 poptSetOtherOptionHelp(pc
, "[OPTIONS] service <password>");
3205 in_client
= True
; /* Make sure that we tell lp_load we are */
3207 while ((opt
= poptGetNextOpt(pc
)) != -1) {
3210 /* Messages are sent to NetBIOS name type 0x3
3211 * (Messenger Service). Make sure we default
3212 * to port 139 instead of port 445. srl,crh
3215 pstrcpy(desthost
,poptGetOptArg(pc
));
3216 if( 0 == port
) port
= 139;
3220 dest_ip
= poptGetOptArg(pc
);
3223 setup_logging("client", DEBUG_STDERR
);
3227 remember_query_host(poptGetOptArg(pc
), query_host
);
3230 pstrcpy(term_code
, poptGetOptArg(pc
));
3233 fstrcpy(base_directory
,poptGetOptArg(pc
));
3236 io_bufsize
= MAX(1, atoi(poptGetOptArg(pc
)));
3243 smbclient_init_subsystems
;
3245 if(poptPeekArg(pc
)) {
3246 pstrcpy(service
,poptGetArg(pc
));
3247 /* Convert any '/' characters in the service name to '\' characters */
3248 string_replace(service
, '/','\\');
3250 if (count_chars(service
,'\\') < 3) {
3251 d_printf("\n%s: Not enough '\\' characters in service\n",service
);
3252 poptPrintUsage(pc
, stderr
, 0);
3257 if (poptPeekArg(pc
)) {
3258 cli_credentials_set_password(cmdline_credentials
, poptGetArg(pc
), CRED_SPECIFIED
);
3263 if (!*query_host
&& !*service
&& !message
) {
3264 poptPrintUsage(pc
, stderr
, 0);
3268 poptFreeContext(pc
);
3270 DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION_STRING
) );
3272 talloc_free(mem_ctx
);
3274 if ((p
=strchr_m(query_host
,'#'))) {
3277 sscanf(p
, "%x", &name_type
);
3281 return do_host_query(query_host
);
3285 return do_message_op();
3288 if (process(base_directory
)) {