Move _sudo_printf from src/conversation.c to common/sudo_printf.c.

Add sudo_printf function pointer that is initialized to _sudo_printf()
instead of requiring a sudo_conv function pointer everywhere.  The
plugin will reset sudo_printf to point to the version passed in via
the plugin open function.  Now plugin_error.c can just call sudo_printf
in all cases.  The sudoers binaries no longer need their own version
of sudo_printf.
This commit is contained in:
Todd C. Miller
2012-11-25 09:34:33 -05:00
parent 5496ffe1e8
commit c2c6616a0c
23 changed files with 128 additions and 221 deletions

View File

@@ -19,6 +19,7 @@ common/secure_path.c
common/setgroups.c
common/sudo_conf.c
common/sudo_debug.c
common/sudo_printf.c
common/term.c
common/ttysize.c
common/zero_bytes.c

View File

@@ -58,8 +58,8 @@ DEFS = @OSDEFS@ -D_PATH_SUDO_CONF=\"$(sysconfdir)/sudo.conf\"
SHELL = @SHELL@
LTOBJS = alloc.lo atobool.lo fileops.lo fmt_string.lo lbuf.lo list.lo \
secure_path.lo setgroups.lo sudo_conf.lo sudo_debug.lo term.lo \
ttysize.lo zero_bytes.lo @COMMON_OBJS@
secure_path.lo setgroups.lo sudo_conf.lo sudo_debug.lo sudo_printf.lo \
term.lo ttysize.lo zero_bytes.lo @COMMON_OBJS@
all: libcommon.la
@@ -151,6 +151,9 @@ sudo_debug.lo: $(srcdir)/sudo_debug.c $(top_builddir)/config.h \
$(incdir)/alloc.h $(incdir)/error.h $(incdir)/gettext.h \
$(incdir)/sudo_plugin.h $(incdir)/sudo_debug.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/sudo_debug.c
sudo_printf.lo: $(srcdir)/sudo_printf.c $(top_builddir)/config.h \
$(incdir)/sudo_plugin.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/sudo_printf.c
term.lo: $(srcdir)/term.c $(top_builddir)/config.h $(incdir)/missing.h \
$(incdir)/sudo_debug.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/term.c

View File

@@ -120,8 +120,6 @@ static int sudo_debug_mode;
static char sudo_debug_pidstr[(((sizeof(int) * 8) + 2) / 3) + 3];
static size_t sudo_debug_pidlen;
extern sudo_conv_t sudo_conv;
/*
* Parse settings string from sudo.conf and open debugfile.
* Returns 1 on success, 0 if cannot open debugfile.
@@ -285,36 +283,21 @@ static void
sudo_debug_write_conv(const char *func, const char *file, int lineno,
const char *str, int len, int errno_val)
{
struct sudo_conv_message msg;
struct sudo_conv_reply repl;
char *buf = NULL;
/* Call conversation function */
if (sudo_conv != NULL) {
/* Remove the newline at the end if appending extra info. */
if (str[len - 1] == '\n')
len--;
if (func != NULL && file != NULL && lineno != 0) {
if (errno_val) {
easprintf(&buf, "%.*s: %s @ %s() %s:%d", len, str,
strerror(errno_val), func, file, lineno);
sudo_printf(SUDO_CONV_DEBUG_MSG, "%.*s: %s @ %s() %s:%d",
len, str, strerror(errno_val), func, file, lineno);
} else {
easprintf(&buf, "%.*s @ %s() %s:%d", len, str,
func, file, lineno);
sudo_printf(SUDO_CONV_DEBUG_MSG, "%.*s @ %s() %s:%d",
len, str, func, file, lineno);
}
str = buf;
} else if (errno_val) {
easprintf(&buf, "%.*s: %s", len, str, strerror(errno_val));
str = buf;
}
memset(&msg, 0, sizeof(msg));
memset(&repl, 0, sizeof(repl));
msg.msg_type = SUDO_CONV_DEBUG_MSG;
msg.msg = str;
sudo_conv(1, &msg, &repl);
if (buf != NULL)
efree(buf);
sudo_printf(SUDO_CONV_DEBUG_MSG, "%.*s: %s",
len, str, strerror(errno_val));
}
}

