1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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 * ---------------------------------------------------
18 #include <sys/resource.h>
19 #include <sys/types.h>
26 #include "inspircd_io.h"
27 #include "inspircd_util.h"
28 #include "inspstring.h"
32 extern FILE *log_file;
33 extern int boundPortCount;
34 extern int openSockfd[MAXSOCKS];
36 extern bool unlimitcore;
38 void WriteOpers(char* text, ...);
40 void Exit (int status)
44 send_error("Server shutdown.");
48 void Killed(int status)
52 send_error("Server terminated.");
56 void Rehash(int status)
58 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
59 ReadConfig(false,NULL);
66 printf("\033[1mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
67 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
68 printf("Developers:\033[1m Brain, FrostyCoolSlug\n");
69 printf("Documentation:\033[1m FrostyCoolSlug, w00t\n");
70 printf("Testers:\033[1m typobox43, piggles, Lord_Zathras, CC\n");
71 printf("Name concept:\033[1m Lord_Zathras\n\n");
74 void WritePID(std::string filename)
76 ofstream outfile(filename.c_str());
77 if (outfile.is_open())
84 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
85 log(DEFAULT,"Failed to write PID-file '%s', exiting.",filename.c_str());
90 void DeadPipe(int status)
92 signal (SIGPIPE, DeadPipe);
98 signal (SIGALRM, SIG_IGN);
99 signal (SIGHUP, Rehash);
100 signal (SIGPIPE, DeadPipe);
101 signal (SIGTERM, Exit);
102 signal (SIGABRT, Exit);
103 signal (SIGSEGV, Error);
104 signal (SIGURG, Exit);
105 signal (SIGKILL, Exit);
106 if ((childpid = fork ()) < 0)
108 else if (childpid > 0)
112 /* close stdin, stdout, stderr */
113 freopen("/dev/null","w",stdout);
114 freopen("/dev/null","w",stderr);
116 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
121 if (getrlimit(RLIMIT_CORE, &rl) == -1)
123 log(DEFAULT,"Failed to getrlimit()!");
128 rl.rlim_cur = rl.rlim_max;
129 if (setrlimit(RLIMIT_CORE, &rl) == -1)
130 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
138 /* Make Sure Modules Are Avaliable!
139 * (BugFix By Craig.. See? I do work! :p)
140 * Modified by brain, requires const char*
141 * to work with other API functions
144 bool FileExists (const char* file)
147 if ((input = fopen (file, "r")) == NULL)
158 /* ConfProcess does the following things to a config line in the following order:
160 * Processes the line for syntax errors as shown below
161 * (1) Line void of quotes or equals (a malformed, illegal tag format)
162 * (2) Odd number of quotes on the line indicating a missing quote
163 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
164 * (4) Spaces between the opening bracket (<) and the keyword
165 * (5) Spaces between a keyword and an equals sign
166 * (6) Spaces between an equals sign and a quote
167 * Removes trailing spaces
168 * Removes leading spaces
169 * Converts tabs to spaces
170 * Turns multiple spaces that are outside of quotes into single spaces
173 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
175 long number_of_quotes = 0;
176 long number_of_equals = 0;
177 bool has_open_bracket = false;
178 bool in_quotes = false;
184 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
185 for (int d = 0; d < strlen(buffer); d++)
186 if ((buffer[d]) == 9)
188 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
189 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
190 // empty lines are syntactically valid
191 if (!strcmp(buffer,""))
193 else if (buffer[0] == '#')
195 for (int c = 0; c < strlen(buffer); c++)
197 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
198 // search and replace later :)
199 if ((!in_quotes) && (buffer[c] == ' '))
201 if ((buffer[c] == '<') && (!in_quotes))
203 has_open_bracket = true;
204 if (strlen(buffer) == 1)
206 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
210 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
212 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
217 if (buffer[c] == '"')
220 in_quotes = (!in_quotes);
222 if ((buffer[c] == '=') && (!in_quotes))
225 if (strlen(buffer) == c)
227 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
231 else if (buffer[c+1] != '"')
233 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
239 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
243 else if (buffer[c-1] == '\xA0')
245 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
251 // no quotes, and no equals. something freaky.
252 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
254 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
258 // odd number of quotes. thats just wrong.
259 if ((number_of_quotes % 2) != 0)
261 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
265 if (number_of_equals < (number_of_quotes/2))
267 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
269 if (number_of_equals > (number_of_quotes/2))
271 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
274 std::string parsedata = buffer;
275 // turn multispace into single space
276 while (parsedata.find("\xA0\xA0") != std::string::npos)
278 parsedata.erase(parsedata.find("\xA0\xA0"),1);
281 // turn our hardspace back into softspace
282 for (int d = 0; d < parsedata.length(); d++)
284 if (parsedata[d] == '\xA0')
288 // and we're done, the line is fine!
292 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
295 errorstream->str("");
297 // first, check that the file exists before we try to do anything with it
298 if (!FileExists(filename))
300 *errorstream << "File " << filename << " not found." << endl;
303 // Fix the chmod of the file to restrict it to the current user and group
304 chmod(filename,0600);
306 FILE* conf = fopen(filename,"r");
312 if (fgets(buffer, MAXBUF, conf))
314 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
316 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
319 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
336 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
338 int EnumConf(std::stringstream *config, const char* tag)
341 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
342 int in_token, in_quotes, tptr, j, idx = 0;
345 const char* buf = config->str().c_str();
347 long len = strlen(buf);
357 if ((c == '#') && (lastc == '\n'))
359 while ((c != '\n') && (bptr<len))
365 if ((c == '<') && (!in_quotes))
380 in_quotes = (!in_quotes);
382 if ((c == '>') && (!in_quotes))
385 if (!strcmp(c_tag,tag))
387 /* correct tag, but wrong index */
397 if ((in_token) && (c != '\n') && (c != '\r'))
407 /* Counts the number of values within a certain tag */
409 int EnumValues(std::stringstream *config, const char* tag, int index)
412 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
413 int in_token, in_quotes, tptr, j, idx = 0;
416 bool correct_tag = false;
419 const char* buf = config->str().c_str();
421 long len = strlen(buf);
431 if ((c == '#') && (lastc == '\n'))
433 while ((c != '\n') && (bptr<len))
439 if ((c == '<') && (!in_quotes))
450 if ((!strcmp(c_tag,tag)) && (idx == index))
459 in_quotes = (!in_quotes);
462 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
466 if ((c == '>') && (!in_quotes))
471 if (!strcmp(c_tag,tag))
473 /* correct tag, but wrong index */
483 if ((in_token) && (c != '\n') && (c != '\r'))
495 int ConfValueEnum(char* tag, std::stringstream* config)
497 return EnumConf(config,tag);
502 /* Retrieves a value from the config file. If there is more than one value of the specified
503 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
505 * ConfValue("oper","name",2,result);
508 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
511 char buffer[65535], c_tag[MAXBUF], c, lastc;
512 int in_token, in_quotes, tptr, j, idx = 0;
515 const char* buf = config->str().c_str();
517 long len = strlen(buf);
529 // FIX: Treat tabs as spaces
532 if ((c == '<') && (!in_quotes))
543 // FIX: Tab can follow a tagname as well as space.
544 } while ((c != ' ') && (c != 9));
548 in_quotes = (!in_quotes);
550 if ((c == '>') && (!in_quotes))
555 if (!strcmp(c_tag,tag))
557 if ((buffer) && (c_tag) && (var))
559 key = strstr(buffer,var);
562 /* value not found in tag */
580 for (j = 0; j < strlen(key); j++)
587 strlcpy(result,key,MAXBUF);
593 if (!strcmp(c_tag,tag))
595 /* correct tag, but wrong index */
605 if ((in_token) && (c != '\n') && (c != '\r'))
612 strcpy(result,""); // value or its tag not found at all
618 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
620 ReadConf(config, tag, var, index, result);
626 // This will bind a socket to a port. It works for UDP/TCP
627 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
629 bzero((char *)&server,sizeof(server));
631 inet_aton(addr,&addy);
632 server.sin_family = AF_INET;
633 if (!strcmp(addr,""))
635 server.sin_addr.s_addr = htonl(INADDR_ANY);
639 server.sin_addr = addy;
641 server.sin_port = htons(port);
642 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
655 int OpenTCPSocket (void)
659 struct linger linger = { 0 };
661 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
665 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
666 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
669 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));