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