Update the admin/ directory.
[kdbg.git] / kdbg / programconfig.cpp
blob28f837ac38a3fecb56be3e9eaa156eaaa92f4247
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #include "programconfig.h"
8 #include <kconfigbackend.h>
9 #include <qfile.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
14 struct ProgramConfig::MyBackend : KConfigINIBackEnd
16 MyBackend(KConfigBase* cfg, const QString& fn) :
17 KConfigINIBackEnd(cfg, fn, "", false)
18 { }
19 // need the following public
20 using KConfigINIBackEnd::parseSingleConfigFile;
23 ProgramConfig::ProgramConfig(const QString& fileName) :
24 m_fileName(fileName)
26 m_iniBackend = new MyBackend(this, fileName);
27 backEnd = m_iniBackend;
28 reparseConfiguration();
31 QStringList ProgramConfig::groupList() const
33 // unused
34 return QStringList();
37 QMap<QString, QString> ProgramConfig::entryMap(const QString&) const
39 // unused
40 return QMap<QString, QString>();
43 void ProgramConfig::reparseConfiguration()
45 m_entryMap.clear();
47 // add the "default group" marker to the map
48 KEntryKey groupKey("<default>", 0);
49 m_entryMap.insert(groupKey, KEntry());
51 QFile file(m_fileName);
52 bool readonly = true;
53 bool useit = true;
54 if (file.open(IO_ReadWrite)) { /* don't truncate! */
55 readonly = false;
56 // the file exists now
57 } else if (!file.open(IO_ReadOnly)) {
58 /* file does not exist and cannot be created: don't use it */
59 useit = false;
62 if (useit)
64 // Check ownership
65 // Important: This must be done using fstat on the opened file
66 // to avoid race conditions.
67 struct stat s;
68 memset(&s, 0, sizeof(s));
69 useit =
70 fstat(file.handle(), &s) == 0 &&
71 s.st_uid == getuid();
74 if (useit)
76 m_iniBackend->parseSingleConfigFile(file, 0, false, false);
78 else
81 * The program specific config file is not ours, so we do not trust
82 * it for the following reason: Should the debuggee be located in a
83 * world-writable directory somebody else may have created it where
84 * the entry DebuggerCmdStr contains a malicious command, or may
85 * have created a (possibly huge) file containing nonsense, which
86 * leads to a DoS.
90 // don't write the file if we don't own it
91 setReadOnly(readonly || !useit);
94 KEntryMap ProgramConfig::internalEntryMap(const QString& group) const
96 QCString group_utf = group.utf8();
97 KEntryMap tmpEntryMap;
99 // copy the whole group starting at the special group marker
100 KEntryKey key(group_utf, 0);
101 KEntryMapConstIterator it = m_entryMap.find(key);
102 for (; it != m_entryMap.end() && it.key().mGroup == group_utf; ++it)
104 tmpEntryMap.insert(it.key(), *it);
107 return tmpEntryMap;
110 KEntryMap ProgramConfig::internalEntryMap() const
112 return m_entryMap;
115 void ProgramConfig::putData(const KEntryKey& key, const KEntry& data, bool checkGroup)
117 if (checkGroup)
119 // make sure the special group marker is present
120 m_entryMap[KEntryKey(key.mGroup, 0)];
122 m_entryMap[key] = data;
125 KEntry ProgramConfig::lookupData(const KEntryKey& key) const
127 KEntryMapConstIterator it;
129 it = m_entryMap.find(key);
130 if (it != m_entryMap.end())
132 const KEntry& entry = *it;
133 if (entry.bDeleted)
134 return KEntry();
135 else
136 return entry;
138 else {
139 return KEntry();
143 bool ProgramConfig::internalHasGroup(const QCString&) const
145 // unused
146 return false;