Changeset 6004


Ignore:
Timestamp:
Jun 2, 2008, 7:44:19 PM (14 years ago)
Author:
charles
Message:

(libT): added an ACL tester to tr_sessionSetRPCACL() and return an error string if the ACL can't be parsed.

Location:
trunk/libtransmission
Files:
5 edited

Legend:

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

    r5959 r6004  
    176176}
    177177
    178 void
    179 tr_rpcSetACL( tr_rpc_server * server, const char * acl )
     178/*
     179 * DELIM_CHARS, FOR_EACH_WORD_IN_LIST, isbyte, and testACL are from, or modified from,
     180 * shttpd, written by Sergey Lyubka under this license:
     181 * "THE BEER-WARE LICENSE" (Revision 42):
     182 * Sergey Lyubka wrote this file.  As long as you retain this notice you
     183 * can do whatever you want with this stuff. If we meet some day, and you think
     184 * this stuff is worth it, you can buy me a beer in return.
     185 */
     186
     187#define  DELIM_CHARS " ," /* Separators for lists */
     188
     189#define FOR_EACH_WORD_IN_LIST(s,len)                                    \
     190        for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0;       \
     191                        s += len, s+= strspn(s, DELIM_CHARS))
     192
     193static int isbyte(int n) { return (n >= 0 && n <= 255); }
     194
     195static char*
     196testACL( const char * s )
     197{
     198    int len;
     199
     200    FOR_EACH_WORD_IN_LIST(s, len)
     201    {
     202
     203        char flag;
     204        int  a, b, c, d, n, mask;
     205
     206        if( sscanf(s, "%c%d.%d.%d.%d%n",&flag,&a,&b,&c,&d,&n) != 5 )
     207            return tr_strdup_printf( _( "[%s]: subnet must be [+|-]x.x.x.x[/x]" ), s );
     208        if( flag != '+' && flag != '-')
     209            return tr_strdup_printf( _( "[%s]: flag must be + or -" ), s );
     210        if( !isbyte(a) || !isbyte(b) || !isbyte(c) || !isbyte(d) )
     211            return tr_strdup_printf( _( "[%s]: bad ip address" ), s );
     212        if( sscanf(s + n, "/%d", &mask) == 1 && ( mask<0 || mask>32 ) )
     213            return tr_strdup_printf( _( "[%s]: bad subnet mask %d" ), s, n );
     214    }
     215
     216    return NULL;
     217}
     218
     219int
     220tr_rpcSetACL( tr_rpc_server * server, const char * acl, char ** setme_errmsg )
    180221{
    181222    const int isRunning = server->ctx != NULL;
    182 
    183     if( isRunning )
    184         stopServer( server );
    185 
    186     tr_free( server->acl );
    187     server->acl = tr_strdup( acl );
    188 
    189     if( isRunning )
    190         startServer( server );
     223    int ret = 0;
     224    char * errmsg = testACL( acl );
     225
     226    if( errmsg )
     227    {
     228        *setme_errmsg = errmsg;
     229        ret = -1;
     230    }
     231    else
     232    {
     233        if( isRunning )
     234            stopServer( server );
     235
     236        tr_free( server->acl );
     237        server->acl = tr_strdup( acl );
     238
     239        if( isRunning )
     240            startServer( server );
     241    }
     242
     243    return ret;
    191244}
    192245
  • trunk/libtransmission/rpc-server.h

    r5959 r6004  
    3333int         tr_rpcGetPort   ( const tr_rpc_server    * server );
    3434
    35 void        tr_rpcSetACL    ( tr_rpc_server          * server,
    36                               const char             * acl );
     35int         tr_rpcSetACL    ( tr_rpc_server          * server,
     36                              const char             * acl,
     37                              char                  ** allocme_errmsg );
    3738
    3839const char* tr_rpcGetACL    ( const tr_rpc_server    * server );
  • trunk/libtransmission/session.c

    r5981 r6004  
    776776}
    777777
    778 void
    779 tr_sessionSetRPCACL( tr_handle * session, const char * acl )
    780 {
    781     tr_rpcSetACL( session->rpcServer, acl );
     778int
     779tr_sessionSetRPCACL( tr_handle    * session,
     780                     const char   * acl,
     781                     char        ** allocme_errmsg )
     782{
     783    return tr_rpcSetACL( session->rpcServer, acl, allocme_errmsg );
    782784}
    783785
  • trunk/libtransmission/transmission.h

    r5994 r6004  
    301301 * The default string is "+127.0.0.1"
    302302 *
    303  * IMPORTANT: a malformed ACL is likely to cause Transmission to crash.
    304  * Client applications need to validate user input, or better yet
    305  * generate it from a higher-level interface that doesn't allow user error,
    306  * before calling this function.
     303 * @param acl the new ACL to use.
     304 * @param allocme_errmsg If the ACL can't be parsed, this is set to a
     305 *                       newly-allocated error string describing the problem.
     306 *                       The client should tr_free() this string when done.
     307 *
     308 * @return 0 on success, -1 on failure due to an unparseable ACL.
    307309 *
    308310 * @see tr_sessionInitFull
    309311 * @see tr_sessionGetRPCACL
    310312 */
    311 void tr_sessionSetRPCACL( tr_handle *, const char * acl );
     313int tr_sessionSetRPCACL( tr_handle   * session,
     314                         const char  * acl,
     315                         char       ** allocme_errmsg );
    312316
    313317/** Returns the Access Control List for allowing/denying RPC requests.
  • trunk/libtransmission/utils-test.c

    r5997 r6004  
    44#include "utils.h"
    55
    6 #define VERBOSE 1
     6#define VERBOSE 0
    77#define NUM_LOOPS 1
    88#define SPEED_TEST 0
Note: See TracChangeset for help on using the changeset viewer.