X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/blobdiff_plain/3cbd2d89482ab48adea16aef781b191b793d9a26..3eb084b221832483d34f0b701730537e76b6cecd:/client/tripectl.c?ds=sidebyside
diff --git a/client/tripectl.c b/client/tripectl.c
index 661e5199..1b02123b 100644
--- a/client/tripectl.c
+++ b/client/tripectl.c
@@ -9,19 +9,18 @@
*
* 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 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 3 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.
+ * 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.
+ * along with TrIPE. If not, see .
*/
/*----- Header files ------------------------------------------------------*/
@@ -52,6 +51,8 @@
#include
#include
#include
+#include
+#include
#include
#include
#include
@@ -74,10 +75,12 @@
/*----- Static variables --------------------------------------------------*/
+static sel_state sel;
static const char *pidfile = 0;
static const char *logname = 0;
static FILE *logfp = 0;
static unsigned f = 0;
+static const char *watch = 0;
static int fd;
static const char *bgtag = 0;
@@ -90,6 +93,8 @@ static const char *bgtag = 0;
#define f_noinput 64u
#define f_warn 128u
#define f_uclose 256u
+#define f_losing 512u
+#define f_nostamp 1024u
/*----- Main code ---------------------------------------------------------*/
@@ -106,8 +111,9 @@ 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);
+ if (f & f_nostamp) buf[0] = 0;
+ else 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)
@@ -119,6 +125,30 @@ static void checkbg(char **p)
die(EXIT_FAILURE, "unexpected background tag `%s'", q);
}
+static void PRINTF_LIKE(2, 3) dolog(int prio, const char *msg, ...)
+{
+ va_list ap;
+ dstr d = DSTR_INIT;
+ const char *cat;
+
+ va_start(ap, msg);
+ dstr_vputf(&d, msg, &ap);
+ va_end(ap);
+ if (f & f_syslog) syslog(prio, "%s", d.buf);
+ if (logfp) {
+ switch (prio) {
+ case LOG_WARNING: cat = "warning"; break;
+ case LOG_DEBUG: cat = "debug"; break;
+ case LOG_ERR: cat = "error"; break;
+ default: cat = "message"; break;
+ }
+ writelog(cat, d.buf);
+ }
+ if (prio == LOG_WARNING && (f & f_warn))
+ fprintf(stderr, "Warning: %s\n", d.buf);
+ dstr_destroy(&d);
+}
+
static void checkfg(void)
{ if (bgtag) die(EXIT_FAILURE, "unexpected foreground response"); }
@@ -128,33 +158,19 @@ static void cline(char *p, size_t len, void *b)
if (!p) {
if (f & f_command)
die(EXIT_FAILURE, "server dropped the connection");
+ f &= ~f_losing;
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) {
+ if (strcmp(q, "WARN") == 0)
+ dolog(LOG_WARNING, "%s", p);
+ else if (strcmp(q, "TRACE") == 0)
+ dolog(LOG_DEBUG, "%s", p);
+ else if (!(f & f_command))
+ dolog(LOG_ERR, "unexpected output `%s %s'", q, p);
+ else if (strcmp(q, "FAIL") == 0) {
checkfg();
die(EXIT_FAILURE, "%s", p);
} else if (strcmp(q, "INFO") == 0) {
@@ -207,6 +223,16 @@ static void uline(char *p, size_t len, void *b)
}
}
+static void eline(char *p, size_t len, void *b)
+{
+ if (p)
+ dolog(LOG_WARNING, "(stderr): %s", p);
+ else {
+ selbuf_destroy(b);
+ close(fd);
+ }
+}
+
static void setup(const char *cmd)
{
dstr d = DSTR_INIT;
@@ -250,7 +276,11 @@ static void logfile(const char *name)
{
FILE *fp;
- if ((fp = fopen(name, "a")) != 0) {
+ if (strcmp(name, "-") == 0)
+ logfp = stdout;
+ else if (strcmp(name, "!") == 0)
+ logfp = stderr;
+ else if ((fp = fopen(name, "a")) != 0) {
if (logfp)
fclose(logfp);
logfp = fp;
@@ -261,7 +291,7 @@ static void logfile(const char *name)
if (logfp)
writelog("error", d.buf);
else if (logname)
- die(EXIT_FAILURE, d.buf);
+ die(EXIT_FAILURE, "%s", d.buf);
if (f & f_syslog)
syslog(LOG_ERR, "%s", d.buf);
dstr_destroy(&d);
@@ -275,6 +305,19 @@ static void cleanup(void) { if (pidfile) unlink(pidfile); }
static void sigdie(int sig)
{ cleanup(); signal(sig, SIG_DFL); raise(sig); }
+static void PRINTF_LIKE(2, 3) putarg(string_v *av, const char *fmt, ...)
+{
+ va_list ap;
+ dstr d = DSTR_INIT;
+
+ va_start(ap, fmt);
+ dstr_vputf(&d, fmt, &ap);
+ dstr_putz(&d);
+ va_end(ap);
+ DA_UNSHIFT(av, xstrdup(d.buf));
+ dstr_destroy(&d);
+}
+
static void version(FILE *fp)
{ pquis(fp, "$, TrIPE version " VERSION "\n"); }
@@ -305,6 +348,8 @@ Options in full:\n\
\n\
-D, --daemon Become a background task after connecting.\n\
-d, --directory=DIR Select current directory [default " CONFIGDIR "].\n\
+-U, --setuid=USER Set uid to USER after initialization.\n\
+-G, --setgid=GROUP Set gid to GROUP after initialization.\n\
-a, --admin-socket=FILE Select socket to connect to\n\
[default " SOCKETDIR "/tripesock].\n\
-P, --pidfile=FILE Write process-id to FILE.\n\
@@ -315,7 +360,9 @@ Options in full:\n\
\n\
-l, --syslog Log messages to system log.\n\
-f, --logfile=FILE Log messages to FILE.\n\
+-t, --no-timestamp When logging to a file, don't emit timestamps.\n\
-w, --warnings Show warnings when running commands.\n\
+-W, --watch=THINGS Watch for THINGS after connecting.\n\
", fp);
}
@@ -327,6 +374,19 @@ int main(int argc, char *argv[])
string_v spawnopts = DA_INIT;
char *p;
FILE *pidfp = 0;
+ int i;
+ size_t sz;
+ uid_t u = -1;
+ gid_t g = -1;
+ int pfd[2], efd[2];
+ mdup_fd md[3];
+ pid_t kid;
+ struct sigaction sa;
+ sigset_t newmask, oldmask;
+ struct sockaddr_un sun;
+ selbuf bu, bs, be;
+ dstr d = DSTR_INIT;
+ sig hup;
ego(argv[0]);
@@ -343,6 +403,10 @@ int main(int argc, char *argv[])
{ "version", 0, 0, 'v' },
{ "usage", 0, 0, 'u' },
{ "daemon", 0, 0, 'D' },
+ { "uid", OPTF_ARGREQ, 0, 'U' },
+ { "setuid", OPTF_ARGREQ, 0, 'U' },
+ { "gid", OPTF_ARGREQ, 0, 'G' },
+ { "setgid", OPTF_ARGREQ, 0, 'G' },
{ "directory", OPTF_ARGREQ, 0, 'd' },
{ "admin-socket", OPTF_ARGREQ, 0, 'a' },
{ "spawn", 0, 0, 's' },
@@ -350,12 +414,14 @@ int main(int argc, char *argv[])
{ "spawn-args", OPTF_ARGREQ, 0, 'S' },
{ "syslog", 0, 0, 'l' },
{ "logfile", OPTF_ARGREQ, 0, 'f' },
+ { "no-timestamp", 0, 0, 't' },
{ "warnings", 0, 0, 'w' },
+ { "watch", OPTF_ARGREQ, 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);
+ i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwW:f:nP:t", opts, 0, 0, 0);
if (i < 0)
break;
switch (i) {
@@ -371,6 +437,12 @@ int main(int argc, char *argv[])
case 'D':
f |= f_daemon | f_noinput;
break;
+ case 'U':
+ u = u_getuser(optarg, &g);
+ break;
+ case 'G':
+ g = u_getgroup(optarg);
+ break;
case 'd':
dir = optarg;
break;
@@ -395,10 +467,16 @@ int main(int argc, char *argv[])
case 'w':
f |= f_warn;
break;
+ case 'W':
+ watch = optarg;
+ break;
case 'f':
logname = optarg;
f |= f_noinput;
break;
+ case 't':
+ f |= f_nostamp;
+ break;
case 'P':
pidfile = optarg;
break;
@@ -418,14 +496,14 @@ int main(int argc, char *argv[])
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));
}
+ sel_init(&sel);
+ sig_init(&sel);
signal(SIGINT, sigdie);
signal(SIGQUIT, sigdie);
signal(SIGTERM, sigdie);
@@ -434,11 +512,6 @@ int main(int argc, char *argv[])
/* --- 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;
@@ -447,13 +520,14 @@ int main(int argc, char *argv[])
#endif
sigaction(SIGCHLD, &sa, 0);
- DA_UNSHIFT(&spawnopts, (char *)sock);
- DA_UNSHIFT(&spawnopts, "-a");
- DA_UNSHIFT(&spawnopts, "-d.");
- DA_UNSHIFT(&spawnopts, "-F");
- DA_UNSHIFT(&spawnopts, (char *)spawnpath);
DA_PUSH(&spawnopts, 0);
- if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd))
+ if (g != (gid_t)-1) putarg(&spawnopts, "-G%lu", (unsigned long)g);
+ if (u != (uid_t)-1) putarg(&spawnopts, "-U%lu", (unsigned long)u);
+ putarg(&spawnopts, "-a%s", sock);
+ putarg(&spawnopts, "-d.");
+ putarg(&spawnopts, "-F");
+ putarg(&spawnopts, "%s", spawnpath);
+ if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd) || pipe(efd))
die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno));
sigemptyset(&newmask);
sigaddset(&newmask, SIGCHLD);
@@ -461,11 +535,12 @@ int main(int argc, char *argv[])
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[0]);
- close(pfd[1]);
- if (logfp) fclose(logfp);
+ close(pfd[0]); close(efd[0]);
+ sigprocmask(SIG_SETMASK, &oldmask, 0);
+ md[0].cur = pfd[1]; md[0].want = STDIN_FILENO;
+ md[1].cur = pfd[1]; md[1].want = STDOUT_FILENO;
+ md[2].cur = efd[1]; md[2].want = STDERR_FILENO;
+ mdup(md, 3);
if (pidfp) fclose(pidfp);
closelog();
if (f & f_daemon) detachtty();
@@ -474,10 +549,10 @@ int main(int argc, char *argv[])
}
sigprocmask(SIG_SETMASK, &oldmask, 0);
fd = pfd[0];
- close(pfd[1]);
+ close(pfd[1]); close(efd[1]);
+ selbuf_init(&be, &sel, efd[0], eline, &be);
} else {
- struct sockaddr_un sun;
- size_t sz = strlen(sock) + 1;
+ sz = strlen(sock) + 1;
if (sz > sizeof(sun.sun_path))
die(EXIT_FAILURE, "socket name `%s' too long", sock);
memset(&sun, 0, sizeof(sun));
@@ -492,6 +567,10 @@ int main(int argc, char *argv[])
}
}
+ f |= f_losing; /* pessimism */
+ u_setugid(u, g);
+ if (logname)
+ logfile(logname);
if (f & f_daemon) {
if (daemonize())
die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
@@ -504,13 +583,13 @@ int main(int argc, char *argv[])
/* --- If we're meant to be interactive, do that --- */
- if (optind == argc)
- setup("WATCH -A+tw");
+ if (optind == argc) {
+ DRESET(&d);
+ dstr_puts(&d, "watch");
+ u_quotify(&d, watch ? watch : "-A+tw");
+ setup(d.buf);
+ }
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 (;;) {
@@ -522,8 +601,11 @@ int main(int argc, char *argv[])
/* --- If there's a command, submit it --- */
if (optind < argc) {
- dstr d = DSTR_INIT;
- setup((f & f_warn) ? "WATCH -A+w" : "WATCH -A");
+ DRESET(&d);
+ dstr_puts(&d, "watch");
+ u_quotify(&d, watch ? watch : (f & f_warn) ? "-A+w" : "-A");
+ setup(d.buf);
+ DRESET(&d);
while (optind < argc)
u_quotify(&d, argv[optind++]);
dstr_putc(&d, '\n');
@@ -536,24 +618,15 @@ int main(int argc, char *argv[])
/* --- Pull everything else out of the box --- */
- {
- sel_state sel;
- selbuf b;
- sig hup;
+ selbuf_init(&bs, &sel, fd, cline, 0);
- 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));
- }
+ if (f & f_syslog)
+ openlog(QUIS, 0, LOG_DAEMON);
+ if (logfp)
+ 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);