From 3a7731437325c85c663d8d60b0ef9eaa35a71cf3 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 18 Sep 2023 12:42:51 -0600 Subject: [PATCH] Add a separate file for visudo callbacks. --- MANIFEST | 1 + plugins/sudoers/Makefile.in | 26 +++++++++++++++++++++++- plugins/sudoers/visudo.c | 6 ++---- plugins/sudoers/visudo_cb.c | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 plugins/sudoers/visudo_cb.c diff --git a/MANIFEST b/MANIFEST index e116653ec..af5321fcf 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1191,6 +1191,7 @@ plugins/sudoers/tsgetgrpw.c plugins/sudoers/tsgetgrpw.h plugins/sudoers/unesc_str.c plugins/sudoers/visudo.c +plugins/sudoers/visudo_cb.c plugins/system_group/Makefile.in plugins/system_group/system_group.c plugins/system_group/system_group.exp diff --git a/plugins/sudoers/Makefile.in b/plugins/sudoers/Makefile.in index f678782ca..ef186eb19 100644 --- a/plugins/sudoers/Makefile.in +++ b/plugins/sudoers/Makefile.in @@ -197,7 +197,7 @@ SUDOERS_IOBJS = $(SUDOERS_OBJS:.lo=.i) VISUDO_OBJS = check_aliases.o editor.lo find_path.lo gc.lo goodpath.lo \ locale.lo sethost.lo stubs.o sudo_printf.o sudoers_ctx_free.lo \ - visudo.o + visudo.o visudo_cb.o VISUDO_IOBJS = sudo_printf.i visudo.i @@ -3471,3 +3471,27 @@ visudo.i: $(srcdir)/visudo.c $(devdir)/def_data.h $(devdir)/gram.h \ $(CC) -E -o $@ $(CPPFLAGS) $< visudo.plog: visudo.i rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/visudo.c --i-file $< --output-file $@ +visudo_cb.o: $(srcdir)/visudo_cb.c $(devdir)/def_data.h \ + $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \ + $(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \ + $(incdir)/sudo_eventlog.h $(incdir)/sudo_fatal.h \ + $(incdir)/sudo_gettext.h $(incdir)/sudo_plugin.h \ + $(incdir)/sudo_queue.h $(incdir)/sudo_util.h $(srcdir)/defaults.h \ + $(srcdir)/logging.h $(srcdir)/parse.h $(srcdir)/pivot.h \ + $(srcdir)/sudo_nss.h $(srcdir)/sudoers.h \ + $(srcdir)/sudoers_debug.h $(top_builddir)/config.h \ + $(top_builddir)/pathnames.h + $(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(HARDENING_CFLAGS) $(srcdir)/visudo_cb.c +visudo_cb.i: $(srcdir)/visudo_cb.c $(devdir)/def_data.h \ + $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \ + $(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \ + $(incdir)/sudo_eventlog.h $(incdir)/sudo_fatal.h \ + $(incdir)/sudo_gettext.h $(incdir)/sudo_plugin.h \ + $(incdir)/sudo_queue.h $(incdir)/sudo_util.h $(srcdir)/defaults.h \ + $(srcdir)/logging.h $(srcdir)/parse.h $(srcdir)/pivot.h \ + $(srcdir)/sudo_nss.h $(srcdir)/sudoers.h \ + $(srcdir)/sudoers_debug.h $(top_builddir)/config.h \ + $(top_builddir)/pathnames.h + $(CC) -E -o $@ $(CPPFLAGS) $< +visudo_cb.plog: visudo_cb.i + rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/visudo_cb.c --i-file $< --output-file $@ diff --git a/plugins/sudoers/visudo.c b/plugins/sudoers/visudo.c index 038fedf7d..d64f9015c 100644 --- a/plugins/sudoers/visudo.c +++ b/plugins/sudoers/visudo.c @@ -156,11 +156,9 @@ main(int argc, char *argv[]) if (argc < 1) usage(); - /* Register fatal/fatalx callback. */ + /* Register callbacks. */ sudo_fatal_callback_register(visudo_cleanup); - - /* Set sudoers locale callback. */ - sudo_defs_table[I_SUDOERS_LOCALE].callback = sudoers_locale_callback; + set_callbacks(); /* Read debug and plugin sections of sudo.conf. */ if (sudo_conf_read(NULL, SUDO_CONF_DEBUG|SUDO_CONF_PLUGINS) == -1) diff --git a/plugins/sudoers/visudo_cb.c b/plugins/sudoers/visudo_cb.c new file mode 100644 index 000000000..681f7371e --- /dev/null +++ b/plugins/sudoers/visudo_cb.c @@ -0,0 +1,40 @@ +/* + * SPDX-License-Identifier: ISC + * + * Copyright (c) 2023 Todd C. Miller + * + * 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. + */ + +/* + * This is an open source non-commercial project. Dear PVS-Studio, please check it. + * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + */ + +#include + +#include "sudoers.h" + +/* + * Set visudo Defaults callbacks. + */ +void +set_callbacks(void) +{ + debug_decl(set_callbacks, SUDOERS_DEBUG_UTIL); + + /* Set locale callback. */ + sudo_defs_table[I_SUDOERS_LOCALE].callback = sudoers_locale_callback; + + debug_return; +}