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 );
72 if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "ldapserver",
73 &credlen )) == NULL ) {
74 return( -1 ); /* ld_errno should already be set */
77 /* create a message to send */
78 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
84 rc = ber_printf( ber, "{it{isto}}", ++ld->ld_msgid, LDAP_REQ_BIND,
85 ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen );
90 ld->ld_errno = LDAP_ENCODING_ERROR;
97 if ( ld->ld_cache != NULL ) {
98 ldap_flush_cache( ld );
100 #endif /* !LDAP_NOCACHE */
102 /* send the message */
103 return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
107 ldap_kerberos_bind1_s( LDAP *ld, LDAP_CONST char *dn )
112 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
114 /* initiate the bind */
115 if ( (msgid = ldap_kerberos_bind1( ld, dn )) == -1 )
116 return( ld->ld_errno );
118 /* wait for a result */
119 if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
121 return( ld->ld_errno ); /* ldap_result sets ld_errno */
124 return( ldap_result2error( ld, res, 1 ) );
128 * ldap_kerberos_bind2 - initiate a bind to the X.500 server using
129 * kerberos authentication. The dn is supplied. It is assumed the user
130 * already has a valid ticket granting ticket. The msgid of the
131 * request is returned on success (suitable for passing to ldap_result()),
132 * -1 is returned if there's trouble.
135 * ldap_kerberos_bind2( ld, "cn=manager, o=university of michigan, c=us" )
138 ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
145 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2\n", 0, 0, 0 );
150 if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen ))
152 return( -1 ); /* ld_errno should already be set */
155 /* create a message to send */
156 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
162 rc = ber_printf( ber, "{it{isto}}", ++ld->ld_msgid, LDAP_REQ_BIND,
163 ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen );
170 ld->ld_errno = LDAP_ENCODING_ERROR;
174 /* send the message */
175 return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
178 /* synchronous bind to DSA using kerberos */
180 ldap_kerberos_bind2_s( LDAP *ld, LDAP_CONST char *dn )
185 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2_s\n", 0, 0, 0 );
187 /* initiate the bind */
188 if ( (msgid = ldap_kerberos_bind2( ld, dn )) == -1 )
189 return( ld->ld_errno );
191 /* wait for a result */
192 if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
194 return( ld->ld_errno ); /* ldap_result sets ld_errno */
197 return( ldap_result2error( ld, res, 1 ) );
200 /* synchronous bind to ldap and DSA using kerberos */
202 ldap_kerberos_bind_s( LDAP *ld, LDAP_CONST char *dn )
206 Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind_s\n", 0, 0, 0 );
208 if ( (err = ldap_kerberos_bind1_s( ld, dn )) != LDAP_SUCCESS )
211 return( ldap_kerberos_bind2_s( ld, dn ) );
217 * ldap_get_kerberosv4_credentials - obtain kerberos v4 credentials for ldap.
218 * The dn of the entry to which to bind is supplied. It's assumed the
219 * user already has a tgt.
223 ldap_get_kerberosv4_credentials(
225 LDAP_CONST char *who,
226 LDAP_CONST char *service,
231 char realm[REALM_SZ], *cred, *krbinstance;
233 Debug( LDAP_DEBUG_TRACE, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
235 if ( (err = krb_get_tf_realm( tkt_string(), realm )) != KSUCCESS ) {
237 fprintf( stderr, "krb_get_tf_realm failed (%s)\n",
239 #endif /* LDAP_LIBUI */
240 ld->ld_errno = LDAP_AUTH_UNKNOWN;
244 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
245 /* not connected yet */
246 int rc = ldap_open_defconn( ld );
248 if( rc < 0 ) return NULL;
251 krbinstance = ld->ld_defconn->lconn_krbinstance;
253 if ( (err = krb_mk_req( &ktxt, service, krbinstance, realm, 0 ))
256 fprintf( stderr, "krb_mk_req failed (%s)\n", krb_err_txt[err] );
257 #endif /* LDAP_LIBUI */
258 ld->ld_errno = LDAP_AUTH_UNKNOWN;
262 if ( ( cred = LDAP_MALLOC( ktxt.length )) == NULL ) {
263 ld->ld_errno = LDAP_NO_MEMORY;
268 memcpy( cred, ktxt.dat, ktxt.length );
273 #endif /* !AUTHMAN */
274 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */