Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177457058
D61405.1794453552.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
D61405.1794453552.diff
View Options
diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -1325,6 +1325,8 @@
..
pw
..
+ pwd_mkdb
+ ..
quot
..
rpcbind
diff --git a/usr.sbin/pwd_mkdb/Makefile b/usr.sbin/pwd_mkdb/Makefile
--- a/usr.sbin/pwd_mkdb/Makefile
+++ b/usr.sbin/pwd_mkdb/Makefile
@@ -1,3 +1,5 @@
+.include <src.opts.mk>
+
.PATH: ${SRCTOP}/lib/libc/gen # for pw_scan.c
PACKAGE= runtime
@@ -10,4 +12,7 @@
CFLAGS+=-I${.CURDIR}/bootstrap
.endif
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+= tests
+
.include <bsd.prog.mk>
diff --git a/usr.sbin/pwd_mkdb/pwd_mkdb.8 b/usr.sbin/pwd_mkdb/pwd_mkdb.8
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.8
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.8
@@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd April 19, 2025
+.Dd November 10, 2026
.Dt PWD_MKDB 8
.Os
.Sh NAME
@@ -56,6 +56,27 @@
It is important to note that the format used in this system is
different from the historic Version 7 style format.
.Pp
+Login names are subject to the same rules as in
+.Xr pw 8 :
+a name may not be empty, may not begin with a hyphen, and may not contain
+whitespace, control characters, 8-bit characters, or any of the following:
+.Bd -literal -offset indent
+, : + & # % $ ^ ( ) ! @ ~ * ? < > = | \e / " ; ' `
+.Ed
+.Pp
+As an exception, a
+.Ql $
+is permitted as the final character.
+For NIS compatibility entries
+.Pq Ql + , Ql +name , Ql -name , Ql +@netgroup , Ql -@netgroup
+the name is checked after the leading
+.Ql +
+or
+.Ql -
+and optional
+.Ql @ .
+A file containing an invalid login name is rejected.
+.Pp
The options are as follows:
.Bl -tag -width flag
.It Fl C
diff --git a/usr.sbin/pwd_mkdb/pwd_mkdb.c b/usr.sbin/pwd_mkdb/pwd_mkdb.c
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.c
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.c
@@ -78,6 +78,7 @@
void cp(char *, char *, mode_t mode);
void mv(char *, char *);
int scan(FILE *, struct passwd *);
+static int name_ok(const char *);
static void usage(void);
int
@@ -566,7 +567,43 @@
fmt: errno = EFTYPE; /* XXX */
error(pname);
}
+ if (!name_ok(pw->pw_name)) {
+ warnx("invalid login name at line #%d", lcnt);
+ goto fmt;
+ }
+
+ return (1);
+}
+/*
+ * Apply the same character rules as pw_checkname() in usr.sbin/pw so that
+ * names pw(8) refuses cannot be introducde through vipw(8) or a hand edit.
+ * NIS compat entries (+, +name, -name, +@netgroup, -@netgroup) are checked
+ * after their prefix.
+ */
+static int
+name_ok(const char *name)
+{
+ static const char badchars[] = " ,\t:+&#%$^()!@~*?<>=|\\/\";'`";
+ const unsigned char *p;
+
+ p = (const unsigned char *)name;
+ if (*p == '+' || *p == '-') {
+ p++;
+ if (*p == '\0')
+ return (name[0] == '+');
+ if (*p == '@')
+ p++;
+ }
+ if (*p == '\0' || *p == '-')
+ return (0);
+ for (; *p != '\0'; p++) {
+ /* A trailing '$' is allowed for Samba machine accounts. */
+ if (*p == '$' && p[1] == '\0')
+ break;
+ if (*p < ' ' || *p >= 0x7f || strchr(badchars, *p) != NULL)
+ return (0);
+ }
return (1);
}
diff --git a/usr.sbin/pwd_mkdb/tests/Makefile b/usr.sbin/pwd_mkdb/tests/Makefile
new file mode 100644
--- /dev/null
+++ b/usr.sbin/pwd_mkdb/tests/Makefile
@@ -0,0 +1,3 @@
+ATF_TESTS_SH+= pwd_mkdb_test
+
+.include <bsd.test.mk>
diff --git a/usr.sbin/pwd_mkdb/tests/Makefile.depend b/usr.sbin/pwd_mkdb/tests/Makefile.depend
new file mode 100644
--- /dev/null
+++ b/usr.sbin/pwd_mkdb/tests/Makefile.depend
@@ -0,0 +1,10 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/usr.sbin/pwd_mkdb/tests/pwd_mkdb_test.sh b/usr.sbin/pwd_mkdb/tests/pwd_mkdb_test.sh
new file mode 100644
--- /dev/null
+++ b/usr.sbin/pwd_mkdb/tests/pwd_mkdb_test.sh
@@ -0,0 +1,108 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Moin Rahman <bofh@FreeBSD.org>
+#
+
+# Write a master.passwd containing root foloowed by the given entry.
+mkpasswd()
+{
+ printf '%s\n' 'root:*:0:0::0:0:Charlie &:/root:/bin/sh' "$1" \
+ > master.passwd
+ chmod 600 master.passwd
+}
+
+# Expect pwd_mkdb to reject the given login name and leave no databases.
+reject()
+{
+ mkpasswd "$1:*:1001:1001::0:0:test:/nonexistent:/usr/sbin/nologin"
+ atf_check -s exit:1 -e match:"invalid login name at line #2" \
+ pwd_mkdb -p -d "$PWD" "$PWD/master.passwd"
+ atf_check test ! -e pwd.db
+ atf_check test ! -e spwd.db
+ atf_check test ! -e passwd
+}
+
+# Expect pwd_mkdb to accept the given master.passwd entry.
+accept()
+{
+ mkpasswd "$1"
+ atf_check pwd_mkdb -p -d "$PWD" "$PWD/master.passwd"
+ atf_check test -e pwd.db
+ atf_check test -e spwd.db
+ rm -f passwd pwd.db spwd.db
+}
+
+atf_test_case reject_shell_metachars
+reject_shell_metachars_head()
+{
+ atf_set "descr" "Login names with shell metacharacters are rejected"
+}
+reject_shell_metachars_body()
+{
+ reject 'user1$(id$IFS-A)'
+ reject 'a;b'
+ reject 'a|b'
+ reject 'a`id`'
+ reject "a'b"
+ reject 'a&b'
+ reject 'a>b'
+ reject 'a"b'
+ reject 'a\b'
+ reject 'a/b'
+ reject 'a b'
+ reject 'ma$ch'
+}
+
+atf_test_case reject_malformed
+reject_malformed_head()
+{
+ atf_set "descr" "Empty, hyphen-led, control and 8-bit login names are rejected"
+}
+reject_malformed_body()
+{
+ reject ''
+ reject "$(printf 'esc\033[2J')"
+ reject "$(printf 'tab\tname')"
+ reject "$(printf 'caf\303\251')"
+ reject "$(printf 'del\177')"
+}
+
+atf_test_case accept_valid
+accept_valid_head()
+{
+ atf_set "descr" "Valid login names, including a trailing dollar, are accepted"
+}
+accept_valid_body()
+{
+ accept 'first.last_1-x:*:1001:1001::0:0:test:/nonexistent:/usr/sbin/nologin'
+ accept 'mach$:*:1002:1002::0:0:test:/nonexistent:/usr/sbin/nologin'
+ accept '_daemon:*:1003:1003::0:0:test:/nonexistent:/usr/sbin/nologin'
+}
+
+atf_test_case nis_compat
+nis_compat_head()
+{
+ atf_set "descr" "NIS compat entries are accepted and their names checked"
+}
+nis_compat_body()
+{
+ accept '+:::::::::'
+ accept '+user:::::::::'
+ accept '-user:::::::::'
+ accept '+@netgroup:::::::::'
+ accept '-@netgroup:::::::::'
+ reject '-'
+ reject '+@'
+ reject '+@ng$(id)'
+ reject '-x$(id)'
+ reject '+-x'
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case reject_shell_metachars
+ atf_add_test_case reject_malformed
+ atf_add_test_case accept_valid
+ atf_add_test_case nis_compat
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 12, 3:19 AM (10 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41666446
Default Alt Text
D61405.1794453552.diff (6 KB)
Attached To
Mode
D61405: pwd_mkdb: validate login names
Attached
Detach File
Event Timeline