| 1 | /* |
| 2 | * This code is copyright 2001 by Craig Hughes |
| 3 | * Conversion to a thread-safe shared library copyright 2002 Liam Widdowson |
| 4 | * Portions copyright 2002 by Brad Jorsch |
| 5 | * It is licensed under the same license as Perl itself. The text of this |
| 6 | * license is included in the SpamAssassin distribution in the file named |
| 7 | * "License". |
| 8 | */ |
| 9 | #ifndef LIBSPAMC_H |
| 10 | #define LIBSPAMC_H 1 |
| 11 | |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/socket.h> |
| 14 | #include <netdb.h> |
| 15 | #include <stdio.h> |
| 16 | |
| 17 | #define EX_NOTSPAM 0 |
| 18 | #define EX_ISSPAM 1 |
| 19 | #define EX_TOOBIG 866 |
| 20 | #define EX_OUTPUTMESSAGE 867 |
| 21 | |
| 22 | /* Aug 14, 2002 bj: Bitflags instead of lots of bool parameters */ |
| 23 | #define SPAMC_MODE_MASK 1 |
| 24 | #define SPAMC_RAW_MODE 0 |
| 25 | #define SPAMC_BSMTP_MODE 1 |
| 26 | |
| 27 | #define SPAMC_USE_SSL (1<<27) |
| 28 | #define SPAMC_SAFE_FALLBACK (1<<28) |
| 29 | #define SPAMC_CHECK_ONLY (1<<29) |
| 30 | |
| 31 | /* Jan 30, 2003 ym: added reporting options */ |
| 32 | #define SPAMC_REPORT (1<<26) |
| 33 | #define SPAMC_REPORT_IFSPAM (1<<25) |
| 34 | |
| 35 | /* Feb 1 2003 jm: might as well fix bug 191 as well */ |
| 36 | #define SPAMC_SYMBOLS (1<<24) |
| 37 | |
| 38 | |
| 39 | /* Aug 14, 2002 bj: A struct for storing a message-in-progress */ |
| 40 | typedef enum { |
| 41 | MESSAGE_NONE, |
| 42 | MESSAGE_ERROR, |
| 43 | MESSAGE_RAW, |
| 44 | MESSAGE_BSMTP, |
| 45 | MAX_MESSAGE_TYPE |
| 46 | } message_type_t; |
| 47 | |
| 48 | struct message { |
| 49 | /* Set before passing the struct on! */ |
| 50 | int max_len; /* messages larger than this will return EX_TOOBIG */ |
| 51 | int timeout; /* timeout for read() system calls */ |
| 52 | |
| 53 | /* Filled in by message_read */ |
| 54 | message_type_t type; |
| 55 | char *raw; int raw_len; /* Raw message buffer */ |
| 56 | char *pre; int pre_len; /* Pre-message data (e.g. SMTP commands) */ |
| 57 | char *msg; int msg_len; /* The message */ |
| 58 | char *post; int post_len; /* Post-message data (e.g. SMTP commands) */ |
| 59 | int content_length; |
| 60 | |
| 61 | /* Filled in by filter_message */ |
| 62 | int is_spam; /* EX_ISSPAM if the message is spam, EX_NOTSPAM |
| 63 | if not, EX_OUTPUTMESSAGE if a filtered message |
| 64 | is returned in "out" below. */ |
| 65 | float score, threshold; /* score and threshold */ |
| 66 | char *out; int out_len; /* Output from spamd. Either the filtered |
| 67 | message, or the check-only response. Or else, |
| 68 | a pointer to msg above. */ |
| 69 | }; |
| 70 | |
| 71 | /* Aug 14, 2002 bj: New interface functions */ |
| 72 | |
| 73 | /* Read in a message from the fd, with the mode specified in the flags. |
| 74 | * Returns EX_OK on success, EX_otherwise on failure. On failure, m may be |
| 75 | * either MESSAGE_NONE or MESSAGE_ERROR. */ |
| 76 | int message_read(int in_fd, int flags, struct message *m); |
| 77 | |
| 78 | /* Write out a message to the fd, as specified by m->type. Note that |
| 79 | * MESSAGE_NONE messages have nothing to write. Also note that if you ran the |
| 80 | * message through message_filter with SPAMC_CHECK_ONLY, it will only output |
| 81 | * the "score/threshold" line. */ |
| 82 | long message_write(int out_fd, struct message *m); |
| 83 | |
| 84 | /* Pass the message through spamd (at addr) as the specified user, with the |
| 85 | * given flags. Returns EX_OK on success, or various errors on error. If it was |
| 86 | * successful, message_write will print either the CHECK_ONLY output, or the |
| 87 | * filtered message in the appropriate output format. */ |
| 88 | int message_filter(const struct sockaddr *addr, char *username, int flags, struct message *m); |
| 89 | |
| 90 | /* Convert the host/port into a struct sockaddr. Returns EX_OK on success, or |
| 91 | * else an error EX. */ |
| 92 | int lookup_host(const char *hostname, int port, struct sockaddr *a); |
| 93 | |
| 94 | /* Pass the message through one of a set of spamd's. This variant will handle |
| 95 | * multiple spamd machines; if a connect failure occurs, it will fail-over to |
| 96 | * the next one in the struct hostent. Otherwise identical to message_filter(). |
| 97 | */ |
| 98 | int message_filter_with_failover (const struct hostent *hent, int port, char |
| 99 | *username, int flags, struct message *m); |
| 100 | |
| 101 | /* Convert the host into a struct hostent, for use with |
| 102 | * message_filter_with_failover() above. Returns EX_OK on success, or else an |
| 103 | * error EX. Note that the data filled into hent is from gethostbyname()'s |
| 104 | * static storage, so any call to gethostbyname() between |
| 105 | * lookup_host_for_failover() and message_filter_with_failover() will overwrite |
| 106 | * this. Take a copy, and use that instead, if you think a call may occur in |
| 107 | * your code, or library code that you use (such as syslog()). */ |
| 108 | int lookup_host_for_failover(const char *hostname, struct hostent *hent); |
| 109 | |
| 110 | /* Dump the message. If there is any data in the message (typically, m->type |
| 111 | * will be MESSAGE_ERROR) it will be message_writed. Then, fd_in will be piped |
| 112 | * to fd_out intol EOF. This is particularly useful if you get back an |
| 113 | * EX_TOOBIG. */ |
| 114 | void message_dump(int in_fd, int out_fd, struct message *m); |
| 115 | |
| 116 | /* Do a message_read->message_filter->message_write sequence, handling errors |
| 117 | * appropriately with dump_message or appropriate CHECK_ONLY output. Returns |
| 118 | * EX_OK or EX_ISSPAM/EX_NOTSPAM on success, some error EX on error. */ |
| 119 | int message_process(const char *hostname, int port, char *username, int max_size, int in_fd, int out_fd, const int flags); |
| 120 | |
| 121 | /* Cleanup the resources we allocated for storing the message. Call after |
| 122 | * you're done processing. */ |
| 123 | void message_cleanup(struct message *m); |
| 124 | |
| 125 | /* Aug 14, 2002 bj: This is now legacy, don't use it. */ |
| 126 | int process_message(const char *hostname, int port, char *username, |
| 127 | int max_size, int in_fd, int out_fd, |
| 128 | const int check_only, const int safe_fallback); |
| 129 | |
| 130 | #endif |
| 131 | |