]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldbm/search.c
modify be_referral to use struct berval DNs.
[openldap] / servers / slapd / back-ldbm / search.c
index b4a9a29fb3c25af0f55d539a3291e789912cb7b2..bc5e67989c27158f2550073f9d04359e05a0a6fc 100644 (file)
@@ -37,7 +37,7 @@ ldbm_back_search(
     int                tlimit,
     Filter     *filter,
     const char *filterstr,
-    char       **attrs,
+    struct berval      **attrs,
     int                attrsonly )
 {
        struct ldbminfo *li = (struct ldbminfo *) be->be_private;
@@ -52,6 +52,7 @@ ldbm_back_search(
        char    *realbase = NULL;
        int             nentries = 0;
        int             manageDSAit = get_manageDSAit( op );
+       int             cscope = LDAP_SCOPE_DEFAULT;
 
        struct slap_limits_set *limit = NULL;
        int isroot = 0;
@@ -66,8 +67,7 @@ ldbm_back_search(
 
        if ( *nbase == '\0' ) {
                /* DIT root special case */
-               static const Entry root = { NOID, "", "", NULL, NULL };
-               e = (Entry *) &root;
+               e = (Entry *) &slap_entry_root;
 
                /* need normalized dn below */
                realbase = ch_strdup( e->e_ndn );
@@ -95,54 +95,73 @@ ldbm_back_search(
                struct berval **refs = NULL;
 
                if ( matched != NULL ) {
+                       struct berval **erefs;
                        matched_dn = ch_strdup( matched->e_dn );
 
-                       refs = is_entry_referral( matched )
-                               ? get_entry_referrals( be, conn, op, matched )
+                       erefs = is_entry_referral( matched )
+                               ? get_entry_referrals( be, conn, op, matched,
+                                       base, scope )
                                : NULL;
 
                        cache_return_entry_r( &li->li_cache, matched );
 
+                       if( erefs ) {
+                               refs = referral_rewrite( erefs, matched_dn,
+                                       base, scope );
+
+                               ber_bvecfree( erefs );
+                       }
+
                } else {
-                       refs = default_referral;
+                       refs = referral_rewrite( default_referral,
+                               NULL, base, scope );
                }
 
                send_ldap_result( conn, op, err,
                        matched_dn, text, refs, NULL );
 
-               if( matched != NULL ) {
-                       ber_bvecfree( refs );
-                       free( matched_dn );
-               }
-
+               ber_bvecfree( refs );
+               free( matched_dn );
                return 1;
        }
 
        if (!manageDSAit && is_entry_referral( e ) ) {
                /* entry is a referral, don't allow add */
                char *matched_dn = ch_strdup( e->e_dn );
-               struct berval **refs = get_entry_referrals( be,
-                       conn, op, e );
+               struct berval **erefs = get_entry_referrals( be,
+                       conn, op, e, base, scope );
+               struct berval **refs = NULL;
 
                cache_return_entry_r( &li->li_cache, e );
 
 #ifdef NEW_LOGGING
                LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
-                          "ldbm_search: entry (%s) is a referral.\n",
-                          e->e_dn ));
+                       "ldbm_search: entry (%s) is a referral.\n",
+                       e->e_dn ));
 #else
                Debug( LDAP_DEBUG_TRACE,
                        "ldbm_search: entry is referral\n",
                        0, 0, 0 );
 #endif
 
+               if( erefs ) {
+                       refs = referral_rewrite( erefs, matched_dn,
+                               base, scope );
 
-               send_ldap_result( conn, op, LDAP_REFERRAL,
-                   matched_dn, NULL, refs, NULL );
+                       ber_bvecfree( erefs );
+               }
 
-               ber_bvecfree( refs );
-               free( matched_dn );
+               if( refs ) {
+                       send_ldap_result( conn, op, LDAP_REFERRAL,
+                               matched_dn, NULL, refs, NULL );
+                       ber_bvecfree( refs );
+
+               } else {
+                       send_ldap_result( conn, op, LDAP_OTHER, matched_dn,
+                               "bad referral object", NULL, NULL );
+               }
 
+               free( matched_dn );
                return 1;
        }
 
@@ -152,9 +171,12 @@ ldbm_back_search(
        }
 
        if ( scope == LDAP_SCOPE_BASE ) {
+               cscope = LDAP_SCOPE_BASE;
                candidates = base_candidate( be, e );
 
        } else {
+               cscope = ( scope != LDAP_SCOPE_SUBTREE )
+                       ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
                candidates = search_candidates( be, e, filter,
                    scope, deref, manageDSAit );
        }
@@ -169,13 +191,12 @@ searchit:
                /* no candidates */
 #ifdef NEW_LOGGING
                LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
-                          "ldbm_search: no candidates\n" ));
+                       "ldbm_search: no candidates\n" ));
 #else
                Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
                        0, 0, 0 );
 #endif
 
