File: filter.c

package info (click to toggle)
prayer 1.3.5-dfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,576 kB
  • sloc: ansic: 43,161; makefile: 817; sh: 445; perl: 166
file content (787 lines) | stat: -rw-r--r-- 23,022 bytes parent folder | download | duplicates (4)
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/* $Cambridge: hermes/src/prayer/accountd/filter.c,v 1.4 2008/09/16 09:59:54 dpc22 Exp $ */
/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

/* Copyright (c) University of Cambridge 2000 - 2002 */
/* See the file NOTICE for conditions of use and distribution. */

#include "accountd.h"

/* Class for converting .MSforward rules into an Exim filter .forward file
 * Transliteration of some Perl that is used in our interactive menu system
 * interface, including a number of rather dubious rules */

struct filter {
    enum { CO_UNKNOWN, CO_VACATION, CO_SPAM,
        CO_SENDER, CO_RECIPIENT, CO_BLOCK, CO_SUBJECT,
        CO_REDIRECT
    } type;
    char *local_part;
    char *domain;
    char *subject;
    char *mailbox;
    char *address;
    char *threshold;
    BOOL copy;
};

/* Quoting for metacharacters: two levels of quoting required */
#define QMETA "\\\\"

/* Quoting for for wildcard characters: four levels of quoting required */
#define QWILD "\\\\\\\\"

/* ====================================================================== */

/* Some Support routines for processing wildcards */

/* filter_quote_double() *************************************************
 *
 * Print string to file, quoting '"' characters.
 *   file: Output file
 *      s: String to quote.
 ************************************************************************/

static void filter_quote_double(FILE * file, char *s)
{
    char c;

    while ((c = *s++)) {
        if (c == '"')
            putc('\\', file);
        putc(c, file);
    }
}

/* filter_has_wildcard() *************************************************
 *
 * Check whether string contains wildcard character.
 *      s: String to check.
 *
 * Returns: T => string contains wildcard. NIL otherwise.
 ************************************************************************/

static BOOL filter_has_wildcard(char *s)
{
    char c;

    while ((c = *s++)) {
        if ((c == '?') || (c == '*') || (c == '"'))
            return (T);
    }
    return (NIL);
}

/* filter_quote_wildcard() ***********************************************
 *
 * Print string to file, quoting wildcard characters.
 *   file: Output file
 *      s: String to quote.
 ************************************************************************/

static void filter_quote_wildcard(FILE * file, char *s)
{
    char c;

    while ((c = *s++)) {
        switch (c) {
        case '[':
        case ']':
        case '\'':
        case '|':
        case '.':
        case '+':
        case '^':
        case '$':
        case '?':
        case '*':
            fprintf(file, "%s", QWILD);
            putc(c, file);
            break;
        case '"':
            fputc('\\', file);
            putc(c, file);
            break;
        default:
            putc(c, file);
        }
    }
}

/* filter_expand_wildcard() **********************************************
 *
 * Print string to file, expanding simple wildcard characters (? and *)
 * into Perl 5 compatible regexps used by Exim.
 *   file: Output file
 *      s: String to quote.
 ************************************************************************/

static void filter_expand_wildcard(FILE * file, char *s)
{
    char c;

    while ((c = *s++)) {
        switch (c) {
        case '[':
        case ']':
        case '\'':
        case '|':
        case '.':
        case '+':
        case '^':
        case '$':
            /* Quota regexp significant character */
            fprintf(file, "%s", QWILD);
            putc(c, file);
            break;
        case '?':
            /* Translate '?' wildcard into appropriate Exim filter file syntax */
            fprintf(file, "%s.", QMETA);
            break;
        case '*':
            /* Translate '*' wildcard into appropriate Exim filter file syntax */
            fprintf(file, "%s.%s*", QMETA, QMETA);
            break;
        case '"':
            fputc('\\', file);
            putc(c, file);
            break;
        default:
            putc(c, file);
        }
    }
}

/* ====================================================================== */

/* filter_expand_local() *************************************************
 *
 * Print local part to file, expanding simple wildcard characters (? and
 * into Perl 5 compatible regexps used by Exim.
 *   file: Output file
 *      s: String to quote.
 ************************************************************************/

