summaryrefslogtreecommitdiffstats
path: root/src/update.cpp
blob: 0785526753be9706af11acbf2b689cf796c9d8b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/***************************************************************************
 * $Id: update.cpp,v 1.26 2008/12/08 19:39:02 hoganrobert Exp $
 *   Copyright (C) 2006 - 2008 Robert Hogan                                *
 *   [email protected]                                                *
 *                                                                         *
 *   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 version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#include "update.h"

#include <kstaticdeleter.h>

#include <klocale.h>
#include <kio/netaccess.h>


#include <kaction.h>

#include <qcheckbox.h>
#include <kbuttonbox.h>
#include <kurlcompletion.h>
#include <kcombobox.h>
#include <qlayout.h>
#include <kmessagebox.h>
#include <klineedit.h>
#include <ktempfile.h>
#include <ksystemtray.h>
#include <ktar.h>
#include <kprogress.h>
#include <kprocio.h>
#include <knotifyclient.h>
#include <dom/html_misc.h>
#include <kapplication.h>
#include <dcopclient.h>
#include <kuser.h>

#include <qtimer.h>
#include <qdir.h>
#include "version.h"

#include <stdlib.h>

const char *mirrors[] = {
    "heanet",
    "internap",
    "surfnet",
    "umn",
    "nchc",
    "ufpr",
    "unc",
    "jaist",
    "voxel",
    "citkit",
    "optusnet",
    "ovh",
    "mesh",
    "easynews",
    "switch",
    "kent",
    "puzzle",
    0
};

TorkUpdate::TorkUpdate(QWidget *parent, const char *name)
    : QWidget(parent, name),filelist(new KHTMLPart(this))
{

}


TorkUpdate::~TorkUpdate()
{
}


void TorkUpdate::downloadComponent(QString component, QString version, QString extension)
{

    upgradeinprogress = true;

    QString location = getenv("HOME");

    location += "/.tork";
    QDir torkdir(location);
    if (!torkdir.exists() && !torkdir.mkdir(location))
        location = getenv("HOME");

    QString currenttor = location + QString("/%1-%2").arg(component).arg(version);
    QDir tordir(currenttor);

    QString configure;
    configure = QString("%1/%2-%3/configure").arg(location).arg(component).arg(version);

    QFile configurefile(configure);

    bool download = true;

    if (torkdir.exists() && configurefile.exists()){
        int result = KMessageBox::questionYesNo(0, i18n( "You seem to have downloaded %1-%2 already (in %3/%4-%5). Would you like to skip re-downloading it and just try to compile it?").arg(component).arg(version).arg(location).arg(component).arg(version),i18n( "Compile %1" ).arg(component));

        switch (result) {
            case KMessageBox::Yes : 
                download = true;break;
            case KMessageBox::No :
                download = false;
        }
    }

    QStringList entry;
    while (download){

        startProgressDialog( i18n( "Downloading %1-%2..." ).arg(component).arg(version) );

        QString dlLocation;
        QString dlLocationSig;
        if (component == "tor"){
            dlLocation = QString("https://torproject.org/dist/%3").arg(highestsofarfilename);
            dlLocationSig = QString("https://torproject.org/dist/%3.asc").arg(highestsofarfilename);
        }else if (component == "privoxy")
            dlLocation = QString("http://%1.dl.sourceforge.net/sourceforge%3").arg(getMirror()).arg(highestsofarfilename);
        else if (component == "dante")
            dlLocation = QString("http://www.mirrors.wiretapped.net/security/firewalls/dante/%3").arg(highestsofarfilename);
        else if (component == "thttpd")
            dlLocation = QString("http://www.acme.com/software/thttpd/thttpd-2.25b.tar.gz");
        else if (component == "Mixminion")
            dlLocation = QString("http://www.mixminion.net/dist/0.0.8alpha3/Mixminion-0.0.8alpha3.tar.gz");
        else
            dlLocation = QString("http://%1.dl.sourceforge.net/sourceforge/%2/%3-%4%5").arg(getMirror()).arg(component).arg(component).arg(version).arg(extension);

        QString tmpFile;
        if ( !KIO::NetAccess::download( dlLocation, tmpFile, 0L ) ){
            KMessageBox::information (0,i18n("Couldn't download %1.").arg(component));
            delete timer;
            timer=0;
            delete progressDialog;
            progressDialog = 0;
            upgradeinprogress = false;
            return;
        }

        QString tmpFile2;
        if (!dlLocationSig.isEmpty()){
            if ( !KIO::NetAccess::download( dlLocationSig, tmpFile2, 0L ) ){
                KMessageBox::information (0,i18n("Couldn't download %1 signature file.").arg(component));
                delete timer;
                timer=0;
                delete progressDialog;
                progressDialog = 0;
                upgradeinprogress = false;
                return;
            }
            int res = KMessageBox::questionYesNo(0, i18n( "<p>Before proceeding you should verify the source package we have just downloaded. You can copy and paste the commands below into a terminal session such as Konsole.<br>"
            "<br>"
            "<b>Step One</b> Import the keys used by the Tor developers to sign the Tor source code:<br>"
            "<b>gpg --keyserver subkeys.pgp.net --recv-keys 0x28988BF5</b><br>"
            "<b>gpg --keyserver subkeys.pgp.net --recv-keys 0x165733EA</b><br>"
            "<br>"
            "<b>Step Two</b> To verify the source package we have just downloaded:<br>"
            "<b>gpg --verify %1 %2</b><br>"
            "<br>"
            "For further info on what you should expect to see, visit: <b>http://wiki.noreply.org/noreply/TheOnionRouter/VerifyingSignatures</b>"
            "<br>"
            "<br> Are you happy that the source file is authentic?</p>").arg(tmpFile2).arg(tmpFile));
            switch (res) {
                case KMessageBox::Yes : 
                    break;
                case KMessageBox::No : 
                default:
                    updateCanceled();
                    delete progressDialog;
                    progressDialog = 0;
                    return;
                    return;
            }

        }
    
        if ((progressDialog) && (progressDialog->wasCancelled())){
            updateCanceled();
            return;
        }
    
            
    
        progressDialog->setLabel( i18n( "Unpacking %1-%2 to %3/%4-%5" ).arg(component).arg(version).arg(location).arg(component).arg(version));
    
        // unpack the tarball
        KTar tar( tmpFile );
        if (!(tar.open( IO_ReadOnly ))){
            int result = KMessageBox::questionYesNo(0, i18n( "The mirror I attempted to download from has not updated yet. Should I try another?"));
            switch (result) {
                case KMessageBox::Yes : 
                    download = true; break;
                case KMessageBox::No : 
                    download = false;
                default:
                    download = false;
            }
        }else{
            download = false;
        } 
        tar.directory()->copyTo( location );
        entry = tar.directory()->entries();
        tar.close();
        // remove the temp file
        //KIO::NetAccess::removeTempFile( tmpFile );
    
        delete timer;
        timer=0;
        delete progressDialog;
        progressDialog = 0;
    }

//     if ((progressDialog) && (progressDialog->wasCancelled())){
//         updateCanceled();
//         delete progressDialog;
//         progressDialog = 0;
//         return;
//     }

    //KTar does not honour executable permissions

    if (component == "privoxy"){
        configure = QString("%1/%2/").arg(location).arg(entry.first());
        configurefile.setName(configure+"configure.in");
    }

    if (component == "Mixminion"){
        configure = QString("%1/%2/").arg(location).arg(entry.first());
        configurefile.setName(configure+"README");
    }

    if (torkdir.exists() && configurefile.exists()){

        chmod((const char *)configure,0700);

        int result = KMessageBox::warningContinueCancel(0, i18n( "%1-%2 is ready for compiling and installation. Would you like the wizard to ask you for the root password so it can compile and install it for you? (If not, you can compile it yourself later at %3/%4-%5)").arg(component).arg(version).arg(location).arg(component).arg(version),i18n( "Install %1-%2" ).arg(component).arg(version),i18n( "Use the Wizard" ));
        
        switch (result) {
            case 2 : 
                KMessageBox::information (0,i18n("Installation of %1 Cancelled.").arg(component));
                upgradeinprogress = false;
                break;
            case 5 : 

                KProcess* arkollonproc = new KShellProcess();

                KMessageBox::information (this,i18n( "<p><b>If this the first time you've compiled software then here are a few useful tips:</b><br>"
                    "1. Any error messages in the log file with the words 'KDE', 'Qt','curl' or 'X' in them mean that you need to install "
                    "the appropriate development libraries.<br>"
                    "2. Any package provided by your distribution with 'lib' or 'devel' in the name is a development library, e.g. qt-devel, libkde.<br>"
                    "</p>" ),"Compilation Tips","compiler");

                 QString command;
                if (component == "Mixminion")
                    command = QString("kdesu -i tork -c 'xterm -hold -e \"cd %1;make install;printf \\\"If there were errors try downloading and installing Mixminion from the Mixminion homepage. If the installation looks successful try sending an anonymous mail to yourself. Close the window to continue.\\\"\"'").arg(configure);
                else if (component == "privoxy")
                    command = QString("cd %1; autoheader;autoconf;torkarkollon %2").arg(configure).arg(configure);
                else
                    command = QString("torkarkollon %1/%2-%3").arg(location).arg(component).arg(version);

                *arkollonproc << command;

                connect( arkollonproc, SIGNAL(processExited(KProcess *)), SLOT(torkInstallationExited(KProcess *)) );
                arkollonproc->start();
        }
    }
    
}

void TorkUpdate::downloadThttpd()
{
/*    startProgressDialog( i18n( "Downloading thttpd..." ) );*/
    downloadComponent("thttpd", "2.25b", "tar.gz");
}

