23 #include <QtCore/QDir>
24 #include <QtCore/QRegExp>
28 #include <QCoreApplication>
40 #include <QMutexLocker>
41 #include <QtCore/QBool>
49 #define checkExactMatch(s, b) \
50 if (s.isEmpty()) b = true; \
51 else if (s[s.length()-1] == QLatin1Char('!')) \
52 { b = false; s.truncate(s.length()-1); } \
54 #define checkStartWildCard(s, b) \
55 if (s.isEmpty()) b = true; \
56 else if (s[0] == QLatin1Char('*')) \
57 { b = true; s = s.mid(1); } \
59 #define checkEqual(s, b) \
60 b = (s == QString::fromLatin1("="));
62 URLActionRule(
const QByteArray &act,
67 baseProt(bProt), baseHost(bHost), basePath(bPath),
68 destProt(dProt), destHost(dHost), destPath(dPath),
81 bool baseMatch(
const KUrl &url,
const QString &protClass)
const
85 if ( !baseProt.isEmpty() && !url.
protocol().startsWith(baseProt) &&
86 (protClass.isEmpty() || (protClass != baseProt)) )
92 (protClass.isEmpty() || (protClass != baseProt)) )
97 if (!baseHost.isEmpty() && !url.host().endsWith(baseHost))
102 if (url.host() != baseHost)
105 if (basePathWildCard)
107 if (!basePath.isEmpty() && !url.
path().startsWith(basePath))
112 if (url.
path() != basePath)
118 bool destMatch(
const KUrl &url,
const QString &protClass,
const KUrl &base,
const QString &baseClass)
const
123 (protClass.isEmpty() || baseClass.isEmpty() || protClass != baseClass) )
126 else if (destProtWildCard)
128 if ( !destProt.isEmpty() && !url.
protocol().startsWith(destProt) &&
129 (protClass.isEmpty() || (protClass != destProt)) )
134 if ( (url.
protocol() != destProt) &&
135 (protClass.isEmpty() || (protClass != destProt)) )
138 if (destHostWildCard)
140 if (!destHost.isEmpty() && !url.host().endsWith(destHost))
143 else if (destHostEqual)
145 if (url.host() != base.host())
150 if (url.host() != destHost)
153 if (destPathWildCard)
155 if (!destPath.isEmpty() && !url.
path().startsWith(destPath))
160 if (url.
path() != destPath)
173 bool baseProtWildCard : 1;
174 bool baseHostWildCard : 1;
175 bool basePathWildCard : 1;
176 bool destProtWildCard : 1;
177 bool destHostWildCard : 1;
178 bool destPathWildCard : 1;
179 bool destProtEqual : 1;
180 bool destHostEqual : 1;
184 class KAuthorizedPrivate {
187 : actionRestrictions( false ), blockEverything(false),mutex(QMutex::Recursive)
189 Q_ASSERT_X(QCoreApplication::instance(),
"KAuthorizedPrivate()",
"There has to be an existing QCoreApplication::instance() pointer");
193 Q_ASSERT_X(config,
"KAuthorizedPrivate()",
"There has to be an existing KGlobal::config() pointer");
195 blockEverything=
true;
201 ~KAuthorizedPrivate()
205 bool actionRestrictions : 1;
206 bool blockEverything : 1;
211 Q_GLOBAL_STATIC(KAuthorizedPrivate,authPrivate)
212 #define MY_D KAuthorizedPrivate *d=authPrivate();
218 if (d->blockEverything)
return false;
220 if (!d->actionRestrictions)
224 return cg.
readEntry(genericAction,
true);
230 if (d->blockEverything)
return false;
231 if (!d->actionRestrictions || action.isEmpty())
234 return authorize(QLatin1String(
"action/") + action);
249 for(QStringList::ConstIterator it = menuIds.begin();
250 it != menuIds.end(); ++it)
263 d->urlActionRestrictions.clear();
264 d->urlActionRestrictions.append(
265 URLActionRule(
"open", Any, Any, Any, Any, Any, Any,
true));
266 d->urlActionRestrictions.append(
267 URLActionRule(
"list", Any, Any, Any, Any, Any, Any,
true));
273 d->urlActionRestrictions.append(
274 URLActionRule(
"link", Any, Any, Any, QLatin1String(
":internet"), Any, Any,
true));
275 d->urlActionRestrictions.append(
276 URLActionRule(
"redirect", Any, Any, Any, QLatin1String(
":internet"), Any, Any,
true));
280 d->urlActionRestrictions.append(
281 URLActionRule(
"redirect", Any, Any, Any, QLatin1String(
"file"), Any, Any,
true));
282 d->urlActionRestrictions.append(
283 URLActionRule(
"redirect", QLatin1String(
":internet"), Any, Any, QLatin1String(
"file"), Any, Any,
false));
286 d->urlActionRestrictions.append(
287 URLActionRule(
"redirect", QLatin1String(
":local"), Any, Any, Any, Any, Any,
true));
290 d->urlActionRestrictions.append(
291 URLActionRule(
"redirect", Any, Any, Any, QLatin1String(
"about"), Any, Any,
true));
294 d->urlActionRestrictions.append(
295 URLActionRule(
"redirect", Any, Any, Any, QLatin1String(
"mailto"), Any, Any,
true));
298 d->urlActionRestrictions.append(
299 URLActionRule(
"redirect", Any, Any, Any, QLatin1String(
"="), Any, Any,
true));
301 d->urlActionRestrictions.append(
302 URLActionRule(
"redirect", QLatin1String(
"about"), Any, Any, Any, Any, Any,
true));
306 int count = cg.
readEntry(
"rule_count", 0);
307 QString keyFormat = QString::fromLatin1(
"rule_%1");
308 for(
int i = 1; i <= count; i++)
310 QString key = keyFormat.arg(i);
312 if (rule.count() != 8)
314 const QByteArray action = rule[0].toLatin1();
321 bool bEnabled = (rule[7].toLower() == QLatin1String(
"true"));
323 if (refPath.startsWith(QLatin1String(
"$HOME")))
324 refPath.replace(0, 5, QDir::homePath());
325 else if (refPath.startsWith(QLatin1Char(
'~')))
326 refPath.replace(0, 1, QDir::homePath());
327 if (urlPath.startsWith(QLatin1String(
"$HOME")))
328 urlPath.replace(0, 5, QDir::homePath());
329 else if (urlPath.startsWith(QLatin1Char(
'~')))
330 urlPath.replace(0, 1, QDir::homePath());
332 if (refPath.startsWith(QLatin1String(
"$TMP")))
334 if (urlPath.startsWith(QLatin1String(
"$TMP")))
337 d->urlActionRestrictions.append(
338 URLActionRule( action, refProt, refHost, refPath, urlProt, urlHost, urlPath, bEnabled));
345 QMutexLocker locker((&d->mutex));
349 d->urlActionRestrictions.append( URLActionRule
357 QMutexLocker locker(&(d->mutex));
358 if (d->blockEverything)
return false;
360 if (_destURL.isEmpty())
364 if (d->urlActionRestrictions.isEmpty())
367 KUrl baseURL(_baseURL);
370 KUrl destURL(_destURL);
371 destURL.setPath(QDir::cleanPath(destURL.path()));
374 foreach(
const URLActionRule &rule, d->urlActionRestrictions) {
375 if ((result != rule.permission) &&
376 (action == QLatin1String(rule.action)) &&
377 rule.baseMatch(baseURL, baseClass) &&
378 rule.destMatch(destURL, destClass, baseURL, baseClass))
380 result = rule.permission;
QString saveLocation(const char *type, const QString &suffix=QString(), bool create=true) const
Finds a location to save files into for the given type in the user's home directory.
#define checkStartWildCard(s, b)
strips a trailing '/', except when the path is already just "/".
KStandardDirs * dirs()
Returns the application standard dirs object.
bool hasGroup(const QString &group) const
Returns true if the specified group is known about.
Represents and parses a URL.
KSharedConfigPtr config()
Returns the general config object.
void setPath(const QString &path)
QStringList authorizeControlModules(const QStringList &menuIds)
Returns which control modules from a given list are authorized for access.
bool authorizeUrlAction(const QString &action, const KUrl &baseUrl, const KUrl &destUrl)
Returns whether a certain URL related action is authorized.
QString protocol() const
Returns the protocol for the URL (i.e., file, http, etc.), lowercased.
static QString protocolClass(const QString &protocol)
Returns the protocol class for the specified protocol.
QString path(AdjustPathOption trailing=LeaveTrailingSlash) const
static void initUrlActionRestrictions()
void allowUrlAction(const QString &action, const KUrl &baseUrl, const KUrl &_destUrl)
Allow a certain URL action.
A class for one specific group in a KConfig object.
#define checkExactMatch(s, b)
bool authorize(const QString &genericAction)
Returns whether a certain action is authorized.
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
bool authorizeControlModule(const QString &menuId)
Returns whether access to a certain control module is authorized.
bool authorizeKAction(const QString &action)
Returns whether a certain KAction is authorized.