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.
19 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ptrace.h>
23 #include <sys/fsuid.h>
24 #include <netinet/in.h>
38 #include "curvetun_mgmt.h"
53 volatile sig_atomic_t sigint
= 0;
55 static const char *short_options
= "kxc::svhp:t:d:uCS46DN";
56 static const struct option long_options
[] = {
57 {"client", optional_argument
, NULL
, 'c'},
58 {"dev", required_argument
, NULL
, 'd'},
59 {"port", required_argument
, NULL
, 'p'},
60 {"stun", required_argument
, NULL
, 't'},
61 {"keygen", no_argument
, NULL
, 'k'},
62 {"export", no_argument
, NULL
, 'x'},
63 {"dumpc", no_argument
, NULL
, 'C'},
64 {"dumps", no_argument
, NULL
, 'S'},
65 {"no-logging", no_argument
, NULL
, 'N'},
66 {"server", no_argument
, NULL
, 's'},
67 {"udp", no_argument
, NULL
, 'u'},
68 {"ipv4", no_argument
, NULL
, '4'},
69 {"ipv6", no_argument
, NULL
, '6'},
70 {"nofork", no_argument
, NULL
, 'D'},
71 {"version", no_argument
, NULL
, 'v'},
72 {"help", no_argument
, NULL
, 'h'},
76 static const char *copyright
=
77 "Please report bugs to <netsniff-ng@googlegroups.com>\n"
78 "Copyright (C) 2011-2013 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,\n"
79 "Swiss federal institute of technology (ETH Zurich)\n"
80 "License: GNU GPL version 2.0\n"
81 "This is free software: you are free to change and redistribute it.\n"
82 "There is NO WARRANTY, to the extent permitted by law.";
84 static void signal_handler(int number
)
96 static void __noreturn
help(void)
98 printf("curvetun %s, lightweight curve25519-based IP tunnel\n", VERSION_STRING
);
99 puts("http://www.netsniff-ng.org\n\n"
100 "Usage: curvetun [options]\n"
101 "Options, general:\n"
102 " -d|--dev <tun> Networking tunnel device, e.g. tun0\n"
103 " -p|--port <num> Server port number (mandatory)\n"
104 " -t|--stun <server> Show public IP/Port mapping via STUN\n"
105 " -c|--client[=alias] Client mode, server alias optional\n"
106 " -k|--keygen Generate public/private keypair\n"
107 " -x|--export Export your public data for remote servers\n"
108 " -C|--dumpc Dump parsed clients\n"
109 " -S|--dumps Dump parsed servers\n"
110 " -D|--nofork Do not daemonize\n"
111 " -s|--server Server mode, options follow below\n"
112 " -N|--no-logging Disable server logging (for better anonymity)\n"
113 " -u|--udp Use UDP as carrier instead of TCP\n"
114 " -4|--ipv4 Tunnel devices are IPv4\n"
115 " -6|--ipv6 Tunnel devices are IPv6\n"
116 " -v|--version Print version and exit\n"
117 " -h|--help Print this help and exit\n\n"
119 " See curvetun's man page for a configuration example.\n"
120 " curvetun --server -4 -u -N --port 6666 --stun stunserver.org\n"
121 " curvetun --client=ethz\n\n"
122 " curvetun --keygen\n"
123 " curvetun --export\n"
125 " There is no default port specified, so that you are forced\n"
126 " to select your own! For client/server status messages see syslog!\n"
127 " This software is an experimental prototype intended for researchers.\n\n"
128 "Secret ingredient: 7647-14-5\n\n");
133 static void __noreturn
version(void)
135 printf("curvetun %s, Git id: %s\n", VERSION_LONG
, GITVERSION
);
136 puts("lightweight curve25519-based IP tunnel\n"
137 "Note: Einstein-Rosen bridge not yet supported\n"
138 "http://www.netsniff-ng.org\n\n");
143 static void check_file_or_die(char *home
, char *file
, int maybeempty
)
148 memset(path
, 0, sizeof(path
));
149 slprintf(path
, sizeof(path
), "%s/%s", home
, file
);
152 panic("No such file %s! Type --help for further information\n",
155 if (!S_ISREG(st
.st_mode
))
156 panic("%s is not a regular file!\n", path
);
158 if ((st
.st_mode
& ~S_IFREG
) != (S_IRUSR
| S_IWUSR
))
159 panic("You have set too many permissions on %s (%o)!\n",
162 if (maybeempty
== 0 && st
.st_size
== 0)
163 panic("%s is empty!\n", path
);
166 static void check_config_exists_or_die(char *home
)
169 panic("No home dir specified!\n");
171 check_file_or_die(home
, FILE_CLIENTS
, 1);
172 check_file_or_die(home
, FILE_SERVERS
, 1);
173 check_file_or_die(home
, FILE_PRIVKEY
, 0);
174 check_file_or_die(home
, FILE_PUBKEY
, 0);
175 check_file_or_die(home
, FILE_USERNAM
, 0);
178 static char *fetch_home_dir(void)
180 char *home
= getenv("HOME");
182 panic("No HOME defined!\n");
186 static void write_username(char *home
)
192 memset(path
, 0, sizeof(path
));
193 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_USERNAM
);
195 printf("Username: [%s] ", getenv("USER"));
198 memset(user
, 0, sizeof(user
));
199 if (fgets(user
, sizeof(user
), stdin
) == NULL
)
200 panic("Could not read from stdin!\n");
201 user
[sizeof(user
) - 1] = 0;
202 user
[strlen(user
) - 1] = 0; /* omit last \n */
203 if (strlen(user
) == 0)
204 strlcpy(user
, getenv("USER"), sizeof(user
));
206 fd
= open_or_die_m(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
208 ret
= write(fd
, user
, strlen(user
));
209 if (ret
!= strlen(user
))
210 panic("Could not write username!\n");
214 printf("Username written to %s!\n", path
);
217 static void create_curvedir(char *home
)
222 memset(path
, 0, sizeof(path
));
223 slprintf(path
, sizeof(path
), "%s/%s", home
, ".curvetun/");
227 ret
= mkdir(path
, S_IRWXU
);
228 if (ret
< 0 && errno
!= EEXIST
)
229 panic("Cannot create curvetun dir!\n");
231 printf("curvetun directory %s created!\n", path
);
232 /* We also create empty files for clients and servers! */
234 memset(path
, 0, sizeof(path
));
235 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_CLIENTS
);
237 create_or_die(path
, S_IRUSR
| S_IWUSR
);
239 printf("Empty client file written to %s!\n", path
);
241 memset(path
, 0, sizeof(path
));
242 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_SERVERS
);
244 create_or_die(path
, S_IRUSR
| S_IWUSR
);
246 printf("Empty server file written to %s!\n", path
);
249 static void create_keypair(char *home
)
253 unsigned char publickey
[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
] = { 0 };
254 unsigned char secretkey
[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES
] = { 0 };
256 const char * errstr
= NULL
;
258 printf("Reading from %s (this may take a while) ...\n", HIG_ENTROPY_SOURCE
);
260 gen_key_bytes(secretkey
, sizeof(secretkey
));
261 crypto_scalarmult_curve25519_base(publickey
, secretkey
);
263 memset(path
, 0, sizeof(path
));
264 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PUBKEY
);
266 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
269 errstr
= "Cannot open pubkey file!\n";
273 ret
= write(fd
, publickey
, sizeof(publickey
));
274 if (ret
!= sizeof(publickey
)) {
276 errstr
= "Cannot write public key!\n";
282 printf("Public key written to %s!\n", path
);
284 memset(path
, 0, sizeof(path
));
285 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PRIVKEY
);
287 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
290 errstr
= "Cannot open privkey file!\n";
294 ret
= write(fd
, secretkey
, sizeof(secretkey
));
295 if (ret
!= sizeof(secretkey
)) {
297 errstr
= "Cannot write private key!\n";
303 xmemset(publickey
, 0, sizeof(publickey
));
304 xmemset(secretkey
, 0, sizeof(secretkey
));
307 panic("%s: %s", errstr
, strerror(errno
));
309 printf("Private key written to %s!\n", path
);
312 static void check_config_keypair_or_die(char *home
)
316 const char * errstr
= NULL
;
317 unsigned char publickey
[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
];
318 unsigned char publicres
[crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
];
319 unsigned char secretkey
[crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES
];
322 memset(path
, 0, sizeof(path
));
323 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PRIVKEY
);
325 fd
= open(path
, O_RDONLY
);
328 errstr
= "Cannot open privkey file!\n";
332 ret
= read(fd
, secretkey
, sizeof(secretkey
));
333 if (ret
!= sizeof(secretkey
)) {
335 errstr
= "Cannot read private key!\n";
341 memset(path
, 0, sizeof(path
));
342 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PUBKEY
);
344 fd
= open(path
, O_RDONLY
);
347 errstr
= "Cannot open pubkey file!\n";
351 ret
= read(fd
, publickey
, sizeof(publickey
));
352 if (ret
!= sizeof(publickey
)) {
354 errstr
= "Cannot read public key!\n";
358 crypto_scalarmult_curve25519_base(publicres
, secretkey
);
360 err
= crypto_verify_32(publicres
, publickey
);
363 errstr
= "WARNING: your keypair is corrupted!!! You need to "
364 "generate new keys!!!\n";
370 xmemset(publickey
, 0, sizeof(publickey
));
371 xmemset(publicres
, 0, sizeof(publicres
));
372 xmemset(secretkey
, 0, sizeof(secretkey
));
375 panic("%s: %s\n", errstr
, strerror(errno
));
378 static int main_keygen(char *home
)
380 create_curvedir(home
);
381 write_username(home
);
382 create_keypair(home
);
383 check_config_keypair_or_die(home
);
388 static int main_export(char *home
)
392 char path
[PATH_MAX
], tmp
[64];
394 check_config_exists_or_die(home
);
395 check_config_keypair_or_die(home
);
397 printf("Your exported public information:\n\n");
399 memset(path
, 0, sizeof(path
));
400 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_USERNAM
);
402 fd
= open_or_die(path
, O_RDONLY
);
404 while ((ret
= read(fd
, tmp
, sizeof(tmp
))) > 0) {
405 ret
= write(STDOUT_FILENO
, tmp
, ret
);
412 memset(path
, 0, sizeof(path
));
413 slprintf(path
, sizeof(path
), "%s/%s", home
, FILE_PUBKEY
);
415 fd
= open_or_die(path
, O_RDONLY
);
417 ret
= read(fd
, tmp
, sizeof(tmp
));
418 if (ret
!= crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
)
419 panic("Cannot read public key!\n");
421 for (i
= 0; i
< ret
; ++i
)
423 printf("%02x\n\n", (unsigned char) tmp
[i
]);
425 printf("%02x:", (unsigned char) tmp
[i
]);
433 static int main_dumpc(char *home
)
435 check_config_exists_or_die(home
);
436 check_config_keypair_or_die(home
);
438 printf("Your clients:\n\n");
440 parse_userfile_and_generate_user_store_or_die(home
);
444 destroy_user_store();
451 static int main_dumps(char *home
)
453 check_config_exists_or_die(home
);
454 check_config_keypair_or_die(home
);
456 printf("Your servers:\n\n");
458 parse_userfile_and_generate_serv_store_or_die(home
);
462 destroy_serv_store();
469 static void daemonize(const char *lockfile
)
472 mode_t lperm
= S_IRWXU
| S_IRGRP
| S_IXGRP
; /* 0750 */
479 panic("Cannot daemonize: %s", strerror(errno
));
486 lfp
= open(lockfile
, O_RDWR
| O_CREAT
| O_EXCL
,
487 S_IRUSR
| S_IWUSR
| S_IRGRP
);
489 syslog_panic("Cannot create lockfile at %s! "
490 "curvetun server already running?\n",
493 slprintf(pidstr
, sizeof(pidstr
), "%u", getpid());
494 if (write(lfp
, pidstr
, strlen(pidstr
)) <= 0)
495 syslog_panic("Could not write pid to pidfile %s",
502 static int main_client(char *home
, char *dev
, char *alias
, int daemon
)
507 check_config_exists_or_die(home
);
508 check_config_keypair_or_die(home
);
510 parse_userfile_and_generate_serv_store_or_die(home
);
512 get_serv_store_entry_by_alias(alias
, alias
? strlen(alias
) + 1 : 0,
514 if (!host
|| !port
|| udp
< 0)
515 panic("Did not find alias/entry in configuration!\n");
517 printf("Using [%s] -> %s:%s via %s as endpoint!\n",
518 alias
? : "default", host
, port
, udp
? "udp" : "tcp");
522 ret
= client_main(home
, dev
, host
, port
, udp
);
524 destroy_serv_store();
529 static int main_server(char *home
, char *dev
, char *port
, int udp
,
530 int ipv4
, int daemon
, int log
)
534 check_config_exists_or_die(home
);
535 check_config_keypair_or_die(home
);
540 ret
= server_main(home
, dev
, port
, udp
, ipv4
, log
);
547 int main(int argc
, char **argv
)
549 int ret
= 0, c
, udp
= 0, ipv4
= -1, daemon
= 1, log
= 1;
550 char *port
= NULL
, *stun
= NULL
, *dev
= NULL
, *home
= NULL
, *alias
= NULL
;
551 enum working_mode wmode
= MODE_UNKNOW
;
556 home
= fetch_home_dir();
558 while ((c
= getopt_long(argc
, argv
, short_options
, long_options
,
584 alias
= xstrdup(optarg
);
588 dev
= xstrdup(optarg
);
609 stun
= xstrdup(optarg
);
612 port
= xstrdup(optarg
);
620 panic("Option -%c requires an argument!\n",
624 printf("Unknown option character `0x%X\'!\n", optopt
);
635 register_signal(SIGINT
, signal_handler
);
636 register_signal(SIGHUP
, signal_handler
);
637 register_signal(SIGTERM
, signal_handler
);
638 register_signal(SIGPIPE
, signal_handler
);
640 curve25519_selftest();
644 ret
= main_keygen(home
);
647 ret
= main_export(home
);
650 ret
= main_dumpc(home
);
653 ret
= main_dumps(home
);
656 ret
= main_client(home
, dev
, alias
, daemon
);
660 panic("No port specified!\n");
662 print_stun_probe(stun
, 3478, strtoul(port
, NULL
, 10));
663 ret
= main_server(home
, dev
, port
, udp
, ipv4
, daemon
, log
);