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