freebsd-nq/crypto/openssh/auth-krb4.c

369 lines
10 KiB
C
Raw Normal View History

2000-02-24 14:29:47 +00:00
/*
* Copyright (c) 1999 Dug Song. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
2000-02-24 14:29:47 +00:00
*/
#include "includes.h"
2003-04-23 17:13:13 +00:00
RCSID("$OpenBSD: auth-krb4.c,v 1.29 2003/02/21 10:34:48 mpech Exp $");
2001-05-04 04:14:23 +00:00
#include "ssh.h"
#include "ssh1.h"
2000-02-24 14:29:47 +00:00
#include "packet.h"
#include "xmalloc.h"
2001-05-04 04:14:23 +00:00
#include "log.h"
2000-02-24 14:29:47 +00:00
#include "servconf.h"
2002-03-18 10:09:43 +00:00
#include "uidswap.h"
2001-05-04 04:14:23 +00:00
#include "auth.h"
2000-02-24 14:29:47 +00:00
2001-05-04 04:14:23 +00:00
#ifdef AFS
#include "radix.h"
#endif
2000-02-24 14:29:47 +00:00
#ifdef KRB4
extern ServerOptions options;
2002-03-18 10:09:43 +00:00
static int
krb4_init(void *context)
{
static int cleanup_registered = 0;
Authctxt *authctxt = (Authctxt *)context;
const char *tkt_root = TKT_ROOT;
struct stat st;
int fd;
if (!authctxt->krb4_ticket_file) {
/* Set unique ticket string manually since we're still root. */
authctxt->krb4_ticket_file = xmalloc(MAXPATHLEN);
#ifdef AFS
if (lstat("/ticket", &st) != -1)
tkt_root = "/ticket/";
#endif /* AFS */
snprintf(authctxt->krb4_ticket_file, MAXPATHLEN, "%s%u_%ld",
tkt_root, authctxt->pw->pw_uid, (long)getpid());
2002-03-18 10:09:43 +00:00
krb_set_tkt_string(authctxt->krb4_ticket_file);
}
/* Register ticket cleanup in case of fatal error. */
if (!cleanup_registered) {
fatal_add_cleanup(krb4_cleanup_proc, authctxt);
cleanup_registered = 1;
}
/* Try to create our ticket file. */
if ((fd = mkstemp(authctxt->krb4_ticket_file)) != -1) {
close(fd);
return (1);
}
/* Ticket file exists - make sure user owns it (just passed ticket). */
if (lstat(authctxt->krb4_ticket_file, &st) != -1) {
if (st.st_mode == (S_IFREG | S_IRUSR | S_IWUSR) &&
st.st_uid == authctxt->pw->pw_uid)
return (1);
}
/* Failure - cancel cleanup function, leaving ticket for inspection. */
log("WARNING: bad ticket file %s", authctxt->krb4_ticket_file);
fatal_remove_cleanup(krb4_cleanup_proc, authctxt);
cleanup_registered = 0;
xfree(authctxt->krb4_ticket_file);
authctxt->krb4_ticket_file = NULL;
return (0);
}
2000-02-24 14:29:47 +00:00
/*
* try krb4 authentication,
* return 1 on success, 0 on failure, -1 if krb4 is not available
*/
int
2002-03-18 10:09:43 +00:00
auth_krb4_password(Authctxt *authctxt, const char *password)
2000-02-24 14:29:47 +00:00
{
AUTH_DAT adata;
KTEXT_ST tkt;
struct hostent *hp;
2002-03-18 10:09:43 +00:00
struct passwd *pw;
char localhost[MAXHOSTNAMELEN], phost[INST_SZ], realm[REALM_SZ];
u_int32_t faddr;
2000-02-24 14:29:47 +00:00
int r;
2002-03-18 10:09:43 +00:00
if ((pw = authctxt->pw) == NULL)
return (0);
2000-02-24 14:29:47 +00:00
/*
* Try Kerberos password authentication only for non-root
* users and only if Kerberos is installed.
*/
if (pw->pw_uid != 0 && krb_get_lrealm(realm, 1) == KSUCCESS) {
/* Set up our ticket file. */
2002-03-18 10:09:43 +00:00
if (!krb4_init(authctxt)) {
2000-02-24 14:29:47 +00:00
log("Couldn't initialize Kerberos ticket file for %s!",
pw->pw_name);
2002-03-18 10:09:43 +00:00
goto failure;
2000-02-24 14:29:47 +00:00
}
/* Try to get TGT using our password. */
2002-03-18 10:09:43 +00:00
r = krb_get_pw_in_tkt((char *) pw->pw_name, "", realm,
"krbtgt", realm, DEFAULT_TKT_LIFE, (char *)password);
2000-02-24 14:29:47 +00:00
if (r != INTK_OK) {
2002-03-18 10:09:43 +00:00
debug("Kerberos v4 password authentication for %s "
"failed: %s", pw->pw_name, krb_err_txt[r]);
goto failure;
2000-02-24 14:29:47 +00:00
}
/* Successful authentication. */
chown(tkt_string(), pw->pw_uid, pw->pw_gid);
/*
* Now that we have a TGT, try to get a local
* "rcmd" ticket to ensure that we are not talking
* to a bogus Kerberos server.
*/
2002-03-18 10:09:43 +00:00
gethostname(localhost, sizeof(localhost));
strlcpy(phost, (char *)krb_get_phost(localhost),
sizeof(phost));
2000-02-24 14:29:47 +00:00
r = krb_mk_req(&tkt, KRB4_SERVICE_NAME, phost, realm, 33);
if (r == KSUCCESS) {
2002-03-18 10:09:43 +00:00
if ((hp = gethostbyname(localhost)) == NULL) {
2000-02-24 14:29:47 +00:00
log("Couldn't get local host address!");
2002-03-18 10:09:43 +00:00
goto failure;
2000-02-24 14:29:47 +00:00
}
2002-03-18 10:09:43 +00:00
memmove((void *)&faddr, (void *)hp->h_addr,
2000-02-24 14:29:47 +00:00
sizeof(faddr));
/* Verify our "rcmd" ticket. */
r = krb_rd_req(&tkt, KRB4_SERVICE_NAME, phost,
faddr, &adata, "");
if (r == RD_AP_UNDEC) {
/*
* Probably didn't have a srvtab on
* localhost. Disallow login.
2000-02-24 14:29:47 +00:00
*/
2002-03-18 10:09:43 +00:00
log("Kerberos v4 TGT for %s unverifiable, "
2000-02-24 14:29:47 +00:00
"no srvtab installed? krb_rd_req: %s",
pw->pw_name, krb_err_txt[r]);
2002-03-18 10:09:43 +00:00
goto failure;
2000-02-24 14:29:47 +00:00
} else if (r != KSUCCESS) {
2002-03-18 10:09:43 +00:00
log("Kerberos v4 %s ticket unverifiable: %s",
2000-02-24 14:29:47 +00:00
KRB4_SERVICE_NAME, krb_err_txt[r]);
2002-03-18 10:09:43 +00:00
goto failure;
2000-02-24 14:29:47 +00:00
}
} else if (r == KDC_PR_UNKNOWN) {
/*
* Disallow login if no rcmd service exists, and
2000-02-24 14:29:47 +00:00
* log the error.
*/
2002-03-18 10:09:43 +00:00
log("Kerberos v4 TGT for %s unverifiable: %s; %s.%s "
2000-02-24 14:29:47 +00:00
"not registered, or srvtab is wrong?", pw->pw_name,
2002-03-18 10:09:43 +00:00
krb_err_txt[r], KRB4_SERVICE_NAME, phost);
goto failure;
2000-02-24 14:29:47 +00:00
} else {
/*
* TGT is bad, forget it. Possibly spoofed!
*/
2002-03-18 10:09:43 +00:00
debug("WARNING: Kerberos v4 TGT possibly spoofed "
"for %s: %s", pw->pw_name, krb_err_txt[r]);
goto failure;
2000-02-24 14:29:47 +00:00
}
/* Authentication succeeded. */
2002-03-18 10:09:43 +00:00
return (1);
} else
/* Logging in as root or no local Kerberos realm. */
debug("Unable to authenticate to Kerberos.");
2000-02-24 14:29:47 +00:00
2002-03-18 10:09:43 +00:00
failure:
krb4_cleanup_proc(authctxt);
if (!options.kerberos_or_local_passwd)
return (0);
2000-02-24 14:29:47 +00:00
/* Fall back to ordinary passwd authentication. */
2002-03-18 10:09:43 +00:00
return (-1);
2000-02-24 14:29:47 +00:00
}
void
2002-03-18 10:09:43 +00:00
krb4_cleanup_proc(void *context)
2000-02-24 14:29:47 +00:00
{
2002-03-18 10:09:43 +00:00
Authctxt *authctxt = (Authctxt *)context;
2000-02-24 14:29:47 +00:00
debug("krb4_cleanup_proc called");
2002-03-18 10:09:43 +00:00
if (authctxt->krb4_ticket_file) {
2000-02-24 14:29:47 +00:00
(void) dest_tkt();
2002-03-18 10:09:43 +00:00
xfree(authctxt->krb4_ticket_file);
authctxt->krb4_ticket_file = NULL;
2000-02-24 14:29:47 +00:00
}
}
int
2002-10-29 10:16:02 +00:00
auth_krb4(Authctxt *authctxt, KTEXT auth, char **client, KTEXT reply)
2000-02-24 14:29:47 +00:00
{
AUTH_DAT adat = {0};
2002-03-18 10:09:43 +00:00
Key_schedule schedule;
struct sockaddr_in local, foreign;
2000-02-24 14:29:47 +00:00
char instance[INST_SZ];
2000-03-26 07:37:48 +00:00
socklen_t slen;
2000-02-24 14:29:47 +00:00
u_int cksum;
2002-03-18 10:09:43 +00:00
int r, s;
2000-02-24 14:29:47 +00:00
s = packet_get_connection_in();
2000-03-26 07:37:48 +00:00
slen = sizeof(local);
2000-02-24 14:29:47 +00:00
memset(&local, 0, sizeof(local));
2000-03-26 07:37:48 +00:00
if (getsockname(s, (struct sockaddr *) & local, &slen) < 0)
2000-02-24 14:29:47 +00:00
debug("getsockname failed: %.100s", strerror(errno));
2000-03-26 07:37:48 +00:00
slen = sizeof(foreign);
2000-02-24 14:29:47 +00:00
memset(&foreign, 0, sizeof(foreign));
2000-03-26 07:37:48 +00:00
if (getpeername(s, (struct sockaddr *) & foreign, &slen) < 0) {
2000-02-24 14:29:47 +00:00
debug("getpeername failed: %.100s", strerror(errno));
fatal_cleanup();
}
instance[0] = '*';
instance[1] = 0;
/* Get the encrypted request, challenge, and session key. */
2002-03-18 10:09:43 +00:00
if ((r = krb_rd_req(auth, KRB4_SERVICE_NAME, instance,
0, &adat, ""))) {
debug("Kerberos v4 krb_rd_req: %.100s", krb_err_txt[r]);
return (0);
2000-02-24 14:29:47 +00:00
}
des_key_sched((des_cblock *) adat.session, schedule);
*client = xmalloc(MAX_K_NAME_SZ);
(void) snprintf(*client, MAX_K_NAME_SZ, "%s%s%s@%s", adat.pname,
*adat.pinst ? "." : "", adat.pinst, adat.prealm);
/* Check ~/.klogin authorization now. */
2002-03-18 10:09:43 +00:00
if (kuserok(&adat, authctxt->user) != KSUCCESS) {
log("Kerberos v4 .klogin authorization failed for %s to "
"account %s", *client, authctxt->user);
2000-02-24 14:29:47 +00:00
xfree(*client);
*client = NULL;
2002-03-18 10:09:43 +00:00
return (0);
2000-02-24 14:29:47 +00:00
}
/* Increment the checksum, and return it encrypted with the
session key. */
cksum = adat.checksum + 1;
cksum = htonl(cksum);
/* If we can't successfully encrypt the checksum, we send back an
empty message, admitting our failure. */
2002-10-29 10:16:02 +00:00
if ((r = krb_mk_priv((u_char *) & cksum, reply->dat, sizeof(cksum) + 1,
2000-02-24 14:29:47 +00:00
schedule, &adat.session, &local, &foreign)) < 0) {
2002-03-18 10:09:43 +00:00
debug("Kerberos v4 mk_priv: (%d) %s", r, krb_err_txt[r]);
2002-10-29 10:16:02 +00:00
reply->dat[0] = 0;
reply->length = 0;
2000-02-24 14:29:47 +00:00
} else
2002-10-29 10:16:02 +00:00
reply->length = r;
2000-02-24 14:29:47 +00:00
/* Clear session key. */
2003-04-23 17:13:13 +00:00
memset(&adat.session, 0, sizeof(adat.session));
2002-03-18 10:09:43 +00:00
return (1);
2000-02-24 14:29:47 +00:00
}
#endif /* KRB4 */
#ifdef AFS
int
2002-03-18 10:09:43 +00:00
auth_krb4_tgt(Authctxt *authctxt, const char *string)
2000-02-24 14:29:47 +00:00
{
CREDENTIALS creds;
2002-03-18 10:09:43 +00:00
struct passwd *pw;
if ((pw = authctxt->pw) == NULL)
goto failure;
temporarily_use_uid(pw);
2000-02-24 14:29:47 +00:00
if (!radix_to_creds(string, &creds)) {
2002-03-18 10:09:43 +00:00
log("Protocol error decoding Kerberos v4 TGT");
goto failure;
2000-02-24 14:29:47 +00:00
}
if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
strlcpy(creds.service, "krbtgt", sizeof creds.service);
if (strcmp(creds.service, "krbtgt")) {
2002-03-18 10:09:43 +00:00
log("Kerberos v4 TGT (%s%s%s@%s) rejected for %s",
2000-02-24 14:29:47 +00:00
creds.pname, creds.pinst[0] ? "." : "", creds.pinst,
creds.realm, pw->pw_name);
2002-03-18 10:09:43 +00:00
goto failure;
2000-02-24 14:29:47 +00:00
}
2002-03-18 10:09:43 +00:00
if (!krb4_init(authctxt))
goto failure;
2000-02-24 14:29:47 +00:00
if (in_tkt(creds.pname, creds.pinst) != KSUCCESS)
2002-03-18 10:09:43 +00:00
goto failure;
2000-02-24 14:29:47 +00:00
if (save_credentials(creds.service, creds.instance, creds.realm,
2002-03-18 10:09:43 +00:00
creds.session, creds.lifetime, creds.kvno, &creds.ticket_st,
creds.issue_date) != KSUCCESS) {
debug("Kerberos v4 TGT refused: couldn't save credentials");
goto failure;
2000-02-24 14:29:47 +00:00
}
/* Successful authentication, passed all checks. */
chown(tkt_string(), pw->pw_uid, pw->pw_gid);
2002-03-18 10:09:43 +00:00
debug("Kerberos v4 TGT accepted (%s%s%s@%s)",
creds.pname, creds.pinst[0] ? "." : "", creds.pinst, creds.realm);
2000-02-24 14:29:47 +00:00
memset(&creds, 0, sizeof(creds));
2002-03-18 10:09:43 +00:00
restore_uid();
return (1);
failure:
krb4_cleanup_proc(authctxt);
2000-02-24 14:29:47 +00:00
memset(&creds, 0, sizeof(creds));
2002-03-18 10:09:43 +00:00
restore_uid();
return (0);
2000-02-24 14:29:47 +00:00
}
int
2002-03-18 10:09:43 +00:00
auth_afs_token(Authctxt *authctxt, const char *token_string)
2000-02-24 14:29:47 +00:00
{
CREDENTIALS creds;
2002-03-18 10:09:43 +00:00
struct passwd *pw;
uid_t uid;
2000-02-24 14:29:47 +00:00
2002-03-18 10:09:43 +00:00
if ((pw = authctxt->pw) == NULL)
return (0);
2000-02-24 14:29:47 +00:00
if (!radix_to_creds(token_string, &creds)) {
log("Protocol error decoding AFS token");
2002-03-18 10:09:43 +00:00
return (0);
2000-02-24 14:29:47 +00:00
}
if (strncmp(creds.service, "", 1) == 0) /* backward compatibility */
strlcpy(creds.service, "afs", sizeof creds.service);
if (strncmp(creds.pname, "AFS ID ", 7) == 0)
uid = atoi(creds.pname + 7);
else
uid = pw->pw_uid;
2000-02-24 14:29:47 +00:00
if (kafs_settoken(creds.realm, uid, &creds)) {
2002-03-18 10:09:43 +00:00
log("AFS token (%s@%s) rejected for %s",
creds.pname, creds.realm, pw->pw_name);
2000-02-24 14:29:47 +00:00
memset(&creds, 0, sizeof(creds));
2002-03-18 10:09:43 +00:00
return (0);
2000-02-24 14:29:47 +00:00
}
2002-03-18 10:09:43 +00:00
debug("AFS token accepted (%s@%s)", creds.pname, creds.realm);
2000-02-24 14:29:47 +00:00
memset(&creds, 0, sizeof(creds));
2002-03-18 10:09:43 +00:00
return (1);
2000-02-24 14:29:47 +00:00
}
#endif /* AFS */