summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2007-10-21 18:38:25 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2007-10-21 18:38:25 (GMT)
commit9701509fced80d18b30bde4e51bca7fb2e73b26e (patch) (unidiff)
treed32015055f10b55bd6f46bb22bb884283061653f
parent44e921546471416e57574f9c00a8014864b60276 (diff)
downloadmailfromd-9701509fced80d18b30bde4e51bca7fb2e73b26e.tar.gz
mailfromd-9701509fced80d18b30bde4e51bca7fb2e73b26e.tar.bz2
* src/dnsbase.c (cname_loop_body): Return TXT only if explicitly
required. Bug reported by Jan Rafaj. git-svn-id: file:///svnroot/mailfromd/trunk@1518 7a8a7f39-df28-0410-adc6-e0d955640f24
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--ChangeLog3
-rw-r--r--src/bi_sa.m4218
-rw-r--r--src/dnsbase.c4
3 files changed, 182 insertions, 43 deletions
diff --git a/ChangeLog b/ChangeLog
index c50cbd6..053fa59 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
12007-10-21 Sergey Poznyakoff <gray@gnu.org.ua> 12007-10-21 Sergey Poznyakoff <gray@gnu.org.ua>
2 2
3 * src/dnsbase.c (cname_loop_body): Return TXT only if explicitly
4 required. Bug reported by Jan Rafaj.
5
3 * src/engine.c: Fix timeout calculations 6 * src/engine.c: Fix timeout calculations
4 (struct timeout_ctl): New data type 7 (struct timeout_ctl): New data type
5 (UPDATE_TTW,smtp_stream_wait,smtp_wait): Operate on struct timeout_ctl. 8 (UPDATE_TTW,smtp_stream_wait,smtp_wait): Operate on struct timeout_ctl.
diff --git a/src/bi_sa.m4 b/src/bi_sa.m4
index 0ed7472..8472bbe 100644
--- a/src/bi_sa.m4
+++ b/src/bi_sa.m4
@@ -27,27 +27,150 @@ MF_VAR(sa_threshold, NUMBER);
27MF_VAR(sa_keywords, STRING); 27MF_VAR(sa_keywords, STRING);
28MF_VAR(clamav_virus_name, STRING); 28MF_VAR(clamav_virus_name, STRING);
29 29
30
31struct mf_stream {
32 eval_environ_t env;
33 mu_stream_t str;
34 time_t start;
35 time_t timeout;
36};
37
38typedef struct mf_stream *mf_stream_t;
39
40/* FIXME: duplicated in engine.c */
41#define UPDATE_TTW(t,start) do { \
42 time_t delta = time(NULL) - start; \
43 if (t > delta) \
44 t -= delta; \
45 else \
46 t = 0; \
47} while (0)
48
49extern int smtp_stream_wait(mu_stream_t stream, int flags, time_t *timeout);
50static void spamd_destroy(mf_stream_t *pstream);
51
52mf_stream_t
53mf_stream_new (eval_environ_t env, mu_stream_t stream, time_t start,
54 time_t timeout)
55{
56 mf_stream_t pmfs = xmalloc (sizeof pmfs[0]);
57 pmfs->env = env;
58 pmfs->str = stream;
59 pmfs->start = start;
60 pmfs->timeout = timeout;
61 return pmfs;
62}
63
64int
65mf_stream_sequential_read(mf_stream_t stream, char *buf, size_t size)
66{
67 int rc;
68 eval_environ_t env = stream->env;
69
70 if (stream->timeout == 0) {
71 spamd_destroy(&stream);
72 MF_THROW(mf_temp_failure,
73 "sa_sequential_read: %s",
74 mu_strerror(ETIMEDOUT));
75 }
76
77 rc = smtp_stream_wait(stream->str, MU_STREAM_READY_RD,
78 &stream->timeout);
79 if (rc == 0) {
80 UPDATE_TTW(stream->timeout, stream->start);
81 rc = mu_stream_sequential_read(stream->str, buf, size, NULL);
82 } else {
83 spamd_destroy(&stream);
84 MF_THROW(mf_temp_failure,
85 "sa_sequential_read: %s",
86 mu_strerror(ETIMEDOUT));
87 }
88
89 return rc;
90}
91
92int
93mf_stream_sequential_readline(mf_stream_t stream, char *buf, size_t size,
94 size_t *nbytes)
95{
96 int rc;
97 eval_environ_t env = stream->env;
98
99 if (stream->timeout == 0) {
100 spamd_destroy(&stream);
101 MF_THROW(mf_temp_failure,
102 "sa_sequential_readline: %s",
103 mu_strerror(ETIMEDOUT));
104 }
105 rc = smtp_stream_wait(stream->str, MU_STREAM_READY_RD,
106 &stream->timeout);
107 if (rc == 0) {
108 UPDATE_TTW(stream->timeout, stream->start);
109 rc = mu_stream_sequential_readline(stream->str, buf, size,
110 nbytes);
111 } else {
112 spamd_destroy(&stream);
113 MF_THROW(mf_temp_failure,
114 "sa_sequential_readline: %s",
115 mu_strerror(ETIMEDOUT));
116 }
117 return rc;
118}
119
120int
121mf_stream_sequential_write(mf_stream_t stream, char *buf, size_t size)
122{
123 int rc;
124 eval_environ_t env = stream->env;
125
126 if (stream->timeout == 0) {
127 spamd_destroy(&stream);
128 MF_THROW(mf_temp_failure,
129 "sa_sequential_write: %s",
130 mu_strerror(ETIMEDOUT));
131 }
132
133 rc = smtp_stream_wait(stream->str, MU_STREAM_READY_WR,
134 &stream->timeout);
135 if (rc == 0) {
136 UPDATE_TTW(stream->timeout, stream->start);
137 rc = mu_stream_sequential_write(stream->str, buf, size);
138 } else {
139 spamd_destroy(&stream);
140 MF_THROW(mf_temp_failure,
141 "sa_sequential_write: %s",
142 mu_strerror(ETIMEDOUT));
143 }
144 return rc;
145}
146
147
148
30static int 149static int
31spamd_connect_tcp(eval_environ_t env, mu_stream_t *stream, 150spamd_connect_tcp(eval_environ_t env, mf_stream_t *pmfs,
32 char *host, int port) 151 char *host, int port, time_t timeout)
33{ 152{
34 int rc = mu_tcp_stream_create(stream, host, port, 0); 153 mu_stream_t stream;
154 int rc = mu_tcp_stream_create(&stream, host, port, 0);
35 MF_ASSERT(rc == 0, mf_failure, 155 MF_ASSERT(rc == 0, mf_failure,
36 "mu_tcp_stream_create: %s", 156 "mu_tcp_stream_create: %s",
37 mu_strerror(rc)); 157 mu_strerror(rc));
38 rc = mu_stream_open(*stream); 158 rc = mu_stream_open(stream);
39 MF_ASSERT(rc == 0, mf_failure, 159 MF_ASSERT(rc == 0, mf_failure,
40 "mu_stream_open: %s", 160 "mu_stream_open: %s",
41 mu_strerror(rc)); 161 mu_strerror(rc));
162 *pmfs = mf_stream_new(env, stream, time(NULL), timeout);
42 return rc; 163 return rc;
43} 164}
44 165
45static int 166static int
46spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path) 167spamd_connect_socket(eval_environ_t env, mf_stream_t *pmfs, char *path,
168 time_t timeout)
47{ 169{
48 int fd, rc; 170 int fd, rc;
49 FILE *fp; 171 FILE *fp;
50 struct sockaddr_un addr; 172 struct sockaddr_un addr;
173 mu_stream_t stream;
51 174
52 fd = socket(PF_UNIX, SOCK_STREAM, 0); 175 fd = socket(PF_UNIX, SOCK_STREAM, 0);
53 MF_ASSERT(fd >= 0, mf_failure, 176 MF_ASSERT(fd >= 0, mf_failure,
@@ -63,7 +186,7 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path)
63 } 186 }
64 187
65 fp = fdopen(fd, "w+"); 188 fp = fdopen(fd, "w+");
66 rc = mu_stdio_stream_create(stream, fp, MU_STREAM_RDWR); 189 rc = mu_stdio_stream_create(&stream, fp, MU_STREAM_RDWR);
67 if (rc) { 190 if (rc) {
68 fclose(fp); 191 fclose(fp);
69 MF_THROW(mf_failure, 192 MF_THROW(mf_failure,
@@ -71,25 +194,28 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path)
71 mu_strerror(rc)); 194 mu_strerror(rc));
72 } 195 }
73 196
74 rc = mu_stream_open(*stream); 197 rc = mu_stream_open(stream);
75 if (rc) { 198 if (rc) {
76 mu_stream_destroy (stream, mu_stream_get_owner (*stream)); 199 mu_stream_destroy(&stream, mu_stream_get_owner(stream));
77 fclose(fp); 200 fclose(fp);
78 MF_THROW(mf_failure, 201 MF_THROW(mf_failure,
79 "mu_stream_open: %s", 202 "mu_stream_open: %s",
80 mu_strerror(rc)); 203 mu_strerror(rc));
81 } 204 }
205
206 *pmfs = mf_stream_new(env, stream, time(NULL), timeout);
207
82 return rc; 208 return rc;
83} 209}
84 210
85static void 211static void
86spamd_shutdown(mu_stream_t stream, int isfile, int flag) 212spamd_shutdown(mf_stream_t stream, int isfile, int flag)
87{ 213{
88 int fd; 214 int fd;
89
90 mu_transport_t trans; 215 mu_transport_t trans;
91 mu_stream_flush(stream); 216
92 mu_stream_get_transport(stream, &trans); 217 mu_stream_flush(stream->str);
218 mu_stream_get_transport(stream->str, &trans);
93 if (isfile) 219 if (isfile)
94 fd = fileno((FILE*)trans); 220 fd = fileno((FILE*)trans);
95 else 221 else
@@ -98,14 +224,17 @@ spamd_shutdown(mu_stream_t stream, int isfile, int flag)
98} 224}
99 225
100static void 226static void
101spamd_destroy(mu_stream_t *stream) 227spamd_destroy(mf_stream_t *pstream)
102{ 228{
103 mu_stream_close(*stream); 229 mf_stream_t stream = *pstream;
104 mu_stream_destroy(stream, mu_stream_get_owner(*stream)); 230 mu_stream_close(stream->str);
231 mu_stream_destroy(&stream->str, mu_stream_get_owner(stream->str));
232 free(stream);
233 *pstream = NULL;
105} 234}
106 235
107static void 236static void
108spamd_send_command(mu_stream_t stream, const char *fmt, ...) 237spamd_send_command(mf_stream_t stream, const char *fmt, ...)
109{ 238{
110 char buf[512]; 239 char buf[512];
111 size_t n; 240 size_t n;
@@ -114,12 +243,12 @@ spamd_send_command(mu_stream_t stream, const char *fmt, ...)
114 va_start (ap, fmt); 243 va_start (ap, fmt);
115 n = vsnprintf (buf, sizeof buf, fmt, ap); 244 n = vsnprintf (buf, sizeof buf, fmt, ap);
116 va_end (ap); 245 va_end (ap);
117 mu_stream_sequential_write(stream, buf, n); 246 mf_stream_sequential_write(stream, buf, n);
118 mu_stream_sequential_write(stream, "\r\n", 2); 247 mf_stream_sequential_write(stream, "\r\n", 2);
119} 248}
120 249
121static void 250static void
122spamd_send_stream(mu_stream_t stream, mu_stream_t instr) 251spamd_send_stream(mf_stream_t stream, mu_stream_t instr)
123{ 252{
124 size_t size; 253 size_t size;
125 char buf[512]; 254 char buf[512];
@@ -129,15 +258,15 @@ spamd_send_stream(mu_stream_t stream, mu_stream_t instr)
129 &size) == 0 258 &size) == 0
130 && size > 0) { 259 && size > 0) {
131 debug3(80,"<< %*.*s", size, size, buf); 260 debug3(80,"<< %*.*s", size, size, buf);
132 mu_stream_sequential_write (stream, buf, size); 261 mf_stream_sequential_write(stream, buf, size);
133 } 262 }
134} 263}
135 264
136static int 265static int
137spamd_read_line(mu_stream_t stream, char *buffer, size_t size, size_t *pn) 266spamd_read_line(mf_stream_t stream, char *buffer, size_t size, size_t *pn)
138{ 267{
139 size_t n = 0; 268 size_t n = 0;
140 int rc = mu_stream_sequential_readline(stream, buffer, size, &n); 269 int rc = mf_stream_sequential_readline(stream, buffer, size, &n);
141 if (rc == 0) { 270 if (rc == 0) {
142 if (pn) 271 if (pn)
143 *pn = n; 272 *pn = n;
@@ -187,11 +316,11 @@ decode_float(long *vn, char *str, int digits)
187} 316}
188 317
189static int 318static int
190decode_boolean (char *str) 319decode_boolean(char *str)
191{ 320{
192 if (strcasecmp (str, "true") == 0) 321 if (strcasecmp(str, "true") == 0)
193 return 1; 322 return 1;
194 else if (strcasecmp (str, "false") == 0) 323 else if (strcasecmp(str, "false") == 0)
195 return 0; 324 return 0;
196 /*else?*/ 325 /*else?*/
197 return 0; 326 return 0;
@@ -222,12 +351,13 @@ sigpipe_handler (int sig)
222 got_sigpipe = 1; 351 got_sigpipe = 1;
223} 352}
224 353
225mu_stream_t 354mf_stream_t
226open_connection(eval_environ_t env, char *urlstr, int *isfile, char **phost) 355open_connection(eval_environ_t env, char *urlstr, int *isfile, char **phost,
356 long timeout)
227{ 357{
228 char *path = NULL; 358 char *path = NULL;
229 short port = 0; 359 short port = 0;
230 mu_stream_t str = NULL; 360 mf_stream_t str = NULL;
231 int rc; 361 int rc;
232 362
233 if (urlstr[0] == '/') { 363 if (urlstr[0] == '/') {
@@ -310,10 +440,10 @@ open_connection(eval_environ_t env, char *urlstr, int *isfile, char **phost)
310 } 440 }
311 441
312 if (port == 0) { 442 if (port == 0) {
313 rc = spamd_connect_socket(env, &str, path); 443 rc = spamd_connect_socket(env, &str, path, timeout);
314 *isfile = 1; 444 *isfile = 1;
315 } else { 445 } else {
316 rc = spamd_connect_tcp(env, &str, path, port); 446 rc = spamd_connect_tcp(env, &str, path, port, timeout);
317 *isfile = 0; 447 *isfile = 0;
318 } 448 }
319 449
@@ -332,12 +462,12 @@ open_connection(eval_environ_t env, char *urlstr, int *isfile, char **phost)
332 462
333MF_STATE(eom) 463MF_STATE(eom)
334 MF_CAPTURE 464 MF_CAPTURE
335MF_DEFUN(sa, NUMBER, STRING urlstr, NUMBER prec) 465MF_DEFUN(sa, NUMBER, STRING urlstr, NUMBER prec, OPTIONAL, NUMBER timeout)
336{ 466{
337 mu_off_t msize; 467 mu_off_t msize;
338 size_t size; 468 size_t size;
339 mu_stream_t mstr = env_get_stream(env); 469 mu_stream_t mstr = env_get_stream(env);
340 mu_stream_t ostr; 470 mf_stream_t ostr;
341 signal_handler_fn handler; 471 signal_handler_fn handler;
342 char buffer[512]; 472 char buffer[512];
343 char version_str[19]; 473 char version_str[19];
@@ -347,7 +477,8 @@ MF_DEFUN(sa, NUMBER, STRING urlstr, NUMBER prec)
347 long score, threshold; 477 long score, threshold;
348 int isfile; 478 int isfile;
349 479
350 ostr = open_connection(env, urlstr, &isfile, NULL); 480 ostr = open_connection(env, urlstr, &isfile, NULL,
481 MF_OPTVAL(timeout, 7200));
351 mu_stream_size(mstr, &msize); 482 mu_stream_size(mstr, &msize);
352 spamd_send_command(ostr, "SYMBOLS SPAMC/1.2"); 483 spamd_send_command(ostr, "SYMBOLS SPAMC/1.2");
353 spamd_send_command(ostr, "Content-length: %lu", 484 spamd_send_command(ostr, "Content-length: %lu",
@@ -414,10 +545,11 @@ END
414 545
415MF_STATE(eom) 546MF_STATE(eom)
416 MF_CAPTURE 547 MF_CAPTURE
417MF_DEFUN(clamav, NUMBER, STRING urlstr) 548MF_DEFUN(clamav, NUMBER, STRING urlstr, OPTIONAL, NUMBER timeout)
418{ 549{
419 mu_stream_t mstr = env_get_stream(env); 550 mu_stream_t mstr = env_get_stream(env);
420 mu_stream_t cstr, dstr; 551 mf_stream_t cstr, dstr;
552 mu_stream_t str;
421 char buffer[512]; 553 char buffer[512];
422 char *host; 554 char *host;
423 unsigned short port; 555 unsigned short port;
@@ -425,8 +557,9 @@ MF_DEFUN(clamav, NUMBER, STRING urlstr)
425 signal_handler_fn handler; 557 signal_handler_fn handler;
426 char *p; 558 char *p;
427 int isfile; 559 int isfile;
428 560
429 cstr = open_connection(env, urlstr, &isfile, &host); 561 cstr = open_connection(env, urlstr, &isfile, &host,
562 MF_OPTVAL(timeout, 7200));
430 563
431 spamd_send_command(cstr, "STREAM"); 564 spamd_send_command(cstr, "STREAM");
432 spamd_read_line(cstr, buffer, sizeof buffer, NULL); 565 spamd_read_line(cstr, buffer, sizeof buffer, NULL);
@@ -439,8 +572,8 @@ MF_DEFUN(clamav, NUMBER, STRING urlstr)
439 572
440 if (!host) 573 if (!host)
441 host = strdup("127.0.0.1"); /* FIXME */ 574 host = strdup("127.0.0.1"); /* FIXME */
442 575
443 rc = mu_tcp_stream_create(&dstr, host, port, 0); 576 rc = mu_tcp_stream_create(&str, host, port, 0);
444 free(host); 577 free(host);
445 if (rc) { 578 if (rc) {
446 spamd_destroy(&cstr); 579 spamd_destroy(&cstr);
@@ -449,18 +582,21 @@ MF_DEFUN(clamav, NUMBER, STRING urlstr)
449 mu_strerror(rc)); 582 mu_strerror(rc));
450 } 583 }
451 584
452 rc = mu_stream_open(dstr); 585 rc = mu_stream_open(str);
453 if (rc) { 586 if (rc) {
454 spamd_destroy(&cstr); 587 spamd_destroy(&cstr);
455 mu_stream_destroy(&dstr, mu_stream_get_owner(dstr)); 588 mu_stream_destroy(&str, mu_stream_get_owner(str));
456 MF_THROW(mf_failure, 589 MF_THROW(mf_failure,
457 "mu_stream_open: %s", 590 "mu_stream_open: %s",
458 mu_strerror(rc)); 591 mu_strerror(rc));
459 } 592 }
460 593
461 handler = set_signal_handler(SIGPIPE, sigpipe_handler); 594 handler = set_signal_handler(SIGPIPE, sigpipe_handler);
595 dstr = mf_stream_new (env, str, cstr->start, cstr->timeout);
462 spamd_send_stream(dstr, mstr); 596 spamd_send_stream(dstr, mstr);
463 spamd_shutdown(dstr, 0, SHUT_WR); 597 spamd_shutdown(dstr, 0, SHUT_WR);
598 spamd_destroy(&dstr);
599
464 set_signal_handler(SIGPIPE, handler); 600 set_signal_handler(SIGPIPE, handler);
465 601
466 rc = spamd_read_line(cstr, buffer, sizeof buffer, NULL); 602 rc = spamd_read_line(cstr, buffer, sizeof buffer, NULL);
diff --git a/src/dnsbase.c b/src/dnsbase.c
index cf4b95a..cbb658d 100644
--- a/src/dnsbase.c
+++ b/src/dnsbase.c
@@ -247,7 +247,7 @@ struct loop_data {
247 /* Return data: */ 247 /* Return data: */
248 char *hbuf; /* Return buffer */ 248 char *hbuf; /* Return buffer */
249 size_t hbsize; /* Size of return buffer */ 249 size_t hbsize; /* Size of return buffer */
250 size_t hbcount; /* ?? */ 250 size_t hbcount; /* Number of items returned */
251 251
252 time_t ttl; /* TTL value */ 252 time_t ttl; /* TTL value */
253 dns_status status; /* Status */ 253 dns_status status; /* Status */
@@ -418,7 +418,7 @@ cname_loop_body(struct loop_data *lp)
418 break; 418 break;
419 419
420 case T_TXT: 420 case T_TXT:
421 if (lp->atype != T_ANY && lp->atype != type) 421 if (lp->atype != type)
422 continue; 422 continue;
423 l = cp[0]; 423 l = cp[0];
424 if (lp->hbcount + l >= lp->hbsize) 424 if (lp->hbcount + l >= lp->hbsize)

Return to:

Send suggestions and report system problems to the System administrator.