pam_get_authtok.c revision 315906
191094Sdes/*-
2115619Sdes * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3228690Sdes * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
491094Sdes * All rights reserved.
591094Sdes *
691094Sdes * This software was developed for the FreeBSD Project by ThinkSec AS and
799158Sdes * Network Associates Laboratories, the Security Research Division of
899158Sdes * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
999158Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
1091094Sdes *
1191094Sdes * Redistribution and use in source and binary forms, with or without
1291094Sdes * modification, are permitted provided that the following conditions
1391094Sdes * are met:
1491094Sdes * 1. Redistributions of source code must retain the above copyright
1591094Sdes *    notice, this list of conditions and the following disclaimer.
1691094Sdes * 2. Redistributions in binary form must reproduce the above copyright
1791094Sdes *    notice, this list of conditions and the following disclaimer in the
1891094Sdes *    documentation and/or other materials provided with the distribution.
1991094Sdes * 3. The name of the author may not be used to endorse or promote
2091094Sdes *    products derived from this software without specific prior written
2191094Sdes *    permission.
2291094Sdes *
2391094Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2491094Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2591094Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2691094Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2791094Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2891094Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2991094Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3091094Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3191094Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3291094Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3391094Sdes * SUCH DAMAGE.
3491094Sdes *
35271624Sdes * $Id: pam_get_authtok.c 807 2014-09-09 09:41:32Z des $
3691094Sdes */
3791094Sdes
38228690Sdes#ifdef HAVE_CONFIG_H
39228690Sdes# include "config.h"
40228690Sdes#endif
41228690Sdes
4291094Sdes#include <sys/param.h>
4391094Sdes
4493982Sdes#include <stdlib.h>
4596364Sdes#include <string.h>
4693982Sdes
4791094Sdes#include <security/pam_appl.h>
4891094Sdes#include <security/openpam.h>
4991094Sdes
5091094Sdes#include "openpam_impl.h"
51271624Sdes#include "openpam_strlset.h"
5291094Sdes
53117610Sdesstatic const char authtok_prompt[] = "Password:";
54236099Sdesstatic const char authtok_prompt_remote[] = "Password for %u@%h:";
55117610Sdesstatic const char oldauthtok_prompt[] = "Old Password:";
56117610Sdesstatic const char newauthtok_prompt[] = "New Password:";
5793982Sdes
5891094Sdes/*
5991094Sdes * OpenPAM extension
6091094Sdes *
6191094Sdes * Retrieve authentication token
6291094Sdes */
6391094Sdes
6491094Sdesint
6591094Sdespam_get_authtok(pam_handle_t *pamh,
6693982Sdes	int item,
6791094Sdes	const char **authtok,
6891094Sdes	const char *prompt)
6991094Sdes{
70228690Sdes	char prompt_buf[1024];
71228690Sdes	size_t prompt_size;
72125647Sdes	const void *oldauthtok, *prevauthtok, *promptp;
73228690Sdes	const char *prompt_option, *default_prompt;
74236099Sdes	const void *lhost, *rhost;
7593982Sdes	char *resp, *resp2;
7693982Sdes	int pitem, r, style, twice;
7791094Sdes
78107937Sdes	ENTER();
7991094Sdes	if (pamh == NULL || authtok == NULL)
80107937Sdes		RETURNC(PAM_SYSTEM_ERR);
8193982Sdes	*authtok = NULL;
8293982Sdes	twice = 0;
8393982Sdes	switch (item) {
8493982Sdes	case PAM_AUTHTOK:
8593982Sdes		pitem = PAM_AUTHTOK_PROMPT;
86228690Sdes		prompt_option = "authtok_prompt";
8793982Sdes		default_prompt = authtok_prompt;
88236099Sdes		r = pam_get_item(pamh, PAM_RHOST, &rhost);
89236099Sdes		if (r == PAM_SUCCESS && rhost != NULL) {
90236099Sdes			r = pam_get_item(pamh, PAM_HOST, &lhost);
91236099Sdes			if (r == PAM_SUCCESS && lhost != NULL) {
92236099Sdes				if (strcmp(rhost, lhost) != 0)
93236099Sdes					default_prompt = authtok_prompt_remote;
94236099Sdes			}
95236099Sdes		}
9693982Sdes		r = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldauthtok);
9793982Sdes		if (r == PAM_SUCCESS && oldauthtok != NULL) {
9893982Sdes			default_prompt = newauthtok_prompt;
9993982Sdes			twice = 1;
10093982Sdes		}
10193982Sdes		break;
10293982Sdes	case PAM_OLDAUTHTOK:
10393982Sdes		pitem = PAM_OLDAUTHTOK_PROMPT;
104228690Sdes		prompt_option = "oldauthtok_prompt";
10593982Sdes		default_prompt = oldauthtok_prompt;
10693982Sdes		twice = 0;
10793982Sdes		break;
10893982Sdes	default:
109107937Sdes		RETURNC(PAM_SYMBOL_ERR);
11093982Sdes	}
11191100Sdes	if (openpam_get_option(pamh, "try_first_pass") ||
11291100Sdes	    openpam_get_option(pamh, "use_first_pass")) {
113125647Sdes		r = pam_get_item(pamh, item, &prevauthtok);
114125647Sdes		if (r == PAM_SUCCESS && prevauthtok != NULL) {
115125647Sdes			*authtok = prevauthtok;
116107937Sdes			RETURNC(PAM_SUCCESS);
117255376Sdes		} else if (openpam_get_option(pamh, "use_first_pass")) {
118255376Sdes			RETURNC(r == PAM_SUCCESS ? PAM_AUTH_ERR : r);
119125647Sdes		}
12091100Sdes	}
121228690Sdes	/* pam policy overrides the module's choice */
122228690Sdes	if ((promptp = openpam_get_option(pamh, prompt_option)) != NULL)
123228690Sdes		prompt = promptp;
124228690Sdes	/* no prompt provided, see if there is one tucked away somewhere */
125315906Sdes	if (prompt == NULL) {
126315906Sdes		r = pam_get_item(pamh, pitem, &promptp);
127315906Sdes		if (r == PAM_SUCCESS && promptp != NULL)
128125647Sdes			prompt = promptp;
129315906Sdes	}
130228690Sdes	/* fall back to hardcoded default */
131228690Sdes	if (prompt == NULL)
132228690Sdes		prompt = default_prompt;
133228690Sdes	/* expand */
134228690Sdes	prompt_size = sizeof prompt_buf;
135228690Sdes	r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt);
136228690Sdes	if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf)
137228690Sdes		prompt = prompt_buf;
13891100Sdes	style = openpam_get_option(pamh, "echo_pass") ?
13991100Sdes	    PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
14093982Sdes	r = pam_prompt(pamh, style, &resp, "%s", prompt);
14191094Sdes	if (r != PAM_SUCCESS)
142107937Sdes		RETURNC(r);
14393982Sdes	if (twice) {
14493982Sdes		r = pam_prompt(pamh, style, &resp2, "Retype %s", prompt);
14593982Sdes		if (r != PAM_SUCCESS) {
146271624Sdes			strlset(resp, 0, PAM_MAX_RESP_SIZE);
147115619Sdes			FREE(resp);
148107937Sdes			RETURNC(r);
14993982Sdes		}
150271624Sdes		if (strcmp(resp, resp2) != 0) {
151271624Sdes			strlset(resp, 0, PAM_MAX_RESP_SIZE);
152115619Sdes			FREE(resp);
153271624Sdes		}
154271624Sdes		strlset(resp2, 0, PAM_MAX_RESP_SIZE);
155115619Sdes		FREE(resp2);
15693982Sdes	}
15793982Sdes	if (resp == NULL)
158107937Sdes		RETURNC(PAM_TRY_AGAIN);
15994706Sdes	r = pam_set_item(pamh, item, resp);
160271624Sdes	strlset(resp, 0, PAM_MAX_RESP_SIZE);
161115619Sdes	FREE(resp);
16293982Sdes	if (r != PAM_SUCCESS)
163107937Sdes		RETURNC(r);
164110556Sdes	r = pam_get_item(pamh, item, (const void **)authtok);
165110556Sdes	RETURNC(r);
16691094Sdes}
16791100Sdes
16891100Sdes/*
16991100Sdes * Error codes:
17091100Sdes *
17191100Sdes *	=pam_get_item
17291100Sdes *	=pam_prompt
17391100Sdes *	=pam_set_item
17491100Sdes *	!PAM_SYMBOL_ERR
17593982Sdes *	PAM_TRY_AGAIN
17691100Sdes */
17793982Sdes
17893982Sdes/**
179255376Sdes * The =pam_get_authtok function either prompts the user for an
180255376Sdes * authentication token or retrieves a cached authentication token,
181255376Sdes * depending on circumstances.
182141098Sdes * Either way, a pointer to the authentication token is stored in the
183255376Sdes * location pointed to by the =authtok argument, and the corresponding PAM
184255376Sdes * item is updated.
18593982Sdes *
18693982Sdes * The =item argument must have one of the following values:
18793982Sdes *
18895908Sdes *	=PAM_AUTHTOK:
18993982Sdes *		Returns the current authentication token, or the new token
19093982Sdes *		when changing authentication tokens.
19195908Sdes *	=PAM_OLDAUTHTOK:
19293982Sdes *		Returns the previous authentication token when changing
19393982Sdes *		authentication tokens.
19493982Sdes *
19593982Sdes * The =prompt argument specifies a prompt to use if no token is cached.
19693982Sdes * If it is =NULL, the =PAM_AUTHTOK_PROMPT or =PAM_OLDAUTHTOK_PROMPT item,
197141098Sdes * as appropriate, will be used.
198141098Sdes * If that item is also =NULL, a hardcoded default prompt will be used.
199255376Sdes * Additionally, when =pam_get_authtok is called from a service module,
200255376Sdes * the prompt may be affected by module options as described below.
201255376Sdes * The prompt is then expanded using =openpam_subst before it is passed to
202255376Sdes * the conversation function.
20393982Sdes *
20493982Sdes * If =item is set to =PAM_AUTHTOK and there is a non-null =PAM_OLDAUTHTOK
20593982Sdes * item, =pam_get_authtok will ask the user to confirm the new token by
206141098Sdes * retyping it.
207141098Sdes * If there is a mismatch, =pam_get_authtok will return =PAM_TRY_AGAIN.
20893982Sdes *
209255376Sdes * MODULE OPTIONS
210255376Sdes *
211255376Sdes * When called by a service module, =pam_get_authtok will recognize the
212255376Sdes * following module options:
213255376Sdes *
214255376Sdes *	;authtok_prompt:
215255376Sdes *		Prompt to use when =item is set to =PAM_AUTHTOK.
216255376Sdes *		This option overrides both the =prompt argument and the
217255376Sdes *		=PAM_AUTHTOK_PROMPT item.
218255376Sdes *	;echo_pass:
219255376Sdes *		If the application's conversation function allows it, this
220255376Sdes *		lets the user see what they are typing.
221255376Sdes *		This should only be used for non-reusable authentication
222255376Sdes *		tokens.
223255376Sdes *	;oldauthtok_prompt:
224255376Sdes *		Prompt to use when =item is set to =PAM_OLDAUTHTOK.
225255376Sdes *		This option overrides both the =prompt argument and the
226255376Sdes *		=PAM_OLDAUTHTOK_PROMPT item.
227255376Sdes *	;try_first_pass:
228255376Sdes *		If the requested item is non-null, return it without
229255376Sdes *		prompting the user.
230255376Sdes *		Typically, the service module will verify the token, and
231255376Sdes *		if it does not match, clear the item before calling
232255376Sdes *		=pam_get_authtok a second time.
233255376Sdes *	;use_first_pass:
234255376Sdes *		Do not prompt the user at all; just return the cached
235255376Sdes *		value, or =PAM_AUTH_ERR if there is none.
236255376Sdes *
237255376Sdes * >pam_conv
23893982Sdes * >pam_get_item
23993982Sdes * >pam_get_user
240255376Sdes * >openpam_get_option
241228690Sdes * >openpam_subst
24293982Sdes */
243