X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fsocket.cpp;h=78b7f2de92214b85731ac6664081f589f26cbad2;hb=66dbd438f23a6beb06b0d44b9121deeb1e3f73bc;hp=5827b8b7743fb7c17c43123bba3cdc86bdd28f74;hpb=7a24867d97c6ffe75b155d96dedb11b30b904a33;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/socket.cpp b/src/socket.cpp index 5827b8b77..78b7f2de9 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -1,11 +1,17 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2009-2010 Daniel De Graaf - * Copyright (C) 2007-2008 Robin Burchell - * Copyright (C) 2005-2008 Craig Edwards + * Copyright (C) 2019 linuxdaemon + * Copyright (C) 2014 Attila Molnar + * Copyright (C) 2013, 2017-2020 Sadie Powell + * Copyright (C) 2013 Daniel Vassdal + * Copyright (C) 2012 Robby + * Copyright (C) 2009-2011 Daniel De Graaf + * Copyright (C) 2009 Uli Schlachter + * Copyright (C) 2008 Thomas Stagner + * Copyright (C) 2007 John Brooks * Copyright (C) 2007 Dennis Friis - * Copyright (C) 2006 Oliver Lupton + * Copyright (C) 2006 Craig Edwards * * This file is part of InspIRCd. InspIRCd is free software: you can * redistribute it and/or modify it under the terms of the GNU General Public @@ -39,9 +45,9 @@ bool InspIRCd::BindPort(ConfigTag* tag, const irc::sockets::sockaddrs& sa, std:: return true; } } - + ListenSocket* ll = new ListenSocket(tag, sa); - if (ll->GetFd() < 0) + if (!ll->HasFd()) { ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "Failed to listen on %s from tag at %s: %s", sa.str().c_str(), tag->getTagLocation().c_str(), strerror(errno)); @@ -54,9 +60,9 @@ bool InspIRCd::BindPort(ConfigTag* tag, const irc::sockets::sockaddrs& sa, std:: return true; } -int InspIRCd::BindPorts(FailedPortList& failed_ports) +size_t InspIRCd::BindPorts(FailedPortList& failed_ports) { - int bound = 0; + size_t bound = 0; std::vector old_ports(ports.begin(), ports.end()); ConfigTagList tags = ServerInstance->Config->ConfTags("bind"); @@ -86,7 +92,7 @@ int InspIRCd::BindPorts(FailedPortList& failed_ports) continue; if (!BindPort(tag, bindspec, old_ports)) - failed_ports.push_back(std::make_pair(bindspec, errno)); + failed_ports.push_back(FailedPort(errno, bindspec, tag)); else bound++; } @@ -98,22 +104,29 @@ int InspIRCd::BindPorts(FailedPortList& failed_ports) const std::string path = tag->getString("path"); if (!path.empty()) { + // Expand the path relative to the config directory. + const std::string fullpath = ServerInstance->Config->Paths.PrependData(path); + // UNIX socket paths are length limited to less than PATH_MAX. irc::sockets::sockaddrs bindspec; - if (path.length() > std::min(ServerInstance->Config->Limits.MaxHost, sizeof(bindspec.un.sun_path))) + if (fullpath.length() > std::min(ServerInstance->Config->Limits.MaxHost, sizeof(bindspec.un.sun_path) - 1)) { this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path that is too long!", - path.c_str(), tag->getTagLocation().c_str()); + fullpath.c_str(), tag->getTagLocation().c_str()); continue; } - // Create the bindspec manually (aptosa doesn't work with AF_UNIX yet). - memset(&bindspec, 0, sizeof(bindspec)); - bindspec.un.sun_family = AF_UNIX; - memcpy(&bindspec.un.sun_path, path.c_str(), sizeof(bindspec.un.sun_path)); + // Check for characters which are problematic in the IRC message format. + if (fullpath.find_first_of("\n\r\t!@: ") != std::string::npos) + { + this->Logs->Log("SOCKET", LOG_DEFAULT, "UNIX listener on %s at %s specified a path containing invalid characters!", + fullpath.c_str(), tag->getTagLocation().c_str()); + continue; + } + irc::sockets::untosa(fullpath, bindspec); if (!BindPort(tag, bindspec, old_ports)) - failed_ports.push_back(std::make_pair(bindspec, errno)); + failed_ports.push_back(FailedPort(errno, bindspec, tag)); else bound++; } @@ -174,6 +187,28 @@ bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::socka return false; } +bool irc::sockets::untosa(const std::string& path, irc::sockets::sockaddrs& sa) +{ + memset(&sa, 0, sizeof(sa)); + if (path.length() >= sizeof(sa.un.sun_path)) + return false; + + sa.un.sun_family = AF_UNIX; + memcpy(&sa.un.sun_path, path.c_str(), path.length() + 1); + return true; +} + +bool irc::sockets::isunix(const std::string& file) +{ +#ifndef _WIN32 + struct stat sb; + if (stat(file.c_str(), &sb) == 0 && S_ISSOCK(sb.st_mode)) + return true; +#endif + return false; +} + + int irc::sockets::sockaddrs::family() const { return sa.sa_family; @@ -354,7 +389,7 @@ irc::sockets::cidr_mask::cidr_mask(const std::string& mask) } else { - int range = ConvToInt(mask.substr(bits_chars + 1)); + unsigned char range = ConvToNum(mask.substr(bits_chars + 1)); irc::sockets::aptosa(mask.substr(0, bits_chars), 0, sa); sa2cidr(*this, sa, range); } @@ -377,6 +412,7 @@ std::string irc::sockets::cidr_mask::str() const case AF_INET6: base = (unsigned char*)&sa.in6.sin6_addr; len = 16; + break; case AF_UNIX: return sa.un.sun_path; @@ -388,7 +424,7 @@ std::string irc::sockets::cidr_mask::str() const } memcpy(base, bits, len); - return sa.addr() + "/" + ConvToStr(length); + return sa.addr() + "/" + ConvToStr((int)length); } bool irc::sockets::cidr_mask::operator==(const cidr_mask& other) const