1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 /* Now with added unF! ;) */
21 #include "inspircd_config.h"
23 #include "inspircd_io.h"
24 #include "inspircd_util.h"
27 #include <sys/errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/utsname.h>
33 #include <ext/hash_map>
55 #include "inspstring.h"
57 #include "helperfuncs.h"
59 #include "socketengine.h"
60 #include "userprocess.h"
65 int WHOWAS_STALE = 48; // default WHOWAS Entries last 2 days before they go 'stale'
66 int WHOWAS_MAX = 100; // default 100 people maximum in the WHOWAS list
68 time_t startup_time = time(NULL);
70 extern std::vector<Module*> modules;
71 extern std::vector<ircd_module*> factory;
73 std::vector<InspSocket*> module_sockets;
76 int openSockfd[MAXSOCKS];
77 struct sockaddr_in client,server;
80 extern InspSocket* socket_ref[65535];
82 time_t TIME = time(NULL), OLDTIME = time(NULL);
84 SocketEngine* SE = NULL;
86 // This table references users by file descriptor.
87 // its an array to make it VERY fast, as all lookups are referenced
88 // by an integer, meaning there is no need for a scan/search operation.
89 userrec* fd_ref_table[65536];
91 serverstats* stats = new serverstats;
92 Server* MyServer = new Server;
93 ServerConfig *Config = new ServerConfig;
98 command_table cmdlist;
100 servernamelist servernames;
101 int boundPortCount = 0;
102 int portCount = 0, ports[MAXSOCKS];
103 std::vector<userrec*> all_opers;
107 void AddOper(userrec* user)
109 log(DEBUG,"Oper added to optimization list");
110 all_opers.push_back(user);
113 void AddServerName(std::string servername)
115 log(DEBUG,"Adding server name: %s",servername.c_str());
116 for (servernamelist::iterator a = servernames.begin(); a < servernames.end(); a++)
118 if (*a == servername)
121 servernames.push_back(servername);
124 const char* FindServerNamePtr(std::string servername)
126 for (servernamelist::iterator a = servernames.begin(); a < servernames.end(); a++)
128 if (*a == servername)
131 AddServerName(servername);
132 return FindServerNamePtr(servername);
135 void DeleteOper(userrec* user)
137 for (std::vector<userrec*>::iterator a = all_opers.begin(); a < all_opers.end(); a++)
141 log(DEBUG,"Oper removed from optimization list");
148 std::string GetRevision()
150 /* w00t got me to replace a bunch of strtok_r
151 * with something nicer, so i did this. Its the
152 * same thing really, only in C++. It places the
153 * text into a std::stringstream which is a readable
154 * and writeable buffer stream, and then pops two
155 * words off it, space delimited. Because it reads
156 * into the same variable twice, the first word
157 * is discarded, and the second one returned.
159 std::stringstream Revision("$Revision$");
161 Revision >> single >> single;
166 /* This function pokes and hacks at a parameter list like the following:
168 * PART #winbot,#darkgalaxy :m00!
170 * to turn it into a series of individual calls like this:
173 * PART #darkgalaxy :m00!
175 * The seperate calls are sent to a callback function provided by the caller
176 * (the caller will usually call itself recursively). The callback function
177 * must be a command handler. Calling this function on a line with no list causes
178 * no action to be taken. You must provide a starting and ending parameter number
179 * where the range of the list can be found, useful if you have a terminating
180 * parameter as above which is actually not part of the list, or parameters
181 * before the actual list as well. This code is used by many functions which
182 * can function as "one to list" (see the RFC) */
184 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
189 char blog[32][MAXBUF];
190 char blog2[32][MAXBUF];
191 int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
195 for (int i = 0; i <32; i++)
198 for (int i = 0; i <32; i++)
202 for (int i = 0; i <10; i++)
211 if (pcnt > 1) /* we have a key to copy */
213 strlcpy(keystr,parameters[1],MAXBUF);
217 if (!parameters[start])
221 if (!strchr(parameters[start],','))
226 for (int i = start; i <= end; i++)
230 strlcat(plist,parameters[i],MAXBUF);
238 for (int i = 0; i < t; i++)
243 strlcpy(blog[j++],param,MAXBUF);
247 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
252 strlcpy(blog[j++],param,MAXBUF);
255 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
260 if ((joins) && (keystr))
262 if (strchr(keystr,','))
267 for (int i = 0; i < t2; i++)
269 if (keystr[i] == ',')
272 strlcpy(blog2[j++],param,MAXBUF);
276 strlcpy(blog2[j++],param,MAXBUF);
281 for (j = 0; j < total; j++)
287 for (q = end; q < pcnt-1; q++)
291 pars[q-end+1] = parameters[q+1];
294 if ((joins) && (parameters[1]))
305 /* repeatedly call the function with the hacked parameter list */
306 if ((joins) && (pcnt > 1))
310 // pars[1] already set up and containing key from blog2[j]
315 pars[1] = parameters[1];
321 fn(pars,pcnt-(end-start),u);
330 void kill_link(userrec *user,const char* r)
332 user_hash::iterator iter = clientlist.find(user->nick);
336 strncpy(reason,r,MAXBUF);
338 if (strlen(reason)>MAXQUIT)
340 reason[MAXQUIT-1] = '\0';
343 log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
344 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
345 log(DEBUG,"closing fd %lu",(unsigned long)user->fd);
347 if (user->registered == 7) {
348 FOREACH_MOD OnUserQuit(user,reason);
349 WriteCommonExcept(user,"QUIT :%s",reason);
352 user->FlushWriteBuf();
354 FOREACH_MOD OnUserDisconnect(user);
358 FOREACH_MOD OnRawSocketClose(user->fd);
363 // this must come before the WriteOpers so that it doesnt try to fill their buffer with anything
364 // if they were an oper with +s.
365 if (user->registered == 7) {
366 purge_empty_chans(user);
367 // fix by brain: only show local quits because we only show local connects (it just makes SENSE)
369 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
373 if (iter != clientlist.end())
375 log(DEBUG,"deleting user hash value %lu",(unsigned long)user);
377 fd_ref_table[user->fd] = NULL;
378 clientlist.erase(iter);
383 void kill_link_silent(userrec *user,const char* r)
385 user_hash::iterator iter = clientlist.find(user->nick);
389 strncpy(reason,r,MAXBUF);
391 if (strlen(reason)>MAXQUIT)
393 reason[MAXQUIT-1] = '\0';
396 log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
397 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
398 log(DEBUG,"closing fd %lu",(unsigned long)user->fd);
400 user->FlushWriteBuf();
402 if (user->registered == 7) {
403 FOREACH_MOD OnUserQuit(user,reason);
404 WriteCommonExcept(user,"QUIT :%s",reason);
407 FOREACH_MOD OnUserDisconnect(user);
411 FOREACH_MOD OnRawSocketClose(user->fd);
416 if (user->registered == 7) {
417 purge_empty_chans(user);
420 if (iter != clientlist.end())
422 log(DEBUG,"deleting user hash value %lu",(unsigned long)user);
424 fd_ref_table[user->fd] = NULL;
425 clientlist.erase(iter);
431 int main(int argc, char** argv)
435 log(DEBUG,"*** InspIRCd starting up!");
436 if (!FileExists(CONFIG_FILE))
438 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
439 log(DEFAULT,"main: no config");
440 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
444 for (int i = 1; i < argc; i++)
446 if (!strcmp(argv[i],"-nofork")) {
447 Config->nofork = true;
449 if (!strcmp(argv[i],"-wait")) {
452 if (!strcmp(argv[i],"-nolimit")) {
453 Config->unlimitcore = true;
458 strlcpy(Config->MyExecutable,argv[0],MAXBUF);
460 // initialize the lowercase mapping table
461 for (unsigned int cn = 0; cn < 256; cn++)
463 // lowercase the uppercase chars
464 for (unsigned int cn = 65; cn < 91; cn++)
465 lowermap[cn] = tolower(cn);
466 // now replace the specific chars for scandanavian comparison
467 lowermap[(unsigned)'['] = '{';
468 lowermap[(unsigned)']'] = '}';
469 lowermap[(unsigned)'\\'] = '|';
471 if (InspIRCd(argv,argc) == ERROR)
473 log(DEFAULT,"main: daemon function bailed");
474 printf("ERROR: could not initialise. Shutting down.\n");
481 template<typename T> inline string ConvToStr(const T &in)
484 if (!(tmp << in)) return string();
488 /* re-allocates a nick in the user_hash after they change nicknames,
489 * returns a pointer to the new user as it may have moved */
491 userrec* ReHashNick(char* Old, char* New)
493 //user_hash::iterator newnick;
494 user_hash::iterator oldnick = clientlist.find(Old);
496 log(DEBUG,"ReHashNick: %s %s",Old,New);
498 if (!strcasecmp(Old,New))
500 log(DEBUG,"old nick is new nick, skipping");
501 return oldnick->second;
504 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
506 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
508 userrec* olduser = oldnick->second;
509 clientlist[New] = olduser;
510 clientlist.erase(oldnick);
512 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
514 return clientlist[New];
517 /* adds or updates an entry in the whowas list */
518 void AddWhoWas(userrec* u)
520 whowas_hash::iterator iter = whowas.find(u->nick);
521 WhoWasUser *a = new WhoWasUser();
522 strlcpy(a->nick,u->nick,NICKMAX);
523 strlcpy(a->ident,u->ident,IDENTMAX);
524 strlcpy(a->dhost,u->dhost,160);
525 strlcpy(a->host,u->host,160);
526 strlcpy(a->fullname,u->fullname,MAXGECOS);
527 strlcpy(a->server,u->server,256);
528 a->signon = u->signon;
530 /* MAX_WHOWAS: max number of /WHOWAS items
531 * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
532 * can be replaced by a newer one
535 if (iter == whowas.end())
537 if (whowas.size() >= (unsigned)WHOWAS_MAX)
539 for (whowas_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
541 // 3600 seconds in an hour ;)
542 if ((i->second->signon)<(TIME-(WHOWAS_STALE*3600)))
544 // delete the old one
545 if (i->second) delete i->second;
546 // replace with new one
548 log(DEBUG,"added WHOWAS entry, purged an old record");
552 // no space left and user doesnt exist. Don't leave ram in use!
553 log(DEBUG,"Not able to update whowas (list at WHOWAS_MAX entries and trying to add new?), freeing excess ram");
558 log(DEBUG,"added fresh WHOWAS entry");
564 log(DEBUG,"updated WHOWAS entry");
565 if (iter->second) delete iter->second;
571 void* dns_task(void* arg)
573 userrec* u = (userrec*)arg;
574 log(DEBUG,"DNS thread for user %s",u->nick);
579 if (dns1.ReverseLookup(u->ip))
581 log(DEBUG,"DNS Step 1");
582 while (!dns1.HasResult())
586 host = dns1.GetResult();
589 log(DEBUG,"DNS Step 2: '%s'",host.c_str());
590 if (dns2.ForwardLookup(host))
592 while (!dns2.HasResult())
596 ip = dns2.GetResultIP();
597 log(DEBUG,"DNS Step 3 '%s'(%d) '%s'(%d)",ip.c_str(),ip.length(),u->ip,strlen(u->ip));
598 if (ip == std::string(u->ip))
600 log(DEBUG,"DNS Step 4");
601 if (host.length() < 160)
603 log(DEBUG,"DNS Step 5");
604 strcpy(u->host,host.c_str());
605 strcpy(u->dhost,host.c_str());
616 /* add a client connection to the sockets list */
617 void AddClient(int socket, char* host, int port, bool iscached, char* ip)
621 user_hash::iterator iter;
623 tempnick = ConvToStr(socket) + "-unknown";
624 sprintf(tn2,"%lu-unknown",(unsigned long)socket);
626 iter = clientlist.find(tempnick);
629 // as these nicknames are 'RFC impossible', we can be sure nobody is going to be
630 // using one as a registered connection. As theyre per fd, we can also safely assume
631 // that we wont have collisions. Therefore, if the nick exists in the list, its only
632 // used by a dead socket, erase the iterator so that the new client may reclaim it.
633 // this was probably the cause of 'server ignores me when i hammer it with reconnects'
634 // issue in earlier alphas/betas
635 if (iter != clientlist.end())
637 userrec* goner = iter->second;
639 clientlist.erase(iter);
643 * It is OK to access the value here this way since we know
644 * it exists, we just created it above.
646 * At NO other time should you access a value in a map or a
649 clientlist[tempnick] = new userrec();
652 log(DEBUG,"AddClient: %lu %s %d %s",(unsigned long)socket,host,port,ip);
654 clientlist[tempnick]->fd = socket;
655 strlcpy(clientlist[tempnick]->nick, tn2,NICKMAX);
656 strlcpy(clientlist[tempnick]->host, host,160);
657 strlcpy(clientlist[tempnick]->dhost, host,160);
658 clientlist[tempnick]->server = (char*)FindServerNamePtr(Config->ServerName);
659 strlcpy(clientlist[tempnick]->ident, "unknown",IDENTMAX);
660 clientlist[tempnick]->registered = 0;
661 clientlist[tempnick]->signon = TIME + Config->dns_timeout;
662 clientlist[tempnick]->lastping = 1;
663 clientlist[tempnick]->port = port;
664 strlcpy(clientlist[tempnick]->ip,ip,16);
666 // set the registration timeout for this user
667 unsigned long class_regtimeout = 90;
669 long class_threshold = 5;
670 long class_sqmax = 262144; // 256kb
671 long class_rqmax = 4096; // 4k
673 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
675 if (match(clientlist[tempnick]->host,i->host) && (i->type == CC_ALLOW))
677 class_regtimeout = (unsigned long)i->registration_timeout;
678 class_flood = i->flood;
679 clientlist[tempnick]->pingmax = i->pingtime;
680 class_threshold = i->threshold;
681 class_sqmax = i->sendqmax;
682 class_rqmax = i->recvqmax;
687 clientlist[tempnick]->nping = TIME+clientlist[tempnick]->pingmax + Config->dns_timeout;
688 clientlist[tempnick]->timeout = TIME+class_regtimeout;
689 clientlist[tempnick]->flood = class_flood;
690 clientlist[tempnick]->threshold = class_threshold;
691 clientlist[tempnick]->sendqmax = class_sqmax;
692 clientlist[tempnick]->recvqmax = class_rqmax;
697 for (int i = 0; i < MAXCHANS; i++)
698 clientlist[tempnick]->chans.push_back(a);
700 if (clientlist.size() > Config->SoftLimit)
702 kill_link(clientlist[tempnick],"No more connections allowed");
706 if (clientlist.size() >= MAXCLIENTS)
708 kill_link(clientlist[tempnick],"No more connections allowed");
712 // this is done as a safety check to keep the file descriptors within range of fd_ref_table.
713 // its a pretty big but for the moment valid assumption:
714 // file descriptors are handed out starting at 0, and are recycled as theyre freed.
715 // therefore if there is ever an fd over 65535, 65536 clients must be connected to the
716 // irc server at once (or the irc server otherwise initiating this many connections, files etc)
717 // which for the time being is a physical impossibility (even the largest networks dont have more
718 // than about 10,000 users on ONE server!)
719 if ((unsigned)socket > 65534)
721 kill_link(clientlist[tempnick],"Server is full");
726 char* e = matches_exception(ip);
729 char* r = matches_zline(ip);
733 snprintf(reason,MAXBUF,"Z-Lined: %s",r);
734 kill_link(clientlist[tempnick],reason);
738 fd_ref_table[socket] = clientlist[tempnick];
739 SE->AddFd(socket,true,X_ESTAB_CLIENT);
742 /* shows the message of the day, and any other on-logon stuff */
743 void FullConnectUser(userrec* user)
745 stats->statsConnects++;
746 user->idle_lastmsg = TIME;
747 log(DEBUG,"ConnectUser: %s",user->nick);
749 if ((strcmp(Passwd(user),"")) && (!user->haspassed))
751 kill_link(user,"Invalid password");
756 kill_link(user,"Unauthorised connection");
760 char match_against[MAXBUF];
761 snprintf(match_against,MAXBUF,"%s@%s",user->ident,user->host);
762 char* e = matches_exception(match_against);
765 char* r = matches_gline(match_against);
769 snprintf(reason,MAXBUF,"G-Lined: %s",r);
770 kill_link_silent(user,reason);
773 r = matches_kline(user->host);
777 snprintf(reason,MAXBUF,"K-Lined: %s",r);
778 kill_link_silent(user,reason);
784 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Config->Network);
785 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Config->Network,user->nick,user->ident,user->host);
786 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,Config->ServerName,VERSION);
787 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
788 WriteServ(user->fd,"004 %s %s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,Config->ServerName,VERSION);
789 // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
791 v << "WALLCHOPS MODES=13 CHANTYPES=# PREFIX=(ohv)@%+ MAP SAFELIST MAXCHANNELS=" << MAXCHANS;
792 v << " MAXBANS=60 NICKLEN=" << NICKMAX;
793 v << " TOPICLEN=" << MAXTOPIC << " KICKLEN=" << MAXKICK << " MAXTARGETS=20 AWAYLEN=" << MAXAWAY << " CHANMODES=ohvb,k,l,psmnti NETWORK=";
794 v << Config->Network;
795 std::string data005 = v.str();
796 FOREACH_MOD On005Numeric(data005);
797 // anfl @ #ratbox, efnet reminded me that according to the RFC this cant contain more than 13 tokens per line...
798 // so i'd better split it :)
799 std::stringstream out(data005);
800 std::string token = "";
801 std::string line5 = "";
802 int token_counter = 0;
806 line5 = line5 + token + " ";
808 if ((token_counter >= 13) || (out.eof() == true))
810 WriteServ(user->fd,"005 %s %s:are supported by this server",user->nick,line5.c_str());
817 // fix 3 by brain, move registered = 7 below these so that spurious modes and host changes dont go out
818 // onto the network and produce 'fake direction'
819 FOREACH_MOD OnUserConnect(user);
820 FOREACH_MOD OnGlobalConnect(user);
821 user->registered = 7;
822 WriteOpers("*** Client connecting on port %lu: %s!%s@%s [%s]",(unsigned long)user->port,user->nick,user->ident,user->host,user->ip);
826 /* shows the message of the day, and any other on-logon stuff */
827 void ConnectUser(userrec *user)
829 // dns is already done, things are fast. no need to wait for dns to complete just pass them straight on
830 if ((user->dns_done) && (user->registered >= 3) && (AllModulesReportReady(user)))
832 FullConnectUser(user);
836 std::string GetVersionString()
838 char versiondata[MAXBUF];
840 char dnsengine[] = "multithread";
842 char dnsengine[] = "singlethread";
844 snprintf(versiondata,MAXBUF,"%s Rev. %s %s :%s [FLAGS=%lu,%s,%s]",VERSION,GetRevision().c_str(),Config->ServerName,SYSTEM,(unsigned long)OPTIMISATION,SE->GetName().c_str(),dnsengine);
848 void handle_version(char **parameters, int pcnt, userrec *user)
850 WriteServ(user->fd,"351 %s :%s",user->nick,GetVersionString().c_str());
854 bool is_valid_cmd(const char* commandname, int pcnt, userrec * user)
856 for (unsigned int i = 0; i < cmdlist.size(); i++)
858 if (!strcasecmp(cmdlist[i].command,commandname))
860 if (cmdlist[i].handler_function)
862 if ((pcnt>=cmdlist[i].min_params) && (strcasecmp(cmdlist[i].source,"<core>")))
864 if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
866 if (cmdlist[i].flags_needed)
868 if ((user->HasPermission((char*)commandname)) || (is_uline(user->server)))
886 // calls a handler function for a command
888 void call_handler(const char* commandname,char **parameters, int pcnt, userrec *user)
890 for (unsigned int i = 0; i < cmdlist.size(); i++)
892 if (!strcasecmp(cmdlist[i].command,commandname))
894 if (cmdlist[i].handler_function)
896 if (pcnt>=cmdlist[i].min_params)
898 if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
900 if (cmdlist[i].flags_needed)
902 if ((user->HasPermission((char*)commandname)) || (is_uline(user->server)))
904 cmdlist[i].handler_function(parameters,pcnt,user);
909 cmdlist[i].handler_function(parameters,pcnt,user);
919 void force_nickchange(userrec* user,const char* newnick)
926 FOREACH_RESULT(OnUserPreNick(user,newnick));
928 stats->statsCollisions++;
929 kill_link(user,"Nickname collision");
932 if (matches_qline(newnick))
934 stats->statsCollisions++;
935 kill_link(user,"Nickname collision");
943 strncpy(nick,newnick,MAXBUF);
945 if (user->registered == 7)
949 handle_nick(pars,1,user);
955 int process_parameters(char **command_p,char *parameters)
958 int q = strlen(parameters);
961 /* no parameters, command_p invalid! */
964 if (parameters[0] == ':')
966 command_p[0] = parameters+1;
971 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
973 /* only one parameter */
974 command_p[0] = parameters;
975 if (parameters[0] == ':')
977 if (strchr(parameters,' ') != NULL)
985 command_p[j++] = parameters;
986 for (int i = 0; i <= q; i++)
988 if (parameters[i] == ' ')
990 command_p[j++] = parameters+i+1;
991 parameters[i] = '\0';
992 if (command_p[j-1][0] == ':')
994 *command_p[j-1]++; /* remove dodgy ":" */
996 /* parameter like this marks end of the sequence */
1000 return j; /* returns total number of items in the list */
1003 void process_command(userrec *user, char* cmd)
1007 char *command_p[127];
1008 char p[MAXBUF], temp[MAXBUF];
1009 int j, items, cmd_found;
1011 for (int i = 0; i < 127; i++)
1012 command_p[i] = NULL;
1027 int total_params = 0;
1030 for (unsigned int q = 0; q < strlen(cmd)-1; q++)
1032 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
1035 // found a 'trailing', we dont count them after this.
1043 // another phidjit bug...
1044 if (total_params > 126)
1046 *(strchr(cmd,' ')) = '\0';
1047 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
1051 strlcpy(temp,cmd,MAXBUF);
1053 std::string tmp = cmd;
1054 for (int i = 0; i <= MODCOUNT; i++)
1056 std::string oldtmp = tmp;
1057 modules[i]->OnServerRaw(tmp,true,user);
1060 log(DEBUG,"A Module changed the input string!");
1061 log(DEBUG,"New string: %s",tmp.c_str());
1062 log(DEBUG,"Old string: %s",oldtmp.c_str());
1066 strlcpy(cmd,tmp.c_str(),MAXBUF);
1067 strlcpy(temp,cmd,MAXBUF);
1069 if (!strchr(cmd,' '))
1071 /* no parameters, lets skip the formalities and not chop up
1073 log(DEBUG,"About to preprocess command with no params");
1075 command_p[0] = NULL;
1077 for (unsigned int i = 0; i <= strlen(cmd); i++)
1079 cmd[i] = toupper(cmd[i]);
1087 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
1088 for (unsigned int i = 0; i < strlen(temp); i++)
1090 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
1096 /* split the full string into a command plus parameters */
1100 if (strchr(cmd,' '))
1102 for (unsigned int i = 0; i <= strlen(cmd); i++)
1104 /* capitalise the command ONLY, leave params intact */
1105 cmd[i] = toupper(cmd[i]);
1106 /* are we nearly there yet?! :P */
1110 parameters = cmd+i+1;
1118 for (unsigned int i = 0; i <= strlen(cmd); i++)
1120 cmd[i] = toupper(cmd[i]);
1127 if (strlen(command)>MAXCOMMAND)
1129 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
1133 for (unsigned int x = 0; x < strlen(command); x++)
1135 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
1137 if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
1139 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
1141 stats->statsUnknown++;
1142 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
1149 for (unsigned int i = 0; i != cmdlist.size(); i++)
1151 if (cmdlist[i].command[0])
1153 if (strlen(command)>=(strlen(cmdlist[i].command))) if (!strncmp(command, cmdlist[i].command,MAXCOMMAND))
1159 items = process_parameters(command_p,parameters);
1164 command_p[0] = NULL;
1170 command_p[0] = NULL;
1175 /* activity resets the ping pending timer */
1176 user->nping = TIME + user->pingmax;
1177 if ((items) < cmdlist[i].min_params)
1179 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
1180 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
1183 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
1185 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
1186 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
1190 if ((cmdlist[i].flags_needed) && (!user->HasPermission(command)))
1192 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
1193 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
1197 /* if the command isnt USER, PASS, or NICK, and nick is empty,
1199 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
1201 if ((!isnick(user->nick)) || (user->registered != 7))
1203 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
1204 WriteServ(user->fd,"451 %s :You have not registered",command);
1208 if ((user->registered == 7) && (!strchr(user->modes,'o')))
1210 std::stringstream dcmds(Config->DisabledCommands);
1211 while (!dcmds.eof())
1213 std::string thiscmd;
1215 if (!strcasecmp(thiscmd.c_str(),command))
1217 // command is disabled!
1218 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
1223 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
1225 if (cmdlist[i].handler_function)
1228 /* ikky /stats counters */
1231 cmdlist[i].use_count++;
1232 cmdlist[i].total_bytes+=strlen(temp);
1236 FOREACH_RESULT(OnPreCommand(command,command_p,items,user));
1237 if (MOD_RESULT == 1) {
1241 /* WARNING: nothing may come after the
1242 * command handler call, as the handler
1243 * may free the user structure! */
1245 cmdlist[i].handler_function(command_p,items,user);
1251 WriteServ(user->fd,"451 %s :You have not registered",command);
1259 if ((!cmd_found) && (user))
1261 stats->statsUnknown++;
1262 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
1266 bool removecommands(const char* source)
1268 bool go_again = true;
1272 for (std::deque<command_t>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
1274 if (!strcmp(i->source,source))
1276 log(DEBUG,"removecommands(%s) Removing dependent command: %s",i->source,i->command);
1287 void process_buffer(const char* cmdbuf,userrec *user)
1291 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
1297 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
1304 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
1306 strlcpy(cmd,cmdbuf,MAXBUF);
1311 int sl = strlen(cmd)-1;
1312 if ((cmd[sl] == 13) || (cmd[sl] == 10))
1317 if ((cmd[sl] == 13) || (cmd[sl] == 10))
1322 while (cmd[sl] == ' ') // strip trailing spaces
1332 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
1334 if ((user) && (cmd))
1336 process_command(user,cmd);
1340 char MODERR[MAXBUF];
1347 void erase_factory(int j)
1350 for (std::vector<ircd_module*>::iterator t = factory.begin(); t != factory.end(); t++)
1355 factory.push_back(NULL);
1362 void erase_module(int j)
1365 for (std::vector<Module*>::iterator m = modules.begin(); m!= modules.end(); m++)
1371 modules.push_back(NULL);
1377 for (std::vector<std::string>::iterator v = Config->module_names.begin(); v != Config->module_names.end(); v++)
1381 Config->module_names.erase(v);
1389 bool UnloadModule(const char* filename)
1391 std::string filename_str = filename;
1392 for (unsigned int j = 0; j != Config->module_names.size(); j++)
1394 if (Config->module_names[j] == filename_str)
1396 if (modules[j]->GetVersion().Flags & VF_STATIC)
1398 log(DEFAULT,"Failed to unload STATIC module %s",filename);
1399 snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)");
1402 /* Give the module a chance to tidy out all its metadata */
1403 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
1405 modules[j]->OnCleanup(TYPE_CHANNEL,c->second);
1407 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
1409 modules[j]->OnCleanup(TYPE_USER,u->second);
1411 FOREACH_MOD OnUnloadModule(modules[j],Config->module_names[j]);
1413 log(DEBUG,"Deleting module...");
1415 log(DEBUG,"Erasing module entry...");
1417 log(DEBUG,"Removing dependent commands...");
1418 removecommands(filename);
1419 log(DEFAULT,"Module %s unloaded",filename);
1424 log(DEFAULT,"Module %s is not loaded, cannot unload it!",filename);
1425 snprintf(MODERR,MAXBUF,"Module not loaded");
1429 bool LoadModule(const char* filename)
1431 char modfile[MAXBUF];
1433 snprintf(modfile,MAXBUF,"%s",filename);
1435 snprintf(modfile,MAXBUF,"%s/%s",Config->ModPath,filename);
1437 std::string filename_str = filename;
1439 if (!DirValid(modfile))
1441 log(DEFAULT,"Module %s is not within the modules directory.",modfile);
1442 snprintf(MODERR,MAXBUF,"Module %s is not within the modules directory.",modfile);
1446 log(DEBUG,"Loading module: %s",modfile);
1448 if (FileExists(modfile))
1451 for (unsigned int j = 0; j < Config->module_names.size(); j++)
1453 if (Config->module_names[j] == filename_str)
1455 log(DEFAULT,"Module %s is already loaded, cannot load a module twice!",modfile);
1456 snprintf(MODERR,MAXBUF,"Module already loaded");
1460 ircd_module* a = new ircd_module(modfile);
1461 factory[MODCOUNT+1] = a;
1462 if (factory[MODCOUNT+1]->LastError())
1464 log(DEFAULT,"Unable to load %s: %s",modfile,factory[MODCOUNT+1]->LastError());
1465 snprintf(MODERR,MAXBUF,"Loader/Linker error: %s",factory[MODCOUNT+1]->LastError());
1469 if (factory[MODCOUNT+1]->factory)
1471 Module* m = factory[MODCOUNT+1]->factory->CreateModule(MyServer);
1472 modules[MODCOUNT+1] = m;
1473 /* save the module and the module's classfactory, if
1474 * this isnt done, random crashes can occur :/ */
1475 Config->module_names.push_back(filename);
1479 log(DEFAULT,"Unable to load %s",modfile);
1480 snprintf(MODERR,MAXBUF,"Factory function failed!");
1487 log(DEFAULT,"InspIRCd: startup: Module Not Found %s",modfile);
1488 snprintf(MODERR,MAXBUF,"Module file could not be found");
1493 FOREACH_MOD OnLoadModule(modules[MODCOUNT],filename_str);
1499 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
1500 int clientportcount = 0;
1501 for (int count = 0; count < Config->ConfValueEnum("bind",&Config->config_f); count++)
1503 Config->ConfValue("bind","port",count,configToken,&Config->config_f);
1504 Config->ConfValue("bind","address",count,Addr,&Config->config_f);
1505 Config->ConfValue("bind","type",count,Type,&Config->config_f);
1506 if (strcmp(Type,"servers"))
1508 // modules handle server bind types now,
1509 // its not a typo in the strcmp.
1510 ports[clientportcount] = atoi(configToken);
1511 strlcpy(Config->addrs[clientportcount],Addr,256);
1513 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
1516 portCount = clientportcount;
1518 for (int count = 0; count < portCount; count++)
1520 if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
1522 log(DEBUG,"InspIRCd: startup: bad fd %lu",(unsigned long)openSockfd[boundPortCount]);
1525 if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],Config->addrs[count]) == ERROR)
1527 log(DEFAULT,"InspIRCd: startup: failed to bind port %lu",(unsigned long)ports[count]);
1529 else /* well we at least bound to one socket so we'll continue */
1535 /* if we didn't bind to anything then abort */
1536 if (!boundPortCount)
1538 log(DEFAULT,"InspIRCd: startup: no ports bound, bailing!");
1539 printf("\nERROR: Was not able to bind any of %lu ports! Please check your configuration.\n\n", (unsigned long)portCount);
1543 return boundPortCount;
1546 int InspIRCd(char** argv, int argc)
1548 bool expire_run = false;
1549 std::vector<int> activefds;
1553 InspSocket* s = NULL;
1554 InspSocket* s_del = NULL;
1556 unsigned int numberactive;
1557 sockaddr_in sock_us; // our port number
1558 socklen_t uslen; // length of our port number
1560 /* Beta 7 moved all this stuff out of the main function
1561 * into smaller sub-functions, much tidier -- Brain
1563 OpenLog(argv, argc);
1564 Config->ClearStack();
1565 Config->Read(true,NULL);
1567 SetupCommandTable();
1568 AddServerName(Config->ServerName);
1570 boundPortCount = BindPorts();
1573 startup_time = time(NULL);
1575 if (!Config->nofork)
1577 if (DaemonSeed() == ERROR)
1579 printf("ERROR: could not go into daemon mode. Shutting down.\n");
1584 /* Because of limitations in kqueue on freebsd, we must fork BEFORE we
1585 * initialize the socket engine.
1587 SE = new SocketEngine();
1589 /* We must load the modules AFTER initializing the socket engine, now */
1592 printf("\nInspIRCd is now running!\n");
1593 if (!Config->nofork)
1595 freopen("/dev/null","w",stdout);
1596 freopen("/dev/null","w",stderr);
1599 /* Add the listening sockets used for client inbound connections
1600 * to the socket engine
1602 for (int count = 0; count < portCount; count++)
1603 SE->AddFd(openSockfd[count],true,X_LISTEN);
1605 WritePID(Config->PID);
1607 /* main loop, this never returns */
1610 /* time() seems to be a pretty expensive syscall, so avoid calling it too much.
1611 * Once per loop iteration is pleanty.
1616 /* Run background module timers every few seconds
1617 * (the docs say modules shouldnt rely on accurate
1618 * timing using this event, so we dont have to
1619 * time this exactly).
1621 if (((TIME % 8) == 0) && (!expire_run))
1624 FOREACH_MOD OnBackgroundTimer(TIME);
1628 if ((TIME % 8) == 1)
1631 /* Once a second, do the background processing */
1632 if (TIME != OLDTIME)
1633 while (DoBackgroundUserStuff(TIME));
1635 /* Call the socket engine to wait on the active
1636 * file descriptors. The socket engine has everything's
1637 * descriptors in its list... dns, modules, users,
1638 * servers... so its nice and easy, just one call.
1640 SE->Wait(activefds);
1643 * Now process each of the fd's. For users, we have a fast
1644 * lookup table which can find a user by file descriptor, so
1645 * processing them by fd isnt expensive. If we have a lot of
1646 * listening ports or module sockets though, things could get
1649 numberactive = activefds.size();
1650 for (unsigned int activefd = 0; activefd < numberactive; activefd++)
1652 int socket_type = SE->GetType(activefds[activefd]);
1653 switch (socket_type)
1655 case X_ESTAB_CLIENT:
1657 cu = fd_ref_table[activefds[activefd]];
1663 case X_ESTAB_MODULE:
1665 /* Process module-owned sockets.
1666 * Modules are encouraged to inherit their sockets from
1667 * InspSocket so we can process them neatly like this.
1669 s = socket_ref[activefds[activefd]];
1671 if ((s) && (!s->Poll()))
1673 log(DEBUG,"Socket poll returned false, close and bail");
1674 SE->DelFd(s->GetFd());
1675 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
1677 s_del = (InspSocket*)*a;
1678 if ((s_del) && (s_del->GetFd() == activefds[activefd]))
1680 module_sockets.erase(a);
1692 /* When we are using single-threaded dns,
1693 * the sockets for dns end up in our mainloop.
1694 * When we are using multi-threaded dns,
1695 * each thread has its own basic poll() loop
1696 * within it, making them 'fire and forget'
1697 * and independent of the mainloop.
1699 #ifndef THREADED_DNS
1700 dns_poll(activefds[activefd]);
1706 /* It's a listener */
1707 uslen = sizeof(sock_us);
1708 length = sizeof(client);
1709 incomingSockfd = accept (activefds[activefd],(struct sockaddr*)&client,&length);
1710 if (!getsockname(incomingSockfd,(sockaddr*)&sock_us,&uslen))
1712 in_port = ntohs(sock_us.sin_port);
1713 log(DEBUG,"Accepted socket %d",incomingSockfd);
1714 target = (char*)inet_ntoa(client.sin_addr);
1715 /* Years and years ago, we used to resolve here
1716 * using gethostbyaddr(). That is sucky and we
1717 * don't do that any more...
1719 if (incomingSockfd >= 0)
1721 FOREACH_MOD OnRawSocketAccept(incomingSockfd, target, in_port);
1722 stats->statsAccept++;
1723 AddClient(incomingSockfd, target, in_port, false, target);
1724 log(DEBUG,"Adding client on port %lu fd=%lu",(unsigned long)in_port,(unsigned long)incomingSockfd);
1728 WriteOpers("*** WARNING: accept() failed on port %lu (%s)",(unsigned long)in_port,target);
1729 log(DEBUG,"accept failed: %lu",(unsigned long)in_port);
1730 stats->statsRefused++;
1735 log(DEBUG,"Couldnt look up the port number for fd %lu (OS BROKEN?!)",incomingSockfd);
1736 shutdown(incomingSockfd,2);
1737 close(incomingSockfd);
1742 /* Something went wrong if we're in here.
1743 * In fact, so wrong, im not quite sure
1744 * what we would do, so for now, its going
1745 * to safely do bugger all.
1752 /* This is never reached -- we hope! */