Imported Upstream version 0.3.5
[anytun.git] / src / anytun-controld.cpp
1 /*
2  *  anytun
3  *
4  *  The secure anycast tunneling protocol (satp) defines a protocol used
5  *  for communication between any combination of unicast and anycast
6  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
7  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
8  *  ethernet, ip, arp ...). satp directly includes cryptography and
9  *  message authentication based on the methods used by SRTP.  It is
10  *  intended to deliver a generic, scaleable and secure solution for
11  *  tunneling and relaying of packets of any protocol.
12  *
13  *
14  *  Copyright (C) 2007-2014 Markus Grüneis, Othmar Gsenger, Erwin Nindl,
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
19  *  Anytun is free software: you can redistribute it and/or modify
20  *  it under the terms of the GNU General Public License as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
24  *  Anytun is distributed in the hope that it will be useful,
25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *  GNU General Public License for more details.
28  *
29  *  You should have received a copy of the GNU General Public License
30  *  along with Anytun.  If not, see <http://www.gnu.org/licenses/>.
31  *
32  *  In addition, as a special exception, the copyright holders give
33  *  permission to link the code of portions of this program with the
34  *  OpenSSL library under certain conditions as described in each
35  *  individual source file, and distribute linked combinations
36  *  including the two.
37  *  You must obey the GNU General Public License in all respects
38  *  for all of the code used other than OpenSSL.  If you modify
39  *  file(s) with this exception, you may extend this exception to your
40  *  version of the file(s), but you are not obligated to do so.  If you
41  *  do not wish to do so, delete this exception statement from your
42  *  version.  If you delete this exception statement from all source
43  *  files in the program, then also delete it here.
44  */
45
46 #include <iostream>
47 #include <fstream>
48 #include <string>
49
50 #include "datatypes.h"
51
52 #include "log.h"
53 #include "signalController.h"
54 #include "options.h"
55 #include "resolver.h"
56
57 #include "syncServer.h"
58 #if !defined(_MSC_VER) && !defined(MINGW)
59 # include "daemonService.h"
60 #else
61 # include "nullDaemon.h"
62 #endif
63 #include <vector>
64
65 std::list<std::string> config_;
66
67 void syncOnConnect(SyncTcpConnection* connptr)
68 {
69   for(std::list<std::string>::const_iterator it=config_.begin(); it!=config_.end(); ++it) {
70     connptr->Send(*it);
71   }
72 }
73
74 void syncListener()
75 {
76   boost::asio::io_service io_service;
77   try {
78     SyncServer server(gOpt.getBindToAddr(), gOpt.getBindToPort(), boost::bind(syncOnConnect, _1));
79     server.run();
80   } catch(std::runtime_error& e) {
81     cLog.msg(Log::PRIO_ERROR) << "sync listener thread died due to an uncaught runtime_error: " << e.what();
82   } catch(std::exception& e) {
83     cLog.msg(Log::PRIO_ERROR) << "sync listener thread died due to an uncaught exception: " << e.what();
84   }
85 }
86
87 int main(int argc, char* argv[])
88 {
89   DaemonService service;
90   try {
91     try {
92       if(!gOpt.parse(argc, argv)) {
93         exit(0);
94       }
95
96       StringList targets = gOpt.getLogTargets();
97       for(StringList::const_iterator it = targets.begin(); it != targets.end(); ++it) {
98         cLog.addTarget(*it);
99       }
100     } catch(syntax_error& e) {
101       std::cerr << e << std::endl;
102       gOpt.printUsage();
103       exit(-1);
104     }
105
106     cLog.msg(Log::PRIO_NOTICE) << "anytun-controld started...";
107     gOpt.parse_post(); // print warnings
108
109
110     std::ifstream file(gOpt.getFileName().c_str());
111     if(file.is_open()) {
112       std::string line;
113       while(!file.eof()) {
114         getline(file,line);
115         config_.push_back(line);
116       }
117       file.close();
118     } else {
119       std::cout << "ERROR: unable to open file!" << std::endl;
120       exit(-1);
121     }
122
123     service.initPrivs(gOpt.getUsername(), gOpt.getGroupname());
124     if(gOpt.getDaemonize()) {
125       service.daemonize();
126     }
127
128     if(gOpt.getChrootDir() != "") {
129       service.chroot(gOpt.getChrootDir());
130     }
131     service.dropPrivs();
132
133     gSignalController.init(service);
134     gResolver.init();
135
136     boost::thread* syncListenerThread;
137     syncListenerThread = new boost::thread(boost::bind(syncListener));
138     if(syncListenerThread) syncListenerThread->detach();
139
140     int ret = gSignalController.run();
141
142     return ret;
143   } catch(std::runtime_error& e) {
144     if(service.isDaemonized()) {
145       cLog.msg(Log::PRIO_ERROR) << "uncaught runtime error, exiting: " << e.what();
146     } else {
147       std::cout << "uncaught runtime error, exiting: " << e.what() << std::endl;
148     }
149   } catch(std::exception& e) {
150     if(service.isDaemonized()) {
151       cLog.msg(Log::PRIO_ERROR) << "uncaught exception, exiting: " << e.what();
152     } else {
153       std::cout << "uncaught exception, exiting: " << e.what() << std::endl;
154     }
155   }
156 }
157