ze-filter  (ze-filter-0.8.0-develop-180218)
mlfi_header.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 sfsistat
30 mlfi_header(ctx, headerf, headerv)
31  SMFICTX *ctx;
32  char *headerf;
33  char *headerv;
34 {
35  CTXPRIV_T *priv = MLFIPRIV(ctx);
36  int result = SMFIS_CONTINUE;
37 
38  char *hf = NULL;
39 
41 
42  if (priv == NULL) {
43  ZE_LogMsgWarning(0, "%s priv is NULL ", CONNID_STR(priv->id));
44  return SMFIS_TEMPFAIL;
45  }
46 
48 
49  sm_macro_update(ctx, priv->sm);
50 
51  if (headerf == NULL) {
52  ZE_MessageWarning(9, "%-12s : headerf NULL", CONNID_STR(priv->id));
53  result = SMFIS_TEMPFAIL;
54 
55  goto fin;
56  }
57  if (headerv == NULL) {
58  ZE_MessageWarning(9, "%-12s : headerv NULL", CONNID_STR(priv->id));
59  result = SMFIS_TEMPFAIL;
60 
61  goto fin;
62  }
63 
64  /*
65  ** spool file creation
66  */
67  if (!spool_file_is_open(priv)) {
68  /*
69  * open a file to store this message
70  */
71  if (!spool_file_create(priv)) {
72  ZE_LogMsgWarning(0, "%s : can't create spool file ",
73  CONNID_STR(priv->id));
74  result = SMFIS_TEMPFAIL;
75  }
76  }
77 
78  if (result != SMFIS_CONTINUE)
79  goto fin;
80 
81  /*
82  * write the header to the spool file
83  */
84  {
85  char buf[2048];
86 
87 #if defined(_FFR_CLEAN_MSG_BUF)
88  snprintf(buf, sizeof (buf), "%s: %s\n", headerf, headerv);
89 #else
90  snprintf(buf, sizeof (buf), "%s: %s%s", headerf, headerv, CRLF);
91 #endif
92  spool_file_write(priv, buf, strlen(buf));
93  }
94 
95  if (strlen(headerf) == 0) {
96  ZE_MessageInfo(9, "%-12s : headerf empty", CONNID_STR(priv->id));
97  }
98  if (result != SMFIS_CONTINUE)
99  goto fin;
100 
101  /*
102  ** Decode header, if RFC2047 encoded.
103  */
104 
105  /*
106  ** Add header to headers linked list
107  */
108  if ((hf = strdup(headerf)) == NULL) {
109  ZE_LogSysError("strdup(headerf) error");
110  result = SMFIS_TEMPFAIL;
111  }
112  if (result != SMFIS_CONTINUE)
113  goto fin;
114 
115  /*
116  * remove an eventual ':'
117  */
118  {
119  char *p = hf;
120 
121  if ((p = strchr(hf, ':')) != NULL)
122  *p = '\0';
123  }
124 
125  ZE_MessageInfo(12, "HEADER Key : %s", hf);
126  ZE_MessageInfo(12, "HEADER Value : %s", headerv);
127  (void) add_to_msgheader_list(&priv->headers, hf, headerv);
128 
129  if (strlen(headerv) == 0) {
130  ZE_MessageInfo(9, "%-12s : %s header empty", CONNID_STR(priv->id), headerf);
131 
132  goto fin;
133  }
134 #if _FFR_MODULES
135  /*
136  ** ze-filter modules
137  **
138  */
139  if (do_module_callback(ctx, 0, &result))
140  goto fin;
141  if (result != SMFIS_CONTINUE)
142  goto fin;
143 #endif /* _FFR_MODULES */
144 
145  if (zeStrRegex(headerv, "<script>", NULL, NULL, TRUE)) {
146  result = SMFIS_REJECT;
147 
149  (void) jsmfi_setreply(ctx, "550", "5.7.1", "HTML scripts inside header");
150  log_msg_context(ctx, "HTML scripts inside header");
151  }
152  if (result != SMFIS_CONTINUE)
153  goto fin;
154 
155  if (zeStrRegex(headerv, "<html>", NULL, NULL, TRUE)) {
156  result = SMFIS_REJECT;
157 
159  (void) jsmfi_setreply(ctx, "550", "5.7.1", MSG_HEADERS_CONTENTS);
160  log_msg_context(ctx, "HTML code inside header");
161  }
162  if (result != SMFIS_CONTINUE)
163  goto fin;
164 
165  if (STRCASEEQUAL(hf, "x-mailer") || STRCASEEQUAL(hf, "user-agent")) {
166  FREE(priv->hdr_mailer);
167  if ((priv->hdr_mailer = strdup(headerv)) == NULL) {
168  ZE_LogSysError("%-12s : strdup hdr_mailer", CONNID_STR(priv->id));
169  result = SMFIS_TEMPFAIL;
170  }
171  }
172  if (result != SMFIS_CONTINUE)
173  goto fin;
174 
175  if (STRCASEEQUAL(hf, "from")) {
176  if (priv->hdr_from != NULL)
177  ZE_MessageInfo(10, "%s : More than one From Header...",
178  CONNID_STR(priv->id));
179 
180  FREE(priv->hdr_from);
181  if ((priv->hdr_from = strdup(headerv)) == NULL) {
182  ZE_LogSysError("%-12s : strdup hdr_from", CONNID_STR(priv->id));
183  result = SMFIS_TEMPFAIL;
184  }
185  }
186  if (result != SMFIS_CONTINUE)
187  goto fin;
188 
189  if (STRCASEEQUAL(hf, "subject")) {
190  if (priv->hdr_subject != NULL)
191  ZE_MessageInfo(10, "%s : More than one Subject Header...",
192  CONNID_STR(priv->id));
193 
194  FREE(priv->hdr_subject);
195  if ((priv->hdr_subject = strdup(headerv)) == NULL) {
196  ZE_LogSysError("%-12s : strdup hdr_subject", CONNID_STR(priv->id));
197  result = SMFIS_TEMPFAIL;
198  }
199  }
200  if (result != SMFIS_CONTINUE)
201  goto fin;
202 
203 #if 1
204  if (STRCASEEQUAL(hf, "content-disposition") ||
205  STRCASEEQUAL(hf, "content-type")) {
206  char *buf = NULL;
207  size_t sz;
208 
209  sz = strlen(hf) + strlen(headerv) + 8;
210  if ((buf = (char *) malloc(sz + 1)) != NULL) {
211  memset(buf, 0, sz + 1);
212  snprintf(buf, sz, "%s: %s\n", hf, headerv);
213  priv->body_res_scan = scan_block(CONNID_STR(priv->id),
214  priv->body_chunk,
215  SZ_CHUNK,
216  buf,
217  strlen(buf),
218  &priv->body_scan_state,
219  &priv->tcontent, &priv->lcontent);
220  }
221  FREE(buf);
222  }
223  if (result != SMFIS_CONTINUE)
224  goto fin;
225 #endif
226 
227  if (STRCASEEQUAL(hf, "content-transfer-encoding"))
228  priv->hdr_content_encoding = mime_encode2val(headerv);
229 
230  if (result != SMFIS_CONTINUE)
231  goto fin;
232 
233  /*
234  * end...
235  */
236 fin:
237  FREE(hf);
239 
240  return result;
241 }
#define CALLBACK_HEADER
Definition: ze-callback.h:33
#define SZ_CHUNK
Definition: ze-filter.h:98
bool spool_file_create(CTXPRIV_T *)
Definition: ze-spool.c:50
void stats_inc(int, long)
Definition: ze-stats.c:401
#define SMFIS_TEMPFAIL
int scan_block(char *, char *, long, char *, long, int *, content_field_T *, content_field_T **)
Definition: ze-scanmail.c:51
int jsmfi_setreply(SMFICTX *, char *, char *, char *)
Definition: ze-libmilter.c:99
#define CRLF
Definition: macros.h:224
#define FREE(x)
Definition: macros.h:37
int hdr_content_encoding
char * hdr_mailer
#define SMFIS_CONTINUE
#define SMFIS_REJECT
bool zeStrRegex(char *, char *, long *, long *, bool)
Definition: zeStrings.c:544
#define MLFIPRIV(ctx)
#define INIT_CALLBACK(p, which)
Definition: ze-filter.c:125
#define strchr
Definition: ze-sys.h:218
void sm_macro_update(SMFICTX *, sm_mac_T *)
Definition: ze-smmacros.c:150
CONNID_T id
content_field_T tcontent
#define INIT_CALLBACK_DELAY()
Definition: ze-filter.c:135
bool spool_file_write(CTXPRIV_T *, char *, size_t)
Definition: ze-spool.c:201
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
bool add_to_msgheader_list(header_T **, char *, char *)
Definition: ze-headers.c:36
#define TRUE
Definition: macros.h:157
#define ZE_MessageWarning(level,...)
Definition: zeSyslog.h:92
sfsistat mlfi_header(SMFICTX *ctx, char *headerf, char *headerv)
Definition: mlfi_header.c:30
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
content_field_T * lcontent
char body_chunk[SZ_CHUNK]
header_T * headers
bool spool_file_is_open(CTXPRIV_T *)
Definition: ze-spool.c:291
#define MSG_HEADERS_CONTENTS
Definition: ze-reply.h:157
#define ZE_LogMsgWarning(level,...)
Definition: zeSyslog.h:112
bool do_module_callback(SMFICTX *ctx, int step, int *result)
Definition: ze-mod-tools.c:84
sm_mac_T * sm
int body_scan_state
#define CONNID_STR(connid)
Definition: ze-filter.h:113
char * hdr_subject
#define CHECK_CALLBACK_DELAY()
Definition: ze-filter.c:146
#define STRCASEEQUAL(a, b)
Definition: macros.h:72
char * hdr_from
void log_msg_context(SMFICTX *ctx, char *why)
#define STAT_HEADERS_CONTENTS
Definition: ze-stats.h:52