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 printf("InspIRCd PID: %d\n",getpid());
113 /* close stdin, stdout, stderr */
114 freopen("/dev/null","w",stdout);
115 freopen("/dev/null","w",stderr);
117 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
122 if (getrlimit(RLIMIT_CORE, &rl) == -1)
124 log(DEFAULT,"Failed to getrlimit()!");
129 rl.rlim_cur = rl.rlim_max;
130 if (setrlimit(RLIMIT_CORE, &rl) == -1)
131 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
139 /* Make Sure Modules Are Avaliable!
140 * (BugFix By Craig.. See? I do work! :p)
141 * Modified by brain, requires const char*
142 * to work with other API functions
145 bool FileExists (const char* file)
148 if ((input = fopen (file, "r")) == NULL)
159 /* ConfProcess does the following things to a config line in the following order:
161 * Processes the line for syntax errors as shown below
162 * (1) Line void of quotes or equals (a malformed, illegal tag format)
163 * (2) Odd number of quotes on the line indicating a missing quote
164 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
165 * (4) Spaces between the opening bracket (<) and the keyword
166 * (5) Spaces between a keyword and an equals sign
167 * (6) Spaces between an equals sign and a quote
168 * Removes trailing spaces
169 * Removes leading spaces
170 * Converts tabs to spaces
171 * Turns multiple spaces that are outside of quotes into single spaces
174 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
176 long number_of_quotes = 0;
177 long number_of_equals = 0;
178 bool has_open_bracket = false;
179 bool in_quotes = false;
185 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
186 for (int d = 0; d < strlen(buffer); d++)
187 if ((buffer[d]) == 9)
189 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
190 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
191 // empty lines are syntactically valid
192 if (!strcmp(buffer,""))
194 else if (buffer[0] == '#')
196 for (int c = 0; c < strlen(buffer); c++)
198 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
199 // search and replace later :)
200 if ((!in_quotes) && (buffer[c] == ' '))
202 if ((buffer[c] == '<') && (!in_quotes))
204 has_open_bracket = true;
205 if (strlen(buffer) == 1)
207 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
211 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
213 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
218 if (buffer[c] == '"')
221 in_quotes = (!in_quotes);
223 if ((buffer[c] == '=') && (!in_quotes))
226 if (strlen(buffer) == c)
228 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
232 else if (buffer[c+1] != '"')
234 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
240 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
244 else if (buffer[c-1] == '\xA0')
246 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
252 // no quotes, and no equals. something freaky.
253 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
255 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
259 // odd number of quotes. thats just wrong.
260 if ((number_of_quotes % 2) != 0)
262 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
266 if (number_of_equals < (number_of_quotes/2))
268 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
270 if (number_of_equals > (number_of_quotes/2))
272 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
275 std::string parsedata = buffer;
276 // turn multispace into single space
277 while (parsedata.find("\xA0\xA0") != std::string::npos)
279 parsedata.erase(parsedata.find("\xA0\xA0"),1);
282 // turn our hardspace back into softspace
283 for (int d = 0; d < parsedata.length(); d++)
285 if (parsedata[d] == '\xA0')
289 // and we're done, the line is fine!
293 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
296 errorstream->str("");
298 // first, check that the file exists before we try to do anything with it
299 if (!FileExists(filename))
301 *errorstream << "File " << filename << " not found." << endl;
304 // Fix the chmod of the file to restrict it to the current user and group
305 chmod(filename,0600);
307 FILE* conf = fopen(filename,"r");
313 if (fgets(buffer, MAXBUF, conf))
315 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
317 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
320 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
337 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
339 int EnumConf(std::stringstream *config, const char* tag)
342 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
343 int in_token, in_quotes, tptr, j, idx = 0;
346 const char* buf = config->str().c_str();
348 long len = strlen(buf);
358 if ((c == '#') && (lastc == '\n'))
360 while ((c != '\n') && (bptr<len))
366 if ((c == '<') && (!in_quotes))
381 in_quotes = (!in_quotes);
383 if ((c == '>') && (!in_quotes))
386 if (!strcmp(c_tag,tag))
388 /* correct tag, but wrong index */
398 if ((in_token) && (c != '\n') && (c != '\r'))
408 /* Counts the number of values within a certain tag */
410 int EnumValues(std::stringstream *config, const char* tag, int index)
413 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
414 int in_token, in_quotes, tptr, j, idx = 0;
417 bool correct_tag = false;
420 const char* buf = config->str().c_str();
422 long len = strlen(buf);
432 if ((c == '#') && (lastc == '\n'))
434 while ((c != '\n') && (bptr<len))
440 if ((c == '<') && (!in_quotes))
451 if ((!strcmp(c_tag,tag)) && (idx == index))
460 in_quotes = (!in_quotes);
463 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
467 if ((c == '>') && (!in_quotes))
472 if (!strcmp(c_tag,tag))
474 /* correct tag, but wrong index */
484 if ((in_token) && (c != '\n') && (c != '\r'))
496 int ConfValueEnum(char* tag, std::stringstream* config)
498 return EnumConf(config,tag);
503 /* Retrieves a value from the config file. If there is more than one value of the specified
504 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
506 * ConfValue("oper","name",2,result);
509 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
512 char buffer[65535], c_tag[MAXBUF], c, lastc;
513 int in_token, in_quotes, tptr, j, idx = 0;
516 const char* buf = config->str().c_str();
518 long len = strlen(buf);
530 // FIX: Treat tabs as spaces
533 if ((c == '<') && (!in_quotes))
544 // FIX: Tab can follow a tagname as well as space.
545 } while ((c != ' ') && (c != 9));
549 in_quotes = (!in_quotes);
551 if ((c == '>') && (!in_quotes))
556 if (!strcmp(c_tag,tag))
558 if ((buffer) && (c_tag) && (var))
560 key = strstr(buffer,var);
563 /* value not found in tag */
581 for (j = 0; j < strlen(key); j++)
588 strlcpy(result,key,MAXBUF);
594 if (!strcmp(c_tag,tag))
596 /* correct tag, but wrong index */
606 if ((in_token) && (c != '\n') && (c != '\r'))
613 strcpy(result,""); // value or its tag not found at all
619 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
621 ReadConf(config, tag, var, index, result);
627 // This will bind a socket to a port. It works for UDP/TCP
628 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
630 bzero((char *)&server,sizeof(server));
632 inet_aton(addr,&addy);
633 server.sin_family = AF_INET;
634 if (!strcmp(addr,""))
636 server.sin_addr.s_addr = htonl(INADDR_ANY);
640 server.sin_addr = addy;
642 server.sin_port = htons(port);
643 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
656 int OpenTCPSocket (void)
660 struct linger linger = { 0 };
662 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
666 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
667 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
670 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));