QtPass  1.2.3
Multi-platform GUI for pass, the standard unix password manager.
qtpass.cpp
Go to the documentation of this file.
1 #include "qtpass.h"
2 #include "mainwindow.h"
3 #include "qtpasssettings.h"
4 #include <QApplication>
5 #include <QClipboard>
6 
7 #ifndef Q_OS_WIN
8 #include <QInputDialog>
9 #include <QLineEdit>
10 #include <utility>
11 #else
12 #define WIN32_LEAN_AND_MEAN /*_KILLING_MACHINE*/
13 #define WIN32_EXTRA_LEAN
14 #include <windows.h>
15 #include <winnetwk.h>
16 #undef DELETE
17 #endif
18 
19 #ifdef QT_DEBUG
20 #include "debughelper.h"
21 #endif
22 
23 QtPass::QtPass() : clippedText(QString()), freshStart(true) {
24  if (!setup()) {
25  // no working config so this should quit without config anything
26  QApplication::quit();
27  {}
28  }
29 
31  clearClipboardTimer.setSingleShot(true);
32  connect(&clearClipboardTimer, SIGNAL(timeout()), this,
33  SLOT(clearClipboard()));
34 
35  QObject::connect(qApp, &QApplication::aboutToQuit, this,
37 }
38 
43 #ifdef Q_OS_WIN
45  WNetCancelConnection2A(QtPassSettings::getPassStore().toUtf8().constData(),
46  0, 1);
47 #else
48  if (fusedav.state() == QProcess::Running) {
49  fusedav.terminate();
50  fusedav.waitForFinished(2000);
51  }
52 #endif
53 }
54 
59 bool QtPass::setup() {
62 
64 
65  QString version = QtPassSettings::getVersion();
66  // dbg()<< version;
67 
68  // Config updates
69  if (version.isEmpty()) {
70 #ifdef QT_DEBUG
71  dbg() << "assuming fresh install";
72 #endif
73 
78  if (!QtPassSettings::getPwgenExecutable().isEmpty())
80  else
82  QtPassSettings::setPassTemplate("login\nurl");
83  } else {
84  // QStringList ver = version.split(".");
85  // dbg()<< ver;
86  // if (ver[0] == "0" && ver[1] == "8") {
88  // }
89  if (QtPassSettings::getPassTemplate().isEmpty())
90  QtPassSettings::setPassTemplate("login\nurl");
91  }
92 
94 
95  if (Util::checkConfig()) {
96  m_mainWindow->config();
97  if (freshStart && Util::checkConfig())
98  return false;
99  }
100 
101  // TODO(annejan): this needs to be before we try to access the store,
102  // but it would be better to do it after the Window is shown,
103  // as the long delay it can cause is irritating otherwise.
105  mountWebDav();
106 
107  freshStart = false;
108  // startupPhase = false;
109  return true;
110 }
111 
113  m_mainWindow = mW;
114  m_mainWindow->restoreWindow();
115 
116  fusedav.setParent(m_mainWindow);
117 
118  // TODO(bezet): this should be reconnected dynamically when pass changes
119  connectPassSignalHandlers(QtPassSettings::getRealPass());
120  connectPassSignalHandlers(QtPassSettings::getImitatePass());
121 
122  // only for ipass
124  m_mainWindow, &MainWindow::startReencryptPath);
126  m_mainWindow, &MainWindow::endReencryptPath);
127 
128  connect(m_mainWindow, &MainWindow::passGitInitNeeded, [=]() {
129 #ifdef QT_DEBUG
130  dbg() << "Pass git init called";
131 #endif
133  });
134 
135  connect(
136  m_mainWindow, &MainWindow::generateGPGKeyPair, [=](const QString &batch) {
138  m_mainWindow->showStatusMessage(tr("Generating GPG key pair"), 60000);
139  });
140 }
141 
142 void QtPass::connectPassSignalHandlers(Pass *pass) {
143  connect(pass, &Pass::error, this, &QtPass::processError);
144  connect(pass, &Pass::processErrorExit, this, &QtPass::processErrorExit);
145 
146  connect(pass, &Pass::critical, m_mainWindow, &MainWindow::critical);
147  connect(pass, &Pass::startingExecuteWrapper, m_mainWindow,
149  connect(pass, &Pass::statusMsg, m_mainWindow, &MainWindow::showStatusMessage);
150  connect(m_mainWindow, &MainWindow::passShowHandlerFinished, this,
151  &QtPass::passShowHandlerFinished);
152  connect(pass, &Pass::finishedShow, m_mainWindow,
154  connect(pass, &Pass::finishedOtpGenerate, m_mainWindow,
156 
157  connect(pass, &Pass::finishedGitInit, this, &QtPass::passStoreChanged);
158  connect(pass, &Pass::finishedGitPull, this, &QtPass::processFinished);
159  connect(pass, &Pass::finishedGitPush, this, &QtPass::processFinished);
160  connect(pass, &Pass::finishedInsert, this, &QtPass::finishedInsert);
161  connect(pass, &Pass::finishedRemove, this, &QtPass::passStoreChanged);
162  connect(pass, &Pass::finishedInit, this, &QtPass::passStoreChanged);
163  connect(pass, &Pass::finishedMove, this, &QtPass::passStoreChanged);
164  connect(pass, &Pass::finishedCopy, this, &QtPass::passStoreChanged);
165  connect(pass, &Pass::finishedGenerateGPGKeys, this,
166  &QtPass::onKeyGenerationComplete);
167 }
168 
172 void QtPass::mountWebDav() {
173 #ifdef Q_OS_WIN
174  char dst[20] = {0};
175  NETRESOURCEA netres;
176  memset(&netres, 0, sizeof(netres));
177  netres.dwType = RESOURCETYPE_DISK;
178  netres.lpLocalName = 0;
179  netres.lpRemoteName = QtPassSettings::getWebDavUrl().toUtf8().data();
180  DWORD size = sizeof(dst);
181  DWORD r = WNetUseConnectionA(
182  reinterpret_cast<HWND>(m_mainWindow->effectiveWinId()), &netres,
183  QtPassSettings::getWebDavPassword().toUtf8().constData(),
184  QtPassSettings::getWebDavUser().toUtf8().constData(),
185  CONNECT_TEMPORARY | CONNECT_INTERACTIVE | CONNECT_REDIRECT, dst, &size,
186  0);
187  if (r == NO_ERROR) {
189  } else {
190  char message[256] = {0};
191  FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, r, 0, message,
192  sizeof(message), 0);
193  m_mainWindow->flashText(tr("Failed to connect WebDAV:\n") + message +
194  " (0x" + QString::number(r, 16) + ")",
195  true);
196  }
197 #else
198  fusedav.start("fusedav -o nonempty -u \"" + QtPassSettings::getWebDavUser() +
199  "\" " + QtPassSettings::getWebDavUrl() + " \"" +
201  fusedav.waitForStarted();
202  if (fusedav.state() == QProcess::Running) {
203  QString pwd = QtPassSettings::getWebDavPassword();
204  bool ok = true;
205  if (pwd.isEmpty()) {
206  pwd = QInputDialog::getText(m_mainWindow, tr("QtPass WebDAV password"),
207  tr("Enter password to connect to WebDAV:"),
208  QLineEdit::Password, "", &ok);
209  }
210  if (ok && !pwd.isEmpty()) {
211  fusedav.write(pwd.toUtf8() + '\n');
212  fusedav.closeWriteChannel();
213  fusedav.waitForFinished(2000);
214  } else {
215  fusedav.terminate();
216  }
217  }
218  QString error = fusedav.readAllStandardError();
219  int prompt = error.indexOf("Password:");
220  if (prompt >= 0)
221  error.remove(0, prompt + 10);
222  if (fusedav.state() != QProcess::Running)
223  error = tr("fusedav exited unexpectedly\n") + error;
224  if (error.size() > 0) {
225  m_mainWindow->flashText(
226  tr("Failed to start fusedav to connect WebDAV:\n") + error, true);
227  }
228 #endif
229 }
230 
235 void QtPass::processError(QProcess::ProcessError error) {
236  QString errorString;
237  switch (error) {
238  case QProcess::FailedToStart:
239  errorString = tr("QProcess::FailedToStart");
240  break;
241  case QProcess::Crashed:
242  errorString = tr("QProcess::Crashed");
243  break;
244  case QProcess::Timedout:
245  errorString = tr("QProcess::Timedout");
246  break;
247  case QProcess::ReadError:
248  errorString = tr("QProcess::ReadError");
249  break;
250  case QProcess::WriteError:
251  errorString = tr("QProcess::WriteError");
252  break;
253  case QProcess::UnknownError:
254  errorString = tr("QProcess::UnknownError");
255  break;
256  }
257 
258  m_mainWindow->flashText(errorString, true);
259  m_mainWindow->setUiElementsEnabled(true);
260 }
261 
262 void QtPass::processErrorExit(int exitCode, const QString &p_error) {
263  if (!p_error.isEmpty()) {
264  QString output;
265  QString error = p_error;
266  error.replace(QRegExp("<"), "&lt;");
267  error.replace(QRegExp(">"), "&gt;");
268  error.replace(QRegExp(" "), "&nbsp;");
269  if (exitCode == 0) {
270  // https://github.com/IJHack/qtpass/issues/111
271  output = "<span style=\"color: darkgray;\">" + error + "</span><br />";
272  } else {
273  output = "<span style=\"color: red;\">" + error + "</span><br />";
274  }
275 
276  output.replace(
277  QRegExp("((?:https?|ftp|ssh|sftp|ftps|webdav|webdavs)://\\S+)"),
278  R"(<a href="\1">\1</a>)");
279  output.replace(QRegExp("\n"), "<br />");
280 
281  m_mainWindow->flashText(output, false, true);
282  }
283 
284  m_mainWindow->setUiElementsEnabled(true);
285 }
286 
294 void QtPass::processFinished(const QString &p_output, const QString &p_errout) {
295  showInTextBrowser(p_output);
296  // Sometimes there is error output even with 0 exit code, which is
297  // assumed in this function
298  processErrorExit(0, p_errout);
299 
300  m_mainWindow->setUiElementsEnabled(true);
301 }
302 
303 void QtPass::passStoreChanged(const QString &p_out, const QString &p_err) {
304  processFinished(p_out, p_err);
305  doGitPush();
306 }
307 
308 void QtPass::finishedInsert(const QString &p_output, const QString &p_errout) {
309  processFinished(p_output, p_errout);
310  doGitPush();
311  m_mainWindow->on_treeView_clicked(m_mainWindow->getCurrentTreeViewIndex());
312 }
313 
314 void QtPass::onKeyGenerationComplete(const QString &p_output,
315  const QString &p_errout) {
316  if (nullptr != m_mainWindow->getKeygenDialog()) {
317 #ifdef QT_DEBUG
318  qDebug() << "Keygen Done";
319 #endif
320 
321  m_mainWindow->cleanKeygenDialog();
322  // TODO(annejan) some sanity checking ?
323  }
324 
325  processFinished(p_output, p_errout);
326 }
327 
328 void QtPass::passShowHandlerFinished(QString output) {
329  showInTextBrowser(std::move(output));
330 }
331 
332 void QtPass::showInTextBrowser(QString output, QString prefix,
333  QString postfix) {
334  output.replace(QRegExp("<"), "&lt;");
335  output.replace(QRegExp(">"), "&gt;");
336  output.replace(QRegExp(" "), "&nbsp;");
337 
338  output.replace(
339  QRegExp("((?:https?|ftp|ssh|sftp|ftps|webdav|webdavs)://\\S+)"),
340  R"(<a href="\1">\1</a>)");
341  output.replace(QRegExp("\n"), "<br />");
342  output = prefix + output + postfix;
343 
344  m_mainWindow->flashText(output, false, true);
345 }
346 
347 void QtPass::doGitPush() {
349  m_mainWindow->onPush();
350 }
351 
352 void QtPass::setClippedText(const QString &password, const QString &p_output) {
354  !p_output.isEmpty()) {
355  clippedText = password;
357  copyTextToClipboard(password);
358  }
359 }
360 void QtPass::clearClippedText() { clippedText = ""; }
361 
363  clearClipboardTimer.setInterval(1000 * QtPassSettings::getAutoclearSeconds());
364 }
365 
370  QClipboard *clipboard = QApplication::clipboard();
371  bool cleared = false;
372  if (this->clippedText == clipboard->text(QClipboard::Selection)) {
373  clipboard->clear(QClipboard::Clipboard);
374  cleared = true;
375  }
376  if (this->clippedText == clipboard->text(QClipboard::Clipboard)) {
377  clipboard->clear(QClipboard::Clipboard);
378  cleared = true;
379  }
380  if (cleared) {
381  m_mainWindow->showStatusMessage(tr("Clipboard cleared"));
382  } else {
383  m_mainWindow->showStatusMessage(tr("Clipboard not cleared"));
384  }
385 
386  clippedText.clear();
387 }
388 
393 void QtPass::copyTextToClipboard(const QString &text) {
394  QClipboard *clip = QApplication::clipboard();
396  clip->setText(text, QClipboard::Clipboard);
397  } else {
398  clip->setText(text, QClipboard::Selection);
399  }
400 
401  clippedText = text;
402  m_mainWindow->showStatusMessage(tr("Copied to clipboard"));
404  clearClipboardTimer.start();
405  }
406 }
void finishedMove(const QString &, const QString &)
void passGitInitNeeded()
static void setAutoclearSeconds(const int &autoClearSeconds)
static int getAutoclearSeconds(const int &defaultValue=QVariant().toInt())
static void setUsePwgen(const bool &usePwgen)
const QModelIndex getCurrentTreeViewIndex()
Definition: mainwindow.cpp:197
void finishedCopy(const QString &, const QString &)
void generateGPGKeyPair(QString batch)
void on_treeView_clicked(const QModelIndex &index)
MainWindow::on_treeView_clicked read the selected password file.
Definition: mainwindow.cpp:319
void GenerateGPGKeys(QString batch)
Pass::GenerateGPGKeys internal gpg keypair generator . .
Definition: pass.cpp:113
void critical(QString, QString)
MainWindow::critical critical message popup wrapper.
void finishedGenerateGPGKeys(const QString &, const QString &)
void finishedGitPush(const QString &, const QString &)
void error(QProcess::ProcessError)
#define dbg()
Definition: debughelper.h:7
void passShowHandlerFinished(QString output)
static bool isUseSelection(const bool &defaultValue=QVariant().toBool())
static void setPassTemplate(const QString &passTemplate)
void finishedInsert(const QString &, const QString &)
QDialog * getKeygenDialog()
Definition: mainwindow.h:59
void restoreWindow()
Definition: mainwindow.cpp:460
static ImitatePass * getImitatePass()
void clearClippedText()
Definition: qtpass.cpp:360
static bool isUseWebDav(const bool &defaultValue=QVariant().toBool())
void passShowHandler(const QString &)
Definition: mainwindow.cpp:367
void setUiElementsEnabled(bool state)
MainWindow::setUiElementsEnabled enable or disable the relevant UI elements.
Definition: mainwindow.cpp:444
void finishedRemove(const QString &, const QString &)
static bool isUseAutoclear(const bool &defaultValue=QVariant().toBool())
virtual void GitInit()=0
void config()
MainWindow::config pops up the configuration screen and handles all inter-window communication.
Definition: mainwindow.cpp:230
Acts as an abstraction for pass or pass imitation.
Definition: pass.h:25
static QString getPassStore(const QString &defaultValue=QVariant().toString())
void setClipboardTimer()
Definition: qtpass.cpp:362
void finishedGitPull(const QString &, const QString &)
static void setAutoclearPanelSeconds(const int &autoClearPanelSeconds)
void critical(QString, QString)
void setClippedText(const QString &, const QString &p_output=QString())
Definition: qtpass.cpp:352
void flashText(const QString &text, const bool isError, const bool isHtml=false)
Definition: mainwindow.cpp:210
static QString getVersion(const QString &defaultValue=QVariant().toString())
void cleanKeygenDialog()
Definition: mainwindow.cpp:201
void startReencryptPath()
~QtPass()
QtPass::~QtPass destroy!
Definition: qtpass.cpp:42
static Pass * getPass()
void statusMsg(QString, int)
static QString getWebDavUser(const QString &defaultValue=QVariant().toString())
void finishedInit(const QString &, const QString &)
void endReencryptPath()
MainWindow::endReencryptPath re-enable ui elements.
void copyTextToClipboard(const QString &text)
MainWindow::copyTextToClipboard copies text to your clipboard.
Definition: qtpass.cpp:393
static QString getPassTemplate(const QString &defaultValue=QVariant().toString())
static Enums::clipBoardType getClipBoardType(const Enums::clipBoardType &defaultvalue=Enums::CLIPBOARD_NEVER)
static QString getWebDavUrl(const QString &defaultValue=QVariant().toString())
void onPush()
MainWindow::onPush do a git push.
Definition: mainwindow.cpp:289
static QString getPwgenExecutable(const QString &defaultValue=QVariant().toString())
void passOtpHandler(const QString &)
Definition: mainwindow.cpp:411
void startReencryptPath()
MainWindow::startReencryptPath disable ui elements and treeview.
static int getAutoclearPanelSeconds(const int &defaultValue=QVariant().toInt())
void finishedShow(const QString &)
void showStatusMessage(QString msg, int timeout=2000)
Displays message in status bar.
static QString getWebDavPassword(const QString &defaultValue=QVariant().toString())
static void initExecutables()
void finishedGitInit(const QString &, const QString &)
void startingExecuteWrapper()
void processErrorExit(int exitCode, const QString &err)
void finishedOtpGenerate(const QString &)
The MainWindow class does way too much, not only is it a switchboard, configuration handler and more...
Definition: mainwindow.h:39
void clearClipboard()
MainWindow::clearClipboard remove clipboard contents.
Definition: qtpass.cpp:369
static RealPass * getRealPass()
static QString findPasswordStore()
Util::findPasswordStore look for common .password-store folder location.
Definition: util.cpp:45
static bool checkConfig()
Util::checkConfig do we have prequisite settings?
Definition: util.cpp:121
static void setVersion(const QString &version)
void setMainWindow(MainWindow *mW)
Definition: qtpass.cpp:112
void executeWrapperStarted()
Definition: mainwindow.cpp:360
static void setPassStore(const QString &passStore)
QtPass()
Definition: qtpass.cpp:23
void endReencryptPath()
static bool isAutoPush(const bool &defaultValue=QVariant().toBool())