]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/search.c
cleanup null mapping detection
[openldap] / servers / slapd / back-ldap / search.c
index c2e278d5c7da2d4da400cff694e41c624be0e41f..41b397a9da22dd2cdef2c9535414219f59671768 100644 (file)
@@ -1,7 +1,7 @@
 /* search.c - ldap backend search function */
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /* This is an altered version */
@@ -82,12 +82,23 @@ ldap_back_search(
 #endif /* ENABLE_REWRITE */
        struct slap_limits_set *limit = NULL;
        int isroot = 0;
+       BerVarray v2refs = NULL;
 
        lc = ldap_back_getconn(li, conn, op);
        if ( !lc ) {
                return( -1 );
        }
 
+       /*
+        * controls are set in ldap_back_dobind()
+        * 
+        * FIXME: in case of values return filter, we might want
+        * to map attrs and maybe rewrite value
+        */
+       if ( !ldap_back_dobind( lc, op ) ) {
+               return( -1 );
+       }
+
        /* if not root, get appropriate limits */
        if ( be_isroot( be, &op->o_ndn ) ) {
                isroot = 1;
@@ -99,13 +110,15 @@ ldap_back_search(
        /* if requested limit higher than hard limit, abort */
        if ( !isroot && tlimit > limit->lms_t_hard ) {
                /* no hard limit means use soft instead */
-               if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
+               if ( limit->lms_t_hard == 0
+                               && limit->lms_t_soft > -1
+                               && tlimit > limit->lms_t_soft ) {
                        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 );
+                       send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
+                                       NULL, NULL, NULL, NULL );
                        rc = 0;
                        goto finish;
                }
@@ -117,13 +130,15 @@ ldap_back_search(
        /* if requested limit higher than hard limit, abort */
        if ( !isroot && slimit > limit->lms_s_hard ) {
                /* no hard limit means use soft instead */
-               if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
+               if ( limit->lms_s_hard == 0
+                               && limit->lms_s_soft > -1
+                               && slimit > limit->lms_s_soft ) {
                        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 );
+                       send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
+                                       NULL, NULL, NULL, NULL );
                        rc = 0;
                        goto finish;
                }
@@ -131,16 +146,13 @@ ldap_back_search(
                /* negative hard limit means no limit */
        }
 
+       /* should we check return values? */
        if (deref != -1)
                ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
        if (tlimit != -1)
                ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&tlimit);
        if (slimit != -1)
                ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&slimit);
-       
-       if ( !ldap_back_dobind( lc, op ) ) {
-               return( -1 );
-       }
 
        /*
         * Rewrite the search base, if required
@@ -164,13 +176,13 @@ ldap_back_search(
                
        case REWRITE_REGEXEC_UNWILLING:
                send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                               NULL, "Unwilling to perform", NULL, NULL );
+                               NULL, "Operation not allowed", NULL, NULL );
                rc = -1;
                goto finish;
 
        case REWRITE_REGEXEC_ERR:
                send_ldap_result( conn, op, LDAP_OTHER,
-                               NULL, "Operations error", NULL, NULL );
+                               NULL, "Rewrite error", NULL, NULL );
                rc = -1;
                goto finish;
        }
@@ -203,8 +215,13 @@ ldap_back_search(
                
        case REWRITE_REGEXEC_UNWILLING:
                send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                               NULL, "Unwilling to perform", NULL, NULL );
+                               NULL, "Operation not allowed", NULL, NULL );
+               rc = -1;
+               goto finish;
+
        case REWRITE_REGEXEC_ERR:
+               send_ldap_result( conn, op, LDAP_OTHER,
+                               NULL, "Rewrite error", NULL, NULL );
                rc = -1;
                goto finish;
        }
@@ -218,7 +235,7 @@ ldap_back_search(
 #else /* !ENABLE_REWRITE */
                        filterstr,
 #endif /* !ENABLE_REWRITE */
-                       0);
+                       BACKLDAP_MAP);
        if ( mapped_filter == NULL ) {
 #ifdef ENABLE_REWRITE
                mapped_filter = mfilter.bv_val;
@@ -233,7 +250,7 @@ ldap_back_search(
        }
 #endif /* ENABLE_REWRITE */
 
-       mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
+       mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, BACKLDAP_MAP);
        if ( mapped_attrs == NULL && attrs) {
                for (count=0; attrs[count].an_name.bv_val; count++);
                mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
@@ -243,9 +260,9 @@ ldap_back_search(
                mapped_attrs[count] = NULL;
        }
 
-       if ((msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter, mapped_attrs,
-               attrsonly)) == -1)
-       {
+       msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter,
+                       mapped_attrs, attrsonly);
+       if ( msgid == -1 ) {
 fail:;
                rc = ldap_back_op_result(lc, op);
                goto finish;
@@ -266,16 +283,61 @@ fail:;
                        rc = 0;
                        goto finish;
                }
