• Skip to content
  • Skip to link menu
KDE 4.0 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 
00044 AuthInfo::AuthInfo()
00045 {
00046     modified = false;
00047     readOnly = false;
00048     verifyPath = false;
00049     keepPassword = false;
00050 }
00051 
00052 AuthInfo::AuthInfo( const AuthInfo& info )
00053 {
00054     (*this) = info;
00055 }
00056 
00057 AuthInfo& AuthInfo::operator= ( const AuthInfo& info )
00058 {
00059     url = info.url;
00060     username = info.username;
00061     password = info.password;
00062     prompt = info.prompt;
00063     caption = info.caption;
00064     comment = info.comment;
00065     commentLabel = info.commentLabel;
00066     realmValue = info.realmValue;
00067     digestInfo = info.digestInfo;
00068     verifyPath = info.verifyPath;
00069     readOnly = info.readOnly;
00070     keepPassword = info.keepPassword;
00071     modified = info.modified;
00072     return *this;
00073 }
00074 
00075 bool AuthInfo::isModified() const
00076 {
00077     return modified;
00078 }
00079 
00080 void AuthInfo::setModified( bool flag )
00081 {
00082     modified = flag;
00083 }
00084 
00085 QDataStream& KIO::operator<< (QDataStream& s, const AuthInfo& a)
00086 {
00087     s << a.url << a.username << a.password << a.prompt << a.caption
00088       << a.comment << a.commentLabel << a.realmValue << a.digestInfo
00089       << a.verifyPath << a.readOnly << a.keepPassword << a.modified;
00090     return s;
00091 }
00092 
00093 QDataStream& KIO::operator>> (QDataStream& s, AuthInfo& a)
00094 {
00095     s >> a.url >> a.username >> a.password >> a.prompt >> a.caption
00096       >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
00097       >> a.verifyPath >> a.readOnly >> a.keepPassword >> a.modified;
00098     return s;
00099 }
00100 
00101 
00102 typedef QList<NetRC::AutoLogin> LoginList;
00103 typedef QMap<QString, LoginList> LoginMap;
00104 
00105 class NetRC::NetRCPrivate
00106 {
00107 public:
00108     NetRCPrivate()
00109         : isDirty(false)
00110     {}
00111     bool isDirty;
00112     LoginMap loginMap;
00113 };
00114 
00115 NetRC* NetRC::instance = 0L;
00116 
00117 NetRC::NetRC()
00118     : d( new NetRCPrivate )
00119 {
00120 }
00121 
00122 NetRC::~NetRC()
00123 {
00124     delete instance;
00125     instance = 0L;
00126     delete d;
00127 }
00128 
00129 NetRC* NetRC::self()
00130 {
00131     if ( !instance )
00132         instance = new NetRC;
00133     return instance;
00134 }
00135 
00136 bool NetRC::lookup( const KUrl& url, AutoLogin& login, bool userealnetrc,
00137                     const QString &_type, LookUpMode mode )
00138 {
00139   // kDebug() << "AutoLogin lookup for: " << url.host();
00140   if ( !url.isValid() )
00141     return false;
00142 
00143   QString type = _type;
00144   if ( type.isEmpty() )
00145     type = url.protocol();
00146 
00147   if ( d->loginMap.isEmpty() || d->isDirty )
00148   {
00149     d->loginMap.clear();
00150 
00151     QString filename = KStandardDirs::locateLocal("config", QLatin1String("kionetrc"));
00152     bool status = parse (openf (filename));
00153 
00154     if ( userealnetrc )
00155     {
00156       filename =  QDir::homePath() + QDir::separator() + QLatin1String(".netrc");
00157       status |= parse (openf(filename));
00158     }
00159 
00160     if ( !status )
00161       return false;
00162   }
00163 
00164   if ( !d->loginMap.contains( type ) )
00165     return false;
00166 
00167   const LoginList& l = d->loginMap[type];
00168   if ( l.isEmpty() )
00169     return false;
00170 
00171   for (LoginList::ConstIterator it = l.begin(); it != l.end(); ++it)
00172   {
00173     const AutoLogin &log = *it;
00174 
00175     if ( (mode & defaultOnly) == defaultOnly &&
00176           log.machine == QLatin1String("default") &&
00177           (login.login.isEmpty() || login.login == log.login) )
00178     {
00179       login.type = log.type;
00180       login.machine = log.machine;
00181       login.login = log.login;
00182       login.password = log.password;
00183       login.macdef = log.macdef;
00184     }
00185 
00186     if ( (mode & presetOnly) == presetOnly &&
00187           log.machine == QLatin1String("preset") &&
00188           (login.login.isEmpty() || login.login == log.login) )
00189     {
00190       login.type = log.type;
00191       login.machine = log.machine;
00192       login.login = log.login;
00193       login.password = log.password;
00194       login.macdef = log.macdef;
00195     }
00196 
00197     if ( (mode & exactOnly) == exactOnly &&
00198           log.machine == url.host() &&
00199           (login.login.isEmpty() || login.login == log.login) )
00200     {
00201       login.type = log.type;
00202       login.machine = log.machine;
00203       login.login = log.login;
00204       login.password = log.password;
00205       login.macdef = log.macdef;
00206       break;
00207     }
00208   }
00209 
00210   return true;
00211 }
00212 
00213 void NetRC::reload()
00214 {
00215     d->isDirty = true;
00216 }
00217 
00218 int NetRC::openf( const QString& f )
00219 {
00220   KDE_struct_stat sbuff;
00221   QByteArray ef = QFile::encodeName(f);
00222   if ( KDE_stat(ef, &sbuff) != 0 )
00223     return -1;
00224 
00225   // Security check!!
00226   if ( sbuff.st_mode != (S_IFREG|S_IRUSR|S_IWUSR) ||
00227        sbuff.st_uid != geteuid() )
00228     return -1;
00229 
00230   return KDE_open( ef, O_RDONLY );
00231 }
00232 
00233 QString NetRC::extract( const char* buf, const char* key, int& pos )
00234 {
00235   int idx = pos;
00236   int m_len = strlen(key);
00237   int b_len = strlen(buf);
00238 
00239   while( idx < b_len )
00240   {
00241     while( buf[idx] == ' ' || buf[idx] == '\t' )
00242       idx++;
00243 
00244     if ( strncasecmp( buf+idx, key, m_len ) != 0 )
00245       idx++;
00246     else
00247     {
00248       idx += m_len;
00249       while( buf[idx] == ' ' || buf[idx] == '\t' )
00250         idx++;
00251 
00252       int start = idx;
00253       while( buf[idx] != ' ' && buf[idx] != '\t' &&
00254              buf[idx] != '\n' && buf[idx] != '\r' )
00255         idx++;
00256 
00257       if ( idx > start )
00258       {
00259         pos = idx;
00260         return QString::fromLatin1( buf+start, idx-start);
00261       }
00262     }
00263   }
00264 
00265   return QString();
00266 }
00267 
00268 bool NetRC::parse( int fd )
00269 {
00270   if (fd == -1)
00271     return false;
00272 
00273   QString type;
00274   QString macro;
00275 
00276   uint index = 0;
00277   bool isMacro = false;
00278   char* buf = new char[NETRC_READ_BUF_SIZE];
00279   FILE* fstream = KDE_fdopen( fd,"rb" );
00280 
00281   while ( fgets (buf, NETRC_READ_BUF_SIZE, fstream) != 0L )
00282   {
00283     int pos = 0;
00284 
00285     while ( buf[pos] == ' ' || buf[pos] == '\t' )
00286       pos++;
00287 
00288     if ( buf[pos] == '#' || buf[pos] == '\n' ||
00289          buf[pos] == '\r' || buf[pos] == '\0' )
00290     {
00291       if ( buf[pos] != '#' && isMacro )
00292         isMacro = false;
00293 
00294       continue;
00295     }
00296 
00297     if ( isMacro )
00298     {
00299       int tail = strlen(buf);
00300       while( buf[tail-1] == '\n' || buf[tail-1] =='\r' )
00301         tail--;
00302 
00303       QString mac = QString::fromLatin1(buf, tail).trimmed();
00304       if ( !mac.isEmpty() )
00305         d->loginMap[type][index].macdef[macro].append( mac );
00306 
00307       continue;
00308     }
00309 
00310     AutoLogin l;
00311     l.machine = extract( buf, "machine", pos );
00312     if ( l.machine.isEmpty() )
00313     {
00314       if (strncasecmp(buf+pos, "default", 7) == 0 )
00315       {
00316         pos += 7;
00317         l.machine = QLatin1String("default");
00318       }
00319       else if (strncasecmp(buf+pos, "preset", 6) == 0 )
00320       {
00321         pos += 6;
00322         l.machine = QLatin1String("preset");
00323       }
00324     }
00325     // kDebug() << "Machine: " << l.machine;
00326 
00327     l.login = extract( buf, "login", pos );
00328     // kDebug() << "Login: " << l.login;
00329 
00330     l.password = extract( buf, "password", pos );
00331     if ( l.password.isEmpty() )
00332       l.password = extract( buf, "account", pos );
00333     // kDebug() << "Password: " << l.password;
00334 
00335     type = l.type = extract( buf, "type", pos );
00336     if ( l.type.isEmpty() && !l.machine.isEmpty() )
00337       type = l.type = QLatin1String("ftp");
00338     // kDebug() << "Type: " << l.type;
00339 
00340     macro = extract( buf, "macdef", pos );
00341     isMacro = !macro.isEmpty();
00342     // kDebug() << "Macro: " << macro;
00343 
00344     d->loginMap[l.type].append(l);
00345     index = d->loginMap[l.type].count()-1;
00346   }
00347 
00348   delete [] buf;
00349   fclose (fstream);
00350   close (fd);
00351   return true;
00352 }

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
  •   WTF
  • KJSEmbed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  •   core
  • Phonon
  •   Backend
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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