2 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3 * Copyright (c) 2004-2011 Dag-Erling Smørgrav
6 * This software was developed for the FreeBSD Project by ThinkSec AS and
7 * Network Associates Laboratories, the Security Research Division of
8 * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9 * ("CBOSS"), as part of the DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $Id: pam_unix.c 437 2011-09-13 12:00:13Z des $
42 #include <sys/param.h>
54 #include <security/pam_modules.h>
55 #include <security/pam_appl.h>
58 static char password_prompt[] = "Password:";
66 pam_sm_authenticate(pam_handle_t *pamh, int flags,
67 int argc, const char *argv[])
70 struct pam_conv *conv;
71 struct pam_message msg;
72 const struct pam_message *msgp;
73 struct pam_response *resp;
77 char *crypt_password, *password;
84 if ((pam_err = pam_get_user(pamh, &user, NULL)) != PAM_SUCCESS)
86 if ((pwd = getpwnam(user)) == NULL)
87 return (PAM_USER_UNKNOWN);
91 pam_err = pam_get_item(pamh, PAM_CONV, (const void **)&conv);
92 if (pam_err != PAM_SUCCESS)
93 return (PAM_SYSTEM_ERR);
94 msg.msg_style = PAM_PROMPT_ECHO_OFF;
95 msg.msg = password_prompt;
98 for (retry = 0; retry < 3; ++retry) {
100 pam_err = pam_get_authtok(pamh, PAM_AUTHTOK,
101 (const char **)&password, NULL);
104 pam_err = (*conv->conv)(1, &msgp, &resp, conv->appdata_ptr);
106 if (pam_err == PAM_SUCCESS)
107 password = resp->resp;
113 if (pam_err == PAM_SUCCESS)
116 if (pam_err == PAM_CONV_ERR)
118 if (pam_err != PAM_SUCCESS)
119 return (PAM_AUTH_ERR);
121 /* compare passwords */
122 if ((!pwd->pw_passwd[0] && (flags & PAM_DISALLOW_NULL_AUTHTOK)) ||
123 (crypt_password = crypt(password, pwd->pw_passwd)) == NULL ||
124 strcmp(crypt_password, pwd->pw_passwd) != 0)
125 pam_err = PAM_AUTH_ERR;
127 pam_err = PAM_SUCCESS;
135 pam_sm_setcred(pam_handle_t *pamh, int flags,
136 int argc, const char *argv[])
143 return (PAM_SUCCESS);
147 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
148 int argc, const char *argv[])
155 return (PAM_SUCCESS);
159 pam_sm_open_session(pam_handle_t *pamh, int flags,
160 int argc, const char *argv[])
167 return (PAM_SUCCESS);
171 pam_sm_close_session(pam_handle_t *pamh, int flags,
172 int argc, const char *argv[])
179 return (PAM_SUCCESS);
183 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
184 int argc, const char *argv[])
191 return (PAM_SERVICE_ERR);
194 #ifdef PAM_MODULE_ENTRY
195 PAM_MODULE_ENTRY("pam_unix");