#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)klogin.c 5.11 (Berkeley) 7/12/92";
#endif
__RCSID("$NetBSD: k5login.c,v 1.33 2012/04/24 16:52:26 christos Exp $");
#endif
#ifdef KERBEROS5
#include <sys/param.h>
#include <sys/syslog.h>
#include <krb5/krb5.h>
#include <pwd.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define KRB5_DEFAULT_OPTIONS 0
#define KRB5_DEFAULT_LIFE 60*60*10
krb5_context kcontext;
extern int notickets;
int krb5_configured;
char *krb5tkfile_env;
extern char *tty;
extern int login_krb5_forwardable_tgt;
extern int has_ccache;
static char tkt_location[MAXPATHLEN];
static krb5_creds forw_creds;
int have_forward;
static krb5_principal me;
int k5_read_creds(char *);
int k5_write_creds(void);
int k5_verify_creds(krb5_context, krb5_ccache);
int k5login(struct passwd *, char *, char *, char *);
void k5destroy(void);
static void __printflike(3, 4)
k5_log(krb5_context context, krb5_error_code kerror, const char *fmt, ...)
{
const char *msg = krb5_get_error_message(context, kerror);
char *str;
va_list ap;
va_start(ap, fmt);
if (vasprintf(&str, fmt, ap) == -1) {
va_end(ap);
syslog(LOG_NOTICE, "Cannot allocate memory for error %s: %s",
fmt, msg);
return;
}
va_end(ap);
syslog(LOG_NOTICE, "warning: %s: %s", str, msg);
krb5_free_error_message(kcontext, msg);
free(str);
}
int
k5_verify_creds(krb5_context c, krb5_ccache ccache)
{
char phost[MAXHOSTNAMELEN];
int retval, have_keys;
krb5_principal princ;
krb5_keyblock *kb = 0;
krb5_error_code kerror;
krb5_data packet;
krb5_auth_context auth_context = NULL;
krb5_ticket *ticket = NULL;
kerror = krb5_sname_to_principal(c, 0, 0, KRB5_NT_SRV_HST, &princ);
if (kerror) {
krb5_warn(kcontext, kerror, "constructing local service name");
return (-1);
}
kerror = krb5_kt_read_service_key(c, NULL, princ, 0, 0, &kb);
if (kb)
krb5_free_keyblock(c, kb);
have_keys = kerror ? 0 : 1;
gethostname(phost, sizeof(phost));
phost[sizeof(phost) - 1] = '\0';
kerror = krb5_mk_req(c, &auth_context, 0, "host", phost,
0, ccache, &packet);
if (auth_context) {
krb5_auth_con_free(c, auth_context);
auth_context = NULL;
}
if (kerror == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
if (have_keys) {
retval = -1;
goto EGRESS;
}
else {
retval = 0;
goto EGRESS;
}
}
else if (kerror) {
krb5_warn(kcontext, kerror,
"Unable to verify Kerberos V5 TGT: %s", phost);
k5_log(kcontext, kerror, "Kerberos V5 TGT bad");
retval = -1;
goto EGRESS;
}
kerror = krb5_rd_req(c, &auth_context, &packet,
princ, NULL, NULL, &ticket);
if (kerror) {
if (!have_keys) {
switch (kerror) {
case ENOENT:
case KRB5_KT_NOTFOUND:
retval = 0;
break;
default:
retval = -1;
break;
}
}
else {
retval = -1;
}
krb5_warn(kcontext, kerror, "Unable to verify host ticket");
k5_log(kcontext, kerror, "can't verify v5 ticket (%s)",
retval ? "keytab found, assuming failure" :
"no keytab found, assuming success");
goto EGRESS;
}
retval = 1;
EGRESS:
if (auth_context)
krb5_auth_con_free(c, auth_context);
krb5_free_principal(c, princ);
return (retval);
}
int
k5_read_creds(char *username)
{
krb5_error_code kerror;
krb5_creds mcreds;
krb5_ccache ccache;
have_forward = 0;
memset((char*) &mcreds, 0, sizeof(forw_creds));
memset((char*) &forw_creds, 0, sizeof(forw_creds));
kerror = krb5_cc_default(kcontext, &ccache);
if (kerror) {
krb5_warn(kcontext, kerror, "while getting default ccache");
return(1);
}
kerror = krb5_parse_name(kcontext, username, &me);
if (kerror) {
krb5_warn(kcontext, kerror, "when parsing name %s", username);
return(1);
}
mcreds.client = me;
const char *realm = krb5_principal_get_realm(kcontext, me);
size_t rlen = strlen(realm);
kerror = krb5_build_principal_ext(kcontext, &mcreds.server,
rlen, realm,
KRB5_TGS_NAME_SIZE,
KRB5_TGS_NAME,
rlen, realm,
0);
if (kerror) {
krb5_warn(kcontext, kerror, "while building server name");
goto nuke_ccache;
}
kerror = krb5_cc_retrieve_cred(kcontext, ccache, 0,
&mcreds, &forw_creds);
if (kerror) {
krb5_warn(kcontext, kerror,
"while retrieving V5 initial ticket for copy");
goto nuke_ccache;
}
have_forward = 1;
strlcpy(tkt_location, getenv("KRB5CCNAME"), sizeof(tkt_location));
krb5tkfile_env = tkt_location;
has_ccache = 1;
notickets = 0;
nuke_ccache:
krb5_cc_destroy(kcontext, ccache);
return(!have_forward);
}
int
k5_write_creds(void)
{
krb5_error_code kerror;
krb5_ccache ccache;
if (!have_forward)
return (1);
kerror = krb5_cc_default(kcontext, &ccache);
if (kerror) {
krb5_warn(kcontext, kerror, "while getting default ccache");
return (1);
}
kerror = krb5_cc_initialize(kcontext, ccache, me);
if (kerror) {
krb5_warn(kcontext, kerror,
"while re-initializing V5 ccache as user");
goto nuke_ccache_contents;
}
kerror = krb5_cc_store_cred(kcontext, ccache, &forw_creds);
if (kerror) {
krb5_warn(kcontext, kerror,
"while re-storing V5 ccache as user");
goto nuke_ccache_contents;
}
nuke_ccache_contents:
krb5_free_cred_contents(kcontext, &forw_creds);
return (kerror != 0);
}
int
k5login(struct passwd *pw, char *instance, char *localhost, char *password)
{
krb5_error_code kerror;
krb5_creds my_creds;
krb5_ccache ccache = NULL;
char *realm, *client_name;
char *principal;
krb5_configured = 1;
if (strcmp(pw->pw_name, "root") == 0 ||
krb5_get_default_realm(kcontext, &realm)) {
krb5_configured = 0;
return (1);
}
if (strcmp(instance, "root") != 0)
(void)snprintf(tkt_location, sizeof tkt_location,
"FILE:/tmp/krb5cc_%d", pw->pw_uid);
else
(void)snprintf(tkt_location, sizeof tkt_location,
"FILE:/tmp/krb5cc_root_%d", pw->pw_uid);
krb5tkfile_env = tkt_location;
has_ccache = 1;
if (strlen(instance))
asprintf(&principal, "%s/%s", pw->pw_name, instance);
else
principal = strdup(pw->pw_name);
if (!principal) {
syslog(LOG_NOTICE, "fatal: %s", strerror(errno));
return (1);
}
if ((kerror = krb5_cc_resolve(kcontext, tkt_location, &ccache)) != 0) {
k5_log(kcontext, kerror, "while getting default ccache");
return (1);
}
if ((kerror = krb5_parse_name(kcontext, principal, &me)) != 0) {
k5_log(kcontext, kerror, "when parsing name %s", principal);
return (1);
}
if ((kerror = krb5_unparse_name(kcontext, me, &client_name)) != 0) {
k5_log(kcontext, kerror, "when unparsing name %s", principal);
return (1);
}
kerror = krb5_cc_initialize(kcontext, ccache, me);
if (kerror != 0) {
k5_log(kcontext, kerror, "when initializing cache %s",
tkt_location);
return (1);
}
memset(&my_creds, 0, sizeof(my_creds));
krb5_get_init_creds_opt *opt;
if ((kerror = krb5_get_init_creds_opt_alloc(kcontext, &opt)) != 0) {
k5_log(kcontext, kerror, "while getting options");
return (1);
}
if (login_krb5_forwardable_tgt)
krb5_get_init_creds_opt_set_forwardable(opt, 1);
kerror = krb5_get_init_creds_password(kcontext, &my_creds, me, password,
NULL, NULL, 0, NULL, opt);
krb5_get_init_creds_opt_free(kcontext, opt);
if (kerror == 0)
kerror = krb5_cc_store_cred(kcontext, ccache, &my_creds);
if (chown(&tkt_location[5], pw->pw_uid, pw->pw_gid) < 0)
syslog(LOG_ERR, "chown tkfile (%s): %m", &tkt_location[5]);
if (kerror) {
if (kerror == KRB5KRB_AP_ERR_BAD_INTEGRITY)
printf("%s: Kerberos Password incorrect\n", principal);
else
krb5_warn(kcontext, kerror,
"while getting initial credentials");
return (1);
}
if (k5_verify_creds(kcontext, ccache) < 0)
return (1);
notickets = 0;
return (0);
}
void
k5destroy(void)
{
krb5_error_code kerror;
krb5_ccache ccache = NULL;
if (krb5tkfile_env == NULL)
return;
kerror = krb5_cc_resolve(kcontext, krb5tkfile_env, &ccache);
if (kerror == 0)
(void)krb5_cc_destroy(kcontext, ccache);
}
#endif