ze-filter  (ze-filter-0.8.0-develop-180218)
mlfi_eoh.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : janvier 2002
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 /* ****************************************************************************
26  * *
27  * *
28  ******************************************************************************/
29 
30 sfsistat
32  SMFICTX *ctx;
33 {
34  CTXPRIV_T *priv = MLFIPRIV(ctx);
35  int result = SMFIS_CONTINUE;
36 
37  int nb_rcpt;
38  int rcpt_rate;
39  int ip_class;
40 
42 
43  if (priv == NULL)
44  return SMFIS_TEMPFAIL;
45 
47 
48  ip_class = priv->netclass.class;
49 #if 0
50  /*
51  * Message with passport ???
52  */
53  if (priv->nb_rcpt == 1 && priv->pass_ok)
54  goto fin;
55 #endif
56 
57  sm_macro_update(ctx, priv->sm);
58 
59  /*
60  * write the header to the spool file
61  */
62 #if defined(_FFR_CLEAN_MSG_BUF)
63  (void) spool_file_write(priv, "\n", strlen("\n"));
64 #else
65  (void) spool_file_write(priv, CRLF, strlen(CRLF));
66 #endif
67 
68  nb_rcpt = priv->env_nb_rcpt;
69 
70  priv->nb_rcpt += nb_rcpt;
71 
72  if (priv->peer_addr == NULL) {
73  ZE_LogMsgError(0, "peer_addr is NULL ???");
74  result = SMFIS_TEMPFAIL;
75 
76  goto fin;
77  }
78 #if !HAVE_XXFI_DATA
79  result = mlfi_data(ctx);
80  if (result != SMFIS_CONTINUE)
81  goto fin;
82 #endif /* HAVE_XXFI_DATA */
83 
84  if (result == SMFIS_CONTINUE
86  char *s = MSG_ENCODED_BODY;
87 
88  switch (priv->hdr_content_encoding) {
89  case MIME_ENCODE_7BIT:
90  break;
91  case MIME_ENCODE_8BIT:
92  break;
93  case MIME_ENCODE_BINARY:
95  ZE_LogMsgInfo(12, "HEADER ENCODE : BINARY %d",
96  priv->hdr_content_encoding);
97  break;
98  case MIME_ENCODE_BASE64:
100  ZE_LogMsgInfo(12, "HEADER ENCODE : B64 %d", priv->hdr_content_encoding);
101  break;
104  ZE_LogMsgInfo(12, "HEADER ENCODE : QP %d", priv->hdr_content_encoding);
105  break;
106  default:
107  ZE_LogMsgInfo(12, "HEADER ENCODE : OTHER %d",
108  priv->hdr_content_encoding);
109  break;
110  }
111  }
112 
113  if (result == SMFIS_CONTINUE) {
114  header_T *h;
115  char *s = "HEADERS PB";
116 
117  if ((cf_get_int(CF_NO_HEADERS) != OPT_OK) && (priv->headers == NULL)) {
118  s = MSG_NO_HEADERS;
119  result = SMFIS_REJECT;
120  }
121 
123  ((h = get_msgheader(priv->headers, "From")) == NULL)) {
124  s = MSG_NO_FROM_HEADER;
125  result = SMFIS_REJECT;
126  }
127 
128  if ((cf_get_int(CF_NO_TO_HEADERS) != OPT_OK) &&
129  ((h = get_msgheader(priv->headers, "To")) == NULL) &&
130  ((h = get_msgheader(priv->headers, "Cc")) == NULL)) {
131  s = MSG_NO_RCPT_HEADER;
132  result = SMFIS_REJECT;
133  }
134 
135  if (result != SMFIS_CONTINUE) {
136 
137  (void) jsmfi_setreply(ctx, "550", "5.7.1", s);
138 
139  log_msg_context(ctx, s);
140 
141  goto fin;
142  }
143  }
144 #if 1
145  /*
146  * check header contents
147  */
148  if (cf_get_int(CF_SPAM_REGEX) == OPT_YES) {
149  ZE_LogMsgDebug(15, "check_header_content");
150  if (shall_check_content(ctx)) {
151  int score = 0;
152  int where = MAIL_HEADERS;
153  header_T *h = NULL;
154 
155  for (h = priv->headers; h != NULL; h = h->next) {
156  if (h->value == NULL || strlen(h->value) == 0)
157  continue;
158 
159  where = MAIL_HEADERS;
160  if (STRCASEEQUAL(h->attr, "subject"))
161  where |= MAIL_SUBJECT;
162  if (STRCASEEQUAL(h->attr, "from"))
163  where |= MAIL_FROM;
164 
165  score += check_regex(CONNID_STR(priv->id), priv->peer_addr,
166  h->value, where);
167 
168  }
169  priv->rawScores.headers += score;
170  }
171  }
172 #endif
173 
174  /*
175  ** Date in a coherent time window
176  */
177  if (result == SMFIS_CONTINUE && priv->headers != NULL) {
178  int nerr_past, nerr_future;
179  header_T *h = priv->headers;
180  time_t now = time(NULL);
181 
182  nerr_past = nerr_future = 0;
183  while ((h = get_msgheader_next(h, "Date")) != NULL) {
184  time_t date_secs;
185 
186  if (h->value == NULL)
187  continue;
188 
189  date_secs = header_date2secs(h->value);
190  if (date_secs < 1000)
191  continue;
192 
193  if (date_secs > (now + 48 HOURS)) {
195  ZE_MessageInfo(10, "%s SPAM CHECK - DATE IN THE FUTUR : %s",
196  CONNID_STR(priv->id), h->value);
197  nerr_future++;
198  continue;
199  }
200 
201  if ((date_secs + 12 MONTHS) < now) {
203  ZE_MessageInfo(10, "%s SPAM CHECK - DATE IN THE PAST : %s",
204  CONNID_STR(priv->id), h->value);
205  nerr_past++;
206  continue;
207  }
208  }
209 
210  if (IS_UNKNOWN(priv->netclass.class)) {
211  char *msg = NULL;
212 
214  if (nerr_future > 0)
215  msg = "Date in the future ???";
216  }
217 
219  if (nerr_past > 0)
220  msg = "Date in remote past ???";
221  }
222 
223  if (msg != NULL) {
224  result = SMFIS_REJECT;
225  (void) jsmfi_setreply(ctx, "550", "5.7.1", msg);
226  log_msg_context(ctx, msg);
227  }
228  }
229 
230  }
231  if (result != SMFIS_CONTINUE)
232  goto fin;
233 
234  /*
235  * end...
236  */
237 fin:
239 
240  /*
241  * continue processing
242  */
243  return result;
244 }
int check_regex(char *, char *, char *, int)
Definition: ze-mailregex.c:295
#define MSG_ENCODED_BODY
Definition: ze-reply.h:175
#define MSG_NO_FROM_HEADER
Definition: ze-reply.h:61
msg_scores_T rawScores
#define SMFIS_TEMPFAIL
int jsmfi_setreply(SMFICTX *, char *, char *, char *)
Definition: ze-libmilter.c:99
header_T * get_msgheader(header_T *, char *)
Definition: ze-headers.c:144
#define CRLF
Definition: macros.h:224
#define MIME_ENCODE_7BIT
Definition: ze-demime.h:45
char * peer_addr
#define CF_REJECT_DATE_IN_PAST
Definition: cfh-defs.h:177
#define ZE_LogMsgInfo(level,...)
Definition: zeSyslog.h:110
int hdr_content_encoding
#define SMFIS_CONTINUE
#define SMFIS_REJECT
#define ZE_LogMsgError(level,...)
Definition: zeSyslog.h:113
#define OPT_YES
Definition: ze-cf.h:45
#define CF_LOG_LEVEL_ORACLE
Definition: cfh-defs.h:126
#define MSG_NO_RCPT_HEADER
Definition: ze-reply.h:65
bool shall_check_content(SMFICTX *)
int cf_get_int(int id)
Definition: ze-cf.c:803
#define MLFIPRIV(ctx)
#define INIT_CALLBACK(p, which)
Definition: ze-filter.c:125
#define MIME_ENCODE_8BIT
Definition: ze-demime.h:46
#define MSG_BODY_ENCODED_BASE64
Definition: ze-reply.h:183
#define OPT_OK
Definition: ze-cf.h:47
void sm_macro_update(SMFICTX *, sm_mac_T *)
Definition: ze-smmacros.c:150
CONNID_T id
#define INIT_CALLBACK_DELAY()
Definition: ze-filter.c:135
#define ZE_LogMsgDebug(level,...)
Definition: zeSyslog.h:109
char * value
Definition: ze-headers.h:36
#define IS_UNKNOWN(class)
Definition: ze-netclass.h:51
bool spool_file_write(CTXPRIV_T *, char *, size_t)
Definition: ze-spool.c:201
#define CF_NO_HEADERS
Definition: cfh-defs.h:184
#define CF_NO_FROM_HEADERS
Definition: cfh-defs.h:183
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define MSG_NO_HEADERS
Definition: ze-reply.h:73
#define OPT_NO
Definition: ze-cf.h:44
#define MSG_BODY_ENCODED_BINARY
Definition: ze-reply.h:179
#define MAIL_SUBJECT
Definition: ze-mailregex.h:38
time_t header_date2secs(char *date)
char * attr
Definition: ze-headers.h:35
header_T * headers
#define MONTHS
Definition: macros.h:147
header_T * next
Definition: ze-headers.h:37
sm_mac_T * sm
#define CF_REJECT_DATE_IN_FUTURE
Definition: cfh-defs.h:176
#define CONNID_STR(connid)
Definition: ze-filter.h:113
#define HOURS
Definition: macros.h:144
#define MSG_BODY_ENCODED_QP
Definition: ze-reply.h:187
#define CHECK_CALLBACK_DELAY()
Definition: ze-filter.c:146
#define STRCASEEQUAL(a, b)
Definition: macros.h:72
#define MAIL_HEADERS
Definition: ze-mailregex.h:39
#define MAIL_FROM
Definition: ze-mailregex.h:41
#define MIME_ENCODE_BINARY
Definition: ze-demime.h:47
sfsistat mlfi_eoh(SMFICTX *ctx)
Definition: mlfi_eoh.c:31
#define CF_SPAM_REGEX
Definition: cfh-defs.h:116
netclass_T netclass
#define CALLBACK_EOH
Definition: ze-callback.h:34
#define MIME_ENCODE_NONE
Definition: ze-demime.h:44
#define CF_NO_TO_HEADERS
Definition: cfh-defs.h:182
void log_msg_context(SMFICTX *ctx, char *why)
int msg[MAX_SCORE+2]
Definition: ze-stats.c:41
#define MIME_ENCODE_QUOTED_PRINTABLE
Definition: ze-demime.h:49
header_T * get_msgheader_next(header_T *, char *)
Definition: ze-headers.c:175
#define MIME_ENCODE_BASE64
Definition: ze-demime.h:48