1998-11-05 19:51:34 +03:00
/*
2002-01-30 09:08:46 +03:00
* Unix SMB / CIFS implementation .
* SMB parameters and setup
1998-11-05 19:51:34 +03:00
* Copyright ( C ) Andrew Tridgell 1992 - 1998 Modified by Jeremy Allison 1995.
2016-02-16 16:59:53 +03:00
*
1998-11-05 19:51:34 +03: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
2007-07-09 23:25:36 +04:00
* Software Foundation ; either version 3 of the License , or ( at your option )
1998-11-05 19:51:34 +03:00
* any later version .
2016-02-16 16:59:53 +03:00
*
1998-11-05 19:51:34 +03: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 .
2016-02-16 16:59:53 +03:00
*
1998-11-05 19:51:34 +03:00
* You should have received a copy of the GNU General Public License along with
2007-07-10 09:23:25 +04:00
* this program ; if not , see < http : //www.gnu.org/licenses/>.
1998-11-05 19:51:34 +03:00
*/
2016-02-16 19:09:43 +03:00
# include "replace.h"
2024-04-10 14:02:39 +03:00
# include "lib/util/util_file.h"
# include "source3/lib/util_file.h"
2016-02-16 19:09:43 +03:00
# include "lib/util/debug.h"
# include "lib/util/samba_util.h"
2015-10-12 16:57:34 +03:00
# include "lib/util/sys_rw.h"
2018-12-27 12:18:28 +03:00
# include "lib/util/sys_popen.h"
2016-02-16 19:01:04 +03:00
# include "lib/async_req/async_sock.h"
# include "lib/util/tevent_unix.h"
2019-05-24 20:08:10 +03:00
struct file_ploadv_state {
2016-02-16 19:01:04 +03:00
struct tevent_context * ev ;
2021-02-13 13:40:34 +03:00
struct tevent_req * subreq ;
2016-02-16 19:01:04 +03:00
size_t maxsize ;
int fd ;
uint8_t * buf ;
} ;
2021-02-13 13:40:34 +03:00
static void file_ploadv_cleanup_fn (
struct tevent_req * req , enum tevent_req_state req_state ) ;
2019-05-24 20:08:10 +03:00
static void file_ploadv_readable ( struct tevent_req * subreq ) ;
2016-02-16 19:01:04 +03:00
2019-05-18 21:18:19 +03:00
struct tevent_req * file_ploadv_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
char * const argl [ ] , size_t maxsize )
{
2021-02-13 13:40:34 +03:00
struct tevent_req * req = NULL ;
2019-05-24 20:08:10 +03:00
struct file_ploadv_state * state = NULL ;
2019-05-18 21:18:19 +03:00
2019-05-24 20:08:10 +03:00
req = tevent_req_create ( mem_ctx , & state , struct file_ploadv_state ) ;
2019-05-18 21:18:19 +03:00
if ( req = = NULL ) {
return NULL ;
}
state - > ev = ev ;
state - > maxsize = maxsize ;
state - > fd = sys_popenv ( argl ) ;
if ( state - > fd = = - 1 ) {
tevent_req_error ( req , errno ) ;
return tevent_req_post ( req , ev ) ;
}
2021-02-13 13:40:34 +03:00
tevent_req_set_cleanup_fn ( req , file_ploadv_cleanup_fn ) ;
2019-05-18 21:18:19 +03:00
2021-02-13 13:40:34 +03:00
state - > subreq = wait_for_read_send ( state , state - > ev , state - > fd , false ) ;
if ( tevent_req_nomem ( state - > subreq , req ) ) {
2019-05-18 21:18:19 +03:00
return tevent_req_post ( req , ev ) ;
}
2021-02-13 13:40:34 +03:00
tevent_req_set_callback ( state - > subreq , file_ploadv_readable , req ) ;
2019-05-18 21:18:19 +03:00
return req ;
}
2021-02-13 13:40:34 +03:00
static void file_ploadv_cleanup_fn (
struct tevent_req * req , enum tevent_req_state req_state )
2016-02-16 19:01:04 +03:00
{
2021-02-13 13:40:34 +03:00
struct file_ploadv_state * state = tevent_req_data (
req , struct file_ploadv_state ) ;
TALLOC_FREE ( state - > subreq ) ;
if ( state - > fd ! = - 1 ) {
sys_pclose ( state - > fd ) ;
state - > fd = - 1 ;
2016-02-16 19:01:04 +03:00
}
}
2019-05-24 20:08:10 +03:00
static void file_ploadv_readable ( struct tevent_req * subreq )
2016-02-16 19:01:04 +03:00
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
2019-05-24 20:08:10 +03:00
struct file_ploadv_state * state = tevent_req_data (
req , struct file_ploadv_state ) ;
2016-02-16 19:01:04 +03:00
uint8_t buf [ 1024 ] ;
uint8_t * tmp ;
ssize_t nread ;
size_t bufsize ;
int err ;
bool ok ;
ok = wait_for_read_recv ( subreq , & err ) ;
TALLOC_FREE ( subreq ) ;
2021-02-13 13:40:34 +03:00
state - > subreq = NULL ;
2016-02-16 19:01:04 +03:00
if ( ! ok ) {
tevent_req_error ( req , err ) ;
return ;
}
nread = sys_read ( state - > fd , buf , sizeof ( buf ) ) ;
if ( nread = = - 1 ) {
tevent_req_error ( req , errno ) ;
return ;
}
if ( nread = = 0 ) {
tevent_req_done ( req ) ;
return ;
}
bufsize = talloc_get_size ( state - > buf ) ;
2021-01-26 18:39:29 +03:00
if ( bufsize > 0 ) {
/*
* Last round we ' ve added the trailing ' \0 ' . Remove it
* for this round .
*/
bufsize - = 1 ;
}
2016-02-16 19:01:04 +03:00
if ( ( ( bufsize + nread ) < bufsize ) | |
( ( bufsize + nread + 1 ) < bufsize ) ) {
/* overflow */
tevent_req_error ( req , EMSGSIZE ) ;
return ;
}
if ( ( state - > maxsize ! = 0 ) & & ( ( bufsize + nread ) > state - > maxsize ) ) {
tevent_req_error ( req , EMSGSIZE ) ;
return ;
}
tmp = talloc_realloc ( state , state - > buf , uint8_t , bufsize + nread + 1 ) ;
if ( tevent_req_nomem ( tmp , req ) ) {
return ;
}
state - > buf = tmp ;
memcpy ( state - > buf + bufsize , buf , nread ) ;
state - > buf [ bufsize + nread ] = ' \0 ' ;
2021-02-13 13:40:34 +03:00
state - > subreq = wait_for_read_send ( state , state - > ev , state - > fd , false ) ;
if ( tevent_req_nomem ( state - > subreq , req ) ) {
2016-02-16 19:01:04 +03:00
return ;
}
2021-02-13 13:40:34 +03:00
tevent_req_set_callback ( state - > subreq , file_ploadv_readable , req ) ;
2016-02-16 19:01:04 +03:00
}
2019-05-24 20:08:10 +03:00
int file_ploadv_recv ( struct tevent_req * req , TALLOC_CTX * mem_ctx ,
2016-02-16 19:01:04 +03:00
uint8_t * * buf )
{
2019-05-24 20:08:10 +03:00
struct file_ploadv_state * state = tevent_req_data (
req , struct file_ploadv_state ) ;
2016-02-16 19:01:04 +03:00
int err ;
if ( tevent_req_is_unix_error ( req , & err ) ) {
return err ;
}
* buf = talloc_move ( mem_ctx , & state - > buf ) ;
tevent_req_received ( req ) ;
return 0 ;
}
1998-11-05 19:51:34 +03:00
2001-04-13 04:37:00 +04:00
2008-10-12 19:34:43 +04:00
/**
2004-09-01 05:33:55 +04:00
Load a pipe into memory and return an array of pointers to lines in the data
2014-12-26 17:42:40 +03:00
must be freed with TALLOC_FREE .
2008-10-12 19:34:43 +04:00
* */
2004-09-01 05:33:55 +04:00
2019-05-17 08:10:51 +03:00
char * * file_lines_ploadv ( TALLOC_CTX * mem_ctx ,
char * const argl [ ] ,
int * numlines )
{
char * p = NULL ;
size_t size ;
p = file_ploadv ( argl , & size ) ;
if ( ! p ) {
return NULL ;
}
return file_lines_parse ( p , size , numlines , mem_ctx ) ;
}