~ubuntu-branches/ubuntu/oneiric/knetworkmanager/oneiric

« back to all changes in this revision

Viewing changes to knetworkmanager/src/knetworkmanager-vpn.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-07-14 14:05:44 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20080714140544-yjhxgrdwartk3kx7
Tags: 1:0.7svn830754-0ubuntu1
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *
3
 
 * knetworkmanager-vpn.cpp - A NetworkManager frontend for KDE 
4
 
 *
5
 
 * Copyright (C) 2006 Novell, Inc.
6
 
 *
7
 
 * Author: Timo Hoenig <thoenig@suse.de>, <thoenig@nouse.net>
8
 
 *         Helmut Schaa <hschaa@suse.de>, <helmut.schaa@gmx.de>
9
 
 *
10
 
 * This program is free software; you can redistribute it and/or modify
11
 
 * it under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation; either version 2 of the License, or
13
 
 * (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 * GNU General Public License for more details.
19
 
 * 
20
 
 * You should have received a copy of the GNU General Public License
21
 
 * along with this program; if not, write to the Free Software
22
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 
 *
24
 
 **************************************************************************/
25
 
 
26
 
#define SERVICE_DIR "/etc/NetworkManager/VPN"
27
 
#define GCONF_PRE   "/.gconf"
28
 
#define VPN_PATH    "/system/networking/vpn_connections"
29
 
 
30
 
#include <stdlib.h>
31
 
#include <kconfig.h>
32
 
#include <qdom.h> 
33
 
#include <qdir.h>
34
 
#include <klocale.h>
35
 
#include <kdebug.h>
36
 
#include <kstddirs.h>
37
 
#include <kprocess.h>
38
 
#include <kconfig.h>
39
 
#include <kplugininfo.h>
40
 
 
41
 
#include "knetworkmanager-vpn.h"
42
 
#include "knetworkmanager-vpn_dbus.h"
43
 
#include "knetworkmanager-storage.h"
44
 
#include "knetworkmanager-vpnplugin.h"
45
 
#include "knetworkmanager-vpnconnectionsdialog.h"
46
 
#include "knetworkmanager-vpnauthenticationdialog.h"
47
 
 
48
 
/*
49
 
 * class VPNConnection
50
 
 *
51
 
 */
52
 
void
53
 
VPNConnection::setConfigGroup(const QString& cfgGrp)
54
 
{
55
 
        _cfgGrp   = new KConfigGroup(KGlobal::config(), cfgGrp);
56
 
        _readonly = false;
57
 
}
58
 
 
59
 
QString
60
 
VPNConnection::getName () const
61
 
{
62
 
        return _name;
63
 
}
64
 
 
65
 
void
66
 
VPNConnection::setName (const QString & name)
67
 
{
68
 
        _name = name;
69
 
}
70
 
 
71
 
VPNService*
72
 
VPNConnection::getVPNService() const
73
 
{
74
 
        return _vpnService;
75
 
}
76
 
 
77
 
QString
78
 
VPNConnection::getService () const
79
 
{
80
 
        if (_vpnService)
81
 
                return _vpnService->getService();
82
 
        else
83
 
                return QString::null;
84
 
}
85
 
 
86
 
QString
87
 
VPNConnection::getServiceName () const
88
 
{
89
 
        if (_vpnService)
90
 
                return _vpnService->getName();
91
 
        else
92
 
                return QString::null;
93
 
}
94
 
 
95
 
void
96
 
VPNConnection::setServiceName(const QString& service)
97
 
{
98
 
        _vpnService  = _vpn->getVPNService(service);
99
 
}
100
 
 
101
 
QString
102
 
VPNConnection::getUser () const
103
 
{
104
 
        return _user;
105
 
}
106
 
 
107
 
void
108
 
VPNConnection::setUser (const QString & user)
109
 
{
110
 
        _user = user;
111
 
}
112
 
 
113
 
QStringList
114
 
VPNConnection::getRoutes () const
115
 
{
116
 
        return _routes;
117
 
}
118
 
 
119
 
void
120
 
VPNConnection::addRoute (const QString & route)
121
 
{
122
 
        _routes.append (route);
123
 
}
124
 
 
125
 
void
126
 
VPNConnection::setRoutes(const QStringList& routes)
127
 
{
128
 
        _routes = routes;
129
 
}
130
 
 
131
 
QStringList
132
 
VPNConnection::getData () const
133
 
{
134
 
        return _data;
135
 
}
136
 
 
137
 
void
138
 
VPNConnection::addData (const QString & data)
139
 
{
140
 
        _data.append (data);
141
 
}
142
 
 
143
 
void
144
 
VPNConnection::setData(const QStringList & data)
145
 
{
146
 
        // overwrite all data
147
 
        _data = data;
148
 
}
149
 
 
150
 
QStringList
151
 
VPNConnection::getPasswords () const
152
 
{
153
 
        return _passwords;
154
 
}
155
 
 
156
 
void
157
 
VPNConnection::addPasswords (const QStringList & passwords, bool storePasswordsPermanent, bool storePasswordsSession)
158
 
{
159
 
        _passwords = passwords;
160
 
        if (_storePasswordsPermanent = storePasswordsPermanent)
161
 
        {
162
 
                // store passwords in KWallet
163
 
                KNetworkManagerStorage* storage = KNetworkManagerStorage::getInstance();
164
 
                QMap<QString, QString> map;
165
 
                map.insert("passwords", _passwords.join(" "));
166
 
                storage->storeCredentials(_name, map);
167
 
        }
168
 
        // should the pwd stored for this session?
169
 
        _storePasswordsSession = storePasswordsSession;
170
 
}
171
 
 
172
 
NMVPNActStage
173
 
VPNConnection::getActivationStage () const
174
 
{
175
 
        return _activationStage;
176
 
}
177
 
 
178
 
void
179
 
VPNConnection::setActivationStage (NMVPNActStage activationStage)
180
 
{
181
 
        _activationStage = activationStage;
182
 
        emit activationStateChanged ();
183
 
}
184
 
 
185
 
void
186
 
VPNConnection::setVPNConnectionFailure(QString& member, QString& err_msg)
187
 
{
188
 
        emit connectionFailure(member, err_msg);
189
 
}
190
 
 
191
 
QString
192
 
VPNConnection::getAuthHelper () const
193
 
{
194
 
        return _vpnService->getAuthHelper();
195
 
}
196
 
 
197
 
void
198
 
VPNConnection::setAuthHelper (const QString & authHelper)
199
 
{
200
 
        _authHelper = authHelper;
201
 
}
202
 
 
203
 
bool
204
 
VPNConnection::isValid ()
205
 
{
206
 
        bool status = true;
207
 
        
208
 
        /* _routes is allowed to be empty for some VPN service */
209
 
        if (_name == QString::null || _user == QString::null || _data.empty () || _vpnService == NULL)
210
 
                status = false;
211
 
 
212
 
        return status;
213
 
}
214
 
 
215
 
bool
216
 
VPNConnection::isReadonly()
217
 
{
218
 
        return _readonly;
219
 
}
220
 
 
221
 
bool
222
 
VPNConnection::isConfirmedByNM()
223
 
{
224
 
        return _confirmedByNM;
225
 
}
226
 
 
227
 
void
228
 
VPNConnection::setConfirmedByNM(bool confirmed)
229
 
{
230
 
        _confirmedByNM = confirmed;
231
 
}
232
 
 
233
 
VPNConnection::VPNConnection (VPN* parent, const char * name ) : QObject( parent, name )
234
 
{
235
 
        _vpn                    = parent;
236
 
        _name                   = QString::null;
237
 
        _user                   = getenv ("USER");
238
 
        _authHelper             = QString::null;
239
 
        _activationStage        = NM_VPN_ACT_STAGE_UNKNOWN;
240
 
        _dirty                  = true;
241
 
        _deleted                = false;
242
 
        _vpnService             = NULL;
243
 
        _storePasswordsPermanent = false;
244
 
        _storePasswordsSession = false;
245
 
        _cfgGrp                 = NULL;
246
 
        _readonly               = true;
247
 
        _confirmedByNM    = false;
248
 
}
249
 
 
250
 
VPNConnection::VPNConnection (const QString& cfgGrp, VPN* parent, const char * name ) : QObject( parent, name )
251
 
{
252
 
        _vpn                    = parent;
253
 
        _name                   = QString::null;
254
 
        _user                   = getenv ("USER");
255
 
        _authHelper             = QString::null;
256
 
        _activationStage        = NM_VPN_ACT_STAGE_UNKNOWN;
257
 
        _dirty                  = true;
258
 
        _deleted                = false;
259
 
        _vpnService             = NULL;
260
 
        _storePasswordsPermanent = false;
261
 
        _storePasswordsSession = false;
262
 
        _readonly               = false;
263
 
        _confirmedByNM    = false;
264
 
 
265
 
        // our ConfigGroup
266
 
        _cfgGrp                 = new KConfigGroup(KGlobal::config(), cfgGrp);
267
 
 
268
 
        // read attribs from config
269
 
        _name                   = _cfgGrp->readEntry("name");
270
 
        _vpnService             = _vpn->getVPNService(_cfgGrp->readEntry("service_name"));
271
 
        _routes                 = _cfgGrp->readPropertyEntry("routes", QVariant::StringList).toStringList();
272
 
        _data                   = _cfgGrp->readPropertyEntry("vpn_data", QVariant::StringList).toStringList();
273
 
}
274
 
 
275
 
 
276
 
VPNConnection::~VPNConnection ()
277
 
{
278
 
        this->save();
279
 
        if (_cfgGrp)
280
 
                delete _cfgGrp;
281
 
}
282
 
 
283
 
bool VPNConnection::save()
284
 
{
285
 
        if(!_cfgGrp)
286
 
                return false;
287
 
 
288
 
        if (_dirty && this->isValid())
289
 
        {
290
 
                // write attribs to configfile
291
 
                _cfgGrp->writeEntry("name", _name);
292
 
                if (_vpnService)
293
 
                        _cfgGrp->writeEntry("service_name", _vpnService->getName());
294
 
                _cfgGrp->writeEntry("routes", _routes);
295
 
                _cfgGrp->writeEntry("vpn_data", _data);
296
 
                //_cfgGrp->sync();
297
 
        }
298
 
        if (_deleted)
299
 
        {
300
 
                _cfgGrp->deleteGroup();
301
 
                //_cfgGrp->sync();
302
 
        }
303
 
        return true;
304
 
}
305
 
 
306
 
void VPNConnection::remove()
307
 
{
308
 
        _deleted = true;
309
 
}
310
 
 
311
 
bool VPNConnection::hasPasswordsStored()
312
 
{
313
 
        // do we have some passwords in kwallet?
314
 
        KNetworkManagerStorage* storage = KNetworkManagerStorage::getInstance();
315
 
        _storePasswordsPermanent = storage->hasCredentialsStored(_name);
316
 
 
317
 
        // check if we have the passwords already
318
 
        return _storePasswordsPermanent || _storePasswordsSession;
319
 
}
320
 
 
321
 
void
322
 
VPNConnection::slotCredentialsLoaded (QString /*id*/, QMap<QString, QString> map, bool canceled)
323
 
{
324
 
        // credentials got loaded
325
 
        if ( !map.isEmpty() )
326
 
        {
327
 
                _passwords = QStringList::split(" ", map["passwords"], TRUE);
328
 
                _storePasswordsPermanent = true;
329
 
        }
330
 
        emit passwordsRestored(canceled, _passwords, _storePasswordsPermanent, _storePasswordsSession);
331
 
}
332
 
 
333
 
void
334
 
VPNConnection::restorePasswords()
335
 
{
336
 
        if (_storePasswordsSession && !_passwords.empty())
337
 
        {
338
 
                // we have the passwords here -> reply
339
 
                emit passwordsRestored(false, _passwords, _storePasswordsPermanent, _storePasswordsSession);
340
 
        }
341
 
        else
342
 
        {
343
 
                // no passwords here -> ask the wallet and wait for answer
344
 
                CredentialsRequest* req = KNetworkManagerStorage::getInstance()->credentialsAsync(_name);
345
 
                connect(req, SIGNAL(credentialsLoaded(QString, QMap<QString, QString>, bool)), this, SLOT(slotCredentialsLoaded (QString, QMap<QString, QString>, bool)));
346
 
                req->loadCredentials();
347
 
        }
348
 
}
349
 
 
350
 
VPNConnection::VPNConnection (const VPNConnection & vpnConnection) : QObject( vpnConnection.parent(), vpnConnection.name())
351
 
{
352
 
        _name = vpnConnection.getName ();
353
 
        _user = vpnConnection.getUser();
354
 
        _routes = vpnConnection.getRoutes ();
355
 
        _data = vpnConnection.getData ();
356
 
        _passwords = vpnConnection.getPasswords ();
357
 
        _activationStage = vpnConnection.getActivationStage ();
358
 
        _authHelper = vpnConnection.getAuthHelper ();
359
 
}
360
 
 
361
 
void
362
 
VPNConnection::receiveAuthenticationData (KProcess* /* authHelper */, char* buffer, int len)
363
 
{
364
 
        QStringList passwords = QStringList::split (QString ("\n"), QString::fromLatin1 (buffer, len), false);
365
 
        this->addPasswords (passwords);
366
 
}
367
 
 
368
 
void
369
 
VPNConnection::authHelperExited (KProcess* authHelper)
370
 
{
371
 
        if (authHelper->exitStatus() != 0)
372
 
                this->setActivationStage(NM_VPN_ACT_STAGE_CANCELED);
373
 
        else
374
 
                VPNDBus::activateVPNConnection (this);
375
 
}
376
 
 
377
 
void
378
 
VPNConnection::authHelperExited (bool cancel, QStringList& passwords)
379
 
{
380
 
        this->authHelperExited(cancel, passwords, false, false);
381
 
}
382
 
 
383
 
void
384
 
VPNConnection::authHelperExited (bool cancel, QStringList& passwords, bool storePasswordsPermanent, bool storePasswordsSession)
385
 
{
386
 
        if (cancel)
387
 
                // lets fake a CANCELD Message, so the Statuswindow gets closed
388
 
                this->setActivationStage(NM_VPN_ACT_STAGE_CANCELED);
389
 
        else
390
 
        {
391
 
                this->addPasswords(passwords, storePasswordsPermanent, storePasswordsSession);
392
 
                VPNDBus::activateVPNConnection ( this );
393
 
        }
394
 
}
395
 
 
396
 
/*
397
 
 *  class VPNService
398
 
 *
399
 
 */
400
 
 
401
 
VPNService::VPNService(const QString& serviceName, const QString& service, VPN* parent, const char* name)
402
 
        : QObject(parent, name)
403
 
{
404
 
        _name = serviceName;
405
 
        _service = service;
406
 
        _vpn = parent;
407
 
        _vpnPlugin = NULL;
408
 
        _useInternalAuthentication = false;
409
 
 
410
 
        // query if a plugin for this vpn service is available
411
 
        PluginManager* plugMan = _vpn->getCtx()->getPluginManager();
412
 
        if (plugMan)
413
 
        {
414
 
                QStringList list = plugMan->getPluginList("KNetworkManager/VPNPlugin", "X-NetworkManager-Services", serviceName);
415
 
                if (list.size() > 0)
416
 
                {
417
 
                        // get the first VPN Plugin     handling our VPNService
418
 
                        VPNPlugin* vpnPlugin = dynamic_cast<VPNPlugin*>( plugMan->getPlugin(list.first()) );
419
 
                        if (vpnPlugin)
420
 
                        {
421
 
                                kdDebug() << k_funcinfo << i18n("Using VPN plugin '%1' for service '%2'").arg(list.first()).arg(serviceName) << endl;
422
 
                                _vpnPlugin = vpnPlugin;
423
 
                                _useInternalAuthentication = true;
424
 
                        }
425
 
                }
426
 
        }
427
 
 
428
 
        // do we have a authentication dialog for this service?
429
 
        if (!_useInternalAuthentication)
430
 
        {
431
 
                // we do not have an own auth dialog -> try to get the gnome dialog
432
 
                _extAuthDialog = readAuthHelper();
433
 
                kdDebug() << i18n("VPN: service %1 uses external authentication dialog (%2)").arg(serviceName).arg(_extAuthDialog) << endl;
434
 
        }
435
 
}
436
 
 
437
 
VPNService::~VPNService()
438
 
{
439
 
 
440
 
}
441
 
 
442
 
QString VPNService::getIcon()
443
 
{
444
 
        if (!_vpnPlugin.isNull())
445
 
        {
446
 
                PluginManager* plugMan = _vpn->getCtx()->getPluginManager();
447
 
                if (plugMan)
448
 
                {
449
 
                        const KPluginInfo* info = plugMan->getPluginInfo(_vpnPlugin);
450
 
                        if (info)
451
 
                        {
452
 
                                QString icon = info->icon();
453
 
                                if (!icon.isEmpty())
454
 
                                        return icon;
455
 
                        }
456
 
                }
457
 
        }
458
 
        return "encrypted";
459
 
}
460
 
 
461
 
VPNPlugin* VPNService::getVPNPlugin()
462
 
{
463
 
        return _vpnPlugin;
464
 
}
465
 
 
466
 
QString VPNService::getService() const
467
 
{
468
 
        return _service;
469
 
}
470
 
 
471
 
QString VPNService::getDisplayName() const
472
 
{
473
 
        const KPluginInfo* info = NULL;
474
 
        PluginManager* plugMan = _vpn->getCtx()->getPluginManager();
475
 
        if (_vpnPlugin && plugMan)
476
 
                if ( (info = plugMan->getPluginInfo(_vpnPlugin)) )
477
 
                        if (!info->name().isEmpty())
478
 
                                return info->name();
479
 
        return _name;
480
 
}
481
 
 
482
 
QString VPNService::getName() const
483
 
{
484
 
        return _name;
485
 
}
486
 
 
487
 
bool VPNService::hasInternalAuthentication()
488
 
{
489
 
        return _useInternalAuthentication;
490
 
}
491
 
 
492
 
QString VPNService::getAuthHelper()
493
 
{
494
 
        return _extAuthDialog;
495
 
}
496
 
 
497
 
QString VPNService::readAuthHelper()
498
 
{
499
 
        // get the auth helper
500
 
        QDir serviceDir(SERVICE_DIR, QString::null, QDir::Name|QDir::IgnoreCase, QDir::Files);
501
 
        QStringList services = serviceDir.entryList ().grep (".name", true);
502
 
        QString retVal = QString::null;
503
 
 
504
 
        for (QStringList::Iterator i = services.begin (); i != services.end (); ++i) {
505
 
                QString service = SERVICE_DIR + QString ("/") + *i;
506
 
                KConfig kconfig (service, true, true, "config");
507
 
                kconfig.setGroup ("VPN Connection");
508
 
                if (kconfig.readEntry ("service", QString::null) == _service) {
509
 
                        kconfig.setGroup ("GNOME");
510
 
                        QString authDialog = kconfig.readEntry ("auth-dialog", QString::null);
511
 
                        if ( !authDialog.isEmpty() ) {
512
 
                                retVal = authDialog;
513
 
                                break;
514
 
                        } else {
515
 
                                printf ("Warning: No authentication helper for service \"%s\" found.\n", service.ascii ());
516
 
                        }
517
 
                }
518
 
        }
519
 
        return retVal;
520
 
}
521
 
 
522
 
/*
523
 
 *  class VPN
524
 
 *
525
 
 */
526
 
 
527
 
bool
528
 
VPN::hasGnomeVPNConfig()
529
 
{
530
 
        return !_gnomeVPNApplet.isEmpty();
531
 
}
532
 
 
533
 
void
534
 
VPN::startGnomeVPNConfig()
535
 
{
536
 
        if (!_gnomeVPNApplet.isEmpty())
537
 
        {
538
 
                KProcess* configureVPNHelper = new KProcess ();
539
 
 
540
 
                *configureVPNHelper << _gnomeVPNApplet;
541
 
 
542
 
                // trigger the vpn module to reread the available connections 
543
 
                connect (configureVPNHelper, SIGNAL (processExited      (KProcess*)),
544
 
                         this,               SLOT   (updateVPNConnections(KProcess*)));
545
 
 
546
 
                configureVPNHelper->start (KProcess::NotifyOnExit);
547
 
        }
548
 
}
549
 
 
550
 
void
551
 
VPN::vpnActivationStateChanged()
552
 
{
553
 
        // look if at least one VPN Connection is active
554
 
        for (VPNList::ConstIterator it = _vpnList->begin(); it != _vpnList->end(); ++it)
555
 
        {
556
 
                if ((*it)->getActivationStage() == NM_VPN_ACT_STAGE_ACTIVATED)
557
 
                {
558
 
                        emit vpnConnectionStateChanged(true);
559
 
                        return;
560
 
                }
561
 
        }
562
 
 
563
 
        emit vpnConnectionStateChanged(false);
564
 
}
565
 
 
566
 
void
567
 
VPN::activateVPNConnection (VPNConnection* vpnConnection)
568
 
{
569
 
        if (!vpnConnection) return;
570
 
 
571
 
        if (vpnConnection->getVPNService()->hasInternalAuthentication())
572
 
        {
573
 
                // lets ask the plugin whether we have to show the widget or not
574
 
                bool waitForPwds = true;
575
 
                VPNPlugin* plugin = vpnConnection->getVPNService()->getVPNPlugin();
576
 
                if (plugin)
577
 
                {       
578
 
                        VPNAuthenticationWidget* auth = plugin->CreateAuthenticationWidget();
579
 
                        if (auth)
580
 
                        {
581
 
                                auth->setVPNData(vpnConnection->getRoutes(), vpnConnection->getData());
582
 
                                waitForPwds = auth->needsUserInteraction();
583
 
                                if (!waitForPwds)
584
 
                                {
585
 
                                        QStringList helper = auth->getPasswords();
586
 
                                        vpnConnection->authHelperExited(false, helper, false, false);
587
 
                                }
588
 
                                delete auth;
589
 
                        }
590
 
                }
591
 
 
592
 
                if (waitForPwds)
593
 
                {
594
 
                        // does the connection has its passwords stored (in session or permanent)
595
 
                        if (vpnConnection->hasPasswordsStored())
596
 
                        {
597
 
        // disconnect every slot from the signal to avoid multiple calls of authHelperExited
598
 
                                disconnect(vpnConnection, SIGNAL(passwordsRestored(bool, QStringList&, bool, bool)), 0, 0);
599
 
        connect(vpnConnection, SIGNAL(passwordsRestored(bool, QStringList&, bool, bool)), vpnConnection, SLOT(authHelperExited(bool, QStringList&, bool, bool)));
600
 
                                vpnConnection->restorePasswords();
601
 
                        }
602
 
                        else
603
 
                        {
604
 
                                // ok, we have to show the auth dialog so the user can type in his pwds 
605
 
                                VPNAuthenticationDialog* dlg = new VPNAuthenticationDialog(vpnConnection);
606
 
                                connect(dlg, SIGNAL(done(bool, QStringList&, bool, bool)), vpnConnection, SLOT(authHelperExited(bool, QStringList&, bool, bool)));
607
 
                                dlg->show();
608
 
                        }
609
 
                }
610
 
        }
611
 
        else
612
 
        {
613
 
                // no internal authentication dialog -> use the external one
614
 
                printf("External AuthHelper: %s\n", vpnConnection->getAuthHelper().ascii());
615
 
                KProcess*             authHelper = new KProcess ();
616
 
 
617
 
                *authHelper << vpnConnection->getAuthHelper ()           \
618
 
                      << "-n" << vpnConnection->getName ()         \
619
 
                      << "-s" << vpnConnection->getService ()  \
620
 
                      << "-r";
621
 
 
622
 
                connect (authHelper, SIGNAL (receivedStdout             (KProcess*, char*, int)),
623
 
                         vpnConnection,    SLOT   (receiveAuthenticationData (KProcess*, char*, int)));
624
 
 
625
 
                connect (authHelper, SIGNAL (processExited    (KProcess*)),
626
 
                         vpnConnection,    SLOT   (authHelperExited (KProcess*)));
627
 
 
628
 
                authHelper->start (KProcess::NotifyOnExit, KProcess::Stdout);
629
 
        }
630
 
}
631
 
 
632
 
void
633
 
VPN::updateVPNConnections ()
634
 
{
635
 
        // notify NM about the changed connections
636
 
        for (VPNList::iterator i = _vpnList->begin (); i != _vpnList->end (); ++i) {
637
 
                VPNDBus::updateVPNConnection (*i);
638
 
        }
639
 
}
640
 
 
641
 
void
642
 
VPN::updateVPNConnections (KProcess* /* p */)
643
 
{
644
 
        for (VPNList::iterator i = _vpnList->begin (); i != _vpnList->end (); ++i) {
645
 
                delete (*i);
646
 
        }
647
 
        
648
 
        _vpnList->clear ();
649
 
        getConnections  ();
650
 
 
651
 
        this->updateVPNConnections();
652
 
}
653
 
 
654
 
void
655
 
VPN::disconnectVPNConnection (void)
656
 
{
657
 
        VPNDBus::disconnectVPNConnection ();
658
 
}
659
 
 
660
 
bool
661
 
VPN::isAvailable (void)
662
 
{
663
 
        return _available;
664
 
}
665
 
 
666
 
QString
667
 
VPN::getAuthHelper(const QString & serviceName)
668
 
{
669
 
        // get the auth helper
670
 
        QDir* serviceDir = new QDir (SERVICE_DIR, QString::null, QDir::Name|QDir::IgnoreCase, QDir::Files);
671
 
        QStringList services = serviceDir->entryList ().grep (".name", true);
672
 
 
673
 
        for (QStringList::Iterator i = services.begin (); i != services.end (); ++i) {
674
 
                QString service = SERVICE_DIR + QString ("/") + *i;
675
 
                KConfig* kconfig = new KConfig (service, true, true, "config");
676
 
                kconfig->setGroup ("VPN Connection");
677
 
                if (kconfig->readEntry ("service", QString::null) == serviceName) {
678
 
                        kconfig->setGroup ("GNOME");
679
 
                        QString authDialog = kconfig->readEntry ("auth-dialog", QString::null);
680
 
                        if ( !authDialog.isEmpty() ) {
681
 
                                return authDialog;
682
 
                        } else {
683
 
                                printf ("Warning: No authentication helper for service \"%s\" found.\n", service.ascii ());
684
 
                        }
685
 
                }
686
 
                delete kconfig;
687
 
        }
688
 
        return QString();
689
 
}
690
 
 
691
 
void
692
 
VPN::addConnection (const QString & cfgGrp)
693
 
{
694
 
        VPNConnection* vpnConnection = new VPNConnection ( cfgGrp, this, "vpnconnection");
695
 
 
696
 
        if (!vpnConnection->getName () || !vpnConnection->getServiceName ())
697
 
                return;
698
 
 
699
 
        if (vpnConnection->isValid ())
700
 
        {
701
 
                _vpnList->append (vpnConnection);
702
 
                connect(vpnConnection, SIGNAL(activationStateChanged()), this, SLOT(vpnActivationStateChanged()));
703
 
        }
704
 
        else
705
 
                delete vpnConnection;
706
 
}
707
 
 
708
 
bool
709
 
VPN::appendVPNConnection(VPNConnection* vpnConnection)
710
 
{
711
 
        if (vpnConnection->isValid())
712
 
        {
713
 
                _vpnList->append(vpnConnection);
714
 
                connect(vpnConnection, SIGNAL(activationStateChanged()), this, SLOT(vpnActivationStateChanged()));
715
 
                return true;
716
 
        }
717
 
        return false;
718
 
}
719
 
 
720
 
void
721
 
VPN::getGConfConnection (const QString & connection)
722
 
{
723
 
        VPNConnection* vpnConnection = new VPNConnection ( this, "vpnconnection" );
724
 
        QString        name;
725
 
        QString        serviceName;
726
 
        QStringList    routes;
727
 
        QStringList    data;
728
 
 
729
 
        QDomDocument doc ("vpnconnection");
730
 
        QFile file (_confPath + "/" + connection +  "/%gconf.xml");
731
 
        if (file.open (IO_ReadOnly) == false)
732
 
                return;
733
 
        if (doc.setContent (&file) == false) {
734
 
                file.close ();
735
 
                return;
736
 
        }
737
 
        file.close ();
738
 
 
739
 
        QDomElement docElem = doc.documentElement ();
740
 
        QDomNode n = docElem.firstChild ();
741
 
        while (n.isNull () == false) {
742
 
                QDomElement e = n.toElement ();
743
 
                if (e.isNull () == false) {
744
 
                        if (e.hasAttribute ("name")) {
745
 
                                QString nameValue = e.attribute ("name", QString::null);
746
 
                                if (nameValue == "name") {
747
 
                                        vpnConnection->setName (e.text () );
748
 
                                } else if (nameValue == "service_name") {
749
 
                                        vpnConnection->setServiceName (e.text ());
750
 
                                } else if (nameValue == "routes") {
751
 
                                        QDomNode m = n.firstChild ();
752
 
                                        while (m.isNull () == false) {
753
 
                                                QDomElement f = m.toElement ();
754
 
                                                if (f.isNull () == false)
755
 
                                                        vpnConnection->addRoute (f.text ());
756
 
                                                m = m.nextSibling ();
757
 
                                        }
758
 
                                } else if (nameValue == "vpn_data") {
759
 
                                        QDomNode m = n.firstChild ();
760
 
                                        while (m.isNull () == false) {
761
 
                                                QDomElement f = m.toElement ();
762
 
                                                if (f.isNull () == false)
763
 
                                                        vpnConnection->addData (f.text ());
764
 
                                                m = m.nextSibling ();
765
 
                                        }
766
 
                                }
767
 
                        }
768
 
                }
769
 
                n = n.nextSibling ();
770
 
        }
771
 
 
772
 
        if (!vpnConnection->getName () || !vpnConnection->getServiceName () || vpnConnection->getService()==NULL)
773
 
        {
774
 
                delete vpnConnection;
775
 
                return;
776
 
        }
777
 
 
778
 
        // we want this conection only if we have none with the same name
779
 
        if (this->getVPNConnection(vpnConnection->getName()) != NULL)
780
 
        {
781
 
                delete vpnConnection;
782
 
                return;
783
 
        }
784
 
 
785
 
        QDir* serviceDir = new QDir (SERVICE_DIR, QString::null, QDir::Name|QDir::IgnoreCase, QDir::Files);
786
 
        QStringList services = serviceDir->entryList ().grep (".name", true);
787
 
 
788
 
        for (QStringList::Iterator i = services.begin (); i != services.end (); ++i) {
789
 
                QString service = SERVICE_DIR + QString ("/") + *i;
790
 
                KConfig* kconfig = new KConfig (service, true, true, "config");
791
 
                kconfig->setGroup ("VPN Connection");
792
 
                if (kconfig->readEntry ("service", QString::null) == vpnConnection->getServiceName ()) {
793
 
                        kconfig->setGroup ("GNOME");
794
 
                        QString helper = kconfig->readEntry ("auth-dialog", QString::null);
795
 
                        if ( !helper.isEmpty() ) {
796
 
                                vpnConnection->setAuthHelper (helper);
797
 
                        } else {
798
 
                                printf ("Warning: No authentication helper for service \"%s\" found.\n", vpnConnection->getServiceName ().ascii ());
799
 
                        }
800
 
                }
801
 
                delete kconfig;
802
 
        }
803
 
 
804
 
        if (vpnConnection->isValid ())
805
 
        {
806
 
                _vpnList->append (vpnConnection);
807
 
                connect(vpnConnection, SIGNAL(activationStateChanged()), this, SLOT(vpnActivationStateChanged()));
808
 
        }
809
 
        else
810
 
                delete vpnConnection;
811
 
}
812
 
 
813
 
QStringList
814
 
VPN::listGConfConnections()
815
 
{
816
 
        QStringList connections;
817
 
        QDir confDir(_confPath);
818
 
        confDir.setFilter (QDir::Dirs);
819
 
 
820
 
        QStringList entryList = confDir.entryList ();
821
 
        for (QStringList::Iterator it = entryList.begin (); it != entryList.end (); ++it) {
822
 
                if ((*it) != "." && (*it) != "..")
823
 
                        if (getVPNConnection(*it)       == NULL)
824
 
                                connections.append (*it);
825
 
        }
826
 
        return connections;
827
 
}
828
 
 
829
 
VPNConnection*
830
 
VPN::newVPNConnection ()
831
 
{
832
 
        KNetworkManagerStorage* storage = KNetworkManagerStorage::getInstance();
833
 
        QString group = storage->vpnConnectionNewGroup();
834
 
        return new VPNConnection(group, this, "vpnconnection");
835
 
}
836
 
 
837
 
bool
838
 
VPN::importVPNConnection(const QString& conn_name)
839
 
{
840
 
        VPNConnection* conn = getVPNConnection(conn_name);
841
 
        if (conn)
842
 
        {
843
 
                KNetworkManagerStorage* storage = KNetworkManagerStorage::getInstance();
844
 
                QString group = storage->vpnConnectionNewGroup();
845
 
                conn->setConfigGroup(group);
846
 
                conn->save();
847
 
                return true;
848
 
        }
849
 
        return false;
850
 
}
851
 
 
852
 
void
853
 
VPN::getConnections (void)
854
 
{
855
 
        KNetworkManagerStorage* storage = KNetworkManagerStorage::getInstance();
856
 
        QStringList connections = storage->vpnConnectionGroups();
857
 
        for(QStringList::Iterator it = connections.begin(); it != connections.end(); ++it)
858
 
        {
859
 
                addConnection (*it);
860
 
        }
861
 
 
862
 
        // import all gconf connections for which we do not have an plugin
863
 
        QDir confDir(_confPath);
864
 
        confDir.setFilter (QDir::Dirs);
865
 
 
866
 
        QStringList entryList = confDir.entryList ();
867
 
        for (QStringList::Iterator it = entryList.begin (); it != entryList.end (); ++it) {
868
 
                if ((*it) != "." && (*it) != "..")
869
 
                        getGConfConnection(*it);
870
 
        }
871
 
 
872
 
        emit vpnConnectionsUpdated();
873
 
}
874
 
 
875
 
void
876
 
VPN::receiveKeyringData (KProcess* /* keyringDaemon */, char* buffer, int len)
877
 
{
878
 
        QStringList env = QStringList::split (QString ("\n"), QString::fromLatin1 (buffer, len), false);
879
 
 
880
 
        for (QStringList::Iterator it = env.begin (); it != env.end (); ++it) {
881
 
                QString* item = &(*it);
882
 
                if (item->startsWith ("GNOME_KEYRING_SOCKET")) {
883
 
                        setenv ("GNOME_KEYRING_SOCKET", item->section ('=', 1, 1).ascii(), 1);
884
 
                }
885
 
        }
886
 
}
887
 
 
888
 
 
889
 
void
890
 
VPN::initKeyring ()
891
 
{
892
 
        QString keyringSocket = getenv ("GNOME_KEYRING_SOCKET");
893
 
 
894
 
        if (!keyringSocket) {
895
 
                KProcess* keyringDaemon = new KProcess ();
896
 
                *keyringDaemon << "gnome-keyring-daemon";
897
 
 
898
 
                connect (keyringDaemon, SIGNAL (receivedStdout     (KProcess*, char*, int)),
899
 
                         this,          SLOT   (receiveKeyringData (KProcess*, char*, int)));
900
 
                
901
 
                keyringDaemon->start (KProcess::NotifyOnExit, KProcess::Stdout);
902
 
        }
903
 
}
904
 
 
905
 
bool
906
 
VPN::getServices ()
907
 
{
908
 
        bool status = false;
909
 
        
910
 
        QDir serviceDir(SERVICE_DIR, QString::null, QDir::Name|QDir::IgnoreCase, QDir::Files);
911
 
        QStringList services = serviceDir.entryList ().grep (".name", true);
912
 
 
913
 
        if (services.count () > 0)
914
 
        {       
915
 
                status = true;
916
 
                // read in all available Services
917
 
                _vpnServiceList = new VPNServiceList();
918
 
                for (QStringList::Iterator i = services.begin (); i != services.end (); ++i) {
919
 
                        QString service = SERVICE_DIR + QString ("/") + *i;
920
 
                        KConfig* kconfig = new KConfig (service, true, true, "config");
921
 
                        kconfig->setGroup ("VPN Connection");
922
 
                        // create new VPNService Object
923
 
                        _vpnServiceList->push_back(new VPNService(kconfig->readEntry ("name", QString::null), kconfig->readEntry ("service", QString::null), this));
924
 
                        delete kconfig;
925
 
                }
926
 
        }
927
 
 
928
 
        return status;
929
 
}
930
 
 
931
 
VPNService*
932
 
VPN::getVPNService(const QString & name)
933
 
{
934
 
        for (VPNServiceList::Iterator i = _vpnServiceList->begin (); i != _vpnServiceList->end (); ++i) {
935
 
                if ((*i)->getName() == name || (*i)->getService() == name)
936
 
                        return (*i);
937
 
        }
938
 
        return NULL;
939
 
}
940
 
 
941
 
QStringList
942
 
VPN::getVPNServices ()
943
 
{
944
 
        QStringList retval;
945
 
        for (VPNServiceList::Iterator i = _vpnServiceList->begin (); i != _vpnServiceList->end (); ++i) {
946
 
                retval.push_back((*i)->getName());
947
 
        }
948
 
        return retval;
949
 
}
950
 
 
951
 
void
952
 
VPN::deleteVPNConnection (const QString & name)
953
 
{
954
 
        VPNConnection* vpnConnection = NULL;
955
 
        for (VPNList::iterator i = _vpnList->begin (); i != _vpnList->end (); ++i) {
956
 
                if ((*i)->getName () == name) {
957
 
                        // remove the configuration
958
 
                        vpnConnection = *i;
959
 
                        vpnConnection->remove();
960
 
                        // remove our reference
961
 
                        _vpnList->erase(i);
962
 
                        delete vpnConnection;
963
 
                        vpnConnection = NULL;
964
 
                        // no more to do
965
 
                        break;
966
 
                }
967
 
        }
968
 
}
969
 
 
970
 
VPNConnection*
971
 
VPN::getVPNConnection (const QString & name)
972
 
{
973
 
        VPNConnection* vpnConnection = NULL;
974
 
        for (VPNList::iterator i = _vpnList->begin (); i != _vpnList->end (); ++i) {
975
 
                if ((*i)->getName () == name) {
976
 
                        vpnConnection = *i;
977
 
                        goto out;
978
 
                }
979
 
        }
980
 
out:
981
 
        return vpnConnection;
982
 
}
983
 
 
984
 
bool
985
 
VPN::isActive (void)
986
 
{
987
 
        bool status = false;
988
 
        
989
 
        for (VPNList::iterator i = _vpnList->begin (); i != _vpnList->end (); ++i) {
990
 
                if ((*i)->getActivationStage () == NM_VPN_ACT_STAGE_ACTIVATED) {
991
 
                        status = true;
992
 
                        goto out;
993
 
                }
994
 
        }
995
 
 
996
 
out:
997
 
        return status;
998
 
}
999
 
 
1000
 
VPNList*
1001
 
VPN::getVPNList (void)
1002
 
{
1003
 
        return _vpnList;
1004
 
}
1005
 
 
1006
 
void
1007
 
VPN::push (KNetworkManager* ctx)
1008
 
{
1009
 
        _ctx = ctx;
1010
 
        VPNDBus::push (ctx);
1011
 
}
1012
 
 
1013
 
KNetworkManager* VPN::getCtx()
1014
 
{
1015
 
        return _ctx;
1016
 
}
1017
 
 
1018
 
VPN::VPN ( KNetworkManager* parent, const char * name )
1019
 
        : QObject( parent, name ), _vpnServiceList(NULL), _vpnList(NULL)
1020
 
{
1021
 
        _ctx = parent;
1022
 
        VPNDBus::push(_ctx);
1023
 
        _confPath  = QDir::homeDirPath() + GCONF_PRE + VPN_PATH;
1024
 
        _available = getServices();
1025
 
 
1026
 
        if (_available == true) {
1027
 
                initKeyring ();
1028
 
                _vpnList = new VPNList ();
1029
 
                getConnections ();
1030
 
        }
1031
 
        // find the gnome vpn configuration applet
1032
 
        _gnomeVPNApplet = KStandardDirs::findExe("nm-vpn-properties");
1033
 
}
1034
 
 
1035
 
VPN::~VPN ()
1036
 
{
1037
 
        // delete connectionslist
1038
 
        if (_vpnList) {
1039
 
                for (VPNList::iterator i = _vpnList->begin (); i != _vpnList->end (); ++i) {
1040
 
                        delete (*i);
1041
 
                }
1042
 
                _vpnList->clear ();
1043
 
                delete _vpnList;
1044
 
                _vpnList = NULL;
1045
 
        }
1046
 
 
1047
 
        if (_vpnServiceList) {
1048
 
                for (VPNServiceList::iterator i = _vpnServiceList->begin (); i != _vpnServiceList->end (); ++i) {
1049
 
                        delete (*i);
1050
 
                }
1051
 
                _vpnServiceList->clear ();
1052
 
                delete _vpnServiceList;
1053
 
                _vpnServiceList = NULL;
1054
 
        }
1055
 
}
1056
 
 
1057
 
#include "knetworkmanager-vpn.moc"