static void filter_expand_local(FILE * file, char *s)
{
    if (*s == '"')
        filter_quote_wildcard(file, s);
    else
        filter_expand_wildcard(file, s);
}

/* filter_expand_domain() *************************************************
 *
 * Print domain to file, expanding simple wildcard characters (? and into
 * Perl 5 compatible regexps used by Exim.
 *   file: Output file
 *      s: String to quote.
 ************************************************************************/

static void filter_expand_domain(FILE * file, char *s)
{
    filter_expand_wildcard(file, s);
}

/* ====================================================================== */
/* ====================================================================== */

/* Routines for generating parts of filter file */

/* filter_write_to() *****************************************************
 *
 * Write target mailbox
 *   config: Gloabl state
 *     file: Output file
 *  mailbox: Target mailbox
 *     copy: Store copy in inbox
 ************************************************************************/

static void
filter_write_to(struct config *config, FILE * file, char *mailbox,
                BOOL copy)
{
    if (copy)
        fprintf(file, "    unseen save \"");
    else
        fprintf(file, "    save \"");

    /* Ghastly restriction on Hermes for historical reasons. Yuck! */
    if (config->filter_restricted)
        fprintf(file, "mail/");

    filter_quote_double(file, mailbox);
    fprintf(file, "\"\n");
    fprintf(file, "    finish\n");
    fprintf(file, "endif\n");
}

/* filter_print_aliases() *************************************************
 *
 * Extract aliases list from vacation.aliases file and include in forward
 * file.
 *   config: Global state
 *     file: Output file
 ************************************************************************/

static BOOL filter_print_aliases(struct config *config, FILE * file)
{
    FILE *aliases;
    char buffer[MAXLENGTH];

    if ((aliases = fopen(config->aliases_name, "r")) == NIL)
        return (T);

    while (fgets(buffer, MAXLENGTH, aliases)) {
        unsigned long len = strlen(buffer);

        /* Chomp: buffer may or may not include a '\n' */
        if ((len > 0) && (buffer[len - 1] == '\n'))
            buffer[--len] = '\0';

        fprintf(file, "   alias %s\n", buffer);
    }
    fclose(aliases);

    return (T);
}

/* filter_print_vacation() ***********************************************
 *
 * Add vacation message to filter file
 *   config: Global state
 *   filter: Filter action structure (actually unused)
 *     file: Output file
 ************************************************************************/

static BOOL
filter_print_vacation(struct config *config, struct filter *filter,
                      FILE * file)
{
    struct passwd *pwd;

    if (!(pwd = getpwuid(getuid())))
        return (NIL);

    fprintf(file, "# MSshell :: vacation\n");
    fprintf(file, "if personal\n");

    if (!filter_print_aliases(config, file))
        return (NIL);

    fprintf(file, "then\n");
    fprintf(file, "   mail\n");
    fprintf(file, "   subject \"Auto reply Re: $h_subject\"\n");
    fprintf(file, "   text \"\\\n");
    fprintf(file, ("This message is automatically generated "
                   "in response to your mail\\n\\" "\n"));
    fprintf(file, ("message (perhaps re-directed) to "
                   "$local_part@$local_domain.\\n\\n\"\n"));
    fprintf(file, "    file ${home}/vacation.message\n");
    fprintf(file, "    log  ${home}/vacation.log\n");
    fprintf(file, "    once ${home}/vacation.once\n");
    fprintf(file, "endif\n");
    fprintf(file, "\n");

    return (T);
}

/* filter_print_spam() ***************************************************
 *
 * Add spam clause to filter file
 *   config: Global state
 *   filter: Filter action structure (actually unused)
 *     file: Output file
 ************************************************************************/

static BOOL
filter_print_spam(struct config *config, struct filter *filter, FILE *file)
{
    int threshold, i;

    if (!(filter->threshold && string_isnumber(filter->threshold)))
        return(NIL);

    threshold = atoi(filter->threshold);

    /* XXX (threshold == 0) okay? */
    fprintf(file, "if $h_X-Spam-Level contains \"");

    for (i=0 ; i < threshold; i++)
        fputc('*', file);

    fprintf(file, "\" then\n");
    fprintf(file, "    save mail/spam\n");
    fprintf(file, "    finish\n");
    fprintf(file, "endif\n");
    fprintf(file, "\n");

    return(T);
}

