]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/result.c
NT ldap_pvt_thread_self fix from HEAD
[openldap] / libraries / libldap / result.c
index 28f20e6b28543d1af15df07b1d03d9a30adcd408..0614f41be9b839e20db8db7b033c24ca1c16a851 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-2004 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)
@@ -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;
                                }
@@ -413,6 +422,9 @@ try_read1msg(
        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
@@ -446,7 +458,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 ) {
@@ -515,10 +529,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 ) {
@@ -655,11 +669,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 ||
@@ -821,6 +833,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 ) {
@@ -853,12 +943,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
@@ -874,7 +964,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;
@@ -888,7 +978,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
                *result = l;
        }
 
-leave:
+exit:
        if ( foundit ) {
                ld->ld_errno = LDAP_SUCCESS;
                return( tag );
@@ -1026,7 +1116,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";