aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/sa.bi
blob: 231ad00ef6ce43c9f18cacdb66e63645126fe070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <signal.h>

MF_VAR(sa_score, NUMBER);
MF_VAR(sa_threshold, NUMBER);
MF_VAR(sa_keywords, STRING);
MF_VAR(clamav_virus_name, STRING);

static int
set_xscript(mu_stream_t *pstr)
{
	int rc;
	mu_stream_t tstr = *pstr;
	
	if (mu_debug_level_p(debug_handle, MU_DEBUG_PROT)) {
		int rc;
		mu_stream_t dstr;

		rc = mu_dbgstream_create(&dstr, MU_DIAG_DEBUG);
		if (rc)
			mu_error(_("cannot create debug stream; "
				   "transcript disabled: %s"),
				 mu_strerror(rc));
		else {
			rc = mu_xscript_stream_create(pstr, tstr, dstr, NULL);
			mu_stream_unref(dstr);
			if (rc)
				mu_error(_("cannot create transcript "
					   "stream: %s"),
					 mu_strerror(rc));
			else
				mu_stream_unref(tstr);
		}
	}
	return rc;
}

static int
spamd_send_stream(mu_stream_t out, mu_stream_t in)
{
  int rc;
  struct mu_buffer_query newbuf, oldbuf;
  int bufchg = 0;
  int xlev;
  int xlevchg = 0;
  
  /* Ensure effective transport buffering */
  if (mu_stream_ioctl(out, MU_IOCTL_TRANSPORT_BUFFER,
		      MU_IOCTL_OP_GET, &oldbuf) == 0) {
	  newbuf.type = MU_TRANSPORT_OUTPUT;
	  newbuf.buftype = mu_buffer_full;
	  newbuf.bufsize = 64*1024;
	  mu_stream_ioctl(out, MU_IOCTL_TRANSPORT_BUFFER, MU_IOCTL_OP_SET, 
			  &newbuf);
	  bufchg = 1;
  }

  if (!mu_debug_level_p(debug_handle, MU_DEBUG_TRACE9)) {
	  /* Mark out the following data as payload */
	  xlev = MU_XSCRIPT_PAYLOAD;
	  if (mu_stream_ioctl(out, MU_IOCTL_XSCRIPTSTREAM,
			      MU_IOCTL_XSCRIPTSTREAM_LEVEL, &xlev) == 0)
		  xlevchg = 1;
  }
  
  rc = mu_stream_copy(out, in, 0, NULL);

  /* Restore prior transport buffering and xscript level */
  if (bufchg)
    mu_stream_ioctl(out, MU_IOCTL_TRANSPORT_BUFFER, MU_IOCTL_OP_SET,  &oldbuf);
  if (xlevchg)
    mu_stream_ioctl(out, MU_IOCTL_XSCRIPTSTREAM,
		    MU_IOCTL_XSCRIPTSTREAM_LEVEL, &xlev);
  return rc;
}


static int
spamd_connect(eval_environ_t env, mu_stream_t *pstream,
	      const char *host, int port)
{
	int rc;
	char *fname;
	mu_stream_t tstr, istr, ostr;
	
	if (port) {
		fname = "mu_tcp_stream_create";
		rc = mu_tcp_stream_create(&tstr, host, port, MU_STREAM_RDWR);
	} else {
		fname = "mu_socket_stream_create";
		rc = mu_socket_stream_create(&tstr, host, MU_STREAM_RDWR);
	}

	MF_ASSERT(rc == 0, mfe_failure,
		  "%s: %s",
		  fname, mu_strerror(rc));

	mu_stream_set_buffer (tstr, mu_buffer_line, 0);

	rc = mu_filter_create(&istr, tstr, "CRLF",
			      MU_FILTER_DECODE, MU_FILTER_READ);
	if (rc) {
		mu_stream_unref(tstr);
		MF_THROW(mfe_failure,
			 "%s input filter: %s",
			 fname, mu_strerror(rc));
	}
	mu_stream_set_buffer(istr, mu_buffer_line, 0);
	
	rc = mu_filter_create(&ostr, tstr, "CRLF",
			      MU_FILTER_ENCODE, MU_FILTER_WRITE);
	if (rc) {
		mu_stream_unref(tstr);
		MF_THROW(mfe_failure,
			 "%s output filter: %s",
			 fname, mu_strerror(rc));
	}
	mu_stream_set_buffer(ostr, mu_buffer_line, 0);
	mu_stream_unref(tstr);
	
	rc = mu_iostream_create(&tstr, istr, ostr);
	mu_stream_unref(istr);
	mu_stream_unref(ostr);
	MF_ASSERT(rc == 0, mfe_failure,
		  "I/O stream: %s",
		  mu_strerror(rc));
	mu_stream_set_buffer(tstr, mu_buffer_line, 0);
	set_xscript(&tstr);
	*pstream = tstr;
	return rc;
}

