2006-09-27 14:32:18 +00:00
2005-09-03 16:55:45 +00:00
/*
* Unix SMB / CIFS implementation .
* MS - RPC client library implementation
* Copyright ( C ) Chris Nicholls 2005.
*
* 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 19:25:36 +00:00
* the Free Software Foundation ; either version 3 of the License , or
2005-09-03 16:55:45 +00: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 05:23:25 +00:00
* along with this program ; if not , see < http : //www.gnu.org/licenses/>.
2005-09-03 16:55:45 +00:00
*/
# include "libmsrpc.h"
# include "libmsrpc_internal.h"
# include "libsmbclient.h"
# include "libsmb_internal.h"
2006-12-20 01:10:04 +00:00
int cac_InitHandleData ( CacServerHandle * hnd ) ;
2005-09-03 16:55:45 +00:00
/*this function is based on code found in smbc_init_context() (libsmb/libsmbclient.c)*/
2006-09-27 14:32:18 +00:00
void cac_Init ( int debug )
{
if ( debug < 0 | | debug > 99 )
debug = 0 ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
DEBUGLEVEL = debug ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
setup_logging ( " libmsrpc " , True ) ;
2005-09-03 16:55:45 +00:00
}
2006-09-27 14:32:18 +00:00
int cac_InitHandleMem ( CacServerHandle * hnd )
{
hnd - > username = SMB_MALLOC_ARRAY ( char , sizeof ( fstring ) ) ;
if ( ! hnd - > username )
return CAC_FAILURE ;
hnd - > username [ 0 ] = ' \0 ' ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > domain = SMB_MALLOC_ARRAY ( char , sizeof ( fstring ) ) ;
if ( ! hnd - > domain )
return CAC_FAILURE ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > domain [ 0 ] = ' \0 ' ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > netbios_name = SMB_MALLOC_ARRAY ( char , sizeof ( fstring ) ) ;
if ( ! hnd - > netbios_name )
return CAC_FAILURE ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > netbios_name [ 0 ] = ' \0 ' ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > password = SMB_MALLOC_ARRAY ( char , sizeof ( fstring ) ) ;
if ( ! hnd - > password )
return CAC_FAILURE ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > password [ 0 ] = ' \0 ' ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > server = SMB_MALLOC_ARRAY ( char , sizeof ( fstring ) ) ;
if ( ! hnd - > server )
return CAC_FAILURE ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > server [ 0 ] = ' \0 ' ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
return CAC_SUCCESS ;
2005-09-03 16:55:45 +00:00
}
2006-09-27 14:32:18 +00:00
CacServerHandle * cac_NewServerHandle ( BOOL allocate_fields )
{
CacServerHandle * hnd ;
hnd = SMB_MALLOC_P ( CacServerHandle ) ;
if ( ! hnd ) {
errno = ENOMEM ;
return NULL ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
ZERO_STRUCTP ( hnd ) ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
if ( allocate_fields = = True ) {
if ( ! cac_InitHandleMem ( hnd ) ) {
SAFE_FREE ( hnd ) ;
return NULL ;
}
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > _internal . ctx = smbc_new_context ( ) ;
if ( ! hnd - > _internal . ctx ) {
cac_FreeHandle ( hnd ) ;
return NULL ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > _internal . ctx - > callbacks . auth_fn = cac_GetAuthDataFn ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*add defaults */
hnd - > debug = 0 ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*start at the highest and it will fall down after trying the functions */
hnd - > _internal . srv_level = SRV_WIN_2K3 ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > _internal . user_supplied_ctx = False ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
return hnd ;
2005-09-03 16:55:45 +00:00
}
2006-09-27 14:32:18 +00:00
int cac_InitHandleData ( CacServerHandle * hnd )
{
/*store any automatically initialized values */
if ( ! hnd - > netbios_name ) {
hnd - > netbios_name =
SMB_STRDUP ( hnd - > _internal . ctx - > netbios_name ) ;
} else if ( hnd - > netbios_name [ 0 ] = = ' \0 ' ) {
strncpy ( hnd - > netbios_name , hnd - > _internal . ctx - > netbios_name ,
sizeof ( fstring ) ) ;
}
if ( ! hnd - > username ) {
hnd - > username = SMB_STRDUP ( hnd - > _internal . ctx - > user ) ;
} else if ( hnd - > username [ 0 ] = = ' \0 ' ) {
strncpy ( hnd - > username , hnd - > _internal . ctx - > user ,
sizeof ( fstring ) ) ;
}
if ( ! hnd - > domain ) {
hnd - > domain = SMB_STRDUP ( hnd - > _internal . ctx - > workgroup ) ;
} else if ( hnd - > domain [ 0 ] = = ' \0 ' ) {
strncpy ( hnd - > domain , hnd - > _internal . ctx - > workgroup ,
sizeof ( fstring ) ) ;
}
return CAC_SUCCESS ;
2005-09-03 16:55:45 +00:00
}
2006-09-27 14:32:18 +00:00
void cac_SetAuthDataFn ( CacServerHandle * hnd , smbc_get_auth_data_fn auth_fn )
{
hnd - > _internal . ctx - > callbacks . auth_fn = auth_fn ;
2005-09-03 16:55:45 +00:00
}
2006-09-27 14:32:18 +00:00
void cac_SetSmbcContext ( CacServerHandle * hnd , SMBCCTX * ctx )
{
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
SAFE_FREE ( hnd - > _internal . ctx ) ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > _internal . user_supplied_ctx = True ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
hnd - > _internal . ctx = ctx ;
2005-09-03 16:55:45 +00:00
/*_try_ to avoid any problems that might occur if cac_Connect() isn't called*/
2006-09-27 14:32:18 +00:00
/*cac_InitHandleData(hnd); */
2005-09-03 16:55:45 +00:00
}
/*used internally*/
2006-09-27 14:32:18 +00:00
SMBCSRV * cac_GetServer ( CacServerHandle * hnd )
{
SMBCSRV * srv ;
if ( ! hnd | | ! hnd - > _internal . ctx ) {
return NULL ;
}
srv = smbc_attr_server ( hnd - > _internal . ctx , hnd - > server , " IPC$ " ,
hnd - > domain , hnd - > username , hnd - > password ,
NULL ) ;
if ( ! srv ) {
hnd - > status = NT_STATUS_UNSUCCESSFUL ;
DEBUG ( 1 ,
( " cac_GetServer: Could not find server connection. \n " ) ) ;
}
return srv ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
int cac_Connect ( CacServerHandle * hnd , const char * srv )
{
if ( ! hnd ) {
return CAC_FAILURE ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*these values should be initialized by the user */
if ( ! hnd - > server & & ! srv ) {
return CAC_FAILURE ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*change the server name in the server handle if necessary */
if ( srv & & hnd - > server & & strcmp ( hnd - > server , srv ) = = 0 ) {
SAFE_FREE ( hnd - > server ) ;
hnd - > server = SMB_STRDUP ( srv ) ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*first see if the context has already been setup */
if ( ! ( hnd - > _internal . ctx - > internal - > _initialized ) ) {
hnd - > _internal . ctx - > debug = hnd - > debug ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*initialize the context */
if ( ! smbc_init_context ( hnd - > _internal . ctx ) ) {
return CAC_FAILURE ;
}
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*copy any uninitialized values out of the smbc context into the handle */
if ( ! cac_InitHandleData ( hnd ) ) {
return CAC_FAILURE ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
DEBUG ( 3 , ( " cac_Connect: Username: %s \n " , hnd - > username ) ) ;
DEBUG ( 3 , ( " cac_Connect: Domain: %s \n " , hnd - > domain ) ) ;
DEBUG ( 3 , ( " cac_Connect: Netbios Name: %s \n " , hnd - > netbios_name ) ) ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
if ( ! cac_GetServer ( hnd ) ) {
return CAC_FAILURE ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
return CAC_SUCCESS ;
2005-09-03 16:55:45 +00:00
}
2006-09-27 14:32:18 +00:00
void cac_FreeHandle ( CacServerHandle * hnd )
{
if ( ! hnd )
return ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*only free the context if we created it */
if ( ! hnd - > _internal . user_supplied_ctx ) {
smbc_free_context ( hnd - > _internal . ctx , True ) ;
}
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
SAFE_FREE ( hnd - > netbios_name ) ;
SAFE_FREE ( hnd - > domain ) ;
SAFE_FREE ( hnd - > username ) ;
SAFE_FREE ( hnd - > password ) ;
SAFE_FREE ( hnd - > server ) ;
SAFE_FREE ( hnd ) ;
2005-09-03 16:55:45 +00:00
}
2006-09-27 14:32:18 +00:00
void cac_InitCacTime ( CacTime * cactime , NTTIME nttime )
{
float high , low ;
uint32 sec ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
if ( ! cactime )
return ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
ZERO_STRUCTP ( cactime ) ;
2005-09-03 16:55:45 +00:00
2006-09-27 14:32:18 +00:00
/*this code is taken from display_time() found in rpcclient/cmd_samr.c */
if ( nttime = = 0 )
2005-09-03 16:55:45 +00:00
return ;
2006-09-27 14:32:18 +00:00
if ( nttime = = 0x80000000000000LL )
2005-09-03 16:55:45 +00:00
return ;
2006-09-27 14:32:18 +00:00
high = 65536 ;
high = high / 10000 ;
high = high * 65536 ;
high = high / 1000 ;
high = high * ( ~ ( nttime > > 32 ) ) ;
low = ~ ( nttime & 0xFFFFFFFF ) ;
low = low / ( 1000 * 1000 * 10 ) ;
sec = high + low ;
cactime - > days = sec / ( 60 * 60 * 24 ) ;
cactime - > hours =
( sec - ( cactime - > days * 60 * 60 * 24 ) ) / ( 60 * 60 ) ;
cactime - > minutes =
( sec - ( cactime - > days * 60 * 60 * 24 ) -
( cactime - > hours * 60 * 60 ) ) / 60 ;
cactime - > seconds =
sec - ( cactime - > days * 60 * 60 * 24 ) -
( cactime - > hours * 60 * 60 ) - ( cactime - > minutes * 60 ) ;
2005-09-03 16:55:45 +00:00
}
2006-09-27 14:32:18 +00:00
void cac_GetAuthDataFn ( const char * pServer ,
const char * pShare ,
char * pWorkgroup ,
int maxLenWorkgroup ,
char * pUsername ,
int maxLenUsername ,
char * pPassword , int maxLenPassword )
2005-09-03 16:55:45 +00:00
{
2006-09-27 14:32:18 +00:00
char temp [ sizeof ( fstring ) ] ;
static char authUsername [ sizeof ( fstring ) ] ;
static char authWorkgroup [ sizeof ( fstring ) ] ;
static char authPassword [ sizeof ( fstring ) ] ;
static char authSet = 0 ;
char * pass = NULL ;
if ( authSet ) {
strncpy ( pWorkgroup , authWorkgroup , maxLenWorkgroup - 1 ) ;
strncpy ( pUsername , authUsername , maxLenUsername - 1 ) ;
strncpy ( pPassword , authPassword , maxLenPassword - 1 ) ;
} else {
d_printf ( " Domain: [%s] " , pWorkgroup ) ;
fgets ( temp , sizeof ( fstring ) , stdin ) ;
if ( temp [ strlen ( temp ) - 1 ] = = ' \n ' ) { /* A new line? */
temp [ strlen ( temp ) - 1 ] = ' \0 ' ;
}
if ( temp [ 0 ] ! = ' \0 ' ) {
strncpy ( pWorkgroup , temp , maxLenWorkgroup - 1 ) ;
strncpy ( authWorkgroup , temp , maxLenWorkgroup - 1 ) ;
}
d_printf ( " Username: [%s] " , pUsername ) ;
fgets ( temp , sizeof ( fstring ) , stdin ) ;
if ( temp [ strlen ( temp ) - 1 ] = = ' \n ' ) { /* A new line? */
temp [ strlen ( temp ) - 1 ] = ' \0 ' ;
}
if ( temp [ 0 ] ! = ' \0 ' ) {
strncpy ( pUsername , temp , maxLenUsername - 1 ) ;
strncpy ( authUsername , pUsername ,
maxLenUsername - 1 ) ;
}
pass = getpass ( " Password: " ) ;
if ( pass )
fstrcpy ( temp , pass ) ;
if ( temp [ strlen ( temp ) - 1 ] = = ' \n ' ) { /* A new line? */
temp [ strlen ( temp ) - 1 ] = ' \0 ' ;
}
if ( temp [ 0 ] ! = ' \0 ' ) {
strncpy ( pPassword , temp , maxLenPassword - 1 ) ;
strncpy ( authPassword , pPassword ,
maxLenPassword - 1 ) ;
}
authSet = 1 ;
}
2005-09-03 16:55:45 +00:00
}