/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (C) 1990 by the Massachusetts Institute of Technology
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*/
int
kerberos5_send(Authenticator *ap)
{
krb5_error_code ret;
krb5_ccache ccache;
int ap_opts;
krb5_data cksum_data;
char foo[2];
printf("[ Trying KERBEROS5 ... ]\r\n");
if (!UserNameRequested) {
if (auth_debug_mode) {
printf("Kerberos V5: no user name supplied\r\n");
}
return (0);
}
ret = krb5_cc_default(telnet_context, &ccache);
if (ret) {
if (auth_debug_mode) {
printf(
"Kerberos V5: could not get default ccache: %s\r\n",
get_krb5_err_text(telnet_context, ret));
}
return (0);
}
if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
ap_opts = AP_OPTS_MUTUAL_REQUIRED;
else
ap_opts = 0;
ap_opts |= AP_OPTS_USE_SUBKEY;
ret = krb5_auth_con_init(telnet_context, &auth_context);
if (ret) {
if (auth_debug_mode) {
printf(
"Kerberos V5: krb5_auth_con_init failed: %s\r\n",
get_krb5_err_text(telnet_context, ret));
}
return (0);
}
ret = krb5_auth_con_setaddrs_from_fd(telnet_context,
auth_context, &net);
if (ret) {
if (auth_debug_mode) {
printf("Kerberos V5: "
"krb5_auth_con_setaddrs_from_fd failed: %s\r\n",
get_krb5_err_text(telnet_context, ret));
}
return (0);
}
krb5_auth_con_setkeytype(telnet_context, auth_context,
KRB5_ENCTYPE_DES_CBC_CRC);
ret = krb5_verify_authenticator_checksum(telnet_context,
auth_context, foo, sizeof(foo));
if (ret) {
char *errbuf;
asprintf(&errbuf, "Bad checksum: %s",
get_krb5_err_text(telnet_context, ret));
Data(ap, KRB_REJECT, errbuf, -1);
if (auth_debug_mode)
printf("%s\r\n", errbuf);
free(errbuf);
return;
}
}
ret = krb5_auth_con_getremotesubkey(telnet_context,
auth_context, &key_block);
if (ret) {
Data(ap, KRB_REJECT, "krb5_auth_con_getremotesubkey failed", -1);
auth_finished(ap, AUTH_REJECT);
if (auth_debug_mode)
printf("Kerberos V5: "
"krb5_auth_con_getremotesubkey failed (%s)\r\n",
get_krb5_err_text(telnet_context, ret));
return;
}
if (key_block == NULL) {
ret = krb5_auth_con_getkey(telnet_context,
auth_context,
&key_block);
}
if (ret) {
Data(ap, KRB_REJECT, "krb5_auth_con_getkey failed", -1);
auth_finished(ap, AUTH_REJECT);
if (auth_debug_mode)
printf("Kerberos V5: "
"krb5_auth_con_getkey failed (%s)\r\n",
get_krb5_err_text(telnet_context, ret));
return;
}
if (key_block == NULL) {
Data(ap, KRB_REJECT, "no subkey received", -1);
auth_finished(ap, AUTH_REJECT);
if (auth_debug_mode)
printf("Kerberos V5: "
"krb5_auth_con_getremotesubkey returned NULL key\r\n");
return;
}
if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
ret = krb5_mk_rep(telnet_context,
auth_context, &outbuf);
if (ret) {
Data(ap, KRB_REJECT,
"krb5_mk_rep failed", -1);
auth_finished(ap, AUTH_REJECT);
if (auth_debug_mode)
printf("Kerberos V5: "
"krb5_mk_rep failed (%s)\r\n",
get_krb5_err_text(telnet_context,
ret));
krb5_free_keyblock(telnet_context, key_block);
return;
}
Data(ap, KRB_RESPONSE, outbuf.data, outbuf.length);
}
if (krb5_unparse_name(telnet_context, ticket->client, &name))
name = 0;
if (UserNameRequested && krb5_kuserok(telnet_context,
ticket->client, UserNameRequested)) {
Data(ap, KRB_ACCEPT, name ? name : "", name ? -1 : 0);
if (auth_debug_mode) {
printf("Kerberos5 identifies him as ``%s''\r\n",
name ? name : "");
}
if (key_block->keytype == ETYPE_DES_CBC_MD5 ||
key_block->keytype == ETYPE_DES_CBC_MD4 ||
key_block->keytype == ETYPE_DES_CBC_CRC) {
Session_Key skey;
ret = krb5_cc_resolve(telnet_context, ccname, &ccache);
if (ret) {
if (auth_debug_mode)
printf("Kerberos V5: could not get ccache: %s\r\n",
get_krb5_err_text(telnet_context,
ret));
break;
}
ret = krb5_cc_initialize(telnet_context, ccache,
ticket->client);
if (ret) {
if (auth_debug_mode)
printf("Kerberos V5: could not init ccache: %s\r\n",
get_krb5_err_text(telnet_context,
ret));
break;
}
ret = krb5_rd_cred2(telnet_context, auth_context,
ccache, &inbuf);
if (ret) {
char *errbuf;
ret = krb5_cc_default(telnet_context, &ccache);
if (ret) {
if (auth_debug_mode)
printf("KerberosV5: could not get default ccache: %s\r\n",
get_krb5_err_text(telnet_context, ret));
return;
}
ret = krb5_cc_get_principal(telnet_context, ccache, &principal);
if (ret) {
if (auth_debug_mode)
printf("KerberosV5: could not get principal: %s\r\n",
get_krb5_err_text(telnet_context, ret));
return;
}
memset(&creds, 0, sizeof(creds));
creds.client = principal;
ret = krb5_build_principal(telnet_context, &creds.server,
strlen(principal->realm), principal->realm, "krbtgt",
principal->realm, NULL);
if (ret) {
if (auth_debug_mode)
printf("KerberosV5: could not get principal: %s\r\n",
get_krb5_err_text(telnet_context, ret));
return;
}
creds.times.endtime = 0;