1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Mozilla Communicator client code, released
15  * March 31, 1998.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998-1999
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either of the GNU General Public License Version 2 or later (the "GPL"),
26  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 /*
38  *  bind.c
39  */
40 
41 #if 0
42 #ifndef lint
43 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
44 #endif
45 #endif
46 
47 #include "ldap-int.h"
48 
49 /*
50  * ldap_bind - bind to the ldap server. The dn and password
51  * of the entry to which to bind are supplied, along with the authentication
52  * method to use.  The msgid of the bind request is returned on success,
53  * -1 if there's trouble.  Note, the kerberos support assumes the user already
54  * has a valid tgt for now.  ldap_result() should be called to find out the
55  * outcome of the bind request.
56  *
57  * Example:
58  *	ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
59  *	    LDAP_AUTH_SIMPLE )
60  */
61 
62 int
63 LDAP_CALL
ldap_bind(LDAP * ld,const char * dn,const char * passwd,int authmethod)64 ldap_bind( LDAP *ld, const char *dn, const char *passwd, int authmethod )
65 {
66 	/*
67 	 * The bind request looks like this:
68 	 *	BindRequest ::= SEQUENCE {
69 	 *		version		INTEGER,
70 	 *		name		DistinguishedName,	 -- who
71 	 *		authentication	CHOICE {
72 	 *			simple		[0] OCTET STRING -- passwd
73 	 *		}
74 	 *	}
75 	 * all wrapped up in an LDAPMessage sequence.
76 	 */
77 
78 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_bind\n", 0, 0, 0 );
79 
80 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
81 		return( -1 );
82 	}
83 
84 	switch ( authmethod ) {
85 	case LDAP_AUTH_SIMPLE:
86 		return( ldap_simple_bind( ld, dn, passwd ) );
87 
88 	default:
89 		LDAP_SET_LDERRNO( ld, LDAP_AUTH_UNKNOWN, NULL, NULL );
90 		return( -1 );
91 	}
92 }
93 
94 /*
95  * ldap_bind_s - bind to the ldap server.  The dn and password
96  * of the entry to which to bind are supplied, along with the authentication
97  * method to use.  This routine just calls whichever bind routine is
98  * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
99  * some other error indication).  Note, the kerberos support assumes the
100  * user already has a valid tgt for now.
101  *
102  * Examples:
103  *	ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
104  *	    "secret", LDAP_AUTH_SIMPLE )
105  *	ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
106  *	    NULL, LDAP_AUTH_KRBV4 )
107  */
108 int
109 LDAP_CALL
ldap_bind_s(LDAP * ld,const char * dn,const char * passwd,int authmethod)110 ldap_bind_s( LDAP *ld, const char *dn, const char *passwd, int authmethod )
111 {
112 	int	err;
113 
114 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_bind_s\n", 0, 0, 0 );
115 
116 	switch ( authmethod ) {
117 	case LDAP_AUTH_SIMPLE:
118 		return( ldap_simple_bind_s( ld, dn, passwd ) );
119 
120 	default:
121 		err = LDAP_AUTH_UNKNOWN;
122 		LDAP_SET_LDERRNO( ld, err, NULL, NULL );
123 		return( err );
124 	}
125 }
126 
127 
128 void
129 LDAP_CALL
ldap_set_rebind_proc(LDAP * ld,LDAP_REBINDPROC_CALLBACK * rebindproc,void * arg)130 ldap_set_rebind_proc( LDAP *ld, LDAP_REBINDPROC_CALLBACK *rebindproc,
131     void *arg )
132 {
133 	if ( ld == NULL ) {
134 		if ( !nsldapi_initialized ) {
135 			nsldapi_initialize_defaults();
136 		}
137 		ld = &nsldapi_ld_defaults;
138 	}
139 
140 	if ( NSLDAPI_VALID_LDAP_POINTER( ld )) {
141 		LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK );
142 		ld->ld_rebind_fn = rebindproc;
143 		ld->ld_rebind_arg = arg;
144 		LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );
145 	}
146 }
147 
148 
149 /*
150  * return a pointer to the bind DN for the default connection (a copy is
151  * not made).  If there is no bind DN available, NULL is returned.
152  */
153 char *
nsldapi_get_binddn(LDAP * ld)154 nsldapi_get_binddn( LDAP *ld )
155 {
156 	char	*binddn;
157 
158 	binddn = NULL;	/* default -- assume they are not bound */
159 
160 	LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
161 	if ( NULL != ld->ld_defconn && LDAP_CONNST_CONNECTED ==
162 	    ld->ld_defconn->lconn_status && ld->ld_defconn->lconn_bound ) {
163 		if (( binddn = ld->ld_defconn->lconn_binddn ) == NULL ) {
164 			binddn = "";
165 		}
166 	}
167 	LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
168 
169 	return( binddn );
170 }
171