2009-04-21 11:14:11 +02:00
/*
Unix SMB / CIFS implementation .
Samba utility functions
2011-08-10 15:15:18 +02:00
2009-04-21 11:14:11 +02:00
Copyright ( C ) Jelmer Vernooij < jelmer @ samba . org > 2008
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/>.
*/
2023-11-09 11:35:56 +01:00
# include "lib/replace/system/python.h"
2017-02-15 09:19:33 +01:00
# include "python/py3compat.h"
2009-04-21 11:14:11 +02:00
# include "librpc/gen_ndr/misc.h"
2017-02-15 09:19:33 +01:00
static PyObject * py_GUID_richcmp ( PyObject * py_self , PyObject * py_other , int op )
{
int ret ;
struct GUID * self = pytalloc_get_ptr ( py_self ) , * other ;
other = pytalloc_get_ptr ( py_other ) ;
if ( other = = NULL ) {
Py_INCREF ( Py_NotImplemented ) ;
return Py_NotImplemented ;
}
ret = GUID_compare ( self , other ) ;
switch ( op ) {
case Py_EQ : if ( ret = = 0 ) Py_RETURN_TRUE ; else Py_RETURN_FALSE ;
case Py_NE : if ( ret ! = 0 ) Py_RETURN_TRUE ; else Py_RETURN_FALSE ;
case Py_LT : if ( ret < 0 ) Py_RETURN_TRUE ; else Py_RETURN_FALSE ;
case Py_GT : if ( ret > 0 ) Py_RETURN_TRUE ; else Py_RETURN_FALSE ;
case Py_LE : if ( ret < = 0 ) Py_RETURN_TRUE ; else Py_RETURN_FALSE ;
case Py_GE : if ( ret > = 0 ) Py_RETURN_TRUE ; else Py_RETURN_FALSE ;
}
Py_INCREF ( Py_NotImplemented ) ;
return Py_NotImplemented ;
}
2009-04-21 11:14:11 +02:00
static PyObject * py_GUID_str ( PyObject * py_self )
{
2011-08-10 15:15:18 +02:00
struct GUID * self = pytalloc_get_ptr ( py_self ) ;
2021-04-16 09:15:43 +02:00
struct GUID_txt_buf buf ;
PyObject * ret = PyUnicode_FromString ( GUID_buf_string ( self , & buf ) ) ;
2009-04-21 11:14:11 +02:00
return ret ;
}
static PyObject * py_GUID_repr ( PyObject * py_self )
{
2011-08-10 15:15:18 +02:00
struct GUID * self = pytalloc_get_ptr ( py_self ) ;
2021-04-16 09:15:43 +02:00
struct GUID_txt_buf buf ;
PyObject * ret = PyUnicode_FromFormat (
" GUID('%s') " , GUID_buf_string ( self , & buf ) ) ;
2009-04-21 11:14:11 +02:00
return ret ;
}
static int py_GUID_init ( PyObject * self , PyObject * args , PyObject * kwargs )
{
2010-11-30 15:59:04 +11:00
PyObject * str = NULL ;
2009-04-21 11:14:11 +02:00
NTSTATUS status ;
2011-08-10 15:15:18 +02:00
struct GUID * guid = pytalloc_get_ptr ( self ) ;
2009-04-21 11:14:11 +02:00
const char * kwnames [ ] = { " str " , NULL } ;
2010-11-30 15:59:04 +11:00
if ( ! PyArg_ParseTupleAndKeywords ( args , kwargs , " |O " , discard_const_p ( char * , kwnames ) , & str ) )
2009-04-21 11:14:11 +02:00
return - 1 ;
if ( str ! = NULL ) {
2010-11-30 15:59:04 +11:00
DATA_BLOB guid_val ;
2017-02-15 09:19:33 +01:00
Py_ssize_t _size ;
2010-11-30 15:59:04 +11:00
2020-03-15 10:47:32 +13:00
if ( PyUnicode_Check ( str ) ) {
2018-02-28 16:25:55 +13:00
guid_val . data =
2018-11-12 18:19:51 +01:00
discard_const_p ( uint8_t ,
2019-06-07 11:44:48 +02:00
PyUnicode_AsUTF8AndSize ( str , & _size ) ) ;
2020-03-15 10:47:32 +13:00
} else if ( PyBytes_Check ( str ) ) {
2018-11-12 18:19:51 +01:00
guid_val . data =
discard_const_p ( uint8_t ,
PyBytes_AsString ( str ) ) ;
2018-02-28 16:25:55 +13:00
_size = PyBytes_Size ( str ) ;
2020-03-15 10:47:32 +13:00
} else {
PyErr_SetString ( PyExc_TypeError ,
" Expected a string or bytes argument to GUID() " ) ;
return - 1 ;
2018-02-28 16:25:55 +13:00
}
2017-02-15 09:19:33 +01:00
guid_val . length = _size ;
2010-11-30 15:59:04 +11:00
status = GUID_from_data_blob ( & guid_val , guid ) ;
2009-04-21 11:14:11 +02:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
return - 1 ;
}
}
return 0 ;
}
static void py_GUID_patch ( PyTypeObject * type )
{
type - > tp_init = py_GUID_init ;
type - > tp_str = py_GUID_str ;
type - > tp_repr = py_GUID_repr ;
2017-02-15 09:19:33 +01:00
type - > tp_richcompare = py_GUID_richcmp ;
2009-04-21 11:14:11 +02:00
}
# define PY_GUID_PATCH py_GUID_patch
2009-04-21 11:53:00 +02:00
static int py_policy_handle_init ( PyObject * self , PyObject * args , PyObject * kwargs )
{
char * str = NULL ;
NTSTATUS status ;
2011-08-10 15:15:18 +02:00
struct policy_handle * handle = pytalloc_get_ptr ( self ) ;
2009-04-21 11:53:00 +02:00
const char * kwnames [ ] = { " uuid " , " type " , NULL } ;
2019-02-07 17:11:41 +13:00
if ( ! PyArg_ParseTupleAndKeywords ( args , kwargs , " |sI " , discard_const_p ( char * , kwnames ) , & str , & handle - > handle_type ) )
2009-04-21 11:53:00 +02:00
return - 1 ;
if ( str ! = NULL ) {
status = GUID_from_string ( str , & handle - > uuid ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
return - 1 ;
}
}
return 0 ;
}
static PyObject * py_policy_handle_repr ( PyObject * py_self )
{
2011-08-10 15:15:18 +02:00
struct policy_handle * self = pytalloc_get_ptr ( py_self ) ;
2021-04-16 09:15:43 +02:00
struct GUID_txt_buf buf ;
PyObject * ret = PyUnicode_FromFormat (
" policy_handle(%d, '%s') " ,
self - > handle_type ,
GUID_buf_string ( & self - > uuid , & buf ) ) ;
2009-04-21 12:06:04 +02:00
return ret ;
}
static PyObject * py_policy_handle_str ( PyObject * py_self )
{
2011-08-10 15:15:18 +02:00
struct policy_handle * self = pytalloc_get_ptr ( py_self ) ;
2021-04-16 09:15:43 +02:00
struct GUID_txt_buf buf ;
PyObject * ret = PyUnicode_FromFormat (
" %d, %s " ,
self - > handle_type ,
GUID_buf_string ( & self - > uuid , & buf ) ) ;
2009-04-21 11:53:00 +02:00
return ret ;
}
static void py_policy_handle_patch ( PyTypeObject * type )
{
type - > tp_init = py_policy_handle_init ;
type - > tp_repr = py_policy_handle_repr ;
2009-04-21 12:06:04 +02:00
type - > tp_str = py_policy_handle_str ;
2009-04-21 11:53:00 +02:00
}
# define PY_POLICY_HANDLE_PATCH py_policy_handle_patch