diff options
author | kh1 <karsten.heimrich@digia.com> | 2014-06-13 12:03:49 +0200 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@digia.com> | 2014-06-19 17:30:37 +0200 |
commit | b8c0d6d49b2eb65524e393baf0e15b70e447dcc3 (patch) | |
tree | 83f13f51dac79bd6ee8980271405bfc0ab25e974 /src | |
parent | 246e13d6920f1fd869267537d6ccde4149192898 (diff) |
Fix AdminAuthorization implementation.
- execute() does the magic of getting privileges and starting
the process
- hasAdminRights() simply returns if we started privileged..
Change-Id: I0940a02556fe240af0ee7dfb068f7f8009eb683e
Reviewed-by: Karsten Heimrich <karsten.heimrich@digia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/libs/installer/adminauthorization.cpp | 57 | ||||
-rw-r--r-- | src/libs/installer/adminauthorization.h | 48 | ||||
-rw-r--r-- | src/libs/installer/adminauthorization_mac.cpp | 76 | ||||
-rw-r--r-- | src/libs/installer/adminauthorization_win.cpp | 17 | ||||
-rw-r--r-- | src/libs/installer/adminauthorization_x11.cpp | 34 | ||||
-rw-r--r-- | src/libs/installer/installer.pro | 1 | ||||
-rw-r--r-- | src/libs/installer/remoteclient_p.h | 3 |
7 files changed, 43 insertions, 193 deletions
diff --git a/src/libs/installer/adminauthorization.cpp b/src/libs/installer/adminauthorization.cpp deleted file mode 100644 index 8f2ced9e5..000000000 --- a/src/libs/installer/adminauthorization.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/************************************************************************** -** -** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the Qt Installer Framework. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -**************************************************************************/ - -#include "adminauthorization.h" - -AdminAuthorizationBase::AdminAuthorizationBase() - : m_authorized(false) -{ -} - -bool AdminAuthorizationBase::isAuthorized() const -{ - return m_authorized; -} - -void AdminAuthorizationBase::setAuthorized() -{ - m_authorized = true; -} diff --git a/src/libs/installer/adminauthorization.h b/src/libs/installer/adminauthorization.h index b5cb67a6a..acb7e190a 100644 --- a/src/libs/installer/adminauthorization.h +++ b/src/libs/installer/adminauthorization.h @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2012-2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Installer Framework. @@ -42,51 +42,17 @@ #ifndef ADMINAUTHORIZATION_H #define ADMINAUTHORIZATION_H -#include <QtCore/QObject> +#include <QWidget> -class AdminAuthorizationBase -{ -protected: - AdminAuthorizationBase(); - -public: - virtual ~AdminAuthorizationBase() {} - - virtual bool authorize() = 0; - bool isAuthorized() const; - -protected: - void setAuthorized(); - -private: - bool m_authorized; -}; +namespace QInstaller { -class AdminAuthorization : public QObject, public AdminAuthorizationBase +class AdminAuthorization { - Q_OBJECT - Q_PROPERTY(bool authorized READ isAuthorized) - public: - AdminAuthorization(); -#ifdef Q_OS_MAC - ~AdminAuthorization(); -#endif - - bool execute(QWidget *dialogParent, const QString &programs, const QStringList &arguments); static bool hasAdminRights(); - -public Q_SLOTS: - bool authorize(); - -Q_SIGNALS: - void authorized(); - -#ifdef Q_OS_MAC -private: - class Private; - Private *d; -#endif + static bool execute(QWidget *parent, const QString &programs, const QStringList &arguments); }; +} // namespace QInstaller + #endif // ADMINAUTHORIZATION_H diff --git a/src/libs/installer/adminauthorization_mac.cpp b/src/libs/installer/adminauthorization_mac.cpp index 8b2269181..ead0cbca8 100644 --- a/src/libs/installer/adminauthorization_mac.cpp +++ b/src/libs/installer/adminauthorization_mac.cpp @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2012-2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Installer Framework. @@ -44,71 +44,31 @@ #include <Security/Authorization.h> #include <Security/AuthorizationTags.h> -#include <QtCore/QStringList> -#include <QtCore/QVector> +#include <QStringList> +#include <QVector> #include <unistd.h> +namespace QInstaller { -// -- AdminAuthorization::Private - -class AdminAuthorization::Private -{ -public: - Private() : auth(0) { } - - AuthorizationRef auth; -}; - - -// -- AdminAuthorization - -AdminAuthorization::AdminAuthorization() - : d(new Private) -{ - AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &d->auth); -} - -AdminAuthorization::~AdminAuthorization() -{ - AuthorizationFree(d->auth, kAuthorizationFlagDestroyRights); - delete d; -} - -bool AdminAuthorization::authorize() +bool AdminAuthorization::execute(QWidget *, const QString &program, const QStringList &arguments) { - if (geteuid() == 0) - setAuthorized(); - - if (isAuthorized()) - return true; - - AuthorizationItem item; - item.name = kAuthorizationRightExecute; - item.valueLength = 0; - item.value = NULL; - item.flags = 0; - - AuthorizationRights rights; - rights.count = 1; - rights.items = &item; + AuthorizationRef authorizationRef; + OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, + kAuthorizationFlagDefaults, &authorizationRef); + if (status != errAuthorizationSuccess) + return false; + AuthorizationItem item = { kAuthorizationRightExecute, 0, 0, 0 }; + AuthorizationRights rights = { 1, &item }; const AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights; - const OSStatus result = AuthorizationCopyRights(d->auth, &rights, kAuthorizationEmptyEnvironment, + status = AuthorizationCopyRights(authorizationRef, &rights, kAuthorizationEmptyEnvironment, flags, 0); - if (result != errAuthorizationSuccess) + if (status != errAuthorizationSuccess) return false; - seteuid(0); - setAuthorized(); - emit authorized(); - return true; -} - -bool AdminAuthorization::execute(QWidget *, const QString &program, const QStringList &arguments) -{ QVector<char *> args; QVector<QByteArray> utf8Args; foreach (const QString &argument, arguments) { @@ -118,12 +78,16 @@ bool AdminAuthorization::execute(QWidget *, const QString &program, const QStrin args.push_back(0); const QByteArray utf8Program = program.toUtf8(); - const OSStatus result = AuthorizationExecuteWithPrivileges(d->auth, utf8Program.data(), + status = AuthorizationExecuteWithPrivileges(authorizationRef, utf8Program.data(), kAuthorizationFlagDefaults, args.data(), 0); - return result == errAuthorizationSuccess; + + AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights); + return status == errAuthorizationSuccess; } bool AdminAuthorization::hasAdminRights() { return geteuid() == 0; } + +} // namespace QInstaller diff --git a/src/libs/installer/adminauthorization_win.cpp b/src/libs/installer/adminauthorization_win.cpp index 528f3403d..4d85ad980 100644 --- a/src/libs/installer/adminauthorization_win.cpp +++ b/src/libs/installer/adminauthorization_win.cpp @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2012-2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Installer Framework. @@ -53,6 +53,8 @@ # endif #endif +namespace QInstaller { + struct DeCoInitializer { DeCoInitializer() @@ -67,17 +69,6 @@ struct DeCoInitializer bool neededCoInit; }; -AdminAuthorization::AdminAuthorization() -{ -} - -bool AdminAuthorization::authorize() -{ - setAuthorized(); - emit authorized(); - return true; -} - bool AdminAuthorization::hasAdminRights() { SID_IDENTIFIER_AUTHORITY authority = { SECURITY_NT_AUTHORITY }; @@ -150,3 +141,5 @@ bool AdminAuthorization::execute(QWidget *, const QString &program, const QStrin } return false; } + +} // namespace QInstaller diff --git a/src/libs/installer/adminauthorization_x11.cpp b/src/libs/installer/adminauthorization_x11.cpp index 6af037e91..cd3a4ecbf 100644 --- a/src/libs/installer/adminauthorization_x11.cpp +++ b/src/libs/installer/adminauthorization_x11.cpp @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2012-2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Installer Framework. @@ -69,31 +69,20 @@ #define SU_COMMAND "/usr/bin/sudo" //#define SU_COMMAND "/bin/echo" -AdminAuthorization::AdminAuthorization() -{ -} +namespace QInstaller { -bool AdminAuthorization::authorize() +static QString getPassword(QWidget *parent) { - return true; -} - -static QString getPassword(QWidget *) -{ -#if QT_VERSION < 0x050000 - if (QApplication::type() == QApplication::GuiClient) -#else - if (qobject_cast<QApplication*> (qApp) != 0) -#endif - { + if (qobject_cast<QApplication*> (qApp) != 0) { bool ok = false; - const QString result = QInputDialog::getText(0, QObject::tr("Authorization required"), + const QString result = QInputDialog::getText(parent, QObject::tr("Authorization required"), QObject::tr("Enter your password to authorize for sudo:"), QLineEdit::Password, QString(), &ok); return ok ? result : QString(); } else { std::cout << QObject::tr("Authorization required").toStdString() << std::endl; - std::cout << QObject::tr("Enter your password to authorize for sudo:").toStdString() << std::endl; + std::cout << QObject::tr("Enter your password to authorize for sudo:").toStdString() + << std::endl; std::string password; std::cin >> password; return QString::fromStdString(password); @@ -102,12 +91,7 @@ static QString getPassword(QWidget *) static void printError(QWidget *parent, const QString &value) { -#if QT_VERSION < 0x050000 - if (QApplication::type() == QApplication::GuiClient) -#else - if (qobject_cast<QApplication*> (qApp) != 0) -#endif - { + if (qobject_cast<QApplication*> (qApp) != 0) { QMessageBox::critical(parent, QObject::tr( "Error acquiring admin rights" ), value, QMessageBox::Ok, QMessageBox::Ok); } else { @@ -280,3 +264,5 @@ bool AdminAuthorization::hasAdminRights() { return getuid() == 0; } + +} // namespace QInstaller diff --git a/src/libs/installer/installer.pro b/src/libs/installer/installer.pro index 4e0e0b2c3..016c08f3b 100644 --- a/src/libs/installer/installer.pro +++ b/src/libs/installer/installer.pro @@ -155,7 +155,6 @@ SOURCES += packagemanagercore.cpp \ updater.cpp \ operationrunner.cpp \ updatesettings.cpp \ - adminauthorization.cpp \ elevatedexecuteoperation.cpp \ fakestopprocessforupdateoperation.cpp \ lazyplaintextedit.cpp \ diff --git a/src/libs/installer/remoteclient_p.h b/src/libs/installer/remoteclient_p.h index ab058c820..454864d62 100644 --- a/src/libs/installer/remoteclient_p.h +++ b/src/libs/installer/remoteclient_p.h @@ -158,8 +158,7 @@ public: m_serverStarting = true; if (m_startServerAsAdmin) { - AdminAuthorization auth; - m_serverStarted = auth.authorize() && auth.execute(0, m_serverCommand, m_serverArguments); + m_serverStarted = AdminAuthorization::execute(0, m_serverCommand, m_serverArguments); if (!m_serverStarted) { // something went wrong with authorizing, either user pressed cancel or entered |