/* ====================================================================== */

/* filter_print_sender() *************************************************
 *
 * Print SENDER filter
 *  config: Global state
 *  filter: Filter action structure
 *    file: Output file
 ************************************************************************/

static BOOL
filter_print_sender(struct config *config, struct filter *filter,
                    FILE * file)
{
    if (!((filter->local_part && filter->domain)))
        return (NIL);

    fprintf(file, "# MSshell :: sender\n");
    if (filter_has_wildcard(filter->local_part) ||
        filter_has_wildcard(filter->domain)) {
        fprintf(file, "if $sender_address matches \"%s^", QMETA);
        filter_expand_local(file, filter->local_part);
        fprintf(file, "@");
        filter_expand_domain(file, filter->domain);
        fprintf(file, "%s$\" then\n", QMETA);
    } else {
        fprintf(file, "if $sender_address is \"%s@%s\" then\n",
                filter->local_part, filter->domain);
    }
    filter_write_to(config, file, filter->mailbox, filter->copy);
    fprintf(file, "\n");

    return (T);
}

/* ====================================================================== */

/* filter_print_recipient() **********************************************
 *
 * Print RECIPIENT filter
 *  config: Global state
 *  filter: Filter action structure
 *    file: Output file
 ************************************************************************/

static BOOL
filter_print_recipient(struct config *config,
                       struct filter *filter, FILE * file)
{
    if (!((filter->local_part && filter->domain)))
        return (NIL);

    fprintf(file, "# MSshell :: recip\n");
    if (filter_has_wildcard(filter->local_part) ||
        filter_has_wildcard(filter->domain)) {
        fprintf(file, "if foranyaddress $h_to:,$h_cc:\n");
        fprintf(file, "   ($thisaddress matches \"");
        filter_expand_local(file, filter->local_part);
        fprintf(file, "@");
        filter_expand_domain(file, filter->domain);
        fprintf(file, "\") then\n");
    } else {
        fprintf(file, "if foranyaddress $h_to:,$h_cc:\n");
        fprintf(file, "   ($thisaddress is \"%s@%s\") then\n",
                filter->local_part, filter->domain);
    }
    filter_write_to(config, file, filter->mailbox, filter->copy);
    fprintf(file, "\n");

    return (T);
}

/* ====================================================================== */

/* filter_print_block() **************************************************
 *
 * Print BLOCK filter
 *    config: Global state
 *    filter: Filter action structure
 *      file: Output file
 ************************************************************************/

static BOOL
filter_print_block(struct config *config, struct filter *filter,
                   FILE * file)
{
    if (!((filter->local_part && filter->domain)))
        return (NIL);

    fprintf(file, "# MSshell :: block\n");
    if (filter_has_wildcard(filter->local_part) ||
        filter_has_wildcard(filter->domain)) {
        fprintf(file, "if $sender_address matches \"%s^", QMETA);
        filter_expand_local(file, filter->local_part);
        fprintf(file, "@");
        filter_expand_domain(file, filter->domain);
        fprintf(file, "%s$\" then\n", QMETA);
    } else {
        fprintf(file, "if $sender_address is \"%s@%s\" then\n",
                filter->local_part, filter->domain);
    }
    fprintf(file, "    seen finish\n");
    fprintf(file, "endif\n");
    fprintf(file, "\n");

    return (T);
}

/* ====================================================================== */

/* filter_print_subject() ************************************************
 *
 * Print SUBJECT filter
 *   config: Global state
 *   filter: Filter action structure
 *     file: Output file
 ************************************************************************/

static BOOL
filter_print_subject(struct config *config, struct filter *filter,
                     FILE * file)
{
    if (!filter->subject)
        return (NIL);

    fprintf(file, "# MSshell :: subject\n");
    if (strchr(filter->subject, '?') || strchr(filter->subject, '*')) {
        fprintf(file, "if $h_subject: matches \"(?s)%s^", QMETA);
        filter_expand_wildcard(file, filter->subject);
        fprintf(file, "%s$\" then\n", QMETA);
    } else {
        fprintf(file, "if $h_subject: is \"");
        filter_quote_double(file, filter->subject);
        fprintf(file, "\" then\n");
    }

    filter_write_to(config, file, filter->mailbox, filter->copy);
    fprintf(file, "\n");
    return (T);
}

