Remoto - VFS: VFS_nopasswd.cpp Source File
Remoto - VFS
VFS_nopasswd.cpp
Go to the documentation of this file.
1 
2 #include "VFS_nopasswd.h"
3 #include "VFS.h"
4 
5 #include <QFile>
6 
34 VFS_nopasswd::VFS_nopasswd(QString nopasswordFile)
35 : VFS_auth()
36 , _nopasswordFile(nopasswordFile)
37 {
38 }
39 
41 {
42 
43 }
44 
60 {
61  QJsonObject d = r->_data.object();
62 
63  bool auth = r->_path == ""; //if the path is empty we're authenticating. Otherwise we're just reading the data.
64 
65  QString user = auth ? d["username"].toString() : r->_path;
66 
67  QFile nopasswd(_nopasswordFile);
68  if (!nopasswd.open(QIODevice::ReadOnly | QIODevice::Text))
69  {
70  r->_reason = QString("(read) Unable to open nopasswd file: '%1'\nAborting.").arg(_nopasswordFile);
71  r->_success = false;
72  return;
73  }
74 
75  QRegExp prx( QString("%1\\s?(\\d*)\\s?\"?([^\"]*)\"?\\s?([^\\s]*)\n").arg(user) );
76  while (!nopasswd.atEnd())
77  {
78  QString line( nopasswd.readLine() );
79  //printf("MATCH: %s || %s\n",qUtf8Printable(line),qUtf8Printable(prx.pattern()));
80  if (prx.exactMatch(line))
81  {
82  //printf("captured %d: %s\n",prx.captureCount(),qUtf8Printable( prx.capturedTexts().join("\n") ));
83  int c = prx.capturedTexts().length();
84 
85  d["username"] = user;
86  d["uidnumber"] = (c > 1) ? prx.cap(1).toInt() : 0;
87  d["realname"] = (c > 2) && prx.cap(2).size() ? prx.cap(2) : user;
88  d["groups"] = (c > 3) ? QJsonArray::fromStringList(prx.cap(3).split(",")) : QJsonArray();
89 
90  //d["type"] = type;
91  //d["authpath"] = d["authpath"];
92  //d["parameters"] = d["parameters"];
93 
94  r->_data.setObject(d);
95  r->_success = true;
96  return;
97  }
98  }
99 
100  r->_success = false;
101 }
102 
103 
110 {
111  //printf("nopasswd LS\n");
112 
113  QFile nopasswd(_nopasswordFile);
114  if (!nopasswd.open(QIODevice::ReadOnly | QIODevice::Text))
115  {
116  r->_reason = QString("(ls) Unable to open nopasswd file: '%1'\nAborting.").arg(_nopasswordFile);
117  r->_success = false;
118  return;
119  }
120 
121  QJsonObject o;
122 
123  QRegExp prx( QString("([^\\s]+)\\s?(\\d*)\\s?\"?([^\"]*)\"?\\s?([^\\s]*)\n") );
124  while (!nopasswd.atEnd())
125  {
126  QString line( nopasswd.readLine() );
127  //printf("MATCH: %s || %s\n",qUtf8Printable(line),qUtf8Printable(prx.pattern()));
128  if (prx.exactMatch(line))
129  {
130  //printf("captured %d: %s\n",prx.captureCount(),qUtf8Printable( prx.capturedTexts().join("\n") ));
131  int c = prx.capturedTexts().length();
132 
133  //printf("C: %d\n",c);
134 
135  switch(c)
136  {
137  case 5:
138  case 4:
139  case 3: {
140  QString username = prx.cap(1);
141  o[username] = QJsonObject { { "username", username }, { "uidnumber", prx.cap(2).toInt() }, { "realname",prx.cap(3).trimmed().size() ? prx.cap(3) : username } };
142  break;
143  }
144 
145  case 2: {
146  QString username = prx.cap(1);
147  o[username] = QJsonObject { { "username", username }, { "uidnumber", prx.cap(2).toInt() }, { "realname", username } };
148  break;
149  }
150 
151  case 1: {
152  QString username = prx.cap(1);
153  o[username] = QJsonObject { { "username", username }, { "uidnumber", 0 }, { "realname", username } };
154  break;
155  }
156 
157  default: break; //don't add the bad line
158  }
159 
160  //if (c>=3)
161  //{
162  // QString username = prx.cap(1);
163  // o[username] = QJsonObject { { "username", username }, { "uidnumber", prx.cap(2).toInt() }, { "realname",prx.cap(3).trimmed().size() ? prx.cap(3) : username } };
164  //}
165  }
166  }
167 
168  QJsonObject u = r->_data.object();
169  //u["nopasswd"] = o;
170  u[r->_initialPath] = o;
171 
172  r->_data.setObject(u);
173  r->_success = true;
174 }
175 
177 {
178  return _nopasswordFile;
179 }
The base class for authenticating users.
Definition: VFS_auth.h:7
virtual void ls(VFS_request *r)
List the users available for this auth method.
Q_INVOKABLE VFS_nopasswd(QString nopasswordFile)
virtual QString reportDetails()
Additional details for a generated report.
virtual ~VFS_nopasswd()
QString _nopasswordFile
The filesystem path to the nopasswd file.
Definition: VFS_nopasswd.h:21
virtual void read(VFS_request *r)
Perform an authentication request by read()ing from this node.
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