3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1993 Regents of the University of Michigan.
14 * BindRequest ::= SEQUENCE {
16 * name DistinguishedName, -- who
17 * authentication CHOICE {
18 * simple [0] OCTET STRING -- passwd
19 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
20 * krbv42ldap [1] OCTET STRING
21 * krbv42dsa [2] OCTET STRING
23 * sasl [3] SaslCredentials -- LDAPv3
27 * BindResponse ::= SEQUENCE {
28 * COMPONENTS OF LDAPResult,
29 * serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
36 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
39 #include <ac/stdlib.h>
42 #include <ac/socket.h>
43 #include <ac/string.h>
50 * ldap_kerberos_bind1 - initiate a bind to the ldap server using
51 * kerberos authentication. The dn is supplied. It is assumed the user
52 * already has a valid ticket granting ticket. The msgid of the
53 * request is returned on success (suitable for passing to ldap_result()),
54 * -1 is returned if there's trouble.
57 * ldap_kerberos_bind1( ld, "cn=manager, o=university of michigan, c=us" )
60 ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
67 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1\n", 0, 0, 0 );
69 if( ld->ld_version > LDAP_VERSION2 ) {
70 ld->ld_errno = LDAP_NOT_SUPPORTED;
77 if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "ldapserver",
78 &credlen )) == NULL ) {
79 return( -1 ); /* ld_errno should already be set */
82 /* create a message to send */
83 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
89 rc = ber_printf( ber, "{it{istoN}N}", ++ld->ld_msgid, LDAP_REQ_BIND,
90 ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen );
95 ld->ld_errno = LDAP_ENCODING_ERROR;
102 if ( ld->ld_cache != NULL ) {
103 ldap_flush_cache( ld );
105 #endif /* !LDAP_NOCACHE */
107 /* send the message */
108 return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
112 ldap_kerberos_bind1_s( LDAP *ld, LDAP_CONST char *dn )
117 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
119 /* initiate the bind */
120 if ( (msgid = ldap_kerberos_bind1( ld, dn )) == -1 )
121 return( ld->ld_errno );
123 /* wait for a result */
124 if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
126 return( ld->ld_errno ); /* ldap_result sets ld_errno */
129 return( ldap_result2error( ld, res, 1 ) );
133 * ldap_kerberos_bind2 - initiate a bind to the X.500 server using
134 * kerberos authentication. The dn is supplied. It is assumed the user
135 * already has a valid ticket granting ticket. The msgid of the
136 * request is returned on success (suitable for passing to ldap_result()),
137 * -1 is returned if there's trouble.
140 * ldap_kerberos_bind2( ld, "cn=manager, o=university of michigan, c=us" )
143 ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
150 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2\n", 0, 0, 0 );
152 if( ld->ld_version > LDAP_VERSION2 ) {
153 ld->ld_errno = LDAP_NOT_SUPPORTED;
160 if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen ))
162 return( -1 ); /* ld_errno should already be set */
165 /* create a message to send */
166 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
172 rc = ber_printf( ber, "{it{istoN}N}", ++ld->ld_msgid, LDAP_REQ_BIND,
173 ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen );
180 ld->ld_errno = LDAP_ENCODING_ERROR;
184 /* send the message */
185 return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
188 /* synchronous bind to DSA using kerberos */
190 ldap_kerberos_bind2_s( LDAP *ld, LDAP_CONST char *dn )
195 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2_s\n", 0, 0, 0 );
197 /* initiate the bind */
198 if ( (msgid = ldap_kerberos_bind2( ld, dn )) == -1 )
199 return( ld->ld_errno );
201 /* wait for a result */
202 if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
204 return( ld->ld_errno ); /* ldap_result sets ld_errno */
207 return( ldap_result2error( ld, res, 1 ) );
210 /* synchronous bind to ldap and DSA using kerberos */
212 ldap_kerberos_bind_s( LDAP *ld, LDAP_CONST char *dn )
216 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind_s\n", 0, 0, 0 );
218 if ( (err = ldap_kerberos_bind1_s( ld, dn )) != LDAP_SUCCESS )
221 return( ldap_kerberos_bind2_s( ld, dn ) );
227 * ldap_get_kerberosv4_credentials - obtain kerberos v4 credentials for ldap.
228 * The dn of the entry to which to bind is supplied. It's assumed the
229 * user already has a tgt.
233 ldap_get_kerberosv4_credentials(
235 LDAP_CONST char *who,
236 LDAP_CONST char *service,
241 char realm[REALM_SZ], *cred, *krbinstance;
243 Debug( LDAP_DEBUG_TRACE, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
245 if ( (err = krb_get_tf_realm( tkt_string(), realm )) != KSUCCESS ) {
246 Debug( LDAP_DEBUG_ANY, "ldap_get_kerberosv4_credentials: "
247 "krb_get_tf_realm failed: %s\n", krb_err_txt[err], 0, 0 );
248 ld->ld_errno = LDAP_AUTH_UNKNOWN;
252 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
253 /* not connected yet */
254 int rc = ldap_open_defconn( ld );
256 if( rc < 0 ) return NULL;
259 krbinstance = ld->ld_defconn->lconn_krbinstance;
261 if ( (err = krb_mk_req( &ktxt, service, krbinstance, realm, 0 ))
264 Debug( LDAP_DEBUG_ANY, "ldap_get_kerberosv4_credentials: "
265 "krb_mk_req failed (%s)\n", krb_err_txt[err], 0, 0 );
266 ld->ld_errno = LDAP_AUTH_UNKNOWN;
270 if ( ( cred = LDAP_MALLOC( ktxt.length )) == NULL ) {
271 ld->ld_errno = LDAP_NO_MEMORY;
276 AC_MEMCPY( cred, ktxt.dat, ktxt.length );
281 #endif /* !AUTHMAN */
282 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */