X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmisc%2Fupdate.c;h=315223a5f36c5825821be9601bff5ed320ae9ae8;hb=8e9cdc1579df7dce26ccb796157d654faf4065f8;hp=b1da7feb30e16949908bc4c0bb41e84310862c62;hpb=0da94177ecd5df425e4662aadb801309eadf1e0d;p=vlc diff --git a/src/misc/update.c b/src/misc/update.c index b1da7feb30..315223a5f3 100644 --- a/src/misc/update.c +++ b/src/misc/update.c @@ -1,10 +1,12 @@ /***************************************************************************** - * update.c: VLC update and plugins download + * update.c: VLC update checking and downloading ***************************************************************************** - * Copyright (C) 2005 the VideoLAN team - * $Id: $ + * Copyright © 2005-2008 the VideoLAN team + * $Id$ * * Authors: Antoine Cellerier + * Rémi Duraffort + Rafaël Carré * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,126 +23,107 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************/ -/* TODO - * --> check release types. - * --> make sure that the version comparision method is ok. - */ - /** * \file - * This file contains functions related to VLC and plugins update management + * This file contains functions related to VLC update management */ /***************************************************************************** * Preamble *****************************************************************************/ -#include +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif -#include /* tolower() */ +#include +#include +#ifdef UPDATE_CHECK -#include +#include -#include +#include #include -#include +#include +#include +#include #include -#include + +#include +#include + +#include + +#include "update.h" +#include "../libvlc.h" /***************************************************************************** * Misc defines *****************************************************************************/ -/* All release notes and source packages should match on "*" - * Only binary installers are OS specific ( we only provide these - * for Win32, Mac OS X, WincCE, beos(?) ) */ +/* + * Here is the format of these "status files" : + * First line is the last version: "X.Y.Ze" where: + * * X is the major number + * * Y is the minor number + * * Z is the revision number + * * e is an OPTIONAL extra letter + * * AKA "0.8.6d" or "0.9.0" + * Second line is an url of the binary for this last version + * Remaining text is a required description of the update + */ + #if defined( UNDER_CE ) -# define UPDATE_VLC_OS "*" -# define UPDATE_VLC_ARCH "*" +# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce" #elif defined( WIN32 ) -# define UPDATE_VLC_OS "windows" -# define UPDATE_VLC_ARCH "i386" +# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86" #elif defined( __APPLE__ ) -# define UPDATE_VLC_OS "macosx" # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ ) -# define UPDATE_VLC_ARCH "ppc" +# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc" # else -# define UPDATE_VLC_ARCH "x86" +# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86" # endif #elif defined( SYS_BEOS ) -# define UPDATE_VLC_OS "beos" -# define UPDATE_VLC_ARCH "i386" +# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86" #else -# define UPDATE_VLC_OS "*" -# define UPDATE_VLC_ARCH "*" +# define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status" #endif -#define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status.xml" -#define UPDATE_VLC_MIRRORS_URL "http://update.videolan.org/mirrors.xml" - -#define STRDUP( a ) ( a ? strdup( a ) : NULL ) - -/***************************************************************************** - * Local Prototypes - *****************************************************************************/ - -void FreeMirrorsList( update_t * ); -void FreeReleasesList( update_t * ); -void GetMirrorsList( update_t *, vlc_bool_t ); -void GetFilesList( update_t *, vlc_bool_t ); - -int CompareReleases( struct update_release_t *, struct update_release_t * ); -int CompareReleaseToCurrent( struct update_release_t * ); - -unsigned int update_iterator_Reset( update_iterator_t * ); -unsigned int update_iterator_NextFile( update_iterator_t * ); -unsigned int update_iterator_PrevFile( update_iterator_t * ); -unsigned int update_iterator_NextMirror( update_iterator_t * ); -unsigned int update_iterator_PrevMirror( update_iterator_t * ); - -void update_iterator_GetData( update_iterator_t * ); -void update_iterator_ClearData( update_iterator_t * ); /***************************************************************************** * Update_t functions *****************************************************************************/ +#undef update_New /** * Create a new update VLC struct * * \param p_this the calling vlc_object * \return pointer to new update_t or NULL */ -update_t *__update_New( vlc_object_t *p_this ) +update_t *update_New( vlc_object_t *p_this ) { update_t *p_update; - - if( p_this == NULL ) return NULL; + assert( p_this ); p_update = (update_t *)malloc( sizeof( update_t ) ); if( !p_update ) return NULL; - vlc_mutex_init( p_this, &p_update->lock ); + vlc_mutex_init( &p_update->lock ); p_update->p_libvlc = p_this->p_libvlc; - p_update->p_releases = NULL; - p_update->i_releases = 0; - p_update->b_releases = VLC_FALSE; + p_update->release.psz_url = NULL; + p_update->release.psz_desc = NULL; - p_update->p_mirrors = NULL; - p_update->i_mirrors = 0; - p_update->b_mirrors = VLC_FALSE; + p_update->p_download = NULL; + p_update->p_check = NULL; -#if 1 - msg_Err( p_this, "Auto-update currently disabled." ); - vlc_mutex_destroy( &p_update->lock ); - free( p_update ); - return NULL; -#else - return p_update -#endif + p_update->p_pkey = NULL; + vlc_gcrypt_init(); + + return p_update; } /** @@ -151,1199 +134,668 @@ update_t *__update_New( vlc_object_t *p_this ) */ void update_Delete( update_t *p_update ) { - vlc_mutex_destroy( &p_update->lock ); - FreeMirrorsList( p_update ); - FreeReleasesList( p_update ); - free( p_update ); -} + assert( p_update ); -/** - * Empty the mirrors list - * *p_update should be locked before using this function - * - * \param p_update pointer to the update struct - * \return nothing - */ -void FreeMirrorsList( update_t *p_update ) -{ - int i; + if( p_update->p_check ) + { + vlc_object_kill( p_update->p_check ); + vlc_thread_join( p_update->p_check ); + vlc_object_release( p_update->p_check ); + } - for( i = 0; i < p_update->i_mirrors; i++ ) + if( p_update->p_download ) { - free( p_update->p_mirrors[i].psz_name ); - free( p_update->p_mirrors[i].psz_location ); - free( p_update->p_mirrors[i].psz_type ); - free( p_update->p_mirrors[i].psz_base_url ); + vlc_object_kill( p_update->p_download ); + vlc_thread_join( p_update->p_download ); + vlc_object_release( p_update->p_download ); } - FREENULL( p_update->p_mirrors ); - p_update->i_mirrors = 0; - p_update->b_mirrors = VLC_FALSE; + + vlc_mutex_destroy( &p_update->lock ); + + free( p_update->release.psz_url ); + free( p_update->release.psz_desc ); + free( p_update->p_pkey ); + + free( p_update ); } /** - * Empty the releases list - * *p_update should be locked before calling this function + * Empty the release struct * - * \param p_update pointer to the update struct + * \param p_update update_t* pointer * \return nothing */ -void FreeReleasesList( update_t *p_update ) +static void EmptyRelease( update_t *p_update ) { - int i; + p_update->release.i_major = 0; + p_update->release.i_minor = 0; + p_update->release.i_revision = 0; - for( i = 0; i < p_update->i_releases; i++ ) - { - int j; - struct update_release_t *p_release = (p_update->p_releases + i); - for( j = 0; j < p_release->i_files; j++ ) - { - free( p_release->p_files[j].psz_md5 ); - free( p_release->p_files[j].psz_url ); - free( p_release->p_files[j].psz_description ); - } - free( p_release->psz_major ); - free( p_release->psz_minor ); - free( p_release->psz_revision ); - free( p_release->psz_extra ); - free( p_release->psz_svn_revision ); - free( p_release->p_files ); - } - FREENULL( p_update->p_releases ); - p_update->i_releases = 0; - p_update->b_releases = VLC_FALSE; + FREENULL( p_update->release.psz_url ); + FREENULL( p_update->release.psz_desc ); } /** - * Get the mirrors list XML file and parse it - * *p_update has to be unlocked when calling this function + * Get the update file and parse it + * p_update has to be locked when calling this function * - * \param p_update pointer to the update struct - * \param b_force set to VLC_TRUE if you want to force the mirrors list update - * \return nothing + * \param p_update pointer to update struct + * \return true if the update is valid and authenticated */ -void GetMirrorsList( update_t *p_update, vlc_bool_t b_force ) +static bool GetUpdateFile( update_t *p_update ) { stream_t *p_stream = NULL; + int i_major = 0; + int i_minor = 0; + int i_revision = 0; + unsigned char extra; + char *psz_version_line = NULL; + char *psz_update_data = NULL; - xml_t *p_xml = NULL; - xml_reader_t *p_xml_reader = NULL; - char *psz_eltname = NULL; - //char *psz_eltvalue = NULL; - char *psz_name = NULL; - char *psz_value = NULL; - struct update_mirror_t tmp_mirror; - - vlc_mutex_lock( &p_update->lock ); - - memset( &tmp_mirror, 0, sizeof(struct update_mirror_t)); - - if( p_update->b_mirrors && b_force == VLC_FALSE ) - { - vlc_mutex_unlock( &p_update->lock ); - return; - } - - p_xml = xml_Create( p_update->p_libvlc ); - if( !p_xml ) - { - msg_Err( p_update->p_libvlc, "Failed to open XML parser" ); - goto error; - } - - p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_MIRRORS_URL ); + p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL ); if( !p_stream ) { msg_Err( p_update->p_libvlc, "Failed to open %s for reading", - UPDATE_VLC_MIRRORS_URL ); + UPDATE_VLC_STATUS_URL ); goto error; } - p_xml_reader = xml_ReaderCreate( p_xml, p_stream ); - - if( !p_xml_reader ) - { - msg_Err( p_update->p_libvlc, "Failed to open %s for parsing", - UPDATE_VLC_MIRRORS_URL ); + const int64_t i_read = stream_Size( p_stream ); + psz_update_data = malloc( i_read + 1 ); /* terminating '\0' */ + if( !psz_update_data ) goto error; - } - - if( p_update->p_mirrors ) - { - FreeMirrorsList( p_update ); - } - while( xml_ReaderRead( p_xml_reader ) == 1 ) + if( stream_Read( p_stream, psz_update_data, i_read ) != i_read ) { - switch( xml_ReaderNodeType( p_xml_reader ) ) - { - case -1: - msg_Err( p_update->p_libvlc, "Error while parsing %s", - UPDATE_VLC_MIRRORS_URL ); - goto error; - - case XML_READER_STARTELEM: - psz_eltname = xml_ReaderName( p_xml_reader ); - if( !psz_eltname ) - { - msg_Err( p_update->p_libvlc, "Error while parsing %s", - UPDATE_VLC_MIRRORS_URL ); - goto error; - } - - while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS ) - { - psz_name = xml_ReaderName( p_xml_reader ); - psz_value = xml_ReaderValue( p_xml_reader ); - - if( !psz_name || !psz_value ) - { - msg_Err( p_update->p_libvlc, "Error while parsing %s", - UPDATE_VLC_MIRRORS_URL ); - goto error; - } - - if( !strcmp( psz_eltname, "mirror" ) ) - { - if( !strcmp( psz_name, "name" ) ) - tmp_mirror.psz_name = STRDUP( psz_value ); - else if( !strcmp( psz_name, "location" ) ) - tmp_mirror.psz_location = STRDUP( psz_value ); - } - else if( !strcmp( psz_eltname, "url" ) ) - { - if( !strcmp( psz_name, "type" ) ) - tmp_mirror.psz_type = STRDUP( psz_value ); - else if( !strcmp( psz_name, "base" ) ) - tmp_mirror.psz_base_url = STRDUP( psz_value ); - } - FREENULL( psz_name ); - FREENULL( psz_value ); - } - if( !strcmp( psz_eltname, "url" ) ) - { - /* append to mirrors list */ - p_update->p_mirrors = - (struct update_mirror_t *)realloc( p_update->p_mirrors, - (++(p_update->i_mirrors)) - *sizeof( struct update_mirror_t ) ); - p_update->p_mirrors[ p_update->i_mirrors - 1 ] = - tmp_mirror; - tmp_mirror.psz_name = STRDUP( tmp_mirror.psz_name ); - tmp_mirror.psz_location = STRDUP( tmp_mirror.psz_location ); - tmp_mirror.psz_type = NULL; - tmp_mirror.psz_base_url = NULL; - } - FREENULL( psz_eltname ); - break; - - case XML_READER_ENDELEM: - psz_eltname = xml_ReaderName( p_xml_reader ); - if( !psz_eltname ) - { - msg_Err( p_update->p_libvlc, "Error while parsing %s", - UPDATE_VLC_MIRRORS_URL ); - goto error; - } - - if( !strcmp( psz_eltname, "mirror" ) ) - { - FREENULL( tmp_mirror.psz_name ); - FREENULL( tmp_mirror.psz_location ); - } - - FREENULL( psz_eltname ); - break; - - /*case XML_READER_TEXT: - psz_eltvalue = xml_ReaderValue( p_xml_reader ); - FREENULL( psz_eltvalue ); - break;*/ - } + msg_Err( p_update->p_libvlc, "Couldn't download update file %s", + UPDATE_VLC_STATUS_URL ); + goto error; } + psz_update_data[i_read] = '\0'; - p_update->b_mirrors = VLC_TRUE; - - error: - vlc_mutex_unlock( &p_update->lock ); - - free( psz_eltname ); - //free( psz_eltvalue ); - free( psz_name ); - free( psz_value ); - - free( tmp_mirror.psz_name ); - free( tmp_mirror.psz_location ); - free( tmp_mirror.psz_type ); - free( tmp_mirror.psz_base_url ); - - if( p_xml_reader && p_xml ) - xml_ReaderDelete( p_xml, p_xml_reader ); - if( p_stream ) - stream_Delete( p_stream ); - if( p_xml ) - xml_Delete( p_xml ); -} - -/** - * Get the files list XML file and parse it - * *p_update has to be unlocked when calling this function - * - * \param p_update pointer to update struct - * \param b_force set to VLC_TRUE if you want to force the files list update - * \return nothing - */ -void GetFilesList( update_t *p_update, vlc_bool_t b_force ) -{ - stream_t *p_stream = NULL; - - xml_t *p_xml = NULL; - xml_reader_t *p_xml_reader = NULL; - - char *psz_eltname = NULL; - char *psz_eltvalue = NULL; - char *psz_name = NULL; - char *psz_value = NULL; + stream_Delete( p_stream ); + p_stream = NULL; - struct update_release_t *p_release = NULL; - struct update_release_t tmp_release; - struct update_file_t tmp_file; + /* first line : version number */ + char *psz_update_data_parser = psz_update_data; + size_t i_len = strcspn( psz_update_data, "\r\n" ); + psz_update_data_parser += i_len; + while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' ) + psz_update_data_parser++; - vlc_bool_t b_os = VLC_FALSE, b_arch = VLC_FALSE; + if( !(psz_version_line = malloc( i_len + 1)) ) + goto error; + strncpy( psz_version_line, psz_update_data, i_len ); + psz_version_line[i_len] = '\0'; + + p_update->release.extra = 0; + switch( sscanf( psz_version_line, "%i.%i.%i%c", + &i_major, &i_minor, &i_revision, &extra ) ) + { + case 4: + p_update->release.extra = extra; + case 3: + p_update->release.i_major = i_major; + p_update->release.i_minor = i_minor; + p_update->release.i_revision = i_revision; + break; + default: + msg_Err( p_update->p_libvlc, "Update version false formated" ); + goto error; + } + + /* second line : URL */ + i_len = strcspn( psz_update_data_parser, "\r\n" ); + if( i_len == 0 ) + { + msg_Err( p_update->p_libvlc, "Update file %s is corrupted: URL missing", + UPDATE_VLC_STATUS_URL ); - memset( &tmp_release, 0, sizeof(struct update_release_t) ); - memset( &tmp_file, 0, sizeof(struct update_file_t) ); + goto error; + } - tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE; + if( !(p_update->release.psz_url = malloc( i_len + 1)) ) + goto error; + strncpy( p_update->release.psz_url, psz_update_data_parser, i_len ); + p_update->release.psz_url[i_len] = '\0'; - vlc_mutex_lock( &p_update->lock ); + psz_update_data_parser += i_len; + while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' ) + psz_update_data_parser++; - if( p_update->b_releases && b_force == VLC_FALSE ) + /* Remaining data : description */ + i_len = strlen( psz_update_data_parser ); + if( i_len == 0 ) { - vlc_mutex_unlock( &p_update->lock ); - return; + msg_Err( p_update->p_libvlc, + "Update file %s is corrupted: description missing", + UPDATE_VLC_STATUS_URL ); + goto error; } - p_xml = xml_Create( p_update->p_libvlc ); - if( !p_xml ) + if( !(p_update->release.psz_desc = malloc( i_len + 1)) ) + goto error; + strncpy( p_update->release.psz_desc, psz_update_data_parser, i_len ); + p_update->release.psz_desc[i_len] = '\0'; + + printf("desc %s\n", p_update->release.psz_desc); + + /* Now that we know the status is valid, we must download its signature + * to authenticate it */ + signature_packet_t sign; + if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign, + UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS ) { - msg_Err( p_update->p_libvlc, "Failed to open XML parser" ); + msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" ); goto error; } - p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL ); - if( !p_stream ) + if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE ) { - msg_Err( p_update->p_libvlc, "Failed to open %s for reading", - UPDATE_VLC_STATUS_URL ); + msg_Err( p_update->p_libvlc, "Invalid signature type" ); goto error; } - p_xml_reader = xml_ReaderCreate( p_xml, p_stream ); + p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) ); + if( !p_update->p_pkey ) + goto error; - if( !p_xml_reader ) + if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ), + p_update->p_pkey, NULL ) != VLC_SUCCESS ) { - msg_Err( p_update->p_libvlc, "Failed to open %s for parsing", - UPDATE_VLC_STATUS_URL ); + msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." ); + FREENULL( p_update->p_pkey ); goto error; } - if( p_update->p_releases ) - { - FreeReleasesList( p_update ); - } + memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 ); - while( xml_ReaderRead( p_xml_reader ) == 1 ) + if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 ) { - switch( xml_ReaderNodeType( p_xml_reader ) ) + msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" ); + public_key_t *p_new_pkey = download_key( + VLC_OBJECT(p_update->p_libvlc), + sign.issuer_longid, videolan_public_key_longid ); + if( !p_new_pkey ) { - case -1: - msg_Err( p_update->p_libvlc, "Error while parsing %s", - UPDATE_VLC_STATUS_URL ); - goto error; - - case XML_READER_STARTELEM: - psz_eltname = xml_ReaderName( p_xml_reader ); - if( !psz_eltname ) - { - msg_Err( p_update->p_libvlc, "Error while parsing %s", - UPDATE_VLC_STATUS_URL ); - goto error; - } - - while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS ) - { - psz_name = xml_ReaderName( p_xml_reader ); - psz_value = xml_ReaderValue( p_xml_reader ); - - if( !psz_name || !psz_value ) - { - msg_Err( p_update->p_libvlc, "Error while parsing %s", - UPDATE_VLC_STATUS_URL ); - goto error; - } - - if( b_os && b_arch ) - { - if( strcmp( psz_eltname, "version" ) == 0 ) - { - if( !strcmp( psz_name, "major" ) ) - tmp_release.psz_major = STRDUP( psz_value ); - else if( !strcmp( psz_name, "minor" ) ) - tmp_release.psz_minor = STRDUP( psz_value ); - else if( !strcmp( psz_name, "revision" ) ) - tmp_release.psz_revision = STRDUP( psz_value ); - else if( !strcmp( psz_name, "extra" ) ) - tmp_release.psz_extra = STRDUP( psz_value ); - else if( !strcmp( psz_name, "svn" ) ) - tmp_release.psz_svn_revision = - STRDUP( psz_value ); - else if( !strcmp( psz_name, "version" ) ) - { - if( !strcmp( psz_value, "unstable" ) ) - tmp_release.i_type = - UPDATE_RELEASE_TYPE_UNSTABLE; - else if( !strcmp( psz_value, "testing" ) ) - tmp_release.i_type = - UPDATE_RELEASE_TYPE_TESTING; - else - tmp_release.i_type = - UPDATE_RELEASE_TYPE_STABLE; - } - } - else if( !strcmp( psz_eltname, "file" ) ) - { - if( !strcmp( psz_name, "type" ) ) - { - if( !strcmp( psz_value, "info" ) ) - tmp_file.i_type = UPDATE_FILE_TYPE_INFO; - else if( !strcmp( psz_value, "source" ) ) - tmp_file.i_type = UPDATE_FILE_TYPE_SOURCE; - else if( !strcmp( psz_value, "binary" ) ) - tmp_file.i_type = UPDATE_FILE_TYPE_BINARY; - else if( !strcmp( psz_value, "plugin" ) ) - tmp_file.i_type = UPDATE_FILE_TYPE_PLUGIN; - else - tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF; - } - else if( !strcmp( psz_name, "md5" ) ) - tmp_file.psz_md5 = STRDUP( psz_value ); - else if( !strcmp( psz_name, "size" ) ) - tmp_file.l_size = atol( psz_value ); - else if( !strcmp( psz_name, "url" ) ) - tmp_file.psz_url = STRDUP( psz_value ); - } - } - if( !strcmp( psz_name, "name" ) - && ( !strcmp( psz_value, UPDATE_VLC_OS ) - || !strcmp( psz_value, "*" ) ) - && !strcmp( psz_eltname, "os" ) ) - { - b_os = VLC_TRUE; - } - if( b_os && !strcmp( psz_name, "name" ) - && ( !strcmp( psz_value, UPDATE_VLC_ARCH ) - || !strcmp( psz_value, "*" ) ) - && !strcmp( psz_eltname, "arch" ) ) - { - b_arch = VLC_TRUE; - } - FREENULL( psz_name ); - FREENULL( psz_value ); - } - if( ( b_os && b_arch && strcmp( psz_eltname, "arch" ) ) ) - { - if( !strcmp( psz_eltname, "version" ) ) - { - int i; - /* look for a previous occurrence of this release */ - for( i = 0; i < p_update->i_releases; i++ ) - { - p_release = p_update->p_releases + i; - if( CompareReleases( p_release, &tmp_release ) - == UPDATE_RELEASE_STATUS_EQUAL ) - { - break; - } - } - /* if this is the first time that we see this release, - * append it to the list of releases */ - if( i == p_update->i_releases ) - { - tmp_release.i_status = - CompareReleaseToCurrent( &tmp_release ); - p_update->p_releases = - (struct update_release_t *)realloc( p_update->p_releases, - (++(p_update->i_releases))*sizeof( struct update_release_t ) ); - p_update->p_releases[ p_update->i_releases - 1 ] = - tmp_release; - p_release = - p_update->p_releases + p_update->i_releases - 1; - tmp_release.psz_major = NULL; - tmp_release.psz_minor = NULL; - tmp_release.psz_revision = NULL; - tmp_release.psz_extra = NULL; - tmp_release.psz_svn_revision = NULL; - tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE; - tmp_release.i_status = 0; - tmp_release.p_files = NULL; - tmp_release.i_files = 0; - } - else - { - FREENULL( tmp_release.psz_major ); - FREENULL( tmp_release.psz_minor ); - FREENULL( tmp_release.psz_revision ); - FREENULL( tmp_release.psz_extra ); - FREENULL( tmp_release.psz_svn_revision ); - tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE; - FREENULL( tmp_release.p_files ); - tmp_release.i_files = 0; - } - } - else if( !strcmp( psz_eltname, "file" ) ) - { - /* append file to p_release's file list */ - if( p_release == NULL ) - { - goto error; - } - p_release->p_files = - (struct update_file_t *)realloc( p_release->p_files, - (++(p_release->i_files))*sizeof( struct update_file_t ) ); - p_release->p_files[ p_release->i_files - 1 ] = tmp_file; - tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF; - tmp_file.psz_md5 = NULL; - tmp_file.l_size = 0; - tmp_file.psz_url = NULL; - tmp_file.psz_description = NULL; - } - } - FREENULL( psz_eltname ); - break; - - case XML_READER_ENDELEM: - psz_eltname = xml_ReaderName( p_xml_reader ); - if( !psz_eltname ) - { - msg_Err( p_update->p_libvlc, "Error while parsing %s", - UPDATE_VLC_STATUS_URL ); - goto error; - } - - if( !strcmp( psz_eltname, "os" ) ) - b_os = VLC_FALSE; - else if( !strcmp( psz_eltname, "arch" ) ) - b_arch = VLC_FALSE; - FREENULL( psz_eltname ); - break; - - case XML_READER_TEXT: - psz_eltvalue = xml_ReaderValue( p_xml_reader ); - if( p_release && p_release->i_files ) - p_release->p_files[ p_release->i_files - 1 ] - .psz_description = STRDUP( psz_eltvalue ); - FREENULL( psz_eltvalue ); - break; + msg_Err( p_update->p_libvlc, "Couldn't download GPG key" ); + FREENULL( p_update->p_pkey ); + goto error; } - } - - p_update->b_releases = VLC_TRUE; - - error: - vlc_mutex_unlock( &p_update->lock ); - free( psz_eltname ); - free( psz_eltvalue ); - free( psz_name ); - free( psz_value ); + uint8_t *p_hash = hash_sha1_from_public_key( p_new_pkey ); + if( !p_hash ) + { + msg_Err( p_update->p_libvlc, "Failed to hash signature" ); + free( p_new_pkey ); + FREENULL( p_update->p_pkey ); + goto error; + } - free( tmp_release.psz_major ); - free( tmp_release.psz_minor ); - free( tmp_release.psz_revision ); - free( tmp_release.psz_extra ); - free( tmp_release.psz_svn_revision ); + if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s, + &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS ) + { + free( p_hash ); + msg_Info( p_update->p_libvlc, "Key authenticated" ); + free( p_update->p_pkey ); + p_update->p_pkey = p_new_pkey; + } + else + { + free( p_hash ); + msg_Err( p_update->p_libvlc, "Key signature invalid !\n" ); + goto error; + } + } - free( tmp_file.psz_md5 ); - free( tmp_file.psz_url ); - free( tmp_file.psz_description ); + uint8_t *p_hash = hash_sha1_from_text( psz_update_data, &sign ); + if( !p_hash ) + { + msg_Warn( p_update->p_libvlc, "Can't compute SHA1 hash for status file" ); + goto error; + } - if( p_xml_reader && p_xml ) - xml_ReaderDelete( p_xml, p_xml_reader ); - if( p_stream ) - stream_Delete( p_stream ); - if( p_xml ) - xml_Delete( p_xml ); -} + else if( p_hash[0] != sign.hash_verification[0] || + p_hash[1] != sign.hash_verification[1] ) + { + msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" ); + goto error; + } -/** - * Check for updates - * - * \param p_update pointer to update struct - * \param b_force set to VLC_TRUE if you want to force the update - * \returns nothing - */ -void update_Check( update_t *p_update, vlc_bool_t b_force ) -{ - if( p_update == NULL ) return; - GetMirrorsList( p_update, b_force ); - GetFilesList( p_update, b_force ); -} + else if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash ) + != VLC_SUCCESS ) + { + msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" ); + goto error; + } -/** - * Compare two release numbers - * The comparision algorith basically performs an alphabetical order (strcmp) - * comparision of each of the version number elements until it finds two - * different ones. This is the tricky function. - * - * \param p1 first release - * \param p2 second release - * \return like strcmp - */ -int CompareReleases( struct update_release_t *p1, struct update_release_t *p2 ) -{ - int d; - if( ( d = strcmp( p1->psz_major, p2->psz_major ) ) ) ; - else if( ( d = strcmp( p1->psz_minor, p2->psz_minor ) ) ) ; - else if( ( d = strcmp( p1->psz_revision, p2->psz_revision ) ) ) ; else { - d = strcmp( p1->psz_extra, p2->psz_extra ); - if( d<0 ) - { - /* FIXME: - * not num < NULL < num - * -test and -svn releases are thus always considered older than - * -'' or -0 releases, which is the best i could come up with */ - char *psz_end1; - char *psz_end2; - strtol( p1->psz_extra, &psz_end1, 10 ); - strtol( p2->psz_extra, &psz_end2, 10 ); - if( psz_end2 == p2->psz_extra - && ( psz_end1 != p1->psz_extra || *p1->psz_extra == '\0' ) ) - d = 1; - } + msg_Info( p_update->p_libvlc, "Status file authenticated" ); + return true; } - if( d < 0 ) - return UPDATE_RELEASE_STATUS_OLDER; - else if( d == 0 ) - return UPDATE_RELEASE_STATUS_EQUAL; - else - return UPDATE_RELEASE_STATUS_NEWER; -} -/** - * Compare a given release's version number to the current VLC's one - * - * \param p a release - * \return >0 if newer, 0 if equal and <0 if older - */ -int CompareReleaseToCurrent( struct update_release_t *p ) -{ - struct update_release_t c; - int r; - - memset( &c, 0, sizeof(struct update_release_t) ); - c.psz_major = STRDUP( PACKAGE_VERSION_MAJOR ); - c.psz_minor = STRDUP( PACKAGE_VERSION_MINOR ); - c.psz_revision = STRDUP( PACKAGE_VERSION_REVISION ); - c.psz_extra = STRDUP( PACKAGE_VERSION_EXTRA ); - r = CompareReleases( p, &c ); - free( c.psz_major ); - free( c.psz_minor ); - free( c.psz_revision ); - free( c.psz_extra ); - return r; +error: + if( p_stream ) + stream_Delete( p_stream ); + free( psz_version_line ); + free( psz_update_data ); + return false; } -/***************************************************************************** - * Updatei_iterator_t functions - *****************************************************************************/ +static void* update_CheckReal( vlc_object_t *p_this ); /** - * Create a new update iterator structure. This structure can then be used to - * describe a position and move through the update and mirror trees/lists. - * This will use an existing update struct or create a new one if none is - * found + * Check for updates * - * \param p_u the calling update_t - * \return a pointer to an update iterator + * \param p_update pointer to update struct + * \param pf_callback pointer to a function to call when the update_check is finished + * \param p_data pointer to some datas to give to the callback + * \returns nothing */ -update_iterator_t *update_iterator_New( update_t *p_u ) +void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data ) { - update_iterator_t *p_uit = NULL; + assert( p_update ); - if( p_u == NULL ) - return NULL; + // If the object already exist, destroy it + if( p_update->p_check ) + { + vlc_object_kill( p_update->p_check ); + vlc_thread_join( p_update->p_check ); + vlc_object_release( p_update->p_check ); + } - p_uit = (update_iterator_t *)malloc( sizeof( update_iterator_t ) ); - if( p_uit == NULL ) return NULL; + update_check_thread_t *p_uct = + vlc_custom_create( p_update->p_libvlc, sizeof( *p_uct ), + VLC_OBJECT_GENERIC, "update check" ); + if( !p_uct ) return; - p_uit->p_u = p_u; + p_uct->p_update = p_update; + p_update->p_check = p_uct; + p_uct->pf_callback = pf_callback; + p_uct->p_data = p_data; - p_uit->i_m = -1; - p_uit->i_r = -1; - p_uit->i_f = -1; + vlc_thread_create( p_uct, "check for update", update_CheckReal, + VLC_THREAD_PRIORITY_LOW ); +} - p_uit->i_t = UPDATE_FILE_TYPE_ALL; - p_uit->i_rs = UPDATE_RELEASE_STATUS_ALL; - p_uit->i_rt = UPDATE_RELEASE_TYPE_STABLE; +void* update_CheckReal( vlc_object_t* p_this ) +{ + update_check_thread_t *p_uct = (update_check_thread_t *)p_this; + bool b_ret; + int canc; - p_uit->file.i_type = UPDATE_FILE_TYPE_NONE; - p_uit->file.psz_md5 = NULL; - p_uit->file.psz_url = NULL; - p_uit->file.l_size = 0; - p_uit->file.psz_description = NULL; + canc = vlc_savecancel (); + vlc_mutex_lock( &p_uct->p_update->lock ); - p_uit->release.psz_version = NULL; - p_uit->release.psz_svn_revision = NULL; - p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE; - p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE; + EmptyRelease( p_uct->p_update ); + b_ret = GetUpdateFile( p_uct->p_update ); + vlc_mutex_unlock( &p_uct->p_update->lock ); - p_uit->mirror.psz_name = NULL; - p_uit->mirror.psz_location = NULL; - p_uit->mirror.psz_type = NULL; + if( p_uct->pf_callback ) + (p_uct->pf_callback)( p_uct->p_data, b_ret ); - return p_uit; + vlc_restorecancel (canc); + return NULL; } /** - * Delete an update iterator structure (duh!) + * Compare a given release's version number to the current VLC's one * - * \param p_uit pointer to an update iterator - * \return nothing + * \param p_update structure + * \return true if we have to upgrade to the given version to be up to date */ -void update_iterator_Delete( update_iterator_t *p_uit ) +static bool is_strictly_greater( int * a, int * b, int n) +{ + if( n <= 0 ) return false; + if(a[0] > b[0] ) return true; + if(a[0] == b[0] ) return is_strictly_greater( a+1, b+1, n-1 ); + /* a[0] < b[0] */ return false; +} + +bool update_NeedUpgrade( update_t *p_update ) { - if( !p_uit ) return; - update_iterator_ClearData( p_uit ); - free( p_uit ); + assert( p_update ); + + int current_version[] = { + *PACKAGE_VERSION_MAJOR - '0', + *PACKAGE_VERSION_MINOR - '0', + *PACKAGE_VERSION_REVISION - '0', + /* extra string of development versions is "-git", "-rc" .. + * so make sure version a.b.c is newer than a.b.c-XXX */ + (*PACKAGE_VERSION_EXTRA == '-') ? -1 : *PACKAGE_VERSION_EXTRA + }; + int latest_version[] = { + p_update->release.i_major, + p_update->release.i_minor, + p_update->release.i_revision, + p_update->release.extra + }; + + return is_strictly_greater( latest_version, current_version, 4 ); } /** - * Reset an update_iterator_t structure + * Convert a long int size in bytes to a string * - * \param p_uit pointer to an update iterator - * \return UPDATE_FAIL upon error, UPDATE_SUCCESS otherwise + * \param l_size the size in bytes + * \return the size as a string */ -unsigned int update_iterator_Reset( update_iterator_t *p_uit ) +static char *size_str( long int l_size ) { - if( !p_uit ) return UPDATE_FAIL; - - p_uit->i_r = -1; - p_uit->i_f = -1; - p_uit->i_m = -1; + char *psz_tmp = NULL; + int i_retval = 0; + if( l_size >> 30 ) + i_retval = asprintf( &psz_tmp, _("%.1f GB"), (float)l_size/(1<<30) ); + else if( l_size >> 20 ) + i_retval = asprintf( &psz_tmp, _("%.1f MB"), (float)l_size/(1<<20) ); + else if( l_size >> 10 ) + i_retval = asprintf( &psz_tmp, _("%.1f kB"), (float)l_size/(1<<10) ); + else + i_retval = asprintf( &psz_tmp, _("%ld B"), l_size ); - update_iterator_ClearData( p_uit ); - return UPDATE_SUCCESS; + return i_retval == -1 ? NULL : psz_tmp; } +static void* update_DownloadReal( vlc_object_t *p_this ); + /** - * Finds the next file in the update tree that matches status and type - * requirements set in the update_iterator + * Download the file given in the update_t * - * \param p_uit update iterator - * \return UPDATE_FAIL if we can't find the next file, UPDATE_SUCCESS|UPDATE_FILE if we stay in the same release, UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE if we change the release index + * \param p_update structure + * \param dir to store the download file + * \return nothing */ -unsigned int update_iterator_NextFile( update_iterator_t *p_uit ) +void update_Download( update_t *p_update, const char *psz_destdir ) { - int r,f=-1,old_r; + assert( p_update ); - if( !p_uit ) return UPDATE_FAIL; - - old_r=p_uit->i_r; - - /* if the update iterator was already in a "no match" state, start over */ - if( p_uit->i_r == -1 ) p_uit->i_r = 0; - //if( p_uit->i_f == -1 ) p_uit->i_f = 0; - - vlc_mutex_lock( &p_uit->p_u->lock ); - - for( r = p_uit->i_r; r < p_uit->p_u->i_releases; r++ ) + // If the object already exist, destroy it + if( p_update->p_download ) { - if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue; - for( f = ( r == p_uit->i_r ? p_uit->i_f + 1 : 0 ); - f < p_uit->p_u->p_releases[r].i_files; f++ ) - { - if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t ) - { - goto done;/* "double break" */ - } - } + vlc_object_kill( p_update->p_download ); + vlc_thread_join( p_update->p_download ); + vlc_object_release( p_update->p_download ); } - done: - p_uit->i_r = r; - p_uit->i_f = f; - r = p_uit->p_u->i_releases; + update_download_thread_t *p_udt = + vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ), + VLC_OBJECT_GENERIC, "update download" ); + if( !p_udt ) + return; - if( old_r == p_uit->i_r ) - { - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); - return UPDATE_SUCCESS|UPDATE_FILE; - } - else if( p_uit->i_r == r ) - { - p_uit->i_r = -1; - p_uit->i_f = -1; - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); - return UPDATE_FAIL; - } - else - { - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); - return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE; - } + p_udt->p_update = p_update; + p_update->p_download = p_udt; + p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL; + + vlc_thread_create( p_udt, "download update", update_DownloadReal, + VLC_THREAD_PRIORITY_LOW ); } -/** - * Finds the previous file in the update tree that matches status and type - * requirements set in the update_iterator - * - * \param p_uit update iterator - * \return UPDATE_FAIL if we can't find the previous file, UPDATE_SUCCESS|UPDATE_FILE if we stay in the same release, UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE if we change the release index - */ -//TODO: test -unsigned int update_iterator_PrevFile( update_iterator_t *p_uit ) +static void* update_DownloadReal( vlc_object_t *p_this ) { - int r,f=-1,old_r; + update_download_thread_t *p_udt = (update_download_thread_t *)p_this; + dialog_progress_bar_t *p_progress = NULL; + long int l_size; + long int l_downloaded = 0; + float f_progress; + char *psz_status = NULL; + char *psz_downloaded = NULL; + char *psz_size = NULL; + char *psz_destfile = NULL; + char *psz_tmpdestfile = NULL; - if( !p_uit ) return UPDATE_FAIL; - - old_r=p_uit->i_r; + FILE *p_file = NULL; + stream_t *p_stream = NULL; + void* p_buffer = NULL; + int i_read; + int canc; - /* if the update iterator was already in a "no match" state, start over - * (begin at the end of the list) */ - if( p_uit->i_r == -1 ) p_uit->i_r = p_uit->p_u->i_releases - 1; - p_uit->i_f = p_uit->p_u->p_releases[p_uit->i_r].i_files + 1; + update_t *p_update = p_udt->p_update; + char *psz_destdir = p_udt->psz_destdir; - vlc_mutex_lock( &p_uit->p_u->lock ); + msg_Dbg( p_udt, "Opening Stream '%s'", p_update->release.psz_url ); + canc = vlc_savecancel (); - for( r = p_uit->i_r; r >= 0; r-- ) + /* Open the stream */ + p_stream = stream_UrlNew( p_udt, p_update->release.psz_url ); + if( !p_stream ) { - if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue; - for( f =( r==p_uit->i_r ? p_uit->i_f - 1 : p_uit->p_u->p_releases[r].i_files ); - f >= 0; f-- ) - { - if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t ) - { - goto done;/* "double break" */ - } - } + msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url ); + goto end; } - done: - p_uit->i_r = r; - p_uit->i_f = f; - r = p_uit->p_u->i_releases; + /* Get the stream size */ + l_size = stream_Size( p_stream ); - if( old_r == p_uit->i_r ) + /* Get the file name and open it*/ + psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' ); + if( !psz_tmpdestfile ) { - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); - return UPDATE_SUCCESS|UPDATE_FILE; + msg_Err( p_udt, "The URL %s is badly formated", + p_update->release.psz_url ); + goto end; } - else if( p_uit->i_r == -1 ) + psz_tmpdestfile++; + if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 ) + goto end; + + p_file = vlc_fopen( psz_destfile, "w" ); + if( !p_file ) { - p_uit->i_r = -1; - p_uit->i_f = -1; - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); - return UPDATE_FAIL; + msg_Err( p_udt, "Failed to open %s for writing", psz_destfile ); + dialog_FatalWait( p_udt, _("Saving file failed"), + _("Failed to open \"%s\" for writing"), + psz_destfile ); + goto end; } - else + + /* Create a buffer and fill it with the downloaded file */ + p_buffer = (void *)malloc( 1 << 10 ); + if( !p_buffer ) { - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); - return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE; + msg_Err( p_udt, "Can't malloc (1 << 10) bytes! download cancelled." ); + goto end; } -} -/** - * Finds the next mirror in the update tree - * - * \param update iterator - * \return UPDATE_FAIL if we can't find the next mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise - */ -unsigned int update_iterator_NextMirror( update_iterator_t *p_uit ) -{ - if( !p_uit ) return UPDATE_FAIL; - vlc_mutex_lock( &p_uit->p_u->lock ); - p_uit->i_m++; - if( p_uit->i_m >= p_uit->p_u->i_mirrors ) p_uit->i_m = -1; - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); - return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR; -} + msg_Dbg( p_udt, "Downloading Stream '%s'", p_update->release.psz_url ); -/** - * Finds the previous mirror in the update tree - * - * \param update iterator - * \return UPDATE_FAIL if we can't find a previous mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise - */ -unsigned int update_iterator_PrevMirror( update_iterator_t *p_uit ) -{ - if( !p_uit ) return UPDATE_FAIL; - vlc_mutex_lock( &p_uit->p_u->lock ); - p_uit->i_m--; - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); - return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR; -} - -/** - * Change the update iterator's position in the file and mirrors tree - * If position is negative, don't change it - * - * \param i_m position in mirrors list - * \param i_r position in releases list - * \param i_f position in release's files list - * \return UPDATE_FAIL when changing position fails or position wasn't changed, a combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE otherwise - */ -unsigned int update_iterator_ChooseMirrorAndFile( update_iterator_t *p_uit, - int i_m, int i_r, int i_f ) -{ - unsigned int i_val = 0; - - if( !p_uit ) return 0; - vlc_mutex_lock( &p_uit->p_u->lock ); - - if( i_m >= 0 ) + psz_size = size_str( l_size ); + if( asprintf( &psz_status, _("%s\nDownloading... %s/%s %.1f%% done"), + p_update->release.psz_url, "0.0", psz_size, 0.0 ) != -1 ) { - if( i_m < p_uit->p_u->i_mirrors ) - { - if( i_m != p_uit->i_m ) - i_val |= UPDATE_MIRROR; - p_uit->i_m = i_m; - } - else i_m = -1; + p_progress = dialog_ProgressCreate( p_udt, _( "Downloading ..."), + psz_status, _("Cancel") ); + free( psz_status ); } - if( i_r >= 0 ) + while( vlc_object_alive( p_udt ) && + ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) && + !dialog_ProgressCancelled( p_progress ) ) { - if( i_r < p_uit->p_u->i_releases ) + if( fwrite( p_buffer, i_read, 1, p_file ) < 1 ) { - if( i_r != p_uit->i_r ) - i_val |= UPDATE_FILE; - p_uit->i_r = i_r; + msg_Err( p_udt, "Failed to write into %s", psz_destfile ); + break; } - else i_r = -1; - } - if( i_f >= 0 ) - { - if( i_r >= 0 && i_r < p_uit->p_u->i_releases - && i_f < p_uit->p_u->p_releases[p_uit->i_r].i_files ) + l_downloaded += i_read; + psz_downloaded = size_str( l_downloaded ); + f_progress = (float)l_downloaded/(float)l_size; + + if( asprintf( &psz_status, _( "%s\nDownloading... %s/%s - %.1f%% done" ), + p_update->release.psz_url, psz_downloaded, psz_size, + f_progress*100 ) != -1 ) { - if( i_f != p_uit->i_f ) - i_val |= UPDATE_FILE; - p_uit->i_f = i_f; + dialog_ProgressSet( p_progress, psz_status, f_progress ); + free( psz_status ); } - else i_f = -1; + free( psz_downloaded ); } - update_iterator_GetData( p_uit ); - vlc_mutex_unlock( &p_uit->p_u->lock ); + /* Finish the progress bar or delete the file if the user had canceled */ + fclose( p_file ); + p_file = NULL; - if( ( i_m < 0 || p_uit->i_m >= 0 ) - && ( i_r < 0 || p_uit->i_r >= 0 ) - && ( i_f < 0 || p_uit->i_f >= 0 ) ) + if( vlc_object_alive( p_udt ) && + !dialog_ProgressCancelled( p_progress ) ) { - /* Everything worked */ - return UPDATE_SUCCESS|i_val; + if( asprintf( &psz_status, _("%s\nDone %s (100.0%%)"), + p_update->release.psz_url, psz_size ) != -1 ) + { + dialog_ProgressDestroy( p_progress ); + p_progress = NULL; + free( psz_status ); + } } else { - /* Something failed */ - return UPDATE_FAIL; + vlc_unlink( psz_destfile ); + goto end; } -} -/** - * Fills the iterator data (file, release and mirror structs) - * The update struct should be locked before calling this function. - * - * \param p_uit update iterator - * \return nothing - */ -void update_iterator_GetData( update_iterator_t *p_uit ) -{ - struct update_release_t *p_r = NULL; - struct update_file_t *p_f = NULL; - struct update_mirror_t *p_m = NULL; + signature_packet_t sign; + if( download_signature( VLC_OBJECT( p_udt ), &sign, + p_update->release.psz_url ) != VLC_SUCCESS ) + { + vlc_unlink( psz_destfile ); - update_iterator_ClearData( p_uit ); + dialog_FatalWait( p_udt, _("File could not be verified"), + _("It was not possible to download a cryptographic signature for " + "the downloaded file \"%s\". Thus, it was deleted."), + psz_destfile ); + msg_Err( p_udt, "Couldn't download signature of downloaded file" ); + goto end; + } - if( p_uit->i_m >= 0 ) + if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) ) { - p_m = p_uit->p_u->p_mirrors + p_uit->i_m; - p_uit->mirror.psz_name = STRDUP( p_m->psz_name ); - p_uit->mirror.psz_location = STRDUP( p_m->psz_location ); - p_uit->mirror.psz_type = STRDUP( p_m->psz_type ); + vlc_unlink( psz_destfile ); + msg_Err( p_udt, "Invalid signature issuer" ); + dialog_FatalWait( p_udt, _("Invalid signature"), + _("The cryptographic signature for the downloaded file \"%s\" was " + "invalid and could not be used to securely verify it. Thus, the " + "file was deleted."), + psz_destfile ); + goto end; } - if( p_uit->i_r >= 0 ) + if( sign.type != BINARY_SIGNATURE ) { - p_r = p_uit->p_u->p_releases + p_uit->i_r; - asprintf( &p_uit->release.psz_version, "%s.%s.%s-%s", - p_r->psz_major, - p_r->psz_minor, - p_r->psz_revision, - p_r->psz_extra ); - p_uit->release.psz_svn_revision = STRDUP( p_r->psz_svn_revision ); - p_uit->release.i_type = p_r->i_type; - p_uit->release.i_status = p_r->i_status; - if( p_uit->i_f >= 0 ) - { - p_f = p_r->p_files + p_uit->i_f; - p_uit->file.i_type = p_f->i_type; - p_uit->file.psz_md5 = STRDUP( p_f->psz_md5 ); - p_uit->file.l_size = p_f->l_size; - p_uit->file.psz_description = STRDUP( p_f->psz_description); - if( p_f->psz_url[0] == '/' ) - { - if( p_m ) - { - asprintf( &p_uit->file.psz_url, "%s%s", - p_m->psz_base_url, p_f->psz_url ); - } - } - else - { - p_uit->file.psz_url = STRDUP( p_f->psz_url ); - } - } + vlc_unlink( psz_destfile ); + msg_Err( p_udt, "Invalid signature type" ); + dialog_FatalWait( p_udt, _("Invalid signature"), + _("The cryptographic signature for the downloaded file \"%s\" was " + "invalid and could not be used to securely verify it. Thus, the " + "file was deleted."), + psz_destfile ); + goto end; } -} -/** - * Clears the iterator data (file, release and mirror structs) - * - * \param p_uit update iterator - * \return nothing - */ -void update_iterator_ClearData( update_iterator_t *p_uit ) -{ - p_uit->file.i_type = UPDATE_FILE_TYPE_NONE; - FREENULL( p_uit->file.psz_md5 ); - p_uit->file.l_size = 0; - FREENULL( p_uit->file.psz_description ); - FREENULL( p_uit->file.psz_url ); - FREENULL( p_uit->release.psz_version ); - FREENULL( p_uit->release.psz_svn_revision ); - p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE; - p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE; - FREENULL( p_uit->mirror.psz_name ); - FREENULL( p_uit->mirror.psz_location ); - FREENULL( p_uit->mirror.psz_type ); -} - -/** - * Perform an action on the update iterator - * Only the first matching action is performed. - * - * \param p_uit update iterator - * \param i_action update action bitmask. can be a combination of UPDATE_NEXT, UPDATE_PREV, UPDATE_MIRROR, UPDATE_RELEASE, UPDATE_FILE, UPDATE_RESET - * \return UPDATE_FAIL if action fails, UPDATE_SUCCESS|(combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE if these changed) otherwise - */ -unsigned int update_iterator_Action( update_iterator_t *p_uit, int i_action ) -{ - if( i_action & UPDATE_RESET ) + uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign ); + if( !p_hash ) { - return update_iterator_Reset( p_uit ); + msg_Err( p_udt, "Unable to hash %s", psz_destfile ); + vlc_unlink( psz_destfile ); + dialog_FatalWait( p_udt, _("File not verifiable"), + _("It was not possible to securely verify the downloaded file" + " \"%s\". Thus, it was deleted."), + psz_destfile ); + + goto end; } - else - if( i_action & UPDATE_MIRROR ) + + if( p_hash[0] != sign.hash_verification[0] || + p_hash[1] != sign.hash_verification[1] ) { - if( i_action & UPDATE_PREV ) - { - return update_iterator_PrevMirror( p_uit ); - } - else - { - return update_iterator_NextMirror( p_uit ); - } + vlc_unlink( psz_destfile ); + dialog_FatalWait( p_udt, _("File corrupted"), + _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."), + psz_destfile ); + msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile ); + free( p_hash ); + goto end; } - /*else if( i_action & UPDATE_RELEASE ) - { - if( i_action & UPDATE_PREV ) - { - return update_iterator_PrevRelease( p_uit ); - } - else - { - return update_iterator_NextRelease( p_uit ); - } - }*/ - else if( i_action & UPDATE_FILE ) + + if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash ) + != VLC_SUCCESS ) { - if( i_action & UPDATE_PREV ) - { - return update_iterator_PrevFile( p_uit ); - } - else - { - return update_iterator_NextFile( p_uit ); - } + vlc_unlink( psz_destfile ); + dialog_FatalWait( p_udt, _("File corrupted"), + _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."), + psz_destfile ); + msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile ); + free( p_hash ); + goto end; } - else + + msg_Info( p_udt, "%s authenticated", psz_destfile ); + free( p_hash ); + + int answer = dialog_Question( p_udt, _("Update VLC media player"), + _("The new version was successfully downloaded. Do you want to close VLC and install it now?"), + _("Install"), _("Cancel"), NULL); + + if(answer == 1) { - return UPDATE_SUCCESS; + answer = ShellExecuteA( NULL, "open", psz_destfile, NULL, NULL, SW_SHOW); + if(answer > 32) + libvlc_Quit(p_this->p_libvlc); } -} +end: + if( p_progress ) + dialog_ProgressDestroy( p_progress ); + if( p_stream ) + stream_Delete( p_stream ); + if( p_file ) + fclose( p_file ); + free( psz_destdir ); + free( psz_destfile ); + free( p_buffer ); + free( psz_size ); -/** - * Object to launch download thread in a different object - */ -typedef struct { - VLC_COMMON_MEMBERS - char *psz_dest; //< Download destination - char *psz_src; //< Download source - char *psz_status; //< Download status displayed in progress dialog -} download_thread_t; - -void update_download_for_real( download_thread_t *p_this ); + vlc_restorecancel( canc ); + return NULL; +} -/** - * Download the file selected by the update iterator. This function will - * launch the download in a new thread (downloads can be long) - * - * \param p_uit update iterator - * \param psz_dest destination file path - * \return nothing - */ -void update_download( update_iterator_t *p_uit, const char *psz_dest ) +update_release_t *update_GetRelease( update_t *p_update ) { - download_thread_t *p_dt = - vlc_object_create( p_uit->p_u->p_libvlc, sizeof( download_thread_t ) ); - - p_dt->psz_dest = strdup( psz_dest ); - p_dt->psz_src = strdup( p_uit->file.psz_url ); - asprintf( &p_dt->psz_status, "%s - %s (%s)\nSource: %s\nDestination: %s", - p_uit->file.psz_description, p_uit->release.psz_version, - p_uit->release.psz_svn_revision, p_uit->file.psz_url, - psz_dest); - - vlc_thread_create( p_dt, "download thread", update_download_for_real, - VLC_THREAD_PRIORITY_LOW, VLC_FALSE ); + return &p_update->release; } -/** - * Convert a long int size in bytes to a string - * - * \param l_size the size in bytes - * \return the size as a string - */ -static char *size_str( long int l_size ) +#else +#undef update_New +update_t *update_New( vlc_object_t *p_this ) { - char *psz_tmp; - if( l_size>> 30 ) - asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) ); - if( l_size >> 20 ) - asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) ); - else if( l_size >> 10 ) - asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) ); - else - asprintf( &psz_tmp, "%ld B", l_size ); - return psz_tmp; + (void)p_this; + return NULL; } -/** - * The true download function. - * - * \param p_this the download_thread_t object - * \return nothing - */ -void update_download_for_real( download_thread_t *p_this ) +void update_Delete( update_t *p_update ) { - char *psz_dest = p_this->psz_dest; - char *psz_src = p_this->psz_src; - stream_t *p_stream; - libvlc_int_t *p_libvlc = p_this->p_libvlc; - - FILE *p_file = NULL; - void *p_buffer; - - char *psz_status; - - int i_progress; - long int l_size, l_done = 0; - - vlc_thread_ready( p_this ); - - asprintf( &psz_status, "%s\nDownloading... 0.0/? %.1f%% done", - p_this->psz_status, 0.0 ); - i_progress = intf_UserProgress( p_libvlc, "Downloading...", - psz_status, 0.0, 0 ); + (void)p_update; +} - p_stream = stream_UrlNew( p_libvlc, psz_src ); - if( !p_stream ) - { - msg_Err( p_libvlc, "Failed to open %s for reading", psz_src ); - intf_UserFatal( p_libvlc, VLC_TRUE, "Download Error", - "VLC failed to open %s for reading.", psz_src ); - intf_UserHide( p_libvlc, i_progress ); - } - else - { - p_file = utf8_fopen( psz_dest, "w" ); - if( !p_file ) - { - msg_Err( p_libvlc, "Failed to open %s for writing", psz_dest ); - intf_UserFatal( p_libvlc, VLC_TRUE, "Download Error", - "VLC failed to open %s for writing.", psz_dest ); - intf_UserHide( p_libvlc, i_progress ); - } - else - { - long int l_read; - char *psz_s1; char *psz_s2; - - l_size = stream_Size(p_stream); - p_buffer = (void *)malloc( 1<<10 ); - if( p_buffer ) - { - while( ( l_read = stream_Read( p_stream, p_buffer, 1<<10 ) ) ) - { - float f_progress; - - fwrite( p_buffer, l_read, 1, p_file ); - - l_done += l_read; - free( psz_status ); - f_progress = 100.0*(float)l_done/(float)l_size; - psz_s1 = size_str( l_done ); - psz_s2 = size_str( l_size ); - asprintf( &psz_status, "%s\nDownloading... %s/%s (%.1f%%) done", - p_this->psz_status, psz_s1, psz_s2, f_progress ); - free( psz_s1 ); - free( psz_s2 ); - - intf_ProgressUpdate( p_libvlc, i_progress, - psz_status, f_progress, 0 ); - } - - free( p_buffer ); - } - fclose( p_file ); - stream_Delete( p_stream ); +void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), + void *p_data ) +{ + (void)p_update; (void)pf_callback; (void)p_data; +} - free( psz_status ); - psz_s2 = size_str( l_size ); - asprintf( &psz_status, "%s\nDone %s (100.00%%)", - p_this->psz_status, psz_s2 ); - free( psz_s2 ); - intf_ProgressUpdate( p_libvlc, i_progress, psz_status, 100.0, 0 ); - free( psz_status ); - } - } +bool update_NeedUpgrade( update_t *p_update ) +{ + (void)p_update; + return false; +} - free( p_this->psz_dest ); - free( p_this->psz_src ); - free( p_this->psz_status ); +void update_Download( update_t *p_update, const char *psz_destdir ) +{ + (void)p_update; (void)psz_destdir; +} - vlc_object_destroy( p_this ); +update_release_t *update_GetRelease( update_t *p_update ) +{ + (void)p_update; + return NULL; } +#endif