3 * $Id: mallory.c,v 1.4 2004/04/08 01:36:17 mdw Exp $
5 * An evil proxy for TrIPE
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
40 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
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>
60 #include <catacomb/buf.h>
62 #include <catacomb/key.h>
64 #include <catacomb/mp.h>
65 #include <catacomb/mprand.h>
66 #include <catacomb/dh.h>
68 #include <catacomb/noise.h>
69 #include <catacomb/rand.h>
70 #include <catacomb/rc4.h>
73 /*----- Data structures ---------------------------------------------------*/
82 typedef struct filter {
85 void (*func)(struct filter */*f*/, const octet */*buf*/, size_t /*sz*/);
89 typedef struct qnode {
94 /*----- Static variables --------------------------------------------------*/
100 static unsigned npeer = 0;
101 static key_file keys;
104 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
105 #define RND(i) (rng->ops->range(rng, (i)))
107 /*----- Peer management ---------------------------------------------------*/
109 static void dopacket(int fd, unsigned mode, void *vv)
113 int r = read(fd, buf, sizeof(buf));
115 printf("recv from `%s'\n", p->name);
120 static void addpeer(unsigned ac, char **av)
122 key_packstruct kps[DH_PUBFETCHSZ];
125 struct sockaddr_in sin;
132 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
134 die(1, "enough peers already");
136 p->name = xstrdup(av[0]);
137 kp = key_fetchinit(dh_pubfetch, kps, &p->kpub);
138 e = key_fetchbyname(kp, &keys, av[0]);
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);
166 /*----- Fork filter -------------------------------------------------------*/
168 typedef struct forknode {
169 struct forknode *next;
173 typedef struct forkfilt {
178 static void dofork(filter *f, const octet *buf, size_t sz)
180 forkfilt *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);
189 printf("fork branch %u of fork `%s'\n", i++, ff->name);
190 PASS(f->next, buf, sz);
193 static void addfork(filter *f, unsigned ac, char **av)
197 die(1, "syntax: filt:fork:NAME");
198 ff = CREATE(forkfilt);
199 ff->name = xstrdup(av[0]);
205 static void nextfork(unsigned ac, char **av)
214 die(1, "syntax: next:NAME:...");
215 for (i = 0; i < 2; i++) {
217 for (f = p->f; f; f = f->next) {
218 if (f->func != dofork)
221 for (j = 0; j < ac; j++) {
222 if (strcmp(av[j], ff->name) == 0)
227 fn = CREATE(forknode);
228 for (ffn = &ff->fn; *ffn; ffn = &(*ffn)->next)
238 /*----- Corrupt filter ----------------------------------------------------*/
240 typedef struct corrupt {
244 static void docorrupt(filter *f, const octet *buf, size_t sz)
246 corrupt *c = f->state;
250 while (!RND(c->p_corrupt)) {
251 puts("corrupt packet");
252 b[RND(sz)] ^= RND(256);
254 PASS(f->next, b, sz);
257 static void addcorrupt(filter *f, unsigned ac, char **av)
261 die(1, "syntax: filt:corrupt[:PCORRUPT]");
264 c->p_corrupt = atoi(av[0]);
271 /*----- Delay filter ------------------------------------------------------*/
273 typedef struct delaynode {
283 typedef struct delay {
291 static void dtimer(struct timeval *tv, void *vv);
293 static void dinsert(delaynode *dn)
297 unsigned long tdelta = RND(dn->d->t);
298 gettimeofday(&tv, 0);
299 TV_ADDL(&tv, &tv, 0, tdelta);
301 sel_addtimer(&sel, &dn->tm, &tv, dtimer, dn);
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);
308 printf(" delay %lu usecs", tdelta);
311 static void dsend(delaynode *dn, unsigned force)
315 fputs(" send...\n", stdout);
317 PASS(d->f->next, dn->buf, dn->sz);
318 fputs("delay ...", stdout);
327 sel_rmtimer(&ddn->tm);
328 sel_addtimer(&sel, &dn->tm, &ddn->tm.tv, dtimer, dn);
335 printf(" move id %u from slot %u to slot %u", ddn->seq, ddn->i, dn->i);
337 { unsigned i; for (i = 0; i < d->n; i++) assert(d->q[i].buf); }
338 fputs(" remove", stdout);
342 static void dtimer(struct timeval *tv, void *vv)
345 printf("delay timer peer `%s' id %u slot %u",
346 dn->d->f->p_from->name, dn->seq, dn->i);
348 dsend(dn, RND(dn->d->p_replay));
352 static void dodelay(filter *f, const octet *buf, size_t sz)
356 static unsigned seq = 0;
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);
369 printf(" new id %u in slot %u", dn->seq, dn->i);
370 dn->buf = xmalloc(sz);
372 memcpy(dn->buf, buf, sz);
377 static void adddelay(filter *f, unsigned ac, char **av)
382 if (ac < 1 || ac > 3)
383 die(1, "syntax: filt:delay:QLEN[:MILLIS:PREPLAY]");
385 d->max = atoi(av[0]);
387 d->t = strtoul(av[1], 0, 10);
392 d->p_replay = atoi(av[2]);
396 d->q = xmalloc(d->max * sizeof(delaynode));
400 for (i = 0; i < d->max; i++) {
408 /*----- Filters -----------------------------------------------------------*/
410 static void dosend(filter *f, const octet *buf, size_t sz)
412 printf("send to `%s'\n", f->p_to->name);
413 write(f->p_to->sf.fd, buf, sz);
416 static void addsend(filter *f, unsigned ac, char **av)
419 die(1, "syntax: filt:send");
423 const struct filtab {
425 void (*func)(filter */*f*/, unsigned /*ac*/, char **/*av*/);
429 { "delay", adddelay },
430 { "corrupt", addcorrupt },
434 static void dofilter(peer *from, peer *to, unsigned ac, char **av)
436 filter **ff, *f = CREATE(filter);
437 const struct filtab *ft;
439 die(1, "syntax: {l,r,}filt:NAME:...");
444 for (ff = &from->f; *ff; ff = &(*ff)->next)
447 for (ft = filtab; ft->name; ft++) {
448 if (strcmp(av[0], ft->name) == 0) {
449 ft->func(f, ac - 1, av + 1);
453 die(1, "unknown filter `%s'", av[0]);
456 /*----- Flooding ----------------------------------------------------------*/
458 typedef struct flood {
466 static void setflood(flood *f);
468 static void floodtimer(struct timeval *tv, void *vv)
480 rng->ops->fill(rng, buf, sz);
483 puts("flood packet");
484 PASS(f->p->f, buf, sz);
488 static void setflood(flood *f)
491 gettimeofday(&tv, 0);
492 TV_ADDL(&tv, &tv, 0, RND(f->t));
493 sel_addtimer(&sel, &f->tm, &tv, floodtimer, f);
496 static void doflood(peer *p, unsigned ac, char **av)
500 die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
504 f->type = strtoul(av[0], 0, 16);
519 /*----- Configuration commands --------------------------------------------*/
521 static void parse(char *p);
523 static void addflood(unsigned ac, char **av) {
524 doflood(&peers[0], ac, av);
525 doflood(&peers[1], ac, av);
527 static void addlflood(unsigned ac, char **av) {
528 doflood(&peers[0], ac, av);
530 static void addrflood(unsigned ac, char **av) {
531 doflood(&peers[1], ac, av);
534 static void addfilter(unsigned ac, char **av) {
535 dofilter(&peers[0], &peers[1], ac, av);
536 dofilter(&peers[1], &peers[0], ac, av);
538 static void addlfilter(unsigned ac, char **av) {
539 dofilter(&peers[0], &peers[1], ac, av);
541 static void addrfilter(unsigned ac, char **av) {
542 dofilter(&peers[1], &peers[0], ac, av);
545 static void include(unsigned ac, char **av)
550 die(1, "syntax: include:FILE:...");
552 if ((fp = fopen(*av, "r")) == 0)
553 die(1, "fopen `%s': %s", *av, strerror(errno));
554 while (dstr_putline(&d, fp) != EOF) {
563 const struct cmdtab {
565 void (*func)(unsigned /*ac*/, char **/*av*/);
568 { "include", include },
569 { "filt", addfilter },
570 { "lfilt", addlfilter },
571 { "rfilt", addrfilter },
572 { "next", nextfork },
573 { "flood", addflood },
574 { "lflood", addlflood },
575 { "rflood", addrflood },
581 static void parse(char *p)
585 const struct cmdtab *ct;
593 } while (p && c < AVMAX - 1);
595 for (ct = cmdtab; ct->name; ct++) {
596 if (strcmp(ct->name, v[0]) == 0) {
597 ct->func(c - 1, v + 1);
601 die(1, "unknown command `%s'", v[0]);
604 /*----- Main driver -------------------------------------------------------*/
606 static void version(FILE *fp)
608 pquis(fp, "$, TrIPE version " VERSION "\n");
611 static void usage(FILE *fp)
613 pquis(fp, "Usage: $ [-k keyring] directive...\n");
616 static void help(FILE *fp)
623 int main(int argc, char *argv[])
625 const char *kfname = "keyring.pub";
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' },
641 if ((i = mdwopt(argc, argv, "hvuk:", opt, 0, 0, 0)) < 0)
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));
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++)
675 die(1, "need two peers");
682 /*----- That's all, folks -------------------------------------------------*/