1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
3*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
4*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
5*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
6*7c478bd9Sstevel@tonic-gate  *
7*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
8*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
10*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
13*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
14*7c478bd9Sstevel@tonic-gate  *
15*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
16*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
17*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
18*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * Contributor(s):
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1993 Regents of the University of Michigan.
24*7c478bd9Sstevel@tonic-gate  *  All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  *  sbind.c
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #if 0
31*7c478bd9Sstevel@tonic-gate #ifndef lint
32*7c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
33*7c478bd9Sstevel@tonic-gate #endif
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate static int simple_bind_nolock( LDAP *ld, const char *dn, const char *passwd,
39*7c478bd9Sstevel@tonic-gate 	int unlock_permitted );
40*7c478bd9Sstevel@tonic-gate static int simple_bindifnot_s( LDAP *ld, const char *dn, const char *passwd );
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * ldap_simple_bind - bind to the ldap server.  The dn and
44*7c478bd9Sstevel@tonic-gate  * password of the entry to which to bind are supplied.  The message id
45*7c478bd9Sstevel@tonic-gate  * of the request initiated is returned.
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * Example:
48*7c478bd9Sstevel@tonic-gate  *	ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
49*7c478bd9Sstevel@tonic-gate  *	    "secret" )
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate int
53*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_simple_bind(LDAP * ld,const char * dn,const char * passwd)54*7c478bd9Sstevel@tonic-gate ldap_simple_bind( LDAP *ld, const char *dn, const char *passwd )
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	int	rc;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
61*7c478bd9Sstevel@tonic-gate 		return( -1 );
62*7c478bd9Sstevel@tonic-gate 	}
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	rc = simple_bind_nolock( ld, dn, passwd, 1 );
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	return( rc );
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate static int
simple_bind_nolock(LDAP * ld,const char * dn,const char * passwd,int unlock_permitted)71*7c478bd9Sstevel@tonic-gate simple_bind_nolock( LDAP *ld, const char *dn, const char *passwd,
72*7c478bd9Sstevel@tonic-gate     int unlock_permitted )
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	BerElement	*ber;
75*7c478bd9Sstevel@tonic-gate 	int		rc, msgid;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	/*
78*7c478bd9Sstevel@tonic-gate 	 * The bind request looks like this:
79*7c478bd9Sstevel@tonic-gate 	 *	BindRequest ::= SEQUENCE {
80*7c478bd9Sstevel@tonic-gate 	 *		version		INTEGER,
81*7c478bd9Sstevel@tonic-gate 	 *		name		DistinguishedName,	 -- who
82*7c478bd9Sstevel@tonic-gate 	 *		authentication	CHOICE {
83*7c478bd9Sstevel@tonic-gate 	 *			simple		[0] OCTET STRING -- passwd
84*7c478bd9Sstevel@tonic-gate 	 *		}
85*7c478bd9Sstevel@tonic-gate 	 *	}
86*7c478bd9Sstevel@tonic-gate 	 * all wrapped up in an LDAPMessage sequence.
87*7c478bd9Sstevel@tonic-gate 	 */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
90*7c478bd9Sstevel@tonic-gate 	msgid = ++ld->ld_msgid;
91*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	if ( dn == NULL )
94*7c478bd9Sstevel@tonic-gate 		dn = "";
95*7c478bd9Sstevel@tonic-gate 	if ( passwd == NULL )
96*7c478bd9Sstevel@tonic-gate 		passwd = "";
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_cache_on && ld->ld_cache_bind != NULL ) {
99*7c478bd9Sstevel@tonic-gate 		struct berval	bv;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		bv.bv_val = (char *)passwd;
102*7c478bd9Sstevel@tonic-gate 		bv.bv_len = strlen( passwd );
103*7c478bd9Sstevel@tonic-gate 		/* if ( unlock_permitted ) LDAP_MUTEX_UNLOCK( ld ); */
104*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
105*7c478bd9Sstevel@tonic-gate 		rc = (ld->ld_cache_bind)( ld, msgid, LDAP_REQ_BIND, dn, &bv,
106*7c478bd9Sstevel@tonic-gate 		    LDAP_AUTH_SIMPLE );
107*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
108*7c478bd9Sstevel@tonic-gate 		/* if ( unlock_permitted ) LDAP_MUTEX_LOCK( ld ); */
109*7c478bd9Sstevel@tonic-gate 		if ( rc != 0 ) {
110*7c478bd9Sstevel@tonic-gate 			return( rc );
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	/* create a message to send */
115*7c478bd9Sstevel@tonic-gate 	if (( rc = nsldapi_alloc_ber_with_options( ld, &ber ))
116*7c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
117*7c478bd9Sstevel@tonic-gate 		return( -1 );
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/* fill it in */
121*7c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "{it{ists}", msgid, LDAP_REQ_BIND,
122*7c478bd9Sstevel@tonic-gate 	    NSLDAPI_LDAP_VERSION( ld ), dn, LDAP_AUTH_SIMPLE, passwd ) == -1 ) {
123*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
124*7c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
125*7c478bd9Sstevel@tonic-gate 		return( -1 );
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if ( nsldapi_put_controls( ld, NULL, 1, ber ) != LDAP_SUCCESS ) {
129*7c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
130*7c478bd9Sstevel@tonic-gate 		return( -1 );
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	/* send the message */
134*7c478bd9Sstevel@tonic-gate 	return( nsldapi_send_initial_request( ld, msgid, LDAP_REQ_BIND,
135*7c478bd9Sstevel@tonic-gate 		(char *)dn, ber ));
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate  * ldap_simple_bind - bind to the ldap server using simple
141*7c478bd9Sstevel@tonic-gate  * authentication.  The dn and password of the entry to which to bind are
142*7c478bd9Sstevel@tonic-gate  * supplied.  LDAP_SUCCESS is returned upon success, the ldap error code
143*7c478bd9Sstevel@tonic-gate  * otherwise.
144*7c478bd9Sstevel@tonic-gate  *
145*7c478bd9Sstevel@tonic-gate  * Example:
146*7c478bd9Sstevel@tonic-gate  *	ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
147*7c478bd9Sstevel@tonic-gate  *	    "secret" )
148*7c478bd9Sstevel@tonic-gate  */
149*7c478bd9Sstevel@tonic-gate int
150*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_simple_bind_s(LDAP * ld,const char * dn,const char * passwd)151*7c478bd9Sstevel@tonic-gate ldap_simple_bind_s( LDAP *ld, const char *dn, const char *passwd )
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	int		msgid;
154*7c478bd9Sstevel@tonic-gate 	LDAPMessage	*result;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if ( NSLDAPI_VALID_LDAP_POINTER( ld ) &&
159*7c478bd9Sstevel@tonic-gate 	    ( ld->ld_options & LDAP_BITOPT_RECONNECT ) != 0 ) {
160*7c478bd9Sstevel@tonic-gate 		return( simple_bindifnot_s( ld, dn, passwd ));
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if ( (msgid = ldap_simple_bind( ld, dn, passwd )) == -1 )
164*7c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &result ) == -1 )
167*7c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	return( ldap_result2error( ld, result, 1 ) );
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * simple_bindifnot_s() is like ldap_simple_bind_s() except that it only does
175*7c478bd9Sstevel@tonic-gate  * a bind if the default connection is not currently bound.
176*7c478bd9Sstevel@tonic-gate  * If a successful bind using the same DN has already taken place we just
177*7c478bd9Sstevel@tonic-gate  * return LDAP_SUCCESS without conversing with the server at all.
178*7c478bd9Sstevel@tonic-gate  */
179*7c478bd9Sstevel@tonic-gate static int
simple_bindifnot_s(LDAP * ld,const char * dn,const char * passwd)180*7c478bd9Sstevel@tonic-gate simple_bindifnot_s( LDAP *ld, const char *dn, const char *passwd )
181*7c478bd9Sstevel@tonic-gate {
182*7c478bd9Sstevel@tonic-gate 	int		msgid, rc;
183*7c478bd9Sstevel@tonic-gate 	LDAPMessage	*result;
184*7c478bd9Sstevel@tonic-gate 	char		*binddn;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "simple_bindifnot_s\n", 0, 0, 0 );
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
189*7c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
193*7c478bd9Sstevel@tonic-gate 		dn = "";	/* to make comparisons simpler */
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	/*
197*7c478bd9Sstevel@tonic-gate 	 * if we are already bound using the same DN, just return LDAP_SUCCESS.
198*7c478bd9Sstevel@tonic-gate 	 */
199*7c478bd9Sstevel@tonic-gate 	if ( NULL != ( binddn = nsldapi_get_binddn( ld ))
200*7c478bd9Sstevel@tonic-gate 	    && 0 == strcmp( dn, binddn )) {
201*7c478bd9Sstevel@tonic-gate 		rc = LDAP_SUCCESS;
202*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
203*7c478bd9Sstevel@tonic-gate 		return rc;
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	/*
207*7c478bd9Sstevel@tonic-gate 	 * if the default connection has been lost and is now marked dead,
208*7c478bd9Sstevel@tonic-gate 	 * dispose of the default connection so it will get re-established.
209*7c478bd9Sstevel@tonic-gate 	 *
210*7c478bd9Sstevel@tonic-gate 	 * if not, clear the bind DN and status to ensure that we don't
211*7c478bd9Sstevel@tonic-gate 	 * report the wrong bind DN to a different thread while waiting
212*7c478bd9Sstevel@tonic-gate 	 * for our bind result to return from the server.
213*7c478bd9Sstevel@tonic-gate 	 */
214*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
215*7c478bd9Sstevel@tonic-gate 	if ( NULL != ld->ld_defconn ) {
216*7c478bd9Sstevel@tonic-gate 	    if ( LDAP_CONNST_DEAD == ld->ld_defconn->lconn_status ) {
217*7c478bd9Sstevel@tonic-gate 		nsldapi_free_connection( ld, ld->ld_defconn, NULL, NULL, 1, 0 );
218*7c478bd9Sstevel@tonic-gate 		ld->ld_defconn = NULL;
219*7c478bd9Sstevel@tonic-gate 	    } else if ( ld->ld_defconn->lconn_binddn != NULL ) {
220*7c478bd9Sstevel@tonic-gate 		NSLDAPI_FREE( ld->ld_defconn->lconn_binddn );
221*7c478bd9Sstevel@tonic-gate 		ld->ld_defconn->lconn_binddn = NULL;
222*7c478bd9Sstevel@tonic-gate 		ld->ld_defconn->lconn_bound = 0;
223*7c478bd9Sstevel@tonic-gate 	    }
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	/*
228*7c478bd9Sstevel@tonic-gate 	 * finally, bind (this will open a new connection if necessary)
229*7c478bd9Sstevel@tonic-gate 	 *
230*7c478bd9Sstevel@tonic-gate 	 * do everything under the protection of the result lock to
231*7c478bd9Sstevel@tonic-gate 	 * ensure that only one thread will be in this code at a time.
232*7c478bd9Sstevel@tonic-gate 	 * XXXmcs: we should use a condition variable instead?
233*7c478bd9Sstevel@tonic-gate 	 */
234*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_RESULT_LOCK );
235*7c478bd9Sstevel@tonic-gate 	if ( (msgid = simple_bind_nolock( ld, dn, passwd, 0 )) == -1 ) {
236*7c478bd9Sstevel@tonic-gate 		rc = LDAP_GET_LDERRNO( ld, NULL, NULL );
237*7c478bd9Sstevel@tonic-gate 		goto unlock_and_return;
238*7c478bd9Sstevel@tonic-gate 	}
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	/*
241*7c478bd9Sstevel@tonic-gate 	 * Note that at this point the bind request is on its way to the
242*7c478bd9Sstevel@tonic-gate 	 * server and at any time now we will either be bound as the new
243*7c478bd9Sstevel@tonic-gate 	 * DN (if the bind succeeded) or we will be bound as anonymous (if
244*7c478bd9Sstevel@tonic-gate 	 * the bind failed).
245*7c478bd9Sstevel@tonic-gate 	 */
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	/*
248*7c478bd9Sstevel@tonic-gate 	 * Wait for the bind result.  Code inside result.c:read1msg()
249*7c478bd9Sstevel@tonic-gate 	 * takes care of setting the connection's bind DN and status.
250*7c478bd9Sstevel@tonic-gate 	 */
251*7c478bd9Sstevel@tonic-gate 	if ( nsldapi_result_nolock( ld, msgid, 1, 0, (struct timeval *) 0,
252*7c478bd9Sstevel@tonic-gate 	    &result ) == -1 ) {
253*7c478bd9Sstevel@tonic-gate 		rc = LDAP_GET_LDERRNO( ld, NULL, NULL );
254*7c478bd9Sstevel@tonic-gate 		goto unlock_and_return;
255*7c478bd9Sstevel@tonic-gate 	}
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	rc = ldap_result2error( ld, result, 1 );
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate unlock_and_return:
260*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_RESULT_LOCK );
261*7c478bd9Sstevel@tonic-gate 	return( rc );
262*7c478bd9Sstevel@tonic-gate }
263