Back to index

openldap  2.4.31
references.c
Go to the documentation of this file.
00001 /* references.c */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 
00021 #include <ac/stdlib.h>
00022 
00023 #include <ac/socket.h>
00024 #include <ac/string.h>
00025 #include <ac/time.h>
00026 
00027 #include "ldap-int.h"
00028 
00029 LDAPMessage *
00030 ldap_first_reference( LDAP *ld, LDAPMessage *chain )
00031 {
00032        assert( ld != NULL );
00033        assert( LDAP_VALID( ld ) );
00034        assert( chain != NULL );
00035 
00036        return chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE
00037               ? chain
00038               : ldap_next_reference( ld, chain );
00039 }
00040 
00041 LDAPMessage *
00042 ldap_next_reference( LDAP *ld, LDAPMessage *ref )
00043 {
00044        assert( ld != NULL );
00045        assert( LDAP_VALID( ld ) );
00046        assert( ref != NULL );
00047 
00048        for (
00049               ref = ref->lm_chain;
00050               ref != NULL;
00051               ref = ref->lm_chain )
00052        {
00053               if( ref->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
00054                      return( ref );
00055               }
00056        }
00057 
00058        return( NULL );
00059 }
00060 
00061 int
00062 ldap_count_references( LDAP *ld, LDAPMessage *chain )
00063 {
00064        int    i;
00065 
00066        assert( ld != NULL );
00067        assert( LDAP_VALID( ld ) );
00068 
00069        for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
00070               if( chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
00071                      i++;
00072               }
00073        }
00074 
00075        return( i );
00076 }
00077 
00078 int
00079 ldap_parse_reference( 
00080        LDAP            *ld,    
00081        LDAPMessage     *ref,
00082        char            ***referralsp,
00083        LDAPControl     ***serverctrls,
00084        int             freeit)
00085 {
00086        BerElement be;
00087        char **refs = NULL;
00088        int rc;
00089 
00090        assert( ld != NULL );
00091        assert( LDAP_VALID( ld ) );
00092        assert( ref !=  NULL );
00093 
00094        if( ref->lm_msgtype != LDAP_RES_SEARCH_REFERENCE ) {
00095               return LDAP_PARAM_ERROR;
00096        }
00097 
00098        /* make a private copy of BerElement */
00099        AC_MEMCPY(&be, ref->lm_ber, sizeof(be));
00100        
00101        if ( ber_scanf( &be, "{v" /*}*/, &refs ) == LBER_ERROR ) {
00102               rc = LDAP_DECODING_ERROR;
00103               goto free_and_return;
00104        }
00105 
00106        if ( serverctrls == NULL ) {
00107               rc = LDAP_SUCCESS;
00108               goto free_and_return;
00109        }
00110 
00111        if ( ber_scanf( &be, /*{*/ "}" ) == LBER_ERROR ) {
00112               rc = LDAP_DECODING_ERROR;
00113               goto free_and_return;
00114        }
00115 
00116        rc = ldap_pvt_get_controls( &be, serverctrls );
00117 
00118 free_and_return:
00119 
00120        if( referralsp != NULL ) {
00121               /* provide references regradless of return code */
00122               *referralsp = refs;
00123 
00124        } else {
00125               LDAP_VFREE( refs );
00126        }
00127 
00128        if( freeit ) {
00129               ldap_msgfree( ref );
00130        }
00131 
00132        if( rc != LDAP_SUCCESS ) {
00133               ld->ld_errno = rc;
00134 
00135               if( ld->ld_matched != NULL ) {
00136                      LDAP_FREE( ld->ld_matched );
00137                      ld->ld_matched = NULL;
00138               }
00139 
00140               if( ld->ld_error != NULL ) {
00141                      LDAP_FREE( ld->ld_error );
00142                      ld->ld_error = NULL;
00143               }
00144        }
00145 
00146        return rc;
00147 }