move bindconf_[un]parse() into generic API

This commit is contained in:
Pierangelo Masarati 2005-11-06 23:28:54 +00:00
parent a6453f28f8
commit fc2912ba92
3 changed files with 76 additions and 24 deletions

View file

@ -991,15 +991,7 @@ static slap_verbmasks methkey[] = {
{ BER_BVNULL, 0 }
};
typedef struct cf_aux_table {
struct berval key;
int off;
char type;
char quote;
slap_verbmasks *aux;
} cf_aux_table;
static cf_aux_table bindkey[] = {
static slap_cf_aux_table bindkey[] = {
{ BER_BVC("starttls="), offsetof(slap_bindconf, sb_tls), 'd', 0, tlskey },
{ BER_BVC("bindmethod="), offsetof(slap_bindconf, sb_method), 'd', 0, methkey },
{ BER_BVC("binddn="), offsetof(slap_bindconf, sb_binddn), 'b', 1, NULL },
@ -1012,31 +1004,34 @@ static cf_aux_table bindkey[] = {
{ BER_BVNULL, 0, 0, 0, NULL }
};
int bindconf_parse( const char *word, slap_bindconf *bc ) {
int
slap_cf_aux_table_parse( const char *word, void *dst, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg )
{
int rc = 0;
cf_aux_table *tab;
slap_cf_aux_table *tab;
for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
if ( !strncasecmp( word, tab->key.bv_val, tab->key.bv_len )) {
char **cptr;
char **cptr, *next;
int *iptr, j;
unsigned *uptr;
struct berval *bptr;
const char *val = word + tab->key.bv_len;
switch ( tab->type ) {
case 's':
cptr = (char **)((char *)bc + tab->off);
cptr = (char **)((char *)dst + tab->off);
*cptr = ch_strdup( val );
break;
case 'b':
bptr = (struct berval *)((char *)bc + tab->off);
bptr = (struct berval *)((char *)dst + tab->off);
ber_str2bv( val, 0, 1, bptr );
break;
case 'd':
assert( tab->aux != NULL );
iptr = (int *)((char *)bc + tab->off);
iptr = (int *)((char *)dst + tab->off);
rc = 1;
for ( j = 0; !BER_BVISNULL( &tab->aux[j].word ); j++ ) {
@ -1046,11 +1041,29 @@ int bindconf_parse( const char *word, slap_bindconf *bc ) {
}
}
break;
case 'i':
iptr = (int *)((char *)dst + tab->off);
*iptr = strtol( val, &next, 0 );
if ( next == val || next[ 0 ] != '\0' ) {
rc = 1;
}
break;
case 'u':
uptr = (unsigned *)((char *)dst + tab->off);
*uptr = strtoul( val, &next, 0 );
if ( next == val || next[ 0 ] != '\0' ) {
rc = 1;
}
break;
}
if ( rc ) {
Debug( LDAP_DEBUG_ANY, "invalid bind config value %s\n",
word, 0, 0 );
Debug( LDAP_DEBUG_ANY, "invalid %s value %s\n",
tabmsg, word, 0 );
}
return rc;
@ -1060,22 +1073,25 @@ int bindconf_parse( const char *word, slap_bindconf *bc ) {
return rc;
}
int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
int
slap_cf_aux_table_unparse( void *src, struct berval *bv, slap_cf_aux_table *tab0 )
{
char buf[BUFSIZ], *ptr;
cf_aux_table *tab;
slap_cf_aux_table *tab;
struct berval tmp;
ptr = buf;
for (tab = bindkey; !BER_BVISNULL(&tab->key); tab++) {
for (tab = tab0; !BER_BVISNULL(&tab->key); tab++ ) {
char **cptr;
int *iptr, i;
unsigned *uptr;
struct berval *bptr;
cptr = (char **)((char *)bc + tab->off);
cptr = (char **)((char *)src + tab->off);
switch ( tab->type ) {
case 'b':
bptr = (struct berval *)((char *)bc + tab->off);
bptr = (struct berval *)((char *)src + tab->off);
cptr = &bptr->bv_val;
case 's':
if ( *cptr ) {
@ -1089,7 +1105,7 @@ int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
case 'd':
assert( tab->aux != NULL );
iptr = (int *)((char *)bc + tab->off);
iptr = (int *)((char *)src + tab->off);
for ( i = 0; !BER_BVISNULL( &tab->aux[i].word ); i++ ) {
if ( *iptr == tab->aux[i].mask ) {
@ -1100,6 +1116,20 @@ int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
}
}
break;
case 'i':
iptr = (int *)((char *)src + tab->off);
*ptr++ = ' ';
ptr = lutil_strcopy( ptr, tab->key.bv_val );
ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%d", *iptr );
break;
case 'u':
uptr = (int *)((char *)src + tab->off);
*ptr++ = ' ';
ptr = lutil_strcopy( ptr, tab->key.bv_val );
ptr += snprintf( ptr, sizeof( buf ) - ( ptr - buf ), "%u", *uptr );
break;
}
}
tmp.bv_val = buf;
@ -1108,6 +1138,18 @@ int bindconf_unparse( slap_bindconf *bc, struct berval *bv ) {
return 0;
}
int
bindconf_parse( const char *word, slap_bindconf *bc )
{
return slap_cf_aux_table_parse( word, bc, bindkey, "bind config" );
}
int
bindconf_unparse( slap_bindconf *bc, struct berval *bv )
{
return slap_cf_aux_table_unparse( bc, bv, bindkey );
}
void bindconf_free( slap_bindconf *bc ) {
if ( !BER_BVISNULL( &bc->sb_binddn ) ) {
ch_free( bc->sb_binddn.bv_val );

View file

@ -436,6 +436,8 @@ LDAP_SLAPD_F (int) str2loglevel LDAP_P(( const char *s, int *l ));
LDAP_SLAPD_F (int) loglevel2bvarray LDAP_P(( int l, BerVarray *bva ));
LDAP_SLAPD_F (const char *) loglevel2str LDAP_P(( int l ));
LDAP_SLAPD_F (int) loglevel2bv LDAP_P(( int l, struct berval *bv ));
LDAP_SLAPD_F (int) slap_cf_aux_table_parse LDAP_P(( const char *word, void *bc, slap_cf_aux_table *tab0, LDAP_CONST char *tabmsg ));
LDAP_SLAPD_F (int) slap_cf_aux_table_unparse LDAP_P(( void *bc, struct berval *bv, slap_cf_aux_table *tab0 ));
/*
* ch_malloc.c

View file

@ -1607,6 +1607,14 @@ typedef struct slap_verbmasks {
const slap_mask_t mask;
} slap_verbmasks;
typedef struct slap_cf_aux_table {
struct berval key;
int off;
char type;
char quote;
slap_verbmasks *aux;
} slap_cf_aux_table;
#define SLAP_LIMIT_TIME 1
#define SLAP_LIMIT_SIZE 2