Remoto - VFS: VFS_passwd.cpp Source File
Remoto - VFS
VFS_passwd.cpp
Go to the documentation of this file.
1 
2 #include "VFS_passwd.h"
3 #include "VFS.h"
4 
5 #include <QCryptographicHash>
6 #include <QFile>
7 
37 VFS_passwd::VFS_passwd(QString passwdFile)
38 : VFS_auth()
39 , _passwordFile(passwdFile)
40 {
41 }
42 
44 {
45 }
46 
64 {
65  QJsonObject d = r->_data.object();
66 
67  bool auth = r->_path == ""; //if the path is empty we're authenticating. Otherwise we're just reading the data.
68 
69  QString user = auth ? d["username"].toString("") : r->_path;
70  QString pass = d["password"].toString("");
71  //QString hash = auth ? QCryptographicHash::hash(pass.toLocal8Bit(),QCryptographicHash::Md5).toHex() : "[0-9A-F]+";
72  QString hash = auth ? QCryptographicHash::hash(pass.toUtf8(),QCryptographicHash::Md5).toHex() : "[0-9A-F]+";
73 
74  QFile passwd(_passwordFile);
75  if (!passwd.open(QIODevice::ReadOnly | QIODevice::Text))
76  {
77  r->_reason = QString("(read) Unable to open passwd file: '%1'\nAborting.").arg(_passwordFile);
78  r->_success = false;
79  return;
80  }
81 
82  QRegExp prx( QString("%1 %2 ?(\\d*) ?\"?([^\"]*)\"?\\s?([^\\s]*)\n").arg(user).arg(hash), Qt::CaseInsensitive );
83  while (!passwd.atEnd())
84  {
85  QString line( passwd.readLine() );
86  //printf("MATCH: %s || %s\n",qUtf8Printable(line),qUtf8Printable(prx.pattern()));
87  if (prx.exactMatch(line))
88  {
89  //printf("captured: %s\n",qUtf8Printable( prx.capturedTexts().join("\n") ));
90  int c = prx.capturedTexts().length();
91 
92  d["username"] = user;
93  d["uidnumber"] = c > 1 ? prx.cap(1).toInt() : 0;
94  d["realname"] = c > 2 && prx.cap(2).size() ? prx.cap(2) : user;
95  d["groups"] = (c > 3) ? QJsonArray::fromStringList(prx.cap(3).split(",")) : QJsonArray();
96 
97  //d["type"] = type;
98  //d["authpath"] = d["authpath"];
99  //d["parameters"] = d["parameters"];
100 
101  r->_data.setObject(d);
102  r->_success = true;
103  return;
104  }
105  }
106 
107  //VFS::ERROR( QString("Authentication failed for user '%1:%2'").arg(user).arg(pass), 0, "passwd" );
108  //VFS::ERROR( QString("Authentication failed for user '%1'").arg(user), 0, "passwd" );
109 
110  r->_success = false;
111 }
112 
113 
120 {
121  //printf("passwd LS\n");
122 
123  QFile passwd(_passwordFile);
124  if (!passwd.open(QIODevice::ReadOnly | QIODevice::Text))
125  {
126  r->_reason = QString("(ls) Unable to open passwd file: '%1'\nAborting.").arg(_passwordFile);
127  r->_success = false;
128  return;
129  }
130 
131  QJsonObject o;
132 
133  QRegExp prx( QString("([^\\s]*) ([^\\s]*) ?(\\d*) ?\"?([^\"]*)\"?\\s?([^\\s]*)\n") );
134  while (!passwd.atEnd())
135  {
136  QString line( passwd.readLine() );
137  //printf("MATCH: %s || %s\n",qUtf8Printable(line),qUtf8Printable(prx.pattern()));
138  if (prx.exactMatch(line))
139  {
140  //printf("captured %d: %s\n",prx.captureCount(),qUtf8Printable( prx.capturedTexts().join("\n") ));
141  int c = prx.capturedTexts().length();
142 
143  switch(c)
144  {
145  case 6:
146  case 5:
147  case 4: {
148  QString username = prx.cap(1);
149  o[username] = QJsonObject { { "username", username }, { "uidnumber", prx.cap(3).toInt() }, { "realname", prx.cap(4).trimmed().size() ? prx.cap(4) : username } };
150  break;
151  }
152 
153  case 3: { QString username = prx.cap(1);
154  o[username] = QJsonObject { { "username", username }, { "uidnumber", prx.cap(3).toInt() }, { "realname", username } };
155  break;
156  }
157 
158  case 2: { QString username = prx.cap(1);
159  o[username] = QJsonObject { { "username", username }, { "uidnumber", 0 }, { "realname", username } };
160  break;
161  }
162 
163  case 1:
164  default: break; //don't add the bad line
165  }
166 
167  //if (c>=4)
168  //{
169  // QString username = prx.cap(1);
170  // o[username] = QJsonObject { { "username", username }, { "uidnumber", prx.cap(3).toInt() }, { "realname", prx.cap(4).trimmed().size() ? prx.cap(4) : username } };
171  //}
172  }
173  }
174 
175  QJsonObject u = r->_data.object(); //add to the previous object, don't replace it
176  //u["passwd"] = o;
177  u[r->_initialPath] = o;
178 
179  r->_data.setObject(u);
180  r->_success = true;
181 }
182 
184 {
185  return _passwordFile;
186 }
The base class for authenticating users.
Definition: VFS_auth.h:7
virtual ~VFS_passwd()
Definition: VFS_passwd.cpp:43
QString _passwordFile
The filesystem path to the passwd file.
Definition: VFS_passwd.h:21
virtual void ls(VFS_request *r)
List the users available for this auth method.
Definition: VFS_passwd.cpp:119
Q_INVOKABLE VFS_passwd(QString file)
Definition: VFS_passwd.cpp:37
virtual QString reportDetails()
Additional details for a generated report.
Definition: VFS_passwd.cpp:183
virtual void read(VFS_request *r)
Perform an authentication request by read()ing from this node.
Definition: VFS_passwd.cpp:63
The base class for all requests between nodes.
Definition: VFS_node.h:54
QString _initialPath
the target path when the request was made (relative to the responder)
Definition: VFS_node.h:93
QString _reason
if something (probably bad) happened, this is the reason
Definition: VFS_node.h:108
QString _path
the target path remnant... the remaining path element once the request has found its target
Definition: VFS_node.h:95
bool _success
if the request was successfully completed
Definition: VFS_node.h:107
QJsonDocument _data
the request payload
Definition: VFS_node.h:102