Back to index

lightning-sunbird  0.9+nobinonly
referral.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  referral.c - routines for handling LDAPv3 referrals and references.
00039  */
00040 
00041 #include "ldap-int.h"
00042 
00043 
00044 LDAPMessage *
00045 LDAP_CALL
00046 ldap_first_reference( LDAP *ld, LDAPMessage *res )
00047 {
00048        if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || res == NULLMSG ) {
00049               return( NULLMSG );
00050        }
00051 
00052        if ( res->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
00053               return( res );
00054        }
00055 
00056        return( ldap_next_reference( ld, res ));
00057 }
00058 
00059 
00060 LDAPMessage *
00061 LDAP_CALL
00062 ldap_next_reference( LDAP *ld, LDAPMessage *ref )
00063 {
00064        if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || ref == NULLMSG ) {
00065               return( NULLMSG );          /* punt */
00066        }
00067 
00068        for ( ref = ref->lm_chain; ref != NULLMSG; ref = ref->lm_chain ) {
00069               if ( ref->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
00070                      return( ref );
00071               }
00072        }
00073 
00074        return( NULLMSG );
00075 }
00076 
00077 
00078 int
00079 LDAP_CALL
00080 ldap_count_references( LDAP *ld, LDAPMessage *res )
00081 {
00082        int    i;
00083 
00084        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00085               return( -1 );
00086        }
00087 
00088        for ( i = 0; res != NULL; res = res->lm_chain ) {
00089               if ( res->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
00090                      ++i;
00091               }
00092        }
00093 
00094        return( i );
00095 }
00096 
00097 
00098 /*
00099  * returns an LDAP error code.
00100  */
00101 int
00102 LDAP_CALL
00103 ldap_parse_reference( LDAP *ld, LDAPMessage *ref, char ***referralsp,
00104     LDAPControl ***serverctrlsp, int freeit )
00105 {
00106        int           err;
00107 
00108        if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) ||
00109            !NSLDAPI_VALID_LDAPMESSAGE_REFERENCE_POINTER( ref )) {
00110               return( LDAP_PARAM_ERROR );
00111        }
00112 
00113        err = nsldapi_parse_reference( ld, ref->lm_ber, referralsp,
00114               serverctrlsp );
00115 
00116        LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00117 
00118        if ( freeit ) {
00119               ldap_msgfree( ref );
00120        }
00121 
00122        return( err );
00123 }
00124 
00125 
00126 /*
00127  * returns an LDAP error code indicating success or failure of parsing
00128  * does NOT set any error information inside "ld"
00129  */
00130 int
00131 nsldapi_parse_reference( LDAP *ld, BerElement *rber, char ***referralsp,
00132     LDAPControl ***serverctrlsp )
00133 {
00134        int           err;
00135        BerElement    ber;
00136        char          **refs;
00137 
00138        /*
00139         * Parse a searchResultReference message.  These are used in LDAPv3
00140         * and beyond and look like this:
00141         *
00142         *     SearchResultReference ::= [APPLICATION 19] SEQUENCE OF LDAPURL
00143         *
00144         * all wrapped up in an LDAPMessage sequence which looks like this:
00145         *
00146         *     LDAPMessage ::= SEQUENCE {
00147         *            messageID     MessageID,
00148         *            SearchResultReference
00149         *            controls      [0] Controls OPTIONAL
00150         *     }
00151         *
00152         * ldap_result() pulls out the message id, so by the time a result
00153         * message gets here we are conveniently sitting at the start of the
00154         * SearchResultReference itself.
00155         */
00156        err = LDAP_SUCCESS;  /* optimistic */
00157        ber = *rber;         /* struct copy */
00158 
00159        if ( ber_scanf( &ber, "{v", &refs ) == LBER_ERROR ) {
00160            err = LDAP_DECODING_ERROR;
00161        } else if ( serverctrlsp != NULL ) {
00162            /* pull out controls (if requested and any are present) */
00163            if ( ber_scanf( &ber, "}" ) == LBER_ERROR ) {
00164               err = LDAP_DECODING_ERROR;
00165            } else {
00166               err = nsldapi_get_controls( &ber, serverctrlsp );
00167            }
00168        }
00169 
00170        if ( referralsp == NULL ) {
00171            ldap_value_free( refs );
00172        } else {
00173            *referralsp = refs;
00174        }
00175 
00176        return( err );
00177 }