2008-05-18 16:44:30 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2008-05-18 16:44:30 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-05-18 16:44:30 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-10-06 13:36:06 +00:00
|
|
|
#include <errno.h>
|
2008-09-25 18:48:09 +00:00
|
|
|
#include <string.h> /* memcpy */
|
2008-09-25 05:03:39 +00:00
|
|
|
|
2014-07-13 21:50:56 +00:00
|
|
|
#include <zlib.h>
|
2008-10-01 15:53:56 +00:00
|
|
|
|
2010-12-24 08:58:41 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/http.h>
|
|
|
|
#include <event2/http_struct.h> /* TODO: eventually remove this */
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto.h" /* tr_ssha1_matches() */
|
|
|
|
#include "crypto-utils.h" /* tr_rand_buffer() */
|
2015-04-11 14:54:01 +00:00
|
|
|
#include "error.h"
|
2010-12-20 02:07:51 +00:00
|
|
|
#include "fdlimit.h"
|
2008-07-16 17:47:20 +00:00
|
|
|
#include "list.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "net.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "platform.h" /* tr_getWebClientDir() */
|
2009-05-14 17:21:07 +00:00
|
|
|
#include "ptrarray.h"
|
2008-09-05 14:31:58 +00:00
|
|
|
#include "rpcimpl.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "rpc-server.h"
|
2010-12-24 08:58:41 +00:00
|
|
|
#include "session.h"
|
2016-09-05 19:06:26 +00:00
|
|
|
#include "session-id.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2008-09-29 04:26:52 +00:00
|
|
|
#include "trevent.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2008-09-25 18:48:09 +00:00
|
|
|
#include "web.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2009-05-08 14:56:11 +00:00
|
|
|
/* session-id is used to make cross-site request forgery attacks difficult.
|
|
|
|
* Don't disable this feature unless you really know what you're doing!
|
|
|
|
* http://en.wikipedia.org/wiki/Cross-site_request_forgery
|
|
|
|
* http://shiflett.org/articles/cross-site-request-forgeries
|
|
|
|
* http://www.webappsec.org/lists/websecurity/archive/2008-04/msg00037.html */
|
|
|
|
#define REQUIRE_SESSION_ID
|
|
|
|
|
2008-06-02 19:57:16 +00:00
|
|
|
#define MY_NAME "RPC Server"
|
2008-08-22 23:04:16 +00:00
|
|
|
#define MY_REALM "Transmission"
|
2008-09-25 05:03:39 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
struct tr_rpc_server
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isEnabled;
|
|
|
|
bool isPasswordEnabled;
|
|
|
|
bool isWhitelistEnabled;
|
2018-01-11 18:00:41 +00:00
|
|
|
bool isHostWhitelistEnabled;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_port port;
|
|
|
|
char* url;
|
2017-01-28 20:18:38 +00:00
|
|
|
struct tr_address bindAddress;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evhttp* httpd;
|
|
|
|
struct event* start_retry_timer;
|
|
|
|
int start_retry_counter;
|
|
|
|
tr_session* session;
|
|
|
|
char* username;
|
|
|
|
char* password;
|
|
|
|
char* whitelistStr;
|
|
|
|
tr_list* whitelist;
|
2018-01-11 18:00:41 +00:00
|
|
|
tr_list* hostWhitelist;
|
2017-08-13 11:53:41 +00:00
|
|
|
int loginattempts;
|
2020-11-14 18:43:42 +00:00
|
|
|
bool isAntiBruteForceEnabled;
|
|
|
|
int antiBruteForceThreshold;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
bool isStreamInitialized;
|
|
|
|
z_stream stream;
|
2008-05-18 16:44:30 +00:00
|
|
|
};
|
|
|
|
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(...) tr_logAddDeepNamed(MY_NAME, __VA_ARGS__)
|
2008-06-05 04:02:46 +00:00
|
|
|
|
2009-05-14 17:21:07 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* get_current_session_id(struct tr_rpc_server* server)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_session_id_get_current(server->session->session_id);
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
|
|
|
|
2008-09-26 15:40:24 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void send_simple_response(struct evhttp_request* req, int code, char const* text)
|
2008-09-26 15:40:24 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* code_text = tr_webGetResponseStr(code);
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* body = evbuffer_new();
|
|
|
|
|
|
|
|
evbuffer_add_printf(body, "<h1>%d: %s</h1>", code, code_text);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (text != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_add_printf(body, "%s", text);
|
|
|
|
}
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evhttp_send_reply(req, code, code_text, body);
|
2008-12-30 20:32:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_free(body);
|
2008-09-26 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2009-05-14 17:21:07 +00:00
|
|
|
struct tr_mimepart
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* headers;
|
|
|
|
size_t headers_len;
|
|
|
|
char* body;
|
|
|
|
size_t body_len;
|
2009-05-14 17:21:07 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_mimepart_free(struct tr_mimepart* p)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(p->body);
|
|
|
|
tr_free(p->headers);
|
|
|
|
tr_free(p);
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void extract_parts_from_multipart(struct evkeyvalq const* headers, struct evbuffer* body, tr_ptrArray* setme_parts)
|
2013-02-02 16:22:21 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* content_type = evhttp_find_header(headers, "Content-Type");
|
|
|
|
char const* in = (char const*)evbuffer_pullup(body, -1);
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t inlen = evbuffer_get_length(body);
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* boundary_key = "boundary=";
|
2017-04-30 16:25:26 +00:00
|
|
|
char const* boundary_key_begin = content_type != NULL ? strstr(content_type, boundary_key) : NULL;
|
|
|
|
char const* boundary_val = boundary_key_begin != NULL ? boundary_key_begin + strlen(boundary_key) : "arglebargle";
|
2017-04-19 12:04:45 +00:00
|
|
|
char* boundary = tr_strdup_printf("--%s", boundary_val);
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const boundary_len = strlen(boundary);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* delim = tr_memmem(in, inlen, boundary, boundary_len);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (delim != NULL)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t part_len;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* part = delim + boundary_len;
|
2009-05-14 17:21:07 +00:00
|
|
|
|
2017-04-30 16:30:03 +00:00
|
|
|
inlen -= part - in;
|
2017-04-19 12:04:45 +00:00
|
|
|
in = part;
|
2009-05-14 17:21:07 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
delim = tr_memmem(in, inlen, boundary, boundary_len);
|
2017-04-30 16:25:26 +00:00
|
|
|
part_len = delim != NULL ? (size_t)(delim - part) : inlen;
|
2009-05-14 17:21:07 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (part_len != 0)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* rnrn = tr_memmem(part, part_len, "\r\n\r\n", 4);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (rnrn != NULL)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_mimepart* p = tr_new(struct tr_mimepart, 1);
|
|
|
|
p->headers_len = (size_t)(rnrn - part);
|
|
|
|
p->headers = tr_strndup(part, p->headers_len);
|
|
|
|
p->body_len = (size_t)((part + part_len) - (rnrn + 4));
|
|
|
|
p->body = tr_strndup(rnrn + 4, p->body_len);
|
|
|
|
tr_ptrArrayAppend(setme_parts, p);
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(boundary);
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void handle_upload(struct evhttp_request* req, struct tr_rpc_server* server)
|
2008-06-30 21:11:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (req->type != EVHTTP_REQ_POST)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
send_simple_response(req, 405, NULL);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int n;
|
|
|
|
bool hasSessionId = false;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto parts = tr_ptrArray{};
|
2013-02-02 16:22:21 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* query = strchr(req->uri, '?');
|
2017-04-30 16:25:26 +00:00
|
|
|
bool const paused = query != NULL && strstr(query + 1, "paused=true") != NULL;
|
2013-02-02 16:22:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
extract_parts_from_multipart(req->input_headers, req->input_buffer, &parts);
|
|
|
|
n = tr_ptrArraySize(&parts);
|
2013-02-02 16:22:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* first look for the session id */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2013-02-02 16:22:21 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* const p = static_cast<struct tr_mimepart const*>(tr_ptrArrayNth(&parts, i));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-03-17 14:37:52 +00:00
|
|
|
if (tr_strcasestr(p->headers, TR_RPC_SESSION_ID_HEADER) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
char const* ours = get_current_session_id(server);
|
|
|
|
size_t const ourlen = strlen(ours);
|
|
|
|
hasSessionId = ourlen <= p->body_len && memcmp(p->body, ours, ourlen) == 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!hasSessionId)
|
2008-09-26 04:41:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int code = 409;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* codetext = tr_webGetResponseStr(code);
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* body = evbuffer_new();
|
|
|
|
evbuffer_add_printf(body, "%s", "{ \"success\": false, \"msg\": \"Bad Session-Id\" }");
|
|
|
|
evhttp_send_reply(req, code, codetext, body);
|
|
|
|
evbuffer_free(body);
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2010-08-21 12:50:13 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* const p = static_cast<struct tr_mimepart const*>(tr_ptrArrayNth(&parts, i));
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t body_len = p->body_len;
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_variant top;
|
|
|
|
tr_variant* args;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant test;
|
|
|
|
bool have_source = false;
|
2020-11-05 22:46:21 +00:00
|
|
|
char const* body = p->body;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (body_len >= 2 && memcmp(&body[body_len - 2], "\r\n", 2) == 0)
|
|
|
|
{
|
|
|
|
body_len -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantInitDict(&top, 2);
|
|
|
|
tr_variantDictAddStr(&top, TR_KEY_method, "torrent-add");
|
|
|
|
args = tr_variantDictAddDict(&top, TR_KEY_arguments, 2);
|
|
|
|
tr_variantDictAddBool(args, TR_KEY_paused, paused);
|
|
|
|
|
|
|
|
if (tr_urlIsValid(body, body_len))
|
|
|
|
{
|
|
|
|
tr_variantDictAddRaw(args, TR_KEY_filename, body, body_len);
|
|
|
|
have_source = true;
|
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (tr_variantFromBenc(&test, body, body_len) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* b64 = static_cast<char*>(tr_base64_encode(body, body_len, NULL));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictAddStr(args, TR_KEY_metainfo, b64);
|
|
|
|
tr_free(b64);
|
|
|
|
have_source = true;
|
|
|
|
}
|
2010-08-21 12:50:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (have_source)
|
|
|
|
{
|
|
|
|
tr_rpc_request_exec_json(server->session, &top, NULL, NULL);
|
|
|
|
}
|
2010-08-21 12:50:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
|
|
|
}
|
2008-09-26 04:41:13 +00:00
|
|
|
}
|
2008-06-30 21:11:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayDestruct(&parts, (PtrArrayForeachFunc)tr_mimepart_free);
|
|
|
|
|
|
|
|
/* send "success" response */
|
|
|
|
{
|
|
|
|
int code = HTTP_OK;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* codetext = tr_webGetResponseStr(code);
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* body = evbuffer_new();
|
|
|
|
evbuffer_add_printf(body, "%s", "{ \"success\": true, \"msg\": \"Torrent Added\" }");
|
|
|
|
evhttp_send_reply(req, code, codetext, body);
|
|
|
|
evbuffer_free(body);
|
|
|
|
}
|
2008-07-16 17:47:20 +00:00
|
|
|
}
|
2008-06-30 21:11:53 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 18:33:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* mimetype_guess(char const* path)
|
2008-08-06 00:24:05 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* suffix;
|
|
|
|
char const* mime_type;
|
2021-08-15 09:41:48 +00:00
|
|
|
} const types[] = {
|
2017-04-19 12:04:45 +00:00
|
|
|
/* these are the ones we need for serving the web client's files... */
|
|
|
|
{ "css", "text/css" },
|
|
|
|
{ "gif", "image/gif" },
|
|
|
|
{ "html", "text/html" },
|
|
|
|
{ "ico", "image/vnd.microsoft.icon" },
|
|
|
|
{ "js", "application/javascript" },
|
2020-10-24 01:04:25 +00:00
|
|
|
{ "png", "image/png" },
|
2021-08-15 09:41:48 +00:00
|
|
|
{ "svg", "image/svg+xml" },
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* dot = strrchr(path, '.');
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; dot != NULL && i < TR_N_ELEMENTS(types); ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (strcmp(dot + 1, types[i].suffix) == 0)
|
|
|
|
{
|
|
|
|
return types[i].mime_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "application/octet-stream";
|
2008-08-06 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static void add_response(
|
|
|
|
struct evhttp_request* req,
|
|
|
|
struct tr_rpc_server* server,
|
|
|
|
struct evbuffer* out,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* content)
|
2008-10-01 15:53:56 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* key = "Accept-Encoding";
|
|
|
|
char const* encoding = evhttp_find_header(req->input_headers, key);
|
2017-04-30 16:25:26 +00:00
|
|
|
bool const do_compress = encoding != NULL && strstr(encoding, "gzip") != NULL;
|
2008-10-03 17:38:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!do_compress)
|
2008-10-08 13:33:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_buffer(out, content);
|
2008-10-01 20:23:57 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-10-08 13:33:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int state;
|
|
|
|
struct evbuffer_iovec iovec[1];
|
|
|
|
void* content_ptr = evbuffer_pullup(content, -1);
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const content_len = evbuffer_get_length(content);
|
2009-06-02 18:21:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!server->isStreamInitialized)
|
2009-07-02 20:20:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int compressionLevel;
|
2010-08-15 23:45:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
server->isStreamInitialized = true;
|
|
|
|
server->stream.zalloc = (alloc_func)Z_NULL;
|
|
|
|
server->stream.zfree = (free_func)Z_NULL;
|
|
|
|
server->stream.opaque = (voidpf)Z_NULL;
|
2010-08-04 20:57:08 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* zlib's manual says: "Add 16 to windowBits to write a simple gzip header
|
|
|
|
* and trailer around the compressed data instead of a zlib wrapper." */
|
2011-01-27 03:53:02 +00:00
|
|
|
#ifdef TR_LIGHTWEIGHT
|
2017-04-19 12:04:45 +00:00
|
|
|
compressionLevel = Z_DEFAULT_COMPRESSION;
|
2010-08-15 23:45:58 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
compressionLevel = Z_BEST_COMPRESSION;
|
2010-08-15 23:45:58 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
deflateInit2(&server->stream, compressionLevel, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
|
2009-07-02 20:20:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
server->stream.next_in = static_cast<Bytef*>(content_ptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
server->stream.avail_in = content_len;
|
2008-10-01 20:23:57 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* allocate space for the raw data and call deflate() just once --
|
2017-04-19 12:04:45 +00:00
|
|
|
* we won't use the deflated data if it's longer than the raw data,
|
2017-04-21 07:40:57 +00:00
|
|
|
* so it's okay to let deflate() run out of output buffer space */
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_reserve_space(out, content_len, iovec, 1);
|
2021-09-12 17:41:49 +00:00
|
|
|
server->stream.next_out = static_cast<Bytef*>(iovec[0].iov_base);
|
2017-04-19 12:04:45 +00:00
|
|
|
server->stream.avail_out = iovec[0].iov_len;
|
|
|
|
state = deflate(&server->stream, Z_FINISH);
|
2008-10-08 13:39:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (state == Z_STREAM_END)
|
2008-10-08 13:39:44 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
iovec[0].iov_len -= server->stream.avail_out;
|
2008-11-01 22:09:16 +00:00
|
|
|
|
2008-11-19 16:12:00 +00:00
|
|
|
#if 0
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "compressed response is %.2f of original (raw==%zu bytes; compressed==%zu)\n",
|
|
|
|
(double)evbuffer_get_length(out) / content_len, content_len, evbuffer_get_length(out));
|
|
|
|
|
2008-11-19 16:12:00 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:30:03 +00:00
|
|
|
evhttp_add_header(req->output_headers, "Content-Encoding", "gzip");
|
2008-10-08 13:33:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-10-08 13:39:44 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(iovec[0].iov_base, content_ptr, content_len);
|
|
|
|
iovec[0].iov_len = content_len;
|
2008-10-08 13:39:44 +00:00
|
|
|
}
|
2008-10-08 13:33:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_commit_space(out, iovec, 1);
|
|
|
|
deflateReset(&server->stream);
|
2008-10-08 13:33:19 +00:00
|
|
|
}
|
2008-10-01 15:53:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void add_time_header(struct evkeyvalq* headers, char const* key, time_t value)
|
2009-05-08 16:41:32 +00:00
|
|
|
{
|
2020-09-11 21:07:45 +00:00
|
|
|
char buf[128];
|
|
|
|
struct tm tm;
|
2017-04-19 12:04:45 +00:00
|
|
|
/* According to RFC 2616 this must follow RFC 1123's date format,
|
|
|
|
so use gmtime instead of localtime... */
|
2020-09-11 21:07:45 +00:00
|
|
|
tr_gmtime_r(&value, &tm);
|
2017-04-19 12:04:45 +00:00
|
|
|
strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", &tm);
|
|
|
|
evhttp_add_header(headers, key, buf);
|
2009-05-08 16:41:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void evbuffer_ref_cleanup_tr_free(void const* data, size_t datalen, void* extra)
|
2011-03-16 17:42:32 +00:00
|
|
|
{
|
2020-08-18 10:36:10 +00:00
|
|
|
TR_UNUSED(data);
|
|
|
|
TR_UNUSED(datalen);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(extra);
|
2011-03-16 17:42:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void serve_file(struct evhttp_request* req, struct tr_rpc_server* server, char const* filename)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (req->type != EVHTTP_REQ_GET)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evhttp_add_header(req->output_headers, "Allow", "GET");
|
|
|
|
send_simple_response(req, 405, NULL);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void* file;
|
|
|
|
size_t file_len;
|
|
|
|
tr_error* error = NULL;
|
2013-02-02 16:22:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
file_len = 0;
|
|
|
|
file = tr_loadFile(filename, &file_len, &error);
|
2013-02-02 16:22:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (file == NULL)
|
2009-05-08 20:52:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tmp = tr_strdup_printf("%s (%s)", filename, error->message);
|
|
|
|
send_simple_response(req, HTTP_NOTFOUND, tmp);
|
|
|
|
tr_free(tmp);
|
|
|
|
tr_error_free(error);
|
2009-05-08 20:52:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* content;
|
|
|
|
struct evbuffer* out;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
content = evbuffer_new();
|
|
|
|
evbuffer_add_reference(content, file, file_len, evbuffer_ref_cleanup_tr_free, file);
|
|
|
|
|
|
|
|
out = evbuffer_new();
|
|
|
|
evhttp_add_header(req->output_headers, "Content-Type", mimetype_guess(filename));
|
|
|
|
add_time_header(req->output_headers, "Date", now);
|
|
|
|
add_time_header(req->output_headers, "Expires", now + (24 * 60 * 60));
|
|
|
|
add_response(req, server, out, content);
|
|
|
|
evhttp_send_reply(req, HTTP_OK, "OK", out);
|
|
|
|
|
|
|
|
evbuffer_free(out);
|
|
|
|
evbuffer_free(content);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2008-07-16 17:47:20 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void handle_web_client(struct evhttp_request* req, struct tr_rpc_server* server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* webClientDir = tr_getWebClientDir(server->session);
|
2008-09-25 05:03:39 +00:00
|
|
|
|
2019-07-13 08:52:44 +00:00
|
|
|
if (tr_str_is_empty(webClientDir))
|
2008-10-07 01:25:29 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
send_simple_response(
|
|
|
|
req,
|
|
|
|
HTTP_NOTFOUND,
|
2017-04-19 12:04:45 +00:00
|
|
|
"<p>Couldn't find Transmission's web interface files!</p>"
|
|
|
|
"<p>Users: to tell Transmission where to look, "
|
|
|
|
"set the TRANSMISSION_WEB_HOME environment "
|
|
|
|
"variable to the folder where the web interface's "
|
|
|
|
"index.html is located.</p>"
|
|
|
|
"<p>Package Builders: to set a custom default at compile time, "
|
|
|
|
"#define PACKAGE_DATA_DIR in libtransmission/platform.c "
|
2017-04-21 07:40:57 +00:00
|
|
|
"or tweak tr_getClutchDir() by hand.</p>");
|
2008-10-07 01:25:29 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-10-07 01:25:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* pch;
|
|
|
|
char* subpath;
|
|
|
|
|
|
|
|
subpath = tr_strdup(req->uri + strlen(server->url) + 4);
|
2008-10-07 01:25:29 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((pch = strchr(subpath, '?')) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*pch = '\0';
|
|
|
|
}
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (strstr(subpath, "..") != NULL)
|
2009-04-10 15:09:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
send_simple_response(req, HTTP_NOTFOUND, "<p>Tsk, tsk.</p>");
|
2009-04-10 15:09:31 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-04-10 15:09:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
char* filename = tr_strdup_printf(
|
|
|
|
"%s%s%s",
|
|
|
|
webClientDir,
|
|
|
|
TR_PATH_DELIMITER_STR,
|
2019-07-13 08:52:44 +00:00
|
|
|
tr_str_is_empty(subpath) ? "index.html" : subpath);
|
2017-04-19 12:04:45 +00:00
|
|
|
serve_file(req, server, filename);
|
|
|
|
tr_free(filename);
|
2009-04-10 15:09:31 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(subpath);
|
2008-10-07 01:25:29 +00:00
|
|
|
}
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
struct rpc_response_data
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evhttp_request* req;
|
|
|
|
struct tr_rpc_server* server;
|
2009-01-18 15:24:26 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void rpc_response_func(tr_session* session, tr_variant* response, void* user_data)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2020-08-18 10:36:10 +00:00
|
|
|
TR_UNUSED(session);
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct rpc_response_data*>(user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* response_buf = tr_variantToBuf(response, TR_VARIANT_FMT_JSON_LEAN);
|
|
|
|
struct evbuffer* buf = evbuffer_new();
|
|
|
|
|
|
|
|
add_response(data->req, data->server, buf, response_buf);
|
|
|
|
evhttp_add_header(data->req->output_headers, "Content-Type", "application/json; charset=UTF-8");
|
|
|
|
evhttp_send_reply(data->req, HTTP_OK, "OK", buf);
|
|
|
|
|
|
|
|
evbuffer_free(buf);
|
|
|
|
evbuffer_free(response_buf);
|
|
|
|
tr_free(data);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void handle_rpc_from_json(struct evhttp_request* req, struct tr_rpc_server* server, char const* json, size_t json_len)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
bool have_content = tr_variantFromJson(&top, json, json_len) == 0;
|
|
|
|
struct rpc_response_data* data;
|
2008-06-05 04:02:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
data = tr_new0(struct rpc_response_data, 1);
|
|
|
|
data->req = req;
|
|
|
|
data->server = server;
|
2009-08-12 14:40:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_rpc_request_exec_json(server->session, have_content ? &top : NULL, rpc_response_func, data);
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (have_content)
|
|
|
|
{
|
|
|
|
tr_variantFree(&top);
|
|
|
|
}
|
2013-02-10 18:33:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void handle_rpc(struct evhttp_request* req, struct tr_rpc_server* server)
|
2013-02-10 18:33:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (req->type == EVHTTP_REQ_POST)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
handle_rpc_from_json(
|
|
|
|
req,
|
|
|
|
server,
|
|
|
|
(char const*)evbuffer_pullup(req->input_buffer, -1),
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_get_length(req->input_buffer));
|
2019-03-17 06:09:08 +00:00
|
|
|
return;
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2019-03-17 06:09:08 +00:00
|
|
|
|
|
|
|
if (req->type == EVHTTP_REQ_GET)
|
2013-02-10 18:33:04 +00:00
|
|
|
{
|
2019-03-17 06:09:08 +00:00
|
|
|
char const* q = strchr(req->uri, '?');
|
|
|
|
|
|
|
|
if (q != NULL)
|
|
|
|
{
|
|
|
|
struct rpc_response_data* data = tr_new0(struct rpc_response_data, 1);
|
|
|
|
data->req = req;
|
|
|
|
data->server = server;
|
|
|
|
tr_rpc_request_exec_uri(server->session, q + 1, TR_BAD_SIZE, rpc_response_func, data);
|
|
|
|
return;
|
|
|
|
}
|
2008-12-30 22:07:39 +00:00
|
|
|
}
|
2019-03-17 06:09:08 +00:00
|
|
|
|
|
|
|
send_simple_response(req, 405, NULL);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool isAddressAllowed(tr_rpc_server const* server, char const* address)
|
2008-09-26 15:40:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!server->isWhitelistEnabled)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_list* l = server->whitelist; l != NULL; l = l->next)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
if (tr_wildmat(address, static_cast<char const*>(l->data)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2008-09-26 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 00:07:52 +00:00
|
|
|
static bool isIPAddressWithOptionalPort(char const* host)
|
2018-01-17 20:32:14 +00:00
|
|
|
{
|
2018-01-25 00:07:52 +00:00
|
|
|
struct sockaddr_storage address;
|
|
|
|
int address_len = sizeof(address);
|
2018-01-17 20:32:14 +00:00
|
|
|
|
2018-01-25 00:07:52 +00:00
|
|
|
/* TODO: move to net.{c,h} */
|
2019-02-10 11:05:16 +00:00
|
|
|
return evutil_parse_sockaddr_port(host, (struct sockaddr*)&address, &address_len) != -1;
|
2018-01-17 20:32:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 18:00:41 +00:00
|
|
|
static bool isHostnameAllowed(tr_rpc_server const* server, struct evhttp_request* req)
|
|
|
|
{
|
|
|
|
/* If password auth is enabled, any hostname is permitted. */
|
|
|
|
if (server->isPasswordEnabled)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If whitelist is disabled, no restrictions. */
|
|
|
|
if (!server->isHostWhitelistEnabled)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const* const host = evhttp_find_header(req->input_headers, "Host");
|
|
|
|
|
|
|
|
/* No host header, invalid request. */
|
|
|
|
if (host == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-25 00:07:52 +00:00
|
|
|
/* IP address is always acceptable. */
|
|
|
|
if (isIPAddressWithOptionalPort(host))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-17 20:32:14 +00:00
|
|
|
|
2018-01-11 18:00:41 +00:00
|
|
|
/* Host header might include the port. */
|
|
|
|
char* const hostname = tr_strndup(host, strcspn(host, ":"));
|
|
|
|
|
2018-01-25 00:07:52 +00:00
|
|
|
/* localhost is always acceptable. */
|
|
|
|
if (strcmp(hostname, "localhost") == 0 || strcmp(hostname, "localhost.") == 0)
|
2018-01-11 18:00:41 +00:00
|
|
|
{
|
|
|
|
tr_free(hostname);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, hostname must be whitelisted. */
|
|
|
|
for (tr_list* l = server->hostWhitelist; l != NULL; l = l->next)
|
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
if (tr_wildmat(hostname, static_cast<char const*>(l->data)))
|
2018-01-11 18:00:41 +00:00
|
|
|
{
|
|
|
|
tr_free(hostname);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(hostname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool test_session_id(struct tr_rpc_server* server, struct evhttp_request* req)
|
2009-05-08 14:56:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* ours = get_current_session_id(server);
|
|
|
|
char const* theirs = evhttp_find_header(req->input_headers, TR_RPC_SESSION_ID_HEADER);
|
|
|
|
bool const success = theirs != NULL && strcmp(theirs, ours) == 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
return success;
|
2009-05-08 14:56:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void handle_request(struct evhttp_request* req, void* arg)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* server = static_cast<struct tr_rpc_server*>(arg);
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (req != NULL && req->evcon != NULL)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* auth;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* user = NULL;
|
|
|
|
char* pass = NULL;
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evhttp_add_header(req->output_headers, "Server", MY_REALM);
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2020-11-14 18:43:42 +00:00
|
|
|
if (server->isAntiBruteForceEnabled && server->loginattempts >= server->antiBruteForceThreshold)
|
2017-08-12 14:36:43 +00:00
|
|
|
{
|
2017-08-13 11:53:41 +00:00
|
|
|
send_simple_response(req, 403, "<p>Too many unsuccessful login attempts. Please restart transmission-daemon.</p>");
|
2017-08-12 14:36:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isAddressAllowed(server, req->remote_host))
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
send_simple_response(
|
|
|
|
req,
|
|
|
|
403,
|
2017-08-12 14:36:43 +00:00
|
|
|
"<p>Unauthorized IP Address.</p>"
|
|
|
|
"<p>Either disable the IP address whitelist or add your address to it.</p>"
|
|
|
|
"<p>If you're editing settings.json, see the 'rpc-whitelist' and 'rpc-whitelist-enabled' entries.</p>"
|
|
|
|
"<p>If you're still using ACLs, use a whitelist instead. See the transmission-daemon manpage for details.</p>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
auth = evhttp_find_header(req->input_headers, "Authorization");
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (auth != NULL && evutil_ascii_strncasecmp(auth, "basic ", 6) == 0)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* p = static_cast<char*>(tr_base64_decode_str(auth + 6, NULL));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (p != NULL)
|
2008-09-26 15:40:24 +00:00
|
|
|
{
|
2019-03-17 06:09:08 +00:00
|
|
|
if ((pass = strchr(p, ':')) != NULL)
|
2015-05-09 08:37:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
user = p;
|
|
|
|
*pass++ = '\0';
|
2015-05-09 08:37:55 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-05-09 08:37:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(p);
|
2015-05-09 08:37:55 +00:00
|
|
|
}
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-15 19:45:46 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (server->isPasswordEnabled &&
|
|
|
|
(pass == NULL || user == NULL || strcmp(server->username, user) != 0 || !tr_ssha1_matches(server->password, pass)))
|
2008-07-16 23:55:49 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evhttp_add_header(req->output_headers, "WWW-Authenticate", "Basic realm=\"" MY_REALM "\"");
|
2020-11-14 18:43:42 +00:00
|
|
|
if (server->isAntiBruteForceEnabled)
|
|
|
|
{
|
|
|
|
server->loginattempts++;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
char* unauthuser = tr_strdup_printf(
|
|
|
|
"<p>Unauthorized User. %d unsuccessful login attempts.</p>",
|
2019-02-10 11:05:16 +00:00
|
|
|
server->loginattempts);
|
2017-08-12 14:36:43 +00:00
|
|
|
send_simple_response(req, 401, unauthuser);
|
2017-08-13 11:53:41 +00:00
|
|
|
tr_free(unauthuser);
|
|
|
|
tr_free(user);
|
2017-08-12 14:36:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-13 11:53:41 +00:00
|
|
|
|
|
|
|
server->loginattempts = 0;
|
2017-08-12 14:36:43 +00:00
|
|
|
|
|
|
|
if (strncmp(req->uri, server->url, strlen(server->url)) != 0)
|
2008-07-16 23:55:49 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* location = tr_strdup_printf("%sweb/", server->url);
|
|
|
|
evhttp_add_header(req->output_headers, "Location", location);
|
|
|
|
send_simple_response(req, HTTP_MOVEPERM, NULL);
|
|
|
|
tr_free(location);
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (strncmp(req->uri + strlen(server->url), "web/", 4) == 0)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
handle_web_client(req, server);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (strcmp(req->uri + strlen(server->url), "upload") == 0)
|
2010-12-31 14:07:27 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
handle_upload(req, server);
|
2010-12-31 14:07:27 +00:00
|
|
|
}
|
2018-01-11 18:00:41 +00:00
|
|
|
else if (!isHostnameAllowed(server, req))
|
|
|
|
{
|
|
|
|
char* const tmp = tr_strdup_printf(
|
|
|
|
"<p>Transmission received your request, but the hostname was unrecognized.</p>"
|
|
|
|
"<p>To fix this, choose one of the following options:"
|
|
|
|
"<ul>"
|
|
|
|
"<li>Enable password authentication, then any hostname is allowed.</li>"
|
|
|
|
"<li>Add the hostname you want to use to the whitelist in settings.</li>"
|
|
|
|
"</ul></p>"
|
|
|
|
"<p>If you're editing settings.json, see the 'rpc-host-whitelist' and 'rpc-host-whitelist-enabled' entries.</p>"
|
|
|
|
"<p>This requirement has been added to help prevent "
|
|
|
|
"<a href=\"https://en.wikipedia.org/wiki/DNS_rebinding\">DNS Rebinding</a> "
|
|
|
|
"attacks.</p>");
|
|
|
|
send_simple_response(req, 421, tmp);
|
|
|
|
tr_free(tmp);
|
|
|
|
}
|
2018-01-25 00:07:52 +00:00
|
|
|
|
2018-01-17 20:34:40 +00:00
|
|
|
#ifdef REQUIRE_SESSION_ID
|
2018-01-25 00:07:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (!test_session_id(server, req))
|
2009-05-08 14:56:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* sessionId = get_current_session_id(server);
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tmp = tr_strdup_printf(
|
|
|
|
"<p>Your request had an invalid session-id header.</p>"
|
|
|
|
"<p>To fix this, follow these steps:"
|
|
|
|
"<ol><li> When reading a response, get its X-Transmission-Session-Id header and remember it"
|
|
|
|
"<li> Add the updated header to your outgoing requests"
|
|
|
|
"<li> When you get this 409 error message, resend your request with the updated header"
|
|
|
|
"</ol></p>"
|
|
|
|
"<p>This requirement has been added to help prevent "
|
2018-01-11 18:00:41 +00:00
|
|
|
"<a href=\"https://en.wikipedia.org/wiki/Cross-site_request_forgery\">CSRF</a> "
|
2017-04-19 12:04:45 +00:00
|
|
|
"attacks.</p>"
|
|
|
|
"<p><code>%s: %s</code></p>",
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_RPC_SESSION_ID_HEADER,
|
|
|
|
sessionId);
|
2017-04-19 12:04:45 +00:00
|
|
|
evhttp_add_header(req->output_headers, TR_RPC_SESSION_ID_HEADER, sessionId);
|
|
|
|
send_simple_response(req, 409, tmp);
|
|
|
|
tr_free(tmp);
|
2009-05-08 14:56:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2009-05-08 14:56:11 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
else if (strncmp(req->uri + strlen(server->url), "rpc", 3) == 0)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
handle_rpc(req, server);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-09-03 19:59:09 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
send_simple_response(req, HTTP_NOTFOUND, req->uri);
|
2008-09-03 19:59:09 +00:00
|
|
|
}
|
2008-07-16 23:55:49 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(user);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 15:28:58 +00:00
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
SERVER_START_RETRY_COUNT = 10,
|
|
|
|
SERVER_START_RETRY_DELAY_STEP = 3,
|
|
|
|
SERVER_START_RETRY_DELAY_INCREMENT = 5,
|
|
|
|
SERVER_START_RETRY_MAX_DELAY = 60
|
2016-01-07 15:28:58 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void startServer(void* vserver);
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void rpc_server_on_start_retry(evutil_socket_t fd, short type, void* context)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2020-08-18 10:36:10 +00:00
|
|
|
TR_UNUSED(fd);
|
|
|
|
TR_UNUSED(type);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
startServer(context);
|
2016-01-07 15:28:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int rpc_server_start_retry(tr_rpc_server* server)
|
2016-01-07 15:28:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int retry_delay = (server->start_retry_counter / SERVER_START_RETRY_DELAY_STEP + 1) * SERVER_START_RETRY_DELAY_INCREMENT;
|
|
|
|
retry_delay = MIN(retry_delay, SERVER_START_RETRY_MAX_DELAY);
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (server->start_retry_timer == NULL)
|
|
|
|
{
|
|
|
|
server->start_retry_timer = evtimer_new(server->session->event_base, rpc_server_on_start_retry, server);
|
|
|
|
}
|
2008-10-01 04:26:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_timerAdd(server->start_retry_timer, retry_delay, 0);
|
|
|
|
++server->start_retry_counter;
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return retry_delay;
|
2016-01-07 15:28:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void rpc_server_start_retry_cancel(tr_rpc_server* server)
|
2016-01-07 15:28:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (server->start_retry_timer != NULL)
|
2008-09-29 03:02:27 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
event_free(server->start_retry_timer);
|
|
|
|
server->start_retry_timer = NULL;
|
2008-09-29 03:02:27 +00:00
|
|
|
}
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
server->start_retry_counter = 0;
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void startServer(void* vserver)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* server = static_cast<tr_rpc_server*>(vserver);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (server->httpd != NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct evhttp* httpd = evhttp_new(server->session->event_base);
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* address = tr_rpcGetBindAddress(server);
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
tr_port const port = server->port;
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (evhttp_bind_socket(httpd, address, port) == -1)
|
2016-01-07 15:28:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evhttp_free(httpd);
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (server->start_retry_counter < SERVER_START_RETRY_COUNT)
|
2016-01-07 15:28:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const retry_delay = rpc_server_start_retry(server);
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedDbg(MY_NAME, "Unable to bind to %s:%d, retrying in %d seconds", address, port, retry_delay);
|
|
|
|
return;
|
2016-01-07 15:28:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddNamedError(
|
|
|
|
MY_NAME,
|
|
|
|
"Unable to bind to %s:%d after %d attempts, giving up",
|
|
|
|
address,
|
|
|
|
port,
|
2017-04-19 12:04:45 +00:00
|
|
|
SERVER_START_RETRY_COUNT);
|
2016-01-07 15:28:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evhttp_set_gencb(httpd, handle_request, server);
|
|
|
|
server->httpd = httpd;
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedDbg(MY_NAME, "Started listening on %s:%d", address, port);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
rpc_server_start_retry_cancel(server);
|
2016-01-07 15:28:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void stopServer(tr_rpc_server* server)
|
2016-01-07 15:28:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
rpc_server_start_retry_cancel(server);
|
|
|
|
|
|
|
|
struct evhttp* httpd = server->httpd;
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (httpd == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* address = tr_rpcGetBindAddress(server);
|
|
|
|
int const port = server->port;
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
server->httpd = NULL;
|
|
|
|
evhttp_free(httpd);
|
2016-01-07 15:28:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedDbg(MY_NAME, "Stopped listening on %s:%d", address, port);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void onEnabledChanged(void* vserver)
|
2008-10-01 04:26:38 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* server = static_cast<tr_rpc_server*>(vserver);
|
2008-10-01 04:26:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!server->isEnabled)
|
|
|
|
{
|
|
|
|
stopServer(server);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startServer(server);
|
|
|
|
}
|
2008-10-01 04:26:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_rpcSetEnabled(tr_rpc_server* server, bool isEnabled)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
server->isEnabled = isEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_runInEventThread(server->session, onEnabledChanged, server);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_rpcIsEnabled(tr_rpc_server const* server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return server->isEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void restartServer(void* vserver)
|
2008-10-01 04:26:38 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* server = static_cast<tr_rpc_server*>(vserver);
|
2008-10-01 04:26:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (server->isEnabled)
|
2008-10-01 04:26:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
stopServer(server);
|
|
|
|
startServer(server);
|
2008-10-01 04:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_rpcSetPort(tr_rpc_server* server, tr_port port)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(server != NULL);
|
2010-01-16 22:46:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (server->port != port)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
server->port = port;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (server->isEnabled)
|
|
|
|
{
|
|
|
|
tr_runInEventThread(server->session, restartServer, server);
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_port tr_rpcGetPort(tr_rpc_server const* server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return server->port;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_rpcSetUrl(tr_rpc_server* server, char const* url)
|
2010-12-12 18:22:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tmp = server->url;
|
|
|
|
server->url = tr_strdup(url);
|
|
|
|
dbgmsg("setting our URL to [%s]", server->url);
|
|
|
|
tr_free(tmp);
|
2010-12-12 18:22:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_rpcGetUrl(tr_rpc_server const* server)
|
2010-12-12 18:22:11 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return server->url != NULL ? server->url : "";
|
2010-12-12 18:22:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 18:00:41 +00:00
|
|
|
static void tr_rpcSetList(char const* whitelistStr, tr_list** list)
|
2008-12-21 19:23:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tmp;
|
2013-02-02 16:22:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* clear out the old whitelist entries */
|
2018-01-11 18:00:41 +00:00
|
|
|
while ((tmp = tr_list_pop_front(list)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(tmp);
|
|
|
|
}
|
2008-12-21 19:23:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* build the new whitelist entries */
|
2019-07-13 08:52:44 +00:00
|
|
|
for (char const* walk = whitelistStr; !tr_str_is_empty(walk);)
|
2013-02-02 16:22:21 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* delimiters = " ,;";
|
|
|
|
size_t const len = strcspn(walk, delimiters);
|
2017-04-19 12:04:45 +00:00
|
|
|
char* token = tr_strndup(walk, len);
|
2017-05-16 18:37:00 +00:00
|
|
|
|
2018-01-11 18:00:41 +00:00
|
|
|
tr_list_append(list, token);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (strcspn(token, "+-") < len)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddNamedInfo(
|
|
|
|
MY_NAME,
|
|
|
|
"Adding address to whitelist: %s (And it has a '+' or '-'! Are you using an old ACL by mistake?)",
|
|
|
|
token);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_logAddNamedInfo(MY_NAME, "Adding address to whitelist: %s", token);
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:37:00 +00:00
|
|
|
walk += len;
|
|
|
|
|
|
|
|
if (*walk == '\0')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-16 18:37:00 +00:00
|
|
|
++walk;
|
2008-12-21 19:23:41 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 18:00:41 +00:00
|
|
|
void tr_rpcSetHostWhitelist(tr_rpc_server* server, char const* whitelistStr)
|
|
|
|
{
|
|
|
|
tr_rpcSetList(whitelistStr, &server->hostWhitelist);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_rpcSetWhitelist(tr_rpc_server* server, char const* whitelistStr)
|
|
|
|
{
|
|
|
|
/* keep the string */
|
|
|
|
char* const tmp = server->whitelistStr;
|
|
|
|
server->whitelistStr = tr_strdup(whitelistStr);
|
|
|
|
tr_free(tmp);
|
|
|
|
|
|
|
|
tr_rpcSetList(whitelistStr, &server->whitelist);
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_rpcGetWhitelist(tr_rpc_server const* server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return server->whitelistStr != NULL ? server->whitelistStr : "";
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_rpcSetWhitelistEnabled(tr_rpc_server* server, bool isEnabled)
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
server->isWhitelistEnabled = isEnabled;
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_rpcGetWhitelistEnabled(tr_rpc_server const* server)
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return server->isWhitelistEnabled;
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 18:00:41 +00:00
|
|
|
void tr_rpcSetHostWhitelistEnabled(tr_rpc_server* server, bool isEnabled)
|
|
|
|
{
|
|
|
|
server->isHostWhitelistEnabled = isEnabled;
|
|
|
|
}
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
/****
|
|
|
|
***** PASSWORD
|
|
|
|
****/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_rpcSetUsername(tr_rpc_server* server, char const* username)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tmp = server->username;
|
|
|
|
server->username = tr_strdup(username);
|
|
|
|
dbgmsg("setting our Username to [%s]", server->username);
|
|
|
|
tr_free(tmp);
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_rpcGetUsername(tr_rpc_server const* server)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return server->username != NULL ? server->username : "";
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_rpcSetPassword(tr_rpc_server* server, char const* password)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(server->password);
|
|
|
|
|
|
|
|
if (*password != '{')
|
|
|
|
{
|
|
|
|
server->password = tr_ssha1(password);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
server->password = strdup(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbgmsg("setting our Password to [%s]", server->password);
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_rpcGetPassword(tr_rpc_server const* server)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return server->password != NULL ? server->password : "";
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_rpcSetPasswordEnabled(tr_rpc_server* server, bool isEnabled)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
server->isPasswordEnabled = isEnabled;
|
|
|
|
dbgmsg("setting 'password enabled' to %d", (int)isEnabled);
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_rpcIsPasswordEnabled(tr_rpc_server const* server)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return server->isPasswordEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_rpcGetBindAddress(tr_rpc_server const* server)
|
2009-04-15 21:05:58 +00:00
|
|
|
{
|
2017-01-28 20:18:38 +00:00
|
|
|
return tr_address_to_string(&server->bindAddress);
|
2009-04-15 21:05:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 18:43:42 +00:00
|
|
|
bool tr_rpcGetAntiBruteForceEnabled(tr_rpc_server const* server)
|
|
|
|
{
|
|
|
|
return server->isAntiBruteForceEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_rpcSetAntiBruteForceEnabled(tr_rpc_server* server, bool isEnabled)
|
|
|
|
{
|
|
|
|
server->isAntiBruteForceEnabled = isEnabled;
|
|
|
|
if (!isEnabled)
|
|
|
|
{
|
|
|
|
server->loginattempts = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int tr_rpcGetAntiBruteForceThreshold(tr_rpc_server const* server)
|
|
|
|
{
|
|
|
|
return server->antiBruteForceThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_rpcSetAntiBruteForceThreshold(tr_rpc_server* server, int badRequests)
|
|
|
|
{
|
|
|
|
server->antiBruteForceThreshold = badRequests;
|
|
|
|
}
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
/****
|
|
|
|
***** LIFE CYCLE
|
|
|
|
****/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void closeServer(void* vserver)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tmp;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* server = static_cast<tr_rpc_server*>(vserver);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
stopServer(server);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
while ((tmp = tr_list_pop_front(&server->whitelist)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(tmp);
|
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (server->isStreamInitialized)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
deflateEnd(&server->stream);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_free(server->url);
|
|
|
|
tr_free(server->whitelistStr);
|
|
|
|
tr_free(server->username);
|
|
|
|
tr_free(server->password);
|
|
|
|
tr_free(server);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_rpcClose(tr_rpc_server** ps)
|
2008-10-01 04:26:38 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_runInEventThread((*ps)->session, closeServer, *ps);
|
|
|
|
*ps = NULL;
|
2008-10-01 04:26:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void missing_settings_key(tr_quark const q)
|
2012-12-22 20:35:19 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str = tr_quark_get_string(q, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedError(MY_NAME, _("Couldn't find settings key \"%s\""), str);
|
2015-01-02 11:15:31 +00:00
|
|
|
}
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_rpc_server* tr_rpcInit(tr_session* session, tr_variant* settings)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool boolVal;
|
|
|
|
int64_t i;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_quark key;
|
|
|
|
tr_address address;
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
tr_rpc_server* s = tr_new0(tr_rpc_server, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
s->session = session;
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_enabled;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindBool(settings, key, &boolVal))
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s->isEnabled = boolVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_port;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindInt(settings, key, &i))
|
2013-02-02 16:22:21 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
missing_settings_key(key);
|
2013-02-02 16:22:21 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
s->port = (tr_port)i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_url;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindStr(settings, key, &str, NULL))
|
2013-02-02 16:22:21 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
missing_settings_key(key);
|
2013-02-02 16:22:21 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-02-02 16:22:21 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s->url = tr_strdup(str);
|
2009-04-15 21:05:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
key = TR_KEY_rpc_whitelist_enabled;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindBool(settings, key, &boolVal))
|
2008-12-16 16:13:21 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetWhitelistEnabled(s, boolVal);
|
|
|
|
}
|
2008-12-16 16:13:21 +00:00
|
|
|
|
2018-01-11 18:00:41 +00:00
|
|
|
key = TR_KEY_rpc_host_whitelist_enabled;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindBool(settings, key, &boolVal))
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetHostWhitelistEnabled(s, boolVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_host_whitelist;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindStr(settings, key, &str, NULL) && str != NULL)
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetHostWhitelist(s, str);
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
key = TR_KEY_rpc_authentication_required;
|
2008-12-16 16:13:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictFindBool(settings, key, &boolVal))
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetPasswordEnabled(s, boolVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_whitelist;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (!tr_variantDictFindStr(settings, key, &str, NULL) && str != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetWhitelist(s, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_username;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindStr(settings, key, &str, NULL))
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetUsername(s, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_password;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindStr(settings, key, &str, NULL))
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetPassword(s, str);
|
|
|
|
}
|
|
|
|
|
2020-11-14 18:43:42 +00:00
|
|
|
key = TR_KEY_anti_brute_force_enabled;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindBool(settings, key, &boolVal))
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetAntiBruteForceEnabled(s, boolVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = TR_KEY_anti_brute_force_threshold;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindInt(settings, key, &i))
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_rpcSetAntiBruteForceThreshold(s, i);
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
key = TR_KEY_rpc_bind_address;
|
|
|
|
|
|
|
|
if (!tr_variantDictFindStr(settings, key, &str, NULL))
|
|
|
|
{
|
|
|
|
missing_settings_key(key);
|
|
|
|
address = tr_inaddr_any;
|
|
|
|
}
|
|
|
|
else if (!tr_address_from_string(&address, str))
|
|
|
|
{
|
|
|
|
tr_logAddNamedError(MY_NAME, _("%s is not a valid address"), str);
|
|
|
|
address = tr_inaddr_any;
|
|
|
|
}
|
2017-01-28 20:18:38 +00:00
|
|
|
else if (address.type != TR_AF_INET && address.type != TR_AF_INET6)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-01-28 20:18:38 +00:00
|
|
|
tr_logAddNamedError(MY_NAME, _("%s is not an IPv4 or IPv6 address. RPC listeners must be IPv4 or IPv6"), str);
|
2017-04-19 12:04:45 +00:00
|
|
|
address = tr_inaddr_any;
|
|
|
|
}
|
|
|
|
|
2017-01-28 20:18:38 +00:00
|
|
|
s->bindAddress = address;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (s->isEnabled)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddNamedInfo(
|
|
|
|
MY_NAME,
|
|
|
|
_("Serving RPC and Web requests on %s:%d%s"),
|
|
|
|
tr_rpcGetBindAddress(s),
|
|
|
|
(int)s->port,
|
2019-02-10 11:05:16 +00:00
|
|
|
s->url);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_runInEventThread(session, startServer, s);
|
|
|
|
|
|
|
|
if (s->isWhitelistEnabled)
|
|
|
|
{
|
|
|
|
tr_logAddNamedInfo(MY_NAME, "%s", _("Whitelist enabled"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->isPasswordEnabled)
|
|
|
|
{
|
|
|
|
tr_logAddNamedInfo(MY_NAME, "%s", _("Password required"));
|
|
|
|
}
|
2008-12-16 16:13:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 01:04:25 +00:00
|
|
|
char const* webClientDir = tr_getWebClientDir(s->session);
|
|
|
|
if (!tr_str_is_empty(webClientDir))
|
|
|
|
{
|
|
|
|
tr_logAddNamedInfo(MY_NAME, _("Serving RPC and Web requests from directory '%s'"), webClientDir);
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return s;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|