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