void TorkUpdate::downloadMixminion()
{
/*    startProgressDialog( i18n( "Downloading thttpd..." ) );*/
    downloadComponent("Mixminion", "0.0.8alpha3", "tar.gz");
}

void TorkUpdate::checkForNewTork()
{

    highestsofarnumeric = 0;
    tork_url = "http://prdownloads.sourceforge.net/tork/";
    getLatestVersionFromSFHack(tork_url);

}

void TorkUpdate::checkForNewPrivoxyDirectly()
{

    checkingDirectly = true;

    highestsofarnumeric = 0;
    tork_url = "http://prdownloads.sourceforge.net/ijbswa/";
    startProgressDialog( i18n( "Checking for new version of Privoxy..." ) );

    getLatestVersionFromSF(tork_url);

}

void TorkUpdate::checkForNewTorkDirectly()
{

    checkingDirectly = true;

    highestsofarnumeric = 0;
    tork_url = "http://downloadtork.anonymityanywhere.com/";
    startProgressDialog( i18n( "Checking for new version of Tork..." ) );

    getLatestVersionFromSF(tork_url);

}


void TorkUpdate::checkForNewTorDirectly(bool alpha)
{

    checkingDirectly = true;
    m_alpha=alpha;
    highestsofarnumeric = 0;
    tork_url = "http://torproject.org/dist/";

    startProgressDialog( i18n( "Checking for new version of Tor..." ) );
    
    getLatestVersionFromSF(tork_url);

}

