-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.c
382 lines (342 loc) · 10.7 KB
/
pong.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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#define BUF_SIZE 1024*2
#define LINE_SIZE 1024*2
#define LOG_FORMAT "%Y-%m-%d %H:%M:%S"
#define LOG_FILE "log.txt"
#define DEFAULT_SERVER_NAME "pong"
#define DEFAULT_BANNER_FILE "./banner"
#define DEFAULT_QUOTES_FILE "./quotes.txt"
struct stack_t {
char **lines;
int count;
};
// @todo insert non-volatile variable
static volatile int running = 1;
static uid_t ruid, euid;
void dump(struct stack_t *stack) {
for (int i = 0; i < stack->count; i++) {
printf("line %d: %s\n", i, stack->lines[i]);
}
}
void freeStack(struct stack_t *stack) {
if (stack) {
// Cleanup stack.
for (int i = 0; i < stack->count; i++) {
free(stack->lines[i]);
}
free(stack->lines);
}
}
void doLog(char *fmt, ...) {
va_list ap;
if (ruid == 0) {
// Running as root.
int uid = atoi(getenv("SUDO_UID"));
seteuid(uid);
setuid(uid);
}
FILE *fin = fopen(LOG_FILE, "a");
if (fin) {
va_start(ap, fmt);
vfprintf(fin, fmt, ap);
va_end(ap);
fclose(fin);
}
if (ruid == 0) {
// Switch back to root.
seteuid(euid);
setuid(ruid);
}
}
int isWhiteListed(struct in_addr in, struct stack_t whitelist) {
if (whitelist.count) {
char* ip = inet_ntoa(in);
for (int i = 0; i < whitelist.count; i++) {
if (strncmp(ip, whitelist.lines[i], strlen(ip)) == 0) {
return 1;
}
}
return 0;
}
return 1;
}
void closeSocket(int s) {
char buffer[BUF_SIZE];
shutdown(s, SHUT_WR);
while (recv(s, buffer, BUF_SIZE, 0) > 0);
close(s);
}
void sigHandler(int sig) {
char c;
signal(sig, SIG_IGN);
printf("Do you really want to quit? [y/n]: ");
c = getchar();
if (c == 'y' || c == 'Y') {
running = 0;
return;
} else {
signal(SIGINT, sigHandler);
}
getchar(); // Get new line character
}
void help() {
printf("Syntax:\npong [-s name] [-q file] [-b file] [-w ip,...] [-xv] port\n");
}
int main(int argc, char **argv) {
int c, s, port, r;
FILE *fp;
FILE *fbanner;
int error;
time_t date;
struct tm *tm_info;
char buffer[BUF_SIZE];
char logBuffer[BUF_SIZE];
char **quotes = 0;
char line[LINE_SIZE] = {0};
struct sockaddr_in saddr, client_addr;
struct stack_t stack;
struct stack_t whitelist = {
0,
0,
};
socklen_t addr_len;
int enable = 1;
int one = 1;
char *banner = 0;
ssize_t banner_len = 0;
ssize_t read = 0;
ssize_t header_len = 0;
ssize_t verbose = 0;
ssize_t pos = 0;
char header[256];
int opt;
struct timeval tv = {
1,
0,
};
char *start, end;
char *banner_file = DEFAULT_BANNER_FILE;
char *quotes_file = DEFAULT_QUOTES_FILE;
char *serverName = DEFAULT_SERVER_NAME;
// Parse arguments.
if (argc < 2) {
help();
exit(1);
}
// Get uid and euid.
ruid = getuid();
euid = geteuid();
while ((opt = getopt(argc, argv, "w:q:b:s:vx")) != -1) {
switch (opt) {
case '?':
help();
exit(2);
break;
case 's':
printf("%s\n", optarg);
serverName = optarg;
break;
case 'x':
banner_file = 0;
break;
case 'q':
fp = fopen(optarg, "r");
if (!fp) {
printf("Failed to open quotes file, trying to use default.\n");
} else {
quotes_file = optarg;
fclose(fp);
}
break;
case 'w':
{
char *ptr = strtok(optarg, ",");
if (ptr) {
while (ptr) {
whitelist.lines = realloc(whitelist.lines, sizeof(char *) * (whitelist.count + 1));
whitelist.lines[whitelist.count] = malloc(strlen(ptr) + 1);
strncpy(whitelist.lines[whitelist.count], ptr, strlen(ptr));
whitelist.lines[whitelist.count][strlen(ptr)] = 0;
whitelist.count++;
ptr = strtok(NULL, ",");
}
}
} break;
case 'b':
fbanner = fopen(optarg, "r");
if (!fbanner) {
printf("Failed to open banner file, trying to use default.\n");
} else {
banner_file = optarg;
fclose(fbanner);
}
break;
case 'v':
verbose = 1;
break;
}
}
// Assume last argument is port.
port = atoi(argv[optind]);
if (port < 1 || port > 65535) {
perror("Invalid port, specify a port between 1 and 65535.\n");
exit(1);
}
// Check if we should set default banner.
if (banner_file) {
// Read in our banner.
fbanner = fopen(banner_file, "r");
if (fbanner) {
fseek(fbanner, 0, SEEK_END);
banner_len = ftell(fbanner);
fseek(fbanner, 0, SEEK_SET);
banner = malloc(banner_len + 1);
fread(banner, banner_len, 1, fbanner);
fclose(fbanner);
}
}
// Load quotes.
if ((fp = fopen(quotes_file, "r")) == 0) {
perror("Failed to open file.\n");
exit(1);
}
// Initialize stack.
stack.lines = 0;
stack.count = 0;
while (fgets(line, LINE_SIZE, (FILE *) fp) != 0) {
line[strlen(line) - 1] = 0;
// Add to stack.
stack.lines = realloc(stack.lines, sizeof(char *) * (stack.count + 1));
stack.lines[stack.count] = malloc(strlen(line) + 1);
strncpy(stack.lines[stack.count], line, strlen(line));
stack.lines[stack.count][strlen(line)] = 0;
stack.count++;
}
fclose(fp);
// Create socket.
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
error = errno;
perror("Error creating socket");
exit(error);
}
// Reuse address in waiting state.
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
error = errno;
perror("Failed to reuse address");
exit(error);
}
// Set non-blocking mode for socket.
fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK);
// Setup address.
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons(port);
// Bind socket to specific address.
if (bind(s, (struct sockaddr *) &saddr, sizeof(saddr)) != 0) {
error = errno;
perror("Error binding to address");
exit(error);
}
// Set socket in listening state.
if (listen(s, 10) != 0) {
error = errno;
perror("Error listening on socket");
exit(error);
}
memset(&client_addr, 0, sizeof(client_addr));
addr_len = sizeof(client_addr);
// Randomize.
srand(0);
printf("===========================================\n");
printf("Start waiting for clients\n");
printf("===========================================\n");
signal(SIGINT, sigHandler);
// Ignore SIGPIPE trigged when sending data to an already closed socket.
signal(SIGPIPE, SIG_IGN);
while (running) {
c = accept(s, (struct sockaddr *) &client_addr, &addr_len);
if (c > 0) {
if (!isWhiteListed(client_addr.sin_addr, whitelist)) {
printf("Client %s is not allowed to connect.\n", inet_ntoa(client_addr.sin_addr));
if (verbose) {
doLog("Client %s is not allowed to connect.\n", inet_ntoa(client_addr.sin_addr));
}
header_len = snprintf(header, sizeof(header), "HTTP/1.1 401 Unauthorized\nServer: %s\n\n", serverName);
send(c, header, header_len, 0);
closeSocket(c);
continue;
}
// TODO: Use select instead to see when something is happening on the socket.
// Set client in non-blocking mode.
// fcntl(c, F_SETFL, O_NONBLOCK);
// Set timeout for blocking calls.
setsockopt(c, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
time(&date);
tm_info = localtime(&date);
strftime(logBuffer, 26, LOG_FORMAT, tm_info);
// Log client connection.
doLog("%s - %s\n\n", logBuffer, inet_ntoa(client_addr.sin_addr));
printf("%s - %s\n", logBuffer, inet_ntoa(client_addr.sin_addr));
// Read all initial data from client.
pos = 0;
do {
read = recv(c, &buffer[pos], BUF_SIZE, 0);
if (read > 0) {
pos += read;
}
} while (read > 0);
buffer[pos] = 0;
if (pos == 0) {
close(c);
usleep(250);
continue;
}
// Log request headers and body in verbose mode.
if (verbose) {
doLog("%s\n", buffer);
}
// Method SP Request-URI SP HTTP-Version CRLF
char *start = strchr(buffer, ' ') + 1;
char *end = strchr(start, ' ');
// Check so we don't have any path
if (end - start > 1) {
// Always send 404
header_len = sprintf(header, "HTTP/1.1 404 NOT FOUND\n\n");
send(c, header, header_len, 0);
close(c);
usleep(250);
continue;
}
r = rand() % (stack.count - 1);
header_len = snprintf(header,
sizeof(header),
"HTTP/1.1 200 OK\nServer: %s\nContent-Type: text/plain\nContent-Length: %lu\n\n",
serverName,
banner_len + strlen(stack.lines[r]));
// First send a valid http response header.
send(c, header, header_len, 0);
// Then send the banner.
send(c, banner, banner_len, 0);
// Finally send a random quote to the client.
send(c, stack.lines[r], strlen(stack.lines[r]), 0);
// Close client socket.
closeSocket(c);
}
usleep(250);
}
close(s);
freeStack(&stack);
freeStack(&whitelist);
free(banner);
exit(0);
return 0;
}