1 /* $NetBSD: pam_get_authtok.c,v 1.5 2023/06/30 21:46:21 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
5 * Copyright (c) 2004-2017 Dag-Erling Smørgrav
6 * All rights reserved.
7 *
8 * This software was developed for the FreeBSD Project by ThinkSec AS and
9 * Network Associates Laboratories, the Security Research Division of
10 * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
11 * ("CBOSS"), as part of the DARPA CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote
22 * products derived from this software without specific prior written
23 * permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <sys/cdefs.h>
43 __RCSID("$NetBSD: pam_get_authtok.c,v 1.5 2023/06/30 21:46:21 christos Exp $");
44
45 #include <sys/param.h>
46
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include <security/pam_appl.h>
51 #include <security/openpam.h>
52
53 #include "openpam_impl.h"
54 #include "openpam_strlset.h"
55
56 static const char authtok_prompt[] = "Password:";
57 static const char authtok_prompt_remote[] = "Password for %u@%h:";
58 static const char oldauthtok_prompt[] = "Old Password:";
59 static const char newauthtok_prompt[] = "New Password:";
60
61 /*
62 * OpenPAM extension
63 *
64 * Retrieve authentication token
65 */
66
67 int
pam_get_authtok(pam_handle_t * pamh,int item,const char ** authtok,const char * prompt)68 pam_get_authtok(pam_handle_t *pamh,
69 int item,
70 const char **authtok,
71 const char *prompt)
72 {
73 char prompt_buf[1024];
74 size_t prompt_size;
75 const void *oldauthtok, *prevauthtok, *promptp;
76 const char *prompt_option, *default_prompt;
77 const void *lhost, *rhost;
78 char *resp, *resp2;
79 int pitem, r, style, twice;
80
81 ENTER();
82 *authtok = NULL;
83 twice = 0;
84 switch ((enum openpam_item_primitives)item) {
85 case PAM_AUTHTOK:
86 pitem = PAM_AUTHTOK_PROMPT;
87 prompt_option = "authtok_prompt";
88 default_prompt = authtok_prompt;
89 r = pam_get_item(pamh, PAM_RHOST, &rhost);
90 if (r == PAM_SUCCESS && rhost != NULL) {
91 r = pam_get_item(pamh, PAM_HOST, &lhost);
92 if (r == PAM_SUCCESS && lhost != NULL) {
93 if (strcmp(rhost, lhost) != 0)
94 default_prompt = authtok_prompt_remote;
95 }
96 }
97 r = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldauthtok);
98 if (r == PAM_SUCCESS && oldauthtok != NULL) {
99 default_prompt = newauthtok_prompt;
100 twice = 1;
101 }
102 break;
103 case PAM_OLDAUTHTOK:
104 pitem = PAM_OLDAUTHTOK_PROMPT;
105 prompt_option = "oldauthtok_prompt";
106 default_prompt = oldauthtok_prompt;
107 twice = 0;
108 break;
109 default:
110 RETURNC(PAM_BAD_CONSTANT);
111 }
112 if (openpam_get_option(pamh, "try_first_pass") ||
113 openpam_get_option(pamh, "use_first_pass")) {
114 r = pam_get_item(pamh, item, &prevauthtok);
115 if (r == PAM_SUCCESS && prevauthtok != NULL) {
116 *authtok = prevauthtok;
117 RETURNC(PAM_SUCCESS);
118 } else if (openpam_get_option(pamh, "use_first_pass")) {
119 RETURNC(r == PAM_SUCCESS ? PAM_AUTH_ERR : r);
120 }
121 }
122 /* pam policy overrides the module's choice */
123 if ((promptp = openpam_get_option(pamh, prompt_option)) != NULL)
124 prompt = promptp;
125 /* no prompt provided, see if there is one tucked away somewhere */
126 if (prompt == NULL) {
127 r = pam_get_item(pamh, pitem, &promptp);
128 if (r == PAM_SUCCESS && promptp != NULL)
129 prompt = promptp;
130 }
131 /* fall back to hardcoded default */
132 if (prompt == NULL)
133 prompt = default_prompt;
134 /* expand */
135 prompt_size = sizeof prompt_buf;
136 r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt);
137 if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf)
138 prompt = prompt_buf;
139 style = openpam_get_option(pamh, "echo_pass") ?
140 PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
141 r = pam_prompt(pamh, style, &resp, "%s", prompt);
142 if (r != PAM_SUCCESS)
143 RETURNC(r);
144 if (twice) {
145 r = pam_prompt(pamh, style, &resp2, "Retype %s", prompt);
146 if (r != PAM_SUCCESS) {
147 strlset(resp, 0, PAM_MAX_RESP_SIZE);
148 FREE(resp);
149 RETURNC(r);
150 }
151 if (strcmp(resp, resp2) != 0) {
152 strlset(resp, 0, PAM_MAX_RESP_SIZE);
153 FREE(resp);
154 }
155 strlset(resp2, 0, PAM_MAX_RESP_SIZE);
156 FREE(resp2);
157 }
158 if (resp == NULL)
159 RETURNC(PAM_TRY_AGAIN);
160 r = pam_set_item(pamh, item, resp);
161 strlset(resp, 0, PAM_MAX_RESP_SIZE);
162 FREE(resp);
163 if (r != PAM_SUCCESS)
164 RETURNC(r);
165 r = pam_get_item(pamh, item, (const void **)authtok);
166 RETURNC(r);
167 }
168
169 /*
170 * Error codes:
171 *
172 * =pam_get_item
173 * =pam_prompt
174 * =pam_set_item
175 * !PAM_SYMBOL_ERR
176 * PAM_BAD_CONSTANT
177 * PAM_TRY_AGAIN
178 */
179
180 /**
181 * The =pam_get_authtok function either prompts the user for an
182 * authentication token or retrieves a cached authentication token,
183 * depending on circumstances.
184 * Either way, a pointer to the authentication token is stored in the
185 * location pointed to by the =authtok argument, and the corresponding PAM
186 * item is updated.
187 *
188 * The =item argument must have one of the following values:
189 *
190 * =PAM_AUTHTOK:
191 * Returns the current authentication token, or the new token
192 * when changing authentication tokens.
193 * =PAM_OLDAUTHTOK:
194 * Returns the previous authentication token when changing
195 * authentication tokens.
196 *
197 * The =prompt argument specifies a prompt to use if no token is cached.
198 * If it is =NULL, the =PAM_AUTHTOK_PROMPT or =PAM_OLDAUTHTOK_PROMPT item,
199 * as appropriate, will be used.
200 * If that item is also =NULL, a hardcoded default prompt will be used.
201 * Additionally, when =pam_get_authtok is called from a service module,
202 * the prompt may be affected by module options as described below.
203 * The prompt is then expanded using =openpam_subst before it is passed to
204 * the conversation function.
205 *
206 * If =item is set to =PAM_AUTHTOK and there is a non-null =PAM_OLDAUTHTOK
207 * item, =pam_get_authtok will ask the user to confirm the new token by
208 * retyping it.
209 * If there is a mismatch, =pam_get_authtok will return =PAM_TRY_AGAIN.
210 *
211 * MODULE OPTIONS
212 *
213 * When called by a service module, =pam_get_authtok will recognize the
214 * following module options:
215 *
216 * ;authtok_prompt:
217 * Prompt to use when =item is set to =PAM_AUTHTOK.
218 * This option overrides both the =prompt argument and the
219 * =PAM_AUTHTOK_PROMPT item.
220 * ;echo_pass:
221 * If the application's conversation function allows it, this
222 * lets the user see what they are typing.
223 * This should only be used for non-reusable authentication
224 * tokens.
225 * ;oldauthtok_prompt:
226 * Prompt to use when =item is set to =PAM_OLDAUTHTOK.
227 * This option overrides both the =prompt argument and the
228 * =PAM_OLDAUTHTOK_PROMPT item.
229 * ;try_first_pass:
230 * If the requested item is non-null, return it without
231 * prompting the user.
232 * Typically, the service module will verify the token, and
233 * if it does not match, clear the item before calling
234 * =pam_get_authtok a second time.
235 * ;use_first_pass:
236 * Do not prompt the user at all; just return the cached
237 * value, or =PAM_AUTH_ERR if there is none.
238 *
239 * >pam_conv
240 * >pam_get_item
241 * >pam_get_user
242 * >openpam_get_option
243 * >openpam_subst
244 */
245