aboutsummaryrefslogtreecommitdiff
path: root/src/http.c
blob: 974e76184c6cd1722de552e4f06140b0475a70c5 (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
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
#include "../beaker.h"
#include "beaker_globals.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

static bool is_http_token_char(unsigned char c) {
  if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
      (c >= 'a' && c <= 'z')) {
    return true;
  }

  return c == '!' || c == '#' || c == '$' || c == '%' || c == '&' ||
         c == '\'' || c == '*' || c == '+' || c == '-' || c == '.' ||
         c == '^' || c == '_' || c == '`' || c == '|' || c == '~';
}

bool beaker_is_valid_http_token(const char *value) {
  if (value == NULL) {
    return false;
  }

  return beaker_is_valid_http_token_span(value, strlen(value));
}

bool beaker_is_valid_http_token_span(const char *value, size_t length) {
  if (value == NULL || length == 0)
    return false;

  for (size_t i = 0; i < length; i++) {
    if (!is_http_token_char((unsigned char)value[i])) {
      return false;
    }
  }
  return true;
}

bool beaker_is_valid_header_value(const char *value) {
  if (value == NULL) {
    return false;
  }

  for (size_t i = 0; value[i] != '\0'; i++) {
    unsigned char c = (unsigned char)value[i];
    if (c < 0x20 || c == 0x7f) {
      return false;
    }
  }
  return true;
}

static bool is_valid_cookie_value(const char *value) {
  if (value == NULL) {
    return false;
  }

  for (size_t i = 0; value[i] != '\0'; i++) {
    unsigned char c = (unsigned char)value[i];
    if (!(c == 0x21 || (c >= 0x23 && c <= 0x2b) || (c >= 0x2d && c <= 0x3a) ||
          (c >= 0x3c && c <= 0x5b) || (c >= 0x5d && c <= 0x7e))) {
      return false;
    }
  }
  return true;
}

static bool is_valid_cookie_attribute(const char *value) {
  return value == NULL ||
         (beaker_is_valid_header_value(value) && strchr(value, ';') == NULL);
}

static bool parse_status_line(const char *status_line, int *status_code) {
  if (status_line == NULL || status_code == NULL)
    return false;

  size_t status_line_len = strlen(status_line);
  if (status_line_len < 5 || status_line_len >= 128 || status_line[0] < '0' ||
      status_line[0] > '9' || status_line[1] < '0' || status_line[1] > '9' ||
      status_line[2] < '0' || status_line[2] > '9' || status_line[3] != ' ' ||
      status_line[4] == '\0' || !beaker_is_valid_header_value(status_line)) {
    return false;
  }

  int code = (status_line[0] - '0') * 100 + (status_line[1] - '0') * 10 +
             (status_line[2] - '0');
  if (code < 100 || code > 599) {
    return false;
  }

  *status_code = code;
  return true;
}