static int
spamd_get_line(mu_stream_t stream, char **pbuffer, size_t *psize)
{
	int rc = mu_stream_getline(stream, pbuffer, psize, NULL);
	if (rc == 0)
		mu_rtrim_class (*pbuffer, MU_CTYPE_SPACE);
	return rc;
}

#define char_to_num(c) (c-'0')

static void
decode_float(long *vn, char *str, int digits)
{
	long v;
	size_t frac = 0;
	size_t base = 1;
	int i;
	int negative = 0;
  
	for (i = 0; i < digits; i++)
		base *= 10;
  
	v = strtol(str, &str, 10);
	if (v < 0) {
		negative = 1;
		v = - v;
	}
  
	v *= base;
	if (*str == '.') {
		for (str++, i = 0; *str && i < digits; i++, str++)
			frac = frac * 10 + char_to_num (*str);
		if (*str) {
			if (char_to_num(*str) >= 5)
				frac++;
		}
		else
			for (; i < digits; i++)
				frac *= 10;
	}
	*vn = v + frac;
	if (negative)
		*vn = - *vn;
}

static int
decode_boolean (char *str)
{
	if (strcasecmp (str, "true") == 0)
		return 1;
	else if (strcasecmp (str, "false") == 0)
		return 0;
	/*else?*/
	return 0;
}

typedef RETSIGTYPE (*signal_handler_fn)(int);

static signal_handler_fn
set_signal_handler (int sig, signal_handler_fn h)
{
#ifdef HAVE_SIGACTION
	struct sigaction act, oldact;
	act.sa_handler = h;
	sigemptyset (&act.sa_mask);
	act.sa_flags = 0;
	sigaction (sig, &act, &oldact);
	return oldact.sa_handler;
#else
	return signal (sig, h);
#endif
}

static int got_sigpipe;

static RETSIGTYPE
sigpipe_handler (int sig)
{
	got_sigpipe = 1;
}

mu_stream_t
open_connection(eval_environ_t env, char *urlstr, char **phost)
{
	char *path = NULL;
	short port = 0;
	mu_stream_t str = NULL;
	int rc;
	
	if (urlstr[0] == '/') {
		path = strdup(urlstr);
		if (!path)
			runtime_error(env, _("not enough memory"));
	} else {
		const char *buf;
		mu_url_t url = NULL;
		
		rc = mu_url_create(&url, urlstr);
		if (rc)
			MF_THROW(mfe_failure, 
				 _("cannot create URL from `%s': %s"),
				 urlstr, mu_strerror(rc));
		
		if (rc = mu_url_sget_scheme(url, &buf)) {
			mu_url_destroy(&url);
			MF_THROW(mfe_url,
				     _("%s: cannot get scheme: %s"),
				     urlstr, mu_strerror(rc));
		}

		if (strcmp(buf, "file") == 0
		    || strcmp(buf, "socket") == 0) {
			if (rc = mu_url_aget_path(url, &path)) {
				MF_THROW(mfe_url,
					 _("%s: cannot get path: %s"),
					 urlstr, mu_strerror(rc));
			}
		} else if (strcmp(buf, "tcp") == 0) {
			unsigned n;
			
			if (rc = mu_url_get_port(url, &n)) {
				mu_url_destroy(&url);
				MF_THROW(mfe_url,
					 _("%s: cannot get port: %s"),
					 urlstr, mu_strerror(rc));
			}

			if (n == 0 || (port = n) != n) {
				mu_url_destroy(&url);
				MF_THROW(mfe_range,
					 _("port out of range: %u"),
					 n);
			}
			
			if (rc = mu_url_aget_host(url, &path)) {
				mu_url_destroy(&url);
				MF_THROW(mfe_url,
					 _("%s: cannot get host: %s"),
					 urlstr, mu_strerror(rc));
			}
		} else
			MF_THROW(mfe_url,
				 _("invalid URL: %s"), buf);

		mu_url_destroy(&url);
	}

	rc = spamd_connect(env, &str, path, port);
	
	if (rc == 0 && phost) {
		if (port)
			*phost = path;
		else
			*phost = NULL;
	}
	return str;
}	

