2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1993 Regents of the University of Michigan.
13 * BindRequest ::= SEQUENCE {
15 * name DistinguishedName, -- who
16 * authentication CHOICE {
17 * simple [0] OCTET STRING -- passwd
19 * krbv42ldap [1] OCTET STRING
20 * krbv42dsa [2] OCTET STRING
22 * sasl [3] SaslCredentials -- LDAPv3
26 * BindResponse ::= SEQUENCE {
27 * COMPONENTS OF LDAPResult,
28 * serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
38 #include <ac/stdlib.h>
41 #include <ac/socket.h>
42 #include <ac/string.h>
49 * ldap_kerberos_bind1 - initiate a bind to the ldap server using
50 * kerberos authentication. The dn is supplied. It is assumed the user
51 * already has a valid ticket granting ticket. The msgid of the
52 * request is returned on success (suitable for passing to ldap_result()),
53 * -1 is returned if there's trouble.
56 * ldap_kerberos_bind1( ld, "cn=manager, o=university of michigan, c=us" )
59 ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
65 #ifdef STR_TRANSLATION
66 int str_translation_on;
67 #endif /* STR_TRANSLATION */
69 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1\n", 0, 0, 0 );
74 if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "ldapserver",
75 &credlen )) == NULL ) {
76 return( -1 ); /* ld_errno should already be set */
79 /* create a message to send */
80 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
85 #ifdef STR_TRANSLATION
86 if (( str_translation_on = (( ber->ber_options &
87 LBER_TRANSLATE_STRINGS ) != 0 ))) { /* turn translation off */
88 ber->ber_options &= ~LBER_TRANSLATE_STRINGS;
90 #endif /* STR_TRANSLATION */
93 rc = ber_printf( ber, "{it{isto}}", ++ld->ld_msgid, LDAP_REQ_BIND,
94 ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen );
96 #ifdef STR_TRANSLATION
97 if ( str_translation_on ) { /* restore translation */
98 ber->ber_options |= LBER_TRANSLATE_STRINGS;
100 #endif /* STR_TRANSLATION */
105 ld->ld_errno = LDAP_ENCODING_ERROR;
112 if ( ld->ld_cache != NULL ) {
113 ldap_flush_cache( ld );
115 #endif /* !LDAP_NOCACHE */
117 /* send the message */
118 return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
122 ldap_kerberos_bind1_s( LDAP *ld, LDAP_CONST char *dn )
127 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
129 /* initiate the bind */
130 if ( (msgid = ldap_kerberos_bind1( ld, dn )) == -1 )
131 return( ld->ld_errno );
133 /* wait for a result */
134 if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
136 return( ld->ld_errno ); /* ldap_result sets ld_errno */
139 return( ldap_result2error( ld, res, 1 ) );
143 * ldap_kerberos_bind2 - initiate a bind to the X.500 server using
144 * kerberos authentication. The dn is supplied. It is assumed the user
145 * already has a valid ticket granting ticket. The msgid of the
146 * request is returned on success (suitable for passing to ldap_result()),
147 * -1 is returned if there's trouble.
150 * ldap_kerberos_bind2( ld, "cn=manager, o=university of michigan, c=us" )
153 ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
159 #ifdef STR_TRANSLATION
160 int str_translation_on;
161 #endif /* STR_TRANSLATION */
163 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2\n", 0, 0, 0 );
168 if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen ))
170 return( -1 ); /* ld_errno should already be set */
173 /* create a message to send */
174 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
179 #ifdef STR_TRANSLATION
180 if (( str_translation_on = (( ber->ber_options &
181 LBER_TRANSLATE_STRINGS ) != 0 ))) { /* turn translation off */
182 ber->ber_options &= ~LBER_TRANSLATE_STRINGS;
184 #endif /* STR_TRANSLATION */
187 rc = ber_printf( ber, "{it{isto}}", ++ld->ld_msgid, LDAP_REQ_BIND,
188 ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen );
191 #ifdef STR_TRANSLATION
192 if ( str_translation_on ) { /* restore translation */
193 ber->ber_options |= LBER_TRANSLATE_STRINGS;
195 #endif /* STR_TRANSLATION */
201 ld->ld_errno = LDAP_ENCODING_ERROR;
205 /* send the message */
206 return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
209 /* synchronous bind to DSA using kerberos */
211 ldap_kerberos_bind2_s( LDAP *ld, LDAP_CONST char *dn )
216 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2_s\n", 0, 0, 0 );
218 /* initiate the bind */
219 if ( (msgid = ldap_kerberos_bind2( ld, dn )) == -1 )
220 return( ld->ld_errno );
222 /* wait for a result */
223 if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
225 return( ld->ld_errno ); /* ldap_result sets ld_errno */
228 return( ldap_result2error( ld, res, 1 ) );
231 /* synchronous bind to ldap and DSA using kerberos */
233 ldap_kerberos_bind_s( LDAP *ld, LDAP_CONST char *dn )
237 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind_s\n", 0, 0, 0 );
239 if ( (err = ldap_kerberos_bind1_s( ld, dn )) != LDAP_SUCCESS )
242 return( ldap_kerberos_bind2_s( ld, dn ) );
248 * ldap_get_kerberosv4_credentials - obtain kerberos v4 credentials for ldap.
249 * The dn of the entry to which to bind is supplied. It's assumed the
250 * user already has a tgt.
254 ldap_get_kerberosv4_credentials(
256 LDAP_CONST char *who,
257 LDAP_CONST char *service,
262 char realm[REALM_SZ], *cred, *krbinstance;
264 Debug( LDAP_DEBUG_TRACE, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
266 if ( (err = krb_get_tf_realm( tkt_string(), realm )) != KSUCCESS ) {
268 fprintf( stderr, "krb_get_tf_realm failed (%s)\n",
270 #endif /* LDAP_LIBUI */
271 ld->ld_errno = LDAP_INVALID_CREDENTIALS;
275 krbinstance = ld->ld_defconn->lconn_krbinstance;
277 if ( (err = krb_mk_req( &ktxt, service, krbinstance, realm, 0 ))
280 fprintf( stderr, "krb_mk_req failed (%s)\n", krb_err_txt[err] );
281 #endif /* LDAP_LIBUI */
282 ld->ld_errno = LDAP_INVALID_CREDENTIALS;
286 if ( ( cred = LDAP_MALLOC( ktxt.length )) == NULL ) {
287 ld->ld_errno = LDAP_NO_MEMORY;
292 memcpy( cred, ktxt.dat, ktxt.length );
297 #endif /* !AUTHMAN */
298 #endif /* HAVE_KERBEROS */