-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
337 lines (286 loc) · 9.94 KB
/
main.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
#include "mongoose.h"
#include "index.h"
#include <string.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <crypt.h>
#include <time.h>
char *port = "8080";
char *data_dir = "/srv/clink";
char *seed = "secret";
char *proto = "http://";
static struct mg_http_serve_opts s_http_server_opts;
static void rec_mkdir(const char *dir) {
char tmp[256];
char *p = NULL;
size_t len;
snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
if (tmp[len - 1] == '/')
tmp[len - 1] = 0;
for (p = tmp + 1; *p; p++)
if (*p == '/') {
*p = 0;
mkdir(tmp, S_IRWXU);
*p = '/';
}
mkdir(tmp, S_IRWXU);
}
bool file_exists(char *filename) {
struct stat buffer;
return (stat (filename, &buffer) == 0);
}
char *get_link_filename(char *link) {
char *filename = malloc(strlen(data_dir) + strlen(link) + 8);
sprintf(filename, "%s/links/%s", data_dir, link);
return filename;
}
char *get_del_filename(char *link) {
char *filename = malloc(strlen(data_dir) + strlen(link) + 6);
sprintf(filename, "%s/del/%s", data_dir, link);
return filename;
}
bool link_exists(char *link) {
char *filename = get_link_filename(link);
bool ret = file_exists(filename);
free(filename);
return ret;
}
char *random_short_link() {
srand(time(NULL));
char *short_link = malloc(17);
do {
for(size_t i = 0; i < 16; ++i) {
sprintf(short_link + i, "%x", rand() % 16);
}
} while (link_exists(short_link));
return short_link;
}
char *gen_del_key(char *link) {
char *salt = malloc(20);
char *rand_str = malloc(17);
srand(time(NULL));
for (size_t i = 0; i < 16; ++i) {
rand_str[i] = 37 + (rand() % 90); // random printable char
if (rand_str[i] == 92 || rand_str[i] == 58 ||
rand_str[i] == 59 || rand_str[i] == 42) --i; // chars not allowed for salts
}
rand_str[16] = 0;
sprintf(salt, "$6$%s", rand_str);
free(rand_str);
char *use_link = malloc(strlen(link) + strlen(seed) + 1);
sprintf(use_link, "%s%s", seed, link);
char *del_key = crypt(use_link, salt);
free(salt);
free(use_link);
return del_key;
}
void trim(char *str) {
char *_str = str;
int len = strlen(_str);
while(*_str && *_str == '/') ++_str, --len;
memmove(str, _str, len + 1);
}
void make_short_url(struct mg_connection *nc, char *to, char *host, char *link) {
char *short_link;
if (strlen(link) == 0) {
short_link = random_short_link();
} else if (strlen(link) >= 255) {
return mg_http_reply(nc, 413, "", "short link length can not exceed 255 characters");
} else {
short_link = strdup(link);
}
if (link_exists(short_link)) {
mg_http_reply(nc, 500, "", "short link %s already exists", short_link);
return free(short_link);
}
if (strstr(short_link, "/")) {
mg_http_reply(nc, 400, "", "short link can not contain slashes");
return free(short_link);
}
char *link_file = get_link_filename(short_link);
if (!mg_file_write(link_file, to, strlen(to))) {
fprintf(stderr, "failed to write to file %s", link_file);
mg_http_reply(nc, 500, "", "failed to write data");
free(short_link);
return free(link_file);
}
free(link_file);
char *del_key = gen_del_key(short_link);
char *del_file = get_del_filename(short_link);
if (!mg_file_write(del_file, del_key, strlen(del_key))) {
fprintf(stderr, "failed to write to file %s", del_file);
return mg_http_reply(nc, 500, "", "failed to write data");
free(short_link);
free(link_file);
return free(del_file);
}
free(del_file);
char *del_header = malloc(256);
sprintf(del_header, "X-Delete-With: %s\r\n", del_key);
mg_http_reply(nc, 201, del_header, "%s%s/%s", proto, host, short_link);
free(del_header);
free(short_link);
}
void handle_url_req(struct mg_connection *nc, char *to, char *host, char *link) {
if (strlen(to) != 0) {
make_short_url(nc, to, host, link);
} else {
if (strlen(link) == 0) {
return mg_http_reply(nc, 200, "Content-Type: text/html\r\n", INDEX_HTML,
host, host, host, host, host, host, host, host, host, host, host); // FIXME: need better solution
} else {
if (strncmp(link, "favicon.ico", 12) == 0) {
mg_http_reply(nc, 404, "", "Not Found");
} else if (strlen(link) >= 255) {
mg_http_reply(nc, 414, "", "short link length can not exceed 255 characters");
} else if (link_exists(link)) {
size_t urlto_size;
char *link_file = get_link_filename(link);
char *urlto = mg_file_read(link_file, &urlto_size);
char *loc = malloc(urlto_size + 14);
sprintf(loc, "Location: %s\r\n", urlto);
mg_http_reply(nc, 302, loc, urlto);
free(urlto);
free(loc);
free(link_file);
} else {
mg_http_reply(nc, 404, "", "this short link does not exist");
}
}
}
}
void handle_delete(struct mg_connection *nc, char *link, char *del_key) {
if (link_exists(link)) {
char *del_file = get_del_filename(link);
char *key = mg_file_read(del_file, NULL);
if (strcmp(key, del_key) == 0) {
char *link_file = get_link_filename(link);
remove(link_file);
remove(del_file);
mg_http_reply(nc, 204, "", "");
free(link_file);
} else {
mg_http_reply(nc, 403, "", "incorrect deletion key");
}
free(del_file);
free(key);
} else {
mg_http_reply(nc, 404, "", "this short link does not exist");
}
}
static void ev_handler(struct mg_connection *nc, int ev, void *p, void *f) {
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) p;
char *uri = malloc(hm->uri.len + 1);
snprintf(uri, hm->uri.len + 1, "%s", hm->uri.ptr);
trim(uri);
char *query = NULL;
struct mg_str hquery = hm->query;
if (hquery.len > 0) {
char *base_query = malloc(hquery.len + 1);
snprintf(base_query, hquery.len + 1, "%s", hquery.ptr);
query = malloc(256);
mg_url_decode(base_query, hquery.len + 1, query, 256, 0);
free(base_query);
} else {
query = malloc(1);
sprintf(query, "");
}
struct mg_str *pmhost = mg_http_get_header(hm, "Host");
struct mg_str mhost;
if (pmhost == NULL) {
fprintf(stderr, "request sent with no Host header");
mhost = mg_str("<UNKNOWN DOMAIN>");
} else {
mhost = *pmhost;
}
char *host = malloc(mhost.len + 1);
snprintf(host, mhost.len + 1, "%s", mhost.ptr);
char *body = strdup(hm->body.ptr);
if (strncmp(hm->method.ptr, "POST", hm->method.len) == 0) {
handle_url_req(nc, body, host, uri); // FIXME: return 400 on bad Content-Type
} else if (strncmp(hm->method.ptr, "DELETE", hm->method.len) == 0) {
handle_delete(nc, uri, body);
} else if (strncmp(hm->method.ptr, "GET", hm->method.len) == 0){
handle_url_req(nc, query, host, uri);
} else {
mg_http_reply(nc, 405, "Allow: GET, POST, DELETE\r\n", "");
}
free(uri);
free(host);
free(body);
free(query);
}
}
int main(int argc, char *argv[]) {
int index;
int c;
opterr = 0;
setvbuf(stdout, NULL, _IONBF, 0);
while ((c = getopt (argc, argv, "p:d:s:kh")) != -1) {
switch (c) {
case 'p':
port = optarg;
break;
case 'd':
data_dir = optarg;
break;
case 's':
seed = optarg;
break;
case 'k':
proto = "https://";
break;
case 'h':
printf("clink: a minimal URL shortener\n");
printf("usage: %s [-p port] [-d data_dir] [-s seed] [-k]\n\n", argv[0]);
printf("options:\n");
printf("-p <port>\t\tport to use (default 8080)\n");
printf("-d <data directory>\tdirectory to store data (default /srv/clink)\n");
printf("-s <seed>\t\tsecret seed to use (DO NOT SHARE THIS; default 'secret')\n");
printf("-k\t\t\treturns HTTPS URLs when uploading files, use with an HTTPS-enabled reverse proxy\n\n");
printf("source: https://short.swurl.xyz/src (submit bug reports, suggestions, etc. here)\n");
return 0;
case '?':
if (optopt == 'p' || optopt == 'd' || optopt == 's') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
}
else if (isprint (optopt)) {
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
}
else {
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
}
return 1;
default:
abort();
}
}
for (index = optind; index < argc; index++) {
printf ("Non-option argument %s\n", argv[index]);
}
char *links_dir = strcat(strdup(data_dir), "/links");
char *del_dir = strcat(strdup(data_dir), "/del");
rec_mkdir(links_dir);
rec_mkdir(del_dir);
free(links_dir);
free(del_dir);
struct mg_mgr mgr;
struct mg_connection *nc;
mg_mgr_init(&mgr);
printf("Starting web server on port %s\n", port);
char *str_port = malloc(20);
sprintf(str_port, "http://0.0.0.0:%s", port);
nc = mg_http_listen(&mgr, str_port, ev_handler, &mgr);
if (nc == NULL) {
printf("Failed to create listener\n");
return 1;
}
for (;;) { mg_mgr_poll(&mgr, 1000); }
mg_mgr_free(&mgr);
return 0;
}