]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/result.c
Import ITS#3278 from HEAD
[openldap] / libraries / libldap / result.c
index deecde2d6a93457a87489181b62197cf1184bdc0..1c48c338a78087cb5cd3a7cfe3e851d52ba96baa 100644 (file)
@@ -1,14 +1,22 @@
+/* result.c - wait for an ldap result */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2005 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/*  Portions
- *  Copyright (c) 1990 Regents of the University of Michigan.
- *  All rights reserved.
+/* Portions Copyright (c) 1990 Regents of the University of Michigan.
+ * All rights reserved.
  */
-/*---
- * This notice applies to changes, created by or for Novell, Inc.,
+/* This notice applies to changes, created by or for Novell, Inc.,
  * to preexisting works for which notices appear elsewhere in this file.
  *
  * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
  *---
  * Modification to OpenLDAP source by Novell, Inc.
  * April 2000 sfs Add code to process V3 referrals and search results
- *
- *  result.c - wait for an ldap result
- */
-/* Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License 
+ *---
+ * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License 
  * can be found in the file "build/LICENSE-2.0.1" in this distribution
  * of OpenLDAP Software.
  */
+/* Portions Copyright (C) The Internet Society (1997)
+ * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
+ */
 
 /*
  * LDAPv3 (RFC2251)
@@ -64,7 +73,7 @@ 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 ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
-       int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
+       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 ));
 static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
@@ -185,7 +194,7 @@ chkResponseList(
                        for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
                                if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
                                    && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
-                                       && tmp->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL )
+                                       && tmp->lm_msgtype != LDAP_RES_INTERMEDIATE )
                                {
                                        break;
                                }
@@ -305,23 +314,21 @@ wait4msg(
         if( (*result = chkResponseList(ld, msgid, all)) != NULL ) {
             rc = (*result)->lm_msgtype;
         } else {
+                       int lc_ready = 0;
 
-                       for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
+                       for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
+                               nextlc = lc->lconn_next;
                                if ( ber_sockbuf_ctrl( lc->lconn_sb,
                                                LBER_SB_OPT_DATA_READY, NULL ) ) {
-                                           rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
-                                               lc, result );
+                                       rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
+                                               &lc, result );
+                                       lc_ready = 1;
                                    break;
                                }
                }
 
-                   if ( lc == NULL ) {
+                   if ( !lc_ready ) {
                            rc = ldap_int_select( ld, tvp );
-#ifdef LDAP_R_COMPILE
-                           ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
-                           ldap_pvt_thread_yield();
-                           ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
-#endif
 #ifdef LDAP_DEBUG
                            if ( rc == -1 ) {
 #ifdef NEW_LOGGING
@@ -342,7 +349,7 @@ wait4msg(
                            {
                                    ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
                                        LDAP_TIMEOUT);
-                                   return( rc );
+                                   return rc;
                            }
 
                            if ( rc == -1 ) {
@@ -369,7 +376,8 @@ wait4msg(
                                                ldap_is_read_ready( ld,
                                                lc->lconn_sb )) {
                                                    rc = try_read1msg( ld, msgid, all,
-                                                       lc->lconn_sb, lc, result );
+                                                       lc->lconn_sb, &lc, result );
+                                                       if ( lc == NULL ) lc = nextlc;
                                            }
                                    }
                            }
@@ -405,7 +413,7 @@ try_read1msg(
        ber_int_t msgid,
        int all,
        Sockbuf *sb,
-       LDAPConn *lc,
+       LDAPConn **lcp,
        LDAPMessage **result )
 {
        BerElement      *ber;
@@ -415,9 +423,13 @@ try_read1msg(
        ber_len_t       len;
        int             foundit = 0;
        LDAPRequest     *lr, *tmplr;
+       LDAPConn        *lc;
        BerElement      tmpber;
        int             rc, refer_cnt, hadref, simple_request;
        ber_int_t       lderr;
+#ifdef LDAP_CONNECTIONLESS
+       int             firstmsg = 1, moremsgs = 0, isv2 = 0;
+#endif
        /*
         * v3ref = flag for V3 referral / search reference
         * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
@@ -425,7 +437,8 @@ try_read1msg(
        int     v3ref;
 
        assert( ld != NULL );
-       assert( lc != NULL );
+       assert( lcp != NULL );
+       assert( *lcp != NULL );
        
 #ifdef NEW_LOGGING
        LDAP_LOG ( OPERATION, ARGS, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
@@ -433,6 +446,8 @@ try_read1msg(
        Debug( LDAP_DEBUG_TRACE, "read1msg: msgid %d, all %d\n", msgid, all, 0 );
 #endif
 
+       lc = *lcp;
+
 retry:
        if ( lc->lconn_ber == NULL ) {
                lc->lconn_ber = ldap_alloc_ber_with_options(ld);
@@ -451,7 +466,9 @@ retry:
        if ( LDAP_IS_UDP(ld) ) {
                struct sockaddr from;
                ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
+               if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
        }
+nextresp3:
 #endif
        tag = ber_get_next( sb, &len, ber );
        if ( tag == LDAP_TAG_MESSAGE ) {
@@ -520,10 +537,10 @@ retry_ber:
                goto retry_ber;
        }
 #ifdef LDAP_CONNECTIONLESS
-       if (LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
-               struct berval blank;
-               ber_scanf(ber, "m{", &blank);
+       if (LDAP_IS_UDP(ld) && isv2) {
+               ber_scanf(ber, "x{");
        }
+nextresp2:
 #endif
        /* the message type */
        if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
