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_get_authtok.c 455 2011-10-29 18:31:11Z des $
42 #include <sys/param.h>
47 #include <security/pam_appl.h>
48 #include <security/openpam.h>
50 #include "openpam_impl.h"
52 static const char authtok_prompt[] = "Password:";
53 static const char oldauthtok_prompt[] = "Old Password:";
54 static const char newauthtok_prompt[] = "New Password:";
59 * Retrieve authentication token
63 pam_get_authtok(pam_handle_t *pamh,
68 char prompt_buf[1024];
70 const void *oldauthtok, *prevauthtok, *promptp;
71 const char *prompt_option, *default_prompt;
73 int pitem, r, style, twice;
76 if (pamh == NULL || authtok == NULL)
77 RETURNC(PAM_SYSTEM_ERR);
82 pitem = PAM_AUTHTOK_PROMPT;
83 prompt_option = "authtok_prompt";
84 default_prompt = authtok_prompt;
85 r = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldauthtok);
86 if (r == PAM_SUCCESS && oldauthtok != NULL) {
87 default_prompt = newauthtok_prompt;
92 pitem = PAM_OLDAUTHTOK_PROMPT;
93 prompt_option = "oldauthtok_prompt";
94 default_prompt = oldauthtok_prompt;
98 RETURNC(PAM_SYMBOL_ERR);
100 if (openpam_get_option(pamh, "try_first_pass") ||
101 openpam_get_option(pamh, "use_first_pass")) {
102 r = pam_get_item(pamh, item, &prevauthtok);
103 if (r == PAM_SUCCESS && prevauthtok != NULL) {
104 *authtok = prevauthtok;
105 RETURNC(PAM_SUCCESS);
107 else if (openpam_get_option(pamh, "use_first_pass"))
108 RETURNC(r == PAM_SUCCESS ? PAM_AUTH_ERR : r);
110 /* pam policy overrides the module's choice */
111 if ((promptp = openpam_get_option(pamh, prompt_option)) != NULL)
113 /* no prompt provided, see if there is one tucked away somewhere */
115 if (pam_get_item(pamh, pitem, &promptp) && promptp != NULL)
117 /* fall back to hardcoded default */
119 prompt = default_prompt;
121 prompt_size = sizeof prompt_buf;
122 r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt);
123 if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf)
125 style = openpam_get_option(pamh, "echo_pass") ?
126 PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
127 r = pam_prompt(pamh, style, &resp, "%s", prompt);
128 if (r != PAM_SUCCESS)
131 r = pam_prompt(pamh, style, &resp2, "Retype %s", prompt);
132 if (r != PAM_SUCCESS) {
136 if (strcmp(resp, resp2) != 0)
141 RETURNC(PAM_TRY_AGAIN);
142 r = pam_set_item(pamh, item, resp);
144 if (r != PAM_SUCCESS)
146 r = pam_get_item(pamh, item, (const void **)authtok);
161 * The =pam_get_authtok function returns the cached authentication token,
162 * or prompts the user if no token is currently cached.
163 * Either way, a pointer to the authentication token is stored in the
164 * location pointed to by the =authtok argument.
166 * The =item argument must have one of the following values:
169 * Returns the current authentication token, or the new token
170 * when changing authentication tokens.
172 * Returns the previous authentication token when changing
173 * authentication tokens.
175 * The =prompt argument specifies a prompt to use if no token is cached.
176 * If it is =NULL, the =PAM_AUTHTOK_PROMPT or =PAM_OLDAUTHTOK_PROMPT item,
177 * as appropriate, will be used.
178 * If that item is also =NULL, a hardcoded default prompt will be used.
179 * Either way, the prompt is expanded using =openpam_subst before it is
180 * passed to the conversation function.
182 * If =pam_get_authtok is called from a module and the ;authtok_prompt /
183 * ;oldauthtok_prompt option is set in the policy file, the value of that
184 * option takes precedence over both the =prompt argument and the
185 * =PAM_AUTHTOK_PROMPT / =PAM_OLDAUTHTOK_PROMPT item.
187 * If =item is set to =PAM_AUTHTOK and there is a non-null =PAM_OLDAUTHTOK
188 * item, =pam_get_authtok will ask the user to confirm the new token by
190 * If there is a mismatch, =pam_get_authtok will return =PAM_TRY_AGAIN.