-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
379 lines (311 loc) · 9.58 KB
/
main.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
// === Includes ===
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <netdb.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <libmilter/mfapi.h>
#include <syslog.h>
// === Global definitions ===
enum flags {
FLAG_EMPTY = 0x00,
FLAG_CHECK_MAILFROM = 0x01,
FLAG_CHECK_MAILFROM_PASS = 0x02,
};
typedef enum flags flags_t;
flags_t flags = FLAG_EMPTY;
enum status {
ST_NONE = 0x00,
ST_FROMMISMATCH = 0x01,
};
typedef enum status status_t;
struct private {
char *mailfrom;
status_t status;
};
typedef struct private private_t;
// === Code ===
extern sfsistat fromckmilter_cleanup(SMFICTX *, bool);
sfsistat fromckmilter_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) {
private_t *priv = calloc(sizeof(*priv), 1);
if(priv == NULL) {
syslog(LOG_CRIT, "fromckmilter_connect(): Cannot allocate memory. Exit.\n");
exit(EX_SOFTWARE);
}
smfi_setpriv(ctx, priv);
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_helo(SMFICTX *ctx, char *helohost) {
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_envfrom(SMFICTX *ctx, char **argv) {
if(flags & FLAG_CHECK_MAILFROM) {
private_t *priv = smfi_getpriv(ctx);
if(argv[0] == NULL) {
syslog(LOG_NOTICE, "fromckmilter_envfrom(): argv[0]==NULL. Sending TEMPFAIL.\n");
return SMFIS_TEMPFAIL;
}
if(*argv[0] == 0) {
syslog(LOG_NOTICE, "fromckmilter_envfrom(): *argv[0]==0. Sending TEMPFAIL.\n");
return SMFIS_TEMPFAIL;
}
priv->mailfrom = strdup(argv[0]);
}
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_envrcpt(SMFICTX *ctx, char **argv) {
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_header(SMFICTX *ctx, char *headerf, char *headerv) {
if(!strcasecmp(headerf, "From")) {
char *domainname_mailfrom=NULL, *domainname_from;
private_t *priv = smfi_getpriv(ctx);
// "MAIL FROM"
if(flags & FLAG_CHECK_MAILFROM) {
// Getting MAIL FROM value
char *mailfrom = priv->mailfrom;
if(mailfrom == NULL) {
syslog(LOG_NOTICE, "fromckmilter_header(): mailfrom==NULL. Sending TEMPFAIL.\n");
return SMFIS_TEMPFAIL;
}
if(*mailfrom == 0) {
syslog(LOG_NOTICE, "fromckmilter_header(): *mailfrom==0. Sending TEMPFAIL.\n");
return SMFIS_TEMPFAIL;
}
// Getting domain name from MAIL FROM value
char *at = strchr(mailfrom, '@');
if(!at) {
syslog(LOG_NOTICE, "%s: Invalid \"MAIL FROM\" value: no \"@\" in the string: \"%s\"\n",
smfi_getsymval(ctx, "i"), mailfrom);
return SMFIS_REJECT; // No "@" in "From:" value
}
domainname_mailfrom = &at[1];
if(!domainname_mailfrom[0]) { // Empty after "@" in "From:" value
syslog(LOG_NOTICE, "%s: Invalid \"MAIL FROM\" value: empty after the \"@\": \"%s\"\n",
smfi_getsymval(ctx, "i"), mailfrom);
return SMFIS_REJECT;
}
// Cutting the domain name of MAIL FROM value
char *strtok_saveptr = NULL;
char *domainname_cut = strtok_r(domainname_mailfrom, " \t)(<>@,;:\"/[]?=", &strtok_saveptr);
if(domainname_cut != NULL)
domainname_mailfrom = domainname_cut;
}
// "From"
{
// Checking the domain name: getting the domain name
char *at = strchr(headerv, '@');
if(!at) {
syslog(LOG_NOTICE, "%s: Invalid \"from\" value: no \"@\" in the string: \"%s\"\n",
smfi_getsymval(ctx, "i"), headerv);
return SMFIS_REJECT; // No "@" in "From:" value
}
domainname_from = &at[1];
if(!domainname_from[0]) { // Empty after "@" in "From:" value
syslog(LOG_NOTICE, "%s: Invalid \"from\" value: empty after the \"@\": \"%s\"\n",
smfi_getsymval(ctx, "i"), headerv);
return SMFIS_REJECT;
}
// Checking the domain name: Cutting the domain name
char *strtok_saveptr = NULL;
char *domainname_cut = strtok_r(domainname_from, " \t)(<>@,;:\"/[]?=", &strtok_saveptr);
if(domainname_cut != NULL)
domainname_from = domainname_cut;
// Checking the domain name
#ifdef METHOD_GETADDRINFO
// Deprecated method: Resolves A-record, but MX is required
struct addrinfo *res;
if(getaddrinfo(domainname_from, NULL, NULL, &res))
#else
// Good method.
unsigned char answer[BUFSIZ];
int answer_len = res_search(domainname_from, C_IN, T_MX, answer, BUFSIZ);
if(answer_len == -1) {
syslog(LOG_NOTICE, "%s: Unable to resolve MX-record of domain name \"%s\". Unusual for mail server.\n",
smfi_getsymval(ctx, "i"), domainname_from);
answer_len = res_search(domainname_from, C_IN, T_A, answer, BUFSIZ);
}
if(answer_len == -1)
answer_len = res_search(domainname_from, C_IN, T_AAAA, answer, BUFSIZ);
if(answer_len == -1)
#endif
{
syslog(LOG_NOTICE, "%s: Unable to resolve domain name \"%s\" from \"From\" value: \"%s\". Answering TEMPFAIL.\n",
smfi_getsymval(ctx, "i"), domainname_from, headerv);
return SMFIS_TEMPFAIL; // Non existant domain name in "From:" value
}
}
if(flags & FLAG_CHECK_MAILFROM) {
char *result;
size_t domainname_from_len, domainname_mailfrom_len;
domainname_from_len = strlen(domainname_from);
domainname_mailfrom_len = strlen(domainname_mailfrom);
if(domainname_from_len > domainname_mailfrom_len)
result = strcasestr(domainname_from, domainname_mailfrom);
else
result = strcasestr(domainname_mailfrom, domainname_from);
if(result == NULL) {
syslog(LOG_NOTICE, "%s: \"MAIL FROM\" !~ \"From\": \"%s\" !~ \"%s\".\n",
smfi_getsymval(ctx, "i"), domainname_mailfrom, domainname_from);
if(flags & FLAG_CHECK_MAILFROM_PASS)
priv->status |= ST_FROMMISMATCH;
else
return SMFIS_REJECT;
}
}
}
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_eoh(SMFICTX *ctx) {
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_body(SMFICTX *ctx, unsigned char *bodyp, size_t bodylen) {
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_eom(SMFICTX *ctx) {
smfi_addheader(ctx, "X-FromChk-Milter", "passed");
if(flags & FLAG_CHECK_MAILFROM_PASS) {
private_t *priv = smfi_getpriv(ctx);
if(priv->status & ST_FROMMISMATCH)
smfi_addheader(ctx, "X-FromChk-Milter-MailFrom", "mismatch");
else
smfi_addheader(ctx, "X-FromChk-Milter-MailFrom", "passed");
}
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_abort(SMFICTX *ctx) {
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_close(SMFICTX *ctx) {
private_t *priv = smfi_getpriv(ctx);
if(priv == NULL)
return SMFIS_CONTINUE;
if(priv->mailfrom != NULL)
free(priv->mailfrom);
free(priv);
smfi_setpriv(ctx, NULL);
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_unknown(SMFICTX *ctx, const char *cmd) {
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_data(SMFICTX *ctx) {
return SMFIS_CONTINUE;
}
sfsistat fromckmilter_negotiate(ctx, f0, f1, f2, f3, pf0, pf1, pf2, pf3)
SMFICTX *ctx;
unsigned long f0;
unsigned long f1;
unsigned long f2;
unsigned long f3;
unsigned long *pf0;
unsigned long *pf1;
unsigned long *pf2;
unsigned long *pf3;
{
return SMFIS_ALL_OPTS;
}
static void usage(const char *path) {
fprintf(stderr, "Usage: %s -p socket-addr [-t timeout] [-mM]\n",
path);
}
int main(int argc, char *argv[]) {
struct smfiDesc mailfilterdesc = {
"FromCheckMilter", // filter name
SMFI_VERSION, // version code -- do not change
SMFIF_ADDHDRS|SMFIF_ADDRCPT, // flags
fromckmilter_connect, // connection info filter
fromckmilter_helo, // SMTP HELO command filter
fromckmilter_envfrom, // envelope sender filter
fromckmilter_envrcpt, // envelope recipient filter
fromckmilter_header, // header filter
fromckmilter_eoh, // end of header
fromckmilter_body, // body block filter
fromckmilter_eom, // end of message
fromckmilter_abort, // message aborted
fromckmilter_close, // connection cleanup
fromckmilter_unknown, // unknown SMTP commands
fromckmilter_data, // DATA command
fromckmilter_negotiate // Once, at the start of each SMTP connection
};
if (res_init() != 0) {
fprintf(stderr, "Error while res_init()\n");
exit(EX_SOFTWARE);
}
char setconn = 0;
int c;
const char *args = "p:t:hMm";
extern char *optarg;
// Process command line options
while ((c = getopt(argc, argv, args)) != -1) {
switch (c) {
case 'p':
if (optarg == NULL || *optarg == '\0')
{
(void)fprintf(stderr, "Illegal conn: %s\n",
optarg);
exit(EX_USAGE);
}
if (smfi_setconn(optarg) == MI_FAILURE)
{
(void)fprintf(stderr,
"smfi_setconn failed\n");
exit(EX_SOFTWARE);
}
if (strncasecmp(optarg, "unix:", 5) == 0)
unlink(optarg + 5);
else if (strncasecmp(optarg, "local:", 6) == 0)
unlink(optarg + 6);
setconn = 1;
break;
case 't':
if (optarg == NULL || *optarg == '\0') {
(void)fprintf(stderr, "Illegal timeout: %s\n",
optarg);
exit(EX_USAGE);
}
if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
(void)fprintf(stderr,
"smfi_settimeout failed\n");
exit(EX_SOFTWARE);
}
break;
case 'm':
flags |= FLAG_CHECK_MAILFROM;
break;
case 'M':
flags |= FLAG_CHECK_MAILFROM_PASS;
break;
case 'h':
default:
usage(argv[0]);
exit(EX_USAGE);
}
}
if (!setconn) {
fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
usage(argv[0]);
exit(EX_USAGE);
}
if ((flags&(FLAG_CHECK_MAILFROM_PASS|FLAG_CHECK_MAILFROM)) == FLAG_CHECK_MAILFROM_PASS) {
fprintf(stderr, "%s: -M can be used only in conjunction with -m\n", argv[0]);
exit(EX_USAGE);
}
if (smfi_register(mailfilterdesc) == MI_FAILURE) {
fprintf(stderr, "smfi_register failed\n");
exit(EX_UNAVAILABLE);
}
openlog(NULL, LOG_PID, LOG_MAIL);
int ret = smfi_main();
closelog();
return ret;
}