75
common/sudo_printf.c Normal file
View File

@@ -0,0 +1,75 @@
/*
* Copyright (c) 2010-2012 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <sys/types.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif /* STDC_HEADERS */
#include <stdarg.h>
#include <errno.h>
#include "sudo_plugin.h"
#include "sudo_debug.h"
int
_sudo_printf(int msg_type, const char *fmt, ...)
{
va_list ap;
FILE *fp;
int len;
switch (msg_type) {
case SUDO_CONV_INFO_MSG:
fp = stdout;
break;
case SUDO_CONV_ERROR_MSG:
fp = stderr;
break;
case SUDO_CONV_DEBUG_MSG:
{
char *buf;
va_list ap;
/* XXX - add debug version of vfprintf()? */
va_start(ap, fmt);
len = vasprintf(&buf, fmt, ap);
va_end(ap);
if (len == -1)
return -1;
sudo_debug_write(buf, len, 0);
break;
}
default:
errno = EINVAL;
return -1;
}
va_start(ap, fmt);
len = vfprintf(fp, fmt, ap);
va_end(ap);
return len;
}
int (*sudo_printf)(int msg_type, const char *fmt, ...) = _sudo_printf;

View File

@@ -18,6 +18,7 @@
#define _SUDO_ERROR_H_
#include <stdarg.h>
#include <sudo_plugin.h>
/*
* We wrap error/errorx and warn/warnx so that the same output can
@@ -170,6 +171,8 @@
warning_restore_locale(); \
} while (0)
extern sudo_printf_t sudo_printf;
void error2(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__));
void errorx2(int, const char *, ...) __printflike(2, 3) __attribute__((__noreturn__));
void verror2(int, const char *, va_list ap) __attribute__((__noreturn__));

View File

@@ -615,6 +615,7 @@ locale.lo: $(srcdir)/locale.c $(top_builddir)/config.h $(srcdir)/sudoers.h \
$(devdir)/def_data.h $(srcdir)/logging.h $(srcdir)/sudo_nss.h \
$(incdir)/sudo_plugin.h $(incdir)/sudo_debug.h $(incdir)/gettext.h
$(LIBTOOL) --mode=compile $(CC) -c $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/locale.c
locale.o: locale.lo
logging.lo: $(srcdir)/logging.c $(top_builddir)/config.h $(srcdir)/sudoers.h \
$(top_srcdir)/compat/stdbool.h $(top_builddir)/pathnames.h \
$(incdir)/missing.h $(incdir)/error.h $(incdir)/alloc.h \
@@ -800,8 +801,8 @@ sudoreplay.o: $(srcdir)/sudoreplay.c $(top_builddir)/config.h \
$(top_srcdir)/compat/timespec.h $(top_srcdir)/compat/stdbool.h \
$(top_builddir)/pathnames.h $(incdir)/missing.h \
$(incdir)/alloc.h $(incdir)/error.h $(incdir)/gettext.h \
$(incdir)/sudo_plugin.h $(incdir)/sudo_conf.h $(incdir)/list.h \
$(incdir)/sudo_debug.h
$(srcdir)/logging.h $(incdir)/sudo_plugin.h \
$(incdir)/sudo_conf.h $(incdir)/list.h $(incdir)/sudo_debug.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/sudoreplay.c
testsudoers.o: $(srcdir)/testsudoers.c $(top_builddir)/config.h \
$(top_srcdir)/compat/fnmatch.h $(srcdir)/tsgetgrpw.h \

View File

@@ -148,27 +148,12 @@ sudoerserror(const char *s)
LEXTRACE("<*> ");
#ifndef TRACELEXER
if (trace_print == NULL || trace_print == sudoers_trace_print) {
int oldlocale;
const char fmt[] = ">>> %s: %s near line %d <<<\n";
int oldlocale;
/* Warnings are displayed in the user's locale. */
sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale);
if (sudo_conv != NULL) {
struct sudo_conv_message msg;
struct sudo_conv_reply repl;
char *str;
easprintf(&str, _(fmt), sudoers, _(s), sudolineno);
memset(&msg, 0, sizeof(repl));
memset(&repl, 0, sizeof(repl));
msg.msg_type = SUDO_CONV_ERROR_MSG;
msg.msg = str;
sudo_conv(1, &msg, &repl);
efree(str);
} else {
fprintf(stderr, _(fmt), sudoers, _(s), sudolineno);
}
sudo_printf(SUDO_CONV_ERROR_MSG, _(fmt), sudoers, _(s), sudolineno);
sudoers_setlocale(oldlocale, NULL);
}
#endif

