From nobody Mon May 06 16:19:21 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VY66x5Qgkz5KWW1; Mon, 06 May 2024 16:19:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VY66x4xsnz4ZmL; Mon, 6 May 2024 16:19:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1715012361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BsHQereJrnRgtfb2hJ1m0efahdgtiDcdB9PAk/MC5JE=; b=qdGKw9aiLAh1H5T+a/lSpI+aHUg/VXfsx9eWpgQmiA9Pzu+8Td/9Z9/zsFcKFhXc+RrFoa pSn+zPxQSjhcruij1Q8MKZ+NaMmfZPU72SrfFg5pp7NSnYklw+20Vl5wmGMm2/h0oOz0a5 yfOMkAQnYJ60MAhMF9vh53qQSsa7ngSNfQbX0PWz63FeahH6C7bRm4iT7ujhfAdswuYpkV PMH5RtNjtbCducx7zUIN0BWmdXtihUq7BgVFD4h9zJc5vq/TUSZk/G/4n2dQFrh9uYvtIW 30wcrazVmdCOxX1yiBIBeYtvRsiuPgOZEP7AbfbY4HjXTqfFBFbd1+YAfgbyzQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1715012361; a=rsa-sha256; cv=none; b=vcssgfnHbIeN3f/ONspTBx29QWypptM57UBw25UEIbbMPXuP8HM45LC44F9ZDrYj2x5wCR BhaUwAvpD1l69p1Ge+U7/3M715xZ3DTFSkFaMVtYa4nFv+4hydGtI7K1zfYsijHVo1WZOl xC051TmV6S6Htf2QYfxbAInIY+z5AEIeMeiFhlb7Qk75phtahj2/1BkidSg+/QgLnxIp/j gpovwsS5LS+x1V+KL5a5eIYqbwukVygnj3XU1TbaCsgd4gZH2kEnxM9dZ4CowdyqvGLJQi sdjayGNkgxMuf1AuMyTYuSvfiVjSPMCteIB3FuUBJqxiEpqqh6TZzT4NUyNBTA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1715012361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BsHQereJrnRgtfb2hJ1m0efahdgtiDcdB9PAk/MC5JE=; b=N7t4vvfL44UJkjp6BzIg5U4Gs+HFuDOnHLpcnXG8RR5r6K0jF6nPjqbsy8NsWjWeAJwhZK bqM7kzMpirwfx5O0+zwB3P34yiq5BFqdmSPkXKv2rLumULpB+0xNRB6Rrp9yX2+z8R9HI0 E5KdgK1Nk+7xCorKMEi8xImoNzrIBv7VcXNCcEP6fRdBHNJ40c04gPQzBacqtGo1KBy6tW oDpwhQrU9cfZH4J7Iz1xQ8+7LFWU5yFHuxYAhDlZiFWaYhzJuySUjqpYKyu/iRHeZI7l0Q GZMXIKGN+ZMqKn7dCOzLi/+GwiljW4UEvvFzq3i2k/rJBYUVvwoLx7u8X+LPQA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VY66x4Y66z13tK; Mon, 6 May 2024 16:19:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 446GJLSK022759; Mon, 6 May 2024 16:19:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 446GJLPG022756; Mon, 6 May 2024 16:19:21 GMT (envelope-from git) Date: Mon, 6 May 2024 16:19:21 GMT Message-Id: <202405061619.446GJLPG022756@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: c86119328e6b - main - pw: Don't silently ignore unparsed command line arguments. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c86119328e6b2cfeb4f9319f6b154524d88caaf4 Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=c86119328e6b2cfeb4f9319f6b154524d88caaf4 commit c86119328e6b2cfeb4f9319f6b154524d88caaf4 Author: Dag-Erling Smørgrav AuthorDate: 2024-05-06 16:18:43 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-05-06 16:18:43 +0000 pw: Don't silently ignore unparsed command line arguments. MFC after: 3 days Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D45097 --- usr.sbin/pw/pw.c | 13 +++++++++++-- usr.sbin/pw/pw.h | 2 ++ usr.sbin/pw/pw_group.c | 31 ++++++++++++++++++++++++++----- usr.sbin/pw/pw_user.c | 42 +++++++++++++++++++++++++++++++++++------- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/usr.sbin/pw/pw.c b/usr.sbin/pw/pw.c index 063553dd084f..fc17f6dba022 100644 --- a/usr.sbin/pw/pw.c +++ b/usr.sbin/pw/pw.c @@ -101,13 +101,16 @@ static int (*cmdfunc[W_NUM][M_NUM])(int argc, char **argv, char *_name) = { struct pwconf conf; +static int mode = -1; +static int which = -1; + static int getindex(const char *words[], const char *word); static void cmdhelp(int mode, int which); int main(int argc, char *argv[]) { - int mode = -1, which = -1, tmp; + int tmp; struct stat st; char arg, *arg1; bool relocated, nis; @@ -375,5 +378,11 @@ cmdhelp(int mode, int which) fprintf(stderr, "%s", help[which][mode]); } - exit(EXIT_FAILURE); + exit(EX_USAGE); +} + +void +usage(void) +{ + cmdhelp(mode, which); } diff --git a/usr.sbin/pw/pw.h b/usr.sbin/pw/pw.h index e8ec95079fec..c3725693f91d 100644 --- a/usr.sbin/pw/pw.h +++ b/usr.sbin/pw/pw.h @@ -114,3 +114,5 @@ uintmax_t strtounum(const char * __restrict, uintmax_t, uintmax_t, const char ** __restrict); bool grp_has_member(struct group *grp, const char *name); + +void usage(void); diff --git a/usr.sbin/pw/pw_group.c b/usr.sbin/pw/pw_group.c index b2777177b9e6..1941c03aa2c5 100644 --- a/usr.sbin/pw/pw_group.c +++ b/usr.sbin/pw/pw_group.c @@ -273,9 +273,13 @@ pw_group_next(int argc, char **argv, char *arg1 __unused) quiet = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (quiet) freopen(_PATH_DEVNULL, "w", stderr); @@ -332,9 +336,13 @@ pw_group_show(int argc, char **argv, char *arg1) all = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (quiet) freopen(_PATH_DEVNULL, "w", stderr); @@ -391,9 +399,13 @@ pw_group_del(int argc, char **argv, char *arg1) nis = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (quiet) freopen(_PATH_DEVNULL, "w", stderr); @@ -551,9 +563,13 @@ pw_group_add(int argc, char **argv, char *arg1) nis = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (quiet) freopen(_PATH_DEVNULL, "w", stderr); @@ -645,9 +661,14 @@ pw_group_mod(int argc, char **argv, char *arg1) nis = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); + if (quiet) freopen(_PATH_DEVNULL, "w", stderr); cnf = get_userconfig(cfg); diff --git a/usr.sbin/pw/pw_user.c b/usr.sbin/pw/pw_user.c index 66f021fff983..89354b249935 100644 --- a/usr.sbin/pw/pw_user.c +++ b/usr.sbin/pw/pw_user.c @@ -708,9 +708,13 @@ pw_user_next(int argc, char **argv, char *name __unused) quiet = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (quiet) freopen(_PATH_DEVNULL, "w", stderr); @@ -772,9 +776,13 @@ pw_user_show(int argc, char **argv, char *arg1) v7 = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (quiet) freopen(_PATH_DEVNULL, "w", stderr); @@ -855,9 +863,13 @@ pw_user_del(int argc, char **argv, char *arg1) nis = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (quiet) freopen(_PATH_DEVNULL, "w", stderr); @@ -1003,9 +1015,13 @@ pw_user_lock(int argc, char **argv, char *arg1) /* compatibility */ break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); return (pw_userlock(arg1, M_LOCK)); } @@ -1022,9 +1038,13 @@ pw_user_unlock(int argc, char **argv, char *arg1) /* compatibility */ break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); return (pw_userlock(arg1, M_UNLOCK)); } @@ -1291,9 +1311,13 @@ pw_user_add(int argc, char **argv, char *arg1) nis = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (geteuid() != 0 && ! dryrun) errx(EX_NOPERM, "you must be root"); @@ -1604,9 +1628,13 @@ pw_user_mod(int argc, char **argv, char *arg1) nis = true; break; default: - exit(EX_USAGE); + usage(); } } + argc -= optind; + argv += optind; + if (argc > 0) + usage(); if (geteuid() != 0 && ! dryrun) errx(EX_NOPERM, "you must be root");