-
                send_search_result( conn, op,
                        LDAP_SUCCESS,
                        NULL, NULL, NULL, NULL, 0 );
@@ -185,15 +206,15 @@ searchit:
        }
 
        /* if not root, get appropriate limits */
-       if ( be_isroot( be, op->o_ndn ) ) {
+       if ( be_isroot( be, &op->o_ndn ) ) {
                isroot = 1;
        } else {
-               ( void ) get_limits( be, op->o_ndn, &limit );
+               ( void ) get_limits( be, &op->o_ndn, &limit );
        }
 
        /* if candidates exceed to-be-checked entries, abort */
        if ( !isroot && limit->lms_s_unchecked != -1 ) {
-               if ( ID_BLOCK_NIDS( candidates ) > limit->lms_s_unchecked ) {
+               if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
                        send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
                                        NULL, NULL, NULL, NULL, 0 );
                        rc = 0;
@@ -201,63 +222,69 @@ searchit:
                }
        }
        
-       /* if no time limit requested, use soft limit (unless root!) */
-       if ( tlimit <= 0 ) {
-               if ( isroot ) {
-                       tlimit = -1;    /* allow root to set no limit */
-               } else {
-                       tlimit = limit->lms_t_soft;
-               }
-               
-       /* if requested limit higher than hard limit, abort */
-       } else if ( tlimit > limit->lms_t_hard ) {
-               /* no hard limit means use soft instead */
-               if ( limit->lms_t_hard == 0 ) {
-                       tlimit = limit->lms_t_soft;
-                       
-               /* positive hard limit means abort */
-               } else if ( limit->lms_t_hard > 0 ) {
-                       send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                                       NULL, NULL, NULL, NULL, 0 );
-                       rc = 0; 
-                       goto done;
+       /* if root an no specific limit is required, allow unlimited search */
+       if ( isroot ) {
+               if ( tlimit == 0 ) {
+                       tlimit = -1;
                }
 
-               /* negative hard limit means no limit */
-       }
+               if ( slimit == 0 ) {
+                       slimit = -1;
+               }
 
-       /* compute it anyway; root does not use it */
-       stoptime = op->o_time + tlimit;
+       } else {
+               /* if no limit is required, use soft limit */
+               if ( tlimit <= 0 ) {
+                       tlimit = limit->lms_t_soft;
+               
+               /* if requested limit higher than hard limit, abort */
+               } else if ( tlimit > limit->lms_t_hard ) {
+                       /* no hard limit means use soft instead */
+                       if ( limit->lms_t_hard == 0 ) {
+                               tlimit = limit->lms_t_soft;
+                       
+                       /* positive hard limit means abort */
+                       } else if ( limit->lms_t_hard > 0 ) {
+                               send_search_result( conn, op, 
+                                               LDAP_UNWILLING_TO_PERFORM,
+                                               NULL, NULL, NULL, NULL, 0 );
+                               rc = 0; 
+                               goto done;
+                       }
 
-       /* if no size limit requested, use soft limit (unless root!) */
-       if ( slimit == 0 ) {
-               if ( isroot ) {
-                       slimit = -1;    /* allow root to set no limit */
-               } else {
-                       slimit = limit->lms_s_soft;
+                       /* negative hard limit means no limit */
                }
-       
-       /* if requested limit higher than hard limit, abort */
-       } else if ( slimit > limit->lms_s_hard ) {
-               /* no hard limit means use soft instead */
-               if ( limit->lms_s_hard == 0 ) {
+
+               /* if no limit is required, use soft limit */
+               if ( slimit <= 0 ) {
                        slimit = limit->lms_s_soft;
 
-               /* positive hard limit means abort */
-               } else if ( limit->lms_s_hard > 0 ) {
-                       send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                                       NULL, NULL, NULL, NULL, 0 );
-                       rc = 0;
-                       goto done;
-               }
+               /* if requested limit higher than hard limit, abort */
+               } else if ( slimit > limit->lms_s_hard ) {
+                       /* no hard limit means use soft instead */
+                       if ( limit->lms_s_hard == 0 ) {
+                               slimit = limit->lms_s_soft;
+
+                       /* positive hard limit means abort */
+                       } else if ( limit->lms_s_hard > 0 ) {
+                               send_search_result( conn, op,
+                                               LDAP_UNWILLING_TO_PERFORM,
+                                               NULL, NULL, NULL, NULL, 0 );
+                               rc = 0;
+                               goto done;
+                       }
 
-               /* negative hard limit means no limit */
+                       /* negative hard limit means no limit */
+               }
        }
 
+       /* compute it anyway; root does not use it */
+       stoptime = op->o_time + tlimit;
+
        for ( id = idl_firstid( candidates, &cursor ); id != NOID;
            id = idl_nextid( candidates, &cursor ) )
        {
-               int             scopeok = 0;
+               int scopeok = 0;
 
                /* check for abandon */
                ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
@@ -284,14 +311,13 @@ searchit:
                if ( e == NULL ) {
 #ifdef NEW_LOGGING
                        LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
-                                  "ldbm_search: candidate %ld not found.\n", id ));
+                               "ldbm_search: candidate %ld not found.\n", id ));
 #else
                        Debug( LDAP_DEBUG_TRACE,
                                "ldbm_search: candidate %ld not found\n",
                                id, 0, 0 );
 #endif
 
