chiark / gitweb /
Initial checkin.
[tripe] / client.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
3 * $Id: client.c,v 1.1 2001/02/03 20:26:37 mdw Exp $
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 $
32 * Revision 1.1 2001/02/03 20:26:37 mdw
33 * Initial checkin.
34 *
35 */
36
37/*----- Header files ------------------------------------------------------*/
38
39#include <ctype.h>
40#include <errno.h>
41#include <signal.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <time.h>
46
47#include <sys/types.h>
48#include <sys/time.h>
49#include <unistd.h>
50#include <fcntl.h>
51#include <sys/wait.h>
52#include <syslog.h>
53
54#include <sys/socket.h>
55#include <sys/un.h>
56#include <arpa/inet.h>
57#include <netdb.h>
58
59#include <mLib/alloc.h>
60#include <mLib/darray.h>
61#include <mLib/dstr.h>
62#include <mLib/lbuf.h>
63#include <mLib/mdwopt.h>
64#include <mLib/quis.h>
65#include <mLib/report.h>
66#include <mLib/sel.h>
67#include <mLib/selbuf.h>
68#include <mLib/str.h>
69
70#include "util.h"
71
72#undef sun
73
74/*----- Data structures ---------------------------------------------------*/
75
76#ifndef STRING_V
77# define STRING_V
78 DA_DECL(string_v, char *);
79#endif
80
81/*----- Static variables --------------------------------------------------*/
82
83static FILE *logfp = 0;
84static unsigned f = 0;
85static int fd;
86
87#define f_bogus 1u
88#define f_spawn 2u
89#define f_daemon 4u
90#define f_spawnopts 8u
91#define f_syslog 16u
92#define f_command 32u
93#define f_noinput 64u
94#define f_warn 128u
95#define f_uclose 256u
96
97/*----- Main code ---------------------------------------------------------*/
98
99static void reap(int sig)
100{
101 int s;
102 int e = errno;
103 while (waitpid(-1, &s, WNOHANG) > 0)
104 ;
105 errno = e;
106}
107
108static void writelog(const char *cat, const char *msg)
109{
110 char buf[256];
111 time_t t = time(0);
112 struct tm *tm = localtime(&t);
113 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
114 fprintf(logfp, "%s %s: %s\n", buf, cat, msg);
115}
116
117static void cline(char *p, void *b)
118{
119 char *q;
120 if (!p) {
121 if (f & f_command)
122 die(EXIT_FAILURE, "server dropped the connection");
123 exit(0);
124 }
125 q = str_getword(&p);
126 if (!q)
127 return;
128 if (strcmp(q, "WARN") == 0) {
129 if (f & f_syslog)
130 syslog(LOG_WARNING, "%s", p);
131 if (logfp)
132 writelog("warning", p);
133 if (f & f_warn)
134 fprintf(stderr, "Warning: %s\n", p);
135 } else if (strcmp(q, "TRACE") == 0) {
136 if (f & f_syslog)
137 syslog(LOG_DEBUG, "%s", p);
138 if (logfp)
139 writelog("debug", p);
140 } else if (!(f & f_command)) {
141 if (f & f_syslog)
142 syslog(LOG_ERR, "unexpected output `%s %s'", q, p);
143 if (logfp) {
144 dstr d = DSTR_INIT;
145 dstr_putf(&d, "unexpected output `%s %s'", q, p);
146 writelog("error", d.buf);
147 dstr_destroy(&d);
148 }
149 } else if (strcmp(q, "ERR") == 0)
150 die(EXIT_FAILURE, "%s", p);
151 else if (strcmp(q, "INFO") == 0)
152 puts(p);
153 else if (strcmp(q, "OK") == 0)
154 exit(0);
155 else
156 die(EXIT_FAILURE, "unexpected output `%s %s'", q, p);
157}
158
159static void sline(char *p, void *b)
160{
161 if (!p) {
162 if (!(f & f_uclose))
163 moan("server closed the connection");
164 exit(0);
165 }
166 puts(p);
167}
168
169static void uline(char *p, void *b)
170{
171 size_t sz;
172 if (!p) {
173 selbuf_destroy(b);
174 shutdown(fd, 1);
175 f |= f_uclose;
176 } else {
177 sz = strlen(p);
178 p[sz] = '\n';
179 write(fd, p, sz + 1);
180 }
181}
182
183static void version(FILE *fp)
184{
185 pquis(fp, "$, TrIPE version " VERSION "\n");
186}
187
188static void usage(FILE *fp)
189{
190 pquis(fp, "\
191Usage:\n\
192 $ [-w] [-options] [command [args]...]\n\
193 $ [-Dl] [-f file] [-options]\n\
194Options:\n\
195 [-s] [-d directory] [-a socket] [-p program] [-S arg,arg,...]\n\
196");
197}
198
199static void help(FILE *fp)
200{
201 version(fp);
202 fputc('\n', fp);
203 usage(fp);
204 fputs("\
205\n\
206Options in full:\n\
207\n\
208-h, --help Show this help text.\n\
209-v, --version Show version number.\n\
210-u, --usage Show brief usage message.\n\
211\n\
212-D, --daemon Become a background task after connecting.\n\
213-d, --directory=DIR Select current directory [default /var/lib/tripe]\n\
214-a, --admin-socket=FILE Select socket to connect to.\n\
215\n\
216-s, --spawn Start server rather than connecting.\n\
217-p, --spawn-path=PATH Specify path to executable.\n\
218-S, --spawn-args=ARGS Specify comma-separated arguments.\n\
219\n\
220-l, --syslog Log messages to system log.\n\
221-f, --logfile=FILE Log messages to FILE.\n\
222-w, --warnings Show warnings when running commands.\n\
223", fp);
224}
225
226int main(int argc, char *argv[])
227{
228 const char *dir = "/var/lib/tripe";
229 const char *sock = "tripesock";
230 const char *spawnpath = "tripe";
231 string_v spawnopts = DA_INIT;
232 char *p;
233
234 ego(argv[0]);
235
236 if ((p = getenv("TRIPEDIR")) != 0)
237 dir = p;
238
239 /* --- Parse the arguments --- */
240
241 for (;;) {
242 static const struct option opts[] = {
243 { "help", 0, 0, 'h' },
244 { "version", 0, 0, 'v' },
245 { "usage", 0, 0, 'u' },
246 { "daemon", 0, 0, 'D' },
247 { "directory", OPTF_ARGREQ, 0, 'd' },
248 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
249 { "spawn", 0, 0, 's' },
250 { "spawn-path", OPTF_ARGREQ, 0, 'p' },
251 { "spawn-args", OPTF_ARGREQ, 0, 'S' },
252 { "syslog", 0, 0, 'l' },
253 { "logfile", OPTF_ARGREQ, 0, 'f' },
254 { "warnings", 0, 0, 'w' },
255 { 0, 0, 0, 0 }
256 };
257
258 int i = mdwopt(argc, argv, "hvuDd:a:sp:S:lwf:n", opts, 0, 0, 0);
259 if (i < 0)
260 break;
261 switch (i) {
262 case 'h':
263 help(stdout);
264 exit(0);
265 case 'v':
266 version(stdout);
267 exit(0);
268 case 'u':
269 usage(stdout);
270 exit(0);
271 case 'D':
272 f |= f_daemon | f_noinput;
273 break;
274 case 'd':
275 dir = optarg;
276 break;
277 case 'a':
278 sock = optarg;
279 break;
280 case 's':
281 f |= f_spawn;
282 break;
283 case 'p':
284 f |= f_spawn;
285 spawnpath = optarg;
286 break;
287 case 'S':
288 f |= f_spawn | f_spawnopts;
289 for (p = strtok(optarg, ","); p; p = strtok(0, ","))
290 DA_PUSH(&spawnopts, p);
291 break;
292 case 'l':
293 f |= f_syslog | f_noinput;
294 break;
295 case 'w':
296 f |= f_warn;
297 break;
298 case 'f':
299 if (logfp)
300 fclose(logfp);
301 if ((logfp = fopen(optarg, "a")) == 0) {
302 die(EXIT_FAILURE, "error opening logfile `%s': %s",
303 optarg, strerror(errno));
304 }
305 f |= f_noinput;
306 break;
307 default:
308 f |= f_bogus;
309 break;
310 }
311 }
312 if ((f & f_bogus) || ((f & f_noinput) && optind < argc)) {
313 usage(stderr);
314 exit(EXIT_FAILURE);
315 }
316
317 /* --- Set the world up --- */
318
319 if (chdir(dir)) {
320 die(EXIT_FAILURE, "couldn't set current directory to `%s': %s",
321 dir, strerror(errno));
322 }
323
324 /* --- Connect to the server --- */
325
326 if (f & f_spawn) {
327 int pfd[2];
328 pid_t kid;
329 struct sigaction sa;
330 sigset_t newmask, oldmask;
331
332 sa.sa_handler = reap;
333 sigemptyset(&sa.sa_mask);
334 sa.sa_flags = SA_NOCLDSTOP;
335#ifdef SA_RESTART
336 sa.sa_flags |= SA_RESTART;
337#endif
338 sigaction(SIGCHLD, &sa, 0);
339
340 DA_UNSHIFT(&spawnopts, (char *)spawnpath);
341 if (!(f & f_spawnopts)) {
342 DA_PUSH(&spawnopts, "-d.");
343 DA_PUSH(&spawnopts, "-a");
344 DA_PUSH(&spawnopts, (char *)sock);
345 }
346 DA_PUSH(&spawnopts, 0);
347 if (socketpair(PF_UNIX, SOCK_STREAM, 0, pfd))
348 die(EXIT_FAILURE, "error from socketpair: %s", strerror(errno));
349 sigemptyset(&newmask);
350 sigaddset(&newmask, SIGCHLD);
351 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
352 if ((kid = fork()) < 0)
353 die(EXIT_FAILURE, "fork failed: %s", strerror(errno));
354 if (!kid) {
355 dup2(pfd[1], STDIN_FILENO);
356 dup2(pfd[1], STDOUT_FILENO);
357 close(pfd[1]);
358 close(pfd[0]);
359 execvp(DA(&spawnopts)[0], DA(&spawnopts));
360 die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno));
361 }
362 sigprocmask(SIG_SETMASK, &oldmask, 0);
363 fd = pfd[0];
364 close(pfd[1]);
365 } else {
366 struct sockaddr_un sun;
367 size_t sz = strlen(sock) + 1;
368 if (sz > sizeof(sun.sun_path))
369 die(EXIT_FAILURE, "socket name `%s' too long", sock);
370 memset(&sun, 0, sizeof(sun));
371 sun.sun_family = AF_UNIX;
372 memcpy(sun.sun_path, sock, sz);
373 sz += offsetof(struct sockaddr_un, sun_path);
374 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
375 die(EXIT_FAILURE, "error making socket: %s", strerror(errno));
376 if (connect(fd, (struct sockaddr *)&sun, sz)) {
377 die(EXIT_FAILURE, "error connecting to `%s': %s",
378 sock, strerror(errno));
379 }
380 }
381
382 if (f & f_daemon) {
383 if (u_daemon())
384 die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
385 }
386
387 /* --- If we're meant to be interactive, do that --- */
388
389 if (!(f & f_noinput) && optind == argc) {
390 sel_state sel;
391 selbuf bu, bs;
392
393 sel_init(&sel);
394 selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu);
395 selbuf_init(&bs, &sel, fd, sline, &bs);
396 for (;;) {
397 if (sel_select(&sel))
398 die(EXIT_FAILURE, "select failed: %s", strerror(errno));
399 }
400 }
401
402 /* --- If there's a command, submit it --- */
403
404 if (optind < argc) {
405 dstr d = DSTR_INIT;
406 dstr_puts(&d, argv[optind++]);
407 while (optind < argc) {
408 dstr_putc(&d, ' ');
409 dstr_puts(&d, argv[optind++]);
410 }
411 dstr_putc(&d, '\n');
412 write(fd, d.buf, d.len);
413 shutdown(fd, 1);
414 dstr_destroy(&d);
415 f |= f_command;
416 }
417
418 /* --- Pull everything else out of the box --- */
419
420 {
421 lbuf b;
422 lbuf_init(&b, cline, 0);
423 if (f & f_syslog)
424 openlog(QUIS, 0, LOG_DAEMON);
425 for (;;) {
426 size_t sz = lbuf_free(&b, &p);
427 ssize_t n = read(fd, p, sz);
428 if (n < 0)
429 die(EXIT_FAILURE, "read failed: %s", strerror(errno));
430 if (n == 0)
431 break;
432 lbuf_flush(&b, p, n);
433 }
434 lbuf_close(&b);
435 }
436
437 return (0);
438}
439
440/*----- That's all, folks -------------------------------------------------*/