MF_DSEXP
MF_STATE(eom)
MF_CAPTURE(mstr)	
MF_DEFUN(sa, NUMBER, STRING urlstr, NUMBER prec, OPTIONAL, NUMBER report)
{
	mu_off_t msize;
	mu_stream_t ostr;
	signal_handler_fn handler;
	char *buffer = NULL;
	size_t bufsize = 0;
	size_t n;
	char version_str[19];
	char spam_str[6], score_str[21], threshold_str[21];
	long version;
	int result;
	long score, threshold;
	int report_mode = MF_OPTVAL(report);
	int rc;
	
	rc = mu_stream_size(mstr, &msize);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  "mu_stream_size: %s",
		  mu_strerror (rc));
	
	ostr = open_connection(env, urlstr, NULL);
		
	msize += env_get_line_count(env);
	mu_stream_printf(ostr,
			   report_mode ? "REPORT SPAMC/1.2\n"
			               : "SYMBOLS SPAMC/1.2\n");
	mu_stream_printf(ostr, "Content-length: %lu\n",
			 (unsigned long) msize);
	/*FIXME: spamd_send_command(ostr, "User: %s", ??) */
	got_sigpipe = 0;
	handler = set_signal_handler(SIGPIPE, sigpipe_handler);
	mu_stream_write(ostr, "\n", 1, NULL);
	if (rc = spamd_send_stream(ostr, mstr)) {
		mu_stream_destroy(&ostr);
		MF_THROW(mfe_failure,
			 _("send stream failed: %s"),
			 mu_strerror (rc));
	}
	mu_stream_shutdown(ostr, MU_STREAM_WRITE);
	set_signal_handler(SIGPIPE, handler);
	
	spamd_get_line(ostr, &buffer, &bufsize);
	if (got_sigpipe) {
		mu_stream_destroy(&ostr);
		free(buffer);
		MF_THROW(mfe_failure, _("remote side has closed connection"));
	}

	if (sscanf(buffer, "SPAMD/%18s %d %*s", version_str, &result) != 2) {
		mu_stream_destroy(&ostr);
		free(buffer);
		MF_THROW(mfe_failure,
			 _("spamd responded with bad string '%s'"),
			 buffer);
	}

	decode_float(&version, version_str, 1);
	if (version < 10) {
		mu_stream_destroy(&ostr);
		free(buffer);
		MF_THROW(mfe_failure,
			 _("unsupported SPAMD version: %s"),
			 version_str);
	}

	if (result) {
		mu_stream_destroy(&ostr);
		free(buffer);
		MF_THROW(mfe_failure, "%s", buffer);
	}
	
	spamd_get_line(ostr, &buffer, &bufsize);
	if (sscanf (buffer, "Spam: %5s ; %20s / %20s",
		    spam_str, score_str, threshold_str) != 3) {
		mu_stream_destroy(&ostr);
		free(buffer);
		MF_THROW(mfe_failure,
			 _("spamd responded with bad Spam header '%s'"),
			 buffer);
	}

	result = decode_boolean(spam_str);
	decode_float(&score, score_str, prec);
	decode_float(&threshold, threshold_str, prec);

	MF_VAR_REF(sa_score, score);
	MF_VAR_REF(sa_threshold, threshold);

	/* Skip newline */
	spamd_get_line(ostr, &buffer, &bufsize);
	if (report_mode) {
		MF_OBSTACK_BEGIN();
		while (mu_stream_getline(ostr, &buffer, &bufsize, &n) == 0
		       && n > 0) 
			MF_OBSTACK_GROW(buffer, n);
		MF_OBSTACK_1GROW(0);
		MF_VAR_REF(sa_keywords, MF_OBSTACK_FINISH);
	} else {
		/* Read symbol list */
		spamd_get_line(ostr, &buffer, &bufsize);

		MF_VAR_SET_STRING(sa_keywords, buffer);
	
		while (mu_stream_getline(ostr, &buffer, &bufsize, &n) == 0
		       && n > 0) 
			/* Drain input */;
	}
  	mu_stream_destroy(&ostr);
	free(buffer);
	MF_RETURN(result);
}
END

