#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <krb5.h>
#include <com_err.h>
#include <Kerberos/krb5-ipc.h>
#include <CoreFoundation/CoreFoundation.h>
#include <OpenDirectory/OpenDirectory.h>
#include <OpenDirectory/OpenDirectoryPriv.h>
#include <DirectoryService/DirectoryService.h>
#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_SM_PASSWORD
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/openpam.h>
#define COMPAT_MIT
static int verify_krb_v5_tgt(krb5_context, krb5_ccache, char *, int);
static const char *compat_princ_component(krb5_context, krb5_principal, int);
static void compat_free_data_contents(krb5_context, krb5_data *);
#define USER_PROMPT "Username: "
#define PASSWORD_PROMPT "Password:"
#define NEW_PASSWORD_PROMPT "New Password:"
#define PAM_OPT_CCACHE "ccache"
#define PAM_OPT_DEBUG "debug"
#define PAM_OPT_DEFAULT_PRINCIPAL "default_principal"
#define PAM_OPT_FORWARDABLE "forwardable"
#define PAM_OPT_NO_CCACHE "no_ccache"
#define PAM_OPT_REUSE_CCACHE "reuse_ccache"
#define PAM_OPT_USE_FIRST_PASS "use_first_pass"
#define PAM_OPT_AUTH_AS_SELF "auth_as_self"
#define PAM_OPT_DEBUG "debug"
#define PAM_LOG(...) \
openpam_log(PAM_LOG_DEBUG, __VA_ARGS__)
#define PAM_VERBOSE_ERROR(...) \
openpam_log(PAM_LOG_ERROR, __VA_ARGS__)
#ifdef COMPAT_MIT
#define krb5_get_err_text(c,e) error_message(e)
#endif
static int
od_principal_for_user(const char *user, char **od_principal)
{
int retval = -1;
ODNodeRef cfNodeRef = NULL;
CFStringRef cfUser = NULL;
ODRecordRef cfRecord = NULL;
CFArrayRef vals = NULL;
CFArrayRef authparts = NULL;
CFStringRef principal = NULL;
CFArrayRef princparts = NULL;
CFStringRef realm = NULL;
if (NULL == (cfNodeRef = ODNodeCreateWithNodeType(kCFAllocatorDefault, kODSessionDefault, eDSAuthenticationSearchNodeName, NULL)))
goto fin;
if (NULL == (cfUser = CFStringCreateWithCString(NULL, user, kCFStringEncodingUTF8)))
goto fin;
if (NULL == (cfRecord = ODNodeCopyRecord(cfNodeRef, CFSTR(kDSStdRecordTypeUsers), cfUser, NULL, NULL)))
goto fin;
if (NULL == (vals = ODRecordCopyValues(cfRecord, CFSTR(kDSNAttrAuthenticationAuthority), NULL)))
goto fin;
CFIndex count = CFArrayGetCount(vals);
CFIndex i;
for (i = 0; i < count; ++i) {
const void *val = CFArrayGetValueAtIndex(vals, i);
if (val == NULL || CFGetTypeID(val) != CFStringGetTypeID() || !CFStringHasPrefix(val, CFSTR(";Kerberosv5;")))
continue;
if (NULL == (authparts = CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault, val, CFSTR(";"))) || CFArrayGetCount(authparts) < 4)
goto fin;
if (NULL == (principal = CFArrayGetValueAtIndex(authparts, 3)))
goto fin;
if (NULL == (princparts = CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault, principal, CFSTR("@"))) || CFArrayGetCount(princparts) < 2)
goto fin;
if (NULL == (realm = CFArrayGetValueAtIndex(princparts, 1)))
goto fin;
if (CFStringHasPrefix(realm, CFSTR("LKDC:"))) {
CFRelease(authparts);
CFRelease(princparts);
authparts = princparts = NULL;
continue;
}
CFIndex maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(principal), kCFStringEncodingUTF8);
char *buffer = malloc(maxlen+1);
if (NULL != buffer && CFStringGetCString(principal, buffer, maxlen+1, kCFStringEncodingUTF8)) {
*od_principal = buffer;
retval = 0;
} else {
free(buffer);
}
}
fin:
if (NULL != cfNodeRef)
CFRelease(cfNodeRef);
if (NULL != cfUser)
CFRelease(cfUser);
if (NULL != cfRecord)
CFRelease(cfRecord);
if (NULL != vals)
CFRelease(vals);
if (NULL != authparts)
CFRelease(authparts);
if (NULL != princparts)
CFRelease(princparts);
return retval;
}
PAM_EXTERN int
pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
int argc __unused, const char *argv[] __unused)
{
krb5_error_code krbret;
krb5_context pam_context;
krb5_creds creds;
krb5_principal princ;
krb5_ccache ccache;
krb5_get_init_creds_opt opts;
struct passwd *pwd;
int retval;
const void *ccache_data;
const char *user;
char *pass;
const void *sourceuser, *service;
char *principal = NULL, *princ_name = NULL, *ccache_name, luser[32], *srvdup;
retval = pam_get_user(pamh, &user, USER_PROMPT);
if (retval != PAM_SUCCESS)
return (retval);
PAM_LOG("Got user: %s", user);
retval = pam_get_item(pamh, PAM_RUSER, &sourceuser);
if (retval != PAM_SUCCESS)
return (retval);
PAM_LOG("Got ruser: %s", (const char *)sourceuser);
service = NULL;
pam_get_item(pamh, PAM_SERVICE, &service);
if (service == NULL)
service = "unknown";
PAM_LOG("Got service: %s", (const char *)service);
krbret = krb5_init_secure_context(&pam_context);
if (krbret != 0) {
PAM_VERBOSE_ERROR("Kerberos 5 error");
return (PAM_SERVICE_ERR);
}
PAM_LOG("Context initialised");
krb5_get_init_creds_opt_init(&opts);
if (openpam_get_option(pamh, PAM_OPT_FORWARDABLE))
krb5_get_init_creds_opt_set_forwardable(&opts, 1);
PAM_LOG("Credentials initialised");
if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
asprintf(&principal, "%s/%s", (const char *)sourceuser, user);
else if (NULL == openpam_get_option(pamh, PAM_OPT_DEFAULT_PRINCIPAL))
od_principal_for_user(user, &principal);
else
principal = strdup(user);
if (principal == NULL) {
PAM_VERBOSE_ERROR("Failed to determine Kerberos principal name.");
return (PAM_SERVICE_ERR);
}
PAM_LOG("Created principal: %s", principal);
krbret = krb5_parse_name(pam_context, principal, &princ);
free(principal);
if (krbret != 0) {
PAM_LOG("Error krb5_parse_name(): %s",
krb5_get_err_text(pam_context, krbret));
PAM_VERBOSE_ERROR("Kerberos 5 error");
retval = PAM_SERVICE_ERR;
goto cleanup3;
}
PAM_LOG("Done krb5_parse_name()");
princ_name = NULL;
krbret = krb5_unparse_name(pam_context, princ, &princ_name);
if (krbret != 0) {
PAM_LOG("Error krb5_unparse_name(): %s",
krb5_get_err_text(pam_context, krbret));
PAM_VERBOSE_ERROR("Kerberos 5 error");
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
PAM_LOG("Got principal: %s", princ_name);
retval = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&pass);
if ((PAM_SUCCESS == retval) && (NULL == pass))
retval = pam_get_authtok(pamh, PAM_AUTHTOK, (const char **)&pass, PASSWORD_PROMPT);
if (retval != PAM_SUCCESS)
goto cleanup2;
PAM_LOG("Got password");
if (strchr(user, '@')) {
krbret = krb5_aname_to_localname(pam_context, princ,
sizeof(luser), luser);
if (krbret != 0) {
PAM_VERBOSE_ERROR("Kerberos 5 error");
PAM_LOG("Error krb5_aname_to_localname(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_USER_UNKNOWN;
goto cleanup2;
}
retval = pam_set_item(pamh, PAM_USER, luser);
if (retval != PAM_SUCCESS)
goto cleanup2;
PAM_LOG("PAM_USER Redone");
}
pwd = getpwnam(user);
if (pwd == NULL) {
retval = PAM_USER_UNKNOWN;
goto cleanup2;
}
PAM_LOG("Done getpwnam()");
memset(&creds, 0, sizeof(krb5_creds));
krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
pass, NULL, pamh, 0, NULL, &opts);
if (krbret != 0) {
PAM_VERBOSE_ERROR("Kerberos 5 error");
PAM_LOG("Error krb5_get_init_creds_password(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_AUTH_ERR;
goto cleanup2;
}
PAM_LOG("Got TGT");
krbret = krb5_cc_new_unique(pam_context, "FILE", NULL, &ccache);
if (krbret != 0) {
PAM_VERBOSE_ERROR("Kerberos 5 error");
PAM_LOG("Error krb5_cc_gen_new(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_SERVICE_ERR;
goto cleanup;
}
krbret = krb5_cc_initialize(pam_context, ccache, princ);
if (krbret != 0) {
PAM_VERBOSE_ERROR("Kerberos 5 error");
PAM_LOG("Error krb5_cc_initialize(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_SERVICE_ERR;
goto cleanup;
}
krbret = krb5_cc_store_cred(pam_context, ccache, &creds);
if (krbret != 0) {
PAM_VERBOSE_ERROR("Kerberos 5 error");
PAM_LOG("Error krb5_cc_store_cred(): %s",
krb5_get_err_text(pam_context, krbret));
krb5_cc_destroy(pam_context, ccache);
retval = PAM_SERVICE_ERR;
goto cleanup;
}
if (0 == strcmp("FILE", krb5_cc_get_type(pam_context, ccache)))
chown(krb5_cc_get_name(pam_context, ccache), pwd->pw_uid, pwd->pw_gid);
PAM_LOG("Credentials stashed");
if ((srvdup = strdup(service)) == NULL) {
retval = PAM_BUF_ERR;
goto cleanup;
}
krbret = verify_krb_v5_tgt(pam_context, ccache, srvdup,
openpam_get_option(pamh, PAM_OPT_DEBUG) ? 1 : 0);
free(srvdup);
if (krbret == -1) {
PAM_VERBOSE_ERROR("Kerberos 5 error");
krb5_cc_destroy(pam_context, ccache);
retval = PAM_AUTH_ERR;
goto cleanup;
}
PAM_LOG("Credentials stash verified");
retval = pam_get_data(pamh, "ccache", &ccache_data);
if (retval == PAM_SUCCESS) {
krb5_cc_destroy(pam_context, ccache);
PAM_VERBOSE_ERROR("Kerberos 5 error");
retval = PAM_AUTH_ERR;
goto cleanup;
}
PAM_LOG("Credentials stash not pre-existing");
asprintf(&ccache_name, "%s:%s", krb5_cc_get_type(pam_context,
ccache), krb5_cc_get_name(pam_context, ccache));
if (ccache_name == NULL) {
PAM_VERBOSE_ERROR("Kerberos 5 error");
retval = PAM_BUF_ERR;
goto cleanup;
}
retval = pam_setenv(pamh, "krb5_ccache", ccache_name, 1);
if (retval != 0) {
krb5_cc_destroy(pam_context, ccache);
PAM_VERBOSE_ERROR("Kerberos 5 error");
retval = PAM_SERVICE_ERR;
goto cleanup;
}
PAM_LOG("Credentials stash saved");
cleanup:
krb5_free_cred_contents(pam_context, &creds);
PAM_LOG("Done cleanup");
cleanup2:
krb5_free_principal(pam_context, princ);
PAM_LOG("Done cleanup2");
cleanup3:
if (princ_name)
free(princ_name);
krb5_free_context(pam_context);
PAM_LOG("Done cleanup3");
if (retval != PAM_SUCCESS)
PAM_VERBOSE_ERROR("Kerberos 5 refuses you");
return (retval);
}
PAM_EXTERN int
pam_sm_setcred(pam_handle_t *pamh, int flags,
int argc __unused, const char *argv[] __unused)
{
#ifdef _FREEFALL_CONFIG
return (PAM_SUCCESS);
#else
krb5_error_code krbret;
krb5_context pam_context;
krb5_principal princ = NULL;
krb5_creds creds;
krb5_ccache ccache_temp, ccache_perm;
krb5_cc_cursor cursor;
struct passwd *pwd = NULL;
int retval;
const char *cache_type, *cache_name, *q;
const void *user;
const void *cache_data;
char *cache_name_buf = NULL, *p, *cache_type_colon_name = NULL;
uid_t euid;
gid_t egid;
if (flags & PAM_DELETE_CRED)
return (PAM_SUCCESS);
if (flags & PAM_REFRESH_CRED)
return (PAM_SUCCESS);
if (flags & PAM_REINITIALIZE_CRED)
return (PAM_SUCCESS);
if (!(flags & PAM_ESTABLISH_CRED))
return (PAM_SERVICE_ERR);
if (openpam_get_option(pamh, PAM_OPT_NO_CCACHE))
return (PAM_SUCCESS);
PAM_LOG("Establishing credentials");
retval = pam_get_item(pamh, PAM_USER, &user);
if (retval != PAM_SUCCESS)
return (retval);
PAM_LOG("Got user: %s", (const char *)user);
krbret = krb5_init_secure_context(&pam_context);
if (krbret != 0) {
PAM_LOG("Error krb5_init_secure_context() failed");
return (PAM_SERVICE_ERR);
}
PAM_LOG("Context initialised");
euid = geteuid();
egid = getegid();
PAM_LOG("Got euid, egid: %d %d", euid, egid);
if ((cache_data = pam_getenv(pamh, "krb5_ccache")) == NULL) {
PAM_LOG("Error pam_getenv failed.");
retval = PAM_IGNORE;
goto cleanup3;
}
krbret = krb5_cc_resolve(pam_context, cache_data, &ccache_temp);
if (krbret != 0) {
PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)cache_data,
krb5_get_err_text(pam_context, krbret));
retval = PAM_SERVICE_ERR;
goto cleanup3;
}
pwd = getpwnam(user);
if (pwd == NULL) {
retval = PAM_USER_UNKNOWN;
goto cleanup3;
}
PAM_LOG("Done getpwnam()");
if (0 == egid && 0 != setegid(pwd->pw_gid)) {
retval = PAM_SERVICE_ERR;
goto cleanup3;
}
if (0 == euid && 0 != seteuid(pwd->pw_uid)) {
retval = PAM_SERVICE_ERR;
goto cleanup3;
}
PAM_LOG("Done setegid() & seteuid()");
krbret = krb5_cc_get_principal(pam_context, ccache_temp, &princ);
if (krbret != 0) {
PAM_LOG("Error krb5_cc_get_principal(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_SERVICE_ERR;
goto cleanup3;
}
cache_name = openpam_get_option(pamh, PAM_OPT_CCACHE);
if (cache_name == NULL) {
krb5_ipc_client_set_target_uid(pwd->pw_uid);
krbret = krb5_cc_default(pam_context, &ccache_perm);
}
else {
size_t len = (PATH_MAX + 16);
p = calloc(len, sizeof(char));
q = cache_name;
if (p == NULL) {
PAM_LOG("Error malloc(): failure");
retval = PAM_BUF_ERR;
goto cleanup3;
}
cache_name = cache_name_buf = p;
while (*q) {
if (*q == '%') {
q++;
if (*q == 'u') {
len -= snprintf(p, len, "%d", pwd->pw_uid);
p += strlen(p);
}
else if (*q == 'p') {
len -= snprintf(p, len, "%d", getpid());
p += strlen(p);
}
else {
*p++ = '%';
len--;
q--;
}
q++;
}
else {
*p++ = *q++;
len--;
}
}
PAM_LOG("Got cache_name: %s", cache_name);
krbret = krb5_cc_resolve(pam_context, cache_name, &ccache_perm);
if (krbret != 0) {
PAM_LOG("Error krb5_cc_resolve(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
}
krbret = krb5_cc_initialize(pam_context, ccache_perm, princ);
if (krbret != 0) {
PAM_LOG("Error krb5_cc_initialize(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
PAM_LOG("Cache initialised");
cache_type = krb5_cc_get_type(pam_context, ccache_perm);
cache_name = krb5_cc_get_name(pam_context, ccache_perm);
PAM_LOG("Got cache_name: %s:%s", cache_type, cache_name);
krbret = krb5_cc_start_seq_get(pam_context, ccache_temp, &cursor);
if (krbret != 0) {
PAM_LOG("Error krb5_cc_start_seq_get(): %s",
krb5_get_err_text(pam_context, krbret));
krb5_cc_destroy(pam_context, ccache_perm);
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
PAM_LOG("Prepared for iteration");
while ((krbret = krb5_cc_next_cred(pam_context, ccache_temp,
&cursor, &creds) == 0)) {
krbret = krb5_cc_store_cred(pam_context, ccache_perm, &creds);
if (krbret != 0) {
PAM_LOG("Error krb5_cc_store_cred(): %s",
krb5_get_err_text(pam_context, krbret));
krb5_cc_destroy(pam_context, ccache_perm);
krb5_free_cred_contents(pam_context, &creds);
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
krb5_free_cred_contents(pam_context, &creds);
PAM_LOG("Iteration");
}
krb5_cc_end_seq_get(pam_context, ccache_temp, &cursor);
PAM_LOG("Done iterating");
if (0 == strcmp(cache_type, "FILE")) {
if (chown(cache_name, pwd->pw_uid, pwd->pw_gid) == -1) {
PAM_LOG("Error chown(): %s", strerror(errno));
krb5_cc_destroy(pam_context, ccache_perm);
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
PAM_LOG("Done chown()");
if (chmod(cache_name, (S_IRUSR | S_IWUSR)) == -1) {
PAM_LOG("Error chmod(): %s", strerror(errno));
krb5_cc_destroy(pam_context, ccache_perm);
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
PAM_LOG("Done chmod()");
}
asprintf(&cache_type_colon_name, "%s:%s", cache_type, cache_name);
if (NULL == cache_type_colon_name)
goto cleanup2;
retval = pam_setenv(pamh, "KRB5CCNAME", cache_type_colon_name, 1);
free(cache_type_colon_name);
if (retval != PAM_SUCCESS) {
PAM_LOG("Error pam_setenv(): %s", pam_strerror(pamh, retval));
krb5_cc_destroy(pam_context, ccache_perm);
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
PAM_LOG("Environment done: KRB5CCNAME=%s", cache_name);
krb5_cc_close(pam_context, ccache_perm);
PAM_LOG("Cache closed");
cleanup2:
if (NULL != princ)
krb5_free_principal(pam_context, princ);
PAM_LOG("Done cleanup2");
cleanup3:
krb5_free_context(pam_context);
PAM_LOG("Done cleanup3");
seteuid(euid);
setegid(egid);
krb5_ipc_client_clear_target();
PAM_LOG("Done seteuid() & setegid()");
if (cache_name_buf != NULL)
free(cache_name_buf);
return (retval);
#endif
}
PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
int argc __unused, const char *argv[] __unused)
{
krb5_error_code krbret;
krb5_context pam_context;
krb5_ccache ccache;
krb5_principal princ;
int retval;
const void *user;
const void *ccache_name;
retval = pam_get_item(pamh, PAM_USER, &user);
if (retval != PAM_SUCCESS)
return (retval);
PAM_LOG("Got user: %s", (const char *)user);
retval = pam_get_data(pamh, "ccache", &ccache_name);
if (retval != PAM_SUCCESS)
return (PAM_SUCCESS);
PAM_LOG("Got credentials");
krbret = krb5_init_secure_context(&pam_context);
if (krbret != 0) {
PAM_LOG("Error krb5_init_secure_context() failed");
return (PAM_PERM_DENIED);
}
PAM_LOG("Context initialised");
krbret = krb5_cc_resolve(pam_context, (const char *)ccache_name, &ccache);
if (krbret != 0) {
PAM_LOG("Error krb5_cc_resolve(\"%s\"): %s", (const char *)ccache_name,
krb5_get_err_text(pam_context, krbret));
krb5_free_context(pam_context);
return (PAM_PERM_DENIED);
}
PAM_LOG("Got ccache %s", (const char *)ccache_name);
krbret = krb5_cc_get_principal(pam_context, ccache, &princ);
if (krbret != 0) {
PAM_LOG("Error krb5_cc_get_principal(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_PERM_DENIED;;
goto cleanup;
}
PAM_LOG("Got principal");
if (krb5_kuserok(pam_context, princ, (const char *)user))
retval = PAM_SUCCESS;
else
retval = PAM_PERM_DENIED;
krb5_free_principal(pam_context, princ);
PAM_LOG("Done kuserok()");
cleanup:
krb5_free_context(pam_context);
PAM_LOG("Done cleanup");
return (retval);
}
PAM_EXTERN int
pam_sm_chauthtok(pam_handle_t *pamh, int flags,
int argc __unused, const char *argv[] __unused)
{
krb5_error_code krbret;
krb5_context pam_context;
krb5_creds creds;
krb5_principal princ;
krb5_get_init_creds_opt opts;
krb5_data result_code_string, result_string;
int result_code, retval;
char *pass;
const void *user;
char *princ_name = NULL, *passdup;
if (!(flags & PAM_UPDATE_AUTHTOK))
return (PAM_AUTHTOK_ERR);
retval = pam_get_item(pamh, PAM_USER, &user);
if (retval != PAM_SUCCESS)
return (retval);
PAM_LOG("Got user: %s", (const char *)user);
krbret = krb5_init_secure_context(&pam_context);
if (krbret != 0) {
PAM_LOG("Error krb5_init_secure_context() failed");
return (PAM_SERVICE_ERR);
}
PAM_LOG("Context initialised");
krb5_get_init_creds_opt_init(&opts);
PAM_LOG("Credentials options initialised");
krbret = krb5_parse_name(pam_context, (const char *)user, &princ);
if (krbret != 0) {
PAM_LOG("Error krb5_parse_name(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_USER_UNKNOWN;
goto cleanup3;
}
princ_name = NULL;
krbret = krb5_unparse_name(pam_context, princ, &princ_name);
if (krbret != 0) {
PAM_LOG("Error krb5_unparse_name(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_SERVICE_ERR;
goto cleanup2;
}
PAM_LOG("Got principal: %s", princ_name);
retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, (const char **)&pass, PASSWORD_PROMPT);
if (retval != PAM_SUCCESS)
goto cleanup2;
PAM_LOG("Got password");
memset(&creds, 0, sizeof(krb5_creds));
krbret = krb5_get_init_creds_password(pam_context, &creds, princ,
pass, NULL, pamh, 0, "kadmin/changepw", &opts);
if (krbret != 0) {
PAM_LOG("Error krb5_get_init_creds_password(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_AUTH_ERR;
goto cleanup2;
}
PAM_LOG("Credentials established");
for (;;) {
retval = pam_get_authtok(pamh,
PAM_AUTHTOK, (const char **)&pass, NEW_PASSWORD_PROMPT);
if (retval != PAM_TRY_AGAIN)
break;
pam_error(pamh, "Mismatch; try again, EOF to quit.");
}
if (retval != PAM_SUCCESS)
goto cleanup;
PAM_LOG("Got new password");
if ((passdup = strdup(pass)) == NULL) {
retval = PAM_BUF_ERR;
goto cleanup;
}
krbret = krb5_change_password(pam_context, &creds, passdup,
&result_code, &result_code_string, &result_string);
free(passdup);
if (krbret != 0) {
PAM_LOG("Error krb5_change_password(): %s",
krb5_get_err_text(pam_context, krbret));
retval = PAM_AUTHTOK_ERR;
goto cleanup;
}
if (result_code) {
PAM_LOG("Error krb5_change_password(): (result_code)");
retval = PAM_AUTHTOK_ERR;
goto cleanup;
}
PAM_LOG("Password changed");
if (result_string.data)
free(result_string.data);
if (result_code_string.data)
free(result_code_string.data);
cleanup:
krb5_free_cred_contents(pam_context, &creds);
PAM_LOG("Done cleanup");
cleanup2:
krb5_free_principal(pam_context, princ);
PAM_LOG("Done cleanup2");
cleanup3:
if (princ_name)
free(princ_name);
krb5_free_context(pam_context);
PAM_LOG("Done cleanup3");
return (retval);
}
PAM_MODULE_ENTRY("pam_krb5");
static int
verify_krb_v5_tgt(krb5_context context, krb5_ccache ccache,
char *pam_service, int debug)
{
krb5_error_code retval;
krb5_principal princ;
krb5_keyblock *keyblock;
krb5_data packet;
krb5_auth_context auth_context;
char phost[BUFSIZ];
const char *services[3], **service;
packet.data = 0;
services[0] = "host";
services[1] = pam_service;
services[2] = NULL;
keyblock = 0;
retval = -1;
for (service = &services[0]; *service != NULL; service++) {
retval = krb5_sname_to_principal(context, NULL, *service,
KRB5_NT_SRV_HST, &princ);
if (retval != 0) {
if (debug)
syslog(LOG_DEBUG,
"pam_krb5: verify_krb_v5_tgt(): %s: %s",
"krb5_sname_to_principal()",
krb5_get_err_text(context, retval));
return -1;
}
strncpy(phost, compat_princ_component(context, princ, 1),
BUFSIZ);
phost[BUFSIZ - 1] = '\0';
retval = krb5_kt_read_service_key(context, NULL, princ, 0, 0,
&keyblock);
if (retval != 0) {
krb5_free_principal(context, princ);
princ = NULL;
continue;
}
break;
}
if (retval != 0) {
if (debug)
syslog(LOG_DEBUG,
"pam_krb5: verify_krb_v5_tgt(): %s: %s",
"krb5_kt_read_service_key()",
krb5_get_err_text(context, retval));
retval = 0;
goto cleanup;
}
if (keyblock)
krb5_free_keyblock(context, keyblock);
auth_context = NULL;
retval = krb5_mk_req(context, &auth_context, 0, (char *)*service, phost,
NULL, ccache, &packet);
if (auth_context) {
krb5_auth_con_free(context, auth_context);
auth_context = NULL;
}
if (retval) {
if (debug)
syslog(LOG_DEBUG,
"pam_krb5: verify_krb_v5_tgt(): %s: %s",
"krb5_mk_req()",
krb5_get_err_text(context, retval));
retval = -1;
goto cleanup;
}
retval = krb5_rd_req(context, &auth_context, &packet, princ, NULL,
NULL, NULL);
if (retval) {
if (debug)
syslog(LOG_DEBUG,
"pam_krb5: verify_krb_v5_tgt(): %s: %s",
"krb5_rd_req()",
krb5_get_err_text(context, retval));
retval = -1;
}
else
retval = 1;
cleanup:
if (packet.data)
compat_free_data_contents(context, &packet);
krb5_free_principal(context, princ);
return retval;
}
#ifdef COMPAT_HEIMDAL
#ifdef COMPAT_MIT
#error This cannot be MIT and Heimdal compatible!
#endif
#endif
#ifndef COMPAT_HEIMDAL
#ifndef COMPAT_MIT
#error One of COMPAT_MIT and COMPAT_HEIMDAL must be specified!
#endif
#endif
#ifdef COMPAT_HEIMDAL
static const char *
compat_princ_component(krb5_context context __unused, krb5_principal princ, int n)
{
return princ->name.name_string.val[n];
}
static void
compat_free_data_contents(krb5_context context __unused, krb5_data * data)
{
krb5_xfree(data->data);
}
#endif
#ifdef COMPAT_MIT
static const char *
compat_princ_component(krb5_context context, krb5_principal princ, int n)
{
return krb5_princ_component(context, princ, n)->data;
}
static void
compat_free_data_contents(krb5_context context, krb5_data * data)
{
krb5_free_data_contents(context, data);
}
#endif