2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/wait.h>
2N/A#include <sys/stat.h>
2N/A#include <fcntl.h>
2N/A#include <stdlib.h>
2N/A#include <security/pam_appl.h>
2N/A#include <security/pam_modules.h>
2N/A#include <security/pam_impl.h>
2N/A#include <syslog.h>
2N/A#include <pwd.h>
2N/A#include <shadow.h>
2N/A#include <lastlog.h>
2N/A#include <ctype.h>
2N/A#include <unistd.h>
2N/A#include <stdlib.h>
2N/A#include <stdio.h>
2N/A#include <libintl.h>
2N/A#include <signal.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include <errno.h>
2N/A#include <time.h>
2N/A#include <string.h>
2N/A#include <crypt.h>
2N/A#include <assert.h>
2N/A#include <deflt.h>
2N/A#include <libintl.h>
2N/A#include <passwdutil.h>
2N/A
2N/A#define LASTLOG "/var/adm/lastlog"
2N/A#define LOGINADMIN "/etc/default/login"
2N/A#define UNIX_AUTH_DATA "SUNW-UNIX-AUTH-DATA"
2N/A#define UNIX_AUTHTOK_DATA "SUNW-UNIX-AUTHTOK-DATA"
2N/A
2N/A/*
2N/A * Function Declarations
2N/A */
2N/Aextern void setusershell();
2N/Aextern int _nfssys(int, void *);
2N/A
2N/Atypedef struct _unix_authtok_data_ {
2N/A int age_status;
2N/A}unix_authtok_data;
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Aunix_cleanup(
2N/A pam_handle_t *pamh,
2N/A void *data,
2N/A int pam_status)
2N/A{
2N/A free((unix_authtok_data *)data);
2N/A}
2N/A
2N/A/*
2N/A * check_for_login_inactivity - Check for login inactivity
2N/A *
2N/A */
2N/A
2N/Astatic int
2N/Acheck_for_login_inactivity(
2N/A uid_t pw_uid,
2N/A struct spwd *shpwd)
2N/A{
2N/A int fdl;
2N/A struct lastlog ll;
2N/A int retval;
2N/A offset_t offset;
2N/A
2N/A offset = (offset_t)pw_uid * (offset_t)sizeof (struct lastlog);
2N/A
2N/A if ((fdl = open(LASTLOG, O_RDWR|O_CREAT, 0444)) >= 0) {
2N/A /*
2N/A * Read the last login (ll) time
2N/A */
2N/A if (llseek(fdl, offset, SEEK_SET) != offset) {
2N/A __pam_log(LOG_AUTH | LOG_ERR,
2N/A "pam_unix_acct: pam_sm_acct_mgmt: "
2N/A "can't obtain last login info on uid %d "
2N/A "(uid too large)", pw_uid);
2N/A (void) close(fdl);
2N/A return (0);
2N/A }
2N/A
2N/A retval = read(fdl, (char *)&ll, sizeof (ll));
2N/A
2N/A /* Check for login inactivity */
2N/A
2N/A if ((shpwd->sp_inact > 0) && (retval == sizeof (ll)) &&
2N/A ll.ll_time) {
2N/A /*
2N/A * account inactive too long.
2N/A * and no update password set
2N/A * and no last pwd change date in shadow file
2N/A * and last pwd change more than inactive time
2N/A * then account inactive too long and no access.
2N/A */
2N/A if (((time_t)((ll.ll_time / DAY) + shpwd->sp_inact)
2N/A < DAY_NOW) &&
2N/A (shpwd->sp_lstchg != 0) &&
2N/A (shpwd->sp_lstchg != -1) &&
2N/A ((shpwd->sp_lstchg + shpwd->sp_inact) < DAY_NOW)) {
2N/A /*
2N/A * Account inactive for too long
2N/A */
2N/A (void) close(fdl);
2N/A return (1);
2N/A }
2N/A }
2N/A
2N/A (void) close(fdl);
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * new_password_check()
2N/A *
2N/A * check to see if the user needs to change their password
2N/A */
2N/A
2N/Astatic int
2N/Anew_password_check(shpwd, flags)
2N/A struct spwd *shpwd;
2N/A int flags;
2N/A{
2N/A time_t now = DAY_NOW;
2N/A
2N/A /*
2N/A * We want to make sure that we change the password only if
2N/A * passwords are required for the system, the user does not
2N/A * have a password, AND the user's NULL password can be changed
2N/A * according to its password aging information
2N/A */
2N/A
2N/A if ((flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) {
2N/A if (shpwd->sp_pwdp[0] == '\0') {
2N/A if (((shpwd->sp_max == -1) ||
2N/A ((time_t)shpwd->sp_lstchg > now) ||
2N/A ((now >= (time_t)(shpwd->sp_lstchg +
2N/A shpwd->sp_min)) &&
2N/A (shpwd->sp_max >= shpwd->sp_min)))) {
2N/A return (PAM_NEW_AUTHTOK_REQD);
2N/A }
2N/A }
2N/A }
2N/A return (PAM_SUCCESS);
2N/A}
2N/A
2N/A/*
2N/A * perform_passwd_aging_check
2N/A * - Check for password exipration.
2N/A */
2N/Astatic int
2N/Aperform_passwd_aging_check(
2N/A pam_handle_t *pamh,
2N/A struct spwd *shpwd,
2N/A int flags)
2N/A{
2N/A time_t now = DAY_NOW;
2N/A int idledays = -1;
2N/A char *ptr;
2N/A char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
2N/A void *defp;
2N/A
2N/A
2N/A if ((defp = defopen_r(LOGINADMIN)) != NULL) {
2N/A if ((ptr = defread_r("IDLEWEEKS=", defp)) != NULL)
2N/A idledays = 7 * atoi(ptr);
2N/A defclose_r(defp);
2N/A }
2N/A
2N/A /*
2N/A * if (sp_lstchg == 0), the administrator has forced the
2N/A * user to change his/her passwd
2N/A */
2N/A if (shpwd->sp_lstchg == 0)
2N/A return (PAM_NEW_AUTHTOK_REQD);
2N/A
2N/A /* If password aging is disabled (or min>max), all is well */
2N/A if (shpwd->sp_max < 0 || shpwd->sp_max < shpwd->sp_min)
2N/A return (PAM_SUCCESS);
2N/A
2N/A /* Password aging is enabled. See if the password has aged */
2N/A if (now < (time_t)(shpwd->sp_lstchg + shpwd->sp_max))
2N/A return (PAM_SUCCESS);
2N/A
2N/A /* Password has aged. Has it aged more than idledays ? */
2N/A if (idledays < 0) /* IDLEWEEKS not configured */
2N/A return (PAM_NEW_AUTHTOK_REQD);
2N/A
2N/A /* idledays is configured */
2N/A if (idledays > 0 && (now < (time_t)(shpwd->sp_lstchg + idledays)))
2N/A return (PAM_NEW_AUTHTOK_REQD);
2N/A
2N/A /* password has aged more that allowed for by IDLEWEEKS */
2N/A if (!(flags & PAM_SILENT)) {
2N/A (void) strlcpy(messages[0], dgettext(TEXT_DOMAIN,
2N/A "Your password has been expired for too long."),
2N/A sizeof (messages[0]));
2N/A (void) strlcpy(messages[1], dgettext(TEXT_DOMAIN,
2N/A "Please contact the system administrator."),
2N/A sizeof (messages[0]));
2N/A (void) __pam_display_msg(pamh, PAM_ERROR_MSG, 2, messages,
2N/A NULL);
2N/A }
2N/A return (PAM_AUTHTOK_EXPIRED);
2N/A}
2N/A
2N/A/*
2N/A * warn_user_passwd_will_expire - warn the user when the password will
2N/A * expire.
2N/A */
2N/A
2N/Astatic void
2N/Awarn_user_passwd_will_expire(
2N/A pam_handle_t *pamh,
2N/A struct spwd shpwd)
2N/A{
2N/A time_t now = DAY_NOW;
2N/A char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
2N/A time_t days;
2N/A
2N/A
2N/A if ((shpwd.sp_warn > 0) && (shpwd.sp_max > 0) &&
2N/A (now + shpwd.sp_warn) >= (time_t)(shpwd.sp_lstchg + shpwd.sp_max)) {
2N/A days = (time_t)(shpwd.sp_lstchg + shpwd.sp_max) - now;
2N/A if (days <= 0)
2N/A (void) snprintf(messages[0],
2N/A sizeof (messages[0]),
2N/A dgettext(TEXT_DOMAIN,
2N/A "Your password will expire within 24 hours."));
2N/A else if (days == 1)
2N/A (void) snprintf(messages[0],
2N/A sizeof (messages[0]),
2N/A dgettext(TEXT_DOMAIN,
2N/A "Your password will expire in 1 day."));
2N/A else
2N/A (void) snprintf(messages[0],
2N/A sizeof (messages[0]),
2N/A dgettext(TEXT_DOMAIN,
2N/A "Your password will expire in %d days."),
2N/A (int)days);
2N/A
2N/A (void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages,
2N/A NULL);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * pam_sm_acct_mgmt - main account managment routine.
2N/A * Returns: module error or specific error on failure
2N/A */
2N/A
2N/Aint
2N/Apam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
2N/A{
2N/A uid_t pw_uid;
2N/A char *repository_name = NULL;
2N/A char *user;
2N/A attrlist attr_pw[3];
2N/A attrlist attr_spw[7];
2N/A pwu_repository_t *pwu_rep = PWU_DEFAULT_REP;
2N/A pwu_repository_t *auth_rep = NULL;
2N/A int error = PAM_ACCT_EXPIRED;
2N/A int result;
2N/A int i;
2N/A int debug = 0;
2N/A int server_policy = 0;
2N/A unix_authtok_data *status;
2N/A struct spwd shpwd = {NULL, NULL,
2N/A -1, -1, -1, -1, -1, -1, 0};
2N/A
2N/A for (i = 0; i < argc; i++) {
2N/A if (strcasecmp(argv[i], "debug") == 0)
2N/A debug = 1;
2N/A else if (strcasecmp(argv[i], "server_policy") == 0)
2N/A server_policy = 1;
2N/A else if (strcasecmp(argv[i], "nowarn") == 0) {
2N/A flags = flags | PAM_SILENT;
2N/A } else {
2N/A __pam_log(LOG_AUTH | LOG_ERR,
2N/A "ACCOUNT:pam_sm_acct_mgmt: illegal option %s",
2N/A argv[i]);
2N/A }
2N/A }
2N/A
2N/A if (debug)
2N/A __pam_log(LOG_AUTH | LOG_DEBUG,
2N/A "pam_unix_account: entering pam_sm_acct_mgmt()");
2N/A
2N/A if ((error = pam_get_item(pamh, PAM_USER, (void **)&user))
2N/A != PAM_SUCCESS)
2N/A goto out;
2N/A
2N/A if (user == NULL) {
2N/A error = PAM_USER_UNKNOWN;
2N/A goto out;
2N/A } else
2N/A shpwd.sp_namp = user;
2N/A
2N/A if ((error = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep))
2N/A != PAM_SUCCESS)
2N/A goto out;
2N/A
2N/A if (auth_rep == NULL) {
2N/A pwu_rep = PWU_DEFAULT_REP;
2N/A } else {
2N/A if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL) {
2N/A error = PAM_BUF_ERR;
2N/A goto out;
2N/A }
2N/A pwu_rep->type = auth_rep->type;
2N/A pwu_rep->scope = auth_rep->scope;
2N/A pwu_rep->scope_len = auth_rep->scope_len;
2N/A }
2N/A
2N/A /*
2N/A * First get the password information
2N/A */
2N/A attr_pw[0].type = ATTR_REP_NAME; attr_pw[0].next = &attr_pw[1];
2N/A attr_pw[1].type = ATTR_UID; attr_pw[1].next = &attr_pw[2];
2N/A attr_pw[2].type = ATTR_PASSWD; attr_pw[2].next = NULL;
2N/A result = __get_authtoken_attr(user, pwu_rep, attr_pw);
2N/A
2N/A if (result == PWU_NOT_FOUND) {
2N/A error = PAM_USER_UNKNOWN;
2N/A goto out;
2N/A } else if (result == PWU_DENIED) {
2N/A error = PAM_PERM_DENIED;
2N/A goto out;
2N/A } else if (result == PWU_NOMEM) {
2N/A error = PAM_BUF_ERR;
2N/A goto out;
2N/A } else if (result != PWU_SUCCESS) {
2N/A error = PAM_SERVICE_ERR;
2N/A goto out;
2N/A } else {
2N/A repository_name = attr_pw[0].data.val_s;
2N/A pw_uid = attr_pw[1].data.val_i;
2N/A shpwd.sp_pwdp = attr_pw[2].data.val_s;
2N/A }
2N/A
2N/A /*
2N/A * if repository is not files|nis, and user wants server_policy,
2N/A * we don't care about aging and hence return PAM_IGNORE
2N/A */
2N/A if (server_policy &&
2N/A strcmp(repository_name, "files") != 0 &&
2N/A strcmp(repository_name, "nis") != 0) {
2N/A error = PAM_IGNORE;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Now get the aging information
2N/A */
2N/A attr_spw[0].type = ATTR_LSTCHG; attr_spw[0].next = &attr_spw[1];
2N/A attr_spw[1].type = ATTR_MIN; attr_spw[1].next = &attr_spw[2];
2N/A attr_spw[2].type = ATTR_MAX; attr_spw[2].next = &attr_spw[3];
2N/A attr_spw[3].type = ATTR_WARN; attr_spw[3].next = &attr_spw[4];
2N/A attr_spw[4].type = ATTR_INACT; attr_spw[4].next = &attr_spw[5];
2N/A attr_spw[5].type = ATTR_EXPIRE; attr_spw[5].next = &attr_spw[6];
2N/A attr_spw[6].type = ATTR_FLAG; attr_spw[6].next = NULL;
2N/A
2N/A result = __get_authtoken_attr(user, pwu_rep, attr_spw);
2N/A if (result == PWU_SUCCESS) {
2N/A shpwd.sp_lstchg = attr_spw[0].data.val_i;
2N/A shpwd.sp_min = attr_spw[1].data.val_i;
2N/A shpwd.sp_max = attr_spw[2].data.val_i;
2N/A shpwd.sp_warn = attr_spw[3].data.val_i;
2N/A shpwd.sp_inact = attr_spw[4].data.val_i;
2N/A shpwd.sp_expire = attr_spw[5].data.val_i;
2N/A shpwd.sp_flag = attr_spw[6].data.val_i;
2N/A }
2N/A
2N/A if (debug) {
2N/A char *pw = "Unix PW";
2N/A
2N/A if (shpwd.sp_pwdp == NULL) {
2N/A pw = "NULL";
2N/A } else if (strncmp(shpwd.sp_pwdp, LOCKSTRING,
2N/A sizeof (LOCKSTRING) - 1) == 0) {
2N/A pw = LOCKSTRING;
2N/A } else if (strcmp(shpwd.sp_pwdp, NOPWDRTR) == 0) {
2N/A pw = NOPWDRTR;
2N/A } else if (strcmp(shpwd.sp_pwdp, UNINITPW) == 0) {
2N/A pw = UNINITPW;
2N/A }
2N/A
2N/A if (result == PWU_DENIED) {
2N/A __pam_log(LOG_AUTH | LOG_DEBUG,
2N/A "pam_unix_account: %s: permission denied "
2N/A "to access password aging information. "
2N/A "Using defaults.", user);
2N/A }
2N/A
2N/A __pam_log(LOG_AUTH | LOG_DEBUG,
2N/A "%s Policy:Unix, pw=%s, lstchg=%d, min=%d, max=%d, "
2N/A "warn=%d, inact=%d, expire=%d",
2N/A user, pw, shpwd.sp_lstchg, shpwd.sp_min, shpwd.sp_max,
2N/A shpwd.sp_warn, shpwd.sp_inact, shpwd.sp_expire);
2N/A }
2N/A
2N/A if (pwu_rep != PWU_DEFAULT_REP) {
2N/A free(pwu_rep);
2N/A pwu_rep = PWU_DEFAULT_REP;
2N/A }
2N/A
2N/A if (result == PWU_NOT_FOUND) {
2N/A error = PAM_USER_UNKNOWN;
2N/A goto out;
2N/A } else if (result == PWU_NOMEM) {
2N/A error = PAM_BUF_ERR;
2N/A goto out;
2N/A } else if (result != PWU_SUCCESS && result != PWU_DENIED) {
2N/A error = PAM_SERVICE_ERR;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Check for locked account
2N/A */
2N/A if (shpwd.sp_pwdp != NULL &&
2N/A strncmp(shpwd.sp_pwdp, LOCKSTRING, sizeof (LOCKSTRING) - 1) == 0) {
2N/A char *service;
2N/A char *rhost = NULL;
2N/A
2N/A (void) pam_get_item(pamh, PAM_SERVICE, (void **)&service);
2N/A (void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost);
2N/A __pam_log(LOG_AUTH | LOG_NOTICE,
2N/A "pam_unix_account: %s attempting to validate locked "
2N/A "account %s from %s",
2N/A service, user,
2N/A (rhost != NULL && *rhost != '\0') ? rhost : "local host");
2N/A error = PAM_PERM_DENIED;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Check for NULL password and, if so, see if such is allowed
2N/A */
2N/A if (shpwd.sp_pwdp[0] == '\0' &&
2N/A (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) {
2N/A char *service;
2N/A char *rhost = NULL;
2N/A
2N/A (void) pam_get_item(pamh, PAM_SERVICE, (void **)&service);
2N/A (void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost);
2N/A
2N/A __pam_log(LOG_AUTH | LOG_NOTICE,
2N/A "pam_unix_account: %s: empty password not allowed for "
2N/A "account %s from %s", service, user,
2N/A (rhost != NULL && *rhost != '\0') ? rhost : "local host");
2N/A error = PAM_PERM_DENIED;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Check for account expiration
2N/A */
2N/A if (shpwd.sp_expire > 0 &&
2N/A (time_t)shpwd.sp_expire < DAY_NOW) {
2N/A error = PAM_ACCT_EXPIRED;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Check for excessive login account inactivity
2N/A */
2N/A if (check_for_login_inactivity(pw_uid, &shpwd)) {
2N/A error = PAM_PERM_DENIED;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Check to see if the user needs to change their password
2N/A */
2N/A if (error = new_password_check(&shpwd, flags)) {
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Check to make sure password aging information is okay
2N/A */
2N/A if ((error = perform_passwd_aging_check(pamh, &shpwd, flags))
2N/A != PAM_SUCCESS) {
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Finally, warn the user if their password is about to expire.
2N/A */
2N/A if (!(flags & PAM_SILENT)) {
2N/A warn_user_passwd_will_expire(pamh, shpwd);
2N/A }
2N/A
2N/A /*
2N/A * All done, return Success
2N/A */
2N/A error = PAM_SUCCESS;
2N/A
2N/Aout:
2N/A
2N/A {
2N/A int pam_res;
2N/A unix_authtok_data *authtok_data;
2N/A
2N/A if (debug) {
2N/A __pam_log(LOG_AUTH | LOG_DEBUG,
2N/A "pam_unix_account: %s: %s",
2N/A (user == NULL)?"NULL":user,
2N/A pam_strerror(pamh, error));
2N/A }
2N/A
2N/A if (repository_name)
2N/A free(repository_name);
2N/A if (pwu_rep != PWU_DEFAULT_REP)
2N/A free(pwu_rep);
2N/A if (shpwd.sp_pwdp) {
2N/A (void) memset(shpwd.sp_pwdp, 0, strlen(shpwd.sp_pwdp));
2N/A free(shpwd.sp_pwdp);
2N/A }
2N/A
2N/A /* store the password aging status in the pam handle */
2N/A pam_res = pam_get_data(pamh, UNIX_AUTHTOK_DATA,
2N/A (const void **)&authtok_data);
2N/A
2N/A if ((status = (unix_authtok_data *)calloc(1,
2N/A sizeof (unix_authtok_data))) == NULL) {
2N/A return (PAM_BUF_ERR);
2N/A }
2N/A
2N/A if (pam_res == PAM_SUCCESS)
2N/A (void) memcpy(status, authtok_data,
2N/A sizeof (unix_authtok_data));
2N/A
2N/A status->age_status = error;
2N/A if (pam_set_data(pamh, UNIX_AUTHTOK_DATA, status, unix_cleanup)
2N/A != PAM_SUCCESS) {
2N/A free(status);
2N/A return (PAM_SERVICE_ERR);
2N/A }
2N/A }
2N/A
2N/A return (error);
2N/A}