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 * bind.c
24*7c478bd9Sstevel@tonic-gate */
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate #if 0
27*7c478bd9Sstevel@tonic-gate #ifndef lint
28*7c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
29*7c478bd9Sstevel@tonic-gate #endif
30*7c478bd9Sstevel@tonic-gate #endif
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate * ldap_bind - bind to the ldap server. The dn and password
36*7c478bd9Sstevel@tonic-gate * of the entry to which to bind are supplied, along with the authentication
37*7c478bd9Sstevel@tonic-gate * method to use. The msgid of the bind request is returned on success,
38*7c478bd9Sstevel@tonic-gate * -1 if there's trouble. Note, the kerberos support assumes the user already
39*7c478bd9Sstevel@tonic-gate * has a valid tgt for now. ldap_result() should be called to find out the
40*7c478bd9Sstevel@tonic-gate * outcome of the bind request.
41*7c478bd9Sstevel@tonic-gate *
42*7c478bd9Sstevel@tonic-gate * Example:
43*7c478bd9Sstevel@tonic-gate * ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
44*7c478bd9Sstevel@tonic-gate * LDAP_AUTH_SIMPLE )
45*7c478bd9Sstevel@tonic-gate */
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate int
48*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_bind(LDAP * ld,const char * dn,const char * passwd,int authmethod)49*7c478bd9Sstevel@tonic-gate ldap_bind( LDAP *ld, const char *dn, const char *passwd, int authmethod )
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate * The bind request looks like this:
53*7c478bd9Sstevel@tonic-gate * BindRequest ::= SEQUENCE {
54*7c478bd9Sstevel@tonic-gate * version INTEGER,
55*7c478bd9Sstevel@tonic-gate * name DistinguishedName, -- who
56*7c478bd9Sstevel@tonic-gate * authentication CHOICE {
57*7c478bd9Sstevel@tonic-gate * simple [0] OCTET STRING -- passwd
58*7c478bd9Sstevel@tonic-gate * }
59*7c478bd9Sstevel@tonic-gate * }
60*7c478bd9Sstevel@tonic-gate * all wrapped up in an LDAPMessage sequence.
61*7c478bd9Sstevel@tonic-gate */
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_bind\n", 0, 0, 0 );
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
66*7c478bd9Sstevel@tonic-gate return( -1 );
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate switch ( authmethod ) {
70*7c478bd9Sstevel@tonic-gate case LDAP_AUTH_SIMPLE:
71*7c478bd9Sstevel@tonic-gate return( ldap_simple_bind( ld, dn, passwd ) );
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate default:
74*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_AUTH_UNKNOWN, NULL, NULL );
75*7c478bd9Sstevel@tonic-gate return( -1 );
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate * ldap_bind_s - bind to the ldap server. The dn and password
81*7c478bd9Sstevel@tonic-gate * of the entry to which to bind are supplied, along with the authentication
82*7c478bd9Sstevel@tonic-gate * method to use. This routine just calls whichever bind routine is
83*7c478bd9Sstevel@tonic-gate * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
84*7c478bd9Sstevel@tonic-gate * some other error indication). Note, the kerberos support assumes the
85*7c478bd9Sstevel@tonic-gate * user already has a valid tgt for now.
86*7c478bd9Sstevel@tonic-gate *
87*7c478bd9Sstevel@tonic-gate * Examples:
88*7c478bd9Sstevel@tonic-gate * ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
89*7c478bd9Sstevel@tonic-gate * "secret", LDAP_AUTH_SIMPLE )
90*7c478bd9Sstevel@tonic-gate * ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
91*7c478bd9Sstevel@tonic-gate * NULL, LDAP_AUTH_KRBV4 )
92*7c478bd9Sstevel@tonic-gate */
93*7c478bd9Sstevel@tonic-gate int
94*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_bind_s(LDAP * ld,const char * dn,const char * passwd,int authmethod)95*7c478bd9Sstevel@tonic-gate ldap_bind_s( LDAP *ld, const char *dn, const char *passwd, int authmethod )
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate int err;
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_bind_s\n", 0, 0, 0 );
100*7c478bd9Sstevel@tonic-gate
101*7c478bd9Sstevel@tonic-gate switch ( authmethod ) {
102*7c478bd9Sstevel@tonic-gate case LDAP_AUTH_SIMPLE:
103*7c478bd9Sstevel@tonic-gate return( ldap_simple_bind_s( ld, dn, passwd ) );
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate default:
106*7c478bd9Sstevel@tonic-gate err = LDAP_AUTH_UNKNOWN;
107*7c478bd9Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL );
108*7c478bd9Sstevel@tonic-gate return( err );
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate void
114*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_set_rebind_proc(LDAP * ld,LDAP_REBINDPROC_CALLBACK * rebindproc,void * arg)115*7c478bd9Sstevel@tonic-gate ldap_set_rebind_proc( LDAP *ld, LDAP_REBINDPROC_CALLBACK *rebindproc,
116*7c478bd9Sstevel@tonic-gate void *arg )
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate if ( ld == NULL ) {
119*7c478bd9Sstevel@tonic-gate if ( !nsldapi_initialized ) {
120*7c478bd9Sstevel@tonic-gate nsldapi_initialize_defaults();
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate ld = &nsldapi_ld_defaults;
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate if ( NSLDAPI_VALID_LDAP_POINTER( ld )) {
126*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK );
127*7c478bd9Sstevel@tonic-gate ld->ld_rebind_fn = rebindproc;
128*7c478bd9Sstevel@tonic-gate ld->ld_rebind_arg = arg;
129*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate * return a pointer to the bind DN for the default connection (a copy is
136*7c478bd9Sstevel@tonic-gate * not made). If there is no bind DN available, NULL is returned.
137*7c478bd9Sstevel@tonic-gate */
138*7c478bd9Sstevel@tonic-gate char *
nsldapi_get_binddn(LDAP * ld)139*7c478bd9Sstevel@tonic-gate nsldapi_get_binddn( LDAP *ld )
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate char *binddn;
142*7c478bd9Sstevel@tonic-gate
143*7c478bd9Sstevel@tonic-gate binddn = NULL; /* default -- assume they are not bound */
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
146*7c478bd9Sstevel@tonic-gate if ( NULL != ld->ld_defconn && LDAP_CONNST_CONNECTED ==
147*7c478bd9Sstevel@tonic-gate ld->ld_defconn->lconn_status && ld->ld_defconn->lconn_bound ) {
148*7c478bd9Sstevel@tonic-gate if (( binddn = ld->ld_defconn->lconn_binddn ) == NULL ) {
149*7c478bd9Sstevel@tonic-gate binddn = "";
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate return( binddn );
155*7c478bd9Sstevel@tonic-gate }
156