3 * An evil proxy for TrIPE
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
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>
58 #include <catacomb/buf.h>
60 #include <catacomb/key.h>
62 #include <catacomb/mp.h>
63 #include <catacomb/mprand.h>
64 #include <catacomb/dh.h>
66 #include <catacomb/noise.h>
67 #include <catacomb/rand.h>
68 #include <catacomb/rc4.h>
70 /*----- Data structures ---------------------------------------------------*/
79 typedef struct filter {
82 void (*func)(struct filter */*f*/, const octet */*buf*/, size_t /*sz*/);
86 typedef struct qnode {
91 /*----- Static variables --------------------------------------------------*/
97 static unsigned npeer = 0;
101 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
102 #define RND(i) (rng->ops->range(rng, (i)))
104 /*----- Peer management ---------------------------------------------------*/
106 static void dopacket(int fd, unsigned mode, void *vv)
110 int r = read(fd, buf, sizeof(buf));
112 printf("recv from `%s'\n", p->name);
117 static void addpeer(unsigned ac, char **av)
119 key_packstruct kps[DH_PUBFETCHSZ];
122 struct sockaddr_in sin;
129 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
131 die(1, "enough peers already");
133 p->name = xstrdup(av[0]);
134 kp = key_fetchinit(dh_pubfetch, kps, &p->kpub);
135 e = key_fetchbyname(kp, &keys, av[0]);
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);
163 /*----- Fork filter -------------------------------------------------------*/
165 typedef struct forknode {
166 struct forknode *next;
170 typedef struct forkfilt {
175 static void dofork(filter *f, const octet *buf, size_t sz)
177 forkfilt *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);
186 printf("fork branch %u of fork `%s'\n", i++, ff->name);
187 PASS(f->next, buf, sz);
190 static void addfork(filter *f, unsigned ac, char **av)
194 die(1, "syntax: filt:fork:NAME");
195 ff = CREATE(forkfilt);
196 ff->name = xstrdup(av[0]);
202 static void nextfork(unsigned ac, char **av)
211 die(1, "syntax: next:NAME:...");
212 for (i = 0; i < 2; i++) {
214 for (f = p->f; f; f = f->next) {
215 if (f->func != dofork)
218 for (j = 0; j < ac; j++) {
219 if (strcmp(av[j], ff->name) == 0)
224 fn = CREATE(forknode);
225 for (ffn = &ff->fn; *ffn; ffn = &(*ffn)->next)
235 /*----- Corrupt filter ----------------------------------------------------*/
237 typedef struct corrupt {
241 static void docorrupt(filter *f, const octet *buf, size_t sz)
243 corrupt *c = f->state;
247 while (!RND(c->p_corrupt)) {
248 puts("corrupt packet");
249 b[RND(sz)] ^= RND(256);
251 PASS(f->next, b, sz);
254 static void addcorrupt(filter *f, unsigned ac, char **av)
258 die(1, "syntax: filt:corrupt[:PCORRUPT]");
261 c->p_corrupt = atoi(av[0]);
268 /*----- Delay filter ------------------------------------------------------*/
270 typedef struct delaynode {
280 typedef struct delay {
288 static void dtimer(struct timeval *tv, void *vv);
290 static void dinsert(delaynode *dn)
294 unsigned long tdelta = RND(dn->d->t);
295 gettimeofday(&tv, 0);
296 TV_ADDL(&tv, &tv, 0, tdelta);
298 sel_addtimer(&sel, &dn->tm, &tv, dtimer, dn);
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);
305 printf(" delay %lu usecs", tdelta);
308 static void dsend(delaynode *dn, unsigned force)
312 fputs(" send...\n", stdout);
314 PASS(d->f->next, dn->buf, dn->sz);
315 fputs("delay ...", stdout);
324 sel_rmtimer(&ddn->tm);
325 sel_addtimer(&sel, &dn->tm, &ddn->tm.tv, dtimer, dn);
332 printf(" move id %u from slot %u to slot %u", ddn->seq, ddn->i, dn->i);
334 { unsigned i; for (i = 0; i < d->n; i++) assert(d->q[i].buf); }
335 fputs(" remove", stdout);
339 static void dtimer(struct timeval *tv, void *vv)
342 printf("delay timer peer `%s' id %u slot %u",
343 dn->d->f->p_from->name, dn->seq, dn->i);
345 dsend(dn, RND(dn->d->p_replay));
349 static void dodelay(filter *f, const octet *buf, size_t sz)
353 static unsigned seq = 0;
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);
366 printf(" new id %u in slot %u", dn->seq, dn->i);
367 dn->buf = xmalloc(sz);
369 memcpy(dn->buf, buf, sz);
374 static void adddelay(filter *f, unsigned ac, char **av)
379 if (ac < 1 || ac > 3)
380 die(1, "syntax: filt:delay:QLEN[:MILLIS:PREPLAY]");
382 d->max = atoi(av[0]);
384 d->t = strtoul(av[1], 0, 10);
389 d->p_replay = atoi(av[2]);
393 d->q = xmalloc(d->max * sizeof(delaynode));
397 for (i = 0; i < d->max; i++) {
405 /*----- Filters -----------------------------------------------------------*/
407 static void dosend(filter *f, const octet *buf, size_t sz)
409 printf("send to `%s'\n", f->p_to->name);
410 write(f->p_to->sf.fd, buf, sz);
413 static void addsend(filter *f, unsigned ac, char **av)
416 die(1, "syntax: filt:send");
420 const struct filtab {
422 void (*func)(filter */*f*/, unsigned /*ac*/, char **/*av*/);
426 { "delay", adddelay },
427 { "corrupt", addcorrupt },
431 static void dofilter(peer *from, peer *to, unsigned ac, char **av)
433 filter **ff, *f = CREATE(filter);
434 const struct filtab *ft;
436 die(1, "syntax: {l,r,}filt:NAME:...");
441 for (ff = &from->f; *ff; ff = &(*ff)->next)
444 for (ft = filtab; ft->name; ft++) {
445 if (strcmp(av[0], ft->name) == 0) {
446 ft->func(f, ac - 1, av + 1);
450 die(1, "unknown filter `%s'", av[0]);
453 /*----- Flooding ----------------------------------------------------------*/
455 typedef struct flood {
463 static void setflood(flood *f);
465 static void floodtimer(struct timeval *tv, void *vv)
477 rng->ops->fill(rng, buf, sz);
480 puts("flood packet");
481 PASS(f->p->f, buf, sz);
485 static void setflood(flood *f)
488 gettimeofday(&tv, 0);
489 TV_ADDL(&tv, &tv, 0, RND(f->t));
490 sel_addtimer(&sel, &f->tm, &tv, floodtimer, f);
493 static void doflood(peer *p, unsigned ac, char **av)
497 die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
501 f->type = strtoul(av[0], 0, 16);
516 /*----- Configuration commands --------------------------------------------*/
518 static void parse(char *p);
520 static void addflood(unsigned ac, char **av) {
521 doflood(&peers[0], ac, av);
522 doflood(&peers[1], ac, av);
524 static void addlflood(unsigned ac, char **av) {
525 doflood(&peers[0], ac, av);
527 static void addrflood(unsigned ac, char **av) {
528 doflood(&peers[1], ac, av);
531 static void addfilter(unsigned ac, char **av) {
532 dofilter(&peers[0], &peers[1], ac, av);
533 dofilter(&peers[1], &peers[0], ac, av);
535 static void addlfilter(unsigned ac, char **av) {
536 dofilter(&peers[0], &peers[1], ac, av);
538 static void addrfilter(unsigned ac, char **av) {
539 dofilter(&peers[1], &peers[0], ac, av);
542 static void include(unsigned ac, char **av)
547 die(1, "syntax: include:FILE:...");
549 if ((fp = fopen(*av, "r")) == 0)
550 die(1, "fopen `%s': %s", *av, strerror(errno));
551 while (dstr_putline(&d, fp) != EOF) {
560 const struct cmdtab {
562 void (*func)(unsigned /*ac*/, char **/*av*/);
565 { "include", include },
566 { "filt", addfilter },
567 { "lfilt", addlfilter },
568 { "rfilt", addrfilter },
569 { "next", nextfork },
570 { "flood", addflood },
571 { "lflood", addlflood },
572 { "rflood", addrflood },
578 static void parse(char *p)
582 const struct cmdtab *ct;
590 } while (p && c < AVMAX - 1);
592 for (ct = cmdtab; ct->name; ct++) {
593 if (strcmp(ct->name, v[0]) == 0) {
594 ct->func(c - 1, v + 1);
598 die(1, "unknown command `%s'", v[0]);
601 /*----- Main driver -------------------------------------------------------*/
603 static void version(FILE *fp)
604 { pquis(fp, "$, TrIPE version " VERSION "\n"); }
606 static void usage(FILE *fp)
607 { pquis(fp, "Usage: $ [-k KEYRING] DIRECTIVE...\n"); }
609 static void help(FILE *fp)
617 -h, --help Show this help text.\n\
618 -v, --version Show the version number.\n\
619 -u, --usage Show terse usage summary.\n\
621 -k, --keyring=FILE Fetch keys from FILE.\n\
624 peer:NAME:LOCAL-PORT:REMOTE-ADDR:REMOTE-PORT\n\
626 {,l,r}filt:FILTER:ARGS:...\n\
628 {,l,r}flood:TYPE:MILLIS:SIZE\n\
633 delay:QLEN[:MILLIS:P-REPLAY]\n\
634 corrupt[:P-CORRUPT]\n",
638 int main(int argc, char *argv[])
640 const char *kfname = "keyring.pub";
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' },
656 if ((i = mdwopt(argc, argv, "hvuk:", opt, 0, 0, 0)) < 0)
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));
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++)
690 die(1, "need two peers");
697 /*----- That's all, folks -------------------------------------------------*/