xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/pam_get_user.c (revision 0d9d0fd8a30be9a1924e715bbcf67a4a83efd262)
1 /*	$NetBSD: pam_get_user.c,v 1.4 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_user.c,v 1.4 2023/06/30 21:46:21 christos Exp $");
44 
45 #include <sys/param.h>
46 
47 #include <stdlib.h>
48 
49 #include <security/pam_appl.h>
50 #include <security/openpam.h>
51 
52 #include "openpam_impl.h"
53 
54 static const char user_prompt[] = "Login:";
55 
56 /*
57  * XSSO 4.2.1
58  * XSSO 6 page 52
59  *
60  * Retrieve user name
61  */
62 
63 int
pam_get_user(pam_handle_t * pamh,const char ** user,const char * prompt)64 pam_get_user(pam_handle_t *pamh,
65 	const char **user,
66 	const char *prompt)
67 {
68 	char prompt_buf[1024];
69 	size_t prompt_size;
70 	const void *promptp;
71 	char *resp;
72 	int r;
73 
74 	ENTER();
75 	r = pam_get_item(pamh, PAM_USER, (const void **)user);
76 	if (r == PAM_SUCCESS && *user != NULL)
77 		RETURNC(PAM_SUCCESS);
78 	/* pam policy overrides the module's choice */
79 	if ((promptp = openpam_get_option(pamh, "user_prompt")) != NULL)
80 		prompt = promptp;
81 	/* no prompt provided, see if there is one tucked away somewhere */
82 	if (prompt == NULL) {
83 		r = pam_get_item(pamh, PAM_USER_PROMPT, &promptp);
84 		if (r == PAM_SUCCESS && promptp != NULL)
85 			prompt = promptp;
86 	}
87 	/* fall back to hardcoded default */
88 	if (prompt == NULL)
89 		prompt = user_prompt;
90 	/* expand */
91 	prompt_size = sizeof prompt_buf;
92 	r = openpam_subst(pamh, prompt_buf, &prompt_size, prompt);
93 	if (r == PAM_SUCCESS && prompt_size <= sizeof prompt_buf)
94 		prompt = prompt_buf;
95 	r = pam_prompt(pamh, PAM_PROMPT_ECHO_ON, &resp, "%s", prompt);
96 	if (r != PAM_SUCCESS)
97 		RETURNC(r);
98 	r = pam_set_item(pamh, PAM_USER, resp);
99 	FREE(resp);
100 	if (r != PAM_SUCCESS)
101 		RETURNC(r);
102 	r = pam_get_item(pamh, PAM_USER, (const void **)user);
103 	RETURNC(r);
104 }
105 
106 /*
107  * Error codes:
108  *
109  *	=pam_get_item
110  *	=pam_prompt
111  *	=pam_set_item
112  *	!PAM_SYMBOL_ERR
113  */
114 
115 /**
116  * The =pam_get_user function returns the name of the target user, as
117  * specified to =pam_start.
118  * If no user was specified, nor set using =pam_set_item, =pam_get_user
119  * will prompt for a user name.
120  * Either way, a pointer to the user name is stored in the location
121  * pointed to by the =user argument, and the corresponding PAM item is
122  * updated.
123  *
124  * The =prompt argument specifies a prompt to use if no user name is
125  * cached.
126  * If it is =NULL, the =PAM_USER_PROMPT item will be used.
127  * If that item is also =NULL, a hardcoded default prompt will be used.
128  * Additionally, when =pam_get_user is called from a service module, the
129  * prompt may be affected by module options as described below.
130  * The prompt is then expanded using =openpam_subst before it is passed to
131  * the conversation function.
132  *
133  * MODULE OPTIONS
134  *
135  * When called by a service module, =pam_get_user will recognize the
136  * following module options:
137  *
138  *	;user_prompt:
139  *		Prompt to use when asking for the user name.
140  *		This option overrides both the =prompt argument and the
141  *		=PAM_USER_PROMPT item.
142  *
143  * >pam_conv
144  * >pam_get_item
145  * >pam_get_authtok
146  * >openpam_get_option
147  * >openpam_subst
148  */
149