2012-08-03 01:21:27 +04:00
/*
* Unix SMB / CIFS implementation .
2019-01-09 00:15:49 +03:00
*
* SMB client Python bindings used internally by Samba ( for things like
* samba - tool ) . These Python bindings may change without warning , and so
* should not be used outside of the Samba codebase .
2012-08-03 01:21:27 +04:00
*
* Copyright ( C ) Volker Lendecke 2012
*
* 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 .
*
* 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 , see < http : //www.gnu.org/licenses/>.
*/
# include <Python.h>
# include "includes.h"
2018-02-05 14:34:15 +03:00
# include "python/py3compat.h"
2019-05-02 21:40:23 +03:00
# include "python/modules.h"
2018-12-04 12:42:55 +03:00
# include "libcli/smb/smbXcli_base.h"
2012-08-03 01:21:27 +04:00
# include "libsmb/libsmb.h"
# include "libcli/security/security.h"
# include "system/select.h"
# include "source4/libcli/util/pyerrors.h"
# include "auth/credentials/pycredentials.h"
2012-08-20 16:38:42 +04:00
# include "trans2.h"
2018-12-05 05:08:09 +03:00
# include "libsmb/clirap.h"
2018-12-12 06:14:43 +03:00
# include "librpc/rpc/pyrpc_util.h"
2012-08-03 01:21:27 +04:00
2018-12-12 03:47:46 +03:00
# define LIST_ATTRIBUTE_MASK \
( FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN )
2018-12-12 06:14:43 +03:00
# define SECINFO_DEFAULT_FLAGS \
( SECINFO_OWNER | SECINFO_GROUP | \
SECINFO_DACL | SECINFO_PROTECTED_DACL | SECINFO_UNPROTECTED_DACL | \
SECINFO_SACL | SECINFO_PROTECTED_SACL | SECINFO_UNPROTECTED_SACL )
2012-08-03 01:21:27 +04:00
static PyTypeObject * get_pytype ( const char * module , const char * type )
{
PyObject * mod ;
PyTypeObject * result ;
mod = PyImport_ImportModule ( module ) ;
if ( mod = = NULL ) {
PyErr_Format ( PyExc_RuntimeError ,
" Unable to import %s to check type %s " ,
module , type ) ;
return NULL ;
}
result = ( PyTypeObject * ) PyObject_GetAttrString ( mod , type ) ;
Py_DECREF ( mod ) ;
if ( result = = NULL ) {
PyErr_Format ( PyExc_RuntimeError ,
" Unable to find type %s in module %s " ,
module , type ) ;
return NULL ;
}
return result ;
}
2012-09-22 21:38:46 +04:00
/*
2013-08-09 12:15:48 +04:00
* We ' re using " const char * const * " for keywords ,
2012-09-22 21:38:46 +04:00
* PyArg_ParseTupleAndKeywords expects a " char ** " . Confine the
* inevitable warnings to just one place .
*/
static int ParseTupleAndKeywords ( PyObject * args , PyObject * kw ,
2013-08-09 12:15:48 +04:00
const char * format , const char * const * keywords ,
2012-09-22 21:38:46 +04:00
. . . )
{
2013-08-09 12:15:48 +04:00
char * * _keywords = discard_const_p ( char * , keywords ) ;
2012-09-22 21:38:46 +04:00
va_list a ;
int ret ;
va_start ( a , keywords ) ;
ret = PyArg_VaParseTupleAndKeywords ( args , kw , format ,
2013-08-09 12:15:48 +04:00
_keywords , a ) ;
2012-09-22 21:38:46 +04:00
va_end ( a ) ;
return ret ;
}
2012-08-03 01:21:27 +04:00
struct py_cli_thread ;
2012-09-22 21:45:32 +04:00
struct py_cli_oplock_break {
uint16_t fnum ;
uint8_t level ;
} ;
2012-08-03 01:21:27 +04:00
struct py_cli_state {
PyObject_HEAD
struct cli_state * cli ;
2018-12-04 12:42:55 +03:00
bool is_smb1 ;
2012-08-03 01:21:27 +04:00
struct tevent_context * ev ;
2018-12-03 17:02:06 +03:00
int ( * req_wait_fn ) ( struct tevent_context * ev ,
struct tevent_req * req ) ;
2012-08-03 01:21:27 +04:00
struct py_cli_thread * thread_state ;
2012-09-22 21:45:32 +04:00
struct tevent_req * oplock_waiter ;
struct py_cli_oplock_break * oplock_breaks ;
struct py_tevent_cond * oplock_cond ;
2012-08-03 01:21:27 +04:00
} ;
2018-11-20 17:53:23 +03:00
# ifdef HAVE_PTHREAD
2012-08-03 01:21:27 +04:00
# include <pthread.h>
struct py_cli_thread {
/*
* Pipe to make the poll thread wake up in our destructor , so
* that we can exit and join the thread .
*/
int shutdown_pipe [ 2 ] ;
struct tevent_fd * shutdown_fde ;
bool do_shutdown ;
pthread_t id ;
/*
* Thread state to release the GIL during the poll ( 2 ) syscall
*/
PyThreadState * py_threadstate ;
} ;
static void * py_cli_state_poll_thread ( void * private_data )
{
struct py_cli_state * self = ( struct py_cli_state * ) private_data ;
struct py_cli_thread * t = self - > thread_state ;
PyGILState_STATE gstate ;
gstate = PyGILState_Ensure ( ) ;
while ( ! t - > do_shutdown ) {
int ret ;
ret = tevent_loop_once ( self - > ev ) ;
assert ( ret = = 0 ) ;
}
PyGILState_Release ( gstate ) ;
return NULL ;
}
static void py_cli_state_trace_callback ( enum tevent_trace_point point ,
void * private_data )
{
struct py_cli_state * self = ( struct py_cli_state * ) private_data ;
struct py_cli_thread * t = self - > thread_state ;
switch ( point ) {
case TEVENT_TRACE_BEFORE_WAIT :
assert ( t - > py_threadstate = = NULL ) ;
t - > py_threadstate = PyEval_SaveThread ( ) ;
break ;
case TEVENT_TRACE_AFTER_WAIT :
assert ( t - > py_threadstate ! = NULL ) ;
PyEval_RestoreThread ( t - > py_threadstate ) ;
t - > py_threadstate = NULL ;
break ;
default :
break ;
}
}
static void py_cli_state_shutdown_handler ( struct tevent_context * ev ,
struct tevent_fd * fde ,
uint16_t flags ,
void * private_data )
{
struct py_cli_state * self = ( struct py_cli_state * ) private_data ;
struct py_cli_thread * t = self - > thread_state ;
if ( ( flags & TEVENT_FD_READ ) = = 0 ) {
return ;
}
TALLOC_FREE ( t - > shutdown_fde ) ;
t - > do_shutdown = true ;
}
static int py_cli_thread_destructor ( struct py_cli_thread * t )
{
char c = 0 ;
ssize_t written ;
int ret ;
do {
/*
* This will wake the poll thread from the poll ( 2 )
*/
written = write ( t - > shutdown_pipe [ 1 ] , & c , 1 ) ;
} while ( ( written = = - 1 ) & & ( errno = = EINTR ) ) ;
/*
* Allow the poll thread to do its own cleanup under the GIL
*/
Py_BEGIN_ALLOW_THREADS
ret = pthread_join ( t - > id , NULL ) ;
Py_END_ALLOW_THREADS
assert ( ret = = 0 ) ;
if ( t - > shutdown_pipe [ 0 ] ! = - 1 ) {
close ( t - > shutdown_pipe [ 0 ] ) ;
t - > shutdown_pipe [ 0 ] = - 1 ;
}
if ( t - > shutdown_pipe [ 1 ] ! = - 1 ) {
close ( t - > shutdown_pipe [ 1 ] ) ;
t - > shutdown_pipe [ 1 ] = - 1 ;
}
return 0 ;
}
2018-12-03 17:02:06 +03:00
static int py_tevent_cond_req_wait ( struct tevent_context * ev ,
struct tevent_req * req ) ;
static bool py_cli_state_setup_mt_ev ( struct py_cli_state * self )
2012-08-03 01:21:27 +04:00
{
2012-09-16 02:15:21 +04:00
struct py_cli_thread * t = NULL ;
2012-08-03 01:21:27 +04:00
int ret ;
self - > ev = tevent_context_init_byname ( NULL , " poll_mt " ) ;
if ( self - > ev = = NULL ) {
goto fail ;
}
2013-02-21 11:29:18 +04:00
samba_tevent_set_debug ( self - > ev , " pylibsmb_tevent_mt " ) ;
2012-08-03 01:21:27 +04:00
tevent_set_trace_callback ( self - > ev , py_cli_state_trace_callback , self ) ;
2018-12-03 17:02:06 +03:00
self - > req_wait_fn = py_tevent_cond_req_wait ;
2012-08-03 01:21:27 +04:00
self - > thread_state = talloc_zero ( NULL , struct py_cli_thread ) ;
if ( self - > thread_state = = NULL ) {
goto fail ;
}
t = self - > thread_state ;
ret = pipe ( t - > shutdown_pipe ) ;
if ( ret = = - 1 ) {
goto fail ;
}
t - > shutdown_fde = tevent_add_fd (
self - > ev , self - > ev , t - > shutdown_pipe [ 0 ] , TEVENT_FD_READ ,
py_cli_state_shutdown_handler , self ) ;
if ( t - > shutdown_fde = = NULL ) {
goto fail ;
}
PyEval_InitThreads ( ) ;
ret = pthread_create ( & t - > id , NULL , py_cli_state_poll_thread , self ) ;
if ( ret ! = 0 ) {
goto fail ;
}
talloc_set_destructor ( self - > thread_state , py_cli_thread_destructor ) ;
return true ;
fail :
2012-09-16 02:15:21 +04:00
if ( t ! = NULL ) {
TALLOC_FREE ( t - > shutdown_fde ) ;
2012-08-03 01:21:27 +04:00
2012-09-16 02:15:21 +04:00
if ( t - > shutdown_pipe [ 0 ] ! = - 1 ) {
close ( t - > shutdown_pipe [ 0 ] ) ;
t - > shutdown_pipe [ 0 ] = - 1 ;
}
if ( t - > shutdown_pipe [ 1 ] ! = - 1 ) {
close ( t - > shutdown_pipe [ 1 ] ) ;
t - > shutdown_pipe [ 1 ] = - 1 ;
}
2012-08-03 01:21:27 +04:00
}
TALLOC_FREE ( self - > thread_state ) ;
TALLOC_FREE ( self - > ev ) ;
return false ;
}
struct py_tevent_cond {
pthread_mutex_t mutex ;
pthread_cond_t cond ;
bool is_done ;
} ;
static void py_tevent_signalme ( struct tevent_req * req ) ;
2012-09-21 23:05:04 +04:00
static int py_tevent_cond_wait ( struct py_tevent_cond * cond )
2012-08-03 01:21:27 +04:00
{
int ret , result ;
2012-09-21 23:05:04 +04:00
result = pthread_mutex_init ( & cond - > mutex , NULL ) ;
2012-08-03 01:21:27 +04:00
if ( result ! = 0 ) {
goto fail ;
}
2012-09-21 23:05:04 +04:00
result = pthread_cond_init ( & cond - > cond , NULL ) ;
2012-08-03 01:21:27 +04:00
if ( result ! = 0 ) {
goto fail_mutex ;
}
2012-09-21 23:05:04 +04:00
result = pthread_mutex_lock ( & cond - > mutex ) ;
2012-08-03 01:21:27 +04:00
if ( result ! = 0 ) {
goto fail_cond ;
}
2012-09-21 23:05:04 +04:00
cond - > is_done = false ;
while ( ! cond - > is_done ) {
2012-08-03 01:21:27 +04:00
Py_BEGIN_ALLOW_THREADS
2012-09-21 23:05:04 +04:00
result = pthread_cond_wait ( & cond - > cond , & cond - > mutex ) ;
2012-08-03 01:21:27 +04:00
Py_END_ALLOW_THREADS
if ( result ! = 0 ) {
goto fail_unlock ;
}
}
fail_unlock :
2012-09-21 23:05:04 +04:00
ret = pthread_mutex_unlock ( & cond - > mutex ) ;
2012-08-03 01:21:27 +04:00
assert ( ret = = 0 ) ;
fail_cond :
2012-09-21 23:05:04 +04:00
ret = pthread_cond_destroy ( & cond - > cond ) ;
2012-08-03 01:21:27 +04:00
assert ( ret = = 0 ) ;
fail_mutex :
2012-09-21 23:05:04 +04:00
ret = pthread_mutex_destroy ( & cond - > mutex ) ;
2012-08-03 01:21:27 +04:00
assert ( ret = = 0 ) ;
fail :
return result ;
}
2018-12-03 17:02:06 +03:00
static int py_tevent_cond_req_wait ( struct tevent_context * ev ,
struct tevent_req * req )
2012-09-21 23:05:04 +04:00
{
struct py_tevent_cond cond ;
tevent_req_set_callback ( req , py_tevent_signalme , & cond ) ;
return py_tevent_cond_wait ( & cond ) ;
}
2012-09-22 21:40:06 +04:00
static void py_tevent_cond_signal ( struct py_tevent_cond * cond )
2012-08-03 01:21:27 +04:00
{
int ret ;
ret = pthread_mutex_lock ( & cond - > mutex ) ;
assert ( ret = = 0 ) ;
cond - > is_done = true ;
ret = pthread_cond_signal ( & cond - > cond ) ;
assert ( ret = = 0 ) ;
ret = pthread_mutex_unlock ( & cond - > mutex ) ;
assert ( ret = = 0 ) ;
}
2012-09-22 21:40:06 +04:00
static void py_tevent_signalme ( struct tevent_req * req )
{
struct py_tevent_cond * cond = ( struct py_tevent_cond * )
tevent_req_callback_data_void ( req ) ;
py_tevent_cond_signal ( cond ) ;
}
2018-12-03 17:02:06 +03:00
# endif
static int py_tevent_req_wait ( struct tevent_context * ev ,
struct tevent_req * req ) ;
2012-08-03 01:21:27 +04:00
static bool py_cli_state_setup_ev ( struct py_cli_state * self )
{
self - > ev = tevent_context_init ( NULL ) ;
2013-02-21 11:29:18 +04:00
if ( self - > ev = = NULL ) {
return false ;
}
samba_tevent_set_debug ( self - > ev , " pylibsmb_tevent " ) ;
2018-12-03 17:02:06 +03:00
self - > req_wait_fn = py_tevent_req_wait ;
2013-02-21 11:29:18 +04:00
return true ;
2012-08-03 01:21:27 +04:00
}
static int py_tevent_req_wait ( struct tevent_context * ev ,
struct tevent_req * req )
{
while ( tevent_req_is_in_progress ( req ) ) {
int ret ;
ret = tevent_loop_once ( ev ) ;
if ( ret ! = 0 ) {
return ret ;
}
}
return 0 ;
}
2018-12-03 16:37:05 +03:00
static bool py_tevent_req_wait_exc ( struct py_cli_state * self ,
2012-09-16 22:39:26 +04:00
struct tevent_req * req )
{
int ret ;
if ( req = = NULL ) {
PyErr_NoMemory ( ) ;
return false ;
}
2018-12-03 17:02:06 +03:00
ret = self - > req_wait_fn ( self - > ev , req ) ;
2012-09-16 22:39:26 +04:00
if ( ret ! = 0 ) {
TALLOC_FREE ( req ) ;
errno = ret ;
PyErr_SetFromErrno ( PyExc_RuntimeError ) ;
return false ;
}
return true ;
}
2012-08-03 01:21:27 +04:00
static PyObject * py_cli_state_new ( PyTypeObject * type , PyObject * args ,
PyObject * kwds )
{
struct py_cli_state * self ;
self = ( struct py_cli_state * ) type - > tp_alloc ( type , 0 ) ;
if ( self = = NULL ) {
return NULL ;
}
self - > cli = NULL ;
2018-12-04 12:42:55 +03:00
self - > is_smb1 = false ;
2012-08-03 01:21:27 +04:00
self - > ev = NULL ;
self - > thread_state = NULL ;
2012-09-22 21:45:32 +04:00
self - > oplock_waiter = NULL ;
self - > oplock_cond = NULL ;
self - > oplock_breaks = NULL ;
2012-08-03 01:21:27 +04:00
return ( PyObject * ) self ;
}
2012-09-22 21:45:32 +04:00
static void py_cli_got_oplock_break ( struct tevent_req * req ) ;
2012-08-03 01:21:27 +04:00
static int py_cli_state_init ( struct py_cli_state * self , PyObject * args ,
PyObject * kwds )
{
NTSTATUS status ;
char * host , * share ;
2013-04-09 16:29:11 +04:00
PyObject * creds = NULL ;
2012-08-03 01:21:27 +04:00
struct cli_credentials * cli_creds ;
2018-12-12 00:25:35 +03:00
PyObject * py_lp = Py_None ;
2018-12-03 17:02:06 +03:00
PyObject * py_multi_threaded = Py_False ;
bool multi_threaded = false ;
2018-12-03 17:42:50 +03:00
PyObject * py_sign = Py_False ;
bool sign = false ;
int signing_state = SMB_SIGNING_DEFAULT ;
2018-12-04 12:40:18 +03:00
PyObject * py_force_smb1 = Py_False ;
bool force_smb1 = false ;
2013-04-09 14:15:02 +04:00
struct tevent_req * req ;
2012-08-03 01:21:27 +04:00
bool ret ;
2018-12-04 12:40:18 +03:00
int flags = 0 ;
2012-08-03 01:21:27 +04:00
static const char * kwlist [ ] = {
2018-12-13 02:40:49 +03:00
" host " , " share " , " lp " , " creds " ,
2018-12-04 12:40:18 +03:00
" multi_threaded " , " sign " , " force_smb1 " ,
NULL
2012-08-03 01:21:27 +04:00
} ;
PyTypeObject * py_type_Credentials = get_pytype (
" samba.credentials " , " Credentials " ) ;
if ( py_type_Credentials = = NULL ) {
return - 1 ;
}
2012-09-22 21:38:46 +04:00
ret = ParseTupleAndKeywords (
2018-12-12 00:25:35 +03:00
args , kwds , " ssO|O!OOO " , kwlist ,
& host , & share , & py_lp ,
2018-12-03 17:02:06 +03:00
py_type_Credentials , & creds ,
2018-12-03 17:42:50 +03:00
& py_multi_threaded ,
2018-12-04 12:40:18 +03:00
& py_sign ,
& py_force_smb1 ) ;
2012-08-03 01:21:27 +04:00
Py_DECREF ( py_type_Credentials ) ;
if ( ! ret ) {
return - 1 ;
}
2018-12-03 17:02:06 +03:00
multi_threaded = PyObject_IsTrue ( py_multi_threaded ) ;
2018-12-03 17:42:50 +03:00
sign = PyObject_IsTrue ( py_sign ) ;
2018-12-04 12:40:18 +03:00
force_smb1 = PyObject_IsTrue ( py_force_smb1 ) ;
2018-12-03 17:42:50 +03:00
if ( sign ) {
signing_state = SMB_SIGNING_REQUIRED ;
}
2018-12-03 17:02:06 +03:00
2018-12-04 12:40:18 +03:00
if ( force_smb1 ) {
/*
* As most of the cli_ * _send ( ) function
* don ' t support SMB2 ( it ' s only plugged
* into the sync wrapper functions currently )
* we have a way to force SMB1 .
*/
flags = CLI_FULL_CONNECTION_FORCE_SMB1 ;
}
2018-12-03 17:02:06 +03:00
if ( multi_threaded ) {
# ifdef HAVE_PTHREAD
ret = py_cli_state_setup_mt_ev ( self ) ;
if ( ! ret ) {
return - 1 ;
}
# else
PyErr_SetString ( PyExc_RuntimeError ,
" No PTHREAD support available " ) ;
2012-08-03 01:21:27 +04:00
return - 1 ;
2018-12-03 17:02:06 +03:00
# endif
2018-12-04 12:40:18 +03:00
if ( ! force_smb1 ) {
PyErr_SetString ( PyExc_RuntimeError ,
" multi_threaded is only possible on "
" SMB1 connections " ) ;
return - 1 ;
}
2018-12-03 17:02:06 +03:00
} else {
ret = py_cli_state_setup_ev ( self ) ;
if ( ! ret ) {
return - 1 ;
}
2012-08-03 01:21:27 +04:00
}
2013-04-09 16:29:11 +04:00
if ( creds = = NULL ) {
cli_creds = cli_credentials_init_anon ( NULL ) ;
} else {
cli_creds = PyCredentials_AsCliCredentials ( creds ) ;
2012-08-03 01:21:27 +04:00
}
2016-10-26 14:48:58 +03:00
req = cli_full_connection_creds_send (
2013-04-09 14:15:02 +04:00
NULL , self - > ev , " myname " , host , NULL , 0 , share , " ????? " ,
2018-12-03 17:42:50 +03:00
cli_creds , flags , signing_state ) ;
2018-12-03 16:37:05 +03:00
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
2013-08-09 12:15:48 +04:00
return - 1 ;
2013-04-09 14:15:02 +04:00
}
2016-10-26 14:48:58 +03:00
status = cli_full_connection_creds_recv ( req , & self - > cli ) ;
2013-04-09 14:15:02 +04:00
TALLOC_FREE ( req ) ;
2012-08-03 01:21:27 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
return - 1 ;
}
2012-09-22 21:45:32 +04:00
2018-12-04 12:42:55 +03:00
if ( smbXcli_conn_protocol ( self - > cli - > conn ) < PROTOCOL_SMB2_02 ) {
self - > is_smb1 = true ;
}
2018-12-04 02:32:58 +03:00
/*
* Oplocks require a multi threaded connection
*/
if ( self - > thread_state = = NULL ) {
return 0 ;
}
2012-09-22 21:45:32 +04:00
self - > oplock_waiter = cli_smb_oplock_break_waiter_send (
self - > ev , self - > ev , self - > cli ) ;
if ( self - > oplock_waiter = = NULL ) {
PyErr_NoMemory ( ) ;
return - 1 ;
}
tevent_req_set_callback ( self - > oplock_waiter , py_cli_got_oplock_break ,
self ) ;
2012-08-03 01:21:27 +04:00
return 0 ;
}
2012-09-22 21:45:32 +04:00
static void py_cli_got_oplock_break ( struct tevent_req * req )
{
struct py_cli_state * self = ( struct py_cli_state * )
tevent_req_callback_data_void ( req ) ;
struct py_cli_oplock_break b ;
struct py_cli_oplock_break * tmp ;
size_t num_breaks ;
NTSTATUS status ;
status = cli_smb_oplock_break_waiter_recv ( req , & b . fnum , & b . level ) ;
TALLOC_FREE ( req ) ;
self - > oplock_waiter = NULL ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return ;
}
num_breaks = talloc_array_length ( self - > oplock_breaks ) ;
tmp = talloc_realloc ( self - > ev , self - > oplock_breaks ,
struct py_cli_oplock_break , num_breaks + 1 ) ;
if ( tmp = = NULL ) {
return ;
}
self - > oplock_breaks = tmp ;
self - > oplock_breaks [ num_breaks ] = b ;
if ( self - > oplock_cond ! = NULL ) {
py_tevent_cond_signal ( self - > oplock_cond ) ;
}
self - > oplock_waiter = cli_smb_oplock_break_waiter_send (
self - > ev , self - > ev , self - > cli ) ;
if ( self - > oplock_waiter = = NULL ) {
return ;
}
tevent_req_set_callback ( self - > oplock_waiter , py_cli_got_oplock_break ,
self ) ;
}
static PyObject * py_cli_get_oplock_break ( struct py_cli_state * self ,
PyObject * args )
{
size_t num_oplock_breaks ;
if ( ! PyArg_ParseTuple ( args , " " ) ) {
return NULL ;
}
2018-12-04 02:32:58 +03:00
if ( self - > thread_state = = NULL ) {
PyErr_SetString ( PyExc_RuntimeError ,
" get_oplock_break() only possible on "
" a multi_threaded connection " ) ;
return NULL ;
}
2012-09-22 21:45:32 +04:00
if ( self - > oplock_cond ! = NULL ) {
errno = EBUSY ;
PyErr_SetFromErrno ( PyExc_RuntimeError ) ;
return NULL ;
}
num_oplock_breaks = talloc_array_length ( self - > oplock_breaks ) ;
if ( num_oplock_breaks = = 0 ) {
struct py_tevent_cond cond ;
int ret ;
self - > oplock_cond = & cond ;
ret = py_tevent_cond_wait ( & cond ) ;
self - > oplock_cond = NULL ;
if ( ret ! = 0 ) {
errno = ret ;
PyErr_SetFromErrno ( PyExc_RuntimeError ) ;
return NULL ;
}
}
num_oplock_breaks = talloc_array_length ( self - > oplock_breaks ) ;
if ( num_oplock_breaks > 0 ) {
PyObject * result ;
result = Py_BuildValue (
" {s:i,s:i} " ,
" fnum " , self - > oplock_breaks [ 0 ] . fnum ,
" level " , self - > oplock_breaks [ 0 ] . level ) ;
memmove ( & self - > oplock_breaks [ 0 ] , & self - > oplock_breaks [ 1 ] ,
sizeof ( self - > oplock_breaks [ 0 ] ) *
( num_oplock_breaks - 1 ) ) ;
self - > oplock_breaks = talloc_realloc (
NULL , self - > oplock_breaks , struct py_cli_oplock_break ,
num_oplock_breaks - 1 ) ;
return result ;
}
2012-09-24 05:50:22 +04:00
Py_RETURN_NONE ;
2012-09-22 21:45:32 +04:00
}
2012-08-03 01:21:27 +04:00
static void py_cli_state_dealloc ( struct py_cli_state * self )
{
TALLOC_FREE ( self - > thread_state ) ;
2012-09-22 21:45:32 +04:00
TALLOC_FREE ( self - > oplock_waiter ) ;
2012-08-03 01:21:27 +04:00
TALLOC_FREE ( self - > ev ) ;
if ( self - > cli ! = NULL ) {
cli_shutdown ( self - > cli ) ;
self - > cli = NULL ;
}
2018-02-05 14:34:15 +03:00
Py_TYPE ( self ) - > tp_free ( ( PyObject * ) self ) ;
2012-08-03 01:21:27 +04:00
}
2018-12-19 17:18:17 +03:00
static PyObject * py_cli_settimeout ( struct py_cli_state * self , PyObject * args )
{
unsigned int nmsecs = 0 ;
unsigned int omsecs = 0 ;
if ( ! PyArg_ParseTuple ( args , " I " , & nmsecs ) ) {
return NULL ;
}
omsecs = cli_set_timeout ( self - > cli , nmsecs ) ;
2020-03-15 00:36:59 +03:00
return PyLong_FromLong ( omsecs ) ;
2018-12-19 17:18:17 +03:00
}
2012-08-03 01:21:27 +04:00
static PyObject * py_cli_create ( struct py_cli_state * self , PyObject * args ,
PyObject * kwds )
{
char * fname ;
unsigned CreateFlags = 0 ;
unsigned DesiredAccess = FILE_GENERIC_READ ;
unsigned FileAttributes = 0 ;
unsigned ShareAccess = 0 ;
unsigned CreateDisposition = FILE_OPEN ;
unsigned CreateOptions = 0 ;
2018-12-07 18:38:57 +03:00
unsigned ImpersonationLevel = SMB2_IMPERSONATION_IMPERSONATION ;
2012-08-03 01:21:27 +04:00
unsigned SecurityFlags = 0 ;
uint16_t fnum ;
struct tevent_req * req ;
NTSTATUS status ;
static const char * kwlist [ ] = {
" Name " , " CreateFlags " , " DesiredAccess " , " FileAttributes " ,
" ShareAccess " , " CreateDisposition " , " CreateOptions " ,
2018-12-07 18:40:10 +03:00
" ImpersonationLevel " , " SecurityFlags " , NULL } ;
2012-08-03 01:21:27 +04:00
2012-09-22 21:38:46 +04:00
if ( ! ParseTupleAndKeywords (
2018-12-07 18:40:10 +03:00
args , kwds , " s|IIIIIIII " , kwlist ,
2012-08-03 01:21:27 +04:00
& fname , & CreateFlags , & DesiredAccess , & FileAttributes ,
& ShareAccess , & CreateDisposition , & CreateOptions ,
2018-12-07 18:40:10 +03:00
& ImpersonationLevel , & SecurityFlags ) ) {
2012-08-03 01:21:27 +04:00
return NULL ;
}
req = cli_ntcreate_send ( NULL , self - > ev , self - > cli , fname , CreateFlags ,
DesiredAccess , FileAttributes , ShareAccess ,
CreateDisposition , CreateOptions ,
2018-12-07 18:38:57 +03:00
ImpersonationLevel , SecurityFlags ) ;
2018-12-03 16:37:05 +03:00
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
2012-08-03 01:21:27 +04:00
return NULL ;
}
2014-05-09 07:55:57 +04:00
status = cli_ntcreate_recv ( req , & fnum , NULL ) ;
2012-08-03 01:21:27 +04:00
TALLOC_FREE ( req ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
}
return Py_BuildValue ( " I " , ( unsigned ) fnum ) ;
}
static PyObject * py_cli_close ( struct py_cli_state * self , PyObject * args )
{
struct tevent_req * req ;
int fnum ;
NTSTATUS status ;
if ( ! PyArg_ParseTuple ( args , " i " , & fnum ) ) {
return NULL ;
}
req = cli_close_send ( NULL , self - > ev , self - > cli , fnum ) ;
2018-12-03 16:37:05 +03:00
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
2012-08-03 01:21:27 +04:00
return NULL ;
}
status = cli_close_recv ( req ) ;
TALLOC_FREE ( req ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
}
2012-09-24 05:50:22 +04:00
Py_RETURN_NONE ;
2012-08-03 01:21:27 +04:00
}
2018-12-10 23:34:42 +03:00
struct push_state {
char * data ;
off_t nread ;
off_t total_data ;
} ;
/*
* cli_push ( ) helper to write a chunk of data to a remote file
*/
static size_t push_data ( uint8_t * buf , size_t n , void * priv )
{
struct push_state * state = ( struct push_state * ) priv ;
char * curr_ptr = NULL ;
off_t remaining ;
size_t copied_bytes ;
if ( state - > nread > = state - > total_data ) {
return 0 ;
}
curr_ptr = state - > data + state - > nread ;
remaining = state - > total_data - state - > nread ;
copied_bytes = MIN ( remaining , n ) ;
memcpy ( buf , curr_ptr , copied_bytes ) ;
state - > nread + = copied_bytes ;
return copied_bytes ;
}
/*
* Writes a file with the contents specified
*/
2019-05-02 21:40:23 +03:00
static PyObject * py_smb_savefile ( struct py_cli_state * self , PyObject * args )
2018-12-10 23:34:42 +03:00
{
uint16_t fnum ;
const char * filename = NULL ;
char * data = NULL ;
Py_ssize_t size = 0 ;
NTSTATUS status ;
struct tevent_req * req = NULL ;
struct push_state state ;
if ( ! PyArg_ParseTuple ( args , " s " PYARG_BYTES_LEN " :savefile " , & filename ,
& data , & size ) ) {
return NULL ;
}
/* create a new file handle for writing to */
req = cli_ntcreate_send ( NULL , self - > ev , self - > cli , filename , 0 ,
FILE_WRITE_DATA , FILE_ATTRIBUTE_NORMAL ,
FILE_SHARE_READ | FILE_SHARE_WRITE ,
FILE_OVERWRITE_IF , FILE_NON_DIRECTORY_FILE ,
SMB2_IMPERSONATION_IMPERSONATION , 0 ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NULL ;
}
status = cli_ntcreate_recv ( req , & fnum , NULL ) ;
TALLOC_FREE ( req ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
/* write the new file contents */
state . data = data ;
state . nread = 0 ;
state . total_data = size ;
req = cli_push_send ( NULL , self - > ev , self - > cli , fnum , 0 , 0 , 0 ,
push_data , & state ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NULL ;
}
status = cli_push_recv ( req ) ;
TALLOC_FREE ( req ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
/* close the file handle */
req = cli_close_send ( NULL , self - > ev , self - > cli , fnum ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NULL ;
}
status = cli_close_recv ( req ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
Py_RETURN_NONE ;
}
2012-08-03 01:21:27 +04:00
static PyObject * py_cli_write ( struct py_cli_state * self , PyObject * args ,
PyObject * kwds )
{
int fnum ;
unsigned mode = 0 ;
char * buf ;
2016-01-04 03:04:46 +03:00
Py_ssize_t buflen ;
2012-08-03 01:21:27 +04:00
unsigned long long offset ;
struct tevent_req * req ;
NTSTATUS status ;
size_t written ;
static const char * kwlist [ ] = {
" fnum " , " buffer " , " offset " , " mode " , NULL } ;
2012-09-22 21:38:46 +04:00
if ( ! ParseTupleAndKeywords (
2019-02-07 07:04:43 +03:00
args , kwds , " i " PYARG_BYTES_LEN " K|I " , kwlist ,
2012-08-03 01:21:27 +04:00
& fnum , & buf , & buflen , & offset , & mode ) ) {
return NULL ;
}
2018-12-07 16:28:04 +03:00
req = cli_write_send ( NULL , self - > ev , self - > cli , fnum , mode ,
( uint8_t * ) buf , offset , buflen ) ;
2018-12-03 16:37:05 +03:00
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
2012-08-03 01:21:27 +04:00
return NULL ;
}
2018-12-07 16:28:04 +03:00
status = cli_write_recv ( req , & written ) ;
2012-08-03 01:21:27 +04:00
TALLOC_FREE ( req ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
}
return Py_BuildValue ( " K " , ( unsigned long long ) written ) ;
}
2018-12-05 05:08:09 +03:00
/*
* Returns the size of the given file
*/
static NTSTATUS py_smb_filesize ( struct py_cli_state * self , uint16_t fnum ,
off_t * size )
{
NTSTATUS status ;
if ( self - > is_smb1 ) {
uint8_t * rdata = NULL ;
struct tevent_req * req = NULL ;
req = cli_qfileinfo_send ( NULL , self - > ev , self - > cli , fnum ,
SMB_QUERY_FILE_ALL_INFO , 68 ,
CLI_BUFFER_SIZE ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NT_STATUS_INTERNAL_ERROR ;
}
status = cli_qfileinfo_recv ( req , NULL , NULL , & rdata , NULL ) ;
if ( NT_STATUS_IS_OK ( status ) ) {
* size = IVAL2_TO_SMB_BIG_UINT ( rdata , 48 ) ;
}
TALLOC_FREE ( req ) ;
TALLOC_FREE ( rdata ) ;
} else {
status = cli_qfileinfo_basic ( self - > cli , fnum , NULL , size ,
NULL , NULL , NULL , NULL , NULL ) ;
}
return status ;
}
/*
* Loads the specified file ' s contents and returns it
*/
2019-05-02 21:40:23 +03:00
static PyObject * py_smb_loadfile ( struct py_cli_state * self , PyObject * args )
2018-12-05 05:08:09 +03:00
{
NTSTATUS status ;
const char * filename = NULL ;
struct tevent_req * req = NULL ;
uint16_t fnum ;
off_t size ;
char * buf = NULL ;
off_t nread = 0 ;
PyObject * result = NULL ;
if ( ! PyArg_ParseTuple ( args , " s:loadfile " , & filename ) ) {
return NULL ;
}
/* get a read file handle */
req = cli_ntcreate_send ( NULL , self - > ev , self - > cli , filename , 0 ,
2019-01-11 04:25:32 +03:00
FILE_READ_DATA | FILE_READ_ATTRIBUTES ,
FILE_ATTRIBUTE_NORMAL ,
2018-12-05 05:08:09 +03:00
FILE_SHARE_READ , FILE_OPEN , 0 ,
SMB2_IMPERSONATION_IMPERSONATION , 0 ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NULL ;
}
status = cli_ntcreate_recv ( req , & fnum , NULL ) ;
TALLOC_FREE ( req ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
/* get a buffer to hold the file contents */
status = py_smb_filesize ( self , fnum , & size ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
result = PyBytes_FromStringAndSize ( NULL , size ) ;
if ( result = = NULL ) {
return NULL ;
}
/* read the file contents */
buf = PyBytes_AS_STRING ( result ) ;
req = cli_pull_send ( NULL , self - > ev , self - > cli , fnum , 0 , size ,
2019-01-03 07:48:39 +03:00
size , cli_read_sink , & buf ) ;
2018-12-05 05:08:09 +03:00
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
Py_XDECREF ( result ) ;
return NULL ;
}
status = cli_pull_recv ( req , & nread ) ;
TALLOC_FREE ( req ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
Py_XDECREF ( result ) ;
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
}
/* close the file handle */
req = cli_close_send ( NULL , self - > ev , self - > cli , fnum ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
Py_XDECREF ( result ) ;
return NULL ;
}
status = cli_close_recv ( req ) ;
TALLOC_FREE ( req ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
Py_XDECREF ( result ) ;
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
}
/* sanity-check we read the expected number of bytes */
if ( nread > size ) {
Py_XDECREF ( result ) ;
PyErr_Format ( PyExc_IOError ,
" read invalid - got %zu requested %zu " ,
nread , size ) ;
return NULL ;
}
if ( nread < size ) {
if ( _PyBytes_Resize ( & result , nread ) < 0 ) {
return NULL ;
}
}
return result ;
}
2012-08-03 01:21:27 +04:00
static PyObject * py_cli_read ( struct py_cli_state * self , PyObject * args ,
PyObject * kwds )
{
int fnum ;
unsigned long long offset ;
unsigned size ;
struct tevent_req * req ;
NTSTATUS status ;
2018-12-07 16:28:04 +03:00
char * buf ;
size_t received ;
2012-08-03 01:21:27 +04:00
PyObject * result ;
static const char * kwlist [ ] = {
" fnum " , " offset " , " size " , NULL } ;
2012-09-22 21:38:46 +04:00
if ( ! ParseTupleAndKeywords (
2019-02-07 07:04:43 +03:00
args , kwds , " iKI " , kwlist , & fnum , & offset ,
2012-08-03 01:21:27 +04:00
& size ) ) {
return NULL ;
}
2018-12-07 16:28:04 +03:00
result = PyBytes_FromStringAndSize ( NULL , size ) ;
if ( result = = NULL ) {
return NULL ;
}
buf = PyBytes_AS_STRING ( result ) ;
req = cli_read_send ( NULL , self - > ev , self - > cli , fnum ,
buf , offset , size ) ;
2018-12-03 16:37:05 +03:00
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
2018-12-07 16:28:04 +03:00
Py_XDECREF ( result ) ;
2012-08-03 01:21:27 +04:00
return NULL ;
}
2018-12-07 16:28:04 +03:00
status = cli_read_recv ( req , & received ) ;
TALLOC_FREE ( req ) ;
2012-08-03 01:21:27 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2018-12-07 16:28:04 +03:00
Py_XDECREF ( result ) ;
2012-08-03 01:21:27 +04:00
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
}
2018-12-07 16:28:04 +03:00
if ( received > size ) {
Py_XDECREF ( result ) ;
PyErr_Format ( PyExc_IOError ,
" read invalid - got %zu requested %u " ,
received , size ) ;
return NULL ;
}
if ( received < size ) {
if ( _PyBytes_Resize ( & result , received ) < 0 ) {
return NULL ;
}
}
2012-08-03 01:21:27 +04:00
return result ;
}
static PyObject * py_cli_ftruncate ( struct py_cli_state * self , PyObject * args ,
PyObject * kwds )
{
int fnum ;
unsigned long long size ;
struct tevent_req * req ;
NTSTATUS status ;
static const char * kwlist [ ] = {
" fnum " , " size " , NULL } ;
2012-09-22 21:38:46 +04:00
if ( ! ParseTupleAndKeywords (
args , kwds , " IK " , kwlist , & fnum , & size ) ) {
2012-08-03 01:21:27 +04:00
return NULL ;
}
req = cli_ftruncate_send ( NULL , self - > ev , self - > cli , fnum , size ) ;
2018-12-03 16:37:05 +03:00
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
2012-08-03 01:21:27 +04:00
return NULL ;
}
status = cli_ftruncate_recv ( req ) ;
2012-08-20 15:43:41 +04:00
TALLOC_FREE ( req ) ;
2012-08-03 01:21:27 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
}
2012-09-24 05:50:22 +04:00
Py_RETURN_NONE ;
2012-08-03 01:21:27 +04:00
}
static PyObject * py_cli_delete_on_close ( struct py_cli_state * self ,
PyObject * args ,
PyObject * kwds )
{
unsigned fnum , flag ;
struct tevent_req * req ;
NTSTATUS status ;
static const char * kwlist [ ] = {
" fnum " , " flag " , NULL } ;
2012-09-22 21:38:46 +04:00
if ( ! ParseTupleAndKeywords (
args , kwds , " II " , kwlist , & fnum , & flag ) ) {
2012-08-03 01:21:27 +04:00
return NULL ;
}
req = cli_nt_delete_on_close_send ( NULL , self - > ev , self - > cli , fnum ,
flag ) ;
2018-12-03 16:37:05 +03:00
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
2012-08-03 01:21:27 +04:00
return NULL ;
}
status = cli_nt_delete_on_close_recv ( req ) ;
2012-08-20 15:43:41 +04:00
TALLOC_FREE ( req ) ;
2012-08-03 01:21:27 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
}
2012-09-24 05:50:22 +04:00
Py_RETURN_NONE ;
2012-08-03 01:21:27 +04:00
}
2018-12-04 05:43:53 +03:00
/*
* Helper to add directory listing entries to an overall Python list
*/
static NTSTATUS list_helper ( const char * mntpoint , struct file_info * finfo ,
const char * mask , void * state )
{
PyObject * result = ( PyObject * ) state ;
PyObject * file = NULL ;
2019-01-22 21:26:23 +03:00
PyObject * size = NULL ;
2018-12-04 05:43:53 +03:00
int ret ;
2018-12-04 06:03:35 +03:00
/* suppress '.' and '..' in the results we return */
if ( ISDOT ( finfo - > name ) | | ISDOTDOT ( finfo - > name ) ) {
return NT_STATUS_OK ;
}
2019-01-22 21:26:23 +03:00
size = PyLong_FromUnsignedLongLong ( finfo - > size ) ;
2018-12-04 06:25:10 +03:00
/*
* Build a dictionary representing the file info .
* Note : Windows does not always return short_name ( so it may be None )
*/
file = Py_BuildValue ( " {s:s,s:i,s:s,s:O,s:l} " ,
2018-12-04 05:43:53 +03:00
" name " , finfo - > name ,
2020-06-03 20:41:27 +03:00
" attrib " , ( int ) finfo - > attr ,
2018-12-04 06:25:10 +03:00
" short_name " , finfo - > short_name ,
2019-01-22 21:26:23 +03:00
" size " , size ,
2018-12-04 06:25:10 +03:00
" mtime " ,
convert_timespec_to_time_t ( finfo - > mtime_ts ) ) ;
2019-01-22 21:26:23 +03:00
Py_CLEAR ( size ) ;
2018-12-04 05:43:53 +03:00
if ( file = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
ret = PyList_Append ( result , file ) ;
2019-01-23 21:43:43 +03:00
Py_CLEAR ( file ) ;
2018-12-04 05:43:53 +03:00
if ( ret = = - 1 ) {
return NT_STATUS_INTERNAL_ERROR ;
}
return NT_STATUS_OK ;
}
2018-12-12 01:28:22 +03:00
static NTSTATUS do_listing ( struct py_cli_state * self ,
const char * base_dir , const char * user_mask ,
uint16_t attribute ,
NTSTATUS ( * callback_fn ) ( const char * ,
struct file_info * ,
const char * , void * ) ,
void * priv )
2012-08-20 16:38:42 +04:00
{
2018-12-12 01:28:22 +03:00
char * mask = NULL ;
2018-12-12 03:47:46 +03:00
unsigned int info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO ;
2018-12-12 01:28:22 +03:00
struct file_info * finfos = NULL ;
2018-12-12 02:08:24 +03:00
size_t i ;
size_t num_finfos = 0 ;
2018-12-12 01:28:22 +03:00
NTSTATUS status ;
2012-08-20 16:38:42 +04:00
2018-12-03 23:15:30 +03:00
if ( user_mask = = NULL ) {
mask = talloc_asprintf ( NULL , " %s \\ * " , base_dir ) ;
} else {
mask = talloc_asprintf ( NULL , " %s \\ %s " , base_dir , user_mask ) ;
}
2018-12-12 01:28:22 +03:00
2018-12-03 23:15:30 +03:00
if ( mask = = NULL ) {
2018-12-12 01:28:22 +03:00
return NT_STATUS_NO_MEMORY ;
2018-12-03 23:15:30 +03:00
}
dos_format ( mask ) ;
2018-12-12 02:08:24 +03:00
if ( self - > is_smb1 ) {
struct tevent_req * req = NULL ;
req = cli_list_send ( NULL , self - > ev , self - > cli , mask , attribute ,
info_level ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NT_STATUS_INTERNAL_ERROR ;
}
status = cli_list_recv ( req , NULL , & finfos , & num_finfos ) ;
TALLOC_FREE ( req ) ;
} else {
status = cli_list ( self - > cli , mask , attribute , callback_fn ,
priv ) ;
2012-08-20 16:38:42 +04:00
}
2018-12-03 23:15:30 +03:00
TALLOC_FREE ( mask ) ;
2012-08-20 16:38:42 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2018-12-12 01:28:22 +03:00
return status ;
}
2018-12-12 02:08:24 +03:00
/* invoke the callback for the async results (SMBv1 connections) */
2018-12-12 01:28:22 +03:00
for ( i = 0 ; i < num_finfos ; i + + ) {
status = callback_fn ( base_dir , & finfos [ i ] , user_mask ,
priv ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2018-12-12 02:23:42 +03:00
TALLOC_FREE ( finfos ) ;
2018-12-12 01:28:22 +03:00
return status ;
}
}
2018-12-12 02:23:42 +03:00
TALLOC_FREE ( finfos ) ;
2018-12-12 01:28:22 +03:00
return status ;
}
static PyObject * py_cli_list ( struct py_cli_state * self ,
PyObject * args ,
PyObject * kwds )
{
char * base_dir ;
char * user_mask = NULL ;
2018-12-12 03:47:46 +03:00
unsigned int attribute = LIST_ATTRIBUTE_MASK ;
2018-12-12 01:28:22 +03:00
NTSTATUS status ;
2019-01-17 00:52:24 +03:00
PyObject * result = NULL ;
2018-12-12 01:28:22 +03:00
const char * kwlist [ ] = { " directory " , " mask " , " attribs " , NULL } ;
2019-02-07 07:04:43 +03:00
if ( ! ParseTupleAndKeywords ( args , kwds , " z|sI:list " , kwlist ,
2018-12-12 01:28:22 +03:00
& base_dir , & user_mask , & attribute ) ) {
2012-08-20 16:38:42 +04:00
return NULL ;
}
result = Py_BuildValue ( " [] " ) ;
if ( result = = NULL ) {
return NULL ;
}
2018-12-12 01:28:22 +03:00
status = do_listing ( self , base_dir , user_mask , attribute ,
list_helper , result ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
Py_XDECREF ( result ) ;
PyErr_SetNTSTATUS ( status ) ;
return NULL ;
2012-08-20 16:38:42 +04:00
}
return result ;
}
2018-12-03 00:50:19 +03:00
/*
* Deletes a file
*/
static NTSTATUS unlink_file ( struct py_cli_state * self , const char * filename )
{
NTSTATUS status ;
2020-06-04 07:03:39 +03:00
uint32_t attrs = ( FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN ) ;
2020-05-26 10:01:57 +03:00
struct tevent_req * req = NULL ;
2018-12-03 00:50:19 +03:00
2020-05-26 10:01:57 +03:00
req = cli_unlink_send (
NULL , self - > ev , self - > cli , filename , attrs ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NT_STATUS_INTERNAL_ERROR ;
2018-12-03 00:50:19 +03:00
}
2020-05-26 10:01:57 +03:00
status = cli_unlink_recv ( req ) ;
TALLOC_FREE ( req ) ;
2018-12-03 00:50:19 +03:00
return status ;
}
static PyObject * py_smb_unlink ( struct py_cli_state * self , PyObject * args )
{
NTSTATUS status ;
const char * filename = NULL ;
if ( ! PyArg_ParseTuple ( args , " s:unlink " , & filename ) ) {
return NULL ;
}
status = unlink_file ( self , filename ) ;
PyErr_NTSTATUS_NOT_OK_RAISE ( status ) ;
Py_RETURN_NONE ;
}
2018-12-03 06:27:07 +03:00
/*
* Delete an empty directory
*/
static NTSTATUS remove_dir ( struct py_cli_state * self , const char * dirname )
{
NTSTATUS status ;
2020-05-26 10:01:57 +03:00
struct tevent_req * req = NULL ;
2018-12-03 06:27:07 +03:00
2020-05-26 10:01:57 +03:00
req = cli_rmdir_send ( NULL , self - > ev , self - > cli , dirname ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NT_STATUS_INTERNAL_ERROR ;
2018-12-03 06:27:07 +03:00
}
2020-05-26 10:01:57 +03:00
status = cli_rmdir_recv ( req ) ;
TALLOC_FREE ( req ) ;
2018-12-03 06:27:07 +03:00
return status ;
}
static PyObject * py_smb_rmdir ( struct py_cli_state * self , PyObject * args )
{
NTSTATUS status ;
2019-01-17 00:52:24 +03:00
const char * dirname = NULL ;
2018-12-03 06:27:07 +03:00
if ( ! PyArg_ParseTuple ( args , " s:rmdir " , & dirname ) ) {
return NULL ;
}
status = remove_dir ( self , dirname ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
Py_RETURN_NONE ;
}
/*
* Create a directory
*/
static PyObject * py_smb_mkdir ( struct py_cli_state * self , PyObject * args )
{
NTSTATUS status ;
2019-01-17 00:52:24 +03:00
const char * dirname = NULL ;
2020-05-26 10:01:57 +03:00
struct tevent_req * req = NULL ;
2018-12-03 06:27:07 +03:00
if ( ! PyArg_ParseTuple ( args , " s:mkdir " , & dirname ) ) {
return NULL ;
}
2020-05-26 10:01:57 +03:00
req = cli_mkdir_send ( NULL , self - > ev , self - > cli , dirname ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return NULL ;
2018-12-03 06:27:07 +03:00
}
2020-05-26 10:01:57 +03:00
status = cli_mkdir_recv ( req ) ;
TALLOC_FREE ( req ) ;
2018-12-03 06:27:07 +03:00
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
Py_RETURN_NONE ;
}
2018-12-03 06:33:19 +03:00
/*
* Checks existence of a directory
*/
static bool check_dir_path ( struct py_cli_state * self , const char * path )
{
NTSTATUS status ;
2020-05-26 10:01:57 +03:00
struct tevent_req * req = NULL ;
2018-12-03 06:33:19 +03:00
2020-05-26 10:01:57 +03:00
req = cli_chkpath_send ( NULL , self - > ev , self - > cli , path ) ;
if ( ! py_tevent_req_wait_exc ( self , req ) ) {
return false ;
2018-12-03 06:33:19 +03:00
}
2020-05-26 10:01:57 +03:00
status = cli_chkpath_recv ( req ) ;
TALLOC_FREE ( req ) ;
2018-12-03 06:33:19 +03:00
return NT_STATUS_IS_OK ( status ) ;
}
static PyObject * py_smb_chkpath ( struct py_cli_state * self , PyObject * args )
{
2019-01-17 00:52:24 +03:00
const char * path = NULL ;
2018-12-03 06:33:19 +03:00
bool dir_exists ;
if ( ! PyArg_ParseTuple ( args , " s:chkpath " , & path ) ) {
return NULL ;
}
dir_exists = check_dir_path ( self , path ) ;
return PyBool_FromLong ( dir_exists ) ;
}
2018-12-12 03:45:46 +03:00
struct deltree_state {
struct py_cli_state * self ;
const char * full_dirpath ;
} ;
static NTSTATUS delete_dir_tree ( struct py_cli_state * self ,
const char * dirpath ) ;
/*
* Deletes a single item in the directory tree . This could be either a file
* or a directory . This function gets invoked as a callback for every item in
* the given directory ' s listings .
*/
static NTSTATUS delete_tree_callback ( const char * mntpoint ,
struct file_info * finfo ,
const char * mask , void * priv )
{
char * filepath = NULL ;
struct deltree_state * state = priv ;
NTSTATUS status ;
/* skip '.' or '..' directory listings */
if ( ISDOT ( finfo - > name ) | | ISDOTDOT ( finfo - > name ) ) {
return NT_STATUS_OK ;
}
/* get the absolute filepath */
filepath = talloc_asprintf ( NULL , " %s \\ %s " , state - > full_dirpath ,
finfo - > name ) ;
if ( filepath = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2020-06-03 20:41:27 +03:00
if ( finfo - > attr & FILE_ATTRIBUTE_DIRECTORY ) {
2018-12-12 03:45:46 +03:00
/* recursively delete the sub-directory and its contents */
status = delete_dir_tree ( state - > self , filepath ) ;
} else {
status = unlink_file ( state - > self , filepath ) ;
}
TALLOC_FREE ( filepath ) ;
return status ;
}
/*
* Removes a directory and all its contents
*/
static NTSTATUS delete_dir_tree ( struct py_cli_state * self ,
const char * filepath )
{
NTSTATUS status ;
const char * mask = " * " ;
struct deltree_state state = { 0 } ;
/* go through the directory's contents, deleting each item */
state . self = self ;
state . full_dirpath = filepath ;
status = do_listing ( self , filepath , mask , LIST_ATTRIBUTE_MASK ,
delete_tree_callback , & state ) ;
/* remove the directory itself */
if ( NT_STATUS_IS_OK ( status ) ) {
status = remove_dir ( self , filepath ) ;
}
return status ;
}
static PyObject * py_smb_deltree ( struct py_cli_state * self , PyObject * args )
{
NTSTATUS status ;
const char * filepath = NULL ;
bool dir_exists ;
if ( ! PyArg_ParseTuple ( args , " s:deltree " , & filepath ) ) {
return NULL ;
}
/* check whether we're removing a directory or a file */
dir_exists = check_dir_path ( self , filepath ) ;
if ( dir_exists ) {
status = delete_dir_tree ( self , filepath ) ;
} else {
status = unlink_file ( self , filepath ) ;
}
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
Py_RETURN_NONE ;
}
2018-12-12 06:14:43 +03:00
/*
* Read ACL on a given file / directory as a security descriptor object
*/
static PyObject * py_smb_getacl ( struct py_cli_state * self , PyObject * args )
{
NTSTATUS status ;
const char * filename = NULL ;
unsigned int sinfo = SECINFO_DEFAULT_FLAGS ;
unsigned int access_mask = SEC_FLAG_MAXIMUM_ALLOWED ;
uint16_t fnum ;
struct security_descriptor * sd = NULL ;
/* there's no async version of cli_query_security_descriptor() */
if ( self - > thread_state ! = NULL ) {
PyErr_SetString ( PyExc_RuntimeError ,
" get_acl() is not supported on "
" a multi_threaded connection " ) ;
return NULL ;
}
if ( ! PyArg_ParseTuple ( args , " s|II:get_acl " , & filename , & sinfo ,
& access_mask ) ) {
return NULL ;
}
/* get a file handle with the desired access */
status = cli_ntcreate ( self - > cli , filename , 0 , access_mask , 0 ,
FILE_SHARE_READ | FILE_SHARE_WRITE ,
FILE_OPEN , 0x0 , 0x0 , & fnum , NULL ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
/* query the security descriptor for this file */
status = cli_query_security_descriptor ( self - > cli , fnum , sinfo ,
NULL , & sd ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
/* close the file handle and convert the SD to a python struct */
status = cli_close ( self - > cli , fnum ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
return py_return_ndr_struct ( " samba.dcerpc.security " , " descriptor " ,
sd , sd ) ;
}
2019-01-08 04:42:05 +03:00
/*
* Set ACL on file / directory using given security descriptor object
*/
static PyObject * py_smb_setacl ( struct py_cli_state * self , PyObject * args )
{
NTSTATUS status ;
char * filename = NULL ;
PyObject * py_sd = NULL ;
struct security_descriptor * sd = NULL ;
unsigned int sinfo = SECINFO_DEFAULT_FLAGS ;
uint16_t fnum ;
/* there's no async version of cli_set_security_descriptor() */
if ( self - > thread_state ! = NULL ) {
PyErr_SetString ( PyExc_RuntimeError ,
" set_acl() is not supported on "
" a multi_threaded connection " ) ;
return NULL ;
}
if ( ! PyArg_ParseTuple ( args , " sO|I:set_acl " , & filename , & py_sd ,
& sinfo ) ) {
return NULL ;
}
sd = pytalloc_get_type ( py_sd , struct security_descriptor ) ;
if ( ! sd ) {
PyErr_Format ( PyExc_TypeError ,
" Expected dcerpc.security.descriptor as argument, got %s " ,
2019-07-09 13:51:18 +03:00
pytalloc_get_name ( py_sd ) ) ;
2019-01-08 04:42:05 +03:00
return NULL ;
}
status = cli_ntcreate ( self - > cli , filename , 0 ,
SEC_FLAG_MAXIMUM_ALLOWED , 0 ,
FILE_SHARE_READ | FILE_SHARE_WRITE ,
FILE_OPEN , 0x0 , 0x0 , & fnum , NULL ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
status = cli_set_security_descriptor ( self - > cli , fnum , sinfo , sd ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
status = cli_close ( self - > cli , fnum ) ;
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
Py_RETURN_NONE ;
}
2012-08-03 01:21:27 +04:00
static PyMethodDef py_cli_state_methods [ ] = {
2018-12-19 17:18:17 +03:00
{ " settimeout " , ( PyCFunction ) py_cli_settimeout , METH_VARARGS ,
" settimeout(new_timeout_msecs) => return old_timeout_msecs " } ,
2019-05-02 21:40:23 +03:00
{ " create " , PY_DISCARD_FUNC_SIG ( PyCFunction , py_cli_create ) ,
METH_VARARGS | METH_KEYWORDS ,
2012-08-03 01:21:27 +04:00
" Open a file " } ,
{ " close " , ( PyCFunction ) py_cli_close , METH_VARARGS ,
" Close a file handle " } ,
2019-05-02 21:40:23 +03:00
{ " write " , PY_DISCARD_FUNC_SIG ( PyCFunction , py_cli_write ) ,
METH_VARARGS | METH_KEYWORDS ,
2012-08-03 01:21:27 +04:00
" Write to a file handle " } ,
2019-05-02 21:40:23 +03:00
{ " read " , PY_DISCARD_FUNC_SIG ( PyCFunction , py_cli_read ) ,
METH_VARARGS | METH_KEYWORDS ,
2012-08-03 01:21:27 +04:00
" Read from a file handle " } ,
2019-05-02 21:40:23 +03:00
{ " truncate " , PY_DISCARD_FUNC_SIG ( PyCFunction ,
py_cli_ftruncate ) ,
2012-08-03 01:21:27 +04:00
METH_VARARGS | METH_KEYWORDS ,
" Truncate a file " } ,
2019-05-02 21:40:23 +03:00
{ " delete_on_close " , PY_DISCARD_FUNC_SIG ( PyCFunction ,
py_cli_delete_on_close ) ,
2012-08-03 01:21:27 +04:00
METH_VARARGS | METH_KEYWORDS ,
" Set/Reset the delete on close flag " } ,
2019-05-02 21:40:23 +03:00
{ " list " , PY_DISCARD_FUNC_SIG ( PyCFunction , py_cli_list ) ,
METH_VARARGS | METH_KEYWORDS ,
2018-12-03 23:15:30 +03:00
" list(directory, mask='*', attribs=DEFAULT_ATTRS) -> "
" directory contents as a dictionary \n "
" \t \t DEFAULT_ATTRS: FILE_ATTRIBUTE_SYSTEM | "
" FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ARCHIVE \n \n "
2018-12-04 06:25:10 +03:00
" \t \t List contents of a directory. The keys are, \n "
" \t \t \t name: Long name of the directory item \n "
" \t \t \t short_name: Short name of the directory item \n "
" \t \t \t size: File size in bytes \n "
" \t \t \t attrib: Attributes \n "
" \t \t \t mtime: Modification time \n " } ,
2012-09-22 21:45:32 +04:00
{ " get_oplock_break " , ( PyCFunction ) py_cli_get_oplock_break ,
METH_VARARGS , " Wait for an oplock break " } ,
2018-12-03 00:50:19 +03:00
{ " unlink " , ( PyCFunction ) py_smb_unlink ,
METH_VARARGS ,
" unlink(path) -> None \n \n \t \t Delete a file. " } ,
2018-12-03 06:27:07 +03:00
{ " mkdir " , ( PyCFunction ) py_smb_mkdir , METH_VARARGS ,
" mkdir(path) -> None \n \n \t \t Create a directory. " } ,
{ " rmdir " , ( PyCFunction ) py_smb_rmdir , METH_VARARGS ,
" rmdir(path) -> None \n \n \t \t Delete a directory. " } ,
2018-12-03 06:33:19 +03:00
{ " chkpath " , ( PyCFunction ) py_smb_chkpath , METH_VARARGS ,
" chkpath(dir_path) -> True or False \n \n "
" \t \t Return true if directory exists, false otherwise. " } ,
2018-12-10 23:34:42 +03:00
{ " savefile " , ( PyCFunction ) py_smb_savefile , METH_VARARGS ,
" savefile(path, str) -> None \n \n "
" \t \t Write " PY_DESC_PY3_BYTES " str to file. " } ,
2018-12-05 05:08:09 +03:00
{ " loadfile " , ( PyCFunction ) py_smb_loadfile , METH_VARARGS ,
" loadfile(path) -> file contents as a " PY_DESC_PY3_BYTES
" \n \n \t \t Read contents of a file. " } ,
2018-12-12 03:45:46 +03:00
{ " deltree " , ( PyCFunction ) py_smb_deltree , METH_VARARGS ,
" deltree(path) -> None \n \n "
" \t \t Delete a directory and all its contents. " } ,
2018-12-12 06:14:43 +03:00
{ " get_acl " , ( PyCFunction ) py_smb_getacl , METH_VARARGS ,
" get_acl(path[, security_info=0]) -> security_descriptor object \n \n "
" \t \t Get security descriptor for file. " } ,
2019-01-08 04:42:05 +03:00
{ " set_acl " , ( PyCFunction ) py_smb_setacl , METH_VARARGS ,
" set_acl(path, security_descriptor[, security_info=0]) -> None \n \n "
" \t \t Set security descriptor for file. " } ,
2012-08-03 01:21:27 +04:00
{ NULL , NULL , 0 , NULL }
} ;
static PyTypeObject py_cli_state_type = {
2018-02-05 14:34:15 +03:00
PyVarObject_HEAD_INIT ( NULL , 0 )
2019-01-09 00:15:49 +03:00
. tp_name = " libsmb_samba_internal.Conn " ,
2012-08-03 01:21:27 +04:00
. tp_basicsize = sizeof ( struct py_cli_state ) ,
. tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE ,
. tp_doc = " libsmb connection " ,
. tp_new = py_cli_state_new ,
. tp_init = ( initproc ) py_cli_state_init ,
. tp_dealloc = ( destructor ) py_cli_state_dealloc ,
. tp_methods = py_cli_state_methods ,
} ;
static PyMethodDef py_libsmb_methods [ ] = {
2020-05-05 04:47:39 +03:00
{ 0 } ,
2012-08-03 01:21:27 +04:00
} ;
2019-01-09 00:15:49 +03:00
void initlibsmb_samba_internal ( void ) ;
2018-02-05 14:34:15 +03:00
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT ,
2019-01-09 00:15:49 +03:00
. m_name = " libsmb_samba_internal " ,
2018-02-05 14:34:15 +03:00
. m_doc = " libsmb wrapper " ,
. m_size = - 1 ,
. m_methods = py_libsmb_methods ,
} ;
2019-01-09 00:15:49 +03:00
MODULE_INIT_FUNC ( libsmb_samba_internal )
2012-08-03 01:21:27 +04:00
{
2018-02-05 14:34:15 +03:00
PyObject * m = NULL ;
2012-08-03 01:21:27 +04:00
talloc_stackframe ( ) ;
2018-02-05 14:34:15 +03:00
m = PyModule_Create ( & moduledef ) ;
if ( m = = NULL ) {
return m ;
}
2012-08-03 01:21:27 +04:00
if ( PyType_Ready ( & py_cli_state_type ) < 0 ) {
2018-02-05 14:34:15 +03:00
return NULL ;
2012-08-03 01:21:27 +04:00
}
Py_INCREF ( & py_cli_state_type ) ;
PyModule_AddObject ( m , " Conn " , ( PyObject * ) & py_cli_state_type ) ;
2018-12-14 00:30:38 +03:00
2020-03-15 00:36:59 +03:00
# define ADD_FLAGS(val) PyModule_AddObject(m, #val, PyLong_FromLong(val))
2018-12-14 00:30:38 +03:00
ADD_FLAGS ( FILE_ATTRIBUTE_READONLY ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_HIDDEN ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_SYSTEM ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_VOLUME ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_DIRECTORY ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_ARCHIVE ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_DEVICE ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_NORMAL ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_TEMPORARY ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_SPARSE ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_REPARSE_POINT ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_COMPRESSED ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_OFFLINE ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_NONINDEXED ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_ENCRYPTED ) ;
ADD_FLAGS ( FILE_ATTRIBUTE_ALL_MASK ) ;
2018-02-05 14:34:15 +03:00
return m ;
2012-08-03 01:21:27 +04:00
}