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>
72 /*----- Data structures ---------------------------------------------------*/
81 typedef struct filter
{
84 void (*func
)(struct filter */
*f*/
, const octet */
*buf*/
, size_t /*sz*/);
88 typedef struct qnode
{
93 /*----- Static variables --------------------------------------------------*/
99 static unsigned npeer
= 0;
100 static key_file keys
;
103 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
104 #define RND(i) (rng->ops->range(rng, (i)))
106 /*----- Peer management ---------------------------------------------------*/
108 static void dopacket(int fd
, unsigned mode
, void *vv
)
112 int r
= read(fd
, buf
, sizeof(buf
));
114 printf("recv from `%s'\n", p
->name
);
119 static void addpeer(unsigned ac
, char **av
)
121 key_packstruct kps
[DH_PUBFETCHSZ
];
124 struct sockaddr_in sin
;
131 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
133 die(1, "enough peers already");
135 p
->name
= xstrdup(av
[0]);
136 kp
= key_fetchinit(dh_pubfetch
, kps
, &p
->kpub
);
137 e
= key_fetchbyname(kp
, &keys
, av
[0]);
140 die(1, "key_fetch `%s': %s", av
[0], key_strerror(e
));
141 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
142 die(1, "socket: %s", strerror(errno
));
143 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
144 memset(&sin
, 0, sizeof(sin
));
145 sin
.sin_family
= AF_INET
;
146 sin
.sin_addr
.s_addr
= INADDR_ANY
;
147 sin
.sin_port
= htons(atoi(av
[1]));
148 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
149 die(1, "bind: %s", strerror(errno
));
150 memset(&sin
, 0, sizeof(sin
));
151 sin
.sin_family
= AF_INET
;
152 if ((h
= gethostbyname(av
[2])) == 0)
153 die(1, "gethostbyname `%s'", av
[2]);
154 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
155 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
)))
156 die(1, "setsockopt: %s", strerror(errno
));
157 memcpy(&sin
.sin_addr
, h
->h_addr
, sizeof(sin
.sin_addr
));
158 sin
.sin_port
= htons(atoi(av
[3]));
159 if (connect(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
160 die(1, "connect: %s", strerror(errno
));
161 sel_initfile(&sel
, &p
->sf
, fd
, SEL_READ
, dopacket
, p
);
165 /*----- Fork filter -------------------------------------------------------*/
167 typedef struct forknode
{
168 struct forknode
*next
;
172 typedef struct forkfilt
{
177 static void dofork(filter
*f
, const octet
*buf
, size_t sz
)
179 forkfilt
*ff
= f
->state
;
184 for (fn
= ff
->fn
; fn
; fn
= fn
->next
) {
185 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
186 PASS(fn
->f
, buf
, sz
);
188 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
189 PASS(f
->next
, buf
, sz
);
192 static void addfork(filter
*f
, unsigned ac
, char **av
)
196 die(1, "syntax: filt:fork:NAME");
197 ff
= CREATE(forkfilt
);
198 ff
->name
= xstrdup(av
[0]);
204 static void nextfork(unsigned ac
, char **av
)
213 die(1, "syntax: next:NAME:...");
214 for (i
= 0; i
< 2; i
++) {
216 for (f
= p
->f
; f
; f
= f
->next
) {
217 if (f
->func
!= dofork
)
220 for (j
= 0; j
< ac
; j
++) {
221 if (strcmp(av
[j
], ff
->name
) == 0)
226 fn
= CREATE(forknode
);
227 for (ffn
= &ff
->fn
; *ffn
; ffn
= &(*ffn
)->next
)
237 /*----- Corrupt filter ----------------------------------------------------*/
239 typedef struct corrupt
{
243 static void docorrupt(filter
*f
, const octet
*buf
, size_t sz
)
245 corrupt
*c
= f
->state
;
249 while (!RND(c
->p_corrupt
)) {
250 puts("corrupt packet");
251 b
[RND(sz
)] ^= RND(256);
253 PASS(f
->next
, b
, sz
);
256 static void addcorrupt(filter
*f
, unsigned ac
, char **av
)
260 die(1, "syntax: filt:corrupt[:PCORRUPT]");
263 c
->p_corrupt
= atoi(av
[0]);
270 /*----- Delay filter ------------------------------------------------------*/
272 typedef struct delaynode
{
282 typedef struct delay
{
290 static void dtimer(struct timeval
*tv
, void *vv
);
292 static void dinsert(delaynode
*dn
)
296 unsigned long tdelta
= RND(dn
->d
->t
);
297 gettimeofday(&tv
, 0);
298 TV_ADDL(&tv
, &tv
, 0, tdelta
);
300 sel_addtimer(&sel
, &dn
->tm
, &tv
, dtimer
, dn
);
302 for (ta
= tb
= sel
.timers
; ta
; ta
= ta
->next
) {
303 ta
= ta
->next
; if (!ta
) break; assert(ta
!= tb
);
304 ta
= ta
->next
; if (!ta
) break; assert(ta
!= tb
);
307 printf(" delay %lu usecs", tdelta
);
310 static void dsend(delaynode
*dn
, unsigned force
)
314 fputs(" send...\n", stdout
);
316 PASS(d
->f
->next
, dn
->buf
, dn
->sz
);
317 fputs("delay ...", stdout
);
326 sel_rmtimer(&ddn
->tm
);
327 sel_addtimer(&sel
, &dn
->tm
, &ddn
->tm
.tv
, dtimer
, dn
);
334 printf(" move id %u from slot %u to slot %u", ddn
->seq
, ddn
->i
, dn
->i
);
336 { unsigned i
; for (i
= 0; i
< d
->n
; i
++) assert(d
->q
[i
].buf
); }
337 fputs(" remove", stdout
);
341 static void dtimer(struct timeval
*tv
, void *vv
)
344 printf("delay timer peer `%s' id %u slot %u",
345 dn
->d
->f
->p_from
->name
, dn
->seq
, dn
->i
);
347 dsend(dn
, RND(dn
->d
->p_replay
));
351 static void dodelay(filter
*f
, const octet
*buf
, size_t sz
)
355 static unsigned seq
= 0;
357 fputs("delay", stdout
);
358 if (d
->n
== d
->max
) {
359 dn
= &d
->q
[RND(d
->n
)];
360 printf(" force uid %u", dn
->seq
);
361 sel_rmtimer(&dn
->tm
);
368 printf(" new id %u in slot %u", dn
->seq
, dn
->i
);
369 dn
->buf
= xmalloc(sz
);
371 memcpy(dn
->buf
, buf
, sz
);
376 static void adddelay(filter
*f
, unsigned ac
, char **av
)
381 if (ac
< 1 || ac
> 3)
382 die(1, "syntax: filt:delay:QLEN[:MILLIS:PREPLAY]");
384 d
->max
= atoi(av
[0]);
386 d
->t
= strtoul(av
[1], 0, 10);
391 d
->p_replay
= atoi(av
[2]);
395 d
->q
= xmalloc(d
->max
* sizeof(delaynode
));
399 for (i
= 0; i
< d
->max
; i
++) {
407 /*----- Filters -----------------------------------------------------------*/
409 static void dosend(filter
*f
, const octet
*buf
, size_t sz
)
411 printf("send to `%s'\n", f
->p_to
->name
);
412 write(f
->p_to
->sf
.fd
, buf
, sz
);
415 static void addsend(filter
*f
, unsigned ac
, char **av
)
418 die(1, "syntax: filt:send");
422 const struct filtab
{
424 void (*func
)(filter */
*f*/
, unsigned /*ac*/, char **/
*av*/
);
428 { "delay", adddelay
},
429 { "corrupt", addcorrupt
},
433 static void dofilter(peer
*from
, peer
*to
, unsigned ac
, char **av
)
435 filter
**ff
, *f
= CREATE(filter
);
436 const struct filtab
*ft
;
438 die(1, "syntax: {l,r,}filt:NAME:...");
443 for (ff
= &from
->f
; *ff
; ff
= &(*ff
)->next
)
446 for (ft
= filtab
; ft
->name
; ft
++) {
447 if (strcmp(av
[0], ft
->name
) == 0) {
448 ft
->func(f
, ac
- 1, av
+ 1);
452 die(1, "unknown filter `%s'", av
[0]);
455 /*----- Flooding ----------------------------------------------------------*/
457 typedef struct flood
{
465 static void setflood(flood
*f
);
467 static void floodtimer(struct timeval
*tv
, void *vv
)
479 rng
->ops
->fill(rng
, buf
, sz
);
482 puts("flood packet");
483 PASS(f
->p
->f
, buf
, sz
);
487 static void setflood(flood
*f
)
490 gettimeofday(&tv
, 0);
491 TV_ADDL(&tv
, &tv
, 0, RND(f
->t
));
492 sel_addtimer(&sel
, &f
->tm
, &tv
, floodtimer
, f
);
495 static void doflood(peer
*p
, unsigned ac
, char **av
)
499 die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
503 f
->type
= strtoul(av
[0], 0, 16);
518 /*----- Configuration commands --------------------------------------------*/
520 static void parse(char *p
);
522 static void addflood(unsigned ac
, char **av
) {
523 doflood(&peers
[0], ac
, av
);
524 doflood(&peers
[1], ac
, av
);
526 static void addlflood(unsigned ac
, char **av
) {
527 doflood(&peers
[0], ac
, av
);
529 static void addrflood(unsigned ac
, char **av
) {
530 doflood(&peers
[1], ac
, av
);
533 static void addfilter(unsigned ac
, char **av
) {
534 dofilter(&peers
[0], &peers
[1], ac
, av
);
535 dofilter(&peers
[1], &peers
[0], ac
, av
);
537 static void addlfilter(unsigned ac
, char **av
) {
538 dofilter(&peers
[0], &peers
[1], ac
, av
);
540 static void addrfilter(unsigned ac
, char **av
) {
541 dofilter(&peers
[1], &peers
[0], ac
, av
);
544 static void include(unsigned ac
, char **av
)
549 die(1, "syntax: include:FILE:...");
551 if ((fp
= fopen(*av
, "r")) == 0)
552 die(1, "fopen `%s': %s", *av
, strerror(errno
));
553 while (dstr_putline(&d
, fp
) != EOF
) {
562 const struct cmdtab
{
564 void (*func
)(unsigned /*ac*/, char **/
*av*/
);
567 { "include", include
},
568 { "filt", addfilter
},
569 { "lfilt", addlfilter
},
570 { "rfilt", addrfilter
},
571 { "next", nextfork
},
572 { "flood", addflood
},
573 { "lflood", addlflood
},
574 { "rflood", addrflood
},
580 static void parse(char *p
)
584 const struct cmdtab
*ct
;
592 } while (p
&& c
< AVMAX
- 1);
594 for (ct
= cmdtab
; ct
->name
; ct
++) {
595 if (strcmp(ct
->name
, v
[0]) == 0) {
596 ct
->func(c
- 1, v
+ 1);
600 die(1, "unknown command `%s'", v
[0]);
603 /*----- Main driver -------------------------------------------------------*/
605 static void version(FILE *fp
)
606 { pquis(fp
, "$, TrIPE version " VERSION
"\n"); }
608 static void usage(FILE *fp
)
609 { pquis(fp
, "Usage: $ [-k KEYRING] DIRECTIVE...\n"); }
611 static void help(FILE *fp
)
619 -h, --help Show this help text.\n\
620 -v, --version Show the version number.\n\
621 -u, --usage Show terse usage summary.\n\
623 -k, --keyring=FILE Fetch keys from FILE.\n\
626 peer:NAME:LOCAL-PORT:REMOTE-ADDR:REMOTE-PORT\n\
628 {,l,r}filt:FILTER:ARGS:...\n\
630 {,l,r}flood:TYPE:MILLIS:SIZE\n\
635 delay:QLEN[:MILLIS:P-REPLAY]\n\
636 corrupt[:P-CORRUPT]\n",
640 int main(int argc
, char *argv
[])
642 const char *kfname
= "keyring.pub";
651 static const struct option opt
[] = {
652 { "help", 0, 0, 'h' },
653 { "version", 0, 0, 'v' },
654 { "usage", 0, 0, 'u' },
655 { "keyring", OPTF_ARGREQ
, 0, 'k' },
658 if ((i
= mdwopt(argc
, argv
, "hvuk:", opt
, 0, 0, 0)) < 0)
682 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
683 rand_seed(RAND_GLOBAL
, 160);
684 rand_get(RAND_GLOBAL
, buf
, sizeof(buf
));
685 rng
= rc4_rand(buf
, sizeof(buf
));
687 if (key_open(&keys
, kfname
, KOPEN_READ
, key_moan
, 0))
688 die(1, "couldn't open `%s': %s", kfname
, strerror(errno
));
689 for (i
= optind
; i
< argc
; i
++)
692 die(1, "need two peers");
699 /*----- That's all, folks -------------------------------------------------*/