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