#include <config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <unistd.h>
#include <errno.h>
#include "sudo.h"
#include "sudo_plugin.h"
#include "sudo_plugin_int.h"
extern int tgetpass_flags;
int
sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
struct sudo_conv_reply replies[], struct sudo_conv_callback *callback)
{
char *pass;
int n;
const int conv_debug_instance = sudo_debug_get_active_instance();
sudo_debug_set_active_instance(sudo_debug_instance);
for (n = 0; n < num_msgs; n++) {
const struct sudo_conv_message *msg = &msgs[n];
int flags = tgetpass_flags;
switch (msg->msg_type & 0xff) {
case SUDO_CONV_PROMPT_ECHO_ON:
SET(flags, TGP_ECHO);
goto read_pass;
case SUDO_CONV_PROMPT_MASK:
SET(flags, TGP_MASK);
case SUDO_CONV_PROMPT_ECHO_OFF:
if (ISSET(msg->msg_type, SUDO_CONV_PROMPT_ECHO_OK))
SET(flags, TGP_NOECHO_TRY);
read_pass:
pass = tgetpass(msg->msg, msg->timeout, flags, callback);
if (pass == NULL)
goto err;
replies[n].reply = strdup(pass);
if (replies[n].reply == NULL) {
sudo_fatalx_nodebug(U_("%s: %s"), "sudo_conversation",
U_("unable to allocate memory"));
}
memset_s(pass, SUDO_CONV_REPL_MAX, 0, strlen(pass));
break;
case SUDO_CONV_INFO_MSG:
if (msg->msg != NULL && fputs(msg->msg, stdout) == EOF)
goto err;
break;
case SUDO_CONV_ERROR_MSG:
if (msg->msg != NULL && fputs(msg->msg, stderr) == EOF)
goto err;
break;
default:
goto err;
}
}
sudo_debug_set_active_instance(conv_debug_instance);
return 0;
err:
if (replies != 0) {
do {
struct sudo_conv_reply *repl = &replies[n];
if (repl->reply == NULL)
continue;
memset_s(repl->reply, SUDO_CONV_REPL_MAX, 0, strlen(repl->reply));
free(repl->reply);
repl->reply = NULL;
} while (n--);
}
sudo_debug_set_active_instance(conv_debug_instance);
return -1;
}
int
sudo_conversation_1_7(int num_msgs, const struct sudo_conv_message msgs[],
struct sudo_conv_reply replies[])
{
return sudo_conversation(num_msgs, msgs, replies, NULL);
}
int
sudo_conversation_printf(int msg_type, const char *fmt, ...)
{
va_list ap;
int len;
const int conv_debug_instance = sudo_debug_get_active_instance();
sudo_debug_set_active_instance(sudo_debug_instance);
switch (msg_type) {
case SUDO_CONV_INFO_MSG:
va_start(ap, fmt);
len = vfprintf(stdout, fmt, ap);
va_end(ap);
break;
case SUDO_CONV_ERROR_MSG:
va_start(ap, fmt);
len = vfprintf(stderr, fmt, ap);
va_end(ap);
break;
default:
len = -1;
errno = EINVAL;
break;
}
sudo_debug_set_active_instance(conv_debug_instance);
return len;
}