update.c 22.7 KB
Newer Older
1
/*****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
2
 * update.c: VLC update checking and downloading
3
 *****************************************************************************
4
 * Copyright © 2005-2008 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
Rafaël Carré's avatar
Rafaël Carré committed
8 9
 *          Rémi Duraffort <ivoire at via.ecp.fr>
            Rafaël Carré <funman@videolanorg>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either release 2 of the License, or
 * (at your option) any later release.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/
25

26 27
/**
 *   \file
28
 *   This file contains functions related to VLC update management
29 30 31 32 33
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/
34

35 36 37 38
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

39
#include <vlc_common.h>
40
#include <vlc_update.h>
41

42 43
#ifdef UPDATE_CHECK

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
44
#include <assert.h>
45

46
#include <vlc_pgpkey.h>
zorglub's avatar
zorglub committed
47
#include <vlc_stream.h>
48
#include <vlc_strings.h>
49
#include <vlc_fs.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
50
#include <vlc_dialog.h>
51
#include <vlc_interface.h>
52 53

#include <gcrypt.h>
54
#include <vlc_gcrypt.h>
55
#ifdef WIN32
56
#include <shellapi.h>
57
#endif
58
#include "update.h"
59
#include "../libvlc.h"
60

61 62 63 64
/*****************************************************************************
 * Misc defines
 *****************************************************************************/

Rafaël Carré's avatar
Rafaël Carré committed
65 66 67 68 69 70 71 72
/*
 * Here is the format of these "status files" :
 * First line is the last version: "X.Y.Ze" where:
 *      * X is the major number
 *      * Y is the minor number
 *      * Z is the revision number
 *      * e is an OPTIONAL extra letter
 *      * 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
Rafaël Carré's avatar
Rafaël Carré committed
75 76
 */

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
#else
82
#   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
83 84
#endif

85

86 87 88 89
/*****************************************************************************
 * Update_t functions
 *****************************************************************************/

90
#undef update_New
91 92 93 94 95 96
/**
 * Create a new update VLC struct
 *
 * \param p_this the calling vlc_object
 * \return pointer to new update_t or NULL
 */
97
update_t *update_New( vlc_object_t *p_this )
98 99
{
    update_t *p_update;
100
    assert( p_this );
101 102

    p_update = (update_t *)malloc( sizeof( update_t ) );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
103
    if( !p_update ) return NULL;
104

105
    vlc_mutex_init( &p_update->lock );
106

107
    p_update->p_libvlc = p_this->p_libvlc;
108

109 110
    p_update->release.psz_url = NULL;
    p_update->release.psz_desc = NULL;
111

112 113 114
    p_update->p_download = NULL;
    p_update->p_check = NULL;

115
    p_update->p_pkey = NULL;
116
    vlc_gcrypt_init();
117

118
    return p_update;
119 120 121 122 123 124 125 126 127 128
}

/**
 * Delete an update_t struct
 *
 * \param p_update update_t* pointer
 * \return nothing
 */
void update_Delete( update_t *p_update )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
129 130
    assert( p_update );

131 132 133 134
    if( p_update->p_check )
    {
        vlc_object_kill( p_update->p_check );
        vlc_thread_join( p_update->p_check );
135
        vlc_object_release( p_update->p_check );
136
    }
137 138

    if( p_update->p_download )
139 140 141
    {
        vlc_object_kill( p_update->p_download );
        vlc_thread_join( p_update->p_download );
142
        vlc_object_release( p_update->p_download );
143 144
    }

145 146
    vlc_mutex_destroy( &p_update->lock );

147 148 149
    free( p_update->release.psz_url );
    free( p_update->release.psz_desc );
    free( p_update->p_pkey );
150 151

    free( p_update );
152 153 154
}

/**
155
 * Empty the release struct
156
 *
157
 * \param p_update update_t* pointer
158 159
 * \return nothing
 */
