Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <userenv.h>
#include <wtsapi32.h>

#include <span>

using namespace Qt::StringLiterals;

int main( int argc, char *argv[] )
Expand All @@ -53,8 +55,8 @@ Application::Application( int &argc, char **argv )
if( log.exists() && log.open( QFile::WriteOnly|QFile::Append ) )
qInstallMessageHandler( msgHandler );

QTranslator *qt = new QTranslator( this );
QTranslator *t = new QTranslator( this );
auto *qt = new QTranslator(this);
auto *t = new QTranslator(this);
QString lang;
auto languages = QLocale().uiLanguages().first();
if(languages.contains("et"_L1, Qt::CaseInsensitive))
Expand Down Expand Up @@ -83,7 +85,7 @@ Application::~Application()
qInstallMessageHandler(nullptr);
}

int Application::confTask( const QStringList &args ) const
int Application::confTask(const QStringList &args)
{
ScheduledUpdateTask task;
if(args.contains("-status"_L1))
Expand All @@ -106,29 +108,32 @@ bool Application::execute(const QStringList &arguments)
QString command = QDir::toNativeSeparators(applicationFilePath()) + ' ' + arguments.join(' ');
qDebug() << "command:" << command;

PWTS_SESSION_INFOW sessionInfo = 0;
PWTS_SESSION_INFOW sessionInfo {};
DWORD count = 0;
WTSEnumerateSessionsW(WTS_CURRENT_SERVER_HANDLE, 0, 1, &sessionInfo, &count);
auto ret = WTSEnumerateSessionsW(WTS_CURRENT_SERVER_HANDLE, 0, 1, &sessionInfo, &count);
qDebug() << "WTSEnumerateSessionsW" << ret << GetLastError();
if(!ret)
return false;

DWORD sessionId = 0;
for(DWORD i = 0; i < count; ++i)
for(const auto &session: std::span(sessionInfo, count))
{
if(sessionInfo[i].State == WTSActive)
if(session.State == WTSActive)
{
sessionId = sessionInfo[i].SessionId;
sessionId = session.SessionId;
break;
}
}
WTSFreeMemory(sessionInfo);
qDebug() << "Active session ID " << sessionId;

HANDLE currentToken = 0;
BOOL ret = WTSQueryUserToken(sessionId, &currentToken);
HANDLE currentToken {};
ret = WTSQueryUserToken(sessionId, &currentToken);
qDebug() << "WTSQueryUserToken" << ret << GetLastError();
if(!ret)
return false;

HANDLE primaryToken = 0;
HANDLE primaryToken {};
ret = DuplicateTokenEx(currentToken, TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS, 0,
SecurityImpersonation, TokenPrimary, &primaryToken);
CloseHandle(currentToken);
Expand All @@ -140,7 +145,7 @@ bool Application::execute(const QStringList &arguments)
if(!primaryToken)
return false;

void *environment = nullptr;
void *environment {};
ret = CreateEnvironmentBlock(&environment, primaryToken, true);
qDebug() << "CreateEnvironmentBlock" << environment << ret << GetLastError();

Expand All @@ -162,17 +167,17 @@ void Application::messageReceived( const QString &str )
w->checkUpdates(str.contains("-autoupdate"_L1), str.contains("-autoclose"_L1));
}

void Application::msgHandler( QtMsgType type, const QMessageLogContext &, const QString &msg )
void Application::msgHandler(QtMsgType type, const QMessageLogContext &/* ctx */, const QString &msg)
{
QFile *log = &qobject_cast<Application*>(qApp)->log;
log->write(QDateTime::currentDateTime().toString(u"yyyy-MM-dd hh:mm:ss:zzz "_s).toUtf8());
QFile &log = qobject_cast<Application*>(qApp)->log;
log.write(QDateTime::currentDateTime().toString(u"yyyy-MM-dd hh:mm:ss:zzz "_s).toUtf8());
switch( type )
{
case QtDebugMsg: log->write("DBG: %1\n"_L1.arg(msg).toUtf8()); break;
case QtInfoMsg: log->write("INF: %1\n"_L1.arg(msg).toUtf8()); break;
case QtWarningMsg: log->write("WRN: %1\n"_L1.arg(msg).toUtf8()); break;
case QtCriticalMsg: log->write("CRI: %1\n"_L1.arg( msg).toUtf8()); break;
case QtFatalMsg: log->write("FAT: %1\n"_L1.arg(msg).toUtf8()); abort();
case QtDebugMsg: log.write("DBG: %1\n"_L1.arg(msg).toUtf8()); break;
case QtInfoMsg: log.write("INF: %1\n"_L1.arg(msg).toUtf8()); break;
case QtWarningMsg: log.write("WRN: %1\n"_L1.arg(msg).toUtf8()); break;
case QtCriticalMsg: log.write("CRI: %1\n"_L1.arg( msg).toUtf8()); break;
case QtFatalMsg: log.write("FAT: %1\n"_L1.arg(msg).toUtf8()); abort();
}
}