-
                        goto loop_continue;
                }
 
@@ -320,17 +346,16 @@ searchit:
                                                free( pdn );
                                                goto loop_continue;
                                        }
-                                       free(pdn);
                                }
 
                        } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
                                /* alias is within scope */
 #ifdef NEW_LOGGING
                                LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
-                                          "ldbm_search: \"%s\" in subtree\n", e->e_dn ));
+                                       "ldbm_search: alias \"%s\" in subtree\n", e->e_dn ));
 #else
                                Debug( LDAP_DEBUG_TRACE,
-                                       "ldbm_search: \"%s\" in subtree\n",
+                                       "ldbm_search: alias \"%s\" in subtree\n",
                                        e->e_dn, 0, 0 );
 #endif
 
@@ -348,13 +373,51 @@ searchit:
                if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
                        is_entry_referral( e ) )
                {
-                       struct berval **refs = get_entry_referrals(
-                               be, conn, op, e );
+                       char    *dn;
 
-                       send_search_reference( be, conn, op,
-                               e, refs, scope, NULL, &v2refs );
+                       /* check scope */
+                       if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
+                               if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
+                                       scopeok = (dn == realbase)
+                                               ? 1
+                                               : (strcmp( dn, realbase ) ? 0 : 1 );
+                               } else {
+                                       scopeok = (realbase == NULL || *realbase == '\0');
+                               }
 
-                       ber_bvecfree( refs );
+                       } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
+                               dn = ch_strdup( e->e_ndn );
+                               scopeok = dn_issuffix( dn, realbase );
+                               free( dn );
+
+                       } else {
+                               scopeok = 1;
+                       }
+
+                       if( scopeok ) {
+                               struct berval **erefs = get_entry_referrals(
+                                       be, conn, op, e, NULL, cscope );
+                               struct berval **refs = referral_rewrite( erefs, e->e_dn, NULL,
+                                       scope == LDAP_SCOPE_SUBTREE
+                                               ? LDAP_SCOPE_SUBTREE
+                                               : LDAP_SCOPE_BASE );
+
+                               send_search_reference( be, conn, op,
+                                       e, refs, NULL, &v2refs );
+
+                               ber_bvecfree( refs );
+
+                       } else {
+#ifdef NEW_LOGGING
+                               LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
+                                       "ldbm_search: candidate referral %ld scope not okay\n",
+                                       id ));
+#else
+                               Debug( LDAP_DEBUG_TRACE,
+                                       "ldbm_search: candidate referral %ld scope not okay\n",
+                                       id, 0, 0 );
+#endif
+                       }
 
                        goto loop_continue;
                }
@@ -366,12 +429,9 @@ searchit:
                        /* check scope */
                        if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
                                if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
-                                       (void) dn_normalize( dn );
                                        scopeok = (dn == realbase)
                                                ? 1
                                                : (strcmp( dn, realbase ) ? 0 : 1 );
-                                       free( dn );
-
                                } else {
                                        scopeok = (realbase == NULL || *realbase == '\0');
                                }
@@ -415,24 +475,23 @@ searchit:
                        } else {
 #ifdef NEW_LOGGING
                                LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
-                                          "ldbm_search: candidate %ld scope not okay\n", id ));
+                                       "ldbm_search: candidate entry %ld scope not okay\n", id ));
 #else
                                Debug( LDAP_DEBUG_TRACE,
-                                       "ldbm_search: candidate %ld scope not okay\n",
+                                       "ldbm_search: candidate entry %ld scope not okay\n",
                                        id, 0, 0 );
 #endif
-
                        }
+
                } else {
 #ifdef NEW_LOGGING
                        LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
-                                  "ldbm_search: candidate %ld does not match filter\n", id ));
+                               "ldbm_search: candidate entry %ld does not match filter\n", id ));
 #else
                        Debug( LDAP_DEBUG_TRACE,
-                               "ldbm_search: candidate %ld does not match filter\n",
+                               "ldbm_search: candidate entry %ld does not match filter\n",
                                id, 0, 0 );
 #endif
-
                }
 
 loop_continue:
@@ -443,6 +502,7 @@ loop_continue:
 
                ldap_pvt_thread_yield();
        }
+
        send_search_result( conn, op,
                v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
                NULL, NULL, v2refs, NULL, nentries );