View File

@@ -110,27 +110,12 @@ sudoerserror(const char *s)
LEXTRACE("<*> ");
#ifndef TRACELEXER
if (trace_print == NULL || trace_print == sudoers_trace_print) {
int oldlocale;
const char fmt[] = ">>> %s: %s near line %d <<<\n";
int oldlocale;
/* Warnings are displayed in the user's locale. */
sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale);
if (sudo_conv != NULL) {
struct sudo_conv_message msg;
struct sudo_conv_reply repl;
char *str;
easprintf(&str, _(fmt), sudoers, _(s), sudolineno);
memset(&msg, 0, sizeof(repl));
memset(&repl, 0, sizeof(repl));
msg.msg_type = SUDO_CONV_ERROR_MSG;
msg.msg = str;
sudo_conv(1, &msg, &repl);
efree(str);
} else {
fprintf(stderr, _(fmt), sudoers, _(s), sudolineno);
}
sudo_printf(SUDO_CONV_ERROR_MSG, _(fmt), sudoers, _(s), sudolineno);
sudoers_setlocale(oldlocale, NULL);
}
#endif

View File

@@ -467,9 +467,7 @@ sudoers_io_open(unsigned int version, sudo_conv_t conversation,
int rval = -1;
debug_decl(sudoers_io_open, SUDO_DEBUG_PLUGIN)
if (!sudo_conv)
sudo_conv = conversation;
if (!sudo_printf)
sudo_printf = plugin_printf;
/* If we have no command (because -V was specified) just return. */

View File

@@ -44,8 +44,6 @@ static void _warning(int, const char *, va_list);
static sigjmp_buf error_jmp;
static bool setjmp_enabled = false;
extern sudo_conv_t sudo_conv;
void
error2(int eval, const char *fmt, ...)
{
@@ -133,47 +131,23 @@ static void
_warning(int use_errno, const char *fmt, va_list ap)
{
int serrno = errno;
if (sudo_conv != NULL) {
struct sudo_conv_message msg[6];
struct sudo_conv_reply repl[6];
int nmsgs = 4;
char *str;
evasprintf(&str, _(fmt), ap);
/* Call conversation function */
memset(&msg, 0, sizeof(msg));
msg[0].msg_type = SUDO_CONV_ERROR_MSG;
msg[0].msg = getprogname();
msg[1].msg_type = SUDO_CONV_ERROR_MSG;
msg[1].msg = _(": ");
msg[2].msg_type = SUDO_CONV_ERROR_MSG;
msg[2].msg = str;
evasprintf(&str, fmt, ap);
if (use_errno) {
msg[3].msg_type = SUDO_CONV_ERROR_MSG;
msg[3].msg = _(": ");
msg[4].msg_type = SUDO_CONV_ERROR_MSG;
msg[4].msg = strerror(errno);
nmsgs = 6;
}
msg[nmsgs - 1].msg_type = SUDO_CONV_ERROR_MSG;
msg[nmsgs - 1].msg = "\n";
memset(&repl, 0, sizeof(repl));
sudo_conv(nmsgs, msg, repl);
efree(str);
} else {
fputs(getprogname(), stderr);
if (fmt != NULL) {
fputs(_(": "), stderr);
vfprintf(stderr, _(fmt), ap);
sudo_printf(SUDO_CONV_ERROR_MSG,
_("%s: %s: %s\n"), getprogname(), str, strerror(serrno));
} else {
sudo_printf(SUDO_CONV_ERROR_MSG,
_("%s: %s\n"), getprogname(), strerror(serrno));
}
if (use_errno) {
fputs(_(": "), stderr);
fputs(strerror(serrno), stderr);
}
putc('\n', stderr);
} else {
sudo_printf(SUDO_CONV_ERROR_MSG,
_("%s: %s\n"), getprogname(), str ? str : "(null)");
}
efree(str);
errno = serrno;
}
static int oldlocale;

View File

@@ -458,9 +458,7 @@ sudoers_policy_open(unsigned int version, sudo_conv_t conversation,
debug_decl(sudoers_policy_open, SUDO_DEBUG_PLUGIN)
sudo_version = version;
if (!sudo_conv)
sudo_conv = conversation;
if (!sudo_printf)
sudo_printf = plugin_printf;
/* Plugin args are only specified for API version 1.2 and higher. */

View File

@@ -116,7 +116,7 @@ main(int argc, char *argv[])
}
void
cleanup(int gotsig)
sudoers_cleanup(int gotsig)
{
return;
}

View File

@@ -47,7 +47,6 @@
struct sudo_user sudo_user;
struct passwd *list_pw;
sudo_conv_t sudo_conv; /* NULL in non-plugin */
static char sessid[7];
@@ -205,7 +204,7 @@ void io_nextid(char *iolog_dir, char *fallback, char id[7])
}
void
cleanup(int gotsig)
sudoers_cleanup(int gotsig)
{
return;
}

View File

@@ -42,8 +42,6 @@
#include "error.h"
#include "sudo_plugin.h"
sudo_conv_t sudo_conv; /* NULL in non-plugin */
extern void writeln_wrap(FILE *fp, char *line, size_t len, size_t maxlen);
__dso_public int main(int argc, char *argv[]);
@@ -108,7 +106,7 @@ main(int argc, char *argv[])
}
void
cleanup(int gotsig)
sudoers_cleanup(int gotsig)
{
return;
}

