2005-12-02 [paul] 1.9.100cvs55
[claws.git] / src / plugins / spamassassin / 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 /* Aug 14, 2002 bj: A struct for storing a message-in-progress */
101 typedef enum
102 {
103     MESSAGE_NONE,
104     MESSAGE_ERROR,
105     MESSAGE_RAW,
106     MESSAGE_BSMTP,
107     MAX_MESSAGE_TYPE
108 } message_type_t;
109
110 struct libspamc_private_message;
111
112 struct message
113 {
114     /* Set before passing the struct on! */
115     unsigned int max_len; /* messages larger than this will return EX_TOOBIG */
116     int timeout;                /* timeout for read() system calls */
117
118     /* Filled in by message_read */
119     message_type_t type;
120     char *raw;
121     unsigned int raw_len;               /* Raw message buffer */
122     char *pre;
123     int pre_len;                /* Pre-message data (e.g. SMTP commands) */
124     char *msg;
125     unsigned int msg_len;               /* The message */
126     char *post;
127     int post_len;               /* Post-message data (e.g. SMTP commands) */
128     int content_length;
129
130     /* Filled in by filter_message */
131     int is_spam;                /* EX_ISSPAM if the message is spam, EX_NOTSPAM
132                                    if not */
133     float score, threshold;     /* score and threshold */
134     char *out;
135     int out_len;                /* Output from spamd. Either the filtered
136                                    message, or the check-only response. Or else,
137                                    a pointer to msg above. */
138
139     /* these members added in SpamAssassin version 2.60: */
140     struct libspamc_private_message *priv;
141 };
142
143 /*------------------------------------------------------------------------
144  * TRANSPORT (2004/04/16 - SJF)
145  *
146  * The code to connect with the daemon has gotten more complicated: support
147  * for SSL, fallback to multiple hosts, and using UNIX domain sockets. The
148  * code has gotten ugly with way too many parameters being passed all around.
149  *
150  * So we've created this object to hold all the info required to connect with
151  * the remote site, including a self-contained list of all the IP addresses
152  * in the event this is using TCP sockets. These multiple IPs can be obtained
153  * only from DNS returning more than one A record for a single name, and
154  * this allows for fallback.
155  *
156  * We also allow a kind of quasi-load balancing, where we take the list of
157  * A records from DNS and randomize them before starting out - this lets
158  * us spread the load out among multiple servers if desired. The idea for
159  * load balancing goes to Jeremy Zawodny.
160  *
161  * By putting all our data here, we remove "fallback" from being a special
162  * case. We may find ourselves with several IP addresses, but if the user
163  * disables fallback, we set the IP address count to one. Now the connect
164  * code just loops over that same address.
165  */
166 #define TRANSPORT_LOCALHOST 0x01        /* TCP to localhost only */
167 #define TRANSPORT_TCP       0x02        /* standard TCP socket   */
168 #define TRANSPORT_UNIX      0x03        /* UNIX domain socket    */
169
170 #define TRANSPORT_MAX_HOSTS 256 /* max hosts we can failover between */
171
172 struct transport
173 {
174     int type;
175
176     const char *socketpath;     /* for UNIX dommain socket      */
177     const char *hostname;       /* for TCP sockets              */
178
179     unsigned short port;        /* for TCP sockets              */
180
181     struct in_addr hosts[TRANSPORT_MAX_HOSTS];
182     int nhosts;
183     int flags;
184 };
185
186 extern void transport_init(struct transport *tp);
187 extern int transport_setup(struct transport *tp, int flags);
188
189 /* Aug 14, 2002 bj: New interface functions */
190
191 /* Read in a message from the fd, with the mode specified in the flags.
192  * Returns EX_OK on success, EX_otherwise on failure. On failure, m may be
193  * either MESSAGE_NONE or MESSAGE_ERROR. */
194 int message_read(int in_fd, int flags, struct message *m);
195
196 /* Write out a message to the fd, as specified by m->type. Note that
197  * MESSAGE_NONE messages have nothing to write. Also note that if you ran the
198  * message through message_filter with SPAMC_CHECK_ONLY, it will only output
199  * the "score/threshold" line. */
200 long message_write(int out_fd, struct message *m);
201
202 /* Process the message through the spamd filter, making as many connection
203  * attempts as are implied by the transport structure. To make this do
204  * failover, more than one host is defined, but if there is only one there,
205  * no failover is done.
206  */
207 int message_filter(struct transport *tp, const char *username,
208                    int flags, struct message *m);
209
210 /* Dump the message. If there is any data in the message (typically, m->type
211  * will be MESSAGE_ERROR) it will be message_writed. Then, fd_in will be piped
212  * to fd_out intol EOF. This is particularly useful if you get back an
213  * EX_TOOBIG. */
214 void message_dump(int in_fd, int out_fd, struct message *m);
215
216 /* Do a message_read->message_filter->message_write sequence, handling errors
217  * appropriately with dump_message or appropriate CHECK_ONLY output. Returns
218  * EX_OK or EX_ISSPAM/EX_NOTSPAM on success, some error EX on error. */
219 int message_process(struct transport *trans, char *username, int max_size,
220                     int in_fd, int out_fd, const int flags);
221
222 /* Cleanup the resources we allocated for storing the message. Call after
223  * you're done processing. */
224 void message_cleanup(struct message *m);
225
226 /* Aug 14, 2002 bj: This is now legacy, don't use it. */
227 int process_message(struct transport *tp, char *username,
228                     int max_size, int in_fd, int out_fd,
229                     const int check_only, const int safe_fallback);
230
231 void libspamc_log(int flags, int level, char *msg, ...);
232
233 #endif