]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/result.c
Removed numerous memory leaks detected by Mark Meredith.
[openldap] / libraries / libldap / result.c
index 81c12f80cee89914d60fc7c0a5045732df0b6424..8b03df7697467b60834a69e9bf494f154526892f 100644 (file)
@@ -9,10 +9,23 @@
  *  result.c - wait for an ldap result
  */
 
+/*
+ * LDAPv3 (RFC2251)
+ *     LDAPResult ::= SEQUENCE {
+ *             resultCode              ENUMERATED { ... },
+ *             matchedDN               LDAPDN,
+ *             errorMessage    LDAPString,
+ *             referral                Referral OPTIONAL
+ *     }
+ *     Referral ::= SEQUENCE OF LDAPURL        (one or more)
+ *     LDAPURL ::= LDAPString                          (limited to URL chars)
+ */
+
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
 
 #include <ac/errno.h>
 #include <ac/socket.h>
 #include "ldap-int.h"
 
 
-static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
-static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
-static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
-       LDAPMessage **result ));
-static int try_read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
+static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
+static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
+static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
        LDAPMessage **result ));
-static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
+static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
+       int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
+static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
 
 
@@ -48,7 +61,11 @@ static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPReques
  *     ldap_result( s, msgid, all, timeout, result )
  */
 int
-ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
+ldap_result(
+       LDAP *ld,
+       int msgid,
+       int all,
+       struct timeval *timeout,
        LDAPMessage **result )
 {
        LDAPMessage     *lm, *lastlm, *nextlm;
@@ -74,15 +91,15 @@ ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
         * wait until it arrives or timeout occurs.
         */
 
-       *result = NULLMSG;
-       lastlm = NULLMSG;
-       for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
+       *result = NULL;
+       lastlm = NULL;
+       for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
                nextlm = lm->lm_next;
 
                if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
                        ldap_mark_abandoned( ld, lm->lm_msgid );
 
-                       if ( lastlm == NULLMSG ) {
+                       if ( lastlm == NULL ) {
                                ld->ld_responses = lm->lm_next;
                        } else {
                                lastlm->lm_next = nextlm;
@@ -102,34 +119,33 @@ ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
                            && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
                                break;
 
-                       for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
+                       for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
                                if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
                                        break;
                        }
 
-                       if ( tmp == NULLMSG ) {
-                               return( wait4msg( ld, msgid, all, timeout,
-                                   result ) );
+                       if ( tmp == NULL ) {
+                               lm = NULL;
                        }
 
                        break;
                }
                lastlm = lm;
        }
-       if ( lm == NULLMSG ) {
+       if ( lm == NULL ) {
                return( wait4msg( ld, msgid, all, timeout, result ) );
        }
 
-       if ( lastlm == NULLMSG ) {
-               ld->ld_responses = (all == 0 && lm->lm_chain != NULLMSG
+       if ( lastlm == NULL ) {
+               ld->ld_responses = (all == 0 && lm->lm_chain != NULL
                    ? lm->lm_chain : lm->lm_next);
        } else {
-               lastlm->lm_next = (all == 0 && lm->lm_chain != NULLMSG
+               lastlm->lm_next = (all == 0 && lm->lm_chain != NULL
                    ? lm->lm_chain : lm->lm_next);
        }
        if ( all == 0 )
-               lm->lm_chain = NULLMSG;
-       lm->lm_next = NULLMSG;
+               lm->lm_chain = NULL;
+       lm->lm_next = NULL;
 
        *result = lm;
        ld->ld_errno = LDAP_SUCCESS;
@@ -137,7 +153,11 @@ ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
 }
 
 static int
-wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
+wait4msg(
+       LDAP *ld,
+       ber_int_t msgid,
+       int all,
+       struct timeval *timeout,
        LDAPMessage **result )
 {
        int             rc;
@@ -240,26 +260,32 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 }
 
 
-static int
-try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
-    LDAPConn *lc, LDAPMessage **result )
+static ber_tag_t
+try_read1msg(
+       LDAP *ld,
+       ber_int_t msgid,
+       int all,
+       Sockbuf *sb,
+    LDAPConn *lc,
+       LDAPMessage **result )
 {
        BerElement      *ber;
        LDAPMessage     *new, *l, *prev, *tmp;
-       long            id;
-       unsigned long   tag, len;
+       ber_int_t       id;
+       ber_tag_t       tag;
+       ber_len_t       len;
        int             foundit = 0;
        LDAPRequest     *lr;
        BerElement      tmpber;
        int             rc, refer_cnt, hadref, simple_request;
-       unsigned long   lderr;
+       ber_int_t       lderr;
 
        assert( ld != NULL );
        assert( lc != NULL );
        
        Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
 
-    if ( lc->lconn_ber == NULLBER ) {
+    if ( lc->lconn_ber == NULL ) {
                lc->lconn_ber = ldap_alloc_ber_with_options(ld);
 
                if( lc->lconn_ber == NULL ) {
@@ -296,7 +322,7 @@ try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
      * We read a complete message.
         * The connection should no longer need this ber.
         */
-    lc->lconn_ber = NULLBER;
+    lc->lconn_ber = NULL;
 
        /* message id */
        if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
@@ -306,7 +332,7 @@ try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
        }
 
        /* if it's been abandoned, toss it */
-       if ( ldap_abandoned( ld, (int)id ) ) {
+       if ( ldap_abandoned( ld, id ) ) {
                ber_free( ber, 1 );
                return( -2 );   /* continue looking */
        }
@@ -410,9 +436,8 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
                                if ( !simple_request ) {
                                        ber_free( ber, 1 );
                                        ber = NULL;
-                                       if ( build_result_ber( ld, ber, lr )
+                                       if ( build_result_ber( ld, &ber, lr )
                                            == LBER_ERROR ) {
-                                               ld->ld_errno = LDAP_NO_MEMORY;
                                                rc = -1; /* fatal error */
                                        }
                                }
@@ -465,15 +490,15 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
         * search response.
         */
 
-       prev = NULLMSG;
-       for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
+       prev = NULL;
+       for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
                if ( l->lm_msgid == new->lm_msgid )
                        break;
                prev = l;
        }
 
        /* not part of an existing search response */
-       if ( l == NULLMSG ) {
+       if ( l == NULL ) {
                if ( foundit ) {
                        *result = new;
                        ld->ld_errno = LDAP_SUCCESS;
@@ -489,7 +514,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
            new->lm_msgid, new->lm_msgtype, 0 );
 
        /* part of a search response - add to end of list of entries */
-       for ( tmp = l; tmp->lm_chain != NULLMSG &&
+       for ( tmp = l; tmp->lm_chain != NULL &&
            tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
            tmp = tmp->lm_chain )
                ;       /* NULL */
@@ -497,7 +522,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 
        /* return the whole chain if that's what we were looking for */
        if ( foundit ) {
-               if ( prev == NULLMSG )
+               if ( prev == NULL )
                        ld->ld_responses = l->lm_next;
                else
                        prev->lm_next = l->lm_next;
@@ -521,31 +546,56 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 }
 
 
-static unsigned long
-build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
+static ber_tag_t
+build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
 {
-       unsigned long   len;
-       long            along;
+       ber_len_t       len;
+       ber_int_t       tag;
+       ber_int_t       along;
+       BerElement *ber;
+
+       *bp = NULL;
+       ber = ldap_alloc_ber_with_options( ld );
+
+       if( ber == NULL ) {
+               ld->ld_errno = LDAP_NO_MEMORY;
+               return LBER_ERROR;
+       }
 
-       ber_init_w_nullc( ber, 0 );
-       ldap_set_ber_options( ld, ber );
        if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
-           (unsigned long) lr->lr_res_msgtype, lr->lr_res_errno,
+           lr->lr_res_msgtype, lr->lr_res_errno,
            lr->lr_res_matched ? lr->lr_res_matched : "",
            lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
+
+               ld->ld_errno = LDAP_ENCODING_ERROR;
+               ber_free(ber, 1);
                return( LBER_ERROR );
        }
 
        ber_reset( ber, 1 );
+
        if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
+               ld->ld_errno = LDAP_DECODING_ERROR;
+               ber_free(ber, 1);
                return( LBER_ERROR );
        }
 
        if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
+               ld->ld_errno = LDAP_DECODING_ERROR;
+               ber_free(ber, 1);
                return( LBER_ERROR );
        }
 
-       return( ber_peek_tag( ber, &len ));
+       tag = ber_peek_tag( ber, &len );
+
+       if ( tag == LBER_ERROR ) {
+               ld->ld_errno = LDAP_DECODING_ERROR;
+               ber_free(ber, 1);
+               return( LBER_ERROR );
+       }
+
+       *bp = ber;
+       return tag;
 }
 
 
@@ -615,7 +665,7 @@ ldap_msgfree( LDAPMessage *lm )
 
        Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
 
-       for ( ; lm != NULLMSG; lm = next ) {
+       for ( ; lm != NULL; lm = next ) {
                next = lm->lm_chain;
                type = lm->lm_msgtype;
                ber_free( lm->lm_ber, 1 );
@@ -639,17 +689,17 @@ ldap_msgdelete( LDAP *ld, int msgid )
 
        Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
 
-       prev = NULLMSG;
-       for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
+       prev = NULL;
+       for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
                if ( lm->lm_msgid == msgid )
                        break;
                prev = lm;
        }
 
-       if ( lm == NULLMSG )
+       if ( lm == NULL )
                return( -1 );
 
-       if ( prev == NULLMSG )
+       if ( prev == NULL )
                ld->ld_responses = lm->lm_next;
        else
                prev->lm_next = lm->lm_next;
@@ -665,7 +715,7 @@ ldap_msgdelete( LDAP *ld, int msgid )
  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
  */
 static int
-ldap_abandoned( LDAP *ld, int msgid )
+ldap_abandoned( LDAP *ld, ber_int_t msgid )
 {
        int     i;
 
@@ -681,7 +731,7 @@ ldap_abandoned( LDAP *ld, int msgid )
 
 
 static int
-ldap_mark_abandoned( LDAP *ld, int msgid )
+ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
 {
        int     i;
 
@@ -707,8 +757,9 @@ ldap_mark_abandoned( LDAP *ld, int msgid )
 int
 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
 {
-       int             rc;
-       unsigned long   tag, len;
+       int     rc;
+       ber_tag_t       tag;
+       ber_len_t       len;
 
        if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
                rc = ldap_select1( ld, timeout );
@@ -727,6 +778,6 @@ cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
                return( -1 );
        }
 
-       return( tag );
+       return( 0 );
 }
 #endif /* LDAP_CONNECTIONLESS */