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 * ---------------------------------------------------
20 #include <sys/resource.h>
21 #include <sys/types.h>
28 #include "inspircd_io.h"
29 #include "inspircd_util.h"
30 #include "inspstring.h"
31 #include "helperfuncs.h"
33 extern FILE *log_file;
34 extern int boundPortCount;
35 extern int openSockfd[MAXSOCKS];
37 extern bool unlimitcore;
39 void WriteOpers(char* text, ...);
41 void Exit (int status)
45 send_error("Server shutdown.");
49 void Killed(int status)
53 send_error("Server terminated.");
57 void Rehash(int status)
59 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
60 ReadConfig(false,NULL);
67 printf("\033[1;32mInspire Internet Relay Chat Server, compiled %s at %s\n",__DATE__,__TIME__);
68 printf("(C) ChatSpike Development team.\033[0m\n\n");
69 printf("Developers:\033[1;32m Brain, FrostyCoolSlug\033[0m\n");
70 printf("Documentation:\033[1;32m FrostyCoolSlug, w00t\033[0m\n");
71 printf("Testers:\033[1;32m typobox43, piggles, Lord_Zathras, CC\033[0m\n");
72 printf("Name concept:\033[1;32m Lord_Zathras\033[0m\n\n");
75 void WritePID(std::string filename)
77 ofstream outfile(filename.c_str());
78 if (outfile.is_open())
85 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
86 log(DEFAULT,"Failed to write PID-file '%s', exiting.",filename.c_str());
95 signal (SIGALRM, SIG_IGN);
96 signal (SIGHUP, Rehash);
97 signal (SIGPIPE, SIG_IGN);
98 signal (SIGTERM, Exit);
99 signal (SIGSEGV, Error);
100 if ((childpid = fork ()) < 0)
102 else if (childpid > 0)
106 printf("InspIRCd Process ID: \033[1;32m%lu\033[0m\n",(unsigned long)getpid());
107 freopen("/dev/null","w",stdout);
108 freopen("/dev/null","w",stderr);
110 setpriority(PRIO_PROCESS,(int)getpid(),15);
115 if (getrlimit(RLIMIT_CORE, &rl) == -1)
117 log(DEFAULT,"Failed to getrlimit()!");
122 rl.rlim_cur = rl.rlim_max;
123 if (setrlimit(RLIMIT_CORE, &rl) == -1)
124 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
132 /* Make Sure Modules Are Avaliable!
133 * (BugFix By Craig.. See? I do work! :p)
134 * Modified by brain, requires const char*
135 * to work with other API functions
138 bool FileExists (const char* file)
141 if ((input = fopen (file, "r")) == NULL)
152 /* ConfProcess does the following things to a config line in the following order:
154 * Processes the line for syntax errors as shown below
155 * (1) Line void of quotes or equals (a malformed, illegal tag format)
156 * (2) Odd number of quotes on the line indicating a missing quote
157 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
158 * (4) Spaces between the opening bracket (<) and the keyword
159 * (5) Spaces between a keyword and an equals sign
160 * (6) Spaces between an equals sign and a quote
161 * Removes trailing spaces
162 * Removes leading spaces
163 * Converts tabs to spaces
164 * Turns multiple spaces that are outside of quotes into single spaces
167 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
169 long number_of_quotes = 0;
170 long number_of_equals = 0;
171 bool has_open_bracket = false;
172 bool in_quotes = false;
178 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
179 for (int d = 0; d < strlen(buffer); d++)
180 if ((buffer[d]) == 9)
182 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
183 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
184 // empty lines are syntactically valid
185 if (!strcmp(buffer,""))
187 else if (buffer[0] == '#')
189 for (int c = 0; c < strlen(buffer); c++)
191 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
192 // search and replace later :)
193 if ((!in_quotes) && (buffer[c] == ' '))
195 if ((buffer[c] == '<') && (!in_quotes))
197 has_open_bracket = true;
198 if (strlen(buffer) == 1)
200 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
204 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
206 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
211 if (buffer[c] == '"')
214 in_quotes = (!in_quotes);
216 if ((buffer[c] == '=') && (!in_quotes))
219 if (strlen(buffer) == c)
221 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
225 else if (buffer[c+1] != '"')
227 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
233 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
237 else if (buffer[c-1] == '\xA0')
239 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
245 // no quotes, and no equals. something freaky.
246 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
248 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
252 // odd number of quotes. thats just wrong.
253 if ((number_of_quotes % 2) != 0)
255 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
259 if (number_of_equals < (number_of_quotes/2))
261 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
263 if (number_of_equals > (number_of_quotes/2))
265 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
268 std::string parsedata = buffer;
269 // turn multispace into single space
270 while (parsedata.find("\xA0\xA0") != std::string::npos)
272 parsedata.erase(parsedata.find("\xA0\xA0"),1);
275 // turn our hardspace back into softspace
276 for (int d = 0; d < parsedata.length(); d++)
278 if (parsedata[d] == '\xA0')
282 // and we're done, the line is fine!
286 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
289 errorstream->str("");
291 // first, check that the file exists before we try to do anything with it
292 if (!FileExists(filename))
294 *errorstream << "File " << filename << " not found." << endl;
297 // Fix the chmod of the file to restrict it to the current user and group
298 chmod(filename,0600);
300 FILE* conf = fopen(filename,"r");
306 if (fgets(buffer, MAXBUF, conf))
308 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
310 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
313 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
330 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
332 int EnumConf(std::stringstream *config, const char* tag)
335 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
336 int in_token, in_quotes, tptr, idx = 0;
338 const char* buf = config->str().c_str();
340 long len = strlen(buf);
350 if ((c == '#') && (lastc == '\n'))
352 while ((c != '\n') && (bptr<len))
358 if ((c == '<') && (!in_quotes))
373 in_quotes = (!in_quotes);
375 if ((c == '>') && (!in_quotes))
378 if (!strcmp(c_tag,tag))
380 /* correct tag, but wrong index */
390 if ((in_token) && (c != '\n') && (c != '\r'))
400 /* Counts the number of values within a certain tag */
402 int EnumValues(std::stringstream *config, const char* tag, int index)
405 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
406 int in_token, in_quotes, tptr, idx = 0;
408 bool correct_tag = false;
411 const char* buf = config->str().c_str();
413 long len = strlen(buf);
423 if ((c == '#') && (lastc == '\n'))
425 while ((c != '\n') && (bptr<len))
431 if ((c == '<') && (!in_quotes))
442 if ((!strcmp(c_tag,tag)) && (idx == index))
451 in_quotes = (!in_quotes);
454 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
458 if ((c == '>') && (!in_quotes))
463 if (!strcmp(c_tag,tag))
465 /* correct tag, but wrong index */
475 if ((in_token) && (c != '\n') && (c != '\r'))
487 int ConfValueEnum(char* tag, std::stringstream* config)
489 return EnumConf(config,tag);
494 /* Retrieves a value from the config file. If there is more than one value of the specified
495 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
497 * ConfValue("oper","name",2,result);
500 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
503 char buffer[65535], c_tag[MAXBUF], c, lastc;
504 int in_token, in_quotes, tptr, j, idx = 0;
507 const char* buf = config->str().c_str();
509 long len = config->str().length();
521 // FIX: Treat tabs as spaces
524 if ((c == '<') && (!in_quotes))
535 // FIX: Tab can follow a tagname as well as space.
536 } while ((c != ' ') && (c != 9));
540 in_quotes = (!in_quotes);
542 if ((c == '>') && (!in_quotes))
547 if (!strcmp(c_tag,tag))
549 if ((buffer) && (c_tag) && (var))
551 key = strstr(buffer,var);
554 /* value not found in tag */
572 for (j = 0; j < strlen(key); j++)
579 strlcpy(result,key,MAXBUF);
585 if (!strcmp(c_tag,tag))
587 /* correct tag, but wrong index */
597 if ((in_token) && (c != '\n') && (c != '\r'))
604 strcpy(result,""); // value or its tag not found at all
610 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
612 ReadConf(config, tag, var, index, result);
618 // This will bind a socket to a port. It works for UDP/TCP
619 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
621 bzero((char *)&server,sizeof(server));
623 inet_aton(addr,&addy);
624 server.sin_family = AF_INET;
625 if (!strcmp(addr,""))
627 server.sin_addr.s_addr = htonl(INADDR_ANY);
631 server.sin_addr = addy;
633 server.sin_port = htons(port);
634 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
647 int OpenTCPSocket (void)
651 struct linger linger = { 0 };
653 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
657 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
658 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
661 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));