2 * Copyright 2004 Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
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
30 #pragma warning( disable : 4115 4127 4514 )
35 #include <sys/socket.h>
36 #include <netinet/in.h>
40 /* FIXME: This stuff has to go somewhere else */
48 #define EX_UNAVAILABLE 69
49 #define EX_SOFTWARE 70
52 #define EX_CANTCREAT 73
54 #define EX_TEMPFAIL 75
55 #define EX_PROTOCOL 76
59 #define STDIN_FILENO 0
60 #define STDOUT_FILENO 1
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 */
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
83 #define SPAMC_USE_SSL (1<<27)
84 #define SPAMC_SAFE_FALLBACK (1<<28)
85 #define SPAMC_CHECK_ONLY (1<<29)
87 /* Jan 30, 2003 ym: added reporting options */
88 #define SPAMC_REPORT (1<<26)
89 #define SPAMC_REPORT_IFSPAM (1<<25)
91 /* Feb 1 2003 jm: might as well fix bug 191 as well */
92 #define SPAMC_SYMBOLS (1<<24)
94 /* 2003/04/16 SJF: randomize hostname order (quasi load balancing) */
95 #define SPAMC_RANDOMIZE_HOSTS (1<<23)
98 #define SPAMC_LOG_TO_STDERR (1<<22)
100 /* Aug 14, 2002 bj: A struct for storing a message-in-progress */
110 struct libspamc_private_message;
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 */
118 /* Filled in by message_read */
121 unsigned int raw_len; /* Raw message buffer */
123 int pre_len; /* Pre-message data (e.g. SMTP commands) */
125 unsigned int msg_len; /* The message */
127 int post_len; /* Post-message data (e.g. SMTP commands) */
130 /* Filled in by filter_message */
131 int is_spam; /* EX_ISSPAM if the message is spam, EX_NOTSPAM
133 float score, threshold; /* score and threshold */
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. */
139 /* these members added in SpamAssassin version 2.60: */
140 struct libspamc_private_message *priv;
143 /*------------------------------------------------------------------------
144 * TRANSPORT (2004/04/16 - SJF)
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.
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.
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.
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.
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 */
170 #define TRANSPORT_MAX_HOSTS 256 /* max hosts we can failover between */
176 const char *socketpath; /* for UNIX dommain socket */
177 const char *hostname; /* for TCP sockets */
179 unsigned short port; /* for TCP sockets */
181 struct in_addr hosts[TRANSPORT_MAX_HOSTS];
186 extern void transport_init(struct transport *tp);
187 extern int transport_setup(struct transport *tp, int flags);
189 /* Aug 14, 2002 bj: New interface functions */
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);
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);
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.
207 int message_filter(struct transport *tp, const char *username,
208 int flags, struct message *m);
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
214 void message_dump(int in_fd, int out_fd, struct message *m);
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);
222 /* Cleanup the resources we allocated for storing the message. Call after
223 * you're done processing. */
224 void message_cleanup(struct message *m);
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);
231 void libspamc_log(int flags, int level, char *msg, ...);