authinfo.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <stdio.h>
00024 #include <fcntl.h>
00025 #include <unistd.h>
00026 #include <sys/stat.h>
00027 #include <sys/types.h>
00028
00029 #include <qdir.h>
00030 #include <qfile.h>
00031
00032 #include <kdebug.h>
00033 #include <kstandarddirs.h>
00034 #include <ksavefile.h>
00035 #include <kstaticdeleter.h>
00036
00037 #include "kio/authinfo.h"
00038
00039 #define NETRC_READ_BUF_SIZE 4096
00040
00041 using namespace KIO;
00042
00043 AuthInfo::AuthInfo()
00044 {
00045 modified = false;
00046 readOnly = false;
00047 verifyPath = false;
00048 keepPassword = false;
00049 }
00050
00051 AuthInfo::AuthInfo( const AuthInfo& info )
00052 {
00053 (*this) = info;
00054 }
00055
00056 AuthInfo& AuthInfo::operator= ( const AuthInfo& info )
00057 {
00058 url = info.url;
00059 username = info.username;
00060 password = info.password;
00061 prompt = info.prompt;
00062 caption = info.caption;
00063 comment = info.comment;
00064 commentLabel = info.commentLabel;
00065 realmValue = info.realmValue;
00066 digestInfo = info.digestInfo;
00067 verifyPath = info.verifyPath;
00068 readOnly = info.readOnly;
00069 keepPassword = info.keepPassword;
00070 modified = info.modified;
00071 return *this;
00072 }
00073
00074 QDataStream& KIO::operator<< (QDataStream& s, const AuthInfo& a)
00075 {
00076 s << a.url << a.username << a.password << a.prompt << a.caption
00077 << a.comment << a.commentLabel << a.realmValue << a.digestInfo
00078 << Q_UINT8(a.verifyPath ? 1:0) << Q_UINT8(a.readOnly ? 1:0)
00079 << Q_UINT8(a.keepPassword ? 1:0) << Q_UINT8(a.modified ? 1:0);
00080 return s;
00081 }
00082
00083 QDataStream& KIO::operator>> (QDataStream& s, AuthInfo& a)
00084 {
00085 Q_UINT8 verify = 0;
00086 Q_UINT8 ro = 0;
00087 Q_UINT8 keep = 0;
00088 Q_UINT8 mod = 0;
00089
00090 s >> a.url >> a.username >> a.password >> a.prompt >> a.caption
00091 >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
00092 >> verify >> ro >> keep >> mod;
00093 a.verifyPath = (verify != 0);
00094 a.readOnly = (ro != 0);
00095 a.keepPassword = (keep != 0);
00096 a.modified = (mod != 0);
00097 return s;
00098 }
00099
00100
00101 NetRC* NetRC::instance = 0L;
00102
00103 NetRC::NetRC()
00104 {
00105 isDirty = false;
00106 }
00107
00108 NetRC::~NetRC()
00109 {
00110 delete instance;
00111 instance = 0L;
00112 }
00113
00114 NetRC* NetRC::self()
00115 {
00116 if ( !instance )
00117 instance = new NetRC();
00118 return instance;
00119 }
00120
00121 bool NetRC::lookup( const KURL& url, AutoLogin& login, bool userealnetrc,
00122 QString type, int mode )
00123 {
00124
00125 if ( !url.isValid() )
00126 return false;
00127
00128 if ( type.isEmpty() )
00129 type = url.protocol();
00130
00131 if ( loginMap.isEmpty() || isDirty )
00132 {
00133 int fd;
00134 loginMap.clear();
00135 QString filename = locateLocal("config", "kionetrc");
00136 bool status=((fd=openf(filename)) != -1);
00137 if ( status )
00138 parse( fd );
00139
00140 if ( userealnetrc )
00141 {
00142 filename = QDir::homeDirPath()+ QDir::separator() + ".netrc";
00143 bool hasnetrc = ((fd=openf(filename)) != -1);
00144 if ( hasnetrc )
00145 parse( fd );
00146 status |= hasnetrc;
00147 }
00148 close( fd );
00149 if ( !status )
00150 return false;
00151 }
00152
00153 if ( !loginMap.contains( type ) )
00154 return false;
00155
00156 LoginList l = loginMap[type];
00157 if ( l.isEmpty() )
00158 return false;
00159
00160 LoginList::Iterator it = l.begin();
00161 for ( ; it != l.end(); ++it )
00162 {
00163 AutoLogin &log = *it;
00164
00165 if ( (mode & defaultOnly) == defaultOnly &&
00166 log.machine == QString::fromLatin1("default") &&
00167 (login.login.isEmpty() || login.login == log.login) )
00168 {
00169 login.type = log.type;
00170 login.machine = log.machine;
00171 login.login = log.login;
00172 login.password = log.password;
00173 login.macdef = log.macdef;
00174 }
00175
00176 if ( (mode & presetOnly) == presetOnly &&
00177 log.machine == QString::fromLatin1("preset") &&
00178 (login.login.isEmpty() || login.login == log.login) )
00179 {
00180 login.type = log.type;
00181 login.machine = log.machine;
00182 login.login = log.login;
00183 login.password = log.password;
00184 login.macdef = log.macdef;
00185 }
00186
00187 if ( (mode & exactOnly) == exactOnly &&
00188 log.machine == url.host() &&
00189 (login.login.isEmpty() || login.login == log.login) )
00190 {
00191 login.type = log.type;
00192 login.machine = log.machine;
00193 login.login = log.login;
00194 login.password = log.password;
00195 login.macdef = log.macdef;
00196 break;
00197 }
00198 }
00199 return true;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 int NetRC::openf( const QString& f )
00238 {
00239 struct stat sbuff;
00240 QCString ef = QFile::encodeName(f);
00241 if ( stat(ef, &sbuff) != 0 )
00242 return -1;
00243
00244
00245 if ( sbuff.st_mode != (S_IFREG|S_IRUSR|S_IWUSR) ||
00246 sbuff.st_uid != geteuid() )
00247 return -1;
00248
00249 return open( ef, O_RDONLY );
00250 }
00251
00252 QString NetRC::extract( const char* buf, const char* key, int& pos )
00253 {
00254 int idx = pos;
00255 int m_len = strlen(key);
00256 int b_len = strlen(buf);
00257
00258 while( idx < b_len )
00259 {
00260 while( buf[idx] == ' ' || buf[idx] == '\t' ) idx++;
00261 if ( strncasecmp( buf+idx, key, m_len ) == 0 )
00262 {
00263 idx += m_len;
00264 while( buf[idx] == ' ' || buf[idx] == '\t' ) idx++;
00265 int start = idx;
00266 while( buf[idx] != ' ' && buf[idx] != '\t' &&
00267 buf[idx] != '\n' && buf[idx] != '\r' ) idx++;
00268 if ( idx > start )
00269 {
00270 pos = idx;
00271 return QString::fromLatin1( buf+start, idx-start);
00272 }
00273 }
00274 else
00275 idx++;
00276 }
00277 return QString::null;
00278 }
00279
00280 void NetRC::parse( int fd )
00281 {
00282
00283 uint index = 0;
00284 QString macro, type;
00285 bool isMacro = false;
00286 char* buf = new char[NETRC_READ_BUF_SIZE];
00287 FILE* fstream = fdopen( fd,"rb" );
00288
00289 while ( fgets( buf, NETRC_READ_BUF_SIZE, fstream ) != 0L )
00290 {
00291 int pos = 0;
00292 while ( buf[pos] == ' ' || buf[pos] == '\t' ) pos++;
00293 if ( buf[pos] == '#' || buf[pos] == '\n' ||
00294 buf[pos] == '\r' || buf[pos] == '\0' )
00295 {
00296 if ( buf[pos] != '#' && isMacro )
00297 isMacro = false;
00298 continue;
00299 }
00300
00301 if ( isMacro )
00302 {
00303 int tail = strlen(buf);
00304 while( buf[tail-1] == '\n' || buf[tail-1] =='\r' ) tail--;
00305 QString mac = QString::fromLatin1(buf, tail).stripWhiteSpace();
00306
00307 if ( !mac.isEmpty() )
00308 {
00309 loginMap[type][index].macdef[macro].append( mac );
00310
00311 }
00312 continue;
00313 }
00314
00315 AutoLogin l;
00316 l.machine = extract( buf, "machine", pos );
00317 if ( l.machine.isEmpty() )
00318 {
00319 if (strncasecmp(buf+pos, "default", 7) == 0 )
00320 {
00321 pos += 7;
00322 l.machine = QString::fromLatin1("default");
00323 }
00324 else if (strncasecmp(buf+pos, "preset", 6) == 0 )
00325 {
00326 pos += 6;
00327 l.machine = QString::fromLatin1("preset");
00328 }
00329 }
00330
00331
00332 l.login = extract( buf, "login", pos );
00333
00334
00335 l.password = extract( buf, "password", pos );
00336 if ( l.password.isEmpty() )
00337 l.password = extract( buf, "account", pos );
00338
00339
00340 type = l.type = extract( buf, "type", pos );
00341 if ( l.type.isEmpty() && !l.machine.isEmpty() )
00342 type = l.type = QString::fromLatin1("ftp");
00343
00344
00345 macro = extract( buf, "macdef", pos );
00346 isMacro = !macro.isEmpty();
00347
00348
00349 loginMap[l.type].append(l);
00350 index = loginMap[l.type].count()-1;
00351 }
00352 delete [] buf;
00353 }
This file is part of the documentation for kdelibs Version 3.1.5.