openpam_ttyconv.c revision 147455
1/*- 2 * Copyright (c) 2002-2003 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by ThinkSec AS and 6 * Network Associates Laboratories, the Security Research Division of 7 * Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 8 * ("CBOSS"), as part of the DARPA CHATS research program. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote 19 * products derived from this software without specific prior written 20 * permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $P4: //depot/projects/openpam/lib/openpam_ttyconv.c#26 $ 35 */ 36 37#include <sys/types.h> 38 39#include <ctype.h> 40#include <errno.h> 41#include <setjmp.h> 42#include <signal.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <termios.h> 47#include <unistd.h> 48 49#include <security/pam_appl.h> 50 51#include "openpam_impl.h" 52 53int openpam_ttyconv_timeout = 0; 54 55static void 56timeout(int sig) 57{ 58 59 (void)sig; 60} 61 62static char * 63prompt(const char *msg) 64{ 65 char buf[PAM_MAX_RESP_SIZE]; 66 struct sigaction action, saved_action; 67 sigset_t saved_sigset, sigset; 68 unsigned int saved_alarm; 69 int eof, error, fd; 70 size_t len; 71 char *retval; 72 char ch; 73 74 sigemptyset(&sigset); 75 sigaddset(&sigset, SIGINT); 76 sigaddset(&sigset, SIGTSTP); 77 sigprocmask(SIG_SETMASK, &sigset, &saved_sigset); 78 action.sa_handler = &timeout; 79 action.sa_flags = 0; 80 sigemptyset(&action.sa_mask); 81 sigaction(SIGALRM, &action, &saved_action); 82 fputs(msg, stdout); 83 fflush(stdout); 84#ifdef HAVE_FPURGE 85 fpurge(stdin); 86#endif 87 fd = fileno(stdin); 88 buf[0] = '\0'; 89 eof = error = 0; 90 if (openpam_ttyconv_timeout >= 0) 91 saved_alarm = alarm(openpam_ttyconv_timeout); 92 ch = '\0'; 93 for (len = 0; ch != '\n' && !eof && !error; ++len) { 94 switch (read(fd, &ch, 1)) { 95 case 1: 96 if (len < PAM_MAX_RESP_SIZE - 1) { 97 buf[len + 1] = '\0'; 98 buf[len] = ch; 99 } 100 break; 101 case 0: 102 eof = 1; 103 break; 104 default: 105 error = errno; 106 break; 107 } 108 } 109 if (openpam_ttyconv_timeout >= 0) 110 alarm(0); 111 sigaction(SIGALRM, &saved_action, NULL); 112 sigprocmask(SIG_SETMASK, &saved_sigset, NULL); 113 if (openpam_ttyconv_timeout >= 0) 114 alarm(saved_alarm); 115 if (error == EINTR) 116 fputs(" timeout!", stderr); 117 if (error || eof) { 118 fputs("\n", stderr); 119 memset(buf, 0, sizeof(buf)); 120 return (NULL); 121 } 122 /* trim trailing whitespace */ 123 for (len = strlen(buf); len > 0; --len) 124 if (buf[len - 1] != '\r' && buf[len - 1] != '\n') 125 break; 126 buf[len] = '\0'; 127 retval = strdup(buf); 128 memset(buf, 0, sizeof(buf)); 129 return (retval); 130} 131 132static char * 133prompt_echo_off(const char *msg) 134{ 135 struct termios tattr; 136 tcflag_t lflag; 137 char *ret; 138 int fd; 139 140 fd = fileno(stdin); 141 if (tcgetattr(fd, &tattr) != 0) { 142 openpam_log(PAM_LOG_ERROR, "tcgetattr(): %m"); 143 return (NULL); 144 } 145 lflag = tattr.c_lflag; 146 tattr.c_lflag &= ~ECHO; 147 if (tcsetattr(fd, TCSAFLUSH, &tattr) != 0) { 148 openpam_log(PAM_LOG_ERROR, "tcsetattr(): %m"); 149 return (NULL); 150 } 151 ret = prompt(msg); 152 tattr.c_lflag = lflag; 153 (void)tcsetattr(fd, TCSANOW, &tattr); 154 if (ret != NULL) 155 fputs("\n", stdout); 156 return (ret); 157} 158 159/* 160 * OpenPAM extension 161 * 162 * Simple tty-based conversation function 163 */ 164 165int 166openpam_ttyconv(int n, 167 const struct pam_message **msg, 168 struct pam_response **resp, 169 void *data) 170{ 171 struct pam_response *aresp; 172 int i; 173 174 ENTER(); 175 (void)data; 176 if (n <= 0 || n > PAM_MAX_NUM_MSG) 177 RETURNC(PAM_CONV_ERR); 178 if ((aresp = calloc(n, sizeof *aresp)) == NULL) 179 RETURNC(PAM_BUF_ERR); 180 for (i = 0; i < n; ++i) { 181 aresp[i].resp_retcode = 0; 182 aresp[i].resp = NULL; 183 switch (msg[i]->msg_style) { 184 case PAM_PROMPT_ECHO_OFF: 185 aresp[i].resp = prompt_echo_off(msg[i]->msg); 186 if (aresp[i].resp == NULL) 187 goto fail; 188 break; 189 case PAM_PROMPT_ECHO_ON: 190 aresp[i].resp = prompt(msg[i]->msg); 191 if (aresp[i].resp == NULL) 192 goto fail; 193 break; 194 case PAM_ERROR_MSG: 195 fputs(msg[i]->msg, stderr); 196 if (strlen(msg[i]->msg) > 0 && 197 msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n') 198 fputc('\n', stderr); 199 break; 200 case PAM_TEXT_INFO: 201 fputs(msg[i]->msg, stdout); 202 if (strlen(msg[i]->msg) > 0 && 203 msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n') 204 fputc('\n', stdout); 205 break; 206 default: 207 goto fail; 208 } 209 } 210 *resp = aresp; 211 RETURNC(PAM_SUCCESS); 212 fail: 213 for (i = 0; i < n; ++i) { 214 if (aresp[i].resp != NULL) { 215 memset(aresp[i].resp, 0, strlen(aresp[i].resp)); 216 FREE(aresp[i].resp); 217 } 218 } 219 memset(aresp, 0, n * sizeof *aresp); 220 FREE(aresp); 221 *resp = NULL; 222 RETURNC(PAM_CONV_ERR); 223} 224 225/* 226 * Error codes: 227 * 228 * PAM_SYSTEM_ERR 229 * PAM_BUF_ERR 230 * PAM_CONV_ERR 231 */ 232 233/** 234 * The =openpam_ttyconv function is a standard conversation function 235 * suitable for use on TTY devices. 236 * It should be adequate for the needs of most text-based interactive 237 * programs. 238 * 239 * The =openpam_ttyconv function allows the application to specify a 240 * timeout for user input by setting the global integer variable 241 * :openpam_ttyconv_timeout to the length of the timeout in seconds. 242 * 243 * >openpam_nullconv 244 * >pam_prompt 245 * >pam_vprompt 246 */ 247