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