xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/pam_get_authtok.c (revision 0d9d0fd8a30be9a1924e715bbcf67a4a83efd262)
1*0d9d0fd8Schristos /*	$NetBSD: pam_get_authtok.c,v 1.5 2023/06/30 21:46:21 christos Exp $	*/
2201780c4Schristos 
376e8c542Schristos /*-
476e8c542Schristos  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
54cb4af11Schristos  * Copyright (c) 2004-2017 Dag-Erling Smørgrav
676e8c542Schristos  * All rights reserved.
776e8c542Schristos  *
876e8c542Schristos  * This software was developed for the FreeBSD Project by ThinkSec AS and
976e8c542Schristos  * Network Associates Laboratories, the Security Research Division of
1076e8c542Schristos  * Network Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1176e8c542Schristos  * ("CBOSS"), as part of the DARPA CHATS research program.
1276e8c542Schristos  *
1376e8c542Schristos  * Redistribution and use in source and binary forms, with or without
1476e8c542Schristos  * modification, are permitted provided that the following conditions
1576e8c542Schristos  * are met:
1676e8c542Schristos  * 1. Redistributions of source code must retain the above copyright
1776e8c542Schristos  *    notice, this list of conditions and the following disclaimer.
1876e8c542Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1976e8c542Schristos  *    notice, this list of conditions and the following disclaimer in the
2076e8c542Schristos  *    documentation and/or other materials provided with the distribution.
2176e8c542Schristos  * 3. The name of the author may not be used to endorse or promote
2276e8c542Schristos  *    products derived from this software without specific prior written
2376e8c542Schristos  *    permission.
2476e8c542Schristos  *
2576e8c542Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2676e8c542Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2776e8c542Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2876e8c542Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2976e8c542Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3076e8c542Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3176e8c542Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3276e8c542Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3376e8c542Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3476e8c542Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3576e8c542Schristos  * SUCH DAMAGE.
3676e8c542Schristos  */
3776e8c542Schristos 
3876e8c542Schristos #ifdef HAVE_CONFIG_H
3976e8c542Schristos # include "config.h"
4076e8c542Schristos #endif
4176e8c542Schristos 
42201780c4Schristos #include <sys/cdefs.h>
43*0d9d0fd8Schristos __RCSID("$NetBSD: pam_get_authtok.c,v 1.5 2023/06/30 21:46:21 christos Exp $");
44201780c4Schristos 
4576e8c542Schristos #include <sys/param.h>
4676e8c542Schristos 
4776e8c542Schristos #include <stdlib.h>
4876e8c542Schristos #include <string.h>
4976e8c542Schristos 
5076e8c542Schristos #include <security/pam_appl.h>
5176e8c542Schristos #include <security/openpam.h>
5276e8c542Schristos 
5376e8c542Schristos #include "openpam_impl.h"
5476e8c542Schristos #include "openpam_strlset.h"
5576e8c542Schristos 
5676e8c542Schristos static const char authtok_prompt[] = "Password:";
5776e8c542Schristos static const char authtok_prompt_remote[] = "Password for %u@%h:";
5876e8c542Schristos static const char oldauthtok_prompt[] = "Old Password:";
5976e8c542Schristos static const char newauthtok_prompt[] = "New Password:";
6076e8c542Schristos 
6176e8c542Schristos /*
6276e8c542Schristos  * OpenPAM extension
6376e8c542Schristos  *
6476e8c542Schristos  * Retrieve authentication token
6576e8c542Schristos  */
6676e8c542Schristos 
6776e8c542Schristos int
pam_get_authtok(pam_handle_t * pamh,int item,const char ** authtok,const char * prompt)6876e8c542Schristos pam_get_authtok(pam_handle_t *pamh,
6976e8c542Schristos 	int item,
7076e8c542Schristos 	const char **authtok,
7176e8c542Schristos 	const char *prompt)
7276e8c542Schristos {
7376e8c542Schristos 	char prompt_buf[1024];
7476e8c542Schristos 	size_t prompt_size;
7576e8c542Schristos 	const void *oldauthtok, *prevauthtok, *promptp;
7676e8c542Schristos 	const char *prompt_option, *default_prompt;
7776e8c542Schristos 	const void *lhost, *rhost;
7876e8c542Schristos 	char *resp, *resp2;
7976e8c542Schristos 	int pitem, r, style, twice;
8076e8c542Schristos 
8176e8c542Schristos 	ENTER();
8276e8c542Schristos 	*authtok = NULL;
8376e8c542Schristos 	twice = 0;
8472d8d6c3Schristos 	switch ((enum openpam_item_primitives)item) {
8576e8c542Schristos 	case PAM_AUTHTOK:
8676e8c542Schristos 		pitem = PAM_AUTHTOK_PROMPT;
8776e8c542Schristos 		prompt_option = "authtok_prompt";
8876e8c542Schristos 		default_prompt = authtok_prompt;
8976e8c542Schristos 		r = pam_get_item(pamh, PAM_RHOST, &rhost);
9076e8c542Schristos 		if (r == PAM_SUCCESS && rhost != NULL) {
9176e8c542Schristos 			r = pam_get_item(pamh, PAM_HOST, &lhost);
9276e8c542Schristos 			if (r == PAM_SUCCESS && lhost != NULL) {
9376e8c542Schristos 				if (strcmp(rhost, lhost) != 0)
9476e8c542Schristos 					default_prompt = authtok_prompt_remote;
9576e8c542Schristos 			}
9676e8c542Schristos 		}
9776e8c542Schristos 		r = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldauthtok);
9876e8c542Schristos 		if (r == PAM_SUCCESS && oldauthtok != NULL) {
9976e8c542Schristos 			default_prompt = newauthtok_prompt;
10076e8c542Schristos 			twice = 1;
10176e8c542Schristos 		}
10276e8c542Schristos 		break;
10376e8c542Schristos 	case PAM_OLDAUTHTOK:
10476e8c542Schristos 		pitem = PAM_OLDAUTHTOK_PROMPT;
10576e8c542Schristos 		prompt_option = "oldauthtok_prompt";
10676e8c542Schristos 		default_prompt = oldauthtok_prompt;
10776e8c542Schristos 		twice = 0;
10876e8c542Schristos 		break;
10976e8c542Schristos 	default:
1104cb4af11Schristos 		RETURNC(PAM_BAD_CONSTANT);
11176e8c542Schristos 	}
11276e8c542Schristos 	if (openpam_get_option(pamh, "try_first_pass") ||
11376e8c542Schristos 	    openpam_get_option(pamh, "use_first_pass")) {
11476e8c542Schristos 		r = pam_get_item(pamh, item, &prevauthtok);
11576e8c542Schristos 		if (r == PAM_SUCCESS && prevauthtok != NULL) {
11676e8c542Schristos 			*authtok = prevauthtok;
11776e8c542Schristos 			RETURNC(PAM_SUCCESS);
11876e8c542Schristos 		} else if (openpam_get_option(pamh, "use_first_pass")) {
11976e8c542Schristos 			RETURNC(r == PAM_SUCCESS ? PAM_AUTH_ERR : r);
12076e8c542Schristos 		}
12176e8c542Schristos 	}
12276e8c542Schristos 	/* pam policy overrides the module's choice */
12376e8c542Schristos 	if ((promptp = openpam_get_option(pamh, prompt_option)) != NULL)
12476e8c542Schristos 		prompt = promptp;
12576e8c542Schristos 	/* no prompt provided, see if there is one tucked away somewhere */
1264cb4af11Schristos 	if (prompt == NULL) {
1274cb4af11Schristos 		r = pam_get_item(pamh, pitem, &promptp);
1284cb4af11Schristos 		if (r == PAM_SUCCESS && promptp != NULL)
12976e8c542Schristos 			prompt = promptp;
1304cb4af11Schristos 	}
13176e8c542Schristos 	/* fall back to hardcoded default */
13276e8c542Schristos 	if (prompt == NULL)
13376e8c542Schristos 		prompt = default_prompt;
13476e8c542Schristos 	/* expand */
13576e8c542Schristos 	prompt_size = sizeof prompt_buf;
13676e8c542Schristos 	r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt);
13776e8c542Schristos 	if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf)
13876e8c542Schristos 		prompt = prompt_buf;
13976e8c542Schristos 	style = openpam_get_option(pamh, "echo_pass") ?
14076e8c542Schristos 	    PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
14176e8c542Schristos 	r = pam_prompt(pamh, style, &resp, "%s", prompt);
14276e8c542Schristos 	if (r != PAM_SUCCESS)
14376e8c542Schristos 		RETURNC(r);
14476e8c542Schristos 	if (twice) {
14576e8c542Schristos 		r = pam_prompt(pamh, style, &resp2, "Retype %s", prompt);
14676e8c542Schristos 		if (r != PAM_SUCCESS) {
14776e8c542Schristos 			strlset(resp, 0, PAM_MAX_RESP_SIZE);
14876e8c542Schristos 			FREE(resp);
14976e8c542Schristos 			RETURNC(r);
15076e8c542Schristos 		}
15176e8c542Schristos 		if (strcmp(resp, resp2) != 0) {
15276e8c542Schristos 			strlset(resp, 0, PAM_MAX_RESP_SIZE);
15376e8c542Schristos 			FREE(resp);
15476e8c542Schristos 		}
15576e8c542Schristos 		strlset(resp2, 0, PAM_MAX_RESP_SIZE);
15676e8c542Schristos 		FREE(resp2);
15776e8c542Schristos 	}
15876e8c542Schristos 	if (resp == NULL)
15976e8c542Schristos 		RETURNC(PAM_TRY_AGAIN);
16076e8c542Schristos 	r = pam_set_item(pamh, item, resp);
16176e8c542Schristos 	strlset(resp, 0, PAM_MAX_RESP_SIZE);
16276e8c542Schristos 	FREE(resp);
16376e8c542Schristos 	if (r != PAM_SUCCESS)
16476e8c542Schristos 		RETURNC(r);
16576e8c542Schristos 	r = pam_get_item(pamh, item, (const void **)authtok);
16676e8c542Schristos 	RETURNC(r);
16776e8c542Schristos }
16876e8c542Schristos 
16976e8c542Schristos /*
17076e8c542Schristos  * Error codes:
17176e8c542Schristos  *
17276e8c542Schristos  *	=pam_get_item
17376e8c542Schristos  *	=pam_prompt
17476e8c542Schristos  *	=pam_set_item
17576e8c542Schristos  *	!PAM_SYMBOL_ERR
1764cb4af11Schristos  *	PAM_BAD_CONSTANT
17776e8c542Schristos  *	PAM_TRY_AGAIN
17876e8c542Schristos  */
17976e8c542Schristos 
18076e8c542Schristos /**
18176e8c542Schristos  * The =pam_get_authtok function either prompts the user for an
18276e8c542Schristos  * authentication token or retrieves a cached authentication token,
18376e8c542Schristos  * depending on circumstances.
18476e8c542Schristos  * Either way, a pointer to the authentication token is stored in the
18576e8c542Schristos  * location pointed to by the =authtok argument, and the corresponding PAM
18676e8c542Schristos  * item is updated.
18776e8c542Schristos  *
18876e8c542Schristos  * The =item argument must have one of the following values:
18976e8c542Schristos  *
19076e8c542Schristos  *	=PAM_AUTHTOK:
19176e8c542Schristos  *		Returns the current authentication token, or the new token
19276e8c542Schristos  *		when changing authentication tokens.
19376e8c542Schristos  *	=PAM_OLDAUTHTOK:
19476e8c542Schristos  *		Returns the previous authentication token when changing
19576e8c542Schristos  *		authentication tokens.
19676e8c542Schristos  *
19776e8c542Schristos  * The =prompt argument specifies a prompt to use if no token is cached.
19876e8c542Schristos  * If it is =NULL, the =PAM_AUTHTOK_PROMPT or =PAM_OLDAUTHTOK_PROMPT item,
19976e8c542Schristos  * as appropriate, will be used.
20076e8c542Schristos  * If that item is also =NULL, a hardcoded default prompt will be used.
20176e8c542Schristos  * Additionally, when =pam_get_authtok is called from a service module,
20276e8c542Schristos  * the prompt may be affected by module options as described below.
20376e8c542Schristos  * The prompt is then expanded using =openpam_subst before it is passed to
20476e8c542Schristos  * the conversation function.
20576e8c542Schristos  *
20676e8c542Schristos  * If =item is set to =PAM_AUTHTOK and there is a non-null =PAM_OLDAUTHTOK
20776e8c542Schristos  * item, =pam_get_authtok will ask the user to confirm the new token by
20876e8c542Schristos  * retyping it.
20976e8c542Schristos  * If there is a mismatch, =pam_get_authtok will return =PAM_TRY_AGAIN.
21076e8c542Schristos  *
21176e8c542Schristos  * MODULE OPTIONS
21276e8c542Schristos  *
21376e8c542Schristos  * When called by a service module, =pam_get_authtok will recognize the
21476e8c542Schristos  * following module options:
21576e8c542Schristos  *
21676e8c542Schristos  *	;authtok_prompt:
21776e8c542Schristos  *		Prompt to use when =item is set to =PAM_AUTHTOK.
21876e8c542Schristos  *		This option overrides both the =prompt argument and the
21976e8c542Schristos  *		=PAM_AUTHTOK_PROMPT item.
22076e8c542Schristos  *	;echo_pass:
22176e8c542Schristos  *		If the application's conversation function allows it, this
22276e8c542Schristos  *		lets the user see what they are typing.
22376e8c542Schristos  *		This should only be used for non-reusable authentication
22476e8c542Schristos  *		tokens.
22576e8c542Schristos  *	;oldauthtok_prompt:
22676e8c542Schristos  *		Prompt to use when =item is set to =PAM_OLDAUTHTOK.
22776e8c542Schristos  *		This option overrides both the =prompt argument and the
22876e8c542Schristos  *		=PAM_OLDAUTHTOK_PROMPT item.
22976e8c542Schristos  *	;try_first_pass:
23076e8c542Schristos  *		If the requested item is non-null, return it without
23176e8c542Schristos  *		prompting the user.
23276e8c542Schristos  *		Typically, the service module will verify the token, and
23376e8c542Schristos  *		if it does not match, clear the item before calling
23476e8c542Schristos  *		=pam_get_authtok a second time.
23576e8c542Schristos  *	;use_first_pass:
23676e8c542Schristos  *		Do not prompt the user at all; just return the cached
23776e8c542Schristos  *		value, or =PAM_AUTH_ERR if there is none.
23876e8c542Schristos  *
23976e8c542Schristos  * >pam_conv
24076e8c542Schristos  * >pam_get_item
24176e8c542Schristos  * >pam_get_user
24276e8c542Schristos  * >openpam_get_option
24376e8c542Schristos  * >openpam_subst
24476e8c542Schristos  */
245