forked from olvemaudal/pal
/
httpget.cpp
400 lines (354 loc) · 11.9 KB
/
httpget.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// demo program for the PAL library, beware lots of happy-go-lucky code here
#include "pal.hpp"
#include <arpa/inet.h>
#include <errno.h>
#include <iostream>
#include <map>
#include <memory>
#include <netdb.h>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unistd.h>
static int verbose_mode = 0;
static char * my_fgets(char * s, int n, FILE * f)
{
const char lf = 0x0a;
const char cr = 0x0d;
int i = 0;
char c = 0;
char prev_c = 0;
int fd = fileno(f);
while (i < n-1) {
int rc = read(fd, &c, 1);
if (rc == 0)
continue;
if (rc != 1)
return 0;
if ((prev_c == cr) && (c == lf))
break;
if (c == cr) {
prev_c = c;
continue;
}
prev_c = s[i++] = c;
}
s[i++] = '\0';
return s;
}
class http_message
{
public:
void header_value(const std::string & name, const std::string & value)
{
header_fields_[name] = value;
}
std::string header_value(const std::string & name) const
{
std::map<std::string,std::string>::const_iterator i = header_fields_.find(name);
return (i != header_fields_.end()) ? i->second : "";
}
void body(const std::string & data)
{
body_ = data;
}
std::string body() const
{
return body_;
}
std::string as_string() const
{
std::ostringstream ss;
ss << start_line_ << "\r\n";
for (std::map<std::string,std::string>::const_iterator
i = header_fields_.begin(); i != header_fields_.end(); ++i)
ss << i->first << ": " << i->second << "\r\n";
ss << "\r\n";
ss << body_;
return ss.str();
}
protected:
http_message() : start_line_(), header_fields_(), body_() {}
explicit http_message(const std::string & start_line)
: start_line_(start_line), header_fields_(), body_() {}
virtual ~http_message() {}
void start_line(const std::string & line) { start_line_ = line; }
std::string start_line() const { return start_line_; }
private:
std::string start_line_;
std::map<std::string,std::string> header_fields_;
std::string body_;
};
class http_request : public http_message
{
public:
http_request(const std::string & method, const std::string & uri)
: http_message("GET " + uri + " HTTP/1.1")
{
if (method != "GET")
throw std::runtime_error("unsupported method");
}
};
class http_response : public http_message
{
public:
explicit http_response(const std::string & status_line)
: http_message(status_line), status_code_(), reason_()
{
std::istringstream ss(status_line);
std::string version;
ss >> version;
if (version != "HTTP/1.1")
throw std::runtime_error("unsupported HTTP version");
ss >> status_code_;
}
int status_code() const
{
return status_code_;
}
std::string status_line() const
{
return start_line();
}
private:
int status_code_;
std::string reason_;
};
class http_socket
{
public:
http_socket(const std::string & host, int port)
: host_(host), port_(port), sock_(0) {
// find host
struct hostent * addr = gethostbyname(host_.c_str());
if (addr == NULL) {
std::ostringstream ss;
ss << "failed to find host: " << host_
<< " h_errno=" << h_errno
<< " " << hstrerror(h_errno);
throw std::runtime_error(ss.str());
}
// open socket
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if (sockfd < 0) {
std::ostringstream ss;
ss << "failed to create socket: "
<< "errno=" << errno
<< " " << strerror(errno);
throw std::runtime_error(ss.str());
}
// connect
struct sockaddr_in servaddr;
char addrstr[INET_ADDRSTRLEN];
memset(&servaddr, 0, sizeof servaddr);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port_);
inet_ntop(AF_INET, *(addr->h_addr_list), addrstr, sizeof addrstr);
inet_pton(AF_INET, addrstr, &servaddr.sin_addr);
if (verbose_mode)
std::cerr << "Connecting to " << host_ << ":" << port_ << std::endl;
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof servaddr) < 0) {
std::ostringstream ss;
ss << "failed to connect: "
<< errno << " " << strerror(errno) << std::endl;
throw std::runtime_error(ss.str());
}
sock_ = fdopen(sockfd, "wr");
}
~http_socket() {
fclose(sock_);
sock_ = 0;
}
void send(const http_request & request) const {
std::string r = request.as_string();
ssize_t n = write(fileno(sock_), r.c_str(), r.length());
if (verbose_mode)
std::cerr << "wrote " << n << " bytes to socket" << std::endl;
if (n != (ssize_t)r.length())
throw std::runtime_error("failed to write data to socket");
}
http_response receive() const {
char linebuf[1024]; // TODO: fix
char * start_line = my_fgets(linebuf, sizeof linebuf, sock_);
http_response response(start_line);
typedef std::map<std::string,std::string> field_map;
field_map fields;
while(1) {
std::string header(my_fgets(linebuf, sizeof linebuf, sock_));
if (header == "")
break;
std::size_t pos = header.find(':');
if (pos == std::string::npos || header.length() < pos+2) {
std::cerr << "huh?" << std::endl;
continue;
}
std::string name = header.substr(0,pos);
std::string value = header.substr(pos+2);
fields[name] = value;
response.header_value(name,value);
}
std::size_t content_length = atoi(response.header_value("Content-Length").c_str());
std::vector<char> content(content_length);
std::size_t read_bytes = 0;
while (read_bytes < content_length) {
int rc = read(fileno(sock_), content.data() + read_bytes, content.size() - read_bytes);
if (rc == 0)
throw std::runtime_error("unexpected end of file");
if (rc < -1) {
std::ostringstream ss;
ss << "failed to read content: "
<< errno << " " << strerror(errno) << std::endl;
throw std::runtime_error(ss.str());
}
read_bytes += rc;
}
response.body(std::string(content.begin(), content.end()));
return response;
}
private:
http_socket(const http_socket &);
http_socket & operator=(const http_socket &);
std::string host_;
int port_;
FILE * sock_;
};
class http_uri
{
public:
explicit http_uri(const std::string & uri)
: username_(), password_(), hostname_(), port_(), path_()
{
std::string str(uri);
if (str.find("http://") != 0)
throw std::invalid_argument("invalid uri scheme (expected http://)");
str.erase(0,7);
std::size_t pos = str.find(':');
if (pos == str.npos)
throw std::invalid_argument("failed to find username");
username_ = str.substr(0,pos);
str.erase(0,username_.size()+1);
pos = str.find('@');
if (pos == str.npos)
throw std::invalid_argument("failed to find password");
password_ = str.substr(0,pos);
str.erase(0,password_.size()+1);
pos = str.find(':');
if (pos == str.npos)
throw std::invalid_argument("failed to find hostname");
hostname_ = str.substr(0,pos);
str.erase(0,hostname_.size()+1);
pos = str.find('/');
if (pos == str.npos)
throw std::invalid_argument("failed to find port");
std::string portstr = str.substr(0,pos);
port_ = atoi(portstr.c_str());
str.erase(0,portstr.size());
path_ = str;
}
std::string username() const {
return username_;
}
std::string password() const {
return password_;
}
std::string hostname() const {
return hostname_;
}
int port() const {
return port_;
}
std::string path() const {
return path_;
}
private:
std::string username_;
std::string password_;
std::string hostname_;
int port_;
std::string path_;
};
void print_usage(std::ostream & out)
{
out << "examples:\n"
<< " httpget http://Administrator:super@192.168.56.101:80/secret.txt\n"
<< " httpget -v http://Administrator:super@192.168.56.101:80/public.txt"
<< std::endl;
}
int main(int argc, char * argv[])
{
// parse command line
if (argc < 2) {
print_usage(std::cout);
return 0;
}
int argi = 1;
if (argc == 3 && std::string(argv[1]) == "-v" ) {
verbose_mode = 1;
++argi;
}
if (argi+1 != argc) {
std::cerr << "failed to parse command line" << std::endl;
return -1;
}
// parse the uri and open http socket
http_uri uri(argv[argi]);
http_socket socket(uri.hostname(),uri.port());
// send initial http request
http_request request("GET", uri.path());
request.header_value("Host", uri.hostname().c_str());
request.header_value("Connection", "keep-alive");
socket.send(request);
http_response response = socket.receive();
// if "200 OK" then no authentication needed, we're done
if (response.status_code() == 200) {
std::cout << response.body() << std::endl;
return 0;
}
// if it is not 401 + NTLM authentication that is requested then give up
if (response.status_code() != 401) {
std::cerr << response.status_line() << std::endl;
return -1;
}
if (response.header_value("WWW-Authenticate") != "NTLM") { // TODO: fix this, might be several
std::cerr << "unknown authentication requested" << std::endl;
return -1;
}
// create initial NTLM request
std::auto_ptr<pal::ntlm_message> ntlm_request_msg (pal::create_ntlm_request());
std::string ntlm_request = pal::as_base64_string(ntlm_request_msg->as_bytes());
// send a new request, expect another 401 containing an NTLM challenge
request.header_value("Authorization", "NTLM " + ntlm_request);
socket.send(request);
response = socket.receive();
// assume a 401, extract the NTLM challenge and solve it
if (response.status_code() != 401) {
std::cerr << response.status_line() << std::endl;
return -1;
}
std::string auth_str = response.header_value("WWW-Authenticate");
if (auth_str.length() < 10 || auth_str.substr(0,4) != "NTLM") {
std::cerr << "unexpected authentication challenge: " << auth_str << std::endl;
return -1;
}
std::string ntlm_challenge_str = auth_str.substr(5);
std::auto_ptr<pal::ntlm_message> ntlm_challenge_msg (
pal::create_ntlm_challenge(pal::as_bytes_from_base64_string(ntlm_challenge_str)));
std::auto_ptr<pal::ntlm_message> ntlm_response_msg(
pal::create_ntlm_response(uri.username(), uri.password(), *ntlm_challenge_msg));
std::vector<uint8_t> response_buf = ntlm_response_msg->as_bytes();
std::string ntlm_response = pal::as_base64_string(response_buf);
// challenge solved (hopefully), send the NTLM response and get the data
request.header_value("Authorization", "NTLM " + ntlm_response);
socket.send(request);
response = socket.receive();
if (response.status_code() != 200) {
std::cerr << response.status_line() << std::endl
<< "Sorry, failed to get data" << std::endl;
return -1;
}
// we have the content, here it is
std::cout << response.body() << std::endl;
}