2010-08-10 18:12:50 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-02-02 18:34:22 +01:00
|
|
|
* Copyright (c) 2010-2022, Tomasz Sowa
|
2010-08-10 18:12:50 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2010-08-10 18:12:50 +02:00
|
|
|
*/
|
|
|
|
|
2011-01-23 15:15:30 +01:00
|
|
|
#ifndef headerfile_winix_core_app
|
|
|
|
#define headerfile_winix_core_app
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <ctime>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
2010-11-23 22:52:25 +01:00
|
|
|
#include <fcgiapp.h>
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
#include "sessionmanager.h"
|
2010-09-18 02:51:12 +02:00
|
|
|
#include "db/db.h"
|
2010-08-10 18:12:50 +02:00
|
|
|
#include "functions/functions.h"
|
|
|
|
#include "templates/templates.h"
|
2010-08-12 21:10:12 +02:00
|
|
|
#include "compress.h"
|
|
|
|
#include "postparser.h"
|
|
|
|
#include "cookieparser.h"
|
|
|
|
#include "postmultiparser.h"
|
2022-02-02 18:34:22 +01:00
|
|
|
#include "utils/acceptencodingparser.h"
|
2018-11-21 12:03:53 +01:00
|
|
|
#include "winixrequest.h"
|
2018-11-23 18:53:43 +01:00
|
|
|
#include "log/log.h"
|
2018-11-21 12:03:53 +01:00
|
|
|
#include "filelog.h"
|
2021-06-16 18:07:44 +02:00
|
|
|
#include "models/winixmodelconnector.h"
|
2018-11-21 12:03:53 +01:00
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
class App
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
App();
|
|
|
|
|
2010-11-23 22:52:25 +01:00
|
|
|
bool InitFCGI();
|
|
|
|
bool DropPrivileges();
|
2018-11-21 23:53:24 +01:00
|
|
|
void InitLoggers();
|
|
|
|
Log & GetMainLog();
|
2018-11-21 12:03:53 +01:00
|
|
|
void InitPlugins();
|
2010-08-10 18:12:50 +02:00
|
|
|
bool Init();
|
|
|
|
void Start();
|
|
|
|
void Close();
|
2010-10-24 19:49:38 +02:00
|
|
|
void LogUserGroups();
|
2021-06-24 15:33:44 +02:00
|
|
|
void LogRequestTime();
|
2010-10-24 21:26:54 +02:00
|
|
|
bool Demonize();
|
2010-12-02 02:02:02 +01:00
|
|
|
void SetStopSignal();
|
|
|
|
bool WasStopSignal();
|
|
|
|
bool Lock();
|
|
|
|
void Unlock();
|
|
|
|
void StartThreads();
|
|
|
|
void WaitForThreads();
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
// configuration read from a config file
|
|
|
|
Config config;
|
|
|
|
|
2011-01-23 15:15:30 +01:00
|
|
|
// pointers to the current request and a session
|
|
|
|
Cur cur;
|
|
|
|
|
|
|
|
// temporary one request object
|
2010-08-10 18:12:50 +02:00
|
|
|
// current request
|
2011-01-23 15:15:30 +01:00
|
|
|
Request req;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
// users sessions
|
|
|
|
SessionManager session_manager;
|
|
|
|
|
2018-11-21 12:03:53 +01:00
|
|
|
// database (DEPRACATED)
|
2010-08-10 18:12:50 +02:00
|
|
|
Db db;
|
2010-09-18 02:51:12 +02:00
|
|
|
DbConn db_conn;
|
2010-08-10 18:12:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-15 04:09:02 +02:00
|
|
|
// ...
|
2010-08-10 18:12:50 +02:00
|
|
|
System system;
|
|
|
|
|
2011-07-15 04:09:02 +02:00
|
|
|
// functions (ls, cat, emacs, ...)
|
|
|
|
Functions functions;
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
// false at the beginning
|
2013-12-04 02:21:57 +01:00
|
|
|
// !! IMPROVE ME moze to do loggera dac?
|
2010-08-10 18:12:50 +02:00
|
|
|
bool stdout_is_closed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Templates templates;
|
|
|
|
|
|
|
|
|
2018-11-21 12:03:53 +01:00
|
|
|
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
private:
|
2010-08-12 21:10:12 +02:00
|
|
|
|
|
|
|
enum Header
|
|
|
|
{
|
|
|
|
h_200,
|
|
|
|
h_404,
|
|
|
|
h_403
|
|
|
|
};
|
|
|
|
|
|
|
|
PostParser post_parser;
|
|
|
|
PostMultiParser post_multi_parser;
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::SpaceParser space_parser;
|
2018-04-26 20:56:31 +02:00
|
|
|
std::string post_buffer;
|
|
|
|
|
2010-08-12 21:10:12 +02:00
|
|
|
CookieParser cookie_parser;
|
2022-02-02 17:58:27 +01:00
|
|
|
AcceptBaseParser accept_base_parser;
|
2011-07-29 00:18:10 +02:00
|
|
|
AcceptEncodingParser accept_encoding_parser;
|
2010-08-12 21:10:12 +02:00
|
|
|
Compress compress;
|
2010-11-23 22:52:25 +01:00
|
|
|
FCGX_Request fcgi_request;
|
|
|
|
int fcgi_socket;
|
2010-12-02 02:02:02 +01:00
|
|
|
Synchro synchro;
|
|
|
|
pthread_t signal_thread;
|
2015-01-02 08:01:08 +01:00
|
|
|
std::string socket_to_send_on_exit;
|
2012-09-11 07:19:45 +02:00
|
|
|
std::string send_data_buf;
|
2021-07-16 18:17:57 +02:00
|
|
|
pt::WTextStream json_out_stream;
|
2013-11-29 22:39:23 +01:00
|
|
|
std::string aheader_name, aheader_value;
|
2021-10-13 01:27:14 +02:00
|
|
|
//std::wstring html_filtered;
|
|
|
|
//std::string output_8bit;
|
|
|
|
pt::TextStream serialized_model;
|
|
|
|
|
2022-01-28 15:54:11 +01:00
|
|
|
pt::WTextStream output_tmp_filtered_stream;
|
|
|
|
BinaryPage output_8bit;
|
2013-12-04 02:21:57 +01:00
|
|
|
BinaryPage compressed_output;
|
2021-10-13 01:27:14 +02:00
|
|
|
|
2014-11-22 16:30:56 +01:00
|
|
|
std::wstring cookie_id_string;
|
2021-04-09 17:50:58 +02:00
|
|
|
std::wstring http_header_name;
|
|
|
|
std::wstring http_header_value;
|
2018-05-01 13:22:55 +02:00
|
|
|
std::string http_header_8bit;
|
2021-07-16 18:17:57 +02:00
|
|
|
pt::WTextStream empty_response;
|
2018-04-25 21:48:47 +02:00
|
|
|
|
2021-06-16 18:07:44 +02:00
|
|
|
WinixModelConnector model_connector; // main thread model connector, each thread has its own connector
|
2018-11-21 12:03:53 +01:00
|
|
|
morm::JSONConnector json_connector;
|
|
|
|
morm::PostgreSQLConnector postgresql_connector;
|
|
|
|
|
|
|
|
// objects for main thread
|
|
|
|
WinixBase winix_base;
|
2021-06-16 18:07:44 +02:00
|
|
|
WinixModelDeprecated winix_model;
|
2018-11-21 12:03:53 +01:00
|
|
|
WinixSystem winix_system;
|
|
|
|
WinixRequest winix_request;
|
|
|
|
// ///////////////////////
|
|
|
|
|
|
|
|
Plugin plugin;
|
|
|
|
|
|
|
|
//////////////////////////
|
|
|
|
|
|
|
|
// log_buffer for the main thread
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::WTextStream log_buffer;
|
2018-11-21 12:03:53 +01:00
|
|
|
|
2021-06-16 18:07:44 +02:00
|
|
|
// logger only for the main thread
|
2018-11-21 12:03:53 +01:00
|
|
|
Log log;
|
2010-08-12 21:10:12 +02:00
|
|
|
|
2021-06-16 18:07:44 +02:00
|
|
|
// file logger, one object for all Log objects
|
2018-11-21 23:53:24 +01:00
|
|
|
FileLog file_log;
|
|
|
|
|
2014-10-09 22:44:56 +02:00
|
|
|
bool InitFCGI(char * sock, char * sock_user, char * sock_group);
|
|
|
|
bool InitFCGIChmodChownSocket(char * sock, char * sock_user, char * sock_group);
|
|
|
|
bool DropPrivileges(char * user, char * group);
|
|
|
|
bool DropPrivileges(const char * user, uid_t uid, gid_t gid, bool additional_groups);
|
2012-10-17 21:52:55 +02:00
|
|
|
bool CheckAccessFromPlugins();
|
2010-08-12 21:10:12 +02:00
|
|
|
void ProcessRequestThrow();
|
2010-08-10 18:12:50 +02:00
|
|
|
void ProcessRequest();
|
2012-06-07 02:26:37 +02:00
|
|
|
void BaseUrlRedirect(int code, bool add_subdomain);
|
2010-08-10 18:12:50 +02:00
|
|
|
bool BaseUrlRedirect();
|
2012-09-24 22:31:01 +02:00
|
|
|
void CheckIfNeedSSLredirect();
|
2012-06-27 01:19:19 +02:00
|
|
|
void SetLocale();
|
2012-09-04 22:24:42 +02:00
|
|
|
void CheckPostRedirect();
|
2022-02-01 18:44:23 +01:00
|
|
|
void UseEzcGenerator();
|
2021-06-20 18:04:50 +02:00
|
|
|
void AddDefaultModels();
|
2010-08-10 18:12:50 +02:00
|
|
|
void Make();
|
2013-12-04 02:21:57 +01:00
|
|
|
void SaveSessionsIfNeeded(); // !! IMPROVE ME wywalic do menagera sesji??
|
2010-08-12 21:10:12 +02:00
|
|
|
void LogAccess();
|
2012-09-11 07:19:45 +02:00
|
|
|
void SendData(const BinaryPage & page, FCGX_Stream * out);
|
2010-08-12 21:10:12 +02:00
|
|
|
|
|
|
|
void ReadRequest();
|
2022-02-01 18:44:23 +01:00
|
|
|
|
2010-08-12 21:10:12 +02:00
|
|
|
void SendAnswer();
|
2022-02-01 18:44:23 +01:00
|
|
|
void PrepareRawAnswer();
|
|
|
|
void PrepareJsonAnswer();
|
|
|
|
void PrepareXmlAnswer();
|
|
|
|
void PrepareCsvAnswer();
|
|
|
|
void PrepareContenerizedAnswer();
|
|
|
|
|
|
|
|
void PutSeparatorIfNeeded(bool put_separator);
|
|
|
|
|
|
|
|
void SerializeFieldJson(const wchar_t * field_name);
|
|
|
|
|
2021-10-13 01:27:14 +02:00
|
|
|
void SerializeStream(const pt::WTextStream & input_stream, const wchar_t * field_name);
|
|
|
|
void SerializeStreamJson(const pt::WTextStream & input_stream, const wchar_t * field_name);
|
|
|
|
void SerializeStreamXml(const pt::WTextStream & input_stream, const wchar_t * field_name);
|
|
|
|
void SerializeStreamCsv(const pt::WTextStream & input_stream, const wchar_t * field_name);
|
|
|
|
void SerializeAllFrames();
|
2022-02-01 18:44:23 +01:00
|
|
|
void SerializeSpecificFrames();
|
2021-10-13 01:27:14 +02:00
|
|
|
void SerializeModels();
|
|
|
|
void Send8bitOutput(BinaryPage & output);
|
|
|
|
|
|
|
|
void SerializeModel(morm::Wrapper & wrapper, const wchar_t * field_name);
|
|
|
|
void SerializeModelJson(morm::Wrapper & wrapper, const wchar_t * field_name);
|
|
|
|
void SerializeModelXml(morm::Wrapper & wrapper, const wchar_t * field_name);
|
|
|
|
void SerializeModelCsv(morm::Wrapper & wrapper, const wchar_t * field_name);
|
|
|
|
|
|
|
|
void FilterHtmlIfNeeded(const pt::WTextStream & input_stream, BinaryPage & output, bool clear_stream = true);
|
|
|
|
|
2010-08-12 21:10:12 +02:00
|
|
|
|
2013-11-29 22:39:23 +01:00
|
|
|
void LogEnvironmentVariables();
|
2018-04-25 21:48:47 +02:00
|
|
|
void LogEnvironmentHTTPVariables();
|
2022-02-02 18:20:32 +01:00
|
|
|
void ParseAcceptHeader(const wchar_t * header_name, const std::wstring & env, std::vector<HeaderValue> & container, size_t max_len);
|
2022-02-02 17:58:27 +01:00
|
|
|
void ParseAcceptHeader();
|
2022-02-02 18:20:32 +01:00
|
|
|
void ParseAcceptLanguageHeader();
|
2011-09-07 00:46:15 +02:00
|
|
|
|
2014-10-09 22:44:56 +02:00
|
|
|
void SetEnv(const char * name, std::wstring & env);
|
2010-08-12 21:10:12 +02:00
|
|
|
void ReadEnvVariables();
|
2018-04-25 21:48:47 +02:00
|
|
|
void ReadEnvHTTPVariables();
|
|
|
|
bool SaveEnvHTTPVariable(const char * env);
|
2014-10-09 22:44:56 +02:00
|
|
|
void ReadEnvRemoteIP();
|
2018-04-26 20:56:31 +02:00
|
|
|
void ReadPostJson();
|
2014-10-09 22:44:56 +02:00
|
|
|
void ReadPostVars();
|
2010-08-12 21:10:12 +02:00
|
|
|
|
|
|
|
void CheckIE();
|
|
|
|
void CheckKonqueror();
|
|
|
|
void CheckRequestMethod();
|
2011-09-07 00:46:15 +02:00
|
|
|
void CheckSSL();
|
2021-05-31 10:58:34 +02:00
|
|
|
void CheckHtmx();
|
2012-04-22 20:23:44 +02:00
|
|
|
void SetSubdomain();
|
2010-08-12 21:10:12 +02:00
|
|
|
|
2021-10-08 03:08:11 +02:00
|
|
|
bool IsRequestedFrame();
|
2013-11-14 21:59:23 +01:00
|
|
|
|
2013-12-04 02:21:57 +01:00
|
|
|
Header GetHTTPStatusCode();
|
2010-08-12 21:10:12 +02:00
|
|
|
void PrepareSessionCookie();
|
2013-11-29 22:39:23 +01:00
|
|
|
void SendHeaders();
|
|
|
|
void SendCookies();
|
2013-12-19 18:19:47 +01:00
|
|
|
bool AddHeader(const wchar_t * name, const wchar_t * value);
|
|
|
|
bool AddHeader(const std::wstring & name, const std::wstring & value);
|
2021-05-20 20:59:12 +02:00
|
|
|
bool AddHeader(const wchar_t * name, const pt::WTextStream & value);
|
|
|
|
bool AddHeader(const std::wstring & name, const pt::WTextStream & value);
|
|
|
|
bool PrepareHeadersStaticCreateResource(pt::WTextStream & out_path);
|
2013-11-29 22:39:23 +01:00
|
|
|
void PrepareHeadersStatic();
|
|
|
|
void PrepareHeaderContentType();
|
|
|
|
void PrepareHeadersForbidden();
|
|
|
|
void PrepareHeadersRedirect();
|
|
|
|
void PrepareHeadersSendFile();
|
|
|
|
void PrepareHeadersCompression(int compress_encoding);
|
2013-12-04 02:21:57 +01:00
|
|
|
void PrepareHeadersNormal(Header header, size_t output_size);
|
|
|
|
void PrepareHeaders(bool compressing, int compress_encoding, Header header, size_t output_size);
|
2011-04-02 02:10:16 +02:00
|
|
|
int SelectDeflateVersion();
|
|
|
|
void SelectCompression(size_t source_len, bool & compression_allowed, int & compression_encoding);
|
2013-12-04 02:21:57 +01:00
|
|
|
bool CanSendContent();
|
|
|
|
void ClearAfterRequest();
|
2010-08-12 21:10:12 +02:00
|
|
|
|
2010-10-24 19:49:38 +02:00
|
|
|
void LogUser(const char * msg, uid_t id);
|
|
|
|
void LogGroup(const char * msg, gid_t id, bool put_logend = true);
|
|
|
|
void LogUsers();
|
|
|
|
void LogEffectiveGroups(std::vector<gid_t> & tab);
|
|
|
|
void LogGroups();
|
2010-08-10 18:12:50 +02:00
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
static void * SpecialThreadForSignals(void*);
|
2015-01-02 08:01:08 +01:00
|
|
|
void SendEmptyFastCGIPacket();
|
2010-12-02 02:02:02 +01:00
|
|
|
|
2010-12-10 22:07:01 +01:00
|
|
|
void CreateStaticTree();
|
|
|
|
|
2021-05-14 03:31:29 +02:00
|
|
|
bool DoDatabaseMigration();
|
|
|
|
bool TryToMakeDatabaseMigration();
|
|
|
|
|
|
|
|
|
2013-12-04 02:21:57 +01:00
|
|
|
// !! IMPROVE ME
|
|
|
|
// !! move to the session manager?
|
2010-08-10 18:12:50 +02:00
|
|
|
time_t last_sessions_save;
|
|
|
|
};
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|