authinfo.cpp
/*
* This file is part of the KDE libraries
* Copyright (C) 2000-2001 Dawit Alemayehu <adawit@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <qdir.h>
#include <qfile.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <ksavefile.h>
#include <kstaticdeleter.h>
#include <kde_file.h>
#include "kio/authinfo.h"
#define NETRC_READ_BUF_SIZE 4096
using namespace KIO;
AuthInfo::AuthInfo()
{
modified = false;
readOnly = false;
verifyPath = false;
keepPassword = false;
}
AuthInfo::AuthInfo( const AuthInfo& info )
{
(*this) = info;
}
AuthInfo& AuthInfo::operator= ( const AuthInfo& info )
{
url = info.url;
username = info.username;
password = info.password;
prompt = info.prompt;
caption = info.caption;
comment = info.comment;
commentLabel = info.commentLabel;
realmValue = info.realmValue;
digestInfo = info.digestInfo;
verifyPath = info.verifyPath;
readOnly = info.readOnly;
keepPassword = info.keepPassword;
modified = info.modified;
return *this;
}
QDataStream& KIO::operator<< (QDataStream& s, const AuthInfo& a)
{
s << a.url << a.username << a.password << a.prompt << a.caption
<< a.comment << a.commentLabel << a.realmValue << a.digestInfo
<< Q_UINT8(a.verifyPath ? 1:0) << Q_UINT8(a.readOnly ? 1:0)
<< Q_UINT8(a.keepPassword ? 1:0) << Q_UINT8(a.modified ? 1:0);
return s;
}
QDataStream& KIO::operator>> (QDataStream& s, AuthInfo& a)
{
Q_UINT8 verify = 0;
Q_UINT8 ro = 0;
Q_UINT8 keep = 0;
Q_UINT8 mod = 0;
s >> a.url >> a.username >> a.password >> a.prompt >> a.caption
>> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
>> verify >> ro >> keep >> mod;
a.verifyPath = (verify != 0);
a.readOnly = (ro != 0);
a.keepPassword = (keep != 0);
a.modified = (mod != 0);
return s;
}
NetRC* NetRC::instance = 0L;
NetRC::NetRC()
{
isDirty = false;
}
NetRC::~NetRC()
{
delete instance;
instance = 0L;
}
NetRC* NetRC::self()
{
if ( !instance )
instance = new NetRC();
return instance;
}
bool NetRC::lookup( const KURL& url, AutoLogin& login, bool userealnetrc,
QString type, int mode )
{
// kdDebug() << "AutoLogin lookup for: " << url.host() << endl;
if ( !url.isValid() )
return false;
if ( type.isEmpty() )
type = url.protocol();
if ( loginMap.isEmpty() || isDirty )
{
loginMap.clear();
QString filename = locateLocal("config", "kionetrc");
bool status = parse (openf (filename));
if ( userealnetrc )
{
filename = QDir::homeDirPath()+ QDir::separator() + ".netrc";
status |= parse (openf(filename));
}
if ( !status )
return false;
}
if ( !loginMap.contains( type ) )
return false;
LoginList l = loginMap[type];
if ( l.isEmpty() )
return false;
for (LoginList::Iterator it = l.begin(); it != l.end(); ++it)
{
AutoLogin &log = *it;
if ( (mode & defaultOnly) == defaultOnly &&
log.machine == QString::fromLatin1("default") &&
(login.login.isEmpty() || login.login == log.login) )
{
login.type = log.type;
login.machine = log.machine;
login.login = log.login;
login.password = log.password;
login.macdef = log.macdef;
}
if ( (mode & presetOnly) == presetOnly &&
log.machine == QString::fromLatin1("preset") &&
(login.login.isEmpty() || login.login == log.login) )
{
login.type = log.type;
login.machine = log.machine;
login.login = log.login;
login.password = log.password;
login.macdef = log.macdef;
}
if ( (mode & exactOnly) == exactOnly &&
log.machine == url.host() &&
(login.login.isEmpty() || login.login == log.login) )
{
login.type = log.type;
login.machine = log.machine;
login.login = log.login;
login.password = log.password;
login.macdef = log.macdef;
break;
}
}
return true;
}
int NetRC::openf( const QString& f )
{
KDE_struct_stat sbuff;
QCString ef = QFile::encodeName(f);
if ( KDE_stat(ef, &sbuff) != 0 )
return -1;
// Security check!!
if ( sbuff.st_mode != (S_IFREG|S_IRUSR|S_IWUSR) ||
sbuff.st_uid != geteuid() )
return -1;
return KDE_open( ef, O_RDONLY );
}
QString NetRC::extract( const char* buf, const char* key, int& pos )
{
int idx = pos;
int m_len = strlen(key);
int b_len = strlen(buf);
while( idx < b_len )
{
while( buf[idx] == ' ' || buf[idx] == '\t' )
idx++;
if ( strncasecmp( buf+idx, key, m_len ) != 0 )
idx++;
else
{
idx += m_len;
while( buf[idx] == ' ' || buf[idx] == '\t' )
idx++;
int start = idx;
while( buf[idx] != ' ' && buf[idx] != '\t' &&
buf[idx] != '\n' && buf[idx] != '\r' )
idx++;
if ( idx > start )
{
pos = idx;
return QString::fromLatin1( buf+start, idx-start);
}
}
}
return QString::null;
}
bool NetRC::parse( int fd )
{
if (fd == -1)
return false;
QString type;
QString macro;
uint index = 0;
bool isMacro = false;
char* buf = new char[NETRC_READ_BUF_SIZE];
FILE* fstream = KDE_fdopen( fd,"rb" );
while ( fgets (buf, NETRC_READ_BUF_SIZE, fstream) != 0L )
{
int pos = 0;
while ( buf[pos] == ' ' || buf[pos] == '\t' )
pos++;
if ( buf[pos] == '#' || buf[pos] == '\n' ||
buf[pos] == '\r' || buf[pos] == '\0' )
{
if ( buf[pos] != '#' && isMacro )
isMacro = false;
continue;
}
if ( isMacro )
{
int tail = strlen(buf);
while( buf[tail-1] == '\n' || buf[tail-1] =='\r' )
tail--;
QString mac = QString::fromLatin1(buf, tail).stripWhiteSpace();
if ( !mac.isEmpty() )
loginMap[type][index].macdef[macro].append( mac );
continue;
}
AutoLogin l;
l.machine = extract( buf, "machine", pos );
if ( l.machine.isEmpty() )
{
if (strncasecmp(buf+pos, "default", 7) == 0 )
{
pos += 7;
l.machine = QString::fromLatin1("default");
}
else if (strncasecmp(buf+pos, "preset", 6) == 0 )
{
pos += 6;
l.machine = QString::fromLatin1("preset");
}
}
// kdDebug() << "Machine: " << l.machine << endl;
l.login = extract( buf, "login", pos );
// kdDebug() << "Login: " << l.login << endl;
l.password = extract( buf, "password", pos );
if ( l.password.isEmpty() )
l.password = extract( buf, "account", pos );
// kdDebug() << "Password: " << l.password << endl;
type = l.type = extract( buf, "type", pos );
if ( l.type.isEmpty() && !l.machine.isEmpty() )
type = l.type = QString::fromLatin1("ftp");
// kdDebug() << "Type: " << l.type << endl;
macro = extract( buf, "macdef", pos );
isMacro = !macro.isEmpty();
// kdDebug() << "Macro: " << macro << endl;
loginMap[l.type].append(l);
index = loginMap[l.type].count()-1;
}
delete [] buf;
fclose (fstream);
close (fd);
return true;
}
KIO::AuthInfo::comment
QString comment
Additional comment to be displayed when prompting the user for authentication information.
Definition: authinfo.h:150
KIO::AuthInfo::operator=
AuthInfo & operator=(const AuthInfo &info)
Overloaded equal to operator.
Definition: authinfo.cpp:57
KIO::AuthInfo::keepPassword
bool keepPassword
Flag to indicate the persistence of the given password.
Definition: authinfo.h:220
KIO::NetRC::self
static NetRC * self()
A reference to the instance of the class.
Definition: authinfo.cpp:115
KIO::AuthInfo::digestInfo
QString digestInfo
Field to store any extra authentication information for protocols that need it (ex: http)...
Definition: authinfo.h:187
KIO::AuthInfo
This class is intended to make it easier to prompt for, cache and retrieve authorization information...
Definition: authinfo.h:51
KIO::AuthInfo::readOnly
bool readOnly
Flag which if set forces the username field to be read-only.
Definition: authinfo.h:207
KIO::AuthInfo::url
KURL url
The URL for which authentication is to be stored.
Definition: authinfo.h:94
KIO::AuthInfo::realmValue
QString realmValue
A unique identifier that allows caching of multiple passwords for different resources in the same ser...
Definition: authinfo.h:177
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:200
KIO::AuthInfo::AuthInfo
AuthInfo()
Default constructor.
Definition: authinfo.cpp:44
KIO::NetRC::lookup
bool lookup(const KURL &url, AutoLogin &login, bool userealnetrc=false, QString type=QString::null, int mode=(exactOnly|defaultOnly))
Looks up the login information for the given url.
Definition: authinfo.cpp:122
KIO::NetRC
A Singleton class that provides access to passwords stored in .netrc files for automatic login purpos...
Definition: authinfo.h:240
KIO::AuthInfo::password
QString password
This is required for caching.
Definition: authinfo.h:104
KIO::AuthInfo::username
QString username
This is required for caching.
Definition: authinfo.h:99
KIO::AuthInfo::caption
QString caption
The text to displayed in the title bar of the password prompting dialog.
Definition: authinfo.h:126
KIO::AuthInfo::commentLabel
QString commentLabel
Descriptive label to be displayed in front of the comment when prompting the user for password...
Definition: authinfo.h:159
KIO::AuthInfo::prompt
QString prompt
Information to be displayed when prompting the user for authentication information.
Definition: authinfo.h:115
KIO::NetRC::AutoLogin
Contains auto login information.
Definition: authinfo.h:265

KDE Home | KDE Accessibility Home | Description of Access Keys