+
                if (rc == 0) {
                        tv.tv_sec = 0;
                        tv.tv_usec = 100000;
                        ldap_pvt_thread_yield();
+
                } else if (rc == LDAP_RES_SEARCH_ENTRY) {
                        e = ldap_first_entry(lc->ld,res);
-                       if ( ldap_send_entry(be, op, lc, e, attrs, attrsonly) == LDAP_SUCCESS ) {
+                       if ( ldap_send_entry(be, op, lc, e, attrs, attrsonly) 
+                                       == LDAP_SUCCESS ) {
                                count++;
                        }
                        ldap_msgfree(res);
+
+               } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
+                       char            **references = NULL;
+                       LDAPControl     **ctrls = NULL;
+                       BerVarray       refs;
+                       int             cnt;
+
+                       rc = ldap_parse_reference( lc->ld, res,
+                                       &references, &ctrls, 1 );
+
+                       if ( rc != LDAP_SUCCESS ) {
+                               continue;
+                       }
+
+                       if ( references == NULL ) {
+                               continue;
+                       }
+
+                       for ( cnt = 0; references[ cnt ]; cnt++ )
+                               /* NO OP */ ;
+                               
+                       refs = ch_calloc( cnt + 1, sizeof( struct berval ) );
+
+                       for ( cnt = 0; references[ cnt ]; cnt++ ) {
+                               refs[ cnt ].bv_val = references[ cnt ];
+                               refs[ cnt ].bv_len = strlen( references[ cnt ] );
+                       }
+
+                       /* ignore return value by now */
+                       ( void )send_search_reference( be, conn, op, 
+                                       NULL, refs, ctrls, &v2refs );
+
+                       /* cleanup */
+                       if ( references ) {
+                               ldap_value_free( references );
+                               ch_free( refs );
+                       }
+
+                       if ( ctrls ) {
+                               ldap_controls_free( ctrls );
+                       }
+
                } else {
                        sres = ldap_result2error(lc->ld, res, 1);
                        sres = ldap_back_map_result(sres);
@@ -311,22 +373,27 @@ fail:;
                        break;
                        
                case REWRITE_REGEXEC_UNWILLING:
-                       send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                                       NULL, "Unwilling to perform",
-                                       NULL, NULL );
                        
                case REWRITE_REGEXEC_ERR:
-                       rc = -1;
-                       goto finish;
+                       /* FIXME: no error, but no matched ... */
+                       mmatch = NULL;
+                       break;
                }
        }
 
+       if ( v2refs ) {
+               sres = LDAP_REFERRAL;
+       }
+
        send_search_result( conn, op, sres,
-               mmatch, err, NULL, NULL, count );
+               mmatch, err, v2refs, NULL, count );
 
 #else /* !ENABLE_REWRITE */
+       if ( v2refs ) {
+               sres = LDAP_REFERRAL;
+       }
        send_search_result( conn, op, sres,
-               match, err, NULL, NULL, count );
+               match, err, v2refs, NULL, count );
 #endif /* !ENABLE_REWRITE */
 
 finish:;
@@ -424,8 +491,8 @@ ldap_send_entry(
        attrp = &ent.e_attrs;
 
        while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
-               ldap_back_map(&li->at_map, &a, &mapped, 1);
-               if (mapped.bv_val == NULL)
+               ldap_back_map(&li->at_map, &a, &mapped, BACKLDAP_REMAP);
+               if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
                        continue;
                attr = (Attribute *)ch_malloc( sizeof(Attribute) );
                if (attr == NULL)
@@ -455,16 +522,23 @@ ldap_send_entry(
                        continue;
                }
                
-               if (ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR ) {
+               if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
+                               || attr->a_vals == NULL ) {
+                       /*
+                        * Note: attr->a_vals can be null when using
+                        * values result filter
+                        */
                        attr->a_vals = &dummy;
+
                } else if ( attr->a_desc == slap_schema.si_ad_objectClass
                                || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
                        int i, last;
-                       assert( attr->a_vals );
+
                        for ( last = 0; attr->a_vals[last].bv_val; last++ ) ;
                        for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
-                               ldap_back_map(&li->oc_map, bv, &mapped, 1);
-                               if (mapped.bv_val == NULL) {
+                               ldap_back_map(&li->oc_map, bv, &mapped,
+                                               BACKLDAP_REMAP);
+                               if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
                                        LBER_FREE(bv->bv_val);
                                        bv->bv_val = NULL;
                                        if (--last < 0)
@@ -497,7 +571,6 @@ ldap_send_entry(
                } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
                                        SLAPD_DN_SYNTAX ) == 0 ) {
                        int i;
-                       assert( attr->a_vals );
                        for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
                                struct berval newval;