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.");
46 // close down all listening sockets
47 for (int count = 0; count < boundPortCount; count++)
49 shutdown(openSockfd[count], 2);
55 void Killed(int status)
59 send_error("Server terminated.");
60 // close down all listening sockets
61 for (int count = 0; count < boundPortCount; count++)
63 shutdown(openSockfd[count], 2);
68 void Rehash(int status)
70 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
71 ReadConfig(false,NULL);
78 printf("\033[1mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
79 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
80 printf("Developers:\033[1m Brain, FrostyCoolSlug\n");
81 printf("Documentation:\033[1m FrostyCoolSlug, w00t\n");
82 printf("Testers:\033[1m typobox43, piggles, Lord_Zathras, CC\n");
83 printf("Name concept:\033[1m Lord_Zathras\n\n");
86 void WritePID(std::string filename)
88 ofstream outfile(filename.c_str());
89 if (outfile.is_open())
96 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
97 log(DEFAULT,"Failed to write PID-file '%s', exiting.",filename.c_str());
102 void DeadPipe(int status)
104 signal (SIGPIPE, DeadPipe);
107 int DaemonSeed (void)
110 signal (SIGALRM, SIG_IGN);
111 signal (SIGHUP, Rehash);
112 signal (SIGPIPE, DeadPipe);
113 signal (SIGTERM, Exit);
114 signal (SIGABRT, Exit);
115 signal (SIGSEGV, Error);
116 signal (SIGURG, Exit);
117 signal (SIGKILL, Exit);
118 if ((childpid = fork ()) < 0)
120 else if (childpid > 0)
124 /* close stdin, stdout, stderr */
125 freopen("/dev/null","w",stdout);
126 freopen("/dev/null","w",stderr);
128 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
133 if (getrlimit(RLIMIT_CORE, &rl) == -1)
135 log(DEFAULT,"Failed to getrlimit()!");
140 rl.rlim_cur = rl.rlim_max;
141 if (setrlimit(RLIMIT_CORE, &rl) == -1)
142 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
150 /* Make Sure Modules Are Avaliable!
151 * (BugFix By Craig.. See? I do work! :p)
152 * Modified by brain, requires const char*
153 * to work with other API functions
156 bool FileExists (const char* file)
159 if ((input = fopen (file, "r")) == NULL)
170 /* ConfProcess does the following things to a config line in the following order:
172 * Processes the line for syntax errors as shown below
173 * (1) Line void of quotes or equals (a malformed, illegal tag format)
174 * (2) Odd number of quotes on the line indicating a missing quote
175 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
176 * (4) Spaces between the opening bracket (<) and the keyword
177 * (5) Spaces between a keyword and an equals sign
178 * (6) Spaces between an equals sign and a quote
179 * Removes trailing spaces
180 * Removes leading spaces
181 * Converts tabs to spaces
182 * Turns multiple spaces that are outside of quotes into single spaces
185 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
187 long number_of_quotes = 0;
188 long number_of_equals = 0;
189 bool has_open_bracket = false;
190 bool in_quotes = false;
196 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
197 for (int d = 0; d < strlen(buffer); d++)
198 if ((buffer[d]) == 9)
200 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
201 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
202 // empty lines are syntactically valid
203 if (!strcmp(buffer,""))
205 else if (buffer[0] == '#')
207 for (int c = 0; c < strlen(buffer); c++)
209 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
210 // search and replace later :)
211 if ((!in_quotes) && (buffer[c] == ' '))
213 if ((buffer[c] == '<') && (!in_quotes))
215 has_open_bracket = true;
216 if (strlen(buffer) == 1)
218 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
222 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
224 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
229 if (buffer[c] == '"')
232 in_quotes = (!in_quotes);
234 if ((buffer[c] == '=') && (!in_quotes))
237 if (strlen(buffer) == c)
239 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
243 else if (buffer[c+1] != '"')
245 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
251 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
255 else if (buffer[c-1] == '\xA0')
257 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
263 // no quotes, and no equals. something freaky.
264 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
266 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
270 // odd number of quotes. thats just wrong.
271 if ((number_of_quotes % 2) != 0)
273 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
277 if (number_of_equals < (number_of_quotes/2))
279 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
281 if (number_of_equals > (number_of_quotes/2))
283 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
286 std::string parsedata = buffer;
287 // turn multispace into single space
288 while (parsedata.find("\xA0\xA0") != std::string::npos)
290 parsedata.erase(parsedata.find("\xA0\xA0"),1);
293 // turn our hardspace back into softspace
294 for (int d = 0; d < parsedata.length(); d++)
296 if (parsedata[d] == '\xA0')
300 // and we're done, the line is fine!
304 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
307 errorstream->str("");
309 // first, check that the file exists before we try to do anything with it
310 if (!FileExists(filename))
312 *errorstream << "File " << filename << " not found." << endl;
315 // Fix the chmod of the file to restrict it to the current user and group
316 chmod(filename,0600);
318 FILE* conf = fopen(filename,"r");
324 if (fgets(buffer, MAXBUF, conf))
326 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
328 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
331 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
348 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
350 int EnumConf(std::stringstream *config, const char* tag)
353 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
354 int in_token, in_quotes, tptr, j, idx = 0;
357 const char* buf = config->str().c_str();
359 long len = strlen(buf);
369 if ((c == '#') && (lastc == '\n'))
371 while ((c != '\n') && (bptr<len))
377 if ((c == '<') && (!in_quotes))
392 in_quotes = (!in_quotes);
394 if ((c == '>') && (!in_quotes))
397 if (!strcmp(c_tag,tag))
399 /* correct tag, but wrong index */
409 if ((in_token) && (c != '\n') && (c != '\r'))
419 /* Counts the number of values within a certain tag */
421 int EnumValues(std::stringstream *config, const char* tag, int index)
424 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
425 int in_token, in_quotes, tptr, j, idx = 0;
428 bool correct_tag = false;
431 const char* buf = config->str().c_str();
433 long len = strlen(buf);
443 if ((c == '#') && (lastc == '\n'))
445 while ((c != '\n') && (bptr<len))
451 if ((c == '<') && (!in_quotes))
462 if ((!strcmp(c_tag,tag)) && (idx == index))
471 in_quotes = (!in_quotes);
474 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
478 if ((c == '>') && (!in_quotes))
483 if (!strcmp(c_tag,tag))
485 /* correct tag, but wrong index */
495 if ((in_token) && (c != '\n') && (c != '\r'))
507 int ConfValueEnum(char* tag, std::stringstream* config)
509 return EnumConf(config,tag);
514 /* Retrieves a value from the config file. If there is more than one value of the specified
515 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
517 * ConfValue("oper","name",2,result);
520 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
523 char buffer[65535], c_tag[MAXBUF], c, lastc;
524 int in_token, in_quotes, tptr, j, idx = 0;
527 const char* buf = config->str().c_str();
529 long len = strlen(buf);
541 // FIX: Treat tabs as spaces
544 if ((c == '<') && (!in_quotes))
555 // FIX: Tab can follow a tagname as well as space.
556 } while ((c != ' ') && (c != 9));
560 in_quotes = (!in_quotes);
562 if ((c == '>') && (!in_quotes))
567 if (!strcmp(c_tag,tag))
569 if ((buffer) && (c_tag) && (var))
571 key = strstr(buffer,var);
574 /* value not found in tag */
592 for (j = 0; j < strlen(key); j++)
599 strlcpy(result,key,MAXBUF);
605 if (!strcmp(c_tag,tag))
607 /* correct tag, but wrong index */
617 if ((in_token) && (c != '\n') && (c != '\r'))
624 strcpy(result,""); // value or its tag not found at all
630 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
632 ReadConf(config, tag, var, index, result);
638 // This will bind a socket to a port. It works for UDP/TCP
639 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
641 bzero((char *)&server,sizeof(server));
643 inet_aton(addr,&addy);
644 server.sin_family = AF_INET;
645 if (!strcmp(addr,""))
647 server.sin_addr.s_addr = htonl(INADDR_ANY);
651 server.sin_addr = addy;
653 server.sin_port = htons(port);
654 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
667 int OpenTCPSocket (void)
671 struct linger linger = { 0 };
673 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
677 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
678 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
681 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));