2006-07-11 22:01:26 +04:00
/*
Unix SMB / CIFS implementation .
libndr interface
Copyright ( C ) Jelmer Vernooij 2006
2009-07-27 16:28:52 +04:00
Copyright ( C ) Volker Lendecke 2009
2006-07-11 22:01:26 +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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2006-07-11 22:01:26 +04:00
( 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
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2006-07-11 22:01:26 +04:00
*/
# include "includes.h"
2009-07-27 16:28:52 +04:00
struct cli_do_rpc_ndr_state {
const struct ndr_interface_call * call ;
2010-07-15 16:48:51 +04:00
DATA_BLOB q_pdu ;
2010-07-15 03:39:54 +04:00
DATA_BLOB r_pdu ;
2009-07-27 16:28:52 +04:00
void * r ;
} ;
2006-07-11 22:01:26 +04:00
2009-07-27 16:28:52 +04:00
static void cli_do_rpc_ndr_done ( struct tevent_req * subreq ) ;
struct tevent_req * cli_do_rpc_ndr_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct rpc_pipe_client * cli ,
2009-11-08 21:38:01 +03:00
const struct ndr_interface_table * table ,
2009-07-27 16:28:52 +04:00
uint32_t opnum ,
void * r )
2006-07-11 22:01:26 +04:00
{
2009-07-27 16:28:52 +04:00
struct tevent_req * req , * subreq ;
struct cli_do_rpc_ndr_state * state ;
2006-07-11 22:01:26 +04:00
struct ndr_push * push ;
2007-11-09 16:39:45 +03:00
enum ndr_err_code ndr_err ;
2009-07-27 16:28:52 +04:00
req = tevent_req_create ( mem_ctx , & state ,
struct cli_do_rpc_ndr_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2006-07-11 22:01:26 +04:00
2009-07-27 16:28:52 +04:00
if ( ! ndr_syntax_id_equal ( & table - > syntax_id , & cli - > abstract_syntax )
| | ( opnum > = table - > num_calls ) ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER ) ;
return tevent_req_post ( req , ev ) ;
}
2007-08-21 16:20:33 +04:00
2009-07-27 16:28:52 +04:00
state - > r = r ;
state - > call = & table - > calls [ opnum ] ;
2006-07-11 22:01:26 +04:00
2009-11-07 12:28:40 +03:00
if ( DEBUGLEVEL > = 10 ) {
ndr_print_function_debug ( state - > call - > ndr_print ,
state - > call - > name , NDR_IN , r ) ;
}
2010-07-15 16:48:51 +04:00
push = ndr_push_init_ctx ( state ) ;
2009-07-27 16:28:52 +04:00
if ( tevent_req_nomem ( push , req ) ) {
return tevent_req_post ( req , ev ) ;
2006-07-11 22:01:26 +04:00
}
2009-07-27 16:28:52 +04:00
ndr_err = state - > call - > ndr_push ( push , NDR_IN , r ) ;
2007-11-09 16:39:45 +03:00
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_err ) ) {
2009-07-27 16:28:52 +04:00
tevent_req_nterror ( req , ndr_map_error2ntstatus ( ndr_err ) ) ;
TALLOC_FREE ( push ) ;
return tevent_req_post ( req , ev ) ;
2006-07-11 22:01:26 +04:00
}
2010-07-15 16:48:51 +04:00
state - > q_pdu = ndr_push_blob ( push ) ;
talloc_steal ( mem_ctx , state - > q_pdu . data ) ;
2009-07-27 16:28:52 +04:00
TALLOC_FREE ( push ) ;
2006-07-11 22:01:26 +04:00
2010-07-15 16:48:51 +04:00
subreq = rpc_api_pipe_req_send ( state , ev , cli , opnum , & state - > q_pdu ) ;
2009-07-27 16:28:52 +04:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_do_rpc_ndr_done , req ) ;
return req ;
}
2006-07-11 22:01:26 +04:00
2009-07-27 16:28:52 +04:00
static void cli_do_rpc_ndr_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_do_rpc_ndr_state * state = tevent_req_data (
req , struct cli_do_rpc_ndr_state ) ;
NTSTATUS status ;
2006-07-11 22:01:26 +04:00
2010-07-15 03:39:54 +04:00
status = rpc_api_pipe_req_recv ( subreq , state , & state - > r_pdu ) ;
2009-07-27 16:28:52 +04:00
TALLOC_FREE ( subreq ) ;
2006-07-11 22:01:26 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-07-27 16:28:52 +04:00
tevent_req_nterror ( req , status ) ;
return ;
}
tevent_req_done ( req ) ;
}
NTSTATUS cli_do_rpc_ndr_recv ( struct tevent_req * req , TALLOC_CTX * mem_ctx )
{
struct cli_do_rpc_ndr_state * state = tevent_req_data (
req , struct cli_do_rpc_ndr_state ) ;
struct ndr_pull * pull ;
enum ndr_err_code ndr_err ;
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
2006-07-11 22:01:26 +04:00
return status ;
}
2010-07-15 03:39:54 +04:00
pull = ndr_pull_init_blob ( & state - > r_pdu , mem_ctx ) ;
2006-07-11 22:01:26 +04:00
if ( pull = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
2006-09-12 00:00:00 +04:00
/* have the ndr parser alloc memory for us */
pull - > flags | = LIBNDR_FLAG_REF_ALLOC ;
2009-07-27 16:28:52 +04:00
ndr_err = state - > call - > ndr_pull ( pull , NDR_OUT , state - > r ) ;
TALLOC_FREE ( pull ) ;
2006-07-11 22:01:26 +04:00
2009-11-07 12:28:40 +03:00
if ( NDR_ERR_CODE_IS_SUCCESS ( ndr_err ) ) {
if ( DEBUGLEVEL > = 10 ) {
ndr_print_function_debug ( state - > call - > ndr_print ,
state - > call - > name , NDR_OUT ,
state - > r ) ;
}
} else {
2007-11-09 16:39:45 +03:00
return ndr_map_error2ntstatus ( ndr_err ) ;
2006-07-11 22:01:26 +04:00
}
return NT_STATUS_OK ;
}
2009-07-27 16:28:52 +04:00
NTSTATUS cli_do_rpc_ndr ( struct rpc_pipe_client * cli ,
TALLOC_CTX * mem_ctx ,
2009-11-08 21:38:01 +03:00
const struct ndr_interface_table * table ,
2009-07-27 16:28:52 +04:00
uint32_t opnum , void * r )
{
TALLOC_CTX * frame = talloc_stackframe ( ) ;
struct event_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_OK ;
ev = event_context_init ( frame ) ;
if ( ev = = NULL ) {
status = NT_STATUS_NO_MEMORY ;
goto fail ;
}
2009-11-08 21:38:01 +03:00
req = cli_do_rpc_ndr_send ( frame , ev , cli , table , opnum , r ) ;
2009-07-27 16:28:52 +04:00
if ( req = = NULL ) {
status = NT_STATUS_NO_MEMORY ;
goto fail ;
}
if ( ! tevent_req_poll ( req , ev ) ) {
status = map_nt_error_from_unix ( errno ) ;
goto fail ;
}
status = cli_do_rpc_ndr_recv ( req , mem_ctx ) ;
2010-01-06 07:13:35 +03:00
2009-07-27 16:28:52 +04:00
fail :
TALLOC_FREE ( frame ) ;
return status ;
}