Imported Upstream version 0.3.5
[anytun.git] / src / posix / posixDaemon.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 <poll.h>
47 #include <fcntl.h>
48 #include <pwd.h>
49 #include <grp.h>
50 #include <sys/wait.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53
54 #include "daemonService.h"
55 #include "log.h"
56 #include "options.h"
57 #include "anytunError.h"
58
59 DaemonService::DaemonService() : pw_(NULL), gr_(NULL), daemonized_(false)
60 {
61 }
62
63 void DaemonService::initPrivs(std::string const& username, std::string const& groupname)
64 {
65   if(username == "") {
66     return;
67   }
68
69   pw_ = getpwnam(username.c_str());
70   if(!pw_) {
71     AnytunError::throwErr() << "unknown user " << username;
72   }
73
74   if(groupname != "") {
75     gr_ = getgrnam(groupname.c_str());
76   } else {
77     gr_ = getgrgid(pw_->pw_gid);
78   }
79
80   if(!gr_) {
81     AnytunError::throwErr() << "unknown group " << groupname;
82   }
83 }
84
85 void DaemonService::dropPrivs()
86 {
87   if(!pw_ || !gr_) {
88     return;
89   }
90
91   if(setgid(gr_->gr_gid)) {
92     AnytunError::throwErr() << "setgid('" << gr_->gr_name << "') failed: " << AnytunErrno(errno);
93   }
94
95   gid_t gr_list[1];
96   gr_list[0] = gr_->gr_gid;
97   if(setgroups(1, gr_list)) {
98     AnytunError::throwErr() << "setgroups(['" << gr_->gr_name << "']) failed: " << AnytunErrno(errno);
99   }
100
101   if(setuid(pw_->pw_uid)) {
102     AnytunError::throwErr() << "setuid('" << pw_->pw_name << "') failed: " << AnytunErrno(errno);
103   }
104
105   cLog.msg(Log::PRIO_NOTICE) << "dropped privileges to " << pw_->pw_name << ":" << gr_->gr_name;
106 }
107
108 void DaemonService::chroot(std::string const& chrootdir)
109 {
110   if(getuid() != 0) {
111     AnytunError::throwErr() << "this program has to be run as root in order to run in a chroot";
112   }
113
114   if(::chroot(chrootdir.c_str())) {
115     AnytunError::throwErr() << "can't chroot to " << chrootdir;
116   }
117
118   cLog.msg(Log::PRIO_NOTICE) << "we are in chroot jail (" << chrootdir << ") now" << std::endl;
119   if(chdir("/")) {
120     AnytunError::throwErr() << "can't change to /";
121   }
122 }
123
124 /// TODO: this outstandignly ugly please and i really can't stress the please fix it asap!!!!!!!
125
126 std::ofstream pidFile; // FIXXXME no global variable
127
128 void DaemonService::daemonize()
129 {
130   //  std::ofstream pidFile;
131   if(gOpt.getPidFile() != "") {
132     pidFile.open(gOpt.getPidFile().c_str());
133     if(!pidFile.is_open()) {
134       AnytunError::throwErr() << "can't open pid file (" << gOpt.getPidFile() << "): " << AnytunErrno(errno);
135     }
136   }
137
138   pid_t pid;
139
140   pid = fork();
141   if(pid < 0) {
142     AnytunError::throwErr() << "daemonizing failed at fork(): " << AnytunErrno(errno) << ", exitting";
143   }
144
145   if(pid) { exit(0); }
146
147   umask(0);
148
149   if(setsid() < 0) {
150     AnytunError::throwErr() << "daemonizing failed at setsid(): " << AnytunErrno(errno) << ", exitting";
151   }
152
153   pid = fork();
154   if(pid < 0) {
155     AnytunError::throwErr() << "daemonizing failed at fork(): " << AnytunErrno(errno) << ", exitting";
156   }
157
158   if(pid) { exit(0); }
159
160   if((chdir("/")) < 0) {
161     AnytunError::throwErr() << "daemonizing failed at chdir(): " << AnytunErrno(errno) << ", exitting";
162   }
163
164   //  std::cout << "running in background now..." << std::endl;
165
166   int fd;
167   //  for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
168   for(fd=0; fd<=2; fd++) { // close all file descriptors
169     close(fd);
170   }
171   fd = open("/dev/null",O_RDWR);        // stdin
172   if(fd == -1) {
173     cLog.msg(Log::PRIO_WARNING) << "can't open /dev/null as stdin";
174   } else {
175     if(dup(fd) == -1) { // stdout
176       cLog.msg(Log::PRIO_WARNING) << "can't open /dev/null as stdout";
177     }
178     if(dup(fd) == -1) { // stderr
179       cLog.msg(Log::PRIO_WARNING) << "can't open /dev/null as stderr";
180     }
181   }
182
183   // FIXXXXME: write this pid to file (currently pid from posix/signhandler.hpp:77 is used)
184   //
185   //   if(pidFile.is_open()) {
186   //     pid_t pid = getpid();
187   //     pidFile << pid;
188   //     pidFile.close();
189   //   }
190
191   daemonized_ = true;
192 }
193
194 bool DaemonService::isDaemonized()
195 {
196   return daemonized_;
197 }