2 * $Id: misc_conv.c,v 1.5 1997/01/04 20:16:48 morgan Exp morgan $
3 * $FreeBSD: src/contrib/libpam/libpam_misc/misc_conv.c,v 1.1.1.1.6.3 2003/02/10 12:15:30 des Exp $
4 * $DragonFly: src/contrib/libpam/libpam_misc/Attic/misc_conv.c,v 1.2 2003/06/17 04:24:03 dillon Exp $
6 * A generic conversation function for text based applications
8 * Written by Andrew Morgan <morgan@linux.kernel.org>
10 * $Log: misc_conv.c,v $
11 * Revision 1.5 1997/01/04 20:16:48 morgan
12 * removed getpass. Replaced with POSIX code for same function which
13 * also observes timeouts specified by the parent application
15 * Revision 1.4 1996/12/01 03:26:51 morgan
16 * *** empty log message ***
18 * Revision 1.3 1996/11/10 20:10:01 morgan
21 * Revision 1.2 1996/07/07 23:59:56 morgan
22 * changed the name of the misc include file
24 * Revision 1.1 1996/05/02 05:17:06 morgan
37 #include <sys/types.h>
42 #include <security/pam_appl.h>
43 #include <security/pam_misc.h>
45 #define INPUTSIZE PAM_MAX_MSG_SIZE /* maximum length of input+1 */
46 #define CONV_ECHO_ON 1 /* types of echo state */
47 #define CONV_ECHO_OFF 0
50 * external timeout definitions - these can be overriden by the
54 time_t pam_misc_conv_warn_time = 0; /* time when we warn */
55 time_t pam_misc_conv_die_time = 0; /* time when we timeout */
57 const char *pam_misc_conv_warn_line = "..\a.Time is running out...\n";
58 const char *pam_misc_conv_die_line = "..\a.Sorry, your time is up!\n";
60 int pam_misc_conv_died=0; /* application can probe this for timeout */
62 static void pam_misc_conv_delete_binary(void **delete_me)
64 if (delete_me && *delete_me) {
65 unsigned char *packet = *(unsigned char **)delete_me;
68 length = 4+(packet[0]<<24)+(packet[1]<<16)+(packet[2]<<8)+packet[3];
69 memset(packet, 0, length);
71 *delete_me = packet = NULL;
75 /* These function pointers are for application specific binary
76 conversations. One or both of the arguments to the first function
77 must be non-NULL. The first function must return PAM_SUCCESS or
78 PAM_CONV_ERR. If input is non-NULL, a response is expected, this
79 response should be malloc()'d and will eventually be free()'d by
80 the calling module. The structure of this malloc()'d response is as
83 { int length, char data[length] }
85 For convenience, the pointer used by the two function pointer
86 prototypes is 'void *'.
88 The ...free() fn pointer is used to discard a binary message that
89 is not of the default form. It should be explicitly overwritten
90 when using some other convention for the structure of a binary
91 prompt (not recommended). */
93 int (*pam_binary_handler_fn)(const void *send, void **receive) = NULL;
94 void (*pam_binary_handler_free)(void **packet_p) = pam_misc_conv_delete_binary;
96 /* the following code is used to get text input */
98 volatile static int expired=0;
100 /* return to the previous signal handling */
101 static void reset_alarm(struct sigaction *o_ptr)
103 (void) alarm(0); /* stop alarm clock - if still ticking */
104 (void) sigaction(SIGALRM, o_ptr, NULL);
107 /* this is where we intercept the alarm signal */
108 static void time_is_up(int ignore)
113 /* set the new alarm to hit the time_is_up() function */
114 static int set_alarm(int delay, struct sigaction *o_ptr)
116 struct sigaction new_sig;
118 sigemptyset(&new_sig.sa_mask);
119 new_sig.sa_flags = 0;
120 new_sig.sa_handler = time_is_up;
121 if ( sigaction(SIGALRM, &new_sig, o_ptr) ) {
122 return 1; /* setting signal failed */
124 if ( alarm(delay) ) {
125 (void) sigaction(SIGALRM, o_ptr, NULL);
126 return 1; /* failed to set alarm */
128 return 0; /* all seems to have worked */
131 /* return the number of seconds to next alarm. 0 = no delay, -1 = expired */
132 static int get_delay(void)
136 expired = 0; /* reset flag */
139 /* has the quit time past? */
140 if (pam_misc_conv_die_time && now >= pam_misc_conv_die_time) {
141 fprintf(stderr,"%s",pam_misc_conv_die_line);
143 pam_misc_conv_died = 1; /* note we do not reset the die_time */
144 return -1; /* time is up */
147 /* has the warning time past? */
148 if (pam_misc_conv_warn_time && now >= pam_misc_conv_warn_time) {
149 fprintf(stderr, "%s", pam_misc_conv_warn_line);
150 pam_misc_conv_warn_time = 0; /* reset warn_time */
152 /* indicate remaining delay - if any */
154 return (pam_misc_conv_die_time ? pam_misc_conv_die_time - now:0 );
157 /* indicate possible warning delay */
159 if (pam_misc_conv_warn_time)
160 return (pam_misc_conv_warn_time - now);
161 else if (pam_misc_conv_die_time)
162 return (pam_misc_conv_die_time - now);
167 /* read a line of input string, giving prompt when appropriate */
168 static char *read_string(int echo, const char *prompt)
170 struct termios term_before, term_tmp;
171 char line[INPUTSIZE];
172 struct sigaction old_sig;
173 int delay, nc, have_term=0;
175 D(("called with echo='%s', prompt='%s'.", echo ? "ON":"OFF" , prompt));
177 if (isatty(STDIN_FILENO)) { /* terminal state */
179 /* is a terminal so record settings and flush it */
180 if ( tcgetattr(STDIN_FILENO, &term_before) != 0 ) {
181 D(("<error: failed to get terminal settings>"));
184 memcpy(&term_tmp, &term_before, sizeof(term_tmp));
186 term_tmp.c_lflag |= ECHO;
188 term_tmp.c_lflag &= ~(ECHO);
193 D(("<warning: cannot turn echo off>"));
196 /* set up the signal handling */
199 /* reading the line */
202 fprintf(stderr, "%s", prompt);
203 /* this may, or may not set echo off -- drop pending input */
205 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_tmp);
207 if ( delay > 0 && set_alarm(delay, &old_sig) ) {
208 D(("<failed to set alarm>"));
211 nc = read(STDIN_FILENO, line, INPUTSIZE-1);
213 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &term_before);
214 if (!echo || expired) /* do we need a newline? */
215 fprintf(stderr,"\n");
218 reset_alarm(&old_sig);
222 } else if (nc > 0) { /* we got some user input */
225 if (nc > 0 && line[nc-1] == '\n') { /* <NUL> terminate */
230 input = x_strdup(line);
231 _pam_overwrite(line);
233 return input; /* return malloc()ed string */
234 } else if (nc == 0) { /* Ctrl-D */
235 D(("user did not want to type anything"));
236 fprintf(stderr, "\n");
242 /* getting here implies that the timer expired */
244 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &term_before);
246 memset(line, 0, INPUTSIZE); /* clean up */
250 /* end of read_string functions */
252 int misc_conv(int num_msg, const struct pam_message **msgm,
253 struct pam_response **response, void *appdata_ptr)
256 struct pam_response *reply;
261 D(("allocating empty response structure array."));
263 reply = (struct pam_response *) calloc(num_msg,
264 sizeof(struct pam_response));
266 D(("no memory for responses"));
270 D(("entering conversation function."));
272 for (count=0; count < num_msg; ++count) {
275 switch (msgm[count]->msg_style) {
276 case PAM_PROMPT_ECHO_OFF:
277 string = read_string(CONV_ECHO_OFF,msgm[count]->msg);
278 if (string == NULL) {
279 goto failed_conversation;
282 case PAM_PROMPT_ECHO_ON:
283 string = read_string(CONV_ECHO_ON,msgm[count]->msg);
284 if (string == NULL) {
285 goto failed_conversation;
289 if (fprintf(stderr,"%s\n",msgm[count]->msg) < 0) {
290 goto failed_conversation;
294 if (fprintf(stdout,"%s\n",msgm[count]->msg) < 0) {
295 goto failed_conversation;
298 case PAM_BINARY_PROMPT:
301 const void *pack_in = msgm[count]->msg;
303 if (!pam_binary_handler_fn
304 || pam_binary_handler_fn(pack_in, &pack_out) != PAM_SUCCESS
305 || pack_out == NULL) {
306 goto failed_conversation;
308 string = (char *) pack_out;
315 const void *pack_in = msgm[count]->msg;
316 if (!pam_binary_handler_fn
317 || pam_binary_handler_fn(pack_in, NULL) != PAM_SUCCESS) {
318 goto failed_conversation;
323 fprintf(stderr, "erroneous conversation (%d)\n"
324 ,msgm[count]->msg_style);
325 goto failed_conversation;
328 if (string) { /* must add to reply array */
329 /* add string to list of responses */
331 reply[count].resp_retcode = 0;
332 reply[count].resp = string;
337 /* New (0.59+) behavior is to always have a reply - this is
338 compatable with the X/Open (March 1997) spec. */
347 for (count=0; count<num_msg; ++count) {
348 if (reply[count].resp == NULL) {
351 switch (msgm[count]->msg_style) {
352 case PAM_PROMPT_ECHO_ON:
353 case PAM_PROMPT_ECHO_OFF:
354 _pam_overwrite(reply[count].resp);
355 free(reply[count].resp);
357 case PAM_BINARY_PROMPT:
358 pam_binary_handler_free((void **) &reply[count].resp);
363 /* should not actually be able to get here... */
364 free(reply[count].resp);
366 reply[count].resp = NULL;
368 /* forget reply too */