3 * $Id: client.c,v 1.6 2001/02/22 09:06:08 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.6 2001/02/22 09:06:08 mdw
33 * Fix logfile rotation to avoid program collapse.
35 * Revision 1.5 2001/02/16 21:23:39 mdw
36 * Use reliable signal handling for reopening logs.
38 * Revision 1.4 2001/02/06 09:34:53 mdw
39 * Change ERR response to FAIL for consistency with other programs.
41 * Revision 1.3 2001/02/04 17:10:15 mdw
42 * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't
43 * change directory -- just mangle pathnames instead.
45 * Revision 1.2 2001/02/04 01:17:54 mdw
46 * Create a configuration header file to tidy up command lines.
48 * Revision 1.1 2001/02/03 20:26:37 mdw
53 /*----- Header files ------------------------------------------------------*/
65 #include <sys/types.h>
72 #include <sys/socket.h>
74 #include <arpa/inet.h>
77 #include <mLib/alloc.h>
78 #include <mLib/darray.h>
79 #include <mLib/dstr.h>
80 #include <mLib/mdwopt.h>
81 #include <mLib/quis.h>
82 #include <mLib/report.h>
84 #include <mLib/selbuf.h>
92 /*----- Data structures ---------------------------------------------------*/
96 DA_DECL(string_v, char *);
99 /*----- Static variables --------------------------------------------------*/
101 static FILE *logfp = 0;
102 static unsigned f = 0;
103 static const char *logname = 0;
109 #define f_spawnopts 8u
111 #define f_command 32u
112 #define f_noinput 64u
114 #define f_uclose 256u
116 /*----- Main code ---------------------------------------------------------*/
118 static void reap(int sig)
122 while (waitpid(-1, &s, WNOHANG) > 0)
127 static void writelog(const char *cat, const char *msg)
131 struct tm *tm = localtime(&t);
132 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
133 fprintf(logfp, "%s %s: %s\n", buf, cat, msg);
136 static void cline(char *p, void *b)
141 die(EXIT_FAILURE, "server dropped the connection");
147 if (strcmp(q, "WARN") == 0) {
149 syslog(LOG_WARNING, "%s", p);
151 writelog("warning", p);
153 fprintf(stderr, "Warning: %s\n", p);
154 } else if (strcmp(q, "TRACE") == 0) {
156 syslog(LOG_DEBUG, "%s", p);
158 writelog("debug", p);
159 } else if (!(f & f_command)) {
161 syslog(LOG_ERR, "unexpected output `%s %s'", q, p);
164 dstr_putf(&d, "unexpected output `%s %s'", q, p);
165 writelog("error", d.buf);
168 } else if (strcmp(q, "FAIL") == 0)
169 die(EXIT_FAILURE, "%s", p);
170 else if (strcmp(q, "INFO") == 0)
172 else if (strcmp(q, "OK") == 0)
175 die(EXIT_FAILURE, "unexpected output `%s %s'", q, p);
178 static void sline(char *p, void *b)
182 moan("server closed the connection");
188 static void uline(char *p, void *b)
198 write(fd, p, sz + 1);
202 static void logfile(const char *name)
206 if ((fp = fopen(name, "a")) != 0) {
210 setvbuf(logfp, 0, _IOLBF, BUFSIZ);
213 dstr_putf(&d, "error opening logfile `%s': %s", name, strerror(errno));
215 writelog("error", d.buf);
217 die(EXIT_FAILURE, d.buf);
219 syslog(LOG_ERR, d.buf);
224 static void sighup(int sig, void *v)
229 static void version(FILE *fp)
231 pquis(fp, "$, TrIPE version " VERSION "\n");
234 static void usage(FILE *fp)
238 $ [-w] [-options] [command [args]...]\n\
239 $ [-Dl] [-f file] [-options]\n\
241 [-s] [-d directory] [-a socket] [-p program] [-S arg,arg,...]\n\
245 static void help(FILE *fp)
254 -h, --help Show this help text.\n\
255 -v, --version Show version number.\n\
256 -u, --usage Show brief usage message.\n\
258 -D, --daemon Become a background task after connecting.\n\
259 -d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
260 -a, --admin-socket=FILE Select socket to connect to.\n\
262 -s, --spawn Start server rather than connecting.\n\
263 -p, --spawn-path=PATH Specify path to executable.\n\
264 -S, --spawn-args=ARGS Specify comma-separated arguments.\n\
266 -l, --syslog Log messages to system log.\n\
267 -f, --logfile=FILE Log messages to FILE.\n\
268 -w, --warnings Show warnings when running commands.\n\
272 int main(int argc, char *argv[])
274 const char *dir = "/var/lib/tripe";
275 const char *sock = "tripesock";
276 const char *spawnpath = "tripe";
277 string_v spawnopts = DA_INIT;
282 if ((p = getenv("TRIPEDIR")) != 0)
285 /* --- Parse the arguments --- */
288 static const struct option opts[] = {
289 { "help", 0, 0, 'h' },
290 { "version", 0, 0, 'v' },
291 { "usage", 0, 0, 'u' },
292 { "daemon", 0, 0, 'D' },
293 { "directory", OPTF_ARGREQ, 0, 'd' },
294 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
295 { "spawn", 0, 0, 's' },
296 { "spawn-path", OPTF_ARGREQ, 0, 'p' },
297 { "spawn-args", OPTF_ARGREQ, 0, 'S' },
298 { "syslog", 0, 0, 'l' },
299 { "logfile", OPTF_ARGREQ, 0, 'f' },
300 { "warnings", 0, 0, 'w' },
304 int i = mdwopt(argc, argv, "hvuDd:a:sp:S:lwf:n", opts, 0, 0, 0);
318 f |= f_daemon | f_noinput;
334 f |= f_spawn | f_spawnopts;
335 for (p = strtok(optarg, ","); p; p = strtok(0, ","))
336 DA_PUSH(&spawnopts, p);
339 f |= f_syslog | f_noinput;
354 if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) {
359 /* --- Connect to the server --- */
365 sigset_t newmask, oldmask;
367 sa.sa_handler = reap;
368 sigemptyset(&sa.sa_mask);
369 sa.sa_flags = SA_NOCLDSTOP;
371 sa.sa_flags |= SA_RESTART;
373 sigaction(SIGCHLD, &sa, 0);
375 DA_UNSHIFT(&spawnopts, (char *)spawnpath);
376 if (!(f & f_spawnopts)) {
377 DA_PUSH(&spawnopts, "-d");
378 DA_PUSH(&spawnopts, (char *)dir);
379 DA_PUSH(&spawnopts, "-a");
380 DA_PUSH(&spawnopts, (char *)sock);
382 DA_PUSH(&spawnopts, 0);
383 if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd))
384 die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno));
385 sigemptyset(&newmask);
386 sigaddset(&newmask, SIGCHLD);
387 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
388 if ((kid = fork()) < 0)
389 die(EXIT_FAILURE, "fork failed: %s", strerror(errno));
391 dup2(pfd[1], STDIN_FILENO);
392 dup2(pfd[1], STDOUT_FILENO);
398 execvp(DA(&spawnopts)[0], DA(&spawnopts));
399 die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno));
401 sigprocmask(SIG_SETMASK, &oldmask, 0);
405 struct sockaddr_un sun;
406 size_t sz = strlen(sock) + 1;
408 dstr_putf(&d, "%s/%s", dir, sock);
409 if (d.sz + 1 > sizeof(sun.sun_path))
410 die(EXIT_FAILURE, "socket name `%s' too long", sock);
411 memset(&sun, 0, sizeof(sun));
412 sun.sun_family = AF_UNIX;
413 memcpy(sun.sun_path, d.buf, d.sz + 1);
414 sz = d.sz + offsetof(struct sockaddr_un, sun_path) + 1;
416 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
417 die(EXIT_FAILURE, "error making socket: %s", strerror(errno));
418 if (connect(fd, (struct sockaddr *)&sun, sz)) {
419 die(EXIT_FAILURE, "error connecting to `%s': %s",
420 sun.sun_path, strerror(errno));
426 die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
429 /* --- If we're meant to be interactive, do that --- */
431 if (!(f & f_noinput) && optind == argc) {
436 selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu);
437 selbuf_init(&bs, &sel, fd, sline, &bs);
439 if (sel_select(&sel) && errno != EINTR && errno != EAGAIN)
440 die(EXIT_FAILURE, "select failed: %s", strerror(errno));
444 /* --- If there's a command, submit it --- */
448 dstr_puts(&d, argv[optind++]);
449 while (optind < argc) {
451 dstr_puts(&d, argv[optind++]);
454 write(fd, d.buf, d.len);
460 /* --- Pull everything else out of the box --- */
468 selbuf_init(&b, &sel, fd, cline, 0);
471 openlog(QUIS, 0, LOG_DAEMON);
474 sig_add(&hup, SIGHUP, sighup, 0);
477 if (sel_select(&sel) && errno != EINTR && errno != EAGAIN)
478 die(EXIT_FAILURE, "select failed: %s", strerror(errno));
485 /*----- That's all, folks -------------------------------------------------*/