1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Code for calling Brightmail AntiSpam.
6 Copyright (c) Tom Kistner <tom@duncanthrax.net> 2004
10 #ifdef EXPERIMENTAL_BRIGHTMAIL
14 uschar *bmi_current_optin = NULL;
16 uschar *bmi_process_message(header_line *header_list, int data_fd) {
17 BmiSystem *system = NULL;
18 BmiMessage *message = NULL;
20 BmiErrorLocation err_loc;
21 BmiErrorType err_type;
22 const BmiVerdict *verdict = NULL;
24 uschar data_buffer[4096];
25 uschar localhost[] = "127.0.0.1";
27 uschar *verdicts = NULL;
30 err = bmiInitSystem(BMI_VERSION, CS bmi_config_file, &system);
31 if (bmiErrorIsFatal(err) == BMI_TRUE) {
32 err_loc = bmiErrorGetLocation(err);
33 err_type = bmiErrorGetType(err);
34 log_write(0, LOG_PANIC,
35 "bmi error [loc %d type %d]: could not initialize Brightmail system.", (int)err_loc, (int)err_type);
39 err = bmiInitMessage(system, &message);
40 if (bmiErrorIsFatal(err) == BMI_TRUE) {
41 err_loc = bmiErrorGetLocation(err);
42 err_type = bmiErrorGetType(err);
43 log_write(0, LOG_PANIC,
44 "bmi error [loc %d type %d]: could not initialize Brightmail message.", (int)err_loc, (int)err_type);
45 bmiFreeSystem(system);
49 /* Send IP address of sending host */
50 if (sender_host_address == NULL)
51 host_address = localhost;
53 host_address = sender_host_address;
54 err = bmiProcessConnection(CS host_address, message);
55 if (bmiErrorIsFatal(err) == BMI_TRUE) {
56 err_loc = bmiErrorGetLocation(err);
57 err_type = bmiErrorGetType(err);
58 log_write(0, LOG_PANIC,
59 "bmi error [loc %d type %d]: bmiProcessConnection() failed (IP %s).", (int)err_loc, (int)err_type, CS host_address);
60 bmiFreeMessage(message);
61 bmiFreeSystem(system);
65 /* Send envelope sender address */
66 err = bmiProcessFROM(CS sender_address, message);
67 if (bmiErrorIsFatal(err) == BMI_TRUE) {
68 err_loc = bmiErrorGetLocation(err);
69 err_type = bmiErrorGetType(err);
70 log_write(0, LOG_PANIC,
71 "bmi error [loc %d type %d]: bmiProcessFROM() failed (address %s).", (int)err_loc, (int)err_type, CS sender_address);
72 bmiFreeMessage(message);
73 bmiFreeSystem(system);
77 /* Send envelope recipients */
78 for(i=0;i<recipients_count;i++) {
79 recipient_item *r = recipients_list + i;
80 BmiOptin *optin = NULL;
82 /* create optin object if optin string is given */
83 if ((r->bmi_optin != NULL) && (Ustrlen(r->bmi_optin) > 1)) {
84 debug_printf("passing bmiOptin string: %s\n", r->bmi_optin);
86 err = bmiOptinMset(optin, r->bmi_optin, ':');
87 if (bmiErrorIsFatal(err) == BMI_TRUE) {
88 log_write(0, LOG_PANIC|LOG_MAIN,
89 "bmi warning: [loc %d type %d]: bmiOptinMSet() failed (address '%s', string '%s').", (int)err_loc, (int)err_type, CS r->address, CS r->bmi_optin);
96 err = bmiAccumulateTO(CS r->address, optin, message);
101 if (bmiErrorIsFatal(err) == BMI_TRUE) {
102 err_loc = bmiErrorGetLocation(err);
103 err_type = bmiErrorGetType(err);
104 log_write(0, LOG_PANIC,
105 "bmi error [loc %d type %d]: bmiAccumulateTO() failed (address %s).", (int)err_loc, (int)err_type, CS r->address);
106 bmiFreeMessage(message);
107 bmiFreeSystem(system);
111 err = bmiEndTO(message);
112 if (bmiErrorIsFatal(err) == BMI_TRUE) {
113 err_loc = bmiErrorGetLocation(err);
114 err_type = bmiErrorGetType(err);
115 log_write(0, LOG_PANIC,
116 "bmi error [loc %d type %d]: bmiEndTO() failed.", (int)err_loc, (int)err_type);
117 bmiFreeMessage(message);
118 bmiFreeSystem(system);
122 /* Send message headers */
123 while (header_list != NULL) {
124 /* skip deleted headers */
125 if (header_list->type == '*') {
126 header_list = header_list->next;
129 err = bmiAccumulateHeaders(CCS header_list->text, header_list->slen, message);
130 if (bmiErrorIsFatal(err) == BMI_TRUE) {
131 err_loc = bmiErrorGetLocation(err);
132 err_type = bmiErrorGetType(err);
133 log_write(0, LOG_PANIC,
134 "bmi error [loc %d type %d]: bmiAccumulateHeaders() failed.", (int)err_loc, (int)err_type);
135 bmiFreeMessage(message);
136 bmiFreeSystem(system);
139 header_list = header_list->next;
141 err = bmiEndHeaders(message);
142 if (bmiErrorIsFatal(err) == BMI_TRUE) {
143 err_loc = bmiErrorGetLocation(err);
144 err_type = bmiErrorGetType(err);
145 log_write(0, LOG_PANIC,
146 "bmi error [loc %d type %d]: bmiEndHeaders() failed.", (int)err_loc, (int)err_type);
147 bmiFreeMessage(message);
148 bmiFreeSystem(system);
153 data_file = fdopen(data_fd,"r");
155 j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
157 err = bmiAccumulateBody(CCS data_buffer, j, message);
158 if (bmiErrorIsFatal(err) == BMI_TRUE) {
159 err_loc = bmiErrorGetLocation(err);
160 err_type = bmiErrorGetType(err);
161 log_write(0, LOG_PANIC,
162 "bmi error [loc %d type %d]: bmiAccumulateBody() failed.", (int)err_loc, (int)err_type);
163 bmiFreeMessage(message);
164 bmiFreeSystem(system);
169 err = bmiEndBody(message);
170 if (bmiErrorIsFatal(err) == BMI_TRUE) {
171 err_loc = bmiErrorGetLocation(err);
172 err_type = bmiErrorGetType(err);
173 log_write(0, LOG_PANIC,
174 "bmi error [loc %d type %d]: bmiEndBody() failed.", (int)err_loc, (int)err_type);
175 bmiFreeMessage(message);
176 bmiFreeSystem(system);
182 err = bmiEndMessage(message);
183 if (bmiErrorIsFatal(err) == BMI_TRUE) {
184 err_loc = bmiErrorGetLocation(err);
185 err_type = bmiErrorGetType(err);
186 log_write(0, LOG_PANIC,
187 "bmi error [loc %d type %d]: bmiEndMessage() failed.", (int)err_loc, (int)err_type);
188 bmiFreeMessage(message);
189 bmiFreeSystem(system);
193 /* get store for the verdict string */
194 verdicts = store_get(1);
197 for ( err = bmiAccessFirstVerdict(message, &verdict);
199 err = bmiAccessNextVerdict(message, verdict, &verdict) ) {
202 err = bmiCreateStrFromVerdict(verdict,&verdict_str);
203 if (!store_extend(verdicts, Ustrlen(verdicts)+1, Ustrlen(verdicts)+1+strlen(verdict_str)+1)) {
204 /* can't allocate more store */
207 if (*verdicts != '\0')
208 Ustrcat(verdicts, US ":");
209 Ustrcat(verdicts, US verdict_str);
210 bmiFreeStr(verdict_str);
213 DEBUG(D_receive) debug_printf("bmi verdicts: %s\n", verdicts);
215 if (Ustrlen(verdicts) == 0)
222 int bmi_get_delivery_status(uschar *base64_verdict) {
224 BmiErrorLocation err_loc;
225 BmiErrorType err_type;
226 BmiVerdict *verdict = NULL;
227 int rc = 1; /* deliver by default */
229 /* always deliver when there is no verdict */
230 if (base64_verdict == NULL)
233 /* create verdict from base64 string */
234 err = bmiCreateVerdictFromStr(CS base64_verdict, &verdict);
235 if (bmiErrorIsFatal(err) == BMI_TRUE) {
236 err_loc = bmiErrorGetLocation(err);
237 err_type = bmiErrorGetType(err);
238 log_write(0, LOG_PANIC,
239 "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
243 err = bmiVerdictError(verdict);
244 if (bmiErrorIsFatal(err) == BMI_TRUE) {
245 /* deliver normally due to error */
248 else if (bmiVerdictDestinationIsDefault(verdict) == BMI_TRUE) {
249 /* deliver normally */
252 else if (bmiVerdictAccessDestination(verdict) == NULL) {
257 /* deliver to alternate location */
261 bmiFreeVerdict(verdict);
266 uschar *bmi_get_alt_location(uschar *base64_verdict) {
268 BmiErrorLocation err_loc;
269 BmiErrorType err_type;
270 BmiVerdict *verdict = NULL;
273 /* always deliver when there is no verdict */
274 if (base64_verdict == NULL)
277 /* create verdict from base64 string */
278 err = bmiCreateVerdictFromStr(CS base64_verdict, &verdict);
279 if (bmiErrorIsFatal(err) == BMI_TRUE) {
280 err_loc = bmiErrorGetLocation(err);
281 err_type = bmiErrorGetType(err);
282 log_write(0, LOG_PANIC,
283 "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
287 err = bmiVerdictError(verdict);
288 if (bmiErrorIsFatal(err) == BMI_TRUE) {
289 /* deliver normally due to error */
292 else if (bmiVerdictDestinationIsDefault(verdict) == BMI_TRUE) {
293 /* deliver normally */
296 else if (bmiVerdictAccessDestination(verdict) == NULL) {
301 /* deliver to alternate location */
302 rc = store_get(strlen(bmiVerdictAccessDestination(verdict))+1);
303 Ustrcpy(rc, bmiVerdictAccessDestination(verdict));
304 rc[strlen(bmiVerdictAccessDestination(verdict))] = '\0';
307 bmiFreeVerdict(verdict);
311 uschar *bmi_get_base64_verdict(uschar *bmi_local_part, uschar *bmi_domain) {
313 BmiErrorLocation err_loc;
314 BmiErrorType err_type;
315 BmiVerdict *verdict = NULL;
316 const BmiRecipient *recipient = NULL;
317 const char *verdict_str = NULL;
319 uschar *verdict_buffer = NULL;
322 /* return nothing if there are no verdicts available */
323 if (bmi_verdicts == NULL)
326 /* allocate room for the b64 verdict string */
327 verdict_buffer = store_get(Ustrlen(bmi_verdicts)+1);
329 /* loop through verdicts */
330 verdict_ptr = bmi_verdicts;
331 while ((verdict_str = CCS string_nextinlist(&verdict_ptr, &sep,
333 Ustrlen(bmi_verdicts)+1)) != NULL) {
335 /* create verdict from base64 string */
336 err = bmiCreateVerdictFromStr(verdict_str, &verdict);
337 if (bmiErrorIsFatal(err) == BMI_TRUE) {
338 err_loc = bmiErrorGetLocation(err);
339 err_type = bmiErrorGetType(err);
340 log_write(0, LOG_PANIC,
341 "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, verdict_str);
345 /* loop through rcpts for this verdict */
346 for ( recipient = bmiVerdictAccessFirstRecipient(verdict);
348 recipient = bmiVerdictAccessNextRecipient(verdict, recipient)) {
349 uschar *rcpt_local_part;
352 /* compare address against our subject */
353 rcpt_local_part = US bmiRecipientAccessAddress(recipient);
354 rcpt_domain = Ustrchr(rcpt_local_part,'@');
355 if (rcpt_domain == NULL) {
363 if ( (strcmpic(rcpt_local_part, bmi_local_part) == 0) &&
364 (strcmpic(rcpt_domain, bmi_domain) == 0) ) {
366 bmiFreeVerdict(verdict);
367 return US verdict_str;
371 bmiFreeVerdict(verdict);
378 uschar *bmi_get_base64_tracker_verdict(uschar *base64_verdict) {
380 BmiErrorLocation err_loc;
381 BmiErrorType err_type;
382 BmiVerdict *verdict = NULL;
385 /* always deliver when there is no verdict */
386 if (base64_verdict == NULL)
389 /* create verdict from base64 string */
390 err = bmiCreateVerdictFromStr(CS base64_verdict, &verdict);
391 if (bmiErrorIsFatal(err) == BMI_TRUE) {
392 err_loc = bmiErrorGetLocation(err);
393 err_type = bmiErrorGetType(err);
394 log_write(0, LOG_PANIC,
395 "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
399 /* create old tracker string from verdict */
400 err = bmiCreateOldStrFromVerdict(verdict, &rc);
401 if (bmiErrorIsFatal(err) == BMI_TRUE) {
402 err_loc = bmiErrorGetLocation(err);
403 err_type = bmiErrorGetType(err);
404 log_write(0, LOG_PANIC,
405 "bmi error [loc %d type %d]: bmiCreateOldStrFromVerdict() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
409 bmiFreeVerdict(verdict);
414 int bmi_check_rule(uschar *base64_verdict, uschar *option_list) {
416 BmiErrorLocation err_loc;
417 BmiErrorType err_type;
418 BmiVerdict *verdict = NULL;
422 uschar rule_buffer[32];
426 /* no verdict -> no rule fired */
427 if (base64_verdict == NULL)
430 /* create verdict from base64 string */
431 err = bmiCreateVerdictFromStr(CS base64_verdict, &verdict);
432 if (bmiErrorIsFatal(err) == BMI_TRUE) {
433 err_loc = bmiErrorGetLocation(err);
434 err_type = bmiErrorGetType(err);
435 log_write(0, LOG_PANIC,
436 "bmi error [loc %d type %d]: bmiCreateVerdictFromStr() failed. [%s]", (int)err_loc, (int)err_type, base64_verdict);
440 err = bmiVerdictError(verdict);
441 if (bmiErrorIsFatal(err) == BMI_TRUE) {
442 /* error -> no rule fired */
443 bmiFreeVerdict(verdict);
447 /* loop through numbers */
448 rule_ptr = option_list;
449 while ((rule_num = string_nextinlist(&rule_ptr, &sep,
450 rule_buffer, 32)) != NULL) {
453 /* try to translate to int */
454 (void)sscanf(rule_num, "%d", &rule_int);
456 debug_printf("checking rule #%d\n", rule_int);
457 /* check if rule fired on the message */
458 if (bmiVerdictRuleFired(verdict, rule_int) == BMI_TRUE) {
459 debug_printf("rule #%d fired\n", rule_int);
466 bmiFreeVerdict(verdict);