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 );
196 psz_update_data = malloc( i_read + 1 ); /* terminating '\0' */
197 if( !psz_update_data )
200 if( stream_Read( p_stream, psz_update_data, i_read ) != i_read )
202 msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
203 UPDATE_VLC_STATUS_URL );
206 psz_update_data[i_read] = '\0';
208 stream_Delete( p_stream );
211 /* first line : version number */
212 char *psz_update_data_parser = psz_update_data;
213 size_t i_len = strcspn( psz_update_data, "\r\n" );
214 psz_update_data_parser += i_len;
215 while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
216 psz_update_data_parser++;
218 if( !(psz_version_line = malloc( i_len + 1)) )
220 strncpy( psz_version_line, psz_update_data, i_len );
221 psz_version_line[i_len] = '\0';
223 p_update->release.i_extra = 0;
224 int ret = sscanf( psz_version_line, "%i.%i.%i.%i",
225 &p_update->release.i_major, &p_update->release.i_minor,
226 &p_update->release.i_revision, &p_update->release.i_extra);
227 if( ret != 3 && ret != 4 )
229 msg_Err( p_update->p_libvlc, "Update version false formated" );
233 /* second line : URL */
234 i_len = strcspn( psz_update_data_parser, "\r\n" );
237 msg_Err( p_update->p_libvlc, "Update file %s is corrupted: URL missing",
238 UPDATE_VLC_STATUS_URL );
243 if( !(p_update->release.psz_url = malloc( i_len + 1)) )
245 strncpy( p_update->release.psz_url, psz_update_data_parser, i_len );
246 p_update->release.psz_url[i_len] = '\0';
248 psz_update_data_parser += i_len;
249 while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
250 psz_update_data_parser++;
252 /* Remaining data : description */
253 i_len = strlen( psz_update_data_parser );
256 msg_Err( p_update->p_libvlc,
257 "Update file %s is corrupted: description missing",
258 UPDATE_VLC_STATUS_URL );
262 if( !(p_update->release.psz_desc = malloc( i_len + 1)) )
264 strncpy( p_update->release.psz_desc, psz_update_data_parser, i_len );
265 p_update->release.psz_desc[i_len] = '\0';
267 /* Now that we know the status is valid, we must download its signature
268 * to authenticate it */
269 signature_packet_t sign;
270 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
271 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
273 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
277 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
279 msg_Err( p_update->p_libvlc, "Invalid signature type" );
283 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
284 if( !p_update->p_pkey )
287 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
288 p_update->p_pkey, NULL ) != VLC_SUCCESS )
290 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
291 FREENULL( p_update->p_pkey );
295 memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
297 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
299 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
300 public_key_t *p_new_pkey = download_key(
301 VLC_OBJECT(p_update->p_libvlc),
302 sign.issuer_longid, videolan_public_key_longid );
305 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
306 FREENULL( p_update->p_pkey );
310 uint8_t *p_hash = hash_from_public_key( p_new_pkey );
313 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
315 FREENULL( p_update->p_pkey );
319 if( verify_signature( &p_new_pkey->sig,
320 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
323 msg_Info( p_update->p_libvlc, "Key authenticated" );
324 free( p_update->p_pkey );
325 p_update->p_pkey = p_new_pkey;
330 msg_Err( p_update->p_libvlc, "Key signature invalid !" );
335 uint8_t *p_hash = hash_from_text( psz_update_data, &sign );
338 msg_Warn( p_update->p_libvlc, "Can't compute hash for status file" );
342 else if( p_hash[0] != sign.hash_verification[0] ||
343 p_hash[1] != sign.hash_verification[1] )
345 msg_Warn( p_update->p_libvlc, "Bad hash for status file" );
350 else if( verify_signature( &sign, &p_update->p_pkey->key, p_hash )
353 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
360 msg_Info( p_update->p_libvlc, "Status file authenticated" );
362 free( psz_version_line );
363 free( psz_update_data );
369 stream_Delete( p_stream );
370 free( psz_version_line );
371 free( psz_update_data );
375 static void* update_CheckReal( void * );
380 * \param p_update pointer to update struct
381 * \param pf_callback pointer to a function to call when the update_check is finished
382 * \param p_data pointer to some datas to give to the callback
385 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
389 // If the object already exist, destroy it
390 if( p_update->p_check )
392 vlc_join( p_update->p_check->thread, NULL );
393 free( p_update->p_check );
396 update_check_thread_t *p_uct = calloc( 1, sizeof( *p_uct ) );
399 p_uct->p_update = p_update;
400 p_update->p_check = p_uct;
401 p_uct->pf_callback = pf_callback;
402 p_uct->p_data = p_data;
404 vlc_clone( &p_uct->thread, update_CheckReal, p_uct, VLC_THREAD_PRIORITY_LOW );
407 void* update_CheckReal( void *obj )
409 update_check_thread_t *p_uct = (update_check_thread_t *)obj;
413 canc = vlc_savecancel ();
414 vlc_mutex_lock( &p_uct->p_update->lock );
416 EmptyRelease( p_uct->p_update );
417 b_ret = GetUpdateFile( p_uct->p_update );
418 vlc_mutex_unlock( &p_uct->p_update->lock );
420 if( p_uct->pf_callback )
421 (p_uct->pf_callback)( p_uct->p_data, b_ret );
423 vlc_restorecancel (canc);
427 bool update_NeedUpgrade( update_t *p_update )
431 static const int current[4] = {
432 PACKAGE_VERSION_MAJOR,
433 PACKAGE_VERSION_MINOR,
434 PACKAGE_VERSION_REVISION,
435 PACKAGE_VERSION_EXTRA
437 const int latest[4] = {
438 p_update->release.i_major,
439 p_update->release.i_minor,
440 p_update->release.i_revision,
441 p_update->release.i_extra
444 for (unsigned i = 0; i < sizeof latest / sizeof *latest; i++) {
445 /* there is a new version available */
446 if (latest[i] > current[i])
449 /* current version is more recent than the latest version ?! */
450 if (latest[i] < current[i])
454 /* current version is not a release, it's a -git or -rc version */
455 if (*PACKAGE_VERSION_DEV)
458 /* current version is latest version */
463 * Convert a long int size in bytes to a string
465 * \param l_size the size in bytes
466 * \return the size as a string
468 static char *size_str( long int l_size )
470 char *psz_tmp = NULL;
473 i_retval = asprintf( &psz_tmp, _("%.1f GiB"), (float)l_size/(1<<30) );
474 else if( l_size >> 20 )
475 i_retval = asprintf( &psz_tmp, _("%.1f MiB"), (float)l_size/(1<<20) );
476 else if( l_size >> 10 )
477 i_retval = asprintf( &psz_tmp, _("%.1f KiB"), (float)l_size/(1<<10) );
479 i_retval = asprintf( &psz_tmp, _("%ld B"), l_size );
481 return i_retval == -1 ? NULL : psz_tmp;
484 static void* update_DownloadReal( void * );
487 * Download the file given in the update_t
489 * \param p_update structure
490 * \param dir to store the download file
493 void update_Download( update_t *p_update, const char *psz_destdir )
497 // If the object already exist, destroy it
498 if( p_update->p_download )
500 atomic_store( &p_update->p_download->aborted, true );
501 vlc_join( p_update->p_download->thread, NULL );
502 vlc_object_release( p_update->p_download );
505 update_download_thread_t *p_udt =
506 vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),
511 p_udt->p_update = p_update;
512 p_update->p_download = p_udt;
513 p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
515 atomic_store(&p_udt->aborted, false);
516 vlc_clone( &p_udt->thread, update_DownloadReal, p_udt, VLC_THREAD_PRIORITY_LOW );
519 static void* update_DownloadReal( void *obj )
521 update_download_thread_t *p_udt = (update_download_thread_t *)obj;
522 dialog_progress_bar_t *p_progress = NULL;
524 long int l_downloaded = 0;
527 char *psz_downloaded = NULL;
528 char *psz_size = NULL;
529 char *psz_destfile = NULL;
530 char *psz_tmpdestfile = NULL;
533 stream_t *p_stream = NULL;
534 void* p_buffer = NULL;
538 update_t *p_update = p_udt->p_update;
539 char *psz_destdir = p_udt->psz_destdir;
541 msg_Dbg( p_udt, "Opening Stream '%s'", p_update->release.psz_url );
542 canc = vlc_savecancel ();
544 /* Open the stream */
545 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
548 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
552 /* Get the stream size */
553 l_size = stream_Size( p_stream );
555 /* Get the file name and open it*/
556 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
557 if( !psz_tmpdestfile )
559 msg_Err( p_udt, "The URL %s is badly formated",
560 p_update->release.psz_url );
564 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
567 p_file = vlc_fopen( psz_destfile, "w" );
570 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
571 dialog_FatalWait( p_udt, _("Saving file failed"),
572 _("Failed to open \"%s\" for writing"),
577 /* Create a buffer and fill it with the downloaded file */
578 p_buffer = (void *)malloc( 1 << 10 );
579 if( unlikely(p_buffer == NULL) )
582 msg_Dbg( p_udt, "Downloading Stream '%s'", p_update->release.psz_url );
584 psz_size = size_str( l_size );
585 if( asprintf( &psz_status, _("%s\nDownloading... %s/%s %.1f%% done"),
586 p_update->release.psz_url, "0.0", psz_size, 0.0 ) == -1 )
589 p_progress = dialog_ProgressCreate( p_udt, _( "Downloading ..."),
590 psz_status, _("Cancel") );
593 if( p_progress == NULL )
596 while( !atomic_load( &p_udt->aborted ) &&
597 ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
598 !dialog_ProgressCancelled( p_progress ) )
600 if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
602 msg_Err( p_udt, "Failed to write into %s", psz_destfile );
606 l_downloaded += i_read;
607 psz_downloaded = size_str( l_downloaded );
608 f_progress = (float)l_downloaded/(float)l_size;
610 if( asprintf( &psz_status, _( "%s\nDownloading... %s/%s - %.1f%% done" ),
611 p_update->release.psz_url, psz_downloaded, psz_size,
612 f_progress*100 ) != -1 )
614 dialog_ProgressSet( p_progress, psz_status, f_progress );
617 free( psz_downloaded );
620 /* Finish the progress bar or delete the file if the user had canceled */
624 if( !atomic_load( &p_udt->aborted ) &&
625 !dialog_ProgressCancelled( p_progress ) )
627 dialog_ProgressDestroy( p_progress );
632 vlc_unlink( psz_destfile );
636 signature_packet_t sign;
637 if( download_signature( VLC_OBJECT( p_udt ), &sign,
638 p_update->release.psz_url ) != VLC_SUCCESS )
640 vlc_unlink( psz_destfile );
642 dialog_FatalWait( p_udt, _("File could not be verified"),
643 _("It was not possible to download a cryptographic signature for "
644 "the downloaded file \"%s\". Thus, it was deleted."),
646 msg_Err( p_udt, "Couldn't download signature of downloaded file" );
650 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
652 vlc_unlink( psz_destfile );
653 msg_Err( p_udt, "Invalid signature issuer" );
654 dialog_FatalWait( p_udt, _("Invalid signature"),
655 _("The cryptographic signature for the downloaded file \"%s\" was "
656 "invalid and could not be used to securely verify it. Thus, the "
657 "file was deleted."),
662 if( sign.type != BINARY_SIGNATURE )
664 vlc_unlink( psz_destfile );
665 msg_Err( p_udt, "Invalid signature type" );
666 dialog_FatalWait( p_udt, _("Invalid signature"),
667 _("The cryptographic signature for the downloaded file \"%s\" was "
668 "invalid and could not be used to securely verify it. Thus, the "
669 "file was deleted."),
674 uint8_t *p_hash = hash_from_file( psz_destfile, &sign );
677 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
678 vlc_unlink( psz_destfile );
679 dialog_FatalWait( p_udt, _("File not verifiable"),
680 _("It was not possible to securely verify the downloaded file"
681 " \"%s\". Thus, it was deleted."),
687 if( p_hash[0] != sign.hash_verification[0] ||
688 p_hash[1] != sign.hash_verification[1] )
690 vlc_unlink( psz_destfile );
691 dialog_FatalWait( p_udt, _("File corrupted"),
692 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
694 msg_Err( p_udt, "Bad hash for %s", psz_destfile );
699 if( verify_signature( &sign, &p_update->p_pkey->key, p_hash )
702 vlc_unlink( psz_destfile );
703 dialog_FatalWait( p_udt, _("File corrupted"),
704 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
706 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
711 msg_Info( p_udt, "%s authenticated", psz_destfile );
715 int answer = dialog_Question( p_udt, _("Update VLC media player"),
716 _("The new version was successfully downloaded. Do you want to close VLC and install it now?"),
717 _("Install"), _("Cancel"), NULL);
721 wchar_t psz_wdestfile[MAX_PATH];
722 MultiByteToWideChar( CP_UTF8, 0, psz_destfile, -1, psz_wdestfile, MAX_PATH );
723 answer = (int)ShellExecuteW( NULL, L"open", psz_wdestfile, NULL, NULL, SW_SHOW);
725 libvlc_Quit(p_udt->p_libvlc);
730 dialog_ProgressDestroy( p_progress );
732 stream_Delete( p_stream );
736 free( psz_destfile );
740 vlc_restorecancel( canc );
744 update_release_t *update_GetRelease( update_t *p_update )
746 return &p_update->release;
751 update_t *update_New( vlc_object_t *p_this )
757 void update_Delete( update_t *p_update )
762 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
765 (void)p_update; (void)pf_callback; (void)p_data;
768 bool update_NeedUpgrade( update_t *p_update )
774 void update_Download( update_t *p_update, const char *psz_destdir )
776 (void)p_update; (void)psz_destdir;
779 update_release_t *update_GetRelease( update_t *p_update )