void TorkUpdate::checkForNewDanteDirectly()
{

    checkingDirectly = true;

    highestsofarnumeric = 0;
    tork_url = "http://www.mirrors.wiretapped.net/security/firewalls/dante/";

    startProgressDialog( i18n( "Checking for new version of Dante..." ) );
    
    getLatestVersionFromSF(tork_url);

}

void TorkUpdate::completedSearchForUpdates(const QString &component, const QString &extension)
{

    double currentversion;


    if (component == "Tork")
        currentversion = VERSION_TORK;
    else{
        if (currentTorVersion.mid(currentTorVersion.length() - 2,1) == ".")
            currentversion = currentTorVersion.replace(currentTorVersion.length() - 2,1,"").toDouble();
        else
            currentversion = currentTorVersion.toDouble();
    }

    if (highestsofarnumeric > currentversion){
        int result = KMessageBox::warningContinueCancel(0, i18n( "The newest version of %1 available is  %2-%3. Would you like Tork to download and compile it for you?").arg(component).arg(component).arg(highestsofarraw),i18n( "Download and Install %1-%2" ).arg(component).arg(highestsofarraw),i18n( "Download and Install %1-%2" ).arg(component).arg(highestsofarraw));
    
        switch (result) {
            case 2 : 
                break;
            case 5 : 
                downloadComponent(component.lower(), highestsofarraw, extension);
        }
    }else if (checkingDirectly){
        KMessageBox::information (this,i18n("Your installation of %1 is already up-to-date!").arg(component));
    }
    checkingDirectly = false;
    m_alpha=false;
//     tork_options->setEnabled(true);
//     tork_options->setEnabled(true);

//     if (filelist)
//         filelist->deleteLater();

}



