-
Notifications
You must be signed in to change notification settings - Fork 548
/
ServerInstanceDir.h
347 lines (302 loc) · 10.3 KB
/
ServerInstanceDir.h
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Phusion Passenger - http://www.modrails.com/
* Copyright (c) 2010 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _PASSENGER_SERVER_INSTANCE_DIR_H_
#define _PASSENGER_SERVER_INSTANCE_DIR_H_
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <oxt/backtrace.hpp>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <cstdlib>
#include <cstring>
#include <string>
#include "Exceptions.h"
#include "Utils.h"
#include "Utils/StrIntUtils.h"
namespace Passenger {
using namespace std;
using namespace boost;
class ServerInstanceDir: public noncopyable {
public:
// Don't forget to update lib/phusion_passenger/admin_tools/server_instance.rb too.
static const int DIR_STRUCTURE_MAJOR_VERSION = 1;
static const int DIR_STRUCTURE_MINOR_VERSION = 0;
static const int GENERATION_STRUCTURE_MAJOR_VERSION = 1;
static const int GENERATION_STRUCTURE_MINOR_VERSION = 0;
class Generation: public noncopyable {
private:
friend class ServerInstanceDir;
string path;
unsigned int number;
bool owner;
Generation(const string &serverInstanceDir, unsigned int number) {
path = serverInstanceDir + "/generation-" + toString(number);
this->number = number;
owner = false;
}
void create(bool userSwitching, const string &defaultUser,
const string &defaultGroup, uid_t webServerWorkerUid,
gid_t webServerWorkerGid)
{
TRACE_POINT();
bool runningAsRoot = geteuid() == 0;
struct passwd *defaultUserEntry;
struct group *defaultGroupEntry;
uid_t defaultUid;
gid_t defaultGid;
defaultUserEntry = getpwnam(defaultUser.c_str());
if (defaultUserEntry == NULL) {
throw NonExistentUserException("Default user '" + defaultUser +
"' does not exist.");
}
defaultUid = defaultUserEntry->pw_uid;
defaultGroupEntry = getgrnam(defaultGroup.c_str());
if (defaultGroupEntry == NULL) {
throw NonExistentGroupException("Default group '" + defaultGroup +
"' does not exist.");
}
defaultGid = defaultGroupEntry->gr_gid;
/* We set a very tight permission here: no read or write access for
* anybody except the owner. The individual files and subdirectories
* decide for themselves whether they're readable by anybody.
*/
makeDirTree(path, "u=rwxs,g=x,o=x");
/* Write structure version file. */
string structureVersionFile = path + "/structure_version.txt";
createFile(structureVersionFile,
toString(GENERATION_STRUCTURE_MAJOR_VERSION) + "." +
toString(GENERATION_STRUCTURE_MINOR_VERSION),
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
/* We want the upload buffer directory to be only writable by the web
* server's worker processs. Other users may not have any access to this
* directory.
*/
if (runningAsRoot) {
makeDirTree(path + "/buffered_uploads", "u=rwxs,g=,o=",
webServerWorkerUid, webServerWorkerGid);
} else {
makeDirTree(path + "/buffered_uploads", "u=rwxs,g=,o=");
}
/* The web server must be able to directly connect to a backend. */
if (runningAsRoot) {
if (userSwitching) {
/* Each backend process may be running as a different user,
* so the backends subdirectory must be world-writable.
* However we don't want everybody to be able to know the
* sockets' filenames, so the directory is not readable.
*/
makeDirTree(path + "/backends", "u=rwxs,g=wx,o=wx");
} else {
/* All backend processes are running as defaultUser/defaultGroup,
* so make defaultUser/defaultGroup the owner and group of the
* subdirecory.
*
* The directory is not readable as a security precaution:
* nobody should be able to know the sockets' filenames without
* having access to the application pool.
*/
makeDirTree(path + "/backends", "u=rwxs,g=x,o=x", defaultUid, defaultGid);
}
} else {
/* All backend processes are running as the same user as the web server,
* so only allow access for this user.
*/
makeDirTree(path + "/backends", "u=rwxs,g=,o=");
}
/* The helper server (containing the application pool) must be able to access
* the spawn server's socket.
*/
if (runningAsRoot) {
if (userSwitching) {
/* Both the helper server and the spawn server are
* running as root.
*/
makeDirTree(path + "/spawn-server", "u=rwxs,g=,o=");
} else {
/* Both the helper server and the spawn server are
* running as defaultUser/defaultGroup.
*/
makeDirTree(path + "/spawn-server", "u=rwxs,g=,o=",
defaultUid, defaultGid);
}
} else {
makeDirTree(path + "/spawn-server", "u=rwxs,g=,o=");
}
owner = true;
}
public:
~Generation() {
if (owner) {
removeDirTree(path);
}
}
unsigned int getNumber() const {
return number;
}
string getPath() const {
return path;
}
void detach() {
owner = false;
}
};
typedef shared_ptr<Generation> GenerationPtr;
private:
string path;
bool owner;
friend class Generation;
void initialize(const string &path, bool owner) {
TRACE_POINT();
this->path = path;
this->owner = owner;
/* Create the server instance directory. We only need to write to this
* directory for these reasons:
* 1. Initial population of structure files (structure_version.txt, instance.pid).
* 2. Creating/removing a generation directory.
* 3. Removing the entire server instance directory (after all
* generations are removed).
*
* 1 and 2 are done by the helper server during initialization and before lowering
* privilege. 3 is done during helper server shutdown by a cleanup process that's
* running as the same user the helper server was running as before privilege
* lowering.
* Therefore, we make the directory only writable by the user the helper server
* was running as before privilege is lowered. Everybody else has read and execute
* rights though, because we want admin tools to be able to list the available
* generations no matter what user they're running as.
*/
makeDirTree(path, "u=rwxs,g=rx,o=rx");
}
bool isDirectory(const string &dir, struct dirent *entry) const {
#ifdef DT_DIR
if (entry->d_type == DT_DIR) {
return true;
} else if (entry->d_type != DT_UNKNOWN) {
return false;
}
// If DT_UNKNOWN, use normal check.
#endif
string path = dir;
path.append("/");
path.append(entry->d_name);
return getFileType(path) == FT_DIRECTORY;
}
public:
ServerInstanceDir(pid_t webServerPid, const string &parentDir = "", bool owner = true) {
string theParentDir;
if (parentDir.empty()) {
theParentDir = getSystemTempDir();
} else {
theParentDir = parentDir;
}
/* We embed the super structure version in the server instance directory name
* because it's possible to upgrade Phusion Passenger without changing the
* web server's PID. This way each incompatible upgrade will use its own
* server instance directory.
*/
initialize(theParentDir + "/passenger." +
toString(DIR_STRUCTURE_MAJOR_VERSION) + "." +
toString(DIR_STRUCTURE_MINOR_VERSION) + "." +
toString<unsigned long long>(webServerPid),
owner);
}
ServerInstanceDir(const string &path, bool owner = true) {
initialize(path, owner);
}
~ServerInstanceDir() {
if (owner) {
GenerationPtr newestGeneration;
try {
newestGeneration = getNewestGeneration();
} catch (const FileSystemException &e) {
if (e.code() == ENOENT) {
return;
} else {
throw;
}
}
if (newestGeneration == NULL) {
removeDirTree(path);
}
}
}
string getPath() const {
return path;
}
void detach() {
owner = false;
}
GenerationPtr newGeneration(bool userSwitching, const string &defaultUser,
const string &defaultGroup, uid_t webServerWorkerUid,
gid_t webServerWorkerGid)
{
GenerationPtr newestGeneration = getNewestGeneration();
unsigned int newNumber;
if (newestGeneration != NULL) {
newNumber = newestGeneration->getNumber() + 1;
} else {
newNumber = 0;
}
GenerationPtr generation(new Generation(path, newNumber));
generation->create(userSwitching, defaultUser, defaultGroup,
webServerWorkerUid, webServerWorkerGid);
return generation;
}
GenerationPtr getGeneration(unsigned int number) const {
return ptr(new Generation(path, number));
}
GenerationPtr getNewestGeneration() const {
DIR *dir = opendir(path.c_str());
struct dirent *entry;
int result = -1;
if (dir == NULL) {
int e = errno;
throw FileSystemException("Cannot open directory " + path,
e, path);
}
while ((entry = readdir(dir)) != NULL) {
if (isDirectory(path, entry)
&& strncmp(entry->d_name, "generation-", sizeof("generation-") - 1) == 0) {
const char *numberString = entry->d_name + sizeof("generation-") - 1;
int number = atoi(numberString);
if (number >= 0 && number > result) {
result = number;
}
}
}
closedir(dir);
if (result == -1) {
return GenerationPtr();
} else {
return getGeneration(result);
}
}
};
typedef shared_ptr<ServerInstanceDir> ServerInstanceDirPtr;
} // namespace Passenger
#endif /* _PASSENGER_SERVER_INSTANCE_DIR_H_ */