Debian Codegraph @

src/tls/httpredirect.c

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "httpredirect.h"
9
10 #include <sys/socket.h>
11 #include <pthread.h>
12 #include <stdio.h>
13 #include <stdbool.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 static char *
18 read_line (FILE *stream,
19 char *buffer,
20 size_t sizeof_buffer,
21 size_t *offset)
22 {
23 if (*offset >= sizeof_buffer)
24 return NULL;
25
26 char *line = fgets (buffer + *offset, sizeof_buffer - *offset, stream);
27
28 if (line == NULL)
29 return NULL;
30
31 /* Make sure we're terminated with \r\n or \n */
32 size_t line_length = strcspn (line, "\r\n");
33 const char *ending = line + line_length;
34 if (strcmp (ending, "\r\n") != 0 && strcmp (ending, "\n") != 0)
35 return NULL;
36
37 /* Discard the ending */
38 line[line_length++] = '\0';
39
40 *offset += line_length;
41
42 return line;
43 }
44
45 static bool
46 write_error (FILE *output)
47 {
48 fprintf (output, "HTTP/1.1 400 Client Error\r\n"
49 "Connection: close\r\n"
50 "\r\n"
51 "Incorrect request.\r\n");
52
53 return false;
54 }
55
56 static bool
57 http_redirect (FILE *input,
58 FILE *output)
59 {
60 char buffer[10000];
61 size_t offset = 0;
62
63 char *request_line = read_line (input, buffer, sizeof buffer, &offset);
64 if (request_line == NULL)
65 return write_error (output);
66
67 char *path = strchr (request_line, ' ');
68 if (path == NULL)
69 return write_error (output);
70 path++;
71
72 char *end_path = strchr (path, ' ');
73 if (end_path == NULL)
74 return write_error (output);
75 *end_path = '\0';
76
77 const char *host = NULL;
78 const char *header;
79 do
80 {
81 header = read_line (input, buffer, sizeof buffer, &offset);
82 if (header == NULL)
83 return write_error (output);
84
85 #define HOST_HEADER "Host:"
86 if (strncmp (header, HOST_HEADER, strlen (HOST_HEADER)) == 0)
87 {
88 if (host != NULL)
89 return write_error (output);
90
91 host = header + strlen (HOST_HEADER);
92 host += strspn (host, " \t");
93 }
94 }
95 while (header[0] != '\0');
96
97 if (!host)
98 return write_error (output);
99
100 /* Defense-in-depth: validate that host and path don't contain CR or LF. The current read_line() implementation
101 * already prevents this, but add explicit validation just in case, and reject invalid values.
102 */
103 if (strchr (host, '\r') || strchr (host, '\n') ||
104 strchr (path, '\r') || strchr (path, '\n'))
105 return write_error (output);
106
107 fprintf (output, "HTTP/1.1 301 Moved Permanently\r\n"
108 "Location: https://%s%s\r\n"
109 "Connection: close\r\n"
110 "\r\n", host, path);
111
112 return true;
113 }
114
115 static void *
116 http_redirect_start (void *arg)
117 {
118 FILE *stream = arg;
119
120 http_redirect (stream, stream);
121
122 fclose (stream);
123
124 return NULL;
125 }
126
127 int
128 http_redirect_connect (void)
129 {
130 int sv[2];
131 int r = socketpair (AF_UNIX, SOCK_STREAM, 0, sv);
132 if (r != 0)
133 return -1;
134
135 /* At this point we're going to succeed and return sv[1] to the
136 * caller. We need to make sure that sv[0] gets closed one way or
137 * another:
138 * - if we fail to create the stream, close()
139 * - if we fail to spawn the thread, fclose()
140 * - otherwise, the thread calls fclose()
141 */
142 FILE *stream = fdopen (sv[0], "r+");
143 if (stream == NULL)
144 close (sv[0]);
145 else
146 {
147 pthread_attr_t attr;
148 pthread_t thread;
149
150 pthread_attr_init (&attr);
151 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
152
153 if (pthread_create (&thread, &attr, &http_redirect_start, stream))
154 fclose (stream);
155
156 pthread_attr_destroy (&attr);
157 }
158
159 return sv[1];
160 }
161
162 #ifdef HTTP_REDIRECT_STANDALONE
163 int
164 main (void)
165 {
166 return http_redirect (stdin, stdout) ? 0 : 1;
167 }
168 #endif