3 * $Id: mallory.c,v 1.2 2003/10/15 09:31:06 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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 2003/10/15 09:31:06 mdw
33 * Make forking work properly.
35 * Revision 1.1 2001/06/19 22:11:14 mdw
36 * The beginnings of a malicious proxy for TrIPE.
40 /*----- Header files ------------------------------------------------------*/
51 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
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>
71 #include <catacomb/buf.h>
73 #include <catacomb/key.h>
75 #include <catacomb/mp.h>
76 #include <catacomb/mpmont.h>
77 #include <catacomb/mprand.h>
78 #include <catacomb/dh.h>
80 #include <catacomb/noise.h>
81 #include <catacomb/rand.h>
82 #include <catacomb/rc4.h>
85 /*----- Data structures ---------------------------------------------------*/
94 typedef struct filter
{
97 void (*func
)(struct filter */
*f*/
, const octet */
*buf*/
, size_t /*sz*/);
101 typedef struct qnode
{
106 /*----- Static variables --------------------------------------------------*/
108 #define PKBUFSZ 65536
110 static sel_state sel
;
111 static peer peers
[2];
112 static unsigned npeer
= 0;
113 static key_file keys
;
116 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
117 #define RND(i) (rng->ops->range(rng, (i)))
119 /*----- Peer management ---------------------------------------------------*/
121 static void dopacket(int fd
, unsigned mode
, void *vv
)
125 int r
= read(fd
, buf
, sizeof(buf
));
127 printf("recv from `%s'\n", p
->name
);
132 static void addpeer(unsigned ac
, char **av
)
134 key_packstruct kps
[DH_PUBFETCHSZ
];
137 struct sockaddr_in sin
;
144 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
146 die(1, "enough peers already");
148 p
->name
= xstrdup(av
[0]);
149 kp
= key_fetchinit(dh_pubfetch
, kps
, &p
->kpub
);
150 e
= key_fetchbyname(kp
, &keys
, av
[0]);
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
);
178 /*----- Fork filter -------------------------------------------------------*/
180 typedef struct forknode
{
181 struct forknode
*next
;
185 typedef struct forkfilt
{
190 static void dofork(filter
*f
, const octet
*buf
, size_t sz
)
192 forkfilt
*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
);
201 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
202 PASS(f
->next
, buf
, sz
);
205 static void addfork(filter
*f
, unsigned ac
, char **av
)
209 die(1, "syntax: filt:fork:NAME");
210 ff
= CREATE(forkfilt
);
211 ff
->name
= xstrdup(av
[0]);
217 static void nextfork(unsigned ac
, char **av
)
226 die(1, "syntax: next:NAME:...");
227 for (i
= 0; i
< 2; i
++) {
229 for (f
= p
->f
; f
; f
= f
->next
) {
230 if (f
->func
!= dofork
)
233 for (j
= 0; j
< ac
; j
++) {
234 if (strcmp(av
[j
], ff
->name
) == 0)
239 fn
= CREATE(forknode
);
240 for (ffn
= &ff
->fn
; *ffn
; ffn
= &(*ffn
)->next
)
250 /*----- Corrupt filter ----------------------------------------------------*/
252 typedef struct corrupt
{
256 static void docorrupt(filter
*f
, const octet
*buf
, size_t sz
)
258 corrupt
*c
= f
->state
;
262 while (!RND(c
->p_corrupt
)) {
263 puts("corrupt packet");
264 b
[RND(sz
)] ^= RND(256);
266 PASS(f
->next
, b
, sz
);
269 static void addcorrupt(filter
*f
, unsigned ac
, char **av
)
273 die(1, "syntax: filt:corrupt[:PCORRUPT]");
276 c
->p_corrupt
= atoi(av
[0]);
283 /*----- Delay filter ------------------------------------------------------*/
285 typedef struct delaynode
{
295 typedef struct delay
{
303 static void dtimer(struct timeval
*tv
, void *vv
);
305 static void dinsert(delaynode
*dn
)
309 unsigned long tdelta
= RND(dn
->d
->t
);
310 gettimeofday(&tv
, 0);
311 TV_ADDL(&tv
, &tv
, 0, tdelta
);
313 sel_addtimer(&sel
, &dn
->tm
, &tv
, dtimer
, dn
);
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
);
320 printf(" delay %lu usecs", tdelta
);
323 static void dsend(delaynode
*dn
, unsigned force
)
327 fputs(" send...\n", stdout
);
329 PASS(d
->f
->next
, dn
->buf
, dn
->sz
);
330 fputs("delay ...", stdout
);
339 sel_rmtimer(&ddn
->tm
);
340 sel_addtimer(&sel
, &dn
->tm
, &ddn
->tm
.tv
, dtimer
, dn
);
347 printf(" move id %u from slot %u to slot %u", ddn
->seq
, ddn
->i
, dn
->i
);
349 { unsigned i
; for (i
= 0; i
< d
->n
; i
++) assert(d
->q
[i
].buf
); }
350 fputs(" remove", stdout
);
354 static void dtimer(struct timeval
*tv
, void *vv
)
357 printf("delay timer peer `%s' id %u slot %u",
358 dn
->d
->f
->p_from
->name
, dn
->seq
, dn
->i
);
360 dsend(dn
, RND(dn
->d
->p_replay
));
364 static void dodelay(filter
*f
, const octet
*buf
, size_t sz
)
368 static unsigned seq
= 0;
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
);
381 printf(" new id %u in slot %u", dn
->seq
, dn
->i
);
382 dn
->buf
= xmalloc(sz
);
384 memcpy(dn
->buf
, buf
, sz
);
389 static void adddelay(filter
*f
, unsigned ac
, char **av
)
394 if (ac
< 1 || ac
> 3)
395 die(1, "syntax: filt:delay:QLEN[:MILLIS:PREPLAY]");
397 d
->max
= atoi(av
[0]);
399 d
->t
= strtoul(av
[1], 0, 10);
404 d
->p_replay
= atoi(av
[2]);
408 d
->q
= xmalloc(d
->max
* sizeof(delaynode
));
412 for (i
= 0; i
< d
->max
; i
++) {
420 /*----- Filters -----------------------------------------------------------*/
422 static void dosend(filter
*f
, const octet
*buf
, size_t sz
)
424 printf("send to `%s'\n", f
->p_to
->name
);
425 write(f
->p_to
->sf
.fd
, buf
, sz
);
428 static void addsend(filter
*f
, unsigned ac
, char **av
)
431 die(1, "syntax: filt:send");
435 const struct filtab
{
437 void (*func
)(filter */
*f*/
, unsigned /*ac*/, char **/
*av*/
);
441 { "delay", adddelay
},
442 { "corrupt", addcorrupt
},
446 static void dofilter(peer
*from
, peer
*to
, unsigned ac
, char **av
)
448 filter
**ff
, *f
= CREATE(filter
);
449 const struct filtab
*ft
;
451 die(1, "syntax: {l,r,}filt:NAME:...");
456 for (ff
= &from
->f
; *ff
; ff
= &(*ff
)->next
)
459 for (ft
= filtab
; ft
->name
; ft
++) {
460 if (strcmp(av
[0], ft
->name
) == 0) {
461 ft
->func(f
, ac
- 1, av
+ 1);
465 die(1, "unknown filter `%s'", av
[0]);
468 /*----- Flooding ----------------------------------------------------------*/
470 typedef struct flood
{
478 static void setflood(flood
*f
);
480 static void floodtimer(struct timeval
*tv
, void *vv
)
492 rng
->ops
->fill(rng
, buf
, sz
);
495 puts("flood packet");
496 PASS(f
->p
->f
, buf
, sz
);
500 static void setflood(flood
*f
)
503 gettimeofday(&tv
, 0);
504 TV_ADDL(&tv
, &tv
, 0, RND(f
->t
));
505 sel_addtimer(&sel
, &f
->tm
, &tv
, floodtimer
, f
);
508 static void doflood(peer
*p
, unsigned ac
, char **av
)
512 die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
516 f
->type
= strtoul(av
[0], 0, 16);
531 /*----- Configuration commands --------------------------------------------*/
533 static void parse(char *p
);
535 static void addflood(unsigned ac
, char **av
) {
536 doflood(&peers
[0], ac
, av
);
537 doflood(&peers
[1], ac
, av
);
539 static void addlflood(unsigned ac
, char **av
) {
540 doflood(&peers
[0], ac
, av
);
542 static void addrflood(unsigned ac
, char **av
) {
543 doflood(&peers
[1], ac
, av
);
546 static void addfilter(unsigned ac
, char **av
) {
547 dofilter(&peers
[0], &peers
[1], ac
, av
);
548 dofilter(&peers
[1], &peers
[0], ac
, av
);
550 static void addlfilter(unsigned ac
, char **av
) {
551 dofilter(&peers
[0], &peers
[1], ac
, av
);
553 static void addrfilter(unsigned ac
, char **av
) {
554 dofilter(&peers
[1], &peers
[0], ac
, av
);
557 static void include(unsigned ac
, char **av
)
562 die(1, "syntax: include:FILE:...");
564 if ((fp
= fopen(*av
, "r")) == 0)
565 die(1, "fopen `%s': %s", *av
, strerror(errno
));
566 while (dstr_putline(&d
, fp
) != EOF
) {
575 const struct cmdtab
{
577 void (*func
)(unsigned /*ac*/, char **/
*av*/
);
580 { "include", include
},
581 { "filt", addfilter
},
582 { "lfilt", addlfilter
},
583 { "rfilt", addrfilter
},
584 { "next", nextfork
},
585 { "flood", addflood
},
586 { "lflood", addlflood
},
587 { "rflood", addrflood
},
593 static void parse(char *p
)
597 const struct cmdtab
*ct
;
605 } while (p
&& c
< AVMAX
- 1);
607 for (ct
= cmdtab
; ct
->name
; ct
++) {
608 if (strcmp(ct
->name
, v
[0]) == 0) {
609 ct
->func(c
- 1, v
+ 1);
613 die(1, "unknown command `%s'", v
[0]);
616 /*----- Main driver -------------------------------------------------------*/
618 static void version(FILE *fp
)
620 pquis(fp
, "$, TrIPE version " VERSION
"\n");
623 static void usage(FILE *fp
)
625 pquis(fp
, "Usage: $ [-k keyring] directive...\n");
628 static void help(FILE *fp
)
635 int main(int argc
, char *argv
[])
637 const char *kfname
= "keyring.pub";
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' },
653 if ((i
= mdwopt(argc
, argv
, "hvuk:", opt
, 0, 0, 0)) < 0)
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
));
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
++)
687 die(1, "need two peers");
694 /*----- That's all, folks -------------------------------------------------*/