]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow maps with HTTP auth
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 3 Oct 2018 13:18:52 +0000 (14:18 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 3 Oct 2018 13:18:52 +0000 (14:18 +0100)
src/libutil/map.c
src/libutil/map_private.h

index 9d9287137422ec999465308fc09c061e5c58f3ac..d96cdc2adcabd85eca6f67aa9c8faa0fd25f68a6 100644 (file)
@@ -124,9 +124,23 @@ write_http_request (struct http_callback_data *cbd)
                        g_assert_not_reached ();
                }
 
+               msg->url = rspamd_fstring_append (msg->url, cbd->data->rest,
+                                       strlen (cbd->data->rest));
+
+               if (cbd->data->userinfo) {
+                       rspamd_http_message_add_header (msg, "Authorization",
+                                       cbd->data->userinfo);
+               }
+
                MAP_RETAIN (cbd, "http_callback_data");
-               rspamd_http_connection_write_message (cbd->conn, msg, cbd->data->host,
-                               NULL, cbd, cbd->fd, &cbd->tv, cbd->ev_base);
+               rspamd_http_connection_write_message (cbd->conn,
+                               msg,
+                               cbd->data->host,
+                               NULL,
+                               cbd,
+                               cbd->fd,
+                               &cbd->tv,
+                               cbd->ev_base);
        }
        else {
                msg_err_map ("cannot connect to %s: %s", cbd->data->host,
@@ -2245,6 +2259,11 @@ rspamd_map_backend_dtor (struct rspamd_map_backend *bk)
 
                        g_free (data->host);
                        g_free (data->path);
+                       g_free (data->rest);
+
+                       if (data->userinfo) {
+                               g_free (data->userinfo);
+                       }
 
                        if (data->etag) {
                                rspamd_fstring_free (data->etag);
@@ -2339,7 +2358,7 @@ rspamd_map_parse_backend (struct rspamd_config *cfg, const gchar *map_line)
                        goto err;
                }
                else {
-                       if (!(up.field_set & 1 << UF_HOST)) {
+                       if (!(up.field_set & 1u << UF_HOST)) {
                                msg_err_config ("cannot parse HTTP url: %s: no host", bk->uri);
                                goto err;
                        }
@@ -2348,7 +2367,7 @@ rspamd_map_parse_backend (struct rspamd_config *cfg, const gchar *map_line)
                        tok.len = up.field_data[UF_HOST].len;
                        hdata->host = rspamd_ftokdup (&tok);
 
-                       if (up.field_set & 1 << UF_PORT) {
+                       if (up.field_set & (1u << UF_PORT)) {
                                hdata->port = up.port;
                        }
                        else {
@@ -2360,11 +2379,32 @@ rspamd_map_parse_backend (struct rspamd_config *cfg, const gchar *map_line)
                                }
                        }
 
-                       if (up.field_set & 1 << UF_PATH) {
+                       if (up.field_set & (1u << UF_PATH)) {
                                tok.begin = bk->uri + up.field_data[UF_PATH].off;
-                               tok.len = strlen (tok.begin);
+                               tok.len = up.field_data[UF_PATH].len;
 
                                hdata->path = rspamd_ftokdup (&tok);
+
+                               /* We also need to check query + fragment */
+                               if (up.field_set & ((1u << UF_QUERY) | (1u << UF_FRAGMENT))) {
+                                       tok.begin = bk->uri + up.field_data[UF_PATH].off +
+                                                       up.field_data[UF_PATH].len;
+                                       tok.len = strlen (tok.begin);
+                                       hdata->rest = rspamd_ftokdup (&tok);
+                               }
+                               else {
+                                       hdata->rest = g_strdup ("");
+                               }
+                       }
+
+                       if (up.field_set & (1u << UF_USERINFO)) {
+                               /* Create authorisation header for basic auth */
+                               guint len = sizeof ("Basic ") +
+                                                       up.field_data[UF_USERINFO].len * 8 / 5 + 4;
+                               hdata->userinfo = g_malloc (len);
+                               rspamd_snprintf (hdata->userinfo, len, "Basic %*Bs",
+                                               (int)up.field_data[UF_USERINFO].len,
+                                               bk->uri + up.field_data[UF_USERINFO].off);
                        }
                }
 
index 7e0390a6cc69c1122797508138185271b9f6c82b..68415d0e0c55c4f910dbaf8bef8ccfcf9430005b 100644 (file)
@@ -84,8 +84,10 @@ struct http_map_data {
        struct rspamd_map_cachepoint *cache;
        /* Non-shared for cache owner, used to cleanup cache */
        struct rspamd_http_map_cached_cbdata *cur_cache_cbd;
+       gchar *userinfo;
        gchar *path;
        gchar *host;
+       gchar *rest;
        gchar *last_signature;
        rspamd_fstring_t *etag;
        time_t last_modified;