-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbconfig.c
715 lines (614 loc) · 14.9 KB
/
mbconfig.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/* mbsync(1) configuration parser. */
#include <ctype.h>
#include <errno.h>
#include <openssl/ssl.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "mbconfig.h"
enum {
ERR = -1,
NONE = 0,
OK = 1,
};
#define MBIDLED_CMD_PREFIX "#MBIDLED:"
#define ISARG(kw) (strcmp(ctx->arg, kw) == 0)
#define ISPREFIXARG(kw) (strncmp(ctx->arg, kw, strlen(kw)) == 0)
#define ALLOC_DATA(slist_head, type) \
struct type *data = calloc(1, sizeof *data); \
if (data == NULL) \
abort(); \
SLIST_INSERT_HEAD(&ctx->config->slist_head, data, link);
#define SECTION_FOREACH \
int rc = parse_str(ctx, &data->name); \
while (rc != ERR && (rc = read_line(ctx)) == OK && (rc = read_kw(ctx)) == OK && \
(rc = preprocess_cmd(ctx, 0)) == OK)
static int
read_arg(struct mbconfig_parser *ctx, int caseless)
{
for (;; ctx->col += 1) {
char const *s = ctx->buf + ctx->col;
if (*s == '\0')
return NONE;
if (isspace(*s))
continue;
break;
}
int quoted = 0;
int escaped = 0;
ctx->arg = ctx->buf + ctx->col;
char *p = ctx->arg;
for (;;) {
char c = ctx->buf[ctx->col];
if (c == '\0')
break;
ctx->col += 1;
if (!escaped && c == '\\') {
escaped = 1;
} else if (!escaped && c == '"') {
quoted ^= 1;
} else if (!escaped && !quoted && isspace(c)) {
break;
} else {
if (caseless && islower(c))
c = toupper(c);
*p++ = c;
escaped = 0;
}
}
if (quoted) {
ctx->error_msg = "Unterminated quoted string";
return ERR;
}
if (escaped) {
ctx->error_msg = "Unterminated escape sequence";
return ERR;
}
*p = '\0';
return OK;
}
static int
read_kw(struct mbconfig_parser *ctx)
{
return read_arg(ctx, 1);
}
static int
read_str(struct mbconfig_parser *ctx)
{
return read_arg(ctx, 0);
}
static int
expect_kw(struct mbconfig_parser *ctx)
{
int rc = read_kw(ctx);
if (rc == NONE) {
ctx->error_msg = "Argument expected";
rc = ERR;
}
return rc;
}
static int
expect_str(struct mbconfig_parser *ctx)
{
int rc = read_str(ctx);
if (rc == NONE) {
ctx->error_msg = "Argument expected";
rc = ERR;
}
return rc;
}
static int
read_line(struct mbconfig_parser *ctx)
{
int rc = read_str(ctx);
if (rc) {
ctx->error_msg = "Extra arguments";
return ERR;
}
do {
ctx->lnum += 1;
ctx->col = 0;
*ctx->buf = '\0';
ctx->arg = ctx->buf;
if (fgets(ctx->buf, sizeof ctx->buf, ctx->stream) == NULL) {
if (ferror(ctx->stream)) {
ctx->error_msg = "I/O error";
return ERR;
}
return NONE;
}
ctx->buf[strcspn(ctx->buf, "\r\n")] = '\0';
if (strncmp(MBIDLED_CMD_PREFIX, ctx->buf, strlen(MBIDLED_CMD_PREFIX)) == 0)
break;
} while (*ctx->buf == '#');
return OK;
}
static int
parse_str(struct mbconfig_parser *ctx, char **data)
{
int rc = expect_str(ctx);
if (rc == OK) {
free(*data);
*data = strdup(ctx->arg);
if (*data == NULL)
abort();
}
return rc;
}
static int
parse_int(struct mbconfig_parser *ctx, int *data)
{
int rc = expect_str(ctx);
if (rc != OK)
return rc;
char *end;
errno = 0;
*data = strtol(ctx->arg, &end, 10);
if (ctx->arg == end || errno) {
ctx->error_msg = "Invalid number";
return ERR;
} else if (*end != '\0') {
ctx->error_msg = "Junk after number";
return ERR;
}
return OK;
}
static int
parse_path(struct mbconfig_parser *ctx, char **data)
{
int rc = expect_str(ctx);
if (rc != OK)
return rc;
free(*data);
char *s = ctx->arg;
if (*s == '~') {
s += 1;
char *slash = s + strcspn(s, "/");
struct passwd *pw;
if (s == slash) {
pw = getpwuid(geteuid());
} else {
char old = *slash;
*slash = '\0';
pw = getpwnam(s);
*slash = old;
}
int n = snprintf(NULL, 0, "%s%s", pw->pw_dir, slash);
*data = malloc(n + 1 /* NUL */);
if (*data == NULL)
abort();
sprintf(*data, "%s%s", pw->pw_dir, slash);
} else {
*data = strdup(s);
if (*data == NULL)
abort();
}
return OK;
}
static int
parse_bool(struct mbconfig_parser *ctx, int *data)
{
int rc = expect_kw(ctx);
if (rc == ERR)
return ERR;
if (ISARG("YES") || ISARG("TRUE") || ISARG("ON") || ISARG("1")) {
*data = 1;
} else if (ISARG("NO") || ISARG("FALSE") || ISARG("OFF") || ISARG("0")) {
*data = 0;
} else {
ctx->error_msg = "Invalid boolean value";
return ERR;
}
return OK;
}
static int
parse_store(struct mbconfig_parser *ctx, struct mbconfig_store *data)
{
if (expect_str(ctx) == ERR)
return ERR;
if (*ctx->arg != ':') {
ctx->error_msg = "Expected ':' before store name";
return ERR;
}
ctx->arg += 1;
char const *store = ctx->arg;
char *sep = strchr(ctx->arg, ':');
if (sep == NULL) {
ctx->error_msg = "Expected ':' after store name";
return ERR;
}
*sep = '\0';
do {
struct mbconfig_imap_store *imap_store;
SLIST_FOREACH (imap_store, &ctx->config->imap_stores, link)
if (strcmp(store, imap_store->name) == 0)
break;
if (imap_store) {
data->type = MBCONFIG_STORE_IMAP;
data->imap_store = imap_store;
break;
}
struct mbconfig_maildir_store *maildir_store;
SLIST_FOREACH (maildir_store, &ctx->config->maildir_stores, link)
if (strcmp(store, maildir_store->name) == 0)
break;
if (maildir_store) {
data->type = MBCONFIG_STORE_MAILDIR;
data->maildir_store = maildir_store;
break;
}
ctx->error_msg = "No such IMAPStore or MaildirStore";
return ERR;
} while (0);
free(data->mailbox);
data->mailbox = strdup(sep + 1);
if (data->mailbox == NULL)
abort();
return OK;
}
static int
parse_patterns(struct mbconfig_parser *ctx, struct mbconfig_str_list *data)
{
/* We barely care about memory leaks. */
SLIST_INIT(data);
int rc;
while (0 < (rc = read_str(ctx))) {
struct mbconfig_str *pattern = calloc(1, sizeof *pattern);
if (pattern == NULL)
abort();
pattern->str = strdup(ctx->arg);
if (pattern->str == NULL)
abort();
/* Note that patterns are in reverse order. */
SLIST_INSERT_HEAD(data, pattern, link);
}
if (0 <= rc)
rc = 1;
return rc;
}
static int
preprocess_cmd(struct mbconfig_parser *ctx, int global)
{
int rc = 1;
struct mbconfig_mbidled_channel *c = &ctx->channel_config[global];
if (ISARG(MBIDLED_CMD_PREFIX "STRICTPROPAGATE")) {
if ((rc = expect_kw(ctx)) != OK)
return rc;
if (ISARG("NONE")) {
c->strict_propagate = 0;
} else if (ISARG("FAR")) {
c->strict_propagate = MBCONFIG_PROPAGATE_FAR;
} else if (ISARG("NEAR")) {
c->strict_propagate = MBCONFIG_PROPAGATE_NEAR;
} else if (ISARG("BOTH")) {
c->strict_propagate = MBCONFIG_PROPAGATE_NEAR | MBCONFIG_PROPAGATE_FAR;
} else {
ctx->error_msg = "Unknown argument";
return ERR;
}
} else if (ISARG(MBIDLED_CMD_PREFIX "STARTTIMEOUT")) {
rc = parse_int(ctx, &c->start_timeout);
} else if (ISARG(MBIDLED_CMD_PREFIX "STARTINTERVAL")) {
rc = parse_int(ctx, &c->start_interval);
} else if (ISPREFIXARG(MBIDLED_CMD_PREFIX)) {
ctx->arg += strlen(MBIDLED_CMD_PREFIX);
}
if (global)
/* Reset local config to global. */
memcpy(&ctx->channel_config[0],
&ctx->channel_config[1],
sizeof ctx->channel_config[0]);
return rc;
}
static void
skip_unknown_cmd(struct mbconfig_parser *ctx)
{
ctx->buf[ctx->col] = '\0';
}
static int
parse_imap_account_section(struct mbconfig_parser *ctx)
{
ALLOC_DATA(imap_accounts, mbconfig_imap_account);
data->system_certs = 1;
data->login_auth = 1;
data->ssl = MBCONFIG_SSL_STARTTLS;
data->ssl_versions = SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1;
SECTION_FOREACH
{
if (ISARG("HOST")) {
rc = parse_str(ctx, &data->host);
} else if (ISARG("PORT")) {
rc = parse_str(ctx, &data->port);
} else if (ISARG("USER")) {
rc = parse_str(ctx, &data->user);
} else if (ISARG("USERCMD")) {
rc = parse_str(ctx, &data->user_cmd);
} else if (ISARG("PASS")) {
rc = parse_str(ctx, &data->pass);
} else if (ISARG("PASSCMD")) {
rc = parse_str(ctx, &data->pass_cmd);
} else if (ISARG("TUNNEL")) {
rc = parse_str(ctx, &data->tunnel_cmd);
} else if (ISARG("AUTHMECHS")) {
data->login_auth = 0;
while ((rc = read_kw(ctx)) == OK)
data->login_auth |= ISARG("*") || ISARG("LOGIN");
} else if (ISARG("TLSTYPE") || ISARG("SSLTYPE")) {
if ((rc = expect_kw(ctx)) != OK)
continue;
if (ISARG("NONE")) {
data->ssl = MBCONFIG_SSL_NONE;
} else if (ISARG("STARTTLS")) {
data->ssl = MBCONFIG_SSL_STARTTLS;
} else if (ISARG("IMAPS")) {
data->ssl = MBCONFIG_SSL_IMAPS;
} else {
ctx->error_msg = "Unknown argument";
return ERR;
}
} else if (ISARG("TLSVERSIONS")) {
while ((rc = read_kw(ctx)) == OK) {
int op = *ctx->arg;
ctx->arg += 1;
int version;
if (ISARG("1.0")) {
version = SSL_OP_NO_TLSv1;
} else if (ISARG("1.1")) {
version = SSL_OP_NO_TLSv1_1;
} else if (ISARG("1.2")) {
version = SSL_OP_NO_TLSv1_2;
} else if (ISARG("1.3")) {
version = SSL_OP_NO_TLSv1_3;
} else {
ctx->error_msg = "Unrecognized TLS version";
return ERR;
}
if (op == '-') {
data->ssl_versions |= version;
} else if (op == '+') {
data->ssl_versions &= ~version;
} else {
ctx->error_msg = "+ OR - expected";
return ERR;
}
}
} else if (ISARG("SSLVERSIONS")) {
#define ARGS \
/* xmacro(name, op) */ \
xmacro("SSLV3", SSL_OP_NO_SSLv3) xmacro("TLSV1", SSL_OP_NO_TLSv1) \
xmacro("TLSV1.1", SSL_OP_NO_TLSv1_1) xmacro("TLSV1.2", SSL_OP_NO_TLSv1_2) \
xmacro("TLSV1.3", SSL_OP_NO_TLSv1_3)
#define xmacro(name, op) | op
data->ssl_versions = 0 ARGS;
#undef xmacro
while ((rc = read_kw(ctx)) == OK) {
if (0)
(void)0;
#define xmacro(name, op) else if (ISARG(name)) data->ssl_versions &= ~(op);
ARGS
#undef xmacro
}
#undef ARGS
} else if (ISARG("SYSTEMCERTIFICATES")) {
rc = parse_bool(ctx, &data->system_certs);
} else if (ISARG("CERTIFICATEFILE")) {
rc = parse_path(ctx, &data->cert_file);
} else if (ISARG("CIPHERSTRING")) {
rc = parse_str(ctx, &data->ciphers);
} else {
skip_unknown_cmd(ctx);
}
}
if (rc == ERR)
return ERR;
if (data->user == NULL && data->user_cmd == NULL) {
ctx->error_msg = "Neither User nor UserCmd present";
return ERR;
}
if (data->pass == NULL && data->pass_cmd == NULL) {
ctx->error_msg = "Neither Pass nor PassCmd present";
return ERR;
}
if (data->host == NULL && data->tunnel_cmd == NULL) {
ctx->error_msg = "Neither Host nor Tunnel present";
return ERR;
}
return OK;
}
static int
parse_imap_store_section(struct mbconfig_parser *ctx)
{
ALLOC_DATA(imap_stores, mbconfig_imap_store);
SECTION_FOREACH
{
if (ISARG("ACCOUNT")) {
if ((rc = expect_str(ctx)) <= 0)
break;
struct mbconfig_imap_account *account;
SLIST_FOREACH (account, &ctx->config->imap_accounts, link)
if (strcmp(ctx->arg, account->name) == 0)
break;
if (account == NULL) {
ctx->error_msg = "No such IMAPAccount";
return ERR;
}
data->account = account;
} else {
skip_unknown_cmd(ctx);
}
}
if (0 <= rc && data->account == NULL) {
ctx->error_msg = "Missing required Account";
return ERR;
}
return rc;
}
static int
parse_maildir_store_section(struct mbconfig_parser *ctx)
{
ALLOC_DATA(maildir_stores, mbconfig_maildir_store);
SECTION_FOREACH
{
if (ISARG("PATH"))
rc = parse_path(ctx, &data->path);
else if (ISARG("INBOX"))
rc = parse_path(ctx, &data->inbox);
else
skip_unknown_cmd(ctx);
}
if (0 <= rc && data->path == NULL) {
ctx->error_msg = "Missing required Path";
return ERR;
}
return rc;
}
static int
parse_channel_section(struct mbconfig_parser *ctx)
{
ALLOC_DATA(channels, mbconfig_channel);
data->sync = MBCONFIG_SYNC_PUSH | MBCONFIG_SYNC_PULL;
SECTION_FOREACH
{
if (ISARG("FAR")) {
rc = parse_store(ctx, &data->far);
} else if (ISARG("NEAR")) {
rc = parse_store(ctx, &data->near);
} else if (ISARG("PATTERN") || ISARG("PATTERNS")) {
rc = parse_patterns(ctx, &data->patterns);
} else if (ISARG("SYNC")) {
data->sync = 0;
while ((rc = read_kw(ctx)) == OK) {
if (ISARG("NONE"))
/* Nop. */;
else if (ISPREFIXARG("PULL"))
data->sync |= MBCONFIG_SYNC_PULL;
else if (ISPREFIXARG("PUSH"))
data->sync |= MBCONFIG_SYNC_PUSH;
else
/* Specifying flag both pulls and pushes. */
data->sync |= MBCONFIG_SYNC_PULL | MBCONFIG_SYNC_PUSH;
}
} else {
skip_unknown_cmd(ctx);
}
}
if (rc == ERR)
return rc;
if (data->near.store == NULL) {
ctx->error_msg = "Missing required Near";
return ERR;
}
if (data->far.store == NULL) {
ctx->error_msg = "Missing required Far";
return ERR;
}
memcpy(&data->mbidled, &ctx->channel_config[0], sizeof data->mbidled);
return OK;
}
int
mbconfig_parse(struct mbconfig_parser *ctx, char const *filename)
{
struct mbconfig *config = ctx->config;
ctx->channel_config[1] = (struct mbconfig_mbidled_channel){
.start_timeout = 1,
.start_interval = 30,
.strict_propagate = MBCONFIG_PROPAGATE_NEAR | MBCONFIG_PROPAGATE_FAR,
};
SLIST_INIT(&config->imap_accounts);
SLIST_INIT(&config->imap_stores);
SLIST_INIT(&config->maildir_stores);
SLIST_INIT(&config->channels);
ctx->lnum = 0;
ctx->col = 0;
*ctx->buf = '\0';
ctx->arg = ctx->buf;
config->filename = strdup(filename);
if (config->filename == NULL)
abort();
ctx->stream = fopen(filename, "r");
if (ctx->stream == NULL) {
free(config->filename);
ctx->error_msg = strerror(errno);
return ERR;
}
int rc = NONE;
while (rc != ERR && (rc = read_line(ctx)) == OK && (rc = read_kw(ctx)) != ERR &&
(rc = preprocess_cmd(ctx, 1)) == OK) {
if (ISARG("IMAPACCOUNT"))
rc = parse_imap_account_section(ctx);
else if (ISARG("IMAPSTORE"))
rc = parse_imap_store_section(ctx);
else if (ISARG("MAILDIRSTORE"))
rc = parse_maildir_store_section(ctx);
else if (ISARG("CHANNEL"))
rc = parse_channel_section(ctx);
else
skip_unknown_cmd(ctx);
}
fclose(ctx->stream);
return rc == ERR ? -1 : 0;
}
void
mbconfig_eval_cmd_option(char **option, char const *option_cmd)
{
if (*option != NULL)
return;
char buf[8192];
option_cmd += *option_cmd == '+';
FILE *stream = popen(option_cmd, "r");
if (stream == NULL)
return;
char *ok = fgets(buf, sizeof buf, stream);
pclose(stream);
if (ok == NULL)
return;
char *s = strchr(buf, '\n');
if (s != NULL)
*s = '\0';
*option = strdup(buf);
}
static int
match_pattern(char const *pat, char const *s)
{
if (*pat == '*') {
return
/* Continue * match. */
(*s && match_pattern(pat, s + 1)) ||
/* End of * match. */
match_pattern(pat + 1, s);
} else if (*pat == '%') {
/* '/' seems to be the hardcoded hierarchy delimiter. */
return
/* Continue % match. */
(*s && *s != '/' && match_pattern(pat, s + 1)) ||
/* End of % match. */
match_pattern(pat + 1, s);
} else if (*pat == *s) {
/* Accept. */
if (*s == '\0')
return 1;
/* Next. */
return match_pattern(pat + 1, s + 1);
} else {
/* Reject. */
return 0;
}
}
int
mbconfig_patterns_test(struct mbconfig_str_list const *patterns, char const *s)
{
if (SLIST_EMPTY(patterns))
return 1;
struct mbconfig_str *pattern;
SLIST_FOREACH (pattern, patterns, link) {
int bang;
char const *pat = pattern->str;
pat += (bang = (*pat == '!'));
if (match_pattern(pat, s))
return !bang;
}
return 0;
}