2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Simo Sorce 2001-2002
6 Copyright (C) Jelmer Vernooij 2003
7 Copyright (C) Gerald (Jerry) Carter 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "client/client_proto.h"
26 #include "include/rpc_client.h"
31 extern BOOL AllowDebugChange
;
32 extern BOOL override_logfile
;
34 extern BOOL in_client
;
36 pstring cur_dir
= "\\";
37 static pstring cd_path
= "";
38 static pstring service
;
39 static pstring desthost
;
40 static pstring username
;
41 static pstring calling_name
;
42 static BOOL grepable
=False
;
43 static char *cmdstr
= NULL
;
45 static int io_bufsize
= 64512;
47 static int name_type
= 0x20;
48 extern int max_protocol
;
50 static int process_tok(pstring tok
);
51 static int cmd_help(void);
53 static TALLOC_CTX
*ctx
;
54 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
57 /* 30 second timeout on most commands */
58 #define CLIENT_TIMEOUT (30*1000)
59 #define SHORT_TIMEOUT (5*1000)
61 /* value for unused fid field in trans2 secondary request */
62 #define FID_UNUSED (0xFFFF)
64 time_t newer_than
= 0;
65 static int archive_level
= 0;
67 static BOOL translation
= False
;
70 /* clitar bits insert */
73 extern BOOL tar_reset
;
77 static BOOL prompt
= True
;
79 static BOOL recurse
= False
;
80 static BOOL showacls
= False
;
81 BOOL lowercase
= False
;
83 static struct in_addr dest_ip
;
85 #define SEPARATORS " \t\n\r"
87 static BOOL abort_mget
= True
;
89 static pstring fileselection
= "";
91 extern file_info def_finfo
;
94 SMB_BIG_UINT get_total_size
= 0;
95 unsigned int get_total_time_ms
= 0;
96 static SMB_BIG_UINT put_total_size
= 0;
97 static unsigned int put_total_time_ms
= 0;
100 static double dir_total
;
102 /* root cli_state connection */
104 struct cli_state
*cli
;
106 static char CLI_DIRSEP_CHAR
= '\\';
107 static char CLI_DIRSEP_STR
[] = { '\\', '\0' };
109 /****************************************************************************
110 Write to a local file with CR/LF->LF translation if appropriate. Return the
111 number taken from the buffer. This may not equal the number written.
112 ****************************************************************************/
114 static int writefile(int f
, char *b
, int n
)
124 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
127 if (write(f
, b
, 1) != 1) {
137 /****************************************************************************
138 Read from a file with LF->CR/LF translation if appropriate. Return the
139 number read. read approx n bytes.
140 ****************************************************************************/
142 static int readfile(char *b
, int n
, XFILE
*f
)
148 return x_fread(b
,1,n
,f
);
151 while (i
< (n
- 1) && (i
< BUFFER_SIZE
)) {
152 if ((c
= x_getc(f
)) == EOF
) {
156 if (c
== '\n') { /* change all LFs to CR/LF */
166 /****************************************************************************
168 ****************************************************************************/
170 static void send_message(void)
175 if (!cli_message_start(cli
, desthost
, username
, &grp_id
)) {
176 d_printf("message start: %s\n", cli_errstr(cli
));
181 d_printf("Connected. Type your message, ending it with a Control-D\n");
183 while (!feof(stdin
) && total_len
< 1600) {
184 int maxlen
= MIN(1600 - total_len
,127);
191 for (l
=0;l
<maxlen
&& (c
=fgetc(stdin
))!=EOF
;l
++) {
197 if (!cli_message_text(cli
, msg
, l
, grp_id
)) {
198 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli
));
205 if (total_len
>= 1600)
206 d_printf("the message was truncated to 1600 bytes\n");
208 d_printf("sent %d bytes\n",total_len
);
210 if (!cli_message_end(cli
, grp_id
)) {
211 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli
));
216 /****************************************************************************
217 Check the space on a device.
218 ****************************************************************************/
220 static int do_dskattr(void)
222 int total
, bsize
, avail
;
223 struct cli_state
*targetcli
;
226 if ( !cli_resolve_path( "", cli
, cur_dir
, &targetcli
, targetpath
) ) {
227 d_printf("Error in dskattr: %s\n", cli_errstr(cli
));
231 if (!cli_dskattr(targetcli
, &bsize
, &total
, &avail
)) {
232 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli
));
236 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
237 total
, bsize
, avail
);
242 /****************************************************************************
244 ****************************************************************************/
246 static int cmd_pwd(void)
248 d_printf("Current directory is %s",service
);
249 d_printf("%s\n",cur_dir
);
253 /****************************************************************************
254 Change directory - inner section.
255 ****************************************************************************/
257 static int do_cd(char *newdir
)
263 struct cli_state
*targetcli
;
264 SMB_STRUCT_STAT sbuf
;
270 /* Save the current directory in case the new directory is invalid */
272 pstrcpy(saved_dir
, cur_dir
);
274 if (*p
== CLI_DIRSEP_CHAR
) {
278 if ((cur_dir
[0] != '\0') && (*(cur_dir
+strlen(cur_dir
)-1) != CLI_DIRSEP_CHAR
)) {
279 pstrcat(cur_dir
, CLI_DIRSEP_STR
);
284 pstrcpy( dname
, cur_dir
);
286 if ( !cli_resolve_path( "", cli
, dname
, &targetcli
, targetpath
) ) {
287 d_printf("cd %s: %s\n", dname
, cli_errstr(cli
));
288 pstrcpy(cur_dir
,saved_dir
);
292 if (strequal(targetpath
,CLI_DIRSEP_STR
)) {
296 /* Use a trans2_qpathinfo to test directories for modern servers.
297 Except Win9x doesn't support the qpathinfo_basic() call..... */
299 if ( targetcli
->protocol
> PROTOCOL_LANMAN2
&& !targetcli
->win95
) {
300 if ( !cli_qpathinfo_basic( targetcli
, targetpath
, &sbuf
, &attributes
) ) {
301 d_printf("cd %s: %s\n", dname
, cli_errstr(targetcli
));
302 pstrcpy(cur_dir
,saved_dir
);
306 if ( !(attributes
&FILE_ATTRIBUTE_DIRECTORY
) ) {
307 d_printf("cd %s: not a directory\n", dname
);
308 pstrcpy(cur_dir
,saved_dir
);
312 pstrcat( targetpath
, CLI_DIRSEP_STR
);
313 clean_name( targetpath
);
315 if ( !cli_chkpath(targetcli
, targetpath
) ) {
316 d_printf("cd %s: %s\n", dname
, cli_errstr(targetcli
));
317 pstrcpy(cur_dir
,saved_dir
);
326 pstrcpy(cd_path
,cur_dir
);
330 /****************************************************************************
332 ****************************************************************************/
334 static int cmd_cd(void)
339 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)))
342 d_printf("Current directory is %s\n",cur_dir
);
347 /*******************************************************************
348 Decide if a file should be operated on.
349 ********************************************************************/
351 static BOOL
do_this_one(file_info
*finfo
)
353 if (finfo
->mode
& aDIR
)
356 if (*fileselection
&&
357 !mask_match(finfo
->name
,fileselection
,False
)) {
358 DEBUG(3,("mask_match %s failed\n", finfo
->name
));
362 if (newer_than
&& finfo
->mtime_ts
.tv_sec
< newer_than
) {
363 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
367 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& aARCH
)) {
368 DEBUG(3,("archive %s failed\n", finfo
->name
));
375 /****************************************************************************
376 Display info about a file.
377 ****************************************************************************/
379 static void display_finfo(file_info
*finfo
)
381 if (do_this_one(finfo
)) {
382 time_t t
= finfo
->mtime_ts
.tv_sec
; /* the time is assumed to be passed as GMT */
384 d_printf(" %-30s%7.7s %8.0f %s",
386 attrib_string(finfo
->mode
),
389 dir_total
+= finfo
->size
;
394 /* skip if this is . or .. */
395 if ( strequal(finfo
->name
,"..") || strequal(finfo
->name
,".") )
397 /* create absolute filename for cli_nt_create() FIXME */
398 pstrcpy( afname
, cwd
);
399 pstrcat( afname
, CLI_DIRSEP_STR
);
400 pstrcat( afname
, finfo
->name
);
401 /* print file meta date header */
402 d_printf( "FILENAME:%s\n", afname
);
403 d_printf( "MODE:%s\n", attrib_string(finfo
->mode
));
404 d_printf( "SIZE:%.0f\n", (double)finfo
->size
);
405 d_printf( "MTIME:%s", time_to_asc(t
));
406 fnum
= cli_nt_create(finfo
->cli
, afname
, CREATE_ACCESS_READ
);
408 DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
410 cli_errstr( finfo
->cli
)));
413 sd
= cli_query_secdesc(finfo
->cli
, fnum
, ctx
);
415 DEBUG( 0, ("display_finfo() failed to "
416 "get security descriptor: %s",
417 cli_errstr( finfo
->cli
)));
419 display_sec_desc(sd
);
426 /****************************************************************************
427 Accumulate size of a file.
428 ****************************************************************************/
430 static void do_du(file_info
*finfo
)
432 if (do_this_one(finfo
)) {
433 dir_total
+= finfo
->size
;
437 static BOOL do_list_recurse
;
438 static BOOL do_list_dirs
;
439 static char *do_list_queue
= 0;
440 static long do_list_queue_size
= 0;
441 static long do_list_queue_start
= 0;
442 static long do_list_queue_end
= 0;
443 static void (*do_list_fn
)(file_info
*);
445 /****************************************************************************
446 Functions for do_list_queue.
447 ****************************************************************************/
450 * The do_list_queue is a NUL-separated list of strings stored in a
451 * char*. Since this is a FIFO, we keep track of the beginning and
452 * ending locations of the data in the queue. When we overflow, we
453 * double the size of the char*. When the start of the data passes
454 * the midpoint, we move everything back. This is logically more
455 * complex than a linked list, but easier from a memory management
456 * angle. In any memory error condition, do_list_queue is reset.
457 * Functions check to ensure that do_list_queue is non-NULL before
461 static void reset_do_list_queue(void)
463 SAFE_FREE(do_list_queue
);
464 do_list_queue_size
= 0;
465 do_list_queue_start
= 0;
466 do_list_queue_end
= 0;
469 static void init_do_list_queue(void)
471 reset_do_list_queue();
472 do_list_queue_size
= 1024;
473 do_list_queue
= (char *)SMB_MALLOC(do_list_queue_size
);
474 if (do_list_queue
== 0) {
475 d_printf("malloc fail for size %d\n",
476 (int)do_list_queue_size
);
477 reset_do_list_queue();
479 memset(do_list_queue
, 0, do_list_queue_size
);
483 static void adjust_do_list_queue(void)
486 * If the starting point of the queue is more than half way through,
487 * move everything toward the beginning.
490 if (do_list_queue
== NULL
) {
491 DEBUG(4,("do_list_queue is empty\n"));
492 do_list_queue_start
= do_list_queue_end
= 0;
496 if (do_list_queue_start
== do_list_queue_end
) {
497 DEBUG(4,("do_list_queue is empty\n"));
498 do_list_queue_start
= do_list_queue_end
= 0;
499 *do_list_queue
= '\0';
500 } else if (do_list_queue_start
> (do_list_queue_size
/ 2)) {
501 DEBUG(4,("sliding do_list_queue backward\n"));
502 memmove(do_list_queue
,
503 do_list_queue
+ do_list_queue_start
,
504 do_list_queue_end
- do_list_queue_start
);
505 do_list_queue_end
-= do_list_queue_start
;
506 do_list_queue_start
= 0;
510 static void add_to_do_list_queue(const char* entry
)
512 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
513 while (new_end
> do_list_queue_size
) {
514 do_list_queue_size
*= 2;
515 DEBUG(4,("enlarging do_list_queue to %d\n",
516 (int)do_list_queue_size
));
517 do_list_queue
= (char *)SMB_REALLOC(do_list_queue
, do_list_queue_size
);
518 if (! do_list_queue
) {
519 d_printf("failure enlarging do_list_queue to %d bytes\n",
520 (int)do_list_queue_size
);
521 reset_do_list_queue();
523 memset(do_list_queue
+ do_list_queue_size
/ 2,
524 0, do_list_queue_size
/ 2);
528 safe_strcpy_base(do_list_queue
+ do_list_queue_end
,
529 entry
, do_list_queue
, do_list_queue_size
);
530 do_list_queue_end
= new_end
;
531 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
532 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
536 static char *do_list_queue_head(void)
538 return do_list_queue
+ do_list_queue_start
;
541 static void remove_do_list_queue_head(void)
543 if (do_list_queue_end
> do_list_queue_start
) {
544 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
545 adjust_do_list_queue();
546 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
547 (int)do_list_queue_start
, (int)do_list_queue_end
));
551 static int do_list_queue_empty(void)
553 return (! (do_list_queue
&& *do_list_queue
));
556 /****************************************************************************
557 A helper for do_list.
558 ****************************************************************************/
560 static void do_list_helper(const char *mntpoint
, file_info
*f
, const char *mask
, void *state
)
564 /* save the directory */
565 pstrcpy( f
->dir
, mask
);
566 if ( (dir_end
= strrchr( f
->dir
, CLI_DIRSEP_CHAR
)) != NULL
) {
570 if (f
->mode
& aDIR
) {
571 if (do_list_dirs
&& do_this_one(f
)) {
574 if (do_list_recurse
&&
575 !strequal(f
->name
,".") &&
576 !strequal(f
->name
,"..")) {
581 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
585 pstrcpy(mask2
, mntpoint
);
586 pstrcat(mask2
, mask
);
587 p
= strrchr_m(mask2
,CLI_DIRSEP_CHAR
);
591 pstrcat(mask2
, f
->name
);
592 pstrcat(mask2
,CLI_DIRSEP_STR
);
594 add_to_do_list_queue(mask2
);
599 if (do_this_one(f
)) {
604 /****************************************************************************
605 A wrapper around cli_list that adds recursion.
606 ****************************************************************************/
608 void do_list(const char *mask
,uint16 attribute
,void (*fn
)(file_info
*),BOOL rec
, BOOL dirs
)
610 static int in_do_list
= 0;
611 struct cli_state
*targetcli
;
614 if (in_do_list
&& rec
) {
615 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
621 do_list_recurse
= rec
;
626 init_do_list_queue();
627 add_to_do_list_queue(mask
);
629 while (! do_list_queue_empty()) {
631 * Need to copy head so that it doesn't become
632 * invalid inside the call to cli_list. This
633 * would happen if the list were expanded
635 * Fix from E. Jay Berkenbilt (ejb@ql.org)
638 pstrcpy(head
, do_list_queue_head());
642 if ( !cli_resolve_path( "", cli
, head
, &targetcli
, targetpath
) ) {
643 d_printf("do_list: [%s] %s\n", head
, cli_errstr(cli
));
644 remove_do_list_queue_head();
648 cli_list(targetcli
, targetpath
, attribute
, do_list_helper
, NULL
);
649 remove_do_list_queue_head();
650 if ((! do_list_queue_empty()) && (fn
== display_finfo
)) {
651 char* next_file
= do_list_queue_head();
653 if ((strlen(next_file
) >= 2) &&
654 (next_file
[strlen(next_file
) - 1] == '*') &&
655 (next_file
[strlen(next_file
) - 2] == CLI_DIRSEP_CHAR
)) {
656 save_ch
= next_file
+
657 strlen(next_file
) - 2;
659 if (showacls
) /* cwd is only used if showacls is on */
660 pstrcpy( cwd
, next_file
);
662 if (!showacls
) /* don't disturbe the showacls output */
663 d_printf("\n%s\n",next_file
);
665 *save_ch
= CLI_DIRSEP_CHAR
;
672 if ( cli_resolve_path( "", cli
, mask
, &targetcli
, targetpath
) ) {
673 if (cli_list(targetcli
, targetpath
, attribute
, do_list_helper
, NULL
) == -1)
674 d_printf("%s listing %s\n", cli_errstr(targetcli
), targetpath
);
677 d_printf("do_list: [%s] %s\n", mask
, cli_errstr(cli
));
682 reset_do_list_queue();
685 /****************************************************************************
686 Get a directory listing.
687 ****************************************************************************/
689 static int cmd_dir(void)
691 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
698 if (strcmp(cur_dir
, CLI_DIRSEP_STR
) != 0) {
699 pstrcpy(mask
,cur_dir
);
700 if ((mask
[0] != '\0') && (mask
[strlen(mask
)-1]!=CLI_DIRSEP_CHAR
))
701 pstrcat(mask
,CLI_DIRSEP_STR
);
703 pstrcpy(mask
, CLI_DIRSEP_STR
);
706 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
708 if (*p
== CLI_DIRSEP_CHAR
)
717 /* cwd is only used if showacls is on */
718 pstrcpy(cwd
, cur_dir
);
721 do_list(mask
, attribute
, display_finfo
, recurse
, True
);
725 DEBUG(3, ("Total bytes listed: %.0f\n", dir_total
));
730 /****************************************************************************
731 Get a directory listing.
732 ****************************************************************************/
734 static int cmd_du(void)
736 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
743 pstrcpy(mask
,cur_dir
);
744 if ((mask
[0] != '\0') && (mask
[strlen(mask
)-1]!=CLI_DIRSEP_CHAR
))
745 pstrcat(mask
,CLI_DIRSEP_STR
);
747 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
749 if (*p
== CLI_DIRSEP_CHAR
)
757 do_list(mask
, attribute
, do_du
, recurse
, True
);
761 d_printf("Total number of bytes: %.0f\n", dir_total
);
766 /****************************************************************************
767 Get a file from rname to lname
768 ****************************************************************************/
770 static int do_get(char *rname
, char *lname
, BOOL reget
)
772 int handle
= 0, fnum
;
773 BOOL newhandle
= False
;
775 struct timeval tp_start
;
776 int read_size
= io_bufsize
;
782 struct cli_state
*targetcli
;
790 if ( !cli_resolve_path( "", cli
, rname
, &targetcli
, targetname
) ) {
791 d_printf("Failed to open %s: %s\n", rname
, cli_errstr(cli
));
795 GetTimeOfDay(&tp_start
);
797 fnum
= cli_open(targetcli
, targetname
, O_RDONLY
, DENY_NONE
);
800 d_printf("%s opening remote file %s\n",cli_errstr(cli
),rname
);
804 if(!strcmp(lname
,"-")) {
805 handle
= fileno(stdout
);
808 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
, 0644);
810 start
= sys_lseek(handle
, 0, SEEK_END
);
812 d_printf("Error seeking local file\n");
817 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
822 d_printf("Error opening local file %s\n",lname
);
827 if (!cli_qfileinfo(targetcli
, fnum
,
828 &attr
, &size
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
829 !cli_getattrE(targetcli
, fnum
,
830 &attr
, &size
, NULL
, NULL
, NULL
)) {
831 d_printf("getattrib: %s\n",cli_errstr(targetcli
));
835 DEBUG(1,("getting file %s of size %.0f as %s ",
836 rname
, (double)size
, lname
));
838 if(!(data
= (char *)SMB_MALLOC(read_size
))) {
839 d_printf("malloc fail for size %d\n", read_size
);
840 cli_close(targetcli
, fnum
);
845 int n
= cli_read(targetcli
, fnum
, data
, nread
+ start
, read_size
);
850 if (writefile(handle
,data
, n
) != n
) {
851 d_printf("Error writing local file\n");
859 if (nread
+ start
< size
) {
860 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
861 rname
, (long)nread
));
868 if (!cli_close(targetcli
, fnum
)) {
869 d_printf("Error %s closing remote file\n",cli_errstr(cli
));
877 if (archive_level
>= 2 && (attr
& aARCH
)) {
878 cli_setatr(cli
, rname
, attr
& ~(uint16
)aARCH
, 0);
882 struct timeval tp_end
;
885 GetTimeOfDay(&tp_end
);
887 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
888 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
889 get_total_time_ms
+= this_time
;
890 get_total_size
+= nread
;
892 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
893 nread
/ (1.024*this_time
+ 1.0e-4),
894 get_total_size
/ (1.024*get_total_time_ms
)));
900 /****************************************************************************
902 ****************************************************************************/
904 static int cmd_get(void)
910 pstrcpy(rname
,cur_dir
);
911 pstrcat(rname
,CLI_DIRSEP_STR
);
913 p
= rname
+ strlen(rname
);
915 if (!next_token_nr(NULL
,p
,NULL
,sizeof(rname
)-strlen(rname
))) {
916 d_printf("get <filename>\n");
922 next_token_nr(NULL
,lname
,NULL
,sizeof(lname
));
924 return do_get(rname
, lname
, False
);
927 /****************************************************************************
928 Do an mget operation on one file.
929 ****************************************************************************/
931 static void do_mget(file_info
*finfo
)
935 pstring saved_curdir
;
938 if (strequal(finfo
->name
,".") || strequal(finfo
->name
,".."))
942 d_printf("mget aborted\n");
946 if (finfo
->mode
& aDIR
)
947 slprintf(quest
,sizeof(pstring
)-1,
948 "Get directory %s? ",finfo
->name
);
950 slprintf(quest
,sizeof(pstring
)-1,
951 "Get file %s? ",finfo
->name
);
953 if (prompt
&& !yesno(quest
))
956 if (!(finfo
->mode
& aDIR
)) {
957 pstrcpy(rname
,cur_dir
);
958 pstrcat(rname
,finfo
->name
);
959 do_get(rname
, finfo
->name
, False
);
963 /* handle directories */
964 pstrcpy(saved_curdir
,cur_dir
);
966 pstrcat(cur_dir
,finfo
->name
);
967 pstrcat(cur_dir
,CLI_DIRSEP_STR
);
969 unix_format(finfo
->name
);
971 strlower_m(finfo
->name
);
973 if (!directory_exist(finfo
->name
,NULL
) &&
974 mkdir(finfo
->name
,0777) != 0) {
975 d_printf("failed to create directory %s\n",finfo
->name
);
976 pstrcpy(cur_dir
,saved_curdir
);
980 if (chdir(finfo
->name
) != 0) {
981 d_printf("failed to chdir to directory %s\n",finfo
->name
);
982 pstrcpy(cur_dir
,saved_curdir
);
986 pstrcpy(mget_mask
,cur_dir
);
987 pstrcat(mget_mask
,"*");
989 do_list(mget_mask
, aSYSTEM
| aHIDDEN
| aDIR
,do_mget
,False
, True
);
991 pstrcpy(cur_dir
,saved_curdir
);
994 /****************************************************************************
995 View the file using the pager.
996 ****************************************************************************/
998 static int cmd_more(void)
1000 pstring rname
,lname
,pager_cmd
;
1005 pstrcpy(rname
,cur_dir
);
1006 pstrcat(rname
,CLI_DIRSEP_STR
);
1008 slprintf(lname
,sizeof(lname
)-1, "%s/smbmore.XXXXXX",tmpdir());
1009 fd
= smb_mkstemp(lname
);
1011 d_printf("failed to create temporary file for more\n");
1016 if (!next_token_nr(NULL
,rname
+strlen(rname
),NULL
,sizeof(rname
)-strlen(rname
))) {
1017 d_printf("more <filename>\n");
1023 rc
= do_get(rname
, lname
, False
);
1025 pager
=getenv("PAGER");
1027 slprintf(pager_cmd
,sizeof(pager_cmd
)-1,
1028 "%s %s",(pager
? pager
:PAGER
), lname
);
1035 /****************************************************************************
1037 ****************************************************************************/
1039 static int cmd_mget(void)
1041 uint16 attribute
= aSYSTEM
| aHIDDEN
;
1053 while (next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1054 pstrcpy(mget_mask
,cur_dir
);
1055 if ((mget_mask
[0] != '\0') && (mget_mask
[strlen(mget_mask
)-1]!=CLI_DIRSEP_CHAR
))
1056 pstrcat(mget_mask
,CLI_DIRSEP_STR
);
1058 if (*p
== CLI_DIRSEP_CHAR
)
1059 pstrcpy(mget_mask
,p
);
1061 pstrcat(mget_mask
,p
);
1062 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
1066 pstrcpy(mget_mask
,cur_dir
);
1067 if(mget_mask
[strlen(mget_mask
)-1]!=CLI_DIRSEP_CHAR
)
1068 pstrcat(mget_mask
,CLI_DIRSEP_STR
);
1069 pstrcat(mget_mask
,"*");
1070 do_list(mget_mask
, attribute
,do_mget
,False
,True
);
1076 /****************************************************************************
1077 Make a directory of name "name".
1078 ****************************************************************************/
1080 static BOOL
do_mkdir(char *name
)
1082 struct cli_state
*targetcli
;
1085 if ( !cli_resolve_path( "", cli
, name
, &targetcli
, targetname
) ) {
1086 d_printf("mkdir %s: %s\n", name
, cli_errstr(cli
));
1090 if (!cli_mkdir(targetcli
, targetname
)) {
1091 d_printf("%s making remote directory %s\n",
1092 cli_errstr(targetcli
),name
);
1099 /****************************************************************************
1100 Show 8.3 name of a file.
1101 ****************************************************************************/
1103 static BOOL
do_altname(char *name
)
1106 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli
, name
, altname
))) {
1107 d_printf("%s getting alt name for %s\n",
1108 cli_errstr(cli
),name
);
1111 d_printf("%s\n", altname
);
1116 /****************************************************************************
1118 ****************************************************************************/
1120 static int cmd_quit(void)
1123 talloc_destroy( ctx
);
1129 /****************************************************************************
1131 ****************************************************************************/
1133 static int cmd_mkdir(void)
1139 pstrcpy(mask
,cur_dir
);
1141 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1143 d_printf("mkdir <dirname>\n");
1151 struct cli_state
*targetcli
;
1155 if ( !cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
) ) {
1159 pstrcpy(ddir
,targetname
);
1160 trim_char(ddir
,'.','\0');
1161 p
= strtok(ddir
,"/\\");
1164 if (!cli_chkpath(targetcli
, ddir2
)) {
1167 pstrcat(ddir2
,CLI_DIRSEP_STR
);
1168 p
= strtok(NULL
,"/\\");
1177 /****************************************************************************
1179 ****************************************************************************/
1181 static int cmd_altname(void)
1187 pstrcpy(name
,cur_dir
);
1189 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1190 d_printf("altname <file>\n");
1200 /****************************************************************************
1202 ****************************************************************************/
1204 static int do_put(char *rname
, char *lname
, BOOL reput
)
1208 SMB_OFF_T start
= 0;
1211 int maxwrite
= io_bufsize
;
1213 struct timeval tp_start
;
1214 struct cli_state
*targetcli
;
1217 if ( !cli_resolve_path( "", cli
, rname
, &targetcli
, targetname
) ) {
1218 d_printf("Failed to open %s: %s\n", rname
, cli_errstr(cli
));
1222 GetTimeOfDay(&tp_start
);
1225 fnum
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
, DENY_NONE
);
1227 if (!cli_qfileinfo(targetcli
, fnum
, NULL
, &start
, NULL
, NULL
, NULL
, NULL
, NULL
) &&
1228 !cli_getattrE(targetcli
, fnum
, NULL
, &start
, NULL
, NULL
, NULL
)) {
1229 d_printf("getattrib: %s\n",cli_errstr(cli
));
1234 fnum
= cli_open(targetcli
, targetname
, O_RDWR
|O_CREAT
|O_TRUNC
, DENY_NONE
);
1238 d_printf("%s opening remote file %s\n",cli_errstr(targetcli
),rname
);
1242 /* allow files to be piped into smbclient
1245 Note that in this case this function will exit(0) rather
1247 if (!strcmp(lname
, "-")) {
1249 /* size of file is not known */
1251 f
= x_fopen(lname
,O_RDONLY
, 0);
1253 if (x_tseek(f
, start
, SEEK_SET
) == -1) {
1254 d_printf("Error seeking local file\n");
1261 d_printf("Error opening local file %s\n",lname
);
1265 DEBUG(1,("putting file %s as %s ",lname
,
1268 buf
= (char *)SMB_MALLOC(maxwrite
);
1270 d_printf("ERROR: Not enough memory!\n");
1273 while (!x_feof(f
)) {
1277 if ((n
= readfile(buf
,n
,f
)) < 1) {
1278 if((n
== 0) && x_feof(f
))
1279 break; /* Empty local file. */
1281 d_printf("Error reading local file: %s\n", strerror(errno
));
1286 ret
= cli_write(targetcli
, fnum
, 0, buf
, nread
+ start
, n
);
1289 d_printf("Error writing file: %s\n", cli_errstr(cli
));
1297 if (!cli_close(targetcli
, fnum
)) {
1298 d_printf("%s closing remote file %s\n",cli_errstr(cli
),rname
);
1312 struct timeval tp_end
;
1315 GetTimeOfDay(&tp_end
);
1317 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
1318 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
1319 put_total_time_ms
+= this_time
;
1320 put_total_size
+= nread
;
1322 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1323 nread
/ (1.024*this_time
+ 1.0e-4),
1324 put_total_size
/ (1.024*put_total_time_ms
)));
1335 /****************************************************************************
1337 ****************************************************************************/
1339 static int cmd_put(void)
1346 pstrcpy(rname
,cur_dir
);
1347 pstrcat(rname
,CLI_DIRSEP_STR
);
1349 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1350 d_printf("put <filename>\n");
1355 if (next_token_nr(NULL
,p
,NULL
,sizeof(buf
)))
1358 pstrcat(rname
,lname
);
1364 /* allow '-' to represent stdin
1365 jdblair, 24.jun.98 */
1366 if (!file_exist(lname
,&st
) &&
1367 (strcmp(lname
,"-"))) {
1368 d_printf("%s does not exist\n",lname
);
1373 return do_put(rname
, lname
, False
);
1376 /*************************************
1377 File list structure.
1378 *************************************/
1380 static struct file_list
{
1381 struct file_list
*prev
, *next
;
1386 /****************************************************************************
1387 Free a file_list structure.
1388 ****************************************************************************/
1390 static void free_file_list (struct file_list
*list_head
)
1392 struct file_list
*list
, *next
;
1394 for (list
= list_head
; list
; list
= next
) {
1396 DLIST_REMOVE(list_head
, list
);
1397 SAFE_FREE(list
->file_path
);
1402 /****************************************************************************
1403 Seek in a directory/file list until you get something that doesn't start with
1405 ****************************************************************************/
1407 static BOOL
seek_list(struct file_list
*list
, char *name
)
1410 trim_string(list
->file_path
,"./","\n");
1411 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
1420 /****************************************************************************
1421 Set the file selection mask.
1422 ****************************************************************************/
1424 static int cmd_select(void)
1426 pstrcpy(fileselection
,"");
1427 next_token_nr(NULL
,fileselection
,NULL
,sizeof(fileselection
));
1432 /****************************************************************************
1433 Recursive file matching function act as find
1434 match must be always set to True when calling this function
1435 ****************************************************************************/
1437 static int file_find(struct file_list
**list
, const char *directory
,
1438 const char *expression
, BOOL match
)
1440 SMB_STRUCT_DIR
*dir
;
1441 struct file_list
*entry
;
1442 struct stat statbuf
;
1448 dir
= sys_opendir(directory
);
1452 while ((dname
= readdirname(dir
))) {
1453 if (!strcmp("..", dname
))
1455 if (!strcmp(".", dname
))
1458 if (asprintf(&path
, "%s/%s", directory
, dname
) <= 0) {
1463 if (!match
|| !gen_fnmatch(expression
, dname
)) {
1465 ret
= stat(path
, &statbuf
);
1467 if (S_ISDIR(statbuf
.st_mode
)) {
1469 ret
= file_find(list
, path
, expression
, False
);
1472 d_printf("file_find: cannot stat file %s\n", path
);
1481 entry
= SMB_MALLOC_P(struct file_list
);
1483 d_printf("Out of memory in file_find\n");
1487 entry
->file_path
= path
;
1488 entry
->isdir
= isdir
;
1489 DLIST_ADD(*list
, entry
);
1499 /****************************************************************************
1501 ****************************************************************************/
1503 static int cmd_mput(void)
1508 while (next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1510 struct file_list
*temp_list
;
1511 char *quest
, *lname
, *rname
;
1515 ret
= file_find(&file_list
, ".", p
, True
);
1517 free_file_list(file_list
);
1525 for (temp_list
= file_list
; temp_list
;
1526 temp_list
= temp_list
->next
) {
1529 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0)
1531 trim_string(lname
, "./", "/");
1533 /* check if it's a directory */
1534 if (temp_list
->isdir
) {
1535 /* if (!recurse) continue; */
1538 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) break;
1539 if (prompt
&& !yesno(quest
)) { /* No */
1540 /* Skip the directory */
1541 lname
[strlen(lname
)-1] = '/';
1542 if (!seek_list(temp_list
, lname
))
1546 if(asprintf(&rname
, "%s%s", cur_dir
, lname
) < 0) break;
1548 if (!cli_chkpath(cli
, rname
) &&
1550 DEBUG (0, ("Unable to make dir, skipping..."));
1551 /* Skip the directory */
1552 lname
[strlen(lname
)-1] = '/';
1553 if (!seek_list(temp_list
, lname
))
1560 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) break;
1561 if (prompt
&& !yesno(quest
)) /* No */
1566 if (asprintf(&rname
, "%s%s", cur_dir
, lname
) < 0) break;
1571 do_put(rname
, lname
, False
);
1573 free_file_list(file_list
);
1582 /****************************************************************************
1584 ****************************************************************************/
1586 static int do_cancel(int job
)
1588 if (cli_printjob_del(cli
, job
)) {
1589 d_printf("Job %d cancelled\n",job
);
1592 d_printf("Error cancelling job %d : %s\n",job
,cli_errstr(cli
));
1597 /****************************************************************************
1599 ****************************************************************************/
1601 static int cmd_cancel(void)
1606 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1607 d_printf("cancel <jobid> ...\n");
1613 } while (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)));
1618 /****************************************************************************
1620 ****************************************************************************/
1622 static int cmd_print(void)
1628 if (!next_token_nr(NULL
,lname
,NULL
, sizeof(lname
))) {
1629 d_printf("print <filename>\n");
1633 pstrcpy(rname
,lname
);
1634 p
= strrchr_m(rname
,'/');
1636 slprintf(rname
, sizeof(rname
)-1, "%s-%d", p
+1, (int)sys_getpid());
1639 if (strequal(lname
,"-")) {
1640 slprintf(rname
, sizeof(rname
)-1, "stdin-%d", (int)sys_getpid());
1643 return do_put(rname
, lname
, False
);
1646 /****************************************************************************
1647 Show a print queue entry.
1648 ****************************************************************************/
1650 static void queue_fn(struct print_job_info
*p
)
1652 d_printf("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
);
1655 /****************************************************************************
1657 ****************************************************************************/
1659 static int cmd_queue(void)
1661 cli_print_queue(cli
, queue_fn
);
1666 /****************************************************************************
1668 ****************************************************************************/
1670 static void do_del(file_info
*finfo
)
1674 pstr_sprintf( mask
, "%s%c%s", finfo
->dir
, CLI_DIRSEP_CHAR
, finfo
->name
);
1676 if (finfo
->mode
& aDIR
)
1679 if (!cli_unlink(finfo
->cli
, mask
)) {
1680 d_printf("%s deleting remote file %s\n",cli_errstr(finfo
->cli
),mask
);
1684 /****************************************************************************
1686 ****************************************************************************/
1688 static int cmd_del(void)
1692 uint16 attribute
= aSYSTEM
| aHIDDEN
;
1697 pstrcpy(mask
,cur_dir
);
1699 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1700 d_printf("del <filename>\n");
1705 do_list(mask
, attribute
,do_del
,False
,False
);
1710 /****************************************************************************
1711 Wildcard delete some files.
1712 ****************************************************************************/
1714 static int cmd_wdel(void)
1719 struct cli_state
*targetcli
;
1722 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1723 d_printf("wdel 0x<attrib> <wcard>\n");
1727 attribute
= (uint16
)strtol(buf
, (char **)NULL
, 16);
1729 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1730 d_printf("wdel 0x<attrib> <wcard>\n");
1734 pstrcpy(mask
,cur_dir
);
1737 if ( !cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
) ) {
1738 d_printf("cmd_wdel %s: %s\n", mask
, cli_errstr(cli
));
1742 if (!cli_unlink_full(targetcli
, targetname
, attribute
)) {
1743 d_printf("%s deleting remote files %s\n",cli_errstr(targetcli
),targetname
);
1748 /****************************************************************************
1749 ****************************************************************************/
1751 static int cmd_open(void)
1755 struct cli_state
*targetcli
;
1759 pstrcpy(mask
,cur_dir
);
1761 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1762 d_printf("open <filename>\n");
1767 if ( !cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
) ) {
1768 d_printf("open %s: %s\n", mask
, cli_errstr(cli
));
1772 fnum
= cli_nt_create(targetcli
, targetname
, FILE_READ_DATA
|FILE_WRITE_DATA
);
1774 fnum
= cli_nt_create(targetcli
, targetname
, FILE_READ_DATA
);
1776 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
1778 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
1781 d_printf("open file %s: for read/write fnum %d\n", targetname
, fnum
);
1787 /****************************************************************************
1788 ****************************************************************************/
1790 static int cmd_posix_open(void)
1794 struct cli_state
*targetcli
;
1799 pstrcpy(mask
,cur_dir
);
1801 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1802 d_printf("posix_open <filename> 0<mode>\n");
1806 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1807 d_printf("posix_open <filename> 0<mode>\n");
1810 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
1812 if (!cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
)) {
1813 d_printf("posix_open %s: %s\n", mask
, cli_errstr(cli
));
1817 fnum
= cli_posix_open(targetcli
, targetname
, O_CREAT
|O_RDWR
, mode
);
1819 fnum
= cli_posix_open(targetcli
, targetname
, O_CREAT
|O_RDONLY
, mode
);
1821 d_printf("posix_open file %s: for read/write fnum %d\n", targetname
, fnum
);
1823 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
1826 d_printf("posix_open file %s: for read/write fnum %d\n", targetname
, fnum
);
1832 static int cmd_posix_mkdir(void)
1836 struct cli_state
*targetcli
;
1841 pstrcpy(mask
,cur_dir
);
1843 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1844 d_printf("posix_mkdir <filename> 0<mode>\n");
1848 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1849 d_printf("posix_mkdir <filename> 0<mode>\n");
1852 mode
= (mode_t
)strtol(buf
, (char **)NULL
, 8);
1854 if (!cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
)) {
1855 d_printf("posix_mkdir %s: %s\n", mask
, cli_errstr(cli
));
1859 fnum
= cli_posix_mkdir(targetcli
, targetname
, mode
);
1861 d_printf("Failed to open file %s. %s\n", targetname
, cli_errstr(cli
));
1863 d_printf("posix_mkdir created directory %s\n", targetname
);
1869 static int cmd_posix_unlink(void)
1873 struct cli_state
*targetcli
;
1876 pstrcpy(mask
,cur_dir
);
1878 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1879 d_printf("posix_unlink <filename>\n");
1884 if (!cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
)) {
1885 d_printf("posix_unlink %s: %s\n", mask
, cli_errstr(cli
));
1889 if (!cli_posix_unlink(targetcli
, targetname
)) {
1890 d_printf("Failed to unlink file %s. %s\n", targetname
, cli_errstr(cli
));
1892 d_printf("posix_unlink deleted file %s\n", targetname
);
1898 static int cmd_posix_rmdir(void)
1902 struct cli_state
*targetcli
;
1905 pstrcpy(mask
,cur_dir
);
1907 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1908 d_printf("posix_rmdir <filename>\n");
1913 if (!cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
)) {
1914 d_printf("posix_rmdir %s: %s\n", mask
, cli_errstr(cli
));
1918 if (!cli_posix_rmdir(targetcli
, targetname
)) {
1919 d_printf("Failed to unlink directory %s. %s\n", targetname
, cli_errstr(cli
));
1921 d_printf("posix_rmdir deleted directory %s\n", targetname
);
1927 static int cmd_close(void)
1932 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1933 d_printf("close <fnum>\n");
1938 /* We really should use the targetcli here.... */
1939 if (!cli_close(cli
, fnum
)) {
1940 d_printf("close %d: %s\n", fnum
, cli_errstr(cli
));
1946 static int cmd_posix(void)
1948 uint16 major
, minor
;
1949 uint32 caplow
, caphigh
;
1952 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1953 d_printf("Server doesn't support UNIX CIFS extensions.\n");
1957 if (!cli_unix_extensions_version(cli
, &major
, &minor
, &caplow
, &caphigh
)) {
1958 d_printf("Can't get UNIX CIFS extensions version from server.\n");
1962 d_printf("Server supports CIFS extensions %u.%u\n", (unsigned int)major
, (unsigned int)minor
);
1965 if (caplow
& CIFS_UNIX_FCNTL_LOCKS_CAP
) {
1966 pstrcat(caps
, "locks ");
1968 if (caplow
& CIFS_UNIX_POSIX_ACLS_CAP
) {
1969 pstrcat(caps
, "acls ");
1971 if (caplow
& CIFS_UNIX_XATTTR_CAP
) {
1972 pstrcat(caps
, "eas ");
1974 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
1975 pstrcat(caps
, "pathnames ");
1977 if (caplow
& CIFS_UNIX_POSIX_PATH_OPERATIONS_CAP
) {
1978 pstrcat(caps
, "posix_path_operations ");
1981 if (strlen(caps
) > 0 && caps
[strlen(caps
)-1] == ' ') {
1982 caps
[strlen(caps
)-1] = '\0';
1985 if (!cli_set_unix_extensions_capabilities(cli
, major
, minor
, caplow
, caphigh
)) {
1986 d_printf("Can't set UNIX CIFS extensions capabilities. %s.\n", cli_errstr(cli
));
1990 d_printf("Selecting server supported CIFS capabilities %s\n", caps
);
1992 if (caplow
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
1993 CLI_DIRSEP_CHAR
= '/';
1994 *CLI_DIRSEP_STR
= '/';
1995 pstrcpy(cur_dir
, CLI_DIRSEP_STR
);
2001 static int cmd_lock(void)
2004 SMB_BIG_UINT start
, len
;
2005 enum brl_type lock_type
;
2008 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2009 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2014 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2015 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2019 if (*buf
== 'r' || *buf
== 'R') {
2020 lock_type
= READ_LOCK
;
2021 } else if (*buf
== 'w' || *buf
== 'W') {
2022 lock_type
= WRITE_LOCK
;
2024 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2028 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2029 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2033 start
= (SMB_BIG_UINT
)strtol(buf
, (char **)NULL
, 16);
2035 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2036 d_printf("lock <fnum> [r|w] <hex-start> <hex-len>\n");
2040 len
= (SMB_BIG_UINT
)strtol(buf
, (char **)NULL
, 16);
2042 if (!cli_posix_lock(cli
, fnum
, start
, len
, True
, lock_type
)) {
2043 d_printf("lock failed %d: %s\n", fnum
, cli_errstr(cli
));
2049 static int cmd_unlock(void)
2052 SMB_BIG_UINT start
, len
;
2055 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2056 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2061 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2062 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2066 start
= (SMB_BIG_UINT
)strtol(buf
, (char **)NULL
, 16);
2068 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2069 d_printf("unlock <fnum> <hex-start> <hex-len>\n");
2073 len
= (SMB_BIG_UINT
)strtol(buf
, (char **)NULL
, 16);
2075 if (!cli_posix_unlock(cli
, fnum
, start
, len
)) {
2076 d_printf("unlock failed %d: %s\n", fnum
, cli_errstr(cli
));
2083 /****************************************************************************
2085 ****************************************************************************/
2087 static int cmd_rmdir(void)
2091 struct cli_state
*targetcli
;
2094 pstrcpy(mask
,cur_dir
);
2096 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2097 d_printf("rmdir <dirname>\n");
2102 if ( !cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
) ) {
2103 d_printf("rmdir %s: %s\n", mask
, cli_errstr(cli
));
2107 if (!cli_rmdir(targetcli
, targetname
)) {
2108 d_printf("%s removing remote directory file %s\n",
2109 cli_errstr(targetcli
),mask
);
2115 /****************************************************************************
2117 ****************************************************************************/
2119 static int cmd_link(void)
2121 pstring oldname
,newname
;
2123 struct cli_state
*targetcli
;
2126 pstrcpy(oldname
,cur_dir
);
2127 pstrcpy(newname
,cur_dir
);
2129 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2130 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
2131 d_printf("link <oldname> <newname>\n");
2135 pstrcat(oldname
,buf
);
2136 pstrcat(newname
,buf2
);
2138 if ( !cli_resolve_path( "", cli
, oldname
, &targetcli
, targetname
) ) {
2139 d_printf("link %s: %s\n", oldname
, cli_errstr(cli
));
2143 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2144 d_printf("Server doesn't support UNIX CIFS calls.\n");
2148 if (!cli_unix_hardlink(targetcli
, targetname
, newname
)) {
2149 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli
), newname
, oldname
);
2156 /****************************************************************************
2158 ****************************************************************************/
2160 static int cmd_symlink(void)
2162 pstring oldname
,newname
;
2164 struct cli_state
*targetcli
;
2167 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
2168 d_printf("Server doesn't support UNIX CIFS calls.\n");
2172 pstrcpy(newname
,cur_dir
);
2174 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2175 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
2176 d_printf("symlink <oldname> <newname>\n");
2180 pstrcpy(oldname
,buf
);
2181 pstrcat(newname
,buf2
);
2183 if ( !cli_resolve_path( "", cli
, oldname
, &targetcli
, targetname
) ) {
2184 d_printf("link %s: %s\n", oldname
, cli_errstr(cli
));
2188 if (!cli_unix_symlink(targetcli
, targetname
, newname
)) {
2189 d_printf("%s symlinking files (%s -> %s)\n",
2190 cli_errstr(targetcli
), newname
, targetname
);
2197 /****************************************************************************
2199 ****************************************************************************/
2201 static int cmd_chmod(void)
2206 struct cli_state
*targetcli
;
2209 pstrcpy(src
,cur_dir
);
2211 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2212 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
2213 d_printf("chmod mode file\n");
2217 mode
= (mode_t
)strtol(buf
, NULL
, 8);
2220 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2221 d_printf("chmod %s: %s\n", src
, cli_errstr(cli
));
2225 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2226 d_printf("Server doesn't support UNIX CIFS calls.\n");
2230 if (!cli_unix_chmod(targetcli
, targetname
, mode
)) {
2231 d_printf("%s chmod file %s 0%o\n",
2232 cli_errstr(targetcli
), src
, (unsigned int)mode
);
2239 static const char *filetype_to_str(mode_t mode
)
2241 if (S_ISREG(mode
)) {
2242 return "regular file";
2243 } else if (S_ISDIR(mode
)) {
2247 if (S_ISCHR(mode
)) {
2248 return "character device";
2252 if (S_ISBLK(mode
)) {
2253 return "block device";
2257 if (S_ISFIFO(mode
)) {
2262 if (S_ISLNK(mode
)) {
2263 return "symbolic link";
2267 if (S_ISSOCK(mode
)) {
2274 static char rwx_to_str(mode_t m
, mode_t bt
, char ret
)
2283 static char *unix_mode_to_str(char *s
, mode_t m
)
2286 const char *str
= filetype_to_str(m
);
2302 *p
++ = str
[1] == 'y' ? 'l' : 's';
2309 *p
++ = rwx_to_str(m
, S_IRUSR
, 'r');
2310 *p
++ = rwx_to_str(m
, S_IWUSR
, 'w');
2311 *p
++ = rwx_to_str(m
, S_IXUSR
, 'x');
2312 *p
++ = rwx_to_str(m
, S_IRGRP
, 'r');
2313 *p
++ = rwx_to_str(m
, S_IWGRP
, 'w');
2314 *p
++ = rwx_to_str(m
, S_IXGRP
, 'x');
2315 *p
++ = rwx_to_str(m
, S_IROTH
, 'r');
2316 *p
++ = rwx_to_str(m
, S_IWOTH
, 'w');
2317 *p
++ = rwx_to_str(m
, S_IXOTH
, 'x');
2322 /****************************************************************************
2323 Utility function for UNIX getfacl.
2324 ****************************************************************************/
2326 static char *perms_to_string(fstring permstr
, unsigned char perms
)
2328 fstrcpy(permstr
, "---");
2329 if (perms
& SMB_POSIX_ACL_READ
) {
2332 if (perms
& SMB_POSIX_ACL_WRITE
) {
2335 if (perms
& SMB_POSIX_ACL_EXECUTE
) {
2341 /****************************************************************************
2343 ****************************************************************************/
2345 static int cmd_getfacl(void)
2348 uint16 major
, minor
;
2349 uint32 caplow
, caphigh
;
2350 char *retbuf
= NULL
;
2352 SMB_STRUCT_STAT sbuf
;
2353 uint16 num_file_acls
= 0;
2354 uint16 num_dir_acls
= 0;
2356 struct cli_state
*targetcli
;
2359 pstrcpy(src
,cur_dir
);
2361 if (!next_token_nr(NULL
,name
,NULL
,sizeof(name
))) {
2362 d_printf("stat file\n");
2368 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2369 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
2373 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2374 d_printf("Server doesn't support UNIX CIFS calls.\n");
2378 if (!cli_unix_extensions_version(targetcli
, &major
, &minor
, &caplow
, &caphigh
)) {
2379 d_printf("Can't get UNIX CIFS version from server.\n");
2383 if (!(caplow
& CIFS_UNIX_POSIX_ACLS_CAP
)) {
2384 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
2388 if (!cli_unix_stat(targetcli
, targetname
, &sbuf
)) {
2389 d_printf("%s getfacl doing a stat on file %s\n",
2390 cli_errstr(targetcli
), src
);
2394 if (!cli_unix_getfacl(targetcli
, targetname
, &rb_size
, &retbuf
)) {
2395 d_printf("%s getfacl file %s\n",
2396 cli_errstr(targetcli
), src
);
2400 /* ToDo : Print out the ACL values. */
2401 if (SVAL(retbuf
,0) != SMB_POSIX_ACL_VERSION
|| rb_size
< 6) {
2402 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2403 src
, (unsigned int)CVAL(retbuf
,0) );
2408 num_file_acls
= SVAL(retbuf
,2);
2409 num_dir_acls
= SVAL(retbuf
,4);
2410 if (rb_size
!= SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)) {
2411 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2413 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)),
2414 (unsigned int)rb_size
);
2420 d_printf("# file: %s\n", src
);
2421 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf
.st_uid
, (unsigned int)sbuf
.st_gid
);
2423 if (num_file_acls
== 0 && num_dir_acls
== 0) {
2424 d_printf("No acls found.\n");
2427 for (i
= 0; i
< num_file_acls
; i
++) {
2430 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
));
2431 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
2434 case SMB_POSIX_ACL_USER_OBJ
:
2437 case SMB_POSIX_ACL_USER
:
2438 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
2439 d_printf("user:%u:", uorg
);
2441 case SMB_POSIX_ACL_GROUP_OBJ
:
2442 d_printf("group::");
2444 case SMB_POSIX_ACL_GROUP
:
2445 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
2446 d_printf("group:%u", uorg
);
2448 case SMB_POSIX_ACL_MASK
:
2451 case SMB_POSIX_ACL_OTHER
:
2452 d_printf("other::");
2455 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2456 src
, (unsigned int)tagtype
);
2461 d_printf("%s\n", perms_to_string(permstring
, perms
));
2464 for (i
= 0; i
< num_dir_acls
; i
++) {
2467 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
));
2468 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
2471 case SMB_POSIX_ACL_USER_OBJ
:
2472 d_printf("default:user::");
2474 case SMB_POSIX_ACL_USER
:
2475 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
2476 d_printf("default:user:%u:", uorg
);
2478 case SMB_POSIX_ACL_GROUP_OBJ
:
2479 d_printf("default:group::");
2481 case SMB_POSIX_ACL_GROUP
:
2482 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
2483 d_printf("default:group:%u", uorg
);
2485 case SMB_POSIX_ACL_MASK
:
2486 d_printf("default:mask::");
2488 case SMB_POSIX_ACL_OTHER
:
2489 d_printf("default:other::");
2492 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2493 src
, (unsigned int)tagtype
);
2498 d_printf("%s\n", perms_to_string(permstring
, perms
));
2505 /****************************************************************************
2507 ****************************************************************************/
2509 static int cmd_stat(void)
2513 SMB_STRUCT_STAT sbuf
;
2514 struct cli_state
*targetcli
;
2518 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
2519 d_printf("Server doesn't support UNIX CIFS calls.\n");
2523 pstrcpy(src
,cur_dir
);
2525 if (!next_token_nr(NULL
,name
,NULL
,sizeof(name
))) {
2526 d_printf("stat file\n");
2533 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2534 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
2538 if (!cli_unix_stat(targetcli
, targetname
, &sbuf
)) {
2539 d_printf("%s stat file %s\n",
2540 cli_errstr(targetcli
), src
);
2544 /* Print out the stat values. */
2545 d_printf("File: %s\n", src
);
2546 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2547 (double)sbuf
.st_size
,
2548 (unsigned int)sbuf
.st_blocks
,
2549 filetype_to_str(sbuf
.st_mode
));
2551 #if defined(S_ISCHR) && defined(S_ISBLK)
2552 if (S_ISCHR(sbuf
.st_mode
) || S_ISBLK(sbuf
.st_mode
)) {
2553 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2554 (double)sbuf
.st_ino
,
2555 (unsigned int)sbuf
.st_nlink
,
2556 unix_dev_major(sbuf
.st_rdev
),
2557 unix_dev_minor(sbuf
.st_rdev
));
2560 d_printf("Inode: %.0f\tLinks: %u\n",
2561 (double)sbuf
.st_ino
,
2562 (unsigned int)sbuf
.st_nlink
);
2564 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2565 ((int)sbuf
.st_mode
& 0777),
2566 unix_mode_to_str(mode_str
, sbuf
.st_mode
),
2567 (unsigned int)sbuf
.st_uid
,
2568 (unsigned int)sbuf
.st_gid
);
2570 lt
= localtime(&sbuf
.st_atime
);
2572 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
2574 fstrcpy(mode_str
, "unknown");
2576 d_printf("Access: %s\n", mode_str
);
2578 lt
= localtime(&sbuf
.st_mtime
);
2580 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
2582 fstrcpy(mode_str
, "unknown");
2584 d_printf("Modify: %s\n", mode_str
);
2586 lt
= localtime(&sbuf
.st_ctime
);
2588 strftime(mode_str
, sizeof(mode_str
), "%Y-%m-%d %T %z", lt
);
2590 fstrcpy(mode_str
, "unknown");
2592 d_printf("Change: %s\n", mode_str
);
2598 /****************************************************************************
2600 ****************************************************************************/
2602 static int cmd_chown(void)
2607 pstring buf
, buf2
, buf3
;
2608 struct cli_state
*targetcli
;
2611 pstrcpy(src
,cur_dir
);
2613 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2614 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
)) ||
2615 !next_token_nr(NULL
,buf3
,NULL
, sizeof(buf3
))) {
2616 d_printf("chown uid gid file\n");
2620 uid
= (uid_t
)atoi(buf
);
2621 gid
= (gid_t
)atoi(buf2
);
2624 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2625 d_printf("chown %s: %s\n", src
, cli_errstr(cli
));
2629 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2630 d_printf("Server doesn't support UNIX CIFS calls.\n");
2634 if (!cli_unix_chown(targetcli
, targetname
, uid
, gid
)) {
2635 d_printf("%s chown file %s uid=%d, gid=%d\n",
2636 cli_errstr(targetcli
), src
, (int)uid
, (int)gid
);
2643 /****************************************************************************
2645 ****************************************************************************/
2647 static int cmd_rename(void)
2651 struct cli_state
*targetcli
;
2654 pstrcpy(src
,cur_dir
);
2655 pstrcpy(dest
,cur_dir
);
2657 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2658 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
2659 d_printf("rename <src> <dest>\n");
2666 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2667 d_printf("chown %s: %s\n", src
, cli_errstr(cli
));
2671 if (!cli_rename(targetcli
, targetname
, dest
)) {
2672 d_printf("%s renaming files\n",cli_errstr(targetcli
));
2679 /****************************************************************************
2680 Print the volume name.
2681 ****************************************************************************/
2683 static int cmd_volume(void)
2689 if (!cli_get_fs_volume_info(cli
, volname
, &serial_num
, &create_date
)) {
2690 d_printf("Errr %s getting volume info\n",cli_errstr(cli
));
2694 d_printf("Volume: |%s| serial number 0x%x\n", volname
, (unsigned int)serial_num
);
2698 /****************************************************************************
2699 Hard link files using the NT call.
2700 ****************************************************************************/
2702 static int cmd_hardlink(void)
2706 struct cli_state
*targetcli
;
2709 pstrcpy(src
,cur_dir
);
2710 pstrcpy(dest
,cur_dir
);
2712 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2713 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
2714 d_printf("hardlink <src> <dest>\n");
2721 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2722 d_printf("hardlink %s: %s\n", src
, cli_errstr(cli
));
2726 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2727 d_printf("Server doesn't support UNIX CIFS calls.\n");
2731 if (!cli_nt_hardlink(targetcli
, targetname
, dest
)) {
2732 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli
));
2739 /****************************************************************************
2740 Toggle the prompt flag.
2741 ****************************************************************************/
2743 static int cmd_prompt(void)
2746 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
2751 /****************************************************************************
2752 Set the newer than time.
2753 ****************************************************************************/
2755 static int cmd_newer(void)
2759 SMB_STRUCT_STAT sbuf
;
2761 ok
= next_token_nr(NULL
,buf
,NULL
,sizeof(buf
));
2762 if (ok
&& (sys_stat(buf
,&sbuf
) == 0)) {
2763 newer_than
= sbuf
.st_mtime
;
2764 DEBUG(1,("Getting files newer than %s",
2765 time_to_asc(newer_than
)));
2770 if (ok
&& newer_than
== 0) {
2771 d_printf("Error setting newer-than time\n");
2778 /****************************************************************************
2779 Set the archive level.
2780 ****************************************************************************/
2782 static int cmd_archive(void)
2786 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2787 archive_level
= atoi(buf
);
2789 d_printf("Archive level is %d\n",archive_level
);
2794 /****************************************************************************
2795 Toggle the lowercaseflag.
2796 ****************************************************************************/
2798 static int cmd_lowercase(void)
2800 lowercase
= !lowercase
;
2801 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
2806 /****************************************************************************
2807 Toggle the case sensitive flag.
2808 ****************************************************************************/
2810 static int cmd_setcase(void)
2812 BOOL orig_case_sensitive
= cli_set_case_sensitive(cli
, False
);
2814 cli_set_case_sensitive(cli
, !orig_case_sensitive
);
2815 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive
?
2821 /****************************************************************************
2822 Toggle the showacls flag.
2823 ****************************************************************************/
2825 static int cmd_showacls(void)
2827 showacls
= !showacls
;
2828 DEBUG(2,("showacls is now %s\n",showacls
?"on":"off"));
2830 if (!ctx
&& showacls
)
2831 ctx
= talloc_init("smbclient:showacls");
2833 DEBUG( 0, ("cmd_showacls() out of memory. talloc_init() failed.\n"));
2840 /****************************************************************************
2841 Toggle the recurse flag.
2842 ****************************************************************************/
2844 static int cmd_recurse(void)
2847 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
2852 /****************************************************************************
2853 Toggle the translate flag.
2854 ****************************************************************************/
2856 static int cmd_translate(void)
2858 translation
= !translation
;
2859 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2860 translation
?"on":"off"));
2865 /****************************************************************************
2867 ****************************************************************************/
2869 static int cmd_lcd(void)
2874 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)))
2876 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
2881 /****************************************************************************
2882 Get a file restarting at end of local file.
2883 ****************************************************************************/
2885 static int cmd_reget(void)
2888 pstring remote_name
;
2891 pstrcpy(remote_name
, cur_dir
);
2892 pstrcat(remote_name
, CLI_DIRSEP_STR
);
2894 p
= remote_name
+ strlen(remote_name
);
2896 if (!next_token_nr(NULL
, p
, NULL
, sizeof(remote_name
) - strlen(remote_name
))) {
2897 d_printf("reget <filename>\n");
2900 pstrcpy(local_name
, p
);
2901 clean_name(remote_name
);
2903 next_token_nr(NULL
, local_name
, NULL
, sizeof(local_name
));
2905 return do_get(remote_name
, local_name
, True
);
2908 /****************************************************************************
2909 Put a file restarting at end of local file.
2910 ****************************************************************************/
2912 static int cmd_reput(void)
2915 pstring remote_name
;
2920 pstrcpy(remote_name
, cur_dir
);
2921 pstrcat(remote_name
, CLI_DIRSEP_STR
);
2923 if (!next_token_nr(NULL
, p
, NULL
, sizeof(buf
))) {
2924 d_printf("reput <filename>\n");
2927 pstrcpy(local_name
, p
);
2929 if (!file_exist(local_name
, &st
)) {
2930 d_printf("%s does not exist\n", local_name
);
2934 if (next_token_nr(NULL
, p
, NULL
, sizeof(buf
)))
2935 pstrcat(remote_name
, p
);
2937 pstrcat(remote_name
, local_name
);
2939 clean_name(remote_name
);
2941 return do_put(remote_name
, local_name
, True
);
2944 /****************************************************************************
2946 ****************************************************************************/
2948 static void browse_fn(const char *name
, uint32 m
,
2949 const char *comment
, void *state
)
2957 case STYPE_DISKTREE
:
2958 fstrcpy(typestr
,"Disk"); break;
2960 fstrcpy(typestr
,"Printer"); break;
2962 fstrcpy(typestr
,"Device"); break;
2964 fstrcpy(typestr
,"IPC"); break;
2966 /* FIXME: If the remote machine returns non-ascii characters
2967 in any of these fields, they can corrupt the output. We
2968 should remove them. */
2970 d_printf("\t%-15s %-10.10s%s\n",
2971 name
,typestr
,comment
);
2973 d_printf ("%s|%s|%s\n",typestr
,name
,comment
);
2977 static BOOL
browse_host_rpc(BOOL sort
)
2980 struct rpc_pipe_client
*pipe_hnd
;
2981 TALLOC_CTX
*mem_ctx
;
2984 SRV_SHARE_INFO_CTR ctr
;
2987 mem_ctx
= talloc_new(NULL
);
2988 if (mem_ctx
== NULL
) {
2989 DEBUG(0, ("talloc_new failed\n"));
2993 init_enum_hnd(&enum_hnd
, 0);
2995 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SRVSVC
, &status
);
2997 if (pipe_hnd
== NULL
) {
2998 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
2999 nt_errstr(status
)));
3000 TALLOC_FREE(mem_ctx
);
3004 werr
= rpccli_srvsvc_net_share_enum(pipe_hnd
, mem_ctx
, 1, &ctr
,
3005 0xffffffff, &enum_hnd
);
3007 if (!W_ERROR_IS_OK(werr
)) {
3008 TALLOC_FREE(mem_ctx
);
3009 cli_rpc_pipe_close(pipe_hnd
);
3013 for (i
=0; i
<ctr
.num_entries
; i
++) {
3014 SRV_SHARE_INFO_1
*info
= &ctr
.share
.info1
[i
];
3015 char *name
, *comment
;
3016 name
= rpcstr_pull_unistr2_talloc(
3017 mem_ctx
, &info
->info_1_str
.uni_netname
);
3018 comment
= rpcstr_pull_unistr2_talloc(
3019 mem_ctx
, &info
->info_1_str
.uni_remark
);
3020 browse_fn(name
, info
->info_1
.type
, comment
, NULL
);
3023 TALLOC_FREE(mem_ctx
);
3024 cli_rpc_pipe_close(pipe_hnd
);
3028 /****************************************************************************
3029 Try and browse available connections on a host.
3030 ****************************************************************************/
3032 static BOOL
browse_host(BOOL sort
)
3036 d_printf("\n\tSharename Type Comment\n");
3037 d_printf("\t--------- ---- -------\n");
3040 if (browse_host_rpc(sort
)) {
3044 if((ret
= cli_RNetShareEnum(cli
, browse_fn
, NULL
)) == -1)
3045 d_printf("Error returning browse list: %s\n", cli_errstr(cli
));
3050 /****************************************************************************
3052 ****************************************************************************/
3054 static void server_fn(const char *name
, uint32 m
,
3055 const char *comment
, void *state
)
3059 d_printf("\t%-16s %s\n", name
, comment
);
3061 d_printf("%s|%s|%s\n",(char *)state
, name
, comment
);
3065 /****************************************************************************
3066 Try and browse available connections on a host.
3067 ****************************************************************************/
3069 static BOOL
list_servers(const char *wk_grp
)
3073 if (!cli
->server_domain
)
3077 d_printf("\n\tServer Comment\n");
3078 d_printf("\t--------- -------\n");
3080 fstrcpy( state
, "Server" );
3081 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
,
3085 d_printf("\n\tWorkgroup Master\n");
3086 d_printf("\t--------- -------\n");
3089 fstrcpy( state
, "Workgroup" );
3090 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
,
3095 /****************************************************************************
3096 Print or set current VUID
3097 ****************************************************************************/
3099 static int cmd_vuid(void)
3103 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
3104 d_printf("Current VUID is %d\n", cli
->vuid
);
3108 cli
->vuid
= atoi(buf
);
3112 /****************************************************************************
3113 Setup a new VUID, by issuing a session setup
3114 ****************************************************************************/
3116 static int cmd_logon(void)
3118 pstring l_username
, l_password
;
3121 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
3122 d_printf("logon <username> [<password>]\n");
3126 pstrcpy(l_username
, buf
);
3128 if (!next_token_nr(NULL
,buf2
,NULL
,sizeof(buf
)))
3130 char *pass
= getpass("Password: ");
3132 pstrcpy(l_password
, pass
);
3135 pstrcpy(l_password
, buf2
);
3137 if (!NT_STATUS_IS_OK(cli_session_setup(cli
, l_username
,
3138 l_password
, strlen(l_password
),
3139 l_password
, strlen(l_password
),
3141 d_printf("session setup failed: %s\n", cli_errstr(cli
));
3145 d_printf("Current VUID is %d\n", cli
->vuid
);
3150 /****************************************************************************
3151 list active connections
3152 ****************************************************************************/
3154 static int cmd_list_connect(void)
3161 /****************************************************************************
3162 display the current active client connection
3163 ****************************************************************************/
3165 static int cmd_show_connect( void )
3167 struct cli_state
*targetcli
;
3170 if ( !cli_resolve_path( "", cli
, cur_dir
, &targetcli
, targetpath
) ) {
3171 d_printf("showconnect %s: %s\n", cur_dir
, cli_errstr(cli
));
3175 d_printf("//%s/%s\n", targetcli
->desthost
, targetcli
->share
);
3179 /* Some constants for completing filename arguments */
3181 #define COMPL_NONE 0 /* No completions */
3182 #define COMPL_REMOTE 1 /* Complete remote filename */
3183 #define COMPL_LOCAL 2 /* Complete local filename */
3185 /* This defines the commands supported by this client.
3186 * NOTE: The "!" must be the last one in the list because it's fn pointer
3187 * field is NULL, and NULL in that field is used in process_tok()
3188 * (below) to indicate the end of the list. crh
3194 const char *description
;
3195 char compl_args
[2]; /* Completion argument info */
3197 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
3198 {"altname",cmd_altname
,"<file> show alt name",{COMPL_NONE
,COMPL_NONE
}},
3199 {"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
}},
3200 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
3201 {"cancel",cmd_cancel
,"<jobid> cancel a print queue entry",{COMPL_NONE
,COMPL_NONE
}},
3202 {"case_sensitive",cmd_setcase
,"toggle the case sensitive flag to server",{COMPL_NONE
,COMPL_NONE
}},
3203 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
3204 {"chmod",cmd_chmod
,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE
,COMPL_REMOTE
}},
3205 {"chown",cmd_chown
,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE
,COMPL_REMOTE
}},
3206 {"close",cmd_close
,"<fid> close a file given a fid",{COMPL_REMOTE
,COMPL_REMOTE
}},
3207 {"del",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
3208 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
3209 {"du",cmd_du
,"<mask> computes the total size of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
3210 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
3211 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
3212 {"getfacl",cmd_getfacl
,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE
,COMPL_LOCAL
}},
3213 {"hardlink",cmd_hardlink
,"<src> <dest> create a Windows hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
3214 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
3215 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
3216 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
3217 {"link",cmd_link
,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE
,COMPL_REMOTE
}},
3218 {"lock",cmd_lock
,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
3219 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
3220 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
3221 {"mask",cmd_select
,"<mask> mask all filenames against this",{COMPL_REMOTE
,COMPL_NONE
}},
3222 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
3223 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
3224 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
3225 {"more",cmd_more
,"<remote name> view a remote file with your pager",{COMPL_REMOTE
,COMPL_NONE
}},
3226 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
3227 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
3228 {"open",cmd_open
,"<mask> open a file",{COMPL_REMOTE
,COMPL_NONE
}},
3229 {"posix", cmd_posix
, "turn on all POSIX capabilities", {COMPL_REMOTE
,COMPL_NONE
}},
3230 {"posix_open",cmd_posix_open
,"<name> 0<mode> open_flags mode open a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
3231 {"posix_mkdir",cmd_posix_mkdir
,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
3232 {"posix_rmdir",cmd_posix_rmdir
,"<name> removes a directory using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
3233 {"posix_unlink",cmd_posix_unlink
,"<name> removes a file using POSIX interface",{COMPL_REMOTE
,COMPL_NONE
}},
3234 {"print",cmd_print
,"<file name> print a file",{COMPL_NONE
,COMPL_NONE
}},
3235 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
3236 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
3237 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
3238 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
3239 {"queue",cmd_queue
,"show the print queue",{COMPL_NONE
,COMPL_NONE
}},
3240 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
3241 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
3242 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
3243 {"reget",cmd_reget
,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE
,COMPL_LOCAL
}},
3244 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
3245 {"reput",cmd_reput
,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL
,COMPL_REMOTE
}},
3246 {"rm",cmd_del
,"<mask> delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
3247 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
3248 {"showacls",cmd_showacls
,"toggle if ACLs are shown or not",{COMPL_NONE
,COMPL_NONE
}},
3249 {"setmode",cmd_setmode
,"filename <setmode string> change modes of file",{COMPL_REMOTE
,COMPL_NONE
}},
3250 {"stat",cmd_stat
,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE
,COMPL_REMOTE
}},
3251 {"symlink",cmd_symlink
,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE
,COMPL_REMOTE
}},
3252 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
3253 {"tarmode",cmd_tarmode
,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
3254 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
3255 {"unlock",cmd_unlock
,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE
,COMPL_REMOTE
}},
3256 {"volume",cmd_volume
,"print the volume name",{COMPL_NONE
,COMPL_NONE
}},
3257 {"vuid",cmd_vuid
,"change current vuid",{COMPL_NONE
,COMPL_NONE
}},
3258 {"wdel",cmd_wdel
,"<attrib> <mask> wildcard delete all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
3259 {"logon",cmd_logon
,"establish new logon",{COMPL_NONE
,COMPL_NONE
}},
3260 {"listconnect",cmd_list_connect
,"list open connections",{COMPL_NONE
,COMPL_NONE
}},
3261 {"showconnect",cmd_show_connect
,"display the current active connection",{COMPL_NONE
,COMPL_NONE
}},
3263 /* Yes, this must be here, see crh's comment above. */
3264 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
3265 {NULL
,NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
3268 /*******************************************************************
3269 Lookup a command string in the list of commands, including
3271 ******************************************************************/
3273 static int process_tok(pstring tok
)
3275 int i
= 0, matches
= 0;
3277 int tok_len
= strlen(tok
);
3279 while (commands
[i
].fn
!= NULL
) {
3280 if (strequal(commands
[i
].name
,tok
)) {
3284 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
3293 else if (matches
== 1)
3299 /****************************************************************************
3301 ****************************************************************************/
3303 static int cmd_help(void)
3308 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
3309 if ((i
= process_tok(buf
)) >= 0)
3310 d_printf("HELP %s:\n\t%s\n\n",commands
[i
].name
,commands
[i
].description
);
3312 while (commands
[i
].description
) {
3313 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
3314 d_printf("%-15s",commands
[i
].name
);
3323 /****************************************************************************
3324 Process a -c command string.
3325 ****************************************************************************/
3327 static int process_command_string(char *cmd
)
3333 /* establish the connection if not already */
3336 cli
= cli_cm_open(desthost
, service
, True
);
3341 while (cmd
[0] != '\0') {
3346 if ((p
= strchr_m(cmd
, ';')) == 0) {
3347 strncpy(line
, cmd
, 999);
3353 strncpy(line
, cmd
, p
- cmd
);
3354 line
[p
- cmd
] = '\0';
3358 /* and get the first part of the command */
3360 if (!next_token_nr(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
3362 if ((i
= process_tok(tok
)) >= 0) {
3363 rc
= commands
[i
].fn();
3364 } else if (i
== -2) {
3365 d_printf("%s: command abbreviation ambiguous\n",tok
);
3367 d_printf("%s: command not found\n",tok
);
3374 #define MAX_COMPLETIONS 100
3382 } completion_remote_t
;
3384 static void completion_remote_filter(const char *mnt
, file_info
*f
, const char *mask
, void *state
)
3386 completion_remote_t
*info
= (completion_remote_t
*)state
;
3388 if ((info
->count
< MAX_COMPLETIONS
- 1) && (strncmp(info
->text
, f
->name
, info
->len
) == 0) && (strcmp(f
->name
, ".") != 0) && (strcmp(f
->name
, "..") != 0)) {
3389 if ((info
->dirmask
[0] == 0) && !(f
->mode
& aDIR
))
3390 info
->matches
[info
->count
] = SMB_STRDUP(f
->name
);
3394 if (info
->dirmask
[0] != 0)
3395 pstrcpy(tmp
, info
->dirmask
);
3398 pstrcat(tmp
, f
->name
);
3401 info
->matches
[info
->count
] = SMB_STRDUP(tmp
);
3403 if (info
->matches
[info
->count
] == NULL
)
3406 smb_readline_ca_char(0);
3408 if (info
->count
== 1)
3409 info
->samelen
= strlen(info
->matches
[info
->count
]);
3411 while (strncmp(info
->matches
[info
->count
], info
->matches
[info
->count
-1], info
->samelen
) != 0)
3417 static char **remote_completion(const char *text
, int len
)
3421 completion_remote_t info
= { "", NULL
, 1, 0, NULL
, 0 };
3423 /* can't have non-static intialisation on Sun CC, so do it
3429 if (len
>= MIN(PATH_MAX
,sizeof(pstring
))) {
3433 info
.matches
= SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS
);
3434 if (!info
.matches
) {
3439 * We're leaving matches[0] free to fill it later with the text to
3440 * display: Either the one single match or the longest common subset
3443 info
.matches
[0] = NULL
;
3446 for (i
= len
-1; i
>= 0; i
--) {
3447 if ((text
[i
] == '/') || (text
[i
] == CLI_DIRSEP_CHAR
)) {
3452 info
.text
= text
+i
+1;
3453 info
.samelen
= info
.len
= len
-i
-1;
3456 strncpy(info
.dirmask
, text
, i
+1);
3457 info
.dirmask
[i
+1] = 0;
3458 pstr_sprintf(dirmask
, "%s%*s*", cur_dir
, i
-1, text
);
3460 pstr_sprintf(dirmask
, "%s*", cur_dir
);
3463 if (cli_list(cli
, dirmask
, aDIR
| aSYSTEM
| aHIDDEN
, completion_remote_filter
, &info
) < 0)
3466 if (info
.count
== 1) {
3469 * No matches at all, NULL indicates there is nothing
3472 SAFE_FREE(info
.matches
[0]);
3473 SAFE_FREE(info
.matches
);
3477 if (info
.count
== 2) {
3480 * Exactly one match in matches[1], indicate this is the one
3484 info
.matches
[0] = info
.matches
[1];
3485 info
.matches
[1] = NULL
;
3487 return info
.matches
;
3491 * We got more than one possible match, set the result to the maximum
3495 info
.matches
[0] = SMB_STRNDUP(info
.matches
[1], info
.samelen
);
3496 info
.matches
[info
.count
] = NULL
;
3497 return info
.matches
;
3500 for (i
= 0; i
< info
.count
; i
++)
3501 free(info
.matches
[i
]);
3506 static char **completion_fn(const char *text
, int start
, int end
)
3508 smb_readline_ca_char(' ');
3511 const char *buf
, *sp
;
3515 buf
= smb_readline_get_line_buffer();
3519 sp
= strchr(buf
, ' ');
3523 for (i
= 0; commands
[i
].name
; i
++) {
3524 if ((strncmp(commands
[i
].name
, buf
, sp
- buf
) == 0) &&
3525 (commands
[i
].name
[sp
- buf
] == 0)) {
3529 if (commands
[i
].name
== NULL
)
3535 if (sp
== (buf
+ start
))
3536 compl_type
= commands
[i
].compl_args
[0];
3538 compl_type
= commands
[i
].compl_args
[1];
3540 if (compl_type
== COMPL_REMOTE
)
3541 return remote_completion(text
, end
- start
);
3542 else /* fall back to local filename completion */
3546 int i
, len
, samelen
= 0, count
=1;
3548 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
3555 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
3556 if (strncmp(text
, commands
[i
].name
, len
) == 0) {
3557 matches
[count
] = SMB_STRDUP(commands
[i
].name
);
3558 if (!matches
[count
])
3561 samelen
= strlen(matches
[count
]);
3563 while (strncmp(matches
[count
], matches
[count
-1], samelen
) != 0)
3570 case 0: /* should never happen */
3574 matches
[0] = SMB_STRDUP(matches
[1]);
3577 matches
[0] = (char *)SMB_MALLOC(samelen
+1);
3580 strncpy(matches
[0], matches
[1], samelen
);
3581 matches
[0][samelen
] = 0;
3583 matches
[count
] = NULL
;
3587 for (i
= 0; i
< count
; i
++)
3595 /****************************************************************************
3596 Make sure we swallow keepalives during idle time.
3597 ****************************************************************************/
3599 static void readline_callback(void)
3602 struct timeval timeout
;
3603 static time_t last_t
;
3619 FD_SET(cli
->fd
,&fds
);
3622 timeout
.tv_usec
= 0;
3623 sys_select_intr(cli
->fd
+1,&fds
,NULL
,NULL
,&timeout
);
3625 /* We deliberately use receive_smb instead of
3626 client_receive_smb as we want to receive
3627 session keepalives and then drop them here.
3629 if (FD_ISSET(cli
->fd
,&fds
)) {
3630 if (!receive_smb(cli
->fd
,cli
->inbuf
,0)) {
3631 DEBUG(0, ("Read from server failed, maybe it closed the "
3638 /* Ping the server to keep the connection alive using SMBecho. */
3640 unsigned char garbage
[16];
3641 memset(garbage
, 0xf0, sizeof(garbage
));
3642 cli_echo(cli
, garbage
, sizeof(garbage
));
3646 /****************************************************************************
3647 Process commands on stdin.
3648 ****************************************************************************/
3650 static int process_stdin(void)
3662 /* display a prompt */
3663 slprintf(the_prompt
, sizeof(the_prompt
)-1, "smb: %s> ", cur_dir
);
3664 cline
= smb_readline(the_prompt
, readline_callback
, completion_fn
);
3668 pstrcpy(line
, cline
);
3670 /* special case - first char is ! */
3676 /* and get the first part of the command */
3678 if (!next_token_nr(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
3680 if ((i
= process_tok(tok
)) >= 0) {
3681 rc
= commands
[i
].fn();
3682 } else if (i
== -2) {
3683 d_printf("%s: command abbreviation ambiguous\n",tok
);
3685 d_printf("%s: command not found\n",tok
);
3691 /****************************************************************************
3692 Process commands from the client.
3693 ****************************************************************************/
3695 static int process(char *base_directory
)
3699 cli
= cli_cm_open(desthost
, service
, True
);
3704 if (*base_directory
) {
3705 rc
= do_cd(base_directory
);
3713 rc
= process_command_string(cmdstr
);
3722 /****************************************************************************
3724 ****************************************************************************/
3726 static int do_host_query(char *query_host
)
3728 cli
= cli_cm_open(query_host
, "IPC$", True
);
3736 /* Workgroups simply don't make sense over anything
3737 else but port 139... */
3740 cli_cm_set_port( 139 );
3741 cli
= cli_cm_open(query_host
, "IPC$", True
);
3745 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3749 list_servers(lp_workgroup());
3756 /****************************************************************************
3757 Handle a tar operation.
3758 ****************************************************************************/
3760 static int do_tar_op(char *base_directory
)
3764 /* do we already have a connection? */
3766 cli
= cli_cm_open(desthost
, service
, True
);
3773 if (*base_directory
) {
3774 ret
= do_cd(base_directory
);
3788 /****************************************************************************
3789 Handle a message operation.
3790 ****************************************************************************/
3792 static int do_message_op(void)
3795 struct nmb_name called
, calling
;
3796 fstring server_name
;
3797 char name_type_hex
[10];
3800 make_nmb_name(&calling
, calling_name
, 0x0);
3801 make_nmb_name(&called
, desthost
, name_type
);
3803 fstrcpy(server_name
, desthost
);
3804 snprintf(name_type_hex
, sizeof(name_type_hex
), "#%X", name_type
);
3805 fstrcat(server_name
, name_type_hex
);
3811 /* we can only do messages over port 139 (to windows clients at least) */
3813 msg_port
= port
? port
: 139;
3815 if (!(cli
=cli_initialise()) || (cli_set_port(cli
, msg_port
) != msg_port
) ||
3816 !cli_connect(cli
, server_name
, &ip
)) {
3817 d_printf("Connection to %s failed\n", desthost
);
3821 if (!cli_session_request(cli
, &calling
, &called
)) {
3822 d_printf("session request failed\n");
3834 /****************************************************************************
3836 ****************************************************************************/
3838 int main(int argc
,char *argv
[])
3840 pstring base_directory
;
3843 BOOL message
= False
;
3845 static const char *new_name_resolve_order
= NULL
;
3849 fstring new_workgroup
;
3850 BOOL tar_opt
= False
;
3851 BOOL service_opt
= False
;
3852 struct poptOption long_options
[] = {
3855 { "name-resolve", 'R', POPT_ARG_STRING
, &new_name_resolve_order
, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3856 { "message", 'M', POPT_ARG_STRING
, NULL
, 'M', "Send message", "HOST" },
3857 { "ip-address", 'I', POPT_ARG_STRING
, NULL
, 'I', "Use this IP to connect to", "IP" },
3858 { "stderr", 'E', POPT_ARG_NONE
, NULL
, 'E', "Write messages to stderr instead of stdout" },
3859 { "list", 'L', POPT_ARG_STRING
, NULL
, 'L', "Get a list of shares available on a host", "HOST" },
3860 { "terminal", 't', POPT_ARG_STRING
, NULL
, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3861 { "max-protocol", 'm', POPT_ARG_STRING
, NULL
, 'm', "Set the max protocol level", "LEVEL" },
3862 { "tar", 'T', POPT_ARG_STRING
, NULL
, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3863 { "directory", 'D', POPT_ARG_STRING
, NULL
, 'D', "Start from directory", "DIR" },
3864 { "command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated commands" },
3865 { "send-buffer", 'b', POPT_ARG_INT
, &io_bufsize
, 'b', "Changes the transmit/send buffer", "BYTES" },
3866 { "port", 'p', POPT_ARG_INT
, &port
, 'p', "Port to connect to", "PORT" },
3867 { "grepable", 'g', POPT_ARG_NONE
, NULL
, 'g', "Produce grepable output" },
3869 POPT_COMMON_CONNECTION
3870 POPT_COMMON_CREDENTIALS
3877 pstrcpy(term_code
, KANJI
);
3883 *base_directory
= 0;
3885 /* initialize the workgroup name so we can determine whether or
3886 not it was set by a command line option */
3888 set_global_myworkgroup( "" );
3889 set_global_myname( "" );
3891 /* set default debug level to 0 regardless of what smb.conf sets */
3892 setup_logging( "smbclient", True
);
3893 DEBUGLEVEL_CLASS
[DBGC_ALL
] = 1;
3894 if ((dbf
= x_fdup(x_stderr
))) {
3895 x_setbuf( dbf
, NULL
);
3899 pc
= poptGetContext("smbclient", argc
, (const char **) argv
, long_options
, 0);
3900 poptSetOtherOptionHelp(pc
, "service <password>");
3902 in_client
= True
; /* Make sure that we tell lp_load we are */
3904 while ((opt
= poptGetNextOpt(pc
)) != -1) {
3906 /* if the tar option has been called previouslt, now we need to eat out the leftovers */
3907 /* I see no other way to keep things sane --SSS */
3908 if (tar_opt
== True
) {
3909 while (poptPeekArg(pc
)) {
3915 /* if the service has not yet been specified lets see if it is available in the popt stack */
3916 if (!service_opt
&& poptPeekArg(pc
)) {
3917 pstrcpy(service
, poptGetArg(pc
));
3921 /* if the service has already been retrieved then check if we have also a password */
3922 if (service_opt
&& (!cmdline_auth_info
.got_pass
) && poptPeekArg(pc
)) {
3923 pstrcpy(cmdline_auth_info
.password
, poptGetArg(pc
));
3924 cmdline_auth_info
.got_pass
= True
;
3929 /* Messages are sent to NetBIOS name type 0x3
3930 * (Messenger Service). Make sure we default
3931 * to port 139 instead of port 445. srl,crh
3934 cli_cm_set_dest_name_type( name_type
);
3935 pstrcpy(desthost
,poptGetOptArg(pc
));
3937 cli_cm_set_port( 139 );
3942 dest_ip
= *interpret_addr2(poptGetOptArg(pc
));
3943 if (is_zero_ip(dest_ip
))
3947 cli_cm_set_dest_ip( dest_ip
);
3955 display_set_stderr();
3959 pstrcpy(query_host
, poptGetOptArg(pc
));
3962 pstrcpy(term_code
, poptGetOptArg(pc
));
3965 max_protocol
= interpret_protocol(poptGetOptArg(pc
), max_protocol
);
3968 /* We must use old option processing for this. Find the
3969 * position of the -T option in the raw argv[]. */
3972 for (i
= 1; i
< argc
; i
++) {
3973 if (strncmp("-T", argv
[i
],2)==0)
3977 if (!tar_parseargs(argc
, argv
, poptGetOptArg(pc
), i
)) {
3978 poptPrintUsage(pc
, stderr
, 0);
3982 /* this must be the last option, mark we have parsed it so that we know we have */
3986 pstrcpy(base_directory
,poptGetOptArg(pc
));
3994 /* We may still have some leftovers after the last popt option has been called */
3995 if (tar_opt
== True
) {
3996 while (poptPeekArg(pc
)) {
4002 /* if the service has not yet been specified lets see if it is available in the popt stack */
4003 if (!service_opt
&& poptPeekArg(pc
)) {
4004 pstrcpy(service
, poptGetArg(pc
));
4008 /* if the service has already been retrieved then check if we have also a password */
4009 if (service_opt
&& (!cmdline_auth_info
.got_pass
) && poptPeekArg(pc
)) {
4010 pstrcpy(cmdline_auth_info
.password
, poptGetArg(pc
));
4011 cmdline_auth_info
.got_pass
= True
;
4014 /* check for the -P option */
4017 cli_cm_set_port( port
);
4020 * Don't load debug level from smb.conf. It should be
4021 * set by cmdline arg or remain default (0)
4023 AllowDebugChange
= False
;
4025 /* save the workgroup...
4027 FIXME!! do we need to do this for other options as well
4028 (or maybe a generic way to keep lp_load() from overwriting
4031 fstrcpy( new_workgroup
, lp_workgroup() );
4032 pstrcpy( calling_name
, global_myname() );
4034 if ( override_logfile
)
4035 setup_logging( lp_logfile(), False
);
4037 if (!lp_load(dyn_CONFIGFILE
,True
,False
,False
,True
)) {
4038 fprintf(stderr
, "%s: Can't load %s - run testparm to debug it\n",
4039 argv
[0], dyn_CONFIGFILE
);
4045 /* Convert any '/' characters in the service name to '\' characters */
4046 string_replace(service
, '/','\\');
4047 if (count_chars(service
,'\\') < 3) {
4048 d_printf("\n%s: Not enough '\\' characters in service\n",service
);
4049 poptPrintUsage(pc
, stderr
, 0);
4054 if ( strlen(new_workgroup
) != 0 )
4055 set_global_myworkgroup( new_workgroup
);
4057 if ( strlen(calling_name
) != 0 )
4058 set_global_myname( calling_name
);
4060 pstrcpy( calling_name
, global_myname() );
4064 if(new_name_resolve_order
)
4065 lp_set_name_resolve_order(new_name_resolve_order
);
4067 if (!tar_type
&& !*query_host
&& !*service
&& !message
) {
4068 poptPrintUsage(pc
, stderr
, 0);
4072 poptFreeContext(pc
);
4074 /* store the username an password for dfs support */
4076 cli_cm_set_credentials( &cmdline_auth_info
);
4077 pstrcpy(username
, cmdline_auth_info
.username
);
4079 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING
));
4083 process_command_string(cmdstr
);
4084 return do_tar_op(base_directory
);
4088 char *qhost
= query_host
;
4091 while (*qhost
== '\\' || *qhost
== '/')
4094 if ((slash
= strchr_m(qhost
, '/'))
4095 || (slash
= strchr_m(qhost
, '\\'))) {
4099 if ((p
=strchr_m(qhost
, '#'))) {
4102 sscanf(p
, "%x", &name_type
);
4103 cli_cm_set_dest_name_type( name_type
);
4106 return do_host_query(qhost
);
4110 return do_message_op();
4113 if (process(base_directory
)) {
4117 talloc_destroy( ctx
);