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