chiark / gitweb /
server: Option `-F' to run the server in the `foreground'.
[tripe] / server / admin.c
index 797b47869589b702813b2e48f71ba9c074064a3c..382baca97b7ea6e85c2705a477b72fbd127845ad 100644 (file)
@@ -1,13 +1,11 @@
 /* -*-c-*-
- *
- * $Id$
  *
  * Admin interface for configuration
  *
  * (c) 2001 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Trivial IP Encryption (TrIPE).
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * TrIPE is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with TrIPE; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -72,6 +70,7 @@ static sig s_term, s_int, s_hup;
 
 #define F_DAEMON 1u
 #define F_INIT 2u
+#define F_FOREGROUND 4u
 
 #define T_RESOLVE SEC(30)
 #define T_PING SEC(5)
@@ -231,34 +230,6 @@ static void a_flush(int fd, unsigned mode, void *v)
 
 /*----- Utility functions -------------------------------------------------*/
 
-/* --- @quotify@ --- *
- *
- * Arguments:  @dstr *d@ = where to write the answer
- *             @const char *p@ = string to quotify
- *
- * Returns:    ---
- *
- * Use:                Quotes the given string if necessary, according to our
- *             quoting rules.
- */
-
-static void quotify(dstr *d, const char *p)
-{
-  if (d->len)
-    dstr_putc(d, ' ');
-  if (*p && !p[strcspn(p, "\"' \t\n\v")])
-    dstr_puts(d, p);
-  else {
-    dstr_putc(d, '\"');
-    while (*p) {
-      if (*p == '\\' || *p == '\"')
-       dstr_putc(d, '\\');
-      dstr_putc(d, *p++);
-    }
-    dstr_putc(d, '\"');
-  }
-}
-
 /* --- @a_vformat@ --- *
  *
  * Arguments:  @dstr *d@ = where to leave the formatted message
@@ -283,8 +254,8 @@ static void a_vformat(dstr *d, const char *fmt, va_list ap)
        const addr *a = va_arg(ap, const addr *);
        switch (a->sa.sa_family) {
          case AF_INET:
-           quotify(d, "INET");
-           quotify(d, inet_ntoa(a->sin.sin_addr));
+           u_quotify(d, "INET");
+           u_quotify(d, inet_ntoa(a->sin.sin_addr));
            dstr_putf(d, " %u", (unsigned)ntohs(a->sin.sin_port));
            break;
          default:
@@ -303,19 +274,19 @@ static void a_vformat(dstr *d, const char *fmt, va_list ap)
        while (d->len && d->buf[d->len - 1] == '=') d->len--;
       } else if (strcmp(fmt, "?TOKENS") == 0) {
        const char *const *av = va_arg(ap, const char *const *);
-       while (*av) quotify(d, *av++);
+       while (*av) u_quotify(d, *av++);
       } else if (strcmp(fmt, "?PEER") == 0)
-       quotify(d, p_name(va_arg(ap, peer *)));
+       u_quotify(d, p_name(va_arg(ap, peer *)));
       else if (strcmp(fmt, "?ERRNO") == 0) {
        dstr_putf(d, " E%d", errno);
-       quotify(d, strerror(errno));
+       u_quotify(d, strerror(errno));
       } else
        abort();
     } else {
       if (*fmt == '!') fmt++;
       DRESET(&dd);
       dstr_vputf(&dd, fmt, &ap);
-      quotify(d, dd.buf);
+      u_quotify(d, dd.buf);
     }
     fmt = va_arg(ap, const char *);
   }
@@ -344,7 +315,7 @@ static void a_vwrite(admin *a, const char *status, const char *tag,
 
   if (tag) dstr_puts(&d, "BG");
   dstr_puts(&d, status);
-  if (tag) quotify(&d, tag);
+  if (tag) u_quotify(&d, tag);
   a_vformat(&d, fmt, ap);
   dstr_putc(&d, '\n');
   dosend(a, d.buf, d.len);
@@ -413,7 +384,7 @@ static void a_rawalert(unsigned f_and, unsigned f_eq, const char *status,
 {
   admin *a, *aa;
   dstr d = DSTR_INIT;
-  
+
   if (!(flags & F_INIT))
     return;
   dstr_puts(&d, status);
@@ -529,12 +500,9 @@ void a_notify(const char *fmt, ...)
 
 void a_quit(void)
 {
-  peer *p;
-
   close(sock.fd);
   unlink(sockname);
-  while ((p = p_first()) != 0)
-    p_destroy(p);
+  FOREACH_PEER(p, { p_destroy(p); });
   exit(0);
 }
 
@@ -595,10 +563,10 @@ static long a_parsetime(const char *p)
     case 'h': t *= 60;
     case 'm': t *= 60;
     case 's': if (q[1] != 0)
-    default:    t = -1;
+    default:   t = -1;
     case 0:   break;
   }
-  return (t);    
+  return (t);
 }
 
 /* --- @a_findpeer@ --- *
@@ -1014,7 +982,7 @@ static void a_resolved(struct hostent *h, void *v)
   sel_rmtimer(&r->t);
   xfree(r->addr);
   a_bgrelease(&r->bg);
-} 
+}
 
 /* --- @a_restimer@ --- *
  *
@@ -1087,21 +1055,25 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag,
   r->addr = 0;
   r->func = func;
   if (mystrieq(av[i], "inet")) i++;
-  if (ac - i != 2) {
-    a_fail(a, "bad-addr-syntax", "[inet] ADDRESS PORT", A_END);
+  if (ac - i != 1 && ac - i != 2) {
+    a_fail(a, "bad-addr-syntax", "[inet] ADDRESS [PORT]", A_END);
     goto fail;
   }
   r->sa.sin.sin_family = AF_INET;
   r->sasz = sizeof(r->sa.sin);
   r->addr = xstrdup(av[i]);
-  pt = strtoul(av[i + 1], &p, 0);
-  if (*p) {
-    struct servent *s = getservbyname(av[i + 1], "udp");
-    if (!s) {
-      a_fail(a, "unknown-service", "%s", av[i + 1], A_END);
-      goto fail;
+  if (!av[i + i])
+    pt = TRIPE_PORT;
+  else {
+    pt = strtoul(av[i + 1], &p, 0);
+    if (*p) {
+      struct servent *s = getservbyname(av[i + 1], "udp");
+      if (!s) {
+       a_fail(a, "unknown-service", "%s", av[i + 1], A_END);
+       goto fail;
+      }
+      pt = ntohs(s->s_port);
     }
-    pt = ntohs(s->s_port);
   }
   if (pt == 0 || pt >= 65536) {
     a_fail(a, "invalid-port", "%lu", pt, A_END);
@@ -1121,7 +1093,7 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag,
           a->seq, BGTAG(r), r->addr); )
 
   /* --- If the name is numeric, do it the easy way --- */
