00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _PASSENGER_MESSAGE_SERVER_H_
00026 #define _PASSENGER_MESSAGE_SERVER_H_
00027
00028 #include <string>
00029 #include <vector>
00030
00031 #include <boost/shared_ptr.hpp>
00032 #include <boost/thread.hpp>
00033 #include <oxt/system_calls.hpp>
00034 #include <oxt/dynamic_thread_group.hpp>
00035
00036 #include <sys/types.h>
00037 #include <sys/stat.h>
00038 #include <sys/un.h>
00039 #include <unistd.h>
00040 #include <cerrno>
00041 #include <cassert>
00042
00043 #include "Account.h"
00044 #include "AccountsDatabase.h"
00045 #include "Constants.h"
00046 #include "FileDescriptor.h"
00047 #include "MessageChannel.h"
00048 #include "Logging.h"
00049 #include "Exceptions.h"
00050 #include "Utils.h"
00051
00052 namespace Passenger {
00053
00054 using namespace std;
00055 using namespace boost;
00056 using namespace oxt;
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 class MessageServer {
00158 public:
00159 static const unsigned int CLIENT_THREAD_STACK_SIZE = 64 * 1024;
00160
00161
00162 class ClientContext {
00163 public:
00164 virtual ~ClientContext() { }
00165 };
00166
00167 typedef shared_ptr<ClientContext> ClientContextPtr;
00168
00169
00170
00171
00172
00173 class CommonClientContext: public ClientContext {
00174 public:
00175
00176 FileDescriptor fd;
00177
00178
00179 MessageChannel channel;
00180
00181
00182 AccountPtr account;
00183
00184
00185 CommonClientContext(FileDescriptor &theFd, AccountPtr &theAccount)
00186 : fd(theFd), channel(theFd), account(theAccount)
00187 { }
00188
00189
00190 string name() {
00191 return toString(channel.filenum());
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 void requireRights(Account::Rights rights) {
00204 if (!account->hasRights(rights)) {
00205 P_TRACE(2, "Security error: insufficient rights to execute this command.");
00206 channel.write("SecurityException", "Insufficient rights to execute this command.", NULL);
00207 throw SecurityException("Insufficient rights to execute this command.");
00208 } else {
00209 channel.write("Passed security", NULL);
00210 }
00211 }
00212 };
00213
00214
00215
00216
00217
00218
00219
00220
00221 class Handler {
00222 public:
00223 virtual ~Handler() { }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 virtual ClientContextPtr newClient(CommonClientContext &context) {
00235 return ClientContextPtr();
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 virtual void clientDisconnected(MessageServer::CommonClientContext &context,
00250 MessageServer::ClientContextPtr &handlerSpecificContext)
00251 { }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 virtual bool processMessage(CommonClientContext &commonContext,
00266 ClientContextPtr &handlerSpecificContext,
00267 const vector<string> &args) = 0;
00268 };
00269
00270 typedef shared_ptr<Handler> HandlerPtr;
00271
00272 protected:
00273
00274 string socketFilename;
00275
00276
00277 AccountsDatabasePtr accountsDatabase;
00278
00279
00280 vector<HandlerPtr> handlers;
00281
00282
00283
00284
00285
00286
00287 unsigned long long loginTimeout;
00288
00289
00290 dynamic_thread_group threadGroup;
00291
00292
00293
00294
00295 int serverFd;
00296
00297
00298
00299 struct DisconnectEventBroadcastGuard {
00300 vector<HandlerPtr> &handlers;
00301 CommonClientContext &commonContext;
00302 vector<ClientContextPtr> &handlerSpecificContexts;
00303
00304 DisconnectEventBroadcastGuard(vector<HandlerPtr> &_handlers,
00305 CommonClientContext &_commonContext,
00306 vector<ClientContextPtr> &_handlerSpecificContexts)
00307 : handlers(_handlers),
00308 commonContext(_commonContext),
00309 handlerSpecificContexts(_handlerSpecificContexts)
00310 { }
00311
00312 ~DisconnectEventBroadcastGuard() {
00313 vector<HandlerPtr>::iterator handler_iter;
00314 vector<ClientContextPtr>::iterator context_iter;
00315
00316 for (handler_iter = handlers.begin(), context_iter = handlerSpecificContexts.begin();
00317 handler_iter != handlers.end();
00318 handler_iter++, context_iter++) {
00319 (*handler_iter)->clientDisconnected(commonContext, *context_iter);
00320 }
00321 }
00322 };
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 void startListening() {
00334 TRACE_POINT();
00335 int ret;
00336
00337 serverFd = createUnixServer(socketFilename.c_str());
00338 do {
00339 ret = chmod(socketFilename.c_str(),
00340 S_ISVTX |
00341 S_IRUSR | S_IWUSR | S_IXUSR |
00342 S_IRGRP | S_IWGRP | S_IXGRP |
00343 S_IROTH | S_IWOTH | S_IXOTH);
00344 } while (ret == -1 && errno == EINTR);
00345 }
00346
00347
00348
00349
00350
00351
00352 AccountPtr authenticate(FileDescriptor &client) {
00353 MessageChannel channel(client);
00354 string username, password;
00355 MemZeroGuard passwordGuard(password);
00356 unsigned long long timeout = loginTimeout;
00357
00358 try {
00359 try {
00360 if (!channel.readScalar(username, 50, &timeout)) {
00361 return AccountPtr();
00362 }
00363 } catch (const SecurityException &) {
00364 channel.write("The supplied username is too long.", NULL);
00365 return AccountPtr();
00366 }
00367
00368 try {
00369 if (!channel.readScalar(password, MESSAGE_SERVER_MAX_PASSWORD_SIZE, &timeout)) {
00370 return AccountPtr();
00371 }
00372 } catch (const SecurityException &) {
00373 channel.write("The supplied password is too long.", NULL);
00374 return AccountPtr();
00375 }
00376
00377 AccountPtr account = accountsDatabase->authenticate(username, password);
00378 passwordGuard.zeroNow();
00379 if (account == NULL) {
00380 channel.write("Invalid username or password.", NULL);
00381 return AccountPtr();
00382 } else {
00383 channel.write("ok", NULL);
00384 return account;
00385 }
00386 } catch (const SystemException &) {
00387 return AccountPtr();
00388 } catch (const TimeoutException &) {
00389 return AccountPtr();
00390 }
00391 }
00392
00393 void broadcastNewClientEvent(CommonClientContext &context,
00394 vector<ClientContextPtr> &handlerSpecificContexts) {
00395 vector<HandlerPtr>::iterator it;
00396
00397 for (it = handlers.begin(); it != handlers.end(); it++) {
00398 handlerSpecificContexts.push_back((*it)->newClient(context));
00399 }
00400 }
00401
00402 bool processMessage(CommonClientContext &commonContext,
00403 vector<ClientContextPtr> &handlerSpecificContexts,
00404 const vector<string> &args) {
00405 vector<HandlerPtr>::iterator handler_iter;
00406 vector<ClientContextPtr>::iterator context_iter;
00407
00408 for (handler_iter = handlers.begin(), context_iter = handlerSpecificContexts.begin();
00409 handler_iter != handlers.end();
00410 handler_iter++, context_iter++) {
00411 if ((*handler_iter)->processMessage(commonContext, *context_iter, args)) {
00412 return true;
00413 }
00414 }
00415 return false;
00416 }
00417
00418 void processUnknownMessage(CommonClientContext &commonContext, const vector<string> &args) {
00419 TRACE_POINT();
00420 string name;
00421 if (args.empty()) {
00422 name = "(null)";
00423 } else {
00424 name = args[0];
00425 }
00426 P_TRACE(2, "A MessageServer client sent an invalid command: "
00427 << name << " (" << args.size() << " elements)");
00428 }
00429
00430
00431
00432
00433 void clientHandlingMainLoop(FileDescriptor &client) {
00434 TRACE_POINT();
00435 vector<string> args;
00436
00437 P_TRACE(4, "MessageServer client thread " << (int) client << " started.");
00438
00439 try {
00440 AccountPtr account(authenticate(client));
00441 if (account == NULL) {
00442 P_TRACE(4, "MessageServer client thread " << (int) client << " exited.");
00443 return;
00444 }
00445
00446 CommonClientContext commonContext(client, account);
00447 vector<ClientContextPtr> handlerSpecificContexts;
00448 broadcastNewClientEvent(commonContext, handlerSpecificContexts);
00449 DisconnectEventBroadcastGuard dguard(handlers, commonContext, handlerSpecificContexts);
00450
00451 while (!this_thread::interruption_requested()) {
00452 UPDATE_TRACE_POINT();
00453 if (!commonContext.channel.read(args)) {
00454
00455 break;
00456 }
00457
00458 P_TRACE(4, "MessageServer client " << commonContext.name() <<
00459 ": received message: " << toString(args));
00460
00461 UPDATE_TRACE_POINT();
00462 if (!processMessage(commonContext, handlerSpecificContexts, args)) {
00463 processUnknownMessage(commonContext, args);
00464 break;
00465 }
00466 args.clear();
00467 }
00468
00469 client.close();
00470 P_TRACE(4, "MessageServer client thread " << (int) client << " exited.");
00471 } catch (const boost::thread_interrupted &) {
00472 P_TRACE(2, "MessageServer client thread " << (int) client << " interrupted.");
00473 } catch (const tracable_exception &e) {
00474 P_TRACE(2, "An error occurred in a MessageServer client thread " << (int) client << ":\n"
00475 << " message: " << toString(args) << "\n"
00476 << " exception: " << e.what() << "\n"
00477 << " backtrace:\n" << e.backtrace());
00478 } catch (const std::exception &e) {
00479 P_TRACE(2, "An error occurred in a MessageServer client thread " << (int) client <<":\n"
00480 << " message: " << toString(args) << "\n"
00481 << " exception: " << e.what() << "\n"
00482 << " backtrace: not available");
00483 } catch (...) {
00484 P_TRACE(2, "An unknown exception occurred in a MessageServer client thread.");
00485 }
00486 }
00487
00488 public:
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 MessageServer(const string &socketFilename, AccountsDatabasePtr accountsDatabase) {
00502 this->socketFilename = socketFilename;
00503 this->accountsDatabase = accountsDatabase;
00504 loginTimeout = 2000;
00505 startListening();
00506 }
00507
00508 ~MessageServer() {
00509 this_thread::disable_syscall_interruption dsi;
00510 syscalls::close(serverFd);
00511 syscalls::unlink(socketFilename.c_str());
00512 }
00513
00514 string getSocketFilename() const {
00515 return socketFilename;
00516 }
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 void mainLoop() {
00528 TRACE_POINT();
00529 while (true) {
00530 this_thread::interruption_point();
00531 sockaddr_un addr;
00532 socklen_t len = sizeof(addr);
00533 FileDescriptor fd;
00534
00535 UPDATE_TRACE_POINT();
00536 fd = syscalls::accept(serverFd, (struct sockaddr *) &addr, &len);
00537 if (fd == -1) {
00538 throw SystemException("Unable to accept a new client", errno);
00539 }
00540
00541 UPDATE_TRACE_POINT();
00542 this_thread::disable_interruption di;
00543 this_thread::disable_syscall_interruption dsi;
00544
00545 function<void ()> func(boost::bind(&MessageServer::clientHandlingMainLoop,
00546 this, fd));
00547 string name = "MessageServer client thread ";
00548 name.append(toString(fd));
00549 threadGroup.create_thread(func, name, CLIENT_THREAD_STACK_SIZE);
00550 }
00551 }
00552
00553
00554
00555
00556
00557
00558 void addHandler(HandlerPtr handler) {
00559 handlers.push_back(handler);
00560 }
00561
00562
00563
00564
00565
00566
00567
00568
00569 void setLoginTimeout(unsigned long long timeout) {
00570 assert(timeout != 0);
00571 loginTimeout = timeout;
00572 }
00573 };
00574
00575 typedef shared_ptr<MessageServer> MessageServerPtr;
00576
00577 }
00578
00579 #endif