unix_auth.c revision 66e150d7d3c0cb2de3c45c74612784ffd3e73de6
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 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A#include <stdlib.h>
2N/A#include <pwd.h>
2N/A#include <shadow.h>
2N/A#include <syslog.h>
2N/A#include <errno.h>
2N/A#include <string.h>
2N/A#include <crypt.h>
2N/A#include <unistd.h>
2N/A#include <user_attr.h>
2N/A#include <auth_attr.h>
2N/A#include <userdefs.h>
2N/A#include <deflt.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/param.h>
2N/A#include <stdarg.h>
2N/A
2N/A#include <security/pam_appl.h>
2N/A#include <security/pam_modules.h>
2N/A#include <security/pam_impl.h>
2N/A
2N/A#include <libintl.h>
2N/A
2N/A#include <passwdutil.h>
2N/A
2N/A#define LOGINADMIN "/etc/default/login"
2N/A#define MAXTRYS 5
2N/A
2N/A/*PRINTFLIKE2*/
2N/Avoid
2N/Aerror(pam_handle_t *pamh, char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A char messages[1][PAM_MAX_MSG_SIZE];
2N/A
2N/A va_start(ap, fmt);
2N/A (void) vsnprintf(messages[0], sizeof (messages[0]), fmt, ap);
2N/A (void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1, messages, NULL);
2N/A va_end(ap);
2N/A}
2N/A
2N/Astatic int
2N/Aget_max_failed(char *user)
2N/A{
2N/A char *val = NULL;
2N/A userattr_t *uattr;
2N/A int do_lock = 0;
2N/A int retval = 0;
2N/A char *p;
2N/A
2N/A if ((uattr = getusernam(user)) != NULL)
2N/A val = kva_match(uattr->attr, USERATTR_LOCK_AFTER_RETRIES_KW);
2N/A
2N/A if (val != NULL)
2N/A do_lock = (strcasecmp(val, "yes") == 0);
2N/A else if (defopen(AUTH_POLICY) == 0) {
2N/A int flags;
2N/A flags = defcntl(DC_GETFLAGS, 0);
2N/A TURNOFF(flags, DC_CASE);
2N/A (void) defcntl(DC_SETFLAGS, flags);
2N/A if ((p = defread("LOCK_AFTER_RETRIES=")) != NULL)
2N/A do_lock = (strcasecmp(p, "yes") == 0);
2N/A (void) defopen(NULL);
2N/A }
2N/A
2N/A if (uattr != NULL)
2N/A free_userattr(uattr);
2N/A
2N/A if (do_lock) {
2N/A retval = MAXTRYS;
2N/A if (defopen(LOGINADMIN) == 0) {
2N/A if ((p = defread("RETRIES=")) != NULL)
2N/A retval = atoi(p);
2N/A (void) defopen(NULL);
2N/A }
2N/A }
2N/A
2N/A return (retval);
2N/A}
2N/A
2N/Astatic void
2N/Adisplay_warning(pam_handle_t *pamh, int failures, char *homedir)
2N/A{
2N/A char hushpath[MAXPATHLEN];
2N/A struct stat buf;
2N/A
2N/A (void) snprintf(hushpath, sizeof (hushpath), "%s/.hushlogin", homedir);
2N/A if (stat(hushpath, &buf) == 0)
2N/A return;
2N/A
2N/A if (failures == 1)
2N/A error(pamh, "Warning: 1 failed login attempt since last "
2N/A "successful login.");
2N/A else if (failures < FAILCOUNT_MASK)
2N/A error(pamh, "Warning: %d failed login attempts since last "
2N/A "successful login.", failures);
2N/A else
2N/A error(pamh, "Warning: at least %d failed login attempts since "
2N/A "last successful login.", failures);
2N/A}
2N/A
2N/A/*
2N/A * int pam_sm_authenticate(pamh, flags, arc, argv)
2N/A *
2N/A * This routine verifies that the password as stored in the
2N/A * PAM_AUTHTOK item is indeed the password that belongs to the user
2N/A * as stored in PAM_USER.
2N/A *
2N/A * This routine will not establish Secure RPC Credentials. If these
2N/A * credentials are needed to obtain the password from the NIS+ service,
2N/A * the pam_dhkeys module should be stacked before us!
2N/A */
2N/Aint
2N/Apam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
2N/A{
2N/A int i;
2N/A int debug = 0;
2N/A int nowarn = (flags & PAM_SILENT) != 0;
2N/A char messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
2N/A char *user;
2N/A char *passwd;
2N/A char *rep_passwd;
2N/A char *crypt_passwd;
2N/A char *repository_name;
2N/A struct pam_repository *auth_rep;
2N/A pwu_repository_t *pwu_rep;
2N/A attrlist attr_pw[4];
2N/A int result;
2N/A int server_policy = 0;
2N/A int old_failed_count;
2N/A char *homedir = NULL;
2N/A int dolock = 1;
2N/A
2N/A for (i = 0; i < argc; i++) {
2N/A if (strcmp(argv[i], "debug") == 0)
2N/A debug = 1;
2N/A else if (strcmp(argv[i], "nowarn") == 0)
2N/A nowarn = 1;
2N/A else if (strcmp(argv[i], "server_policy") == 0)
2N/A server_policy = 1;
2N/A else if (strcmp(argv[i], "nolock") == 0)
2N/A dolock = 0;
2N/A }
2N/A
2N/A if (debug)
2N/A syslog(LOG_DEBUG,
2N/A "pam_unix_auth: entering pam_sm_authenticate()");
2N/A
2N/A if (pam_get_item(pamh, PAM_USER, (void **)&user) != PAM_SUCCESS) {
2N/A syslog(LOG_DEBUG, "pam_unix_auth: USER not set");
2N/A return (PAM_SYSTEM_ERR);
2N/A }
2N/A
2N/A if (user == NULL || *user == '\0') {
2N/A syslog(LOG_DEBUG, "pam_unix_auth: USER NULL or empty!\n");
2N/A return (PAM_USER_UNKNOWN);
2N/A }
2N/A
2N/A if (pam_get_item(pamh, PAM_AUTHTOK, (void **)&passwd) != PAM_SUCCESS) {
2N/A syslog(LOG_DEBUG, "pam_unix_auth: AUTHTOK not set!\n");
2N/A return (PAM_SYSTEM_ERR);
2N/A }
2N/A
2N/A result = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep);
2N/A if (result == PAM_SUCCESS && auth_rep != NULL) {
2N/A if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL)
2N/A return (PAM_BUF_ERR);
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 } else {
2N/A pwu_rep = PWU_DEFAULT_REP;
2N/A }
2N/A
2N/A /*
2N/A * Get password and the name of the repository where the
2N/A * password resides.
2N/A */
2N/A attr_pw[0].type = ATTR_PASSWD; attr_pw[0].next = &attr_pw[1];
2N/A attr_pw[1].type = ATTR_REP_NAME; attr_pw[1].next = &attr_pw[2];
2N/A /*
2N/A * Also get the current number of failed logins; we use
2N/A * this later to determine whether we need to reset the count
2N/A * on a succesful authentication. We use the home-directory
2N/A * to look for .hushlogin in order to optionaly surpress the
2N/A * "failed attempts" message.
2N/A */
2N/A attr_pw[2].type = ATTR_FAILED_LOGINS; attr_pw[2].next = &attr_pw[3];
2N/A attr_pw[3].type = ATTR_HOMEDIR; attr_pw[3].next = NULL;
2N/A
2N/A result = __get_authtoken_attr(user, pwu_rep, attr_pw);
2N/A
2N/A if (pwu_rep != PWU_DEFAULT_REP)
2N/A free(pwu_rep);
2N/A
2N/A if (result == PWU_NOT_FOUND) {
2N/A syslog(LOG_DEBUG, "pam_unix_auth: user %s not found\n",
2N/A user);
2N/A return (PAM_USER_UNKNOWN);
2N/A }
2N/A
2N/A if (result == PWU_DENIED) {
2N/A syslog(LOG_DEBUG, "pam_unix_auth: failed to obtain attributes");
2N/A return (PAM_PERM_DENIED);
2N/A }
2N/A
2N/A if (result != PWU_SUCCESS)
2N/A return (PAM_SYSTEM_ERR);
2N/A
2N/A rep_passwd = attr_pw[0].data.val_s;
2N/A repository_name = attr_pw[1].data.val_s;
2N/A old_failed_count = attr_pw[2].data.val_i;
2N/A homedir = attr_pw[3].data.val_s;
2N/A
2N/A /*
2N/A * Chop off old SunOS-style password aging information.
2N/A *
2N/A * Note: old style password aging is only defined for UNIX-style
2N/A * crypt strings, hence the comma will always be at position 14.
2N/A * Note: This code is here because some other vendors might still
2N/A * support this style of password aging. If we don't remove
2N/A * the age field, no one will be able to login.
2N/A * XXX yank this code when we're certain this "compatibility"
2N/A * isn't needed anymore.
2N/A */
2N/A if (rep_passwd != NULL && rep_passwd[0] != '$' &&
2N/A strlen(rep_passwd) > 13 && rep_passwd[13] == ',')
2N/A rep_passwd[13] = '\0';
2N/A
2N/A /* Is a password check required? */
2N/A if (rep_passwd == NULL || *rep_passwd == '\0') {
2N/A if (flags & PAM_DISALLOW_NULL_AUTHTOK) {
2N/A result = PAM_AUTH_ERR;
2N/A goto out;
2N/A } else {
2N/A result = PAM_SUCCESS;
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Password check *is* required. Make sure we have a valid
2N/A * pointer in PAM_AUTHTOK
2N/A */
2N/A if (passwd == NULL) {
2N/A result = PAM_AUTH_ERR;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * "rep_passwd" holds the encrypted password.
2N/A * If, however, we detect that the password equals NOPWDRTR,
2N/A * while we've obtained it from NIS+, it
2N/A * means that the permissions on the NIS+ table are too tight
2N/A * for us to get the password without having Secure RPC
2N/A * Credentials. In that case, we syslog an error stating that
2N/A * the Secure RPC credential Module should be on the PAM stack
2N/A * before the unix_auth module. We also tell the user to go
2N/A * and inform the administrator of this error.
2N/A */
2N/A if (strcmp(repository_name, "nisplus") == 0 &&
2N/A strcmp(rep_passwd, NOPWDRTR) == 0) {
2N/A syslog(LOG_ERR, "pam_unix_auth: NIS+ permissions require that"
2N/A "the pam_dhkeys module is on the PAM stack before "
2N/A "pam_unix_auth");
2N/A if (nowarn == 0) {
2N/A (void) snprintf(messages[0], sizeof (messages[0]),
2N/A dgettext(TEXT_DOMAIN,
2N/A "NIS+ permissions are too tight. "
2N/A "Please inform your administrator."));
2N/A (void) __pam_display_msg(pamh, PAM_ERROR_MSG, 1,
2N/A messages, NULL);
2N/A }
2N/A result = PAM_USER_UNKNOWN;
2N/A goto out;
2N/A }
2N/A
2N/A if (server_policy &&
2N/A strcmp(repository_name, "files") != 0 &&
2N/A strcmp(repository_name, "nis") != 0 &&
2N/A strcmp(repository_name, "nisplus") != 0) {
2N/A result = PAM_IGNORE;
2N/A goto out;
2N/A }
2N/A
2N/A /* Now check the entered password */
2N/A if ((crypt_passwd = crypt(passwd, rep_passwd)) == NULL) {
2N/A switch (errno) {
2N/A case ENOMEM:
2N/A result = PAM_BUF_ERR;
2N/A break;
2N/A case ELIBACC:
2N/A result = PAM_OPEN_ERR;
2N/A break;
2N/A default:
2N/A result = PAM_SYSTEM_ERR;
2N/A }
2N/A goto out;
2N/A }
2N/A
2N/A if (strcmp(crypt_passwd, rep_passwd) == 0)
2N/A result = PAM_SUCCESS;
2N/A else
2N/A result = PAM_AUTH_ERR;
2N/A
2N/A /* Clear or increment failed failed count */
2N/A if (dolock && (result == PAM_SUCCESS && old_failed_count > 0)) {
2N/A old_failed_count = __rst_failed_count(user, repository_name);
2N/A if (nowarn == 0 && old_failed_count > 0)
2N/A display_warning(pamh, old_failed_count, homedir);
2N/A } else if (dolock && result == PAM_AUTH_ERR) {
2N/A int max_failed = get_max_failed(user);
2N/A if (max_failed != 0) {
2N/A if (__incr_failed_count(user, repository_name,
2N/A max_failed) == PWU_ACCOUNT_LOCKED)
2N/A result = PAM_MAXTRIES;
2N/A }
2N/A }
2N/Aout:
2N/A if (rep_passwd)
2N/A free(rep_passwd);
2N/A if (repository_name)
2N/A free(repository_name);
2N/A if (homedir)
2N/A free(homedir);
2N/A return (result);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Aint
2N/Apam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
2N/A{
2N/A return (PAM_IGNORE);
2N/A}
2N/A