• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

  • tdeio
  • tdeio
authinfo.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2000-2001 Dawit Alemayehu <adawit@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include <config.h>
22 
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 
29 #include <tqdir.h>
30 #include <tqfile.h>
31 
32 #include <kdebug.h>
33 #include <kstandarddirs.h>
34 #include <ksavefile.h>
35 #include <kstaticdeleter.h>
36 #include <kde_file.h>
37 
38 #include "tdeio/authinfo.h"
39 
40 #define NETRC_READ_BUF_SIZE 4096
41 
42 using namespace TDEIO;
43 
44 AuthInfo::AuthInfo()
45 {
46  modified = false;
47  readOnly = false;
48  verifyPath = false;
49  keepPassword = false;
50 }
51 
52 AuthInfo::AuthInfo( const AuthInfo& info )
53 {
54  (*this) = info;
55 }
56 
57 AuthInfo& AuthInfo::operator= ( const AuthInfo& info )
58 {
59  url = info.url;
60  username = info.username;
61  password = info.password;
62  prompt = info.prompt;
63  caption = info.caption;
64  comment = info.comment;
65  commentLabel = info.commentLabel;
66  realmValue = info.realmValue;
67  digestInfo = info.digestInfo;
68  verifyPath = info.verifyPath;
69  readOnly = info.readOnly;
70  keepPassword = info.keepPassword;
71  modified = info.modified;
72  return *this;
73 }
74 
75 TQDataStream& TDEIO::operator<< (TQDataStream& s, const AuthInfo& a)
76 {
77  s << a.url << a.username << a.password << a.prompt << a.caption
78  << a.comment << a.commentLabel << a.realmValue << a.digestInfo
79  << TQ_UINT8(a.verifyPath ? 1:0) << TQ_UINT8(a.readOnly ? 1:0)
80  << TQ_UINT8(a.keepPassword ? 1:0) << TQ_UINT8(a.modified ? 1:0);
81  return s;
82 }
83 
84 TQDataStream& TDEIO::operator>> (TQDataStream& s, AuthInfo& a)
85 {
86  TQ_UINT8 verify = 0;
87  TQ_UINT8 ro = 0;
88  TQ_UINT8 keep = 0;
89  TQ_UINT8 mod = 0;
90 
91  s >> a.url >> a.username >> a.password >> a.prompt >> a.caption
92  >> a.comment >> a.commentLabel >> a.realmValue >> a.digestInfo
93  >> verify >> ro >> keep >> mod;
94  a.verifyPath = (verify != 0);
95  a.readOnly = (ro != 0);
96  a.keepPassword = (keep != 0);
97  a.modified = (mod != 0);
98  return s;
99 }
100 
101 
102 NetRC* NetRC::instance = 0L;
103 
104 NetRC::NetRC()
105 {
106  isDirty = false;
107 }
108 
109 NetRC::~NetRC()
110 {
111  delete instance;
112  instance = 0L;
113 }
114 
115 NetRC* NetRC::self()
116 {
117  if ( !instance )
118  instance = new NetRC();
119  return instance;
120 }
121 
122 bool NetRC::lookup( const KURL& url, AutoLogin& login, bool userealnetrc,
123  TQString type, int mode )
124 {
125  // kdDebug() << "AutoLogin lookup for: " << url.host() << endl;
126  if ( !url.isValid() )
127  return false;
128 
129  if ( type.isEmpty() )
130  type = url.protocol();
131 
132  if ( loginMap.isEmpty() || isDirty )
133  {
134  loginMap.clear();
135 
136  TQString filename = locateLocal("config", "kionetrc");
137  bool status = parse (openf (filename));
138 
139  if ( userealnetrc )
140  {
141  filename = TQDir::homeDirPath()+ TQDir::separator() + ".netrc";
142  status |= parse (openf(filename));
143  }
144 
145  if ( !status )
146  return false;
147  }
148 
149  if ( !loginMap.contains( type ) )
150  return false;
151 
152  LoginList l = loginMap[type];
153  if ( l.isEmpty() )
154  return false;
155 
156  for (LoginList::Iterator it = l.begin(); it != l.end(); ++it)
157  {
158  AutoLogin &log = *it;
159 
160  if ( (mode & defaultOnly) == defaultOnly &&
161  log.machine == TQString::fromLatin1("default") &&
162  (login.login.isEmpty() || login.login == log.login) )
163  {
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;
169  }
170 
171  if ( (mode & presetOnly) == presetOnly &&
172  log.machine == TQString::fromLatin1("preset") &&
173  (login.login.isEmpty() || login.login == log.login) )
174  {
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;
180  }
181 
182  if ( (mode & exactOnly) == exactOnly &&
183  log.machine == url.host() &&
184  (login.login.isEmpty() || login.login == log.login) )
185  {
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;
191  break;
192  }
193  }
194 
195  return true;
196 }
197 
198 int NetRC::openf( const TQString& f )
199 {
200  KDE_struct_stat sbuff;
201  TQCString ef = TQFile::encodeName(f);
202  if ( KDE_stat(ef, &sbuff) != 0 )
203  return -1;
204 
205  // Security check!!
206  if ( sbuff.st_mode != (S_IFREG|S_IRUSR|S_IWUSR) ||
207  sbuff.st_uid != geteuid() )
208  return -1;
209 
210  return KDE_open( ef, O_RDONLY );
211 }
212 
213 TQString NetRC::extract( const char* buf, const char* key, int& pos )
214 {
215  int idx = pos;
216  int m_len = strlen(key);
217  int b_len = strlen(buf);
218 
219  while( idx < b_len )
220  {
221  while( buf[idx] == ' ' || buf[idx] == '\t' )
222  idx++;
223 
224  if ( strncasecmp( buf+idx, key, m_len ) != 0 )
225  idx++;
226  else
227  {
228  idx += m_len;
229  while( buf[idx] == ' ' || buf[idx] == '\t' )
230  idx++;
231 
232  int start = idx;
233  while( buf[idx] != ' ' && buf[idx] != '\t' &&
234  buf[idx] != '\n' && buf[idx] != '\r' )
235  idx++;
236 
237  if ( idx > start )
238  {
239  pos = idx;
240  return TQString::fromLatin1( buf+start, idx-start);
241  }
242  }
243  }
244 
245  return TQString::null;
246 }
247 
248 bool NetRC::parse( int fd )
249 {
250  if (fd == -1)
251  return false;
252 
253  TQString type;
254  TQString macro;
255 
256  uint index = 0;
257  bool isMacro = false;
258  char* buf = new char[NETRC_READ_BUF_SIZE];
259  FILE* fstream = KDE_fdopen( fd,"rb" );
260 
261  while ( fgets (buf, NETRC_READ_BUF_SIZE, fstream) != 0L )
262  {
263  int pos = 0;
264 
265  while ( buf[pos] == ' ' || buf[pos] == '\t' )
266  pos++;
267 
268  if ( buf[pos] == '#' || buf[pos] == '\n' ||
269  buf[pos] == '\r' || buf[pos] == '\0' )
270  {
271  if ( buf[pos] != '#' && isMacro )
272  isMacro = false;
273 
274  continue;
275  }
276 
277  if ( isMacro )
278  {
279  int tail = strlen(buf);
280  while( buf[tail-1] == '\n' || buf[tail-1] =='\r' )
281  tail--;
282 
283  TQString mac = TQString::fromLatin1(buf, tail).stripWhiteSpace();
284  if ( !mac.isEmpty() )
285  loginMap[type][index].macdef[macro].append( mac );
286 
287  continue;
288  }
289 
290  AutoLogin l;
291  l.machine = extract( buf, "machine", pos );
292  if ( l.machine.isEmpty() )
293  {
294  if (strncasecmp(buf+pos, "default", 7) == 0 )
295  {
296  pos += 7;
297  l.machine = TQString::fromLatin1("default");
298  }
299  else if (strncasecmp(buf+pos, "preset", 6) == 0 )
300  {
301  pos += 6;
302  l.machine = TQString::fromLatin1("preset");
303  }
304  }
305  // kdDebug() << "Machine: " << l.machine << endl;
306 
307  l.login = extract( buf, "login", pos );
308  // kdDebug() << "Login: " << l.login << endl;
309 
310  l.password = extract( buf, "password", pos );
311  if ( l.password.isEmpty() )
312  l.password = extract( buf, "account", pos );
313  // kdDebug() << "Password: " << l.password << endl;
314 
315  type = l.type = extract( buf, "type", pos );
316  if ( l.type.isEmpty() && !l.machine.isEmpty() )
317  type = l.type = TQString::fromLatin1("ftp");
318  // kdDebug() << "Type: " << l.type << endl;
319 
320  macro = extract( buf, "macdef", pos );
321  isMacro = !macro.isEmpty();
322  // kdDebug() << "Macro: " << macro << endl;
323 
324  loginMap[l.type].append(l);
325  index = loginMap[l.type].count()-1;
326  }
327 
328  delete [] buf;
329  fclose (fstream);
330  close (fd);
331  return true;
332 }
TDEIO::AuthInfo
This class is intended to make it easier to prompt for, cache and retrieve authorization information.
Definition: authinfo.h:52
TDEIO::AuthInfo::username
TQString username
This is required for caching.
Definition: authinfo.h:99
TDEIO::AuthInfo::prompt
TQString prompt
Information to be displayed when prompting the user for authentication information.
Definition: authinfo.h:115
TDEIO::AuthInfo::commentLabel
TQString commentLabel
Descriptive label to be displayed in front of the comment when prompting the user for password.
Definition: authinfo.h:159
TDEIO::AuthInfo::operator=
AuthInfo & operator=(const AuthInfo &info)
Overloaded equal to operator.
Definition: authinfo.cpp:57
TDEIO::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
TDEIO::AuthInfo::comment
TQString comment
Additional comment to be displayed when prompting the user for authentication information.
Definition: authinfo.h:150
TDEIO::AuthInfo::AuthInfo
AuthInfo()
Default constructor.
Definition: authinfo.cpp:44
TDEIO::AuthInfo::url
KURL url
The URL for which authentication is to be stored.
Definition: authinfo.h:94
TDEIO::AuthInfo::digestInfo
TQString digestInfo
Field to store any extra authentication information for protocols that need it (ex: http).
Definition: authinfo.h:187
TDEIO::AuthInfo::password
TQString password
This is required for caching.
Definition: authinfo.h:104
TDEIO::AuthInfo::realmValue
TQString realmValue
A unique identifier that allows caching of multiple passwords for different resources in the same ser...
Definition: authinfo.h:177
TDEIO::AuthInfo::keepPassword
bool keepPassword
Flag to indicate the persistence of the given password.
Definition: authinfo.h:220
TDEIO::AuthInfo::caption
TQString caption
The text to displayed in the title bar of the password prompting dialog.
Definition: authinfo.h:126
TDEIO::AuthInfo::readOnly
bool readOnly
Flag which if set forces the username field to be read-only.
Definition: authinfo.h:207
TDEIO::NetRC
A Singleton class that provides access to passwords stored in .netrc files for automatic login purpos...
Definition: authinfo.h:241
TDEIO::NetRC::lookup
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.
Definition: authinfo.cpp:122
TDEIO::NetRC::self
static NetRC * self()
A reference to the instance of the class.
Definition: authinfo.cpp:115
TDEIO
A namespace for TDEIO globals.
Definition: authinfo.h:29
TDEIO::NetRC::AutoLogin
Contains auto login information.
Definition: authinfo.h:266

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.9.1
This website is maintained by Timothy Pearson.