/* * anytun * * The secure anycast tunneling protocol (satp) defines a protocol used * for communication between any combination of unicast and anycast * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel * mode and allows tunneling of every ETHER TYPE protocol (e.g. * ethernet, ip, arp ...). satp directly includes cryptography and * message authentication based on the methodes used by SRTP. It is * intended to deliver a generic, scaleable and secure solution for * tunneling and relaying of packets of any protocol. * * * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, * Christian Pointner * * This file is part of Anytun. * * Anytun is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * Anytun is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with anytun. If not, see . */ #include #include #include #include #include #include "datatypes.h" #include "options.h" #include "log.h" std::ostream& operator<<(std::ostream& stream, syntax_error const& error) { stream << "syntax error: " << error.what() << std::endl; stream << " "; for(u_int32_t i = 0; i < error.pos; ++i) stream << " "; return stream << "^"; } void OptionHost::init(std::string addrPort) { std::string origAddrPort(addrPort); size_t pos = addrPort.find_first_of("["); if(pos != std::string::npos && pos != 0) throw syntax_error(origAddrPort, pos); // an [ was found but not at the beginning; bool hasPort = false; if(pos != std::string::npos) { addrPort.erase(pos, 1); pos = addrPort.find_first_of("]"); if(pos == std::string::npos) throw syntax_error(origAddrPort, origAddrPort.length()); //no trailing ] although an leading [ was found if(pos < addrPort.length()-2) { if(addrPort[pos+1] != ':') throw syntax_error(origAddrPort, pos+2); // wrong port delimieter addrPort[pos+1] = '/'; hasPort = true; } else if(pos != addrPort.length()-1) throw syntax_error(origAddrPort, pos+2); // too few characters left addrPort.erase(pos, 1); } else { pos = addrPort.find_first_of(":"); if(pos != std::string::npos && pos == addrPort.find_last_of(":")) { // an ':' has been found and it is the only one -> assuming port present hasPort = true; addrPort[pos] = '/'; } } if(hasPort) { std::stringstream tmp_stream(addrPort); getline(tmp_stream, addr, '/'); if(!tmp_stream.good()) throw syntax_error(origAddrPort, addr.length()); tmp_stream >> port; } else { addr = addrPort; port = "2323"; // default sync port } } std::istream& operator>>(std::istream& stream, OptionHost& host) { std::string tmp; stream >> tmp; host.init(tmp); return stream; } void OptionRoute::init(std::string route) { std::stringstream tmp_stream(route); getline(tmp_stream, net_addr, '/'); if(!tmp_stream.good()) throw syntax_error(route, net_addr.length()); tmp_stream >> prefix_length; } std::istream& operator>>(std::istream& stream, OptionRoute& route) { std::string tmp; stream >> tmp; route.init(tmp); return stream; } Options* Options::inst = NULL; Mutex Options::instMutex; Options& gOpt = Options::instance(); Options& Options::instance() { Lock lock(instMutex); static instanceCleaner c; if(!inst) inst = new Options(); return *inst; } Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0)) { progname_ = "anytun"; daemonize_ = true; chroot_ = false; username_ = "nobody"; chroot_dir_ = "/var/run/anytun"; pid_file_ = ""; file_name_ = ""; bind_to_.addr = "127.0.0.1"; bind_to_.port = "2323"; local_.addr = ""; local_.port = "4444"; remote_.addr = ""; remote_.port = "4444"; local_sync_.addr = ""; local_sync_.port = ""; dev_name_ = ""; dev_type_ = ""; ifconfig_param_local_ = ""; ifconfig_param_remote_netmask_ = ""; post_up_script_ = ""; sender_id_ = 0; mux_ = 0; seq_window_size_ = 0; #ifndef NO_CRYPT cipher_ = "aes-ctr"; auth_algo_ = "sha1"; kd_prf_ = "aes-ctr"; #else cipher_ = "null"; auth_algo_ = "null"; kd_prf_ = "null"; #endif ld_kdr_ = 0; } Options::~Options() { } #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \ else if(str == SHORT || str == LONG) \ VALUE = true; #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \ else if(str == SHORT || str == LONG) \ VALUE = false; #define PARSE_SIGNED_INT_PARAM(SHORT, LONG, VALUE) \ else if(str == SHORT || str == LONG) \ { \ if(argc < 1) \ throw syntax_error(str, str.length()); \ std::stringstream tmp; \ tmp << argv[i+1]; \ tmp >> VALUE; \ argc--; \ i++; \ } #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \ else if(str == SHORT || str == LONG) \ { \ if(argc < 1) \ throw syntax_error(str, str.length()); \ if(argv[i+1][0] == '-') { \ u_int32_t pos = str.length() + 1; \ throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ } \ std::stringstream tmp; \ tmp << argv[i+1]; \ tmp >> VALUE; \ argc--; \ i++; \ } #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \ else if(str == SHORT || str == LONG) \ { \ if(argc < 1) \ throw syntax_error(str, str.length()); \ if(argc < 2) \ throw syntax_error(str.append(" ").append(argv[i+1]), str.length()); \ if(argv[i+1][0] == '-') { \ u_int32_t pos = str.length() + 1; \ throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ } \ if(argv[i+2][0] == '-') { \ u_int32_t pos = str.length() + 1 + strlen(argv[i+1]) + 1; \ throw syntax_error(str.append(" ").append(argv[i+1]).append(" ").append(argv[i+2]), pos); \ } \ std::stringstream tmp; \ tmp << argv[i+1] << " " << argv[i+2]; \ tmp >> VALUE1; \ tmp >> VALUE2; \ argc-=2; \ i+=2; \ } #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST, TYPE) \ else if(str == SHORT || str == LONG) \ { \ if(argc < 1) \ throw syntax_error(str, str.length()); \ if(argv[i+1][0] == '-') { \ u_int32_t pos = str.length() + 1; \ throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ } \ std::stringstream tmp(argv[i+1]); \ while (tmp.good()) \ { \ std::string tmp_line; \ getline(tmp,tmp_line,','); \ LIST.push_back(TYPE(tmp_line)); \ } \ argc--; \ i++; \ } #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \ else if(str == SHORT || str == LONG) \ { \ if(argc < 1) \ throw syntax_error(str, str.length()); \ if(argv[i+1][0] == '-') { \ u_int32_t pos = str.length() + 1; \ throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ } \ VALUE = Buffer(std::string(argv[i+1])); \ for(size_t j=0; j < strlen(argv[i+1]); ++j) \ argv[i+1][j] = '#'; \ argc--; \ i++; \ } #define PARSE_PHRASE_PARAM_SEC(SHORT, LONG, VALUE) \ else if(str == SHORT || str == LONG) \ { \ if(argc < 1) \ throw syntax_error(str, str.length()); \ if(argv[i+1][0] == '-') { \ u_int32_t pos = str.length() + 1; \ throw syntax_error(str.append(" ").append(argv[i+1]), pos); \ } \ std::stringstream tmp; \ VALUE = argv[i+1]; \ for(size_t j=0; j < strlen(argv[i+1]); ++j) \ argv[i+1][j] = '#'; \ argc--; \ i++; \ } bool Options::parse(int argc, char* argv[]) { WritersLock lock(mutex); progname_ = argv[0]; argc--; int32_t ld_kdr_tmp = ld_kdr_; for(int i=1; argc > 0; ++i) { std::string str(argv[i]); argc--; if(str == "-h" || str == "--help") return false; #ifndef NO_DAEMON PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_) PARSE_BOOL_PARAM("-C","--chroot", chroot_) PARSE_SCALAR_PARAM("-u","--username", username_) PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_) PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_) #endif // PARSE_SCALAR_PARAM("-f","--file", file_name_) // PARSE_SCALAR_PARAM("-X","--control-host", bind_to_) PARSE_SCALAR_PARAM("-i","--interface", local_.addr) PARSE_SCALAR_PARAM("-p","--port", local_.port) PARSE_SCALAR_PARAM("-r","--remote-host", remote_.addr) PARSE_SCALAR_PARAM("-o","--remote-port", remote_.port) PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_.addr) PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_.port) PARSE_CSLIST_PARAM("-M","--sync-hosts", remote_sync_hosts_, OptionHost) PARSE_CSLIST_PARAM("-X","--control-host", remote_sync_hosts_, OptionHost) PARSE_SCALAR_PARAM("-d","--dev", dev_name_) PARSE_SCALAR_PARAM("-t","--type", dev_type_) PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_, ifconfig_param_remote_netmask_) #ifndef NO_EXEC PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_) #endif #ifndef NO_ROUTING PARSE_CSLIST_PARAM("-R","--route", routes_, OptionRoute) #endif PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_) PARSE_SCALAR_PARAM("-m","--mux", mux_) PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_) #ifndef NO_CRYPT PARSE_SCALAR_PARAM("-c","--cipher", cipher_) PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_) PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_) PARSE_SIGNED_INT_PARAM("-l","--ld-kdr", ld_kdr_tmp) #ifndef NO_PASSPHRASE PARSE_PHRASE_PARAM_SEC("-E","--passphrase", passphrase_) #endif PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_) PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_) #endif else throw syntax_error(str, 0); } ld_kdr_ = static_cast(ld_kdr_tmp); if(cipher_ == "null" && auth_algo_ == "null") kd_prf_ = "null"; if((cipher_ != "null" || auth_algo_ != "null") && kd_prf_ == "null") cLog.msg(Log::PRIO_WARNING) << "using NULL key derivation with encryption and or authentication enabled!"; if(dev_name_ == "" && dev_type_ == "") dev_type_ = "tun"; return true; } void Options::printUsage() { std::cout << "USAGE:" << std::endl; std::cout << "anytun " << std::endl; std::cout << " [-h|--help] prints this..." << std::endl; #ifndef NO_DAEMON std::cout << " [-D|--nodaemonize] don't run in background" << std::endl; std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl; std::cout << " [-u|--username] if chroot change to this user" << std::endl; std::cout << " [-H|--chroot-dir] chroot to this directory" << std::endl; std::cout << " [-P|--write-pid] write pid to this file" << std::endl; #endif // std::cout << " [-f|--file] path to input file" << std::endl; // std::cout << " [-X|--control-host] < [:] | : >" << std::endl; // std::cout << " local tcp port and or ip address to bind to" << std::endl; std::cout << " [-i|--interface] local anycast ip address to bind to" << std::endl; std::cout << " [-p|--port] local anycast(data) port to bind to" << std::endl; std::cout << " [-r|--remote-host] remote host" << std::endl; std::cout << " [-o|--remote-port] remote port" << std::endl; std::cout << " [-I|--sync-interface] local unicast(sync) ip address to bind to" << std::endl; std::cout << " [-S|--sync-port] local unicast(sync) port to bind to" << std::endl; std::cout << " [-M|--sync-hosts] [:][,[:][...]]"<< std::endl; std::cout << " remote hosts to sync with" << std::endl; std::cout << " [-X|--control-host] [:]"<< std::endl; std::cout << " fetch the config from this host" << std::endl; std::cout << " [-d|--dev] device name" << std::endl; std::cout << " [-t|--type] device type" << std::endl; std::cout << " [-n|--ifconfig] the local address for the tun/tap device" << std::endl; std::cout << " the remote address(tun) or netmask(tap)" << std::endl; #ifndef NO_EXEC std::cout << " [-x|--post-up-script]