Commit 93abccde authored by Pierangelo Masarati's avatar Pierangelo Masarati
Browse files

group rewrite/map stuff in one structure and optimize more function calls

parent 77c4389f
......@@ -74,13 +74,12 @@ ldap_back_add(
/*
* Rewrite the add dn, if needed
*/
dc.rwmap = &li->rwmap;
#ifdef ENABLE_REWRITE
dc.rw = li->rwinfo;
dc.conn = op->o_conn;
dc.rs = rs;
dc.ctx = "addDn";
#else
dc.li = li;
dc.tofrom = 1;
dc.normalized = 0;
#endif
......@@ -104,7 +103,7 @@ ldap_back_add(
continue;
}
ldap_back_map(&li->at_map, &a->a_desc->ad_cname, &mapped,
ldap_back_map(&li->rwmap.rwm_at, &a->a_desc->ad_cname, &mapped,
BACKLDAP_MAP);
if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
continue;
......
......@@ -73,6 +73,25 @@ struct ldapmapping {
struct berval dst;
};
struct ldaprwmap {
/*
* DN rewriting
*/
#ifdef ENABLE_REWRITE
struct rewrite_info *rwm_rw;
#else /* !ENABLE_REWRITE */
/* some time the suffix massaging without librewrite
* will be disabled */
BerVarray rwm_suffix_massage;
#endif /* !ENABLE_REWRITE */
/*
* Attribute/objectClass mapping
*/
struct ldapmap rwm_oc;
struct ldapmap rwm_at;
};
struct ldapinfo {
struct slap_backend_db *be;
char *url;
......@@ -81,6 +100,8 @@ struct ldapinfo {
ldap_pvt_thread_mutex_t conn_mutex;
int savecred;
Avlnode *conntree;
#if 0
#ifdef ENABLE_REWRITE
struct rewrite_info *rwinfo;
#else /* !ENABLE_REWRITE */
......@@ -89,29 +110,32 @@ struct ldapinfo {
struct ldapmap oc_map;
struct ldapmap at_map;
};
#endif
struct ldapconn *ldap_back_getconn(struct slap_op *op, struct slap_rep *rs);
int ldap_back_dobind(struct ldapconn *lc, Operation *op, SlapReply *rs);
int ldap_back_map_result(SlapReply *rs);
int ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
ber_int_t msgid, int sendok);
int back_ldap_LTX_init_module(int argc, char *argv[]);
struct ldaprwmap rwmap;
};
/* Whatever context ldap_back_dn_massage needs... */
typedef struct dncookie {
struct ldaprwmap *rwmap;
#ifdef ENABLE_REWRITE
struct rewrite_info *rw;
Connection *conn;
char *ctx;
SlapReply *rs;
#else
struct ldapinfo *li;
int normalized;
int tofrom;
#endif
} dncookie;
struct ldapconn *ldap_back_getconn(struct slap_op *op, struct slap_rep *rs);
int ldap_back_dobind(struct ldapconn *lc, Operation *op, SlapReply *rs);
int ldap_back_map_result(SlapReply *rs);
int ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
ber_int_t msgid, int sendok);
int back_ldap_LTX_init_module(int argc, char *argv[]);
int ldap_back_dn_massage(dncookie *dc, struct berval *dn,
struct berval *res);
......@@ -155,13 +179,8 @@ extern int ldap_back_map_config(
char **argv );
extern int
ldap_back_filter_map_rewrite_(
#ifdef ENABLE_REWRITE
struct rewrite_info *info,
void *cookie,
#endif /* ENABLE_REWRITE */
struct ldapmap *at_map,
struct ldapmap *oc_map,
ldap_back_filter_map_rewrite(
dncookie *dc,
Filter *f,
struct berval *fstr,
int remap );
......
......@@ -72,13 +72,12 @@ ldap_back_bind(
/*
* Rewrite the bind dn if needed
*/
dc.rwmap = &li->rwmap;
#ifdef ENABLE_REWRITE
dc.rw = li->rwinfo;
dc.conn = op->o_conn;
dc.rs = rs;
dc.ctx = "bindDn";
#else
dc.li = li;
dc.tofrom = 1;
dc.normalized = 0;
#endif
......@@ -290,7 +289,7 @@ ldap_back_getconn(Operation *op, SlapReply *rs)
* since we may have different entries
* for the same connection
*/
( void )rewrite_session_init( li->rwinfo, op->o_conn );
( void )rewrite_session_init( li->rwmap.rwm_rw, op->o_conn );
#endif /* ENABLE_REWRITE */
ldap_pvt_thread_mutex_init( &lc->lc_mutex );
......@@ -312,8 +311,8 @@ ldap_back_getconn(Operation *op, SlapReply *rs)
/*
* Rewrite the bind dn if needed
*/
dc.rwmap = &li->rwmap;
#ifdef ENABLE_REWRITE
dc.rw = li->rwinfo;
dc.conn = op->o_conn;
dc.rs = rs;
dc.ctx = "bindDn";
......@@ -503,13 +502,12 @@ ldap_back_op_result(struct ldapconn *lc, Operation *op, SlapReply *rs,
struct berval dn, mdn;
dncookie dc;
dc.rwmap = &li->rwmap;
#ifdef ENABLE_REWRITE
dc.rw = li->rwinfo;
dc.conn = op->o_conn;
dc.rs = rs;
dc.ctx = "matchedDn";
#else
dc.li = li;
dc.tofrom = 0;
dc.normalized = 0;
#endif
......
......@@ -66,13 +66,12 @@ ldap_back_compare(
/*
* Rewrite the compare dn, if needed
*/
dc.rwmap = &li->rwmap;
#ifdef ENABLE_REWRITE
dc.rw = li->rwinfo;
dc.conn = op->o_conn;
dc.rs = rs;
dc.ctx = "compareDn";
#else
dc.li = li;
dc.tofrom = 1;
dc.normalized = 0;
#endif
......@@ -82,14 +81,15 @@ ldap_back_compare(
}
if ( op->oq_compare.rs_ava->aa_desc == slap_schema.si_ad_objectClass ) {
ldap_back_map(&li->oc_map, &op->orc_ava->aa_value, &mapped_val,
BACKLDAP_MAP);
ldap_back_map(&li->rwmap.rwm_oc, &op->orc_ava->aa_value,
&mapped_val, BACKLDAP_MAP);
if (mapped_val.bv_val == NULL || mapped_val.bv_val[0] == '\0') {
return( -1 );
}
mapped_at = op->orc_ava->aa_desc->ad_cname;
} else {
ldap_back_map(&li->at_map, &op->orc_ava->aa_desc->ad_cname, &mapped_at,
ldap_back_map(&li->rwmap.rwm_at,
&op->orc_ava->aa_desc->ad_cname, &mapped_at,
BACKLDAP_MAP);
if (mapped_at.bv_val == NULL || mapped_at.bv_val[0] == '\0') {
return( -1 );
......
......@@ -211,7 +211,8 @@ ldap_back_db_config(
* FIXME: no extra rewrite capabilities should be added
* to the database
*/
rc = suffix_massage_config( li->rwinfo, &pvnc, &nvnc, &prnc, &nrnc );
rc = suffix_massage_config( li->rwmap.rwm_rw,
&pvnc, &nvnc, &prnc, &nrnc );
free( nvnc.bv_val );
free( pvnc.bv_val );
free( nrnc.bv_val );
......@@ -230,7 +231,8 @@ ldap_back_db_config(
/* rewrite stuff ... */
} else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
#ifdef ENABLE_REWRITE
return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
return rewrite_parse( li->rwmap.rwm_rw,
fname, lineno, argc, argv );
#else /* !ENABLE_REWRITE */
fprintf( stderr, "%s: line %d: rewrite capabilities "
......@@ -239,7 +241,8 @@ ldap_back_db_config(
/* objectclass/attribute mapping */
} else if ( strcasecmp( argv[0], "map" ) == 0 ) {
return ldap_back_map_config( &li->oc_map, &li->at_map,
return ldap_back_map_config( &li->rwmap.rwm_oc,
&li->rwmap.rwm_at,
fname, lineno, argc, argv );
/* anything else */
......
......@@ -66,13 +66,12 @@ ldap_back_delete(
/*
* Rewrite the request dn, if needed
*/
dc.rwmap = &li->rwmap;
#ifdef ENABLE_REWRITE
dc.rw = li->rwinfo;
dc.conn = op->o_conn;
dc.rs = rs;
dc.ctx = "deleteDn";
#else
dc.li = li;
dc.tofrom = 1;
dc.normalized = 0;
#endif
......
......@@ -85,13 +85,12 @@ ldap_back_exop_passwd(
return LDAP_UNWILLING_TO_PERFORM;
}
if (id.bv_len) {
dc.rwmap = &li->rwmap;
#ifdef ENABLE_REWRITE
dc.rw = li->rwinfo;
dc.conn = op->o_conn;
dc.rs = rs;
dc.ctx = "modifyPwd";
#else
dc.li = li;
dc.tofrom = 1;
dc.normalized = 0;
#endif
......
......@@ -117,8 +117,8 @@ ldap_back_db_init(
li->bindpw.bv_len = 0;
#ifdef ENABLE_REWRITE
li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
if ( li->rwinfo == NULL ) {
li->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
if ( li->rwmap.rwm_rw == NULL ) {
ch_free( li );
return -1;
}
......@@ -126,7 +126,8 @@ ldap_back_db_init(
ldap_pvt_thread_mutex_init( &li->conn_mutex );
ldap_back_map_init( &li->at_map, &mapping );
ldap_back_map_init( &li->rwmap.rwm_oc, &mapping );
ldap_back_map_init( &li->rwmap.rwm_at, &mapping );
li->be = be;
be->be_private = li;
......@@ -192,8 +193,8 @@ ldap_back_db_destroy(
avl_free( li->conntree, ldap_back_conn_free );
}
#ifdef ENABLE_REWRITE
if (li->rwinfo) {
rewrite_info_delete( li->rwinfo );
if (li->rwmap.rwm_rw) {
rewrite_info_delete( li->rwmap.rwm_rw );
}
#else /* !ENABLE_REWRITE */
if (li->suffix_massage) {
......@@ -201,10 +202,10 @@ ldap_back_db_destroy(
}
#endif /* !ENABLE_REWRITE */
avl_free( li->oc_map.remap, NULL );
avl_free( li->oc_map.map, mapping_free );
avl_free( li->at_map.remap, NULL );
avl_free( li->at_map.map, mapping_free );
avl_free( li->rwmap.rwm_oc.remap, NULL );
avl_free( li->rwmap.rwm_oc.map, mapping_free );
avl_free( li->rwmap.rwm_at.remap, NULL );
avl_free( li->rwmap.rwm_at.map, mapping_free );
ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
......
......@@ -118,102 +118,6 @@ ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
return;
}
char *
ldap_back_map_filter(
struct ldapmap *at_map,
struct ldapmap *oc_map,
struct berval *f,
int remap
)
{
char *nf, *p, *q, *s, c;
int len, extra, plen, in_quote;
struct berval m, tmp;
if (f == NULL)
return(NULL);
len = f->bv_len;
extra = len;
len *= 2;
nf = ch_malloc( len + 1 );
if (nf == NULL)
return(NULL);
/* this loop assumes the filter ends with one
* of the delimiter chars -- probably ')'.
*/
s = nf;
q = NULL;
in_quote = 0;
for (p = f->bv_val; (c = *p); p++) {
if (c == '"') {
in_quote = !in_quote;
if (q != NULL) {
plen = p - q;
AC_MEMCPY(s, q, plen);
s += plen;
q = NULL;
}
*s++ = c;
} else if (in_quote) {
/* ignore everything in quotes --
* what about attrs in DNs?
*/
*s++ = c;
} else if (c != '(' && c != ')'
&& c != '=' && c != '>' && c != '<'
&& c != '|' && c != '&')
{
if (q == NULL)
q = p;
} else {
if (q != NULL) {
*p = 0;
tmp.bv_len = p - q;
tmp.bv_val = q;
ldap_back_map(at_map, &tmp, &m, remap);
if (m.bv_val == NULL || m.bv_val[0] == '\0') {
/*
* FIXME: are we sure we need to search
* oc_map if at_map fails?
*/
ldap_back_map(oc_map, &tmp, &m, remap);
if (m.bv_val == NULL || m.bv_val[0] == '\0') {
m = tmp;
}
}
extra += p - q;
plen = m.bv_len;
extra -= plen;
if (extra < 0) {
char *tmpnf;
while (extra < 0) {
extra += len;
len *= 2;
}
s -= (long)nf;
tmpnf = ch_realloc(nf, len + 1);
if (tmpnf == NULL) {
ch_free(nf);
return(NULL);
}
nf = tmpnf;
s += (long)nf;
}
AC_MEMCPY(s, m.bv_val, plen);
s += plen;
*p = c;
q = NULL;
}
*s++ = c;
}
}
*s = 0;
return(nf);
}
int
ldap_back_map_attrs(
struct ldapmap *at_map,
......@@ -254,14 +158,9 @@ ldap_back_map_attrs(
return LDAP_SUCCESS;
}
#ifdef ENABLE_REWRITE
static int
map_attr_value_(
struct rewrite_info *info,
void *cookie,
struct ldapmap *at_map,
struct ldapmap *oc_map,
int
map_attr_value(
dncookie *dc,
AttributeDescription *ad,
struct berval *mapped_attr,
struct berval *value,
......@@ -271,12 +170,12 @@ map_attr_value_(
struct berval vtmp;
int freeval = 0;
ldap_back_map( at_map, &ad->ad_cname, mapped_attr, remap );
ldap_back_map( &dc->rwmap->rwm_at, &ad->ad_cname, mapped_attr, remap );
if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0') {
/*
* FIXME: are we sure we need to search oc_map if at_map fails?
*/
ldap_back_map( oc_map, &ad->ad_cname, mapped_attr, remap );
ldap_back_map( &dc->rwmap->rwm_oc, &ad->ad_cname, mapped_attr, remap );
if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0' ) {
*mapped_attr = ad->ad_cname;
}
......@@ -288,25 +187,16 @@ map_attr_value_(
if ( strcmp( ad->ad_type->sat_syntax->ssyn_oid, SLAPD_DN_SYNTAX ) == 0 )
{
switch ( rewrite_session( info, "searchFilter",
value->bv_val, cookie, &vtmp.bv_val ) ) {
dncookie fdc = *dc;
fdc.ctx = "searchFilter";
switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) {
case REWRITE_REGEXEC_OK:
if ( vtmp.bv_val == NULL ) {
vtmp = *value;
} else {
vtmp.bv_len = strlen( vtmp.bv_val );
if ( vtmp.bv_val != value->bv_val ) {
freeval = 1;
}
#ifdef NEW_LOGGING
LDAP_LOG( BACK_LDAP, DETAIL1,
"[rw] searchFilter: \"%s\" -> \"%s\"\n",
value->bv_val, vtmp.bv_val, 0 );
#else /* !NEW_LOGGING */
Debug( LDAP_DEBUG_ARGS, "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
value->bv_val, vtmp.bv_val, "" );
#endif /* !NEW_LOGGING */
break;
case REWRITE_REGEXEC_UNWILLING:
return -1;
......@@ -316,7 +206,7 @@ map_attr_value_(
}
} else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
ldap_back_map( oc_map, value, &vtmp, remap );
ldap_back_map( &dc->rwmap->rwm_oc, value, &vtmp, remap );
if ( vtmp.bv_val == NULL || vtmp.bv_val[0] == '\0' ) {
vtmp = *value;
}
......@@ -334,75 +224,9 @@ map_attr_value_(
return 0;
}
#define map_attr_value(at_map, oc_map, ad, mapped_attr, value, mapped_value, remap) \
map_attr_value_(info, cookie, (at_map), (oc_map), (ad), (mapped_attr), (value), (mapped_value), (remap))
#define ldap_back_filter_map_rewrite(at_map, oc_map, f, fstr, remap) \
ldap_back_filter_map_rewrite_(info, cookie, (at_map), (oc_map), (f), (fstr), (remap))
#else /* ! ENABLE_REWRITE */
static int
map_attr_value_(
struct ldapmap *at_map,
struct ldapmap *oc_map,
AttributeDescription *ad,
struct berval *mapped_attr,
struct berval *value,
struct berval *mapped_value,
int remap )
{
struct berval vtmp;
ldap_back_map( at_map, &ad->ad_cname, mapped_attr, remap );
if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0') {
/*
* FIXME: are we sure we need to search oc_map if at_map fails?
*/
ldap_back_map( oc_map, &ad->ad_cname, mapped_attr, remap );
if ( mapped_attr->bv_val == NULL || mapped_attr->bv_val[0] == '\0' ) {
*mapped_attr = ad->ad_cname;
}
}
if ( value == NULL ) {
return 0;
}
if ( strcmp( ad->ad_type->sat_syntax->ssyn_oid, SLAPD_DN_SYNTAX ) == 0 )
{
/* FIXME: use suffix massage capabilities */
vtmp = *value;
} else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) {
ldap_back_map( oc_map, value, &vtmp, remap );
if ( vtmp.bv_val == NULL || vtmp.bv_val[0] == '\0' ) {
vtmp = *value;
}
} else {
vtmp = *value;
}
filter_escape_value( &vtmp, mapped_value );
return 0;
}
#define map_attr_value(at_map, oc_map, ad, mapped_attr, value, mapped_value, remap) \
map_attr_value_((at_map), (oc_map), (ad), (mapped_attr), (value), (mapped_value), (remap))
#define ldap_back_filter_map_rewrite(at_map, oc_map, f, fstr, remap) \
ldap_back_filter_map_rewrite_((at_map), (oc_map), (f), (fstr), (remap))
#endif /* ! ENABLE_REWRITE */
int
ldap_back_filter_map_rewrite_(
#ifdef ENABLE_REWRITE
struct rewrite_info *info,
void *cookie,
#endif /* ENABLE_REWRITE */
struct ldapmap *at_map,
struct ldapmap *oc_map,
ldap_back_filter_map_rewrite(
dncookie *dc,
Filter *f,
struct berval *fstr,
int remap )
......@@ -420,7 +244,7 @@ ldap_back_filter_map_rewrite_(
switch ( f->f_choice ) {
case LDAP_FILTER_EQUALITY:
if ( map_attr_value( at_map, oc_map, f->f_av_desc, &atmp,
if ( map_attr_value( dc, f->f_av_desc, &atmp,
&f->f_av_value, &vtmp, remap ) )
{
return -1;
......@@ -437,8 +261,8 @@ ldap_back_filter_map_rewrite_(
break;
case LDAP_FILTER_GE:
if ( map_attr_value( at_map, oc_map, f->f_av_desc, &atmp,
&f->f_av_value, &vtmp, remap ) )
if ( map_attr_value( dc, f->f_av_desc, &atmp,
&f->f_av_value, &vtmp, remap ) )
{
return -1;
}
......@@ -454,7 +278,7 @@ ldap_back_filter_map_rewrite_(
break;
case LDAP_FILTER_LE:
if ( map_attr_value( at_map, oc_map, f->f_av_desc, &atmp,
if ( map_attr_value( dc, f->f_av_desc, &atmp,
&f->f_av_value, &vtmp, remap ) )
{
return -1;
......@@ -471,7 +295,7 @@ ldap_back_filter_map_rewrite_(
break;
case LDAP_FILTER_APPROX:
if ( map_attr_value( at_map, oc_map, f->f_av_desc, &atmp,
if ( map_attr_value( dc, f->f_av_desc, &atmp,
&f->f_av_value, &vtmp, remap ) )
{
return -1;
......@@ -488,7 +312,7 @@ ldap_back_filter_map_rewrite_(
break;
case LDAP_FILTER_SUBSTRINGS:
if ( map_attr_value( at_map, oc_map, f->f_sub_desc, &atmp,
if ( map_attr_value( dc, f->f_sub_desc, &atmp,
NULL, NULL, remap ) )
{
return -1;
......@@ -550,7 +374,7 @@ ldap_back_filter_map_rewrite_(
break;
case LDAP_FILTER_PRESENT:
if ( map_attr_value( at_map, oc_map, f->f_desc, &atmp,
if ( map_attr_value( dc, f->f_desc, &atmp,
NULL, NULL, remap ) )