From: Vsevolod Stakhov Date: Fri, 16 Jan 2009 14:02:26 +0000 (+0300) Subject: * Add simple utility for sending mail to rspamd X-Git-Tag: 0.2.7~326 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fe5ad5874aad220fb12a259e607f89ce5fae7465;p=rspamd.git * Add simple utility for sending mail to rspamd * Fix some errors in freeing message object --- diff --git a/Makefile.in b/Makefile.in index fc7078d1a..1448d4377 100644 --- a/Makefile.in +++ b/Makefile.in @@ -14,6 +14,7 @@ perl/Makefile: install: $(EXEC) cd perl && make install && cd .. $(INSTALL) -b $(EXEC) $(PREFIX)/sbin/$(EXEC) + $(INSTALL) -b rspamc.pl $(PREFIX)/bin/rspamc $(INSTALL) -v $(EXEC).sh $(PREFIX)/etc/rc.d #$(INSTALL) -m0644 rspamd.8 $(MANPATH)/man8 #$(INSTALL) -m0644 rspamd.conf.sample $(PREFIX)/etc diff --git a/rspamc.pl b/rspamc.pl new file mode 100755 index 000000000..ee203b85a --- /dev/null +++ b/rspamc.pl @@ -0,0 +1,108 @@ +#!/usr/bin/perl -w + +# Simple script that read message from STDIN and test it on rspamd server +# using specified command. +# +# Usage: rspamc.pl [-c conf_file] [command] +# +# By default rspamc.pl would read ./rspamd.conf and default command is SYMBOLS + +use Socket qw(:DEFAULT :crlf); + +my %cfg = ( + 'conf_file' => './rspamd.conf', + 'command' => 'SYMBOLS', + 'host' => 'localhost', + 'port' => '11333', + 'is_unix' => 0, +); + +sub usage { + return "Usage: rspamc.pl [-c conf_file] [command]"; +} + +while (my $param = shift) { + if ($param eq '-c') { + my $value = shift; + if ($value) { + if (-r $value) { + $cfg{'conf_file'} = $value; + } + else { + die "config file $value is not readable"; + } + } + else { + die usage(); + } + } + elsif ($param =~ /(SYMBOLS|SCAN|PROCESS|CHECK|REPORT_IFSPAM|REPORT)/i) { + $cfg{'command'} = $1; + } +} + +open CONF, "< $cfg{'conf_file'}" or die "config file $cfg{'conf_file'} cannot be opened"; + +my $ctrl = 0; +while () { + if ($_ =~ /control\s*{/i) { + $ctrl = 1; + } + if ($ctrl && $_ =~ /}/) { + $ctrl = 0; + } + if (!$ctrl && $_ =~ /^\s*bind_socket\s*=\s*((([^:]+):(\d+))|(\/\S*))/i) { + if ($3 && $4) { + $cfg{'host'} = $3; + $cfg{'port'} = $4; + $cfg{'is_unix'} = 0; + } + else { + $cfg{'host'} = $5; + $cfg{'is_unix'} = 1; + } + last; + } +} + +close CONF; + +if ($cfg{'is_unix'}) { + my $proto = getprotobyname('tcp'); + socket (SOCK, PF_UNIX, SOCK_STREAM, $proto) or die "cannot create unix socket"; + my $sun = sockaddr_un($cfg{'host'}); + connect (SOCK, $sun) or die "cannot connect to socket $cfg{'host'}"; +} +else { + my $proto = getprotobyname('tcp'); + my $sin; + socket (SOCK, PF_INET, SOCK_STREAM, $proto) or die "cannot create tcp socket"; + if (inet_aton ($cfg{'host'})) { + $sin = sockaddr_in ($cfg{'port'}, inet_aton($cfg{'host'})); + } + else { + my $addr = gethostbyname($cfg{'host'}); + if (!$addr) { + die "cannot resolve $cfg{'host'}"; + } + $sin = sockaddr_in ($cfg{'port'}, $addr); + } + + connect (SOCK, $sin) or die "cannot connect to socket $cfg{'host'}:$cfg{'port'}"; +} + +my $input; +while (defined (my $line = <>)) { + $input .= $line; +} + +print "Sending ". length ($input) ." bytes...\n"; + +syswrite SOCK, "$cfg{'command'} RSPAMC/1.0 $CRLF"; +syswrite SOCK, "Content-Length: " . length ($input) . $CRLF . $CRLF; +syswrite SOCK, $input; +syswrite SOCK, $CRLF; +while () { + print $_; +} +close SOCK; diff --git a/src/filter.c b/src/filter.c index 290769c54..443784ad6 100644 --- a/src/filter.c +++ b/src/filter.c @@ -381,6 +381,8 @@ statfiles_callback (gpointer key, gpointer value, void *arg) } weight = st->classifier->classify_func (task->worker->srv->statfile_pool, filename, tokens); + + msg_debug ("process_statfiles: got classify weight: %.2f", weight); if (weight > 0.000001) { if ((w = g_hash_table_lookup (data->metrics, st->metric)) == NULL) { diff --git a/src/fstring.c b/src/fstring.c index cad7c8710..2935fe8e6 100644 --- a/src/fstring.c +++ b/src/fstring.c @@ -188,6 +188,7 @@ fstralloc (memory_pool_t *pool, size_t len) } res->size = len; + res->len = 0; return res; } diff --git a/src/protocol.c b/src/protocol.c index 027e3a91a..0fb7764c0 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -73,7 +73,7 @@ parse_command (struct worker_task *task, char *line) token = strsep (&line, " "); if (line == NULL || token == NULL) { - msg_debug ("parse_command: bad comand: %s", token); + msg_debug ("parse_command: bad command: %s", token); return -1; } @@ -85,7 +85,7 @@ parse_command (struct worker_task *task, char *line) task->cmd = CMD_CHECK; } else { - msg_debug ("parse_command: bad comand: %s", token); + msg_debug ("parse_command: bad command: %s", token); return -1; } break; @@ -99,7 +99,7 @@ parse_command (struct worker_task *task, char *line) task->cmd = CMD_SKIP; } else { - msg_debug ("parse_command: bad comand: %s", token); + msg_debug ("parse_command: bad command: %s", token); return -1; } break; @@ -113,7 +113,7 @@ parse_command (struct worker_task *task, char *line) task->cmd = CMD_PROCESS; } else { - msg_debug ("parse_command: bad comand: %s", token); + msg_debug ("parse_command: bad command: %s", token); return -1; } break; @@ -127,12 +127,12 @@ parse_command (struct worker_task *task, char *line) task->cmd = CMD_REPORT_IFSPAM; } else { - msg_debug ("parse_command: bad comand: %s", token); + msg_debug ("parse_command: bad command: %s", token); return -1; } break; default: - msg_debug ("parse_command: bad comand: %s", token); + msg_debug ("parse_command: bad command: %s", token); return -1; } @@ -187,12 +187,16 @@ parse_header (struct worker_task *task, char *line) case 'C': /* content-length */ if (strncasecmp (headern, CONTENT_LENGTH_HEADER, sizeof (CONTENT_LENGTH_HEADER) - 1) == 0) { - task->content_length = strtoul (line, &err, 10); - task->msg = memory_pool_alloc (task->task_pool, sizeof (f_str_buf_t)); - task->msg->buf = fstralloc (task->task_pool, task->content_length); - if (task->msg->buf == NULL) { - msg_err ("read_socket: cannot allocate memory for message buffer"); - return -1; + if (task->content_length == 0) { + task->content_length = strtoul (line, &err, 10); + task->msg = memory_pool_alloc0 (task->task_pool, sizeof (f_str_buf_t)); + task->msg->buf = fstralloc (task->task_pool, task->content_length); + if (task->msg->buf == NULL) { + msg_err ("read_socket: cannot allocate memory for message buffer"); + return -1; + } + task->msg->pos = task->msg->buf->begin; + update_buf_size (task->msg); } } else { @@ -306,13 +310,13 @@ show_url_header (struct worker_task *task) if (TAILQ_NEXT (url, next) != NULL) { c = *(host.begin + host.len); *(host.begin + host.len) = '\0'; - r += snprintf (outbuf, sizeof (outbuf) - r, "%s, ", host.begin); + r += snprintf (outbuf + r, sizeof (outbuf) - r, "%s, ", host.begin); *(host.begin + host.len) = c; } else { c = *(host.begin + host.len); *(host.begin + host.len) = '\0'; - r += snprintf (outbuf, sizeof (outbuf) - r, "%s" CRLF, host.begin); + r += snprintf (outbuf + r, sizeof (outbuf) - r, "%s" CRLF, host.begin); *(host.begin + host.len) = c; } } diff --git a/src/statfile.c b/src/statfile.c index ca78b79e9..c0a2a2487 100644 --- a/src/statfile.c +++ b/src/statfile.c @@ -295,9 +295,7 @@ statfile_pool_get_block (statfile_pool_t *pool, char *filename, uint32_t h1, uin if (i + blocknum > file->blocks) { break; } - msg_debug ("statfile_pool_get_block: test block with h1=%u, h2=%u, number %u in chain %u", block->hash1, block->hash2, i, blocknum); if (block->hash1 == h1 && block->hash2 == h2) { - msg_debug ("statfile_pool_get_block: found block with h1=%u, h2=%u, number %u in chain %u", h1, h2, i, blocknum); block->last_access = now - (time_t)header->create_time; return block->value; } @@ -305,7 +303,6 @@ statfile_pool_get_block (statfile_pool_t *pool, char *filename, uint32_t h1, uin block = (struct stat_file_block *)c; } - msg_debug ("statfile_pool_get_block: block with h1=%u, h2=%u, not found in chain %u", h1, h2, blocknum); return 0; } diff --git a/src/worker.c b/src/worker.c index bcbea640e..4a6f9acdd 100644 --- a/src/worker.c +++ b/src/worker.c @@ -87,8 +87,7 @@ free_task (struct worker_task *task) } while (!TAILQ_EMPTY (&task->parts)) { part = TAILQ_FIRST (&task->parts); - g_object_unref (part->type); - g_object_unref (part->content); + g_byte_array_free (part->content, FALSE); TAILQ_REMOVE (&task->parts, part, next); } memory_pool_delete (task->task_pool); @@ -222,6 +221,7 @@ read_socket (struct bufferevent *bev, void *arg) r = bufferevent_read (bev, task->msg->pos, task->msg->free); if (r > 0) { task->msg->pos += r; + msg_debug ("read_socket: read %zd bytes from socket, %zd bytes left", r, task->msg->free); update_buf_size (task->msg); if (task->msg->free == 0) { r = process_message (task); @@ -244,9 +244,8 @@ read_socket (struct bufferevent *bev, void *arg) } } else { - msg_err ("read_socket: cannot read data to buffer: %ld", (long int)r); + msg_warn ("read_socket: cannot read data to buffer (free space: %zd): %ld", task->msg->free, (long int)r); bufferevent_disable (bev, EV_READ); - bufferevent_free (bev); free_task (task); } break;