-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_wget.cpp
259 lines (206 loc) · 5.42 KB
/
agent_wget.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
Copyright 2018 Intel Corporation
This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you (License). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.
This software and the related documents are provided as is, with no
express or implied warranties, other than those that are expressly stated
in the License.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "httpparser/response.h"
#include "httpparser/httpresponseparser.h"
#include "agent_wget.h"
#include "common.h"
#include "iasrequest.h"
using namespace std;
using namespace httpparser;
#include <string>
#include <vector>
static vector<string> wget_args;
extern int debug, verbose;
#define CHUNK_SZ 8192
#define WGET_NO_ERROR 0
#define WGET_SERVER_ERROR 8
#define WGET_AUTH_ERROR 6
string AgentWget::name= "wget";
int AgentWget::request (string const &url, string const &post,
Response &response)
{
HttpResponseParser parser;
int pipefd[2];
pid_t pid;
string arg;
string sresponse;
int status;
char buffer[CHUNK_SZ];
size_t bread;
int repeat;
int rv= 1;
char tmpfile[]= "/tmp/wgetpostXXXXXX";
int postdata= 0;
if ( post.length() > 0 ) {
int fd= mkstemp(tmpfile);
size_t bwritten, rem;
const char *bp= post.c_str();
fprintf(stderr, "+++ POST data written to %s\n", tmpfile);
postdata= 1;
if ( fd == -1 ) {
perror("mkstemp");
return 0;
}
rem= post.length();
while ( rem ) {
retry_write:
bwritten= write(fd, bp, rem);
if ( bwritten == -1 ) {
if (errno == EINTR) goto retry_write;
else {
perror("write");
close(fd);
unlink(tmpfile);
return 0;
}
}
rem-= bwritten;
bp+= bwritten;
}
close(fd);
}
// Only need to initialize these once
if ( wget_args.size() == 0 ) {
wget_args.push_back("wget");
// Output options
if ( ! verbose ) wget_args.push_back("--quiet");
wget_args.push_back("--output-document=-");
wget_args.push_back("--save-headers");
wget_args.push_back("--content-on-error");
wget_args.push_back("--no-http-keep-alive");
arg= conn->proxy_server();
// Override environment
if ( arg != "" ) {
string proxy_url= "http://";
proxy_url+= arg;
if ( conn->proxy_port() != 80 ) {
proxy_url+= ":";
proxy_url+= to_string(conn->proxy_port());
}
proxy_url+= "/";
setenv("https_proxy", proxy_url.c_str(), 1);
}
if ( conn->proxy_mode() == IAS_PROXY_NONE ) {
wget_args.push_back("--no-proxy");
} else if ( conn->proxy_mode() == IAS_PROXY_FORCE ) {
unsetenv("no_proxy");
}
}
/* Set up two pipes for reading from the child */
if ( pipe(pipefd) == -1 ) {
perror("pipe");
return 0;
}
/* Spawn the child process */
pid= fork();
if ( pid == -1 ) { /* oops */
perror("fork");
return 0;
} else if ( pid == 0 ) { /* child */
char **argv;
size_t sz, i;
// Add instance-specific options
// construct then add the Ocp-Apim-Subscription-Key subscription key header
string subscriptionKeyHeader = "--header=Ocp-Apim-Subscription-Key: ";
subscriptionKeyHeader.append(conn->getSubscriptionKey());
wget_args.push_back(subscriptionKeyHeader.c_str());
if ( postdata ) {
string contentTypeHeader = "--header=Content-Type: application/json";
wget_args.push_back(contentTypeHeader.c_str());
arg= "--post-file=";
arg+= tmpfile;
wget_args.push_back(arg);
}
// Add the url
wget_args.push_back(url.c_str());
sz= wget_args.size();
// Create the argument list
if ( debug ) eprintf("+++ Exec:");
argv= (char **) malloc(sizeof(char *)*(sz+1));
if ( argv == NULL ) {
perror("malloc");
exit(1);
}
for(i= 0; i< sz; ++i) {
argv[i]= strdup(wget_args[i].c_str());
if ( argv[i] == NULL ) {
perror("strdup");
exit(1);
}
if ( debug ) eprintf(" %s", argv[i]);
}
argv[sz]= 0;
if ( debug ) eprintf("\n");
retry_dup:
/* Dup stdout onto our pipe */
if ( dup2(pipefd[1], STDOUT_FILENO) == -1 ) {
if ( errno == EINTR ) goto retry_dup;
perror("dup2");
exit(1);
}
close(pipefd[1]);
close(pipefd[0]);
execvp("wget", argv);
perror("execvp: wget");
exit(1);
}
/* parent */
close(pipefd[1]);
/* Read until eol */
repeat= 1;
while ( repeat ) {
bread= read(pipefd[0], buffer, CHUNK_SZ);
if ( bread == -1 ) {
if ( errno == EINTR ) continue;
else {
perror("read");
repeat= 0;
rv= 0;
}
} else if ( bread == 0 ) {
repeat= 0;
} else {
sresponse.append(buffer, bread);
}
}
/* This is a blocking wait */
retry_wait:
if ( waitpid(pid, &status, 0) == -1 ) {
if ( errno == EINTR) goto retry_wait;
else {
perror("waitpid");
rv= 0;
}
}
unlink(tmpfile);
if ( ! rv ) return 0;
if ( WIFEXITED(status) ) {
int exitcode= WEXITSTATUS(status);
if ( exitcode == WGET_AUTH_ERROR)
{
response.statusCode = IAS_UNAUTHORIZED;
}
else if ( exitcode == WGET_NO_ERROR || exitcode == WGET_SERVER_ERROR ) {
HttpResponseParser::ParseResult result;
result= parser.parse(response, sresponse.c_str(),
sresponse.c_str()+sresponse.length());
rv= ( result == HttpResponseParser::ParsingCompleted );
}
} else rv= 0;
return rv;
}