qt: svg support for pixmapanimator
[vlc.git] / modules / access / sftp.c
blobc119a223f8f1f1dc62dee1e7b360300a4297bf72
1 /*****************************************************************************
2 * sftp.c: SFTP input module
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: RĂ©mi Duraffort <ivoire@videolan.org>
8 * Petri Hintukainen <phintuka@gmail.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <assert.h>
37 #include <vlc_access.h>
38 #include <vlc_input_item.h>
39 #include <vlc_network.h>
40 #include <vlc_url.h>
41 #include <vlc_keystore.h>
43 #include <libssh2.h>
44 #include <libssh2_sftp.h>
47 /*****************************************************************************
48 * Module descriptor
49 *****************************************************************************/
50 static int Open ( vlc_object_t* );
51 static void Close( vlc_object_t* );
53 #define PORT_TEXT N_("SFTP port")
54 #define PORT_LONGTEXT N_("SFTP port number to use on the server")
55 #define USER_TEXT N_("Username")
56 #define USER_LONGTEXT N_("Username that will be used for the connection, " \
57 "if no username is set in the URL.")
58 #define PASS_TEXT N_("Password")
59 #define PASS_LONGTEXT N_("Password that will be used for the connection, " \
60 "if no username or password are set in URL.")
62 vlc_module_begin ()
63 set_shortname( "SFTP" )
64 set_description( N_("SFTP input") )
65 set_capability( "access", 0 )
66 set_category( CAT_INPUT )
67 set_subcategory( SUBCAT_INPUT_ACCESS )
68 add_integer( "sftp-port", 22, PORT_TEXT, PORT_LONGTEXT, true )
69 add_string( "sftp-user", NULL, USER_TEXT, USER_LONGTEXT, false )
70 add_password( "sftp-pwd", NULL, PASS_TEXT, PASS_LONGTEXT, false )
71 add_shortcut( "sftp" )
72 set_callbacks( Open, Close )
73 vlc_module_end ()
76 /*****************************************************************************
77 * Local prototypes
78 *****************************************************************************/
79 static ssize_t Read( stream_t *, void *, size_t );
80 static int Seek( stream_t *, uint64_t );
81 static int Control( stream_t *, int, va_list );
83 static int DirRead( stream_t *, input_item_node_t * );
85 struct access_sys_t
87 int i_socket;
88 LIBSSH2_SESSION* ssh_session;
89 LIBSSH2_SFTP* sftp_session;
90 LIBSSH2_SFTP_HANDLE* file;
91 uint64_t filesize;
92 char *psz_base_url;
95 static int AuthKeyAgent( stream_t *p_access, const char *psz_username )
97 access_sys_t* p_sys = p_access->p_sys;
98 int i_result = VLC_EGENERIC;
99 LIBSSH2_AGENT *p_sshagent = NULL;
100 struct libssh2_agent_publickey *p_identity = NULL,
101 *p_prev_identity = NULL;
103 if( !psz_username || !psz_username[0] )
104 return i_result;
106 p_sshagent = libssh2_agent_init( p_sys->ssh_session );
108 if( !p_sshagent )
110 msg_Dbg( p_access, "Failed to initialize key agent" );
111 goto bailout;
113 if( libssh2_agent_connect( p_sshagent ) )
115 msg_Dbg( p_access, "Failed to connect key agent" );
116 goto bailout;
118 if( libssh2_agent_list_identities( p_sshagent ) )
120 msg_Dbg( p_access, "Failed to request identities" );
121 goto bailout;
124 while( libssh2_agent_get_identity( p_sshagent, &p_identity, p_prev_identity ) == 0 )
126 msg_Dbg( p_access, "Using key %s", p_identity->comment );
127 if( libssh2_agent_userauth( p_sshagent, psz_username, p_identity ) == 0 )
129 msg_Info( p_access, "Public key agent authentication succeeded" );
130 i_result = VLC_SUCCESS;
131 goto bailout;
133 msg_Dbg( p_access, "Public key agent authentication failed" );
134 p_prev_identity = p_identity;
137 bailout:
138 if( p_sshagent )
140 libssh2_agent_disconnect( p_sshagent );
141 libssh2_agent_free( p_sshagent );
143 return i_result;
147 static int AuthPublicKey( stream_t *p_access, const char *psz_home, const char *psz_username )
149 access_sys_t* p_sys = p_access->p_sys;
150 int i_result = VLC_EGENERIC;
151 char *psz_keyfile1 = NULL;
152 char *psz_keyfile2 = NULL;
154 if( !psz_username || !psz_username[0] )
155 return i_result;
157 if( asprintf( &psz_keyfile1, "%s/.ssh/id_rsa.pub", psz_home ) == -1 ||
158 asprintf( &psz_keyfile2, "%s/.ssh/id_rsa", psz_home ) == -1 )
159 goto bailout;
161 if( libssh2_userauth_publickey_fromfile( p_sys->ssh_session, psz_username, psz_keyfile1, psz_keyfile2, NULL ) )
163 msg_Dbg( p_access, "Public key authentication failed" );
164 goto bailout;
167 msg_Info( p_access, "Public key authentication succeeded" );
168 i_result = VLC_SUCCESS;
170 bailout:
171 free( psz_keyfile1 );
172 free( psz_keyfile2 );
173 return i_result;
177 * Connect to the sftp server and ask for a file
178 * @param p_this: the vlc_object
179 * @return VLC_SUCCESS if everything was fine
181 static int Open( vlc_object_t* p_this )
183 stream_t* p_access = (stream_t*)p_this;
184 access_sys_t* p_sys;
185 vlc_url_t credential_url;
186 vlc_credential credential;
187 const char* psz_path;
188 char* psz_remote_home = NULL;
189 char* psz_home = NULL;
190 int i_port;
191 int i_ret;
192 vlc_url_t url;
193 size_t i_len;
194 int i_type;
195 int i_result = VLC_EGENERIC;
197 if( !p_access->psz_location )
198 return VLC_EGENERIC;
200 p_sys = p_access->p_sys = vlc_calloc( p_this, 1, sizeof( access_sys_t ) );
201 if( !p_sys ) return VLC_ENOMEM;
203 p_sys->i_socket = -1;
205 vlc_UrlParse( &credential_url, p_access->psz_url );
206 vlc_credential_init( &credential, &credential_url );
208 /* Parse the URL */
209 vlc_UrlParse( &url, p_access->psz_url );
210 vlc_uri_decode( url.psz_path );
212 /* Check for some parameters */
213 if( EMPTY_STR( url.psz_host ) )
215 msg_Err( p_access, "Unable to extract host from %s", p_access->psz_url );
216 goto error;
219 if( url.i_port <= 0 )
220 i_port = var_InheritInteger( p_access, "sftp-port" );
221 else
222 i_port = url.i_port;
225 /* Connect to the server using a regular socket */
226 p_sys->i_socket = net_ConnectTCP( p_access, url.psz_host, i_port );
227 if( p_sys->i_socket < 0 )
229 msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
230 goto error;
233 /* Create the ssh connexion and wait until the server answer */
234 if( ( p_sys->ssh_session = libssh2_session_init() ) == NULL )
235 goto error;
237 while( ( i_ret = libssh2_session_startup( p_sys->ssh_session,
238 p_sys->i_socket ) )
239 == LIBSSH2_ERROR_EAGAIN );
241 if( i_ret != 0 )
243 msg_Err( p_access, "Impossible to open the connection to %s:%i", url.psz_host, i_port );
244 goto error;
247 /* Set the socket in non-blocking mode */
248 libssh2_session_set_blocking( p_sys->ssh_session, 1 );
250 /* List the know hosts */
251 LIBSSH2_KNOWNHOSTS *ssh_knownhosts = libssh2_knownhost_init( p_sys->ssh_session );
252 if( !ssh_knownhosts )
253 goto error;
255 psz_home = config_GetUserDir( VLC_HOME_DIR );
256 char *psz_knownhosts_file;
257 if( asprintf( &psz_knownhosts_file, "%s/.ssh/known_hosts", psz_home ) != -1 )
259 libssh2_knownhost_readfile( ssh_knownhosts, psz_knownhosts_file,
260 LIBSSH2_KNOWNHOST_FILE_OPENSSH );
261 free( psz_knownhosts_file );
264 const char *fingerprint = libssh2_session_hostkey( p_sys->ssh_session, &i_len, &i_type );
265 struct libssh2_knownhost *host;
266 int knownhost_fingerprint_algo;
268 switch( i_type )
270 case LIBSSH2_HOSTKEY_TYPE_RSA:
271 knownhost_fingerprint_algo = LIBSSH2_KNOWNHOST_KEY_SSHRSA;
272 break;
274 case LIBSSH2_HOSTKEY_TYPE_DSS:
275 knownhost_fingerprint_algo = LIBSSH2_KNOWNHOST_KEY_SSHDSS;
276 break;
278 default:
279 msg_Err( p_access, "Host uses unrecognized session-key algorithm" );
280 libssh2_knownhost_free( ssh_knownhosts );
281 goto error;
285 int check = libssh2_knownhost_check( ssh_knownhosts, url.psz_host,
286 fingerprint, i_len,
287 LIBSSH2_KNOWNHOST_TYPE_PLAIN |
288 LIBSSH2_KNOWNHOST_KEYENC_RAW |
289 knownhost_fingerprint_algo,
290 &host );
292 libssh2_knownhost_free( ssh_knownhosts );
294 /* Check that it does match or at least that the host is unknown */
295 switch(check)
297 case LIBSSH2_KNOWNHOST_CHECK_FAILURE:
298 case LIBSSH2_KNOWNHOST_CHECK_NOTFOUND:
299 msg_Dbg( p_access, "Unable to check the remote host" );
300 break;
301 case LIBSSH2_KNOWNHOST_CHECK_MATCH:
302 msg_Dbg( p_access, "Succesfuly matched the host" );
303 break;
304 case LIBSSH2_KNOWNHOST_CHECK_MISMATCH:
305 msg_Err( p_access, "The host does not match !! The remote key changed !!" );
306 goto error;
309 char* psz_userauthlist = NULL;
312 if (!credential.psz_username || !credential.psz_username[0])
313 continue;
315 psz_userauthlist = libssh2_userauth_list( p_sys->ssh_session, credential.psz_username, strlen( credential.psz_username ) );
317 /* TODO: Follow PreferredAuthentications in ssh_config */
319 if( strstr( psz_userauthlist, "publickey" ) != NULL &&
320 ( AuthKeyAgent( p_access, credential.psz_username ) == VLC_SUCCESS ||
321 AuthPublicKey( p_access, psz_home, credential.psz_username ) == VLC_SUCCESS ) )
322 break;
323 if( strstr( psz_userauthlist, "password" ) != NULL &&
324 libssh2_userauth_password( p_sys->ssh_session,
325 credential.psz_username,
326 credential.psz_password ) == 0 )
328 vlc_credential_store( &credential, p_access );
329 break;
332 msg_Warn( p_access, "sftp auth failed for %s", credential.psz_username );
333 } while( vlc_credential_get( &credential, p_access, "sftp-user", "sftp-pwd",
334 _("SFTP authentication"),
335 _("Please enter a valid login and password for "
336 "the sftp connexion to %s"), url.psz_host ) );
338 /* Create the sftp session */
339 p_sys->sftp_session = libssh2_sftp_init( p_sys->ssh_session );
341 if( !p_sys->sftp_session )
343 msg_Err( p_access, "Unable to initialize the SFTP session" );
344 goto error;
347 /* No path, default to user Home */
348 if( !url.psz_path )
350 const size_t i_size = 1024;
351 int i_ret;
353 psz_remote_home = malloc( i_size );
354 if( !psz_remote_home )
355 goto error;
357 i_ret = libssh2_sftp_symlink_ex( p_sys->sftp_session, ".", 1,
358 psz_remote_home, i_size - 1,
359 LIBSSH2_SFTP_REALPATH );
360 if( i_ret <= 0 )
362 msg_Err( p_access, "Impossible to get the Home directory" );
363 goto error;
365 psz_remote_home[i_ret] = '\0';
366 psz_path = psz_remote_home;
368 /* store base url for directory read */
369 char *base = vlc_path2uri( psz_path, "sftp" );
370 if( !base )
371 goto error;
372 if( -1 == asprintf( &p_sys->psz_base_url, "sftp://%s%s", p_access->psz_location, base + 7 ) )
374 free( base );
375 goto error;
377 free( base );
379 else
380 psz_path = url.psz_path;
382 /* Get some information */
383 LIBSSH2_SFTP_ATTRIBUTES attributes;
384 if( libssh2_sftp_stat( p_sys->sftp_session, psz_path, &attributes ) )
386 msg_Err( p_access, "Impossible to get information about the remote path %s", psz_path );
387 goto error;
390 if( !LIBSSH2_SFTP_S_ISDIR( attributes.permissions ))
392 /* Open the given file */
393 p_sys->file = libssh2_sftp_open( p_sys->sftp_session, psz_path, LIBSSH2_FXF_READ, 0 );
394 p_sys->filesize = attributes.filesize;
396 ACCESS_SET_CALLBACKS( Read, NULL, Control, Seek );
398 else
400 /* Open the given directory */
401 p_sys->file = libssh2_sftp_opendir( p_sys->sftp_session, psz_path );
403 p_access->pf_readdir = DirRead;
404 p_access->pf_control = access_vaDirectoryControlHelper;
406 if( !p_sys->psz_base_url )
408 if( asprintf( &p_sys->psz_base_url, "sftp://%s", p_access->psz_location ) == -1 )
409 goto error;
411 /* trim trailing '/' */
412 size_t len = strlen( p_sys->psz_base_url );
413 if( len > 0 && p_sys->psz_base_url[ len - 1 ] == '/' )
414 p_sys->psz_base_url[ len - 1 ] = 0;
418 if( !p_sys->file )
420 msg_Err( p_access, "Unable to open the remote path %s", psz_path );
421 goto error;
424 i_result = VLC_SUCCESS;
426 error:
427 free( psz_home );
428 free( psz_remote_home );
429 vlc_UrlClean( &url );
430 vlc_credential_clean( &credential );
431 vlc_UrlClean( &credential_url );
432 if( i_result != VLC_SUCCESS ) {
433 Close( p_this );
435 return i_result;
439 /* Close: quit the module */
440 static void Close( vlc_object_t* p_this )
442 stream_t* p_access = (stream_t*)p_this;
443 access_sys_t* p_sys = p_access->p_sys;
445 if( p_sys->file )
446 libssh2_sftp_close_handle( p_sys->file );
447 if( p_sys->sftp_session )
448 libssh2_sftp_shutdown( p_sys->sftp_session );
449 if( p_sys->ssh_session )
450 libssh2_session_free( p_sys->ssh_session );
451 if( p_sys->i_socket >= 0 )
452 net_Close( p_sys->i_socket );
454 free( p_sys->psz_base_url );
458 static ssize_t Read( stream_t *p_access, void *buf, size_t len )
460 access_sys_t *p_sys = p_access->p_sys;
462 ssize_t val = libssh2_sftp_read( p_sys->file, buf, len );
463 if( val < 0 )
465 msg_Err( p_access, "read failed" );
466 return 0;
469 return val;
473 static int Seek( stream_t* p_access, uint64_t i_pos )
475 access_sys_t *sys = p_access->p_sys;
477 libssh2_sftp_seek( sys->file, i_pos );
478 return VLC_SUCCESS;
482 static int Control( stream_t* p_access, int i_query, va_list args )
484 access_sys_t *sys = p_access->p_sys;
485 bool* pb_bool;
486 int64_t* pi_64;
488 switch( i_query )
490 case STREAM_CAN_SEEK:
491 pb_bool = va_arg( args, bool * );
492 *pb_bool = true;
493 break;
495 case STREAM_CAN_FASTSEEK:
496 pb_bool = va_arg( args, bool * );
497 *pb_bool = false;
498 break;
500 case STREAM_CAN_PAUSE:
501 case STREAM_CAN_CONTROL_PACE:
502 pb_bool = va_arg( args, bool * );
503 *pb_bool = true;
504 break;
506 case STREAM_GET_SIZE:
507 if( p_access->pf_readdir != NULL )
508 return VLC_EGENERIC;
509 *va_arg( args, uint64_t * ) = sys->filesize;
510 break;
512 case STREAM_GET_PTS_DELAY:
513 pi_64 = va_arg( args, int64_t * );
514 *pi_64 = INT64_C(1000)
515 * var_InheritInteger( p_access, "network-caching" );
516 break;
518 case STREAM_SET_PAUSE_STATE:
519 break;
521 default:
522 return VLC_EGENERIC;
525 return VLC_SUCCESS;
529 /*****************************************************************************
530 * Directory access
531 *****************************************************************************/
533 static int DirRead (stream_t *p_access, input_item_node_t *p_current_node)
535 access_sys_t *p_sys = p_access->p_sys;
536 LIBSSH2_SFTP_ATTRIBUTES attrs;
537 int i_ret = VLC_SUCCESS;
538 int err;
539 /* Allocate 1024 bytes for file name. Longer names are skipped.
540 * libssh2 does not support seeking in directory streams.
541 * Retrying with larger buffer is possible, but would require
542 * re-opening the directory stream.
544 size_t i_size = 1024;
545 char *psz_file = malloc( i_size );
547 if( !psz_file )
548 return VLC_ENOMEM;
550 struct vlc_readdir_helper rdh;
551 vlc_readdir_helper_init( &rdh, p_access, p_current_node );
553 while( i_ret == VLC_SUCCESS
554 && 0 != ( err = libssh2_sftp_readdir( p_sys->file, psz_file, i_size, &attrs ) ) )
556 if( err < 0 )
558 if( err == LIBSSH2_ERROR_BUFFER_TOO_SMALL )
560 /* seeking back is not possible ... */
561 msg_Dbg( p_access, "skipped too long file name" );
562 continue;
564 if( err == LIBSSH2_ERROR_EAGAIN )
566 continue;
568 msg_Err( p_access, "directory read failed" );
569 break;
572 /* Create an input item for the current entry */
574 char *psz_full_uri, *psz_uri;
576 psz_uri = vlc_uri_encode( psz_file );
577 if( psz_uri == NULL )
579 i_ret = VLC_ENOMEM;
580 break;
583 if( asprintf( &psz_full_uri, "%s/%s", p_sys->psz_base_url, psz_uri ) == -1 )
585 free( psz_uri );
586 i_ret = VLC_ENOMEM;
587 break;
589 free( psz_uri );
591 int i_type = LIBSSH2_SFTP_S_ISDIR( attrs.permissions ) ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
592 i_ret = vlc_readdir_helper_additem( &rdh, psz_full_uri, NULL, psz_file,
593 i_type, ITEM_NET );
594 free( psz_full_uri );
597 vlc_readdir_helper_finish( &rdh, i_ret == VLC_SUCCESS );
598 free( psz_file );
599 return i_ret;