From 1718a38ffd2b23e234fb63713d49786a25b680c6 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 7 Jun 2015 20:59:59 +0000 Subject: [PATCH] Fix setting uid/gid min/max via pw --- usr.sbin/pw/pw.c | 17 ++++++-- usr.sbin/pw/tests/Makefile | 3 +- usr.sbin/pw/tests/pw-modified.conf | 62 ++++++++++++++++++++++++++++++ usr.sbin/pw/tests/pw_config.sh | 10 +++++ 4 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 usr.sbin/pw/tests/pw-modified.conf diff --git a/usr.sbin/pw/pw.c b/usr.sbin/pw/pw.c index d8d8c034080c..30fb55b4f61d 100644 --- a/usr.sbin/pw/pw.c +++ b/usr.sbin/pw/pw.c @@ -251,11 +251,20 @@ main(int argc, char *argv[]) addarg(&arglist, 'g', optarg); break; } - /* FALLTHROUGH */ - case 'u': if (strspn(optarg, "0123456789") != strlen(optarg)) - errx(EX_USAGE, "%s expects a number", - which == 1 ? "-g" : "-u" ); + errx(EX_USAGE, "-g expects a number"); + id = strtonum(optarg, 0, LONG_MAX, &errstr); + if (errstr != NULL) + errx(EX_USAGE, "Bad id '%s': %s", optarg, + errstr); + break; + case 'u': + if (strspn(optarg, "0123456789,") != strlen(optarg)) + errx(EX_USAGE, "-u expects a number"); + if (strchr(optarg, ',') != NULL) { + addarg(&arglist, 'u', optarg); + break; + } id = strtonum(optarg, 0, LONG_MAX, &errstr); if (errstr != NULL) errx(EX_USAGE, "Bad id '%s': %s", optarg, diff --git a/usr.sbin/pw/tests/Makefile b/usr.sbin/pw/tests/Makefile index 09fe93c2ff71..193b8ab58530 100644 --- a/usr.sbin/pw/tests/Makefile +++ b/usr.sbin/pw/tests/Makefile @@ -19,7 +19,8 @@ ATF_TESTS_SH= pw_etcdir \ TEST_METADATA.${tp}+= required_user="root" .endfor -FILES= group helper_functions.shin master.passwd pw.conf +FILES= group helper_functions.shin master.passwd pw.conf \ + pw-modified.conf FILESDIR= ${TESTSDIR} ATF_TESTS_SH+= pw_test diff --git a/usr.sbin/pw/tests/pw-modified.conf b/usr.sbin/pw/tests/pw-modified.conf new file mode 100644 index 000000000000..84f44e758872 --- /dev/null +++ b/usr.sbin/pw/tests/pw-modified.conf @@ -0,0 +1,62 @@ +# +# pw.conf - user/group configuration defaults +# + +# Password for new users? no=nologin yes=loginid none=blank random=random +defaultpasswd = "no" + +# Reuse gaps in uid sequence? (yes or no) +reuseuids = "no" + +# Reuse gaps in gid sequence? (yes or no) +reusegids = "no" + +# Path to the NIS passwd file (blank or 'no' for none) +nispasswd = + +# Obtain default dotfiles from this directory +skeleton = "/usr/share/skel" + +# Mail this file to new user (/etc/newuser.msg or no) +newmail = "no" + +# Log add/change/remove information in this file +logfile = "/var/log/userlog" + +# Root directory in which $HOME directory is created +home = "/home" + +# Mode for the new $HOME directory, will be modified by umask +homemode = 0777 + +# Colon separated list of directories containing valid shells +shellpath = "/bin" + +# Comma separated list of available shells (without paths) +shells = "sh","csh","tcsh" + +# Default shell (without path) +defaultshell = "sh" + +# Default group (leave blank for new group per user) +defaultgroup = "" + +# Extra groups for new users +extragroups = + +# Default login class for new users +defaultclass = "" + +# Range of valid default user ids +minuid = 2000 +maxuid = 5000 + +# Range of valid default group ids +mingid = 2100 +maxgid = 6000 + +# Days after which account expires (0=disabled) +expire_days = 0 + +# Days after which password expires (0=disabled) +password_days = 0 diff --git a/usr.sbin/pw/tests/pw_config.sh b/usr.sbin/pw/tests/pw_config.sh index b5a4fad723f9..fb6489a0b31c 100755 --- a/usr.sbin/pw/tests/pw_config.sh +++ b/usr.sbin/pw/tests/pw_config.sh @@ -11,6 +11,16 @@ generate_config_body() { cat ${HOME}/foo.conf } +atf_test_case modify_config_uid_gid_boundaries +modify_config_uid_gid_boundaries_body() { + atf_check -s exit:0 \ + ${PW} useradd -D -C ${HOME}/foo.conf \ + -u 2000,5000 -i 2100,6000 + atf_check -o file:$(atf_get_srcdir)/pw-modified.conf \ + cat ${HOME}/foo.conf +} + atf_init_test_cases() { atf_add_test_case generate_config + atf_add_test_case modify_config_uid_gid_boundaries }