public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/main] Cygwin: add very simple newgrp(1) tool
Date: Fri, 13 Jan 2023 20:30:21 +0000 (GMT)	[thread overview]
Message-ID: <20230113203021.D18913858C39@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c8ddd03cb015cf7f9137c42a6cbd0639c6c93a23

commit c8ddd03cb015cf7f9137c42a6cbd0639c6c93a23
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Fri Jan 13 20:59:07 2023 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Fri Jan 13 20:59:29 2023 +0100

    Cygwin: add very simple newgrp(1) tool
    
    This tool allows to change the primary group for a child process.
    The new primary group MUST be part of the supplementary group list
    of newgrp's user token.
    
    The command started as child process is specified on the command line.
    If it's missing, start the user's default shell with the new primary
    group.
    
    TODO: Implement '-' option.
          Add command description to documentation.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/utils/Makefile.am |   1 +
 winsup/utils/newgrp.c    | 101 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)

diff --git a/winsup/utils/Makefile.am b/winsup/utils/Makefile.am
index 6be5b12c239a..75da1163b629 100644
--- a/winsup/utils/Makefile.am
+++ b/winsup/utils/Makefile.am
@@ -31,6 +31,7 @@ bin_PROGRAMS = \
 	mkgroup \
 	mkpasswd \
 	mount \
+	newgrp \
 	passwd \
 	pldd \
 	profiler \
diff --git a/winsup/utils/newgrp.c b/winsup/utils/newgrp.c
new file mode 100644
index 000000000000..012c3188f477
--- /dev/null
+++ b/winsup/utils/newgrp.c
@@ -0,0 +1,101 @@
+/* newgrp.c
+
+This file is part of Cygwin.
+
+This software is a copyrighted work licensed under the terms of the
+Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
+details. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <ctype.h>
+#include <string.h>
+#include <grp.h>
+#include <pwd.h>
+
+int
+main (int argc, const char **argv)
+{
+  struct group *gr;
+  gid_t gid;
+  const char *cmd;
+  const char **cmd_av;
+  const char *fake_av[2];
+
+  /* TODO: Implement '-' option */
+  /* TODO: Add command description to documentation */
+
+  if (argc < 2 || argv[1][0] == '-')
+    {
+      fprintf (stderr,
+	       "Usage: %1$s group [command [args...]]\n"
+	       "\n"
+	       "%1$s changes the current primary group for a command.\n"
+	       "The primary group must be member of the supplementary group\n"
+	       "list of the user.\n"
+	       "The command and its arguments are specified on the command\n"
+	       "line.  Default is the user's standard shell.\n",
+	       program_invocation_short_name);
+      return 1;
+    }
+  if (isdigit ((int) argv[1][0]))
+    {
+      char *e = NULL;
+
+      gid = strtol (argv[1], &e, 10);
+      if (e && *e != '\0')
+	{
+	  fprintf (stderr, "%s: invalid gid `%s'\n",
+		   program_invocation_short_name, argv[1]);
+	  return 2;
+	}
+      gr = getgrgid (gid);
+      if (!gr)
+	{
+	  fprintf (stderr, "%s: unknown group gid `%u'\n",
+		   program_invocation_short_name, gid);
+	  return 2;
+	}
+    }
+  else
+    {
+      gr = getgrnam (argv[1]);
+      if (!gr)
+	{
+	  fprintf (stderr, "%s: unknown group name `%s'\n",
+		   program_invocation_short_name, argv[1]);
+	  return 2;
+	}
+      gid = gr->gr_gid;
+    }
+  if (setgid (gid) != 0)
+    {
+      fprintf (stderr, "%s: can't switch primary group to `%s'\n",
+	       program_invocation_short_name, argv[1]);
+      return 2;
+    }
+  argc -= 2;
+  argv += 2;
+  if (argc < 1)
+    {
+      struct passwd *pw = getpwuid (getuid ());
+      if (!pw)
+	cmd = "/usr/bin/bash";
+      else
+	cmd = pw->pw_shell;
+      fake_av[0] = cmd;
+      fake_av[1] = NULL;
+      cmd_av = fake_av;
+    }
+  else
+    {
+      cmd = argv[0];
+      cmd_av = argv;
+    }
+  execvp (cmd, (char **) cmd_av);
+  fprintf (stderr, "%s: failed to start `%s': %s\n",
+	   program_invocation_short_name, cmd, strerror (errno));
+  return 3;
+}

                 reply	other threads:[~2023-01-13 20:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230113203021.D18913858C39@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).