-
Notifications
You must be signed in to change notification settings - Fork 3
/
fwupgrade-cgi.c
261 lines (213 loc) · 5.66 KB
/
fwupgrade-cgi.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "fwupgrade-cgi.h"
#define VALID_CONTENT_TYPE "multipart/form-data; boundary="
#define VALID_ELEMENT_CONTENT_DISPOSITION "Content-Disposition:"
#define VALID_ELEMENT_CONTENT_TYPE "Content-Type: application/octet-stream"
static char *nextline(char *s, unsigned int *remaining)
{
/* Go to the end of line */
while (*s != '\n' && *s != '\r' && *s != 0) {
(*remaining)--;
s++;
}
if (! *s)
return NULL;
if (*s == '\r') {
(*remaining)--;
s++;
}
else
return NULL;
if (*s == '\n') {
(*remaining)--;
s++;
}
else
return NULL;
return s;
}
static char *get_filename_from_content_disposition(char *buf)
{
char *p, *psave;
char *localbuf, *tmp;
int buflen;
/* Eliminate the Content-Disposition header name */
buf += strlen(VALID_ELEMENT_CONTENT_DISPOSITION);
/* Copy the header value so that it is 0 terminated, which
* allows it to be parsed with strtok_r() */
buflen = strchr(buf, '\r') - buf;
localbuf = malloc(buflen + 1);
if (! localbuf)
return NULL;
memset(localbuf, 0, buflen + 1);
strncpy(localbuf, buf, buflen);
tmp = localbuf;
/* This parses a string like 'form-data; name="file";
* filename="foobar.img"' and extracts 'foobar.img' */
while((p = strtok_r(tmp, ";", &psave)) != NULL) {
char *delim;
/* Eliminate spaces before the field name */
while (*p == ' ')
p++;
/* Split the name from the value */
delim = strchr(p, '=');
if (! delim)
goto next;
else {
int len; char *value; char *name;
name = p;
*delim = '\0';
value = delim + 1;
len = strlen(value);
/* Remove quotes */
if (value[0] == '"' && value[len-1] == '"') {
value[len-1] = '\0';
value++;
}
if (! strcmp(name, "filename")) {
free(localbuf);
return strdup(value);
}
}
next:
tmp = NULL;
}
free(localbuf);
return NULL;
}
char *fwupgrade_cgi_receive_data(unsigned int *length_out)
{
char *method;
char *content_type;
char *content_length;
long length, length_read;
char *buffer = NULL;
char *boundary = NULL, *boundary_start, *data, *cur;
unsigned int boundary_len, data_len, remaining;
int boundary_found;
char *filename;
method = getenv("REQUEST_METHOD");
if (! method) {
printf("ERROR: incorrect REQUEST_METHOD, aborting.\n");
goto error;
}
if (strcasecmp(method, "post")) {
printf("ERROR: incorrect HTTP method, aborting.\n");
goto error;
}
content_type = getenv("CONTENT_TYPE");
if (! content_type) {
printf("ERROR: no content type, aborting.\n");
goto error;
}
content_length = getenv("CONTENT_LENGTH");
if (! content_length) {
printf("ERROR: no content length, aborting.\n");
goto error;
}
/* Verify that we have a supported content type */
if (strncasecmp(content_type, VALID_CONTENT_TYPE,
strlen(VALID_CONTENT_TYPE))) {
printf("ERROR: unsupported content type %s, aborting.\n",
content_type);
goto error;
}
boundary_start = strchr(content_type, '=');
if (! boundary_start) {
printf("ERROR: cannot find boundary delimiter, aborting.\n");
goto error;
}
/* Skip the '=' character */
boundary_start += 1;
boundary_len = 2 + strlen(boundary_start);
boundary = malloc(boundary_len + 1);
if (! boundary) {
printf("ERROR: memory allocation problem, aborting.\n");
goto error;
}
snprintf(boundary, boundary_len + 1, "--%s", boundary_start);
length = strtol(content_length, NULL, 10);
if (length == LONG_MIN || length == LONG_MAX) {
printf("ERROR: incorrect length\n");
goto error;
}
buffer = malloc(length);
if (! buffer) {
printf("ERROR: memory allocation problem, aborting.\n");
goto error;
}
length_read = fread(buffer, 1, length, stdin);
if (length_read != length) {
printf("ERROR: could not read the complete %ld bytes, aborting.\n", length);
goto error;
}
remaining = length;
cur = buffer;
/* The data should start with the boundary delimiter */
if (strncmp(boundary, cur, boundary_len)) {
printf("ERROR: cannot find boundary delimiter in data, aborting.\n");
goto error;
}
cur = nextline(cur, & remaining);
/* Check that we have a Content-Disposition line */
if (strncmp(cur, VALID_ELEMENT_CONTENT_DISPOSITION,
strlen(VALID_ELEMENT_CONTENT_DISPOSITION))) {
printf("ERROR: cannot find Content-Disposition in element\n");
goto error;
}
filename = get_filename_from_content_disposition(cur);
cur = nextline(cur, & remaining);
/* Check that we have a Content-Type line */
if (strncmp(cur, VALID_ELEMENT_CONTENT_TYPE,
strlen(VALID_ELEMENT_CONTENT_TYPE))) {
printf("ERROR: cannot find Content-Type in element\n");
goto error;
}
/* Skip all lines until we find an empty line */
while((cur = nextline(cur, & remaining)) != NULL) {
if (cur[0] == '\r' && cur[1] == '\n') {
cur = nextline(cur, & remaining);
break;
}
}
if (cur == NULL) {
printf("ERROR: cannot find data in firmware image\n");
goto error;
}
/* The real data starts here */
data = cur;
data_len = 0;
boundary_found = 0;
while(remaining >= boundary_len) {
/* Have we reached the boundary, which marks the end
of the data ? */
if (! strncmp(cur, boundary, boundary_len)) {
if (! strncmp(data + data_len - 2, "\r\n", 2))
data_len -= 2;
boundary_found = 1;
break;
}
data_len ++;
cur ++;
remaining --;
}
if (! boundary_found) {
printf("ERROR: cannot find boundary\n");
goto error;
}
printf("Received image file '%s' of %d bytes\n",
filename, data_len);
/* Move the useful data at the beginning of the buffer, so
that the beginning of the data is 4 bytes aligned */
memmove(buffer, data, data_len);
*length_out = data_len;
free(boundary);
return buffer;
error:
free(buffer);
free(boundary);
return NULL;
}