-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmgopherd.c
More file actions
496 lines (429 loc) · 11.5 KB
/
mgopherd.c
File metadata and controls
496 lines (429 loc) · 11.5 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*-
* "THE BEER-WARE LICENSE" (Revision 42):
* <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return.
*/
#define _POSIX_C_SOURCE 200809
#include <sys/stat.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include <unistd.h>
#include "gophermap.h"
#include "itemtypes.h"
#include "options.h"
#include "send.h"
#include "tools.h"
#define GOPHERMAP "gophermap"
#define REQUESTREGEX "^(/|(/[^\\.][^/]*)*)$"
#define BINBLOCK 1024
struct context {
const char *selector;
const char *path;
FILE *out;
};
static void handle_directory(struct opt_options *options,
struct context *context);
static void write_menu(struct opt_options *options, struct context *context);
static void write_gophermap(struct opt_options *options,
struct context *context, const char *map);
static char itemtype(const char *path, FILE *out);
static void write_binary_file(struct context *context);
static void write_text_file(struct context *context);
static int entry_select(const struct dirent *entry);
static bool check_rights(const char *path, char type, FILE *out);
static bool check_request(const char *request, FILE *out);
int
main(int argc, char **argv)
{
openlog("mgopherd", LOG_PID, LOG_USER);
struct opt_options *options;
options = opt_parse(argc, argv);
assert(options != NULL);
char *request = malloc(LINE_MAX);
if (request == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(stdout, "E: malloc", strerror(errno));
send_info(stdout, "I: I could not allocate memory.", NULL);
send_eom(stdout);
exit(EXIT_FAILURE);
}
if (fgets(request, LINE_MAX, stdin) == NULL) {
if (ferror(stdin)) {
syslog(LOG_ERR, "fgets error: %m");
send_error(stdout, "E: fgets", strerror(errno));
send_info(stdout, "I: I have a problem reading your "
"request.", NULL);
send_eom(stdout);
exit(EXIT_FAILURE);
}
}
tool_strip_crlf(request);
if (!check_request(request, stdout)) {
syslog(LOG_NOTICE, "invalid request: \"%s\"", request);
send_error(stdout, "E: request", request);
send_info(stdout, "I: Your request seems to be invalid.", NULL);
send_eom(stdout);
exit(EXIT_FAILURE);
}
if (*request == '\0') {
request[0] = '/';
request[1] = '\0';
}
syslog(LOG_INFO, "selector: \"%s\"", request);
char *path = tool_join_path(opt_get_root(options), request, stdout);
syslog(LOG_DEBUG, "path: \"%s\"", path);
struct context context = {
.selector = request,
.path = path,
.out = stdout
};
switch (itemtype(context.path, context.out)){
case IT_FILE:
syslog(LOG_DEBUG, "serving text file");
write_text_file(&context);
break;
case IT_ARCHIVE:
case IT_BINARY:
case IT_GIF:
case IT_HTML:
case IT_IMAGE:
case IT_AUDIO:
syslog(LOG_DEBUG, "serving binary file");
write_binary_file(&context);
break;
case IT_DIR:
syslog(LOG_DEBUG, "serving directory");
handle_directory(options, &context);
break;
case IT_IGNORE:
default:
syslog(LOG_NOTICE, "invalid item: \"%s\"", context.path);
send_error(context.out, "E: request", request);
send_info(context.out, "I: You requested an invalid item.",
NULL);
send_eom(context.out);
exit(EXIT_FAILURE);
}
free(path);
free(request);
opt_free(options);
closelog();
}
static bool
check_request(const char *request, FILE *out)
{
assert(request != NULL);
assert(out != NULL);
bool isvalid = true;
regex_t re;
int ret = regcomp(&re, REQUESTREGEX, REG_EXTENDED | REG_NOSUB);
if (ret != 0) {
char errbuf[128];
regerror(ret, &re, errbuf, sizeof(errbuf));
syslog(LOG_ERR, "regcomp: %s", errbuf);
send_error(out, "E: regcomp", errbuf);
send_info(out, "I: I could not compile a regular expression.",
NULL);
send_eom(out);
exit(EXIT_FAILURE);
}
ret = regexec(&re, request, 0, NULL, 0);
if (ret != 0) {
if (ret != REG_NOMATCH) {
char errbuf[128];
regerror(ret, &re, errbuf, sizeof(errbuf));
syslog(LOG_ERR, "regexec: %s", errbuf);
send_error(out, "E: regcomp", errbuf);
send_info(out, "I: I could not compile a regular "
"expression.", NULL);
send_eom(out);
exit(EXIT_FAILURE);
}
isvalid = false;
}
regfree(&re);
return (isvalid);
}
static void
handle_directory(struct opt_options *options, struct context *context)
{
assert(options != NULL);
assert(context != NULL);
char *map = tool_join_path(context->path, GOPHERMAP, context->out);
if (check_rights(map, IT_FILE, context->out))
write_gophermap(options, context, map);
else
write_menu(options, context);
free(map);
}
static void
write_menu(struct opt_options *options, struct context *context)
{
assert(options != NULL);
assert(context != NULL);
struct dirent **dirents;
int entries = scandir(context->path, &dirents, &entry_select,
&alphasort);
if (entries == -1) {
syslog(LOG_ERR, "scandir error: %m");
send_error(context->out, "E: scandir", strerror(errno));
send_info(context->out, "I: I have a problem scanning a "
"directory.", context->path);
send_eom(context->out);
exit(EXIT_FAILURE);
}
for (int i = 0; i < entries; i++) {
char *item = dirents[i]->d_name;
char *path = tool_join_path(context->path, item, context->out);
char type = itemtype(path, context->out);
char *sel = tool_join_path(context->selector, item,
context->out);
if (!check_rights(path, type, context->out)) {
syslog(LOG_DEBUG, "missing rights: \"%s\"", path);
free(sel);
free(path);
free(dirents[i]);
continue;
}
struct item it = {
.type = type,
.display = item,
.selector = sel,
.host = opt_get_host(options),
.port = opt_get_port(options)
};
send_item(context->out, &it);
free(sel);
free(path);
free(dirents[i]);
}
send_eom(context->out);
free(dirents);
}
static bool
check_rights(const char *path, char type, FILE *out)
{
assert(path != NULL);
assert(out != NULL);
int mode;
switch (type) {
case IT_FILE:
case IT_ARCHIVE:
case IT_BINARY:
case IT_GIF:
case IT_HTML:
case IT_IMAGE:
case IT_AUDIO:
mode = R_OK;
break;
case IT_DIR:
mode = R_OK | X_OK;
break;
case IT_IGNORE:
default:
return (false);
}
if (access(path, mode) == -1) {
if (errno != EACCES && errno != ENOENT) {
syslog(LOG_ERR, "access error: %m");
send_error(out, "E: accesss", strerror(errno));
send_info(out, "I: I couldn't check access rights "
"for an item.", path);
}
return (false);
}
return (true);
}
static int
entry_select(const struct dirent *entry)
{
assert(entry != NULL);
if (entry->d_name[0] == '.')
return (0);
return (1);
}
static char
itemtype(const char *path, FILE *out)
{
assert(path != NULL);
struct stat s;
if (lstat(path, &s) == -1) {
syslog(LOG_ERR, "lstat error: %m");
send_error(out, "E: lstat", strerror(errno));
send_info(out, "I: I could not get file status.", path);
return (IT_IGNORE);
}
char it;
if (S_ISREG(s.st_mode)) {
char *mime = tool_mimetype(path, out);
if (strcmp(mime, "text/html") == 0)
it = IT_HTML;
else if (strncmp(mime, "text/", 5) == 0)
it = IT_FILE;
else if (strcmp(mime, "image/gif") == 0)
it = IT_GIF;
else if (strncmp(mime, "image/", 6) == 0)
it = IT_IMAGE;
else if ((strncmp(mime, "audio/", 6) == 0) ||
(strcmp(mime, "application/ogg") == 0))
it = IT_AUDIO;
else if ((strcmp(mime, "application/x-bzip2") == 0) ||
(strcmp(mime, "application/x-gzip") == 0) ||
(strcmp(mime, "application/zip") == 0))
it = IT_ARCHIVE;
else
it = IT_BINARY;
free(mime);
} else if (S_ISDIR(s.st_mode))
it = IT_DIR;
else
it = IT_IGNORE;;
return (it);
}
static void
write_binary_file(struct context *context)
{
assert(context != NULL);
FILE *in = fopen(context->path, "r");
if (in == NULL) {
syslog(LOG_ERR, "fopen error: %m");
send_error(context->out, "E: fopen", strerror(errno));
send_info(context->out, "I: I could not open the requested "
"item.", context->path);
send_eom(context->out);
exit(EXIT_FAILURE);
}
void *block = malloc(BINBLOCK);
if (block == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(context->out, "E: malloc", strerror(errno));
send_info(context->out, "I: I could not allocate memory.",
NULL);
send_eom(context->out);
exit(EXIT_FAILURE);
}
size_t r;
while ((r = fread(block, 1, BINBLOCK, in)) > 0) {
size_t w = fwrite(block, 1, r, context->out);
if (w < r) {
syslog(LOG_ERR, "fwrite error: %m");
send_error(context->out, "E: fwrite", strerror(errno));
send_info(context->out, "I: I have a problem writing "
"your requested item.", context->path);
send_eom(context->out);
free(block);
exit(EXIT_FAILURE);
}
if (r < BINBLOCK) {
if (ferror(in)) {
syslog(LOG_ERR, "fread error: %m");
send_error(context->out, "E: fread",
strerror(errno));
send_info(context->out, "I: I have a problem "
"reading your requested item.",
context->path);
send_eom(context->out);
exit(EXIT_FAILURE);
}
break;
}
}
free(block);
}
static void
write_text_file(struct context *context)
{
assert(context != NULL);
FILE *in = fopen(context->path, "r");
if (in == NULL) {
syslog(LOG_ERR, "fopen error: %m");
send_error(context->out, "E: fopen", strerror(errno));
send_info(context->out, "I: I could not open the requested "
"item.", context->path);
send_eom(context->out);
exit(EXIT_FAILURE);
}
void *line = malloc(LINE_MAX);
if (line == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(context->out, "E: malloc", strerror(errno));
send_info(context->out, "I: I could not allocate memory.",
NULL);
send_eom(context->out);
exit(EXIT_FAILURE);
}
while (fgets(line, LINE_MAX, in) != NULL) {
tool_strip_crlf(line);
send_line(context->out, line);
}
if (ferror(stdin)) {
syslog(LOG_ERR, "fgets error: %m");
send_error(context->out, "E: fgets", strerror(errno));
send_info(context->out, "I: I have a problem reading a "
"requested text file.", context->path);
send_eom(context->out);
exit(EXIT_FAILURE);
}
send_eom(context->out);
free(line);
}
static void
write_gophermap(struct opt_options *options, struct context *context,
const char *map)
{
assert(options != NULL);
assert(context != NULL);
FILE *in = fopen(map, "r");
if (in == NULL) {
syslog(LOG_ERR, "fopen error: %m");
send_error(context->out, "E: fopen", strerror(errno));
send_info(context->out, "I: I could not open a gophermap.",
map);
send_eom(context->out);
exit(EXIT_FAILURE);
}
void *line = malloc(LINE_MAX);
if (line == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(context->out, "E: malloc", strerror(errno));
send_info(context->out, "I: I could not allocate memory.",
NULL);
send_eom(context->out);
exit(EXIT_FAILURE);
}
while (fgets(line, LINE_MAX, in) != NULL) {
tool_strip_crlf(line);
if (strchr(line, '\t') != NULL) {
struct item item;
if (!gophermap_parse_item(options, &item,
context->selector, line, context->out)) {
send_info(context->out, "I: I encountered a "
"problem parsing a gophermap.", map);
continue;
}
send_item(context->out, &item);
gophermap_free_item(&item);
} else
send_info(context->out, line, NULL);
}
if (ferror(stdin)) {
syslog(LOG_ERR, "fgets error: %m");
send_error(context->out, "E: fgets", strerror(errno));
send_info(context->out, "I: I have a problem reading a "
"gophermap.", map);
send_eom(context->out);
exit(EXIT_FAILURE);
}
send_eom(context->out);
free(line);
}