2009-11-10 21:49:41 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-04-25 18:04:06 +04:00
client message handling routines
Copyright ( C ) Andrew Tridgell 1994 - 1998
2009-11-10 21:49:41 +03:00
2000-04-25 18:04:06 +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
2000-04-25 18:04:06 +04:00
( at your option ) any later version .
2009-11-10 21:49:41 +03:00
2000-04-25 18:04:06 +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-11-10 21:49:41 +03:00
2000-04-25 18:04:06 +04:00
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/>.
2000-04-25 18:04:06 +04:00
*/
# include "includes.h"
2011-04-28 19:38:09 +04:00
# include "../lib/util/tevent_ntstatus.h"
2010-08-26 11:58:09 +04:00
# include "async_smb.h"
2011-05-06 13:47:43 +04:00
# include "libsmb/libsmb.h"
2012-05-26 13:45:09 +04:00
# include "../libcli/smb/smbXcli_base.h"
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
struct cli_message_start_state {
uint16_t grp ;
} ;
static void cli_message_start_done ( struct tevent_req * subreq ) ;
2007-12-07 04:16:33 +03:00
2009-11-10 21:49:41 +03:00
static struct tevent_req * cli_message_start_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct cli_state * cli ,
const char * host ,
const char * username )
2000-04-25 18:04:06 +04:00
{
2009-11-10 21:49:41 +03:00
struct tevent_req * req , * subreq ;
struct cli_message_start_state * state ;
char * htmp = NULL ;
char * utmp = NULL ;
size_t hlen , ulen ;
uint8_t * bytes , * p ;
req = tevent_req_create ( mem_ctx , & state ,
struct cli_message_start_state ) ;
if ( req = = NULL ) {
return NULL ;
}
if ( ! convert_string_talloc ( talloc_tos ( ) , CH_UNIX , CH_DOS ,
username , strlen ( username ) + 1 ,
2011-03-24 02:59:41 +03:00
& utmp , & ulen ) ) {
2009-11-10 21:49:41 +03:00
goto fail ;
}
if ( ! convert_string_talloc ( talloc_tos ( ) , CH_UNIX , CH_DOS ,
host , strlen ( host ) + 1 ,
2011-03-24 02:59:41 +03:00
& htmp , & hlen ) ) {
2009-11-10 21:49:41 +03:00
goto fail ;
}
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
bytes = talloc_array ( state , uint8_t , ulen + hlen + 2 ) ;
if ( bytes = = NULL ) {
goto fail ;
}
p = bytes ;
2007-12-07 04:16:33 +03:00
2000-04-25 18:04:06 +04:00
* p + + = 4 ;
2009-11-10 21:49:41 +03:00
memcpy ( p , utmp , ulen ) ;
2010-08-21 13:32:58 +04:00
p + = ulen ;
2000-04-25 18:04:06 +04:00
* p + + = 4 ;
2009-11-10 21:49:41 +03:00
memcpy ( p , htmp , hlen ) ;
2010-08-21 13:32:58 +04:00
p + = hlen ;
2009-11-10 21:49:41 +03:00
TALLOC_FREE ( htmp ) ;
TALLOC_FREE ( utmp ) ;
subreq = cli_smb_send ( state , ev , cli , SMBsendstrt , 0 , 0 , NULL ,
talloc_get_size ( bytes ) , bytes ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_message_start_done , req ) ;
return req ;
fail :
TALLOC_FREE ( htmp ) ;
TALLOC_FREE ( utmp ) ;
tevent_req_nterror ( req , NT_STATUS_NO_MEMORY ) ;
return tevent_req_post ( req , ev ) ;
}
2007-12-07 04:16:33 +03:00
2009-11-10 21:49:41 +03:00
static void cli_message_start_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_message_start_state * state = tevent_req_data (
req , struct cli_message_start_state ) ;
NTSTATUS status ;
uint8_t wct ;
uint16_t * vwv ;
2012-06-04 17:59:42 +04:00
status = cli_smb_recv ( subreq , state , NULL , 0 , & wct , & vwv ,
2010-02-20 17:25:34 +03:00
NULL , NULL ) ;
TALLOC_FREE ( subreq ) ;
2009-11-10 21:49:41 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
TALLOC_FREE ( subreq ) ;
tevent_req_nterror ( req , status ) ;
return ;
}
if ( wct > = 1 ) {
state - > grp = SVAL ( vwv + 0 , 0 ) ;
} else {
state - > grp = 0 ;
}
tevent_req_done ( req ) ;
}
2003-06-07 03:09:39 +04:00
2009-11-10 21:49:41 +03:00
static NTSTATUS cli_message_start_recv ( struct tevent_req * req ,
uint16_t * pgrp )
{
struct cli_message_start_state * state = tevent_req_data (
req , struct cli_message_start_state ) ;
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
return status ;
}
* pgrp = state - > grp ;
return NT_STATUS_OK ;
2003-06-07 03:09:39 +04:00
}
2009-11-10 21:49:41 +03:00
struct cli_message_text_state {
uint16_t vwv ;
} ;
static void cli_message_text_done ( struct tevent_req * subreq ) ;
static struct tevent_req * cli_message_text_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct cli_state * cli ,
uint16_t grp ,
const char * msg ,
int msglen )
2003-06-07 03:09:39 +04:00
{
2009-11-10 21:49:41 +03:00
struct tevent_req * req , * subreq ;
struct cli_message_text_state * state ;
char * tmp ;
size_t tmplen ;
uint8_t * bytes ;
req = tevent_req_create ( mem_ctx , & state ,
struct cli_message_text_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2007-12-07 04:16:33 +03:00
2009-11-10 21:49:41 +03:00
SSVAL ( & state - > vwv , 0 , grp ) ;
if ( convert_string_talloc ( talloc_tos ( ) , CH_UNIX , CH_DOS , msg , msglen ,
2011-03-24 02:59:41 +03:00
& tmp , & tmplen ) ) {
2009-11-10 21:49:41 +03:00
msg = tmp ;
msglen = tmplen ;
} else {
DEBUG ( 3 , ( " Conversion failed, sending message in UNIX "
" charset \n " ) ) ;
tmp = NULL ;
2000-04-25 18:04:06 +04:00
}
2009-11-10 21:49:41 +03:00
bytes = talloc_array ( state , uint8_t , msglen + 3 ) ;
if ( tevent_req_nomem ( bytes , req ) ) {
TALLOC_FREE ( tmp ) ;
return tevent_req_post ( req , ev ) ;
}
2010-08-21 13:32:58 +04:00
SCVAL ( bytes , 0 , 1 ) ; /* pad */
SSVAL ( bytes + 1 , 0 , msglen ) ;
2009-11-10 21:49:41 +03:00
memcpy ( bytes + 3 , msg , msglen ) ;
TALLOC_FREE ( tmp ) ;
subreq = cli_smb_send ( state , ev , cli , SMBsendtxt , 0 , 1 , & state - > vwv ,
talloc_get_size ( bytes ) , bytes ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_message_text_done , req ) ;
return req ;
}
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
static void cli_message_text_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
NTSTATUS status ;
2010-02-20 11:53:58 +03:00
status = cli_smb_recv ( subreq , NULL , NULL , 0 , NULL , NULL , NULL , NULL ) ;
2009-11-10 21:49:41 +03:00
TALLOC_FREE ( subreq ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
tevent_req_done ( req ) ;
}
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
static NTSTATUS cli_message_text_recv ( struct tevent_req * req )
{
return tevent_req_simple_recv_ntstatus ( req ) ;
2000-04-25 18:04:06 +04:00
}
2009-11-10 21:49:41 +03:00
struct cli_message_end_state {
uint16_t vwv ;
} ;
2007-12-07 04:16:33 +03:00
2009-11-10 21:49:41 +03:00
static void cli_message_end_done ( struct tevent_req * subreq ) ;
static struct tevent_req * cli_message_end_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct cli_state * cli ,
uint16_t grp )
2000-04-25 18:04:06 +04:00
{
2009-11-10 21:49:41 +03:00
struct tevent_req * req , * subreq ;
struct cli_message_end_state * state ;
req = tevent_req_create ( mem_ctx , & state ,
struct cli_message_end_state ) ;
if ( req = = NULL ) {
return NULL ;
2003-03-20 19:44:14 +03:00
}
2001-06-21 09:38:28 +04:00
2009-11-10 21:49:41 +03:00
SSVAL ( & state - > vwv , 0 , grp ) ;
subreq = cli_smb_send ( state , ev , cli , SMBsendend , 0 , 1 , & state - > vwv ,
0 , NULL ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_message_end_done , req ) ;
return req ;
}
2003-06-07 03:09:39 +04:00
2009-11-10 21:49:41 +03:00
static void cli_message_end_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
NTSTATUS status ;
2010-02-20 11:53:58 +03:00
status = cli_smb_recv ( subreq , NULL , NULL , 0 , NULL , NULL , NULL , NULL ) ;
2009-11-10 21:49:41 +03:00
TALLOC_FREE ( subreq ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
tevent_req_done ( req ) ;
2003-06-07 03:09:39 +04:00
}
2009-11-10 21:49:41 +03:00
static NTSTATUS cli_message_end_recv ( struct tevent_req * req )
2003-06-07 03:09:39 +04:00
{
2009-11-10 21:49:41 +03:00
return tevent_req_simple_recv_ntstatus ( req ) ;
}
2003-06-07 03:09:39 +04:00
2009-11-10 21:49:41 +03:00
struct cli_message_state {
struct tevent_context * ev ;
struct cli_state * cli ;
size_t sent ;
const char * message ;
uint16_t grp ;
} ;
static void cli_message_started ( struct tevent_req * subreq ) ;
static void cli_message_sent ( struct tevent_req * subreq ) ;
static void cli_message_done ( struct tevent_req * subreq ) ;
struct tevent_req * cli_message_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct cli_state * cli ,
const char * host , const char * username ,
const char * message )
{
struct tevent_req * req , * subreq ;
struct cli_message_state * state ;
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
req = tevent_req_create ( mem_ctx , & state , struct cli_message_state ) ;
if ( req = = NULL ) {
return NULL ;
2000-04-25 18:04:06 +04:00
}
2009-11-10 21:49:41 +03:00
state - > ev = ev ;
state - > cli = cli ;
state - > sent = 0 ;
state - > message = message ;
subreq = cli_message_start_send ( state , ev , cli , host , username ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_message_started , req ) ;
return req ;
}
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
static void cli_message_started ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_message_state * state = tevent_req_data (
req , struct cli_message_state ) ;
NTSTATUS status ;
size_t thistime ;
status = cli_message_start_recv ( subreq , & state - > grp ) ;
TALLOC_FREE ( subreq ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
thistime = MIN ( 127 , strlen ( state - > message ) ) ;
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
subreq = cli_message_text_send ( state , state - > ev , state - > cli ,
state - > grp , state - > message , thistime ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
state - > sent + = thistime ;
tevent_req_set_callback ( subreq , cli_message_sent , req ) ;
}
2007-12-07 04:16:33 +03:00
2009-11-10 21:49:41 +03:00
static void cli_message_sent ( struct tevent_req * subreq )
2000-04-25 18:04:06 +04:00
{
2009-11-10 21:49:41 +03:00
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_message_state * state = tevent_req_data (
req , struct cli_message_state ) ;
NTSTATUS status ;
size_t left , thistime ;
status = cli_message_text_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
2003-06-07 03:09:39 +04:00
2009-11-10 21:49:41 +03:00
if ( state - > sent > = strlen ( state - > message ) ) {
subreq = cli_message_end_send ( state , state - > ev , state - > cli ,
state - > grp ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq , cli_message_done , req ) ;
return ;
}
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
left = strlen ( state - > message ) - state - > sent ;
thistime = MIN ( 127 , left ) ;
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
subreq = cli_message_text_send ( state , state - > ev , state - > cli ,
state - > grp ,
state - > message + state - > sent ,
thistime ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
state - > sent + = thistime ;
tevent_req_set_callback ( subreq , cli_message_sent , req ) ;
}
2003-06-07 03:09:39 +04:00
2009-11-10 21:49:41 +03:00
static void cli_message_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
NTSTATUS status ;
status = cli_message_end_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
tevent_req_done ( req ) ;
}
2003-06-07 03:09:39 +04:00
2009-11-10 21:49:41 +03:00
NTSTATUS cli_message_recv ( struct tevent_req * req )
{
return tevent_req_simple_recv_ntstatus ( req ) ;
2003-06-07 03:09:39 +04:00
}
2009-11-10 21:49:41 +03:00
NTSTATUS cli_message ( struct cli_state * cli , const char * host ,
const char * username , const char * message )
2003-06-07 03:09:39 +04:00
{
2009-11-10 21:49:41 +03:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2013-02-18 12:59:58 +04:00
struct tevent_context * ev ;
2009-11-10 21:49:41 +03:00
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_OK ;
2012-05-26 13:45:09 +04:00
if ( smbXcli_conn_has_async_calls ( cli - > conn ) ) {
2009-11-10 21:49:41 +03:00
/*
* Can ' t use sync call while an async call is in flight
*/
status = NT_STATUS_INVALID_PARAMETER ;
goto fail ;
}
2003-06-07 03:09:39 +04:00
2013-02-18 12:08:19 +04:00
ev = samba_tevent_context_init ( frame ) ;
2009-11-10 21:49:41 +03:00
if ( ev = = NULL ) {
status = NT_STATUS_NO_MEMORY ;
goto fail ;
}
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
req = cli_message_send ( frame , ev , cli , host , username , message ) ;
if ( req = = NULL ) {
status = NT_STATUS_NO_MEMORY ;
goto fail ;
2000-04-25 18:04:06 +04:00
}
2009-11-10 21:49:41 +03:00
if ( ! tevent_req_poll ( req , ev ) ) {
status = map_nt_error_from_unix ( errno ) ;
goto fail ;
}
2000-04-25 18:04:06 +04:00
2009-11-10 21:49:41 +03:00
status = cli_message_recv ( req ) ;
fail :
TALLOC_FREE ( frame ) ;
return status ;
2007-12-07 04:16:33 +03:00
}