1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*************************************************
* Unix Command Execution Source File *
* (C) 1999-2007 The Botan Project *
*************************************************/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#endif
#ifndef _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED 1
#endif
#include <botan/unix_cmd.h>
#include <botan/parsing.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
namespace Botan {
namespace {
/*************************************************
* Attempt to execute the command *
************************************************/
void do_exec(const std::vector<std::string>& arg_list,
const std::vector<std::string>& paths)
{
const u32bit args = arg_list.size() - 1;
const char* arg1 = (args >= 1) ? arg_list[1].c_str() : 0;
const char* arg2 = (args >= 2) ? arg_list[2].c_str() : 0;
const char* arg3 = (args >= 3) ? arg_list[3].c_str() : 0;
const char* arg4 = (args >= 4) ? arg_list[4].c_str() : 0;
for(u32bit j = 0; j != paths.size(); j++)
{
const std::string full_path = paths[j] + "/" + arg_list[0];
const char* fsname = full_path.c_str();
::execl(fsname, fsname, arg1, arg2, arg3, arg4, 0);
}
}
}
/*************************************************
* Local information about the pipe *
*************************************************/
struct pipe_wrapper
{
int fd;
pid_t pid;
pipe_wrapper() { fd = -1; pid = 0; }
};
/*************************************************
* Read from the pipe *
*************************************************/
u32bit DataSource_Command::read(byte buf[], u32bit length)
{
if(end_of_data())
return 0;
fd_set set;
FD_ZERO(&set);
FD_SET(pipe->fd, &set);
struct ::timeval tv;
tv.tv_sec = 0;
tv.tv_usec = MAX_BLOCK_USECS;
ssize_t got = 0;
if(::select(pipe->fd + 1, &set, 0, 0, &tv) == 1)
{
if(FD_ISSET(pipe->fd, &set))
got = ::read(pipe->fd, buf, length);
}
if(got <= 0)
{
shutdown_pipe();
return 0;
}
return static_cast<u32bit>(got);
}
/*************************************************
* Peek at the pipe contents *
*************************************************/
u32bit DataSource_Command::peek(byte[], u32bit, u32bit) const
{
if(end_of_data())
throw Invalid_State("DataSource_Command: Cannot peek when out of data");
throw Stream_IO_Error("Cannot peek/seek on a command pipe");
}
/*************************************************
* Check if we reached EOF *
*************************************************/
bool DataSource_Command::end_of_data() const
{
return (pipe) ? false : true;
}
/*************************************************
* Return the Unix file descriptor of the pipe *
*************************************************/
int DataSource_Command::fd() const
{
if(!pipe)
return -1;
return pipe->fd;
}
/*************************************************
* Return a human-readable ID for this stream *
*************************************************/
std::string DataSource_Command::id() const
{
return "Unix command: " + arg_list[0];
}
/*************************************************
* Create the pipe *
*************************************************/
void DataSource_Command::create_pipe(const std::string& path)
{
const std::vector<std::string> paths = split_on(path, ':');
bool found_something = false;
for(u32bit j = 0; j != paths.size(); j++)
{
const std::string full_path = paths[j] + "/" + arg_list[0];
if(::access(full_path.c_str(), X_OK) == 0)
{
found_something = true;
break;
}
}
if(!found_something)
return;
int pipe_fd[2];
if(::pipe(pipe_fd) != 0)
return;
pid_t pid = ::fork();
if(pid == -1)
{
::close(pipe_fd[0]);
::close(pipe_fd[1]);
}
else if(pid > 0)
{
pipe = new pipe_wrapper;
pipe->fd = pipe_fd[0];
pipe->pid = pid;
::close(pipe_fd[1]);
}
else
{
if(dup2(pipe_fd[1], STDOUT_FILENO) == -1)
::exit(127);
if(close(pipe_fd[0]) != 0 || close(pipe_fd[1]) != 0)
::exit(127);
if(close(STDERR_FILENO) != 0)
::exit(127);
do_exec(arg_list, paths);
::exit(127);
}
}
/*************************************************
* Shutdown the pipe *
*************************************************/
void DataSource_Command::shutdown_pipe()
{
if(pipe)
{
pid_t reaped = waitpid(pipe->pid, 0, WNOHANG);
if(reaped == 0)
{
kill(pipe->pid, SIGTERM);
struct ::timeval tv;
tv.tv_sec = 0;
tv.tv_usec = KILL_WAIT;
select(0, 0, 0, 0, &tv);
reaped = ::waitpid(pipe->pid, 0, WNOHANG);
if(reaped == 0)
{
::kill(pipe->pid, SIGKILL);
do
reaped = ::waitpid(pipe->pid, 0, 0);
while(reaped == -1);
}
}
::close(pipe->fd);
delete pipe;
pipe = 0;
}
}
/*************************************************
* DataSource_Command Constructor *
*************************************************/
DataSource_Command::DataSource_Command(const std::string& prog_and_args,
const std::string& path) :
MAX_BLOCK_USECS(100000), KILL_WAIT(10000)
{
arg_list = split_on(prog_and_args, ' ');
if(arg_list.size() == 0)
throw Invalid_Argument("DataSource_Command: No command given");
if(arg_list.size() > 5)
throw Invalid_Argument("DataSource_Command: Too many args");
pipe = 0;
create_pipe(path);
}
/*************************************************
* DataSource_Command Destructor *
*************************************************/
DataSource_Command::~DataSource_Command()
{
if(!end_of_data())
shutdown_pipe();
}
}
|