You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
333 lines
7.8 KiB
333 lines
7.8 KiB
15 years ago
|
/*
|
||
|
* 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>
|
||
|
|
||
14 years ago
|
#include <tqdir.h>
|
||
|
#include <tqfile.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <ksavefile.h>
|
||
|
#include <kstaticdeleter.h>
|
||
|
#include <kde_file.h>
|
||
|
|
||
12 years ago
|
#include "tdeio/authinfo.h"
|
||
15 years ago
|
|
||
|
#define NETRC_READ_BUF_SIZE 4096
|
||
|
|
||
12 years ago
|
using namespace TDEIO;
|
||
15 years ago
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQDataStream& TDEIO::operator<< (TQDataStream& s, const AuthInfo& a)
|
||
15 years ago
|
{
|
||
|
s << a.url << a.username << a.password << a.prompt << a.caption
|
||
|
<< a.comment << a.commentLabel << a.realmValue << a.digestInfo
|
||
14 years ago
|
<< TQ_UINT8(a.verifyPath ? 1:0) << TQ_UINT8(a.readOnly ? 1:0)
|
||
|
<< TQ_UINT8(a.keepPassword ? 1:0) << TQ_UINT8(a.modified ? 1:0);
|
||
15 years ago
|
return s;
|
||
|
}
|
||
|
|
||
12 years ago
|
TQDataStream& TDEIO::operator>> (TQDataStream& s, AuthInfo& a)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQ_UINT8 verify = 0;
|
||
|
TQ_UINT8 ro = 0;
|
||
|
TQ_UINT8 keep = 0;
|
||
|
TQ_UINT8 mod = 0;
|
||
15 years ago
|
|
||
|
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,
|
||
14 years ago
|
TQString type, int mode )
|
||
15 years ago
|
{
|
||
|
// kdDebug() << "AutoLogin lookup for: " << url.host() << endl;
|
||
|
if ( !url.isValid() )
|
||
|
return false;
|
||
|
|
||
|
if ( type.isEmpty() )
|
||
|
type = url.protocol();
|
||
|
|
||
|
if ( loginMap.isEmpty() || isDirty )
|
||
|
{
|
||
|
loginMap.clear();
|
||
|
|
||
14 years ago
|
TQString filename = locateLocal("config", "kionetrc");
|
||
15 years ago
|
bool status = parse (openf (filename));
|
||
|
|
||
|
if ( userealnetrc )
|
||
|
{
|
||
14 years ago
|
filename = TQDir::homeDirPath()+ TQDir::separator() + ".netrc";
|
||
15 years ago
|
status |= parse (openf(filename));
|
||
|
}
|
||
|
|
||
|
if ( !status )
|
||
|
return false;
|
||
|
}
|
||
|
|
||
13 years ago
|
if ( !loginMap.contains( type ) )
|
||
15 years ago
|
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 &&
|
||
13 years ago
|
log.machine == TQString::fromLatin1("default") &&
|
||
15 years ago
|
(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 &&
|
||
13 years ago
|
log.machine == TQString::fromLatin1("preset") &&
|
||
15 years ago
|
(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;
|
||
|
}
|
||
|
|
||
14 years ago
|
int NetRC::openf( const TQString& f )
|
||
15 years ago
|
{
|
||
|
KDE_struct_stat sbuff;
|
||
14 years ago
|
TQCString ef = TQFile::encodeName(f);
|
||
15 years ago
|
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 );
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString NetRC::extract( const char* buf, const char* key, int& pos )
|
||
15 years ago
|
{
|
||
|
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;
|
||
13 years ago
|
return TQString::fromLatin1( buf+start, idx-start);
|
||
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
|
bool NetRC::parse( int fd )
|
||
|
{
|
||
|
if (fd == -1)
|
||
|
return false;
|
||
|
|
||
14 years ago
|
TQString type;
|
||
|
TQString macro;
|
||
15 years ago
|
|
||
|
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--;
|
||
|
|
||
13 years ago
|
TQString mac = TQString::fromLatin1(buf, tail).stripWhiteSpace();
|
||
15 years ago
|
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;
|
||
13 years ago
|
l.machine = TQString::fromLatin1("default");
|
||
15 years ago
|
}
|
||
|
else if (strncasecmp(buf+pos, "preset", 6) == 0 )
|
||
|
{
|
||
|
pos += 6;
|
||
13 years ago
|
l.machine = TQString::fromLatin1("preset");
|
||
15 years ago
|
}
|
||
|
}
|
||
|
// 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() )
|
||
13 years ago
|
type = l.type = TQString::fromLatin1("ftp");
|
||
15 years ago
|
// 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;
|
||
|
}
|