Add testsudoers_setshellfile() and use it in testsudoers.

This commit is contained in:
Todd C. Miller
2023-09-10 16:38:53 -06:00
parent 62b92c7fb8
commit 034b2f3bdd
5 changed files with 51 additions and 13 deletions

View File

@@ -212,7 +212,10 @@ uninstall-nls:
siglist.c signame.c: siglist.c signame.c:
cd lib/util && exec $(MAKE) $@ cd lib/util && exec $(MAKE) $@
depend: siglist.c signame.c tsgetusershell.c:
cd plugins/sudoers && exec $(MAKE) $@
depend: siglist.c signame.c tsgetusershell.c
$(scriptdir)/mkdep.pl \ $(scriptdir)/mkdep.pl \
--srcdir=$(abs_top_srcdir) --builddir=$(abs_top_builddir) \ --srcdir=$(abs_top_srcdir) --builddir=$(abs_top_builddir) \
lib/util/Makefile.in lib/zlib/Makefile.in \ lib/util/Makefile.in lib/zlib/Makefile.in \

View File

@@ -31,6 +31,11 @@
#include "sudo_debug.h" #include "sudo_debug.h"
#include "sudo_util.h" #include "sudo_util.h"
#ifdef TESTSUDOERS
# include "tsgetgrpw.h"
#endif
static const char *shellfile = "/etc/shell";
static char **allowed_shells, * const *current_shell; static char **allowed_shells, * const *current_shell;
static const char *default_shells[] = { static const char *default_shells[] = {
"/bin/sh", "/bin/sh",
@@ -53,7 +58,7 @@ read_shells(void)
FILE *fp; FILE *fp;
debug_decl(read_shells, SUDO_DEBUG_UTIL); debug_decl(read_shells, SUDO_DEBUG_UTIL);
if ((fp = fopen("/etc/shells", "r")) == NULL) if ((fp = fopen(shellfile, "r")) == NULL)
goto bad; goto bad;
free(allowed_shells); free(allowed_shells);
@@ -129,3 +134,12 @@ sudo_getusershell(void)
debug_return_str(*current_shell++); debug_return_str(*current_shell++);
} }
#ifdef TESTSUDOERS
void
testsudoers_setshellfile(const char *file)
{
testsudoers_endusershell();
shellfile = file;
}
#endif /* TESTSUDOERS */

View File

