From 17e46d84685d06b6e912d133e12b82501d99ab25 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Thu, 3 Apr 2003 21:35:27 +0000 Subject: [PATCH] cleanup and fixes --- servers/slapd/back-ldap/back-ldap.h | 6 +-- servers/slapd/back-ldap/bind.c | 58 +++++++++++++++++------------ servers/slapd/back-ldap/config.c | 4 +- servers/slapd/back-ldap/init.c | 17 ++++++--- servers/slapd/back-ldap/search.c | 17 ++++----- 5 files changed, 58 insertions(+), 44 deletions(-) diff --git a/servers/slapd/back-ldap/back-ldap.h b/servers/slapd/back-ldap/back-ldap.h index 7a21585ef8..9d5f0eac6a 100644 --- a/servers/slapd/back-ldap/back-ldap.h +++ b/servers/slapd/back-ldap/back-ldap.h @@ -76,8 +76,8 @@ struct ldapmapping { struct ldapinfo { struct slap_backend_db *be; char *url; - char *binddn; - char *bindpw; + struct berval binddn; + struct berval bindpw; ldap_pvt_thread_mutex_t conn_mutex; int savecred; Avlnode *conntree; @@ -158,7 +158,7 @@ extern int suffix_massage_config( struct rewrite_info *info, extern int ldap_dnattr_rewrite( struct rewrite_info *rwinfo, BerVarray a_vals, void *cookie ); #endif /* ENABLE_REWRITE */ -int ldap_build_entry( Backend *be, Connection *c, LDAPMessage *e, Entry *ent, +int ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent, struct berval *bdn, int private ); LDAP_END_DECL diff --git a/servers/slapd/back-ldap/bind.c b/servers/slapd/back-ldap/bind.c index f8972bc411..e050576c29 100644 --- a/servers/slapd/back-ldap/bind.c +++ b/servers/slapd/back-ldap/bind.c @@ -72,17 +72,23 @@ ldap_back_bind( * Rewrite the bind dn if needed */ #ifdef ENABLE_REWRITE - switch ( rewrite_session( li->rwinfo, "bindDn", op->o_req_dn.bv_val, op->o_conn, &mdn.bv_val ) ) { + switch ( rewrite_session( li->rwinfo, "bindDn", + op->o_req_dn.bv_val, + op->o_conn, &mdn.bv_val ) ) { case REWRITE_REGEXEC_OK: if ( mdn.bv_val == NULL ) { - mdn.bv_val = ( char * )op->o_req_dn.bv_val; + mdn = op->o_req_dn; + } else { + mdn.bv_len = strlen( mdn.bv_val ); } + #ifdef NEW_LOGGING LDAP_LOG( BACK_LDAP, DETAIL1, - "[rw] bindDn: \"%s\" -> \"%s\"\n", op->o_req_dn.bv_val, mdn.bv_val, 0 ); + "[rw] bindDn: \"%s\" -> \"%s\"\n", + op->o_req_dn.bv_val, mdn.bv_val, 0 ); #else /* !NEW_LOGGING */ - Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n%s", - op->o_req_dn.bv_val, mdn.bv_val, "" ); + Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" -> \"%s\"\n", + op->o_req_dn.bv_val, mdn.bv_val, 0 ); #endif /* !NEW_LOGGING */ break; @@ -126,17 +132,19 @@ ldap_back_bind( } /* must re-insert if local DN changed as result of bind */ - if ( lc->bound && ber_bvcmp(&op->o_req_ndn, &lc->local_dn ) ) { - int err; + if ( lc->bound && !bvmatch(&op->o_req_ndn, &lc->local_dn ) ) { + int lerr; + ldap_pvt_thread_mutex_lock( &li->conn_mutex ); - lc = avl_delete( &li->conntree, (caddr_t)lc, ldap_back_conn_cmp ); + lc = avl_delete( &li->conntree, (caddr_t)lc, + ldap_back_conn_cmp ); if ( lc->local_dn.bv_val ) ch_free( lc->local_dn.bv_val ); ber_dupbv( &lc->local_dn, &op->o_req_ndn ); - err = avl_insert( &li->conntree, (caddr_t)lc, + lerr = avl_insert( &li->conntree, (caddr_t)lc, ldap_back_conn_cmp, ldap_back_conn_dup ); ldap_pvt_thread_mutex_unlock( &li->conn_mutex ); - if ( err == -1 ) { + if ( lerr == -1 ) { ldap_back_conn_free( lc ); } } @@ -167,7 +175,7 @@ ldap_back_conn_cmp( /* For shared sessions, conn is NULL. Only explicitly * bound sessions will have non-NULL conn. */ - return lc1->conn - lc2->conn; + return (int)lc1->conn - (int)lc2->conn; } /* @@ -207,7 +215,8 @@ static void ravl_print( Avlnode *root, int depth ) printf( " " ); lc = root->avl_data; - printf( "lc(%lx) local(%s) conn(%lx) %d\n", lc, lc->local_dn.bv_val, lc->conn, root->avl_bf ); + printf( "lc(%lx) local(%s) conn(%lx) %d\n", + lc, lc->local_dn.bv_val, lc->conn, root->avl_bf ); ravl_print( root->avl_left, depth+1 ); } @@ -276,7 +285,7 @@ ldap_back_getconn(struct ldapinfo *li, Operation *op, SlapReply *rs) ber_dupbv( &lc->local_dn, &lc_curr.local_dn ); if ( is_priv ) { - ber_str2bv( li->bindpw, 0, 1, &lc->cred ); + ber_dupbv( &lc->cred, &li->bindpw ); } else { lc->cred.bv_len = 0; lc->cred.bv_val = NULL; @@ -300,25 +309,28 @@ ldap_back_getconn(struct ldapinfo *li, Operation *op, SlapReply *rs) lc->bound_dn.bv_val = NULL; lc->bound_dn.bv_len = 0; switch ( rewrite_session( li->rwinfo, "bindDn", - op->o_conn->c_dn.bv_val, op->o_conn, + op->o_conn->c_dn.bv_val, + op->o_conn, &lc->bound_dn.bv_val ) ) { case REWRITE_REGEXEC_OK: if ( lc->bound_dn.bv_val == NULL ) { ber_dupbv( &lc->bound_dn, &op->o_conn->c_dn ); + } else { + lc->bound_dn.bv_len = strlen( lc->bound_dn.bv_val ); } #ifdef NEW_LOGGING LDAP_LOG( BACK_LDAP, DETAIL1, "[rw] bindDn: \"%s\" ->" - " \"%s\"\n%s", + " \"%s\"\n", op->o_conn->c_dn.bv_val, - lc->bound_dn.bv_val, "" ); + lc->bound_dn.bv_val, 0 ); #else /* !NEW_LOGGING */ Debug( LDAP_DEBUG_ARGS, "rw> bindDn: \"%s\" ->" - " \"%s\"\n%s", + " \"%s\"\n", op->o_conn->c_dn.bv_val, - lc->bound_dn.bv_val, "" ); + lc->bound_dn.bv_val, 0 ); #endif /* !NEW_LOGGING */ break; @@ -367,8 +379,7 @@ ldap_back_getconn(struct ldapinfo *li, Operation *op, SlapReply *rs) "ldap_back_getconn: conn %lx inserted\n", lc, 0, 0); #else /* !NEW_LOGGING */ Debug( LDAP_DEBUG_TRACE, - "=>ldap_back_getconn: conn %lx inserted\n%s%s", - lc, "", "" ); + "=>ldap_back_getconn: conn %lx inserted\n", lc, 0, 0 ); #endif /* !NEW_LOGGING */ /* Err could be -1 in case a duplicate ldapconn is inserted */ @@ -385,8 +396,7 @@ ldap_back_getconn(struct ldapinfo *li, Operation *op, SlapReply *rs) lc, 0, 0 ); #else /* !NEW_LOGGING */ Debug( LDAP_DEBUG_TRACE, - "=>ldap_back_getconn: conn %lx fetched%s%s\n", - lc, "", "" ); + "=>ldap_back_getconn: conn %lx fetched\n", lc, 0, 0 ); #endif /* !NEW_LOGGING */ } @@ -535,9 +545,9 @@ ldap_back_op_result(struct ldapinfo *li, struct ldapconn *lc, } if (rs->sr_matched != match) free((char *)rs->sr_matched); rs->sr_matched = NULL; - if ( match ) free( match ); + if ( match ) ldap_memfree( match ); if ( rs->sr_text ) { - free( (char *)rs->sr_text ); + ldap_memfree( (char *)rs->sr_text ); rs->sr_text = NULL; } return( (err==LDAP_SUCCESS) ? 0 : -1 ); diff --git a/servers/slapd/back-ldap/config.c b/servers/slapd/back-ldap/config.c index 2cdbeb21d3..f1eadd992a 100644 --- a/servers/slapd/back-ldap/config.c +++ b/servers/slapd/back-ldap/config.c @@ -102,7 +102,7 @@ ldap_back_db_config( fname, lineno ); return( 1 ); } - li->binddn = ch_strdup(argv[1]); + ber_str2bv( argv[1], 0, 1, &li->binddn ); /* password to use for ldap_back_group */ } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) { @@ -112,7 +112,7 @@ ldap_back_db_config( fname, lineno ); return( 1 ); } - li->bindpw = ch_strdup(argv[1]); + ber_str2bv( argv[1], 0, 1, &li->bindpw ); /* save bind creds for referral rebinds? */ } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) { diff --git a/servers/slapd/back-ldap/init.c b/servers/slapd/back-ldap/init.c index 765660806e..3beaec8d57 100644 --- a/servers/slapd/back-ldap/init.c +++ b/servers/slapd/back-ldap/init.c @@ -111,6 +111,11 @@ ldap_back_db_init( return -1; } + li->binddn.bv_val = NULL; + li->binddn.bv_len = 0; + li->bindpw.bv_val = NULL; + li->bindpw.bv_len = 0; + #ifdef ENABLE_REWRITE li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT ); if ( li->rwinfo == NULL ) { @@ -174,13 +179,13 @@ ldap_back_db_destroy( ch_free(li->url); li->url = NULL; } - if (li->binddn) { - ch_free(li->binddn); - li->binddn = NULL; + if (li->binddn.bv_val) { + ch_free(li->binddn.bv_val); + li->binddn.bv_val = NULL; } - if (li->bindpw) { - ch_free(li->bindpw); - li->bindpw = NULL; + if (li->bindpw.bv_val) { + ch_free(li->bindpw.bv_val); + li->bindpw.bv_val = NULL; } if (li->conntree) { avl_free( li->conntree, ldap_back_conn_free ); diff --git a/servers/slapd/back-ldap/search.c b/servers/slapd/back-ldap/search.c index 1dd06f0344..3fafc9dcc8 100644 --- a/servers/slapd/back-ldap/search.c +++ b/servers/slapd/back-ldap/search.c @@ -61,14 +61,13 @@ ldap_back_search( struct ldapconn *lc; struct timeval tv; LDAPMessage *res, *e; - int count, rc = 0, msgid, sres = LDAP_SUCCESS; + int count, rc = 0, msgid; char *match = NULL; char **mapped_attrs = NULL; struct berval mbase; struct berval mfilter = { 0, NULL }; struct slap_limits_set *limit = NULL; int isroot = 0; - BerVarray v2refs = NULL; lc = ldap_back_getconn(li, op, rs); if ( !lc ) { @@ -233,7 +232,7 @@ fail:; Entry ent; struct berval bdn; e = ldap_first_entry(lc->ld,res); - if ( ldap_build_entry(op->o_bd, op->o_conn, e, &ent, &bdn, 1) == LDAP_SUCCESS ) { + if ( ldap_build_entry(op, e, &ent, &bdn, 1) == LDAP_SUCCESS ) { Attribute *a; rs->sr_entry = &ent; rs->sr_attrs = op->oq_search.rs_attrs; @@ -376,15 +375,14 @@ finish:; int ldap_build_entry( - Backend *be, - Connection *conn, + Operation *op, LDAPMessage *e, Entry *ent, struct berval *bdn, int private ) { - struct ldapinfo *li = (struct ldapinfo *) be->be_private; + struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private; struct berval a, mapped; BerElement ber = *e->lm_ber; Attribute *attr, **attrp; @@ -400,7 +398,8 @@ ldap_build_entry( * Rewrite the dn of the result, if needed */ switch ( rewrite_session( li->rwinfo, "searchResult", - bdn->bv_val, conn, &ent->e_name.bv_val ) ) { + bdn->bv_val, op->o_conn, + &ent->e_name.bv_val ) ) { case REWRITE_REGEXEC_OK: if ( ent->e_name.bv_val == NULL ) { ent->e_name = *bdn; @@ -539,7 +538,7 @@ ldap_build_entry( switch ( rewrite_session( li->rwinfo, "searchResult", bv->bv_val, - conn, + op->o_conn, &newval.bv_val )) { case REWRITE_REGEXEC_OK: /* left as is */ @@ -678,7 +677,7 @@ ldap_back_entry_get( *ent = ch_malloc(sizeof(Entry)); - rc = ldap_build_entry(op->o_bd, op->o_conn, e, *ent, &bdn, 0); + rc = ldap_build_entry(op, e, *ent, &bdn, 0); if (rc != LDAP_SUCCESS) { ch_free(*ent); -- 2.39.5