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