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>
2016-10-17 17:07:31 +03:00
# include "python/py3compat.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
2011-03-19 02:44:56 +03:00
void initcredentials ( void ) ;
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 ;
2016-10-17 17:07:31 +03:00
return PyStr_FromString ( str ) ;
2008-12-21 07:29:23 +03:00
}
static PyObject * py_creds_new ( PyTypeObject * type , PyObject * args , PyObject * kwargs )
{
2016-02-22 04:10:23 +03:00
return pytalloc_steal ( type , cli_credentials_init ( NULL ) ) ;
2008-12-21 07:29:23 +03:00
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_username ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_username ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
2011-08-08 16:21:42 +04:00
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & _obt ) ) {
2008-12-21 07:29:23 +03:00
return NULL ;
2011-08-08 16:21:42 +04:00
}
obt = _obt ;
2008-12-21 07:29:23 +03:00
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
}
2016-10-12 01:27:54 +03:00
static PyObject * py_creds_get_ntlm_username_domain ( PyObject * self , PyObject * unused )
{
TALLOC_CTX * frame = talloc_stackframe ( ) ;
const char * user = NULL ;
const char * domain = NULL ;
PyObject * ret = NULL ;
cli_credentials_get_ntlm_username_domain ( PyCredentials_AsCliCredentials ( self ) ,
frame , & user , & domain ) ;
ret = Py_BuildValue ( " (OO) " ,
PyString_FromStringOrNULL ( user ) ,
PyString_FromStringOrNULL ( domain ) ) ;
TALLOC_FREE ( frame ) ;
return ret ;
}
2017-03-22 06:40:40 +03:00
static PyObject * py_creds_get_ntlm_response ( PyObject * self , PyObject * args , PyObject * kwargs )
{
TALLOC_CTX * frame = talloc_stackframe ( ) ;
PyObject * ret = NULL ;
int flags ;
struct timeval tv_now ;
NTTIME server_timestamp ;
DATA_BLOB challenge = data_blob_null ;
DATA_BLOB target_info = data_blob_null ;
NTSTATUS status ;
DATA_BLOB lm_response = data_blob_null ;
DATA_BLOB nt_response = data_blob_null ;
DATA_BLOB lm_session_key = data_blob_null ;
DATA_BLOB nt_session_key = data_blob_null ;
const char * kwnames [ ] = { " flags " , " challenge " ,
2017-04-28 04:14:16 +03:00
" target_info " ,
2017-03-22 06:40:40 +03:00
NULL } ;
tv_now = timeval_current ( ) ;
server_timestamp = timeval_to_nttime ( & tv_now ) ;
2017-04-28 04:14:16 +03:00
if ( ! PyArg_ParseTupleAndKeywords ( args , kwargs , " is#|s# " ,
2017-03-22 06:40:40 +03:00
discard_const_p ( char * , kwnames ) ,
2017-04-28 04:14:16 +03:00
& flags ,
& challenge . data ,
& challenge . length ,
& target_info . data ,
& target_info . length ) ) {
2017-03-22 06:40:40 +03:00
return NULL ;
}
status = cli_credentials_get_ntlm_response ( PyCredentials_AsCliCredentials ( self ) ,
frame , & flags ,
challenge ,
& server_timestamp ,
target_info ,
& lm_response , & nt_response ,
& lm_session_key , & nt_session_key ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS ( status ) ;
TALLOC_FREE ( frame ) ;
return NULL ;
}
2017-05-03 17:57:07 +03:00
ret = Py_BuildValue ( " {sis " PYARG_BYTES_LEN " s " PYARG_BYTES_LEN
" s " PYARG_BYTES_LEN " s " PYARG_BYTES_LEN " } " ,
2017-03-22 06:40:40 +03:00
" flags " , flags ,
" lm_reponse " ,
( const char * ) lm_response . data , lm_response . length ,
" nt_response " ,
( const char * ) nt_response . data , nt_response . length ,
" lm_session_key " ,
( const char * ) lm_session_key . data , lm_session_key . length ,
" nt_session_key " ,
( const char * ) nt_session_key . data , nt_session_key . length ) ;
TALLOC_FREE ( frame ) ;
return ret ;
}
2016-10-12 01:27:54 +03:00
static PyObject * py_creds_get_principal ( PyObject * self , PyObject * unused )
{
TALLOC_CTX * frame = talloc_stackframe ( ) ;
PyObject * ret = PyString_FromStringOrNULL ( cli_credentials_get_principal ( PyCredentials_AsCliCredentials ( self ) , frame ) ) ;
TALLOC_FREE ( frame ) ;
return ret ;
}
static PyObject * py_creds_set_principal ( PyObject * self , PyObject * args )
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & _obt ) ) {
return NULL ;
}
obt = _obt ;
return PyBool_FromLong ( cli_credentials_set_principal ( PyCredentials_AsCliCredentials ( self ) , newval , obt ) ) ;
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_password ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_password ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
2011-08-08 16:21:42 +04:00
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & _obt ) ) {
2008-12-21 07:29:23 +03:00
return NULL ;
2011-08-08 16:21:42 +04:00
}
obt = _obt ;
2008-12-21 07:29:23 +03:00
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
}
2016-07-12 10:57:16 +03:00
static PyObject * py_creds_set_utf16_password ( PyObject * self , PyObject * args )
{
enum credentials_obtained obt = CRED_SPECIFIED ;
int _obt = obt ;
PyObject * newval = NULL ;
DATA_BLOB blob = data_blob_null ;
Py_ssize_t size = 0 ;
int result ;
bool ok ;
if ( ! PyArg_ParseTuple ( args , " O|i " , & newval , & _obt ) ) {
return NULL ;
}
obt = _obt ;
result = PyBytes_AsStringAndSize ( newval , ( char * * ) & blob . data , & size ) ;
if ( result ! = 0 ) {
PyErr_SetString ( PyExc_RuntimeError , " Failed to convert passed value to Bytes " ) ;
return NULL ;
}
blob . length = size ;
ok = cli_credentials_set_utf16_password ( PyCredentials_AsCliCredentials ( self ) ,
& blob , obt ) ;
return PyBool_FromLong ( ok ) ;
}
2016-07-12 09:14:36 +03:00
static PyObject * py_creds_get_old_password ( PyObject * self , PyObject * unused )
{
return PyString_FromStringOrNULL ( cli_credentials_get_old_password ( PyCredentials_AsCliCredentials ( self ) ) ) ;
}
static PyObject * py_creds_set_old_password ( PyObject * self , PyObject * args )
{
char * oldval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & oldval , & _obt ) ) {
return NULL ;
}
obt = _obt ;
return PyBool_FromLong ( cli_credentials_set_old_password ( PyCredentials_AsCliCredentials ( self ) , oldval , obt ) ) ;
}
2016-07-12 10:57:16 +03:00
static PyObject * py_creds_set_old_utf16_password ( PyObject * self , PyObject * args )
{
PyObject * oldval = NULL ;
DATA_BLOB blob = data_blob_null ;
Py_ssize_t size = 0 ;
int result ;
bool ok ;
if ( ! PyArg_ParseTuple ( args , " O " , & oldval ) ) {
return NULL ;
}
result = PyBytes_AsStringAndSize ( oldval , ( char * * ) & blob . data , & size ) ;
if ( result ! = 0 ) {
PyErr_SetString ( PyExc_RuntimeError , " Failed to convert passed value to Bytes " ) ;
return NULL ;
}
blob . length = size ;
ok = cli_credentials_set_old_utf16_password ( PyCredentials_AsCliCredentials ( self ) ,
& blob ) ;
return PyBool_FromLong ( ok ) ;
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_domain ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_domain ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
2011-08-08 16:21:42 +04:00
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & _obt ) ) {
2008-12-21 07:29:23 +03:00
return NULL ;
2011-08-08 16:21:42 +04:00
}
obt = _obt ;
2008-12-21 07:29:23 +03:00
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_realm ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_realm ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
2011-08-08 16:21:42 +04:00
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & _obt ) ) {
2008-12-21 07:29:23 +03:00
return NULL ;
2011-08-08 16:21:42 +04:00
}
obt = _obt ;
2008-12-21 07:29:23 +03:00
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_bind_dn ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_bind_dn ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_workstation ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_workstation ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
2011-08-08 16:21:42 +04:00
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & _obt ) ) {
2008-12-21 07:29:23 +03:00
return NULL ;
2011-08-08 16:21:42 +04:00
}
obt = _obt ;
2008-12-21 07:29:23 +03:00
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_is_anonymous ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_anonymous ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_authentication_requested ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_wrong_password ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_cmdline_callbacks ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_parse_string ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
2011-08-08 16:21:42 +04:00
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & _obt ) ) {
2008-12-21 07:29:23 +03:00
return NULL ;
2011-08-08 16:21:42 +04:00
}
obt = _obt ;
2008-12-21 07:29:23 +03:00
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
}
2016-12-15 12:06:25 +03:00
static PyObject * py_creds_parse_file ( PyObject * self , PyObject * args )
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s|i " , & newval , & _obt ) ) {
return NULL ;
}
obt = _obt ;
cli_credentials_parse_file ( PyCredentials_AsCliCredentials ( self ) , newval , obt ) ;
Py_RETURN_NONE ;
}
2016-12-14 12:02:10 +03:00
static PyObject * py_cli_credentials_set_password_will_be_nt_hash ( PyObject * self , PyObject * args )
{
struct cli_credentials * creds = PyCredentials_AsCliCredentials ( self ) ;
PyObject * py_val = NULL ;
bool val = false ;
if ( ! PyArg_ParseTuple ( args , " O! " , & PyBool_Type , & py_val ) ) {
return NULL ;
}
val = PyObject_IsTrue ( py_val ) ;
cli_credentials_set_password_will_be_nt_hash ( creds , val ) ;
Py_RETURN_NONE ;
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_nt_hash ( PyObject * self , PyObject * unused )
2008-12-21 07:29:23 +03:00
{
2016-02-22 04:10:23 +03:00
PyObject * ret ;
struct cli_credentials * creds = PyCredentials_AsCliCredentials ( self ) ;
struct samr_Password * ntpw = cli_credentials_get_nt_hash ( creds , creds ) ;
2008-12-21 07:29:23 +03:00
2016-10-17 17:07:31 +03:00
ret = PyBytes_FromStringAndSize ( discard_const_p ( char , ntpw - > hash ) , 16 ) ;
2016-02-22 04:10:23 +03:00
TALLOC_FREE ( ntpw ) ;
return ret ;
2008-12-21 07:29:23 +03:00
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_kerberos_state ( PyObject * self , PyObject * unused )
2015-12-01 03:17:18 +03:00
{
int state = cli_credentials_get_kerberos_state ( PyCredentials_AsCliCredentials ( self ) ) ;
return PyInt_FromLong ( state ) ;
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_kerberos_state ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
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
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_krb_forwardable ( PyObject * self , PyObject * args )
2010-09-16 08:12:57 +04:00
{
int state ;
if ( ! PyArg_ParseTuple ( args , " i " , & state ) )
return NULL ;
cli_credentials_set_krb_forwardable ( PyCredentials_AsCliCredentials ( self ) , state ) ;
Py_RETURN_NONE ;
}
2013-09-16 20:38:09 +04:00
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_forced_sasl_mech ( PyObject * self , PyObject * unused )
2013-09-16 20:38:09 +04:00
{
return PyString_FromStringOrNULL ( cli_credentials_get_forced_sasl_mech ( PyCredentials_AsCliCredentials ( self ) ) ) ;
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_forced_sasl_mech ( PyObject * self , PyObject * args )
2013-09-16 20:38:09 +04:00
{
char * newval ;
enum credentials_obtained obt = CRED_SPECIFIED ;
int _obt = obt ;
if ( ! PyArg_ParseTuple ( args , " s " , & newval ) ) {
return NULL ;
}
obt = _obt ;
cli_credentials_set_forced_sasl_mech ( PyCredentials_AsCliCredentials ( self ) , newval ) ;
Py_RETURN_NONE ;
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_guess ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
PyObject * py_lp_ctx = Py_None ;
struct loadparm_context * lp_ctx ;
2010-09-23 03:44:17 +04:00
TALLOC_CTX * mem_ctx ;
2010-03-02 00:23:45 +03:00
struct cli_credentials * creds ;
creds = PyCredentials_AsCliCredentials ( self ) ;
2008-12-21 07:29:23 +03:00
if ( ! PyArg_ParseTuple ( args , " |O " , & py_lp_ctx ) )
return NULL ;
2010-09-23 03:44:17 +04:00
mem_ctx = talloc_new ( NULL ) ;
if ( mem_ctx = = NULL ) {
PyErr_NoMemory ( ) ;
2008-12-21 07:29:23 +03:00
return NULL ;
2010-09-23 03:44:17 +04:00
}
lp_ctx = lpcfg_from_py_object ( mem_ctx , py_lp_ctx ) ;
if ( lp_ctx = = NULL ) {
talloc_free ( mem_ctx ) ;
return NULL ;
}
2008-12-21 07:29:23 +03:00
2010-03-02 00:23:45 +03:00
cli_credentials_guess ( creds , lp_ctx ) ;
2008-12-21 07:29:23 +03:00
2010-09-23 03:44:17 +04:00
talloc_free ( mem_ctx ) ;
2010-09-23 02:35:36 +04:00
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 07:29:23 +03:00
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_machine_account ( PyObject * self , PyObject * args )
2008-12-21 07:29:23 +03:00
{
PyObject * py_lp_ctx = Py_None ;
struct loadparm_context * lp_ctx ;
NTSTATUS status ;
2010-03-02 00:23:45 +03:00
struct cli_credentials * creds ;
2010-09-23 03:44:17 +04:00
TALLOC_CTX * mem_ctx ;
2010-03-02 00:23:45 +03:00
creds = PyCredentials_AsCliCredentials ( self ) ;
2008-12-21 07:29:23 +03:00
if ( ! PyArg_ParseTuple ( args , " |O " , & py_lp_ctx ) )
return NULL ;
2010-09-23 03:44:17 +04:00
mem_ctx = talloc_new ( NULL ) ;
if ( mem_ctx = = NULL ) {
PyErr_NoMemory ( ) ;
2008-12-21 07:29:23 +03:00
return NULL ;
2010-09-23 03:44:17 +04:00
}
lp_ctx = lpcfg_from_py_object ( mem_ctx , py_lp_ctx ) ;
if ( lp_ctx = = NULL ) {
talloc_free ( mem_ctx ) ;
return NULL ;
}
2008-12-21 07:29:23 +03:00
2010-03-02 00:23:45 +03:00
status = cli_credentials_set_machine_account ( creds , lp_ctx ) ;
2010-09-23 03:44:17 +04:00
talloc_free ( mem_ctx ) ;
2010-09-23 02:35:36 +04:00
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
}
2011-03-19 02:44:56 +03:00
static PyObject * PyCredentialCacheContainer_from_ccache_container ( struct ccache_container * ccc )
2010-02-20 03:44:41 +03:00
{
2016-02-29 06:26:08 +03:00
return pytalloc_reference ( & PyCredentialCacheContainer , ccc ) ;
2010-02-20 03:44:41 +03:00
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_named_ccache ( PyObject * self , PyObject * args )
2010-02-20 03:44:41 +03:00
{
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-03-02 00:23:45 +03:00
struct cli_credentials * creds ;
2010-09-23 03:44:17 +04:00
TALLOC_CTX * mem_ctx ;
2010-03-02 00:23:45 +03:00
creds = PyCredentials_AsCliCredentials ( self ) ;
2010-02-20 03:44:41 +03:00
if ( ! PyArg_ParseTuple ( args , " |Os " , & py_lp_ctx , & ccache_name ) )
return NULL ;
2010-09-23 03:44:17 +04:00
mem_ctx = talloc_new ( NULL ) ;
if ( mem_ctx = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
lp_ctx = lpcfg_from_py_object ( mem_ctx , py_lp_ctx ) ;
if ( lp_ctx = = NULL ) {
talloc_free ( mem_ctx ) ;
2010-02-20 03:44:41 +03:00
return NULL ;
2010-09-23 03:44:17 +04:00
}
2010-02-20 03:44:41 +03:00
2013-02-21 11:32:35 +04:00
event_ctx = samba_tevent_context_init ( mem_ctx ) ;
2010-02-20 03:44:41 +03:00
2010-03-02 00:23:45 +03:00
ret = cli_credentials_get_named_ccache ( creds , event_ctx , lp_ctx ,
2010-02-25 08:16:33 +03:00
ccache_name , & ccc , & error_string ) ;
2010-09-24 06:46:27 +04:00
talloc_unlink ( mem_ctx , lp_ctx ) ;
2010-02-20 03:44:41 +03:00
if ( ret = = 0 ) {
talloc_steal ( ccc , event_ctx ) ;
2010-09-23 03:44:17 +04:00
talloc_free ( mem_ctx ) ;
2010-02-20 03:44:41 +03:00
return PyCredentialCacheContainer_from_ccache_container ( ccc ) ;
}
2010-04-19 07:43:53 +04:00
PyErr_SetString ( PyExc_RuntimeError , error_string ? error_string : " NULL " ) ;
2010-02-25 08:16:33 +03:00
2010-09-23 03:44:17 +04:00
talloc_free ( mem_ctx ) ;
2010-02-25 08:16:33 +03:00
return NULL ;
2010-02-20 03:44:41 +03:00
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_set_gensec_features ( PyObject * self , PyObject * args )
2010-02-25 12:22:52 +03:00
{
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 ;
}
2016-02-22 04:10:23 +03:00
static PyObject * py_creds_get_gensec_features ( PyObject * self , PyObject * args )
2010-02-25 12:22:52 +03:00
{
unsigned int gensec_features ;
gensec_features = cli_credentials_get_gensec_features ( PyCredentials_AsCliCredentials ( self ) ) ;
return PyInt_FromLong ( gensec_features ) ;
}
2017-04-28 01:16:39 +03:00
static PyObject * py_creds_set_secure_channel_type ( PyObject * self , PyObject * args )
{
unsigned int channel_type ;
if ( ! PyArg_ParseTuple ( args , " I " , & channel_type ) )
return NULL ;
cli_credentials_set_secure_channel_type (
PyCredentials_AsCliCredentials ( self ) ,
channel_type ) ;
Py_RETURN_NONE ;
}
2010-02-25 12:22:52 +03:00
2008-12-21 07:29:23 +03:00
static PyMethodDef py_creds_methods [ ] = {
2016-02-22 04:10:23 +03:00
{ " get_username " , py_creds_get_username , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
" S.get_username() -> username \n Obtain username. " } ,
2016-02-22 04:10:23 +03:00
{ " set_username " , py_creds_set_username , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.set_username(name[, credentials.SPECIFIED]) -> None \n "
2008-12-21 07:29:23 +03:00
" Change username. " } ,
2016-10-12 01:27:54 +03:00
{ " get_principal " , py_creds_get_principal , METH_NOARGS ,
" S.get_principal() -> user@realm \n Obtain user principal. " } ,
{ " set_principal " , py_creds_set_principal , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.set_principal(name[, credentials.SPECIFIED]) -> None \n "
2016-10-12 01:27:54 +03:00
" Change principal. " } ,
2016-02-22 04:10:23 +03:00
{ " get_password " , py_creds_get_password , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
" S.get_password() -> password \n "
" Obtain password. " } ,
2016-10-12 01:27:54 +03:00
{ " get_ntlm_username_domain " , py_creds_get_ntlm_username_domain , METH_NOARGS ,
" S.get_ntlm_username_domain() -> (domain, username) \n "
" Obtain NTLM username and domain, split up either as (DOMAIN, user) or ( \" \" , \" user@realm \" ). " } ,
2017-03-22 06:40:40 +03:00
{ " get_ntlm_response " , ( PyCFunction ) py_creds_get_ntlm_response , METH_VARARGS | METH_KEYWORDS ,
2017-04-28 04:13:28 +03:00
" S.get_ntlm_response "
" (flags, challenge[, target_info]) -> "
2017-03-22 06:40:40 +03:00
" (flags, lm_response, nt_response, lm_session_key, nt_session_key) \n "
" Obtain LM or NTLM response. " } ,
2016-02-22 04:10:23 +03:00
{ " set_password " , py_creds_set_password , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.set_password(password[, credentials.SPECIFIED]) -> None \n "
2008-12-21 07:29:23 +03:00
" Change password. " } ,
2016-07-12 10:57:16 +03:00
{ " set_utf16_password " , py_creds_set_utf16_password , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.set_utf16_password(password[, credentials.SPECIFIED]) -> None \n "
2016-07-12 10:57:16 +03:00
" Change password. " } ,
2016-07-12 09:14:36 +03:00
{ " get_old_password " , py_creds_get_old_password , METH_NOARGS ,
" S.get_old_password() -> password \n "
" Obtain old password. " } ,
{ " set_old_password " , py_creds_set_old_password , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.set_old_password(password[, credentials.SPECIFIED]) -> None \n "
2016-07-12 09:14:36 +03:00
" Change old password. " } ,
2016-07-12 10:57:16 +03:00
{ " set_old_utf16_password " , py_creds_set_old_utf16_password , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.set_old_utf16_password(password[, credentials.SPECIFIED]) -> None \n "
2016-07-12 10:57:16 +03:00
" Change old password. " } ,
2016-02-22 04:10:23 +03:00
{ " get_domain " , py_creds_get_domain , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
" S.get_domain() -> domain \n "
" Obtain domain name. " } ,
2016-02-22 04:10:23 +03:00
{ " set_domain " , py_creds_set_domain , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.set_domain(domain[, credentials.SPECIFIED]) -> None \n "
2008-12-21 07:29:23 +03:00
" Change domain name. " } ,
2016-02-22 04:10:23 +03:00
{ " get_realm " , py_creds_get_realm , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
" S.get_realm() -> realm \n "
" Obtain realm name. " } ,
2016-02-22 04:10:23 +03:00
{ " set_realm " , py_creds_set_realm , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.set_realm(realm[, credentials.SPECIFIED]) -> None \n "
2008-12-21 07:29:23 +03:00
" Change realm name. " } ,
2016-02-22 04:10:23 +03:00
{ " get_bind_dn " , py_creds_get_bind_dn , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
" S.get_bind_dn() -> bind dn \n "
" Obtain bind DN. " } ,
2016-02-22 04:10:23 +03:00
{ " set_bind_dn " , py_creds_set_bind_dn , METH_VARARGS ,
2008-12-21 07:29:23 +03:00
" S.set_bind_dn(bind_dn) -> None \n "
" Change bind DN. " } ,
2016-02-22 04:10:23 +03:00
{ " is_anonymous " , py_creds_is_anonymous , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
NULL } ,
2016-02-22 04:10:23 +03:00
{ " set_anonymous " , py_creds_set_anonymous , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
" S.set_anonymous() -> None \n "
" Use anonymous credentials. " } ,
2016-02-22 04:10:23 +03:00
{ " get_workstation " , py_creds_get_workstation , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
NULL } ,
2016-02-22 04:10:23 +03:00
{ " set_workstation " , py_creds_set_workstation , METH_VARARGS ,
2008-12-21 07:29:23 +03:00
NULL } ,
2016-02-22 04:10:23 +03:00
{ " authentication_requested " , py_creds_authentication_requested , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
NULL } ,
2016-02-22 04:10:23 +03:00
{ " wrong_password " , py_creds_wrong_password , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
" S.wrong_password() -> bool \n "
" Indicate the returned password was incorrect. " } ,
2016-02-22 04:10:23 +03:00
{ " set_cmdline_callbacks " , py_creds_set_cmdline_callbacks , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
" S.set_cmdline_callbacks() -> bool \n "
" Use command-line to obtain credentials not explicitly set. " } ,
2016-02-22 04:10:23 +03:00
{ " parse_string " , py_creds_parse_string , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.parse_string(text[, credentials.SPECIFIED]) -> None \n "
2008-12-21 07:29:23 +03:00
" Parse credentials string. " } ,
2016-12-15 12:06:25 +03:00
{ " parse_file " , py_creds_parse_file , METH_VARARGS ,
2016-12-15 13:37:33 +03:00
" S.parse_file(filename[, credentials.SPECIFIED]) -> None \n "
2016-12-15 12:06:25 +03:00
" Parse credentials file. " } ,
2016-12-14 12:02:10 +03:00
{ " set_password_will_be_nt_hash " ,
py_cli_credentials_set_password_will_be_nt_hash , METH_VARARGS ,
" S.set_password_will_be_nt_hash(bool) -> None \n "
" Alters the behaviour of S.set_password() "
" to expect the NTHASH as hexstring. " } ,
2016-02-22 04:10:23 +03:00
{ " get_nt_hash " , py_creds_get_nt_hash , METH_NOARGS ,
2008-12-21 07:29:23 +03:00
NULL } ,
2016-02-22 04:10:23 +03:00
{ " get_kerberos_state " , py_creds_get_kerberos_state , METH_NOARGS ,
2015-12-01 03:17:18 +03:00
NULL } ,
2016-02-22 04:10:23 +03:00
{ " set_kerberos_state " , py_creds_set_kerberos_state , METH_VARARGS ,
2008-12-21 07:29:23 +03:00
NULL } ,
2016-02-22 04:10:23 +03:00
{ " set_krb_forwardable " , py_creds_set_krb_forwardable , METH_VARARGS ,
2010-09-16 08:12:57 +04:00
NULL } ,
2016-02-22 04:10:23 +03:00
{ " guess " , py_creds_guess , METH_VARARGS , NULL } ,
{ " set_machine_account " , py_creds_set_machine_account , METH_VARARGS , NULL } ,
{ " get_named_ccache " , py_creds_get_named_ccache , METH_VARARGS , NULL } ,
{ " set_gensec_features " , py_creds_set_gensec_features , METH_VARARGS , NULL } ,
{ " get_gensec_features " , py_creds_get_gensec_features , METH_NOARGS , NULL } ,
{ " get_forced_sasl_mech " , py_creds_get_forced_sasl_mech , METH_NOARGS ,
2013-09-16 20:38:09 +04:00
" S.get_forced_sasl_mech() -> SASL mechanism \n Obtain forced SASL mechanism. " } ,
2016-02-22 04:10:23 +03:00
{ " set_forced_sasl_mech " , py_creds_set_forced_sasl_mech , METH_VARARGS ,
2013-09-16 20:38:09 +04:00
" S.set_forced_sasl_mech(name) -> None \n "
" Set forced SASL mechanism. " } ,
2017-04-28 01:16:39 +03:00
{ " set_secure_channel_type " , py_creds_set_secure_channel_type ,
METH_VARARGS , NULL } ,
2008-12-21 07:29:23 +03:00
{ NULL }
} ;
2016-10-17 17:07:31 +03:00
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT ,
. m_name = " credentials " ,
. m_doc = " Credentials management. " ,
. m_size = - 1 ,
. m_methods = py_creds_methods ,
} ;
2008-12-21 07:29:23 +03:00
PyTypeObject PyCredentials = {
2011-07-20 08:48:34 +04:00
. tp_name = " credentials.Credentials " ,
2008-12-21 07:29:23 +03:00
. 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 = {
2011-07-20 08:48:34 +04:00
. tp_name = " credentials.CredentialCacheContainer " ,
2010-02-20 03:44:41 +03:00
. tp_flags = Py_TPFLAGS_DEFAULT ,
} ;
2016-10-17 17:07:31 +03:00
MODULE_INIT_FUNC ( credentials )
2008-12-21 07:29:23 +03:00
{
PyObject * m ;
2016-02-29 23:27:11 +03:00
if ( pytalloc_BaseObject_PyType_Ready ( & PyCredentials ) < 0 )
2016-10-17 17:07:31 +03:00
return NULL ;
2010-12-01 02:02:19 +03:00
2016-02-29 23:27:11 +03:00
if ( pytalloc_BaseObject_PyType_Ready ( & PyCredentialCacheContainer ) < 0 )
2016-10-17 17:07:31 +03:00
return NULL ;
2010-02-20 03:44:41 +03:00
2016-10-17 17:07:31 +03:00
m = PyModule_Create ( & moduledef ) ;
2008-12-21 07:29:23 +03:00
if ( m = = NULL )
2016-10-17 17:07:31 +03:00
return NULL ;
2008-12-21 07:29:23 +03:00
2016-12-15 13:37:33 +03:00
PyModule_AddObject ( m , " UNINITIALISED " , PyInt_FromLong ( CRED_UNINITIALISED ) ) ;
PyModule_AddObject ( m , " CALLBACK " , PyInt_FromLong ( CRED_CALLBACK ) ) ;
PyModule_AddObject ( m , " GUESS_ENV " , PyInt_FromLong ( CRED_GUESS_ENV ) ) ;
PyModule_AddObject ( m , " GUESS_FILE " , PyInt_FromLong ( CRED_GUESS_FILE ) ) ;
PyModule_AddObject ( m , " CALLBACK_RESULT " , PyInt_FromLong ( CRED_CALLBACK_RESULT ) ) ;
PyModule_AddObject ( m , " SPECIFIED " , PyInt_FromLong ( CRED_SPECIFIED ) ) ;
2008-12-21 07:29:23 +03:00
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 ) ) ;
2010-09-16 08:12:57 +04:00
PyModule_AddObject ( m , " AUTO_KRB_FORWARDABLE " , PyInt_FromLong ( CRED_AUTO_KRB_FORWARDABLE ) ) ;
PyModule_AddObject ( m , " NO_KRB_FORWARDABLE " , PyInt_FromLong ( CRED_NO_KRB_FORWARDABLE ) ) ;
PyModule_AddObject ( m , " FORCE_KRB_FORWARDABLE " , PyInt_FromLong ( CRED_FORCE_KRB_FORWARDABLE ) ) ;
2017-03-22 06:40:40 +03:00
PyModule_AddObject ( m , " CLI_CRED_NTLM2 " , PyInt_FromLong ( CLI_CRED_NTLM2 ) ) ;
PyModule_AddObject ( m , " CLI_CRED_NTLMv2_AUTH " , PyInt_FromLong ( CLI_CRED_NTLMv2_AUTH ) ) ;
PyModule_AddObject ( m , " CLI_CRED_LANMAN_AUTH " , PyInt_FromLong ( CLI_CRED_LANMAN_AUTH ) ) ;
PyModule_AddObject ( m , " CLI_CRED_NTLM_AUTH " , PyInt_FromLong ( CLI_CRED_NTLM_AUTH ) ) ;
PyModule_AddObject ( m , " CLI_CRED_CLEAR_AUTH " , PyInt_FromLong ( CLI_CRED_CLEAR_AUTH ) ) ;
2010-09-16 08:12:57 +04:00
2008-12-21 07:29:23 +03:00
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 ) ;
2016-10-17 17:07:31 +03:00
return m ;
2008-12-21 07:29:23 +03:00
}