diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-10-03 14:18:52 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-10-03 14:18:52 +0100 |
commit | b0e57e8f54ee6a8d3a723921452b5cc8ef1e97aa (patch) | |
tree | cd029db4fac47d518fca5ecf5324e1b4519b60d0 | |
parent | be7dfc2c7710904e3b87226b624e34e00950b27d (diff) | |
download | rspamd-b0e57e8f54ee6a8d3a723921452b5cc8ef1e97aa.tar.gz rspamd-b0e57e8f54ee6a8d3a723921452b5cc8ef1e97aa.zip |
[Feature] Allow maps with HTTP auth
-rw-r--r-- | src/libutil/map.c | 52 | ||||
-rw-r--r-- | src/libutil/map_private.h | 2 |
2 files changed, 48 insertions, 6 deletions
diff --git a/src/libutil/map.c b/src/libutil/map.c index 9d9287137..d96cdc2ad 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -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); } } diff --git a/src/libutil/map_private.h b/src/libutil/map_private.h index 7e0390a6c..68415d0e0 100644 --- a/src/libutil/map_private.h +++ b/src/libutil/map_private.h @@ -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; |