diff options
author | Othmar Gsenger <otti@anytun.org> | 2008-11-17 23:42:12 +0000 |
---|---|---|
committer | Othmar Gsenger <otti@anytun.org> | 2008-11-17 23:42:12 +0000 |
commit | 4bb7e445a67c8a5057a1cadd2d6700c2f7728d26 (patch) | |
tree | 2618271cd8357f1997bd5bc0e2d6b6ca49676fee /src | |
parent | added OnConnect Callback (diff) |
fixed anytun-controld
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/anytun-controld.cpp | 86 | ||||
-rw-r--r-- | src/anytun.cpp | 1 | ||||
-rw-r--r-- | src/controldTcpConnection.cpp | 34 | ||||
-rw-r--r-- | src/daemon.hpp | 55 |
5 files changed, 77 insertions, 101 deletions
diff --git a/src/Makefile b/src/Makefile index 4e14925..5902ec6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -85,7 +85,7 @@ SYNCOBJS= syncServer.o \ ANYCTROBJS = signalController.o \ anyCtrOptions.o \ log.o \ - controldTcpConnection.o \ + syncTcpConnection.o \ syncServer.o ANYCONFOBJS = log.o \ diff --git a/src/anytun-controld.cpp b/src/anytun-controld.cpp index 2593a2b..e02f8e7 100644 --- a/src/anytun-controld.cpp +++ b/src/anytun-controld.cpp @@ -35,6 +35,7 @@ #include <fcntl.h> #include <pwd.h> #include <grp.h> +#include <string> #include "datatypes.h" @@ -43,10 +44,9 @@ #include "anyCtrOptions.h" #include "syncServer.h" -//#include "anyCtrSocket.h" -//#include "Sockets/ListenSocket.h" -//#include "Sockets/SocketHandler.h" +#include "daemon.hpp" +std::string filename; class ThreadParam { @@ -56,6 +56,21 @@ public: u_int16_t port; }; +void syncOnConnect(SyncTcpConnection * connptr) +{ + std::ifstream file( filename.c_str() ); + if( file.is_open() ) + { + std::string line; + while (! file.eof() ) + { + getline (file,line); + connptr->Send(line); + } + file.close(); + } +} + void syncListener(void* p ) { ThreadParam* param = reinterpret_cast<ThreadParam*>(p); @@ -63,7 +78,8 @@ void syncListener(void* p ) try { asio::io_service io_service; - SyncServer server(io_service,asio::ip::tcp::endpoint(asio::ip::tcp::v4(), param->port)); + SyncServer server(io_service,asio::ip::tcp::endpoint(asio::ip::tcp::v6(), param->port)); + server.onConnect=boost::bind(syncOnConnect,_1); io_service.run(); } catch (std::exception& e) @@ -73,59 +89,6 @@ void syncListener(void* p ) } -void chrootAndDrop(std::string const& chrootdir, std::string const& username) -{ - if (getuid() != 0) - { - std::cerr << "this programm has to be run as root in order to run in a chroot" << std::endl; - exit(-1); - } - - struct passwd *pw = getpwnam(username.c_str()); - if(pw) { - if(chroot(chrootdir.c_str())) - { - std::cerr << "can't chroot to " << chrootdir << std::endl; - exit(-1); - } - cLog.msg(Log::PRIO_NOTICE) << "we are in chroot jail (" << chrootdir << ") now" << std::endl; - chdir("/"); - if (initgroups(pw->pw_name, pw->pw_gid) || setgid(pw->pw_gid) || setuid(pw->pw_uid)) - { - std::cerr << "can't drop to user " << username << " " << pw->pw_uid << ":" << pw->pw_gid << std::endl; - exit(-1); - } - cLog.msg(Log::PRIO_NOTICE) << "dropped user to " << username << " " << pw->pw_uid << ":" << pw->pw_gid << std::endl; - } - else - { - std::cerr << "unknown user " << username << std::endl; - exit(-1); - } -} - -void daemonize() -{ - pid_t pid; - - pid = fork(); - if(pid) exit(0); - setsid(); - pid = fork(); - if(pid) exit(0); - -// std::cout << "running in background now..." << std::endl; - - int fd; -// for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors - for (fd=0;fd<=2;fd++) // close all file descriptors - close(fd); - fd=open("/dev/null",O_RDWR); // stdin - dup(fd); // stdout - dup(fd); // stderr - umask(027); -} - int main(int argc, char* argv[]) { if(!gOpt.parse(argc, argv)) @@ -168,19 +131,12 @@ int main(int argc, char* argv[]) ThreadParam p; p.addr = gOpt.getBindToAddr(); p.port = gOpt.getBindToPort(); + filename = gOpt.getFileName(); boost::thread * syncListenerThread; syncListenerThread = new boost::thread(boost::bind(syncListener,&p)); - syncListener(&p); -// pthread_t syncListenerThread; -// pthread_create(&syncListenerThread, NULL, syncListener, &p); - int ret = sig.run(); -// pthread_cancel(syncListenerThread); - -// pthread_join(syncListenerThread, NULL); - return ret; } diff --git a/src/anytun.cpp b/src/anytun.cpp index a2407e2..8504d90 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -75,7 +75,6 @@ #endif #include "threadParam.h" - #define MAX_PACKET_LENGTH 1600 #define SESSION_KEYLEN_AUTH 20 // TODO: hardcoded size diff --git a/src/controldTcpConnection.cpp b/src/controldTcpConnection.cpp deleted file mode 100644 index 658fff2..0000000 --- a/src/controldTcpConnection.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "syncTcpConnection.h" -#include <boost/bind.hpp> -#include <asio.hpp> - -#include <sstream> -#include <iostream> -#include <string> - - asio::ip::tcp::socket& SyncTcpConnection::socket() - { - return socket_; - } - -void SyncTcpConnection::start() -{ - //TODO send file content here - Send("Hello"); -} -void SyncTcpConnection::Send(std::string message) -{ - asio::async_write(socket_, asio::buffer(message), - boost::bind(&SyncTcpConnection::handle_write, shared_from_this(), - asio::placeholders::error, - asio::placeholders::bytes_transferred)); -} -SyncTcpConnection::SyncTcpConnection(asio::io_service& io_service) - : socket_(io_service) -{ -} - -void SyncTcpConnection::handle_write(const asio::error_code& /*error*/, - size_t /*bytes_transferred*/) -{ -} diff --git a/src/daemon.hpp b/src/daemon.hpp new file mode 100644 index 0000000..13c4132 --- /dev/null +++ b/src/daemon.hpp @@ -0,0 +1,55 @@ + +void chrootAndDrop(std::string const& chrootdir, std::string const& username) +{ + if (getuid() != 0) + { + std::cerr << "this programm has to be run as root in order to run in a chroot" << std::endl; + exit(-1); + } + + struct passwd *pw = getpwnam(username.c_str()); + if(pw) { + if(chroot(chrootdir.c_str())) + { + std::cerr << "can't chroot to " << chrootdir << std::endl; + exit(-1); + } + cLog.msg(Log::PRIO_NOTICE) << "we are in chroot jail (" << chrootdir << ") now" << std::endl; + chdir("/"); + if (initgroups(pw->pw_name, pw->pw_gid) || setgid(pw->pw_gid) || setuid(pw->pw_uid)) + { + std::cerr << "can't drop to user " << username << " " << pw->pw_uid << ":" << pw->pw_gid << std::endl; + exit(-1); + } + cLog.msg(Log::PRIO_NOTICE) << "dropped user to " << username << " " << pw->pw_uid << ":" << pw->pw_gid << std::endl; + } + else + { + std::cerr << "unknown user " << username << std::endl; + exit(-1); + } +} + +void daemonize() +{ + pid_t pid; + + pid = fork(); + if(pid) exit(0); + setsid(); + pid = fork(); + if(pid) exit(0); + +// std::cout << "running in background now..." << std::endl; + + int fd; +// for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors + for (fd=0;fd<=2;fd++) // close all file descriptors + close(fd); + fd=open("/dev/null",O_RDWR); // stdin + dup(fd); // stdout + dup(fd); // stderr + umask(027); +} + + |