3 * $Id: client.c,v 1.12 2003/10/15 09:29:38 mdw Exp $
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.12 2003/10/15 09:29:38 mdw
33 * Cosmetic fix to changelog comment.
35 * Revision 1.11 2003/07/13 11:19:49 mdw
36 * Incompatible protocol fix! Include message type code under MAC tag to
37 * prevent cut-and-paste from key-exchange messages to general packet
40 * Revision 1.10 2003/05/17 10:59:19 mdw
41 * Put a newline in the pidfile.
43 * Revision 1.9 2002/01/13 14:57:18 mdw
44 * Track @lbuf@ changes in mLib.
46 * Revision 1.8 2001/06/19 22:09:37 mdw
47 * Move the program name to the right place when constructing the arguments
48 * to pass to a new server.
50 * Revision 1.7 2001/02/22 09:07:54 mdw
51 * Write a pidfile on request, and delete it when finished.
53 * Revision 1.6 2001/02/22 09:06:08 mdw
54 * Fix logfile rotation to avoid program collapse.
56 * Revision 1.5 2001/02/16 21:23:39 mdw
57 * Use reliable signal handling for reopening logs.
59 * Revision 1.4 2001/02/06 09:34:53 mdw
60 * Change ERR response to FAIL for consistency with other programs.
62 * Revision 1.3 2001/02/04 17:10:15 mdw
63 * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't
64 * change directory -- just mangle pathnames instead.
66 * Revision 1.2 2001/02/04 01:17:54 mdw
67 * Create a configuration header file to tidy up command lines.
69 * Revision 1.1 2001/02/03 20:26:37 mdw
74 /*----- Header files ------------------------------------------------------*/
86 #include <sys/types.h>
93 #include <sys/socket.h>
95 #include <arpa/inet.h>
98 #include <mLib/alloc.h>
99 #include <mLib/darray.h>
100 #include <mLib/dstr.h>
101 #include <mLib/mdwopt.h>
102 #include <mLib/quis.h>
103 #include <mLib/report.h>
104 #include <mLib/sel.h>
105 #include <mLib/selbuf.h>
106 #include <mLib/sig.h>
107 #include <mLib/str.h>
113 /*----- Data structures ---------------------------------------------------*/
117 DA_DECL(string_v, char *);
120 /*----- Static variables --------------------------------------------------*/
122 static const char *pidfile = 0;
123 static const char *logname = 0;
124 static FILE *logfp = 0;
125 static unsigned f = 0;
131 #define f_spawnopts 8u
133 #define f_command 32u
134 #define f_noinput 64u
136 #define f_uclose 256u
138 /*----- Main code ---------------------------------------------------------*/
140 static void reap(int sig)
143 while (waitpid(-1, 0, WNOHANG) > 0)
148 static void writelog(const char *cat, const char *msg)
152 struct tm *tm = localtime(&t);
153 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
154 fprintf(logfp, "%s %s: %s\n", buf, cat, msg);
157 static void cline(char *p, size_t len, void *b)
162 die(EXIT_FAILURE, "server dropped the connection");
168 if (strcmp(q, "WARN") == 0) {
170 syslog(LOG_WARNING, "%s", p);
172 writelog("warning", p);
174 fprintf(stderr, "Warning: %s\n", p);
175 } else if (strcmp(q, "TRACE") == 0) {
177 syslog(LOG_DEBUG, "%s", p);
179 writelog("debug", p);
180 } else if (!(f & f_command)) {
182 syslog(LOG_ERR, "unexpected output `%s %s'", q, p);
185 dstr_putf(&d, "unexpected output `%s %s'", q, p);
186 writelog("error", d.buf);
189 } else if (strcmp(q, "FAIL") == 0)
190 die(EXIT_FAILURE, "%s", p);
191 else if (strcmp(q, "INFO") == 0)
193 else if (strcmp(q, "OK") == 0)
196 die(EXIT_FAILURE, "unexpected output `%s %s'", q, p);
199 static void sline(char *p, size_t len, void *b)
203 moan("server closed the connection");
209 static void uline(char *p, size_t len, void *b)
217 write(fd, p, len + 1);
221 static void logfile(const char *name)
225 if ((fp = fopen(name, "a")) != 0) {
229 setvbuf(logfp, 0, _IOLBF, BUFSIZ);
232 dstr_putf(&d, "error opening logfile `%s': %s", name, strerror(errno));
234 writelog("error", d.buf);
236 die(EXIT_FAILURE, d.buf);
238 syslog(LOG_ERR, d.buf);
243 static void sighup(int sig, void *v)
248 static void cleanup(void)
254 static void sigdie(int sig)
257 signal(sig, SIG_DFL);
261 static void version(FILE *fp)
263 pquis(fp, "$, TrIPE version " VERSION "\n");
266 static void usage(FILE *fp)
270 $ [-w] [-options] [command [args]...]\n\
271 $ [-Dl] [-f file] [-options]\n\
273 [-s] [-d directory] [-a socket] [-P pidfile]\n\
274 [-p program] [-S arg,arg,...]\n\
278 static void help(FILE *fp)
287 -h, --help Show this help text.\n\
288 -v, --version Show version number.\n\
289 -u, --usage Show brief usage message.\n\
291 -D, --daemon Become a background task after connecting.\n\
292 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
293 -a, --admin-socket=FILE Select socket to connect to.\n\
294 -P, --pidfile=FILE Write process-id to FILE.\n\
296 -s, --spawn Start server rather than connecting.\n\
297 -p, --spawn-path=PATH Specify path to executable.\n\
298 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
300 -l, --syslog Log messages to system log.\n\
301 -f, --logfile=FILE Log messages to FILE.\n\
302 -w, --warnings Show warnings when running commands.\n\
306 int main(int argc, char *argv[])
308 const char *dir = "/var/lib/tripe";
309 const char *sock = "tripesock";
310 const char *spawnpath = "tripe";
311 string_v spawnopts = DA_INIT;
317 if ((p = getenv("TRIPEDIR")) != 0)
320 /* --- Parse the arguments --- */
323 static const struct option opts[] = {
324 { "help", 0, 0, 'h' },
325 { "version", 0, 0, 'v' },
326 { "usage", 0, 0, 'u' },
327 { "daemon", 0, 0, 'D' },
328 { "directory", OPTF_ARGREQ, 0, 'd' },
329 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
330 { "spawn", 0, 0, 's' },
331 { "spawn-path", OPTF_ARGREQ, 0, 'p' },
332 { "spawn-args", OPTF_ARGREQ, 0, 'S' },
333 { "syslog", 0, 0, 'l' },
334 { "logfile", OPTF_ARGREQ, 0, 'f' },
335 { "warnings", 0, 0, 'w' },
336 { "pidfile", OPTF_ARGREQ, 0, 'P' },
340 int i = mdwopt(argc, argv, "+hvuDd:a:sp:S:lwf:nP:", opts, 0, 0, 0);
354 f |= f_daemon | f_noinput;
370 f |= f_spawn | f_spawnopts;
371 for (p = strtok(optarg, ","); p; p = strtok(0, ","))
372 DA_PUSH(&spawnopts, p);
375 f |= f_syslog | f_noinput;
392 if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) {
397 /* --- Set various things up --- */
400 die(EXIT_FAILURE, "couldn't set `%s' as current directory: %s",
401 dir, strerror(errno));
405 if (!pidfile && (f & f_daemon) && ((f & f_syslog) || logname))
406 pidfile = "tripectl.pid";
407 if (pidfile && (pidfp = fopen(pidfile, "w")) == 0) {
408 die(EXIT_FAILURE, "couldn't open `%s' for writing: %s",
409 pidfile, strerror(errno));
411 signal(SIGINT, sigdie);
412 signal(SIGQUIT, sigdie);
413 signal(SIGTERM, sigdie);
416 /* --- Connect to the server --- */
422 sigset_t newmask, oldmask;
424 sa.sa_handler = reap;
425 sigemptyset(&sa.sa_mask);
426 sa.sa_flags = SA_NOCLDSTOP;
428 sa.sa_flags |= SA_RESTART;
430 sigaction(SIGCHLD, &sa, 0);
432 DA_UNSHIFT(&spawnopts, (char *)sock);
433 DA_UNSHIFT(&spawnopts, "-a");
434 DA_UNSHIFT(&spawnopts, "-d.");
435 DA_UNSHIFT(&spawnopts, (char *)spawnpath);
436 DA_PUSH(&spawnopts, 0);
437 if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd))
438 die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno));
439 sigemptyset(&newmask);
440 sigaddset(&newmask, SIGCHLD);
441 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
442 if ((kid = fork()) < 0)
443 die(EXIT_FAILURE, "fork failed: %s", strerror(errno));
445 dup2(pfd[1], STDIN_FILENO);
446 dup2(pfd[1], STDOUT_FILENO);
456 execvp(DA(&spawnopts)[0], DA(&spawnopts));
457 die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno));
459 sigprocmask(SIG_SETMASK, &oldmask, 0);
463 struct sockaddr_un sun;
464 size_t sz = strlen(sock) + 1;
465 if (sz > sizeof(sun.sun_path))
466 die(EXIT_FAILURE, "socket name `%s' too long", sock);
467 memset(&sun, 0, sizeof(sun));
468 sun.sun_family = AF_UNIX;
469 memcpy(sun.sun_path, sock, sz);
470 sz = sz + offsetof(struct sockaddr_un, sun_path);
471 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
472 die(EXIT_FAILURE, "error making socket: %s", strerror(errno));
473 if (connect(fd, (struct sockaddr *)&sun, sz)) {
474 die(EXIT_FAILURE, "error connecting to `%s': %s",
475 sun.sun_path, strerror(errno));
481 die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
484 fprintf(pidfp, "%li\n", (long)getpid());
488 /* --- If we're meant to be interactive, do that --- */
490 if (!(f & f_noinput) && optind == argc) {
495 selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu);
496 selbuf_init(&bs, &sel, fd, sline, &bs);
498 if (sel_select(&sel) && errno != EINTR && errno != EAGAIN)
499 die(EXIT_FAILURE, "select failed: %s", strerror(errno));
503 /* --- If there's a command, submit it --- */
507 dstr_puts(&d, argv[optind++]);
508 while (optind < argc) {
510 dstr_puts(&d, argv[optind++]);
513 write(fd, d.buf, d.len);
519 /* --- Pull everything else out of the box --- */
527 selbuf_init(&b, &sel, fd, cline, 0);
530 openlog(QUIS, 0, LOG_DAEMON);
533 sig_add(&hup, SIGHUP, sighup, 0);
536 if (sel_select(&sel) && errno != EINTR && errno != EAGAIN)
537 die(EXIT_FAILURE, "select failed: %s", strerror(errno));
544 /*----- That's all, folks -------------------------------------------------*/