2 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3 * Copyright (c) 2004-2017 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 * $OpenPAM: pam_get_authtok.c 938 2017-04-30 21:34:42Z des $
42 #include <sys/param.h>
47 #include <security/pam_appl.h>
48 #include <security/openpam.h>
50 #include "openpam_impl.h"
51 #include "openpam_strlset.h"
53 static const char authtok_prompt[] = "Password:";
54 static const char authtok_prompt_remote[] = "Password for %u@%h:";
55 static const char oldauthtok_prompt[] = "Old Password:";
56 static const char newauthtok_prompt[] = "New Password:";
61 * Retrieve authentication token
65 pam_get_authtok(pam_handle_t *pamh,
70 char prompt_buf[1024];
72 const void *oldauthtok, *prevauthtok, *promptp;
73 const char *prompt_option, *default_prompt;
74 const void *lhost, *rhost;
76 int pitem, r, style, twice;
83 pitem = PAM_AUTHTOK_PROMPT;
84 prompt_option = "authtok_prompt";
85 default_prompt = authtok_prompt;
86 r = pam_get_item(pamh, PAM_RHOST, &rhost);
87 if (r == PAM_SUCCESS && rhost != NULL) {
88 r = pam_get_item(pamh, PAM_HOST, &lhost);
89 if (r == PAM_SUCCESS && lhost != NULL) {
90 if (strcmp(rhost, lhost) != 0)
91 default_prompt = authtok_prompt_remote;
94 r = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldauthtok);
95 if (r == PAM_SUCCESS && oldauthtok != NULL) {
96 default_prompt = newauthtok_prompt;
101 pitem = PAM_OLDAUTHTOK_PROMPT;
102 prompt_option = "oldauthtok_prompt";
103 default_prompt = oldauthtok_prompt;
107 RETURNC(PAM_BAD_CONSTANT);
109 if (openpam_get_option(pamh, "try_first_pass") ||
110 openpam_get_option(pamh, "use_first_pass")) {
111 r = pam_get_item(pamh, item, &prevauthtok);
112 if (r == PAM_SUCCESS && prevauthtok != NULL) {
113 *authtok = prevauthtok;
114 RETURNC(PAM_SUCCESS);
115 } else if (openpam_get_option(pamh, "use_first_pass")) {
116 RETURNC(r == PAM_SUCCESS ? PAM_AUTH_ERR : r);
119 /* pam policy overrides the module's choice */
120 if ((promptp = openpam_get_option(pamh, prompt_option)) != NULL)
122 /* no prompt provided, see if there is one tucked away somewhere */
123 if (prompt == NULL) {
124 r = pam_get_item(pamh, pitem, &promptp);
125 if (r == PAM_SUCCESS && promptp != NULL)
128 /* fall back to hardcoded default */
130 prompt = default_prompt;
132 prompt_size = sizeof prompt_buf;
133 r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt);
134 if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf)
136 style = openpam_get_option(pamh, "echo_pass") ?
137 PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
138 r = pam_prompt(pamh, style, &resp, "%s", prompt);
139 if (r != PAM_SUCCESS)
142 r = pam_prompt(pamh, style, &resp2, "Retype %s", prompt);
143 if (r != PAM_SUCCESS) {
144 strlset(resp, 0, PAM_MAX_RESP_SIZE);
148 if (strcmp(resp, resp2) != 0) {
149 strlset(resp, 0, PAM_MAX_RESP_SIZE);
152 strlset(resp2, 0, PAM_MAX_RESP_SIZE);
156 RETURNC(PAM_TRY_AGAIN);
157 r = pam_set_item(pamh, item, resp);
158 strlset(resp, 0, PAM_MAX_RESP_SIZE);
160 if (r != PAM_SUCCESS)
162 r = pam_get_item(pamh, item, (const void **)authtok);
178 * The =pam_get_authtok function either prompts the user for an
179 * authentication token or retrieves a cached authentication token,
180 * depending on circumstances.
181 * Either way, a pointer to the authentication token is stored in the
182 * location pointed to by the =authtok argument, and the corresponding PAM
185 * The =item argument must have one of the following values:
188 * Returns the current authentication token, or the new token
189 * when changing authentication tokens.
191 * Returns the previous authentication token when changing
192 * authentication tokens.
194 * The =prompt argument specifies a prompt to use if no token is cached.
195 * If it is =NULL, the =PAM_AUTHTOK_PROMPT or =PAM_OLDAUTHTOK_PROMPT item,
196 * as appropriate, will be used.
197 * If that item is also =NULL, a hardcoded default prompt will be used.
198 * Additionally, when =pam_get_authtok is called from a service module,
199 * the prompt may be affected by module options as described below.
200 * The prompt is then expanded using =openpam_subst before it is passed to
201 * the conversation function.
203 * If =item is set to =PAM_AUTHTOK and there is a non-null =PAM_OLDAUTHTOK
204 * item, =pam_get_authtok will ask the user to confirm the new token by
206 * If there is a mismatch, =pam_get_authtok will return =PAM_TRY_AGAIN.
210 * When called by a service module, =pam_get_authtok will recognize the
211 * following module options:
214 * Prompt to use when =item is set to =PAM_AUTHTOK.
215 * This option overrides both the =prompt argument and the
216 * =PAM_AUTHTOK_PROMPT item.
218 * If the application's conversation function allows it, this
219 * lets the user see what they are typing.
220 * This should only be used for non-reusable authentication
222 * ;oldauthtok_prompt:
223 * Prompt to use when =item is set to =PAM_OLDAUTHTOK.
224 * This option overrides both the =prompt argument and the
225 * =PAM_OLDAUTHTOK_PROMPT item.
227 * If the requested item is non-null, return it without
228 * prompting the user.
229 * Typically, the service module will verify the token, and
230 * if it does not match, clear the item before calling
231 * =pam_get_authtok a second time.
233 * Do not prompt the user at all; just return the cached
234 * value, or =PAM_AUTH_ERR if there is none.
239 * >openpam_get_option