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.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
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.
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.
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/>.
45 #include "anytunError.h"
51 PrivInfo(std::string const& username, std::string const& groupname)
59 pw_ = getpwnam(username.c_str());
61 AnytunError::throwErr() << "unkown user " << username;
64 gr_ = getgrnam(groupname.c_str());
66 gr_ = getgrgid(pw_->pw_gid);
69 AnytunError::throwErr() << "unkown group " << groupname;
77 if(setgid(gr_->gr_gid))
78 AnytunError::throwErr() << "setgid('" << gr_->gr_name << "') failed: " << AnytunErrno(errno);
81 gr_list[0] = gr_->gr_gid;
82 if(setgroups (1, gr_list))
83 AnytunError::throwErr() << "setgroups(['" << gr_->gr_name << "']) failed: " << AnytunErrno(errno);
85 if(setuid(pw_->pw_uid))
86 AnytunError::throwErr() << "setuid('" << pw_->pw_name << "') failed: " << AnytunErrno(errno);
88 cLog.msg(Log::PRIO_NOTICE) << "dropped privileges to " << pw_->pw_name << ":" << gr_->gr_name;
97 void do_chroot(std::string const& chrootdir)
100 AnytunError::throwErr() << "this programm has to be run as root in order to run in a chroot";
102 if(chroot(chrootdir.c_str()))
103 AnytunError::throwErr() << "can't chroot to " << chrootdir;
105 cLog.msg(Log::PRIO_NOTICE) << "we are in chroot jail (" << chrootdir << ") now" << std::endl;
107 AnytunError::throwErr() << "can't change to /";
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);
123 AnytunError::throwErr() << "daemonizing failed at fork(): " << AnytunErrno(errno) << ", exitting";
130 AnytunError::throwErr() << "daemonizing failed at setsid(): " << AnytunErrno(errno) << ", exitting";
134 AnytunError::throwErr() << "daemonizing failed at fork(): " << AnytunErrno(errno) << ", exitting";
138 if ((chdir("/")) < 0)
139 AnytunError::throwErr() << "daemonizing failed at chdir(): " << AnytunErrno(errno) << ", exitting";
141 // std::cout << "running in background now..." << std::endl;
144 // for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
145 for (fd=0;fd<=2;fd++) // close all file descriptors
147 fd = open("/dev/null",O_RDWR); // stdin
149 cLog.msg(Log::PRIO_WARNING) << "can't open /dev/null as stdin";
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";
157 if(pidFile.is_open()) {
158 pid_t pid = getpid();