4 * A generic conversation function for text based applications
6 * Written by Andrew Morgan <morgan@linux.kernel.org>
9 #include <security/_pam_aconf.h>
15 #include <sys/types.h>
20 #include <security/pam_appl.h>
21 #include <security/pam_misc.h>
23 #define INPUTSIZE PAM_MAX_MSG_SIZE /* maximum length of input+1 */
24 #define CONV_ECHO_ON 1 /* types of echo state */
25 #define CONV_ECHO_OFF 0
28 * external timeout definitions - these can be overriden by the
32 time_t pam_misc_conv_warn_time = 0; /* time when we warn */
33 time_t pam_misc_conv_die_time = 0; /* time when we timeout */
35 const char *pam_misc_conv_warn_line = "..\a.Time is running out...\n";
36 const char *pam_misc_conv_die_line = "..\a.Sorry, your time is up!\n";
38 int pam_misc_conv_died=0; /* application can probe this for timeout */
41 * These functions are for binary prompt manipulation.
42 * The manner in which a binary prompt is processed is application
43 * specific, so these function pointers are provided and can be
44 * initialized by the application prior to the conversation function
48 static void pam_misc_conv_delete_binary(void *appdata,
51 PAM_BP_RENEW(delete_me, 0, 0);
54 int (*pam_binary_handler_fn)(void *appdata, pamc_bp_t *prompt_p) = NULL;
55 void (*pam_binary_handler_free)(void *appdata, pamc_bp_t *prompt_p)
56 = pam_misc_conv_delete_binary;
58 /* the following code is used to get text input */
60 static volatile int expired=0;
62 /* return to the previous signal handling */
63 static void reset_alarm(struct sigaction *o_ptr)
65 (void) alarm(0); /* stop alarm clock - if still ticking */
66 (void) sigaction(SIGALRM, o_ptr, NULL);
69 /* this is where we intercept the alarm signal */
70 static void time_is_up(int ignore)
75 /* set the new alarm to hit the time_is_up() function */
76 static int set_alarm(int delay, struct sigaction *o_ptr)
78 struct sigaction new_sig;
80 sigemptyset(&new_sig.sa_mask);
82 new_sig.sa_handler = time_is_up;
83 if ( sigaction(SIGALRM, &new_sig, o_ptr) ) {
84 return 1; /* setting signal failed */
87 (void) sigaction(SIGALRM, o_ptr, NULL);
88 return 1; /* failed to set alarm */
90 return 0; /* all seems to have worked */
93 /* return the number of seconds to next alarm. 0 = no delay, -1 = expired */
94 static int get_delay(void)
98 expired = 0; /* reset flag */
101 /* has the quit time past? */
102 if (pam_misc_conv_die_time && now >= pam_misc_conv_die_time) {
103 fprintf(stderr,"%s",pam_misc_conv_die_line);
105 pam_misc_conv_died = 1; /* note we do not reset the die_time */
106 return -1; /* time is up */
109 /* has the warning time past? */
110 if (pam_misc_conv_warn_time && now >= pam_misc_conv_warn_time) {
111 fprintf(stderr, "%s", pam_misc_conv_warn_line);
112 pam_misc_conv_warn_time = 0; /* reset warn_time */
114 /* indicate remaining delay - if any */
116 return (pam_misc_conv_die_time ? pam_misc_conv_die_time - now:0 );
119 /* indicate possible warning delay */
121 if (pam_misc_conv_warn_time)
122 return (pam_misc_conv_warn_time - now);
123 else if (pam_misc_conv_die_time)
124 return (pam_misc_conv_die_time - now);
129 /* read a line of input string, giving prompt when appropriate */
130 static char *read_string(int echo, const char *prompt)
132 struct termios term_before, term_tmp;
133 char line[INPUTSIZE], *input;
134 struct sigaction old_sig;
135 int delay, nc, have_term=0;
138 D(("called with echo='%s', prompt='%s'.", echo ? "ON":"OFF" , prompt));
142 if (isatty(STDIN_FILENO)) { /* terminal state */
144 /* is a terminal so record settings and flush it */
145 if ( tcgetattr(STDIN_FILENO, &term_before) != 0 ) {
146 D(("<error: failed to get terminal settings>"));
149 memcpy(&term_tmp, &term_before, sizeof(term_tmp));
151 term_tmp.c_lflag &= ~(ECHO);
156 * We make a simple attempt to block TTY signals from terminating
157 * the conversation without giving PAM a chance to clean up.
161 sigaddset(&nset, SIGINT);
162 sigaddset(&nset, SIGTSTP);
163 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
166 D(("<warning: cannot turn echo off>"));
169 /* set up the signal handling */
172 /* reading the line */
175 fprintf(stderr, "%s", prompt);
176 /* this may, or may not set echo off -- drop pending input */
178 (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_tmp);
180 if ( delay > 0 && set_alarm(delay, &old_sig) ) {
181 D(("<failed to set alarm>"));
184 nc = read(STDIN_FILENO, line, INPUTSIZE-1);
186 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &term_before);
187 if (!echo || expired) /* do we need a newline? */
188 fprintf(stderr,"\n");
191 reset_alarm(&old_sig);
195 } else if (nc > 0) { /* we got some user input */
196 D(("we got some user input"));
198 if (nc > 0 && line[nc-1] == '\n') { /* <NUL> terminate */
202 fprintf(stderr, "\n");
206 input = x_strdup(line);
207 _pam_overwrite(line);
209 goto cleanexit; /* return malloc()ed string */
211 } else if (nc == 0) { /* Ctrl-D */
212 D(("user did not want to type anything"));
214 input = x_strdup("");
216 fprintf(stderr, "\n");
218 goto cleanexit; /* return malloc()ed "" */
223 /* getting here implies that the timer expired */
225 D(("the timer appears to have expired"));
228 _pam_overwrite(line);
233 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
234 (void) tcsetattr(STDIN_FILENO, TCSADRAIN, &term_before);
237 D(("returning [%s]", input));
242 /* end of read_string functions */
245 * This conversation function is supposed to be a generic PAM one.
246 * Unfortunately, it is _not_ completely compatible with the Solaris PAM
249 * Namely, for msgm's that contain multiple prompts, this function
250 * interprets "const struct pam_message **msgm" as equivalent to
251 * "const struct pam_message *msgm[]". The Solaris module
252 * implementation interprets the **msgm object as a pointer to a
253 * pointer to an array of "struct pam_message" objects (that is, a
254 * confusing amount of pointer indirection).
257 int misc_conv(int num_msg, const struct pam_message **msgm,
258 struct pam_response **response, void *appdata_ptr)
261 struct pam_response *reply;
266 D(("allocating empty response structure array."));
268 reply = (struct pam_response *) calloc(num_msg,
269 sizeof(struct pam_response));
271 D(("no memory for responses"));
275 D(("entering conversation function."));
277 for (count=0; count < num_msg; ++count) {
280 switch (msgm[count]->msg_style) {
281 case PAM_PROMPT_ECHO_OFF:
282 string = read_string(CONV_ECHO_OFF,msgm[count]->msg);
283 if (string == NULL) {
284 goto failed_conversation;
287 case PAM_PROMPT_ECHO_ON:
288 string = read_string(CONV_ECHO_ON,msgm[count]->msg);
289 if (string == NULL) {
290 goto failed_conversation;
294 if (fprintf(stderr,"%s\n",msgm[count]->msg) < 0) {
295 goto failed_conversation;
299 if (fprintf(stdout,"%s\n",msgm[count]->msg) < 0) {
300 goto failed_conversation;
303 case PAM_BINARY_PROMPT:
305 pamc_bp_t binary_prompt = NULL;
307 if (!msgm[count]->msg || !pam_binary_handler_fn) {
308 goto failed_conversation;
311 PAM_BP_RENEW(&binary_prompt,
312 PAM_BP_RCONTROL(msgm[count]->msg),
313 PAM_BP_LENGTH(msgm[count]->msg));
314 PAM_BP_FILL(binary_prompt, 0, PAM_BP_LENGTH(msgm[count]->msg),
315 PAM_BP_RDATA(msgm[count]->msg));
317 if (pam_binary_handler_fn(appdata_ptr,
318 &binary_prompt) != PAM_SUCCESS
319 || (binary_prompt == NULL)) {
320 goto failed_conversation;
322 string = (char *) binary_prompt;
323 binary_prompt = NULL;
328 fprintf(stderr, "erroneous conversation (%d)\n"
329 ,msgm[count]->msg_style);
330 goto failed_conversation;
333 if (string) { /* must add to reply array */
334 /* add string to list of responses */
336 reply[count].resp_retcode = 0;
337 reply[count].resp = string;
349 D(("the conversation failed"));
352 for (count=0; count<num_msg; ++count) {
353 if (reply[count].resp == NULL) {
356 switch (msgm[count]->msg_style) {
357 case PAM_PROMPT_ECHO_ON:
358 case PAM_PROMPT_ECHO_OFF:
359 _pam_overwrite(reply[count].resp);
360 free(reply[count].resp);
362 case PAM_BINARY_PROMPT:
363 pam_binary_handler_free(appdata_ptr,
364 (pamc_bp_t *) &reply[count].resp);
368 /* should not actually be able to get here... */
369 free(reply[count].resp);
371 reply[count].resp = NULL;
373 /* forget reply too */