3 * $Id: client.c,v 1.4 2001/02/06 09:34:53 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.4 2001/02/06 09:34:53 mdw
33 * Change ERR response to FAIL for consistency with other programs.
35 * Revision 1.3 2001/02/04 17:10:15 mdw
36 * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't
37 * change directory -- just mangle pathnames instead.
39 * Revision 1.2 2001/02/04 01:17:54 mdw
40 * Create a configuration header file to tidy up command lines.
42 * Revision 1.1 2001/02/03 20:26:37 mdw
47 /*----- Header files ------------------------------------------------------*/
59 #include <sys/types.h>
66 #include <sys/socket.h>
68 #include <arpa/inet.h>
71 #include <mLib/alloc.h>
72 #include <mLib/darray.h>
73 #include <mLib/dstr.h>
74 #include <mLib/lbuf.h>
75 #include <mLib/mdwopt.h>
76 #include <mLib/quis.h>
77 #include <mLib/report.h>
79 #include <mLib/selbuf.h>
86 /*----- Data structures ---------------------------------------------------*/
90 DA_DECL(string_v, char *);
93 /*----- Static variables --------------------------------------------------*/
95 static FILE *logfp = 0;
96 static unsigned f = 0;
98 static volatile sig_atomic_t reopen = 0;
103 #define f_spawnopts 8u
105 #define f_command 32u
106 #define f_noinput 64u
108 #define f_uclose 256u
110 /*----- Main code ---------------------------------------------------------*/
112 static void reap(int sig)
116 while (waitpid(-1, &s, WNOHANG) > 0)
121 static void sighup(int sig)
126 static void writelog(const char *cat, const char *msg)
130 struct tm *tm = localtime(&t);
131 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
132 fprintf(logfp, "%s %s: %s\n", buf, cat, msg);
135 static void cline(char *p, void *b)
140 die(EXIT_FAILURE, "server dropped the connection");
146 if (strcmp(q, "WARN") == 0) {
148 syslog(LOG_WARNING, "%s", p);
150 writelog("warning", p);
152 fprintf(stderr, "Warning: %s\n", p);
153 } else if (strcmp(q, "TRACE") == 0) {
155 syslog(LOG_DEBUG, "%s", p);
157 writelog("debug", p);
158 } else if (!(f & f_command)) {
160 syslog(LOG_ERR, "unexpected output `%s %s'", q, p);
163 dstr_putf(&d, "unexpected output `%s %s'", q, p);
164 writelog("error", d.buf);
167 } else if (strcmp(q, "FAIL") == 0)
168 die(EXIT_FAILURE, "%s", p);
169 else if (strcmp(q, "INFO") == 0)
171 else if (strcmp(q, "OK") == 0)
174 die(EXIT_FAILURE, "unexpected output `%s %s'", q, p);
177 static void sline(char *p, void *b)
181 moan("server closed the connection");
187 static void uline(char *p, void *b)
197 write(fd, p, sz + 1);
201 static void logfile(const char *name)
205 if ((logfp = fopen(name, "a")) == 0) {
206 die(EXIT_FAILURE, "error opening logfile `%s': %s",
207 name, strerror(errno));
209 setvbuf(logfp, 0, _IOLBF, BUFSIZ);
212 static void version(FILE *fp)
214 pquis(fp, "$, TrIPE version " VERSION "\n");
217 static void usage(FILE *fp)
221 $ [-w] [-options] [command [args]...]\n\
222 $ [-Dl] [-f file] [-options]\n\
224 [-s] [-d directory] [-a socket] [-p program] [-S arg,arg,...]\n\
228 static void help(FILE *fp)
237 -h, --help Show this help text.\n\
238 -v, --version Show version number.\n\
239 -u, --usage Show brief usage message.\n\
241 -D, --daemon Become a background task after connecting.\n\
242 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
243 -a, --admin-socket=FILE Select socket to connect to.\n\
245 -s, --spawn Start server rather than connecting.\n\
246 -p, --spawn-path=PATH Specify path to executable.\n\
247 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
249 -l, --syslog Log messages to system log.\n\
250 -f, --logfile=FILE Log messages to FILE.\n\
251 -w, --warnings Show warnings when running commands.\n\
255 int main(int argc, char *argv[])
257 const char *dir = "/var/lib/tripe";
258 const char *sock = "tripesock";
259 const char *spawnpath = "tripe";
260 string_v spawnopts = DA_INIT;
261 const char *logname = 0;
266 if ((p = getenv("TRIPEDIR")) != 0)
269 /* --- Parse the arguments --- */
272 static const struct option opts[] = {
273 { "help", 0, 0, 'h' },
274 { "version", 0, 0, 'v' },
275 { "usage", 0, 0, 'u' },
276 { "daemon", 0, 0, 'D' },
277 { "directory", OPTF_ARGREQ, 0, 'd' },
278 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
279 { "spawn", 0, 0, 's' },
280 { "spawn-path", OPTF_ARGREQ, 0, 'p' },
281 { "spawn-args", OPTF_ARGREQ, 0, 'S' },
282 { "syslog", 0, 0, 'l' },
283 { "logfile", OPTF_ARGREQ, 0, 'f' },
284 { "warnings", 0, 0, 'w' },
288 int i = mdwopt(argc, argv, "hvuDd:a:sp:S:lwf:n", opts, 0, 0, 0);
302 f |= f_daemon | f_noinput;
318 f |= f_spawn | f_spawnopts;
319 for (p = strtok(optarg, ","); p; p = strtok(0, ","))
320 DA_PUSH(&spawnopts, p);
323 f |= f_syslog | f_noinput;
338 if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) {
343 /* --- Connect to the server --- */
349 sigset_t newmask, oldmask;
351 sa.sa_handler = reap;
352 sigemptyset(&sa.sa_mask);
353 sa.sa_flags = SA_NOCLDSTOP;
355 sa.sa_flags |= SA_RESTART;
357 sigaction(SIGCHLD, &sa, 0);
359 DA_UNSHIFT(&spawnopts, (char *)spawnpath);
360 if (!(f & f_spawnopts)) {
361 DA_PUSH(&spawnopts, "-d");
362 DA_PUSH(&spawnopts, (char *)dir);
363 DA_PUSH(&spawnopts, "-a");
364 DA_PUSH(&spawnopts, (char *)sock);
366 DA_PUSH(&spawnopts, 0);
367 if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd))
368 die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno));
369 sigemptyset(&newmask);
370 sigaddset(&newmask, SIGCHLD);
371 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
372 if ((kid = fork()) < 0)
373 die(EXIT_FAILURE, "fork failed: %s", strerror(errno));
375 dup2(pfd[1], STDIN_FILENO);
376 dup2(pfd[1], STDOUT_FILENO);
382 execvp(DA(&spawnopts)[0], DA(&spawnopts));
383 die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno));
385 sigprocmask(SIG_SETMASK, &oldmask, 0);
389 struct sockaddr_un sun;
390 size_t sz = strlen(sock) + 1;
392 dstr_putf(&d, "%s/%s", dir, sock);
393 if (d.sz + 1 > sizeof(sun.sun_path))
394 die(EXIT_FAILURE, "socket name `%s' too long", sock);
395 memset(&sun, 0, sizeof(sun));
396 sun.sun_family = AF_UNIX;
397 memcpy(sun.sun_path, d.buf, d.sz + 1);
398 sz = d.sz + offsetof(struct sockaddr_un, sun_path) + 1;
400 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
401 die(EXIT_FAILURE, "error making socket: %s", strerror(errno));
402 if (connect(fd, (struct sockaddr *)&sun, sz)) {
403 die(EXIT_FAILURE, "error connecting to `%s': %s",
404 sun.sun_path, strerror(errno));
410 die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
413 /* --- If we're meant to be interactive, do that --- */
415 if (!(f & f_noinput) && optind == argc) {
420 selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu);
421 selbuf_init(&bs, &sel, fd, sline, &bs);
423 if (sel_select(&sel))
424 die(EXIT_FAILURE, "select failed: %s", strerror(errno));
428 /* --- If there's a command, submit it --- */
432 dstr_puts(&d, argv[optind++]);
433 while (optind < argc) {
435 dstr_puts(&d, argv[optind++]);
438 write(fd, d.buf, d.len);
444 /* --- Pull everything else out of the box --- */
448 lbuf_init(&b, cline, 0);
450 openlog(QUIS, 0, LOG_DAEMON);
453 sa.sa_handler = sighup;
455 sigemptyset(&sa.sa_mask);
456 sigaction(SIGHUP, &sa, 0);
465 sz = lbuf_free(&b, &p);
468 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
470 die(EXIT_FAILURE, "read failed: %s", strerror(errno));
474 lbuf_flush(&b, p, n);
482 /*----- That's all, folks -------------------------------------------------*/