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