2 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
3 * Copyright (c) 2004-2014 Dag-Erling Smørgrav
6 * This software was developed for the FreeBSD Project by ThinkSec AS and
7 * Network Associates Laboratories, the Security Research Division of
8 * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
9 * ("CBOSS"), as part of the DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $OpenPAM: openpam_ttyconv.c 938 2017-04-30 21:34:42Z des $
42 #include <sys/types.h>
55 #include <security/pam_appl.h>
57 #include "openpam_impl.h"
58 #include "openpam_strlset.h"
60 int openpam_ttyconv_timeout = 0;
62 static volatile sig_atomic_t caught_signal;
65 * Handle incoming signals during tty conversation
68 catch_signal(int signo)
75 caught_signal = signo;
81 * Accept a response from the user on a tty
84 prompt_tty(int ifd, int ofd, const char *message, char *response, int echo)
86 struct sigaction action;
87 struct sigaction saction_sigint, saction_sigquit, saction_sigterm;
88 struct termios tcattr;
89 struct timeval now, target, remaining;
98 if (write(ofd, message, strlen(message)) < 0) {
99 openpam_log(PAM_LOG_ERROR, "write(): %m");
103 /* turn echo off if requested */
104 slflag = 0; /* prevent bogus uninitialized variable warning */
106 if (tcgetattr(ifd, &tcattr) != 0) {
107 openpam_log(PAM_LOG_ERROR, "tcgetattr(): %m");
110 slflag = tcattr.c_lflag;
111 tcattr.c_lflag &= ~ECHO;
112 if (tcsetattr(ifd, TCSAFLUSH, &tcattr) != 0) {
113 openpam_log(PAM_LOG_ERROR, "tcsetattr(): %m");
118 /* install signal handlers */
120 action.sa_handler = &catch_signal;
122 sigfillset(&action.sa_mask);
123 sigaction(SIGINT, &action, &saction_sigint);
124 sigaction(SIGQUIT, &action, &saction_sigquit);
125 sigaction(SIGTERM, &action, &saction_sigterm);
127 /* compute timeout */
128 if (openpam_ttyconv_timeout > 0) {
129 (void)gettimeofday(&now, NULL);
130 remaining.tv_sec = openpam_ttyconv_timeout;
131 remaining.tv_usec = 0;
132 timeradd(&now, &remaining, &target);
134 /* prevent bogus uninitialized variable warning */
135 now.tv_sec = now.tv_usec = 0;
136 remaining.tv_sec = remaining.tv_usec = 0;
137 target.tv_sec = target.tv_usec = 0;
144 while (!caught_signal) {
148 if (openpam_ttyconv_timeout > 0) {
149 gettimeofday(&now, NULL);
150 if (timercmp(&now, &target, >))
152 timersub(&target, &now, &remaining);
153 remaining_ms = remaining.tv_sec * 1000 +
154 remaining.tv_usec / 1000;
158 if ((ret = poll(&pfd, 1, remaining_ms)) < 0) {
162 openpam_log(PAM_LOG_ERROR, "poll(): %m");
164 } else if (ret == 0) {
166 write(ofd, " timed out", 10);
167 openpam_log(PAM_LOG_NOTICE, "timed out");
170 if ((ret = read(ifd, &ch, 1)) < 0) {
172 openpam_log(PAM_LOG_ERROR, "read(): %m");
174 } else if (ret == 0 || ch == '\n') {
175 response[pos] = '\0';
179 if (pos + 1 < PAM_MAX_RESP_SIZE)
180 response[pos++] = ch;
181 /* overflow is discarded */
184 /* restore tty state */
186 tcattr.c_lflag = slflag;
187 if (tcsetattr(ifd, 0, &tcattr) != 0) {
188 /* treat as non-fatal, since we have our answer */
189 openpam_log(PAM_LOG_NOTICE, "tcsetattr(): %m");
193 /* restore signal handlers and re-post caught signal*/
194 sigaction(SIGINT, &saction_sigint, NULL);
195 sigaction(SIGQUIT, &saction_sigquit, NULL);
196 sigaction(SIGTERM, &saction_sigterm, NULL);
197 if (caught_signal != 0) {
198 openpam_log(PAM_LOG_ERROR, "caught signal %d",
200 raise((int)caught_signal);
201 /* if raise() had no effect... */
213 * Accept a response from the user on a non-tty stdin.
216 prompt_notty(const char *message, char *response)
218 struct timeval now, target, remaining;
224 fputs(message, stdout);
227 /* compute timeout */
228 if (openpam_ttyconv_timeout > 0) {
229 (void)gettimeofday(&now, NULL);
230 remaining.tv_sec = openpam_ttyconv_timeout;
231 remaining.tv_usec = 0;
232 timeradd(&now, &remaining, &target);
234 /* prevent bogus uninitialized variable warning */
235 now.tv_sec = now.tv_usec = 0;
236 remaining.tv_sec = remaining.tv_usec = 0;
237 target.tv_sec = target.tv_usec = 0;
243 pfd.fd = STDIN_FILENO;
246 if (openpam_ttyconv_timeout > 0) {
247 gettimeofday(&now, NULL);
248 if (timercmp(&now, &target, >))
250 timersub(&target, &now, &remaining);
251 remaining_ms = remaining.tv_sec * 1000 +
252 remaining.tv_usec / 1000;
256 if ((ret = poll(&pfd, 1, remaining_ms)) < 0) {
257 /* interrupt is ok, everything else -> bail */
260 perror("\nopenpam_ttyconv");
262 } else if (ret == 0) {
267 if ((ch = getchar()) == EOF && ferror(stdin)) {
268 perror("\nopenpam_ttyconv");
271 if (ch == EOF || ch == '\n') {
272 response[pos] = '\0';
275 if (pos + 1 < PAM_MAX_RESP_SIZE)
276 response[pos++] = ch;
277 /* overflow is discarded */
280 fputs("\nopenpam_ttyconv: timeout\n", stderr);
285 * Determine whether stdin is a tty; if not, try to open the tty; in
286 * either case, call the appropriate method.
289 prompt(const char *message, char *response, int echo)
293 if (isatty(STDIN_FILENO)) {
301 if ((ifd = open("/dev/tty", O_RDWR)) < 0)
302 /* no way to prevent echo */
303 return (prompt_notty(message, response));
306 ret = prompt_tty(ifd, ofd, message, response, echo);
307 if (ifd != STDIN_FILENO)
315 * Simple tty-based conversation function
319 openpam_ttyconv(int n,
320 const struct pam_message **msg,
321 struct pam_response **resp,
324 char respbuf[PAM_MAX_RESP_SIZE];
325 struct pam_response *aresp;
330 if (n <= 0 || n > PAM_MAX_NUM_MSG)
331 RETURNC(PAM_CONV_ERR);
332 if ((aresp = calloc(n, sizeof *aresp)) == NULL)
333 RETURNC(PAM_BUF_ERR);
334 for (i = 0; i < n; ++i) {
335 aresp[i].resp_retcode = 0;
336 aresp[i].resp = NULL;
337 switch (msg[i]->msg_style) {
338 case PAM_PROMPT_ECHO_OFF:
339 if (prompt(msg[i]->msg, respbuf, 0) < 0 ||
340 (aresp[i].resp = strdup(respbuf)) == NULL)
343 case PAM_PROMPT_ECHO_ON:
344 if (prompt(msg[i]->msg, respbuf, 1) < 0 ||
345 (aresp[i].resp = strdup(respbuf)) == NULL)
349 fputs(msg[i]->msg, stderr);
350 if (strlen(msg[i]->msg) > 0 &&
351 msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n')
355 fputs(msg[i]->msg, stdout);
356 if (strlen(msg[i]->msg) > 0 &&
357 msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n')
365 memset(respbuf, 0, sizeof respbuf);
366 RETURNC(PAM_SUCCESS);
368 for (i = 0; i < n; ++i) {
369 if (aresp[i].resp != NULL) {
370 strlset(aresp[i].resp, 0, PAM_MAX_RESP_SIZE);
374 memset(aresp, 0, n * sizeof *aresp);
377 memset(respbuf, 0, sizeof respbuf);
378 RETURNC(PAM_CONV_ERR);
390 * The =openpam_ttyconv function is a standard conversation function
391 * suitable for use on TTY devices.
392 * It should be adequate for the needs of most text-based interactive
395 * The =openpam_ttyconv function allows the application to specify a
396 * timeout for user input by setting the global integer variable
397 * :openpam_ttyconv_timeout to the length of the timeout in seconds.