View File

@@ -56,8 +56,6 @@ __dso_public int main(int argc, char *argv[]);
struct interface *interfaces;
sudo_printf_t sudo_printf = check_addr_printf;
sudo_conv_t sudo_conv; /* NULL in non-plugin */
static int
check_addr(char *input)
{
@@ -156,7 +154,7 @@ main(int argc, char *argv[])
/* STUB */
void
cleanup(int gotsig)
sudoers_cleanup(int gotsig)
{
return;
}

View File

@@ -42,6 +42,7 @@
#define SUDO_ERROR_WRAP 0
#include "missing.h"
#include "list.h"
#include "parse.h"
#include "toke.h"
@@ -54,8 +55,6 @@ __dso_public int main(int argc, char *argv[]);
* TODO: test realloc
*/
sudo_conv_t sudo_conv; /* NULL in non-plugin */
YYSTYPE sudoerslval;
struct fill_test {
@@ -186,7 +185,7 @@ main(int argc, char *argv[])
/* STUB */
void
cleanup(int gotsig)
sudoers_cleanup(int gotsig)
{
return;
}

View File

@@ -108,7 +108,6 @@ extern char *errorfile;
char *login_style;
#endif /* HAVE_BSD_AUTH_H */
sudo_conv_t sudo_conv;
sudo_printf_t sudo_printf;
int sudo_mode;
static char *prev_user;

View File

@@ -179,8 +179,6 @@ struct search_node {
} u;
} *search_expr;
sudo_conv_t sudo_conv; /* NULL in non-plugin */
#define STACK_NODE_SIZE 32
static struct search_node *node_stack[32];
static int stack_top;

View File

@@ -82,7 +82,6 @@ void sudoers_cleanup(int);
static void set_runaspw(const char *);
static void set_runasgr(const char *);
static int cb_runas_default(const char *);
static int testsudoers_printf(int msg_type, const char *fmt, ...);
static int testsudoers_print(const char *msg);
extern void setgrfile(const char *);
@@ -109,8 +108,6 @@ static char *runas_group, *runas_user;
extern int errorlineno;
extern bool parse_error;
extern char *errorfile;
sudo_printf_t sudo_printf = testsudoers_printf;
sudo_conv_t sudo_conv; /* NULL in non-plugin */
/* For getopt(3) */
extern char *optarg;
@@ -663,32 +660,6 @@ print_userspecs(void)
debug_return;
}
static int
testsudoers_printf(int msg_type, const char *fmt, ...)
{
va_list ap;
FILE *fp;
debug_decl(testsudoers_printf, SUDO_DEBUG_UTIL)
switch (msg_type) {
case SUDO_CONV_INFO_MSG:
fp = stdout;
break;
case SUDO_CONV_ERROR_MSG:
fp = stderr;
break;
default:
errno = EINVAL;
debug_return_int(-1);
}
va_start(ap, fmt);
vfprintf(fp, fmt, ap);
va_end(ap);
debug_return_int(0);
}
void
dump_sudoers(void)
{

View File

@@ -90,8 +90,6 @@ struct sudoersfile {
};
TQ_DECLARE(sudoersfile)
sudo_conv_t sudo_conv; /* NULL in non-plugin */
/*
* Function prototypes
*/
@@ -107,7 +105,6 @@ static bool install_sudoers(struct sudoersfile *, bool);
static int print_unused(void *, void *);
static void reparse_sudoers(char *, char *, bool, bool);
static int run_command(char *, char **);
static int visudo_printf(int msg_type, const char *fmt, ...);
static void setup_signals(void);
static void help(void) __attribute__((__noreturn__));
static void usage(int);
@@ -134,7 +131,6 @@ extern int optind;
*/
struct sudo_user sudo_user;
struct passwd *list_pw;
sudo_printf_t sudo_printf = visudo_printf;
static struct sudoersfile_list sudoerslist;
static struct rbtree *alias_freelist;
static bool checkonly;
@@ -1310,28 +1306,3 @@ help(void)
" -V display version information and exit"));
exit(0);
}
static int
visudo_printf(int msg_type, const char *fmt, ...)
{
va_list ap;
FILE *fp;
switch (msg_type) {
case SUDO_CONV_INFO_MSG:
fp = stdout;
break;
case SUDO_CONV_ERROR_MSG:
fp = stderr;
break;
default:
errno = EINVAL;
return -1;
}
va_start(ap, fmt);
vfprintf(fp, fmt, ap);
va_end(ap);
return 0;
}

