#include "k5-int.h"
#include <kdb.h>
#include <ctype.h>
#include <pwd.h>
#include <syslog.h>
#include "server_internal.h"
#include <adm_proto.h>
kadm5_ret_t
init_pwqual(kadm5_server_handle_t handle)
{
krb5_error_code ret;
pwqual_handle *list;
const char *dict_file = NULL;
ret = k5_plugin_register(handle->context, PLUGIN_INTERFACE_PWQUAL,
"dict", pwqual_dict_initvt);
if (ret != 0)
return ret;
ret = k5_plugin_register(handle->context, PLUGIN_INTERFACE_PWQUAL,
"empty", pwqual_empty_initvt);
if (ret != 0)
return ret;
ret = k5_plugin_register(handle->context, PLUGIN_INTERFACE_PWQUAL,
"hesiod", pwqual_hesiod_initvt);
if (ret != 0)
return ret;
ret = k5_plugin_register(handle->context, PLUGIN_INTERFACE_PWQUAL,
"princ", pwqual_princ_initvt);
if (ret != 0)
return ret;
if (handle->params.mask & KADM5_CONFIG_DICT_FILE)
dict_file = handle->params.dict_file;
ret = k5_pwqual_load(handle->context, dict_file, &list);
if (ret != 0)
return ret;
handle->qual_handles = list;
return 0;
}
static kadm5_ret_t
check_against_policy(kadm5_server_handle_t handle, const char *password,
kadm5_policy_ent_t pol)
{
int hasupper = 0, haslower = 0, hasdigit = 0, haspunct = 0, hasspec = 0;
int c, nclasses;
if (strlen(password) < (size_t)pol->pw_min_length)
return KADM5_PASS_Q_TOOSHORT;
while ((c = (unsigned char)*password++) != '\0') {
if (islower(c))
haslower = 1;
else if (isupper(c))
hasupper = 1;
else if (isdigit(c))
hasdigit = 1;
else if (ispunct(c))
haspunct = 1;
else
hasspec = 1;
}
nclasses = hasupper + haslower + hasdigit + haspunct + hasspec;
if (nclasses < pol->pw_min_classes)
return KADM5_PASS_Q_CLASS;
return KADM5_OK;
}
kadm5_ret_t
passwd_check(kadm5_server_handle_t handle, const char *password,
kadm5_policy_ent_t policy, krb5_principal princ)
{
krb5_error_code ret;
pwqual_handle *h;
const char *polname = (policy == NULL) ? NULL : policy->policy;
if (policy != NULL) {
ret = check_against_policy(handle, password, policy);
if (ret != 0)
return ret;
}
for (h = handle->qual_handles; *h != NULL; h++) {
ret = k5_pwqual_check(handle->context, *h, password, polname, princ);
if (ret != 0) {
const char *e = krb5_get_error_message(handle->context, ret);
const char *modname = k5_pwqual_name(handle->context, *h);
char *princname;
if (krb5_unparse_name(handle->context, princ, &princname) != 0)
princname = NULL;
krb5_klog_syslog(LOG_ERR,
_("password quality module %s rejected password "
"for %s: %s"), modname,
princname ? princname : "(can't unparse)", e);
krb5_free_error_message(handle->context, e);
free(princname);
return ret;
}
}
return 0;
}
void
destroy_pwqual(kadm5_server_handle_t handle)
{
k5_pwqual_free_handles(handle->context, handle->qual_handles);
handle->qual_handles = NULL;
}
kadm5_ret_t
kadm5_get_privs(void *server_handle, long *privs)
{
CHECK_HANDLE(server_handle);
*privs = ~0;
return KADM5_OK;
}