forked from remram44/http-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-redirect.c
446 lines (404 loc) · 11.5 KB
/
http-redirect.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* Optionnal features */
#ifndef __WIN32__
#ifndef ENABLE_FORK
#ifndef DISABLE_FORK
#define ENABLE_FORK
#endif
#endif
#ifndef ENABLE_CHGUSER
#ifndef DISABLE_CHGUSER
#define ENABLE_CHGUSER
#endif
#endif
#else
#ifdef ENABLE_FORK
#warning ENABLE_FORK is not available on Windows
#undef ENABLE_FORK
#endif
#ifdef ENABLE_CHGUSER
#warning ENABLE_CHGUSER is not available on Windows
#undef ENABLE_CHGUSER
#endif
#endif
/* Configuration */
#ifndef MAX_PENDING_REQUESTS
#define MAX_PENDING_REQUESTS 16
#endif
#ifndef RECV_BUFFER_SIZE
#define RECV_BUFFER_SIZE 512
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __WIN32__
#define _WIN32_WINNT 0x0501 /* needed for getaddrinfo(); means WinXP */
#include <winsock2.h>
#include <ws2tcpip.h>
typedef int socklen_t;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
typedef int SOCKET;
#endif
#ifdef ENABLE_CHGUSER
#include <pwd.h>
#endif
int setup_server(int *serv_sock, const char *addr, const char *port);
int serve(int serv_sock, const char *dest);
void pack_array(void **array, size_t size)
{
size_t src, dest = 0;
for(src = 0; src < size; ++src)
if(array[src] != NULL)
array[dest++] = array[src];
for(; dest < size; ++dest)
array[dest] = NULL;
}
void my_closesocket(int sock)
{
#ifdef __WIN32__
closesocket(sock);
#else
shutdown(sock, SHUT_RDWR);
close(sock);
#endif
}
void print_help(FILE *f)
{
fprintf(
f,
"Usage: http-redirect [options] <destination>\n"
"\n"
" Starts a very simple HTTP server that will always send back 301 "
"redirects to\n"
"the specified destination.\n"
" Example:\n"
" http-redirect -p 80 http://www.google.com/\n"
"\n"
"Recognized options:\n"
" -h, --help: print this message and exit\n"
#ifndef ENABLE_FORK
" -d, --daemon: use fork() to daemonize\n"
#endif
#ifndef ENABLE_CHGUSER
" -u, --user: change to user after binding the socket\n"
#endif
" -p, --port <port>: port on which to listen\n");
}
int main(int argc, char **argv)
{
const char *bind_addr = NULL;
const char *port = NULL;
const char *dest = NULL;
#ifdef ENABLE_FORK
int daemonize = 0;
#endif
#ifdef ENABLE_CHGUSER
const char *user = NULL;
#endif
(void)argc; /* unused */
while(*(++argv) != NULL)
{
if(strcmp(*argv, "-h") == 0 || strcmp(*argv, "--help") == 0)
{
print_help(stdout);
return 0;
}
else if(strcmp(*argv, "-b") == 0 || strcmp(*argv, "--bind") == 0)
{
if(bind_addr != NULL)
{
fprintf(stderr, "Error: --bind was passed multiple times\n");
return 1;
}
if(*(++argv) == NULL)
{
fprintf(stderr, "Error: missing argument for --bind\n");
return 1;
}
bind_addr = *argv;
}
else if(strcmp(*argv, "-p") == 0 || strcmp(*argv, "--port") == 0)
{
if(port != NULL)
{
fprintf(stderr, "Error: --port was passed multiple times\n");
return 1;
}
if(*(++argv) == NULL)
{
fprintf(stderr, "Error: missing argument for --port\n");
return 1;
}
port = *argv;
}
else if(strcmp(*argv, "-d") == 0 || strcmp(*argv, "--daemon") == 0)
{
#ifdef ENABLE_FORK
daemonize = 1;
#else
fprintf(stderr, "Error: --daemon is not available\n");
return 1;
#endif
}
else if(strcmp(*argv, "-u") == 0 || strcmp(*argv, "--user") == 0)
{
#ifdef ENABLE_CHGUSER
if(user != NULL)
{
fprintf(stderr, "Error: --user was passed multiple times\n");
return 1;
}
if(*(++argv) == NULL)
{
fprintf(stderr, "Error: missing argument for --user\n");
return 1;
}
user = *argv;
#else
fprintf(stderr, "Error: --user is not available\n");
return 1;
#endif
}
else
{
if(dest != NULL)
{
fprintf(stderr, "Error: multiple destinations specified\n");
return 1;
}
dest = *argv;
}
}
if(port == NULL)
port = "80";
if(dest == NULL)
{
fprintf(stderr, "Error: no destination specified\n");
return 1;
}
#ifdef __WIN32__
{
/* Initializes WINSOCK */
WSADATA wsa;
if(WSAStartup(MAKEWORD(1, 1), &wsa) != 0)
{
fprintf(stderr, "Error: can't initialize WINSOCK\n");
return 3;
}
}
#endif
{
int serv_sock;
/* Poor man's exception handling... */
int ret = setup_server(&serv_sock, bind_addr, port);
if(ret != 0)
return ret;
#ifdef ENABLE_CHGUSER
if(user != NULL)
{
struct passwd *pwd = getpwnam(user);
if(pwd == NULL)
{
fprintf(stderr, "Error: user %s is unknown\n", user);
return 2;
}
if(setresuid(pwd->pw_uid, pwd->pw_uid, pwd->pw_uid) == -1)
{
fprintf(stderr, "Error: can't change user to %s\n", user);
return 2;
}
}
#endif
#ifdef ENABLE_FORK
if(daemonize)
{
switch(fork())
{
case -1:
perror("Error: fork() failed");
break;
case 0:
/* child: go on... */
fclose(stdin);
fclose(stdout);
fclose(stderr);
break;
default:
/* parent: exit with success */
return 0;
break;
}
}
#endif
ret = serve(serv_sock, dest);
my_closesocket(serv_sock);
return ret;
}
}
int setup_server(int *serv_sock, const char *addr, const char *port)
{
int ret;
struct addrinfo hints, *results, *rp;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
ret = getaddrinfo(addr, port, &hints, &results);
if(ret != 0)
{
fprintf(stderr, "Error: getaddrinfo failed: %s\n", gai_strerror(ret));
return 1;
}
for(rp = results; rp != NULL; rp = rp->ai_next)
{
*serv_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(*serv_sock == -1)
continue;
if(bind(*serv_sock, rp->ai_addr, rp->ai_addrlen) != -1)
break;
my_closesocket(*serv_sock);
}
freeaddrinfo(results);
if(rp == NULL)
{
fprintf(stderr, "Could not bind to %s:%s\n",
((addr == NULL)?"*":addr), port);
*serv_sock = -1;
return 2;
}
if(listen(*serv_sock, 5) == -1)
{
perror("Error: can't listen for incoming connections");
return 2;
}
return 0;
}
char *build_redirect(const char *dest, size_t *response_size)
{
const char *pattern =
"HTTP/1.1 301 Moved Permanently\r\n"
"Location: %s\r\n"
"Server: http-redirect\r\n"
"\r\n";
char *response;
*response_size = strlen(pattern) - 2 + strlen(dest);
response = malloc(*response_size + 1);
snprintf(response, *response_size + 1, pattern, dest);
return response;
}
struct Client {
int sock;
int state;
};
int serve(int serv_sock, const char *dest)
{
size_t response_size;
char *response_data = build_redirect(dest, &response_size);
struct Client *connections[MAX_PENDING_REQUESTS];
size_t i;
for(i = 0; i < MAX_PENDING_REQUESTS; ++i)
connections[i] = NULL;
while(1)
{
int greatest;
fd_set fds;
FD_ZERO(&fds);
FD_SET((SOCKET)serv_sock, &fds);
greatest = serv_sock;
for(i = 0; i < MAX_PENDING_REQUESTS && connections[i] != NULL; ++i)
{
int s = connections[i]->sock;
FD_SET((SOCKET)s, &fds);
if(s > greatest)
greatest = s;
}
select(greatest + 1, &fds, NULL, NULL, NULL);
if(FD_ISSET(serv_sock, &fds))
{
/* If all connections are taken */
if(connections[MAX_PENDING_REQUESTS - 1] != NULL)
{
my_closesocket(connections[0]->sock);
free(connections[0]);
connections[0] = NULL;
pack_array((void**)connections, MAX_PENDING_REQUESTS);
}
/* Accept the new connection */
{
struct sockaddr_in clientsin;
socklen_t size = sizeof(clientsin);
int sock = accept(serv_sock,
(struct sockaddr*)&clientsin, &size);
if(sock != -1)
{
for(i = 0; i < MAX_PENDING_REQUESTS; ++i)
if(connections[i] == NULL)
break;
connections[i] = malloc(sizeof(struct Client));
connections[i]->sock = sock;
connections[i]->state = 0;
}
}
}
else for(i = 0; i < MAX_PENDING_REQUESTS && connections[i] != NULL; ++i)
{
int s = connections[i]->sock;
if(FD_ISSET(s, &fds))
{
int *const state = &connections[i]->state;
int j;
/* Read stuff */
static char buffer[RECV_BUFFER_SIZE];
int len = recv(s, buffer, RECV_BUFFER_SIZE, 0);
/* Decode HTTP request (really just wait for "\r\n\r\n") */
for(j = 0; j < len; ++j)
{
if(buffer[j] == '\r')
{
if(*state == 0 || *state == 2)
++*state;
else
*state = 1;
}
else if(buffer[j] == '\n')
{
if(*state < 2)
*state = 2;
else
{
*state = 4;
break;
}
}
else
*state = 0;
}
if(*state == 4)
/* Print redirect */
send(s, response_data, response_size, 0);
/* Client closed the connection OR request complete */
if(len <= 0 || *state == 4)
{
my_closesocket(s);
free(connections[i]);
connections[i] = NULL;
pack_array((void**)connections, MAX_PENDING_REQUESTS);
}
}
}
}
free(response_data);
return 0;
}