00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "inspircd.h"
00018 #include "inspircd_io.h"
00019 #include "inspircd_util.h"
00020 #include "inspircd_config.h"
00021 #include <unistd.h>
00022 #include <fcntl.h>
00023 #include <sys/errno.h>
00024 #include <sys/ioctl.h>
00025 #include <sys/utsname.h>
00026 #include <cstdio>
00027 #include <time.h>
00028 #include <string>
00029 #ifdef GCC3
00030 #include <ext/hash_map>
00031 #else
00032 #include <hash_map>
00033 #endif
00034 #include <map>
00035 #include <sstream>
00036 #include <vector>
00037 #include <errno.h>
00038 #include <deque>
00039 #include <errno.h>
00040 #include <unistd.h>
00041 #include <sched.h>
00042 #include "connection.h"
00043 #include "users.h"
00044 #include "servers.h"
00045 #include "ctables.h"
00046 #include "globals.h"
00047 #include "modules.h"
00048 #include "dynamic.h"
00049 #include "wildcard.h"
00050 #include "message.h"
00051 #include "mode.h"
00052 #include "xline.h"
00053 #include "commands.h"
00054 #include "inspstring.h"
00055
00056 #ifdef GCC3
00057 #define nspace __gnu_cxx
00058 #else
00059 #define nspace std
00060 #endif
00061
00062 using namespace std;
00063
00064 extern int MODCOUNT;
00065 extern std::vector<Module*> modules;
00066 extern std::vector<ircd_module*> factory;
00067
00068 extern int LogLevel;
00069 extern char ServerName[MAXBUF];
00070 extern char Network[MAXBUF];
00071 extern char ServerDesc[MAXBUF];
00072 extern char AdminName[MAXBUF];
00073 extern char AdminEmail[MAXBUF];
00074 extern char AdminNick[MAXBUF];
00075 extern char diepass[MAXBUF];
00076 extern char restartpass[MAXBUF];
00077 extern char motd[MAXBUF];
00078 extern char rules[MAXBUF];
00079 extern char list[MAXBUF];
00080 extern char PrefixQuit[MAXBUF];
00081 extern char DieValue[MAXBUF];
00082
00083 extern int debugging;
00084 extern int WHOWAS_STALE;
00085 extern int WHOWAS_MAX;
00086 extern int DieDelay;
00087 extern time_t startup_time;
00088 extern int NetBufferSize;
00089 extern int MaxWhoResults;
00090 extern time_t nb_start;
00091
00092 extern std::vector<int> fd_reap;
00093 extern std::vector<std::string> module_names;
00094
00095 extern int boundPortCount;
00096 extern int portCount;
00097 extern int UDPportCount;
00098 extern int ports[MAXSOCKS];
00099 extern int defaultRoute;
00100
00101 extern std::vector<long> auth_cookies;
00102 extern std::stringstream config_f;
00103
00104 extern serverrec* me[32];
00105
00106 extern FILE *log_file;
00107
00108
00109 namespace nspace
00110 {
00111 #ifdef GCC34
00112 template<> struct hash<in_addr>
00113 #else
00114 template<> struct nspace::hash<in_addr>
00115 #endif
00116 {
00117 size_t operator()(const struct in_addr &a) const
00118 {
00119 size_t q;
00120 memcpy(&q,&a,sizeof(size_t));
00121 return q;
00122 }
00123 };
00124 #ifdef GCC34
00125 template<> struct hash<string>
00126 #else
00127 template<> struct nspace::hash<string>
00128 #endif
00129 {
00130 size_t operator()(const string &s) const
00131 {
00132 char a[MAXBUF];
00133 static struct hash<const char *> strhash;
00134 strlcpy(a,s.c_str(),MAXBUF);
00135 strlower(a);
00136 return strhash(a);
00137 }
00138 };
00139 }
00140
00141 struct StrHashComp
00142 {
00143
00144 bool operator()(const string& s1, const string& s2) const
00145 {
00146 char a[MAXBUF],b[MAXBUF];
00147 strlcpy(a,s1.c_str(),MAXBUF);
00148 strlcpy(b,s2.c_str(),MAXBUF);
00149 return (strcasecmp(a,b) == 0);
00150 }
00151
00152 };
00153
00154 struct InAddr_HashComp
00155 {
00156
00157 bool operator()(const in_addr &s1, const in_addr &s2) const
00158 {
00159 size_t q;
00160 size_t p;
00161
00162 memcpy(&q,&s1,sizeof(size_t));
00163 memcpy(&p,&s2,sizeof(size_t));
00164
00165 return (q == p);
00166 }
00167
00168 };
00169
00170
00171 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
00172 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
00173 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
00174 typedef std::deque<command_t> command_table;
00175
00176
00177 extern user_hash clientlist;
00178 extern chan_hash chanlist;
00179 extern user_hash whowas;
00180 extern command_table cmdlist;
00181 extern file_cache MOTD;
00182 extern file_cache RULES;
00183 extern address_cache IP;
00184
00185
00186
00187
00188 class ExtMode : public classbase
00189 {
00190 public:
00191 char modechar;
00192 int type;
00193 int params_when_on;
00194 int params_when_off;
00195 bool needsoper;
00196 bool list;
00197 ExtMode(char mc, int ty, bool oper, int p_on, int p_off) : modechar(mc), type(ty), needsoper(oper), params_when_on(p_on), params_when_off(p_off) { };
00198 };
00199
00200 typedef std::vector<ExtMode> ExtModeList;
00201 typedef ExtModeList::iterator ExtModeListIter;
00202
00203
00204 ExtModeList EMode;
00205
00206
00207 bool ModeDefined(char modechar, int type)
00208 {
00209 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
00210 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
00211 {
00212 if ((i->modechar == modechar) && (i->type == type))
00213 {
00214 return true;
00215 }
00216 }
00217 return false;
00218 }
00219
00220 bool ModeIsListMode(char modechar, int type)
00221 {
00222 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
00223 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
00224 {
00225 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
00226 {
00227 return true;
00228 }
00229 }
00230 return false;
00231 }
00232
00233 bool ModeDefinedOper(char modechar, int type)
00234 {
00235 log(DEBUG,"Size of extmodes vector is %d",EMode.size());
00236 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
00237 {
00238 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
00239 {
00240 return true;
00241 }
00242 }
00243 return false;
00244 }
00245
00246
00247 int ModeDefinedOn(char modechar, int type)
00248 {
00249 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
00250 {
00251 if ((i->modechar == modechar) && (i->type == type))
00252 {
00253 return i->params_when_on;
00254 }
00255 }
00256 return 0;
00257 }
00258
00259
00260 int ModeDefinedOff(char modechar, int type)
00261 {
00262 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
00263 {
00264 if ((i->modechar == modechar) && (i->type == type))
00265 {
00266 return i->params_when_off;
00267 }
00268 }
00269 return 0;
00270 }
00271
00272
00273 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
00274 {
00275 if (ModeDefined(modechar,type)) {
00276 return false;
00277 }
00278 EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
00279 return true;
00280 }
00281
00282
00283 void ModeMakeList(char modechar)
00284 {
00285 for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
00286 {
00287 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
00288 {
00289 i->list = true;
00290 return;
00291 }
00292 }
00293 return;
00294 }
00295
00296
00297
00298 Version::Version(int major, int minor, int revision, int build) : Major(major), Minor(minor), Revision(revision), Build(build) { };
00299
00300
00301
00302 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
00303
00304 Module::Module() { }
00305 Module::~Module() { }
00306 void Module::OnUserConnect(userrec* user) { }
00307 void Module::OnUserQuit(userrec* user) { }
00308 void Module::OnUserJoin(userrec* user, chanrec* channel) { }
00309 void Module::OnUserPart(userrec* user, chanrec* channel) { }
00310 void Module::OnPacketTransmit(char *p) { }
00311 void Module::OnPacketReceive(char *p) { }
00312 void Module::OnRehash() { }
00313 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
00314 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
00315 int Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) { return false; }
00316 Version Module::GetVersion() { return Version(1,0,0,0); }
00317 void Module::OnOper(userrec* user) { };
00318 void Module::OnInfo(userrec* user) { };
00319 void Module::OnWhois(userrec* source, userrec* dest) { };
00320 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
00321 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
00322 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
00323 int Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
00324 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
00325 string_list Module::OnUserSync(userrec* user) { string_list empty; return empty; }
00326 string_list Module::OnChannelSync(chanrec* chan) { string_list empty; return empty; }
00327 void Module::On005Numeric(std::string &output) { };
00328 int Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
00329 void Module::OnLoadModule(Module* mod,std::string name) { };
00330
00331
00332
00333
00334
00335
00336 Server::Server()
00337 {
00338 }
00339
00340 Server::~Server()
00341 {
00342 }
00343
00344 void Server::SendOpers(std::string s)
00345 {
00346 WriteOpers("%s",s.c_str());
00347 }
00348
00349 bool Server::MatchText(std::string sliteral, std::string spattern)
00350 {
00351 char literal[MAXBUF],pattern[MAXBUF];
00352 strlcpy(literal,sliteral.c_str(),MAXBUF);
00353 strlcpy(pattern,spattern.c_str(),MAXBUF);
00354 return match(literal,pattern);
00355 }
00356
00357 void Server::SendToModeMask(std::string modes, int flags, std::string text)
00358 {
00359 WriteMode(modes.c_str(),flags,"%s",text.c_str());
00360 }
00361
00362 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
00363 {
00364 return add_channel(user,cname.c_str(),key.c_str(),true);
00365 }
00366
00367 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
00368 {
00369 return del_channel(user,cname.c_str(),reason.c_str(),false);
00370 }
00371
00372 chanuserlist Server::GetUsers(chanrec* chan)
00373 {
00374 chanuserlist userl;
00375 userl.clear();
00376 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
00377 {
00378 if (i->second)
00379 {
00380 if (has_channel(i->second,chan))
00381 {
00382 if (isnick(i->second->nick))
00383 {
00384 userl.push_back(i->second);
00385 }
00386 }
00387 }
00388 }
00389 return userl;
00390 }
00391 void Server::ChangeUserNick(userrec* user, std::string nickname)
00392 {
00393 force_nickchange(user,nickname.c_str());
00394 }
00395
00396 void Server::QuitUser(userrec* user, std::string reason)
00397 {
00398 send_network_quit(user->nick,reason.c_str());
00399 kill_link(user,reason.c_str());
00400 }
00401
00402 bool Server::IsUlined(std::string server)
00403 {
00404 return is_uline(server.c_str());
00405 }
00406
00407 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
00408 {
00409 call_handler(commandname.c_str(),parameters,pcnt,user);
00410 }
00411
00412 void Server::Log(int level, std::string s)
00413 {
00414 log(level,"%s",s.c_str());
00415 }
00416
00417 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams)
00418 {
00419 createcommand(cmd,f,flags,minparams);
00420 }
00421
00422 void Server::SendMode(char **parameters, int pcnt, userrec *user)
00423 {
00424 server_mode(parameters,pcnt,user);
00425 }
00426
00427 void Server::Send(int Socket, std::string s)
00428 {
00429 Write(Socket,"%s",s.c_str());
00430 }
00431
00432 void Server::SendServ(int Socket, std::string s)
00433 {
00434 WriteServ(Socket,"%s",s.c_str());
00435 }
00436
00437 void Server::SendFrom(int Socket, userrec* User, std::string s)
00438 {
00439 WriteFrom(Socket,User,"%s",s.c_str());
00440 }
00441
00442 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
00443 {
00444 if (!Source)
00445 {
00446
00447 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
00448 }
00449 else
00450 {
00451
00452 WriteTo(Source,Dest,"%s",s.c_str());
00453 }
00454 }
00455
00456 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
00457 {
00458 if (IncludeSender)
00459 {
00460 WriteChannel(Channel,User,"%s",s.c_str());
00461 }
00462 else
00463 {
00464 ChanExceptSender(Channel,User,"%s",s.c_str());
00465 }
00466 }
00467
00468 bool Server::CommonChannels(userrec* u1, userrec* u2)
00469 {
00470 return (common_channels(u1,u2) != 0);
00471 }
00472
00473 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
00474 {
00475 if (IncludeSender)
00476 {
00477 WriteCommon(User,"%s",text.c_str());
00478 }
00479 else
00480 {
00481 WriteCommonExcept(User,"%s",text.c_str());
00482 }
00483 }
00484
00485 void Server::SendWallops(userrec* User, std::string text)
00486 {
00487 WriteWallOps(User,false,"%s",text.c_str());
00488 }
00489
00490 void Server::ChangeHost(userrec* user, std::string host)
00491 {
00492 ChangeDisplayedHost(user,host.c_str());
00493 }
00494
00495 void Server::ChangeGECOS(userrec* user, std::string gecos)
00496 {
00497 ChangeName(user,gecos.c_str());
00498 }
00499
00500 bool Server::IsNick(std::string nick)
00501 {
00502 return (isnick(nick.c_str()) != 0);
00503 }
00504
00505 userrec* Server::FindNick(std::string nick)
00506 {
00507 return Find(nick);
00508 }
00509
00510 chanrec* Server::FindChannel(std::string channel)
00511 {
00512 return FindChan(channel.c_str());
00513 }
00514
00515 std::string Server::ChanMode(userrec* User, chanrec* Chan)
00516 {
00517 return cmode(User,Chan);
00518 }
00519
00520 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
00521 {
00522 return has_channel(User,Chan);
00523 }
00524
00525 std::string Server::GetServerName()
00526 {
00527 return getservername();
00528 }
00529
00530 std::string Server::GetNetworkName()
00531 {
00532 return getnetworkname();
00533 }
00534
00535 Admin Server::GetAdmin()
00536 {
00537 return Admin(getadminname(),getadminemail(),getadminnick());
00538 }
00539
00540
00541
00542 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
00543 {
00544 if (type == MT_SERVER)
00545 {
00546 log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
00547 return false;
00548 }
00549 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
00550 {
00551 log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
00552 return false;
00553 }
00554 if ((params_when_on>1) || (params_when_off>1))
00555 {
00556 log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
00557 return false;
00558 }
00559 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
00560 }
00561
00562 bool Server::AddExtendedListMode(char modechar)
00563 {
00564 bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
00565 if (res)
00566 ModeMakeList(modechar);
00567 return res;
00568 }
00569
00570 int Server::CountUsers(chanrec* c)
00571 {
00572 return usercount(c);
00573 }
00574
00575
00576 ConfigReader::ConfigReader()
00577 {
00578 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
00579 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
00580 this->readerror = LoadConf(CONFIG_FILE,this->cache,this->errorlog);
00581 if (!this->readerror)
00582 this->error = CONF_FILE_NOT_FOUND;
00583 }
00584
00585
00586 ConfigReader::~ConfigReader()
00587 {
00588 if (this->cache)
00589 delete this->cache;
00590 if (this->errorlog)
00591 delete this->errorlog;
00592 }
00593
00594
00595 ConfigReader::ConfigReader(std::string filename)
00596 {
00597 this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
00598 this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
00599 this->readerror = LoadConf(filename.c_str(),this->cache,this->errorlog);
00600 if (!this->readerror)
00601 this->error = CONF_FILE_NOT_FOUND;
00602 };
00603
00604 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
00605 {
00606 char val[MAXBUF];
00607 char t[MAXBUF];
00608 char n[MAXBUF];
00609 strlcpy(t,tag.c_str(),MAXBUF);
00610 strlcpy(n,name.c_str(),MAXBUF);
00611 int res = ReadConf(cache,t,n,index,val);
00612 if (!res)
00613 {
00614 this->error = CONF_VALUE_NOT_FOUND;
00615 return "";
00616 }
00617 return std::string(val);
00618 }
00619
00620 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
00621 {
00622 char val[MAXBUF];
00623 char t[MAXBUF];
00624 char n[MAXBUF];
00625 strlcpy(t,tag.c_str(),MAXBUF);
00626 strlcpy(n,name.c_str(),MAXBUF);
00627 int res = ReadConf(cache,t,n,index,val);
00628 if (!res)
00629 {
00630 this->error = CONF_VALUE_NOT_FOUND;
00631 return false;
00632 }
00633 std::string s = val;
00634 return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
00635 }
00636
00637 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
00638 {
00639 char val[MAXBUF];
00640 char t[MAXBUF];
00641 char n[MAXBUF];
00642 strlcpy(t,tag.c_str(),MAXBUF);
00643 strlcpy(n,name.c_str(),MAXBUF);
00644 int res = ReadConf(cache,t,n,index,val);
00645 if (!res)
00646 {
00647 this->error = CONF_VALUE_NOT_FOUND;
00648 return 0;
00649 }
00650 for (int i = 0; i < strlen(val); i++)
00651 {
00652 if (!isdigit(val[i]))
00653 {
00654 this->error = CONF_NOT_A_NUMBER;
00655 return 0;
00656 }
00657 }
00658 if ((needs_unsigned) && (atoi(val)<0))
00659 {
00660 this->error = CONF_NOT_UNSIGNED;
00661 return 0;
00662 }
00663 return atoi(val);
00664 }
00665
00666 long ConfigReader::GetError()
00667 {
00668 long olderr = this->error;
00669 this->error = 0;
00670 return olderr;
00671 }
00672
00673 void ConfigReader::DumpErrors(bool bail, userrec* user)
00674 {
00675 if (bail)
00676 {
00677 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
00678 exit(0);
00679 }
00680 else
00681 {
00682 char dataline[1024];
00683 if (user)
00684 {
00685 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
00686 while (!errorlog->eof())
00687 {
00688 errorlog->getline(dataline,1024);
00689 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
00690 }
00691 }
00692 else
00693 {
00694 WriteOpers("There were errors in the configuration file:",user->nick);
00695 while (!errorlog->eof())
00696 {
00697 errorlog->getline(dataline,1024);
00698 WriteOpers(dataline);
00699 }
00700 }
00701 return;
00702 }
00703 }
00704
00705
00706 int ConfigReader::Enumerate(std::string tag)
00707 {
00708 return EnumConf(cache,tag.c_str());
00709 }
00710
00711 int ConfigReader::EnumerateValues(std::string tag, int index)
00712 {
00713 return EnumValues(cache, tag.c_str(), index);
00714 }
00715
00716 bool ConfigReader::Verify()
00717 {
00718 return this->readerror;
00719 }
00720
00721
00722 FileReader::FileReader(std::string filename)
00723 {
00724 file_cache c;
00725 readfile(c,filename.c_str());
00726 this->fc = c;
00727 }
00728
00729 FileReader::FileReader()
00730 {
00731 }
00732
00733 void FileReader::LoadFile(std::string filename)
00734 {
00735 file_cache c;
00736 readfile(c,filename.c_str());
00737 this->fc = c;
00738 }
00739
00740
00741 FileReader::~FileReader()
00742 {
00743 }
00744
00745 bool FileReader::Exists()
00746 {
00747 if (fc.size() == 0)
00748 {
00749 return(false);
00750 }
00751 else
00752 {
00753 return(true);
00754 }
00755 }
00756
00757 std::string FileReader::GetLine(int x)
00758 {
00759 if ((x<0) || (x>fc.size()))
00760 return "";
00761 return fc[x];
00762 }
00763
00764 int FileReader::FileSize()
00765 {
00766 return fc.size();
00767 }
00768
00769
00770 std::vector<Module*> modules(255);
00771 std::vector<ircd_module*> factory(255);
00772
00773 int MODCOUNT = -1;
00774
00775