2008-03-07 19:37:07 +03:00
/*
2008-02-28 19:23:20 +03:00
Unix SMB / Netbios implementation .
SMB client library implementation
Copyright ( C ) Andrew Tridgell 1998
Copyright ( C ) Richard Sharpe 2000 , 2002
Copyright ( C ) John Terpstra 2000
2008-03-07 19:37:07 +03:00
Copyright ( C ) Tom Jansen ( Ninja ISD ) 2002
2008-02-28 19:23:20 +03:00
Copyright ( C ) Derrell Lipman 2003 - 2008
Copyright ( C ) Jeremy Allison 2007 , 2008
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03: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 3 of the License , or
( at your option ) any later version .
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
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 .
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
2011-05-06 13:47:43 +04:00
# include "libsmb/libsmb.h"
2012-01-30 13:53:18 +04:00
# include "auth_info.h"
2008-02-28 19:23:20 +03:00
# include "libsmbclient.h"
# include "libsmb_internal.h"
2011-02-28 12:19:44 +03:00
# include "rpc_client/cli_pipe.h"
2011-01-12 14:58:44 +03:00
# include "../librpc/gen_ndr/ndr_srvsvc_c.h"
2011-03-23 16:18:59 +03:00
# include "libsmb/nmblib.h"
2008-02-28 19:23:20 +03:00
/*
* Routine to open a directory
* We accept the URL syntax explained in SMBC_parse_path ( ) , above .
*/
static void
remove_dir ( SMBCFILE * dir )
{
struct smbc_dir_list * d , * f ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
d = dir - > dir_list ;
while ( d ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
f = d ; d = d - > next ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
SAFE_FREE ( f - > dirent ) ;
SAFE_FREE ( f ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_list = dir - > dir_end = dir - > dir_next = NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
static int
add_dirent ( SMBCFILE * dir ,
const char * name ,
const char * comment ,
uint32 type )
{
struct smbc_dirent * dirent ;
int size ;
int name_length = ( name = = NULL ? 0 : strlen ( name ) ) ;
int comment_len = ( comment = = NULL ? 0 : strlen ( comment ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
2008-03-07 19:37:07 +03:00
* Allocate space for the dirent , which must be increased by the
2008-02-28 19:23:20 +03:00
* size of the name and the comment and 1 each for the null terminator .
*/
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
size = sizeof ( struct smbc_dirent ) + name_length + comment_len + 2 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dirent = ( struct smbc_dirent * ) SMB_MALLOC ( size ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! dirent ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_error = ENOMEM ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
ZERO_STRUCTP ( dirent ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir - > dir_list = = NULL ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_list = SMB_MALLOC_P ( struct smbc_dir_list ) ;
if ( ! dir - > dir_list ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
SAFE_FREE ( dirent ) ;
dir - > dir_error = ENOMEM ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
ZERO_STRUCTP ( dir - > dir_list ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_end = dir - > dir_next = dir - > dir_list ;
}
else {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_end - > next = SMB_MALLOC_P ( struct smbc_dir_list ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! dir - > dir_end - > next ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
SAFE_FREE ( dirent ) ;
dir - > dir_error = ENOMEM ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
ZERO_STRUCTP ( dir - > dir_end - > next ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_end = dir - > dir_end - > next ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_end - > next = NULL ;
dir - > dir_end - > dirent = dirent ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dirent - > smbc_type = type ;
dirent - > namelen = name_length ;
dirent - > commentlen = comment_len ;
dirent - > dirlen = size ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* dirent - > namelen + 1 includes the null ( no null termination needed )
* Ditto for dirent - > commentlen .
* The space for the two null bytes was allocated .
*/
strncpy ( dirent - > name , ( name ? name : " " ) , dirent - > namelen + 1 ) ;
dirent - > comment = ( char * ) ( & dirent - > name + dirent - > namelen + 1 ) ;
strncpy ( dirent - > comment , ( comment ? comment : " " ) , dirent - > commentlen + 1 ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
return 0 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
static void
list_unique_wg_fn ( const char * name ,
uint32 type ,
const char * comment ,
void * state )
{
SMBCFILE * dir = ( SMBCFILE * ) state ;
struct smbc_dir_list * dir_list ;
struct smbc_dirent * dirent ;
int dirent_type ;
int do_remove = 0 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dirent_type = dir - > dir_type ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( add_dirent ( dir , name , comment , dirent_type ) < 0 ) {
/* An error occurred, what do we do? */
/* FIXME: Add some code here */
2010-10-29 22:56:51 +04:00
/* Change cli_NetServerEnum to take a fn
returning NTSTATUS . . . JRA . */
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Point to the one just added */
dirent = dir - > dir_end - > dirent ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* See if this was a duplicate */
for ( dir_list = dir - > dir_list ;
dir_list ! = dir - > dir_end ;
dir_list = dir_list - > next ) {
if ( ! do_remove & &
strcmp ( dir_list - > dirent - > name , dirent - > name ) = = 0 ) {
/* Duplicate. End end of list need to be removed. */
do_remove = 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( do_remove & & dir_list - > next = = dir - > dir_end ) {
/* Found the end of the list. Remove it. */
dir - > dir_end = dir_list ;
free ( dir_list - > next ) ;
free ( dirent ) ;
dir_list - > next = NULL ;
break ;
}
}
}
static void
list_fn ( const char * name ,
uint32 type ,
const char * comment ,
void * state )
{
SMBCFILE * dir = ( SMBCFILE * ) state ;
int dirent_type ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* We need to process the type a little . . .
*
* Disk share = 0x00000000
* Print share = 0x00000001
* Comms share = 0x00000002 ( obsolete ? )
* IPC $ share = 0x00000003
*
* administrative shares :
* ADMIN $ , IPC $ , C $ , D $ , E $ . . . are type | = 0x80000000
*/
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir - > dir_type = = SMBC_FILE_SHARE ) {
switch ( type ) {
case 0 | 0x80000000 :
case 0 :
dirent_type = SMBC_FILE_SHARE ;
break ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
case 1 :
dirent_type = SMBC_PRINTER_SHARE ;
break ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
case 2 :
dirent_type = SMBC_COMMS_SHARE ;
break ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
case 3 | 0x80000000 :
case 3 :
dirent_type = SMBC_IPC_SHARE ;
break ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
default :
dirent_type = SMBC_FILE_SHARE ; /* FIXME, error? */
break ;
}
}
else {
dirent_type = dir - > dir_type ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( add_dirent ( dir , name , comment , dirent_type ) < 0 ) {
/* An error occurred, what do we do? */
/* FIXME: Add some code here */
2010-10-29 22:56:51 +04:00
/* Change cli_NetServerEnum to take a fn
returning NTSTATUS . . . JRA . */
2008-02-28 19:23:20 +03:00
}
}
2010-10-29 22:56:51 +04:00
static NTSTATUS
2008-02-28 19:23:20 +03:00
dir_list_fn ( const char * mnt ,
2010-07-27 12:59:55 +04:00
struct file_info * finfo ,
2008-02-28 19:23:20 +03:00
const char * mask ,
void * state )
{
2008-03-07 19:37:07 +03:00
if ( add_dirent ( ( SMBCFILE * ) state , finfo - > name , " " ,
2011-04-29 05:57:02 +04:00
( finfo - > mode & FILE_ATTRIBUTE_DIRECTORY ? SMBC_DIR : SMBC_FILE ) ) < 0 ) {
2010-10-29 22:56:51 +04:00
SMBCFILE * dir = ( SMBCFILE * ) state ;
return map_nt_error_from_unix ( dir - > dir_error ) ;
2008-03-07 19:37:07 +03:00
}
2010-10-29 22:56:51 +04:00
return NT_STATUS_OK ;
2008-02-28 19:23:20 +03:00
}
static int
net_share_enum_rpc ( struct cli_state * cli ,
void ( * fn ) ( const char * name ,
uint32 type ,
const char * comment ,
void * state ) ,
void * state )
{
int i ;
WERROR result ;
uint32 preferred_len = 0xffffffff ;
uint32 type ;
2008-03-10 06:58:43 +03:00
struct srvsvc_NetShareInfoCtr info_ctr ;
struct srvsvc_NetShareCtr1 ctr1 ;
2008-02-28 19:23:20 +03:00
fstring name = " " ;
fstring comment = " " ;
2009-11-13 00:56:33 +03:00
struct rpc_pipe_client * pipe_hnd = NULL ;
2008-02-28 19:23:20 +03:00
NTSTATUS nt_status ;
2008-03-10 06:58:43 +03:00
uint32_t resume_handle = 0 ;
uint32_t total_entries = 0 ;
2011-01-12 14:58:44 +03:00
struct dcerpc_binding_handle * b ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Open the server service pipe */
2008-07-20 13:04:31 +04:00
nt_status = cli_rpc_pipe_open_noauth ( cli , & ndr_table_srvsvc . syntax_id ,
& pipe_hnd ) ;
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
2008-02-28 19:23:20 +03:00
DEBUG ( 1 , ( " net_share_enum_rpc pipe open fail! \n " ) ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-03-10 06:58:43 +03:00
ZERO_STRUCT ( info_ctr ) ;
ZERO_STRUCT ( ctr1 ) ;
info_ctr . level = 1 ;
info_ctr . ctr . ctr1 = & ctr1 ;
2011-01-12 14:58:44 +03:00
b = pipe_hnd - > binding_handle ;
2008-02-28 19:23:20 +03:00
/* Issue the NetShareEnum RPC call and retrieve the response */
2011-01-12 14:58:44 +03:00
nt_status = dcerpc_srvsvc_NetShareEnumAll ( b , talloc_tos ( ) ,
2008-04-19 23:56:43 +04:00
pipe_hnd - > desthost ,
2008-03-10 06:58:43 +03:00
& info_ctr ,
preferred_len ,
& total_entries ,
& resume_handle ,
& result ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Was it successful? */
2011-01-12 14:58:44 +03:00
if ( ! NT_STATUS_IS_OK ( nt_status ) ) {
/* Nope. Go clean up. */
result = ntstatus_to_werror ( nt_status ) ;
goto done ;
}
if ( ! W_ERROR_IS_OK ( result ) ) {
2008-02-28 19:23:20 +03:00
/* Nope. Go clean up. */
goto done ;
}
2008-03-07 19:37:07 +03:00
2011-01-12 14:58:44 +03:00
if ( total_entries = = 0 ) {
/* Nope. Go clean up. */
result = WERR_GENERAL_FAILURE ;
goto done ;
}
2008-02-28 19:23:20 +03:00
/* For each returned entry... */
2010-01-16 15:31:44 +03:00
for ( i = 0 ; i < info_ctr . ctr . ctr1 - > count ; i + + ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* pull out the share name */
2008-03-10 06:58:43 +03:00
fstrcpy ( name , info_ctr . ctr . ctr1 - > array [ i ] . name ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* pull out the share's comment */
2008-03-10 06:58:43 +03:00
fstrcpy ( comment , info_ctr . ctr . ctr1 - > array [ i ] . comment ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Get the type value */
2008-03-10 06:58:43 +03:00
type = info_ctr . ctr . ctr1 - > array [ i ] . type ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Add this share to the list */
( * fn ) ( name , type , comment , state ) ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
done :
/* Close the server service pipe */
2008-04-20 15:51:46 +04:00
TALLOC_FREE ( pipe_hnd ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Tell 'em if it worked */
return W_ERROR_IS_OK ( result ) ? 0 : - 1 ;
}
/*
* Verify that the options specified in a URL are valid
*/
int
SMBC_check_options ( char * server ,
char * share ,
char * path ,
char * options )
{
DEBUG ( 4 , ( " SMBC_check_options(): server='%s' share='%s' "
" path='%s' options='%s' \n " ,
server , share , path , options ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* No options at all is always ok */
if ( ! * options ) return 0 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Currently, we don't support any options. */
return - 1 ;
}
SMBCFILE *
SMBC_opendir_ctx ( SMBCCTX * context ,
const char * fname )
{
int saved_errno ;
2008-03-02 04:44:21 +03:00
char * server = NULL ;
char * share = NULL ;
char * user = NULL ;
char * password = NULL ;
char * options = NULL ;
2008-02-28 19:23:20 +03:00
char * workgroup = NULL ;
char * path = NULL ;
uint16 mode ;
char * p = NULL ;
SMBCSRV * srv = NULL ;
SMBCFILE * dir = NULL ;
struct sockaddr_storage rem_ss ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-02-28 19:23:20 +03:00
DEBUG ( 4 , ( " no valid context \n " ) ) ;
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = EINVAL + 8192 ;
2008-02-28 19:23:20 +03:00
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! fname ) {
DEBUG ( 4 , ( " no valid fname \n " ) ) ;
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = EINVAL + 8193 ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( SMBC_parse_path ( frame ,
2008-03-02 04:44:21 +03:00
context ,
fname ,
& workgroup ,
& server ,
& share ,
& path ,
& user ,
& password ,
& options ) ) {
2008-02-28 19:23:20 +03:00
DEBUG ( 4 , ( " no valid path \n " ) ) ;
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = EINVAL + 8194 ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
DEBUG ( 4 , ( " parsed path: fname='%s' server='%s' share='%s' "
" path='%s' options='%s' \n " ,
fname , server , share , path , options ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Ensure the options are valid */
if ( SMBC_check_options ( server , share , path , options ) ) {
DEBUG ( 4 , ( " unacceptable options (%s) \n " , options ) ) ;
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = EINVAL + 8195 ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! user | | user [ 0 ] = = ( char ) 0 ) {
2008-03-04 02:13:33 +03:00
user = talloc_strdup ( frame , smbc_getUser ( context ) ) ;
2008-02-28 19:23:20 +03:00
if ( ! user ) {
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = ENOMEM ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir = SMB_MALLOC_P ( SMBCFILE ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! dir ) {
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = ENOMEM ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
ZERO_STRUCTP ( dir ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > cli_fd = 0 ;
dir - > fname = SMB_STRDUP ( fname ) ;
dir - > srv = NULL ;
dir - > offset = 0 ;
dir - > file = False ;
dir - > dir_list = dir - > dir_next = dir - > dir_end = NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( server [ 0 ] = = ( char ) 0 ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
int i ;
int count ;
int max_lmb_count ;
2011-04-30 21:23:55 +04:00
struct sockaddr_storage * ip_list ;
struct sockaddr_storage server_addr ;
2008-02-28 19:23:20 +03:00
struct user_auth_info u_info ;
2011-04-30 21:23:55 +04:00
NTSTATUS status ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( share [ 0 ] ! = ( char ) 0 | | path [ 0 ] ! = ( char ) 0 ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = EINVAL + 8196 ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Determine how many local master browsers to query */
2008-03-04 02:13:33 +03:00
max_lmb_count = ( smbc_getOptionBrowseMaxLmbCount ( context ) = = 0
2008-02-28 19:23:20 +03:00
? INT_MAX
2008-03-04 02:13:33 +03:00
: smbc_getOptionBrowseMaxLmbCount ( context ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
memset ( & u_info , ' \0 ' , sizeof ( u_info ) ) ;
u_info . username = talloc_strdup ( frame , user ) ;
u_info . password = talloc_strdup ( frame , password ) ;
if ( ! u_info . username | | ! u_info . password ) {
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* We have server and share and path empty but options
* requesting that we scan all master browsers for their list
* of workgroups / domains . This implies that we must first try
* broadcast queries to find all master browsers , and if that
* doesn ' t work , then try our other methods which return only
* a single master browser .
*/
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
ip_list = NULL ;
2011-04-30 21:23:55 +04:00
status = name_resolve_bcast ( MSBROWSE , 1 , talloc_tos ( ) ,
& ip_list , & count ) ;
if ( ! NT_STATUS_IS_OK ( status ) )
2008-02-28 19:23:20 +03:00
{
2008-03-07 19:37:07 +03:00
2011-04-30 21:23:55 +04:00
TALLOC_FREE ( ip_list ) ;
2008-03-07 19:37:07 +03:00
2011-04-30 21:23:55 +04:00
if ( ! find_master_ip ( workgroup , & server_addr ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = ENOENT ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
2008-03-07 19:37:07 +03:00
2011-04-30 21:23:55 +04:00
ip_list = ( struct sockaddr_storage * ) talloc_memdup (
talloc_tos ( ) , & server_addr ,
sizeof ( server_addr ) ) ;
2008-02-28 19:23:20 +03:00
if ( ip_list = = NULL ) {
2011-04-22 11:51:42 +04:00
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = ENOMEM ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
count = 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
for ( i = 0 ; i < count & & i < max_lmb_count ; i + + ) {
char addr [ INET6_ADDRSTRLEN ] ;
char * wg_ptr = NULL ;
struct cli_state * cli = NULL ;
2008-03-07 19:37:07 +03:00
2011-04-30 21:23:55 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & ip_list [ i ] ) ;
2008-02-28 19:23:20 +03:00
DEBUG ( 99 , ( " Found master browser %d of %d: %s \n " ,
i + 1 , MAX ( count , max_lmb_count ) ,
addr ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
cli = get_ipc_connect_master_ip ( talloc_tos ( ) ,
2011-04-30 21:23:55 +04:00
& ip_list [ i ] ,
2008-02-28 19:23:20 +03:00
& u_info ,
& wg_ptr ) ;
/* cli == NULL is the master browser refused to talk or
could not be found */
if ( ! cli ) {
continue ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
workgroup = talloc_strdup ( frame , wg_ptr ) ;
2011-07-22 18:33:51 +04:00
server = talloc_strdup ( frame , cli_state_remote_name ( cli ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
cli_shutdown ( cli ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! workgroup | | ! server ) {
2011-03-24 17:59:55 +03:00
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = ENOMEM ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
DEBUG ( 4 , ( " using workgroup %s %s \n " ,
workgroup , server ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* For each returned master browser IP address , get a
* connection to IPC $ on the server if we do not
* already have one , and determine the
* workgroups / domains that it knows about .
*/
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
srv = SMBC_server ( frame , context , True , server , " IPC$ " ,
& workgroup , & user , & password ) ;
if ( ! srv ) {
continue ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > srv = srv ;
dir - > dir_type = SMBC_WORKGROUP ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Now, list the stuff ... */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! cli_NetServerEnum ( srv - > cli ,
workgroup ,
SV_TYPE_DOMAIN_ENUM ,
list_unique_wg_fn ,
( void * ) dir ) ) {
continue ;
}
}
2008-03-07 19:37:07 +03:00
2011-04-30 21:23:55 +04:00
TALLOC_FREE ( ip_list ) ;
2008-02-28 19:23:20 +03:00
} else {
/*
* Server not an empty string . . . Check the rest and see what
* gives
*/
if ( * share = = ' \0 ' ) {
if ( * path ! = ' \0 ' ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Should not have empty share with path */
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = EINVAL + 8197 ;
2008-02-28 19:23:20 +03:00
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* We don ' t know if < server > is really a server name
* or is a workgroup / domain name . If we already have
* a server structure for it , we ' ll use it .
* Otherwise , check to see if < server > < 1 D > ,
* < server > < 1 B > , or < server > < 20 > translates . We check
* to see if < server > is an IP address first .
*/
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* See if we have an existing server . Do not
* establish a connection if one does not already
* exist .
*/
2008-03-02 04:44:21 +03:00
srv = SMBC_server ( frame , context , False ,
server , " IPC$ " ,
2008-02-28 19:23:20 +03:00
& workgroup , & user , & password ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* If no existing server and not an IP addr , look for
* LMB or DMB
*/
if ( ! srv & &
! is_ipaddress ( server ) & &
2009-07-28 22:51:58 +04:00
( resolve_name ( server , & rem_ss , 0x1d , false ) | | /* LMB */
resolve_name ( server , & rem_ss , 0x1b , false ) ) ) { /* DMB */
2009-11-09 23:44:47 +03:00
/*
* " server " is actually a workgroup name ,
* not a server . Make this clear .
*/
char * wgroup = server ;
2008-02-28 19:23:20 +03:00
fstring buserver ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_type = SMBC_SERVER ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* Get the backup list . . .
*/
2009-11-09 23:44:47 +03:00
if ( ! name_status_find ( wgroup , 0 , 0 ,
2008-02-28 19:23:20 +03:00
& rem_ss , buserver ) ) {
2009-11-09 23:44:47 +03:00
char addr [ INET6_ADDRSTRLEN ] ;
2008-03-07 19:37:07 +03:00
2009-11-09 23:44:47 +03:00
print_sockaddr ( addr , sizeof ( addr ) , & rem_ss ) ;
2008-03-02 04:44:21 +03:00
DEBUG ( 0 , ( " Could not get name of "
2009-11-09 23:44:47 +03:00
" local/domain master browser "
" for workgroup %s from "
" address %s \n " ,
wgroup ,
addr ) ) ;
2008-02-28 19:23:20 +03:00
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = EPERM ;
2008-02-28 19:23:20 +03:00
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* Get a connection to IPC $ on the server if
* we do not already have one
*/
srv = SMBC_server ( frame , context , True ,
buserver , " IPC$ " ,
2008-03-02 04:44:21 +03:00
& workgroup ,
& user , & password ) ;
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
DEBUG ( 0 , ( " got no contact to IPC$ \n " ) ) ;
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > srv = srv ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Now, list the servers ... */
2009-11-09 23:44:47 +03:00
if ( ! cli_NetServerEnum ( srv - > cli , wgroup ,
2008-02-28 19:23:20 +03:00
0x0000FFFE , list_fn ,
( void * ) dir ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
}
} else if ( srv | |
2009-07-28 22:51:58 +04:00
( resolve_name ( server , & rem_ss , 0x20 , false ) ) ) {
2008-03-07 19:37:07 +03:00
2008-03-02 04:44:21 +03:00
/*
* If we hadn ' t found the server , get one now
*/
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
srv = SMBC_server ( frame , context , True ,
server , " IPC$ " ,
& workgroup ,
& user , & password ) ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_type = SMBC_FILE_SHARE ;
dir - > srv = srv ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* List the shares ... */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( net_share_enum_rpc (
srv - > cli ,
list_fn ,
( void * ) dir ) < 0 & &
cli_RNetShareEnum (
srv - > cli ,
list_fn ,
( void * ) dir ) < 0 ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = cli_errno ( srv - > cli ) ;
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
} else {
/* Neither the workgroup nor server exists */
errno = ECONNREFUSED ;
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
else {
/*
* The server and share are specified . . . work from
* there . . .
*/
char * targetpath ;
struct cli_state * targetcli ;
2010-08-02 21:22:22 +04:00
NTSTATUS status ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* We connect to the server and list the directory */
dir - > dir_type = SMBC_FILE_SHARE ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
srv = SMBC_server ( frame , context , True , server , share ,
& workgroup , & user , & password ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > srv = srv ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Now, list the files ... */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
p = path + strlen ( path ) ;
path = talloc_asprintf_append ( path , " \\ * " ) ;
if ( ! path ) {
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
}
2008-03-07 19:37:07 +03:00
2011-07-03 22:53:55 +04:00
status = cli_resolve_path (
frame , " " , context - > internal - > auth_info ,
srv - > cli , path , & targetcli , & targetpath ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-02-28 19:23:20 +03:00
d_printf ( " Could not resolve %s \n " , path ) ;
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
TALLOC_FREE ( frame ) ;
return NULL ;
}
2008-03-07 19:37:07 +03:00
2010-08-02 21:22:22 +04:00
status = cli_list ( targetcli , targetpath ,
2011-04-29 05:57:02 +04:00
FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN ,
2010-08-02 21:22:22 +04:00
dir_list_fn , ( void * ) dir ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-02-28 19:23:20 +03:00
if ( dir ) {
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ;
}
saved_errno = SMBC_errno ( context , targetcli ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( saved_errno = = EINVAL ) {
2008-03-02 04:44:21 +03:00
/*
* See if they asked to opendir
* something other than a directory .
* If so , the converted error value we
* got would have been EINVAL rather
* than ENOTDIR .
*/
* p = ' \0 ' ; /* restore original path */
2008-03-07 19:37:07 +03:00
2008-03-02 04:44:21 +03:00
if ( SMBC_getatr ( context , srv , path ,
& mode , NULL ,
NULL , NULL , NULL , NULL ,
NULL ) & &
! IS_DOS_DIR ( mode ) ) {
2008-03-07 19:37:07 +03:00
2008-03-02 04:44:21 +03:00
/* It is. Correct the error value */
saved_errno = ENOTDIR ;
}
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* If there was an error and the server is no
* good any more . . .
*/
if ( cli_is_error ( targetcli ) & &
2008-03-04 02:13:33 +03:00
smbc_getFunctionCheckServer ( context ) ( context , srv ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* ... then remove it. */
2008-03-04 02:13:33 +03:00
if ( smbc_getFunctionRemoveUnusedServer ( context ) ( context ,
2008-03-07 19:37:07 +03:00
srv ) ) {
2008-02-28 19:23:20 +03:00
/*
* We could not remove the
* server completely , remove
* it from the cache so we
* will not get it again . It
* will be removed when the
* last file / dir is closed .
*/
2008-03-04 02:13:33 +03:00
smbc_getFunctionRemoveCachedServer ( context ) ( context , srv ) ;
2008-02-28 19:23:20 +03:00
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
2011-03-24 18:03:13 +03:00
errno = saved_errno ;
2008-02-28 19:23:20 +03:00
return NULL ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
DLIST_ADD ( context - > internal - > files , dir ) ;
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return dir ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
/*
* Routine to close a directory
*/
int
SMBC_closedir_ctx ( SMBCCTX * context ,
SMBCFILE * dir )
{
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! dir | | ! SMBC_dlist_contains ( context - > internal - > files , dir ) ) {
2008-02-28 19:23:20 +03:00
errno = EBADF ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
remove_dir ( dir ) ; /* Clean it up */
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
DLIST_REMOVE ( context - > internal - > files , dir ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
SAFE_FREE ( dir - > fname ) ;
SAFE_FREE ( dir ) ; /* Free the space too */
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return 0 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
static void
smbc_readdir_internal ( SMBCCTX * context ,
struct smbc_dirent * dest ,
struct smbc_dirent * src ,
int max_namebuf_len )
{
2008-03-04 02:13:33 +03:00
if ( smbc_getOptionUrlEncodeReaddirEntries ( context ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* url-encode the name. get back remaining buffer space */
max_namebuf_len =
2009-01-17 04:26:46 +03:00
smbc_urlencode ( dest - > name , src - > name , max_namebuf_len ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* We now know the name length */
dest - > namelen = strlen ( dest - > name ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Save the pointer to the beginning of the comment */
dest - > comment = dest - > name + dest - > namelen + 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Copy the comment */
strncpy ( dest - > comment , src - > comment , max_namebuf_len - 1 ) ;
dest - > comment [ max_namebuf_len - 1 ] = ' \0 ' ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Save other fields */
dest - > smbc_type = src - > smbc_type ;
dest - > commentlen = strlen ( dest - > comment ) ;
dest - > dirlen = ( ( dest - > comment + dest - > commentlen + 1 ) -
( char * ) dest ) ;
} else {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* No encoding. Just copy the entry as is. */
memcpy ( dest , src , src - > dirlen ) ;
dest - > comment = ( char * ) ( & dest - > name + src - > namelen + 1 ) ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
/*
* Routine to get a directory entry
*/
struct smbc_dirent *
SMBC_readdir_ctx ( SMBCCTX * context ,
SMBCFILE * dir )
{
int maxlen ;
struct smbc_dirent * dirp , * dirent ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Check that all is ok first ... */
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
DEBUG ( 0 , ( " Invalid context in SMBC_readdir_ctx() \n " ) ) ;
TALLOC_FREE ( frame ) ;
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! dir | | ! SMBC_dlist_contains ( context - > internal - > files , dir ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EBADF ;
DEBUG ( 0 , ( " Invalid dir in SMBC_readdir_ctx() \n " ) ) ;
TALLOC_FREE ( frame ) ;
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir - > file ! = False ) { /* FIXME, should be dir, perhaps */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = ENOTDIR ;
DEBUG ( 0 , ( " Found file vs directory in SMBC_readdir_ctx() \n " ) ) ;
TALLOC_FREE ( frame ) ;
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! dir - > dir_next ) {
TALLOC_FREE ( frame ) ;
return NULL ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dirent = dir - > dir_next - > dirent ;
if ( ! dirent ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = ENOENT ;
TALLOC_FREE ( frame ) ;
return NULL ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-12-16 18:54:30 +03:00
dirp = & context - > internal - > dirent ;
maxlen = sizeof ( context - > internal - > _dirent_name ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
smbc_readdir_internal ( context , dirp , dirent , maxlen ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_next = dir - > dir_next - > next ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return dirp ;
}
/*
* Routine to get directory entries
*/
int
SMBC_getdents_ctx ( SMBCCTX * context ,
SMBCFILE * dir ,
struct smbc_dirent * dirp ,
int count )
{
int rem = count ;
int reqd ;
int maxlen ;
char * ndir = ( char * ) dirp ;
struct smbc_dir_list * dirlist ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Check that all is ok first ... */
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! dir | | ! SMBC_dlist_contains ( context - > internal - > files , dir ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EBADF ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir - > file ! = False ) { /* FIXME, should be dir, perhaps */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = ENOTDIR ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
/*
2008-02-28 19:23:20 +03:00
* Now , retrieve the number of entries that will fit in what was passed
2008-03-07 19:37:07 +03:00
* We have to figure out if the info is in the list , or we need to
2008-02-28 19:23:20 +03:00
* send a request to the server to get the info .
*/
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
while ( ( dirlist = dir - > dir_next ) ) {
struct smbc_dirent * dirent ;
2010-10-28 01:17:46 +04:00
struct smbc_dirent * currentEntry = ( struct smbc_dirent * ) ndir ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! dirlist - > dirent ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = ENOENT ; /* Bad error */
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Do urlencoding of next entry, if so selected */
2008-12-16 18:54:30 +03:00
dirent = & context - > internal - > dirent ;
maxlen = sizeof ( context - > internal - > _dirent_name ) ;
2008-03-02 04:44:21 +03:00
smbc_readdir_internal ( context , dirent ,
dirlist - > dirent , maxlen ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
reqd = dirent - > dirlen ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( rem < reqd ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( rem < count ) { /* We managed to copy something */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = 0 ;
TALLOC_FREE ( frame ) ;
return count - rem ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
else { /* Nothing copied ... */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ; /* Not enough space ... */
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2010-10-28 01:17:46 +04:00
memcpy ( currentEntry , dirent , reqd ) ; /* Copy the data in ... */
2008-03-07 19:37:07 +03:00
2010-10-28 01:17:46 +04:00
currentEntry - > comment = & currentEntry - > name [ 0 ] +
dirent - > namelen + 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
ndir + = reqd ;
rem - = reqd ;
2008-03-07 19:37:07 +03:00
2010-10-28 01:17:46 +04:00
/* Try and align the struct for the next entry
on a valid pointer boundary by appending zeros */
while ( ( rem > 0 ) & & ( ( unsigned long long ) ndir & ( sizeof ( void * ) - 1 ) ) ) {
* ndir = ' \0 ' ;
rem - - ;
ndir + + ;
currentEntry - > dirlen + + ;
}
2008-02-28 19:23:20 +03:00
dir - > dir_next = dirlist = dirlist - > next ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( rem = = count )
return 0 ;
else
return count - rem ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
/*
* Routine to create a directory . . .
*/
int
SMBC_mkdir_ctx ( SMBCCTX * context ,
const char * fname ,
mode_t mode )
{
SMBCSRV * srv = NULL ;
char * server = NULL ;
char * share = NULL ;
char * user = NULL ;
char * password = NULL ;
char * workgroup = NULL ;
char * path = NULL ;
char * targetpath = NULL ;
struct cli_state * targetcli = NULL ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2011-07-03 22:53:55 +04:00
NTSTATUS status ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! fname ) {
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
DEBUG ( 4 , ( " smbc_mkdir(%s) \n " , fname ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( SMBC_parse_path ( frame ,
2008-03-02 04:44:21 +03:00
context ,
fname ,
& workgroup ,
& server ,
& share ,
& path ,
& user ,
& password ,
NULL ) ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! user | | user [ 0 ] = = ( char ) 0 ) {
2008-03-04 02:13:33 +03:00
user = talloc_strdup ( frame , smbc_getUser ( context ) ) ;
2008-02-28 19:23:20 +03:00
if ( ! user ) {
errno = ENOMEM ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
srv = SMBC_server ( frame , context , True ,
server , share , & workgroup , & user , & password ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ; /* errno set by SMBC_server */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*d_printf(">>>mkdir: resolving %s\n", path);*/
2011-07-03 22:53:55 +04:00
status = cli_resolve_path ( frame , " " , context - > internal - > auth_info ,
srv - > cli , path , & targetcli , & targetpath ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-02-28 19:23:20 +03:00
d_printf ( " Could not resolve %s \n " , path ) ;
2009-03-28 01:02:46 +03:00
errno = ENOENT ;
TALLOC_FREE ( frame ) ;
2008-02-28 19:23:20 +03:00
return - 1 ;
}
/*d_printf(">>>mkdir: resolved path as %s\n", targetpath);*/
2008-03-07 19:37:07 +03:00
2009-04-21 16:52:34 +04:00
if ( ! NT_STATUS_IS_OK ( cli_mkdir ( targetcli , targetpath ) ) ) {
2008-02-28 19:23:20 +03:00
errno = SMBC_errno ( context , targetcli ) ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
}
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return 0 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
/*
* Our list function simply checks to see if a directory is not empty
*/
2010-10-29 22:56:51 +04:00
static NTSTATUS
2008-02-28 19:23:20 +03:00
rmdir_list_fn ( const char * mnt ,
2010-07-27 12:59:55 +04:00
struct file_info * finfo ,
2008-02-28 19:23:20 +03:00
const char * mask ,
void * state )
{
if ( strncmp ( finfo - > name , " . " , 1 ) ! = 0 & &
strncmp ( finfo - > name , " .. " , 2 ) ! = 0 ) {
2008-12-13 19:04:12 +03:00
bool * smbc_rmdir_dirempty = ( bool * ) state ;
* smbc_rmdir_dirempty = false ;
2008-02-28 19:23:20 +03:00
}
2010-10-29 22:56:51 +04:00
return NT_STATUS_OK ;
2008-02-28 19:23:20 +03:00
}
/*
* Routine to remove a directory
*/
int
SMBC_rmdir_ctx ( SMBCCTX * context ,
const char * fname )
{
SMBCSRV * srv = NULL ;
char * server = NULL ;
char * share = NULL ;
char * user = NULL ;
char * password = NULL ;
char * workgroup = NULL ;
char * path = NULL ;
char * targetpath = NULL ;
struct cli_state * targetcli = NULL ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2011-07-03 22:53:55 +04:00
NTSTATUS status ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! fname ) {
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
DEBUG ( 4 , ( " smbc_rmdir(%s) \n " , fname ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( SMBC_parse_path ( frame ,
2008-03-02 04:44:21 +03:00
context ,
fname ,
& workgroup ,
& server ,
& share ,
& path ,
& user ,
& password ,
NULL ) ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! user | | user [ 0 ] = = ( char ) 0 ) {
2008-03-04 02:13:33 +03:00
user = talloc_strdup ( frame , smbc_getUser ( context ) ) ;
2008-02-28 19:23:20 +03:00
if ( ! user ) {
errno = ENOMEM ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
srv = SMBC_server ( frame , context , True ,
server , share , & workgroup , & user , & password ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ; /* errno set by SMBC_server */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*d_printf(">>>rmdir: resolving %s\n", path);*/
2011-07-03 22:53:55 +04:00
status = cli_resolve_path ( frame , " " , context - > internal - > auth_info ,
srv - > cli , path , & targetcli , & targetpath ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-02-28 19:23:20 +03:00
d_printf ( " Could not resolve %s \n " , path ) ;
2009-03-28 01:02:46 +03:00
errno = ENOENT ;
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ;
}
/*d_printf(">>>rmdir: resolved path as %s\n", targetpath);*/
2008-03-07 19:37:07 +03:00
2009-04-21 17:52:54 +04:00
if ( ! NT_STATUS_IS_OK ( cli_rmdir ( targetcli , targetpath ) ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = SMBC_errno ( context , targetcli ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( errno = = EACCES ) { /* Check if the dir empty or not */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Local storage to avoid buffer overflows */
char * lpath ;
2008-12-13 19:04:12 +03:00
bool smbc_rmdir_dirempty = true ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
lpath = talloc_asprintf ( frame , " %s \\ * " ,
targetpath ) ;
if ( ! lpath ) {
errno = ENOMEM ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2010-08-02 21:22:22 +04:00
status = cli_list ( targetcli , lpath ,
2011-04-29 05:57:02 +04:00
FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN ,
2010-08-02 21:22:22 +04:00
rmdir_list_fn ,
& smbc_rmdir_dirempty ) ;
2008-03-07 19:37:07 +03:00
2010-08-02 21:22:22 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-02-28 19:23:20 +03:00
/* Fix errno to ignore latest error ... */
DEBUG ( 5 , ( " smbc_rmdir: "
" cli_list returned an error: %d \n " ,
SMBC_errno ( context , targetcli ) ) ) ;
errno = EACCES ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( smbc_rmdir_dirempty )
errno = EACCES ;
else
errno = ENOTEMPTY ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
}
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return 0 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
/*
* Routine to return the current directory position
*/
off_t
SMBC_telldir_ctx ( SMBCCTX * context ,
SMBCFILE * dir )
{
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! dir | | ! SMBC_dlist_contains ( context - > internal - > files , dir ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EBADF ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir - > file ! = False ) { /* FIXME, should be dir, perhaps */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = ENOTDIR ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* See if we're already at the end. */
if ( dir - > dir_next = = NULL ) {
/* We are. */
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*
* We return the pointer here as the offset
*/
TALLOC_FREE ( frame ) ;
return ( off_t ) ( long ) dir - > dir_next - > dirent ;
}
/*
* A routine to run down the list and see if the entry is OK
*/
static struct smbc_dir_list *
2008-03-07 19:37:07 +03:00
check_dir_ent ( struct smbc_dir_list * list ,
2008-03-02 04:44:21 +03:00
struct smbc_dirent * dirent )
2008-02-28 19:23:20 +03:00
{
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Run down the list looking for what we want */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dirent ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
struct smbc_dir_list * tmp = list ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
while ( tmp ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( tmp - > dirent = = dirent )
return tmp ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
tmp = tmp - > next ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
return NULL ; /* Not found, or an error */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
/*
* Routine to seek on a directory
*/
int
SMBC_lseekdir_ctx ( SMBCCTX * context ,
SMBCFILE * dir ,
off_t offset )
{
long int l_offset = offset ; /* Handle problems of size */
struct smbc_dirent * dirent = ( struct smbc_dirent * ) l_offset ;
struct smbc_dir_list * list_ent = ( struct smbc_dir_list * ) NULL ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dir - > file ! = False ) { /* FIXME, should be dir, perhaps */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = ENOTDIR ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Now, check what we were passed and see if it is OK ... */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( dirent = = NULL ) { /* Seek to the begining of the list */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_next = dir - > dir_list ;
TALLOC_FREE ( frame ) ;
return 0 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( offset = = - 1 ) { /* Seek to the end of the list */
dir - > dir_next = NULL ;
TALLOC_FREE ( frame ) ;
return 0 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Now, run down the list and make sure that the entry is OK */
/* This may need to be changed if we change the format of the list */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ( list_ent = check_dir_ent ( dir - > dir_list , dirent ) ) = = NULL ) {
errno = EINVAL ; /* Bad entry */
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dir - > dir_next = list_ent ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return 0 ;
}
/*
* Routine to fstat a dir
*/
int
SMBC_fstatdir_ctx ( SMBCCTX * context ,
SMBCFILE * dir ,
struct stat * st )
{
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* No code yet ... */
return 0 ;
}
int
SMBC_chmod_ctx ( SMBCCTX * context ,
const char * fname ,
mode_t newmode )
{
SMBCSRV * srv = NULL ;
char * server = NULL ;
char * share = NULL ;
char * user = NULL ;
char * password = NULL ;
char * workgroup = NULL ;
2009-02-20 07:00:46 +03:00
char * targetpath = NULL ;
struct cli_state * targetcli = NULL ;
2008-02-28 19:23:20 +03:00
char * path = NULL ;
uint16 mode ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2011-07-03 22:53:55 +04:00
NTSTATUS status ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ; /* Best I can think of ... */
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! fname ) {
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2009-02-24 03:22:43 +03:00
DEBUG ( 4 , ( " smbc_chmod(%s, 0%3o) \n " , fname , ( unsigned int ) newmode ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( SMBC_parse_path ( frame ,
2008-03-02 04:44:21 +03:00
context ,
fname ,
& workgroup ,
& server ,
& share ,
& path ,
& user ,
& password ,
NULL ) ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! user | | user [ 0 ] = = ( char ) 0 ) {
2008-03-04 02:13:33 +03:00
user = talloc_strdup ( frame , smbc_getUser ( context ) ) ;
2008-02-28 19:23:20 +03:00
if ( ! user ) {
errno = ENOMEM ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
srv = SMBC_server ( frame , context , True ,
server , share , & workgroup , & user , & password ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
TALLOC_FREE ( frame ) ;
return - 1 ; /* errno set by SMBC_server */
}
2009-02-20 07:00:46 +03:00
/*d_printf(">>>unlink: resolving %s\n", path);*/
2011-07-03 22:53:55 +04:00
status = cli_resolve_path ( frame , " " , context - > internal - > auth_info ,
srv - > cli , path , & targetcli , & targetpath ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-02-20 07:00:46 +03:00
d_printf ( " Could not resolve %s \n " , path ) ;
2009-03-28 01:02:46 +03:00
errno = ENOENT ;
2009-02-20 07:00:46 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
mode = 0 ;
2008-03-07 19:37:07 +03:00
2011-04-29 05:36:14 +04:00
if ( ! ( newmode & ( S_IWUSR | S_IWGRP | S_IWOTH ) ) ) mode | = FILE_ATTRIBUTE_READONLY ;
2011-04-29 06:00:57 +04:00
if ( ( newmode & S_IXUSR ) & & lp_map_archive ( - 1 ) ) mode | = FILE_ATTRIBUTE_ARCHIVE ;
2011-04-29 07:23:14 +04:00
if ( ( newmode & S_IXGRP ) & & lp_map_system ( - 1 ) ) mode | = FILE_ATTRIBUTE_SYSTEM ;
2011-04-29 05:43:35 +04:00
if ( ( newmode & S_IXOTH ) & & lp_map_hidden ( - 1 ) ) mode | = FILE_ATTRIBUTE_HIDDEN ;
2008-03-07 19:37:07 +03:00
2009-05-07 03:13:42 +04:00
if ( ! NT_STATUS_IS_OK ( cli_setatr ( targetcli , targetpath , mode , 0 ) ) ) {
2009-02-20 07:00:46 +03:00
errno = SMBC_errno ( context , targetcli ) ;
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return 0 ;
}
int
SMBC_utimes_ctx ( SMBCCTX * context ,
const char * fname ,
struct timeval * tbuf )
{
SMBCSRV * srv = NULL ;
char * server = NULL ;
char * share = NULL ;
char * user = NULL ;
char * password = NULL ;
char * workgroup = NULL ;
char * path = NULL ;
time_t access_time ;
time_t write_time ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ; /* Best I can think of ... */
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! fname ) {
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( tbuf = = NULL ) {
access_time = write_time = time ( NULL ) ;
} else {
access_time = tbuf [ 0 ] . tv_sec ;
write_time = tbuf [ 1 ] . tv_sec ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( DEBUGLVL ( 4 ) ) {
char * p ;
char atimebuf [ 32 ] ;
char mtimebuf [ 32 ] ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
strncpy ( atimebuf , ctime ( & access_time ) , sizeof ( atimebuf ) - 1 ) ;
atimebuf [ sizeof ( atimebuf ) - 1 ] = ' \0 ' ;
if ( ( p = strchr ( atimebuf , ' \n ' ) ) ! = NULL ) {
* p = ' \0 ' ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
strncpy ( mtimebuf , ctime ( & write_time ) , sizeof ( mtimebuf ) - 1 ) ;
mtimebuf [ sizeof ( mtimebuf ) - 1 ] = ' \0 ' ;
if ( ( p = strchr ( mtimebuf , ' \n ' ) ) ! = NULL ) {
* p = ' \0 ' ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
dbgtext ( " smbc_utimes(%s, atime = %s mtime = %s) \n " ,
fname , atimebuf , mtimebuf ) ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( SMBC_parse_path ( frame ,
2008-03-02 04:44:21 +03:00
context ,
fname ,
& workgroup ,
& server ,
& share ,
& path ,
& user ,
& password ,
NULL ) ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! user | | user [ 0 ] = = ( char ) 0 ) {
2008-03-04 02:13:33 +03:00
user = talloc_strdup ( frame , smbc_getUser ( context ) ) ;
2008-02-28 19:23:20 +03:00
if ( ! user ) {
errno = ENOMEM ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
srv = SMBC_server ( frame , context , True ,
server , share , & workgroup , & user , & password ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
TALLOC_FREE ( frame ) ;
return - 1 ; /* errno set by SMBC_server */
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! SMBC_setatr ( context , srv , path ,
0 , access_time , write_time , 0 , 0 ) ) {
TALLOC_FREE ( frame ) ;
return - 1 ; /* errno set by SMBC_setatr */
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return 0 ;
}
/*
* Routine to unlink ( ) a file
*/
int
SMBC_unlink_ctx ( SMBCCTX * context ,
const char * fname )
{
2008-03-02 04:44:21 +03:00
char * server = NULL ;
char * share = NULL ;
char * user = NULL ;
char * password = NULL ;
char * workgroup = NULL ;
2008-02-28 19:23:20 +03:00
char * path = NULL ;
char * targetpath = NULL ;
struct cli_state * targetcli = NULL ;
SMBCSRV * srv = NULL ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2011-07-03 22:53:55 +04:00
NTSTATUS status ;
2008-03-07 19:37:07 +03:00
2008-02-29 21:34:35 +03:00
if ( ! context | | ! context - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ; /* Best I can think of ... */
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! fname ) {
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( SMBC_parse_path ( frame ,
2008-03-02 04:44:21 +03:00
context ,
fname ,
& workgroup ,
& server ,
& share ,
& path ,
& user ,
& password ,
NULL ) ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! user | | user [ 0 ] = = ( char ) 0 ) {
2008-03-04 02:13:33 +03:00
user = talloc_strdup ( frame , smbc_getUser ( context ) ) ;
2008-02-28 19:23:20 +03:00
if ( ! user ) {
errno = ENOMEM ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
srv = SMBC_server ( frame , context , True ,
server , share , & workgroup , & user , & password ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! srv ) {
TALLOC_FREE ( frame ) ;
return - 1 ; /* SMBC_server sets errno */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/*d_printf(">>>unlink: resolving %s\n", path);*/
2011-07-03 22:53:55 +04:00
status = cli_resolve_path ( frame , " " , context - > internal - > auth_info ,
srv - > cli , path , & targetcli , & targetpath ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-02-28 19:23:20 +03:00
d_printf ( " Could not resolve %s \n " , path ) ;
2009-03-28 01:02:46 +03:00
errno = ENOENT ;
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ;
}
/*d_printf(">>>unlink: resolved path as %s\n", targetpath);*/
2008-03-07 19:37:07 +03:00
2011-04-29 07:23:14 +04:00
if ( ! NT_STATUS_IS_OK ( cli_unlink ( targetcli , targetpath , FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN ) ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = SMBC_errno ( context , targetcli ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( errno = = EACCES ) { /* Check if the file is a directory */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
int saverr = errno ;
SMB_OFF_T size = 0 ;
uint16 mode = 0 ;
struct timespec write_time_ts ;
struct timespec access_time_ts ;
struct timespec change_time_ts ;
SMB_INO_T ino = 0 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! SMBC_getatr ( context , srv , path , & mode , & size ,
NULL ,
& access_time_ts ,
& write_time_ts ,
& change_time_ts ,
& ino ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
/* Hmmm, bad error ... What? */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = SMBC_errno ( context , targetcli ) ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
else {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( IS_DOS_DIR ( mode ) )
errno = EISDIR ;
else
errno = saverr ; /* Restore this */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return 0 ; /* Success ... */
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
/*
* Routine to rename ( ) a file
*/
int
SMBC_rename_ctx ( SMBCCTX * ocontext ,
2008-03-07 19:37:07 +03:00
const char * oname ,
2008-02-28 19:23:20 +03:00
SMBCCTX * ncontext ,
const char * nname )
{
char * server1 = NULL ;
char * share1 = NULL ;
char * server2 = NULL ;
char * share2 = NULL ;
char * user1 = NULL ;
char * user2 = NULL ;
char * password1 = NULL ;
char * password2 = NULL ;
char * workgroup = NULL ;
char * path1 = NULL ;
char * path2 = NULL ;
char * targetpath1 = NULL ;
char * targetpath2 = NULL ;
struct cli_state * targetcli1 = NULL ;
struct cli_state * targetcli2 = NULL ;
SMBCSRV * srv = NULL ;
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2011-07-03 22:53:55 +04:00
NTSTATUS status ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! ocontext | | ! ncontext | |
2008-02-29 21:34:35 +03:00
! ocontext - > internal - > initialized | |
! ncontext - > internal - > initialized ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = EINVAL ; /* Best I can think of ... */
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! oname | | ! nname ) {
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
DEBUG ( 4 , ( " smbc_rename(%s,%s) \n " , oname , nname ) ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( SMBC_parse_path ( frame ,
2008-03-02 04:44:21 +03:00
ocontext ,
oname ,
& workgroup ,
& server1 ,
& share1 ,
& path1 ,
& user1 ,
& password1 ,
NULL ) ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! user1 | | user1 [ 0 ] = = ( char ) 0 ) {
2008-03-04 02:13:33 +03:00
user1 = talloc_strdup ( frame , smbc_getUser ( ocontext ) ) ;
2008-02-28 19:23:20 +03:00
if ( ! user1 ) {
errno = ENOMEM ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( SMBC_parse_path ( frame ,
2008-03-02 04:44:21 +03:00
ncontext ,
nname ,
NULL ,
& server2 ,
& share2 ,
& path2 ,
& user2 ,
& password2 ,
NULL ) ) {
2008-02-28 19:23:20 +03:00
errno = EINVAL ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( ! user2 | | user2 [ 0 ] = = ( char ) 0 ) {
2008-03-04 02:13:33 +03:00
user2 = talloc_strdup ( frame , smbc_getUser ( ncontext ) ) ;
2008-02-28 19:23:20 +03:00
if ( ! user2 ) {
errno = ENOMEM ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( strcmp ( server1 , server2 ) | | strcmp ( share1 , share2 ) | |
strcmp ( user1 , user2 ) ) {
/* Can't rename across file systems, or users?? */
errno = EXDEV ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
srv = SMBC_server ( frame , ocontext , True ,
server1 , share1 , & workgroup , & user1 , & password1 ) ;
if ( ! srv ) {
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
2008-03-07 19:37:07 +03:00
2009-02-20 07:00:46 +03:00
/* set the credentials to make DFS work */
smbc_set_credentials_with_fallback ( ocontext ,
workgroup ,
user1 ,
password1 ) ;
2008-02-28 19:23:20 +03:00
/*d_printf(">>>rename: resolving %s\n", path1);*/
2011-07-03 22:53:55 +04:00
status = cli_resolve_path ( frame , " " , ocontext - > internal - > auth_info ,
srv - > cli , path1 , & targetcli1 , & targetpath1 ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-02-28 19:23:20 +03:00
d_printf ( " Could not resolve %s \n " , path1 ) ;
2009-03-28 01:02:46 +03:00
errno = ENOENT ;
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2009-02-20 07:00:46 +03:00
/* set the credentials to make DFS work */
smbc_set_credentials_with_fallback ( ncontext ,
workgroup ,
user2 ,
password2 ) ;
2008-02-28 19:23:20 +03:00
/*d_printf(">>>rename: resolved path as %s\n", targetpath1);*/
/*d_printf(">>>rename: resolving %s\n", path2);*/
2011-07-03 22:53:55 +04:00
status = cli_resolve_path ( frame , " " , ncontext - > internal - > auth_info ,
srv - > cli , path2 , & targetcli2 , & targetpath2 ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-02-28 19:23:20 +03:00
d_printf ( " Could not resolve %s \n " , path2 ) ;
2009-03-28 01:02:46 +03:00
errno = ENOENT ;
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return - 1 ;
}
/*d_printf(">>>rename: resolved path as %s\n", targetpath2);*/
2008-03-07 19:37:07 +03:00
2011-07-22 18:33:51 +04:00
if ( strcmp ( cli_state_remote_name ( targetcli1 ) , cli_state_remote_name ( targetcli2 ) ) | |
2008-02-28 19:23:20 +03:00
strcmp ( targetcli1 - > share , targetcli2 - > share ) )
{
/* can't rename across file systems */
errno = EXDEV ;
TALLOC_FREE ( frame ) ;
return - 1 ;
}
2008-03-07 19:37:07 +03:00
2009-04-29 03:43:16 +04:00
if ( ! NT_STATUS_IS_OK ( cli_rename ( targetcli1 , targetpath1 , targetpath2 ) ) ) {
2008-02-28 19:23:20 +03:00
int eno = SMBC_errno ( ocontext , targetcli1 ) ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
if ( eno ! = EEXIST | |
2011-04-29 07:23:14 +04:00
! NT_STATUS_IS_OK ( cli_unlink ( targetcli1 , targetpath2 , FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN ) ) | |
2009-04-29 03:43:16 +04:00
! NT_STATUS_IS_OK ( cli_rename ( targetcli1 , targetpath1 , targetpath2 ) ) ) {
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
errno = eno ;
TALLOC_FREE ( frame ) ;
return - 1 ;
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
}
}
2008-03-07 19:37:07 +03:00
2008-02-28 19:23:20 +03:00
TALLOC_FREE ( frame ) ;
return 0 ; /* Success */
}