Expand Down
4 changes: 2 additions & 2 deletions Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Application: public QtSingleApplication
bool execute(const QStringList &arguments);
void messageReceived( const QString &str );
static void msgHandler( QtMsgType type, const QMessageLogContext &ctx, const QString &msg );
int confTask( const QStringList &args ) const;
void printHelp();
static int confTask(const QStringList &args);
static void printHelp();

QFile log;
QString url;
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
## Building
[![Build Status](https://github.com/open-eid/updater/workflows/CI/badge.svg?branch=master)](https://github.com/open-eid/updater/actions)

### OSX
### Windows

1. Fetch the source

Expand All @@ -23,6 +23,21 @@

cmake --build build

### OSX

1. Fetch the source

git clone https://github.com/open-eid/updater
cd updater

2. Configure

cmake -B build -S .

3. Build

cmake --build build

4. Install

sudo cmake --build build --target install
Expand Down
20 changes: 10 additions & 10 deletions ScheduledUpdateTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,23 @@
template <class T>
struct CPtr
{
T *d{};
~CPtr() { if(d) d->Release(); }
inline T* operator->() const { return d; }
inline operator T*() const { return d; }
inline T** operator&() { return &d; }
T *d{};
~CPtr() { if(d) d->Release(); }
constexpr T* operator->() const { return d; }
constexpr operator T*() const { return d; }
constexpr T** operator&() { return &d; }
};

class ScheduledUpdateTaskPrivate
struct ScheduledUpdateTask::Private
{
public:
CPtr<ITaskService> service;
CPtr<ITaskFolder> folder;
};



ScheduledUpdateTask::ScheduledUpdateTask()
: d(new ScheduledUpdateTaskPrivate)
: d(std::make_unique<Private>())
{
CoInitialize(nullptr);
CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
Expand All @@ -64,7 +63,7 @@ ScheduledUpdateTask::ScheduledUpdateTask()

ScheduledUpdateTask::~ScheduledUpdateTask()
{
delete d;
d.reset();
CoUninitialize();
}

Expand All @@ -84,6 +83,7 @@ bool ScheduledUpdateTask::configure(ScheduledUpdateTask::Interval interval)
settings->put_RunOnlyIfNetworkAvailable(VARIANT_TRUE);
settings->put_DisallowStartIfOnBatteries(VARIANT_FALSE);
settings->put_StopIfGoingOnBatteries(VARIANT_FALSE);
settings->put_MultipleInstances(TASK_INSTANCES_STOP_EXISTING);
}

CPtr<ITriggerCollection> triggerCollection;
Expand Down Expand Up @@ -139,7 +139,7 @@ bool ScheduledUpdateTask::configure(ScheduledUpdateTask::Interval interval)
}
}

QString command = QDir::toNativeSeparators(qApp->applicationFilePath());
QString command = QDir::toNativeSeparators(QCoreApplication::applicationFilePath());
CPtr<IActionCollection> actionCollection;
CPtr<IAction> action;
CPtr<IExecAction> execAction;
Expand Down
6 changes: 3 additions & 3 deletions ScheduledUpdateTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

#pragma once

#include <QString>
#include <memory>

