]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapi/slapi_utils.c
Do not return pointers into BerElement we do not own
[openldap] / servers / slapd / slapi / slapi_utils.c
index 50b2db21d45f23ccfe0d28abcb7db12670135fca..702042fbc773f9a1b16401af162c3149e1e6247d 100644 (file)
@@ -902,6 +902,7 @@ slapi_dn_issuffix(
 #ifdef LDAP_SLAPI
        struct berval   bdn, ndn;
        struct berval   bsuffix, nsuffix;
+       int rc;
 
        assert( dn != NULL );
        assert( suffix != NULL );
@@ -912,10 +913,21 @@ slapi_dn_issuffix(
        bsuffix.bv_val = suffix;
        bsuffix.bv_len = strlen( suffix );
 
-       dnNormalize2( NULL, &bdn, &ndn );
-       dnNormalize2( NULL, &bsuffix, &nsuffix );
+       if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
+               return 0;
+       }
 
-       return dnIsSuffix( &ndn, &nsuffix );
+       if ( dnNormalize2( NULL, &bsuffix, &nsuffix ) != LDAP_SUCCESS ) {
+               slapi_ch_free( (void **)&ndn.bv_val );
+               return 0;
+       }
+
+       rc = dnIsSuffix( &ndn, &nsuffix );
+
+       slapi_ch_free( (void **)&ndn.bv_val );
+       slapi_ch_free( (void **)&nsuffix.bv_val );
+
+       return rc;
 #else /* LDAP_SLAPI */
        return 0;
 #endif /* LDAP_SLAPI */
@@ -1353,7 +1365,7 @@ slapi_send_ldap_result(
        char            *extOID = NULL;
        struct berval   *extValue = NULL;
        int             rc;
-       SlapReply       rs;
+       SlapReply       rs = { REP_RESULT };
 
        slapi_pblock_get( pb, SLAPI_OPERATION, &op );
 
@@ -1363,25 +1375,24 @@ slapi_send_ldap_result(
        rs.sr_ref = NULL;
        rs.sr_ctrls = NULL;
 
+       slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
+
        if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
-               rs.sr_type = REP_SASL;
                slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
-
                send_ldap_sasl( op, &rs );
                return;
        }
 
        slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
        if ( extOID != NULL ) {
-               rs.sr_type = REP_EXTENDED;
                rs.sr_rspoid = extOID;
                slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
-               send_ldap_extended_response( op, &rs );
+               send_ldap_extended( op, &rs );
                return;
        }
 
-       rs.sr_type = REP_RESULT;
-       slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
+       if (op->o_tag == LDAP_REQ_SEARCH)
+               rs.sr_nentries = nentries;
 
        send_ldap_result( op, &rs );
 #endif /* LDAP_SLAPI */
@@ -1396,11 +1407,8 @@ slapi_send_ldap_search_entry(
        int             attrsonly )
 {
 #ifdef LDAP_SLAPI
-       Backend         *be;
-       Connection      *pConn;
        Operation       *pOp;
-       int             rc;
-       SlapReply       rs;
+       SlapReply       rs = { REP_RESULT };
        int             i;
        AttributeName   *an = NULL;
        const char      *text;
@@ -1422,7 +1430,6 @@ slapi_send_ldap_search_entry(
                an[i].an_name.bv_val = NULL;
        }
 
-       rs.sr_type = REP_SEARCH;
        rs.sr_err = LDAP_SUCCESS;
        rs.sr_matched = NULL;
        rs.sr_text = NULL;
@@ -1432,14 +1439,11 @@ slapi_send_ldap_search_entry(
        rs.sr_entry = e;
        rs.sr_v2ref = NULL;
 
-       if ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) {
-               rc = LDAP_OTHER;
-       } else {
-               rc = send_search_entry( pOp, &rs );
+       if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp ) != 0 ) {
+               return LDAP_OTHER;
        }
 
-       return rc;
-
+       return send_search_entry( pOp, &rs );
 #else /* LDAP_SLAPI */
        return -1;
 #endif /* LDAP_SLAPI */
@@ -1861,7 +1865,6 @@ slapi_send_ldap_extended_response(
 #ifdef LDAP_SLAPI
        SlapReply       rs;
 
-       rs.sr_type = REP_EXTENDED;
        rs.sr_err = errornum;
        rs.sr_matched = NULL;
        rs.sr_text = NULL;
@@ -2085,12 +2088,12 @@ slapi_free_search_results_internal( Slapi_PBlock *pb )
 #endif /* LDAP_SLAPI */
 }
 
+#ifdef LDAP_SLAPI
 /*
  * Internal API to prime a Slapi_PBlock with a Backend.
  */
-int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
+static int initBackendPB( Slapi_PBlock *pb, Backend *be )
 {
-#ifdef LDAP_SLAPI
        int rc;
        
        rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
@@ -2104,12 +2107,8 @@ int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
        }
 
        return LDAP_SUCCESS;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
 
-#ifdef LDAP_SLAPI
 /*
  * If oldStyle is TRUE, then a value suitable for setting to
  * the deprecated SLAPI_CONN_AUTHTYPE value is returned 
@@ -2151,14 +2150,12 @@ static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls
 
        return authType;
 }
-#endif
 
 /*
  * Internal API to prime a Slapi_PBlock with a Connection.
  */
-int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
+static int initConnectionPB( Slapi_PBlock *pb, Connection *conn )
 {
-#ifdef LDAP_SLAPI
        char *connAuthType;
        int rc;
 
@@ -2220,31 +2217,33 @@ int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
        }
 
        return rc;
-#else
-       return -1;
-#endif /* LDAP_SLAPI */
 }
+#endif /* LDAP_SLAPI */
 
 /*
  * Internal API to prime a Slapi_PBlock with an Operation.
  */
-int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
+int slapi_x_pblock_set_operation( Slapi_PBlock *pb, Operation *op )
 {
 #ifdef LDAP_SLAPI
        int isRoot = 0;
        int isUpdateDn = 0;
        int rc;
-       Backend *be;
        char *opAuthType;
 
-       if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
-               be = NULL;
-       }
-       if (be != NULL) {
-               isRoot = be_isroot( be, &op->o_ndn );
-               isUpdateDn = be_isupdate( be, &op->o_ndn );
+       if ( op->o_bd != NULL ) {
+               isRoot = be_isroot( op->o_bd, &op->o_ndn );
+               isUpdateDn = be_isupdate( op->o_bd, &op->o_ndn );
        }
-               
+
+       rc = initBackendPB( pb, op->o_bd );
+       if ( rc != LDAP_SUCCESS )
+               return rc;
+
+       rc = initConnectionPB( pb, op->o_conn );
+       if ( rc != LDAP_SUCCESS )
+               return rc;
+
        rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
        if ( rc != LDAP_SUCCESS )
                return rc;
@@ -3205,7 +3204,7 @@ LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
 {
 #ifdef LDAP_SLAPI
-       Modifications *modlist, **modtail;
+       Modifications *modlist = NULL, **modtail;
        LDAPMod **modp;
 
        modtail = &modlist;