chiark / gitweb /
Don't try to run bogus startup scripts.
[tripe] / tripe.c
diff --git a/tripe.c b/tripe.c
index cacc7708a5d4b199ac59aecf7cc2d1a2061f68bc..adbd7a11e54ab3f298ef9578ee63c28c0be05f36 100644 (file)
--- a/tripe.c
+++ b/tripe.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: tripe.c,v 1.2 2001/02/03 22:33:00 mdw Exp $
+ * $Id: tripe.c,v 1.11 2003/05/17 11:02:03 mdw Exp $
  *
  * Main program
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: tripe.c,v $
+ * Revision 1.11  2003/05/17 11:02:03  mdw
+ * Document the new `-b' option.
+ *
+ * Revision 1.10  2003/05/16 12:09:03  mdw
+ * Allow binding to a chosen address.
+ *
+ * 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 (!).
+ *
+ * Revision 1.7  2002/01/13 17:14:05  mdw
+ * Don't include the tracing option in the help if not compiled in.
+ *
+ * Revision 1.6  2001/06/19 22:08:37  mdw
+ * Moved buffers to peer.c.
+ *
+ * Revision 1.5  2001/02/16 21:43:12  mdw
+ * Provide a more helpful usage message.
+ *
+ * Revision 1.4  2001/02/16 21:41:31  mdw
+ * Add a new buffer.
+ *
+ * Revision 1.3  2001/02/04 17:10:40  mdw
+ * Remove a debugging @abort@ call.
+ *
  * Revision 1.2  2001/02/03 22:33:00  mdw
  * Stuff more randomness into the pool in the interval timer.
  *
@@ -44,7 +71,6 @@
 /*----- Global variables --------------------------------------------------*/
 
 sel_state sel;
-octet buf_i[PKBUFSZ], buf_o[PKBUFSZ];
 
 /*----- Static variables --------------------------------------------------*/
 
@@ -67,7 +93,7 @@ void interval(struct timeval *tv, void *v)
 {
   struct timeval tvv;
   T( trace(T_PEER, "peer: interval timer"); )
-  rand_seed(RAND_GLOBAL, RMD160_HASHSZ);
+  rand_seed(RAND_GLOBAL, HASHSZ);
   p_interval();
   tvv = *tv;
   tvv.tv_sec += T_INTERVAL;
@@ -86,7 +112,9 @@ void interval(struct timeval *tv, void *v)
 
 static void usage(FILE *fp)
 {
-  pquis(fp, "Usage: $ [-options]\n");
+  pquis(fp, "Usage: $ [-D] [-d dir] [-b addr] [-p port]\n\
+       [-U user] [-G group] [-a socket] [-T trace-opts]\n\
+       [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n");
 }
 
 static void version(FILE *fp)
@@ -108,12 +136,17 @@ Options:\n\
 \n\
 -D, --daemon           Run in the background.\n\
 -d, --directory=DIR    Switch to directory DIR (default $TRIPEDIR).\n\
+-b, --bind-address=ADDR        Bind UDP socket to this IP ADDR.\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\
 -a, --admin-socket=FILE        Use FILE as the adminstration socket.\n\
+" T( "\
 -T, --trace=OPTIONS    Turn on tracing options.\n\
+" ) "\
 ", fp);
 }
 
@@ -125,6 +158,7 @@ int main(int argc, char *argv[])
   const char *dir = "/var/lib/tripe";
   const char *p;
   unsigned port = 0;
+  struct in_addr baddr = { INADDR_ANY };
   unsigned f = 0;
   uid_t u = -1;
   gid_t g = -1;
@@ -133,7 +167,7 @@ int main(int argc, char *argv[])
 #define f_daemon 2u
 
   ego(argv[0]);
-  trace_on(stderr, 0);
+  T( trace_on(stderr, 0); )
 
   if ((p = getenv("TRIPEDIR")) != 0)
     dir = p;
@@ -149,6 +183,7 @@ int main(int argc, char *argv[])
       { "setuid",      OPTF_ARGREQ,    0,      'U' },
       { "gid",         OPTF_ARGREQ,    0,      'G' },
       { "setgid",      OPTF_ARGREQ,    0,      'G' },
+      { "bind-address",        OPTF_ARGREQ,    0,      'b' },
       { "port",                OPTF_ARGREQ,    0,      'p' },
       { "directory",   OPTF_ARGREQ,    0,      'd' },
       { "priv-keyring",        OPTF_ARGREQ,    0,      'k' },
@@ -162,7 +197,7 @@ int main(int argc, char *argv[])
       { 0,             0,              0,      0 }
     };
 
-    int i = mdwopt(argc, argv, "hvu DU:G: p:d:k:K:t:a:" T("T:"),
+    int i = mdwopt(argc, argv, "hvu DU:G: b:p:d:k:K:t:a:" T("T:"),
                   opts, 0, 0, 0);
     if (i < 0)
       break;
@@ -181,30 +216,38 @@ 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 'b': {
+       struct hostent *h = gethostbyname(optarg);
+       if (!h)
+         die(EXIT_FAILURE, "unknown host name `%s'", optarg);
+       memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
       } break;
-         
       case 'p': {
        char *p;
        unsigned long i = strtoul(optarg, &p, 0);
@@ -261,16 +304,16 @@ int main(int argc, char *argv[])
   rand_seed(RAND_GLOBAL, RMD160_HASHSZ);
   signal(SIGPIPE, SIG_IGN);
   tun_init();
-  p_init(port);
+  p_init(baddr, 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));
@@ -279,7 +322,7 @@ int main(int argc, char *argv[])
   km_init(kr_priv, kr_pub, tag_priv);
   a_init(csock);
   if (f & f_daemon) {
-    if (u_daemon)
+    if (u_daemon())
       die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
     a_daemon();
   }
@@ -298,7 +341,6 @@ int main(int argc, char *argv[])
         selerr = 0;
       else if (errno != EINTR && errno != EAGAIN) {
        a_warn("select failed: %s", strerror(errno));
-       abort();
        selerr++;
        if (selerr > 8) {
          a_warn("too many select errors: bailing out");