diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/CMakeLists.txt | 31 | ||||
-rw-r--r-- | core/polkitqt1-actiondescription.cpp | 148 | ||||
-rw-r--r-- | core/polkitqt1-actiondescription.h | 149 | ||||
-rw-r--r-- | core/polkitqt1-authority.cpp | 855 | ||||
-rw-r--r-- | core/polkitqt1-authority.h | 539 | ||||
-rw-r--r-- | core/polkitqt1-details.cpp | 99 | ||||
-rw-r--r-- | core/polkitqt1-details.h | 97 | ||||
-rw-r--r-- | core/polkitqt1-identity.cpp | 227 | ||||
-rw-r--r-- | core/polkitqt1-identity.h | 211 | ||||
-rw-r--r-- | core/polkitqt1-subject.cpp | 205 | ||||
-rw-r--r-- | core/polkitqt1-subject.h | 262 | ||||
-rw-r--r-- | core/polkitqt1-temporaryauthorization.cpp | 117 | ||||
-rw-r--r-- | core/polkitqt1-temporaryauthorization.h | 124 |
13 files changed, 3064 insertions, 0 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt new file mode 100644 index 000000000..f2efca4de --- /dev/null +++ b/core/CMakeLists.txt @@ -0,0 +1,31 @@ +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} +) + +set(polkit_qt_core_SRCS + polkitqt1-authority.cpp + polkitqt1-identity.cpp + polkitqt1-subject.cpp + polkitqt1-temporaryauthorization.cpp + polkitqt1-details.cpp + polkitqt1-actiondescription.cpp +) + +automoc4_add_library(polkit-qt-core-1 SHARED ${polkit_qt_core_SRCS}) + +target_link_libraries(polkit-qt-core-1 + ${QT_QTCORE_LIBRARY} + ${QT_QTDBUS_LIBRARY} + ${QT_QTXML_LIBRARY} + ${POLKIT_LIBRARIES} + ${GLIB2_LIBRARIES} + ${GOBJECT_LIBRARIES} + ${GIO_LIBRARIES} +) + +set_target_properties(polkit-qt-core-1 PROPERTIES VERSION ${POLKITQT-1_LIBRARY_VERSION} + SOVERSION ${POLKITQT-1_ABI_VERSION} + DEFINE_SYMBOL MAKE_POLKITQT1_LIB) + +install(TARGETS polkit-qt-core-1 ${INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/core/polkitqt1-actiondescription.cpp b/core/polkitqt1-actiondescription.cpp new file mode 100644 index 000000000..dbe1aa894 --- /dev/null +++ b/core/polkitqt1-actiondescription.cpp @@ -0,0 +1,148 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * Copyright (C) 2010 Dario Freddi <drf@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "polkitqt1-actiondescription.h" + +#include <QtCore/QString> + +#include <polkit/polkit.h> + +namespace PolkitQt1 +{ + +class ActionDescription::Data : public QSharedData +{ +public: + Data() {} + Data(const Data& other) + : QSharedData(other) + , actionId(other.actionId) + , description(other.description) + , message(other.message) + , vendorName(other.vendorName) + , vendorUrl(other.vendorUrl) + , iconName(other.iconName) + , implicitAny(other.implicitAny) + , implicitInactive(other.implicitInactive) + , implicitActive(other.implicitActive) + { + } + virtual ~Data() {} + + QString actionId; + QString description; + QString message; + QString vendorName; + QString vendorUrl; + QString iconName; + + ActionDescription::ImplicitAuthorization implicitAny; + ActionDescription::ImplicitAuthorization implicitInactive; + ActionDescription::ImplicitAuthorization implicitActive; +}; + +ActionDescription::ActionDescription() + : d(new Data) +{ + +} + +ActionDescription::ActionDescription(PolkitActionDescription *polkitActionDescription) + : d(new Data) +{ + g_type_init(); + + d->actionId = QString::fromUtf8(polkit_action_description_get_action_id(polkitActionDescription)); + d->description = QString::fromUtf8(polkit_action_description_get_description(polkitActionDescription)); + d->message = QString::fromUtf8(polkit_action_description_get_message(polkitActionDescription)); + d->vendorName = QString::fromUtf8(polkit_action_description_get_vendor_name(polkitActionDescription)); + d->vendorUrl = QString::fromUtf8(polkit_action_description_get_vendor_url(polkitActionDescription)); + d->iconName = QString::fromUtf8(polkit_action_description_get_icon_name(polkitActionDescription)); + + d->implicitAny = static_cast<ActionDescription::ImplicitAuthorization>(polkit_action_description_get_implicit_any( + polkitActionDescription)); + d->implicitInactive = static_cast<ActionDescription::ImplicitAuthorization>(polkit_action_description_get_implicit_inactive( + polkitActionDescription)); + d->implicitActive = static_cast<ActionDescription::ImplicitAuthorization>(polkit_action_description_get_implicit_active( + polkitActionDescription)); +} + +ActionDescription::ActionDescription(const PolkitQt1::ActionDescription& other) + : d(other.d) +{ +} + +ActionDescription& ActionDescription::operator=(const PolkitQt1::ActionDescription& other) +{ + d = other.d; + return *this; +} + +ActionDescription::~ActionDescription() +{ +} + +QString ActionDescription::actionId() const +{ + return d->actionId; +} + +QString ActionDescription::description() const +{ + return d->description; +} + +QString ActionDescription::message() const +{ + return d->message; +} + +QString ActionDescription::vendorName() const +{ + return d->vendorName; +} + +QString ActionDescription::vendorUrl() const +{ + return d->vendorUrl; +} + +QString ActionDescription::iconName() const +{ + return d->iconName; +} + +ActionDescription::ImplicitAuthorization ActionDescription::implicitAny() const +{ + return d->implicitAny; +} + +ActionDescription::ImplicitAuthorization ActionDescription::implicitInactive() const +{ + return d->implicitInactive; +} + +ActionDescription::ImplicitAuthorization ActionDescription::implicitActive() const +{ + return d->implicitActive; +} + +} diff --git a/core/polkitqt1-actiondescription.h b/core/polkitqt1-actiondescription.h new file mode 100644 index 000000000..deed65d6e --- /dev/null +++ b/core/polkitqt1-actiondescription.h @@ -0,0 +1,149 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * Copyright (C) 2010 Dario Freddi <drf@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef POLKITQT1_ACTION_DESCRIPTION_H +#define POLKITQT1_ACTION_DESCRIPTION_H + +#include "polkitqt1-export.h" + +#include <QtCore/QList> +#include <QtCore/QMetaType> +#include <QtCore/QSharedData> + +typedef struct _PolkitActionDescription PolkitActionDescription; + +namespace PolkitQt1 +{ +/** + * \class ActionDescription polkitqt1-actiondescription.h ActionDescription + * \author Jaroslav Reznik <jreznik@redhat.com> + * \author Dario Freddi <drf@kde.org> + * + * \brief Class used to encapsulate a registered action. + */ +class POLKITQT1_EXPORT ActionDescription +{ +public: + enum ImplicitAuthorization { + /** Unknown whether the subject is authorized, never returned in any public API. **/ + Unknown = -1, + /** Subject is not authorized. **/ + NotAuthorized = 0, + /** Authentication is required. **/ + AuthenticationRequired = 1, + /** Authentication as an administrator is required. **/ + AdministratorAuthenticationRequired = 2, + /** Authentication is required. If the authorization is obtained, it is retained. **/ + AuthenticationRequiredRetained = 3, + /** Authentication as an administrator is required. If the authorization is obtained, it is retained. **/ + AdministratorAuthenticationRequiredRetained = 4, + /** The subject is authorized. **/ + Authorized = 5 + }; + + typedef QList< ActionDescription > List; + + ActionDescription(); + /** + * \brief Constructor of ActionDescription object from PolkitActionDescription + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param actionDesciption PolkitActionDescription + */ + explicit ActionDescription(PolkitActionDescription *actionDescription); + ActionDescription(const ActionDescription &other); + ~ActionDescription(); + + ActionDescription &operator=(const ActionDescription &other); + + /** + * \brief Gets the action id for ActionDescription + * + * \return id of the action + */ + QString actionId() const; + + /** + * \brief Gets the description used of ActionDescription + * + * \return description of the action + */ + QString description() const; + + /** + * \brief Gets the message user for ActionDescription + * + * \return action message + */ + QString message() const; + + /** + * \brief Gets the vendor name for ActionDescription, if any + * + * \return vendor name + */ + QString vendorName() const; + + /** + * \brief Gets the vendor URL for ActionDescription, if any + * + * \return vendor URL or empty QString if there is no vendor URL + */ + QString vendorUrl() const; + + /** + * \brief Gets the icon name for ActionDescription, if any + * + * \return icon name or empty QString if there is no icon + */ + QString iconName() const; + + /** + * \brief Gets the implicit authorization for ActionDescription used for any subject + * + * \return A value from ImplicitAuthorization enumeration + */ + ActionDescription::ImplicitAuthorization implicitAny() const; + + /** + * \brief Gets the implicit authorization for ActionDescription used for subjects in inactive session on a local console + * + * \return A value from ImplicitAuthorization enumeration + */ + ActionDescription::ImplicitAuthorization implicitInactive() const; + + /** + * \brief Gets the implicit authorization for ActionDescription used for subjects in active session on a local console + * + * \return A value from ImplicitAuthorization enumeration + */ + ActionDescription::ImplicitAuthorization implicitActive() const; + +private: + class Data; + QSharedDataPointer< Data > d; +}; +} + +Q_DECLARE_METATYPE(PolkitQt1::ActionDescription::List) + +#endif //POLKIT_QT_ACTION_DESCRIPTION_H diff --git a/core/polkitqt1-authority.cpp b/core/polkitqt1-authority.cpp new file mode 100644 index 000000000..0951d97b5 --- /dev/null +++ b/core/polkitqt1-authority.cpp @@ -0,0 +1,855 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Daniel Nicoletti <dantti85-pk@yahoo.com.br> + * Copyright (C) 2009 Dario Freddi <drf@kde.org> + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "polkitqt1-authority.h" + +#include <QtDBus/QDBusInterface> +#include <QtDBus/QDBusReply> + +#include <polkit/polkit.h> + +namespace PolkitQt1 +{ + +class AuthorityHelper +{ +public: + AuthorityHelper() : q(0) {} + ~AuthorityHelper() { + delete q; + } + Authority *q; +}; + +Q_GLOBAL_STATIC(AuthorityHelper, s_globalAuthority) + +Authority *Authority::instance(PolkitAuthority *authority) +{ + if (!s_globalAuthority()->q) { + new Authority(authority); + } + + return s_globalAuthority()->q; +} + +Authority::Result polkitResultToResult(PolkitAuthorizationResult *result) +{ + if (polkit_authorization_result_get_is_challenge(result)) { + return Authority::Challenge; + } else if (polkit_authorization_result_get_is_authorized(result)) { + return Authority::Yes; + } else { + return Authority::No; + } +} + +ActionDescription::List actionsToListAndFree(GList *glist) +{ + ActionDescription::List result; + for (GList *glist2 = glist; glist2; glist2 = g_list_next(glist2)) { + gpointer i = glist2->data; + result.append(ActionDescription(static_cast<PolkitActionDescription *>(i))); + g_object_unref(i); + } + + g_list_free(glist); + return result; +} + +class Authority::Private +{ +public: + // Polkit will return NULL on failures, hence we use it instead of 0 + Private(Authority *qq) : q(qq) + , pkAuthority(NULL) + , m_hasError(false) {} + + ~Private(); + + void init(); + + /** Use this method to set the error message to \p message. Set recover to \c true + * to try to reinitialize this object with init() method + */ + void setError(Authority::ErrorCode code, const QString &details = QString(), bool recover = false); + + void dbusFilter(const QDBusMessage &message); + void dbusSignalAdd(const QString &service, const QString &path, const QString &interface, const QString &name); + void seatSignalsConnect(const QString &seat); + + Authority *q; + PolkitAuthority *pkAuthority; + bool m_hasError; + Authority::ErrorCode m_lastError; + QString m_errorDetails; + QDBusConnection *m_systemBus; + GCancellable *m_checkAuthorizationCancellable, + *m_enumerateActionsCancellable, + *m_registerAuthenticationAgentCancellable, + *m_unregisterAuthenticationAgentCancellable, + *m_authenticationAgentResponseCancellable, + *m_enumerateTemporaryAuthorizationsCancellable, + *m_revokeTemporaryAuthorizationsCancellable, + *m_revokeTemporaryAuthorizationCancellable; + + + static void pk_config_changed(); + static void checkAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data); + static void enumerateActionsCallback(GObject *object, GAsyncResult *result, gpointer user_data); + static void registerAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data); + static void unregisterAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data); + static void authenticationAgentResponseCallback(GObject *object, GAsyncResult *result, gpointer user_data); + static void enumerateTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data); + static void revokeTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data); + static void revokeTemporaryAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data); +}; + +Authority::Private::~Private() +{ + g_object_unref(m_checkAuthorizationCancellable); + g_object_unref(m_enumerateActionsCancellable); + g_object_unref(m_registerAuthenticationAgentCancellable); + g_object_unref(m_unregisterAuthenticationAgentCancellable); + g_object_unref(m_authenticationAgentResponseCancellable); + g_object_unref(m_enumerateTemporaryAuthorizationsCancellable); + g_object_unref(m_revokeTemporaryAuthorizationsCancellable); + g_object_unref(m_revokeTemporaryAuthorizationCancellable); +} + +Authority::Authority(PolkitAuthority *authority, QObject *parent) + : QObject(parent) + , d(new Private(this)) +{ + qRegisterMetaType<PolkitQt1::Authority::Result> (); + qRegisterMetaType<PolkitQt1::ActionDescription::List>(); + + Q_ASSERT(!s_globalAuthority()->q); + s_globalAuthority()->q = this; + + if (authority) { + d->pkAuthority = authority; + } + + d->init(); +} + +Authority::~Authority() +{ + if (d->pkAuthority != NULL) { + g_object_unref(d->pkAuthority); + } + + delete d; +} + +void Authority::Private::init() +{ + QDBusError error; + QDBusError dbus_error; + + g_type_init(); + + m_checkAuthorizationCancellable = g_cancellable_new(); + m_enumerateActionsCancellable = g_cancellable_new(); + m_registerAuthenticationAgentCancellable = g_cancellable_new(); + m_unregisterAuthenticationAgentCancellable = g_cancellable_new(); + m_authenticationAgentResponseCancellable = g_cancellable_new(); + m_enumerateTemporaryAuthorizationsCancellable = g_cancellable_new(); + m_revokeTemporaryAuthorizationsCancellable = g_cancellable_new(); + m_revokeTemporaryAuthorizationCancellable = g_cancellable_new(); + +#ifndef POLKIT_QT_1_COMPATIBILITY_MODE + GError *gerror = NULL; +#endif + if (pkAuthority == NULL) { +#ifndef POLKIT_QT_1_COMPATIBILITY_MODE + pkAuthority = polkit_authority_get_sync(NULL, &gerror); + if (gerror != NULL) { + setError(E_GetAuthority, gerror->message); + g_error_free(gerror); + return; + } +#else + pkAuthority = polkit_authority_get(); +#endif + } + + if (pkAuthority == NULL) { +#ifdef POLKIT_QT_1_COMPATIBILITY_MODE + (E_GetAuthority); +#endif + return; + } + + // connect changed signal + g_signal_connect(G_OBJECT(pkAuthority), "changed", G_CALLBACK(pk_config_changed), NULL); + + // need to listen to NameOwnerChanged + dbusSignalAdd("org.freedesktop.DBus", "/", "org.freedesktop.DBus", "NameOwnerChanged"); + + QString consoleKitService("org.freedesktop.ConsoleKit"); + QString consoleKitManagerPath("/org/freedesktop/ConsoleKit/Manager"); + QString consoleKitManagerInterface("org.freedesktop.ConsoleKit.Manager"); + QString consoleKitSeatInterface("org.freedesktop.ConsoleKit.Seat"); + + // first, add signals SeadAdded and SeatRemoved from ConsoleKit Manager + dbusSignalAdd(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "SeatAdded"); + dbusSignalAdd(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "SeatRemoved"); + + // then we need to extract all seats from ConsoleKit + QDBusMessage msg = QDBusMessage::createMethodCall(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "GetSeats"); + msg = QDBusConnection::systemBus().call(msg); + if (!msg.arguments().isEmpty()) { + // this method returns a list with present seats + QList<QString> seats; + qVariantValue<QDBusArgument> (msg.arguments()[0]) >> seats; + // it can be multiple seats present so connect all their signals + Q_FOREACH(const QString &seat, seats) { + seatSignalsConnect(seat); + } + } +} + +void Authority::Private::setError(Authority::ErrorCode code, const QString &details, bool recover) +{ + if (recover) { + init(); + } + m_lastError = code; + m_errorDetails = details; + m_hasError = true; +} + +void Authority::Private::seatSignalsConnect(const QString &seat) +{ + QString consoleKitService("org.freedesktop.ConsoleKit"); + QString consoleKitSeatInterface("org.freedesktop.ConsoleKit.Seat"); + // we want to connect to all slots of the seat + dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "DeviceAdded"); + dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "DeviceRemoved"); + dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "SessionAdded"); + dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "SessionRemoved"); + dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "ActiveSessionChanged"); +} + +void Authority::Private::dbusSignalAdd(const QString &service, const QString &path, const QString &interface, const QString &name) +{ + // FIXME: This code seems to be nonfunctional - it needs to be fixed somewhere (is it Qt BUG?) + QDBusConnection::systemBus().connect(service, path, interface, name, + q, SLOT(dbusFilter(const QDBusMessage &))); +} + +void Authority::Private::dbusFilter(const QDBusMessage &message) +{ + if (message.type() == QDBusMessage::SignalMessage) { + Q_EMIT q->consoleKitDBChanged(); + + // TODO: Test this with the multiseat support + if (message.member() == "SeatAdded") { + seatSignalsConnect(qVariantValue<QDBusObjectPath> (message.arguments()[0]).path()); + } + } +} + +bool Authority::hasError() const +{ + return d->m_hasError; +} + +Authority::ErrorCode Authority::lastError() const +{ + return d->m_lastError; +} + +const QString Authority::errorDetails() const +{ + if (d->m_lastError == E_None) { + return QString(); + } else { + return d->m_errorDetails; + } +} + +void Authority::clearError() +{ + d->m_hasError = false; + d->m_lastError = E_None; +} + +void Authority::Private::pk_config_changed() +{ + Q_EMIT Authority::instance()->configChanged(); +} + +PolkitAuthority *Authority::polkitAuthority() const +{ + return d->pkAuthority; +} + +Authority::Result Authority::checkAuthorizationSync(const QString &actionId, const Subject &subject, AuthorizationFlags flags) +{ + PolkitAuthorizationResult *pk_result; + GError *error = NULL; + + if (Authority::instance()->hasError()) { + return Unknown; + } + + if (!subject.isValid()) { + d->setError(E_WrongSubject); + return Unknown; + } + + pk_result = polkit_authority_check_authorization_sync(d->pkAuthority, + subject.subject(), + actionId.toAscii().data(), + NULL, + (PolkitCheckAuthorizationFlags)(int)flags, + NULL, + &error); + + if (error != NULL) { + d->setError(E_CheckFailed, error->message); + g_error_free(error); + return Unknown; + } + + if (!pk_result) { + d->setError(E_UnknownResult); + return Unknown; + } else { + Authority::Result res = polkitResultToResult(pk_result); + g_object_unref(pk_result); + return res; + } +} + +void Authority::checkAuthorization(const QString &actionId, const Subject &subject, AuthorizationFlags flags) +{ + if (Authority::instance()->hasError()) { + return; + } + + if (!subject.isValid()) { + d->setError(E_WrongSubject); + return; + } + + polkit_authority_check_authorization(d->pkAuthority, + subject.subject(), + actionId.toAscii().data(), + NULL, + (PolkitCheckAuthorizationFlags)(int)flags, + d->m_checkAuthorizationCancellable, + d->checkAuthorizationCallback, this); +} + +void Authority::Private::checkAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data) +{ + Authority *authority = (Authority *) user_data; + + Q_ASSERT(authority != NULL); + + GError *error = NULL; + PolkitAuthorizationResult *pkResult = polkit_authority_check_authorization_finish((PolkitAuthority *) object, result, &error); + + if (error != NULL) { + // We don't want to set error if this is cancellation of some action + if (error->code != 1) { + authority->d->setError(E_CheckFailed, error->message); + } + g_error_free(error); + return; + } + if (pkResult != NULL) { + Q_EMIT authority->checkAuthorizationFinished(polkitResultToResult(pkResult)); + g_object_unref(pkResult); + } else { + authority->d->setError(E_UnknownResult); + } +} + +void Authority::checkAuthorizationCancel() +{ + if (!g_cancellable_is_cancelled(d->m_checkAuthorizationCancellable)) { + g_cancellable_cancel(d->m_checkAuthorizationCancellable); + } +} + +ActionDescription::List Authority::enumerateActionsSync() +{ + if (Authority::instance()->hasError()) { + return ActionDescription::List(); + } + + GError *error = NULL; + + GList *glist = polkit_authority_enumerate_actions_sync(d->pkAuthority, + NULL, + &error); + + if (error != NULL) { + d->setError(E_EnumFailed, error->message); + g_error_free(error); + return ActionDescription::List(); + } + + return actionsToListAndFree(glist); +} + +void Authority::enumerateActions() +{ + if (Authority::instance()->hasError()) { + return; + } + + polkit_authority_enumerate_actions(d->pkAuthority, + d->m_enumerateActionsCancellable, + d->enumerateActionsCallback, + Authority::instance()); +} + +void Authority::Private::enumerateActionsCallback(GObject *object, GAsyncResult *result, gpointer user_data) +{ + Authority *authority = (Authority *) user_data; + Q_ASSERT(authority != NULL); + GError *error = NULL; + GList *list = polkit_authority_enumerate_actions_finish((PolkitAuthority *) object, result, &error); + if (error != NULL) { + // We don't want to set error if this is cancellation of some action + if (error->code != 1) { + authority->d->setError(E_EnumFailed, error->message); + } + g_error_free(error); + return; + } + + Q_EMIT authority->enumerateActionsFinished(actionsToListAndFree(list)); +} + +void Authority::enumerateActionsCancel() +{ + if (!g_cancellable_is_cancelled(d->m_enumerateActionsCancellable)) { + g_cancellable_cancel(d->m_enumerateActionsCancellable); + } +} + +bool Authority::registerAuthenticationAgentSync(const Subject &subject, const QString &locale, const QString &objectPath) +{ + if (Authority::instance()->hasError()) { + return false; + } + + gboolean result; + GError *error = NULL; + + if (!subject.isValid()) { + d->setError(E_WrongSubject); + return false; + } + + result = polkit_authority_register_authentication_agent_sync(d->pkAuthority, + subject.subject(), locale.toAscii().data(), + objectPath.toAscii().data(), NULL, &error); + + if (error) { + d->setError(E_RegisterFailed, error->message); + g_error_free(error); + return false; + } + + return result; +} + +void Authority::registerAuthenticationAgent(const Subject &subject, const QString &locale, const QString &objectPath) +{ + if (Authority::instance()->hasError()) { + return; + } + + if (!subject.isValid()) { + d->setError(E_WrongSubject); + return; + } + + polkit_authority_register_authentication_agent(d->pkAuthority, + subject.subject(), + locale.toAscii().data(), + objectPath.toAscii().data(), + d->m_registerAuthenticationAgentCancellable, + d->registerAuthenticationAgentCallback, + this); +} + +void Authority::Private::registerAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data) +{ + Authority *authority = (Authority *) user_data; + Q_ASSERT(authority != NULL); + GError *error = NULL; + bool res = polkit_authority_register_authentication_agent_finish((PolkitAuthority *) object, result, &error); + if (error != NULL) { + // We don't want to set error if this is cancellation of some action + if (error->code != 1) { + authority->d->setError(E_EnumFailed , error->message); + } + g_error_free(error); + return; + } + + Q_EMIT authority->registerAuthenticationAgentFinished(res); +} + +void Authority::registerAuthenticationAgentCancel() +{ + if (!g_cancellable_is_cancelled(d->m_registerAuthenticationAgentCancellable)) { + g_cancellable_cancel(d->m_registerAuthenticationAgentCancellable); + } +} + +bool Authority::unregisterAuthenticationAgentSync(const Subject &subject, const QString &objectPath) +{ + if (d->pkAuthority) { + return false; + } + + if (!subject.isValid()) { + d->setError(E_WrongSubject); + return false; + } + + GError *error = NULL; + + bool result = polkit_authority_unregister_authentication_agent_sync(d->pkAuthority, + subject.subject(), + objectPath.toUtf8().data(), + NULL, + &error); + + if (error != NULL) { + d->setError(E_UnregisterFailed, error->message); + g_error_free(error); + return false; + } + + return result; +} + +void Authority::unregisterAuthenticationAgent(const Subject &subject, const QString &objectPath) +{ + if (Authority::instance()->hasError()) { + return; + } + + if (!subject.isValid()) { + d->setError(E_WrongSubject); + return; + } + + polkit_authority_unregister_authentication_agent(d->pkAuthority, + subject.subject(), + objectPath.toUtf8().data(), + d->m_unregisterAuthenticationAgentCancellable, + d->unregisterAuthenticationAgentCallback, + this); +} + +void Authority::Private::unregisterAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data) +{ + Authority *authority = (Authority *) user_data; + Q_ASSERT(authority); + GError *error = NULL; + bool res = polkit_authority_unregister_authentication_agent_finish((PolkitAuthority *) object, result, &error); + if (error != NULL) { + // We don't want to set error if this is cancellation of some action + if (error->code != 1) { + authority->d->setError(E_UnregisterFailed, error->message); + } + g_error_free(error); + return; + } + + Q_EMIT authority->unregisterAuthenticationAgentFinished(res); +} + +void Authority::unregisterAuthenticationAgentCancel() +{ + if (!g_cancellable_is_cancelled(d->m_unregisterAuthenticationAgentCancellable)) { + g_cancellable_cancel(d->m_unregisterAuthenticationAgentCancellable); + } +} + +bool Authority::authenticationAgentResponseSync(const QString &cookie, const Identity &identity) +{ + if (Authority::instance()->hasError()) { + return false; + } + + if (cookie.isEmpty() || !identity.isValid()) { + d->setError(E_CookieOrIdentityEmpty); + return false; + } + + GError *error = NULL; + + bool result = polkit_authority_authentication_agent_response_sync(d->pkAuthority, + cookie.toUtf8().data(), + identity.identity(), + NULL, + &error); + if (error != NULL) { + d->setError(E_AgentResponseFailed, error->message); + g_error_free(error); + return false; + } + + return result; +} + +void Authority::authenticationAgentResponse(const QString &cookie, const Identity &identity) +{ + if (Authority::instance()->hasError()) { + return; + } + + if (cookie.isEmpty() || !identity.isValid()) { + d->setError(E_CookieOrIdentityEmpty); + return; + } + + polkit_authority_authentication_agent_response(d->pkAuthority, + cookie.toUtf8().data(), + identity.identity(), + d->m_authenticationAgentResponseCancellable, + d->authenticationAgentResponseCallback, + this); +} + +void Authority::Private::authenticationAgentResponseCallback(GObject *object, GAsyncResult *result, gpointer user_data) +{ + Authority *authority = (Authority *) user_data; + Q_ASSERT(authority); + GError *error = NULL; + bool res = polkit_authority_authentication_agent_response_finish((PolkitAuthority *) object, result, &error); + if (error != NULL) { + // We don't want to set error if this is cancellation of some action + if (error->code != 1) { + authority->d->setError(E_AgentResponseFailed, error->message); + } + g_error_free(error); + return; + } + + Q_EMIT authority->authenticationAgentResponseFinished(res); +} + +void Authority::authenticationAgentResponseCancel() +{ + if (!g_cancellable_is_cancelled(d->m_authenticationAgentResponseCancellable)) { + g_cancellable_cancel(d->m_authenticationAgentResponseCancellable); + } +} + +TemporaryAuthorization::List Authority::enumerateTemporaryAuthorizationsSync(const Subject &subject) +{ + TemporaryAuthorization::List result; + + GError *error = NULL; + GList *glist = polkit_authority_enumerate_temporary_authorizations_sync(d->pkAuthority, + subject.subject(), + NULL, + &error); + if (error != NULL) { + d->setError(E_EnumFailed, error->message); + g_error_free(error); + return result; + } + + GList *glist2; + for (glist2 = glist; glist2 != NULL; glist2 = g_list_next(glist2)) { + result.append(TemporaryAuthorization((PolkitTemporaryAuthorization *) glist2->data)); + g_object_unref(glist2->data); + } + + g_list_free(glist); + + return result; +} + +void Authority::Private::enumerateTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data) +{ + Authority *authority = (Authority *) user_data; + Q_ASSERT(authority); + GError *error = NULL; + + GList *glist = polkit_authority_enumerate_temporary_authorizations_finish((PolkitAuthority *) object, result, &error); + + if (error != NULL) { + // We don't want to set error if this is cancellation of some action + if (error->code != 1) { + authority->d->setError(E_EnumFailed, error->message); + } + g_error_free(error); + return; + } + TemporaryAuthorization::List res; + GList *glist2; + for (glist2 = glist; glist2 != NULL; glist2 = g_list_next(glist2)) { + res.append(TemporaryAuthorization((PolkitTemporaryAuthorization *) glist2->data)); + g_object_unref(glist2->data); + } + + g_list_free(glist); + + Q_EMIT authority->enumerateTemporaryAuthorizationsFinished(res); +} + +void Authority::enumerateTemporaryAuthorizationsCancel() +{ + if (!g_cancellable_is_cancelled(d->m_enumerateTemporaryAuthorizationsCancellable)) { + g_cancellable_cancel(d->m_enumerateTemporaryAuthorizationsCancellable); + } +} + +bool Authority::revokeTemporaryAuthorizationsSync(const Subject &subject) +{ + bool result; + if (Authority::instance()->hasError()) { + return false; + } + + GError *error = NULL; + result = polkit_authority_revoke_temporary_authorizations_sync(d->pkAuthority, + subject.subject(), + NULL, + &error); + if (error != NULL) { + d->setError(E_RevokeFailed, error->message); + g_error_free(error); + return false; + } + return result; +} + +void Authority::revokeTemporaryAuthorizations(const Subject &subject) +{ + if (Authority::instance()->hasError()) { + return; + } + + polkit_authority_revoke_temporary_authorizations(d->pkAuthority, + subject.subject(), + d->m_revokeTemporaryAuthorizationsCancellable, + d->revokeTemporaryAuthorizationsCallback, + this); +} + +void Authority::Private::revokeTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data) +{ + Authority *authority = (Authority *) user_data; + Q_ASSERT(authority != NULL); + GError *error = NULL; + + bool res = polkit_authority_revoke_temporary_authorizations_finish((PolkitAuthority *) object, result, &error); + + if (error != NULL) { + // We don't want to set error if this is cancellation of some action + if (error->code != 1) { + authority->d->setError(E_RevokeFailed, error->message); + } + g_error_free(error); + return; + } + + Q_EMIT authority->revokeTemporaryAuthorizationsFinished(res); +} + +void Authority::revokeTemporaryAuthorizationsCancel() +{ + if (!g_cancellable_is_cancelled(d->m_revokeTemporaryAuthorizationsCancellable)) { + g_cancellable_cancel(d->m_revokeTemporaryAuthorizationsCancellable); + } +} + +bool Authority::revokeTemporaryAuthorizationSync(const QString &id) +{ + bool result; + if (Authority::instance()->hasError()) { + return false; + } + + GError *error = NULL; + result = polkit_authority_revoke_temporary_authorization_by_id_sync(d->pkAuthority, + id.toUtf8().data(), + NULL, + &error); + if (error != NULL) { + d->setError(E_RevokeFailed, error->message); + g_error_free(error); + return false; + } + return result; +} + +void Authority::revokeTemporaryAuthorization(const QString &id) +{ + if (Authority::instance()->hasError()) { + return; + } + + polkit_authority_revoke_temporary_authorization_by_id(d->pkAuthority, + id.toUtf8().data(), + d->m_revokeTemporaryAuthorizationCancellable, + d->revokeTemporaryAuthorizationCallback, + this); +} + +void Authority::Private::revokeTemporaryAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data) +{ + Authority *authority = (Authority *) user_data; + Q_ASSERT(authority != NULL); + GError *error = NULL; + + bool res = polkit_authority_revoke_temporary_authorization_by_id_finish((PolkitAuthority *) object, result, &error); + + if (error != NULL) { + // We don't want to set error if this is cancellation of some action + if (error->code != 1) { + authority->d->setError(E_RevokeFailed, error->message); + } + g_error_free(error); + return; + } + + Q_EMIT authority->revokeTemporaryAuthorizationFinished(res); +} + +void Authority::revokeTemporaryAuthorizationCancel() +{ + if (!g_cancellable_is_cancelled(d->m_revokeTemporaryAuthorizationCancellable)) { + g_cancellable_cancel(d->m_revokeTemporaryAuthorizationCancellable); + } +} + +} + +#include "polkitqt1-authority.moc" diff --git a/core/polkitqt1-authority.h b/core/polkitqt1-authority.h new file mode 100644 index 000000000..64ebb8f13 --- /dev/null +++ b/core/polkitqt1-authority.h @@ -0,0 +1,539 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Daniel Nicoletti <dantti85-pk@yahoo.com.br> + * Copyright (C) 2009 Dario Freddi <drf@kde.org> + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef POLKITQT1_AUTHORITY_H +#define POLKITQT1_AUTHORITY_H + +#include "polkitqt1-export.h" +#include "polkitqt1-identity.h" +#include "polkitqt1-subject.h" +#include "polkitqt1-temporaryauthorization.h" +#include "polkitqt1-actiondescription.h" + +#include <QtCore/QObject> +#include <QtCore/QMetaType> + +typedef struct _PolkitAuthority PolkitAuthority; +class QStringList; + +/** + * \namespace PolkitQt1 PolkitQt + * + * \brief Namespace wrapping Polkit-Qt classes + * + * This namespace wraps all Polkit-Qt classes. + */ +namespace PolkitQt1 +{ + +/** + * \class Authority polkitqt1-authority.h Authority + * \author Daniel Nicoletti <dantti85-pk@yahoo.com.br> + * \author Dario Freddi <drf@kde.org> + * \author Jaroslav Reznik <jreznik@redhat.com> + * + * \brief Convenience class for Qt/KDE applications + * + * This class is a singleton that provides makes easy the usage + * of PolKitAuthority. It emits configChanged() + * whenever PolicyKit files change (e.g. the PolicyKit.conf + * or .policy files) or when ConsoleKit reports activities changes. + * + * \note This class is a singleton, its constructor is private. + * Call Authority::instance() to get an instance of the Authority object. + * Do not delete Authority::instance(), cleanup will be done automatically. + */ +class POLKITQT1_EXPORT Authority : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY(Authority) + Q_ENUMS(Result) + Q_ENUMS(ErrorCode) +public: + enum Result { + /** Result unknown */ + Unknown = 0x00, + /** The subject is authorized for the specified action */ + Yes = 0x01, + /** The subject is not authorized for the specified action */ + No = 0x02, + /** The subject is authorized if more information is provided */ + Challenge = 0x03 + }; + + enum AuthorizationFlag { + /** No flags set **/ + None = 0x00, + /** If the subject can obtain the authorization through authentication, + * and an authentication agent is available, then attempt to do so. + * + * Note, this means that the method used for checking authorization is likely + * to block for a long time. **/ + AllowUserInteraction = 0x01 + }; + Q_DECLARE_FLAGS(AuthorizationFlags, AuthorizationFlag) + + /** Error codes for the authority class */ + enum ErrorCode { + /** No error occurred **/ + E_None = 0x00, + /** Authority cannot be obtained **/ + E_GetAuthority = 0x01, + /** Authority check failed **/ + E_CheckFailed = 0x02, + /** Wrong or empty subject was given **/ + E_WrongSubject = 0x03, + /** Action returned unknown result **/ + E_UnknownResult = 0x04, + /** Enumerating actions failed **/ + E_EnumFailed = 0x05, + /** Registration of authentication agent failed **/ + E_RegisterFailed = 0x06, + /** Unregistration of authentication agent failed **/ + E_UnregisterFailed = 0x07, + /** Cookie or polkitqt1-identity.handled to the action is empty **/ + E_CookieOrIdentityEmpty = 0x08, + /** Response of auth agent failed **/ + E_AgentResponseFailed = 0x09, + /** Revoke temporary authorizations failed **/ + E_RevokeFailed = 0x0A + }; + + /** + * \brief Returns the instance of Authority + * + * Returns the current instance of Authority. Call this function whenever + * you need to access the Authority class. + * + * \note Authority is a singleton. Memory is handled by polkit-qt, so you just + * need to call this function to get a working instance of Authority. + * Don't delete the object after having used it. + * + * \param authority use this if you want to set an explicit PolkitAuthority. If you + * don't know what this implies, simply ignore the parameter. In case + * you want to use it, be sure of streaming it the first time you call + * this function, otherwise it will have no effect. + * + * \return The current authority instance + */ + static Authority *instance(PolkitAuthority *authority = 0); + + ~Authority(); + + /** + * You should always call this method after every action. No action will be allowed + * if the object is in error state. Use clearError() to clear the error message. + * + * \see lastError + * \see clearError + * + * \return \c true if an error occurred, \c false if the library is ready + */ + bool hasError() const; + + /** + * \return the code of last error + */ + ErrorCode lastError() const; + + /** + * Get detail information about error that occurred. + * + * \return detail message + */ + const QString errorDetails() const; + + /** + * Use this method to clear the error message. + */ + void clearError(); + + /** + * Returns the current instance of PolkitAuthority. If you are handling + * it through Polkit-qt (which is quite likely, since you are calling + * this function), DO NOT use any PolicyKit API's specific method that + * modifies the instance on it, unless you're completely aware of what you're doing and + * of the possible consequencies. Use this instance only to gather information. + * + * \return the current PolkitAuthority instance + */ + PolkitAuthority *polkitAuthority() const; + + /** + * This function should be used by mechanisms (e.g.: helper applications). + * It returns the action should be carried out, so if the caller was + * actually authorized to perform it. The result is in form of a Result, so that + * you can have more control over the whole process, and detect an eventual error. + * Most of the times you simply want to check if the result is == to \c Result::Yes, + * if you don't have specific needs. + * + * It is CRITICAL that you call this function + * and check what it returns before doing anything in your helper, since otherwise + * you could be actually performing an action from an unknown or unauthorized caller. + * + * When operation is finished, signal checkAuthorizationFinish is emitted + * with result of authorization check in its parameter. + * + * \see checkAuthorizationSync Synchronous version of this method. + * \see checkAuthorizationFinished Signal that is emitted when this method finishes. + * \see checkAuthorizationCancel Use it to cancel execution of this method. + * + * \param actionId the Id of the action in question + * \param subject subject that the action is authorized for (e.g. unix process) + * \param flags flags that influences the authorization checking + * + * \return \c Result::Yes if the caller is authorized and the action should be performed + * \c otherwise if the caller was not authorized and the action should not be performed, + * or an error has occurred + * + */ + void checkAuthorization(const QString &actionId, const Subject &subject, + AuthorizationFlags flags); + + /** + * Synchronous version of the checkAuthorization method. + * + * \param actionId the Id of the action in question + * \param subject subject that the action is authorized for (e.g. unix process) + * \param flags flags that influences the authorization checking + * + * \see checkAuthorization Asynchronous version of this method. + */ + Result checkAuthorizationSync(const QString &actionId, const Subject &subject, + AuthorizationFlags flags); + + /** + * This method can be used to cancel last authorization check. + */ + void checkAuthorizationCancel(); + + /** + * Asynchronously retrieves all registered actions. + * + * When operation is finished, signal checkAuthorizationFinish is emitted + * with result of authorization check in its parameter. + * + * \see enumerateActionsSync Synchronous version of this method. + * \see enumerateActionsFinished Signal that is emitted when this method finishes. + * \see enumerateActionsCancel Use it to cancel execution of this method. + */ + void enumerateActions(); + + /** + * Synchronously retrieves all registered actions. + * + * \see enumerateActions Asynchronous version of this method. + * + * \return a list of Action IDs + */ + ActionDescription::List enumerateActionsSync(); + + /** + * This method can be used to cancel enumeration of actions + */ + void enumerateActionsCancel(); + + /** + * Registers an authentication agent. + * + * \see registerAuthenticationAgentSync Synchronous version of this method. + * \see registerAuthenticationAgentFinished Signal that is emitted when this method finishes. + * \see registerAuthenticationAgentCancel Use it to cancel execution of this method. + * + * \param subject caller subject + * \param locale the locale of the authentication agent + * \param objectPath the object path for the authentication agent + */ + void registerAuthenticationAgent(const Subject &subject, const QString &locale, + const QString &objectPath); + + /** + * Registers an authentication agent. + * + * \see registerAuthenticationAgent Asynchronous version of this method. + * + * \param subject caller subject + * \param locale the locale of the authentication agent + * \param objectPath the object path for the authentication agent + * + * \return \c true if the Authentication agent has been successfully registered + * \c false if the Authentication agent registration failed + */ + bool registerAuthenticationAgentSync(const Subject &subject, const QString &locale, + const QString &objectPath); + + /** + * This method can be used to cancel the registration of the authentication agent. + */ + void registerAuthenticationAgentCancel(); + + /** + * Unregisters an Authentication agent. + * + * \see unregisterAuthenticationAgentSync Synchronous version of this method. + * \see unregisterAuthenticationAgentFinished Signal that is emitted when this method finishes. + * \see unregisterAuthenticationAgentCancel Use it to cancel execution of this method. + * + * \param subject caller subject + * \param objectPath the object path for the Authentication agent + * + * \return \c true if the Authentication agent has been successfully unregistered + * \c false if the Authentication agent unregistration failed + */ + void unregisterAuthenticationAgent(const Subject &subject, const QString &objectPath); + + /** + * Unregisters an Authentication agent. + * + * \see unregisterAuthenticationAgent Asynchronous version of this method. + * + * \param subject caller subject + * \param objectPath the object path for the Authentication agent + * + * \return \c true if the Authentication agent has been successfully unregistered + * \c false if the Authentication agent unregistration failed + */ + bool unregisterAuthenticationAgentSync(const Subject &subject, const QString &objectPath); + + /** + * This method can be used to cancel the unregistration of the authentication agent. + */ + void unregisterAuthenticationAgentCancel(); + + /** + * Provide response that \p identity successfully authenticated for the authentication request identified by \p cookie. + * + * \see authenticationAgentResponseSync Synchronous version of this method. + * \see authenticationAgentResponseFinished Signal that is emitted when this method finishes. + * \see authenticationAgentResponseCancel Use it to cancel execution of this method. + * + * \param cookie The cookie passed to the authentication agent from the authority. + * \param identity The identity that was authenticated. + */ + void authenticationAgentResponse(const QString &cookie, const Identity &identity); + + /** + * Provide response that \p identity successfully authenticated for the authentication request identified by \p cookie. + * + * \see authenticationAgentResponse Asynchronous version of this method. + * + * \param cookie The cookie passed to the authentication agent from the authority. + * \param identity The identity that was authenticated. + * + * \return \c true if authority acknowledged the call, \c false if error is set. + * + */ + bool authenticationAgentResponseSync(const QString& cookie, const PolkitQt1::Identity& identity); + + /** + * This method can be used to cancel the authenticationAgentResponseAsync method. + */ + void authenticationAgentResponseCancel(); + + /** + * Retrieves all temporary action that applies to \p subject. + * + * \see enumerateTemporaryAuthorizationsSync Synchronous version of this method. + * \see enumerateTemporaryAuthorizationsFinished Signal that is emitted when this method finishes. + * \see enumerateTemporaryAuthorizationsCancel Use it to cancel execution of this method. + * + * \param subject the subject to get temporary authorizations for + * + */ + void enumerateTemporaryAuthorizations(const Subject &subject); + + /** + * Retrieves all temporary action that applies to \p subject + * + * \see enumerateTemporaryAuthorizations Asynchronous version of this method. + * + * \param subject the subject to get temporary authorizations for + * + * \note Free all TemporaryAuthorization objects using \p delete operator. + * + * \return List of all temporary authorizations + */ + TemporaryAuthorization::List enumerateTemporaryAuthorizationsSync(const Subject &subject); + + /** + * This method can be used to cancel the enumerateTemporaryAuthorizationsAsync method. + */ + void enumerateTemporaryAuthorizationsCancel(); + + /** + * Revokes all temporary authorizations that applies to \p subject + * + * \see revokeTemporaryAuthorizationsSync Synchronous version of this method. + * \see revokeTemporaryAuthorizationsFinished Signal that is emitted when this method finishes. + * \see revokeTemporaryAuthorizationsCancel Use it to cancel execution of this method. + + * \param subject the subject to revoke temporary authorizations from + */ + void revokeTemporaryAuthorizations(const Subject &subject); + + /** + * Revokes all temporary authorizations that applies to \p subject + * + * \see revokeTemporaryAuthorizations Asynchronous version of this method. + * + * \param subject the subject to revoke temporary authorizations from + * + * \return \c true if all temporary authorization were revoked + * \c false if the revoking failed + */ + bool revokeTemporaryAuthorizationsSync(const Subject &subject); + + /** + * This method can be used to cancel the method revokeTemporaryAuthorizationsAsync. + */ + void revokeTemporaryAuthorizationsCancel(); + + /** + * Revokes temporary authorization by \p id + * + * \see revokeTemporaryAuthorizationSync Synchronous version of this method. + * \see revokeTemporaryAuthorizationFinished Signal that is emitted when this method finishes. + * \see revokeTemporaryAuthorizationCancel Use it to cancel execution of this method. + * + * \param id the identifier of the temporary authorization + */ + void revokeTemporaryAuthorization(const QString &id); + + /** + * Revokes temporary authorization by \p id + * + * \see revokeTemporaryAuthorization Asynchronous version of this method. + * + * \param id the identifier of the temporary authorization + * + * \return \c true if the temporary authorization was revoked + * \c false if the revoking failed + */ + bool revokeTemporaryAuthorizationSync(const QString &id); + + /** + * This method can be used to cancel the method revokeTemporaryAuthorizationAsync. + */ + void revokeTemporaryAuthorizationCancel(); + +Q_SIGNALS: + /** + * This signal will be emitted when a configuration + * file gets changed (e.g. /etc/PolicyKit/PolicyKit.conf or + * .policy files). + * Connect to this signal if you want to track down + * actions. + */ + void configChanged(); + + /** + * This signal is emitted when ConsoleKit configuration + * changes. This might happen when a session becomes active + * or inactive. + * + * If you want to track your actions directly you should + * connect to this signal, as this might change the return value + * PolicyKit will give you. + * + * \note If you use Action you'll probably prefer to + * use the dataChanged() signal to track Action changes. + */ + void consoleKitDBChanged(); + + /** + * This signal is emitted when asynchronous method checkAuthorization finishes. + * + * The argument is the result of authorization. + */ + void checkAuthorizationFinished(PolkitQt1::Authority::Result); + + /** + * This signal is emitted when asynchronous method enumerateActions finishes. + * + * The argument is the list of all Action IDs. + */ + void enumerateActionsFinished(PolkitQt1::ActionDescription::List); + + /** + * This signal is emitted when asynchronous method registerAuthenticationAgent finishes. + * + * The argument is \c true if the Authentication agent has been successfully registered + * \c false if the Authentication agent registration failed + */ + void registerAuthenticationAgentFinished(bool); + + /** + * This signal is emitted when asynchronous method unregisterAuthenticationAgent finishes. + * + * The argument is \c true if the Authentication agent has been successfully unregistered + * \c false if the Authentication agent unregistration failed + */ + void unregisterAuthenticationAgentFinished(bool); + + /** + * This signal is emitted when asynchronous method authenticationAgentResponse finishes. + * + * The argument is \c true if authority acknowledged the call, \c false if error is set. + */ + void authenticationAgentResponseFinished(bool); + + /** + * This signal is emmited when asynchronous method enumerateTemporaryAuthorizations finishes. + * + * The argument is list of all temporary authorizations. + * + * \note Free all TemporaryAuthorization objects using \p delete operator. + */ + void enumerateTemporaryAuthorizationsFinished(PolkitQt1::TemporaryAuthorization::List); + + /** + * This signal is emmited when asynchronous method revokeTemporaryAuthorizations finishes. + * + * The argument is \c true if all temporary authorizations were revoked + * \c false if the revoking failed + */ + void revokeTemporaryAuthorizationsFinished(bool); + + /** + * This signal is emmited when asynchronous method revokeTemporaryAuthorization finishes. + * \return \c true if the temporary authorization was revoked + * \c false if the revoking failed + */ + void revokeTemporaryAuthorizationFinished(bool); + +private: + explicit Authority(PolkitAuthority *context, QObject *parent = 0); + + class Private; + friend class Private; + Private * const d; + + Q_PRIVATE_SLOT(d, void dbusFilter(const QDBusMessage &message)) +}; + +} + +Q_DECLARE_OPERATORS_FOR_FLAGS(PolkitQt1::Authority::AuthorizationFlags) +Q_DECLARE_METATYPE(PolkitQt1::Authority::Result) + +#endif diff --git a/core/polkitqt1-details.cpp b/core/polkitqt1-details.cpp new file mode 100644 index 000000000..e1d0f315d --- /dev/null +++ b/core/polkitqt1-details.cpp @@ -0,0 +1,99 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "polkitqt1-details.h" + +#include <QtCore/QStringList> + +#include <polkit/polkit.h> + +namespace PolkitQt1 +{ + +class Details::Data : public QSharedData +{ +public: + Data() {} + Data(const Data &other) + : QSharedData(other) + , polkitDetails(other.polkitDetails) + { + g_object_ref(polkitDetails); + } + ~Data() + { + g_object_unref(polkitDetails); + } + + PolkitDetails *polkitDetails; +}; + +Details::Details() + : d(new Data) +{ + g_type_init(); + d->polkitDetails = polkit_details_new(); +} + +Details::Details(PolkitDetails *pkDetails) + : d(new Data) +{ + g_type_init(); + d->polkitDetails = pkDetails; +} + +Details::~Details() +{ +} + +Details& Details::operator=(const PolkitQt1::Details& other) +{ + d = other.d; + return *this; +} + +QString Details::lookup(const QString &key) const +{ + const gchar *result = polkit_details_lookup(d->polkitDetails, key.toUtf8().data()); + if (result != NULL) { + return QString::fromUtf8(result); + } else { + return QString(); + } +} + +void Details::insert(const QString &key, const QString &value) +{ + polkit_details_insert(d->polkitDetails, key.toUtf8().data(), value.toUtf8().data()); +} + +QStringList Details::keys() const +{ + gchar **result = polkit_details_get_keys(d->polkitDetails); + QStringList list; + int len = g_strv_length(result); + for (int i = 0; i < len; i++) { + list.append(QString::fromUtf8(result[i])); + } + g_strfreev(result); + return list; +} + +} diff --git a/core/polkitqt1-details.h b/core/polkitqt1-details.h new file mode 100644 index 000000000..dc55f854c --- /dev/null +++ b/core/polkitqt1-details.h @@ -0,0 +1,97 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef POLKITQT1_DETAILS_H +#define POLKITQT1_DETAILS_H + +#include "polkitqt1-export.h" + +#include <QtCore/QObject> +#include <QtCore/QSharedData> + +typedef struct _PolkitDetails PolkitDetails; + +/** + * \namespace PolkitQt1 PolkitQt + * + * \brief Namespace wrapping Polkit-Qt classes + * + * This namespace wraps all Polkit-Qt classes. + */ +namespace PolkitQt1 +{ + +/** + * \class Details polkitqt1-details.h Details + * \author Radek Novacek <rnovacek@redhat.com> + * + * \brief Class used for passing details around. + */ +class POLKITQT1_EXPORT Details +{ +public: + /** + * Creates a new Details object + */ + Details(); + + /** + * Creates Details object from PolkitDetails + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param pkDetails PolkitDetails object + */ + explicit Details(PolkitDetails *pkDetails); + + ~Details(); + + Details &operator=(const Details &other); + + /** + * Get the value for \p key + * + * \param key A key + * \return Value of the key \p key, otherwise empty QString. + */ + QString lookup(const QString &key) const; + + /** + * Inserts key \p key with value \p value. + * + * \param key A key. + * \param value A value. + */ + void insert(const QString &key, const QString &value); + + /** + * Gets a list of all keys. + * + * \return List of all keys. + */ + QStringList keys() const; +private: + class Data; + QExplicitlySharedDataPointer< Data > d; +}; + +} + +#endif // DETAILS_H diff --git a/core/polkitqt1-identity.cpp b/core/polkitqt1-identity.cpp new file mode 100644 index 000000000..354107213 --- /dev/null +++ b/core/polkitqt1-identity.cpp @@ -0,0 +1,227 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Lukas Tinkl <ltinkl@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "polkitqt1-identity.h" + +#include <polkit/polkit.h> + +#include <QtCore/QDebug> + +namespace PolkitQt1 +{ + +class Identity::Data : public QSharedData +{ +public: + Data() : identity(0) {} + Data(const Data& other) + : QSharedData(other) + , identity(other.identity) + { + if (identity) { + g_object_ref(identity); + } + } + ~Data() + { + if (identity) { + g_object_unref(identity); + } + } + + PolkitIdentity *identity; +}; + +Identity::Identity() + : d(new Data) +{ + g_type_init(); +} + +Identity::Identity(PolkitIdentity *polkitIdentity) + : d(new Data) +{ + g_type_init(); + d->identity = polkitIdentity; + + if (d->identity) { + g_object_ref(d->identity); + } +} + +Identity::Identity(const PolkitQt1::Identity& other) + : d(other.d) +{ + +} + +Identity::~Identity() +{ +} + +Identity& Identity::operator=(const PolkitQt1::Identity& other) +{ + d = other.d; + return *this; +} + +bool Identity::isValid() const +{ + return d->identity != NULL; +} + +PolkitIdentity *Identity::identity() const +{ + return d->identity; +} + +void Identity::setIdentity(PolkitIdentity *identity) +{ + if (d->identity == identity) { + return; + } + + if (d->identity) { + g_object_unref(d->identity); + } + + d->identity = identity; + + if (d->identity) { + g_object_ref(d->identity); + } +} + +QString Identity::toString() const +{ + Q_ASSERT(d->identity); + return QString::fromUtf8(polkit_identity_to_string(d->identity)); +} + +Identity Identity::fromString(const QString &string) +{ + GError *error = NULL; + PolkitIdentity *pkIdentity = polkit_identity_from_string(string.toUtf8().data(), &error); + if (error != NULL) { + qWarning() << QString("Cannot create Identity from string: %1").arg(error->message); + return Identity(); + } + return Identity(pkIdentity); +} + +UnixGroupIdentity Identity::toUnixGroupIdentity() +{ + UnixGroupIdentity *ugid = static_cast< UnixGroupIdentity* >(this); + if (!ugid) { + return UnixGroupIdentity(); + } + + return *ugid; +} + +UnixUserIdentity Identity::toUnixUserIdentity() +{ + UnixUserIdentity *uuid = static_cast< UnixUserIdentity* >(this); + if (!uuid) { + return UnixUserIdentity(); + } + + return *uuid; +} + +UnixUserIdentity::UnixUserIdentity(const QString &name) + : Identity() +{ + GError *error = NULL; + setIdentity(polkit_unix_user_new_for_name(name.toUtf8().data(), &error)); + if (error != NULL) { + qWarning() << QString("Cannot create UnixUserIdentity: %1").arg(error->message); + setIdentity(NULL); + } +} + +UnixUserIdentity::UnixUserIdentity(uid_t uid) + : Identity() +{ + setIdentity(polkit_unix_user_new(uid)); +} + +UnixUserIdentity::UnixUserIdentity(PolkitUnixUser *pkUnixUser) + : Identity((PolkitIdentity *)pkUnixUser) +{ + +} + +UnixUserIdentity::UnixUserIdentity() + : Identity() +{ + +} + +uid_t UnixUserIdentity::uid() const +{ + return polkit_unix_user_get_uid((PolkitUnixUser *) identity()); +} + +void UnixUserIdentity::setUid(uid_t uid) +{ + polkit_unix_user_set_uid((PolkitUnixUser *) identity(), uid); +} + +UnixGroupIdentity::UnixGroupIdentity(const QString &name) + : Identity() +{ + GError *error = NULL; + setIdentity(polkit_unix_group_new_for_name(name.toUtf8().data(), &error)); + if (error != NULL) { + qWarning() << QString("Cannot create UnixGroupIdentity: %1").arg(error->message); + setIdentity(NULL); + } +} + +UnixGroupIdentity::UnixGroupIdentity(gid_t gid) + : Identity() +{ + setIdentity(polkit_unix_group_new(gid)); +} + +UnixGroupIdentity::UnixGroupIdentity(PolkitUnixGroup *pkUnixGroup) + : Identity((PolkitIdentity *) pkUnixGroup) +{ + +} + +UnixGroupIdentity::UnixGroupIdentity() + : Identity() +{ + +} + +gid_t UnixGroupIdentity::gid() const +{ + return polkit_unix_group_get_gid((PolkitUnixGroup *) identity()); +} + +void UnixGroupIdentity::setGid(gid_t gid) +{ + polkit_unix_group_set_gid((PolkitUnixGroup *) identity(), gid); +} + +} diff --git a/core/polkitqt1-identity.h b/core/polkitqt1-identity.h new file mode 100644 index 000000000..faed3a332 --- /dev/null +++ b/core/polkitqt1-identity.h @@ -0,0 +1,211 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Lukas Tinkl <ltinkl@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef POLKITQT1_IDENTITY_H +#define POLKITQT1_IDENTITY_H + +#include "polkitqt1-export.h" + +#include <unistd.h> + +#include <QtCore/QObject> +#include <QtCore/QSharedData> + +typedef struct _PolkitIdentity PolkitIdentity; +typedef struct _PolkitUnixUser PolkitUnixUser; +typedef struct _PolkitUnixGroup PolkitUnixGroup; + +/** + * \namespace PolkitQt1 PolkitQt + * + * \brief Namespace wrapping Polkit-Qt classes + * + * This namespace wraps all Polkit-Qt classes. + */ +namespace PolkitQt1 +{ + +class UnixUserIdentity; +class UnixGroupIdentity; + +/** + * \class Identity polkitqt1-identity.h Identity + * \author Lukas Tinkl <ltinkl@redhat.com> + * + * This class encapsulates the PolkitIdentity interface. + * + * \brief Abstract class representing identities + * + * \see UnixGroup + * \see UnixUser + */ +class POLKITQT1_EXPORT Identity +{ +public: + typedef QList< Identity > List; + + Identity(); + explicit Identity(PolkitIdentity *polkitIdentity); + Identity(const Identity &other); + + ~Identity(); + + Identity &operator=(const Identity &other); + + bool isValid() const; + + /** + * Serialization of object to the string + * + * \return Serialized Identity object + */ + QString toString() const; + + /** + * Creates the Identity object from string representation + * + * \param string string representation of the object + * + * \return Pointer to new Identity instance + */ + static Identity fromString(const QString &string); + + UnixUserIdentity toUnixUserIdentity(); + UnixGroupIdentity toUnixGroupIdentity(); + + /** + * Gets PolkitIdentity object. + * + * \warning It shouldn't be used directly unless you are completely aware of what are you doing + * + * \return Pointer to PolkitIdentity instance + */ + PolkitIdentity *identity() const; +protected: + void setIdentity(PolkitIdentity *identity); + +private: + class Data; + QExplicitlySharedDataPointer< Data > d; +}; + +/** + * \class UnixUserIdentity polkitqt1-identity.h Identity + * + * An object representing a user identity on a UNIX system. + * + * \brief UNIX user identity + * \sa Identity + */ +class POLKITQT1_EXPORT UnixUserIdentity : public Identity +{ +public: + UnixUserIdentity(); + /** + * Creates UnixUser object by UID of the user + * + * \param uid user id + */ + explicit UnixUserIdentity(uid_t uid); + + /** + * Creates UnixUser object by unix name of the user + * + * \param name Unix name + */ + explicit UnixUserIdentity(const QString &name); + + /** + * Creates UnixUser object from PolkitUnixUser object + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param pkUnixUser The PolkitUnixUser object + */ + explicit UnixUserIdentity(PolkitUnixUser *pkUnixUser); + + /** + * Gets an user id + * + * \return user id + */ + uid_t uid() const; + + /** + * Sets the id of user + * + * \param uid user id + */ + void setUid(uid_t uid); +}; + +/** + * \class UnixGroupIdentity polkitqt1-identity.h Identity + * + * An object representing a group identity on a UNIX system. + * + * \brief UNIX group identity + * \sa Identity + */ +class POLKITQT1_EXPORT UnixGroupIdentity : public Identity +{ +public: + UnixGroupIdentity(); + /** + * Creates UnixGroup object by GID of the group + * + * \param gid group id + */ + explicit UnixGroupIdentity(gid_t gid); + + /** + * Creates UnixGroup object by unix name of the group + * + * \param name group name + */ + explicit UnixGroupIdentity(const QString &name); + + /** + * Creates UnixGroup object from PolkitUnixGroup object + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param pkUnixGroup The PolkitUnixGroup object + */ + explicit UnixGroupIdentity(PolkitUnixGroup *pkUnixGroup); + + /** + * Gets a group id + * + * \return group id + */ + gid_t gid() const; + + /** + * Sets the id of group + * + * \param gid group id + */ + void setGid(gid_t gid); +}; + +} + +#endif // POLKIT_QT_IDENTITY_H diff --git a/core/polkitqt1-subject.cpp b/core/polkitqt1-subject.cpp new file mode 100644 index 000000000..dfc969711 --- /dev/null +++ b/core/polkitqt1-subject.cpp @@ -0,0 +1,205 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "polkitqt1-subject.h" + +#include <QtCore/QDebug> +#include <polkit/polkit.h> + +namespace PolkitQt1 +{ + +class Subject::Data : public QSharedData +{ +public: + Data() + : QSharedData() + , subject(0) + {} + Data(const Data& other) + : QSharedData(other) + , subject(other.subject) + { + g_object_ref(subject); + } + ~Data() + { + g_object_unref(subject); + } + + PolkitSubject *subject; +}; + +Subject::Subject() + : d(new Data) +{ + g_type_init(); +} + +Subject::Subject(PolkitSubject *subject) + : d(new Data) +{ + g_type_init(); + d->subject = subject; +} + +Subject::Subject(const PolkitQt1::Subject& other) + : d(other.d) +{ + +} + +Subject& Subject::operator=(const PolkitQt1::Subject& other) +{ + d = other.d; + return *this; +} + +Subject::~Subject() +{ +} + +bool Subject::isValid() const +{ + return d->subject != NULL; +} + +PolkitSubject *Subject::subject() const +{ + return d->subject; +} + +void Subject::setSubject(PolkitSubject *subject) +{ + d->subject = subject; +} + +QString Subject::toString() const +{ + Q_ASSERT(d->subject); + return QString::fromUtf8(polkit_subject_to_string(d->subject)); +} + +Subject Subject::fromString(const QString &string) +{ + // should be in polkit library!!! but for strange reason it's necessary to have it here + g_type_init(); + + Subject subject; + GError *error = NULL; + subject.d->subject = polkit_subject_from_string(string.toUtf8().data(), &error); + if (error != NULL) { + qWarning() << QString("Cannot create Subject from string: %1").arg(error->message); + return NULL; + } + return subject; +} + +UnixProcessSubject::UnixProcessSubject(qint64 pid) + : Subject() +{ + setSubject(polkit_unix_process_new(pid)); +} + +UnixProcessSubject::UnixProcessSubject(qint64 pid, quint64 startTime) + : Subject() +{ + setSubject(polkit_unix_process_new_full(pid, startTime)); +} + +UnixProcessSubject::UnixProcessSubject(PolkitUnixProcess *pkUnixProcess) + : Subject((PolkitSubject *) pkUnixProcess) +{ + +} + +qint64 UnixProcessSubject::pid() const +{ + return polkit_unix_process_get_pid((PolkitUnixProcess *) subject()); +} + +qint64 UnixProcessSubject::startTime() const +{ + return polkit_unix_process_get_start_time((PolkitUnixProcess *) subject()); +} + +void UnixProcessSubject::setPid(qint64 pid) +{ + polkit_unix_process_set_pid((PolkitUnixProcess *) subject(), pid); +} + +// ----- SystemBusName +SystemBusNameSubject::SystemBusNameSubject(const QString &name) + : Subject() +{ + setSubject(polkit_system_bus_name_new(name.toUtf8().data())); +} + +SystemBusNameSubject::SystemBusNameSubject(PolkitSystemBusName *pkSystemBusName) + : Subject((PolkitSubject *) pkSystemBusName) +{ + +} + +QString SystemBusNameSubject::name() const +{ + return QString::fromUtf8(polkit_system_bus_name_get_name((PolkitSystemBusName *) subject())); +} + +void SystemBusNameSubject::setName(const QString &name) +{ + polkit_system_bus_name_set_name((PolkitSystemBusName *) subject(), name.toUtf8().data()); +} + +// ----- SystemSession +UnixSessionSubject::UnixSessionSubject(const QString &sessionId) + : Subject() +{ + setSubject(polkit_unix_session_new(sessionId.toUtf8().data())); +} + +UnixSessionSubject::UnixSessionSubject(qint64 pid) + : Subject() +{ + GError *error = NULL; + setSubject(polkit_unix_session_new_for_process_sync(pid, NULL, &error)); + if (error != NULL) { + qWarning() << QString("Cannot create unix session: %1").arg(error->message); + setSubject(NULL); + } +} + +UnixSessionSubject::UnixSessionSubject(PolkitSystemBusName *pkUnixSession) + : Subject((PolkitSubject *) pkUnixSession) +{ + +} + +QString UnixSessionSubject::sessionId() const +{ + return QString::fromUtf8(polkit_unix_session_get_session_id((PolkitUnixSession *) subject())); +} + +void UnixSessionSubject::setSessionId(const QString &sessionId) +{ + polkit_unix_session_set_session_id((PolkitUnixSession *) subject(), sessionId.toUtf8().data()); +} + +} diff --git a/core/polkitqt1-subject.h b/core/polkitqt1-subject.h new file mode 100644 index 000000000..800318dfe --- /dev/null +++ b/core/polkitqt1-subject.h @@ -0,0 +1,262 @@ +/* + * This file is part of the PolKit1-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef POLKITQT1_SUBJECT_H +#define POLKITQT1_SUBJECT_H + +#include "polkitqt1-export.h" + +#include <QtCore/QObject> +#include <QtCore/QSharedData> + +typedef struct _PolkitSubject PolkitSubject; +typedef struct _PolkitUnixProcess PolkitUnixProcess; +typedef struct _PolkitSystemBusName PolkitSystemBusName; + +/** + * \namespace PolkitQt1 PolkitQt + * + * \brief Namespace wrapping PolicyKit-Qt classes + * + * This namespace wraps all PolicyKit-Qt classes. + */ +namespace PolkitQt1 +{ + +/** + * \class Subject polkitqt1-subject.h Subject + * \author Jaroslav Reznik <jreznik@redhat.com> + * + * \brief This class represents PolicyKit subjects + * + * This class encapsulates the PolkitSubject interface. + * + * \see UnixProcess + * \see SystemBusName + * \see UnixSession + */ +class POLKITQT1_EXPORT Subject +{ +public: + Subject(); + Subject(const Subject &other); + ~Subject(); + + Subject &operator=(const Subject &other); + + bool isValid() const; + + /** + * Serialization of object to the string + * + * \return Serialized Subject object + */ + QString toString() const; + + /** + * Creates the Subject object from string reprezentation + * + * \param string string reprezentation of the object + * + * \return Pointer to new Subject instance + */ + static Subject fromString(const QString &string); + + /** + * Gets PolkitSubject object. + * + * \warning It shouldn't be used directly unless you are completely aware of what are you doing + * + * \return Pointer to PolkitSubject instance + */ + PolkitSubject *subject() const; + +protected: + Subject(PolkitSubject *subject); + + void setSubject(PolkitSubject *subject); + +private: + class Data; + QExplicitlySharedDataPointer< Data > d; +}; + +/** + * \class UnixProcessSubject polkitqt1-subject.h Subject + * \author Jaroslav Reznik <jreznik@redhat.com> + * + * \brief A class for representing a UNIX process. + * + * To uniquely identify processes, both the process + * id and the start time of the process (a monotonic + * increasing value representing the time since the + * kernel was started) is used. + * + * \sa Subject + */ +class POLKITQT1_EXPORT UnixProcessSubject : public Subject +{ +public: + /** + * Subject constructor, takes one parameter - PID. The start time + * of process will be looked automatically. + * + * \param pid An Unix process PID. + */ + explicit UnixProcessSubject(qint64 pid); + + /** + * Subject constructor, takes two parameters - PID and start time. + * + * \param pid An Unix process PID. + * \param startTime An Unix process start time. + */ + UnixProcessSubject(qint64 pid, quint64 startTime); + + /** + * Subject constructor, it creates UnixProcess object from PolkitUnixProcess object + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param process PolkitUnixProcess object + */ + explicit UnixProcessSubject(PolkitUnixProcess *process); + + /** + * Returns Unix process PID. + * + * \return A PID of associated Unix process. + */ + qint64 pid() const; + + /** + * Returns Unix process start time. + * + * \return A start time of associated Unix process. + */ + qint64 startTime() const; + + /** + * Sets Unix process PID. + * + * \param pid An Unix process PID. + */ + void setPid(qint64 pid); +}; + +/** + * \class SystemBusNameSubject polkitqt1-subject.h Subject + * \author Jaroslav Reznik <jreznik@redhat.com> + * + * \brief A class for representing a process owning a unique name on the system bus. + * + * \sa Subject + */ +class POLKITQT1_EXPORT SystemBusNameSubject : public Subject +{ +public: + /** + * Subject constructor, takes one parameter - system bus name. + * + * \param name A unique system bus name. + */ + explicit SystemBusNameSubject(const QString &name); + + /** + * Subject constructor, it creates SystemBusName object from PolkitSystemBusName object + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param pkSystemBusName PolkitSystemBusName object + */ + explicit SystemBusNameSubject(PolkitSystemBusName *pkSystemBusName); + + /** + * Returns system bus name. + * + * \return A unique system bus name. + */ + QString name() const; + + /** + * Sets system bus name. + * + * \param name System bus name. + */ + void setName(const QString &name); +}; + +/** + * \class UnixSessionSubject polkitqt1-subject.h Subject + * \author Jaroslav Reznik <jreznik@redhat.com> + * + * \brief A class for representing unix session. + * + * The session id is an opaque string obtained from + * ConsoleKit. + * + * \sa Subject + */ +class POLKITQT1_EXPORT UnixSessionSubject : public Subject +{ +public: + /** + * Subject constructor, takes one parameter - session id. + * + * \param sessionId The session id. + */ + explicit UnixSessionSubject(const QString &sessionId); + + /** + * Subject constructor, takes one parameter - pid of process. + * + * Synchronous! + * + * \param pid The session's process pid. + */ + explicit UnixSessionSubject(qint64 pid); + + /** + * Subject constructor, it creates UnixSession object from PolkitUnixSession object + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param pkUnixSession PolkitUnixSession object + */ + explicit UnixSessionSubject(PolkitSystemBusName *pkUnixSession); + + /** + * Returns session id. + * + * \return A session id. + */ + QString sessionId() const; + + /** + * Sets session id. + * + * \param sessionId A session id. + */ + void setSessionId(const QString &sessionId); +}; + +} + +#endif diff --git a/core/polkitqt1-temporaryauthorization.cpp b/core/polkitqt1-temporaryauthorization.cpp new file mode 100644 index 000000000..5242faf7a --- /dev/null +++ b/core/polkitqt1-temporaryauthorization.cpp @@ -0,0 +1,117 @@ +/* + * This file is part of the PolKit1-qt project + * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "polkitqt1-temporaryauthorization.h" +#include "polkitqt1-authority.h" + +#include <polkit/polkit.h> + +namespace PolkitQt1 +{ + +class TemporaryAuthorization::Data : public QSharedData +{ +public: + Data() {} + Data(const Data& other) + : QSharedData(other) + , id(other.id) + , actionId(other.actionId) + , subject(other.subject) + , timeObtained(other.timeObtained) + , timeExpires(other.timeExpires) + { + } + ~Data() {} + + QString id; + QString actionId; + Subject subject; + QDateTime timeObtained; + QDateTime timeExpires; +}; + +TemporaryAuthorization::TemporaryAuthorization(PolkitTemporaryAuthorization *pkTemporaryAuthorization) + : d(new Data) +{ + g_type_init(); + d->id = QString::fromUtf8(polkit_temporary_authorization_get_id(pkTemporaryAuthorization)); + d->actionId = QString::fromUtf8(polkit_temporary_authorization_get_action_id(pkTemporaryAuthorization)); + d->subject = Subject::fromString(polkit_subject_to_string(polkit_temporary_authorization_get_subject(pkTemporaryAuthorization))); + d->timeObtained = QDateTime::fromTime_t(polkit_temporary_authorization_get_time_obtained(pkTemporaryAuthorization)); + d->timeExpires = QDateTime::fromTime_t(polkit_temporary_authorization_get_time_expires(pkTemporaryAuthorization)); + g_object_unref(pkTemporaryAuthorization); +} + +TemporaryAuthorization::TemporaryAuthorization(const PolkitQt1::TemporaryAuthorization& other) + : d(other.d) +{ + +} + +TemporaryAuthorization::TemporaryAuthorization() + : d(new Data) +{ + +} + +TemporaryAuthorization& TemporaryAuthorization::operator=(const PolkitQt1::TemporaryAuthorization& other) +{ + d = other.d; + return *this; +} + +TemporaryAuthorization::~TemporaryAuthorization() +{ +} + +QString TemporaryAuthorization::id() const +{ + return d->id; +} + +QString TemporaryAuthorization::actionId() const +{ + return d->actionId; +} + +Subject TemporaryAuthorization::subject() const +{ + //qFatal(polkit_subject_to_string(polkit_temporary_authorization_get_subject(d->temporaryAuthorization))); + return d->subject;//Subject::fromString(polkit_subject_to_string(d->subject)); +} + +QDateTime TemporaryAuthorization::obtainedAt() const +{ + return d->timeObtained; +} + +QDateTime TemporaryAuthorization::expirationTime() const +{ + return d->timeExpires; +} + +bool TemporaryAuthorization::revoke() +{ + Authority::instance()->revokeTemporaryAuthorization(id()); + return true; +} + +} diff --git a/core/polkitqt1-temporaryauthorization.h b/core/polkitqt1-temporaryauthorization.h new file mode 100644 index 000000000..f8a13ea17 --- /dev/null +++ b/core/polkitqt1-temporaryauthorization.h @@ -0,0 +1,124 @@ +/* + * This file is part of the PolKit1-qt project + * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef POLKITQT1_TEMPORARYAUTHORIZATION_H +#define POLKITQT1_TEMPORARYAUTHORIZATION_H + +#include "polkitqt1-subject.h" + +#include <QtCore/QObject> +#include <QtCore/QDateTime> +#include <QtCore/QMetaType> +#include <QtCore/QSharedData> + +typedef struct _PolkitTemporaryAuthorization PolkitTemporaryAuthorization; + +/** + * \namespace PolkitQt1 PolkitQt + * + * \brief Namespace wrapping PolicyKit-Qt classes + * + * This namespace wraps all PolicyKit-Qt classes. + */ +namespace PolkitQt1 +{ + +/** + * \class TemporaryAuthorization polkitqt1-temporaryauthorization.h TemporaryAuthorization + * \author Radek Novacek <rnovacek@redhat.com> + * + * \brief This class represents PolicyKit temporary authorization + * + * This class encapsulates the PolkitTemporaryAuthorization interface. + */ +class POLKITQT1_EXPORT TemporaryAuthorization +{ +public: + typedef QList< TemporaryAuthorization > List; + TemporaryAuthorization(); + /** + * Creates TemporaryAuthorization object from PolkitTemporaryAuthorization + * + * \warning It shouldn't be used directly unless you are completely aware of what are you doing + * + * \param pkTemporaryAuthorization PolkitTemporaryAuthorization object + * \param parent + */ + explicit TemporaryAuthorization(PolkitTemporaryAuthorization *pkTemporaryAuthorization); + TemporaryAuthorization(const TemporaryAuthorization &other); + + ~TemporaryAuthorization(); + + TemporaryAuthorization &operator=(const TemporaryAuthorization &other); + + /** + * \brief Gets the identifier for the authorization. + * + * This identifier can be user by the revokeTemporaryAuthorization function + * + * \return Unique identifier for the authorization + */ + QString id() const; + + /** + * \brief Gets the identifier of the action that authorization is for + * + * \return String that identifies the action + */ + QString actionId() const; + + /** + * \brief Gets the subject that authorization is for + * + * \return A Subject. + */ + Subject subject() const; + + /** + * \brief Gets the time when authorization was obtained + * + * \return Time of obtaining the authorization + */ + QDateTime obtainedAt() const; + + /** + * \brief Gets the time when authorizaton will expire + * + * \return Time of expiration + */ + QDateTime expirationTime() const; + + /** + * \brief Revoke temporary authorization + * + * \return \c true Authorization has been revoked + * \c false Revoking authorization failed + */ + bool revoke(); + +private: + class Data; + QSharedDataPointer< Data > d; +}; +} + +Q_DECLARE_METATYPE(PolkitQt1::TemporaryAuthorization::List) + +#endif // TEMPORARYAUTHORIZATION_H |