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