160
static void EmptyRelease( update_t *p_update )
161
{
162 163 164
    p_update->release.i_major = 0;
    p_update->release.i_minor = 0;
    p_update->release.i_revision = 0;
165

166 167
    FREENULL( p_update->release.psz_url );
    FREENULL( p_update->release.psz_desc );
168 169 170
}

/**
171
 * Get the update file and parse it
ivoire's avatar
ivoire committed
172
 * p_update has to be locked when calling this function
173 174
 *
 * \param p_update pointer to update struct
175
 * \return true if the update is valid and authenticated
176
 */
177
static bool GetUpdateFile( update_t *p_update )
178 179
{
    stream_t *p_stream = NULL;
180 181 182
    int i_major = 0;
    int i_minor = 0;
    int i_revision = 0;
Rafaël Carré's avatar
Rafaël Carré committed
183
    unsigned char extra;
184
    char *psz_version_line = NULL;
185
    char *psz_update_data = NULL;
186

187
    p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
188 189
    if( !p_stream )
    {
190
        msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
191 192 193 194
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }

195 196 197 198 199 200
    const int64_t i_read = stream_Size( p_stream );
    psz_update_data = malloc( i_read + 1 ); /* terminating '\0' */
    if( !psz_update_data )
        goto error;

    if( stream_Read( p_stream, psz_update_data, i_read ) != i_read )
201
    {
202 203
        msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
                UPDATE_VLC_STATUS_URL );
204 205
        goto error;
    }
206 207 208 209
    psz_update_data[i_read] = '\0';

    stream_Delete( p_stream );
    p_stream = NULL;
210

211
    /* first line : version number */
212 213 214 215 216 217 218 219 220 221 222
    char *psz_update_data_parser = psz_update_data;
    size_t i_len = strcspn( psz_update_data, "\r\n" );
    psz_update_data_parser += i_len;
    while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
        psz_update_data_parser++;

    if( !(psz_version_line = malloc( i_len + 1)) )
        goto error;
    strncpy( psz_version_line, psz_update_data, i_len );
    psz_version_line[i_len] = '\0';

Rafaël Carré's avatar
Rafaël Carré committed
223
    p_update->release.extra = 0;
224 225
    switch( sscanf( psz_version_line, "%i.%i.%i%c",
                    &i_major, &i_minor, &i_revision, &extra ) )
226
    {
Rafaël Carré's avatar
Rafaël Carré committed
227 228 229 230 231 232 233 234 235 236
        case 4:
            p_update->release.extra = extra;
        case 3:
            p_update->release.i_major = i_major;
            p_update->release.i_minor = i_minor;
            p_update->release.i_revision = i_revision;
            break;
        default:
            msg_Err( p_update->p_libvlc, "Update version false formated" );
            goto error;
237
    }
238

239
    /* second line : URL */
240 241
    i_len = strcspn( psz_update_data_parser, "\r\n" );
    if( i_len == 0 )
242
    {
243
        msg_Err( p_update->p_libvlc, "Update file %s is corrupted: URL missing",
244
                 UPDATE_VLC_STATUS_URL );
245

246 247
        goto error;
    }
248

249 250 251 252 253 254 255 256 257
    if( !(p_update->release.psz_url = malloc( i_len + 1)) )
        goto error;
    strncpy( p_update->release.psz_url, psz_update_data_parser, i_len );
    p_update->release.psz_url[i_len] = '\0';

    psz_update_data_parser += i_len;
    while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
        psz_update_data_parser++;

258
    /* Remaining data : description */
259 260
    i_len = strlen( psz_update_data_parser );
    if( i_len == 0 )
261 262 263 264 265 266
    {
        msg_Err( p_update->p_libvlc,
                "Update file %s is corrupted: description missing",
                UPDATE_VLC_STATUS_URL );
        goto error;
    }
267

268
    if( !(p_update->release.psz_desc = malloc( i_len + 1)) )
269
        goto error;
270 271
    strncpy( p_update->release.psz_desc, psz_update_data_parser, i_len );
    p_update->release.psz_desc[i_len] = '\0';
272

