PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/openconnect-vpnd/main.cpp

https://bitbucket.org/devonit/qconnman
C++ | 132 lines | 98 code | 17 blank | 17 comment | 14 complexity | 1bb3cd03fb80cf691d97500e11c58d67 MD5 | raw file
  1. /*
  2. * Copyright (C) 2012 Matt Broadstone
  3. * Contact: http://bitbucket.org/devonit/qconnman
  4. *
  5. * This file is part of the QConnman Library.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <QCoreApplication>
  18. #include <QProcess>
  19. #include <QUrl>
  20. #include <QDebug>
  21. #include "vpn/vpnconnection.h"
  22. #include "vpn/vpnproviders.h"
  23. #include "vpn/vpnmanager.h"
  24. QVariantMap openConnectHelper(const QString &host, const QString &domain,
  25. const QString &user, const QString &password)
  26. {
  27. QStringList arguments;
  28. arguments << "--authenticate"
  29. << "--authgroup" << domain
  30. << "--user" << user
  31. << "--no-cert-check"
  32. << "--no-xmlpost" // this is legacy, but required for testing on my ubuntu box
  33. << host;
  34. QProcess openConnect;
  35. openConnect.setReadChannel(QProcess::StandardError);
  36. openConnect.start("openconnect", arguments);
  37. if (!openConnect.waitForStarted()) {
  38. qDebug() << "unable to start openconnect binary";
  39. return QVariantMap();
  40. }
  41. bool passwordWritten = false;
  42. forever {
  43. if (!openConnect.waitForReadyRead()) {
  44. qDebug() << "no data";
  45. break;
  46. }
  47. if (openConnect.state() == QProcess::NotRunning) {
  48. qDebug() << "process terminated early";
  49. break;
  50. }
  51. QByteArray data = openConnect.readAll();
  52. if (!passwordWritten) {
  53. if (!data.contains("Password:"))
  54. continue;
  55. openConnect.setReadChannel(QProcess::StandardOutput);
  56. openConnect.write(password.toUtf8());
  57. openConnect.closeWriteChannel();
  58. passwordWritten = true;
  59. } else {
  60. openConnect.waitForFinished();
  61. QVariantMap result;
  62. QStringList variables = QString(data).split('\n', QString::SkipEmptyParts);
  63. foreach (QString variable, variables) {
  64. QStringList parts = variable.split('=', QString::SkipEmptyParts);
  65. QString value = parts.last();
  66. if (value.startsWith("'")) value.remove(0, 1);
  67. if (value.endsWith("'")) value.remove(value.size() - 1, 1);
  68. result.insert(parts.first(), value);
  69. }
  70. return result;
  71. }
  72. }
  73. return QVariantMap();
  74. }
  75. int main(int argc, char **argv)
  76. {
  77. QCoreApplication app(argc, argv);
  78. QStringList args = app.arguments();
  79. if (args.length() < 5) {
  80. qDebug() << "usage: " << args.at(0) << " <host> <domain> <user> <password>";
  81. return 0;
  82. }
  83. QString host = QUrl::fromUserInput(args.at(1)).host();
  84. QString domain = args.at(2);
  85. QVariantMap authInfo = openConnectHelper(host, domain, args.at(3), args.at(4));
  86. if (authInfo.isEmpty()) {
  87. qDebug() << Q_FUNC_INFO << "unable to obtain auth info";
  88. return -1;
  89. }
  90. OpenConnectProvider provider;
  91. provider.setHost(host);
  92. provider.setDomain(domain);
  93. provider.setName("testvpn");
  94. provider.setCookie(authInfo.value("COOKIE").toString());
  95. provider.setServerCert(authInfo.value("FINGERPRINT").toString());
  96. provider.setNoCertCheck(true);
  97. VpnManager manager;
  98. QDBusObjectPath path = manager.create(provider);
  99. if (path.path().isEmpty() || path.path().isNull()) {
  100. qDebug() << Q_FUNC_INFO << "unable to connect provider";
  101. return -1;
  102. }
  103. qDebug() << Q_FUNC_INFO << "created vpn service at path: " << path.path();
  104. forever {
  105. app.processEvents();
  106. // need to wait for the connection to show up
  107. VpnConnection *connection = manager.connection(path);
  108. if (connection) {
  109. connection->connect();
  110. qDebug() << Q_FUNC_INFO << "connected vpn service at path: " << path.path();
  111. break;
  112. }
  113. }
  114. return 0;
  115. }