summaryrefslogtreecommitdiffabout
Unidiff
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
@@ -2,2 +2,5 @@
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
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
@@ -29,7 +29,127 @@ MF_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,
@@ -37,3 +157,3 @@ spamd_connect_tcp(eval_environ_t env, mu_stream_t *stream,
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,
@@ -41,2 +161,3 @@ spamd_connect_tcp(eval_environ_t env, mu_stream_t *stream,
41 mu_strerror(rc)); 161 mu_strerror(rc));
162 *pmfs = mf_stream_new(env, stream, time(NULL), timeout);
42 return rc; 163 return rc;
@@ -45,3 +166,4 @@ spamd_connect_tcp(eval_environ_t env, mu_stream_t *stream,
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{
@@ -50,2 +172,3 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path)
50 struct sockaddr_un addr; 172 struct sockaddr_un addr;
173 mu_stream_t stream;
51 174
@@ -65,3 +188,3 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path)
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) {
@@ -73,5 +196,5 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path)
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);
@@ -81,2 +204,5 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path)
81 } 204 }
205
206 *pmfs = mf_stream_new(env, stream, time(NULL), timeout);
207
82 return rc; 208 return rc;
@@ -85,9 +211,9 @@ spamd_connect_socket(eval_environ_t env, mu_stream_t *stream, char *path)
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)
@@ -100,6 +226,9 @@ spamd_shutdown(mu_stream_t stream, int isfile, int flag)
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}
@@ -107,3 +236,3 @@ spamd_destroy(mu_stream_t *stream)
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{
@@ -116,4 +245,4 @@ spamd_send_command(mu_stream_t stream, const char *fmt, ...)
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}
@@ -121,3 +250,3 @@ spamd_send_command(mu_stream_t stream, const char *fmt, ...)
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{
@@ -131,3 +260,3 @@ spamd_send_stream(mu_stream_t stream, mu_stream_t instr)
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 }
@@ -136,6 +265,6 @@ spamd_send_stream(mu_stream_t stream, mu_stream_t instr)
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) {
@@ -189,7 +318,7 @@ decode_float(long *vn, char *str, int digits)
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;
@@ -224,4 +353,5 @@ sigpipe_handler (int sig)
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{
@@ -229,3 +359,3 @@ open_connection(eval_environ_t env, char *urlstr, int *isfile, char **phost)
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;
@@ -312,6 +442,6 @@ open_connection(eval_environ_t env, char *urlstr, int *isfile, char **phost)
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;
@@ -334,3 +464,3 @@ MF_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{
@@ -339,3 +469,3 @@ MF_DEFUN(sa, NUMBER, STRING urlstr, NUMBER prec)
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;
@@ -349,3 +479,4 @@ MF_DEFUN(sa, NUMBER, STRING urlstr, NUMBER prec)
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);
@@ -416,6 +547,7 @@ MF_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];
@@ -427,4 +559,5 @@ MF_DEFUN(clamav, NUMBER, STRING urlstr)
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
@@ -441,4 +574,4 @@ MF_DEFUN(clamav, NUMBER, STRING urlstr)
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);
@@ -451,6 +584,6 @@ MF_DEFUN(clamav, NUMBER, STRING urlstr)
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,
@@ -461,4 +594,7 @@ MF_DEFUN(clamav, NUMBER, STRING urlstr)
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);
diff --git a/src/dnsbase.c b/src/dnsbase.c
index cf4b95a..cbb658d 100644
--- a/src/dnsbase.c
+++ b/src/dnsbase.c
@@ -249,3 +249,3 @@ struct loop_data {
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
@@ -420,3 +420,3 @@ cname_loop_body(struct loop_data *lp)
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;

Return to:

Send suggestions and report system problems to the System administrator.