@@ -201,11 +201,12 @@ VISUDO_OBJS = check_aliases.o editor.lo find_path.lo gc.lo goodpath.lo \
VISUDO_IOBJS = sudo_printf.i visudo.i VISUDO_IOBJS = sudo_printf.i visudo.i
CVTSUDOERS_OBJS = b64_encode.o cvtsudoers.o cvtsudoers_json.o cvtsudoers_csv.o \ CVTSUDOERS_OBJS = b64_encode.o cvtsudoers.o cvtsudoers_csv.o \
cvtsudoers_ldif.o cvtsudoers_merge.o cvtsudoers_pwutil.o \ cvtsudoers_json.o cvtsudoers_ldif.o cvtsudoers_merge.o \
fmtsudoers.lo fmtsudoers_cvt.lo locale.lo parse_ldif.o \ cvtsudoers_pwutil.o fmtsudoers.lo fmtsudoers_cvt.lo \
sethost.lo stubs.o sudo_printf.o sudoers_ctx_free.lo \ ldap_util.lo locale.lo parse_ldif.o sethost.lo \
ldap_util.lo testsudoers_pwutil.o tsgetgrpw.o stubs.o sudo_printf.o sudoers_ctx_free.lo \
testsudoers_pwutil.o tsgetgrpw.o tsgetusershell.o
CVTSUDOERS_IOBJS = cvtsudoers.i cvtsudoers_csv.i cvtsudoers_json.i \ CVTSUDOERS_IOBJS = cvtsudoers.i cvtsudoers_csv.i cvtsudoers_json.i \
cvtsudoers_ldif.i cvtsudoers_merge.i cvtsudoers_pwutil.i cvtsudoers_ldif.i cvtsudoers_merge.i cvtsudoers_pwutil.i
@@ -217,7 +218,7 @@ REPLAY_IOBJS = $(REPLAY_OBJS:.o=.i)
TEST_OBJS = check_util.lo fmtsudoers.lo fmtsudoers_cvt.lo group_plugin.lo \ TEST_OBJS = check_util.lo fmtsudoers.lo fmtsudoers_cvt.lo group_plugin.lo \
interfaces.lo ldap_util.lo locale.lo lookup.lo net_ifs.o \ interfaces.lo ldap_util.lo locale.lo lookup.lo net_ifs.o \
parse_ldif.o sethost.lo sudo_printf.o sudoers_ctx_free.lo \ parse_ldif.o sethost.lo sudo_printf.o sudoers_ctx_free.lo \
testsudoers.o testsudoers_pwutil.o tsgetgrpw.o testsudoers.o testsudoers_pwutil.o tsgetgrpw.o tsgetusershell.o
IOBJS = $(LIBPARSESUDOERS_IOBJS) $(SUDOERS_IOBJS) $(VISUDO_IOBJS) \ IOBJS = $(LIBPARSESUDOERS_IOBJS) $(SUDOERS_IOBJS) $(VISUDO_IOBJS) \
$(CVTSUDOERS_IOBJS) $(REPLAY_IOBJS) $(CVTSUDOERS_IOBJS) $(REPLAY_IOBJS)
@@ -333,7 +334,7 @@ $(shlib_opt): $(shlib_exp)
.y.c: .y.c:
@true @true
depend: depend: tsgetusershell.c
$(scriptdir)/mkdep.pl --srcdir=$(abs_top_srcdir) \ $(scriptdir)/mkdep.pl --srcdir=$(abs_top_srcdir) \
--builddir=$(abs_top_builddir) plugins/sudoers/Makefile.in --builddir=$(abs_top_builddir) plugins/sudoers/Makefile.in
cd $(top_builddir) && ./config.status --file plugins/sudoers/Makefile cd $(top_builddir) && ./config.status --file plugins/sudoers/Makefile
@@ -544,6 +545,9 @@ $(devdir)/def_data.c $(devdir)/def_data.h: $(srcdir)/def_data.in
echo "$$cmd"; eval $$cmd; \ echo "$$cmd"; eval $$cmd; \
fi fi
tsgetusershell.c: $(top_srcdir)/lib/util/getusershell.c
$(SED) -e 's/^sudo_/testsudoers_/' -e '/config.h/a#define TESTSUDOERS' $(top_srcdir)/lib/util/getusershell.c > $@
sudoers: $(srcdir)/sudoers.in sudoers: $(srcdir)/sudoers.in
cd $(top_builddir) && $(SHELL) config.status --file=plugins/sudoers/$@ cd $(top_builddir) && $(SHELL) config.status --file=plugins/sudoers/$@
@@ -695,7 +699,7 @@ clean:
mostlyclean: clean mostlyclean: clean
distclean: clean distclean: clean
-rm -rf Makefile sudoers sudoers.lo regress/harness \ -rm -rf Makefile sudoers sudoers.lo tsgetusershell.c regress/harness \
.libs $(shlib_map) $(shlib_opt) .libs $(shlib_map) $(shlib_opt)
@if [ -n "$(DEVEL)" -a "$(devdir)" != "$(srcdir)" ]; then \ @if [ -n "$(DEVEL)" -a "$(devdir)" != "$(srcdir)" ]; then \
cmd='rm -rf $(GENERATED)'; \ cmd='rm -rf $(GENERATED)'; \
@@ -3354,6 +3358,18 @@ tsgetgrpw.i: $(srcdir)/tsgetgrpw.c $(devdir)/def_data.h \
$(CC) -E -o $@ $(CPPFLAGS) $< $(CC) -E -o $@ $(CPPFLAGS) $<
tsgetgrpw.plog: tsgetgrpw.i tsgetgrpw.plog: tsgetgrpw.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/tsgetgrpw.c --i-file $< --output-file $@ rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/tsgetgrpw.c --i-file $< --output-file $@
tsgetusershell.o: tsgetusershell.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
$(srcdir)/tsgetgrpw.h $(top_builddir)/config.h
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(HARDENING_CFLAGS) tsgetusershell.c
tsgetusershell.i: tsgetusershell.c $(incdir)/compat/stdbool.h \
$(incdir)/sudo_compat.h $(incdir)/sudo_debug.h \
$(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
$(srcdir)/tsgetgrpw.h $(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
tsgetusershell.plog: tsgetusershell.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file tsgetusershell.c --i-file $< --output-file $@
unesc_str.lo: $(srcdir)/unesc_str.c $(devdir)/def_data.h \ unesc_str.lo: $(srcdir)/unesc_str.c $(devdir)/def_data.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \ $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \ $(incdir)/sudo_conf.h $(incdir)/sudo_debug.h \

View File

@@ -462,12 +462,12 @@ PREFIX(valid_shell)(const char *shell)
sudo_debug_printf(SUDO_DEBUG_INFO, sudo_debug_printf(SUDO_DEBUG_INFO,
"%s: checking /etc/shells for %s", __func__, shell); "%s: checking /etc/shells for %s", __func__, shell);
setusershell(); CALL(setusershell)();
while ((entry = getusershell()) != NULL) { while ((entry = CALL(getusershell)()) != NULL) {
if (strcmp(entry, shell) == 0) if (strcmp(entry, shell) == 0)
debug_return_bool(true); debug_return_bool(true);
} }
endusershell(); CALL(endusershell)();
debug_return_bool(false); debug_return_bool(false);
} }

View File

@@ -43,5 +43,10 @@ struct passwd *testsudoers_getpwent(void);
struct passwd *testsudoers_getpwnam(const char *); struct passwd *testsudoers_getpwnam(const char *);
struct passwd *testsudoers_getpwuid(uid_t); struct passwd *testsudoers_getpwuid(uid_t);
char *testsudoers_getusershell(void);
void testsudoers_setusershell(void);
void testsudoers_endusershell(void);
void testsudoers_setshellfile(const char *file);
int testsudoers_getgrouplist2(const char *name, GETGROUPS_T basegid, int testsudoers_getgrouplist2(const char *name, GETGROUPS_T basegid,
GETGROUPS_T **groupsp, int *ngroupsp); GETGROUPS_T **groupsp, int *ngroupsp);