1996-05-04 11:50:46 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
SMB client
2005-02-23 20:29:28 +03:00
Copyright ( C ) Andrew Tridgell 1994 - 1998
Copyright ( C ) Simo Sorce 2001 - 2002
Copyright ( C ) Jelmer Vernooij 2003
Copyright ( C ) Gerald ( Jerry ) Carter 2004
1996-05-04 11:50:46 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
2004-10-07 08:01:18 +04:00
# include "client/client_proto.h"
1996-05-04 11:50:46 +04:00
# ifndef REGISTER
# define REGISTER 0
# endif
2005-04-06 20:28:04 +04:00
extern BOOL AllowDebugChange ;
extern BOOL override_logfile ;
extern char tar_type ;
1998-10-07 03:46:01 +04:00
extern BOOL in_client ;
2001-08-25 00:11:09 +04:00
static int port = 0 ;
1996-05-04 11:50:46 +04:00
pstring cur_dir = " \\ " ;
2002-07-15 14:35:28 +04:00
static pstring cd_path = " " ;
1998-11-09 06:45:49 +03:00
static pstring service ;
static pstring desthost ;
static pstring username ;
2005-02-24 03:11:42 +03:00
static pstring calling_name ;
2004-01-16 18:01:09 +03:00
static BOOL grepable = False ;
2003-04-14 08:05:48 +04:00
static char * cmdstr = NULL ;
2001-01-25 09:40:44 +03:00
static int io_bufsize = 64512 ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
static int name_type = 0x20 ;
2005-03-16 23:07:08 +03:00
extern int max_protocol ;
1996-05-04 11:50:46 +04:00
2004-03-12 17:37:37 +03:00
static int process_tok ( pstring tok ) ;
2001-10-09 23:12:18 +04:00
static int cmd_help ( void ) ;
1998-09-05 09:07:05 +04:00
1996-05-04 11:50:46 +04:00
/* 30 second timeout on most commands */
# define CLIENT_TIMEOUT (30*1000)
# define SHORT_TIMEOUT (5*1000)
/* value for unused fid field in trans2 secondary request */
# define FID_UNUSED (0xFFFF)
time_t newer_than = 0 ;
2002-07-15 14:35:28 +04:00
static int archive_level = 0 ;
1996-05-04 11:50:46 +04:00
2002-07-15 14:35:28 +04:00
static BOOL translation = False ;
1998-11-09 06:45:49 +03:00
static BOOL have_ip ;
1996-06-04 10:42:03 +04:00
1996-05-04 11:50:46 +04:00
/* clitar bits insert */
extern int blocksize ;
extern BOOL tar_inc ;
extern BOOL tar_reset ;
/* clitar bits end */
2002-07-15 14:35:28 +04:00
static BOOL prompt = True ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
static BOOL recurse = False ;
1996-05-04 11:50:46 +04:00
BOOL lowercase = False ;
2002-07-15 14:35:28 +04:00
static struct in_addr dest_ip ;
1996-05-04 11:50:46 +04:00
# define SEPARATORS " \t\n\r"
2002-07-15 14:35:28 +04:00
static BOOL abort_mget = True ;
1996-05-04 11:50:46 +04:00
2002-07-15 14:35:28 +04:00
static pstring fileselection = " " ;
1996-05-04 11:50:46 +04:00
extern file_info def_finfo ;
/* timing globals */
2002-11-09 19:57:45 +03:00
SMB_BIG_UINT get_total_size = 0 ;
unsigned int get_total_time_ms = 0 ;
static SMB_BIG_UINT put_total_size = 0 ;
static unsigned int put_total_time_ms = 0 ;
1996-05-04 11:50:46 +04:00
1997-07-28 22:59:57 +04:00
/* totals globals */
1999-03-30 14:25:20 +04:00
static double dir_total ;
1996-05-04 11:50:46 +04:00
2005-02-25 00:54:52 +03:00
/* root cli_state connection */
2005-02-22 06:31:22 +03:00
struct cli_state * cli ;
2005-02-23 20:29:28 +03:00
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Write to a local file with CR / LF - > LF translation if appropriate . Return the
number taken from the buffer . This may not equal the number written .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1996-05-04 11:50:46 +04:00
static int writefile ( int f , char * b , int n )
{
1998-11-09 06:45:49 +03:00
int i ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! translation ) {
return write ( f , b , n ) ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
i = 0 ;
while ( i < n ) {
if ( * b = = ' \r ' & & ( i < ( n - 1 ) ) & & * ( b + 1 ) = = ' \n ' ) {
b + + ; i + + ;
}
if ( write ( f , b , 1 ) ! = 1 ) {
break ;
}
b + + ;
i + + ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
return ( i ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Read from a file with LF - > CR / LF translation if appropriate . Return the
number read . read approx n bytes .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-09-10 15:08:57 +04:00
static int readfile ( char * b , int n , XFILE * f )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
int i ;
int c ;
1996-05-04 11:50:46 +04:00
2001-09-09 07:18:23 +04:00
if ( ! translation )
2001-09-10 15:08:57 +04:00
return x_fread ( b , 1 , n , f ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
i = 0 ;
1999-12-13 16:27:58 +03:00
while ( i < ( n - 1 ) & & ( i < BUFFER_SIZE ) ) {
2001-09-10 15:08:57 +04:00
if ( ( c = x_getc ( f ) ) = = EOF ) {
1998-11-09 06:45:49 +03:00
break ;
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( c = = ' \n ' ) { /* change all LFs to CR/LF */
b [ i + + ] = ' \r ' ;
}
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
b [ i + + ] = c ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
return ( i ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Send a message .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1998-11-09 06:45:49 +03:00
static void send_message ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
int total_len = 0 ;
int grp_id ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! cli_message_start ( cli , desthost , username , & grp_id ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " message start: %s \n " , cli_errstr ( cli ) ) ;
1998-11-09 06:45:49 +03:00
return ;
}
1996-05-04 11:50:46 +04:00
2001-09-07 18:14:57 +04:00
d_printf ( " Connected. Type your message, ending it with a Control-D \n " ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
while ( ! feof ( stdin ) & & total_len < 1600 ) {
int maxlen = MIN ( 1600 - total_len , 127 ) ;
pstring msg ;
int l = 0 ;
int c ;
1996-05-04 11:50:46 +04:00
1998-11-13 06:37:01 +03:00
ZERO_ARRAY ( msg ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
for ( l = 0 ; l < maxlen & & ( c = fgetc ( stdin ) ) ! = EOF ; l + + ) {
if ( c = = ' \n ' )
msg [ l + + ] = ' \r ' ;
msg [ l ] = c ;
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! cli_message_text ( cli , msg , l , grp_id ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " SMBsendtxt failed (%s) \n " , cli_errstr ( cli ) ) ;
1998-11-09 06:45:49 +03:00
return ;
}
total_len + = l ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
if ( total_len > = 1600 )
2001-09-07 18:14:57 +04:00
d_printf ( " the message was truncated to 1600 bytes \n " ) ;
1998-11-09 06:45:49 +03:00
else
2001-09-07 18:14:57 +04:00
d_printf ( " sent %d bytes \n " , total_len ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! cli_message_end ( cli , grp_id ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " SMBsendend failed (%s) \n " , cli_errstr ( cli ) ) ;
1998-11-09 06:45:49 +03:00
return ;
1996-05-04 11:50:46 +04:00
}
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Check the space on a device .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int do_dskattr ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
int total , bsize , avail ;
2005-02-23 20:29:28 +03:00
struct cli_state * targetcli ;
pstring targetpath ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , cur_dir , & targetcli , targetpath ) ) {
2005-02-23 20:29:28 +03:00
d_printf ( " Error in dskattr: %s \n " , cli_errstr ( cli ) ) ;
2005-03-23 02:18:41 +03:00
return 1 ;
2005-02-23 20:29:28 +03:00
}
1996-05-04 11:50:46 +04:00
2005-02-23 20:29:28 +03:00
if ( ! cli_dskattr ( targetcli , & bsize , & total , & avail ) ) {
d_printf ( " Error in dskattr: %s \n " , cli_errstr ( targetcli ) ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
2001-09-07 18:14:57 +04:00
d_printf ( " \n \t \t %d blocks of size %d. %d blocks available \n " ,
total , bsize , avail ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Show cd / pwd .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_pwd ( void )
1996-05-04 11:50:46 +04:00
{
2001-09-07 18:14:57 +04:00
d_printf ( " Current directory is %s " , service ) ;
d_printf ( " %s \n " , cur_dir ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Change directory - inner section .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int do_cd ( char * newdir )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
char * p = newdir ;
pstring saved_dir ;
pstring dname ;
2005-02-23 20:29:28 +03:00
pstring targetpath ;
struct cli_state * targetcli ;
2005-02-24 22:10:28 +03:00
SMB_STRUCT_STAT sbuf ;
uint32 attributes ;
2005-04-15 04:39:03 +04:00
int ret = 1 ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
dos_format ( newdir ) ;
2005-02-23 20:29:28 +03:00
/* Save the current directory in case the new directory is invalid */
1998-11-09 06:45:49 +03:00
pstrcpy ( saved_dir , cur_dir ) ;
2005-02-23 20:29:28 +03:00
1998-11-09 06:45:49 +03:00
if ( * p = = ' \\ ' )
pstrcpy ( cur_dir , p ) ;
else
pstrcat ( cur_dir , p ) ;
2005-02-23 20:29:28 +03:00
2006-06-15 15:17:57 +04:00
if ( ( cur_dir [ 0 ] ! = ' \0 ' ) & & ( * ( cur_dir + strlen ( cur_dir ) - 1 ) ! = ' \\ ' ) ) {
1998-11-09 06:45:49 +03:00
pstrcat ( cur_dir , " \\ " ) ;
}
2005-02-24 22:10:28 +03:00
1998-11-09 06:45:49 +03:00
dos_clean_name ( cur_dir ) ;
2005-02-23 20:29:28 +03:00
pstrcpy ( dname , cur_dir ) ;
1998-11-09 06:45:49 +03:00
pstrcat ( cur_dir , " \\ " ) ;
dos_clean_name ( cur_dir ) ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , dname , & targetcli , targetpath ) ) {
2005-02-23 20:29:28 +03:00
d_printf ( " cd %s: %s \n " , dname , cli_errstr ( cli ) ) ;
pstrcpy ( cur_dir , saved_dir ) ;
2005-02-24 22:10:28 +03:00
goto out ;
2005-02-23 20:29:28 +03:00
}
2005-02-24 22:10:28 +03:00
if ( strequal ( targetpath , " \\ " ) )
return 0 ;
2005-11-18 06:18:54 +03:00
/* Use a trans2_qpathinfo to test directories for modern servers.
Except Win9x doesn ' t support the qpathinfo_basic ( ) call . . . . . */
2005-02-24 22:10:28 +03:00
2005-11-22 08:21:26 +03:00
if ( targetcli - > protocol > PROTOCOL_LANMAN2 & & ! targetcli - > win95 ) {
2005-02-24 22:10:28 +03:00
if ( ! cli_qpathinfo_basic ( targetcli , targetpath , & sbuf , & attributes ) ) {
d_printf ( " cd %s: %s \n " , dname , cli_errstr ( targetcli ) ) ;
pstrcpy ( cur_dir , saved_dir ) ;
goto out ;
}
if ( ! ( attributes & FILE_ATTRIBUTE_DIRECTORY ) ) {
d_printf ( " cd %s: not a directory \n " , dname ) ;
pstrcpy ( cur_dir , saved_dir ) ;
goto out ;
}
2005-04-15 04:39:03 +04:00
} else {
2005-02-24 22:10:28 +03:00
pstrcat ( targetpath , " \\ " ) ;
dos_clean_name ( targetpath ) ;
2005-02-23 20:29:28 +03:00
if ( ! cli_chkpath ( targetcli , targetpath ) ) {
d_printf ( " cd %s: %s \n " , dname , cli_errstr ( targetcli ) ) ;
1998-11-09 06:45:49 +03:00
pstrcpy ( cur_dir , saved_dir ) ;
2005-04-15 04:39:03 +04:00
goto out ;
1998-11-09 06:45:49 +03:00
}
}
2005-02-23 20:29:28 +03:00
2005-04-15 04:39:03 +04:00
ret = 0 ;
2005-02-24 22:10:28 +03:00
out :
2005-04-15 04:39:03 +04:00
1998-11-09 06:45:49 +03:00
pstrcpy ( cd_path , cur_dir ) ;
2005-04-15 04:39:03 +04:00
return ret ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Change directory .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_cd ( void )
1996-05-04 11:50:46 +04:00
{
2004-03-12 17:37:37 +03:00
pstring buf ;
2001-10-09 23:12:18 +04:00
int rc = 0 ;
2005-02-23 20:29:28 +03:00
2001-06-21 13:10:42 +04:00
if ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) )
2001-10-09 23:12:18 +04:00
rc = do_cd ( buf ) ;
1998-11-09 06:45:49 +03:00
else
2001-09-07 18:14:57 +04:00
d_printf ( " Current directory is %s \n " , cur_dir ) ;
2001-10-09 23:12:18 +04:00
return rc ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
/*******************************************************************
2003-08-07 00:01:31 +04:00
Decide if a file should be operated on .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
static BOOL do_this_one ( file_info * finfo )
{
2003-08-07 00:01:31 +04:00
if ( finfo - > mode & aDIR )
return ( True ) ;
1998-11-09 06:45:49 +03:00
if ( * fileselection & &
2000-04-30 15:04:28 +04:00
! mask_match ( finfo - > name , fileselection , False ) ) {
2002-09-25 19:19:00 +04:00
DEBUG ( 3 , ( " mask_match %s failed \n " , finfo - > name ) ) ;
1998-11-09 06:45:49 +03:00
return False ;
}
if ( newer_than & & finfo - > mtime < newer_than ) {
DEBUG ( 3 , ( " newer_than %s failed \n " , finfo - > name ) ) ;
return ( False ) ;
}
if ( ( archive_level = = 1 | | archive_level = = 2 ) & & ! ( finfo - > mode & aARCH ) ) {
DEBUG ( 3 , ( " archive %s failed \n " , finfo - > name ) ) ;
return ( False ) ;
}
return ( True ) ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Display info about a file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1996-05-04 11:50:46 +04:00
static void display_finfo ( file_info * finfo )
{
1998-11-09 06:45:49 +03:00
if ( do_this_one ( finfo ) ) {
time_t t = finfo - > mtime ; /* the time is assumed to be passed as GMT */
2001-09-07 18:14:57 +04:00
d_printf ( " %-30s%7.7s %8.0f %s " ,
2000-01-05 22:59:42 +03:00
finfo - > name ,
1998-11-09 06:45:49 +03:00
attrib_string ( finfo - > mode ) ,
( double ) finfo - > size ,
2006-06-15 01:36:49 +04:00
time_to_asc ( & t ) ) ;
1998-11-09 06:45:49 +03:00
dir_total + = finfo - > size ;
}
1996-05-04 11:50:46 +04:00
}
1998-09-21 12:45:11 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Accumulate size of a file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-09-21 12:45:11 +04:00
static void do_du ( file_info * finfo )
{
1998-11-09 06:45:49 +03:00
if ( do_this_one ( finfo ) ) {
dir_total + = finfo - > size ;
}
1998-09-21 12:45:11 +04:00
}
1998-11-09 06:45:49 +03:00
static BOOL do_list_recurse ;
static BOOL do_list_dirs ;
1999-12-13 16:27:58 +03:00
static char * do_list_queue = 0 ;
static long do_list_queue_size = 0 ;
static long do_list_queue_start = 0 ;
static long do_list_queue_end = 0 ;
1998-11-09 06:45:49 +03:00
static void ( * do_list_fn ) ( file_info * ) ;
1998-09-21 12:45:11 +04:00
1999-12-13 16:27:58 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Functions for do_list_queue .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-12-13 16:27:58 +03:00
/*
* The do_list_queue is a NUL - separated list of strings stored in a
* char * . Since this is a FIFO , we keep track of the beginning and
* ending locations of the data in the queue . When we overflow , we
* double the size of the char * . When the start of the data passes
* the midpoint , we move everything back . This is logically more
* complex than a linked list , but easier from a memory management
* angle . In any memory error condition , do_list_queue is reset .
* Functions check to ensure that do_list_queue is non - NULL before
* accessing it .
*/
2003-08-07 00:01:31 +04:00
1999-12-13 16:27:58 +03:00
static void reset_do_list_queue ( void )
{
2001-09-17 04:47:40 +04:00
SAFE_FREE ( do_list_queue ) ;
1999-12-13 16:27:58 +03:00
do_list_queue_size = 0 ;
do_list_queue_start = 0 ;
do_list_queue_end = 0 ;
}
static void init_do_list_queue ( void )
{
reset_do_list_queue ( ) ;
do_list_queue_size = 1024 ;
2004-12-07 21:25:53 +03:00
do_list_queue = SMB_MALLOC ( do_list_queue_size ) ;
1999-12-13 16:27:58 +03:00
if ( do_list_queue = = 0 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " malloc fail for size %d \n " ,
( int ) do_list_queue_size ) ;
1999-12-13 16:27:58 +03:00
reset_do_list_queue ( ) ;
} else {
memset ( do_list_queue , 0 , do_list_queue_size ) ;
}
}
static void adjust_do_list_queue ( void )
{
/*
* If the starting point of the queue is more than half way through ,
* move everything toward the beginning .
*/
2006-06-18 13:26:05 +04:00
if ( do_list_queue = = NULL ) {
DEBUG ( 4 , ( " do_list_queue is empty \n " ) ) ;
do_list_queue_start = do_list_queue_end = 0 ;
return ;
}
if ( do_list_queue_start = = do_list_queue_end ) {
1999-12-13 16:27:58 +03:00
DEBUG ( 4 , ( " do_list_queue is empty \n " ) ) ;
do_list_queue_start = do_list_queue_end = 0 ;
* do_list_queue = ' \0 ' ;
2003-08-07 00:01:31 +04:00
} else if ( do_list_queue_start > ( do_list_queue_size / 2 ) ) {
1999-12-13 16:27:58 +03:00
DEBUG ( 4 , ( " sliding do_list_queue backward \n " ) ) ;
memmove ( do_list_queue ,
do_list_queue + do_list_queue_start ,
do_list_queue_end - do_list_queue_start ) ;
do_list_queue_end - = do_list_queue_start ;
do_list_queue_start = 0 ;
}
}
static void add_to_do_list_queue ( const char * entry )
{
long new_end = do_list_queue_end + ( ( long ) strlen ( entry ) ) + 1 ;
2003-08-07 00:01:31 +04:00
while ( new_end > do_list_queue_size ) {
1999-12-13 16:27:58 +03:00
do_list_queue_size * = 2 ;
DEBUG ( 4 , ( " enlarging do_list_queue to %d \n " ,
( int ) do_list_queue_size ) ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
do_list_queue = SMB_REALLOC ( do_list_queue , do_list_queue_size ) ;
if ( ! do_list_queue ) {
2001-09-07 18:14:57 +04:00
d_printf ( " failure enlarging do_list_queue to %d bytes \n " ,
( int ) do_list_queue_size ) ;
1999-12-13 16:27:58 +03:00
reset_do_list_queue ( ) ;
2003-08-07 00:01:31 +04:00
} else {
1999-12-13 16:27:58 +03:00
memset ( do_list_queue + do_list_queue_size / 2 ,
0 , do_list_queue_size / 2 ) ;
}
}
2003-08-07 00:01:31 +04:00
if ( do_list_queue ) {
2003-07-18 05:36:11 +04:00
safe_strcpy_base ( do_list_queue + do_list_queue_end ,
entry , do_list_queue , do_list_queue_size ) ;
1999-12-13 16:27:58 +03:00
do_list_queue_end = new_end ;
DEBUG ( 4 , ( " added %s to do_list_queue (start=%d, end=%d) \n " ,
entry , ( int ) do_list_queue_start , ( int ) do_list_queue_end ) ) ;
}
}
static char * do_list_queue_head ( void )
{
return do_list_queue + do_list_queue_start ;
}
static void remove_do_list_queue_head ( void )
{
2003-08-07 00:01:31 +04:00
if ( do_list_queue_end > do_list_queue_start ) {
1999-12-13 16:27:58 +03:00
do_list_queue_start + = strlen ( do_list_queue_head ( ) ) + 1 ;
adjust_do_list_queue ( ) ;
DEBUG ( 4 , ( " removed head of do_list_queue (start=%d, end=%d) \n " ,
( int ) do_list_queue_start , ( int ) do_list_queue_end ) ) ;
}
}
static int do_list_queue_empty ( void )
{
return ( ! ( do_list_queue & & * do_list_queue ) ) ;
}
1996-06-04 10:42:03 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
A helper for do_list .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-02-26 17:42:55 +03:00
static void do_list_helper ( const char * mntpoint , file_info * f , const char * mask , void * state )
1996-06-04 10:42:03 +04:00
{
2005-10-13 19:51:25 +04:00
char * dir_end ;
/* save the directory */
pstrcpy ( f - > dir , mask ) ;
if ( ( dir_end = strrchr ( f - > dir , ' \\ ' ) ) ! = NULL ) {
* dir_end = ' \0 ' ;
}
1998-11-09 06:45:49 +03:00
if ( f - > mode & aDIR ) {
if ( do_list_dirs & & do_this_one ( f ) ) {
do_list_fn ( f ) ;
}
if ( do_list_recurse & &
! strequal ( f - > name , " . " ) & &
! strequal ( f - > name , " .. " ) ) {
pstring mask2 ;
char * p ;
2003-07-17 08:17:58 +04:00
if ( ! f - > name [ 0 ] ) {
d_printf ( " Empty dir name returned. Possible server misconfiguration. \n " ) ;
return ;
}
2005-02-26 17:42:55 +03:00
pstrcpy ( mask2 , mntpoint ) ;
pstrcat ( mask2 , mask ) ;
2001-07-04 11:36:09 +04:00
p = strrchr_m ( mask2 , ' \\ ' ) ;
2003-08-07 00:01:31 +04:00
if ( ! p )
return ;
1998-11-09 06:45:49 +03:00
p [ 1 ] = 0 ;
pstrcat ( mask2 , f - > name ) ;
1998-11-14 06:34:44 +03:00
pstrcat ( mask2 , " \\ * " ) ;
1999-12-13 16:27:58 +03:00
add_to_do_list_queue ( mask2 ) ;
1998-11-09 06:45:49 +03:00
}
return ;
1996-06-04 10:42:03 +04:00
}
1998-11-09 06:45:49 +03:00
if ( do_this_one ( f ) ) {
do_list_fn ( f ) ;
1996-06-04 10:42:03 +04:00
}
1998-11-09 06:45:49 +03:00
}
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
A wrapper around cli_list that adds recursion .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 23:33:37 +03:00
void do_list ( const char * mask , uint16 attribute , void ( * fn ) ( file_info * ) , BOOL rec , BOOL dirs )
1998-11-09 06:45:49 +03:00
{
1999-12-13 16:27:58 +03:00
static int in_do_list = 0 ;
2005-02-23 20:29:28 +03:00
struct cli_state * targetcli ;
pstring targetpath ;
1999-12-13 16:27:58 +03:00
2003-08-07 00:01:31 +04:00
if ( in_do_list & & rec ) {
1999-12-13 16:27:58 +03:00
fprintf ( stderr , " INTERNAL ERROR: do_list called recursively when the recursive flag is true \n " ) ;
exit ( 1 ) ;
}
in_do_list = 1 ;
1998-11-09 06:45:49 +03:00
do_list_recurse = rec ;
do_list_dirs = dirs ;
do_list_fn = fn ;
1996-06-04 10:42:03 +04:00
2003-08-07 00:01:31 +04:00
if ( rec ) {
1999-12-13 16:27:58 +03:00
init_do_list_queue ( ) ;
add_to_do_list_queue ( mask ) ;
2003-08-07 00:01:31 +04:00
while ( ! do_list_queue_empty ( ) ) {
1999-12-13 16:27:58 +03:00
/*
* Need to copy head so that it doesn ' t become
* invalid inside the call to cli_list . This
* would happen if the list were expanded
* during the call .
* Fix from E . Jay Berkenbilt ( ejb @ ql . org )
*/
pstring head ;
pstrcpy ( head , do_list_queue_head ( ) ) ;
2005-02-23 20:29:28 +03:00
/* check for dfs */
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , head , & targetcli , targetpath ) ) {
2005-02-23 20:29:28 +03:00
d_printf ( " do_list: [%s] %s \n " , head , cli_errstr ( cli ) ) ;
2005-02-26 17:42:55 +03:00
remove_do_list_queue_head ( ) ;
2005-02-23 20:29:28 +03:00
continue ;
}
cli_list ( targetcli , targetpath , attribute , do_list_helper , NULL ) ;
1999-12-13 16:27:58 +03:00
remove_do_list_queue_head ( ) ;
2003-08-07 00:01:31 +04:00
if ( ( ! do_list_queue_empty ( ) ) & & ( fn = = display_finfo ) ) {
1999-12-13 16:27:58 +03:00
char * next_file = do_list_queue_head ( ) ;
char * save_ch = 0 ;
if ( ( strlen ( next_file ) > = 2 ) & &
( next_file [ strlen ( next_file ) - 1 ] = = ' * ' ) & &
2003-08-07 00:01:31 +04:00
( next_file [ strlen ( next_file ) - 2 ] = = ' \\ ' ) ) {
1999-12-13 16:27:58 +03:00
save_ch = next_file +
strlen ( next_file ) - 2 ;
* save_ch = ' \0 ' ;
}
2001-09-07 18:14:57 +04:00
d_printf ( " \n %s \n " , next_file ) ;
2003-08-07 00:01:31 +04:00
if ( save_ch ) {
1999-12-13 16:27:58 +03:00
* save_ch = ' \\ ' ;
}
}
}
2003-08-07 00:01:31 +04:00
} else {
2005-02-23 20:29:28 +03:00
/* check for dfs */
2005-02-26 18:03:16 +03:00
if ( cli_resolve_path ( " " , cli , mask , & targetcli , targetpath ) ) {
2005-02-23 20:29:28 +03:00
if ( cli_list ( targetcli , targetpath , attribute , do_list_helper , NULL ) = = - 1 )
d_printf ( " %s listing %s \n " , cli_errstr ( targetcli ) , targetpath ) ;
1999-12-13 16:27:58 +03:00
}
2005-02-23 20:29:28 +03:00
else
d_printf ( " do_list: [%s] %s \n " , mask , cli_errstr ( cli ) ) ;
1999-12-13 16:27:58 +03:00
}
in_do_list = 0 ;
reset_do_list_queue ( ) ;
1998-11-09 06:45:49 +03:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Get a directory listing .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_dir ( void )
1998-11-09 06:45:49 +03:00
{
1998-11-09 23:33:37 +03:00
uint16 attribute = aDIR | aSYSTEM | aHIDDEN ;
1998-11-09 06:45:49 +03:00
pstring mask ;
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
char * p = buf ;
2001-10-09 23:12:18 +04:00
int rc ;
1998-11-09 06:45:49 +03:00
dir_total = 0 ;
2004-03-03 23:55:59 +03:00
if ( strcmp ( cur_dir , " \\ " ) ! = 0 ) {
pstrcpy ( mask , cur_dir ) ;
2006-06-15 15:17:57 +04:00
if ( ( mask [ 0 ] ! = ' \0 ' ) & & ( mask [ strlen ( mask ) - 1 ] ! = ' \\ ' ) )
2004-03-03 23:55:59 +03:00
pstrcat ( mask , " \\ " ) ;
} else {
2005-01-12 04:25:14 +03:00
pstrcpy ( mask , " \\ " ) ;
2004-03-03 23:55:59 +03:00
}
1998-11-09 06:45:49 +03:00
2001-06-21 13:10:42 +04:00
if ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
1998-11-09 06:45:49 +03:00
dos_format ( p ) ;
if ( * p = = ' \\ ' )
2005-01-12 04:25:14 +03:00
pstrcpy ( mask , p + 1 ) ;
1998-11-09 06:45:49 +03:00
else
pstrcat ( mask , p ) ;
2003-08-07 00:01:31 +04:00
} else {
2005-01-12 04:25:14 +03:00
pstrcat ( mask , " * " ) ;
1996-06-04 10:42:03 +04:00
}
1998-11-09 06:45:49 +03:00
do_list ( mask , attribute , display_finfo , recurse , True ) ;
1996-06-04 10:42:03 +04:00
2001-10-09 23:12:18 +04:00
rc = do_dskattr ( ) ;
1996-06-04 10:42:03 +04:00
1999-03-30 14:25:20 +04:00
DEBUG ( 3 , ( " Total bytes listed: %.0f \n " , dir_total ) ) ;
2001-10-09 23:12:18 +04:00
return rc ;
1996-06-04 10:42:03 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Get a directory listing .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_du ( void )
1996-06-04 10:42:03 +04:00
{
1998-11-09 23:33:37 +03:00
uint16 attribute = aDIR | aSYSTEM | aHIDDEN ;
1998-11-09 06:45:49 +03:00
pstring mask ;
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
char * p = buf ;
2001-10-09 23:12:18 +04:00
int rc ;
1998-11-09 06:45:49 +03:00
dir_total = 0 ;
pstrcpy ( mask , cur_dir ) ;
2006-06-15 15:17:57 +04:00
if ( ( mask [ 0 ] ! = ' \0 ' ) & & ( mask [ strlen ( mask ) - 1 ] ! = ' \\ ' ) )
1998-11-09 06:45:49 +03:00
pstrcat ( mask , " \\ " ) ;
2001-06-21 13:10:42 +04:00
if ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
1998-11-09 06:45:49 +03:00
dos_format ( p ) ;
if ( * p = = ' \\ ' )
pstrcpy ( mask , p ) ;
else
pstrcat ( mask , p ) ;
} else {
1998-11-14 06:34:44 +03:00
pstrcat ( mask , " * " ) ;
1996-06-04 10:42:03 +04:00
}
1998-11-09 06:45:49 +03:00
do_list ( mask , attribute , do_du , recurse , True ) ;
1996-06-04 10:42:03 +04:00
2001-10-09 23:12:18 +04:00
rc = do_dskattr ( ) ;
1996-06-04 10:42:03 +04:00
2001-09-07 18:14:57 +04:00
d_printf ( " Total number of bytes: %.0f \n " , dir_total ) ;
2001-10-09 23:12:18 +04:00
return rc ;
1998-11-09 06:45:49 +03:00
}
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Get a file from rname to lname
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-09-25 19:19:00 +04:00
static int do_get ( char * rname , char * lname , BOOL reget )
1998-11-09 06:45:49 +03:00
{
2002-09-25 19:19:00 +04:00
int handle = 0 , fnum ;
1998-11-09 06:45:49 +03:00
BOOL newhandle = False ;
char * data ;
struct timeval tp_start ;
1999-12-13 16:27:58 +03:00
int read_size = io_bufsize ;
1998-11-09 23:33:37 +03:00
uint16 attr ;
2005-03-23 00:17:01 +03:00
SMB_OFF_T size ;
2002-09-25 19:19:00 +04:00
off_t start = 0 ;
1998-11-09 06:45:49 +03:00
off_t nread = 0 ;
2001-10-09 23:12:18 +04:00
int rc = 0 ;
2005-02-23 21:27:13 +03:00
struct cli_state * targetcli ;
pstring targetname ;
1998-11-09 06:45:49 +03:00
if ( lowercase ) {
2003-07-03 23:11:31 +04:00
strlower_m ( lname ) ;
1998-11-09 06:45:49 +03:00
}
1996-06-04 10:42:03 +04:00
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , rname , & targetcli , targetname ) ) {
2005-02-23 21:27:13 +03:00
d_printf ( " Failed to open %s: %s \n " , rname , cli_errstr ( cli ) ) ;
return 1 ;
}
GetTimeOfDay ( & tp_start ) ;
2005-05-10 02:39:20 +04:00
if ( targetcli - > dfsroot ) {
pstring path ;
/* we need to refer to the full \server\share\path format
for dfs shares */
pstrcpy ( path , targetname ) ;
cli_dfs_make_full_path ( targetname , targetcli - > desthost ,
targetcli - > share , path ) ;
}
2005-02-23 21:27:13 +03:00
fnum = cli_open ( targetcli , targetname , O_RDONLY , DENY_NONE ) ;
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
if ( fnum = = - 1 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s opening remote file %s \n " , cli_errstr ( cli ) , rname ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
if ( ! strcmp ( lname , " - " ) ) {
handle = fileno ( stdout ) ;
} else {
2002-09-25 19:19:00 +04:00
if ( reget ) {
handle = sys_open ( lname , O_WRONLY | O_CREAT , 0644 ) ;
if ( handle > = 0 ) {
start = sys_lseek ( handle , 0 , SEEK_END ) ;
if ( start = = - 1 ) {
d_printf ( " Error seeking local file \n " ) ;
return 1 ;
}
}
} else {
handle = sys_open ( lname , O_WRONLY | O_CREAT | O_TRUNC , 0644 ) ;
}
1998-11-09 06:45:49 +03:00
newhandle = True ;
}
if ( handle < 0 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Error opening local file %s \n " , lname ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
1996-06-04 10:42:03 +04:00
2005-02-23 21:27:13 +03:00
if ( ! cli_qfileinfo ( targetcli , fnum ,
1998-11-14 06:53:24 +03:00
& attr , & size , NULL , NULL , NULL , NULL , NULL ) & &
2005-02-23 21:27:13 +03:00
! cli_getattrE ( targetcli , fnum ,
1998-11-14 06:53:24 +03:00
& attr , & size , NULL , NULL , NULL ) ) {
2005-02-23 21:27:13 +03:00
d_printf ( " getattrib: %s \n " , cli_errstr ( targetcli ) ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
1996-06-04 10:42:03 +04:00
2004-10-27 21:03:09 +04:00
DEBUG ( 1 , ( " getting file %s of size %.0f as %s " ,
2002-09-25 19:19:00 +04:00
rname , ( double ) size , lname ) ) ;
1996-06-04 10:42:03 +04:00
2004-12-07 21:25:53 +03:00
if ( ! ( data = ( char * ) SMB_MALLOC ( read_size ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " malloc fail for size %d \n " , read_size ) ;
2005-02-23 21:27:13 +03:00
cli_close ( targetcli , fnum ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1999-12-13 16:27:58 +03:00
}
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
while ( 1 ) {
2005-02-23 21:27:13 +03:00
int n = cli_read ( targetcli , fnum , data , nread + start , read_size ) ;
1996-06-04 10:42:03 +04:00
2003-08-07 00:01:31 +04:00
if ( n < = 0 )
break ;
1998-11-09 06:45:49 +03:00
if ( writefile ( handle , data , n ) ! = n ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Error writing local file \n " ) ;
2001-10-09 23:12:18 +04:00
rc = 1 ;
1998-11-09 06:45:49 +03:00
break ;
}
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
nread + = n ;
}
1999-12-13 16:27:58 +03:00
2002-09-25 19:19:00 +04:00
if ( nread + start < size ) {
1999-12-13 16:27:58 +03:00
DEBUG ( 0 , ( " Short read when getting file %s. Only got %ld bytes. \n " ,
2001-10-09 23:12:18 +04:00
rname , ( long ) nread ) ) ;
rc = 1 ;
1999-12-13 16:27:58 +03:00
}
2001-09-17 04:47:40 +04:00
SAFE_FREE ( data ) ;
1998-11-09 06:45:49 +03:00
2005-02-23 21:27:13 +03:00
if ( ! cli_close ( targetcli , fnum ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Error %s closing remote file \n " , cli_errstr ( cli ) ) ;
2001-10-09 23:12:18 +04:00
rc = 1 ;
1998-11-09 06:45:49 +03:00
}
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
if ( newhandle ) {
close ( handle ) ;
}
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
if ( archive_level > = 2 & & ( attr & aARCH ) ) {
1998-11-09 23:33:37 +03:00
cli_setatr ( cli , rname , attr & ~ ( uint16 ) aARCH , 0 ) ;
1998-11-09 06:45:49 +03:00
}
1996-06-04 10:42:03 +04:00
1998-11-09 06:45:49 +03:00
{
struct timeval tp_end ;
int this_time ;
GetTimeOfDay ( & tp_end ) ;
this_time =
( tp_end . tv_sec - tp_start . tv_sec ) * 1000 +
( tp_end . tv_usec - tp_start . tv_usec ) / 1000 ;
get_total_time_ms + = this_time ;
get_total_size + = nread ;
2004-10-27 21:03:09 +04:00
DEBUG ( 1 , ( " (%3.1f kb/s) (average %3.1f kb/s) \n " ,
1998-11-09 06:45:49 +03:00
nread / ( 1.024 * this_time + 1.0e-4 ) ,
get_total_size / ( 1.024 * get_total_time_ms ) ) ) ;
}
2001-10-09 23:12:18 +04:00
return rc ;
1996-06-04 10:42:03 +04:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Get a file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_get ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
pstring lname ;
pstring rname ;
char * p ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
pstrcpy ( rname , cur_dir ) ;
pstrcat ( rname , " \\ " ) ;
p = rname + strlen ( rname ) ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , p , NULL , sizeof ( rname ) - strlen ( rname ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " get <filename> \n " ) ;
2001-10-10 11:51:20 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
pstrcpy ( lname , p ) ;
dos_clean_name ( rname ) ;
2001-06-21 13:10:42 +04:00
next_token_nr ( NULL , lname , NULL , sizeof ( lname ) ) ;
1998-11-09 06:45:49 +03:00
2002-09-25 19:19:00 +04:00
return do_get ( rname , lname , False ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Do an mget operation on one file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
static void do_mget ( file_info * finfo )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
pstring rname ;
pstring quest ;
pstring saved_curdir ;
pstring mget_mask ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( strequal ( finfo - > name , " . " ) | | strequal ( finfo - > name , " .. " ) )
return ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( abort_mget ) {
2001-09-07 18:14:57 +04:00
d_printf ( " mget aborted \n " ) ;
1998-11-09 06:45:49 +03:00
return ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
if ( finfo - > mode & aDIR )
slprintf ( quest , sizeof ( pstring ) - 1 ,
2000-01-05 22:59:42 +03:00
" Get directory %s? " , finfo - > name ) ;
1998-11-09 06:45:49 +03:00
else
slprintf ( quest , sizeof ( pstring ) - 1 ,
2000-01-05 22:59:42 +03:00
" Get file %s? " , finfo - > name ) ;
1996-05-04 11:50:46 +04:00
2003-08-07 00:01:31 +04:00
if ( prompt & & ! yesno ( quest ) )
return ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! ( finfo - > mode & aDIR ) ) {
pstrcpy ( rname , cur_dir ) ;
pstrcat ( rname , finfo - > name ) ;
2002-09-25 19:19:00 +04:00
do_get ( rname , finfo - > name , False ) ;
1998-11-09 06:45:49 +03:00
return ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
/* handle directories */
pstrcpy ( saved_curdir , cur_dir ) ;
pstrcat ( cur_dir , finfo - > name ) ;
pstrcat ( cur_dir , " \\ " ) ;
unix_format ( finfo - > name ) ;
if ( lowercase )
2003-07-03 23:11:31 +04:00
strlower_m ( finfo - > name ) ;
1998-11-09 06:45:49 +03:00
2000-03-11 01:25:02 +03:00
if ( ! directory_exist ( finfo - > name , NULL ) & &
mkdir ( finfo - > name , 0777 ) ! = 0 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " failed to create directory %s \n " , finfo - > name ) ;
1998-11-09 06:45:49 +03:00
pstrcpy ( cur_dir , saved_curdir ) ;
return ;
}
2000-03-11 01:25:02 +03:00
if ( chdir ( finfo - > name ) ! = 0 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " failed to chdir to directory %s \n " , finfo - > name ) ;
1998-11-09 06:45:49 +03:00
pstrcpy ( cur_dir , saved_curdir ) ;
return ;
1996-06-04 10:42:03 +04:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
pstrcpy ( mget_mask , cur_dir ) ;
1998-11-14 06:34:44 +03:00
pstrcat ( mget_mask , " * " ) ;
1998-11-09 06:45:49 +03:00
do_list ( mget_mask , aSYSTEM | aHIDDEN | aDIR , do_mget , False , True ) ;
chdir ( " .. " ) ;
pstrcpy ( cur_dir , saved_curdir ) ;
1996-06-04 10:42:03 +04:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
View the file using the pager .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_more ( void )
1998-11-09 06:45:49 +03:00
{
2004-03-12 17:37:37 +03:00
pstring rname , lname , pager_cmd ;
1998-11-09 06:45:49 +03:00
char * pager ;
2001-04-12 03:19:08 +04:00
int fd ;
2001-10-09 23:12:18 +04:00
int rc = 0 ;
1996-05-04 11:50:46 +04:00
2004-03-12 17:37:37 +03:00
pstrcpy ( rname , cur_dir ) ;
pstrcat ( rname , " \\ " ) ;
1998-11-09 06:45:49 +03:00
2001-04-12 03:19:08 +04:00
slprintf ( lname , sizeof ( lname ) - 1 , " %s/smbmore.XXXXXX " , tmpdir ( ) ) ;
fd = smb_mkstemp ( lname ) ;
if ( fd = = - 1 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " failed to create temporary file for more \n " ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
2001-04-12 03:19:08 +04:00
}
close ( fd ) ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , rname + strlen ( rname ) , NULL , sizeof ( rname ) - strlen ( rname ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " more <filename> \n " ) ;
2001-04-12 03:19:08 +04:00
unlink ( lname ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
dos_clean_name ( rname ) ;
1996-05-04 11:50:46 +04:00
2002-09-25 19:19:00 +04:00
rc = do_get ( rname , lname , False ) ;
1998-04-11 11:52:13 +04:00
1998-11-09 06:45:49 +03:00
pager = getenv ( " PAGER " ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
slprintf ( pager_cmd , sizeof ( pager_cmd ) - 1 ,
2001-04-12 03:19:08 +04:00
" %s %s " , ( pager ? pager : PAGER ) , lname ) ;
1998-11-09 06:45:49 +03:00
system ( pager_cmd ) ;
2001-04-12 03:19:08 +04:00
unlink ( lname ) ;
2001-10-09 23:12:18 +04:00
return rc ;
1998-11-09 06:45:49 +03:00
}
1998-04-11 11:52:13 +04:00
1998-11-09 06:45:49 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Do a mget command .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_mget ( void )
1998-11-09 06:45:49 +03:00
{
1998-11-09 23:33:37 +03:00
uint16 attribute = aSYSTEM | aHIDDEN ;
1998-11-09 06:45:49 +03:00
pstring mget_mask ;
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
char * p = buf ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
* mget_mask = 0 ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( recurse )
attribute | = aDIR ;
abort_mget = False ;
1996-05-04 11:50:46 +04:00
2001-06-21 13:10:42 +04:00
while ( next_token_nr ( NULL , p , NULL , sizeof ( buf ) ) ) {
1998-11-09 06:45:49 +03:00
pstrcpy ( mget_mask , cur_dir ) ;
2006-06-15 15:24:01 +04:00
if ( ( mget_mask [ 0 ] ! = ' \0 ' ) & & ( mget_mask [ strlen ( mget_mask ) - 1 ] ! = ' \\ ' ) )
1998-11-09 06:45:49 +03:00
pstrcat ( mget_mask , " \\ " ) ;
if ( * p = = ' \\ ' )
pstrcpy ( mget_mask , p ) ;
else
pstrcat ( mget_mask , p ) ;
do_list ( mget_mask , attribute , do_mget , False , True ) ;
1996-06-04 10:42:03 +04:00
}
1998-11-09 06:45:49 +03:00
if ( ! * mget_mask ) {
pstrcpy ( mget_mask , cur_dir ) ;
if ( mget_mask [ strlen ( mget_mask ) - 1 ] ! = ' \\ ' )
pstrcat ( mget_mask , " \\ " ) ;
1998-11-14 06:34:44 +03:00
pstrcat ( mget_mask , " * " ) ;
1998-11-09 06:45:49 +03:00
do_list ( mget_mask , attribute , do_mget , False , True ) ;
1996-06-04 10:42:03 +04:00
}
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Make a directory of name " name " .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1998-11-09 06:45:49 +03:00
static BOOL do_mkdir ( char * name )
1996-05-04 11:50:46 +04:00
{
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
pstring targetname ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , name , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " mkdir %s: %s \n " , name , cli_errstr ( cli ) ) ;
return False ;
}
if ( ! cli_mkdir ( targetcli , targetname ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s making remote directory %s \n " ,
2005-02-23 22:26:32 +03:00
cli_errstr ( targetcli ) , name ) ;
1998-11-09 06:45:49 +03:00
return ( False ) ;
}
return ( True ) ;
1996-05-04 11:50:46 +04:00
}
2001-10-11 11:42:52 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Show 8.3 name of a file .
2001-10-11 11:42:52 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-11 11:42:52 +04:00
static BOOL do_altname ( char * name )
{
2004-03-12 17:37:37 +03:00
pstring altname ;
2001-10-11 11:42:52 +04:00
if ( ! NT_STATUS_IS_OK ( cli_qpathinfo_alt_name ( cli , name , altname ) ) ) {
d_printf ( " %s getting alt name for %s \n " ,
cli_errstr ( cli ) , name ) ;
return ( False ) ;
}
d_printf ( " %s \n " , altname ) ;
return ( True ) ;
}
1998-09-21 12:45:11 +04:00
/****************************************************************************
1999-12-13 16:27:58 +03:00
Exit client .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_quit ( void )
1998-09-21 12:45:11 +04:00
{
2005-02-23 22:26:32 +03:00
cli_cm_shutdown ( ) ;
1998-11-09 06:45:49 +03:00
exit ( 0 ) ;
2001-10-19 04:02:36 +04:00
/* NOTREACHED */
return 0 ;
1998-09-21 12:45:11 +04:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Make a directory .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_mkdir ( void )
1998-11-09 06:45:49 +03:00
{
pstring mask ;
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
char * p = buf ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
pstrcpy ( mask , cur_dir ) ;
1996-05-04 11:50:46 +04:00
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , p , NULL , sizeof ( buf ) ) ) {
1998-11-09 06:45:49 +03:00
if ( ! recurse )
2001-09-07 18:14:57 +04:00
d_printf ( " mkdir <dirname> \n " ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-10-24 06:49:09 +04:00
}
1998-11-09 06:45:49 +03:00
pstrcat ( mask , p ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( recurse ) {
pstring ddir ;
pstring ddir2 ;
* ddir2 = 0 ;
pstrcpy ( ddir , mask ) ;
2003-09-05 23:59:55 +04:00
trim_char ( ddir , ' . ' , ' \0 ' ) ;
1998-11-09 06:45:49 +03:00
p = strtok ( ddir , " / \\ " ) ;
while ( p ) {
pstrcat ( ddir2 , p ) ;
if ( ! cli_chkpath ( cli , ddir2 ) ) {
do_mkdir ( ddir2 ) ;
}
pstrcat ( ddir2 , " \\ " ) ;
p = strtok ( NULL , " / \\ " ) ;
}
} else {
do_mkdir ( mask ) ;
}
2001-10-09 23:12:18 +04:00
return 0 ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
2001-10-11 11:42:52 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Show alt name .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-11 12:40:42 +04:00
static int cmd_altname ( void )
2001-10-11 11:42:52 +04:00
{
pstring name ;
2004-03-12 17:37:37 +03:00
pstring buf ;
2001-10-11 11:42:52 +04:00
char * p = buf ;
pstrcpy ( name , cur_dir ) ;
if ( ! next_token_nr ( NULL , p , NULL , sizeof ( buf ) ) ) {
d_printf ( " altname <file> \n " ) ;
2001-10-11 12:40:42 +04:00
return 1 ;
2001-10-11 11:42:52 +04:00
}
pstrcat ( name , p ) ;
do_altname ( name ) ;
2001-10-11 12:40:42 +04:00
return 0 ;
2001-10-11 11:42:52 +04:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Put a single file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-09-25 19:19:00 +04:00
static int do_put ( char * rname , char * lname , BOOL reput )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
int fnum ;
2001-09-10 15:08:57 +04:00
XFILE * f ;
2005-03-23 00:17:01 +03:00
SMB_OFF_T start = 0 ;
2002-12-05 03:50:29 +03:00
off_t nread = 0 ;
2002-09-25 19:19:00 +04:00
char * buf = NULL ;
int maxwrite = io_bufsize ;
2001-10-09 23:12:18 +04:00
int rc = 0 ;
1998-11-09 06:45:49 +03:00
struct timeval tp_start ;
2005-02-23 21:27:13 +03:00
struct cli_state * targetcli ;
pstring targetname ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , rname , & targetcli , targetname ) ) {
2005-02-23 21:27:13 +03:00
d_printf ( " Failed to open %s: %s \n " , rname , cli_errstr ( cli ) ) ;
return 1 ;
}
1998-11-09 06:45:49 +03:00
GetTimeOfDay ( & tp_start ) ;
1996-05-04 11:50:46 +04:00
2002-09-25 19:19:00 +04:00
if ( reput ) {
2005-02-23 21:27:13 +03:00
fnum = cli_open ( targetcli , targetname , O_RDWR | O_CREAT , DENY_NONE ) ;
2002-09-25 19:19:00 +04:00
if ( fnum > = 0 ) {
2005-02-23 21:27:13 +03:00
if ( ! cli_qfileinfo ( targetcli , fnum , NULL , & start , NULL , NULL , NULL , NULL , NULL ) & &
! cli_getattrE ( targetcli , fnum , NULL , & start , NULL , NULL , NULL ) ) {
2002-09-25 19:19:00 +04:00
d_printf ( " getattrib: %s \n " , cli_errstr ( cli ) ) ;
return 1 ;
}
}
} else {
2005-02-23 21:27:13 +03:00
fnum = cli_open ( targetcli , targetname , O_RDWR | O_CREAT | O_TRUNC , DENY_NONE ) ;
2002-09-25 19:19:00 +04:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( fnum = = - 1 ) {
2005-02-23 21:27:13 +03:00
d_printf ( " %s opening remote file %s \n " , cli_errstr ( targetcli ) , rname ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
/* allow files to be piped into smbclient
2002-01-29 08:34:16 +03:00
jdblair 24. jun .98
Note that in this case this function will exit ( 0 ) rather
than returning . */
1998-11-09 06:45:49 +03:00
if ( ! strcmp ( lname , " - " ) ) {
2001-09-10 15:08:57 +04:00
f = x_stdin ;
1998-11-09 06:45:49 +03:00
/* size of file is not known */
} else {
2001-09-10 15:08:57 +04:00
f = x_fopen ( lname , O_RDONLY , 0 ) ;
2002-09-25 19:19:00 +04:00
if ( f & & reput ) {
if ( x_tseek ( f , start , SEEK_SET ) = = - 1 ) {
d_printf ( " Error seeking local file \n " ) ;
return 1 ;
}
}
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! f ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Error opening local file %s \n " , lname ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
DEBUG ( 1 , ( " putting file %s as %s " , lname ,
2000-01-05 22:59:42 +03:00
rname ) ) ;
1996-05-04 11:50:46 +04:00
2004-12-07 21:25:53 +03:00
buf = ( char * ) SMB_MALLOC ( maxwrite ) ;
2001-08-08 20:54:16 +04:00
if ( ! buf ) {
2001-09-07 18:14:57 +04:00
d_printf ( " ERROR: Not enough memory! \n " ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
2001-08-08 20:54:16 +04:00
}
2001-09-10 15:08:57 +04:00
while ( ! x_feof ( f ) ) {
1998-11-09 06:45:49 +03:00
int n = maxwrite ;
int ret ;
1996-05-04 11:50:46 +04:00
2001-09-09 06:43:30 +04:00
if ( ( n = readfile ( buf , n , f ) ) < 1 ) {
2001-09-10 15:08:57 +04:00
if ( ( n = = 0 ) & & x_feof ( f ) )
2000-01-07 00:23:49 +03:00
break ; /* Empty local file. */
2001-09-07 18:14:57 +04:00
d_printf ( " Error reading local file: %s \n " , strerror ( errno ) ) ;
2001-10-09 23:12:18 +04:00
rc = 1 ;
1998-11-09 06:45:49 +03:00
break ;
}
1996-05-04 11:50:46 +04:00
2005-02-23 21:27:13 +03:00
ret = cli_write ( targetcli , fnum , 0 , buf , nread + start , n ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( n ! = ret ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Error writing file: %s \n " , cli_errstr ( cli ) ) ;
2001-10-09 23:12:18 +04:00
rc = 1 ;
1998-11-09 06:45:49 +03:00
break ;
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
nread + = n ;
}
1996-05-04 11:50:46 +04:00
2005-02-23 21:27:13 +03:00
if ( ! cli_close ( targetcli , fnum ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s closing remote file %s \n " , cli_errstr ( cli ) , rname ) ;
2001-09-10 15:08:57 +04:00
x_fclose ( f ) ;
2001-09-17 04:47:40 +04:00
SAFE_FREE ( buf ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
2002-07-15 14:35:28 +04:00
if ( f ! = x_stdin ) {
x_fclose ( f ) ;
}
2001-09-17 04:47:40 +04:00
SAFE_FREE ( buf ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
{
struct timeval tp_end ;
int this_time ;
GetTimeOfDay ( & tp_end ) ;
this_time =
( tp_end . tv_sec - tp_start . tv_sec ) * 1000 +
( tp_end . tv_usec - tp_start . tv_usec ) / 1000 ;
put_total_time_ms + = this_time ;
put_total_size + = nread ;
1999-12-21 12:54:24 +03:00
DEBUG ( 1 , ( " (%3.1f kb/s) (average %3.1f kb/s) \n " ,
1998-11-09 06:45:49 +03:00
nread / ( 1.024 * this_time + 1.0e-4 ) ,
put_total_size / ( 1.024 * put_total_time_ms ) ) ) ;
}
1996-05-04 11:50:46 +04:00
2001-09-10 15:08:57 +04:00
if ( f = = x_stdin ) {
2005-02-23 22:26:32 +03:00
cli_cm_shutdown ( ) ;
1998-11-09 06:45:49 +03:00
exit ( 0 ) ;
}
2001-10-09 23:12:18 +04:00
return rc ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Put a file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_put ( void )
1998-11-09 06:45:49 +03:00
{
pstring lname ;
pstring rname ;
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
char * p = buf ;
pstrcpy ( rname , cur_dir ) ;
pstrcat ( rname , " \\ " ) ;
1996-05-04 11:50:46 +04:00
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , p , NULL , sizeof ( buf ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " put <filename> \n " ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
pstrcpy ( lname , p ) ;
1996-05-04 11:50:46 +04:00
2001-06-21 13:10:42 +04:00
if ( next_token_nr ( NULL , p , NULL , sizeof ( buf ) ) )
1998-11-09 06:45:49 +03:00
pstrcat ( rname , p ) ;
else
pstrcat ( rname , lname ) ;
dos_clean_name ( rname ) ;
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
SMB_STRUCT_STAT st ;
/* allow '-' to represent stdin
jdblair , 24. jun .98 */
if ( ! file_exist ( lname , & st ) & &
( strcmp ( lname , " - " ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s does not exist \n " , lname ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
}
1996-05-04 11:50:46 +04:00
2002-09-25 19:19:00 +04:00
return do_put ( rname , lname , False ) ;
1998-08-11 06:13:01 +04:00
}
1996-05-04 11:50:46 +04:00
2001-05-07 05:51:56 +04:00
/*************************************
2003-08-07 00:01:31 +04:00
File list structure .
2001-05-07 05:51:56 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static struct file_list {
struct file_list * prev , * next ;
char * file_path ;
BOOL isdir ;
} * file_list ;
/****************************************************************************
2003-08-07 00:01:31 +04:00
Free a file_list structure .
2001-05-07 05:51:56 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-03-14 02:58:58 +03:00
static void free_file_list ( struct file_list * list_head )
2001-05-07 05:51:56 +04:00
{
2006-03-14 02:58:58 +03:00
struct file_list * list , * next ;
2001-05-07 05:51:56 +04:00
2006-03-14 02:58:58 +03:00
for ( list = list_head ; list ; list = next ) {
next = list - > next ;
DLIST_REMOVE ( list_head , list ) ;
SAFE_FREE ( list - > file_path ) ;
SAFE_FREE ( list ) ;
2001-05-07 05:51:56 +04:00
}
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Seek in a directory / file list until you get something that doesn ' t start with
the specified name .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-07 05:51:56 +04:00
static BOOL seek_list ( struct file_list * list , char * name )
1996-05-04 11:50:46 +04:00
{
2001-05-07 05:51:56 +04:00
while ( list ) {
trim_string ( list - > file_path , " ./ " , " \n " ) ;
if ( strncmp ( list - > file_path , name , strlen ( name ) ) ! = 0 ) {
1998-11-09 06:45:49 +03:00
return ( True ) ;
}
2001-05-07 05:51:56 +04:00
list = list - > next ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
return ( False ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Set the file selection mask .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_select ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
pstrcpy ( fileselection , " " ) ;
2001-06-21 13:10:42 +04:00
next_token_nr ( NULL , fileselection , NULL , sizeof ( fileselection ) ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
2001-05-07 05:51:56 +04:00
/****************************************************************************
Recursive file matching function act as find
match must be always set to True when calling this function
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-05-07 05:51:56 +04:00
static int file_find ( struct file_list * * list , const char * directory ,
const char * expression , BOOL match )
{
2005-08-24 01:29:37 +04:00
SMB_STRUCT_DIR * dir ;
2001-05-07 05:51:56 +04:00
struct file_list * entry ;
struct stat statbuf ;
2001-05-07 09:19:52 +04:00
int ret ;
2001-05-07 05:51:56 +04:00
char * path ;
BOOL isdir ;
2003-03-18 12:43:00 +03:00
const char * dname ;
2001-05-07 05:51:56 +04:00
2005-08-24 01:29:37 +04:00
dir = sys_opendir ( directory ) ;
2003-08-07 00:01:31 +04:00
if ( ! dir )
return - 1 ;
2001-05-07 05:51:56 +04:00
2001-05-07 09:19:52 +04:00
while ( ( dname = readdirname ( dir ) ) ) {
2003-08-07 00:01:31 +04:00
if ( ! strcmp ( " .. " , dname ) )
continue ;
if ( ! strcmp ( " . " , dname ) )
continue ;
2001-05-07 05:51:56 +04:00
if ( asprintf ( & path , " %s/%s " , directory , dname ) < = 0 ) {
continue ;
}
isdir = False ;
2001-08-20 09:15:26 +04:00
if ( ! match | | ! gen_fnmatch ( expression , dname ) ) {
2001-05-07 05:51:56 +04:00
if ( recurse ) {
ret = stat ( path , & statbuf ) ;
2001-05-07 09:19:52 +04:00
if ( ret = = 0 ) {
2001-05-07 05:51:56 +04:00
if ( S_ISDIR ( statbuf . st_mode ) ) {
isdir = True ;
ret = file_find ( list , path , expression , False ) ;
}
} else {
2001-09-07 18:14:57 +04:00
d_printf ( " file_find: cannot stat file %s \n " , path ) ;
2001-05-07 05:51:56 +04:00
}
2001-05-07 09:19:52 +04:00
if ( ret = = - 1 ) {
2001-09-17 04:47:40 +04:00
SAFE_FREE ( path ) ;
2005-08-24 01:29:37 +04:00
sys_closedir ( dir ) ;
2001-05-07 09:19:52 +04:00
return - 1 ;
2001-05-07 05:51:56 +04:00
}
}
2004-12-07 21:25:53 +03:00
entry = SMB_MALLOC_P ( struct file_list ) ;
2001-05-07 05:51:56 +04:00
if ( ! entry ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Out of memory in file_find \n " ) ;
2005-08-24 01:29:37 +04:00
sys_closedir ( dir ) ;
2001-05-07 09:19:52 +04:00
return - 1 ;
2001-05-07 05:51:56 +04:00
}
entry - > file_path = path ;
entry - > isdir = isdir ;
DLIST_ADD ( * list , entry ) ;
} else {
2001-09-17 04:47:40 +04:00
SAFE_FREE ( path ) ;
2001-05-07 05:51:56 +04:00
}
}
2001-05-07 09:19:52 +04:00
2005-08-24 01:29:37 +04:00
sys_closedir ( dir ) ;
2001-05-07 05:51:56 +04:00
return 0 ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
mput some files .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_mput ( void )
1996-05-04 11:50:46 +04:00
{
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
char * p = buf ;
2001-06-21 13:10:42 +04:00
while ( next_token_nr ( NULL , p , NULL , sizeof ( buf ) ) ) {
2001-05-07 05:51:56 +04:00
int ret ;
struct file_list * temp_list ;
char * quest , * lname , * rname ;
file_list = NULL ;
2001-04-12 03:19:08 +04:00
2001-05-07 05:51:56 +04:00
ret = file_find ( & file_list , " . " , p , True ) ;
if ( ret ) {
free_file_list ( file_list ) ;
2001-04-12 03:19:08 +04:00
continue ;
}
1998-11-09 06:45:49 +03:00
2001-05-07 05:51:56 +04:00
quest = NULL ;
lname = NULL ;
rname = NULL ;
for ( temp_list = file_list ; temp_list ;
temp_list = temp_list - > next ) {
1998-11-09 06:45:49 +03:00
2001-09-17 04:47:40 +04:00
SAFE_FREE ( lname ) ;
2001-05-07 05:51:56 +04:00
if ( asprintf ( & lname , " %s/ " , temp_list - > file_path ) < = 0 )
continue ;
trim_string ( lname , " ./ " , " / " ) ;
1998-11-09 06:45:49 +03:00
/* check if it's a directory */
2001-05-07 05:51:56 +04:00
if ( temp_list - > isdir ) {
/* if (!recurse) continue; */
2001-09-17 04:47:40 +04:00
SAFE_FREE ( quest ) ;
2002-01-19 20:29:32 +03:00
if ( asprintf ( & quest , " Put directory %s? " , lname ) < 0 ) break ;
2001-05-07 05:51:56 +04:00
if ( prompt & & ! yesno ( quest ) ) { /* No */
/* Skip the directory */
lname [ strlen ( lname ) - 1 ] = ' / ' ;
if ( ! seek_list ( temp_list , lname ) )
break ;
} else { /* Yes */
2001-09-17 04:47:40 +04:00
SAFE_FREE ( rname ) ;
2002-01-19 20:29:32 +03:00
if ( asprintf ( & rname , " %s%s " , cur_dir , lname ) < 0 ) break ;
2001-05-07 05:51:56 +04:00
dos_format ( rname ) ;
if ( ! cli_chkpath ( cli , rname ) & &
! do_mkdir ( rname ) ) {
DEBUG ( 0 , ( " Unable to make dir, skipping... " ) ) ;
/* Skip the directory */
lname [ strlen ( lname ) - 1 ] = ' / ' ;
if ( ! seek_list ( temp_list , lname ) )
break ;
}
1998-11-09 06:45:49 +03:00
}
continue ;
} else {
2001-09-17 04:47:40 +04:00
SAFE_FREE ( quest ) ;
2002-01-19 20:29:32 +03:00
if ( asprintf ( & quest , " Put file %s? " , lname ) < 0 ) break ;
2001-05-07 05:51:56 +04:00
if ( prompt & & ! yesno ( quest ) ) /* No */
continue ;
1998-11-09 06:45:49 +03:00
2001-05-07 05:51:56 +04:00
/* Yes */
2001-09-17 04:47:40 +04:00
SAFE_FREE ( rname ) ;
2002-01-19 20:29:32 +03:00
if ( asprintf ( & rname , " %s%s " , cur_dir , lname ) < 0 ) break ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
dos_format ( rname ) ;
1996-05-04 11:50:46 +04:00
2002-09-25 19:19:00 +04:00
do_put ( rname , lname , False ) ;
1996-05-04 11:50:46 +04:00
}
2001-05-07 05:51:56 +04:00
free_file_list ( file_list ) ;
2001-09-17 04:47:40 +04:00
SAFE_FREE ( quest ) ;
SAFE_FREE ( lname ) ;
SAFE_FREE ( rname ) ;
1996-05-04 11:50:46 +04:00
}
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Cancel a print job .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int do_cancel ( int job )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
if ( cli_printjob_del ( cli , job ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Job %d cancelled \n " , job ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1998-11-09 06:45:49 +03:00
} else {
2001-09-07 18:14:57 +04:00
d_printf ( " Error cancelling job %d : %s \n " , job , cli_errstr ( cli ) ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Cancel a print job .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_cancel ( void )
1996-05-04 11:50:46 +04:00
{
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
int job ;
1996-05-04 11:50:46 +04:00
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " cancel <jobid> ... \n " ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
do {
job = atoi ( buf ) ;
do_cancel ( job ) ;
2001-06-21 13:10:42 +04:00
} while ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Print a file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-10-09 23:12:18 +04:00
static int cmd_print ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
pstring lname ;
pstring rname ;
char * p ;
1996-05-04 11:50:46 +04:00
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , lname , NULL , sizeof ( lname ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " print <filename> \n " ) ;
2001-10-10 11:51:20 +04:00
return 1 ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
pstrcpy ( rname , lname ) ;
2001-07-04 11:36:09 +04:00
p = strrchr_m ( rname , ' / ' ) ;
1998-11-09 06:45:49 +03:00
if ( p ) {
2000-05-02 06:23:41 +04:00
slprintf ( rname , sizeof ( rname ) - 1 , " %s-%d " , p + 1 , ( int ) sys_getpid ( ) ) ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
if ( strequal ( lname , " - " ) ) {
2000-05-02 06:23:41 +04:00
slprintf ( rname , sizeof ( rname ) - 1 , " stdin-%d " , ( int ) sys_getpid ( ) ) ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
2002-09-25 19:19:00 +04:00
return do_put ( rname , lname , False ) ;
1996-05-04 11:50:46 +04:00
}
1997-07-01 05:19:13 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Show a print queue entry .
1997-07-01 05:19:13 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1998-11-09 06:45:49 +03:00
static void queue_fn ( struct print_job_info * p )
1997-07-01 05:19:13 +04:00
{
2001-09-07 18:14:57 +04:00
d_printf ( " %-6d %-9d %s \n " , ( int ) p - > id , ( int ) p - > size , p - > name ) ;
1997-07-01 05:19:13 +04:00
}
1996-08-13 12:57:55 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Show a print queue .
1996-08-13 12:57:55 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_queue ( void )
1996-08-13 12:57:55 +04:00
{
2001-10-09 23:12:18 +04:00
cli_print_queue ( cli , queue_fn ) ;
return 0 ;
1996-08-13 12:57:55 +04:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Delete some files .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1996-05-04 11:50:46 +04:00
static void do_del ( file_info * finfo )
{
1998-11-09 06:45:49 +03:00
pstring mask ;
1996-05-04 11:50:46 +04:00
2005-10-13 19:51:25 +04:00
pstr_sprintf ( mask , " %s \\ %s " , finfo - > dir , finfo - > name ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( finfo - > mode & aDIR )
return ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! cli_unlink ( cli , mask ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s deleting remote file %s \n " , cli_errstr ( cli ) , mask ) ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Delete some files .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_del ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
pstring mask ;
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 23:33:37 +03:00
uint16 attribute = aSYSTEM | aHIDDEN ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( recurse )
attribute | = aDIR ;
pstrcpy ( mask , cur_dir ) ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " del <filename> \n " ) ;
2001-10-10 11:51:20 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
pstrcat ( mask , buf ) ;
do_list ( mask , attribute , do_del , False , False ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
1999-12-13 16:27:58 +03:00
/****************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_open ( void )
1999-12-13 16:27:58 +03:00
{
pstring mask ;
2004-03-12 17:37:37 +03:00
pstring buf ;
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
pstring targetname ;
1999-12-13 16:27:58 +03:00
pstrcpy ( mask , cur_dir ) ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
2001-10-09 23:12:18 +04:00
d_printf ( " open <filename> \n " ) ;
return 1 ;
1999-12-13 16:27:58 +03:00
}
pstrcat ( mask , buf ) ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , mask , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " open %s: %s \n " , mask , cli_errstr ( cli ) ) ;
return 1 ;
}
cli_nt_create ( targetcli , targetname , FILE_READ_DATA ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1999-12-13 16:27:58 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Remove a directory .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_rmdir ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
pstring mask ;
2004-03-12 17:37:37 +03:00
pstring buf ;
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
pstring targetname ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
pstrcpy ( mask , cur_dir ) ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " rmdir <dirname> \n " ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
pstrcat ( mask , buf ) ;
1996-05-04 11:50:46 +04:00
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , mask , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " rmdir %s: %s \n " , mask , cli_errstr ( cli ) ) ;
return 1 ;
}
if ( ! cli_rmdir ( targetcli , targetname ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s removing remote directory file %s \n " ,
2005-02-23 22:26:32 +03:00
cli_errstr ( targetcli ) , mask ) ;
2001-10-09 23:12:18 +04:00
}
return 0 ;
1996-05-04 11:50:46 +04:00
}
2002-01-16 23:13:28 +03:00
/****************************************************************************
UNIX hardlink .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_link ( void )
{
2004-04-07 03:01:09 +04:00
pstring oldname , newname ;
2004-03-12 17:37:37 +03:00
pstring buf , buf2 ;
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
pstring targetname ;
2002-01-16 23:13:28 +03:00
2004-04-07 03:01:09 +04:00
pstrcpy ( oldname , cur_dir ) ;
pstrcpy ( newname , cur_dir ) ;
2002-01-16 23:13:28 +03:00
2004-02-12 00:42:20 +03:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) | |
! next_token_nr ( NULL , buf2 , NULL , sizeof ( buf2 ) ) ) {
2004-04-07 03:01:09 +04:00
d_printf ( " link <oldname> <newname> \n " ) ;
2002-01-16 23:13:28 +03:00
return 1 ;
}
2004-04-07 03:01:09 +04:00
pstrcat ( oldname , buf ) ;
pstrcat ( newname , buf2 ) ;
2002-01-16 23:13:28 +03:00
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , oldname , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " link %s: %s \n " , oldname , cli_errstr ( cli ) ) ;
return 1 ;
}
if ( ! SERVER_HAS_UNIX_CIFS ( targetcli ) ) {
d_printf ( " Server doesn't support UNIX CIFS calls. \n " ) ;
return 1 ;
}
if ( ! cli_unix_hardlink ( targetcli , targetname , newname ) ) {
d_printf ( " %s linking files (%s -> %s) \n " , cli_errstr ( targetcli ) , newname , oldname ) ;
2002-01-16 23:13:28 +03:00
return 1 ;
}
return 0 ;
}
/****************************************************************************
UNIX symlink .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_symlink ( void )
{
2004-04-07 03:01:09 +04:00
pstring oldname , newname ;
2004-03-12 17:37:37 +03:00
pstring buf , buf2 ;
2002-01-16 23:13:28 +03:00
if ( ! SERVER_HAS_UNIX_CIFS ( cli ) ) {
d_printf ( " Server doesn't support UNIX CIFS calls. \n " ) ;
return 1 ;
}
2004-04-07 03:01:09 +04:00
pstrcpy ( newname , cur_dir ) ;
2002-01-16 23:13:28 +03:00
2004-02-12 00:42:20 +03:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) | |
! next_token_nr ( NULL , buf2 , NULL , sizeof ( buf2 ) ) ) {
2004-04-07 03:01:09 +04:00
d_printf ( " symlink <oldname> <newname> \n " ) ;
2002-01-16 23:13:28 +03:00
return 1 ;
}
2004-05-14 03:34:00 +04:00
pstrcpy ( oldname , buf ) ;
2004-04-07 03:01:09 +04:00
pstrcat ( newname , buf2 ) ;
2002-01-16 23:13:28 +03:00
2004-04-07 03:01:09 +04:00
if ( ! cli_unix_symlink ( cli , oldname , newname ) ) {
2002-01-16 23:13:28 +03:00
d_printf ( " %s symlinking files (%s -> %s) \n " ,
2004-04-07 03:01:09 +04:00
cli_errstr ( cli ) , newname , oldname ) ;
2002-01-16 23:13:28 +03:00
return 1 ;
}
return 0 ;
}
/****************************************************************************
UNIX chmod .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_chmod ( void )
{
pstring src ;
mode_t mode ;
2004-03-12 17:37:37 +03:00
pstring buf , buf2 ;
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
pstring targetname ;
2002-01-16 23:13:28 +03:00
pstrcpy ( src , cur_dir ) ;
2004-02-12 00:42:20 +03:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) | |
! next_token_nr ( NULL , buf2 , NULL , sizeof ( buf2 ) ) ) {
2002-01-16 23:13:28 +03:00
d_printf ( " chmod mode file \n " ) ;
return 1 ;
}
mode = ( mode_t ) strtol ( buf , NULL , 8 ) ;
pstrcat ( src , buf2 ) ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , src , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " chmod %s: %s \n " , src , cli_errstr ( cli ) ) ;
return 1 ;
}
if ( ! SERVER_HAS_UNIX_CIFS ( targetcli ) ) {
d_printf ( " Server doesn't support UNIX CIFS calls. \n " ) ;
return 1 ;
}
if ( ! cli_unix_chmod ( targetcli , targetname , mode ) ) {
2002-01-16 23:13:28 +03:00
d_printf ( " %s chmod file %s 0%o \n " ,
2005-02-23 22:26:32 +03:00
cli_errstr ( targetcli ) , src , ( unsigned int ) mode ) ;
2002-01-16 23:13:28 +03:00
return 1 ;
}
return 0 ;
}
2004-09-26 10:27:54 +04:00
static const char * filetype_to_str ( mode_t mode )
{
if ( S_ISREG ( mode ) ) {
return " regular file " ;
} else if ( S_ISDIR ( mode ) ) {
return " directory " ;
} else
# ifdef S_ISCHR
if ( S_ISCHR ( mode ) ) {
return " character device " ;
} else
# endif
# ifdef S_ISBLK
if ( S_ISBLK ( mode ) ) {
return " block device " ;
} else
# endif
# ifdef S_ISFIFO
if ( S_ISFIFO ( mode ) ) {
return " fifo " ;
} else
# endif
# ifdef S_ISLNK
if ( S_ISLNK ( mode ) ) {
return " symbolic link " ;
} else
# endif
# ifdef S_ISSOCK
if ( S_ISSOCK ( mode ) ) {
return " socket " ;
} else
# endif
return " " ;
}
static char rwx_to_str ( mode_t m , mode_t bt , char ret )
{
if ( m & bt ) {
return ret ;
} else {
return ' - ' ;
}
}
static char * unix_mode_to_str ( char * s , mode_t m )
{
char * p = s ;
const char * str = filetype_to_str ( m ) ;
switch ( str [ 0 ] ) {
case ' d ' :
* p + + = ' d ' ;
break ;
case ' c ' :
* p + + = ' c ' ;
break ;
case ' b ' :
* p + + = ' b ' ;
break ;
case ' f ' :
* p + + = ' p ' ;
break ;
case ' s ' :
* p + + = str [ 1 ] = = ' y ' ? ' l ' : ' s ' ;
break ;
case ' r ' :
default :
* p + + = ' - ' ;
break ;
}
* p + + = rwx_to_str ( m , S_IRUSR , ' r ' ) ;
* p + + = rwx_to_str ( m , S_IWUSR , ' w ' ) ;
* p + + = rwx_to_str ( m , S_IXUSR , ' x ' ) ;
* p + + = rwx_to_str ( m , S_IRGRP , ' r ' ) ;
* p + + = rwx_to_str ( m , S_IWGRP , ' w ' ) ;
* p + + = rwx_to_str ( m , S_IXGRP , ' x ' ) ;
* p + + = rwx_to_str ( m , S_IROTH , ' r ' ) ;
* p + + = rwx_to_str ( m , S_IWOTH , ' w ' ) ;
* p + + = rwx_to_str ( m , S_IXOTH , ' x ' ) ;
* p + + = ' \0 ' ;
return s ;
}
2004-11-13 03:35:31 +03:00
/****************************************************************************
Utility function for UNIX getfacl .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static char * perms_to_string ( fstring permstr , unsigned char perms )
{
fstrcpy ( permstr , " --- " ) ;
if ( perms & SMB_POSIX_ACL_READ ) {
permstr [ 0 ] = ' r ' ;
}
if ( perms & SMB_POSIX_ACL_WRITE ) {
permstr [ 1 ] = ' w ' ;
}
if ( perms & SMB_POSIX_ACL_EXECUTE ) {
permstr [ 2 ] = ' x ' ;
}
return permstr ;
}
2004-11-13 02:42:12 +03:00
/****************************************************************************
UNIX getfacl .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_getfacl ( void )
{
pstring src , name ;
uint16 major , minor ;
uint32 caplow , caphigh ;
char * retbuf = NULL ;
2004-11-13 03:35:31 +03:00
size_t rb_size = 0 ;
SMB_STRUCT_STAT sbuf ;
uint16 num_file_acls = 0 ;
uint16 num_dir_acls = 0 ;
uint16 i ;
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
pstring targetname ;
2004-11-13 02:42:12 +03:00
2005-02-23 22:26:32 +03:00
pstrcpy ( src , cur_dir ) ;
if ( ! next_token_nr ( NULL , name , NULL , sizeof ( name ) ) ) {
d_printf ( " stat file \n " ) ;
2004-11-13 02:42:12 +03:00
return 1 ;
}
2005-02-23 22:26:32 +03:00
pstrcat ( src , name ) ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , src , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " stat %s: %s \n " , src , cli_errstr ( cli ) ) ;
return 1 ;
}
if ( ! SERVER_HAS_UNIX_CIFS ( targetcli ) ) {
d_printf ( " Server doesn't support UNIX CIFS calls. \n " ) ;
return 1 ;
}
if ( ! cli_unix_extensions_version ( targetcli , & major , & minor , & caplow , & caphigh ) ) {
2004-11-13 02:42:12 +03:00
d_printf ( " Can't get UNIX CIFS version from server. \n " ) ;
return 1 ;
}
if ( ! ( caplow & CIFS_UNIX_POSIX_ACLS_CAP ) ) {
d_printf ( " This server supports UNIX extensions but doesn't support POSIX ACLs. \n " ) ;
return 1 ;
}
2005-02-23 22:26:32 +03:00
if ( ! cli_unix_stat ( targetcli , targetname , & sbuf ) ) {
2004-11-13 03:35:31 +03:00
d_printf ( " %s getfacl doing a stat on file %s \n " ,
2005-02-23 22:26:32 +03:00
cli_errstr ( targetcli ) , src ) ;
2004-11-13 03:35:31 +03:00
return 1 ;
}
2005-02-23 22:26:32 +03:00
if ( ! cli_unix_getfacl ( targetcli , targetname , & rb_size , & retbuf ) ) {
2004-11-13 02:42:12 +03:00
d_printf ( " %s getfacl file %s \n " ,
2005-02-23 22:26:32 +03:00
cli_errstr ( targetcli ) , src ) ;
2004-11-13 02:42:12 +03:00
return 1 ;
}
/* ToDo : Print out the ACL values. */
2004-11-13 03:35:31 +03:00
if ( SVAL ( retbuf , 0 ) ! = SMB_POSIX_ACL_VERSION | | rb_size < 6 ) {
d_printf ( " getfacl file %s, unknown POSIX acl version %u. \n " ,
src , ( unsigned int ) CVAL ( retbuf , 0 ) ) ;
SAFE_FREE ( retbuf ) ;
return 1 ;
}
num_file_acls = SVAL ( retbuf , 2 ) ;
num_dir_acls = SVAL ( retbuf , 4 ) ;
if ( rb_size ! = SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE * ( num_file_acls + num_dir_acls ) ) {
d_printf ( " getfacl file %s, incorrect POSIX acl buffer size (should be %u, was %u). \n " ,
src ,
( unsigned int ) ( SMB_POSIX_ACL_HEADER_SIZE + SMB_POSIX_ACL_ENTRY_SIZE * ( num_file_acls + num_dir_acls ) ) ,
( unsigned int ) rb_size ) ;
SAFE_FREE ( retbuf ) ;
return 1 ;
}
d_printf ( " # file: %s \n " , src ) ;
d_printf ( " # owner: %u \n # group: %u \n " , ( unsigned int ) sbuf . st_uid , ( unsigned int ) sbuf . st_gid ) ;
if ( num_file_acls = = 0 & & num_dir_acls = = 0 ) {
d_printf ( " No acls found. \n " ) ;
}
for ( i = 0 ; i < num_file_acls ; i + + ) {
uint32 uorg ;
fstring permstring ;
unsigned char tagtype = CVAL ( retbuf , SMB_POSIX_ACL_HEADER_SIZE + ( i * SMB_POSIX_ACL_ENTRY_SIZE ) ) ;
unsigned char perms = CVAL ( retbuf , SMB_POSIX_ACL_HEADER_SIZE + ( i * SMB_POSIX_ACL_ENTRY_SIZE ) + 1 ) ;
switch ( tagtype ) {
case SMB_POSIX_ACL_USER_OBJ :
d_printf ( " user:: " ) ;
break ;
case SMB_POSIX_ACL_USER :
uorg = IVAL ( retbuf , SMB_POSIX_ACL_HEADER_SIZE + ( i * SMB_POSIX_ACL_ENTRY_SIZE ) + 2 ) ;
d_printf ( " user:%u: " , uorg ) ;
break ;
case SMB_POSIX_ACL_GROUP_OBJ :
d_printf ( " group:: " ) ;
break ;
case SMB_POSIX_ACL_GROUP :
uorg = IVAL ( retbuf , SMB_POSIX_ACL_HEADER_SIZE + ( i * SMB_POSIX_ACL_ENTRY_SIZE ) + 2 ) ;
d_printf ( " group:%u " , uorg ) ;
break ;
case SMB_POSIX_ACL_MASK :
d_printf ( " mask:: " ) ;
break ;
case SMB_POSIX_ACL_OTHER :
d_printf ( " other:: " ) ;
break ;
default :
d_printf ( " getfacl file %s, incorrect POSIX acl tagtype (%u). \n " ,
src , ( unsigned int ) tagtype ) ;
SAFE_FREE ( retbuf ) ;
return 1 ;
}
d_printf ( " %s \n " , perms_to_string ( permstring , perms ) ) ;
}
for ( i = 0 ; i < num_dir_acls ; i + + ) {
uint32 uorg ;
fstring permstring ;
unsigned char tagtype = CVAL ( retbuf , SMB_POSIX_ACL_HEADER_SIZE + ( ( i + num_file_acls ) * SMB_POSIX_ACL_ENTRY_SIZE ) ) ;
unsigned char perms = CVAL ( retbuf , SMB_POSIX_ACL_HEADER_SIZE + ( ( i + num_file_acls ) * SMB_POSIX_ACL_ENTRY_SIZE ) + 1 ) ;
switch ( tagtype ) {
case SMB_POSIX_ACL_USER_OBJ :
d_printf ( " default:user:: " ) ;
break ;
case SMB_POSIX_ACL_USER :
uorg = IVAL ( retbuf , SMB_POSIX_ACL_HEADER_SIZE + ( ( i + num_file_acls ) * SMB_POSIX_ACL_ENTRY_SIZE ) + 2 ) ;
d_printf ( " default:user:%u: " , uorg ) ;
break ;
case SMB_POSIX_ACL_GROUP_OBJ :
d_printf ( " default:group:: " ) ;
break ;
case SMB_POSIX_ACL_GROUP :
uorg = IVAL ( retbuf , SMB_POSIX_ACL_HEADER_SIZE + ( ( i + num_file_acls ) * SMB_POSIX_ACL_ENTRY_SIZE ) + 2 ) ;
d_printf ( " default:group:%u " , uorg ) ;
break ;
case SMB_POSIX_ACL_MASK :
d_printf ( " default:mask:: " ) ;
break ;
case SMB_POSIX_ACL_OTHER :
d_printf ( " default:other:: " ) ;
break ;
default :
d_printf ( " getfacl file %s, incorrect POSIX acl tagtype (%u). \n " ,
src , ( unsigned int ) tagtype ) ;
SAFE_FREE ( retbuf ) ;
return 1 ;
}
d_printf ( " %s \n " , perms_to_string ( permstring , perms ) ) ;
}
SAFE_FREE ( retbuf ) ;
2004-11-13 02:42:12 +03:00
return 0 ;
}
2004-09-26 10:27:54 +04:00
/****************************************************************************
UNIX stat .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_stat ( void )
{
pstring src , name ;
fstring mode_str ;
SMB_STRUCT_STAT sbuf ;
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
2006-06-15 01:36:49 +04:00
struct tm * lt ;
2005-02-23 22:26:32 +03:00
pstring targetname ;
2004-09-26 10:27:54 +04:00
if ( ! SERVER_HAS_UNIX_CIFS ( cli ) ) {
d_printf ( " Server doesn't support UNIX CIFS calls. \n " ) ;
return 1 ;
}
pstrcpy ( src , cur_dir ) ;
if ( ! next_token_nr ( NULL , name , NULL , sizeof ( name ) ) ) {
d_printf ( " stat file \n " ) ;
return 1 ;
}
pstrcat ( src , name ) ;
2005-02-23 22:26:32 +03:00
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , src , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " stat %s: %s \n " , src , cli_errstr ( cli ) ) ;
return 1 ;
}
if ( ! cli_unix_stat ( targetcli , targetname , & sbuf ) ) {
2004-09-26 10:27:54 +04:00
d_printf ( " %s stat file %s \n " ,
2005-02-23 22:26:32 +03:00
cli_errstr ( targetcli ) , src ) ;
2004-09-26 10:27:54 +04:00
return 1 ;
}
/* Print out the stat values. */
d_printf ( " File: %s \n " , src ) ;
d_printf ( " Size: %-12.0f \t Blocks: %u \t %s \n " ,
( double ) sbuf . st_size ,
( unsigned int ) sbuf . st_blocks ,
filetype_to_str ( sbuf . st_mode ) ) ;
# if defined(S_ISCHR) && defined(S_ISBLK)
if ( S_ISCHR ( sbuf . st_mode ) | | S_ISBLK ( sbuf . st_mode ) ) {
d_printf ( " Inode: %.0f \t Links: %u \t Device type: %u,%u \n " ,
( double ) sbuf . st_ino ,
( unsigned int ) sbuf . st_nlink ,
unix_dev_major ( sbuf . st_rdev ) ,
unix_dev_minor ( sbuf . st_rdev ) ) ;
} else
# endif
d_printf ( " Inode: %.0f \t Links: %u \n " ,
( double ) sbuf . st_ino ,
( unsigned int ) sbuf . st_nlink ) ;
d_printf ( " Access: (0%03o/%s) \t Uid: %u \t Gid: %u \n " ,
( ( int ) sbuf . st_mode & 0777 ) ,
unix_mode_to_str ( mode_str , sbuf . st_mode ) ,
( unsigned int ) sbuf . st_uid ,
( unsigned int ) sbuf . st_gid ) ;
2006-06-15 01:36:49 +04:00
lt = localtime ( & sbuf . st_atime ) ;
if ( lt ) {
strftime ( mode_str , sizeof ( mode_str ) , " %F %T %z " , lt ) ;
} else {
fstrcpy ( mode_str , " unknown " ) ;
}
2004-09-26 10:27:54 +04:00
d_printf ( " Access: %s \n " , mode_str ) ;
2006-06-15 01:36:49 +04:00
lt = localtime ( & sbuf . st_mtime ) ;
if ( lt ) {
strftime ( mode_str , sizeof ( mode_str ) , " %F %T %z " , lt ) ;
} else {
fstrcpy ( mode_str , " unknown " ) ;
}
2004-09-26 10:27:54 +04:00
d_printf ( " Modify: %s \n " , mode_str ) ;
2006-06-15 01:36:49 +04:00
lt = localtime ( & sbuf . st_ctime ) ;
if ( lt ) {
strftime ( mode_str , sizeof ( mode_str ) , " %F %T %z " , lt ) ;
} else {
fstrcpy ( mode_str , " unknown " ) ;
}
2004-09-26 10:27:54 +04:00
d_printf ( " Change: %s \n " , mode_str ) ;
return 0 ;
}
2002-01-16 23:13:28 +03:00
/****************************************************************************
UNIX chown .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_chown ( void )
{
pstring src ;
uid_t uid ;
gid_t gid ;
2004-03-12 17:37:37 +03:00
pstring buf , buf2 , buf3 ;
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
pstring targetname ;
2002-01-16 23:13:28 +03:00
pstrcpy ( src , cur_dir ) ;
2004-02-12 00:42:20 +03:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) | |
! next_token_nr ( NULL , buf2 , NULL , sizeof ( buf2 ) ) | |
! next_token_nr ( NULL , buf3 , NULL , sizeof ( buf3 ) ) ) {
2002-01-16 23:13:28 +03:00
d_printf ( " chown uid gid file \n " ) ;
return 1 ;
}
uid = ( uid_t ) atoi ( buf ) ;
gid = ( gid_t ) atoi ( buf2 ) ;
pstrcat ( src , buf3 ) ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , src , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " chown %s: %s \n " , src , cli_errstr ( cli ) ) ;
return 1 ;
}
if ( ! SERVER_HAS_UNIX_CIFS ( targetcli ) ) {
d_printf ( " Server doesn't support UNIX CIFS calls. \n " ) ;
return 1 ;
}
if ( ! cli_unix_chown ( targetcli , targetname , uid , gid ) ) {
2002-01-16 23:13:28 +03:00
d_printf ( " %s chown file %s uid=%d, gid=%d \n " ,
2005-02-23 22:26:32 +03:00
cli_errstr ( targetcli ) , src , ( int ) uid , ( int ) gid ) ;
2002-01-16 23:13:28 +03:00
return 1 ;
}
return 0 ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Rename some file .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_rename ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
pstring src , dest ;
2004-03-12 17:37:37 +03:00
pstring buf , buf2 ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
pstrcpy ( src , cur_dir ) ;
pstrcpy ( dest , cur_dir ) ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) | |
! next_token_nr ( NULL , buf2 , NULL , sizeof ( buf2 ) ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " rename <src> <dest> \n " ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
1998-11-09 06:45:49 +03:00
}
pstrcat ( src , buf ) ;
pstrcat ( dest , buf2 ) ;
if ( ! cli_rename ( cli , src , dest ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s renaming files \n " , cli_errstr ( cli ) ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
}
return 0 ;
1996-05-04 11:50:46 +04:00
}
2005-03-30 04:47:57 +04:00
/****************************************************************************
Print the volume name .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_volume ( void )
{
fstring volname ;
uint32 serial_num ;
time_t create_date ;
if ( ! cli_get_fs_volume_info ( cli , volname , & serial_num , & create_date ) ) {
d_printf ( " Errr %s getting volume info \n " , cli_errstr ( cli ) ) ;
return 1 ;
}
d_printf ( " Volume: |%s| serial number 0x%x \n " , volname , ( unsigned int ) serial_num ) ;
return 0 ;
}
2004-03-03 23:55:59 +03:00
/****************************************************************************
2004-03-04 02:14:23 +03:00
Hard link files using the NT call .
2004-03-03 23:55:59 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-03-04 02:14:23 +03:00
static int cmd_hardlink ( void )
2004-03-03 23:55:59 +03:00
{
pstring src , dest ;
2004-03-12 17:37:37 +03:00
pstring buf , buf2 ;
2005-02-23 22:26:32 +03:00
struct cli_state * targetcli ;
pstring targetname ;
2004-03-03 23:55:59 +03:00
pstrcpy ( src , cur_dir ) ;
pstrcpy ( dest , cur_dir ) ;
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) | |
! next_token_nr ( NULL , buf2 , NULL , sizeof ( buf2 ) ) ) {
2004-03-04 02:14:23 +03:00
d_printf ( " hardlink <src> <dest> \n " ) ;
2004-03-03 23:55:59 +03:00
return 1 ;
}
pstrcat ( src , buf ) ;
pstrcat ( dest , buf2 ) ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , src , & targetcli , targetname ) ) {
2005-02-23 22:26:32 +03:00
d_printf ( " hardlink %s: %s \n " , src , cli_errstr ( cli ) ) ;
return 1 ;
}
if ( ! SERVER_HAS_UNIX_CIFS ( targetcli ) ) {
d_printf ( " Server doesn't support UNIX CIFS calls. \n " ) ;
return 1 ;
}
if ( ! cli_nt_hardlink ( targetcli , targetname , dest ) ) {
d_printf ( " %s doing an NT hard link of files \n " , cli_errstr ( targetcli ) ) ;
2004-03-03 23:55:59 +03:00
return 1 ;
}
return 0 ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Toggle the prompt flag .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_prompt ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
prompt = ! prompt ;
DEBUG ( 2 , ( " prompting is now %s \n " , prompt ? " on " : " off " ) ) ;
2001-10-09 23:12:18 +04:00
2001-10-10 11:51:20 +04:00
return 1 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Set the newer than time .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_newer ( void )
1996-05-04 11:50:46 +04:00
{
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
BOOL ok ;
SMB_STRUCT_STAT sbuf ;
2001-06-21 13:10:42 +04:00
ok = next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ;
2000-03-11 01:25:02 +03:00
if ( ok & & ( sys_stat ( buf , & sbuf ) = = 0 ) ) {
1998-11-09 06:45:49 +03:00
newer_than = sbuf . st_mtime ;
DEBUG ( 1 , ( " Getting files newer than %s " ,
2006-06-15 01:36:49 +04:00
time_to_asc ( & newer_than ) ) ) ;
1998-11-09 06:45:49 +03:00
} else {
newer_than = 0 ;
}
2001-10-09 23:12:18 +04:00
if ( ok & & newer_than = = 0 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Error setting newer-than time \n " ) ;
2001-10-09 23:12:18 +04:00
return 1 ;
}
return 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Set the archive level .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_archive ( void )
1996-05-04 11:50:46 +04:00
{
2004-03-12 17:37:37 +03:00
pstring buf ;
1996-05-04 11:50:46 +04:00
2001-06-21 13:10:42 +04:00
if ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
1998-11-09 06:45:49 +03:00
archive_level = atoi ( buf ) ;
} else
2001-09-07 18:14:57 +04:00
d_printf ( " Archive level is %d \n " , archive_level ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Toggle the lowercaseflag .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_lowercase ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
lowercase = ! lowercase ;
DEBUG ( 2 , ( " filename lowercasing is now %s \n " , lowercase ? " on " : " off " ) ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
2004-06-15 22:36:45 +04:00
/****************************************************************************
Toggle the case sensitive flag .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_setcase ( void )
{
BOOL orig_case_sensitive = cli_set_case_sensitive ( cli , False ) ;
cli_set_case_sensitive ( cli , ! orig_case_sensitive ) ;
DEBUG ( 2 , ( " filename case sensitivity is now %s \n " , ! orig_case_sensitive ?
" on " : " off " ) ) ;
return 0 ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Toggle the recurse flag .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_recurse ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
recurse = ! recurse ;
DEBUG ( 2 , ( " directory recursion is now %s \n " , recurse ? " on " : " off " ) ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Toggle the translate flag .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_translate ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
translation = ! translation ;
DEBUG ( 2 , ( " CR/LF<->LF and print text translation now %s \n " ,
translation ? " on " : " off " ) ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Do the lcd command .
2002-09-25 19:19:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_lcd ( void )
1996-05-04 11:50:46 +04:00
{
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
pstring d ;
2001-06-21 13:10:42 +04:00
if ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) )
1998-11-26 00:17:20 +03:00
chdir ( buf ) ;
DEBUG ( 2 , ( " the local directory is now %s \n " , sys_getwd ( d ) ) ) ;
2001-10-09 23:12:18 +04:00
return 0 ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Get a file restarting at end of local file .
2002-09-25 19:19:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2002-09-25 19:19:00 +04:00
static int cmd_reget ( void )
{
pstring local_name ;
pstring remote_name ;
char * p ;
pstrcpy ( remote_name , cur_dir ) ;
pstrcat ( remote_name , " \\ " ) ;
p = remote_name + strlen ( remote_name ) ;
if ( ! next_token_nr ( NULL , p , NULL , sizeof ( remote_name ) - strlen ( remote_name ) ) ) {
d_printf ( " reget <filename> \n " ) ;
return 1 ;
}
pstrcpy ( local_name , p ) ;
dos_clean_name ( remote_name ) ;
next_token_nr ( NULL , local_name , NULL , sizeof ( local_name ) ) ;
return do_get ( remote_name , local_name , True ) ;
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Put a file restarting at end of local file .
2002-09-25 19:19:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2002-09-25 19:19:00 +04:00
static int cmd_reput ( void )
{
pstring local_name ;
pstring remote_name ;
2004-03-12 17:37:37 +03:00
pstring buf ;
2002-09-25 19:19:00 +04:00
char * p = buf ;
SMB_STRUCT_STAT st ;
pstrcpy ( remote_name , cur_dir ) ;
pstrcat ( remote_name , " \\ " ) ;
if ( ! next_token_nr ( NULL , p , NULL , sizeof ( buf ) ) ) {
d_printf ( " reput <filename> \n " ) ;
return 1 ;
}
pstrcpy ( local_name , p ) ;
if ( ! file_exist ( local_name , & st ) ) {
d_printf ( " %s does not exist \n " , local_name ) ;
return 1 ;
}
if ( next_token_nr ( NULL , p , NULL , sizeof ( buf ) ) )
pstrcat ( remote_name , p ) ;
else
pstrcat ( remote_name , local_name ) ;
dos_clean_name ( remote_name ) ;
return do_put ( remote_name , local_name , True ) ;
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
List a share name .
2002-09-25 19:19:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-01-04 14:35:55 +03:00
static void browse_fn ( const char * name , uint32 m ,
const char * comment , void * state )
1998-11-09 06:45:49 +03:00
{
1998-06-04 22:49:13 +04:00
fstring typestr ;
1999-12-13 16:27:58 +03:00
1998-06-04 22:49:13 +04:00
* typestr = 0 ;
1996-05-04 11:50:46 +04:00
2006-04-16 15:47:26 +04:00
switch ( m & 7 )
1998-06-04 22:49:13 +04:00
{
case STYPE_DISKTREE :
fstrcpy ( typestr , " Disk " ) ; break ;
case STYPE_PRINTQ :
1998-11-09 06:45:49 +03:00
fstrcpy ( typestr , " Printer " ) ; break ;
1998-06-04 22:49:13 +04:00
case STYPE_DEVICE :
fstrcpy ( typestr , " Device " ) ; break ;
case STYPE_IPC :
1998-11-09 06:45:49 +03:00
fstrcpy ( typestr , " IPC " ) ; break ;
1998-06-04 22:49:13 +04:00
}
2002-01-10 06:53:06 +03:00
/* FIXME: If the remote machine returns non-ascii characters
in any of these fields , they can corrupt the output . We
should remove them . */
2004-01-16 18:01:09 +03:00
if ( ! grepable ) {
2004-03-01 20:40:40 +03:00
d_printf ( " \t %-15s %-10.10s%s \n " ,
2004-01-16 18:01:09 +03:00
name , typestr , comment ) ;
} else {
d_printf ( " %s|%s|%s \n " , typestr , name , comment ) ;
}
1996-05-04 11:50:46 +04:00
}
2006-04-16 15:47:26 +04:00
static BOOL browse_host_rpc ( BOOL sort )
{
NTSTATUS status ;
struct rpc_pipe_client * pipe_hnd ;
TALLOC_CTX * mem_ctx ;
ENUM_HND enum_hnd ;
WERROR werr ;
SRV_SHARE_INFO_CTR ctr ;
int i ;
mem_ctx = talloc_new ( NULL ) ;
if ( mem_ctx = = NULL ) {
DEBUG ( 0 , ( " talloc_new failed \n " ) ) ;
return False ;
}
init_enum_hnd ( & enum_hnd , 0 ) ;
pipe_hnd = cli_rpc_pipe_open_noauth ( cli , PI_SRVSVC , & status ) ;
if ( pipe_hnd = = NULL ) {
DEBUG ( 10 , ( " Could not connect to srvsvc pipe: %s \n " ,
nt_errstr ( status ) ) ) ;
TALLOC_FREE ( mem_ctx ) ;
return False ;
}
werr = rpccli_srvsvc_net_share_enum ( pipe_hnd , mem_ctx , 1 , & ctr ,
0xffffffff , & enum_hnd ) ;
if ( ! W_ERROR_IS_OK ( werr ) ) {
TALLOC_FREE ( mem_ctx ) ;
cli_rpc_pipe_close ( pipe_hnd ) ;
return False ;
}
for ( i = 0 ; i < ctr . num_entries ; i + + ) {
SRV_SHARE_INFO_1 * info = & ctr . share . info1 [ i ] ;
char * name , * comment ;
name = rpcstr_pull_unistr2_talloc (
mem_ctx , & info - > info_1_str . uni_netname ) ;
comment = rpcstr_pull_unistr2_talloc (
mem_ctx , & info - > info_1_str . uni_remark ) ;
browse_fn ( name , info - > info_1 . type , comment , NULL ) ;
}
TALLOC_FREE ( mem_ctx ) ;
cli_rpc_pipe_close ( pipe_hnd ) ;
return True ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Try and browse available connections on a host .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1998-11-09 06:45:49 +03:00
static BOOL browse_host ( BOOL sort )
1996-05-04 11:50:46 +04:00
{
2000-02-26 01:25:25 +03:00
int ret ;
2004-01-16 18:01:09 +03:00
if ( ! grepable ) {
2004-03-01 20:40:40 +03:00
d_printf ( " \n \t Sharename Type Comment \n " ) ;
d_printf ( " \t --------- ---- ------- \n " ) ;
2004-01-16 18:01:09 +03:00
}
1996-05-04 11:50:46 +04:00
2006-04-16 15:47:26 +04:00
if ( browse_host_rpc ( sort ) ) {
return True ;
}
2001-01-04 14:35:55 +03:00
if ( ( ret = cli_RNetShareEnum ( cli , browse_fn , NULL ) ) = = - 1 )
2001-09-07 18:14:57 +04:00
d_printf ( " Error returning browse list: %s \n " , cli_errstr ( cli ) ) ;
2000-02-26 01:25:25 +03:00
return ( ret ! = - 1 ) ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
List a server name .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-01-04 14:35:55 +03:00
static void server_fn ( const char * name , uint32 m ,
const char * comment , void * state )
1998-11-09 06:45:49 +03:00
{
2004-01-16 18:01:09 +03:00
if ( ! grepable ) {
2004-03-01 20:40:40 +03:00
d_printf ( " \t %-16s %s \n " , name , comment ) ;
2004-01-16 18:01:09 +03:00
} else {
d_printf ( " %s|%s|%s \n " , ( char * ) state , name , comment ) ;
}
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Try and browse available connections on a host .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2004-01-16 18:01:09 +03:00
static BOOL list_servers ( const char * wk_grp )
1996-05-04 11:50:46 +04:00
{
2005-02-22 06:31:22 +03:00
fstring state ;
2003-08-07 00:01:31 +04:00
if ( ! cli - > server_domain )
return False ;
1998-11-09 06:45:49 +03:00
2004-01-16 18:01:09 +03:00
if ( ! grepable ) {
d_printf ( " \n \t Server Comment \n " ) ;
d_printf ( " \t --------- ------- \n " ) ;
} ;
2005-02-22 06:31:22 +03:00
fstrcpy ( state , " Server " ) ;
2004-01-16 18:01:09 +03:00
cli_NetServerEnum ( cli , cli - > server_domain , SV_TYPE_ALL , server_fn ,
2005-02-22 06:31:22 +03:00
state ) ;
1998-11-09 06:45:49 +03:00
2004-01-16 18:01:09 +03:00
if ( ! grepable ) {
d_printf ( " \n \t Workgroup Master \n " ) ;
d_printf ( " \t --------- ------- \n " ) ;
} ;
1998-11-09 06:45:49 +03:00
2005-02-22 06:31:22 +03:00
fstrcpy ( state , " Workgroup " ) ;
2004-01-16 18:01:09 +03:00
cli_NetServerEnum ( cli , cli - > server_domain , SV_TYPE_DOMAIN_ENUM ,
2005-02-22 06:31:22 +03:00
server_fn , state ) ;
1998-11-09 06:45:49 +03:00
return True ;
1996-05-04 11:50:46 +04:00
}
2004-02-12 20:26:34 +03:00
/****************************************************************************
Print or set current VUID
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_vuid ( void )
{
fstring buf ;
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
d_printf ( " Current VUID is %d \n " , cli - > vuid ) ;
return 0 ;
}
cli - > vuid = atoi ( buf ) ;
return 0 ;
}
/****************************************************************************
Setup a new VUID , by issuing a session setup
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_logon ( void )
{
pstring l_username , l_password ;
2004-03-12 17:37:37 +03:00
pstring buf , buf2 ;
2004-02-12 20:26:34 +03:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
d_printf ( " logon <username> [<password>] \n " ) ;
return 0 ;
}
pstrcpy ( l_username , buf ) ;
2005-02-25 00:54:52 +03:00
if ( ! next_token_nr ( NULL , buf2 , NULL , sizeof ( buf ) ) )
{
2004-02-12 20:26:34 +03:00
char * pass = getpass ( " Password: " ) ;
2005-02-25 00:54:52 +03:00
if ( pass )
2004-02-12 20:26:34 +03:00
pstrcpy ( l_password , pass ) ;
2005-02-25 00:54:52 +03:00
}
else
2004-02-12 20:26:34 +03:00
pstrcpy ( l_password , buf2 ) ;
if ( ! cli_session_setup ( cli , l_username ,
l_password , strlen ( l_password ) ,
l_password , strlen ( l_password ) ,
lp_workgroup ( ) ) ) {
d_printf ( " session setup failed: %s \n " , cli_errstr ( cli ) ) ;
return - 1 ;
}
d_printf ( " Current VUID is %d \n " , cli - > vuid ) ;
return 0 ;
}
2005-02-22 06:31:22 +03:00
/****************************************************************************
list active connections
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int cmd_list_connect ( void )
{
2005-02-25 00:54:52 +03:00
cli_cm_display ( ) ;
2005-02-22 06:31:22 +03:00
return 0 ;
}
/****************************************************************************
2005-02-23 20:29:28 +03:00
display the current active client connection
2005-02-22 06:31:22 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2005-02-23 20:29:28 +03:00
static int cmd_show_connect ( void )
2005-02-22 06:31:22 +03:00
{
2005-02-23 20:29:28 +03:00
struct cli_state * targetcli ;
pstring targetpath ;
2005-02-26 18:03:16 +03:00
if ( ! cli_resolve_path ( " " , cli , cur_dir , & targetcli , targetpath ) ) {
2005-02-23 20:29:28 +03:00
d_printf ( " showconnect %s: %s \n " , cur_dir , cli_errstr ( cli ) ) ;
2005-02-22 06:31:22 +03:00
return 1 ;
}
2005-02-23 20:29:28 +03:00
d_printf ( " //%s/%s \n " , targetcli - > desthost , targetcli - > share ) ;
2005-02-22 06:31:22 +03:00
return 0 ;
}
1998-09-18 16:47:46 +04:00
/* Some constants for completing filename arguments */
# define COMPL_NONE 0 /* No completions */
# define COMPL_REMOTE 1 /* Complete remote filename */
# define COMPL_LOCAL 2 /* Complete local filename */
2001-09-30 10:49:44 +04:00
/* This defines the commands supported by this client.
* NOTE : The " ! " must be the last one in the list because it ' s fn pointer
* field is NULL , and NULL in that field is used in process_tok ( )
* ( below ) to indicate the end of the list . crh
*/
2002-07-15 14:35:28 +04:00
static struct
1996-05-04 11:50:46 +04:00
{
2003-01-03 11:28:12 +03:00
const char * name ;
2001-10-09 23:12:18 +04:00
int ( * fn ) ( void ) ;
2003-01-03 11:28:12 +03:00
const char * description ;
1998-09-18 16:47:46 +04:00
char compl_args [ 2 ] ; /* Completion argument info */
2003-08-07 00:01:31 +04:00
} commands [ ] = {
2002-01-16 23:25:23 +03:00
{ " ? " , cmd_help , " [command] give help on a command " , { COMPL_NONE , COMPL_NONE } } ,
{ " altname " , cmd_altname , " <file> show alt name " , { COMPL_NONE , COMPL_NONE } } ,
2001-09-30 10:49:44 +04:00
{ " archive " , cmd_archive , " <level> \n 0=ignore archive bit \n 1=only get archive files \n 2=only get archive files and reset archive bit \n 3=get all files and reset archive bit " , { COMPL_NONE , COMPL_NONE } } ,
{ " blocksize " , cmd_block , " blocksize <number> (default 20) " , { COMPL_NONE , COMPL_NONE } } ,
{ " cancel " , cmd_cancel , " <jobid> cancel a print queue entry " , { COMPL_NONE , COMPL_NONE } } ,
2004-06-15 22:36:45 +04:00
{ " case_sensitive " , cmd_setcase , " toggle the case sensitive flag to server " , { COMPL_NONE , COMPL_NONE } } ,
2001-09-30 10:49:44 +04:00
{ " cd " , cmd_cd , " [directory] change/report the remote directory " , { COMPL_REMOTE , COMPL_NONE } } ,
2002-01-16 23:13:28 +03:00
{ " chmod " , cmd_chmod , " <src> <mode> chmod a file using UNIX permission " , { COMPL_REMOTE , COMPL_REMOTE } } ,
{ " chown " , cmd_chown , " <src> <uid> <gid> chown a file using UNIX uids and gids " , { COMPL_REMOTE , COMPL_REMOTE } } ,
2001-09-30 10:49:44 +04:00
{ " del " , cmd_del , " <mask> delete all matching files " , { COMPL_REMOTE , COMPL_NONE } } ,
1998-09-23 04:57:34 +04:00
{ " dir " , cmd_dir , " <mask> list the contents of the current directory " , { COMPL_REMOTE , COMPL_NONE } } ,
{ " du " , cmd_du , " <mask> computes the total size of the current directory " , { COMPL_REMOTE , COMPL_NONE } } ,
2001-09-30 10:49:44 +04:00
{ " exit " , cmd_quit , " logoff the server " , { COMPL_NONE , COMPL_NONE } } ,
1998-09-23 04:57:34 +04:00
{ " get " , cmd_get , " <remote name> [local name] get a file " , { COMPL_REMOTE , COMPL_LOCAL } } ,
2004-11-13 02:42:12 +03:00
{ " getfacl " , cmd_getfacl , " <file name> get the POSIX ACL on a file (UNIX extensions only) " , { COMPL_REMOTE , COMPL_LOCAL } } ,
2004-03-04 02:14:23 +03:00
{ " hardlink " , cmd_hardlink , " <src> <dest> create a Windows hard link " , { COMPL_REMOTE , COMPL_REMOTE } } ,
2001-09-30 10:49:44 +04:00
{ " help " , cmd_help , " [command] give help on a command " , { COMPL_NONE , COMPL_NONE } } ,
{ " history " , cmd_history , " displays the command history " , { COMPL_NONE , COMPL_NONE } } ,
{ " lcd " , cmd_lcd , " [directory] change/report the local current working directory " , { COMPL_LOCAL , COMPL_NONE } } ,
2004-04-07 03:01:09 +04:00
{ " link " , cmd_link , " <oldname> <newname> create a UNIX hard link " , { COMPL_REMOTE , COMPL_REMOTE } } ,
2001-09-30 10:49:44 +04:00
{ " lowercase " , cmd_lowercase , " toggle lowercasing of filenames for get " , { COMPL_NONE , COMPL_NONE } } ,
{ " ls " , cmd_dir , " <mask> list the contents of the current directory " , { COMPL_REMOTE , COMPL_NONE } } ,
{ " mask " , cmd_select , " <mask> mask all filenames against this " , { COMPL_REMOTE , COMPL_NONE } } ,
{ " md " , cmd_mkdir , " <directory> make a directory " , { COMPL_NONE , COMPL_NONE } } ,
1998-09-23 04:57:34 +04:00
{ " mget " , cmd_mget , " <mask> get all the matching files " , { COMPL_REMOTE , COMPL_NONE } } ,
2001-09-30 10:49:44 +04:00
{ " mkdir " , cmd_mkdir , " <directory> make a directory " , { COMPL_NONE , COMPL_NONE } } ,
1998-09-23 04:57:34 +04:00
{ " more " , cmd_more , " <remote name> view a remote file with your pager " , { COMPL_REMOTE , COMPL_NONE } } ,
2001-09-30 10:49:44 +04:00
{ " mput " , cmd_mput , " <mask> put all matching files " , { COMPL_REMOTE , COMPL_NONE } } ,
{ " newer " , cmd_newer , " <file> only mget files newer than the specified local file " , { COMPL_LOCAL , COMPL_NONE } } ,
1999-12-13 16:27:58 +03:00
{ " open " , cmd_open , " <mask> open a file " , { COMPL_REMOTE , COMPL_NONE } } ,
1998-09-23 04:57:34 +04:00
{ " print " , cmd_print , " <file name> print a file " , { COMPL_NONE , COMPL_NONE } } ,
2001-09-30 10:49:44 +04:00
{ " prompt " , cmd_prompt , " toggle prompting for filenames for mget and mput " , { COMPL_NONE , COMPL_NONE } } ,
{ " put " , cmd_put , " <local name> [remote name] put a file " , { COMPL_LOCAL , COMPL_REMOTE } } ,
{ " pwd " , cmd_pwd , " show current remote directory (same as 'cd' with no args) " , { COMPL_NONE , COMPL_NONE } } ,
{ " q " , cmd_quit , " logoff the server " , { COMPL_NONE , COMPL_NONE } } ,
1998-09-23 04:57:34 +04:00
{ " queue " , cmd_queue , " show the print queue " , { COMPL_NONE , COMPL_NONE } } ,
1998-11-09 06:45:49 +03:00
{ " quit " , cmd_quit , " logoff the server " , { COMPL_NONE , COMPL_NONE } } ,
2001-09-30 10:49:44 +04:00
{ " rd " , cmd_rmdir , " <directory> remove a directory " , { COMPL_NONE , COMPL_NONE } } ,
{ " recurse " , cmd_recurse , " toggle directory recursion for mget and mput " , { COMPL_NONE , COMPL_NONE } } ,
2002-09-25 19:19:00 +04:00
{ " reget " , cmd_reget , " <remote name> [local name] get a file restarting at end of local file " , { COMPL_REMOTE , COMPL_LOCAL } } ,
2001-09-30 10:49:44 +04:00
{ " rename " , cmd_rename , " <src> <dest> rename some files " , { COMPL_REMOTE , COMPL_REMOTE } } ,
2002-09-25 19:19:00 +04:00
{ " reput " , cmd_reput , " <local name> [remote name] put a file restarting at end of remote file " , { COMPL_LOCAL , COMPL_REMOTE } } ,
2001-09-30 10:49:44 +04:00
{ " rm " , cmd_del , " <mask> delete all matching files " , { COMPL_REMOTE , COMPL_NONE } } ,
{ " rmdir " , cmd_rmdir , " <directory> remove a directory " , { COMPL_NONE , COMPL_NONE } } ,
1998-09-23 04:57:34 +04:00
{ " setmode " , cmd_setmode , " filename <setmode string> change modes of file " , { COMPL_REMOTE , COMPL_NONE } } ,
2004-09-26 10:27:54 +04:00
{ " stat " , cmd_stat , " filename Do a UNIX extensions stat call on a file " , { COMPL_REMOTE , COMPL_REMOTE } } ,
2004-04-07 03:01:09 +04:00
{ " symlink " , cmd_symlink , " <oldname> <newname> create a UNIX symlink " , { COMPL_REMOTE , COMPL_REMOTE } } ,
2001-09-30 10:49:44 +04:00
{ " tar " , cmd_tar , " tar <c|x>[IXFqbgNan] current directory to/from <file name> " , { COMPL_NONE , COMPL_NONE } } ,
{ " tarmode " , cmd_tarmode , " <full|inc|reset|noreset> tar's behaviour towards archive bits " , { COMPL_NONE , COMPL_NONE } } ,
{ " translate " , cmd_translate , " toggle text translation for printing " , { COMPL_NONE , COMPL_NONE } } ,
2005-03-30 04:47:57 +04:00
{ " volume " , cmd_volume , " print the volume name " , { COMPL_NONE , COMPL_NONE } } ,
2004-02-12 20:26:34 +03:00
{ " vuid " , cmd_vuid , " change current vuid " , { COMPL_NONE , COMPL_NONE } } ,
{ " logon " , cmd_logon , " establish new logon " , { COMPL_NONE , COMPL_NONE } } ,
2005-02-22 06:31:22 +03:00
{ " listconnect " , cmd_list_connect , " list open connections " , { COMPL_NONE , COMPL_NONE } } ,
2005-02-23 20:29:28 +03:00
{ " showconnect " , cmd_show_connect , " display the current active connection " , { COMPL_NONE , COMPL_NONE } } ,
2002-01-17 10:14:21 +03:00
/* Yes, this must be here, see crh's comment above. */
{ " ! " , NULL , " run a shell command on the local system " , { COMPL_NONE , COMPL_NONE } } ,
2003-06-11 10:23:53 +04:00
{ NULL , NULL , NULL , { COMPL_NONE , COMPL_NONE } }
1996-05-04 11:50:46 +04:00
} ;
/*******************************************************************
2003-08-07 00:01:31 +04:00
Lookup a command string in the list of commands , including
abbreviations .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-03-12 17:37:37 +03:00
static int process_tok ( pstring tok )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
int i = 0 , matches = 0 ;
int cmd = 0 ;
int tok_len = strlen ( tok ) ;
while ( commands [ i ] . fn ! = NULL ) {
if ( strequal ( commands [ i ] . name , tok ) ) {
matches = 1 ;
cmd = i ;
break ;
} else if ( strnequal ( commands [ i ] . name , tok , tok_len ) ) {
matches + + ;
cmd = i ;
}
i + + ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
if ( matches = = 0 )
return ( - 1 ) ;
else if ( matches = = 1 )
return ( cmd ) ;
else
return ( - 2 ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Help .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int cmd_help ( void )
1996-05-04 11:50:46 +04:00
{
1998-11-09 06:45:49 +03:00
int i = 0 , j ;
2004-03-12 17:37:37 +03:00
pstring buf ;
1998-11-09 06:45:49 +03:00
2001-06-21 13:10:42 +04:00
if ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
1998-11-09 06:45:49 +03:00
if ( ( i = process_tok ( buf ) ) > = 0 )
2001-09-07 18:14:57 +04:00
d_printf ( " HELP %s: \n \t %s \n \n " , commands [ i ] . name , commands [ i ] . description ) ;
1998-11-09 06:45:49 +03:00
} else {
while ( commands [ i ] . description ) {
for ( j = 0 ; commands [ i ] . description & & ( j < 5 ) ; j + + ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %-15s " , commands [ i ] . name ) ;
1998-11-09 06:45:49 +03:00
i + + ;
}
2001-09-07 18:14:57 +04:00
d_printf ( " \n " ) ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
}
2001-10-09 23:12:18 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
1998-09-18 16:47:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Process a - c command string .
1998-09-18 16:47:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int process_command_string ( char * cmd )
1998-09-18 16:47:46 +04:00
{
1998-11-09 06:45:49 +03:00
pstring line ;
2003-08-15 08:42:05 +04:00
const char * ptr ;
2001-10-09 23:12:18 +04:00
int rc = 0 ;
1998-09-18 16:47:46 +04:00
2002-07-15 14:35:28 +04:00
/* establish the connection if not already */
if ( ! cli ) {
2005-02-25 00:54:52 +03:00
cli = cli_cm_open ( desthost , service , True ) ;
2002-07-15 14:35:28 +04:00
if ( ! cli )
return 0 ;
}
1998-11-09 06:45:49 +03:00
while ( cmd [ 0 ] ! = ' \0 ' ) {
char * p ;
2004-03-12 17:37:37 +03:00
pstring tok ;
1998-11-09 06:45:49 +03:00
int i ;
2001-07-04 11:36:09 +04:00
if ( ( p = strchr_m ( cmd , ' ; ' ) ) = = 0 ) {
1998-11-09 06:45:49 +03:00
strncpy ( line , cmd , 999 ) ;
line [ 1000 ] = ' \0 ' ;
cmd + = strlen ( cmd ) ;
} else {
2003-08-07 00:01:31 +04:00
if ( p - cmd > 999 )
p = cmd + 999 ;
1998-11-09 06:45:49 +03:00
strncpy ( line , cmd , p - cmd ) ;
line [ p - cmd ] = ' \0 ' ;
cmd = p + 1 ;
}
/* and get the first part of the command */
ptr = line ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( & ptr , tok , NULL , sizeof ( tok ) ) ) continue ;
1998-11-09 06:45:49 +03:00
if ( ( i = process_tok ( tok ) ) > = 0 ) {
2001-10-09 23:12:18 +04:00
rc = commands [ i ] . fn ( ) ;
1998-11-09 06:45:49 +03:00
} else if ( i = = - 2 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s: command abbreviation ambiguous \n " , tok ) ;
1998-11-09 06:45:49 +03:00
} else {
2001-09-07 18:14:57 +04:00
d_printf ( " %s: command not found \n " , tok ) ;
1998-11-09 06:45:49 +03:00
}
}
2001-10-09 23:12:18 +04:00
return rc ;
1998-11-09 06:45:49 +03:00
}
1998-09-18 16:47:46 +04:00
2001-03-19 03:22:52 +03:00
# define MAX_COMPLETIONS 100
2003-05-12 22:12:31 +04:00
typedef struct {
pstring dirmask ;
char * * matches ;
int count , samelen ;
const char * text ;
int len ;
} completion_remote_t ;
2001-03-19 03:22:52 +03:00
2005-02-26 17:42:55 +03:00
static void completion_remote_filter ( const char * mnt , file_info * f , const char * mask , void * state )
2003-05-12 22:12:31 +04:00
{
completion_remote_t * info = ( completion_remote_t * ) state ;
2001-03-19 03:22:52 +03:00
2003-05-12 22:12:31 +04:00
if ( ( info - > count < MAX_COMPLETIONS - 1 ) & & ( strncmp ( info - > text , f - > name , info - > len ) = = 0 ) & & ( strcmp ( f - > name , " . " ) ! = 0 ) & & ( strcmp ( f - > name , " .. " ) ! = 0 ) ) {
if ( ( info - > dirmask [ 0 ] = = 0 ) & & ! ( f - > mode & aDIR ) )
2004-12-07 21:25:53 +03:00
info - > matches [ info - > count ] = SMB_STRDUP ( f - > name ) ;
2003-05-12 22:12:31 +04:00
else {
pstring tmp ;
2001-03-19 03:22:52 +03:00
2003-05-12 22:12:31 +04:00
if ( info - > dirmask [ 0 ] ! = 0 )
pstrcpy ( tmp , info - > dirmask ) ;
else
tmp [ 0 ] = 0 ;
pstrcat ( tmp , f - > name ) ;
if ( f - > mode & aDIR )
pstrcat ( tmp , " / " ) ;
2004-12-07 21:25:53 +03:00
info - > matches [ info - > count ] = SMB_STRDUP ( tmp ) ;
2001-03-19 03:22:52 +03:00
}
2003-05-12 22:12:31 +04:00
if ( info - > matches [ info - > count ] = = NULL )
return ;
if ( f - > mode & aDIR )
smb_readline_ca_char ( 0 ) ;
if ( info - > count = = 1 )
info - > samelen = strlen ( info - > matches [ info - > count ] ) ;
else
while ( strncmp ( info - > matches [ info - > count ] , info - > matches [ info - > count - 1 ] , info - > samelen ) ! = 0 )
info - > samelen - - ;
info - > count + + ;
2001-03-19 03:22:52 +03:00
}
2003-05-12 22:12:31 +04:00
}
2001-03-19 03:22:52 +03:00
2003-05-12 22:12:31 +04:00
static char * * remote_completion ( const char * text , int len )
{
pstring dirmask ;
int i ;
2003-05-13 10:13:36 +04:00
completion_remote_t info = { " " , NULL , 1 , 0 , NULL , 0 } ;
2003-05-12 22:12:31 +04:00
2003-05-13 10:13:36 +04:00
/* can't have non-static intialisation on Sun CC, so do it
at run time here */
info . samelen = len ;
info . text = text ;
info . len = len ;
2006-03-14 01:49:56 +03:00
if ( len > = MIN ( PATH_MAX , sizeof ( pstring ) ) ) {
2003-05-12 22:12:31 +04:00
return ( NULL ) ;
2006-03-14 01:49:56 +03:00
}
2003-05-12 22:12:31 +04:00
2004-12-07 21:25:53 +03:00
info . matches = SMB_MALLOC_ARRAY ( char * , MAX_COMPLETIONS ) ;
2006-03-14 01:49:56 +03:00
if ( ! info . matches ) {
return NULL ;
}
2003-05-12 22:12:31 +04:00
info . matches [ 0 ] = NULL ;
2006-03-14 01:49:56 +03:00
for ( i = len - 1 ; i > = 0 ; i - - ) {
if ( ( text [ i ] = = ' / ' ) | | ( text [ i ] = = ' \\ ' ) ) {
2003-05-12 22:12:31 +04:00
break ;
2006-03-14 01:49:56 +03:00
}
}
2003-05-12 22:12:31 +04:00
info . text = text + i + 1 ;
info . samelen = info . len = len - i - 1 ;
if ( i > 0 ) {
strncpy ( info . dirmask , text , i + 1 ) ;
info . dirmask [ i + 1 ] = 0 ;
2003-07-23 16:33:59 +04:00
pstr_sprintf ( dirmask , " %s%*s* " , cur_dir , i - 1 , text ) ;
2006-03-14 01:49:56 +03:00
} else {
2003-07-23 16:33:59 +04:00
pstr_sprintf ( dirmask , " %s* " , cur_dir ) ;
2006-03-14 01:49:56 +03:00
}
2003-05-12 22:12:31 +04:00
if ( cli_list ( cli , dirmask , aDIR | aSYSTEM | aHIDDEN , completion_remote_filter , & info ) < 0 )
goto cleanup ;
if ( info . count = = 2 )
2004-12-07 21:25:53 +03:00
info . matches [ 0 ] = SMB_STRDUP ( info . matches [ 1 ] ) ;
2003-05-12 22:12:31 +04:00
else {
2004-12-07 21:25:53 +03:00
info . matches [ 0 ] = SMB_MALLOC ( info . samelen + 1 ) ;
2003-05-12 22:12:31 +04:00
if ( ! info . matches [ 0 ] )
goto cleanup ;
strncpy ( info . matches [ 0 ] , info . matches [ 1 ] , info . samelen ) ;
info . matches [ 0 ] [ info . samelen ] = 0 ;
2001-03-19 03:22:52 +03:00
}
2003-05-12 22:12:31 +04:00
info . matches [ info . count ] = NULL ;
return info . matches ;
cleanup :
for ( i = 0 ; i < info . count ; i + + )
free ( info . matches [ i ] ) ;
free ( info . matches ) ;
return NULL ;
2001-03-19 03:22:52 +03:00
}
2003-05-12 22:12:31 +04:00
static char * * completion_fn ( const char * text , int start , int end )
{
smb_readline_ca_char ( ' ' ) ;
if ( start ) {
const char * buf , * sp ;
int i ;
char compl_type ;
buf = smb_readline_get_line_buffer ( ) ;
if ( buf = = NULL )
return NULL ;
sp = strchr ( buf , ' ' ) ;
if ( sp = = NULL )
return NULL ;
2003-06-11 10:23:53 +04:00
for ( i = 0 ; commands [ i ] . name ; i + + )
2003-05-12 22:12:31 +04:00
if ( ( strncmp ( commands [ i ] . name , text , sp - buf ) = = 0 ) & & ( commands [ i ] . name [ sp - buf ] = = 0 ) )
break ;
if ( commands [ i ] . name = = NULL )
return NULL ;
while ( * sp = = ' ' )
sp + + ;
if ( sp = = ( buf + start ) )
compl_type = commands [ i ] . compl_args [ 0 ] ;
else
compl_type = commands [ i ] . compl_args [ 1 ] ;
if ( compl_type = = COMPL_REMOTE )
return remote_completion ( text , end - start ) ;
else /* fall back to local filename completion */
return NULL ;
} else {
char * * matches ;
2004-11-24 04:03:23 +03:00
int i , len , samelen = 0 , count = 1 ;
2003-05-12 22:12:31 +04:00
2004-12-07 21:25:53 +03:00
matches = SMB_MALLOC_ARRAY ( char * , MAX_COMPLETIONS ) ;
2004-11-24 04:03:23 +03:00
if ( ! matches ) {
return NULL ;
}
2003-05-12 22:12:31 +04:00
matches [ 0 ] = NULL ;
len = strlen ( text ) ;
for ( i = 0 ; commands [ i ] . fn & & count < MAX_COMPLETIONS - 1 ; i + + ) {
if ( strncmp ( text , commands [ i ] . name , len ) = = 0 ) {
2004-12-07 21:25:53 +03:00
matches [ count ] = SMB_STRDUP ( commands [ i ] . name ) ;
2003-05-12 22:12:31 +04:00
if ( ! matches [ count ] )
goto cleanup ;
if ( count = = 1 )
samelen = strlen ( matches [ count ] ) ;
else
while ( strncmp ( matches [ count ] , matches [ count - 1 ] , samelen ) ! = 0 )
samelen - - ;
count + + ;
}
}
switch ( count ) {
case 0 : /* should never happen */
case 1 :
goto cleanup ;
case 2 :
2004-12-07 21:25:53 +03:00
matches [ 0 ] = SMB_STRDUP ( matches [ 1 ] ) ;
2003-05-12 22:12:31 +04:00
break ;
default :
2004-12-07 21:25:53 +03:00
matches [ 0 ] = SMB_MALLOC ( samelen + 1 ) ;
2003-05-12 22:12:31 +04:00
if ( ! matches [ 0 ] )
goto cleanup ;
strncpy ( matches [ 0 ] , matches [ 1 ] , samelen ) ;
matches [ 0 ] [ samelen ] = 0 ;
}
matches [ count ] = NULL ;
return matches ;
cleanup :
2003-11-05 01:38:58 +03:00
for ( i = 0 ; i < count ; i + + )
2003-05-12 22:12:31 +04:00
free ( matches [ i ] ) ;
2003-11-05 01:38:58 +03:00
2003-05-12 22:12:31 +04:00
free ( matches ) ;
return NULL ;
}
}
2001-03-18 16:24:57 +03:00
2001-03-19 02:41:53 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Make sure we swallow keepalives during idle time .
2001-03-19 02:41:53 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-03-19 02:41:53 +03:00
static void readline_callback ( void )
{
fd_set fds ;
struct timeval timeout ;
static time_t last_t ;
time_t t ;
t = time ( NULL ) ;
2003-08-07 00:01:31 +04:00
if ( t - last_t < 5 )
return ;
2001-03-19 02:41:53 +03:00
last_t = t ;
2003-05-10 04:30:47 +04:00
again :
2003-03-18 13:56:37 +03:00
if ( cli - > fd = = - 1 )
return ;
2001-03-19 02:41:53 +03:00
FD_ZERO ( & fds ) ;
FD_SET ( cli - > fd , & fds ) ;
timeout . tv_sec = 0 ;
timeout . tv_usec = 0 ;
2002-02-01 02:26:12 +03:00
sys_select_intr ( cli - > fd + 1 , & fds , NULL , NULL , & timeout ) ;
2001-03-19 02:41:53 +03:00
/* We deliberately use receive_smb instead of
client_receive_smb as we want to receive
session keepalives and then drop them here .
*/
if ( FD_ISSET ( cli - > fd , & fds ) ) {
2005-11-30 20:56:50 +03:00
if ( ! receive_smb ( cli - > fd , cli - > inbuf , 0 ) ) {
DEBUG ( 0 , ( " Read from server failed, maybe it closed the "
" connection \n " ) ) ;
return ;
}
2001-03-19 02:41:53 +03:00
goto again ;
}
2005-11-30 20:53:21 +03:00
/* Ping the server to keep the connection alive using SMBecho. */
{
unsigned char garbage [ 16 ] ;
memset ( garbage , 0xf0 , sizeof ( garbage ) ) ;
cli_echo ( cli , garbage , sizeof ( garbage ) ) ;
}
2001-03-19 02:41:53 +03:00
}
1998-11-09 06:45:49 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Process commands on stdin .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2004-07-02 00:20:06 +04:00
static int process_stdin ( void )
1998-09-18 16:47:46 +04:00
{
2003-08-15 08:42:05 +04:00
const char * ptr ;
2004-07-02 00:20:06 +04:00
int rc = 0 ;
1998-09-18 16:47:46 +04:00
2001-03-19 02:41:53 +03:00
while ( 1 ) {
2004-03-12 17:37:37 +03:00
pstring tok ;
pstring the_prompt ;
2001-07-04 11:15:53 +04:00
char * cline ;
pstring line ;
1998-11-09 06:45:49 +03:00
int i ;
1999-12-13 16:27:58 +03:00
2001-03-18 16:24:57 +03:00
/* display a prompt */
2001-04-16 06:35:35 +04:00
slprintf ( the_prompt , sizeof ( the_prompt ) - 1 , " smb: %s> " , cur_dir ) ;
2001-07-04 11:15:53 +04:00
cline = smb_readline ( the_prompt , readline_callback , completion_fn ) ;
if ( ! cline ) break ;
pstrcpy ( line , cline ) ;
1999-12-13 16:27:58 +03:00
1998-11-09 06:45:49 +03:00
/* special case - first char is ! */
2001-03-19 02:41:53 +03:00
if ( * line = = ' ! ' ) {
system ( line + 1 ) ;
1998-11-09 06:45:49 +03:00
continue ;
}
/* and get the first part of the command */
2001-03-19 02:41:53 +03:00
ptr = line ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( & ptr , tok , NULL , sizeof ( tok ) ) ) continue ;
1998-11-09 06:45:49 +03:00
if ( ( i = process_tok ( tok ) ) > = 0 ) {
2004-07-02 00:20:06 +04:00
rc = commands [ i ] . fn ( ) ;
1998-11-09 06:45:49 +03:00
} else if ( i = = - 2 ) {
2001-09-07 18:14:57 +04:00
d_printf ( " %s: command abbreviation ambiguous \n " , tok ) ;
1998-11-09 06:45:49 +03:00
} else {
2001-09-07 18:14:57 +04:00
d_printf ( " %s: command not found \n " , tok ) ;
1998-11-09 06:45:49 +03:00
}
}
2004-07-02 00:20:06 +04:00
return rc ;
1998-09-18 16:47:46 +04:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Process commands from the client .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
2001-10-09 23:12:18 +04:00
static int process ( char * base_directory )
1996-05-04 11:50:46 +04:00
{
2001-10-09 23:12:18 +04:00
int rc = 0 ;
2005-02-25 00:54:52 +03:00
cli = cli_cm_open ( desthost , service , True ) ;
1998-11-09 06:45:49 +03:00
if ( ! cli ) {
2001-11-02 14:31:49 +03:00
return 1 ;
1996-05-31 19:13:29 +04:00
}
1998-11-13 01:17:51 +03:00
2005-04-15 04:39:03 +04:00
if ( * base_directory ) {
rc = do_cd ( base_directory ) ;
if ( rc ) {
cli_cm_shutdown ( ) ;
return rc ;
}
}
1998-09-18 16:47:46 +04:00
1998-11-09 06:45:49 +03:00
if ( cmdstr ) {
2001-10-09 23:12:18 +04:00
rc = process_command_string ( cmdstr ) ;
1998-11-09 06:45:49 +03:00
} else {
process_stdin ( ) ;
1996-05-04 11:50:46 +04:00
}
2005-02-23 22:26:32 +03:00
cli_cm_shutdown ( ) ;
2001-10-09 23:12:18 +04:00
return rc ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
/****************************************************************************
2003-08-07 00:01:31 +04:00
Handle a - L query .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1999-12-13 16:27:58 +03:00
static int do_host_query ( char * query_host )
1998-11-09 06:45:49 +03:00
{
2005-02-25 00:54:52 +03:00
cli = cli_cm_open ( query_host , " IPC$ " , True ) ;
1998-11-17 04:44:25 +03:00
if ( ! cli )
return 1 ;
1998-11-09 06:45:49 +03:00
browse_host ( True ) ;
2003-08-11 01:43:28 +04:00
if ( port ! = 139 ) {
/* Workgroups simply don't make sense over anything
else but port 139. . . */
2005-02-23 22:26:32 +03:00
cli_cm_shutdown ( ) ;
2005-02-25 00:54:52 +03:00
cli_cm_set_port ( 139 ) ;
cli = cli_cm_open ( query_host , " IPC$ " , True ) ;
2003-08-11 01:43:28 +04:00
}
if ( cli = = NULL ) {
d_printf ( " NetBIOS over TCP disabled -- no workgroup available \n " ) ;
return 1 ;
}
2003-04-28 10:54:49 +04:00
list_servers ( lp_workgroup ( ) ) ;
1998-11-09 06:45:49 +03:00
2005-02-23 22:26:32 +03:00
cli_cm_shutdown ( ) ;
1998-11-09 06:45:49 +03:00
return ( 0 ) ;
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Handle a tar operation .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1999-12-13 16:27:58 +03:00
static int do_tar_op ( char * base_directory )
1998-11-09 06:45:49 +03:00
{
int ret ;
2002-07-15 14:35:28 +04:00
/* do we already have a connection? */
if ( ! cli ) {
2005-02-25 00:54:52 +03:00
cli = cli_cm_open ( desthost , service , True ) ;
2002-07-15 14:35:28 +04:00
if ( ! cli )
return 1 ;
}
1998-11-09 06:45:49 +03:00
recurse = True ;
2005-04-15 04:39:03 +04:00
if ( * base_directory ) {
ret = do_cd ( base_directory ) ;
if ( ret ) {
cli_cm_shutdown ( ) ;
return ret ;
}
}
1998-11-09 06:45:49 +03:00
ret = process_tar ( ) ;
2005-02-23 22:26:32 +03:00
cli_cm_shutdown ( ) ;
1998-11-09 06:45:49 +03:00
return ( ret ) ;
}
/****************************************************************************
2003-08-07 00:01:31 +04:00
Handle a message operation .
1998-11-09 06:45:49 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1998-11-09 06:45:49 +03:00
static int do_message_op ( void )
{
struct in_addr ip ;
struct nmb_name called , calling ;
2002-09-25 19:19:00 +04:00
fstring server_name ;
char name_type_hex [ 10 ] ;
2005-04-11 07:34:27 +04:00
int msg_port ;
1998-11-09 06:45:49 +03:00
2005-02-24 03:11:42 +03:00
make_nmb_name ( & calling , calling_name , 0x0 ) ;
2000-01-07 09:55:36 +03:00
make_nmb_name ( & called , desthost , name_type ) ;
1998-11-09 06:45:49 +03:00
2003-03-18 14:22:52 +03:00
fstrcpy ( server_name , desthost ) ;
2002-09-25 19:19:00 +04:00
snprintf ( name_type_hex , sizeof ( name_type_hex ) , " #%X " , name_type ) ;
2003-03-18 14:22:52 +03:00
fstrcat ( server_name , name_type_hex ) ;
2002-09-25 19:19:00 +04:00
2001-11-26 06:11:44 +03:00
zero_ip ( & ip ) ;
2005-02-25 00:54:52 +03:00
if ( have_ip )
ip = dest_ip ;
1998-11-09 06:45:49 +03:00
2005-04-11 07:34:27 +04:00
/* we can only do messages over port 139 (to windows clients at least) */
msg_port = port ? port : 139 ;
if ( ! ( cli = cli_initialise ( NULL ) ) | | ( cli_set_port ( cli , msg_port ) ! = msg_port ) | |
2002-09-25 19:19:00 +04:00
! cli_connect ( cli , server_name , & ip ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " Connection to %s failed \n " , desthost ) ;
1998-11-09 06:45:49 +03:00
return 1 ;
}
if ( ! cli_session_request ( cli , & calling , & called ) ) {
2001-09-07 18:14:57 +04:00
d_printf ( " session request failed \n " ) ;
2005-02-23 22:26:32 +03:00
cli_cm_shutdown ( ) ;
1998-11-09 06:45:49 +03:00
return 1 ;
}
send_message ( ) ;
2005-02-23 22:26:32 +03:00
cli_cm_shutdown ( ) ;
1998-11-09 06:45:49 +03:00
return 0 ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
main program
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-07 00:01:31 +04:00
1996-06-04 10:42:03 +04:00
int main ( int argc , char * argv [ ] )
1996-05-04 11:50:46 +04:00
{
2004-03-12 17:37:37 +03:00
pstring base_directory ;
1998-11-09 06:45:49 +03:00
int opt ;
pstring query_host ;
BOOL message = False ;
pstring term_code ;
2003-04-14 08:05:48 +04:00
static const char * new_name_resolve_order = NULL ;
poptContext pc ;
1998-11-09 06:45:49 +03:00
char * p ;
2001-10-09 23:12:18 +04:00
int rc = 0 ;
2003-08-25 21:55:33 +04:00
fstring new_workgroup ;
2003-04-14 08:05:48 +04:00
struct poptOption long_options [ ] = {
POPT_AUTOHELP
{ " name-resolve " , ' R ' , POPT_ARG_STRING , & new_name_resolve_order , ' R ' , " Use these name resolution services only " , " NAME-RESOLVE-ORDER " } ,
{ " message " , ' M ' , POPT_ARG_STRING , NULL , ' M ' , " Send message " , " HOST " } ,
{ " ip-address " , ' I ' , POPT_ARG_STRING , NULL , ' I ' , " Use this IP to connect to " , " IP " } ,
{ " stderr " , ' E ' , POPT_ARG_NONE , NULL , ' E ' , " Write messages to stderr instead of stdout " } ,
{ " list " , ' L ' , POPT_ARG_STRING , NULL , ' L ' , " Get a list of shares available on a host " , " HOST " } ,
{ " terminal " , ' t ' , POPT_ARG_STRING , NULL , ' t ' , " Terminal I/O code {sjis|euc|jis7|jis8|junet|hex} " , " CODE " } ,
{ " max-protocol " , ' m ' , POPT_ARG_STRING , NULL , ' m ' , " Set the max protocol level " , " LEVEL " } ,
{ " tar " , ' T ' , POPT_ARG_STRING , NULL , ' T ' , " Command line tar " , " <c|x>IXFqgbNan " } ,
{ " directory " , ' D ' , POPT_ARG_STRING , NULL , ' D ' , " Start from directory " , " DIR " } ,
{ " command " , ' c ' , POPT_ARG_STRING , & cmdstr , ' c ' , " Execute semicolon separated commands " } ,
2003-06-11 10:10:10 +04:00
{ " send-buffer " , ' b ' , POPT_ARG_INT , & io_bufsize , ' b ' , " Changes the transmit/send buffer " , " BYTES " } ,
2003-04-14 08:05:48 +04:00
{ " port " , ' p ' , POPT_ARG_INT , & port , ' p ' , " Port to connect to " , " PORT " } ,
2004-01-16 18:01:09 +03:00
{ " grepable " , ' g ' , POPT_ARG_NONE , NULL , ' g ' , " Produce grepable output " } ,
2003-04-14 08:05:48 +04:00
POPT_COMMON_SAMBA
POPT_COMMON_CONNECTION
POPT_COMMON_CREDENTIALS
POPT_TABLEEND
} ;
2005-12-29 01:48:54 +03:00
load_case_tables ( ) ;
1997-09-12 00:17:32 +04:00
# ifdef KANJI
1998-11-09 06:45:49 +03:00
pstrcpy ( term_code , KANJI ) ;
1997-09-12 00:17:32 +04:00
# else /* KANJI */
1998-11-09 06:45:49 +03:00
* term_code = 0 ;
1997-09-12 00:17:32 +04:00
# endif /* KANJI */
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
* query_host = 0 ;
* base_directory = 0 ;
2003-08-25 21:55:33 +04:00
/* initialize the workgroup name so we can determine whether or
not it was set by a command line option */
set_global_myworkgroup ( " " ) ;
2005-02-24 03:11:42 +03:00
set_global_myname ( " " ) ;
1996-05-04 11:50:46 +04:00
2003-08-07 02:45:46 +04:00
/* set default debug level to 0 regardless of what smb.conf sets */
2004-01-15 22:03:18 +03:00
setup_logging ( " smbclient " , True ) ;
DEBUGLEVEL_CLASS [ DBGC_ALL ] = 1 ;
2006-06-16 03:51:20 +04:00
if ( ( dbf = x_fdup ( x_stderr ) ) ) {
x_setbuf ( dbf , NULL ) ;
}
1998-03-16 23:59:47 +03:00
2003-04-14 08:05:48 +04:00
pc = poptGetContext ( " smbclient " , argc , ( const char * * ) argv , long_options ,
POPT_CONTEXT_KEEP_FIRST ) ;
poptSetOtherOptionHelp ( pc , " service <password> " ) ;
1998-09-29 08:43:40 +04:00
1998-11-09 06:45:49 +03:00
in_client = True ; /* Make sure that we tell lp_load we are */
1998-06-16 02:02:14 +04:00
2003-04-14 08:05:48 +04:00
while ( ( opt = poptGetNextOpt ( pc ) ) ! = - 1 ) {
1998-11-09 06:45:49 +03:00
switch ( opt ) {
case ' M ' :
2003-01-07 07:26:37 +03:00
/* Messages are sent to NetBIOS name type 0x3
* ( Messenger Service ) . Make sure we default
* to port 139 instead of port 445. srl , crh
*/
name_type = 0x03 ;
2005-02-25 00:54:52 +03:00
cli_cm_set_dest_name_type ( name_type ) ;
2003-04-14 08:05:48 +04:00
pstrcpy ( desthost , poptGetOptArg ( pc ) ) ;
2005-02-25 00:54:52 +03:00
if ( ! port )
cli_cm_set_port ( 139 ) ;
2003-01-14 11:53:59 +03:00
message = True ;
break ;
1998-11-09 06:45:49 +03:00
case ' I ' :
1998-10-29 05:18:17 +03:00
{
2003-04-14 08:05:48 +04:00
dest_ip = * interpret_addr2 ( poptGetOptArg ( pc ) ) ;
2001-11-26 06:11:44 +03:00
if ( is_zero_ip ( dest_ip ) )
1998-11-09 06:45:49 +03:00
exit ( 1 ) ;
have_ip = True ;
2005-02-25 00:54:52 +03:00
cli_cm_set_dest_ip ( dest_ip ) ;
1998-10-29 05:18:17 +03:00
}
1998-11-09 06:45:49 +03:00
break ;
case ' E ' :
2001-09-10 15:08:57 +04:00
dbf = x_stderr ;
2003-04-14 08:05:48 +04:00
display_set_stderr ( ) ;
2000-08-29 18:35:31 +04:00
break ;
1998-11-09 06:45:49 +03:00
case ' L ' :
2004-03-11 17:39:32 +03:00
pstrcpy ( query_host , poptGetOptArg ( pc ) ) ;
1998-11-09 06:45:49 +03:00
break ;
case ' t ' :
2003-04-14 08:05:48 +04:00
pstrcpy ( term_code , poptGetOptArg ( pc ) ) ;
1998-11-09 06:45:49 +03:00
break ;
case ' m ' :
2003-04-14 08:05:48 +04:00
max_protocol = interpret_protocol ( poptGetOptArg ( pc ) , max_protocol ) ;
1998-11-09 06:45:49 +03:00
break ;
case ' T ' :
2003-08-02 01:09:10 +04:00
/* We must use old option processing for this. Find the
* position of the - T option in the raw argv [ ] . */
{
int i , optnum ;
for ( i = 1 ; i < argc ; i + + ) {
if ( strncmp ( " -T " , argv [ i ] , 2 ) = = 0 )
break ;
}
i + + ;
if ( ! ( optnum = tar_parseargs ( argc , argv , poptGetOptArg ( pc ) , i ) ) ) {
poptPrintUsage ( pc , stderr , 0 ) ;
exit ( 1 ) ;
}
/* Now we must eat (optnum - i) options - they have
* been processed by tar_parseargs ( ) .
*/
optnum - = i ;
for ( i = 0 ; i < optnum ; i + + )
poptGetOptArg ( pc ) ;
1998-11-09 06:45:49 +03:00
}
break ;
case ' D ' :
2004-03-12 17:37:37 +03:00
pstrcpy ( base_directory , poptGetOptArg ( pc ) ) ;
1998-11-09 06:45:49 +03:00
break ;
2004-01-16 18:01:09 +03:00
case ' g ' :
grepable = True ;
break ;
2003-04-14 08:05:48 +04:00
}
}
poptGetArg ( pc ) ;
2005-02-25 00:54:52 +03:00
2005-05-10 02:39:20 +04:00
/* check for the -P option */
if ( port ! = 0 )
cli_cm_set_port ( port ) ;
2003-08-07 02:45:46 +04:00
/*
* Don ' t load debug level from smb . conf . It should be
* set by cmdline arg or remain default ( 0 )
*/
AllowDebugChange = False ;
2003-08-25 21:55:33 +04:00
/* save the workgroup...
2004-03-26 18:40:06 +03:00
FIXME ! ! do we need to do this for other options as well
2003-08-25 21:55:33 +04:00
( or maybe a generic way to keep lp_load ( ) from overwriting
everything ) ? */
2004-03-26 18:40:06 +03:00
fstrcpy ( new_workgroup , lp_workgroup ( ) ) ;
2005-02-24 03:11:42 +03:00
pstrcpy ( calling_name , global_myname ( ) ) ;
2004-03-26 18:40:06 +03:00
if ( override_logfile )
setup_logging ( lp_logfile ( ) , False ) ;
2003-08-25 21:55:33 +04:00
2006-01-29 01:53:04 +03:00
if ( ! lp_load ( dyn_CONFIGFILE , True , False , False , True ) ) {
2003-08-07 02:45:46 +04:00
fprintf ( stderr , " %s: Can't load %s - run testparm to debug it \n " ,
argv [ 0 ] , dyn_CONFIGFILE ) ;
}
2003-04-14 08:05:48 +04:00
load_interfaces ( ) ;
2003-08-25 21:55:33 +04:00
if ( strlen ( new_workgroup ) ! = 0 )
set_global_myworkgroup ( new_workgroup ) ;
2003-04-14 08:05:48 +04:00
2005-02-24 03:11:42 +03:00
if ( strlen ( calling_name ) ! = 0 )
set_global_myname ( calling_name ) ;
2005-04-19 18:03:08 +04:00
else
pstrcpy ( calling_name , global_myname ( ) ) ;
2005-02-24 03:11:42 +03:00
2003-04-14 08:05:48 +04:00
if ( poptPeekArg ( pc ) ) {
pstrcpy ( service , poptGetArg ( pc ) ) ;
/* Convert any '/' characters in the service name to '\' characters */
string_replace ( service , ' / ' , ' \\ ' ) ;
if ( count_chars ( service , ' \\ ' ) < 3 ) {
d_printf ( " \n %s: Not enough ' \\ ' characters in service \n " , service ) ;
poptPrintUsage ( pc , stderr , 0 ) ;
1998-10-29 05:18:17 +03:00
exit ( 1 ) ;
}
1996-05-04 11:50:46 +04:00
}
2003-08-02 01:09:10 +04:00
if ( poptPeekArg ( pc ) & & ! cmdline_auth_info . got_pass ) {
2003-04-14 08:05:48 +04:00
cmdline_auth_info . got_pass = True ;
pstrcpy ( cmdline_auth_info . password , poptGetArg ( pc ) ) ;
}
2003-02-24 06:28:37 +03:00
2002-11-13 02:20:50 +03:00
init_names ( ) ;
1998-09-18 16:47:46 +04:00
2003-04-14 08:05:48 +04:00
if ( new_name_resolve_order )
1998-11-09 06:45:49 +03:00
lp_set_name_resolve_order ( new_name_resolve_order ) ;
1998-09-18 16:47:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! tar_type & & ! * query_host & & ! * service & & ! message ) {
2003-04-14 08:05:48 +04:00
poptPrintUsage ( pc , stderr , 0 ) ;
1998-11-09 06:45:49 +03:00
exit ( 1 ) ;
}
1996-05-04 11:50:46 +04:00
2003-04-14 08:05:48 +04:00
poptFreeContext ( pc ) ;
2005-02-25 00:54:52 +03:00
/* store the username an password for dfs support */
2003-07-02 07:01:27 +04:00
2005-02-25 00:54:52 +03:00
cli_cm_set_credentials ( & cmdline_auth_info ) ;
pstrcpy ( username , cmdline_auth_info . username ) ;
2003-04-14 08:05:48 +04:00
2003-08-20 21:13:38 +04:00
DEBUG ( 3 , ( " Client started (version %s). \n " , SAMBA_VERSION_STRING ) ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( tar_type ) {
2000-11-23 02:06:29 +03:00
if ( cmdstr )
process_command_string ( cmdstr ) ;
1999-12-13 16:27:58 +03:00
return do_tar_op ( base_directory ) ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( * query_host ) {
2004-03-11 17:39:32 +03:00
char * qhost = query_host ;
char * slash ;
while ( * qhost = = ' \\ ' | | * qhost = = ' / ' )
qhost + + ;
if ( ( slash = strchr_m ( qhost , ' / ' ) )
| | ( slash = strchr_m ( qhost , ' \\ ' ) ) ) {
* slash = 0 ;
}
if ( ( p = strchr_m ( qhost , ' # ' ) ) ) {
* p = 0 ;
p + + ;
sscanf ( p , " %x " , & name_type ) ;
2005-02-25 00:54:52 +03:00
cli_cm_set_dest_name_type ( name_type ) ;
2004-03-11 17:39:32 +03:00
}
2005-02-25 00:54:52 +03:00
2004-03-11 17:39:32 +03:00
return do_host_query ( qhost ) ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
if ( message ) {
return do_message_op ( ) ;
1996-05-04 11:50:46 +04:00
}
2001-03-18 16:24:57 +03:00
2001-10-09 23:12:18 +04:00
if ( process ( base_directory ) ) {
return 1 ;
1996-05-04 11:50:46 +04:00
}
2001-10-09 23:12:18 +04:00
return rc ;
1996-05-04 11:50:46 +04:00
}