"Fossies" - the Fresh Open Source Software Archive

Member "spammilt-0.5.0-rc7/spammilt.h" (17 Jun 2005, 9479 Bytes) of package /linux/privat/old/spammilt-0.5.0-rc7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "spammilt.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2 #-------------------------------------------------------------------------------
    3 #
    4 # $Id: spammilt.h,v 1.38 2005/06/17 13:39:31 mardan Exp $ 
    5 #
    6 # $Log: spammilt.h,v $
    7 # Revision 1.38  2005/06/17 13:39:31  mardan
    8 # more smtp rfc compliant (rejects and discards)
    9 #
   10 # Revision 1.37  2005/06/09 18:45:32  mardan
   11 # properfy -h text
   12 # set priv = NULL when receiving more than 1 email in same connection after
   13 #   reject or discard
   14 #
   15 # Revision 1.36  2005/06/08 22:15:18  mardan
   16 # Added min and max thresholds for dnsbl, blacklist, whitelist and SA.
   17 # Re-arranged options
   18 #
   19 # Revision 1.35  2005/05/24 19:17:06  mardan
   20 # Fixed a bug which caused the shared memory part to fail under hardened
   21 # kernels.
   22 # Added the option to specify a max dnsbl and/or blakclist hits
   23 #
   24 # Revision 1.34  2004/08/27 21:05:48  mardan
   25 # Updated documentation
   26 # Treat mail as spam based upon 'forged' hostname of the connecting host
   27 # Updated documentation
   28 #
   29 # Revision 1.33  2004/08/27 20:03:37  mardan
   30 # Added filtering of spam based on X-Spam-Score from the SpamAssassin package
   31 #
   32 # Revision 1.32  2004/06/12 21:25:10  mardan
   33 # Changed working dir to '/var/spool/spammilt'
   34 #
   35 # Revision 1.31  2003/11/28 21:40:51  mardan
   36 # Added ACCEPT entry in statistics
   37 # Changed scan for IP in header
   38 # Changed err_log to syslog
   39 # Lots of cleanups
   40 #
   41 # Revision 1.30  2003/11/24 22:13:43  mardan
   42 # Started to convert statistics output from file to socket. Socket is now
   43 # temporary on 2002. The shm part is initialised at start and cleaned up
   44 # at stop, but not yet updated when processing mail.
   45 #
   46 # Revision 1.29  2003/11/18 22:13:20  mardan
   47 # Added 'configure' scripts
   48 #
   49 # Revision 1.28  2003/11/18 21:55:29  mardan
   50 # Only adds one warning to header if multiple the same occur (dnsbl & blacklist)
   51 # Added version (-v) option, includes some extra compile information
   52 # Specifying option '-T' now adds a warning to header; applys to redirected and
   53 # normally send _spam_ email, default is changed to none
   54 # Removed '-Q' option, use the file instead
   55 # Simple filter to filterout local LAN IPs (speeds up dnsbl queries)
   56 # X-Spam-Flag is now initially changed if excists, otherwise one is added
   57 #
   58 # Revision 1.27  2003/11/17 23:06:57  mardan
   59 # Added multiple dnsbl hosts, needs testing and lots of cleanups
   60 #
   61 # Revision 1.26  2003/11/16 00:26:22  mardan
   62 # Added new files (dynarray.c and .h) for allocating dynamic arrays for
   63 # mlfi_envrcpts and mlfi_bl_spam_match (blacklist match)
   64 # Changed standard-alloc routines to the better controlled alloc routines
   65 # Started improving better logging of errors in case of program errors
   66 #
   67 # Revision 1.25  2003/11/12 22:13:11  mardan
   68 # Added fucntions to read files, allocate and free memory
   69 # Added array counters for list-arrays to properly read/free to the end.
   70 #
   71 # Revision 1.24  2003/11/09 16:10:34  mardan
   72 # Now properly removing spammilt.sock if process was brutely interrupted
   73 #
   74 # Revision 1.23  2003/11/09 02:20:05  sanne
   75 # Added: _initial_ support for multiple dnsbl servers:
   76 #   - dnsbl-hosts.list configuration file
   77 # Limitation: first entry in dnsbl-hosts.list file is used as dnsbl server, other entries are loaded but not used for now..
   78 #
   79 # Revision 1.22  2003/10/28 14:18:38  mardan
   80 # Some cleanups, Fixed crashbug due to a not initialized subject when changing
   81 # subject when spam was received
   82 #
   83 # Revision 1.21  2003/05/01 22:07:56  mardan
   84 # Version update -> 0.4.1
   85 #
   86 # Revision 1.20  2003/04/27 23:31:45  mardan
   87 # Edited document files
   88 # Changed version to 0.4
   89 #
   90 # Revision 1.19  2003/04/27 14:21:39  mardan
   91 # Added the possibility to compile with Annoyance-filter, which is a nice
   92 # Bayesian based scan-filter. (see http://www.fourmilab.ch/annoyance-filter/)
   93 # (make BAYESIAN_ANNOYANCE=1)
   94 #
   95 # Revision 1.18  2003/01/25 16:47:23  mardan
   96 # Some changes in memory allocation
   97 # Some changes in Makefile (not complet yet)
   98 # Other things I forgot.. ;-o
   99 # Pretty stablew now has been running for couple of months now
  100 #
  101 # Revision 1.17  2002/11/12 19:47:57  mardan
  102 # Added statistics counting and statistics dump through kill signal.
  103 #
  104 # Revision 1.16  2002/10/31 21:12:28  mardan
  105 # Improved stability by changing some of the memory declarations.
  106 # And did some other things ;-)
  107 #
  108 # Revision 1.15  2002/09/16 20:51:39  mardan
  109 # Added signal to reload spamlists.
  110 # Fixed small bug, which added X-header 'max unknown host reached' when spam detected by DNSBL or Blacklist, even if UNKNOWN_HOSTS wasn't reached.
  111 #
  112 # Revision 1.14  2002/09/15 00:07:34  mardan
  113 # Cleaned up some of the code.
  114 # Made some small changes in check_dnsbl to be thread safe.
  115 # Added the option to define DNSBL queryhost using a commandline parameter.
  116 #
  117 # Revision 1.13  2002/09/14 20:06:07  mardan
  118 # Value of last string in whitelist wasn't properly NULLed.
  119 #
  120 # Revision 1.12  2002/09/14 17:36:21  mardan
  121 # Merged patch file provided by Josh Corbin. Which inlcuded the whitelist
  122 # function, rewrite of the memory allocation for the black and white lists and
  123 # cleaned/ modified the Makefile.
  124 # Made some small changes to memory allocation, because of crash during shutdown.
  125 # Still need to cleanup the code.
  126 #
  127 # Revision 1.11  2002/09/14 16:03:05  mardan
  128 # Fixed a important typo. DNSBL server wasn't correctly spelled.
  129 # Minor changes to README and Makefile.
  130 #
  131 # Revision 1.10  2002/09/12 17:11:02  mardan
  132 # Some cleanups.
  133 # Added commanddline option to specify max unresolveble hosts found in header
  134 # and to the option reject them.
  135 # CV: ----------------------------------------------------------------------
  136 #
  137 # Revision 1.9  2002/09/01 16:08:25  mardan
  138 # Complete rewrite of spammilt due to stability problem.
  139 # Reduced number of .c files to spammilt.c and xfunctions.c
  140 # Diff between last spammilt and this version will be enormous when viewing
  141 # with cvsweb.
  142 # Added daemon mode.
  143 #
  144 #
  145 #
  146 # Copyright (c) 2002 by D.Wijsman (mardan@tweegy.demon.nl). 
  147 # All rights reserved.
  148 #
  149 # This program is free software; you can redistribute it and/or modify it
  150 # under the terms of the GNU General Public License as published by the Free
  151 # Software Foundation; either version 2 of the License, or (at your option)
  152 # any later version.
  153 # 
  154 # This program is distributed in the hope that it will be useful, but WITHOUT
  155 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  156 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  157 # more details.
  158 #
  159 # You should have received a copy of the GNU General Public License
  160 # along with this program; see the file COPYING.  If not, write to
  161 # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  162 #
  163 #
  164 #-------------------------------------------------------------------------------
  165 */
  166 
  167 #include <sys/stat.h>
  168 #include <sys/socket.h>
  169 #include <sys/types.h>
  170 #include <sys/wait.h>
  171 #include <arpa/inet.h>
  172 #include <stdio.h>
  173 #include <stdlib.h>
  174 #include <string.h>
  175 #include <sysexits.h>
  176 #include <pthread.h>
  177 #include <unistd.h>
  178 #include <libmilter/mfapi.h>
  179 #include <pwd.h>
  180 #include <sys/utsname.h>
  181 #include <signal.h>
  182 #include <time.h>
  183 #include <errno.h>
  184 #include <syslog.h>
  185 #include <sys/ipc.h>
  186 #include <sys/shm.h>
  187 #include "xfunctions.h"
  188 #include "dynarray.h"
  189 
  190 #ifndef true
  191 typedef int bool;
  192 # define false  0
  193 # define true   1
  194 #endif 
  195 
  196 // new
  197 #define ACTION_REJECT       1
  198 #define ACTION_DISCARD      2
  199 
  200 #define RESULT_BLACK    1
  201 #define RESULT_WHITE    2
  202 #define QUERYHOSTSFILE  "/etc/mail/spam/dnsbl-hosts.list"
  203 #define BLACKLISTFILE   "/etc/mail/spam/black.list"
  204 #define WHITELISTFILE   "/etc/mail/spam/white.list"
  205 #define DNSBLIGNORELISTFILE "/etc/mail/spam/dnsbl-ignore.list"
  206 #define STATISTICS_FILE "/var/spool/spammilt/spammilt.stats"
  207 #define PID_FILE    "/var/spool/spammilt/spammiltd.pid"
  208 // spam assassin max score
  209 #define ASSASSIN_MAX_SCORE 6
  210 // socket stuff
  211 #define PORT            5555    
  212 #define MAX_CHARS   1000
  213 #define LISTENQ     5
  214 // shm stuff
  215 #define SHM_KEY     666
  216 
  217 #ifdef BAYESIAN_ANNOYANCE
  218 #define BAYESIAN_ANNOYANCE_PATH "/usr/local/annoyance-filter"
  219 #endif
  220 
  221 /* spammilt.c */
  222 extern sfsistat mlfi_cleanup(SMFICTX *, bool);
  223 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *addr);
  224 sfsistat mlfi_envfrom(SMFICTX *ctx, char **envfrom);
  225 sfsistat mlfi_header(SMFICTX *ctx, char *headerf, char *headerv);
  226 sfsistat mlfi_eoh(SMFICTX *ctx);
  227 sfsistat mlfi_body(SMFICTX *ctx, u_char *bodyp, size_t bodylen);
  228 sfsistat mlfi_eom(SMFICTX *ctx);
  229 sfsistat mlfi_close(SMFICTX *ctx);
  230 sfsistat mlfi_abort(SMFICTX *ctx);
  231 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv);
  232 sfsistat mlfi_cleanup(SMFICTX *ctx, bool ok);
  233 void usage();
  234 void dump_settings();
  235 void signal_handler(int sig);
  236 void cleanup_lists();
  237 void init_lists(); 
  238 //int output_statistics();
  239 int check_pid();
  240 void write_pid();
  241 void check_socket(char *socket);
  242 char *scan_for_ip (char *string);
  243 
  244 // statistic socket
  245 int await_stat_request(void); 
  246 ssize_t write_stat_socket(int sockd, const void *vptr, size_t n);
  247 
  248 
  249 typedef struct 
  250     {
  251     int CONNECTS;   // use shm_write(1) to raise current value of CONNECTS with 1
  252     int PROCESSED;  // use shm_write(2) to raise current value of PROCESSED with 1
  253     int REJECTS;    // 3
  254     int DISCARDS;   // 4
  255     int REDIRECTS;  // 5
  256     int WHITELISTED;    // 6
  257     int BLACKLISTED;    // 7
  258     int FORGED;     // 8
  259     int DNSBL;      // 9
  260     int DNSBL_IGNORE;   // 10
  261     int BAYESIAN;   // 11
  262     int ACCEPT;     // 12
  263     int ASSASSIN;       // 13
  264     } shared_stats;
  265 
  266 // shm
  267 int get_shmid_create();
  268 int get_shmid();
  269 shared_stats *get_shmat(int shmid);
  270 int write_shm(int code);
  271 int cleanup_shm_stats();
  272 
  273 // definitions
  274 
  275 
  276 
  277 
  278 /* The End */
  279