273
    /* Now that we know the status is valid, we must download its signature
274
     * to authenticate it */
275
    signature_packet_t sign;
276
    if( download_signature( VLC_OBJECT( p_update->p_libvlc ), &sign,
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
            UPDATE_VLC_STATUS_URL ) != VLC_SUCCESS )
    {
        msg_Err( p_update->p_libvlc, "Couldn't download signature of status file" );
        goto error;
    }

    if( sign.type != BINARY_SIGNATURE && sign.type != TEXT_SIGNATURE )
    {
        msg_Err( p_update->p_libvlc, "Invalid signature type" );
        goto error;
    }

    p_update->p_pkey = (public_key_t*)malloc( sizeof( public_key_t ) );
    if( !p_update->p_pkey )
        goto error;

    if( parse_public_key( videolan_public_key, sizeof( videolan_public_key ),
                        p_update->p_pkey, NULL ) != VLC_SUCCESS )
    {
        msg_Err( p_update->p_libvlc, "Couldn't parse embedded public key, something went really wrong..." );
        FREENULL( p_update->p_pkey );
        goto error;
    }

301 302 303
    memcpy( p_update->p_pkey->longid, videolan_public_key_longid, 8 );

    if( memcmp( sign.issuer_longid, p_update->p_pkey->longid , 8 ) != 0 )
304 305 306 307 308 309 310 311 312 313 314 315
    {
        msg_Dbg( p_update->p_libvlc, "Need to download the GPG key" );
        public_key_t *p_new_pkey = download_key(
                VLC_OBJECT(p_update->p_libvlc),
                sign.issuer_longid, videolan_public_key_longid );
        if( !p_new_pkey )
        {
            msg_Err( p_update->p_libvlc, "Couldn't download GPG key" );
            FREENULL( p_update->p_pkey );
            goto error;
        }

316
        uint8_t *p_hash = hash_sha1_from_public_key( p_new_pkey );
317 318 319 320 321 322 323 324
        if( !p_hash )
        {
            msg_Err( p_update->p_libvlc, "Failed to hash signature" );
            free( p_new_pkey );
            FREENULL( p_update->p_pkey );
            goto error;
        }

325
        if( verify_signature( p_new_pkey->sig.r, p_new_pkey->sig.s,
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
                    &p_update->p_pkey->key, p_hash ) == VLC_SUCCESS )
        {
            free( p_hash );
            msg_Info( p_update->p_libvlc, "Key authenticated" );
            free( p_update->p_pkey );
            p_update->p_pkey = p_new_pkey;
        }
        else
        {
            free( p_hash );
            msg_Err( p_update->p_libvlc, "Key signature invalid !\n" );
            goto error;
        }
    }

341
    uint8_t *p_hash = hash_sha1_from_text( psz_update_data, &sign );
342
    if( !p_hash )
343
    {
344 345
        msg_Warn( p_update->p_libvlc, "Can't compute SHA1 hash for status file" );
        goto error;
346
    }
347

348
    else if( p_hash[0] != sign.hash_verification[0] ||
349 350 351
        p_hash[1] != sign.hash_verification[1] )
    {
        msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );
352
        goto error;
353 354
    }

355
    else if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
356
            != VLC_SUCCESS )
357 358
    {
        msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );
359
        goto error;
360
    }
361

362 363 364
    else
    {
        msg_Info( p_update->p_libvlc, "Status file authenticated" );
365
        return true;
366 367
    }

368
error:
369 370
    if( p_stream )
        stream_Delete( p_stream );
371
    free( psz_version_line );
372
    free( psz_update_data );
373
    return false;
374 375
}

ivoire's avatar
ivoire committed
376
static void* update_CheckReal( vlc_object_t *p_this );
377

378 379 380 381
/**
 * Check for updates
 *
 * \param p_update pointer to update struct
382 383
 * \param pf_callback pointer to a function to call when the update_check is finished
 * \param p_data pointer to some datas to give to the callback
384 385
 * \returns nothing
 */
