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 it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <mLib/alloc.h>
48 #include <mLib/dstr.h>
49 #include <mLib/fdflags.h>
50 #include <mLib/mdwopt.h>
51 #include <mLib/quis.h>
52 #include <mLib/report.h>
57 #include <catacomb/buf.h>
59 #include <catacomb/key.h>
61 #include <catacomb/mp.h>
62 #include <catacomb/mprand.h>
63 #include <catacomb/dh.h>
65 #include <catacomb/noise.h>
66 #include <catacomb/rand.h>
67 #include <catacomb/rc4.h>
71 /*----- Data structures ---------------------------------------------------*/
80 typedef struct filter {
83 void (*func)(struct filter */*f*/, const octet */*buf*/, size_t /*sz*/);
87 typedef struct qnode {
92 /*----- Static variables --------------------------------------------------*/
98 static unsigned npeer = 0;
102 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
103 #define RND(i) (rng->ops->range(rng, (i)))
105 /*----- Peer management ---------------------------------------------------*/
107 static void dopacket(int fd, unsigned mode, void *vv)
111 int r = read(fd, buf, sizeof(buf));
113 printf("recv from `%s'\n", p->name);
118 static void addpeer(unsigned ac, char **av)
120 key_packstruct kps[DH_PUBFETCHSZ];
123 struct sockaddr_in sin;
130 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
132 die(1, "enough peers already");
134 p->name = xstrdup(av[0]);
135 kp = key_fetchinit(dh_pubfetch, kps, &p->kpub);
136 e = key_fetchbyname(kp, &keys, av[0]);
139 die(1, "key_fetch `%s': %s", av[0], key_strerror(e));
140 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
141 die(1, "socket: %s", strerror(errno));
142 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
143 memset(&sin, 0, sizeof(sin));
144 sin.sin_family = AF_INET;
145 sin.sin_addr.s_addr = INADDR_ANY;
146 sin.sin_port = htons(atoi(av[1]));
147 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
148 die(1, "bind: %s", strerror(errno));
149 memset(&sin, 0, sizeof(sin));
150 sin.sin_family = AF_INET;
151 if ((h = gethostbyname(av[2])) == 0)
152 die(1, "gethostbyname `%s'", av[2]);
153 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
154 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)))
155 die(1, "setsockopt: %s", strerror(errno));
156 memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr));
157 sin.sin_port = htons(atoi(av[3]));
158 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)))
159 die(1, "connect: %s", strerror(errno));
160 sel_initfile(&sel, &p->sf, fd, SEL_READ, dopacket, p);
164 /*----- Fork filter -------------------------------------------------------*/
166 typedef struct forknode {
167 struct forknode *next;
171 typedef struct forkfilt {
176 static void dofork(filter *f, const octet *buf, size_t sz)
178 forkfilt *ff = f->state;
183 for (fn = ff->fn; fn; fn = fn->next) {
184 printf("fork branch %u of fork `%s'\n", i++, ff->name);
185 PASS(fn->f, buf, sz);
187 printf("fork branch %u of fork `%s'\n", i++, ff->name);
188 PASS(f->next, buf, sz);
191 static void addfork(filter *f, unsigned ac, char **av)
195 die(1, "syntax: filt:fork:NAME");
196 ff = CREATE(forkfilt);
197 ff->name = xstrdup(av[0]);
203 static void nextfork(unsigned ac, char **av)
212 die(1, "syntax: next:NAME:...");
213 for (i = 0; i < 2; i++) {
215 for (f = p->f; f; f = f->next) {
216 if (f->func != dofork)
219 for (j = 0; j < ac; j++) {
220 if (strcmp(av[j], ff->name) == 0)
225 fn = CREATE(forknode);
226 for (ffn = &ff->fn; *ffn; ffn = &(*ffn)->next)
236 /*----- Corrupt filter ----------------------------------------------------*/
238 typedef struct corrupt {
242 static void docorrupt(filter *f, const octet *buf, size_t sz)
244 corrupt *c = f->state;
248 while (!RND(c->p_corrupt)) {
249 puts("corrupt packet");
250 b[RND(sz)] ^= RND(256);
252 PASS(f->next, b, sz);
255 static void addcorrupt(filter *f, unsigned ac, char **av)
259 die(1, "syntax: filt:corrupt[:P-CORRUPT]");
262 c->p_corrupt = atoi(av[0]);
269 /*----- Drop filter -------------------------------------------------------*/
271 typedef struct drop {
275 static void dodrop(filter *f, const octet *buf, size_t sz)
282 PASS(f->next, buf, sz);
285 static void adddrop(filter *f, unsigned ac, char **av)
289 die(1, "syntax: filt:drop[:P-DROP]");
292 d->p_drop = atoi(av[0]);
299 /*----- Delay filter ------------------------------------------------------*/
301 typedef struct delaynode {
311 typedef struct delay {
319 static void dtimer(struct timeval *tv, void *vv);
321 static void dinsert(delaynode *dn)
325 unsigned long tdelta = RND(dn->d->t);
326 gettimeofday(&tv, 0);
327 TV_ADDL(&tv, &tv, 0, tdelta);
329 sel_addtimer(&sel, &dn->tm, &tv, dtimer, dn);
331 for (ta = tb = sel.timers; ta; ta = ta->next) {
332 ta = ta->next; if (!ta) break; assert(ta != tb);
333 ta = ta->next; if (!ta) break; assert(ta != tb);
336 printf(" delay %lu usecs", tdelta);
339 static void dsend(delaynode *dn, unsigned force)
343 fputs(" send...\n", stdout);
345 PASS(d->f->next, dn->buf, dn->sz);
346 fputs("delay ...", stdout);
355 sel_rmtimer(&ddn->tm);
356 sel_addtimer(&sel, &dn->tm, &ddn->tm.tv, dtimer, dn);
363 printf(" move id %u from slot %u to slot %u", ddn->seq, ddn->i, dn->i);
365 { unsigned i; for (i = 0; i < d->n; i++) assert(d->q[i].buf); }
366 fputs(" remove", stdout);
370 static void dtimer(struct timeval *tv, void *vv)
373 printf("delay timer peer `%s' id %u slot %u",
374 dn->d->f->p_from->name, dn->seq, dn->i);
376 dsend(dn, RND(dn->d->p_replay));
380 static void dodelay(filter *f, const octet *buf, size_t sz)
384 static unsigned seq = 0;
386 fputs("delay", stdout);
387 if (d->n == d->max) {
388 dn = &d->q[RND(d->n)];
389 printf(" force uid %u", dn->seq);
390 sel_rmtimer(&dn->tm);
397 printf(" new id %u in slot %u", dn->seq, dn->i);
398 dn->buf = xmalloc(sz);
400 memcpy(dn->buf, buf, sz);
405 static void adddelay(filter *f, unsigned ac, char **av)
410 if (ac < 1 || ac > 3)
411 die(1, "syntax: filt:delay:QLEN[:MILLIS:P-REPLAY]");
413 d->max = atoi(av[0]);
415 d->t = strtoul(av[1], 0, 10);
420 d->p_replay = atoi(av[2]);
424 d->q = xmalloc(d->max * sizeof(delaynode));
428 for (i = 0; i < d->max; i++) {
436 /*----- Filters -----------------------------------------------------------*/
438 static void dosend(filter *f, const octet *buf, size_t sz)
440 printf("send to `%s'\n", f->p_to->name);
441 DISCARD(write(f->p_to->sf.fd, buf, sz));
444 static void addsend(filter *f, unsigned ac, char **av)
447 die(1, "syntax: filt:send");
451 const struct filtab {
453 void (*func)(filter */*f*/, unsigned /*ac*/, char **/*av*/);
457 { "delay", adddelay },
459 { "corrupt", addcorrupt },
463 static void dofilter(peer *from, peer *to, unsigned ac, char **av)
465 filter **ff, *f = CREATE(filter);
466 const struct filtab *ft;
468 die(1, "syntax: {l,r,}filt:NAME:...");
473 for (ff = &from->f; *ff; ff = &(*ff)->next)
476 for (ft = filtab; ft->name; ft++) {
477 if (strcmp(av[0], ft->name) == 0) {
478 ft->func(f, ac - 1, av + 1);
482 die(1, "unknown filter `%s'", av[0]);
485 /*----- Flooding ----------------------------------------------------------*/
487 typedef struct flood {
495 static void setflood(flood *f);
497 static void floodtimer(struct timeval *tv, void *vv)
509 rng->ops->fill(rng, buf, sz);
512 puts("flood packet");
513 PASS(f->p->f, buf, sz);
517 static void setflood(flood *f)
520 gettimeofday(&tv, 0);
521 TV_ADDL(&tv, &tv, 0, RND(f->t));
522 sel_addtimer(&sel, &f->tm, &tv, floodtimer, f);
525 static void doflood(peer *p, unsigned ac, char **av)
529 die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
533 f->type = strtoul(av[0], 0, 16);
548 /*----- Configuration commands --------------------------------------------*/
550 static void parse(char *p);
552 static void addflood(unsigned ac, char **av) {
553 doflood(&peers[0], ac, av);
554 doflood(&peers[1], ac, av);
556 static void addlflood(unsigned ac, char **av) {
557 doflood(&peers[0], ac, av);
559 static void addrflood(unsigned ac, char **av) {
560 doflood(&peers[1], ac, av);
563 static void addfilter(unsigned ac, char **av) {
564 dofilter(&peers[0], &peers[1], ac, av);
565 dofilter(&peers[1], &peers[0], ac, av);
567 static void addlfilter(unsigned ac, char **av) {
568 dofilter(&peers[0], &peers[1], ac, av);
570 static void addrfilter(unsigned ac, char **av) {
571 dofilter(&peers[1], &peers[0], ac, av);
574 static void include(unsigned ac, char **av)
579 die(1, "syntax: include:FILE:...");
581 if ((fp = fopen(*av, "r")) == 0)
582 die(1, "fopen `%s': %s", *av, strerror(errno));
583 while (dstr_putline(&d, fp) != EOF) {
592 const struct cmdtab {
594 void (*func)(unsigned /*ac*/, char **/*av*/);
597 { "include", include },
598 { "filt", addfilter },
599 { "lfilt", addlfilter },
600 { "rfilt", addrfilter },
601 { "next", nextfork },
602 { "flood", addflood },
603 { "lflood", addlflood },
604 { "rflood", addrflood },
610 static void parse(char *p)
614 const struct cmdtab *ct;
622 } while (p && c < AVMAX - 1);
624 for (ct = cmdtab; ct->name; ct++) {
625 if (strcmp(ct->name, v[0]) == 0) {
626 ct->func(c - 1, v + 1);
630 die(1, "unknown command `%s'", v[0]);
633 /*----- Main driver -------------------------------------------------------*/
635 static void version(FILE *fp)
636 { pquis(fp, "$, TrIPE version " VERSION "\n"); }
638 static void usage(FILE *fp)
639 { pquis(fp, "Usage: $ [-k KEYRING] DIRECTIVE...\n"); }
641 static void help(FILE *fp)
649 -h, --help Show this help text.\n\
650 -v, --version Show the version number.\n\
651 -u, --usage Show terse usage summary.\n\
653 -k, --keyring=FILE Fetch keys from FILE.\n\
656 peer:NAME:LOCAL-PORT:REMOTE-ADDR:REMOTE-PORT\n\
658 {,l,r}filt:FILTER:ARGS:...\n\
660 {,l,r}flood:TYPE:MILLIS:SIZE\n\
665 delay:QLEN[:MILLIS:P-REPLAY]\n\
667 corrupt[:P-CORRUPT]\n",
671 int main(int argc, char *argv[])
673 const char *kfname = "keyring.pub";
682 static const struct option opt[] = {
683 { "help", 0, 0, 'h' },
684 { "version", 0, 0, 'v' },
685 { "usage", 0, 0, 'u' },
686 { "keyring", OPTF_ARGREQ, 0, 'k' },
689 if ((i = mdwopt(argc, argv, "hvuk:", opt, 0, 0, 0)) < 0)
713 rand_noisesrc(RAND_GLOBAL, &noise_source);
714 rand_seed(RAND_GLOBAL, 160);
715 rand_get(RAND_GLOBAL, buf, sizeof(buf));
716 rng = rc4_rand(buf, sizeof(buf));
718 if (key_open(&keys, kfname, KOPEN_READ, key_moan, 0))
719 die(1, "couldn't open `%s': %s", kfname, strerror(errno));
720 for (i = optind; i < argc; i++)
723 die(1, "need two peers");
725 if (sel_select(&sel) && errno != EINTR)
726 die(1, "select failed: %s", strerror(errno));
732 /*----- That's all, folks -------------------------------------------------*/