chiark / gitweb /
Rationalize the behaviour of the `-G' and `-U' options.
[tripe] / tripe.c
diff --git a/tripe.c b/tripe.c
index 33a97b7cc98b9e6c91c77fd34f4d7bfd491359b9..9e2256596609f3218aa9774566f4c011abd2de27 100644 (file)
--- a/tripe.c
+++ b/tripe.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: tripe.c,v 1.8 2002/01/13 17:28:29 mdw Exp $
+ * $Id: tripe.c,v 1.9 2003/04/15 14:11:09 mdw Exp $
  *
  * Main program
  *
@@ -29,6 +29,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: tripe.c,v $
+ * Revision 1.9  2003/04/15 14:11:09  mdw
+ * Rationalize the behaviour of the `-G' and `-U' options.
+ *
  * Revision 1.8  2002/01/13 17:28:29  mdw
  * Don't turn tracing on if tracing is turned off (!).
  *
@@ -103,8 +106,9 @@ void interval(struct timeval *tv, void *v)
 
 static void usage(FILE *fp)
 {
-  pquis(fp, "Usage: $ [-D] [-p port] [-T trace-opts] [-d dir] [-a socket]\n\
-       [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n");
+  pquis(fp, "Usage: $ [-D] [-d dir] [-p port] [-U user] [-G group]\n\
+       [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n\
+       [-a socket] [-T trace-opts]\n");
 }
 
 static void version(FILE *fp)
@@ -127,6 +131,8 @@ Options:\n\
 -D, --daemon           Run in the background.\n\
 -d, --directory=DIR    Switch to directory DIR (default $TRIPEDIR).\n\
 -p, --port=PORT                Select UDP port to listen to.\n\
+-u, --setuid=USER      Set uid to USER after initialization.\n\
+-g, --setgid=GROUP     Set gid to GROUP after initialization.\n\
 -k, --priv-keyring=FILE        Get private key from FILE.\n\
 -K, --pub-keyring=FILE Get public keys from FILE.\n\
 -t, --tag=KEYTAG       Use private key labelled TAG.\n\
@@ -201,28 +207,30 @@ int main(int argc, char *argv[])
        f |= f_daemon;
        break;
       case 'U': {
+       struct passwd *pw;
        char *p;
        unsigned long i = strtoul(optarg, &p, 0);
        if (!*p)
-         u = i;
-       else {
-         struct passwd *pw;
-         if ((pw = getpwnam(optarg)) == 0)
-           die(EXIT_FAILURE, "user name `%s' not found", optarg);
-         u = pw->pw_uid;
-       }
+         pw = getpwuid(i);
+       else
+         pw = getpwnam(optarg);
+       if (!pw)
+         die(EXIT_FAILURE, "user `%s' not found", optarg);
+       u = pw->pw_uid;
+       if (g == -1)
+         g = pw->pw_gid;
       } break;
       case 'G': {
+       struct group *gr;
        char *p;
        unsigned long i = strtoul(optarg, &p, 0);
        if (!*p)
-         g = i;
-       else {
-         struct group *gr;
-         if ((gr = getgrnam(optarg)) == 0)
-           die(EXIT_FAILURE, "group name `%s' not found", optarg);
-         g = gr->gr_gid;
-       }
+         gr = getgrgid(i);
+       else
+         gr = getgrnam(optarg);
+       if (!gr)
+         die(EXIT_FAILURE, "group `%s' not found", optarg);
+       g = gr->gr_gid;
       } break;
          
       case 'p': {
@@ -284,13 +292,13 @@ int main(int argc, char *argv[])
   p_init(port);
   if (!(f & f_daemon))
     a_create(STDIN_FILENO, STDOUT_FILENO);
-  if (g != -1) {
-    if (setgid(g)) {
+  if (g != (gid_t)-1) {
+    if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) {
       die(EXIT_FAILURE, "couldn't setgid to %u: %s",
          (unsigned)g, strerror(errno));
     }
   }
-  if (u != -1) {
+  if (u != (uid_t)-1) {
     if (setuid(u)) {
       die(EXIT_FAILURE, "couldn't setuid to %u: %s",
          (unsigned)u, strerror(errno));