2009-12-24 06:43:21 +03:00
/*
2008-04-08 07:16:07 +04:00
Unix SMB / CIFS implementation .
Samba utility functions
Copyright ( C ) Jelmer Vernooij < jelmer @ samba . org > 2008
2009-12-24 06:43:21 +03:00
Copyright ( C ) Kamen Mazdrashki < kamen . mazdrashki @ postpath . com > 2009
2008-04-08 07:16:07 +04:00
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 .
2009-12-24 06:43:21 +03:00
2008-04-08 07:16:07 +04:00
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 .
2009-12-24 06:43:21 +03:00
2008-04-08 07:16:07 +04:00
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>
2009-10-23 09:23:01 +04:00
# include "includes.h"
2008-04-08 07:16:07 +04:00
# include "libnet.h"
2009-07-28 12:00:13 +04:00
# include "auth/credentials/pycredentials.h"
2008-04-08 07:16:07 +04:00
# include "libcli/security/security.h"
2008-04-14 20:43:37 +04:00
# include "lib/events/events.h"
2008-10-24 18:52:25 +04:00
# include "param/param.h"
2008-04-08 07:16:07 +04:00
2008-11-02 18:50:11 +03:00
/* FIXME: This prototype should be in param/pyparam.h */
struct loadparm_context * py_default_loadparm_context ( TALLOC_CTX * mem_ctx ) ;
2009-07-28 10:01:31 +04:00
static struct libnet_context * py_net_ctx ( PyObject * obj , struct tevent_context * ev , struct cli_credentials * creds )
2008-04-08 07:16:07 +04:00
{
2009-07-28 10:01:31 +04:00
/* FIXME: Use obj */
struct libnet_context * libnet ;
libnet = libnet_context_init ( ev , py_default_loadparm_context ( NULL ) ) ;
if ( ! libnet ) {
return NULL ;
}
2009-07-28 12:00:13 +04:00
libnet - > cred = creds ;
return libnet ;
2008-04-08 07:16:07 +04:00
}
static PyObject * py_net_join ( PyObject * cls , PyObject * args , PyObject * kwargs )
{
struct libnet_Join r ;
NTSTATUS status ;
PyObject * result ;
TALLOC_CTX * mem_ctx ;
2008-12-29 22:24:57 +03:00
struct tevent_context * ev ;
2008-04-08 07:16:07 +04:00
struct libnet_context * libnet_ctx ;
2009-07-28 10:01:31 +04:00
struct cli_credentials * creds ;
PyObject * py_creds ;
const char * kwnames [ ] = { " domain_name " , " netbios_name " , " join_type " , " level " , " credentials " , NULL } ;
2008-04-08 07:16:07 +04:00
2009-07-28 10:01:31 +04:00
if ( ! PyArg_ParseTupleAndKeywords ( args , kwargs , " ssiiO:Join " , discard_const_p ( char * , kwnames ) ,
2008-04-08 07:16:07 +04:00
& r . in . domain_name , & r . in . netbios_name ,
2009-07-28 10:01:31 +04:00
& r . in . join_type , & r . in . level , & py_creds ) )
2008-04-08 07:16:07 +04:00
return NULL ;
2008-04-14 20:43:37 +04:00
/* FIXME: we really need to get a context from the caller or we may end
* up with 2 event contexts */
2008-06-14 21:00:53 +04:00
ev = s4_event_context_init ( NULL ) ;
2008-04-14 20:43:37 +04:00
mem_ctx = talloc_new ( ev ) ;
2008-04-08 07:16:07 +04:00
2009-07-28 10:01:31 +04:00
creds = cli_credentials_from_py_object ( py_creds ) ;
if ( creds = = NULL ) {
PyErr_SetString ( PyExc_TypeError , " Expected credentials object " ) ;
2009-12-25 16:48:45 +03:00
talloc_free ( mem_ctx ) ;
2009-07-28 10:01:31 +04:00
return NULL ;
}
libnet_ctx = py_net_ctx ( cls , ev , creds ) ;
2009-12-25 16:48:45 +03:00
if ( libnet_ctx = = NULL ) {
PyErr_SetString ( PyExc_RuntimeError , " Unable to initialize libnet " ) ;
talloc_free ( mem_ctx ) ;
return NULL ;
}
2008-04-08 07:16:07 +04:00
status = libnet_Join ( libnet_ctx , mem_ctx , & r ) ;
if ( NT_STATUS_IS_ERR ( status ) ) {
PyErr_SetString ( PyExc_RuntimeError , r . out . error_string ) ;
talloc_free ( mem_ctx ) ;
return NULL ;
}
2009-12-24 06:43:21 +03:00
result = Py_BuildValue ( " sss " , r . out . join_password ,
2008-04-08 07:16:07 +04:00
dom_sid_string ( mem_ctx , r . out . domain_sid ) ,
r . out . domain_name ) ;
talloc_free ( mem_ctx ) ;
return result ;
}
2010-03-01 22:43:19 +03:00
static const char py_net_join_doc [ ] = " join(domain_name, netbios_name, join_type, level) -> (join_password, domain_sid, domain_name) \n \n " \
" Join the domain with the specified name. " ;
2009-12-24 06:43:21 +03:00
static PyObject * py_net_set_password ( PyObject * cls , PyObject * args , PyObject * kwargs )
{
union libnet_SetPassword r ;
NTSTATUS status ;
PyObject * py_creds ;
TALLOC_CTX * mem_ctx ;
struct tevent_context * ev ;
struct libnet_context * libnet_ctx ;
struct cli_credentials * creds ;
const char * kwnames [ ] = { " account_name " , " domain_name " , " newpassword " , " credentials " , NULL } ;
r . generic . level = LIBNET_SET_PASSWORD_GENERIC ;
2010-03-01 22:43:19 +03:00
if ( ! PyArg_ParseTupleAndKeywords ( args , kwargs , " sssO:set_password " , discard_const_p ( char * , kwnames ) ,
2009-12-24 06:43:21 +03:00
& r . generic . in . account_name , & r . generic . in . domain_name ,
& r . generic . in . newpassword , & py_creds ) ) {
return NULL ;
}
2009-12-25 16:48:45 +03:00
/* FIXME: we really need to get a context from the caller or we may end
* up with 2 event contexts */
2009-12-24 06:43:21 +03:00
ev = s4_event_context_init ( NULL ) ;
mem_ctx = talloc_new ( ev ) ;
creds = cli_credentials_from_py_object ( py_creds ) ;
if ( creds = = NULL ) {
PyErr_SetString ( PyExc_TypeError , " Expected credentials object " ) ;
return NULL ;
}
libnet_ctx = py_net_ctx ( cls , ev , creds ) ;
status = libnet_SetPassword ( libnet_ctx , mem_ctx , & r ) ;
if ( NT_STATUS_IS_ERR ( status ) ) {
PyErr_SetString ( PyExc_RuntimeError , r . generic . out . error_string ) ;
talloc_free ( mem_ctx ) ;
return NULL ;
}
2010-03-01 22:43:19 +03:00
talloc_free ( mem_ctx ) ;
2009-12-24 06:43:21 +03:00
Py_RETURN_NONE ;
}
2010-03-01 22:43:19 +03:00
static const char py_net_set_password_doc [ ] = " set_password(account_name, domain_name, newpassword) -> True \n \n " \
2009-12-24 06:43:21 +03:00
" Set password for a user. You must supply credential with enough rights to do this. \n \n " \
" Sample usage is: \n " \
" creds = samba.credentials.Credentials() \n " \
" creds.set_username('admin_user') \n " \
" creds.set_domain('domain_name') \n " \
" creds.set_password('pass') \n \n " \
2010-03-01 22:43:19 +03:00
" net.set_password(account_name=<account_name>, \n " \
2009-12-24 06:43:21 +03:00
" domain_name=creds.get_domain(), \n " \
" newpassword=new_pass, \n " \
" credentials=creds) \n " ;
2010-03-01 22:43:19 +03:00
static PyObject * py_net_export_keytab ( PyObject * cls , PyObject * args , PyObject * kwargs )
{
struct libnet_export_keytab r ;
struct tevent_context * ev ;
TALLOC_CTX * mem_ctx ;
const char * kwnames [ ] = { " keytab " , " creds " , NULL } ;
struct libnet_context * libnet_ctx ;
PyObject * py_creds ;
struct cli_credentials * creds ;
NTSTATUS status ;
if ( ! PyArg_ParseTupleAndKeywords ( args , kwargs , " sO:export_keytab " , discard_const_p ( char * , kwnames ) ,
& r . in . keytab_name , & py_creds ) ) {
return NULL ;
}
creds = cli_credentials_from_py_object ( py_creds ) ;
if ( creds = = NULL ) {
PyErr_SetString ( PyExc_TypeError , " Expected credentials object " ) ;
return NULL ;
}
/* FIXME: we really need to get a context from the caller or we may end
* up with 2 event contexts */
ev = s4_event_context_init ( NULL ) ;
mem_ctx = talloc_new ( ev ) ;
libnet_ctx = py_net_ctx ( cls , ev , creds ) ;
status = libnet_export_keytab ( libnet_ctx , mem_ctx , & r ) ;
if ( NT_STATUS_IS_ERR ( status ) ) {
PyErr_SetString ( PyExc_RuntimeError , r . out . error_string ) ;
talloc_free ( mem_ctx ) ;
return NULL ;
}
talloc_free ( mem_ctx ) ;
Py_RETURN_NONE ;
}
static const char py_net_export_keytab_doc [ ] = " export_keytab(keytab, name) \n \n "
" Export the DC keytab to a keytab file. " ;
2008-04-08 07:16:07 +04:00
static struct PyMethodDef net_methods [ ] = {
2010-03-01 22:43:19 +03:00
{ " join " , ( PyCFunction ) py_net_join , METH_VARARGS | METH_KEYWORDS , py_net_join_doc } ,
{ " set_password " , ( PyCFunction ) py_net_set_password , METH_VARARGS | METH_KEYWORDS , py_net_set_password_doc } ,
{ " export_keytab " , ( PyCFunction ) py_net_export_keytab , METH_VARARGS | METH_KEYWORDS , py_net_export_keytab_doc } ,
2008-04-08 07:16:07 +04:00
{ NULL }
} ;
void initnet ( void )
{
2009-12-25 16:48:45 +03:00
Py_InitModule3 ( " net " , net_methods , NULL ) ;
2008-04-08 07:16:07 +04:00
}