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
77 #if defined( UNDER_CE )
78 # define UPDATE_OS_SUFFIX "-ce"
79 #elif defined( WIN64 )
80 # define UPDATE_OS_SUFFIX "-win-x64"
81 #elif defined( WIN32 )
82 # define UPDATE_OS_SUFFIX "-win-x86"
84 # define UPDATE_OS_SUFFIX ""
88 # define UPDATE_VLC_STATUS_URL "http://update-test.videolan.org/vlc/status-win-x86"
90 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status" UPDATE_OS_SUFFIX
93 /*****************************************************************************
95 *****************************************************************************/
99 * Create a new update VLC struct
101 * \param p_this the calling vlc_object
102 * \return pointer to new update_t or NULL
104 update_t *update_New( vlc_object_t *p_this )
109 p_update = (update_t *)malloc( sizeof( update_t ) );
110 if( !p_update ) return NULL;
112 vlc_mutex_init( &p_update->lock );
114 p_update->p_libvlc = p_this->p_libvlc;
116 p_update->release.psz_url = NULL;
117 p_update->release.psz_desc = NULL;
119 p_update->p_download = NULL;
120 p_update->p_check = NULL;
122 p_update->p_pkey = NULL;
129 * Delete an update_t struct
131 * \param p_update update_t* pointer
134 void update_Delete( update_t *p_update )
138 if( p_update->p_check )
140 vlc_join( p_update->p_check->thread, NULL );
141 free( p_update->p_check );
144 if( p_update->p_download )
146 vlc_atomic_set( &p_update->p_download->aborted, 1 );
147 vlc_join( p_update->p_download->thread, NULL );
148 vlc_object_release( p_update->p_download );
151 vlc_mutex_destroy( &p_update->lock );
153 free( p_update->release.psz_url );
154 free( p_update->release.psz_desc );
155 free( p_update->p_pkey );
161 * Empty the release struct
163 * \param p_update update_t* pointer
166 static void EmptyRelease( update_t *p_update )
168 p_update->release.i_major = 0;
169 p_update->release.i_minor = 0;
170 p_update->release.i_revision = 0;
172 FREENULL( p_update->release.psz_url );
173 FREENULL( p_update->release.psz_desc );
177 * Get the update file and parse it
178 * p_update has to be locked when calling this function
180 * \param p_update pointer to update struct
181 * \return true if the update is valid and authenticated
183 static bool GetUpdateFile( update_t *p_update )
185 stream_t *p_stream = NULL;
186 char *psz_version_line = NULL;
187 char *psz_update_data = NULL;
189 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
192 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
193 UPDATE_VLC_STATUS_URL );
197 const int64_t i_read = stream_Size( p_stream );
198 psz_update_data = malloc( i_read + 1 ); /* terminating '\0' */
199 if( !psz_update_data )
202 if( stream_Read( p_stream, psz_update_data, i_read ) != i_read )
204 msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
205 UPDATE_VLC_STATUS_URL );
208 psz_update_data[i_read] = '\0';
210 stream_Delete( p_stream );
213 /* first line : version number */
214 char *psz_update_data_parser = psz_update_data;
215 size_t i_len = strcspn( psz_update_data, "\r\n" );
216 psz_update_data_parser += i_len;
217 while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
218 psz_update_data_parser++;
220 if( !(psz_version_line = malloc( i_len + 1)) )
222 strncpy( psz_version_line, psz_update_data, i_len );
223 psz_version_line[i_len] = '\0';
225 p_update->release.i_extra = 0;
226 int ret = sscanf( psz_version_line, "%i.%i.%i.%i",
227 &p_update->release.i_major, &p_update->release.i_minor,
228 &p_update->release.i_revision, &p_update->release.i_extra);
229 if( ret != 3 && ret != 4 )
231 msg_Err( p_update->p_libvlc, "Update version false formated" );
235 /* second line : URL */
236 i_len = strcspn( psz_update_data_parser, "\r\n" );
239 msg_Err( p_update->p_libvlc, "Update file %s is corrupted: URL missing",
240 UPDATE_VLC_STATUS_URL );
245 if( !(p_update->release.psz_url = malloc( i_len + 1)) )
247 strncpy( p_update->release.psz_url, psz_update_data_parser, i_len );
248 p_update->release.psz_url[i_len] = '\0';
250 psz_update_data_parser += i_len;
251 while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
252 psz_update_data_parser++;
254 /* Remaining data : description */
255 i_len = strlen( psz_update_data_parser );
258 msg_Err( p_update->p_libvlc,
259 "Update file %s is corrupted: description missing",
260 UPDATE_VLC_STATUS_URL );
264 if( !(p_update->release.psz_desc = malloc( i_len + 1)) )
266 strncpy( p_update->release.psz_desc, psz_update_data_parser, i_len );
267 p_update->release.psz_desc[i_len] = '\0';
269 /* Now that we know the status is valid, we must download its signature
270 * to authenticate it */
271 signature_packet_t sign;
272 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
273 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
275 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
279 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
281 msg_Err( p_update->p_libvlc, "Invalid signature type" );
285 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
286 if( !p_update->p_pkey )
289 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
290 p_update->p_pkey, NULL ) != VLC_SUCCESS )
292 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
293 FREENULL( p_update->p_pkey );
297 memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
299 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
301 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
302 public_key_t *p_new_pkey = download_key(
303 VLC_OBJECT(p_update->p_libvlc),
304 sign.issuer_longid, videolan_public_key_longid );
307 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
308 FREENULL( p_update->p_pkey );
312 uint8_t *p_hash = hash_sha1_from_public_key( p_new_pkey );
315 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
317 FREENULL( p_update->p_pkey );
321 if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
322 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
325 msg_Info( p_update->p_libvlc, "Key authenticated" );
326 free( p_update->p_pkey );
327 p_update->p_pkey = p_new_pkey;
332 msg_Err( p_update->p_libvlc, "Key signature invalid !" );
337 uint8_t *p_hash = hash_sha1_from_text( psz_update_data, &sign );
340 msg_Warn( p_update->p_libvlc, "Can't compute SHA1 hash for status file" );
344 else if( p_hash[0] != sign.hash_verification[0] ||
345 p_hash[1] != sign.hash_verification[1] )
347 msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
351 else if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
354 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
360 msg_Info( p_update->p_libvlc, "Status file authenticated" );
366 stream_Delete( p_stream );
367 free( psz_version_line );
368 free( psz_update_data );
372 static void* update_CheckReal( void * );
377 * \param p_update pointer to update struct
378 * \param pf_callback pointer to a function to call when the update_check is finished
379 * \param p_data pointer to some datas to give to the callback
382 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
386 // If the object already exist, destroy it
387 if( p_update->p_check )
389 vlc_join( p_update->p_check->thread, NULL );
390 free( p_update->p_check );
393 update_check_thread_t *p_uct = calloc( 1, sizeof( *p_uct ) );
396 p_uct->p_update = p_update;
397 p_update->p_check = p_uct;
398 p_uct->pf_callback = pf_callback;
399 p_uct->p_data = p_data;
401 vlc_clone( &p_uct->thread, update_CheckReal, p_uct, VLC_THREAD_PRIORITY_LOW );
404 void* update_CheckReal( void *obj )
406 update_check_thread_t *p_uct = (update_check_thread_t *)obj;
410 canc = vlc_savecancel ();
411 vlc_mutex_lock( &p_uct->p_update->lock );
413 EmptyRelease( p_uct->p_update );
414 b_ret = GetUpdateFile( p_uct->p_update );
415 vlc_mutex_unlock( &p_uct->p_update->lock );
417 if( p_uct->pf_callback )
418 (p_uct->pf_callback)( p_uct->p_data, b_ret );
420 vlc_restorecancel (canc);
424 bool update_NeedUpgrade( update_t *p_update )
428 static const int current[4] = {
429 PACKAGE_VERSION_MAJOR,
430 PACKAGE_VERSION_MINOR,
431 PACKAGE_VERSION_REVISION,
432 PACKAGE_VERSION_EXTRA
434 const int latest[4] = {
435 p_update->release.i_major,
436 p_update->release.i_minor,
437 p_update->release.i_revision,
438 p_update->release.i_extra
441 for (unsigned i = 0; i < sizeof latest / sizeof *latest; i++) {
442 /* there is a new version available */
443 if (latest[i] > current[i])
446 /* current version is more recent than the latest version ?! */
447 if (latest[i] < current[i])
451 /* current version is not a release, it's a -git or -rc version */
452 if (*PACKAGE_VERSION_DEV)
455 /* current version is latest version */
460 * Convert a long int size in bytes to a string
462 * \param l_size the size in bytes
463 * \return the size as a string
465 static char *size_str( long int l_size )
467 char *psz_tmp = NULL;
470 i_retval = asprintf( &psz_tmp, _("%.1f GiB"), (float)l_size/(1<<30) );
471 else if( l_size >> 20 )
472 i_retval = asprintf( &psz_tmp, _("%.1f MiB"), (float)l_size/(1<<20) );
473 else if( l_size >> 10 )
474 i_retval = asprintf( &psz_tmp, _("%.1f KiB"), (float)l_size/(1<<10) );
476 i_retval = asprintf( &psz_tmp, _("%ld B"), l_size );
478 return i_retval == -1 ? NULL : psz_tmp;
481 static void* update_DownloadReal( void * );
484 * Download the file given in the update_t
486 * \param p_update structure
487 * \param dir to store the download file
490 void update_Download( update_t *p_update, const char *psz_destdir )
494 // If the object already exist, destroy it
495 if( p_update->p_download )
497 vlc_atomic_set( &p_udt->p_download->aborted, 1 );
498 vlc_join( p_update->p_download->thread, NULL );
499 vlc_object_release( p_update->p_download );
502 update_download_thread_t *p_udt =
503 vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),
508 p_udt->p_update = p_update;
509 p_update->p_download = p_udt;
510 p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
512 vlc_atomic_set(&p_udt->aborted, 0);
513 vlc_clone( &p_udt->thread, update_DownloadReal, p_udt, VLC_THREAD_PRIORITY_LOW );
516 static void* update_DownloadReal( void *obj )
518 update_download_thread_t *p_udt = (update_download_thread_t *)obj;
519 dialog_progress_bar_t *p_progress = NULL;
521 long int l_downloaded = 0;
524 char *psz_downloaded = NULL;
525 char *psz_size = NULL;
526 char *psz_destfile = NULL;
527 char *psz_tmpdestfile = NULL;
530 stream_t *p_stream = NULL;
531 void* p_buffer = NULL;
535 update_t *p_update = p_udt->p_update;
536 char *psz_destdir = p_udt->psz_destdir;
538 msg_Dbg( p_udt, "Opening Stream '%s'", p_update->release.psz_url );
539 canc = vlc_savecancel ();
541 /* Open the stream */
542 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
545 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
549 /* Get the stream size */
550 l_size = stream_Size( p_stream );
552 /* Get the file name and open it*/
553 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
554 if( !psz_tmpdestfile )
556 msg_Err( p_udt, "The URL %s is badly formated",
557 p_update->release.psz_url );
561 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
564 p_file = vlc_fopen( psz_destfile, "w" );
567 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
568 dialog_FatalWait( p_udt, _("Saving file failed"),
569 _("Failed to open \"%s\" for writing"),
574 /* Create a buffer and fill it with the downloaded file */
575 p_buffer = (void *)malloc( 1 << 10 );
576 if( unlikely(p_buffer == NULL) )
579 msg_Dbg( p_udt, "Downloading Stream '%s'", p_update->release.psz_url );
581 psz_size = size_str( l_size );
582 if( asprintf( &psz_status, _("%s\nDownloading... %s/%s %.1f%% done"),
583 p_update->release.psz_url, "0.0", psz_size, 0.0 ) == -1 )
586 p_progress = dialog_ProgressCreate( p_udt, _( "Downloading ..."),
587 psz_status, _("Cancel") );
590 if( p_progress == NULL )
593 while( !vlc_atomic_get( &p_udt->aborted ) &&
594 ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
595 !dialog_ProgressCancelled( p_progress ) )
597 if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
599 msg_Err( p_udt, "Failed to write into %s", psz_destfile );
603 l_downloaded += i_read;
604 psz_downloaded = size_str( l_downloaded );
605 f_progress = (float)l_downloaded/(float)l_size;
607 if( asprintf( &psz_status, _( "%s\nDownloading... %s/%s - %.1f%% done" ),
608 p_update->release.psz_url, psz_downloaded, psz_size,
609 f_progress*100 ) != -1 )
611 dialog_ProgressSet( p_progress, psz_status, f_progress );
614 free( psz_downloaded );
617 /* Finish the progress bar or delete the file if the user had canceled */
621 if( !vlc_atomic_get( &p_udt->aborted ) &&
622 !dialog_ProgressCancelled( p_progress ) )
624 dialog_ProgressDestroy( p_progress );
629 vlc_unlink( psz_destfile );
633 signature_packet_t sign;
634 if( download_signature( VLC_OBJECT( p_udt ), &sign,
635 p_update->release.psz_url ) != VLC_SUCCESS )
637 vlc_unlink( psz_destfile );
639 dialog_FatalWait( p_udt, _("File could not be verified"),
640 _("It was not possible to download a cryptographic signature for "
641 "the downloaded file \"%s\". Thus, it was deleted."),
643 msg_Err( p_udt, "Couldn't download signature of downloaded file" );
647 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
649 vlc_unlink( psz_destfile );
650 msg_Err( p_udt, "Invalid signature issuer" );
651 dialog_FatalWait( p_udt, _("Invalid signature"),
652 _("The cryptographic signature for the downloaded file \"%s\" was "
653 "invalid and could not be used to securely verify it. Thus, the "
654 "file was deleted."),
659 if( sign.type != BINARY_SIGNATURE )
661 vlc_unlink( psz_destfile );
662 msg_Err( p_udt, "Invalid signature type" );
663 dialog_FatalWait( p_udt, _("Invalid signature"),
664 _("The cryptographic signature for the downloaded file \"%s\" was "
665 "invalid and could not be used to securely verify it. Thus, the "
666 "file was deleted."),
671 uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
674 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
675 vlc_unlink( psz_destfile );
676 dialog_FatalWait( p_udt, _("File not verifiable"),
677 _("It was not possible to securely verify the downloaded file"
678 " \"%s\". Thus, it was deleted."),
684 if( p_hash[0] != sign.hash_verification[0] ||
685 p_hash[1] != sign.hash_verification[1] )
687 vlc_unlink( psz_destfile );
688 dialog_FatalWait( p_udt, _("File corrupted"),
689 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
691 msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
696 if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
699 vlc_unlink( psz_destfile );
700 dialog_FatalWait( p_udt, _("File corrupted"),
701 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
703 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
708 msg_Info( p_udt, "%s authenticated", psz_destfile );
712 int answer = dialog_Question( p_udt, _("Update VLC media player"),
713 _("The new version was successfully downloaded. Do you want to close VLC and install it now?"),
714 _("Install"), _("Cancel"), NULL);
718 wchar_t psz_wdestfile[MAX_PATH];
719 MultiByteToWideChar( CP_UTF8, 0, psz_destfile, -1, psz_wdestfile, MAX_PATH );
720 answer = (int)ShellExecuteW( NULL, L"open", psz_wdestfile, NULL, NULL, SW_SHOW);
722 libvlc_Quit(p_udt->p_libvlc);
727 dialog_ProgressDestroy( p_progress );
729 stream_Delete( p_stream );
733 free( psz_destfile );
737 vlc_restorecancel( canc );
741 update_release_t *update_GetRelease( update_t *p_update )
743 return &p_update->release;
748 update_t *update_New( vlc_object_t *p_this )
754 void update_Delete( update_t *p_update )
759 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
762 (void)p_update; (void)pf_callback; (void)p_data;
765 bool update_NeedUpgrade( update_t *p_update )
771 void update_Download( update_t *p_update, const char *psz_destdir )
773 (void)p_update; (void)psz_destdir;
776 update_release_t *update_GetRelease( update_t *p_update )