From 1a83160af8e7861cd6ce11515e298e7d8a1a4ca3 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 19 Jul 2018 11:05:20 +0100 Subject: [PATCH] [Minor] Support HTTP basic auth in Lua HTTP --- src/lua/lua_http.c | 51 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/lua/lua_http.c b/src/lua/lua_http.c index 2e331f57d..da4cad890 100644 --- a/src/lua/lua_http.c +++ b/src/lua/lua_http.c @@ -69,13 +69,12 @@ struct lua_http_cbdata { rspamd_inet_addr_t *addr; gchar *mime_type; gchar *host; + gchar *auth; const gchar *url; gsize max_size; gint flags; gint fd; gint cbref; - gint bodyref; - gboolean gzip; }; static const int default_http_timeout = 5000; @@ -123,6 +122,10 @@ lua_http_fin (gpointer arg) g_free (cbd->host); } + if (cbd->auth) { + g_free (cbd->auth); + } + if (cbd->local_kp) { rspamd_keypair_unref (cbd->local_kp); } @@ -274,6 +277,11 @@ lua_http_make_connection (struct lua_http_cbdata *cbd) rspamd_http_connection_set_max_size (cbd->conn, cbd->max_size); } + if (cbd->auth) { + rspamd_http_message_add_header (cbd->msg, "Authorization", + cbd->auth); + } + rspamd_http_connection_write_message (cbd->conn, cbd->msg, cbd->host, cbd->mime_type, cbd, fd, &cbd->tv, cbd->ev_base); @@ -381,6 +389,7 @@ lua_http_request (lua_State *L) gdouble timeout = default_http_timeout; gint flags = 0; gchar *mime_type = NULL; + gchar *auth = NULL; gsize max_size = 0; gboolean gzip = FALSE; @@ -616,6 +625,38 @@ lua_http_request (lua_State *L) } lua_pop (L, 1); + + lua_pushstring (L, "user"); + lua_gettable (L, 1); + + if (lua_type (L, -1) == LUA_TSTRING) { + const gchar *user = lua_tostring (L, -1); + + lua_pushstring (L, "password"); + lua_gettable (L, 1); + + if (lua_type (L, -1) == LUA_TSTRING) { + const gchar *password = lua_tostring (L, -1); + gchar *tmpbuf; + gsize tlen; + + tlen = strlen (user) + strlen (password) + 1; + tmpbuf = g_malloc (tlen + 1); + rspamd_snprintf (tmpbuf, tlen + 1, "%s:%s", user, password); + tlen *= 2; + tlen += sizeof ("Basic ") - 1; + auth = g_malloc (tlen + 1); + rspamd_snprintf (auth, tlen + 1, "Basic %Bs", tmpbuf); + g_free (tmpbuf); + } + else { + msg_warn ("HTTP user must have password, disabling auth"); + } + + lua_pop (L, 1); /* password */ + } + + lua_pop (L, 1); /* username */ } else { msg_err ("http request has bad params"); @@ -638,6 +679,7 @@ lua_http_request (lua_State *L) cbd->flags = flags; cbd->max_size = max_size; cbd->url = url; + cbd->auth = auth; if (msg->host) { cbd->host = rspamd_fstring_cstr (msg->host); @@ -653,11 +695,6 @@ lua_http_request (lua_State *L) rspamd_http_message_set_body_from_fstring_steal (msg, body); } - if (gzip) { - cbd->gzip = TRUE; - /* TODO: Add client support for gzip */ - } - if (session) { cbd->session = session; rspamd_session_add_event (session, -- 2.39.5