4 * A generic conversation function for text based applications
6 * Written by Andrew Morgan <morgan@linux.kernel.org>
18 #include <sys/types.h>
23 #include <security/pam_appl.h>
24 #include <security/pam_misc.h>
26 #define INPUTSIZE PAM_MAX_MSG_SIZE /* maximum length of input+1 */
27 #define CONV_ECHO_ON 1 /* types of echo state */
28 #define CONV_ECHO_OFF 0
31 * external timeout definitions - these can be overriden by the
35 time_t pam_misc_conv_warn_time = 0; /* time when we warn */
36 time_t pam_misc_conv_die_time = 0; /* time when we timeout */
38 const char *pam_misc_conv_warn_line = "..\a.Time is running out...\n";
39 const char *pam_misc_conv_die_line = "..\a.Sorry, your time is up!\n";
41 int pam_misc_conv_died=0; /* application can probe this for timeout */
43 static void pam_misc_conv_delete_binary(void **delete_me)
45 if (delete_me && *delete_me) {
46 unsigned char *packet = *(unsigned char **)delete_me;
49 length = (packet[0]<<24)+(packet[1]<<16)+(packet[2]<<8)+packet[3];
50 memset(packet, 0, length);
52 *delete_me = packet = NULL;
56 /* These function pointers are for application specific binary
57 conversations. One or both of the arguments to the first function
58 must be non-NULL. The first function must return PAM_SUCCESS or
59 PAM_CONV_ERR. If input is non-NULL, a response is expected, this
60 response should be malloc()'d and will eventually be free()'d by
61 the calling module. The structure of this malloc()'d response is as
64 { int length, char data[length] }
66 For convenience, the pointer used by the two function pointer
67 prototypes is 'void *'.
69 The ...free() fn pointer is used to discard a binary message that
70 is not of the default form. It should be explicitly overwritten
71 when using some other convention for the structure of a binary
72 prompt (not recommended). */
74 int (*pam_binary_handler_fn)(const void *send, void **receive) = NULL;
75 void (*pam_binary_handler_free)(void **packet_p) = pam_misc_conv_delete_binary;
77 /* the following code is used to get text input */
79 volatile static int expired=0;
81 /* return to the previous signal handling */
82 static void reset_alarm(struct sigaction *o_ptr)
84 (void) alarm(0); /* stop alarm clock - if still ticking */
85 (void) sigaction(SIGALRM, o_ptr, NULL);
88 /* this is where we intercept the alarm signal */
89 static void time_is_up(int ignore)
94 /* set the new alarm to hit the time_is_up() function */
95 static int set_alarm(int delay, struct sigaction *o_ptr)
97 struct sigaction new_sig;
99 sigemptyset(&new_sig.sa_mask);
100 new_sig.sa_flags = 0;
101 new_sig.sa_handler = time_is_up;
102 if ( sigaction(SIGALRM, &new_sig, o_ptr) ) {
103 return 1; /* setting signal failed */
105 if ( alarm(delay) ) {
106 (void) sigaction(SIGALRM, o_ptr, NULL);
107 return 1; /* failed to set alarm */
109 return 0; /* all seems to have worked */
112 /* return the number of seconds to next alarm. 0 = no delay, -1 = expired */
113 static int get_delay(void)
117 expired = 0; /* reset flag */
120 /* has the quit time past? */
121 if (pam_misc_conv_die_time && now >= pam_misc_conv_die_time) {
122 fprintf(stderr,"%s",pam_misc_conv_die_line);
124 pam_misc_conv_died = 1; /* note we do not reset the die_time */
125 return -1; /* time is up */
128 /* has the warning time past? */
129 if (pam_misc_conv_warn_time && now >= pam_misc_conv_warn_time) {
130 fprintf(stderr, "%s", pam_misc_conv_warn_line);
131 pam_misc_conv_warn_time = 0; /* reset warn_time */
133 /* indicate remaining delay - if any */
135 return (pam_misc_conv_die_time ? pam_misc_conv_die_time - now:0 );
138 /* indicate possible warning delay */
140 if (pam_misc_conv_warn_time)
141 return (pam_misc_conv_warn_time - now);
142 else if (pam_misc_conv_die_time)
143 return (pam_misc_conv_die_time - now);
148 /* read a line of input string, giving prompt when appropriate */
149 static char *read_string(int echo, const char *prompt)
151 struct termios term_before, term_tmp;
152 char line[INPUTSIZE];
153 struct sigaction old_sig;
154 int delay, nc, have_term=0;
156 D(("called with echo='%s', prompt='%s'.", echo ? "ON":"OFF" , prompt));
158 if (isatty(STDIN_FILENO)) { /* terminal state */
160 /* is a terminal so record settings and flush it */
161 if ( tcgetattr(STDIN_FILENO, &term_before) != 0 ) {
162 D(("<error: failed to get terminal settings>"));
165 memcpy(&term_tmp, &term_before, sizeof(term_tmp));
167 term_tmp.c_lflag &= ~(ECHO);
172 D(("<warning: cannot turn echo off>"));
175 /* set up the signal handling */
178 /* reading the line */
181 fprintf(stderr, "%s", prompt);
182 /* this may, or may not set echo off -- drop pending input */
184 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_tmp);
186 if ( delay > 0 && set_alarm(delay, &old_sig) ) {
187 D(("<failed to set alarm>"));
190 nc = read(STDIN_FILENO, line, INPUTSIZE-1);
192 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &term_before);
193 if (!echo || expired) /* do we need a newline? */
194 fprintf(stderr,"\n");
197 reset_alarm(&old_sig);
201 } else if (nc > 0) { /* we got some user input */
204 if (nc > 0 && line[nc-1] == '\n') { /* <NUL> terminate */
209 input = x_strdup(line);
210 _pam_overwrite(line);
212 return input; /* return malloc()ed string */
213 } else if (nc == 0) { /* Ctrl-D */
214 D(("user did not want to type anything"));
215 fprintf(stderr, "\n");
221 /* getting here implies that the timer expired */
223 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &term_before);
225 memset(line, 0, INPUTSIZE); /* clean up */
229 /* end of read_string functions */
231 int misc_conv(int num_msg, const struct pam_message **msgm,
232 struct pam_response **response, void *appdata_ptr)
235 struct pam_response *reply;
240 D(("allocating empty response structure array."));
242 reply = (struct pam_response *) calloc(num_msg,
243 sizeof(struct pam_response));
245 D(("no memory for responses"));
249 D(("entering conversation function."));
251 for (count=0; count < num_msg; ++count) {
254 switch (msgm[count]->msg_style) {
255 case PAM_PROMPT_ECHO_OFF:
256 string = read_string(CONV_ECHO_OFF,msgm[count]->msg);
257 if (string == NULL) {
258 goto failed_conversation;
261 case PAM_PROMPT_ECHO_ON:
262 string = read_string(CONV_ECHO_ON,msgm[count]->msg);
263 if (string == NULL) {
264 goto failed_conversation;
268 if (fprintf(stderr,"%s\n",msgm[count]->msg) < 0) {
269 goto failed_conversation;
273 if (fprintf(stdout,"%s\n",msgm[count]->msg) < 0) {
274 goto failed_conversation;
277 case PAM_BINARY_PROMPT:
280 const void *pack_in = msgm[count]->msg;
282 if (!pam_binary_handler_fn
283 || pam_binary_handler_fn(pack_in, &pack_out) != PAM_SUCCESS
284 || pack_out == NULL) {
285 goto failed_conversation;
287 string = (char *) pack_out;
293 fprintf(stderr, "erroneous conversation (%d)\n"
294 ,msgm[count]->msg_style);
295 goto failed_conversation;
298 if (string) { /* must add to reply array */
299 /* add string to list of responses */
301 reply[count].resp_retcode = 0;
302 reply[count].resp = string;
307 /* New (0.59+) behavior is to always have a reply - this is
308 compatable with the X/Open (March 1997) spec. */
317 for (count=0; count<num_msg; ++count) {
318 if (reply[count].resp == NULL) {
321 switch (msgm[count]->msg_style) {
322 case PAM_PROMPT_ECHO_ON:
323 case PAM_PROMPT_ECHO_OFF:
324 _pam_overwrite(reply[count].resp);
325 free(reply[count].resp);
327 case PAM_BINARY_PROMPT:
328 pam_binary_handler_free((void **) &reply[count].resp);
332 /* should not actually be able to get here... */
333 free(reply[count].resp);
335 reply[count].resp = NULL;
337 /* forget reply too */