• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

authinfo.cpp

Go to the documentation of this file.
00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (C) 2000-2001 Dawit Alemayehu <adawit@kde.org>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  *  Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "authinfo.h"
00022 
00023 #include <config.h>
00024 
00025 #include <sys/stat.h> // don't move it down the include order, it breaks compilation on MSVC
00026 #include <stdio.h>
00027 #include <fcntl.h>
00028 #include <unistd.h>
00029 #include <sys/types.h>
00030 
00031 #include <QtCore/QByteArray>
00032 #include <QtCore/QDir>
00033 #include <QtCore/QFile>
00034 #include <kde_file.h>
00035 
00036 #include <kdebug.h>
00037 #include <kstandarddirs.h>
00038 #include <ksavefile.h>
00039 
00040 #define NETRC_READ_BUF_SIZE 4096
00041 
00042 using namespace KIO;
00043 
00045 
00046 class ExtraField
00047 {
00048 public:    
00049     QString customTitle; // reserved for future use
00050     AuthInfo::FieldFlags flags;
00051     QVariant value;
00052 
00053     ExtraField() : flags(AuthInfo::ExtraFieldNoFlags) {}
00054 };
00055 
00056 QDataStream& operator<< (QDataStream& s, const ExtraField& extraField)
00057 {
00058     s << extraField.customTitle;
00059     s << (int)extraField.flags;
00060     s << extraField.value;
00061     return s;
00062 }
00063 
00064 QDataStream& operator>> (QDataStream& s, ExtraField& extraField)
00065 {
00066     s >> extraField.customTitle ;
00067     int i;
00068     s >> i;
00069     extraField.flags = (AuthInfo::FieldFlags)i;
00070     s >> extraField.value ;
00071     return s;
00072 }
00073 
00074 
00075 class KIO::AuthInfoPrivate  
00076 {
00077 public:
00078     AuthInfoPrivate() 
00079     {}
00080     
00081     QMap<QString, ExtraField> extraFields;
00082 };
00083 
00084 
00086 
00087 AuthInfo::AuthInfo() : d(new AuthInfoPrivate())
00088 {
00089     modified = false;
00090     readOnly = false;
00091     verifyPath = false;
00092     keepPassword = false;
00093 }
00094 
00095 AuthInfo::AuthInfo( const AuthInfo& info ) : d(new AuthInfoPrivate())
00096 {
00097     (*this) = info;
00098 }
00099 
00100 AuthInfo::~AuthInfo()
00101 {
00102     delete d;
00103 }
00104 
00105 AuthInfo& AuthInfo::operator= ( const AuthInfo& info )
00106 {
00107     url = info.url;
00108     username = info.username;
00109     password = info.password;
00110     prompt = info.prompt;
00111     caption = info.caption;
00112     comment = info.comment;
00113     commentLabel = info.commentLabel;
00114     realmValue = info.realmValue;
00115     digestInfo = info.digestInfo;
00116     verifyPath = info.verifyPath;
00117     readOnly = info.readOnly;
00118     keepPassword = info.keepPassword;
00119     modified = info.modified;
00120     d->extraFields = info.d->extraFields; 
00121     return *this;
00122 }
00123 
00124 bool AuthInfo::isModified() const
00125 {
00126     return modified;
00127 }
00128 
00129 void AuthInfo::setModified( bool flag )
00130 {
00131     modified = flag;
00132 }
00133 
00135 
00136 void AuthInfo::setExtraField(const QString &fieldName, const QVariant & value)
00137 {
00138     d->extraFields[fieldName].value = value;
00139 }
00140  
00141 void AuthInfo::setExtraFieldFlags(const QString &fieldName, const FieldFlags flags)
00142 {
00143     d->extraFields[fieldName].flags = flags;
00144 }
00145  
00146 QVariant AuthInfo::getExtraField(const QString &fieldName) const
00147 {
00148     if (!d->extraFields.contains(fieldName)) return QVariant();
00149     return d->extraFields[fieldName].value; 
00150 }
00151  
00152 AuthInfo::FieldFlags AuthInfo::getExtraFieldFlags(const QString &fieldName) const
00153 {
00154     if (!d->extraFields.contains(fieldName)) return AuthInfo::ExtraFieldNoFlags;
00155     return d->extraFields[fieldName].flags; 
00156 }
00157 
00159 
00160 QDataStream& KIO::operator<< (QDataStream& s, const AuthInfo& a)
00161 {
00162     s << (quint8)1
00163       << a.url << a.username << a.password << a.prompt << a.caption
00164       << a.comment << a.commentLabel << a.realmValue << a.digestInfo
00165       << a.verifyPath << a.readOnly << a.keepPassword << a.modified
00166       << a.d->extraFields;
00167     return s;
00168 }
00169 
00170 QDataStream& KIO::operator>> (QDataStream& s, AuthInfo& a)
00171 {
00172     quint8 version;
00173     s >> version
00174       >> a.url >> a.username >> a.password >> a.prompt >> a.caption
00175       >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
00176       >> a.verifyPath >> a.readOnly >> a.keepPassword >> a.modified
00177       >> a.d->extraFields;
00178     return s;
00179 }
00180 
00181 
00182 typedef QList<NetRC::AutoLogin> LoginList;
00183 typedef QMap<QString, LoginList> LoginMap;
00184 
00185 class NetRC::NetRCPrivate
00186 {
00187 public:
00188     NetRCPrivate()
00189         : isDirty(false)
00190     {}
00191     bool isDirty;
00192     LoginMap loginMap;
00193 };
00194 
00195 NetRC* NetRC::instance = 0L;
00196 
00197 NetRC::NetRC()
00198     : d( new NetRCPrivate )
00199 {
00200 }
00201 
00202 NetRC::~NetRC()
00203 {
00204     delete instance;
00205     instance = 0L;
00206     delete d;
00207 }
00208 
00209 NetRC* NetRC::self()
00210 {
00211     if ( !instance )
00212         instance = new NetRC;
00213     return instance;
00214 }
00215 
00216 bool NetRC::lookup( const KUrl& url, AutoLogin& login, bool userealnetrc,
00217                     const QString &_type, LookUpMode mode )
00218 {
00219   // kDebug() << "AutoLogin lookup for: " << url.host();
00220   if ( !url.isValid() )
00221     return false;
00222 
00223   QString type = _type;
00224   if ( type.isEmpty() )
00225     type = url.protocol();
00226 
00227   if ( d->loginMap.isEmpty() || d->isDirty )
00228   {
00229     d->loginMap.clear();
00230 
00231     QString filename = KStandardDirs::locateLocal("config", QLatin1String("kionetrc"));
00232     bool status = parse (openf (filename));
00233 
00234     if ( userealnetrc )
00235     {
00236       filename =  QDir::homePath() + QDir::separator() + QLatin1String(".netrc");
00237       status |= parse (openf(filename));
00238     }
00239 
00240     if ( !status )
00241       return false;
00242   }
00243 
00244   if ( !d->loginMap.contains( type ) )
00245     return false;
00246 
00247   const LoginList& l = d->loginMap[type];
00248   if ( l.isEmpty() )
00249     return false;
00250 
00251   for (LoginList::ConstIterator it = l.begin(); it != l.end(); ++it)
00252   {
00253     const AutoLogin &log = *it;
00254 
00255     if ( (mode & defaultOnly) == defaultOnly &&
00256           log.machine == QLatin1String("default") &&
00257           (login.login.isEmpty() || login.login == log.login) )
00258     {
00259       login.type = log.type;
00260       login.machine = log.machine;
00261       login.login = log.login;
00262       login.password = log.password;
00263       login.macdef = log.macdef;
00264     }
00265 
00266     if ( (mode & presetOnly) == presetOnly &&
00267           log.machine == QLatin1String("preset") &&
00268           (login.login.isEmpty() || login.login == log.login) )
00269     {
00270       login.type = log.type;
00271       login.machine = log.machine;
00272       login.login = log.login;
00273       login.password = log.password;
00274       login.macdef = log.macdef;
00275     }
00276 
00277     if ( (mode & exactOnly) == exactOnly &&
00278           log.machine == url.host() &&
00279           (login.login.isEmpty() || login.login == log.login) )
00280     {
00281       login.type = log.type;
00282       login.machine = log.machine;
00283       login.login = log.login;
00284       login.password = log.password;
00285       login.macdef = log.macdef;
00286       break;
00287     }
00288   }
00289 
00290   return true;
00291 }
00292 
00293 void NetRC::reload()
00294 {
00295     d->isDirty = true;
00296 }
00297 
00298 int NetRC::openf( const QString& f )
00299 {
00300   KDE_struct_stat sbuff;
00301   QByteArray ef = QFile::encodeName(f);
00302   if ( KDE_stat(ef, &sbuff) != 0 )
00303     return -1;
00304 
00305   // Security check!!
00306   if ( sbuff.st_mode != (S_IFREG|S_IRUSR|S_IWUSR) ||
00307        sbuff.st_uid != geteuid() )
00308     return -1;
00309 
00310   return KDE_open( ef, O_RDONLY );
00311 }
00312 
00313 QString NetRC::extract( const char* buf, const char* key, int& pos )
00314 {
00315   int idx = pos;
00316   int m_len = strlen(key);
00317   int b_len = strlen(buf);
00318 
00319   while( idx < b_len )
00320   {
00321     while( buf[idx] == ' ' || buf[idx] == '\t' )
00322       idx++;
00323 
00324     if ( strncasecmp( buf+idx, key, m_len ) != 0 )
00325       idx++;
00326     else
00327     {
00328       idx += m_len;
00329       while( buf[idx] == ' ' || buf[idx] == '\t' )
00330         idx++;
00331 
00332       int start = idx;
00333       while( buf[idx] != ' ' && buf[idx] != '\t' &&
00334              buf[idx] != '\n' && buf[idx] != '\r' )
00335         idx++;
00336 
00337       if ( idx > start )
00338       {
00339         pos = idx;
00340         return QString::fromLatin1( buf+start, idx-start);
00341       }
00342     }
00343   }
00344 
00345   return QString();
00346 }
00347 
00348 bool NetRC::parse( int fd )
00349 {
00350   if (fd == -1)
00351     return false;
00352 
00353   QString type;
00354   QString macro;
00355 
00356   uint index = 0;
00357   bool isMacro = false;
00358   char* buf = new char[NETRC_READ_BUF_SIZE];
00359   FILE* fstream = KDE_fdopen( fd,"rb" );
00360 
00361   while ( fgets (buf, NETRC_READ_BUF_SIZE, fstream) != 0L )
00362   {
00363     int pos = 0;
00364 
00365     while ( buf[pos] == ' ' || buf[pos] == '\t' )
00366       pos++;
00367 
00368     if ( buf[pos] == '#' || buf[pos] == '\n' ||
00369          buf[pos] == '\r' || buf[pos] == '\0' )
00370     {
00371       if ( buf[pos] != '#' && isMacro )
00372         isMacro = false;
00373 
00374       continue;
00375     }
00376 
00377     if ( isMacro )
00378     {
00379       int tail = strlen(buf);
00380       while( buf[tail-1] == '\n' || buf[tail-1] =='\r' )
00381         tail--;
00382 
00383       QString mac = QString::fromLatin1(buf, tail).trimmed();
00384       if ( !mac.isEmpty() )
00385         d->loginMap[type][index].macdef[macro].append( mac );
00386 
00387       continue;
00388     }
00389 
00390     AutoLogin l;
00391     l.machine = extract( buf, "machine", pos );
00392     if ( l.machine.isEmpty() )
00393     {
00394       if (strncasecmp(buf+pos, "default", 7) == 0 )
00395       {
00396         pos += 7;
00397         l.machine = QLatin1String("default");
00398       }
00399       else if (strncasecmp(buf+pos, "preset", 6) == 0 )
00400       {
00401         pos += 6;
00402         l.machine = QLatin1String("preset");
00403       }
00404     }
00405     // kDebug() << "Machine: " << l.machine;
00406 
00407     l.login = extract( buf, "login", pos );
00408     // kDebug() << "Login: " << l.login;
00409 
00410     l.password = extract( buf, "password", pos );
00411     if ( l.password.isEmpty() )
00412       l.password = extract( buf, "account", pos );
00413     // kDebug() << "Password: " << l.password;
00414 
00415     type = l.type = extract( buf, "type", pos );
00416     if ( l.type.isEmpty() && !l.machine.isEmpty() )
00417       type = l.type = QLatin1String("ftp");
00418     // kDebug() << "Type: " << l.type;
00419 
00420     macro = extract( buf, "macdef", pos );
00421     isMacro = !macro.isEmpty();
00422     // kDebug() << "Macro: " << macro;
00423 
00424     d->loginMap[l.type].append(l);
00425     index = d->loginMap[l.type].count()-1;
00426   }
00427 
00428   delete [] buf;
00429   fclose (fstream);
00430   close (fd);
00431   return true;
00432 }

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal