Debian Codegraph @

clamav-milter/clamfi.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2013-2024 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
3 * Copyright (C) 2008-2013 Sourcefire, Inc.
4 *
5 * Author: aCaB <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #if HAVE_CONFIG_H
23 #include "clamav-config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <errno.h>
34
35 #include <libmilter/mfapi.h>
36
37 // libclamav
38 #include "clamav.h"
39 #include "others.h"
40
41 // common
42 #include "optparser.h"
43 #include "output.h"
44
45 #include "connpool.h"
46 #include "netcode.h"
47 #include "allow_list.h"
48 #include "clamfi.h"
49
50 #if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
51 #define _UNUSED_ __attribute__((__unused__))
52 #else
53 #define _UNUSED_
54 #endif
55
56 uint64_t maxfilesize;
57
58 static sfsistat FailAction;
59 static sfsistat (*CleanAction)(SMFICTX *ctx);
60 static sfsistat (*InfectedAction)(SMFICTX *ctx);
61 static char *rejectfmt = NULL;
62
63 int addxvirus = 0; /* 0 - don't add | 1 - replace | 2 - add */
64 char xvirushdr[300];
65 char *viraction = NULL;
66 int multircpt = 1;
67
68 #define LOGINF_NONE 0
69 #define LOGINF_BASIC 1
70 #define LOGINF_FULL 2
71 #define LOGCLN_BASIC 4
72 #define LOGCLN_FULL 8
73 int loginfected;
74
75 #define CLAMFIBUFSZ 1424
76 static const char *HDR_UNAVAIL = "UNKNOWN";
77 static pthread_mutex_t virusaction_lock = PTHREAD_MUTEX_INITIALIZER;
78
79 struct CLAMFI {
80 const char *virusname;
81 char *msg_subj;
82 char *msg_date;
83 char *msg_id;
84 char **recipients;
85 int local;
86 int main;
87 int alt;
88 unsigned int totsz;
89 unsigned int bufsz;
90 unsigned int all_allowed;
91 unsigned int gotbody;
92 unsigned int scanned_count;
93 unsigned int status_count;
94 unsigned int nrecipients;
95 uint32_t sendme;
96 char buffer[CLAMFIBUFSZ];
97 };
98
99 static void add_x_header(SMFICTX *ctx, char *st, unsigned int scanned, unsigned int status)
100 {
101 if (addxvirus == 1) { /* Replace/Yes */
102 while (scanned)
103 if (smfi_chgheader(ctx, (char *)"X-Virus-Scanned", scanned--, NULL) != MI_SUCCESS)
104 logg(LOGG_WARNING, "Failed to remove existing X-Virus-Scanned header\n");
105 while (status)
106 if (smfi_chgheader(ctx, (char *)"X-Virus-Status", status--, NULL) != MI_SUCCESS)
107 logg(LOGG_WARNING, "Failed to remove existing X-Virus-Status header\n");
108 if (smfi_addheader(ctx, (char *)"X-Virus-Scanned", xvirushdr) != MI_SUCCESS)
109 logg(LOGG_WARNING, "Failed to add X-Virus-Scanned header\n");
110 if (smfi_addheader(ctx, (char *)"X-Virus-Status", st) != MI_SUCCESS)
111 logg(LOGG_WARNING, "Failed to add X-Virus-Status header\n");
112 } else { /* Add */
113 if (smfi_insheader(ctx, 1, (char *)"X-Virus-Scanned", xvirushdr) != MI_SUCCESS)
114 logg(LOGG_WARNING, "Failed to insert X-Virus-Scanned header\n");
115 if (smfi_insheader(ctx, 1, (char *)"X-Virus-Status", st) != MI_SUCCESS)
116 logg(LOGG_WARNING, "Failed to insert X-Virus-Status header\n");
117 }
118 }
119
120 enum CFWHAT {
121 CF_NONE, /* 0 */
122 CF_MAIN, /* 1 */
123 CF_ALT, /* 2 */
124 CF_BOTH, /* 3 */
125 CF_ANY /* 4 */
126 };
127
128 static const char *makesanehdr(char *hdr)
129 {
130 char *ret = hdr;
131 if (!hdr) return HDR_UNAVAIL;
132 while (*hdr) {
133 if (*hdr == '\'' || *hdr == '\t' || *hdr == '\r' || *hdr == '\n' || !isprint(*hdr))
134 *hdr = ' ';
135 hdr++;
136 }
137 return ret;
138 }
139
140 static void nullify(SMFICTX *ctx, struct CLAMFI *cf, enum CFWHAT closewhat)
141 {
142 if (closewhat & CF_MAIN || ((closewhat & CF_ANY) && cf->main >= 0))
143 close(cf->main);
144 if (closewhat & CF_ALT || ((closewhat & CF_ANY) && cf->alt >= 0))
145 close(cf->alt);
146 if (cf->msg_subj) free(cf->msg_subj);
147 if (cf->msg_date) free(cf->msg_date);
148 if (cf->msg_id) free(cf->msg_id);
149 if (multircpt && cf->nrecipients) {
150 while (cf->nrecipients) {
151 cf->nrecipients--;
152 free(cf->recipients[cf->nrecipients]);
153 }
154 free(cf->recipients);
155 }
156 smfi_setpriv(ctx, NULL);
157 }
158
159 static sfsistat sendchunk(struct CLAMFI *cf, unsigned char *bodyp, size_t len, SMFICTX *ctx)
160 {
161 if (cf->totsz >= maxfilesize || len == 0)
162 return SMFIS_CONTINUE;
163
164 if (!cf->totsz) {
165 sfsistat ret;
166 if (nc_connect_rand(&cf->main, &cf->alt, &cf->local)) {
167 logg(LOGG_ERROR, "Failed to initiate streaming/fdpassing\n");
168 nullify(ctx, cf, CF_NONE);
169 return FailAction;
170 }
171 cf->totsz = 1; /* do not infloop */
172 if ((ret = sendchunk(cf, (unsigned char *)"From clamav-milter\n", 19, ctx)) != SMFIS_CONTINUE)
173 return ret;
174 cf->totsz -= 1;
175 }
176
177 if (cf->totsz + len > maxfilesize)
178 len = maxfilesize - cf->totsz;
179
180 cf->totsz += len;
181 if (cf->local) {
182 while (len) {
183 int n = write(cf->alt, bodyp, len);
184
185 if (n == -1) {
186 logg(LOGG_ERROR, "Failed to write temporary file\n");
187 nullify(ctx, cf, CF_BOTH);
188 return FailAction;
189 }
190 len -= n;
191 bodyp += n;
192 }
193 } else {
194 int sendfailed = 0;
195
196 if (len < CLAMFIBUFSZ - cf->bufsz) {
197 memcpy(&cf->buffer[cf->bufsz], bodyp, len);
198 cf->bufsz += len;
199 } else if (len < CLAMFIBUFSZ) {
200 memcpy(&cf->buffer[cf->bufsz], bodyp, CLAMFIBUFSZ - cf->bufsz);
201 cf->sendme = htonl(CLAMFIBUFSZ);
202 sendfailed = nc_send(cf->main, &cf->sendme, CLAMFIBUFSZ + 4);
203 len -= (CLAMFIBUFSZ - cf->bufsz);
204 memcpy(cf->buffer, &bodyp[CLAMFIBUFSZ - cf->bufsz], len);
205 cf->bufsz = len;
206 } else {
207 uint32_t sendmetoo = htonl(len);
208 cf->sendme = htonl(cf->bufsz);
209 if ((cf->bufsz && nc_send(cf->main, &cf->sendme, cf->bufsz + 4)) || nc_send(cf->main, &sendmetoo, 4) || nc_send(cf->main, bodyp, len))
210 sendfailed = 1;
211 cf->bufsz = 0;
212 }
213 if (sendfailed) {
214 logg(LOGG_ERROR, "Streaming failed\n");
215 nullify(ctx, cf, CF_NONE);
216 return FailAction;
217 }
218 }
219 return SMFIS_CONTINUE;
220 }
221
222 sfsistat clamfi_header(SMFICTX *ctx, char *headerf, char *headerv)
223 {
224 struct CLAMFI *cf;
225 sfsistat ret;
226
227 if (!(cf = (struct CLAMFI *)smfi_getpriv(ctx)))
228 return SMFIS_CONTINUE; /* whatever */
229
230 if (!cf->totsz && cf->all_allowed) {
231 logg(LOGG_DEBUG, "Skipping scan (all destinations allowed)\n");
232 nullify(ctx, cf, CF_NONE);
233 free(cf);
234 return SMFIS_ACCEPT;
235 }
236
237 if (!headerf) return SMFIS_CONTINUE; /* just in case */
238
239 if ((loginfected & (LOGINF_FULL | LOGCLN_FULL)) || viraction) {
240 if (!cf->msg_subj && !strcasecmp(headerf, "Subject"))
241 cf->msg_subj = strdup(headerv ? headerv : "");
242 if (!cf->msg_date && !strcasecmp(headerf, "Date"))
243 cf->msg_date = strdup(headerv ? headerv : "");
244 if (!cf->msg_id && !strcasecmp(headerf, "Message-ID"))
245 cf->msg_id = strdup(headerv ? headerv : "");
246 }
247
248 if (addxvirus == 1) {
249 if (!strcasecmp(headerf, "X-Virus-Scanned")) cf->scanned_count++;
250 if (!strcasecmp(headerf, "X-Virus-Status")) cf->status_count++;
251 }
252
253 if ((ret = sendchunk(cf, (unsigned char *)headerf, strlen(headerf), ctx)) != SMFIS_CONTINUE) {
254 free(cf);
255 return ret;
256 }
257 if ((ret = sendchunk(cf, (unsigned char *)": ", 2, ctx)) != SMFIS_CONTINUE) {
258 free(cf);
259 return ret;
260 }
261 if (headerv && (ret = sendchunk(cf, (unsigned char *)headerv, strlen(headerv), ctx)) != SMFIS_CONTINUE) {
262 free(cf);
263 return ret;
264 }
265 ret = sendchunk(cf, (unsigned char *)"\r\n", 2, ctx);
266 if (ret != SMFIS_CONTINUE)
267 free(cf);
268 return ret;
269 }
270
271 sfsistat clamfi_body(SMFICTX *ctx, unsigned char *bodyp, size_t len)
272 {
273 struct CLAMFI *cf;
274 sfsistat ret;
275
276 if (!(cf = (struct CLAMFI *)smfi_getpriv(ctx)))
277 return SMFIS_CONTINUE; /* whatever */
278
279 if (!cf->gotbody) {
280 ret = sendchunk(cf, (unsigned char *)"\r\n", 2, ctx);
281 if (ret != SMFIS_CONTINUE) {
282 free(cf);
283 return ret;
284 }
285 cf->gotbody = 1;
286 }
287
288 ret = sendchunk(cf, bodyp, len, ctx);
289 if (ret != SMFIS_CONTINUE)
290 free(cf);
291 return ret;
292 }
293
294 sfsistat clamfi_abort(SMFICTX *ctx)
295 {
296 struct CLAMFI *cf;
297
298 if ((cf = (struct CLAMFI *)smfi_getpriv(ctx))) {
299 nullify(ctx, cf, CF_ANY);
300 free(cf);
301 }
302 return SMFIS_CONTINUE;
303 }
304
305 sfsistat clamfi_eom(SMFICTX *ctx)
306 {
307 struct CLAMFI *cf;
308 char *reply;
309 int len, ret;
310 unsigned int crcpt;
311
312 if (!(cf = (struct CLAMFI *)smfi_getpriv(ctx)))
313 return SMFIS_CONTINUE; /* whatever */
314
315 if (!cf->totsz) {
316 /* got no headers and no body */
317 logg(LOGG_DEBUG, "Not scanning an empty message\n");
318 ret = CleanAction(ctx);
319 nullify(ctx, cf, CF_NONE);
320 free(cf);
321 return ret;
322 }
323
324 if (cf->local) {
325 lseek(cf->alt, 0, SEEK_SET);
326
327 if (nc_sendmsg(cf->main, cf->alt) == -1) {
328 logg(LOGG_ERROR, "FD send failed\n");
329 nullify(ctx, cf, CF_ALT);
330 free(cf);
331 return FailAction;
332 }
333 } else {
334 uint32_t sendmetoo = 0;
335 cf->sendme = htonl(cf->bufsz);
336 if ((cf->bufsz && nc_send(cf->main, &cf->sendme, cf->bufsz + 4)) || nc_send(cf->main, &sendmetoo, 4)) {
337 logg(LOGG_ERROR, "Failed to flush STREAM\n");
338 nullify(ctx, cf, CF_NONE);
339 free(cf);
340 return FailAction;
341 }
342 }
343
344 reply = nc_recv(cf->main);
345
346 if (cf->local)
347 close(cf->alt);
348
349 cf->alt = -1;
350
351 if (!reply) {
352 logg(LOGG_ERROR, "No reply from clamd\n");
353 nullify(ctx, cf, CF_NONE);
354 free(cf);
355 return FailAction;
356 }
357
358 len = strlen(reply);
359 if (len > 5 && !strcmp(reply + len - 5, ": OK\n")) {
360 if (addxvirus) add_x_header(ctx, "Clean", cf->scanned_count, cf->status_count);
361 if (loginfected & LOGCLN_FULL) {
362 const char *id = smfi_getsymval(ctx, "{i}");
363 const char *from = smfi_getsymval(ctx, "{mail_addr}");
364 const char *msg_subj = makesanehdr(cf->msg_subj);
365 const char *msg_date = makesanehdr(cf->msg_date);
366 const char *msg_id = makesanehdr(cf->msg_id);
367 if (multircpt && cf->nrecipients) {
368 for (crcpt = 0; crcpt < cf->nrecipients; crcpt++)
369 logg(LOGG_INFO, "Clean message %s from <%s> to <%s> with subject '%s' message-id '%s' date '%s'\n", id, from, cf->recipients[crcpt], msg_subj, msg_id, msg_date);
370 } else {
371 const char *to = smfi_getsymval(ctx, "{rcpt_addr}");
372 logg(LOGG_INFO, "Clean message %s from <%s> to <%s> with subject '%s' message-id '%s' date '%s'\n", id, from, to ? to : HDR_UNAVAIL, msg_subj, msg_id, msg_date);
373 }
374 } else if (loginfected & LOGCLN_BASIC) {
375 const char *from = smfi_getsymval(ctx, "{mail_addr}");
376 if (multircpt && cf->nrecipients) {
377 for (crcpt = 0; crcpt < cf->nrecipients; crcpt++)
378 logg(LOGG_INFO, "Clean message from <%s> to <%s>\n", from, cf->recipients[crcpt]);
379 } else {
380 const char *to = smfi_getsymval(ctx, "{rcpt_addr}");
381 logg(LOGG_INFO, "Clean message from <%s> to <%s>\n", from, to ? to : HDR_UNAVAIL);
382 }
383 }
384 ret = CleanAction(ctx);
385 } else if (len > 7 && !strcmp(reply + len - 7, " FOUND\n")) {
386 cf->virusname = NULL;
387 if ((loginfected & (LOGINF_BASIC | LOGINF_FULL)) || addxvirus || rejectfmt || viraction) {
388 char *vir;
389
390 reply[len - 7] = '\0';
391 vir = strrchr(reply, ' ');
392 if (vir) {
393 unsigned int have_multi = (multircpt != 0 && cf->nrecipients);
394 unsigned int lst_rcpt = (have_multi * (cf->nrecipients - 1)) + 1;
395 vir++;
396
397 if (rejectfmt)
398 cf->virusname = vir;
399
400 if (addxvirus) {
401 char msg[255];
402 snprintf(msg, sizeof(msg), "Infected (%s)", vir);
403 msg[sizeof(msg) - 1] = '\0';
404 add_x_header(ctx, msg, cf->scanned_count, cf->status_count);
405 }
406
407 for (crcpt = 0; crcpt < lst_rcpt; crcpt++) {
408 if (loginfected || viraction) {
409 const char *from = smfi_getsymval(ctx, "{mail_addr}");
410 const char *to = have_multi ? cf->recipients[crcpt] : smfi_getsymval(ctx, "{rcpt_addr}");
411
412 if (!from) from = HDR_UNAVAIL;
413 if (!to) to = HDR_UNAVAIL;
414 if ((loginfected & LOGINF_FULL) || viraction) {
415 const char *id = smfi_getsymval(ctx, "{i}");
416 const char *msg_subj = makesanehdr(cf->msg_subj);
417 const char *msg_date = makesanehdr(cf->msg_date);
418 const char *msg_id = makesanehdr(cf->msg_id);
419
420 if (!id) id = HDR_UNAVAIL;
421
422 if (loginfected & LOGINF_FULL)
423 logg(LOGG_INFO, "Message %s from <%s> to <%s> with subject '%s' message-id '%s' date '%s' infected by %s\n", id, from, to, msg_subj, msg_id, msg_date, vir);
424
425 if (viraction) {
426 char er[256];
427 char *e_id = strdup(id);
428 char *e_from = strdup(from);
429 char *e_to = strdup(to);
430 char *e_msg_subj = strdup(msg_subj);
431 char *e_msg_date = strdup(msg_date);
432 char *e_msg_id = strdup(msg_id);
433 pid_t pid;
434
435 logg(LOGG_DEBUG, "VirusEvent: about to execute '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s'\n", viraction, vir, e_id, e_from, e_to, e_msg_subj, e_msg_id, e_msg_date);
436
437 pthread_mutex_lock(&virusaction_lock);
438 pid = fork();
439 if (!pid) {
440 char *args[9]; /* avoid element is not computable at load time warns */
441 args[0] = viraction;
442 args[1] = vir;
443 args[2] = e_id;
444 args[3] = e_from;
445 args[4] = e_to;
446 args[5] = e_msg_subj;
447 args[6] = e_msg_id;
448 args[7] = e_msg_date;
449 args[8] = NULL;
450 exit(execvp(viraction, args));
451 } else if (pid > 0) {
452 int wret;
453 pthread_mutex_unlock(&virusaction_lock);
454 while ((wret = waitpid(pid, &ret, 0)) == -1 && errno == EINTR) continue;
455 if (wret < 0)
456 logg(LOGG_ERROR, "VirusEvent: waitpid() failed: %s\n", cli_strerror(errno, er, sizeof(er)));
457 else {
458 if (WIFEXITED(ret))
459 logg(LOGG_DEBUG, "VirusEvent: child exited with code %d\n", WEXITSTATUS(ret));
460 else if (WIFSIGNALED(ret))
461 logg(LOGG_DEBUG, "VirusEvent: child killed by signal %d\n", WTERMSIG(ret));
462 else
463 logg(LOGG_DEBUG, "VirusEvent: child lost\n");
464 }
465 } else {
466 logg(LOGG_ERROR, "VirusEvent: fork failed: %s\n", cli_strerror(errno, er, sizeof(er)));
467 }
468 free(e_id);
469 free(e_from);
470 free(e_to);
471 free(e_msg_subj);
472 free(e_msg_date);
473 free(e_msg_id);
474 }
475 }
476 if (loginfected & LOGINF_BASIC)
477 logg(LOGG_INFO, "Message from <%s> to <%s> infected by %s\n", from, to, vir);
478 }
479 }
480 }
481 }
482 ret = InfectedAction(ctx);
483 } else {
484 logg(LOGG_ERROR, "Unknown reply from clamd\n");
485 ret = FailAction;
486 }
487
488 nullify(ctx, cf, CF_MAIN);
489 free(cf);
490 free(reply);
491 return ret;
492 }
493
494 sfsistat clamfi_connect(_UNUSED_ SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
495 {
496 while (1) {
497 /* Postfix doesn't seem to honor passing a NULL hostaddr and hostname
498 set to "localhost" for non-smtp messages (they still appear as SMTP
499 messages from 127.0.0.1). Here's a small workaround. */
500 if (hostaddr) {
501 if (islocalnet_sock(hostaddr)) {
502 logg(LOGG_DEBUG, "Skipping scan for %s (in LocalNet)\n", hostname);
503 return SMFIS_ACCEPT;
504 }
505 break;
506 }
507 if (!strcasecmp(hostname, "localhost"))
508 hostname = NULL;
509 if (islocalnet_name(hostname)) {
510 logg(LOGG_DEBUG, "Skipping scan for %s (in LocalNet)\n", hostname ? hostname : "local");
511 return SMFIS_ACCEPT;
512 }
513 break;
514 }
515 return SMFIS_CONTINUE;
516 }
517
518 static int parse_action(char *action)
519 {
520 if (!strcasecmp(action, "Accept"))
521 return 0;
522 if (!strcasecmp(action, "Defer"))
523 return 1;
524 if (!strcasecmp(action, "Reject"))
525 return 2;
526 if (!strcasecmp(action, "Blackhole"))
527 return 3;
528 if (!strcasecmp(action, "Quarantine"))
529 return 4;
530 logg(LOGG_ERROR, "Unknown action %s\n", action);
531 return -1;
532 }
533
534 static sfsistat action_accept(_UNUSED_ SMFICTX *ctx)
535 {
536 return SMFIS_ACCEPT;
537 }
538 static sfsistat action_defer(_UNUSED_ SMFICTX *ctx)
539 {
540 return SMFIS_TEMPFAIL;
541 }
542 static sfsistat action_reject(_UNUSED_ SMFICTX *ctx)
543 {
544 return SMFIS_REJECT;
545 }
546 static sfsistat action_blackhole(_UNUSED_ SMFICTX *ctx)
547 {
548 return SMFIS_DISCARD;
549 }
550 static sfsistat action_quarantine(SMFICTX *ctx)
551 {
552 if (smfi_quarantine(ctx, "quarantined by clamav-milter") != MI_SUCCESS) {
553 logg(LOGG_WARNING, "Failed to quarantine message\n");
554 return SMFIS_TEMPFAIL;
555 }
556 return SMFIS_ACCEPT;
557 }
558
559 static sfsistat action_reject_msg(SMFICTX *ctx)
560 {
561 struct CLAMFI *cf;
562 char buf[1024];
563
564 if (!rejectfmt || !(cf = (struct CLAMFI *)smfi_getpriv(ctx)))
565 return SMFIS_REJECT;
566
567 snprintf(buf, sizeof(buf), rejectfmt, cf->virusname);
568 buf[sizeof(buf) - 1] = '\0';
569 smfi_setreply(ctx, "550", "5.7.1", buf);
570 return SMFIS_REJECT;
571 }
572
573 int init_actions(struct optstruct *opts)
574 {
575 const struct optstruct *opt;
576
577 if (!(opt = optget(opts, "LogInfected"))->enabled || !strcasecmp(opt->strarg, "Off"))
578 loginfected = LOGINF_NONE;
579 else if (!strcasecmp(opt->strarg, "Basic"))
580 loginfected = LOGINF_BASIC;
581 else if (!strcasecmp(opt->strarg, "Full"))
582 loginfected = LOGINF_FULL;
583 else {
584 logg(LOGG_ERROR, "Invalid setting %s for option LogInfected\n", opt->strarg);
585 return 1;
586 }
587
588 if ((opt = optget(opts, "LogClean"))->enabled) {
589 if (!strcasecmp(opt->strarg, "Basic"))
590 loginfected |= LOGCLN_BASIC;
591 else if (!strcasecmp(opt->strarg, "Full"))
592 loginfected |= LOGCLN_FULL;
593 else if (strcasecmp(opt->strarg, "Off")) {
594 logg(LOGG_ERROR, "Invalid setting %s for option LogClean\n", opt->strarg);
595 return 1;
596 }
597 }
598
599 if ((opt = optget(opts, "VirusAction"))->enabled)
600 viraction = strdup(opt->strarg);
601
602 if ((opt = optget(opts, "OnFail"))->enabled) {
603 switch (parse_action(opt->strarg)) {
604 case 0:
605 FailAction = SMFIS_ACCEPT;
606 break;
607 case 1:
608 FailAction = SMFIS_TEMPFAIL;
609 break;
610 case 2:
611 FailAction = SMFIS_REJECT;
612 break;
613 default:
614 logg(LOGG_ERROR, "Invalid action %s for option OnFail\n", opt->strarg);
615 return 1;
616 }
617 } else
618 FailAction = SMFIS_TEMPFAIL;
619
620 if ((opt = optget(opts, "OnClean"))->enabled) {
621 switch (parse_action(opt->strarg)) {
622 case 0:
623 CleanAction = action_accept;
624 break;
625 case 1:
626 CleanAction = action_defer;
627 break;
628 case 2:
629 CleanAction = action_reject;
630 break;
631 case 3:
632 CleanAction = action_blackhole;
633 break;
634 case 4:
635 CleanAction = action_quarantine;
636 break;
637 default:
638 logg(LOGG_ERROR, "Invalid action %s for option OnClean\n", opt->strarg);
639 return 1;
640 }
641 } else
642 CleanAction = action_accept;
643
644 if ((opt = optget(opts, "OnInfected"))->enabled) {
645 switch (parse_action(opt->strarg)) {
646 case 0:
647 InfectedAction = action_accept;
648 break;
649 case 1:
650 InfectedAction = action_defer;
651 break;
652 case 3:
653 InfectedAction = action_blackhole;
654 break;
655 case 4:
656 InfectedAction = action_quarantine;
657 break;
658 case 2:
659 InfectedAction = action_reject_msg;
660 if ((opt = optget(opts, "RejectMsg"))->enabled) {
661 const char *src = opt->strarg;
662 char *dst, c;
663 int gotpctv = 0;
664
665 rejectfmt = dst = malloc(strlen(src) * 4 + 1);
666 if (!dst) {
667 logg(LOGG_ERROR, "Failed to allocate memory for RejectMsg\n");
668 return 1;
669 }
670 while ((c = *src++)) {
671 if (!isprint(c)) {
672 logg(LOGG_ERROR, "RejectMsg contains non printable characters\n");
673 free(rejectfmt);
674 return 1;
675 }
676 *dst++ = c;
677 if (c == '%') {
678 if (*src == 'v') {
679 if (gotpctv) {
680 logg(LOGG_ERROR, "%%v may appear at most once in RejectMsg\n");
681 free(rejectfmt);
682 return 1;
683 }
684 gotpctv |= 1;
685 src++;
686 *dst++ = 's';
687 } else {
688 dst[0] = dst[1] = dst[2] = '%';
689 dst += 3;
690 }
691 }
692 }
693 *dst = '\0';
694 }
695 break;
696 default:
697 logg(LOGG_ERROR, "Invalid action %s for option OnInfected\n", opt->strarg);
698 return 1;
699 }
700 } else
701 InfectedAction = action_quarantine;
702 return 0;
703 }
704
705 sfsistat clamfi_envfrom(SMFICTX *ctx, char **argv)
706 {
707 struct CLAMFI *cf;
708 const char *login = smfi_getsymval(ctx, "{auth_authen}");
709
710 if (login && smtpauthed(login)) {
711 logg(LOGG_DEBUG, "Skipping scan for authenticated user %s\n", login);
712 return SMFIS_ACCEPT;
713 }
714
715 if (allowed(argv[0], 1)) {
716 logg(LOGG_DEBUG, "Skipping scan for %s (allowed from)\n", argv[0]);
717 return SMFIS_ACCEPT;
718 }
719
720 if (!(cf = (struct CLAMFI *)malloc(sizeof(*cf)))) {
721 logg(LOGG_ERROR, "Failed to allocate CLAMFI struct\n");
722 return FailAction;
723 }
724 cf->totsz = 0;
725 cf->bufsz = 0;
726 cf->main = cf->alt = -1;
727 cf->all_allowed = 1;
728 cf->gotbody = 0;
729 cf->msg_subj = cf->msg_date = cf->msg_id = NULL;
730 if (multircpt) {
731 cf->recipients = NULL;
732 cf->nrecipients = 0;
733 }
734 if (addxvirus == 1) {
735 cf->scanned_count = 0;
736 cf->status_count = 0;
737 }
738 smfi_setpriv(ctx, (void *)cf);
739
740 return SMFIS_CONTINUE;
741 }
742
743 sfsistat clamfi_envrcpt(SMFICTX *ctx, char **argv)
744 {
745 struct CLAMFI *cf;
746
747 if (!(cf = (struct CLAMFI *)smfi_getpriv(ctx)))
748 return SMFIS_CONTINUE; /* whatever */
749
750 if (cf->all_allowed)
751 cf->all_allowed &= allowed(argv[0], 0);
752
753 if (multircpt) {
754 void *new_rcpt = realloc(cf->recipients, (cf->nrecipients + 1) * sizeof(*(cf->recipients)));
755 unsigned int rcpt_cnt;
756 if (!new_rcpt) {
757 logg(LOGG_ERROR, "Failed to allocate array for new recipient\n");
758 nullify(ctx, cf, CF_ANY);
759 free(cf);
760 return FailAction;
761 }
762 cf->recipients = new_rcpt;
763 rcpt_cnt = cf->nrecipients++;
764 if (!(cf->recipients[rcpt_cnt] = strdup(argv[0]))) {
765 logg(LOGG_ERROR, "Failed to allocate space for new recipient\n");
766 nullify(ctx, cf, CF_ANY);
767 free(cf);
768 return FailAction;
769 }
770 }
771
772 return SMFIS_CONTINUE;
773 }
774
775 /*
776 * Local Variables:
777 * mode: c
778 * c-basic-offset: 4
779 * tab-width: 8
780 * End:
781 * vim: set cindent smartindent autoindent softtabstop=4 shiftwidth=4 tabstop=8:
782 */