2012-04-10 13:09:20 +04:00
/*
Unix SMB / CIFS implementation .
Samba utility functions
Copyright ( C ) Jelmer Vernooij < jelmer @ samba . org > 2008 - 2010
Copyright ( C ) Kamen Mazdrashki < kamen . mazdrashki @ postpath . com > 2009
Copyright ( C ) Alexander Bokovoy < ab @ samba . org > 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/>.
*/
2023-11-09 13:35:56 +03:00
# include "lib/replace/system/python.h"
2012-04-10 13:09:20 +04:00
# include "includes.h"
2018-02-02 19:08:48 +03:00
# include "python/py3compat.h"
2019-05-02 21:45:14 +03:00
# include "python/modules.h"
2012-04-10 13:09:20 +04:00
# include "py_net.h"
# include "libnet_export_keytab.h"
2023-12-19 07:04:35 +03:00
# include "pyldb.h"
2024-03-07 05:34:37 +03:00
# include "libcli/util/pyerrors.h"
2012-04-10 13:09:20 +04:00
static PyObject * py_net_export_keytab ( py_net_Object * self , PyObject * args , PyObject * kwargs )
{
2024-03-15 18:11:10 +03:00
struct libnet_export_keytab r = { . in = { . principal = NULL , } } ;
2023-12-19 07:04:35 +03:00
PyObject * py_samdb = NULL ;
2012-04-10 13:09:20 +04:00
TALLOC_CTX * mem_ctx ;
2024-03-07 01:59:21 +03:00
const char * kwnames [ ] = { " keytab " ,
" samdb " ,
" principal " ,
" keep_stale_entries " ,
2024-03-15 18:11:10 +03:00
" only_current_keys " ,
2024-04-26 04:54:42 +03:00
" as_for_AS_REQ " ,
2024-03-07 01:59:21 +03:00
NULL } ;
2012-04-10 13:09:20 +04:00
NTSTATUS status ;
2024-03-07 01:59:21 +03:00
/*
* int , with values true or false , to match expectation of
* PyArg_ParseTupleAndKeywords ( )
*/
int keep_stale_entries = false ;
2024-03-15 18:11:10 +03:00
int only_current_keys = false ;
2024-04-26 04:54:42 +03:00
int as_for_AS_REQ = false ;
2024-03-07 01:59:21 +03:00
2024-04-26 04:54:42 +03:00
if ( ! PyArg_ParseTupleAndKeywords ( args , kwargs , " s|Ozppp:export_keytab " , discard_const_p ( char * , kwnames ) ,
2012-04-10 13:09:20 +04:00
& r . in . keytab_name ,
2023-12-19 07:04:35 +03:00
& py_samdb ,
2024-03-07 01:59:21 +03:00
& r . in . principal ,
2024-03-15 18:11:10 +03:00
& keep_stale_entries ,
2024-04-26 04:54:42 +03:00
& only_current_keys ,
& as_for_AS_REQ ) ) {
2012-04-10 13:09:20 +04:00
return NULL ;
}
2024-03-07 01:59:21 +03:00
r . in . keep_stale_entries = keep_stale_entries ;
2024-03-15 18:11:10 +03:00
r . in . only_current_keys = only_current_keys ;
2024-04-26 04:54:42 +03:00
r . in . as_for_AS_REQ = as_for_AS_REQ ;
2024-03-07 01:59:21 +03:00
2023-12-19 07:04:35 +03:00
if ( py_samdb = = NULL ) {
r . in . samdb = NULL ;
} else {
PyErr_LDB_OR_RAISE ( py_samdb , r . in . samdb ) ;
}
2012-04-10 13:09:20 +04:00
mem_ctx = talloc_new ( self - > mem_ctx ) ;
if ( mem_ctx = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
status = libnet_export_keytab ( self - > libnet_ctx , mem_ctx , & r ) ;
2024-03-07 05:34:37 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
PyErr_SetNTSTATUS_and_string ( status ,
r . out . error_string
? r . out . error_string
: nt_errstr ( status ) ) ;
2012-04-10 13:09:20 +04:00
talloc_free ( mem_ctx ) ;
return NULL ;
}
talloc_free ( mem_ctx ) ;
Py_RETURN_NONE ;
}
2024-04-24 08:11:03 +03:00
static const char py_net_export_keytab_doc [ ] =
" export_keytab(keytab, samdb=None, principal=None, "
2024-04-26 04:54:42 +03:00
" keep_stale_entries=False, only_current_keys=False, "
" as_for_AS_REQ=False) \n \n "
" Export the DC keytab to a keytab file. \n \n "
" Pass as_for_AS_REQ=True to simulate the combination of flags normally "
" utilized for an AS‐ REQ. Samba’ s testsuite uses this to verify which "
" keys the KDC would see — some combination of previous and current "
" keys — for a Group Managed Service Account performing an AS‐ REQ. " ;
2012-04-10 13:09:20 +04:00
static PyMethodDef export_keytab_method_table [ ] = {
2019-05-02 21:45:14 +03:00
{ " export_keytab " , PY_DISCARD_FUNC_SIG ( PyCFunction ,
py_net_export_keytab ) ,
METH_VARARGS | METH_KEYWORDS , py_net_export_keytab_doc } ,
2012-04-10 13:09:20 +04:00
{ NULL , NULL , 0 , NULL }
} ;
/*
* A fake Python module to inject export_keytab ( ) method into existing samba . net . Net class .
* Python enforces that every loaded module actually creates Python module record in
* the global module table even if we don ' t really need that record . Thus , we initialize
* dckeytab module but never use it .
* */
2018-02-02 19:08:48 +03:00
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT ,
. m_name = " dckeytab " ,
. m_doc = " dckeytab " ,
. m_size = - 1 ,
. m_methods = NULL
} ;
MODULE_INIT_FUNC ( dckeytab )
2012-04-10 13:09:20 +04:00
{
2018-02-02 19:08:48 +03:00
PyObject * m = NULL ;
2012-04-10 13:09:20 +04:00
PyObject * Net ;
PyObject * descr ;
int ret ;
2018-02-02 19:08:48 +03:00
m = PyModule_Create ( & moduledef ) ;
2012-04-10 13:09:20 +04:00
if ( m = = NULL )
2018-02-02 19:08:48 +03:00
return m ;
2012-04-10 13:09:20 +04:00
m = PyImport_ImportModule ( " samba.net " ) ;
if ( m = = NULL )
2018-02-02 19:08:48 +03:00
return m ;
2012-04-10 13:09:20 +04:00
Net = ( PyObject * ) PyObject_GetAttrString ( m , " Net " ) ;
if ( Net = = NULL )
2018-02-02 19:08:48 +03:00
return m ;
2012-04-10 13:09:20 +04:00
descr = PyDescr_NewMethod ( ( PyTypeObject * ) Net , & export_keytab_method_table [ 0 ] ) ;
if ( descr = = NULL )
2018-02-02 19:08:48 +03:00
return m ;
2012-04-10 13:09:20 +04:00
ret = PyDict_SetItemString ( ( ( PyTypeObject * ) Net ) - > tp_dict ,
export_keytab_method_table [ 0 ] . ml_name ,
descr ) ;
if ( ret ! = - 1 ) {
Py_DECREF ( descr ) ;
}
2018-02-02 19:08:48 +03:00
return m ;
2012-04-10 13:09:20 +04:00
}