From: Todd C. Miller Date: Mon, 21 May 2012 19:39:24 +0000 (-0400) Subject: Add tests for sudoers mode, owner and group checks. X-Git-Tag: SUDO_1_8_6^2~177 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ca9331d49835441f435faf1ee996adddea2897a8;p=sudo Add tests for sudoers mode, owner and group checks. --- diff --git a/plugins/sudoers/Makefile.in b/plugins/sudoers/Makefile.in index a1c633c3a..0d1f63bc4 100644 --- a/plugins/sudoers/Makefile.in +++ b/plugins/sudoers/Makefile.in @@ -355,7 +355,7 @@ check: $(TEST_PROGS) visudo testsudoers rval=`expr $$rval + $$failed`; exit $$rval clean: - -$(LIBTOOL) --mode=clean rm -f $(PROGS) $(TEST_PROGS) *.lo *.o *.la *.a stamp-* core *.core core.* *.out *.toke *.err + -$(LIBTOOL) --mode=clean rm -f $(PROGS) $(TEST_PROGS) *.lo *.o *.la *.a stamp-* core *.core core.* *.out *.toke *.err *.inc mostlyclean: clean diff --git a/plugins/sudoers/regress/testsudoers/test1.sh b/plugins/sudoers/regress/testsudoers/test1.sh index a6358b3fa..31f5ba029 100755 --- a/plugins/sudoers/regress/testsudoers/test1.sh +++ b/plugins/sudoers/regress/testsudoers/test1.sh @@ -5,6 +5,7 @@ # This is RedHat bug Bug 667103. # +exec 2>&1 ./testsudoers -g bin root id <&1 +./testsudoers -U $UID root id <&1 +./testsudoers -U $UID root id <&1 +./testsudoers -U 1 root id <$TESTFILE <&1 + +# Test world writable +chmod 666 $TESTFILE +./testsudoers -U $UID root id < /* @@ -160,7 +161,7 @@ main(int argc, char *argv[]) dflag = 0; grfile = pwfile = NULL; - while ((ch = getopt(argc, argv, "dg:G:h:p:tu:U:")) != -1) { + while ((ch = getopt(argc, argv, "dg:G:h:P:p:tu:U:")) != -1) { switch (ch) { case 'd': dflag = 1; @@ -169,7 +170,7 @@ main(int argc, char *argv[]) user_host = optarg; break; case 'G': - grfile = optarg; + sudoers_gid = (gid_t)atoi(optarg); break; case 'g': runas_group = optarg; @@ -177,11 +178,14 @@ main(int argc, char *argv[]) case 'p': pwfile = optarg; break; + case 'P': + grfile = optarg; + break; case 't': trace_print = testsudoers_print; break; case 'U': - sudoers_uid = atoi(optarg); + sudoers_uid = (uid_t)atoi(optarg); break; case 'u': runas_user = optarg; @@ -417,11 +421,44 @@ set_fqdn(void) } FILE * -open_sudoers(const char *path, bool doedit, bool *keepopen) +open_sudoers(const char *sudoers, bool doedit, bool *keepopen) { + struct stat sb; + FILE *fp = NULL; + char *sudoers_base; debug_decl(open_sudoers, SUDO_DEBUG_UTIL) - debug_return_ptr(fopen(path, "r")); + sudoers_base = strrchr(sudoers, '/'); + if (sudoers_base != NULL) + sudoers_base++; + + switch (sudo_secure_file(sudoers, sudoers_uid, sudoers_gid, &sb)) { + case SUDO_PATH_SECURE: + fp = fopen(sudoers, "r"); + break; + case SUDO_PATH_MISSING: + warning("unable to stat %s", sudoers_base); + break; + case SUDO_PATH_BAD_TYPE: + warningx("%s is not a regular file", sudoers_base); + break; + case SUDO_PATH_WRONG_OWNER: + warningx("%s should be owned by uid %u", + sudoers_base, (unsigned int) sudoers_uid); + break; + case SUDO_PATH_WORLD_WRITABLE: + warningx("%s is world writable", sudoers_base); + break; + case SUDO_PATH_GROUP_WRITABLE: + warningx("%s should be owned by gid %u", + sudoers_base, (unsigned int) sudoers_gid); + break; + default: + /* NOTREACHED */ + break; + } + + debug_return_ptr(fp); } void @@ -680,6 +717,6 @@ static int testsudoers_print(const char *msg) void usage(void) { - (void) fprintf(stderr, "usage: %s [-dt] [-G grfile] [-g group] [-h host] [-p pwfile] [-U sudoers_uid] [-u user] [args]\n", getprogname()); + (void) fprintf(stderr, "usage: %s [-dt] [-G sudoers_gid] [-g group] [-h host] [-p grfile] [-p pwfile] [-U sudoers_uid] [-u user] [args]\n", getprogname()); exit(1); }