]> git.sesse.net Git - vlc/blob - modules/access/sftp.c
5c29a9ceab445a5767eea9652adc2e0703a08218
[vlc] / modules / access / sftp.c
1 /*****************************************************************************
2  * sftp.c: SFTP input module
3  *****************************************************************************
4  * Copyright (C) 2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: RĂ©mi Duraffort <ivoire@videolan.org>
8  *          Petri Hintukainen <phintuka@gmail.com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34
35 #include <assert.h>
36
37 #include <vlc_access.h>
38 #include <vlc_dialog.h>
39 #include <vlc_input_item.h>
40 #include <vlc_network.h>
41 #include <vlc_url.h>
42
43 #include <libssh2.h>
44 #include <libssh2_sftp.h>
45
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open ( vlc_object_t* );
51 static void Close( vlc_object_t* );
52
53 #define PORT_TEXT N_("SFTP port")
54 #define PORT_LONGTEXT N_("SFTP port number to use on the server")
55 #define MTU_TEXT N_("Read size")
56 #define MTU_LONGTEXT N_("Size of the request for reading access")
57 #define USER_TEXT N_("Username")
58 #define USER_LONGTEXT N_("Username that will be used for the connection, " \
59         "if no username is set in the URL.")
60 #define PASS_TEXT N_("Password")
61 #define PASS_LONGTEXT N_("Password that will be used for the connection, " \
62         "if no username or password are set in URL.")
63
64 vlc_module_begin ()
65     set_shortname( "SFTP" )
66     set_description( N_("SFTP input") )
67     set_capability( "access", 0 )
68     set_category( CAT_INPUT )
69     set_subcategory( SUBCAT_INPUT_ACCESS )
70     add_integer( "sftp-readsize", 8192, MTU_TEXT, MTU_LONGTEXT, true )
71     add_integer( "sftp-port", 22, PORT_TEXT, PORT_LONGTEXT, true )
72     add_string( "sftp-user", NULL, USER_TEXT, USER_LONGTEXT, false )
73     add_password( "sftp-pwd", NULL, PASS_TEXT, PASS_LONGTEXT, false )
74     add_shortcut( "sftp" )
75     set_callbacks( Open, Close )
76 vlc_module_end ()
77
78
79 /*****************************************************************************
80  * Local prototypes
81  *****************************************************************************/
82 static block_t* Block( access_t * );
83 static int      Seek( access_t *, uint64_t );
84 static int      Control( access_t *, int, va_list );
85
86 static int      DirControl( access_t *, int, va_list );
87 static int      DirRead( access_t *p_access, input_item_node_t *p_current_node );
88
89 struct access_sys_t
90 {
91     int i_socket;
92     LIBSSH2_SESSION* ssh_session;
93     LIBSSH2_SFTP* sftp_session;
94     LIBSSH2_SFTP_HANDLE* file;
95     uint64_t filesize;
96     size_t i_read_size;
97
98     /* browser */
99     char* psz_username_opt;
100     char* psz_password_opt;
101 };
102
103
104
105 /**
106  * Connect to the sftp server and ask for a file
107  * @param p_this: the vlc_object
108  * @return VLC_SUCCESS if everything was fine
109  */
110 static int Open( vlc_object_t* p_this )
111 {
112     access_t*   p_access = (access_t*)p_this;
113     access_sys_t* p_sys;
114     char* psz_username = NULL;
115     char* psz_password = NULL;
116     int i_port;
117     int i_ret;
118     vlc_url_t url;
119     size_t i_len;
120     int i_type;
121
122     if( !p_access->psz_location )
123         return VLC_EGENERIC;
124
125     access_InitFields( p_access );
126     p_sys = p_access->p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
127     if( !p_sys ) return VLC_ENOMEM;
128
129     p_sys->i_socket = -1;
130
131     /* Parse the URL */
132     const char* path = p_access->psz_location;
133     vlc_UrlParse( &url, path, 0 );
134
135     /* Check for some parameters */
136     if( EMPTY_STR( url.psz_host ) )
137     {
138         msg_Err( p_access, "You might give a non empty host" );
139         goto error;
140     }
141
142     /* get user/password from url or options */
143     if( !EMPTY_STR( url.psz_username ) )
144         psz_username = strdup( url.psz_username );
145     else
146         psz_username = var_InheritString( p_access, "sftp-user" );
147
148     if( url.psz_password )
149         psz_password = strdup( url.psz_password );
150     else
151         psz_password = var_InheritString( p_access, "sftp-pwd" );
152
153     /* If the user name or password is empty, ask the user */
154     if( EMPTY_STR( psz_username ) || !psz_password )
155     {
156         dialog_Login( p_access, &psz_username, &psz_password,
157                       _("SFTP authentication"),
158                       _("Please enter a valid login and password for the sftp "
159                         "connexion to %s"), url.psz_host );
160         if( EMPTY_STR(psz_username) || !psz_password )
161             goto error;
162     }
163
164     if( url.i_port <= 0 )
165         i_port = var_InheritInteger( p_access, "sftp-port" );
166     else
167         i_port = url.i_port;
168
169
170     /* Connect to the server using a regular socket */
171     p_sys->i_socket = net_Connect( p_access, url.psz_host, i_port, SOCK_STREAM, 0 );
172     if( p_sys->i_socket < 0 )
173     {
174         msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
175         goto error;
176     }
177
178     /* Create the ssh connexion and wait until the server answer */
179     if( ( p_sys->ssh_session = libssh2_session_init() ) == NULL )
180         goto error;
181
182     while( ( i_ret = libssh2_session_startup( p_sys->ssh_session,
183                                               p_sys->i_socket ) )
184            == LIBSSH2_ERROR_EAGAIN );
185
186     if( i_ret != 0 )
187     {
188         msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
189         goto error;
190     }
191
192     /* Set the socket in non-blocking mode */
193     libssh2_session_set_blocking( p_sys->ssh_session, 1 );
194
195     /* List the know hosts */
196     LIBSSH2_KNOWNHOSTS *ssh_knownhosts = libssh2_knownhost_init( p_sys->ssh_session );
197     if( !ssh_knownhosts )
198         goto error;
199
200     char *psz_home = config_GetUserDir( VLC_HOME_DIR );
201     char *psz_knownhosts_file;
202     if( asprintf( &psz_knownhosts_file, "%s/.ssh/known_hosts", psz_home ) != -1 )
203     {
204         libssh2_knownhost_readfile( ssh_knownhosts, psz_knownhosts_file,
205                 LIBSSH2_KNOWNHOST_FILE_OPENSSH );
206         free( psz_knownhosts_file );
207     }
208     free( psz_home );
209
210     const char *fingerprint = libssh2_session_hostkey( p_sys->ssh_session, &i_len, &i_type );
211     struct libssh2_knownhost *host;
212     int check = libssh2_knownhost_check( ssh_knownhosts, url.psz_host,
213                                          fingerprint, i_len,
214                                          LIBSSH2_KNOWNHOST_TYPE_PLAIN |
215                                          LIBSSH2_KNOWNHOST_KEYENC_RAW,
216                                          &host );
217
218     libssh2_knownhost_free( ssh_knownhosts );
219
220     /* Check that it does match or at least that the host is unknown */
221     switch(check)
222     {
223     case LIBSSH2_KNOWNHOST_CHECK_FAILURE:
224     case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:
225         msg_Dbg( p_access, "Unable to check the remote host" );
226         break;
227     case LIBSSH2_KNOWNHOST_CHECK_MATCH:
228         msg_Dbg( p_access, "Succesfuly matched the host" );
229         break;
230     case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:
231         msg_Err( p_access, "The host does not match !! The remote key changed !!" );
232         goto error;
233     }
234
235     //TODO: ask for the available auth methods
236
237     /* send the login/password */
238     if( libssh2_userauth_password( p_sys->ssh_session, psz_username, psz_password ) )
239     {
240         msg_Err( p_access, "Authentication by password failed" );
241         goto error;
242     }
243
244     /* Create the sftp session */
245     p_sys->sftp_session = libssh2_sftp_init( p_sys->ssh_session );
246
247     if( !p_sys->sftp_session )
248     {
249         msg_Err( p_access, "Unable to initialize the SFTP session" );
250         goto error;
251     }
252
253     /* Get some information */
254     LIBSSH2_SFTP_ATTRIBUTES attributes;
255     if( libssh2_sftp_stat( p_sys->sftp_session, url.psz_path, &attributes ) )
256     {
257         msg_Err( p_access, "Impossible to get information about the remote path %s", url.psz_path );
258         goto error;
259     }
260
261     if( !LIBSSH2_SFTP_S_ISDIR( attributes.permissions ))
262     {
263         /* Open the given file */
264         p_sys->file = libssh2_sftp_open( p_sys->sftp_session, url.psz_path, LIBSSH2_FXF_READ, 0 );
265         p_sys->filesize = attributes.filesize;
266
267         ACCESS_SET_CALLBACKS( NULL, Block, Control, Seek );
268     }
269     else
270     {
271         /* Open the given directory */
272         p_sys->file = libssh2_sftp_opendir( p_sys->sftp_session, url.psz_path );
273
274         p_access->pf_readdir = DirRead;
275         p_access->pf_control = DirControl;
276
277         if( p_sys->file )
278         {
279             if( -1 == asprintf( &p_sys->psz_username_opt, "sftp-user=%s", psz_username ) )
280                 p_sys->psz_username_opt = NULL;
281             if( -1 == asprintf( &p_sys->psz_password_opt, "sftp-pwd=%s", psz_password ) )
282                 p_sys->psz_password_opt = NULL;
283         }
284     }
285
286     if( !p_sys->file )
287     {
288         msg_Err( p_access, "Unable to open the remote path %s", url.psz_path );
289         goto error;
290     }
291
292     p_sys->i_read_size = var_InheritInteger( p_access, "sftp-readsize" );
293
294     free( psz_password );
295     free( psz_username );
296     vlc_UrlClean( &url );
297     return VLC_SUCCESS;
298
299 error:
300     if( p_sys->file )
301         libssh2_sftp_close_handle( p_sys->file );
302     if( p_sys->ssh_session )
303         libssh2_session_free( p_sys->ssh_session );
304     free( psz_password );
305     free( psz_username );
306     vlc_UrlClean( &url );
307     net_Close( p_sys->i_socket );
308     free( p_sys );
309     return VLC_EGENERIC;
310 }
311
312
313 /* Close: quit the module */
314 static void Close( vlc_object_t* p_this )
315 {
316     access_t*   p_access = (access_t*)p_this;
317     access_sys_t* p_sys = p_access->p_sys;
318
319     libssh2_sftp_close_handle( p_sys->file );
320     libssh2_sftp_shutdown( p_sys->sftp_session );
321
322     libssh2_session_free( p_sys->ssh_session );
323     net_Close( p_sys->i_socket );
324
325     free( p_sys->psz_password_opt );
326     free( p_sys->psz_username_opt );
327
328     free( p_sys );
329 }
330
331
332 static block_t* Block( access_t* p_access )
333 {
334     access_sys_t *p_sys = p_access->p_sys;
335
336     if( p_access->info.b_eof )
337         return NULL;
338
339     /* Allocate the buffer we need */
340     size_t i_len = __MIN( p_sys->i_read_size,
341                           p_sys->filesize - p_access->info.i_pos );
342     block_t* p_block = block_Alloc( i_len );
343     if( !p_block )
344         return NULL;
345
346     /* Read the specified size */
347     ssize_t i_ret = libssh2_sftp_read( p_access->p_sys->file, (char*)p_block->p_buffer, i_len );
348
349     if( i_ret < 0 )
350     {
351         block_Release( p_block );
352         msg_Err( p_access, "read failed" );
353         return NULL;
354     }
355     else if( i_ret == 0 )
356     {
357         p_access->info.b_eof = true;
358         block_Release( p_block );
359         return NULL;
360     }
361     else
362     {
363         p_block->i_buffer = i_ret;
364         p_access->info.i_pos += i_ret;
365         return p_block;
366     }
367 }
368
369
370 static int Seek( access_t* p_access, uint64_t i_pos )
371 {
372     p_access->info.i_pos = i_pos;
373     p_access->info.b_eof = false;
374
375     libssh2_sftp_seek( p_access->p_sys->file, i_pos );
376     return VLC_SUCCESS;
377 }
378
379
380 static int Control( access_t* p_access, int i_query, va_list args )
381 {
382     bool*       pb_bool;
383     int64_t*    pi_64;
384
385     switch( i_query )
386     {
387     case ACCESS_CAN_SEEK:
388         pb_bool = (bool*)va_arg( args, bool* );
389         *pb_bool = true;
390         break;
391
392     case ACCESS_CAN_FASTSEEK:
393         pb_bool = (bool*)va_arg( args, bool* );
394         *pb_bool = false;
395         break;
396
397     case ACCESS_CAN_PAUSE:
398     case ACCESS_CAN_CONTROL_PACE:
399         pb_bool = (bool*)va_arg( args, bool* );
400         *pb_bool = true;
401         break;
402
403     case ACCESS_GET_SIZE:
404         *va_arg( args, uint64_t * ) = p_access->p_sys->filesize;
405         break;
406
407     case ACCESS_GET_PTS_DELAY:
408         pi_64 = (int64_t*)va_arg( args, int64_t* );
409         *pi_64 = INT64_C(1000)
410                * var_InheritInteger( p_access, "network-caching" );
411         break;
412
413     case ACCESS_SET_PAUSE_STATE:
414         break;
415
416     default:
417         return VLC_EGENERIC;
418     }
419
420     return VLC_SUCCESS;
421 }
422
423
424 /*****************************************************************************
425  * Directory access
426  *****************************************************************************/
427
428 static int DirRead (access_t *p_access, input_item_node_t *p_current_node)
429 {
430     access_sys_t *p_sys = p_access->p_sys;
431     LIBSSH2_SFTP_ATTRIBUTES attrs;
432     int err;
433     /* Allocate 1024 bytes for file name. Longer names are skipped.
434      * libssh2 does not support seeking in directory streams.
435      * Retrying with larger buffer is possible, but would require
436      * re-opening the directory stream.
437      */
438     size_t i_size = 1024;
439     char *psz_file = malloc( i_size );
440
441     if( !psz_file )
442         return VLC_ENOMEM;
443
444     while( 0 != ( err = libssh2_sftp_readdir( p_sys->file, psz_file, i_size, &attrs ) ) )
445     {
446         if( err < 0 )
447         {
448             if( err == LIBSSH2_ERROR_BUFFER_TOO_SMALL )
449             {
450                 /* seeking back is not possible ... */
451                 msg_Dbg( p_access, "skipped too long file name" );
452                 continue;
453             }
454             if( err == LIBSSH2_ERROR_EAGAIN )
455             {
456                 continue;
457             }
458             msg_Err( p_access, "directory read failed" );
459             break;
460         }
461
462         if( psz_file[0] == '.' )
463         {
464             continue;
465         }
466
467         /* Create an input item for the current entry */
468
469         char *psz_full_uri, *psz_uri;
470
471         psz_uri = encode_URI_component( psz_file );
472         if( psz_uri == NULL )
473             continue;
474
475         if( asprintf( &psz_full_uri, "sftp://%s/%s", p_access->psz_location, psz_uri ) == -1 )
476         {
477             free( psz_uri );
478             continue;
479         }
480         free( psz_uri );
481
482         int i_type = LIBSSH2_SFTP_S_ISDIR( attrs.permissions ) ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
483         input_item_t *p_new = input_item_NewWithType( psz_full_uri, psz_file,
484                                                       0, NULL, 0, 0, i_type );
485
486         if( p_new == NULL )
487         {
488             free( psz_full_uri );
489             continue;
490         }
491
492         /* Here we save on the node the credentials that allowed us to login.
493          * That way the user isn't prompted more than once for credentials */
494         if( p_sys->psz_password_opt )
495             input_item_AddOption( p_new, p_sys->psz_password_opt, VLC_INPUT_OPTION_TRUSTED );
496         if( p_sys->psz_username_opt )
497             input_item_AddOption( p_new, p_sys->psz_username_opt, VLC_INPUT_OPTION_TRUSTED );
498
499         input_item_CopyOptions( p_current_node->p_item, p_new );
500         input_item_node_AppendItem( p_current_node, p_new );
501
502         free( psz_full_uri );
503         input_item_Release( p_new );
504     }
505
506     free( psz_file );
507     return VLC_SUCCESS;
508 }
509
510
511 static int DirControl( access_t *p_access, int i_query, va_list args )
512 {
513     VLC_UNUSED( p_access );
514
515     switch( i_query )
516     {
517     case ACCESS_CAN_SEEK:
518     case ACCESS_CAN_FASTSEEK:
519         *va_arg( args, bool* ) = false;
520         break;
521
522     case ACCESS_CAN_PAUSE:
523     case ACCESS_CAN_CONTROL_PACE:
524         *va_arg( args, bool* ) = true;
525         break;
526
527     case ACCESS_GET_PTS_DELAY:
528         *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
529         break;
530
531     default:
532         return VLC_EGENERIC;
533     }
534     return VLC_SUCCESS;
535 }