summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/tnc_ifmap/tnc_ifmap_http.c
blob: 9105b7b4d40ed9e37d6cc96d365ddab5d2265683 (plain)
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
/*
 * Copyright (C) 2013 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#define _GNU_SOURCE /* for asprintf() */

#include "tnc_ifmap_http.h"

#include <utils/debug.h>
#include <utils/lexparser.h>

#include <stdio.h>

typedef struct private_tnc_ifmap_http_t private_tnc_ifmap_http_t;

/**
 * Private data of an tnc_ifmap_http_t object.
 */
struct private_tnc_ifmap_http_t {

	/**
	 * Public tnc_ifmap_http_t interface.
	 */
	tnc_ifmap_http_t public;

	/**
	 * HTTPS Server URI with https:// prefix removed
	 */
	char *uri;

	/**
	 * Optional base64-encoded username:password for HTTP Basic Authentication
	 */
	chunk_t user_pass;

	/**
	 * HTTP chunked mode
	 */
	bool chunked;

};

METHOD(tnc_ifmap_http_t, build, status_t,
	private_tnc_ifmap_http_t *this, chunk_t *in, chunk_t *out)
{
	char *host, *path, *request, auth[128];
	int len;

	/* Duplicate host[/path] string since we are going to manipulate it */
	len = strlen(this->uri) + 2;
	host = malloc(len);
	memset(host, '\0', len);
	strcpy(host, this->uri);

	/* Extract appended path or set to root */
	path = strchr(host, '/');
	if (!path)
	{
		path = host + len - 2;
		*path = '/';
	}

	/* Use Basic Authentication? */
	if (this->user_pass.len)
	{
		snprintf(auth, sizeof(auth), "Authorization: Basic %.*s\r\n",
				 this->user_pass.len, this->user_pass.ptr);
	}
	else
	{
		*auth = '\0';
	}

	/* Write HTTP POST request, TODO break up into chunks */
	len = asprintf(&request,
			"POST %s HTTP/1.1\r\n"
			"Host: %.*s\r\n"
			"%s"
			"Content-Type: application/soap+xml;charset=utf-8\r\n"
			"Content-Length: %d\r\n"
			"\r\n"
			"%.*s", path, (path-host), host, auth, in->len, in->len, in->ptr);
	free(host);

	if (len == -1)
	{
		return FAILED;
	}
	*out = chunk_create(request, len);
	DBG3(DBG_TLS, "sending HTTP POST request %B", out);

	return SUCCESS;
}

static bool process_header(chunk_t *in, bool *chunked, u_int *content_len)
{
	chunk_t line, version, parameter;
	int code;
	u_int len;

	/* Process HTTP protocol version */
	if (!fetchline(in, &line) || !extract_token(&version, ' ', &line) ||
		!match("HTTP/1.1", &version) || sscanf(line.ptr, "%d", &code) != 1)
	{
		DBG1(DBG_TNC, "malformed http response header");
		return FALSE;
	}
	if (code != 200)
	{
		DBG1(DBG_TNC, "http response returns error code %d", code);
		return FALSE;
	}	

	*content_len = 0;
	*chunked = FALSE;

	/* Process HTTP header line by line until the HTTP body is reached */
	while (fetchline(in, &line))
	{
		if (line.len == 0)
		{
			break;
		}
		if (extract_token(&parameter, ':', &line) && eat_whitespace(&line))
		{
			if (match("Content-Length", &parameter))
			{
				if (sscanf(line.ptr, "%u", &len) == 1)
	 			{
					*content_len = len;
				}
			}
			else if (match("Transfer-Encoding", &parameter) &&
					 match("chunked", &line))
			{
				*chunked = TRUE;
			}
		}
	}

	return TRUE;
}

METHOD(tnc_ifmap_http_t, process, status_t,
	private_tnc_ifmap_http_t *this, chunk_t *in, chunk_t *out)
{
	u_int len = 0;
	chunk_t line, out_chunk;

	DBG3(DBG_TLS, "receiving HTTP response %B", in);

	if (!this->chunked)
	{
		if (!process_header(in, &this->chunked, &len))
		{
			return FAILED;
		}
	}

	while (in->len)
	{
		if (this->chunked)
		{
			if (!fetchline(in, &line) || sscanf(line.ptr, "%x", &len) != 1)
			{
				return FAILED;
			}
			DBG3(DBG_TLS, "received HTTP response is chunked (%u bytes)", len);

			/* Received last chunk? */
			if (len == 0)
			{
				return SUCCESS;
			}
		}

		/* Check size of of remaining HTTP body */
		if (len > in->len)
		{
			DBG1(DBG_TNC, "insufficient data in HTTP body");
			return FAILED;
		}

		if (this->chunked)
		{
			out_chunk = *in;
			out_chunk.len = len;
			*out = chunk_cat("mc", *out, out_chunk); 
			*in = chunk_skip(*in, len);
			if (!fetchline(in, &line) || line.len > 0)
			{
				return FAILED;
			}		
		}
		else
		{
			if (len)
			{
				in->len = len;
			}
			*out = chunk_clone(*in);
			return SUCCESS;
		}
	}
	return NEED_MORE;
}

METHOD(tnc_ifmap_http_t, destroy, void,
	private_tnc_ifmap_http_t *this)
{
	free(this);
}

/**
 * See header
 */
tnc_ifmap_http_t *tnc_ifmap_http_create(char *uri, chunk_t user_pass)
{
	private_tnc_ifmap_http_t *this;

	INIT(this,
		.public = {
			.build = _build,
			.process = _process,
			.destroy = _destroy,
		},
		.uri = uri,
		.user_pass = user_pass,
	);

	return &this->public;
}