1 /*****************************************************************************
2 * update.c: VLC update checking and downloading
3 *****************************************************************************
4 * Copyright © 2005-2008 the VideoLAN team
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
12 * it under the terms of the GNU 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 General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, 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>
49 #include <vlc_charset.h>
50 #include <vlc_dialog.h>
53 #include <vlc_gcrypt.h>
56 #include "../libvlc.h"
58 /*****************************************************************************
60 *****************************************************************************/
63 * Here is the format of these "status files" :
64 * First line is the last version: "X.Y.Ze" where:
65 * * X is the major number
66 * * Y is the minor number
67 * * Z is the revision number
68 * * e is an OPTIONAL extra letter
69 * * AKA "0.8.6d" or "0.9.0"
70 * Second line is an url of the binary for this last version
71 * Remaining text is a required description of the update
74 #if defined( UNDER_CE )
75 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
76 #elif defined( WIN32 )
77 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
78 #elif defined( __APPLE__ )
79 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
80 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
82 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
84 #elif defined( SYS_BEOS )
85 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
87 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
91 /*****************************************************************************
93 *****************************************************************************/
96 * Create a new update VLC struct
98 * \param p_this the calling vlc_object
99 * \return pointer to new update_t or NULL
101 update_t *__update_New( vlc_object_t *p_this )
106 p_update = (update_t *)malloc( sizeof( update_t ) );
107 if( !p_update ) return NULL;
109 vlc_mutex_init( &p_update->lock );
111 p_update->p_libvlc = p_this->p_libvlc;
113 p_update->release.psz_url = NULL;
114 p_update->release.psz_desc = NULL;
116 p_update->p_download = NULL;
117 p_update->p_check = NULL;
119 p_update->p_pkey = NULL;
126 * Delete an update_t struct
128 * \param p_update update_t* pointer
131 void update_Delete( update_t *p_update )
135 if( p_update->p_check )
137 vlc_object_kill( p_update->p_check );
138 vlc_thread_join( p_update->p_check );
139 vlc_object_release( p_update->p_check );
142 if( p_update->p_download )
144 vlc_object_kill( p_update->p_download );
145 vlc_thread_join( p_update->p_download );
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;
188 char *psz_version_line = NULL;
190 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
193 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
194 UPDATE_VLC_STATUS_URL );
198 /* Start reading the status file */
199 if( !( psz_version_line = stream_ReadLine( p_stream ) ) )
201 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
202 UPDATE_VLC_STATUS_URL );
206 /* first line : version number */
207 p_update->release.extra = 0;
208 switch( sscanf( psz_version_line, "%i.%i.%i%c",
209 &i_major, &i_minor, &i_revision, &extra ) )
212 p_update->release.extra = extra;
214 p_update->release.i_major = i_major;
215 p_update->release.i_minor = i_minor;
216 p_update->release.i_revision = i_revision;
219 msg_Err( p_update->p_libvlc, "Update version false formated" );
223 /* second line : URL */
224 if( !( p_update->release.psz_url = stream_ReadLine( p_stream ) ) )
226 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
227 UPDATE_VLC_STATUS_URL );
231 /* Remaining data : description */
232 int i_read = stream_Size( p_stream ) - stream_Tell( p_stream );
235 msg_Err( p_update->p_libvlc,
236 "Update file %s is corrupted: description missing",
237 UPDATE_VLC_STATUS_URL );
241 p_update->release.psz_desc = (char*) malloc( i_read + 1 );
242 if( !p_update->release.psz_desc )
245 if( stream_Read( p_stream, p_update->release.psz_desc, i_read ) != i_read )
247 msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
248 UPDATE_VLC_STATUS_URL );
251 p_update->release.psz_desc[i_read] = '\0';
253 stream_Delete( p_stream );
256 /* Now that we know the status is valid, we must download its signature
257 * to authenticate it */
258 signature_packet_t sign;
259 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
260 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
262 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
266 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
268 msg_Err( p_update->p_libvlc, "Invalid signature type" );
272 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
273 if( !p_update->p_pkey )
276 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
277 p_update->p_pkey, NULL ) != VLC_SUCCESS )
279 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
280 FREENULL( p_update->p_pkey );
284 memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
286 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
288 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
289 public_key_t *p_new_pkey = download_key(
290 VLC_OBJECT(p_update->p_libvlc),
291 sign.issuer_longid, videolan_public_key_longid );
294 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
295 FREENULL( p_update->p_pkey );
299 uint8_t *p_hash = hash_sha1_from_public_key( p_new_pkey );
302 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
304 FREENULL( p_update->p_pkey );
308 if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
309 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
312 msg_Info( p_update->p_libvlc, "Key authenticated" );
313 free( p_update->p_pkey );
314 p_update->p_pkey = p_new_pkey;
319 msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
324 /* FIXME : read the status file all at once instead of line per line */
326 if( asprintf( &psz_text, "%s\n%s\n%s", psz_version_line,
327 p_update->release.psz_url, p_update->release.psz_desc ) == -1 )
331 FREENULL( psz_version_line );
333 uint8_t *p_hash = hash_sha1_from_text( psz_text, &sign );
336 msg_Warn( p_update->p_libvlc, "Can't compute SHA1 hash for status file" );
340 else if( p_hash[0] != sign.hash_verification[0] ||
341 p_hash[1] != sign.hash_verification[1] )
343 msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
347 else if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
350 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
356 msg_Info( p_update->p_libvlc, "Status file authenticated" );
362 stream_Delete( p_stream );
363 free( psz_version_line );
367 static void* update_CheckReal( vlc_object_t *p_this );
372 * \param p_update pointer to update struct
373 * \param pf_callback pointer to a function to call when the update_check is finished
374 * \param p_data pointer to some datas to give to the callback
377 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
381 // If the object already exist, destroy it
382 if( p_update->p_check )
384 vlc_object_kill( p_update->p_check );
385 vlc_thread_join( p_update->p_check );
386 vlc_object_release( p_update->p_check );
389 update_check_thread_t *p_uct =
390 vlc_custom_create( p_update->p_libvlc, sizeof( *p_uct ),
391 VLC_OBJECT_GENERIC, "update check" );
394 p_uct->p_update = p_update;
395 p_update->p_check = p_uct;
396 p_uct->pf_callback = pf_callback;
397 p_uct->p_data = p_data;
399 vlc_thread_create( p_uct, "check for update", update_CheckReal,
400 VLC_THREAD_PRIORITY_LOW );
403 void* update_CheckReal( vlc_object_t* p_this )
405 update_check_thread_t *p_uct = (update_check_thread_t *)p_this;
409 canc = vlc_savecancel ();
410 vlc_mutex_lock( &p_uct->p_update->lock );
412 EmptyRelease( p_uct->p_update );
413 b_ret = GetUpdateFile( p_uct->p_update );
414 vlc_mutex_unlock( &p_uct->p_update->lock );
416 if( p_uct->pf_callback )
417 (p_uct->pf_callback)( p_uct->p_data, b_ret );
419 vlc_restorecancel (canc);
424 * Compare a given release's version number to the current VLC's one
426 * \param p_update structure
427 * \return true if we have to upgrade to the given version to be up to date
429 static bool is_strictly_greater( int * a, int * b, int n)
431 if( n <= 0 ) return false;
432 if(a[0] > b[0] ) return true;
433 if(a[0] == b[0] ) return is_strictly_greater( a+1, b+1, n-1 );
434 /* a[0] < b[0] */ return false;
437 bool update_NeedUpgrade( update_t *p_update )
441 int current_version[] = {
442 *PACKAGE_VERSION_MAJOR - '0',
443 *PACKAGE_VERSION_MINOR - '0',
444 *PACKAGE_VERSION_REVISION - '0',
445 /* extra string of development versions is "-git", "-rc" ..
446 * so make sure version a.b.c is newer than a.b.c-XXX */
447 (*PACKAGE_VERSION_EXTRA == '-') ? -1 : *PACKAGE_VERSION_EXTRA
449 int latest_version[] = {
450 p_update->release.i_major,
451 p_update->release.i_minor,
452 p_update->release.i_revision,
453 p_update->release.extra
456 return is_strictly_greater( latest_version, current_version, 4 );
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 GB"), (float)l_size/(1<<30) );
471 else if( l_size >> 20 )
472 i_retval = asprintf( &psz_tmp, _("%.1f MB"), (float)l_size/(1<<20) );
473 else if( l_size >> 10 )
474 i_retval = asprintf( &psz_tmp, _("%.1f kB"), (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( vlc_object_t *p_this );
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_object_kill( p_update->p_download );
498 vlc_thread_join( p_update->p_download );
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 ),
504 VLC_OBJECT_GENERIC, "update download" );
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_thread_create( p_udt, "download update", update_DownloadReal,
513 VLC_THREAD_PRIORITY_LOW );
516 static void* update_DownloadReal( vlc_object_t *p_this )
518 update_download_thread_t *p_udt = (update_download_thread_t *)p_this;
519 dialog_progress_bar_t *p_progress = NULL;
521 long int l_downloaded = 0;
523 char *psz_status = NULL;
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 = utf8_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 );
578 msg_Err( p_udt, "Can't malloc (1 << 10) bytes! download cancelled." );
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 )
588 p_progress = dialog_ProgressCreate( p_udt, _( "Downloading ..."),
589 psz_status, _("Cancel") );
593 while( vlc_object_alive( p_udt ) &&
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_object_alive( p_udt ) &&
622 !dialog_ProgressCancelled( p_progress ) )
624 if( asprintf( &psz_status, _("%s\nDone %s (100.0%%)"),
625 p_update->release.psz_url, psz_size ) != -1 )
627 dialog_ProgressDestroy( p_progress );
634 utf8_unlink( psz_destfile );
638 signature_packet_t sign;
639 if( download_signature( VLC_OBJECT( p_udt ), &sign,
640 p_update->release.psz_url ) != VLC_SUCCESS )
642 utf8_unlink( psz_destfile );
644 dialog_FatalWait( p_udt, _("File could not be verified"),
645 _("It was not possible to download a cryptographic signature for "
646 "the downloaded file \"%s\". Thus, it was deleted."),
648 msg_Err( p_udt, "Couldn't download signature of downloaded file" );
652 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
654 utf8_unlink( psz_destfile );
655 msg_Err( p_udt, "Invalid signature issuer" );
656 dialog_FatalWait( p_udt, _("Invalid signature"),
657 _("The cryptographic signature for the downloaded file \"%s\" was "
658 "invalid and could not be used to securely verify it. Thus, the "
659 "file was deleted."),
664 if( sign.type != BINARY_SIGNATURE )
666 utf8_unlink( psz_destfile );
667 msg_Err( p_udt, "Invalid signature type" );
668 dialog_FatalWait( p_udt, _("Invalid signature"),
669 _("The cryptographic signature for the downloaded file \"%s\" was "
670 "invalid and could not be used to securely verify it. Thus, the "
671 "file was deleted."),
676 uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
679 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
680 utf8_unlink( psz_destfile );
681 dialog_FatalWait( p_udt, _("File not verifiable"),
682 _("It was not possible to securely verify the downloaded file"
683 " \"%s\". Thus, it was deleted."),
689 if( p_hash[0] != sign.hash_verification[0] ||
690 p_hash[1] != sign.hash_verification[1] )
692 utf8_unlink( psz_destfile );
693 dialog_FatalWait( p_udt, _("File corrupted"),
694 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
696 msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
701 if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
704 utf8_unlink( psz_destfile );
705 dialog_FatalWait( p_udt, _("File corrupted"),
706 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
708 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
713 msg_Info( p_udt, "%s authenticated", psz_destfile );
718 dialog_ProgressDestroy( p_progress );
720 stream_Delete( p_stream );
724 free( psz_destfile );
728 vlc_restorecancel( canc );
732 update_release_t *update_GetRelease( update_t *p_update )
734 return &p_update->release;
738 update_t *__update_New( vlc_object_t *p_this )
744 void update_Delete( update_t *p_update )
749 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
752 (void)p_update; (void)pf_callback; (void)p_data;
755 bool update_NeedUpgrade( update_t *p_update )
761 void update_Download( update_t *p_update, const char *psz_destdir )
763 (void)p_update; (void)psz_destdir;
766 update_release_t *update_GetRelease( update_t *p_update )