]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-sql/search.c
ITS#6001 SID of queued CSN must match the one in the op
[openldap] / servers / slapd / back-sql / search.c
index 93b7156ad016cb180eaac4eb4325b905d25c23e4..a65ffcf027388734073696f83078dd9a92eb739f 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1999-2007 The OpenLDAP Foundation.
+ * Copyright 1999-2009 The OpenLDAP Foundation.
  * Portions Copyright 1999 Dmitry Kovalev.
  * Portions Copyright 2002 Pierangelo Masarati.
  * Portions Copyright 2004 Mark Adamson.
@@ -184,7 +184,7 @@ backsql_init_search(
                        BER_BVZERO( &bsi->bsi_attrs[ 0 ].an_name );
        
                        for ( p = attrs; !BER_BVISNULL( &p->an_name ); p++ ) {
-                               if ( BACKSQL_NCMP( &p->an_name, &AllUser ) == 0 ) {
+                               if ( BACKSQL_NCMP( &p->an_name, slap_bv_all_user_attrs ) == 0 ) {
                                        /* handle "*" */
                                        bsi->bsi_flags |= BSQL_SF_ALL_USER;
 
@@ -198,7 +198,7 @@ backsql_init_search(
                                        }
                                        continue;
 
-                               } else if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
+                               } else if ( BACKSQL_NCMP( &p->an_name, slap_bv_all_operational_attrs ) == 0 ) {
                                        /* handle "+" */
                                        bsi->bsi_flags |= BSQL_SF_ALL_OPER;
 
@@ -212,7 +212,7 @@ backsql_init_search(
                                        }
                                        continue;
 
-                               } else if ( BACKSQL_NCMP( &p->an_name, &NoAttrs ) == 0 ) {
+                               } else if ( BACKSQL_NCMP( &p->an_name, slap_bv_no_attrs ) == 0 ) {
                                        /* ignore "1.1" */
                                        continue;
 
@@ -237,7 +237,7 @@ backsql_init_search(
                        
                        /* use hints if available */
                        for ( p = bi->sql_anlist; !BER_BVISNULL( &p->an_name ); p++ ) {
-                               if ( BACKSQL_NCMP( &p->an_name, &AllUser ) == 0 ) {
+                               if ( BACKSQL_NCMP( &p->an_name, slap_bv_all_user_attrs ) == 0 ) {
                                        /* handle "*" */
                                        bsi->bsi_flags |= BSQL_SF_ALL_USER;
 
@@ -251,7 +251,7 @@ backsql_init_search(
                                        }
                                        continue;
 
-                               } else if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
+                               } else if ( BACKSQL_NCMP( &p->an_name, slap_bv_all_operational_attrs ) == 0 ) {
                                        /* handle "+" */
                                        bsi->bsi_flags |= BSQL_SF_ALL_OPER;
 
@@ -339,6 +339,17 @@ backsql_init_search(
                                rs->sr_err = rc;
                        }
                }
+
+               if ( gotit && BACKSQL_IS_GET_OC( flags ) ) {
+                       bsi->bsi_base_id.eid_oc = backsql_id2oc( bi,
+                               bsi->bsi_base_id.eid_oc_id );
+                       if ( bsi->bsi_base_id.eid_oc == NULL ) {
+                               /* error? */
+                               backsql_free_entryID( &bsi->bsi_base_id, 1,
+                                       op->o_tmpmemctx );
+                               rc = rs->sr_err = LDAP_OTHER;
+                       }
+               }
        }
 
        bsi->bsi_status = rc;
@@ -853,7 +864,7 @@ backsql_process_filter( backsql_srch_info *bsi, Filter *f )
                struct berval   keyval;
 #else /* ! BACKSQL_ARBITRARY_KEY */
                unsigned long   keyval;
-               char            keyvalbuf[] = "18446744073709551615";
+               char            keyvalbuf[LDAP_PVT_INTTYPE_CHARS(unsigned long)];
 #endif /* ! BACKSQL_ARBITRARY_KEY */
 
                switch ( f->f_choice ) {
@@ -1512,6 +1523,7 @@ backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
                                        (ber_len_t)STRLENOF( "9=9"), "9=9");
 
                } else if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) {
+                       /* This should always be true... */
                        backsql_strfcat_x( &bsi->bsi_join_where,
                                        bsi->bsi_op->o_tmpmemctx,
                                        "b",
@@ -1548,11 +1560,13 @@ backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
                {
                        lowid = PAGECOOKIE_TO_SQL_ID( ((PagedResultsState *)bsi->bsi_op->o_pagedresults_state)->ps_cookie );
                }
+
                if ( lowid ) {
                        char lowidstring[48];
                        int  lowidlen;
 
-                       lowidlen = snprintf( lowidstring, 48, " AND ldap_entries.id>%d", lowid );
+                       lowidlen = snprintf( lowidstring, sizeof( lowidstring ),
+                               " AND ldap_entries.id>%lu", lowid );
                        backsql_strfcat_x( &bsi->bsi_join_where,
                                        bsi->bsi_op->o_tmpmemctx,
                                        "l",
@@ -1912,6 +1926,7 @@ backsql_oc_get_candidates( void *v_oc, void *v_bsi )
                        goto cleanup;
                }
 #endif /* ! BACKSQL_ARBITRARY_KEY */
+               c_id->eid_oc = bsi->bsi_oc;
                c_id->eid_oc_id = bsi->bsi_oc->bom_id;
 
                c_id->eid_dn = pdn;
@@ -2112,7 +2127,6 @@ backsql_search( Operation *op, SlapReply *rs )
 
        /* If paged results are in effect, check the paging cookie */
        if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
-               PagedResultsState *ps = op->o_pagedresults_state;
                rs->sr_err = parse_paged_cookie( op, rs );
                if ( rs->sr_err != LDAP_SUCCESS ) {
                        send_ldap_result( op, rs );
@@ -2175,9 +2189,9 @@ backsql_search( Operation *op, SlapReply *rs )
         * and then send to client; don't free entry_id if baseObject...
         */
        for ( eid = bsi.bsi_id_list;
-                       eid != NULL; 
-                       eid = backsql_free_entryID( op,
-                               eid, eid == &bsi.bsi_base_id ? 0 : 1 ) )
+               eid != NULL; 
+               eid = backsql_free_entryID( 
+                       eid, eid == &bsi.bsi_base_id ? 0 : 1, op->o_tmpmemctx ) )
        {
                int             rc;
                Attribute       *a_hasSubordinate = NULL,
@@ -2326,6 +2340,9 @@ backsql_search( Operation *op, SlapReply *rs )
                        rs->sr_ref = NULL;
                        rs->sr_matched = NULL;
                        rs->sr_entry = NULL;
+                       if ( rs->sr_err == LDAP_REFERRAL ) {
+                               rs->sr_err = LDAP_SUCCESS;
+                       }
 
                        goto next_entry;
                }
@@ -2405,9 +2422,11 @@ backsql_search( Operation *op, SlapReply *rs )
                        rs->sr_attrs = op->ors_attrs;
                        rs->sr_operational_attrs = NULL;
                        rs->sr_entry = e;
+                       e->e_private = (void *)eid;
                        rs->sr_flags = ( e == &user_entry ) ? REP_ENTRY_MODIFIABLE : 0;
                        /* FIXME: need the whole entry (ITS#3480) */
                        rs->sr_err = send_search_entry( op, rs );
+                       e->e_private = NULL;
                        rs->sr_entry = NULL;
                        rs->sr_attrs = NULL;
                        rs->sr_operational_attrs = NULL;
@@ -2456,8 +2475,8 @@ send_results:;
 
        /* cleanup in case of abandon */
        for ( ; eid != NULL; 
-                       eid = backsql_free_entryID( op,
-                               eid, eid == &bsi.bsi_base_id ? 0 : 1 ) )
+               eid = backsql_free_entryID(
+                       eid, eid == &bsi.bsi_base_id ? 0 : 1, op->o_tmpmemctx ) )
                ;
 
        backsql_entry_clean( op, &base_entry );
@@ -2495,7 +2514,7 @@ send_results:;
 #endif /* BACKSQL_SYNCPROV */
 
 done:;
-       (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
+       (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
 
        if ( bsi.bsi_attrs != NULL ) {
                op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
@@ -2538,8 +2557,8 @@ backsql_entry_get(
        *ent = NULL;
 
        rc = backsql_get_db_conn( op, &dbh );
-       if ( !dbh ) {
-               return LDAP_OTHER;
+       if ( rc != LDAP_SUCCESS ) {
+               return rc;
        }
 
        if ( at ) {
@@ -2557,7 +2576,7 @@ backsql_entry_get(
                        BACKSQL_ISF_GET_ENTRY );
 
        if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
-               (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
+               (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
        }
 
        if ( rc == LDAP_SUCCESS ) {
@@ -2639,7 +2658,7 @@ backsql_entry_release(
 {
        backsql_entry_clean( op, e );
 
-       ch_free( e );
+       entry_free( e );
 
        return 0;
 }
@@ -2649,12 +2668,7 @@ backsql_entry_release(
 static int
 parse_paged_cookie( Operation *op, SlapReply *rs )
 {
-       LDAPControl     **c;
        int             rc = LDAP_SUCCESS;
-       ber_tag_t       tag;
-       ber_int_t       size;
-       BerElement      *ber;
-       struct berval   cookie = BER_BVNULL;
        PagedResultsState *ps = op->o_pagedresults_state;
 
        /* this function must be invoked only if the pagedResults
@@ -2662,53 +2676,17 @@ parse_paged_cookie( Operation *op, SlapReply *rs )
         * by the frontend */
        assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
 
-       /* look for the appropriate ctrl structure */
-       for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
-               if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
-               {
-                       break;
-               }
-       }
-
-       if ( c[0] == NULL ) {
-               rs->sr_text = "missing pagedResults control";
-               return LDAP_PROTOCOL_ERROR;
-       }
-
-       /* Tested by frontend */
-       assert( c[0]->ldctl_value.bv_len > 0 );
-
-       /* Parse the control value
-        *      realSearchControlValue ::= SEQUENCE {
-        *              size    INTEGER (0..maxInt),
-        *                              -- requested page size from client
-        *                              -- result set size estimate from server
-        *              cookie  OCTET STRING
-        * }
-        */
-       ber = ber_init( &c[0]->ldctl_value );
-       if ( ber == NULL ) {
-               rs->sr_text = "internal error";
-               return LDAP_OTHER;
-       }
-
-       tag = ber_scanf( ber, "{im}", &size, &cookie );
-
-       /* Tested by frontend */
-       assert( tag != LBER_ERROR );
-       assert( size >= 0 );
-
        /* cookie decoding/checks deferred to backend... */
-       if ( cookie.bv_len ) {
+       if ( ps->ps_cookieval.bv_len ) {
                PagedResultsCookie reqcookie;
-               if( cookie.bv_len != sizeof( reqcookie ) ) {
+               if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
                        /* bad cookie */
                        rs->sr_text = "paged results cookie is invalid";
                        rc = LDAP_PROTOCOL_ERROR;
                        goto done;
                }
 
-               AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
+               AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
 
                if ( reqcookie > ps->ps_cookie ) {
                        /* bad cookie */
@@ -2724,22 +2702,11 @@ parse_paged_cookie( Operation *op, SlapReply *rs )
 
        } else {
                /* Initial request.  Initialize state. */
-#if 0
-               if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
-                       /* There's another pagedResults control on the
-                        * same connection; reject new pagedResults controls 
-                        * (allowed by RFC2696) */
-                       rs->sr_text = "paged results cookie unavailable; try later";
-                       rc = LDAP_UNWILLING_TO_PERFORM;
-                       goto done;
-               }
-#endif
                ps->ps_cookie = 0;
                ps->ps_count = 0;
        }
 
 done:;
-       (void)ber_free( ber, 1 );
 
        return rc;
 }