Imported Upstream version 0.3.2
[anytun.git] / src / sysExec.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 <boost/bind.hpp>
34 #include <boost/thread.hpp>
35
36 #include "datatypes.h"
37 #include "sysExec.h"
38 #include "log.h"
39 #include "anytunError.h"
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <sys/wait.h>
46 #include <sys/select.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <cstring>
50
51 void anytun_exec(std::string const& script)
52 {
53   anytun_exec(script, StringVector(), StringList());
54 }
55
56 void anytun_exec(std::string const& script, StringVector const& args)
57 {
58   anytun_exec(script, args, StringList());
59 }
60
61 void anytun_exec(std::string const& script, StringList const& env)
62 {
63   anytun_exec(script, StringVector(), env);
64 }
65
66 void anytun_exec(std::string const& script, StringVector const& args, StringList const& env)
67 {
68   int pipefd[2];
69   if(pipe(pipefd) == -1) {
70     cLog.msg(Log::PRIO_ERROR) << "executing script '" << script << "' pipe() error: " << AnytunErrno(errno);
71     return;
72   }
73
74   pid_t pid;
75   pid = fork();
76   if(pid == -1) {
77     cLog.msg(Log::PRIO_ERROR) << "executing script '" << script << "' fork() error: " << AnytunErrno(errno);
78     return;
79   }
80
81   if(pid) {
82     close(pipefd[1]);
83     boost::thread(boost::bind(waitForScript, script, pid, pipefd[0]));
84     return;
85   }
86
87 // child code
88   int fd;
89   for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
90     if(fd != pipefd[1]) close(fd);
91   
92   fd = open("/dev/null",O_RDWR);        // stdin
93   if(fd == -1)
94     cLog.msg(Log::PRIO_WARNING) << "can't open stdin";
95   else {
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";
100   }
101   
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());
108   }
109   argv[args.size() + 1] = NULL;
110
111   char** evp;
112   evp = new char*[env.size() + 1];
113   unsigned int i = 0;
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());
117     ++i;
118   }
119   evp[env.size()] = NULL;
120   
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
125   int err = errno;
126   int ret = write(pipefd[1], (void*)(&err), sizeof(err));
127   if(ret != sizeof(errno))
128     exit(-2);
129   exit(-1);
130 }
131
132 void waitForScript(std::string const& script, pid_t pid, int pipefd)
133 {
134   int status = 0;
135   waitpid(pid, &status, 0);
136
137   fd_set rfds;
138   FD_ZERO(&rfds);
139   FD_SET(pipefd, &rfds);
140   struct timeval tv = { 0 , 0 };
141   if(select(pipefd+1, &rfds, NULL, NULL, &tv) == 1) {
142     int err = 0;
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);
145       close(pipefd);
146       return;
147     }
148   }
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);
153   else
154     cLog.msg(Log::PRIO_ERROR) << "executing script '" << script << "': unkown error";
155
156   close(pipefd);
157 }