diff --git a/src/kauthaction.cpp b/src/kauthaction.cpp index d8a736c..5de7c23 100644 --- a/src/kauthaction.cpp +++ b/src/kauthaction.cpp @@ -1,212 +1,212 @@ /* * Copyright (C) 2009-2012 Dario Freddi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . */ #include "kauthaction.h" #include -#include +#include class QWidget; #include "kauthexecutejob.h" #include "BackendsManager.h" namespace KAuth { class ActionData : public QSharedData { public: ActionData() : valid(false), parent(nullptr), timeout(-1) {} ActionData(const ActionData &other) : QSharedData(other) , name(other.name) , details(other.details) , helperId(other.helperId) , args(other.args) , valid(other.valid) , parent(other.parent) , timeout(other.timeout) {} ~ActionData() {} QString name; QString details; QString helperId; QVariantMap args; bool valid; QWidget *parent = nullptr; int timeout; }; // Constructors Action::Action() : d(new ActionData()) { } Action::Action(const Action &action) : d(action.d) { } Action::Action(const QString &name) : d(new ActionData()) { setName(name); BackendsManager::authBackend()->setupAction(d->name); } Action::Action(const QString &name, const QString &details) : d(new ActionData()) { setName(name); setDetails(details); BackendsManager::authBackend()->setupAction(d->name); } Action::~Action() { } // Operators Action &Action::operator=(const Action &action) { if (this == &action) { // Protect against self-assignment return *this; } d = action.d; return *this; } bool Action::operator==(const Action &action) const { return d->name == action.d->name; } bool Action::operator!=(const Action &action) const { return d->name != action.d->name; } // Accessors QString Action::name() const { return d->name; } void Action::setName(const QString &name) { d->name = name; // Does the backend support checking for known actions? if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::CheckActionExistenceCapability) { // In this case, just ask the backend d->valid = BackendsManager::authBackend()->actionExists(name); } else { // Otherwise, check through a regexp - QRegExp exp(QLatin1String("[0-z]+(\\.[0-z]+)*")); - d->valid = exp.exactMatch(name); + const QRegularExpression re(QRegularExpression::anchoredPattern(QStringLiteral("[0-z]+(\\.[0-z]+)*"))); + d->valid = re.match(name).hasMatch(); } } // Accessors int Action::timeout() const { return d->timeout; } void Action::setTimeout(int timeout) { d->timeout = timeout; } QString Action::details() const { return d->details; } void Action::setDetails(const QString &details) { d->details = details; } bool Action::isValid() const { return d->valid; } void Action::setArguments(const QVariantMap &arguments) { d->args = arguments; } void Action::addArgument(const QString &key, const QVariant &value) { d->args.insert(key, value); } QVariantMap Action::arguments() const { return d->args; } QString Action::helperId() const { return d->helperId; } // TODO: Check for helper id's syntax void Action::setHelperId(const QString &id) { d->helperId = id; } void Action::setParentWidget(QWidget *parent) { d->parent = parent; } QWidget *Action::parentWidget() const { return d->parent; } Action::AuthStatus Action::status() const { if (!isValid()) { return Action::InvalidStatus; } return BackendsManager::authBackend()->actionStatus(d->name); } ExecuteJob *Action::execute(ExecutionMode mode) { return new ExecuteJob(*this, mode, nullptr); } bool Action::hasHelper() const { return !d->helperId.isEmpty(); } } // namespace Auth diff --git a/src/policy-gen/policy-gen.cpp b/src/policy-gen/policy-gen.cpp index 2b91191..bc58919 100644 --- a/src/policy-gen/policy-gen.cpp +++ b/src/policy-gen/policy-gen.cpp @@ -1,174 +1,177 @@ /* * Copyright (C) 2008 Nicola Gigante * Copyright (C) 2009 Dario Freddi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . */ #include "policy-gen.h" #include #include #include -#include +#include #include #include #include #include using namespace std; QList parse(QSettings &ini); QMap parseDomain(QSettings &ini); int main(int argc, char **argv) { QCoreApplication app(argc, argv); if (argc < 2) { qCritical("Too few arguments"); return 1; } QSettings ini(QFile::decodeName(argv[1]), QSettings::IniFormat); ini.setIniCodec("UTF-8"); if (ini.status()) { qCritical("Error loading file: %s", argv[1]); return 1; } if (argc == 3) { // Support an optional 2nd argument pointing to the output file // // This is safer to use in build systems than // "kauth-policy-gen foo.actions > foo.policy" because when using a // redirection "foo.policy" is created even if kauth-policy-gen fails. // This means the first call to make fails, but a second call succeeds // because an empty "foo.policy" exists. if (!freopen(argv[2], "w", stdout)) { qCritical("Failed to open %s for writing: %s", argv[2], strerror(errno)); return 1; } } output(parse(ini), parseDomain(ini)); } QList parse(QSettings &ini) { QList actions; - QRegExp actionExp(QLatin1String("[0-9a-z]+(\\.[0-9a-z]+)*")); - QRegExp descriptionExp(QLatin1String("description(?:\\[(\\w+)\\])?")); - QRegExp nameExp(QLatin1String("name(?:\\[(\\w+)\\])?")); - QRegExp policyExp(QLatin1String("yes|no|auth_self|auth_admin")); + const QRegularExpression actionExp(QRegularExpression::anchoredPattern(QStringLiteral("[0-9a-z]+(\\.[0-9a-z]+)*"))); - descriptionExp.setCaseSensitivity(Qt::CaseInsensitive); - nameExp.setCaseSensitivity(Qt::CaseInsensitive); + const QRegularExpression descriptionExp(QRegularExpression::anchoredPattern(QStringLiteral("description(?:\\[(\\w+)\\])?")) + , QRegularExpression::CaseInsensitiveOption); + + const QRegularExpression nameExp(QRegularExpression::anchoredPattern(QStringLiteral("name(?:\\[(\\w+)\\])?")) + , QRegularExpression::CaseInsensitiveOption); + + const QRegularExpression policyExp(QRegularExpression::anchoredPattern(QStringLiteral("(?:yes|no|auth_self|auth_admin)"))); const auto listChilds = ini.childGroups(); for (const QString &name : listChilds) { Action action; if (name == QLatin1String("Domain")) { continue; } - if (!actionExp.exactMatch(name)) { + if (!actionExp.match(name).hasMatch()) { qCritical("Wrong action syntax: %s\n", name.toLatin1().data()); exit(1); } action.name = name; ini.beginGroup(name); const auto listChildKeys = ini.childKeys(); for (const QString &key : listChildKeys) { - if (descriptionExp.exactMatch(key)) { - QString lang = descriptionExp.capturedTexts().at(1); + QRegularExpressionMatch match; + if ((match = descriptionExp.match(key)).hasMatch()) { + QString lang = match.captured(); if (lang.isEmpty()) { lang = QString::fromLatin1("en"); } action.descriptions.insert(lang, ini.value(key).toString()); - } else if (nameExp.exactMatch(key)) { - QString lang = nameExp.capturedTexts().at(1); + } else if ((match = nameExp.match(key)).hasMatch()) { + QString lang = match.captured(); if (lang.isEmpty()) { lang = QString::fromLatin1("en"); } action.messages.insert(lang, ini.value(key).toString()); } else if (key.toLower() == QLatin1String("policy")) { QString policy = ini.value(key).toString(); - if (!policyExp.exactMatch(policy)) { + if (!policyExp.match(policy).hasMatch()) { qCritical("Wrong policy: %s", policy.toLatin1().data()); exit(1); } action.policy = policy; } else if (key.toLower() == QLatin1String("policyinactive")) { QString policyInactive = ini.value(key).toString(); - if (!policyExp.exactMatch(policyInactive)) { + if (!policyExp.match(policyInactive).hasMatch()) { qCritical("Wrong policy: %s", policyInactive.toLatin1().data()); exit(1); } action.policyInactive = policyInactive; } else if (key.toLower() == QLatin1String("persistence")) { QString persistence = ini.value(key).toString(); if (persistence != QLatin1String("session") && persistence != QLatin1String("always")) { qCritical("Wrong persistence: %s", persistence.toLatin1().data()); exit(1); } action.persistence = persistence; } } if (action.policy.isEmpty() || action.messages.isEmpty() || action.descriptions.isEmpty()) { qCritical("Missing option in action: %s", name.toLatin1().data()); exit(1); } ini.endGroup(); actions.append(action); } return actions; } QMap parseDomain(QSettings &ini) { QMap rethash; if (ini.childGroups().contains(QString::fromLatin1("Domain"))) { if (ini.contains(QString::fromLatin1("Domain/Name"))) { rethash[QString::fromLatin1("vendor")] = ini.value(QString::fromLatin1("Domain/Name")).toString(); } if (ini.contains(QString::fromLatin1("Domain/URL"))) { rethash[QString::fromLatin1("vendorurl")] = ini.value(QString::fromLatin1("Domain/URL")).toString(); } if (ini.contains(QString::fromLatin1("Domain/Icon"))) { rethash[QString::fromLatin1("icon")] = ini.value(QString::fromLatin1("Domain/Icon")).toString(); } } return rethash; }