On 05/12/2025 19:41, Corinna Vinschen wrote:
From: Corinna Vinschen <[email protected]>
- allow calling without argument
- allow not only - but also -l to regenerate stock environment
- allow numerical group IDs
- do not advertise the ability to run an arbitrary command instead
of just starting a new shell
Signed-off-by: Corinna Vinschen <[email protected]>
---
winsup/utils/newgrp.c | 28 ++++++++++++++++++----------
1 file changed, 18 insertions(+), 10 deletions(-)
diff --git a/winsup/utils/newgrp.c b/winsup/utils/newgrp.c
index 414e8cdf8edc..da54637c57d2 100644
--- a/winsup/utils/newgrp.c
+++ b/winsup/utils/newgrp.c
@@ -140,16 +140,16 @@ main (int argc, const char **argv)
setlocale (LC_ALL, "");
- if (argc < 2 || (argv[1][0] == '-' && argv[1][1]))
- {
- fprintf (stderr, "Usage: %s [-] [group] [command [args...]]\n",
- program_invocation_short_name);
- return 1;
- }
-
/* Check if we have to regenerate a stock environment */
- if (argv[1][0] == '-')
+ if (argc > 1 && argv[1][0] == '-')
{
+ if (argv[1][1] != '\0' && strcmp (argv[1], "-l") != 0)
+ {
+ /* Do not advertise the ability to run an arbitrary command. */
+ fprintf (stderr, "Usage: %s [-] [group]\n",
Maybe '[-|-l]'?
+ program_invocation_short_name);
+ return 1;
+ }
new_child_env = true;
--argc;
++argv;
@@ -165,8 +165,16 @@ main (int argc, const char **argv)
}
else
{
- gr = getgrnam (argv[1]);
- if (!gr)
+ char *eptr;
+
+ if ((gr = getgrnam (argv[1])) != NULL)
+ /*valid*/;
+ else if (isdigit ((int) argv[1][0])
+ && (gid = strtoul (argv[1], &eptr, 10)) != ULONG_MAX
+ && *eptr == '\0'
+ && (gr = getgrgid (gid)) != NULL)
I spent a bit of time worrying how this handled edge cases like '' or
'0', but I think it's all good!
+ /*valid*/;
+ else
{
fprintf (stderr, "%s: group '%s' does not exist\n",
program_invocation_short_name, argv[1]);