]> source.dussan.org Git - rspamd.git/commitdiff
* Allow full access to controller's commands without a password if
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Mon, 23 Apr 2012 13:57:02 +0000 (17:57 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Mon, 23 Apr 2012 13:57:02 +0000 (17:57 +0400)
 controller password is not specified in configuration.
Fix some minor stuff while I'm here.

lib/client/librspamdclient.c
src/client/rspamc.c
src/controller.c

index 2488abd37de376f6c137a0aebcc53d849a197828..6351175bc3c2050d6716d8fd2289b51150707b83 100644 (file)
@@ -1081,9 +1081,9 @@ static GString *
 rspamd_send_controller_command (struct rspamd_connection *c, const gchar *line, gsize len, gint fd, GError **err)
 {
        GString                        *res = NULL;
-       gchar                           tmpbuf[BUFSIZ];
+       gchar                           tmpbuf[BUFSIZ], *p;
        gint                            r = 0;
-       static const gchar              end_marker[] = "END\r\n";
+       static const gchar              end_marker[] = "\r\nEND\r\n";
 
        /* Set blocking for writing */
        make_socket_blocking (c->socket);
@@ -1114,20 +1114,21 @@ rspamd_send_controller_command (struct rspamd_connection *c, const gchar *line,
                        upstream_fail (&c->server->up, c->connection_time);
                        return NULL;
                }
-               if ((r = read (c->socket, tmpbuf, sizeof (tmpbuf))) > 0) {
+               if ((r = read (c->socket, tmpbuf, sizeof (tmpbuf) - 1)) > 0) {
                        /* Check the end of the buffer for END marker */
+                       tmpbuf[r] = '\0';
                        if (r >= (gint)sizeof (end_marker) - 1 &&
-                                       memcmp (tmpbuf + r - sizeof (end_marker) + 1, end_marker, sizeof (end_marker) - 1) == 0) {
-                               r -= sizeof (end_marker) - 1;
+                                       (p = strstr (tmpbuf, end_marker)) != NULL) {
+                               *p = '\0';
                                /* Copy the rest to the result string */
                                if (res == NULL) {
-                                       res = g_string_new_len (tmpbuf, r);
+                                       res = g_string_new (tmpbuf);
                                        return res;
                                }
                                else {
                                        /* Append data to string */
                                        if (r > 0) {
-                                               res = g_string_append_len (res, tmpbuf, r);
+                                               res = g_string_append (res, tmpbuf);
                                        }
                                        return res;
                                }
@@ -1450,12 +1451,14 @@ rspamd_learn_memory (struct rspamd_client *client, const guchar *message, gsize
                }
                return FALSE;
        }
-       /* Perform auth */
-       if (! rspamd_controller_auth (c, password, err)) {
-               if (*err == NULL) {
-                       *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+       if (password != NULL) {
+               /* Perform auth */
+               if (! rspamd_controller_auth (c, password, err)) {
+                       if (*err == NULL) {
+                               *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+                       }
+                       return FALSE;
                }
-               return FALSE;
        }
 
        r = length + sizeof ("learn %s %uz\r\n") + strlen (symbol) + sizeof ("4294967296");
@@ -1530,12 +1533,14 @@ rspamd_learn_fd (struct rspamd_client *client, int fd, const gchar *symbol, cons
                }
                return FALSE;
        }
-       /* Perform auth */
-       if (! rspamd_controller_auth (c, password, err)) {
-               if (*err == NULL) {
-                       *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+       if (password != NULL) {
+               /* Perform auth */
+               if (! rspamd_controller_auth (c, password, err)) {
+                       if (*err == NULL) {
+                               *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+                       }
+                       return FALSE;
                }
-               return FALSE;
        }
 
        /* Get length */
@@ -1604,12 +1609,14 @@ rspamd_learn_spam_memory (struct rspamd_client *client, const guchar *message, g
                }
                return FALSE;
        }
-       /* Perform auth */
-       if (! rspamd_controller_auth (c, password, err)) {
-               if (*err == NULL) {
-                       *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+       if (password != NULL) {
+               /* Perform auth */
+               if (! rspamd_controller_auth (c, password, err)) {
+                       if (*err == NULL) {
+                               *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+                       }
+                       return FALSE;
                }
-               return FALSE;
        }
 
        r = length + sizeof ("learn_spam %s %uz\r\n") + strlen (classifier) + sizeof ("4294967296");
@@ -1685,12 +1692,14 @@ rspamd_learn_spam_fd (struct rspamd_client *client, int fd, const gchar *classif
                }
                return FALSE;
        }
-       /* Perform auth */
-       if (! rspamd_controller_auth (c, password, err)) {
-               if (*err == NULL) {
-                       *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+       if (password != NULL) {
+               /* Perform auth */
+               if (! rspamd_controller_auth (c, password, err)) {
+                       if (*err == NULL) {
+                               *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+                       }
+                       return FALSE;
                }
-               return FALSE;
        }
 
        /* Get length */
@@ -1761,12 +1770,14 @@ rspamd_fuzzy_memory (struct rspamd_client *client, const guchar *message, gsize
                }
                return FALSE;
        }
-       /* Perform auth */
-       if (! rspamd_controller_auth (c, password, err)) {
-               if (*err == NULL) {
-                       *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+       if (password != NULL) {
+               /* Perform auth */
+               if (! rspamd_controller_auth (c, password, err)) {
+                       if (*err == NULL) {
+                               *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+                       }
+                       return FALSE;
                }
-               return FALSE;
        }
 
        r = length + sizeof ("fuzzy_add %uz %d %d\r\n") + sizeof ("4294967296") * 3;
@@ -1846,12 +1857,14 @@ rspamd_fuzzy_fd (struct rspamd_client *client, int fd, const gchar *password, gi
                }
                return FALSE;
        }
-       /* Perform auth */
-       if (! rspamd_controller_auth (c, password, err)) {
-               if (*err == NULL) {
-                       *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+       if (password != NULL) {
+               /* Perform auth */
+               if (! rspamd_controller_auth (c, password, err)) {
+                       if (*err == NULL) {
+                               *err = g_error_new (G_RSPAMD_ERROR, errno, "Authentication error");
+                       }
+                       return FALSE;
                }
-               return FALSE;
        }
        /* Get length */
        if (fstat (fd, &st) == -1) {
index d91b8cdf0942d5282dcf793be5e1cb17704f8972..7219d35b55c518e7670522308ca709574029f761 100644 (file)
@@ -395,7 +395,7 @@ learn_rspamd_stdin (gboolean is_spam)
        gint                             r = 0, len;
        GError                          *err = NULL;
 
-       if (password == NULL || (statfile == NULL && classifier == NULL)) {
+       if ((statfile == NULL && classifier == NULL)) {
                fprintf (stderr, "cannot learn message without password and symbol/classifier name\n");
                exit (EXIT_FAILURE);
        }
@@ -462,7 +462,7 @@ learn_rspamd_file (gboolean is_spam, const gchar *file)
 {
        GError                          *err = NULL;
 
-       if (password == NULL || (statfile == NULL && classifier == NULL)) {
+       if ((statfile == NULL && classifier == NULL)) {
                fprintf (stderr, "cannot learn message without password and symbol/classifier name\n");
                exit (EXIT_FAILURE);
        }
@@ -514,10 +514,6 @@ fuzzy_rspamd_stdin (gboolean delete)
        gint                             r = 0, len;
        GError                          *err = NULL;
 
-       if (password == NULL) {
-               fprintf (stderr, "cannot learn message without password\n");
-               exit (EXIT_FAILURE);
-       }
        /* Add server */
        add_rspamd_server (TRUE);
 
@@ -559,11 +555,6 @@ fuzzy_rspamd_file (const gchar *file, gboolean delete)
 {
        GError                          *err = NULL;
 
-       if (password == NULL) {
-               fprintf (stderr, "cannot learn message without password\n");
-               exit (EXIT_FAILURE);
-       }
-
        if (!rspamd_fuzzy_file (client, file, password, weight, flag, delete, &err)) {
                if (err != NULL) {
                        fprintf (stderr, "cannot learn message: %s\n", err->message);
index c24d240c82aee57bfbfae49741b59f4e8d59c4e3..d11edbfe6451e5de03c3d21e158308acd849028a 100644 (file)
@@ -194,7 +194,7 @@ free_session (void *ud)
        close (session->sock);
 
        memory_pool_delete (session->session_pool);
-       g_free (session);
+       g_slice_free1 (sizeof (struct controller_session), session);
 }
 
 static gint
@@ -478,7 +478,7 @@ process_command (struct controller_command *cmd, gchar **cmd_args, struct contro
                        return TRUE;
                }
                if (ctx->password == NULL) {
-                       r = rspamd_snprintf (out_buf, sizeof (out_buf), "password command disabled in config, authorized access unallowed" CRLF);
+                       r = rspamd_snprintf (out_buf, sizeof (out_buf), "password command disabled in config, authorized access granted" CRLF);
                        if (! rspamd_dispatcher_write (session->dispatcher, out_buf, r, FALSE, FALSE)) {
                                return FALSE;
                        }
@@ -1196,12 +1196,12 @@ accept_socket (gint fd, short what, void *arg)
                return;
        }
 
-       new_session = g_malloc (sizeof (struct controller_session));
+       new_session = g_slice_alloc0 (sizeof (struct controller_session));
        if (new_session == NULL) {
                msg_err ("cannot allocate memory for task, %s", strerror (errno));
                return;
        }
-       bzero (new_session, sizeof (struct controller_session));
+
        new_session->worker = worker;
        new_session->sock = nfd;
        new_session->cfg = worker->srv->cfg;
@@ -1209,6 +1209,9 @@ accept_socket (gint fd, short what, void *arg)
        new_session->session_pool = memory_pool_new (memory_pool_get_size () - 1);
        new_session->resolver = ctx->resolver;
        new_session->ev_base = ctx->ev_base;
+       if (ctx->password == NULL) {
+               new_session->authorized = TRUE;
+       }
        worker->srv->stat->control_connections_count++;
 
        /* Set up dispatcher */