386
void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data )
387
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
388 389
    assert( p_update );

ivoire's avatar
ivoire committed
390 391 392 393 394 395 396 397
    // If the object already exist, destroy it
    if( p_update->p_check )
    {
        vlc_object_kill( p_update->p_check );
        vlc_thread_join( p_update->p_check );
        vlc_object_release( p_update->p_check );
    }

398 399 400
    update_check_thread_t *p_uct =
        vlc_custom_create( p_update->p_libvlc, sizeof( *p_uct ),
                           VLC_OBJECT_GENERIC, "update check" );
401
    if( !p_uct ) return;
402

403
    p_uct->p_update = p_update;
404
    p_update->p_check = p_uct;
405 406
    p_uct->pf_callback = pf_callback;
    p_uct->p_data = p_data;
407

408
    vlc_thread_create( p_uct, update_CheckReal, VLC_THREAD_PRIORITY_LOW );
409 410
}

ivoire's avatar
ivoire committed
411
void* update_CheckReal( vlc_object_t* p_this )
412
{
ivoire's avatar
ivoire committed
413
    update_check_thread_t *p_uct = (update_check_thread_t *)p_this;
414
    bool b_ret;
415 416
    int canc;

417
    canc = vlc_savecancel ();
418 419 420
    vlc_mutex_lock( &p_uct->p_update->lock );

    EmptyRelease( p_uct->p_update );
421
    b_ret = GetUpdateFile( p_uct->p_update );
422
    vlc_mutex_unlock( &p_uct->p_update->lock );
423

424 425
    if( p_uct->pf_callback )
        (p_uct->pf_callback)( p_uct->p_data, b_ret );
426 427

    vlc_restorecancel (canc);
ivoire's avatar
ivoire committed
428
    return NULL;
429 430
}

431
/**
432
 * Compare a given release's version number to the current VLC's one
433
 *
434
 * \param p_update structure
435
 * \return true if we have to upgrade to the given version to be up to date
436
 */
437 438 439 440 441 442 443 444
static bool is_strictly_greater( int * a, int * b, int n)
{
    if( n <= 0 ) return false;
    if(a[0] > b[0] ) return true;
    if(a[0] == b[0] ) return is_strictly_greater( a+1, b+1, n-1 );
    /* a[0] < b[0] */ return false;
}

445
bool update_NeedUpgrade( update_t *p_update )
446
{
447
    assert( p_update );
448

449 450 451 452
    int current_version[] = {
        *PACKAGE_VERSION_MAJOR - '0',
        *PACKAGE_VERSION_MINOR - '0',
        *PACKAGE_VERSION_REVISION - '0',
453 454 455
        /* extra string of development versions is "-git", "-rc" ..
         * so make sure version a.b.c is newer than a.b.c-XXX */
        (*PACKAGE_VERSION_EXTRA == '-') ? -1 : *PACKAGE_VERSION_EXTRA
456 457 458 459 460 461 462 463 464
    };
    int latest_version[] = {
        p_update->release.i_major,
        p_update->release.i_minor,
        p_update->release.i_revision,
        p_update->release.extra
    };

    return is_strictly_greater( latest_version, current_version, 4 );
465
}
466

467 468 469 470 471 472 473 474
/**
 * Convert a long int size in bytes to a string
 *
 * \param l_size the size in bytes
 * \return the size as a string
 */
static char *size_str( long int l_size )
{
Rafaël Carré's avatar
Rafaël Carré committed
475
    char *psz_tmp = NULL;
ivoire's avatar
ivoire committed
476
    int i_retval = 0;
Rafaël Carré's avatar
Rafaël Carré committed
477
    if( l_size >> 30 )
478
        i_retval = asprintf( &psz_tmp, _("%.1f GiB"), (float)l_size/(1<<30) );
Rafaël Carré's avatar
Rafaël Carré committed
479
    else if( l_size >> 20 )
480
        i_retval = asprintf( &psz_tmp, _("%.1f MiB"), (float)l_size/(1<<20) );
481
    else if( l_size >> 10 )
482
        i_retval = asprintf( &psz_tmp, _("%.1f KiB"), (float)l_size/(1<<10) );
483
    else
Christophe Mutricy's avatar
Christophe Mutricy committed
484
        i_retval = asprintf( &psz_tmp, _("%ld B"), l_size );
ivoire's avatar
ivoire committed
485 486

    return i_retval == -1 ? NULL : psz_tmp;
487 488
}

