1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Portions Copyright %G% Sun Microsystems, Inc.
4*0Sstevel@tonic-gate  * All Rights Reserved
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
11*0Sstevel@tonic-gate  *  All rights reserved.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  *  bind.c
14*0Sstevel@tonic-gate  */
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #ifndef lint
17*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
18*0Sstevel@tonic-gate #endif
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate #include <stdio.h>
21*0Sstevel@tonic-gate #include <string.h>
22*0Sstevel@tonic-gate #ifdef MACOS
23*0Sstevel@tonic-gate #include <stdlib.h>
24*0Sstevel@tonic-gate #include "macos.h"
25*0Sstevel@tonic-gate #else /* MACOS */
26*0Sstevel@tonic-gate #ifdef DOS
27*0Sstevel@tonic-gate #include "msdos.h"
28*0Sstevel@tonic-gate #ifdef NCSA
29*0Sstevel@tonic-gate #include "externs.h"
30*0Sstevel@tonic-gate #endif /* NCSA */
31*0Sstevel@tonic-gate #else /* DOS */
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/socket.h>
34*0Sstevel@tonic-gate #include <sys/time.h>
35*0Sstevel@tonic-gate #endif /* DOS */
36*0Sstevel@tonic-gate #endif /* MACOS */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "lber.h"
39*0Sstevel@tonic-gate #include "ldap.h"
40*0Sstevel@tonic-gate #include "ldap-private.h"
41*0Sstevel@tonic-gate #include "ldap-int.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * ldap_bind - bind to the ldap server (and X.500).  The dn and password
46*0Sstevel@tonic-gate  * of the entry to which to bind are supplied, along with the authentication
47*0Sstevel@tonic-gate  * method to use.  The msgid of the bind request is returned on success,
48*0Sstevel@tonic-gate  * -1 if there's trouble.  Note, the kerberos support assumes the user already
49*0Sstevel@tonic-gate  * has a valid tgt for now.  ldap_result() should be called to find out the
50*0Sstevel@tonic-gate  * outcome of the bind request.
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  * Example:
53*0Sstevel@tonic-gate  *	ldap_bind( ld, "cn=manager, o=university of michigan, c=us", "secret",
54*0Sstevel@tonic-gate  *	    LDAP_AUTH_SIMPLE )
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate int
58*0Sstevel@tonic-gate ldap_bind( LDAP *ld, char *dn, char *passwd, int authmethod )
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	/*
61*0Sstevel@tonic-gate 	 * The bind request looks like this:
62*0Sstevel@tonic-gate 	 *	BindRequest ::= SEQUENCE {
63*0Sstevel@tonic-gate 	 *		version		INTEGER,
64*0Sstevel@tonic-gate 	 *		name		DistinguishedName,	 -- who
65*0Sstevel@tonic-gate 	 *		authentication	CHOICE {
66*0Sstevel@tonic-gate 	 *			simple		[0] OCTET STRING -- passwd
67*0Sstevel@tonic-gate #ifdef KERBEROS
68*0Sstevel@tonic-gate 	 *			krbv42ldap	[1] OCTET STRING
69*0Sstevel@tonic-gate 	 *			krbv42dsa	[2] OCTET STRING
70*0Sstevel@tonic-gate #endif
71*0Sstevel@tonic-gate 	 *		}
72*0Sstevel@tonic-gate 	 *	}
73*0Sstevel@tonic-gate 	 * all wrapped up in an LDAPMessage sequence.
74*0Sstevel@tonic-gate 	 */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 88, "ldap_bind\n"), 0, 0, 0 );
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	switch ( authmethod ) {
79*0Sstevel@tonic-gate 	case LDAP_AUTH_SIMPLE:
80*0Sstevel@tonic-gate 		return( ldap_simple_bind( ld, dn, passwd ) );
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate #ifdef KERBEROS
83*0Sstevel@tonic-gate 	case LDAP_AUTH_KRBV41:
84*0Sstevel@tonic-gate 		return( ldap_kerberos_bind1( ld, dn ) );
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	case LDAP_AUTH_KRBV42:
87*0Sstevel@tonic-gate 		return( ldap_kerberos_bind2( ld, dn ) );
88*0Sstevel@tonic-gate #endif
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	default:
91*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_AUTH_UNKNOWN;
92*0Sstevel@tonic-gate 		return( -1 );
93*0Sstevel@tonic-gate 	}
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * ldap_bind_s - bind to the ldap server (and X.500).  The dn and password
98*0Sstevel@tonic-gate  * of the entry to which to bind are supplied, along with the authentication
99*0Sstevel@tonic-gate  * method to use.  This routine just calls whichever bind routine is
100*0Sstevel@tonic-gate  * appropriate and returns the result of the bind (e.g. LDAP_SUCCESS or
101*0Sstevel@tonic-gate  * some other error indication).  Note, the kerberos support assumes the
102*0Sstevel@tonic-gate  * user already has a valid tgt for now.
103*0Sstevel@tonic-gate  *
104*0Sstevel@tonic-gate  * Examples:
105*0Sstevel@tonic-gate  *	ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
106*0Sstevel@tonic-gate  *	    "secret", LDAP_AUTH_SIMPLE )
107*0Sstevel@tonic-gate  *	ldap_bind_s( ld, "cn=manager, o=university of michigan, c=us",
108*0Sstevel@tonic-gate  *	    NULL, LDAP_AUTH_KRBV4 )
109*0Sstevel@tonic-gate  */
110*0Sstevel@tonic-gate int
111*0Sstevel@tonic-gate ldap_bind_s( LDAP *ld, char *dn, char *passwd, int authmethod )
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 89, "ldap_bind_s\n"), 0, 0, 0 );
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	switch ( authmethod ) {
116*0Sstevel@tonic-gate 	case LDAP_AUTH_SIMPLE:
117*0Sstevel@tonic-gate 		return( ldap_simple_bind_s( ld, dn, passwd ) );
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate #ifdef KERBEROS
120*0Sstevel@tonic-gate 	case LDAP_AUTH_KRBV4:
121*0Sstevel@tonic-gate 		return( ldap_kerberos_bind_s( ld, dn ) );
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	case LDAP_AUTH_KRBV41:
124*0Sstevel@tonic-gate 		return( ldap_kerberos_bind1_s( ld, dn ) );
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	case LDAP_AUTH_KRBV42:
127*0Sstevel@tonic-gate 		return( ldap_kerberos_bind2_s( ld, dn ) );
128*0Sstevel@tonic-gate #endif
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	default:
131*0Sstevel@tonic-gate 		return( ld->ld_errno = LDAP_AUTH_UNKNOWN );
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate void
137*0Sstevel@tonic-gate ldap_set_rebind_proc( LDAP *ld, LDAP_REBIND_FUNCTION *rebindproc, void *extra_arg )
138*0Sstevel@tonic-gate {
139*0Sstevel@tonic-gate #ifdef _REENTRANT
140*0Sstevel@tonic-gate         LOCK_LDAP(ld);
141*0Sstevel@tonic-gate #endif
142*0Sstevel@tonic-gate 	ld->ld_rebindproc = rebindproc;
143*0Sstevel@tonic-gate 	ld->ld_rebind_extra_arg = extra_arg;
144*0Sstevel@tonic-gate #ifdef _REENTRANT
145*0Sstevel@tonic-gate         UNLOCK_LDAP(ld);
146*0Sstevel@tonic-gate #endif
147*0Sstevel@tonic-gate }
148