void TorkUpdate::startProgressDialog( const QString & text )
{


    progressDialog = new KProgressDialog( 0, "progress_dialog", QString::null, text, false );

    progressDialog->setAllowCancel( true );
    progressDialog->showCancelButton( true );
    progressDialog->setPlainCaption( i18n( "Please Wait" ) );

    progressDialog->progressBar()->setTotalSteps( 0 );
    progressDialog->progressBar()->setPercentageVisible( false );

    progressDialog->setMinimumDuration( 500 );
    progressDialog->show();

    connect( progressDialog, SIGNAL( cancelClicked() ), this,
		SLOT( updateCanceled() ) );

    timer = new QTimer( this );
    connect( timer, SIGNAL( timeout() ), this, SLOT( slotProg() ) );

    timer->start( 200, FALSE );
}

void TorkUpdate::slotProg()
{

    if (progressDialog)
        progressDialog->progressBar()->setProgress(progressDialog->progressBar()->progress() + 4 );
}


void TorkUpdate::torkInstallationExited(KProcess* arkollonproc)
{

    arkollonproc = 0;
    delete arkollonproc;
    upgradeinprogress = false;

    KMessageBox::information (this,i18n( "If the installation completed successfully you should restart the component for the new version to take effect." ));

}

void
TorkUpdate::checkInternet() //SLOT
{
    m_url.setHost( "prdownloads.sourceforge.net" ); 
    if ( !m_url.port() ) m_url.setPort( 80 );

    connect( &m_resolver,   SIGNAL( finished( KResolverResults ) ), SLOT( resolved( KResolverResults ) ) );
    connectToHost();
}

void
TorkUpdate::connectToHost() //SLOT
{
        m_resolver.setNodeName( m_url.host() );
        m_resolver.setFamily( KResolver::InetFamily );
        m_resolver.start();
}


void
TorkUpdate::resolved( KResolverResults result) // SLOT
{
    if ((!( result.error() != KResolver::NoError || result.isEmpty() )) 
       /*&& tork_box->isChecked()*/)
        checkForNewTork();
    else
        kdDebug() << "network error or not set to check for new tork" << endl;
}



void TorkUpdate::getLatestVersionFromSF(KURL url)
{

    connect( filelist, SIGNAL( completed() ), this,
		SLOT( parseSFPage() ) );

    filelist->setStatusMessagesEnabled(false);
    filelist->openURL(url);

}

void TorkUpdate::getLatestVersionFromSFHack(KURL url)
{


    if (!filelist)
        filelist = new KHTMLPart();

    connect( filelist, SIGNAL( completed() ), this,
		SLOT( parseSFPageHack() ) );

    filelist->setStatusMessagesEnabled(false);
    filelist->openURL(url);

}



void TorkUpdate::updateCanceled()
{

    filelist->closeURL();
//     tork_options->setEnabled(true);
//     tork_options->setEnabled(true);

}

