1998-08-09 08:43:13 +08:00
|
|
|
/*
|
1998-12-29 04:53:15 +08:00
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* Copyright (c) 1993 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* kbind.c
|
|
|
|
*/
|
|
|
|
|
1999-06-04 03:22:33 +08:00
|
|
|
/*
|
|
|
|
* BindRequest ::= SEQUENCE {
|
|
|
|
* version INTEGER,
|
|
|
|
* name DistinguishedName, -- who
|
|
|
|
* authentication CHOICE {
|
|
|
|
* simple [0] OCTET STRING -- passwd
|
|
|
|
#ifdef HAVE_KERBEROS
|
|
|
|
* krbv42ldap [1] OCTET STRING
|
|
|
|
* krbv42dsa [2] OCTET STRING
|
|
|
|
#endif
|
|
|
|
* sasl [3] SaslCredentials -- LDAPv3
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* BindResponse ::= SEQUENCE {
|
|
|
|
* COMPONENTS OF LDAPResult,
|
|
|
|
* serverSaslCreds OCTET STRING OPTIONAL -- LDAPv3
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_KERBEROS
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/krb.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include "ldap-int.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ldap_kerberos_bind1 - initiate a bind to the ldap server using
|
|
|
|
* kerberos authentication. The dn is supplied. It is assumed the user
|
|
|
|
* already has a valid ticket granting ticket. The msgid of the
|
|
|
|
* request is returned on success (suitable for passing to ldap_result()),
|
|
|
|
* -1 is returned if there's trouble.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* ldap_kerberos_bind1( ld, "cn=manager, o=university of michigan, c=us" )
|
|
|
|
*/
|
|
|
|
int
|
1999-05-19 09:53:09 +08:00
|
|
|
ldap_kerberos_bind1( LDAP *ld, LDAP_CONST char *dn )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
BerElement *ber;
|
|
|
|
char *cred;
|
1999-06-19 07:53:05 +08:00
|
|
|
int rc;
|
|
|
|
ber_len_t credlen;
|
1998-08-09 08:43:13 +08:00
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
int str_translation_on;
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
if ( dn == NULL )
|
|
|
|
dn = "";
|
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "ldapserver",
|
1998-08-09 08:43:13 +08:00
|
|
|
&credlen )) == NULL ) {
|
|
|
|
return( -1 ); /* ld_errno should already be set */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a message to send */
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( cred );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
if (( str_translation_on = (( ber->ber_options &
|
|
|
|
LBER_TRANSLATE_STRINGS ) != 0 ))) { /* turn translation off */
|
|
|
|
ber->ber_options &= ~LBER_TRANSLATE_STRINGS;
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
/* fill it in */
|
|
|
|
rc = ber_printf( ber, "{it{isto}}", ++ld->ld_msgid, LDAP_REQ_BIND,
|
|
|
|
ld->ld_version, dn, LDAP_AUTH_KRBV41, cred, credlen );
|
|
|
|
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
if ( str_translation_on ) { /* restore translation */
|
|
|
|
ber->ber_options |= LBER_TRANSLATE_STRINGS;
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
if ( rc == -1 ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( cred );
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_free( ber, 1 );
|
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( cred );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#ifndef LDAP_NOCACHE
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ld->ld_cache != NULL ) {
|
|
|
|
ldap_flush_cache( ld );
|
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
#endif /* !LDAP_NOCACHE */
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* send the message */
|
1998-08-09 11:04:12 +08:00
|
|
|
return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-05-19 09:53:09 +08:00
|
|
|
ldap_kerberos_bind1_s( LDAP *ld, LDAP_CONST char *dn )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
int msgid;
|
|
|
|
LDAPMessage *res;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind1_s\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
/* initiate the bind */
|
|
|
|
if ( (msgid = ldap_kerberos_bind1( ld, dn )) == -1 )
|
|
|
|
return( ld->ld_errno );
|
|
|
|
|
|
|
|
/* wait for a result */
|
|
|
|
if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
|
|
|
|
== -1 ) {
|
|
|
|
return( ld->ld_errno ); /* ldap_result sets ld_errno */
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ldap_result2error( ld, res, 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ldap_kerberos_bind2 - initiate a bind to the X.500 server using
|
|
|
|
* kerberos authentication. The dn is supplied. It is assumed the user
|
|
|
|
* already has a valid ticket granting ticket. The msgid of the
|
|
|
|
* request is returned on success (suitable for passing to ldap_result()),
|
|
|
|
* -1 is returned if there's trouble.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* ldap_kerberos_bind2( ld, "cn=manager, o=university of michigan, c=us" )
|
|
|
|
*/
|
|
|
|
int
|
1999-05-19 09:53:09 +08:00
|
|
|
ldap_kerberos_bind2( LDAP *ld, LDAP_CONST char *dn )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
BerElement *ber;
|
|
|
|
char *cred;
|
1999-06-19 07:53:05 +08:00
|
|
|
int rc;
|
|
|
|
ber_len_t credlen;
|
1998-08-09 08:43:13 +08:00
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
int str_translation_on;
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
if ( dn == NULL )
|
|
|
|
dn = "";
|
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
if ( (cred = ldap_get_kerberosv4_credentials( ld, dn, "x500dsa", &credlen ))
|
1998-08-09 08:43:13 +08:00
|
|
|
== NULL ) {
|
|
|
|
return( -1 ); /* ld_errno should already be set */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a message to send */
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( cred );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
if (( str_translation_on = (( ber->ber_options &
|
|
|
|
LBER_TRANSLATE_STRINGS ) != 0 ))) { /* turn translation off */
|
|
|
|
ber->ber_options &= ~LBER_TRANSLATE_STRINGS;
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
/* fill it in */
|
|
|
|
rc = ber_printf( ber, "{it{isto}}", ++ld->ld_msgid, LDAP_REQ_BIND,
|
|
|
|
ld->ld_version, dn, LDAP_AUTH_KRBV42, cred, credlen );
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
if ( str_translation_on ) { /* restore translation */
|
|
|
|
ber->ber_options |= LBER_TRANSLATE_STRINGS;
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( cred );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( rc == -1 ) {
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the message */
|
1998-08-09 11:04:12 +08:00
|
|
|
return ( ldap_send_initial_request( ld, LDAP_REQ_BIND, dn, ber ));
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* synchronous bind to DSA using kerberos */
|
|
|
|
int
|
1999-05-19 09:53:09 +08:00
|
|
|
ldap_kerberos_bind2_s( LDAP *ld, LDAP_CONST char *dn )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
int msgid;
|
|
|
|
LDAPMessage *res;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind2_s\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
/* initiate the bind */
|
|
|
|
if ( (msgid = ldap_kerberos_bind2( ld, dn )) == -1 )
|
|
|
|
return( ld->ld_errno );
|
|
|
|
|
|
|
|
/* wait for a result */
|
|
|
|
if ( ldap_result( ld, ld->ld_msgid, 1, (struct timeval *) 0, &res )
|
|
|
|
== -1 ) {
|
|
|
|
return( ld->ld_errno ); /* ldap_result sets ld_errno */
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ldap_result2error( ld, res, 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* synchronous bind to ldap and DSA using kerberos */
|
|
|
|
int
|
1999-05-19 09:53:09 +08:00
|
|
|
ldap_kerberos_bind_s( LDAP *ld, LDAP_CONST char *dn )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_kerberos_bind_s\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
if ( (err = ldap_kerberos_bind1_s( ld, dn )) != LDAP_SUCCESS )
|
|
|
|
return( err );
|
|
|
|
|
|
|
|
return( ldap_kerberos_bind2_s( ld, dn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef AUTHMAN
|
|
|
|
/*
|
1998-08-09 11:04:12 +08:00
|
|
|
* ldap_get_kerberosv4_credentials - obtain kerberos v4 credentials for ldap.
|
1998-08-09 08:43:13 +08:00
|
|
|
* The dn of the entry to which to bind is supplied. It's assumed the
|
|
|
|
* user already has a tgt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
1999-05-19 09:53:09 +08:00
|
|
|
ldap_get_kerberosv4_credentials(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAP_CONST char *who,
|
|
|
|
LDAP_CONST char *service,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t *len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
KTEXT_ST ktxt;
|
|
|
|
int err;
|
|
|
|
char realm[REALM_SZ], *cred, *krbinstance;
|
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_get_kerberosv4_credentials\n", 0, 0, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( (err = krb_get_tf_realm( tkt_string(), realm )) != KSUCCESS ) {
|
1998-10-25 09:41:42 +08:00
|
|
|
#ifdef LDAP_LIBUI
|
1998-08-09 08:43:13 +08:00
|
|
|
fprintf( stderr, "krb_get_tf_realm failed (%s)\n",
|
|
|
|
krb_err_txt[err] );
|
1998-10-25 09:41:42 +08:00
|
|
|
#endif /* LDAP_LIBUI */
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_INVALID_CREDENTIALS;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
krbinstance = ld->ld_defconn->lconn_krbinstance;
|
|
|
|
|
|
|
|
if ( (err = krb_mk_req( &ktxt, service, krbinstance, realm, 0 ))
|
|
|
|
!= KSUCCESS ) {
|
1998-10-25 09:41:42 +08:00
|
|
|
#ifdef LDAP_LIBUI
|
1998-08-09 08:43:13 +08:00
|
|
|
fprintf( stderr, "krb_mk_req failed (%s)\n", krb_err_txt[err] );
|
1998-10-25 09:41:42 +08:00
|
|
|
#endif /* LDAP_LIBUI */
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_INVALID_CREDENTIALS;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
if ( ( cred = LDAP_MALLOC( ktxt.length )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = ktxt.length;
|
|
|
|
memcpy( cred, ktxt.dat, ktxt.length );
|
|
|
|
|
|
|
|
return( cred );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !AUTHMAN */
|
1998-10-25 09:41:42 +08:00
|
|
|
#endif /* HAVE_KERBEROS */
|