2008-12-21 07:29:23 +03:00
/*
Unix SMB / CIFS implementation .
Copyright ( C ) Jelmer Vernooij < jelmer @ samba . org > 2007
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/>.
*/
2009-01-08 14:20:20 +03:00
# include <Python.h>
2009-10-23 09:23:01 +04:00
# include "includes.h"
2008-12-21 07:29:23 +03:00
# include "pycredentials.h"
# include "param/param.h"
# include "lib/cmdline/credentials.h"
# include "librpc/gen_ndr/samr.h" /* for struct samr_Password */
# include "libcli/util/pyerrors.h"
2008-12-22 06:38:57 +03:00
# include "param/pyparam.h"
2010-02-20 03:44:41 +03:00
# include <tevent.h>
2008-12-21 07:29:23 +03:00
static PyObject * PyString_FromStringOrNULL ( const char * str )
{
if ( str = = NULL )
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 07:29:23 +03:00
return PyString_FromString ( str ) ;
}
static PyObject * py_creds_new ( PyTypeObject * type , PyObject * args , PyObject * kwargs )
{
2009-06-17 21:07:22 +04:00
py_talloc_Object * ret = ( py_talloc_Object * ) type - > tp_alloc ( type , 0 ) ;
if ( ret = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
2009-06-17 21:00:31 +04:00
ret - > talloc_ctx = talloc_new ( NULL ) ;
if ( ret - > talloc_ctx = = NULL ) {
2009-06-17 21:07:22 +04:00
PyErr_NoMemory ( ) ;
2009-06-17 21:00:31 +04:00
return NULL ;
}
ret - > ptr = cli_credentials_init ( ret - > talloc_ctx ) ;
return ( PyObject * ) ret ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_get_username ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyString_FromStringOrNULL ( cli_credentials_get_username ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_username ( py_talloc_Object * self , PyObject * args )
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & obt ) )
return NULL ;
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_set_username ( PyCredentials_AsCliCredentials ( self ) , newval , obt ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_get_password ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyString_FromStringOrNULL ( cli_credentials_get_password ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_password ( py_talloc_Object * self , PyObject * args )
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & obt ) )
return NULL ;
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_set_password ( PyCredentials_AsCliCredentials ( self ) , newval , obt ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_get_domain ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyString_FromStringOrNULL ( cli_credentials_get_domain ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_domain ( py_talloc_Object * self , PyObject * args )
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & obt ) )
return NULL ;
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_set_domain ( PyCredentials_AsCliCredentials ( self ) , newval , obt ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_get_realm ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyString_FromStringOrNULL ( cli_credentials_get_realm ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_realm ( py_talloc_Object * self , PyObject * args )
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & obt ) )
return NULL ;
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_set_realm ( PyCredentials_AsCliCredentials ( self ) , newval , obt ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_get_bind_dn ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyString_FromStringOrNULL ( cli_credentials_get_bind_dn ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_bind_dn ( py_talloc_Object * self , PyObject * args )
{
char * newval ;
if ( ! PyArg_ParseTuple ( args , " s " , & newval ) )
return NULL ;
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_set_bind_dn ( PyCredentials_AsCliCredentials ( self ) , newval ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_get_workstation ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyString_FromStringOrNULL ( cli_credentials_get_workstation ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_workstation ( py_talloc_Object * self , PyObject * args )
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & obt ) )
return NULL ;
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_set_workstation ( PyCredentials_AsCliCredentials ( self ) , newval , obt ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_is_anonymous ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_is_anonymous ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_anonymous ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
cli_credentials_set_anonymous ( PyCredentials_AsCliCredentials ( self ) ) ;
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_authentication_requested ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_authentication_requested ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_wrong_password ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_wrong_password ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_cmdline_callbacks ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
return PyBool_FromLong ( cli_credentials_set_cmdline_callbacks ( PyCredentials_AsCliCredentials ( self ) ) ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_parse_string ( py_talloc_Object * self , PyObject * args )
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & obt ) )
return NULL ;
2009-02-05 13:13:08 +03:00
cli_credentials_parse_string ( PyCredentials_AsCliCredentials ( self ) , newval , obt ) ;
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_get_nt_hash ( py_talloc_Object * self )
{
2009-02-05 13:13:08 +03:00
const struct samr_Password * ntpw = cli_credentials_get_nt_hash ( PyCredentials_AsCliCredentials ( self ) , self - > ptr ) ;
2008-12-21 07:29:23 +03:00
2009-02-05 13:13:08 +03:00
return PyString_FromStringAndSize ( discard_const_p ( char , ntpw - > hash ) , 16 ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_kerberos_state ( py_talloc_Object * self , PyObject * args )
{
int state ;
if ( ! PyArg_ParseTuple ( args , " i " , & state ) )
return NULL ;
2009-02-05 13:13:08 +03:00
cli_credentials_set_kerberos_state ( PyCredentials_AsCliCredentials ( self ) , state ) ;
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_guess ( py_talloc_Object * self , PyObject * args )
{
PyObject * py_lp_ctx = Py_None ;
struct loadparm_context * lp_ctx ;
if ( ! PyArg_ParseTuple ( args , " |O " , & py_lp_ctx ) )
return NULL ;
lp_ctx = lp_from_py_object ( py_lp_ctx ) ;
if ( lp_ctx = = NULL )
return NULL ;
2009-02-05 13:13:08 +03:00
cli_credentials_guess ( PyCredentials_AsCliCredentials ( self ) , lp_ctx ) ;
2008-12-21 07:29:23 +03:00
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_set_machine_account ( py_talloc_Object * self , PyObject * args )
{
PyObject * py_lp_ctx = Py_None ;
struct loadparm_context * lp_ctx ;
NTSTATUS status ;
if ( ! PyArg_ParseTuple ( args , " |O " , & py_lp_ctx ) )
return NULL ;
lp_ctx = lp_from_py_object ( py_lp_ctx ) ;
if ( lp_ctx = = NULL )
return NULL ;
2009-02-05 13:13:08 +03:00
status = cli_credentials_set_machine_account ( PyCredentials_AsCliCredentials ( self ) , lp_ctx ) ;
2008-12-21 07:29:23 +03:00
PyErr_NTSTATUS_IS_ERR_RAISE ( status ) ;
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 07:29:23 +03:00
}
2010-02-20 03:44:41 +03:00
PyObject * PyCredentialCacheContainer_from_ccache_container ( struct ccache_container * ccc )
{
PyCredentialCacheContainerObject * py_ret ;
if ( ccc = = NULL ) {
Py_RETURN_NONE ;
}
py_ret = ( PyCredentialCacheContainerObject * ) PyCredentialCacheContainer . tp_alloc ( & PyCredentialCacheContainer , 0 ) ;
if ( py_ret = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
py_ret - > mem_ctx = talloc_new ( NULL ) ;
py_ret - > ccc = talloc_reference ( py_ret - > mem_ctx , ccc ) ;
return ( PyObject * ) py_ret ;
}
static PyObject * py_creds_get_named_ccache ( py_talloc_Object * self , PyObject * args )
{
PyObject * py_lp_ctx = Py_None ;
char * ccache_name ;
struct loadparm_context * lp_ctx ;
struct ccache_container * ccc ;
struct tevent_context * event_ctx ;
int ret ;
2010-02-25 08:16:33 +03:00
const char * error_string ;
2010-02-20 03:44:41 +03:00
if ( ! PyArg_ParseTuple ( args , " |Os " , & py_lp_ctx , & ccache_name ) )
return NULL ;
lp_ctx = lp_from_py_object ( py_lp_ctx ) ;
if ( lp_ctx = = NULL )
return NULL ;
event_ctx = tevent_context_init ( NULL ) ;
2010-02-25 08:16:33 +03:00
ret = cli_credentials_get_named_ccache ( PyCredentials_AsCliCredentials ( self ) , event_ctx , lp_ctx ,
ccache_name , & ccc , & error_string ) ;
2010-02-20 03:44:41 +03:00
if ( ret = = 0 ) {
talloc_steal ( ccc , event_ctx ) ;
return PyCredentialCacheContainer_from_ccache_container ( ccc ) ;
}
2010-04-04 02:22:29 +04:00
PyErr_SetString ( PyExc_RuntimeError , error_string ) ;
2010-02-25 08:16:33 +03:00
talloc_free ( event_ctx ) ;
return NULL ;
2010-02-20 03:44:41 +03:00
}
2010-02-25 12:22:52 +03:00
static PyObject * py_creds_set_gensec_features ( py_talloc_Object * self , PyObject * args )
{
unsigned int gensec_features ;
if ( ! PyArg_ParseTuple ( args , " I " , & gensec_features ) )
return NULL ;
cli_credentials_set_gensec_features ( PyCredentials_AsCliCredentials ( self ) , gensec_features ) ;
Py_RETURN_NONE ;
}
static PyObject * py_creds_get_gensec_features ( py_talloc_Object * self , PyObject * args )
{
unsigned int gensec_features ;
gensec_features = cli_credentials_get_gensec_features ( PyCredentials_AsCliCredentials ( self ) ) ;
return PyInt_FromLong ( gensec_features ) ;
}
2008-12-21 07:29:23 +03:00
static PyMethodDef py_creds_methods [ ] = {
{ " get_username " , ( PyCFunction ) py_creds_get_username , METH_NOARGS ,
" S.get_username() -> username \n Obtain username. " } ,
{ " set_username " , ( PyCFunction ) py_creds_set_username , METH_VARARGS ,
" S.set_username(name, obtained=CRED_SPECIFIED) -> None \n "
" Change username. " } ,
{ " get_password " , ( PyCFunction ) py_creds_get_password , METH_NOARGS ,
" S.get_password() -> password \n "
" Obtain password. " } ,
{ " set_password " , ( PyCFunction ) py_creds_set_password , METH_VARARGS ,
" S.set_password(password, obtained=CRED_SPECIFIED) -> None \n "
" Change password. " } ,
{ " get_domain " , ( PyCFunction ) py_creds_get_domain , METH_NOARGS ,
" S.get_domain() -> domain \n "
" Obtain domain name. " } ,
{ " set_domain " , ( PyCFunction ) py_creds_set_domain , METH_VARARGS ,
" S.set_domain(domain, obtained=CRED_SPECIFIED) -> None \n "
" Change domain name. " } ,
{ " get_realm " , ( PyCFunction ) py_creds_get_realm , METH_NOARGS ,
" S.get_realm() -> realm \n "
" Obtain realm name. " } ,
{ " set_realm " , ( PyCFunction ) py_creds_set_realm , METH_VARARGS ,
" S.set_realm(realm, obtained=CRED_SPECIFIED) -> None \n "
" Change realm name. " } ,
{ " get_bind_dn " , ( PyCFunction ) py_creds_get_bind_dn , METH_NOARGS ,
" S.get_bind_dn() -> bind dn \n "
" Obtain bind DN. " } ,
{ " set_bind_dn " , ( PyCFunction ) py_creds_set_bind_dn , METH_VARARGS ,
" S.set_bind_dn(bind_dn) -> None \n "
" Change bind DN. " } ,
{ " is_anonymous " , ( PyCFunction ) py_creds_is_anonymous , METH_NOARGS ,
NULL } ,
{ " set_anonymous " , ( PyCFunction ) py_creds_set_anonymous , METH_NOARGS ,
" S.set_anonymous() -> None \n "
" Use anonymous credentials. " } ,
{ " get_workstation " , ( PyCFunction ) py_creds_get_workstation , METH_NOARGS ,
NULL } ,
{ " set_workstation " , ( PyCFunction ) py_creds_set_workstation , METH_VARARGS ,
NULL } ,
{ " authentication_requested " , ( PyCFunction ) py_creds_authentication_requested , METH_NOARGS ,
NULL } ,
{ " wrong_password " , ( PyCFunction ) py_creds_wrong_password , METH_NOARGS ,
" S.wrong_password() -> bool \n "
" Indicate the returned password was incorrect. " } ,
{ " set_cmdline_callbacks " , ( PyCFunction ) py_creds_set_cmdline_callbacks , METH_NOARGS ,
" S.set_cmdline_callbacks() -> bool \n "
" Use command-line to obtain credentials not explicitly set. " } ,
{ " parse_string " , ( PyCFunction ) py_creds_parse_string , METH_VARARGS ,
" S.parse_string(text, obtained=CRED_SPECIFIED) -> None \n "
" Parse credentials string. " } ,
{ " get_nt_hash " , ( PyCFunction ) py_creds_get_nt_hash , METH_NOARGS ,
NULL } ,
{ " set_kerberos_state " , ( PyCFunction ) py_creds_set_kerberos_state , METH_VARARGS ,
NULL } ,
{ " guess " , ( PyCFunction ) py_creds_guess , METH_VARARGS , NULL } ,
{ " set_machine_account " , ( PyCFunction ) py_creds_set_machine_account , METH_VARARGS , NULL } ,
2010-02-20 03:44:41 +03:00
{ " get_named_ccache " , ( PyCFunction ) py_creds_get_named_ccache , METH_VARARGS , NULL } ,
2010-02-25 12:22:52 +03:00
{ " set_gensec_features " , ( PyCFunction ) py_creds_set_gensec_features , METH_VARARGS , NULL } ,
{ " get_gensec_features " , ( PyCFunction ) py_creds_get_gensec_features , METH_NOARGS , NULL } ,
2008-12-21 07:29:23 +03:00
{ NULL }
} ;
PyTypeObject PyCredentials = {
. tp_name = " Credentials " ,
. tp_basicsize = sizeof ( py_talloc_Object ) ,
. tp_dealloc = py_talloc_dealloc ,
. tp_new = py_creds_new ,
. tp_flags = Py_TPFLAGS_DEFAULT ,
. tp_methods = py_creds_methods ,
} ;
2010-02-20 03:44:41 +03:00
PyTypeObject PyCredentialCacheContainer = {
. tp_name = " CredentialCacheContainer " ,
. tp_basicsize = sizeof ( py_talloc_Object ) ,
. tp_dealloc = py_talloc_dealloc ,
. tp_flags = Py_TPFLAGS_DEFAULT ,
} ;
2008-12-21 07:29:23 +03:00
void initcredentials ( void )
{
PyObject * m ;
if ( PyType_Ready ( & PyCredentials ) < 0 )
return ;
2010-02-20 03:44:41 +03:00
if ( PyType_Ready ( & PyCredentialCacheContainer ) < 0 )
return ;
2008-12-21 07:29:23 +03:00
m = Py_InitModule3 ( " credentials " , NULL , " Credentials management. " ) ;
if ( m = = NULL )
return ;
PyModule_AddObject ( m , " AUTO_USE_KERBEROS " , PyInt_FromLong ( CRED_AUTO_USE_KERBEROS ) ) ;
PyModule_AddObject ( m , " DONT_USE_KERBEROS " , PyInt_FromLong ( CRED_DONT_USE_KERBEROS ) ) ;
PyModule_AddObject ( m , " MUST_USE_KERBEROS " , PyInt_FromLong ( CRED_MUST_USE_KERBEROS ) ) ;
Py_INCREF ( & PyCredentials ) ;
PyModule_AddObject ( m , " Credentials " , ( PyObject * ) & PyCredentials ) ;
2010-02-20 03:44:41 +03:00
Py_INCREF ( & PyCredentialCacheContainer ) ;
PyModule_AddObject ( m , " CredentialCacheContainer " , ( PyObject * ) & PyCredentialCacheContainer ) ;
2008-12-21 07:29:23 +03:00
}