Changeset 7449


Ignore:
Timestamp:
Dec 21, 2008, 7:23:41 PM (12 years ago)
Author:
charles
Message:

(trunk libT) add html and console hints about whitelist status & configuration

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/rpc-server.c

    r7412 r7449  
    5656    char *             username;
    5757    char *             password;
    58     char *             whitelist;
     58    char *             whitelistStr;
     59    tr_list *          whitelist;
    5960};
    6061
     
    409410}
    410411
    411 static int
     412static tr_bool
    412413isAddressAllowed( const tr_rpc_server * server,
    413414                  const char *          address )
    414415{
    415     const char * str;
     416    tr_list * l;
    416417
    417418    if( !server->isWhitelistEnabled )
    418         return 1;
    419 
    420     for( str = server->whitelist; str && *str; )
    421     {
    422         const char * delimiter = strchr( str, ',' );
    423         const int    len = delimiter ? delimiter - str : (int)strlen( str );
    424         char *       token = tr_strndup( str, len );
    425         const int    match = tr_wildmat( address, token );
    426         tr_free( token );
    427         if( match )
    428             return 1;
    429         if( !delimiter )
    430             break;
    431         str = delimiter + 1;
    432     }
    433 
    434     return 0;
     419        return TRUE;
     420
     421    for( l=server->whitelist; l!=NULL; l=l->next )
     422        if( tr_wildmat( address, l->data ) )
     423            return TRUE;
     424
     425    return FALSE;
    435426}
    436427
     
    464455        if( !isAddressAllowed( server, req->remote_host ) )
    465456        {
    466             send_simple_response( req, 401, "Unauthorized IP Address" );
     457            send_simple_response( req, 401,
     458                "<p>Unauthorized IP Address.</p>"
     459                "<p>Either disable the IP address whitelist or add your address to it.</p>"
     460                "<p>If you're editing settings.json, see the 'rpc-whitelist' and 'rpc-whitelist-enabled' entries.</p>" );
    467461        }
    468462        else if( server->isPasswordEnabled
     
    586580void
    587581tr_rpcSetWhitelist( tr_rpc_server * server,
    588                     const char *    whitelist )
    589 {
    590     tr_free( server->whitelist );
    591     server->whitelist = tr_strdup( whitelist );
     582                    const char    * whitelistStr )
     583{
     584    void * tmp;
     585    const char * walk;
     586
     587    /* keep the string */
     588    tr_free( server->whitelistStr );
     589    server->whitelistStr = tr_strdup( whitelistStr );
     590
     591    /* clear out the old whitelist entries */
     592    while(( tmp = tr_list_pop_front( &server->whitelist )))
     593        tr_free( tmp );
     594
     595    /* build the new whitelist entries */
     596    for( walk=whitelistStr; walk && *walk; ) {
     597        const char * delimiters = " ,;";
     598        const size_t len = strcspn( walk, delimiters );
     599        char * token = tr_strndup( walk, len );
     600        tr_list_append( &server->whitelist, token );
     601        tr_ninf( MY_NAME, "Adding address to whitelist: [%s]", token );
     602        if( walk[len]=='\0' )
     603            break;
     604        walk += len + 1;
     605    }
    592606}
    593607
     
    595609tr_rpcGetWhitelist( const tr_rpc_server * server )
    596610{
    597     return tr_strdup( server->whitelist ? server->whitelist : "" );
     611    return tr_strdup( server->whitelistStr ? server->whitelistStr : "" );
    598612}
    599613
     
    666680closeServer( void * vserver )
    667681{
     682    void * tmp;
    668683    tr_rpc_server * s = vserver;
    669684
    670685    stopServer( s );
    671     tr_free( s->whitelist );
     686    while(( tmp = tr_list_pop_front( &s->whitelist )))
     687        tr_free( tmp );
     688    tr_free( s->whitelistStr );
    672689    tr_free( s->username );
    673690    tr_free( s->password );
     
    697714    s->session = session;
    698715    s->port = port;
    699     s->whitelist = tr_strdup( whitelist && *whitelist ? whitelist : "127.0.0.1" );
    700716    s->username = tr_strdup( username );
    701717    s->password = tr_strdup( password );
    702     s->isWhitelistEnabled = isWhitelistEnabled != 0;
    703     s->isPasswordEnabled = isPasswordEnabled != 0;
     718    s->isWhitelistEnabled = isWhitelistEnabled;
     719    s->isPasswordEnabled = isPasswordEnabled;
    704720    s->isEnabled = isEnabled != 0;
     721    tr_rpcSetWhitelist( s, whitelist ? whitelist : "127.0.0.1" );
    705722    if( isEnabled )
    706723        tr_runInEventThread( session, startServer, s );
     
    711728
    712729        if( isWhitelistEnabled )
    713             tr_ninf( MY_NAME, _( "Whitelist is: %s" ), whitelist );
     730            tr_ninf( MY_NAME, _( "Whitelist enabled" ) );
    714731
    715732        if( isPasswordEnabled )
Note: See TracChangeset for help on using the changeset viewer.