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
|
/* $OpenBSD: ruleset.c,v 1.33 2016/08/31 15:24:04 gilles Exp $ */
/*
* Copyright (c) 2009 Gilles Chehade <gilles@poolp.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <event.h>
#include <imsg.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "smtpd.h"
#include "log.h"
static int ruleset_check_source(struct table *,
const struct sockaddr_storage *, int);
static int ruleset_check_mailaddr(struct table *, const struct mailaddr *);
struct rule *
ruleset_match(const struct envelope *evp)
{
const struct mailaddr *maddr = &evp->dest;
const struct sockaddr_storage *ss = &evp->ss;
struct rule *r;
int ret;
TAILQ_FOREACH(r, env->sc_rules, r_entry) {
if (r->r_tag[0] != '\0') {
ret = strcmp(r->r_tag, evp->tag);
if (ret != 0 && !r->r_nottag)
continue;
if (ret == 0 && r->r_nottag)
continue;
}
if (r->r_wantauth && !(evp->flags & EF_AUTHENTICATED))
continue;
ret = ruleset_check_source(r->r_sources, ss, evp->flags);
if (ret == -1) {
errno = EAGAIN;
return (NULL);
}
if ((ret == 0 && !r->r_notsources) || (ret != 0 && r->r_notsources))
continue;
if (r->r_senders) {
ret = ruleset_check_mailaddr(r->r_senders, &evp->sender);
if (ret == -1) {
errno = EAGAIN;
return (NULL);
}
if ((ret == 0 && !r->r_notsenders) || (ret != 0 && r->r_notsenders))
continue;
}
if (r->r_recipients) {
ret = ruleset_check_mailaddr(r->r_recipients, &evp->dest);
if (ret == -1) {
errno = EAGAIN;
return (NULL);
}
if ((ret == 0 && !r->r_notrecipients) || (ret != 0 && r->r_notrecipients))
continue;
}
ret = r->r_destination == NULL ? 1 :
table_lookup(r->r_destination, NULL, maddr->domain, K_DOMAIN,
NULL);
if (ret == -1) {
errno = EAGAIN;
return NULL;
}
if ((ret == 0 && !r->r_notdestination) || (ret != 0 && r->r_notdestination))
continue;
goto matched;
}
errno = 0;
log_trace(TRACE_RULES, "no rule matched");
return (NULL);
matched:
log_trace(TRACE_RULES, "rule matched: %s", rule_to_text(r));
return r;
}
static int
ruleset_check_source(struct table *table, const struct sockaddr_storage *ss,
int evpflags)
{
const char *key;
if (evpflags & (EF_AUTHENTICATED | EF_INTERNAL))
key = "local";
else
key = ss_to_text(ss);
switch (table_lookup(table, NULL, key, K_NETADDR, NULL)) {
case 1:
return 1;
case -1:
log_warnx("warn: failure to perform a table lookup on table %s",
table->t_name);
return -1;
default:
break;
}
return 0;
}
static int
ruleset_check_mailaddr(struct table *table, const struct mailaddr *maddr)
{
const char *key;
key = mailaddr_to_text(maddr);
if (key == NULL)
return -1;
switch (table_lookup(table, NULL, key, K_MAILADDR, NULL)) {
case 1:
return 1;
case -1:
log_warnx("warn: failure to perform a table lookup on table %s",
table->t_name);
return -1;
default:
break;
}
return 0;
}
|