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