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