]> git.sesse.net Git - vlc/blob - modules/access/ftp.c
Safer EPSV implementation (needed new networking API)
[vlc] / modules / access / ftp.c
1 /*****************************************************************************
2  * ftp.c: FTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr> - original code
8  *          RĂ©mi Denis-Courmont <rem # videolan.org> - EPSV support
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "network.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int     Open ( vlc_object_t * );
39 static void    Close( vlc_object_t * );
40
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43     "Allows you to modify the default caching value for FTP streams. This " \
44     "value should be set in millisecond units." )
45 #define USER_TEXT N_("FTP user name")
46 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
47     "be used for the connection.")
48 #define PASS_TEXT N_("FTP password")
49 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
50     "used for the connection.")
51 #define ACCOUNT_TEXT N_("FTP account")
52 #define ACCOUNT_LONGTEXT N_("Allows you to modify the account that will be " \
53     "used for the connection.")
54
55 vlc_module_begin();
56     set_shortname( "FTP" );
57     set_description( _("FTP input") );
58     set_capability( "access2", 0 );
59     set_category( CAT_INPUT );
60     set_subcategory( SUBCAT_INPUT_ACCESS );
61     add_integer( "ftp-caching", 2 * DEFAULT_PTS_DELAY / 1000, NULL,
62                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
63     add_string( "ftp-user", "anonymous", NULL, USER_TEXT, USER_LONGTEXT,
64                 VLC_FALSE );
65     add_string( "ftp-pwd", "anonymous@dummy.org", NULL, PASS_TEXT,
66                 PASS_LONGTEXT, VLC_FALSE );
67     add_string( "ftp-account", "anonymous", NULL, ACCOUNT_TEXT,
68                 ACCOUNT_LONGTEXT, VLC_FALSE );
69     add_shortcut( "ftp" );
70     set_callbacks( Open, Close );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static int Read( access_t *, uint8_t *, int );
77 static int Seek( access_t *, int64_t );
78 static int Control( access_t *, int, va_list );
79
80 struct access_sys_t
81 {
82     vlc_url_t  url;
83
84     int        fd_cmd;
85     int        fd_data;
86     
87     char       *psz_epsv_ip;
88 };
89
90 static int  ftp_SendCommand( access_t *, char *, ... );
91 static int  ftp_ReadCommand( access_t *, int *, char ** );
92 static int  ftp_StartStream( access_t *, int64_t );
93 static int  ftp_StopStream ( access_t *);
94
95 /****************************************************************************
96  * Open: connect to ftp server and ask for file
97  ****************************************************************************/
98 static int Open( vlc_object_t *p_this )
99 {
100     access_t     *p_access = (access_t*)p_this;
101     access_sys_t *p_sys;
102     char         *psz;
103
104     int          i_answer;
105     char         *psz_arg;
106
107     /* Init p_access */
108     p_access->pf_read = Read;
109     p_access->pf_block = NULL;
110     p_access->pf_seek = Seek;
111     p_access->pf_control = Control;
112     p_access->info.i_update = 0;
113     p_access->info.i_size = 0;
114     p_access->info.i_pos = 0;
115     p_access->info.b_eof = VLC_FALSE;
116     p_access->info.i_title = 0;
117     p_access->info.i_seekpoint = 0;
118     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
119     memset( p_sys, 0, sizeof( access_sys_t ) );
120     p_sys->fd_cmd = -1;
121     p_sys->fd_data = -1;
122     p_sys->psz_epsv_ip = NULL;
123
124     /* *** Parse URL and get server addr/port and path *** */
125     psz = p_access->psz_path;
126     while( *psz == '/' )
127     {
128         psz++;
129     }
130     vlc_UrlParse( &p_sys->url, psz, 0 );
131
132     if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
133     {
134         msg_Err( p_access, "invalid server name" );
135         goto exit_error;
136     }
137     if( p_sys->url.i_port <= 0 )
138     {
139         p_sys->url.i_port = 21; /* default port */
140     }
141
142     /* *** Open a TCP connection with server *** */
143     msg_Dbg( p_access, "waiting for connection..." );
144     p_sys->fd_cmd = net_OpenTCP( p_access, p_sys->url.psz_host,
145                                  p_sys->url.i_port );
146     if( p_sys->fd_cmd < 0 )
147     {
148         msg_Err( p_access, "failed to connect with server" );
149         goto exit_error;
150     }
151
152     for( ;; )
153     {
154         if( ftp_ReadCommand( p_access, &i_answer, NULL ) != 1 )
155         {
156             break;
157         }
158     }
159     if( i_answer / 100 != 2 )
160     {
161         msg_Err( p_access, "connection rejected" );
162         goto exit_error;
163     }
164
165     msg_Dbg( p_access, "connection accepted (%d)", i_answer );
166
167     psz = var_CreateGetString( p_access, "ftp-user" );
168     if( ftp_SendCommand( p_access, "USER %s", psz ) < 0 ||
169         ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
170     {
171         free( psz );
172         goto exit_error;
173     }
174     free( psz );
175
176     switch( i_answer / 100 )
177     {
178         case 2:
179             msg_Dbg( p_access, "user accepted" );
180             break;
181         case 3:
182             msg_Dbg( p_access, "password needed" );
183             psz = var_CreateGetString( p_access, "ftp-pwd" );
184             if( ftp_SendCommand( p_access, "PASS %s", psz ) < 0 ||
185                 ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
186             {
187                 free( psz );
188                 goto exit_error;
189             }
190             free( psz );
191
192             switch( i_answer / 100 )
193             {
194                 case 2:
195                     msg_Dbg( p_access, "password accepted" );
196                     break;
197                 case 3:
198                     msg_Dbg( p_access, "account needed" );
199                     psz = var_CreateGetString( p_access, "ftp-account" );
200                     if( ftp_SendCommand( p_access, "ACCT %s",
201                                          psz ) < 0 ||
202                         ftp_ReadCommand( p_access, &i_answer, NULL ) < 0 )
203                     {
204                         free( psz );
205                         goto exit_error;
206                     }
207                     free( psz );
208
209                     if( i_answer / 100 != 2 )
210                     {
211                         msg_Err( p_access, "account rejected" );
212                         goto exit_error;
213                     }
214                     msg_Dbg( p_access, "account accepted" );
215                     break;
216
217                 default:
218                     msg_Err( p_access, "password rejected" );
219                     goto exit_error;
220             }
221             break;
222         default:
223             msg_Err( p_access, "user rejected" );
224             goto exit_error;
225     }
226
227     /* Extended passive mode */
228     if( ftp_SendCommand( p_access, "EPSV ALL" ) < 0 )
229     {
230         msg_Err( p_access, "cannot request extended passive mode" );
231         goto exit_error;
232     }
233
234     if( ftp_ReadCommand( p_access, &i_answer, NULL ) == 2 )
235     {
236         char hostaddr[NI_MAXHOST];
237         struct sockaddr_storage addr;
238         socklen_t len = sizeof (addr);
239
240         if( getpeername( p_sys->fd_cmd, (struct sockaddr *)&addr, &len ) )
241         {
242             msg_Err( p_access, "getpeername failed" );
243             goto exit_error;
244         }
245
246         i_answer = vlc_getnameinfo( p_this, (struct sockaddr *)&addr, len,
247                                     hostaddr, sizeof( hostaddr ), NULL, 0,
248                                     NI_NUMERICHOST );
249         if( i_answer )
250         {
251             msg_Err( p_access, "getnameinfo failed: %s",
252                      vlc_gai_strerror( i_answer ) );
253             goto exit_error;
254         }
255         p_sys->psz_epsv_ip = strdup( hostaddr );
256     }
257     if( p_sys->psz_epsv_ip == NULL )
258         msg_Info( p_access, "FTP Extended passive mode disabled" );
259
260     /* binary mode */
261     if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
262         ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
263     {
264         msg_Err( p_access, "cannot set binary transfer mode" );
265         goto exit_error;
266     }
267
268     /* get size */
269     if( ftp_SendCommand( p_access, "SIZE %s", p_sys->url.psz_path ) < 0 ||
270         ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 )
271     {
272         msg_Err( p_access, "cannot get file size" );
273         goto exit_error;
274     }
275     p_access->info.i_size = atoll( &psz_arg[4] );
276     free( psz_arg );
277     msg_Dbg( p_access, "file size: "I64Fd, p_access->info.i_size );
278
279     /* Start the 'stream' */
280     if( ftp_StartStream( p_access, 0 ) < 0 )
281     {
282         msg_Err( p_access, "cannot retrieve file" );
283         goto exit_error;
284     }
285
286     /* Update default_pts to a suitable value for ftp access */
287     var_Create( p_access, "ftp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
288
289     return VLC_SUCCESS;
290
291 exit_error:
292     if( p_sys->fd_cmd > 0 )
293     {
294         net_Close( p_sys->fd_cmd );
295     }
296     vlc_UrlClean( &p_sys->url );
297     free( p_sys );
298     return VLC_EGENERIC;
299 }
300
301 /*****************************************************************************
302  * Close: free unused data structures
303  *****************************************************************************/
304 static void Close( vlc_object_t *p_this )
305 {
306     access_t      *p_access = (access_t*)p_this;
307     access_sys_t  *p_sys = p_access->p_sys;
308
309     msg_Dbg( p_access, "stopping stream" );
310     ftp_StopStream( p_access );
311
312     if( ftp_SendCommand( p_access, "QUIT" ) < 0 )
313     {
314         msg_Warn( p_access, "cannot quit" );
315     }
316     else
317     {
318         ftp_ReadCommand( p_access, NULL, NULL );
319     }
320     net_Close( p_sys->fd_cmd );
321     if( p_sys->psz_epsv_ip != NULL )
322         free( p_sys->psz_epsv_ip );
323
324     /* free memory */
325     vlc_UrlClean( &p_sys->url );
326     free( p_sys );
327 }
328
329 /*****************************************************************************
330  * Seek: try to go at the right place
331  *****************************************************************************/
332 static int Seek( access_t *p_access, int64_t i_pos )
333 {
334     if( i_pos < 0 )
335     {
336         return VLC_EGENERIC;
337     }
338     msg_Dbg( p_access, "seeking to "I64Fd, i_pos );
339
340     ftp_StopStream( p_access );
341     if( ftp_StartStream( p_access, i_pos ) < 0 )
342     {
343         p_access->info.b_eof = VLC_TRUE;
344         return VLC_EGENERIC;
345     }
346
347     p_access->info.b_eof = VLC_FALSE;
348     p_access->info.i_pos = i_pos;
349
350     return VLC_SUCCESS;
351 }
352
353 /*****************************************************************************
354  * Read:
355  *****************************************************************************/
356 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
357 {
358     access_sys_t *p_sys = p_access->p_sys;
359     int i_read;
360
361     if( p_access->info.b_eof )
362         return 0;
363
364     i_read = net_Read( p_access, p_sys->fd_data, NULL, p_buffer, i_len,
365                        VLC_FALSE );
366     if( i_read == 0 )
367         p_access->info.b_eof = VLC_TRUE;
368     else if( i_read > 0 )
369         p_access->info.i_pos += i_read;
370
371     return i_read;
372 }
373
374 /*****************************************************************************
375  * Control:
376  *****************************************************************************/
377 static int Control( access_t *p_access, int i_query, va_list args )
378 {
379     vlc_bool_t   *pb_bool;
380     int          *pi_int;
381     int64_t      *pi_64;
382     vlc_value_t  val;
383
384     switch( i_query )
385     {
386         /* */
387         case ACCESS_CAN_SEEK:
388             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
389             *pb_bool = VLC_TRUE;
390             break;
391         case ACCESS_CAN_FASTSEEK:
392             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
393             *pb_bool = VLC_FALSE;
394             break;
395         case ACCESS_CAN_PAUSE:
396             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
397             *pb_bool = VLC_TRUE;    /* FIXME */
398             break;
399         case ACCESS_CAN_CONTROL_PACE:
400             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
401             *pb_bool = VLC_TRUE;    /* FIXME */
402             break;
403
404         /* */
405         case ACCESS_GET_MTU:
406             pi_int = (int*)va_arg( args, int * );
407             *pi_int = 0;
408             break;
409
410         case ACCESS_GET_PTS_DELAY:
411             pi_64 = (int64_t*)va_arg( args, int64_t * );
412             var_Get( p_access, "ftp-caching", &val );
413             *pi_64 = (int64_t)var_GetInteger( p_access, "ftp-caching" ) * I64C(1000);
414             break;
415
416         /* */
417         case ACCESS_SET_PAUSE_STATE:
418             /* Nothing to do */
419             break;
420
421         case ACCESS_GET_TITLE_INFO:
422         case ACCESS_SET_TITLE:
423         case ACCESS_SET_SEEKPOINT:
424         case ACCESS_SET_PRIVATE_ID_STATE:
425             return VLC_EGENERIC;
426
427         default:
428             msg_Warn( p_access, "unimplemented query in control" );
429             return VLC_EGENERIC;
430
431     }
432     return VLC_SUCCESS;
433 }
434
435 /*****************************************************************************
436  * ftp_*:
437  *****************************************************************************/
438 static int ftp_SendCommand( access_t *p_access, char *psz_fmt, ... )
439 {
440     access_sys_t *p_sys = p_access->p_sys;
441     va_list      args;
442     char         *psz_cmd;
443
444     va_start( args, psz_fmt );
445     vasprintf( &psz_cmd, psz_fmt, args );
446     va_end( args );
447
448     msg_Dbg( p_access, "ftp_SendCommand:\"%s\"", psz_cmd);
449     if( net_Printf( VLC_OBJECT(p_access), p_sys->fd_cmd, NULL, "%s\r\n",
450                     psz_cmd ) < 0 )
451     {
452         msg_Err( p_access, "failed to send command" );
453         return VLC_EGENERIC;
454     }
455     return VLC_SUCCESS;
456 }
457
458 /* TODO support this s**t :
459  RFC 959 allows the client to send certain TELNET strings at any moment,
460  even in the middle of a request:
461
462  * \377\377.
463  * \377\376x where x is one byte.
464  * \377\375x where x is one byte. The server is obliged to send \377\374x
465  *                                immediately after reading x.
466  * \377\374x where x is one byte.
467  * \377\373x where x is one byte. The server is obliged to send \377\376x
468  *                                immediately after reading x.
469  * \377x for any other byte x.
470
471  These strings are not part of the requests, except in the case \377\377,
472  where the request contains one \377. */
473 static int ftp_ReadCommand( access_t *p_access,
474                             int *pi_answer, char **ppsz_answer )
475 {
476     access_sys_t *p_sys = p_access->p_sys;
477     char         *psz_line;
478     int          i_answer;
479
480     psz_line = net_Gets( p_access, p_sys->fd_cmd, NULL );
481     msg_Dbg( p_access, "answer=%s", psz_line );
482     if( psz_line == NULL || strlen( psz_line ) < 3 )
483     {
484         msg_Err( p_access, "cannot get answer" );
485         if( psz_line ) free( psz_line );
486         if( pi_answer ) *pi_answer    = 500;
487         if( ppsz_answer ) *ppsz_answer  = NULL;
488         return -1;
489     }
490
491     if( psz_line[3] == '-' )    /* Multiple response */
492     {
493         char end[4];
494
495         memcpy( end, psz_line, 3 );
496         end[3] = ' ';
497
498         for( ;; )
499         {
500             char *psz_tmp = net_Gets( p_access, p_sys->fd_cmd, NULL );
501
502             if( psz_tmp == NULL )   /* Error */
503                 break;
504
505             if( !strncmp( psz_tmp, end, 4 ) )
506             {
507                 free( psz_tmp );
508                 break;
509             }
510             free( psz_tmp );
511         }
512     }
513
514     i_answer = atoi( psz_line );
515
516     if( pi_answer ) *pi_answer = i_answer;
517     if( ppsz_answer )
518     {
519         *ppsz_answer = psz_line;
520     }
521     else
522     {
523         free( psz_line );
524     }
525     return( i_answer / 100 );
526 }
527
528 static int ftp_StartStream( access_t *p_access, off_t i_start )
529 {
530     access_sys_t *p_sys = p_access->p_sys;
531
532     char psz_ipv4[16], *psz_ip;
533     int  i_answer;
534     char *psz_arg, *psz_parser;
535     unsigned  a1, a2, a3, a4, p1, p2;
536     int  i_port;
537
538     if( ( ftp_SendCommand( p_access, p_sys->psz_epsv_ip != NULL
539                                      ? "EPSV" : "PASV" ) < 0 )
540      || ( ftp_ReadCommand( p_access, &i_answer, &psz_arg ) != 2 ) )
541     {
542         msg_Err( p_access, "cannot set passive mode" );
543         return VLC_EGENERIC;
544     }
545
546     psz_parser = strchr( psz_arg, '(' );
547     if( psz_parser == NULL )
548     {
549         free( psz_arg );
550         msg_Err( p_access, "cannot parse passive mode response" );
551         return VLC_EGENERIC;
552     }
553
554     psz_ip = p_sys->psz_epsv_ip;
555     if( psz_ip != NULL )
556     {
557         char psz_fmt[7] = "(|||%u";
558         psz_fmt[1] = psz_fmt[2] = psz_fmt[3] = psz_parser[1];
559
560         if( sscanf( psz_parser, psz_fmt, &i_port ) < 1 )
561         {
562             free( psz_arg );
563             msg_Err( p_access, "cannot parse passive mode response" );
564             return VLC_EGENERIC;
565         }
566     }
567     else
568     {
569         if( ( sscanf( psz_parser, "(%u,%u,%u,%u,%u,%u", &a1, &a2, &a3, &a4,
570                       &p1, &p2 ) < 6 ) || ( a1 > 255 ) || ( a2 > 255 )
571          || ( a3 > 255 ) || ( a4 > 255 ) || ( p1 > 255 ) || ( p2 > 255 ) )
572         {
573             free( psz_arg );
574             msg_Err( p_access, "cannot parse passive mode response" );
575             return VLC_EGENERIC;
576         }
577
578         sprintf( psz_ipv4, "%u.%u.%u.%u", a1, a2, a3, a4 );
579         psz_ip = psz_ipv4;
580         i_port = (p1 << 8) | p2;
581     }
582     free( psz_arg );
583
584     msg_Dbg( p_access, "ip:%s port:%d", psz_ip, i_port );
585
586     if( ftp_SendCommand( p_access, "TYPE I" ) < 0 ||
587         ftp_ReadCommand( p_access, &i_answer, NULL ) != 2 )
588     {
589         msg_Err( p_access, "cannot set binary transfer mode" );
590         return VLC_EGENERIC;
591     }
592
593     if( i_start > 0 )
594     {
595         if( ftp_SendCommand( p_access, "REST "I64Fu, i_start ) < 0 ||
596             ftp_ReadCommand( p_access, &i_answer, NULL ) > 3 )
597         {
598             msg_Err( p_access, "cannot set restart point" );
599             return VLC_EGENERIC;
600         }
601     }
602
603     msg_Dbg( p_access, "waiting for data connection..." );
604     p_sys->fd_data = net_OpenTCP( p_access, psz_ip, i_port );
605     if( p_sys->fd_data < 0 )
606     {
607         msg_Err( p_access, "failed to connect with server" );
608         return VLC_EGENERIC;
609     }
610     msg_Dbg( p_access, "connection with \"%s:%d\" successful",
611              psz_ip, i_port );
612
613     /* "1xx" message */
614     if( ftp_SendCommand( p_access, "RETR %s", p_sys->url.psz_path ) < 0 ||
615         ftp_ReadCommand( p_access, &i_answer, NULL ) > 2 )
616     {
617         msg_Err( p_access, "cannot retreive file" );
618         return VLC_EGENERIC;
619     }
620     return VLC_SUCCESS;
621 }
622
623 static int ftp_StopStream ( access_t *p_access )
624 {
625     access_sys_t *p_sys = p_access->p_sys;
626
627     int i_answer;
628
629     if( ftp_SendCommand( p_access, "ABOR" ) < 0 )
630     {
631         msg_Warn( p_access, "cannot abord file" );
632         if(  p_sys->fd_data > 0 )
633             net_Close( p_sys->fd_data );
634         p_sys->fd_data = -1;
635         return VLC_EGENERIC;
636     }
637     if(  p_sys->fd_data > 0 )
638     {
639         net_Close( p_sys->fd_data );
640         p_sys->fd_data = -1;
641         ftp_ReadCommand( p_access, &i_answer, NULL );
642     }
643     ftp_ReadCommand( p_access, &i_answer, NULL );
644
645     return VLC_SUCCESS;
646 }
647