/* ====================================================================== */

/* filter_print_redirect() ***********************************************
 *
 * Print REDIRECT filter
 *   config: Global state
 *   filter: Filter action structure
 *     file: Output file
 ************************************************************************/

static BOOL
filter_print_redirect(struct config *config, struct filter *filter,
                      FILE * file)
{
    fprintf(file, "# MSshell :: redirect\n");
    if (filter->copy)
        fprintf(file, "unseen deliver %s", filter->address);
    else
        fprintf(file, "deliver %s", filter->address);
    fprintf(file, "\n");

    return (T);
}

/* ====================================================================== */

/* filter_clear() ********************************************************
 *
 * Clear out filter structure.
 *      filter: Filter action structure
 ************************************************************************/

static void filter_clear(struct filter *filter)
{
    filter->type = CO_UNKNOWN;
    filter->local_part = NIL;
    filter->domain = NIL;
    filter->subject = NIL;
    filter->mailbox = NIL;
    filter->address = NIL;
    filter->copy = NIL;
    filter->threshold = NIL;
}

/* filter_print() ********************************************************
 *
 * Print a single filter item, using the static support routines above.
 *   config: Global configuration
 *   filter: Filter action structure
 ************************************************************************/

static BOOL
filter_print(struct config *config, struct filter *filter, FILE * file)
{
    switch (filter->type) {
    case CO_UNKNOWN:
        return (NIL);
    case CO_VACATION:
        return (filter_print_vacation(config, filter, file));
    case CO_SPAM:
        return (filter_print_spam(config, filter, file));
    case CO_SENDER:
        return (filter_print_sender(config, filter, file));
    case CO_RECIPIENT:
        return (filter_print_recipient(config, filter, file));
    case CO_BLOCK:
        return (filter_print_block(config, filter, file));
    case CO_SUBJECT:
        return (filter_print_subject(config, filter, file));
    case CO_REDIRECT:
        return (filter_print_redirect(config, filter, file));
    }
    return (NIL);
}

/* ====================================================================== */

/* Some simple string tokenisation routines stolen from prayer string.c */

/* filter_get_token() ****************************************************
 *
 * Get a token from current line
 *     sp:  Ptr to string. Returns ptr to following token
 *
 * Returns: ptr to next token
 ************************************************************************/

static char *filter_get_token(char **sp)
{
    char *result;
    char *s = *sp;

    /* Isolate first token */
    while ((*s == ' ') || (*s == '\t'))
        s++;
    result = s;

    while (*s && (*s != ' ') && (*s != '\t'))
        s++;

    if (*s)
        *s++ = '\0';

    while ((*s == ' ') || (*s == '\t'))
        s++;

    *sp = s;
    return (result);
}

/* filter_next_token() ****************************************************
 *
 * Move to next token (skips whitespace)
 *     sp:  Ptr to string. Returns ptr to next token
 *
 * Returns: ptr to next token
 ************************************************************************/

static char *filter_next_token(char **sp)
{
    char *s = *sp;

    /* Isolate first token */
    while ((*s == ' ') || (*s == '\t'))
        s++;

    *sp = s;
    return (s);
}

/* filter_trim_whitespace() ***********************************************
 *
 * Trim leading and trailing whitespace from a string
 *    string: String to trim
 *
 * Returns: Ptr to trimmed string
 *************************************************************************/

char *filter_trim_whitespace(char *string)
{
    unsigned long len;

    /* Remove leading whitespace */
    while ((string[0] == ' ') ||
           (string[0] == '\t') ||
           (string[0] == '\015') || (string[0] == '\012'))
        string++;

    /* Remove traiing whitespace */
    len = strlen(string);
    while ((len > 0) &&
           ((string[len - 1] == ' ') ||
            (string[len - 1] == '\t') ||
            (string[len - 1] == '\015') || (string[len - 1] == '\012')))
        len--;

    /* Tie off the string */
    string[len] = '\0';

    return (string);
}


/* ====================================================================== */

