From 58af43bae578a62f9f67ba842516e9afe78dfb05 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 29 Jan 2015 19:27:20 +0000 Subject: [PATCH] Implement keys manipulation in client. --- src/client/rspamc.c | 5 ++++- src/client/rspamdclient.c | 24 ++++++++++++++++++++++-- src/client/rspamdclient.h | 3 ++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/client/rspamc.c b/src/client/rspamc.c index 5432aeae7..4d137be81 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -57,6 +57,7 @@ static gboolean json = FALSE; static gboolean headers = FALSE; static gboolean raw = FALSE; static gboolean extended_urls = FALSE; +static gchar *key = NULL; static GOptionEntry entries[] = { @@ -106,6 +107,8 @@ static GOptionEntry entries[] = "Maximum count of parallel requests to rspamd", NULL }, { "extended-urls", 0, 0, G_OPTION_ARG_NONE, &extended_urls, "Output urls in extended format", NULL }, + { "key", 0, 0, G_OPTION_ARG_STRING, &key, + "Use specified pubkey to encrypt request", NULL }, { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } }; @@ -823,7 +826,7 @@ rspamc_process_input (struct event_base *ev_base, struct rspamc_command *cmd, port = 0; } - conn = rspamd_client_init (ev_base, connectv[0], port, timeout); + conn = rspamd_client_init (ev_base, connectv[0], port, timeout, key); g_strfreev (connectv); if (conn != NULL) { diff --git a/src/client/rspamdclient.c b/src/client/rspamdclient.c index b66d67f2d..c95f982cf 100644 --- a/src/client/rspamdclient.c +++ b/src/client/rspamdclient.c @@ -39,6 +39,8 @@ struct rspamd_client_request; struct rspamd_client_connection { gint fd; GString *server_name; + GString *key; + gpointer keypair; struct event_base *ev_base; struct timeval timeout; struct rspamd_http_connection *http_conn; @@ -132,7 +134,7 @@ rspamd_client_finish_handler (struct rspamd_http_connection *conn, struct rspamd_client_connection * rspamd_client_init (struct event_base *ev_base, const gchar *name, - guint16 port, gdouble timeout) + guint16 port, gdouble timeout, const gchar *key) { struct rspamd_client_connection *conn; gint fd; @@ -142,7 +144,7 @@ rspamd_client_init (struct event_base *ev_base, const gchar *name, return NULL; } - conn = g_slice_alloc (sizeof (struct rspamd_client_connection)); + conn = g_slice_alloc0 (sizeof (struct rspamd_client_connection)); conn->ev_base = ev_base; conn->fd = fd; conn->req_sent = FALSE; @@ -151,6 +153,14 @@ rspamd_client_init (struct event_base *ev_base, const gchar *name, rspamd_client_finish_handler, 0, RSPAMD_HTTP_CLIENT); + + if (key) { + conn->key = rspamd_http_connection_make_peer_key (key); + if (conn->key) { + conn->keypair = rspamd_http_connection_gen_key (); + rspamd_http_connection_set_key (conn->http_conn, conn->key); + } + } conn->server_name = g_string_new (name); if (port != 0) { rspamd_printf_gstring (conn->server_name, ":%d", (int)port); @@ -178,6 +188,10 @@ rspamd_client_command (struct rspamd_client_connection *conn, req->ud = ud; req->msg = rspamd_http_new_message (HTTP_REQUEST); + if (conn->key) { + req->msg->peer_key = g_string_new (conn->key->str); + } + if (in != NULL) { /* Read input stream */ req->msg->body = g_string_sized_new (BUFSIZ); @@ -233,6 +247,12 @@ rspamd_client_destroy (struct rspamd_client_connection *conn) g_slice_free1 (sizeof (struct rspamd_client_request), conn->req); } close (conn->fd); + if (conn->key) { + g_string_free (conn->key, TRUE); + } + if (conn->keypair) { + rspamd_http_connection_key_destroy (conn->keypair); + } g_string_free (conn->server_name, TRUE); g_slice_free1 (sizeof (struct rspamd_client_connection), conn); } diff --git a/src/client/rspamdclient.h b/src/client/rspamdclient.h index 6a41c35c1..badfaafb2 100644 --- a/src/client/rspamdclient.h +++ b/src/client/rspamdclient.h @@ -58,7 +58,8 @@ struct rspamd_client_connection * rspamd_client_init ( struct event_base *ev_base, const gchar *name, guint16 port, - gdouble timeout); + gdouble timeout, + const gchar *key); /** * -- 2.39.5