ivoire's avatar
ivoire committed
489
static void* update_DownloadReal( vlc_object_t *p_this );
490 491 492 493 494

/**
 * Download the file given in the update_t
 *
 * \param p_update structure
495
 * \param dir to store the download file
496 497
 * \return nothing
 */
498
void update_Download( update_t *p_update, const char *psz_destdir )
499 500 501
{
    assert( p_update );

ivoire's avatar
ivoire committed
502 503 504 505 506 507 508 509
    // If the object already exist, destroy it
    if( p_update->p_download )
    {
        vlc_object_kill( p_update->p_download );
        vlc_thread_join( p_update->p_download );
        vlc_object_release( p_update->p_download );
    }

510 511 512
    update_download_thread_t *p_udt =
        vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),
                           VLC_OBJECT_GENERIC, "update download" );
513 514
    if( !p_udt )
        return;
515 516

    p_udt->p_update = p_update;
517
    p_update->p_download = p_udt;
518
    p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
519

520
    vlc_thread_create( p_udt, update_DownloadReal, VLC_THREAD_PRIORITY_LOW );
521
}
ivoire's avatar
ivoire committed
522

ivoire's avatar
ivoire committed
523
static void* update_DownloadReal( vlc_object_t *p_this )
524
{
ivoire's avatar
ivoire committed
525
    update_download_thread_t *p_udt = (update_download_thread_t *)p_this;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
526
    dialog_progress_bar_t *p_progress = NULL;
527 528
    long int l_size;
    long int l_downloaded = 0;
ivoire's avatar
ivoire committed
529
    float f_progress;
530
    char *psz_status;
ivoire's avatar
ivoire committed
531 532 533 534
    char *psz_downloaded = NULL;
    char *psz_size = NULL;
    char *psz_destfile = NULL;
    char *psz_tmpdestfile = NULL;
535 536

    FILE *p_file = NULL;
ivoire's avatar
ivoire committed
537 538
    stream_t *p_stream = NULL;
    void* p_buffer = NULL;
539
    int i_read;
540
    int canc;
541 542

    update_t *p_update = p_udt->p_update;
543
    char *psz_destdir = p_udt->psz_destdir;
544

545
    msg_Dbg( p_udt, "Opening Stream '%s'", p_update->release.psz_url );
546
    canc = vlc_savecancel ();
547

548
    /* Open the stream */
549
    p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
550 551
    if( !p_stream )
    {
552
        msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
553
        goto end;
554
    }
ivoire's avatar
ivoire committed
555 556 557 558 559 560 561 562

    /* Get the stream size */
    l_size = stream_Size( p_stream );

    /* Get the file name and open it*/
    psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
    if( !psz_tmpdestfile )
    {
Christophe Mutricy's avatar
Christophe Mutricy committed
563 564
        msg_Err( p_udt, "The URL %s is badly formated",
                 p_update->release.psz_url );
565
        goto end;
ivoire's avatar
ivoire committed
566 567
    }
    psz_tmpdestfile++;
568 569
    if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
        goto end;
ivoire's avatar
ivoire committed
570

571
    p_file = vlc_fopen( psz_destfile, "w" );
ivoire's avatar
ivoire committed
572 573
    if( !p_file )
    {
574
        msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
575
        dialog_FatalWait( p_udt, _("Saving file failed"),
576 577
            _("Failed to open \"%s\" for writing"),
             psz_destfile );
578
        goto end;
ivoire's avatar
ivoire committed
579 580 581 582 583
    }

    /* Create a buffer and fill it with the downloaded file */
    p_buffer = (void *)malloc( 1 << 10 );
    if( !p_buffer )
584 585
    {
        msg_Err( p_udt, "Can't malloc (1 << 10) bytes! download cancelled." );
586
        goto end;
587 588 589
    }

    msg_Dbg( p_udt, "Downloading Stream '%s'", p_update->release.psz_url );
ivoire's avatar
ivoire committed
590 591

    psz_size = size_str( l_size );
Christophe Mutricy's avatar
Christophe Mutricy committed
592 593
    if( asprintf( &psz_status, _("%s\nDownloading... %s/%s %.1f%% done"),
        p_update->release.psz_url, "0.0", psz_size, 0.0 ) != -1 )
ivoire's avatar
ivoire committed
594
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
595 596
        p_progress = dialog_ProgressCreate( p_udt, _( "Downloading ..."),
                                            psz_status, _("Cancel") );
ivoire's avatar
ivoire committed
597 598 599
        free( psz_status );
    }

600 601
    while( vlc_object_alive( p_udt ) &&
           ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
602
           !dialog_ProgressCancelled( p_progress ) )
603
    {
ivoire's avatar
ivoire committed
604 605 606 607 608
        if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
        {
            msg_Err( p_udt, "Failed to write into %s", psz_destfile );
            break;
        }
ivoire's avatar
ivoire committed
609 610 611

        l_downloaded += i_read;
        psz_downloaded = size_str( l_downloaded );
612
        f_progress = (float)l_downloaded/(float)l_size;
613

614
        if( asprintf( &psz_status, _( "%s\nDownloading... %s/%s - %.1f%% done" ),
ivoire's avatar
ivoire committed
615
                      p_update->release.psz_url, psz_downloaded, psz_size,
616
                      f_progress*100 ) != -1 )
617
        {
618
            dialog_ProgressSet( p_progress, psz_status, f_progress );
ivoire's avatar
ivoire committed
619
            free( psz_status );
620
        }
ivoire's avatar
ivoire committed
621 622 623 624 625 626
        free( psz_downloaded );
    }

    /* Finish the progress bar or delete the file if the user had canceled */
    fclose( p_file );
    p_file = NULL;
627

628
    if( vlc_object_alive( p_udt ) &&
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
629
        !dialog_ProgressCancelled( p_progress ) )
ivoire's avatar
ivoire committed
630
    {
631 632
        dialog_ProgressDestroy( p_progress );
        p_progress = NULL;
633
    }
ivoire's avatar
ivoire committed
634
    else
635
    {
636
        vlc_unlink( psz_destfile );
637 638
        goto end;
    }
ivoire's avatar
ivoire committed
639

640
    signature_packet_t sign;
641 642 643
    if( download_signature( VLC_OBJECT( p_udt ), &sign,
            p_update->release.psz_url ) != VLC_SUCCESS )
    {
644
        vlc_unlink( psz_destfile );
645

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
646
        dialog_FatalWait( p_udt, _("File could not be verified"),
dionoea's avatar
dionoea committed
647
            _("It was not possible to download a cryptographic signature for "
Felix Paul Kühne's avatar
Felix Paul Kühne committed
648
              "the downloaded file \"%s\". Thus, it was deleted."),
649
            psz_destfile );
650
        msg_Err( p_udt, "Couldn't download signature of downloaded file" );
651 652
        goto end;
    }
