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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.