1 /*****************************************************************************
2 * update.c: VLC update checking and downloading
3 *****************************************************************************
4 * Copyright © 2005-2008 VLC authors and VideoLAN
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8 * Rémi Duraffort <ivoire at via.ecp.fr>
9 Rafaël Carré <funman@videolanorg>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either release 2 of the License, or
14 * (at your option) any later release.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
28 * This file contains functions related to VLC update management
31 /*****************************************************************************
33 *****************************************************************************/
39 #include <vlc_common.h>
40 #include <vlc_update.h>
46 #include <vlc_pgpkey.h>
47 #include <vlc_stream.h>
48 #include <vlc_strings.h>
50 #include <vlc_dialog.h>
51 #include <vlc_interface.h>
54 #include <vlc_gcrypt.h>
59 #include "../libvlc.h"
61 /*****************************************************************************
63 *****************************************************************************/
66 * Here is the format of these "status files" :
67 * First line is the last version: "X.Y.Z.E" where:
68 * * X is the major number
69 * * Y is the minor number
70 * * Z is the revision number
71 * * .E is an OPTIONAL extra number
72 * * IE "1.2.0" or "1.1.10.1"
73 * Second line is a url of the binary for this last version
74 * Remaining text is a required description of the update
78 # define UPDATE_OS_SUFFIX "-win-x64"
79 #elif defined( _WIN32 )
80 # define UPDATE_OS_SUFFIX "-win-x86"
82 # define UPDATE_OS_SUFFIX ""
86 # define UPDATE_VLC_STATUS_URL "http://update-test.videolan.org/vlc/status-win-x86"
88 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status" UPDATE_OS_SUFFIX
91 /*****************************************************************************
93 *****************************************************************************/
97 * Create a new update VLC struct
99 * \param p_this the calling vlc_object
100 * \return pointer to new update_t or NULL
102 update_t *update_New( vlc_object_t *p_this )
107 p_update = (update_t *)malloc( sizeof( update_t ) );
108 if( !p_update ) return NULL;
110 vlc_mutex_init( &p_update->lock );
112 p_update->p_libvlc = p_this->p_libvlc;
114 p_update->release.psz_url = NULL;
115 p_update->release.psz_desc = NULL;
117 p_update->p_download = NULL;
118 p_update->p_check = NULL;
120 p_update->p_pkey = NULL;
127 * Delete an update_t struct
129 * \param p_update update_t* pointer
132 void update_Delete( update_t *p_update )
136 if( p_update->p_check )
138 vlc_join( p_update->p_check->thread, NULL );
139 free( p_update->p_check );
142 if( p_update->p_download )
144 atomic_store( &p_update->p_download->aborted, true );
145 vlc_join( p_update->p_download->thread, NULL );
146 vlc_object_release( p_update->p_download );
149 vlc_mutex_destroy( &p_update->lock );
151 free( p_update->release.psz_url );
152 free( p_update->release.psz_desc );
153 free( p_update->p_pkey );
159 * Empty the release struct
161 * \param p_update update_t* pointer
164 static void EmptyRelease( update_t *p_update )
166 p_update->release.i_major = 0;
167 p_update->release.i_minor = 0;
168 p_update->release.i_revision = 0;
170 FREENULL( p_update->release.psz_url );
171 FREENULL( p_update->release.psz_desc );
175 * Get the update file and parse it
176 * p_update has to be locked when calling this function
178 * \param p_update pointer to update struct
179 * \return true if the update is valid and authenticated
181 static bool GetUpdateFile( update_t *p_update )
183 stream_t *p_stream = NULL;
184 char *psz_version_line = NULL;
185 char *psz_update_data = NULL;
187 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
190 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
191 UPDATE_VLC_STATUS_URL );
195 const int64_t i_read = stream_Size( p_stream );
197 if( i_read < 0 || i_read >= UINT16_MAX)
199 msg_Err(p_update->p_libvlc, "Status file too large");
203 psz_update_data = malloc( i_read + 1 ); /* terminating '\0' */
204 if( !psz_update_data )
207 if( stream_Read( p_stream, psz_update_data, i_read ) != i_read )
209 msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
210 UPDATE_VLC_STATUS_URL );
213 psz_update_data[i_read] = '\0';
215 stream_Delete( p_stream );
218 /* first line : version number */
219 char *psz_update_data_parser = psz_update_data;
220 size_t i_len = strcspn( psz_update_data, "\r\n" );
221 psz_update_data_parser += i_len;
222 while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
223 psz_update_data_parser++;
225 if( !(psz_version_line = malloc( i_len + 1)) )
227 strncpy( psz_version_line, psz_update_data, i_len );
228 psz_version_line[i_len] = '\0';
230 p_update->release.i_extra = 0;
231 int ret = sscanf( psz_version_line, "%i.%i.%i.%i",
232 &p_update->release.i_major, &p_update->release.i_minor,
233 &p_update->release.i_revision, &p_update->release.i_extra);
234 if( ret != 3 && ret != 4 )
236 msg_Err( p_update->p_libvlc, "Update version false formated" );
240 /* second line : URL */
241 i_len = strcspn( psz_update_data_parser, "\r\n" );
244 msg_Err( p_update->p_libvlc, "Update file %s is corrupted: URL missing",
245 UPDATE_VLC_STATUS_URL );
250 if( !(p_update->release.psz_url = malloc( i_len + 1)) )
252 strncpy( p_update->release.psz_url, psz_update_data_parser, i_len );
253 p_update->release.psz_url[i_len] = '\0';
255 psz_update_data_parser += i_len;
256 while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
257 psz_update_data_parser++;
259 /* Remaining data : description */
260 i_len = strlen( psz_update_data_parser );
263 msg_Err( p_update->p_libvlc,
264 "Update file %s is corrupted: description missing",
265 UPDATE_VLC_STATUS_URL );
269 if( !(p_update->release.psz_desc = malloc( i_len + 1)) )
271 strncpy( p_update->release.psz_desc, psz_update_data_parser, i_len );
272 p_update->release.psz_desc[i_len] = '\0';
274 /* Now that we know the status is valid, we must download its signature
275 * to authenticate it */
276 signature_packet_t sign;
277 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
278 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
280 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
284 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
286 msg_Err( p_update->p_libvlc, "Invalid signature type" );
290 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
291 if( !p_update->p_pkey )
294 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
295 p_update->p_pkey, NULL ) != VLC_SUCCESS )
297 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
298 FREENULL( p_update->p_pkey );
302 memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
304 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
306 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
307 public_key_t *p_new_pkey = download_key(
308 VLC_OBJECT(p_update->p_libvlc),
309 sign.issuer_longid, videolan_public_key_longid );
312 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
313 FREENULL( p_update->p_pkey );
317 uint8_t *p_hash = hash_from_public_key( p_new_pkey );
320 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
322 FREENULL( p_update->p_pkey );
326 if( verify_signature( &p_new_pkey->sig,
327 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
330 msg_Info( p_update->p_libvlc, "Key authenticated" );
331 free( p_update->p_pkey );
332 p_update->p_pkey = p_new_pkey;
337 msg_Err( p_update->p_libvlc, "Key signature invalid !" );
342 uint8_t *p_hash = hash_from_text( psz_update_data, &sign );
345 msg_Warn( p_update->p_libvlc, "Can't compute hash for status file" );
349 else if( p_hash[0] != sign.hash_verification[0] ||
350 p_hash[1] != sign.hash_verification[1] )
352 msg_Warn( p_update->p_libvlc, "Bad hash for status file" );
357 else if( verify_signature( &sign, &p_update->p_pkey->key, p_hash )
360 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
367 msg_Info( p_update->p_libvlc, "Status file authenticated" );
369 free( psz_version_line );
370 free( psz_update_data );
376 stream_Delete( p_stream );
377 free( psz_version_line );
378 free( psz_update_data );
382 static void* update_CheckReal( void * );
387 * \param p_update pointer to update struct
388 * \param pf_callback pointer to a function to call when the update_check is finished
389 * \param p_data pointer to some datas to give to the callback
392 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
396 // If the object already exist, destroy it
397 if( p_update->p_check )
399 vlc_join( p_update->p_check->thread, NULL );
400 free( p_update->p_check );
403 update_check_thread_t *p_uct = calloc( 1, sizeof( *p_uct ) );
406 p_uct->p_update = p_update;
407 p_update->p_check = p_uct;
408 p_uct->pf_callback = pf_callback;
409 p_uct->p_data = p_data;
411 vlc_clone( &p_uct->thread, update_CheckReal, p_uct, VLC_THREAD_PRIORITY_LOW );
414 void* update_CheckReal( void *obj )
416 update_check_thread_t *p_uct = (update_check_thread_t *)obj;
420 canc = vlc_savecancel ();
421 vlc_mutex_lock( &p_uct->p_update->lock );
423 EmptyRelease( p_uct->p_update );
424 b_ret = GetUpdateFile( p_uct->p_update );
425 vlc_mutex_unlock( &p_uct->p_update->lock );
427 if( p_uct->pf_callback )
428 (p_uct->pf_callback)( p_uct->p_data, b_ret );
430 vlc_restorecancel (canc);
434 bool update_NeedUpgrade( update_t *p_update )
438 static const int current[4] = {
439 PACKAGE_VERSION_MAJOR,
440 PACKAGE_VERSION_MINOR,
441 PACKAGE_VERSION_REVISION,
442 PACKAGE_VERSION_EXTRA
444 const int latest[4] = {
445 p_update->release.i_major,
446 p_update->release.i_minor,
447 p_update->release.i_revision,
448 p_update->release.i_extra
451 for (unsigned i = 0; i < sizeof latest / sizeof *latest; i++) {
452 /* there is a new version available */
453 if (latest[i] > current[i])
456 /* current version is more recent than the latest version ?! */
457 if (latest[i] < current[i])
461 /* current version is not a release, it's a -git or -rc version */
462 if (*PACKAGE_VERSION_DEV)
465 /* current version is latest version */
470 * Convert a long int size in bytes to a string
472 * \param l_size the size in bytes
473 * \return the size as a string
475 static char *size_str( long int l_size )
477 char *psz_tmp = NULL;
480 i_retval = asprintf( &psz_tmp, _("%.1f GiB"), (float)l_size/(1<<30) );
481 else if( l_size >> 20 )
482 i_retval = asprintf( &psz_tmp, _("%.1f MiB"), (float)l_size/(1<<20) );
483 else if( l_size >> 10 )
484 i_retval = asprintf( &psz_tmp, _("%.1f KiB"), (float)l_size/(1<<10) );
486 i_retval = asprintf( &psz_tmp, _("%ld B"), l_size );
488 return i_retval == -1 ? NULL : psz_tmp;
491 static void* update_DownloadReal( void * );
494 * Download the file given in the update_t
496 * \param p_update structure
497 * \param dir to store the download file
500 void update_Download( update_t *p_update, const char *psz_destdir )
504 // If the object already exist, destroy it
505 if( p_update->p_download )
507 atomic_store( &p_update->p_download->aborted, true );
508 vlc_join( p_update->p_download->thread, NULL );
509 vlc_object_release( p_update->p_download );
512 update_download_thread_t *p_udt =
513 vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),
518 p_udt->p_update = p_update;
519 p_update->p_download = p_udt;
520 p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
522 atomic_store(&p_udt->aborted, false);
523 vlc_clone( &p_udt->thread, update_DownloadReal, p_udt, VLC_THREAD_PRIORITY_LOW );
526 static void* update_DownloadReal( void *obj )
528 update_download_thread_t *p_udt = (update_download_thread_t *)obj;
529 dialog_progress_bar_t *p_progress = NULL;
531 long int l_downloaded = 0;
534 char *psz_downloaded = NULL;
535 char *psz_size = NULL;
536 char *psz_destfile = NULL;
537 char *psz_tmpdestfile = NULL;
540 stream_t *p_stream = NULL;
541 void* p_buffer = NULL;
545 update_t *p_update = p_udt->p_update;
546 char *psz_destdir = p_udt->psz_destdir;
548 msg_Dbg( p_udt, "Opening Stream '%s'", p_update->release.psz_url );
549 canc = vlc_savecancel ();
551 /* Open the stream */
552 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
555 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
559 /* Get the stream size */
560 l_size = stream_Size( p_stream );
562 /* Get the file name and open it*/
563 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
564 if( !psz_tmpdestfile )
566 msg_Err( p_udt, "The URL %s is badly formated",
567 p_update->release.psz_url );
571 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
574 p_file = vlc_fopen( psz_destfile, "w" );
577 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
578 dialog_FatalWait( p_udt, _("Saving file failed"),
579 _("Failed to open \"%s\" for writing"),
584 /* Create a buffer and fill it with the downloaded file */
585 p_buffer = (void *)malloc( 1 << 10 );
586 if( unlikely(p_buffer == NULL) )
589 msg_Dbg( p_udt, "Downloading Stream '%s'", p_update->release.psz_url );
591 psz_size = size_str( l_size );
592 if( asprintf( &psz_status, _("%s\nDownloading... %s/%s %.1f%% done"),
593 p_update->release.psz_url, "0.0", psz_size, 0.0 ) == -1 )
596 p_progress = dialog_ProgressCreate( p_udt, _( "Downloading ..."),
597 psz_status, _("Cancel") );
600 if( p_progress == NULL )
603 while( !atomic_load( &p_udt->aborted ) &&
604 ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
605 !dialog_ProgressCancelled( p_progress ) )
607 if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
609 msg_Err( p_udt, "Failed to write into %s", psz_destfile );
613 l_downloaded += i_read;
614 psz_downloaded = size_str( l_downloaded );
615 f_progress = (float)l_downloaded/(float)l_size;
617 if( asprintf( &psz_status, _( "%s\nDownloading... %s/%s - %.1f%% done" ),
618 p_update->release.psz_url, psz_downloaded, psz_size,
619 f_progress*100 ) != -1 )
621 dialog_ProgressSet( p_progress, psz_status, f_progress );
624 free( psz_downloaded );
627 /* Finish the progress bar or delete the file if the user had canceled */
631 if( !atomic_load( &p_udt->aborted ) &&
632 !dialog_ProgressCancelled( p_progress ) )
634 dialog_ProgressDestroy( p_progress );
639 vlc_unlink( psz_destfile );
643 signature_packet_t sign;
644 if( download_signature( VLC_OBJECT( p_udt ), &sign,
645 p_update->release.psz_url ) != VLC_SUCCESS )
647 vlc_unlink( psz_destfile );
649 dialog_FatalWait( p_udt, _("File could not be verified"),
650 _("It was not possible to download a cryptographic signature for "
651 "the downloaded file \"%s\". Thus, it was deleted."),
653 msg_Err( p_udt, "Couldn't download signature of downloaded file" );
657 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
659 vlc_unlink( psz_destfile );
660 msg_Err( p_udt, "Invalid signature issuer" );
661 dialog_FatalWait( p_udt, _("Invalid signature"),
662 _("The cryptographic signature for the downloaded file \"%s\" was "
663 "invalid and could not be used to securely verify it. Thus, the "
664 "file was deleted."),
669 if( sign.type != BINARY_SIGNATURE )
671 vlc_unlink( psz_destfile );
672 msg_Err( p_udt, "Invalid signature type" );
673 dialog_FatalWait( p_udt, _("Invalid signature"),
674 _("The cryptographic signature for the downloaded file \"%s\" was "
675 "invalid and could not be used to securely verify it. Thus, the "
676 "file was deleted."),
681 uint8_t *p_hash = hash_from_file( psz_destfile, &sign );
684 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
685 vlc_unlink( psz_destfile );
686 dialog_FatalWait( p_udt, _("File not verifiable"),
687 _("It was not possible to securely verify the downloaded file"
688 " \"%s\". Thus, it was deleted."),
694 if( p_hash[0] != sign.hash_verification[0] ||
695 p_hash[1] != sign.hash_verification[1] )
697 vlc_unlink( psz_destfile );
698 dialog_FatalWait( p_udt, _("File corrupted"),
699 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
701 msg_Err( p_udt, "Bad hash for %s", psz_destfile );
706 if( verify_signature( &sign, &p_update->p_pkey->key, p_hash )
709 vlc_unlink( psz_destfile );
710 dialog_FatalWait( p_udt, _("File corrupted"),
711 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
713 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
718 msg_Info( p_udt, "%s authenticated", psz_destfile );
722 int answer = dialog_Question( p_udt, _("Update VLC media player"),
723 _("The new version was successfully downloaded. Do you want to close VLC and install it now?"),
724 _("Install"), _("Cancel"), NULL);
728 wchar_t psz_wdestfile[MAX_PATH];
729 MultiByteToWideChar( CP_UTF8, 0, psz_destfile, -1, psz_wdestfile, MAX_PATH );
730 answer = (int)ShellExecuteW( NULL, L"open", psz_wdestfile, NULL, NULL, SW_SHOW);
732 libvlc_Quit(p_udt->p_libvlc);
737 dialog_ProgressDestroy( p_progress );
739 stream_Delete( p_stream );
743 free( psz_destfile );
747 vlc_restorecancel( canc );
751 update_release_t *update_GetRelease( update_t *p_update )
753 return &p_update->release;
758 update_t *update_New( vlc_object_t *p_this )
764 void update_Delete( update_t *p_update )
769 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
772 (void)p_update; (void)pf_callback; (void)p_data;
775 bool update_NeedUpgrade( update_t *p_update )
781 void update_Download( update_t *p_update, const char *psz_destdir )
783 (void)p_update; (void)psz_destdir;
786 update_release_t *update_GetRelease( update_t *p_update )