From 278ccf828b72dbc32c791687a45108636ec289fc Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Sat, 26 Jan 2002 06:52:59 +0000 Subject: [PATCH] Changed be_issuffix and dnParent to struct bervals --- servers/slapd/back-ldbm/add.c | 13 ++-- servers/slapd/back-ldbm/delete.c | 6 +- servers/slapd/back-ldbm/dn2id.c | 95 +++++++++++++++--------------- servers/slapd/back-ldbm/modrdn.c | 24 ++++---- servers/slapd/back-ldbm/search.c | 27 ++++----- servers/slapd/back-monitor/cache.c | 2 +- servers/slapd/back-passwd/search.c | 12 ++-- 7 files changed, 89 insertions(+), 90 deletions(-) diff --git a/servers/slapd/back-ldbm/add.c b/servers/slapd/back-ldbm/add.c index ec7d13f515..d6e4b300eb 100644 --- a/servers/slapd/back-ldbm/add.c +++ b/servers/slapd/back-ldbm/add.c @@ -80,11 +80,12 @@ ldbm_back_add( * add the entry. */ - pdn.bv_val = dn_parent( be, e->e_ndn ); - if (pdn.bv_val && pdn.bv_val[0]) - pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_ndn); - else - pdn.bv_len = 0; + if ( be_issuffix( be, &e->e_nname ) ) { + pdn = slap_empty_bv; + } else { + rc = dnParent( &e->e_nname, &pdn ); + /* dnParent always returns success */ + } if( pdn.bv_len ) { Entry *matched = NULL; @@ -206,7 +207,7 @@ ldbm_back_add( /* no parent, must be adding entry to root */ if ( !be_isroot( be, &op->o_ndn ) ) { - if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) { + if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) { p = (Entry *)&slap_entry_root; rc = access_allowed( be, conn, op, p, diff --git a/servers/slapd/back-ldbm/delete.c b/servers/slapd/back-ldbm/delete.c index bfe7a48eba..adb1ff6b72 100644 --- a/servers/slapd/back-ldbm/delete.c +++ b/servers/slapd/back-ldbm/delete.c @@ -115,8 +115,8 @@ ldbm_back_delete( } /* delete from parent's id2children entry */ - if( (pdn.bv_val = dn_parent( be, e->e_ndn )) != NULL && pdn.bv_val[ 0 ] != '\0' ) { - pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_ndn); + if( !be_issuffix( be, &e->e_nname ) && dnParent( &e->e_nname, &pdn ) == LDAP_SUCCESS + && pdn.bv_len ) { if( (p = dn2entry_w( be, &pdn, NULL )) == NULL) { #ifdef NEW_LOGGING LDAP_LOG(( "backend", LDAP_LEVEL_ERR, @@ -154,7 +154,7 @@ ldbm_back_delete( } else { /* no parent, must be root to delete */ if( ! be_isroot( be, &op->o_ndn ) ) { - if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) { + if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) { p = (Entry *)&slap_entry_root; rc = access_allowed( be, conn, op, p, diff --git a/servers/slapd/back-ldbm/dn2id.c b/servers/slapd/back-ldbm/dn2id.c index 83325ad275..64be1196bf 100644 --- a/servers/slapd/back-ldbm/dn2id.c +++ b/servers/slapd/back-ldbm/dn2id.c @@ -26,7 +26,8 @@ dn2id_add( int rc, flags; DBCache *db; Datum key, data; - char *buf, *ptr, *pdn; + char *buf; + struct berval ptr, pdn; #ifdef NEW_LOGGING LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY, @@ -55,8 +56,9 @@ dn2id_add( buf = ch_malloc( key.dsize ); key.dptr = buf; buf[0] = DN_BASE_PREFIX; - ptr = buf + 1; - strcpy( ptr, dn->bv_val ); + ptr.bv_val = buf + 1; + ptr.bv_len = dn->bv_len; + strcpy( ptr.bv_val, dn->bv_val ); ldbm_datum_init( data ); data.dptr = (char *) &id; @@ -65,41 +67,39 @@ dn2id_add( flags = LDBM_INSERT; rc = ldbm_cache_store( db, key, data, flags ); - if ( rc != -1 && !be_issuffix( be, ptr )) { + if ( rc != -1 && !be_issuffix( be, &ptr )) { buf[0] = DN_SUBTREE_PREFIX; ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex ); rc = idl_insert_key( be, db, key, id ); ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex ); - } - - if ( rc != -1 ) { - pdn = dn_parent( be, ptr ); - if( pdn != NULL ) { - pdn[-1] = DN_ONE_PREFIX; - key.dsize -= pdn - ptr; - key.dptr = pdn - 1; - ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex ); - rc = idl_insert_key( be, db, key, id ); - ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex ); + if ( rc != -1 ) { + rc = dnParent( &ptr, &pdn ); + + if( rc == LDAP_SUCCESS ) { + pdn.bv_val[-1] = DN_ONE_PREFIX; + key.dsize = pdn.bv_len + 2; + key.dptr = pdn.bv_val - 1; + ptr = pdn; + ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex ); + rc = idl_insert_key( be, db, key, id ); + ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex ); + } } } - while ( rc != -1 && pdn != NULL ) { - if ( be_issuffix( be, pdn )) - break; - - pdn[-1] = DN_SUBTREE_PREFIX; + while ( rc != -1 && !be_issuffix( be, &ptr )) { + ptr.bv_val[-1] = DN_SUBTREE_PREFIX; ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex ); rc = idl_insert_key( be, db, key, id ); ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex ); if( rc != 0 ) break; + rc = dnParent( &ptr, &pdn ); + key.dsize = pdn.bv_len + 2; + key.dptr = pdn.bv_val - 1; ptr = pdn; - pdn = dn_parent( be, pdn ); - key.dsize -= pdn - ptr; - key.dptr = pdn - 1; } free( buf ); @@ -226,7 +226,7 @@ dn2idl( assert( idlp != NULL ); *idlp = NULL; - if ( prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn->bv_val) ) { + if ( prefix == DN_SUBTREE_PREFIX && be_issuffix(be, dn) ) { *idlp = idl_allids( be ); return 0; } @@ -270,7 +270,8 @@ dn2id_delete( DBCache *db; Datum key; int rc; - char *buf, *ptr, *pdn; + char *buf; + struct berval ptr, pdn; #ifdef NEW_LOGGING LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY, @@ -301,44 +302,43 @@ dn2id_delete( buf = ch_malloc( key.dsize ); key.dptr = buf; buf[0] = DN_BASE_PREFIX; - ptr = buf + 1; - strcpy( ptr, dn->bv_val ); + ptr.bv_val = buf + 1; + ptr.bv_len = dn->bv_len; + strcpy( ptr.bv_val, dn->bv_val ); rc = ldbm_cache_delete( db, key ); - if( !be_issuffix( be, ptr )) { + if( !be_issuffix( be, &ptr )) { buf[0] = DN_SUBTREE_PREFIX; ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex ); (void) idl_delete_key( be, db, key, id ); ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex ); - } - pdn = dn_parent( be, ptr ); + rc = dnParent( &ptr, &pdn ); - if( pdn != NULL ) { - pdn[-1] = DN_ONE_PREFIX; - key.dsize -= pdn - ptr; - key.dptr = pdn - 1; + if( rc == LDAP_SUCCESS ) { + pdn.bv_val[-1] = DN_ONE_PREFIX; + key.dsize = pdn.bv_len + 2; + key.dptr = pdn.bv_val - 1; + ptr = pdn; - ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex ); - (void) idl_delete_key( be, db, key, id ); - ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex ); + ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex ); + (void) idl_delete_key( be, db, key, id ); + ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex ); + } } - while ( pdn != NULL ) { - if ( be_issuffix( be, pdn )) - break; - - pdn[-1] = DN_SUBTREE_PREFIX; + while ( rc != -1 && !be_issuffix( be, &ptr )) { + ptr.bv_val[-1] = DN_SUBTREE_PREFIX; ldap_pvt_thread_mutex_lock( &db->dbc_write_mutex ); (void) idl_delete_key( be, db, key, id ); ldap_pvt_thread_mutex_unlock( &db->dbc_write_mutex ); + rc = dnParent( &ptr, &pdn ); + key.dsize = pdn.bv_len + 2; + key.dptr = pdn.bv_val - 1; ptr = pdn; - pdn = dn_parent( be, pdn ); - key.dsize -= pdn - ptr; - key.dptr = pdn - 1; } free( buf ); @@ -415,9 +415,8 @@ dn2entry_rw( if( matched == NULL ) return NULL; /* entry does not exist - see how much of the dn does exist */ - /* dn_parent checks returns NULL if dn is suffix */ - if ( (pdn.bv_val = dn_parent( be, dn->bv_val )) != NULL && *pdn.bv_val ) { - pdn.bv_len = dn->bv_len - (pdn.bv_val - dn->bv_val); + if ( !be_issuffix( be, dn ) && dnParent( dn, &pdn ) == LDAP_SUCCESS + && pdn.bv_len ) { /* get entry with reader lock */ if ( (e = dn2entry_r( be, &pdn, matched )) != NULL ) { *matched = e; diff --git a/servers/slapd/back-ldbm/modrdn.c b/servers/slapd/back-ldbm/modrdn.c index d8a80509a4..195fa4d434 100644 --- a/servers/slapd/back-ldbm/modrdn.c +++ b/servers/slapd/back-ldbm/modrdn.c @@ -148,11 +148,11 @@ ldbm_back_modrdn( goto return_results; } - p_ndn.bv_val = dn_parent( be, e->e_ndn ); - if ( p_ndn.bv_val ) - p_ndn.bv_len = e->e_nname.bv_len - (p_ndn.bv_val - e->e_ndn); - else - p_ndn.bv_len = 0; + if ( be_issuffix( be, &e->e_nname ) ) { + p_ndn = slap_empty_bv ; + } else { + dnParent( &e->e_nname, &p_ndn ); + } if ( p_ndn.bv_len != 0 ) { /* Make sure parent entry exist and we can write its @@ -201,11 +201,11 @@ ldbm_back_modrdn( p_ndn.bv_val, 0, 0 ); #endif - p_dn.bv_val = dn_parent( be, e->e_dn ); - if ( p_dn.bv_val ) - p_dn.bv_len = e->e_name.bv_len - (p_dn.bv_val - e->e_dn); - else - p_dn.bv_len = 0; + if ( p_ndn.bv_val == slap_empty_bv.bv_val ) { + p_dn = slap_empty_bv; + } else { + dnParent( &e->e_name, &p_dn ); + } #ifdef NEW_LOGGING LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1, @@ -219,7 +219,7 @@ ldbm_back_modrdn( /* no parent, must be root to modify rdn */ isroot = be_isroot( be, &op->o_ndn ); if ( ! isroot ) { - if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) { + if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) { p = (Entry *)&slap_entry_root; rc = access_allowed( be, conn, op, p, @@ -397,7 +397,7 @@ ldbm_back_modrdn( } if ( ! isroot ) { - if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) { + if ( be_issuffix( be, (struct berval *)&slap_empty_bv ) || be_isupdate( be, &op->o_ndn ) ) { np = (Entry *)&slap_entry_root; rc = access_allowed( be, conn, op, np, diff --git a/servers/slapd/back-ldbm/search.c b/servers/slapd/back-ldbm/search.c index c16404a3ac..c80359f8c6 100644 --- a/servers/slapd/back-ldbm/search.c +++ b/servers/slapd/back-ldbm/search.c @@ -343,11 +343,10 @@ searchit: /* need to skip alias which deref into scope */ if( scope & LDAP_SCOPE_ONELEVEL ) { - char *pdn = dn_parent( NULL, e->e_ndn ); - if ( pdn != NULL ) { - if( strcmp( pdn, realbase.bv_val ) ) { - goto loop_continue; - } + struct berval pdn; + dnParent( &e->e_nname, &pdn ); + if ( ber_bvcmp( &pdn, &realbase ) ) { + goto loop_continue; } } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) { @@ -375,14 +374,13 @@ searchit: if ( !manageDSAit && scope != LDAP_SCOPE_BASE && is_entry_referral( e ) ) { - char *dn; + struct berval dn; /* check scope */ if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) { - if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) { - scopeok = (dn == realbase.bv_val) - ? 1 - : (strcmp( dn, realbase.bv_val ) ? 0 : 1 ); + if ( !be_issuffix( be, &e->e_nname ) ) { + dnParent( &e->e_nname, &dn ); + scopeok = dn_match( &dn, &realbase ); } else { scopeok = (realbase.bv_len == 0); } @@ -425,14 +423,13 @@ searchit: /* if it matches the filter and scope, send it */ if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) { - char *dn; + struct berval dn; /* check scope */ if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) { - if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) { - scopeok = (dn == realbase.bv_val) - ? 1 - : (strcmp( dn, realbase.bv_val ) ? 0 : 1 ); + if ( !be_issuffix( be, &e->e_nname ) ) { + dnParent( &e->e_nname, &dn ); + scopeok = dn_match( &dn, &realbase ); } else { scopeok = (realbase.bv_len == 0); } diff --git a/servers/slapd/back-monitor/cache.c b/servers/slapd/back-monitor/cache.c index 70b7cdf2a0..6572c2af2e 100644 --- a/servers/slapd/back-monitor/cache.c +++ b/servers/slapd/back-monitor/cache.c @@ -206,7 +206,7 @@ monitor_cache_dn2entry( /* try with parent/ancestors */ if ( ndn->bv_len ) { - p_ndn.bv_val = dn_parent( NULL, ndn->bv_val ); + dnParent( ndn, &p_ndn ); } if ( p_ndn.bv_val == NULL ) { diff --git a/servers/slapd/back-passwd/search.c b/servers/slapd/back-passwd/search.c index 4366f32aa8..bad2c17763 100644 --- a/servers/slapd/back-passwd/search.c +++ b/servers/slapd/back-passwd/search.c @@ -47,7 +47,7 @@ passwd_back_search( int err = LDAP_SUCCESS; LDAPRDN *rdn = NULL; - char *parent = NULL; + struct berval parent = { 0, NULL }; char *matched = NULL; const char *text = NULL; @@ -68,7 +68,7 @@ passwd_back_search( #endif /* HAVE_SETPWFILE */ /* Handle a query for the base of this backend */ - if ( be_issuffix( be, nbase->bv_val ) ) { + if ( be_issuffix( be, nbase ) ) { struct berval vals[2]; vals[1].bv_val = NULL; @@ -174,12 +174,14 @@ passwd_back_search( } } else { - parent = dn_parent( be, nbase->bv_val ); + if (! be_issuffix( be, nbase ) ) { + dnParent( nbase, &parent ); + } /* This backend is only one layer deep. Don't answer requests for * anything deeper than that. */ - if( !be_issuffix( be, parent ) ) { + if( !be_issuffix( be, &parent ) ) { int i; for( i=0; be->be_nsuffix[i] != NULL; i++ ) { if( dnIsSuffix( nbase, be->be_nsuffix[i] ) ) { @@ -203,7 +205,7 @@ passwd_back_search( } if ( (pw = getpwnam( rdn[0][0]->la_value.bv_val )) == NULL ) { - matched = parent; + matched = parent.bv_val; err = LDAP_NO_SUCH_OBJECT; goto done; } -- 2.39.5