1 |
/******** |
2 |
headermatch checks an rfc822 message on standard input for a matching |
3 |
header, and exits 0 if finds a match, and 1 otherwise. |
4 |
|
5 |
The header to match by default is "X-Spam-Flag: YES". |
6 |
If a command line argument is given, its text is used |
7 |
as the sorting header rather than "X-Spam-Flag: YES". |
8 |
|
9 |
Note that standard input must be able to be mmaped(*) for the testing to |
10 |
succeed - this will be the case when qmail-local is doing the delivery. |
11 |
If mmap fails then headermatch exits with status 100. |
12 |
|
13 |
(*) mmap was used so that there are no local buffers used in the program, so |
14 |
that the program logic is very simple. |
15 |
|
16 |
Copyright (C) 2006 Charlie Brady |
17 |
|
18 |
This program is free software; you can redistribute it and/or modify |
19 |
it under the terms of the GNU General Public License as published by |
20 |
the Free Software Foundation; either version 2 of the License, or |
21 |
(at your option) any later version. |
22 |
|
23 |
This program is distributed in the hope that it will be useful, |
24 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 |
GNU General Public License for more details. |
27 |
|
28 |
You should have received a copy of the GNU General Public License |
29 |
along with this program; if not, write to the Free Software |
30 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
31 |
|
32 |
***********/ |
33 |
|
34 |
#include <sys/types.h> |
35 |
#include <sys/stat.h> |
36 |
#include <sys/mman.h> |
37 |
#include <unistd.h> |
38 |
#include <errno.h> |
39 |
|
40 |
error(const char* s) |
41 |
{ |
42 |
write(2, s, strlen(s)); |
43 |
write(2, "\n", 1); |
44 |
} |
45 |
|
46 |
char * find_str(const char* s, const long len, const char* pattern) |
47 |
{ |
48 |
char* sptr; |
49 |
size_t plen; |
50 |
long slen; |
51 |
char p; |
52 |
if (*pattern == 0) return 0; |
53 |
slen = len; |
54 |
plen = strlen(pattern); |
55 |
for (sptr = (char *)s; (p = *pattern) != 0; ++sptr, --slen) |
56 |
{ |
57 |
if (*sptr == '\n') |
58 |
{ |
59 |
sptr++; |
60 |
slen--; |
61 |
if (*sptr == '\n') |
62 |
{ |
63 |
return 0; |
64 |
} |
65 |
if (slen < plen) |
66 |
{ |
67 |
return 0; |
68 |
} |
69 |
if (!memcmp(sptr, pattern, (size_t)plen)) |
70 |
{ |
71 |
return sptr; |
72 |
} |
73 |
} |
74 |
} |
75 |
return 0; |
76 |
} |
77 |
|
78 |
int main(int argc, char **argv) |
79 |
{ |
80 |
struct stat st; |
81 |
char *message; |
82 |
int fstat_ret; |
83 |
char *end_of_headers; |
84 |
char *spam_header = "X-Spam-Flag: YES"; |
85 |
size_t spam_header_len; |
86 |
size_t message_len; |
87 |
|
88 |
if (argc > 1) |
89 |
{ |
90 |
spam_header = argv[1]; |
91 |
} |
92 |
if (fstat_ret = fstat(0,&st)) |
93 |
{ |
94 |
error("Could not fstat standard input"); |
95 |
exit(111); |
96 |
} |
97 |
message_len = st.st_size; |
98 |
spam_header_len = strlen(spam_header); |
99 |
if (message_len < spam_header_len) |
100 |
{ |
101 |
exit(99); |
102 |
} |
103 |
if (message_len > 0xffffffff) |
104 |
{ |
105 |
error("Input file too big"); |
106 |
exit(100); |
107 |
} |
108 |
message = mmap(0,message_len,PROT_READ,MAP_SHARED,0,0); |
109 |
if (message + 1) |
110 |
{ |
111 |
if (!memcmp(message, spam_header, spam_header_len)) |
112 |
{ |
113 |
munmap(message, message_len); |
114 |
exit(0); |
115 |
} |
116 |
if (find_str(message, message_len, spam_header)) |
117 |
{ |
118 |
munmap(message, message_len); |
119 |
exit(0); |
120 |
} |
121 |
munmap(message, message_len); |
122 |
exit(99); |
123 |
} |
124 |
error("MMap failed"); |
125 |
exit(100); |
126 |
} |