410c8acf |
1 | /* -*-c-*- |
2 | * |
ef4a1ab7 |
3 | * $Id: client.c,v 1.13 2003/11/29 23:49:32 mdw Exp $ |
410c8acf |
4 | * |
5 | * Client for TrIPE |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Trivial IP Encryption (TrIPE). |
13 | * |
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. |
18 | * |
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. |
23 | * |
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. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: client.c,v $ |
ef4a1ab7 |
32 | * Revision 1.13 2003/11/29 23:49:32 mdw |
33 | * Debianization. |
34 | * |
dd9647cc |
35 | * Revision 1.12 2003/10/15 09:29:38 mdw |
36 | * Cosmetic fix to changelog comment. |
37 | * |
7ed14135 |
38 | * Revision 1.11 2003/07/13 11:19:49 mdw |
dd9647cc |
39 | * Incompatible protocol fix! Include message type code under MAC tag to |
40 | * prevent cut-and-paste from key-exchange messages to general packet |
41 | * transport. |
7ed14135 |
42 | * |
a803add7 |
43 | * Revision 1.10 2003/05/17 10:59:19 mdw |
44 | * Put a newline in the pidfile. |
45 | * |
6be88c14 |
46 | * Revision 1.9 2002/01/13 14:57:18 mdw |
47 | * Track @lbuf@ changes in mLib. |
48 | * |
d071d798 |
49 | * Revision 1.8 2001/06/19 22:09:37 mdw |
50 | * Move the program name to the right place when constructing the arguments |
51 | * to pass to a new server. |
52 | * |
06b2a088 |
53 | * Revision 1.7 2001/02/22 09:07:54 mdw |
54 | * Write a pidfile on request, and delete it when finished. |
55 | * |
fa4bef80 |
56 | * Revision 1.6 2001/02/22 09:06:08 mdw |
57 | * Fix logfile rotation to avoid program collapse. |
58 | * |
5f5150b1 |
59 | * Revision 1.5 2001/02/16 21:23:39 mdw |
60 | * Use reliable signal handling for reopening logs. |
61 | * |
7fadfa0d |
62 | * Revision 1.4 2001/02/06 09:34:53 mdw |
63 | * Change ERR response to FAIL for consistency with other programs. |
64 | * |
9dd01bc6 |
65 | * Revision 1.3 2001/02/04 17:10:15 mdw |
66 | * Reopen logfiles on receipt of @SIGHUP@ (not done very well). Don't |
67 | * change directory -- just mangle pathnames instead. |
68 | * |
73189848 |
69 | * Revision 1.2 2001/02/04 01:17:54 mdw |
70 | * Create a configuration header file to tidy up command lines. |
71 | * |
410c8acf |
72 | * Revision 1.1 2001/02/03 20:26:37 mdw |
73 | * Initial checkin. |
74 | * |
75 | */ |
76 | |
77 | /*----- Header files ------------------------------------------------------*/ |
78 | |
73189848 |
79 | #include "config.h" |
80 | |
410c8acf |
81 | #include <ctype.h> |
82 | #include <errno.h> |
83 | #include <signal.h> |
84 | #include <stdio.h> |
85 | #include <stdlib.h> |
86 | #include <string.h> |
87 | #include <time.h> |
88 | |
89 | #include <sys/types.h> |
90 | #include <sys/time.h> |
91 | #include <unistd.h> |
92 | #include <fcntl.h> |
93 | #include <sys/wait.h> |
94 | #include <syslog.h> |
95 | |
96 | #include <sys/socket.h> |
97 | #include <sys/un.h> |
98 | #include <arpa/inet.h> |
99 | #include <netdb.h> |
100 | |
101 | #include <mLib/alloc.h> |
102 | #include <mLib/darray.h> |
103 | #include <mLib/dstr.h> |
410c8acf |
104 | #include <mLib/mdwopt.h> |
105 | #include <mLib/quis.h> |
106 | #include <mLib/report.h> |
107 | #include <mLib/sel.h> |
108 | #include <mLib/selbuf.h> |
5f5150b1 |
109 | #include <mLib/sig.h> |
410c8acf |
110 | #include <mLib/str.h> |
111 | |
112 | #include "util.h" |
113 | |
114 | #undef sun |
115 | |
116 | /*----- Data structures ---------------------------------------------------*/ |
117 | |
118 | #ifndef STRING_V |
119 | # define STRING_V |
120 | DA_DECL(string_v, char *); |
121 | #endif |
122 | |
123 | /*----- Static variables --------------------------------------------------*/ |
124 | |
06b2a088 |
125 | static const char *pidfile = 0; |
126 | static const char *logname = 0; |
410c8acf |
127 | static FILE *logfp = 0; |
128 | static unsigned f = 0; |
129 | static int fd; |
130 | |
131 | #define f_bogus 1u |
132 | #define f_spawn 2u |
133 | #define f_daemon 4u |
134 | #define f_spawnopts 8u |
135 | #define f_syslog 16u |
136 | #define f_command 32u |
137 | #define f_noinput 64u |
138 | #define f_warn 128u |
139 | #define f_uclose 256u |
140 | |
141 | /*----- Main code ---------------------------------------------------------*/ |
142 | |
143 | static void reap(int sig) |
144 | { |
410c8acf |
145 | int e = errno; |
06b2a088 |
146 | while (waitpid(-1, 0, WNOHANG) > 0) |
410c8acf |
147 | ; |
148 | errno = e; |
149 | } |
150 | |
151 | static void writelog(const char *cat, const char *msg) |
152 | { |
153 | char buf[256]; |
154 | time_t t = time(0); |
155 | struct tm *tm = localtime(&t); |
156 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm); |
157 | fprintf(logfp, "%s %s: %s\n", buf, cat, msg); |
158 | } |
159 | |
6be88c14 |
160 | static void cline(char *p, size_t len, void *b) |
410c8acf |
161 | { |
162 | char *q; |
163 | if (!p) { |
164 | if (f & f_command) |
165 | die(EXIT_FAILURE, "server dropped the connection"); |
166 | exit(0); |
167 | } |
168 | q = str_getword(&p); |
169 | if (!q) |
170 | return; |
171 | if (strcmp(q, "WARN") == 0) { |
172 | if (f & f_syslog) |
173 | syslog(LOG_WARNING, "%s", p); |
174 | if (logfp) |
175 | writelog("warning", p); |
176 | if (f & f_warn) |
177 | fprintf(stderr, "Warning: %s\n", p); |
178 | } else if (strcmp(q, "TRACE") == 0) { |
179 | if (f & f_syslog) |
180 | syslog(LOG_DEBUG, "%s", p); |
181 | if (logfp) |
182 | writelog("debug", p); |
183 | } else if (!(f & f_command)) { |
184 | if (f & f_syslog) |
185 | syslog(LOG_ERR, "unexpected output `%s %s'", q, p); |
186 | if (logfp) { |
187 | dstr d = DSTR_INIT; |
188 | dstr_putf(&d, "unexpected output `%s %s'", q, p); |
189 | writelog("error", d.buf); |
190 | dstr_destroy(&d); |
191 | } |
7fadfa0d |
192 | } else if (strcmp(q, "FAIL") == 0) |
410c8acf |
193 | die(EXIT_FAILURE, "%s", p); |
194 | else if (strcmp(q, "INFO") == 0) |
195 | puts(p); |
196 | else if (strcmp(q, "OK") == 0) |
197 | exit(0); |
198 | else |
199 | die(EXIT_FAILURE, "unexpected output `%s %s'", q, p); |
200 | } |
201 | |
6be88c14 |
202 | static void sline(char *p, size_t len, void *b) |
410c8acf |
203 | { |
204 | if (!p) { |
205 | if (!(f & f_uclose)) |
206 | moan("server closed the connection"); |
207 | exit(0); |
208 | } |
209 | puts(p); |
210 | } |
211 | |
6be88c14 |
212 | static void uline(char *p, size_t len, void *b) |
410c8acf |
213 | { |
410c8acf |
214 | if (!p) { |
215 | selbuf_destroy(b); |
216 | shutdown(fd, 1); |
217 | f |= f_uclose; |
218 | } else { |
6be88c14 |
219 | p[len] = '\n'; |
220 | write(fd, p, len + 1); |
410c8acf |
221 | } |
222 | } |
223 | |
9dd01bc6 |
224 | static void logfile(const char *name) |
225 | { |
fa4bef80 |
226 | FILE *fp; |
227 | |
228 | if ((fp = fopen(name, "a")) != 0) { |
229 | if (logfp) |
230 | fclose(logfp); |
231 | logfp = fp; |
232 | setvbuf(logfp, 0, _IOLBF, BUFSIZ); |
233 | } else { |
234 | dstr d = DSTR_INIT; |
235 | dstr_putf(&d, "error opening logfile `%s': %s", name, strerror(errno)); |
236 | if (logfp) |
237 | writelog("error", d.buf); |
238 | else if (logname) |
239 | die(EXIT_FAILURE, d.buf); |
240 | if (f & f_syslog) |
241 | syslog(LOG_ERR, d.buf); |
242 | dstr_destroy(&d); |
9dd01bc6 |
243 | } |
9dd01bc6 |
244 | } |
245 | |
5f5150b1 |
246 | static void sighup(int sig, void *v) |
247 | { |
248 | logfile(logname); |
249 | } |
250 | |
06b2a088 |
251 | static void cleanup(void) |
252 | { |
253 | if (pidfile) |
254 | unlink(pidfile); |
255 | } |
256 | |
257 | static void sigdie(int sig) |
258 | { |
259 | cleanup(); |
260 | signal(sig, SIG_DFL); |
261 | raise(sig); |
262 | } |
263 | |
410c8acf |
264 | static void version(FILE *fp) |
265 | { |
266 | pquis(fp, "$, TrIPE version " VERSION "\n"); |
267 | } |
268 | |
269 | static void usage(FILE *fp) |
270 | { |
271 | pquis(fp, "\ |
272 | Usage:\n\ |
273 | $ [-w] [-options] [command [args]...]\n\ |
274 | $ [-Dl] [-f file] [-options]\n\ |
275 | Options:\n\ |
06b2a088 |
276 | [-s] [-d directory] [-a socket] [-P pidfile]\n\ |
277 | [-p program] [-S arg,arg,...]\n\ |
410c8acf |
278 | "); |
279 | } |
280 | |
281 | static void help(FILE *fp) |
282 | { |
283 | version(fp); |
284 | fputc('\n', fp); |
285 | usage(fp); |
286 | fputs("\ |
287 | \n\ |
288 | Options in full:\n\ |
289 | \n\ |
290 | -h, --help Show this help text.\n\ |
291 | -v, --version Show version number.\n\ |
292 | -u, --usage Show brief usage message.\n\ |
293 | \n\ |
294 | -D, --daemon Become a background task after connecting.\n\ |
ef4a1ab7 |
295 | -d, --directory=DIR Select current directory [default " CONFIGDIR "].\n\ |
296 | -a, --admin-socket=FILE Select socket to connect to |
297 | [default " SOCKETDIR "/tripesock].\n\ |
06b2a088 |
298 | -P, --pidfile=FILE Write process-id to FILE.\n\ |
410c8acf |
299 | \n\ |
300 | -s, --spawn Start server rather than connecting.\n\ |
301 | -p, --spawn-path=PATH Specify path to executable.\n\ |
302 | -S, --spawn-args=ARGS Specify comma-separated arguments.\n\ |
303 | \n\ |
304 | -l, --syslog Log messages to system log.\n\ |
305 | -f, --logfile=FILE Log messages to FILE.\n\ |
306 | -w, --warnings Show warnings when running commands.\n\ |
307 | ", fp); |
308 | } |
309 | |
310 | int main(int argc, char *argv[]) |
311 | { |
ef4a1ab7 |
312 | const char *dir = CONFIGDIR; |
313 | const char *sock = SOCKETDIR "/tripesock"; |
410c8acf |
314 | const char *spawnpath = "tripe"; |
315 | string_v spawnopts = DA_INIT; |
316 | char *p; |
06b2a088 |
317 | FILE *pidfp = 0; |
410c8acf |
318 | |
319 | ego(argv[0]); |
320 | |
321 | if ((p = getenv("TRIPEDIR")) != 0) |
322 | dir = p; |
323 | |
324 | /* --- Parse the arguments --- */ |
325 | |
326 | for (;;) { |
327 | static const struct option opts[] = { |
328 | { "help", 0, 0, 'h' }, |
329 | { "version", 0, 0, 'v' }, |
330 | { "usage", 0, 0, 'u' }, |
331 | { "daemon", 0, 0, 'D' }, |
332 | { "directory", OPTF_ARGREQ, 0, 'd' }, |
333 | { "admin-socket", OPTF_ARGREQ, 0, 'a' }, |
334 | { "spawn", 0, 0, 's' }, |
335 | { "spawn-path", OPTF_ARGREQ, 0, 'p' }, |
336 | { "spawn-args", OPTF_ARGREQ, 0, 'S' }, |
337 | { "syslog", 0, 0, 'l' }, |
338 | { "logfile", OPTF_ARGREQ, 0, 'f' }, |
339 | { "warnings", 0, 0, 'w' }, |
06b2a088 |
340 | { "pidfile", OPTF_ARGREQ, 0, 'P' }, |
410c8acf |
341 | { 0, 0, 0, 0 } |
342 | }; |
343 | |
06b2a088 |
344 | int i = mdwopt(argc, argv, "+hvuDd:a:sp:S:lwf:nP:", opts, 0, 0, 0); |
410c8acf |
345 | if (i < 0) |
346 | break; |
347 | switch (i) { |
348 | case 'h': |
349 | help(stdout); |
350 | exit(0); |
351 | case 'v': |
352 | version(stdout); |
353 | exit(0); |
354 | case 'u': |
355 | usage(stdout); |
356 | exit(0); |
357 | case 'D': |
358 | f |= f_daemon | f_noinput; |
359 | break; |
360 | case 'd': |
361 | dir = optarg; |
362 | break; |
363 | case 'a': |
364 | sock = optarg; |
365 | break; |
366 | case 's': |
367 | f |= f_spawn; |
368 | break; |
369 | case 'p': |
370 | f |= f_spawn; |
371 | spawnpath = optarg; |
372 | break; |
373 | case 'S': |
374 | f |= f_spawn | f_spawnopts; |
375 | for (p = strtok(optarg, ","); p; p = strtok(0, ",")) |
376 | DA_PUSH(&spawnopts, p); |
377 | break; |
378 | case 'l': |
379 | f |= f_syslog | f_noinput; |
380 | break; |
381 | case 'w': |
382 | f |= f_warn; |
383 | break; |
384 | case 'f': |
9dd01bc6 |
385 | logname = optarg; |
410c8acf |
386 | f |= f_noinput; |
387 | break; |
06b2a088 |
388 | case 'P': |
389 | pidfile = optarg; |
390 | break; |
410c8acf |
391 | default: |
392 | f |= f_bogus; |
393 | break; |
394 | } |
395 | } |
396 | if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) { |
397 | usage(stderr); |
398 | exit(EXIT_FAILURE); |
399 | } |
400 | |
06b2a088 |
401 | /* --- Set various things up --- */ |
402 | |
403 | if (chdir(dir)) { |
404 | die(EXIT_FAILURE, "couldn't set `%s' as current directory: %s", |
405 | dir, strerror(errno)); |
406 | } |
407 | if (logname) |
408 | logfile(logname); |
409 | if (!pidfile && (f & f_daemon) && ((f & f_syslog) || logname)) |
410 | pidfile = "tripectl.pid"; |
411 | if (pidfile && (pidfp = fopen(pidfile, "w")) == 0) { |
412 | die(EXIT_FAILURE, "couldn't open `%s' for writing: %s", |
413 | pidfile, strerror(errno)); |
414 | } |
415 | signal(SIGINT, sigdie); |
416 | signal(SIGQUIT, sigdie); |
417 | signal(SIGTERM, sigdie); |
418 | atexit(cleanup); |
419 | |
410c8acf |
420 | /* --- Connect to the server --- */ |
421 | |
422 | if (f & f_spawn) { |
423 | int pfd[2]; |
424 | pid_t kid; |
425 | struct sigaction sa; |
426 | sigset_t newmask, oldmask; |
427 | |
428 | sa.sa_handler = reap; |
429 | sigemptyset(&sa.sa_mask); |
430 | sa.sa_flags = SA_NOCLDSTOP; |
431 | #ifdef SA_RESTART |
432 | sa.sa_flags |= SA_RESTART; |
433 | #endif |
434 | sigaction(SIGCHLD, &sa, 0); |
435 | |
06b2a088 |
436 | DA_UNSHIFT(&spawnopts, (char *)sock); |
437 | DA_UNSHIFT(&spawnopts, "-a"); |
438 | DA_UNSHIFT(&spawnopts, "-d."); |
d071d798 |
439 | DA_UNSHIFT(&spawnopts, (char *)spawnpath); |
410c8acf |
440 | DA_PUSH(&spawnopts, 0); |
441 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd)) |
442 | die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno)); |
443 | sigemptyset(&newmask); |
444 | sigaddset(&newmask, SIGCHLD); |
445 | sigprocmask(SIG_BLOCK, &newmask, &oldmask); |
446 | if ((kid = fork()) < 0) |
447 | die(EXIT_FAILURE, "fork failed: %s", strerror(errno)); |
448 | if (!kid) { |
449 | dup2(pfd[1], STDIN_FILENO); |
450 | dup2(pfd[1], STDOUT_FILENO); |
451 | close(pfd[1]); |
452 | close(pfd[0]); |
9dd01bc6 |
453 | if (logfp) |
454 | fclose(logfp); |
7ed14135 |
455 | if (pidfp) |
456 | fclose(pidfp); |
9dd01bc6 |
457 | closelog(); |
06b2a088 |
458 | if (f & f_daemon) |
459 | u_detach(); |
410c8acf |
460 | execvp(DA(&spawnopts)[0], DA(&spawnopts)); |
461 | die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno)); |
462 | } |
463 | sigprocmask(SIG_SETMASK, &oldmask, 0); |
464 | fd = pfd[0]; |
465 | close(pfd[1]); |
466 | } else { |
467 | struct sockaddr_un sun; |
468 | size_t sz = strlen(sock) + 1; |
06b2a088 |
469 | if (sz > sizeof(sun.sun_path)) |
410c8acf |
470 | die(EXIT_FAILURE, "socket name `%s' too long", sock); |
471 | memset(&sun, 0, sizeof(sun)); |
472 | sun.sun_family = AF_UNIX; |
06b2a088 |
473 | memcpy(sun.sun_path, sock, sz); |
474 | sz = sz + offsetof(struct sockaddr_un, sun_path); |
410c8acf |
475 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
476 | die(EXIT_FAILURE, "error making socket: %s", strerror(errno)); |
477 | if (connect(fd, (struct sockaddr *)&sun, sz)) { |
478 | die(EXIT_FAILURE, "error connecting to `%s': %s", |
9dd01bc6 |
479 | sun.sun_path, strerror(errno)); |
410c8acf |
480 | } |
481 | } |
482 | |
483 | if (f & f_daemon) { |
484 | if (u_daemon()) |
485 | die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno)); |
486 | } |
06b2a088 |
487 | if (pidfp) { |
a803add7 |
488 | fprintf(pidfp, "%li\n", (long)getpid()); |
06b2a088 |
489 | fclose(pidfp); |
490 | } |
410c8acf |
491 | |
492 | /* --- If we're meant to be interactive, do that --- */ |
493 | |
494 | if (!(f & f_noinput) && optind == argc) { |
495 | sel_state sel; |
496 | selbuf bu, bs; |
497 | |
498 | sel_init(&sel); |
499 | selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu); |
500 | selbuf_init(&bs, &sel, fd, sline, &bs); |
501 | for (;;) { |
5f5150b1 |
502 | if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) |
410c8acf |
503 | die(EXIT_FAILURE, "select failed: %s", strerror(errno)); |
504 | } |
505 | } |
506 | |
507 | /* --- If there's a command, submit it --- */ |
508 | |
509 | if (optind < argc) { |
510 | dstr d = DSTR_INIT; |
511 | dstr_puts(&d, argv[optind++]); |
512 | while (optind < argc) { |
513 | dstr_putc(&d, ' '); |
514 | dstr_puts(&d, argv[optind++]); |
515 | } |
516 | dstr_putc(&d, '\n'); |
517 | write(fd, d.buf, d.len); |
518 | shutdown(fd, 1); |
519 | dstr_destroy(&d); |
520 | f |= f_command; |
521 | } |
522 | |
523 | /* --- Pull everything else out of the box --- */ |
524 | |
525 | { |
5f5150b1 |
526 | sel_state sel; |
527 | selbuf b; |
528 | sig hup; |
529 | |
530 | sel_init(&sel); |
531 | selbuf_init(&b, &sel, fd, cline, 0); |
532 | |
410c8acf |
533 | if (f & f_syslog) |
534 | openlog(QUIS, 0, LOG_DAEMON); |
9dd01bc6 |
535 | if (logfp) { |
5f5150b1 |
536 | sig_init(&sel); |
537 | sig_add(&hup, SIGHUP, sighup, 0); |
9dd01bc6 |
538 | } |
410c8acf |
539 | for (;;) { |
5f5150b1 |
540 | if (sel_select(&sel) && errno != EINTR && errno != EAGAIN) |
541 | die(EXIT_FAILURE, "select failed: %s", strerror(errno)); |
410c8acf |
542 | } |
410c8acf |
543 | } |
544 | |
545 | return (0); |
546 | } |
547 | |
548 | /*----- That's all, folks -------------------------------------------------*/ |