#include "ldap_headers.h"
static void
ldap_cleanup(
pam_handle_t *pamh,
void *data,
int pam_status)
{
free((ldap_authtok_data *)data);
}
static void
warn_user_passwd_will_expire(
pam_handle_t *pamh,
int sec_until_expired)
{
char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
int days = 0, hours = 0;
int seconds_d = 0, seconds_h = 0;
days = sec_until_expired / 86400;
seconds_d = sec_until_expired % 86400;
hours = (days * 24) + seconds_d / 3600;
seconds_h = seconds_d % 3600;
if (sec_until_expired <= (86400 * 2)) {
if (seconds_d <= 3600 && days == 0)
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password will expire within one hour."));
else
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password will expire in %d hours."),
(seconds_h == 0) ? hours : hours + 1);
} else {
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password will expire in %d days."),
(seconds_d == 0) ? days : days + 1);
}
(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages, NULL);
}
static void
display_acct_unlock_time(pam_handle_t *pamh, int sec_b4_unlock)
{
char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
int days = 0, hours = 0;
int seconds_d = 0, seconds_h = 0;
if (sec_b4_unlock == -1) {
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your account is locked, please contact administrator."));
(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1,
messages, NULL);
return;
}
days = sec_b4_unlock / 86400;
seconds_d = sec_b4_unlock % 86400;
hours = (days * 24) + seconds_d / 3600;
seconds_h = seconds_d % 3600;
if (sec_b4_unlock <= (86400 * 2)) {
if (seconds_d <= 3600 && days == 0)
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your account is locked and will be unlocked"
" within one hour."));
else
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your account is locked and will be unlocked"
" in %d hours."),
(seconds_h == 0) ? hours : hours + 1);
} else {
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your account is locked and will be unlocked"
" in %d days."),
(seconds_d == 0) ? days : days + 1);
}
(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages, NULL);
}
static void
warn_user_passwd_expired(pam_handle_t *pamh, int grace)
{
char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
if (grace)
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password has expired. "
"Number of grace logins allowed are %d."),
grace);
else
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password has expired."));
(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages, NULL);
}
static void
display_passwd_reset_msg(pam_handle_t *pamh)
{
char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
(void) snprintf(messages[0], sizeof (messages[0]),
dgettext(TEXT_DOMAIN,
"Your password has been reset by administrator."));
(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages, NULL);
}
static int
get_account_mgmt(const char *user, int *seconds, int *grace)
{
int rc = PAM_AUTH_ERR;
AcctUsableResponse_t acctResp;
(void *)memset((void*)&acctResp, 0, sizeof (acctResp));
if ((rc = __ns_ldap_getAcctMgmt(user, &acctResp))
!= NS_LDAP_SUCCESS) {
syslog(LOG_DEBUG,
"__ns_ldap_getAcctMgmt() failed for %s with error %d",
user, rc);
return (PAM_AUTH_ERR);
}
if (acctResp.choice == 0) {
*seconds =
acctResp.AcctUsableResp.seconds_before_expiry;
return (PAM_SUCCESS);
} else if (acctResp.choice == 1) {
if (acctResp.AcctUsableResp.more_info.inactive)
return (PAM_ACCT_EXPIRED);
if (acctResp.AcctUsableResp.more_info.reset)
return (PAM_NEW_AUTHTOK_REQD);
if (acctResp.AcctUsableResp.more_info.expired) {
*grace =
acctResp.AcctUsableResp.more_info.rem_grace;
return (PAM_AUTHTOK_EXPIRED);
}
if (acctResp.AcctUsableResp.more_info.sec_b4_unlock) {
*seconds =
acctResp.AcctUsableResp.more_info.sec_b4_unlock;
return (PAM_MAXTRIES);
}
}
return (PAM_AUTH_ERR);
}
int
pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
const char *user = NULL;
int result = PAM_AUTH_ERR;
int debug = 0;
int i;
const char *password = NULL;
ns_cred_t *credp = NULL;
int nowarn = 0;
int seconds = 0, grace = 0;
ldap_authtok_data *status;
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "debug") == 0)
debug = 1;
else if (strcasecmp(argv[i], "nowarn") == 0) {
nowarn = 1;
flags = flags | PAM_SILENT;
}
else
syslog(LOG_DEBUG,
"pam_ldap pam_sm_acct_mgmt: "
"illegal option %s",
argv[i]);
}
if ((result = pam_get_item(pamh, PAM_USER, (const void **)&user)) !=
PAM_SUCCESS) {
goto out;
}
if (debug)
syslog(LOG_DEBUG,
"ldap pam_sm_acct_mgmt(%s), flags = %x %s",
(user)?user:"no-user", flags,
(nowarn)? ", nowarn": "");
if (user == NULL) {
result = PAM_USER_UNKNOWN;
goto out;
}
result = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&password);
if (password == NULL) {
if (debug)
syslog(LOG_DEBUG, "ldap pam_sm_acct_mgmt: "
"no password for user %s", user);
result = get_account_mgmt(user, &seconds, &grace);
} else {
result = authenticate(&credp, user,
password, &seconds);
}
if (result == PAM_MAXTRIES) {
if (!(flags & PAM_SILENT))
display_acct_unlock_time(pamh, seconds);
result = PAM_PERM_DENIED;
} else if (result == PAM_ACCT_EXPIRED)
result = PAM_ACCT_EXPIRED;
else if (result == PAM_AUTHTOK_EXPIRED) {
if (!(flags & PAM_SILENT))
warn_user_passwd_expired(pamh, grace);
if (grace > 0)
result = PAM_SUCCESS;
else
result = PAM_AUTHTOK_EXPIRED;
} else if (result == PAM_NEW_AUTHTOK_REQD) {
if (!(flags & PAM_SILENT))
display_passwd_reset_msg(pamh);
result = PAM_NEW_AUTHTOK_REQD;
} else if (result == PAM_SUCCESS) {
if (!(flags & PAM_SILENT) &&
seconds > 0)
warn_user_passwd_will_expire(pamh,
seconds);
}
out:
if (credp != NULL)
(void) __ns_ldap_freeCred(&credp);
if (result != PAM_SUCCESS) {
int pam_res;
ldap_authtok_data *authtok_data;
pam_res = pam_get_data(
pamh, LDAP_AUTHTOK_DATA, (const void **)&authtok_data);
if ((status = (ldap_authtok_data *)calloc
(1, sizeof (ldap_authtok_data))) == NULL) {
return (PAM_BUF_ERR);
}
if (pam_res == PAM_SUCCESS)
(void) memcpy(status, authtok_data,
sizeof (ldap_authtok_data));
status->age_status = result;
if (pam_set_data(pamh, LDAP_AUTHTOK_DATA, status, ldap_cleanup)
!= PAM_SUCCESS) {
free(status);
return (PAM_SERVICE_ERR);
}
}
return (result);
}