-  
+
   if (inet_aton(av[i], &r->sa.sin.sin_addr)) {
     T( trace(T_ADMIN, "admin: resop %s done the easy way", BGTAG(r)); )
     func(r, ARES_OK);
@@ -1203,7 +1175,9 @@ static void a_doadd(admin_resop *r, int rc)
   if (rc == ARES_OK) {
     add->peer.sasz = add->r.sasz;
     add->peer.sa = add->r.sa;
-    if (p_find(add->peer.name))
+    if (p_findbyaddr(&add->r.sa))
+      a_bgfail(&add->r.bg, "peer-addr-exists", "?ADDR", &add->r.sa, A_END);
+    else if (p_find(add->peer.name))
       a_bgfail(&add->r.bg, "peer-exists", "%s", add->peer.name, A_END);
     else if (!p_create(&add->peer))
       a_bgfail(&add->r.bg, "peer-create-fail", "%s", add->peer.name, A_END);
@@ -1236,6 +1210,7 @@ static void acmd_add(admin *a, unsigned ac, char *av[])
   add->peer.name = 0;
   add->peer.t_ka = 0;
   add->peer.tops = tun_default;
+  add->peer.kxf = 0;
 
   /* --- Parse options --- */
 
@@ -1255,11 +1230,12 @@ static void acmd_add(admin *a, unsigned ac, char *av[])
       }
     })
     OPTTIME("-keepalive", t, { add->peer.t_ka = t; })
+    OPT("-cork", { add->peer.kxf |= KXF_CORK; })
   });
 
   /* --- Make sure someone's not got there already --- */
 
