diff options
68 files changed, 3360 insertions, 34 deletions
diff --git a/data/modules.example.conf b/data/modules.example.conf index 2a06b1a99..d4d86c0ba 100644 --- a/data/modules.example.conf +++ b/data/modules.example.conf @@ -92,6 +92,38 @@ m_helpchan } /* + * m_httpd + * + * Allows services to serve web pages. By itself, this module does nothing useful. + * + * Note that using this will allow users to get the IP of your services. + * To prevent this we recommend using a reverse proxy or a tunnel. + */ +#module { name = "m_httpd" } +httpd +{ + /* Name of this service */ + name = "httpd/main" + + /* IP to listen on */ + ip = "0.0.0.0" + /* Port to listen on */ + port = 8080 + /* Time before connections to this server are timed out */ + timeout = 30 + + /* If you are using a reverse proxy that sends one of the + * extforward_headers set below, set this to its IP. + * This allows services to obtain the real IP of users by + * reading the forwarded-for HTTP header. + */ + #extforward_ip = "192.168.0.255" + + /* The header to look for. These probably work as is. */ + extforward_header = "X-Forwarded-For Forwarded-For" +} + +/* * m_ldap * * This module allows other modules to use LDAP. By itself, this module does nothing useful. @@ -440,3 +472,26 @@ ns_maxemail #maxemails = 1 } +/* + * webcpanel + * + * This module creates a web configuration panel that allows users and operators to perform any task + * as they could over IRC. If you are using the default configuration you should be able to access + * this panel by visiting http://127.0.0.1:8080 in your web browser from the machine Anope is running on. + * + * This module requires m_httpd. + */ +#module { name = "webcpanel" } +webcpanel +{ + /* Web server to use */ + server = "httpd/main"; + + /* Template to use */ + template = "default"; + + /* Page title */ + title = "Anope IRC Services"; +} + + diff --git a/include/access.h b/include/access.h index 18499b602..b29572dfc 100644 --- a/include/access.h +++ b/include/access.h @@ -53,6 +53,11 @@ class CoreExport AccessProvider : public Service AccessProvider(Module *o, const Anope::string &n); virtual ~AccessProvider(); virtual ChanAccess *Create() = 0; + + private: + static std::list<AccessProvider *> providers; + public: + static const std::list<AccessProvider *>& GetProviders(); }; class CoreExport ChanAccess : public Serializable diff --git a/include/anope.h b/include/anope.h index 2a51514e0..2ec6602ea 100644 --- a/include/anope.h +++ b/include/anope.h @@ -342,7 +342,7 @@ namespace Anope * @param dest The destination string */ extern CoreExport void Unhex(const string &src, string &dest); - extern CoreExport void Unhex(const string &src, char *dest); + extern CoreExport void Unhex(const string &src, char *dest, size_t sz); /** Base 64 encode a string * @param src The string to encode diff --git a/include/logger.h b/include/logger.h index 9364cfb7c..eea86f890 100644 --- a/include/logger.h +++ b/include/logger.h @@ -49,6 +49,7 @@ class CoreExport Log { public: const BotInfo *bi; + Anope::string nick; const User *u; const NickCore *nc; Command *c; diff --git a/include/services.h b/include/services.h index c03991a13..31ab1cf37 100644 --- a/include/services.h +++ b/include/services.h @@ -55,8 +55,10 @@ #ifdef __GXX_EXPERIMENTAL_CXX0X__ # define anope_override override +# define anope_final final #else # define anope_override +# define anope_final #endif /** diff --git a/include/sockets.h b/include/sockets.h index 16cd10a1a..8f863b59c 100644 --- a/include/sockets.h +++ b/include/sockets.h @@ -288,6 +288,9 @@ class CoreExport BufferedSocket : public virtual Socket /** Write to the socket * @param message The message */ + protected: + virtual void Write(const char *buffer, size_t l); + public: void Write(const char *message, ...); void Write(const Anope::string &message); @@ -304,8 +307,10 @@ class CoreExport BufferedSocket : public virtual Socket class CoreExport BinarySocket : public virtual Socket { + protected: struct DataBlock { + char *orig; char *buf; size_t len; @@ -338,7 +343,9 @@ class CoreExport BinarySocket : public virtual Socket * @param buffer The data to write * @param l The length of the data */ - void Write(const char *buffer, size_t l); + virtual void Write(const char *buffer, size_t l); + void Write(const char *message, ...); + void Write(const Anope::string &message); /** Called with data from the socket * @param buffer The data diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 3653a59f1..13eb83d5a 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -105,7 +105,7 @@ foreach(MODULE_FOLDER ${MODULES_FOLDERS}) foreach(SUBDIR ${SUBMODULE_DIRS}) if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}") - file(GLOB MODULES_SUBDIR_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${SUBDIR}/*.cpp") + file(GLOB_RECURSE MODULES_SUBDIR_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${SUBDIR}/*.cpp") sort_list(MODULES_SUBDIR_SRCS) # Set all the files to use C++ as well as set their compile flags (use the module-specific compile flags, though) @@ -195,6 +195,11 @@ foreach(MODULE_FOLDER ${MODULES_FOLDERS}) else(NOT SKIP_DEPENDS AND NOT SKIP_LIBRARIES AND HAS_FUNCTION) message(" This is not a fatal error - ${SUBDIR} will not be built.") endif(NOT SKIP_DEPENDS AND NOT SKIP_LIBRARIES AND HAS_FUNCTION) + + # Run the directories CMakeLists.txt if there is one + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/CMakeLists.txt") + add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}") + endif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/CMakeLists.txt") endif(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}") endforeach(SUBDIR) endif(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${MODULE_FOLDER}") diff --git a/modules/commands/ns_register.cpp b/modules/commands/ns_register.cpp index 0d51ef20c..1a219c8ba 100644 --- a/modules/commands/ns_register.cpp +++ b/modules/commands/ns_register.cpp @@ -224,9 +224,12 @@ class CommandNSRegister : public Command } else if (Config->NSRegistration.equals_ci("none")) { - ircdproto->SendLogin(u); - if (!Config->NoNicknameOwnership && u && na->nc == u->Account() && na->nc->HasFlag(NI_UNCONFIRMED) == false) - u->SetMode(findbot(Config->NickServ), UMODE_REGISTERED); + if (u) + { + ircdproto->SendLogin(u); + if (!Config->NoNicknameOwnership && na->nc == u->Account() && na->nc->HasFlag(NI_UNCONFIRMED) == false) + u->SetMode(findbot(Config->NickServ), UMODE_REGISTERED); + } } if (u) diff --git a/modules/encryption/enc_sha256.cpp b/modules/encryption/enc_sha256.cpp index b119eeba6..cee21b01d 100644 --- a/modules/encryption/enc_sha256.cpp +++ b/modules/encryption/enc_sha256.cpp @@ -148,7 +148,7 @@ class ESHA256 : public Module size_t pos = password.find(':'); Anope::string buf = password.substr(password.find(':', pos + 1) + 1, password.length()); char buf2[33]; - Anope::Unhex(buf, buf2); + Anope::Unhex(buf, buf2, sizeof(buf2)); for (int i = 0 ; i < 8; ++i) PACK32(reinterpret_cast<unsigned char *>(&buf2[i << 2]), iv[i]); } diff --git a/modules/extra/httpd.h b/modules/extra/httpd.h new file mode 100644 index 000000000..684c11512 --- /dev/null +++ b/modules/extra/httpd.h @@ -0,0 +1,224 @@ +#ifndef ANOPE_HTTPD_H +#define ANOPE_HTTPD_H + +enum HTTPError +{ + HTTP_ERROR_OK = 200, + HTTP_FOUND = 302, + HTTP_BAD_REQUEST = 400, + HTTP_PAGE_NOT_FOUND = 404, + HTTP_NOT_SUPPORTED = 505 +}; + +/* A message to someone */ +struct HTTPReply +{ + HTTPError error; + Anope::string content_type; + std::map<Anope::string, Anope::string> headers; + typedef std::list<std::pair<Anope::string, Anope::string> > cookie; + std::vector<cookie> cookies; + + HTTPReply() : error(HTTP_ERROR_OK), length(0) { } + + struct Data + { + char *buf; + size_t len; + + Data(const char *b, size_t l) + { + this->buf = new char[l]; + memcpy(this->buf, b, l); + this->len = l; + } + + ~Data() + { + delete [] buf; + } + }; + + std::deque<Data *> out; + size_t length; + + void Write(const Anope::string &message) + { + this->out.push_back(new Data(message.c_str(), message.length())); + this->length += message.length(); + } + + void Write(const char *b, size_t l) + { + this->out.push_back(new Data(b, l)); + this->length += l; + } +}; + +/* A message from soneone */ +struct HTTPMessage +{ + std::map<Anope::string, Anope::string> headers; + std::map<Anope::string, Anope::string> cookies; + std::map<Anope::string, Anope::string> get_data; + std::map<Anope::string, Anope::string> post_data; + Anope::string content; +}; + +class HTTPClient; +class HTTPProvider; + +class HTTPPage : public Base +{ + Anope::string url; + Anope::string content_type; + + public: + HTTPPage(const Anope::string &u, const Anope::string &ct = "text/html") : url(u), content_type(ct) { } + + const Anope::string &GetURL() const { return this->url; } + + const Anope::string &GetContentType() const { return this->content_type; } + + /** Called when this page is requested + * @param The server this page is on + * @param The page name + * @param The client requesting the page + * @param The HTTP header sent from the client to request the page + * @param The HTTP header that will be sent back to the client + */ + virtual void OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &) = 0; +}; + +class HTTPClient : public ClientSocket, public BufferedSocket, public BinarySocket +{ + protected: + void WriteClient(const Anope::string &message) + { + BinarySocket::Write(message + "\r\n"); + } + + public: + HTTPClient(ListenSocket *l, int f, const sockaddrs &a) : ClientSocket(l, a), BufferedSocket(), BinarySocket() { } + + virtual const Anope::string GetIP() + { + return this->clientaddr.addr(); + } + + bool ProcessRead() anope_override + { + return BufferedSocket::ProcessRead(); + } + + bool ProcessWrite() anope_override + { + return !BinarySocket::ProcessWrite() || BinarySocket::WriteBuffer.empty() ? false : true; + } + + void Write(const char *buffer, size_t l) anope_override + { + BinarySocket::Write(buffer, l); + } + + virtual void SendError(HTTPError err, const Anope::string &msg) = 0; + virtual void SendReply(HTTPReply *) = 0; +}; + +class HTTPProvider : public Service, public ListenSocket +{ + Anope::string ip; + unsigned short port; + public: + Anope::string ext_ip; + std::vector<Anope::string> ext_headers; + + HTTPProvider(Module *c, const Anope::string &n, const Anope::string &i, const unsigned short p) : Service(c, "HTTPProvider", n), ListenSocket(i, p, i.find(':') != Anope::string::npos), ip(i), port(p) { } + + const Anope::string &GetIP() const + { + return this->ip; + } + + unsigned short GetPort() const + { + return this->port; + } + + virtual bool RegisterPage(HTTPPage *page) = 0; + virtual void UnregisterPage(HTTPPage *page) = 0; + virtual HTTPPage* FindPage(const Anope::string &name) = 0; +}; + +namespace HTTPUtils +{ + inline Anope::string URLDecode(const Anope::string &url) + { + Anope::string decoded; + + for (unsigned i = 0; i < url.length(); ++i) + { + const char& c = url[i]; + + if (c == '%' && i + 2 < url.length()) + { + Anope::string dest; + Anope::Unhex(url.substr(i + 1, 2), dest); + decoded += dest; + i += 2; + } + else if (c == '+') + decoded += ' '; + else + decoded += c; + } + + return decoded; + } + + inline Anope::string URLEncode(const Anope::string &url) + { + Anope::string encoded; + + for (unsigned i = 0; i < url.length(); ++i) + { + const char& c = url[i]; + + if (isalnum(c) || c == '.' || c == '-' || c == '*' || c == '_') + encoded += c; + else if (c == ' ') + encoded += '+'; + else + encoded += "%" + Anope::Hex(c); + } + + return encoded; + } + + inline Anope::string Escape(const Anope::string &src) + { + Anope::string dst; + + for (unsigned i = 0; i < src.length(); ++i) + { + switch (src[i]) + { + case '<': + dst += "<"; + break; + case '>': + dst += ">"; + break; + case '"': + dst += """; + break; + default: + dst += src[i]; + } + } + + return dst; + } +} + +#endif // ANOPE_HTTPD_H diff --git a/modules/extra/m_httpd.cpp b/modules/extra/m_httpd.cpp new file mode 100644 index 000000000..aec3ce4a1 --- /dev/null +++ b/modules/extra/m_httpd.cpp @@ -0,0 +1,415 @@ +/* + * (C) 2003-2012 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + */ + +#include "module.h" +#include "httpd.h" + +static Anope::string BuildDate() +{ + char timebuf[64]; + struct tm *tm = localtime(&Anope::CurTime); + strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %T %Z", tm); + return timebuf; +} + +static Anope::string GetStatusFromCode(HTTPError err) +{ + switch (err) + { + case HTTP_ERROR_OK: + return "200 OK"; + case HTTP_FOUND: + return "302 Found"; + case HTTP_BAD_REQUEST: + return "400 Bad Request"; + case HTTP_PAGE_NOT_FOUND: + return "404 Not Found"; + case HTTP_NOT_SUPPORTED: + return "505 HTTP Version Not Supported"; + } + + return "501 Not Implemented"; +} + +class MyHTTPClient : public HTTPClient, public Base +{ + HTTPProvider *provider; + HTTPMessage header; + bool header_done; + Anope::string page_name; + dynamic_reference<HTTPPage> page; + Anope::string ip; + + enum + { + ACTION_NONE, + ACTION_GET, + ACTION_POST + } action; + + void Serve() + { + if (!this->page) + { + this->SendError(HTTP_PAGE_NOT_FOUND, "Page not found"); + return; + } + + if (this->ip == this->provider->ext_ip) + { + for (unsigned i = 0; i < this->provider->ext_headers.size(); ++i) + { + const Anope::string &token = this->provider->ext_headers[i]; + + if (this->header.headers.count(token)) + { + Log(LOG_DEBUG, "httpd") << "m_httpd: IP for connection " << this->GetFD() << " changed to " << this->ip; + this->ip = this->header.headers[token]; + break; + } + } + } + + Log(LOG_DEBUG, "httpd") << "m_httpd: Serving page " << this->page_name << " to " << this->ip; + + HTTPReply reply; + + this->page->OnRequest(this->provider, this->page_name, this, this->header, reply); + + this->SendReply(&reply); + } + + public: + time_t created; + + MyHTTPClient(HTTPProvider *l, int f, const sockaddrs &a) : Socket(f, l->IsIPv6()), HTTPClient(l, f, a), provider(l), header_done(false), ip(a.addr()), action(ACTION_NONE), created(Anope::CurTime) + { + Log(LOG_DEBUG, "httpd") << "Accepted connection " << f << " from " << a.addr(); + } + + ~MyHTTPClient() + { + Log(LOG_DEBUG, "httpd") << "Closing connection " << this->GetFD() << " from " << this->ip; + } + + const Anope::string GetIP() anope_override + { + return this->ip; + } + + bool Read(const Anope::string &buf) anope_override + { + Log(LOG_DEBUG_2) << "HTTP from " << this->clientaddr.addr() << ": " << buf; + + if (!this->header_done) + { + if (this->action == ACTION_NONE) + { + std::vector<Anope::string> params = BuildStringVector(buf); + + if (params.empty() || (params[0] != "GET" && params[0] != "POST")) + { + this->SendError(HTTP_BAD_REQUEST, "Unknown operation"); + return true; + } + + if (params.size() != 3) + { + this->SendError(HTTP_BAD_REQUEST, "Invalid parameters"); + return true; + } + + if (params[0] == "GET") + this->action = ACTION_GET; + else if (params[0] == "POST") + this->action = |
