Path: blob/main/documentation/static/source/articles/pam/pam_unix.c
18096 views
/*-1* Copyright (c) 2002 Networks Associates Technology, Inc.2* All rights reserved.3*4* This software was developed for the FreeBSD Project by ThinkSec AS and5* Network Associates Laboratories, the Security Research Division of6* Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-80357* ("CBOSS"), as part of the DARPA CHATS research program.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. The name of the author may not be used to endorse or promote18* products derived from this software without specific prior written19* permission.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*33* $P4: //depot/projects/openpam/modules/pam_unix/pam_unix.c#3 $34* $FreeBSD: head/en_US.ISO8859-1/articles/pam/pam_unix.c 38826 2012-05-17 19:12:14Z hrs $35*/3637#include <sys/param.h>3839#include <pwd.h>40#include <stdlib.h>41#include <stdio.h>42#include <string.h>43#include <unistd.h>4445#include <security/pam_modules.h>46#include <security/pam_appl.h>4748#ifndef _OPENPAM49static char password_prompt[] = "Password:";50#endif5152#ifndef PAM_EXTERN53#define PAM_EXTERN54#endif5556PAM_EXTERN int57pam_sm_authenticate(pam_handle_t *pamh, int flags,58int argc, const char *argv[])59{60#ifndef _OPENPAM61struct pam_conv *conv;62struct pam_message msg;63const struct pam_message *msgp;64struct pam_response *resp;65#endif66struct passwd *pwd;67const char *user;68char *crypt_password, *password;69int pam_err, retry;7071/* identify user */72if ((pam_err = pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS)73return (pam_err);74if ((pwd = getpwnam(user)) == NULL)75return (PAM_USER_UNKNOWN);7677/* get password */78#ifndef _OPENPAM79pam_err = pam_get_item(pamh, PAM_CONV, (const void **)&conv);80if (pam_err != PAM_SUCCESS)81return (PAM_SYSTEM_ERR);82msg.msg_style = PAM_PROMPT_ECHO_OFF;83msg.msg = password_prompt;84msgp = &msg;85#endif86for (retry = 0; retry < 3; ++retry) {87#ifdef _OPENPAM88pam_err = pam_get_authtok(pamh, PAM_AUTHTOK,89(const char **)&password, NULL);90#else91resp = NULL;92pam_err = (*conv->conv)(1, &msgp, &resp, conv->appdata_ptr);93if (resp != NULL) {94if (pam_err == PAM_SUCCESS)95password = resp->resp;96else97free(resp->resp);98free(resp);99}100#endif101if (pam_err == PAM_SUCCESS)102break;103}104if (pam_err == PAM_CONV_ERR)105return (pam_err);106if (pam_err != PAM_SUCCESS)107return (PAM_AUTH_ERR);108109/* compare passwords */110if ((!pwd->pw_passwd[0] && (flags & PAM_DISALLOW_NULL_AUTHTOK)) ||111(crypt_password = crypt(password, pwd->pw_passwd)) == NULL ||112strcmp(crypt_password, pwd->pw_passwd) != 0)113pam_err = PAM_AUTH_ERR;114else115pam_err = PAM_SUCCESS;116#ifndef _OPENPAM117free(password);118#endif119return (pam_err);120}121122PAM_EXTERN int123pam_sm_setcred(pam_handle_t *pamh, int flags,124int argc, const char *argv[])125{126127return (PAM_SUCCESS);128}129130PAM_EXTERN int131pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,132int argc, const char *argv[])133{134135return (PAM_SUCCESS);136}137138PAM_EXTERN int139pam_sm_open_session(pam_handle_t *pamh, int flags,140int argc, const char *argv[])141{142143return (PAM_SUCCESS);144}145146PAM_EXTERN int147pam_sm_close_session(pam_handle_t *pamh, int flags,148int argc, const char *argv[])149{150151return (PAM_SUCCESS);152}153154PAM_EXTERN int155pam_sm_chauthtok(pam_handle_t *pamh, int flags,156int argc, const char *argv[])157{158159return (PAM_SERVICE_ERR);160}161162#ifdef PAM_MODULE_ENTRY163PAM_MODULE_ENTRY("pam_unix");164#endif165166167