14Srgrimes/*-
24Srgrimes * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
34Srgrimes * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
44Srgrimes * All rights reserved.
54Srgrimes *
64Srgrimes * This software was developed for the FreeBSD Project by ThinkSec AS and
74Srgrimes * Network Associates Laboratories, the Security Research Division of
84Srgrimes * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
94Srgrimes * ("CBOSS"), as part of the DARPA CHATS research program.
104Srgrimes *
114Srgrimes * Redistribution and use in source and binary forms, with or without
124Srgrimes * modification, are permitted provided that the following conditions
134Srgrimes * are met:
144Srgrimes * 1. Redistributions of source code must retain the above copyright
154Srgrimes *    notice, this list of conditions and the following disclaimer.
164Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
174Srgrimes *    notice, this list of conditions and the following disclaimer in the
184Srgrimes *    documentation and/or other materials provided with the distribution.
194Srgrimes * 3. The name of the author may not be used to endorse or promote
204Srgrimes *    products derived from this software without specific prior written
214Srgrimes *    permission.
224Srgrimes *
234Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
244Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
274Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32551Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3350477Speter * SUCH DAMAGE.
344Srgrimes *
354Srgrimes * $Id: pam_get_authtok.c 670 2013-03-17 19:26:07Z des $
36196994Sphk */
37196994Sphk
38196994Sphk#ifdef HAVE_CONFIG_H
39196994Sphk# include "config.h"
40249722Stijl#endif
41249722Stijl
424Srgrimes#include <sys/param.h>
434Srgrimes
444Srgrimes#include <stdlib.h>
4568498Sasmodai#include <string.h>
4668498Sasmodai
47154128Simp#include <security/pam_appl.h>
48223440Sjhb#include <security/openpam.h>
49154128Simp
50154128Simp#include "openpam_impl.h"
5144364Simp
52551Srgrimesstatic const char authtok_prompt[] = "Password:";
5344364Simpstatic const char authtok_prompt_remote[] = "Password for %u@%h:";
5444364Simpstatic const char oldauthtok_prompt[] = "Old Password:";
5538673Skatostatic const char newauthtok_prompt[] = "New Password:";
5644364Simp
5736977Sbde/*
5836977Sbde * OpenPAM extension
59177661Sjb *
60224207Sattilio * Retrieve authentication token
61183525Sjhb */
62224207Sattilio
6328354Sfsmpint
6466296Spspam_get_authtok(pam_handle_t *pamh,
65177661Sjb	int item,
664Srgrimes	const char **authtok,
67250338Sattilio	const char *prompt)
68250338Sattilio{
69250338Sattilio	char prompt_buf[1024];
70250338Sattilio	size_t prompt_size;
7168498Sasmodai	const void *oldauthtok, *prevauthtok, *promptp;
7268498Sasmodai	const char *prompt_option, *default_prompt;
73195376Ssam	const void *lhost, *rhost;
74195376Ssam	char *resp, *resp2;
75195376Ssam	int pitem, r, style, twice;
76195376Ssam
77195376Ssam	ENTER();
78195376Ssam	if (pamh == NULL || authtok == NULL)
79195376Ssam		RETURNC(PAM_SYSTEM_ERR);
804Srgrimes	*authtok = NULL;
81191278Srwatson	twice = 0;
82191278Srwatson	switch (item) {
83191278Srwatson	case PAM_AUTHTOK:
84191278Srwatson		pitem = PAM_AUTHTOK_PROMPT;
85192331Sjhb		prompt_option = "authtok_prompt";
86191276Srwatson		default_prompt = authtok_prompt;
87191276Srwatson		r = pam_get_item(pamh, PAM_RHOST, &rhost);
8815543Sphk		if (r == PAM_SUCCESS && rhost != NULL) {
8915565Sphk			r = pam_get_item(pamh, PAM_HOST, &lhost);
901045Sdg			if (r == PAM_SUCCESS && lhost != NULL) {
9115543Sphk				if (strcmp(rhost, lhost) != 0)
9215565Sphk					default_prompt = authtok_prompt_remote;
93282065Skib			}
94112841Sjake		}
95112841Sjake		r = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldauthtok);
96183343Skmacy		if (r == PAM_SUCCESS && oldauthtok != NULL) {
97112841Sjake			default_prompt = newauthtok_prompt;
98111363Sjake			twice = 1;
99111363Sjake		}
100183343Skmacy		break;
101112841Sjake	case PAM_OLDAUTHTOK:
102111363Sjake		pitem = PAM_OLDAUTHTOK_PROMPT;
103111363Sjake		prompt_option = "oldauthtok_prompt";
104111363Sjake		default_prompt = oldauthtok_prompt;
10515543Sphk		twice = 0;
10615565Sphk		break;
10727950Sdyson	default:
1084Srgrimes		RETURNC(PAM_SYMBOL_ERR);
109197316Salc	}
110197316Salc	if (openpam_get_option(pamh, "try_first_pass") ||
11127993Sdyson	    openpam_get_option(pamh, "use_first_pass")) {
11282309Speter		r = pam_get_item(pamh, item, &prevauthtok);
11383366Sjulian		if (r == PAM_SUCCESS && prevauthtok != NULL) {
11483366Sjulian			*authtok = prevauthtok;
11582309Speter			RETURNC(PAM_SUCCESS);
116116355Salc		} else if (openpam_get_option(pamh, "use_first_pass")) {
1174Srgrimes			RETURNC(r == PAM_SUCCESS ? PAM_AUTH_ERR : r);
1184Srgrimes		}
119239730Sdes	}
120239730Sdes	/* pam policy overrides the module's choice */
121239730Sdes	if ((promptp = openpam_get_option(pamh, prompt_option)) != NULL)
122239730Sdes		prompt = promptp;
123239730Sdes	/* no prompt provided, see if there is one tucked away somewhere */
124239730Sdes	if (prompt == NULL)
125239730Sdes		if (pam_get_item(pamh, pitem, &promptp) && promptp != NULL)
126239730Sdes			prompt = promptp;
127239730Sdes	/* fall back to hardcoded default */
128239730Sdes	if (prompt == NULL)
129239730Sdes		prompt = default_prompt;
130239730Sdes	/* expand */
131239730Sdes	prompt_size = sizeof prompt_buf;
132239730Sdes	r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt);
13381933Sdillon	if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf)
134102738Sdillon		prompt = prompt_buf;
135102738Sdillon	style = openpam_get_option(pamh, "echo_pass") ?
136248789Skib	    PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
137248789Skib	r = pam_prompt(pamh, style, &resp, "%s", prompt);
138248789Skib	if (r != PAM_SUCCESS)
13981933Sdillon		RETURNC(r);
14081933Sdillon	if (twice) {
141248789Skib		r = pam_prompt(pamh, style, &resp2, "Retype %s", prompt);
14281933Sdillon		if (r != PAM_SUCCESS) {
14381933Sdillon			FREE(resp);
14481933Sdillon			RETURNC(r);
1454Srgrimes		}
1464Srgrimes		if (strcmp(resp, resp2) != 0)
14740286Sdg			FREE(resp);
14840286Sdg		FREE(resp2);
149112569Sjake	}
150112569Sjake	if (resp == NULL)
1511549Srgrimes		RETURNC(PAM_TRY_AGAIN);
152112569Sjake	r = pam_set_item(pamh, item, resp);
153112569Sjake	FREE(resp);
1541045Sdg	if (r != PAM_SUCCESS)
155112569Sjake		RETURNC(r);
156112569Sjake	r = pam_get_item(pamh, item, (const void **)authtok);
157584Srgrimes	RETURNC(r);
15844157Sluoqi}
15944157Sluoqi
160196994Sphk/*
161 * Error codes:
162 *
163 *	=pam_get_item
164 *	=pam_prompt
165 *	=pam_set_item
166 *	!PAM_SYMBOL_ERR
167 *	PAM_TRY_AGAIN
168 */
169
170/**
171 * The =pam_get_authtok function either prompts the user for an
172 * authentication token or retrieves a cached authentication token,
173 * depending on circumstances.
174 * Either way, a pointer to the authentication token is stored in the
175 * location pointed to by the =authtok argument, and the corresponding PAM
176 * item is updated.
177 *
178 * The =item argument must have one of the following values:
179 *
180 *	=PAM_AUTHTOK:
181 *		Returns the current authentication token, or the new token
182 *		when changing authentication tokens.
183 *	=PAM_OLDAUTHTOK:
184 *		Returns the previous authentication token when changing
185 *		authentication tokens.
186 *
187 * The =prompt argument specifies a prompt to use if no token is cached.
188 * If it is =NULL, the =PAM_AUTHTOK_PROMPT or =PAM_OLDAUTHTOK_PROMPT item,
189 * as appropriate, will be used.
190 * If that item is also =NULL, a hardcoded default prompt will be used.
191 * Additionally, when =pam_get_authtok is called from a service module,
192 * the prompt may be affected by module options as described below.
193 * The prompt is then expanded using =openpam_subst before it is passed to
194 * the conversation function.
195 *
196 * If =item is set to =PAM_AUTHTOK and there is a non-null =PAM_OLDAUTHTOK
197 * item, =pam_get_authtok will ask the user to confirm the new token by
198 * retyping it.
199 * If there is a mismatch, =pam_get_authtok will return =PAM_TRY_AGAIN.
200 *
201 * MODULE OPTIONS
202 *
203 * When called by a service module, =pam_get_authtok will recognize the
204 * following module options:
205 *
206 *	;authtok_prompt:
207 *		Prompt to use when =item is set to =PAM_AUTHTOK.
208 *		This option overrides both the =prompt argument and the
209 *		=PAM_AUTHTOK_PROMPT item.
210 *	;echo_pass:
211 *		If the application's conversation function allows it, this
212 *		lets the user see what they are typing.
213 *		This should only be used for non-reusable authentication
214 *		tokens.
215 *	;oldauthtok_prompt:
216 *		Prompt to use when =item is set to =PAM_OLDAUTHTOK.
217 *		This option overrides both the =prompt argument and the
218 *		=PAM_OLDAUTHTOK_PROMPT item.
219 *	;try_first_pass:
220 *		If the requested item is non-null, return it without
221 *		prompting the user.
222 *		Typically, the service module will verify the token, and
223 *		if it does not match, clear the item before calling
224 *		=pam_get_authtok a second time.
225 *	;use_first_pass:
226 *		Do not prompt the user at all; just return the cached
227 *		value, or =PAM_AUTH_ERR if there is none.
228 *
229 * >pam_conv
230 * >pam_get_item
231 * >pam_get_user
232 * >openpam_get_option
233 * >openpam_subst
234 */
235