3 * $Id: mallory.c,v 1.1 2001/06/19 22:11:14 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.1 2001/06/19 22:11:14 mdw
33 * The beginnings of a malicious proxy for TrIPE.
37 /*----- Header files ------------------------------------------------------*/
48 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
58 #include <mLib/alloc.h>
59 #include <mLib/dstr.h>
60 #include <mLib/fdflags.h>
61 #include <mLib/mdwopt.h>
62 #include <mLib/quis.h>
63 #include <mLib/report.h>
68 #include <catacomb/key.h>
70 #include <catacomb/mp.h>
71 #include <catacomb/mpmont.h>
72 #include <catacomb/mprand.h>
73 #include <catacomb/dh.h>
75 #include <catacomb/noise.h>
76 #include <catacomb/rand.h>
77 #include <catacomb/rc4.h>
81 /*----- Data structures ---------------------------------------------------*/
90 typedef struct filter
{
93 void (*func
)(struct filter */
*f*/
, const octet */
*buf*/
, size_t /*sz*/);
97 typedef struct qnode
{
102 /*----- Static variables --------------------------------------------------*/
104 #define PKBUFSZ 65536
106 static sel_state sel
;
107 static peer peers
[2];
108 static unsigned npeer
= 0;
109 static key_file keys
;
112 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
113 #define RND(i) (rng->ops->range(rng, (i)))
115 /*----- Peer management ---------------------------------------------------*/
117 static void dopacket(int fd
, unsigned mode
, void *vv
)
121 int r
= read(fd
, buf
, sizeof(buf
));
123 printf("recv from `%s'\n", p
->name
);
128 static void addpeer(unsigned ac
, char **av
)
130 key_packstruct kps
[DH_PUBFETCHSZ
];
133 struct sockaddr_in sin
;
140 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
142 die(1, "enough peers already");
144 p
->name
= xstrdup(av
[0]);
145 kp
= key_fetchinit(dh_pubfetch
, kps
, &p
->kpub
);
146 e
= key_fetchbyname(kp
, &keys
, av
[0]);
149 die(1, "key_fetch `%s': %s", av
[0], key_strerror(e
));
150 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
151 die(1, "socket: %s", strerror(errno
));
152 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
153 memset(&sin
, 0, sizeof(sin
));
154 sin
.sin_family
= AF_INET
;
155 sin
.sin_addr
.s_addr
= INADDR_ANY
;
156 sin
.sin_port
= htons(atoi(av
[1]));
157 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
158 die(1, "bind: %s", strerror(errno
));
159 memset(&sin
, 0, sizeof(sin
));
160 sin
.sin_family
= AF_INET
;
161 if ((h
= gethostbyname(av
[2])) == 0)
162 die(1, "gethostbyname `%s'", av
[2]);
163 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
164 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
)))
165 die(1, "setsockopt: %s", strerror(errno
));
166 memcpy(&sin
.sin_addr
, h
->h_addr
, sizeof(sin
.sin_addr
));
167 sin
.sin_port
= htons(atoi(av
[3]));
168 if (connect(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
169 die(1, "connect: %s", strerror(errno
));
170 sel_initfile(&sel
, &p
->sf
, fd
, SEL_READ
, dopacket
, p
);
174 /*----- Fork filter -------------------------------------------------------*/
176 typedef struct forknode
{
177 struct forknode
*next
;
181 typedef struct forkfilt
{
186 static void dofork(filter
*f
, const octet
*buf
, size_t sz
)
188 forkfilt
*ff
= f
->state
;
193 for (fn
= ff
->fn
; fn
; fn
= fn
->next
) {
194 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
195 PASS(fn
->f
, buf
, sz
);
197 printf("fork branch %u of fork `%s'\n", i
++, ff
->name
);
198 PASS(f
->next
, buf
, sz
);
201 static void addfork(filter
*f
, unsigned ac
, char **av
)
205 die(1, "syntax: filt:fork:NAME");
206 ff
= CREATE(forkfilt
);
207 ff
->name
= xstrdup(av
[0]);
213 static void nextfork(unsigned ac
, char **av
)
221 for (i
= 0; i
< 2; i
++) {
223 for (f
= p
->f
; f
; f
= f
->next
) {
224 if (f
->func
!= dofork
)
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 -------------------------------------------------*/