653 654 655

    if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
    {
656
        vlc_unlink( psz_destfile );
657
        msg_Err( p_udt, "Invalid signature issuer" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
658
        dialog_FatalWait( p_udt, _("Invalid signature"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
659 660 661
            _("The cryptographic signature for the downloaded file \"%s\" was "
              "invalid and could not be used to securely verify it. Thus, the "
              "file was deleted."),
662 663 664
            psz_destfile );
        goto end;
    }
665 666 667

    if( sign.type != BINARY_SIGNATURE )
    {
668
        vlc_unlink( psz_destfile );
669
        msg_Err( p_udt, "Invalid signature type" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
670
        dialog_FatalWait( p_udt, _("Invalid signature"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
671 672
            _("The cryptographic signature for the downloaded file \"%s\" was "
              "invalid and could not be used to securely verify it. Thus, the "
Felix Paul Kühne's avatar
Typo  
Felix Paul Kühne committed
673
              "file was deleted."),
674
            psz_destfile );
675 676 677 678 679 680 681
        goto end;
    }

    uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );
    if( !p_hash )
    {
        msg_Err( p_udt, "Unable to hash %s", psz_destfile );
682
        vlc_unlink( psz_destfile );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
683
        dialog_FatalWait( p_udt, _("File not verifiable"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
684
            _("It was not possible to securely verify the downloaded file"
Pavlov Konstantin's avatar
Pavlov Konstantin committed
685
              " \"%s\". Thus, it was deleted."),
686 687
            psz_destfile );

688 689 690 691 692 693
        goto end;
    }

    if( p_hash[0] != sign.hash_verification[0] ||
        p_hash[1] != sign.hash_verification[1] )
    {
694
        vlc_unlink( psz_destfile );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
695
        dialog_FatalWait( p_udt, _("File corrupted"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
696
            _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
697 698 699
             psz_destfile );
        msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
        free( p_hash );
700 701 702
        goto end;
    }

703 704
    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
            != VLC_SUCCESS )
705
    {
706
        vlc_unlink( psz_destfile );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
707
        dialog_FatalWait( p_udt, _("File corrupted"),
Felix Paul Kühne's avatar
Felix Paul Kühne committed
708
            _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
709
             psz_destfile );
710 711 712 713 714 715 716 717
        msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
        free( p_hash );
        goto end;
    }

    msg_Info( p_udt, "%s authenticated", psz_destfile );
    free( p_hash );

718
#ifdef WIN32
719 720 721 722 723 724
    int answer = dialog_Question( p_udt, _("Update VLC media player"),
    _("The new version was successfully downloaded. Do you want to close VLC and install it now?"),
    _("Install"), _("Cancel"), NULL);

    if(answer == 1)
    {
725 726 727
        wchar_t psz_wdestfile[MAX_PATH];
        MultiByteToWideChar( CP_UTF8, 0, psz_destfile, -1, psz_wdestfile, MAX_PATH );
        answer = ShellExecuteW( NULL, L"open", psz_wdestfile, NULL, NULL, SW_SHOW);
728 729 730
        if(answer > 32)
            libvlc_Quit(p_this->p_libvlc);
    }
731
#endif
732
end:
733
    if( p_progress )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
734
        dialog_ProgressDestroy( p_progress );
735 736 737 738
    if( p_stream )
        stream_Delete( p_stream );
    if( p_file )
        fclose( p_file );
739
    free( psz_destdir );
740 741 742
    free( psz_destfile );
    free( p_buffer );
    free( psz_size );
743

dionoea's avatar
dionoea committed
744
    vlc_restorecancel( canc );
ivoire's avatar
ivoire committed
745
    return NULL;
746
}
747 748 749 750 751 752

update_release_t *update_GetRelease( update_t *p_update )
{
    return &p_update->release;
}

753
#else
754 755
#undef update_New
update_t *update_New( vlc_object_t *p_this )
756 757 758 759 760 761 762 763 764
{
    (void)p_this;
    return NULL;
}

void update_Delete( update_t *p_update )
{
    (void)p_update;
}
765

766 767 768 769 770 771 772 773 774 775 776 777
void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),
                   void *p_data )
{
    (void)p_update; (void)pf_callback; (void)p_data;
}

bool update_NeedUpgrade( update_t *p_update )
{
    (void)p_update;
    return false;
}

778
void update_Download( update_t *p_update, const char *psz_destdir )
779 780 781
{
    (void)p_update; (void)psz_destdir;
}
782 783 784 785 786 787

update_release_t *update_GetRelease( update_t *p_update )
{
    (void)p_update;
    return NULL;
}
788
#endif