27 #include <sys/types.h>
33 #include <kstandarddirs.h>
34 #include <ksavefile.h>
35 #include <kstaticdeleter.h>
38 #include "tdeio/authinfo.h"
40 #define NETRC_READ_BUF_SIZE 4096
42 using namespace TDEIO;
71 modified = info.modified;
75 TQDataStream& TDEIO::operator<< (TQDataStream& s,
const AuthInfo& a)
80 << TQ_UINT8(a.
keepPassword ? 1:0) << TQ_UINT8(a.modified ? 1:0);
84 TQDataStream& TDEIO::operator>> (TQDataStream& s,
AuthInfo& a)
93 >> verify >> ro >> keep >> mod;
97 a.modified = (mod != 0);
102 NetRC* NetRC::instance = 0L;
118 instance =
new NetRC();
123 TQString type,
int mode )
126 if ( !url.isValid() )
129 if ( type.isEmpty() )
130 type = url.protocol();
132 if ( loginMap.isEmpty() || isDirty )
136 TQString filename = locateLocal(
"config",
"kionetrc");
137 bool status = parse (openf (filename));
141 filename = TQDir::homeDirPath()+ TQDir::separator() +
".netrc";
142 status |= parse (openf(filename));
149 if ( !loginMap.contains( type ) )
152 LoginList l = loginMap[type];
156 for (LoginList::Iterator it = l.begin(); it != l.end(); ++it)
160 if ( (mode & defaultOnly) == defaultOnly &&
161 log.machine == TQString::fromLatin1(
"default") &&
162 (login.login.isEmpty() || login.login == log.login) )
164 login.type = log.type;
165 login.machine = log.machine;
166 login.login = log.login;
167 login.password = log.password;
168 login.macdef = log.macdef;
171 if ( (mode & presetOnly) == presetOnly &&
172 log.machine == TQString::fromLatin1(
"preset") &&
173 (login.login.isEmpty() || login.login == log.login) )
175 login.type = log.type;
176 login.machine = log.machine;
177 login.login = log.login;
178 login.password = log.password;
179 login.macdef = log.macdef;
182 if ( (mode & exactOnly) == exactOnly &&
183 log.machine == url.host() &&
184 (login.login.isEmpty() || login.login == log.login) )
186 login.type = log.type;
187 login.machine = log.machine;
188 login.login = log.login;
189 login.password = log.password;
190 login.macdef = log.macdef;
198 int NetRC::openf(
const TQString& f )
200 KDE_struct_stat sbuff;
201 TQCString ef = TQFile::encodeName(f);
202 if ( KDE_stat(ef, &sbuff) != 0 )
206 if ( sbuff.st_mode != (S_IFREG|S_IRUSR|S_IWUSR) ||
207 sbuff.st_uid != geteuid() )
210 return KDE_open( ef, O_RDONLY );
213 TQString NetRC::extract(
const char* buf,
const char* key,
int& pos )
216 int m_len = strlen(key);
217 int b_len = strlen(buf);
221 while( buf[idx] ==
' ' || buf[idx] ==
'\t' )
224 if ( strncasecmp( buf+idx, key, m_len ) != 0 )
229 while( buf[idx] ==
' ' || buf[idx] ==
'\t' )
233 while( buf[idx] !=
' ' && buf[idx] !=
'\t' &&
234 buf[idx] !=
'\n' && buf[idx] !=
'\r' )
240 return TQString::fromLatin1( buf+start, idx-start);
245 return TQString::null;
248 bool NetRC::parse(
int fd )
257 bool isMacro =
false;
258 char* buf =
new char[NETRC_READ_BUF_SIZE];
259 FILE* fstream = KDE_fdopen( fd,
"rb" );
261 while ( fgets (buf, NETRC_READ_BUF_SIZE, fstream) != 0L )
265 while ( buf[pos] ==
' ' || buf[pos] ==
'\t' )
268 if ( buf[pos] ==
'#' || buf[pos] ==
'\n' ||
269 buf[pos] ==
'\r' || buf[pos] ==
'\0' )
271 if ( buf[pos] !=
'#' && isMacro )
279 int tail = strlen(buf);
280 while( buf[tail-1] ==
'\n' || buf[tail-1] ==
'\r' )
283 TQString mac = TQString::fromLatin1(buf, tail).stripWhiteSpace();
284 if ( !mac.isEmpty() )
285 loginMap[type][index].macdef[macro].append( mac );
291 l.machine = extract( buf,
"machine", pos );
292 if ( l.machine.isEmpty() )
294 if (strncasecmp(buf+pos,
"default", 7) == 0 )
297 l.machine = TQString::fromLatin1(
"default");
299 else if (strncasecmp(buf+pos,
"preset", 6) == 0 )
302 l.machine = TQString::fromLatin1(
"preset");
307 l.login = extract( buf,
"login", pos );
310 l.password = extract( buf,
"password", pos );
311 if ( l.password.isEmpty() )
312 l.password = extract( buf,
"account", pos );
315 type = l.type = extract( buf,
"type", pos );
316 if ( l.type.isEmpty() && !l.machine.isEmpty() )
317 type = l.type = TQString::fromLatin1(
"ftp");
320 macro = extract( buf,
"macdef", pos );
321 isMacro = !macro.isEmpty();
324 loginMap[l.type].append(l);
325 index = loginMap[l.type].count()-1;
This class is intended to make it easier to prompt for, cache and retrieve authorization information.
TQString username
This is required for caching.
TQString prompt
Information to be displayed when prompting the user for authentication information.
TQString commentLabel
Descriptive label to be displayed in front of the comment when prompting the user for password.
AuthInfo & operator=(const AuthInfo &info)
Overloaded equal to operator.
bool verifyPath
Flag that, if set, indicates whether a path match should be performed when requesting for cached auth...
TQString comment
Additional comment to be displayed when prompting the user for authentication information.
AuthInfo()
Default constructor.
KURL url
The URL for which authentication is to be stored.
TQString digestInfo
Field to store any extra authentication information for protocols that need it (ex: http).
TQString password
This is required for caching.
TQString realmValue
A unique identifier that allows caching of multiple passwords for different resources in the same ser...
bool keepPassword
Flag to indicate the persistence of the given password.
TQString caption
The text to displayed in the title bar of the password prompting dialog.
bool readOnly
Flag which if set forces the username field to be read-only.
A Singleton class that provides access to passwords stored in .netrc files for automatic login purpos...
bool lookup(const KURL &url, AutoLogin &login, bool userealnetrc=false, TQString type=TQString::null, int mode=(exactOnly|defaultOnly))
Looks up the login information for the given url.
static NetRC * self()
A reference to the instance of the class.
A namespace for TDEIO globals.
Contains auto login information.