3 * $Id: client.c,v 1.3 2001/02/04 17:10:15 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.3 2001/02/04 17:10:15 mdw
33 * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't
34 * change directory -- just mangle pathnames instead.
36 * Revision 1.2 2001/02/04 01:17:54 mdw
37 * Create a configuration header file to tidy up command lines.
39 * Revision 1.1 2001/02/03 20:26:37 mdw
44 /*----- Header files ------------------------------------------------------*/
56 #include <sys/types.h>
63 #include <sys/socket.h>
65 #include <arpa/inet.h>
68 #include <mLib/alloc.h>
69 #include <mLib/darray.h>
70 #include <mLib/dstr.h>
71 #include <mLib/lbuf.h>
72 #include <mLib/mdwopt.h>
73 #include <mLib/quis.h>
74 #include <mLib/report.h>
76 #include <mLib/selbuf.h>
83 /*----- Data structures ---------------------------------------------------*/
87 DA_DECL(string_v, char *);
90 /*----- Static variables --------------------------------------------------*/
92 static FILE *logfp = 0;
93 static unsigned f = 0;
95 static volatile sig_atomic_t reopen = 0;
100 #define f_spawnopts 8u
102 #define f_command 32u
103 #define f_noinput 64u
105 #define f_uclose 256u
107 /*----- Main code ---------------------------------------------------------*/
109 static void reap(int sig)
113 while (waitpid(-1, &s, WNOHANG) > 0)
118 static void sighup(int sig)
123 static void writelog(const char *cat, const char *msg)
127 struct tm *tm = localtime(&t);
128 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
129 fprintf(logfp, "%s %s: %s\n", buf, cat, msg);
132 static void cline(char *p, void *b)
137 die(EXIT_FAILURE, "server dropped the connection");
143 if (strcmp(q, "WARN") == 0) {
145 syslog(LOG_WARNING, "%s", p);
147 writelog("warning", p);
149 fprintf(stderr, "Warning: %s\n", p);
150 } else if (strcmp(q, "TRACE") == 0) {
152 syslog(LOG_DEBUG, "%s", p);
154 writelog("debug", p);
155 } else if (!(f & f_command)) {
157 syslog(LOG_ERR, "unexpected output `%s %s'", q, p);
160 dstr_putf(&d, "unexpected output `%s %s'", q, p);
161 writelog("error", d.buf);
164 } else if (strcmp(q, "ERR") == 0)
165 die(EXIT_FAILURE, "%s", p);
166 else if (strcmp(q, "INFO") == 0)
168 else if (strcmp(q, "OK") == 0)
171 die(EXIT_FAILURE, "unexpected output `%s %s'", q, p);
174 static void sline(char *p, void *b)
178 moan("server closed the connection");
184 static void uline(char *p, void *b)
194 write(fd, p, sz + 1);
198 static void logfile(const char *name)
202 if ((logfp = fopen(name, "a")) == 0) {
203 die(EXIT_FAILURE, "error opening logfile `%s': %s",
204 name, strerror(errno));
206 setvbuf(logfp, 0, _IOLBF, BUFSIZ);
209 static void version(FILE *fp)
211 pquis(fp, "$, TrIPE version " VERSION "\n");
214 static void usage(FILE *fp)
218 $ [-w] [-options] [command [args]...]\n\
219 $ [-Dl] [-f file] [-options]\n\
221 [-s] [-d directory] [-a socket] [-p program] [-S arg,arg,...]\n\
225 static void help(FILE *fp)
234 -h, --help Show this help text.\n\
235 -v, --version Show version number.\n\
236 -u, --usage Show brief usage message.\n\
238 -D, --daemon Become a background task after connecting.\n\
239 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
240 -a, --admin-socket=FILE Select socket to connect to.\n\
242 -s, --spawn Start server rather than connecting.\n\
243 -p, --spawn-path=PATH Specify path to executable.\n\
244 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
246 -l, --syslog Log messages to system log.\n\
247 -f, --logfile=FILE Log messages to FILE.\n\
248 -w, --warnings Show warnings when running commands.\n\
252 int main(int argc, char *argv[])
254 const char *dir = "/var/lib/tripe";
255 const char *sock = "tripesock";
256 const char *spawnpath = "tripe";
257 string_v spawnopts = DA_INIT;
258 const char *logname = 0;
263 if ((p = getenv("TRIPEDIR")) != 0)
266 /* --- Parse the arguments --- */
269 static const struct option opts[] = {
270 { "help", 0, 0, 'h' },
271 { "version", 0, 0, 'v' },
272 { "usage", 0, 0, 'u' },
273 { "daemon", 0, 0, 'D' },
274 { "directory", OPTF_ARGREQ, 0, 'd' },
275 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
276 { "spawn", 0, 0, 's' },
277 { "spawn-path", OPTF_ARGREQ, 0, 'p' },
278 { "spawn-args", OPTF_ARGREQ, 0, 'S' },
279 { "syslog", 0, 0, 'l' },
280 { "logfile", OPTF_ARGREQ, 0, 'f' },
281 { "warnings", 0, 0, 'w' },
285 int i = mdwopt(argc, argv, "hvuDd:a:sp:S:lwf:n", opts, 0, 0, 0);
299 f |= f_daemon | f_noinput;
315 f |= f_spawn | f_spawnopts;
316 for (p = strtok(optarg, ","); p; p = strtok(0, ","))
317 DA_PUSH(&spawnopts, p);
320 f |= f_syslog | f_noinput;
335 if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) {
340 /* --- Connect to the server --- */
346 sigset_t newmask, oldmask;
348 sa.sa_handler = reap;
349 sigemptyset(&sa.sa_mask);
350 sa.sa_flags = SA_NOCLDSTOP;
352 sa.sa_flags |= SA_RESTART;
354 sigaction(SIGCHLD, &sa, 0);
356 DA_UNSHIFT(&spawnopts, (char *)spawnpath);
357 if (!(f & f_spawnopts)) {
358 DA_PUSH(&spawnopts, "-d");
359 DA_PUSH(&spawnopts, (char *)dir);
360 DA_PUSH(&spawnopts, "-a");
361 DA_PUSH(&spawnopts, (char *)sock);
363 DA_PUSH(&spawnopts, 0);
364 if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd))
365 die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno));
366 sigemptyset(&newmask);
367 sigaddset(&newmask, SIGCHLD);
368 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
369 if ((kid = fork()) < 0)
370 die(EXIT_FAILURE, "fork failed: %s", strerror(errno));
372 dup2(pfd[1], STDIN_FILENO);
373 dup2(pfd[1], STDOUT_FILENO);
379 execvp(DA(&spawnopts)[0], DA(&spawnopts));
380 die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno));
382 sigprocmask(SIG_SETMASK, &oldmask, 0);
386 struct sockaddr_un sun;
387 size_t sz = strlen(sock) + 1;
389 dstr_putf(&d, "%s/%s", dir, sock);
390 if (d.sz + 1 > sizeof(sun.sun_path))
391 die(EXIT_FAILURE, "socket name `%s' too long", sock);
392 memset(&sun, 0, sizeof(sun));
393 sun.sun_family = AF_UNIX;
394 memcpy(sun.sun_path, d.buf, d.sz + 1);
395 sz = d.sz + offsetof(struct sockaddr_un, sun_path) + 1;
397 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
398 die(EXIT_FAILURE, "error making socket: %s", strerror(errno));
399 if (connect(fd, (struct sockaddr *)&sun, sz)) {
400 die(EXIT_FAILURE, "error connecting to `%s': %s",
401 sun.sun_path, strerror(errno));
407 die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
410 /* --- If we're meant to be interactive, do that --- */
412 if (!(f & f_noinput) && optind == argc) {
417 selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu);
418 selbuf_init(&bs, &sel, fd, sline, &bs);
420 if (sel_select(&sel))
421 die(EXIT_FAILURE, "select failed: %s", strerror(errno));
425 /* --- If there's a command, submit it --- */
429 dstr_puts(&d, argv[optind++]);
430 while (optind < argc) {
432 dstr_puts(&d, argv[optind++]);
435 write(fd, d.buf, d.len);
441 /* --- Pull everything else out of the box --- */
445 lbuf_init(&b, cline, 0);
447 openlog(QUIS, 0, LOG_DAEMON);
450 sa.sa_handler = sighup;
452 sigemptyset(&sa.sa_mask);
453 sigaction(SIGHUP, &sa, 0);
462 sz = lbuf_free(&b, &p);
465 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
467 die(EXIT_FAILURE, "read failed: %s", strerror(errno));
471 lbuf_flush(&b, p, n);
479 /*----- That's all, folks -------------------------------------------------*/