xref: /titanic_44/usr/src/lib/libldap5/sources/ldap/common/referral.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
3*7c478bd9Sstevel@tonic-gate  * All rights reserved.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
10*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
11*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
12*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
15*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
17*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
20*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
23*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
24*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * Contributor(s):
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  *  referral.c - routines for handling LDAPv3 referrals and references.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate LDAPMessage *
37*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_first_reference(LDAP * ld,LDAPMessage * res)38*7c478bd9Sstevel@tonic-gate ldap_first_reference( LDAP *ld, LDAPMessage *res )
39*7c478bd9Sstevel@tonic-gate {
40*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || res == NULLMSG ) {
41*7c478bd9Sstevel@tonic-gate 		return( NULLMSG );
42*7c478bd9Sstevel@tonic-gate 	}
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	if ( res->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
45*7c478bd9Sstevel@tonic-gate 		return( res );
46*7c478bd9Sstevel@tonic-gate 	}
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	return( ldap_next_reference( ld, res ));
49*7c478bd9Sstevel@tonic-gate }
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate LDAPMessage *
53*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_next_reference(LDAP * ld,LDAPMessage * ref)54*7c478bd9Sstevel@tonic-gate ldap_next_reference( LDAP *ld, LDAPMessage *ref )
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || ref == NULLMSG ) {
57*7c478bd9Sstevel@tonic-gate 		return( NULLMSG );		/* punt */
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	for ( ref = ref->lm_chain; ref != NULLMSG; ref = ref->lm_chain ) {
61*7c478bd9Sstevel@tonic-gate 		if ( ref->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
62*7c478bd9Sstevel@tonic-gate 			return( ref );
63*7c478bd9Sstevel@tonic-gate 		}
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	return( NULLMSG );
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate int
71*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_count_references(LDAP * ld,LDAPMessage * res)72*7c478bd9Sstevel@tonic-gate ldap_count_references( LDAP *ld, LDAPMessage *res )
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	int	i;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
77*7c478bd9Sstevel@tonic-gate 		return( -1 );
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	for ( i = 0; res != NULL; res = res->lm_chain ) {
81*7c478bd9Sstevel@tonic-gate 		if ( res->lm_msgtype == LDAP_RES_SEARCH_REFERENCE ) {
82*7c478bd9Sstevel@tonic-gate 			++i;
83*7c478bd9Sstevel@tonic-gate 		}
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	return( i );
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate /*
91*7c478bd9Sstevel@tonic-gate  * returns an LDAP error code.
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate int
94*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_parse_reference(LDAP * ld,LDAPMessage * ref,char *** referralsp,LDAPControl *** serverctrlsp,int freeit)95*7c478bd9Sstevel@tonic-gate ldap_parse_reference( LDAP *ld, LDAPMessage *ref, char ***referralsp,
96*7c478bd9Sstevel@tonic-gate     LDAPControl ***serverctrlsp, int freeit )
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	int		err;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) ||
101*7c478bd9Sstevel@tonic-gate 	    !NSLDAPI_VALID_LDAPMESSAGE_REFERENCE_POINTER( ref )) {
102*7c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	err = nsldapi_parse_reference( ld, ref->lm_ber, referralsp,
106*7c478bd9Sstevel@tonic-gate 		serverctrlsp );
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	LDAP_SET_LDERRNO( ld, err, NULL, NULL );
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	if ( freeit ) {
111*7c478bd9Sstevel@tonic-gate 		ldap_msgfree( ref );
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	return( err );
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /*
119*7c478bd9Sstevel@tonic-gate  * returns an LDAP error code indicating success or failure of parsing
120*7c478bd9Sstevel@tonic-gate  * does NOT set any error information inside "ld"
121*7c478bd9Sstevel@tonic-gate  */
122*7c478bd9Sstevel@tonic-gate int
nsldapi_parse_reference(LDAP * ld,BerElement * rber,char *** referralsp,LDAPControl *** serverctrlsp)123*7c478bd9Sstevel@tonic-gate nsldapi_parse_reference( LDAP *ld, BerElement *rber, char ***referralsp,
124*7c478bd9Sstevel@tonic-gate     LDAPControl ***serverctrlsp )
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 	int		err;
127*7c478bd9Sstevel@tonic-gate 	BerElement	ber;
128*7c478bd9Sstevel@tonic-gate 	char		**refs;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	/*
131*7c478bd9Sstevel@tonic-gate 	 * Parse a searchResultReference message.  These are used in LDAPv3
132*7c478bd9Sstevel@tonic-gate 	 * and beyond and look like this:
133*7c478bd9Sstevel@tonic-gate 	 *
134*7c478bd9Sstevel@tonic-gate 	 *	SearchResultReference ::= [APPLICATION 19] SEQUENCE OF LDAPURL
135*7c478bd9Sstevel@tonic-gate 	 *
136*7c478bd9Sstevel@tonic-gate 	 * all wrapped up in an LDAPMessage sequence which looks like this:
137*7c478bd9Sstevel@tonic-gate 	 *
138*7c478bd9Sstevel@tonic-gate 	 *	LDAPMessage ::= SEQUENCE {
139*7c478bd9Sstevel@tonic-gate 	 *		messageID	MessageID,
140*7c478bd9Sstevel@tonic-gate 	 *		SearchResultReference
141*7c478bd9Sstevel@tonic-gate 	 *		controls	[0] Controls OPTIONAL
142*7c478bd9Sstevel@tonic-gate 	 *	}
143*7c478bd9Sstevel@tonic-gate 	 *
144*7c478bd9Sstevel@tonic-gate 	 * ldap_result() pulls out the message id, so by the time a result
145*7c478bd9Sstevel@tonic-gate 	 * message gets here we are conveniently sitting at the start of the
146*7c478bd9Sstevel@tonic-gate 	 * SearchResultReference itself.
147*7c478bd9Sstevel@tonic-gate 	 */
148*7c478bd9Sstevel@tonic-gate 	err = LDAP_SUCCESS;	/* optimistic */
149*7c478bd9Sstevel@tonic-gate 	ber = *rber;		/* struct copy */
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if ( ber_scanf( &ber, "{v", &refs ) == LBER_ERROR ) {
152*7c478bd9Sstevel@tonic-gate 	    err = LDAP_DECODING_ERROR;
153*7c478bd9Sstevel@tonic-gate 	} else if ( serverctrlsp != NULL ) {
154*7c478bd9Sstevel@tonic-gate 	    /* pull out controls (if requested and any are present) */
155*7c478bd9Sstevel@tonic-gate 	    if ( ber_scanf( &ber, "}" ) == LBER_ERROR ) {
156*7c478bd9Sstevel@tonic-gate 		err = LDAP_DECODING_ERROR;
157*7c478bd9Sstevel@tonic-gate 	    } else {
158*7c478bd9Sstevel@tonic-gate 		err = nsldapi_get_controls( &ber, serverctrlsp );
159*7c478bd9Sstevel@tonic-gate 	    }
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	if ( referralsp == NULL ) {
163*7c478bd9Sstevel@tonic-gate 	    ldap_value_free( refs );
164*7c478bd9Sstevel@tonic-gate 	} else {
165*7c478bd9Sstevel@tonic-gate 	    *referralsp = refs;
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	return( err );
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
172*7c478bd9Sstevel@tonic-gate 
ldap_get_reference_urls(LDAP * ld,LDAPMessage * res)173*7c478bd9Sstevel@tonic-gate char ** ldap_get_reference_urls(LDAP *ld, LDAPMessage *res)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate         BerElement tmp;
176*7c478bd9Sstevel@tonic-gate         char **urls = NULL;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate         LDAPDebug( LDAP_DEBUG_TRACE, "ldap_get_reference_urls\n", 0, 0, 0 );
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate         if (res == NULL){
181*7c478bd9Sstevel@tonic-gate                 ld->ld_errno = LDAP_PARAM_ERROR;
182*7c478bd9Sstevel@tonic-gate                 return (NULL);
183*7c478bd9Sstevel@tonic-gate         }
184*7c478bd9Sstevel@tonic-gate         tmp = *res->lm_ber; /* struct copy */
185*7c478bd9Sstevel@tonic-gate         if ( ber_scanf( &tmp, "{v}", &urls) == LBER_ERROR){
186*7c478bd9Sstevel@tonic-gate                 ld->ld_errno = LDAP_DECODING_ERROR;
187*7c478bd9Sstevel@tonic-gate                 return (NULL);
188*7c478bd9Sstevel@tonic-gate         }
189*7c478bd9Sstevel@tonic-gate         return (urls);
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate #endif /* _SOLARIS_SDK */
193*7c478bd9Sstevel@tonic-gate 
194