chiark / gitweb /
Great reorganization.
[tripe] / client.c
diff --git a/client.c b/client.c
deleted file mode 100644 (file)
index 4aece8d..0000000
--- a/client.c
+++ /dev/null
@@ -1,578 +0,0 @@
-/* -*-c-*-
- *
- * $Id$
- *
- * Client for TrIPE
- *
- * (c) 2001 Straylight/Edgeware
- */
-
-/*----- Licensing notice --------------------------------------------------* 
- *
- * This file is part of Trivial IP Encryption (TrIPE).
- *
- * TrIPE is free software; you can redistribute it and/or modify
- * 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.
- */
-
-/*----- Header files ------------------------------------------------------*/
-
-#include "config.h"
-
-#include <ctype.h>
-#include <errno.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include <sys/types.h>
-#include <sys/time.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/wait.h>
-#include <syslog.h>
-
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-
-#include <mLib/alloc.h>
-#include <mLib/darray.h>
-#include <mLib/dstr.h>
-#include <mLib/mdwopt.h>
-#include <mLib/quis.h>
-#include <mLib/report.h>
-#include <mLib/sel.h>
-#include <mLib/selbuf.h>
-#include <mLib/sig.h>
-#include <mLib/str.h>
-
-#include "util.h"
-
-#undef sun
-
-/*----- Data structures ---------------------------------------------------*/
-
-#ifndef STRING_V
-#  define STRING_V
-   DA_DECL(string_v, char *);
-#endif
-
-/*----- Static variables --------------------------------------------------*/
-
-static const char *pidfile = 0;
-static const char *logname = 0;
-static FILE *logfp = 0;
-static unsigned f = 0;
-static int fd;
-static const char *bgtag = 0;
-
-#define f_bogus 1u
-#define f_spawn 2u
-#define f_daemon 4u
-#define f_spawnopts 8u
-#define f_syslog 16u
-#define f_command 32u
-#define f_noinput 64u
-#define f_warn 128u
-#define f_uclose 256u
-
-/*----- Main code ---------------------------------------------------------*/
-
-static void reap(int sig)
-{
-  int e = errno;
-  while (waitpid(-1, 0, WNOHANG) > 0)
-    ;
-  errno = e;
-}
-
-static void writelog(const char *cat, const char *msg)
-{
-  char buf[256];
-  time_t t = time(0);
-  struct tm *tm = localtime(&t);
-  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
-  fprintf(logfp, "%s %s: %s\n", buf, cat, msg);
-}
-
-static void checkbg(char **p)
-{
-  char *q = str_getword(p);
-  if (!q)
-    die(EXIT_FAILURE, "missing background tag");
-  if (!bgtag || strcmp(bgtag, q) != 0)
-    die(EXIT_FAILURE, "unexpected background tag `%s'", q);
-}
-
-static void checkfg(void)
-{
-  if (bgtag)
-    die(EXIT_FAILURE, "unexpected foreground response");
-}
-
-static void cline(char *p, size_t len, void *b)
-{
-  char *q;
-  if (!p) {
-    if (f & f_command)
-      die(EXIT_FAILURE, "server dropped the connection");
-    exit(0);
-  }
-  q = str_getword(&p);
-  if (!q)
-    return;
-  if (strcmp(q, "WARN") == 0) {
-    if (f & f_syslog)
-      syslog(LOG_WARNING, "%s", p);
-    if (logfp)
-      writelog("warning", p);
-    if (f & f_warn)
-      fprintf(stderr, "Warning: %s\n", p);
-  } else if (strcmp(q, "TRACE") == 0) {
-    if (f & f_syslog)
-      syslog(LOG_DEBUG, "%s", p);
-    if (logfp)
-      writelog("debug", p);
-  } else if (!(f & f_command)) {
-    if (f & f_syslog)
-      syslog(LOG_ERR, "unexpected output `%s %s'", q, p);
-    if (logfp) {
-      dstr d = DSTR_INIT;
-      dstr_putf(&d, "unexpected output `%s %s'", q, p);
-      writelog("error", d.buf);
-      dstr_destroy(&d);
-    }
-  } else if (strcmp(q, "FAIL") == 0) {
-    checkfg();
-    die(EXIT_FAILURE, "%s", p);
-  } else if (strcmp(q, "INFO") == 0) {
-    checkfg();
-    puts(p);
-  } else if (strcmp(q, "OK") == 0) {
-    checkfg();
-    exit(0);
-  } else if (strcmp(q, "BGDETACH") == 0) {
-    if (bgtag)
-      die(EXIT_FAILURE, "repeat detach");
-    bgtag = xstrdup(p);
-  } else if (strcmp(q, "BGOK") == 0) {
-    checkbg(&p);
-    exit(0);
-  } else if (strcmp(q, "BGINFO") == 0) {
-    checkbg(&p);
-    puts(p);
-  } else if (strcmp(q, "BGFAIL") == 0) {
-    checkbg(&p);
-    die(EXIT_FAILURE, "%s", p);
-  } else
-    die(EXIT_FAILURE, "unexpected output `%s %s'", q, p); 
-}
-
-static void sline(char *p, size_t len, void *b)
-{
-  if (!p) {
-    if (!(f & f_uclose))
-      moan("server closed the connection");
-    exit(0);
-  }
-  puts(p);
-}
-
-static void uline(char *p, size_t len, void *b)
-{
-  if (!p) {
-    selbuf_destroy(b);
-    shutdown(fd, 1);
-    f |= f_uclose;
-  } else {
-    p[len] = '\n';
-    errno = EIO;
-    if (write(fd, p, len + 1) != len + 1)
-      moan("write failed: %s", strerror(errno));
-  }
-}
-
-static void setup(const char *cmd)
-{
-  dstr d = DSTR_INIT;
-  char ch;
-  char *p, *q;
-  int n;
-
-  dstr_puts(&d, cmd);
-  dstr_putc(&d, '\n');
-  errno = EIO; /* Relax: be vague */
-  if (write(fd, d.buf, d.len) != d.len) {
-    die(EXIT_FAILURE, "error sending setup command `%s': %s",
-       cmd, strerror(errno));
-  }
-  dstr_reset(&d);
-  for (;;) {
-    n = read(fd, &ch, 1);
-    if (!n)
-      die(EXIT_FAILURE, "unexpected EOF during setup");
-    if (n < 0) {
-      die(EXIT_FAILURE, "error receiving reply to `%s': %s",
-         cmd, strerror(errno));
-    }
-    if (d.len < 256)
-      dstr_putc(&d, ch);
-    if (ch == '\n') {
-      p = d.buf;
-      q = str_getword(&p);
-      if (!q)
-       ;
-      else if (strcmp(q, "OK") == 0)
-       return;
-      else if (strcmp(q, "FAIL") == 0)
-       die(EXIT_FAILURE, "setup command `%s' failed: %s", cmd, p);
-      dstr_reset(&d);
-    }
-  }
-}
-
-static void logfile(const char *name)
-{
-  FILE *fp;
-
-  if ((fp = fopen(name, "a")) != 0) {
-    if (logfp)
-      fclose(logfp);
-    logfp =  fp;
-    setvbuf(logfp, 0, _IOLBF, BUFSIZ);
-  } else {
-    dstr d = DSTR_INIT;
-    dstr_putf(&d, "error opening logfile `%s': %s", name, strerror(errno));
-    if (logfp)
-      writelog("error", d.buf);
-    else if (logname)
-      die(EXIT_FAILURE, d.buf);
-    if (f & f_syslog)
-      syslog(LOG_ERR, d.buf);
-    dstr_destroy(&d);
-  }
-}
-
-static void sighup(int sig, void *v)
-{
-  logfile(logname);
-}
-
-static void cleanup(void)
-{
-  if (pidfile)
-    unlink(pidfile);
-}
-
-static void sigdie(int sig)
-{
-  cleanup();
-  signal(sig, SIG_DFL);
-  raise(sig);
-}
-
-static void version(FILE *fp)
-{
-  pquis(fp, "$, TrIPE version " VERSION "\n");
-}
-
-static void usage(FILE *fp)
-{
-  pquis(fp, "\
-Usage:\n\
-       $ [-w] [-OPTIONS] [COMMAND [ARGS]...]\n\
-       $ [-Dl] [-f FILE] [-OPTIONS]\n\
-Options:\n\
-       [-s] [-d DIRECTORY] [-a SOCKET] [-P PIDFILE]\n\
-       [-p PROGRAM] [-S ARG,ARG,...]\n\
-");
-}
-
-static void help(FILE *fp)
-{
-  version(fp);
-  fputc('\n', fp);
-  usage(fp);
-  fputs("\
-\n\
-Options in full:\n\
-\n\
--h, --help             Show this help text.\n\
--v, --version          Show version number.\n\
--u, --usage            Show brief usage message.\n\
-\n\
--D, --daemon           Become a background task after connecting.\n\
--d, --directory=DIR    Select current directory [default " CONFIGDIR "].\n\
--a, --admin-socket=FILE        Select socket to connect to\n\
-                          [default " SOCKETDIR "/tripesock].\n\
--P, --pidfile=FILE     Write process-id to FILE.\n\
-\n\
--s, --spawn            Start server rather than connecting.\n\
--p, --spawn-path=PATH  Specify path to executable.\n\
--S, --spawn-args=ARGS  Specify comma-separated arguments.\n\
-\n\
--l, --syslog           Log messages to system log.\n\
--f, --logfile=FILE     Log messages to FILE.\n\
--w, --warnings         Show warnings when running commands.\n\
-", fp);
-}
-
-int main(int argc, char *argv[])
-{
-  const char *dir = CONFIGDIR;
-  const char *sock = SOCKETDIR "/tripesock";
-  const char *spawnpath = "tripe";
-  string_v spawnopts = DA_INIT;
-  char *p;
-  FILE *pidfp = 0;
-
-  ego(argv[0]);
-
-  if ((p = getenv("TRIPEDIR")) != 0)
-    dir = p;
-
-  /* --- Parse the arguments --- */
-
-  for (;;) {
-    static const struct option opts[] = {
-      { "help",                0,              0,      'h' },
-      { "version",     0,              0,      'v' },
-      { "usage",       0,              0,      'u' },
-      { "daemon",      0,              0,      'D' },
-      { "directory",   OPTF_ARGREQ,    0,      'd' },
-      { "admin-socket",        OPTF_ARGREQ,    0,      'a' },
-      { "spawn",       0,              0,      's' },
-      { "spawn-path",  OPTF_ARGREQ,    0,      'p' },
-      { "spawn-args",  OPTF_ARGREQ,    0,      'S' },
-      { "syslog",      0,              0,      'l' },
-      { "logfile",     OPTF_ARGREQ,    0,      'f' },
-      { "warnings",    0,              0,      'w' },
-      { "pidfile",     OPTF_ARGREQ,    0,      'P' },
-      { 0,             0,              0,      0 }
-    };
-
-    int i = mdwopt(argc, argv, "+hvuDd:a:sp:S:lwf:nP:", opts, 0, 0, 0);
-    if (i < 0)
-      break;
-    switch (i) {
-      case 'h':
-       help(stdout);
-       exit(0);
-      case 'v':
-       version(stdout);
-       exit(0);
-      case 'u':
-       usage(stdout);
-       exit(0);
-      case 'D':
-       f |= f_daemon | f_noinput;
-       break;
-      case 'd':
-       dir = optarg;
-       break;
-      case 'a':
-       sock = optarg;
-       break;
-      case 's':
-       f |= f_spawn;
-       break;
-      case 'p':
-       f |= f_spawn;
-       spawnpath = optarg;
-       break;
-      case 'S':
-       f |= f_spawn | f_spawnopts;
-       for (p = strtok(optarg, ","); p; p = strtok(0, ","))
-         DA_PUSH(&spawnopts, p);
-       break;
-      case 'l':
-       f |= f_syslog | f_noinput;
-       break;
-      case 'w':
-       f |= f_warn;
-       break;
-      case 'f':
-       logname = optarg;
-       f |= f_noinput;
-       break;
-      case 'P':
-       pidfile = optarg;
-       break;
-      default:
-       f |= f_bogus;
-       break;
-    }
-  }
-  if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) {
-    usage(stderr);
-    exit(EXIT_FAILURE);
-  }
-
-  /* --- Set various things up --- */
-
-  if (chdir(dir)) {
-    die(EXIT_FAILURE, "couldn't set `%s' as current directory: %s",
-       dir, strerror(errno));
-  }
-  if (logname)
-    logfile(logname);
-  if (!pidfile && (f & f_daemon) && ((f & f_syslog) || logname))
-    pidfile = "tripectl.pid";
-  if (pidfile && (pidfp = fopen(pidfile, "w")) == 0) {
-    die(EXIT_FAILURE, "couldn't open `%s' for writing: %s",
-       pidfile, strerror(errno));
-  }
-  signal(SIGINT, sigdie);
-  signal(SIGQUIT, sigdie);
-  signal(SIGTERM, sigdie);
-  atexit(cleanup);
-
-  /* --- Connect to the server --- */
-
-  if (f & f_spawn) {
-    int pfd[2];
-    pid_t kid;
-    struct sigaction sa;
-    sigset_t newmask, oldmask;
-
-    sa.sa_handler = reap;
-    sigemptyset(&sa.sa_mask);
-    sa.sa_flags = SA_NOCLDSTOP;
-#ifdef SA_RESTART
-    sa.sa_flags |= SA_RESTART;
-#endif
-    sigaction(SIGCHLD, &sa, 0);
-
-    DA_UNSHIFT(&spawnopts, (char *)sock);
-    DA_UNSHIFT(&spawnopts, "-a");
-    DA_UNSHIFT(&spawnopts, "-d.");
-    DA_UNSHIFT(&spawnopts, (char *)spawnpath);
-    DA_PUSH(&spawnopts, 0);
-    if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd))
-      die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno));
-    sigemptyset(&newmask);
-    sigaddset(&newmask, SIGCHLD);
-    sigprocmask(SIG_BLOCK, &newmask, &oldmask);
-    if ((kid = fork()) < 0)
-      die(EXIT_FAILURE, "fork failed: %s", strerror(errno));
-    if (!kid) {
-      dup2(pfd[1], STDIN_FILENO);
-      dup2(pfd[1], STDOUT_FILENO);
-      close(pfd[1]);
-      close(pfd[0]);
-      if (logfp)
-       fclose(logfp);
-      if (pidfp)
-       fclose(pidfp);
-      closelog();
-      if (f & f_daemon)
-       u_detach();
-      execvp(DA(&spawnopts)[0], DA(&spawnopts));
-      die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno));
-    }
-    sigprocmask(SIG_SETMASK, &oldmask, 0);
-    fd = pfd[0];
-    close(pfd[1]);
-  } else {
-    struct sockaddr_un sun;
-    size_t sz = strlen(sock) + 1;
-    if (sz > sizeof(sun.sun_path))
-      die(EXIT_FAILURE, "socket name `%s' too long", sock);
-    memset(&sun, 0, sizeof(sun));
-    sun.sun_family = AF_UNIX;
-    memcpy(sun.sun_path, sock, sz);
-    sz = sz + offsetof(struct sockaddr_un, sun_path);
-    if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
-      die(EXIT_FAILURE, "error making socket: %s", strerror(errno));
-    if (connect(fd, (struct sockaddr *)&sun, sz)) {
-      die(EXIT_FAILURE, "error connecting to `%s': %s",
-         sun.sun_path, strerror(errno));
-    }
-  }
-
-  if (f & f_daemon) {
-    if (u_daemon())
-      die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
-  }
-  if (pidfp) {
-    fprintf(pidfp, "%li\n", (long)getpid());
-    fclose(pidfp);
-  }
-  signal(SIGPIPE, SIG_IGN);
-
-  /* --- If we're meant to be interactive, do that --- */
-
-  if (optind == argc)
-    setup("WATCH -A+tw");
-  if (!(f & f_noinput) && optind == argc) {
-    sel_state sel;
-    selbuf bu, bs;
-
-    sel_init(&sel);
-    selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu);
-    selbuf_init(&bs, &sel, fd, sline, &bs);
-    for (;;) {
-      if (sel_select(&sel) && errno != EINTR && errno != EAGAIN)
-       die(EXIT_FAILURE, "select failed: %s", strerror(errno));
-    }
-  }
-
-  /* --- If there's a command, submit it --- */
-
-  if (optind < argc) {
-    dstr d = DSTR_INIT;
-    setup((f & f_warn) ? "WATCH -A+w" : "WATCH -A");
-    dstr_puts(&d, argv[optind++]);
-    while (optind < argc) {
-      dstr_putc(&d, ' ');
-      dstr_puts(&d, argv[optind++]);
-    }
-    dstr_putc(&d, '\n');
-    errno = EIO;
-    if (write(fd, d.buf, d.len) != d.len || shutdown(fd, 1))
-      die(EXIT_FAILURE, "write failed: %s", strerror(errno));
-    dstr_destroy(&d);
-    f |= f_command;
-  }
-
-  /* --- Pull everything else out of the box --- */
-
-  {
-    sel_state sel;
-    selbuf b;
-    sig hup;
-
-    sel_init(&sel);
-    selbuf_init(&b, &sel, fd, cline, 0);
-
-    if (f & f_syslog)
-      openlog(QUIS, 0, LOG_DAEMON);
-    if (logfp) {
-      sig_init(&sel);
-      sig_add(&hup, SIGHUP, sighup, 0);
-    }
-    for (;;) {
-      if (sel_select(&sel) && errno != EINTR && errno != EAGAIN)
-       die(EXIT_FAILURE, "select failed: %s", strerror(errno));
-    }
-  }
-
-  return (0);
-}
-
-/*----- That's all, folks -------------------------------------------------*/