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-2009 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 as published by
21 * the Free Software Foundation, either version 3 of the License, or
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.
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/>.
33 #include <boost/bind.hpp>
34 #include <boost/thread.hpp>
36 #include "datatypes.h"
39 #include "anytunError.h"
41 #include <sys/types.h>
46 #include <sys/select.h>
51 void anytun_exec(std::string const& script)
53 anytun_exec(script, StringVector(), StringList());
56 void anytun_exec(std::string const& script, StringVector const& args)
58 anytun_exec(script, args, StringList());
61 void anytun_exec(std::string const& script, StringList const& env)
63 anytun_exec(script, StringVector(), env);
66 void anytun_exec(std::string const& script, StringVector const& args, StringList const& env)
69 if(pipe(pipefd) == -1) {
70 cLog.msg(Log::PRIO_ERROR) << "executing script '" << script << "' pipe() error: " << AnytunErrno(errno);
77 cLog.msg(Log::PRIO_ERROR) << "executing script '" << script << "' fork() error: " << AnytunErrno(errno);
83 boost::thread(boost::bind(waitForScript, script, pid, pipefd[0]));
89 for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
90 if(fd != pipefd[1]) close(fd);
92 fd = open("/dev/null",O_RDWR); // stdin
94 cLog.msg(Log::PRIO_WARNING) << "can't open stdin";
96 if(dup(fd) == -1) // stdout
97 cLog.msg(Log::PRIO_WARNING) << "can't open stdout";
98 if(dup(fd) == -1) // stderr
99 cLog.msg(Log::PRIO_WARNING) << "can't open stderr";
102 char** argv = new char*[args.size() + 2];
103 argv[0] = new char[script.size() + 1];
104 std::strcpy(argv[0], script.c_str());
105 for(unsigned int i=0; i<args.size(); ++i) {
106 argv[i+1] = new char[args[i].size() + 1];
107 std::strcpy(argv[i+1], args[i].c_str());
109 argv[args.size() + 1] = NULL;
112 evp = new char*[env.size() + 1];
114 for(StringList::const_iterator it = env.begin(); it != env.end(); ++it) {
115 evp[i] = new char[it->size() + 1];
116 std::strcpy(evp[i], it->c_str());
119 evp[env.size()] = NULL;
121 execve(script.c_str(), argv, evp);
122 // if execve returns, an error occurred, but logging doesn't work
123 // because we closed all file descriptors, so just write errno to
124 // pipe and call exit
126 int ret = write(pipefd[1], (void*)(&err), sizeof(err));
127 if(ret != sizeof(errno))
132 void waitForScript(std::string const& script, pid_t pid, int pipefd)
135 waitpid(pid, &status, 0);
139 FD_SET(pipefd, &rfds);
140 struct timeval tv = { 0 , 0 };
141 if(select(pipefd+1, &rfds, NULL, NULL, &tv) == 1) {
143 if(read(pipefd, (void*)(&err), sizeof(err)) >= static_cast<int>(sizeof(err))) {
144 cLog.msg(Log::PRIO_NOTICE) << "script '" << script << "' exec() error: " << AnytunErrno(err);
149 if(WIFEXITED(status))
150 cLog.msg(Log::PRIO_NOTICE) << "script '" << script << "' returned " << WEXITSTATUS(status);
151 else if(WIFSIGNALED(status))
152 cLog.msg(Log::PRIO_NOTICE) << "script '" << script << "' terminated after signal " << WTERMSIG(status);
154 cLog.msg(Log::PRIO_ERROR) << "executing script '" << script << "': unkown error";