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 *
35255376Sdes * $Id: pam_get_user.c 670 2013-03-17 19:26:07Z 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>
4593982Sdes
4691094Sdes#include <security/pam_appl.h>
4791094Sdes#include <security/openpam.h>
4891094Sdes
4991094Sdes#include "openpam_impl.h"
5091094Sdes
51117610Sdesstatic const char user_prompt[] = "Login:";
5293982Sdes
5391094Sdes/*
5491094Sdes * XSSO 4.2.1
5591094Sdes * XSSO 6 page 52
5691094Sdes *
5791094Sdes * Retrieve user name
5891094Sdes */
5991094Sdes
6091094Sdesint
6191094Sdespam_get_user(pam_handle_t *pamh,
6291094Sdes	const char **user,
6391094Sdes	const char *prompt)
6491094Sdes{
65228690Sdes	char prompt_buf[1024];
66228690Sdes	size_t prompt_size;
67125647Sdes	const void *promptp;
6893982Sdes	char *resp;
6991094Sdes	int r;
7091094Sdes
71107937Sdes	ENTER();
7291094Sdes	if (pamh == NULL || user == NULL)
73107937Sdes		RETURNC(PAM_SYSTEM_ERR);
7491094Sdes	r = pam_get_item(pamh, PAM_USER, (const void **)user);
75117610Sdes	if (r == PAM_SUCCESS && *user != NULL)
76107937Sdes		RETURNC(PAM_SUCCESS);
77228690Sdes	/* pam policy overrides the module's choice */
78228690Sdes	if ((promptp = openpam_get_option(pamh, "user_prompt")) != NULL)
79228690Sdes		prompt = promptp;
80228690Sdes	/* no prompt provided, see if there is one tucked away somewhere */
81228690Sdes	if (prompt == NULL)
82228690Sdes		if (pam_get_item(pamh, PAM_USER_PROMPT, &promptp) &&
83228690Sdes		    promptp != NULL)
84125647Sdes			prompt = promptp;
85228690Sdes	/* fall back to hardcoded default */
86228690Sdes	if (prompt == NULL)
87228690Sdes		prompt = user_prompt;
88228690Sdes	/* expand */
89228690Sdes	prompt_size = sizeof prompt_buf;
90228690Sdes	r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt);
91228690Sdes	if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf)
92228690Sdes		prompt = prompt_buf;
9393982Sdes	r = pam_prompt(pamh, PAM_PROMPT_ECHO_ON, &resp, "%s", prompt);
9491094Sdes	if (r != PAM_SUCCESS)
95107937Sdes		RETURNC(r);
9693982Sdes	r = pam_set_item(pamh, PAM_USER, resp);
97115619Sdes	FREE(resp);
9893982Sdes	if (r != PAM_SUCCESS)
99107937Sdes		RETURNC(r);
100110556Sdes	r = pam_get_item(pamh, PAM_USER, (const void **)user);
101110556Sdes	RETURNC(r);
10291094Sdes}
10391100Sdes
10491100Sdes/*
10591100Sdes * Error codes:
10691100Sdes *
10791100Sdes *	=pam_get_item
10891100Sdes *	=pam_prompt
10991100Sdes *	=pam_set_item
11091100Sdes *	!PAM_SYMBOL_ERR
11191100Sdes */
11293982Sdes
11393982Sdes/**
11493982Sdes * The =pam_get_user function returns the name of the target user, as
115141098Sdes * specified to =pam_start.
116141098Sdes * If no user was specified, nor set using =pam_set_item, =pam_get_user
117141098Sdes * will prompt for a user name.
118141098Sdes * Either way, a pointer to the user name is stored in the location
119255376Sdes * pointed to by the =user argument, and the corresponding PAM item is
120255376Sdes * updated.
121141098Sdes *
12293982Sdes * The =prompt argument specifies a prompt to use if no user name is
123141098Sdes * cached.
124228690Sdes * If it is =NULL, the =PAM_USER_PROMPT item will be used.
125141098Sdes * If that item is also =NULL, a hardcoded default prompt will be used.
126255376Sdes * Additionally, when =pam_get_user is called from a service module, the
127255376Sdes * prompt may be affected by module options as described below.
128255376Sdes * The prompt is then expanded using =openpam_subst before it is passed to
129255376Sdes * the conversation function.
13093982Sdes *
131255376Sdes * MODULE OPTIONS
132228690Sdes *
133255376Sdes * When called by a service module, =pam_get_user will recognize the
134255376Sdes * following module options:
135255376Sdes *
136255376Sdes *	;user_prompt:
137255376Sdes *		Prompt to use when asking for the user name.
138255376Sdes *		This option overrides both the =prompt argument and the
139255376Sdes *		=PAM_USER_PROMPT item.
140255376Sdes *
141255376Sdes * >pam_conv
14293982Sdes * >pam_get_item
14393982Sdes * >pam_get_authtok
144255376Sdes * >openpam_get_option
145228690Sdes * >openpam_subst
14693982Sdes */
147