Changeset 5f9a5f7ccf3c9e630d4f098ab9aa90dff0dc56d2
- 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
| r98e742c |
r5f9a5f7 |
|
| 314 | 314 | #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e) |
|---|
| 315 | 315 | VLC_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) |
|---|
| | 318 | VLC_EXPORT( int, __net_CheckIP, ( vlc_object_t *p_this, char *psz_ip, char **ppsz_hosts, int i_hosts ) ); |
|---|
| 316 | 319 | |
|---|
| 317 | 320 | /* Portable network names/addresses resolution layer */ |
|---|
| rf59392e |
r5f9a5f7 |
|
| 120 | 120 | |
|---|
| 121 | 121 | /* 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 ) ); |
|---|
| | 122 | VLC_EXPORT( httpd_url_t *, httpd_UrlNew, ( httpd_host_t *, char *psz_url, char *psz_user, char *psz_password, char **ppsz_hosts, int i_hosts ) ); |
|---|
| | 123 | VLC_EXPORT( httpd_url_t *, httpd_UrlNewUnique, ( httpd_host_t *, char *psz_url, char *psz_user, char *psz_password, char **ppsz_hosts, int i_hosts ) ); |
|---|
| 124 | 124 | /* register callback on a url */ |
|---|
| 125 | 125 | VLC_EXPORT( int, httpd_UrlCatch, ( httpd_url_t *, int i_msg, httpd_callback_t, httpd_callback_sys_t * ) ); |
|---|
| … | … | |
| 134 | 134 | /* High level */ |
|---|
| 135 | 135 | |
|---|
| 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 * ) ); |
|---|
| | 136 | VLC_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 * ) ); |
|---|
| 137 | 137 | VLC_EXPORT( void, httpd_FileDelete, ( httpd_file_t * ) ); |
|---|
| 138 | 138 | |
|---|
| … | … | |
| 142 | 142 | |
|---|
| 143 | 143 | |
|---|
| 144 | | VLC_EXPORT( httpd_stream_t *, httpd_StreamNew, ( httpd_host_t *, char *psz_url, char *psz_mime, char *psz_user, char *psz_password ) ); |
|---|
| | 144 | VLC_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 ) ); |
|---|
| 145 | 145 | VLC_EXPORT( void, httpd_StreamDelete, ( httpd_stream_t * ) ); |
|---|
| 146 | 146 | VLC_EXPORT( int, httpd_StreamHeader, ( httpd_stream_t *, uint8_t *p_data, int i_data ) ); |
|---|
| rf59392e |
r5f9a5f7 |
|
| 256 | 256 | p_sys->p_httpd_stream = |
|---|
| 257 | 257 | httpd_StreamNew( p_sys->p_httpd_host, psz_file_name, psz_mime, |
|---|
| 258 | | psz_user, psz_pwd ); |
|---|
| | 258 | psz_user, psz_pwd, NULL, 0 ); |
|---|
| 259 | 259 | if( psz_user ) free( psz_user ); |
|---|
| 260 | 260 | if( psz_pwd ) free( psz_pwd ); |
|---|
| rf59392e |
r5f9a5f7 |
|
| 523 | 523 | char *user = NULL; |
|---|
| 524 | 524 | char *password = NULL; |
|---|
| | 525 | char **ppsz_hosts = NULL; |
|---|
| | 526 | int i_hosts = 0; |
|---|
| | 527 | |
|---|
| | 528 | int i; |
|---|
| 525 | 529 | |
|---|
| 526 | 530 | #ifdef HAVE_SYS_STAT_H |
|---|
| … | … | |
| 571 | 575 | |
|---|
| 572 | 576 | 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 | } |
|---|
| 573 | 614 | } |
|---|
| 574 | 615 | |
|---|
| … | … | |
| 612 | 653 | f->name, |
|---|
| 613 | 654 | f->b_html ? p_sys->psz_html_type : NULL, |
|---|
| 614 | | user, password, |
|---|
| | 655 | user, password, ppsz_hosts, i_hosts, |
|---|
| 615 | 656 | HttpCallback, f ); |
|---|
| 616 | 657 | |
|---|
| … | … | |
| 653 | 694 | { |
|---|
| 654 | 695 | free( password ); |
|---|
| | 696 | } |
|---|
| | 697 | for( i = 0; i < i_hosts; i++ ) |
|---|
| | 698 | { |
|---|
| | 699 | TAB_REMOVE( i_hosts, ppsz_hosts, ppsz_hosts[0] ); |
|---|
| 655 | 700 | } |
|---|
| 656 | 701 | |
|---|
| r7ea393d |
r5f9a5f7 |
|
| 277 | 277 | asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name ); |
|---|
| 278 | 278 | 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 ); |
|---|
| 280 | 281 | |
|---|
| 281 | 282 | if( !p_media->p_rtsp_url ) |
|---|
| … | … | |
| 453 | 454 | |
|---|
| 454 | 455 | 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 ); |
|---|
| 456 | 458 | |
|---|
| 457 | 459 | if( !p_es->p_rtsp_url ) |
|---|
| re2dedca |
r5f9a5f7 |
|
| 1033 | 1033 | sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es ); |
|---|
| 1034 | 1034 | 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 ); |
|---|
| 1036 | 1036 | |
|---|
| 1037 | 1037 | if( id->p_rtsp_url ) |
|---|
| … | … | |
| 1297 | 1297 | url->psz_path ? url->psz_path : "/", |
|---|
| 1298 | 1298 | "application/sdp", |
|---|
| 1299 | | NULL, NULL, |
|---|
| | 1299 | NULL, NULL, NULL, 0, |
|---|
| 1300 | 1300 | HttpCallback, (void*)p_sys ); |
|---|
| 1301 | 1301 | } |
|---|
| … | … | |
| 1395 | 1395 | url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path ); |
|---|
| 1396 | 1396 | |
|---|
| 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 ); |
|---|
| 1398 | 1398 | if( p_sys->p_rtsp_url == 0 ) |
|---|
| 1399 | 1399 | { |
|---|
| rf59392e |
r5f9a5f7 |
|
| 238 | 238 | char *psz_user; |
|---|
| 239 | 239 | char *psz_password; |
|---|
| | 240 | char **ppsz_hosts; |
|---|
| | 241 | int i_hosts; |
|---|
| 240 | 242 | |
|---|
| 241 | 243 | struct |
|---|
| … | … | |
| 490 | 492 | char *psz_url, char *psz_mime, |
|---|
| 491 | 493 | char *psz_user, char *psz_password, |
|---|
| | 494 | char **ppsz_hosts, int i_hosts, |
|---|
| 492 | 495 | httpd_file_callback_t pf_fill, |
|---|
| 493 | 496 | httpd_file_sys_t *p_sys ) |
|---|
| … | … | |
| 496 | 499 | |
|---|
| 497 | 500 | if( ( file->url = httpd_UrlNewUnique( host, psz_url, psz_user, |
|---|
| 498 | | psz_password ) ) == NULL ) |
|---|
| | 501 | psz_password, ppsz_hosts, i_hosts ) |
|---|
| | 502 | ) == NULL ) |
|---|
| 499 | 503 | { |
|---|
| 500 | 504 | free( file ); |
|---|
| … | … | |
| 587 | 591 | httpd_redirect_t *rdir = malloc( sizeof( httpd_redirect_t ) ); |
|---|
| 588 | 592 | |
|---|
| 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 ) ) ) |
|---|
| 590 | 595 | { |
|---|
| 591 | 596 | free( rdir ); |
|---|
| … | … | |
| 762 | 767 | httpd_stream_t *httpd_StreamNew( httpd_host_t *host, |
|---|
| 763 | 768 | 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 ) |
|---|
| 765 | 771 | { |
|---|
| 766 | 772 | httpd_stream_t *stream = malloc( sizeof( httpd_stream_t ) ); |
|---|
| 767 | 773 | |
|---|
| 768 | 774 | if( ( stream->url = httpd_UrlNewUnique( host, psz_url, psz_user, |
|---|
| 769 | | psz_password ) ) == NULL ) |
|---|
| | 775 | psz_password, ppsz_hosts, i_hosts ) |
|---|
| | 776 | ) == NULL ) |
|---|
| 770 | 777 | { |
|---|
| 771 | 778 | free( stream ); |
|---|
| … | … | |
| 1091 | 1098 | static httpd_url_t *httpd_UrlNewPrivate( httpd_host_t *host, char *psz_url, |
|---|
| 1092 | 1099 | char *psz_user, char *psz_password, |
|---|
| | 1100 | char **ppsz_hosts, int i_hosts, |
|---|
| 1093 | 1101 | vlc_bool_t b_check ) |
|---|
| 1094 | 1102 | { |
|---|
| … | … | |
| 1118 | 1126 | url->psz_user = strdup( psz_user ? psz_user : "" ); |
|---|
| 1119 | 1127 | 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 | } |
|---|
| 1120 | 1134 | for( i = 0; i < HTTPD_MSG_MAX; i++ ) |
|---|
| 1121 | 1135 | { |
|---|
| … | … | |
| 1131 | 1145 | |
|---|
| 1132 | 1146 | httpd_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 ) |
|---|
| 1134 | 1149 | { |
|---|
| 1135 | 1150 | return httpd_UrlNewPrivate( host, psz_url, psz_user, |
|---|
| 1136 | | psz_password, VLC_FALSE ); |
|---|
| | 1151 | psz_password, ppsz_hosts, i_hosts, VLC_FALSE ); |
|---|
| 1137 | 1152 | } |
|---|
| 1138 | 1153 | |
|---|
| 1139 | 1154 | httpd_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 ) |
|---|
| 1141 | 1157 | { |
|---|
| 1142 | 1158 | return httpd_UrlNewPrivate( host, psz_url, psz_user, |
|---|
| 1143 | | psz_password, VLC_TRUE ); |
|---|
| | 1159 | psz_password, ppsz_hosts, i_hosts, VLC_TRUE ); |
|---|
| 1144 | 1160 | } |
|---|
| 1145 | 1161 | |
|---|
| … | … | |
| 1170 | 1186 | free( url->psz_user ); |
|---|
| 1171 | 1187 | 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 | } |
|---|
| 1172 | 1192 | |
|---|
| 1173 | 1193 | for( i = 0; i < host->i_client; i++ ) |
|---|
| … | … | |
| 2074 | 2094 | { |
|---|
| 2075 | 2095 | vlc_bool_t b_auth_failed = VLC_FALSE; |
|---|
| | 2096 | vlc_bool_t b_hosts_failed = VLC_FALSE; |
|---|
| 2076 | 2097 | int i; |
|---|
| 2077 | 2098 | |
|---|
| … | … | |
| 2085 | 2106 | if( url->catch[i_msg].cb ) |
|---|
| 2086 | 2107 | { |
|---|
| | 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 | |
|---|
| 2087 | 2125 | if( answer && ( *url->psz_user || *url->psz_password ) ) |
|---|
| 2088 | 2126 | { |
|---|
| … | … | |
| 2143 | 2181 | p = answer->p_body = malloc( 1000 + strlen(query->psz_url) ); |
|---|
| 2144 | 2182 | |
|---|
| 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 ) |
|---|
| 2146 | 2200 | { |
|---|
| 2147 | 2201 | answer->i_status = 401; |
|---|
| … | … | |
| 2355 | 2409 | msg_Dbg( host, "new connection (%s)", |
|---|
| 2356 | 2410 | ip != NULL ? ip : "unknown" ); |
|---|
| 2357 | | if( ip != NULL) |
|---|
| | 2411 | if( ip != NULL ) |
|---|
| 2358 | 2412 | free( ip ); |
|---|
| 2359 | 2413 | } |
|---|
| r423003a |
r5f9a5f7 |
|
| 1172 | 1172 | return VLC_SUCCESS; |
|---|
| 1173 | 1173 | } |
|---|
| | 1174 | |
|---|
| | 1175 | /***************************************************************************** |
|---|
| | 1176 | * __net_CheckIP |
|---|
| | 1177 | ***************************************************************************** |
|---|
| | 1178 | * Check that a given IP is within a set of IP/netmask. |
|---|
| | 1179 | *****************************************************************************/ |
|---|
| | 1180 | int __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 | |
|---|