/* filter_write() ********************************************************
 *
 * Write out an entire filter file
 *  config: Global configuration
 *    file: Output file
 *    text: Msforward contains to parse and translate into Exim .forward file
 *
 * Returns: T on sucess. NIL otherwise.
 ************************************************************************/

static BOOL filter_write(struct config *config, FILE * file, char *text)
{
    struct filter filter_space;
    struct filter *filter = &filter_space;
    char *s = text;
    char *t;

    fprintf(file, "# Exim Filter <-- don't change this line\n");
    fprintf(file,
            "# This file automatically generated. Do not edit!\n\n");;

    filter_clear(filter);
    for (; *s; s = t) {
        t = s;

        /* Get a line and tie it off */
        while (*t && (*t != '\n'))
            t++;

        if (*t)
            *t++ = '\0';

        /* Ignore comment lines */
        if (*s == '#')
            continue;

        /* End of block: process filter */
        if (*s == '\0') {
            if (!filter_print(config, filter, file))
                return (NIL);
            filter_clear(filter);
            continue;
        }

        if ((*s == ' ') || (*s == '\t')) {
            char *arg = filter_get_token(&s);
            char *value = filter_next_token(&s);

            if (!strcmp(arg, "local_part"))
                filter->local_part = value;
            else if (!strcmp(arg, "domain"))
                filter->domain = value;
            else if (!strcmp(arg, "subject"))
                filter->subject = value;
            else if (!strcmp(arg, "mailbox"))
                filter->mailbox = value;
            else if (!strcmp(arg, "address"))
                filter->address = value;
            else if (!strcmp(arg, "copy"))
                filter->copy = !strcmp(value, "true") ? T : NIL;
            else if (!strcmp(arg, "threshold"))
                filter->threshold = value;
            else
                return (NIL);
            continue;
        }

        if ((filter->type != CO_UNKNOWN)
            && !filter_print(config, filter, file))
            return (NIL);
        filter_clear(filter);

        s = filter_trim_whitespace(s);

        if (!strcmp(s, "vacation"))
            filter->type = CO_VACATION;
        else if (!strcmp(s, "spam"))
            filter->type = CO_SPAM;
        else if (!strcmp(s, "sender"))
            filter->type = CO_SENDER;
        else if (!strcmp(s, "recip"))
            filter->type = CO_RECIPIENT;
        else if (!strcmp(s, "recipient"))
            filter->type = CO_RECIPIENT;
        else if (!strcmp(s, "block"))
            filter->type = CO_BLOCK;
        else if (!strcmp(s, "subject"))
            filter->type = CO_SUBJECT;
        else if (!strcmp(s, "redirect"))
            filter->type = CO_REDIRECT;
    }
    if ((filter->type != CO_UNKNOWN)
        && !filter_print(config, filter, file))
        return (NIL);

    return (T);
}

/* ====================================================================== */

/* filter_write_forward() ************************************************
 *
 * Write out .forward file. This is external interface to filter module
 *     config: Global configuration
 *       text: MSforward text to parse and translate into Exim .forward
 *
 * Returns: T on success. NIL otherwise
 ************************************************************************/

BOOL filter_write_forward(struct config * config, char *text)
{
    FILE *file;
    BOOL rc;

    if ((file = fopen(config->forward_name, "w")) == NIL)
        return (NIL);

    rc = filter_write(config, file, (char *) text);

    if (fclose(file))           /* Probable quota error */
        rc = NIL;

    /* Better than leaving it in an inconsistent state */
    if (!rc)
        unlink(config->forward_name);

    if (!checksum_generate(config->forward_name))
        unlink(config->forward_name);

    return (rc);
}

/* ====================================================================== */

/* filter_empty() ********************************************************
 *
 * Check whether filter text is empty (approximation: contains alphanumeric
 * characters outside comment line).
 *    text: Filter file to test
 *
 * Returns: T => no actions defined by filter file.
 ************************************************************************/

BOOL filter_empty(char *s)
{
    char c;

    while ((c = *s++)) {
        if (c == '#') {
            /* Skip comment line */
            while ((c = *s) && (c != '\015') && (c != '\012'))
                s++;
            continue;
        }
        if (Uisalpha(c))
            return (NIL);
    }
    return (T);
}