static void build_cookie_headers(char *cookie_headers_buffer,
                                 size_t buffer_size) {

  cookie_headers_buffer[0] = '\0';

  for (int i = 0; i < cookies_to_set_count; i++) {
    char single_cookie_header[MAX_KEY_LEN * 2 + MAX_VALUE_LEN * 2 + 128];

    int header_length = snprintf(
        single_cookie_header, sizeof(single_cookie_header), "Set-Cookie: %s=%s",
        cookies_to_set[i].name, cookies_to_set[i].value);
    if (header_length < 0 ||
        (size_t)header_length >= sizeof(single_cookie_header)) {
      beaker_log("ERROR", "build_cookie_headers: Cookie is too large.");
      continue;
    }

    if (strlen(cookies_to_set[i].expires) > 0) {
      strncat(single_cookie_header, "; Expires=",
              sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
      strncat(single_cookie_header, cookies_to_set[i].expires,
              sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
    }

    if (strlen(cookies_to_set[i].path) > 0) {
      strncat(single_cookie_header, "; Path=",
              sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
      strncat(single_cookie_header, cookies_to_set[i].path,
              sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
    }

    if (cookies_to_set[i].http_only) {
      strncat(single_cookie_header, "; HttpOnly",
              sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
    }

    if (cookies_to_set[i].secure) {
      strncat(single_cookie_header, "; Secure",
              sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);
    }

    strncat(single_cookie_header, "\r\n",
            sizeof(single_cookie_header) - strlen(single_cookie_header) - 1);

    if (strlen(cookie_headers_buffer) + strlen(single_cookie_header) <
        buffer_size) {
      strncat(cookie_headers_buffer, single_cookie_header,
              buffer_size - strlen(cookie_headers_buffer) - 1);
    } else {
      beaker_log(
          "WARN",
          "build_cookie_headers: Cookie headers buffer full, truncating\n");
      break;
    }
  }
}

void beaker_clear_response_cookies(void) {
  memset(cookies_to_set, 0, sizeof(cookies_to_set));
  cookies_to_set_count = 0;
}

void send_status(const char *status_line) {
  if (current_client_socket == -1) {
    beaker_log("ERROR",
               "send_status: No client socket set. Cannot send response.\n");
    return;
  }

  int status_code;
  if (!parse_status_line(status_line, &status_code)) {
    beaker_log("SECURITY", "send_status: Rejected invalid HTTP status line.");
    status_line = "500 Internal Server Error";
    status_code = 500;
  }

  char http_response[BUFFER_SIZE];
  current_response_status = status_code;
  current_response_size = 0;
  int response_length = snprintf(http_response, sizeof(http_response),
                                 "HTTP/1.1 %s\r\n"
                                 "Content-Length: 0\r\n"
                                 "Connection: close\r\n"
                                 "\r\n",
                                 status_line);
  if (response_length < 0 || (size_t)response_length >= sizeof(http_response)) {
    beaker_log("ERROR", "send_status: Failed to construct HTTP status.");
    return;
  }

  if (beaker_send_all(current_client_socket, http_response,
                      strlen(http_response)) < 0) {
    beaker_log_errno_format("ERROR",
                            "send_status: Failed to send HTTP status.\n");
  }
}

void send_response(const char *html) {

  if (current_client_socket == -1) {
    beaker_log("ERROR",
               "send_response: No client socket set. Cannot send response.\n");
    return;
  }

  char http_response_header[BUFFER_SIZE * 2];
  size_t content_length = strlen(html);
  current_response_status = 200;
  current_response_size = content_length;
  char cookie_headers[BUFFER_SIZE];

  build_cookie_headers(cookie_headers, sizeof(cookie_headers));
  beaker_clear_response_cookies();

  snprintf(http_response_header, sizeof(http_response_header),
           "HTTP/1.1 200 OK\r\n"
           "Content-Type: text/html; charset=UTF-8\r\n"
           "Content-Length: %zu\r\n"
           "%s"
           "Connection: close\r\n"
           "\r\n",
           content_length, cookie_headers);

  if (beaker_send_all(current_client_socket, http_response_header,
                      strlen(http_response_header)) < 0) {
    beaker_log_errno_format("ERROR",
                            "send_response: Failed to send HTTP header.\n");
    return;
  }

  if (beaker_send_all(current_client_socket, html, content_length) < 0) {
    beaker_log_errno_format("ERROR",
                            "send_response: Failed to send HTML body.\n");
    return;
  }
}

void send_redirect(const char *location) {

  if (current_client_socket == -1) {
    beaker_log("ERROR",
               "send_redirect: No client socket set. Cannot send redirect.\n");
    return;
  }

  if (location == NULL || location[0] == '\0' ||
      !beaker_is_valid_header_value(location)) {
    beaker_log("SECURITY", "send_redirect: Rejected invalid Location value.");
    beaker_clear_response_cookies();
    send_status("500 Internal Server Error");
    return;
  }

  char http_response_header[BUFFER_SIZE * 2];
  current_response_status = 302;
  current_response_size = 0;
  char cookie_headers[BUFFER_SIZE];

  build_cookie_headers(cookie_headers, sizeof(cookie_headers));
  beaker_clear_response_cookies();

  int header_length =
      snprintf(http_response_header, sizeof(http_response_header),
               "HTTP/1.1 302 Found\r\n"
               "Location: %s\r\n"
               "%s"
               "Connection: close\r\n"
               "\r\n",
               location, cookie_headers);
  if (header_length < 0 ||
      (size_t)header_length >= sizeof(http_response_header)) {
    beaker_log("SECURITY", "send_redirect: Location value is too long.");
    send_status("500 Internal Server Error");
    return;
  }

  if (beaker_send_all(current_client_socket, http_response_header,
                      strlen(http_response_header)) < 0) {
    beaker_log_errno_format("ERROR",
                            "send_redirect: Failed to send redirect header.\n");
    return;
  }
}

void set_cookie(const char *name, const char *value, const char *expires,
                const char *path, bool http_only, bool secure) {

  if (!beaker_is_valid_http_token(name) || !is_valid_cookie_value(value) ||
      !is_valid_cookie_attribute(expires) || !is_valid_cookie_attribute(path) ||
      strlen(name) >= MAX_KEY_LEN || strlen(value) >= MAX_VALUE_LEN ||
      (expires != NULL && strlen(expires) >= MAX_VALUE_LEN) ||
      (path != NULL && strlen(path) >= MAX_KEY_LEN)) {
    beaker_log("SECURITY", "set_cookie: Rejected invalid cookie data.");
    return;
  }

  if (cookies_to_set_count >= MAX_COOKIES) {
    beaker_log("WARN",
               "set_cookie: Maximum number of cookies to set reached. Cannot "
               "set cookie '%s'.\n",
               name);
    return;
  }

  Cookie *new_cookie = &cookies_to_set[cookies_to_set_count];

  strncpy(new_cookie->name, name, MAX_KEY_LEN - 1);
  new_cookie->name[MAX_KEY_LEN - 1] = '\0';

  strncpy(new_cookie->value, value, MAX_VALUE_LEN - 1);
  new_cookie->value[MAX_VALUE_LEN - 1] = '\0';

  if (expires && strlen(expires) > 0) {
    strncpy(new_cookie->expires, expires, MAX_VALUE_LEN - 1);
    new_cookie->expires[MAX_VALUE_LEN - 1] = '\0';
  } else {
    new_cookie->expires[0] = '\0';
  }

  if (path && strlen(path) > 0) {
    strncpy(new_cookie->path, path, MAX_KEY_LEN - 1);
    new_cookie->path[MAX_KEY_LEN - 1] = '\0';
  } else {
    new_cookie->path[0] = '\0';
  }

  new_cookie->http_only = http_only;
  new_cookie->secure = secure;

  cookies_to_set_count++;
}

char *get_cookie(const char *cookie_name) {

  char *cookie_header_start = strstr(current_request_buffer, "\r\nCookie: ");
  if (cookie_header_start == NULL) {
    return NULL;
  }

  cookie_header_start += strlen("\r\nCookie: ");

  char *cookie_header_end = strstr(cookie_header_start, "\r\n");
  if (cookie_header_end == NULL) {

    cookie_header_end =
        (char *)(current_request_buffer + strlen(current_request_buffer));
  }

  size_t cookie_str_len = cookie_header_end - cookie_header_start;
  char *cookie_str = (char *)malloc(cookie_str_len + 1);
  if (cookie_str == NULL) {
    beaker_log_errno_format("ERROR",
                            "get_cookie: Allocation failed for cookie_str.\n");
    return NULL;
  }

  strncpy(cookie_str, cookie_header_start, cookie_str_len);
  cookie_str[cookie_str_len] = '\0';

  char *token;
  char *saveptr_cookie;

  token = strtok_r(cookie_str, ";", &saveptr_cookie);
  while (token != NULL) {

    while (*token == ' ') {
      token++;
    }

    char *equals_sign = strchr(token, '=');
    if (equals_sign != NULL) {
      size_t name_len = equals_sign - token;

      if (name_len == strlen(cookie_name) &&
          strncmp(token, cookie_name, name_len) == 0) {

        char *cookie_value = strdup(equals_sign + 1);
        if (cookie_value == NULL) {
          beaker_log_errno_format(
              "ERROR", "get_cookie: Allocation failed for cookie_value.\n");
        }
        free(cookie_str);
        return cookie_value;
      }
    }

    token = strtok_r(NULL, ";", &saveptr_cookie);
  }

  free(cookie_str);
  return NULL;
}