• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
authinfo.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2000-2001 Dawit Alemayehu <adawit@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "authinfo.h"
22
23#include <config.h>
24
25#include <sys/stat.h> // don't move it down the include order, it breaks compilation on MSVC
26#include <stdio.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <sys/types.h>
30
31#include <QtCore/QByteArray>
32#include <QtCore/QDir>
33#include <QtCore/QFile>
34#include <QtDBus/QDBusArgument>
35#include <QtDBus/QDBusMetaType>
36#include <kde_file.h>
37
38#include <kdebug.h>
39#include <kstandarddirs.h>
40
41#define NETRC_READ_BUF_SIZE 4096
42
43using namespace KIO;
44
46
47class ExtraField
48{
49public:
50 ExtraField()
51 : flags(AuthInfo::ExtraFieldNoFlags)
52 {
53 }
54
55 ExtraField(const ExtraField& other)
56 : customTitle(other.customTitle),
57 flags (other.flags),
58 value (other.value)
59 {
60 }
61
62 ExtraField& operator=(const ExtraField& other)
63 {
64 customTitle = other.customTitle;
65 flags = other.flags;
66 value = other.value;
67 return *this;
68 }
69
70 QString customTitle; // reserved for future use
71 AuthInfo::FieldFlags flags;
72 QVariant value;
73};
74Q_DECLARE_METATYPE(ExtraField)
75
76QDataStream& operator<< (QDataStream& s, const ExtraField& extraField)
77{
78 s << extraField.customTitle;
79 s << (int)extraField.flags;
80 s << extraField.value;
81 return s;
82}
83
84QDataStream& operator>> (QDataStream& s, ExtraField& extraField)
85{
86 s >> extraField.customTitle ;
87 int i;
88 s >> i;
89 extraField.flags = (AuthInfo::FieldFlags)i;
90 s >> extraField.value ;
91 return s;
92}
93
94QDBusArgument &operator<<(QDBusArgument &argument, const ExtraField &extraField)
95{
96 argument.beginStructure();
97 argument << extraField.customTitle << (int)extraField.flags
98 << QDBusVariant(extraField.value);
99 argument.endStructure();
100 return argument;
101}
102
103const QDBusArgument &operator>>(const QDBusArgument &argument, ExtraField &extraField)
104{
105 QDBusVariant value;
106 int flag;
107
108 argument.beginStructure();
109 argument >> extraField.customTitle >> flag >> value;
110 argument.endStructure();
111
112 extraField.value = value.variant();
113 extraField.flags = (KIO::AuthInfo::FieldFlags)flag;
114 return argument;
115}
116
117class KIO::AuthInfoPrivate
118{
119public:
120 QMap<QString, ExtraField> extraFields;
121};
122
123
125
126AuthInfo::AuthInfo() : d(new AuthInfoPrivate())
127{
128 modified = false;
129 readOnly = false;
130 verifyPath = false;
131 keepPassword = false;
132 AuthInfo::registerMetaTypes();
133}
134
135AuthInfo::AuthInfo( const AuthInfo& info ) : d(new AuthInfoPrivate())
136{
137 (*this) = info;
138 AuthInfo::registerMetaTypes();
139}
140
141AuthInfo::~AuthInfo()
142{
143 delete d;
144}
145
146AuthInfo& AuthInfo::operator= ( const AuthInfo& info )
147{
148 url = info.url;
149 username = info.username;
150 password = info.password;
151 prompt = info.prompt;
152 caption = info.caption;
153 comment = info.comment;
154 commentLabel = info.commentLabel;
155 realmValue = info.realmValue;
156 digestInfo = info.digestInfo;
157 verifyPath = info.verifyPath;
158 readOnly = info.readOnly;
159 keepPassword = info.keepPassword;
160 modified = info.modified;
161 d->extraFields = info.d->extraFields;
162 return *this;
163}
164
165bool AuthInfo::isModified() const
166{
167 return modified;
168}
169
170void AuthInfo::setModified( bool flag )
171{
172 modified = flag;
173}
174
176
177void AuthInfo::setExtraField(const QString &fieldName, const QVariant & value)
178{
179 d->extraFields[fieldName].value = value;
180}
181
182void AuthInfo::setExtraFieldFlags(const QString &fieldName, const FieldFlags flags)
183{
184 d->extraFields[fieldName].flags = flags;
185}
186
187QVariant AuthInfo::getExtraField(const QString &fieldName) const
188{
189 if (!d->extraFields.contains(fieldName)) return QVariant();
190 return d->extraFields[fieldName].value;
191}
192
193AuthInfo::FieldFlags AuthInfo::getExtraFieldFlags(const QString &fieldName) const
194{
195 if (!d->extraFields.contains(fieldName)) return AuthInfo::ExtraFieldNoFlags;
196 return d->extraFields[fieldName].flags;
197}
198
199void AuthInfo::registerMetaTypes()
200{
201 qRegisterMetaType<ExtraField>();
202 qRegisterMetaType<KIO::AuthInfo>();
203 qDBusRegisterMetaType<ExtraField>();
204 qDBusRegisterMetaType<KIO::AuthInfo>();
205}
206
208
209QDataStream& KIO::operator<< (QDataStream& s, const AuthInfo& a)
210{
211 s << (quint8)1
212 << a.url << a.username << a.password << a.prompt << a.caption
213 << a.comment << a.commentLabel << a.realmValue << a.digestInfo
214 << a.verifyPath << a.readOnly << a.keepPassword << a.modified
215 << a.d->extraFields;
216 return s;
217}
218
219QDataStream& KIO::operator>> (QDataStream& s, AuthInfo& a)
220{
221 quint8 version;
222 s >> version
223 >> a.url >> a.username >> a.password >> a.prompt >> a.caption
224 >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
225 >> a.verifyPath >> a.readOnly >> a.keepPassword >> a.modified
226 >> a.d->extraFields;
227 return s;
228}
229
230QDBusArgument &KIO::operator<<(QDBusArgument &argument, const AuthInfo &a)
231{
232 argument.beginStructure();
233 argument << (quint8)1
234 << a.url.url() << a.username << a.password << a.prompt << a.caption
235 << a.comment << a.commentLabel << a.realmValue << a.digestInfo
236 << a.verifyPath << a.readOnly << a.keepPassword << a.modified
237 << a.d->extraFields;
238 argument.endStructure();
239 return argument;
240}
241
242const QDBusArgument &KIO::operator>>(const QDBusArgument &argument, AuthInfo &a)
243{
244 QString url;
245 quint8 version;
246
247 argument.beginStructure();
248 argument >> version
249 >> url >> a.username >> a.password >> a.prompt >> a.caption
250 >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
251 >> a.verifyPath >> a.readOnly >> a.keepPassword >> a.modified
252 >> a.d->extraFields;
253 argument.endStructure();
254
255 a.url = url;
256 return argument;
257}
258
259typedef QList<NetRC::AutoLogin> LoginList;
260typedef QMap<QString, LoginList> LoginMap;
261
262class NetRC::NetRCPrivate
263{
264public:
265 NetRCPrivate()
266 : isDirty(false)
267 {}
268 bool isDirty;
269 LoginMap loginMap;
270};
271
272NetRC* NetRC::instance = 0L;
273
274NetRC::NetRC()
275 : d( new NetRCPrivate )
276{
277}
278
279NetRC::~NetRC()
280{
281 delete instance;
282 instance = 0L;
283 delete d;
284}
285
286NetRC* NetRC::self()
287{
288 if ( !instance )
289 instance = new NetRC;
290 return instance;
291}
292
293bool NetRC::lookup( const KUrl& url, AutoLogin& login, bool userealnetrc,
294 const QString &_type, LookUpMode mode )
295{
296 // kDebug() << "AutoLogin lookup for: " << url.host();
297 if ( !url.isValid() )
298 return false;
299
300 QString type = _type;
301 if ( type.isEmpty() )
302 type = url.protocol();
303
304 if ( d->loginMap.isEmpty() || d->isDirty )
305 {
306 d->loginMap.clear();
307
308 QString filename = KStandardDirs::locateLocal("config", QLatin1String("kionetrc"));
309 bool status = parse (openf (filename));
310
311 if ( userealnetrc )
312 {
313 filename = QDir::homePath() + QLatin1String("/.netrc");
314 status |= parse (openf(filename));
315 }
316
317 if ( !status )
318 return false;
319 }
320
321 if ( !d->loginMap.contains( type ) )
322 return false;
323
324 const LoginList& l = d->loginMap[type];
325 if ( l.isEmpty() )
326 return false;
327
328 for (LoginList::ConstIterator it = l.begin(); it != l.end(); ++it)
329 {
330 const AutoLogin &log = *it;
331
332 if ( (mode & defaultOnly) == defaultOnly &&
333 log.machine == QLatin1String("default") &&
334 (login.login.isEmpty() || login.login == log.login) )
335 {
336 login.type = log.type;
337 login.machine = log.machine;
338 login.login = log.login;
339 login.password = log.password;
340 login.macdef = log.macdef;
341 }
342
343 if ( (mode & presetOnly) == presetOnly &&
344 log.machine == QLatin1String("preset") &&
345 (login.login.isEmpty() || login.login == log.login) )
346 {
347 login.type = log.type;
348 login.machine = log.machine;
349 login.login = log.login;
350 login.password = log.password;
351 login.macdef = log.macdef;
352 }
353
354 if ( (mode & exactOnly) == exactOnly &&
355 log.machine == url.host() &&
356 (login.login.isEmpty() || login.login == log.login) )
357 {
358 login.type = log.type;
359 login.machine = log.machine;
360 login.login = log.login;
361 login.password = log.password;
362 login.macdef = log.macdef;
363 break;
364 }
365 }
366
367 return true;
368}
369
370void NetRC::reload()
371{
372 d->isDirty = true;
373}
374
375int NetRC::openf( const QString& f )
376{
377 KDE_struct_stat sbuff;
378 if ( KDE::stat(f, &sbuff) != 0 )
379 return -1;
380
381 // Security check!!
382 if ( sbuff.st_mode != (S_IFREG|S_IRUSR|S_IWUSR) ||
383 sbuff.st_uid != geteuid() )
384 return -1;
385
386 return KDE::open( f, O_RDONLY );
387}
388
389QString NetRC::extract( const char* buf, const char* key, int& pos )
390{
391 int idx = pos;
392 int m_len = strlen(key);
393 int b_len = strlen(buf);
394
395 while( idx < b_len )
396 {
397 while( buf[idx] == ' ' || buf[idx] == '\t' )
398 idx++;
399
400 if ( strncasecmp( buf+idx, key, m_len ) != 0 )
401 idx++;
402 else
403 {
404 idx += m_len;
405 while( buf[idx] == ' ' || buf[idx] == '\t' )
406 idx++;
407
408 int start = idx;
409 while( buf[idx] != ' ' && buf[idx] != '\t' &&
410 buf[idx] != '\n' && buf[idx] != '\r' )
411 idx++;
412
413 if ( idx > start )
414 {
415 pos = idx;
416 return QString::fromLatin1( buf+start, idx-start);
417 }
418 }
419 }
420
421 return QString();
422}
423
424bool NetRC::parse( int fd )
425{
426 if (fd == -1)
427 return false;
428
429 QString type;
430 QString macro;
431
432 uint index = 0;
433 bool isMacro = false;
434 char* buf = new char[NETRC_READ_BUF_SIZE];
435 FILE* fstream = KDE_fdopen( fd,"rb" );
436
437 while ( fgets (buf, NETRC_READ_BUF_SIZE, fstream) != 0L )
438 {
439 int pos = 0;
440
441 while ( buf[pos] == ' ' || buf[pos] == '\t' )
442 pos++;
443
444 if ( buf[pos] == '#' || buf[pos] == '\n' ||
445 buf[pos] == '\r' || buf[pos] == '\0' )
446 {
447 if ( buf[pos] != '#' && isMacro )
448 isMacro = false;
449
450 continue;
451 }
452
453 if ( isMacro )
454 {
455 int tail = strlen(buf);
456 while( buf[tail-1] == '\n' || buf[tail-1] =='\r' )
457 tail--;
458
459 QString mac = QString::fromLatin1(buf, tail).trimmed();
460 if ( !mac.isEmpty() )
461 d->loginMap[type][index].macdef[macro].append( mac );
462
463 continue;
464 }
465
466 AutoLogin l;
467 l.machine = extract( buf, "machine", pos );
468 if ( l.machine.isEmpty() )
469 {
470 if (strncasecmp(buf+pos, "default", 7) == 0 )
471 {
472 pos += 7;
473 l.machine = QLatin1String("default");
474 }
475 else if (strncasecmp(buf+pos, "preset", 6) == 0 )
476 {
477 pos += 6;
478 l.machine = QLatin1String("preset");
479 }
480 }
481 // kDebug() << "Machine: " << l.machine;
482
483 l.login = extract( buf, "login", pos );
484 // kDebug() << "Login: " << l.login;
485
486 l.password = extract( buf, "password", pos );
487 if ( l.password.isEmpty() )
488 l.password = extract( buf, "account", pos );
489 // kDebug() << "Password: " << l.password;
490
491 type = l.type = extract( buf, "type", pos );
492 if ( l.type.isEmpty() && !l.machine.isEmpty() )
493 type = l.type = QLatin1String("ftp");
494 // kDebug() << "Type: " << l.type;
495
496 macro = extract( buf, "macdef", pos );
497 isMacro = !macro.isEmpty();
498 // kDebug() << "Macro: " << macro;
499
500 d->loginMap[l.type].append(l);
501 index = d->loginMap[l.type].count()-1;
502 }
503
504 delete [] buf;
505 fclose (fstream);
506 close (fd);
507 return true;
508}
operator>>
QDataStream & operator>>(QDataStream &s, ExtraField &extraField)
Definition: authinfo.cpp:84
operator<<
QDataStream & operator<<(QDataStream &s, const ExtraField &extraField)
Definition: authinfo.cpp:76
LoginList
QList< NetRC::AutoLogin > LoginList
Definition: authinfo.cpp:259
LoginMap
QMap< QString, LoginList > LoginMap
Definition: authinfo.cpp:260
NETRC_READ_BUF_SIZE
#define NETRC_READ_BUF_SIZE
Definition: authinfo.cpp:41
authinfo.h
KIO::AuthInfo
This class is intended to make it easier to prompt for, cache and retrieve authorization information.
Definition: authinfo.h:58
KIO::AuthInfo::isModified
bool isModified() const
Use this method to check if the object was modified.
Definition: authinfo.cpp:165
KIO::AuthInfo::setExtraFieldFlags
void setExtraFieldFlags(const QString &fieldName, const FieldFlags flags)
Set Extra Field Flags.
Definition: authinfo.cpp:182
KIO::AuthInfo::digestInfo
QString digestInfo
Field to store any extra authentication information for protocols that need it.
Definition: authinfo.h:199
KIO::AuthInfo::realmValue
QString realmValue
A unique identifier that allows caching of multiple passwords for different resources in the same ser...
Definition: authinfo.h:189
KIO::AuthInfo::setModified
void setModified(bool flag)
Use this method to indicate that this object has been modified.
Definition: authinfo.cpp:170
KIO::AuthInfo::operator=
AuthInfo & operator=(const AuthInfo &info)
Custom assignment operator.
Definition: authinfo.cpp:146
KIO::AuthInfo::modified
bool modified
Definition: authinfo.h:284
KIO::AuthInfo::verifyPath
bool verifyPath
Flag that, if set, indicates whether a path match should be performed when requesting for cached auth...
Definition: authinfo.h:212
KIO::AuthInfo::~AuthInfo
~AuthInfo()
Destructor.
Definition: authinfo.cpp:141
KIO::AuthInfo::FieldFlags
FieldFlags
Flags for extra fields.
Definition: authinfo.h:239
KIO::AuthInfo::ExtraFieldNoFlags
@ ExtraFieldNoFlags
Definition: authinfo.h:240
KIO::AuthInfo::keepPassword
bool keepPassword
Flag to indicate the persistence of the given password.
Definition: authinfo.h:232
KIO::AuthInfo::AuthInfo
AuthInfo()
Default constructor.
Definition: authinfo.cpp:126
KIO::AuthInfo::getExtraField
QVariant getExtraField(const QString &fieldName) const
Get Extra Field Value Check QVariant::isValid() to find out if the field exists.
Definition: authinfo.cpp:187
KIO::AuthInfo::comment
QString comment
Additional comment to be displayed when prompting the user for authentication information.
Definition: authinfo.h:166
KIO::AuthInfo::readOnly
bool readOnly
Flag which if set forces the username field to be read-only.
Definition: authinfo.h:219
KIO::AuthInfo::caption
QString caption
The text to displayed in the title bar of the password prompting dialog.
Definition: authinfo.h:142
KIO::AuthInfo::getExtraFieldFlags
AuthInfo::FieldFlags getExtraFieldFlags(const QString &fieldName) const
Get Extra Field Flags.
Definition: authinfo.cpp:193
KIO::AuthInfo::setExtraField
void setExtraField(const QString &fieldName, const QVariant &value)
Set Extra Field Value.
Definition: authinfo.cpp:177
KIO::AuthInfo::url
KUrl url
The URL for which authentication is to be stored.
Definition: authinfo.h:110
KIO::AuthInfo::username
QString username
This is required for caching.
Definition: authinfo.h:115
KIO::AuthInfo::registerMetaTypes
static void registerMetaTypes()
Register the meta-types for AuthInfo.
Definition: authinfo.cpp:199
KIO::AuthInfo::password
QString password
This is required for caching.
Definition: authinfo.h:120
KIO::AuthInfo::prompt
QString prompt
Information to be displayed when prompting the user for authentication information.
Definition: authinfo.h:131
KIO::AuthInfo::commentLabel
QString commentLabel
Descriptive label to be displayed in front of the comment when prompting the user for password.
Definition: authinfo.h:175
KIO::NetRC
A Singleton class that provides access to passwords stored in .netrc files for automatic login purpos...
Definition: authinfo.h:307
KIO::NetRC::lookup
bool lookup(const KUrl &url, AutoLogin &login, bool userealnetrc=false, const QString &type=QString(), LookUpMode mode=LookUpMode(exactOnly)|defaultOnly)
Looks up the login information for the given url.
Definition: authinfo.cpp:293
KIO::NetRC::presetOnly
@ presetOnly
Definition: authinfo.h:324
KIO::NetRC::defaultOnly
@ defaultOnly
Definition: authinfo.h:323
KIO::NetRC::exactOnly
@ exactOnly
Definition: authinfo.h:322
KIO::NetRC::parse
bool parse(int)
Definition: authinfo.cpp:424
KIO::NetRC::self
static NetRC * self()
A reference to the instance of the class.
Definition: authinfo.cpp:286
KIO::NetRC::openf
int openf(const QString &)
Definition: authinfo.cpp:375
KIO::NetRC::reload
void reload()
Reloads the auto login information.
Definition: authinfo.cpp:370
KIO::NetRC::extract
QString extract(const char *, const char *, int &)
Definition: authinfo.cpp:389
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, bool createDir, const KComponentData &cData=KGlobal::mainComponent())
KUrl
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
KUrl::protocol
QString protocol() const
QList
QMap
f
static quint32 f(DES_KEY *key, quint32 r, char *subkey)
Definition: des.cpp:378
kdebug.h
kstandarddirs.h
KDE::open
int open(const QString &pathname, int flags, mode_t mode)
KDE::stat
int stat(const QString &path, KDE_struct_stat *buf)
KIO
A namespace for KIO globals.
Definition: kbookmarkmenu.h:55
KIO::operator<<
QDataStream & operator<<(QDataStream &s, const AuthInfo &a)
Definition: authinfo.cpp:209
KIO::operator>>
QDataStream & operator>>(QDataStream &s, AuthInfo &a)
Definition: authinfo.cpp:219
close
KAction * close(const QObject *recvr, const char *slot, QObject *parent)
KIO::NetRC::AutoLogin
Contains auto login information.
Definition: authinfo.h:333
KIO::NetRC::AutoLogin::machine
QString machine
Definition: authinfo.h:335
KIO::NetRC::AutoLogin::login
QString login
Definition: authinfo.h:336
KIO::NetRC::AutoLogin::macdef
QMap< QString, QStringList > macdef
Definition: authinfo.h:338
KIO::NetRC::AutoLogin::type
QString type
Definition: authinfo.h:334
KIO::NetRC::AutoLogin::password
QString password
Definition: authinfo.h:337
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal