installdialog.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "installdialog.h"
  28. #include "mainwindow.h"
  29. #include "ui_installdialog.h"
  30. #include "../node/Constants.hpp"
  31. #include "../node/Defaults.hpp"
  32. #include "../node/SoftwareUpdater.hpp"
  33. #ifdef __UNIX_LIKE__
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <sys/stat.h>
  38. #include <sys/types.h>
  39. #include <fcntl.h>
  40. #endif
  41. #ifdef __APPLE__
  42. #include "mac_doprivileged.h"
  43. #endif
  44. #include <QMainWindow>
  45. #include <QMessageBox>
  46. #include <QByteArray>
  47. #include <QSslSocket>
  48. #include <QFile>
  49. #include <QDir>
  50. #include <QProcess>
  51. InstallDialog::InstallDialog(QWidget *parent) :
  52. QMainWindow(parent),
  53. ui(new Ui::InstallDialog),
  54. nam(new QNetworkAccessManager(this)),
  55. phase(FETCHING_NFO)
  56. {
  57. ui->setupUi(this);
  58. QObject::connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(on_networkReply(QNetworkReply*)));
  59. const char *nfoUrl = ZeroTier::ZT_DEFAULTS.updateLatestNfoURL.c_str();
  60. if (!*nfoUrl) {
  61. QMessageBox::critical(this,"Download Failed","Download failed: internal error: no update URL configured in build!",QMessageBox::Ok,QMessageBox::NoButton);
  62. QApplication::exit(1);
  63. return;
  64. }
  65. QNetworkReply *reply = nam->get(QNetworkRequest(QUrl(nfoUrl)));
  66. QObject::connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(on_downloadProgress(qint64,qint64)));
  67. }
  68. InstallDialog::~InstallDialog()
  69. {
  70. delete ui;
  71. }
  72. void InstallDialog::on_networkReply(QNetworkReply *reply)
  73. {
  74. reply->deleteLater();
  75. if (reply->error() != QNetworkReply::NoError) {
  76. QMessageBox::critical(this,"Download Failed",QString("Download failed: ") + reply->errorString() + "\n\nAre you connected to the Internet?",QMessageBox::Ok,QMessageBox::NoButton);
  77. QApplication::exit(1);
  78. } else {
  79. if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) {
  80. QByteArray installerData(reply->readAll());
  81. switch(phase) {
  82. case FETCHING_NFO: {
  83. unsigned int vMajor = 0,vMinor = 0,vRevision = 0;
  84. installerData.append((char)0);
  85. const char *err = ZeroTier::SoftwareUpdater::parseNfo(installerData.data(),vMajor,vMinor,vRevision,signedBy,signature,url);
  86. if (err) {
  87. QMessageBox::critical(this,"Download Failed","Download failed: there is a problem with the software update web site.\nTry agian later. (invalid .nfo file)",QMessageBox::Ok,QMessageBox::NoButton);
  88. QApplication::exit(1);
  89. return;
  90. }
  91. phase = FETCHING_INSTALLER;
  92. reply = nam->get(QNetworkRequest(QUrl(url.c_str())));
  93. QObject::connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(on_downloadProgress(qint64,qint64)));
  94. } break;
  95. case FETCHING_INSTALLER: {
  96. if (!ZeroTier::SoftwareUpdater::validateUpdate(installerData.data(),installerData.length(),signedBy,signature)) {
  97. QMessageBox::critical(this,"Download Failed","Download failed: there is a problem with the software update web site. Try agian later. (downloaded data failed signature check)",QMessageBox::Ok,QMessageBox::NoButton);
  98. QApplication::exit(1);
  99. return;
  100. }
  101. #ifdef __APPLE__
  102. {
  103. std::string homePath(QDir::homePath().toStdString());
  104. QString zt1Caches(QDir::homePath() + "/Library/Caches/ZeroTier/One");
  105. QDir::root().mkpath(zt1Caches);
  106. std::string instPath((zt1Caches + "/ZeroTierOneInstaller").toStdString());
  107. std::string tmpPath((zt1Caches + "/inst.sh").toStdString());
  108. int outfd = ::open(instPath.c_str(),O_CREAT|O_TRUNC|O_WRONLY,0755);
  109. if (outfd <= 0) {
  110. QMessageBox::critical(this,"Download Failed",QString("Installation failed: unable to write to ")+instPath.c_str(),QMessageBox::Ok,QMessageBox::NoButton);
  111. QApplication::exit(1);
  112. return;
  113. }
  114. if (::write(outfd,installerData.data(),installerData.length()) != installerData.length()) {
  115. QMessageBox::critical(this,"Installation Failed",QString("Installation failed: unable to write to ")+instPath.c_str(),QMessageBox::Ok,QMessageBox::NoButton);
  116. QApplication::exit(1);
  117. return;
  118. }
  119. ::close(outfd);
  120. chmod(instPath.c_str(),0755);
  121. FILE *scr = fopen(tmpPath.c_str(),"w");
  122. if (!scr) {
  123. QMessageBox::critical(this,"Installation Failed","Cannot write script to temporary Library/Caches/ZeroTier/One folder.",QMessageBox::Ok,QMessageBox::NoButton);
  124. QApplication::exit(1);
  125. return;
  126. }
  127. fprintf(scr,"#!/bin/bash\n");
  128. fprintf(scr,"export PATH=\"/bin:/usr/bin:/sbin:/usr/sbin\"\n");
  129. fprintf(scr,"'%s'\n",instPath.c_str());
  130. fprintf(scr,"if [ -f '/Library/Application Support/ZeroTier/One/authtoken.secret' ]; then\n");
  131. fprintf(scr," mkdir -p '%s/Library/Application Support/ZeroTier/One'\n",homePath.c_str());
  132. fprintf(scr," chown %d '%s/Library/Application Support/ZeroTier'\n",(int)getuid(),homePath.c_str());
  133. fprintf(scr," chgrp %d '%s/Library/Application Support/ZeroTier'\n",(int)getgid(),homePath.c_str());
  134. fprintf(scr," chmod 0700 '%s/Library/Application Support/ZeroTier'\n",homePath.c_str());
  135. fprintf(scr," chown %d '%s/Library/Application Support/ZeroTier/One'\n",(int)getuid(),homePath.c_str());
  136. fprintf(scr," chgrp %d '%s/Library/Application Support/ZeroTier/One'\n",(int)getgid(),homePath.c_str());
  137. fprintf(scr," chmod 0700 '%s/Library/Application Support/ZeroTier/One'\n",homePath.c_str());
  138. fprintf(scr," cp -f '/Library/Application Support/ZeroTier/One/authtoken.secret' '%s/Library/Application Support/ZeroTier/One/authtoken.secret'\n",homePath.c_str());
  139. fprintf(scr," chown %d '%s/Library/Application Support/ZeroTier/One/authtoken.secret'\n",(int)getuid(),homePath.c_str());
  140. fprintf(scr," chgrp %d '%s/Library/Application Support/ZeroTier/One/authtoken.secret'\n",(int)getgid(),homePath.c_str());
  141. fprintf(scr," chmod 0600 '%s/Library/Application Support/ZeroTier/One/authtoken.secret'\n",homePath.c_str());
  142. fprintf(scr,"fi\n");
  143. fprintf(scr,"exit 0\n");
  144. fclose(scr);
  145. chmod(tmpPath.c_str(),0755);
  146. macExecutePrivilegedShellCommand((std::string("'")+tmpPath+"' >>/dev/null 2>&1").c_str());
  147. unlink(tmpPath.c_str());
  148. unlink(instPath.c_str());
  149. // Restart the binary with whatever updates may have occurred
  150. std::string appPath(QCoreApplication::applicationFilePath().toStdString());
  151. execl(appPath.c_str(),appPath.c_str(),(const char *)0);
  152. // We only make it here if execl() fails
  153. QMessageBox::critical(this,"Re-Launch Failed","An error occurred re-launching ZeroTier One.app. Try launching it manually.",QMessageBox::Ok,QMessageBox::NoButton);
  154. QApplication::exit(1);
  155. return;
  156. }
  157. #endif
  158. } break;
  159. }
  160. ui->progressBar->setMinimum(0);
  161. ui->progressBar->setMaximum(100);
  162. ui->progressBar->setValue(0);
  163. } else {
  164. QMessageBox::critical(this,"Download Failed",QString("Download failed: HTTP status code ") + reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString(),QMessageBox::Ok,QMessageBox::NoButton);
  165. QApplication::exit(1);
  166. }
  167. }
  168. }
  169. void InstallDialog::on_InstallDialog_rejected()
  170. {
  171. QApplication::exit();
  172. }
  173. void InstallDialog::on_cancelButton_clicked()
  174. {
  175. QApplication::exit();
  176. }
  177. void InstallDialog::on_downloadProgress(qint64 bytesReceived,qint64 bytesTotal)
  178. {
  179. if (bytesTotal <= 0) {
  180. ui->progressBar->setValue(0);
  181. ui->progressBar->setMinimum(0);
  182. ui->progressBar->setMaximum(0);
  183. } else {
  184. double pct = ((double)bytesReceived / (double)bytesTotal) * 100.0;
  185. if (pct > 100.0)
  186. pct = 100.0;
  187. ui->progressBar->setMinimum(0);
  188. ui->progressBar->setMaximum(100);
  189. ui->progressBar->setValue((int)pct);
  190. }
  191. }