27 #include <sys/types.h>
33 #include <kstandarddirs.h>
34 #include <ksavefile.h>
35 #include <kstaticdeleter.h>
38 #include "kio/authinfo.h"
40 #define NETRC_READ_BUF_SIZE 4096
71 modified = info.modified;
75 TQDataStream& KIO::operator<< (TQDataStream& s,
const AuthInfo& a)
80 << TQ_UINT8(a.
keepPassword ? 1:0) << TQ_UINT8(a.modified ? 1:0);
84 TQDataStream& KIO::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;