chiark / gitweb /
build, debian: Add a suffix to the main TrIPE-specific manpages.
[tripe] / proxy / tripe-mitm.c
CommitLineData
50c74957 1/* -*-c-*-
2 *
50c74957 3 * An evil proxy for TrIPE
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
50c74957 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
e04c2d50 16 *
50c74957 17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
e04c2d50 21 *
50c74957 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
50c74957 27/*----- Header files ------------------------------------------------------*/
28
29#include "config.h"
30
31#include <assert.h>
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <time.h>
37
38#include <sys/types.h>
39#include <sys/time.h>
40#include <unistd.h>
41#include <fcntl.h>
42
43#include <sys/socket.h>
44#include <netinet/in.h>
45#include <arpa/inet.h>
46#include <netdb.h>
47
48#include <mLib/alloc.h>
49#include <mLib/dstr.h>
50#include <mLib/fdflags.h>
51#include <mLib/mdwopt.h>
52#include <mLib/quis.h>
53#include <mLib/report.h>
54#include <mLib/sel.h>
55#include <mLib/sub.h>
56#include <mLib/tv.h>
57
c953f4b5 58#include <catacomb/buf.h>
59
50c74957 60#include <catacomb/key.h>
61
62#include <catacomb/mp.h>
50c74957 63#include <catacomb/mprand.h>
64#include <catacomb/dh.h>
65
66#include <catacomb/noise.h>
67#include <catacomb/rand.h>
68#include <catacomb/rc4.h>
69
23df0e5b
MW
70#include "util.h"
71
50c74957 72/*----- Data structures ---------------------------------------------------*/
73
74typedef struct peer {
75 sel_file sf;
76 dh_pub kpub;
77 const char *name;
78 struct filter *f;
79} peer;
80
81typedef struct filter {
82 struct filter *next;
83 peer *p_from, *p_to;
84 void (*func)(struct filter */*f*/, const octet */*buf*/, size_t /*sz*/);
85 void *state;
86} filter;
87
88typedef struct qnode {
89 octet *buf;
90 size_t sz;
91} qnode;
92
93/*----- Static variables --------------------------------------------------*/
94
95#define PKBUFSZ 65536
96
97static sel_state sel;
98static peer peers[2];
99static unsigned npeer = 0;
100static key_file keys;
101static grand *rng;
102
103#define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
104#define RND(i) (rng->ops->range(rng, (i)))
105
106/*----- Peer management ---------------------------------------------------*/
107
108static void dopacket(int fd, unsigned mode, void *vv)
109{
110 octet buf[PKBUFSZ];
111 peer *p = vv;
112 int r = read(fd, buf, sizeof(buf));
113 if (r >= 0) {
114 printf("recv from `%s'\n", p->name);
115 PASS(p->f, buf, r);
116 }
117}
118
119static void addpeer(unsigned ac, char **av)
120{
121 key_packstruct kps[DH_PUBFETCHSZ];
122 key_packdef *kp;
123 struct hostent *h;
124 struct sockaddr_in sin;
125 int len = PKBUFSZ;
126 peer *p;
127 int fd;
128 int e;
129
130 if (ac != 4)
131 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
132 if (npeer >= 2)
133 die(1, "enough peers already");
134 p = &peers[npeer++];
135 p->name = xstrdup(av[0]);
136 kp = key_fetchinit(dh_pubfetch, kps, &p->kpub);
137 e = key_fetchbyname(kp, &keys, av[0]);
138 key_fetchdone(kp);
139 if (e)
140 die(1, "key_fetch `%s': %s", av[0], key_strerror(e));
141 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
142 die(1, "socket: %s", strerror(errno));
143 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
144 memset(&sin, 0, sizeof(sin));
145 sin.sin_family = AF_INET;
146 sin.sin_addr.s_addr = INADDR_ANY;
147 sin.sin_port = htons(atoi(av[1]));
148 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
149 die(1, "bind: %s", strerror(errno));
150 memset(&sin, 0, sizeof(sin));
151 sin.sin_family = AF_INET;
152 if ((h = gethostbyname(av[2])) == 0)
153 die(1, "gethostbyname `%s'", av[2]);
154 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
155 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)))
156 die(1, "setsockopt: %s", strerror(errno));
157 memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr));
158 sin.sin_port = htons(atoi(av[3]));
159 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)))
160 die(1, "connect: %s", strerror(errno));
161 sel_initfile(&sel, &p->sf, fd, SEL_READ, dopacket, p);
162 sel_addfile(&p->sf);
163}
164
165/*----- Fork filter -------------------------------------------------------*/
166
167typedef struct forknode {
168 struct forknode *next;
169 filter *f;
170} forknode;
171
172typedef struct forkfilt {
173 const char *name;
174 forknode *fn;
175} forkfilt;
176
177static void dofork(filter *f, const octet *buf, size_t sz)
178{
179 forkfilt *ff = f->state;
180 forknode *fn;
181 unsigned i = 0;
182
183 ff = f->state;
184 for (fn = ff->fn; fn; fn = fn->next) {
185 printf("fork branch %u of fork `%s'\n", i++, ff->name);
186 PASS(fn->f, buf, sz);
187 }
188 printf("fork branch %u of fork `%s'\n", i++, ff->name);
189 PASS(f->next, buf, sz);
190}
191
192static void addfork(filter *f, unsigned ac, char **av)
193{
194 forkfilt *ff;
195 if (ac != 1)
196 die(1, "syntax: filt:fork:NAME");
197 ff = CREATE(forkfilt);
198 ff->name = xstrdup(av[0]);
199 ff->fn = 0;
200 f->func = dofork;
201 f->state = ff;
202}
203
204static void nextfork(unsigned ac, char **av)
205{
c953f4b5 206 unsigned i, j;
50c74957 207 filter *f;
208 forkfilt *ff;
209 forknode *fn, **ffn;
210 peer *p;
211
c953f4b5 212 if (ac < 1)
213 die(1, "syntax: next:NAME:...");
50c74957 214 for (i = 0; i < 2; i++) {
215 p = &peers[i];
216 for (f = p->f; f; f = f->next) {
217 if (f->func != dofork)
218 continue;
219 ff = f->state;
c953f4b5 220 for (j = 0; j < ac; j++) {
221 if (strcmp(av[j], ff->name) == 0)
222 goto match;
223 }
224 continue;
225 match:
50c74957 226 fn = CREATE(forknode);
227 for (ffn = &ff->fn; *ffn; ffn = &(*ffn)->next)
228 ;
229 fn->f = f->next;
230 f->next = 0;
231 fn->next = 0;
232 *ffn = fn;
233 }
234 }
235}
236
237/*----- Corrupt filter ----------------------------------------------------*/
238
239typedef struct corrupt {
240 unsigned p_corrupt;
241} corrupt;
242
243static void docorrupt(filter *f, const octet *buf, size_t sz)
244{
245 corrupt *c = f->state;
246 octet b[PKBUFSZ];
247 memcpy(b, buf, sz);
248
249 while (!RND(c->p_corrupt)) {
250 puts("corrupt packet");
251 b[RND(sz)] ^= RND(256);
252 }
253 PASS(f->next, b, sz);
254}
255
256static void addcorrupt(filter *f, unsigned ac, char **av)
257{
258 corrupt *c;
259 if (ac > 1)
a2e06ed5 260 die(1, "syntax: filt:corrupt[:P-CORRUPT]");
50c74957 261 c = CREATE(corrupt);
262 if (ac > 0)
263 c->p_corrupt = atoi(av[0]);
264 else
265 c->p_corrupt = 5;
266 f->state = c;
267 f->func = docorrupt;
268}
269
a2e06ed5
MW
270/*----- Drop filter -------------------------------------------------------*/
271
272typedef struct drop {
273 unsigned p_drop;
274} drop;
275
276static void dodrop(filter *f, const octet *buf, size_t sz)
277{
278 drop *d = f->state;
279
280 if (!RND(d->p_drop))
281 puts("drop packet");
282 else
283 PASS(f->next, buf, sz);
284}
285
286static void adddrop(filter *f, unsigned ac, char **av)
287{
288 drop *d;
289 if (ac > 1)
290 die(1, "syntax: filt:drop[:P-DROP]");
291 d = CREATE(drop);
292 if (ac > 0)
293 d->p_drop = atoi(av[0]);
294 else
295 d->p_drop = 5;
296 f->state = d;
297 f->func = dodrop;
298}
299
50c74957 300/*----- Delay filter ------------------------------------------------------*/
301
302typedef struct delaynode {
303 unsigned flag;
304 sel_timer tm;
305 unsigned i;
306 struct delay *d;
307 octet *buf;
308 size_t sz;
309 unsigned seq;
310} delaynode;
311
312typedef struct delay {
313 unsigned max, n;
314 unsigned long t;
315 unsigned p_replay;
316 filter *f;
317 delaynode *q;
318} delay;
319
320static void dtimer(struct timeval *tv, void *vv);
321
322static void dinsert(delaynode *dn)
323{
324 struct timeval tv;
325 sel_timer *ta, *tb;
326 unsigned long tdelta = RND(dn->d->t);
327 gettimeofday(&tv, 0);
328 TV_ADDL(&tv, &tv, 0, tdelta);
329 assert(!dn->flag);
330 sel_addtimer(&sel, &dn->tm, &tv, dtimer, dn);
331 dn->flag = 1;
332 for (ta = tb = sel.timers; ta; ta = ta->next) {
333 ta = ta->next; if (!ta) break; assert(ta != tb);
334 ta = ta->next; if (!ta) break; assert(ta != tb);
335 tb = tb->next;
336 }
337 printf(" delay %lu usecs", tdelta);
338}
339
340static void dsend(delaynode *dn, unsigned force)
341{
342 delay *d = dn->d;
343 delaynode *ddn;
344 fputs(" send...\n", stdout);
345 assert(dn->buf);
346 PASS(d->f->next, dn->buf, dn->sz);
347 fputs("delay ...", stdout);
348 if (!force)
349 dinsert(dn);
350 else {
351 xfree(dn->buf);
352 dn->buf = 0;
353 d->n--;
354 if (dn->i < d->n) {
355 ddn = &d->q[d->n];
356 sel_rmtimer(&ddn->tm);
357 sel_addtimer(&sel, &dn->tm, &ddn->tm.tv, dtimer, dn);
358 dn->flag = 1;
359 dn->buf = ddn->buf;
360 dn->sz = ddn->sz;
361 dn->seq = ddn->seq;
362 ddn->buf = 0;
363 ddn->flag = 0;
364 printf(" move id %u from slot %u to slot %u", ddn->seq, ddn->i, dn->i);
365 }
366 { unsigned i; for (i = 0; i < d->n; i++) assert(d->q[i].buf); }
367 fputs(" remove", stdout);
368 }
369}
370
371static void dtimer(struct timeval *tv, void *vv)
372{
373 delaynode *dn = vv;
374 printf("delay timer peer `%s' id %u slot %u",
375 dn->d->f->p_from->name, dn->seq, dn->i);
376 dn->flag = 0;
377 dsend(dn, RND(dn->d->p_replay));
378 fputc('\n', stdout);
379}
380
381static void dodelay(filter *f, const octet *buf, size_t sz)
382{
383 delay *d = f->state;
384 delaynode *dn;
385 static unsigned seq = 0;
386
387 fputs("delay", stdout);
388 if (d->n == d->max) {
389 dn = &d->q[RND(d->n)];
390 printf(" force uid %u", dn->seq);
391 sel_rmtimer(&dn->tm);
392 dn->flag = 0;
393 dsend(dn, 1);
394 fputc(';', stdout);
395 }
396 dn = &d->q[d->n++];
397 dn->seq = seq++;
398 printf(" new id %u in slot %u", dn->seq, dn->i);
399 dn->buf = xmalloc(sz);
400 dn->sz = sz;
401 memcpy(dn->buf, buf, sz);
402 dinsert(dn);
403 fputc('\n', stdout);
404}
405
406static void adddelay(filter *f, unsigned ac, char **av)
407{
408 delay *d;
409 unsigned i;
410
411 if (ac < 1 || ac > 3)
a2e06ed5 412 die(1, "syntax: filt:delay:QLEN[:MILLIS:P-REPLAY]");
50c74957 413 d = CREATE(delay);
414 d->max = atoi(av[0]);
415 if (ac > 1)
416 d->t = strtoul(av[1], 0, 10);
417 else
418 d->t = 100;
419 d->t *= 1000;
420 if (ac > 2)
421 d->p_replay = atoi(av[2]);
422 else
423 d->p_replay = 20;
424 d->n = 0;
425 d->q = xmalloc(d->max * sizeof(delaynode));
426 d->f = f;
427 f->state = d;
428 f->func = dodelay;
429 for (i = 0; i < d->max; i++) {
430 d->q[i].d = d;
431 d->q[i].i = i;
432 d->q[i].buf = 0;
433 d->q[i].flag = 0;
434 }
435}
436
437/*----- Filters -----------------------------------------------------------*/
438
439static void dosend(filter *f, const octet *buf, size_t sz)
440{
441 printf("send to `%s'\n", f->p_to->name);
99735357 442 DISCARD(write(f->p_to->sf.fd, buf, sz));
50c74957 443}
444
445static void addsend(filter *f, unsigned ac, char **av)
446{
447 if (ac)
448 die(1, "syntax: filt:send");
449 f->func = dosend;
450}
451
452const struct filtab {
453 const char *name;
454 void (*func)(filter */*f*/, unsigned /*ac*/, char **/*av*/);
455} filtab[] = {
456 { "send", addsend },
457 { "fork", addfork },
458 { "delay", adddelay },
a2e06ed5 459 { "drop", adddrop },
50c74957 460 { "corrupt", addcorrupt },
461 { 0, 0 }
462};
463
464static void dofilter(peer *from, peer *to, unsigned ac, char **av)
465{
466 filter **ff, *f = CREATE(filter);
467 const struct filtab *ft;
468 if (ac < 1)
469 die(1, "syntax: {l,r,}filt:NAME:...");
470 f->next = 0;
471 f->p_from = from;
472 f->p_to = to;
473 f->state = 0;
474 for (ff = &from->f; *ff; ff = &(*ff)->next)
475 ;
476 *ff = f;
477 for (ft = filtab; ft->name; ft++) {
478 if (strcmp(av[0], ft->name) == 0) {
479 ft->func(f, ac - 1, av + 1);
480 return;
481 }
482 }
483 die(1, "unknown filter `%s'", av[0]);
484}
485
486/*----- Flooding ----------------------------------------------------------*/
487
488typedef struct flood {
489 peer *p;
490 unsigned type;
491 size_t sz;
492 unsigned long t;
493 sel_timer tm;
494} flood;
495
496static void setflood(flood *f);
497
498static void floodtimer(struct timeval *tv, void *vv)
499{
500 flood *f = vv;
501 octet buf[PKBUFSZ];
502 size_t sz;
503
504 sz = RND(f->sz);
505 sz += RND(f->sz);
506 sz += RND(f->sz);
507 sz += RND(f->sz);
508 sz /= 2;
509
510 rng->ops->fill(rng, buf, sz);
511 if (f->type < 0x100)
512 buf[0] = f->type;
513 puts("flood packet");
514 PASS(f->p->f, buf, sz);
515 setflood(f);
516}
e04c2d50 517
50c74957 518static void setflood(flood *f)
519{
520 struct timeval tv;
521 gettimeofday(&tv, 0);
522 TV_ADDL(&tv, &tv, 0, RND(f->t));
523 sel_addtimer(&sel, &f->tm, &tv, floodtimer, f);
524}
525
526static void doflood(peer *p, unsigned ac, char **av)
527{
528 flood *f;
529 if (ac > 3)
530 die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
531 f = CREATE(flood);
532 f->p = p;
533 if (ac > 0)
534 f->type = strtoul(av[0], 0, 16);
535 else
536 f->type = 0x100;
537 if (ac > 1)
538 f->t = atoi(av[1]);
539 else
540 f->t = 10;
541 if (ac > 2)
542 f->sz = atoi(av[2]);
543 else
544 f->sz = 128;
545 f->t *= 1000;
546 setflood(f);
547}
548
549/*----- Configuration commands --------------------------------------------*/
550
551static void parse(char *p);
552
553static void addflood(unsigned ac, char **av) {
554 doflood(&peers[0], ac, av);
555 doflood(&peers[1], ac, av);
556}
557static void addlflood(unsigned ac, char **av) {
558 doflood(&peers[0], ac, av);
559}
560static void addrflood(unsigned ac, char **av) {
561 doflood(&peers[1], ac, av);
562}
563
564static void addfilter(unsigned ac, char **av) {
565 dofilter(&peers[0], &peers[1], ac, av);
566 dofilter(&peers[1], &peers[0], ac, av);
567}
568static void addlfilter(unsigned ac, char **av) {
569 dofilter(&peers[0], &peers[1], ac, av);
570}
571static void addrfilter(unsigned ac, char **av) {
572 dofilter(&peers[1], &peers[0], ac, av);
573}
574
575static void include(unsigned ac, char **av)
576{
577 FILE *fp;
578 dstr d = DSTR_INIT;
579 if (!ac)
580 die(1, "syntax: include:FILE:...");
581 while (*av) {
582 if ((fp = fopen(*av, "r")) == 0)
583 die(1, "fopen `%s': %s", *av, strerror(errno));
584 while (dstr_putline(&d, fp) != EOF) {
585 parse(d.buf);
586 DRESET(&d);
587 }
588 fclose(fp);
589 av++;
590 }
591}
592
593const struct cmdtab {
594 const char *name;
595 void (*func)(unsigned /*ac*/, char **/*av*/);
596} cmdtab[] = {
597 { "peer", addpeer },
598 { "include", include },
599 { "filt", addfilter },
600 { "lfilt", addlfilter },
601 { "rfilt", addrfilter },
602 { "next", nextfork },
603 { "flood", addflood },
604 { "lflood", addlflood },
605 { "rflood", addrflood },
606 { 0, 0 }
607};
608
609#define AVMAX 16
610
611static void parse(char *p)
612{
613 char *v[AVMAX];
614 unsigned c = 0;
615 const struct cmdtab *ct;
616
617 p = strtok(p, ":");
618 if (!p || *p == '#')
619 return;
620 do {
621 v[c++] = p;
622 p = strtok(0, ":");
623 } while (p && c < AVMAX - 1);
624 v[c] = 0;
625 for (ct = cmdtab; ct->name; ct++) {
626 if (strcmp(ct->name, v[0]) == 0) {
627 ct->func(c - 1, v + 1);
628 return;
629 }
630 }
631 die(1, "unknown command `%s'", v[0]);
632}
633
634/*----- Main driver -------------------------------------------------------*/
635
636static void version(FILE *fp)
f98df549 637 { pquis(fp, "$, TrIPE version " VERSION "\n"); }
50c74957 638
639static void usage(FILE *fp)
f98df549 640 { pquis(fp, "Usage: $ [-k KEYRING] DIRECTIVE...\n"); }
50c74957 641
642static void help(FILE *fp)
643{
644 version(fp);
645 putc('\n', fp);
646 usage(fp);
2d752320 647 fputs("\n\
3cdc3f3a 648Options:\n\
649\n\
650-h, --help Show this help text.\n\
651-v, --version Show the version number.\n\
652-u, --usage Show terse usage summary.\n\
653\n\
654-k, --keyring=FILE Fetch keys from FILE.\n\
655\n\
2d752320 656Directives:\n\
657 peer:NAME:LOCAL-PORT:REMOTE-ADDR:REMOTE-PORT\n\
658 include:FILE\n\
659 {,l,r}filt:FILTER:ARGS:...\n\
660 next:TAG\n\
661 {,l,r}flood:TYPE:MILLIS:SIZE\n\
662\n\
663Filters:\n\
664 send\n\
665 fork:TAG\n\
666 delay:QLEN[:MILLIS:P-REPLAY]\n\
a2e06ed5 667 drop[:P-DROP]\n\
2d752320 668 corrupt[:P-CORRUPT]\n",
669 fp);
50c74957 670}
671
672int main(int argc, char *argv[])
673{
674 const char *kfname = "keyring.pub";
675 int i;
676 unsigned f = 0;
677 char buf[16];
678
679#define f_bogus 1u
680
681 ego(argv[0]);
682 for (;;) {
683 static const struct option opt[] = {
684 { "help", 0, 0, 'h' },
685 { "version", 0, 0, 'v' },
686 { "usage", 0, 0, 'u' },
687 { "keyring", OPTF_ARGREQ, 0, 'k' },
688 { 0, 0, 0, 0 }
689 };
690 if ((i = mdwopt(argc, argv, "hvuk:", opt, 0, 0, 0)) < 0)
691 break;
692 switch (i) {
693 case 'h':
694 help(stdout);
695 exit(0);
696 case 'v':
697 version(stdout);
698 exit(0);
699 case 'u':
700 usage(stdout);
701 exit(0);
702 case 'k':
703 kfname = optarg;
704 break;
705 default:
706 f |= f_bogus;
707 break;
708 }
709 }
710 if (f & f_bogus) {
711 usage(stderr);
712 exit(1);
713 }
714 rand_noisesrc(RAND_GLOBAL, &noise_source);
715 rand_seed(RAND_GLOBAL, 160);
716 rand_get(RAND_GLOBAL, buf, sizeof(buf));
717 rng = rc4_rand(buf, sizeof(buf));
718 sel_init(&sel);
719 if (key_open(&keys, kfname, KOPEN_READ, key_moan, 0))
720 die(1, "couldn't open `%s': %s", kfname, strerror(errno));
721 for (i = optind; i < argc; i++)
722 parse(argv[i]);
723 if (npeer != 2)
724 die(1, "need two peers");
725 for (;;)
726 sel_select(&sel);
727
728#undef f_bogus
729}
730
731/*----- That's all, folks -------------------------------------------------*/