From 5b697ce84fa67e6d237d21001814b8f3a91342d6 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 23 Apr 2012 17:57:02 +0400 Subject: [PATCH] * Allow full access to controller's commands without a password if controller password is not specified in configuration. Fix some minor stuff while I'm here. --- lib/client/librspamdclient.c | 87 +++++++++++++++++++++--------------- src/client/rspamc.c | 13 +----- src/controller.c | 11 +++-- 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/lib/client/librspamdclient.c b/lib/client/librspamdclient.c index 2488abd37..6351175bc 100644 --- a/lib/client/librspamdclient.c +++ b/lib/client/librspamdclient.c @@ -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) { diff --git a/src/client/rspamc.c b/src/client/rspamc.c index d91b8cdf0..7219d35b5 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -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); diff --git a/src/controller.c b/src/controller.c index c24d240c8..d11edbfe6 100644 --- a/src/controller.c +++ b/src/controller.c @@ -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 */ -- 2.39.5