Changeset 5f9a5f7ccf3c9e630d4f098ab9aa90dff0dc56d2

Show
Ignore:
Timestamp:
17/06/05 14:43:46 (3 years ago)
Author:
Christophe Massiot <massiot@videolan.org>
git-committer:
Christophe Massiot <massiot@videolan.org> 1119012226 +0000
git-parent:

[d026e61f6d21944e1e064830316f92ab87823ea3]

git-author:
Christophe Massiot <massiot@videolan.org> 1119012226 +0000
Message:
  • modules/control/http.c: Added support for .hosts files detailing hosts
    allowed to connect. The format is :
    192.168.0.0/24
    172.16.12.42/32
  • src/misc/net.c: New function net_CheckIP to check that an IP is in a
    given range.
  • OTHERS: Changed prototypes to allow for two new arguments for the
    hosts list.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • include/network.h

    r98e742c r5f9a5f7  
    314314#define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e) 
    315315VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) ); 
     316 
     317#define net_CheckIP(a,b,c,d) __net_CheckIP(VLC_OBJECT(a),b,c,d) 
     318VLC_EXPORT( int, __net_CheckIP, ( vlc_object_t *p_this, char *psz_ip, char **ppsz_hosts, int i_hosts ) ); 
    316319 
    317320/* Portable network names/addresses resolution layer */ 
  • include/vlc_httpd.h

    rf59392e r5f9a5f7  
    120120 
    121121/* register a new url */ 
    122 VLC_EXPORT( httpd_url_t *,  httpd_UrlNew, ( httpd_host_t *, char *psz_url, char *psz_user, char *psz_password ) ); 
    123 VLC_EXPORT( httpd_url_t *,  httpd_UrlNewUnique, ( httpd_host_t *, char *psz_url, char *psz_user, char *psz_password ) ); 
     122VLC_EXPORT( httpd_url_t *,  httpd_UrlNew, ( httpd_host_t *, char *psz_url, char *psz_user, char *psz_password, char **ppsz_hosts, int i_hosts ) ); 
     123VLC_EXPORT( httpd_url_t *,  httpd_UrlNewUnique, ( httpd_host_t *, char *psz_url, char *psz_user, char *psz_password, char **ppsz_hosts, int i_hosts ) ); 
    124124/* register callback on a url */ 
    125125VLC_EXPORT( int,            httpd_UrlCatch, ( httpd_url_t *, int i_msg, httpd_callback_t, httpd_callback_sys_t * ) ); 
     
    134134/* High level */ 
    135135 
    136 VLC_EXPORT( httpd_file_t *, httpd_FileNew, ( httpd_host_t *, char *psz_url, char *psz_mime, char *psz_user, char *psz_password, httpd_file_callback_t pf_fill, httpd_file_sys_t * ) ); 
     136VLC_EXPORT( httpd_file_t *, httpd_FileNew, ( httpd_host_t *, char *psz_url, char *psz_mime, char *psz_user, char *psz_password, char **ppsz_hosts, int i_hosts, httpd_file_callback_t pf_fill, httpd_file_sys_t * ) ); 
    137137VLC_EXPORT( void,           httpd_FileDelete, ( httpd_file_t * ) ); 
    138138 
     
    142142 
    143143 
    144 VLC_EXPORT( httpd_stream_t *, httpd_StreamNew,    ( httpd_host_t *, char *psz_url, char *psz_mime, char *psz_user, char *psz_password ) ); 
     144VLC_EXPORT( httpd_stream_t *, httpd_StreamNew,    ( httpd_host_t *, char *psz_url, char *psz_mime, char *psz_user, char *psz_password, char **ppsz_hosts, int i_hosts ) ); 
    145145VLC_EXPORT( void,             httpd_StreamDelete, ( httpd_stream_t * ) ); 
    146146VLC_EXPORT( int,              httpd_StreamHeader, ( httpd_stream_t *, uint8_t *p_data, int i_data ) ); 
  • modules/access_output/http.c

    rf59392e r5f9a5f7  
    256256    p_sys->p_httpd_stream = 
    257257        httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime, 
    258                          psz_user, psz_pwd ); 
     258                         psz_user, psz_pwd, NULL, 0 ); 
    259259    if( psz_user ) free( psz_user ); 
    260260    if( psz_pwd ) free( psz_pwd ); 
  • modules/control/http.c

    rf59392e r5f9a5f7  
    523523    char          *user = NULL; 
    524524    char          *password = NULL; 
     525    char          **ppsz_hosts = NULL; 
     526    int           i_hosts = 0; 
     527 
     528    int           i; 
    525529 
    526530#ifdef HAVE_SYS_STAT_H 
     
    571575 
    572576        fclose( file ); 
     577    } 
     578 
     579    sprintf( dir, "%s/.hosts", psz_dir ); 
     580    if( ( file = fopen( dir, "r" ) ) != NULL ) 
     581    { 
     582        char line[1024]; 
     583        int  i_size; 
     584 
     585        msg_Dbg( p_intf, "find .hosts in dir=%s", psz_dir ); 
     586 
     587        while( !feof( file ) ) 
     588        { 
     589            fgets( line, 1023, file ); 
     590            i_size = strlen(line); 
     591            if( i_size > 0 && line[0] != '#' ) 
     592            { 
     593                while( i_size > 0 && ( line[i_size-1] == '\n' || 
     594                       line[i_size-1] == '\r' ) ) 
     595                { 
     596                    i_size--; 
     597                } 
     598                if( !i_size ) continue; 
     599 
     600                line[i_size] = '\0'; 
     601 
     602                msg_Dbg( p_intf, "restricted to %s (read=%d)", 
     603                         line, i_size ); 
     604                TAB_APPEND( i_hosts, ppsz_hosts, strdup( line ) ); 
     605            } 
     606        } 
     607 
     608        fclose( file ); 
     609 
     610        if( net_CheckIP( p_intf, "0.0.0.0", ppsz_hosts, i_hosts ) < 0 ) 
     611        { 
     612            msg_Err( p_intf, ".hosts file is invalid in dir=%s", psz_dir ); 
     613        } 
    573614    } 
    574615 
     
    612653                                       f->name, 
    613654                                       f->b_html ? p_sys->psz_html_type : NULL, 
    614                                        user, password, 
     655                                       user, password, ppsz_hosts, i_hosts, 
    615656                                       HttpCallback, f ); 
    616657 
     
    653694    { 
    654695        free( password ); 
     696    } 
     697    for( i = 0; i < i_hosts; i++ ) 
     698    { 
     699        TAB_REMOVE( i_hosts, ppsz_hosts, ppsz_hosts[0] ); 
    655700    } 
    656701 
  • modules/misc/rtsp.c

    r7ea393d r5f9a5f7  
    277277    asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name ); 
    278278    p_media->p_rtsp_url = 
    279         httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0 ); 
     279        httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0, 
     280                            NULL, 0 ); 
    280281 
    281282    if( !p_media->p_rtsp_url ) 
     
    453454 
    454455    p_es->p_rtsp_url = 
    455         httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0 ); 
     456        httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0, NULL, 
     457                            0 ); 
    456458 
    457459    if( !p_es->p_rtsp_url ) 
  • modules/stream_out/rtp.c

    re2dedca r5f9a5f7  
    10331033        sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es ); 
    10341034        fprintf( stderr, "rtsp: adding %s\n", psz_urlc ); 
    1035         id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL ); 
     1035        id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL, 0 ); 
    10361036 
    10371037        if( id->p_rtsp_url ) 
     
    12971297                                             url->psz_path ? url->psz_path : "/", 
    12981298                                             "application/sdp", 
    1299                                              NULL, NULL, 
     1299                                             NULL, NULL, NULL, 0, 
    13001300                                             HttpCallback, (void*)p_sys ); 
    13011301    } 
     
    13951395             url->psz_host,  url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path ); 
    13961396 
    1397     p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL ); 
     1397    p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL, 0 ); 
    13981398    if( p_sys->p_rtsp_url == 0 ) 
    13991399    { 
  • src/misc/httpd.c

    rf59392e r5f9a5f7  
    238238    char    *psz_user; 
    239239    char    *psz_password; 
     240    char    **ppsz_hosts; 
     241    int     i_hosts; 
    240242 
    241243    struct 
     
    490492                             char *psz_url, char *psz_mime, 
    491493                             char *psz_user, char *psz_password, 
     494                             char **ppsz_hosts, int i_hosts, 
    492495                             httpd_file_callback_t pf_fill, 
    493496                             httpd_file_sys_t *p_sys ) 
     
    496499 
    497500    if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user, 
    498                                           psz_password ) ) == NULL ) 
     501                                          psz_password, ppsz_hosts, i_hosts ) 
     502        ) == NULL ) 
    499503    { 
    500504        free( file ); 
     
    587591    httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) ); 
    588592 
    589     if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL ) ) ) 
     593    if( !( rdir->url = httpd_UrlNewUnique( host, psz_url_src, NULL, NULL, 
     594                                           NULL, 0 ) ) ) 
    590595    { 
    591596        free( rdir ); 
     
    762767httpd_stream_t *httpd_StreamNew( httpd_host_t *host, 
    763768                                 char *psz_url, char *psz_mime, 
    764                                  char *psz_user, char *psz_password ) 
     769                                 char *psz_user, char *psz_password, 
     770                                 char **ppsz_hosts, int i_hosts ) 
    765771{ 
    766772    httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) ); 
    767773 
    768774    if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user, 
    769                                             psz_password ) ) == NULL ) 
     775                                            psz_password, ppsz_hosts, i_hosts ) 
     776        ) == NULL ) 
    770777    { 
    771778        free( stream ); 
     
    10911098static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url, 
    10921099                                         char *psz_user, char *psz_password, 
     1100                                         char **ppsz_hosts, int i_hosts, 
    10931101                                         vlc_bool_t b_check ) 
    10941102{ 
     
    11181126    url->psz_user = strdup( psz_user ? psz_user : "" ); 
    11191127    url->psz_password = strdup( psz_password ? psz_password : "" ); 
     1128    url->i_hosts = 0; 
     1129    url->ppsz_hosts = NULL; 
     1130    for( i = 0; i < i_hosts; i++ ) 
     1131    { 
     1132        TAB_APPEND( url->i_hosts, url->ppsz_hosts, strdup(ppsz_hosts[i]) ); 
     1133    } 
    11201134    for( i = 0; i < HTTPD_MSG_MAX; i++ ) 
    11211135    { 
     
    11311145 
    11321146httpd_url_t *httpd_UrlNew( httpd_host_t *host, char *psz_url, 
    1133                            char *psz_user, char *psz_password ) 
     1147                           char *psz_user, char *psz_password, 
     1148                           char **ppsz_hosts, int i_hosts ) 
    11341149{ 
    11351150    return httpd_UrlNewPrivate( host, psz_url, psz_user, 
    1136                                 psz_password, VLC_FALSE ); 
     1151                                psz_password, ppsz_hosts, i_hosts, VLC_FALSE ); 
    11371152} 
    11381153 
    11391154httpd_url_t *httpd_UrlNewUnique( httpd_host_t *host, char *psz_url, 
    1140                                  char *psz_user, char *psz_password ) 
     1155                                 char *psz_user, char *psz_password, 
     1156                                 char **ppsz_hosts, int i_hosts ) 
    11411157{ 
    11421158    return httpd_UrlNewPrivate( host, psz_url, psz_user, 
    1143                                 psz_password, VLC_TRUE ); 
     1159                                psz_password, ppsz_hosts, i_hosts, VLC_TRUE ); 
    11441160} 
    11451161 
     
    11701186    free( url->psz_user ); 
    11711187    free( url->psz_password ); 
     1188    for( i = 0; i < url->i_hosts; i++ ) 
     1189    { 
     1190        TAB_REMOVE( url->i_hosts, url->ppsz_hosts, url->ppsz_hosts[0] ); 
     1191    } 
    11721192 
    11731193    for( i = 0; i < host->i_client; i++ ) 
     
    20742094                { 
    20752095                    vlc_bool_t b_auth_failed = VLC_FALSE; 
     2096                    vlc_bool_t b_hosts_failed = VLC_FALSE; 
    20762097                    int i; 
    20772098 
     
    20852106                            if( url->catch[i_msg].cb ) 
    20862107                            { 
     2108                                if( answer && url->i_hosts ) 
     2109                                { 
     2110                                    char *ip = httpd_ClientIP( cl ); 
     2111                                    if( ip != NULL ) 
     2112                                    { 
     2113                                        if( net_CheckIP( host, ip, 
     2114                                                         url->ppsz_hosts, 
     2115                                                         url->i_hosts ) <= 0 ) 
     2116                                        { 
     2117                                            b_hosts_failed = VLC_TRUE; 
     2118                                            free( ip ); 
     2119                                            break; 
     2120                                        } 
     2121                                        free( ip ); 
     2122                                    } 
     2123                                } 
     2124 
    20872125                                if( answer && ( *url->psz_user || *url->psz_password ) ) 
    20882126                                { 
     
    21432181                        p = answer->p_body = malloc( 1000 + strlen(query->psz_url) ); 
    21442182 
    2145                         if( b_auth_failed ) 
     2183                        if( b_hosts_failed ) 
     2184                        { 
     2185                            answer->i_status = 403; 
     2186                            answer->psz_status = strdup( "Forbidden" ); 
     2187 
     2188                            p += sprintf( p, "<html>\n" ); 
     2189                            p += sprintf( p, "<head>\n" ); 
     2190                            p += sprintf( p, "<title>Error 403</title>\n" ); 
     2191                            p += sprintf( p, "</head>\n" ); 
     2192                            p += sprintf( p, "<body>\n" ); 
     2193                            p += sprintf( p, "<h1><center> 403 Forbidden (%s)</center></h1>\n", query->psz_url ); 
     2194                            p += sprintf( p, "<hr />\n" ); 
     2195                            p += sprintf( p, "<a href=\"http://www.videolan.org\">VideoLAN</a>\n" ); 
     2196                            p += sprintf( p, "</body>\n" ); 
     2197                            p += sprintf( p, "</html>\n" ); 
     2198                        } 
     2199                        else if( b_auth_failed ) 
    21462200                        { 
    21472201                            answer->i_status = 401; 
     
    23552409                        msg_Dbg( host, "new connection (%s)", 
    23562410                                ip != NULL ? ip : "unknown" ); 
    2357                         if( ip != NULL
     2411                        if( ip != NULL
    23582412                            free( ip ); 
    23592413                    } 
  • src/misc/net.c

    r423003a r5f9a5f7  
    11721172    return VLC_SUCCESS; 
    11731173} 
     1174 
     1175/***************************************************************************** 
     1176 * __net_CheckIP 
     1177 ***************************************************************************** 
     1178 * Check that a given IP is within a set of IP/netmask. 
     1179 *****************************************************************************/ 
     1180int __net_CheckIP( vlc_object_t *p_this, char *psz_ip, char **ppsz_hosts, 
     1181                   int i_hosts ) 
     1182{ 
     1183    struct in_addr ip; 
     1184    int i; 
     1185 
     1186    if( !inet_aton( psz_ip, &ip ) ) 
     1187    { 
     1188        return VLC_EGENERIC; 
     1189    } 
     1190 
     1191    for( i = 0; i < i_hosts; i++ ) 
     1192    { 
     1193        struct in_addr base, mask; 
     1194        char *psz_host = strdup( ppsz_hosts[i] ); 
     1195        char *p = strchr( psz_host, '/' ); 
     1196 
     1197        if( p != NULL ) 
     1198        { 
     1199            int i_mask; 
     1200            *p++ = '\0'; 
     1201            i_mask = atoi(p); 
     1202            if( i_mask < 0 || i_mask > 32 ) 
     1203            { 
     1204                msg_Err( p_this, "invalid netmask %s", p ); 
     1205                mask.s_addr = INADDR_NONE; 
     1206            } 
     1207            else if( i_mask == 0 ) 
     1208                mask.s_addr = INADDR_ANY; 
     1209            else 
     1210                mask.s_addr = htons( ntohs(INADDR_NONE) << (32 - i_mask) ); 
     1211        } 
     1212        else 
     1213            mask.s_addr = INADDR_NONE; 
     1214 
     1215        if( !inet_aton( psz_host, &base ) ) 
     1216        { 
     1217            msg_Err( p_this, "invalid base address %s", psz_host ); 
     1218            free( psz_host ); 
     1219            continue; 
     1220        } 
     1221        free( psz_host ); 
     1222 
     1223        if( !((ip.s_addr ^ base.s_addr) & mask.s_addr) ) 
     1224            return VLC_TRUE; 
     1225    } 
     1226 
     1227    return VLC_FALSE; 
     1228} 
     1229