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