@@ -660,11 +677,9 @@ retry_ber:
         * go through the following code.  This code also chases V2 referrals
         * and checks if all referrals have been chased.
         */
-       if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1)
-#ifdef LDAP_RES_INTERMEDIATE_RESP
-               && (tag != LDAP_RES_INTERMEDIATE_RESP )
-#endif
-       ) {
+       if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) &&
+               (tag != LDAP_RES_INTERMEDIATE ))
+       {
                /* For a v3 search referral/reference, only come here if already chased it */
                if ( ld->ld_version >= LDAP_VERSION2 &&
                        ( lr->lr_parent != NULL ||
@@ -808,6 +823,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 
                        if ( lc != NULL ) {
                                ldap_free_connection( ld, lc, 0, 1 );
+                               *lcp = NULL;
                        }
                }
        }
@@ -826,6 +842,84 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
        new->lm_msgtype = tag;
        new->lm_ber = ber;
 
+#ifdef LDAP_CONNECTIONLESS
+       /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
+        * the responses are all a sequence wrapped in one message. In
+        * LDAPv3 each response is in its own message. The datagram must
+        * end with a SearchResult. We can't just parse each response in
+        * separate calls to try_read1msg because the header info is only
+        * present at the beginning of the datagram, not at the beginning
+        * of each response. So parse all the responses at once and queue
+        * them up, then pull off the first response to return to the
+        * caller when all parsing is complete.
+        */
+       if ( LDAP_IS_UDP(ld) ) {
+               /* If not a result, look for more */
+               if ( tag != LDAP_RES_SEARCH_RESULT ) {
+                       int ok = 0;
+                       moremsgs = 1;
+                       if (isv2) {
+                               /* LDAPv2: dup the current ber, skip past the current
+                                * response, and see if there are any more after it.
+                                */
+                               ber = ber_dup( ber );
+                               ber_scanf( ber, "x" );
+                               if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
+                                       /* There's more - dup the ber buffer so they can all be
+                                        * individually freed by ldap_msgfree.
+                                        */
+                                       struct berval bv;
+                                       ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
+                                       bv.bv_val = LDAP_MALLOC(len);
+                                       if (bv.bv_val) {
+                                               ok=1;
+                                               ber_read(ber, bv.bv_val, len);
+                                               bv.bv_len = len;
+                                               ber_init2(ber, &bv, ld->ld_lberoptions );
+                                       }
+                               }
+                       } else {
+                               /* LDAPv3: Just allocate a new ber. Since this is a buffered
+                                * datagram, if the sockbuf is readable we still have data
+                                * to parse.
+                                */
+                               ber = ldap_alloc_ber_with_options(ld);
+                               if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
+                       }
+                       /* set up response chain */
+                       if ( firstmsg ) {
+                               firstmsg = 0;
+                               new->lm_next = ld->ld_responses;
+                               ld->ld_responses = new;
+                       } else {
+                               tmp->lm_chain = new;
+                       }
+                       tmp = new;
+                       /* "ok" means there's more to parse */
+                       if (ok) {
+                               if (isv2) goto nextresp2;
+                               else goto nextresp3;
+                       } else {
+                               /* got to end of datagram without a SearchResult. Free
+                                * our dup'd ber, but leave any buffer alone. For v2 case,
+                                * the previous response is still using this buffer. For v3,
+                                * the new ber has no buffer to free yet.
+                                */
+                               ber_free(ber, 0);
+                               return -1;
+                       }
+               } else if ( moremsgs ) {
+               /* got search result, and we had multiple responses in 1 datagram.
+                * stick the result onto the end of the chain, and then pull the
+                * first response off the head of the chain.
+                */
+                       tmp->lm_chain = new;
+                       *result = chkResponseList( ld, msgid, all );
+                       ld->ld_errno = LDAP_SUCCESS;
+                       return( (*result)->lm_msgtype );
+               }
+       }
+#endif
 
        /* is this the one we're looking for? */
        if ( msgid == LDAP_RES_ANY || id == msgid ) {
@@ -858,12 +952,12 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
        if ( l == NULL ) {
                if ( foundit ) {
                        *result = new;
-                       goto leave;
+                       goto exit;
                }
 
                new->lm_next = ld->ld_responses;
                ld->ld_responses = new;
-               goto leave;
+               goto exit;
        }
 
 #ifdef NEW_LOGGING
@@ -879,7 +973,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
        for ( tmp = l; (tmp->lm_chain != NULL) &&
                ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
                 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
-                        (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
+                        (tmp->lm_chain->lm_msgtype == LDAP_RES_INTERMEDIATE ));
            tmp = tmp->lm_chain )
                ;       /* NULL */
        tmp->lm_chain = new;
@@ -893,7 +987,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
                *result = l;
        }
 
-leave:
+exit:
        if ( foundit ) {
                ld->ld_errno = LDAP_SUCCESS;
                return( tag );
@@ -1031,7 +1125,7 @@ char * ldap_int_msgtype2str( ber_tag_t tag )
        case LDAP_RES_COMPARE: return "compare";
        case LDAP_RES_DELETE: return "delete";
        case LDAP_RES_EXTENDED: return "extended-result";
-       case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
+       case LDAP_RES_INTERMEDIATE: return "intermediate";
        case LDAP_RES_MODIFY: return "modify";
        case LDAP_RES_RENAME: return "rename";
        case LDAP_RES_SEARCH_ENTRY: return "search-entry";