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
8 Copyright (C) Kalim Moghul 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libsmbclient.h"
26 #include "client/client_proto.h"
31 extern BOOL AllowDebugChange
;
32 extern BOOL override_logfile
;
34 extern BOOL in_client
;
36 pstring cur_dir
= "/";
37 static pstring service
;
38 static pstring desthost
;
39 static pstring username
;
40 static pstring workgroup
;
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 /* 30 second timeout on most commands */
54 #define CLIENT_TIMEOUT (30*1000)
55 #define SHORT_TIMEOUT (5*1000)
57 /* value for unused fid field in trans2 secondary request */
58 #define FID_UNUSED (0xFFFF)
60 time_t newer_than
= 0;
61 static int archive_level
= 0;
63 static BOOL translation
= False
;
66 /* clitar bits insert */
69 extern BOOL tar_reset
;
73 static BOOL prompt
= True
;
75 static BOOL recurse
= False
;
76 BOOL lowercase
= False
;
78 static struct in_addr dest_ip
;
80 #define SEPARATORS " \t\n\r"
82 static BOOL abort_mget
= True
;
84 static pstring fileselection
= "";
86 extern file_info def_finfo
;
89 SMB_BIG_UINT get_total_size
= 0;
90 unsigned int get_total_time_ms
= 0;
91 static SMB_BIG_UINT put_total_size
= 0;
92 static unsigned int put_total_time_ms
= 0;
95 static double dir_total
;
97 /* root cli_state connection */
99 struct cli_state
*cli
;
102 /****************************************************************************
103 Authentication callback function for libsmbclient
104 ****************************************************************************/
107 get_auth_data_fn(const char * pServer
, const char * pShare
,
108 char * pWorkgroup
, int maxLenWorkgroup
,
109 char * pUsername
, int maxLenUsername
,
110 char * pPassword
, int maxLenPassword
)
112 char temp
[sizeof(fstring
)];
114 static char authUsername
[sizeof(fstring
)];
115 static char authWorkgroup
[sizeof(fstring
)];
116 static char authPassword
[sizeof(fstring
)];
117 static char authSet
= 0;
122 strncpy(pWorkgroup
, authWorkgroup
, maxLenWorkgroup
- 1);
123 strncpy(pUsername
, authUsername
, maxLenUsername
- 1);
124 strncpy(pPassword
, authPassword
, maxLenPassword
- 1);
128 d_printf("Workgroup: %s\n", workgroup
);
129 strncpy(pWorkgroup
, workgroup
, maxLenWorkgroup
- 1);
130 strncpy(authWorkgroup
, workgroup
, maxLenWorkgroup
- 1);
132 d_printf("Username: %s\n", username
);
133 strncpy(pUsername
, username
, maxLenUsername
- 1);
134 strncpy(authUsername
, username
, maxLenUsername
- 1);
136 if (cmdline_auth_info
.got_pass
)
138 strncpy(pPassword
, cmdline_auth_info
.password
, maxLenPassword
- 1);
139 strncpy(authPassword
, cmdline_auth_info
.password
, maxLenPassword
- 1);
143 char *pass
= getpass("Password: ");
146 if (temp
[strlen(temp
) - 1] == '\n') /* A new line? */
148 temp
[strlen(temp
) - 1] = '\0';
152 strncpy(pPassword
, temp
, maxLenPassword
- 1);
153 strncpy(authPassword
, pPassword
, maxLenPassword
- 1);
156 pstrcpy(cmdline_auth_info
.username
, authUsername
);
157 pstrcpy(cmdline_auth_info
.password
, authPassword
);
158 cmdline_auth_info
.got_pass
= True
;
159 set_global_myworkgroup(authWorkgroup
);
160 cli_cm_set_credentials(&cmdline_auth_info
);
165 /*******************************************************************
166 Return a string representing an attribute for a file.
167 ********************************************************************/
169 fstring
*mode_t_string(mode_t mode
)
171 static fstring attrstr
;
175 S_ISDIR(mode
) ? fstrcat(attrstr
, "d") : fstrcat(attrstr
, "-");
176 (mode
& S_IRUSR
) ? fstrcat(attrstr
, "r") : fstrcat(attrstr
, "-");
177 (mode
& S_IWUSR
) ? fstrcat(attrstr
, "w") : fstrcat(attrstr
, "-");
178 (mode
& S_IXUSR
) ? fstrcat(attrstr
, "x") : fstrcat(attrstr
, "-");
180 (mode
& S_IRGRP
) ? fstrcat(attrstr
, "r") : fstrcat(attrstr
, "-");
181 (mode
& S_IWGRP
) ? fstrcat(attrstr
, "w") : fstrcat(attrstr
, "-");
182 (mode
& S_IXGRP
) ? fstrcat(attrstr
, "x") : fstrcat(attrstr
, "-");
184 (mode
& S_IROTH
) ? fstrcat(attrstr
, "r") : fstrcat(attrstr
, "-");
185 (mode
& S_IWOTH
) ? fstrcat(attrstr
, "w") : fstrcat(attrstr
, "-");
186 (mode
& S_IXOTH
) ? fstrcat(attrstr
, "x") : fstrcat(attrstr
, "-");
192 /****************************************************************************
193 Write to a local file with CR/LF->LF translation if appropriate. Return the
194 number taken from the buffer. This may not equal the number written.
195 ****************************************************************************/
197 static int writefile(int f
, char *b
, int n
)
207 if (*b
== '\r' && (i
<(n
-1)) && *(b
+1) == '\n') {
210 if (write(f
, b
, 1) != 1) {
220 /****************************************************************************
221 Read from a file with LF->CR/LF translation if appropriate. Return the
222 number read. read approx n bytes.
223 ****************************************************************************/
225 static int readfile(char *b
, int n
, XFILE
*f
)
231 return x_fread(b
,1,n
,f
);
234 while (i
< (n
- 1) && (i
< BUFFER_SIZE
)) {
235 if ((c
= x_getc(f
)) == EOF
) {
239 if (c
== '\n') { /* change all LFs to CR/LF */
249 /****************************************************************************
251 ****************************************************************************/
253 static void send_message(void)
258 if (!cli_message_start(cli
, desthost
, username
, &grp_id
)) {
259 d_printf("message start: %s\n", cli_errstr(cli
));
264 d_printf("Connected. Type your message, ending it with a Control-D\n");
266 while (!feof(stdin
) && total_len
< 1600) {
267 int maxlen
= MIN(1600 - total_len
,127);
274 for (l
=0;l
<maxlen
&& (c
=fgetc(stdin
))!=EOF
;l
++) {
280 if (!cli_message_text(cli
, msg
, l
, grp_id
)) {
281 d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli
));
288 if (total_len
>= 1600)
289 d_printf("the message was truncated to 1600 bytes\n");
291 d_printf("sent %d bytes\n",total_len
);
293 if (!cli_message_end(cli
, grp_id
)) {
294 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli
));
299 /****************************************************************************
300 Check the space on a device.
301 ****************************************************************************/
303 static int do_dskattr(void)
305 int total
, bsize
, avail
;
306 struct cli_state
*targetcli
;
309 if ( !cli_resolve_path( "", cli
, cur_dir
, &targetcli
, targetpath
) ) {
310 d_printf("Error in dskattr: %s\n", cli_errstr(cli
));
314 if (!cli_dskattr(targetcli
, &bsize
, &total
, &avail
)) {
315 d_printf("Error in dskattr: %s\n",cli_errstr(targetcli
));
319 d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
320 total
, bsize
, avail
);
325 /****************************************************************************
327 ****************************************************************************/
329 static int cmd_pwd(void)
331 d_printf("Current directory is %s",service
);
332 d_printf("%s\n",cur_dir
);
336 /****************************************************************************
337 Change directory - inner section.
338 ****************************************************************************/
340 static int do_cd(char *newdir
)
346 struct cli_state
*targetcli
;
347 SMB_STRUCT_STAT sbuf
;
353 /* Save the current directory in case the new directory is invalid */
354 pstrcpy(saved_dir
, cur_dir
);
356 pstrcpy(dname
, p
); /* first save the argument */
359 pstrcpy(cur_dir
,dname
);
361 pstrcat(cur_dir
,dname
);
363 if (*(cur_dir
+strlen(cur_dir
)-1) != '/') {
364 pstrcat(cur_dir
, "/");
367 all_string_sub(cur_dir
, "/./", "/", 0);
369 /* Format the directory in a libmsmbclient friendly way */
371 all_string_sub(cur_dir
, "/./", "/", 0);
372 pstrcpy(targetpath
, "smb:");
373 pstrcat(targetpath
, service
);
374 pstrcat(targetpath
, cur_dir
);
375 unix_format(targetpath
);
377 dh
= smbc_opendir(targetpath
);
381 d_printf("%s changing to directory %s\n", strerror(errno
), cur_dir
);
382 pstrcpy(cur_dir
, saved_dir
);
390 /****************************************************************************
392 ****************************************************************************/
394 static int cmd_cd(void)
399 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)))
402 d_printf("Current directory is %s\n",cur_dir
);
407 /*******************************************************************
408 Decide if a file should be operated on.
409 ********************************************************************/
411 static BOOL
do_this_one(file_info
*finfo
)
413 if (finfo
->mode
& aDIR
)
416 if (*fileselection
&&
417 !mask_match(finfo
->name
,fileselection
,False
)) {
418 DEBUG(3,("mask_match %s failed\n", finfo
->name
));
422 if (newer_than
&& finfo
->mtime
< newer_than
) {
423 DEBUG(3,("newer_than %s failed\n", finfo
->name
));
427 if ((archive_level
==1 || archive_level
==2) && !(finfo
->mode
& aARCH
)) {
428 DEBUG(3,("archive %s failed\n", finfo
->name
));
435 /****************************************************************************
436 Display info about a file.
437 ****************************************************************************/
439 static void display_finfo(file_info
*finfo
)
441 if (do_this_one(finfo
)) {
442 time_t t
= finfo
->mtime
; /* the time is assumed to be passed as GMT */
443 d_printf(" %-30s%7.7s %8.0f %s",
445 attrib_string(finfo
->mode
),
448 dir_total
+= finfo
->size
;
452 /****************************************************************************
453 Display info about a file.
454 ****************************************************************************/
456 static void display_stat(char *name
, struct stat
*st
)
458 time_t t
= st
->st_mtime
;
460 pstrcpy(time_str
, time_to_asc(t
));
461 time_str
[strlen(time_str
)-1] = 0;
462 d_printf("> %-30s", name
);
463 d_printf("%10.10s %8.0f %s\n", *mode_t_string(st
->st_mode
), (double)st
->st_size
, time_str
);
466 /****************************************************************************
467 Accumulate size of a file.
468 ****************************************************************************/
470 static void do_du(file_info
*finfo
)
472 if (do_this_one(finfo
)) {
473 dir_total
+= finfo
->size
;
477 static BOOL do_list_recurse
;
478 static BOOL do_list_dirs
;
479 static char *do_list_queue
= 0;
480 static long do_list_queue_size
= 0;
481 static long do_list_queue_start
= 0;
482 static long do_list_queue_end
= 0;
483 static void (*do_list_fn
)(file_info
*);
484 static void (*tool_list_fn
)(char *, struct stat
*);
486 /****************************************************************************
487 Functions for do_list_queue.
488 ****************************************************************************/
491 * The do_list_queue is a NUL-separated list of strings stored in a
492 * char*. Since this is a FIFO, we keep track of the beginning and
493 * ending locations of the data in the queue. When we overflow, we
494 * double the size of the char*. When the start of the data passes
495 * the midpoint, we move everything back. This is logically more
496 * complex than a linked list, but easier from a memory management
497 * angle. In any memory error condition, do_list_queue is reset.
498 * Functions check to ensure that do_list_queue is non-NULL before
502 static void reset_do_list_queue(void)
504 SAFE_FREE(do_list_queue
);
505 do_list_queue_size
= 0;
506 do_list_queue_start
= 0;
507 do_list_queue_end
= 0;
510 static void init_do_list_queue(void)
512 reset_do_list_queue();
513 do_list_queue_size
= 1024;
514 do_list_queue
= SMB_MALLOC(do_list_queue_size
);
515 if (do_list_queue
== 0) {
516 d_printf("malloc fail for size %d\n",
517 (int)do_list_queue_size
);
518 reset_do_list_queue();
520 memset(do_list_queue
, 0, do_list_queue_size
);
524 static void adjust_do_list_queue(void)
527 * If the starting point of the queue is more than half way through,
528 * move everything toward the beginning.
531 if (do_list_queue
== NULL
) {
532 DEBUG(4,("do_list_queue is empty\n"));
533 do_list_queue_start
= do_list_queue_end
= 0;
537 if (do_list_queue_start
== do_list_queue_end
) {
538 DEBUG(4,("do_list_queue is empty\n"));
539 do_list_queue_start
= do_list_queue_end
= 0;
540 *do_list_queue
= '\0';
541 } else if (do_list_queue_start
> (do_list_queue_size
/ 2)) {
542 DEBUG(4,("sliding do_list_queue backward\n"));
543 memmove(do_list_queue
,
544 do_list_queue
+ do_list_queue_start
,
545 do_list_queue_end
- do_list_queue_start
);
546 do_list_queue_end
-= do_list_queue_start
;
547 do_list_queue_start
= 0;
551 static void add_to_do_list_queue(const char* entry
)
553 long new_end
= do_list_queue_end
+ ((long)strlen(entry
)) + 1;
554 while (new_end
> do_list_queue_size
) {
555 do_list_queue_size
*= 2;
556 DEBUG(4,("enlarging do_list_queue to %d\n",
557 (int)do_list_queue_size
));
558 do_list_queue
= SMB_REALLOC(do_list_queue
, do_list_queue_size
);
559 if (!do_list_queue
) {
560 d_printf("failure enlarging do_list_queue to %d bytes\n",
561 (int)do_list_queue_size
);
562 reset_do_list_queue();
564 memset(do_list_queue
+ do_list_queue_size
/ 2,
565 0, do_list_queue_size
/ 2);
569 safe_strcpy_base(do_list_queue
+ do_list_queue_end
,
570 entry
, do_list_queue
, do_list_queue_size
);
571 do_list_queue_end
= new_end
;
572 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
573 entry
, (int)do_list_queue_start
, (int)do_list_queue_end
));
577 static char *do_list_queue_head(void)
579 return do_list_queue
+ do_list_queue_start
;
582 static void remove_do_list_queue_head(void)
584 if (do_list_queue_end
> do_list_queue_start
) {
585 do_list_queue_start
+= strlen(do_list_queue_head()) + 1;
586 adjust_do_list_queue();
587 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
588 (int)do_list_queue_start
, (int)do_list_queue_end
));
592 static int do_list_queue_empty(void)
594 return (! (do_list_queue
&& *do_list_queue
));
597 /****************************************************************************
598 A helper for tool_list.
599 ****************************************************************************/
601 static void tool_list_helper(const char *mntpoint
, struct stat
*f
, const char *mask
, void *state
)
603 /*if (f is a directory)
605 if (we want to do directories and we want to do this f)
607 execute the callback on f
609 if (recursion is set and f isn't . and it isn't ..)
611 make sure the name is valid
612 construct a full path out of the name
613 add the full path to the list
617 if (we want to do this f)
619 execute the callback on f
623 /****************************************************************************
624 A cli_list-like function that executes fn on each directory entry.
625 fn operates on the returned entry name and struct stat.
626 ****************************************************************************/
628 int tool_list( char *mask
,
630 void (*fn
)(char *, struct stat
*),
638 struct smbc_dirent
* dent
;
640 pstrcpy(res
, "smb:");
641 pstrcat(res
, service
);
642 pstrcat(res
, cur_dir
);
644 if ((dh
= smbc_opendir(res
)) < 1)
646 d_printf("Error: %s opening %s\n", strerror(errno
), res
);
650 while (dent
= smbc_readdir(dh
))
652 switch(dent
->smbc_type
)
656 case SMBC_FILE_SHARE
:
657 case SMBC_PRINTER_SHARE
:
658 case SMBC_COMMS_SHARE
:
665 pstrcpy(dentname
, res
);
666 pstrcat(dentname
, dent
->name
);
667 /*if (mask_match(dent->name, mask, False))*/
668 if (mask_match(dentname
, mask
, False
))
670 if (smbc_stat(dentname
, &stat
) < 0)
672 d_printf("> %s - stat error: %s\n", dent
->name
, strerror(errno
));
676 fn(dent
->name
, &stat
);
687 /****************************************************************************
688 A helper for do_list.
689 ****************************************************************************/
691 static void do_list_helper(const char *mntpoint
, file_info
*f
, const char *mask
, void *state
)
693 if (f
->mode
& aDIR
) {
694 if (do_list_dirs
&& do_this_one(f
)) {
697 if (do_list_recurse
&&
698 !strequal(f
->name
,".") &&
699 !strequal(f
->name
,"..")) {
704 d_printf("Empty dir name returned. Possible server misconfiguration.\n");
708 pstrcpy(mask2
, mntpoint
);
709 pstrcat(mask2
, mask
);
710 p
= strrchr_m(mask2
,'/');
714 pstrcat(mask2
, f
->name
);
716 add_to_do_list_queue(mask2
);
721 if (do_this_one(f
)) {
726 /****************************************************************************
727 A wrapper around cli_list that adds recursion.
728 ****************************************************************************/
730 void do_list(const char *mask
,uint16 attribute
,void (*fn
)(file_info
*),BOOL rec
, BOOL dirs
)
732 static int in_do_list
= 0;
733 struct cli_state
*targetcli
;
736 if (in_do_list
&& rec
) {
737 fprintf(stderr
, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
743 do_list_recurse
= rec
;
748 init_do_list_queue();
749 add_to_do_list_queue(mask
);
751 while (! do_list_queue_empty()) {
753 * Need to copy head so that it doesn't become
754 * invalid inside the call to cli_list. This
755 * would happen if the list were expanded
757 * Fix from E. Jay Berkenbilt (ejb@ql.org)
760 pstrcpy(head
, do_list_queue_head());
764 if ( !cli_resolve_path( "", cli
, head
, &targetcli
, targetpath
) ) {
765 d_printf("do_list: [%s] %s\n", head
, cli_errstr(cli
));
766 remove_do_list_queue_head();
770 cli_list(targetcli
, targetpath
, attribute
, do_list_helper
, NULL
);
771 remove_do_list_queue_head();
772 if ((! do_list_queue_empty()) && (fn
== display_finfo
)) {
773 char* next_file
= do_list_queue_head();
775 if ((strlen(next_file
) >= 2) &&
776 (next_file
[strlen(next_file
) - 1] == '*') &&
777 (next_file
[strlen(next_file
) - 2] == '/')) {
778 save_ch
= next_file
+
779 strlen(next_file
) - 2;
782 d_printf("\n%s\n",next_file
);
791 if ( cli_resolve_path( "", cli
, mask
, &targetcli
, targetpath
) ) {
792 if (cli_list(targetcli
, targetpath
, attribute
, do_list_helper
, NULL
) == -1)
793 d_printf("%s listing %s\n", cli_errstr(targetcli
), targetpath
);
796 d_printf("do_list: [%s] %s\n", mask
, cli_errstr(cli
));
801 reset_do_list_queue();
804 /****************************************************************************
805 Get a directory listing.
806 ****************************************************************************/
808 static int cmd_dir(void)
810 mode_t mode
= S_IFDIR
| S_IRWXU
| S_IRWXG
| S_IRWXO
;
815 pstrcpy(mask
, "smb:");
816 pstrcat(mask
, service
);
817 pstrcat(mask
, cur_dir
);
819 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)))
824 tool_list(mask
, mode
, display_stat
, recurse
, True
);
828 /****************************************************************************
829 Get a directory listing.
830 ****************************************************************************/
832 static int cmd_du(void)
834 uint16 attribute
= aDIR
| aSYSTEM
| aHIDDEN
;
841 pstrcpy(mask
,cur_dir
);
842 if(mask
[strlen(mask
)-1]!='/')
845 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
855 do_list(mask
, attribute
, do_du
, recurse
, True
);
859 d_printf("Total number of bytes: %.0f\n", dir_total
);
864 /****************************************************************************
865 Get a file from rname to lname
866 ****************************************************************************/
868 static int do_get(char *rname
, char *lname
, BOOL reget
)
870 int handle
= 0, fnum
;
871 BOOL newhandle
= False
;
873 struct timeval tp_start
;
874 int read_size
= io_bufsize
;
885 GetTimeOfDay(&tp_start
);
887 fnum
= smbc_open(rname
, O_RDONLY
, 0666);
890 d_printf("%s opening remote file %s\n", strerror(errno
), rname
);
894 if(!strcmp(lname
,"-")) {
895 handle
= fileno(stdout
);
898 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
, 0644);
900 start
= sys_lseek(handle
, 0, SEEK_END
);
903 d_printf("Error seeking local file\n");
908 handle
= sys_open(lname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
913 d_printf("Error opening local file %s\n",lname
);
918 if (smbc_fstat(fnum
, &stat
) < 0)
920 d_printf("%s trying to stat remote file %s\n", strerror(errno
), rname
);
927 DEBUG(1,("getting file %s of size %.0f as %s ",
928 rname
, (double)stat
.st_size
, lname
));
930 if(!(data
= (char *)SMB_MALLOC(read_size
))) {
931 d_printf("malloc fail for size %d\n", read_size
);
938 if (smbc_lseek(fnum
, start
, SEEK_SET
) < 0)
940 d_printf("%s trying to lseek remote file %s\n", strerror(errno
), rname
);
948 int n
= smbc_read(fnum
, data
, read_size
);
952 d_printf("%s while reading remote file %s\n", strerror(errno
), rname
);
962 if (writefile(handle
,data
, n
) != n
) {
963 d_printf("Error writing local file\n");
971 if (nread
+ start
< stat
.st_size
) {
972 DEBUG (1, ("Short read when getting file %s. Only got %ld bytes.\n", rname
, (long)nread
));
978 if (smbc_close(fnum
) < 0)
980 d_printf("%s closing remote file %s\n", strerror(errno
), rname
);
988 /*if (archive_level >= 2 && (attr & aARCH)) {
989 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
993 struct timeval tp_end
;
996 GetTimeOfDay(&tp_end
);
998 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
999 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
1000 get_total_time_ms
+= this_time
;
1001 get_total_size
+= nread
;
1003 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1004 nread
/ (1.024*this_time
+ 1.0e-4),
1005 get_total_size
/ (1.024*get_total_time_ms
)));
1011 /****************************************************************************
1013 ****************************************************************************/
1015 static int cmd_get(void)
1021 pstrcpy(rname
, "smb:");
1022 pstrcat(rname
, service
);
1023 pstrcat(rname
, cur_dir
);
1025 p
= rname
+ strlen(rname
);
1027 if (!next_token_nr(NULL
,p
,NULL
,sizeof(rname
)-strlen(rname
))) {
1028 d_printf("get <filename>\n");
1033 next_token_nr(NULL
,lname
,NULL
,sizeof(lname
));
1034 /*d_printf("lname: %s, rname: %s\n", lname, rname);*/
1035 return do_get(rname
, lname
, False
);
1038 /****************************************************************************
1039 Do an mget operation on one file.
1040 ****************************************************************************/
1042 static void do_mget(char *name
, struct stat
*st
)
1046 pstring saved_curdir
;
1050 if (strequal(name
,".") || strequal(name
,".."))
1053 if (S_ISDIR(st
->st_mode
))
1054 slprintf(quest
,sizeof(pstring
)-1, "Get directory %s%s? ", cur_dir
, name
);
1056 slprintf(quest
,sizeof(pstring
)-1, "Get file %s%s? ", cur_dir
, name
);
1058 if (prompt
&& !yesno(quest
))
1061 if (!S_ISDIR(st
->st_mode
)) {
1062 pstrcpy(rname
,"smb:");
1063 pstrcat(rname
,service
);
1064 pstrcat(rname
,cur_dir
);
1065 pstrcat(rname
,name
);
1066 do_get(rname
,name
, False
);
1070 /* handle directories */
1071 /* TODO: clean this code up for recursive calls */
1072 pstrcpy(saved_curdir
,cur_dir
);
1074 pstrcat(cur_dir
,name
);
1075 pstrcat(cur_dir
,"/");
1081 if (!directory_exist(name
,NULL
) &&
1082 mkdir(name
,0777) != 0) {
1083 d_printf("failed to create directory %s\n",name
);
1084 pstrcpy(cur_dir
,saved_curdir
);
1088 if (chdir(name
) != 0) {
1089 d_printf("failed to chdir to directory %s\n",name
);
1090 pstrcpy(cur_dir
,saved_curdir
);
1094 pstrcpy(mget_mask
,"smb:");
1095 pstrcat(mget_mask
,service
);
1096 pstrcat(mget_mask
,cur_dir
);
1097 pstrcat(mget_mask
,"*");
1099 /*d_printf("Calling with mask: %s\n", mget_mask);*/
1100 tool_list(mget_mask
, mode
, do_mget
, recurse
, recurse
);
1102 pstrcpy(cur_dir
,saved_curdir
);
1105 /****************************************************************************
1106 View the file using the pager.
1107 ****************************************************************************/
1109 static int cmd_more(void)
1111 pstring rname
,lname
,pager_cmd
;
1116 pstrcpy(rname
,cur_dir
);
1119 slprintf(lname
,sizeof(lname
)-1, "%s/smbmore.XXXXXX",tmpdir());
1120 fd
= smb_mkstemp(lname
);
1122 d_printf("failed to create temporary file for more\n");
1127 if (!next_token_nr(NULL
,rname
+strlen(rname
),NULL
,sizeof(rname
)-strlen(rname
))) {
1128 d_printf("more <filename>\n");
1134 rc
= do_get(rname
, lname
, False
);
1136 pager
=getenv("PAGER");
1138 slprintf(pager_cmd
,sizeof(pager_cmd
)-1,
1139 "%s %s",(pager
? pager
:PAGER
), lname
);
1146 /****************************************************************************
1148 ****************************************************************************/
1150 static int cmd_mget(void)
1159 while (next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1160 pstrcpy(mget_mask
, "smb:");
1161 pstrcat(mget_mask
, service
);
1162 pstrcat(mget_mask
,cur_dir
);
1163 if(mget_mask
[strlen(mget_mask
)-1]!='/')
1164 pstrcat(mget_mask
,"/");
1168 pstrcpy(mget_mask
, "smb:");
1169 pstrcat(mget_mask
, service
);
1170 pstrcat(mget_mask
,p
);
1173 pstrcat(mget_mask
,p
);
1174 /* TODO: enable directories on calls to tool_list
1175 once recursion is worked out */
1176 tool_list(mget_mask
, mode
, do_mget
, recurse
, recurse
);
1180 pstrcpy(mget_mask
, "smb:");
1181 pstrcat(mget_mask
, service
);
1182 pstrcat(mget_mask
,cur_dir
);
1183 if(mget_mask
[strlen(mget_mask
)-1]!='/')
1184 pstrcat(mget_mask
,"/");
1185 pstrcat(mget_mask
,"*");
1186 tool_list(mget_mask
, mode
, do_mget
, recurse
, recurse
);
1192 /****************************************************************************
1193 Make a directory of name "name".
1194 ****************************************************************************/
1196 static BOOL
do_mkdir(char *name
)
1198 if (smbc_mkdir(name
, 755) < 0)
1200 d_printf("Error: %s making remote directory %s\n", strerror(errno
), name
);
1207 /****************************************************************************
1208 Show 8.3 name of a file.
1209 ****************************************************************************/
1211 static BOOL
do_altname(char *name
)
1214 if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli
, name
, altname
))) {
1215 d_printf("%s getting alt name for %s\n",
1216 cli_errstr(cli
),name
);
1219 d_printf("%s\n", altname
);
1224 /****************************************************************************
1226 ****************************************************************************/
1228 static int cmd_quit(void)
1236 /****************************************************************************
1238 ****************************************************************************/
1240 static int cmd_mkdir(void)
1248 pstrcpy(mask
,cur_dir
);
1250 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1252 d_printf("mkdir <dirname>\n");
1263 trim_char(ddir
,'.','\0');
1264 p
= strtok(ddir
,"/\\");
1268 pstrcpy(targetname
, "smb:");
1269 pstrcat(targetname
, service
);
1270 pstrcat(targetname
, "/");
1271 pstrcat(targetname
, ddir2
);
1272 DEBUG(3, ("Recursively making directory %s\n", targetname
));
1273 if ((dh
= smbc_opendir(targetname
)) < 0) {
1274 if (!do_mkdir(targetname
))
1281 p
= strtok(NULL
,"/\\");
1285 pstrcpy(targetname
, "smb:");
1286 pstrcat(targetname
, service
);
1287 pstrcat(targetname
, mask
);
1289 if (!do_mkdir(targetname
))
1296 /****************************************************************************
1298 ****************************************************************************/
1300 static int cmd_altname(void)
1306 pstrcpy(name
,cur_dir
);
1308 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1309 d_printf("altname <file>\n");
1319 /****************************************************************************
1321 ****************************************************************************/
1323 static int do_put(char *rname
, char *lname
, BOOL reput
)
1327 SMB_OFF_T start
= 0;
1330 int maxwrite
= io_bufsize
;
1332 struct timeval tp_start
;
1335 GetTimeOfDay(&tp_start
);
1338 fnum
= smbc_open(rname
, O_RDWR
|O_CREAT
, 0644);
1341 d_printf("%s opening remote file %s\n", strerror(errno
), rname
);
1344 if (smbc_fstat(fnum
, &stat
) < 0)
1346 d_printf("%s trying to stat remote file %s\n", strerror(errno
), rname
);
1350 start
= stat
.st_size
;
1352 fnum
= smbc_creat(rname
, 0644);
1355 d_printf("%s trying to create remote file %s\n", strerror(errno
), rname
);
1360 /* allow files to be piped into smbclient
1363 Note that in this case this function will exit(0) rather
1365 if (!strcmp(lname
, "-")) {
1367 /* size of file is not known */
1369 f
= x_fopen(lname
,O_RDONLY
, 0);
1371 if (x_tseek(f
, start
, SEEK_SET
) == -1) {
1372 d_printf("Error seeking local file\n");
1381 d_printf("Error opening local file %s\n",lname
);
1386 DEBUG(1,("putting file %s as %s ",lname
,rname
));
1388 buf
= (char *)SMB_MALLOC(maxwrite
);
1390 d_printf("ERROR: Not enough memory!\n");
1397 if (smbc_lseek(fnum
, start
, SEEK_SET
) < 0)
1399 d_printf("%s trying to lseek remote file %s\n", strerror(errno
), rname
);
1406 while (!x_feof(f
)) {
1410 if ((n
= readfile(buf
,n
,f
)) < 1) {
1411 if((n
== 0) && x_feof(f
))
1412 break; /* Empty local file. */
1414 d_printf("Error reading local file: %s\n", strerror(errno
));
1419 ret
= smbc_write(fnum
, buf
, n
);
1422 d_printf("Error writing file: %s\n", strerror(errno
));
1430 if (smbc_close(fnum
) < 0) {
1431 d_printf("%s closing remote file %s\n",strerror(errno
),rname
);
1446 struct timeval tp_end
;
1449 GetTimeOfDay(&tp_end
);
1451 (tp_end
.tv_sec
- tp_start
.tv_sec
)*1000 +
1452 (tp_end
.tv_usec
- tp_start
.tv_usec
)/1000;
1453 put_total_time_ms
+= this_time
;
1454 put_total_size
+= nread
;
1456 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1457 nread
/ (1.024*this_time
+ 1.0e-4),
1458 put_total_size
/ (1.024*put_total_time_ms
)));
1469 /****************************************************************************
1471 ****************************************************************************/
1473 static int cmd_put(void)
1480 pstrcpy(rname
, "smb:");
1481 pstrcat(rname
, service
);
1482 pstrcat(rname
, cur_dir
);
1484 if (!next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1485 d_printf("put <filename>\n");
1490 if (next_token_nr(NULL
,p
,NULL
,sizeof(buf
)))
1493 pstrcat(rname
,lname
);
1497 /* allow '-' to represent stdin
1498 jdblair, 24.jun.98 */
1499 if (!file_exist(lname
,&st
) &&
1500 (strcmp(lname
,"-"))) {
1501 d_printf("%s does not exist\n",lname
);
1506 /*d_printf("lname: %s, rname: %s\n", lname, rname);*/
1507 return do_put(rname
, lname
, False
);
1510 /*************************************
1511 File list structure.
1512 *************************************/
1514 static struct file_list
{
1515 struct file_list
*prev
, *next
;
1520 /****************************************************************************
1521 Free a file_list structure.
1522 ****************************************************************************/
1524 static void free_file_list (struct file_list
* list
)
1526 struct file_list
*tmp
;
1530 DLIST_REMOVE(list
, list
);
1531 SAFE_FREE(tmp
->file_path
);
1536 /****************************************************************************
1537 Seek in a directory/file list until you get something that doesn't start with
1539 ****************************************************************************/
1541 static BOOL
seek_list(struct file_list
*list
, char *name
)
1544 trim_string(list
->file_path
,"./","\n");
1545 if (strncmp(list
->file_path
, name
, strlen(name
)) != 0) {
1554 /****************************************************************************
1555 Set the file selection mask.
1556 ****************************************************************************/
1558 static int cmd_select(void)
1560 pstrcpy(fileselection
,"");
1561 next_token_nr(NULL
,fileselection
,NULL
,sizeof(fileselection
));
1566 /****************************************************************************
1567 Recursive file matching function act as find
1568 match must be always set to True when calling this function
1569 ****************************************************************************/
1571 static int file_find(struct file_list
**list
, const char *directory
,
1572 const char *expression
, BOOL match
)
1575 struct file_list
*entry
;
1576 struct stat statbuf
;
1582 dir
= opendir(directory
);
1586 while ((dname
= readdirname(dir
))) {
1587 if (!strcmp("..", dname
))
1589 if (!strcmp(".", dname
))
1592 if (asprintf(&path
, "%s/%s", directory
, dname
) <= 0) {
1597 if (!match
|| !gen_fnmatch(expression
, dname
)) {
1599 ret
= stat(path
, &statbuf
);
1601 if (S_ISDIR(statbuf
.st_mode
)) {
1603 ret
= file_find(list
, path
, expression
, False
);
1606 d_printf("file_find: cannot stat file %s\n", path
);
1615 entry
= SMB_MALLOC_P(struct file_list
);
1617 d_printf("Out of memory in file_find\n");
1621 entry
->file_path
= path
;
1622 entry
->isdir
= isdir
;
1623 DLIST_ADD(*list
, entry
);
1633 /****************************************************************************
1635 ****************************************************************************/
1637 static int cmd_mput(void)
1642 while (next_token_nr(NULL
,p
,NULL
,sizeof(buf
))) {
1644 struct file_list
*temp_list
;
1645 char *quest
, *lname
, *rname
;
1649 ret
= file_find(&file_list
, ".", p
, True
);
1651 free_file_list(file_list
);
1659 for (temp_list
= file_list
; temp_list
;
1660 temp_list
= temp_list
->next
) {
1663 if (asprintf(&lname
, "%s/", temp_list
->file_path
) <= 0)
1665 trim_string(lname
, "./", "/");
1667 /* check if it's a directory */
1668 if (temp_list
->isdir
) {
1669 /* if (!recurse) continue; */
1672 if (asprintf(&quest
, "Put directory %s? ", lname
) < 0) break;
1673 if (prompt
&& !yesno(quest
)) { /* No */
1674 /* Skip the directory */
1675 lname
[strlen(lname
)-1] = '/';
1676 if (!seek_list(temp_list
, lname
))
1680 if(asprintf(&rname
, "smb:%s%s%s", service
, cur_dir
, lname
) < 0) break;
1681 /*dos_format(rname);*/
1683 /* test if the directory exists by opening it */
1684 /*if (!((dh=smbc_opendir(rname)) < 0)) {
1688 /* test if the directory exists by making it */
1689 ret
= smbc_mkdir(rname
, 755);
1690 if ((ret
!= 0) && (errno
!= EEXIST
))
1692 d_printf("Error: %s. Unable to open or create dir %s, skipping...\n", strerror(errno
), rname
);
1693 /* Skip the directory */
1694 lname
[strlen(lname
)-1] = '/';
1695 if (!seek_list(temp_list
, lname
))
1702 if (asprintf(&quest
,"Put file %s? ", lname
) < 0) break;
1703 if (prompt
&& !yesno(quest
)) /* No */
1708 if (asprintf(&rname
, "smb:%s%s%s", service
, cur_dir
, lname
) < 0) break;
1711 /*d_printf("PUT: rname: %s, lname: %s\n", rname, lname);*/
1712 do_put(rname
, lname
, False
);
1714 free_file_list(file_list
);
1723 /****************************************************************************
1725 ****************************************************************************/
1727 static int do_cancel(int job
)
1729 if (cli_printjob_del(cli
, job
)) {
1730 d_printf("Job %d cancelled\n",job
);
1733 d_printf("Error cancelling job %d : %s\n",job
,cli_errstr(cli
));
1738 /****************************************************************************
1740 ****************************************************************************/
1742 static int cmd_cancel(void)
1747 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1748 d_printf("cancel <jobid> ...\n");
1754 } while (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)));
1759 /****************************************************************************
1761 ****************************************************************************/
1763 static int cmd_print(void)
1769 if (!next_token_nr(NULL
,lname
,NULL
, sizeof(lname
))) {
1770 d_printf("print <filename>\n");
1774 pstrcpy(rname
,lname
);
1775 p
= strrchr_m(rname
,'/');
1777 slprintf(rname
, sizeof(rname
)-1, "%s-%d", p
+1, (int)sys_getpid());
1780 if (strequal(lname
,"-")) {
1781 slprintf(rname
, sizeof(rname
)-1, "stdin-%d", (int)sys_getpid());
1784 return do_put(rname
, lname
, False
);
1787 /****************************************************************************
1788 Show a print queue entry.
1789 ****************************************************************************/
1791 static void queue_fn(struct print_job_info
*p
)
1793 d_printf("%-6d %-9d %s\n", (int)p
->id
, (int)p
->size
, p
->name
);
1796 /****************************************************************************
1798 ****************************************************************************/
1800 static int cmd_queue(void)
1802 cli_print_queue(cli
, queue_fn
);
1807 /****************************************************************************
1809 ****************************************************************************/
1811 static void do_del(file_info
*finfo
)
1815 pstrcpy(mask
,cur_dir
);
1816 pstrcat(mask
,finfo
->name
);
1818 if (finfo
->mode
& aDIR
)
1821 if (!cli_unlink(cli
, mask
)) {
1822 d_printf("%s deleting remote file %s\n",cli_errstr(cli
),mask
);
1826 /****************************************************************************
1828 ****************************************************************************/
1830 static int cmd_del(void)
1834 uint16 attribute
= aSYSTEM
| aHIDDEN
;
1839 pstrcpy(mask
,cur_dir
);
1841 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1842 d_printf("del <filename>\n");
1847 do_list(mask
, attribute
,do_del
,False
,False
);
1852 /****************************************************************************
1853 ****************************************************************************/
1855 static int cmd_open(void)
1859 struct cli_state
*targetcli
;
1862 pstrcpy(mask
,cur_dir
);
1864 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1865 d_printf("open <filename>\n");
1870 if ( !cli_resolve_path( "", cli
, mask
, &targetcli
, targetname
) ) {
1871 d_printf("open %s: %s\n", mask
, cli_errstr(cli
));
1875 cli_nt_create(targetcli
, targetname
, FILE_READ_DATA
);
1881 /****************************************************************************
1883 ****************************************************************************/
1885 static int cmd_rmdir(void)
1889 struct cli_state
*targetcli
;
1892 pstrcpy(mask
, "smb:");
1893 pstrcat(mask
, service
);
1894 pstrcat(mask
,cur_dir
);
1896 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
1897 d_printf("rmdir <dirname>\n");
1902 /*if ( !cli_resolve_path( "", cli, mask, &targetcli, targetname ) ) {
1903 d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
1907 if (!cli_rmdir(targetcli, targetname)) {
1908 d_printf("%s removing remote directory file %s\n",
1909 cli_errstr(targetcli),mask);
1912 if (smbc_rmdir(mask
) < 0)
1913 d_printf("Error: %s removing remote directory file %s\n", strerror(errno
), mask
);
1918 /****************************************************************************
1920 ****************************************************************************/
1922 static int cmd_link(void)
1924 pstring oldname
,newname
;
1926 struct cli_state
*targetcli
;
1929 pstrcpy(oldname
,cur_dir
);
1930 pstrcpy(newname
,cur_dir
);
1932 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
1933 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1934 d_printf("link <oldname> <newname>\n");
1938 pstrcat(oldname
,buf
);
1939 pstrcat(newname
,buf2
);
1941 if ( !cli_resolve_path( "", cli
, oldname
, &targetcli
, targetname
) ) {
1942 d_printf("link %s: %s\n", oldname
, cli_errstr(cli
));
1946 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
1947 d_printf("Server doesn't support UNIX CIFS calls.\n");
1951 if (!cli_unix_hardlink(targetcli
, targetname
, newname
)) {
1952 d_printf("%s linking files (%s -> %s)\n", cli_errstr(targetcli
), newname
, oldname
);
1959 /****************************************************************************
1961 ****************************************************************************/
1963 static int cmd_symlink(void)
1965 pstring oldname
,newname
;
1968 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
1969 d_printf("Server doesn't support UNIX CIFS calls.\n");
1973 pstrcpy(newname
,cur_dir
);
1975 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
1976 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
1977 d_printf("symlink <oldname> <newname>\n");
1981 pstrcpy(oldname
,buf
);
1982 pstrcat(newname
,buf2
);
1984 if (!cli_unix_symlink(cli
, oldname
, newname
)) {
1985 d_printf("%s symlinking files (%s -> %s)\n",
1986 cli_errstr(cli
), newname
, oldname
);
1993 /****************************************************************************
1995 ****************************************************************************/
1997 static int cmd_chmod(void)
2002 struct cli_state
*targetcli
;
2005 pstrcpy(src
,cur_dir
);
2007 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2008 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
2009 d_printf("chmod mode file\n");
2013 mode
= (mode_t
)strtol(buf
, NULL
, 8);
2016 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2017 d_printf("chmod %s: %s\n", src
, cli_errstr(cli
));
2021 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2022 d_printf("Server doesn't support UNIX CIFS calls.\n");
2026 if (!cli_unix_chmod(targetcli
, targetname
, mode
)) {
2027 d_printf("%s chmod file %s 0%o\n",
2028 cli_errstr(targetcli
), src
, (unsigned int)mode
);
2035 static const char *filetype_to_str(mode_t mode
)
2037 if (S_ISREG(mode
)) {
2038 return "regular file";
2039 } else if (S_ISDIR(mode
)) {
2043 if (S_ISCHR(mode
)) {
2044 return "character device";
2048 if (S_ISBLK(mode
)) {
2049 return "block device";
2053 if (S_ISFIFO(mode
)) {
2058 if (S_ISLNK(mode
)) {
2059 return "symbolic link";
2063 if (S_ISSOCK(mode
)) {
2070 static char rwx_to_str(mode_t m
, mode_t bt
, char ret
)
2079 static char *unix_mode_to_str(char *s
, mode_t m
)
2082 const char *str
= filetype_to_str(m
);
2098 *p
++ = str
[1] == 'y' ? 'l' : 's';
2105 *p
++ = rwx_to_str(m
, S_IRUSR
, 'r');
2106 *p
++ = rwx_to_str(m
, S_IWUSR
, 'w');
2107 *p
++ = rwx_to_str(m
, S_IXUSR
, 'x');
2108 *p
++ = rwx_to_str(m
, S_IRGRP
, 'r');
2109 *p
++ = rwx_to_str(m
, S_IWGRP
, 'w');
2110 *p
++ = rwx_to_str(m
, S_IXGRP
, 'x');
2111 *p
++ = rwx_to_str(m
, S_IROTH
, 'r');
2112 *p
++ = rwx_to_str(m
, S_IWOTH
, 'w');
2113 *p
++ = rwx_to_str(m
, S_IXOTH
, 'x');
2118 /****************************************************************************
2119 Utility function for UNIX getfacl.
2120 ****************************************************************************/
2122 static char *perms_to_string(fstring permstr
, unsigned char perms
)
2124 fstrcpy(permstr
, "---");
2125 if (perms
& SMB_POSIX_ACL_READ
) {
2128 if (perms
& SMB_POSIX_ACL_WRITE
) {
2131 if (perms
& SMB_POSIX_ACL_EXECUTE
) {
2137 /****************************************************************************
2139 ****************************************************************************/
2141 static int cmd_getfacl(void)
2144 uint16 major
, minor
;
2145 uint32 caplow
, caphigh
;
2146 char *retbuf
= NULL
;
2148 SMB_STRUCT_STAT sbuf
;
2149 uint16 num_file_acls
= 0;
2150 uint16 num_dir_acls
= 0;
2152 struct cli_state
*targetcli
;
2155 pstrcpy(src
,cur_dir
);
2157 if (!next_token_nr(NULL
,name
,NULL
,sizeof(name
))) {
2158 d_printf("stat file\n");
2164 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2165 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
2169 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2170 d_printf("Server doesn't support UNIX CIFS calls.\n");
2174 if (!cli_unix_extensions_version(targetcli
, &major
, &minor
, &caplow
, &caphigh
)) {
2175 d_printf("Can't get UNIX CIFS version from server.\n");
2179 if (!(caplow
& CIFS_UNIX_POSIX_ACLS_CAP
)) {
2180 d_printf("This server supports UNIX extensions but doesn't support POSIX ACLs.\n");
2185 if (!cli_unix_stat(targetcli
, targetname
, &sbuf
)) {
2186 d_printf("%s getfacl doing a stat on file %s\n",
2187 cli_errstr(targetcli
), src
);
2191 if (!cli_unix_getfacl(targetcli
, targetname
, &rb_size
, &retbuf
)) {
2192 d_printf("%s getfacl file %s\n",
2193 cli_errstr(targetcli
), src
);
2197 /* ToDo : Print out the ACL values. */
2198 if (SVAL(retbuf
,0) != SMB_POSIX_ACL_VERSION
|| rb_size
< 6) {
2199 d_printf("getfacl file %s, unknown POSIX acl version %u.\n",
2200 src
, (unsigned int)CVAL(retbuf
,0) );
2205 num_file_acls
= SVAL(retbuf
,2);
2206 num_dir_acls
= SVAL(retbuf
,4);
2207 if (rb_size
!= SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)) {
2208 d_printf("getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u).\n",
2210 (unsigned int)(SMB_POSIX_ACL_HEADER_SIZE
+ SMB_POSIX_ACL_ENTRY_SIZE
*(num_file_acls
+num_dir_acls
)),
2211 (unsigned int)rb_size
);
2217 d_printf("# file: %s\n", src
);
2218 d_printf("# owner: %u\n# group: %u\n", (unsigned int)sbuf
.st_uid
, (unsigned int)sbuf
.st_gid
);
2220 if (num_file_acls
== 0 && num_dir_acls
== 0) {
2221 d_printf("No acls found.\n");
2224 for (i
= 0; i
< num_file_acls
; i
++) {
2227 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
));
2228 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
2231 case SMB_POSIX_ACL_USER_OBJ
:
2234 case SMB_POSIX_ACL_USER
:
2235 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
2236 d_printf("user:%u:", uorg
);
2238 case SMB_POSIX_ACL_GROUP_OBJ
:
2239 d_printf("group::");
2241 case SMB_POSIX_ACL_GROUP
:
2242 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+(i
*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
2243 d_printf("group:%u", uorg
);
2245 case SMB_POSIX_ACL_MASK
:
2248 case SMB_POSIX_ACL_OTHER
:
2249 d_printf("other::");
2252 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2253 src
, (unsigned int)tagtype
);
2258 d_printf("%s\n", perms_to_string(permstring
, perms
));
2261 for (i
= 0; i
< num_dir_acls
; i
++) {
2264 unsigned char tagtype
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
));
2265 unsigned char perms
= CVAL(retbuf
, SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+1);
2268 case SMB_POSIX_ACL_USER_OBJ
:
2269 d_printf("default:user::");
2271 case SMB_POSIX_ACL_USER
:
2272 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
2273 d_printf("default:user:%u:", uorg
);
2275 case SMB_POSIX_ACL_GROUP_OBJ
:
2276 d_printf("default:group::");
2278 case SMB_POSIX_ACL_GROUP
:
2279 uorg
= IVAL(retbuf
,SMB_POSIX_ACL_HEADER_SIZE
+((i
+num_file_acls
)*SMB_POSIX_ACL_ENTRY_SIZE
)+2);
2280 d_printf("default:group:%u", uorg
);
2282 case SMB_POSIX_ACL_MASK
:
2283 d_printf("default:mask::");
2285 case SMB_POSIX_ACL_OTHER
:
2286 d_printf("default:other::");
2289 d_printf("getfacl file %s, incorrect POSIX acl tagtype (%u).\n",
2290 src
, (unsigned int)tagtype
);
2295 d_printf("%s\n", perms_to_string(permstring
, perms
));
2302 /****************************************************************************
2304 ****************************************************************************/
2306 static int cmd_stat(void)
2310 SMB_STRUCT_STAT sbuf
;
2311 struct cli_state
*targetcli
;
2315 if (!SERVER_HAS_UNIX_CIFS(cli
)) {
2316 d_printf("Server doesn't support UNIX CIFS calls.\n");
2320 pstrcpy(src
,cur_dir
);
2322 if (!next_token_nr(NULL
,name
,NULL
,sizeof(name
))) {
2323 d_printf("stat file\n");
2330 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2331 d_printf("stat %s: %s\n", src
, cli_errstr(cli
));
2335 if (!cli_unix_stat(targetcli
, targetname
, &sbuf
)) {
2336 d_printf("%s stat file %s\n",
2337 cli_errstr(targetcli
), src
);
2341 /* Print out the stat values. */
2342 d_printf("File: %s\n", src
);
2343 d_printf("Size: %-12.0f\tBlocks: %u\t%s\n",
2344 (double)sbuf
.st_size
,
2345 (unsigned int)sbuf
.st_blocks
,
2346 filetype_to_str(sbuf
.st_mode
));
2348 #if defined(S_ISCHR) && defined(S_ISBLK)
2349 if (S_ISCHR(sbuf
.st_mode
) || S_ISBLK(sbuf
.st_mode
)) {
2350 d_printf("Inode: %.0f\tLinks: %u\tDevice type: %u,%u\n",
2351 (double)sbuf
.st_ino
,
2352 (unsigned int)sbuf
.st_nlink
,
2353 unix_dev_major(sbuf
.st_rdev
),
2354 unix_dev_minor(sbuf
.st_rdev
));
2357 d_printf("Inode: %.0f\tLinks: %u\n",
2358 (double)sbuf
.st_ino
,
2359 (unsigned int)sbuf
.st_nlink
);
2361 d_printf("Access: (0%03o/%s)\tUid: %u\tGid: %u\n",
2362 ((int)sbuf
.st_mode
& 0777),
2363 unix_mode_to_str(mode_str
, sbuf
.st_mode
),
2364 (unsigned int)sbuf
.st_uid
,
2365 (unsigned int)sbuf
.st_gid
);
2367 lt
= localtime(&sbuf
.st_atime
);
2369 strftime(mode_str
, sizeof(mode_str
), "%F %T %z", lt
);
2371 fstrcpy(mode_str
, "unknown");
2373 d_printf("Access: %s\n", mode_str
);
2375 lt
= localtime(&sbuf
.st_mtime
);
2377 strftime(mode_str
, sizeof(mode_str
), "%F %T %z", lt
);
2379 fstrcpy(mode_str
, "unknown");
2381 d_printf("Modify: %s\n", mode_str
);
2383 lt
= localtime(&sbuf
.st_ctime
);
2385 strftime(mode_str
, sizeof(mode_str
), "%F %T %z", lt
);
2387 fstrcpy(mode_str
, "unknown");
2389 d_printf("Change: %s\n", mode_str
);
2395 /****************************************************************************
2397 ****************************************************************************/
2399 static int cmd_chown(void)
2404 pstring buf
, buf2
, buf3
;
2405 struct cli_state
*targetcli
;
2408 pstrcpy(src
,cur_dir
);
2410 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2411 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
)) ||
2412 !next_token_nr(NULL
,buf3
,NULL
, sizeof(buf3
))) {
2413 d_printf("chown uid gid file\n");
2417 uid
= (uid_t
)atoi(buf
);
2418 gid
= (gid_t
)atoi(buf2
);
2421 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2422 d_printf("chown %s: %s\n", src
, cli_errstr(cli
));
2427 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2428 d_printf("Server doesn't support UNIX CIFS calls.\n");
2432 if (!cli_unix_chown(targetcli
, targetname
, uid
, gid
)) {
2433 d_printf("%s chown file %s uid=%d, gid=%d\n",
2434 cli_errstr(targetcli
), src
, (int)uid
, (int)gid
);
2441 /****************************************************************************
2443 ****************************************************************************/
2445 static int cmd_rename(void)
2449 pstring oname
, nname
;
2451 pstrcpy(src
, "smb:");
2452 pstrcat(src
, service
);
2453 pstrcat(src
, cur_dir
);
2456 if (!next_token_nr(NULL
,oname
,NULL
,sizeof(oname
)) ||
2457 !next_token_nr(NULL
,nname
,NULL
, sizeof(nname
))) {
2458 d_printf("rename <src> <dest>\n");
2462 pstrcat(src
, oname
);
2463 pstrcat(dest
, nname
);
2465 DEBUG(4, ("O: %s\nN: %s\n", src
, dest
));
2467 err
= smbc_rename(src
, dest
);
2470 d_printf("%s renaming files\n", strerror(errno
));
2477 /****************************************************************************
2478 Print the volume name.
2479 ****************************************************************************/
2481 static int cmd_volume(void)
2487 if (!cli_get_fs_volume_info(cli
, volname
, &serial_num
, &create_date
)) {
2488 d_printf("Errr %s getting volume info\n",cli_errstr(cli
));
2492 d_printf("Volume: |%s| serial number 0x%x\n", volname
, (unsigned int)serial_num
);
2496 /****************************************************************************
2497 Hard link files using the NT call.
2498 ****************************************************************************/
2500 static int cmd_hardlink(void)
2504 struct cli_state
*targetcli
;
2507 pstrcpy(src
,cur_dir
);
2508 pstrcpy(dest
,cur_dir
);
2510 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)) ||
2511 !next_token_nr(NULL
,buf2
,NULL
, sizeof(buf2
))) {
2512 d_printf("hardlink <src> <dest>\n");
2519 if ( !cli_resolve_path( "", cli
, src
, &targetcli
, targetname
) ) {
2520 d_printf("hardlink %s: %s\n", src
, cli_errstr(cli
));
2524 if (!SERVER_HAS_UNIX_CIFS(targetcli
)) {
2525 d_printf("Server doesn't support UNIX CIFS calls.\n");
2529 if (!cli_nt_hardlink(targetcli
, targetname
, dest
)) {
2530 d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli
));
2537 /****************************************************************************
2538 Toggle the prompt flag.
2539 ****************************************************************************/
2541 static int cmd_prompt(void)
2544 DEBUG(2,("prompting is now %s\n",prompt
?"on":"off"));
2549 /****************************************************************************
2550 Set the newer than time.
2551 ****************************************************************************/
2553 static int cmd_newer(void)
2557 SMB_STRUCT_STAT sbuf
;
2559 ok
= next_token_nr(NULL
,buf
,NULL
,sizeof(buf
));
2560 if (ok
&& (sys_stat(buf
,&sbuf
) == 0)) {
2561 newer_than
= sbuf
.st_mtime
;
2562 DEBUG(1,("Getting files newer than %s",
2563 time_to_asc(newer_than
)));
2568 if (ok
&& newer_than
== 0) {
2569 d_printf("Error setting newer-than time\n");
2576 /****************************************************************************
2577 Set the archive level.
2578 ****************************************************************************/
2580 static int cmd_archive(void)
2584 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2585 archive_level
= atoi(buf
);
2587 d_printf("Archive level is %d\n",archive_level
);
2592 /****************************************************************************
2593 Toggle the lowercaseflag.
2594 ****************************************************************************/
2596 static int cmd_lowercase(void)
2598 lowercase
= !lowercase
;
2599 DEBUG(2,("filename lowercasing is now %s\n",lowercase
?"on":"off"));
2604 /****************************************************************************
2605 Toggle the case sensitive flag.
2606 ****************************************************************************/
2608 static int cmd_setcase(void)
2610 BOOL orig_case_sensitive
= cli_set_case_sensitive(cli
, False
);
2612 cli_set_case_sensitive(cli
, !orig_case_sensitive
);
2613 DEBUG(2,("filename case sensitivity is now %s\n",!orig_case_sensitive
?
2619 /****************************************************************************
2620 Toggle the recurse flag.
2621 ****************************************************************************/
2623 static int cmd_recurse(void)
2626 DEBUG(2,("directory recursion is now %s\n",recurse
?"on":"off"));
2631 /****************************************************************************
2632 Toggle the translate flag.
2633 ****************************************************************************/
2635 static int cmd_translate(void)
2637 translation
= !translation
;
2638 DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2639 translation
?"on":"off"));
2644 /****************************************************************************
2646 ****************************************************************************/
2648 static int cmd_lcd(void)
2653 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
)))
2655 DEBUG(2,("the local directory is now %s\n",sys_getwd(d
)));
2660 /****************************************************************************
2661 Get a file restarting at end of local file.
2662 ****************************************************************************/
2664 static int cmd_reget(void)
2667 pstring remote_name
;
2670 pstrcpy(remote_name
, cur_dir
);
2671 pstrcat(remote_name
, "/");
2673 p
= remote_name
+ strlen(remote_name
);
2675 if (!next_token_nr(NULL
, p
, NULL
, sizeof(remote_name
) - strlen(remote_name
))) {
2676 d_printf("reget <filename>\n");
2679 pstrcpy(local_name
, p
);
2680 clean_name(remote_name
);
2682 next_token_nr(NULL
, local_name
, NULL
, sizeof(local_name
));
2684 return do_get(remote_name
, local_name
, True
);
2687 /****************************************************************************
2688 Put a file restarting at end of local file.
2689 ****************************************************************************/
2691 static int cmd_reput(void)
2694 pstring remote_name
;
2699 pstrcpy(remote_name
, cur_dir
);
2700 pstrcat(remote_name
, "/");
2702 if (!next_token_nr(NULL
, p
, NULL
, sizeof(buf
))) {
2703 d_printf("reput <filename>\n");
2706 pstrcpy(local_name
, p
);
2708 if (!file_exist(local_name
, &st
)) {
2709 d_printf("%s does not exist\n", local_name
);
2713 if (next_token_nr(NULL
, p
, NULL
, sizeof(buf
)))
2714 pstrcat(remote_name
, p
);
2716 pstrcat(remote_name
, local_name
);
2718 clean_name(remote_name
);
2720 return do_put(remote_name
, local_name
, True
);
2723 /****************************************************************************
2725 ****************************************************************************/
2727 static void browse_fn(const char *name
, uint32 m
,
2728 const char *comment
, void *state
)
2736 case STYPE_DISKTREE
:
2737 fstrcpy(typestr
,"Disk"); break;
2739 fstrcpy(typestr
,"Printer"); break;
2741 fstrcpy(typestr
,"Device"); break;
2743 fstrcpy(typestr
,"IPC"); break;
2745 /* FIXME: If the remote machine returns non-ascii characters
2746 in any of these fields, they can corrupt the output. We
2747 should remove them. */
2749 d_printf("\t%-15s %-10.10s%s\n",
2750 name
,typestr
,comment
);
2752 d_printf ("%s|%s|%s\n",typestr
,name
,comment
);
2756 /****************************************************************************
2757 Try and browse available connections on a host.
2758 ****************************************************************************/
2760 static BOOL
browse_host(BOOL sort
)
2764 d_printf("\n\tSharename Type Comment\n");
2765 d_printf("\t--------- ---- -------\n");
2768 if((ret
= cli_RNetShareEnum(cli
, browse_fn
, NULL
)) == -1)
2769 d_printf("Error returning browse list: %s\n", cli_errstr(cli
));
2774 /****************************************************************************
2776 ****************************************************************************/
2778 static void server_fn(const char *name
, uint32 m
,
2779 const char *comment
, void *state
)
2783 d_printf("\t%-16s %s\n", name
, comment
);
2785 d_printf("%s|%s|%s\n",(char *)state
, name
, comment
);
2789 /****************************************************************************
2790 Try and browse available connections on a host.
2791 ****************************************************************************/
2793 static BOOL
list_servers(const char *wk_grp
)
2797 if (!cli
->server_domain
)
2801 d_printf("\n\tServer Comment\n");
2802 d_printf("\t--------- -------\n");
2804 fstrcpy( state
, "Server" );
2805 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_ALL
, server_fn
,
2809 d_printf("\n\tWorkgroup Master\n");
2810 d_printf("\t--------- -------\n");
2813 fstrcpy( state
, "Workgroup" );
2814 cli_NetServerEnum(cli
, cli
->server_domain
, SV_TYPE_DOMAIN_ENUM
,
2819 /****************************************************************************
2820 Print or set current VUID
2821 ****************************************************************************/
2823 static int cmd_vuid(void)
2827 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2828 d_printf("Current VUID is %d\n", cli
->vuid
);
2832 cli
->vuid
= atoi(buf
);
2836 /****************************************************************************
2837 Setup a new VUID, by issuing a session setup
2838 ****************************************************************************/
2840 static int cmd_logon(void)
2842 pstring l_username
, l_password
;
2845 if (!next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
2846 d_printf("logon <username> [<password>]\n");
2850 pstrcpy(l_username
, buf
);
2852 if (!next_token_nr(NULL
,buf2
,NULL
,sizeof(buf
)))
2854 char *pass
= getpass("Password: ");
2856 pstrcpy(l_password
, pass
);
2859 pstrcpy(l_password
, buf2
);
2861 if (!cli_session_setup(cli
, l_username
,
2862 l_password
, strlen(l_password
),
2863 l_password
, strlen(l_password
),
2865 d_printf("session setup failed: %s\n", cli_errstr(cli
));
2869 d_printf("Current VUID is %d\n", cli
->vuid
);
2874 /****************************************************************************
2875 list active connections
2876 ****************************************************************************/
2878 static int cmd_list_connect(void)
2885 /****************************************************************************
2886 display the current active client connection
2887 ****************************************************************************/
2889 static int cmd_show_connect( void )
2891 struct cli_state
*targetcli
;
2894 if ( !cli_resolve_path( "", cli
, cur_dir
, &targetcli
, targetpath
) ) {
2895 d_printf("showconnect %s: %s\n", cur_dir
, cli_errstr(cli
));
2899 d_printf("//%s/%s\n", targetcli
->desthost
, targetcli
->share
);
2903 /* Some constants for completing filename arguments */
2905 #define COMPL_NONE 0 /* No completions */
2906 #define COMPL_REMOTE 1 /* Complete remote filename */
2907 #define COMPL_LOCAL 2 /* Complete local filename */
2909 /* This defines the commands supported by this client.
2910 * NOTE: The "!" must be the last one in the list because it's fn pointer
2911 * field is NULL, and NULL in that field is used in process_tok()
2912 * (below) to indicate the end of the list. crh
2918 const char *description
;
2919 char compl_args
[2]; /* Completion argument info */
2921 {"?",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
2922 /* {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}}, */
2923 {"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
}},
2924 {"blocksize",cmd_block
,"blocksize <number> (default 20)",{COMPL_NONE
,COMPL_NONE
}},
2925 /* {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}}, */
2926 /* {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}}, */
2927 {"cd",cmd_cd
,"[directory] change/report the remote directory",{COMPL_REMOTE
,COMPL_NONE
}},
2928 /* {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}}, */
2929 /* {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}}, */
2930 /* {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}}, */
2931 {"dir",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
2932 /* {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}}, */
2933 {"exit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2934 {"get",cmd_get
,"<remote name> [local name] get a file",{COMPL_REMOTE
,COMPL_LOCAL
}},
2935 /* {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}}, */
2936 /* {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}}, */
2937 {"help",cmd_help
,"[command] give help on a command",{COMPL_NONE
,COMPL_NONE
}},
2938 {"history",cmd_history
,"displays the command history",{COMPL_NONE
,COMPL_NONE
}},
2939 {"lcd",cmd_lcd
,"[directory] change/report the local current working directory",{COMPL_LOCAL
,COMPL_NONE
}},
2940 /* {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}}, */
2941 /* {"listconnect",cmd_list_connect,"list open connections",{COMPL_NONE,COMPL_NONE}}, */
2942 /* {"logon",cmd_logon,"establish new logon",{COMPL_NONE,COMPL_NONE}}, */
2943 {"lowercase",cmd_lowercase
,"toggle lowercasing of filenames for get",{COMPL_NONE
,COMPL_NONE
}},
2944 {"ls",cmd_dir
,"<mask> list the contents of the current directory",{COMPL_REMOTE
,COMPL_NONE
}},
2945 /* {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}}, */
2946 {"md",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
2947 {"mget",cmd_mget
,"<mask> get all the matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2948 {"mkdir",cmd_mkdir
,"<directory> make a directory",{COMPL_NONE
,COMPL_NONE
}},
2949 /* {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}}, */
2950 {"mput",cmd_mput
,"<mask> put all matching files",{COMPL_REMOTE
,COMPL_NONE
}},
2951 {"newer",cmd_newer
,"<file> only mget files newer than the specified local file",{COMPL_LOCAL
,COMPL_NONE
}},
2952 /* {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}}, */
2953 /* {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}}, */
2954 {"prompt",cmd_prompt
,"toggle prompting for filenames for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
2955 {"put",cmd_put
,"<local name> [remote name] put a file",{COMPL_LOCAL
,COMPL_REMOTE
}},
2956 {"pwd",cmd_pwd
,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE
,COMPL_NONE
}},
2957 {"q",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2958 /* {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}}, */
2959 {"quit",cmd_quit
,"logoff the server",{COMPL_NONE
,COMPL_NONE
}},
2960 {"rd",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
2961 {"recurse",cmd_recurse
,"toggle directory recursion for mget and mput",{COMPL_NONE
,COMPL_NONE
}},
2962 /* {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},*/
2963 {"rename",cmd_rename
,"<src> <dest> rename some files",{COMPL_REMOTE
,COMPL_REMOTE
}},
2964 /* {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},*/
2965 /* {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},*/
2966 {"rmdir",cmd_rmdir
,"<directory> remove a directory",{COMPL_NONE
,COMPL_NONE
}},
2967 /* {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},*/
2968 /* {"showconnect",cmd_show_connect,"display the current active connection",{COMPL_NONE,COMPL_NONE}},*/
2969 /* {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},*/
2970 /* {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},*/
2971 {"tar",cmd_tar
,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE
,COMPL_NONE
}},
2972 {"tarmode",cmd_tarmode
,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE
,COMPL_NONE
}},
2973 {"translate",cmd_translate
,"toggle text translation for printing",{COMPL_NONE
,COMPL_NONE
}},
2974 /* {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}}, */
2975 /* {"vuid",cmd_vuid,"change current vuid",{COMPL_NONE,COMPL_NONE}}, */
2977 /* Yes, this must be here, see crh's comment above. */
2978 {"!",NULL
,"run a shell command on the local system",{COMPL_NONE
,COMPL_NONE
}},
2979 {NULL
,NULL
,NULL
,{COMPL_NONE
,COMPL_NONE
}}
2982 /*******************************************************************
2983 Lookup a command string in the list of commands, including
2985 ******************************************************************/
2987 static int process_tok(pstring tok
)
2989 int i
= 0, matches
= 0;
2991 int tok_len
= strlen(tok
);
2993 while ((commands
[i
].fn
!= NULL
) || (strequal(commands
[i
].name
, "!"))) {
2994 if (strequal(commands
[i
].name
,tok
)) {
2998 } else if (strnequal(commands
[i
].name
, tok
, tok_len
)) {
3007 else if (matches
== 1)
3013 /****************************************************************************
3015 ****************************************************************************/
3017 static int cmd_help(void)
3022 if (next_token_nr(NULL
,buf
,NULL
,sizeof(buf
))) {
3023 if ((i
= process_tok(buf
)) >= 0)
3024 d_printf("HELP %s:\n\t%s\n\n",commands
[i
].name
,commands
[i
].description
);
3026 while (commands
[i
].description
) {
3027 for (j
=0; commands
[i
].description
&& (j
<5); j
++) {
3028 d_printf("%-15s",commands
[i
].name
);
3037 /****************************************************************************
3038 Process a -c command string.
3039 ****************************************************************************/
3041 static int process_command_string(char *cmd
)
3047 /* establish the connection if not already */
3050 cli
= cli_cm_open(desthost
, service
, True
);
3055 while (cmd
[0] != '\0') {
3060 if ((p
= strchr_m(cmd
, ';')) == 0) {
3061 strncpy(line
, cmd
, 999);
3067 strncpy(line
, cmd
, p
- cmd
);
3068 line
[p
- cmd
] = '\0';
3072 /* and get the first part of the command */
3074 if (!next_token_nr(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
3076 if ((i
= process_tok(tok
)) >= 0) {
3077 rc
= commands
[i
].fn();
3078 } else if (i
== -2) {
3079 d_printf("%s: command abbreviation ambiguous\n",tok
);
3081 d_printf("%s: command not found\n",tok
);
3088 #define MAX_COMPLETIONS 100
3096 } completion_remote_t
;
3098 static void completion_remote_filter(const char *mnt
, file_info
*f
, const char *mask
, void *state
)
3100 completion_remote_t
*info
= (completion_remote_t
*)state
;
3102 if ((info
->count
< MAX_COMPLETIONS
- 1) && (strncmp(info
->text
, f
->name
, info
->len
) == 0) && (strcmp(f
->name
, ".") != 0) && (strcmp(f
->name
, "..") != 0)) {
3103 if ((info
->dirmask
[0] == 0) && !(f
->mode
& aDIR
))
3104 info
->matches
[info
->count
] = SMB_STRDUP(f
->name
);
3108 if (info
->dirmask
[0] != 0)
3109 pstrcpy(tmp
, info
->dirmask
);
3112 pstrcat(tmp
, f
->name
);
3115 info
->matches
[info
->count
] = SMB_STRDUP(tmp
);
3117 if (info
->matches
[info
->count
] == NULL
)
3120 smb_readline_ca_char(0);
3122 if (info
->count
== 1)
3123 info
->samelen
= strlen(info
->matches
[info
->count
]);
3125 while (strncmp(info
->matches
[info
->count
], info
->matches
[info
->count
-1], info
->samelen
) != 0)
3131 static char **remote_completion(const char *text
, int len
)
3135 completion_remote_t info
= { "", NULL
, 1, 0, NULL
, 0 };
3137 /* can't have non-static intialisation on Sun CC, so do it
3143 if (len
>= PATH_MAX
)
3146 info
.matches
= SMB_MALLOC_ARRAY(char *,MAX_COMPLETIONS
);
3147 if (!info
.matches
) return NULL
;
3148 info
.matches
[0] = NULL
;
3150 for (i
= len
-1; i
>= 0; i
--)
3151 if ((text
[i
] == '/') || (text
[i
] == '\\'))
3153 info
.text
= text
+i
+1;
3154 info
.samelen
= info
.len
= len
-i
-1;
3157 strncpy(info
.dirmask
, text
, i
+1);
3158 info
.dirmask
[i
+1] = 0;
3159 pstr_sprintf(dirmask
, "%s%*s*", cur_dir
, i
-1, text
);
3161 pstr_sprintf(dirmask
, "%s*", cur_dir
);
3163 if (cli_list(cli
, dirmask
, aDIR
| aSYSTEM
| aHIDDEN
, completion_remote_filter
, &info
) < 0)
3166 if (info
.count
== 2)
3167 info
.matches
[0] = SMB_STRDUP(info
.matches
[1]);
3169 info
.matches
[0] = SMB_MALLOC(info
.samelen
+1);
3170 if (!info
.matches
[0])
3172 strncpy(info
.matches
[0], info
.matches
[1], info
.samelen
);
3173 info
.matches
[0][info
.samelen
] = 0;
3175 info
.matches
[info
.count
] = NULL
;
3176 return info
.matches
;
3179 for (i
= 0; i
< info
.count
; i
++)
3180 free(info
.matches
[i
]);
3185 static char **completion_fn(const char *text
, int start
, int end
)
3187 smb_readline_ca_char(' ');
3190 const char *buf
, *sp
;
3194 buf
= smb_readline_get_line_buffer();
3198 sp
= strchr(buf
, ' ');
3202 for (i
= 0; commands
[i
].name
; i
++)
3203 if ((strncmp(commands
[i
].name
, text
, sp
- buf
) == 0) && (commands
[i
].name
[sp
- buf
] == 0))
3205 if (commands
[i
].name
== NULL
)
3211 if (sp
== (buf
+ start
))
3212 compl_type
= commands
[i
].compl_args
[0];
3214 compl_type
= commands
[i
].compl_args
[1];
3216 if (compl_type
== COMPL_REMOTE
)
3217 return remote_completion(text
, end
- start
);
3218 else /* fall back to local filename completion */
3222 int i
, len
, samelen
= 0, count
=1;
3224 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
3231 for (i
=0;commands
[i
].fn
&& count
< MAX_COMPLETIONS
-1;i
++) {
3232 if (strncmp(text
, commands
[i
].name
, len
) == 0) {
3233 matches
[count
] = SMB_STRDUP(commands
[i
].name
);
3234 if (!matches
[count
])
3237 samelen
= strlen(matches
[count
]);
3239 while (strncmp(matches
[count
], matches
[count
-1], samelen
) != 0)
3246 case 0: /* should never happen */
3250 matches
[0] = SMB_STRDUP(matches
[1]);
3253 matches
[0] = SMB_MALLOC(samelen
+1);
3256 strncpy(matches
[0], matches
[1], samelen
);
3257 matches
[0][samelen
] = 0;
3259 matches
[count
] = NULL
;
3263 for (i
= 0; i
< count
; i
++)
3271 /****************************************************************************
3272 Make sure we swallow keepalives during idle time.
3273 ****************************************************************************/
3275 static void readline_callback(void)
3278 struct timeval timeout
;
3279 static time_t last_t
;
3295 FD_SET(cli
->fd
,&fds
);
3298 timeout
.tv_usec
= 0;
3299 sys_select_intr(cli
->fd
+1,&fds
,NULL
,NULL
,&timeout
);
3301 /* We deliberately use receive_smb instead of
3302 client_receive_smb as we want to receive
3303 session keepalives and then drop them here.
3305 if (FD_ISSET(cli
->fd
,&fds
)) {
3306 receive_smb(cli
->fd
,cli
->inbuf
,0);
3310 cli_chkpath(cli
, "/");
3313 /****************************************************************************
3314 Process commands on stdin.
3315 ****************************************************************************/
3317 static int process_stdin(void)
3329 /* display a prompt */
3330 slprintf(the_prompt
, sizeof(the_prompt
)-1, "smb: %s> ", cur_dir
);
3331 /* Removed callback since we don't need to swallow keepalives with libsmbclient */
3332 /*cline = smb_readline(the_prompt, readline_callback, completion_fn);*/
3333 cline
= smb_readline(the_prompt
, NULL
, completion_fn
);
3337 pstrcpy(line
, cline
);
3339 /* special case - first char is ! */
3345 /* and get the first part of the command */
3347 if (!next_token_nr(&ptr
,tok
,NULL
,sizeof(tok
))) continue;
3349 if ((i
= process_tok(tok
)) >= 0) {
3350 rc
= commands
[i
].fn();
3351 } else if (i
== -2) {
3352 d_printf("%s: command abbreviation ambiguous\n",tok
);
3354 d_printf("%s: command not found\n",tok
);
3360 /****************************************************************************
3361 Process commands from the client.
3362 ****************************************************************************/
3364 static int process(char *base_directory
)
3368 /*cli = cli_cm_open(desthost, service, True);
3372 rc
= smbc_init(get_auth_data_fn
, 0); /* Initialize libsmbclient */
3375 d_printf("Error initializing libsmbclient: %s\n", strerror(errno
));
3379 if (*base_directory
) {
3380 rc
= do_cd(base_directory
);
3386 else /* start the auth fn*/
3394 rc
= process_command_string(cmdstr
);
3403 /****************************************************************************
3405 ****************************************************************************/
3407 static int do_host_query(char *query_host
)
3409 cli
= cli_cm_open(query_host
, "IPC$", True
);
3417 /* Workgroups simply don't make sense over anything
3418 else but port 139... */
3421 cli_cm_set_port( 139 );
3422 cli
= cli_cm_open(query_host
, "IPC$", True
);
3426 d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
3430 list_servers(lp_workgroup());
3437 /****************************************************************************
3438 Handle a tar operation.
3439 ****************************************************************************/
3441 static int do_tar_op(char *base_directory
)
3445 /* do we already have a connection? */
3447 cli
= cli_cm_open(desthost
, service
, True
);
3454 if (*base_directory
) {
3455 ret
= do_cd(base_directory
);
3469 /****************************************************************************
3470 Handle a message operation.
3471 ****************************************************************************/
3473 static int do_message_op(void)
3476 struct nmb_name called
, calling
;
3477 fstring server_name
;
3478 char name_type_hex
[10];
3481 make_nmb_name(&calling
, calling_name
, 0x0);
3482 make_nmb_name(&called
, desthost
, name_type
);
3484 fstrcpy(server_name
, desthost
);
3485 snprintf(name_type_hex
, sizeof(name_type_hex
), "#%X", name_type
);
3486 fstrcat(server_name
, name_type_hex
);
3492 /* we can only do messages over port 139 (to windows clients at least) */
3494 msg_port
= port
? port
: 139;
3496 if (!(cli
=cli_initialise()) || (cli_set_port(cli
, msg_port
) != msg_port
) ||
3497 !NT_STATUS_IS_OK(cli_connect(cli
, server_name
, &ip
))) {
3498 d_printf("Connection to %s failed\n", desthost
);
3502 if (!cli_session_request(cli
, &calling
, &called
)) {
3503 d_printf("session request failed\n");
3515 /****************************************************************************
3517 ****************************************************************************/
3519 int main(int argc
,char *argv
[])
3521 pstring base_directory
;
3524 BOOL message
= False
;
3526 static const char *new_name_resolve_order
= NULL
;
3530 fstring new_workgroup
;
3531 struct poptOption long_options
[] = {
3534 { "name-resolve", 'R', POPT_ARG_STRING
, &new_name_resolve_order
, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
3535 { "message", 'M', POPT_ARG_STRING
, NULL
, 'M', "Send message", "HOST" },
3536 { "ip-address", 'I', POPT_ARG_STRING
, NULL
, 'I', "Use this IP to connect to", "IP" },
3537 { "stderr", 'E', POPT_ARG_NONE
, NULL
, 'E', "Write messages to stderr instead of stdout" },
3538 { "list", 'L', POPT_ARG_STRING
, NULL
, 'L', "Get a list of shares available on a host", "HOST" },
3539 { "terminal", 't', POPT_ARG_STRING
, NULL
, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
3540 { "max-protocol", 'm', POPT_ARG_STRING
, NULL
, 'm', "Set the max protocol level", "LEVEL" },
3541 { "tar", 'T', POPT_ARG_STRING
, NULL
, 'T', "Command line tar", "<c|x>IXFqgbNan" },
3542 { "directory", 'D', POPT_ARG_STRING
, NULL
, 'D', "Start from directory", "DIR" },
3543 { "command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated commands" },
3544 { "send-buffer", 'b', POPT_ARG_INT
, &io_bufsize
, 'b', "Changes the transmit/send buffer", "BYTES" },
3545 { "port", 'p', POPT_ARG_INT
, &port
, 'p', "Port to connect to", "PORT" },
3546 { "grepable", 'g', POPT_ARG_NONE
, NULL
, 'g', "Produce grepable output" },
3548 POPT_COMMON_CONNECTION
3549 POPT_COMMON_CREDENTIALS
3555 pstrcpy(term_code
, KANJI
);
3561 *base_directory
= 0;
3563 /* initialize the workgroup name so we can determine whether or
3564 not it was set by a command line option */
3566 set_global_myworkgroup( "" );
3567 set_global_myname( "" );
3569 /* set default debug level to 0 regardless of what smb.conf sets */
3570 setup_logging( "smbctool", True
);
3571 DEBUGLEVEL_CLASS
[DBGC_ALL
] = 1;
3572 if ((dbf
= x_fdup(x_stderr
))) {
3573 x_setbuf( dbf
, NULL
);
3576 pc
= poptGetContext("smbclient", argc
, (const char **) argv
, long_options
,
3577 POPT_CONTEXT_KEEP_FIRST
);
3578 poptSetOtherOptionHelp(pc
, "service <password>");
3580 in_client
= True
; /* Make sure that we tell lp_load we are */
3582 while ((opt
= poptGetNextOpt(pc
)) != -1) {
3585 /* Messages are sent to NetBIOS name type 0x3
3586 * (Messenger Service). Make sure we default
3587 * to port 139 instead of port 445. srl,crh
3590 cli_cm_set_dest_name_type( name_type
);
3591 pstrcpy(desthost
,poptGetOptArg(pc
));
3593 cli_cm_set_port( 139 );
3598 dest_ip
= *interpret_addr2(poptGetOptArg(pc
));
3599 if (is_zero_ip_v4(dest_ip
))
3603 cli_cm_set_dest_ip( dest_ip
);
3611 display_set_stderr();
3615 pstrcpy(query_host
, poptGetOptArg(pc
));
3618 pstrcpy(term_code
, poptGetOptArg(pc
));
3621 max_protocol
= interpret_protocol(poptGetOptArg(pc
), max_protocol
);
3624 /* We must use old option processing for this. Find the
3625 * position of the -T option in the raw argv[]. */
3628 for (i
= 1; i
< argc
; i
++) {
3629 if (strncmp("-T", argv
[i
],2)==0)
3633 if (!(optnum
= tar_parseargs(argc
, argv
, poptGetOptArg(pc
), i
))) {
3634 poptPrintUsage(pc
, stderr
, 0);
3637 /* Now we must eat (optnum - i) options - they have
3638 * been processed by tar_parseargs().
3641 for (i
= 0; i
< optnum
; i
++)
3646 pstrcpy(base_directory
,poptGetOptArg(pc
));
3656 /* check for the -P option */
3659 cli_cm_set_port( port
);
3662 * Don't load debug level from smb.conf. It should be
3663 * set by cmdline arg or remain default (0)
3665 AllowDebugChange
= False
;
3667 /* save the workgroup...
3669 FIXME!! do we need to do this for other options as well
3670 (or maybe a generic way to keep lp_load() from overwriting
3673 fstrcpy( new_workgroup
, lp_workgroup() );
3674 pstrcpy( calling_name
, global_myname() );
3676 if ( override_logfile
)
3677 setup_logging( lp_logfile(), False
);
3681 if (!lp_load(dyn_CONFIGFILE
,True
,False
,False
,True
)) {
3682 fprintf(stderr
, "%s: Can't load %s - run testparm to debug it\n",
3683 argv
[0], dyn_CONFIGFILE
);
3688 if ( strlen(new_workgroup
) != 0 )
3689 set_global_myworkgroup( new_workgroup
);
3690 pstrcpy(workgroup
, lp_workgroup());
3692 if ( strlen(calling_name
) != 0 )
3693 set_global_myname( calling_name
);
3695 pstrcpy( calling_name
, global_myname() );
3697 if(poptPeekArg(pc
)) {
3698 pstrcpy(service
,poptGetArg(pc
));
3699 /* Convert any '\' characters in the service name to '/' characters */
3700 string_replace(service
, '\\','/');
3702 if (count_chars(service
,'/') < 3) {
3703 d_printf("\n%s: Not enough '/' characters in service\n",service
);
3704 poptPrintUsage(pc
, stderr
, 0);
3709 if (poptPeekArg(pc
) && !cmdline_auth_info
.got_pass
) {
3710 cmdline_auth_info
.got_pass
= True
;
3711 pstrcpy(cmdline_auth_info
.password
,poptGetArg(pc
));
3716 if(new_name_resolve_order
)
3717 lp_set_name_resolve_order(new_name_resolve_order
);
3719 if (!tar_type
&& !*query_host
&& !*service
&& !message
) {
3720 poptPrintUsage(pc
, stderr
, 0);
3724 poptFreeContext(pc
);
3726 /* store the username an password for dfs support */
3728 cli_cm_set_credentials( &cmdline_auth_info
);
3729 pstrcpy(username
, cmdline_auth_info
.username
);
3731 DEBUG(3,("Client started (version %s).\n", SAMBA_VERSION_STRING
));
3735 process_command_string(cmdstr
);
3736 return do_tar_op(base_directory
);
3740 char *qhost
= query_host
;
3743 while (*qhost
== '\\' || *qhost
== '/')
3746 if ((slash
= strchr_m(qhost
, '/'))
3747 || (slash
= strchr_m(qhost
, '\\'))) {
3751 if ((p
=strchr_m(qhost
, '#'))) {
3754 sscanf(p
, "%x", &name_type
);
3755 cli_cm_set_dest_name_type( name_type
);
3758 return do_host_query(qhost
);
3762 return do_message_op();
3765 if (process(base_directory
)) {