File: Configuration.cpp

package info (click to toggle)
suphp 0.6.2-1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 2,304 kB
  • ctags: 439
  • sloc: sh: 20,746; cpp: 2,459; ansic: 1,114; makefile: 121
file content (252 lines) | stat: -rw-r--r-- 7,296 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
    suPHP - (c)2002-2005 Sebastian Marsching <sebastian@marsching.com>

    This file is part of suPHP.

    suPHP is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    suPHP 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 General Public License
    along with suPHP; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <string>
#include <vector>

#include "IniFile.hpp"
#include "Util.hpp"

#include "Configuration.hpp"

using namespace suPHP;

bool suPHP::Configuration::strToBool(const std::string& bstr) const
    throw (ParsingException) {
    std::string str = bstr;
    // Convert upper characters to lower characters
    for (int i=0; i<str.size(); i++) {
	if (str[i] >= 65 && str[i] <= 90)
	    str[i] += 32;
    }

    if (str == std::string("true")) {
	return true;
    } else if (str == std::string("yes")) {
	return true;
    } else if (str == std::string("on")) {
	return true;
    } else if (str == std::string("enabled")) {
	return true;
    } else if (str == std::string("1")) {
	return true;
    } else if (str == std::string("false")) {
	return false;
    } else if (str == std::string("no")) {
	return false;
    } else if (str == std::string("off")) {
	return false;
    } else if (str == std::string("disabled")) {
	return false;
    } else if (str == std::string("0")) {
	return false;
    } else {
	throw ParsingException("\"" + str + "\" is not a valid boolean value",
			       __FILE__, __LINE__);
    }
	
}


LogLevel suPHP::Configuration::strToLogLevel(const std::string& str) const
    throw (ParsingException) {
    if (str == "none")
	return LOGLEVEL_NONE;
    else if (str == "error")
	return LOGLEVEL_ERROR;
    else if (str == "warn")
	return LOGLEVEL_WARN;
    else if (str == "info")
	return LOGLEVEL_INFO;
    else
	throw ParsingException("\"" + str + "\" is not a valid log level",
			       __FILE__, __LINE__);
}

suPHP::Configuration::Configuration() {
    this->logfile = "/var/log/suphp.log";
#ifdef OPT_APACHE_USER
    this->webserver_user = OPT_APACHE_USER;
#else
    this->webserver_user = "wwwrun";
#endif
    this->docroot = "/";
    this->allow_file_group_writeable = false;
    this->allow_directory_group_writeable = false;
    this->allow_file_others_writeable = false;
    this->allow_directory_others_writeable = false;
#ifdef OPT_DISABLE_CHECKPATH
    this->check_vhost_docroot = false;
#else
    this->check_vhost_docroot = true;
#endif
    this->errors_to_browser = false;
    this->env_path = "/bin:/usr/bin";
    this->loglevel = LOGLEVEL_INFO;
#ifdef OPT_MIN_UID
    this->min_uid = OPT_MIN_UID;
#else
    this->min_uid = 1;
#endif
#ifdef OPT_MIN_GID
    this->min_gid = OPT_MIN_GID;
#else
    this->min_gid = 1;
#endif
    this->umask = 0077;
    this->chroot_path = "";
}

void suPHP::Configuration::readFromFile(File& file) 
    throw (IOException, ParsingException) {
    IniFile ini;
    ini.parse(file);
    if (ini.hasSection("global")) {
	IniSection& sect = ini.getSection("global");
	std::vector<std::string> keys = sect.getKeys();
	std::vector<std::string>::iterator i;
	for (i = keys.begin(); i < keys.end(); i++) {
	    std::string key = *i;
	    std::string value = sect.getValue(key);

	    if (key == "logfile")
		this->logfile = value;
	    else if (key == "webserver_user")
		this->webserver_user = value;
	    else if (key == "docroot")
		this->docroot = value;
	    else if (key == "allow_file_group_writeable")
		this->allow_file_group_writeable = this->strToBool(value);
	    else if (key == "allow_directory_group_writeable")
		this->allow_directory_group_writeable = this->strToBool(value);
	    else if (key == "allow_file_others_writeable")
		this->allow_file_others_writeable = this->strToBool(value);
	    else if (key == "allow_directory_others_writeable")
		this->allow_directory_others_writeable = 
		    this->strToBool(value);
	    else if (key == "check_vhost_docroot")
		this->check_vhost_docroot = this->strToBool(value);
	    else if (key == "errors_to_browser")
		this->errors_to_browser = this->strToBool(value);
	    else if (key == "env_path")
		this->env_path = value;
	    else if (key == "loglevel")
		this->loglevel = this->strToLogLevel(value);
	    else if (key == "min_uid")
		this->min_uid = Util::strToInt(value);
	    else if (key == "min_gid")
		this->min_gid = Util::strToInt(value);
	    else if (key == "umask")
		this->umask = Util::octalStrToInt(value);
	    else if (key == "chroot")
		this->chroot_path = value;
	    else 
		throw ParsingException("Unknown option \"" + key + 
				       "\" in section [global]", 
				       __FILE__, __LINE__);
	}
    }
    
    // Get handlers / interpreters
    if (ini.hasSection("handlers")) {
	IniSection sect = ini.getSection("handlers");
	std::vector<std::string> keys = sect.getKeys();
	std::vector<std::string>::iterator i;
	for (i = keys.begin(); i < keys.end(); i++) {
	    std::string key = *i;
	    std::string value = sect.getValue(key);
	    std::pair<std::string, std::string> p;
	    p.first = key;
	    p.second = value;
	    this->handlers.insert(p);
	}
    }
    
}

std::string suPHP::Configuration::getLogfile() const {
    return this->logfile;
}

LogLevel suPHP::Configuration::getLogLevel() const {
    return this->loglevel;
}

std::string suPHP::Configuration::getWebserverUser() const {
    return this->webserver_user;
}

std::string suPHP::Configuration::getDocroot() const {
    return this->docroot;
}

bool suPHP::Configuration::getCheckVHostDocroot() const {
    return this->check_vhost_docroot;
}

bool suPHP::Configuration::getAllowFileGroupWriteable() const {
    return this->allow_file_group_writeable;
}

bool suPHP::Configuration::getAllowDirectoryGroupWriteable() const {
    return this->allow_directory_group_writeable;
}

bool suPHP::Configuration::getAllowFileOthersWriteable() const {
    return this->allow_file_others_writeable;
}

bool suPHP::Configuration::getAllowDirectoryOthersWriteable() const {
    return this->allow_directory_others_writeable;
}

bool suPHP::Configuration::getErrorsToBrowser() const {
    return this->errors_to_browser;
}

std::string suPHP::Configuration::getEnvPath() const {
    return this->env_path;
}

std::string suPHP::Configuration::getInterpreter(std::string handler) const 
    throw (KeyNotFoundException) {
    if (this->handlers.find(handler) != this->handlers.end()) {
	return this->handlers.find(handler) -> second;
    } else {
	throw KeyNotFoundException("Handler \"" + handler + "\" not found",
				   __FILE__, __LINE__);
    }
}

int suPHP::Configuration::getMinUid() const {
    return this->min_uid;
}

int suPHP::Configuration::getMinGid() const {
    return this->min_gid;
}

int suPHP::Configuration::getUmask() const {
    return this->umask;
}

std::string suPHP::Configuration::getChrootPath() const {
    return this->chroot_path;
}