class ScheduledUpdateTaskPrivate;
class ScheduledUpdateTask
{
public:
Expand All @@ -39,5 +38,6 @@ class ScheduledUpdateTask
bool remove();

private:
ScheduledUpdateTaskPrivate *d;
struct Private;
std::unique_ptr<Private> d;
};
2 changes: 1 addition & 1 deletion common
Submodule common updated 2 files
+30 −4 Configuration.cpp
+1 −0 Configuration.h
29 changes: 16 additions & 13 deletions idupdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
using namespace Qt::StringLiterals;

idupdaterui::idupdaterui( const QString &version, idupdater *parent )
: QWidget()
{
setupUi( this );
m_message->hide();
Expand Down Expand Up @@ -152,7 +151,7 @@ void idupdater::finished(bool /*changed*/, const QString &err)

QJsonObject obj = conf->object();
trusted.clear();
for(const auto array = conf->object().value("CERT-BUNDLE"_L1).toArray(); const auto &c: array)
for(const auto array = conf->rawObject().value("CERT-BUNDLE"_L1).toArray(); const auto &c: array)
trusted.append(QSslCertificate(QByteArray::fromBase64(c.toString().toLatin1()), QSsl::Der));
if(obj.contains("UPDATER-MESSAGE-URL"_L1))
{
Expand Down Expand Up @@ -196,7 +195,7 @@ void idupdater::finished(bool /*changed*/, const QString &err)
if(w) w->setInfo(version, available);
}

QString idupdater::installedVersion(const QString &upgradeCode) const
QString idupdater::installedVersion(const QString &upgradeCode)
{
QString code = upgradeCode.toUpper();
QSettings s(u"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"_s, QSettings::Registry32Format);
Expand All @@ -212,7 +211,7 @@ QString idupdater::installedVersion(const QString &upgradeCode) const
return {};

DWORD size = 0;
MsiGetProductInfo(prodCode, INSTALLPROPERTY_VERSIONSTRING, 0, &size);
MsiGetProductInfo(prodCode, INSTALLPROPERTY_VERSIONSTRING, nullptr, &size);
QString version(size, '\0');
size += 1;
MsiGetProductInfo(prodCode, INSTALLPROPERTY_VERSIONSTRING, LPWSTR(version.data()), &size);
Expand Down Expand Up @@ -299,16 +298,20 @@ bool idupdater::verifyPackage(const QString &filePath) const
if(!trusted.contains(cert))
return false;

WINTRUST_FILE_INFO FileData { sizeof(WINTRUST_FILE_INFO) };
FileData.pcwszFilePath = LPCWSTR(path.utf16());
WINTRUST_FILE_INFO FileData {
.cbStruct = sizeof(WINTRUST_FILE_INFO),
.pcwszFilePath = LPCWSTR(path.utf16()),
};

WINTRUST_DATA WinTrustData { sizeof(WinTrustData) };
WinTrustData.dwUIChoice = m_autoupdate ? WTD_UI_NONE : WTD_UI_ALL;
WinTrustData.fdwRevocationChecks = WTD_REVOKE_NONE;
WinTrustData.dwUnionChoice = WTD_CHOICE_FILE;
WinTrustData.dwProvFlags = WTD_SAFER_FLAG;
WinTrustData.pFile = &FileData;
WINTRUST_DATA WinTrustData {
.cbStruct = sizeof(WinTrustData),
.dwUIChoice = DWORD(m_autoupdate ? WTD_UI_NONE : WTD_UI_ALL),
.fdwRevocationChecks = WTD_REVOKE_NONE,
.dwUnionChoice = WTD_CHOICE_FILE,
.pFile = &FileData,
.dwProvFlags = WTD_SAFER_FLAG,
};

GUID WVTPolicyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
return WinVerifyTrust(0, &WVTPolicyGUID, &WinTrustData) == ERROR_SUCCESS;
return WinVerifyTrust(nullptr, &WVTPolicyGUID, &WinTrustData) == ERROR_SUCCESS;
}
2 changes: 1 addition & 1 deletion idupdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class idupdater : public QNetworkAccessManager

private:
void finished(bool changed, const QString &error);
QString installedVersion(const QString &upgradeCode) const;
static QString installedVersion(const QString &upgradeCode);
bool verifyPackage(const QString &filePath) const;

bool m_autoupdate = false, m_autoclose = false;
Expand Down