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 *****************************************************************************/
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
42 #include <vlc_common.h>
43 #include <vlc_update.h>
49 #include <vlc_pgpkey.h>
50 #include <vlc_stream.h>
51 #include <vlc_strings.h>
52 #include <vlc_charset.h>
53 #include <vlc_dialog.h>
56 #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.Ze" 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 letter
72 * * AKA "0.8.6d" or "0.9.0"
73 * Second line is an 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_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
79 #elif defined( WIN32 )
80 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
81 #elif defined( __APPLE__ )
82 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
83 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
85 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
87 #elif defined( SYS_BEOS )
88 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
90 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
94 /*****************************************************************************
96 *****************************************************************************/
97 static void EmptyRelease( update_t *p_update );
98 static bool GetUpdateFile( update_t *p_update );
99 static char * size_str( long int l_size );
103 /*****************************************************************************
105 *****************************************************************************/
108 * Create a new update VLC struct
110 * \param p_this the calling vlc_object
111 * \return pointer to new update_t or NULL
113 update_t *__update_New( vlc_object_t *p_this )
118 p_update = (update_t *)malloc( sizeof( update_t ) );
119 if( !p_update ) return NULL;
121 vlc_mutex_init( &p_update->lock );
123 p_update->p_libvlc = p_this->p_libvlc;
125 p_update->release.psz_url = NULL;
126 p_update->release.psz_desc = NULL;
128 p_update->p_download = NULL;
129 p_update->p_check = NULL;
131 p_update->p_pkey = NULL;
138 * Delete an update_t struct
140 * \param p_update update_t* pointer
143 void update_Delete( update_t *p_update )
147 if( p_update->p_check )
149 assert( !p_update->p_download );
150 vlc_object_kill( p_update->p_check );
151 vlc_thread_join( p_update->p_check );
152 vlc_object_release( p_update->p_check );
154 else if( p_update->p_download )
156 vlc_object_kill( p_update->p_download );
157 vlc_thread_join( p_update->p_download );
158 vlc_object_release( p_update->p_download );
161 vlc_mutex_destroy( &p_update->lock );
163 free( p_update->release.psz_url );
164 free( p_update->release.psz_desc );
165 free( p_update->p_pkey );
171 * Empty the release struct
173 * \param p_update update_t* pointer
176 static void EmptyRelease( update_t *p_update )
178 p_update->release.i_major = 0;
179 p_update->release.i_minor = 0;
180 p_update->release.i_revision = 0;
182 FREENULL( p_update->release.psz_url );
183 FREENULL( p_update->release.psz_desc );
187 * Get the update file and parse it
188 * p_update has to be locked when calling this function
190 * \param p_update pointer to update struct
191 * \return true if the update is valid and authenticated
193 static bool GetUpdateFile( update_t *p_update )
195 stream_t *p_stream = NULL;
200 char *psz_version_line = NULL;
202 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
205 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
206 UPDATE_VLC_STATUS_URL );
210 /* Start reading the status file */
211 if( !( psz_version_line = stream_ReadLine( p_stream ) ) )
213 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
214 UPDATE_VLC_STATUS_URL );
218 /* first line : version number */
219 p_update->release.extra = 0;
220 switch( sscanf( psz_version_line, "%i.%i.%i%c",
221 &i_major, &i_minor, &i_revision, &extra ) )
224 p_update->release.extra = extra;
226 p_update->release.i_major = i_major;
227 p_update->release.i_minor = i_minor;
228 p_update->release.i_revision = i_revision;
231 msg_Err( p_update->p_libvlc, "Update version false formated" );
235 /* second line : URL */
236 if( !( p_update->release.psz_url = stream_ReadLine( p_stream ) ) )
238 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
239 UPDATE_VLC_STATUS_URL );
243 /* Remaining data : description */
244 int i_read = stream_Size( p_stream ) - stream_Tell( p_stream );
247 msg_Err( p_update->p_libvlc,
248 "Update file %s is corrupted: description missing",
249 UPDATE_VLC_STATUS_URL );
253 p_update->release.psz_desc = (char*) malloc( i_read + 1 );
254 if( !p_update->release.psz_desc )
257 if( stream_Read( p_stream, p_update->release.psz_desc, i_read ) != i_read )
259 msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
260 UPDATE_VLC_STATUS_URL );
263 p_update->release.psz_desc[i_read] = '\0';
265 stream_Delete( p_stream );
268 /* Now that we know the status is valid, we must download its signature
269 * to authenticate it */
270 signature_packet_t sign;
271 if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
272 UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
274 msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
278 if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
280 msg_Err( p_update->p_libvlc, "Invalid signature type" );
284 p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
285 if( !p_update->p_pkey )
288 if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
289 p_update->p_pkey, NULL ) != VLC_SUCCESS )
291 msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
292 FREENULL( p_update->p_pkey );
296 memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );
298 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
300 msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
301 public_key_t *p_new_pkey = download_key(
302 VLC_OBJECT(p_update->p_libvlc),
303 sign.issuer_longid, videolan_public_key_longid );
306 msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
307 FREENULL( p_update->p_pkey );
311 uint8_t *p_hash = hash_sha1_from_public_key( p_new_pkey );
314 msg_Err( p_update->p_libvlc, "Failed to hash signature" );
316 FREENULL( p_update->p_pkey );
320 if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
321 &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
324 msg_Info( p_update->p_libvlc, "Key authenticated" );
325 free( p_update->p_pkey );
326 p_update->p_pkey = p_new_pkey;
331 msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
336 /* FIXME : read the status file all at once instead of line per line */
338 if( asprintf( &psz_text, "%s\n%s\n%s", psz_version_line,
339 p_update->release.psz_url, p_update->release.psz_desc ) == -1 )
343 FREENULL( psz_version_line );
345 uint8_t *p_hash = hash_sha1_from_text( psz_text, &sign );
348 msg_Warn( p_update->p_libvlc, "Can't compute SHA1 hash for status file" );
352 else if( p_hash[0] != sign.hash_verification[0] ||
353 p_hash[1] != sign.hash_verification[1] )
355 msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
359 else if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
362 msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
368 msg_Info( p_update->p_libvlc, "Status file authenticated" );
374 stream_Delete( p_stream );
375 free( psz_version_line );
379 static void* update_CheckReal( vlc_object_t *p_this );
384 * \param p_update pointer to update struct
385 * \param pf_callback pointer to a function to call when the update_check is finished
386 * \param p_data pointer to some datas to give to the callback
389 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
393 // If the object already exist, destroy it
394 if( p_update->p_check )
396 vlc_object_kill( p_update->p_check );
397 vlc_thread_join( p_update->p_check );
398 vlc_object_release( p_update->p_check );
401 update_check_thread_t *p_uct =
402 vlc_custom_create( p_update->p_libvlc, sizeof( *p_uct ),
403 VLC_OBJECT_GENERIC, "update check" );
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_thread_create( p_uct, "check for update", update_CheckReal,
412 VLC_THREAD_PRIORITY_LOW );
415 void* update_CheckReal( vlc_object_t* p_this )
417 update_check_thread_t *p_uct = (update_check_thread_t *)p_this;
421 canc = vlc_savecancel ();
422 vlc_mutex_lock( &p_uct->p_update->lock );
424 EmptyRelease( p_uct->p_update );
425 b_ret = GetUpdateFile( p_uct->p_update );
426 vlc_mutex_unlock( &p_uct->p_update->lock );
428 if( p_uct->pf_callback )
429 (p_uct->pf_callback)( p_uct->p_data, b_ret );
431 vlc_restorecancel (canc);
436 * Compare a given release's version number to the current VLC's one
438 * \param p_update structure
439 * \return true if we have to upgrade to the given version to be up to date
441 static bool is_strictly_greater( int * a, int * b, int n)
443 if( n <= 0 ) return false;
444 if(a[0] > b[0] ) return true;
445 if(a[0] == b[0] ) return is_strictly_greater( a+1, b+1, n-1 );
446 /* a[0] < b[0] */ return false;
449 bool update_NeedUpgrade( update_t *p_update )
453 int current_version[] = {
454 *PACKAGE_VERSION_MAJOR - '0',
455 *PACKAGE_VERSION_MINOR - '0',
456 *PACKAGE_VERSION_REVISION - '0',
457 *PACKAGE_VERSION_EXTRA
459 int latest_version[] = {
460 p_update->release.i_major,
461 p_update->release.i_minor,
462 p_update->release.i_revision,
463 p_update->release.extra
466 return is_strictly_greater( latest_version, current_version, 4 );
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 GB"), (float)l_size/(1<<30) );
481 else if( l_size >> 20 )
482 i_retval = asprintf( &psz_tmp, _("%.1f MB"), (float)l_size/(1<<20) );
483 else if( l_size >> 10 )
484 i_retval = asprintf( &psz_tmp, _("%.1f kB"), (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( vlc_object_t *p_this );
494 * Download the file given in the update_t
496 * \param p_update structure
497 * \param destination to store the download file
498 * This can be an existing dir, a (non)existing target fullpath filename or
499 * NULL for the current working dir.
502 void update_Download( update_t *p_update, const char *destination )
506 // If the object already exist, destroy it
507 if( p_update->p_download )
509 vlc_object_kill( p_update->p_download );
510 vlc_thread_join( p_update->p_download );
511 vlc_object_release( p_update->p_download );
514 update_download_thread_t *p_udt =
515 vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),
516 VLC_OBJECT_GENERIC, "update download" );
520 p_udt->p_update = p_update;
521 p_update->p_download = p_udt;
522 p_udt->psz_destination = destination ? strdup( destination ) : NULL;
524 vlc_thread_create( p_udt, "download update", update_DownloadReal,
525 VLC_THREAD_PRIORITY_LOW );
528 static void* update_DownloadReal( vlc_object_t *p_this )
530 update_download_thread_t *p_udt = (update_download_thread_t *)p_this;
531 dialog_progress_bar_t *p_progress = NULL;
533 long int l_downloaded = 0;
535 char *psz_status = NULL;
536 char *psz_downloaded = NULL;
537 char *psz_size = NULL;
538 char *psz_destfile = NULL;
539 char *psz_tmpdestfile = NULL;
543 stream_t *p_stream = NULL;
544 void* p_buffer = NULL;
548 update_t *p_update = p_udt->p_update;
549 char *psz_destination = p_udt->psz_destination;
551 msg_Dbg( p_udt, "Opening Stream '%s'", p_update->release.psz_url );
552 canc = vlc_savecancel ();
554 /* Open the stream */
555 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
558 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
562 /* Get the stream size */
563 l_size = stream_Size( p_stream );
565 /* Get the file name and open it*/
566 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
567 if( !psz_tmpdestfile )
569 msg_Err( p_udt, "The URL %s is badly formated",
570 p_update->release.psz_url );
575 if( utf8_stat( psz_destination, &p_stat) == 0 && (p_stat.st_mode & S_IFDIR) )
577 if( asprintf( &psz_destfile, "%s%c%s", psz_destination, DIR_SEP_CHAR, psz_tmpdestfile ) == -1 )
580 else if( psz_destination )
581 psz_destfile = strdup( psz_destination );
583 psz_destfile = strdup( psz_tmpdestfile );
585 p_file = utf8_fopen( psz_destfile, "w" );
588 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
589 dialog_FatalWait( p_udt, _("Saving file failed"),
590 _("Failed to open \"%s\" for writing"),
595 /* Create a buffer and fill it with the downloaded file */
596 p_buffer = (void *)malloc( 1 << 10 );
599 msg_Err( p_udt, "Can't malloc (1 << 10) bytes! download cancelled." );
603 msg_Dbg( p_udt, "Downloading Stream '%s'", p_update->release.psz_url );
605 psz_size = size_str( l_size );
606 if( asprintf( &psz_status, _("%s\nDownloading... %s/%s %.1f%% done"),
607 p_update->release.psz_url, "0.0", psz_size, 0.0 ) != -1 )
609 p_progress = dialog_ProgressCreate( p_udt, _( "Downloading ..."),
610 psz_status, _("Cancel") );
614 while( vlc_object_alive( p_udt ) &&
615 ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
616 !dialog_ProgressCancelled( p_progress ) )
618 if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
620 msg_Err( p_udt, "Failed to write into %s", psz_destfile );
624 l_downloaded += i_read;
625 psz_downloaded = size_str( l_downloaded );
626 f_progress = (float)l_downloaded/(float)l_size;
628 if( asprintf( &psz_status, _( "%s\nDownloading... %s/%s %.1f%% done" ),
629 p_update->release.psz_url, psz_downloaded, psz_size,
632 dialog_ProgressSet( p_progress, psz_status, f_progress );
635 free( psz_downloaded );
638 /* Finish the progress bar or delete the file if the user had canceled */
642 if( vlc_object_alive( p_udt ) &&
643 !dialog_ProgressCancelled( p_progress ) )
645 if( asprintf( &psz_status, _("%s\nDone %s (100.0%%)"),
646 p_update->release.psz_url, psz_size ) != -1 )
648 dialog_ProgressDestroy( p_progress );
655 utf8_unlink( psz_destfile );
659 signature_packet_t sign;
660 if( download_signature( VLC_OBJECT( p_udt ), &sign,
661 p_update->release.psz_url ) != VLC_SUCCESS )
663 utf8_unlink( psz_destfile );
665 dialog_FatalWait( p_udt, _("File could not be verified"),
666 _("It was not possible to download a cryptographic signature for "
667 "the downloaded file \"%s\". Thus, it was deleted."),
669 msg_Err( p_udt, "Couldn't download signature of downloaded file" );
673 if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
675 utf8_unlink( psz_destfile );
676 msg_Err( p_udt, "Invalid signature issuer" );
677 dialog_FatalWait( p_udt, _("Invalid signature"),
678 _("The cryptographic signature for the downloaded file \"%s\" was "
679 "invalid and could not be used to securely verify it. Thus, the "
680 "file was deleted."),
685 if( sign.type != BINARY_SIGNATURE )
687 utf8_unlink( psz_destfile );
688 msg_Err( p_udt, "Invalid signature type" );
689 dialog_FatalWait( p_udt, _("Invalid signature"),
690 _("The cryptographic signature for the downloaded file \"%s\" was "
691 "invalid and could not be used to securely verify it. Thus, the "
692 "file was deleted."),
697 uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
700 msg_Err( p_udt, "Unable to hash %s", psz_destfile );
701 utf8_unlink( psz_destfile );
702 dialog_FatalWait( p_udt, _("File not verifiable"),
703 _("It was not possible to securely verify the downloaded file"
704 " \"%s\". Thus, it was deleted."),
710 if( p_hash[0] != sign.hash_verification[0] ||
711 p_hash[1] != sign.hash_verification[1] )
713 utf8_unlink( psz_destfile );
714 dialog_FatalWait( p_udt, _("File corrupted"),
715 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
717 msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
722 if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
725 utf8_unlink( psz_destfile );
726 dialog_FatalWait( p_udt, _("File corrupted"),
727 _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
729 msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
734 msg_Info( p_udt, "%s authenticated", psz_destfile );
739 dialog_ProgressDestroy( p_progress );
741 stream_Delete( p_stream );
744 free( psz_destfile );
748 free( p_udt->psz_destination );
749 p_udt->p_update->p_download = NULL;
751 vlc_restorecancel( canc );
755 update_release_t *update_GetRelease( update_t *p_update )
757 return &p_update->release;
761 update_t *__update_New( vlc_object_t *p_this )
767 void update_Delete( update_t *p_update )
772 void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
775 (void)p_update; (void)pf_callback; (void)p_data;
778 bool update_NeedUpgrade( update_t *p_update )
784 void update_Download( update_t *p_update, const char *psz_destdir )
786 (void)p_update; (void)psz_destdir;
789 update_release_t *update_GetRelease( update_t *p_update )