-  if (!*av)
+  if (!av[0] || !av[1])
     goto bad_syntax;
   if (p_find(*av)) {
     a_fail(a, "peer-exists", "%s", *av, A_END);
@@ -1377,7 +1353,7 @@ static void a_ping(admin *a, unsigned ac, char *av[],
     a_bgrelease(&pg->bg);
   }
   return;
-    
+
 bad_syntax:
   a_fail(a, "bad-syntax", "%s", cmd, "[OPTIONS] PEER", cmd, A_END);
 fail:
@@ -1593,7 +1569,7 @@ static int traceish(admin *a, unsigned ac, char *av[],
          }
          a_fail(a, "bad-%s-option", what, "%c", *p, A_END);
          return (0);
-        tropt_ok:;
+       tropt_ok:;
          break;
       }
       p++;
@@ -1637,7 +1613,7 @@ static void acmd_daemon(admin *a, unsigned ac, char *av[])
     a_notify("DAEMON", A_END);
     if (a_stdin)
       a_destroy(a_stdin);
-    if (u_daemon())
+    if (daemonize())
       a_fail(a, "daemon-error", "?ERRNO", A_END);
     else {
       flags |= F_DAEMON;
@@ -1672,10 +1648,7 @@ static void acmd_bgcancel(admin *a, unsigned ac, char *av[])
 
 static void acmd_list(admin *a, unsigned ac, char *av[])
 {
-  peer *p;
-
-  for (p = p_first(); p; p = p_next(p))
-    a_info(a, "%s", p_name(p), A_END);
+  FOREACH_PEER(p, {  a_info(a, "%s", p_name(p), A_END); });
   a_ok(a);
 }
 
@@ -1697,7 +1670,7 @@ static void acmd_setifname(admin *a, unsigned ac, char *av[])
     a_notify("NEWIFNAME", "?PEER", p, "%s", p_ifname(p), "%s", av[1], A_END);
     p_setifname(p, av[1]);
     a_ok(a);
-  }  
+  }
 }
 
 static void acmd_getchal(admin *a, unsigned ac, char *av[])
@@ -1807,7 +1780,7 @@ static void acmd_stats(admin *a, unsigned ac, char *av[])
         "ip-bytes-in=%lu", st->sz_ipin,
         A_END);
   a_info(a,
-        "ip-packets-out=%lu", st->n_ipout, 
+        "ip-packets-out=%lu", st->n_ipout,
         "ip-bytes-out=%lu", st->sz_ipout,
         A_END);
   a_info(a, "rejected-packets=%lu", st->n_reject, A_END);
@@ -1910,7 +1883,7 @@ static const acmd acmdtab[] = {
   { "tunnels", 0,                      0,      0,      acmd_tunnels },
   { "version", 0,                      0,      0,      acmd_version },
   { "warn",    "MESSAGE ...",          1,      0xffff, acmd_warn },
-  { "watch",   "[OPTIONS]",            0,      1,      acmd_watch },
+  { "watch",   "[OPTIONS]",            0,      1,      acmd_watch },
   { 0,         0,                      0,      0,      0 }
 };
 
@@ -1921,7 +1894,7 @@ static void acmd_help(admin *a, unsigned ac, char *av[])
   for (c = acmdtab; c->name; c++) {
     if (c->help)
       a_info(a, "%s", c->name, "*%s", c->help, A_END);
-    else 
+    else
       a_info(a, "%s", c->name, A_END);
   }
   a_ok(a);
@@ -1955,6 +1928,14 @@ static void a_destroypending(void)
     T( trace(T_ADMIN, "admin: completing destruction of connection %u",
             a->seq); )
 
+    /* --- If this is the foreground client then shut down --- */
+
+    if (a->f & AF_FOREGROUND) {
+      T( trace(T_ADMIN, "admin: foreground client quit: shutting down"); )
+      a_warn("SERVER", "quit", "foreground-eof", A_END);
+      a_quit();
+    }
+
     /* --- Abort any background jobs in progress --- */
 
     for (bg = a->bg; bg; bg = bbg) {
@@ -2068,7 +2049,7 @@ static void a_line(char *p, size_t len, void *vp)
       if (c->argmin > ac || ac > c->argmax) {
        if (!c->help)
          a_fail(a, "bad-syntax", "%s", c->name, "", A_END);
-       else 
+       else
          a_fail(a, "bad-syntax", "%s", c->name, "%s", c->help, A_END);
       } else
        c->func(a, ac, av + 1);