]> diplodocus.org Git - mdeliver/blob - libspamc.h
be3d0fd90bbad18a415fe0fa0d1fdb97d45a4642
[mdeliver] / libspamc.h
1 /* <@LICENSE>
2 * Copyright 2004 Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * </@LICENSE>
16 */
17 #ifndef LIBSPAMC_H
18 #define LIBSPAMC_H 1
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <sys/types.h>
23 #ifdef _WIN32
24 #ifdef _MSC_VER
25 /* ignore MSVC++ warnings that are annoying and hard to remove:
26 4115 named type definition in parentheses
27 4127 conditional expression is constant
28 4514 unreferenced inline function removed
29 */
30 #pragma warning( disable : 4115 4127 4514 )
31 #endif
32 #include <winsock.h>
33 #else
34 #include <netdb.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #endif
38
39 #ifdef _WIN32
40 /* FIXME: This stuff has to go somewhere else */
41
42 #define EX_OK 0
43 #define EX_USAGE 64
44 #define EX_DATAERR 65
45 #define EX_NOINPUT 66
46 #define EX_NOUSER 67
47 #define EX_NOHOST 68
48 #define EX_UNAVAILABLE 69
49 #define EX_SOFTWARE 70
50 #define EX_OSERR 71
51 #define EX_OSFILE 72
52 #define EX_CANTCREAT 73
53 #define EX_IOERR 74
54 #define EX_TEMPFAIL 75
55 #define EX_PROTOCOL 76
56 #define EX_NOPERM 77
57 #define EX_CONFIG 78
58
59 #define STDIN_FILENO 0
60 #define STDOUT_FILENO 1
61
62 /* FIXME: This doesn't belong here either */
63 #define LOG_EMERG 0 /* system is unusable */
64 #define LOG_ALERT 1 /* action must be taken immediately */
65 #define LOG_CRIT 2 /* critical conditions */
66 #define LOG_ERR 3 /* error conditions */
67 #define LOG_WARNING 4 /* warning conditions */
68 #define LOG_NOTICE 5 /* normal but significant condition */
69 #define LOG_INFO 6 /* informational */
70 #define LOG_DEBUG 7 /* debug-level messages */
71
72 #endif
73
74 #define EX_NOTSPAM 0
75 #define EX_ISSPAM 1
76 #define EX_TOOBIG 866
77
78 /* Aug 14, 2002 bj: Bitflags instead of lots of bool parameters */
79 #define SPAMC_MODE_MASK 1
80 #define SPAMC_RAW_MODE 0
81 #define SPAMC_BSMTP_MODE 1
82
83 #define SPAMC_USE_SSL (1<<27)
84 #define SPAMC_SAFE_FALLBACK (1<<28)
85 #define SPAMC_CHECK_ONLY (1<<29)
86
87 /* Jan 30, 2003 ym: added reporting options */
88 #define SPAMC_REPORT (1<<26)
89 #define SPAMC_REPORT_IFSPAM (1<<25)
90
91 /* Feb 1 2003 jm: might as well fix bug 191 as well */
92 #define SPAMC_SYMBOLS (1<<24)
93
94 /* 2003/04/16 SJF: randomize hostname order (quasi load balancing) */
95 #define SPAMC_RANDOMIZE_HOSTS (1<<23)
96
97 /* log to stderr */
98 #define SPAMC_LOG_TO_STDERR (1<<22)
99
100 /* Nov 24, 2004 NP: added learning support */
101 #define SPAMC_LEARN (1<<21)
102
103 /* May 5, 2005 NP: added list reporting support */
104 #define SPAMC_REPORT_MSG (1<<20)
105
106
107 #define SPAMC_MESSAGE_CLASS_SPAM 1
108 #define SPAMC_MESSAGE_CLASS_HAM 2
109
110 #define SPAMC_SET_LOCAL 1
111 #define SPAMC_SET_REMOTE 2
112
113 #define SPAMC_REMOVE_LOCAL 4
114 #define SPAMC_REMOVE_REMOTE 8
115
116 /* Aug 14, 2002 bj: A struct for storing a message-in-progress */
117 typedef enum
118 {
119 MESSAGE_NONE,
120 MESSAGE_ERROR,
121 MESSAGE_RAW,
122 MESSAGE_BSMTP,
123 MAX_MESSAGE_TYPE
124 } message_type_t;
125
126 struct libspamc_private_message;
127
128 struct message
129 {
130 /* Set before passing the struct on! */
131 unsigned int max_len; /* messages larger than this will return EX_TOOBIG */
132 int timeout; /* timeout for read() system calls */
133
134 /* Filled in by message_read */
135 message_type_t type;
136 char *raw;
137 unsigned int raw_len; /* Raw message buffer */
138 char *pre;
139 int pre_len; /* Pre-message data (e.g. SMTP commands) */
140 char *msg;
141 unsigned int msg_len; /* The message */
142 char *post;
143 int post_len; /* Post-message data (e.g. SMTP commands) */
144 int content_length;
145
146 /* Filled in by filter_message */
147 int is_spam; /* EX_ISSPAM if the message is spam, EX_NOTSPAM
148 if not */
149 float score, threshold; /* score and threshold */
150 char *outbuf; /* Buffer for output from spamd */
151 char *out;
152 int out_len; /* Output from spamd. Either the filtered
153 message, or the check-only response. Or else,
154 a pointer to msg above. */
155
156 /* these members added in SpamAssassin version 2.60: */
157 struct libspamc_private_message *priv;
158 };
159
160 /*------------------------------------------------------------------------
161 * TRANSPORT (2004/04/16 - SJF)
162 *
163 * The code to connect with the daemon has gotten more complicated: support
164 * for SSL, fallback to multiple hosts, and using UNIX domain sockets. The
165 * code has gotten ugly with way too many parameters being passed all around.
166 *
167 * So we've created this object to hold all the info required to connect with
168 * the remote site, including a self-contained list of all the IP addresses
169 * in the event this is using TCP sockets. These multiple IPs can be obtained
170 * only from DNS returning more than one A record for a single name, and
171 * this allows for fallback.
172 *
173 * We also allow a kind of quasi-load balancing, where we take the list of
174 * A records from DNS and randomize them before starting out - this lets
175 * us spread the load out among multiple servers if desired. The idea for
176 * load balancing goes to Jeremy Zawodny.
177 *
178 * By putting all our data here, we remove "fallback" from being a special
179 * case. We may find ourselves with several IP addresses, but if the user
180 * disables fallback, we set the IP address count to one. Now the connect
181 * code just loops over that same address.
182 */
183 #define TRANSPORT_LOCALHOST 0x01 /* TCP to localhost only */
184 #define TRANSPORT_TCP 0x02 /* standard TCP socket */
185 #define TRANSPORT_UNIX 0x03 /* UNIX domain socket */
186
187 #define TRANSPORT_MAX_HOSTS 256 /* max hosts we can failover between */
188
189 struct transport
190 {
191 int type;
192
193 const char *socketpath; /* for UNIX dommain socket */
194 const char *hostname; /* for TCP sockets */
195
196 unsigned short port; /* for TCP sockets */
197
198 struct in_addr hosts[TRANSPORT_MAX_HOSTS];
199 int nhosts;
200 int flags;
201 };
202
203 extern void transport_init(struct transport *tp);
204 extern int transport_setup(struct transport *tp, int flags);
205
206 /* Aug 14, 2002 bj: New interface functions */
207
208 /* Read in a message from the fd, with the mode specified in the flags.
209 * Returns EX_OK on success, EX_otherwise on failure. On failure, m may be
210 * either MESSAGE_NONE or MESSAGE_ERROR. */
211 int message_read(int in_fd, int flags, struct message *m);
212
213 /* Write out a message to the fd, as specified by m->type. Note that
214 * MESSAGE_NONE messages have nothing to write. Also note that if you ran the
215 * message through message_filter with SPAMC_CHECK_ONLY, it will only output
216 * the "score/threshold" line. */
217 long message_write(int out_fd, struct message *m);
218
219 /* Process the message through the spamd filter, making as many connection
220 * attempts as are implied by the transport structure. To make this do
221 * failover, more than one host is defined, but if there is only one there,
222 * no failover is done.
223 */
224 int message_filter(struct transport *tp, const char *username,
225 int flags, struct message *m);
226
227 /* Process the message through the spamd tell command, making as many
228 * connection attempts as are implied by the transport structure. To make
229 * this do failover, more than one host is defined, but if there is only
230 * one there, no failover is done.
231 */
232 int message_tell(struct transport *tp, const char *username, int flags,
233 struct message *m, int msg_class,
234 unsigned int tellflags, unsigned int *didtellflags);
235
236 /* Dump the message. If there is any data in the message (typically, m->type
237 * will be MESSAGE_ERROR) it will be message_writed. Then, fd_in will be piped
238 * to fd_out intol EOF. This is particularly useful if you get back an
239 * EX_TOOBIG. */
240 void message_dump(int in_fd, int out_fd, struct message *m);
241
242 /* Do a message_read->message_filter->message_write sequence, handling errors
243 * appropriately with dump_message or appropriate CHECK_ONLY output. Returns
244 * EX_OK or EX_ISSPAM/EX_NOTSPAM on success, some error EX on error. */
245 int message_process(struct transport *trans, char *username, int max_size,
246 int in_fd, int out_fd, const int flags);
247
248 /* Cleanup the resources we allocated for storing the message. Call after
249 * you're done processing. */
250 void message_cleanup(struct message *m);
251
252 /* Aug 14, 2002 bj: This is now legacy, don't use it. */
253 int process_message(struct transport *tp, char *username,
254 int max_size, int in_fd, int out_fd,
255 const int check_only, const int safe_fallback);
256
257 #ifndef libspamc_log
258 void libspamc_log(int flags, int level, char *msg, ...);
259 #endif
260
261 #endif