void TorkUpdate::parseSFPage()
{

    const DOM::HTMLCollection links = filelist->htmlDocument().links();

    if (links.length() == 0){
            KMessageBox::information (this,i18n( "Could not contact update server!" ));
            updateCanceled();
            delete progressDialog;
            progressDialog = 0;
            return;
    }
    
    disconnect( filelist, SIGNAL( completed() ), this,
	SLOT( parseSFPage() ) );

    for (unsigned int j=0; j != links.length(); j++ ){
        const DOM::Node linkNode = links.item( j );
        getVersionFromLink( linkNode );
    }

    delete progressDialog;
    progressDialog = 0;
    
    if (highestsofarfilename.contains("tork"))
        completedSearchForUpdates("Tork", ".tar.bz2");
    else if (highestsofarfilename.contains("tor"))
            completedSearchForUpdates("Tor", ".tar.gz");
    else if (highestsofarfilename.contains("privoxy"))
            completedSearchForUpdates("Privoxy", ".tar.gz");
    else if (highestsofarfilename.contains("dante"))
            completedSearchForUpdates("Dante", ".tar.gz");


}

void TorkUpdate::parseSFPageHack()
{

    const DOM::HTMLCollection links = filelist->htmlDocument().links();

    if (links.length() == 0){
            return;
    }
    
    disconnect( filelist, SIGNAL( completed() ), this,
	SLOT( parseSFPageHack() ) );

    for (unsigned int j=0; j != links.length(); j++ ){
        const DOM::Node linkNode = links.item( j );
        getVersionFromLink( linkNode );
    }

    //delete progressDialog;
    //progressDialog = 0;

    if (highestsofarfilename.contains("tork"))
        completedSearchForUpdates("Tork", "tar.bz2");
    else if (highestsofarfilename.contains("tor"))
        completedSearchForUpdates("Tor", "tar.gz");


}


void TorkUpdate::getVersionFromLink( const DOM::Node &n )
{
        double numericversion;
        QString tmpversion;

        if ( n.isNull() || n.nodeType() != DOM::Node::ELEMENT_NODE )
            return;
        
        DOM::Element elem = static_cast<DOM::Element>( n );
        
        KURL href ( elem.getAttribute( "href" ).string() );
        
        QString name = elem.getAttribute( "href" ).string();
        QString version = name.replace(".tar.bz2","").replace(".tar.gz","");
        version = version.replace("tork-","").replace("tor-","")
                          .replace("/ijbswa/privoxy-","")
                          .replace("-stable-src","")
                          .replace("dante-","");
        
        QString rawversion = version;
        if (m_alpha) {
          version.remove("-alpha").remove("-beta").remove("-rc");
        }
        numericversion = numericizeVersion(version);

        if (numericversion > highestsofarnumeric){
            highestsofarnumeric = numericversion;
            highestsofarraw = rawversion;
            highestsofarfilename = elem.getAttribute( "href" ).string();
        }


}

QString TorkUpdate::getMirror()
{
    int r = 1+(int) (16.0*rand()/(RAND_MAX+1.0));
    static int previousmirror = 0;

    while (r == previousmirror)
        r=1+(int) (16.0*rand()/(RAND_MAX+1.0));

    previousmirror = r;

    return mirrors[r];
}

double TorkUpdate::numericizeVersion(QString &version)
{
        QString tmpversion = version;
        QStringList tokens = QStringList::split(".",tmpversion);
        for ( QStringList::Iterator it = tokens.begin(); it != tokens.end(); it++ )
        {
            if ((*it).length() < 2)
                (*it).prepend("0");
        }
        tmpversion = tokens.join(".");

        unsigned int firstdot = tmpversion.find(".");
        for (unsigned int j=tmpversion.length(); j != firstdot; j-- ){
            if (tmpversion.mid(j,1) == "."){
                tmpversion.remove(j,1);
                j--;
            }
        }

        return tmpversion.toDouble();
}
#include "update.moc"

// forward-socks4a / localhost:9050 .