Imported Upstream version 0.3.4
[anytun.git] / src / win32 / sysExec.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 #pragma once
33 #ifndef ANYTUN_sysexec_hpp_INCLUDED
34 #define ANYTUN_sysexec_hpp_INCLUDED
35
36 #include <algorithm>
37 #include <iostream> // todo remove
38 #include <windows.h>
39
40 SysExec::~SysExec()
41 {
42   if(!closed_) {
43     CloseHandle(process_info_.hProcess);
44     CloseHandle(process_info_.hThread);
45   }
46 }
47
48 STARTUPINFOA getStartupInfo()
49 {
50   STARTUPINFOA startup_info;
51   startup_info.cb = sizeof(STARTUPINFOA);
52   GetStartupInfoA(&startup_info);
53
54   //startup_info.dwFlags = STARTF_USESTDHANDLES;
55   //startup_info.hStdInput = CreateFile("NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 0, 0, 0); // INVALID_HANDLE_VALUE;
56   //startup_info.hStdOutput = CreateFile("NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 0, 0, 0); // INVALID_HANDLE_VALUE;
57   //startup_info.hStdError = CreateFile("NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 0, 0, 0); // INVALID_HANDLE_VALUE;
58   startup_info.dwFlags |= STARTF_USESHOWWINDOW;
59   startup_info.wShowWindow = SW_HIDE;
60
61   return startup_info;
62 }
63
64 char const* const BATCH_FILE_EXTS[] = { ".bat", ".cmd" };
65 int const BATCH_FILE_EXTS_COUNT = sizeof(BATCH_FILE_EXTS) / sizeof(BATCH_FILE_EXTS[0]);
66
67 bool endsWith(std::string const& string, std::string const& suffix)
68 {
69   return string.find(suffix, string.size() - suffix.size()) != std::string::npos;
70 }
71
72 void SysExec::doExec(StringVector args, StringList env)
73 {
74   std::vector<char> arguments;
75
76   bool isBatchFile = false;
77   for(int i = 0; i < BATCH_FILE_EXTS_COUNT; ++i) {
78     if(endsWith(script_, BATCH_FILE_EXTS[i])) {
79       isBatchFile = true;
80       break;
81     }
82   }
83
84   if(isBatchFile) {
85     std::string const BATCH_INTERPRETER = "cmd /c \"";
86     arguments.insert(arguments.end(), BATCH_INTERPRETER.begin(), BATCH_INTERPRETER.end());
87   }
88   arguments.push_back('\"');
89   arguments.insert(arguments.end(), script_.begin(), script_.end());
90   arguments.push_back('\"');
91   arguments.push_back(' ');
92
93   for(StringVector::const_iterator it = args.begin(); it != args.end(); ++it) {
94     arguments.push_back('\"');
95     arguments.insert(arguments.end(), it->begin(), it->end());
96     arguments.push_back('\"');
97     arguments.push_back(' ');
98   }
99
100   if(isBatchFile) {
101     arguments.push_back('\"');
102   }
103   arguments.push_back(0);
104
105   STARTUPINFOA startup_info = getStartupInfo();
106
107   std::map<std::string, std::string> envDict;
108   for(StringList::const_iterator it = env.begin(); it != env.begin(); ++it) {
109     size_t delimiter_pos = it->find('=');
110     envDict.insert(std::make_pair(it->substr(0, delimiter_pos), it->substr(delimiter_pos + 1)));
111   }
112   std::vector<char> env;
113   for(std::map<std::string, std::string>::iterator it = envDict.begin(); it != envDict.end(); ++it) {
114     env.insert(env.end(), it->first.begin(), it->first.end());
115     env.push_back(0);
116   }
117   env.push_back(0);
118
119   if(!CreateProcessA(NULL,
120                      &arguments[0],
121                      NULL,
122                      NULL,
123                      false,
124                      NULL,
125                      &env[0],
126                      NULL,
127                      &startup_info,
128                      &process_info_
129                     )) {
130     cLog.msg(Log::PRIO_ERROR) << "executing script '" << script_ << "' CreateProcess() error: " << GetLastError();
131     return;
132   }
133 }
134
135 int SysExec::waitForScript()
136 {
137   DWORD result = WaitForSingleObject(process_info_.hProcess, INFINITE);
138   assert(WAIT_OBJECT_0 == result); // WAIT_FAILED, WAIT_TIMEOUT ... ???
139   bool success = GetExitCodeProcess(process_info_.hProcess, &return_code_) != 0;
140   assert(true == success); // false -> HU?
141
142   CloseHandle(process_info_.hProcess);
143   CloseHandle(process_info_.hThread);
144   closed_ = true;
145
146   return static_cast<int>(return_code_);
147 }
148
149 void SysExec::waitAndDestroy(SysExec*& s)
150 {
151   if(!s) {
152     return;
153   }
154
155   s->waitForScript();
156   cLog.msg(Log::PRIO_NOTICE) << "script '" << s->script_ << "' returned " << s->return_code_;
157
158   delete(s);
159   s = NULL;
160 }
161
162 #endif // ANYTUN_sysexec_hpp_INCLUDED