2 * curvetun - the cipherspace wormhole creator
3 * Part of the netsniff-ng project
4 * Copyright 2011 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Copyright 2011 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
8 * He used often to say there was only one Road; that it was like a great
9 * river: it's springs were at every doorstep and every path was it's
10 * tributary. "It's a dangerous business, Frodo, going out of your door,"
11 * he used to say. "You step into the Road, and if you don't keep your
12 * feet, there is no telling where you might be swept off to."
14 * -- The Lord of the Rings, Frodo about his uncle Bilbo Baggins,
15 * Chapter 'Three is Company'.
28 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/ptrace.h>
32 #include <sys/fsuid.h>
33 #include <netinet/in.h>
42 #include "ct_usermgmt.h"
43 #include "ct_servmgmt.h"
46 #include "crypto_verify_32.h"
47 #include "crypto_box_curve25519xsalsa20poly1305.h"
48 #include "crypto_scalarmult_curve25519.h"
49 #include "crypto_auth_hmacsha512256.h"
51 #define CURVETUN_ENTROPY_SOURCE "/dev/random"
53 extern void print_stun_probe(char *server
, uint16_t sport
, uint16_t tunport
);
65 volatile sig_atomic_t sigint
= 0;
67 static const char *short_options
= "kxc::svhp:t:d:uCS46DN";
68 static const struct option long_options
[] = {
69 {"client", optional_argument
, NULL
, 'c'},
70 {"dev", required_argument
, NULL
, 'd'},
71 {"port", required_argument
, NULL
, 'p'},
72 {"stun", required_argument
, NULL
, 't'},
73 {"keygen", no_argument
, NULL
, 'k'},
74 {"export", no_argument
, NULL
, 'x'},
75 {"dumpc", no_argument
, NULL
, 'C'},
76 {"dumps", no_argument
, NULL
, 'S'},
77 {"no-logging", no_argument
, NULL
, 'N'},
78 {"server", no_argument
, NULL
, 's'},
79 {"udp", no_argument
, NULL
, 'u'},
80 {"ipv4", no_argument
, NULL
, '4'},
81 {"ipv6", no_argument
, NULL
, '6'},
82 {"nofork", no_argument
, NULL
, 'D'},
83 {"version", no_argument
, NULL
, 'v'},
84 {"help", no_argument
, NULL
, 'h'},
88 static void signal_handler(int number
)
99 static void help(void)
101 printf("\ncurvetun %s, lightweight curve25519-based VPN/IP tunnel\n", VERSION_STRING
);
102 puts("http://www.netsniff-ng.org\n\n"
103 "Usage: curvetun [options]\n"
104 "Options, general:\n"
105 " -d|--dev <tun> Networking tunnel device, e.g. tun0\n"
106 " -p|--port <num> Server port number (mandatory)\n"
107 " -t|--stun <server> Show public IP/Port mapping via STUN\n"
108 " -c|--client[=alias] Client mode, server alias optional\n"
109 " -k|--keygen Generate public/private keypair\n"
110 " -x|--export Export your public data for remote servers\n"
111 " -C|--dumpc Dump parsed clients\n"
112 " -S|--dumps Dump parsed servers\n"
113 " -D|--nofork Do not daemonize\n"
114 " -s|--server Server mode, options follow below\n"
115 " -N|--no-logging Disable server logging (for better anonymity)\n"
116 " -u|--udp Use UDP as carrier instead of TCP\n"
117 " -4|--ipv4 Tunnel devices are IPv4\n"
118 " -6|--ipv6 Tunnel devices are IPv6\n"
119 " -v|--version Print version\n"
120 " -h|--help Print this help\n\n"
122 " See Documentation/Curvetun for a configuration example.\n"
123 " curvetun --server -4 -u -N --port 6666 --stun stunserver.org\n"
124 " curvetun --client=ethz\n\n"
125 " curvetun --keygen\n"
126 " curvetun --export\n"
128 " There is no default port specified, so that you are forced\n"
129 " to select your own! For client/server status messages see syslog!\n"
130 " This software is an experimental prototype intended for researchers.\n\n"
131 "Secret ingredient: 7647-14-5\n\n"
132 "Please report bugs to <bugs@netsniff-ng.org>\n"
133 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
134 "Swiss federal institute of technology (ETH Zurich)\n"
135 "License: GNU GPL version 2.0\n"
136 "This is free software: you are free to change and redistribute it.\n"
137 "There is NO WARRANTY, to the extent permitted by law.\n");
141 static void version(void)
143 printf("\ncurvetun %s, lightweight curve25519-based VPN/IP tunnel\n", VERSION_STRING
);
144 puts("http://www.netsniff-ng.org\n\n"
145 "Please report bugs to <bugs@netsniff-ng.org>\n"
146 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
147 "Swiss federal institute of technology (ETH Zurich)\n"
148 "License: GNU GPL version 2.0\n"
149 "This is free software: you are free to change and redistribute it.\n"
150 "There is NO WARRANTY, to the extent permitted by law.\n");
154 static void check_file_or_die(char *home
, char *file
, int maybeempty
)
159 memset(path
, 0, sizeof(path
));
160 slprintf(path
, sizeof(path
), "%s/%s", home
, file
);
163 panic("No such file %s! Type --help for further information\n",
166 if (!S_ISREG(st
.st_mode
))
167 panic("%s is not a regular file!\n", path
);
169 if ((st
.st_mode
& ~S_IFREG
) != (S_IRUSR
| S_IWUSR
))
170 panic("You have set too many permissions on %s (%o)!\n",
173 if (maybeempty
== 0 && st
.st_size
== 0)
174 panic("%s is empty!\n", path
);
177 static void check_config_exists_or_die(char *home
)
180 panic("No home dir specified!\n");
182 check_file_or_die(home
, FILE_CLIENTS
, 1);
183 check_file_or_die(home
, FILE_SERVERS
, 1);
184 check_file_or_die(home
, FILE_PRIVKEY
, 0);
185 check_file_or_die(home
, FILE_PUBKEY
, 0);
186 check_file_or_die(home
, FILE_USERNAM
, 0);
189 static char *fetch_home_dir(void)
191 char *home
= getenv("HOME");
193 panic("No HOME defined!\n");
197 static void write_username(char *home
)
203 memset(path
, 0, sizeof(path
));
204 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_USERNAM
);
206 printf("Username: [%s] ", getenv("USER"));
209 memset(user
, 0, sizeof(user
));
210 if (fgets(user
, sizeof(user
), stdin
) == NULL
)
211 panic("Could not read from stdin!\n");
212 user
[sizeof(user
) - 1] = 0;
213 user
[strlen(user
) - 1] = 0; /* omit last \n */
214 if (strlen(user
) == 0)
215 strlcpy(user
, getenv("USER"), sizeof(user
));
217 fd
= open_or_die_m(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
219 ret
= write(fd
, user
, strlen(user
));
220 if (ret
!= strlen(user
))
221 panic("Could not write username!\n");
225 printf("Username written to %s!\n", path
);
228 static void create_curvedir(char *home
)
233 memset(path
, 0, sizeof(path
));
234 slprintf(path
, sizeof(path
), "%s/%s", home
, ".curvetun/");
238 ret
= mkdir(path
, S_IRWXU
);
239 if (ret
< 0 && errno
!= EEXIST
)
240 panic("Cannot create curvetun dir!\n");
242 printf("curvetun directory %s created!\n", path
);
243 /* We also create empty files for clients and servers! */
245 memset(path
, 0, sizeof(path
));
246 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_CLIENTS
);
248 create_or_die(path
, S_IRUSR
| S_IWUSR
);
250 printf("Empty client file written to %s!\n", path
);
252 memset(path
, 0, sizeof(path
));
253 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_SERVERS
);
255 create_or_die(path
, S_IRUSR
| S_IWUSR
);
257 printf("Empty server file written to %s!\n", path
);
260 static void create_keypair(char *home
)
264 unsigned char publickey
[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
] = { 0 };
265 unsigned char secretkey
[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES
] = { 0 };
267 const char * errstr
= NULL
;
269 printf("Reading from %s (this may take a while) ...\n", CURVETUN_ENTROPY_SOURCE
);
271 fd
= open_or_die(CURVETUN_ENTROPY_SOURCE
, O_RDONLY
);
273 ret
= read_exact(fd
, secretkey
, sizeof(secretkey
), 0);
274 if (ret
!= sizeof(secretkey
)) {
276 errstr
= "Cannot read from "CURVETUN_ENTROPY_SOURCE
"!\n";
282 crypto_scalarmult_curve25519_base(publickey
, secretkey
);
284 memset(path
, 0, sizeof(path
));
285 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PUBKEY
);
287 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
290 errstr
= "Cannot open pubkey file!\n";
294 ret
= write(fd
, publickey
, sizeof(publickey
));
295 if (ret
!= sizeof(publickey
)) {
297 errstr
= "Cannot write public key!\n";
303 printf("Public key written to %s!\n", path
);
305 memset(path
, 0, sizeof(path
));
306 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PRIVKEY
);
308 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
311 errstr
= "Cannot open privkey file!\n";
315 ret
= write(fd
, secretkey
, sizeof(secretkey
));
316 if (ret
!= sizeof(secretkey
)) {
318 errstr
= "Cannot write private key!\n";
324 xmemset(publickey
, 0, sizeof(publickey
));
325 xmemset(secretkey
, 0, sizeof(secretkey
));
328 panic("%s: %s", errstr
, strerror(errno
));
330 printf("Private key written to %s!\n", path
);
333 static void check_config_keypair_or_die(char *home
)
337 const char * errstr
= NULL
;
338 unsigned char publickey
[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
];
339 unsigned char publicres
[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
];
340 unsigned char secretkey
[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES
];
343 memset(path
, 0, sizeof(path
));
344 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PRIVKEY
);
346 fd
= open(path
, O_RDONLY
);
349 errstr
= "Cannot open privkey file!\n";
353 ret
= read(fd
, secretkey
, sizeof(secretkey
));
354 if (ret
!= sizeof(secretkey
)) {
356 errstr
= "Cannot read private key!\n";
362 memset(path
, 0, sizeof(path
));
363 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PUBKEY
);
365 fd
= open(path
, O_RDONLY
);
368 errstr
= "Cannot open pubkey file!\n";
372 ret
= read(fd
, publickey
, sizeof(publickey
));
373 if (ret
!= sizeof(publickey
)) {
375 errstr
= "Cannot read public key!\n";
379 crypto_scalarmult_curve25519_base(publicres
, secretkey
);
381 err
= crypto_verify_32(publicres
, publickey
);
384 errstr
= "WARNING: your keypair is corrupted!!! You need to "
385 "generate new keys!!!\n";
391 xmemset(publickey
, 0, sizeof(publickey
));
392 xmemset(publicres
, 0, sizeof(publicres
));
393 xmemset(secretkey
, 0, sizeof(secretkey
));
396 panic("%s: %s\n", errstr
, strerror(errno
));
399 static int main_keygen(char *home
)
401 create_curvedir(home
);
402 write_username(home
);
403 create_keypair(home
);
404 check_config_keypair_or_die(home
);
409 static int main_export(char *home
)
413 char path
[PATH_MAX
], tmp
[64];
415 check_config_exists_or_die(home
);
416 check_config_keypair_or_die(home
);
418 printf("Your exported public information:\n\n");
420 memset(path
, 0, sizeof(path
));
421 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_USERNAM
);
423 fd
= open_or_die(path
, O_RDONLY
);
425 while ((ret
= read(fd
, tmp
, sizeof(tmp
))) > 0) {
426 ret
= write(STDOUT_FILENO
, tmp
, ret
);
433 memset(path
, 0, sizeof(path
));
434 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PUBKEY
);
436 fd
= open_or_die(path
, O_RDONLY
);
438 ret
= read(fd
, tmp
, sizeof(tmp
));
439 if (ret
!= crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
)
440 panic("Cannot read public key!\n");
442 for (i
= 0; i
< ret
; ++i
)
444 printf("%02x\n\n", (unsigned char) tmp
[i
]);
446 printf("%02x:", (unsigned char) tmp
[i
]);
454 static int main_dumpc(char *home
)
456 check_config_exists_or_die(home
);
457 check_config_keypair_or_die(home
);
459 printf("Your clients:\n\n");
461 parse_userfile_and_generate_user_store_or_die(home
);
465 destroy_user_store();
472 static int main_dumps(char *home
)
474 check_config_exists_or_die(home
);
475 check_config_keypair_or_die(home
);
477 printf("Your servers:\n\n");
479 parse_userfile_and_generate_serv_store_or_die(home
);
483 destroy_serv_store();
490 static void daemonize(const char *lockfile
)
493 mode_t lperm
= S_IRWXU
| S_IRGRP
| S_IXGRP
; /* 0750 */
500 panic("Cannot daemonize: %s", strerror(errno
));
507 lfp
= open(lockfile
, O_RDWR
| O_CREAT
| O_EXCL
,
508 S_IRUSR
| S_IWUSR
| S_IRGRP
);
510 syslog_panic("Cannot create lockfile at %s! "
511 "curvetun server already running?\n",
514 slprintf(pidstr
, sizeof(pidstr
), "%u", getpid());
515 if (write(lfp
, pidstr
, strlen(pidstr
)) <= 0)
516 syslog_panic("Could not write pid to pidfile %s",
523 static int main_client(char *home
, char *dev
, char *alias
, int daemon
)
528 check_config_exists_or_die(home
);
529 check_config_keypair_or_die(home
);
531 parse_userfile_and_generate_serv_store_or_die(home
);
533 get_serv_store_entry_by_alias(alias
, alias
? strlen(alias
) + 1 : 0,
535 if (!host
|| !port
|| udp
< 0)
536 panic("Did not find alias/entry in configuration!\n");
538 printf("Using [%s] -> %s:%s via %s as endpoint!\n",
539 alias
? : "default", host
, port
, udp
? "udp" : "tcp");
543 ret
= client_main(home
, dev
, host
, port
, udp
);
545 destroy_serv_store();
550 static int main_server(char *home
, char *dev
, char *port
, int udp
,
551 int ipv4
, int daemon
, int log
)
555 check_config_exists_or_die(home
);
556 check_config_keypair_or_die(home
);
561 ret
= server_main(home
, dev
, port
, udp
, ipv4
, log
);
568 int main(int argc
, char **argv
)
570 int ret
= 0, c
, opt_index
, udp
= 0, ipv4
= -1, daemon
= 1, log
= 1;
571 char *port
= NULL
, *stun
= NULL
, *dev
= NULL
, *home
= NULL
, *alias
= NULL
;
572 enum working_mode wmode
= MODE_UNKNOW
;
577 home
= fetch_home_dir();
579 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
580 &opt_index
)) != EOF
) {
605 alias
= xstrdup(optarg
);
609 dev
= xstrdup(optarg
);
630 stun
= xstrdup(optarg
);
633 port
= xstrdup(optarg
);
641 panic("Option -%c requires an argument!\n",
645 printf("Unknown option character `0x%X\'!\n", optopt
);
656 register_signal(SIGINT
, signal_handler
);
657 register_signal(SIGHUP
, signal_handler
);
658 register_signal(SIGTERM
, signal_handler
);
659 register_signal(SIGPIPE
, signal_handler
);
661 curve25519_selftest();
665 ret
= main_keygen(home
);
668 ret
= main_export(home
);
671 ret
= main_dumpc(home
);
674 ret
= main_dumps(home
);
677 ret
= main_client(home
, dev
, alias
, daemon
);
681 panic("No port specified!\n");
683 print_stun_probe(stun
, 3478, strtoul(port
, NULL
, 10));
684 ret
= main_server(home
, dev
, port
, udp
, ipv4
, daemon
, log
);