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-05-28 17:17:12 +00:00
|
|
|
* $Id$
|
2008-05-18 16:44:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
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"
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto.h" /* tr_ssha1_matches () */
|
|
|
|
#include "crypto-utils.h" /* tr_rand_buffer () */
|
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"
|
2012-12-05 17:29:46 +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"
|
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"
|
2012-12-05 17:29:46 +00:00
|
|
|
#define TR_N_ELEMENTS(ary) (sizeof (ary) / sizeof (*ary))
|
2008-09-25 05:03:39 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
struct tr_rpc_server
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool isEnabled;
|
|
|
|
bool isPasswordEnabled;
|
|
|
|
bool isWhitelistEnabled;
|
2008-12-01 20:51:01 +00:00
|
|
|
tr_port port;
|
2013-02-02 16:22:21 +00:00
|
|
|
char * url;
|
2009-04-15 21:05:58 +00:00
|
|
|
struct in_addr bindAddress;
|
2013-02-02 16:22:21 +00:00
|
|
|
struct evhttp * httpd;
|
|
|
|
tr_session * session;
|
|
|
|
char * username;
|
|
|
|
char * password;
|
|
|
|
char * whitelistStr;
|
|
|
|
tr_list * whitelist;
|
|
|
|
|
|
|
|
char * sessionId;
|
2009-05-08 14:56:11 +00:00
|
|
|
time_t sessionIdExpiresAt;
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool isStreamInitialized;
|
2008-12-30 20:40:48 +00:00
|
|
|
z_stream stream;
|
2008-05-18 16:44:30 +00:00
|
|
|
};
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(...) \
|
2013-02-02 16:22:21 +00:00
|
|
|
do { \
|
|
|
|
if (tr_logGetDeepEnabled ()) \
|
|
|
|
tr_logAddDeep (__FILE__, __LINE__, MY_NAME, __VA_ARGS__); \
|
|
|
|
} while (0)
|
2008-10-26 15:39:04 +00:00
|
|
|
|
2008-06-05 04:02:46 +00:00
|
|
|
|
2009-05-14 17:21:07 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static char*
|
2012-12-05 17:29:46 +00:00
|
|
|
get_current_session_id (struct tr_rpc_server * server)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
const time_t now = tr_time ();
|
2009-05-14 17:21:07 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!server->sessionId || (now >= server->sessionIdExpiresAt))
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
int i;
|
|
|
|
const int n = 48;
|
|
|
|
const char * pool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
const size_t pool_size = strlen (pool);
|
|
|
|
unsigned char * buf = tr_new (unsigned char, n+1);
|
|
|
|
|
2014-12-04 11:27:38 +00:00
|
|
|
tr_rand_buffer (buf, n);
|
2013-02-02 16:22:21 +00:00
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
buf[i] = pool[ buf[i] % pool_size ];
|
|
|
|
buf[n] = '\0';
|
|
|
|
|
|
|
|
tr_free (server->sessionId);
|
|
|
|
server->sessionId = (char*) buf;
|
|
|
|
server->sessionIdExpiresAt = now + (60*60); /* expire in an hour */
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->sessionId;
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-26 15:40:24 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
send_simple_response (struct evhttp_request * req,
|
2008-09-26 15:40:24 +00:00
|
|
|
int code,
|
2013-02-02 16:22:21 +00:00
|
|
|
const char * text)
|
2008-09-26 15:40:24 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
const char * code_text = tr_webGetResponseStr (code);
|
|
|
|
struct evbuffer * body = evbuffer_new ();
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
evbuffer_add_printf (body, "<h1>%d: %s</h1>", code, code_text);
|
|
|
|
if (text)
|
|
|
|
evbuffer_add_printf (body, "%s", text);
|
|
|
|
evhttp_send_reply (req, code, code_text, body);
|
2008-12-30 20:32:00 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
evbuffer_free (body);
|
2008-09-26 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2009-05-14 17:21:07 +00:00
|
|
|
struct tr_mimepart
|
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
char * headers;
|
|
|
|
int headers_len;
|
|
|
|
char * body;
|
|
|
|
int body_len;
|
2009-05-14 17:21:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_mimepart_free (struct tr_mimepart * p)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_free (p->body);
|
|
|
|
tr_free (p->headers);
|
|
|
|
tr_free (p);
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-02 16:22:21 +00:00
|
|
|
extract_parts_from_multipart (const struct evkeyvalq * headers,
|
|
|
|
struct evbuffer * body,
|
|
|
|
tr_ptrArray * setme_parts)
|
|
|
|
{
|
|
|
|
const char * content_type = evhttp_find_header (headers, "Content-Type");
|
|
|
|
const char * in = (const char*) evbuffer_pullup (body, -1);
|
|
|
|
size_t inlen = evbuffer_get_length (body);
|
|
|
|
|
|
|
|
const char * boundary_key = "boundary=";
|
|
|
|
const char * boundary_key_begin = content_type ? strstr (content_type, boundary_key) : NULL;
|
|
|
|
const char * boundary_val = boundary_key_begin ? boundary_key_begin + strlen (boundary_key) : "arglebargle";
|
|
|
|
char * boundary = tr_strdup_printf ("--%s", boundary_val);
|
|
|
|
const size_t boundary_len = strlen (boundary);
|
|
|
|
|
|
|
|
const char * delim = tr_memmem (in, inlen, boundary, boundary_len);
|
|
|
|
while (delim)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
size_t part_len;
|
|
|
|
const char * part = delim + boundary_len;
|
2009-05-14 17:21:07 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
inlen -= (part - in);
|
|
|
|
in = part;
|
2009-05-14 17:21:07 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
delim = tr_memmem (in, inlen, boundary, boundary_len);
|
|
|
|
part_len = delim ? (size_t)(delim - part) : inlen;
|
2009-05-14 17:21:07 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (part_len)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
const char * rnrn = tr_memmem (part, part_len, "\r\n\r\n", 4);
|
|
|
|
if (rnrn)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
struct tr_mimepart * p = tr_new (struct tr_mimepart, 1);
|
|
|
|
p->headers_len = rnrn - part;
|
|
|
|
p->headers = tr_strndup (part, p->headers_len);
|
|
|
|
p->body_len = (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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_free (boundary);
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
|
|
|
|
2008-06-30 21:11:53 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
handle_upload (struct evhttp_request * req,
|
2013-02-02 16:22:21 +00:00
|
|
|
struct tr_rpc_server * server)
|
2008-06-30 21:11:53 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
if (req->type != EVHTTP_REQ_POST)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
send_simple_response (req, 405, NULL);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
bool hasSessionId = false;
|
|
|
|
tr_ptrArray parts = TR_PTR_ARRAY_INIT;
|
|
|
|
|
|
|
|
const char * query = strchr (req->uri, '?');
|
|
|
|
const bool paused = query && strstr (query + 1, "paused=true");
|
|
|
|
|
|
|
|
extract_parts_from_multipart (req->input_headers, req->input_buffer, &parts);
|
|
|
|
n = tr_ptrArraySize (&parts);
|
|
|
|
|
|
|
|
/* first look for the session id */
|
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
{
|
|
|
|
struct tr_mimepart * p = tr_ptrArrayNth (&parts, i);
|
|
|
|
if (tr_memmem (p->headers, p->headers_len, TR_RPC_SESSION_ID_HEADER, strlen (TR_RPC_SESSION_ID_HEADER)))
|
|
|
|
break;
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
|
|
|
|
if (i<n)
|
|
|
|
{
|
|
|
|
const struct tr_mimepart * p = tr_ptrArrayNth (&parts, i);
|
|
|
|
const char * ours = get_current_session_id (server);
|
|
|
|
const int ourlen = strlen (ours);
|
|
|
|
hasSessionId = ourlen<=p->body_len && !memcmp (p->body, ours, ourlen);
|
2009-05-14 17:21:07 +00:00
|
|
|
}
|
2008-09-25 05:03:39 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!hasSessionId)
|
2008-09-26 04:41:13 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
int code = 409;
|
|
|
|
const char * codetext = tr_webGetResponseStr (code);
|
|
|
|
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
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else for (i=0; i<n; ++i)
|
2009-05-14 17:21:07 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
struct tr_mimepart * p = tr_ptrArrayNth (&parts, i);
|
|
|
|
int body_len = p->body_len;
|
|
|
|
tr_variant top, *args;
|
|
|
|
tr_variant test;
|
|
|
|
bool have_source = false;
|
|
|
|
char * body = p->body;
|
|
|
|
|
|
|
|
if (body_len >= 2 && !memcmp (&body[body_len - 2], "\r\n", 2))
|
|
|
|
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))
|
2010-08-21 12:50:13 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_variantDictAddRaw (args, TR_KEY_filename, body, body_len);
|
|
|
|
have_source = true;
|
2010-08-21 12:50:13 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else if (!tr_variantFromBenc (&test, body, body_len))
|
2010-08-21 12:50:13 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
char * b64 = tr_base64_encode (body, body_len, NULL);
|
|
|
|
tr_variantDictAddStr (args, TR_KEY_metainfo, b64);
|
|
|
|
tr_free (b64);
|
|
|
|
have_source = true;
|
2010-08-21 12:50:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (have_source)
|
2010-08-21 12:50:13 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
struct evbuffer * json = tr_variantToBuf (&top, TR_VARIANT_FMT_JSON);
|
|
|
|
tr_rpc_request_exec_json (server->session,
|
|
|
|
evbuffer_pullup (json, -1),
|
|
|
|
evbuffer_get_length (json),
|
|
|
|
NULL, NULL);
|
|
|
|
evbuffer_free (json);
|
2008-06-30 21:11:53 +00:00
|
|
|
}
|
2010-08-21 12:50:13 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_variantFree (&top);
|
2008-09-26 04:41:13 +00:00
|
|
|
}
|
2008-06-30 21:11:53 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_ptrArrayDestruct (&parts, (PtrArrayForeachFunc)tr_mimepart_free);
|
|
|
|
|
|
|
|
/* send "success" response */
|
|
|
|
{
|
|
|
|
int code = HTTP_OK;
|
|
|
|
const char * codetext = tr_webGetResponseStr (code);
|
|
|
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
static const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
mimetype_guess (const char * path)
|
2008-08-06 00:24:05 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
const char * suffix;
|
|
|
|
const char * mime_type;
|
|
|
|
} types[] = {
|
|
|
|
/* 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" },
|
|
|
|
{ "png", "image/png" }
|
|
|
|
};
|
|
|
|
const char * dot = strrchr (path, '.');
|
|
|
|
|
|
|
|
for (i = 0; dot && i < TR_N_ELEMENTS (types); ++i)
|
|
|
|
if (!strcmp (dot + 1, types[i].suffix))
|
|
|
|
return types[i].mime_type;
|
|
|
|
|
|
|
|
return "application/octet-stream";
|
2008-08-06 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
2008-10-08 13:33:19 +00:00
|
|
|
static void
|
2013-02-02 16:22:21 +00:00
|
|
|
add_response (struct evhttp_request * req,
|
|
|
|
struct tr_rpc_server * server,
|
|
|
|
struct evbuffer * out,
|
|
|
|
struct evbuffer * content)
|
2008-10-01 15:53:56 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
const char * key = "Accept-Encoding";
|
|
|
|
const char * encoding = evhttp_find_header (req->input_headers, key);
|
|
|
|
const int do_compress = encoding && strstr (encoding, "gzip");
|
2008-10-03 17:38:14 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!do_compress)
|
2008-10-08 13:33:19 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
evbuffer_add_buffer (out, content);
|
2008-10-01 20:23:57 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else
|
2008-10-08 13:33:19 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
int state;
|
|
|
|
struct evbuffer_iovec iovec[1];
|
|
|
|
void * content_ptr = evbuffer_pullup (content, -1);
|
|
|
|
const size_t content_len = evbuffer_get_length (content);
|
2009-06-02 18:21:23 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!server->isStreamInitialized)
|
2009-07-02 20:20:00 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
int compressionLevel;
|
2010-08-15 23:45:58 +00:00
|
|
|
|
2013-02-02 16:22:21 +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
|
|
|
|
2013-02-02 16:22:21 +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
|
2013-02-02 16:22:21 +00:00
|
|
|
compressionLevel = Z_DEFAULT_COMPRESSION;
|
2010-08-15 23:45:58 +00:00
|
|
|
#else
|
2013-02-02 16:22:21 +00:00
|
|
|
compressionLevel = Z_BEST_COMPRESSION;
|
2010-08-15 23:45:58 +00:00
|
|
|
#endif
|
2013-02-02 16:22:21 +00:00
|
|
|
deflateInit2 (&server->stream, compressionLevel, Z_DEFLATED, 15+16, 8, Z_DEFAULT_STRATEGY);
|
2009-07-02 20:20:00 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
server->stream.next_in = content_ptr;
|
|
|
|
server->stream.avail_in = content_len;
|
2008-10-01 20:23:57 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
/* allocate space for the raw data and call deflate () just once --
|
|
|
|
* we won't use the deflated data if it's longer than the raw data,
|
|
|
|
* so it's okay to let deflate () run out of output buffer space */
|
|
|
|
evbuffer_reserve_space (out, content_len, iovec, 1);
|
|
|
|
server->stream.next_out = iovec[0].iov_base;
|
|
|
|
server->stream.avail_out = iovec[0].iov_len;
|
|
|
|
state = deflate (&server->stream, Z_FINISH);
|
2008-10-08 13:39:44 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (state == Z_STREAM_END)
|
2008-10-08 13:39:44 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +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
|
2013-09-08 17:32:09 +00:00
|
|
|
fprintf (stderr, "compressed response is %.2f of original (raw==%"TR_PRIuSIZE" bytes; compressed==%"TR_PRIuSIZE")\n",
|
2013-02-02 16:22:21 +00:00
|
|
|
(double)evbuffer_get_length (out)/content_len,
|
|
|
|
content_len, evbuffer_get_length (out));
|
2008-11-19 16:12:00 +00:00
|
|
|
#endif
|
2013-02-02 16:22:21 +00:00
|
|
|
evhttp_add_header (req->output_headers,
|
|
|
|
"Content-Encoding", "gzip");
|
2008-10-08 13:33:19 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else
|
2008-10-08 13:39:44 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +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
|
|
|
|
2013-02-02 16:22:21 +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
|
|
|
}
|
|
|
|
|
2009-05-08 16:41:32 +00:00
|
|
|
static void
|
2013-02-02 16:22:21 +00:00
|
|
|
add_time_header (struct evkeyvalq * headers,
|
|
|
|
const char * key,
|
|
|
|
time_t value)
|
2009-05-08 16:41:32 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
/* According to RFC 2616 this must follow RFC 1123's date format,
|
|
|
|
so use gmtime instead of localtime... */
|
|
|
|
char buf[128];
|
|
|
|
struct tm tm = *gmtime (&value);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-03-16 17:42:32 +00:00
|
|
|
static void
|
2013-02-02 16:22:21 +00:00
|
|
|
evbuffer_ref_cleanup_tr_free (const void * data UNUSED,
|
|
|
|
size_t datalen UNUSED,
|
|
|
|
void * extra)
|
2011-03-16 17:42:32 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_free (extra);
|
2011-03-16 17:42:32 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
2013-02-02 16:22:21 +00:00
|
|
|
serve_file (struct evhttp_request * req,
|
|
|
|
struct tr_rpc_server * server,
|
|
|
|
const char * filename)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
if (req->type != EVHTTP_REQ_GET)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
evhttp_add_header (req->output_headers, "Allow", "GET");
|
|
|
|
send_simple_response (req, 405, NULL);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
void * file;
|
|
|
|
size_t file_len;
|
|
|
|
struct evbuffer * content;
|
|
|
|
const int error = errno;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
file_len = 0;
|
|
|
|
file = tr_loadFile (filename, &file_len);
|
|
|
|
content = evbuffer_new ();
|
|
|
|
evbuffer_add_reference (content, file, file_len, evbuffer_ref_cleanup_tr_free, file);
|
|
|
|
|
|
|
|
if (errno)
|
2009-05-08 20:52:12 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
char * tmp = tr_strdup_printf ("%s (%s)", filename, tr_strerror (errno));
|
|
|
|
send_simple_response (req, HTTP_NOTFOUND, tmp);
|
|
|
|
tr_free (tmp);
|
2009-05-08 20:52:12 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
struct evbuffer * out;
|
|
|
|
const time_t now = tr_time ();
|
|
|
|
|
|
|
|
errno = error;
|
|
|
|
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);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2010-12-20 02:07:51 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
evbuffer_free (content);
|
2008-07-16 17:47:20 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
handle_web_client (struct evhttp_request * req,
|
|
|
|
struct tr_rpc_server * server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
const char * webClientDir = tr_getWebClientDir (server->session);
|
2008-09-25 05:03:39 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!webClientDir || !*webClientDir)
|
2008-10-07 01:25:29 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
send_simple_response (req, HTTP_NOTFOUND,
|
2013-02-02 16:22:21 +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 "
|
|
|
|
"or tweak tr_getClutchDir () by hand.</p>");
|
2008-10-07 01:25:29 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else
|
2008-10-07 01:25:29 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
char * pch;
|
|
|
|
char * subpath;
|
2008-10-07 01:25:29 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
subpath = tr_strdup (req->uri + strlen (server->url) + 4);
|
|
|
|
if ((pch = strchr (subpath, '?')))
|
|
|
|
*pch = '\0';
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (strstr (subpath, ".."))
|
2009-04-10 15:09:31 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
send_simple_response (req, HTTP_NOTFOUND, "<p>Tsk, tsk.</p>");
|
2009-04-10 15:09:31 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else
|
2009-04-10 15:09:31 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
char * filename = tr_strdup_printf ("%s%s%s",
|
|
|
|
webClientDir,
|
|
|
|
TR_PATH_DELIMITER_STR,
|
|
|
|
subpath && *subpath ? subpath : "index.html");
|
|
|
|
serve_file (req, server, filename);
|
|
|
|
tr_free (filename);
|
2009-04-10 15:09:31 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2013-02-02 16:22:21 +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
|
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
struct evhttp_request * req;
|
|
|
|
struct tr_rpc_server * server;
|
2009-01-18 15:24:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
rpc_response_func (tr_session * session UNUSED,
|
2010-12-20 02:07:51 +00:00
|
|
|
struct evbuffer * response,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * user_data)
|
2009-01-18 15:24:26 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
struct rpc_response_data * data = user_data;
|
|
|
|
struct evbuffer * buf = evbuffer_new ();
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
add_response (data->req, data->server, buf, response);
|
|
|
|
evhttp_add_header (data->req->output_headers,
|
|
|
|
"Content-Type", "application/json; charset=UTF-8");
|
|
|
|
evhttp_send_reply (data->req, HTTP_OK, "OK", buf);
|
2009-01-18 15:24:26 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
evbuffer_free (buf);
|
|
|
|
tr_free (data);
|
2009-01-18 15:24:26 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
2013-02-10 18:33:04 +00:00
|
|
|
handle_rpc_from_json (struct evhttp_request * req,
|
|
|
|
struct tr_rpc_server * server,
|
|
|
|
const char * json,
|
|
|
|
size_t json_len)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-10 18:33:04 +00:00
|
|
|
struct rpc_response_data * data;
|
2008-06-05 04:02:46 +00:00
|
|
|
|
2013-02-10 18:33:04 +00:00
|
|
|
data = tr_new0 (struct rpc_response_data, 1);
|
2013-02-02 16:22:21 +00:00
|
|
|
data->req = req;
|
|
|
|
data->server = server;
|
2009-08-12 14:40:32 +00:00
|
|
|
|
2013-02-10 18:33:04 +00:00
|
|
|
tr_rpc_request_exec_json (server->session, json, json_len, rpc_response_func, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_rpc (struct evhttp_request * req, struct tr_rpc_server * server)
|
|
|
|
{
|
|
|
|
const char * q;
|
|
|
|
|
|
|
|
if (req->type == EVHTTP_REQ_POST)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-10 18:33:04 +00:00
|
|
|
handle_rpc_from_json (req, server,
|
|
|
|
(const char *) evbuffer_pullup (req->input_buffer, -1),
|
|
|
|
evbuffer_get_length (req->input_buffer));
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2013-02-10 18:33:04 +00:00
|
|
|
else if ((req->type == EVHTTP_REQ_GET) && ((q = strchr (req->uri, '?'))))
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-10 18:33:04 +00:00
|
|
|
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, -1, rpc_response_func, data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
send_simple_response (req, 405, NULL);
|
2008-12-30 22:07:39 +00:00
|
|
|
}
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2013-02-02 16:22:21 +00:00
|
|
|
isAddressAllowed (const tr_rpc_server * server, const char * address)
|
2008-09-26 15:40:24 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_list * l;
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!server->isWhitelistEnabled)
|
|
|
|
return true;
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
for (l=server->whitelist; l!=NULL; l=l->next)
|
|
|
|
if (tr_wildmat (address, l->data))
|
|
|
|
return true;
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
return false;
|
2008-09-26 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2012-12-05 17:29:46 +00:00
|
|
|
test_session_id (struct tr_rpc_server * server, struct evhttp_request * req)
|
2009-05-08 14:56:11 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
const char * ours = get_current_session_id (server);
|
|
|
|
const char * theirs = evhttp_find_header (req->input_headers, TR_RPC_SESSION_ID_HEADER);
|
|
|
|
const bool success = theirs && !strcmp (theirs, ours);
|
|
|
|
return success;
|
2009-05-08 14:56:11 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
handle_request (struct evhttp_request * req, void * arg)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
struct tr_rpc_server * server = arg;
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (req && req->evcon)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
const char * auth;
|
|
|
|
char * user = NULL;
|
|
|
|
char * pass = NULL;
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
evhttp_add_header (req->output_headers, "Server", MY_REALM);
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
auth = evhttp_find_header (req->input_headers, "Authorization");
|
|
|
|
if (auth && !evutil_ascii_strncasecmp (auth, "basic ", 6))
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2014-12-04 19:58:34 +00:00
|
|
|
size_t plen;
|
|
|
|
char * p = tr_base64_decode_str (auth + 6, &plen);
|
2013-02-02 16:22:21 +00:00
|
|
|
if (p && plen && ((pass = strchr (p, ':'))))
|
2008-09-26 15:40:24 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
user = p;
|
|
|
|
*pass++ = '\0';
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-15 19:45:46 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!isAddressAllowed (server, req->remote_host))
|
2008-07-16 23:55:49 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
send_simple_response (req, 403,
|
|
|
|
"<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>");
|
2008-07-11 17:09:53 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else if (server->isPasswordEnabled
|
2012-12-05 17:29:46 +00:00
|
|
|
&& (!pass || !user || strcmp (server->username, user)
|
|
|
|
|| !tr_ssha1_matches (server->password,
|
|
|
|
pass)))
|
2008-07-16 23:55:49 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
evhttp_add_header (req->output_headers,
|
|
|
|
"WWW-Authenticate",
|
|
|
|
"Basic realm=\"" MY_REALM "\"");
|
|
|
|
send_simple_response (req, 401, "Unauthorized User");
|
2008-06-05 04:02:46 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else if (strncmp (req->uri, server->url, strlen (server->url)))
|
2008-07-16 23:55:49 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +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
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else if (!strncmp (req->uri + strlen (server->url), "web/", 4))
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
handle_web_client (req, server);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2013-02-10 18:33:04 +00:00
|
|
|
else if (!strcmp (req->uri + strlen (server->url), "upload"))
|
2010-12-31 14:07:27 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
handle_upload (req, server);
|
2010-12-31 14:07:27 +00:00
|
|
|
}
|
2009-05-08 14:56:11 +00:00
|
|
|
#ifdef REQUIRE_SESSION_ID
|
2013-02-02 16:22:21 +00:00
|
|
|
else if (!test_session_id (server, req))
|
2009-05-08 14:56:11 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
const char * sessionId = get_current_session_id (server);
|
|
|
|
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 "
|
|
|
|
"<a href=\"http://en.wikipedia.org/wiki/Cross-site_request_forgery\">CSRF</a> "
|
|
|
|
"attacks.</p>"
|
|
|
|
"<p><code>%s: %s</code></p>",
|
|
|
|
TR_RPC_SESSION_ID_HEADER, sessionId);
|
|
|
|
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
|
|
|
}
|
|
|
|
#endif
|
2013-02-02 16:22:21 +00:00
|
|
|
else if (!strncmp (req->uri + strlen (server->url), "rpc", 3))
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
handle_rpc (req, server);
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
else
|
2008-09-03 19:59:09 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +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
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_free (user);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
startServer (void * vserver)
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_rpc_server * server = vserver;
|
|
|
|
tr_address addr;
|
2008-10-01 04:26:38 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!server->httpd)
|
2008-09-29 03:02:27 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
addr.type = TR_AF_INET;
|
|
|
|
addr.addr.addr4 = server->bindAddress;
|
|
|
|
server->httpd = evhttp_new (server->session->event_base);
|
|
|
|
evhttp_bind_socket (server->httpd, tr_address_to_string (&addr), server->port);
|
|
|
|
evhttp_set_gencb (server->httpd, handle_request, server);
|
2008-09-29 03:02:27 +00:00
|
|
|
}
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
stopServer (tr_rpc_server * server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
if (server->httpd)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
evhttp_free (server->httpd);
|
|
|
|
server->httpd = NULL;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-01 04:26:38 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
onEnabledChanged (void * vserver)
|
2008-10-01 04:26:38 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_rpc_server * server = vserver;
|
2008-10-01 04:26:38 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (!server->isEnabled)
|
|
|
|
stopServer (server);
|
|
|
|
else
|
|
|
|
startServer (server);
|
2008-10-01 04:26:38 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcSetEnabled (tr_rpc_server * server,
|
|
|
|
bool isEnabled)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
server->isEnabled = isEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_runInEventThread (server->session, onEnabledChanged, server);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcIsEnabled (const tr_rpc_server * server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->isEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2008-10-01 04:26:38 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
restartServer (void * vserver)
|
2008-10-01 04:26:38 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_rpc_server * server = vserver;
|
2008-10-01 04:26:38 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (server->isEnabled)
|
2008-10-01 04:26:38 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
stopServer (server);
|
|
|
|
startServer (server);
|
2008-10-01 04:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcSetPort (tr_rpc_server * server,
|
|
|
|
tr_port port)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
assert (server != NULL);
|
2010-01-16 22:46:38 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (server->port != port)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
server->port = port;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (server->isEnabled)
|
|
|
|
tr_runInEventThread (server->session, restartServer, server);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-01 20:51:01 +00:00
|
|
|
tr_port
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcGetPort (const tr_rpc_server * server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->port;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 18:48:09 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcSetUrl (tr_rpc_server * server, const char * url)
|
2010-12-12 18:22:11 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +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
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcGetUrl (const tr_rpc_server * server)
|
2010-12-12 18:22:11 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->url ? server->url : "";
|
2010-12-12 18:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcSetWhitelist (tr_rpc_server * server, const char * whitelistStr)
|
2008-12-21 19:23:41 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
void * tmp;
|
|
|
|
const char * walk;
|
|
|
|
|
|
|
|
/* keep the string */
|
|
|
|
tmp = server->whitelistStr;
|
|
|
|
server->whitelistStr = tr_strdup (whitelistStr);
|
|
|
|
tr_free (tmp);
|
2008-12-21 19:23:41 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
/* clear out the old whitelist entries */
|
|
|
|
while ((tmp = tr_list_pop_front (&server->whitelist)))
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (tmp);
|
2008-12-21 19:23:41 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
/* build the new whitelist entries */
|
|
|
|
for (walk=whitelistStr; walk && *walk;)
|
|
|
|
{
|
|
|
|
const char * delimiters = " ,;";
|
|
|
|
const size_t len = strcspn (walk, delimiters);
|
|
|
|
char * token = tr_strndup (walk, len);
|
|
|
|
tr_list_append (&server->whitelist, token);
|
|
|
|
if (strcspn (token, "+-") < len)
|
|
|
|
tr_logAddNamedInfo (MY_NAME, "Adding address to whitelist: %s (And it has a '+' or '-'! Are you using an old ACL by mistake?)", token);
|
|
|
|
else
|
|
|
|
tr_logAddNamedInfo (MY_NAME, "Adding address to whitelist: %s", token);
|
|
|
|
|
|
|
|
if (walk[len]=='\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
walk += len + 1;
|
2008-12-21 19:23:41 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 19:14:37 +00:00
|
|
|
const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcGetWhitelist (const tr_rpc_server * server)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->whitelistStr ? server->whitelistStr : "";
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2008-10-01 22:59:29 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcSetWhitelistEnabled (tr_rpc_server * server,
|
|
|
|
bool isEnabled)
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2013-08-24 18:08:38 +00:00
|
|
|
assert (tr_isBool (isEnabled));
|
|
|
|
|
|
|
|
server->isWhitelistEnabled = isEnabled;
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcGetWhitelistEnabled (const tr_rpc_server * server)
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->isWhitelistEnabled;
|
2008-10-01 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
/****
|
|
|
|
***** PASSWORD
|
|
|
|
****/
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcSetUsername (tr_rpc_server * server, const char * username)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +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
|
|
|
}
|
|
|
|
|
2009-04-16 19:14:37 +00:00
|
|
|
const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcGetUsername (const tr_rpc_server * server)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->username ? server->username : "";
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcSetPassword (tr_rpc_server * server,
|
|
|
|
const char * password)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +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
|
|
|
}
|
|
|
|
|
2009-04-16 19:14:37 +00:00
|
|
|
const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcGetPassword (const tr_rpc_server * server)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->password ? server->password : "" ;
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcSetPasswordEnabled (tr_rpc_server * server, bool isEnabled)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
server->isPasswordEnabled = isEnabled;
|
|
|
|
dbgmsg ("setting 'password enabled' to %d", (int)isEnabled);
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcIsPasswordEnabled (const tr_rpc_server * server)
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
return server->isPasswordEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2009-04-15 21:05:58 +00:00
|
|
|
const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcGetBindAddress (const tr_rpc_server * server)
|
2009-04-15 21:05:58 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_address addr;
|
|
|
|
addr.type = TR_AF_INET;
|
|
|
|
addr.addr.addr4 = server->bindAddress;
|
|
|
|
return tr_address_to_string (&addr);
|
2009-04-15 21:05:58 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
/****
|
|
|
|
***** LIFE CYCLE
|
|
|
|
****/
|
|
|
|
|
2008-10-01 04:26:38 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
closeServer (void * vserver)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
void * tmp;
|
|
|
|
tr_rpc_server * s = vserver;
|
2008-10-01 20:23:57 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
stopServer (s);
|
|
|
|
while ((tmp = tr_list_pop_front (&s->whitelist)))
|
|
|
|
tr_free (tmp);
|
|
|
|
if (s->isStreamInitialized)
|
|
|
|
deflateEnd (&s->stream);
|
|
|
|
tr_free (s->url);
|
|
|
|
tr_free (s->sessionId);
|
|
|
|
tr_free (s->whitelistStr);
|
|
|
|
tr_free (s->username);
|
|
|
|
tr_free (s->password);
|
|
|
|
tr_free (s);
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2008-10-01 04:26:38 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_rpcClose (tr_rpc_server ** ps)
|
2008-10-01 04:26:38 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_runInEventThread ((*ps)->session, closeServer, *ps);
|
|
|
|
*ps = NULL;
|
2008-10-01 04:26:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
static void
|
|
|
|
missing_settings_key (const tr_quark q)
|
|
|
|
{
|
|
|
|
const char * str = tr_quark_get_string (q, NULL);
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedError (MY_NAME, _("Couldn't find settings key \"%s\""), str);
|
2012-12-22 20:35:19 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_rpc_server *
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_rpcInit (tr_session * session, tr_variant * settings)
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_rpc_server * s;
|
|
|
|
bool boolVal;
|
|
|
|
int64_t i;
|
|
|
|
const char * str;
|
|
|
|
tr_quark key;
|
|
|
|
tr_address address;
|
|
|
|
|
|
|
|
s = tr_new0 (tr_rpc_server, 1);
|
|
|
|
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))
|
|
|
|
missing_settings_key (key);
|
|
|
|
else
|
|
|
|
s->port = i;
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_url;
|
|
|
|
if (!tr_variantDictFindStr (settings, key, &str, NULL))
|
|
|
|
missing_settings_key (key);
|
|
|
|
else
|
|
|
|
s->url = tr_strdup (str);
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_whitelist_enabled;
|
|
|
|
if (!tr_variantDictFindBool (settings, key, &boolVal))
|
|
|
|
missing_settings_key (key);
|
|
|
|
else
|
|
|
|
tr_rpcSetWhitelistEnabled (s, boolVal);
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_authentication_required;
|
|
|
|
if (!tr_variantDictFindBool (settings, key, &boolVal))
|
|
|
|
missing_settings_key (key);
|
|
|
|
else
|
|
|
|
tr_rpcSetPasswordEnabled (s, boolVal);
|
|
|
|
|
|
|
|
key = TR_KEY_rpc_whitelist;
|
|
|
|
if (!tr_variantDictFindStr (settings, key, &str, NULL) && str)
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else if (address.type != TR_AF_INET)
|
|
|
|
{
|
|
|
|
tr_logAddNamedError (MY_NAME, _("%s is not an IPv4 address. RPC listeners must be IPv4"), str);
|
|
|
|
address = tr_inaddr_any;
|
2009-04-15 21:05:58 +00:00
|
|
|
}
|
2013-02-02 16:22:21 +00:00
|
|
|
s->bindAddress = address.addr.addr4;
|
2009-04-15 21:05:58 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (s->isEnabled)
|
2008-12-16 16:13:21 +00:00
|
|
|
{
|
2013-02-02 16:22:21 +00:00
|
|
|
tr_logAddNamedInfo (MY_NAME, _("Serving RPC and Web requests on port 127.0.0.1:%d%s"), (int) s->port, s->url);
|
|
|
|
tr_runInEventThread (session, startServer, s);
|
2008-12-16 16:13:21 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (s->isWhitelistEnabled)
|
|
|
|
tr_logAddNamedInfo (MY_NAME, "%s", _("Whitelist enabled"));
|
2008-12-16 16:13:21 +00:00
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
if (s->isPasswordEnabled)
|
|
|
|
tr_logAddNamedInfo (MY_NAME, "%s", _("Password required"));
|
2008-12-16 16:13:21 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 16:22:21 +00:00
|
|
|
return s;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|