1 | /* |
---|
2 | * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: rpc-server.c 6334 2008-07-15 17:16:57Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <ctype.h> /* isdigit */ |
---|
15 | #include <errno.h> |
---|
16 | #include <stdlib.h> /* strtol */ |
---|
17 | #include <string.h> |
---|
18 | |
---|
19 | #include <unistd.h> /* unlink */ |
---|
20 | |
---|
21 | #include <libevent/event.h> |
---|
22 | #include <shttpd/defs.h> /* edit_passwords */ |
---|
23 | #include <shttpd/shttpd.h> |
---|
24 | |
---|
25 | #include "transmission.h" |
---|
26 | #include "bencode.h" |
---|
27 | #include "platform.h" |
---|
28 | #include "rpc.h" |
---|
29 | #include "rpc-server.h" |
---|
30 | #include "utils.h" |
---|
31 | |
---|
32 | #define MY_NAME "RPC Server" |
---|
33 | #define MY_REALM "Transmission RPC Server" |
---|
34 | |
---|
35 | #define BUSY_INTERVAL_MSEC 30 |
---|
36 | #define IDLE_INTERVAL_MSEC 66 |
---|
37 | #define UNUSED_INTERVAL_MSEC 100 |
---|
38 | |
---|
39 | struct tr_rpc_server |
---|
40 | { |
---|
41 | int port; |
---|
42 | time_t lastRequestTime; |
---|
43 | struct shttpd_ctx * ctx; |
---|
44 | tr_handle * session; |
---|
45 | struct evbuffer * in; |
---|
46 | struct evbuffer * out; |
---|
47 | struct event timer; |
---|
48 | int isPasswordEnabled; |
---|
49 | char * username; |
---|
50 | char * password; |
---|
51 | char * acl; |
---|
52 | }; |
---|
53 | |
---|
54 | #define dbgmsg(fmt...) tr_deepLog(__FILE__, __LINE__, MY_NAME, ##fmt ) |
---|
55 | |
---|
56 | static const char* |
---|
57 | tr_memmem( const char * s1, size_t l1, |
---|
58 | const char * s2, size_t l2 ) |
---|
59 | { |
---|
60 | if (!l2) return s1; |
---|
61 | while (l1 >= l2) { |
---|
62 | l1--; |
---|
63 | if (!memcmp(s1,s2,l2)) |
---|
64 | return s1; |
---|
65 | s1++; |
---|
66 | } |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | |
---|
70 | static void |
---|
71 | handle_upload( struct shttpd_arg * arg ) |
---|
72 | { |
---|
73 | struct tr_rpc_server * s = arg->user_data; |
---|
74 | s->lastRequestTime = time( NULL ); |
---|
75 | |
---|
76 | /* if we haven't parsed the POST, do that now */ |
---|
77 | if( !EVBUFFER_LENGTH( s->out ) ) |
---|
78 | { |
---|
79 | /* if we haven't finished reading the POST, read more now */ |
---|
80 | evbuffer_add( s->in, arg->in.buf, arg->in.len ); |
---|
81 | arg->in.num_bytes = arg->in.len; |
---|
82 | if( arg->flags & SHTTPD_MORE_POST_DATA ) |
---|
83 | return; |
---|
84 | |
---|
85 | const char * query_string = shttpd_get_env( arg, "QUERY_STRING" ); |
---|
86 | const char * content_type = shttpd_get_header( arg, "Content-Type" ); |
---|
87 | const char * delim; |
---|
88 | const char * in = (const char *) EVBUFFER_DATA( s->in ); |
---|
89 | size_t inlen = EVBUFFER_LENGTH( s->in ); |
---|
90 | char * boundary = tr_strdup_printf( "--%s", strstr( content_type, "boundary=" ) + strlen( "boundary=" ) ); |
---|
91 | const size_t boundary_len = strlen( boundary ); |
---|
92 | char buf[64]; |
---|
93 | int paused = ( query_string != NULL ) |
---|
94 | && ( shttpd_get_var( "paused", query_string, strlen( query_string ), buf, sizeof( buf ) ) == 4 ) |
---|
95 | && ( !strcmp( buf, "true" ) ); |
---|
96 | |
---|
97 | delim = tr_memmem( in, inlen, boundary, boundary_len ); |
---|
98 | if( delim ) do |
---|
99 | { |
---|
100 | size_t part_len; |
---|
101 | const char * part = delim + boundary_len; |
---|
102 | inlen -= ( part - in ); |
---|
103 | in = part; |
---|
104 | delim = tr_memmem( in, inlen, boundary, boundary_len ); |
---|
105 | part_len = delim ? (size_t)(delim-part) : inlen; |
---|
106 | |
---|
107 | if( part_len ) |
---|
108 | { |
---|
109 | char * text = tr_strndup( part, part_len ); |
---|
110 | if( strstr( text, "filename=\"" ) ) |
---|
111 | { |
---|
112 | const char * body = strstr( text, "\r\n\r\n" ); |
---|
113 | if( body ) |
---|
114 | { |
---|
115 | int err; |
---|
116 | tr_ctor * ctor; |
---|
117 | size_t body_len; |
---|
118 | body += 4; |
---|
119 | body_len = part_len - ( body - text ); |
---|
120 | if( body_len >= 2 && !memcmp(&body[body_len-2],"\r\n",2) ) |
---|
121 | body_len -= 2; |
---|
122 | |
---|
123 | ctor = tr_ctorNew( s->session ); |
---|
124 | tr_ctorSetMetainfo( ctor, (void*)body, body_len ); |
---|
125 | tr_ctorSetPaused( ctor, TR_FORCE, paused ); |
---|
126 | tr_torrentNew( s->session, ctor, &err ); |
---|
127 | tr_ctorFree( ctor ); |
---|
128 | } |
---|
129 | } |
---|
130 | tr_free( text ); |
---|
131 | } |
---|
132 | } |
---|
133 | while( delim ); |
---|
134 | |
---|
135 | evbuffer_drain( s->in, EVBUFFER_LENGTH( s->in ) ); |
---|
136 | tr_free( boundary ); |
---|
137 | |
---|
138 | { |
---|
139 | /* use xml here because json responses to file uploads is trouble. |
---|
140 | * see http://www.malsup.com/jquery/form/#sample7 for details */ |
---|
141 | const char * response = "<result>success</result>"; |
---|
142 | const int len = strlen( response ); |
---|
143 | evbuffer_add_printf( s->out, "HTTP/1.1 200 OK\r\n" |
---|
144 | "Content-Type: text/xml\r\n" |
---|
145 | "Content-Length: %d\r\n" |
---|
146 | "\r\n" |
---|
147 | "%s\r\n", len, response ); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | if( EVBUFFER_LENGTH( s->out ) ) |
---|
152 | { |
---|
153 | const int n = MIN( ( int )EVBUFFER_LENGTH( s->out ), arg->out.len ); |
---|
154 | memcpy( arg->out.buf, EVBUFFER_DATA( s->out ), n ); |
---|
155 | evbuffer_drain( s->out, n ); |
---|
156 | arg->out.num_bytes = n; |
---|
157 | } |
---|
158 | |
---|
159 | if( !EVBUFFER_LENGTH( s->out ) ) |
---|
160 | arg->flags |= SHTTPD_END_OF_OUTPUT; |
---|
161 | } |
---|
162 | |
---|
163 | static void |
---|
164 | handle_rpc( struct shttpd_arg * arg ) |
---|
165 | { |
---|
166 | struct tr_rpc_server * s = arg->user_data; |
---|
167 | s->lastRequestTime = time( NULL ); |
---|
168 | |
---|
169 | if( !EVBUFFER_LENGTH( s->out ) ) |
---|
170 | { |
---|
171 | int len = 0; |
---|
172 | char * response = NULL; |
---|
173 | const char * request_method = shttpd_get_env( arg, "REQUEST_METHOD" ); |
---|
174 | const char * query_string = shttpd_get_env( arg, "QUERY_STRING" ); |
---|
175 | |
---|
176 | if( query_string && *query_string ) |
---|
177 | response = tr_rpc_request_exec_uri( s->session, |
---|
178 | query_string, |
---|
179 | strlen( query_string ), |
---|
180 | &len ); |
---|
181 | else if( !strcmp( request_method, "POST" ) ) |
---|
182 | { |
---|
183 | evbuffer_add( s->in, arg->in.buf, arg->in.len ); |
---|
184 | arg->in.num_bytes = arg->in.len; |
---|
185 | if( arg->flags & SHTTPD_MORE_POST_DATA ) |
---|
186 | return; |
---|
187 | response = tr_rpc_request_exec_json( s->session, |
---|
188 | EVBUFFER_DATA( s->in ), |
---|
189 | EVBUFFER_LENGTH( s->in ), |
---|
190 | &len ); |
---|
191 | evbuffer_drain( s->in, EVBUFFER_LENGTH( s->in ) ); |
---|
192 | } |
---|
193 | |
---|
194 | evbuffer_add_printf( s->out, "HTTP/1.1 200 OK\r\n" |
---|
195 | "Content-Type: application/json\r\n" |
---|
196 | "Content-Length: %d\r\n" |
---|
197 | "\r\n" |
---|
198 | "%*.*s", len, len, len, response ); |
---|
199 | tr_free( response ); |
---|
200 | } |
---|
201 | |
---|
202 | if( EVBUFFER_LENGTH( s->out ) ) |
---|
203 | { |
---|
204 | const int n = MIN( ( int )EVBUFFER_LENGTH( s->out ), arg->out.len ); |
---|
205 | memcpy( arg->out.buf, EVBUFFER_DATA( s->out ), n ); |
---|
206 | evbuffer_drain( s->out, n ); |
---|
207 | arg->out.num_bytes = n; |
---|
208 | } |
---|
209 | |
---|
210 | if( !EVBUFFER_LENGTH( s->out ) ) |
---|
211 | arg->flags |= SHTTPD_END_OF_OUTPUT; |
---|
212 | } |
---|
213 | |
---|
214 | static void |
---|
215 | rpcPulse( int socket UNUSED, short action UNUSED, void * vserver ) |
---|
216 | { |
---|
217 | int interval; |
---|
218 | struct timeval tv; |
---|
219 | tr_rpc_server * server = vserver; |
---|
220 | const time_t now = time( NULL ); |
---|
221 | |
---|
222 | assert( server ); |
---|
223 | |
---|
224 | if( server->ctx ) |
---|
225 | shttpd_poll( server->ctx, 1 ); |
---|
226 | |
---|
227 | /* set a timer for the next pulse */ |
---|
228 | if( EVBUFFER_LENGTH( server->in ) || EVBUFFER_LENGTH( server->out ) ) |
---|
229 | interval = BUSY_INTERVAL_MSEC; |
---|
230 | else if( now - server->lastRequestTime < 300 ) |
---|
231 | interval = IDLE_INTERVAL_MSEC; |
---|
232 | else |
---|
233 | interval = UNUSED_INTERVAL_MSEC; |
---|
234 | tv = tr_timevalMsec( interval ); |
---|
235 | evtimer_add( &server->timer, &tv ); |
---|
236 | } |
---|
237 | |
---|
238 | static void |
---|
239 | getPasswordFile( tr_rpc_server * server, char * buf, int buflen ) |
---|
240 | { |
---|
241 | tr_buildPath( buf, buflen, tr_sessionGetConfigDir( server->session ), |
---|
242 | "htpasswd", |
---|
243 | NULL ); |
---|
244 | } |
---|
245 | |
---|
246 | static void |
---|
247 | startServer( tr_rpc_server * server ) |
---|
248 | { |
---|
249 | dbgmsg( "in startServer; current context is %p", server->ctx ); |
---|
250 | |
---|
251 | if( !server->ctx ) |
---|
252 | { |
---|
253 | char ports[128]; |
---|
254 | char passwd[MAX_PATH_LENGTH]; |
---|
255 | const char * clutchDir = tr_getClutchDir( server->session ); |
---|
256 | struct timeval tv = tr_timevalMsec( UNUSED_INTERVAL_MSEC ); |
---|
257 | |
---|
258 | getPasswordFile( server, passwd, sizeof( passwd ) ); |
---|
259 | if( !server->isPasswordEnabled ) |
---|
260 | unlink( passwd ); |
---|
261 | else |
---|
262 | edit_passwords( passwd, MY_REALM, server->username, server->password ); |
---|
263 | |
---|
264 | server->ctx = shttpd_init( ); |
---|
265 | tr_snprintf( ports, sizeof( ports ), "%d", server->port ); |
---|
266 | shttpd_register_uri( server->ctx, "/transmission/rpc", handle_rpc, server ); |
---|
267 | shttpd_register_uri( server->ctx, "/transmission/upload", handle_upload, server ); |
---|
268 | |
---|
269 | if( clutchDir && *clutchDir ) { |
---|
270 | char * clutchAlias = tr_strdup_printf( "%s=%s,%s=%s", |
---|
271 | "/transmission/clutch", clutchDir, |
---|
272 | "/transmission/web", clutchDir ); |
---|
273 | tr_inf( _( "Serving the web interface files from \"%s\"" ), clutchDir ); |
---|
274 | shttpd_set_option( server->ctx, "aliases", clutchAlias ); |
---|
275 | tr_free( clutchAlias ); |
---|
276 | } |
---|
277 | |
---|
278 | shttpd_set_option( server->ctx, "ports", ports ); |
---|
279 | shttpd_set_option( server->ctx, "dir_list", "0" ); |
---|
280 | //shttpd_set_option( server->ctx, "root", "/dev/null" ); |
---|
281 | shttpd_set_option( server->ctx, "auth_realm", MY_REALM ); |
---|
282 | if( server->acl ) { |
---|
283 | dbgmsg( "setting acl [%s]", server->acl ); |
---|
284 | shttpd_set_option( server->ctx, "acl", server->acl ); |
---|
285 | } |
---|
286 | if( server->isPasswordEnabled ) { |
---|
287 | char * buf = tr_strdup_printf( "/transmission/rpc=%s," |
---|
288 | "/transmission/upload=%s", passwd, passwd ); |
---|
289 | shttpd_set_option( server->ctx, "protect", buf ); |
---|
290 | tr_free( buf ); |
---|
291 | } |
---|
292 | |
---|
293 | evtimer_set( &server->timer, rpcPulse, server ); |
---|
294 | evtimer_add( &server->timer, &tv ); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | static void |
---|
299 | stopServer( tr_rpc_server * server ) |
---|
300 | { |
---|
301 | if( server->ctx ) |
---|
302 | { |
---|
303 | char passwd[MAX_PATH_LENGTH]; |
---|
304 | getPasswordFile( server, passwd, sizeof( passwd ) ); |
---|
305 | unlink( passwd ); |
---|
306 | |
---|
307 | evtimer_del( &server->timer ); |
---|
308 | shttpd_fini( server->ctx ); |
---|
309 | server->ctx = NULL; |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | void |
---|
314 | tr_rpcSetEnabled( tr_rpc_server * server, int isEnabled ) |
---|
315 | { |
---|
316 | if( !isEnabled && server->ctx ) |
---|
317 | stopServer( server ); |
---|
318 | |
---|
319 | if( isEnabled && !server->ctx ) |
---|
320 | startServer( server ); |
---|
321 | } |
---|
322 | |
---|
323 | int |
---|
324 | tr_rpcIsEnabled( const tr_rpc_server * server ) |
---|
325 | { |
---|
326 | return server->ctx != NULL; |
---|
327 | } |
---|
328 | |
---|
329 | void |
---|
330 | tr_rpcSetPort( tr_rpc_server * server, int port ) |
---|
331 | { |
---|
332 | if( server->port != port ) |
---|
333 | { |
---|
334 | server->port = port; |
---|
335 | |
---|
336 | if( server->ctx ) |
---|
337 | { |
---|
338 | stopServer( server ); |
---|
339 | startServer( server ); |
---|
340 | } |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | int |
---|
345 | tr_rpcGetPort( const tr_rpc_server * server ) |
---|
346 | { |
---|
347 | return server->port; |
---|
348 | } |
---|
349 | |
---|
350 | /**** |
---|
351 | ***** ACL |
---|
352 | ****/ |
---|
353 | |
---|
354 | /* |
---|
355 | * DELIM_CHARS, FOR_EACH_WORD_IN_LIST, isbyte, and testACL are from, or modified from, |
---|
356 | * shttpd, written by Sergey Lyubka under this license: |
---|
357 | * "THE BEER-WARE LICENSE" (Revision 42): |
---|
358 | * Sergey Lyubka wrote this file. As long as you retain this notice you |
---|
359 | * can do whatever you want with this stuff. If we meet some day, and you think |
---|
360 | * this stuff is worth it, you can buy me a beer in return. |
---|
361 | */ |
---|
362 | |
---|
363 | #define DELIM_CHARS "," /* Separators for lists */ |
---|
364 | |
---|
365 | #define FOR_EACH_WORD_IN_LIST(s,len) \ |
---|
366 | for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0; \ |
---|
367 | s += len, s+= strspn(s, DELIM_CHARS)) |
---|
368 | |
---|
369 | static int isbyte(int n) { return (n >= 0 && n <= 255); } |
---|
370 | |
---|
371 | static char* |
---|
372 | testACL( const char * s ) |
---|
373 | { |
---|
374 | int len; |
---|
375 | |
---|
376 | FOR_EACH_WORD_IN_LIST(s, len) |
---|
377 | { |
---|
378 | |
---|
379 | char flag; |
---|
380 | int a, b, c, d, n, mask; |
---|
381 | |
---|
382 | if( sscanf(s, "%c%d.%d.%d.%d%n",&flag,&a,&b,&c,&d,&n) != 5 ) |
---|
383 | return tr_strdup_printf( _( "[%s]: subnet must be [+|-]x.x.x.x[/x]" ), s ); |
---|
384 | if( flag != '+' && flag != '-') |
---|
385 | return tr_strdup_printf( _( "[%s]: flag must be + or -" ), s ); |
---|
386 | if( !isbyte(a) || !isbyte(b) || !isbyte(c) || !isbyte(d) ) |
---|
387 | return tr_strdup_printf( _( "[%s]: bad ip address" ), s ); |
---|
388 | if( sscanf(s + n, "/%d", &mask) == 1 && ( mask<0 || mask>32 ) ) |
---|
389 | return tr_strdup_printf( _( "[%s]: bad subnet mask %d" ), s, n ); |
---|
390 | } |
---|
391 | |
---|
392 | return NULL; |
---|
393 | } |
---|
394 | |
---|
395 | /* 192.*.*.* --> 192.0.0.0/8 |
---|
396 | 192.64.*.* --> 192.64.0.0/16 |
---|
397 | 192.64.1.* --> 192.64.1.0/24 |
---|
398 | 192.64.1.2 --> 192.64.1.2/32 */ |
---|
399 | static void |
---|
400 | cidrizeOne( const char * in, int len, struct evbuffer * out ) |
---|
401 | { |
---|
402 | int stars = 0; |
---|
403 | const char * pch; |
---|
404 | const char * end; |
---|
405 | char zero = '0'; |
---|
406 | char huh = '?'; |
---|
407 | |
---|
408 | for( pch=in, end=pch+len; pch!=end; ++pch ) { |
---|
409 | if( stars && isdigit(*pch) ) |
---|
410 | evbuffer_add( out, &huh, 1 ); |
---|
411 | else if( *pch!='*' ) |
---|
412 | evbuffer_add( out, pch, 1 ); |
---|
413 | else { |
---|
414 | evbuffer_add( out, &zero, 1 ); |
---|
415 | ++stars; |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | evbuffer_add_printf( out, "/%d", (32-(stars*8))); |
---|
420 | } |
---|
421 | |
---|
422 | char* |
---|
423 | cidrize( const char * acl ) |
---|
424 | { |
---|
425 | int len; |
---|
426 | const char * walk = acl; |
---|
427 | char * ret; |
---|
428 | struct evbuffer * out = evbuffer_new( ); |
---|
429 | |
---|
430 | FOR_EACH_WORD_IN_LIST( walk, len ) |
---|
431 | { |
---|
432 | cidrizeOne( walk, len, out ); |
---|
433 | evbuffer_add_printf( out, "," ); |
---|
434 | } |
---|
435 | |
---|
436 | /* the -1 is to eat the final ", " */ |
---|
437 | ret = tr_strndup( (char*) EVBUFFER_DATA(out), EVBUFFER_LENGTH(out)-1 ); |
---|
438 | evbuffer_free( out ); |
---|
439 | return ret; |
---|
440 | } |
---|
441 | |
---|
442 | int |
---|
443 | tr_rpcTestACL( const tr_rpc_server * server UNUSED, |
---|
444 | const char * acl, |
---|
445 | char ** setme_errmsg ) |
---|
446 | { |
---|
447 | int err = 0; |
---|
448 | char * cidr = cidrize( acl ); |
---|
449 | char * errmsg = testACL( cidr ); |
---|
450 | if( errmsg ) |
---|
451 | { |
---|
452 | if( setme_errmsg ) |
---|
453 | *setme_errmsg = errmsg; |
---|
454 | else |
---|
455 | tr_free( errmsg ); |
---|
456 | err = -1; |
---|
457 | } |
---|
458 | tr_free( cidr ); |
---|
459 | return err; |
---|
460 | } |
---|
461 | |
---|
462 | int |
---|
463 | tr_rpcSetACL( tr_rpc_server * server, |
---|
464 | const char * acl, |
---|
465 | char ** setme_errmsg ) |
---|
466 | { |
---|
467 | char * cidr = cidrize( acl ); |
---|
468 | const int err = tr_rpcTestACL( server, cidr, setme_errmsg ); |
---|
469 | |
---|
470 | if( !err ) |
---|
471 | { |
---|
472 | const int isRunning = server->ctx != NULL; |
---|
473 | |
---|
474 | if( isRunning ) |
---|
475 | stopServer( server ); |
---|
476 | |
---|
477 | tr_free( server->acl ); |
---|
478 | server->acl = tr_strdup( cidr ); |
---|
479 | dbgmsg( "setting our ACL to [%s]", server->acl ); |
---|
480 | |
---|
481 | if( isRunning ) |
---|
482 | startServer( server ); |
---|
483 | } |
---|
484 | tr_free( cidr ); |
---|
485 | |
---|
486 | return err; |
---|
487 | } |
---|
488 | |
---|
489 | char* |
---|
490 | tr_rpcGetACL( const tr_rpc_server * server ) |
---|
491 | { |
---|
492 | return tr_strdup( server->acl ? server->acl : "" ); |
---|
493 | } |
---|
494 | |
---|
495 | /**** |
---|
496 | ***** PASSWORD |
---|
497 | ****/ |
---|
498 | |
---|
499 | void |
---|
500 | tr_rpcSetUsername( tr_rpc_server * server, |
---|
501 | const char * username ) |
---|
502 | { |
---|
503 | const int isRunning = server->ctx != NULL; |
---|
504 | |
---|
505 | if( isRunning ) |
---|
506 | stopServer( server ); |
---|
507 | |
---|
508 | tr_free( server->username ); |
---|
509 | server->username = tr_strdup( username ); |
---|
510 | dbgmsg( "setting our Username to [%s]", server->username ); |
---|
511 | |
---|
512 | if( isRunning ) |
---|
513 | startServer( server ); |
---|
514 | } |
---|
515 | |
---|
516 | char* |
---|
517 | tr_rpcGetUsername( const tr_rpc_server * server ) |
---|
518 | { |
---|
519 | return tr_strdup( server->username ? server->username : "" ); |
---|
520 | } |
---|
521 | |
---|
522 | void |
---|
523 | tr_rpcSetPassword( tr_rpc_server * server, |
---|
524 | const char * password ) |
---|
525 | { |
---|
526 | const int isRunning = server->ctx != NULL; |
---|
527 | |
---|
528 | if( isRunning ) |
---|
529 | stopServer( server ); |
---|
530 | |
---|
531 | tr_free( server->password ); |
---|
532 | server->password = tr_strdup( password ); |
---|
533 | dbgmsg( "setting our Password to [%s]", server->password ); |
---|
534 | |
---|
535 | if( isRunning ) |
---|
536 | startServer( server ); |
---|
537 | } |
---|
538 | |
---|
539 | char* |
---|
540 | tr_rpcGetPassword( const tr_rpc_server * server ) |
---|
541 | { |
---|
542 | return tr_strdup( server->password ? server->password : "" ); |
---|
543 | } |
---|
544 | |
---|
545 | void |
---|
546 | tr_rpcSetPasswordEnabled( tr_rpc_server * server, |
---|
547 | int isEnabled ) |
---|
548 | { |
---|
549 | const int isRunning = server->ctx != NULL; |
---|
550 | |
---|
551 | if( isRunning ) |
---|
552 | stopServer( server ); |
---|
553 | |
---|
554 | server->isPasswordEnabled = isEnabled; |
---|
555 | dbgmsg( "setting 'password enabled' to %d", isEnabled ); |
---|
556 | |
---|
557 | if( isRunning ) |
---|
558 | startServer( server ); |
---|
559 | } |
---|
560 | |
---|
561 | int |
---|
562 | tr_rpcIsPasswordEnabled( const tr_rpc_server * server ) |
---|
563 | { |
---|
564 | return server->isPasswordEnabled; |
---|
565 | } |
---|
566 | |
---|
567 | /**** |
---|
568 | ***** LIFE CYCLE |
---|
569 | ****/ |
---|
570 | |
---|
571 | void |
---|
572 | tr_rpcClose( tr_rpc_server ** ps ) |
---|
573 | { |
---|
574 | tr_rpc_server * s = *ps; |
---|
575 | *ps = NULL; |
---|
576 | |
---|
577 | stopServer( s ); |
---|
578 | evbuffer_free( s->in ); |
---|
579 | evbuffer_free( s->out ); |
---|
580 | tr_free( s->acl ); |
---|
581 | tr_free( s ); |
---|
582 | } |
---|
583 | |
---|
584 | tr_rpc_server * |
---|
585 | tr_rpcInit( tr_handle * session, |
---|
586 | int isEnabled, |
---|
587 | int port, |
---|
588 | const char * acl, |
---|
589 | int isPasswordEnabled, |
---|
590 | const char * username, |
---|
591 | const char * password ) |
---|
592 | { |
---|
593 | char * errmsg; |
---|
594 | tr_rpc_server * s; |
---|
595 | |
---|
596 | if(( errmsg = testACL ( acl ))) |
---|
597 | { |
---|
598 | tr_nerr( MY_NAME, errmsg ); |
---|
599 | tr_free( errmsg ); |
---|
600 | acl = TR_DEFAULT_RPC_ACL; |
---|
601 | tr_nerr( MY_NAME, "using fallback ACL \"%s\"", acl ); |
---|
602 | } |
---|
603 | |
---|
604 | s = tr_new0( tr_rpc_server, 1 ); |
---|
605 | s->session = session; |
---|
606 | s->port = port; |
---|
607 | s->in = evbuffer_new( ); |
---|
608 | s->out = evbuffer_new( ); |
---|
609 | s->acl = tr_strdup( acl ); |
---|
610 | s->username = tr_strdup( username ); |
---|
611 | s->password = tr_strdup( password ); |
---|
612 | s->isPasswordEnabled = isPasswordEnabled; |
---|
613 | |
---|
614 | if( isEnabled ) |
---|
615 | startServer( s ); |
---|
616 | return s; |
---|
617 | } |
---|