Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/sudoers.man.in
Original file line number Diff line number Diff line change
Expand Up @@ -2589,11 +2589,16 @@ For example, given:
\fBsudo\fR
will suspend processing of the current file and read each file in
\fI/etc/sudoers.d\fR,
skipping file names that end in
\(oq~\(cq
or contain a
skipping file names that begin with
\(oq.\&\(cq
character to avoid causing problems with package manager or editor
or end in
\(oq~\(cq,
\(oq.bak\(cq,
\(oq.orig\(cq,
\(oq.rej\(cq,
or
\(oq.swp\(cq
to avoid causing problems with package manager or editor
temporary/backup files.
.PP
Files are parsed in sorted lexical order.
Expand Down
13 changes: 9 additions & 4 deletions docs/sudoers.mdoc.in
Original file line number Diff line number Diff line change
Expand Up @@ -2442,11 +2442,16 @@ For example, given:
.Nm sudo
will suspend processing of the current file and read each file in
.Pa /etc/sudoers.d ,
skipping file names that end in
.Ql ~
or contain a
skipping file names that begin with
.Ql .\&
character to avoid causing problems with package manager or editor
or end in
.Ql ~ ,
.Ql .bak ,
.Ql .orig ,
.Ql .rej ,
or
.Ql .swp
to avoid causing problems with package manager or editor
temporary/backup files.
.Pp
Files are parsed in sorted lexical order.
Expand Down
Empty file.
29 changes: 29 additions & 0 deletions plugins/sudoers/regress/visudo/test12.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh
#
# Test that includedir files with a '.' in the name are parsed
# and editor backup files are ignored.
#

: ${VISUDO=visudo}

TESTDIR="${TMPDIR:-/tmp}/sudo-visudo-test12.$$"
trap 'rm -rf "$TESTDIR"' 0 1 2 3 15
mkdir -p "$TESTDIR/sudoers.d" || exit 1

cat >"$TESTDIR/sudoers" <<EOF
#includedir $TESTDIR/sudoers.d
FOO ALL = ALL
EOF

cat >"$TESTDIR/sudoers.d/myrules.conf" <<EOF
User_Alias FOO = nobody
EOF

for file in .hidden myrules.conf~ myrules.conf.bak myrules.conf.orig myrules.conf.rej myrules.conf.swp; do
cat >"$TESTDIR/sudoers.d/$file" <<EOF
this is not valid sudoers
EOF
done

$VISUDO -cf "$TESTDIR/sudoers" >"$TESTDIR/out" 2>&1 || exit 1
grep -q "myrules.conf: parsed OK" "$TESTDIR/out" || exit 1
35 changes: 24 additions & 11 deletions plugins/sudoers/toke.c
Original file line number Diff line number Diff line change
Expand Up @@ -5706,9 +5706,29 @@ pl_compare(const void *v1, const void *v2)
return strcmp((*p2)->path, (*p1)->path);
}

/*
* Return true if name is a package manager or editor temporary/backup
* file that should be ignored in an includedir.
*/
static bool
ignore_sudoers_file(const char *name, size_t namelen)
{
static const char * const suffixes[] = { ".bak", ".orig", ".rej", ".swp" };
size_t i;

if (namelen == 0 || name[0] == '.' || name[namelen - 1] == '~')
return true;
for (i = 0; i < nitems(suffixes); i++) {
const size_t len = strlen(suffixes[i]);
if (namelen > len && strcmp(&name[namelen - len], suffixes[i]) == 0)
return true;
}
return false;
}

/*
* Open dirpath and fill in pathsp with an array of regular files
* that do not end in '~' or contain a '.'.
* that are not package manager or editor temporary/backup files.
* Returns the number of files or SIZE_MAX (-1) on error.
* If zero files are found, NULL is stored in pathsp.
*/
Expand Down Expand Up @@ -5742,18 +5762,11 @@ read_dir_files(const char *dirpath, struct path_list ***pathsp, int verbose)
size_t len;
char *path;

/* Ignore files that end in '~' or have a '.' in them. */
if (namelen == 0 || name[namelen - 1] == '~' || strchr(name, '.') != NULL) {
if (ignore_sudoers_file(name, namelen)) {
/* Warn about ignored files not starting with '.' if verbose. */
if (namelen > 0 && name[0] != '.' && verbose > 1) {
if (name[namelen - 1] == '~' ||
(namelen > 4 && strcmp(&name[namelen - 4], ".bak") == 0)) {
fprintf(stderr, U_("%s/%s: %s"), dirpath, name,
U_("ignoring editor backup file"));
} else {
fprintf(stderr, U_("%s/%s: %s"), dirpath, name,
U_("ignoring file name containing '.'"));
}
fprintf(stderr, U_("%s/%s: %s"), dirpath, name,
U_("ignoring editor backup file"));
fputc('\n', stderr);
}
continue;
Expand Down
35 changes: 24 additions & 11 deletions plugins/sudoers/toke.l
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,29 @@ pl_compare(const void *v1, const void *v2)
return strcmp((*p2)->path, (*p1)->path);
}

/*
* Return true if name is a package manager or editor temporary/backup
* file that should be ignored in an includedir.
*/
static bool
ignore_sudoers_file(const char *name, size_t namelen)
{
static const char * const suffixes[] = { ".bak", ".orig", ".rej", ".swp" };
size_t i;

if (namelen == 0 || name[0] == '.' || name[namelen - 1] == '~')
return true;
for (i = 0; i < nitems(suffixes); i++) {
const size_t len = strlen(suffixes[i]);
if (namelen > len && strcmp(&name[namelen - len], suffixes[i]) == 0)
return true;
}
return false;
}

/*
* Open dirpath and fill in pathsp with an array of regular files
* that do not end in '~' or contain a '.'.
* that are not package manager or editor temporary/backup files.
* Returns the number of files or SIZE_MAX (-1) on error.
* If zero files are found, NULL is stored in pathsp.
*/
Expand Down Expand Up @@ -967,18 +987,11 @@ read_dir_files(const char *dirpath, struct path_list ***pathsp, int verbose)
size_t len;
char *path;

/* Ignore files that end in '~' or have a '.' in them. */
if (namelen == 0 || name[namelen - 1] == '~' || strchr(name, '.') != NULL) {
if (ignore_sudoers_file(name, namelen)) {
/* Warn about ignored files not starting with '.' if verbose. */
if (namelen > 0 && name[0] != '.' && verbose > 1) {
if (name[namelen - 1] == '~' ||
(namelen > 4 && strcmp(&name[namelen - 4], ".bak") == 0)) {
fprintf(stderr, U_("%s/%s: %s"), dirpath, name,
U_("ignoring editor backup file"));
} else {
fprintf(stderr, U_("%s/%s: %s"), dirpath, name,
U_("ignoring file name containing '.'"));
}
fprintf(stderr, U_("%s/%s: %s"), dirpath, name,
U_("ignoring editor backup file"));
fputc('\n', stderr);
}
continue;
Expand Down
Loading