Add the ability to force read-only configuration file access in a TDE application

Force tde-config to use read-only access
This closes Bug 293
pull/16/head
Timothy Pearson 13 years ago
parent fffcd810f8
commit d3a9d56143

@ -247,17 +247,22 @@ void KConfigBackEnd::changeFileName(const TQString &_fileName,
mfileName = _fileName; mfileName = _fileName;
resType = _resType; resType = _resType;
useKDEGlobals = _useKDEGlobals; useKDEGlobals = _useKDEGlobals;
if (mfileName.isEmpty()) if (mfileName.isEmpty()) {
mLocalFileName = TQString::null; mLocalFileName = TQString::null;
else if (!TQDir::isRelativePath(mfileName)) }
else if (!TQDir::isRelativePath(mfileName)) {
mLocalFileName = mfileName; mLocalFileName = mfileName;
else }
mLocalFileName = KGlobal::dirs()->saveLocation(resType) + mfileName; else {
mLocalFileName = KGlobal::dirs()->saveLocation(resType, TQString(), false) + mfileName;
}
if (useKDEGlobals) if (useKDEGlobals) {
mGlobalFileName = KGlobal::dirs()->saveLocation("config", TQString(), false) + TQString::fromLatin1("kdeglobals"); mGlobalFileName = KGlobal::dirs()->saveLocation("config", TQString(), false) + TQString::fromLatin1("kdeglobals");
else }
else {
mGlobalFileName = TQString::null; mGlobalFileName = TQString::null;
}
d->localLastModified = TQDateTime(); d->localLastModified = TQDateTime();
d->localLastSize = 0; d->localLastSize = 0;

@ -1139,16 +1139,18 @@ static TQString translatePath( TQString path )
// return original path, if it refers to another type of URL (e.g. http:/), or // return original path, if it refers to another type of URL (e.g. http:/), or
// if the path is already relative to another directory // if the path is already relative to another directory
if (!startsWithFile && path[0] != '/' || if (((!startsWithFile) && (path[0] != '/')) || (startsWithFile && (path[5] != '/'))) {
startsWithFile && path[5] != '/')
return path; return path;
}
if (startsWithFile) if (startsWithFile) {
path.remove(0,5); // strip leading "file:/" off the string path.remove(0,5); // strip leading "file:/" off the string
}
// keep only one single '/' at the beginning - needed for cleanHomeDirPath() // keep only one single '/' at the beginning - needed for cleanHomeDirPath()
while (path[0] == '/' && path[1] == '/') while (path[0] == '/' && path[1] == '/') {
path.remove(0,1); path.remove(0,1);
}
// we can not use KGlobal::dirs()->relativeLocation("home", path) here, // we can not use KGlobal::dirs()->relativeLocation("home", path) here,
// since it would not recognize paths without a trailing '/'. // since it would not recognize paths without a trailing '/'.

@ -72,7 +72,7 @@ KInstance::KInstance( const TQCString& name)
_config (0L), _config (0L),
_iconLoader (0L), _iconLoader (0L),
_hardwaredevices (0L), _hardwaredevices (0L),
_name( name ), _aboutData( new KAboutData( name, "", 0 ) ) _name( name ), _aboutData( new KAboutData( name, "", 0 ) ), m_configReadOnly(false)
{ {
DEBUG_ADD DEBUG_ADD
Q_ASSERT(!name.isEmpty()); Q_ASSERT(!name.isEmpty());
@ -91,7 +91,7 @@ KInstance::KInstance( const KAboutData * aboutData )
_config (0L), _config (0L),
_iconLoader (0L), _iconLoader (0L),
_hardwaredevices (0L), _hardwaredevices (0L),
_name( aboutData->appName() ), _aboutData( aboutData ) _name( aboutData->appName() ), _aboutData( aboutData ), m_configReadOnly(false)
{ {
DEBUG_ADD DEBUG_ADD
Q_ASSERT(!_name.isEmpty()); Q_ASSERT(!_name.isEmpty());
@ -111,7 +111,7 @@ KInstance::KInstance( KInstance* src )
_config ( src->_config ), _config ( src->_config ),
_iconLoader ( src->_iconLoader ), _iconLoader ( src->_iconLoader ),
_hardwaredevices ( src->_hardwaredevices ), _hardwaredevices ( src->_hardwaredevices ),
_name( src->_name ), _aboutData( src->_aboutData ) _name( src->_name ), _aboutData( src->_aboutData ), m_configReadOnly(false)
{ {
DEBUG_ADD DEBUG_ADD
Q_ASSERT(!_name.isEmpty()); Q_ASSERT(!_name.isEmpty());
@ -182,6 +182,11 @@ KStandardDirs *KInstance::dirs() const
extern bool kde_kiosk_exception; extern bool kde_kiosk_exception;
extern bool kde_kiosk_admin; extern bool kde_kiosk_admin;
void KInstance::setConfigReadOnly(bool ro)
{
m_configReadOnly = ro;
}
KConfig *KInstance::config() const KConfig *KInstance::config() const
{ {
DEBUG_CHECK_ALIVE DEBUG_CHECK_ALIVE
@ -206,10 +211,12 @@ KConfig *KInstance::config() const
if ( d->sharedConfig == 0 ) if ( d->sharedConfig == 0 )
{ {
if ( !_name.isEmpty() ) if ( !_name.isEmpty() ) {
d->sharedConfig = KSharedConfig::openConfig( _name + "rc"); d->sharedConfig = KSharedConfig::openConfig( _name + "rc", m_configReadOnly );
else }
else {
d->sharedConfig = KSharedConfig::openConfig( TQString::null ); d->sharedConfig = KSharedConfig::openConfig( TQString::null );
}
} }
// Check if we are excempt from kiosk restrictions // Check if we are excempt from kiosk restrictions

@ -95,6 +95,14 @@ class TDECORE_EXPORT KInstance
*/ */
KSharedConfig *sharedConfig() const; KSharedConfig *sharedConfig() const;
/**
* Set a read-only flag on the configuration files
* This must be called before config() or dirs() to have any effect
* Defaults to FALSE
* @param ro read only if TRUE
*/
void setConfigReadOnly(bool ro);
/** /**
* Returns an iconloader object. * Returns an iconloader object.
* @return the iconloader object. * @return the iconloader object.
@ -162,6 +170,7 @@ protected:
virtual void virtual_hook( int id, void* data ); virtual void virtual_hook( int id, void* data );
private: private:
KInstancePrivate *d; KInstancePrivate *d;
bool m_configReadOnly;
}; };
#endif #endif

@ -48,7 +48,6 @@
#include "kstandarddirs.h" #include "kstandarddirs.h"
#include "kconfig.h" #include "kconfig.h"
#include "kdebug.h"
#include "kinstance.h" #include "kinstance.h"
#include "kshell.h" #include "kshell.h"
#include "ksimpleconfig.h" #include "ksimpleconfig.h"
@ -415,7 +414,7 @@ TQString KStandardDirs::findResourceDir( const char *type,
{ {
#ifndef NDEBUG #ifndef NDEBUG
if (filename.isEmpty()) { if (filename.isEmpty()) {
kdWarning() << "filename for type " << type << " in KStandardDirs::findResourceDir is not supposed to be empty!!" << endl; printf("filename for type %s in KStandardDirs::findResourceDir is not supposed to be empty!!", type);
return TQString::null; return TQString::null;
} }
#endif #endif
@ -431,7 +430,7 @@ TQString KStandardDirs::findResourceDir( const char *type,
#ifdef Q_WS_WIN //this ensures we're using installed .la files #ifdef Q_WS_WIN //this ensures we're using installed .la files
if ((*it).isEmpty() && filename.right(3)==".la") { if ((*it).isEmpty() && filename.right(3)==".la") {
#ifndef NDEBUG #ifndef NDEBUG
kdDebug() << "KStandardDirs::findResourceDir() found .la in cwd: skipping. (fname=" << filename << ")" << endl; printf("KStandardDirs::findResourceDir() found .la in cwd: skipping. (fname=%s)\n", filename.ascii());
#endif #endif
continue; continue;
} }
@ -442,7 +441,7 @@ TQString KStandardDirs::findResourceDir( const char *type,
#ifndef NDEBUG #ifndef NDEBUG
if(false && strcmp(type, "locale")) if(false && strcmp(type, "locale"))
kdDebug() << "KStdDirs::findResDir(): can't find \"" << filename << "\" in type \"" << type << "\"." << endl; printf("KStdDirs::findResDir(): can't find \"%s\" in type \"%s\".\n", filename.ascii(), type);
#endif #endif
return TQString::null; return TQString::null;
@ -451,13 +450,17 @@ TQString KStandardDirs::findResourceDir( const char *type,
bool KStandardDirs::exists(const TQString &fullPath) bool KStandardDirs::exists(const TQString &fullPath)
{ {
KDE_struct_stat buff; KDE_struct_stat buff;
if (access(TQFile::encodeName(fullPath), R_OK) == 0 && KDE_stat( TQFile::encodeName(fullPath), &buff ) == 0) if ((access(TQFile::encodeName(fullPath), R_OK) == 0) && (KDE_stat( TQFile::encodeName(fullPath), &buff ) == 0)) {
if (fullPath.at(fullPath.length() - 1) != QChar('/')) { if (fullPath.at(fullPath.length() - 1) != QChar('/')) {
if (S_ISREG( buff.st_mode )) if (S_ISREG( buff.st_mode ))
return true; return true;
} else }
if (S_ISDIR( buff.st_mode )) else {
if (S_ISDIR( buff.st_mode )) {
return true; return true;
}
}
}
return false; return false;
} }
@ -500,7 +503,7 @@ static void lookupDirectory(const TQString& path, const TQString &relPart,
TQString pathfn = path + fn; TQString pathfn = path + fn;
if ( KDE_stat( TQFile::encodeName(pathfn), &buff ) != 0 ) { if ( KDE_stat( TQFile::encodeName(pathfn), &buff ) != 0 ) {
kdDebug() << "Error stat'ing " << pathfn << " : " << perror << endl; printf("Error stat'ing %s : %d\n", pathfn.ascii(), errno);
continue; // Couldn't stat (e.g. no read permissions) continue; // Couldn't stat (e.g. no read permissions)
} }
if ( recursive ) { if ( recursive ) {
@ -599,7 +602,7 @@ static void lookupPrefix(const TQString& prefix, const TQString& relpath,
TQString rfn = relPart+fn; TQString rfn = relPart+fn;
fn = prefix + fn; fn = prefix + fn;
if ( KDE_stat( TQFile::encodeName(fn), &buff ) != 0 ) { if ( KDE_stat( TQFile::encodeName(fn), &buff ) != 0 ) {
kdDebug() << "Error statting " << fn << " : " << perror << endl; printf("Error statting %s : %d\n", fn.ascii(), errno);
continue; // Couldn't stat (e.g. no permissions) continue; // Couldn't stat (e.g. no permissions)
} }
if ( S_ISDIR( buff.st_mode )) if ( S_ISDIR( buff.st_mode ))
@ -767,17 +770,23 @@ void KStandardDirs::createSpecialResource(const char *type)
srv = findExe(TQString::fromLatin1("lnusertemp")); srv = findExe(TQString::fromLatin1("lnusertemp"));
if (!srv.isEmpty()) if (!srv.isEmpty())
{ {
system(TQFile::encodeName(srv)+" "+type); if (system(TQFile::encodeName(srv)+" "+type) < 0 ) {
result = readlink(TQFile::encodeName(dir).data(), link, 1023); result = readlink(TQFile::encodeName(dir).data(), link, 1023);
}
else {
result = -1;
}
} }
} }
if (result > 0) if (result > 0)
{ {
link[result] = 0; link[result] = 0;
if (link[0] == TQChar('/').latin1()) if (link[0] == TQChar('/').latin1()) {
dir = TQFile::decodeName(link); dir = TQFile::decodeName(link);
else }
else {
dir = TQDir::cleanDirPath(dir+TQFile::decodeName(link)); dir = TQDir::cleanDirPath(dir+TQFile::decodeName(link));
}
} }
#endif #endif
addResourceDir(type, dir+QChar('/')); addResourceDir(type, dir+QChar('/'));
@ -1130,7 +1139,7 @@ TQString KStandardDirs::saveLocation(const char *type,
if (KDE_stat(TQFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) { if (KDE_stat(TQFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) {
if(!create) { if(!create) {
#ifndef NDEBUG #ifndef NDEBUG
kdDebug() << TQString("save location %1 doesn't exist").arg(fullPath) << endl; printf("%s", TQString("save location %1 doesn't exist\n").arg(fullPath).ascii());
#endif #endif
return fullPath; return fullPath;
} }
@ -1139,8 +1148,9 @@ TQString KStandardDirs::saveLocation(const char *type,
} }
dircache.remove(type); dircache.remove(type);
} }
if (!fullPath.endsWith("/")) if (!fullPath.endsWith("/")) {
fullPath += "/"; fullPath += "/";
}
return fullPath; return fullPath;
} }
@ -1261,8 +1271,9 @@ TQString KStandardDirs::kfsstnd_defaultprefix()
#else //UNIX #else //UNIX
s->defaultprefix = TDEDIR; s->defaultprefix = TDEDIR;
#endif #endif
if (s->defaultprefix.isEmpty()) if (s->defaultprefix.isEmpty()) {
kdWarning() << "KStandardDirs::kfsstnd_defaultprefix(): default KDE prefix not found!" << endl; printf("KStandardDirs::kfsstnd_defaultprefix(): default TDE prefix not found!\n");
}
return s->defaultprefix; return s->defaultprefix;
} }
@ -1278,8 +1289,9 @@ TQString KStandardDirs::kfsstnd_defaultbindir()
if (s->defaultbindir.isEmpty()) if (s->defaultbindir.isEmpty())
s->defaultbindir = kfsstnd_defaultprefix() + TQString::fromLatin1("/bin"); s->defaultbindir = kfsstnd_defaultprefix() + TQString::fromLatin1("/bin");
#endif #endif
if (s->defaultbindir.isEmpty()) if (s->defaultbindir.isEmpty()) {
kdWarning() << "KStandardDirs::kfsstnd_defaultbindir(): default binary KDE dir not found!" << endl; printf("KStandardDirs::kfsstnd_defaultbindir(): default binary TDE dir not found!\n");
}
return s->defaultbindir; return s->defaultbindir;
} }

@ -120,6 +120,7 @@ int main(int argc, char **argv)
KCmdLineArgs::addCmdLineOptions( options ); // Add my own options. KCmdLineArgs::addCmdLineOptions( options ); // Add my own options.
KInstance a("tde-config"); KInstance a("tde-config");
a.setConfigReadOnly(TRUE);
(void)KGlobal::dirs(); // trigger the creation (void)KGlobal::dirs(); // trigger the creation
(void)KGlobal::config(); (void)KGlobal::config();

Loading…
Cancel
Save