-
Notifications
You must be signed in to change notification settings - Fork 8
/
rewrite.c
700 lines (604 loc) · 18.9 KB
/
rewrite.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#define FUSE_USE_VERSION 31
#define _GNU_SOURCE
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fuse.h>
#include <fuse_opt.h>
#include <pcre.h>
#include "rewrite.h"
#define DEBUG(lvl, x...) if(config.verbose >= lvl) fprintf(stderr, x)
/*
* Type definiton
*/
struct regexp {
pcre *regexp;
pcre_extra *extra;
int captures;
int replace_all;
char *raw;
};
struct replacement_part {
int group;
char *data;
int len;
};
struct replacement_template {
int nparts;
char *raw;
struct replacement_part *parts;
};
struct rewrite_rule {
struct regexp *filename_regexp;
struct replacement_template *rewritten_path; /* NULL for "." */
struct rewrite_rule *next;
};
struct rewrite_context {
struct regexp *cmdline; /* NULL for all contexts */
struct rewrite_rule *rules;
struct rewrite_context *next;
};
struct config {
char *config_file;
char *orig_fs;
int orig_fd;
char *mount_point;
struct rewrite_context *contexts;
int verbose;
int autocreate;
};
enum type {
CMDLINE,
RULE,
END
};
/*
* Global variables
*/
static struct config config;
/*
* Config-file parsing
*/
static void *abmalloc(size_t sz) {
void *res = malloc(sz);
if(res == NULL) {
perror("malloc");
abort();
}
return res;
}
/* Consume all blanks (according to isspace) */
static void parse_blanks(FILE *fd) {
int c;
do {
c = getc(fd);
} while(isspace(c) && c != EOF);
ungetc(c, fd);
}
/* Consume all characters until reaching EOL */
static void parse_comment(FILE *fd) {
int c;
do {
c = getc(fd);
} while(c != '\n' && c != EOF);
}
static char *string_new(int *string_cap, int *string_size) {
*string_cap = 255;
*string_size = 0;
char *s = abmalloc(*string_cap);
s[0] = '\0';
return s;
}
/* append c to string, extending it if necessary */
static void string_append(char **string, char c, int *string_cap, int *string_size) {
if(*string_cap == *string_size + 1) {
*string_cap *= 2;
*string = realloc(*string, *string_cap);
if(*string == NULL) {
perror("realloc");
abort();
}
}
(*string)[(*string_size)++] = c;
(*string)[*string_size] = 0;
}
/* Consume the string until reaching sep */
static void parse_string(FILE *fd, char **string, char sep) {
int string_cap, string_size;
int escaped = 0;
int c;
*string = string_new(&string_cap, &string_size);
for(;;) {
c = getc(fd);
if(c == EOF) {
fprintf(stderr, "Unexpected EOF\n");
exit(1);
}
if(escaped) {
/* \\ -> \
* \(sep) -> (sep)
* \(other) -> \(other)
*/
escaped = 0;
if(c != '\\' && c != (int)sep) {
string_append(string, '\\', &string_cap, &string_size);
}
string_append(string, c, &string_cap, &string_size);
} else {
if(c == '\\') {
escaped = 1;
} else if(c == (int)sep) {
break;
} else {
string_append(string, c, &string_cap, &string_size);
}
}
}
}
/* Consume the regexp (until reaching end-of-flags) and put it in regexp */
static void parse_regexp(FILE *fd, struct regexp **regexp, char sep) {
char *regexp_body;
int regexp_flags = 0;
int replace_all = 0;
const char *error;
int offset;
int c;
/* Determine separator */
if(sep == 0) {
sep = getc(fd);
if(sep == 'm') {
sep = getc(fd);
} else if(sep != '/') {
fprintf(stderr, "Unexpected character \"%c\"\n", (char)sep);
exit(1);
}
}
if(sep == EOF) {
fprintf(stderr, "Unexpected EOF\n");
exit(1);
}
/* Get body */
parse_string(fd, ®exp_body, sep);
/* Get flags */
while(!isspace(c = getc(fd))) {
switch(c) {
case 'i':
regexp_flags |= PCRE_CASELESS;
break;
case 'x':
regexp_flags |= PCRE_EXTENDED;
break;
case 'u':
regexp_flags |= PCRE_UCP | PCRE_UTF8;
break;
case 'g':
replace_all = 1;
break;
case EOF:
fprintf(stderr, "Unexpected EOF\n");
exit(1);
default:
fprintf(stderr, "Unknown flag %c\n", (char)c);
exit(1);
}
}
/* Compilation */
*regexp = abmalloc(sizeof(struct regexp));
(*regexp)->replace_all = replace_all;
(*regexp)->regexp = pcre_compile(regexp_body, regexp_flags, &error, &offset, NULL);
if((*regexp)->regexp == NULL) {
fprintf(stderr, "Invalid regular expression: %s\n. Regular expression was :\n %s\n", error, regexp_body);
exit(1);
}
(*regexp)->extra = pcre_study((*regexp)->regexp, 0, &error);
if((*regexp)->extra == NULL && error != NULL) {
fprintf(stderr, "Can't compile regular expression: %s\n. Regular expression was :\n %s\n", error, regexp_body);
exit(1);
}
pcre_fullinfo((*regexp)->regexp, (*regexp)->extra, PCRE_INFO_CAPTURECOUNT, &(*regexp)->captures);
(*regexp)->raw = regexp_body;
}
/* Get a CMDLINE or RULE definition */
static void parse_item(FILE *fd, enum type *type, struct regexp **regexp, char **string) {
int c;
parse_blanks(fd);
switch(c = getc(fd)) {
case '-':
*type = CMDLINE;
parse_blanks(fd);
parse_regexp(fd, regexp, 0);
return;
case 'm':
c = getc(fd);
/* continue */
case '/':
*type = RULE;
parse_regexp(fd, regexp, (char)c);
parse_blanks(fd);
parse_string(fd, string, '\n');
return;
case '#':
parse_comment(fd);
parse_item(fd, type, regexp, string);
return;
case EOF:
*type = END;
return;
default:
fprintf(stderr, "Unexpected character \"%c\"\n", (char)c);
exit(1);
}
}
static struct replacement_part *alloc_part(struct replacement_part **parts, int *nparts) {
*parts = reallocarray(*parts, ++(*nparts), sizeof(struct replacement_part));
if(*parts == NULL) {
perror("reallocarray");
abort();
}
return (*parts) + (*nparts - 1);
}
static struct replacement_template *parse_replacement_template(char *tpl) {
int buf_cap, buf_size;
char *buf = string_new(&buf_cap, &buf_size);
int nparts = 0;
struct replacement_part *parts = NULL;
struct replacement_part *cur_part;
int escaped = 0;
for(int i = 0; i < strlen(tpl); i++) {
if(escaped) {
if(tpl[i] >= '0' && tpl[i] <= '9') {
if(buf_size > 0) {
cur_part = alloc_part(&parts, &nparts);
cur_part->data = buf;
cur_part->len = buf_size;
buf = string_new(&buf_cap, &buf_size);
}
cur_part = alloc_part(&parts, &nparts);
cur_part->data = NULL;
cur_part->group = tpl[i] - '0';
} else {
string_append(&buf, tpl[i], &buf_cap, &buf_size);
}
escaped = 0;
} else {
if(tpl[i] == '\\') {
escaped = 1;
} else {
string_append(&buf, tpl[i], &buf_cap, &buf_size);
}
}
}
if(nparts == 0 || buf_size > 0) {
cur_part = alloc_part(&parts, &nparts);
cur_part->data = buf;
cur_part->len = buf_size;
}
struct replacement_template *res = abmalloc(sizeof(struct replacement_template));
res->parts = parts;
res->nparts = nparts;
res->raw = tpl;
return res;
}
static void parse_config(FILE *fd) {
enum type type;
struct regexp *regexp;
char *string;
struct rewrite_rule *rule, *last_rule = NULL;
struct rewrite_context *new_context;
struct rewrite_context *current_context = abmalloc(sizeof(struct rewrite_context));
current_context->cmdline = NULL;
current_context->rules = NULL;
current_context->next = NULL;
config.contexts = current_context;
do {
parse_item(fd, &type, ®exp, &string);
if(type == CMDLINE) {
new_context = abmalloc(sizeof(struct rewrite_context));
new_context->cmdline = !strcmp(regexp->raw, "") ? NULL : regexp;
new_context->rules = last_rule = NULL;
new_context->next = NULL;
current_context->next = new_context;
current_context = new_context;
} else if(type == RULE) {
rule = abmalloc(sizeof(struct rewrite_rule));
rule->filename_regexp = regexp;
rule->rewritten_path = (!strcmp(string, ".")) ? (free(string), NULL) : parse_replacement_template(string);
rule->next = NULL;
if(last_rule)
last_rule->next = rule;
last_rule = rule;
if(current_context->rules == NULL)
current_context->rules = rule;
}
} while(type != END);
}
/*
* Command-line arguments parsing
*/
enum {
KEY_HELP,
KEY_VERSION,
};
#define REWRITE_OPT(t, p, v) { t, offsetof(struct config, p), v }
static struct fuse_opt options[] = {
REWRITE_OPT("config=%s", config_file, 0),
REWRITE_OPT("verbose=%i", verbose, 0),
REWRITE_OPT("autocreate", autocreate, 1),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_END
};
static int options_proc(void *data, const char *arg, int key, struct fuse_args *outargs) {
switch(key) {
case FUSE_OPT_KEY_NONOPT:
if(config.orig_fs == NULL) {
config.orig_fs = strdup(arg);
return 0;
} else if(config.mount_point == NULL) {
config.mount_point = strdup(arg);
return 1;
} else {
fprintf(stderr, "Invalid argument: %s\n", arg);
exit(1);
}
break;
case KEY_HELP:
fprintf(stderr,
"usage: %s [-o options] source mountpoint\n"
"\n"
"rewritefs options:\n"
" -o opt,[opt...] mount options (see mount.fuse)\n"
" -h --help Fuse help\n"
" -V --version print version\n"
" -f foreground\n"
" -d debug\n"
" -o config=CONFIG path to configuration file\n"
" -o verbose=LEVEL verbose level [to be used with -f or -d] (LEVEL is 1 to 4)\n"
"\n",
outargs->argv[0]);
fuse_opt_add_arg(outargs, "-ho");
fuse_main(outargs->argc, outargs->argv, NULL, NULL);
exit(0);
case KEY_VERSION:
fuse_opt_add_arg(outargs, "--version");
fuse_main(outargs->argc, outargs->argv, NULL, NULL);
exit(0);
}
return 1;
}
void parse_args(int argc, char **argv, struct fuse_args *outargs) {
FILE *fd;
memset(&config, 0, sizeof(config));
fuse_opt_parse(outargs, &config, options, options_proc);
fuse_opt_add_arg(outargs, "-o");
fuse_opt_add_arg(outargs, "default_permissions");
if(config.orig_fs == NULL) {
fprintf(stderr, "missing source argument\n");
exit(1);
} else {
config.orig_fd = open(config.orig_fs, O_PATH);
if(config.orig_fd == -1) {
fprintf(stderr, "Cannot open source directory: %s\n", strerror(errno));
exit(1);
}
}
if(config.mount_point == NULL) {
fprintf(stderr, "missing mount point argument\n");
exit(1);
}
if(config.config_file) {
fd = fopen(config.config_file, "r");
if(fd == NULL) {
perror("opening config file");
exit(1);
}
parse_config(fd);
fclose(fd);
struct rewrite_context *ctx;
struct rewrite_rule *rule;
for(ctx = config.contexts; ctx != NULL; ctx = ctx->next) {
DEBUG(1, "CTX \"%s\":\n", ctx->cmdline ? ctx->cmdline->raw : "default");
for(rule = ctx->rules; rule != NULL; rule = rule->next)
DEBUG(1, " \"%s\" -> \"%s\"\n", rule->filename_regexp->raw, rule->rewritten_path ? rule->rewritten_path->raw : "(don't rewrite)");
}
DEBUG(1, "\n");
}
}
/*
* Rewrite stuff
*/
char *get_caller_cmdline() {
char path[PATH_MAX];
FILE *fd;
int size = 0, cap = 255, c;
char *ret = malloc(cap);
if(ret == NULL) {
return NULL;
} else {
*ret = 0;
}
snprintf(path, PATH_MAX, "/proc/%d/cmdline", fuse_get_context()->pid);
fd = fopen(path, "r");
if(fd == NULL)
return ret;
while((c = getc(fd)) != EOF) {
if(c == 0)
c = ' ';
string_append(&ret, c, &cap, &size);
}
fclose(fd);
return ret;
}
/* Recursively create all parent directories in `path`. */
static int mkdir_parents(const char *path, mode_t mode) {
int result = 0;
/* dirname() could clobber its argument. */
char *path_ = strdup(path);
/* dirname() may statically allocate the result; we need to copy so we don’t */
char *dir = strdup(dirname(path_));
struct stat dirstat;
errno = 0;
if ((fstatat(config.orig_fd, dir, &dirstat, 0) == -1) && errno == ENOENT) {
if (mkdir_parents(dir, mode)) {
result = -1;
goto done;
}
WLOCK(result = mkdirat(config.orig_fd, dir, mode));
if (result == -1)
goto done;
}
done:
free(dir);
free(path_);
return result;
}
char *regexp_replace(struct regexp *re, const char *subject, struct replacement_template *tpl) {
int nvec, repl_sz, *ovector = NULL;
char *result, *repl, *repl_buf = NULL, *suffix_buf = NULL;
const char *suffix;
/* Fill ovector */
nvec = (re->captures + 1) * 3;
ovector = calloc(nvec, sizeof(int));
if(ovector == NULL) {
result = NULL;
goto end;
}
int scount = pcre_exec(re->regexp, re->extra, subject,
strlen(subject), 0, 0, ovector, nvec);
if(scount == PCRE_ERROR_NOMATCH)
return strdup(subject);
/* Replace backreferences */
if(tpl->nparts > 1 || tpl->parts[0].data == NULL) {
int i, wpos, group;
for(i = 0, repl_sz = 0; i < tpl->nparts; i++) {
if(tpl->parts[i].data == NULL) {
group = tpl->parts[i].group;
if(group < scount) {
repl_sz += ovector[group*2+1] - ovector[group*2];
}
} else {
repl_sz += tpl->parts[i].len;
}
}
repl = repl_buf = malloc(repl_sz + 1);
if(repl == NULL) {
result = NULL;
goto end;
}
for(i = 0, wpos = 0; i < tpl->nparts; i++) {
if(tpl->parts[i].data == NULL) {
group = tpl->parts[i].group;
if(group < scount) {
strncpy(repl + wpos, subject + ovector[group*2], ovector[group*2+1]-ovector[group*2]);
wpos += ovector[group*2+1] - ovector[group*2];
}
} else {
strcpy(repl + wpos, tpl->parts[i].data);
wpos += tpl->parts[i].len;
}
}
repl[wpos] = '\0';
} else {
repl = tpl->parts[0].data;
repl_sz = tpl->parts[0].len;
}
suffix = subject + ovector[1];
if(re->replace_all && suffix[0] != '\0') {
suffix = suffix_buf = regexp_replace(re, suffix, tpl);
if(suffix == NULL) {
result = NULL;
goto end;
}
}
DEBUG(4, " subject = %s\n", subject);
DEBUG(4, " prefix = %s\n", strndup(subject, ovector[0]));
DEBUG(4, " replaced match = %s\n", repl);
DEBUG(4, " suffix = %s\n", suffix);
result = malloc(ovector[0] + repl_sz + strlen(suffix) + 1);
if(result == NULL) {
result = NULL;
goto end;
}
result[0] = 0;
strncat(result, subject, ovector[0]);
strcat(result, repl);
strcat(result, suffix);
end:
free(repl_buf);
free(suffix_buf);
free(ovector);
return result;
}
char *apply_rule(const char *path, struct rewrite_rule *rule) {
if(rule == NULL || rule->rewritten_path == NULL) {
DEBUG(2, " (ignored) %s -> %s\n", path, path + 1);
DEBUG(3, "\n");
return strdup(path[1] == '\0' ? "." : path+1);
}
char *rewritten = regexp_replace(rule->filename_regexp, path + 1, rule->rewritten_path);
if(config.autocreate) {
if(mkdir_parents(rewritten, (S_IRWXU | S_IRWXG | S_IRWXO)) == -1)
fprintf(stderr, "Warning: %s -> %s: autocreating parents failed: %s\n",
path, rewritten, strerror(errno));
}
DEBUG(1, " %s -> %s\n", path, rewritten);
DEBUG(3, "\n");
return rewritten;
}
char *rewrite(const char *path) {
struct rewrite_context *ctx;
struct rewrite_rule *rule;
char *caller = NULL;
int res;
DEBUG(3, "%s:\n", path);
for(ctx = config.contexts; ctx != NULL; ctx = ctx->next) {
if(ctx->cmdline) {
if(!caller) {
caller = get_caller_cmdline();
if(caller == NULL) {
fprintf(stderr, "WARNING: cannot obtain caller command line\n");
continue;
}
}
res = pcre_exec(ctx->cmdline->regexp, ctx->cmdline->extra, caller,
strlen(caller), 0, 0, NULL, 0);
if(res < 0) {
if(res != PCRE_ERROR_NOMATCH)
fprintf(stderr, "WARNING: pcre_exec returned %d\n", res);
DEBUG(3, " CTX NOMATCH \"%s\"\n", ctx->cmdline->raw);
continue;
}
DEBUG(3, " CTX OK \"%s\"\n", ctx->cmdline->raw);
} else {
DEBUG(3, " CTX DEFAULT\n");
}
for(rule = ctx->rules; rule != NULL; rule = rule->next) {
res = pcre_exec(rule->filename_regexp->regexp, rule->filename_regexp->extra, path + 1,
strlen(path) - 1, 0, 0, NULL, 0);
if(res < 0) {
if(res != PCRE_ERROR_NOMATCH)
fprintf(stderr, "WARNING: pcre_exec returned %d\n", res);
DEBUG(3, " RULE NOMATCH \"%s\"\n", rule->filename_regexp->raw);
} else {
DEBUG(3, " RULE OK \"%s\" \"%s\"\n", rule->filename_regexp->raw, rule->rewritten_path ? rule->rewritten_path->raw : "(don't rewrite)");
free(caller);
return apply_rule(path, rule);
}
}
}
free(caller);
return apply_rule(path, NULL);
}
int orig_fd() {
return config.orig_fd;
}