]> source.dussan.org Git - rspamd.git/commitdiff
* Add simple utility for sending mail to rspamd
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Fri, 16 Jan 2009 14:02:26 +0000 (17:02 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Fri, 16 Jan 2009 14:02:26 +0000 (17:02 +0300)
* Fix some errors in freeing message object

Makefile.in
rspamc.pl [new file with mode: 0755]
src/filter.c
src/fstring.c
src/protocol.c
src/statfile.c
src/worker.c

index fc7078d1a57c06eb6844a6cdb117cbbd4546c1d9..1448d4377a7a248160c58edcd07d9242d90e41b9 100644 (file)
@@ -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 (executable)
index 0000000..ee203b8
--- /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 (<CONF>) {
+    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 (<SOCK>) {
+    print $_;
+}
+close SOCK;
index 290769c54cc6e4fd809fb09af9c92568420ed7a7..443784ad63d8d6c565d71599d6c40652147c4bde 100644 (file)
@@ -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) {
index cad7c87105cb94ba08efead1b9ce4dd575bb0094..2935fe8e6f6461a3cb27e005f7ad03a079c8f6ea 100644 (file)
@@ -188,6 +188,7 @@ fstralloc (memory_pool_t *pool, size_t len)
        }
 
        res->size = len;
+    res->len = 0;
        return res;
 }
 
index 027e3a91acc195be79b4edc7b08030f4900ef290..0fb7764c0e01ff03428474c7deb6409d23d79643 100644 (file)
@@ -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;
                }
        }
index ca78b79e9d499e622c3c95f71e9d4f450fb1bff2..c0a2a248751afa4518a6bbbe7d45224850349d08 100644 (file)
@@ -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;
 }
index bcbea640eac142efeb7575f9e191e90ae0cbda96..4a6f9acdd69e05ae5bc3bc0775943a603e6e1f83 100644 (file)
@@ -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;