Imported Upstream version 0.3
[anytun.git] / src / daemon.hpp
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-2008 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 version 3 as
21  *  published by the Free Software Foundation.
22  *
23  *  Anytun is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27  *
28  *  You should have received a copy of the GNU General Public License
29  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32 #ifndef _DAEMON_HPP
33 #define _DAEMON_HPP
34 #ifndef NO_DAEMON
35
36 #include <poll.h>
37 #include <fcntl.h>
38 #include <pwd.h>
39 #include <grp.h>
40 #include <sys/wait.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43
44 #include "log.h"
45 #include "anytunError.h"
46
47 #ifndef NO_PRIVDROP
48 class PrivInfo
49 {
50 public:
51   PrivInfo(std::string const& username, std::string const& groupname)
52   {
53     pw_ = NULL;
54     gr_ = NULL;
55     
56     if(username == "")
57       return;
58
59     pw_ = getpwnam(username.c_str());
60     if(!pw_)
61       AnytunError::throwErr() << "unkown user " << username;
62     
63     if(groupname != "")
64       gr_ = getgrnam(groupname.c_str());
65     else
66       gr_ = getgrgid(pw_->pw_gid);
67     
68     if(!gr_)
69       AnytunError::throwErr() << "unkown group " << groupname;
70   }
71
72   void drop()
73   {
74     if(!pw_ || !gr_)
75       return;
76
77     if(setgid(gr_->gr_gid))
78       AnytunError::throwErr() << "setgid('" << gr_->gr_name << "') failed: " << AnytunErrno(errno);
79     
80     gid_t gr_list[1];
81     gr_list[0] = gr_->gr_gid;
82     if(setgroups (1, gr_list))
83       AnytunError::throwErr() << "setgroups(['" << gr_->gr_name << "']) failed: " << AnytunErrno(errno);
84     
85     if(setuid(pw_->pw_uid))
86       AnytunError::throwErr() << "setuid('" << pw_->pw_name << "') failed: " << AnytunErrno(errno);
87     
88     cLog.msg(Log::PRIO_NOTICE) << "dropped privileges to " << pw_->pw_name << ":" << gr_->gr_name;
89   }
90
91 private:
92   struct passwd* pw_;
93   struct group* gr_;
94 };
95 #endif
96
97 void do_chroot(std::string const& chrootdir)
98 {
99   if (getuid() != 0)
100     AnytunError::throwErr() << "this programm has to be run as root in order to run in a chroot";
101
102   if(chroot(chrootdir.c_str()))
103     AnytunError::throwErr() << "can't chroot to " << chrootdir;
104
105   cLog.msg(Log::PRIO_NOTICE) << "we are in chroot jail (" << chrootdir << ") now" << std::endl;
106   if(chdir("/"))
107     AnytunError::throwErr() << "can't change to /";
108 }
109
110 void daemonize()
111 {
112   std::ofstream pidFile;
113   if(gOpt.getPidFile() != "") {
114     pidFile.open(gOpt.getPidFile().c_str());
115     if(!pidFile.is_open())
116       AnytunError::throwErr() << "can't open pid file (" << gOpt.getPidFile() << "): " << AnytunErrno(errno);
117   }
118
119   pid_t pid;
120
121   pid = fork();
122   if(pid < 0)
123     AnytunError::throwErr() << "daemonizing failed at fork(): " << AnytunErrno(errno) << ", exitting";
124
125   if(pid) exit(0);
126
127   umask(0);
128
129   if(setsid() < 0)
130     AnytunError::throwErr() << "daemonizing failed at setsid(): " << AnytunErrno(errno) << ", exitting";
131
132   pid = fork();
133   if(pid < 0)
134     AnytunError::throwErr() << "daemonizing failed at fork(): " << AnytunErrno(errno) << ", exitting";
135
136   if(pid) exit(0);
137
138   if ((chdir("/")) < 0)
139     AnytunError::throwErr() << "daemonizing failed at chdir(): " << AnytunErrno(errno) << ", exitting";
140
141 //  std::cout << "running in background now..." << std::endl;
142
143   int fd;
144 //  for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
145   for (fd=0;fd<=2;fd++) // close all file descriptors
146     close(fd);
147   fd = open("/dev/null",O_RDWR);        // stdin
148   if(fd == -1)
149     cLog.msg(Log::PRIO_WARNING) << "can't open /dev/null as stdin";
150   else {
151     if(dup(fd) == -1)   // stdout
152       cLog.msg(Log::PRIO_WARNING) << "can't open /dev/null as stdout";
153     if(dup(fd) == -1)   // stderr
154       cLog.msg(Log::PRIO_WARNING) << "can't open /dev/null as stderr";
155   }
156
157   if(pidFile.is_open()) {
158     pid_t pid = getpid();
159     pidFile << pid;
160     pidFile.close();
161   }
162 }
163 #endif
164 #endif
165