diff options
-rw-r--r-- | anytun.cpp | 41 | ||||
-rw-r--r-- | options.cpp | 22 | ||||
-rw-r--r-- | options.h | 3 |
3 files changed, 53 insertions, 13 deletions
@@ -34,6 +34,7 @@ #include <fcntl.h> #include <pwd.h> #include <grp.h> +#include <sys/wait.h> #include <gcrypt.h> #include <cerrno> // for ENOMEM @@ -394,21 +395,34 @@ void daemonize() umask(027); } -void writePid(string const& pidFilename) +int execScript(string const& script, string const& ifname) { + pid_t pid; + pid = fork(); + if(!pid) { + int fd; + for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors + close(fd); + fd=open("/dev/null",O_RDWR); // stdin + dup(fd); // stdout + dup(fd); // stderr + return execl("/bin/sh", "/bin/sh", script.c_str(), ifname.c_str(), NULL); + } + int status = 0; + waitpid(pid, &status, 0); + return status; } int main(int argc, char* argv[]) { // std::cout << "anytun - secure anycast tunneling protocol" << std::endl; - if(!gOpt.parse(argc, argv)) - { + if(!gOpt.parse(argc, argv)) { gOpt.printUsage(); exit(-1); } - + cLog.msg(Log::PRIO_NOTICE) << "anytun started..."; - + std::ofstream pidFile; if(gOpt.getPidFile() != "") { pidFile.open(gOpt.getPidFile().c_str()); @@ -416,7 +430,7 @@ int main(int argc, char* argv[]) std::cout << "can't open pid file" << std::endl; } } - + std::string dev_type(gOpt.getDevType()); TunDevice dev(gOpt.getDevName().c_str(), dev_type=="" ? NULL : dev_type.c_str(), gOpt.getIfconfigParamLocal() =="" ? NULL : gOpt.getIfconfigParamLocal().c_str(), @@ -424,6 +438,10 @@ int main(int argc, char* argv[]) cLog.msg(Log::PRIO_NOTICE) << "dev created (opened)"; cLog.msg(Log::PRIO_NOTICE) << "dev opened - actual name is '" << dev.getActualName() << "'"; cLog.msg(Log::PRIO_NOTICE) << "dev type is '" << dev.getTypeString() << "'"; + if(gOpt.getPostUpScript() != "") { + int postup_ret = execScript(gOpt.getPostUpScript(), dev.getActualName()); + cLog.msg(Log::PRIO_NOTICE) << "post up script '" << gOpt.getPostUpScript() << "' returned " << postup_ret; + } if(gOpt.getChroot()) chrootAndDrop(gOpt.getChrootDir(), gOpt.getUsername()); @@ -444,7 +462,7 @@ int main(int argc, char* argv[]) src = new UDPPacketSource(gOpt.getLocalPort()); else src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort()); - + ConnectionList cl; ConnectToList connect_to = gOpt.getConnectTo(); SyncQueue queue; @@ -468,11 +486,10 @@ int main(int argc, char* argv[]) pthread_create(&syncListenerThread, NULL, syncListener, &p); std::list<pthread_t> connectThreads; - for(ConnectToList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) - { - connectThreads.push_back(pthread_t()); - ThreadParam * point = new ThreadParam(dev, *src, cl, queue,*it); - pthread_create(& connectThreads.back(), NULL, syncConnector, point); + for(ConnectToList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) { + connectThreads.push_back(pthread_t()); + ThreadParam * point = new ThreadParam(dev, *src, cl, queue,*it); + pthread_create(& connectThreads.back(), NULL, syncConnector, point); } int ret = sig.run(); diff --git a/options.cpp b/options.cpp index 86fd50a..ad70459 100644 --- a/options.cpp +++ b/options.cpp @@ -70,6 +70,7 @@ Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0)) dev_type_ = ""; ifconfig_param_local_ = ""; ifconfig_param_remote_netmask_ = ""; + post_up_script_ = ""; seq_window_size_ = 100; cipher_ = "aes-ctr"; kd_prf_ = "aes-ctr"; @@ -172,6 +173,7 @@ bool Options::parse(int argc, char* argv[]) 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_) + PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_) PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_) PARSE_SCALAR_PARAM("-m","--mux", mux_) PARSE_SCALAR_PARAM("-c","--cipher", cipher_) @@ -180,6 +182,7 @@ bool Options::parse(int argc, char* argv[]) PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_) PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_) PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue) + PARSE_CSLIST_PARAM("-X","--control-host", host_port_queue) else return false; } @@ -220,18 +223,21 @@ void Options::printUsage() std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl; std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl; std::cout << " remote hosts to sync with" << std::endl; + std::cout << " [-X|--control-host] <hostname|ip>:<port>"<< std::endl; + std::cout << " fetch the config from this host" << std::endl; std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl; std::cout << " [-o|--remote-port] <port> remote port" << std::endl; std::cout << " [-d|--dev] <name> device name" << std::endl; std::cout << " [-t|--type] <tun|tap> device type" << std::endl; std::cout << " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl; + std::cout << " [-x|--post-up-script] <script> script gets called after interface is created" << std::endl; std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl; std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl; std::cout << " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl; std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl; std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl; - std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl; +// std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl; std::cout << " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl; } @@ -255,6 +261,7 @@ void Options::printOptions() std::cout << "dev_type='" << dev_type_ << "'" << std::endl; std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl; std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl; + std::cout << "post_up_script='" << post_up_script_ << "'" << std::endl; std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl; std::cout << "mux_id='" << mux_ << "'" << std::endl; std::cout << "cipher='" << cipher_ << "'" << std::endl; @@ -512,6 +519,19 @@ Options& Options::setIfconfigParamRemoteNetmask(std::string i) return *this; } +std::string Options::getPostUpScript() +{ + Lock lock(mutex); + return post_up_script_; +} + +Options& Options::setPostUpScript(std::string p) +{ + Lock lock(mutex); + post_up_script_ = p; + return *this; +} + window_size_t Options::getSeqWindowSize() { return seq_window_size_; @@ -92,6 +92,8 @@ public: Options& setIfconfigParamLocal(std::string i); std::string getIfconfigParamRemoteNetmask(); Options& setIfconfigParamRemoteNetmask(std::string i); + std::string getPostUpScript(); + Options& setPostUpScript(std::string p); window_size_t getSeqWindowSize(); Options& setSeqWindowSize(window_size_t s); std::string getCipher(); @@ -146,6 +148,7 @@ private: std::string dev_type_; std::string ifconfig_param_local_; std::string ifconfig_param_remote_netmask_; + std::string post_up_script_; window_size_t seq_window_size_; std::string cipher_; std::string kd_prf_; |