xref: /netbsd-src/external/bsd/openpam/dist/lib/libpam/pam_vprompt.c (revision 4cb4af11b1521f1626a9baaf40ae4f58e0929844)
1 /*	$NetBSD: pam_vprompt.c,v 1.3 2017/05/06 19:50:10 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
5  * Copyright (c) 2004-2011 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  * $OpenPAM: pam_vprompt.c 938 2017-04-30 21:34:42Z des $
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43 
44 #include <sys/cdefs.h>
45 __RCSID("$NetBSD: pam_vprompt.c,v 1.3 2017/05/06 19:50:10 christos Exp $");
46 
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 
51 #include <security/pam_appl.h>
52 
53 #include "openpam_impl.h"
54 
55 /*
56  * OpenPAM extension
57  *
58  * Call the conversation function
59  */
60 
61 int
62 pam_vprompt(const pam_handle_t *pamh,
63 	int style,
64 	char **resp,
65 	const char *fmt,
66 	va_list ap)
67 {
68 	char msgbuf[PAM_MAX_MSG_SIZE];
69 	struct pam_message msg;
70 	const struct pam_message *msgp;
71 	struct pam_response *rsp;
72 	const struct pam_conv *conv;
73 	const void *convp;
74 	int r;
75 
76 	ENTER();
77 	r = pam_get_item(pamh, PAM_CONV, &convp);
78 	if (r != PAM_SUCCESS)
79 		RETURNC(r);
80 	conv = convp;
81 	if (conv == NULL || conv->conv == NULL) {
82 		openpam_log(PAM_LOG_ERROR, "no conversation function");
83 		RETURNC(PAM_SYSTEM_ERR);
84 	}
85 	vsnprintf(msgbuf, (size_t)PAM_MAX_MSG_SIZE, fmt, ap);
86 	msg.msg_style = style;
87 	msg.msg = msgbuf;
88 	msgp = &msg;
89 	rsp = NULL;
90 	r = (conv->conv)(1, &msgp, &rsp, conv->appdata_ptr);
91 	*resp = rsp == NULL ? NULL : rsp->resp;
92 	FREE(rsp);
93 	RETURNC(r);
94 }
95 
96 /*
97  * Error codes:
98  *
99  *     !PAM_SYMBOL_ERR
100  *	PAM_SYSTEM_ERR
101  *	PAM_BUF_ERR
102  *	PAM_CONV_ERR
103  */
104 
105 /**
106  * The =pam_vprompt function constructs a string from the =fmt and =ap
107  * arguments using =vsnprintf, and passes it to the given PAM context's
108  * conversation function.
109  *
110  * The =style argument specifies the type of interaction requested, and
111  * must be one of the following:
112  *
113  *	=PAM_PROMPT_ECHO_OFF:
114  *		Display the message and obtain the user's response without
115  *		displaying it.
116  *	=PAM_PROMPT_ECHO_ON:
117  *		Display the message and obtain the user's response.
118  *	=PAM_ERROR_MSG:
119  *		Display the message as an error message, and do not wait
120  *		for a response.
121  *	=PAM_TEXT_INFO:
122  *		Display the message as an informational message, and do
123  *		not wait for a response.
124  *
125  * A pointer to the response, or =NULL if the conversation function did
126  * not return one, is stored in the location pointed to by the =resp
127  * argument.
128  *
129  * The message and response should not exceed =PAM_MAX_MSG_SIZE or
130  * =PAM_MAX_RESP_SIZE, respectively.
131  * If they do, they may be truncated.
132  *
133  * >pam_error
134  * >pam_info
135  * >pam_prompt
136  * >pam_verror
137  * >pam_vinfo
138  */
139