static int
clamav_open_data_stream(mu_stream_t *retstr, const char *host, unsigned port)
{
	mu_stream_t tstr, dstr;
	int rc = mu_tcp_stream_create(&dstr, host, port, 0);
	if (rc)
		return rc;
	rc = mu_filter_create(&tstr, dstr, "CRLF",
			      MU_FILTER_ENCODE, MU_FILTER_WRITE);
	mu_stream_unref(dstr);
	if (rc)
		return rc;
	set_xscript(&tstr);
	*retstr = tstr;
	return 0;
}
	
MF_DSEXP
MF_STATE(eom)
MF_CAPTURE(mstr)	
MF_DEFUN(clamav, NUMBER, STRING urlstr)
{
	mu_stream_t cstr, dstr;
	char *buffer = NULL;
	size_t bufsize = 0;
	char *host;
	unsigned short port;
	int rc;
	signal_handler_fn handler;
	char *p;

	cstr = open_connection(env, urlstr, &host);

	mu_stream_printf(cstr, "STREAM\n");
	spamd_get_line(cstr, &buffer, &bufsize);
	if (sscanf(buffer, "PORT %hu", &port) != 1) {
		mu_stream_destroy(&cstr);
		free(buffer);
		MF_THROW(mfe_failure,
		 _("bad response from clamav: expected `PORT' but found `%s'"),
			 buffer);
	}

	if (!host) 
		host = strdup("127.0.0.1"); /* FIXME */
	rc = clamav_open_data_stream(&dstr, host, port);
	free(host);
	if (rc) {
		mu_stream_destroy(&cstr);
		MF_THROW(mfe_failure,
			 "mu_tcp_stream_create: %s",
			 mu_strerror(rc));
	}
	
	handler = set_signal_handler(SIGPIPE, sigpipe_handler);
	rc = spamd_send_stream(dstr, mstr);
	mu_stream_shutdown(dstr, MU_STREAM_WRITE);
	mu_stream_destroy(&dstr);
	set_signal_handler(SIGPIPE, handler);
	if (rc) {
		mu_stream_destroy(&cstr);
		free(buffer);
		MF_THROW(mfe_failure,
			 _("sending to stream failed: %s"),
			 mu_strerror (rc));
	}

	rc = spamd_get_line(cstr, &buffer, &bufsize);
	mu_stream_destroy(&cstr);
	MF_ASSERT(rc == 0, mfe_failure, _("error reading clamav response: %s"),
		  mu_strerror(rc));

	p = strrchr(buffer, ' ');
        MF_ASSERT(p, mfe_failure,
	          _("unknown clamav response: %s"), buffer);
	++p;
	if (strncmp(p, "OK", 2) == 0)
		rc = 0;
	else if (strncmp(p, "FOUND", 5) == 0) {
		char *s;
			
		*--p = '\0';
		s = strrchr(buffer, ' ');
		if (!s)
			s = buffer;
		else
			s++;
		MF_VAR_SET_STRING(clamav_virus_name, s);

		MF_DEBUG(2,
		         ("%sclamav found %s",
		          mailfromd_msgid(env_get_context(env)), s));
		rc = 1;
	} else if (strncmp(p, "ERROR", 5) == 0) {
		/* FIXME: mf code */
		free(buffer);
		MF_THROW(mfe_failure,
			 _("clamav error: %s"),
			 buffer);
	} else {
		free(buffer);
		MF_THROW(mfe_failure,
			 _("unknown clamav response: %s"),
			 buffer);
	}
	free(buffer);
	MF_RETURN(rc);
}
END

MF_INIT

Return to:

Send suggestions and report system problems to the System administrator.