2010-11-01 10:43:46 +03:00
/*
ldb database library - samba extensions
Copyright ( C ) Andrew Tridgell 2010
* * NOTE ! The following LGPL license applies to the ldb
* * library . This does NOT imply that all of Samba is released
* * under the LGPL
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 3 of the License , or ( at your option ) any later version .
This library 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
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
# include "ldb_module.h"
2020-12-02 20:06:24 +03:00
# include "lib/cmdline/cmdline.h"
2010-11-01 10:43:46 +03:00
# include "auth/gensec/gensec.h"
# include "auth/auth.h"
# include "param/param.h"
# include "dsdb/samdb/samdb.h"
2018-05-31 06:12:46 +03:00
# include "dsdb/common/util.h"
2010-11-01 10:43:46 +03:00
# include "ldb_wrap.h"
# include "popt.h"
2020-12-17 21:16:13 +03:00
static bool is_popt_table_end ( const struct poptOption * o )
{
if ( o - > longName = = NULL & &
o - > shortName = = ' \0 ' & &
o - > arg = = NULL ) {
return true ;
}
return false ;
}
2010-11-01 10:43:46 +03:00
/*
work out the length of a popt array
*/
2020-12-17 21:16:13 +03:00
static size_t calculate_popt_array_length ( struct poptOption * opts )
2010-11-01 10:43:46 +03:00
{
2020-12-17 21:16:13 +03:00
size_t i = 0 ;
for ( i = 0 ; i < UINT32_MAX ; i + + ) {
struct poptOption * o = & ( opts [ i ] ) ;
if ( is_popt_table_end ( o ) ) {
break ;
}
}
2010-11-01 10:43:46 +03:00
return i ;
}
/*
called to register additional command line options
*/
static int extensions_hook ( struct ldb_context * ldb , enum ldb_module_hook_type t )
{
switch ( t ) {
case LDB_MODULE_HOOK_CMDLINE_OPTIONS : {
2020-12-17 21:16:13 +03:00
size_t len1 , len2 ;
2010-11-02 02:41:28 +03:00
struct poptOption * * popt_options = ldb_module_popt_options ( ldb ) ;
2020-12-02 20:06:24 +03:00
struct poptOption * new_array = NULL ;
bool ok ;
struct poptOption cmdline_extensions [ ] = {
POPT_COMMON_SAMBA_LDB
POPT_COMMON_CONNECTION
POPT_COMMON_CREDENTIALS
POPT_LEGACY_S4
POPT_COMMON_VERSION
POPT_TABLEEND
} ;
ok = samba_cmdline_init ( ldb ,
SAMBA_CMDLINE_CONFIG_CLIENT ,
false /* require_smbconf */ ) ;
if ( ! ok ) {
return ldb_oom ( ldb ) ;
}
2010-11-01 10:43:46 +03:00
2010-11-02 02:41:28 +03:00
len1 = calculate_popt_array_length ( * popt_options ) ;
2010-11-01 10:43:46 +03:00
len2 = calculate_popt_array_length ( cmdline_extensions ) ;
2020-12-18 10:38:22 +03:00
new_array = talloc_array ( ldb ,
struct poptOption ,
len1 + len2 + 1 ) ;
2010-11-01 10:43:46 +03:00
if ( NULL = = new_array ) {
return ldb_oom ( ldb ) ;
}
2010-11-02 02:41:28 +03:00
memcpy ( new_array , * popt_options , len1 * sizeof ( struct poptOption ) ) ;
2010-11-01 10:43:46 +03:00
memcpy ( new_array + len1 , cmdline_extensions , ( 1 + len2 ) * sizeof ( struct poptOption ) ) ;
2020-12-02 20:06:24 +03:00
# ifdef DEVELOPER
ok = samba_cmdline_sanity_check ( new_array ) ;
if ( ! ok ) {
talloc_free ( new_array ) ;
return ldb_error ( ldb ,
LDB_ERR_OPERATIONS_ERROR ,
" Duplicate cmdline options detected! " ) ;
}
# endif
2010-11-02 02:41:28 +03:00
( * popt_options ) = new_array ;
2010-11-01 10:43:46 +03:00
return LDB_SUCCESS ;
}
case LDB_MODULE_HOOK_CMDLINE_PRECONNECT : {
2020-12-02 20:06:24 +03:00
struct loadparm_context * lp_ctx = NULL ;
struct cli_credentials * creds = NULL ;
2010-11-01 10:43:46 +03:00
int r = ldb_register_samba_handlers ( ldb ) ;
if ( r ! = LDB_SUCCESS ) {
return ldb_operr ( ldb ) ;
}
2011-06-06 08:58:28 +04:00
gensec_init ( ) ;
2010-11-01 10:43:46 +03:00
2020-12-02 20:06:24 +03:00
lp_ctx = samba_cmdline_get_lp_ctx ( ) ;
creds = samba_cmdline_get_creds ( ) ;
2018-05-31 06:12:46 +03:00
if ( ldb_set_opaque (
ldb ,
DSDB_SESSION_INFO ,
2020-12-02 20:06:24 +03:00
system_session ( lp_ctx ) ) ) {
2018-05-31 06:12:46 +03:00
2010-11-01 10:43:46 +03:00
return ldb_operr ( ldb ) ;
}
2020-12-02 20:06:24 +03:00
if ( ldb_set_opaque ( ldb , " credentials " , creds ) ) {
2010-11-01 10:43:46 +03:00
return ldb_operr ( ldb ) ;
}
2020-12-02 20:06:24 +03:00
if ( ldb_set_opaque ( ldb , " loadparm " , lp_ctx ) ) {
2010-11-01 10:43:46 +03:00
return ldb_operr ( ldb ) ;
}
ldb_set_utf8_fns ( ldb , NULL , wrap_casefold ) ;
break ;
}
case LDB_MODULE_HOOK_CMDLINE_POSTCONNECT :
/* get the domain SID into the cache for SDDL processing */
samdb_domain_sid ( ldb ) ;
break ;
}
return LDB_SUCCESS ;
}
/*
initialise the module
*/
_PUBLIC_ int ldb_samba_extensions_init ( const char * ldb_version )
{
ldb_register_hook ( extensions_hook ) ;
return LDB_SUCCESS ;
}