View File

@@ -175,7 +175,7 @@ env_hooks.o: $(srcdir)/env_hooks.c $(top_builddir)/config.h \
$(incdir)/sudo_plugin.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/env_hooks.c
error.o: $(srcdir)/error.c $(top_builddir)/config.h $(incdir)/missing.h \
$(incdir)/alloc.h $(incdir)/error.h $(incdir)/gettext.h
$(incdir)/error.h $(incdir)/gettext.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/error.c
exec.o: $(srcdir)/exec.c $(top_builddir)/config.h $(srcdir)/sudo.h \
$(top_builddir)/pathnames.h $(top_srcdir)/compat/stdbool.h \

View File

@@ -51,10 +51,6 @@
extern int tgetpass_flags; /* XXX */
#if defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD)
sudo_conv_t sudo_conv; /* NULL in sudo front-end */
#endif
/*
* Sudo conversation function.
*/
@@ -120,29 +116,3 @@ err:
return -1;
}
int
_sudo_printf(int msg_type, const char *fmt, ...)
{
va_list ap;
FILE *fp;
int len;
switch (msg_type) {
case SUDO_CONV_INFO_MSG:
fp = stdout;
break;
case SUDO_CONV_ERROR_MSG:
fp = stderr;
break;
default:
errno = EINVAL;
return -1;
}
va_start(ap, fmt);
len = vfprintf(fp, fmt, ap);
va_end(ap);
return len;
}

View File

@@ -40,8 +40,6 @@
#include "sudo_exec.h"
#include "sudo_plugin.h"
sudo_conv_t sudo_conv; /* NULL in non-plugin */
__dso_public int main(int argc, char *argv[], char *envp[]);
/*