2 suPHP - (c)2002-2008 Sebastian Marsching <sebastian@marsching.com>
4 This file is part of suPHP.
6 suPHP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 suPHP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with suPHP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "IniFile.hpp"
27 #include "Configuration.hpp"
29 using namespace suPHP
;
31 bool suPHP::Configuration::strToBool(const std::string
& bstr
) const
32 throw (ParsingException
) {
33 std::string str
= bstr
;
34 // Convert upper characters to lower characters
35 for (int i
=0; i
<str
.size(); i
++) {
36 if (str
[i
] >= 65 && str
[i
] <= 90)
40 if (str
== std::string("true")) {
42 } else if (str
== std::string("yes")) {
44 } else if (str
== std::string("on")) {
46 } else if (str
== std::string("enabled")) {
48 } else if (str
== std::string("1")) {
50 } else if (str
== std::string("false")) {
52 } else if (str
== std::string("no")) {
54 } else if (str
== std::string("off")) {
56 } else if (str
== std::string("disabled")) {
58 } else if (str
== std::string("0")) {
61 throw ParsingException("\"" + str
+ "\" is not a valid boolean value",
68 LogLevel
suPHP::Configuration::strToLogLevel(const std::string
& str
) const
69 throw (ParsingException
) {
72 else if (str
== "error")
73 return LOGLEVEL_ERROR
;
74 else if (str
== "warn")
76 else if (str
== "info")
79 throw ParsingException("\"" + str
+ "\" is not a valid log level",
83 suPHP::Configuration::Configuration() {
84 this->logfile
= "/var/log/suphp.log";
85 #ifdef OPT_APACHE_USER
86 this->webserver_user
= OPT_APACHE_USER
;
88 this->webserver_user
= "wwwrun";
90 this->docroots
.push_back("/");
91 this->allow_file_group_writeable
= false;
92 this->allow_directory_group_writeable
= false;
93 this->allow_file_others_writeable
= false;
94 this->allow_directory_others_writeable
= false;
95 #ifdef OPT_DISABLE_CHECKPATH
96 this->check_vhost_docroot
= false;
98 this->check_vhost_docroot
= true;
100 this->errors_to_browser
= false;
101 this->env_path
= "/bin:/usr/bin";
102 this->loglevel
= LOGLEVEL_INFO
;
104 this->min_uid
= OPT_MIN_UID
;
109 this->min_gid
= OPT_MIN_GID
;
114 this->chroot_path
= "";
117 void suPHP::Configuration::readFromFile(File
& file
)
118 throw (IOException
, ParsingException
) {
121 if (ini
.hasSection("global")) {
122 const IniSection
& sect
= ini
.getSection("global");
123 const std::vector
<std::string
> keys
= sect
.getKeys();
124 std::vector
<std::string
>::const_iterator i
;
125 for (i
= keys
.begin(); i
< keys
.end(); i
++) {
126 std::string key
= *i
;
127 std::string value
= sect
.getValue(key
);
129 if (key
== "logfile")
130 this->logfile
= value
;
131 else if (key
== "webserver_user")
132 this->webserver_user
= value
;
133 else if (key
== "docroot") {
134 this->docroots
= sect
.getValues(key
);
135 } else if (key
== "allow_file_group_writeable")
136 this->allow_file_group_writeable
= this->strToBool(value
);
137 else if (key
== "allow_directory_group_writeable")
138 this->allow_directory_group_writeable
= this->strToBool(value
);
139 else if (key
== "allow_file_others_writeable")
140 this->allow_file_others_writeable
= this->strToBool(value
);
141 else if (key
== "allow_directory_others_writeable")
142 this->allow_directory_others_writeable
=
143 this->strToBool(value
);
144 else if (key
== "check_vhost_docroot")
145 this->check_vhost_docroot
= this->strToBool(value
);
146 else if (key
== "errors_to_browser")
147 this->errors_to_browser
= this->strToBool(value
);
148 else if (key
== "env_path")
149 this->env_path
= value
;
150 else if (key
== "loglevel")
151 this->loglevel
= this->strToLogLevel(value
);
152 else if (key
== "min_uid")
153 this->min_uid
= Util::strToInt(value
);
154 else if (key
== "min_gid")
155 this->min_gid
= Util::strToInt(value
);
156 else if (key
== "umask")
157 this->umask
= Util::octalStrToInt(value
);
158 else if (key
== "chroot")
159 this->chroot_path
= value
;
161 throw ParsingException("Unknown option \"" + key
+
162 "\" in section [global]",
167 // Get handlers / interpreters
168 if (ini
.hasSection("handlers")) {
169 IniSection sect
= ini
.getSection("handlers");
170 const std::vector
<std::string
> keys
= sect
.getKeys();
171 std::vector
<std::string
>::const_iterator i
;
172 for (i
= keys
.begin(); i
< keys
.end(); i
++) {
173 std::string key
= *i
;
174 std::string value
= sect
.getValue(key
);
175 std::pair
<std::string
, std::string
> p
;
178 this->handlers
.insert(p
);
184 std::string
suPHP::Configuration::getLogfile() const {
185 return this->logfile
;
188 LogLevel
suPHP::Configuration::getLogLevel() const {
189 return this->loglevel
;
192 std::string
suPHP::Configuration::getWebserverUser() const {
193 return this->webserver_user
;
196 const std::vector
<std::string
>& suPHP::Configuration::getDocroots() const {
197 return this->docroots
;
200 bool suPHP::Configuration::getCheckVHostDocroot() const {
201 return this->check_vhost_docroot
;
204 bool suPHP::Configuration::getAllowFileGroupWriteable() const {
205 return this->allow_file_group_writeable
;
208 bool suPHP::Configuration::getAllowDirectoryGroupWriteable() const {
209 return this->allow_directory_group_writeable
;
212 bool suPHP::Configuration::getAllowFileOthersWriteable() const {
213 return this->allow_file_others_writeable
;
216 bool suPHP::Configuration::getAllowDirectoryOthersWriteable() const {
217 return this->allow_directory_others_writeable
;
220 bool suPHP::Configuration::getErrorsToBrowser() const {
221 return this->errors_to_browser
;
224 std::string
suPHP::Configuration::getEnvPath() const {
225 return this->env_path
;
228 std::string
suPHP::Configuration::getInterpreter(std::string handler
) const
229 throw (KeyNotFoundException
) {
230 if (this->handlers
.find(handler
) != this->handlers
.end()) {
231 return this->handlers
.find(handler
) -> second
;
233 throw KeyNotFoundException("Handler \"" + handler
+ "\" not found",
238 int suPHP::Configuration::getMinUid() const {
239 return this->min_uid
;
242 int suPHP::Configuration::getMinGid() const {
243 return this->min_gid
;
246 int suPHP::Configuration::getUmask() const {
250 std::string
suPHP::Configuration::getChrootPath() const {
251 return this->chroot_path
;