2008-05-18 16:44:30 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com>
|
2008-05-18 16:44:30 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-05-18 16:44:30 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
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
|
|
|
#include <limits.h> /* INT_MAX */
|
|
|
|
|
2008-09-26 00:58:06 +00:00
|
|
|
#include <sys/types.h> /* open */
|
|
|
|
#include <sys/stat.h> /* open */
|
|
|
|
#include <fcntl.h> /* open */
|
|
|
|
#include <unistd.h> /* close */
|
2008-06-05 16:23:03 +00:00
|
|
|
|
2008-11-09 15:43:31 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2008-10-01 20:23:57 +00:00
|
|
|
#include <zlib.h>
|
2008-10-01 15:53:56 +00:00
|
|
|
#endif
|
|
|
|
|
2008-05-19 00:22:04 +00:00
|
|
|
#include <libevent/event.h>
|
2008-09-25 05:03:39 +00:00
|
|
|
#include <libevent/evhttp.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-06-30 21:11:53 +00:00
|
|
|
#include "bencode.h"
|
2009-03-17 21:50:20 +00:00
|
|
|
#include "crypto.h"
|
2008-07-16 17:47:20 +00:00
|
|
|
#include "list.h"
|
2008-07-11 04:07:14 +00:00
|
|
|
#include "platform.h"
|
2008-09-05 14:31:58 +00:00
|
|
|
#include "rpcimpl.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "rpc-server.h"
|
2008-09-29 04:26:52 +00:00
|
|
|
#include "trevent.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "utils.h"
|
2008-09-25 18:48:09 +00:00
|
|
|
#include "web.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
|
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
|
|
|
#define TR_N_ELEMENTS( ary ) ( sizeof( ary ) / sizeof( *ary ) )
|
|
|
|
|
2008-10-14 01:00:15 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#define strncasecmp _strnicmp
|
|
|
|
#endif
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
struct tr_rpc_server
|
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isEnabled;
|
|
|
|
tr_bool isPasswordEnabled;
|
|
|
|
tr_bool isWhitelistEnabled;
|
2008-12-01 20:51:01 +00:00
|
|
|
tr_port port;
|
2008-10-01 20:23:57 +00:00
|
|
|
struct evhttp * httpd;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_session * session;
|
2008-10-01 20:23:57 +00:00
|
|
|
char * username;
|
|
|
|
char * password;
|
2008-12-21 19:23:41 +00:00
|
|
|
char * whitelistStr;
|
|
|
|
tr_list * whitelist;
|
2008-12-30 20:40:48 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
z_stream stream;
|
|
|
|
#endif
|
2008-05-18 16:44:30 +00:00
|
|
|
};
|
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, MY_NAME, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
2008-06-05 04:02:46 +00:00
|
|
|
|
2008-09-26 15:40:24 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
|
|
|
send_simple_response( struct evhttp_request * req,
|
|
|
|
int code,
|
|
|
|
const char * text )
|
|
|
|
{
|
|
|
|
const char * code_text = tr_webGetResponseStr( code );
|
2008-12-30 20:32:00 +00:00
|
|
|
struct evbuffer * body = tr_getBuffer( );
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2008-10-06 13:36:06 +00:00
|
|
|
evbuffer_add_printf( body, "<h1>%d: %s</h1>", code, code_text );
|
2008-09-26 15:40:24 +00:00
|
|
|
if( text )
|
2008-10-07 01:25:29 +00:00
|
|
|
evbuffer_add_printf( body, "%s", text );
|
2008-09-26 15:40:24 +00:00
|
|
|
evhttp_send_reply( req, code, code_text, body );
|
2008-12-30 20:32:00 +00:00
|
|
|
|
|
|
|
tr_releaseBuffer( body );
|
2008-09-26 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2008-06-30 21:11:53 +00:00
|
|
|
static const char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_memmem( const char * s1,
|
|
|
|
size_t l1,
|
|
|
|
const char * s2,
|
|
|
|
size_t l2 )
|
2008-06-30 21:11:53 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !l2 ) return s1;
|
|
|
|
while( l1 >= l2 )
|
|
|
|
{
|
2008-07-16 17:47:20 +00:00
|
|
|
l1--;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !memcmp( s1, s2, l2 ) )
|
2008-07-16 17:47:20 +00:00
|
|
|
return s1;
|
|
|
|
s1++;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-16 17:47:20 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-30 21:11:53 +00:00
|
|
|
static void
|
2008-09-26 15:40:24 +00:00
|
|
|
handle_upload( struct evhttp_request * req,
|
|
|
|
struct tr_rpc_server * server )
|
2008-06-30 21:11:53 +00:00
|
|
|
{
|
2008-09-25 05:03:39 +00:00
|
|
|
if( req->type != EVHTTP_REQ_POST )
|
|
|
|
{
|
2008-09-26 00:58:06 +00:00
|
|
|
send_simple_response( req, 405, NULL );
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-26 15:40:24 +00:00
|
|
|
const char * content_type = evhttp_find_header( req->input_headers,
|
|
|
|
"Content-Type" );
|
2008-06-30 21:11:53 +00:00
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
const char * query = strchr( req->uri, '?' );
|
2008-09-26 15:40:24 +00:00
|
|
|
const int paused = query && strstr( query + 1, "paused=true" );
|
2008-09-25 05:03:39 +00:00
|
|
|
|
|
|
|
const char * in = (const char *) EVBUFFER_DATA( req->input_buffer );
|
2008-09-26 15:40:24 +00:00
|
|
|
size_t inlen = EVBUFFER_LENGTH( req->input_buffer );
|
2008-09-25 05:03:39 +00:00
|
|
|
|
2008-09-26 04:41:13 +00:00
|
|
|
const char * boundary_key = "boundary=";
|
2008-09-26 15:40:24 +00:00
|
|
|
const char * boundary_key_begin = strstr( content_type,
|
|
|
|
boundary_key );
|
|
|
|
const char * boundary_val =
|
|
|
|
boundary_key_begin ? boundary_key_begin +
|
|
|
|
strlen( boundary_key ) : "arglebargle";
|
2008-09-26 04:41:13 +00:00
|
|
|
|
2008-09-26 15:40:24 +00:00
|
|
|
char * boundary = tr_strdup_printf( "--%s", boundary_val );
|
2008-09-25 05:03:39 +00:00
|
|
|
const size_t boundary_len = strlen( boundary );
|
|
|
|
|
|
|
|
const char * delim = tr_memmem( in, inlen, boundary, boundary_len );
|
2008-09-25 18:48:09 +00:00
|
|
|
while( delim )
|
2008-09-26 04:41:13 +00:00
|
|
|
{
|
|
|
|
size_t part_len;
|
|
|
|
const char * part = delim + boundary_len;
|
|
|
|
inlen -= ( part - in );
|
|
|
|
in = part;
|
|
|
|
delim = tr_memmem( in, inlen, boundary, boundary_len );
|
|
|
|
part_len = delim ? (size_t)( delim - part ) : inlen;
|
|
|
|
|
|
|
|
if( part_len )
|
2008-06-30 21:11:53 +00:00
|
|
|
{
|
2008-09-26 04:41:13 +00:00
|
|
|
char * text = tr_strndup( part, part_len );
|
|
|
|
if( strstr( text, "filename=\"" ) )
|
2008-06-30 21:11:53 +00:00
|
|
|
{
|
2008-09-26 04:41:13 +00:00
|
|
|
const char * body = strstr( text, "\r\n\r\n" );
|
|
|
|
if( body )
|
2008-06-30 21:11:53 +00:00
|
|
|
{
|
2008-12-30 22:07:39 +00:00
|
|
|
char * b64;
|
2008-09-26 04:41:13 +00:00
|
|
|
size_t body_len;
|
|
|
|
tr_benc top, *args;
|
2008-12-30 22:07:39 +00:00
|
|
|
struct evbuffer * json = tr_getBuffer( );
|
2008-09-26 04:41:13 +00:00
|
|
|
|
2008-10-06 13:36:06 +00:00
|
|
|
body += 4; /* walk past the \r\n\r\n */
|
2008-09-26 04:41:13 +00:00
|
|
|
body_len = part_len - ( body - text );
|
|
|
|
if( body_len >= 2
|
2008-09-26 15:40:24 +00:00
|
|
|
&& !memcmp( &body[body_len - 2], "\r\n", 2 ) )
|
|
|
|
body_len -= 2;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-09-26 04:41:13 +00:00
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
args = tr_bencDictAddDict( &top, "arguments", 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-add" );
|
|
|
|
b64 = tr_base64_encode( body, body_len, NULL );
|
|
|
|
tr_bencDictAddStr( args, "metainfo", b64 );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( args, "paused", paused );
|
2008-12-30 22:07:39 +00:00
|
|
|
tr_bencSaveAsJSON( &top, json );
|
|
|
|
tr_rpc_request_exec_json( server->session,
|
|
|
|
EVBUFFER_DATA( json ),
|
|
|
|
EVBUFFER_LENGTH( json ),
|
2009-01-18 15:24:26 +00:00
|
|
|
NULL, NULL );
|
2008-09-26 04:41:13 +00:00
|
|
|
|
2008-12-30 22:07:39 +00:00
|
|
|
tr_releaseBuffer( json );
|
2008-09-26 04:41:13 +00:00
|
|
|
tr_free( b64 );
|
|
|
|
tr_bencFree( &top );
|
2008-06-30 21:11:53 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-26 04:41:13 +00:00
|
|
|
tr_free( text );
|
2008-06-30 21:11:53 +00:00
|
|
|
}
|
2008-09-26 04:41:13 +00:00
|
|
|
}
|
2008-06-30 21:11:53 +00:00
|
|
|
|
|
|
|
tr_free( boundary );
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
/* use xml here because json responses to file uploads is trouble.
|
2008-09-26 15:40:24 +00:00
|
|
|
* see http://www.malsup.com/jquery/form/#sample7 for details */
|
|
|
|
evhttp_add_header( req->output_headers, "Content-Type",
|
|
|
|
"text/xml; charset=UTF-8" );
|
2008-09-25 18:48:09 +00:00
|
|
|
send_simple_response( req, HTTP_OK, NULL );
|
2008-07-16 17:47:20 +00:00
|
|
|
}
|
2008-06-30 21:11:53 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
static const char*
|
|
|
|
mimetype_guess( const char * path )
|
2008-08-06 00:24:05 +00:00
|
|
|
{
|
2008-09-25 05:03:39 +00:00
|
|
|
unsigned int i;
|
2008-09-26 15:40:24 +00:00
|
|
|
|
|
|
|
const struct
|
|
|
|
{
|
2008-10-01 20:23:57 +00:00
|
|
|
const char * suffix;
|
|
|
|
const char * mime_type;
|
2008-09-25 05:03:39 +00:00
|
|
|
} types[] = {
|
|
|
|
/* these are just the ones we need for serving clutch... */
|
2008-10-06 13:36:06 +00:00
|
|
|
{ "css", "text/css" },
|
|
|
|
{ "gif", "image/gif" },
|
|
|
|
{ "html", "text/html" },
|
|
|
|
{ "ico", "image/vnd.microsoft.icon" },
|
|
|
|
{ "js", "application/javascript" },
|
|
|
|
{ "png", "image/png" }
|
2008-09-25 05:03:39 +00:00
|
|
|
};
|
|
|
|
const char * dot = strrchr( path, '.' );
|
|
|
|
|
2008-09-26 15:40:24 +00:00
|
|
|
for( i = 0; dot && i < TR_N_ELEMENTS( types ); ++i )
|
|
|
|
if( !strcmp( dot + 1, types[i].suffix ) )
|
2008-09-25 05:03:39 +00:00
|
|
|
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
|
|
|
|
add_response( struct evhttp_request * req,
|
2008-12-30 20:40:48 +00:00
|
|
|
struct tr_rpc_server * server,
|
2008-10-08 13:33:19 +00:00
|
|
|
struct evbuffer * out,
|
|
|
|
const void * content,
|
|
|
|
size_t content_len )
|
2008-10-01 15:53:56 +00:00
|
|
|
{
|
2008-11-09 15:43:31 +00:00
|
|
|
#ifndef HAVE_ZLIB
|
2008-10-08 13:33:19 +00:00
|
|
|
evbuffer_add( out, content, content_len );
|
|
|
|
#else
|
|
|
|
const char * key = "Accept-Encoding";
|
|
|
|
const char * encoding = evhttp_find_header( req->input_headers, key );
|
|
|
|
const int do_deflate = encoding && strstr( encoding, "deflate" );
|
2008-10-03 17:38:14 +00:00
|
|
|
|
2008-10-08 13:33:19 +00:00
|
|
|
if( !do_deflate )
|
|
|
|
{
|
|
|
|
evbuffer_add( out, content, content_len );
|
2008-10-01 20:23:57 +00:00
|
|
|
}
|
2008-10-08 13:33:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int state;
|
2008-10-01 15:53:56 +00:00
|
|
|
|
2008-12-30 20:40:48 +00:00
|
|
|
server->stream.next_in = (Bytef*) content;
|
|
|
|
server->stream.avail_in = content_len;
|
2008-10-01 20:23:57 +00:00
|
|
|
|
2008-10-08 13:39:44 +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 */
|
2008-10-08 13:33:19 +00:00
|
|
|
evbuffer_expand( out, content_len );
|
2008-12-30 20:40:48 +00:00
|
|
|
server->stream.next_out = EVBUFFER_DATA( out );
|
|
|
|
server->stream.avail_out = content_len;
|
2008-10-01 15:53:56 +00:00
|
|
|
|
2008-12-30 20:40:48 +00:00
|
|
|
state = deflate( &server->stream, Z_FINISH );
|
2008-10-08 13:39:44 +00:00
|
|
|
|
|
|
|
if( state == Z_STREAM_END )
|
|
|
|
{
|
2008-12-30 20:40:48 +00:00
|
|
|
EVBUFFER_LENGTH( out ) = content_len - server->stream.avail_out;
|
2008-11-01 22:09:16 +00:00
|
|
|
|
|
|
|
/* http://carsten.codimi.de/gzip.yaws/
|
|
|
|
It turns out that some browsers expect deflated data without
|
|
|
|
the first two bytes (a kind of header) and and the last four
|
|
|
|
bytes (an ADLER32 checksum). This format can of course
|
|
|
|
be produced by simply stripping these off. */
|
|
|
|
if( EVBUFFER_LENGTH( out ) >= 6 ) {
|
|
|
|
EVBUFFER_LENGTH( out ) -= 4;
|
|
|
|
evbuffer_drain( out, 2 );
|
|
|
|
}
|
|
|
|
|
2008-11-19 16:12:00 +00:00
|
|
|
#if 0
|
2008-10-08 13:33:19 +00:00
|
|
|
tr_ninf( MY_NAME, _( "Deflated response from %zu bytes to %zu" ),
|
|
|
|
content_len,
|
|
|
|
EVBUFFER_LENGTH( out ) );
|
2008-11-19 16:12:00 +00:00
|
|
|
#endif
|
2008-10-08 13:33:19 +00:00
|
|
|
evhttp_add_header( req->output_headers,
|
|
|
|
"Content-Encoding", "deflate" );
|
|
|
|
}
|
2008-10-08 13:39:44 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
evbuffer_drain( out, EVBUFFER_LENGTH( out ) );
|
|
|
|
evbuffer_add( out, content, content_len );
|
|
|
|
}
|
2008-10-08 13:33:19 +00:00
|
|
|
|
2008-12-30 20:40:48 +00:00
|
|
|
deflateReset( &server->stream );
|
2008-10-08 13:33:19 +00:00
|
|
|
}
|
2008-10-01 15:53:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
2008-09-26 15:40:24 +00:00
|
|
|
serve_file( struct evhttp_request * req,
|
2008-12-30 20:40:48 +00:00
|
|
|
struct tr_rpc_server * server,
|
2008-10-06 13:36:06 +00:00
|
|
|
const char * filename )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-09-25 05:03:39 +00:00
|
|
|
if( req->type != EVHTTP_REQ_GET )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-09-26 15:40:24 +00:00
|
|
|
evhttp_add_header( req->output_headers, "Allow", "GET" );
|
2008-09-25 18:48:09 +00:00
|
|
|
send_simple_response( req, 405, NULL );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2008-09-25 05:03:39 +00:00
|
|
|
else
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-10-06 13:36:06 +00:00
|
|
|
size_t content_len;
|
|
|
|
uint8_t * content;
|
|
|
|
const int error = errno;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
content_len = 0;
|
|
|
|
content = tr_loadFile( filename, &content_len );
|
|
|
|
|
|
|
|
if( errno )
|
2008-09-25 18:48:09 +00:00
|
|
|
{
|
2009-03-17 21:50:20 +00:00
|
|
|
send_simple_response( req, HTTP_NOTFOUND, filename );
|
2008-09-25 18:48:09 +00:00
|
|
|
}
|
|
|
|
else
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2008-10-06 16:33:33 +00:00
|
|
|
struct evbuffer * out;
|
2008-10-06 13:36:06 +00:00
|
|
|
|
2008-10-06 16:33:33 +00:00
|
|
|
errno = error;
|
2008-12-30 20:32:00 +00:00
|
|
|
out = tr_getBuffer( );
|
2008-09-26 15:40:24 +00:00
|
|
|
evhttp_add_header( req->output_headers, "Content-Type",
|
2008-10-06 13:36:06 +00:00
|
|
|
mimetype_guess( filename ) );
|
2008-12-30 20:40:48 +00:00
|
|
|
add_response( req, server, out, content, content_len );
|
2008-10-06 13:36:06 +00:00
|
|
|
evhttp_send_reply( req, HTTP_OK, "OK", out );
|
|
|
|
|
2008-12-30 20:32:00 +00:00
|
|
|
tr_releaseBuffer( out );
|
2008-10-06 13:36:06 +00:00
|
|
|
tr_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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-26 15:40:24 +00:00
|
|
|
handle_clutch( struct evhttp_request * req,
|
|
|
|
struct tr_rpc_server * server )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-10-02 16:24:44 +00:00
|
|
|
const char * clutchDir = tr_getClutchDir( server->session );
|
2008-09-25 05:03:39 +00:00
|
|
|
|
|
|
|
assert( !strncmp( req->uri, "/transmission/web/", 18 ) );
|
|
|
|
|
2008-10-07 01:25:29 +00:00
|
|
|
if( !clutchDir || !*clutchDir )
|
|
|
|
{
|
|
|
|
send_simple_response( req, HTTP_NOTFOUND,
|
|
|
|
"<p>Couldn't find Transmission's web interface files!</p>"
|
|
|
|
"<p>Users: to tell Transmission where to look, "
|
|
|
|
"set the TRANSMISSION_WEB_HOME environmental "
|
|
|
|
"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>" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char * pch;
|
|
|
|
char * subpath;
|
|
|
|
char * filename;
|
|
|
|
|
|
|
|
subpath = tr_strdup( req->uri + 18 );
|
|
|
|
if(( pch = strchr( subpath, '?' )))
|
|
|
|
*pch = '\0';
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2008-10-08 13:33:19 +00:00
|
|
|
filename = tr_strdup_printf( "%s%s%s",
|
|
|
|
clutchDir,
|
|
|
|
TR_PATH_DELIMITER_STR,
|
2008-10-08 16:31:17 +00:00
|
|
|
subpath && *subpath ? subpath : "index.html" );
|
2008-10-02 16:24:44 +00:00
|
|
|
|
2008-12-30 20:40:48 +00:00
|
|
|
serve_file( req, server, filename );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-10-07 01:25:29 +00:00
|
|
|
tr_free( filename );
|
|
|
|
tr_free( subpath );
|
|
|
|
}
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
struct rpc_response_data
|
|
|
|
{
|
|
|
|
struct evhttp_request * req;
|
|
|
|
struct tr_rpc_server * server;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
rpc_response_func( tr_session * session UNUSED,
|
|
|
|
const char * response,
|
|
|
|
size_t response_len,
|
|
|
|
void * user_data )
|
|
|
|
{
|
|
|
|
struct rpc_response_data * data = user_data;
|
|
|
|
struct evbuffer * buf = tr_getBuffer( );
|
|
|
|
|
|
|
|
add_response( data->req, data->server, buf, response, response_len );
|
|
|
|
evhttp_add_header( data->req->output_headers,
|
|
|
|
"Content-Type", "application/json; charset=UTF-8" );
|
|
|
|
evhttp_send_reply( data->req, HTTP_OK, "OK", buf );
|
|
|
|
|
|
|
|
tr_releaseBuffer( buf );
|
|
|
|
tr_free( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
2008-09-26 15:40:24 +00:00
|
|
|
handle_rpc( struct evhttp_request * req,
|
2008-12-30 22:07:39 +00:00
|
|
|
struct tr_rpc_server * server )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-01-18 15:24:26 +00:00
|
|
|
struct rpc_response_data * data = tr_new0( struct rpc_response_data, 1 );
|
2008-06-05 04:02:46 +00:00
|
|
|
|
2009-01-18 15:24:26 +00:00
|
|
|
data->req = req;
|
|
|
|
data->server = server;
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
if( req->type == EVHTTP_REQ_GET )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-09-25 05:03:39 +00:00
|
|
|
const char * q;
|
2008-09-26 15:40:24 +00:00
|
|
|
if( ( q = strchr( req->uri, '?' ) ) )
|
2009-01-18 15:24:26 +00:00
|
|
|
tr_rpc_request_exec_uri( server->session, q+1, -1, rpc_response_func, data );
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
|
|
|
else if( req->type == EVHTTP_REQ_POST )
|
|
|
|
{
|
2008-12-30 22:07:39 +00:00
|
|
|
tr_rpc_request_exec_json( server->session,
|
|
|
|
EVBUFFER_DATA( req->input_buffer ),
|
|
|
|
EVBUFFER_LENGTH( req->input_buffer ),
|
2009-01-18 15:24:26 +00:00
|
|
|
rpc_response_func, data );
|
2008-12-30 22:07:39 +00:00
|
|
|
}
|
2008-10-02 16:50:05 +00:00
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
}
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2008-12-21 19:23:41 +00:00
|
|
|
static tr_bool
|
2008-09-26 15:40:24 +00:00
|
|
|
isAddressAllowed( const tr_rpc_server * server,
|
|
|
|
const char * address )
|
|
|
|
{
|
2008-12-21 19:23:41 +00:00
|
|
|
tr_list * l;
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2008-10-01 22:59:29 +00:00
|
|
|
if( !server->isWhitelistEnabled )
|
2008-12-21 19:23:41 +00:00
|
|
|
return TRUE;
|
2008-10-01 22:59:29 +00:00
|
|
|
|
2008-12-21 19:23:41 +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
|
|
|
|
2008-12-21 19:23:41 +00:00
|
|
|
return FALSE;
|
2008-09-26 15:40:24 +00:00
|
|
|
}
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
static void
|
2008-09-26 15:40:24 +00:00
|
|
|
handle_request( struct evhttp_request * req,
|
|
|
|
void * arg )
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
|
|
|
struct tr_rpc_server * server = arg;
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2008-09-26 15:40:24 +00:00
|
|
|
if( req && req->evcon )
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
|
|
|
const char * auth;
|
2008-09-26 15:40:24 +00:00
|
|
|
char * user = NULL;
|
|
|
|
char * pass = NULL;
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2008-09-25 18:48:09 +00:00
|
|
|
evhttp_add_header( req->output_headers, "Server", MY_REALM );
|
2008-09-26 15:40:24 +00:00
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
auth = evhttp_find_header( req->input_headers, "Authorization" );
|
2008-07-17 14:45:31 +00:00
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
if( auth && !strncasecmp( auth, "basic ", 6 ) )
|
|
|
|
{
|
2008-09-26 15:40:24 +00:00
|
|
|
int plen;
|
2008-09-25 05:03:39 +00:00
|
|
|
char * p = tr_base64_decode( auth + 6, 0, &plen );
|
2008-09-26 15:40:24 +00:00
|
|
|
if( p && plen && ( ( pass = strchr( p, ':' ) ) ) )
|
|
|
|
{
|
2008-09-25 05:03:39 +00:00
|
|
|
user = p;
|
|
|
|
*pass++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
2008-08-15 19:45:46 +00:00
|
|
|
|
2008-10-01 22:59:29 +00:00
|
|
|
if( !isAddressAllowed( server, req->remote_host ) )
|
2008-07-16 23:55:49 +00:00
|
|
|
{
|
2008-12-21 19:23:41 +00:00
|
|
|
send_simple_response( req, 401,
|
|
|
|
"<p>Unauthorized IP Address.</p>"
|
|
|
|
"<p>Either disable the IP address whitelist or add your address to it.</p>"
|
2008-12-23 21:34:07 +00:00
|
|
|
"<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
|
|
|
}
|
2008-10-06 13:36:06 +00:00
|
|
|
else if( server->isPasswordEnabled
|
|
|
|
&& ( !pass || !user || strcmp( server->username, user )
|
2009-03-24 01:39:06 +00:00
|
|
|
|| !tr_ssha1_matches( server->password,
|
|
|
|
pass ) ) )
|
2008-07-16 23:55:49 +00:00
|
|
|
{
|
2008-09-25 05:03:39 +00:00
|
|
|
evhttp_add_header( req->output_headers,
|
2008-09-26 15:40:24 +00:00
|
|
|
"WWW-Authenticate",
|
|
|
|
"Basic realm=\"" MY_REALM "\"" );
|
2008-09-25 18:48:09 +00:00
|
|
|
send_simple_response( req, 401, "Unauthorized User" );
|
2008-06-05 04:02:46 +00:00
|
|
|
}
|
2008-09-26 15:40:24 +00:00
|
|
|
else if( !strcmp( req->uri, "/transmission/web" )
|
|
|
|
|| !strcmp( req->uri, "/transmission/clutch" )
|
|
|
|
|| !strcmp( req->uri, "/" ) )
|
2008-07-16 23:55:49 +00:00
|
|
|
{
|
2009-03-24 17:11:53 +00:00
|
|
|
const char * protocol = "http";
|
|
|
|
const char * host = evhttp_find_header( req->input_headers, "Host" );
|
|
|
|
const char * uri = "transmission/web/";
|
|
|
|
char * location = tr_strdup_printf( "%s://%s/%s", protocol, host, uri );
|
|
|
|
evhttp_add_header( req->output_headers, "Location", location );
|
2008-09-25 18:48:09 +00:00
|
|
|
send_simple_response( req, HTTP_MOVEPERM, NULL );
|
2009-03-24 17:11:53 +00:00
|
|
|
tr_free( location );
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
2008-09-25 05:03:39 +00:00
|
|
|
else if( !strncmp( req->uri, "/transmission/web/", 18 ) )
|
|
|
|
{
|
|
|
|
handle_clutch( req, server );
|
|
|
|
}
|
|
|
|
else if( !strncmp( req->uri, "/transmission/rpc", 17 ) )
|
|
|
|
{
|
|
|
|
handle_rpc( req, server );
|
|
|
|
}
|
|
|
|
else if( !strncmp( req->uri, "/transmission/upload", 20 ) )
|
|
|
|
{
|
|
|
|
handle_upload( req, server );
|
|
|
|
}
|
|
|
|
else
|
2008-09-03 19:59:09 +00:00
|
|
|
{
|
2009-03-17 21:50:20 +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
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
tr_free( user );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
static void
|
2008-10-01 04:26:38 +00:00
|
|
|
startServer( void * vserver )
|
2008-09-25 05:03:39 +00:00
|
|
|
{
|
2008-10-01 04:26:38 +00:00
|
|
|
tr_rpc_server * server = vserver;
|
|
|
|
|
2008-09-25 05:03:39 +00:00
|
|
|
if( !server->httpd )
|
2008-09-29 03:02:27 +00:00
|
|
|
{
|
2008-09-29 04:26:52 +00:00
|
|
|
server->httpd = evhttp_new( tr_eventGetBase( server->session ) );
|
2008-10-01 15:53:56 +00:00
|
|
|
evhttp_bind_socket( server->httpd, "0.0.0.0", server->port );
|
2008-09-30 16:04:41 +00:00
|
|
|
evhttp_set_gencb( server->httpd, handle_request, server );
|
2009-04-05 15:49:03 +00:00
|
|
|
|
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
|
|
|
|
stopServer( tr_rpc_server * server )
|
|
|
|
{
|
2008-09-25 05:03:39 +00:00
|
|
|
if( server->httpd )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-09-25 05:03:39 +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
|
|
|
|
onEnabledChanged( void * vserver )
|
|
|
|
{
|
|
|
|
tr_rpc_server * server = vserver;
|
|
|
|
|
|
|
|
if( !server->isEnabled )
|
|
|
|
stopServer( server );
|
|
|
|
else
|
|
|
|
startServer( server );
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_rpcSetEnabled( tr_rpc_server * server,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isEnabled )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-12-13 23:39:12 +00:00
|
|
|
server->isEnabled = isEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2008-10-01 04:26:38 +00:00
|
|
|
tr_runInEventThread( server->session, onEnabledChanged, server );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_rpcIsEnabled( const tr_rpc_server * server )
|
|
|
|
{
|
2008-09-25 18:48:09 +00:00
|
|
|
return server->isEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2008-10-01 04:26:38 +00:00
|
|
|
static void
|
|
|
|
restartServer( void * vserver )
|
|
|
|
{
|
|
|
|
tr_rpc_server * server = vserver;
|
|
|
|
|
|
|
|
if( server->isEnabled )
|
|
|
|
{
|
|
|
|
stopServer( server );
|
|
|
|
startServer( server );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_rpcSetPort( tr_rpc_server * server,
|
2008-12-01 20:51:01 +00:00
|
|
|
tr_port port )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
if( server->port != port )
|
|
|
|
{
|
|
|
|
server->port = port;
|
|
|
|
|
2008-09-03 19:59:09 +00:00
|
|
|
if( server->isEnabled )
|
2008-10-01 04:26:38 +00:00
|
|
|
tr_runInEventThread( server->session, restartServer, server );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-01 20:51:01 +00:00
|
|
|
tr_port
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_rpcGetPort( const tr_rpc_server * server )
|
|
|
|
{
|
|
|
|
return server->port;
|
|
|
|
}
|
|
|
|
|
2008-09-25 18:48:09 +00:00
|
|
|
void
|
2008-10-01 20:23:57 +00:00
|
|
|
tr_rpcSetWhitelist( tr_rpc_server * server,
|
2008-12-21 19:23:41 +00:00
|
|
|
const char * whitelistStr )
|
|
|
|
{
|
|
|
|
void * tmp;
|
|
|
|
const char * walk;
|
|
|
|
|
|
|
|
/* keep the string */
|
|
|
|
tr_free( server->whitelistStr );
|
|
|
|
server->whitelistStr = tr_strdup( whitelistStr );
|
|
|
|
|
|
|
|
/* clear out the old whitelist entries */
|
|
|
|
while(( tmp = tr_list_pop_front( &server->whitelist )))
|
|
|
|
tr_free( tmp );
|
|
|
|
|
|
|
|
/* 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 );
|
2008-12-21 19:35:38 +00:00
|
|
|
if( strcspn( token, "+-" ) < len )
|
|
|
|
tr_ninf( MY_NAME, "Adding address to whitelist: %s (And it has a '+' or '-'! Are you using an old ACL by mistake?)", token );
|
|
|
|
else
|
|
|
|
tr_ninf( MY_NAME, "Adding address to whitelist: %s", token );
|
|
|
|
|
2008-12-21 19:23:41 +00:00
|
|
|
if( walk[len]=='\0' )
|
|
|
|
break;
|
|
|
|
walk += len + 1;
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
char*
|
2008-10-01 20:23:57 +00:00
|
|
|
tr_rpcGetWhitelist( const tr_rpc_server * server )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-12-21 19:23:41 +00:00
|
|
|
return tr_strdup( server->whitelistStr ? server->whitelistStr : "" );
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2008-10-01 22:59:29 +00:00
|
|
|
void
|
|
|
|
tr_rpcSetWhitelistEnabled( tr_rpc_server * server,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isEnabled )
|
2008-10-01 22:59:29 +00:00
|
|
|
{
|
|
|
|
server->isWhitelistEnabled = isEnabled != 0;
|
|
|
|
}
|
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool
|
2008-10-01 22:59:29 +00:00
|
|
|
tr_rpcGetWhitelistEnabled( const tr_rpc_server * server )
|
|
|
|
{
|
|
|
|
return server->isWhitelistEnabled;
|
|
|
|
}
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
/****
|
|
|
|
***** PASSWORD
|
|
|
|
****/
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_rpcSetUsername( tr_rpc_server * server,
|
|
|
|
const char * username )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
|
|
|
tr_free( server->username );
|
|
|
|
server->username = tr_strdup( username );
|
|
|
|
dbgmsg( "setting our Username to [%s]", server->username );
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_rpcGetUsername( const tr_rpc_server * server )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
|
|
|
return tr_strdup( server->username ? server->username : "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_rpcSetPassword( tr_rpc_server * server,
|
|
|
|
const char * password )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
|
|
|
tr_free( server->password );
|
2009-03-24 01:39:06 +00:00
|
|
|
if( *password != '{' )
|
|
|
|
server->password = tr_ssha1( password );
|
|
|
|
else
|
|
|
|
server->password = strdup( password );
|
2008-06-05 16:23:03 +00:00
|
|
|
dbgmsg( "setting our Password to [%s]", server->password );
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_rpcGetPassword( const tr_rpc_server * server )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
|
|
|
return tr_strdup( server->password ? server->password : "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_rpcSetPasswordEnabled( tr_rpc_server * server,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isEnabled )
|
2008-06-05 16:23:03 +00:00
|
|
|
{
|
2008-12-13 23:39:12 +00:00
|
|
|
server->isPasswordEnabled = isEnabled;
|
|
|
|
dbgmsg( "setting 'password enabled' to %d", (int)isEnabled );
|
2008-06-05 16:23:03 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool
|
2008-06-05 16:23:03 +00:00
|
|
|
tr_rpcIsPasswordEnabled( const tr_rpc_server * server )
|
|
|
|
{
|
|
|
|
return server->isPasswordEnabled;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 16:23:03 +00:00
|
|
|
/****
|
|
|
|
***** LIFE CYCLE
|
|
|
|
****/
|
|
|
|
|
2008-10-01 04:26:38 +00:00
|
|
|
static void
|
|
|
|
closeServer( void * vserver )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-12-21 19:23:41 +00:00
|
|
|
void * tmp;
|
2008-10-01 04:26:38 +00:00
|
|
|
tr_rpc_server * s = vserver;
|
2008-10-01 20:23:57 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
stopServer( s );
|
2008-12-21 19:23:41 +00:00
|
|
|
while(( tmp = tr_list_pop_front( &s->whitelist )))
|
|
|
|
tr_free( tmp );
|
2008-12-30 20:40:48 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
deflateEnd( &s->stream );
|
|
|
|
#endif
|
2008-12-21 19:23:41 +00:00
|
|
|
tr_free( s->whitelistStr );
|
2008-07-16 19:36:46 +00:00
|
|
|
tr_free( s->username );
|
|
|
|
tr_free( s->password );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_free( s );
|
|
|
|
}
|
|
|
|
|
2008-10-01 04:26:38 +00:00
|
|
|
void
|
|
|
|
tr_rpcClose( tr_rpc_server ** ps )
|
|
|
|
{
|
2008-10-01 20:23:57 +00:00
|
|
|
tr_runInEventThread( ( *ps )->session, closeServer, *ps );
|
2008-10-01 04:26:38 +00:00
|
|
|
*ps = NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_rpc_server *
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_rpcInit( tr_session * session,
|
2009-01-13 16:35:06 +00:00
|
|
|
tr_benc * settings )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-06-02 19:57:16 +00:00
|
|
|
tr_rpc_server * s;
|
2009-01-13 16:35:06 +00:00
|
|
|
tr_bool found;
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bool boolVal;
|
2009-01-13 16:35:06 +00:00
|
|
|
int64_t i;
|
|
|
|
const char *str;
|
2008-06-02 19:57:16 +00:00
|
|
|
|
|
|
|
s = tr_new0( tr_rpc_server, 1 );
|
2008-05-18 16:44:30 +00:00
|
|
|
s->session = session;
|
2009-01-13 16:35:06 +00:00
|
|
|
|
2009-03-29 23:05:32 +00:00
|
|
|
found = tr_bencDictFindBool( settings, TR_PREFS_KEY_RPC_ENABLED, &boolVal );
|
2009-01-13 16:35:06 +00:00
|
|
|
assert( found );
|
2009-03-29 23:05:32 +00:00
|
|
|
s->isEnabled = boolVal;
|
2009-01-13 16:35:06 +00:00
|
|
|
|
|
|
|
found = tr_bencDictFindInt( settings, TR_PREFS_KEY_RPC_PORT, &i );
|
|
|
|
assert( found );
|
|
|
|
s->port = i;
|
|
|
|
|
2009-03-29 23:05:32 +00:00
|
|
|
found = tr_bencDictFindBool( settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, &boolVal );
|
2009-01-13 16:35:06 +00:00
|
|
|
assert( found );
|
2009-03-29 23:05:32 +00:00
|
|
|
s->isWhitelistEnabled = boolVal;
|
2009-01-13 16:35:06 +00:00
|
|
|
|
2009-03-29 23:05:32 +00:00
|
|
|
found = tr_bencDictFindBool( settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &boolVal );
|
2009-01-13 16:35:06 +00:00
|
|
|
assert( found );
|
2009-03-29 23:05:32 +00:00
|
|
|
s->isPasswordEnabled = boolVal;
|
2009-01-13 16:35:06 +00:00
|
|
|
|
|
|
|
found = tr_bencDictFindStr( settings, TR_PREFS_KEY_RPC_WHITELIST, &str );
|
|
|
|
assert( found );
|
|
|
|
tr_rpcSetWhitelist( s, str ? str : "127.0.0.1" );
|
|
|
|
|
|
|
|
found = tr_bencDictFindStr( settings, TR_PREFS_KEY_RPC_USERNAME, &str );
|
|
|
|
assert( found );
|
|
|
|
s->username = tr_strdup( str );
|
|
|
|
|
|
|
|
found = tr_bencDictFindStr( settings, TR_PREFS_KEY_RPC_PASSWORD, &str );
|
|
|
|
assert( found );
|
2009-03-24 01:39:06 +00:00
|
|
|
if( *str != '{' )
|
|
|
|
s->password = tr_ssha1( str );
|
|
|
|
else
|
|
|
|
s->password = strdup( str );
|
2008-12-30 20:40:48 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
s->stream.zalloc = (alloc_func) Z_NULL;
|
|
|
|
s->stream.zfree = (free_func) Z_NULL;
|
|
|
|
s->stream.opaque = (voidpf) Z_NULL;
|
|
|
|
deflateInit( &s->stream, Z_BEST_COMPRESSION );
|
|
|
|
#endif
|
|
|
|
|
2009-01-13 16:35:06 +00:00
|
|
|
if( s->isEnabled )
|
2008-12-16 16:13:21 +00:00
|
|
|
{
|
2009-01-13 16:35:06 +00:00
|
|
|
tr_ninf( MY_NAME, _( "Serving RPC and Web requests on port %d" ), (int) s->port );
|
|
|
|
tr_runInEventThread( session, startServer, s );
|
2008-12-16 16:13:21 +00:00
|
|
|
|
2009-01-13 16:35:06 +00:00
|
|
|
if( s->isWhitelistEnabled )
|
2008-12-21 19:23:41 +00:00
|
|
|
tr_ninf( MY_NAME, _( "Whitelist enabled" ) );
|
2008-12-16 16:13:21 +00:00
|
|
|
|
2009-01-13 16:35:06 +00:00
|
|
|
if( s->isPasswordEnabled )
|
2008-12-16 16:13:21 +00:00
|
|
|
tr_ninf( MY_NAME, _( "Password required" ) );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
return s;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|